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>
27 #include BCMEMBEDIMAGE
28 #endif /* BCMEMBEDIMAGE */
35 #include <hndrte_armtrap.h>
36 #include <hndrte_cons.h>
37 #endif /* DHD_DEBUG */
43 #include <sbsdpcmdev.h>
46 #include <proto/802.11.h>
48 #include <dngl_stats.h>
51 #include <dhd_proto.h>
57 #define CBUF_LEN (128)
60 u32 buf; /* Can't be pointer on (64-bit) hosts */
63 char *_buf_compat; /* Redundant pointer for backward compat. */
68 * When there is no UART (e.g. Quickturn),
69 * the host should write a complete
70 * input line directly into cbuf and then write
71 * the length into vcons_in.
72 * This may also be used when there is a real UART
73 * (at risk of conflicting with
74 * the real UART). vcons_out is currently unused.
76 volatile uint vcons_in;
77 volatile uint vcons_out;
79 /* Output (logging) buffer
80 * Console output is written to a ring buffer log_buf at index log_idx.
81 * The host may read the output when it sees log_idx advance.
82 * Output will be lost if the output wraps around faster than the host
87 /* Console input line buffer
88 * Characters are read one at a time into cbuf
89 * until <CR> is received, then
90 * the buffer is processed as a command line.
91 * Also used for virtual UART.
98 #include <chipcommon.h>
102 #include "dhd_proto.h"
106 #define TXQLEN 2048 /* bulk tx queue length */
107 #define TXHI (TXQLEN - 256) /* turn on flow control above TXHI */
108 #define TXLOW (TXHI - 256) /* turn off flow control below TXLOW */
111 #define TXRETRIES 2 /* # of retries for tx frames */
113 #define BRCMF_RXBOUND 50 /* Default for max rx frames in
116 #define BRCMF_TXBOUND 20 /* Default for max tx frames in
119 #define BRCMF_TXMINMAX 1 /* Max tx frames if rx still pending */
121 #define MEMBLOCK 2048 /* Block size used for downloading
123 #define MAX_DATA_BUF (32 * 1024) /* Must be large enough to hold
124 biggest possible glom */
126 #ifndef BRCMF_FIRSTREAD
127 #define BRCMF_FIRSTREAD 32
130 #if !ISPOWEROF2(BRCMF_FIRSTREAD)
131 #error BRCMF_FIRSTREAD is not a power of 2!
134 /* SBSDIO_DEVICE_CTL */
135 #define SBSDIO_DEVCTL_SETBUSY 0x01 /* 1: device will assert busy signal when
138 #define SBSDIO_DEVCTL_SPI_INTR_SYNC 0x02 /* 1: assertion of sdio interrupt is
139 * synchronous to the sdio clock
141 #define SBSDIO_DEVCTL_CA_INT_ONLY 0x04 /* 1: mask all interrupts to host
142 * except the chipActive (rev 8)
144 #define SBSDIO_DEVCTL_PADS_ISO 0x08 /* 1: isolate internal sdio signals, put
145 * external pads in tri-state; requires
146 * sdio bus power cycle to clear (rev 9)
148 #define SBSDIO_DEVCTL_SB_RST_CTL 0x30 /* Force SD->SB reset mapping (rev 11) */
149 #define SBSDIO_DEVCTL_RST_CORECTL 0x00 /* Determined by CoreControl bit */
150 #define SBSDIO_DEVCTL_RST_BPRESET 0x10 /* Force backplane reset */
151 #define SBSDIO_DEVCTL_RST_NOBPRESET 0x20 /* Force no backplane reset */
153 /* SBSDIO_FUNC1_CHIPCLKCSR */
154 #define SBSDIO_FORCE_ALP 0x01 /* Force ALP request to backplane */
155 #define SBSDIO_FORCE_HT 0x02 /* Force HT request to backplane */
156 #define SBSDIO_FORCE_ILP 0x04 /* Force ILP request to backplane */
157 #define SBSDIO_ALP_AVAIL_REQ 0x08 /* Make ALP ready (power up xtal) */
158 #define SBSDIO_HT_AVAIL_REQ 0x10 /* Make HT ready (power up PLL) */
159 #define SBSDIO_FORCE_HW_CLKREQ_OFF 0x20 /* Squelch clock requests from HW */
160 #define SBSDIO_ALP_AVAIL 0x40 /* Status: ALP is ready */
161 #define SBSDIO_HT_AVAIL 0x80 /* Status: HT is ready */
163 #define SBSDIO_AVBITS (SBSDIO_HT_AVAIL | SBSDIO_ALP_AVAIL)
164 #define SBSDIO_ALPAV(regval) ((regval) & SBSDIO_AVBITS)
165 #define SBSDIO_HTAV(regval) (((regval) & SBSDIO_AVBITS) == SBSDIO_AVBITS)
166 #define SBSDIO_ALPONLY(regval) (SBSDIO_ALPAV(regval) && !SBSDIO_HTAV(regval))
167 #define SBSDIO_CLKAV(regval, alponly) (SBSDIO_ALPAV(regval) && \
168 (alponly ? 1 : SBSDIO_HTAV(regval)))
169 /* direct(mapped) cis space */
170 #define SBSDIO_CIS_BASE_COMMON 0x1000 /* MAPPED common CIS address */
171 #define SBSDIO_CIS_SIZE_LIMIT 0x200 /* maximum bytes in one CIS */
172 #define SBSDIO_CIS_OFT_ADDR_MASK 0x1FFFF /* cis offset addr is < 17 bits */
174 #define SBSDIO_CIS_MANFID_TUPLE_LEN 6 /* manfid tuple length, include tuple,
179 #define I_SMB_SW0 (1 << 0) /* To SB Mail S/W interrupt 0 */
180 #define I_SMB_SW1 (1 << 1) /* To SB Mail S/W interrupt 1 */
181 #define I_SMB_SW2 (1 << 2) /* To SB Mail S/W interrupt 2 */
182 #define I_SMB_SW3 (1 << 3) /* To SB Mail S/W interrupt 3 */
183 #define I_SMB_SW_MASK 0x0000000f /* To SB Mail S/W interrupts mask */
184 #define I_SMB_SW_SHIFT 0 /* To SB Mail S/W interrupts shift */
185 #define I_HMB_SW0 (1 << 4) /* To Host Mail S/W interrupt 0 */
186 #define I_HMB_SW1 (1 << 5) /* To Host Mail S/W interrupt 1 */
187 #define I_HMB_SW2 (1 << 6) /* To Host Mail S/W interrupt 2 */
188 #define I_HMB_SW3 (1 << 7) /* To Host Mail S/W interrupt 3 */
189 #define I_HMB_SW_MASK 0x000000f0 /* To Host Mail S/W interrupts mask */
190 #define I_HMB_SW_SHIFT 4 /* To Host Mail S/W interrupts shift */
191 #define I_WR_OOSYNC (1 << 8) /* Write Frame Out Of Sync */
192 #define I_RD_OOSYNC (1 << 9) /* Read Frame Out Of Sync */
193 #define I_PC (1 << 10) /* descriptor error */
194 #define I_PD (1 << 11) /* data error */
195 #define I_DE (1 << 12) /* Descriptor protocol Error */
196 #define I_RU (1 << 13) /* Receive descriptor Underflow */
197 #define I_RO (1 << 14) /* Receive fifo Overflow */
198 #define I_XU (1 << 15) /* Transmit fifo Underflow */
199 #define I_RI (1 << 16) /* Receive Interrupt */
200 #define I_BUSPWR (1 << 17) /* SDIO Bus Power Change (rev 9) */
201 #define I_XMTDATA_AVAIL (1 << 23) /* bits in fifo */
202 #define I_XI (1 << 24) /* Transmit Interrupt */
203 #define I_RF_TERM (1 << 25) /* Read Frame Terminate */
204 #define I_WF_TERM (1 << 26) /* Write Frame Terminate */
205 #define I_PCMCIA_XU (1 << 27) /* PCMCIA Transmit FIFO Underflow */
206 #define I_SBINT (1 << 28) /* sbintstatus Interrupt */
207 #define I_CHIPACTIVE (1 << 29) /* chip from doze to active state */
208 #define I_SRESET (1 << 30) /* CCCR RES interrupt */
209 #define I_IOE2 (1U << 31) /* CCCR IOE2 Bit Changed */
210 #define I_ERRORS (I_PC | I_PD | I_DE | I_RU | I_RO | I_XU)
211 #define I_DMA (I_RI | I_XI | I_ERRORS)
214 #define CC_CISRDY (1 << 0) /* CIS Ready */
215 #define CC_BPRESEN (1 << 1) /* CCCR RES signal */
216 #define CC_F2RDY (1 << 2) /* set CCCR IOR2 bit */
217 #define CC_CLRPADSISO (1 << 3) /* clear SDIO pads isolation */
218 #define CC_XMTDATAAVAIL_MODE (1 << 4)
219 #define CC_XMTDATAAVAIL_CTRL (1 << 5)
222 #define SFC_RF_TERM (1 << 0) /* Read Frame Terminate */
223 #define SFC_WF_TERM (1 << 1) /* Write Frame Terminate */
224 #define SFC_CRC4WOOS (1 << 2) /* CRC error for write out of sync */
225 #define SFC_ABORTALL (1 << 3) /* Abort all in-progress frames */
228 #define SDPCM_FRAMETAG_LEN 4 /* 2 bytes len, 2 bytes check val */
230 /* Total length of frame header for dongle protocol */
231 #define SDPCM_HDRLEN (SDPCM_FRAMETAG_LEN + SDPCM_SWHEADER_LEN)
233 #define SDPCM_RESERVE (SDPCM_HDRLEN + SDPCM_TEST_HDRLEN + BRCMF_SDALIGN)
235 #define SDPCM_RESERVE (SDPCM_HDRLEN + BRCMF_SDALIGN)
239 * Software allocation of To SB Mailbox resources
242 /* tosbmailbox bits corresponding to intstatus bits */
243 #define SMB_NAK (1 << 0) /* Frame NAK */
244 #define SMB_INT_ACK (1 << 1) /* Host Interrupt ACK */
245 #define SMB_USE_OOB (1 << 2) /* Use OOB Wakeup */
246 #define SMB_DEV_INT (1 << 3) /* Miscellaneous Interrupt */
248 /* tosbmailboxdata */
249 #define SMB_DATA_VERSION_SHIFT 16 /* host protocol version */
252 * Software allocation of To Host Mailbox resources
256 #define I_HMB_FC_STATE I_HMB_SW0 /* Flow Control State */
257 #define I_HMB_FC_CHANGE I_HMB_SW1 /* Flow Control State Changed */
258 #define I_HMB_FRAME_IND I_HMB_SW2 /* Frame Indication */
259 #define I_HMB_HOST_INT I_HMB_SW3 /* Miscellaneous Interrupt */
261 /* tohostmailboxdata */
262 #define HMB_DATA_NAKHANDLED 1 /* retransmit NAK'd frame */
263 #define HMB_DATA_DEVREADY 2 /* talk to host after enable */
264 #define HMB_DATA_FC 4 /* per prio flowcontrol update flag */
265 #define HMB_DATA_FWREADY 8 /* fw ready for protocol activity */
267 #define HMB_DATA_FCDATA_MASK 0xff000000
268 #define HMB_DATA_FCDATA_SHIFT 24
270 #define HMB_DATA_VERSION_MASK 0x00ff0000
271 #define HMB_DATA_VERSION_SHIFT 16
274 * Software-defined protocol header
277 /* Current protocol version */
278 #define SDPCM_PROT_VERSION 4
280 /* SW frame header */
281 #define SDPCM_PACKET_SEQUENCE(p) (((u8 *)p)[0] & 0xff)
283 #define SDPCM_CHANNEL_MASK 0x00000f00
284 #define SDPCM_CHANNEL_SHIFT 8
285 #define SDPCM_PACKET_CHANNEL(p) (((u8 *)p)[1] & 0x0f)
287 #define SDPCM_NEXTLEN_OFFSET 2
289 /* Data Offset from SOF (HW Tag, SW Tag, Pad) */
290 #define SDPCM_DOFFSET_OFFSET 3 /* Data Offset */
291 #define SDPCM_DOFFSET_VALUE(p) (((u8 *)p)[SDPCM_DOFFSET_OFFSET] & 0xff)
292 #define SDPCM_DOFFSET_MASK 0xff000000
293 #define SDPCM_DOFFSET_SHIFT 24
294 #define SDPCM_FCMASK_OFFSET 4 /* Flow control */
295 #define SDPCM_FCMASK_VALUE(p) (((u8 *)p)[SDPCM_FCMASK_OFFSET] & 0xff)
296 #define SDPCM_WINDOW_OFFSET 5 /* Credit based fc */
297 #define SDPCM_WINDOW_VALUE(p) (((u8 *)p)[SDPCM_WINDOW_OFFSET] & 0xff)
299 #define SDPCM_SWHEADER_LEN 8 /* SW header is 64 bits */
301 /* logical channel numbers */
302 #define SDPCM_CONTROL_CHANNEL 0 /* Control channel Id */
303 #define SDPCM_EVENT_CHANNEL 1 /* Asyc Event Indication Channel Id */
304 #define SDPCM_DATA_CHANNEL 2 /* Data Xmit/Recv Channel Id */
305 #define SDPCM_GLOM_CHANNEL 3 /* For coalesced packets */
306 #define SDPCM_TEST_CHANNEL 15 /* Reserved for test/debug packets */
308 #define SDPCM_SEQUENCE_WRAP 256 /* wrap-around val for 8bit frame seq */
310 #define SDPCM_GLOMDESC(p) (((u8 *)p)[1] & 0x80)
312 /* For TEST_CHANNEL packets, define another 4-byte header */
313 #define SDPCM_TEST_HDRLEN 4 /*
314 * Generally: Cmd(1), Ext(1), Len(2);
315 * Semantics of Ext byte depend on
316 * command. Len is current or requested
317 * frame length, not including test
318 * header; sent little-endian.
320 #define SDPCM_TEST_DISCARD 0x01 /* Receiver discards. Ext:pattern id. */
321 #define SDPCM_TEST_ECHOREQ 0x02 /* Echo request. Ext:pattern id. */
322 #define SDPCM_TEST_ECHORSP 0x03 /* Echo response. Ext:pattern id. */
323 #define SDPCM_TEST_BURST 0x04 /*
324 * Receiver to send a burst.
325 * Ext is a frame count
327 #define SDPCM_TEST_SEND 0x05 /*
328 * Receiver sets send mode.
329 * Ext is boolean on/off
332 /* Handy macro for filling in datagen packets with a pattern */
333 #define SDPCM_TEST_FILL(byteno, id) ((u8)(id + byteno))
336 * Shared structure between dongle and the host.
337 * The structure contains pointers to trap or assert information.
339 #define SDPCM_SHARED_VERSION 0x0002
340 #define SDPCM_SHARED_VERSION_MASK 0x00FF
341 #define SDPCM_SHARED_ASSERT_BUILT 0x0100
342 #define SDPCM_SHARED_ASSERT 0x0200
343 #define SDPCM_SHARED_TRAP 0x0400
346 /* Space for header read, limit for data packets */
348 #define MAX_HDR_READ 32
350 #if !ISPOWEROF2(MAX_HDR_READ)
351 #error MAX_HDR_READ is not a power of 2!
354 #define MAX_RX_DATASZ 2048
356 /* Maximum milliseconds to wait for F2 to come up */
357 #define BRCMF_WAIT_F2RDY 3000
359 /* Bump up limit on waiting for HT to account for first startup;
360 * if the image is doing a CRC calculation before programming the PMU
361 * for HT availability, it could take a couple hundred ms more, so
362 * max out at a 1 second (1000000us).
364 #if (PMU_MAX_TRANSITION_DLY <= 1000000)
365 #undef PMU_MAX_TRANSITION_DLY
366 #define PMU_MAX_TRANSITION_DLY 1000000
369 /* Value for ChipClockCSR during initial setup */
370 #define BRCMF_INIT_CLKCTL1 (SBSDIO_FORCE_HW_CLKREQ_OFF | \
371 SBSDIO_ALP_AVAIL_REQ)
373 /* Flags for SDH calls */
374 #define F2SYNC (SDIO_REQ_4BYTE | SDIO_REQ_FIXED)
377 #define SBIM_IBE 0x20000 /* inbanderror */
378 #define SBIM_TO 0x40000 /* timeout */
379 #define SBIM_BY 0x01800000 /* busy (sonics >= 2.3) */
380 #define SBIM_RJ 0x02000000 /* reject (sonics >= 2.3) */
383 #define SBTML_RESET 0x0001 /* reset */
384 #define SBTML_REJ_MASK 0x0006 /* reject field */
385 #define SBTML_REJ 0x0002 /* reject */
386 #define SBTML_TMPREJ 0x0004 /* temporary reject, for error recovery */
388 #define SBTML_SICF_SHIFT 16 /* Shift to locate the SI control flags in sbtml */
391 #define SBTMH_SERR 0x0001 /* serror */
392 #define SBTMH_INT 0x0002 /* interrupt */
393 #define SBTMH_BUSY 0x0004 /* busy */
394 #define SBTMH_TO 0x0020 /* timeout (sonics >= 2.3) */
396 #define SBTMH_SISF_SHIFT 16 /* Shift to locate the SI status flags in sbtmh */
399 #define SBIDL_INIT 0x80 /* initiator */
402 #define SBIDH_RC_MASK 0x000f /* revision code */
403 #define SBIDH_RCE_MASK 0x7000 /* revision code extension field */
404 #define SBIDH_RCE_SHIFT 8
405 #define SBCOREREV(sbidh) \
406 ((((sbidh) & SBIDH_RCE_MASK) >> SBIDH_RCE_SHIFT) | ((sbidh) & SBIDH_RC_MASK))
407 #define SBIDH_CC_MASK 0x8ff0 /* core code */
408 #define SBIDH_CC_SHIFT 4
409 #define SBIDH_VC_MASK 0xffff0000 /* vendor code */
410 #define SBIDH_VC_SHIFT 16
413 * Conversion of 802.1D priority to precedence level
415 #define PRIO2PREC(prio) \
416 (((prio) == PRIO_8021D_NONE || (prio) == PRIO_8021D_BE) ? \
419 BRCMF_SPINWAIT_SLEEP_INIT(sdioh_spinwait_sleep);
422 * Core reg address translation.
423 * Both macro's returns a 32 bits byte address on the backplane bus.
425 #define CORE_CC_REG(base, field) (base + offsetof(chipcregs_t, field))
426 #define CORE_BUS_REG(base, field) \
427 (base + offsetof(struct sdpcmd_regs, field))
428 #define CORE_SB(base, field) \
429 (base + SBCONFIGOFF + offsetof(struct sbconfig, field))
433 u32 corecontrol; /* 0x00, rev8 */
434 u32 corestatus; /* rev8 */
436 u32 biststatus; /* rev8 */
439 u16 pcmciamesportaladdr; /* 0x010, rev8 */
441 u16 pcmciamesportalmask; /* rev8 */
443 u16 pcmciawrframebc; /* rev8 */
445 u16 pcmciaunderflowtimer; /* rev8 */
449 u32 intstatus; /* 0x020, rev8 */
450 u32 hostintmask; /* rev8 */
451 u32 intmask; /* rev8 */
452 u32 sbintstatus; /* rev8 */
453 u32 sbintmask; /* rev8 */
454 u32 funcintmask; /* rev4 */
456 u32 tosbmailbox; /* 0x040, rev8 */
457 u32 tohostmailbox; /* rev8 */
458 u32 tosbmailboxdata; /* rev8 */
459 u32 tohostmailboxdata; /* rev8 */
461 /* synchronized access to registers in SDIO clock domain */
462 u32 sdioaccess; /* 0x050, rev8 */
465 /* PCMCIA frame control */
466 u8 pcmciaframectrl; /* 0x060, rev8 */
468 u8 pcmciawatermark; /* rev8 */
471 /* interrupt batching control */
472 u32 intrcvlazy; /* 0x100, rev8 */
476 u32 cmd52rd; /* 0x110, rev8 */
477 u32 cmd52wr; /* rev8 */
478 u32 cmd53rd; /* rev8 */
479 u32 cmd53wr; /* rev8 */
480 u32 abort; /* rev8 */
481 u32 datacrcerror; /* rev8 */
482 u32 rdoutofsync; /* rev8 */
483 u32 wroutofsync; /* rev8 */
484 u32 writebusy; /* rev8 */
485 u32 readwait; /* rev8 */
486 u32 readterm; /* rev8 */
487 u32 writeterm; /* rev8 */
489 u32 clockctlstatus; /* rev8 */
492 u32 PAD[128]; /* DMA engines */
494 /* SDIO/PCMCIA CIS region */
495 char cis[512]; /* 0x400-0x5ff, rev6 */
497 /* PCMCIA function control registers */
498 char pcmciafcr[256]; /* 0x600-6ff, rev6 */
501 /* PCMCIA backplane access */
502 u16 backplanecsr; /* 0x76E, rev6 */
503 u16 backplaneaddr0; /* rev6 */
504 u16 backplaneaddr1; /* rev6 */
505 u16 backplaneaddr2; /* rev6 */
506 u16 backplaneaddr3; /* rev6 */
507 u16 backplanedata0; /* rev6 */
508 u16 backplanedata1; /* rev6 */
509 u16 backplanedata2; /* rev6 */
510 u16 backplanedata3; /* rev6 */
513 /* sprom "size" & "blank" info */
514 u16 spromstatus; /* 0x7BE, rev2 */
521 /* Device console log buffer state */
522 struct brcmf_console {
523 uint count; /* Poll interval msec counter */
524 uint log_addr; /* Log struct address (fixed) */
525 struct rte_log log; /* Log struct (host copy) */
526 uint bufsize; /* Size of log buffer */
527 u8 *buf; /* Log buffer (host copy) */
528 uint last; /* Last buffer read index */
532 struct sdpcm_shared {
536 u32 assert_file_addr;
538 u32 console_addr; /* Address of struct rte_console */
544 /* misc chip info needed by some of the routines */
551 u32 buscorebase; /* 32 bits backplane bus address */
560 /* Private data for SDIO bus interaction */
562 struct brcmf_pub *drvr;
564 struct brcmf_sdio_card *card; /* Handle for sdio card calls */
565 struct chip_info *ci; /* Chip info struct */
566 char *vars; /* Variables (from CIS and/or other) */
567 uint varsz; /* Size of variables buffer */
569 u32 ramsize; /* Size of RAM in SOCRAM (bytes) */
570 u32 orig_ramsize; /* Size of RAM in SOCRAM (bytes) */
572 u32 bus; /* gSPI or SDIO bus */
573 u32 hostintmask; /* Copy of Host Interrupt Mask */
574 u32 intstatus; /* Intstatus bits (events) pending */
575 bool dpc_sched; /* Indicates DPC schedule (intrpt rcvd) */
576 bool fcstate; /* State of dongle flow-control */
578 u16 cl_devid; /* cached devid for brcmf_sdio_probe_attach() */
580 uint blocksize; /* Block size of SDIO transfers */
581 uint roundup; /* Max roundup limit */
583 struct pktq txq; /* Queue length used for flow-control */
584 u8 flowcontrol; /* per prio flow control bitmask */
585 u8 tx_seq; /* Transmit sequence number (next) */
586 u8 tx_max; /* Maximum transmit sequence allowed */
588 u8 hdrbuf[MAX_HDR_READ + BRCMF_SDALIGN];
589 u8 *rxhdr; /* Header of current rx frame (in hdrbuf) */
590 u16 nextlen; /* Next Read Len from last header */
591 u8 rx_seq; /* Receive sequence number (expected) */
592 bool rxskip; /* Skip receive (awaiting NAK ACK) */
594 struct sk_buff *glomd; /* Packet containing glomming descriptor */
595 struct sk_buff *glom; /* Packet chain for glommed superframe */
596 uint glomerr; /* Glom packet read errors */
598 u8 *rxbuf; /* Buffer for receiving control packets */
599 uint rxblen; /* Allocated length of rxbuf */
600 u8 *rxctl; /* Aligned pointer into rxbuf */
601 u8 *databuf; /* Buffer for receiving big glom packet */
602 u8 *dataptr; /* Aligned pointer into databuf */
603 uint rxlen; /* Length of valid data in buffer */
605 u8 sdpcm_ver; /* Bus protocol reported by dongle */
607 bool intr; /* Use interrupts */
608 bool poll; /* Use polling */
609 bool ipend; /* Device interrupt is pending */
610 bool intdis; /* Interrupts disabled by isr */
611 uint intrcount; /* Count of device interrupt callbacks */
612 uint lastintrs; /* Count as of last watchdog timer */
613 uint spurious; /* Count of spurious interrupts */
614 uint pollrate; /* Ticks between device polls */
615 uint polltick; /* Tick counter */
616 uint pollcnt; /* Count of active polls */
619 struct brcmf_console console; /* Console output polling support */
620 uint console_addr; /* Console address from shared struct */
623 uint regfails; /* Count of R_REG failures */
625 uint clkstate; /* State of sd and backplane clock(s) */
626 bool activity; /* Activity flag for clock down */
627 s32 idletime; /* Control for activity timeout */
628 s32 idlecount; /* Activity timeout counter */
629 s32 idleclock; /* How to set bus driver when idle */
631 bool use_rxchain; /* If brcmf should use PKT chains */
632 bool sleeping; /* Is SDIO bus sleeping? */
633 bool rxflow_mode; /* Rx flow control mode */
634 bool rxflow; /* Is rx flow control on */
635 bool alp_only; /* Don't use HT clock (ALP only) */
636 /* Field to decide if rx of control frames happen in rxbuf or lb-pool */
640 /* external loopback */
644 /* pktgen configuration */
645 uint pktgen_freq; /* Ticks between bursts */
646 uint pktgen_count; /* Packets to send each burst */
647 uint pktgen_print; /* Bursts between count displays */
648 uint pktgen_total; /* Stop after this many */
649 uint pktgen_minlen; /* Minimum packet data len */
650 uint pktgen_maxlen; /* Maximum packet data len */
651 uint pktgen_mode; /* Configured mode: tx, rx, or echo */
652 uint pktgen_stop; /* Number of tx failures causing stop */
654 /* active pktgen fields */
655 uint pktgen_tick; /* Tick counter for bursts */
656 uint pktgen_ptick; /* Burst counter for printing */
657 uint pktgen_sent; /* Number of test packets generated */
658 uint pktgen_rcvd; /* Number of test packets received */
659 uint pktgen_fail; /* Number of failed send attempts */
660 u16 pktgen_len; /* Length of next packet to send */
663 /* Some additional counters */
664 uint tx_sderrs; /* Count of tx attempts with sd errors */
665 uint fcqueued; /* Tx packets that got queued */
666 uint rxrtx; /* Count of rtx requests (NAK to dongle) */
667 uint rx_toolong; /* Receive frames too long to receive */
668 uint rxc_errors; /* SDIO errors when reading control frames */
669 uint rx_hdrfail; /* SDIO errors on header reads */
670 uint rx_badhdr; /* Bad received headers (roosync?) */
671 uint rx_badseq; /* Mismatched rx sequence number */
672 uint fc_rcvd; /* Number of flow-control events received */
673 uint fc_xoff; /* Number which turned on flow-control */
674 uint fc_xon; /* Number which turned off flow-control */
675 uint rxglomfail; /* Failed deglom attempts */
676 uint rxglomframes; /* Number of glom frames (superframes) */
677 uint rxglompkts; /* Number of packets from glom frames */
678 uint f2rxhdrs; /* Number of header reads */
679 uint f2rxdata; /* Number of frame data reads */
680 uint f2txdata; /* Number of f2 frame writes */
681 uint f1regdata; /* Number of f1 register accesses */
685 bool ctrl_frame_stat;
688 wait_queue_head_t ctrl_wait;
690 struct timer_list timer;
691 struct completion watchdog_wait;
692 struct task_struct *watchdog_tsk;
695 struct tasklet_struct tasklet;
696 struct task_struct *dpc_tsk;
697 struct completion dpc_wait;
700 struct semaphore sdsem;
704 const struct firmware *firmware;
711 u32 sbipsflag; /* initiator port ocp slave flag */
713 u32 sbtpsflag; /* target port ocp slave flag */
715 u32 sbtmerrloga; /* (sonics >= 2.3) */
717 u32 sbtmerrlog; /* (sonics >= 2.3) */
719 u32 sbadmatch3; /* address match3 */
721 u32 sbadmatch2; /* address match2 */
723 u32 sbadmatch1; /* address match1 */
725 u32 sbimstate; /* initiator agent state */
726 u32 sbintvec; /* interrupt mask */
727 u32 sbtmstatelow; /* target state */
728 u32 sbtmstatehigh; /* target state */
729 u32 sbbwa0; /* bandwidth allocation table0 */
731 u32 sbimconfiglow; /* initiator configuration */
732 u32 sbimconfighigh; /* initiator configuration */
733 u32 sbadmatch0; /* address match0 */
735 u32 sbtmconfiglow; /* target configuration */
736 u32 sbtmconfighigh; /* target configuration */
737 u32 sbbconfig; /* broadcast configuration */
739 u32 sbbstate; /* broadcast state */
741 u32 sbactcnfg; /* activate configuration */
743 u32 sbflagst; /* current sbflags */
745 u32 sbidlow; /* identification */
746 u32 sbidhigh; /* identification */
752 #define CLK_PENDING 2 /* Not used yet */
755 #define BRCMF_NOPMU(brcmf) (false)
758 static int qcount[NUMPRIO];
759 static int tx_packets[NUMPRIO];
762 /* Deferred transmit */
763 uint brcmf_deferred_tx = 1;
764 module_param(brcmf_deferred_tx, uint, 0);
766 /* Watchdog thread priority, -1 to use kernel timer */
767 int brcmf_watchdog_prio = 97;
768 module_param(brcmf_watchdog_prio, int, 0);
770 /* Watchdog interval */
771 uint brcmf_watchdog_ms = 10;
772 module_param(brcmf_watchdog_ms, uint, 0);
774 /* DPC thread priority, -1 to use tasklet */
775 int brcmf_dpc_prio = 98;
776 module_param(brcmf_dpc_prio, int, 0);
779 /* Console poll interval */
780 uint brcmf_console_ms;
781 module_param(brcmf_console_ms, uint, 0);
789 /* override the RAM size if possible */
790 #define DONGLE_MIN_MEMSIZE (128 * 1024)
791 int brcmf_dongle_memsize;
793 static bool brcmf_alignctl;
797 static bool retrydata;
798 #define RETRYCHAN(chan) (((chan) == SDPCM_EVENT_CHANNEL) || retrydata)
800 static const uint watermark = 8;
801 static const uint firstread = BRCMF_FIRSTREAD;
803 /* Retry count for register access failures */
804 static const uint retry_limit = 2;
806 /* Force even SD lengths (some host controllers mess up on odd bytes) */
807 static bool forcealign;
811 #define PKTALIGN(_p, _len, _align) \
814 datalign = (unsigned long)((_p)->data); \
815 datalign = roundup(datalign, (_align)) - datalign; \
817 skb_pull((_p), datalign); \
818 __skb_trim((_p), (_len)); \
821 /* Limit on rounding up frames */
822 static const uint max_roundup = 512;
824 /* Try doing readahead */
825 static bool brcmf_readahead;
827 /* To check if there's window offered */
828 #define DATAOK(bus) \
829 (((u8)(bus->tx_max - bus->tx_seq) != 0) && \
830 (((u8)(bus->tx_max - bus->tx_seq) & 0x80) == 0))
833 * Reads a register in the SDIO hardware block. This block occupies a series of
834 * adresses on the 32 bit backplane bus.
837 r_sdreg32(struct brcmf_bus *bus, u32 *regvar, u32 reg_offset, u32 *retryvar)
841 *regvar = R_REG(bus->ci->buscorebase + reg_offset, u32);
842 } while (brcmf_sdcard_regfail(bus->card) &&
843 (++(*retryvar) <= retry_limit));
845 bus->regfails += (*retryvar-1);
846 if (*retryvar > retry_limit) {
847 BRCMF_ERROR(("FAILED READ %Xh\n", reg_offset));
854 w_sdreg32(struct brcmf_bus *bus, u32 regval, u32 reg_offset, u32 *retryvar)
858 brcmf_sdcard_reg_write(NULL, bus->ci->buscorebase + reg_offset,
859 sizeof(u32), regval);
860 } while (brcmf_sdcard_regfail(bus->card) &&
861 (++(*retryvar) <= retry_limit));
863 bus->regfails += (*retryvar-1);
864 if (*retryvar > retry_limit)
865 BRCMF_ERROR(("FAILED REGISTER WRITE"
866 " %Xh\n", reg_offset));
870 #define BRCMF_BUS SDIO_BUS
872 #define PKT_AVAILABLE() (intstatus & I_HMB_FRAME_IND)
874 #define HOSTINTMASK (I_HMB_SW_MASK | I_CHIPACTIVE)
877 static void brcmf_sdbrcm_checkdied(struct brcmf_bus *bus, void *pkt, uint seq);
878 static void brcmf_sdbrcm_sdtest_set(struct brcmf_bus *bus, bool start);
882 static int brcmf_sdbrcm_bus_console_in(struct brcmf_pub *drvr,
883 unsigned char *msg, uint msglen);
884 static int brcmf_sdbrcm_checkdied(struct brcmf_bus *bus, u8 *data, uint size);
885 static int brcmf_sdbrcm_mem_dump(struct brcmf_bus *bus);
887 static int brcmf_sdbrcm_download_state(struct brcmf_bus *bus, bool enter);
889 static void brcmf_sdbrcm_release(struct brcmf_bus *bus);
890 static void brcmf_sdbrcm_release_malloc(struct brcmf_bus *bus);
891 static void brcmf_sdbrcm_disconnect(void *ptr);
892 static bool brcmf_sdbrcm_chipmatch(u16 chipid);
893 static bool brcmf_sdbrcm_probe_attach(struct brcmf_bus *bus, void *card,
894 u32 regsva, u16 devid);
895 static bool brcmf_sdbrcm_probe_malloc(struct brcmf_bus *bus, void *card);
896 static bool brcmf_sdbrcm_probe_init(struct brcmf_bus *bus, void *card);
897 static void brcmf_sdbrcm_release_dongle(struct brcmf_bus *bus);
899 static uint brcmf_process_nvram_vars(char *varbuf, uint len);
901 static void brcmf_sdbrcm_setmemsize(struct brcmf_bus *bus, int mem_size);
902 static int brcmf_sdbrcm_send_buf(struct brcmf_bus *bus, u32 addr, uint fn,
903 uint flags, u8 *buf, uint nbytes,
905 void (*complete)(void *handle, int status,
909 static bool brcmf_sdbrcm_download_firmware(struct brcmf_bus *bus, void *card);
910 static int _brcmf_sdbrcm_download_firmware(struct brcmf_bus *bus);
912 static int brcmf_sdbrcm_download_code_file(struct brcmf_bus *bus);
913 static int brcmf_sdbrcm_download_nvram(struct brcmf_bus *bus);
916 brcmf_sdbrcm_chip_disablecore(struct brcmf_sdio_card *card, u32 corebase);
918 static int brcmf_sdbrcm_chip_attach(struct brcmf_bus *bus, u32 regs);
921 brcmf_sdbrcm_chip_resetcore(struct brcmf_sdio_card *card, u32 corebase);
923 static void brcmf_sdbrcm_sdiod_drive_strength_init(struct brcmf_bus *bus,
925 static void brcmf_sdbrcm_chip_detach(struct brcmf_bus *bus);
926 static void brcmf_sdbrcm_wait_for_event(struct brcmf_bus *bus, bool *lockvar);
927 static void brcmf_sdbrcm_wait_event_wakeup(struct brcmf_bus *bus);
928 static void brcmf_sdbrcm_watchdog(unsigned long data);
929 static int brcmf_sdbrcm_watchdog_thread(void *data);
930 static int brcmf_sdbrcm_dpc_thread(void *data);
931 static void brcmf_sdbrcm_dpc_tasklet(unsigned long data);
932 static void brcmf_sdbrcm_sched_dpc(struct brcmf_bus *bus);
933 static void brcmf_sdbrcm_sdlock(struct brcmf_bus *bus);
934 static void brcmf_sdbrcm_sdunlock(struct brcmf_bus *bus);
935 static int brcmf_sdbrcm_get_image(char *buf, int len, struct brcmf_bus *bus);
937 /* Packet free applicable unconditionally for sdio and sdspi.
938 * Conditional if bufpool was present for gspi bus.
940 static void brcmf_sdbrcm_pktfree2(struct brcmf_bus *bus, struct sk_buff *pkt)
942 if ((bus->bus != SPI_BUS) || bus->usebufpool)
943 brcmu_pkt_buf_free_skb(pkt);
946 static void brcmf_sdbrcm_setmemsize(struct brcmf_bus *bus, int mem_size)
948 s32 min_size = DONGLE_MIN_MEMSIZE;
949 /* Restrict the memsize to user specified limit */
950 BRCMF_ERROR(("user: Restrict the dongle ram size to %d, min %d\n",
951 brcmf_dongle_memsize, min_size));
952 if ((brcmf_dongle_memsize > min_size) &&
953 (brcmf_dongle_memsize < (s32) bus->orig_ramsize))
954 bus->ramsize = brcmf_dongle_memsize;
957 static int brcmf_sdbrcm_set_siaddr_window(struct brcmf_bus *bus, u32 address)
960 brcmf_sdcard_cfg_write(bus->card, SDIO_FUNC_1, SBSDIO_FUNC1_SBADDRLOW,
961 (address >> 8) & SBSDIO_SBADDRLOW_MASK, &err);
963 brcmf_sdcard_cfg_write(bus->card, SDIO_FUNC_1,
964 SBSDIO_FUNC1_SBADDRMID,
965 (address >> 16) & SBSDIO_SBADDRMID_MASK, &err);
967 brcmf_sdcard_cfg_write(bus->card, SDIO_FUNC_1,
968 SBSDIO_FUNC1_SBADDRHIGH,
969 (address >> 24) & SBSDIO_SBADDRHIGH_MASK,
974 /* Turn backplane clock on or off */
975 static int brcmf_sdbrcm_htclk(struct brcmf_bus *bus, bool on, bool pendok)
978 u8 clkctl, clkreq, devctl;
979 struct brcmf_sdio_card *card;
981 BRCMF_TRACE(("%s: Enter\n", __func__));
987 /* Request HT Avail */
989 bus->alp_only ? SBSDIO_ALP_AVAIL_REQ : SBSDIO_HT_AVAIL_REQ;
991 if ((bus->ci->chip == BCM4329_CHIP_ID)
992 && (bus->ci->chiprev == 0))
993 clkreq |= SBSDIO_FORCE_ALP;
995 brcmf_sdcard_cfg_write(card, SDIO_FUNC_1,
996 SBSDIO_FUNC1_CHIPCLKCSR, clkreq, &err);
998 BRCMF_ERROR(("%s: HT Avail request error: %d\n",
1003 if (pendok && ((bus->ci->buscoretype == PCMCIA_CORE_ID)
1004 && (bus->ci->buscorerev == 9))) {
1006 r_sdreg32(bus, &dummy,
1007 offsetof(struct sdpcmd_regs, clockctlstatus),
1011 /* Check current status */
1012 clkctl = brcmf_sdcard_cfg_read(card, SDIO_FUNC_1,
1013 SBSDIO_FUNC1_CHIPCLKCSR, &err);
1015 BRCMF_ERROR(("%s: HT Avail read error: %d\n",
1020 /* Go to pending and await interrupt if appropriate */
1021 if (!SBSDIO_CLKAV(clkctl, bus->alp_only) && pendok) {
1022 /* Allow only clock-available interrupt */
1023 devctl = brcmf_sdcard_cfg_read(card, SDIO_FUNC_1,
1024 SBSDIO_DEVICE_CTL, &err);
1026 BRCMF_ERROR(("%s: Devctl error setting CA:"
1027 " %d\n", __func__, err));
1031 devctl |= SBSDIO_DEVCTL_CA_INT_ONLY;
1032 brcmf_sdcard_cfg_write(card, SDIO_FUNC_1,
1033 SBSDIO_DEVICE_CTL, devctl, &err);
1034 BRCMF_INFO(("CLKCTL: set PENDING\n"));
1035 bus->clkstate = CLK_PENDING;
1038 } else if (bus->clkstate == CLK_PENDING) {
1039 /* Cancel CA-only interrupt filter */
1041 brcmf_sdcard_cfg_read(card, SDIO_FUNC_1,
1042 SBSDIO_DEVICE_CTL, &err);
1043 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
1044 brcmf_sdcard_cfg_write(card, SDIO_FUNC_1,
1045 SBSDIO_DEVICE_CTL, devctl, &err);
1048 /* Otherwise, wait here (polling) for HT Avail */
1049 if (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
1050 BRCMF_SPINWAIT_SLEEP(sdioh_spinwait_sleep,
1052 brcmf_sdcard_cfg_read(card, SDIO_FUNC_1,
1053 SBSDIO_FUNC1_CHIPCLKCSR,
1055 !SBSDIO_CLKAV(clkctl, bus->alp_only)),
1056 PMU_MAX_TRANSITION_DLY);
1059 BRCMF_ERROR(("%s: HT Avail request error: %d\n",
1063 if (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
1064 BRCMF_ERROR(("%s: HT Avail timeout (%d): "
1065 "clkctl 0x%02x\n", __func__,
1066 PMU_MAX_TRANSITION_DLY, clkctl));
1070 /* Mark clock available */
1071 bus->clkstate = CLK_AVAIL;
1072 BRCMF_INFO(("CLKCTL: turned ON\n"));
1075 if (bus->alp_only != true) {
1076 if (SBSDIO_ALPONLY(clkctl)) {
1077 BRCMF_ERROR(("%s: HT Clock should be on.\n",
1081 #endif /* defined (BCMDBG) */
1083 bus->activity = true;
1087 if (bus->clkstate == CLK_PENDING) {
1088 /* Cancel CA-only interrupt filter */
1089 devctl = brcmf_sdcard_cfg_read(card, SDIO_FUNC_1,
1090 SBSDIO_DEVICE_CTL, &err);
1091 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
1092 brcmf_sdcard_cfg_write(card, SDIO_FUNC_1,
1093 SBSDIO_DEVICE_CTL, devctl, &err);
1096 bus->clkstate = CLK_SDONLY;
1097 brcmf_sdcard_cfg_write(card, SDIO_FUNC_1,
1098 SBSDIO_FUNC1_CHIPCLKCSR, clkreq, &err);
1099 BRCMF_INFO(("CLKCTL: turned OFF\n"));
1101 BRCMF_ERROR(("%s: Failed access turning clock off:"
1102 " %d\n", __func__, err));
1109 /* Change idle/active SD state */
1110 static int brcmf_sdbrcm_sdclk(struct brcmf_bus *bus, bool on)
1112 BRCMF_TRACE(("%s: Enter\n", __func__));
1115 bus->clkstate = CLK_SDONLY;
1117 bus->clkstate = CLK_NONE;
1122 /* Transition SD and backplane clock readiness */
1123 static int brcmf_sdbrcm_clkctl(struct brcmf_bus *bus, uint target, bool pendok)
1126 uint oldstate = bus->clkstate;
1129 BRCMF_TRACE(("%s: Enter\n", __func__));
1131 /* Early exit if we're already there */
1132 if (bus->clkstate == target) {
1133 if (target == CLK_AVAIL) {
1134 brcmf_sdbrcm_wd_timer(bus, brcmf_watchdog_ms);
1135 bus->activity = true;
1142 /* Make sure SD clock is available */
1143 if (bus->clkstate == CLK_NONE)
1144 brcmf_sdbrcm_sdclk(bus, true);
1145 /* Now request HT Avail on the backplane */
1146 brcmf_sdbrcm_htclk(bus, true, pendok);
1147 brcmf_sdbrcm_wd_timer(bus, brcmf_watchdog_ms);
1148 bus->activity = true;
1152 /* Remove HT request, or bring up SD clock */
1153 if (bus->clkstate == CLK_NONE)
1154 brcmf_sdbrcm_sdclk(bus, true);
1155 else if (bus->clkstate == CLK_AVAIL)
1156 brcmf_sdbrcm_htclk(bus, false, false);
1158 BRCMF_ERROR(("brcmf_sdbrcm_clkctl: request for %d -> %d"
1159 "\n", bus->clkstate, target));
1160 brcmf_sdbrcm_wd_timer(bus, brcmf_watchdog_ms);
1164 /* Make sure to remove HT request */
1165 if (bus->clkstate == CLK_AVAIL)
1166 brcmf_sdbrcm_htclk(bus, false, false);
1167 /* Now remove the SD clock */
1168 brcmf_sdbrcm_sdclk(bus, false);
1169 brcmf_sdbrcm_wd_timer(bus, 0);
1173 BRCMF_INFO(("brcmf_sdbrcm_clkctl: %d -> %d\n",
1174 oldstate, bus->clkstate));
1180 int brcmf_sdbrcm_bussleep(struct brcmf_bus *bus, bool sleep)
1182 struct brcmf_sdio_card *card = bus->card;
1185 BRCMF_INFO(("brcmf_sdbrcm_bussleep: request %s (currently %s)\n",
1186 (sleep ? "SLEEP" : "WAKE"),
1187 (bus->sleeping ? "SLEEP" : "WAKE")));
1189 /* Done if we're already in the requested state */
1190 if (sleep == bus->sleeping)
1193 /* Going to sleep: set the alarm and turn off the lights... */
1195 /* Don't sleep if something is pending */
1196 if (bus->dpc_sched || bus->rxskip || pktq_len(&bus->txq))
1199 /* Disable SDIO interrupts (no longer interested) */
1200 brcmf_sdcard_intr_disable(bus->card);
1202 /* Make sure the controller has the bus up */
1203 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
1205 /* Tell device to start using OOB wakeup */
1206 w_sdreg32(bus, SMB_USE_OOB,
1207 offsetof(struct sdpcmd_regs, tosbmailbox), &retries);
1208 if (retries > retry_limit)
1209 BRCMF_ERROR(("CANNOT SIGNAL CHIP, "
1210 "WILL NOT WAKE UP!!\n"));
1212 /* Turn off our contribution to the HT clock request */
1213 brcmf_sdbrcm_clkctl(bus, CLK_SDONLY, false);
1215 brcmf_sdcard_cfg_write(card, SDIO_FUNC_1,
1216 SBSDIO_FUNC1_CHIPCLKCSR,
1217 SBSDIO_FORCE_HW_CLKREQ_OFF, NULL);
1219 /* Isolate the bus */
1220 if (bus->ci->chip != BCM4329_CHIP_ID
1221 && bus->ci->chip != BCM4319_CHIP_ID) {
1222 brcmf_sdcard_cfg_write(card, SDIO_FUNC_1,
1224 SBSDIO_DEVCTL_PADS_ISO, NULL);
1228 bus->sleeping = true;
1231 /* Waking up: bus power up is ok, set local state */
1233 brcmf_sdcard_cfg_write(card, SDIO_FUNC_1,
1234 SBSDIO_FUNC1_CHIPCLKCSR, 0, NULL);
1236 /* Force pad isolation off if possible
1237 (in case power never toggled) */
1238 if ((bus->ci->buscoretype == PCMCIA_CORE_ID)
1239 && (bus->ci->buscorerev >= 10))
1240 brcmf_sdcard_cfg_write(card, SDIO_FUNC_1,
1241 SBSDIO_DEVICE_CTL, 0, NULL);
1243 /* Make sure the controller has the bus up */
1244 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
1246 /* Send misc interrupt to indicate OOB not needed */
1247 w_sdreg32(bus, 0, offsetof(struct sdpcmd_regs, tosbmailboxdata),
1249 if (retries <= retry_limit)
1250 w_sdreg32(bus, SMB_DEV_INT,
1251 offsetof(struct sdpcmd_regs, tosbmailbox),
1254 if (retries > retry_limit)
1255 BRCMF_ERROR(("CANNOT SIGNAL CHIP TO CLEAR OOB!!\n"));
1257 /* Make sure we have SD bus access */
1258 brcmf_sdbrcm_clkctl(bus, CLK_SDONLY, false);
1261 bus->sleeping = false;
1263 /* Enable interrupts again */
1264 if (bus->intr && (bus->drvr->busstate == BRCMF_BUS_DATA)) {
1265 bus->intdis = false;
1266 brcmf_sdcard_intr_enable(bus->card);
1273 #define BUS_WAKE(bus) \
1275 if ((bus)->sleeping) \
1276 brcmf_sdbrcm_bussleep((bus), false); \
1279 /* Writes a HW/SW header into the packet and sends it. */
1280 /* Assumes: (a) header space already there, (b) caller holds lock */
1281 static int brcmf_sdbrcm_txpkt(struct brcmf_bus *bus, struct sk_buff *pkt, uint chan,
1289 struct brcmf_sdio_card *card;
1290 struct sk_buff *new;
1293 BRCMF_TRACE(("%s: Enter\n", __func__));
1297 if (bus->drvr->dongle_reset) {
1302 frame = (u8 *) (pkt->data);
1304 /* Add alignment padding, allocate new packet if needed */
1305 pad = ((unsigned long)frame % BRCMF_SDALIGN);
1307 if (skb_headroom(pkt) < pad) {
1308 BRCMF_INFO(("%s: insufficient headroom %d for %d pad\n",
1309 __func__, skb_headroom(pkt), pad));
1310 bus->drvr->tx_realloc++;
1311 new = brcmu_pkt_buf_get_skb(pkt->len + BRCMF_SDALIGN);
1313 BRCMF_ERROR(("%s: couldn't allocate new "
1314 "%d-byte packet\n", __func__,
1315 pkt->len + BRCMF_SDALIGN));
1320 PKTALIGN(new, pkt->len, BRCMF_SDALIGN);
1321 memcpy(new->data, pkt->data, pkt->len);
1323 brcmu_pkt_buf_free_skb(pkt);
1324 /* free the pkt if canned one is not used */
1327 frame = (u8 *) (pkt->data);
1328 /* precondition: (frame % BRCMF_SDALIGN) == 0) */
1332 frame = (u8 *) (pkt->data);
1333 /* precondition: pad + SDPCM_HDRLEN <= pkt->len */
1334 memset(frame, 0, pad + SDPCM_HDRLEN);
1337 /* precondition: pad < BRCMF_SDALIGN */
1339 /* Hardware tag: 2 byte len followed by 2 byte ~len check (all LE) */
1340 len = (u16) (pkt->len);
1341 *(u16 *) frame = cpu_to_le16(len);
1342 *(((u16 *) frame) + 1) = cpu_to_le16(~len);
1344 /* Software tag: channel, sequence number, data offset */
1346 ((chan << SDPCM_CHANNEL_SHIFT) & SDPCM_CHANNEL_MASK) | bus->tx_seq |
1348 SDPCM_HDRLEN) << SDPCM_DOFFSET_SHIFT) & SDPCM_DOFFSET_MASK);
1350 put_unaligned_le32(swheader, frame + SDPCM_FRAMETAG_LEN);
1351 put_unaligned_le32(0, frame + SDPCM_FRAMETAG_LEN + sizeof(swheader));
1354 tx_packets[pkt->priority]++;
1355 if (BRCMF_BYTES_ON() &&
1356 (((BRCMF_CTL_ON() && (chan == SDPCM_CONTROL_CHANNEL)) ||
1357 (BRCMF_DATA_ON() && (chan != SDPCM_CONTROL_CHANNEL))))) {
1358 printk(KERN_DEBUG "Tx Frame:\n");
1359 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, frame, len);
1360 } else if (BRCMF_HDRS_ON()) {
1361 printk(KERN_DEBUG "TxHdr:\n");
1362 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
1363 frame, min_t(u16, len, 16));
1367 /* Raise len to next SDIO block to eliminate tail command */
1368 if (bus->roundup && bus->blocksize && (len > bus->blocksize)) {
1369 u16 pad = bus->blocksize - (len % bus->blocksize);
1370 if ((pad <= bus->roundup) && (pad < bus->blocksize))
1372 } else if (len % BRCMF_SDALIGN) {
1373 len += BRCMF_SDALIGN - (len % BRCMF_SDALIGN);
1376 /* Some controllers have trouble with odd bytes -- round to even */
1377 if (forcealign && (len & (ALIGNMENT - 1))) {
1378 len = roundup(len, ALIGNMENT);
1382 ret = brcmf_sdbrcm_send_buf(bus, brcmf_sdcard_cur_sbwad(card),
1383 SDIO_FUNC_2, F2SYNC, frame, len, pkt, NULL, NULL);
1387 /* On failure, abort the command
1388 and terminate the frame */
1389 BRCMF_INFO(("%s: sdio error %d, abort command and "
1390 "terminate frame.\n", __func__, ret));
1393 brcmf_sdcard_abort(card, SDIO_FUNC_2);
1394 brcmf_sdcard_cfg_write(card, SDIO_FUNC_1,
1395 SBSDIO_FUNC1_FRAMECTRL, SFC_WF_TERM,
1399 for (i = 0; i < 3; i++) {
1401 hi = brcmf_sdcard_cfg_read(card, SDIO_FUNC_1,
1402 SBSDIO_FUNC1_WFRAMEBCHI,
1404 lo = brcmf_sdcard_cfg_read(card, SDIO_FUNC_1,
1405 SBSDIO_FUNC1_WFRAMEBCLO,
1407 bus->f1regdata += 2;
1408 if ((hi == 0) && (lo == 0))
1414 bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQUENCE_WRAP;
1416 } while ((ret < 0) && retrydata && retries++ < TXRETRIES);
1419 /* restore pkt buffer pointer before calling tx complete routine */
1420 skb_pull(pkt, SDPCM_HDRLEN + pad);
1421 brcmf_sdbrcm_sdunlock(bus);
1422 brcmf_txcomplete(bus->drvr, pkt, ret != 0);
1423 brcmf_sdbrcm_sdlock(bus);
1426 brcmu_pkt_buf_free_skb(pkt);
1431 int brcmf_sdbrcm_bus_txdata(struct brcmf_bus *bus, struct sk_buff *pkt)
1436 BRCMF_TRACE(("%s: Enter\n", __func__));
1441 /* Push the test header if doing loopback */
1442 if (bus->ext_loop) {
1444 skb_push(pkt, SDPCM_TEST_HDRLEN);
1446 *data++ = SDPCM_TEST_ECHOREQ;
1447 *data++ = (u8) bus->loopid++;
1448 *data++ = (datalen >> 0);
1449 *data++ = (datalen >> 8);
1450 datalen += SDPCM_TEST_HDRLEN;
1454 /* Add space for the header */
1455 skb_push(pkt, SDPCM_HDRLEN);
1456 /* precondition: IS_ALIGNED((unsigned long)(pkt->data), 2) */
1458 prec = PRIO2PREC((pkt->priority & PRIOMASK));
1460 /* Check for existing queue, current flow-control,
1461 pending event, or pending clock */
1462 if (brcmf_deferred_tx || bus->fcstate || pktq_len(&bus->txq)
1463 || bus->dpc_sched || (!DATAOK(bus))
1464 || (bus->flowcontrol & NBITVAL(prec))
1465 || (bus->clkstate != CLK_AVAIL)) {
1466 BRCMF_TRACE(("%s: deferring pktq len %d\n", __func__,
1467 pktq_len(&bus->txq)));
1470 /* Priority based enq */
1471 spin_lock_bh(&bus->txqlock);
1472 if (brcmf_c_prec_enq(bus->drvr, &bus->txq, pkt, prec) == false) {
1473 skb_pull(pkt, SDPCM_HDRLEN);
1474 brcmf_txcomplete(bus->drvr, pkt, false);
1475 brcmu_pkt_buf_free_skb(pkt);
1476 BRCMF_ERROR(("%s: out of bus->txq !!!\n", __func__));
1481 spin_unlock_bh(&bus->txqlock);
1483 if (pktq_len(&bus->txq) >= TXHI)
1484 brcmf_txflowcontrol(bus->drvr, 0, ON);
1487 if (pktq_plen(&bus->txq, prec) > qcount[prec])
1488 qcount[prec] = pktq_plen(&bus->txq, prec);
1490 /* Schedule DPC if needed to send queued packet(s) */
1491 if (brcmf_deferred_tx && !bus->dpc_sched) {
1492 bus->dpc_sched = true;
1493 brcmf_sdbrcm_sched_dpc(bus);
1496 /* Lock: we're about to use shared data/code (and SDIO) */
1497 brcmf_sdbrcm_sdlock(bus);
1499 /* Otherwise, send it now */
1501 /* Make sure back plane ht clk is on, no pending allowed */
1502 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, true);
1505 BRCMF_TRACE(("%s: calling txpkt\n", __func__));
1506 ret = brcmf_sdbrcm_txpkt(bus, pkt, SDPCM_DATA_CHANNEL, true);
1508 ret = brcmf_sdbrcm_txpkt(bus, pkt,
1509 (bus->ext_loop ? SDPCM_TEST_CHANNEL :
1510 SDPCM_DATA_CHANNEL), true);
1513 bus->drvr->tx_errors++;
1515 bus->drvr->dstats.tx_bytes += datalen;
1517 if (bus->idletime == BRCMF_IDLE_IMMEDIATE &&
1519 bus->activity = false;
1520 brcmf_sdbrcm_clkctl(bus, CLK_NONE, true);
1523 brcmf_sdbrcm_sdunlock(bus);
1529 static uint brcmf_sdbrcm_sendfromq(struct brcmf_bus *bus, uint maxframes)
1531 struct sk_buff *pkt;
1534 int ret = 0, prec_out;
1539 struct brcmf_pub *drvr = bus->drvr;
1541 BRCMF_TRACE(("%s: Enter\n", __func__));
1543 tx_prec_map = ~bus->flowcontrol;
1545 /* Send frames until the limit or some other event */
1546 for (cnt = 0; (cnt < maxframes) && DATAOK(bus); cnt++) {
1547 spin_lock_bh(&bus->txqlock);
1548 pkt = brcmu_pktq_mdeq(&bus->txq, tx_prec_map, &prec_out);
1550 spin_unlock_bh(&bus->txqlock);
1553 spin_unlock_bh(&bus->txqlock);
1554 datalen = pkt->len - SDPCM_HDRLEN;
1557 ret = brcmf_sdbrcm_txpkt(bus, pkt, SDPCM_DATA_CHANNEL, true);
1559 ret = brcmf_sdbrcm_txpkt(bus, pkt,
1560 (bus->ext_loop ? SDPCM_TEST_CHANNEL :
1561 SDPCM_DATA_CHANNEL), true);
1564 bus->drvr->tx_errors++;
1566 bus->drvr->dstats.tx_bytes += datalen;
1568 /* In poll mode, need to check for other events */
1569 if (!bus->intr && cnt) {
1570 /* Check device status, signal pending interrupt */
1571 r_sdreg32(bus, &intstatus,
1572 offsetof(struct sdpcmd_regs, intstatus),
1575 if (brcmf_sdcard_regfail(bus->card))
1577 if (intstatus & bus->hostintmask)
1582 /* Deflow-control stack if needed */
1583 if (drvr->up && (drvr->busstate == BRCMF_BUS_DATA) &&
1584 drvr->txoff && (pktq_len(&bus->txq) < TXLOW))
1585 brcmf_txflowcontrol(drvr, 0, OFF);
1591 brcmf_sdbrcm_bus_txctl(struct brcmf_bus *bus, unsigned char *msg, uint msglen)
1597 struct brcmf_sdio_card *card = bus->card;
1602 BRCMF_TRACE(("%s: Enter\n", __func__));
1604 if (bus->drvr->dongle_reset)
1607 /* Back the pointer to make a room for bus header */
1608 frame = msg - SDPCM_HDRLEN;
1609 len = (msglen += SDPCM_HDRLEN);
1611 /* Add alignment padding (optional for ctl frames) */
1612 if (brcmf_alignctl) {
1613 doff = ((unsigned long)frame % BRCMF_SDALIGN);
1618 memset(frame, 0, doff + SDPCM_HDRLEN);
1620 /* precondition: doff < BRCMF_SDALIGN */
1622 doff += SDPCM_HDRLEN;
1624 /* Round send length to next SDIO block */
1625 if (bus->roundup && bus->blocksize && (len > bus->blocksize)) {
1626 u16 pad = bus->blocksize - (len % bus->blocksize);
1627 if ((pad <= bus->roundup) && (pad < bus->blocksize))
1629 } else if (len % BRCMF_SDALIGN) {
1630 len += BRCMF_SDALIGN - (len % BRCMF_SDALIGN);
1633 /* Satisfy length-alignment requirements */
1634 if (forcealign && (len & (ALIGNMENT - 1)))
1635 len = roundup(len, ALIGNMENT);
1637 /* precondition: IS_ALIGNED((unsigned long)frame, 2) */
1639 /* Need to lock here to protect txseq and SDIO tx calls */
1640 brcmf_sdbrcm_sdlock(bus);
1644 /* Make sure backplane clock is on */
1645 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
1647 /* Hardware tag: 2 byte len followed by 2 byte ~len check (all LE) */
1648 *(u16 *) frame = cpu_to_le16((u16) msglen);
1649 *(((u16 *) frame) + 1) = cpu_to_le16(~msglen);
1651 /* Software tag: channel, sequence number, data offset */
1653 ((SDPCM_CONTROL_CHANNEL << SDPCM_CHANNEL_SHIFT) &
1655 | bus->tx_seq | ((doff << SDPCM_DOFFSET_SHIFT) &
1656 SDPCM_DOFFSET_MASK);
1657 put_unaligned_le32(swheader, frame + SDPCM_FRAMETAG_LEN);
1658 put_unaligned_le32(0, frame + SDPCM_FRAMETAG_LEN + sizeof(swheader));
1661 BRCMF_INFO(("%s: No bus credit bus->tx_max %d,"
1662 " bus->tx_seq %d\n", __func__,
1663 bus->tx_max, bus->tx_seq));
1664 bus->ctrl_frame_stat = true;
1666 bus->ctrl_frame_buf = frame;
1667 bus->ctrl_frame_len = len;
1669 brcmf_sdbrcm_wait_for_event(bus, &bus->ctrl_frame_stat);
1671 if (bus->ctrl_frame_stat == false) {
1672 BRCMF_INFO(("%s: ctrl_frame_stat == false\n",
1676 BRCMF_INFO(("%s: ctrl_frame_stat == true\n", __func__));
1683 if (BRCMF_BYTES_ON() && BRCMF_CTL_ON()) {
1684 printk(KERN_DEBUG "Tx Frame:\n");
1685 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
1687 } else if (BRCMF_HDRS_ON()) {
1688 printk(KERN_DEBUG "TxHdr:\n");
1689 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
1690 frame, min_t(u16, len, 16));
1695 bus->ctrl_frame_stat = false;
1696 ret = brcmf_sdbrcm_send_buf(bus,
1697 brcmf_sdcard_cur_sbwad(card), SDIO_FUNC_2,
1698 F2SYNC, frame, len, NULL, NULL, NULL);
1701 /* On failure, abort the command and
1702 terminate the frame */
1703 BRCMF_INFO(("%s: sdio error %d, abort command "
1704 "and terminate frame.\n",
1708 brcmf_sdcard_abort(card, SDIO_FUNC_2);
1710 brcmf_sdcard_cfg_write(card, SDIO_FUNC_1,
1711 SBSDIO_FUNC1_FRAMECTRL,
1715 for (i = 0; i < 3; i++) {
1717 hi = brcmf_sdcard_cfg_read(card,
1719 SBSDIO_FUNC1_WFRAMEBCHI,
1721 lo = brcmf_sdcard_cfg_read(card,
1723 SBSDIO_FUNC1_WFRAMEBCLO,
1725 bus->f1regdata += 2;
1726 if ((hi == 0) && (lo == 0))
1733 (bus->tx_seq + 1) % SDPCM_SEQUENCE_WRAP;
1735 } while ((ret < 0) && retries++ < TXRETRIES);
1738 if ((bus->idletime == BRCMF_IDLE_IMMEDIATE) && !bus->dpc_sched) {
1739 bus->activity = false;
1740 brcmf_sdbrcm_clkctl(bus, CLK_NONE, true);
1743 brcmf_sdbrcm_sdunlock(bus);
1746 bus->drvr->tx_ctlerrs++;
1748 bus->drvr->tx_ctlpkts++;
1750 return ret ? -EIO : 0;
1753 int brcmf_sdbrcm_bus_rxctl(struct brcmf_bus *bus, unsigned char *msg, uint msglen)
1759 BRCMF_TRACE(("%s: Enter\n", __func__));
1761 if (bus->drvr->dongle_reset)
1764 /* Wait until control frame is available */
1765 timeleft = brcmf_os_ioctl_resp_wait(bus->drvr, &bus->rxlen, &pending);
1767 brcmf_sdbrcm_sdlock(bus);
1769 memcpy(msg, bus->rxctl, min(msglen, rxlen));
1771 brcmf_sdbrcm_sdunlock(bus);
1774 BRCMF_CTL(("%s: resumed on rxctl frame, got %d expected %d\n",
1775 __func__, rxlen, msglen));
1776 } else if (timeleft == 0) {
1777 BRCMF_ERROR(("%s: resumed on timeout\n", __func__));
1779 brcmf_sdbrcm_sdlock(bus);
1780 brcmf_sdbrcm_checkdied(bus, NULL, 0);
1781 brcmf_sdbrcm_sdunlock(bus);
1783 } else if (pending == true) {
1784 BRCMF_CTL(("%s: cancelled\n", __func__));
1785 return -ERESTARTSYS;
1787 BRCMF_CTL(("%s: resumed for unknown reason?\n", __func__));
1789 brcmf_sdbrcm_sdlock(bus);
1790 brcmf_sdbrcm_checkdied(bus, NULL, 0);
1791 brcmf_sdbrcm_sdunlock(bus);
1796 bus->drvr->rx_ctlpkts++;
1798 bus->drvr->rx_ctlerrs++;
1800 return rxlen ? (int)rxlen : -ETIMEDOUT;
1842 const struct brcmu_iovar brcmf_sdio_iovars[] = {
1843 {"intr", IOV_INTR, 0, IOVT_BOOL, 0},
1844 {"sleep", IOV_SLEEP, 0, IOVT_BOOL, 0},
1845 {"pollrate", IOV_POLLRATE, 0, IOVT_UINT32, 0},
1846 {"idletime", IOV_IDLETIME, 0, IOVT_INT32, 0},
1847 {"idleclock", IOV_IDLECLOCK, 0, IOVT_INT32, 0},
1848 {"sd1idle", IOV_SD1IDLE, 0, IOVT_BOOL, 0},
1849 {"membytes", IOV_MEMBYTES, 0, IOVT_BUFFER, 2 * sizeof(int)},
1850 {"memsize", IOV_MEMSIZE, 0, IOVT_UINT32, 0},
1851 {"download", IOV_DOWNLOAD, 0, IOVT_BOOL, 0},
1852 {"vars", IOV_VARS, 0, IOVT_BUFFER, 0},
1853 {"sdiod_drive", IOV_SDIOD_DRIVE, 0, IOVT_UINT32, 0},
1854 {"readahead", IOV_READAHEAD, 0, IOVT_BOOL, 0},
1855 {"sdrxchain", IOV_SDRXCHAIN, 0, IOVT_BOOL, 0},
1856 {"alignctl", IOV_ALIGNCTL, 0, IOVT_BOOL, 0},
1857 {"sdalign", IOV_SDALIGN, 0, IOVT_BOOL, 0},
1858 {"devreset", IOV_DEVRESET, 0, IOVT_BOOL, 0},
1859 {"wdtick", IOV_WDTICK, 0, IOVT_UINT32, 0},
1861 {"cons", IOV_CONS, 0, IOVT_BUFFER, 0}
1863 {"dconpoll", IOV_DCONSOLE_POLL, 0, IOVT_UINT32, 0}
1865 {"sdreg", IOV_SDREG, 0, IOVT_BUFFER, sizeof(struct brcmf_sdreg)}
1867 {"sbreg", IOV_SBREG, 0, IOVT_BUFFER, sizeof(struct brcmf_sdreg)}
1869 {"sd_cis", IOV_SDCIS, 0, IOVT_BUFFER, BRCMF_IOCTL_MAXLEN}
1871 {"forcealign", IOV_FORCEEVEN, 0, IOVT_BOOL, 0}
1873 {"txbound", IOV_TXBOUND, 0, IOVT_UINT32, 0}
1875 {"rxbound", IOV_RXBOUND, 0, IOVT_UINT32, 0}
1877 {"txminmax", IOV_TXMINMAX, 0, IOVT_UINT32, 0}
1879 {"cpu", IOV_CPU, 0, IOVT_BOOL, 0}
1881 {"checkdied", IOV_CHECKDIED, 0, IOVT_BUFFER, 0}
1885 {"extloop", IOV_EXTLOOP, 0, IOVT_BOOL, 0}
1887 {"pktgen", IOV_PKTGEN, 0, IOVT_BUFFER, sizeof(struct brcmf_pktgen)}
1895 brcmf_dump_pct(struct brcmu_strbuf *strbuf, char *desc, uint num, uint div)
1900 brcmu_bprintf(strbuf, "%s N/A", desc);
1903 q2 = (100 * (num - (q1 * div))) / div;
1904 brcmu_bprintf(strbuf, "%s %d.%02d", desc, q1, q2);
1908 void brcmf_sdbrcm_bus_dump(struct brcmf_pub *drvr, struct brcmu_strbuf *strbuf)
1910 struct brcmf_bus *bus = drvr->bus;
1912 brcmu_bprintf(strbuf, "Bus SDIO structure:\n");
1913 brcmu_bprintf(strbuf,
1914 "hostintmask 0x%08x intstatus 0x%08x sdpcm_ver %d\n",
1915 bus->hostintmask, bus->intstatus, bus->sdpcm_ver);
1916 brcmu_bprintf(strbuf,
1917 "fcstate %d qlen %d tx_seq %d, max %d, rxskip %d rxlen %d rx_seq %d\n",
1918 bus->fcstate, pktq_len(&bus->txq), bus->tx_seq, bus->tx_max,
1919 bus->rxskip, bus->rxlen, bus->rx_seq);
1920 brcmu_bprintf(strbuf, "intr %d intrcount %d lastintrs %d spurious %d\n",
1921 bus->intr, bus->intrcount, bus->lastintrs, bus->spurious);
1922 brcmu_bprintf(strbuf, "pollrate %d pollcnt %d regfails %d\n",
1923 bus->pollrate, bus->pollcnt, bus->regfails);
1925 brcmu_bprintf(strbuf, "\nAdditional counters:\n");
1926 brcmu_bprintf(strbuf,
1927 "tx_sderrs %d fcqueued %d rxrtx %d rx_toolong %d rxc_errors %d\n",
1928 bus->tx_sderrs, bus->fcqueued, bus->rxrtx, bus->rx_toolong,
1930 brcmu_bprintf(strbuf, "rx_hdrfail %d badhdr %d badseq %d\n",
1931 bus->rx_hdrfail, bus->rx_badhdr, bus->rx_badseq);
1932 brcmu_bprintf(strbuf, "fc_rcvd %d, fc_xoff %d, fc_xon %d\n",
1933 bus->fc_rcvd, bus->fc_xoff, bus->fc_xon);
1934 brcmu_bprintf(strbuf, "rxglomfail %d, rxglomframes %d, rxglompkts %d\n",
1935 bus->rxglomfail, bus->rxglomframes, bus->rxglompkts);
1936 brcmu_bprintf(strbuf, "f2rx (hdrs/data) %d (%d/%d), f2tx %d f1regs"
1938 (bus->f2rxhdrs + bus->f2rxdata), bus->f2rxhdrs,
1939 bus->f2rxdata, bus->f2txdata, bus->f1regdata);
1941 brcmf_dump_pct(strbuf, "\nRx: pkts/f2rd", bus->drvr->rx_packets,
1942 (bus->f2rxhdrs + bus->f2rxdata));
1943 brcmf_dump_pct(strbuf, ", pkts/f1sd", bus->drvr->rx_packets,
1945 brcmf_dump_pct(strbuf, ", pkts/sd", bus->drvr->rx_packets,
1946 (bus->f2rxhdrs + bus->f2rxdata + bus->f1regdata));
1947 brcmf_dump_pct(strbuf, ", pkts/int", bus->drvr->rx_packets,
1949 brcmu_bprintf(strbuf, "\n");
1951 brcmf_dump_pct(strbuf, "Rx: glom pct", (100 * bus->rxglompkts),
1952 bus->drvr->rx_packets);
1953 brcmf_dump_pct(strbuf, ", pkts/glom", bus->rxglompkts,
1955 brcmu_bprintf(strbuf, "\n");
1957 brcmf_dump_pct(strbuf, "Tx: pkts/f2wr", bus->drvr->tx_packets,
1959 brcmf_dump_pct(strbuf, ", pkts/f1sd", bus->drvr->tx_packets,
1961 brcmf_dump_pct(strbuf, ", pkts/sd", bus->drvr->tx_packets,
1962 (bus->f2txdata + bus->f1regdata));
1963 brcmf_dump_pct(strbuf, ", pkts/int", bus->drvr->tx_packets,
1965 brcmu_bprintf(strbuf, "\n");
1967 brcmf_dump_pct(strbuf, "Total: pkts/f2rw",
1968 (bus->drvr->tx_packets + bus->drvr->rx_packets),
1969 (bus->f2txdata + bus->f2rxhdrs + bus->f2rxdata));
1970 brcmf_dump_pct(strbuf, ", pkts/f1sd",
1971 (bus->drvr->tx_packets + bus->drvr->rx_packets),
1973 brcmf_dump_pct(strbuf, ", pkts/sd",
1974 (bus->drvr->tx_packets + bus->drvr->rx_packets),
1975 (bus->f2txdata + bus->f2rxhdrs + bus->f2rxdata +
1977 brcmf_dump_pct(strbuf, ", pkts/int",
1978 (bus->drvr->tx_packets + bus->drvr->rx_packets),
1980 brcmu_bprintf(strbuf, "\n\n");
1984 if (bus->pktgen_count) {
1985 brcmu_bprintf(strbuf, "pktgen config and count:\n");
1986 brcmu_bprintf(strbuf,
1987 "freq %d count %d print %d total %d min %d len %d\n",
1988 bus->pktgen_freq, bus->pktgen_count,
1989 bus->pktgen_print, bus->pktgen_total,
1990 bus->pktgen_minlen, bus->pktgen_maxlen);
1991 brcmu_bprintf(strbuf, "send attempts %d rcvd %d fail %d\n",
1992 bus->pktgen_sent, bus->pktgen_rcvd,
1997 brcmu_bprintf(strbuf, "dpc_sched %d host interrupt%spending\n",
1998 bus->dpc_sched, " not ");
1999 brcmu_bprintf(strbuf, "blocksize %d roundup %d\n", bus->blocksize,
2002 brcmu_bprintf(strbuf,
2003 "clkstate %d activity %d idletime %d idlecount %d sleeping %d\n",
2004 bus->clkstate, bus->activity, bus->idletime, bus->idlecount,
2008 void brcmf_bus_clearcounts(struct brcmf_pub *drvr)
2010 struct brcmf_bus *bus = (struct brcmf_bus *) drvr->bus;
2012 bus->intrcount = bus->lastintrs = bus->spurious = bus->regfails = 0;
2013 bus->rxrtx = bus->rx_toolong = bus->rxc_errors = 0;
2014 bus->rx_hdrfail = bus->rx_badhdr = bus->rx_badseq = 0;
2015 bus->tx_sderrs = bus->fc_rcvd = bus->fc_xoff = bus->fc_xon = 0;
2016 bus->rxglomfail = bus->rxglomframes = bus->rxglompkts = 0;
2017 bus->f2rxhdrs = bus->f2rxdata = bus->f2txdata = bus->f1regdata = 0;
2021 static int brcmf_sdbrcm_pktgen_get(struct brcmf_bus *bus, u8 *arg)
2023 struct brcmf_pktgen pktgen;
2025 pktgen.version = BRCMF_PKTGEN_VERSION;
2026 pktgen.freq = bus->pktgen_freq;
2027 pktgen.count = bus->pktgen_count;
2028 pktgen.print = bus->pktgen_print;
2029 pktgen.total = bus->pktgen_total;
2030 pktgen.minlen = bus->pktgen_minlen;
2031 pktgen.maxlen = bus->pktgen_maxlen;
2032 pktgen.numsent = bus->pktgen_sent;
2033 pktgen.numrcvd = bus->pktgen_rcvd;
2034 pktgen.numfail = bus->pktgen_fail;
2035 pktgen.mode = bus->pktgen_mode;
2036 pktgen.stop = bus->pktgen_stop;
2038 memcpy(arg, &pktgen, sizeof(pktgen));
2043 static int brcmf_sdbrcm_pktgen_set(struct brcmf_bus *bus, u8 *arg)
2045 struct brcmf_pktgen pktgen;
2046 uint oldcnt, oldmode;
2048 memcpy(&pktgen, arg, sizeof(pktgen));
2049 if (pktgen.version != BRCMF_PKTGEN_VERSION)
2052 oldcnt = bus->pktgen_count;
2053 oldmode = bus->pktgen_mode;
2055 bus->pktgen_freq = pktgen.freq;
2056 bus->pktgen_count = pktgen.count;
2057 bus->pktgen_print = pktgen.print;
2058 bus->pktgen_total = pktgen.total;
2059 bus->pktgen_minlen = pktgen.minlen;
2060 bus->pktgen_maxlen = pktgen.maxlen;
2061 bus->pktgen_mode = pktgen.mode;
2062 bus->pktgen_stop = pktgen.stop;
2064 bus->pktgen_tick = bus->pktgen_ptick = 0;
2065 bus->pktgen_len = max(bus->pktgen_len, bus->pktgen_minlen);
2066 bus->pktgen_len = min(bus->pktgen_len, bus->pktgen_maxlen);
2068 /* Clear counts for a new pktgen (mode change, or was stopped) */
2069 if (bus->pktgen_count && (!oldcnt || oldmode != bus->pktgen_mode))
2070 bus->pktgen_sent = bus->pktgen_rcvd = bus->pktgen_fail = 0;
2077 brcmf_sdbrcm_membytes(struct brcmf_bus *bus, bool write, u32 address, u8 *data,
2084 /* Determine initial transfer parameters */
2085 sdaddr = address & SBSDIO_SB_OFT_ADDR_MASK;
2086 if ((sdaddr + size) & SBSDIO_SBWINDOW_MASK)
2087 dsize = (SBSDIO_SB_OFT_ADDR_LIMIT - sdaddr);
2091 /* Set the backplane window to include the start address */
2092 bcmerror = brcmf_sdbrcm_set_siaddr_window(bus, address);
2094 BRCMF_ERROR(("%s: window change failed\n", __func__));
2098 /* Do the transfer(s) */
2100 BRCMF_INFO(("%s: %s %d bytes at offset 0x%08x in window"
2101 " 0x%08x\n", __func__, (write ? "write" : "read"),
2102 dsize, sdaddr, (address & SBSDIO_SBWINDOW_MASK)));
2104 brcmf_sdcard_rwdata(bus->card, write, sdaddr, data, dsize);
2106 BRCMF_ERROR(("%s: membytes transfer failed\n",
2111 /* Adjust for next transfer (if any) */
2116 bcmerror = brcmf_sdbrcm_set_siaddr_window(bus, address);
2118 BRCMF_ERROR(("%s: window change failed\n",
2123 dsize = min_t(uint, SBSDIO_SB_OFT_ADDR_LIMIT, size);
2128 /* Return the window to backplane enumeration space for core access */
2129 if (brcmf_sdbrcm_set_siaddr_window(bus,
2130 brcmf_sdcard_cur_sbwad(bus->card))) {
2131 BRCMF_ERROR(("%s: FAILED to set window back to 0x%x\n",
2132 __func__, brcmf_sdcard_cur_sbwad(bus->card)));
2139 static int brcmf_sdbrcm_readshared(struct brcmf_bus *bus, struct sdpcm_shared *sh)
2144 /* Read last word in memory to determine address of
2145 sdpcm_shared structure */
2146 rv = brcmf_sdbrcm_membytes(bus, false, bus->ramsize - 4, (u8 *)&addr,
2151 addr = le32_to_cpu(addr);
2153 BRCMF_INFO(("sdpcm_shared address 0x%08X\n", addr));
2156 * Check if addr is valid.
2157 * NVRAM length at the end of memory should have been overwritten.
2159 if (addr == 0 || ((~addr >> 16) & 0xffff) == (addr & 0xffff)) {
2160 BRCMF_ERROR(("%s: address (0x%08x) of sdpcm_shared invalid\n",
2165 /* Read rte_shared structure */
2166 rv = brcmf_sdbrcm_membytes(bus, false, addr, (u8 *) sh,
2167 sizeof(struct sdpcm_shared));
2172 sh->flags = le32_to_cpu(sh->flags);
2173 sh->trap_addr = le32_to_cpu(sh->trap_addr);
2174 sh->assert_exp_addr = le32_to_cpu(sh->assert_exp_addr);
2175 sh->assert_file_addr = le32_to_cpu(sh->assert_file_addr);
2176 sh->assert_line = le32_to_cpu(sh->assert_line);
2177 sh->console_addr = le32_to_cpu(sh->console_addr);
2178 sh->msgtrace_addr = le32_to_cpu(sh->msgtrace_addr);
2180 if ((sh->flags & SDPCM_SHARED_VERSION_MASK) != SDPCM_SHARED_VERSION) {
2181 BRCMF_ERROR(("%s: sdpcm_shared version %d in brcmf "
2182 "is different than sdpcm_shared version %d in dongle\n",
2183 __func__, SDPCM_SHARED_VERSION,
2184 sh->flags & SDPCM_SHARED_VERSION_MASK));
2191 static int brcmf_sdbrcm_checkdied(struct brcmf_bus *bus, u8 *data, uint size)
2195 char *mbuffer = NULL;
2196 uint maxstrlen = 256;
2198 struct brcmf_trap tr;
2199 struct sdpcm_shared sdpcm_shared;
2200 struct brcmu_strbuf strbuf;
2202 BRCMF_TRACE(("%s: Enter\n", __func__));
2206 * Called after a rx ctrl timeout. "data" is NULL.
2207 * allocate memory to trace the trap or assert.
2210 mbuffer = data = kmalloc(msize, GFP_ATOMIC);
2211 if (mbuffer == NULL) {
2212 BRCMF_ERROR(("%s: kmalloc(%d) failed\n", __func__,
2219 str = kmalloc(maxstrlen, GFP_ATOMIC);
2221 BRCMF_ERROR(("%s: kmalloc(%d) failed\n", __func__, maxstrlen));
2226 bcmerror = brcmf_sdbrcm_readshared(bus, &sdpcm_shared);
2230 brcmu_binit(&strbuf, data, size);
2232 brcmu_bprintf(&strbuf,
2233 "msgtrace address : 0x%08X\nconsole address : 0x%08X\n",
2234 sdpcm_shared.msgtrace_addr, sdpcm_shared.console_addr);
2236 if ((sdpcm_shared.flags & SDPCM_SHARED_ASSERT_BUILT) == 0) {
2237 /* NOTE: Misspelled assert is intentional - DO NOT FIX.
2238 * (Avoids conflict with real asserts for programmatic
2239 * parsing of output.)
2241 brcmu_bprintf(&strbuf, "Assrt not built in dongle\n");
2244 if ((sdpcm_shared.flags & (SDPCM_SHARED_ASSERT | SDPCM_SHARED_TRAP)) ==
2246 /* NOTE: Misspelled assert is intentional - DO NOT FIX.
2247 * (Avoids conflict with real asserts for programmatic
2248 * parsing of output.)
2250 brcmu_bprintf(&strbuf, "No trap%s in dongle",
2251 (sdpcm_shared.flags & SDPCM_SHARED_ASSERT_BUILT)
2254 if (sdpcm_shared.flags & SDPCM_SHARED_ASSERT) {
2255 /* Download assert */
2256 brcmu_bprintf(&strbuf, "Dongle assert");
2257 if (sdpcm_shared.assert_exp_addr != 0) {
2259 bcmerror = brcmf_sdbrcm_membytes(bus, false,
2260 sdpcm_shared.assert_exp_addr,
2261 (u8 *) str, maxstrlen);
2265 str[maxstrlen - 1] = '\0';
2266 brcmu_bprintf(&strbuf, " expr \"%s\"", str);
2269 if (sdpcm_shared.assert_file_addr != 0) {
2271 bcmerror = brcmf_sdbrcm_membytes(bus, false,
2272 sdpcm_shared.assert_file_addr,
2273 (u8 *) str, maxstrlen);
2277 str[maxstrlen - 1] = '\0';
2278 brcmu_bprintf(&strbuf, " file \"%s\"", str);
2281 brcmu_bprintf(&strbuf, " line %d ",
2282 sdpcm_shared.assert_line);
2285 if (sdpcm_shared.flags & SDPCM_SHARED_TRAP) {
2286 bcmerror = brcmf_sdbrcm_membytes(bus, false,
2287 sdpcm_shared.trap_addr, (u8 *)&tr,
2288 sizeof(struct brcmf_trap));
2292 brcmu_bprintf(&strbuf,
2293 "Dongle trap type 0x%x @ epc 0x%x, cpsr 0x%x, spsr 0x%x, sp 0x%x,"
2294 "lp 0x%x, rpc 0x%x Trap offset 0x%x, "
2295 "r0 0x%x, r1 0x%x, r2 0x%x, r3 0x%x, r4 0x%x, r5 0x%x, r6 0x%x, r7 0x%x\n",
2296 tr.type, tr.epc, tr.cpsr, tr.spsr, tr.r13,
2297 tr.r14, tr.pc, sdpcm_shared.trap_addr,
2298 tr.r0, tr.r1, tr.r2, tr.r3, tr.r4, tr.r5,
2303 if (sdpcm_shared.flags & (SDPCM_SHARED_ASSERT | SDPCM_SHARED_TRAP))
2304 BRCMF_ERROR(("%s: %s\n", __func__, strbuf.origbuf));
2307 if (sdpcm_shared.flags & SDPCM_SHARED_TRAP) {
2308 /* Mem dump to a file on device */
2309 brcmf_sdbrcm_mem_dump(bus);
2320 static int brcmf_sdbrcm_mem_dump(struct brcmf_bus *bus)
2323 int size; /* Full mem size */
2324 int start = 0; /* Start address */
2325 int read_size = 0; /* Read size of each iteration */
2326 u8 *buf = NULL, *databuf = NULL;
2328 /* Get full mem size */
2329 size = bus->ramsize;
2330 buf = kmalloc(size, GFP_ATOMIC);
2332 BRCMF_ERROR(("%s: Out of memory (%d bytes)\n", __func__, size));
2336 /* Read mem content */
2337 printk(KERN_DEBUG "Dump dongle memory");
2340 read_size = min(MEMBLOCK, size);
2341 ret = brcmf_sdbrcm_membytes(bus, false, start, databuf,
2344 BRCMF_ERROR(("%s: Error membytes %d\n", __func__, ret));
2350 /* Decrement size and increment start address */
2353 databuf += read_size;
2355 printk(KERN_DEBUG "Done\n");
2357 /* free buf before return !!! */
2358 if (brcmf_write_to_file(bus->drvr, buf, bus->ramsize)) {
2359 BRCMF_ERROR(("%s: Error writing to files\n", __func__));
2363 /* buf free handled in brcmf_write_to_file, not here */
2367 #define CONSOLE_LINE_MAX 192
2369 static int brcmf_sdbrcm_readconsole(struct brcmf_bus *bus)
2371 struct brcmf_console *c = &bus->console;
2372 u8 line[CONSOLE_LINE_MAX], ch;
2376 /* Don't do anything until FWREADY updates console address */
2377 if (bus->console_addr == 0)
2380 /* Read console log struct */
2381 addr = bus->console_addr + offsetof(struct rte_console, log);
2382 rv = brcmf_sdbrcm_membytes(bus, false, addr, (u8 *)&c->log,
2387 /* Allocate console buffer (one time only) */
2388 if (c->buf == NULL) {
2389 c->bufsize = le32_to_cpu(c->log.buf_size);
2390 c->buf = kmalloc(c->bufsize, GFP_ATOMIC);
2395 idx = le32_to_cpu(c->log.idx);
2397 /* Protect against corrupt value */
2398 if (idx > c->bufsize)
2401 /* Skip reading the console buffer if the index pointer
2406 /* Read the console buffer */
2407 addr = le32_to_cpu(c->log.buf);
2408 rv = brcmf_sdbrcm_membytes(bus, false, addr, c->buf, c->bufsize);
2412 while (c->last != idx) {
2413 for (n = 0; n < CONSOLE_LINE_MAX - 2; n++) {
2414 if (c->last == idx) {
2415 /* This would output a partial line.
2417 * the buffer pointer and output this
2418 * line next time around.
2423 c->last = c->bufsize - n;
2426 ch = c->buf[c->last];
2427 c->last = (c->last + 1) % c->bufsize;
2434 if (line[n - 1] == '\r')
2437 printk(KERN_DEBUG "CONSOLE: %s\n", line);
2446 int brcmf_sdbrcm_downloadvars(struct brcmf_bus *bus, void *arg, int len)
2450 BRCMF_TRACE(("%s: Enter\n", __func__));
2452 /* Basic sanity checks */
2453 if (bus->drvr->up) {
2454 bcmerror = -EISCONN;
2458 bcmerror = -EOVERFLOW;
2462 /* Free the old ones and replace with passed variables */
2465 bus->vars = kmalloc(len, GFP_ATOMIC);
2466 bus->varsz = bus->vars ? len : 0;
2467 if (bus->vars == NULL) {
2472 /* Copy the passed variables, which should include the
2473 terminating double-null */
2474 memcpy(bus->vars, arg, bus->varsz);
2480 brcmf_sdbrcm_doiovar(struct brcmf_bus *bus, const struct brcmu_iovar *vi, u32 actionid,
2481 const char *name, void *params, int plen, void *arg, int len,
2488 BRCMF_TRACE(("%s: Enter, action %d name %s params %p plen %d arg %p "
2489 "len %d val_size %d\n", __func__, actionid, name, params,
2490 plen, arg, len, val_size));
2492 bcmerror = brcmu_iovar_lencheck(vi, arg, len, IOV_ISSET(actionid));
2496 if (plen >= (int)sizeof(int_val))
2497 memcpy(&int_val, params, sizeof(int_val));
2499 bool_val = (int_val != 0) ? true : false;
2501 /* Some ioctls use the bus */
2502 brcmf_sdbrcm_sdlock(bus);
2504 /* Check if dongle is in reset. If so, only allow DEVRESET iovars */
2505 if (bus->drvr->dongle_reset && !(actionid == IOV_SVAL(IOV_DEVRESET) ||
2506 actionid == IOV_GVAL(IOV_DEVRESET))) {
2511 /* Handle sleep stuff before any clock mucking */
2512 if (vi->varid == IOV_SLEEP) {
2513 if (IOV_ISSET(actionid)) {
2514 bcmerror = brcmf_sdbrcm_bussleep(bus, bool_val);
2516 int_val = (s32) bus->sleeping;
2517 memcpy(arg, &int_val, val_size);
2522 /* Request clock to allow SDIO accesses */
2523 if (!bus->drvr->dongle_reset) {
2525 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
2529 case IOV_GVAL(IOV_INTR):
2530 int_val = (s32) bus->intr;
2531 memcpy(arg, &int_val, val_size);
2534 case IOV_SVAL(IOV_INTR):
2535 bus->intr = bool_val;
2536 bus->intdis = false;
2537 if (bus->drvr->up) {
2538 BRCMF_INTR(("%s: %s SDIO interrupts\n", __func__,
2539 bus->intr ? "enable" : "disable"));
2541 brcmf_sdcard_intr_enable(bus->card);
2543 brcmf_sdcard_intr_disable(bus->card);
2548 case IOV_GVAL(IOV_POLLRATE):
2549 int_val = (s32) bus->pollrate;
2550 memcpy(arg, &int_val, val_size);
2553 case IOV_SVAL(IOV_POLLRATE):
2554 bus->pollrate = (uint) int_val;
2555 bus->poll = (bus->pollrate != 0);
2558 case IOV_GVAL(IOV_IDLETIME):
2559 int_val = bus->idletime;
2560 memcpy(arg, &int_val, val_size);
2563 case IOV_SVAL(IOV_IDLETIME):
2564 if ((int_val < 0) && (int_val != BRCMF_IDLE_IMMEDIATE))
2567 bus->idletime = int_val;
2570 case IOV_GVAL(IOV_IDLECLOCK):
2571 int_val = (s32) bus->idleclock;
2572 memcpy(arg, &int_val, val_size);
2575 case IOV_SVAL(IOV_IDLECLOCK):
2576 bus->idleclock = int_val;
2579 case IOV_GVAL(IOV_SD1IDLE):
2580 int_val = (s32) sd1idle;
2581 memcpy(arg, &int_val, val_size);
2584 case IOV_SVAL(IOV_SD1IDLE):
2588 case IOV_SVAL(IOV_MEMBYTES):
2589 case IOV_GVAL(IOV_MEMBYTES):
2595 bool set = (actionid == IOV_SVAL(IOV_MEMBYTES));
2597 address = (u32) int_val;
2598 memcpy(&int_val, (char *)params + sizeof(int_val),
2600 size = (uint) int_val;
2602 /* Do some validation */
2603 dsize = set ? plen - (2 * sizeof(int)) : len;
2605 BRCMF_ERROR(("%s: error on %s membytes, addr "
2606 "0x%08x size %d dsize %d\n",
2607 __func__, (set ? "set" : "get"),
2608 address, size, dsize));
2613 BRCMF_INFO(("%s: Request to %s %d bytes at address "
2614 "0x%08x\n", __func__,
2615 (set ? "write" : "read"), size, address));
2617 /* If we know about SOCRAM, check for a fit */
2618 if ((bus->orig_ramsize) &&
2619 ((address > bus->orig_ramsize)
2620 || (address + size > bus->orig_ramsize))) {
2621 BRCMF_ERROR(("%s: ramsize 0x%08x doesn't have"
2622 " %d bytes at 0x%08x\n", __func__,
2623 bus->orig_ramsize, size, address));
2628 /* Generate the actual data pointer */
2630 set ? (u8 *) params +
2631 2 * sizeof(int) : (u8 *) arg;
2633 /* Call to do the transfer */
2634 bcmerror = brcmf_sdbrcm_membytes(bus, set, address,
2640 case IOV_GVAL(IOV_MEMSIZE):
2641 int_val = (s32) bus->ramsize;
2642 memcpy(arg, &int_val, val_size);
2645 case IOV_GVAL(IOV_SDIOD_DRIVE):
2646 int_val = (s32) brcmf_sdiod_drive_strength;
2647 memcpy(arg, &int_val, val_size);
2650 case IOV_SVAL(IOV_SDIOD_DRIVE):
2651 brcmf_sdiod_drive_strength = int_val;
2652 brcmf_sdbrcm_sdiod_drive_strength_init(bus,
2653 brcmf_sdiod_drive_strength);
2656 case IOV_SVAL(IOV_DOWNLOAD):
2657 bcmerror = brcmf_sdbrcm_download_state(bus, bool_val);
2660 case IOV_SVAL(IOV_VARS):
2661 bcmerror = brcmf_sdbrcm_downloadvars(bus, arg, len);
2664 case IOV_GVAL(IOV_READAHEAD):
2665 int_val = (s32) brcmf_readahead;
2666 memcpy(arg, &int_val, val_size);
2669 case IOV_SVAL(IOV_READAHEAD):
2670 if (bool_val && !brcmf_readahead)
2672 brcmf_readahead = bool_val;
2675 case IOV_GVAL(IOV_SDRXCHAIN):
2676 int_val = (s32) bus->use_rxchain;
2677 memcpy(arg, &int_val, val_size);
2680 case IOV_SVAL(IOV_SDRXCHAIN):
2681 if (bool_val && !bus->sd_rxchain)
2682 bcmerror = -ENOTSUPP;
2684 bus->use_rxchain = bool_val;
2686 case IOV_GVAL(IOV_ALIGNCTL):
2687 int_val = (s32) brcmf_alignctl;
2688 memcpy(arg, &int_val, val_size);
2691 case IOV_SVAL(IOV_ALIGNCTL):
2692 brcmf_alignctl = bool_val;
2695 case IOV_GVAL(IOV_SDALIGN):
2696 int_val = BRCMF_SDALIGN;
2697 memcpy(arg, &int_val, val_size);
2701 case IOV_GVAL(IOV_VARS):
2702 if (bus->varsz < (uint) len)
2703 memcpy(arg, bus->vars, bus->varsz);
2705 bcmerror = -EOVERFLOW;
2710 case IOV_GVAL(IOV_DCONSOLE_POLL):
2711 int_val = (s32) brcmf_console_ms;
2712 memcpy(arg, &int_val, val_size);
2715 case IOV_SVAL(IOV_DCONSOLE_POLL):
2716 brcmf_console_ms = (uint) int_val;
2719 case IOV_SVAL(IOV_CONS):
2721 bcmerror = brcmf_sdbrcm_bus_console_in(bus->drvr,
2725 case IOV_GVAL(IOV_SDREG):
2727 struct brcmf_sdreg *sd_ptr;
2730 sd_ptr = (struct brcmf_sdreg *) params;
2732 addr = bus->ci->buscorebase + sd_ptr->offset;
2733 size = sd_ptr->func;
2734 int_val = (s32) brcmf_sdcard_reg_read(bus->card, addr,
2736 if (brcmf_sdcard_regfail(bus->card))
2738 memcpy(arg, &int_val, sizeof(s32));
2742 case IOV_SVAL(IOV_SDREG):
2744 struct brcmf_sdreg *sd_ptr;
2747 sd_ptr = (struct brcmf_sdreg *) params;
2749 addr = bus->ci->buscorebase + sd_ptr->offset;
2750 size = sd_ptr->func;
2751 brcmf_sdcard_reg_write(bus->card, addr, size,
2753 if (brcmf_sdcard_regfail(bus->card))
2758 /* Same as above, but offset is not backplane
2760 case IOV_GVAL(IOV_SBREG):
2762 struct brcmf_sdreg sdreg;
2765 memcpy(&sdreg, params, sizeof(sdreg));
2767 addr = SI_ENUM_BASE + sdreg.offset;
2769 int_val = (s32) brcmf_sdcard_reg_read(bus->card, addr,
2771 if (brcmf_sdcard_regfail(bus->card))
2773 memcpy(arg, &int_val, sizeof(s32));
2777 case IOV_SVAL(IOV_SBREG):
2779 struct brcmf_sdreg sdreg;
2782 memcpy(&sdreg, params, sizeof(sdreg));
2784 addr = SI_ENUM_BASE + sdreg.offset;
2786 brcmf_sdcard_reg_write(bus->card, addr, size,
2788 if (brcmf_sdcard_regfail(bus->card))
2793 case IOV_GVAL(IOV_SDCIS):
2797 strcat(arg, "\nFunc 0\n");
2798 brcmf_sdcard_cis_read(bus->card, 0x10,
2799 (u8 *) arg + strlen(arg),
2800 SBSDIO_CIS_SIZE_LIMIT);
2801 strcat(arg, "\nFunc 1\n");
2802 brcmf_sdcard_cis_read(bus->card, 0x11,
2803 (u8 *) arg + strlen(arg),
2804 SBSDIO_CIS_SIZE_LIMIT);
2805 strcat(arg, "\nFunc 2\n");
2806 brcmf_sdcard_cis_read(bus->card, 0x12,
2807 (u8 *) arg + strlen(arg),
2808 SBSDIO_CIS_SIZE_LIMIT);
2812 case IOV_GVAL(IOV_FORCEEVEN):
2813 int_val = (s32) forcealign;
2814 memcpy(arg, &int_val, val_size);
2817 case IOV_SVAL(IOV_FORCEEVEN):
2818 forcealign = bool_val;
2821 case IOV_GVAL(IOV_TXBOUND):
2822 int_val = (s32) brcmf_txbound;
2823 memcpy(arg, &int_val, val_size);
2826 case IOV_SVAL(IOV_TXBOUND):
2827 brcmf_txbound = (uint) int_val;
2830 case IOV_GVAL(IOV_RXBOUND):
2831 int_val = (s32) brcmf_rxbound;
2832 memcpy(arg, &int_val, val_size);
2835 case IOV_SVAL(IOV_RXBOUND):
2836 brcmf_rxbound = (uint) int_val;
2839 case IOV_GVAL(IOV_TXMINMAX):
2840 int_val = (s32) brcmf_txminmax;
2841 memcpy(arg, &int_val, val_size);
2844 case IOV_SVAL(IOV_TXMINMAX):
2845 brcmf_txminmax = (uint) int_val;
2850 case IOV_GVAL(IOV_EXTLOOP):
2851 int_val = (s32) bus->ext_loop;
2852 memcpy(arg, &int_val, val_size);
2855 case IOV_SVAL(IOV_EXTLOOP):
2856 bus->ext_loop = bool_val;
2859 case IOV_GVAL(IOV_PKTGEN):
2860 bcmerror = brcmf_sdbrcm_pktgen_get(bus, arg);
2863 case IOV_SVAL(IOV_PKTGEN):
2864 bcmerror = brcmf_sdbrcm_pktgen_set(bus, arg);
2868 case IOV_SVAL(IOV_DEVRESET):
2869 BRCMF_TRACE(("%s: Called set IOV_DEVRESET=%d dongle_reset=%d "
2871 __func__, bool_val, bus->drvr->dongle_reset,
2872 bus->drvr->busstate));
2874 brcmf_bus_devreset(bus->drvr, (u8) bool_val);
2878 case IOV_GVAL(IOV_DEVRESET):
2879 BRCMF_TRACE(("%s: Called get IOV_DEVRESET\n", __func__));
2881 /* Get its status */
2882 int_val = (bool) bus->drvr->dongle_reset;
2883 memcpy(arg, &int_val, val_size);
2887 case IOV_GVAL(IOV_WDTICK):
2888 int_val = (s32) brcmf_watchdog_ms;
2889 memcpy(arg, &int_val, val_size);
2892 case IOV_SVAL(IOV_WDTICK):
2893 if (!bus->drvr->up) {
2894 bcmerror = -ENOLINK;
2897 brcmf_sdbrcm_wd_timer(bus, (uint) int_val);
2901 bcmerror = -ENOTSUPP;
2906 if ((bus->idletime == BRCMF_IDLE_IMMEDIATE) && !bus->dpc_sched) {
2907 bus->activity = false;
2908 brcmf_sdbrcm_clkctl(bus, CLK_NONE, true);
2911 brcmf_sdbrcm_sdunlock(bus);
2913 if (actionid == IOV_SVAL(IOV_DEVRESET) && bool_val == false)
2914 brcmf_c_preinit_ioctls(bus->drvr);
2919 static int brcmf_sdbrcm_write_vars(struct brcmf_bus *bus)
2927 char *nvram_ularray;
2930 /* Even if there are no vars are to be written, we still
2931 need to set the ramsize. */
2932 varsize = bus->varsz ? roundup(bus->varsz, 4) : 0;
2933 varaddr = (bus->ramsize - 4) - varsize;
2936 vbuffer = kzalloc(varsize, GFP_ATOMIC);
2940 memcpy(vbuffer, bus->vars, bus->varsz);
2942 /* Write the vars list */
2944 brcmf_sdbrcm_membytes(bus, true, varaddr, vbuffer, varsize);
2946 /* Verify NVRAM bytes */
2947 BRCMF_INFO(("Compare NVRAM dl & ul; varsize=%d\n", varsize));
2948 nvram_ularray = kmalloc(varsize, GFP_ATOMIC);
2952 /* Upload image to verify downloaded contents. */
2953 memset(nvram_ularray, 0xaa, varsize);
2955 /* Read the vars list to temp buffer for comparison */
2957 brcmf_sdbrcm_membytes(bus, false, varaddr, nvram_ularray,
2960 BRCMF_ERROR(("%s: error %d on reading %d nvram bytes"
2961 " at 0x%08x\n", __func__, bcmerror,
2964 /* Compare the org NVRAM with the one read from RAM */
2965 if (memcmp(vbuffer, nvram_ularray, varsize)) {
2966 BRCMF_ERROR(("%s: Downloaded NVRAM image is "
2967 "corrupted.\n", __func__));
2969 BRCMF_ERROR(("%s: Download/Upload/Compare of"
2970 " NVRAM ok.\n", __func__));
2972 kfree(nvram_ularray);
2978 /* adjust to the user specified RAM */
2979 BRCMF_INFO(("Physical memory size: %d, usable memory size: %d\n",
2980 bus->orig_ramsize, bus->ramsize));
2981 BRCMF_INFO(("Vars are at %d, orig varsize is %d\n", varaddr, varsize));
2982 varsize = ((bus->orig_ramsize - 4) - varaddr);
2985 * Determine the length token:
2986 * Varsize, converted to words, in lower 16-bits, checksum
2992 varsizew = varsize / 4;
2993 varsizew = (~varsizew << 16) | (varsizew & 0x0000FFFF);
2994 varsizew = cpu_to_le32(varsizew);
2997 BRCMF_INFO(("New varsize is %d, length token=0x%08x\n", varsize,
3000 /* Write the length token to the last word */
3001 bcmerror = brcmf_sdbrcm_membytes(bus, true, (bus->orig_ramsize - 4),
3002 (u8 *)&varsizew, 4);
3007 static int brcmf_sdbrcm_download_state(struct brcmf_bus *bus, bool enter)
3013 /* To enter download state, disable ARM and reset SOCRAM.
3014 * To exit download state, simply reset ARM (default is RAM boot).
3017 bus->alp_only = true;
3019 brcmf_sdbrcm_chip_disablecore(bus->card, bus->ci->armcorebase);
3021 brcmf_sdbrcm_chip_resetcore(bus->card, bus->ci->ramcorebase);
3023 /* Clear the top bit of memory */
3026 brcmf_sdbrcm_membytes(bus, true, bus->ramsize - 4,
3030 regdata = brcmf_sdcard_reg_read(bus->card,
3031 CORE_SB(bus->ci->ramcorebase, sbtmstatelow), 4);
3032 regdata &= (SBTML_RESET | SBTML_REJ_MASK |
3033 (SICF_CLOCK_EN << SBTML_SICF_SHIFT));
3034 if ((SICF_CLOCK_EN << SBTML_SICF_SHIFT) != regdata) {
3035 BRCMF_ERROR(("%s: SOCRAM core is down after reset?\n",
3041 bcmerror = brcmf_sdbrcm_write_vars(bus);
3043 BRCMF_ERROR(("%s: no vars written to RAM\n", __func__));
3047 w_sdreg32(bus, 0xFFFFFFFF,
3048 offsetof(struct sdpcmd_regs, intstatus), &retries);
3050 brcmf_sdbrcm_chip_resetcore(bus->card, bus->ci->armcorebase);
3052 /* Allow HT Clock now that the ARM is running. */
3053 bus->alp_only = false;
3055 bus->drvr->busstate = BRCMF_BUS_LOAD;
3062 brcmf_sdbrcm_bus_iovar_op(struct brcmf_pub *drvr, const char *name,
3063 void *params, int plen, void *arg, int len, bool set)
3065 struct brcmf_bus *bus = drvr->bus;
3066 const struct brcmu_iovar *vi = NULL;
3071 BRCMF_TRACE(("%s: Enter\n", __func__));
3073 if (name == NULL || len <= 0)
3076 /* Set does not take qualifiers */
3077 if (set && (params || plen))
3080 /* Get must have return space;*/
3081 if (!set && !(arg && len))
3084 /* Look up var locally; if not found pass to host driver */
3085 vi = brcmu_iovar_lookup(brcmf_sdio_iovars, name);
3087 brcmf_sdbrcm_sdlock(bus);
3091 /* Turn on clock in case SD command needs backplane */
3092 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
3094 bcmerror = brcmf_sdcard_iovar_op(bus->card, name, params, plen,
3097 /* Similar check for blocksize change */
3098 if (set && strcmp(name, "sd_blocksize") == 0) {
3100 if (brcmf_sdcard_iovar_op
3101 (bus->card, "sd_blocksize", &fnum, sizeof(s32),
3102 &bus->blocksize, sizeof(s32),
3105 BRCMF_ERROR(("%s: fail on %s get\n", __func__,
3108 BRCMF_INFO(("%s: noted sd_blocksize update,"
3109 " value now %d\n", __func__,
3113 bus->roundup = min(max_roundup, bus->blocksize);
3115 if (bus->idletime == BRCMF_IDLE_IMMEDIATE &&
3117 bus->activity = false;
3118 brcmf_sdbrcm_clkctl(bus, CLK_NONE, true);
3121 brcmf_sdbrcm_sdunlock(bus);
3125 BRCMF_CTL(("%s: %s %s, len %d plen %d\n", __func__,
3126 name, (set ? "set" : "get"), len, plen));
3128 /* set up 'params' pointer in case this is a set command so that
3129 * the convenience int and bool code can be common to set and get
3131 if (params == NULL) {
3136 if (vi->type == IOVT_VOID)
3138 else if (vi->type == IOVT_BUFFER)
3141 /* all other types are integer sized */
3142 val_size = sizeof(int);
3144 actionid = set ? IOV_SVAL(vi->varid) : IOV_GVAL(vi->varid);
3145 bcmerror = brcmf_sdbrcm_doiovar(bus, vi, actionid, name, params, plen,
3146 arg, len, val_size);
3152 void brcmf_sdbrcm_bus_stop(struct brcmf_bus *bus, bool enforce_mutex)
3154 u32 local_hostintmask;
3159 BRCMF_TRACE(("%s: Enter\n", __func__));
3162 brcmf_sdbrcm_sdlock(bus);
3166 /* Enable clock for device interrupts */
3167 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
3169 if (bus->watchdog_tsk) {
3170 send_sig(SIGTERM, bus->watchdog_tsk, 1);
3171 kthread_stop(bus->watchdog_tsk);
3172 bus->watchdog_tsk = NULL;
3176 send_sig(SIGTERM, bus->dpc_tsk, 1);
3177 kthread_stop(bus->dpc_tsk);
3178 bus->dpc_tsk = NULL;
3180 tasklet_kill(&bus->tasklet);
3182 /* Disable and clear interrupts at the chip level also */
3183 w_sdreg32(bus, 0, offsetof(struct sdpcmd_regs, hostintmask), &retries);
3184 local_hostintmask = bus->hostintmask;
3185 bus->hostintmask = 0;
3187 /* Change our idea of bus state */
3188 bus->drvr->busstate = BRCMF_BUS_DOWN;
3190 /* Force clocks on backplane to be sure F2 interrupt propagates */
3191 saveclk = brcmf_sdcard_cfg_read(bus->card, SDIO_FUNC_1,
3192 SBSDIO_FUNC1_CHIPCLKCSR, &err);
3194 brcmf_sdcard_cfg_write(bus->card, SDIO_FUNC_1,
3195 SBSDIO_FUNC1_CHIPCLKCSR,
3196 (saveclk | SBSDIO_FORCE_HT), &err);
3199 BRCMF_ERROR(("%s: Failed to force clock for F2: err %d\n",
3203 /* Turn off the bus (F2), free any pending packets */
3204 BRCMF_INTR(("%s: disable SDIO interrupts\n", __func__));
3205 brcmf_sdcard_intr_disable(bus->card);
3206 brcmf_sdcard_cfg_write(bus->card, SDIO_FUNC_0, SDIO_CCCR_IOEx,
3207 SDIO_FUNC_ENABLE_1, NULL);
3209 /* Clear any pending interrupts now that F2 is disabled */
3210 w_sdreg32(bus, local_hostintmask,
3211 offsetof(struct sdpcmd_regs, intstatus), &retries);
3213 /* Turn off the backplane clock (only) */
3214 brcmf_sdbrcm_clkctl(bus, CLK_SDONLY, false);
3216 /* Clear the data packet queues */
3217 brcmu_pktq_flush(&bus->txq, true, NULL, NULL);
3219 /* Clear any held glomming stuff */
3221 brcmu_pkt_buf_free_skb(bus->glomd);
3224 brcmu_pkt_buf_free_skb(bus->glom);
3226 bus->glom = bus->glomd = NULL;
3228 /* Clear rx control and wake any waiters */
3230 brcmf_os_ioctl_resp_wake(bus->drvr);
3232 /* Reset some F2 state stuff */
3233 bus->rxskip = false;
3234 bus->tx_seq = bus->rx_seq = 0;
3237 brcmf_sdbrcm_sdunlock(bus);
3240 int brcmf_sdbrcm_bus_init(struct brcmf_pub *drvr, bool enforce_mutex)
3242 struct brcmf_bus *bus = drvr->bus;
3243 struct brcmf_timeout tmo;
3249 BRCMF_TRACE(("%s: Enter\n", __func__));
3251 /* try to download image and nvram to the dongle */
3252 if (drvr->busstate == BRCMF_BUS_DOWN) {
3253 if (!(brcmf_sdbrcm_download_firmware(bus, bus->card)))
3260 /* Start the watchdog timer */
3261 bus->drvr->tickcnt = 0;
3262 brcmf_sdbrcm_wd_timer(bus, brcmf_watchdog_ms);
3265 brcmf_sdbrcm_sdlock(bus);
3267 /* Make sure backplane clock is on, needed to generate F2 interrupt */
3268 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
3269 if (bus->clkstate != CLK_AVAIL)
3272 /* Force clocks on backplane to be sure F2 interrupt propagates */
3274 brcmf_sdcard_cfg_read(bus->card, SDIO_FUNC_1,
3275 SBSDIO_FUNC1_CHIPCLKCSR, &err);
3277 brcmf_sdcard_cfg_write(bus->card, SDIO_FUNC_1,
3278 SBSDIO_FUNC1_CHIPCLKCSR,
3279 (saveclk | SBSDIO_FORCE_HT), &err);
3282 BRCMF_ERROR(("%s: Failed to force clock for F2: err %d\n",
3287 /* Enable function 2 (frame transfers) */
3288 w_sdreg32(bus, SDPCM_PROT_VERSION << SMB_DATA_VERSION_SHIFT,
3289 offsetof(struct sdpcmd_regs, tosbmailboxdata), &retries);
3290 enable = (SDIO_FUNC_ENABLE_1 | SDIO_FUNC_ENABLE_2);
3292 brcmf_sdcard_cfg_write(bus->card, SDIO_FUNC_0, SDIO_CCCR_IOEx, enable,
3295 /* Give the dongle some time to do its thing and set IOR2 */
3296 brcmf_timeout_start(&tmo, BRCMF_WAIT_F2RDY * 1000);
3299 while (ready != enable && !brcmf_timeout_expired(&tmo))
3300 ready = brcmf_sdcard_cfg_read(bus->card, SDIO_FUNC_0,
3301 SDIO_CCCR_IORx, NULL);
3303 BRCMF_INFO(("%s: enable 0x%02x, ready 0x%02x (waited %uus)\n",
3304 __func__, enable, ready, tmo.elapsed));
3306 /* If F2 successfully enabled, set core and enable interrupts */
3307 if (ready == enable) {
3308 /* Set up the interrupt mask and enable interrupts */
3309 bus->hostintmask = HOSTINTMASK;
3310 w_sdreg32(bus, bus->hostintmask,
3311 offsetof(struct sdpcmd_regs, hostintmask), &retries);
3313 brcmf_sdcard_cfg_write(bus->card, SDIO_FUNC_1, SBSDIO_WATERMARK,
3314 (u8) watermark, &err);
3316 /* Set bus state according to enable result */
3317 drvr->busstate = BRCMF_BUS_DATA;
3319 bus->intdis = false;
3321 BRCMF_INTR(("%s: enable SDIO device interrupts\n",
3323 brcmf_sdcard_intr_enable(bus->card);
3325 BRCMF_INTR(("%s: disable SDIO interrupts\n", __func__));
3326 brcmf_sdcard_intr_disable(bus->card);
3332 /* Disable F2 again */
3333 enable = SDIO_FUNC_ENABLE_1;
3334 brcmf_sdcard_cfg_write(bus->card, SDIO_FUNC_0, SDIO_CCCR_IOEx,
3338 /* Restore previous clock setting */
3339 brcmf_sdcard_cfg_write(bus->card, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
3342 #if defined(OOB_INTR_ONLY)
3343 /* Host registration for OOB interrupt */
3344 if (brcmf_sdio_register_oob_intr(bus->dhd)) {
3345 brcmf_sdbrcm_wd_timer(bus, 0);
3346 BRCMF_ERROR(("%s Host failed to resgister for OOB\n",
3352 /* Enable oob at firmware */
3353 brcmf_sdbrcm_enable_oob_intr(bus, true);
3354 #endif /* defined(OOB_INTR_ONLY) */
3356 /* If we didn't come up, turn off backplane clock */
3357 if (drvr->busstate != BRCMF_BUS_DATA)
3358 brcmf_sdbrcm_clkctl(bus, CLK_NONE, false);
3362 brcmf_sdbrcm_sdunlock(bus);
3367 static void brcmf_sdbrcm_rxfail(struct brcmf_bus *bus, bool abort, bool rtx)
3369 struct brcmf_sdio_card *card = bus->card;
3375 BRCMF_ERROR(("%s: %sterminate frame%s\n", __func__,
3376 (abort ? "abort command, " : ""),
3377 (rtx ? ", send NAK" : "")));
3380 brcmf_sdcard_abort(card, SDIO_FUNC_2);
3382 brcmf_sdcard_cfg_write(card, SDIO_FUNC_1, SBSDIO_FUNC1_FRAMECTRL,
3386 /* Wait until the packet has been flushed (device/FIFO stable) */
3387 for (lastrbc = retries = 0xffff; retries > 0; retries--) {
3388 hi = brcmf_sdcard_cfg_read(card, SDIO_FUNC_1,
3389 SBSDIO_FUNC1_RFRAMEBCHI, NULL);
3390 lo = brcmf_sdcard_cfg_read(card, SDIO_FUNC_1,
3391 SBSDIO_FUNC1_RFRAMEBCLO, NULL);
3392 bus->f1regdata += 2;
3394 if ((hi == 0) && (lo == 0))
3397 if ((hi > (lastrbc >> 8)) && (lo > (lastrbc & 0x00ff))) {
3398 BRCMF_ERROR(("%s: count growing: last 0x%04x now "
3400 __func__, lastrbc, ((hi << 8) + lo)));
3402 lastrbc = (hi << 8) + lo;
3406 BRCMF_ERROR(("%s: count never zeroed: last 0x%04x\n",
3407 __func__, lastrbc));
3409 BRCMF_INFO(("%s: flush took %d iterations\n", __func__,
3410 (0xffff - retries)));
3415 w_sdreg32(bus, SMB_NAK,
3416 offsetof(struct sdpcmd_regs, tosbmailbox), &retries);
3419 if (retries <= retry_limit)
3423 /* Clear partial in any case */
3426 /* If we can't reach the device, signal failure */
3427 if (err || brcmf_sdcard_regfail(card))
3428 bus->drvr->busstate = BRCMF_BUS_DOWN;
3432 brcmf_sdbrcm_read_control(struct brcmf_bus *bus, u8 *hdr, uint len, uint doff)
3434 struct brcmf_sdio_card *card = bus->card;
3439 BRCMF_TRACE(("%s: Enter\n", __func__));
3441 /* Control data already received in aligned rxctl */
3442 if ((bus->bus == SPI_BUS) && (!bus->usebufpool))
3445 /* Set rxctl for frame (w/optional alignment) */
3446 bus->rxctl = bus->rxbuf;
3447 if (brcmf_alignctl) {
3448 bus->rxctl += firstread;
3449 pad = ((unsigned long)bus->rxctl % BRCMF_SDALIGN);
3451 bus->rxctl += (BRCMF_SDALIGN - pad);
3452 bus->rxctl -= firstread;
3455 /* Copy the already-read portion over */
3456 memcpy(bus->rxctl, hdr, firstread);
3457 if (len <= firstread)
3460 /* Copy the full data pkt in gSPI case and process ioctl. */
3461 if (bus->bus == SPI_BUS) {
3462 memcpy(bus->rxctl, hdr, len);
3466 /* Raise rdlen to next SDIO block to avoid tail command */
3467 rdlen = len - firstread;
3468 if (bus->roundup && bus->blocksize && (rdlen > bus->blocksize)) {
3469 pad = bus->blocksize - (rdlen % bus->blocksize);
3470 if ((pad <= bus->roundup) && (pad < bus->blocksize) &&
3471 ((len + pad) < bus->drvr->maxctl))
3473 } else if (rdlen % BRCMF_SDALIGN) {
3474 rdlen += BRCMF_SDALIGN - (rdlen % BRCMF_SDALIGN);
3477 /* Satisfy length-alignment requirements */
3478 if (forcealign && (rdlen & (ALIGNMENT - 1)))
3479 rdlen = roundup(rdlen, ALIGNMENT);
3481 /* Drop if the read is too big or it exceeds our maximum */
3482 if ((rdlen + firstread) > bus->drvr->maxctl) {
3483 BRCMF_ERROR(("%s: %d-byte control read exceeds %d-byte"
3484 " buffer\n", __func__, rdlen, bus->drvr->maxctl));
3485 bus->drvr->rx_errors++;
3486 brcmf_sdbrcm_rxfail(bus, false, false);
3490 if ((len - doff) > bus->drvr->maxctl) {
3491 BRCMF_ERROR(("%s: %d-byte ctl frame (%d-byte ctl data) exceeds "
3493 __func__, len, (len - doff), bus->drvr->maxctl));
3494 bus->drvr->rx_errors++;
3496 brcmf_sdbrcm_rxfail(bus, false, false);
3500 /* Read remainder of frame body into the rxctl buffer */
3501 sdret = brcmf_sdcard_recv_buf(card, brcmf_sdcard_cur_sbwad(card),
3503 F2SYNC, (bus->rxctl + firstread), rdlen,
3507 /* Control frame failures need retransmission */
3509 BRCMF_ERROR(("%s: read %d control bytes failed: %d\n",
3510 __func__, rdlen, sdret));
3512 brcmf_sdbrcm_rxfail(bus, true, true);
3519 if (BRCMF_BYTES_ON() && BRCMF_CTL_ON()) {
3520 printk(KERN_DEBUG "RxCtrl:\n");
3521 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, bus->rxctl, len);
3525 /* Point to valid data and indicate its length */
3527 bus->rxlen = len - doff;
3530 /* Awake any waiters */
3531 brcmf_os_ioctl_resp_wake(bus->drvr);
3534 static u8 brcmf_sdbrcm_rxglom(struct brcmf_bus *bus, u8 rxseq)
3540 struct sk_buff *pfirst, *plast, *pnext, *save_pfirst;
3543 u8 chan, seq, doff, sfdoff;
3547 bool usechain = bus->use_rxchain;
3549 /* If packets, issue read(s) and send up packet chain */
3550 /* Return sequence numbers consumed? */
3552 BRCMF_TRACE(("brcmf_sdbrcm_rxglom: start: glomd %p glom %p\n",
3553 bus->glomd, bus->glom));
3555 /* If there's a descriptor, generate the packet chain */
3557 pfirst = plast = pnext = NULL;
3558 dlen = (u16) (bus->glomd->len);
3559 dptr = bus->glomd->data;
3560 if (!dlen || (dlen & 1)) {
3561 BRCMF_ERROR(("%s: bad glomd len(%d),"
3562 " ignore descriptor\n",
3567 for (totlen = num = 0; dlen; num++) {
3568 /* Get (and move past) next length */
3569 sublen = get_unaligned_le16(dptr);
3570 dlen -= sizeof(u16);
3571 dptr += sizeof(u16);
3572 if ((sublen < SDPCM_HDRLEN) ||
3573 ((num == 0) && (sublen < (2 * SDPCM_HDRLEN)))) {
3574 BRCMF_ERROR(("%s: descriptor len %d bad: %d\n",
3575 __func__, num, sublen));
3579 if (sublen % BRCMF_SDALIGN) {
3580 BRCMF_ERROR(("%s: sublen %d not multiple of"
3581 " %d\n", __func__, sublen,
3587 /* For last frame, adjust read len so total
3588 is a block multiple */
3591 (roundup(totlen, bus->blocksize) - totlen);
3592 totlen = roundup(totlen, bus->blocksize);
3595 /* Allocate/chain packet for next subframe */
3596 pnext = brcmu_pkt_buf_get_skb(sublen + BRCMF_SDALIGN);
3597 if (pnext == NULL) {
3598 BRCMF_ERROR(("%s: bcm_pkt_buf_get_skb failed, "
3599 "num %d len %d\n", __func__,
3604 pfirst = plast = pnext;
3606 plast->next = pnext;
3610 /* Adhere to start alignment requirements */
3611 PKTALIGN(pnext, sublen, BRCMF_SDALIGN);
3614 /* If all allocations succeeded, save packet chain
3617 BRCMF_GLOM(("%s: allocated %d-byte packet chain for %d "
3618 "subframes\n", __func__, totlen, num));
3619 if (BRCMF_GLOM_ON() && bus->nextlen) {
3620 if (totlen != bus->nextlen) {
3621 BRCMF_GLOM(("%s: glomdesc mismatch: "
3622 "nextlen %d glomdesc %d "
3623 "rxseq %d\n", __func__,
3629 pfirst = pnext = NULL;
3632 brcmu_pkt_buf_free_skb(pfirst);
3637 /* Done with descriptor packet */
3638 brcmu_pkt_buf_free_skb(bus->glomd);
3643 /* Ok -- either we just generated a packet chain,
3644 or had one from before */
3646 if (BRCMF_GLOM_ON()) {
3647 BRCMF_GLOM(("%s: try superframe read, packet chain:\n",
3649 for (pnext = bus->glom; pnext; pnext = pnext->next) {
3650 BRCMF_GLOM((" %p: %p len 0x%04x (%d)\n",
3651 pnext, (u8 *) (pnext->data),
3652 pnext->len, pnext->len));
3657 dlen = (u16) brcmu_pkttotlen(pfirst);
3659 /* Do an SDIO read for the superframe. Configurable iovar to
3660 * read directly into the chained packet, or allocate a large
3661 * packet and and copy into the chain.
3664 errcode = brcmf_sdcard_recv_buf(bus->card,
3665 brcmf_sdcard_cur_sbwad(bus->card),
3667 F2SYNC, (u8 *) pfirst->data, dlen,
3668 pfirst, NULL, NULL);
3669 } else if (bus->dataptr) {
3670 errcode = brcmf_sdcard_recv_buf(bus->card,
3671 brcmf_sdcard_cur_sbwad(bus->card),
3673 F2SYNC, bus->dataptr, dlen,
3675 sublen = (u16) brcmu_pktfrombuf(pfirst, 0, dlen,
3677 if (sublen != dlen) {
3678 BRCMF_ERROR(("%s: FAILED TO COPY, dlen %d "
3680 __func__, dlen, sublen));
3685 BRCMF_ERROR(("COULDN'T ALLOC %d-BYTE GLOM, "
3686 "FORCE FAILURE\n", dlen));
3691 /* On failure, kill the superframe, allow a couple retries */
3693 BRCMF_ERROR(("%s: glom read of %d bytes failed: %d\n",
3694 __func__, dlen, errcode));
3695 bus->drvr->rx_errors++;
3697 if (bus->glomerr++ < 3) {
3698 brcmf_sdbrcm_rxfail(bus, true, true);
3701 brcmf_sdbrcm_rxfail(bus, true, false);
3702 brcmu_pkt_buf_free_skb(bus->glom);
3709 if (BRCMF_GLOM_ON()) {
3710 printk(KERN_DEBUG "SUPERFRAME:\n");
3711 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
3712 pfirst->data, min_t(int, pfirst->len, 48));
3716 /* Validate the superframe header */
3717 dptr = (u8 *) (pfirst->data);
3718 sublen = get_unaligned_le16(dptr);
3719 check = get_unaligned_le16(dptr + sizeof(u16));
3721 chan = SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]);
3722 seq = SDPCM_PACKET_SEQUENCE(&dptr[SDPCM_FRAMETAG_LEN]);
3723 bus->nextlen = dptr[SDPCM_FRAMETAG_LEN + SDPCM_NEXTLEN_OFFSET];
3724 if ((bus->nextlen << 4) > MAX_RX_DATASZ) {
3725 BRCMF_INFO(("%s: nextlen too large (%d) seq %d\n",
3726 __func__, bus->nextlen, seq));
3729 doff = SDPCM_DOFFSET_VALUE(&dptr[SDPCM_FRAMETAG_LEN]);
3730 txmax = SDPCM_WINDOW_VALUE(&dptr[SDPCM_FRAMETAG_LEN]);
3733 if ((u16)~(sublen ^ check)) {
3734 BRCMF_ERROR(("%s (superframe): HW hdr error: len/check "
3735 "0x%04x/0x%04x\n", __func__, sublen,
3738 } else if (roundup(sublen, bus->blocksize) != dlen) {
3739 BRCMF_ERROR(("%s (superframe): len 0x%04x, rounded "
3740 "0x%04x, expect 0x%04x\n",
3742 roundup(sublen, bus->blocksize), dlen));
3744 } else if (SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]) !=
3745 SDPCM_GLOM_CHANNEL) {
3746 BRCMF_ERROR(("%s (superframe): bad channel %d\n",
3748 SDPCM_PACKET_CHANNEL(&dptr
3749 [SDPCM_FRAMETAG_LEN])));
3751 } else if (SDPCM_GLOMDESC(&dptr[SDPCM_FRAMETAG_LEN])) {
3752 BRCMF_ERROR(("%s (superframe): got 2nd descriptor?\n",
3755 } else if ((doff < SDPCM_HDRLEN) ||
3756 (doff > (pfirst->len - SDPCM_HDRLEN))) {
3757 BRCMF_ERROR(("%s (superframe): Bad data offset %d: "
3758 "HW %d pkt %d min %d\n",
3759 __func__, doff, sublen,
3760 pfirst->len, SDPCM_HDRLEN));
3764 /* Check sequence number of superframe SW header */
3766 BRCMF_INFO(("%s: (superframe) rx_seq %d, expected %d\n",
3767 __func__, seq, rxseq));
3772 /* Check window for sanity */
3773 if ((u8) (txmax - bus->tx_seq) > 0x40) {
3774 BRCMF_ERROR(("%s: unlikely tx max %d with tx_seq %d\n",
3775 __func__, txmax, bus->tx_seq));
3776 txmax = bus->tx_seq + 2;
3778 bus->tx_max = txmax;
3780 /* Remove superframe header, remember offset */
3781 skb_pull(pfirst, doff);
3784 /* Validate all the subframe headers */
3785 for (num = 0, pnext = pfirst; pnext && !errcode;
3786 num++, pnext = pnext->next) {
3787 dptr = (u8 *) (pnext->data);
3788 dlen = (u16) (pnext->len);
3789 sublen = get_unaligned_le16(dptr);
3790 check = get_unaligned_le16(dptr + sizeof(u16));
3791 chan = SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]);
3792 doff = SDPCM_DOFFSET_VALUE(&dptr[SDPCM_FRAMETAG_LEN]);
3794 if (BRCMF_GLOM_ON()) {
3795 printk(KERN_DEBUG "subframe:\n");
3796 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
3801 if ((u16)~(sublen ^ check)) {
3802 BRCMF_ERROR(("%s (subframe %d): HW hdr error: "
3803 "len/check 0x%04x/0x%04x\n",
3804 __func__, num, sublen, check));
3806 } else if ((sublen > dlen) || (sublen < SDPCM_HDRLEN)) {
3807 BRCMF_ERROR(("%s (subframe %d): length mismatch"
3808 ": len 0x%04x, expect 0x%04x\n",
3809 __func__, num, sublen, dlen));
3811 } else if ((chan != SDPCM_DATA_CHANNEL) &&
3812 (chan != SDPCM_EVENT_CHANNEL)) {
3813 BRCMF_ERROR(("%s (subframe %d): bad channel"
3814 " %d\n", __func__, num, chan));
3816 } else if ((doff < SDPCM_HDRLEN) || (doff > sublen)) {
3817 BRCMF_ERROR(("%s (subframe %d): Bad data offset"
3818 " %d: HW %d min %d\n",
3819 __func__, num, doff, sublen,
3826 /* Terminate frame on error, request
3828 if (bus->glomerr++ < 3) {
3829 /* Restore superframe header space */
3830 skb_push(pfirst, sfdoff);
3831 brcmf_sdbrcm_rxfail(bus, true, true);
3834 brcmf_sdbrcm_rxfail(bus, true, false);
3835 brcmu_pkt_buf_free_skb(bus->glom);
3843 /* Basic SD framing looks ok - process each packet (header) */
3844 save_pfirst = pfirst;
3848 for (num = 0; pfirst; rxseq++, pfirst = pnext) {
3849 pnext = pfirst->next;
3850 pfirst->next = NULL;
3852 dptr = (u8 *) (pfirst->data);
3853 sublen = get_unaligned_le16(dptr);
3854 chan = SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]);
3855 seq = SDPCM_PACKET_SEQUENCE(&dptr[SDPCM_FRAMETAG_LEN]);
3856 doff = SDPCM_DOFFSET_VALUE(&dptr[SDPCM_FRAMETAG_LEN]);
3858 BRCMF_GLOM(("%s: Get subframe %d, %p(%p/%d), sublen %d "
3860 __func__, num, pfirst, pfirst->data,
3861 pfirst->len, sublen, chan, seq));
3863 /* precondition: chan == SDPCM_DATA_CHANNEL ||
3864 chan == SDPCM_EVENT_CHANNEL */
3867 BRCMF_GLOM(("%s: rx_seq %d, expected %d\n",
3868 __func__, seq, rxseq));
3873 if (BRCMF_BYTES_ON() && BRCMF_DATA_ON()) {
3874 printk(KERN_DEBUG "Rx Subframe Data:\n");
3875 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
3880 __skb_trim(pfirst, sublen);
3881 skb_pull(pfirst, doff);
3883 if (pfirst->len == 0) {
3884 brcmu_pkt_buf_free_skb(pfirst);
3886 plast->next = pnext;
3888 save_pfirst = pnext;
3891 } else if (brcmf_proto_hdrpull(bus->drvr, &ifidx, pfirst)
3893 BRCMF_ERROR(("%s: rx protocol error\n",
3895 bus->drvr->rx_errors++;
3896 brcmu_pkt_buf_free_skb(pfirst);
3898 plast->next = pnext;
3900 save_pfirst = pnext;
3905 /* this packet will go up, link back into
3906 chain and count it */
3907 pfirst->next = pnext;
3912 if (BRCMF_GLOM_ON()) {
3913 BRCMF_GLOM(("%s subframe %d to stack, %p"
3914 "(%p/%d) nxt/lnk %p/%p\n",
3915 __func__, num, pfirst, pfirst->data,
3916 pfirst->len, pfirst->next,
3918 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
3920 min_t(int, pfirst->len, 32));
3925 brcmf_sdbrcm_sdunlock(bus);
3926 brcmf_rx_frame(bus->drvr, ifidx, save_pfirst, num);
3927 brcmf_sdbrcm_sdlock(bus);
3930 bus->rxglomframes++;
3931 bus->rxglompkts += num;
3936 /* Return true if there may be more frames to read */
3938 brcmf_sdbrcm_readframes(struct brcmf_bus *bus, uint maxframes, bool *finished)
3940 struct brcmf_sdio_card *card = bus->card;
3942 u16 len, check; /* Extracted hardware header fields */
3943 u8 chan, seq, doff; /* Extracted software header fields */
3944 u8 fcbits; /* Extracted fcbits from software header */
3946 struct sk_buff *pkt; /* Packet for event or data frames */
3947 u16 pad; /* Number of pad bytes to read */
3948 u16 rdlen; /* Total number of bytes to read */
3949 u8 rxseq; /* Next sequence number to expect */
3950 uint rxleft = 0; /* Remaining number of frames allowed */
3951 int sdret; /* Return code from calls */
3952 u8 txmax; /* Maximum tx sequence offered */
3953 bool len_consistent; /* Result of comparing readahead len and
3957 uint rxcount = 0; /* Total frames read */
3959 #if defined(BCMDBG) || defined(SDTEST)
3960 bool sdtest = false; /* To limit message spew from test mode */
3963 BRCMF_TRACE(("%s: Enter\n", __func__));
3966 /* Allow pktgen to override maxframes */
3967 if (bus->pktgen_count && (bus->pktgen_mode == BRCMF_PKTGEN_RECV)) {
3968 maxframes = bus->pktgen_count;
3973 /* Not finished unless we encounter no more frames indication */
3976 for (rxseq = bus->rx_seq, rxleft = maxframes;
3977 !bus->rxskip && rxleft && bus->drvr->busstate != BRCMF_BUS_DOWN;
3978 rxseq++, rxleft--) {
3980 /* Handle glomming separately */
3981 if (bus->glom || bus->glomd) {
3983 BRCMF_GLOM(("%s: calling rxglom: glomd %p, glom %p\n",
3984 __func__, bus->glomd, bus->glom));
3985 cnt = brcmf_sdbrcm_rxglom(bus, rxseq);
3986 BRCMF_GLOM(("%s: rxglom returned %d\n", __func__, cnt));
3988 rxleft = (rxleft > cnt) ? (rxleft - cnt) : 1;
3992 /* Try doing single read if we can */
3993 if (brcmf_readahead && bus->nextlen) {
3994 u16 nextlen = bus->nextlen;
3997 if (bus->bus == SPI_BUS) {
3998 rdlen = len = nextlen;
4000 rdlen = len = nextlen << 4;
4002 /* Pad read to blocksize for efficiency */
4003 if (bus->roundup && bus->blocksize
4004 && (rdlen > bus->blocksize)) {
4007 (rdlen % bus->blocksize);
4008 if ((pad <= bus->roundup)
4009 && (pad < bus->blocksize)
4010 && ((rdlen + pad + firstread) <
4013 } else if (rdlen % BRCMF_SDALIGN) {
4015 BRCMF_SDALIGN - (rdlen % BRCMF_SDALIGN);
4019 /* We use bus->rxctl buffer in WinXP for initial
4020 * control pkt receives.
4021 * Later we use buffer-poll for data as well
4022 * as control packets.
4023 * This is required because dhd receives full
4024 * frame in gSPI unlike SDIO.
4025 * After the frame is received we have to
4026 * distinguish whether it is data
4027 * or non-data frame.
4029 /* Allocate a packet buffer */
4030 pkt = brcmu_pkt_buf_get_skb(rdlen + BRCMF_SDALIGN);
4032 if (bus->bus == SPI_BUS) {
4033 bus->usebufpool = false;
4034 bus->rxctl = bus->rxbuf;
4035 if (brcmf_alignctl) {
4036 bus->rxctl += firstread;
4037 pad = ((unsigned long)bus->rxctl %
4041 (BRCMF_SDALIGN - pad);
4042 bus->rxctl -= firstread;
4045 /* Read the entire frame */
4046 sdret = brcmf_sdcard_recv_buf(card,
4047 brcmf_sdcard_cur_sbwad(card),
4048 SDIO_FUNC_2, F2SYNC,
4053 /* Control frame failures need
4056 BRCMF_ERROR(("%s: read %d "
4061 /* dhd.rx_ctlerrs is higher */
4063 brcmf_sdbrcm_rxfail(bus, true,
4071 request rtx of events */
4072 BRCMF_ERROR(("%s (nextlen): "
4073 "brcmu_pkt_buf_get_skb "
4075 " len %d rdlen %d expected"
4076 " rxseq %d\n", __func__,
4077 len, rdlen, rxseq));
4081 if (bus->bus == SPI_BUS)
4082 bus->usebufpool = true;
4084 PKTALIGN(pkt, rdlen, BRCMF_SDALIGN);
4085 rxbuf = (u8 *) (pkt->data);
4086 /* Read the entire frame */
4087 sdret = brcmf_sdcard_recv_buf(card,
4088 brcmf_sdcard_cur_sbwad(card),
4089 SDIO_FUNC_2, F2SYNC,
4095 BRCMF_ERROR(("%s (nextlen): read %d"
4096 " bytes failed: %d\n",
4097 __func__, rdlen, sdret));
4098 brcmu_pkt_buf_free_skb(pkt);
4099 bus->drvr->rx_errors++;
4100 /* Force retry w/normal header read.
4101 * Don't attempt NAK for
4104 brcmf_sdbrcm_rxfail(bus, true,
4112 /* Now check the header */
4113 memcpy(bus->rxhdr, rxbuf, SDPCM_HDRLEN);
4115 /* Extract hardware header fields */
4116 len = get_unaligned_le16(bus->rxhdr);
4117 check = get_unaligned_le16(bus->rxhdr + sizeof(u16));
4119 /* All zeros means readahead info was bad */
4120 if (!(len | check)) {
4121 BRCMF_INFO(("%s (nextlen): read zeros in HW "
4122 "header???\n", __func__));
4123 brcmf_sdbrcm_pktfree2(bus, pkt);
4127 /* Validate check bytes */
4128 if ((u16)~(len ^ check)) {
4129 BRCMF_ERROR(("%s (nextlen): HW hdr error:"
4130 " nextlen/len/check"
4131 " 0x%04x/0x%04x/0x%04x\n",
4132 __func__, nextlen, len, check));
4134 brcmf_sdbrcm_rxfail(bus, false, false);
4135 brcmf_sdbrcm_pktfree2(bus, pkt);
4139 /* Validate frame length */
4140 if (len < SDPCM_HDRLEN) {
4141 BRCMF_ERROR(("%s (nextlen): HW hdr length "
4142 "invalid: %d\n", __func__, len));
4143 brcmf_sdbrcm_pktfree2(bus, pkt);
4147 /* Check for consistency withreadahead info */
4148 len_consistent = (nextlen != (roundup(len, 16) >> 4));
4149 if (len_consistent) {
4150 /* Mismatch, force retry w/normal
4151 header (may be >4K) */
4152 BRCMF_ERROR(("%s (nextlen): mismatch, "
4153 "nextlen %d len %d rnd %d; "
4154 "expected rxseq %d\n",
4156 len, roundup(len, 16), rxseq));
4157 brcmf_sdbrcm_rxfail(bus, true,
4158 bus->bus != SPI_BUS);
4159 brcmf_sdbrcm_pktfree2(bus, pkt);
4163 /* Extract software header fields */
4164 chan = SDPCM_PACKET_CHANNEL(
4165 &bus->rxhdr[SDPCM_FRAMETAG_LEN]);
4166 seq = SDPCM_PACKET_SEQUENCE(
4167 &bus->rxhdr[SDPCM_FRAMETAG_LEN]);
4168 doff = SDPCM_DOFFSET_VALUE(
4169 &bus->rxhdr[SDPCM_FRAMETAG_LEN]);
4170 txmax = SDPCM_WINDOW_VALUE(
4171 &bus->rxhdr[SDPCM_FRAMETAG_LEN]);
4174 bus->rxhdr[SDPCM_FRAMETAG_LEN +
4175 SDPCM_NEXTLEN_OFFSET];
4176 if ((bus->nextlen << 4) > MAX_RX_DATASZ) {
4177 BRCMF_INFO(("%s (nextlen): got frame w/nextlen"
4178 " too large (%d), seq %d\n",
4179 __func__, bus->nextlen, seq));
4183 bus->drvr->rx_readahead_cnt++;
4185 /* Handle Flow Control */
4186 fcbits = SDPCM_FCMASK_VALUE(
4187 &bus->rxhdr[SDPCM_FRAMETAG_LEN]);
4189 if (bus->flowcontrol != fcbits) {
4190 if (~bus->flowcontrol & fcbits)
4193 if (bus->flowcontrol & ~fcbits)
4197 bus->flowcontrol = fcbits;
4200 /* Check and update sequence number */
4202 BRCMF_INFO(("%s (nextlen): rx_seq %d, expected "
4203 "%d\n", __func__, seq, rxseq));
4208 /* Check window for sanity */
4209 if ((u8) (txmax - bus->tx_seq) > 0x40) {
4210 BRCMF_ERROR(("%s: got unlikely tx max %d with "
4212 __func__, txmax, bus->tx_seq));
4213 txmax = bus->tx_seq + 2;
4215 bus->tx_max = txmax;
4218 if (BRCMF_BYTES_ON() && BRCMF_DATA_ON()) {
4219 printk(KERN_DEBUG "Rx Data:\n");
4220 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
4222 } else if (BRCMF_HDRS_ON()) {
4223 printk(KERN_DEBUG "RxHdr:\n");
4224 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
4225 bus->rxhdr, SDPCM_HDRLEN);
4229 if (chan == SDPCM_CONTROL_CHANNEL) {
4230 if (bus->bus == SPI_BUS) {
4231 brcmf_sdbrcm_read_control(bus, rxbuf,
4234 BRCMF_ERROR(("%s (nextlen): readahead"
4235 " on control packet %d?\n",
4237 /* Force retry w/normal header read */
4239 brcmf_sdbrcm_rxfail(bus, false, true);
4241 brcmf_sdbrcm_pktfree2(bus, pkt);
4245 if ((bus->bus == SPI_BUS) && !bus->usebufpool) {
4246 BRCMF_ERROR(("Received %d bytes on %d channel."
4247 " Running out of " "rx pktbuf's or"
4248 " not yet malloced.\n",
4253 /* Validate data offset */
4254 if ((doff < SDPCM_HDRLEN) || (doff > len)) {
4255 BRCMF_ERROR(("%s (nextlen): bad data offset %d:"
4256 " HW len %d min %d\n", __func__,
4257 doff, len, SDPCM_HDRLEN));
4258 brcmf_sdbrcm_rxfail(bus, false, false);
4259 brcmf_sdbrcm_pktfree2(bus, pkt);
4263 /* All done with this one -- now deliver the packet */
4266 /* gSPI frames should not be handled in fractions */
4267 if (bus->bus == SPI_BUS)
4270 /* Read frame header (hardware and software) */
4271 sdret = brcmf_sdcard_recv_buf(card,
4272 brcmf_sdcard_cur_sbwad(card),
4273 SDIO_FUNC_2, F2SYNC, bus->rxhdr, firstread,
4278 BRCMF_ERROR(("%s: RXHEADER FAILED: %d\n", __func__,
4281 brcmf_sdbrcm_rxfail(bus, true, true);
4285 if (BRCMF_BYTES_ON() || BRCMF_HDRS_ON()) {
4286 printk(KERN_DEBUG "RxHdr:\n");
4287 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
4288 bus->rxhdr, SDPCM_HDRLEN);
4292 /* Extract hardware header fields */
4293 len = get_unaligned_le16(bus->rxhdr);
4294 check = get_unaligned_le16(bus->rxhdr + sizeof(u16));
4296 /* All zeros means no more frames */
4297 if (!(len | check)) {
4302 /* Validate check bytes */
4303 if ((u16) ~(len ^ check)) {
4304 BRCMF_ERROR(("%s: HW hdr err: len/check "
4305 "0x%04x/0x%04x\n", __func__, len, check));
4307 brcmf_sdbrcm_rxfail(bus, false, false);
4311 /* Validate frame length */
4312 if (len < SDPCM_HDRLEN) {
4313 BRCMF_ERROR(("%s: HW hdr length invalid: %d\n",
4318 /* Extract software header fields */
4319 chan = SDPCM_PACKET_CHANNEL(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
4320 seq = SDPCM_PACKET_SEQUENCE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
4321 doff = SDPCM_DOFFSET_VALUE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
4322 txmax = SDPCM_WINDOW_VALUE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
4324 /* Validate data offset */
4325 if ((doff < SDPCM_HDRLEN) || (doff > len)) {
4326 BRCMF_ERROR(("%s: Bad data offset %d: HW len %d,"
4327 " min %d seq %d\n", __func__, doff,
4328 len, SDPCM_HDRLEN, seq));
4330 brcmf_sdbrcm_rxfail(bus, false, false);
4334 /* Save the readahead length if there is one */
4336 bus->rxhdr[SDPCM_FRAMETAG_LEN + SDPCM_NEXTLEN_OFFSET];
4337 if ((bus->nextlen << 4) > MAX_RX_DATASZ) {
4338 BRCMF_INFO(("%s (nextlen): got frame w/nextlen too"
4339 " large (%d), seq %d\n",
4340 __func__, bus->nextlen, seq));
4344 /* Handle Flow Control */
4345 fcbits = SDPCM_FCMASK_VALUE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
4347 if (bus->flowcontrol != fcbits) {
4348 if (~bus->flowcontrol & fcbits)
4351 if (bus->flowcontrol & ~fcbits)
4355 bus->flowcontrol = fcbits;
4358 /* Check and update sequence number */
4360 BRCMF_INFO(("%s: rx_seq %d, expected %d\n", __func__,
4366 /* Check window for sanity */
4367 if ((u8) (txmax - bus->tx_seq) > 0x40) {
4368 BRCMF_ERROR(("%s: unlikely tx max %d with tx_seq %d\n",
4369 __func__, txmax, bus->tx_seq));
4370 txmax = bus->tx_seq + 2;
4372 bus->tx_max = txmax;
4374 /* Call a separate function for control frames */
4375 if (chan == SDPCM_CONTROL_CHANNEL) {
4376 brcmf_sdbrcm_read_control(bus, bus->rxhdr, len, doff);
4380 /* precondition: chan is either SDPCM_DATA_CHANNEL,
4381 SDPCM_EVENT_CHANNEL, SDPCM_TEST_CHANNEL or
4382 SDPCM_GLOM_CHANNEL */
4384 /* Length to read */
4385 rdlen = (len > firstread) ? (len - firstread) : 0;
4387 /* May pad read to blocksize for efficiency */
4388 if (bus->roundup && bus->blocksize &&
4389 (rdlen > bus->blocksize)) {
4390 pad = bus->blocksize - (rdlen % bus->blocksize);
4391 if ((pad <= bus->roundup) && (pad < bus->blocksize) &&
4392 ((rdlen + pad + firstread) < MAX_RX_DATASZ))
4394 } else if (rdlen % BRCMF_SDALIGN) {
4395 rdlen += BRCMF_SDALIGN - (rdlen % BRCMF_SDALIGN);
4398 /* Satisfy length-alignment requirements */
4399 if (forcealign && (rdlen & (ALIGNMENT - 1)))
4400 rdlen = roundup(rdlen, ALIGNMENT);
4402 if ((rdlen + firstread) > MAX_RX_DATASZ) {
4403 /* Too long -- skip this frame */
4404 BRCMF_ERROR(("%s: too long: len %d rdlen %d\n",
4405 __func__, len, rdlen));
4406 bus->drvr->rx_errors++;
4408 brcmf_sdbrcm_rxfail(bus, false, false);
4412 pkt = brcmu_pkt_buf_get_skb(rdlen + firstread + BRCMF_SDALIGN);
4414 /* Give up on data, request rtx of events */
4415 BRCMF_ERROR(("%s: brcmu_pkt_buf_get_skb failed:"
4416 " rdlen %d chan %d\n", __func__, rdlen,
4418 bus->drvr->rx_dropped++;
4419 brcmf_sdbrcm_rxfail(bus, false, RETRYCHAN(chan));
4423 /* Leave room for what we already read, and align remainder */
4424 skb_pull(pkt, firstread);
4425 PKTALIGN(pkt, rdlen, BRCMF_SDALIGN);
4427 /* Read the remaining frame data */
4428 sdret = brcmf_sdcard_recv_buf(card,
4429 brcmf_sdcard_cur_sbwad(card),
4430 SDIO_FUNC_2, F2SYNC, ((u8 *) (pkt->data)),
4431 rdlen, pkt, NULL, NULL);
4435 BRCMF_ERROR(("%s: read %d %s bytes failed: %d\n",
4437 ((chan == SDPCM_EVENT_CHANNEL) ? "event"
4438 : ((chan == SDPCM_DATA_CHANNEL) ? "data"
4439 : "test")), sdret));
4440 brcmu_pkt_buf_free_skb(pkt);
4441 bus->drvr->rx_errors++;
4442 brcmf_sdbrcm_rxfail(bus, true, RETRYCHAN(chan));
4446 /* Copy the already-read portion */
4447 skb_push(pkt, firstread);
4448 memcpy(pkt->data, bus->rxhdr, firstread);
4451 if (BRCMF_BYTES_ON() && BRCMF_DATA_ON()) {
4452 printk(KERN_DEBUG "Rx Data:\n");
4453 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
4459 /* Save superframe descriptor and allocate packet frame */
4460 if (chan == SDPCM_GLOM_CHANNEL) {
4461 if (SDPCM_GLOMDESC(&bus->rxhdr[SDPCM_FRAMETAG_LEN])) {
4462 BRCMF_GLOM(("%s: glom descriptor, %d bytes:\n",
4465 if (BRCMF_GLOM_ON()) {
4466 printk(KERN_DEBUG "Glom Data:\n");
4467 print_hex_dump_bytes("",
4472 __skb_trim(pkt, len);
4473 skb_pull(pkt, SDPCM_HDRLEN);
4476 BRCMF_ERROR(("%s: glom superframe w/o "
4477 "descriptor!\n", __func__));
4478 brcmf_sdbrcm_rxfail(bus, false, false);
4483 /* Fill in packet len and prio, deliver upward */
4484 __skb_trim(pkt, len);
4485 skb_pull(pkt, doff);
4488 /* Test channel packets are processed separately */
4489 if (chan == SDPCM_TEST_CHANNEL) {
4490 brcmf_sdbrcm_checkdied(bus, pkt, seq);
4495 if (pkt->len == 0) {
4496 brcmu_pkt_buf_free_skb(pkt);
4498 } else if (brcmf_proto_hdrpull(bus->drvr, &ifidx, pkt) != 0) {
4499 BRCMF_ERROR(("%s: rx protocol error\n", __func__));
4500 brcmu_pkt_buf_free_skb(pkt);
4501 bus->drvr->rx_errors++;
4505 /* Unlock during rx call */
4506 brcmf_sdbrcm_sdunlock(bus);
4507 brcmf_rx_frame(bus->drvr, ifidx, pkt, 1);
4508 brcmf_sdbrcm_sdlock(bus);
4510 rxcount = maxframes - rxleft;
4512 /* Message if we hit the limit */
4513 if (!rxleft && !sdtest)
4514 BRCMF_DATA(("%s: hit rx limit of %d frames\n", __func__,
4518 BRCMF_DATA(("%s: processed %d frames\n", __func__, rxcount));
4519 /* Back off rxseq if awaiting rtx, update rx_seq */
4522 bus->rx_seq = rxseq;
4527 static u32 brcmf_sdbrcm_hostmail(struct brcmf_bus *bus)
4534 BRCMF_TRACE(("%s: Enter\n", __func__));
4536 /* Read mailbox data and ack that we did so */
4537 r_sdreg32(bus, &hmb_data,
4538 offsetof(struct sdpcmd_regs, tohostmailboxdata), &retries);
4540 if (retries <= retry_limit)
4541 w_sdreg32(bus, SMB_INT_ACK,
4542 offsetof(struct sdpcmd_regs, tosbmailbox), &retries);
4543 bus->f1regdata += 2;
4545 /* Dongle recomposed rx frames, accept them again */
4546 if (hmb_data & HMB_DATA_NAKHANDLED) {
4547 BRCMF_INFO(("Dongle reports NAK handled, expect rtx of %d\n",
4550 BRCMF_ERROR(("%s: unexpected NAKHANDLED!\n", __func__));
4552 bus->rxskip = false;
4553 intstatus |= I_HMB_FRAME_IND;
4557 * DEVREADY does not occur with gSPI.
4559 if (hmb_data & (HMB_DATA_DEVREADY | HMB_DATA_FWREADY)) {
4561 (hmb_data & HMB_DATA_VERSION_MASK) >>
4562 HMB_DATA_VERSION_SHIFT;
4563 if (bus->sdpcm_ver != SDPCM_PROT_VERSION)
4564 BRCMF_ERROR(("Version mismatch, dongle reports %d, "
4566 bus->sdpcm_ver, SDPCM_PROT_VERSION));
4568 BRCMF_INFO(("Dongle ready, protocol version %d\n",
4573 * Flow Control has been moved into the RX headers and this out of band
4574 * method isn't used any more.
4575 * remaining backward compatible with older dongles.
4577 if (hmb_data & HMB_DATA_FC) {
4578 fcbits = (hmb_data & HMB_DATA_FCDATA_MASK) >>
4579 HMB_DATA_FCDATA_SHIFT;
4581 if (fcbits & ~bus->flowcontrol)
4584 if (bus->flowcontrol & ~fcbits)
4588 bus->flowcontrol = fcbits;
4591 /* Shouldn't be any others */
4592 if (hmb_data & ~(HMB_DATA_DEVREADY |
4593 HMB_DATA_NAKHANDLED |
4596 HMB_DATA_FCDATA_MASK | HMB_DATA_VERSION_MASK)) {
4597 BRCMF_ERROR(("Unknown mailbox data content: 0x%02x\n",
4604 static bool brcmf_sdbrcm_dpc(struct brcmf_bus *bus)
4606 struct brcmf_sdio_card *card = bus->card;
4607 u32 intstatus, newstatus = 0;
4609 uint rxlimit = brcmf_rxbound; /* Rx frames to read before resched */
4610 uint txlimit = brcmf_txbound; /* Tx frames to send before resched */
4611 uint framecnt = 0; /* Temporary counter of tx/rx frames */
4612 bool rxdone = true; /* Flag for no more read data */
4613 bool resched = false; /* Flag indicating resched wanted */
4615 BRCMF_TRACE(("%s: Enter\n", __func__));
4617 /* Start with leftover status bits */
4618 intstatus = bus->intstatus;
4620 brcmf_sdbrcm_sdlock(bus);
4622 /* If waiting for HTAVAIL, check status */
4623 if (bus->clkstate == CLK_PENDING) {
4625 u8 clkctl, devctl = 0;
4628 /* Check for inconsistent device control */
4629 devctl = brcmf_sdcard_cfg_read(card, SDIO_FUNC_1,
4630 SBSDIO_DEVICE_CTL, &err);
4632 BRCMF_ERROR(("%s: error reading DEVCTL: %d\n",
4634 bus->drvr->busstate = BRCMF_BUS_DOWN;
4638 /* Read CSR, if clock on switch to AVAIL, else ignore */
4639 clkctl = brcmf_sdcard_cfg_read(card, SDIO_FUNC_1,
4640 SBSDIO_FUNC1_CHIPCLKCSR, &err);
4642 BRCMF_ERROR(("%s: error reading CSR: %d\n", __func__,
4644 bus->drvr->busstate = BRCMF_BUS_DOWN;
4647 BRCMF_INFO(("DPC: PENDING, devctl 0x%02x clkctl 0x%02x\n",
4650 if (SBSDIO_HTAV(clkctl)) {
4651 devctl = brcmf_sdcard_cfg_read(card, SDIO_FUNC_1,
4652 SBSDIO_DEVICE_CTL, &err);
4654 BRCMF_ERROR(("%s: error reading DEVCTL: %d\n",
4656 bus->drvr->busstate = BRCMF_BUS_DOWN;
4658 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
4659 brcmf_sdcard_cfg_write(card, SDIO_FUNC_1,
4660 SBSDIO_DEVICE_CTL, devctl, &err);
4662 BRCMF_ERROR(("%s: error writing DEVCTL: %d\n",
4664 bus->drvr->busstate = BRCMF_BUS_DOWN;
4666 bus->clkstate = CLK_AVAIL;
4674 /* Make sure backplane clock is on */
4675 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, true);
4676 if (bus->clkstate == CLK_PENDING)
4679 /* Pending interrupt indicates new device status */
4682 r_sdreg32(bus, &newstatus,
4683 offsetof(struct sdpcmd_regs, intstatus), &retries);
4685 if (brcmf_sdcard_regfail(bus->card))
4687 newstatus &= bus->hostintmask;
4688 bus->fcstate = !!(newstatus & I_HMB_FC_STATE);
4690 w_sdreg32(bus, newstatus,
4691 offsetof(struct sdpcmd_regs, intstatus),
4697 /* Merge new bits with previous */
4698 intstatus |= newstatus;
4701 /* Handle flow-control change: read new state in case our ack
4702 * crossed another change interrupt. If change still set, assume
4703 * FC ON for safety, let next loop through do the debounce.
4705 if (intstatus & I_HMB_FC_CHANGE) {
4706 intstatus &= ~I_HMB_FC_CHANGE;
4707 w_sdreg32(bus, I_HMB_FC_CHANGE,
4708 offsetof(struct sdpcmd_regs, intstatus), &retries);
4710 r_sdreg32(bus, &newstatus,
4711 offsetof(struct sdpcmd_regs, intstatus), &retries);
4712 bus->f1regdata += 2;
4714 !!(newstatus & (I_HMB_FC_STATE | I_HMB_FC_CHANGE));
4715 intstatus |= (newstatus & bus->hostintmask);
4718 /* Handle host mailbox indication */
4719 if (intstatus & I_HMB_HOST_INT) {
4720 intstatus &= ~I_HMB_HOST_INT;
4721 intstatus |= brcmf_sdbrcm_hostmail(bus);
4724 /* Generally don't ask for these, can get CRC errors... */
4725 if (intstatus & I_WR_OOSYNC) {
4726 BRCMF_ERROR(("Dongle reports WR_OOSYNC\n"));
4727 intstatus &= ~I_WR_OOSYNC;
4730 if (intstatus & I_RD_OOSYNC) {
4731 BRCMF_ERROR(("Dongle reports RD_OOSYNC\n"));
4732 intstatus &= ~I_RD_OOSYNC;
4735 if (intstatus & I_SBINT) {
4736 BRCMF_ERROR(("Dongle reports SBINT\n"));
4737 intstatus &= ~I_SBINT;
4740 /* Would be active due to wake-wlan in gSPI */
4741 if (intstatus & I_CHIPACTIVE) {
4742 BRCMF_INFO(("Dongle reports CHIPACTIVE\n"));
4743 intstatus &= ~I_CHIPACTIVE;
4746 /* Ignore frame indications if rxskip is set */
4748 intstatus &= ~I_HMB_FRAME_IND;
4750 /* On frame indication, read available frames */
4751 if (PKT_AVAILABLE()) {
4752 framecnt = brcmf_sdbrcm_readframes(bus, rxlimit, &rxdone);
4753 if (rxdone || bus->rxskip)
4754 intstatus &= ~I_HMB_FRAME_IND;
4755 rxlimit -= min(framecnt, rxlimit);
4758 /* Keep still-pending events for next scheduling */
4759 bus->intstatus = intstatus;
4762 /* Re-enable interrupts to detect new device events (mailbox, rx frame)
4763 * or clock availability. (Allows tx loop to check ipend if desired.)
4764 * (Unless register access seems hosed, as we may not be able to ACK...)
4766 if (bus->intr && bus->intdis && !brcmf_sdcard_regfail(card)) {
4767 BRCMF_INTR(("%s: enable SDIO interrupts, rxdone %d"
4768 " framecnt %d\n", __func__, rxdone, framecnt));
4769 bus->intdis = false;
4770 brcmf_sdcard_intr_enable(card);
4773 if (DATAOK(bus) && bus->ctrl_frame_stat &&
4774 (bus->clkstate == CLK_AVAIL)) {
4777 ret = brcmf_sdbrcm_send_buf(bus, brcmf_sdcard_cur_sbwad(card),
4778 SDIO_FUNC_2, F2SYNC, (u8 *) bus->ctrl_frame_buf,
4779 (u32) bus->ctrl_frame_len, NULL, NULL, NULL);
4782 /* On failure, abort the command and
4783 terminate the frame */
4784 BRCMF_INFO(("%s: sdio error %d, abort command and "
4785 "terminate frame.\n", __func__, ret));
4788 brcmf_sdcard_abort(card, SDIO_FUNC_2);
4790 brcmf_sdcard_cfg_write(card, SDIO_FUNC_1,
4791 SBSDIO_FUNC1_FRAMECTRL, SFC_WF_TERM,
4795 for (i = 0; i < 3; i++) {
4797 hi = brcmf_sdcard_cfg_read(card, SDIO_FUNC_1,
4798 SBSDIO_FUNC1_WFRAMEBCHI,
4800 lo = brcmf_sdcard_cfg_read(card, SDIO_FUNC_1,
4801 SBSDIO_FUNC1_WFRAMEBCLO,
4803 bus->f1regdata += 2;
4804 if ((hi == 0) && (lo == 0))
4810 bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQUENCE_WRAP;
4812 BRCMF_INFO(("Return_dpc value is : %d\n", ret));
4813 bus->ctrl_frame_stat = false;
4814 brcmf_sdbrcm_wait_event_wakeup(bus);
4816 /* Send queued frames (limit 1 if rx may still be pending) */
4817 else if ((bus->clkstate == CLK_AVAIL) && !bus->fcstate &&
4818 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) && txlimit
4820 framecnt = rxdone ? txlimit : min(txlimit, brcmf_txminmax);
4821 framecnt = brcmf_sdbrcm_sendfromq(bus, framecnt);
4822 txlimit -= framecnt;
4825 /* Resched if events or tx frames are pending,
4826 else await next interrupt */
4827 /* On failed register access, all bets are off:
4828 no resched or interrupts */
4829 if ((bus->drvr->busstate == BRCMF_BUS_DOWN) ||
4830 brcmf_sdcard_regfail(card)) {
4831 BRCMF_ERROR(("%s: failed backplane access over SDIO, halting "
4832 "operation %d\n", __func__,
4833 brcmf_sdcard_regfail(card)));
4834 bus->drvr->busstate = BRCMF_BUS_DOWN;
4836 } else if (bus->clkstate == CLK_PENDING) {
4837 BRCMF_INFO(("%s: rescheduled due to CLK_PENDING awaiting "
4838 "I_CHIPACTIVE interrupt\n", __func__));
4840 } else if (bus->intstatus || bus->ipend ||
4841 (!bus->fcstate && brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol)
4842 && DATAOK(bus)) || PKT_AVAILABLE()) {
4846 bus->dpc_sched = resched;
4848 /* If we're done for now, turn off clock request. */
4849 if ((bus->clkstate != CLK_PENDING)
4850 && bus->idletime == BRCMF_IDLE_IMMEDIATE) {
4851 bus->activity = false;
4852 brcmf_sdbrcm_clkctl(bus, CLK_NONE, false);
4855 brcmf_sdbrcm_sdunlock(bus);
4860 void brcmf_sdbrcm_isr(void *arg)
4862 struct brcmf_bus *bus = (struct brcmf_bus *) arg;
4863 struct brcmf_sdio_card *card;
4865 BRCMF_TRACE(("%s: Enter\n", __func__));
4868 BRCMF_ERROR(("%s : bus is null pointer , exit\n", __func__));
4873 if (bus->drvr->busstate == BRCMF_BUS_DOWN) {
4874 BRCMF_ERROR(("%s : bus is down. we have nothing to do\n",
4878 /* Count the interrupt call */
4882 /* Shouldn't get this interrupt if we're sleeping? */
4883 if (bus->sleeping) {
4884 BRCMF_ERROR(("INTERRUPT WHILE SLEEPING??\n"));
4888 /* Disable additional interrupts (is this needed now)? */
4890 BRCMF_INTR(("%s: disable SDIO interrupts\n", __func__));
4892 BRCMF_ERROR(("brcmf_sdbrcm_isr() w/o interrupt configured!\n"));
4894 brcmf_sdcard_intr_disable(card);
4897 #if defined(SDIO_ISR_THREAD)
4898 BRCMF_TRACE(("Calling brcmf_sdbrcm_dpc() from %s\n", __func__));
4899 while (brcmf_sdbrcm_dpc(bus))
4902 bus->dpc_sched = true;
4903 brcmf_sdbrcm_sched_dpc(bus);
4909 static void brcmf_sdbrcm_pktgen_init(struct brcmf_bus *bus)
4911 /* Default to specified length, or full range */
4912 if (brcmf_pktgen_len) {
4913 bus->pktgen_maxlen = min(brcmf_pktgen_len,
4914 BRCMF_MAX_PKTGEN_LEN);
4915 bus->pktgen_minlen = bus->pktgen_maxlen;
4917 bus->pktgen_maxlen = BRCMF_MAX_PKTGEN_LEN;
4918 bus->pktgen_minlen = 0;
4920 bus->pktgen_len = (u16) bus->pktgen_minlen;
4922 /* Default to per-watchdog burst with 10s print time */
4923 bus->pktgen_freq = 1;
4924 bus->pktgen_print = 10000 / brcmf_watchdog_ms;
4925 bus->pktgen_count = (brcmf_pktgen * brcmf_watchdog_ms + 999) / 1000;
4927 /* Default to echo mode */
4928 bus->pktgen_mode = BRCMF_PKTGEN_ECHO;
4929 bus->pktgen_stop = 1;
4932 static void brcmf_sdbrcm_pktgen(struct brcmf_bus *bus)
4934 struct sk_buff *pkt;
4940 /* Display current count if appropriate */
4941 if (bus->pktgen_print && (++bus->pktgen_ptick >= bus->pktgen_print)) {
4942 bus->pktgen_ptick = 0;
4943 printk(KERN_DEBUG "%s: send attempts %d rcvd %d\n",
4944 __func__, bus->pktgen_sent, bus->pktgen_rcvd);
4947 /* For recv mode, just make sure dongle has started sending */
4948 if (bus->pktgen_mode == BRCMF_PKTGEN_RECV) {
4949 if (!bus->pktgen_rcvd)
4950 brcmf_sdbrcm_sdtest_set(bus, true);
4954 /* Otherwise, generate or request the specified number of packets */
4955 for (pktcount = 0; pktcount < bus->pktgen_count; pktcount++) {
4956 /* Stop if total has been reached */
4957 if (bus->pktgen_total
4958 && (bus->pktgen_sent >= bus->pktgen_total)) {
4959 bus->pktgen_count = 0;
4963 /* Allocate an appropriate-sized packet */
4964 len = bus->pktgen_len;
4965 pkt = brcmu_pkt_buf_get_skb(
4966 len + SDPCM_HDRLEN + SDPCM_TEST_HDRLEN + BRCMF_SDALIGN,
4969 BRCMF_ERROR(("%s: brcmu_pkt_buf_get_skb failed!\n",
4973 PKTALIGN(pkt, (len + SDPCM_HDRLEN + SDPCM_TEST_HDRLEN),
4975 data = (u8 *) (pkt->data) + SDPCM_HDRLEN;
4977 /* Write test header cmd and extra based on mode */
4978 switch (bus->pktgen_mode) {
4979 case BRCMF_PKTGEN_ECHO:
4980 *data++ = SDPCM_TEST_ECHOREQ;
4981 *data++ = (u8) bus->pktgen_sent;
4984 case BRCMF_PKTGEN_SEND:
4985 *data++ = SDPCM_TEST_DISCARD;
4986 *data++ = (u8) bus->pktgen_sent;
4989 case BRCMF_PKTGEN_RXBURST:
4990 *data++ = SDPCM_TEST_BURST;
4991 *data++ = (u8) bus->pktgen_count;
4995 BRCMF_ERROR(("Unrecognized pktgen mode %d\n",
4997 brcmu_pkt_buf_free_skb(pkt, true);
4998 bus->pktgen_count = 0;
5002 /* Write test header length field */
5003 *data++ = (len >> 0);
5004 *data++ = (len >> 8);
5006 /* Then fill in the remainder -- N/A for burst,
5008 for (fillbyte = 0; fillbyte < len; fillbyte++)
5010 SDPCM_TEST_FILL(fillbyte, (u8) bus->pktgen_sent);
5013 if (BRCMF_BYTES_ON() && BRCMF_DATA_ON()) {
5014 data = (u8 *) (pkt->data) + SDPCM_HDRLEN;
5015 printk(KERN_DEBUG "brcmf_sdbrcm_pktgen: Tx Data:\n");
5016 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, data,
5017 pkt->len - SDPCM_HDRLEN);
5022 if (brcmf_sdbrcm_txpkt(bus, pkt, SDPCM_TEST_CHANNEL, true)) {
5024 if (bus->pktgen_stop
5025 && bus->pktgen_stop == bus->pktgen_fail)
5026 bus->pktgen_count = 0;
5030 /* Bump length if not fixed, wrap at max */
5031 if (++bus->pktgen_len > bus->pktgen_maxlen)
5032 bus->pktgen_len = (u16) bus->pktgen_minlen;
5034 /* Special case for burst mode: just send one request! */
5035 if (bus->pktgen_mode == BRCMF_PKTGEN_RXBURST)
5040 static void brcmf_sdbrcm_sdtest_set(struct brcmf_bus *bus, bool start)
5042 struct sk_buff *pkt;
5045 /* Allocate the packet */
5046 pkt = brcmu_pkt_buf_get_skb(SDPCM_HDRLEN + SDPCM_TEST_HDRLEN +
5047 BRCMF_SDALIGN, true);
5049 BRCMF_ERROR(("%s: brcmu_pkt_buf_get_skb failed!\n", __func__));
5052 PKTALIGN(pkt, (SDPCM_HDRLEN + SDPCM_TEST_HDRLEN), BRCMF_SDALIGN);
5053 data = (u8 *) (pkt->data) + SDPCM_HDRLEN;
5055 /* Fill in the test header */
5056 *data++ = SDPCM_TEST_SEND;
5058 *data++ = (bus->pktgen_maxlen >> 0);
5059 *data++ = (bus->pktgen_maxlen >> 8);
5062 if (brcmf_sdbrcm_txpkt(bus, pkt, SDPCM_TEST_CHANNEL, true))
5067 brcmf_sdbrcm_checkdied(struct brcmf_bus *bus, struct sk_buff *pkt, uint seq)
5077 /* Check for min length */
5079 if (pktlen < SDPCM_TEST_HDRLEN) {
5080 BRCMF_ERROR(("brcmf_sdbrcm_checkdied: toss runt frame, pktlen "
5082 brcmu_pkt_buf_free_skb(pkt, false);
5086 /* Extract header fields */
5091 len += *data++ << 8;
5093 /* Check length for relevant commands */
5094 if (cmd == SDPCM_TEST_DISCARD || cmd == SDPCM_TEST_ECHOREQ
5095 || cmd == SDPCM_TEST_ECHORSP) {
5096 if (pktlen != len + SDPCM_TEST_HDRLEN) {
5097 BRCMF_ERROR(("brcmf_sdbrcm_checkdied: frame length "
5098 "mismatch, pktlen %d seq %d"
5099 " cmd %d extra %d len %d\n",
5100 pktlen, seq, cmd, extra, len));
5101 brcmu_pkt_buf_free_skb(pkt, false);
5106 /* Process as per command */
5108 case SDPCM_TEST_ECHOREQ:
5109 /* Rx->Tx turnaround ok (even on NDIS w/current
5111 *(u8 *) (pkt->data) = SDPCM_TEST_ECHORSP;
5112 if (brcmf_sdbrcm_txpkt(bus, pkt, SDPCM_TEST_CHANNEL, true) == 0)
5116 brcmu_pkt_buf_free_skb(pkt, false);
5121 case SDPCM_TEST_ECHORSP:
5122 if (bus->ext_loop) {
5123 brcmu_pkt_buf_free_skb(pkt, false);
5128 for (offset = 0; offset < len; offset++, data++) {
5129 if (*data != SDPCM_TEST_FILL(offset, extra)) {
5130 BRCMF_ERROR(("brcmf_sdbrcm_checkdied: echo"
5132 "offset %d (len %d) "
5133 "expect 0x%02x rcvd 0x%02x\n",
5135 SDPCM_TEST_FILL(offset, extra),
5140 brcmu_pkt_buf_free_skb(pkt, false);
5144 case SDPCM_TEST_DISCARD:
5145 brcmu_pkt_buf_free_skb(pkt, false);
5149 case SDPCM_TEST_BURST:
5150 case SDPCM_TEST_SEND:
5152 BRCMF_INFO(("brcmf_sdbrcm_checkdied: unsupported or unknown "
5153 "command, pktlen %d seq %d" " cmd %d extra %d"
5154 " len %d\n", pktlen, seq, cmd, extra, len));
5155 brcmu_pkt_buf_free_skb(pkt, false);
5159 /* For recv mode, stop at limie (and tell dongle to stop sending) */
5160 if (bus->pktgen_mode == BRCMF_PKTGEN_RECV) {
5161 if (bus->pktgen_total
5162 && (bus->pktgen_rcvd >= bus->pktgen_total)) {
5163 bus->pktgen_count = 0;
5164 brcmf_sdbrcm_sdtest_set(bus, false);
5170 extern bool brcmf_sdbrcm_bus_watchdog(struct brcmf_pub *drvr)
5172 struct brcmf_bus *bus;
5174 BRCMF_TIMER(("%s: Enter\n", __func__));
5178 if (bus->drvr->dongle_reset)
5181 /* Ignore the timer if simulating bus down */
5185 brcmf_sdbrcm_sdlock(bus);
5187 /* Poll period: check device if appropriate. */
5188 if (bus->poll && (++bus->polltick >= bus->pollrate)) {
5191 /* Reset poll tick */
5194 /* Check device if no interrupts */
5195 if (!bus->intr || (bus->intrcount == bus->lastintrs)) {
5197 if (!bus->dpc_sched) {
5199 devpend = brcmf_sdcard_cfg_read(bus->card,
5200 SDIO_FUNC_0, SDIO_CCCR_INTx,
5203 devpend & (INTR_STATUS_FUNC1 |
5207 /* If there is something, make like the ISR and
5213 brcmf_sdcard_intr_disable(bus->card);
5215 bus->dpc_sched = true;
5216 brcmf_sdbrcm_sched_dpc(bus);
5221 /* Update interrupt tracking */
5222 bus->lastintrs = bus->intrcount;
5225 /* Poll for console output periodically */
5226 if (drvr->busstate == BRCMF_BUS_DATA && brcmf_console_ms != 0) {
5227 bus->console.count += brcmf_watchdog_ms;
5228 if (bus->console.count >= brcmf_console_ms) {
5229 bus->console.count -= brcmf_console_ms;
5230 /* Make sure backplane clock is on */
5231 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
5232 if (brcmf_sdbrcm_readconsole(bus) < 0)
5233 brcmf_console_ms = 0; /* On error,
5240 /* Generate packets if configured */
5241 if (bus->pktgen_count && (++bus->pktgen_tick >= bus->pktgen_freq)) {
5242 /* Make sure backplane clock is on */
5243 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
5244 bus->pktgen_tick = 0;
5245 brcmf_sdbrcm_pktgen(bus);
5249 /* On idle timeout clear activity flag and/or turn off clock */
5250 if ((bus->idletime > 0) && (bus->clkstate == CLK_AVAIL)) {
5251 if (++bus->idlecount >= bus->idletime) {
5253 if (bus->activity) {
5254 bus->activity = false;
5255 brcmf_sdbrcm_wd_timer(bus, brcmf_watchdog_ms);
5257 brcmf_sdbrcm_clkctl(bus, CLK_NONE, false);
5262 brcmf_sdbrcm_sdunlock(bus);
5268 static int brcmf_sdbrcm_bus_console_in(struct brcmf_pub *drvr,
5269 unsigned char *msg, uint msglen)
5271 struct brcmf_bus *bus = drvr->bus;
5274 struct sk_buff *pkt;
5276 /* Address could be zero if CONSOLE := 0 in dongle Makefile */
5277 if (bus->console_addr == 0)
5280 /* Exclusive bus access */
5281 brcmf_sdbrcm_sdlock(bus);
5283 /* Don't allow input if dongle is in reset */
5284 if (bus->drvr->dongle_reset) {
5285 brcmf_sdbrcm_sdunlock(bus);
5289 /* Request clock to allow SDIO accesses */
5291 /* No pend allowed since txpkt is called later, ht clk has to be on */
5292 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
5294 /* Zero cbuf_index */
5295 addr = bus->console_addr + offsetof(struct rte_console, cbuf_idx);
5296 val = cpu_to_le32(0);
5297 rv = brcmf_sdbrcm_membytes(bus, true, addr, (u8 *)&val, sizeof(val));
5301 /* Write message into cbuf */
5302 addr = bus->console_addr + offsetof(struct rte_console, cbuf);
5303 rv = brcmf_sdbrcm_membytes(bus, true, addr, (u8 *)msg, msglen);
5307 /* Write length into vcons_in */
5308 addr = bus->console_addr + offsetof(struct rte_console, vcons_in);
5309 val = cpu_to_le32(msglen);
5310 rv = brcmf_sdbrcm_membytes(bus, true, addr, (u8 *)&val, sizeof(val));
5314 /* Bump dongle by sending an empty event pkt.
5315 * sdpcm_sendup (RX) checks for virtual console input.
5317 pkt = brcmu_pkt_buf_get_skb(4 + SDPCM_RESERVE);
5318 if ((pkt != NULL) && bus->clkstate == CLK_AVAIL)
5319 brcmf_sdbrcm_txpkt(bus, pkt, SDPCM_EVENT_CHANNEL, true);
5322 if ((bus->idletime == BRCMF_IDLE_IMMEDIATE) && !bus->dpc_sched) {
5323 bus->activity = false;
5324 brcmf_sdbrcm_clkctl(bus, CLK_NONE, true);
5327 brcmf_sdbrcm_sdunlock(bus);
5333 static bool brcmf_sdbrcm_chipmatch(u16 chipid)
5335 if (chipid == BCM4325_CHIP_ID)
5337 if (chipid == BCM4329_CHIP_ID)
5339 if (chipid == BCM4319_CHIP_ID)
5344 static void *brcmf_sdbrcm_probe(u16 venid, u16 devid, u16 bus_no,
5345 u16 slot, u16 func, uint bustype, u32 regsva,
5349 struct brcmf_bus *bus;
5351 /* Init global variables at run-time, not as part of the declaration.
5352 * This is required to support init/de-init of the driver.
5354 * of globals as part of the declaration results in non-deterministic
5355 * behavior since the value of the globals may be different on the
5356 * first time that the driver is initialized vs subsequent
5359 brcmf_txbound = BRCMF_TXBOUND;
5360 brcmf_rxbound = BRCMF_RXBOUND;
5361 brcmf_alignctl = true;
5363 brcmf_readahead = true;
5365 brcmf_dongle_memsize = 0;
5366 brcmf_txminmax = BRCMF_TXMINMAX;
5372 BRCMF_TRACE(("%s: Enter\n", __func__));
5373 BRCMF_INFO(("%s: venid 0x%04x devid 0x%04x\n", __func__, venid, devid));
5375 /* We make an assumption about address window mappings:
5376 * regsva == SI_ENUM_BASE*/
5378 /* SDIO car passes venid and devid based on CIS parsing -- but
5380 * means early parse could fail, so here we should get either an ID
5381 * we recognize OR (-1) indicating we must request power first.
5383 /* Check the Vendor ID */
5386 case PCI_VENDOR_ID_BROADCOM:
5389 BRCMF_ERROR(("%s: unknown vendor: 0x%04x\n", __func__, venid));
5393 /* Check the Device ID and make sure it's one that we support */
5395 case BCM4325_D11DUAL_ID: /* 4325 802.11a/g id */
5396 case BCM4325_D11G_ID: /* 4325 802.11g 2.4Ghz band id */
5397 case BCM4325_D11A_ID: /* 4325 802.11a 5Ghz band id */
5398 BRCMF_INFO(("%s: found 4325 Dongle\n", __func__));
5400 case BCM4329_D11NDUAL_ID: /* 4329 802.11n dualband device */
5401 case BCM4329_D11N2G_ID: /* 4329 802.11n 2.4G device */
5402 case BCM4329_D11N5G_ID: /* 4329 802.11n 5G device */
5404 BRCMF_INFO(("%s: found 4329 Dongle\n", __func__));
5406 case BCM4319_D11N_ID: /* 4319 802.11n id */
5407 case BCM4319_D11N2G_ID: /* 4319 802.11n2g id */
5408 case BCM4319_D11N5G_ID: /* 4319 802.11n5g id */
5409 BRCMF_INFO(("%s: found 4319 Dongle\n", __func__));
5412 BRCMF_INFO(("%s: allow device id 0, will check chip"
5413 " internals\n", __func__));
5417 BRCMF_ERROR(("%s: skipping 0x%04x/0x%04x, not a dongle\n",
5418 __func__, venid, devid));
5422 /* Allocate private bus interface state */
5423 bus = kzalloc(sizeof(struct brcmf_bus), GFP_ATOMIC);
5425 BRCMF_ERROR(("%s: kmalloc of struct dhd_bus failed\n",
5430 bus->cl_devid = (u16) devid;
5431 bus->bus = BRCMF_BUS;
5432 bus->tx_seq = SDPCM_SEQUENCE_WRAP - 1;
5433 bus->usebufpool = false; /* Use bufpool if allocated,
5434 else use locally malloced rxbuf */
5436 /* attempt to attach to the dongle */
5437 if (!(brcmf_sdbrcm_probe_attach(bus, card, regsva, devid))) {
5438 BRCMF_ERROR(("%s: brcmf_sdbrcm_probe_attach failed\n",
5443 spin_lock_init(&bus->txqlock);
5444 init_waitqueue_head(&bus->ctrl_wait);
5446 /* Set up the watchdog timer */
5447 init_timer(&bus->timer);
5448 bus->timer.data = (unsigned long)bus;
5449 bus->timer.function = brcmf_sdbrcm_watchdog;
5451 /* Initialize thread based operation and lock */
5452 if ((brcmf_watchdog_prio >= 0) && (brcmf_dpc_prio >= 0)) {
5453 bus->threads_only = true;
5454 sema_init(&bus->sdsem, 1);
5456 bus->threads_only = false;
5457 spin_lock_init(&bus->sdlock);
5460 if (brcmf_dpc_prio >= 0) {
5461 /* Initialize watchdog thread */
5462 init_completion(&bus->watchdog_wait);
5463 bus->watchdog_tsk = kthread_run(brcmf_sdbrcm_watchdog_thread,
5464 bus, "brcmf_watchdog");
5465 if (IS_ERR(bus->watchdog_tsk)) {
5467 "brcmf_watchdog thread failed to start\n");
5468 bus->watchdog_tsk = NULL;
5471 bus->watchdog_tsk = NULL;
5473 /* Set up the bottom half handler */
5474 if (brcmf_dpc_prio >= 0) {
5475 /* Initialize DPC thread */
5476 init_completion(&bus->dpc_wait);
5477 bus->dpc_tsk = kthread_run(brcmf_sdbrcm_dpc_thread,
5479 if (IS_ERR(bus->dpc_tsk)) {
5481 "brcmf_dpc thread failed to start\n");
5482 bus->dpc_tsk = NULL;
5485 tasklet_init(&bus->tasklet, brcmf_sdbrcm_dpc_tasklet,
5486 (unsigned long)bus);
5487 bus->dpc_tsk = NULL;
5490 /* Attach to the brcmf/OS/network interface */
5491 bus->drvr = brcmf_attach(bus, SDPCM_RESERVE);
5493 BRCMF_ERROR(("%s: brcmf_attach failed\n", __func__));
5497 /* Allocate buffers */
5498 if (!(brcmf_sdbrcm_probe_malloc(bus, card))) {
5499 BRCMF_ERROR(("%s: brcmf_sdbrcm_probe_malloc failed\n",
5504 if (!(brcmf_sdbrcm_probe_init(bus, card))) {
5505 BRCMF_ERROR(("%s: brcmf_sdbrcm_probe_init failed\n", __func__));
5509 /* Register interrupt callback, but mask it (not operational yet). */
5510 BRCMF_INTR(("%s: disable SDIO interrupts (not interested yet)\n",
5512 brcmf_sdcard_intr_disable(card);
5513 ret = brcmf_sdcard_intr_reg(card, brcmf_sdbrcm_isr, bus);
5515 BRCMF_ERROR(("%s: FAILED: sdcard_intr_reg returned %d\n",
5519 BRCMF_INTR(("%s: registered SDIO interrupt function ok\n", __func__));
5521 BRCMF_INFO(("%s: completed!!\n", __func__));
5523 /* if firmware path present try to download and bring up bus */
5524 ret = brcmf_bus_start(bus->drvr);
5526 if (ret == -ENOLINK) {
5527 BRCMF_ERROR(("%s: dongle is not responding\n",
5532 /* Ok, have the per-port tell the stack we're open for business */
5533 if (brcmf_net_attach(bus->drvr, 0) != 0) {
5534 BRCMF_ERROR(("%s: Net attach failed!!\n", __func__));
5541 brcmf_sdbrcm_release(bus);
5546 brcmf_sdbrcm_probe_attach(struct brcmf_bus *bus, void *card, u32 regsva,
5552 bus->alp_only = true;
5554 /* Return the window to backplane enumeration space for core access */
5555 if (brcmf_sdbrcm_set_siaddr_window(bus, SI_ENUM_BASE))
5556 BRCMF_ERROR(("%s: FAILED to return to SI_ENUM_BASE\n",
5560 printk(KERN_DEBUG "F1 signature read @0x18000000=0x%4x\n",
5561 brcmf_sdcard_reg_read(bus->card, SI_ENUM_BASE, 4));
5566 * Force PLL off until brcmf_sdbrcm_chip_attach()
5567 * programs PLL control regs
5570 brcmf_sdcard_cfg_write(card, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
5571 BRCMF_INIT_CLKCTL1, &err);
5574 brcmf_sdcard_cfg_read(card, SDIO_FUNC_1,
5575 SBSDIO_FUNC1_CHIPCLKCSR, &err);
5577 if (err || ((clkctl & ~SBSDIO_AVBITS) != BRCMF_INIT_CLKCTL1)) {
5578 BRCMF_ERROR(("brcmf_sdbrcm_probe: ChipClkCSR access: err %d"
5579 " wrote 0x%02x read 0x%02x\n",
5580 err, BRCMF_INIT_CLKCTL1, clkctl));
5584 if (brcmf_sdbrcm_chip_attach(bus, regsva)) {
5585 BRCMF_ERROR(("%s: brcmf_sdbrcm_chip_attach failed!\n",
5590 if (!brcmf_sdbrcm_chipmatch((u16) bus->ci->chip)) {
5591 BRCMF_ERROR(("%s: unsupported chip: 0x%04x\n",
5592 __func__, bus->ci->chip));
5596 brcmf_sdbrcm_sdiod_drive_strength_init(bus, brcmf_sdiod_drive_strength);
5598 /* Get info on the ARM and SOCRAM cores... */
5599 if (!BRCMF_NOPMU(bus)) {
5600 brcmf_sdcard_reg_read(bus->card,
5601 CORE_SB(bus->ci->armcorebase, sbidhigh), 4);
5602 bus->orig_ramsize = bus->ci->ramsize;
5603 if (!(bus->orig_ramsize)) {
5604 BRCMF_ERROR(("%s: failed to find SOCRAM memory!\n",
5608 bus->ramsize = bus->orig_ramsize;
5609 if (brcmf_dongle_memsize)
5610 brcmf_sdbrcm_setmemsize(bus, brcmf_dongle_memsize);
5612 BRCMF_ERROR(("DHD: dongle ram size is set to %d(orig %d)\n",
5613 bus->ramsize, bus->orig_ramsize));
5616 /* Set core control so an SDIO reset does a backplane reset */
5617 OR_REG(bus->ci->buscorebase + offsetof(struct sdpcmd_regs,
5621 brcmu_pktq_init(&bus->txq, (PRIOMASK + 1), TXQLEN);
5623 /* Locate an appropriately-aligned portion of hdrbuf */
5624 bus->rxhdr = (u8 *) roundup((unsigned long)&bus->hdrbuf[0],
5627 /* Set the poll and/or interrupt flags */
5628 bus->intr = (bool) brcmf_intr;
5629 bus->poll = (bool) brcmf_poll;
5639 static bool brcmf_sdbrcm_probe_malloc(struct brcmf_bus *bus, void *card)
5641 BRCMF_TRACE(("%s: Enter\n", __func__));
5643 if (bus->drvr->maxctl) {
5645 roundup((bus->drvr->maxctl + SDPCM_HDRLEN),
5646 ALIGNMENT) + BRCMF_SDALIGN;
5647 bus->rxbuf = kmalloc(bus->rxblen, GFP_ATOMIC);
5648 if (!(bus->rxbuf)) {
5649 BRCMF_ERROR(("%s: kmalloc of %d-byte rxbuf failed\n",
5650 __func__, bus->rxblen));
5655 /* Allocate buffer to receive glomed packet */
5656 bus->databuf = kmalloc(MAX_DATA_BUF, GFP_ATOMIC);
5657 if (!(bus->databuf)) {
5658 BRCMF_ERROR(("%s: kmalloc of %d-byte databuf failed\n",
5659 __func__, MAX_DATA_BUF));
5660 /* release rxbuf which was already located as above */
5666 /* Align the buffer */
5667 if ((unsigned long)bus->databuf % BRCMF_SDALIGN)
5668 bus->dataptr = bus->databuf + (BRCMF_SDALIGN -
5669 ((unsigned long)bus->databuf % BRCMF_SDALIGN));
5671 bus->dataptr = bus->databuf;
5679 static bool brcmf_sdbrcm_probe_init(struct brcmf_bus *bus, void *card)
5683 BRCMF_TRACE(("%s: Enter\n", __func__));
5686 brcmf_sdbrcm_pktgen_init(bus);
5689 /* Disable F2 to clear any intermediate frame state on the dongle */
5690 brcmf_sdcard_cfg_write(card, SDIO_FUNC_0, SDIO_CCCR_IOEx,
5691 SDIO_FUNC_ENABLE_1, NULL);
5693 bus->drvr->busstate = BRCMF_BUS_DOWN;
5694 bus->sleeping = false;
5695 bus->rxflow = false;
5697 /* Done with backplane-dependent accesses, can drop clock... */
5698 brcmf_sdcard_cfg_write(card, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR, 0,
5701 /* ...and initialize clock/power states */
5702 bus->clkstate = CLK_SDONLY;
5703 bus->idletime = (s32) brcmf_idletime;
5704 bus->idleclock = BRCMF_IDLE_ACTIVE;
5706 /* Query the F2 block size, set roundup accordingly */
5708 if (brcmf_sdcard_iovar_op(card, "sd_blocksize", &fnum, sizeof(s32),
5709 &bus->blocksize, sizeof(s32), false) != 0) {
5711 BRCMF_ERROR(("%s: fail on %s get\n", __func__, "sd_blocksize"));
5713 BRCMF_INFO(("%s: Initial value for %s is %d\n",
5714 __func__, "sd_blocksize", bus->blocksize));
5716 bus->roundup = min(max_roundup, bus->blocksize);
5718 /* Query if bus module supports packet chaining,
5719 default to use if supported */
5720 if (brcmf_sdcard_iovar_op(card, "sd_rxchain", NULL, 0,
5721 &bus->sd_rxchain, sizeof(s32),
5723 bus->sd_rxchain = false;
5725 BRCMF_INFO(("%s: bus module (through sdiocard API) %s"
5726 " chaining\n", __func__, bus->sd_rxchain
5727 ? "supports" : "does not support"));
5729 bus->use_rxchain = (bool) bus->sd_rxchain;
5735 brcmf_sdbrcm_download_firmware(struct brcmf_bus *bus, void *card)
5739 /* Download the firmware */
5740 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
5742 ret = _brcmf_sdbrcm_download_firmware(bus) == 0;
5744 brcmf_sdbrcm_clkctl(bus, CLK_SDONLY, false);
5749 /* Detach and free everything */
5750 static void brcmf_sdbrcm_release(struct brcmf_bus *bus)
5752 BRCMF_TRACE(("%s: Enter\n", __func__));
5755 /* De-register interrupt handler */
5756 brcmf_sdcard_intr_disable(bus->card);
5757 brcmf_sdcard_intr_dereg(bus->card);
5760 brcmf_detach(bus->drvr);
5761 brcmf_sdbrcm_release_dongle(bus);
5765 brcmf_sdbrcm_release_malloc(bus);
5770 BRCMF_TRACE(("%s: Disconnected\n", __func__));
5773 static void brcmf_sdbrcm_release_malloc(struct brcmf_bus *bus)
5775 BRCMF_TRACE(("%s: Enter\n", __func__));
5777 if (bus->drvr && bus->drvr->dongle_reset)
5781 bus->rxctl = bus->rxbuf = NULL;
5784 kfree(bus->databuf);
5785 bus->databuf = NULL;
5788 static void brcmf_sdbrcm_release_dongle(struct brcmf_bus *bus)
5790 BRCMF_TRACE(("%s: Enter\n", __func__));
5792 if (bus->drvr && bus->drvr->dongle_reset)
5796 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
5797 brcmf_sdbrcm_clkctl(bus, CLK_NONE, false);
5798 brcmf_sdbrcm_chip_detach(bus);
5799 if (bus->vars && bus->varsz)
5804 BRCMF_TRACE(("%s: Disconnected\n", __func__));
5807 static void brcmf_sdbrcm_disconnect(void *ptr)
5809 struct brcmf_bus *bus = (struct brcmf_bus *)ptr;
5811 BRCMF_TRACE(("%s: Enter\n", __func__));
5814 brcmf_sdbrcm_release(bus);
5817 BRCMF_TRACE(("%s: Disconnected\n", __func__));
5820 /* Register/Unregister functions are called by the main DHD entry
5821 * point (e.g. module insertion) to link with the bus driver, in
5822 * order to look for or await the device.
5825 static struct brcmf_sdioh_driver brcmf_sdio = {
5827 brcmf_sdbrcm_disconnect
5830 int brcmf_bus_register(void)
5832 BRCMF_TRACE(("%s: Enter\n", __func__));
5834 /* Sanity check on the module parameters */
5836 /* Both watchdog and DPC as tasklets are ok */
5837 if ((brcmf_watchdog_prio < 0) && (brcmf_dpc_prio < 0))
5840 /* If both watchdog and DPC are threads, TX must be deferred */
5841 if ((brcmf_watchdog_prio >= 0) && (brcmf_dpc_prio >= 0)
5842 && brcmf_deferred_tx)
5845 BRCMF_ERROR(("Invalid module parameters.\n"));
5849 return brcmf_sdio_register(&brcmf_sdio);
5852 void brcmf_bus_unregister(void)
5854 BRCMF_TRACE(("%s: Enter\n", __func__));
5856 brcmf_sdio_unregister();
5859 static int brcmf_sdbrcm_download_code_file(struct brcmf_bus *bus)
5863 u8 *memblock = NULL, *memptr;
5866 BRCMF_INFO(("%s: Enter\n", __func__));
5868 bus->fw_name = BCM4329_FW_NAME;
5869 ret = request_firmware(&bus->firmware, bus->fw_name,
5870 &gInstance->func[2]->dev);
5872 BRCMF_ERROR(("%s: Fail to request firmware %d\n",
5878 memptr = memblock = kmalloc(MEMBLOCK + BRCMF_SDALIGN, GFP_ATOMIC);
5879 if (memblock == NULL) {
5880 BRCMF_ERROR(("%s: Failed to allocate memory %d bytes\n",
5881 __func__, MEMBLOCK));
5885 if ((u32)(unsigned long)memblock % BRCMF_SDALIGN)
5886 memptr += (BRCMF_SDALIGN -
5887 ((u32)(unsigned long)memblock % BRCMF_SDALIGN));
5889 /* Download image */
5891 brcmf_sdbrcm_get_image((char *)memptr, MEMBLOCK, bus))) {
5892 ret = brcmf_sdbrcm_membytes(bus, true, offset, memptr, len);
5894 BRCMF_ERROR(("%s: error %d on writing %d membytes at "
5895 "0x%08x\n", __func__, ret, MEMBLOCK,
5906 release_firmware(bus->firmware);
5913 * ProcessVars:Takes a buffer of "<var>=<value>\n" lines read from a file
5914 * and ending in a NUL.
5915 * Removes carriage returns, empty lines, comment lines, and converts
5917 * Shortens buffer as needed and pads with NULs. End of buffer is marked
5921 static uint brcmf_process_nvram_vars(char *varbuf, uint len)
5930 findNewline = false;
5933 for (n = 0; n < len; n++) {
5936 if (varbuf[n] == '\r')
5938 if (findNewline && varbuf[n] != '\n')
5940 findNewline = false;
5941 if (varbuf[n] == '#') {
5945 if (varbuf[n] == '\n') {
5955 buf_len = dp - varbuf;
5957 while (dp < varbuf + n)
5963 static int brcmf_sdbrcm_download_nvram(struct brcmf_bus *bus)
5966 char *memblock = NULL;
5970 bus->nv_name = BCM4329_NV_NAME;
5971 ret = request_firmware(&bus->firmware, bus->nv_name,
5972 &gInstance->func[2]->dev);
5974 BRCMF_ERROR(("%s: Fail to request nvram %d\n", __func__, ret));
5979 memblock = kmalloc(MEMBLOCK, GFP_ATOMIC);
5980 if (memblock == NULL) {
5981 BRCMF_ERROR(("%s: Failed to allocate memory %d bytes\n",
5982 __func__, MEMBLOCK));
5987 len = brcmf_sdbrcm_get_image(memblock, MEMBLOCK, bus);
5989 if (len > 0 && len < MEMBLOCK) {
5990 bufp = (char *)memblock;
5992 len = brcmf_process_nvram_vars(bufp, len);
5996 ret = brcmf_sdbrcm_downloadvars(bus, memblock, len + 1);
5998 BRCMF_ERROR(("%s: error downloading vars: %d\n",
6001 BRCMF_ERROR(("%s: error reading nvram file: %d\n",
6009 release_firmware(bus->firmware);
6015 static int _brcmf_sdbrcm_download_firmware(struct brcmf_bus *bus)
6019 /* Keep arm in reset */
6020 if (brcmf_sdbrcm_download_state(bus, true)) {
6021 BRCMF_ERROR(("%s: error placing ARM core in reset\n",
6026 /* External image takes precedence if specified */
6027 if (brcmf_sdbrcm_download_code_file(bus)) {
6028 BRCMF_ERROR(("%s: dongle image file download failed\n",
6033 /* External nvram takes precedence if specified */
6034 if (brcmf_sdbrcm_download_nvram(bus)) {
6035 BRCMF_ERROR(("%s: dongle nvram file download failed\n",
6039 /* Take arm out of reset */
6040 if (brcmf_sdbrcm_download_state(bus, false)) {
6041 BRCMF_ERROR(("%s: error getting out of ARM core reset\n",
6054 brcmf_sdbrcm_send_buf(struct brcmf_bus *bus, u32 addr, uint fn, uint flags,
6055 u8 *buf, uint nbytes, struct sk_buff *pkt,
6056 void (*complete)(void *handle, int status,
6060 return brcmf_sdcard_send_buf
6061 (bus->card, addr, fn, flags, buf, nbytes, pkt, complete,
6065 int brcmf_bus_devreset(struct brcmf_pub *drvr, u8 flag)
6068 struct brcmf_bus *bus;
6073 brcmf_sdbrcm_wd_timer(bus, 0);
6074 if (!bus->drvr->dongle_reset) {
6075 /* Expect app to have torn down any
6076 connection before calling */
6077 /* Stop the bus, disable F2 */
6078 brcmf_sdbrcm_bus_stop(bus, false);
6080 /* Clean tx/rx buffer pointers,
6081 detach from the dongle */
6082 brcmf_sdbrcm_release_dongle(bus);
6084 bus->drvr->dongle_reset = true;
6085 bus->drvr->up = false;
6087 BRCMF_TRACE(("%s: WLAN OFF DONE\n", __func__));
6088 /* App can now remove power from device */
6092 /* App must have restored power to device before calling */
6094 BRCMF_TRACE(("\n\n%s: == WLAN ON ==\n", __func__));
6096 if (bus->drvr->dongle_reset) {
6099 /* Attempt to re-attach & download */
6100 if (brcmf_sdbrcm_probe_attach(bus, bus->card,
6103 /* Attempt to download binary to the dongle */
6104 if (brcmf_sdbrcm_probe_init(bus, bus->card)) {
6105 /* Re-init bus, enable F2 transfer */
6106 brcmf_sdbrcm_bus_init(bus->drvr, false);
6108 bus->drvr->dongle_reset = false;
6109 bus->drvr->up = true;
6111 BRCMF_TRACE(("%s: WLAN ON DONE\n",
6118 bcmerror = -EISCONN;
6119 BRCMF_ERROR(("%s: Set DEVRESET=false invoked when"
6120 " device is on\n", __func__));
6123 brcmf_sdbrcm_wd_timer(bus, brcmf_watchdog_ms);
6129 brcmf_sdbrcm_chip_recognition(struct brcmf_sdio_card *card,
6130 struct chip_info *ci, u32 regs)
6136 * Chipid is assume to be at offset 0 from regs arg
6137 * For different chiptypes or old sdio hosts w/o chipcommon,
6138 * other ways of recognition should be added here.
6140 ci->cccorebase = regs;
6141 regdata = brcmf_sdcard_reg_read(card,
6142 CORE_CC_REG(ci->cccorebase, chipid), 4);
6143 ci->chip = regdata & CID_ID_MASK;
6144 ci->chiprev = (regdata & CID_REV_MASK) >> CID_REV_SHIFT;
6146 BRCMF_INFO(("%s: chipid=0x%x chiprev=%d\n",
6147 __func__, ci->chip, ci->chiprev));
6149 /* Address of cores for new chips should be added here */
6151 case BCM4329_CHIP_ID:
6152 ci->buscorebase = BCM4329_CORE_BUS_BASE;
6153 ci->ramcorebase = BCM4329_CORE_SOCRAM_BASE;
6154 ci->armcorebase = BCM4329_CORE_ARM_BASE;
6155 ci->ramsize = BCM4329_RAMSIZE;
6158 BRCMF_ERROR(("%s: chipid 0x%x is not supported\n",
6159 __func__, ci->chip));
6163 regdata = brcmf_sdcard_reg_read(card,
6164 CORE_SB(ci->cccorebase, sbidhigh), 4);
6165 ci->ccrev = SBCOREREV(regdata);
6167 regdata = brcmf_sdcard_reg_read(card,
6168 CORE_CC_REG(ci->cccorebase, pmucapabilities), 4);
6169 ci->pmurev = regdata & PCAP_REV_MASK;
6171 regdata = brcmf_sdcard_reg_read(card,
6172 CORE_SB(ci->buscorebase, sbidhigh), 4);
6173 ci->buscorerev = SBCOREREV(regdata);
6174 ci->buscoretype = (regdata & SBIDH_CC_MASK) >> SBIDH_CC_SHIFT;
6176 BRCMF_INFO(("%s: ccrev=%d, pmurev=%d, buscore rev/type=%d/0x%x\n",
6177 __func__, ci->ccrev, ci->pmurev,
6178 ci->buscorerev, ci->buscoretype));
6180 /* get chipcommon capabilites */
6181 ci->cccaps = brcmf_sdcard_reg_read(card,
6182 CORE_CC_REG(ci->cccorebase, capabilities), 4);
6188 brcmf_sdbrcm_chip_disablecore(struct brcmf_sdio_card *card, u32 corebase)
6192 regdata = brcmf_sdcard_reg_read(card,
6193 CORE_SB(corebase, sbtmstatelow), 4);
6194 if (regdata & SBTML_RESET)
6197 regdata = brcmf_sdcard_reg_read(card,
6198 CORE_SB(corebase, sbtmstatelow), 4);
6199 if ((regdata & (SICF_CLOCK_EN << SBTML_SICF_SHIFT)) != 0) {
6201 * set target reject and spin until busy is clear
6202 * (preserve core-specific bits)
6204 regdata = brcmf_sdcard_reg_read(card,
6205 CORE_SB(corebase, sbtmstatelow), 4);
6206 brcmf_sdcard_reg_write(card, CORE_SB(corebase, sbtmstatelow), 4,
6207 regdata | SBTML_REJ);
6209 regdata = brcmf_sdcard_reg_read(card,
6210 CORE_SB(corebase, sbtmstatelow), 4);
6212 SPINWAIT((brcmf_sdcard_reg_read(card,
6213 CORE_SB(corebase, sbtmstatehigh), 4) &
6214 SBTMH_BUSY), 100000);
6216 regdata = brcmf_sdcard_reg_read(card,
6217 CORE_SB(corebase, sbtmstatehigh), 4);
6218 if (regdata & SBTMH_BUSY)
6219 BRCMF_ERROR(("%s: ARM core still busy\n", __func__));
6221 regdata = brcmf_sdcard_reg_read(card,
6222 CORE_SB(corebase, sbidlow), 4);
6223 if (regdata & SBIDL_INIT) {
6224 regdata = brcmf_sdcard_reg_read(card,
6225 CORE_SB(corebase, sbimstate), 4) |
6227 brcmf_sdcard_reg_write(card,
6228 CORE_SB(corebase, sbimstate), 4,
6230 regdata = brcmf_sdcard_reg_read(card,
6231 CORE_SB(corebase, sbimstate), 4);
6233 SPINWAIT((brcmf_sdcard_reg_read(card,
6234 CORE_SB(corebase, sbimstate), 4) &
6238 /* set reset and reject while enabling the clocks */
6239 brcmf_sdcard_reg_write(card,
6240 CORE_SB(corebase, sbtmstatelow), 4,
6241 (((SICF_FGC | SICF_CLOCK_EN) << SBTML_SICF_SHIFT) |
6242 SBTML_REJ | SBTML_RESET));
6243 regdata = brcmf_sdcard_reg_read(card,
6244 CORE_SB(corebase, sbtmstatelow), 4);
6247 /* clear the initiator reject bit */
6248 regdata = brcmf_sdcard_reg_read(card,
6249 CORE_SB(corebase, sbidlow), 4);
6250 if (regdata & SBIDL_INIT) {
6251 regdata = brcmf_sdcard_reg_read(card,
6252 CORE_SB(corebase, sbimstate), 4) &
6254 brcmf_sdcard_reg_write(card,
6255 CORE_SB(corebase, sbimstate), 4,
6260 /* leave reset and reject asserted */
6261 brcmf_sdcard_reg_write(card, CORE_SB(corebase, sbtmstatelow), 4,
6262 (SBTML_REJ | SBTML_RESET));
6267 brcmf_sdbrcm_chip_attach(struct brcmf_bus *bus, u32 regs)
6269 struct chip_info *ci;
6273 BRCMF_TRACE(("%s: Enter\n", __func__));
6275 /* alloc chip_info_t */
6276 ci = kmalloc(sizeof(struct chip_info), GFP_ATOMIC);
6278 BRCMF_ERROR(("%s: malloc failed!\n", __func__));
6282 memset((unsigned char *)ci, 0, sizeof(struct chip_info));
6284 /* bus/core/clk setup for register access */
6285 /* Try forcing SDIO core to do ALPAvail request only */
6286 clkset = SBSDIO_FORCE_HW_CLKREQ_OFF | SBSDIO_ALP_AVAIL_REQ;
6287 brcmf_sdcard_cfg_write(bus->card, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
6290 BRCMF_ERROR(("%s: error writing for HT off\n", __func__));
6294 /* If register supported, wait for ALPAvail and then force ALP */
6295 /* This may take up to 15 milliseconds */
6296 clkval = brcmf_sdcard_cfg_read(bus->card, SDIO_FUNC_1,
6297 SBSDIO_FUNC1_CHIPCLKCSR, NULL);
6298 if ((clkval & ~SBSDIO_AVBITS) == clkset) {
6300 brcmf_sdcard_cfg_read(bus->card, SDIO_FUNC_1,
6301 SBSDIO_FUNC1_CHIPCLKCSR,
6303 !SBSDIO_ALPAV(clkval)),
6304 PMU_MAX_TRANSITION_DLY);
6305 if (!SBSDIO_ALPAV(clkval)) {
6306 BRCMF_ERROR(("%s: timeout on ALPAV wait,"
6307 " clkval 0x%02x\n", __func__, clkval));
6311 clkset = SBSDIO_FORCE_HW_CLKREQ_OFF |
6313 brcmf_sdcard_cfg_write(bus->card, SDIO_FUNC_1,
6314 SBSDIO_FUNC1_CHIPCLKCSR,
6318 BRCMF_ERROR(("%s: ChipClkCSR access: wrote 0x%02x"
6319 " read 0x%02x\n", __func__, clkset, clkval));
6324 /* Also, disable the extra SDIO pull-ups */
6325 brcmf_sdcard_cfg_write(bus->card, SDIO_FUNC_1, SBSDIO_FUNC1_SDIOPULLUP,
6328 err = brcmf_sdbrcm_chip_recognition(bus->card, ci, regs);
6333 * Make sure any on-chip ARM is off (in case strapping is wrong),
6334 * or downloaded code was already running.
6336 brcmf_sdbrcm_chip_disablecore(bus->card, ci->armcorebase);
6338 brcmf_sdcard_reg_write(bus->card,
6339 CORE_CC_REG(ci->cccorebase, gpiopullup), 4, 0);
6340 brcmf_sdcard_reg_write(bus->card,
6341 CORE_CC_REG(ci->cccorebase, gpiopulldown), 4, 0);
6343 /* Disable F2 to clear any intermediate frame state on the dongle */
6344 brcmf_sdcard_cfg_write(bus->card, SDIO_FUNC_0, SDIO_CCCR_IOEx,
6345 SDIO_FUNC_ENABLE_1, NULL);
6347 /* WAR: cmd52 backplane read so core HW will drop ALPReq */
6348 clkval = brcmf_sdcard_cfg_read(bus->card, SDIO_FUNC_1,
6351 /* Done with backplane-dependent accesses, can drop clock... */
6352 brcmf_sdcard_cfg_write(bus->card, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
6364 brcmf_sdbrcm_chip_resetcore(struct brcmf_sdio_card *card, u32 corebase)
6369 * Must do the disable sequence first to work for
6370 * arbitrary current core state.
6372 brcmf_sdbrcm_chip_disablecore(card, corebase);
6375 * Now do the initialization sequence.
6376 * set reset while enabling the clock and
6377 * forcing them on throughout the core
6379 brcmf_sdcard_reg_write(card, CORE_SB(corebase, sbtmstatelow), 4,
6380 ((SICF_FGC | SICF_CLOCK_EN) << SBTML_SICF_SHIFT) |
6384 regdata = brcmf_sdcard_reg_read(card, CORE_SB(corebase, sbtmstatehigh),
6386 if (regdata & SBTMH_SERR)
6387 brcmf_sdcard_reg_write(card, CORE_SB(corebase, sbtmstatehigh),
6390 regdata = brcmf_sdcard_reg_read(card, CORE_SB(corebase, sbimstate), 4);
6391 if (regdata & (SBIM_IBE | SBIM_TO))
6392 brcmf_sdcard_reg_write(card, CORE_SB(corebase, sbimstate), 4,
6393 regdata & ~(SBIM_IBE | SBIM_TO));
6395 /* clear reset and allow it to propagate throughout the core */
6396 brcmf_sdcard_reg_write(card, CORE_SB(corebase, sbtmstatelow), 4,
6397 (SICF_FGC << SBTML_SICF_SHIFT) |
6398 (SICF_CLOCK_EN << SBTML_SICF_SHIFT));
6401 /* leave clock enabled */
6402 brcmf_sdcard_reg_write(card, CORE_SB(corebase, sbtmstatelow), 4,
6403 (SICF_CLOCK_EN << SBTML_SICF_SHIFT));
6407 /* SDIO Pad drive strength to select value mappings */
6408 struct sdiod_drive_str {
6409 u8 strength; /* Pad Drive Strength in mA */
6410 u8 sel; /* Chip-specific select value */
6413 /* SDIO Drive Strength to sel value table for PMU Rev 1 */
6414 static const struct sdiod_drive_str sdiod_drive_strength_tab1[] = {
6422 /* SDIO Drive Strength to sel value table for PMU Rev 2, 3 */
6423 static const struct sdiod_drive_str sdiod_drive_strength_tab2[] = {
6434 /* SDIO Drive Strength to sel value table for PMU Rev 8 (1.8V) */
6435 static const struct sdiod_drive_str sdiod_drive_strength_tab3[] = {
6447 #define SDIOD_DRVSTR_KEY(chip, pmu) (((chip) << 16) | (pmu))
6450 brcmf_sdbrcm_sdiod_drive_strength_init(struct brcmf_bus *bus, u32 drivestrength) {
6451 struct sdiod_drive_str *str_tab = NULL;
6456 if (!(bus->ci->cccaps & CC_CAP_PMU))
6459 switch (SDIOD_DRVSTR_KEY(bus->ci->chip, bus->ci->pmurev)) {
6460 case SDIOD_DRVSTR_KEY(BCM4325_CHIP_ID, 1):
6461 str_tab = (struct sdiod_drive_str *)&sdiod_drive_strength_tab1;
6462 str_mask = 0x30000000;
6465 case SDIOD_DRVSTR_KEY(BCM4325_CHIP_ID, 2):
6466 case SDIOD_DRVSTR_KEY(BCM4325_CHIP_ID, 3):
6467 str_tab = (struct sdiod_drive_str *)&sdiod_drive_strength_tab2;
6468 str_mask = 0x00003800;
6471 case SDIOD_DRVSTR_KEY(BCM4336_CHIP_ID, 8):
6472 str_tab = (struct sdiod_drive_str *)&sdiod_drive_strength_tab3;
6473 str_mask = 0x00003800;
6477 BRCMF_ERROR(("No SDIO Drive strength init"
6478 "done for chip %s rev %d pmurev %d\n",
6479 brcmu_chipname(bus->ci->chip, chn, 8),
6480 bus->ci->chiprev, bus->ci->pmurev));
6484 if (str_tab != NULL) {
6485 u32 drivestrength_sel = 0;
6489 for (i = 0; str_tab[i].strength != 0; i++) {
6490 if (drivestrength >= str_tab[i].strength) {
6491 drivestrength_sel = str_tab[i].sel;
6496 brcmf_sdcard_reg_write(bus->card,
6497 CORE_CC_REG(bus->ci->cccorebase, chipcontrol_addr),
6499 cc_data_temp = brcmf_sdcard_reg_read(bus->card,
6500 CORE_CC_REG(bus->ci->cccorebase, chipcontrol_addr), 4);
6501 cc_data_temp &= ~str_mask;
6502 drivestrength_sel <<= str_shift;
6503 cc_data_temp |= drivestrength_sel;
6504 brcmf_sdcard_reg_write(bus->card,
6505 CORE_CC_REG(bus->ci->cccorebase, chipcontrol_addr),
6508 BRCMF_INFO(("SDIO: %dmA drive strength selected, "
6509 "set to 0x%08x\n", drivestrength, cc_data_temp));
6514 brcmf_sdbrcm_chip_detach(struct brcmf_bus *bus)
6516 BRCMF_TRACE(("%s: Enter\n", __func__));
6523 brcmf_sdbrcm_wait_for_event(struct brcmf_bus *bus, bool *lockvar)
6525 brcmf_sdbrcm_sdunlock(bus);
6526 wait_event_interruptible_timeout(bus->ctrl_wait,
6527 (*lockvar == false), HZ * 2);
6528 brcmf_sdbrcm_sdlock(bus);
6533 brcmf_sdbrcm_wait_event_wakeup(struct brcmf_bus *bus)
6535 if (waitqueue_active(&bus->ctrl_wait))
6536 wake_up_interruptible(&bus->ctrl_wait);
6541 brcmf_sdbrcm_watchdog_thread(void *data)
6543 struct brcmf_bus *bus = (struct brcmf_bus *)data;
6545 /* This thread doesn't need any user-level access,
6546 * so get rid of all our resources
6548 if (brcmf_watchdog_prio > 0) {
6549 struct sched_param param;
6550 param.sched_priority = (brcmf_watchdog_prio < MAX_RT_PRIO) ?
6551 brcmf_watchdog_prio : (MAX_RT_PRIO - 1);
6552 sched_setscheduler(current, SCHED_FIFO, ¶m);
6555 allow_signal(SIGTERM);
6556 /* Run until signal received */
6558 if (kthread_should_stop())
6560 if (!wait_for_completion_interruptible(&bus->watchdog_wait)) {
6561 if (bus->drvr->dongle_reset == false)
6562 brcmf_sdbrcm_bus_watchdog(bus->drvr);
6563 /* Count the tick for reference */
6564 bus->drvr->tickcnt++;
6572 brcmf_sdbrcm_watchdog(unsigned long data)
6574 struct brcmf_bus *bus = (struct brcmf_bus *)data;
6576 if (brcmf_watchdog_prio >= 0) {
6577 if (bus->watchdog_tsk)
6578 complete(&bus->watchdog_wait);
6582 brcmf_sdbrcm_bus_watchdog(bus->drvr);
6584 /* Count the tick for reference */
6585 bus->drvr->tickcnt++;
6588 /* Reschedule the watchdog */
6589 if (bus->wd_timer_valid)
6590 mod_timer(&bus->timer, jiffies + brcmf_watchdog_ms * HZ / 1000);
6594 brcmf_sdbrcm_wd_timer(struct brcmf_bus *bus, uint wdtick)
6596 static uint save_ms;
6598 /* don't start the wd until fw is loaded */
6599 if (bus->drvr->busstate == BRCMF_BUS_DOWN)
6602 /* Totally stop the timer */
6603 if (!wdtick && bus->wd_timer_valid == true) {
6604 del_timer_sync(&bus->timer);
6605 bus->wd_timer_valid = false;
6611 brcmf_watchdog_ms = (uint) wdtick;
6613 if (save_ms != brcmf_watchdog_ms) {
6614 if (bus->wd_timer_valid == true)
6615 /* Stop timer and restart at new value */
6616 del_timer_sync(&bus->timer);
6618 /* Create timer again when watchdog period is
6619 dynamically changed or in the first instance
6621 bus->timer.expires =
6622 jiffies + brcmf_watchdog_ms * HZ / 1000;
6623 add_timer(&bus->timer);
6626 /* Re arm the timer, at last watchdog period */
6627 mod_timer(&bus->timer,
6628 jiffies + brcmf_watchdog_ms * HZ / 1000);
6631 bus->wd_timer_valid = true;
6636 static int brcmf_sdbrcm_dpc_thread(void *data)
6638 struct brcmf_bus *bus = (struct brcmf_bus *) data;
6640 /* This thread doesn't need any user-level access,
6641 * so get rid of all our resources
6643 if (brcmf_dpc_prio > 0) {
6644 struct sched_param param;
6645 param.sched_priority = (brcmf_dpc_prio < MAX_RT_PRIO) ?
6646 brcmf_dpc_prio : (MAX_RT_PRIO - 1);
6647 sched_setscheduler(current, SCHED_FIFO, ¶m);
6650 allow_signal(SIGTERM);
6651 /* Run until signal received */
6653 if (kthread_should_stop())
6655 if (!wait_for_completion_interruptible(&bus->dpc_wait)) {
6656 /* Call bus dpc unless it indicated down
6657 (then clean stop) */
6658 if (bus->drvr->busstate != BRCMF_BUS_DOWN) {
6659 if (brcmf_sdbrcm_dpc(bus))
6660 complete(&bus->dpc_wait);
6662 brcmf_sdbrcm_bus_stop(bus, true);
6670 static void brcmf_sdbrcm_dpc_tasklet(unsigned long data)
6672 struct brcmf_bus *bus = (struct brcmf_bus *) data;
6674 /* Call bus dpc unless it indicated down (then clean stop) */
6675 if (bus->drvr->busstate != BRCMF_BUS_DOWN) {
6676 if (brcmf_sdbrcm_dpc(bus))
6677 tasklet_schedule(&bus->tasklet);
6679 brcmf_sdbrcm_bus_stop(bus, true);
6682 static void brcmf_sdbrcm_sched_dpc(struct brcmf_bus *bus)
6685 complete(&bus->dpc_wait);
6689 tasklet_schedule(&bus->tasklet);
6692 static void brcmf_sdbrcm_sdlock(struct brcmf_bus *bus)
6694 if (bus->threads_only)
6697 spin_lock_bh(&bus->sdlock);
6700 static void brcmf_sdbrcm_sdunlock(struct brcmf_bus *bus)
6702 if (bus->threads_only)
6705 spin_unlock_bh(&bus->sdlock);
6708 static int brcmf_sdbrcm_get_image(char *buf, int len, struct brcmf_bus *bus)
6710 if (bus->firmware->size < bus->fw_ptr + len)
6711 len = bus->firmware->size - bus->fw_ptr;
6713 memcpy(buf, &bus->firmware->data[bus->fw_ptr], len);
6718 MODULE_FIRMWARE(BCM4329_FW_NAME);
6719 MODULE_FIRMWARE(BCM4329_NV_NAME);