2 * Copyright (c) 2010 Broadcom Corporation
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <linux/types.h>
18 #include <linux/kernel.h>
19 #include <linux/kthread.h>
20 #include <linux/printk.h>
21 #include <linux/pci_ids.h>
22 #include <linux/netdevice.h>
23 #include <linux/interrupt.h>
24 #include <linux/sched.h>
25 #include <linux/mmc/sdio.h>
26 #include <linux/mmc/sdio_func.h>
27 #include <linux/mmc/card.h>
28 #include <linux/semaphore.h>
29 #include <linux/firmware.h>
30 #include <linux/module.h>
31 #include <asm/unaligned.h>
33 #include <brcmu_wifi.h>
34 #include <brcmu_utils.h>
35 #include <brcm_hw_ids.h>
37 #include "sdio_host.h"
39 #define DCMD_RESP_TIMEOUT 2000 /* In milli second */
43 #define BRCMF_TRAP_INFO_SIZE 80
45 #define CBUF_LEN (128)
48 __le32 buf
; /* Can't be pointer on (64-bit) hosts */
51 char *_buf_compat
; /* Redundant pointer for backward compat. */
56 * When there is no UART (e.g. Quickturn),
57 * the host should write a complete
58 * input line directly into cbuf and then write
59 * the length into vcons_in.
60 * This may also be used when there is a real UART
61 * (at risk of conflicting with
62 * the real UART). vcons_out is currently unused.
67 /* Output (logging) buffer
68 * Console output is written to a ring buffer log_buf at index log_idx.
69 * The host may read the output when it sees log_idx advance.
70 * Output will be lost if the output wraps around faster than the host
73 struct rte_log_le log_le
;
75 /* Console input line buffer
76 * Characters are read one at a time into cbuf
77 * until <CR> is received, then
78 * the buffer is processed as a command line.
79 * Also used for virtual UART.
86 #include <chipcommon.h>
90 #include "dhd_proto.h"
94 #define TXQLEN 2048 /* bulk tx queue length */
95 #define TXHI (TXQLEN - 256) /* turn on flow control above TXHI */
96 #define TXLOW (TXHI - 256) /* turn off flow control below TXLOW */
99 #define TXRETRIES 2 /* # of retries for tx frames */
101 #define BRCMF_RXBOUND 50 /* Default for max rx frames in
104 #define BRCMF_TXBOUND 20 /* Default for max tx frames in
107 #define BRCMF_TXMINMAX 1 /* Max tx frames if rx still pending */
109 #define MEMBLOCK 2048 /* Block size used for downloading
111 #define MAX_DATA_BUF (32 * 1024) /* Must be large enough to hold
112 biggest possible glom */
114 #define BRCMF_FIRSTREAD (1 << 6)
117 /* SBSDIO_DEVICE_CTL */
119 /* 1: device will assert busy signal when receiving CMD53 */
120 #define SBSDIO_DEVCTL_SETBUSY 0x01
121 /* 1: assertion of sdio interrupt is synchronous to the sdio clock */
122 #define SBSDIO_DEVCTL_SPI_INTR_SYNC 0x02
123 /* 1: mask all interrupts to host except the chipActive (rev 8) */
124 #define SBSDIO_DEVCTL_CA_INT_ONLY 0x04
125 /* 1: isolate internal sdio signals, put external pads in tri-state; requires
126 * sdio bus power cycle to clear (rev 9) */
127 #define SBSDIO_DEVCTL_PADS_ISO 0x08
128 /* Force SD->SB reset mapping (rev 11) */
129 #define SBSDIO_DEVCTL_SB_RST_CTL 0x30
130 /* Determined by CoreControl bit */
131 #define SBSDIO_DEVCTL_RST_CORECTL 0x00
132 /* Force backplane reset */
133 #define SBSDIO_DEVCTL_RST_BPRESET 0x10
134 /* Force no backplane reset */
135 #define SBSDIO_DEVCTL_RST_NOBPRESET 0x20
137 /* SBSDIO_FUNC1_CHIPCLKCSR */
139 /* Force ALP request to backplane */
140 #define SBSDIO_FORCE_ALP 0x01
141 /* Force HT request to backplane */
142 #define SBSDIO_FORCE_HT 0x02
143 /* Force ILP request to backplane */
144 #define SBSDIO_FORCE_ILP 0x04
145 /* Make ALP ready (power up xtal) */
146 #define SBSDIO_ALP_AVAIL_REQ 0x08
147 /* Make HT ready (power up PLL) */
148 #define SBSDIO_HT_AVAIL_REQ 0x10
149 /* Squelch clock requests from HW */
150 #define SBSDIO_FORCE_HW_CLKREQ_OFF 0x20
151 /* Status: ALP is ready */
152 #define SBSDIO_ALP_AVAIL 0x40
153 /* Status: HT is ready */
154 #define SBSDIO_HT_AVAIL 0x80
156 #define SBSDIO_AVBITS (SBSDIO_HT_AVAIL | SBSDIO_ALP_AVAIL)
157 #define SBSDIO_ALPAV(regval) ((regval) & SBSDIO_AVBITS)
158 #define SBSDIO_HTAV(regval) (((regval) & SBSDIO_AVBITS) == SBSDIO_AVBITS)
159 #define SBSDIO_ALPONLY(regval) (SBSDIO_ALPAV(regval) && !SBSDIO_HTAV(regval))
161 #define SBSDIO_CLKAV(regval, alponly) \
162 (SBSDIO_ALPAV(regval) && (alponly ? 1 : SBSDIO_HTAV(regval)))
164 /* direct(mapped) cis space */
166 /* MAPPED common CIS address */
167 #define SBSDIO_CIS_BASE_COMMON 0x1000
168 /* maximum bytes in one CIS */
169 #define SBSDIO_CIS_SIZE_LIMIT 0x200
170 /* cis offset addr is < 17 bits */
171 #define SBSDIO_CIS_OFT_ADDR_MASK 0x1FFFF
173 /* manfid tuple length, include tuple, link bytes */
174 #define SBSDIO_CIS_MANFID_TUPLE_LEN 6
177 #define I_SMB_SW0 (1 << 0) /* To SB Mail S/W interrupt 0 */
178 #define I_SMB_SW1 (1 << 1) /* To SB Mail S/W interrupt 1 */
179 #define I_SMB_SW2 (1 << 2) /* To SB Mail S/W interrupt 2 */
180 #define I_SMB_SW3 (1 << 3) /* To SB Mail S/W interrupt 3 */
181 #define I_SMB_SW_MASK 0x0000000f /* To SB Mail S/W interrupts mask */
182 #define I_SMB_SW_SHIFT 0 /* To SB Mail S/W interrupts shift */
183 #define I_HMB_SW0 (1 << 4) /* To Host Mail S/W interrupt 0 */
184 #define I_HMB_SW1 (1 << 5) /* To Host Mail S/W interrupt 1 */
185 #define I_HMB_SW2 (1 << 6) /* To Host Mail S/W interrupt 2 */
186 #define I_HMB_SW3 (1 << 7) /* To Host Mail S/W interrupt 3 */
187 #define I_HMB_SW_MASK 0x000000f0 /* To Host Mail S/W interrupts mask */
188 #define I_HMB_SW_SHIFT 4 /* To Host Mail S/W interrupts shift */
189 #define I_WR_OOSYNC (1 << 8) /* Write Frame Out Of Sync */
190 #define I_RD_OOSYNC (1 << 9) /* Read Frame Out Of Sync */
191 #define I_PC (1 << 10) /* descriptor error */
192 #define I_PD (1 << 11) /* data error */
193 #define I_DE (1 << 12) /* Descriptor protocol Error */
194 #define I_RU (1 << 13) /* Receive descriptor Underflow */
195 #define I_RO (1 << 14) /* Receive fifo Overflow */
196 #define I_XU (1 << 15) /* Transmit fifo Underflow */
197 #define I_RI (1 << 16) /* Receive Interrupt */
198 #define I_BUSPWR (1 << 17) /* SDIO Bus Power Change (rev 9) */
199 #define I_XMTDATA_AVAIL (1 << 23) /* bits in fifo */
200 #define I_XI (1 << 24) /* Transmit Interrupt */
201 #define I_RF_TERM (1 << 25) /* Read Frame Terminate */
202 #define I_WF_TERM (1 << 26) /* Write Frame Terminate */
203 #define I_PCMCIA_XU (1 << 27) /* PCMCIA Transmit FIFO Underflow */
204 #define I_SBINT (1 << 28) /* sbintstatus Interrupt */
205 #define I_CHIPACTIVE (1 << 29) /* chip from doze to active state */
206 #define I_SRESET (1 << 30) /* CCCR RES interrupt */
207 #define I_IOE2 (1U << 31) /* CCCR IOE2 Bit Changed */
208 #define I_ERRORS (I_PC | I_PD | I_DE | I_RU | I_RO | I_XU)
209 #define I_DMA (I_RI | I_XI | I_ERRORS)
212 #define CC_CISRDY (1 << 0) /* CIS Ready */
213 #define CC_BPRESEN (1 << 1) /* CCCR RES signal */
214 #define CC_F2RDY (1 << 2) /* set CCCR IOR2 bit */
215 #define CC_CLRPADSISO (1 << 3) /* clear SDIO pads isolation */
216 #define CC_XMTDATAAVAIL_MODE (1 << 4)
217 #define CC_XMTDATAAVAIL_CTRL (1 << 5)
220 #define SFC_RF_TERM (1 << 0) /* Read Frame Terminate */
221 #define SFC_WF_TERM (1 << 1) /* Write Frame Terminate */
222 #define SFC_CRC4WOOS (1 << 2) /* CRC error for write out of sync */
223 #define SFC_ABORTALL (1 << 3) /* Abort all in-progress frames */
226 #define SDPCM_FRAMETAG_LEN 4 /* 2 bytes len, 2 bytes check val */
228 /* Total length of frame header for dongle protocol */
229 #define SDPCM_HDRLEN (SDPCM_FRAMETAG_LEN + SDPCM_SWHEADER_LEN)
230 #define SDPCM_RESERVE (SDPCM_HDRLEN + BRCMF_SDALIGN)
233 * Software allocation of To SB Mailbox resources
236 /* tosbmailbox bits corresponding to intstatus bits */
237 #define SMB_NAK (1 << 0) /* Frame NAK */
238 #define SMB_INT_ACK (1 << 1) /* Host Interrupt ACK */
239 #define SMB_USE_OOB (1 << 2) /* Use OOB Wakeup */
240 #define SMB_DEV_INT (1 << 3) /* Miscellaneous Interrupt */
242 /* tosbmailboxdata */
243 #define SMB_DATA_VERSION_SHIFT 16 /* host protocol version */
246 * Software allocation of To Host Mailbox resources
250 #define I_HMB_FC_STATE I_HMB_SW0 /* Flow Control State */
251 #define I_HMB_FC_CHANGE I_HMB_SW1 /* Flow Control State Changed */
252 #define I_HMB_FRAME_IND I_HMB_SW2 /* Frame Indication */
253 #define I_HMB_HOST_INT I_HMB_SW3 /* Miscellaneous Interrupt */
255 /* tohostmailboxdata */
256 #define HMB_DATA_NAKHANDLED 1 /* retransmit NAK'd frame */
257 #define HMB_DATA_DEVREADY 2 /* talk to host after enable */
258 #define HMB_DATA_FC 4 /* per prio flowcontrol update flag */
259 #define HMB_DATA_FWREADY 8 /* fw ready for protocol activity */
261 #define HMB_DATA_FCDATA_MASK 0xff000000
262 #define HMB_DATA_FCDATA_SHIFT 24
264 #define HMB_DATA_VERSION_MASK 0x00ff0000
265 #define HMB_DATA_VERSION_SHIFT 16
268 * Software-defined protocol header
271 /* Current protocol version */
272 #define SDPCM_PROT_VERSION 4
274 /* SW frame header */
275 #define SDPCM_PACKET_SEQUENCE(p) (((u8 *)p)[0] & 0xff)
277 #define SDPCM_CHANNEL_MASK 0x00000f00
278 #define SDPCM_CHANNEL_SHIFT 8
279 #define SDPCM_PACKET_CHANNEL(p) (((u8 *)p)[1] & 0x0f)
281 #define SDPCM_NEXTLEN_OFFSET 2
283 /* Data Offset from SOF (HW Tag, SW Tag, Pad) */
284 #define SDPCM_DOFFSET_OFFSET 3 /* Data Offset */
285 #define SDPCM_DOFFSET_VALUE(p) (((u8 *)p)[SDPCM_DOFFSET_OFFSET] & 0xff)
286 #define SDPCM_DOFFSET_MASK 0xff000000
287 #define SDPCM_DOFFSET_SHIFT 24
288 #define SDPCM_FCMASK_OFFSET 4 /* Flow control */
289 #define SDPCM_FCMASK_VALUE(p) (((u8 *)p)[SDPCM_FCMASK_OFFSET] & 0xff)
290 #define SDPCM_WINDOW_OFFSET 5 /* Credit based fc */
291 #define SDPCM_WINDOW_VALUE(p) (((u8 *)p)[SDPCM_WINDOW_OFFSET] & 0xff)
293 #define SDPCM_SWHEADER_LEN 8 /* SW header is 64 bits */
295 /* logical channel numbers */
296 #define SDPCM_CONTROL_CHANNEL 0 /* Control channel Id */
297 #define SDPCM_EVENT_CHANNEL 1 /* Asyc Event Indication Channel Id */
298 #define SDPCM_DATA_CHANNEL 2 /* Data Xmit/Recv Channel Id */
299 #define SDPCM_GLOM_CHANNEL 3 /* For coalesced packets */
300 #define SDPCM_TEST_CHANNEL 15 /* Reserved for test/debug packets */
302 #define SDPCM_SEQUENCE_WRAP 256 /* wrap-around val for 8bit frame seq */
304 #define SDPCM_GLOMDESC(p) (((u8 *)p)[1] & 0x80)
307 * Shared structure between dongle and the host.
308 * The structure contains pointers to trap or assert information.
310 #define SDPCM_SHARED_VERSION 0x0002
311 #define SDPCM_SHARED_VERSION_MASK 0x00FF
312 #define SDPCM_SHARED_ASSERT_BUILT 0x0100
313 #define SDPCM_SHARED_ASSERT 0x0200
314 #define SDPCM_SHARED_TRAP 0x0400
316 /* Space for header read, limit for data packets */
317 #define MAX_HDR_READ (1 << 6)
318 #define MAX_RX_DATASZ 2048
320 /* Maximum milliseconds to wait for F2 to come up */
321 #define BRCMF_WAIT_F2RDY 3000
323 /* Bump up limit on waiting for HT to account for first startup;
324 * if the image is doing a CRC calculation before programming the PMU
325 * for HT availability, it could take a couple hundred ms more, so
326 * max out at a 1 second (1000000us).
328 #undef PMU_MAX_TRANSITION_DLY
329 #define PMU_MAX_TRANSITION_DLY 1000000
331 /* Value for ChipClockCSR during initial setup */
332 #define BRCMF_INIT_CLKCTL1 (SBSDIO_FORCE_HW_CLKREQ_OFF | \
333 SBSDIO_ALP_AVAIL_REQ)
335 /* Flags for SDH calls */
336 #define F2SYNC (SDIO_REQ_4BYTE | SDIO_REQ_FIXED)
339 #define SBIM_IBE 0x20000 /* inbanderror */
340 #define SBIM_TO 0x40000 /* timeout */
341 #define SBIM_BY 0x01800000 /* busy (sonics >= 2.3) */
342 #define SBIM_RJ 0x02000000 /* reject (sonics >= 2.3) */
347 #define SBTML_RESET 0x0001
349 #define SBTML_REJ_MASK 0x0006
351 #define SBTML_REJ 0x0002
352 /* temporary reject, for error recovery */
353 #define SBTML_TMPREJ 0x0004
355 /* Shift to locate the SI control flags in sbtml */
356 #define SBTML_SICF_SHIFT 16
359 #define SBTMH_SERR 0x0001 /* serror */
360 #define SBTMH_INT 0x0002 /* interrupt */
361 #define SBTMH_BUSY 0x0004 /* busy */
362 #define SBTMH_TO 0x0020 /* timeout (sonics >= 2.3) */
364 /* Shift to locate the SI status flags in sbtmh */
365 #define SBTMH_SISF_SHIFT 16
368 #define SBIDL_INIT 0x80 /* initiator */
371 #define SBIDH_RC_MASK 0x000f /* revision code */
372 #define SBIDH_RCE_MASK 0x7000 /* revision code extension field */
373 #define SBIDH_RCE_SHIFT 8
374 #define SBCOREREV(sbidh) \
375 ((((sbidh) & SBIDH_RCE_MASK) >> SBIDH_RCE_SHIFT) | \
376 ((sbidh) & SBIDH_RC_MASK))
377 #define SBIDH_CC_MASK 0x8ff0 /* core code */
378 #define SBIDH_CC_SHIFT 4
379 #define SBIDH_VC_MASK 0xffff0000 /* vendor code */
380 #define SBIDH_VC_SHIFT 16
383 * Conversion of 802.1D priority to precedence level
385 static uint
prio2prec(u32 prio
)
387 return (prio
== PRIO_8021D_NONE
|| prio
== PRIO_8021D_BE
) ?
392 * Core reg address translation.
393 * Both macro's returns a 32 bits byte address on the backplane bus.
395 #define CORE_CC_REG(base, field) \
396 (base + offsetof(struct chipcregs, field))
397 #define CORE_BUS_REG(base, field) \
398 (base + offsetof(struct sdpcmd_regs, field))
399 #define CORE_SB(base, field) \
400 (base + SBCONFIGOFF + offsetof(struct sbconfig, field))
404 u32 corecontrol
; /* 0x00, rev8 */
405 u32 corestatus
; /* rev8 */
407 u32 biststatus
; /* rev8 */
410 u16 pcmciamesportaladdr
; /* 0x010, rev8 */
412 u16 pcmciamesportalmask
; /* rev8 */
414 u16 pcmciawrframebc
; /* rev8 */
416 u16 pcmciaunderflowtimer
; /* rev8 */
420 u32 intstatus
; /* 0x020, rev8 */
421 u32 hostintmask
; /* rev8 */
422 u32 intmask
; /* rev8 */
423 u32 sbintstatus
; /* rev8 */
424 u32 sbintmask
; /* rev8 */
425 u32 funcintmask
; /* rev4 */
427 u32 tosbmailbox
; /* 0x040, rev8 */
428 u32 tohostmailbox
; /* rev8 */
429 u32 tosbmailboxdata
; /* rev8 */
430 u32 tohostmailboxdata
; /* rev8 */
432 /* synchronized access to registers in SDIO clock domain */
433 u32 sdioaccess
; /* 0x050, rev8 */
436 /* PCMCIA frame control */
437 u8 pcmciaframectrl
; /* 0x060, rev8 */
439 u8 pcmciawatermark
; /* rev8 */
442 /* interrupt batching control */
443 u32 intrcvlazy
; /* 0x100, rev8 */
447 u32 cmd52rd
; /* 0x110, rev8 */
448 u32 cmd52wr
; /* rev8 */
449 u32 cmd53rd
; /* rev8 */
450 u32 cmd53wr
; /* rev8 */
451 u32 abort
; /* rev8 */
452 u32 datacrcerror
; /* rev8 */
453 u32 rdoutofsync
; /* rev8 */
454 u32 wroutofsync
; /* rev8 */
455 u32 writebusy
; /* rev8 */
456 u32 readwait
; /* rev8 */
457 u32 readterm
; /* rev8 */
458 u32 writeterm
; /* rev8 */
460 u32 clockctlstatus
; /* rev8 */
463 u32 PAD
[128]; /* DMA engines */
465 /* SDIO/PCMCIA CIS region */
466 char cis
[512]; /* 0x400-0x5ff, rev6 */
468 /* PCMCIA function control registers */
469 char pcmciafcr
[256]; /* 0x600-6ff, rev6 */
472 /* PCMCIA backplane access */
473 u16 backplanecsr
; /* 0x76E, rev6 */
474 u16 backplaneaddr0
; /* rev6 */
475 u16 backplaneaddr1
; /* rev6 */
476 u16 backplaneaddr2
; /* rev6 */
477 u16 backplaneaddr3
; /* rev6 */
478 u16 backplanedata0
; /* rev6 */
479 u16 backplanedata1
; /* rev6 */
480 u16 backplanedata2
; /* rev6 */
481 u16 backplanedata3
; /* rev6 */
484 /* sprom "size" & "blank" info */
485 u16 spromstatus
; /* 0x7BE, rev2 */
492 /* Device console log buffer state */
493 struct brcmf_console
{
494 uint count
; /* Poll interval msec counter */
495 uint log_addr
; /* Log struct address (fixed) */
496 struct rte_log_le log_le
; /* Log struct (host copy) */
497 uint bufsize
; /* Size of log buffer */
498 u8
*buf
; /* Log buffer (host copy) */
499 uint last
; /* Last buffer read index */
503 struct sdpcm_shared
{
507 u32 assert_file_addr
;
509 u32 console_addr
; /* Address of struct rte_console */
514 struct sdpcm_shared_le
{
517 __le32 assert_exp_addr
;
518 __le32 assert_file_addr
;
520 __le32 console_addr
; /* Address of struct rte_console */
521 __le32 msgtrace_addr
;
526 /* misc chip info needed by some of the routines */
533 u32 buscorebase
; /* 32 bits backplane bus address */
542 /* Private data for SDIO bus interaction */
544 struct brcmf_pub
*drvr
;
546 struct brcmf_sdio_dev
*sdiodev
; /* sdio device handler */
547 struct chip_info
*ci
; /* Chip info struct */
548 char *vars
; /* Variables (from CIS and/or other) */
549 uint varsz
; /* Size of variables buffer */
551 u32 ramsize
; /* Size of RAM in SOCRAM (bytes) */
553 u32 hostintmask
; /* Copy of Host Interrupt Mask */
554 u32 intstatus
; /* Intstatus bits (events) pending */
555 bool dpc_sched
; /* Indicates DPC schedule (intrpt rcvd) */
556 bool fcstate
; /* State of dongle flow-control */
558 uint blocksize
; /* Block size of SDIO transfers */
559 uint roundup
; /* Max roundup limit */
561 struct pktq txq
; /* Queue length used for flow-control */
562 u8 flowcontrol
; /* per prio flow control bitmask */
563 u8 tx_seq
; /* Transmit sequence number (next) */
564 u8 tx_max
; /* Maximum transmit sequence allowed */
566 u8 hdrbuf
[MAX_HDR_READ
+ BRCMF_SDALIGN
];
567 u8
*rxhdr
; /* Header of current rx frame (in hdrbuf) */
568 u16 nextlen
; /* Next Read Len from last header */
569 u8 rx_seq
; /* Receive sequence number (expected) */
570 bool rxskip
; /* Skip receive (awaiting NAK ACK) */
572 uint rxbound
; /* Rx frames to read before resched */
573 uint txbound
; /* Tx frames to send before resched */
576 struct sk_buff
*glomd
; /* Packet containing glomming descriptor */
577 struct sk_buff_head glom
; /* Packet list for glommed superframe */
578 uint glomerr
; /* Glom packet read errors */
580 u8
*rxbuf
; /* Buffer for receiving control packets */
581 uint rxblen
; /* Allocated length of rxbuf */
582 u8
*rxctl
; /* Aligned pointer into rxbuf */
583 u8
*databuf
; /* Buffer for receiving big glom packet */
584 u8
*dataptr
; /* Aligned pointer into databuf */
585 uint rxlen
; /* Length of valid data in buffer */
587 u8 sdpcm_ver
; /* Bus protocol reported by dongle */
589 bool intr
; /* Use interrupts */
590 bool poll
; /* Use polling */
591 bool ipend
; /* Device interrupt is pending */
592 uint intrcount
; /* Count of device interrupt callbacks */
593 uint lastintrs
; /* Count as of last watchdog timer */
594 uint spurious
; /* Count of spurious interrupts */
595 uint pollrate
; /* Ticks between device polls */
596 uint polltick
; /* Tick counter */
597 uint pollcnt
; /* Count of active polls */
600 uint console_interval
;
601 struct brcmf_console console
; /* Console output polling support */
602 uint console_addr
; /* Console address from shared struct */
605 uint regfails
; /* Count of R_REG failures */
607 uint clkstate
; /* State of sd and backplane clock(s) */
608 bool activity
; /* Activity flag for clock down */
609 s32 idletime
; /* Control for activity timeout */
610 s32 idlecount
; /* Activity timeout counter */
611 s32 idleclock
; /* How to set bus driver when idle */
613 bool use_rxchain
; /* If brcmf should use PKT chains */
614 bool sleeping
; /* Is SDIO bus sleeping? */
615 bool rxflow_mode
; /* Rx flow control mode */
616 bool rxflow
; /* Is rx flow control on */
617 bool alp_only
; /* Don't use HT clock (ALP only) */
618 /* Field to decide if rx of control frames happen in rxbuf or lb-pool */
621 /* Some additional counters */
622 uint tx_sderrs
; /* Count of tx attempts with sd errors */
623 uint fcqueued
; /* Tx packets that got queued */
624 uint rxrtx
; /* Count of rtx requests (NAK to dongle) */
625 uint rx_toolong
; /* Receive frames too long to receive */
626 uint rxc_errors
; /* SDIO errors when reading control frames */
627 uint rx_hdrfail
; /* SDIO errors on header reads */
628 uint rx_badhdr
; /* Bad received headers (roosync?) */
629 uint rx_badseq
; /* Mismatched rx sequence number */
630 uint fc_rcvd
; /* Number of flow-control events received */
631 uint fc_xoff
; /* Number which turned on flow-control */
632 uint fc_xon
; /* Number which turned off flow-control */
633 uint rxglomfail
; /* Failed deglom attempts */
634 uint rxglomframes
; /* Number of glom frames (superframes) */
635 uint rxglompkts
; /* Number of packets from glom frames */
636 uint f2rxhdrs
; /* Number of header reads */
637 uint f2rxdata
; /* Number of frame data reads */
638 uint f2txdata
; /* Number of f2 frame writes */
639 uint f1regdata
; /* Number of f1 register accesses */
643 bool ctrl_frame_stat
;
646 wait_queue_head_t ctrl_wait
;
647 wait_queue_head_t dcmd_resp_wait
;
649 struct timer_list timer
;
650 struct completion watchdog_wait
;
651 struct task_struct
*watchdog_tsk
;
655 struct task_struct
*dpc_tsk
;
656 struct completion dpc_wait
;
658 struct semaphore sdsem
;
661 const struct firmware
*firmware
;
668 u32 sbipsflag
; /* initiator port ocp slave flag */
670 u32 sbtpsflag
; /* target port ocp slave flag */
672 u32 sbtmerrloga
; /* (sonics >= 2.3) */
674 u32 sbtmerrlog
; /* (sonics >= 2.3) */
676 u32 sbadmatch3
; /* address match3 */
678 u32 sbadmatch2
; /* address match2 */
680 u32 sbadmatch1
; /* address match1 */
682 u32 sbimstate
; /* initiator agent state */
683 u32 sbintvec
; /* interrupt mask */
684 u32 sbtmstatelow
; /* target state */
685 u32 sbtmstatehigh
; /* target state */
686 u32 sbbwa0
; /* bandwidth allocation table0 */
688 u32 sbimconfiglow
; /* initiator configuration */
689 u32 sbimconfighigh
; /* initiator configuration */
690 u32 sbadmatch0
; /* address match0 */
692 u32 sbtmconfiglow
; /* target configuration */
693 u32 sbtmconfighigh
; /* target configuration */
694 u32 sbbconfig
; /* broadcast configuration */
696 u32 sbbstate
; /* broadcast state */
698 u32 sbactcnfg
; /* activate configuration */
700 u32 sbflagst
; /* current sbflags */
702 u32 sbidlow
; /* identification */
703 u32 sbidhigh
; /* identification */
709 #define CLK_PENDING 2 /* Not used yet */
713 static int qcount
[NUMPRIO
];
714 static int tx_packets
[NUMPRIO
];
717 #define SDIO_DRIVE_STRENGTH 6 /* in milliamps */
719 #define RETRYCHAN(chan) ((chan) == SDPCM_EVENT_CHANNEL)
721 /* Retry count for register access failures */
722 static const uint retry_limit
= 2;
724 /* Limit on rounding up frames */
725 static const uint max_roundup
= 512;
729 static void pkt_align(struct sk_buff
*p
, int len
, int align
)
732 datalign
= (unsigned long)(p
->data
);
733 datalign
= roundup(datalign
, (align
)) - datalign
;
735 skb_pull(p
, datalign
);
739 /* To check if there's window offered */
740 static bool data_ok(struct brcmf_bus
*bus
)
742 return (u8
)(bus
->tx_max
- bus
->tx_seq
) != 0 &&
743 ((u8
)(bus
->tx_max
- bus
->tx_seq
) & 0x80) == 0;
747 * Reads a register in the SDIO hardware block. This block occupies a series of
748 * adresses on the 32 bit backplane bus.
751 r_sdreg32(struct brcmf_bus
*bus
, u32
*regvar
, u32 reg_offset
, u32
*retryvar
)
755 *regvar
= brcmf_sdcard_reg_read(bus
->sdiodev
,
756 bus
->ci
->buscorebase
+ reg_offset
, sizeof(u32
));
757 } while (brcmf_sdcard_regfail(bus
->sdiodev
) &&
758 (++(*retryvar
) <= retry_limit
));
760 bus
->regfails
+= (*retryvar
-1);
761 if (*retryvar
> retry_limit
) {
762 brcmf_dbg(ERROR
, "FAILED READ %Xh\n", reg_offset
);
769 w_sdreg32(struct brcmf_bus
*bus
, u32 regval
, u32 reg_offset
, u32
*retryvar
)
773 brcmf_sdcard_reg_write(bus
->sdiodev
,
774 bus
->ci
->buscorebase
+ reg_offset
,
775 sizeof(u32
), regval
);
776 } while (brcmf_sdcard_regfail(bus
->sdiodev
) &&
777 (++(*retryvar
) <= retry_limit
));
779 bus
->regfails
+= (*retryvar
-1);
780 if (*retryvar
> retry_limit
)
781 brcmf_dbg(ERROR
, "FAILED REGISTER WRITE %Xh\n",
786 #define PKT_AVAILABLE() (intstatus & I_HMB_FRAME_IND)
788 #define HOSTINTMASK (I_HMB_SW_MASK | I_CHIPACTIVE)
790 /* Packet free applicable unconditionally for sdio and sdspi.
791 * Conditional if bufpool was present for gspi bus.
793 static void brcmf_sdbrcm_pktfree2(struct brcmf_bus
*bus
, struct sk_buff
*pkt
)
796 brcmu_pkt_buf_free_skb(pkt
);
799 /* Turn backplane clock on or off */
800 static int brcmf_sdbrcm_htclk(struct brcmf_bus
*bus
, bool on
, bool pendok
)
803 u8 clkctl
, clkreq
, devctl
;
804 unsigned long timeout
;
806 brcmf_dbg(TRACE
, "Enter\n");
811 /* Request HT Avail */
813 bus
->alp_only
? SBSDIO_ALP_AVAIL_REQ
: SBSDIO_HT_AVAIL_REQ
;
815 brcmf_sdcard_cfg_write(bus
->sdiodev
, SDIO_FUNC_1
,
816 SBSDIO_FUNC1_CHIPCLKCSR
, clkreq
, &err
);
818 brcmf_dbg(ERROR
, "HT Avail request error: %d\n", err
);
822 if (pendok
&& ((bus
->ci
->buscoretype
== PCMCIA_CORE_ID
)
823 && (bus
->ci
->buscorerev
== 9))) {
825 r_sdreg32(bus
, &dummy
,
826 offsetof(struct sdpcmd_regs
, clockctlstatus
),
830 /* Check current status */
831 clkctl
= brcmf_sdcard_cfg_read(bus
->sdiodev
, SDIO_FUNC_1
,
832 SBSDIO_FUNC1_CHIPCLKCSR
, &err
);
834 brcmf_dbg(ERROR
, "HT Avail read error: %d\n", err
);
838 /* Go to pending and await interrupt if appropriate */
839 if (!SBSDIO_CLKAV(clkctl
, bus
->alp_only
) && pendok
) {
840 /* Allow only clock-available interrupt */
841 devctl
= brcmf_sdcard_cfg_read(bus
->sdiodev
,
843 SBSDIO_DEVICE_CTL
, &err
);
845 brcmf_dbg(ERROR
, "Devctl error setting CA: %d\n",
850 devctl
|= SBSDIO_DEVCTL_CA_INT_ONLY
;
851 brcmf_sdcard_cfg_write(bus
->sdiodev
, SDIO_FUNC_1
,
852 SBSDIO_DEVICE_CTL
, devctl
, &err
);
853 brcmf_dbg(INFO
, "CLKCTL: set PENDING\n");
854 bus
->clkstate
= CLK_PENDING
;
857 } else if (bus
->clkstate
== CLK_PENDING
) {
858 /* Cancel CA-only interrupt filter */
860 brcmf_sdcard_cfg_read(bus
->sdiodev
, SDIO_FUNC_1
,
861 SBSDIO_DEVICE_CTL
, &err
);
862 devctl
&= ~SBSDIO_DEVCTL_CA_INT_ONLY
;
863 brcmf_sdcard_cfg_write(bus
->sdiodev
, SDIO_FUNC_1
,
864 SBSDIO_DEVICE_CTL
, devctl
, &err
);
867 /* Otherwise, wait here (polling) for HT Avail */
869 msecs_to_jiffies(PMU_MAX_TRANSITION_DLY
/1000);
870 while (!SBSDIO_CLKAV(clkctl
, bus
->alp_only
)) {
871 clkctl
= brcmf_sdcard_cfg_read(bus
->sdiodev
,
873 SBSDIO_FUNC1_CHIPCLKCSR
,
875 if (time_after(jiffies
, timeout
))
878 usleep_range(5000, 10000);
881 brcmf_dbg(ERROR
, "HT Avail request error: %d\n", err
);
884 if (!SBSDIO_CLKAV(clkctl
, bus
->alp_only
)) {
885 brcmf_dbg(ERROR
, "HT Avail timeout (%d): clkctl 0x%02x\n",
886 PMU_MAX_TRANSITION_DLY
, clkctl
);
890 /* Mark clock available */
891 bus
->clkstate
= CLK_AVAIL
;
892 brcmf_dbg(INFO
, "CLKCTL: turned ON\n");
895 if (bus
->alp_only
!= true) {
896 if (SBSDIO_ALPONLY(clkctl
))
897 brcmf_dbg(ERROR
, "HT Clock should be on\n");
899 #endif /* defined (BCMDBG) */
901 bus
->activity
= true;
905 if (bus
->clkstate
== CLK_PENDING
) {
906 /* Cancel CA-only interrupt filter */
907 devctl
= brcmf_sdcard_cfg_read(bus
->sdiodev
,
909 SBSDIO_DEVICE_CTL
, &err
);
910 devctl
&= ~SBSDIO_DEVCTL_CA_INT_ONLY
;
911 brcmf_sdcard_cfg_write(bus
->sdiodev
, SDIO_FUNC_1
,
912 SBSDIO_DEVICE_CTL
, devctl
, &err
);
915 bus
->clkstate
= CLK_SDONLY
;
916 brcmf_sdcard_cfg_write(bus
->sdiodev
, SDIO_FUNC_1
,
917 SBSDIO_FUNC1_CHIPCLKCSR
, clkreq
, &err
);
918 brcmf_dbg(INFO
, "CLKCTL: turned OFF\n");
920 brcmf_dbg(ERROR
, "Failed access turning clock off: %d\n",
928 /* Change idle/active SD state */
929 static int brcmf_sdbrcm_sdclk(struct brcmf_bus
*bus
, bool on
)
931 brcmf_dbg(TRACE
, "Enter\n");
934 bus
->clkstate
= CLK_SDONLY
;
936 bus
->clkstate
= CLK_NONE
;
941 /* Transition SD and backplane clock readiness */
942 static int brcmf_sdbrcm_clkctl(struct brcmf_bus
*bus
, uint target
, bool pendok
)
945 uint oldstate
= bus
->clkstate
;
948 brcmf_dbg(TRACE
, "Enter\n");
950 /* Early exit if we're already there */
951 if (bus
->clkstate
== target
) {
952 if (target
== CLK_AVAIL
) {
953 brcmf_sdbrcm_wd_timer(bus
, BRCMF_WD_POLL_MS
);
954 bus
->activity
= true;
961 /* Make sure SD clock is available */
962 if (bus
->clkstate
== CLK_NONE
)
963 brcmf_sdbrcm_sdclk(bus
, true);
964 /* Now request HT Avail on the backplane */
965 brcmf_sdbrcm_htclk(bus
, true, pendok
);
966 brcmf_sdbrcm_wd_timer(bus
, BRCMF_WD_POLL_MS
);
967 bus
->activity
= true;
971 /* Remove HT request, or bring up SD clock */
972 if (bus
->clkstate
== CLK_NONE
)
973 brcmf_sdbrcm_sdclk(bus
, true);
974 else if (bus
->clkstate
== CLK_AVAIL
)
975 brcmf_sdbrcm_htclk(bus
, false, false);
977 brcmf_dbg(ERROR
, "request for %d -> %d\n",
978 bus
->clkstate
, target
);
979 brcmf_sdbrcm_wd_timer(bus
, BRCMF_WD_POLL_MS
);
983 /* Make sure to remove HT request */
984 if (bus
->clkstate
== CLK_AVAIL
)
985 brcmf_sdbrcm_htclk(bus
, false, false);
986 /* Now remove the SD clock */
987 brcmf_sdbrcm_sdclk(bus
, false);
988 brcmf_sdbrcm_wd_timer(bus
, 0);
992 brcmf_dbg(INFO
, "%d -> %d\n", oldstate
, bus
->clkstate
);
998 static int brcmf_sdbrcm_bussleep(struct brcmf_bus
*bus
, bool sleep
)
1002 brcmf_dbg(INFO
, "request %s (currently %s)\n",
1003 sleep
? "SLEEP" : "WAKE",
1004 bus
->sleeping
? "SLEEP" : "WAKE");
1006 /* Done if we're already in the requested state */
1007 if (sleep
== bus
->sleeping
)
1010 /* Going to sleep: set the alarm and turn off the lights... */
1012 /* Don't sleep if something is pending */
1013 if (bus
->dpc_sched
|| bus
->rxskip
|| pktq_len(&bus
->txq
))
1016 /* Make sure the controller has the bus up */
1017 brcmf_sdbrcm_clkctl(bus
, CLK_AVAIL
, false);
1019 /* Tell device to start using OOB wakeup */
1020 w_sdreg32(bus
, SMB_USE_OOB
,
1021 offsetof(struct sdpcmd_regs
, tosbmailbox
), &retries
);
1022 if (retries
> retry_limit
)
1023 brcmf_dbg(ERROR
, "CANNOT SIGNAL CHIP, WILL NOT WAKE UP!!\n");
1025 /* Turn off our contribution to the HT clock request */
1026 brcmf_sdbrcm_clkctl(bus
, CLK_SDONLY
, false);
1028 brcmf_sdcard_cfg_write(bus
->sdiodev
, SDIO_FUNC_1
,
1029 SBSDIO_FUNC1_CHIPCLKCSR
,
1030 SBSDIO_FORCE_HW_CLKREQ_OFF
, NULL
);
1032 /* Isolate the bus */
1033 brcmf_sdcard_cfg_write(bus
->sdiodev
, SDIO_FUNC_1
,
1035 SBSDIO_DEVCTL_PADS_ISO
, NULL
);
1038 bus
->sleeping
= true;
1041 /* Waking up: bus power up is ok, set local state */
1043 brcmf_sdcard_cfg_write(bus
->sdiodev
, SDIO_FUNC_1
,
1044 SBSDIO_FUNC1_CHIPCLKCSR
, 0, NULL
);
1046 /* Force pad isolation off if possible
1047 (in case power never toggled) */
1048 if ((bus
->ci
->buscoretype
== PCMCIA_CORE_ID
)
1049 && (bus
->ci
->buscorerev
>= 10))
1050 brcmf_sdcard_cfg_write(bus
->sdiodev
, SDIO_FUNC_1
,
1051 SBSDIO_DEVICE_CTL
, 0, NULL
);
1053 /* Make sure the controller has the bus up */
1054 brcmf_sdbrcm_clkctl(bus
, CLK_AVAIL
, false);
1056 /* Send misc interrupt to indicate OOB not needed */
1057 w_sdreg32(bus
, 0, offsetof(struct sdpcmd_regs
, tosbmailboxdata
),
1059 if (retries
<= retry_limit
)
1060 w_sdreg32(bus
, SMB_DEV_INT
,
1061 offsetof(struct sdpcmd_regs
, tosbmailbox
),
1064 if (retries
> retry_limit
)
1065 brcmf_dbg(ERROR
, "CANNOT SIGNAL CHIP TO CLEAR OOB!!\n");
1067 /* Make sure we have SD bus access */
1068 brcmf_sdbrcm_clkctl(bus
, CLK_SDONLY
, false);
1071 bus
->sleeping
= false;
1077 static void bus_wake(struct brcmf_bus
*bus
)
1080 brcmf_sdbrcm_bussleep(bus
, false);
1083 static u32
brcmf_sdbrcm_hostmail(struct brcmf_bus
*bus
)
1090 brcmf_dbg(TRACE
, "Enter\n");
1092 /* Read mailbox data and ack that we did so */
1093 r_sdreg32(bus
, &hmb_data
,
1094 offsetof(struct sdpcmd_regs
, tohostmailboxdata
), &retries
);
1096 if (retries
<= retry_limit
)
1097 w_sdreg32(bus
, SMB_INT_ACK
,
1098 offsetof(struct sdpcmd_regs
, tosbmailbox
), &retries
);
1099 bus
->f1regdata
+= 2;
1101 /* Dongle recomposed rx frames, accept them again */
1102 if (hmb_data
& HMB_DATA_NAKHANDLED
) {
1103 brcmf_dbg(INFO
, "Dongle reports NAK handled, expect rtx of %d\n",
1106 brcmf_dbg(ERROR
, "unexpected NAKHANDLED!\n");
1108 bus
->rxskip
= false;
1109 intstatus
|= I_HMB_FRAME_IND
;
1113 * DEVREADY does not occur with gSPI.
1115 if (hmb_data
& (HMB_DATA_DEVREADY
| HMB_DATA_FWREADY
)) {
1117 (hmb_data
& HMB_DATA_VERSION_MASK
) >>
1118 HMB_DATA_VERSION_SHIFT
;
1119 if (bus
->sdpcm_ver
!= SDPCM_PROT_VERSION
)
1120 brcmf_dbg(ERROR
, "Version mismatch, dongle reports %d, "
1122 bus
->sdpcm_ver
, SDPCM_PROT_VERSION
);
1124 brcmf_dbg(INFO
, "Dongle ready, protocol version %d\n",
1129 * Flow Control has been moved into the RX headers and this out of band
1130 * method isn't used any more.
1131 * remaining backward compatible with older dongles.
1133 if (hmb_data
& HMB_DATA_FC
) {
1134 fcbits
= (hmb_data
& HMB_DATA_FCDATA_MASK
) >>
1135 HMB_DATA_FCDATA_SHIFT
;
1137 if (fcbits
& ~bus
->flowcontrol
)
1140 if (bus
->flowcontrol
& ~fcbits
)
1144 bus
->flowcontrol
= fcbits
;
1147 /* Shouldn't be any others */
1148 if (hmb_data
& ~(HMB_DATA_DEVREADY
|
1149 HMB_DATA_NAKHANDLED
|
1152 HMB_DATA_FCDATA_MASK
| HMB_DATA_VERSION_MASK
))
1153 brcmf_dbg(ERROR
, "Unknown mailbox data content: 0x%02x\n",
1159 static void brcmf_sdbrcm_rxfail(struct brcmf_bus
*bus
, bool abort
, bool rtx
)
1166 brcmf_dbg(ERROR
, "%sterminate frame%s\n",
1167 abort
? "abort command, " : "",
1168 rtx
? ", send NAK" : "");
1171 brcmf_sdcard_abort(bus
->sdiodev
, SDIO_FUNC_2
);
1173 brcmf_sdcard_cfg_write(bus
->sdiodev
, SDIO_FUNC_1
,
1174 SBSDIO_FUNC1_FRAMECTRL
,
1178 /* Wait until the packet has been flushed (device/FIFO stable) */
1179 for (lastrbc
= retries
= 0xffff; retries
> 0; retries
--) {
1180 hi
= brcmf_sdcard_cfg_read(bus
->sdiodev
, SDIO_FUNC_1
,
1181 SBSDIO_FUNC1_RFRAMEBCHI
, NULL
);
1182 lo
= brcmf_sdcard_cfg_read(bus
->sdiodev
, SDIO_FUNC_1
,
1183 SBSDIO_FUNC1_RFRAMEBCLO
, NULL
);
1184 bus
->f1regdata
+= 2;
1186 if ((hi
== 0) && (lo
== 0))
1189 if ((hi
> (lastrbc
>> 8)) && (lo
> (lastrbc
& 0x00ff))) {
1190 brcmf_dbg(ERROR
, "count growing: last 0x%04x now 0x%04x\n",
1191 lastrbc
, (hi
<< 8) + lo
);
1193 lastrbc
= (hi
<< 8) + lo
;
1197 brcmf_dbg(ERROR
, "count never zeroed: last 0x%04x\n", lastrbc
);
1199 brcmf_dbg(INFO
, "flush took %d iterations\n", 0xffff - retries
);
1203 w_sdreg32(bus
, SMB_NAK
,
1204 offsetof(struct sdpcmd_regs
, tosbmailbox
), &retries
);
1207 if (retries
<= retry_limit
)
1211 /* Clear partial in any case */
1214 /* If we can't reach the device, signal failure */
1215 if (err
|| brcmf_sdcard_regfail(bus
->sdiodev
))
1216 bus
->drvr
->busstate
= BRCMF_BUS_DOWN
;
1219 /* copy a buffer into a pkt buffer chain */
1220 static uint
brcmf_sdbrcm_glom_from_buf(struct brcmf_bus
*bus
, uint len
)
1229 skb_queue_walk(&bus
->glom
, p
) {
1230 n
= min_t(uint
, p
->len
, len
);
1231 memcpy(p
->data
, buf
, n
);
1242 static u8
brcmf_sdbrcm_rxglom(struct brcmf_bus
*bus
, u8 rxseq
)
1248 struct sk_buff
*pfirst
, *plast
, *pnext
, *save_pfirst
;
1251 u8 chan
, seq
, doff
, sfdoff
;
1255 bool usechain
= bus
->use_rxchain
;
1257 /* If packets, issue read(s) and send up packet chain */
1258 /* Return sequence numbers consumed? */
1260 brcmf_dbg(TRACE
, "start: glomd %p glom %p\n",
1261 bus
->glomd
, skb_peek(&bus
->glom
));
1263 /* If there's a descriptor, generate the packet chain */
1265 pfirst
= plast
= pnext
= NULL
;
1266 dlen
= (u16
) (bus
->glomd
->len
);
1267 dptr
= bus
->glomd
->data
;
1268 if (!dlen
|| (dlen
& 1)) {
1269 brcmf_dbg(ERROR
, "bad glomd len(%d), ignore descriptor\n",
1274 for (totlen
= num
= 0; dlen
; num
++) {
1275 /* Get (and move past) next length */
1276 sublen
= get_unaligned_le16(dptr
);
1277 dlen
-= sizeof(u16
);
1278 dptr
+= sizeof(u16
);
1279 if ((sublen
< SDPCM_HDRLEN
) ||
1280 ((num
== 0) && (sublen
< (2 * SDPCM_HDRLEN
)))) {
1281 brcmf_dbg(ERROR
, "descriptor len %d bad: %d\n",
1286 if (sublen
% BRCMF_SDALIGN
) {
1287 brcmf_dbg(ERROR
, "sublen %d not multiple of %d\n",
1288 sublen
, BRCMF_SDALIGN
);
1293 /* For last frame, adjust read len so total
1294 is a block multiple */
1297 (roundup(totlen
, bus
->blocksize
) - totlen
);
1298 totlen
= roundup(totlen
, bus
->blocksize
);
1301 /* Allocate/chain packet for next subframe */
1302 pnext
= brcmu_pkt_buf_get_skb(sublen
+ BRCMF_SDALIGN
);
1303 if (pnext
== NULL
) {
1304 brcmf_dbg(ERROR
, "bcm_pkt_buf_get_skb failed, num %d len %d\n",
1308 skb_queue_tail(&bus
->glom
, pnext
);
1310 /* Adhere to start alignment requirements */
1311 pkt_align(pnext
, sublen
, BRCMF_SDALIGN
);
1314 /* If all allocations succeeded, save packet chain
1317 brcmf_dbg(GLOM
, "allocated %d-byte packet chain for %d subframes\n",
1319 if (BRCMF_GLOM_ON() && bus
->nextlen
&&
1320 totlen
!= bus
->nextlen
) {
1321 brcmf_dbg(GLOM
, "glomdesc mismatch: nextlen %d glomdesc %d rxseq %d\n",
1322 bus
->nextlen
, totlen
, rxseq
);
1324 pfirst
= pnext
= NULL
;
1326 if (!skb_queue_empty(&bus
->glom
))
1327 skb_queue_walk_safe(&bus
->glom
, pfirst
, pnext
) {
1328 skb_unlink(pfirst
, &bus
->glom
);
1329 brcmu_pkt_buf_free_skb(pfirst
);
1334 /* Done with descriptor packet */
1335 brcmu_pkt_buf_free_skb(bus
->glomd
);
1340 /* Ok -- either we just generated a packet chain,
1341 or had one from before */
1342 if (!skb_queue_empty(&bus
->glom
)) {
1343 if (BRCMF_GLOM_ON()) {
1344 brcmf_dbg(GLOM
, "try superframe read, packet chain:\n");
1345 skb_queue_walk(&bus
->glom
, pnext
) {
1346 brcmf_dbg(GLOM
, " %p: %p len 0x%04x (%d)\n",
1347 pnext
, (u8
*) (pnext
->data
),
1348 pnext
->len
, pnext
->len
);
1352 pfirst
= skb_peek(&bus
->glom
);
1353 dlen
= (u16
) brcmu_pkttotlen(pfirst
);
1355 /* Do an SDIO read for the superframe. Configurable iovar to
1356 * read directly into the chained packet, or allocate a large
1357 * packet and and copy into the chain.
1360 errcode
= brcmf_sdcard_recv_buf(bus
->sdiodev
,
1361 bus
->sdiodev
->sbwad
,
1363 F2SYNC
, (u8
*) pfirst
->data
, dlen
,
1365 } else if (bus
->dataptr
) {
1366 errcode
= brcmf_sdcard_recv_buf(bus
->sdiodev
,
1367 bus
->sdiodev
->sbwad
,
1369 F2SYNC
, bus
->dataptr
, dlen
,
1371 sublen
= (u16
) brcmf_sdbrcm_glom_from_buf(bus
, dlen
);
1372 if (sublen
!= dlen
) {
1373 brcmf_dbg(ERROR
, "FAILED TO COPY, dlen %d sublen %d\n",
1379 brcmf_dbg(ERROR
, "COULDN'T ALLOC %d-BYTE GLOM, FORCE FAILURE\n",
1385 /* On failure, kill the superframe, allow a couple retries */
1387 brcmf_dbg(ERROR
, "glom read of %d bytes failed: %d\n",
1389 bus
->drvr
->rx_errors
++;
1391 if (bus
->glomerr
++ < 3) {
1392 brcmf_sdbrcm_rxfail(bus
, true, true);
1395 brcmf_sdbrcm_rxfail(bus
, true, false);
1397 skb_queue_walk_safe(&bus
->glom
, pfirst
, pnext
) {
1398 skb_unlink(pfirst
, &bus
->glom
);
1399 brcmu_pkt_buf_free_skb(pfirst
);
1405 if (BRCMF_GLOM_ON()) {
1406 printk(KERN_DEBUG
"SUPERFRAME:\n");
1407 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET
,
1408 pfirst
->data
, min_t(int, pfirst
->len
, 48));
1412 /* Validate the superframe header */
1413 dptr
= (u8
*) (pfirst
->data
);
1414 sublen
= get_unaligned_le16(dptr
);
1415 check
= get_unaligned_le16(dptr
+ sizeof(u16
));
1417 chan
= SDPCM_PACKET_CHANNEL(&dptr
[SDPCM_FRAMETAG_LEN
]);
1418 seq
= SDPCM_PACKET_SEQUENCE(&dptr
[SDPCM_FRAMETAG_LEN
]);
1419 bus
->nextlen
= dptr
[SDPCM_FRAMETAG_LEN
+ SDPCM_NEXTLEN_OFFSET
];
1420 if ((bus
->nextlen
<< 4) > MAX_RX_DATASZ
) {
1421 brcmf_dbg(INFO
, "nextlen too large (%d) seq %d\n",
1425 doff
= SDPCM_DOFFSET_VALUE(&dptr
[SDPCM_FRAMETAG_LEN
]);
1426 txmax
= SDPCM_WINDOW_VALUE(&dptr
[SDPCM_FRAMETAG_LEN
]);
1429 if ((u16
)~(sublen
^ check
)) {
1430 brcmf_dbg(ERROR
, "(superframe): HW hdr error: len/check 0x%04x/0x%04x\n",
1433 } else if (roundup(sublen
, bus
->blocksize
) != dlen
) {
1434 brcmf_dbg(ERROR
, "(superframe): len 0x%04x, rounded 0x%04x, expect 0x%04x\n",
1435 sublen
, roundup(sublen
, bus
->blocksize
),
1438 } else if (SDPCM_PACKET_CHANNEL(&dptr
[SDPCM_FRAMETAG_LEN
]) !=
1439 SDPCM_GLOM_CHANNEL
) {
1440 brcmf_dbg(ERROR
, "(superframe): bad channel %d\n",
1441 SDPCM_PACKET_CHANNEL(
1442 &dptr
[SDPCM_FRAMETAG_LEN
]));
1444 } else if (SDPCM_GLOMDESC(&dptr
[SDPCM_FRAMETAG_LEN
])) {
1445 brcmf_dbg(ERROR
, "(superframe): got 2nd descriptor?\n");
1447 } else if ((doff
< SDPCM_HDRLEN
) ||
1448 (doff
> (pfirst
->len
- SDPCM_HDRLEN
))) {
1449 brcmf_dbg(ERROR
, "(superframe): Bad data offset %d: HW %d pkt %d min %d\n",
1450 doff
, sublen
, pfirst
->len
, SDPCM_HDRLEN
);
1454 /* Check sequence number of superframe SW header */
1456 brcmf_dbg(INFO
, "(superframe) rx_seq %d, expected %d\n",
1462 /* Check window for sanity */
1463 if ((u8
) (txmax
- bus
->tx_seq
) > 0x40) {
1464 brcmf_dbg(ERROR
, "unlikely tx max %d with tx_seq %d\n",
1465 txmax
, bus
->tx_seq
);
1466 txmax
= bus
->tx_seq
+ 2;
1468 bus
->tx_max
= txmax
;
1470 /* Remove superframe header, remember offset */
1471 skb_pull(pfirst
, doff
);
1474 /* Validate all the subframe headers */
1475 for (num
= 0, pnext
= pfirst
; pnext
&& !errcode
;
1476 num
++, pnext
= pnext
->next
) {
1477 dptr
= (u8
*) (pnext
->data
);
1478 dlen
= (u16
) (pnext
->len
);
1479 sublen
= get_unaligned_le16(dptr
);
1480 check
= get_unaligned_le16(dptr
+ sizeof(u16
));
1481 chan
= SDPCM_PACKET_CHANNEL(&dptr
[SDPCM_FRAMETAG_LEN
]);
1482 doff
= SDPCM_DOFFSET_VALUE(&dptr
[SDPCM_FRAMETAG_LEN
]);
1484 if (BRCMF_GLOM_ON()) {
1485 printk(KERN_DEBUG
"subframe:\n");
1486 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET
,
1491 if ((u16
)~(sublen
^ check
)) {
1492 brcmf_dbg(ERROR
, "(subframe %d): HW hdr error: len/check 0x%04x/0x%04x\n",
1493 num
, sublen
, check
);
1495 } else if ((sublen
> dlen
) || (sublen
< SDPCM_HDRLEN
)) {
1496 brcmf_dbg(ERROR
, "(subframe %d): length mismatch: len 0x%04x, expect 0x%04x\n",
1499 } else if ((chan
!= SDPCM_DATA_CHANNEL
) &&
1500 (chan
!= SDPCM_EVENT_CHANNEL
)) {
1501 brcmf_dbg(ERROR
, "(subframe %d): bad channel %d\n",
1504 } else if ((doff
< SDPCM_HDRLEN
) || (doff
> sublen
)) {
1505 brcmf_dbg(ERROR
, "(subframe %d): Bad data offset %d: HW %d min %d\n",
1506 num
, doff
, sublen
, SDPCM_HDRLEN
);
1512 /* Terminate frame on error, request
1514 if (bus
->glomerr
++ < 3) {
1515 /* Restore superframe header space */
1516 skb_push(pfirst
, sfdoff
);
1517 brcmf_sdbrcm_rxfail(bus
, true, true);
1520 brcmf_sdbrcm_rxfail(bus
, true, false);
1522 skb_queue_walk_safe(&bus
->glom
, pfirst
, pnext
) {
1523 skb_unlink(pfirst
, &bus
->glom
);
1524 brcmu_pkt_buf_free_skb(pfirst
);
1531 /* Basic SD framing looks ok - process each packet (header) */
1532 save_pfirst
= pfirst
;
1535 for (num
= 0; pfirst
; rxseq
++, pfirst
= pnext
) {
1536 pnext
= pfirst
->next
;
1537 pfirst
->next
= NULL
;
1539 dptr
= (u8
*) (pfirst
->data
);
1540 sublen
= get_unaligned_le16(dptr
);
1541 chan
= SDPCM_PACKET_CHANNEL(&dptr
[SDPCM_FRAMETAG_LEN
]);
1542 seq
= SDPCM_PACKET_SEQUENCE(&dptr
[SDPCM_FRAMETAG_LEN
]);
1543 doff
= SDPCM_DOFFSET_VALUE(&dptr
[SDPCM_FRAMETAG_LEN
]);
1545 brcmf_dbg(GLOM
, "Get subframe %d, %p(%p/%d), sublen %d chan %d seq %d\n",
1546 num
, pfirst
, pfirst
->data
,
1547 pfirst
->len
, sublen
, chan
, seq
);
1549 /* precondition: chan == SDPCM_DATA_CHANNEL ||
1550 chan == SDPCM_EVENT_CHANNEL */
1553 brcmf_dbg(GLOM
, "rx_seq %d, expected %d\n",
1559 if (BRCMF_BYTES_ON() && BRCMF_DATA_ON()) {
1560 printk(KERN_DEBUG
"Rx Subframe Data:\n");
1561 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET
,
1566 __skb_trim(pfirst
, sublen
);
1567 skb_pull(pfirst
, doff
);
1569 if (pfirst
->len
== 0) {
1570 brcmu_pkt_buf_free_skb(pfirst
);
1572 plast
->next
= pnext
;
1574 save_pfirst
= pnext
;
1577 } else if (brcmf_proto_hdrpull(bus
->drvr
, &ifidx
,
1579 brcmf_dbg(ERROR
, "rx protocol error\n");
1580 bus
->drvr
->rx_errors
++;
1581 brcmu_pkt_buf_free_skb(pfirst
);
1583 plast
->next
= pnext
;
1585 save_pfirst
= pnext
;
1590 /* this packet will go up, link back into
1591 chain and count it */
1592 pfirst
->next
= pnext
;
1597 if (BRCMF_GLOM_ON()) {
1598 brcmf_dbg(GLOM
, "subframe %d to stack, %p (%p/%d) nxt/lnk %p/%p\n",
1599 num
, pfirst
, pfirst
->data
,
1600 pfirst
->len
, pfirst
->next
,
1602 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET
,
1604 min_t(int, pfirst
->len
, 32));
1610 brcmf_rx_frame(bus
->drvr
, ifidx
, save_pfirst
, num
);
1614 bus
->rxglomframes
++;
1615 bus
->rxglompkts
+= num
;
1620 static int brcmf_sdbrcm_dcmd_resp_wait(struct brcmf_bus
*bus
, uint
*condition
,
1623 DECLARE_WAITQUEUE(wait
, current
);
1624 int timeout
= msecs_to_jiffies(DCMD_RESP_TIMEOUT
);
1626 /* Wait until control frame is available */
1627 add_wait_queue(&bus
->dcmd_resp_wait
, &wait
);
1628 set_current_state(TASK_INTERRUPTIBLE
);
1630 while (!(*condition
) && (!signal_pending(current
) && timeout
))
1631 timeout
= schedule_timeout(timeout
);
1633 if (signal_pending(current
))
1636 set_current_state(TASK_RUNNING
);
1637 remove_wait_queue(&bus
->dcmd_resp_wait
, &wait
);
1642 static int brcmf_sdbrcm_dcmd_resp_wake(struct brcmf_bus
*bus
)
1644 if (waitqueue_active(&bus
->dcmd_resp_wait
))
1645 wake_up_interruptible(&bus
->dcmd_resp_wait
);
1650 brcmf_sdbrcm_read_control(struct brcmf_bus
*bus
, u8
*hdr
, uint len
, uint doff
)
1656 brcmf_dbg(TRACE
, "Enter\n");
1658 /* Set rxctl for frame (w/optional alignment) */
1659 bus
->rxctl
= bus
->rxbuf
;
1660 bus
->rxctl
+= BRCMF_FIRSTREAD
;
1661 pad
= ((unsigned long)bus
->rxctl
% BRCMF_SDALIGN
);
1663 bus
->rxctl
+= (BRCMF_SDALIGN
- pad
);
1664 bus
->rxctl
-= BRCMF_FIRSTREAD
;
1666 /* Copy the already-read portion over */
1667 memcpy(bus
->rxctl
, hdr
, BRCMF_FIRSTREAD
);
1668 if (len
<= BRCMF_FIRSTREAD
)
1671 /* Raise rdlen to next SDIO block to avoid tail command */
1672 rdlen
= len
- BRCMF_FIRSTREAD
;
1673 if (bus
->roundup
&& bus
->blocksize
&& (rdlen
> bus
->blocksize
)) {
1674 pad
= bus
->blocksize
- (rdlen
% bus
->blocksize
);
1675 if ((pad
<= bus
->roundup
) && (pad
< bus
->blocksize
) &&
1676 ((len
+ pad
) < bus
->drvr
->maxctl
))
1678 } else if (rdlen
% BRCMF_SDALIGN
) {
1679 rdlen
+= BRCMF_SDALIGN
- (rdlen
% BRCMF_SDALIGN
);
1682 /* Satisfy length-alignment requirements */
1683 if (rdlen
& (ALIGNMENT
- 1))
1684 rdlen
= roundup(rdlen
, ALIGNMENT
);
1686 /* Drop if the read is too big or it exceeds our maximum */
1687 if ((rdlen
+ BRCMF_FIRSTREAD
) > bus
->drvr
->maxctl
) {
1688 brcmf_dbg(ERROR
, "%d-byte control read exceeds %d-byte buffer\n",
1689 rdlen
, bus
->drvr
->maxctl
);
1690 bus
->drvr
->rx_errors
++;
1691 brcmf_sdbrcm_rxfail(bus
, false, false);
1695 if ((len
- doff
) > bus
->drvr
->maxctl
) {
1696 brcmf_dbg(ERROR
, "%d-byte ctl frame (%d-byte ctl data) exceeds %d-byte limit\n",
1697 len
, len
- doff
, bus
->drvr
->maxctl
);
1698 bus
->drvr
->rx_errors
++;
1700 brcmf_sdbrcm_rxfail(bus
, false, false);
1704 /* Read remainder of frame body into the rxctl buffer */
1705 sdret
= brcmf_sdcard_recv_buf(bus
->sdiodev
,
1706 bus
->sdiodev
->sbwad
,
1708 F2SYNC
, (bus
->rxctl
+ BRCMF_FIRSTREAD
), rdlen
,
1712 /* Control frame failures need retransmission */
1714 brcmf_dbg(ERROR
, "read %d control bytes failed: %d\n",
1717 brcmf_sdbrcm_rxfail(bus
, true, true);
1724 if (BRCMF_BYTES_ON() && BRCMF_CTL_ON()) {
1725 printk(KERN_DEBUG
"RxCtrl:\n");
1726 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET
, bus
->rxctl
, len
);
1730 /* Point to valid data and indicate its length */
1732 bus
->rxlen
= len
- doff
;
1735 /* Awake any waiters */
1736 brcmf_sdbrcm_dcmd_resp_wake(bus
);
1739 /* Pad read to blocksize for efficiency */
1740 static void brcmf_pad(struct brcmf_bus
*bus
, u16
*pad
, u16
*rdlen
)
1742 if (bus
->roundup
&& bus
->blocksize
&& *rdlen
> bus
->blocksize
) {
1743 *pad
= bus
->blocksize
- (*rdlen
% bus
->blocksize
);
1744 if (*pad
<= bus
->roundup
&& *pad
< bus
->blocksize
&&
1745 *rdlen
+ *pad
+ BRCMF_FIRSTREAD
< MAX_RX_DATASZ
)
1747 } else if (*rdlen
% BRCMF_SDALIGN
) {
1748 *rdlen
+= BRCMF_SDALIGN
- (*rdlen
% BRCMF_SDALIGN
);
1753 brcmf_alloc_pkt_and_read(struct brcmf_bus
*bus
, u16 rdlen
,
1754 struct sk_buff
**pkt
, u8
**rxbuf
)
1756 int sdret
; /* Return code from calls */
1758 *pkt
= brcmu_pkt_buf_get_skb(rdlen
+ BRCMF_SDALIGN
);
1762 pkt_align(*pkt
, rdlen
, BRCMF_SDALIGN
);
1763 *rxbuf
= (u8
*) ((*pkt
)->data
);
1764 /* Read the entire frame */
1765 sdret
= brcmf_sdcard_recv_buf(bus
->sdiodev
, bus
->sdiodev
->sbwad
,
1766 SDIO_FUNC_2
, F2SYNC
,
1767 *rxbuf
, rdlen
, *pkt
);
1771 brcmf_dbg(ERROR
, "(nextlen): read %d bytes failed: %d\n",
1773 brcmu_pkt_buf_free_skb(*pkt
);
1774 bus
->drvr
->rx_errors
++;
1775 /* Force retry w/normal header read.
1776 * Don't attempt NAK for
1779 brcmf_sdbrcm_rxfail(bus
, true, true);
1784 /* Checks the header */
1786 brcmf_check_rxbuf(struct brcmf_bus
*bus
, struct sk_buff
*pkt
, u8
*rxbuf
,
1787 u8 rxseq
, u16 nextlen
, u16
*len
)
1790 bool len_consistent
; /* Result of comparing readahead len and
1793 memcpy(bus
->rxhdr
, rxbuf
, SDPCM_HDRLEN
);
1795 /* Extract hardware header fields */
1796 *len
= get_unaligned_le16(bus
->rxhdr
);
1797 check
= get_unaligned_le16(bus
->rxhdr
+ sizeof(u16
));
1799 /* All zeros means readahead info was bad */
1800 if (!(*len
| check
)) {
1801 brcmf_dbg(INFO
, "(nextlen): read zeros in HW header???\n");
1805 /* Validate check bytes */
1806 if ((u16
)~(*len
^ check
)) {
1807 brcmf_dbg(ERROR
, "(nextlen): HW hdr error: nextlen/len/check 0x%04x/0x%04x/0x%04x\n",
1808 nextlen
, *len
, check
);
1810 brcmf_sdbrcm_rxfail(bus
, false, false);
1814 /* Validate frame length */
1815 if (*len
< SDPCM_HDRLEN
) {
1816 brcmf_dbg(ERROR
, "(nextlen): HW hdr length invalid: %d\n",
1821 /* Check for consistency with readahead info */
1822 len_consistent
= (nextlen
!= (roundup(*len
, 16) >> 4));
1823 if (len_consistent
) {
1824 /* Mismatch, force retry w/normal
1825 header (may be >4K) */
1826 brcmf_dbg(ERROR
, "(nextlen): mismatch, nextlen %d len %d rnd %d; expected rxseq %d\n",
1827 nextlen
, *len
, roundup(*len
, 16),
1829 brcmf_sdbrcm_rxfail(bus
, true, true);
1836 brcmf_sdbrcm_pktfree2(bus
, pkt
);
1840 /* Return true if there may be more frames to read */
1842 brcmf_sdbrcm_readframes(struct brcmf_bus
*bus
, uint maxframes
, bool *finished
)
1844 u16 len
, check
; /* Extracted hardware header fields */
1845 u8 chan
, seq
, doff
; /* Extracted software header fields */
1846 u8 fcbits
; /* Extracted fcbits from software header */
1848 struct sk_buff
*pkt
; /* Packet for event or data frames */
1849 u16 pad
; /* Number of pad bytes to read */
1850 u16 rdlen
; /* Total number of bytes to read */
1851 u8 rxseq
; /* Next sequence number to expect */
1852 uint rxleft
= 0; /* Remaining number of frames allowed */
1853 int sdret
; /* Return code from calls */
1854 u8 txmax
; /* Maximum tx sequence offered */
1857 uint rxcount
= 0; /* Total frames read */
1859 brcmf_dbg(TRACE
, "Enter\n");
1861 /* Not finished unless we encounter no more frames indication */
1864 for (rxseq
= bus
->rx_seq
, rxleft
= maxframes
;
1865 !bus
->rxskip
&& rxleft
&& bus
->drvr
->busstate
!= BRCMF_BUS_DOWN
;
1866 rxseq
++, rxleft
--) {
1868 /* Handle glomming separately */
1869 if (bus
->glomd
|| !skb_queue_empty(&bus
->glom
)) {
1871 brcmf_dbg(GLOM
, "calling rxglom: glomd %p, glom %p\n",
1872 bus
->glomd
, skb_peek(&bus
->glom
));
1873 cnt
= brcmf_sdbrcm_rxglom(bus
, rxseq
);
1874 brcmf_dbg(GLOM
, "rxglom returned %d\n", cnt
);
1876 rxleft
= (rxleft
> cnt
) ? (rxleft
- cnt
) : 1;
1880 /* Try doing single read if we can */
1882 u16 nextlen
= bus
->nextlen
;
1885 rdlen
= len
= nextlen
<< 4;
1886 brcmf_pad(bus
, &pad
, &rdlen
);
1889 * After the frame is received we have to
1890 * distinguish whether it is data
1891 * or non-data frame.
1893 brcmf_alloc_pkt_and_read(bus
, rdlen
, &pkt
, &rxbuf
);
1895 /* Give up on data, request rtx of events */
1896 brcmf_dbg(ERROR
, "(nextlen): brcmf_alloc_pkt_and_read failed: len %d rdlen %d expected rxseq %d\n",
1901 if (brcmf_check_rxbuf(bus
, pkt
, rxbuf
, rxseq
, nextlen
,
1905 /* Extract software header fields */
1906 chan
= SDPCM_PACKET_CHANNEL(
1907 &bus
->rxhdr
[SDPCM_FRAMETAG_LEN
]);
1908 seq
= SDPCM_PACKET_SEQUENCE(
1909 &bus
->rxhdr
[SDPCM_FRAMETAG_LEN
]);
1910 doff
= SDPCM_DOFFSET_VALUE(
1911 &bus
->rxhdr
[SDPCM_FRAMETAG_LEN
]);
1912 txmax
= SDPCM_WINDOW_VALUE(
1913 &bus
->rxhdr
[SDPCM_FRAMETAG_LEN
]);
1916 bus
->rxhdr
[SDPCM_FRAMETAG_LEN
+
1917 SDPCM_NEXTLEN_OFFSET
];
1918 if ((bus
->nextlen
<< 4) > MAX_RX_DATASZ
) {
1919 brcmf_dbg(INFO
, "(nextlen): got frame w/nextlen too large (%d), seq %d\n",
1924 bus
->drvr
->rx_readahead_cnt
++;
1926 /* Handle Flow Control */
1927 fcbits
= SDPCM_FCMASK_VALUE(
1928 &bus
->rxhdr
[SDPCM_FRAMETAG_LEN
]);
1930 if (bus
->flowcontrol
!= fcbits
) {
1931 if (~bus
->flowcontrol
& fcbits
)
1934 if (bus
->flowcontrol
& ~fcbits
)
1938 bus
->flowcontrol
= fcbits
;
1941 /* Check and update sequence number */
1943 brcmf_dbg(INFO
, "(nextlen): rx_seq %d, expected %d\n",
1949 /* Check window for sanity */
1950 if ((u8
) (txmax
- bus
->tx_seq
) > 0x40) {
1951 brcmf_dbg(ERROR
, "got unlikely tx max %d with tx_seq %d\n",
1952 txmax
, bus
->tx_seq
);
1953 txmax
= bus
->tx_seq
+ 2;
1955 bus
->tx_max
= txmax
;
1958 if (BRCMF_BYTES_ON() && BRCMF_DATA_ON()) {
1959 printk(KERN_DEBUG
"Rx Data:\n");
1960 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET
,
1962 } else if (BRCMF_HDRS_ON()) {
1963 printk(KERN_DEBUG
"RxHdr:\n");
1964 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET
,
1965 bus
->rxhdr
, SDPCM_HDRLEN
);
1969 if (chan
== SDPCM_CONTROL_CHANNEL
) {
1970 brcmf_dbg(ERROR
, "(nextlen): readahead on control packet %d?\n",
1972 /* Force retry w/normal header read */
1974 brcmf_sdbrcm_rxfail(bus
, false, true);
1975 brcmf_sdbrcm_pktfree2(bus
, pkt
);
1979 /* Validate data offset */
1980 if ((doff
< SDPCM_HDRLEN
) || (doff
> len
)) {
1981 brcmf_dbg(ERROR
, "(nextlen): bad data offset %d: HW len %d min %d\n",
1982 doff
, len
, SDPCM_HDRLEN
);
1983 brcmf_sdbrcm_rxfail(bus
, false, false);
1984 brcmf_sdbrcm_pktfree2(bus
, pkt
);
1988 /* All done with this one -- now deliver the packet */
1992 /* Read frame header (hardware and software) */
1993 sdret
= brcmf_sdcard_recv_buf(bus
->sdiodev
, bus
->sdiodev
->sbwad
,
1994 SDIO_FUNC_2
, F2SYNC
, bus
->rxhdr
,
1995 BRCMF_FIRSTREAD
, NULL
);
1999 brcmf_dbg(ERROR
, "RXHEADER FAILED: %d\n", sdret
);
2001 brcmf_sdbrcm_rxfail(bus
, true, true);
2005 if (BRCMF_BYTES_ON() || BRCMF_HDRS_ON()) {
2006 printk(KERN_DEBUG
"RxHdr:\n");
2007 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET
,
2008 bus
->rxhdr
, SDPCM_HDRLEN
);
2012 /* Extract hardware header fields */
2013 len
= get_unaligned_le16(bus
->rxhdr
);
2014 check
= get_unaligned_le16(bus
->rxhdr
+ sizeof(u16
));
2016 /* All zeros means no more frames */
2017 if (!(len
| check
)) {
2022 /* Validate check bytes */
2023 if ((u16
) ~(len
^ check
)) {
2024 brcmf_dbg(ERROR
, "HW hdr err: len/check 0x%04x/0x%04x\n",
2027 brcmf_sdbrcm_rxfail(bus
, false, false);
2031 /* Validate frame length */
2032 if (len
< SDPCM_HDRLEN
) {
2033 brcmf_dbg(ERROR
, "HW hdr length invalid: %d\n", len
);
2037 /* Extract software header fields */
2038 chan
= SDPCM_PACKET_CHANNEL(&bus
->rxhdr
[SDPCM_FRAMETAG_LEN
]);
2039 seq
= SDPCM_PACKET_SEQUENCE(&bus
->rxhdr
[SDPCM_FRAMETAG_LEN
]);
2040 doff
= SDPCM_DOFFSET_VALUE(&bus
->rxhdr
[SDPCM_FRAMETAG_LEN
]);
2041 txmax
= SDPCM_WINDOW_VALUE(&bus
->rxhdr
[SDPCM_FRAMETAG_LEN
]);
2043 /* Validate data offset */
2044 if ((doff
< SDPCM_HDRLEN
) || (doff
> len
)) {
2045 brcmf_dbg(ERROR
, "Bad data offset %d: HW len %d, min %d seq %d\n",
2046 doff
, len
, SDPCM_HDRLEN
, seq
);
2048 brcmf_sdbrcm_rxfail(bus
, false, false);
2052 /* Save the readahead length if there is one */
2054 bus
->rxhdr
[SDPCM_FRAMETAG_LEN
+ SDPCM_NEXTLEN_OFFSET
];
2055 if ((bus
->nextlen
<< 4) > MAX_RX_DATASZ
) {
2056 brcmf_dbg(INFO
, "(nextlen): got frame w/nextlen too large (%d), seq %d\n",
2061 /* Handle Flow Control */
2062 fcbits
= SDPCM_FCMASK_VALUE(&bus
->rxhdr
[SDPCM_FRAMETAG_LEN
]);
2064 if (bus
->flowcontrol
!= fcbits
) {
2065 if (~bus
->flowcontrol
& fcbits
)
2068 if (bus
->flowcontrol
& ~fcbits
)
2072 bus
->flowcontrol
= fcbits
;
2075 /* Check and update sequence number */
2077 brcmf_dbg(INFO
, "rx_seq %d, expected %d\n", seq
, rxseq
);
2082 /* Check window for sanity */
2083 if ((u8
) (txmax
- bus
->tx_seq
) > 0x40) {
2084 brcmf_dbg(ERROR
, "unlikely tx max %d with tx_seq %d\n",
2085 txmax
, bus
->tx_seq
);
2086 txmax
= bus
->tx_seq
+ 2;
2088 bus
->tx_max
= txmax
;
2090 /* Call a separate function for control frames */
2091 if (chan
== SDPCM_CONTROL_CHANNEL
) {
2092 brcmf_sdbrcm_read_control(bus
, bus
->rxhdr
, len
, doff
);
2096 /* precondition: chan is either SDPCM_DATA_CHANNEL,
2097 SDPCM_EVENT_CHANNEL, SDPCM_TEST_CHANNEL or
2098 SDPCM_GLOM_CHANNEL */
2100 /* Length to read */
2101 rdlen
= (len
> BRCMF_FIRSTREAD
) ? (len
- BRCMF_FIRSTREAD
) : 0;
2103 /* May pad read to blocksize for efficiency */
2104 if (bus
->roundup
&& bus
->blocksize
&&
2105 (rdlen
> bus
->blocksize
)) {
2106 pad
= bus
->blocksize
- (rdlen
% bus
->blocksize
);
2107 if ((pad
<= bus
->roundup
) && (pad
< bus
->blocksize
) &&
2108 ((rdlen
+ pad
+ BRCMF_FIRSTREAD
) < MAX_RX_DATASZ
))
2110 } else if (rdlen
% BRCMF_SDALIGN
) {
2111 rdlen
+= BRCMF_SDALIGN
- (rdlen
% BRCMF_SDALIGN
);
2114 /* Satisfy length-alignment requirements */
2115 if (rdlen
& (ALIGNMENT
- 1))
2116 rdlen
= roundup(rdlen
, ALIGNMENT
);
2118 if ((rdlen
+ BRCMF_FIRSTREAD
) > MAX_RX_DATASZ
) {
2119 /* Too long -- skip this frame */
2120 brcmf_dbg(ERROR
, "too long: len %d rdlen %d\n",
2122 bus
->drvr
->rx_errors
++;
2124 brcmf_sdbrcm_rxfail(bus
, false, false);
2128 pkt
= brcmu_pkt_buf_get_skb(rdlen
+
2129 BRCMF_FIRSTREAD
+ BRCMF_SDALIGN
);
2131 /* Give up on data, request rtx of events */
2132 brcmf_dbg(ERROR
, "brcmu_pkt_buf_get_skb failed: rdlen %d chan %d\n",
2134 bus
->drvr
->rx_dropped
++;
2135 brcmf_sdbrcm_rxfail(bus
, false, RETRYCHAN(chan
));
2139 /* Leave room for what we already read, and align remainder */
2140 skb_pull(pkt
, BRCMF_FIRSTREAD
);
2141 pkt_align(pkt
, rdlen
, BRCMF_SDALIGN
);
2143 /* Read the remaining frame data */
2144 sdret
= brcmf_sdcard_recv_buf(bus
->sdiodev
, bus
->sdiodev
->sbwad
,
2145 SDIO_FUNC_2
, F2SYNC
, ((u8
*) (pkt
->data
)),
2150 brcmf_dbg(ERROR
, "read %d %s bytes failed: %d\n", rdlen
,
2151 ((chan
== SDPCM_EVENT_CHANNEL
) ? "event"
2152 : ((chan
== SDPCM_DATA_CHANNEL
) ? "data"
2154 brcmu_pkt_buf_free_skb(pkt
);
2155 bus
->drvr
->rx_errors
++;
2156 brcmf_sdbrcm_rxfail(bus
, true, RETRYCHAN(chan
));
2160 /* Copy the already-read portion */
2161 skb_push(pkt
, BRCMF_FIRSTREAD
);
2162 memcpy(pkt
->data
, bus
->rxhdr
, BRCMF_FIRSTREAD
);
2165 if (BRCMF_BYTES_ON() && BRCMF_DATA_ON()) {
2166 printk(KERN_DEBUG
"Rx Data:\n");
2167 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET
,
2173 /* Save superframe descriptor and allocate packet frame */
2174 if (chan
== SDPCM_GLOM_CHANNEL
) {
2175 if (SDPCM_GLOMDESC(&bus
->rxhdr
[SDPCM_FRAMETAG_LEN
])) {
2176 brcmf_dbg(GLOM
, "glom descriptor, %d bytes:\n",
2179 if (BRCMF_GLOM_ON()) {
2180 printk(KERN_DEBUG
"Glom Data:\n");
2181 print_hex_dump_bytes("",
2186 __skb_trim(pkt
, len
);
2187 skb_pull(pkt
, SDPCM_HDRLEN
);
2190 brcmf_dbg(ERROR
, "%s: glom superframe w/o "
2191 "descriptor!\n", __func__
);
2192 brcmf_sdbrcm_rxfail(bus
, false, false);
2197 /* Fill in packet len and prio, deliver upward */
2198 __skb_trim(pkt
, len
);
2199 skb_pull(pkt
, doff
);
2201 if (pkt
->len
== 0) {
2202 brcmu_pkt_buf_free_skb(pkt
);
2204 } else if (brcmf_proto_hdrpull(bus
->drvr
, &ifidx
, pkt
) != 0) {
2205 brcmf_dbg(ERROR
, "rx protocol error\n");
2206 brcmu_pkt_buf_free_skb(pkt
);
2207 bus
->drvr
->rx_errors
++;
2211 /* Unlock during rx call */
2213 brcmf_rx_frame(bus
->drvr
, ifidx
, pkt
, 1);
2216 rxcount
= maxframes
- rxleft
;
2218 /* Message if we hit the limit */
2220 brcmf_dbg(DATA
, "hit rx limit of %d frames\n",
2224 brcmf_dbg(DATA
, "processed %d frames\n", rxcount
);
2225 /* Back off rxseq if awaiting rtx, update rx_seq */
2228 bus
->rx_seq
= rxseq
;
2234 brcmf_sdbrcm_send_buf(struct brcmf_bus
*bus
, u32 addr
, uint fn
, uint flags
,
2235 u8
*buf
, uint nbytes
, struct sk_buff
*pkt
)
2237 return brcmf_sdcard_send_buf
2238 (bus
->sdiodev
, addr
, fn
, flags
, buf
, nbytes
, pkt
);
2242 brcmf_sdbrcm_wait_for_event(struct brcmf_bus
*bus
, bool *lockvar
)
2245 wait_event_interruptible_timeout(bus
->ctrl_wait
,
2246 (*lockvar
== false), HZ
* 2);
2252 brcmf_sdbrcm_wait_event_wakeup(struct brcmf_bus
*bus
)
2254 if (waitqueue_active(&bus
->ctrl_wait
))
2255 wake_up_interruptible(&bus
->ctrl_wait
);
2259 /* Writes a HW/SW header into the packet and sends it. */
2260 /* Assumes: (a) header space already there, (b) caller holds lock */
2261 static int brcmf_sdbrcm_txpkt(struct brcmf_bus
*bus
, struct sk_buff
*pkt
,
2262 uint chan
, bool free_pkt
)
2268 struct sk_buff
*new;
2271 brcmf_dbg(TRACE
, "Enter\n");
2273 frame
= (u8
*) (pkt
->data
);
2275 /* Add alignment padding, allocate new packet if needed */
2276 pad
= ((unsigned long)frame
% BRCMF_SDALIGN
);
2278 if (skb_headroom(pkt
) < pad
) {
2279 brcmf_dbg(INFO
, "insufficient headroom %d for %d pad\n",
2280 skb_headroom(pkt
), pad
);
2281 bus
->drvr
->tx_realloc
++;
2282 new = brcmu_pkt_buf_get_skb(pkt
->len
+ BRCMF_SDALIGN
);
2284 brcmf_dbg(ERROR
, "couldn't allocate new %d-byte packet\n",
2285 pkt
->len
+ BRCMF_SDALIGN
);
2290 pkt_align(new, pkt
->len
, BRCMF_SDALIGN
);
2291 memcpy(new->data
, pkt
->data
, pkt
->len
);
2293 brcmu_pkt_buf_free_skb(pkt
);
2294 /* free the pkt if canned one is not used */
2297 frame
= (u8
*) (pkt
->data
);
2298 /* precondition: (frame % BRCMF_SDALIGN) == 0) */
2302 frame
= (u8
*) (pkt
->data
);
2303 /* precondition: pad + SDPCM_HDRLEN <= pkt->len */
2304 memset(frame
, 0, pad
+ SDPCM_HDRLEN
);
2307 /* precondition: pad < BRCMF_SDALIGN */
2309 /* Hardware tag: 2 byte len followed by 2 byte ~len check (all LE) */
2310 len
= (u16
) (pkt
->len
);
2311 *(__le16
*) frame
= cpu_to_le16(len
);
2312 *(((__le16
*) frame
) + 1) = cpu_to_le16(~len
);
2314 /* Software tag: channel, sequence number, data offset */
2316 ((chan
<< SDPCM_CHANNEL_SHIFT
) & SDPCM_CHANNEL_MASK
) | bus
->tx_seq
|
2318 SDPCM_HDRLEN
) << SDPCM_DOFFSET_SHIFT
) & SDPCM_DOFFSET_MASK
);
2320 put_unaligned_le32(swheader
, frame
+ SDPCM_FRAMETAG_LEN
);
2321 put_unaligned_le32(0, frame
+ SDPCM_FRAMETAG_LEN
+ sizeof(swheader
));
2324 tx_packets
[pkt
->priority
]++;
2325 if (BRCMF_BYTES_ON() &&
2326 (((BRCMF_CTL_ON() && (chan
== SDPCM_CONTROL_CHANNEL
)) ||
2327 (BRCMF_DATA_ON() && (chan
!= SDPCM_CONTROL_CHANNEL
))))) {
2328 printk(KERN_DEBUG
"Tx Frame:\n");
2329 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET
, frame
, len
);
2330 } else if (BRCMF_HDRS_ON()) {
2331 printk(KERN_DEBUG
"TxHdr:\n");
2332 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET
,
2333 frame
, min_t(u16
, len
, 16));
2337 /* Raise len to next SDIO block to eliminate tail command */
2338 if (bus
->roundup
&& bus
->blocksize
&& (len
> bus
->blocksize
)) {
2339 u16 pad
= bus
->blocksize
- (len
% bus
->blocksize
);
2340 if ((pad
<= bus
->roundup
) && (pad
< bus
->blocksize
))
2342 } else if (len
% BRCMF_SDALIGN
) {
2343 len
+= BRCMF_SDALIGN
- (len
% BRCMF_SDALIGN
);
2346 /* Some controllers have trouble with odd bytes -- round to even */
2347 if (len
& (ALIGNMENT
- 1))
2348 len
= roundup(len
, ALIGNMENT
);
2350 ret
= brcmf_sdbrcm_send_buf(bus
, bus
->sdiodev
->sbwad
,
2351 SDIO_FUNC_2
, F2SYNC
, frame
,
2356 /* On failure, abort the command and terminate the frame */
2357 brcmf_dbg(INFO
, "sdio error %d, abort command and terminate frame\n",
2361 brcmf_sdcard_abort(bus
->sdiodev
, SDIO_FUNC_2
);
2362 brcmf_sdcard_cfg_write(bus
->sdiodev
, SDIO_FUNC_1
,
2363 SBSDIO_FUNC1_FRAMECTRL
, SFC_WF_TERM
,
2367 for (i
= 0; i
< 3; i
++) {
2369 hi
= brcmf_sdcard_cfg_read(bus
->sdiodev
,
2371 SBSDIO_FUNC1_WFRAMEBCHI
,
2373 lo
= brcmf_sdcard_cfg_read(bus
->sdiodev
,
2375 SBSDIO_FUNC1_WFRAMEBCLO
,
2377 bus
->f1regdata
+= 2;
2378 if ((hi
== 0) && (lo
== 0))
2384 bus
->tx_seq
= (bus
->tx_seq
+ 1) % SDPCM_SEQUENCE_WRAP
;
2387 /* restore pkt buffer pointer before calling tx complete routine */
2388 skb_pull(pkt
, SDPCM_HDRLEN
+ pad
);
2390 brcmf_txcomplete(bus
->drvr
, pkt
, ret
!= 0);
2394 brcmu_pkt_buf_free_skb(pkt
);
2399 static uint
brcmf_sdbrcm_sendfromq(struct brcmf_bus
*bus
, uint maxframes
)
2401 struct sk_buff
*pkt
;
2404 int ret
= 0, prec_out
;
2409 struct brcmf_pub
*drvr
= bus
->drvr
;
2411 brcmf_dbg(TRACE
, "Enter\n");
2413 tx_prec_map
= ~bus
->flowcontrol
;
2415 /* Send frames until the limit or some other event */
2416 for (cnt
= 0; (cnt
< maxframes
) && data_ok(bus
); cnt
++) {
2417 spin_lock_bh(&bus
->txqlock
);
2418 pkt
= brcmu_pktq_mdeq(&bus
->txq
, tx_prec_map
, &prec_out
);
2420 spin_unlock_bh(&bus
->txqlock
);
2423 spin_unlock_bh(&bus
->txqlock
);
2424 datalen
= pkt
->len
- SDPCM_HDRLEN
;
2426 ret
= brcmf_sdbrcm_txpkt(bus
, pkt
, SDPCM_DATA_CHANNEL
, true);
2428 bus
->drvr
->tx_errors
++;
2430 bus
->drvr
->dstats
.tx_bytes
+= datalen
;
2432 /* In poll mode, need to check for other events */
2433 if (!bus
->intr
&& cnt
) {
2434 /* Check device status, signal pending interrupt */
2435 r_sdreg32(bus
, &intstatus
,
2436 offsetof(struct sdpcmd_regs
, intstatus
),
2439 if (brcmf_sdcard_regfail(bus
->sdiodev
))
2441 if (intstatus
& bus
->hostintmask
)
2446 /* Deflow-control stack if needed */
2447 if (drvr
->up
&& (drvr
->busstate
== BRCMF_BUS_DATA
) &&
2448 drvr
->txoff
&& (pktq_len(&bus
->txq
) < TXLOW
))
2449 brcmf_txflowcontrol(drvr
, 0, OFF
);
2454 static bool brcmf_sdbrcm_dpc(struct brcmf_bus
*bus
)
2456 u32 intstatus
, newstatus
= 0;
2458 uint rxlimit
= bus
->rxbound
; /* Rx frames to read before resched */
2459 uint txlimit
= bus
->txbound
; /* Tx frames to send before resched */
2460 uint framecnt
= 0; /* Temporary counter of tx/rx frames */
2461 bool rxdone
= true; /* Flag for no more read data */
2462 bool resched
= false; /* Flag indicating resched wanted */
2464 brcmf_dbg(TRACE
, "Enter\n");
2466 /* Start with leftover status bits */
2467 intstatus
= bus
->intstatus
;
2471 /* If waiting for HTAVAIL, check status */
2472 if (bus
->clkstate
== CLK_PENDING
) {
2474 u8 clkctl
, devctl
= 0;
2477 /* Check for inconsistent device control */
2478 devctl
= brcmf_sdcard_cfg_read(bus
->sdiodev
, SDIO_FUNC_1
,
2479 SBSDIO_DEVICE_CTL
, &err
);
2481 brcmf_dbg(ERROR
, "error reading DEVCTL: %d\n", err
);
2482 bus
->drvr
->busstate
= BRCMF_BUS_DOWN
;
2486 /* Read CSR, if clock on switch to AVAIL, else ignore */
2487 clkctl
= brcmf_sdcard_cfg_read(bus
->sdiodev
, SDIO_FUNC_1
,
2488 SBSDIO_FUNC1_CHIPCLKCSR
, &err
);
2490 brcmf_dbg(ERROR
, "error reading CSR: %d\n",
2492 bus
->drvr
->busstate
= BRCMF_BUS_DOWN
;
2495 brcmf_dbg(INFO
, "DPC: PENDING, devctl 0x%02x clkctl 0x%02x\n",
2498 if (SBSDIO_HTAV(clkctl
)) {
2499 devctl
= brcmf_sdcard_cfg_read(bus
->sdiodev
,
2501 SBSDIO_DEVICE_CTL
, &err
);
2503 brcmf_dbg(ERROR
, "error reading DEVCTL: %d\n",
2505 bus
->drvr
->busstate
= BRCMF_BUS_DOWN
;
2507 devctl
&= ~SBSDIO_DEVCTL_CA_INT_ONLY
;
2508 brcmf_sdcard_cfg_write(bus
->sdiodev
, SDIO_FUNC_1
,
2509 SBSDIO_DEVICE_CTL
, devctl
, &err
);
2511 brcmf_dbg(ERROR
, "error writing DEVCTL: %d\n",
2513 bus
->drvr
->busstate
= BRCMF_BUS_DOWN
;
2515 bus
->clkstate
= CLK_AVAIL
;
2523 /* Make sure backplane clock is on */
2524 brcmf_sdbrcm_clkctl(bus
, CLK_AVAIL
, true);
2525 if (bus
->clkstate
== CLK_PENDING
)
2528 /* Pending interrupt indicates new device status */
2531 r_sdreg32(bus
, &newstatus
,
2532 offsetof(struct sdpcmd_regs
, intstatus
), &retries
);
2534 if (brcmf_sdcard_regfail(bus
->sdiodev
))
2536 newstatus
&= bus
->hostintmask
;
2537 bus
->fcstate
= !!(newstatus
& I_HMB_FC_STATE
);
2539 w_sdreg32(bus
, newstatus
,
2540 offsetof(struct sdpcmd_regs
, intstatus
),
2546 /* Merge new bits with previous */
2547 intstatus
|= newstatus
;
2550 /* Handle flow-control change: read new state in case our ack
2551 * crossed another change interrupt. If change still set, assume
2552 * FC ON for safety, let next loop through do the debounce.
2554 if (intstatus
& I_HMB_FC_CHANGE
) {
2555 intstatus
&= ~I_HMB_FC_CHANGE
;
2556 w_sdreg32(bus
, I_HMB_FC_CHANGE
,
2557 offsetof(struct sdpcmd_regs
, intstatus
), &retries
);
2559 r_sdreg32(bus
, &newstatus
,
2560 offsetof(struct sdpcmd_regs
, intstatus
), &retries
);
2561 bus
->f1regdata
+= 2;
2563 !!(newstatus
& (I_HMB_FC_STATE
| I_HMB_FC_CHANGE
));
2564 intstatus
|= (newstatus
& bus
->hostintmask
);
2567 /* Handle host mailbox indication */
2568 if (intstatus
& I_HMB_HOST_INT
) {
2569 intstatus
&= ~I_HMB_HOST_INT
;
2570 intstatus
|= brcmf_sdbrcm_hostmail(bus
);
2573 /* Generally don't ask for these, can get CRC errors... */
2574 if (intstatus
& I_WR_OOSYNC
) {
2575 brcmf_dbg(ERROR
, "Dongle reports WR_OOSYNC\n");
2576 intstatus
&= ~I_WR_OOSYNC
;
2579 if (intstatus
& I_RD_OOSYNC
) {
2580 brcmf_dbg(ERROR
, "Dongle reports RD_OOSYNC\n");
2581 intstatus
&= ~I_RD_OOSYNC
;
2584 if (intstatus
& I_SBINT
) {
2585 brcmf_dbg(ERROR
, "Dongle reports SBINT\n");
2586 intstatus
&= ~I_SBINT
;
2589 /* Would be active due to wake-wlan in gSPI */
2590 if (intstatus
& I_CHIPACTIVE
) {
2591 brcmf_dbg(INFO
, "Dongle reports CHIPACTIVE\n");
2592 intstatus
&= ~I_CHIPACTIVE
;
2595 /* Ignore frame indications if rxskip is set */
2597 intstatus
&= ~I_HMB_FRAME_IND
;
2599 /* On frame indication, read available frames */
2600 if (PKT_AVAILABLE()) {
2601 framecnt
= brcmf_sdbrcm_readframes(bus
, rxlimit
, &rxdone
);
2602 if (rxdone
|| bus
->rxskip
)
2603 intstatus
&= ~I_HMB_FRAME_IND
;
2604 rxlimit
-= min(framecnt
, rxlimit
);
2607 /* Keep still-pending events for next scheduling */
2608 bus
->intstatus
= intstatus
;
2611 if (data_ok(bus
) && bus
->ctrl_frame_stat
&&
2612 (bus
->clkstate
== CLK_AVAIL
)) {
2615 ret
= brcmf_sdbrcm_send_buf(bus
, bus
->sdiodev
->sbwad
,
2616 SDIO_FUNC_2
, F2SYNC
, (u8
*) bus
->ctrl_frame_buf
,
2617 (u32
) bus
->ctrl_frame_len
, NULL
);
2620 /* On failure, abort the command and
2621 terminate the frame */
2622 brcmf_dbg(INFO
, "sdio error %d, abort command and terminate frame\n",
2626 brcmf_sdcard_abort(bus
->sdiodev
, SDIO_FUNC_2
);
2628 brcmf_sdcard_cfg_write(bus
->sdiodev
, SDIO_FUNC_1
,
2629 SBSDIO_FUNC1_FRAMECTRL
, SFC_WF_TERM
,
2633 for (i
= 0; i
< 3; i
++) {
2635 hi
= brcmf_sdcard_cfg_read(bus
->sdiodev
,
2637 SBSDIO_FUNC1_WFRAMEBCHI
,
2639 lo
= brcmf_sdcard_cfg_read(bus
->sdiodev
,
2641 SBSDIO_FUNC1_WFRAMEBCLO
,
2643 bus
->f1regdata
+= 2;
2644 if ((hi
== 0) && (lo
== 0))
2650 bus
->tx_seq
= (bus
->tx_seq
+ 1) % SDPCM_SEQUENCE_WRAP
;
2652 brcmf_dbg(INFO
, "Return_dpc value is : %d\n", ret
);
2653 bus
->ctrl_frame_stat
= false;
2654 brcmf_sdbrcm_wait_event_wakeup(bus
);
2656 /* Send queued frames (limit 1 if rx may still be pending) */
2657 else if ((bus
->clkstate
== CLK_AVAIL
) && !bus
->fcstate
&&
2658 brcmu_pktq_mlen(&bus
->txq
, ~bus
->flowcontrol
) && txlimit
2660 framecnt
= rxdone
? txlimit
: min(txlimit
, bus
->txminmax
);
2661 framecnt
= brcmf_sdbrcm_sendfromq(bus
, framecnt
);
2662 txlimit
-= framecnt
;
2665 /* Resched if events or tx frames are pending,
2666 else await next interrupt */
2667 /* On failed register access, all bets are off:
2668 no resched or interrupts */
2669 if ((bus
->drvr
->busstate
== BRCMF_BUS_DOWN
) ||
2670 brcmf_sdcard_regfail(bus
->sdiodev
)) {
2671 brcmf_dbg(ERROR
, "failed backplane access over SDIO, halting operation %d\n",
2672 brcmf_sdcard_regfail(bus
->sdiodev
));
2673 bus
->drvr
->busstate
= BRCMF_BUS_DOWN
;
2675 } else if (bus
->clkstate
== CLK_PENDING
) {
2676 brcmf_dbg(INFO
, "rescheduled due to CLK_PENDING awaiting I_CHIPACTIVE interrupt\n");
2678 } else if (bus
->intstatus
|| bus
->ipend
||
2679 (!bus
->fcstate
&& brcmu_pktq_mlen(&bus
->txq
, ~bus
->flowcontrol
)
2680 && data_ok(bus
)) || PKT_AVAILABLE()) {
2684 bus
->dpc_sched
= resched
;
2686 /* If we're done for now, turn off clock request. */
2687 if ((bus
->clkstate
!= CLK_PENDING
)
2688 && bus
->idletime
== BRCMF_IDLE_IMMEDIATE
) {
2689 bus
->activity
= false;
2690 brcmf_sdbrcm_clkctl(bus
, CLK_NONE
, false);
2698 static int brcmf_sdbrcm_dpc_thread(void *data
)
2700 struct brcmf_bus
*bus
= (struct brcmf_bus
*) data
;
2702 allow_signal(SIGTERM
);
2703 /* Run until signal received */
2705 if (kthread_should_stop())
2707 if (!wait_for_completion_interruptible(&bus
->dpc_wait
)) {
2708 /* Call bus dpc unless it indicated down
2709 (then clean stop) */
2710 if (bus
->drvr
->busstate
!= BRCMF_BUS_DOWN
) {
2711 if (brcmf_sdbrcm_dpc(bus
))
2712 complete(&bus
->dpc_wait
);
2714 /* after stopping the bus, exit thread */
2715 brcmf_sdbrcm_bus_stop(bus
);
2716 bus
->dpc_tsk
= NULL
;
2725 int brcmf_sdbrcm_bus_txdata(struct brcmf_bus
*bus
, struct sk_buff
*pkt
)
2730 brcmf_dbg(TRACE
, "Enter\n");
2734 /* Add space for the header */
2735 skb_push(pkt
, SDPCM_HDRLEN
);
2736 /* precondition: IS_ALIGNED((unsigned long)(pkt->data), 2) */
2738 prec
= prio2prec((pkt
->priority
& PRIOMASK
));
2740 /* Check for existing queue, current flow-control,
2741 pending event, or pending clock */
2742 brcmf_dbg(TRACE
, "deferring pktq len %d\n", pktq_len(&bus
->txq
));
2745 /* Priority based enq */
2746 spin_lock_bh(&bus
->txqlock
);
2747 if (brcmf_c_prec_enq(bus
->drvr
, &bus
->txq
, pkt
, prec
) == false) {
2748 skb_pull(pkt
, SDPCM_HDRLEN
);
2749 brcmf_txcomplete(bus
->drvr
, pkt
, false);
2750 brcmu_pkt_buf_free_skb(pkt
);
2751 brcmf_dbg(ERROR
, "out of bus->txq !!!\n");
2756 spin_unlock_bh(&bus
->txqlock
);
2758 if (pktq_len(&bus
->txq
) >= TXHI
)
2759 brcmf_txflowcontrol(bus
->drvr
, 0, ON
);
2762 if (pktq_plen(&bus
->txq
, prec
) > qcount
[prec
])
2763 qcount
[prec
] = pktq_plen(&bus
->txq
, prec
);
2765 /* Schedule DPC if needed to send queued packet(s) */
2766 if (!bus
->dpc_sched
) {
2767 bus
->dpc_sched
= true;
2769 complete(&bus
->dpc_wait
);
2776 brcmf_sdbrcm_membytes(struct brcmf_bus
*bus
, bool write
, u32 address
, u8
*data
,
2783 /* Determine initial transfer parameters */
2784 sdaddr
= address
& SBSDIO_SB_OFT_ADDR_MASK
;
2785 if ((sdaddr
+ size
) & SBSDIO_SBWINDOW_MASK
)
2786 dsize
= (SBSDIO_SB_OFT_ADDR_LIMIT
- sdaddr
);
2790 /* Set the backplane window to include the start address */
2791 bcmerror
= brcmf_sdcard_set_sbaddr_window(bus
->sdiodev
, address
);
2793 brcmf_dbg(ERROR
, "window change failed\n");
2797 /* Do the transfer(s) */
2799 brcmf_dbg(INFO
, "%s %d bytes at offset 0x%08x in window 0x%08x\n",
2800 write
? "write" : "read", dsize
,
2801 sdaddr
, address
& SBSDIO_SBWINDOW_MASK
);
2802 bcmerror
= brcmf_sdcard_rwdata(bus
->sdiodev
, write
,
2803 sdaddr
, data
, dsize
);
2805 brcmf_dbg(ERROR
, "membytes transfer failed\n");
2809 /* Adjust for next transfer (if any) */
2814 bcmerror
= brcmf_sdcard_set_sbaddr_window(bus
->sdiodev
,
2817 brcmf_dbg(ERROR
, "window change failed\n");
2821 dsize
= min_t(uint
, SBSDIO_SB_OFT_ADDR_LIMIT
, size
);
2826 /* Return the window to backplane enumeration space for core access */
2827 if (brcmf_sdcard_set_sbaddr_window(bus
->sdiodev
, bus
->sdiodev
->sbwad
))
2828 brcmf_dbg(ERROR
, "FAILED to set window back to 0x%x\n",
2829 bus
->sdiodev
->sbwad
);
2835 #define CONSOLE_LINE_MAX 192
2837 static int brcmf_sdbrcm_readconsole(struct brcmf_bus
*bus
)
2839 struct brcmf_console
*c
= &bus
->console
;
2840 u8 line
[CONSOLE_LINE_MAX
], ch
;
2844 /* Don't do anything until FWREADY updates console address */
2845 if (bus
->console_addr
== 0)
2848 /* Read console log struct */
2849 addr
= bus
->console_addr
+ offsetof(struct rte_console
, log_le
);
2850 rv
= brcmf_sdbrcm_membytes(bus
, false, addr
, (u8
*)&c
->log_le
,
2855 /* Allocate console buffer (one time only) */
2856 if (c
->buf
== NULL
) {
2857 c
->bufsize
= le32_to_cpu(c
->log_le
.buf_size
);
2858 c
->buf
= kmalloc(c
->bufsize
, GFP_ATOMIC
);
2863 idx
= le32_to_cpu(c
->log_le
.idx
);
2865 /* Protect against corrupt value */
2866 if (idx
> c
->bufsize
)
2869 /* Skip reading the console buffer if the index pointer
2874 /* Read the console buffer */
2875 addr
= le32_to_cpu(c
->log_le
.buf
);
2876 rv
= brcmf_sdbrcm_membytes(bus
, false, addr
, c
->buf
, c
->bufsize
);
2880 while (c
->last
!= idx
) {
2881 for (n
= 0; n
< CONSOLE_LINE_MAX
- 2; n
++) {
2882 if (c
->last
== idx
) {
2883 /* This would output a partial line.
2885 * the buffer pointer and output this
2886 * line next time around.
2891 c
->last
= c
->bufsize
- n
;
2894 ch
= c
->buf
[c
->last
];
2895 c
->last
= (c
->last
+ 1) % c
->bufsize
;
2902 if (line
[n
- 1] == '\r')
2905 printk(KERN_DEBUG
"CONSOLE: %s\n", line
);
2914 static int brcmf_tx_frame(struct brcmf_bus
*bus
, u8
*frame
, u16 len
)
2919 bus
->ctrl_frame_stat
= false;
2920 ret
= brcmf_sdbrcm_send_buf(bus
, bus
->sdiodev
->sbwad
,
2921 SDIO_FUNC_2
, F2SYNC
, frame
, len
, NULL
);
2924 /* On failure, abort the command and terminate the frame */
2925 brcmf_dbg(INFO
, "sdio error %d, abort command and terminate frame\n",
2929 brcmf_sdcard_abort(bus
->sdiodev
, SDIO_FUNC_2
);
2931 brcmf_sdcard_cfg_write(bus
->sdiodev
, SDIO_FUNC_1
,
2932 SBSDIO_FUNC1_FRAMECTRL
,
2936 for (i
= 0; i
< 3; i
++) {
2938 hi
= brcmf_sdcard_cfg_read(bus
->sdiodev
, SDIO_FUNC_1
,
2939 SBSDIO_FUNC1_WFRAMEBCHI
,
2941 lo
= brcmf_sdcard_cfg_read(bus
->sdiodev
, SDIO_FUNC_1
,
2942 SBSDIO_FUNC1_WFRAMEBCLO
,
2944 bus
->f1regdata
+= 2;
2945 if (hi
== 0 && lo
== 0)
2951 bus
->tx_seq
= (bus
->tx_seq
+ 1) % SDPCM_SEQUENCE_WRAP
;
2957 brcmf_sdbrcm_bus_txctl(struct brcmf_bus
*bus
, unsigned char *msg
, uint msglen
)
2966 brcmf_dbg(TRACE
, "Enter\n");
2968 /* Back the pointer to make a room for bus header */
2969 frame
= msg
- SDPCM_HDRLEN
;
2970 len
= (msglen
+= SDPCM_HDRLEN
);
2972 /* Add alignment padding (optional for ctl frames) */
2973 doff
= ((unsigned long)frame
% BRCMF_SDALIGN
);
2978 memset(frame
, 0, doff
+ SDPCM_HDRLEN
);
2980 /* precondition: doff < BRCMF_SDALIGN */
2981 doff
+= SDPCM_HDRLEN
;
2983 /* Round send length to next SDIO block */
2984 if (bus
->roundup
&& bus
->blocksize
&& (len
> bus
->blocksize
)) {
2985 u16 pad
= bus
->blocksize
- (len
% bus
->blocksize
);
2986 if ((pad
<= bus
->roundup
) && (pad
< bus
->blocksize
))
2988 } else if (len
% BRCMF_SDALIGN
) {
2989 len
+= BRCMF_SDALIGN
- (len
% BRCMF_SDALIGN
);
2992 /* Satisfy length-alignment requirements */
2993 if (len
& (ALIGNMENT
- 1))
2994 len
= roundup(len
, ALIGNMENT
);
2996 /* precondition: IS_ALIGNED((unsigned long)frame, 2) */
2998 /* Need to lock here to protect txseq and SDIO tx calls */
3003 /* Make sure backplane clock is on */
3004 brcmf_sdbrcm_clkctl(bus
, CLK_AVAIL
, false);
3006 /* Hardware tag: 2 byte len followed by 2 byte ~len check (all LE) */
3007 *(__le16
*) frame
= cpu_to_le16((u16
) msglen
);
3008 *(((__le16
*) frame
) + 1) = cpu_to_le16(~msglen
);
3010 /* Software tag: channel, sequence number, data offset */
3012 ((SDPCM_CONTROL_CHANNEL
<< SDPCM_CHANNEL_SHIFT
) &
3014 | bus
->tx_seq
| ((doff
<< SDPCM_DOFFSET_SHIFT
) &
3015 SDPCM_DOFFSET_MASK
);
3016 put_unaligned_le32(swheader
, frame
+ SDPCM_FRAMETAG_LEN
);
3017 put_unaligned_le32(0, frame
+ SDPCM_FRAMETAG_LEN
+ sizeof(swheader
));
3019 if (!data_ok(bus
)) {
3020 brcmf_dbg(INFO
, "No bus credit bus->tx_max %d, bus->tx_seq %d\n",
3021 bus
->tx_max
, bus
->tx_seq
);
3022 bus
->ctrl_frame_stat
= true;
3024 bus
->ctrl_frame_buf
= frame
;
3025 bus
->ctrl_frame_len
= len
;
3027 brcmf_sdbrcm_wait_for_event(bus
, &bus
->ctrl_frame_stat
);
3029 if (bus
->ctrl_frame_stat
== false) {
3030 brcmf_dbg(INFO
, "ctrl_frame_stat == false\n");
3033 brcmf_dbg(INFO
, "ctrl_frame_stat == true\n");
3040 if (BRCMF_BYTES_ON() && BRCMF_CTL_ON()) {
3041 printk(KERN_DEBUG
"Tx Frame:\n");
3042 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET
,
3044 } else if (BRCMF_HDRS_ON()) {
3045 printk(KERN_DEBUG
"TxHdr:\n");
3046 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET
,
3047 frame
, min_t(u16
, len
, 16));
3052 ret
= brcmf_tx_frame(bus
, frame
, len
);
3053 } while (ret
< 0 && retries
++ < TXRETRIES
);
3056 if ((bus
->idletime
== BRCMF_IDLE_IMMEDIATE
) && !bus
->dpc_sched
) {
3057 bus
->activity
= false;
3058 brcmf_sdbrcm_clkctl(bus
, CLK_NONE
, true);
3064 bus
->drvr
->tx_ctlerrs
++;
3066 bus
->drvr
->tx_ctlpkts
++;
3068 return ret
? -EIO
: 0;
3072 brcmf_sdbrcm_bus_rxctl(struct brcmf_bus
*bus
, unsigned char *msg
, uint msglen
)
3078 brcmf_dbg(TRACE
, "Enter\n");
3080 /* Wait until control frame is available */
3081 timeleft
= brcmf_sdbrcm_dcmd_resp_wait(bus
, &bus
->rxlen
, &pending
);
3085 memcpy(msg
, bus
->rxctl
, min(msglen
, rxlen
));
3090 brcmf_dbg(CTL
, "resumed on rxctl frame, got %d expected %d\n",
3092 } else if (timeleft
== 0) {
3093 brcmf_dbg(ERROR
, "resumed on timeout\n");
3094 } else if (pending
== true) {
3095 brcmf_dbg(CTL
, "cancelled\n");
3096 return -ERESTARTSYS
;
3098 brcmf_dbg(CTL
, "resumed for unknown reason?\n");
3102 bus
->drvr
->rx_ctlpkts
++;
3104 bus
->drvr
->rx_ctlerrs
++;
3106 return rxlen
? (int)rxlen
: -ETIMEDOUT
;
3109 static int brcmf_sdbrcm_downloadvars(struct brcmf_bus
*bus
, void *arg
, int len
)
3113 brcmf_dbg(TRACE
, "Enter\n");
3115 /* Basic sanity checks */
3116 if (bus
->drvr
->up
) {
3117 bcmerror
= -EISCONN
;
3121 bcmerror
= -EOVERFLOW
;
3125 /* Free the old ones and replace with passed variables */
3128 bus
->vars
= kmalloc(len
, GFP_ATOMIC
);
3129 bus
->varsz
= bus
->vars
? len
: 0;
3130 if (bus
->vars
== NULL
) {
3135 /* Copy the passed variables, which should include the
3136 terminating double-null */
3137 memcpy(bus
->vars
, arg
, bus
->varsz
);
3142 static int brcmf_sdbrcm_write_vars(struct brcmf_bus
*bus
)
3151 char *nvram_ularray
;
3154 /* Even if there are no vars are to be written, we still
3155 need to set the ramsize. */
3156 varsize
= bus
->varsz
? roundup(bus
->varsz
, 4) : 0;
3157 varaddr
= (bus
->ramsize
- 4) - varsize
;
3160 vbuffer
= kzalloc(varsize
, GFP_ATOMIC
);
3164 memcpy(vbuffer
, bus
->vars
, bus
->varsz
);
3166 /* Write the vars list */
3168 brcmf_sdbrcm_membytes(bus
, true, varaddr
, vbuffer
, varsize
);
3170 /* Verify NVRAM bytes */
3171 brcmf_dbg(INFO
, "Compare NVRAM dl & ul; varsize=%d\n", varsize
);
3172 nvram_ularray
= kmalloc(varsize
, GFP_ATOMIC
);
3176 /* Upload image to verify downloaded contents. */
3177 memset(nvram_ularray
, 0xaa, varsize
);
3179 /* Read the vars list to temp buffer for comparison */
3181 brcmf_sdbrcm_membytes(bus
, false, varaddr
, nvram_ularray
,
3184 brcmf_dbg(ERROR
, "error %d on reading %d nvram bytes at 0x%08x\n",
3185 bcmerror
, varsize
, varaddr
);
3187 /* Compare the org NVRAM with the one read from RAM */
3188 if (memcmp(vbuffer
, nvram_ularray
, varsize
))
3189 brcmf_dbg(ERROR
, "Downloaded NVRAM image is corrupted\n");
3191 brcmf_dbg(ERROR
, "Download/Upload/Compare of NVRAM ok\n");
3193 kfree(nvram_ularray
);
3199 /* adjust to the user specified RAM */
3200 brcmf_dbg(INFO
, "Physical memory size: %d\n", bus
->ramsize
);
3201 brcmf_dbg(INFO
, "Vars are at %d, orig varsize is %d\n",
3203 varsize
= ((bus
->ramsize
- 4) - varaddr
);
3206 * Determine the length token:
3207 * Varsize, converted to words, in lower 16-bits, checksum
3212 varsizew_le
= cpu_to_le32(0);
3214 varsizew
= varsize
/ 4;
3215 varsizew
= (~varsizew
<< 16) | (varsizew
& 0x0000FFFF);
3216 varsizew_le
= cpu_to_le32(varsizew
);
3219 brcmf_dbg(INFO
, "New varsize is %d, length token=0x%08x\n",
3222 /* Write the length token to the last word */
3223 bcmerror
= brcmf_sdbrcm_membytes(bus
, true, (bus
->ramsize
- 4),
3224 (u8
*)&varsizew_le
, 4);
3230 brcmf_sdbrcm_chip_disablecore(struct brcmf_sdio_dev
*sdiodev
, u32 corebase
)
3234 regdata
= brcmf_sdcard_reg_read(sdiodev
,
3235 CORE_SB(corebase
, sbtmstatelow
), 4);
3236 if (regdata
& SBTML_RESET
)
3239 regdata
= brcmf_sdcard_reg_read(sdiodev
,
3240 CORE_SB(corebase
, sbtmstatelow
), 4);
3241 if ((regdata
& (SICF_CLOCK_EN
<< SBTML_SICF_SHIFT
)) != 0) {
3243 * set target reject and spin until busy is clear
3244 * (preserve core-specific bits)
3246 regdata
= brcmf_sdcard_reg_read(sdiodev
,
3247 CORE_SB(corebase
, sbtmstatelow
), 4);
3248 brcmf_sdcard_reg_write(sdiodev
, CORE_SB(corebase
, sbtmstatelow
),
3249 4, regdata
| SBTML_REJ
);
3251 regdata
= brcmf_sdcard_reg_read(sdiodev
,
3252 CORE_SB(corebase
, sbtmstatelow
), 4);
3254 SPINWAIT((brcmf_sdcard_reg_read(sdiodev
,
3255 CORE_SB(corebase
, sbtmstatehigh
), 4) &
3256 SBTMH_BUSY
), 100000);
3258 regdata
= brcmf_sdcard_reg_read(sdiodev
,
3259 CORE_SB(corebase
, sbtmstatehigh
), 4);
3260 if (regdata
& SBTMH_BUSY
)
3261 brcmf_dbg(ERROR
, "ARM core still busy\n");
3263 regdata
= brcmf_sdcard_reg_read(sdiodev
,
3264 CORE_SB(corebase
, sbidlow
), 4);
3265 if (regdata
& SBIDL_INIT
) {
3266 regdata
= brcmf_sdcard_reg_read(sdiodev
,
3267 CORE_SB(corebase
, sbimstate
), 4) |
3269 brcmf_sdcard_reg_write(sdiodev
,
3270 CORE_SB(corebase
, sbimstate
), 4,
3272 regdata
= brcmf_sdcard_reg_read(sdiodev
,
3273 CORE_SB(corebase
, sbimstate
), 4);
3275 SPINWAIT((brcmf_sdcard_reg_read(sdiodev
,
3276 CORE_SB(corebase
, sbimstate
), 4) &
3280 /* set reset and reject while enabling the clocks */
3281 brcmf_sdcard_reg_write(sdiodev
,
3282 CORE_SB(corebase
, sbtmstatelow
), 4,
3283 (((SICF_FGC
| SICF_CLOCK_EN
) << SBTML_SICF_SHIFT
) |
3284 SBTML_REJ
| SBTML_RESET
));
3285 regdata
= brcmf_sdcard_reg_read(sdiodev
,
3286 CORE_SB(corebase
, sbtmstatelow
), 4);
3289 /* clear the initiator reject bit */
3290 regdata
= brcmf_sdcard_reg_read(sdiodev
,
3291 CORE_SB(corebase
, sbidlow
), 4);
3292 if (regdata
& SBIDL_INIT
) {
3293 regdata
= brcmf_sdcard_reg_read(sdiodev
,
3294 CORE_SB(corebase
, sbimstate
), 4) &
3296 brcmf_sdcard_reg_write(sdiodev
,
3297 CORE_SB(corebase
, sbimstate
), 4,
3302 /* leave reset and reject asserted */
3303 brcmf_sdcard_reg_write(sdiodev
, CORE_SB(corebase
, sbtmstatelow
), 4,
3304 (SBTML_REJ
| SBTML_RESET
));
3309 brcmf_sdbrcm_chip_resetcore(struct brcmf_sdio_dev
*sdiodev
, u32 corebase
)
3314 * Must do the disable sequence first to work for
3315 * arbitrary current core state.
3317 brcmf_sdbrcm_chip_disablecore(sdiodev
, corebase
);
3320 * Now do the initialization sequence.
3321 * set reset while enabling the clock and
3322 * forcing them on throughout the core
3324 brcmf_sdcard_reg_write(sdiodev
, CORE_SB(corebase
, sbtmstatelow
), 4,
3325 ((SICF_FGC
| SICF_CLOCK_EN
) << SBTML_SICF_SHIFT
) |
3329 regdata
= brcmf_sdcard_reg_read(sdiodev
,
3330 CORE_SB(corebase
, sbtmstatehigh
), 4);
3331 if (regdata
& SBTMH_SERR
)
3332 brcmf_sdcard_reg_write(sdiodev
,
3333 CORE_SB(corebase
, sbtmstatehigh
), 4, 0);
3335 regdata
= brcmf_sdcard_reg_read(sdiodev
,
3336 CORE_SB(corebase
, sbimstate
), 4);
3337 if (regdata
& (SBIM_IBE
| SBIM_TO
))
3338 brcmf_sdcard_reg_write(sdiodev
, CORE_SB(corebase
, sbimstate
), 4,
3339 regdata
& ~(SBIM_IBE
| SBIM_TO
));
3341 /* clear reset and allow it to propagate throughout the core */
3342 brcmf_sdcard_reg_write(sdiodev
, CORE_SB(corebase
, sbtmstatelow
), 4,
3343 (SICF_FGC
<< SBTML_SICF_SHIFT
) |
3344 (SICF_CLOCK_EN
<< SBTML_SICF_SHIFT
));
3347 /* leave clock enabled */
3348 brcmf_sdcard_reg_write(sdiodev
, CORE_SB(corebase
, sbtmstatelow
), 4,
3349 (SICF_CLOCK_EN
<< SBTML_SICF_SHIFT
));
3353 static int brcmf_sdbrcm_download_state(struct brcmf_bus
*bus
, bool enter
)
3359 /* To enter download state, disable ARM and reset SOCRAM.
3360 * To exit download state, simply reset ARM (default is RAM boot).
3363 bus
->alp_only
= true;
3365 brcmf_sdbrcm_chip_disablecore(bus
->sdiodev
,
3366 bus
->ci
->armcorebase
);
3368 brcmf_sdbrcm_chip_resetcore(bus
->sdiodev
, bus
->ci
->ramcorebase
);
3370 /* Clear the top bit of memory */
3373 brcmf_sdbrcm_membytes(bus
, true, bus
->ramsize
- 4,
3377 regdata
= brcmf_sdcard_reg_read(bus
->sdiodev
,
3378 CORE_SB(bus
->ci
->ramcorebase
, sbtmstatelow
), 4);
3379 regdata
&= (SBTML_RESET
| SBTML_REJ_MASK
|
3380 (SICF_CLOCK_EN
<< SBTML_SICF_SHIFT
));
3381 if ((SICF_CLOCK_EN
<< SBTML_SICF_SHIFT
) != regdata
) {
3382 brcmf_dbg(ERROR
, "SOCRAM core is down after reset?\n");
3387 bcmerror
= brcmf_sdbrcm_write_vars(bus
);
3389 brcmf_dbg(ERROR
, "no vars written to RAM\n");
3393 w_sdreg32(bus
, 0xFFFFFFFF,
3394 offsetof(struct sdpcmd_regs
, intstatus
), &retries
);
3396 brcmf_sdbrcm_chip_resetcore(bus
->sdiodev
, bus
->ci
->armcorebase
);
3398 /* Allow HT Clock now that the ARM is running. */
3399 bus
->alp_only
= false;
3401 bus
->drvr
->busstate
= BRCMF_BUS_LOAD
;
3407 static int brcmf_sdbrcm_get_image(char *buf
, int len
, struct brcmf_bus
*bus
)
3409 if (bus
->firmware
->size
< bus
->fw_ptr
+ len
)
3410 len
= bus
->firmware
->size
- bus
->fw_ptr
;
3412 memcpy(buf
, &bus
->firmware
->data
[bus
->fw_ptr
], len
);
3417 MODULE_FIRMWARE(BCM4329_FW_NAME
);
3418 MODULE_FIRMWARE(BCM4329_NV_NAME
);
3420 static int brcmf_sdbrcm_download_code_file(struct brcmf_bus
*bus
)
3424 u8
*memblock
= NULL
, *memptr
;
3427 brcmf_dbg(INFO
, "Enter\n");
3429 bus
->fw_name
= BCM4329_FW_NAME
;
3430 ret
= request_firmware(&bus
->firmware
, bus
->fw_name
,
3431 &bus
->sdiodev
->func
[2]->dev
);
3433 brcmf_dbg(ERROR
, "Fail to request firmware %d\n", ret
);
3438 memptr
= memblock
= kmalloc(MEMBLOCK
+ BRCMF_SDALIGN
, GFP_ATOMIC
);
3439 if (memblock
== NULL
) {
3443 if ((u32
)(unsigned long)memblock
% BRCMF_SDALIGN
)
3444 memptr
+= (BRCMF_SDALIGN
-
3445 ((u32
)(unsigned long)memblock
% BRCMF_SDALIGN
));
3447 /* Download image */
3449 brcmf_sdbrcm_get_image((char *)memptr
, MEMBLOCK
, bus
))) {
3450 ret
= brcmf_sdbrcm_membytes(bus
, true, offset
, memptr
, len
);
3452 brcmf_dbg(ERROR
, "error %d on writing %d membytes at 0x%08x\n",
3453 ret
, MEMBLOCK
, offset
);
3463 release_firmware(bus
->firmware
);
3470 * ProcessVars:Takes a buffer of "<var>=<value>\n" lines read from a file
3471 * and ending in a NUL.
3472 * Removes carriage returns, empty lines, comment lines, and converts
3474 * Shortens buffer as needed and pads with NULs. End of buffer is marked
3478 static uint
brcmf_process_nvram_vars(char *varbuf
, uint len
)
3487 findNewline
= false;
3490 for (n
= 0; n
< len
; n
++) {
3493 if (varbuf
[n
] == '\r')
3495 if (findNewline
&& varbuf
[n
] != '\n')
3497 findNewline
= false;
3498 if (varbuf
[n
] == '#') {
3502 if (varbuf
[n
] == '\n') {
3512 buf_len
= dp
- varbuf
;
3514 while (dp
< varbuf
+ n
)
3520 static int brcmf_sdbrcm_download_nvram(struct brcmf_bus
*bus
)
3523 char *memblock
= NULL
;
3527 bus
->nv_name
= BCM4329_NV_NAME
;
3528 ret
= request_firmware(&bus
->firmware
, bus
->nv_name
,
3529 &bus
->sdiodev
->func
[2]->dev
);
3531 brcmf_dbg(ERROR
, "Fail to request nvram %d\n", ret
);
3536 memblock
= kmalloc(MEMBLOCK
, GFP_ATOMIC
);
3537 if (memblock
== NULL
) {
3542 len
= brcmf_sdbrcm_get_image(memblock
, MEMBLOCK
, bus
);
3544 if (len
> 0 && len
< MEMBLOCK
) {
3545 bufp
= (char *)memblock
;
3547 len
= brcmf_process_nvram_vars(bufp
, len
);
3551 ret
= brcmf_sdbrcm_downloadvars(bus
, memblock
, len
+ 1);
3553 brcmf_dbg(ERROR
, "error downloading vars: %d\n", ret
);
3555 brcmf_dbg(ERROR
, "error reading nvram file: %d\n", len
);
3562 release_firmware(bus
->firmware
);
3568 static int _brcmf_sdbrcm_download_firmware(struct brcmf_bus
*bus
)
3572 /* Keep arm in reset */
3573 if (brcmf_sdbrcm_download_state(bus
, true)) {
3574 brcmf_dbg(ERROR
, "error placing ARM core in reset\n");
3578 /* External image takes precedence if specified */
3579 if (brcmf_sdbrcm_download_code_file(bus
)) {
3580 brcmf_dbg(ERROR
, "dongle image file download failed\n");
3584 /* External nvram takes precedence if specified */
3585 if (brcmf_sdbrcm_download_nvram(bus
))
3586 brcmf_dbg(ERROR
, "dongle nvram file download failed\n");
3588 /* Take arm out of reset */
3589 if (brcmf_sdbrcm_download_state(bus
, false)) {
3590 brcmf_dbg(ERROR
, "error getting out of ARM core reset\n");
3601 brcmf_sdbrcm_download_firmware(struct brcmf_bus
*bus
)
3605 /* Download the firmware */
3606 brcmf_sdbrcm_clkctl(bus
, CLK_AVAIL
, false);
3608 ret
= _brcmf_sdbrcm_download_firmware(bus
) == 0;
3610 brcmf_sdbrcm_clkctl(bus
, CLK_SDONLY
, false);
3615 void brcmf_sdbrcm_bus_stop(struct brcmf_bus
*bus
)
3617 u32 local_hostintmask
;
3621 struct sk_buff
*cur
;
3622 struct sk_buff
*next
;
3624 brcmf_dbg(TRACE
, "Enter\n");
3626 if (bus
->watchdog_tsk
) {
3627 send_sig(SIGTERM
, bus
->watchdog_tsk
, 1);
3628 kthread_stop(bus
->watchdog_tsk
);
3629 bus
->watchdog_tsk
= NULL
;
3632 if (bus
->dpc_tsk
&& bus
->dpc_tsk
!= current
) {
3633 send_sig(SIGTERM
, bus
->dpc_tsk
, 1);
3634 kthread_stop(bus
->dpc_tsk
);
3635 bus
->dpc_tsk
= NULL
;
3642 /* Enable clock for device interrupts */
3643 brcmf_sdbrcm_clkctl(bus
, CLK_AVAIL
, false);
3645 /* Disable and clear interrupts at the chip level also */
3646 w_sdreg32(bus
, 0, offsetof(struct sdpcmd_regs
, hostintmask
), &retries
);
3647 local_hostintmask
= bus
->hostintmask
;
3648 bus
->hostintmask
= 0;
3650 /* Change our idea of bus state */
3651 bus
->drvr
->busstate
= BRCMF_BUS_DOWN
;
3653 /* Force clocks on backplane to be sure F2 interrupt propagates */
3654 saveclk
= brcmf_sdcard_cfg_read(bus
->sdiodev
, SDIO_FUNC_1
,
3655 SBSDIO_FUNC1_CHIPCLKCSR
, &err
);
3657 brcmf_sdcard_cfg_write(bus
->sdiodev
, SDIO_FUNC_1
,
3658 SBSDIO_FUNC1_CHIPCLKCSR
,
3659 (saveclk
| SBSDIO_FORCE_HT
), &err
);
3662 brcmf_dbg(ERROR
, "Failed to force clock for F2: err %d\n", err
);
3664 /* Turn off the bus (F2), free any pending packets */
3665 brcmf_dbg(INTR
, "disable SDIO interrupts\n");
3666 brcmf_sdcard_cfg_write(bus
->sdiodev
, SDIO_FUNC_0
, SDIO_CCCR_IOEx
,
3667 SDIO_FUNC_ENABLE_1
, NULL
);
3669 /* Clear any pending interrupts now that F2 is disabled */
3670 w_sdreg32(bus
, local_hostintmask
,
3671 offsetof(struct sdpcmd_regs
, intstatus
), &retries
);
3673 /* Turn off the backplane clock (only) */
3674 brcmf_sdbrcm_clkctl(bus
, CLK_SDONLY
, false);
3676 /* Clear the data packet queues */
3677 brcmu_pktq_flush(&bus
->txq
, true, NULL
, NULL
);
3679 /* Clear any held glomming stuff */
3681 brcmu_pkt_buf_free_skb(bus
->glomd
);
3682 if (!skb_queue_empty(&bus
->glom
))
3683 skb_queue_walk_safe(&bus
->glom
, cur
, next
) {
3684 skb_unlink(cur
, &bus
->glom
);
3685 brcmu_pkt_buf_free_skb(cur
);
3688 /* Clear rx control and wake any waiters */
3690 brcmf_sdbrcm_dcmd_resp_wake(bus
);
3692 /* Reset some F2 state stuff */
3693 bus
->rxskip
= false;
3694 bus
->tx_seq
= bus
->rx_seq
= 0;
3699 int brcmf_sdbrcm_bus_init(struct brcmf_pub
*drvr
)
3701 struct brcmf_bus
*bus
= drvr
->bus
;
3702 unsigned long timeout
;
3708 brcmf_dbg(TRACE
, "Enter\n");
3710 /* try to download image and nvram to the dongle */
3711 if (drvr
->busstate
== BRCMF_BUS_DOWN
) {
3712 if (!(brcmf_sdbrcm_download_firmware(bus
)))
3719 /* Start the watchdog timer */
3720 bus
->drvr
->tickcnt
= 0;
3721 brcmf_sdbrcm_wd_timer(bus
, BRCMF_WD_POLL_MS
);
3725 /* Make sure backplane clock is on, needed to generate F2 interrupt */
3726 brcmf_sdbrcm_clkctl(bus
, CLK_AVAIL
, false);
3727 if (bus
->clkstate
!= CLK_AVAIL
)
3730 /* Force clocks on backplane to be sure F2 interrupt propagates */
3732 brcmf_sdcard_cfg_read(bus
->sdiodev
, SDIO_FUNC_1
,
3733 SBSDIO_FUNC1_CHIPCLKCSR
, &err
);
3735 brcmf_sdcard_cfg_write(bus
->sdiodev
, SDIO_FUNC_1
,
3736 SBSDIO_FUNC1_CHIPCLKCSR
,
3737 (saveclk
| SBSDIO_FORCE_HT
), &err
);
3740 brcmf_dbg(ERROR
, "Failed to force clock for F2: err %d\n", err
);
3744 /* Enable function 2 (frame transfers) */
3745 w_sdreg32(bus
, SDPCM_PROT_VERSION
<< SMB_DATA_VERSION_SHIFT
,
3746 offsetof(struct sdpcmd_regs
, tosbmailboxdata
), &retries
);
3747 enable
= (SDIO_FUNC_ENABLE_1
| SDIO_FUNC_ENABLE_2
);
3749 brcmf_sdcard_cfg_write(bus
->sdiodev
, SDIO_FUNC_0
, SDIO_CCCR_IOEx
,
3752 timeout
= jiffies
+ msecs_to_jiffies(BRCMF_WAIT_F2RDY
);
3754 while (enable
!= ready
) {
3755 ready
= brcmf_sdcard_cfg_read(bus
->sdiodev
, SDIO_FUNC_0
,
3756 SDIO_CCCR_IORx
, NULL
);
3757 if (time_after(jiffies
, timeout
))
3759 else if (time_after(jiffies
, timeout
- BRCMF_WAIT_F2RDY
+ 50))
3760 /* prevent busy waiting if it takes too long */
3761 msleep_interruptible(20);
3764 brcmf_dbg(INFO
, "enable 0x%02x, ready 0x%02x\n", enable
, ready
);
3766 /* If F2 successfully enabled, set core and enable interrupts */
3767 if (ready
== enable
) {
3768 /* Set up the interrupt mask and enable interrupts */
3769 bus
->hostintmask
= HOSTINTMASK
;
3770 w_sdreg32(bus
, bus
->hostintmask
,
3771 offsetof(struct sdpcmd_regs
, hostintmask
), &retries
);
3773 brcmf_sdcard_cfg_write(bus
->sdiodev
, SDIO_FUNC_1
,
3774 SBSDIO_WATERMARK
, 8, &err
);
3776 /* Set bus state according to enable result */
3777 drvr
->busstate
= BRCMF_BUS_DATA
;
3781 /* Disable F2 again */
3782 enable
= SDIO_FUNC_ENABLE_1
;
3783 brcmf_sdcard_cfg_write(bus
->sdiodev
, SDIO_FUNC_0
,
3784 SDIO_CCCR_IOEx
, enable
, NULL
);
3787 /* Restore previous clock setting */
3788 brcmf_sdcard_cfg_write(bus
->sdiodev
, SDIO_FUNC_1
,
3789 SBSDIO_FUNC1_CHIPCLKCSR
, saveclk
, &err
);
3791 /* If we didn't come up, turn off backplane clock */
3792 if (drvr
->busstate
!= BRCMF_BUS_DATA
)
3793 brcmf_sdbrcm_clkctl(bus
, CLK_NONE
, false);
3801 void brcmf_sdbrcm_isr(void *arg
)
3803 struct brcmf_bus
*bus
= (struct brcmf_bus
*) arg
;
3805 brcmf_dbg(TRACE
, "Enter\n");
3808 brcmf_dbg(ERROR
, "bus is null pointer, exiting\n");
3812 if (bus
->drvr
->busstate
== BRCMF_BUS_DOWN
) {
3813 brcmf_dbg(ERROR
, "bus is down. we have nothing to do\n");
3816 /* Count the interrupt call */
3820 /* Shouldn't get this interrupt if we're sleeping? */
3821 if (bus
->sleeping
) {
3822 brcmf_dbg(ERROR
, "INTERRUPT WHILE SLEEPING??\n");
3826 /* Disable additional interrupts (is this needed now)? */
3828 brcmf_dbg(ERROR
, "isr w/o interrupt configured!\n");
3830 bus
->dpc_sched
= true;
3832 complete(&bus
->dpc_wait
);
3835 static bool brcmf_sdbrcm_bus_watchdog(struct brcmf_pub
*drvr
)
3837 struct brcmf_bus
*bus
;
3839 brcmf_dbg(TIMER
, "Enter\n");
3843 /* Ignore the timer if simulating bus down */
3849 /* Poll period: check device if appropriate. */
3850 if (bus
->poll
&& (++bus
->polltick
>= bus
->pollrate
)) {
3853 /* Reset poll tick */
3856 /* Check device if no interrupts */
3857 if (!bus
->intr
|| (bus
->intrcount
== bus
->lastintrs
)) {
3859 if (!bus
->dpc_sched
) {
3861 devpend
= brcmf_sdcard_cfg_read(bus
->sdiodev
,
3862 SDIO_FUNC_0
, SDIO_CCCR_INTx
,
3865 devpend
& (INTR_STATUS_FUNC1
|
3869 /* If there is something, make like the ISR and
3875 bus
->dpc_sched
= true;
3877 complete(&bus
->dpc_wait
);
3881 /* Update interrupt tracking */
3882 bus
->lastintrs
= bus
->intrcount
;
3885 /* Poll for console output periodically */
3886 if (drvr
->busstate
== BRCMF_BUS_DATA
&& bus
->console_interval
!= 0) {
3887 bus
->console
.count
+= BRCMF_WD_POLL_MS
;
3888 if (bus
->console
.count
>= bus
->console_interval
) {
3889 bus
->console
.count
-= bus
->console_interval
;
3890 /* Make sure backplane clock is on */
3891 brcmf_sdbrcm_clkctl(bus
, CLK_AVAIL
, false);
3892 if (brcmf_sdbrcm_readconsole(bus
) < 0)
3894 bus
->console_interval
= 0;
3899 /* On idle timeout clear activity flag and/or turn off clock */
3900 if ((bus
->idletime
> 0) && (bus
->clkstate
== CLK_AVAIL
)) {
3901 if (++bus
->idlecount
>= bus
->idletime
) {
3903 if (bus
->activity
) {
3904 bus
->activity
= false;
3905 brcmf_sdbrcm_wd_timer(bus
, BRCMF_WD_POLL_MS
);
3907 brcmf_sdbrcm_clkctl(bus
, CLK_NONE
, false);
3917 static bool brcmf_sdbrcm_chipmatch(u16 chipid
)
3919 if (chipid
== BCM4329_CHIP_ID
)
3924 static void brcmf_sdbrcm_release_malloc(struct brcmf_bus
*bus
)
3926 brcmf_dbg(TRACE
, "Enter\n");
3929 bus
->rxctl
= bus
->rxbuf
= NULL
;
3932 kfree(bus
->databuf
);
3933 bus
->databuf
= NULL
;
3936 static bool brcmf_sdbrcm_probe_malloc(struct brcmf_bus
*bus
)
3938 brcmf_dbg(TRACE
, "Enter\n");
3940 if (bus
->drvr
->maxctl
) {
3942 roundup((bus
->drvr
->maxctl
+ SDPCM_HDRLEN
),
3943 ALIGNMENT
) + BRCMF_SDALIGN
;
3944 bus
->rxbuf
= kmalloc(bus
->rxblen
, GFP_ATOMIC
);
3949 /* Allocate buffer to receive glomed packet */
3950 bus
->databuf
= kmalloc(MAX_DATA_BUF
, GFP_ATOMIC
);
3951 if (!(bus
->databuf
)) {
3952 /* release rxbuf which was already located as above */
3958 /* Align the buffer */
3959 if ((unsigned long)bus
->databuf
% BRCMF_SDALIGN
)
3960 bus
->dataptr
= bus
->databuf
+ (BRCMF_SDALIGN
-
3961 ((unsigned long)bus
->databuf
% BRCMF_SDALIGN
));
3963 bus
->dataptr
= bus
->databuf
;
3971 /* SDIO Pad drive strength to select value mappings */
3972 struct sdiod_drive_str
{
3973 u8 strength
; /* Pad Drive Strength in mA */
3974 u8 sel
; /* Chip-specific select value */
3977 /* SDIO Drive Strength to sel value table for PMU Rev 1 */
3978 static const struct sdiod_drive_str sdiod_drive_strength_tab1
[] = {
3986 /* SDIO Drive Strength to sel value table for PMU Rev 2, 3 */
3987 static const struct sdiod_drive_str sdiod_drive_strength_tab2
[] = {
3998 /* SDIO Drive Strength to sel value table for PMU Rev 8 (1.8V) */
3999 static const struct sdiod_drive_str sdiod_drive_strength_tab3
[] = {
4011 #define SDIOD_DRVSTR_KEY(chip, pmu) (((chip) << 16) | (pmu))
4013 static char *brcmf_chipname(uint chipid
, char *buf
, uint len
)
4017 fmt
= ((chipid
> 0xa000) || (chipid
< 0x4000)) ? "%d" : "%x";
4018 snprintf(buf
, len
, fmt
, chipid
);
4022 static void brcmf_sdbrcm_sdiod_drive_strength_init(struct brcmf_bus
*bus
,
4023 u32 drivestrength
) {
4024 struct sdiod_drive_str
*str_tab
= NULL
;
4029 if (!(bus
->ci
->cccaps
& CC_CAP_PMU
))
4032 switch (SDIOD_DRVSTR_KEY(bus
->ci
->chip
, bus
->ci
->pmurev
)) {
4033 case SDIOD_DRVSTR_KEY(BCM4325_CHIP_ID
, 1):
4034 str_tab
= (struct sdiod_drive_str
*)&sdiod_drive_strength_tab1
;
4035 str_mask
= 0x30000000;
4038 case SDIOD_DRVSTR_KEY(BCM4325_CHIP_ID
, 2):
4039 case SDIOD_DRVSTR_KEY(BCM4325_CHIP_ID
, 3):
4040 str_tab
= (struct sdiod_drive_str
*)&sdiod_drive_strength_tab2
;
4041 str_mask
= 0x00003800;
4044 case SDIOD_DRVSTR_KEY(BCM4336_CHIP_ID
, 8):
4045 str_tab
= (struct sdiod_drive_str
*)&sdiod_drive_strength_tab3
;
4046 str_mask
= 0x00003800;
4050 brcmf_dbg(ERROR
, "No SDIO Drive strength init done for chip %s rev %d pmurev %d\n",
4051 brcmf_chipname(bus
->ci
->chip
, chn
, 8),
4052 bus
->ci
->chiprev
, bus
->ci
->pmurev
);
4056 if (str_tab
!= NULL
) {
4057 u32 drivestrength_sel
= 0;
4061 for (i
= 0; str_tab
[i
].strength
!= 0; i
++) {
4062 if (drivestrength
>= str_tab
[i
].strength
) {
4063 drivestrength_sel
= str_tab
[i
].sel
;
4068 brcmf_sdcard_reg_write(bus
->sdiodev
,
4069 CORE_CC_REG(bus
->ci
->cccorebase
, chipcontrol_addr
),
4071 cc_data_temp
= brcmf_sdcard_reg_read(bus
->sdiodev
,
4072 CORE_CC_REG(bus
->ci
->cccorebase
, chipcontrol_addr
), 4);
4073 cc_data_temp
&= ~str_mask
;
4074 drivestrength_sel
<<= str_shift
;
4075 cc_data_temp
|= drivestrength_sel
;
4076 brcmf_sdcard_reg_write(bus
->sdiodev
,
4077 CORE_CC_REG(bus
->ci
->cccorebase
, chipcontrol_addr
),
4080 brcmf_dbg(INFO
, "SDIO: %dmA drive strength selected, set to 0x%08x\n",
4081 drivestrength
, cc_data_temp
);
4086 brcmf_sdbrcm_chip_recognition(struct brcmf_sdio_dev
*sdiodev
,
4087 struct chip_info
*ci
, u32 regs
)
4093 * Chipid is assume to be at offset 0 from regs arg
4094 * For different chiptypes or old sdio hosts w/o chipcommon,
4095 * other ways of recognition should be added here.
4097 ci
->cccorebase
= regs
;
4098 regdata
= brcmf_sdcard_reg_read(sdiodev
,
4099 CORE_CC_REG(ci
->cccorebase
, chipid
), 4);
4100 ci
->chip
= regdata
& CID_ID_MASK
;
4101 ci
->chiprev
= (regdata
& CID_REV_MASK
) >> CID_REV_SHIFT
;
4103 brcmf_dbg(INFO
, "chipid=0x%x chiprev=%d\n", ci
->chip
, ci
->chiprev
);
4105 /* Address of cores for new chips should be added here */
4107 case BCM4329_CHIP_ID
:
4108 ci
->buscorebase
= BCM4329_CORE_BUS_BASE
;
4109 ci
->ramcorebase
= BCM4329_CORE_SOCRAM_BASE
;
4110 ci
->armcorebase
= BCM4329_CORE_ARM_BASE
;
4111 ci
->ramsize
= BCM4329_RAMSIZE
;
4114 brcmf_dbg(ERROR
, "chipid 0x%x is not supported\n", ci
->chip
);
4118 regdata
= brcmf_sdcard_reg_read(sdiodev
,
4119 CORE_SB(ci
->cccorebase
, sbidhigh
), 4);
4120 ci
->ccrev
= SBCOREREV(regdata
);
4122 regdata
= brcmf_sdcard_reg_read(sdiodev
,
4123 CORE_CC_REG(ci
->cccorebase
, pmucapabilities
), 4);
4124 ci
->pmurev
= regdata
& PCAP_REV_MASK
;
4126 regdata
= brcmf_sdcard_reg_read(sdiodev
,
4127 CORE_SB(ci
->buscorebase
, sbidhigh
), 4);
4128 ci
->buscorerev
= SBCOREREV(regdata
);
4129 ci
->buscoretype
= (regdata
& SBIDH_CC_MASK
) >> SBIDH_CC_SHIFT
;
4131 brcmf_dbg(INFO
, "ccrev=%d, pmurev=%d, buscore rev/type=%d/0x%x\n",
4132 ci
->ccrev
, ci
->pmurev
, ci
->buscorerev
, ci
->buscoretype
);
4134 /* get chipcommon capabilites */
4135 ci
->cccaps
= brcmf_sdcard_reg_read(sdiodev
,
4136 CORE_CC_REG(ci
->cccorebase
, capabilities
), 4);
4142 brcmf_sdbrcm_chip_attach(struct brcmf_bus
*bus
, u32 regs
)
4144 struct chip_info
*ci
;
4148 brcmf_dbg(TRACE
, "Enter\n");
4150 /* alloc chip_info_t */
4151 ci
= kzalloc(sizeof(struct chip_info
), GFP_ATOMIC
);
4155 /* bus/core/clk setup for register access */
4156 /* Try forcing SDIO core to do ALPAvail request only */
4157 clkset
= SBSDIO_FORCE_HW_CLKREQ_OFF
| SBSDIO_ALP_AVAIL_REQ
;
4158 brcmf_sdcard_cfg_write(bus
->sdiodev
, SDIO_FUNC_1
,
4159 SBSDIO_FUNC1_CHIPCLKCSR
, clkset
, &err
);
4161 brcmf_dbg(ERROR
, "error writing for HT off\n");
4165 /* If register supported, wait for ALPAvail and then force ALP */
4166 /* This may take up to 15 milliseconds */
4167 clkval
= brcmf_sdcard_cfg_read(bus
->sdiodev
, SDIO_FUNC_1
,
4168 SBSDIO_FUNC1_CHIPCLKCSR
, NULL
);
4169 if ((clkval
& ~SBSDIO_AVBITS
) == clkset
) {
4171 brcmf_sdcard_cfg_read(bus
->sdiodev
, SDIO_FUNC_1
,
4172 SBSDIO_FUNC1_CHIPCLKCSR
,
4174 !SBSDIO_ALPAV(clkval
)),
4175 PMU_MAX_TRANSITION_DLY
);
4176 if (!SBSDIO_ALPAV(clkval
)) {
4177 brcmf_dbg(ERROR
, "timeout on ALPAV wait, clkval 0x%02x\n",
4182 clkset
= SBSDIO_FORCE_HW_CLKREQ_OFF
|
4184 brcmf_sdcard_cfg_write(bus
->sdiodev
, SDIO_FUNC_1
,
4185 SBSDIO_FUNC1_CHIPCLKCSR
,
4189 brcmf_dbg(ERROR
, "ChipClkCSR access: wrote 0x%02x read 0x%02x\n",
4195 /* Also, disable the extra SDIO pull-ups */
4196 brcmf_sdcard_cfg_write(bus
->sdiodev
, SDIO_FUNC_1
,
4197 SBSDIO_FUNC1_SDIOPULLUP
, 0, NULL
);
4199 err
= brcmf_sdbrcm_chip_recognition(bus
->sdiodev
, ci
, regs
);
4204 * Make sure any on-chip ARM is off (in case strapping is wrong),
4205 * or downloaded code was already running.
4207 brcmf_sdbrcm_chip_disablecore(bus
->sdiodev
, ci
->armcorebase
);
4209 brcmf_sdcard_reg_write(bus
->sdiodev
,
4210 CORE_CC_REG(ci
->cccorebase
, gpiopullup
), 4, 0);
4211 brcmf_sdcard_reg_write(bus
->sdiodev
,
4212 CORE_CC_REG(ci
->cccorebase
, gpiopulldown
), 4, 0);
4214 /* Disable F2 to clear any intermediate frame state on the dongle */
4215 brcmf_sdcard_cfg_write(bus
->sdiodev
, SDIO_FUNC_0
, SDIO_CCCR_IOEx
,
4216 SDIO_FUNC_ENABLE_1
, NULL
);
4218 /* WAR: cmd52 backplane read so core HW will drop ALPReq */
4219 clkval
= brcmf_sdcard_cfg_read(bus
->sdiodev
, SDIO_FUNC_1
,
4222 /* Done with backplane-dependent accesses, can drop clock... */
4223 brcmf_sdcard_cfg_write(bus
->sdiodev
, SDIO_FUNC_1
,
4224 SBSDIO_FUNC1_CHIPCLKCSR
, 0, NULL
);
4235 brcmf_sdbrcm_probe_attach(struct brcmf_bus
*bus
, u32 regsva
)
4242 bus
->alp_only
= true;
4244 /* Return the window to backplane enumeration space for core access */
4245 if (brcmf_sdcard_set_sbaddr_window(bus
->sdiodev
, SI_ENUM_BASE
))
4246 brcmf_dbg(ERROR
, "FAILED to return to SI_ENUM_BASE\n");
4249 printk(KERN_DEBUG
"F1 signature read @0x18000000=0x%4x\n",
4250 brcmf_sdcard_reg_read(bus
->sdiodev
, SI_ENUM_BASE
, 4));
4255 * Force PLL off until brcmf_sdbrcm_chip_attach()
4256 * programs PLL control regs
4259 brcmf_sdcard_cfg_write(bus
->sdiodev
, SDIO_FUNC_1
,
4260 SBSDIO_FUNC1_CHIPCLKCSR
,
4261 BRCMF_INIT_CLKCTL1
, &err
);
4264 brcmf_sdcard_cfg_read(bus
->sdiodev
, SDIO_FUNC_1
,
4265 SBSDIO_FUNC1_CHIPCLKCSR
, &err
);
4267 if (err
|| ((clkctl
& ~SBSDIO_AVBITS
) != BRCMF_INIT_CLKCTL1
)) {
4268 brcmf_dbg(ERROR
, "ChipClkCSR access: err %d wrote 0x%02x read 0x%02x\n",
4269 err
, BRCMF_INIT_CLKCTL1
, clkctl
);
4273 if (brcmf_sdbrcm_chip_attach(bus
, regsva
)) {
4274 brcmf_dbg(ERROR
, "brcmf_sdbrcm_chip_attach failed!\n");
4278 if (!brcmf_sdbrcm_chipmatch((u16
) bus
->ci
->chip
)) {
4279 brcmf_dbg(ERROR
, "unsupported chip: 0x%04x\n", bus
->ci
->chip
);
4283 brcmf_sdbrcm_sdiod_drive_strength_init(bus
, SDIO_DRIVE_STRENGTH
);
4285 /* Get info on the ARM and SOCRAM cores... */
4286 brcmf_sdcard_reg_read(bus
->sdiodev
,
4287 CORE_SB(bus
->ci
->armcorebase
, sbidhigh
), 4);
4288 bus
->ramsize
= bus
->ci
->ramsize
;
4289 if (!(bus
->ramsize
)) {
4290 brcmf_dbg(ERROR
, "failed to find SOCRAM memory!\n");
4294 /* Set core control so an SDIO reset does a backplane reset */
4295 reg_addr
= bus
->ci
->buscorebase
+
4296 offsetof(struct sdpcmd_regs
, corecontrol
);
4297 reg_val
= brcmf_sdcard_reg_read(bus
->sdiodev
, reg_addr
, sizeof(u32
));
4298 brcmf_sdcard_reg_write(bus
->sdiodev
, reg_addr
, sizeof(u32
),
4299 reg_val
| CC_BPRESEN
);
4301 brcmu_pktq_init(&bus
->txq
, (PRIOMASK
+ 1), TXQLEN
);
4303 /* Locate an appropriately-aligned portion of hdrbuf */
4304 bus
->rxhdr
= (u8
*) roundup((unsigned long)&bus
->hdrbuf
[0],
4307 /* Set the poll and/or interrupt flags */
4319 static bool brcmf_sdbrcm_probe_init(struct brcmf_bus
*bus
)
4321 brcmf_dbg(TRACE
, "Enter\n");
4323 /* Disable F2 to clear any intermediate frame state on the dongle */
4324 brcmf_sdcard_cfg_write(bus
->sdiodev
, SDIO_FUNC_0
, SDIO_CCCR_IOEx
,
4325 SDIO_FUNC_ENABLE_1
, NULL
);
4327 bus
->drvr
->busstate
= BRCMF_BUS_DOWN
;
4328 bus
->sleeping
= false;
4329 bus
->rxflow
= false;
4331 /* Done with backplane-dependent accesses, can drop clock... */
4332 brcmf_sdcard_cfg_write(bus
->sdiodev
, SDIO_FUNC_1
,
4333 SBSDIO_FUNC1_CHIPCLKCSR
, 0, NULL
);
4335 /* ...and initialize clock/power states */
4336 bus
->clkstate
= CLK_SDONLY
;
4337 bus
->idletime
= BRCMF_IDLE_INTERVAL
;
4338 bus
->idleclock
= BRCMF_IDLE_ACTIVE
;
4340 /* Query the F2 block size, set roundup accordingly */
4341 bus
->blocksize
= bus
->sdiodev
->func
[2]->cur_blksize
;
4342 bus
->roundup
= min(max_roundup
, bus
->blocksize
);
4344 /* bus module does not support packet chaining */
4345 bus
->use_rxchain
= false;
4346 bus
->sd_rxchain
= false;
4352 brcmf_sdbrcm_watchdog_thread(void *data
)
4354 struct brcmf_bus
*bus
= (struct brcmf_bus
*)data
;
4356 allow_signal(SIGTERM
);
4357 /* Run until signal received */
4359 if (kthread_should_stop())
4361 if (!wait_for_completion_interruptible(&bus
->watchdog_wait
)) {
4362 brcmf_sdbrcm_bus_watchdog(bus
->drvr
);
4363 /* Count the tick for reference */
4364 bus
->drvr
->tickcnt
++;
4372 brcmf_sdbrcm_watchdog(unsigned long data
)
4374 struct brcmf_bus
*bus
= (struct brcmf_bus
*)data
;
4376 if (bus
->watchdog_tsk
) {
4377 complete(&bus
->watchdog_wait
);
4378 /* Reschedule the watchdog */
4379 if (bus
->wd_timer_valid
)
4380 mod_timer(&bus
->timer
,
4381 jiffies
+ BRCMF_WD_POLL_MS
* HZ
/ 1000);
4386 brcmf_sdbrcm_chip_detach(struct brcmf_bus
*bus
)
4388 brcmf_dbg(TRACE
, "Enter\n");
4394 static void brcmf_sdbrcm_release_dongle(struct brcmf_bus
*bus
)
4396 brcmf_dbg(TRACE
, "Enter\n");
4399 brcmf_sdbrcm_clkctl(bus
, CLK_AVAIL
, false);
4400 brcmf_sdbrcm_clkctl(bus
, CLK_NONE
, false);
4401 brcmf_sdbrcm_chip_detach(bus
);
4402 if (bus
->vars
&& bus
->varsz
)
4407 brcmf_dbg(TRACE
, "Disconnected\n");
4410 /* Detach and free everything */
4411 static void brcmf_sdbrcm_release(struct brcmf_bus
*bus
)
4413 brcmf_dbg(TRACE
, "Enter\n");
4416 /* De-register interrupt handler */
4417 brcmf_sdcard_intr_dereg(bus
->sdiodev
);
4420 brcmf_detach(bus
->drvr
);
4421 brcmf_sdbrcm_release_dongle(bus
);
4425 brcmf_sdbrcm_release_malloc(bus
);
4430 brcmf_dbg(TRACE
, "Disconnected\n");
4433 void *brcmf_sdbrcm_probe(u16 bus_no
, u16 slot
, u16 func
, uint bustype
,
4434 u32 regsva
, struct brcmf_sdio_dev
*sdiodev
)
4437 struct brcmf_bus
*bus
;
4439 /* Init global variables at run-time, not as part of the declaration.
4440 * This is required to support init/de-init of the driver.
4442 * of globals as part of the declaration results in non-deterministic
4443 * behavior since the value of the globals may be different on the
4444 * first time that the driver is initialized vs subsequent
4449 brcmf_dbg(TRACE
, "Enter\n");
4451 /* We make an assumption about address window mappings:
4452 * regsva == SI_ENUM_BASE*/
4454 /* Allocate private bus interface state */
4455 bus
= kzalloc(sizeof(struct brcmf_bus
), GFP_ATOMIC
);
4459 bus
->sdiodev
= sdiodev
;
4461 skb_queue_head_init(&bus
->glom
);
4462 bus
->txbound
= BRCMF_TXBOUND
;
4463 bus
->rxbound
= BRCMF_RXBOUND
;
4464 bus
->txminmax
= BRCMF_TXMINMAX
;
4465 bus
->tx_seq
= SDPCM_SEQUENCE_WRAP
- 1;
4466 bus
->usebufpool
= false; /* Use bufpool if allocated,
4467 else use locally malloced rxbuf */
4469 /* attempt to attach to the dongle */
4470 if (!(brcmf_sdbrcm_probe_attach(bus
, regsva
))) {
4471 brcmf_dbg(ERROR
, "brcmf_sdbrcm_probe_attach failed\n");
4475 spin_lock_init(&bus
->txqlock
);
4476 init_waitqueue_head(&bus
->ctrl_wait
);
4477 init_waitqueue_head(&bus
->dcmd_resp_wait
);
4479 /* Set up the watchdog timer */
4480 init_timer(&bus
->timer
);
4481 bus
->timer
.data
= (unsigned long)bus
;
4482 bus
->timer
.function
= brcmf_sdbrcm_watchdog
;
4484 /* Initialize thread based operation and lock */
4485 sema_init(&bus
->sdsem
, 1);
4487 /* Initialize watchdog thread */
4488 init_completion(&bus
->watchdog_wait
);
4489 bus
->watchdog_tsk
= kthread_run(brcmf_sdbrcm_watchdog_thread
,
4490 bus
, "brcmf_watchdog");
4491 if (IS_ERR(bus
->watchdog_tsk
)) {
4493 "brcmf_watchdog thread failed to start\n");
4494 bus
->watchdog_tsk
= NULL
;
4496 /* Initialize DPC thread */
4497 init_completion(&bus
->dpc_wait
);
4498 bus
->dpc_tsk
= kthread_run(brcmf_sdbrcm_dpc_thread
,
4500 if (IS_ERR(bus
->dpc_tsk
)) {
4502 "brcmf_dpc thread failed to start\n");
4503 bus
->dpc_tsk
= NULL
;
4506 /* Attach to the brcmf/OS/network interface */
4507 bus
->drvr
= brcmf_attach(bus
, SDPCM_RESERVE
);
4509 brcmf_dbg(ERROR
, "brcmf_attach failed\n");
4513 /* Allocate buffers */
4514 if (!(brcmf_sdbrcm_probe_malloc(bus
))) {
4515 brcmf_dbg(ERROR
, "brcmf_sdbrcm_probe_malloc failed\n");
4519 if (!(brcmf_sdbrcm_probe_init(bus
))) {
4520 brcmf_dbg(ERROR
, "brcmf_sdbrcm_probe_init failed\n");
4524 /* Register interrupt callback, but mask it (not operational yet). */
4525 brcmf_dbg(INTR
, "disable SDIO interrupts (not interested yet)\n");
4526 ret
= brcmf_sdcard_intr_reg(bus
->sdiodev
);
4528 brcmf_dbg(ERROR
, "FAILED: sdcard_intr_reg returned %d\n", ret
);
4531 brcmf_dbg(INTR
, "registered SDIO interrupt function ok\n");
4533 brcmf_dbg(INFO
, "completed!!\n");
4535 /* if firmware path present try to download and bring up bus */
4536 ret
= brcmf_bus_start(bus
->drvr
);
4538 if (ret
== -ENOLINK
) {
4539 brcmf_dbg(ERROR
, "dongle is not responding\n");
4544 /* add interface and open for business */
4545 if (brcmf_add_if((struct brcmf_info
*)bus
->drvr
, 0, "wlan%d", NULL
)) {
4546 brcmf_dbg(ERROR
, "Add primary net device interface failed!!\n");
4553 brcmf_sdbrcm_release(bus
);
4557 void brcmf_sdbrcm_disconnect(void *ptr
)
4559 struct brcmf_bus
*bus
= (struct brcmf_bus
*)ptr
;
4561 brcmf_dbg(TRACE
, "Enter\n");
4564 brcmf_sdbrcm_release(bus
);
4566 brcmf_dbg(TRACE
, "Disconnected\n");
4569 struct device
*brcmf_bus_get_device(struct brcmf_bus
*bus
)
4571 return &bus
->sdiodev
->func
[2]->dev
;
4575 brcmf_sdbrcm_wd_timer(struct brcmf_bus
*bus
, uint wdtick
)
4577 /* Totally stop the timer */
4578 if (!wdtick
&& bus
->wd_timer_valid
== true) {
4579 del_timer_sync(&bus
->timer
);
4580 bus
->wd_timer_valid
= false;
4581 bus
->save_ms
= wdtick
;
4585 /* don't start the wd until fw is loaded */
4586 if (bus
->drvr
->busstate
== BRCMF_BUS_DOWN
)
4590 if (bus
->save_ms
!= BRCMF_WD_POLL_MS
) {
4591 if (bus
->wd_timer_valid
== true)
4592 /* Stop timer and restart at new value */
4593 del_timer_sync(&bus
->timer
);
4595 /* Create timer again when watchdog period is
4596 dynamically changed or in the first instance
4598 bus
->timer
.expires
=
4599 jiffies
+ BRCMF_WD_POLL_MS
* HZ
/ 1000;
4600 add_timer(&bus
->timer
);
4603 /* Re arm the timer, at last watchdog period */
4604 mod_timer(&bus
->timer
,
4605 jiffies
+ BRCMF_WD_POLL_MS
* HZ
/ 1000);
4608 bus
->wd_timer_valid
= true;
4609 bus
->save_ms
= wdtick
;