2 * Copyright (c) 2010 Broadcom Corporation
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <linux/types.h>
18 #include <linux/kernel.h>
19 #include <linux/kthread.h>
20 #include <linux/printk.h>
21 #include <linux/pci_ids.h>
22 #include <linux/netdevice.h>
23 #include <linux/interrupt.h>
24 #include <linux/sched.h>
25 #include <linux/mmc/sdio.h>
26 #include <linux/mmc/sdio_func.h>
27 #include <linux/mmc/card.h>
28 #include <linux/semaphore.h>
29 #include <linux/firmware.h>
30 #include <linux/module.h>
31 #include <linux/bcma/bcma.h>
32 #include <linux/debugfs.h>
33 #include <linux/vmalloc.h>
34 #include <linux/platform_data/brcmfmac-sdio.h>
35 #include <linux/moduleparam.h>
36 #include <asm/unaligned.h>
38 #include <brcmu_wifi.h>
39 #include <brcmu_utils.h>
40 #include <brcm_hw_ids.h>
42 #include "sdio_host.h"
43 #include "sdio_chip.h"
46 #define DCMD_RESP_TIMEOUT 2000 /* In milli second */
50 #define BRCMF_TRAP_INFO_SIZE 80
52 #define CBUF_LEN (128)
54 /* Device console log buffer state */
55 #define CONSOLE_BUFFER_MAX 2024
58 __le32 buf; /* Can't be pointer on (64-bit) hosts */
61 char *_buf_compat; /* Redundant pointer for backward compat. */
66 * When there is no UART (e.g. Quickturn),
67 * the host should write a complete
68 * input line directly into cbuf and then write
69 * the length into vcons_in.
70 * This may also be used when there is a real UART
71 * (at risk of conflicting with
72 * the real UART). vcons_out is currently unused.
77 /* Output (logging) buffer
78 * Console output is written to a ring buffer log_buf at index log_idx.
79 * The host may read the output when it sees log_idx advance.
80 * Output will be lost if the output wraps around faster than the host
83 struct rte_log_le log_le;
85 /* Console input line buffer
86 * Characters are read one at a time into cbuf
87 * until <CR> is received, then
88 * the buffer is processed as a command line.
89 * Also used for virtual UART.
96 #include <chipcommon.h>
100 #include "tracepoint.h"
102 #define TXQLEN 2048 /* bulk tx queue length */
103 #define TXHI (TXQLEN - 256) /* turn on flow control above TXHI */
104 #define TXLOW (TXHI - 256) /* turn off flow control below TXLOW */
107 #define TXRETRIES 2 /* # of retries for tx frames */
109 #define BRCMF_RXBOUND 50 /* Default for max rx frames in
112 #define BRCMF_TXBOUND 20 /* Default for max tx frames in
115 #define BRCMF_DEFAULT_TXGLOM_SIZE 32 /* max tx frames in glom chain */
117 #define BRCMF_TXMINMAX 1 /* Max tx frames if rx still pending */
119 #define MEMBLOCK 2048 /* Block size used for downloading
121 #define MAX_DATA_BUF (32 * 1024) /* Must be large enough to hold
122 biggest possible glom */
124 #define BRCMF_FIRSTREAD (1 << 6)
127 /* SBSDIO_DEVICE_CTL */
129 /* 1: device will assert busy signal when receiving CMD53 */
130 #define SBSDIO_DEVCTL_SETBUSY 0x01
131 /* 1: assertion of sdio interrupt is synchronous to the sdio clock */
132 #define SBSDIO_DEVCTL_SPI_INTR_SYNC 0x02
133 /* 1: mask all interrupts to host except the chipActive (rev 8) */
134 #define SBSDIO_DEVCTL_CA_INT_ONLY 0x04
135 /* 1: isolate internal sdio signals, put external pads in tri-state; requires
136 * sdio bus power cycle to clear (rev 9) */
137 #define SBSDIO_DEVCTL_PADS_ISO 0x08
138 /* Force SD->SB reset mapping (rev 11) */
139 #define SBSDIO_DEVCTL_SB_RST_CTL 0x30
140 /* Determined by CoreControl bit */
141 #define SBSDIO_DEVCTL_RST_CORECTL 0x00
142 /* Force backplane reset */
143 #define SBSDIO_DEVCTL_RST_BPRESET 0x10
144 /* Force no backplane reset */
145 #define SBSDIO_DEVCTL_RST_NOBPRESET 0x20
147 /* direct(mapped) cis space */
149 /* MAPPED common CIS address */
150 #define SBSDIO_CIS_BASE_COMMON 0x1000
151 /* maximum bytes in one CIS */
152 #define SBSDIO_CIS_SIZE_LIMIT 0x200
153 /* cis offset addr is < 17 bits */
154 #define SBSDIO_CIS_OFT_ADDR_MASK 0x1FFFF
156 /* manfid tuple length, include tuple, link bytes */
157 #define SBSDIO_CIS_MANFID_TUPLE_LEN 6
160 #define I_SMB_SW0 (1 << 0) /* To SB Mail S/W interrupt 0 */
161 #define I_SMB_SW1 (1 << 1) /* To SB Mail S/W interrupt 1 */
162 #define I_SMB_SW2 (1 << 2) /* To SB Mail S/W interrupt 2 */
163 #define I_SMB_SW3 (1 << 3) /* To SB Mail S/W interrupt 3 */
164 #define I_SMB_SW_MASK 0x0000000f /* To SB Mail S/W interrupts mask */
165 #define I_SMB_SW_SHIFT 0 /* To SB Mail S/W interrupts shift */
166 #define I_HMB_SW0 (1 << 4) /* To Host Mail S/W interrupt 0 */
167 #define I_HMB_SW1 (1 << 5) /* To Host Mail S/W interrupt 1 */
168 #define I_HMB_SW2 (1 << 6) /* To Host Mail S/W interrupt 2 */
169 #define I_HMB_SW3 (1 << 7) /* To Host Mail S/W interrupt 3 */
170 #define I_HMB_SW_MASK 0x000000f0 /* To Host Mail S/W interrupts mask */
171 #define I_HMB_SW_SHIFT 4 /* To Host Mail S/W interrupts shift */
172 #define I_WR_OOSYNC (1 << 8) /* Write Frame Out Of Sync */
173 #define I_RD_OOSYNC (1 << 9) /* Read Frame Out Of Sync */
174 #define I_PC (1 << 10) /* descriptor error */
175 #define I_PD (1 << 11) /* data error */
176 #define I_DE (1 << 12) /* Descriptor protocol Error */
177 #define I_RU (1 << 13) /* Receive descriptor Underflow */
178 #define I_RO (1 << 14) /* Receive fifo Overflow */
179 #define I_XU (1 << 15) /* Transmit fifo Underflow */
180 #define I_RI (1 << 16) /* Receive Interrupt */
181 #define I_BUSPWR (1 << 17) /* SDIO Bus Power Change (rev 9) */
182 #define I_XMTDATA_AVAIL (1 << 23) /* bits in fifo */
183 #define I_XI (1 << 24) /* Transmit Interrupt */
184 #define I_RF_TERM (1 << 25) /* Read Frame Terminate */
185 #define I_WF_TERM (1 << 26) /* Write Frame Terminate */
186 #define I_PCMCIA_XU (1 << 27) /* PCMCIA Transmit FIFO Underflow */
187 #define I_SBINT (1 << 28) /* sbintstatus Interrupt */
188 #define I_CHIPACTIVE (1 << 29) /* chip from doze to active state */
189 #define I_SRESET (1 << 30) /* CCCR RES interrupt */
190 #define I_IOE2 (1U << 31) /* CCCR IOE2 Bit Changed */
191 #define I_ERRORS (I_PC | I_PD | I_DE | I_RU | I_RO | I_XU)
192 #define I_DMA (I_RI | I_XI | I_ERRORS)
195 #define CC_CISRDY (1 << 0) /* CIS Ready */
196 #define CC_BPRESEN (1 << 1) /* CCCR RES signal */
197 #define CC_F2RDY (1 << 2) /* set CCCR IOR2 bit */
198 #define CC_CLRPADSISO (1 << 3) /* clear SDIO pads isolation */
199 #define CC_XMTDATAAVAIL_MODE (1 << 4)
200 #define CC_XMTDATAAVAIL_CTRL (1 << 5)
203 #define SFC_RF_TERM (1 << 0) /* Read Frame Terminate */
204 #define SFC_WF_TERM (1 << 1) /* Write Frame Terminate */
205 #define SFC_CRC4WOOS (1 << 2) /* CRC error for write out of sync */
206 #define SFC_ABORTALL (1 << 3) /* Abort all in-progress frames */
209 * Software allocation of To SB Mailbox resources
212 /* tosbmailbox bits corresponding to intstatus bits */
213 #define SMB_NAK (1 << 0) /* Frame NAK */
214 #define SMB_INT_ACK (1 << 1) /* Host Interrupt ACK */
215 #define SMB_USE_OOB (1 << 2) /* Use OOB Wakeup */
216 #define SMB_DEV_INT (1 << 3) /* Miscellaneous Interrupt */
218 /* tosbmailboxdata */
219 #define SMB_DATA_VERSION_SHIFT 16 /* host protocol version */
222 * Software allocation of To Host Mailbox resources
226 #define I_HMB_FC_STATE I_HMB_SW0 /* Flow Control State */
227 #define I_HMB_FC_CHANGE I_HMB_SW1 /* Flow Control State Changed */
228 #define I_HMB_FRAME_IND I_HMB_SW2 /* Frame Indication */
229 #define I_HMB_HOST_INT I_HMB_SW3 /* Miscellaneous Interrupt */
231 /* tohostmailboxdata */
232 #define HMB_DATA_NAKHANDLED 1 /* retransmit NAK'd frame */
233 #define HMB_DATA_DEVREADY 2 /* talk to host after enable */
234 #define HMB_DATA_FC 4 /* per prio flowcontrol update flag */
235 #define HMB_DATA_FWREADY 8 /* fw ready for protocol activity */
237 #define HMB_DATA_FCDATA_MASK 0xff000000
238 #define HMB_DATA_FCDATA_SHIFT 24
240 #define HMB_DATA_VERSION_MASK 0x00ff0000
241 #define HMB_DATA_VERSION_SHIFT 16
244 * Software-defined protocol header
247 /* Current protocol version */
248 #define SDPCM_PROT_VERSION 4
251 * Shared structure between dongle and the host.
252 * The structure contains pointers to trap or assert information.
254 #define SDPCM_SHARED_VERSION 0x0003
255 #define SDPCM_SHARED_VERSION_MASK 0x00FF
256 #define SDPCM_SHARED_ASSERT_BUILT 0x0100
257 #define SDPCM_SHARED_ASSERT 0x0200
258 #define SDPCM_SHARED_TRAP 0x0400
260 /* Space for header read, limit for data packets */
261 #define MAX_HDR_READ (1 << 6)
262 #define MAX_RX_DATASZ 2048
264 /* Bump up limit on waiting for HT to account for first startup;
265 * if the image is doing a CRC calculation before programming the PMU
266 * for HT availability, it could take a couple hundred ms more, so
267 * max out at a 1 second (1000000us).
269 #undef PMU_MAX_TRANSITION_DLY
270 #define PMU_MAX_TRANSITION_DLY 1000000
272 /* Value for ChipClockCSR during initial setup */
273 #define BRCMF_INIT_CLKCTL1 (SBSDIO_FORCE_HW_CLKREQ_OFF | \
274 SBSDIO_ALP_AVAIL_REQ)
276 /* Flags for SDH calls */
277 #define F2SYNC (SDIO_REQ_4BYTE | SDIO_REQ_FIXED)
279 #define BRCMF_IDLE_IMMEDIATE (-1) /* Enter idle immediately */
280 #define BRCMF_IDLE_ACTIVE 0 /* Do not request any SD clock change
283 #define BRCMF_IDLE_INTERVAL 1
285 #define KSO_WAIT_US 50
286 #define MAX_KSO_ATTEMPTS (PMU_MAX_TRANSITION_DLY/KSO_WAIT_US)
289 * Conversion of 802.1D priority to precedence level
291 static uint prio2prec(u32 prio)
293 return (prio == PRIO_8021D_NONE || prio == PRIO_8021D_BE) ?
298 /* Device console log buffer state */
299 struct brcmf_console {
300 uint count; /* Poll interval msec counter */
301 uint log_addr; /* Log struct address (fixed) */
302 struct rte_log_le log_le; /* Log struct (host copy) */
303 uint bufsize; /* Size of log buffer */
304 u8 *buf; /* Log buffer (host copy) */
305 uint last; /* Last buffer read index */
308 struct brcmf_trap_info {
322 __le32 r9; /* sb/v6 */
323 __le32 r10; /* sl/v7 */
324 __le32 r11; /* fp/v8 */
332 struct sdpcm_shared {
336 u32 assert_file_addr;
338 u32 console_addr; /* Address of struct rte_console */
344 struct sdpcm_shared_le {
347 __le32 assert_exp_addr;
348 __le32 assert_file_addr;
350 __le32 console_addr; /* Address of struct rte_console */
351 __le32 msgtrace_addr;
356 /* dongle SDIO bus specific header info */
357 struct brcmf_sdio_hdrinfo {
368 /* misc chip info needed by some of the routines */
369 /* Private data for SDIO bus interaction */
371 struct brcmf_sdio_dev *sdiodev; /* sdio device handler */
372 struct chip_info *ci; /* Chip info struct */
374 u32 ramsize; /* Size of RAM in SOCRAM (bytes) */
376 u32 hostintmask; /* Copy of Host Interrupt Mask */
377 atomic_t intstatus; /* Intstatus bits (events) pending */
378 atomic_t fcstate; /* State of dongle flow-control */
380 uint blocksize; /* Block size of SDIO transfers */
381 uint roundup; /* Max roundup limit */
383 struct pktq txq; /* Queue length used for flow-control */
384 u8 flowcontrol; /* per prio flow control bitmask */
385 u8 tx_seq; /* Transmit sequence number (next) */
386 u8 tx_max; /* Maximum transmit sequence allowed */
388 u8 *hdrbuf; /* buffer for handling rx frame */
389 u8 *rxhdr; /* Header of current rx frame (in hdrbuf) */
390 u8 rx_seq; /* Receive sequence number (expected) */
391 struct brcmf_sdio_hdrinfo cur_read;
392 /* info of current read frame */
393 bool rxskip; /* Skip receive (awaiting NAK ACK) */
394 bool rxpending; /* Data frame pending in dongle */
396 uint rxbound; /* Rx frames to read before resched */
397 uint txbound; /* Tx frames to send before resched */
400 struct sk_buff *glomd; /* Packet containing glomming descriptor */
401 struct sk_buff_head glom; /* Packet list for glommed superframe */
402 uint glomerr; /* Glom packet read errors */
404 u8 *rxbuf; /* Buffer for receiving control packets */
405 uint rxblen; /* Allocated length of rxbuf */
406 u8 *rxctl; /* Aligned pointer into rxbuf */
407 u8 *rxctl_orig; /* pointer for freeing rxctl */
408 uint rxlen; /* Length of valid data in buffer */
409 spinlock_t rxctl_lock; /* protection lock for ctrl frame resources */
411 u8 sdpcm_ver; /* Bus protocol reported by dongle */
413 bool intr; /* Use interrupts */
414 bool poll; /* Use polling */
415 atomic_t ipend; /* Device interrupt is pending */
416 uint spurious; /* Count of spurious interrupts */
417 uint pollrate; /* Ticks between device polls */
418 uint polltick; /* Tick counter */
421 uint console_interval;
422 struct brcmf_console console; /* Console output polling support */
423 uint console_addr; /* Console address from shared struct */
426 uint clkstate; /* State of sd and backplane clock(s) */
427 bool activity; /* Activity flag for clock down */
428 s32 idletime; /* Control for activity timeout */
429 s32 idlecount; /* Activity timeout counter */
430 s32 idleclock; /* How to set bus driver when idle */
431 bool rxflow_mode; /* Rx flow control mode */
432 bool rxflow; /* Is rx flow control on */
433 bool alp_only; /* Don't use HT clock (ALP only) */
437 bool ctrl_frame_stat;
440 wait_queue_head_t ctrl_wait;
441 wait_queue_head_t dcmd_resp_wait;
443 struct timer_list timer;
444 struct completion watchdog_wait;
445 struct task_struct *watchdog_tsk;
449 struct workqueue_struct *brcmf_wq;
450 struct work_struct datawork;
453 bool txoff; /* Transmit flow-controlled */
454 struct brcmf_sdio_count sdcnt;
455 bool sr_enabled; /* SaveRestore enabled */
456 bool sleeping; /* SDIO bus sleeping */
458 u8 tx_hdrlen; /* sdio bus header length for tx packet */
459 bool txglom; /* host tx glomming enable flag */
460 struct sk_buff *txglom_sgpad; /* scatter-gather padding buffer */
461 u16 head_align; /* buffer pointer alignment */
462 u16 sgentry_align; /* scatter-gather buffer alignment */
468 #define CLK_PENDING 2
472 static int qcount[NUMPRIO];
475 #define DEFAULT_SDIO_DRIVE_STRENGTH 6 /* in milliamps */
477 #define RETRYCHAN(chan) ((chan) == SDPCM_EVENT_CHANNEL)
479 /* Retry count for register access failures */
480 static const uint retry_limit = 2;
482 /* Limit on rounding up frames */
483 static const uint max_roundup = 512;
487 static int brcmf_sdio_txglomsz = BRCMF_DEFAULT_TXGLOM_SIZE;
488 module_param_named(txglomsz, brcmf_sdio_txglomsz, int, 0);
489 MODULE_PARM_DESC(txglomsz, "maximum tx packet chain size [SDIO]");
491 enum brcmf_sdio_frmtype {
492 BRCMF_SDIO_FT_NORMAL,
497 #define BCM43143_FIRMWARE_NAME "brcm/brcmfmac43143-sdio.bin"
498 #define BCM43143_NVRAM_NAME "brcm/brcmfmac43143-sdio.txt"
499 #define BCM43241B0_FIRMWARE_NAME "brcm/brcmfmac43241b0-sdio.bin"
500 #define BCM43241B0_NVRAM_NAME "brcm/brcmfmac43241b0-sdio.txt"
501 #define BCM43241B4_FIRMWARE_NAME "brcm/brcmfmac43241b4-sdio.bin"
502 #define BCM43241B4_NVRAM_NAME "brcm/brcmfmac43241b4-sdio.txt"
503 #define BCM4329_FIRMWARE_NAME "brcm/brcmfmac4329-sdio.bin"
504 #define BCM4329_NVRAM_NAME "brcm/brcmfmac4329-sdio.txt"
505 #define BCM4330_FIRMWARE_NAME "brcm/brcmfmac4330-sdio.bin"
506 #define BCM4330_NVRAM_NAME "brcm/brcmfmac4330-sdio.txt"
507 #define BCM4334_FIRMWARE_NAME "brcm/brcmfmac4334-sdio.bin"
508 #define BCM4334_NVRAM_NAME "brcm/brcmfmac4334-sdio.txt"
509 #define BCM4335_FIRMWARE_NAME "brcm/brcmfmac4335-sdio.bin"
510 #define BCM4335_NVRAM_NAME "brcm/brcmfmac4335-sdio.txt"
511 #define BCM43362_FIRMWARE_NAME "brcm/brcmfmac43362-sdio.bin"
512 #define BCM43362_NVRAM_NAME "brcm/brcmfmac43362-sdio.txt"
513 #define BCM4339_FIRMWARE_NAME "brcm/brcmfmac4339-sdio.bin"
514 #define BCM4339_NVRAM_NAME "brcm/brcmfmac4339-sdio.txt"
516 MODULE_FIRMWARE(BCM43143_FIRMWARE_NAME);
517 MODULE_FIRMWARE(BCM43143_NVRAM_NAME);
518 MODULE_FIRMWARE(BCM43241B0_FIRMWARE_NAME);
519 MODULE_FIRMWARE(BCM43241B0_NVRAM_NAME);
520 MODULE_FIRMWARE(BCM43241B4_FIRMWARE_NAME);
521 MODULE_FIRMWARE(BCM43241B4_NVRAM_NAME);
522 MODULE_FIRMWARE(BCM4329_FIRMWARE_NAME);
523 MODULE_FIRMWARE(BCM4329_NVRAM_NAME);
524 MODULE_FIRMWARE(BCM4330_FIRMWARE_NAME);
525 MODULE_FIRMWARE(BCM4330_NVRAM_NAME);
526 MODULE_FIRMWARE(BCM4334_FIRMWARE_NAME);
527 MODULE_FIRMWARE(BCM4334_NVRAM_NAME);
528 MODULE_FIRMWARE(BCM4335_FIRMWARE_NAME);
529 MODULE_FIRMWARE(BCM4335_NVRAM_NAME);
530 MODULE_FIRMWARE(BCM43362_FIRMWARE_NAME);
531 MODULE_FIRMWARE(BCM43362_NVRAM_NAME);
532 MODULE_FIRMWARE(BCM4339_FIRMWARE_NAME);
533 MODULE_FIRMWARE(BCM4339_NVRAM_NAME);
535 struct brcmf_firmware_names {
542 enum brcmf_firmware_type {
547 #define BRCMF_FIRMWARE_NVRAM(name) \
548 name ## _FIRMWARE_NAME, name ## _NVRAM_NAME
550 static const struct brcmf_firmware_names brcmf_fwname_data[] = {
551 { BCM43143_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM43143) },
552 { BCM43241_CHIP_ID, 0x0000001F, BRCMF_FIRMWARE_NVRAM(BCM43241B0) },
553 { BCM43241_CHIP_ID, 0xFFFFFFE0, BRCMF_FIRMWARE_NVRAM(BCM43241B4) },
554 { BCM4329_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM4329) },
555 { BCM4330_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM4330) },
556 { BCM4334_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM4334) },
557 { BCM4335_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM4335) },
558 { BCM43362_CHIP_ID, 0xFFFFFFFE, BRCMF_FIRMWARE_NVRAM(BCM43362) },
559 { BCM4339_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM4339) }
563 static const struct firmware *brcmf_sdio_get_fw(struct brcmf_sdio *bus,
564 enum brcmf_firmware_type type)
566 const struct firmware *fw;
570 for (i = 0; i < ARRAY_SIZE(brcmf_fwname_data); i++) {
571 if (brcmf_fwname_data[i].chipid == bus->ci->chip &&
572 brcmf_fwname_data[i].revmsk & BIT(bus->ci->chiprev)) {
574 case BRCMF_FIRMWARE_BIN:
575 name = brcmf_fwname_data[i].bin;
577 case BRCMF_FIRMWARE_NVRAM:
578 name = brcmf_fwname_data[i].nv;
581 brcmf_err("invalid firmware type (%d)\n", type);
587 brcmf_err("Unknown chipid %d [%d]\n",
588 bus->ci->chip, bus->ci->chiprev);
592 err = request_firmware(&fw, name, &bus->sdiodev->func[2]->dev);
593 if ((err) || (!fw)) {
594 brcmf_err("fail to request firmware %s (%d)\n", name, err);
601 static void pkt_align(struct sk_buff *p, int len, int align)
604 datalign = (unsigned long)(p->data);
605 datalign = roundup(datalign, (align)) - datalign;
607 skb_pull(p, datalign);
611 /* To check if there's window offered */
612 static bool data_ok(struct brcmf_sdio *bus)
614 return (u8)(bus->tx_max - bus->tx_seq) != 0 &&
615 ((u8)(bus->tx_max - bus->tx_seq) & 0x80) == 0;
619 * Reads a register in the SDIO hardware block. This block occupies a series of
620 * adresses on the 32 bit backplane bus.
623 r_sdreg32(struct brcmf_sdio *bus, u32 *regvar, u32 offset)
625 u8 idx = brcmf_sdio_chip_getinfidx(bus->ci, BCMA_CORE_SDIO_DEV);
628 *regvar = brcmf_sdiod_regrl(bus->sdiodev,
629 bus->ci->c_inf[idx].base + offset, &ret);
635 w_sdreg32(struct brcmf_sdio *bus, u32 regval, u32 reg_offset)
637 u8 idx = brcmf_sdio_chip_getinfidx(bus->ci, BCMA_CORE_SDIO_DEV);
640 brcmf_sdiod_regwl(bus->sdiodev,
641 bus->ci->c_inf[idx].base + reg_offset,
648 brcmf_sdio_kso_control(struct brcmf_sdio *bus, bool on)
650 u8 wr_val = 0, rd_val, cmp_val, bmask;
654 brcmf_dbg(TRACE, "Enter\n");
656 wr_val = (on << SBSDIO_FUNC1_SLEEPCSR_KSO_SHIFT);
657 /* 1st KSO write goes to AOS wake up core if device is asleep */
658 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
661 brcmf_err("SDIO_AOS KSO write error: %d\n", err);
666 /* device WAKEUP through KSO:
667 * write bit 0 & read back until
668 * both bits 0 (kso bit) & 1 (dev on status) are set
670 cmp_val = SBSDIO_FUNC1_SLEEPCSR_KSO_MASK |
671 SBSDIO_FUNC1_SLEEPCSR_DEVON_MASK;
673 usleep_range(2000, 3000);
675 /* Put device to sleep, turn off KSO */
677 /* only check for bit0, bit1(dev on status) may not
678 * get cleared right away
680 bmask = SBSDIO_FUNC1_SLEEPCSR_KSO_MASK;
684 /* reliable KSO bit set/clr:
685 * the sdiod sleep write access is synced to PMU 32khz clk
686 * just one write attempt may fail,
687 * read it back until it matches written value
689 rd_val = brcmf_sdiod_regrb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
691 if (((rd_val & bmask) == cmp_val) && !err)
693 brcmf_dbg(SDIO, "KSO wr/rd retry:%d (max: %d) ERR:%x\n",
694 try_cnt, MAX_KSO_ATTEMPTS, err);
696 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
698 } while (try_cnt++ < MAX_KSO_ATTEMPTS);
703 #define PKT_AVAILABLE() (intstatus & I_HMB_FRAME_IND)
705 #define HOSTINTMASK (I_HMB_SW_MASK | I_CHIPACTIVE)
707 /* Turn backplane clock on or off */
708 static int brcmf_sdio_htclk(struct brcmf_sdio *bus, bool on, bool pendok)
711 u8 clkctl, clkreq, devctl;
712 unsigned long timeout;
714 brcmf_dbg(SDIO, "Enter\n");
718 if (bus->sr_enabled) {
719 bus->clkstate = (on ? CLK_AVAIL : CLK_SDONLY);
724 /* Request HT Avail */
726 bus->alp_only ? SBSDIO_ALP_AVAIL_REQ : SBSDIO_HT_AVAIL_REQ;
728 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
731 brcmf_err("HT Avail request error: %d\n", err);
735 /* Check current status */
736 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
737 SBSDIO_FUNC1_CHIPCLKCSR, &err);
739 brcmf_err("HT Avail read error: %d\n", err);
743 /* Go to pending and await interrupt if appropriate */
744 if (!SBSDIO_CLKAV(clkctl, bus->alp_only) && pendok) {
745 /* Allow only clock-available interrupt */
746 devctl = brcmf_sdiod_regrb(bus->sdiodev,
747 SBSDIO_DEVICE_CTL, &err);
749 brcmf_err("Devctl error setting CA: %d\n",
754 devctl |= SBSDIO_DEVCTL_CA_INT_ONLY;
755 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
757 brcmf_dbg(SDIO, "CLKCTL: set PENDING\n");
758 bus->clkstate = CLK_PENDING;
761 } else if (bus->clkstate == CLK_PENDING) {
762 /* Cancel CA-only interrupt filter */
763 devctl = brcmf_sdiod_regrb(bus->sdiodev,
764 SBSDIO_DEVICE_CTL, &err);
765 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
766 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
770 /* Otherwise, wait here (polling) for HT Avail */
772 msecs_to_jiffies(PMU_MAX_TRANSITION_DLY/1000);
773 while (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
774 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
775 SBSDIO_FUNC1_CHIPCLKCSR,
777 if (time_after(jiffies, timeout))
780 usleep_range(5000, 10000);
783 brcmf_err("HT Avail request error: %d\n", err);
786 if (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
787 brcmf_err("HT Avail timeout (%d): clkctl 0x%02x\n",
788 PMU_MAX_TRANSITION_DLY, clkctl);
792 /* Mark clock available */
793 bus->clkstate = CLK_AVAIL;
794 brcmf_dbg(SDIO, "CLKCTL: turned ON\n");
797 if (!bus->alp_only) {
798 if (SBSDIO_ALPONLY(clkctl))
799 brcmf_err("HT Clock should be on\n");
801 #endif /* defined (DEBUG) */
803 bus->activity = true;
807 if (bus->clkstate == CLK_PENDING) {
808 /* Cancel CA-only interrupt filter */
809 devctl = brcmf_sdiod_regrb(bus->sdiodev,
810 SBSDIO_DEVICE_CTL, &err);
811 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
812 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
816 bus->clkstate = CLK_SDONLY;
817 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
819 brcmf_dbg(SDIO, "CLKCTL: turned OFF\n");
821 brcmf_err("Failed access turning clock off: %d\n",
829 /* Change idle/active SD state */
830 static int brcmf_sdio_sdclk(struct brcmf_sdio *bus, bool on)
832 brcmf_dbg(SDIO, "Enter\n");
835 bus->clkstate = CLK_SDONLY;
837 bus->clkstate = CLK_NONE;
842 /* Transition SD and backplane clock readiness */
843 static int brcmf_sdio_clkctl(struct brcmf_sdio *bus, uint target, bool pendok)
846 uint oldstate = bus->clkstate;
849 brcmf_dbg(SDIO, "Enter\n");
851 /* Early exit if we're already there */
852 if (bus->clkstate == target) {
853 if (target == CLK_AVAIL) {
854 brcmf_sdio_wd_timer(bus, BRCMF_WD_POLL_MS);
855 bus->activity = true;
862 /* Make sure SD clock is available */
863 if (bus->clkstate == CLK_NONE)
864 brcmf_sdio_sdclk(bus, true);
865 /* Now request HT Avail on the backplane */
866 brcmf_sdio_htclk(bus, true, pendok);
867 brcmf_sdio_wd_timer(bus, BRCMF_WD_POLL_MS);
868 bus->activity = true;
872 /* Remove HT request, or bring up SD clock */
873 if (bus->clkstate == CLK_NONE)
874 brcmf_sdio_sdclk(bus, true);
875 else if (bus->clkstate == CLK_AVAIL)
876 brcmf_sdio_htclk(bus, false, false);
878 brcmf_err("request for %d -> %d\n",
879 bus->clkstate, target);
880 brcmf_sdio_wd_timer(bus, BRCMF_WD_POLL_MS);
884 /* Make sure to remove HT request */
885 if (bus->clkstate == CLK_AVAIL)
886 brcmf_sdio_htclk(bus, false, false);
887 /* Now remove the SD clock */
888 brcmf_sdio_sdclk(bus, false);
889 brcmf_sdio_wd_timer(bus, 0);
893 brcmf_dbg(SDIO, "%d -> %d\n", oldstate, bus->clkstate);
900 brcmf_sdio_bus_sleep(struct brcmf_sdio *bus, bool sleep, bool pendok)
903 brcmf_dbg(TRACE, "Enter\n");
904 brcmf_dbg(SDIO, "request %s currently %s\n",
905 (sleep ? "SLEEP" : "WAKE"),
906 (bus->sleeping ? "SLEEP" : "WAKE"));
908 /* If SR is enabled control bus state with KSO */
909 if (bus->sr_enabled) {
910 /* Done if we're already in the requested state */
911 if (sleep == bus->sleeping)
916 /* Don't sleep if something is pending */
917 if (atomic_read(&bus->intstatus) ||
918 atomic_read(&bus->ipend) > 0 ||
919 (!atomic_read(&bus->fcstate) &&
920 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) &&
923 err = brcmf_sdio_kso_control(bus, false);
924 /* disable watchdog */
926 brcmf_sdio_wd_timer(bus, 0);
929 err = brcmf_sdio_kso_control(bus, true);
933 bus->sleeping = sleep;
934 brcmf_dbg(SDIO, "new state %s\n",
935 (sleep ? "SLEEP" : "WAKE"));
937 brcmf_err("error while changing bus sleep state %d\n",
946 if (!bus->sr_enabled)
947 brcmf_sdio_clkctl(bus, CLK_NONE, pendok);
949 brcmf_sdio_clkctl(bus, CLK_AVAIL, pendok);
956 static u32 brcmf_sdio_hostmail(struct brcmf_sdio *bus)
963 brcmf_dbg(SDIO, "Enter\n");
965 /* Read mailbox data and ack that we did so */
966 ret = r_sdreg32(bus, &hmb_data,
967 offsetof(struct sdpcmd_regs, tohostmailboxdata));
970 w_sdreg32(bus, SMB_INT_ACK,
971 offsetof(struct sdpcmd_regs, tosbmailbox));
972 bus->sdcnt.f1regdata += 2;
974 /* Dongle recomposed rx frames, accept them again */
975 if (hmb_data & HMB_DATA_NAKHANDLED) {
976 brcmf_dbg(SDIO, "Dongle reports NAK handled, expect rtx of %d\n",
979 brcmf_err("unexpected NAKHANDLED!\n");
982 intstatus |= I_HMB_FRAME_IND;
986 * DEVREADY does not occur with gSPI.
988 if (hmb_data & (HMB_DATA_DEVREADY | HMB_DATA_FWREADY)) {
990 (hmb_data & HMB_DATA_VERSION_MASK) >>
991 HMB_DATA_VERSION_SHIFT;
992 if (bus->sdpcm_ver != SDPCM_PROT_VERSION)
993 brcmf_err("Version mismatch, dongle reports %d, "
995 bus->sdpcm_ver, SDPCM_PROT_VERSION);
997 brcmf_dbg(SDIO, "Dongle ready, protocol version %d\n",
1002 * Flow Control has been moved into the RX headers and this out of band
1003 * method isn't used any more.
1004 * remaining backward compatible with older dongles.
1006 if (hmb_data & HMB_DATA_FC) {
1007 fcbits = (hmb_data & HMB_DATA_FCDATA_MASK) >>
1008 HMB_DATA_FCDATA_SHIFT;
1010 if (fcbits & ~bus->flowcontrol)
1011 bus->sdcnt.fc_xoff++;
1013 if (bus->flowcontrol & ~fcbits)
1014 bus->sdcnt.fc_xon++;
1016 bus->sdcnt.fc_rcvd++;
1017 bus->flowcontrol = fcbits;
1020 /* Shouldn't be any others */
1021 if (hmb_data & ~(HMB_DATA_DEVREADY |
1022 HMB_DATA_NAKHANDLED |
1025 HMB_DATA_FCDATA_MASK | HMB_DATA_VERSION_MASK))
1026 brcmf_err("Unknown mailbox data content: 0x%02x\n",
1032 static void brcmf_sdio_rxfail(struct brcmf_sdio *bus, bool abort, bool rtx)
1039 brcmf_err("%sterminate frame%s\n",
1040 abort ? "abort command, " : "",
1041 rtx ? ", send NAK" : "");
1044 brcmf_sdiod_abort(bus->sdiodev, SDIO_FUNC_2);
1046 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL,
1048 bus->sdcnt.f1regdata++;
1050 /* Wait until the packet has been flushed (device/FIFO stable) */
1051 for (lastrbc = retries = 0xffff; retries > 0; retries--) {
1052 hi = brcmf_sdiod_regrb(bus->sdiodev,
1053 SBSDIO_FUNC1_RFRAMEBCHI, &err);
1054 lo = brcmf_sdiod_regrb(bus->sdiodev,
1055 SBSDIO_FUNC1_RFRAMEBCLO, &err);
1056 bus->sdcnt.f1regdata += 2;
1058 if ((hi == 0) && (lo == 0))
1061 if ((hi > (lastrbc >> 8)) && (lo > (lastrbc & 0x00ff))) {
1062 brcmf_err("count growing: last 0x%04x now 0x%04x\n",
1063 lastrbc, (hi << 8) + lo);
1065 lastrbc = (hi << 8) + lo;
1069 brcmf_err("count never zeroed: last 0x%04x\n", lastrbc);
1071 brcmf_dbg(SDIO, "flush took %d iterations\n", 0xffff - retries);
1075 err = w_sdreg32(bus, SMB_NAK,
1076 offsetof(struct sdpcmd_regs, tosbmailbox));
1078 bus->sdcnt.f1regdata++;
1083 /* Clear partial in any case */
1084 bus->cur_read.len = 0;
1086 /* If we can't reach the device, signal failure */
1088 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
1091 /* return total length of buffer chain */
1092 static uint brcmf_sdio_glom_len(struct brcmf_sdio *bus)
1098 skb_queue_walk(&bus->glom, p)
1103 static void brcmf_sdio_free_glom(struct brcmf_sdio *bus)
1105 struct sk_buff *cur, *next;
1107 skb_queue_walk_safe(&bus->glom, cur, next) {
1108 skb_unlink(cur, &bus->glom);
1109 brcmu_pkt_buf_free_skb(cur);
1114 * brcmfmac sdio bus specific header
1115 * This is the lowest layer header wrapped on the packets transmitted between
1116 * host and WiFi dongle which contains information needed for SDIO core and
1119 * It consists of 3 parts: hardware header, hardware extension header and
1121 * hardware header (frame tag) - 4 bytes
1122 * Byte 0~1: Frame length
1123 * Byte 2~3: Checksum, bit-wise inverse of frame length
1124 * hardware extension header - 8 bytes
1125 * Tx glom mode only, N/A for Rx or normal Tx
1126 * Byte 0~1: Packet length excluding hw frame tag
1128 * Byte 3: Frame flags, bit 0: last frame indication
1129 * Byte 4~5: Reserved
1130 * Byte 6~7: Tail padding length
1131 * software header - 8 bytes
1132 * Byte 0: Rx/Tx sequence number
1133 * Byte 1: 4 MSB Channel number, 4 LSB arbitrary flag
1134 * Byte 2: Length of next data frame, reserved for Tx
1135 * Byte 3: Data offset
1136 * Byte 4: Flow control bits, reserved for Tx
1137 * Byte 5: Maximum Sequence number allowed by firmware for Tx, N/A for Tx packet
1138 * Byte 6~7: Reserved
1140 #define SDPCM_HWHDR_LEN 4
1141 #define SDPCM_HWEXT_LEN 8
1142 #define SDPCM_SWHDR_LEN 8
1143 #define SDPCM_HDRLEN (SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN)
1144 /* software header */
1145 #define SDPCM_SEQ_MASK 0x000000ff
1146 #define SDPCM_SEQ_WRAP 256
1147 #define SDPCM_CHANNEL_MASK 0x00000f00
1148 #define SDPCM_CHANNEL_SHIFT 8
1149 #define SDPCM_CONTROL_CHANNEL 0 /* Control */
1150 #define SDPCM_EVENT_CHANNEL 1 /* Asyc Event Indication */
1151 #define SDPCM_DATA_CHANNEL 2 /* Data Xmit/Recv */
1152 #define SDPCM_GLOM_CHANNEL 3 /* Coalesced packets */
1153 #define SDPCM_TEST_CHANNEL 15 /* Test/debug packets */
1154 #define SDPCM_GLOMDESC(p) (((u8 *)p)[1] & 0x80)
1155 #define SDPCM_NEXTLEN_MASK 0x00ff0000
1156 #define SDPCM_NEXTLEN_SHIFT 16
1157 #define SDPCM_DOFFSET_MASK 0xff000000
1158 #define SDPCM_DOFFSET_SHIFT 24
1159 #define SDPCM_FCMASK_MASK 0x000000ff
1160 #define SDPCM_WINDOW_MASK 0x0000ff00
1161 #define SDPCM_WINDOW_SHIFT 8
1163 static inline u8 brcmf_sdio_getdatoffset(u8 *swheader)
1166 hdrvalue = *(u32 *)swheader;
1167 return (u8)((hdrvalue & SDPCM_DOFFSET_MASK) >> SDPCM_DOFFSET_SHIFT);
1170 static int brcmf_sdio_hdparse(struct brcmf_sdio *bus, u8 *header,
1171 struct brcmf_sdio_hdrinfo *rd,
1172 enum brcmf_sdio_frmtype type)
1175 u8 rx_seq, fc, tx_seq_max;
1178 trace_brcmf_sdpcm_hdr(SDPCM_RX, header);
1181 len = get_unaligned_le16(header);
1182 checksum = get_unaligned_le16(header + sizeof(u16));
1183 /* All zero means no more to read */
1184 if (!(len | checksum)) {
1185 bus->rxpending = false;
1188 if ((u16)(~(len ^ checksum))) {
1189 brcmf_err("HW header checksum error\n");
1190 bus->sdcnt.rx_badhdr++;
1191 brcmf_sdio_rxfail(bus, false, false);
1194 if (len < SDPCM_HDRLEN) {
1195 brcmf_err("HW header length error\n");
1198 if (type == BRCMF_SDIO_FT_SUPER &&
1199 (roundup(len, bus->blocksize) != rd->len)) {
1200 brcmf_err("HW superframe header length error\n");
1203 if (type == BRCMF_SDIO_FT_SUB && len > rd->len) {
1204 brcmf_err("HW subframe header length error\n");
1209 /* software header */
1210 header += SDPCM_HWHDR_LEN;
1211 swheader = le32_to_cpu(*(__le32 *)header);
1212 if (type == BRCMF_SDIO_FT_SUPER && SDPCM_GLOMDESC(header)) {
1213 brcmf_err("Glom descriptor found in superframe head\n");
1217 rx_seq = (u8)(swheader & SDPCM_SEQ_MASK);
1218 rd->channel = (swheader & SDPCM_CHANNEL_MASK) >> SDPCM_CHANNEL_SHIFT;
1219 if (len > MAX_RX_DATASZ && rd->channel != SDPCM_CONTROL_CHANNEL &&
1220 type != BRCMF_SDIO_FT_SUPER) {
1221 brcmf_err("HW header length too long\n");
1222 bus->sdcnt.rx_toolong++;
1223 brcmf_sdio_rxfail(bus, false, false);
1227 if (type == BRCMF_SDIO_FT_SUPER && rd->channel != SDPCM_GLOM_CHANNEL) {
1228 brcmf_err("Wrong channel for superframe\n");
1232 if (type == BRCMF_SDIO_FT_SUB && rd->channel != SDPCM_DATA_CHANNEL &&
1233 rd->channel != SDPCM_EVENT_CHANNEL) {
1234 brcmf_err("Wrong channel for subframe\n");
1238 rd->dat_offset = brcmf_sdio_getdatoffset(header);
1239 if (rd->dat_offset < SDPCM_HDRLEN || rd->dat_offset > rd->len) {
1240 brcmf_err("seq %d: bad data offset\n", rx_seq);
1241 bus->sdcnt.rx_badhdr++;
1242 brcmf_sdio_rxfail(bus, false, false);
1246 if (rd->seq_num != rx_seq) {
1247 brcmf_err("seq %d: sequence number error, expect %d\n",
1248 rx_seq, rd->seq_num);
1249 bus->sdcnt.rx_badseq++;
1250 rd->seq_num = rx_seq;
1252 /* no need to check the reset for subframe */
1253 if (type == BRCMF_SDIO_FT_SUB)
1255 rd->len_nxtfrm = (swheader & SDPCM_NEXTLEN_MASK) >> SDPCM_NEXTLEN_SHIFT;
1256 if (rd->len_nxtfrm << 4 > MAX_RX_DATASZ) {
1257 /* only warm for NON glom packet */
1258 if (rd->channel != SDPCM_GLOM_CHANNEL)
1259 brcmf_err("seq %d: next length error\n", rx_seq);
1262 swheader = le32_to_cpu(*(__le32 *)(header + 4));
1263 fc = swheader & SDPCM_FCMASK_MASK;
1264 if (bus->flowcontrol != fc) {
1265 if (~bus->flowcontrol & fc)
1266 bus->sdcnt.fc_xoff++;
1267 if (bus->flowcontrol & ~fc)
1268 bus->sdcnt.fc_xon++;
1269 bus->sdcnt.fc_rcvd++;
1270 bus->flowcontrol = fc;
1272 tx_seq_max = (swheader & SDPCM_WINDOW_MASK) >> SDPCM_WINDOW_SHIFT;
1273 if ((u8)(tx_seq_max - bus->tx_seq) > 0x40) {
1274 brcmf_err("seq %d: max tx seq number error\n", rx_seq);
1275 tx_seq_max = bus->tx_seq + 2;
1277 bus->tx_max = tx_seq_max;
1282 static inline void brcmf_sdio_update_hwhdr(u8 *header, u16 frm_length)
1284 *(__le16 *)header = cpu_to_le16(frm_length);
1285 *(((__le16 *)header) + 1) = cpu_to_le16(~frm_length);
1288 static void brcmf_sdio_hdpack(struct brcmf_sdio *bus, u8 *header,
1289 struct brcmf_sdio_hdrinfo *hd_info)
1294 brcmf_sdio_update_hwhdr(header, hd_info->len);
1295 hdr_offset = SDPCM_HWHDR_LEN;
1298 hdrval = (hd_info->len - hdr_offset) | (hd_info->lastfrm << 24);
1299 *((__le32 *)(header + hdr_offset)) = cpu_to_le32(hdrval);
1300 hdrval = (u16)hd_info->tail_pad << 16;
1301 *(((__le32 *)(header + hdr_offset)) + 1) = cpu_to_le32(hdrval);
1302 hdr_offset += SDPCM_HWEXT_LEN;
1305 hdrval = hd_info->seq_num;
1306 hdrval |= (hd_info->channel << SDPCM_CHANNEL_SHIFT) &
1308 hdrval |= (hd_info->dat_offset << SDPCM_DOFFSET_SHIFT) &
1310 *((__le32 *)(header + hdr_offset)) = cpu_to_le32(hdrval);
1311 *(((__le32 *)(header + hdr_offset)) + 1) = 0;
1312 trace_brcmf_sdpcm_hdr(SDPCM_TX + !!(bus->txglom), header);
1315 static u8 brcmf_sdio_rxglom(struct brcmf_sdio *bus, u8 rxseq)
1320 struct sk_buff *pfirst, *pnext;
1325 struct brcmf_sdio_hdrinfo rd_new;
1327 /* If packets, issue read(s) and send up packet chain */
1328 /* Return sequence numbers consumed? */
1330 brcmf_dbg(SDIO, "start: glomd %p glom %p\n",
1331 bus->glomd, skb_peek(&bus->glom));
1333 /* If there's a descriptor, generate the packet chain */
1335 pfirst = pnext = NULL;
1336 dlen = (u16) (bus->glomd->len);
1337 dptr = bus->glomd->data;
1338 if (!dlen || (dlen & 1)) {
1339 brcmf_err("bad glomd len(%d), ignore descriptor\n",
1344 for (totlen = num = 0; dlen; num++) {
1345 /* Get (and move past) next length */
1346 sublen = get_unaligned_le16(dptr);
1347 dlen -= sizeof(u16);
1348 dptr += sizeof(u16);
1349 if ((sublen < SDPCM_HDRLEN) ||
1350 ((num == 0) && (sublen < (2 * SDPCM_HDRLEN)))) {
1351 brcmf_err("descriptor len %d bad: %d\n",
1356 if (sublen % bus->sgentry_align) {
1357 brcmf_err("sublen %d not multiple of %d\n",
1358 sublen, bus->sgentry_align);
1362 /* For last frame, adjust read len so total
1363 is a block multiple */
1366 (roundup(totlen, bus->blocksize) - totlen);
1367 totlen = roundup(totlen, bus->blocksize);
1370 /* Allocate/chain packet for next subframe */
1371 pnext = brcmu_pkt_buf_get_skb(sublen + bus->sgentry_align);
1372 if (pnext == NULL) {
1373 brcmf_err("bcm_pkt_buf_get_skb failed, num %d len %d\n",
1377 skb_queue_tail(&bus->glom, pnext);
1379 /* Adhere to start alignment requirements */
1380 pkt_align(pnext, sublen, bus->sgentry_align);
1383 /* If all allocations succeeded, save packet chain
1386 brcmf_dbg(GLOM, "allocated %d-byte packet chain for %d subframes\n",
1388 if (BRCMF_GLOM_ON() && bus->cur_read.len &&
1389 totlen != bus->cur_read.len) {
1390 brcmf_dbg(GLOM, "glomdesc mismatch: nextlen %d glomdesc %d rxseq %d\n",
1391 bus->cur_read.len, totlen, rxseq);
1393 pfirst = pnext = NULL;
1395 brcmf_sdio_free_glom(bus);
1399 /* Done with descriptor packet */
1400 brcmu_pkt_buf_free_skb(bus->glomd);
1402 bus->cur_read.len = 0;
1405 /* Ok -- either we just generated a packet chain,
1406 or had one from before */
1407 if (!skb_queue_empty(&bus->glom)) {
1408 if (BRCMF_GLOM_ON()) {
1409 brcmf_dbg(GLOM, "try superframe read, packet chain:\n");
1410 skb_queue_walk(&bus->glom, pnext) {
1411 brcmf_dbg(GLOM, " %p: %p len 0x%04x (%d)\n",
1412 pnext, (u8 *) (pnext->data),
1413 pnext->len, pnext->len);
1417 pfirst = skb_peek(&bus->glom);
1418 dlen = (u16) brcmf_sdio_glom_len(bus);
1420 /* Do an SDIO read for the superframe. Configurable iovar to
1421 * read directly into the chained packet, or allocate a large
1422 * packet and and copy into the chain.
1424 sdio_claim_host(bus->sdiodev->func[1]);
1425 errcode = brcmf_sdiod_recv_chain(bus->sdiodev,
1427 sdio_release_host(bus->sdiodev->func[1]);
1428 bus->sdcnt.f2rxdata++;
1430 /* On failure, kill the superframe, allow a couple retries */
1432 brcmf_err("glom read of %d bytes failed: %d\n",
1435 sdio_claim_host(bus->sdiodev->func[1]);
1436 if (bus->glomerr++ < 3) {
1437 brcmf_sdio_rxfail(bus, true, true);
1440 brcmf_sdio_rxfail(bus, true, false);
1441 bus->sdcnt.rxglomfail++;
1442 brcmf_sdio_free_glom(bus);
1444 sdio_release_host(bus->sdiodev->func[1]);
1448 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
1449 pfirst->data, min_t(int, pfirst->len, 48),
1452 rd_new.seq_num = rxseq;
1454 sdio_claim_host(bus->sdiodev->func[1]);
1455 errcode = brcmf_sdio_hdparse(bus, pfirst->data, &rd_new,
1456 BRCMF_SDIO_FT_SUPER);
1457 sdio_release_host(bus->sdiodev->func[1]);
1458 bus->cur_read.len = rd_new.len_nxtfrm << 4;
1460 /* Remove superframe header, remember offset */
1461 skb_pull(pfirst, rd_new.dat_offset);
1462 sfdoff = rd_new.dat_offset;
1465 /* Validate all the subframe headers */
1466 skb_queue_walk(&bus->glom, pnext) {
1467 /* leave when invalid subframe is found */
1471 rd_new.len = pnext->len;
1472 rd_new.seq_num = rxseq++;
1473 sdio_claim_host(bus->sdiodev->func[1]);
1474 errcode = brcmf_sdio_hdparse(bus, pnext->data, &rd_new,
1476 sdio_release_host(bus->sdiodev->func[1]);
1477 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
1478 pnext->data, 32, "subframe:\n");
1484 /* Terminate frame on error, request
1486 sdio_claim_host(bus->sdiodev->func[1]);
1487 if (bus->glomerr++ < 3) {
1488 /* Restore superframe header space */
1489 skb_push(pfirst, sfdoff);
1490 brcmf_sdio_rxfail(bus, true, true);
1493 brcmf_sdio_rxfail(bus, true, false);
1494 bus->sdcnt.rxglomfail++;
1495 brcmf_sdio_free_glom(bus);
1497 sdio_release_host(bus->sdiodev->func[1]);
1498 bus->cur_read.len = 0;
1502 /* Basic SD framing looks ok - process each packet (header) */
1504 skb_queue_walk_safe(&bus->glom, pfirst, pnext) {
1505 dptr = (u8 *) (pfirst->data);
1506 sublen = get_unaligned_le16(dptr);
1507 doff = brcmf_sdio_getdatoffset(&dptr[SDPCM_HWHDR_LEN]);
1509 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_DATA_ON(),
1511 "Rx Subframe Data:\n");
1513 __skb_trim(pfirst, sublen);
1514 skb_pull(pfirst, doff);
1516 if (pfirst->len == 0) {
1517 skb_unlink(pfirst, &bus->glom);
1518 brcmu_pkt_buf_free_skb(pfirst);
1522 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
1524 min_t(int, pfirst->len, 32),
1525 "subframe %d to stack, %p (%p/%d) nxt/lnk %p/%p\n",
1526 bus->glom.qlen, pfirst, pfirst->data,
1527 pfirst->len, pfirst->next,
1529 skb_unlink(pfirst, &bus->glom);
1530 brcmf_rx_frame(bus->sdiodev->dev, pfirst);
1531 bus->sdcnt.rxglompkts++;
1534 bus->sdcnt.rxglomframes++;
1539 static int brcmf_sdio_dcmd_resp_wait(struct brcmf_sdio *bus, uint *condition,
1542 DECLARE_WAITQUEUE(wait, current);
1543 int timeout = msecs_to_jiffies(DCMD_RESP_TIMEOUT);
1545 /* Wait until control frame is available */
1546 add_wait_queue(&bus->dcmd_resp_wait, &wait);
1547 set_current_state(TASK_INTERRUPTIBLE);
1549 while (!(*condition) && (!signal_pending(current) && timeout))
1550 timeout = schedule_timeout(timeout);
1552 if (signal_pending(current))
1555 set_current_state(TASK_RUNNING);
1556 remove_wait_queue(&bus->dcmd_resp_wait, &wait);
1561 static int brcmf_sdio_dcmd_resp_wake(struct brcmf_sdio *bus)
1563 if (waitqueue_active(&bus->dcmd_resp_wait))
1564 wake_up_interruptible(&bus->dcmd_resp_wait);
1569 brcmf_sdio_read_control(struct brcmf_sdio *bus, u8 *hdr, uint len, uint doff)
1572 u8 *buf = NULL, *rbuf;
1575 brcmf_dbg(TRACE, "Enter\n");
1578 buf = vzalloc(bus->rxblen);
1583 pad = ((unsigned long)rbuf % bus->head_align);
1585 rbuf += (bus->head_align - pad);
1587 /* Copy the already-read portion over */
1588 memcpy(buf, hdr, BRCMF_FIRSTREAD);
1589 if (len <= BRCMF_FIRSTREAD)
1592 /* Raise rdlen to next SDIO block to avoid tail command */
1593 rdlen = len - BRCMF_FIRSTREAD;
1594 if (bus->roundup && bus->blocksize && (rdlen > bus->blocksize)) {
1595 pad = bus->blocksize - (rdlen % bus->blocksize);
1596 if ((pad <= bus->roundup) && (pad < bus->blocksize) &&
1597 ((len + pad) < bus->sdiodev->bus_if->maxctl))
1599 } else if (rdlen % bus->head_align) {
1600 rdlen += bus->head_align - (rdlen % bus->head_align);
1603 /* Drop if the read is too big or it exceeds our maximum */
1604 if ((rdlen + BRCMF_FIRSTREAD) > bus->sdiodev->bus_if->maxctl) {
1605 brcmf_err("%d-byte control read exceeds %d-byte buffer\n",
1606 rdlen, bus->sdiodev->bus_if->maxctl);
1607 brcmf_sdio_rxfail(bus, false, false);
1611 if ((len - doff) > bus->sdiodev->bus_if->maxctl) {
1612 brcmf_err("%d-byte ctl frame (%d-byte ctl data) exceeds %d-byte limit\n",
1613 len, len - doff, bus->sdiodev->bus_if->maxctl);
1614 bus->sdcnt.rx_toolong++;
1615 brcmf_sdio_rxfail(bus, false, false);
1619 /* Read remain of frame body */
1620 sdret = brcmf_sdiod_recv_buf(bus->sdiodev, rbuf, rdlen);
1621 bus->sdcnt.f2rxdata++;
1623 /* Control frame failures need retransmission */
1625 brcmf_err("read %d control bytes failed: %d\n",
1627 bus->sdcnt.rxc_errors++;
1628 brcmf_sdio_rxfail(bus, true, true);
1631 memcpy(buf + BRCMF_FIRSTREAD, rbuf, rdlen);
1635 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_CTL_ON(),
1636 buf, len, "RxCtrl:\n");
1638 /* Point to valid data and indicate its length */
1639 spin_lock_bh(&bus->rxctl_lock);
1641 brcmf_err("last control frame is being processed.\n");
1642 spin_unlock_bh(&bus->rxctl_lock);
1646 bus->rxctl = buf + doff;
1647 bus->rxctl_orig = buf;
1648 bus->rxlen = len - doff;
1649 spin_unlock_bh(&bus->rxctl_lock);
1652 /* Awake any waiters */
1653 brcmf_sdio_dcmd_resp_wake(bus);
1656 /* Pad read to blocksize for efficiency */
1657 static void brcmf_sdio_pad(struct brcmf_sdio *bus, u16 *pad, u16 *rdlen)
1659 if (bus->roundup && bus->blocksize && *rdlen > bus->blocksize) {
1660 *pad = bus->blocksize - (*rdlen % bus->blocksize);
1661 if (*pad <= bus->roundup && *pad < bus->blocksize &&
1662 *rdlen + *pad + BRCMF_FIRSTREAD < MAX_RX_DATASZ)
1664 } else if (*rdlen % bus->head_align) {
1665 *rdlen += bus->head_align - (*rdlen % bus->head_align);
1669 static uint brcmf_sdio_readframes(struct brcmf_sdio *bus, uint maxframes)
1671 struct sk_buff *pkt; /* Packet for event or data frames */
1672 u16 pad; /* Number of pad bytes to read */
1673 uint rxleft = 0; /* Remaining number of frames allowed */
1674 int ret; /* Return code from calls */
1675 uint rxcount = 0; /* Total frames read */
1676 struct brcmf_sdio_hdrinfo *rd = &bus->cur_read, rd_new;
1679 brcmf_dbg(TRACE, "Enter\n");
1681 /* Not finished unless we encounter no more frames indication */
1682 bus->rxpending = true;
1684 for (rd->seq_num = bus->rx_seq, rxleft = maxframes;
1685 !bus->rxskip && rxleft &&
1686 bus->sdiodev->bus_if->state != BRCMF_BUS_DOWN;
1687 rd->seq_num++, rxleft--) {
1689 /* Handle glomming separately */
1690 if (bus->glomd || !skb_queue_empty(&bus->glom)) {
1692 brcmf_dbg(GLOM, "calling rxglom: glomd %p, glom %p\n",
1693 bus->glomd, skb_peek(&bus->glom));
1694 cnt = brcmf_sdio_rxglom(bus, rd->seq_num);
1695 brcmf_dbg(GLOM, "rxglom returned %d\n", cnt);
1696 rd->seq_num += cnt - 1;
1697 rxleft = (rxleft > cnt) ? (rxleft - cnt) : 1;
1701 rd->len_left = rd->len;
1702 /* read header first for unknow frame length */
1703 sdio_claim_host(bus->sdiodev->func[1]);
1705 ret = brcmf_sdiod_recv_buf(bus->sdiodev,
1706 bus->rxhdr, BRCMF_FIRSTREAD);
1707 bus->sdcnt.f2rxhdrs++;
1709 brcmf_err("RXHEADER FAILED: %d\n",
1711 bus->sdcnt.rx_hdrfail++;
1712 brcmf_sdio_rxfail(bus, true, true);
1713 sdio_release_host(bus->sdiodev->func[1]);
1717 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() || BRCMF_HDRS_ON(),
1718 bus->rxhdr, SDPCM_HDRLEN,
1721 if (brcmf_sdio_hdparse(bus, bus->rxhdr, rd,
1722 BRCMF_SDIO_FT_NORMAL)) {
1723 sdio_release_host(bus->sdiodev->func[1]);
1724 if (!bus->rxpending)
1730 if (rd->channel == SDPCM_CONTROL_CHANNEL) {
1731 brcmf_sdio_read_control(bus, bus->rxhdr,
1734 /* prepare the descriptor for the next read */
1735 rd->len = rd->len_nxtfrm << 4;
1737 /* treat all packet as event if we don't know */
1738 rd->channel = SDPCM_EVENT_CHANNEL;
1739 sdio_release_host(bus->sdiodev->func[1]);
1742 rd->len_left = rd->len > BRCMF_FIRSTREAD ?
1743 rd->len - BRCMF_FIRSTREAD : 0;
1744 head_read = BRCMF_FIRSTREAD;
1747 brcmf_sdio_pad(bus, &pad, &rd->len_left);
1749 pkt = brcmu_pkt_buf_get_skb(rd->len_left + head_read +
1752 /* Give up on data, request rtx of events */
1753 brcmf_err("brcmu_pkt_buf_get_skb failed\n");
1754 brcmf_sdio_rxfail(bus, false,
1755 RETRYCHAN(rd->channel));
1756 sdio_release_host(bus->sdiodev->func[1]);
1759 skb_pull(pkt, head_read);
1760 pkt_align(pkt, rd->len_left, bus->head_align);
1762 ret = brcmf_sdiod_recv_pkt(bus->sdiodev, pkt);
1763 bus->sdcnt.f2rxdata++;
1764 sdio_release_host(bus->sdiodev->func[1]);
1767 brcmf_err("read %d bytes from channel %d failed: %d\n",
1768 rd->len, rd->channel, ret);
1769 brcmu_pkt_buf_free_skb(pkt);
1770 sdio_claim_host(bus->sdiodev->func[1]);
1771 brcmf_sdio_rxfail(bus, true,
1772 RETRYCHAN(rd->channel));
1773 sdio_release_host(bus->sdiodev->func[1]);
1778 skb_push(pkt, head_read);
1779 memcpy(pkt->data, bus->rxhdr, head_read);
1782 memcpy(bus->rxhdr, pkt->data, SDPCM_HDRLEN);
1783 rd_new.seq_num = rd->seq_num;
1784 sdio_claim_host(bus->sdiodev->func[1]);
1785 if (brcmf_sdio_hdparse(bus, bus->rxhdr, &rd_new,
1786 BRCMF_SDIO_FT_NORMAL)) {
1788 brcmu_pkt_buf_free_skb(pkt);
1790 bus->sdcnt.rx_readahead_cnt++;
1791 if (rd->len != roundup(rd_new.len, 16)) {
1792 brcmf_err("frame length mismatch:read %d, should be %d\n",
1794 roundup(rd_new.len, 16) >> 4);
1796 brcmf_sdio_rxfail(bus, true, true);
1797 sdio_release_host(bus->sdiodev->func[1]);
1798 brcmu_pkt_buf_free_skb(pkt);
1801 sdio_release_host(bus->sdiodev->func[1]);
1802 rd->len_nxtfrm = rd_new.len_nxtfrm;
1803 rd->channel = rd_new.channel;
1804 rd->dat_offset = rd_new.dat_offset;
1806 brcmf_dbg_hex_dump(!(BRCMF_BYTES_ON() &&
1809 bus->rxhdr, SDPCM_HDRLEN,
1812 if (rd_new.channel == SDPCM_CONTROL_CHANNEL) {
1813 brcmf_err("readahead on control packet %d?\n",
1815 /* Force retry w/normal header read */
1817 sdio_claim_host(bus->sdiodev->func[1]);
1818 brcmf_sdio_rxfail(bus, false, true);
1819 sdio_release_host(bus->sdiodev->func[1]);
1820 brcmu_pkt_buf_free_skb(pkt);
1825 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_DATA_ON(),
1826 pkt->data, rd->len, "Rx Data:\n");
1828 /* Save superframe descriptor and allocate packet frame */
1829 if (rd->channel == SDPCM_GLOM_CHANNEL) {
1830 if (SDPCM_GLOMDESC(&bus->rxhdr[SDPCM_HWHDR_LEN])) {
1831 brcmf_dbg(GLOM, "glom descriptor, %d bytes:\n",
1833 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
1836 __skb_trim(pkt, rd->len);
1837 skb_pull(pkt, SDPCM_HDRLEN);
1840 brcmf_err("%s: glom superframe w/o "
1841 "descriptor!\n", __func__);
1842 sdio_claim_host(bus->sdiodev->func[1]);
1843 brcmf_sdio_rxfail(bus, false, false);
1844 sdio_release_host(bus->sdiodev->func[1]);
1846 /* prepare the descriptor for the next read */
1847 rd->len = rd->len_nxtfrm << 4;
1849 /* treat all packet as event if we don't know */
1850 rd->channel = SDPCM_EVENT_CHANNEL;
1854 /* Fill in packet len and prio, deliver upward */
1855 __skb_trim(pkt, rd->len);
1856 skb_pull(pkt, rd->dat_offset);
1858 /* prepare the descriptor for the next read */
1859 rd->len = rd->len_nxtfrm << 4;
1861 /* treat all packet as event if we don't know */
1862 rd->channel = SDPCM_EVENT_CHANNEL;
1864 if (pkt->len == 0) {
1865 brcmu_pkt_buf_free_skb(pkt);
1869 brcmf_rx_frame(bus->sdiodev->dev, pkt);
1872 rxcount = maxframes - rxleft;
1873 /* Message if we hit the limit */
1875 brcmf_dbg(DATA, "hit rx limit of %d frames\n", maxframes);
1877 brcmf_dbg(DATA, "processed %d frames\n", rxcount);
1878 /* Back off rxseq if awaiting rtx, update rx_seq */
1881 bus->rx_seq = rd->seq_num;
1887 brcmf_sdio_wait_event_wakeup(struct brcmf_sdio *bus)
1889 if (waitqueue_active(&bus->ctrl_wait))
1890 wake_up_interruptible(&bus->ctrl_wait);
1894 static int brcmf_sdio_txpkt_hdalign(struct brcmf_sdio *bus, struct sk_buff *pkt)
1899 dat_buf = (u8 *)(pkt->data);
1901 /* Check head padding */
1902 head_pad = ((unsigned long)dat_buf % bus->head_align);
1904 if (skb_headroom(pkt) < head_pad) {
1905 bus->sdiodev->bus_if->tx_realloc++;
1907 if (skb_cow(pkt, head_pad))
1910 skb_push(pkt, head_pad);
1911 dat_buf = (u8 *)(pkt->data);
1912 memset(dat_buf, 0, head_pad + bus->tx_hdrlen);
1918 * struct brcmf_skbuff_cb reserves first two bytes in sk_buff::cb for
1921 /* flag marking a dummy skb added for DMA alignment requirement */
1922 #define ALIGN_SKB_FLAG 0x8000
1923 /* bit mask of data length chopped from the previous packet */
1924 #define ALIGN_SKB_CHOP_LEN_MASK 0x7fff
1926 static int brcmf_sdio_txpkt_prep_sg(struct brcmf_sdio *bus,
1927 struct sk_buff_head *pktq,
1928 struct sk_buff *pkt, u16 total_len)
1930 struct brcmf_sdio_dev *sdiodev;
1931 struct sk_buff *pkt_pad;
1932 u16 tail_pad, tail_chop, chain_pad;
1933 unsigned int blksize;
1937 sdiodev = bus->sdiodev;
1938 blksize = sdiodev->func[SDIO_FUNC_2]->cur_blksize;
1939 /* sg entry alignment should be a divisor of block size */
1940 WARN_ON(blksize % bus->sgentry_align);
1942 /* Check tail padding */
1943 lastfrm = skb_queue_is_last(pktq, pkt);
1945 tail_chop = pkt->len % bus->sgentry_align;
1947 tail_pad = bus->sgentry_align - tail_chop;
1948 chain_pad = (total_len + tail_pad) % blksize;
1949 if (lastfrm && chain_pad)
1950 tail_pad += blksize - chain_pad;
1951 if (skb_tailroom(pkt) < tail_pad && pkt->len > blksize) {
1952 pkt_pad = bus->txglom_sgpad;
1953 if (pkt_pad == NULL)
1954 brcmu_pkt_buf_get_skb(tail_pad + tail_chop);
1955 if (pkt_pad == NULL)
1957 ret = brcmf_sdio_txpkt_hdalign(bus, pkt_pad);
1958 if (unlikely(ret < 0))
1960 memcpy(pkt_pad->data,
1961 pkt->data + pkt->len - tail_chop,
1963 *(u32 *)(pkt_pad->cb) = ALIGN_SKB_FLAG + tail_chop;
1964 skb_trim(pkt, pkt->len - tail_chop);
1965 __skb_queue_after(pktq, pkt, pkt_pad);
1967 ntail = pkt->data_len + tail_pad -
1968 (pkt->end - pkt->tail);
1969 if (skb_cloned(pkt) || ntail > 0)
1970 if (pskb_expand_head(pkt, 0, ntail, GFP_ATOMIC))
1972 if (skb_linearize(pkt))
1974 __skb_put(pkt, tail_pad);
1981 * brcmf_sdio_txpkt_prep - packet preparation for transmit
1982 * @bus: brcmf_sdio structure pointer
1983 * @pktq: packet list pointer
1984 * @chan: virtual channel to transmit the packet
1986 * Processes to be applied to the packet
1987 * - Align data buffer pointer
1988 * - Align data buffer length
1990 * Return: negative value if there is error
1993 brcmf_sdio_txpkt_prep(struct brcmf_sdio *bus, struct sk_buff_head *pktq,
1996 u16 head_pad, total_len;
1997 struct sk_buff *pkt_next;
2000 struct brcmf_sdio_hdrinfo hd_info = {0};
2002 txseq = bus->tx_seq;
2004 skb_queue_walk(pktq, pkt_next) {
2005 /* alignment packet inserted in previous
2006 * loop cycle can be skipped as it is
2007 * already properly aligned and does not
2008 * need an sdpcm header.
2010 if (*(u32 *)(pkt_next->cb) & ALIGN_SKB_FLAG)
2013 /* align packet data pointer */
2014 ret = brcmf_sdio_txpkt_hdalign(bus, pkt_next);
2017 head_pad = (u16)ret;
2019 memset(pkt_next->data, 0, head_pad + bus->tx_hdrlen);
2021 total_len += pkt_next->len;
2023 hd_info.len = pkt_next->len;
2024 hd_info.lastfrm = skb_queue_is_last(pktq, pkt_next);
2025 if (bus->txglom && pktq->qlen > 1) {
2026 ret = brcmf_sdio_txpkt_prep_sg(bus, pktq,
2027 pkt_next, total_len);
2030 hd_info.tail_pad = (u16)ret;
2031 total_len += (u16)ret;
2034 hd_info.channel = chan;
2035 hd_info.dat_offset = head_pad + bus->tx_hdrlen;
2036 hd_info.seq_num = txseq++;
2038 /* Now fill the header */
2039 brcmf_sdio_hdpack(bus, pkt_next->data, &hd_info);
2041 if (BRCMF_BYTES_ON() &&
2042 ((BRCMF_CTL_ON() && chan == SDPCM_CONTROL_CHANNEL) ||
2043 (BRCMF_DATA_ON() && chan != SDPCM_CONTROL_CHANNEL)))
2044 brcmf_dbg_hex_dump(true, pkt_next, hd_info.len,
2046 else if (BRCMF_HDRS_ON())
2047 brcmf_dbg_hex_dump(true, pkt_next,
2048 head_pad + bus->tx_hdrlen,
2051 /* Hardware length tag of the first packet should be total
2052 * length of the chain (including padding)
2055 brcmf_sdio_update_hwhdr(pktq->next->data, total_len);
2060 * brcmf_sdio_txpkt_postp - packet post processing for transmit
2061 * @bus: brcmf_sdio structure pointer
2062 * @pktq: packet list pointer
2064 * Processes to be applied to the packet
2065 * - Remove head padding
2066 * - Remove tail padding
2069 brcmf_sdio_txpkt_postp(struct brcmf_sdio *bus, struct sk_buff_head *pktq)
2074 u32 dummy_flags, chop_len;
2075 struct sk_buff *pkt_next, *tmp, *pkt_prev;
2077 skb_queue_walk_safe(pktq, pkt_next, tmp) {
2078 dummy_flags = *(u32 *)(pkt_next->cb);
2079 if (dummy_flags & ALIGN_SKB_FLAG) {
2080 chop_len = dummy_flags & ALIGN_SKB_CHOP_LEN_MASK;
2082 pkt_prev = pkt_next->prev;
2083 skb_put(pkt_prev, chop_len);
2085 __skb_unlink(pkt_next, pktq);
2086 brcmu_pkt_buf_free_skb(pkt_next);
2088 hdr = pkt_next->data + bus->tx_hdrlen - SDPCM_SWHDR_LEN;
2089 dat_offset = le32_to_cpu(*(__le32 *)hdr);
2090 dat_offset = (dat_offset & SDPCM_DOFFSET_MASK) >>
2091 SDPCM_DOFFSET_SHIFT;
2092 skb_pull(pkt_next, dat_offset);
2094 tail_pad = le16_to_cpu(*(__le16 *)(hdr - 2));
2095 skb_trim(pkt_next, pkt_next->len - tail_pad);
2101 /* Writes a HW/SW header into the packet and sends it. */
2102 /* Assumes: (a) header space already there, (b) caller holds lock */
2103 static int brcmf_sdio_txpkt(struct brcmf_sdio *bus, struct sk_buff_head *pktq,
2108 struct sk_buff *pkt_next, *tmp;
2110 brcmf_dbg(TRACE, "Enter\n");
2112 ret = brcmf_sdio_txpkt_prep(bus, pktq, chan);
2116 sdio_claim_host(bus->sdiodev->func[1]);
2117 ret = brcmf_sdiod_send_pkt(bus->sdiodev, pktq);
2118 bus->sdcnt.f2txdata++;
2121 /* On failure, abort the command and terminate the frame */
2122 brcmf_dbg(INFO, "sdio error %d, abort command and terminate frame\n",
2124 bus->sdcnt.tx_sderrs++;
2126 brcmf_sdiod_abort(bus->sdiodev, SDIO_FUNC_2);
2127 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL,
2129 bus->sdcnt.f1regdata++;
2131 for (i = 0; i < 3; i++) {
2133 hi = brcmf_sdiod_regrb(bus->sdiodev,
2134 SBSDIO_FUNC1_WFRAMEBCHI, NULL);
2135 lo = brcmf_sdiod_regrb(bus->sdiodev,
2136 SBSDIO_FUNC1_WFRAMEBCLO, NULL);
2137 bus->sdcnt.f1regdata += 2;
2138 if ((hi == 0) && (lo == 0))
2142 sdio_release_host(bus->sdiodev->func[1]);
2145 brcmf_sdio_txpkt_postp(bus, pktq);
2147 bus->tx_seq = (bus->tx_seq + pktq->qlen) % SDPCM_SEQ_WRAP;
2148 skb_queue_walk_safe(pktq, pkt_next, tmp) {
2149 __skb_unlink(pkt_next, pktq);
2150 brcmf_txcomplete(bus->sdiodev->dev, pkt_next, ret == 0);
2155 static uint brcmf_sdio_sendfromq(struct brcmf_sdio *bus, uint maxframes)
2157 struct sk_buff *pkt;
2158 struct sk_buff_head pktq;
2160 int ret = 0, prec_out, i;
2162 u8 tx_prec_map, pkt_num;
2164 brcmf_dbg(TRACE, "Enter\n");
2166 tx_prec_map = ~bus->flowcontrol;
2168 /* Send frames until the limit or some other event */
2169 for (cnt = 0; (cnt < maxframes) && data_ok(bus);) {
2171 __skb_queue_head_init(&pktq);
2173 pkt_num = min_t(u8, bus->tx_max - bus->tx_seq,
2174 brcmf_sdio_txglomsz);
2175 pkt_num = min_t(u32, pkt_num,
2176 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol));
2177 spin_lock_bh(&bus->txqlock);
2178 for (i = 0; i < pkt_num; i++) {
2179 pkt = brcmu_pktq_mdeq(&bus->txq, tx_prec_map,
2183 __skb_queue_tail(&pktq, pkt);
2185 spin_unlock_bh(&bus->txqlock);
2189 ret = brcmf_sdio_txpkt(bus, &pktq, SDPCM_DATA_CHANNEL);
2192 /* In poll mode, need to check for other events */
2193 if (!bus->intr && cnt) {
2194 /* Check device status, signal pending interrupt */
2195 sdio_claim_host(bus->sdiodev->func[1]);
2196 ret = r_sdreg32(bus, &intstatus,
2197 offsetof(struct sdpcmd_regs,
2199 sdio_release_host(bus->sdiodev->func[1]);
2200 bus->sdcnt.f2txdata++;
2203 if (intstatus & bus->hostintmask)
2204 atomic_set(&bus->ipend, 1);
2208 /* Deflow-control stack if needed */
2209 if ((bus->sdiodev->bus_if->state == BRCMF_BUS_DATA) &&
2210 bus->txoff && (pktq_len(&bus->txq) < TXLOW)) {
2212 brcmf_txflowblock(bus->sdiodev->dev, false);
2218 static void brcmf_sdio_bus_stop(struct device *dev)
2220 u32 local_hostintmask;
2223 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
2224 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
2225 struct brcmf_sdio *bus = sdiodev->bus;
2227 brcmf_dbg(TRACE, "Enter\n");
2229 if (bus->watchdog_tsk) {
2230 send_sig(SIGTERM, bus->watchdog_tsk, 1);
2231 kthread_stop(bus->watchdog_tsk);
2232 bus->watchdog_tsk = NULL;
2235 sdio_claim_host(bus->sdiodev->func[1]);
2237 /* Enable clock for device interrupts */
2238 brcmf_sdio_bus_sleep(bus, false, false);
2240 /* Disable and clear interrupts at the chip level also */
2241 w_sdreg32(bus, 0, offsetof(struct sdpcmd_regs, hostintmask));
2242 local_hostintmask = bus->hostintmask;
2243 bus->hostintmask = 0;
2245 /* Change our idea of bus state */
2246 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
2248 /* Force clocks on backplane to be sure F2 interrupt propagates */
2249 saveclk = brcmf_sdiod_regrb(bus->sdiodev,
2250 SBSDIO_FUNC1_CHIPCLKCSR, &err);
2252 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
2253 (saveclk | SBSDIO_FORCE_HT), &err);
2256 brcmf_err("Failed to force clock for F2: err %d\n", err);
2258 /* Turn off the bus (F2), free any pending packets */
2259 brcmf_dbg(INTR, "disable SDIO interrupts\n");
2260 sdio_disable_func(bus->sdiodev->func[SDIO_FUNC_2]);
2262 /* Clear any pending interrupts now that F2 is disabled */
2263 w_sdreg32(bus, local_hostintmask,
2264 offsetof(struct sdpcmd_regs, intstatus));
2266 /* Turn off the backplane clock (only) */
2267 brcmf_sdio_clkctl(bus, CLK_SDONLY, false);
2268 sdio_release_host(bus->sdiodev->func[1]);
2270 /* Clear the data packet queues */
2271 brcmu_pktq_flush(&bus->txq, true, NULL, NULL);
2273 /* Clear any held glomming stuff */
2275 brcmu_pkt_buf_free_skb(bus->glomd);
2276 brcmf_sdio_free_glom(bus);
2278 /* Clear rx control and wake any waiters */
2279 spin_lock_bh(&bus->rxctl_lock);
2281 spin_unlock_bh(&bus->rxctl_lock);
2282 brcmf_sdio_dcmd_resp_wake(bus);
2284 /* Reset some F2 state stuff */
2285 bus->rxskip = false;
2286 bus->tx_seq = bus->rx_seq = 0;
2289 static inline void brcmf_sdio_clrintr(struct brcmf_sdio *bus)
2291 unsigned long flags;
2293 if (bus->sdiodev->oob_irq_requested) {
2294 spin_lock_irqsave(&bus->sdiodev->irq_en_lock, flags);
2295 if (!bus->sdiodev->irq_en && !atomic_read(&bus->ipend)) {
2296 enable_irq(bus->sdiodev->pdata->oob_irq_nr);
2297 bus->sdiodev->irq_en = true;
2299 spin_unlock_irqrestore(&bus->sdiodev->irq_en_lock, flags);
2303 static int brcmf_sdio_intr_rstatus(struct brcmf_sdio *bus)
2310 idx = brcmf_sdio_chip_getinfidx(bus->ci, BCMA_CORE_SDIO_DEV);
2311 addr = bus->ci->c_inf[idx].base +
2312 offsetof(struct sdpcmd_regs, intstatus);
2314 val = brcmf_sdiod_regrl(bus->sdiodev, addr, &ret);
2315 bus->sdcnt.f1regdata++;
2319 val &= bus->hostintmask;
2320 atomic_set(&bus->fcstate, !!(val & I_HMB_FC_STATE));
2322 /* Clear interrupts */
2324 brcmf_sdiod_regwl(bus->sdiodev, addr, val, &ret);
2325 bus->sdcnt.f1regdata++;
2329 atomic_set(&bus->intstatus, 0);
2331 for_each_set_bit(n, &val, 32)
2332 set_bit(n, (unsigned long *)&bus->intstatus.counter);
2338 static void brcmf_sdio_dpc(struct brcmf_sdio *bus)
2341 unsigned long intstatus;
2342 uint rxlimit = bus->rxbound; /* Rx frames to read before resched */
2343 uint txlimit = bus->txbound; /* Tx frames to send before resched */
2344 uint framecnt = 0; /* Temporary counter of tx/rx frames */
2347 brcmf_dbg(TRACE, "Enter\n");
2349 sdio_claim_host(bus->sdiodev->func[1]);
2351 /* If waiting for HTAVAIL, check status */
2352 if (!bus->sr_enabled && bus->clkstate == CLK_PENDING) {
2353 u8 clkctl, devctl = 0;
2356 /* Check for inconsistent device control */
2357 devctl = brcmf_sdiod_regrb(bus->sdiodev,
2358 SBSDIO_DEVICE_CTL, &err);
2360 brcmf_err("error reading DEVCTL: %d\n", err);
2361 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
2365 /* Read CSR, if clock on switch to AVAIL, else ignore */
2366 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
2367 SBSDIO_FUNC1_CHIPCLKCSR, &err);
2369 brcmf_err("error reading CSR: %d\n",
2371 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
2374 brcmf_dbg(SDIO, "DPC: PENDING, devctl 0x%02x clkctl 0x%02x\n",
2377 if (SBSDIO_HTAV(clkctl)) {
2378 devctl = brcmf_sdiod_regrb(bus->sdiodev,
2379 SBSDIO_DEVICE_CTL, &err);
2381 brcmf_err("error reading DEVCTL: %d\n",
2383 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
2385 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
2386 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
2389 brcmf_err("error writing DEVCTL: %d\n",
2391 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
2393 bus->clkstate = CLK_AVAIL;
2397 /* Make sure backplane clock is on */
2398 brcmf_sdio_bus_sleep(bus, false, true);
2400 /* Pending interrupt indicates new device status */
2401 if (atomic_read(&bus->ipend) > 0) {
2402 atomic_set(&bus->ipend, 0);
2403 err = brcmf_sdio_intr_rstatus(bus);
2406 /* Start with leftover status bits */
2407 intstatus = atomic_xchg(&bus->intstatus, 0);
2409 /* Handle flow-control change: read new state in case our ack
2410 * crossed another change interrupt. If change still set, assume
2411 * FC ON for safety, let next loop through do the debounce.
2413 if (intstatus & I_HMB_FC_CHANGE) {
2414 intstatus &= ~I_HMB_FC_CHANGE;
2415 err = w_sdreg32(bus, I_HMB_FC_CHANGE,
2416 offsetof(struct sdpcmd_regs, intstatus));
2418 err = r_sdreg32(bus, &newstatus,
2419 offsetof(struct sdpcmd_regs, intstatus));
2420 bus->sdcnt.f1regdata += 2;
2421 atomic_set(&bus->fcstate,
2422 !!(newstatus & (I_HMB_FC_STATE | I_HMB_FC_CHANGE)));
2423 intstatus |= (newstatus & bus->hostintmask);
2426 /* Handle host mailbox indication */
2427 if (intstatus & I_HMB_HOST_INT) {
2428 intstatus &= ~I_HMB_HOST_INT;
2429 intstatus |= brcmf_sdio_hostmail(bus);
2432 sdio_release_host(bus->sdiodev->func[1]);
2434 /* Generally don't ask for these, can get CRC errors... */
2435 if (intstatus & I_WR_OOSYNC) {
2436 brcmf_err("Dongle reports WR_OOSYNC\n");
2437 intstatus &= ~I_WR_OOSYNC;
2440 if (intstatus & I_RD_OOSYNC) {
2441 brcmf_err("Dongle reports RD_OOSYNC\n");
2442 intstatus &= ~I_RD_OOSYNC;
2445 if (intstatus & I_SBINT) {
2446 brcmf_err("Dongle reports SBINT\n");
2447 intstatus &= ~I_SBINT;
2450 /* Would be active due to wake-wlan in gSPI */
2451 if (intstatus & I_CHIPACTIVE) {
2452 brcmf_dbg(INFO, "Dongle reports CHIPACTIVE\n");
2453 intstatus &= ~I_CHIPACTIVE;
2456 /* Ignore frame indications if rxskip is set */
2458 intstatus &= ~I_HMB_FRAME_IND;
2460 /* On frame indication, read available frames */
2461 if (PKT_AVAILABLE() && bus->clkstate == CLK_AVAIL) {
2462 framecnt = brcmf_sdio_readframes(bus, rxlimit);
2463 if (!bus->rxpending)
2464 intstatus &= ~I_HMB_FRAME_IND;
2465 rxlimit -= min(framecnt, rxlimit);
2468 /* Keep still-pending events for next scheduling */
2470 for_each_set_bit(n, &intstatus, 32)
2471 set_bit(n, (unsigned long *)&bus->intstatus.counter);
2474 brcmf_sdio_clrintr(bus);
2476 if (data_ok(bus) && bus->ctrl_frame_stat &&
2477 (bus->clkstate == CLK_AVAIL)) {
2480 sdio_claim_host(bus->sdiodev->func[1]);
2481 err = brcmf_sdiod_send_buf(bus->sdiodev, bus->ctrl_frame_buf,
2482 (u32)bus->ctrl_frame_len);
2485 /* On failure, abort the command and
2486 terminate the frame */
2487 brcmf_dbg(INFO, "sdio error %d, abort command and terminate frame\n",
2489 bus->sdcnt.tx_sderrs++;
2491 brcmf_sdiod_abort(bus->sdiodev, SDIO_FUNC_2);
2493 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL,
2495 bus->sdcnt.f1regdata++;
2497 for (i = 0; i < 3; i++) {
2499 hi = brcmf_sdiod_regrb(bus->sdiodev,
2500 SBSDIO_FUNC1_WFRAMEBCHI,
2502 lo = brcmf_sdiod_regrb(bus->sdiodev,
2503 SBSDIO_FUNC1_WFRAMEBCLO,
2505 bus->sdcnt.f1regdata += 2;
2506 if ((hi == 0) && (lo == 0))
2511 bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQ_WRAP;
2513 sdio_release_host(bus->sdiodev->func[1]);
2514 bus->ctrl_frame_stat = false;
2515 brcmf_sdio_wait_event_wakeup(bus);
2517 /* Send queued frames (limit 1 if rx may still be pending) */
2518 else if ((bus->clkstate == CLK_AVAIL) && !atomic_read(&bus->fcstate) &&
2519 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) && txlimit
2521 framecnt = bus->rxpending ? min(txlimit, bus->txminmax) :
2523 framecnt = brcmf_sdio_sendfromq(bus, framecnt);
2524 txlimit -= framecnt;
2527 if ((bus->sdiodev->bus_if->state == BRCMF_BUS_DOWN) || (err != 0)) {
2528 brcmf_err("failed backplane access over SDIO, halting operation\n");
2529 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
2530 atomic_set(&bus->intstatus, 0);
2531 } else if (atomic_read(&bus->intstatus) ||
2532 atomic_read(&bus->ipend) > 0 ||
2533 (!atomic_read(&bus->fcstate) &&
2534 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) &&
2535 data_ok(bus)) || PKT_AVAILABLE()) {
2536 atomic_inc(&bus->dpc_tskcnt);
2539 /* If we're done for now, turn off clock request. */
2540 if ((bus->clkstate != CLK_PENDING)
2541 && bus->idletime == BRCMF_IDLE_IMMEDIATE) {
2542 bus->activity = false;
2543 brcmf_dbg(SDIO, "idle state\n");
2544 sdio_claim_host(bus->sdiodev->func[1]);
2545 brcmf_sdio_bus_sleep(bus, true, false);
2546 sdio_release_host(bus->sdiodev->func[1]);
2550 static struct pktq *brcmf_sdio_bus_gettxq(struct device *dev)
2552 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
2553 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
2554 struct brcmf_sdio *bus = sdiodev->bus;
2559 static int brcmf_sdio_bus_txdata(struct device *dev, struct sk_buff *pkt)
2563 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
2564 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
2565 struct brcmf_sdio *bus = sdiodev->bus;
2568 brcmf_dbg(TRACE, "Enter\n");
2572 /* Add space for the header */
2573 skb_push(pkt, bus->tx_hdrlen);
2574 /* precondition: IS_ALIGNED((unsigned long)(pkt->data), 2) */
2576 prec = prio2prec((pkt->priority & PRIOMASK));
2578 /* Check for existing queue, current flow-control,
2579 pending event, or pending clock */
2580 brcmf_dbg(TRACE, "deferring pktq len %d\n", pktq_len(&bus->txq));
2581 bus->sdcnt.fcqueued++;
2583 /* Priority based enq */
2584 spin_lock_irqsave(&bus->txqlock, flags);
2585 if (!brcmf_c_prec_enq(bus->sdiodev->dev, &bus->txq, pkt, prec)) {
2586 skb_pull(pkt, bus->tx_hdrlen);
2587 brcmf_err("out of bus->txq !!!\n");
2593 if (pktq_len(&bus->txq) >= TXHI) {
2595 brcmf_txflowblock(bus->sdiodev->dev, true);
2597 spin_unlock_irqrestore(&bus->txqlock, flags);
2600 if (pktq_plen(&bus->txq, prec) > qcount[prec])
2601 qcount[prec] = pktq_plen(&bus->txq, prec);
2604 if (atomic_read(&bus->dpc_tskcnt) == 0) {
2605 atomic_inc(&bus->dpc_tskcnt);
2606 queue_work(bus->brcmf_wq, &bus->datawork);
2613 #define CONSOLE_LINE_MAX 192
2615 static int brcmf_sdio_readconsole(struct brcmf_sdio *bus)
2617 struct brcmf_console *c = &bus->console;
2618 u8 line[CONSOLE_LINE_MAX], ch;
2622 /* Don't do anything until FWREADY updates console address */
2623 if (bus->console_addr == 0)
2626 /* Read console log struct */
2627 addr = bus->console_addr + offsetof(struct rte_console, log_le);
2628 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, (u8 *)&c->log_le,
2633 /* Allocate console buffer (one time only) */
2634 if (c->buf == NULL) {
2635 c->bufsize = le32_to_cpu(c->log_le.buf_size);
2636 c->buf = kmalloc(c->bufsize, GFP_ATOMIC);
2641 idx = le32_to_cpu(c->log_le.idx);
2643 /* Protect against corrupt value */
2644 if (idx > c->bufsize)
2647 /* Skip reading the console buffer if the index pointer
2652 /* Read the console buffer */
2653 addr = le32_to_cpu(c->log_le.buf);
2654 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, c->buf, c->bufsize);
2658 while (c->last != idx) {
2659 for (n = 0; n < CONSOLE_LINE_MAX - 2; n++) {
2660 if (c->last == idx) {
2661 /* This would output a partial line.
2663 * the buffer pointer and output this
2664 * line next time around.
2669 c->last = c->bufsize - n;
2672 ch = c->buf[c->last];
2673 c->last = (c->last + 1) % c->bufsize;
2680 if (line[n - 1] == '\r')
2683 pr_debug("CONSOLE: %s\n", line);
2692 static int brcmf_sdio_tx_frame(struct brcmf_sdio *bus, u8 *frame, u16 len)
2697 bus->ctrl_frame_stat = false;
2698 ret = brcmf_sdiod_send_buf(bus->sdiodev, frame, len);
2701 /* On failure, abort the command and terminate the frame */
2702 brcmf_dbg(INFO, "sdio error %d, abort command and terminate frame\n",
2704 bus->sdcnt.tx_sderrs++;
2706 brcmf_sdiod_abort(bus->sdiodev, SDIO_FUNC_2);
2708 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL,
2710 bus->sdcnt.f1regdata++;
2712 for (i = 0; i < 3; i++) {
2714 hi = brcmf_sdiod_regrb(bus->sdiodev,
2715 SBSDIO_FUNC1_WFRAMEBCHI, NULL);
2716 lo = brcmf_sdiod_regrb(bus->sdiodev,
2717 SBSDIO_FUNC1_WFRAMEBCLO, NULL);
2718 bus->sdcnt.f1regdata += 2;
2719 if (hi == 0 && lo == 0)
2725 bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQ_WRAP;
2731 brcmf_sdio_bus_txctl(struct device *dev, unsigned char *msg, uint msglen)
2738 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
2739 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
2740 struct brcmf_sdio *bus = sdiodev->bus;
2741 struct brcmf_sdio_hdrinfo hd_info = {0};
2743 brcmf_dbg(TRACE, "Enter\n");
2745 /* Back the pointer to make a room for bus header */
2746 frame = msg - bus->tx_hdrlen;
2747 len = (msglen += bus->tx_hdrlen);
2749 /* Add alignment padding (optional for ctl frames) */
2750 doff = ((unsigned long)frame % bus->head_align);
2755 memset(frame, 0, doff + bus->tx_hdrlen);
2757 /* precondition: doff < bus->head_align */
2758 doff += bus->tx_hdrlen;
2760 /* Round send length to next SDIO block */
2762 if (bus->roundup && bus->blocksize && (len > bus->blocksize)) {
2763 pad = bus->blocksize - (len % bus->blocksize);
2764 if ((pad > bus->roundup) || (pad >= bus->blocksize))
2766 } else if (len % bus->head_align) {
2767 pad = bus->head_align - (len % bus->head_align);
2771 /* precondition: IS_ALIGNED((unsigned long)frame, 2) */
2773 /* Make sure backplane clock is on */
2774 sdio_claim_host(bus->sdiodev->func[1]);
2775 brcmf_sdio_bus_sleep(bus, false, false);
2776 sdio_release_host(bus->sdiodev->func[1]);
2778 hd_info.len = (u16)msglen;
2779 hd_info.channel = SDPCM_CONTROL_CHANNEL;
2780 hd_info.dat_offset = doff;
2781 hd_info.seq_num = bus->tx_seq;
2782 hd_info.lastfrm = true;
2783 hd_info.tail_pad = pad;
2784 brcmf_sdio_hdpack(bus, frame, &hd_info);
2787 brcmf_sdio_update_hwhdr(frame, len);
2789 if (!data_ok(bus)) {
2790 brcmf_dbg(INFO, "No bus credit bus->tx_max %d, bus->tx_seq %d\n",
2791 bus->tx_max, bus->tx_seq);
2792 bus->ctrl_frame_stat = true;
2794 bus->ctrl_frame_buf = frame;
2795 bus->ctrl_frame_len = len;
2797 wait_event_interruptible_timeout(bus->ctrl_wait,
2798 !bus->ctrl_frame_stat,
2799 msecs_to_jiffies(2000));
2801 if (!bus->ctrl_frame_stat) {
2802 brcmf_dbg(SDIO, "ctrl_frame_stat == false\n");
2805 brcmf_dbg(SDIO, "ctrl_frame_stat == true\n");
2811 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_CTL_ON(),
2812 frame, len, "Tx Frame:\n");
2813 brcmf_dbg_hex_dump(!(BRCMF_BYTES_ON() && BRCMF_CTL_ON()) &&
2815 frame, min_t(u16, len, 16), "TxHdr:\n");
2818 sdio_claim_host(bus->sdiodev->func[1]);
2819 ret = brcmf_sdio_tx_frame(bus, frame, len);
2820 sdio_release_host(bus->sdiodev->func[1]);
2821 } while (ret < 0 && retries++ < TXRETRIES);
2824 if ((bus->idletime == BRCMF_IDLE_IMMEDIATE) &&
2825 atomic_read(&bus->dpc_tskcnt) == 0) {
2826 bus->activity = false;
2827 sdio_claim_host(bus->sdiodev->func[1]);
2828 brcmf_dbg(INFO, "idle\n");
2829 brcmf_sdio_clkctl(bus, CLK_NONE, true);
2830 sdio_release_host(bus->sdiodev->func[1]);
2834 bus->sdcnt.tx_ctlerrs++;
2836 bus->sdcnt.tx_ctlpkts++;
2838 return ret ? -EIO : 0;
2842 static inline bool brcmf_sdio_valid_shared_address(u32 addr)
2844 return !(addr == 0 || ((~addr >> 16) & 0xffff) == (addr & 0xffff));
2847 static int brcmf_sdio_readshared(struct brcmf_sdio *bus,
2848 struct sdpcm_shared *sh)
2853 struct sdpcm_shared_le sh_le;
2856 shaddr = bus->ci->rambase + bus->ramsize - 4;
2859 * Read last word in socram to determine
2860 * address of sdpcm_shared structure
2862 sdio_claim_host(bus->sdiodev->func[1]);
2863 brcmf_sdio_bus_sleep(bus, false, false);
2864 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, shaddr, (u8 *)&addr_le, 4);
2865 sdio_release_host(bus->sdiodev->func[1]);
2869 addr = le32_to_cpu(addr_le);
2871 brcmf_dbg(SDIO, "sdpcm_shared address 0x%08X\n", addr);
2874 * Check if addr is valid.
2875 * NVRAM length at the end of memory should have been overwritten.
2877 if (!brcmf_sdio_valid_shared_address(addr)) {
2878 brcmf_err("invalid sdpcm_shared address 0x%08X\n",
2883 /* Read hndrte_shared structure */
2884 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, (u8 *)&sh_le,
2885 sizeof(struct sdpcm_shared_le));
2890 sh->flags = le32_to_cpu(sh_le.flags);
2891 sh->trap_addr = le32_to_cpu(sh_le.trap_addr);
2892 sh->assert_exp_addr = le32_to_cpu(sh_le.assert_exp_addr);
2893 sh->assert_file_addr = le32_to_cpu(sh_le.assert_file_addr);
2894 sh->assert_line = le32_to_cpu(sh_le.assert_line);
2895 sh->console_addr = le32_to_cpu(sh_le.console_addr);
2896 sh->msgtrace_addr = le32_to_cpu(sh_le.msgtrace_addr);
2898 if ((sh->flags & SDPCM_SHARED_VERSION_MASK) > SDPCM_SHARED_VERSION) {
2899 brcmf_err("sdpcm shared version unsupported: dhd %d dongle %d\n",
2900 SDPCM_SHARED_VERSION,
2901 sh->flags & SDPCM_SHARED_VERSION_MASK);
2908 static int brcmf_sdio_dump_console(struct brcmf_sdio *bus,
2909 struct sdpcm_shared *sh, char __user *data,
2912 u32 addr, console_ptr, console_size, console_index;
2913 char *conbuf = NULL;
2919 /* obtain console information from device memory */
2920 addr = sh->console_addr + offsetof(struct rte_console, log_le);
2921 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
2922 (u8 *)&sh_val, sizeof(u32));
2925 console_ptr = le32_to_cpu(sh_val);
2927 addr = sh->console_addr + offsetof(struct rte_console, log_le.buf_size);
2928 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
2929 (u8 *)&sh_val, sizeof(u32));
2932 console_size = le32_to_cpu(sh_val);
2934 addr = sh->console_addr + offsetof(struct rte_console, log_le.idx);
2935 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
2936 (u8 *)&sh_val, sizeof(u32));
2939 console_index = le32_to_cpu(sh_val);
2941 /* allocate buffer for console data */
2942 if (console_size <= CONSOLE_BUFFER_MAX)
2943 conbuf = vzalloc(console_size+1);
2948 /* obtain the console data from device */
2949 conbuf[console_size] = '\0';
2950 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, console_ptr, (u8 *)conbuf,
2955 rv = simple_read_from_buffer(data, count, &pos,
2956 conbuf + console_index,
2957 console_size - console_index);
2962 if (console_index > 0) {
2964 rv = simple_read_from_buffer(data+nbytes, count, &pos,
2965 conbuf, console_index - 1);
2975 static int brcmf_sdio_trap_info(struct brcmf_sdio *bus, struct sdpcm_shared *sh,
2976 char __user *data, size_t count)
2980 struct brcmf_trap_info tr;
2983 if ((sh->flags & SDPCM_SHARED_TRAP) == 0) {
2984 brcmf_dbg(INFO, "no trap in firmware\n");
2988 error = brcmf_sdiod_ramrw(bus->sdiodev, false, sh->trap_addr, (u8 *)&tr,
2989 sizeof(struct brcmf_trap_info));
2993 res = scnprintf(buf, sizeof(buf),
2994 "dongle trap info: type 0x%x @ epc 0x%08x\n"
2995 " cpsr 0x%08x spsr 0x%08x sp 0x%08x\n"
2996 " lr 0x%08x pc 0x%08x offset 0x%x\n"
2997 " r0 0x%08x r1 0x%08x r2 0x%08x r3 0x%08x\n"
2998 " r4 0x%08x r5 0x%08x r6 0x%08x r7 0x%08x\n",
2999 le32_to_cpu(tr.type), le32_to_cpu(tr.epc),
3000 le32_to_cpu(tr.cpsr), le32_to_cpu(tr.spsr),
3001 le32_to_cpu(tr.r13), le32_to_cpu(tr.r14),
3002 le32_to_cpu(tr.pc), sh->trap_addr,
3003 le32_to_cpu(tr.r0), le32_to_cpu(tr.r1),
3004 le32_to_cpu(tr.r2), le32_to_cpu(tr.r3),
3005 le32_to_cpu(tr.r4), le32_to_cpu(tr.r5),
3006 le32_to_cpu(tr.r6), le32_to_cpu(tr.r7));
3008 return simple_read_from_buffer(data, count, &pos, buf, res);
3011 static int brcmf_sdio_assert_info(struct brcmf_sdio *bus,
3012 struct sdpcm_shared *sh, char __user *data,
3017 char file[80] = "?";
3018 char expr[80] = "<???>";
3022 if ((sh->flags & SDPCM_SHARED_ASSERT_BUILT) == 0) {
3023 brcmf_dbg(INFO, "firmware not built with -assert\n");
3025 } else if ((sh->flags & SDPCM_SHARED_ASSERT) == 0) {
3026 brcmf_dbg(INFO, "no assert in dongle\n");
3030 sdio_claim_host(bus->sdiodev->func[1]);
3031 if (sh->assert_file_addr != 0) {
3032 error = brcmf_sdiod_ramrw(bus->sdiodev, false,
3033 sh->assert_file_addr, (u8 *)file, 80);
3037 if (sh->assert_exp_addr != 0) {
3038 error = brcmf_sdiod_ramrw(bus->sdiodev, false,
3039 sh->assert_exp_addr, (u8 *)expr, 80);
3043 sdio_release_host(bus->sdiodev->func[1]);
3045 res = scnprintf(buf, sizeof(buf),
3046 "dongle assert: %s:%d: assert(%s)\n",
3047 file, sh->assert_line, expr);
3048 return simple_read_from_buffer(data, count, &pos, buf, res);
3051 static int brcmf_sdio_checkdied(struct brcmf_sdio *bus)
3054 struct sdpcm_shared sh;
3056 error = brcmf_sdio_readshared(bus, &sh);
3061 if ((sh.flags & SDPCM_SHARED_ASSERT_BUILT) == 0)
3062 brcmf_dbg(INFO, "firmware not built with -assert\n");
3063 else if (sh.flags & SDPCM_SHARED_ASSERT)
3064 brcmf_err("assertion in dongle\n");
3066 if (sh.flags & SDPCM_SHARED_TRAP)
3067 brcmf_err("firmware trap in dongle\n");
3072 static int brcmf_sdio_died_dump(struct brcmf_sdio *bus, char __user *data,
3073 size_t count, loff_t *ppos)
3076 struct sdpcm_shared sh;
3083 error = brcmf_sdio_readshared(bus, &sh);
3087 error = brcmf_sdio_assert_info(bus, &sh, data, count);
3092 error = brcmf_sdio_trap_info(bus, &sh, data+nbytes, count);
3097 error = brcmf_sdio_dump_console(bus, &sh, data+nbytes, count);
3108 static ssize_t brcmf_sdio_forensic_read(struct file *f, char __user *data,
3109 size_t count, loff_t *ppos)
3111 struct brcmf_sdio *bus = f->private_data;
3114 res = brcmf_sdio_died_dump(bus, data, count, ppos);
3117 return (ssize_t)res;
3120 static const struct file_operations brcmf_sdio_forensic_ops = {
3121 .owner = THIS_MODULE,
3122 .open = simple_open,
3123 .read = brcmf_sdio_forensic_read
3126 static void brcmf_sdio_debugfs_create(struct brcmf_sdio *bus)
3128 struct brcmf_pub *drvr = bus->sdiodev->bus_if->drvr;
3129 struct dentry *dentry = brcmf_debugfs_get_devdir(drvr);
3131 if (IS_ERR_OR_NULL(dentry))
3134 debugfs_create_file("forensics", S_IRUGO, dentry, bus,
3135 &brcmf_sdio_forensic_ops);
3136 brcmf_debugfs_create_sdio_count(drvr, &bus->sdcnt);
3139 static int brcmf_sdio_checkdied(struct brcmf_sdio *bus)
3144 static void brcmf_sdio_debugfs_create(struct brcmf_sdio *bus)
3150 brcmf_sdio_bus_rxctl(struct device *dev, unsigned char *msg, uint msglen)
3156 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3157 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3158 struct brcmf_sdio *bus = sdiodev->bus;
3160 brcmf_dbg(TRACE, "Enter\n");
3162 /* Wait until control frame is available */
3163 timeleft = brcmf_sdio_dcmd_resp_wait(bus, &bus->rxlen, &pending);
3165 spin_lock_bh(&bus->rxctl_lock);
3167 memcpy(msg, bus->rxctl, min(msglen, rxlen));
3169 buf = bus->rxctl_orig;
3170 bus->rxctl_orig = NULL;
3172 spin_unlock_bh(&bus->rxctl_lock);
3176 brcmf_dbg(CTL, "resumed on rxctl frame, got %d expected %d\n",
3178 } else if (timeleft == 0) {
3179 brcmf_err("resumed on timeout\n");
3180 brcmf_sdio_checkdied(bus);
3181 } else if (pending) {
3182 brcmf_dbg(CTL, "cancelled\n");
3183 return -ERESTARTSYS;
3185 brcmf_dbg(CTL, "resumed for unknown reason?\n");
3186 brcmf_sdio_checkdied(bus);
3190 bus->sdcnt.rx_ctlpkts++;
3192 bus->sdcnt.rx_ctlerrs++;
3194 return rxlen ? (int)rxlen : -ETIMEDOUT;
3197 static bool brcmf_sdio_download_state(struct brcmf_sdio *bus, bool enter)
3199 struct chip_info *ci = bus->ci;
3201 /* To enter download state, disable ARM and reset SOCRAM.
3202 * To exit download state, simply reset ARM (default is RAM boot).
3205 bus->alp_only = true;
3207 brcmf_sdio_chip_enter_download(bus->sdiodev, ci);
3209 if (!brcmf_sdio_chip_exit_download(bus->sdiodev, ci))
3212 /* Allow HT Clock now that the ARM is running. */
3213 bus->alp_only = false;
3215 bus->sdiodev->bus_if->state = BRCMF_BUS_LOAD;
3223 brcmf_sdio_verifymemory(struct brcmf_sdio_dev *sdiodev, u32 ram_addr,
3224 u8 *ram_data, uint ram_sz)
3233 /* read back and verify */
3234 brcmf_dbg(INFO, "Compare RAM dl & ul at 0x%08x; size=%d\n", ram_addr,
3236 ram_cmp = kmalloc(MEMBLOCK, GFP_KERNEL);
3237 /* do not proceed while no memory but */
3243 while (offset < ram_sz) {
3244 len = ((offset + MEMBLOCK) < ram_sz) ? MEMBLOCK :
3246 err = brcmf_sdiod_ramrw(sdiodev, false, address, ram_cmp, len);
3248 brcmf_err("error %d on reading %d membytes at 0x%08x\n",
3252 } else if (memcmp(ram_cmp, &ram_data[offset], len)) {
3253 brcmf_err("Downloaded RAM image is corrupted, block offset is %d, len is %d\n",
3268 brcmf_sdio_verifymemory(struct brcmf_sdio_dev *sdiodev, u32 ram_addr,
3269 u8 *ram_data, uint ram_sz)
3275 static int brcmf_sdio_download_code_file(struct brcmf_sdio *bus)
3277 const struct firmware *fw;
3283 brcmf_dbg(TRACE, "Enter\n");
3285 fw = brcmf_sdio_get_fw(bus, BRCMF_FIRMWARE_BIN);
3289 if (brcmf_sdio_chip_getinfidx(bus->ci, BCMA_CORE_ARM_CR4) !=
3291 memcpy(&bus->ci->rst_vec, fw->data, sizeof(bus->ci->rst_vec));
3295 address = bus->ci->rambase;
3296 while (offset < fw->size) {
3297 len = ((offset + MEMBLOCK) < fw->size) ? MEMBLOCK :
3299 err = brcmf_sdiod_ramrw(bus->sdiodev, true, address,
3300 (u8 *)&fw->data[offset], len);
3302 brcmf_err("error %d on writing %d membytes at 0x%08x\n",
3310 if (!brcmf_sdio_verifymemory(bus->sdiodev, bus->ci->rambase,
3311 (u8 *)fw->data, fw->size))
3315 release_firmware(fw);
3320 static int brcmf_sdio_download_nvram(struct brcmf_sdio *bus)
3322 const struct firmware *nv;
3328 brcmf_dbg(TRACE, "Enter\n");
3330 nv = brcmf_sdio_get_fw(bus, BRCMF_FIRMWARE_NVRAM);
3334 vars = brcmf_nvram_strip(nv, &varsz);
3335 release_firmware(nv);
3340 address = bus->ci->ramsize - varsz + bus->ci->rambase;
3341 err = brcmf_sdiod_ramrw(bus->sdiodev, true, address, vars, varsz);
3343 brcmf_err("error %d on writing %d nvram bytes at 0x%08x\n",
3344 err, varsz, address);
3345 else if (!brcmf_sdio_verifymemory(bus->sdiodev, address, vars, varsz))
3348 brcmf_nvram_free(vars);
3353 static int brcmf_sdio_download_firmware(struct brcmf_sdio *bus)
3355 int bcmerror = -EFAULT;
3358 sdio_claim_host(bus->sdiodev->func[1]);
3359 brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
3361 /* Keep arm in reset */
3362 if (!brcmf_sdio_download_state(bus, true)) {
3363 brcmf_err("error placing ARM core in reset\n");
3367 if (brcmf_sdio_download_code_file(bus)) {
3368 brcmf_err("dongle image file download failed\n");
3372 if (brcmf_sdio_download_nvram(bus)) {
3373 brcmf_err("dongle nvram file download failed\n");
3377 /* Take arm out of reset */
3378 if (!brcmf_sdio_download_state(bus, false)) {
3379 brcmf_err("error getting out of ARM core reset\n");
3386 brcmf_sdio_clkctl(bus, CLK_SDONLY, false);
3387 sdio_release_host(bus->sdiodev->func[1]);
3391 static bool brcmf_sdio_sr_capable(struct brcmf_sdio *bus)
3393 u32 addr, reg, pmu_cc3_mask = ~0;
3396 brcmf_dbg(TRACE, "Enter\n");
3398 /* old chips with PMU version less than 17 don't support save restore */
3399 if (bus->ci->pmurev < 17)
3402 switch (bus->ci->chip) {
3403 case BCM43241_CHIP_ID:
3404 case BCM4335_CHIP_ID:
3405 case BCM4339_CHIP_ID:
3406 /* read PMU chipcontrol register 3 */
3407 addr = CORE_CC_REG(bus->ci->c_inf[0].base, chipcontrol_addr);
3408 brcmf_sdiod_regwl(bus->sdiodev, addr, 3, NULL);
3409 addr = CORE_CC_REG(bus->ci->c_inf[0].base, chipcontrol_data);
3410 reg = brcmf_sdiod_regrl(bus->sdiodev, addr, NULL);
3411 return (reg & pmu_cc3_mask) != 0;
3413 addr = CORE_CC_REG(bus->ci->c_inf[0].base, pmucapabilities_ext);
3414 reg = brcmf_sdiod_regrl(bus->sdiodev, addr, &err);
3415 if ((reg & PCAPEXT_SR_SUPPORTED_MASK) == 0)
3418 addr = CORE_CC_REG(bus->ci->c_inf[0].base, retention_ctl);
3419 reg = brcmf_sdiod_regrl(bus->sdiodev, addr, NULL);
3420 return (reg & (PMU_RCTL_MACPHY_DISABLE_MASK |
3421 PMU_RCTL_LOGIC_DISABLE_MASK)) == 0;
3425 static void brcmf_sdio_sr_init(struct brcmf_sdio *bus)
3430 brcmf_dbg(TRACE, "Enter\n");
3432 val = brcmf_sdiod_regrb(bus->sdiodev, SBSDIO_FUNC1_WAKEUPCTRL, &err);
3434 brcmf_err("error reading SBSDIO_FUNC1_WAKEUPCTRL\n");
3438 val |= 1 << SBSDIO_FUNC1_WCTRL_HTWAIT_SHIFT;
3439 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_WAKEUPCTRL, val, &err);
3441 brcmf_err("error writing SBSDIO_FUNC1_WAKEUPCTRL\n");
3445 /* Add CMD14 Support */
3446 brcmf_sdiod_regwb(bus->sdiodev, SDIO_CCCR_BRCM_CARDCAP,
3447 (SDIO_CCCR_BRCM_CARDCAP_CMD14_SUPPORT |
3448 SDIO_CCCR_BRCM_CARDCAP_CMD14_EXT),
3451 brcmf_err("error writing SDIO_CCCR_BRCM_CARDCAP\n");
3455 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3456 SBSDIO_FORCE_HT, &err);
3458 brcmf_err("error writing SBSDIO_FUNC1_CHIPCLKCSR\n");
3463 bus->sr_enabled = true;
3464 brcmf_dbg(INFO, "SR enabled\n");
3467 /* enable KSO bit */
3468 static int brcmf_sdio_kso_init(struct brcmf_sdio *bus)
3473 brcmf_dbg(TRACE, "Enter\n");
3475 /* KSO bit added in SDIO core rev 12 */
3476 if (bus->ci->c_inf[1].rev < 12)
3479 val = brcmf_sdiod_regrb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR, &err);
3481 brcmf_err("error reading SBSDIO_FUNC1_SLEEPCSR\n");
3485 if (!(val & SBSDIO_FUNC1_SLEEPCSR_KSO_MASK)) {
3486 val |= (SBSDIO_FUNC1_SLEEPCSR_KSO_EN <<
3487 SBSDIO_FUNC1_SLEEPCSR_KSO_SHIFT);
3488 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
3491 brcmf_err("error writing SBSDIO_FUNC1_SLEEPCSR\n");
3500 static int brcmf_sdio_bus_preinit(struct device *dev)
3502 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3503 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3504 struct brcmf_sdio *bus = sdiodev->bus;
3510 /* the commands below use the terms tx and rx from
3511 * a device perspective, ie. bus:txglom affects the
3512 * bus transfers from device to host.
3514 idx = brcmf_sdio_chip_getinfidx(bus->ci, BCMA_CORE_SDIO_DEV);
3515 if (bus->ci->c_inf[idx].rev < 12) {
3516 /* for sdio core rev < 12, disable txgloming */
3518 err = brcmf_iovar_data_set(dev, "bus:txglom", &value,
3521 /* otherwise, set txglomalign */
3524 value = sdiodev->pdata->sd_sgentry_align;
3525 /* SDIO ADMA requires at least 32 bit alignment */
3526 value = max_t(u32, value, 4);
3527 err = brcmf_iovar_data_set(dev, "bus:txglomalign", &value,
3534 bus->tx_hdrlen = SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN;
3535 if (sdiodev->sg_support) {
3536 bus->txglom = false;
3538 pad_size = bus->sdiodev->func[2]->cur_blksize << 1;
3539 bus->txglom_sgpad = brcmu_pkt_buf_get_skb(pad_size);
3540 if (!bus->txglom_sgpad)
3541 brcmf_err("allocating txglom padding skb failed, reduced performance\n");
3543 err = brcmf_iovar_data_set(bus->sdiodev->dev, "bus:rxglom",
3544 &value, sizeof(u32));
3546 /* bus:rxglom is allowed to fail */
3550 bus->tx_hdrlen += SDPCM_HWEXT_LEN;
3553 brcmf_bus_add_txhdrlen(bus->sdiodev->dev, bus->tx_hdrlen);
3559 static int brcmf_sdio_bus_init(struct device *dev)
3561 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3562 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3563 struct brcmf_sdio *bus = sdiodev->bus;
3567 brcmf_dbg(TRACE, "Enter\n");
3569 /* try to download image and nvram to the dongle */
3570 if (bus_if->state == BRCMF_BUS_DOWN) {
3571 err = brcmf_sdio_download_firmware(bus);
3576 if (!bus->sdiodev->bus_if->drvr)
3579 /* Start the watchdog timer */
3580 bus->sdcnt.tickcnt = 0;
3581 brcmf_sdio_wd_timer(bus, BRCMF_WD_POLL_MS);
3583 sdio_claim_host(bus->sdiodev->func[1]);
3585 /* Make sure backplane clock is on, needed to generate F2 interrupt */
3586 brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
3587 if (bus->clkstate != CLK_AVAIL)
3590 /* Force clocks on backplane to be sure F2 interrupt propagates */
3591 saveclk = brcmf_sdiod_regrb(bus->sdiodev,
3592 SBSDIO_FUNC1_CHIPCLKCSR, &err);
3594 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3595 (saveclk | SBSDIO_FORCE_HT), &err);
3598 brcmf_err("Failed to force clock for F2: err %d\n", err);
3602 /* Enable function 2 (frame transfers) */
3603 w_sdreg32(bus, SDPCM_PROT_VERSION << SMB_DATA_VERSION_SHIFT,
3604 offsetof(struct sdpcmd_regs, tosbmailboxdata));
3605 err = sdio_enable_func(bus->sdiodev->func[SDIO_FUNC_2]);
3608 brcmf_dbg(INFO, "enable F2: err=%d\n", err);
3610 /* If F2 successfully enabled, set core and enable interrupts */
3612 /* Set up the interrupt mask and enable interrupts */
3613 bus->hostintmask = HOSTINTMASK;
3614 w_sdreg32(bus, bus->hostintmask,
3615 offsetof(struct sdpcmd_regs, hostintmask));
3617 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_WATERMARK, 8, &err);
3619 /* Disable F2 again */
3620 sdio_disable_func(bus->sdiodev->func[SDIO_FUNC_2]);
3624 if (brcmf_sdio_sr_capable(bus)) {
3625 brcmf_sdio_sr_init(bus);
3627 /* Restore previous clock setting */
3628 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3633 ret = brcmf_sdiod_intr_register(bus->sdiodev);
3635 brcmf_err("intr register failed:%d\n", ret);
3638 /* If we didn't come up, turn off backplane clock */
3640 brcmf_sdio_clkctl(bus, CLK_NONE, false);
3643 sdio_release_host(bus->sdiodev->func[1]);
3648 void brcmf_sdio_isr(struct brcmf_sdio *bus)
3650 brcmf_dbg(TRACE, "Enter\n");
3653 brcmf_err("bus is null pointer, exiting\n");
3657 if (bus->sdiodev->bus_if->state == BRCMF_BUS_DOWN) {
3658 brcmf_err("bus is down. we have nothing to do\n");
3661 /* Count the interrupt call */
3662 bus->sdcnt.intrcount++;
3664 atomic_set(&bus->ipend, 1);
3666 if (brcmf_sdio_intr_rstatus(bus)) {
3667 brcmf_err("failed backplane access\n");
3668 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
3671 /* Disable additional interrupts (is this needed now)? */
3673 brcmf_err("isr w/o interrupt configured!\n");
3675 atomic_inc(&bus->dpc_tskcnt);
3676 queue_work(bus->brcmf_wq, &bus->datawork);
3679 static bool brcmf_sdio_bus_watchdog(struct brcmf_sdio *bus)
3682 struct brcmf_bus *bus_if = dev_get_drvdata(bus->sdiodev->dev);
3685 brcmf_dbg(TIMER, "Enter\n");
3687 /* Poll period: check device if appropriate. */
3688 if (!bus->sr_enabled &&
3689 bus->poll && (++bus->polltick >= bus->pollrate)) {
3692 /* Reset poll tick */
3695 /* Check device if no interrupts */
3697 (bus->sdcnt.intrcount == bus->sdcnt.lastintrs)) {
3699 if (atomic_read(&bus->dpc_tskcnt) == 0) {
3702 sdio_claim_host(bus->sdiodev->func[1]);
3703 devpend = brcmf_sdiod_regrb(bus->sdiodev,
3706 sdio_release_host(bus->sdiodev->func[1]);
3708 devpend & (INTR_STATUS_FUNC1 |
3712 /* If there is something, make like the ISR and
3715 bus->sdcnt.pollcnt++;
3716 atomic_set(&bus->ipend, 1);
3718 atomic_inc(&bus->dpc_tskcnt);
3719 queue_work(bus->brcmf_wq, &bus->datawork);
3723 /* Update interrupt tracking */
3724 bus->sdcnt.lastintrs = bus->sdcnt.intrcount;
3727 /* Poll for console output periodically */
3728 if (bus_if && bus_if->state == BRCMF_BUS_DATA &&
3729 bus->console_interval != 0) {
3730 bus->console.count += BRCMF_WD_POLL_MS;
3731 if (bus->console.count >= bus->console_interval) {
3732 bus->console.count -= bus->console_interval;
3733 sdio_claim_host(bus->sdiodev->func[1]);
3734 /* Make sure backplane clock is on */
3735 brcmf_sdio_bus_sleep(bus, false, false);
3736 if (brcmf_sdio_readconsole(bus) < 0)
3738 bus->console_interval = 0;
3739 sdio_release_host(bus->sdiodev->func[1]);
3744 /* On idle timeout clear activity flag and/or turn off clock */
3745 if ((bus->idletime > 0) && (bus->clkstate == CLK_AVAIL)) {
3746 if (++bus->idlecount >= bus->idletime) {
3748 if (bus->activity) {
3749 bus->activity = false;
3750 brcmf_sdio_wd_timer(bus, BRCMF_WD_POLL_MS);
3752 brcmf_dbg(SDIO, "idle\n");
3753 sdio_claim_host(bus->sdiodev->func[1]);
3754 brcmf_sdio_bus_sleep(bus, true, false);
3755 sdio_release_host(bus->sdiodev->func[1]);
3760 return (atomic_read(&bus->ipend) > 0);
3763 static void brcmf_sdio_dataworker(struct work_struct *work)
3765 struct brcmf_sdio *bus = container_of(work, struct brcmf_sdio,
3768 while (atomic_read(&bus->dpc_tskcnt)) {
3769 brcmf_sdio_dpc(bus);
3770 atomic_dec(&bus->dpc_tskcnt);
3775 brcmf_sdio_probe_attach(struct brcmf_sdio *bus)
3783 bus->alp_only = true;
3785 sdio_claim_host(bus->sdiodev->func[1]);
3787 pr_debug("F1 signature read @0x18000000=0x%4x\n",
3788 brcmf_sdiod_regrl(bus->sdiodev, SI_ENUM_BASE, NULL));
3791 * Force PLL off until brcmf_sdio_chip_attach()
3792 * programs PLL control regs
3795 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3796 BRCMF_INIT_CLKCTL1, &err);
3798 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
3799 SBSDIO_FUNC1_CHIPCLKCSR, &err);
3801 if (err || ((clkctl & ~SBSDIO_AVBITS) != BRCMF_INIT_CLKCTL1)) {
3802 brcmf_err("ChipClkCSR access: err %d wrote 0x%02x read 0x%02x\n",
3803 err, BRCMF_INIT_CLKCTL1, clkctl);
3807 if (brcmf_sdio_chip_attach(bus->sdiodev, &bus->ci)) {
3808 brcmf_err("brcmf_sdio_chip_attach failed!\n");
3812 if (brcmf_sdio_kso_init(bus)) {
3813 brcmf_err("error enabling KSO\n");
3817 if ((bus->sdiodev->pdata) && (bus->sdiodev->pdata->drive_strength))
3818 drivestrength = bus->sdiodev->pdata->drive_strength;
3820 drivestrength = DEFAULT_SDIO_DRIVE_STRENGTH;
3821 brcmf_sdio_chip_drivestrengthinit(bus->sdiodev, bus->ci, drivestrength);
3823 /* Get info on the SOCRAM cores... */
3824 bus->ramsize = bus->ci->ramsize;
3825 if (!(bus->ramsize)) {
3826 brcmf_err("failed to find SOCRAM memory!\n");
3830 /* Set card control so an SDIO card reset does a WLAN backplane reset */
3831 reg_val = brcmf_sdiod_regrb(bus->sdiodev,
3832 SDIO_CCCR_BRCM_CARDCTRL, &err);
3836 reg_val |= SDIO_CCCR_BRCM_CARDCTRL_WLANRESET;
3838 brcmf_sdiod_regwb(bus->sdiodev,
3839 SDIO_CCCR_BRCM_CARDCTRL, reg_val, &err);
3843 /* set PMUControl so a backplane reset does PMU state reload */
3844 reg_addr = CORE_CC_REG(bus->ci->c_inf[0].base,
3846 reg_val = brcmf_sdiod_regrl(bus->sdiodev,
3852 reg_val |= (BCMA_CC_PMU_CTL_RES_RELOAD << BCMA_CC_PMU_CTL_RES_SHIFT);
3854 brcmf_sdiod_regwl(bus->sdiodev,
3862 sdio_release_host(bus->sdiodev->func[1]);
3864 brcmu_pktq_init(&bus->txq, (PRIOMASK + 1), TXQLEN);
3866 /* allocate header buffer */
3867 bus->hdrbuf = kzalloc(MAX_HDR_READ + bus->head_align, GFP_KERNEL);
3870 /* Locate an appropriately-aligned portion of hdrbuf */
3871 bus->rxhdr = (u8 *) roundup((unsigned long)&bus->hdrbuf[0],
3874 /* Set the poll and/or interrupt flags */
3883 sdio_release_host(bus->sdiodev->func[1]);
3888 brcmf_sdio_watchdog_thread(void *data)
3890 struct brcmf_sdio *bus = (struct brcmf_sdio *)data;
3892 allow_signal(SIGTERM);
3893 /* Run until signal received */
3895 if (kthread_should_stop())
3897 if (!wait_for_completion_interruptible(&bus->watchdog_wait)) {
3898 brcmf_sdio_bus_watchdog(bus);
3899 /* Count the tick for reference */
3900 bus->sdcnt.tickcnt++;
3908 brcmf_sdio_watchdog(unsigned long data)
3910 struct brcmf_sdio *bus = (struct brcmf_sdio *)data;
3912 if (bus->watchdog_tsk) {
3913 complete(&bus->watchdog_wait);
3914 /* Reschedule the watchdog */
3915 if (bus->wd_timer_valid)
3916 mod_timer(&bus->timer,
3917 jiffies + BRCMF_WD_POLL_MS * HZ / 1000);
3921 static struct brcmf_bus_ops brcmf_sdio_bus_ops = {
3922 .stop = brcmf_sdio_bus_stop,
3923 .preinit = brcmf_sdio_bus_preinit,
3924 .init = brcmf_sdio_bus_init,
3925 .txdata = brcmf_sdio_bus_txdata,
3926 .txctl = brcmf_sdio_bus_txctl,
3927 .rxctl = brcmf_sdio_bus_rxctl,
3928 .gettxq = brcmf_sdio_bus_gettxq,
3931 struct brcmf_sdio *brcmf_sdio_probe(struct brcmf_sdio_dev *sdiodev)
3934 struct brcmf_sdio *bus;
3936 brcmf_dbg(TRACE, "Enter\n");
3938 /* Allocate private bus interface state */
3939 bus = kzalloc(sizeof(struct brcmf_sdio), GFP_ATOMIC);
3943 bus->sdiodev = sdiodev;
3945 skb_queue_head_init(&bus->glom);
3946 bus->txbound = BRCMF_TXBOUND;
3947 bus->rxbound = BRCMF_RXBOUND;
3948 bus->txminmax = BRCMF_TXMINMAX;
3949 bus->tx_seq = SDPCM_SEQ_WRAP - 1;
3951 /* platform specific configuration:
3952 * alignments must be at least 4 bytes for ADMA
3954 bus->head_align = ALIGNMENT;
3955 bus->sgentry_align = ALIGNMENT;
3956 if (sdiodev->pdata) {
3957 if (sdiodev->pdata->sd_head_align > ALIGNMENT)
3958 bus->head_align = sdiodev->pdata->sd_head_align;
3959 if (sdiodev->pdata->sd_sgentry_align > ALIGNMENT)
3960 bus->sgentry_align = sdiodev->pdata->sd_sgentry_align;
3963 INIT_WORK(&bus->datawork, brcmf_sdio_dataworker);
3964 bus->brcmf_wq = create_singlethread_workqueue("brcmf_wq");
3965 if (bus->brcmf_wq == NULL) {
3966 brcmf_err("insufficient memory to create txworkqueue\n");
3970 /* attempt to attach to the dongle */
3971 if (!(brcmf_sdio_probe_attach(bus))) {
3972 brcmf_err("brcmf_sdio_probe_attach failed\n");
3976 spin_lock_init(&bus->rxctl_lock);
3977 spin_lock_init(&bus->txqlock);
3978 init_waitqueue_head(&bus->ctrl_wait);
3979 init_waitqueue_head(&bus->dcmd_resp_wait);
3981 /* Set up the watchdog timer */
3982 init_timer(&bus->timer);
3983 bus->timer.data = (unsigned long)bus;
3984 bus->timer.function = brcmf_sdio_watchdog;
3986 /* Initialize watchdog thread */
3987 init_completion(&bus->watchdog_wait);
3988 bus->watchdog_tsk = kthread_run(brcmf_sdio_watchdog_thread,
3989 bus, "brcmf_watchdog");
3990 if (IS_ERR(bus->watchdog_tsk)) {
3991 pr_warn("brcmf_watchdog thread failed to start\n");
3992 bus->watchdog_tsk = NULL;
3994 /* Initialize DPC thread */
3995 atomic_set(&bus->dpc_tskcnt, 0);
3997 /* Assign bus interface call back */
3998 bus->sdiodev->bus_if->dev = bus->sdiodev->dev;
3999 bus->sdiodev->bus_if->ops = &brcmf_sdio_bus_ops;
4000 bus->sdiodev->bus_if->chip = bus->ci->chip;
4001 bus->sdiodev->bus_if->chiprev = bus->ci->chiprev;
4003 /* default sdio bus header length for tx packet */
4004 bus->tx_hdrlen = SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN;
4006 /* Attach to the common layer, reserve hdr space */
4007 ret = brcmf_attach(bus->sdiodev->dev);
4009 brcmf_err("brcmf_attach failed\n");
4013 /* Allocate buffers */
4014 if (bus->sdiodev->bus_if->maxctl) {
4016 roundup((bus->sdiodev->bus_if->maxctl + SDPCM_HDRLEN),
4017 ALIGNMENT) + bus->head_align;
4018 bus->rxbuf = kmalloc(bus->rxblen, GFP_ATOMIC);
4019 if (!(bus->rxbuf)) {
4020 brcmf_err("rxbuf allocation failed\n");
4025 sdio_claim_host(bus->sdiodev->func[1]);
4027 /* Disable F2 to clear any intermediate frame state on the dongle */
4028 sdio_disable_func(bus->sdiodev->func[SDIO_FUNC_2]);
4030 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
4031 bus->rxflow = false;
4033 /* Done with backplane-dependent accesses, can drop clock... */
4034 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, 0, NULL);
4036 sdio_release_host(bus->sdiodev->func[1]);
4038 /* ...and initialize clock/power states */
4039 bus->clkstate = CLK_SDONLY;
4040 bus->idletime = BRCMF_IDLE_INTERVAL;
4041 bus->idleclock = BRCMF_IDLE_ACTIVE;
4043 /* Query the F2 block size, set roundup accordingly */
4044 bus->blocksize = bus->sdiodev->func[2]->cur_blksize;
4045 bus->roundup = min(max_roundup, bus->blocksize);
4048 bus->sleeping = false;
4049 bus->sr_enabled = false;
4051 brcmf_sdio_debugfs_create(bus);
4052 brcmf_dbg(INFO, "completed!!\n");
4054 /* if firmware path present try to download and bring up bus */
4055 ret = brcmf_bus_start(bus->sdiodev->dev);
4057 brcmf_err("dongle is not responding\n");
4064 brcmf_sdio_remove(bus);
4068 /* Detach and free everything */
4069 void brcmf_sdio_remove(struct brcmf_sdio *bus)
4071 brcmf_dbg(TRACE, "Enter\n");
4074 /* De-register interrupt handler */
4075 brcmf_sdiod_intr_unregister(bus->sdiodev);
4077 cancel_work_sync(&bus->datawork);
4079 destroy_workqueue(bus->brcmf_wq);
4081 if (bus->sdiodev->bus_if->drvr) {
4082 brcmf_detach(bus->sdiodev->dev);
4086 sdio_claim_host(bus->sdiodev->func[1]);
4087 brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
4088 brcmf_sdio_clkctl(bus, CLK_NONE, false);
4089 sdio_release_host(bus->sdiodev->func[1]);
4090 brcmf_sdio_chip_detach(&bus->ci);
4093 brcmu_pkt_buf_free_skb(bus->txglom_sgpad);
4099 brcmf_dbg(TRACE, "Disconnected\n");
4102 void brcmf_sdio_wd_timer(struct brcmf_sdio *bus, uint wdtick)
4104 /* Totally stop the timer */
4105 if (!wdtick && bus->wd_timer_valid) {
4106 del_timer_sync(&bus->timer);
4107 bus->wd_timer_valid = false;
4108 bus->save_ms = wdtick;
4112 /* don't start the wd until fw is loaded */
4113 if (bus->sdiodev->bus_if->state != BRCMF_BUS_DATA)
4117 if (bus->save_ms != BRCMF_WD_POLL_MS) {
4118 if (bus->wd_timer_valid)
4119 /* Stop timer and restart at new value */
4120 del_timer_sync(&bus->timer);
4122 /* Create timer again when watchdog period is
4123 dynamically changed or in the first instance
4125 bus->timer.expires =
4126 jiffies + BRCMF_WD_POLL_MS * HZ / 1000;
4127 add_timer(&bus->timer);
4130 /* Re arm the timer, at last watchdog period */
4131 mod_timer(&bus->timer,
4132 jiffies + BRCMF_WD_POLL_MS * HZ / 1000);
4135 bus->wd_timer_valid = true;
4136 bus->save_ms = wdtick;