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"
45 #define DCMD_RESP_TIMEOUT 2000 /* In milli second */
49 #define BRCMF_TRAP_INFO_SIZE 80
51 #define CBUF_LEN (128)
53 /* Device console log buffer state */
54 #define CONSOLE_BUFFER_MAX 2024
57 __le32 buf; /* Can't be pointer on (64-bit) hosts */
60 char *_buf_compat; /* Redundant pointer for backward compat. */
65 * When there is no UART (e.g. Quickturn),
66 * the host should write a complete
67 * input line directly into cbuf and then write
68 * the length into vcons_in.
69 * This may also be used when there is a real UART
70 * (at risk of conflicting with
71 * the real UART). vcons_out is currently unused.
76 /* Output (logging) buffer
77 * Console output is written to a ring buffer log_buf at index log_idx.
78 * The host may read the output when it sees log_idx advance.
79 * Output will be lost if the output wraps around faster than the host
82 struct rte_log_le log_le;
84 /* Console input line buffer
85 * Characters are read one at a time into cbuf
86 * until <CR> is received, then
87 * the buffer is processed as a command line.
88 * Also used for virtual UART.
95 #include <chipcommon.h>
99 #include "tracepoint.h"
101 #define TXQLEN 2048 /* bulk tx queue length */
102 #define TXHI (TXQLEN - 256) /* turn on flow control above TXHI */
103 #define TXLOW (TXHI - 256) /* turn off flow control below TXLOW */
106 #define TXRETRIES 2 /* # of retries for tx frames */
108 #define BRCMF_RXBOUND 50 /* Default for max rx frames in
111 #define BRCMF_TXBOUND 20 /* Default for max tx frames in
114 #define BRCMF_DEFAULT_TXGLOM_SIZE 32 /* max tx frames in glom chain */
116 #define BRCMF_TXMINMAX 1 /* Max tx frames if rx still pending */
118 #define MEMBLOCK 2048 /* Block size used for downloading
120 #define MAX_DATA_BUF (32 * 1024) /* Must be large enough to hold
121 biggest possible glom */
123 #define BRCMF_FIRSTREAD (1 << 6)
126 /* SBSDIO_DEVICE_CTL */
128 /* 1: device will assert busy signal when receiving CMD53 */
129 #define SBSDIO_DEVCTL_SETBUSY 0x01
130 /* 1: assertion of sdio interrupt is synchronous to the sdio clock */
131 #define SBSDIO_DEVCTL_SPI_INTR_SYNC 0x02
132 /* 1: mask all interrupts to host except the chipActive (rev 8) */
133 #define SBSDIO_DEVCTL_CA_INT_ONLY 0x04
134 /* 1: isolate internal sdio signals, put external pads in tri-state; requires
135 * sdio bus power cycle to clear (rev 9) */
136 #define SBSDIO_DEVCTL_PADS_ISO 0x08
137 /* Force SD->SB reset mapping (rev 11) */
138 #define SBSDIO_DEVCTL_SB_RST_CTL 0x30
139 /* Determined by CoreControl bit */
140 #define SBSDIO_DEVCTL_RST_CORECTL 0x00
141 /* Force backplane reset */
142 #define SBSDIO_DEVCTL_RST_BPRESET 0x10
143 /* Force no backplane reset */
144 #define SBSDIO_DEVCTL_RST_NOBPRESET 0x20
146 /* direct(mapped) cis space */
148 /* MAPPED common CIS address */
149 #define SBSDIO_CIS_BASE_COMMON 0x1000
150 /* maximum bytes in one CIS */
151 #define SBSDIO_CIS_SIZE_LIMIT 0x200
152 /* cis offset addr is < 17 bits */
153 #define SBSDIO_CIS_OFT_ADDR_MASK 0x1FFFF
155 /* manfid tuple length, include tuple, link bytes */
156 #define SBSDIO_CIS_MANFID_TUPLE_LEN 6
159 #define I_SMB_SW0 (1 << 0) /* To SB Mail S/W interrupt 0 */
160 #define I_SMB_SW1 (1 << 1) /* To SB Mail S/W interrupt 1 */
161 #define I_SMB_SW2 (1 << 2) /* To SB Mail S/W interrupt 2 */
162 #define I_SMB_SW3 (1 << 3) /* To SB Mail S/W interrupt 3 */
163 #define I_SMB_SW_MASK 0x0000000f /* To SB Mail S/W interrupts mask */
164 #define I_SMB_SW_SHIFT 0 /* To SB Mail S/W interrupts shift */
165 #define I_HMB_SW0 (1 << 4) /* To Host Mail S/W interrupt 0 */
166 #define I_HMB_SW1 (1 << 5) /* To Host Mail S/W interrupt 1 */
167 #define I_HMB_SW2 (1 << 6) /* To Host Mail S/W interrupt 2 */
168 #define I_HMB_SW3 (1 << 7) /* To Host Mail S/W interrupt 3 */
169 #define I_HMB_SW_MASK 0x000000f0 /* To Host Mail S/W interrupts mask */
170 #define I_HMB_SW_SHIFT 4 /* To Host Mail S/W interrupts shift */
171 #define I_WR_OOSYNC (1 << 8) /* Write Frame Out Of Sync */
172 #define I_RD_OOSYNC (1 << 9) /* Read Frame Out Of Sync */
173 #define I_PC (1 << 10) /* descriptor error */
174 #define I_PD (1 << 11) /* data error */
175 #define I_DE (1 << 12) /* Descriptor protocol Error */
176 #define I_RU (1 << 13) /* Receive descriptor Underflow */
177 #define I_RO (1 << 14) /* Receive fifo Overflow */
178 #define I_XU (1 << 15) /* Transmit fifo Underflow */
179 #define I_RI (1 << 16) /* Receive Interrupt */
180 #define I_BUSPWR (1 << 17) /* SDIO Bus Power Change (rev 9) */
181 #define I_XMTDATA_AVAIL (1 << 23) /* bits in fifo */
182 #define I_XI (1 << 24) /* Transmit Interrupt */
183 #define I_RF_TERM (1 << 25) /* Read Frame Terminate */
184 #define I_WF_TERM (1 << 26) /* Write Frame Terminate */
185 #define I_PCMCIA_XU (1 << 27) /* PCMCIA Transmit FIFO Underflow */
186 #define I_SBINT (1 << 28) /* sbintstatus Interrupt */
187 #define I_CHIPACTIVE (1 << 29) /* chip from doze to active state */
188 #define I_SRESET (1 << 30) /* CCCR RES interrupt */
189 #define I_IOE2 (1U << 31) /* CCCR IOE2 Bit Changed */
190 #define I_ERRORS (I_PC | I_PD | I_DE | I_RU | I_RO | I_XU)
191 #define I_DMA (I_RI | I_XI | I_ERRORS)
194 #define CC_CISRDY (1 << 0) /* CIS Ready */
195 #define CC_BPRESEN (1 << 1) /* CCCR RES signal */
196 #define CC_F2RDY (1 << 2) /* set CCCR IOR2 bit */
197 #define CC_CLRPADSISO (1 << 3) /* clear SDIO pads isolation */
198 #define CC_XMTDATAAVAIL_MODE (1 << 4)
199 #define CC_XMTDATAAVAIL_CTRL (1 << 5)
202 #define SFC_RF_TERM (1 << 0) /* Read Frame Terminate */
203 #define SFC_WF_TERM (1 << 1) /* Write Frame Terminate */
204 #define SFC_CRC4WOOS (1 << 2) /* CRC error for write out of sync */
205 #define SFC_ABORTALL (1 << 3) /* Abort all in-progress frames */
208 * Software allocation of To SB Mailbox resources
211 /* tosbmailbox bits corresponding to intstatus bits */
212 #define SMB_NAK (1 << 0) /* Frame NAK */
213 #define SMB_INT_ACK (1 << 1) /* Host Interrupt ACK */
214 #define SMB_USE_OOB (1 << 2) /* Use OOB Wakeup */
215 #define SMB_DEV_INT (1 << 3) /* Miscellaneous Interrupt */
217 /* tosbmailboxdata */
218 #define SMB_DATA_VERSION_SHIFT 16 /* host protocol version */
221 * Software allocation of To Host Mailbox resources
225 #define I_HMB_FC_STATE I_HMB_SW0 /* Flow Control State */
226 #define I_HMB_FC_CHANGE I_HMB_SW1 /* Flow Control State Changed */
227 #define I_HMB_FRAME_IND I_HMB_SW2 /* Frame Indication */
228 #define I_HMB_HOST_INT I_HMB_SW3 /* Miscellaneous Interrupt */
230 /* tohostmailboxdata */
231 #define HMB_DATA_NAKHANDLED 1 /* retransmit NAK'd frame */
232 #define HMB_DATA_DEVREADY 2 /* talk to host after enable */
233 #define HMB_DATA_FC 4 /* per prio flowcontrol update flag */
234 #define HMB_DATA_FWREADY 8 /* fw ready for protocol activity */
236 #define HMB_DATA_FCDATA_MASK 0xff000000
237 #define HMB_DATA_FCDATA_SHIFT 24
239 #define HMB_DATA_VERSION_MASK 0x00ff0000
240 #define HMB_DATA_VERSION_SHIFT 16
243 * Software-defined protocol header
246 /* Current protocol version */
247 #define SDPCM_PROT_VERSION 4
250 * Shared structure between dongle and the host.
251 * The structure contains pointers to trap or assert information.
253 #define SDPCM_SHARED_VERSION 0x0003
254 #define SDPCM_SHARED_VERSION_MASK 0x00FF
255 #define SDPCM_SHARED_ASSERT_BUILT 0x0100
256 #define SDPCM_SHARED_ASSERT 0x0200
257 #define SDPCM_SHARED_TRAP 0x0400
259 /* Space for header read, limit for data packets */
260 #define MAX_HDR_READ (1 << 6)
261 #define MAX_RX_DATASZ 2048
263 /* Bump up limit on waiting for HT to account for first startup;
264 * if the image is doing a CRC calculation before programming the PMU
265 * for HT availability, it could take a couple hundred ms more, so
266 * max out at a 1 second (1000000us).
268 #undef PMU_MAX_TRANSITION_DLY
269 #define PMU_MAX_TRANSITION_DLY 1000000
271 /* Value for ChipClockCSR during initial setup */
272 #define BRCMF_INIT_CLKCTL1 (SBSDIO_FORCE_HW_CLKREQ_OFF | \
273 SBSDIO_ALP_AVAIL_REQ)
275 /* Flags for SDH calls */
276 #define F2SYNC (SDIO_REQ_4BYTE | SDIO_REQ_FIXED)
278 #define BRCMF_IDLE_IMMEDIATE (-1) /* Enter idle immediately */
279 #define BRCMF_IDLE_ACTIVE 0 /* Do not request any SD clock change
282 #define BRCMF_IDLE_INTERVAL 1
284 #define KSO_WAIT_US 50
285 #define MAX_KSO_ATTEMPTS (PMU_MAX_TRANSITION_DLY/KSO_WAIT_US)
288 * Conversion of 802.1D priority to precedence level
290 static uint prio2prec(u32 prio)
292 return (prio == PRIO_8021D_NONE || prio == PRIO_8021D_BE) ?
297 /* Device console log buffer state */
298 struct brcmf_console {
299 uint count; /* Poll interval msec counter */
300 uint log_addr; /* Log struct address (fixed) */
301 struct rte_log_le log_le; /* Log struct (host copy) */
302 uint bufsize; /* Size of log buffer */
303 u8 *buf; /* Log buffer (host copy) */
304 uint last; /* Last buffer read index */
307 struct brcmf_trap_info {
321 __le32 r9; /* sb/v6 */
322 __le32 r10; /* sl/v7 */
323 __le32 r11; /* fp/v8 */
331 struct sdpcm_shared {
335 u32 assert_file_addr;
337 u32 console_addr; /* Address of struct rte_console */
343 struct sdpcm_shared_le {
346 __le32 assert_exp_addr;
347 __le32 assert_file_addr;
349 __le32 console_addr; /* Address of struct rte_console */
350 __le32 msgtrace_addr;
355 /* dongle SDIO bus specific header info */
356 struct brcmf_sdio_hdrinfo {
367 /* misc chip info needed by some of the routines */
368 /* Private data for SDIO bus interaction */
370 struct brcmf_sdio_dev *sdiodev; /* sdio device handler */
371 struct chip_info *ci; /* Chip info struct */
372 char *vars; /* Variables (from CIS and/or other) */
373 uint varsz; /* Size of variables buffer */
375 u32 ramsize; /* Size of RAM in SOCRAM (bytes) */
377 u32 hostintmask; /* Copy of Host Interrupt Mask */
378 atomic_t intstatus; /* Intstatus bits (events) pending */
379 atomic_t fcstate; /* State of dongle flow-control */
381 uint blocksize; /* Block size of SDIO transfers */
382 uint roundup; /* Max roundup limit */
384 struct pktq txq; /* Queue length used for flow-control */
385 u8 flowcontrol; /* per prio flow control bitmask */
386 u8 tx_seq; /* Transmit sequence number (next) */
387 u8 tx_max; /* Maximum transmit sequence allowed */
389 u8 *hdrbuf; /* buffer for handling rx frame */
390 u8 *rxhdr; /* Header of current rx frame (in hdrbuf) */
391 u8 rx_seq; /* Receive sequence number (expected) */
392 struct brcmf_sdio_hdrinfo cur_read;
393 /* info of current read frame */
394 bool rxskip; /* Skip receive (awaiting NAK ACK) */
395 bool rxpending; /* Data frame pending in dongle */
397 uint rxbound; /* Rx frames to read before resched */
398 uint txbound; /* Tx frames to send before resched */
401 struct sk_buff *glomd; /* Packet containing glomming descriptor */
402 struct sk_buff_head glom; /* Packet list for glommed superframe */
403 uint glomerr; /* Glom packet read errors */
405 u8 *rxbuf; /* Buffer for receiving control packets */
406 uint rxblen; /* Allocated length of rxbuf */
407 u8 *rxctl; /* Aligned pointer into rxbuf */
408 u8 *rxctl_orig; /* pointer for freeing rxctl */
409 uint rxlen; /* Length of valid data in buffer */
410 spinlock_t rxctl_lock; /* protection lock for ctrl frame resources */
412 u8 sdpcm_ver; /* Bus protocol reported by dongle */
414 bool intr; /* Use interrupts */
415 bool poll; /* Use polling */
416 atomic_t ipend; /* Device interrupt is pending */
417 uint spurious; /* Count of spurious interrupts */
418 uint pollrate; /* Ticks between device polls */
419 uint polltick; /* Tick counter */
422 uint console_interval;
423 struct brcmf_console console; /* Console output polling support */
424 uint console_addr; /* Console address from shared struct */
427 uint clkstate; /* State of sd and backplane clock(s) */
428 bool activity; /* Activity flag for clock down */
429 s32 idletime; /* Control for activity timeout */
430 s32 idlecount; /* Activity timeout counter */
431 s32 idleclock; /* How to set bus driver when idle */
432 bool rxflow_mode; /* Rx flow control mode */
433 bool rxflow; /* Is rx flow control on */
434 bool alp_only; /* Don't use HT clock (ALP only) */
438 bool ctrl_frame_stat;
441 wait_queue_head_t ctrl_wait;
442 wait_queue_head_t dcmd_resp_wait;
444 struct timer_list timer;
445 struct completion watchdog_wait;
446 struct task_struct *watchdog_tsk;
450 struct workqueue_struct *brcmf_wq;
451 struct work_struct datawork;
454 bool txoff; /* Transmit flow-controlled */
455 struct brcmf_sdio_count sdcnt;
456 bool sr_enabled; /* SaveRestore enabled */
457 bool sleeping; /* SDIO bus sleeping */
459 u8 tx_hdrlen; /* sdio bus header length for tx packet */
460 bool txglom; /* host tx glomming enable flag */
461 struct sk_buff *txglom_sgpad; /* scatter-gather padding buffer */
462 u16 head_align; /* buffer pointer alignment */
463 u16 sgentry_align; /* scatter-gather buffer alignment */
469 #define CLK_PENDING 2
473 static int qcount[NUMPRIO];
476 #define DEFAULT_SDIO_DRIVE_STRENGTH 6 /* in milliamps */
478 #define RETRYCHAN(chan) ((chan) == SDPCM_EVENT_CHANNEL)
480 /* Retry count for register access failures */
481 static const uint retry_limit = 2;
483 /* Limit on rounding up frames */
484 static const uint max_roundup = 512;
488 static int brcmf_sdio_txglomsz = BRCMF_DEFAULT_TXGLOM_SIZE;
489 module_param_named(txglomsz, brcmf_sdio_txglomsz, int, 0);
490 MODULE_PARM_DESC(txglomsz, "maximum tx packet chain size [SDIO]");
492 enum brcmf_sdio_frmtype {
493 BRCMF_SDIO_FT_NORMAL,
498 #define BCM43143_FIRMWARE_NAME "brcm/brcmfmac43143-sdio.bin"
499 #define BCM43143_NVRAM_NAME "brcm/brcmfmac43143-sdio.txt"
500 #define BCM43241B0_FIRMWARE_NAME "brcm/brcmfmac43241b0-sdio.bin"
501 #define BCM43241B0_NVRAM_NAME "brcm/brcmfmac43241b0-sdio.txt"
502 #define BCM43241B4_FIRMWARE_NAME "brcm/brcmfmac43241b4-sdio.bin"
503 #define BCM43241B4_NVRAM_NAME "brcm/brcmfmac43241b4-sdio.txt"
504 #define BCM4329_FIRMWARE_NAME "brcm/brcmfmac4329-sdio.bin"
505 #define BCM4329_NVRAM_NAME "brcm/brcmfmac4329-sdio.txt"
506 #define BCM4330_FIRMWARE_NAME "brcm/brcmfmac4330-sdio.bin"
507 #define BCM4330_NVRAM_NAME "brcm/brcmfmac4330-sdio.txt"
508 #define BCM4334_FIRMWARE_NAME "brcm/brcmfmac4334-sdio.bin"
509 #define BCM4334_NVRAM_NAME "brcm/brcmfmac4334-sdio.txt"
510 #define BCM4335_FIRMWARE_NAME "brcm/brcmfmac4335-sdio.bin"
511 #define BCM4335_NVRAM_NAME "brcm/brcmfmac4335-sdio.txt"
512 #define BCM4339_FIRMWARE_NAME "brcm/brcmfmac4339-sdio.bin"
513 #define BCM4339_NVRAM_NAME "brcm/brcmfmac4339-sdio.txt"
515 MODULE_FIRMWARE(BCM43143_FIRMWARE_NAME);
516 MODULE_FIRMWARE(BCM43143_NVRAM_NAME);
517 MODULE_FIRMWARE(BCM43241B0_FIRMWARE_NAME);
518 MODULE_FIRMWARE(BCM43241B0_NVRAM_NAME);
519 MODULE_FIRMWARE(BCM43241B4_FIRMWARE_NAME);
520 MODULE_FIRMWARE(BCM43241B4_NVRAM_NAME);
521 MODULE_FIRMWARE(BCM4329_FIRMWARE_NAME);
522 MODULE_FIRMWARE(BCM4329_NVRAM_NAME);
523 MODULE_FIRMWARE(BCM4330_FIRMWARE_NAME);
524 MODULE_FIRMWARE(BCM4330_NVRAM_NAME);
525 MODULE_FIRMWARE(BCM4334_FIRMWARE_NAME);
526 MODULE_FIRMWARE(BCM4334_NVRAM_NAME);
527 MODULE_FIRMWARE(BCM4335_FIRMWARE_NAME);
528 MODULE_FIRMWARE(BCM4335_NVRAM_NAME);
529 MODULE_FIRMWARE(BCM4339_FIRMWARE_NAME);
530 MODULE_FIRMWARE(BCM4339_NVRAM_NAME);
532 struct brcmf_firmware_names {
539 enum brcmf_firmware_type {
544 #define BRCMF_FIRMWARE_NVRAM(name) \
545 name ## _FIRMWARE_NAME, name ## _NVRAM_NAME
547 static const struct brcmf_firmware_names brcmf_fwname_data[] = {
548 { BCM43143_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM43143) },
549 { BCM43241_CHIP_ID, 0x0000001F, BRCMF_FIRMWARE_NVRAM(BCM43241B0) },
550 { BCM43241_CHIP_ID, 0xFFFFFFE0, BRCMF_FIRMWARE_NVRAM(BCM43241B4) },
551 { BCM4329_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM4329) },
552 { BCM4330_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM4330) },
553 { BCM4334_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM4334) },
554 { BCM4335_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM4335) },
555 { BCM4339_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM4339) }
559 static const struct firmware *brcmf_sdbrcm_get_fw(struct brcmf_sdio *bus,
560 enum brcmf_firmware_type type)
562 const struct firmware *fw;
566 for (i = 0; i < ARRAY_SIZE(brcmf_fwname_data); i++) {
567 if (brcmf_fwname_data[i].chipid == bus->ci->chip &&
568 brcmf_fwname_data[i].revmsk & BIT(bus->ci->chiprev)) {
570 case BRCMF_FIRMWARE_BIN:
571 name = brcmf_fwname_data[i].bin;
573 case BRCMF_FIRMWARE_NVRAM:
574 name = brcmf_fwname_data[i].nv;
577 brcmf_err("invalid firmware type (%d)\n", type);
583 brcmf_err("Unknown chipid %d [%d]\n",
584 bus->ci->chip, bus->ci->chiprev);
588 err = request_firmware(&fw, name, &bus->sdiodev->func[2]->dev);
589 if ((err) || (!fw)) {
590 brcmf_err("fail to request firmware %s (%d)\n", name, err);
597 static void pkt_align(struct sk_buff *p, int len, int align)
600 datalign = (unsigned long)(p->data);
601 datalign = roundup(datalign, (align)) - datalign;
603 skb_pull(p, datalign);
607 /* To check if there's window offered */
608 static bool data_ok(struct brcmf_sdio *bus)
610 return (u8)(bus->tx_max - bus->tx_seq) != 0 &&
611 ((u8)(bus->tx_max - bus->tx_seq) & 0x80) == 0;
615 * Reads a register in the SDIO hardware block. This block occupies a series of
616 * adresses on the 32 bit backplane bus.
619 r_sdreg32(struct brcmf_sdio *bus, u32 *regvar, u32 offset)
621 u8 idx = brcmf_sdio_chip_getinfidx(bus->ci, BCMA_CORE_SDIO_DEV);
624 *regvar = brcmf_sdiod_regrl(bus->sdiodev,
625 bus->ci->c_inf[idx].base + offset, &ret);
631 w_sdreg32(struct brcmf_sdio *bus, u32 regval, u32 reg_offset)
633 u8 idx = brcmf_sdio_chip_getinfidx(bus->ci, BCMA_CORE_SDIO_DEV);
636 brcmf_sdiod_regwl(bus->sdiodev,
637 bus->ci->c_inf[idx].base + reg_offset,
644 brcmf_sdbrcm_kso_control(struct brcmf_sdio *bus, bool on)
646 u8 wr_val = 0, rd_val, cmp_val, bmask;
650 brcmf_dbg(TRACE, "Enter\n");
652 wr_val = (on << SBSDIO_FUNC1_SLEEPCSR_KSO_SHIFT);
653 /* 1st KSO write goes to AOS wake up core if device is asleep */
654 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
657 brcmf_err("SDIO_AOS KSO write error: %d\n", err);
662 /* device WAKEUP through KSO:
663 * write bit 0 & read back until
664 * both bits 0 (kso bit) & 1 (dev on status) are set
666 cmp_val = SBSDIO_FUNC1_SLEEPCSR_KSO_MASK |
667 SBSDIO_FUNC1_SLEEPCSR_DEVON_MASK;
669 usleep_range(2000, 3000);
671 /* Put device to sleep, turn off KSO */
673 /* only check for bit0, bit1(dev on status) may not
674 * get cleared right away
676 bmask = SBSDIO_FUNC1_SLEEPCSR_KSO_MASK;
680 /* reliable KSO bit set/clr:
681 * the sdiod sleep write access is synced to PMU 32khz clk
682 * just one write attempt may fail,
683 * read it back until it matches written value
685 rd_val = brcmf_sdiod_regrb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
687 if (((rd_val & bmask) == cmp_val) && !err)
689 brcmf_dbg(SDIO, "KSO wr/rd retry:%d (max: %d) ERR:%x\n",
690 try_cnt, MAX_KSO_ATTEMPTS, err);
692 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
694 } while (try_cnt++ < MAX_KSO_ATTEMPTS);
699 #define PKT_AVAILABLE() (intstatus & I_HMB_FRAME_IND)
701 #define HOSTINTMASK (I_HMB_SW_MASK | I_CHIPACTIVE)
703 /* Turn backplane clock on or off */
704 static int brcmf_sdbrcm_htclk(struct brcmf_sdio *bus, bool on, bool pendok)
707 u8 clkctl, clkreq, devctl;
708 unsigned long timeout;
710 brcmf_dbg(SDIO, "Enter\n");
714 if (bus->sr_enabled) {
715 bus->clkstate = (on ? CLK_AVAIL : CLK_SDONLY);
720 /* Request HT Avail */
722 bus->alp_only ? SBSDIO_ALP_AVAIL_REQ : SBSDIO_HT_AVAIL_REQ;
724 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
727 brcmf_err("HT Avail request error: %d\n", err);
731 /* Check current status */
732 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
733 SBSDIO_FUNC1_CHIPCLKCSR, &err);
735 brcmf_err("HT Avail read error: %d\n", err);
739 /* Go to pending and await interrupt if appropriate */
740 if (!SBSDIO_CLKAV(clkctl, bus->alp_only) && pendok) {
741 /* Allow only clock-available interrupt */
742 devctl = brcmf_sdiod_regrb(bus->sdiodev,
743 SBSDIO_DEVICE_CTL, &err);
745 brcmf_err("Devctl error setting CA: %d\n",
750 devctl |= SBSDIO_DEVCTL_CA_INT_ONLY;
751 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
753 brcmf_dbg(SDIO, "CLKCTL: set PENDING\n");
754 bus->clkstate = CLK_PENDING;
757 } else if (bus->clkstate == CLK_PENDING) {
758 /* Cancel CA-only interrupt filter */
759 devctl = brcmf_sdiod_regrb(bus->sdiodev,
760 SBSDIO_DEVICE_CTL, &err);
761 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
762 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
766 /* Otherwise, wait here (polling) for HT Avail */
768 msecs_to_jiffies(PMU_MAX_TRANSITION_DLY/1000);
769 while (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
770 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
771 SBSDIO_FUNC1_CHIPCLKCSR,
773 if (time_after(jiffies, timeout))
776 usleep_range(5000, 10000);
779 brcmf_err("HT Avail request error: %d\n", err);
782 if (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
783 brcmf_err("HT Avail timeout (%d): clkctl 0x%02x\n",
784 PMU_MAX_TRANSITION_DLY, clkctl);
788 /* Mark clock available */
789 bus->clkstate = CLK_AVAIL;
790 brcmf_dbg(SDIO, "CLKCTL: turned ON\n");
793 if (!bus->alp_only) {
794 if (SBSDIO_ALPONLY(clkctl))
795 brcmf_err("HT Clock should be on\n");
797 #endif /* defined (DEBUG) */
799 bus->activity = true;
803 if (bus->clkstate == CLK_PENDING) {
804 /* Cancel CA-only interrupt filter */
805 devctl = brcmf_sdiod_regrb(bus->sdiodev,
806 SBSDIO_DEVICE_CTL, &err);
807 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
808 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
812 bus->clkstate = CLK_SDONLY;
813 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
815 brcmf_dbg(SDIO, "CLKCTL: turned OFF\n");
817 brcmf_err("Failed access turning clock off: %d\n",
825 /* Change idle/active SD state */
826 static int brcmf_sdbrcm_sdclk(struct brcmf_sdio *bus, bool on)
828 brcmf_dbg(SDIO, "Enter\n");
831 bus->clkstate = CLK_SDONLY;
833 bus->clkstate = CLK_NONE;
838 /* Transition SD and backplane clock readiness */
839 static int brcmf_sdbrcm_clkctl(struct brcmf_sdio *bus, uint target, bool pendok)
842 uint oldstate = bus->clkstate;
845 brcmf_dbg(SDIO, "Enter\n");
847 /* Early exit if we're already there */
848 if (bus->clkstate == target) {
849 if (target == CLK_AVAIL) {
850 brcmf_sdbrcm_wd_timer(bus, BRCMF_WD_POLL_MS);
851 bus->activity = true;
858 /* Make sure SD clock is available */
859 if (bus->clkstate == CLK_NONE)
860 brcmf_sdbrcm_sdclk(bus, true);
861 /* Now request HT Avail on the backplane */
862 brcmf_sdbrcm_htclk(bus, true, pendok);
863 brcmf_sdbrcm_wd_timer(bus, BRCMF_WD_POLL_MS);
864 bus->activity = true;
868 /* Remove HT request, or bring up SD clock */
869 if (bus->clkstate == CLK_NONE)
870 brcmf_sdbrcm_sdclk(bus, true);
871 else if (bus->clkstate == CLK_AVAIL)
872 brcmf_sdbrcm_htclk(bus, false, false);
874 brcmf_err("request for %d -> %d\n",
875 bus->clkstate, target);
876 brcmf_sdbrcm_wd_timer(bus, BRCMF_WD_POLL_MS);
880 /* Make sure to remove HT request */
881 if (bus->clkstate == CLK_AVAIL)
882 brcmf_sdbrcm_htclk(bus, false, false);
883 /* Now remove the SD clock */
884 brcmf_sdbrcm_sdclk(bus, false);
885 brcmf_sdbrcm_wd_timer(bus, 0);
889 brcmf_dbg(SDIO, "%d -> %d\n", oldstate, bus->clkstate);
896 brcmf_sdbrcm_bus_sleep(struct brcmf_sdio *bus, bool sleep, bool pendok)
899 brcmf_dbg(TRACE, "Enter\n");
900 brcmf_dbg(SDIO, "request %s currently %s\n",
901 (sleep ? "SLEEP" : "WAKE"),
902 (bus->sleeping ? "SLEEP" : "WAKE"));
904 /* If SR is enabled control bus state with KSO */
905 if (bus->sr_enabled) {
906 /* Done if we're already in the requested state */
907 if (sleep == bus->sleeping)
912 /* Don't sleep if something is pending */
913 if (atomic_read(&bus->intstatus) ||
914 atomic_read(&bus->ipend) > 0 ||
915 (!atomic_read(&bus->fcstate) &&
916 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) &&
919 err = brcmf_sdbrcm_kso_control(bus, false);
920 /* disable watchdog */
922 brcmf_sdbrcm_wd_timer(bus, 0);
925 err = brcmf_sdbrcm_kso_control(bus, true);
929 bus->sleeping = sleep;
930 brcmf_dbg(SDIO, "new state %s\n",
931 (sleep ? "SLEEP" : "WAKE"));
933 brcmf_err("error while changing bus sleep state %d\n",
942 if (!bus->sr_enabled)
943 brcmf_sdbrcm_clkctl(bus, CLK_NONE, pendok);
945 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, pendok);
952 static u32 brcmf_sdbrcm_hostmail(struct brcmf_sdio *bus)
959 brcmf_dbg(SDIO, "Enter\n");
961 /* Read mailbox data and ack that we did so */
962 ret = r_sdreg32(bus, &hmb_data,
963 offsetof(struct sdpcmd_regs, tohostmailboxdata));
966 w_sdreg32(bus, SMB_INT_ACK,
967 offsetof(struct sdpcmd_regs, tosbmailbox));
968 bus->sdcnt.f1regdata += 2;
970 /* Dongle recomposed rx frames, accept them again */
971 if (hmb_data & HMB_DATA_NAKHANDLED) {
972 brcmf_dbg(SDIO, "Dongle reports NAK handled, expect rtx of %d\n",
975 brcmf_err("unexpected NAKHANDLED!\n");
978 intstatus |= I_HMB_FRAME_IND;
982 * DEVREADY does not occur with gSPI.
984 if (hmb_data & (HMB_DATA_DEVREADY | HMB_DATA_FWREADY)) {
986 (hmb_data & HMB_DATA_VERSION_MASK) >>
987 HMB_DATA_VERSION_SHIFT;
988 if (bus->sdpcm_ver != SDPCM_PROT_VERSION)
989 brcmf_err("Version mismatch, dongle reports %d, "
991 bus->sdpcm_ver, SDPCM_PROT_VERSION);
993 brcmf_dbg(SDIO, "Dongle ready, protocol version %d\n",
998 * Flow Control has been moved into the RX headers and this out of band
999 * method isn't used any more.
1000 * remaining backward compatible with older dongles.
1002 if (hmb_data & HMB_DATA_FC) {
1003 fcbits = (hmb_data & HMB_DATA_FCDATA_MASK) >>
1004 HMB_DATA_FCDATA_SHIFT;
1006 if (fcbits & ~bus->flowcontrol)
1007 bus->sdcnt.fc_xoff++;
1009 if (bus->flowcontrol & ~fcbits)
1010 bus->sdcnt.fc_xon++;
1012 bus->sdcnt.fc_rcvd++;
1013 bus->flowcontrol = fcbits;
1016 /* Shouldn't be any others */
1017 if (hmb_data & ~(HMB_DATA_DEVREADY |
1018 HMB_DATA_NAKHANDLED |
1021 HMB_DATA_FCDATA_MASK | HMB_DATA_VERSION_MASK))
1022 brcmf_err("Unknown mailbox data content: 0x%02x\n",
1028 static void brcmf_sdbrcm_rxfail(struct brcmf_sdio *bus, bool abort, bool rtx)
1035 brcmf_err("%sterminate frame%s\n",
1036 abort ? "abort command, " : "",
1037 rtx ? ", send NAK" : "");
1040 brcmf_sdiod_abort(bus->sdiodev, SDIO_FUNC_2);
1042 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL,
1044 bus->sdcnt.f1regdata++;
1046 /* Wait until the packet has been flushed (device/FIFO stable) */
1047 for (lastrbc = retries = 0xffff; retries > 0; retries--) {
1048 hi = brcmf_sdiod_regrb(bus->sdiodev,
1049 SBSDIO_FUNC1_RFRAMEBCHI, &err);
1050 lo = brcmf_sdiod_regrb(bus->sdiodev,
1051 SBSDIO_FUNC1_RFRAMEBCLO, &err);
1052 bus->sdcnt.f1regdata += 2;
1054 if ((hi == 0) && (lo == 0))
1057 if ((hi > (lastrbc >> 8)) && (lo > (lastrbc & 0x00ff))) {
1058 brcmf_err("count growing: last 0x%04x now 0x%04x\n",
1059 lastrbc, (hi << 8) + lo);
1061 lastrbc = (hi << 8) + lo;
1065 brcmf_err("count never zeroed: last 0x%04x\n", lastrbc);
1067 brcmf_dbg(SDIO, "flush took %d iterations\n", 0xffff - retries);
1071 err = w_sdreg32(bus, SMB_NAK,
1072 offsetof(struct sdpcmd_regs, tosbmailbox));
1074 bus->sdcnt.f1regdata++;
1079 /* Clear partial in any case */
1080 bus->cur_read.len = 0;
1082 /* If we can't reach the device, signal failure */
1084 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
1087 /* return total length of buffer chain */
1088 static uint brcmf_sdbrcm_glom_len(struct brcmf_sdio *bus)
1094 skb_queue_walk(&bus->glom, p)
1099 static void brcmf_sdbrcm_free_glom(struct brcmf_sdio *bus)
1101 struct sk_buff *cur, *next;
1103 skb_queue_walk_safe(&bus->glom, cur, next) {
1104 skb_unlink(cur, &bus->glom);
1105 brcmu_pkt_buf_free_skb(cur);
1110 * brcmfmac sdio bus specific header
1111 * This is the lowest layer header wrapped on the packets transmitted between
1112 * host and WiFi dongle which contains information needed for SDIO core and
1115 * It consists of 3 parts: hardware header, hardware extension header and
1117 * hardware header (frame tag) - 4 bytes
1118 * Byte 0~1: Frame length
1119 * Byte 2~3: Checksum, bit-wise inverse of frame length
1120 * hardware extension header - 8 bytes
1121 * Tx glom mode only, N/A for Rx or normal Tx
1122 * Byte 0~1: Packet length excluding hw frame tag
1124 * Byte 3: Frame flags, bit 0: last frame indication
1125 * Byte 4~5: Reserved
1126 * Byte 6~7: Tail padding length
1127 * software header - 8 bytes
1128 * Byte 0: Rx/Tx sequence number
1129 * Byte 1: 4 MSB Channel number, 4 LSB arbitrary flag
1130 * Byte 2: Length of next data frame, reserved for Tx
1131 * Byte 3: Data offset
1132 * Byte 4: Flow control bits, reserved for Tx
1133 * Byte 5: Maximum Sequence number allowed by firmware for Tx, N/A for Tx packet
1134 * Byte 6~7: Reserved
1136 #define SDPCM_HWHDR_LEN 4
1137 #define SDPCM_HWEXT_LEN 8
1138 #define SDPCM_SWHDR_LEN 8
1139 #define SDPCM_HDRLEN (SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN)
1140 /* software header */
1141 #define SDPCM_SEQ_MASK 0x000000ff
1142 #define SDPCM_SEQ_WRAP 256
1143 #define SDPCM_CHANNEL_MASK 0x00000f00
1144 #define SDPCM_CHANNEL_SHIFT 8
1145 #define SDPCM_CONTROL_CHANNEL 0 /* Control */
1146 #define SDPCM_EVENT_CHANNEL 1 /* Asyc Event Indication */
1147 #define SDPCM_DATA_CHANNEL 2 /* Data Xmit/Recv */
1148 #define SDPCM_GLOM_CHANNEL 3 /* Coalesced packets */
1149 #define SDPCM_TEST_CHANNEL 15 /* Test/debug packets */
1150 #define SDPCM_GLOMDESC(p) (((u8 *)p)[1] & 0x80)
1151 #define SDPCM_NEXTLEN_MASK 0x00ff0000
1152 #define SDPCM_NEXTLEN_SHIFT 16
1153 #define SDPCM_DOFFSET_MASK 0xff000000
1154 #define SDPCM_DOFFSET_SHIFT 24
1155 #define SDPCM_FCMASK_MASK 0x000000ff
1156 #define SDPCM_WINDOW_MASK 0x0000ff00
1157 #define SDPCM_WINDOW_SHIFT 8
1159 static inline u8 brcmf_sdio_getdatoffset(u8 *swheader)
1162 hdrvalue = *(u32 *)swheader;
1163 return (u8)((hdrvalue & SDPCM_DOFFSET_MASK) >> SDPCM_DOFFSET_SHIFT);
1166 static int brcmf_sdio_hdparse(struct brcmf_sdio *bus, u8 *header,
1167 struct brcmf_sdio_hdrinfo *rd,
1168 enum brcmf_sdio_frmtype type)
1171 u8 rx_seq, fc, tx_seq_max;
1174 trace_brcmf_sdpcm_hdr(SDPCM_RX, header);
1177 len = get_unaligned_le16(header);
1178 checksum = get_unaligned_le16(header + sizeof(u16));
1179 /* All zero means no more to read */
1180 if (!(len | checksum)) {
1181 bus->rxpending = false;
1184 if ((u16)(~(len ^ checksum))) {
1185 brcmf_err("HW header checksum error\n");
1186 bus->sdcnt.rx_badhdr++;
1187 brcmf_sdbrcm_rxfail(bus, false, false);
1190 if (len < SDPCM_HDRLEN) {
1191 brcmf_err("HW header length error\n");
1194 if (type == BRCMF_SDIO_FT_SUPER &&
1195 (roundup(len, bus->blocksize) != rd->len)) {
1196 brcmf_err("HW superframe header length error\n");
1199 if (type == BRCMF_SDIO_FT_SUB && len > rd->len) {
1200 brcmf_err("HW subframe header length error\n");
1205 /* software header */
1206 header += SDPCM_HWHDR_LEN;
1207 swheader = le32_to_cpu(*(__le32 *)header);
1208 if (type == BRCMF_SDIO_FT_SUPER && SDPCM_GLOMDESC(header)) {
1209 brcmf_err("Glom descriptor found in superframe head\n");
1213 rx_seq = (u8)(swheader & SDPCM_SEQ_MASK);
1214 rd->channel = (swheader & SDPCM_CHANNEL_MASK) >> SDPCM_CHANNEL_SHIFT;
1215 if (len > MAX_RX_DATASZ && rd->channel != SDPCM_CONTROL_CHANNEL &&
1216 type != BRCMF_SDIO_FT_SUPER) {
1217 brcmf_err("HW header length too long\n");
1218 bus->sdcnt.rx_toolong++;
1219 brcmf_sdbrcm_rxfail(bus, false, false);
1223 if (type == BRCMF_SDIO_FT_SUPER && rd->channel != SDPCM_GLOM_CHANNEL) {
1224 brcmf_err("Wrong channel for superframe\n");
1228 if (type == BRCMF_SDIO_FT_SUB && rd->channel != SDPCM_DATA_CHANNEL &&
1229 rd->channel != SDPCM_EVENT_CHANNEL) {
1230 brcmf_err("Wrong channel for subframe\n");
1234 rd->dat_offset = brcmf_sdio_getdatoffset(header);
1235 if (rd->dat_offset < SDPCM_HDRLEN || rd->dat_offset > rd->len) {
1236 brcmf_err("seq %d: bad data offset\n", rx_seq);
1237 bus->sdcnt.rx_badhdr++;
1238 brcmf_sdbrcm_rxfail(bus, false, false);
1242 if (rd->seq_num != rx_seq) {
1243 brcmf_err("seq %d: sequence number error, expect %d\n",
1244 rx_seq, rd->seq_num);
1245 bus->sdcnt.rx_badseq++;
1246 rd->seq_num = rx_seq;
1248 /* no need to check the reset for subframe */
1249 if (type == BRCMF_SDIO_FT_SUB)
1251 rd->len_nxtfrm = (swheader & SDPCM_NEXTLEN_MASK) >> SDPCM_NEXTLEN_SHIFT;
1252 if (rd->len_nxtfrm << 4 > MAX_RX_DATASZ) {
1253 /* only warm for NON glom packet */
1254 if (rd->channel != SDPCM_GLOM_CHANNEL)
1255 brcmf_err("seq %d: next length error\n", rx_seq);
1258 swheader = le32_to_cpu(*(__le32 *)(header + 4));
1259 fc = swheader & SDPCM_FCMASK_MASK;
1260 if (bus->flowcontrol != fc) {
1261 if (~bus->flowcontrol & fc)
1262 bus->sdcnt.fc_xoff++;
1263 if (bus->flowcontrol & ~fc)
1264 bus->sdcnt.fc_xon++;
1265 bus->sdcnt.fc_rcvd++;
1266 bus->flowcontrol = fc;
1268 tx_seq_max = (swheader & SDPCM_WINDOW_MASK) >> SDPCM_WINDOW_SHIFT;
1269 if ((u8)(tx_seq_max - bus->tx_seq) > 0x40) {
1270 brcmf_err("seq %d: max tx seq number error\n", rx_seq);
1271 tx_seq_max = bus->tx_seq + 2;
1273 bus->tx_max = tx_seq_max;
1278 static inline void brcmf_sdio_update_hwhdr(u8 *header, u16 frm_length)
1280 *(__le16 *)header = cpu_to_le16(frm_length);
1281 *(((__le16 *)header) + 1) = cpu_to_le16(~frm_length);
1284 static void brcmf_sdio_hdpack(struct brcmf_sdio *bus, u8 *header,
1285 struct brcmf_sdio_hdrinfo *hd_info)
1290 brcmf_sdio_update_hwhdr(header, hd_info->len);
1291 hdr_offset = SDPCM_HWHDR_LEN;
1294 hdrval = (hd_info->len - hdr_offset) | (hd_info->lastfrm << 24);
1295 *((__le32 *)(header + hdr_offset)) = cpu_to_le32(hdrval);
1296 hdrval = (u16)hd_info->tail_pad << 16;
1297 *(((__le32 *)(header + hdr_offset)) + 1) = cpu_to_le32(hdrval);
1298 hdr_offset += SDPCM_HWEXT_LEN;
1301 hdrval = hd_info->seq_num;
1302 hdrval |= (hd_info->channel << SDPCM_CHANNEL_SHIFT) &
1304 hdrval |= (hd_info->dat_offset << SDPCM_DOFFSET_SHIFT) &
1306 *((__le32 *)(header + hdr_offset)) = cpu_to_le32(hdrval);
1307 *(((__le32 *)(header + hdr_offset)) + 1) = 0;
1308 trace_brcmf_sdpcm_hdr(SDPCM_TX + !!(bus->txglom), header);
1311 static u8 brcmf_sdbrcm_rxglom(struct brcmf_sdio *bus, u8 rxseq)
1316 struct sk_buff *pfirst, *pnext;
1321 struct brcmf_sdio_hdrinfo rd_new;
1323 /* If packets, issue read(s) and send up packet chain */
1324 /* Return sequence numbers consumed? */
1326 brcmf_dbg(SDIO, "start: glomd %p glom %p\n",
1327 bus->glomd, skb_peek(&bus->glom));
1329 /* If there's a descriptor, generate the packet chain */
1331 pfirst = pnext = NULL;
1332 dlen = (u16) (bus->glomd->len);
1333 dptr = bus->glomd->data;
1334 if (!dlen || (dlen & 1)) {
1335 brcmf_err("bad glomd len(%d), ignore descriptor\n",
1340 for (totlen = num = 0; dlen; num++) {
1341 /* Get (and move past) next length */
1342 sublen = get_unaligned_le16(dptr);
1343 dlen -= sizeof(u16);
1344 dptr += sizeof(u16);
1345 if ((sublen < SDPCM_HDRLEN) ||
1346 ((num == 0) && (sublen < (2 * SDPCM_HDRLEN)))) {
1347 brcmf_err("descriptor len %d bad: %d\n",
1352 if (sublen % bus->sgentry_align) {
1353 brcmf_err("sublen %d not multiple of %d\n",
1354 sublen, bus->sgentry_align);
1358 /* For last frame, adjust read len so total
1359 is a block multiple */
1362 (roundup(totlen, bus->blocksize) - totlen);
1363 totlen = roundup(totlen, bus->blocksize);
1366 /* Allocate/chain packet for next subframe */
1367 pnext = brcmu_pkt_buf_get_skb(sublen + bus->sgentry_align);
1368 if (pnext == NULL) {
1369 brcmf_err("bcm_pkt_buf_get_skb failed, num %d len %d\n",
1373 skb_queue_tail(&bus->glom, pnext);
1375 /* Adhere to start alignment requirements */
1376 pkt_align(pnext, sublen, bus->sgentry_align);
1379 /* If all allocations succeeded, save packet chain
1382 brcmf_dbg(GLOM, "allocated %d-byte packet chain for %d subframes\n",
1384 if (BRCMF_GLOM_ON() && bus->cur_read.len &&
1385 totlen != bus->cur_read.len) {
1386 brcmf_dbg(GLOM, "glomdesc mismatch: nextlen %d glomdesc %d rxseq %d\n",
1387 bus->cur_read.len, totlen, rxseq);
1389 pfirst = pnext = NULL;
1391 brcmf_sdbrcm_free_glom(bus);
1395 /* Done with descriptor packet */
1396 brcmu_pkt_buf_free_skb(bus->glomd);
1398 bus->cur_read.len = 0;
1401 /* Ok -- either we just generated a packet chain,
1402 or had one from before */
1403 if (!skb_queue_empty(&bus->glom)) {
1404 if (BRCMF_GLOM_ON()) {
1405 brcmf_dbg(GLOM, "try superframe read, packet chain:\n");
1406 skb_queue_walk(&bus->glom, pnext) {
1407 brcmf_dbg(GLOM, " %p: %p len 0x%04x (%d)\n",
1408 pnext, (u8 *) (pnext->data),
1409 pnext->len, pnext->len);
1413 pfirst = skb_peek(&bus->glom);
1414 dlen = (u16) brcmf_sdbrcm_glom_len(bus);
1416 /* Do an SDIO read for the superframe. Configurable iovar to
1417 * read directly into the chained packet, or allocate a large
1418 * packet and and copy into the chain.
1420 sdio_claim_host(bus->sdiodev->func[1]);
1421 errcode = brcmf_sdiod_recv_chain(bus->sdiodev,
1422 bus->sdiodev->sbwad,
1423 SDIO_FUNC_2, F2SYNC,
1425 sdio_release_host(bus->sdiodev->func[1]);
1426 bus->sdcnt.f2rxdata++;
1428 /* On failure, kill the superframe, allow a couple retries */
1430 brcmf_err("glom read of %d bytes failed: %d\n",
1433 sdio_claim_host(bus->sdiodev->func[1]);
1434 if (bus->glomerr++ < 3) {
1435 brcmf_sdbrcm_rxfail(bus, true, true);
1438 brcmf_sdbrcm_rxfail(bus, true, false);
1439 bus->sdcnt.rxglomfail++;
1440 brcmf_sdbrcm_free_glom(bus);
1442 sdio_release_host(bus->sdiodev->func[1]);
1446 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
1447 pfirst->data, min_t(int, pfirst->len, 48),
1450 rd_new.seq_num = rxseq;
1452 sdio_claim_host(bus->sdiodev->func[1]);
1453 errcode = brcmf_sdio_hdparse(bus, pfirst->data, &rd_new,
1454 BRCMF_SDIO_FT_SUPER);
1455 sdio_release_host(bus->sdiodev->func[1]);
1456 bus->cur_read.len = rd_new.len_nxtfrm << 4;
1458 /* Remove superframe header, remember offset */
1459 skb_pull(pfirst, rd_new.dat_offset);
1460 sfdoff = rd_new.dat_offset;
1463 /* Validate all the subframe headers */
1464 skb_queue_walk(&bus->glom, pnext) {
1465 /* leave when invalid subframe is found */
1469 rd_new.len = pnext->len;
1470 rd_new.seq_num = rxseq++;
1471 sdio_claim_host(bus->sdiodev->func[1]);
1472 errcode = brcmf_sdio_hdparse(bus, pnext->data, &rd_new,
1474 sdio_release_host(bus->sdiodev->func[1]);
1475 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
1476 pnext->data, 32, "subframe:\n");
1482 /* Terminate frame on error, request
1484 sdio_claim_host(bus->sdiodev->func[1]);
1485 if (bus->glomerr++ < 3) {
1486 /* Restore superframe header space */
1487 skb_push(pfirst, sfdoff);
1488 brcmf_sdbrcm_rxfail(bus, true, true);
1491 brcmf_sdbrcm_rxfail(bus, true, false);
1492 bus->sdcnt.rxglomfail++;
1493 brcmf_sdbrcm_free_glom(bus);
1495 sdio_release_host(bus->sdiodev->func[1]);
1496 bus->cur_read.len = 0;
1500 /* Basic SD framing looks ok - process each packet (header) */
1502 skb_queue_walk_safe(&bus->glom, pfirst, pnext) {
1503 dptr = (u8 *) (pfirst->data);
1504 sublen = get_unaligned_le16(dptr);
1505 doff = brcmf_sdio_getdatoffset(&dptr[SDPCM_HWHDR_LEN]);
1507 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_DATA_ON(),
1509 "Rx Subframe Data:\n");
1511 __skb_trim(pfirst, sublen);
1512 skb_pull(pfirst, doff);
1514 if (pfirst->len == 0) {
1515 skb_unlink(pfirst, &bus->glom);
1516 brcmu_pkt_buf_free_skb(pfirst);
1520 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
1522 min_t(int, pfirst->len, 32),
1523 "subframe %d to stack, %p (%p/%d) nxt/lnk %p/%p\n",
1524 bus->glom.qlen, pfirst, pfirst->data,
1525 pfirst->len, pfirst->next,
1527 skb_unlink(pfirst, &bus->glom);
1528 brcmf_rx_frame(bus->sdiodev->dev, pfirst);
1529 bus->sdcnt.rxglompkts++;
1532 bus->sdcnt.rxglomframes++;
1537 static int brcmf_sdbrcm_dcmd_resp_wait(struct brcmf_sdio *bus, uint *condition,
1540 DECLARE_WAITQUEUE(wait, current);
1541 int timeout = msecs_to_jiffies(DCMD_RESP_TIMEOUT);
1543 /* Wait until control frame is available */
1544 add_wait_queue(&bus->dcmd_resp_wait, &wait);
1545 set_current_state(TASK_INTERRUPTIBLE);
1547 while (!(*condition) && (!signal_pending(current) && timeout))
1548 timeout = schedule_timeout(timeout);
1550 if (signal_pending(current))
1553 set_current_state(TASK_RUNNING);
1554 remove_wait_queue(&bus->dcmd_resp_wait, &wait);
1559 static int brcmf_sdbrcm_dcmd_resp_wake(struct brcmf_sdio *bus)
1561 if (waitqueue_active(&bus->dcmd_resp_wait))
1562 wake_up_interruptible(&bus->dcmd_resp_wait);
1567 brcmf_sdbrcm_read_control(struct brcmf_sdio *bus, u8 *hdr, uint len, uint doff)
1570 u8 *buf = NULL, *rbuf;
1573 brcmf_dbg(TRACE, "Enter\n");
1576 buf = vzalloc(bus->rxblen);
1581 pad = ((unsigned long)rbuf % bus->head_align);
1583 rbuf += (bus->head_align - pad);
1585 /* Copy the already-read portion over */
1586 memcpy(buf, hdr, BRCMF_FIRSTREAD);
1587 if (len <= BRCMF_FIRSTREAD)
1590 /* Raise rdlen to next SDIO block to avoid tail command */
1591 rdlen = len - BRCMF_FIRSTREAD;
1592 if (bus->roundup && bus->blocksize && (rdlen > bus->blocksize)) {
1593 pad = bus->blocksize - (rdlen % bus->blocksize);
1594 if ((pad <= bus->roundup) && (pad < bus->blocksize) &&
1595 ((len + pad) < bus->sdiodev->bus_if->maxctl))
1597 } else if (rdlen % bus->head_align) {
1598 rdlen += bus->head_align - (rdlen % bus->head_align);
1601 /* Drop if the read is too big or it exceeds our maximum */
1602 if ((rdlen + BRCMF_FIRSTREAD) > bus->sdiodev->bus_if->maxctl) {
1603 brcmf_err("%d-byte control read exceeds %d-byte buffer\n",
1604 rdlen, bus->sdiodev->bus_if->maxctl);
1605 brcmf_sdbrcm_rxfail(bus, false, false);
1609 if ((len - doff) > bus->sdiodev->bus_if->maxctl) {
1610 brcmf_err("%d-byte ctl frame (%d-byte ctl data) exceeds %d-byte limit\n",
1611 len, len - doff, bus->sdiodev->bus_if->maxctl);
1612 bus->sdcnt.rx_toolong++;
1613 brcmf_sdbrcm_rxfail(bus, false, false);
1617 /* Read remain of frame body */
1618 sdret = brcmf_sdiod_recv_buf(bus->sdiodev, bus->sdiodev->sbwad,
1619 SDIO_FUNC_2, F2SYNC, rbuf, rdlen);
1620 bus->sdcnt.f2rxdata++;
1622 /* Control frame failures need retransmission */
1624 brcmf_err("read %d control bytes failed: %d\n",
1626 bus->sdcnt.rxc_errors++;
1627 brcmf_sdbrcm_rxfail(bus, true, true);
1630 memcpy(buf + BRCMF_FIRSTREAD, rbuf, rdlen);
1634 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_CTL_ON(),
1635 buf, len, "RxCtrl:\n");
1637 /* Point to valid data and indicate its length */
1638 spin_lock_bh(&bus->rxctl_lock);
1640 brcmf_err("last control frame is being processed.\n");
1641 spin_unlock_bh(&bus->rxctl_lock);
1645 bus->rxctl = buf + doff;
1646 bus->rxctl_orig = buf;
1647 bus->rxlen = len - doff;
1648 spin_unlock_bh(&bus->rxctl_lock);
1651 /* Awake any waiters */
1652 brcmf_sdbrcm_dcmd_resp_wake(bus);
1655 /* Pad read to blocksize for efficiency */
1656 static void brcmf_pad(struct brcmf_sdio *bus, u16 *pad, u16 *rdlen)
1658 if (bus->roundup && bus->blocksize && *rdlen > bus->blocksize) {
1659 *pad = bus->blocksize - (*rdlen % bus->blocksize);
1660 if (*pad <= bus->roundup && *pad < bus->blocksize &&
1661 *rdlen + *pad + BRCMF_FIRSTREAD < MAX_RX_DATASZ)
1663 } else if (*rdlen % bus->head_align) {
1664 *rdlen += bus->head_align - (*rdlen % bus->head_align);
1668 static uint brcmf_sdio_readframes(struct brcmf_sdio *bus, uint maxframes)
1670 struct sk_buff *pkt; /* Packet for event or data frames */
1671 u16 pad; /* Number of pad bytes to read */
1672 uint rxleft = 0; /* Remaining number of frames allowed */
1673 int ret; /* Return code from calls */
1674 uint rxcount = 0; /* Total frames read */
1675 struct brcmf_sdio_hdrinfo *rd = &bus->cur_read, rd_new;
1678 brcmf_dbg(TRACE, "Enter\n");
1680 /* Not finished unless we encounter no more frames indication */
1681 bus->rxpending = true;
1683 for (rd->seq_num = bus->rx_seq, rxleft = maxframes;
1684 !bus->rxskip && rxleft &&
1685 bus->sdiodev->bus_if->state != BRCMF_BUS_DOWN;
1686 rd->seq_num++, rxleft--) {
1688 /* Handle glomming separately */
1689 if (bus->glomd || !skb_queue_empty(&bus->glom)) {
1691 brcmf_dbg(GLOM, "calling rxglom: glomd %p, glom %p\n",
1692 bus->glomd, skb_peek(&bus->glom));
1693 cnt = brcmf_sdbrcm_rxglom(bus, rd->seq_num);
1694 brcmf_dbg(GLOM, "rxglom returned %d\n", cnt);
1695 rd->seq_num += cnt - 1;
1696 rxleft = (rxleft > cnt) ? (rxleft - cnt) : 1;
1700 rd->len_left = rd->len;
1701 /* read header first for unknow frame length */
1702 sdio_claim_host(bus->sdiodev->func[1]);
1704 ret = brcmf_sdiod_recv_buf(bus->sdiodev,
1705 bus->sdiodev->sbwad,
1706 SDIO_FUNC_2, F2SYNC,
1707 bus->rxhdr, BRCMF_FIRSTREAD);
1708 bus->sdcnt.f2rxhdrs++;
1710 brcmf_err("RXHEADER FAILED: %d\n",
1712 bus->sdcnt.rx_hdrfail++;
1713 brcmf_sdbrcm_rxfail(bus, true, true);
1714 sdio_release_host(bus->sdiodev->func[1]);
1718 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() || BRCMF_HDRS_ON(),
1719 bus->rxhdr, SDPCM_HDRLEN,
1722 if (brcmf_sdio_hdparse(bus, bus->rxhdr, rd,
1723 BRCMF_SDIO_FT_NORMAL)) {
1724 sdio_release_host(bus->sdiodev->func[1]);
1725 if (!bus->rxpending)
1731 if (rd->channel == SDPCM_CONTROL_CHANNEL) {
1732 brcmf_sdbrcm_read_control(bus, bus->rxhdr,
1735 /* prepare the descriptor for the next read */
1736 rd->len = rd->len_nxtfrm << 4;
1738 /* treat all packet as event if we don't know */
1739 rd->channel = SDPCM_EVENT_CHANNEL;
1740 sdio_release_host(bus->sdiodev->func[1]);
1743 rd->len_left = rd->len > BRCMF_FIRSTREAD ?
1744 rd->len - BRCMF_FIRSTREAD : 0;
1745 head_read = BRCMF_FIRSTREAD;
1748 brcmf_pad(bus, &pad, &rd->len_left);
1750 pkt = brcmu_pkt_buf_get_skb(rd->len_left + head_read +
1753 /* Give up on data, request rtx of events */
1754 brcmf_err("brcmu_pkt_buf_get_skb failed\n");
1755 brcmf_sdbrcm_rxfail(bus, false,
1756 RETRYCHAN(rd->channel));
1757 sdio_release_host(bus->sdiodev->func[1]);
1760 skb_pull(pkt, head_read);
1761 pkt_align(pkt, rd->len_left, bus->head_align);
1763 ret = brcmf_sdiod_recv_pkt(bus->sdiodev, bus->sdiodev->sbwad,
1764 SDIO_FUNC_2, F2SYNC, pkt);
1765 bus->sdcnt.f2rxdata++;
1766 sdio_release_host(bus->sdiodev->func[1]);
1769 brcmf_err("read %d bytes from channel %d failed: %d\n",
1770 rd->len, rd->channel, ret);
1771 brcmu_pkt_buf_free_skb(pkt);
1772 sdio_claim_host(bus->sdiodev->func[1]);
1773 brcmf_sdbrcm_rxfail(bus, true,
1774 RETRYCHAN(rd->channel));
1775 sdio_release_host(bus->sdiodev->func[1]);
1780 skb_push(pkt, head_read);
1781 memcpy(pkt->data, bus->rxhdr, head_read);
1784 memcpy(bus->rxhdr, pkt->data, SDPCM_HDRLEN);
1785 rd_new.seq_num = rd->seq_num;
1786 sdio_claim_host(bus->sdiodev->func[1]);
1787 if (brcmf_sdio_hdparse(bus, bus->rxhdr, &rd_new,
1788 BRCMF_SDIO_FT_NORMAL)) {
1790 brcmu_pkt_buf_free_skb(pkt);
1792 bus->sdcnt.rx_readahead_cnt++;
1793 if (rd->len != roundup(rd_new.len, 16)) {
1794 brcmf_err("frame length mismatch:read %d, should be %d\n",
1796 roundup(rd_new.len, 16) >> 4);
1798 brcmf_sdbrcm_rxfail(bus, true, true);
1799 sdio_release_host(bus->sdiodev->func[1]);
1800 brcmu_pkt_buf_free_skb(pkt);
1803 sdio_release_host(bus->sdiodev->func[1]);
1804 rd->len_nxtfrm = rd_new.len_nxtfrm;
1805 rd->channel = rd_new.channel;
1806 rd->dat_offset = rd_new.dat_offset;
1808 brcmf_dbg_hex_dump(!(BRCMF_BYTES_ON() &&
1811 bus->rxhdr, SDPCM_HDRLEN,
1814 if (rd_new.channel == SDPCM_CONTROL_CHANNEL) {
1815 brcmf_err("readahead on control packet %d?\n",
1817 /* Force retry w/normal header read */
1819 sdio_claim_host(bus->sdiodev->func[1]);
1820 brcmf_sdbrcm_rxfail(bus, false, true);
1821 sdio_release_host(bus->sdiodev->func[1]);
1822 brcmu_pkt_buf_free_skb(pkt);
1827 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_DATA_ON(),
1828 pkt->data, rd->len, "Rx Data:\n");
1830 /* Save superframe descriptor and allocate packet frame */
1831 if (rd->channel == SDPCM_GLOM_CHANNEL) {
1832 if (SDPCM_GLOMDESC(&bus->rxhdr[SDPCM_HWHDR_LEN])) {
1833 brcmf_dbg(GLOM, "glom descriptor, %d bytes:\n",
1835 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
1838 __skb_trim(pkt, rd->len);
1839 skb_pull(pkt, SDPCM_HDRLEN);
1842 brcmf_err("%s: glom superframe w/o "
1843 "descriptor!\n", __func__);
1844 sdio_claim_host(bus->sdiodev->func[1]);
1845 brcmf_sdbrcm_rxfail(bus, false, false);
1846 sdio_release_host(bus->sdiodev->func[1]);
1848 /* prepare the descriptor for the next read */
1849 rd->len = rd->len_nxtfrm << 4;
1851 /* treat all packet as event if we don't know */
1852 rd->channel = SDPCM_EVENT_CHANNEL;
1856 /* Fill in packet len and prio, deliver upward */
1857 __skb_trim(pkt, rd->len);
1858 skb_pull(pkt, rd->dat_offset);
1860 /* prepare the descriptor for the next read */
1861 rd->len = rd->len_nxtfrm << 4;
1863 /* treat all packet as event if we don't know */
1864 rd->channel = SDPCM_EVENT_CHANNEL;
1866 if (pkt->len == 0) {
1867 brcmu_pkt_buf_free_skb(pkt);
1871 brcmf_rx_frame(bus->sdiodev->dev, pkt);
1874 rxcount = maxframes - rxleft;
1875 /* Message if we hit the limit */
1877 brcmf_dbg(DATA, "hit rx limit of %d frames\n", maxframes);
1879 brcmf_dbg(DATA, "processed %d frames\n", rxcount);
1880 /* Back off rxseq if awaiting rtx, update rx_seq */
1883 bus->rx_seq = rd->seq_num;
1889 brcmf_sdbrcm_wait_event_wakeup(struct brcmf_sdio *bus)
1891 if (waitqueue_active(&bus->ctrl_wait))
1892 wake_up_interruptible(&bus->ctrl_wait);
1896 static int brcmf_sdio_txpkt_hdalign(struct brcmf_sdio *bus, struct sk_buff *pkt)
1901 dat_buf = (u8 *)(pkt->data);
1903 /* Check head padding */
1904 head_pad = ((unsigned long)dat_buf % bus->head_align);
1906 if (skb_headroom(pkt) < head_pad) {
1907 bus->sdiodev->bus_if->tx_realloc++;
1909 if (skb_cow(pkt, head_pad))
1912 skb_push(pkt, head_pad);
1913 dat_buf = (u8 *)(pkt->data);
1914 memset(dat_buf, 0, head_pad + bus->tx_hdrlen);
1920 * struct brcmf_skbuff_cb reserves first two bytes in sk_buff::cb for
1923 /* flag marking a dummy skb added for DMA alignment requirement */
1924 #define ALIGN_SKB_FLAG 0x8000
1925 /* bit mask of data length chopped from the previous packet */
1926 #define ALIGN_SKB_CHOP_LEN_MASK 0x7fff
1928 static int brcmf_sdio_txpkt_prep_sg(struct brcmf_sdio *bus,
1929 struct sk_buff_head *pktq,
1930 struct sk_buff *pkt, u16 total_len)
1932 struct brcmf_sdio_dev *sdiodev;
1933 struct sk_buff *pkt_pad;
1934 u16 tail_pad, tail_chop, chain_pad;
1935 unsigned int blksize;
1939 sdiodev = bus->sdiodev;
1940 blksize = sdiodev->func[SDIO_FUNC_2]->cur_blksize;
1941 /* sg entry alignment should be a divisor of block size */
1942 WARN_ON(blksize % bus->sgentry_align);
1944 /* Check tail padding */
1945 lastfrm = skb_queue_is_last(pktq, pkt);
1947 tail_chop = pkt->len % bus->sgentry_align;
1949 tail_pad = bus->sgentry_align - tail_chop;
1950 chain_pad = (total_len + tail_pad) % blksize;
1951 if (lastfrm && chain_pad)
1952 tail_pad += blksize - chain_pad;
1953 if (skb_tailroom(pkt) < tail_pad && pkt->len > blksize) {
1954 pkt_pad = bus->txglom_sgpad;
1955 if (pkt_pad == NULL)
1956 brcmu_pkt_buf_get_skb(tail_pad + tail_chop);
1957 if (pkt_pad == NULL)
1959 ret = brcmf_sdio_txpkt_hdalign(bus, pkt_pad);
1960 if (unlikely(ret < 0))
1962 memcpy(pkt_pad->data,
1963 pkt->data + pkt->len - tail_chop,
1965 *(u32 *)(pkt_pad->cb) = ALIGN_SKB_FLAG + tail_chop;
1966 skb_trim(pkt, pkt->len - tail_chop);
1967 __skb_queue_after(pktq, pkt, pkt_pad);
1969 ntail = pkt->data_len + tail_pad -
1970 (pkt->end - pkt->tail);
1971 if (skb_cloned(pkt) || ntail > 0)
1972 if (pskb_expand_head(pkt, 0, ntail, GFP_ATOMIC))
1974 if (skb_linearize(pkt))
1976 __skb_put(pkt, tail_pad);
1983 * brcmf_sdio_txpkt_prep - packet preparation for transmit
1984 * @bus: brcmf_sdio structure pointer
1985 * @pktq: packet list pointer
1986 * @chan: virtual channel to transmit the packet
1988 * Processes to be applied to the packet
1989 * - Align data buffer pointer
1990 * - Align data buffer length
1992 * Return: negative value if there is error
1995 brcmf_sdio_txpkt_prep(struct brcmf_sdio *bus, struct sk_buff_head *pktq,
1998 u16 head_pad, total_len;
1999 struct sk_buff *pkt_next;
2002 struct brcmf_sdio_hdrinfo hd_info = {0};
2004 txseq = bus->tx_seq;
2006 skb_queue_walk(pktq, pkt_next) {
2007 /* alignment packet inserted in previous
2008 * loop cycle can be skipped as it is
2009 * already properly aligned and does not
2010 * need an sdpcm header.
2012 if (*(u32 *)(pkt_next->cb) & ALIGN_SKB_FLAG)
2015 /* align packet data pointer */
2016 ret = brcmf_sdio_txpkt_hdalign(bus, pkt_next);
2019 head_pad = (u16)ret;
2021 memset(pkt_next->data, 0, head_pad + bus->tx_hdrlen);
2023 total_len += pkt_next->len;
2025 hd_info.len = pkt_next->len;
2026 hd_info.lastfrm = skb_queue_is_last(pktq, pkt_next);
2027 if (bus->txglom && pktq->qlen > 1) {
2028 ret = brcmf_sdio_txpkt_prep_sg(bus, pktq,
2029 pkt_next, total_len);
2032 hd_info.tail_pad = (u16)ret;
2033 total_len += (u16)ret;
2036 hd_info.channel = chan;
2037 hd_info.dat_offset = head_pad + bus->tx_hdrlen;
2038 hd_info.seq_num = txseq++;
2040 /* Now fill the header */
2041 brcmf_sdio_hdpack(bus, pkt_next->data, &hd_info);
2043 if (BRCMF_BYTES_ON() &&
2044 ((BRCMF_CTL_ON() && chan == SDPCM_CONTROL_CHANNEL) ||
2045 (BRCMF_DATA_ON() && chan != SDPCM_CONTROL_CHANNEL)))
2046 brcmf_dbg_hex_dump(true, pkt_next, hd_info.len,
2048 else if (BRCMF_HDRS_ON())
2049 brcmf_dbg_hex_dump(true, pkt_next,
2050 head_pad + bus->tx_hdrlen,
2053 /* Hardware length tag of the first packet should be total
2054 * length of the chain (including padding)
2057 brcmf_sdio_update_hwhdr(pktq->next->data, total_len);
2062 * brcmf_sdio_txpkt_postp - packet post processing for transmit
2063 * @bus: brcmf_sdio structure pointer
2064 * @pktq: packet list pointer
2066 * Processes to be applied to the packet
2067 * - Remove head padding
2068 * - Remove tail padding
2071 brcmf_sdio_txpkt_postp(struct brcmf_sdio *bus, struct sk_buff_head *pktq)
2076 u32 dummy_flags, chop_len;
2077 struct sk_buff *pkt_next, *tmp, *pkt_prev;
2079 skb_queue_walk_safe(pktq, pkt_next, tmp) {
2080 dummy_flags = *(u32 *)(pkt_next->cb);
2081 if (dummy_flags & ALIGN_SKB_FLAG) {
2082 chop_len = dummy_flags & ALIGN_SKB_CHOP_LEN_MASK;
2084 pkt_prev = pkt_next->prev;
2085 skb_put(pkt_prev, chop_len);
2087 __skb_unlink(pkt_next, pktq);
2088 brcmu_pkt_buf_free_skb(pkt_next);
2090 hdr = pkt_next->data + bus->tx_hdrlen - SDPCM_SWHDR_LEN;
2091 dat_offset = le32_to_cpu(*(__le32 *)hdr);
2092 dat_offset = (dat_offset & SDPCM_DOFFSET_MASK) >>
2093 SDPCM_DOFFSET_SHIFT;
2094 skb_pull(pkt_next, dat_offset);
2096 tail_pad = le16_to_cpu(*(__le16 *)(hdr - 2));
2097 skb_trim(pkt_next, pkt_next->len - tail_pad);
2103 /* Writes a HW/SW header into the packet and sends it. */
2104 /* Assumes: (a) header space already there, (b) caller holds lock */
2105 static int brcmf_sdbrcm_txpkt(struct brcmf_sdio *bus, struct sk_buff_head *pktq,
2110 struct sk_buff *pkt_next, *tmp;
2112 brcmf_dbg(TRACE, "Enter\n");
2114 ret = brcmf_sdio_txpkt_prep(bus, pktq, chan);
2118 sdio_claim_host(bus->sdiodev->func[1]);
2119 ret = brcmf_sdiod_send_pkt(bus->sdiodev, bus->sdiodev->sbwad,
2120 SDIO_FUNC_2, F2SYNC, pktq);
2121 bus->sdcnt.f2txdata++;
2124 /* On failure, abort the command and terminate the frame */
2125 brcmf_dbg(INFO, "sdio error %d, abort command and terminate frame\n",
2127 bus->sdcnt.tx_sderrs++;
2129 brcmf_sdiod_abort(bus->sdiodev, SDIO_FUNC_2);
2130 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL,
2132 bus->sdcnt.f1regdata++;
2134 for (i = 0; i < 3; i++) {
2136 hi = brcmf_sdiod_regrb(bus->sdiodev,
2137 SBSDIO_FUNC1_WFRAMEBCHI, NULL);
2138 lo = brcmf_sdiod_regrb(bus->sdiodev,
2139 SBSDIO_FUNC1_WFRAMEBCLO, NULL);
2140 bus->sdcnt.f1regdata += 2;
2141 if ((hi == 0) && (lo == 0))
2145 sdio_release_host(bus->sdiodev->func[1]);
2148 brcmf_sdio_txpkt_postp(bus, pktq);
2150 bus->tx_seq = (bus->tx_seq + pktq->qlen) % SDPCM_SEQ_WRAP;
2151 skb_queue_walk_safe(pktq, pkt_next, tmp) {
2152 __skb_unlink(pkt_next, pktq);
2153 brcmf_txcomplete(bus->sdiodev->dev, pkt_next, ret == 0);
2158 static uint brcmf_sdbrcm_sendfromq(struct brcmf_sdio *bus, uint maxframes)
2160 struct sk_buff *pkt;
2161 struct sk_buff_head pktq;
2163 int ret = 0, prec_out, i;
2165 u8 tx_prec_map, pkt_num;
2167 brcmf_dbg(TRACE, "Enter\n");
2169 tx_prec_map = ~bus->flowcontrol;
2171 /* Send frames until the limit or some other event */
2172 for (cnt = 0; (cnt < maxframes) && data_ok(bus);) {
2174 __skb_queue_head_init(&pktq);
2176 pkt_num = min_t(u8, bus->tx_max - bus->tx_seq,
2177 brcmf_sdio_txglomsz);
2178 pkt_num = min_t(u32, pkt_num,
2179 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol));
2180 spin_lock_bh(&bus->txqlock);
2181 for (i = 0; i < pkt_num; i++) {
2182 pkt = brcmu_pktq_mdeq(&bus->txq, tx_prec_map,
2186 __skb_queue_tail(&pktq, pkt);
2188 spin_unlock_bh(&bus->txqlock);
2192 ret = brcmf_sdbrcm_txpkt(bus, &pktq, SDPCM_DATA_CHANNEL);
2195 /* In poll mode, need to check for other events */
2196 if (!bus->intr && cnt) {
2197 /* Check device status, signal pending interrupt */
2198 sdio_claim_host(bus->sdiodev->func[1]);
2199 ret = r_sdreg32(bus, &intstatus,
2200 offsetof(struct sdpcmd_regs,
2202 sdio_release_host(bus->sdiodev->func[1]);
2203 bus->sdcnt.f2txdata++;
2206 if (intstatus & bus->hostintmask)
2207 atomic_set(&bus->ipend, 1);
2211 /* Deflow-control stack if needed */
2212 if ((bus->sdiodev->bus_if->state == BRCMF_BUS_DATA) &&
2213 bus->txoff && (pktq_len(&bus->txq) < TXLOW)) {
2215 brcmf_txflowblock(bus->sdiodev->dev, false);
2221 static void brcmf_sdbrcm_bus_stop(struct device *dev)
2223 u32 local_hostintmask;
2226 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
2227 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
2228 struct brcmf_sdio *bus = sdiodev->bus;
2230 brcmf_dbg(TRACE, "Enter\n");
2232 if (bus->watchdog_tsk) {
2233 send_sig(SIGTERM, bus->watchdog_tsk, 1);
2234 kthread_stop(bus->watchdog_tsk);
2235 bus->watchdog_tsk = NULL;
2238 sdio_claim_host(bus->sdiodev->func[1]);
2240 /* Enable clock for device interrupts */
2241 brcmf_sdbrcm_bus_sleep(bus, false, false);
2243 /* Disable and clear interrupts at the chip level also */
2244 w_sdreg32(bus, 0, offsetof(struct sdpcmd_regs, hostintmask));
2245 local_hostintmask = bus->hostintmask;
2246 bus->hostintmask = 0;
2248 /* Change our idea of bus state */
2249 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
2251 /* Force clocks on backplane to be sure F2 interrupt propagates */
2252 saveclk = brcmf_sdiod_regrb(bus->sdiodev,
2253 SBSDIO_FUNC1_CHIPCLKCSR, &err);
2255 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
2256 (saveclk | SBSDIO_FORCE_HT), &err);
2259 brcmf_err("Failed to force clock for F2: err %d\n", err);
2261 /* Turn off the bus (F2), free any pending packets */
2262 brcmf_dbg(INTR, "disable SDIO interrupts\n");
2263 sdio_disable_func(bus->sdiodev->func[SDIO_FUNC_2]);
2265 /* Clear any pending interrupts now that F2 is disabled */
2266 w_sdreg32(bus, local_hostintmask,
2267 offsetof(struct sdpcmd_regs, intstatus));
2269 /* Turn off the backplane clock (only) */
2270 brcmf_sdbrcm_clkctl(bus, CLK_SDONLY, false);
2271 sdio_release_host(bus->sdiodev->func[1]);
2273 /* Clear the data packet queues */
2274 brcmu_pktq_flush(&bus->txq, true, NULL, NULL);
2276 /* Clear any held glomming stuff */
2278 brcmu_pkt_buf_free_skb(bus->glomd);
2279 brcmf_sdbrcm_free_glom(bus);
2281 /* Clear rx control and wake any waiters */
2282 spin_lock_bh(&bus->rxctl_lock);
2284 spin_unlock_bh(&bus->rxctl_lock);
2285 brcmf_sdbrcm_dcmd_resp_wake(bus);
2287 /* Reset some F2 state stuff */
2288 bus->rxskip = false;
2289 bus->tx_seq = bus->rx_seq = 0;
2292 static inline void brcmf_sdbrcm_clrintr(struct brcmf_sdio *bus)
2294 unsigned long flags;
2296 if (bus->sdiodev->oob_irq_requested) {
2297 spin_lock_irqsave(&bus->sdiodev->irq_en_lock, flags);
2298 if (!bus->sdiodev->irq_en && !atomic_read(&bus->ipend)) {
2299 enable_irq(bus->sdiodev->pdata->oob_irq_nr);
2300 bus->sdiodev->irq_en = true;
2302 spin_unlock_irqrestore(&bus->sdiodev->irq_en_lock, flags);
2306 static int brcmf_sdio_intr_rstatus(struct brcmf_sdio *bus)
2313 idx = brcmf_sdio_chip_getinfidx(bus->ci, BCMA_CORE_SDIO_DEV);
2314 addr = bus->ci->c_inf[idx].base +
2315 offsetof(struct sdpcmd_regs, intstatus);
2317 val = brcmf_sdiod_regrl(bus->sdiodev, addr, &ret);
2318 bus->sdcnt.f1regdata++;
2322 val &= bus->hostintmask;
2323 atomic_set(&bus->fcstate, !!(val & I_HMB_FC_STATE));
2325 /* Clear interrupts */
2327 brcmf_sdiod_regwl(bus->sdiodev, addr, val, &ret);
2328 bus->sdcnt.f1regdata++;
2332 atomic_set(&bus->intstatus, 0);
2334 for_each_set_bit(n, &val, 32)
2335 set_bit(n, (unsigned long *)&bus->intstatus.counter);
2341 static void brcmf_sdbrcm_dpc(struct brcmf_sdio *bus)
2344 unsigned long intstatus;
2345 uint rxlimit = bus->rxbound; /* Rx frames to read before resched */
2346 uint txlimit = bus->txbound; /* Tx frames to send before resched */
2347 uint framecnt = 0; /* Temporary counter of tx/rx frames */
2350 brcmf_dbg(TRACE, "Enter\n");
2352 sdio_claim_host(bus->sdiodev->func[1]);
2354 /* If waiting for HTAVAIL, check status */
2355 if (!bus->sr_enabled && bus->clkstate == CLK_PENDING) {
2356 u8 clkctl, devctl = 0;
2359 /* Check for inconsistent device control */
2360 devctl = brcmf_sdiod_regrb(bus->sdiodev,
2361 SBSDIO_DEVICE_CTL, &err);
2363 brcmf_err("error reading DEVCTL: %d\n", err);
2364 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
2368 /* Read CSR, if clock on switch to AVAIL, else ignore */
2369 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
2370 SBSDIO_FUNC1_CHIPCLKCSR, &err);
2372 brcmf_err("error reading CSR: %d\n",
2374 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
2377 brcmf_dbg(SDIO, "DPC: PENDING, devctl 0x%02x clkctl 0x%02x\n",
2380 if (SBSDIO_HTAV(clkctl)) {
2381 devctl = brcmf_sdiod_regrb(bus->sdiodev,
2382 SBSDIO_DEVICE_CTL, &err);
2384 brcmf_err("error reading DEVCTL: %d\n",
2386 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
2388 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
2389 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
2392 brcmf_err("error writing DEVCTL: %d\n",
2394 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
2396 bus->clkstate = CLK_AVAIL;
2400 /* Make sure backplane clock is on */
2401 brcmf_sdbrcm_bus_sleep(bus, false, true);
2403 /* Pending interrupt indicates new device status */
2404 if (atomic_read(&bus->ipend) > 0) {
2405 atomic_set(&bus->ipend, 0);
2406 err = brcmf_sdio_intr_rstatus(bus);
2409 /* Start with leftover status bits */
2410 intstatus = atomic_xchg(&bus->intstatus, 0);
2412 /* Handle flow-control change: read new state in case our ack
2413 * crossed another change interrupt. If change still set, assume
2414 * FC ON for safety, let next loop through do the debounce.
2416 if (intstatus & I_HMB_FC_CHANGE) {
2417 intstatus &= ~I_HMB_FC_CHANGE;
2418 err = w_sdreg32(bus, I_HMB_FC_CHANGE,
2419 offsetof(struct sdpcmd_regs, intstatus));
2421 err = r_sdreg32(bus, &newstatus,
2422 offsetof(struct sdpcmd_regs, intstatus));
2423 bus->sdcnt.f1regdata += 2;
2424 atomic_set(&bus->fcstate,
2425 !!(newstatus & (I_HMB_FC_STATE | I_HMB_FC_CHANGE)));
2426 intstatus |= (newstatus & bus->hostintmask);
2429 /* Handle host mailbox indication */
2430 if (intstatus & I_HMB_HOST_INT) {
2431 intstatus &= ~I_HMB_HOST_INT;
2432 intstatus |= brcmf_sdbrcm_hostmail(bus);
2435 sdio_release_host(bus->sdiodev->func[1]);
2437 /* Generally don't ask for these, can get CRC errors... */
2438 if (intstatus & I_WR_OOSYNC) {
2439 brcmf_err("Dongle reports WR_OOSYNC\n");
2440 intstatus &= ~I_WR_OOSYNC;
2443 if (intstatus & I_RD_OOSYNC) {
2444 brcmf_err("Dongle reports RD_OOSYNC\n");
2445 intstatus &= ~I_RD_OOSYNC;
2448 if (intstatus & I_SBINT) {
2449 brcmf_err("Dongle reports SBINT\n");
2450 intstatus &= ~I_SBINT;
2453 /* Would be active due to wake-wlan in gSPI */
2454 if (intstatus & I_CHIPACTIVE) {
2455 brcmf_dbg(INFO, "Dongle reports CHIPACTIVE\n");
2456 intstatus &= ~I_CHIPACTIVE;
2459 /* Ignore frame indications if rxskip is set */
2461 intstatus &= ~I_HMB_FRAME_IND;
2463 /* On frame indication, read available frames */
2464 if (PKT_AVAILABLE() && bus->clkstate == CLK_AVAIL) {
2465 framecnt = brcmf_sdio_readframes(bus, rxlimit);
2466 if (!bus->rxpending)
2467 intstatus &= ~I_HMB_FRAME_IND;
2468 rxlimit -= min(framecnt, rxlimit);
2471 /* Keep still-pending events for next scheduling */
2473 for_each_set_bit(n, &intstatus, 32)
2474 set_bit(n, (unsigned long *)&bus->intstatus.counter);
2477 brcmf_sdbrcm_clrintr(bus);
2479 if (data_ok(bus) && bus->ctrl_frame_stat &&
2480 (bus->clkstate == CLK_AVAIL)) {
2483 sdio_claim_host(bus->sdiodev->func[1]);
2484 err = brcmf_sdiod_send_buf(bus->sdiodev, bus->sdiodev->sbwad,
2485 SDIO_FUNC_2, F2SYNC,
2486 bus->ctrl_frame_buf,
2487 (u32)bus->ctrl_frame_len);
2490 /* On failure, abort the command and
2491 terminate the frame */
2492 brcmf_dbg(INFO, "sdio error %d, abort command and terminate frame\n",
2494 bus->sdcnt.tx_sderrs++;
2496 brcmf_sdiod_abort(bus->sdiodev, SDIO_FUNC_2);
2498 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL,
2500 bus->sdcnt.f1regdata++;
2502 for (i = 0; i < 3; i++) {
2504 hi = brcmf_sdiod_regrb(bus->sdiodev,
2505 SBSDIO_FUNC1_WFRAMEBCHI,
2507 lo = brcmf_sdiod_regrb(bus->sdiodev,
2508 SBSDIO_FUNC1_WFRAMEBCLO,
2510 bus->sdcnt.f1regdata += 2;
2511 if ((hi == 0) && (lo == 0))
2516 bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQ_WRAP;
2518 sdio_release_host(bus->sdiodev->func[1]);
2519 bus->ctrl_frame_stat = false;
2520 brcmf_sdbrcm_wait_event_wakeup(bus);
2522 /* Send queued frames (limit 1 if rx may still be pending) */
2523 else if ((bus->clkstate == CLK_AVAIL) && !atomic_read(&bus->fcstate) &&
2524 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) && txlimit
2526 framecnt = bus->rxpending ? min(txlimit, bus->txminmax) :
2528 framecnt = brcmf_sdbrcm_sendfromq(bus, framecnt);
2529 txlimit -= framecnt;
2532 if ((bus->sdiodev->bus_if->state == BRCMF_BUS_DOWN) || (err != 0)) {
2533 brcmf_err("failed backplane access over SDIO, halting operation\n");
2534 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
2535 atomic_set(&bus->intstatus, 0);
2536 } else if (atomic_read(&bus->intstatus) ||
2537 atomic_read(&bus->ipend) > 0 ||
2538 (!atomic_read(&bus->fcstate) &&
2539 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) &&
2540 data_ok(bus)) || PKT_AVAILABLE()) {
2541 atomic_inc(&bus->dpc_tskcnt);
2544 /* If we're done for now, turn off clock request. */
2545 if ((bus->clkstate != CLK_PENDING)
2546 && bus->idletime == BRCMF_IDLE_IMMEDIATE) {
2547 bus->activity = false;
2548 brcmf_dbg(SDIO, "idle state\n");
2549 sdio_claim_host(bus->sdiodev->func[1]);
2550 brcmf_sdbrcm_bus_sleep(bus, true, false);
2551 sdio_release_host(bus->sdiodev->func[1]);
2555 static struct pktq *brcmf_sdbrcm_bus_gettxq(struct device *dev)
2557 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
2558 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
2559 struct brcmf_sdio *bus = sdiodev->bus;
2564 static int brcmf_sdbrcm_bus_txdata(struct device *dev, struct sk_buff *pkt)
2568 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
2569 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
2570 struct brcmf_sdio *bus = sdiodev->bus;
2573 brcmf_dbg(TRACE, "Enter\n");
2577 /* Add space for the header */
2578 skb_push(pkt, bus->tx_hdrlen);
2579 /* precondition: IS_ALIGNED((unsigned long)(pkt->data), 2) */
2581 prec = prio2prec((pkt->priority & PRIOMASK));
2583 /* Check for existing queue, current flow-control,
2584 pending event, or pending clock */
2585 brcmf_dbg(TRACE, "deferring pktq len %d\n", pktq_len(&bus->txq));
2586 bus->sdcnt.fcqueued++;
2588 /* Priority based enq */
2589 spin_lock_irqsave(&bus->txqlock, flags);
2590 if (!brcmf_c_prec_enq(bus->sdiodev->dev, &bus->txq, pkt, prec)) {
2591 skb_pull(pkt, bus->tx_hdrlen);
2592 brcmf_err("out of bus->txq !!!\n");
2598 if (pktq_len(&bus->txq) >= TXHI) {
2600 brcmf_txflowblock(bus->sdiodev->dev, true);
2602 spin_unlock_irqrestore(&bus->txqlock, flags);
2605 if (pktq_plen(&bus->txq, prec) > qcount[prec])
2606 qcount[prec] = pktq_plen(&bus->txq, prec);
2609 if (atomic_read(&bus->dpc_tskcnt) == 0) {
2610 atomic_inc(&bus->dpc_tskcnt);
2611 queue_work(bus->brcmf_wq, &bus->datawork);
2618 #define CONSOLE_LINE_MAX 192
2620 static int brcmf_sdbrcm_readconsole(struct brcmf_sdio *bus)
2622 struct brcmf_console *c = &bus->console;
2623 u8 line[CONSOLE_LINE_MAX], ch;
2627 /* Don't do anything until FWREADY updates console address */
2628 if (bus->console_addr == 0)
2631 /* Read console log struct */
2632 addr = bus->console_addr + offsetof(struct rte_console, log_le);
2633 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, (u8 *)&c->log_le,
2638 /* Allocate console buffer (one time only) */
2639 if (c->buf == NULL) {
2640 c->bufsize = le32_to_cpu(c->log_le.buf_size);
2641 c->buf = kmalloc(c->bufsize, GFP_ATOMIC);
2646 idx = le32_to_cpu(c->log_le.idx);
2648 /* Protect against corrupt value */
2649 if (idx > c->bufsize)
2652 /* Skip reading the console buffer if the index pointer
2657 /* Read the console buffer */
2658 addr = le32_to_cpu(c->log_le.buf);
2659 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, c->buf, c->bufsize);
2663 while (c->last != idx) {
2664 for (n = 0; n < CONSOLE_LINE_MAX - 2; n++) {
2665 if (c->last == idx) {
2666 /* This would output a partial line.
2668 * the buffer pointer and output this
2669 * line next time around.
2674 c->last = c->bufsize - n;
2677 ch = c->buf[c->last];
2678 c->last = (c->last + 1) % c->bufsize;
2685 if (line[n - 1] == '\r')
2688 pr_debug("CONSOLE: %s\n", line);
2697 static int brcmf_tx_frame(struct brcmf_sdio *bus, u8 *frame, u16 len)
2702 bus->ctrl_frame_stat = false;
2703 ret = brcmf_sdiod_send_buf(bus->sdiodev, bus->sdiodev->sbwad,
2704 SDIO_FUNC_2, F2SYNC, frame, len);
2707 /* On failure, abort the command and terminate the frame */
2708 brcmf_dbg(INFO, "sdio error %d, abort command and terminate frame\n",
2710 bus->sdcnt.tx_sderrs++;
2712 brcmf_sdiod_abort(bus->sdiodev, SDIO_FUNC_2);
2714 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL,
2716 bus->sdcnt.f1regdata++;
2718 for (i = 0; i < 3; i++) {
2720 hi = brcmf_sdiod_regrb(bus->sdiodev,
2721 SBSDIO_FUNC1_WFRAMEBCHI, NULL);
2722 lo = brcmf_sdiod_regrb(bus->sdiodev,
2723 SBSDIO_FUNC1_WFRAMEBCLO, NULL);
2724 bus->sdcnt.f1regdata += 2;
2725 if (hi == 0 && lo == 0)
2731 bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQ_WRAP;
2737 brcmf_sdbrcm_bus_txctl(struct device *dev, unsigned char *msg, uint msglen)
2744 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
2745 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
2746 struct brcmf_sdio *bus = sdiodev->bus;
2747 struct brcmf_sdio_hdrinfo hd_info = {0};
2749 brcmf_dbg(TRACE, "Enter\n");
2751 /* Back the pointer to make a room for bus header */
2752 frame = msg - bus->tx_hdrlen;
2753 len = (msglen += bus->tx_hdrlen);
2755 /* Add alignment padding (optional for ctl frames) */
2756 doff = ((unsigned long)frame % bus->head_align);
2761 memset(frame, 0, doff + bus->tx_hdrlen);
2763 /* precondition: doff < bus->head_align */
2764 doff += bus->tx_hdrlen;
2766 /* Round send length to next SDIO block */
2768 if (bus->roundup && bus->blocksize && (len > bus->blocksize)) {
2769 pad = bus->blocksize - (len % bus->blocksize);
2770 if ((pad > bus->roundup) || (pad >= bus->blocksize))
2772 } else if (len % bus->head_align) {
2773 pad = bus->head_align - (len % bus->head_align);
2777 /* precondition: IS_ALIGNED((unsigned long)frame, 2) */
2779 /* Make sure backplane clock is on */
2780 sdio_claim_host(bus->sdiodev->func[1]);
2781 brcmf_sdbrcm_bus_sleep(bus, false, false);
2782 sdio_release_host(bus->sdiodev->func[1]);
2784 hd_info.len = (u16)msglen;
2785 hd_info.channel = SDPCM_CONTROL_CHANNEL;
2786 hd_info.dat_offset = doff;
2787 hd_info.seq_num = bus->tx_seq;
2788 hd_info.lastfrm = true;
2789 hd_info.tail_pad = pad;
2790 brcmf_sdio_hdpack(bus, frame, &hd_info);
2793 brcmf_sdio_update_hwhdr(frame, len);
2795 if (!data_ok(bus)) {
2796 brcmf_dbg(INFO, "No bus credit bus->tx_max %d, bus->tx_seq %d\n",
2797 bus->tx_max, bus->tx_seq);
2798 bus->ctrl_frame_stat = true;
2800 bus->ctrl_frame_buf = frame;
2801 bus->ctrl_frame_len = len;
2803 wait_event_interruptible_timeout(bus->ctrl_wait,
2804 !bus->ctrl_frame_stat,
2805 msecs_to_jiffies(2000));
2807 if (!bus->ctrl_frame_stat) {
2808 brcmf_dbg(SDIO, "ctrl_frame_stat == false\n");
2811 brcmf_dbg(SDIO, "ctrl_frame_stat == true\n");
2817 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_CTL_ON(),
2818 frame, len, "Tx Frame:\n");
2819 brcmf_dbg_hex_dump(!(BRCMF_BYTES_ON() && BRCMF_CTL_ON()) &&
2821 frame, min_t(u16, len, 16), "TxHdr:\n");
2824 sdio_claim_host(bus->sdiodev->func[1]);
2825 ret = brcmf_tx_frame(bus, frame, len);
2826 sdio_release_host(bus->sdiodev->func[1]);
2827 } while (ret < 0 && retries++ < TXRETRIES);
2830 if ((bus->idletime == BRCMF_IDLE_IMMEDIATE) &&
2831 atomic_read(&bus->dpc_tskcnt) == 0) {
2832 bus->activity = false;
2833 sdio_claim_host(bus->sdiodev->func[1]);
2834 brcmf_dbg(INFO, "idle\n");
2835 brcmf_sdbrcm_clkctl(bus, CLK_NONE, true);
2836 sdio_release_host(bus->sdiodev->func[1]);
2840 bus->sdcnt.tx_ctlerrs++;
2842 bus->sdcnt.tx_ctlpkts++;
2844 return ret ? -EIO : 0;
2848 static inline bool brcmf_sdio_valid_shared_address(u32 addr)
2850 return !(addr == 0 || ((~addr >> 16) & 0xffff) == (addr & 0xffff));
2853 static int brcmf_sdio_readshared(struct brcmf_sdio *bus,
2854 struct sdpcm_shared *sh)
2859 struct sdpcm_shared_le sh_le;
2862 shaddr = bus->ci->rambase + bus->ramsize - 4;
2865 * Read last word in socram to determine
2866 * address of sdpcm_shared structure
2868 sdio_claim_host(bus->sdiodev->func[1]);
2869 brcmf_sdbrcm_bus_sleep(bus, false, false);
2870 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, shaddr, (u8 *)&addr_le, 4);
2871 sdio_release_host(bus->sdiodev->func[1]);
2875 addr = le32_to_cpu(addr_le);
2877 brcmf_dbg(SDIO, "sdpcm_shared address 0x%08X\n", addr);
2880 * Check if addr is valid.
2881 * NVRAM length at the end of memory should have been overwritten.
2883 if (!brcmf_sdio_valid_shared_address(addr)) {
2884 brcmf_err("invalid sdpcm_shared address 0x%08X\n",
2889 /* Read hndrte_shared structure */
2890 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, (u8 *)&sh_le,
2891 sizeof(struct sdpcm_shared_le));
2896 sh->flags = le32_to_cpu(sh_le.flags);
2897 sh->trap_addr = le32_to_cpu(sh_le.trap_addr);
2898 sh->assert_exp_addr = le32_to_cpu(sh_le.assert_exp_addr);
2899 sh->assert_file_addr = le32_to_cpu(sh_le.assert_file_addr);
2900 sh->assert_line = le32_to_cpu(sh_le.assert_line);
2901 sh->console_addr = le32_to_cpu(sh_le.console_addr);
2902 sh->msgtrace_addr = le32_to_cpu(sh_le.msgtrace_addr);
2904 if ((sh->flags & SDPCM_SHARED_VERSION_MASK) > SDPCM_SHARED_VERSION) {
2905 brcmf_err("sdpcm shared version unsupported: dhd %d dongle %d\n",
2906 SDPCM_SHARED_VERSION,
2907 sh->flags & SDPCM_SHARED_VERSION_MASK);
2914 static int brcmf_sdio_dump_console(struct brcmf_sdio *bus,
2915 struct sdpcm_shared *sh, char __user *data,
2918 u32 addr, console_ptr, console_size, console_index;
2919 char *conbuf = NULL;
2925 /* obtain console information from device memory */
2926 addr = sh->console_addr + offsetof(struct rte_console, log_le);
2927 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
2928 (u8 *)&sh_val, sizeof(u32));
2931 console_ptr = le32_to_cpu(sh_val);
2933 addr = sh->console_addr + offsetof(struct rte_console, log_le.buf_size);
2934 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
2935 (u8 *)&sh_val, sizeof(u32));
2938 console_size = le32_to_cpu(sh_val);
2940 addr = sh->console_addr + offsetof(struct rte_console, log_le.idx);
2941 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
2942 (u8 *)&sh_val, sizeof(u32));
2945 console_index = le32_to_cpu(sh_val);
2947 /* allocate buffer for console data */
2948 if (console_size <= CONSOLE_BUFFER_MAX)
2949 conbuf = vzalloc(console_size+1);
2954 /* obtain the console data from device */
2955 conbuf[console_size] = '\0';
2956 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, console_ptr, (u8 *)conbuf,
2961 rv = simple_read_from_buffer(data, count, &pos,
2962 conbuf + console_index,
2963 console_size - console_index);
2968 if (console_index > 0) {
2970 rv = simple_read_from_buffer(data+nbytes, count, &pos,
2971 conbuf, console_index - 1);
2981 static int brcmf_sdio_trap_info(struct brcmf_sdio *bus, struct sdpcm_shared *sh,
2982 char __user *data, size_t count)
2986 struct brcmf_trap_info tr;
2989 if ((sh->flags & SDPCM_SHARED_TRAP) == 0) {
2990 brcmf_dbg(INFO, "no trap in firmware\n");
2994 error = brcmf_sdiod_ramrw(bus->sdiodev, false, sh->trap_addr, (u8 *)&tr,
2995 sizeof(struct brcmf_trap_info));
2999 res = scnprintf(buf, sizeof(buf),
3000 "dongle trap info: type 0x%x @ epc 0x%08x\n"
3001 " cpsr 0x%08x spsr 0x%08x sp 0x%08x\n"
3002 " lr 0x%08x pc 0x%08x offset 0x%x\n"
3003 " r0 0x%08x r1 0x%08x r2 0x%08x r3 0x%08x\n"
3004 " r4 0x%08x r5 0x%08x r6 0x%08x r7 0x%08x\n",
3005 le32_to_cpu(tr.type), le32_to_cpu(tr.epc),
3006 le32_to_cpu(tr.cpsr), le32_to_cpu(tr.spsr),
3007 le32_to_cpu(tr.r13), le32_to_cpu(tr.r14),
3008 le32_to_cpu(tr.pc), sh->trap_addr,
3009 le32_to_cpu(tr.r0), le32_to_cpu(tr.r1),
3010 le32_to_cpu(tr.r2), le32_to_cpu(tr.r3),
3011 le32_to_cpu(tr.r4), le32_to_cpu(tr.r5),
3012 le32_to_cpu(tr.r6), le32_to_cpu(tr.r7));
3014 return simple_read_from_buffer(data, count, &pos, buf, res);
3017 static int brcmf_sdio_assert_info(struct brcmf_sdio *bus,
3018 struct sdpcm_shared *sh, char __user *data,
3023 char file[80] = "?";
3024 char expr[80] = "<???>";
3028 if ((sh->flags & SDPCM_SHARED_ASSERT_BUILT) == 0) {
3029 brcmf_dbg(INFO, "firmware not built with -assert\n");
3031 } else if ((sh->flags & SDPCM_SHARED_ASSERT) == 0) {
3032 brcmf_dbg(INFO, "no assert in dongle\n");
3036 sdio_claim_host(bus->sdiodev->func[1]);
3037 if (sh->assert_file_addr != 0) {
3038 error = brcmf_sdiod_ramrw(bus->sdiodev, false,
3039 sh->assert_file_addr, (u8 *)file, 80);
3043 if (sh->assert_exp_addr != 0) {
3044 error = brcmf_sdiod_ramrw(bus->sdiodev, false,
3045 sh->assert_exp_addr, (u8 *)expr, 80);
3049 sdio_release_host(bus->sdiodev->func[1]);
3051 res = scnprintf(buf, sizeof(buf),
3052 "dongle assert: %s:%d: assert(%s)\n",
3053 file, sh->assert_line, expr);
3054 return simple_read_from_buffer(data, count, &pos, buf, res);
3057 static int brcmf_sdbrcm_checkdied(struct brcmf_sdio *bus)
3060 struct sdpcm_shared sh;
3062 error = brcmf_sdio_readshared(bus, &sh);
3067 if ((sh.flags & SDPCM_SHARED_ASSERT_BUILT) == 0)
3068 brcmf_dbg(INFO, "firmware not built with -assert\n");
3069 else if (sh.flags & SDPCM_SHARED_ASSERT)
3070 brcmf_err("assertion in dongle\n");
3072 if (sh.flags & SDPCM_SHARED_TRAP)
3073 brcmf_err("firmware trap in dongle\n");
3078 static int brcmf_sdbrcm_died_dump(struct brcmf_sdio *bus, char __user *data,
3079 size_t count, loff_t *ppos)
3082 struct sdpcm_shared sh;
3089 error = brcmf_sdio_readshared(bus, &sh);
3093 error = brcmf_sdio_assert_info(bus, &sh, data, count);
3098 error = brcmf_sdio_trap_info(bus, &sh, data+nbytes, count);
3103 error = brcmf_sdio_dump_console(bus, &sh, data+nbytes, count);
3114 static ssize_t brcmf_sdio_forensic_read(struct file *f, char __user *data,
3115 size_t count, loff_t *ppos)
3117 struct brcmf_sdio *bus = f->private_data;
3120 res = brcmf_sdbrcm_died_dump(bus, data, count, ppos);
3123 return (ssize_t)res;
3126 static const struct file_operations brcmf_sdio_forensic_ops = {
3127 .owner = THIS_MODULE,
3128 .open = simple_open,
3129 .read = brcmf_sdio_forensic_read
3132 static void brcmf_sdio_debugfs_create(struct brcmf_sdio *bus)
3134 struct brcmf_pub *drvr = bus->sdiodev->bus_if->drvr;
3135 struct dentry *dentry = brcmf_debugfs_get_devdir(drvr);
3137 if (IS_ERR_OR_NULL(dentry))
3140 debugfs_create_file("forensics", S_IRUGO, dentry, bus,
3141 &brcmf_sdio_forensic_ops);
3142 brcmf_debugfs_create_sdio_count(drvr, &bus->sdcnt);
3145 static int brcmf_sdbrcm_checkdied(struct brcmf_sdio *bus)
3150 static void brcmf_sdio_debugfs_create(struct brcmf_sdio *bus)
3156 brcmf_sdbrcm_bus_rxctl(struct device *dev, unsigned char *msg, uint msglen)
3162 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3163 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3164 struct brcmf_sdio *bus = sdiodev->bus;
3166 brcmf_dbg(TRACE, "Enter\n");
3168 /* Wait until control frame is available */
3169 timeleft = brcmf_sdbrcm_dcmd_resp_wait(bus, &bus->rxlen, &pending);
3171 spin_lock_bh(&bus->rxctl_lock);
3173 memcpy(msg, bus->rxctl, min(msglen, rxlen));
3175 buf = bus->rxctl_orig;
3176 bus->rxctl_orig = NULL;
3178 spin_unlock_bh(&bus->rxctl_lock);
3182 brcmf_dbg(CTL, "resumed on rxctl frame, got %d expected %d\n",
3184 } else if (timeleft == 0) {
3185 brcmf_err("resumed on timeout\n");
3186 brcmf_sdbrcm_checkdied(bus);
3187 } else if (pending) {
3188 brcmf_dbg(CTL, "cancelled\n");
3189 return -ERESTARTSYS;
3191 brcmf_dbg(CTL, "resumed for unknown reason?\n");
3192 brcmf_sdbrcm_checkdied(bus);
3196 bus->sdcnt.rx_ctlpkts++;
3198 bus->sdcnt.rx_ctlerrs++;
3200 return rxlen ? (int)rxlen : -ETIMEDOUT;
3203 static bool brcmf_sdbrcm_download_state(struct brcmf_sdio *bus, bool enter)
3205 struct chip_info *ci = bus->ci;
3207 /* To enter download state, disable ARM and reset SOCRAM.
3208 * To exit download state, simply reset ARM (default is RAM boot).
3211 bus->alp_only = true;
3213 brcmf_sdio_chip_enter_download(bus->sdiodev, ci);
3215 if (!brcmf_sdio_chip_exit_download(bus->sdiodev, ci, bus->vars,
3219 /* Allow HT Clock now that the ARM is running. */
3220 bus->alp_only = false;
3222 bus->sdiodev->bus_if->state = BRCMF_BUS_LOAD;
3228 static int brcmf_sdbrcm_download_code_file(struct brcmf_sdio *bus)
3230 const struct firmware *fw;
3236 fw = brcmf_sdbrcm_get_fw(bus, BRCMF_FIRMWARE_BIN);
3240 if (brcmf_sdio_chip_getinfidx(bus->ci, BCMA_CORE_ARM_CR4) !=
3242 memcpy(&bus->ci->rst_vec, fw->data, sizeof(bus->ci->rst_vec));
3246 address = bus->ci->rambase;
3247 while (offset < fw->size) {
3248 len = ((offset + MEMBLOCK) < fw->size) ? MEMBLOCK :
3250 err = brcmf_sdiod_ramrw(bus->sdiodev, true, address,
3251 (u8 *)&fw->data[offset], len);
3253 brcmf_err("error %d on writing %d membytes at 0x%08x\n",
3262 release_firmware(fw);
3268 * ProcessVars:Takes a buffer of "<var>=<value>\n" lines read from a file
3269 * and ending in a NUL.
3270 * Removes carriage returns, empty lines, comment lines, and converts
3272 * Shortens buffer as needed and pads with NULs. End of buffer is marked
3276 static int brcmf_process_nvram_vars(struct brcmf_sdio *bus,
3277 const struct firmware *nv)
3284 uint buf_len, n, len;
3287 varbuf = vmalloc(len);
3291 memcpy(varbuf, nv->data, len);
3294 findNewline = false;
3297 for (n = 0; n < len; n++) {
3300 if (varbuf[n] == '\r')
3302 if (findNewline && varbuf[n] != '\n')
3304 findNewline = false;
3305 if (varbuf[n] == '#') {
3309 if (varbuf[n] == '\n') {
3319 buf_len = dp - varbuf;
3320 while (dp < varbuf + n)
3324 /* roundup needed for download to device */
3325 bus->varsz = roundup(buf_len + 1, 4);
3326 bus->vars = kmalloc(bus->varsz, GFP_KERNEL);
3327 if (bus->vars == NULL) {
3333 /* copy the processed variables and add null termination */
3334 memcpy(bus->vars, varbuf, buf_len);
3335 bus->vars[buf_len] = 0;
3341 static int brcmf_sdbrcm_download_nvram(struct brcmf_sdio *bus)
3343 const struct firmware *nv;
3346 nv = brcmf_sdbrcm_get_fw(bus, BRCMF_FIRMWARE_NVRAM);
3350 ret = brcmf_process_nvram_vars(bus, nv);
3352 release_firmware(nv);
3357 static int _brcmf_sdbrcm_download_firmware(struct brcmf_sdio *bus)
3361 /* Keep arm in reset */
3362 if (!brcmf_sdbrcm_download_state(bus, true)) {
3363 brcmf_err("error placing ARM core in reset\n");
3367 if (brcmf_sdbrcm_download_code_file(bus)) {
3368 brcmf_err("dongle image file download failed\n");
3372 if (brcmf_sdbrcm_download_nvram(bus)) {
3373 brcmf_err("dongle nvram file download failed\n");
3377 /* Take arm out of reset */
3378 if (!brcmf_sdbrcm_download_state(bus, false)) {
3379 brcmf_err("error getting out of ARM core reset\n");
3389 static bool brcmf_sdbrcm_sr_capable(struct brcmf_sdio *bus)
3393 brcmf_dbg(TRACE, "Enter\n");
3395 /* old chips with PMU version less than 17 don't support save restore */
3396 if (bus->ci->pmurev < 17)
3399 /* read PMU chipcontrol register 3*/
3400 addr = CORE_CC_REG(bus->ci->c_inf[0].base, chipcontrol_addr);
3401 brcmf_sdiod_regwl(bus->sdiodev, addr, 3, NULL);
3402 addr = CORE_CC_REG(bus->ci->c_inf[0].base, chipcontrol_data);
3403 reg = brcmf_sdiod_regrl(bus->sdiodev, addr, NULL);
3408 static void brcmf_sdbrcm_sr_init(struct brcmf_sdio *bus)
3413 brcmf_dbg(TRACE, "Enter\n");
3415 val = brcmf_sdiod_regrb(bus->sdiodev, SBSDIO_FUNC1_WAKEUPCTRL, &err);
3417 brcmf_err("error reading SBSDIO_FUNC1_WAKEUPCTRL\n");
3421 val |= 1 << SBSDIO_FUNC1_WCTRL_HTWAIT_SHIFT;
3422 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_WAKEUPCTRL, val, &err);
3424 brcmf_err("error writing SBSDIO_FUNC1_WAKEUPCTRL\n");
3428 /* Add CMD14 Support */
3429 brcmf_sdiod_regwb(bus->sdiodev, SDIO_CCCR_BRCM_CARDCAP,
3430 (SDIO_CCCR_BRCM_CARDCAP_CMD14_SUPPORT |
3431 SDIO_CCCR_BRCM_CARDCAP_CMD14_EXT),
3434 brcmf_err("error writing SDIO_CCCR_BRCM_CARDCAP\n");
3438 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3439 SBSDIO_FORCE_HT, &err);
3441 brcmf_err("error writing SBSDIO_FUNC1_CHIPCLKCSR\n");
3446 bus->sr_enabled = true;
3447 brcmf_dbg(INFO, "SR enabled\n");
3450 /* enable KSO bit */
3451 static int brcmf_sdbrcm_kso_init(struct brcmf_sdio *bus)
3456 brcmf_dbg(TRACE, "Enter\n");
3458 /* KSO bit added in SDIO core rev 12 */
3459 if (bus->ci->c_inf[1].rev < 12)
3462 val = brcmf_sdiod_regrb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR, &err);
3464 brcmf_err("error reading SBSDIO_FUNC1_SLEEPCSR\n");
3468 if (!(val & SBSDIO_FUNC1_SLEEPCSR_KSO_MASK)) {
3469 val |= (SBSDIO_FUNC1_SLEEPCSR_KSO_EN <<
3470 SBSDIO_FUNC1_SLEEPCSR_KSO_SHIFT);
3471 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
3474 brcmf_err("error writing SBSDIO_FUNC1_SLEEPCSR\n");
3484 brcmf_sdbrcm_download_firmware(struct brcmf_sdio *bus)
3488 sdio_claim_host(bus->sdiodev->func[1]);
3490 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
3492 ret = _brcmf_sdbrcm_download_firmware(bus) == 0;
3494 brcmf_sdbrcm_clkctl(bus, CLK_SDONLY, false);
3496 sdio_release_host(bus->sdiodev->func[1]);
3501 static int brcmf_sdbrcm_bus_preinit(struct device *dev)
3503 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3504 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3505 struct brcmf_sdio *bus = sdiodev->bus;
3511 /* the commands below use the terms tx and rx from
3512 * a device perspective, ie. bus:txglom affects the
3513 * bus transfers from device to host.
3515 idx = brcmf_sdio_chip_getinfidx(bus->ci, BCMA_CORE_SDIO_DEV);
3516 if (bus->ci->c_inf[idx].rev < 12) {
3517 /* for sdio core rev < 12, disable txgloming */
3519 err = brcmf_iovar_data_set(dev, "bus:txglom", &value,
3522 /* otherwise, set txglomalign */
3525 value = sdiodev->pdata->sd_sgentry_align;
3526 /* SDIO ADMA requires at least 32 bit alignment */
3527 value = max_t(u32, value, 4);
3528 err = brcmf_iovar_data_set(dev, "bus:txglomalign", &value,
3535 bus->tx_hdrlen = SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN;
3536 if (sdiodev->sg_support) {
3537 bus->txglom = false;
3539 pad_size = bus->sdiodev->func[2]->cur_blksize << 1;
3540 bus->txglom_sgpad = brcmu_pkt_buf_get_skb(pad_size);
3541 if (!bus->txglom_sgpad)
3542 brcmf_err("allocating txglom padding skb failed, reduced performance\n");
3544 err = brcmf_iovar_data_set(bus->sdiodev->dev, "bus:rxglom",
3545 &value, sizeof(u32));
3547 /* bus:rxglom is allowed to fail */
3551 bus->tx_hdrlen += SDPCM_HWEXT_LEN;
3554 brcmf_bus_add_txhdrlen(bus->sdiodev->dev, bus->tx_hdrlen);
3560 static int brcmf_sdbrcm_bus_init(struct device *dev)
3562 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3563 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3564 struct brcmf_sdio *bus = sdiodev->bus;
3568 brcmf_dbg(TRACE, "Enter\n");
3570 /* try to download image and nvram to the dongle */
3571 if (bus_if->state == BRCMF_BUS_DOWN) {
3572 if (!(brcmf_sdbrcm_download_firmware(bus)))
3576 if (!bus->sdiodev->bus_if->drvr)
3579 /* Start the watchdog timer */
3580 bus->sdcnt.tickcnt = 0;
3581 brcmf_sdbrcm_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_sdbrcm_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_sdbrcm_sr_capable(bus)) {
3625 brcmf_sdbrcm_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 */
3639 if (bus_if->state != BRCMF_BUS_DATA)
3640 brcmf_sdbrcm_clkctl(bus, CLK_NONE, false);
3643 sdio_release_host(bus->sdiodev->func[1]);
3648 void brcmf_sdbrcm_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_sdbrcm_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_sdbrcm_bus_sleep(bus, false, false);
3736 if (brcmf_sdbrcm_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_sdbrcm_wd_timer(bus, BRCMF_WD_POLL_MS);
3752 brcmf_dbg(SDIO, "idle\n");
3753 sdio_claim_host(bus->sdiodev->func[1]);
3754 brcmf_sdbrcm_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_sdbrcm_dpc(bus);
3770 atomic_dec(&bus->dpc_tskcnt);
3774 static void brcmf_sdbrcm_release_malloc(struct brcmf_sdio *bus)
3776 brcmf_dbg(TRACE, "Enter\n");
3779 bus->rxctl = bus->rxbuf = NULL;
3783 static bool brcmf_sdbrcm_probe_malloc(struct brcmf_sdio *bus)
3785 brcmf_dbg(TRACE, "Enter\n");
3787 if (bus->sdiodev->bus_if->maxctl) {
3789 roundup((bus->sdiodev->bus_if->maxctl + SDPCM_HDRLEN),
3790 ALIGNMENT) + bus->head_align;
3791 bus->rxbuf = kmalloc(bus->rxblen, GFP_ATOMIC);
3800 brcmf_sdbrcm_probe_attach(struct brcmf_sdio *bus)
3808 bus->alp_only = true;
3810 sdio_claim_host(bus->sdiodev->func[1]);
3812 pr_debug("F1 signature read @0x18000000=0x%4x\n",
3813 brcmf_sdiod_regrl(bus->sdiodev, SI_ENUM_BASE, NULL));
3816 * Force PLL off until brcmf_sdio_chip_attach()
3817 * programs PLL control regs
3820 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3821 BRCMF_INIT_CLKCTL1, &err);
3823 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
3824 SBSDIO_FUNC1_CHIPCLKCSR, &err);
3826 if (err || ((clkctl & ~SBSDIO_AVBITS) != BRCMF_INIT_CLKCTL1)) {
3827 brcmf_err("ChipClkCSR access: err %d wrote 0x%02x read 0x%02x\n",
3828 err, BRCMF_INIT_CLKCTL1, clkctl);
3832 if (brcmf_sdio_chip_attach(bus->sdiodev, &bus->ci)) {
3833 brcmf_err("brcmf_sdio_chip_attach failed!\n");
3837 if (brcmf_sdbrcm_kso_init(bus)) {
3838 brcmf_err("error enabling KSO\n");
3842 if ((bus->sdiodev->pdata) && (bus->sdiodev->pdata->drive_strength))
3843 drivestrength = bus->sdiodev->pdata->drive_strength;
3845 drivestrength = DEFAULT_SDIO_DRIVE_STRENGTH;
3846 brcmf_sdio_chip_drivestrengthinit(bus->sdiodev, bus->ci, drivestrength);
3848 /* Get info on the SOCRAM cores... */
3849 bus->ramsize = bus->ci->ramsize;
3850 if (!(bus->ramsize)) {
3851 brcmf_err("failed to find SOCRAM memory!\n");
3855 /* Set card control so an SDIO card reset does a WLAN backplane reset */
3856 reg_val = brcmf_sdiod_regrb(bus->sdiodev,
3857 SDIO_CCCR_BRCM_CARDCTRL, &err);
3861 reg_val |= SDIO_CCCR_BRCM_CARDCTRL_WLANRESET;
3863 brcmf_sdiod_regwb(bus->sdiodev,
3864 SDIO_CCCR_BRCM_CARDCTRL, reg_val, &err);
3868 /* set PMUControl so a backplane reset does PMU state reload */
3869 reg_addr = CORE_CC_REG(bus->ci->c_inf[0].base,
3871 reg_val = brcmf_sdiod_regrl(bus->sdiodev,
3877 reg_val |= (BCMA_CC_PMU_CTL_RES_RELOAD << BCMA_CC_PMU_CTL_RES_SHIFT);
3879 brcmf_sdiod_regwl(bus->sdiodev,
3887 sdio_release_host(bus->sdiodev->func[1]);
3889 brcmu_pktq_init(&bus->txq, (PRIOMASK + 1), TXQLEN);
3891 /* allocate header buffer */
3892 bus->hdrbuf = kzalloc(MAX_HDR_READ + bus->head_align, GFP_KERNEL);
3895 /* Locate an appropriately-aligned portion of hdrbuf */
3896 bus->rxhdr = (u8 *) roundup((unsigned long)&bus->hdrbuf[0],
3899 /* Set the poll and/or interrupt flags */
3908 sdio_release_host(bus->sdiodev->func[1]);
3912 static bool brcmf_sdbrcm_probe_init(struct brcmf_sdio *bus)
3914 brcmf_dbg(TRACE, "Enter\n");
3916 sdio_claim_host(bus->sdiodev->func[1]);
3918 /* Disable F2 to clear any intermediate frame state on the dongle */
3919 sdio_disable_func(bus->sdiodev->func[SDIO_FUNC_2]);
3921 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
3922 bus->rxflow = false;
3924 /* Done with backplane-dependent accesses, can drop clock... */
3925 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, 0, NULL);
3927 sdio_release_host(bus->sdiodev->func[1]);
3929 /* ...and initialize clock/power states */
3930 bus->clkstate = CLK_SDONLY;
3931 bus->idletime = BRCMF_IDLE_INTERVAL;
3932 bus->idleclock = BRCMF_IDLE_ACTIVE;
3934 /* Query the F2 block size, set roundup accordingly */
3935 bus->blocksize = bus->sdiodev->func[2]->cur_blksize;
3936 bus->roundup = min(max_roundup, bus->blocksize);
3939 bus->sleeping = false;
3940 bus->sr_enabled = false;
3946 brcmf_sdbrcm_watchdog_thread(void *data)
3948 struct brcmf_sdio *bus = (struct brcmf_sdio *)data;
3950 allow_signal(SIGTERM);
3951 /* Run until signal received */
3953 if (kthread_should_stop())
3955 if (!wait_for_completion_interruptible(&bus->watchdog_wait)) {
3956 brcmf_sdbrcm_bus_watchdog(bus);
3957 /* Count the tick for reference */
3958 bus->sdcnt.tickcnt++;
3966 brcmf_sdbrcm_watchdog(unsigned long data)
3968 struct brcmf_sdio *bus = (struct brcmf_sdio *)data;
3970 if (bus->watchdog_tsk) {
3971 complete(&bus->watchdog_wait);
3972 /* Reschedule the watchdog */
3973 if (bus->wd_timer_valid)
3974 mod_timer(&bus->timer,
3975 jiffies + BRCMF_WD_POLL_MS * HZ / 1000);
3979 static void brcmf_sdbrcm_release_dongle(struct brcmf_sdio *bus)
3981 brcmf_dbg(TRACE, "Enter\n");
3984 sdio_claim_host(bus->sdiodev->func[1]);
3985 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
3986 brcmf_sdbrcm_clkctl(bus, CLK_NONE, false);
3987 sdio_release_host(bus->sdiodev->func[1]);
3988 brcmf_sdio_chip_detach(&bus->ci);
3989 if (bus->vars && bus->varsz)
3994 brcmf_dbg(TRACE, "Disconnected\n");
3997 /* Detach and free everything */
3998 static void brcmf_sdbrcm_release(struct brcmf_sdio *bus)
4000 brcmf_dbg(TRACE, "Enter\n");
4003 /* De-register interrupt handler */
4004 brcmf_sdiod_intr_unregister(bus->sdiodev);
4006 cancel_work_sync(&bus->datawork);
4008 destroy_workqueue(bus->brcmf_wq);
4010 if (bus->sdiodev->bus_if->drvr) {
4011 brcmf_detach(bus->sdiodev->dev);
4012 brcmf_sdbrcm_release_dongle(bus);
4015 brcmu_pkt_buf_free_skb(bus->txglom_sgpad);
4016 brcmf_sdbrcm_release_malloc(bus);
4021 brcmf_dbg(TRACE, "Disconnected\n");
4024 static struct brcmf_bus_ops brcmf_sdio_bus_ops = {
4025 .stop = brcmf_sdbrcm_bus_stop,
4026 .preinit = brcmf_sdbrcm_bus_preinit,
4027 .init = brcmf_sdbrcm_bus_init,
4028 .txdata = brcmf_sdbrcm_bus_txdata,
4029 .txctl = brcmf_sdbrcm_bus_txctl,
4030 .rxctl = brcmf_sdbrcm_bus_rxctl,
4031 .gettxq = brcmf_sdbrcm_bus_gettxq,
4034 struct brcmf_sdio *brcmf_sdbrcm_probe(struct brcmf_sdio_dev *sdiodev)
4037 struct brcmf_sdio *bus;
4039 brcmf_dbg(TRACE, "Enter\n");
4041 /* Allocate private bus interface state */
4042 bus = kzalloc(sizeof(struct brcmf_sdio), GFP_ATOMIC);
4046 bus->sdiodev = sdiodev;
4048 skb_queue_head_init(&bus->glom);
4049 bus->txbound = BRCMF_TXBOUND;
4050 bus->rxbound = BRCMF_RXBOUND;
4051 bus->txminmax = BRCMF_TXMINMAX;
4052 bus->tx_seq = SDPCM_SEQ_WRAP - 1;
4054 /* platform specific configuration:
4055 * alignments must be at least 4 bytes for ADMA
4057 bus->head_align = ALIGNMENT;
4058 bus->sgentry_align = ALIGNMENT;
4059 if (sdiodev->pdata) {
4060 if (sdiodev->pdata->sd_head_align > ALIGNMENT)
4061 bus->head_align = sdiodev->pdata->sd_head_align;
4062 if (sdiodev->pdata->sd_sgentry_align > ALIGNMENT)
4063 bus->sgentry_align = sdiodev->pdata->sd_sgentry_align;
4066 INIT_WORK(&bus->datawork, brcmf_sdio_dataworker);
4067 bus->brcmf_wq = create_singlethread_workqueue("brcmf_wq");
4068 if (bus->brcmf_wq == NULL) {
4069 brcmf_err("insufficient memory to create txworkqueue\n");
4073 /* attempt to attach to the dongle */
4074 if (!(brcmf_sdbrcm_probe_attach(bus))) {
4075 brcmf_err("brcmf_sdbrcm_probe_attach failed\n");
4079 spin_lock_init(&bus->rxctl_lock);
4080 spin_lock_init(&bus->txqlock);
4081 init_waitqueue_head(&bus->ctrl_wait);
4082 init_waitqueue_head(&bus->dcmd_resp_wait);
4084 /* Set up the watchdog timer */
4085 init_timer(&bus->timer);
4086 bus->timer.data = (unsigned long)bus;
4087 bus->timer.function = brcmf_sdbrcm_watchdog;
4089 /* Initialize watchdog thread */
4090 init_completion(&bus->watchdog_wait);
4091 bus->watchdog_tsk = kthread_run(brcmf_sdbrcm_watchdog_thread,
4092 bus, "brcmf_watchdog");
4093 if (IS_ERR(bus->watchdog_tsk)) {
4094 pr_warn("brcmf_watchdog thread failed to start\n");
4095 bus->watchdog_tsk = NULL;
4097 /* Initialize DPC thread */
4098 atomic_set(&bus->dpc_tskcnt, 0);
4100 /* Assign bus interface call back */
4101 bus->sdiodev->bus_if->dev = bus->sdiodev->dev;
4102 bus->sdiodev->bus_if->ops = &brcmf_sdio_bus_ops;
4103 bus->sdiodev->bus_if->chip = bus->ci->chip;
4104 bus->sdiodev->bus_if->chiprev = bus->ci->chiprev;
4106 /* default sdio bus header length for tx packet */
4107 bus->tx_hdrlen = SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN;
4109 /* Attach to the common layer, reserve hdr space */
4110 ret = brcmf_attach(bus->sdiodev->dev);
4112 brcmf_err("brcmf_attach failed\n");
4116 /* Allocate buffers */
4117 if (!(brcmf_sdbrcm_probe_malloc(bus))) {
4118 brcmf_err("brcmf_sdbrcm_probe_malloc failed\n");
4122 if (!(brcmf_sdbrcm_probe_init(bus))) {
4123 brcmf_err("brcmf_sdbrcm_probe_init failed\n");
4127 brcmf_sdio_debugfs_create(bus);
4128 brcmf_dbg(INFO, "completed!!\n");
4130 /* if firmware path present try to download and bring up bus */
4131 ret = brcmf_bus_start(bus->sdiodev->dev);
4133 brcmf_err("dongle is not responding\n");
4140 brcmf_sdbrcm_release(bus);
4144 void brcmf_sdbrcm_disconnect(struct brcmf_sdio *bus)
4146 brcmf_dbg(TRACE, "Enter\n");
4148 brcmf_sdbrcm_release(bus);
4150 brcmf_dbg(TRACE, "Disconnected\n");
4154 brcmf_sdbrcm_wd_timer(struct brcmf_sdio *bus, uint wdtick)
4156 /* Totally stop the timer */
4157 if (!wdtick && bus->wd_timer_valid) {
4158 del_timer_sync(&bus->timer);
4159 bus->wd_timer_valid = false;
4160 bus->save_ms = wdtick;
4164 /* don't start the wd until fw is loaded */
4165 if (bus->sdiodev->bus_if->state == BRCMF_BUS_DOWN)
4169 if (bus->save_ms != BRCMF_WD_POLL_MS) {
4170 if (bus->wd_timer_valid)
4171 /* Stop timer and restart at new value */
4172 del_timer_sync(&bus->timer);
4174 /* Create timer again when watchdog period is
4175 dynamically changed or in the first instance
4177 bus->timer.expires =
4178 jiffies + BRCMF_WD_POLL_MS * HZ / 1000;
4179 add_timer(&bus->timer);
4182 /* Re arm the timer, at last watchdog period */
4183 mod_timer(&bus->timer,
4184 jiffies + BRCMF_WD_POLL_MS * HZ / 1000);
4187 bus->wd_timer_valid = true;
4188 bus->save_ms = wdtick;