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/atomic.h>
19 #include <linux/kernel.h>
20 #include <linux/kthread.h>
21 #include <linux/printk.h>
22 #include <linux/pci_ids.h>
23 #include <linux/netdevice.h>
24 #include <linux/interrupt.h>
25 #include <linux/sched.h>
26 #include <linux/mmc/sdio.h>
27 #include <linux/mmc/sdio_ids.h>
28 #include <linux/mmc/sdio_func.h>
29 #include <linux/mmc/card.h>
30 #include <linux/semaphore.h>
31 #include <linux/firmware.h>
32 #include <linux/module.h>
33 #include <linux/bcma/bcma.h>
34 #include <linux/debugfs.h>
35 #include <linux/vmalloc.h>
36 #include <linux/platform_data/brcmfmac-sdio.h>
37 #include <linux/moduleparam.h>
38 #include <asm/unaligned.h>
40 #include <brcmu_wifi.h>
41 #include <brcmu_utils.h>
42 #include <brcm_hw_ids.h>
48 #define DCMD_RESP_TIMEOUT 2000 /* In milli second */
49 #define CTL_DONE_TIMEOUT 2000 /* In milli second */
53 #define BRCMF_TRAP_INFO_SIZE 80
55 #define CBUF_LEN (128)
57 /* Device console log buffer state */
58 #define CONSOLE_BUFFER_MAX 2024
61 __le32 buf; /* Can't be pointer on (64-bit) hosts */
64 char *_buf_compat; /* Redundant pointer for backward compat. */
69 * When there is no UART (e.g. Quickturn),
70 * the host should write a complete
71 * input line directly into cbuf and then write
72 * the length into vcons_in.
73 * This may also be used when there is a real UART
74 * (at risk of conflicting with
75 * the real UART). vcons_out is currently unused.
80 /* Output (logging) buffer
81 * Console output is written to a ring buffer log_buf at index log_idx.
82 * The host may read the output when it sees log_idx advance.
83 * Output will be lost if the output wraps around faster than the host
86 struct rte_log_le log_le;
88 /* Console input line buffer
89 * Characters are read one at a time into cbuf
90 * until <CR> is received, then
91 * the buffer is processed as a command line.
92 * Also used for virtual UART.
99 #include <chipcommon.h>
103 #include "tracepoint.h"
105 #define TXQLEN 2048 /* bulk tx queue length */
106 #define TXHI (TXQLEN - 256) /* turn on flow control above TXHI */
107 #define TXLOW (TXHI - 256) /* turn off flow control below TXLOW */
110 #define TXRETRIES 2 /* # of retries for tx frames */
112 #define BRCMF_RXBOUND 50 /* Default for max rx frames in
115 #define BRCMF_TXBOUND 20 /* Default for max tx frames in
118 #define BRCMF_TXMINMAX 1 /* Max tx frames if rx still pending */
120 #define MEMBLOCK 2048 /* Block size used for downloading
122 #define MAX_DATA_BUF (32 * 1024) /* Must be large enough to hold
123 biggest possible glom */
125 #define BRCMF_FIRSTREAD (1 << 6)
127 #define BRCMF_CONSOLE 10 /* watchdog interval to poll console */
129 /* SBSDIO_DEVICE_CTL */
131 /* 1: device will assert busy signal when receiving CMD53 */
132 #define SBSDIO_DEVCTL_SETBUSY 0x01
133 /* 1: assertion of sdio interrupt is synchronous to the sdio clock */
134 #define SBSDIO_DEVCTL_SPI_INTR_SYNC 0x02
135 /* 1: mask all interrupts to host except the chipActive (rev 8) */
136 #define SBSDIO_DEVCTL_CA_INT_ONLY 0x04
137 /* 1: isolate internal sdio signals, put external pads in tri-state; requires
138 * sdio bus power cycle to clear (rev 9) */
139 #define SBSDIO_DEVCTL_PADS_ISO 0x08
140 /* Force SD->SB reset mapping (rev 11) */
141 #define SBSDIO_DEVCTL_SB_RST_CTL 0x30
142 /* Determined by CoreControl bit */
143 #define SBSDIO_DEVCTL_RST_CORECTL 0x00
144 /* Force backplane reset */
145 #define SBSDIO_DEVCTL_RST_BPRESET 0x10
146 /* Force no backplane reset */
147 #define SBSDIO_DEVCTL_RST_NOBPRESET 0x20
149 /* direct(mapped) cis space */
151 /* MAPPED common CIS address */
152 #define SBSDIO_CIS_BASE_COMMON 0x1000
153 /* maximum bytes in one CIS */
154 #define SBSDIO_CIS_SIZE_LIMIT 0x200
155 /* cis offset addr is < 17 bits */
156 #define SBSDIO_CIS_OFT_ADDR_MASK 0x1FFFF
158 /* manfid tuple length, include tuple, link bytes */
159 #define SBSDIO_CIS_MANFID_TUPLE_LEN 6
161 #define CORE_BUS_REG(base, field) \
162 (base + offsetof(struct sdpcmd_regs, field))
164 /* SDIO function 1 register CHIPCLKCSR */
165 /* Force ALP request to backplane */
166 #define SBSDIO_FORCE_ALP 0x01
167 /* Force HT request to backplane */
168 #define SBSDIO_FORCE_HT 0x02
169 /* Force ILP request to backplane */
170 #define SBSDIO_FORCE_ILP 0x04
171 /* Make ALP ready (power up xtal) */
172 #define SBSDIO_ALP_AVAIL_REQ 0x08
173 /* Make HT ready (power up PLL) */
174 #define SBSDIO_HT_AVAIL_REQ 0x10
175 /* Squelch clock requests from HW */
176 #define SBSDIO_FORCE_HW_CLKREQ_OFF 0x20
177 /* Status: ALP is ready */
178 #define SBSDIO_ALP_AVAIL 0x40
179 /* Status: HT is ready */
180 #define SBSDIO_HT_AVAIL 0x80
181 #define SBSDIO_CSR_MASK 0x1F
182 #define SBSDIO_AVBITS (SBSDIO_HT_AVAIL | SBSDIO_ALP_AVAIL)
183 #define SBSDIO_ALPAV(regval) ((regval) & SBSDIO_AVBITS)
184 #define SBSDIO_HTAV(regval) (((regval) & SBSDIO_AVBITS) == SBSDIO_AVBITS)
185 #define SBSDIO_ALPONLY(regval) (SBSDIO_ALPAV(regval) && !SBSDIO_HTAV(regval))
186 #define SBSDIO_CLKAV(regval, alponly) \
187 (SBSDIO_ALPAV(regval) && (alponly ? 1 : SBSDIO_HTAV(regval)))
190 #define I_SMB_SW0 (1 << 0) /* To SB Mail S/W interrupt 0 */
191 #define I_SMB_SW1 (1 << 1) /* To SB Mail S/W interrupt 1 */
192 #define I_SMB_SW2 (1 << 2) /* To SB Mail S/W interrupt 2 */
193 #define I_SMB_SW3 (1 << 3) /* To SB Mail S/W interrupt 3 */
194 #define I_SMB_SW_MASK 0x0000000f /* To SB Mail S/W interrupts mask */
195 #define I_SMB_SW_SHIFT 0 /* To SB Mail S/W interrupts shift */
196 #define I_HMB_SW0 (1 << 4) /* To Host Mail S/W interrupt 0 */
197 #define I_HMB_SW1 (1 << 5) /* To Host Mail S/W interrupt 1 */
198 #define I_HMB_SW2 (1 << 6) /* To Host Mail S/W interrupt 2 */
199 #define I_HMB_SW3 (1 << 7) /* To Host Mail S/W interrupt 3 */
200 #define I_HMB_SW_MASK 0x000000f0 /* To Host Mail S/W interrupts mask */
201 #define I_HMB_SW_SHIFT 4 /* To Host Mail S/W interrupts shift */
202 #define I_WR_OOSYNC (1 << 8) /* Write Frame Out Of Sync */
203 #define I_RD_OOSYNC (1 << 9) /* Read Frame Out Of Sync */
204 #define I_PC (1 << 10) /* descriptor error */
205 #define I_PD (1 << 11) /* data error */
206 #define I_DE (1 << 12) /* Descriptor protocol Error */
207 #define I_RU (1 << 13) /* Receive descriptor Underflow */
208 #define I_RO (1 << 14) /* Receive fifo Overflow */
209 #define I_XU (1 << 15) /* Transmit fifo Underflow */
210 #define I_RI (1 << 16) /* Receive Interrupt */
211 #define I_BUSPWR (1 << 17) /* SDIO Bus Power Change (rev 9) */
212 #define I_XMTDATA_AVAIL (1 << 23) /* bits in fifo */
213 #define I_XI (1 << 24) /* Transmit Interrupt */
214 #define I_RF_TERM (1 << 25) /* Read Frame Terminate */
215 #define I_WF_TERM (1 << 26) /* Write Frame Terminate */
216 #define I_PCMCIA_XU (1 << 27) /* PCMCIA Transmit FIFO Underflow */
217 #define I_SBINT (1 << 28) /* sbintstatus Interrupt */
218 #define I_CHIPACTIVE (1 << 29) /* chip from doze to active state */
219 #define I_SRESET (1 << 30) /* CCCR RES interrupt */
220 #define I_IOE2 (1U << 31) /* CCCR IOE2 Bit Changed */
221 #define I_ERRORS (I_PC | I_PD | I_DE | I_RU | I_RO | I_XU)
222 #define I_DMA (I_RI | I_XI | I_ERRORS)
225 #define CC_CISRDY (1 << 0) /* CIS Ready */
226 #define CC_BPRESEN (1 << 1) /* CCCR RES signal */
227 #define CC_F2RDY (1 << 2) /* set CCCR IOR2 bit */
228 #define CC_CLRPADSISO (1 << 3) /* clear SDIO pads isolation */
229 #define CC_XMTDATAAVAIL_MODE (1 << 4)
230 #define CC_XMTDATAAVAIL_CTRL (1 << 5)
233 #define SFC_RF_TERM (1 << 0) /* Read Frame Terminate */
234 #define SFC_WF_TERM (1 << 1) /* Write Frame Terminate */
235 #define SFC_CRC4WOOS (1 << 2) /* CRC error for write out of sync */
236 #define SFC_ABORTALL (1 << 3) /* Abort all in-progress frames */
239 * Software allocation of To SB Mailbox resources
242 /* tosbmailbox bits corresponding to intstatus bits */
243 #define SMB_NAK (1 << 0) /* Frame NAK */
244 #define SMB_INT_ACK (1 << 1) /* Host Interrupt ACK */
245 #define SMB_USE_OOB (1 << 2) /* Use OOB Wakeup */
246 #define SMB_DEV_INT (1 << 3) /* Miscellaneous Interrupt */
248 /* tosbmailboxdata */
249 #define SMB_DATA_VERSION_SHIFT 16 /* host protocol version */
252 * Software allocation of To Host Mailbox resources
256 #define I_HMB_FC_STATE I_HMB_SW0 /* Flow Control State */
257 #define I_HMB_FC_CHANGE I_HMB_SW1 /* Flow Control State Changed */
258 #define I_HMB_FRAME_IND I_HMB_SW2 /* Frame Indication */
259 #define I_HMB_HOST_INT I_HMB_SW3 /* Miscellaneous Interrupt */
261 /* tohostmailboxdata */
262 #define HMB_DATA_NAKHANDLED 1 /* retransmit NAK'd frame */
263 #define HMB_DATA_DEVREADY 2 /* talk to host after enable */
264 #define HMB_DATA_FC 4 /* per prio flowcontrol update flag */
265 #define HMB_DATA_FWREADY 8 /* fw ready for protocol activity */
267 #define HMB_DATA_FCDATA_MASK 0xff000000
268 #define HMB_DATA_FCDATA_SHIFT 24
270 #define HMB_DATA_VERSION_MASK 0x00ff0000
271 #define HMB_DATA_VERSION_SHIFT 16
274 * Software-defined protocol header
277 /* Current protocol version */
278 #define SDPCM_PROT_VERSION 4
281 * Shared structure between dongle and the host.
282 * The structure contains pointers to trap or assert information.
284 #define SDPCM_SHARED_VERSION 0x0003
285 #define SDPCM_SHARED_VERSION_MASK 0x00FF
286 #define SDPCM_SHARED_ASSERT_BUILT 0x0100
287 #define SDPCM_SHARED_ASSERT 0x0200
288 #define SDPCM_SHARED_TRAP 0x0400
290 /* Space for header read, limit for data packets */
291 #define MAX_HDR_READ (1 << 6)
292 #define MAX_RX_DATASZ 2048
294 /* Bump up limit on waiting for HT to account for first startup;
295 * if the image is doing a CRC calculation before programming the PMU
296 * for HT availability, it could take a couple hundred ms more, so
297 * max out at a 1 second (1000000us).
299 #undef PMU_MAX_TRANSITION_DLY
300 #define PMU_MAX_TRANSITION_DLY 1000000
302 /* Value for ChipClockCSR during initial setup */
303 #define BRCMF_INIT_CLKCTL1 (SBSDIO_FORCE_HW_CLKREQ_OFF | \
304 SBSDIO_ALP_AVAIL_REQ)
306 /* Flags for SDH calls */
307 #define F2SYNC (SDIO_REQ_4BYTE | SDIO_REQ_FIXED)
309 #define BRCMF_IDLE_ACTIVE 0 /* Do not request any SD clock change
312 #define BRCMF_IDLE_INTERVAL 1
314 #define KSO_WAIT_US 50
315 #define MAX_KSO_ATTEMPTS (PMU_MAX_TRANSITION_DLY/KSO_WAIT_US)
318 * Conversion of 802.1D priority to precedence level
320 static uint prio2prec(u32 prio)
322 return (prio == PRIO_8021D_NONE || prio == PRIO_8021D_BE) ?
327 /* Device console log buffer state */
328 struct brcmf_console {
329 uint count; /* Poll interval msec counter */
330 uint log_addr; /* Log struct address (fixed) */
331 struct rte_log_le log_le; /* Log struct (host copy) */
332 uint bufsize; /* Size of log buffer */
333 u8 *buf; /* Log buffer (host copy) */
334 uint last; /* Last buffer read index */
337 struct brcmf_trap_info {
351 __le32 r9; /* sb/v6 */
352 __le32 r10; /* sl/v7 */
353 __le32 r11; /* fp/v8 */
361 struct sdpcm_shared {
365 u32 assert_file_addr;
367 u32 console_addr; /* Address of struct rte_console */
373 struct sdpcm_shared_le {
376 __le32 assert_exp_addr;
377 __le32 assert_file_addr;
379 __le32 console_addr; /* Address of struct rte_console */
380 __le32 msgtrace_addr;
385 /* dongle SDIO bus specific header info */
386 struct brcmf_sdio_hdrinfo {
398 * hold counter variables
400 struct brcmf_sdio_count {
401 uint intrcount; /* Count of device interrupt callbacks */
402 uint lastintrs; /* Count as of last watchdog timer */
403 uint pollcnt; /* Count of active polls */
404 uint regfails; /* Count of R_REG failures */
405 uint tx_sderrs; /* Count of tx attempts with sd errors */
406 uint fcqueued; /* Tx packets that got queued */
407 uint rxrtx; /* Count of rtx requests (NAK to dongle) */
408 uint rx_toolong; /* Receive frames too long to receive */
409 uint rxc_errors; /* SDIO errors when reading control frames */
410 uint rx_hdrfail; /* SDIO errors on header reads */
411 uint rx_badhdr; /* Bad received headers (roosync?) */
412 uint rx_badseq; /* Mismatched rx sequence number */
413 uint fc_rcvd; /* Number of flow-control events received */
414 uint fc_xoff; /* Number which turned on flow-control */
415 uint fc_xon; /* Number which turned off flow-control */
416 uint rxglomfail; /* Failed deglom attempts */
417 uint rxglomframes; /* Number of glom frames (superframes) */
418 uint rxglompkts; /* Number of packets from glom frames */
419 uint f2rxhdrs; /* Number of header reads */
420 uint f2rxdata; /* Number of frame data reads */
421 uint f2txdata; /* Number of f2 frame writes */
422 uint f1regdata; /* Number of f1 register accesses */
423 uint tickcnt; /* Number of watchdog been schedule */
424 ulong tx_ctlerrs; /* Err of sending ctrl frames */
425 ulong tx_ctlpkts; /* Ctrl frames sent to dongle */
426 ulong rx_ctlerrs; /* Err of processing rx ctrl frames */
427 ulong rx_ctlpkts; /* Ctrl frames processed from dongle */
428 ulong rx_readahead_cnt; /* packets where header read-ahead was used */
431 /* misc chip info needed by some of the routines */
432 /* Private data for SDIO bus interaction */
434 struct brcmf_sdio_dev *sdiodev; /* sdio device handler */
435 struct brcmf_chip *ci; /* Chip info struct */
437 u32 hostintmask; /* Copy of Host Interrupt Mask */
438 atomic_t intstatus; /* Intstatus bits (events) pending */
439 atomic_t fcstate; /* State of dongle flow-control */
441 uint blocksize; /* Block size of SDIO transfers */
442 uint roundup; /* Max roundup limit */
444 struct pktq txq; /* Queue length used for flow-control */
445 u8 flowcontrol; /* per prio flow control bitmask */
446 u8 tx_seq; /* Transmit sequence number (next) */
447 u8 tx_max; /* Maximum transmit sequence allowed */
449 u8 *hdrbuf; /* buffer for handling rx frame */
450 u8 *rxhdr; /* Header of current rx frame (in hdrbuf) */
451 u8 rx_seq; /* Receive sequence number (expected) */
452 struct brcmf_sdio_hdrinfo cur_read;
453 /* info of current read frame */
454 bool rxskip; /* Skip receive (awaiting NAK ACK) */
455 bool rxpending; /* Data frame pending in dongle */
457 uint rxbound; /* Rx frames to read before resched */
458 uint txbound; /* Tx frames to send before resched */
461 struct sk_buff *glomd; /* Packet containing glomming descriptor */
462 struct sk_buff_head glom; /* Packet list for glommed superframe */
463 uint glomerr; /* Glom packet read errors */
465 u8 *rxbuf; /* Buffer for receiving control packets */
466 uint rxblen; /* Allocated length of rxbuf */
467 u8 *rxctl; /* Aligned pointer into rxbuf */
468 u8 *rxctl_orig; /* pointer for freeing rxctl */
469 uint rxlen; /* Length of valid data in buffer */
470 spinlock_t rxctl_lock; /* protection lock for ctrl frame resources */
472 u8 sdpcm_ver; /* Bus protocol reported by dongle */
474 bool intr; /* Use interrupts */
475 bool poll; /* Use polling */
476 atomic_t ipend; /* Device interrupt is pending */
477 uint spurious; /* Count of spurious interrupts */
478 uint pollrate; /* Ticks between device polls */
479 uint polltick; /* Tick counter */
482 uint console_interval;
483 struct brcmf_console console; /* Console output polling support */
484 uint console_addr; /* Console address from shared struct */
487 uint clkstate; /* State of sd and backplane clock(s) */
488 s32 idletime; /* Control for activity timeout */
489 s32 idlecount; /* Activity timeout counter */
490 s32 idleclock; /* How to set bus driver when idle */
491 bool rxflow_mode; /* Rx flow control mode */
492 bool rxflow; /* Is rx flow control on */
493 bool alp_only; /* Don't use HT clock (ALP only) */
497 bool ctrl_frame_stat;
500 spinlock_t txq_lock; /* protect bus->txq */
501 wait_queue_head_t ctrl_wait;
502 wait_queue_head_t dcmd_resp_wait;
504 struct timer_list timer;
505 struct completion watchdog_wait;
506 struct task_struct *watchdog_tsk;
510 struct workqueue_struct *brcmf_wq;
511 struct work_struct datawork;
515 bool txoff; /* Transmit flow-controlled */
516 struct brcmf_sdio_count sdcnt;
517 bool sr_enabled; /* SaveRestore enabled */
520 u8 tx_hdrlen; /* sdio bus header length for tx packet */
521 bool txglom; /* host tx glomming enable flag */
522 u16 head_align; /* buffer pointer alignment */
523 u16 sgentry_align; /* scatter-gather buffer alignment */
529 #define CLK_PENDING 2
533 static int qcount[NUMPRIO];
536 #define DEFAULT_SDIO_DRIVE_STRENGTH 6 /* in milliamps */
538 #define RETRYCHAN(chan) ((chan) == SDPCM_EVENT_CHANNEL)
540 /* Retry count for register access failures */
541 static const uint retry_limit = 2;
543 /* Limit on rounding up frames */
544 static const uint max_roundup = 512;
548 enum brcmf_sdio_frmtype {
549 BRCMF_SDIO_FT_NORMAL,
554 #define SDIOD_DRVSTR_KEY(chip, pmu) (((chip) << 16) | (pmu))
556 /* SDIO Pad drive strength to select value mappings */
557 struct sdiod_drive_str {
558 u8 strength; /* Pad Drive Strength in mA */
559 u8 sel; /* Chip-specific select value */
562 /* SDIO Drive Strength to sel value table for PMU Rev 11 (1.8V) */
563 static const struct sdiod_drive_str sdiod_drvstr_tab1_1v8[] = {
574 /* SDIO Drive Strength to sel value table for PMU Rev 13 (1.8v) */
575 static const struct sdiod_drive_str sdiod_drive_strength_tab5_1v8[] = {
585 /* SDIO Drive Strength to sel value table for PMU Rev 17 (1.8v) */
586 static const struct sdiod_drive_str sdiod_drvstr_tab6_1v8[] = {
592 /* SDIO Drive Strength to sel value table for 43143 PMU Rev 17 (3.3V) */
593 static const struct sdiod_drive_str sdiod_drvstr_tab2_3v3[] = {
600 #define BCM43143_FIRMWARE_NAME "brcm/brcmfmac43143-sdio.bin"
601 #define BCM43143_NVRAM_NAME "brcm/brcmfmac43143-sdio.txt"
602 #define BCM43241B0_FIRMWARE_NAME "brcm/brcmfmac43241b0-sdio.bin"
603 #define BCM43241B0_NVRAM_NAME "brcm/brcmfmac43241b0-sdio.txt"
604 #define BCM43241B4_FIRMWARE_NAME "brcm/brcmfmac43241b4-sdio.bin"
605 #define BCM43241B4_NVRAM_NAME "brcm/brcmfmac43241b4-sdio.txt"
606 #define BCM43241B5_FIRMWARE_NAME "brcm/brcmfmac43241b5-sdio.bin"
607 #define BCM43241B5_NVRAM_NAME "brcm/brcmfmac43241b5-sdio.txt"
608 #define BCM4329_FIRMWARE_NAME "brcm/brcmfmac4329-sdio.bin"
609 #define BCM4329_NVRAM_NAME "brcm/brcmfmac4329-sdio.txt"
610 #define BCM4330_FIRMWARE_NAME "brcm/brcmfmac4330-sdio.bin"
611 #define BCM4330_NVRAM_NAME "brcm/brcmfmac4330-sdio.txt"
612 #define BCM4334_FIRMWARE_NAME "brcm/brcmfmac4334-sdio.bin"
613 #define BCM4334_NVRAM_NAME "brcm/brcmfmac4334-sdio.txt"
614 #define BCM43340_FIRMWARE_NAME "brcm/brcmfmac43340-sdio.bin"
615 #define BCM43340_NVRAM_NAME "brcm/brcmfmac43340-sdio.txt"
616 #define BCM4335_FIRMWARE_NAME "brcm/brcmfmac4335-sdio.bin"
617 #define BCM4335_NVRAM_NAME "brcm/brcmfmac4335-sdio.txt"
618 #define BCM43362_FIRMWARE_NAME "brcm/brcmfmac43362-sdio.bin"
619 #define BCM43362_NVRAM_NAME "brcm/brcmfmac43362-sdio.txt"
620 #define BCM4339_FIRMWARE_NAME "brcm/brcmfmac4339-sdio.bin"
621 #define BCM4339_NVRAM_NAME "brcm/brcmfmac4339-sdio.txt"
622 #define BCM43430_FIRMWARE_NAME "brcm/brcmfmac43430-sdio.bin"
623 #define BCM43430_NVRAM_NAME "brcm/brcmfmac43430-sdio.txt"
624 #define BCM43455_FIRMWARE_NAME "brcm/brcmfmac43455-sdio.bin"
625 #define BCM43455_NVRAM_NAME "brcm/brcmfmac43455-sdio.txt"
626 #define BCM4354_FIRMWARE_NAME "brcm/brcmfmac4354-sdio.bin"
627 #define BCM4354_NVRAM_NAME "brcm/brcmfmac4354-sdio.txt"
629 MODULE_FIRMWARE(BCM43143_FIRMWARE_NAME);
630 MODULE_FIRMWARE(BCM43143_NVRAM_NAME);
631 MODULE_FIRMWARE(BCM43241B0_FIRMWARE_NAME);
632 MODULE_FIRMWARE(BCM43241B0_NVRAM_NAME);
633 MODULE_FIRMWARE(BCM43241B4_FIRMWARE_NAME);
634 MODULE_FIRMWARE(BCM43241B4_NVRAM_NAME);
635 MODULE_FIRMWARE(BCM43241B5_FIRMWARE_NAME);
636 MODULE_FIRMWARE(BCM43241B5_NVRAM_NAME);
637 MODULE_FIRMWARE(BCM4329_FIRMWARE_NAME);
638 MODULE_FIRMWARE(BCM4329_NVRAM_NAME);
639 MODULE_FIRMWARE(BCM4330_FIRMWARE_NAME);
640 MODULE_FIRMWARE(BCM4330_NVRAM_NAME);
641 MODULE_FIRMWARE(BCM4334_FIRMWARE_NAME);
642 MODULE_FIRMWARE(BCM4334_NVRAM_NAME);
643 MODULE_FIRMWARE(BCM43340_FIRMWARE_NAME);
644 MODULE_FIRMWARE(BCM43340_NVRAM_NAME);
645 MODULE_FIRMWARE(BCM4335_FIRMWARE_NAME);
646 MODULE_FIRMWARE(BCM4335_NVRAM_NAME);
647 MODULE_FIRMWARE(BCM43362_FIRMWARE_NAME);
648 MODULE_FIRMWARE(BCM43362_NVRAM_NAME);
649 MODULE_FIRMWARE(BCM4339_FIRMWARE_NAME);
650 MODULE_FIRMWARE(BCM4339_NVRAM_NAME);
651 MODULE_FIRMWARE(BCM43430_FIRMWARE_NAME);
652 MODULE_FIRMWARE(BCM43430_NVRAM_NAME);
653 MODULE_FIRMWARE(BCM43455_FIRMWARE_NAME);
654 MODULE_FIRMWARE(BCM43455_NVRAM_NAME);
655 MODULE_FIRMWARE(BCM4354_FIRMWARE_NAME);
656 MODULE_FIRMWARE(BCM4354_NVRAM_NAME);
658 struct brcmf_firmware_names {
665 enum brcmf_firmware_type {
670 #define BRCMF_FIRMWARE_NVRAM(name) \
671 name ## _FIRMWARE_NAME, name ## _NVRAM_NAME
673 static const struct brcmf_firmware_names brcmf_fwname_data[] = {
674 { BRCM_CC_43143_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM43143) },
675 { BRCM_CC_43241_CHIP_ID, 0x0000001F, BRCMF_FIRMWARE_NVRAM(BCM43241B0) },
676 { BRCM_CC_43241_CHIP_ID, 0x00000020, BRCMF_FIRMWARE_NVRAM(BCM43241B4) },
677 { BRCM_CC_43241_CHIP_ID, 0xFFFFFFC0, BRCMF_FIRMWARE_NVRAM(BCM43241B5) },
678 { BRCM_CC_4329_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM4329) },
679 { BRCM_CC_4330_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM4330) },
680 { BRCM_CC_4334_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM4334) },
681 { BRCM_CC_43340_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM43340) },
682 { BRCM_CC_4335_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM4335) },
683 { BRCM_CC_43362_CHIP_ID, 0xFFFFFFFE, BRCMF_FIRMWARE_NVRAM(BCM43362) },
684 { BRCM_CC_4339_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM4339) },
685 { BRCM_CC_43430_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM43430) },
686 { BRCM_CC_4345_CHIP_ID, 0xFFFFFFC0, BRCMF_FIRMWARE_NVRAM(BCM43455) },
687 { BRCM_CC_4354_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM4354) }
690 static int brcmf_sdio_get_fwnames(struct brcmf_chip *ci,
691 struct brcmf_sdio_dev *sdiodev)
696 for (i = 0; i < ARRAY_SIZE(brcmf_fwname_data); i++) {
697 if (brcmf_fwname_data[i].chipid == ci->chip &&
698 brcmf_fwname_data[i].revmsk & BIT(ci->chiprev))
702 if (i == ARRAY_SIZE(brcmf_fwname_data)) {
703 brcmf_err("Unknown chipid %d [%d]\n", ci->chip, ci->chiprev);
707 /* check if firmware path is provided by module parameter */
708 if (brcmf_firmware_path[0] != '\0') {
709 strlcpy(sdiodev->fw_name, brcmf_firmware_path,
710 sizeof(sdiodev->fw_name));
711 strlcpy(sdiodev->nvram_name, brcmf_firmware_path,
712 sizeof(sdiodev->nvram_name));
714 end = brcmf_firmware_path[strlen(brcmf_firmware_path) - 1];
716 strlcat(sdiodev->fw_name, "/",
717 sizeof(sdiodev->fw_name));
718 strlcat(sdiodev->nvram_name, "/",
719 sizeof(sdiodev->nvram_name));
722 strlcat(sdiodev->fw_name, brcmf_fwname_data[i].bin,
723 sizeof(sdiodev->fw_name));
724 strlcat(sdiodev->nvram_name, brcmf_fwname_data[i].nv,
725 sizeof(sdiodev->nvram_name));
730 static void pkt_align(struct sk_buff *p, int len, int align)
733 datalign = (unsigned long)(p->data);
734 datalign = roundup(datalign, (align)) - datalign;
736 skb_pull(p, datalign);
740 /* To check if there's window offered */
741 static bool data_ok(struct brcmf_sdio *bus)
743 return (u8)(bus->tx_max - bus->tx_seq) != 0 &&
744 ((u8)(bus->tx_max - bus->tx_seq) & 0x80) == 0;
748 * Reads a register in the SDIO hardware block. This block occupies a series of
749 * adresses on the 32 bit backplane bus.
751 static int r_sdreg32(struct brcmf_sdio *bus, u32 *regvar, u32 offset)
753 struct brcmf_core *core;
756 core = brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV);
757 *regvar = brcmf_sdiod_regrl(bus->sdiodev, core->base + offset, &ret);
762 static int w_sdreg32(struct brcmf_sdio *bus, u32 regval, u32 reg_offset)
764 struct brcmf_core *core;
767 core = brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV);
768 brcmf_sdiod_regwl(bus->sdiodev, core->base + reg_offset, regval, &ret);
774 brcmf_sdio_kso_control(struct brcmf_sdio *bus, bool on)
776 u8 wr_val = 0, rd_val, cmp_val, bmask;
780 brcmf_dbg(TRACE, "Enter: on=%d\n", on);
782 wr_val = (on << SBSDIO_FUNC1_SLEEPCSR_KSO_SHIFT);
783 /* 1st KSO write goes to AOS wake up core if device is asleep */
784 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
788 /* device WAKEUP through KSO:
789 * write bit 0 & read back until
790 * both bits 0 (kso bit) & 1 (dev on status) are set
792 cmp_val = SBSDIO_FUNC1_SLEEPCSR_KSO_MASK |
793 SBSDIO_FUNC1_SLEEPCSR_DEVON_MASK;
795 usleep_range(2000, 3000);
797 /* Put device to sleep, turn off KSO */
799 /* only check for bit0, bit1(dev on status) may not
800 * get cleared right away
802 bmask = SBSDIO_FUNC1_SLEEPCSR_KSO_MASK;
806 /* reliable KSO bit set/clr:
807 * the sdiod sleep write access is synced to PMU 32khz clk
808 * just one write attempt may fail,
809 * read it back until it matches written value
811 rd_val = brcmf_sdiod_regrb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
813 if (((rd_val & bmask) == cmp_val) && !err)
817 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
819 } while (try_cnt++ < MAX_KSO_ATTEMPTS);
822 brcmf_dbg(SDIO, "try_cnt=%d rd_val=0x%x err=%d\n", try_cnt,
825 if (try_cnt > MAX_KSO_ATTEMPTS)
826 brcmf_err("max tries: rd_val=0x%x err=%d\n", rd_val, err);
831 #define HOSTINTMASK (I_HMB_SW_MASK | I_CHIPACTIVE)
833 /* Turn backplane clock on or off */
834 static int brcmf_sdio_htclk(struct brcmf_sdio *bus, bool on, bool pendok)
837 u8 clkctl, clkreq, devctl;
838 unsigned long timeout;
840 brcmf_dbg(SDIO, "Enter\n");
844 if (bus->sr_enabled) {
845 bus->clkstate = (on ? CLK_AVAIL : CLK_SDONLY);
850 /* Request HT Avail */
852 bus->alp_only ? SBSDIO_ALP_AVAIL_REQ : SBSDIO_HT_AVAIL_REQ;
854 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
857 brcmf_err("HT Avail request error: %d\n", err);
861 /* Check current status */
862 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
863 SBSDIO_FUNC1_CHIPCLKCSR, &err);
865 brcmf_err("HT Avail read error: %d\n", err);
869 /* Go to pending and await interrupt if appropriate */
870 if (!SBSDIO_CLKAV(clkctl, bus->alp_only) && pendok) {
871 /* Allow only clock-available interrupt */
872 devctl = brcmf_sdiod_regrb(bus->sdiodev,
873 SBSDIO_DEVICE_CTL, &err);
875 brcmf_err("Devctl error setting CA: %d\n",
880 devctl |= SBSDIO_DEVCTL_CA_INT_ONLY;
881 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
883 brcmf_dbg(SDIO, "CLKCTL: set PENDING\n");
884 bus->clkstate = CLK_PENDING;
887 } else if (bus->clkstate == CLK_PENDING) {
888 /* Cancel CA-only interrupt filter */
889 devctl = brcmf_sdiod_regrb(bus->sdiodev,
890 SBSDIO_DEVICE_CTL, &err);
891 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
892 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
896 /* Otherwise, wait here (polling) for HT Avail */
898 msecs_to_jiffies(PMU_MAX_TRANSITION_DLY/1000);
899 while (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
900 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
901 SBSDIO_FUNC1_CHIPCLKCSR,
903 if (time_after(jiffies, timeout))
906 usleep_range(5000, 10000);
909 brcmf_err("HT Avail request error: %d\n", err);
912 if (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
913 brcmf_err("HT Avail timeout (%d): clkctl 0x%02x\n",
914 PMU_MAX_TRANSITION_DLY, clkctl);
918 /* Mark clock available */
919 bus->clkstate = CLK_AVAIL;
920 brcmf_dbg(SDIO, "CLKCTL: turned ON\n");
923 if (!bus->alp_only) {
924 if (SBSDIO_ALPONLY(clkctl))
925 brcmf_err("HT Clock should be on\n");
927 #endif /* defined (DEBUG) */
932 if (bus->clkstate == CLK_PENDING) {
933 /* Cancel CA-only interrupt filter */
934 devctl = brcmf_sdiod_regrb(bus->sdiodev,
935 SBSDIO_DEVICE_CTL, &err);
936 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
937 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
941 bus->clkstate = CLK_SDONLY;
942 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
944 brcmf_dbg(SDIO, "CLKCTL: turned OFF\n");
946 brcmf_err("Failed access turning clock off: %d\n",
954 /* Change idle/active SD state */
955 static int brcmf_sdio_sdclk(struct brcmf_sdio *bus, bool on)
957 brcmf_dbg(SDIO, "Enter\n");
960 bus->clkstate = CLK_SDONLY;
962 bus->clkstate = CLK_NONE;
967 /* Transition SD and backplane clock readiness */
968 static int brcmf_sdio_clkctl(struct brcmf_sdio *bus, uint target, bool pendok)
971 uint oldstate = bus->clkstate;
974 brcmf_dbg(SDIO, "Enter\n");
976 /* Early exit if we're already there */
977 if (bus->clkstate == target)
982 /* Make sure SD clock is available */
983 if (bus->clkstate == CLK_NONE)
984 brcmf_sdio_sdclk(bus, true);
985 /* Now request HT Avail on the backplane */
986 brcmf_sdio_htclk(bus, true, pendok);
990 /* Remove HT request, or bring up SD clock */
991 if (bus->clkstate == CLK_NONE)
992 brcmf_sdio_sdclk(bus, true);
993 else if (bus->clkstate == CLK_AVAIL)
994 brcmf_sdio_htclk(bus, false, false);
996 brcmf_err("request for %d -> %d\n",
997 bus->clkstate, target);
1001 /* Make sure to remove HT request */
1002 if (bus->clkstate == CLK_AVAIL)
1003 brcmf_sdio_htclk(bus, false, false);
1004 /* Now remove the SD clock */
1005 brcmf_sdio_sdclk(bus, false);
1009 brcmf_dbg(SDIO, "%d -> %d\n", oldstate, bus->clkstate);
1016 brcmf_sdio_bus_sleep(struct brcmf_sdio *bus, bool sleep, bool pendok)
1021 brcmf_dbg(SDIO, "Enter: request %s currently %s\n",
1022 (sleep ? "SLEEP" : "WAKE"),
1023 (bus->sleeping ? "SLEEP" : "WAKE"));
1025 /* If SR is enabled control bus state with KSO */
1026 if (bus->sr_enabled) {
1027 /* Done if we're already in the requested state */
1028 if (sleep == bus->sleeping)
1031 /* Going to sleep */
1033 clkcsr = brcmf_sdiod_regrb(bus->sdiodev,
1034 SBSDIO_FUNC1_CHIPCLKCSR,
1036 if ((clkcsr & SBSDIO_CSR_MASK) == 0) {
1037 brcmf_dbg(SDIO, "no clock, set ALP\n");
1038 brcmf_sdiod_regwb(bus->sdiodev,
1039 SBSDIO_FUNC1_CHIPCLKCSR,
1040 SBSDIO_ALP_AVAIL_REQ, &err);
1042 err = brcmf_sdio_kso_control(bus, false);
1044 err = brcmf_sdio_kso_control(bus, true);
1047 brcmf_err("error while changing bus sleep state %d\n",
1054 /* control clocks */
1056 if (!bus->sr_enabled)
1057 brcmf_sdio_clkctl(bus, CLK_NONE, pendok);
1059 brcmf_sdio_clkctl(bus, CLK_AVAIL, pendok);
1060 brcmf_sdio_wd_timer(bus, BRCMF_WD_POLL_MS);
1062 bus->sleeping = sleep;
1063 brcmf_dbg(SDIO, "new state %s\n",
1064 (sleep ? "SLEEP" : "WAKE"));
1066 brcmf_dbg(SDIO, "Exit: err=%d\n", err);
1072 static inline bool brcmf_sdio_valid_shared_address(u32 addr)
1074 return !(addr == 0 || ((~addr >> 16) & 0xffff) == (addr & 0xffff));
1077 static int brcmf_sdio_readshared(struct brcmf_sdio *bus,
1078 struct sdpcm_shared *sh)
1083 struct sdpcm_shared_le sh_le;
1086 sdio_claim_host(bus->sdiodev->func[1]);
1087 brcmf_sdio_bus_sleep(bus, false, false);
1090 * Read last word in socram to determine
1091 * address of sdpcm_shared structure
1093 shaddr = bus->ci->rambase + bus->ci->ramsize - 4;
1094 if (!bus->ci->rambase && brcmf_chip_sr_capable(bus->ci))
1095 shaddr -= bus->ci->srsize;
1096 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, shaddr,
1102 * Check if addr is valid.
1103 * NVRAM length at the end of memory should have been overwritten.
1105 addr = le32_to_cpu(addr_le);
1106 if (!brcmf_sdio_valid_shared_address(addr)) {
1107 brcmf_err("invalid sdpcm_shared address 0x%08X\n", addr);
1112 brcmf_dbg(INFO, "sdpcm_shared address 0x%08X\n", addr);
1114 /* Read hndrte_shared structure */
1115 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, (u8 *)&sh_le,
1116 sizeof(struct sdpcm_shared_le));
1120 sdio_release_host(bus->sdiodev->func[1]);
1123 sh->flags = le32_to_cpu(sh_le.flags);
1124 sh->trap_addr = le32_to_cpu(sh_le.trap_addr);
1125 sh->assert_exp_addr = le32_to_cpu(sh_le.assert_exp_addr);
1126 sh->assert_file_addr = le32_to_cpu(sh_le.assert_file_addr);
1127 sh->assert_line = le32_to_cpu(sh_le.assert_line);
1128 sh->console_addr = le32_to_cpu(sh_le.console_addr);
1129 sh->msgtrace_addr = le32_to_cpu(sh_le.msgtrace_addr);
1131 if ((sh->flags & SDPCM_SHARED_VERSION_MASK) > SDPCM_SHARED_VERSION) {
1132 brcmf_err("sdpcm shared version unsupported: dhd %d dongle %d\n",
1133 SDPCM_SHARED_VERSION,
1134 sh->flags & SDPCM_SHARED_VERSION_MASK);
1140 brcmf_err("unable to obtain sdpcm_shared info: rv=%d (addr=0x%x)\n",
1142 sdio_release_host(bus->sdiodev->func[1]);
1146 static void brcmf_sdio_get_console_addr(struct brcmf_sdio *bus)
1148 struct sdpcm_shared sh;
1150 if (brcmf_sdio_readshared(bus, &sh) == 0)
1151 bus->console_addr = sh.console_addr;
1154 static void brcmf_sdio_get_console_addr(struct brcmf_sdio *bus)
1159 static u32 brcmf_sdio_hostmail(struct brcmf_sdio *bus)
1166 brcmf_dbg(SDIO, "Enter\n");
1168 /* Read mailbox data and ack that we did so */
1169 ret = r_sdreg32(bus, &hmb_data,
1170 offsetof(struct sdpcmd_regs, tohostmailboxdata));
1173 w_sdreg32(bus, SMB_INT_ACK,
1174 offsetof(struct sdpcmd_regs, tosbmailbox));
1175 bus->sdcnt.f1regdata += 2;
1177 /* Dongle recomposed rx frames, accept them again */
1178 if (hmb_data & HMB_DATA_NAKHANDLED) {
1179 brcmf_dbg(SDIO, "Dongle reports NAK handled, expect rtx of %d\n",
1182 brcmf_err("unexpected NAKHANDLED!\n");
1184 bus->rxskip = false;
1185 intstatus |= I_HMB_FRAME_IND;
1189 * DEVREADY does not occur with gSPI.
1191 if (hmb_data & (HMB_DATA_DEVREADY | HMB_DATA_FWREADY)) {
1193 (hmb_data & HMB_DATA_VERSION_MASK) >>
1194 HMB_DATA_VERSION_SHIFT;
1195 if (bus->sdpcm_ver != SDPCM_PROT_VERSION)
1196 brcmf_err("Version mismatch, dongle reports %d, "
1198 bus->sdpcm_ver, SDPCM_PROT_VERSION);
1200 brcmf_dbg(SDIO, "Dongle ready, protocol version %d\n",
1204 * Retrieve console state address now that firmware should have
1207 brcmf_sdio_get_console_addr(bus);
1211 * Flow Control has been moved into the RX headers and this out of band
1212 * method isn't used any more.
1213 * remaining backward compatible with older dongles.
1215 if (hmb_data & HMB_DATA_FC) {
1216 fcbits = (hmb_data & HMB_DATA_FCDATA_MASK) >>
1217 HMB_DATA_FCDATA_SHIFT;
1219 if (fcbits & ~bus->flowcontrol)
1220 bus->sdcnt.fc_xoff++;
1222 if (bus->flowcontrol & ~fcbits)
1223 bus->sdcnt.fc_xon++;
1225 bus->sdcnt.fc_rcvd++;
1226 bus->flowcontrol = fcbits;
1229 /* Shouldn't be any others */
1230 if (hmb_data & ~(HMB_DATA_DEVREADY |
1231 HMB_DATA_NAKHANDLED |
1234 HMB_DATA_FCDATA_MASK | HMB_DATA_VERSION_MASK))
1235 brcmf_err("Unknown mailbox data content: 0x%02x\n",
1241 static void brcmf_sdio_rxfail(struct brcmf_sdio *bus, bool abort, bool rtx)
1248 brcmf_err("%sterminate frame%s\n",
1249 abort ? "abort command, " : "",
1250 rtx ? ", send NAK" : "");
1253 brcmf_sdiod_abort(bus->sdiodev, SDIO_FUNC_2);
1255 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL,
1257 bus->sdcnt.f1regdata++;
1259 /* Wait until the packet has been flushed (device/FIFO stable) */
1260 for (lastrbc = retries = 0xffff; retries > 0; retries--) {
1261 hi = brcmf_sdiod_regrb(bus->sdiodev,
1262 SBSDIO_FUNC1_RFRAMEBCHI, &err);
1263 lo = brcmf_sdiod_regrb(bus->sdiodev,
1264 SBSDIO_FUNC1_RFRAMEBCLO, &err);
1265 bus->sdcnt.f1regdata += 2;
1267 if ((hi == 0) && (lo == 0))
1270 if ((hi > (lastrbc >> 8)) && (lo > (lastrbc & 0x00ff))) {
1271 brcmf_err("count growing: last 0x%04x now 0x%04x\n",
1272 lastrbc, (hi << 8) + lo);
1274 lastrbc = (hi << 8) + lo;
1278 brcmf_err("count never zeroed: last 0x%04x\n", lastrbc);
1280 brcmf_dbg(SDIO, "flush took %d iterations\n", 0xffff - retries);
1284 err = w_sdreg32(bus, SMB_NAK,
1285 offsetof(struct sdpcmd_regs, tosbmailbox));
1287 bus->sdcnt.f1regdata++;
1292 /* Clear partial in any case */
1293 bus->cur_read.len = 0;
1296 static void brcmf_sdio_txfail(struct brcmf_sdio *bus)
1298 struct brcmf_sdio_dev *sdiodev = bus->sdiodev;
1301 /* On failure, abort the command and terminate the frame */
1302 brcmf_err("sdio error, abort command and terminate frame\n");
1303 bus->sdcnt.tx_sderrs++;
1305 brcmf_sdiod_abort(sdiodev, SDIO_FUNC_2);
1306 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_FRAMECTRL, SFC_WF_TERM, NULL);
1307 bus->sdcnt.f1regdata++;
1309 for (i = 0; i < 3; i++) {
1310 hi = brcmf_sdiod_regrb(sdiodev, SBSDIO_FUNC1_WFRAMEBCHI, NULL);
1311 lo = brcmf_sdiod_regrb(sdiodev, SBSDIO_FUNC1_WFRAMEBCLO, NULL);
1312 bus->sdcnt.f1regdata += 2;
1313 if ((hi == 0) && (lo == 0))
1318 /* return total length of buffer chain */
1319 static uint brcmf_sdio_glom_len(struct brcmf_sdio *bus)
1325 skb_queue_walk(&bus->glom, p)
1330 static void brcmf_sdio_free_glom(struct brcmf_sdio *bus)
1332 struct sk_buff *cur, *next;
1334 skb_queue_walk_safe(&bus->glom, cur, next) {
1335 skb_unlink(cur, &bus->glom);
1336 brcmu_pkt_buf_free_skb(cur);
1341 * brcmfmac sdio bus specific header
1342 * This is the lowest layer header wrapped on the packets transmitted between
1343 * host and WiFi dongle which contains information needed for SDIO core and
1346 * It consists of 3 parts: hardware header, hardware extension header and
1348 * hardware header (frame tag) - 4 bytes
1349 * Byte 0~1: Frame length
1350 * Byte 2~3: Checksum, bit-wise inverse of frame length
1351 * hardware extension header - 8 bytes
1352 * Tx glom mode only, N/A for Rx or normal Tx
1353 * Byte 0~1: Packet length excluding hw frame tag
1355 * Byte 3: Frame flags, bit 0: last frame indication
1356 * Byte 4~5: Reserved
1357 * Byte 6~7: Tail padding length
1358 * software header - 8 bytes
1359 * Byte 0: Rx/Tx sequence number
1360 * Byte 1: 4 MSB Channel number, 4 LSB arbitrary flag
1361 * Byte 2: Length of next data frame, reserved for Tx
1362 * Byte 3: Data offset
1363 * Byte 4: Flow control bits, reserved for Tx
1364 * Byte 5: Maximum Sequence number allowed by firmware for Tx, N/A for Tx packet
1365 * Byte 6~7: Reserved
1367 #define SDPCM_HWHDR_LEN 4
1368 #define SDPCM_HWEXT_LEN 8
1369 #define SDPCM_SWHDR_LEN 8
1370 #define SDPCM_HDRLEN (SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN)
1371 /* software header */
1372 #define SDPCM_SEQ_MASK 0x000000ff
1373 #define SDPCM_SEQ_WRAP 256
1374 #define SDPCM_CHANNEL_MASK 0x00000f00
1375 #define SDPCM_CHANNEL_SHIFT 8
1376 #define SDPCM_CONTROL_CHANNEL 0 /* Control */
1377 #define SDPCM_EVENT_CHANNEL 1 /* Asyc Event Indication */
1378 #define SDPCM_DATA_CHANNEL 2 /* Data Xmit/Recv */
1379 #define SDPCM_GLOM_CHANNEL 3 /* Coalesced packets */
1380 #define SDPCM_TEST_CHANNEL 15 /* Test/debug packets */
1381 #define SDPCM_GLOMDESC(p) (((u8 *)p)[1] & 0x80)
1382 #define SDPCM_NEXTLEN_MASK 0x00ff0000
1383 #define SDPCM_NEXTLEN_SHIFT 16
1384 #define SDPCM_DOFFSET_MASK 0xff000000
1385 #define SDPCM_DOFFSET_SHIFT 24
1386 #define SDPCM_FCMASK_MASK 0x000000ff
1387 #define SDPCM_WINDOW_MASK 0x0000ff00
1388 #define SDPCM_WINDOW_SHIFT 8
1390 static inline u8 brcmf_sdio_getdatoffset(u8 *swheader)
1393 hdrvalue = *(u32 *)swheader;
1394 return (u8)((hdrvalue & SDPCM_DOFFSET_MASK) >> SDPCM_DOFFSET_SHIFT);
1397 static inline bool brcmf_sdio_fromevntchan(u8 *swheader)
1402 hdrvalue = *(u32 *)swheader;
1403 ret = (u8)((hdrvalue & SDPCM_CHANNEL_MASK) >> SDPCM_CHANNEL_SHIFT);
1405 return (ret == SDPCM_EVENT_CHANNEL);
1408 static int brcmf_sdio_hdparse(struct brcmf_sdio *bus, u8 *header,
1409 struct brcmf_sdio_hdrinfo *rd,
1410 enum brcmf_sdio_frmtype type)
1413 u8 rx_seq, fc, tx_seq_max;
1416 trace_brcmf_sdpcm_hdr(SDPCM_RX, header);
1419 len = get_unaligned_le16(header);
1420 checksum = get_unaligned_le16(header + sizeof(u16));
1421 /* All zero means no more to read */
1422 if (!(len | checksum)) {
1423 bus->rxpending = false;
1426 if ((u16)(~(len ^ checksum))) {
1427 brcmf_err("HW header checksum error\n");
1428 bus->sdcnt.rx_badhdr++;
1429 brcmf_sdio_rxfail(bus, false, false);
1432 if (len < SDPCM_HDRLEN) {
1433 brcmf_err("HW header length error\n");
1436 if (type == BRCMF_SDIO_FT_SUPER &&
1437 (roundup(len, bus->blocksize) != rd->len)) {
1438 brcmf_err("HW superframe header length error\n");
1441 if (type == BRCMF_SDIO_FT_SUB && len > rd->len) {
1442 brcmf_err("HW subframe header length error\n");
1447 /* software header */
1448 header += SDPCM_HWHDR_LEN;
1449 swheader = le32_to_cpu(*(__le32 *)header);
1450 if (type == BRCMF_SDIO_FT_SUPER && SDPCM_GLOMDESC(header)) {
1451 brcmf_err("Glom descriptor found in superframe head\n");
1455 rx_seq = (u8)(swheader & SDPCM_SEQ_MASK);
1456 rd->channel = (swheader & SDPCM_CHANNEL_MASK) >> SDPCM_CHANNEL_SHIFT;
1457 if (len > MAX_RX_DATASZ && rd->channel != SDPCM_CONTROL_CHANNEL &&
1458 type != BRCMF_SDIO_FT_SUPER) {
1459 brcmf_err("HW header length too long\n");
1460 bus->sdcnt.rx_toolong++;
1461 brcmf_sdio_rxfail(bus, false, false);
1465 if (type == BRCMF_SDIO_FT_SUPER && rd->channel != SDPCM_GLOM_CHANNEL) {
1466 brcmf_err("Wrong channel for superframe\n");
1470 if (type == BRCMF_SDIO_FT_SUB && rd->channel != SDPCM_DATA_CHANNEL &&
1471 rd->channel != SDPCM_EVENT_CHANNEL) {
1472 brcmf_err("Wrong channel for subframe\n");
1476 rd->dat_offset = brcmf_sdio_getdatoffset(header);
1477 if (rd->dat_offset < SDPCM_HDRLEN || rd->dat_offset > rd->len) {
1478 brcmf_err("seq %d: bad data offset\n", rx_seq);
1479 bus->sdcnt.rx_badhdr++;
1480 brcmf_sdio_rxfail(bus, false, false);
1484 if (rd->seq_num != rx_seq) {
1485 brcmf_err("seq %d: sequence number error, expect %d\n",
1486 rx_seq, rd->seq_num);
1487 bus->sdcnt.rx_badseq++;
1488 rd->seq_num = rx_seq;
1490 /* no need to check the reset for subframe */
1491 if (type == BRCMF_SDIO_FT_SUB)
1493 rd->len_nxtfrm = (swheader & SDPCM_NEXTLEN_MASK) >> SDPCM_NEXTLEN_SHIFT;
1494 if (rd->len_nxtfrm << 4 > MAX_RX_DATASZ) {
1495 /* only warm for NON glom packet */
1496 if (rd->channel != SDPCM_GLOM_CHANNEL)
1497 brcmf_err("seq %d: next length error\n", rx_seq);
1500 swheader = le32_to_cpu(*(__le32 *)(header + 4));
1501 fc = swheader & SDPCM_FCMASK_MASK;
1502 if (bus->flowcontrol != fc) {
1503 if (~bus->flowcontrol & fc)
1504 bus->sdcnt.fc_xoff++;
1505 if (bus->flowcontrol & ~fc)
1506 bus->sdcnt.fc_xon++;
1507 bus->sdcnt.fc_rcvd++;
1508 bus->flowcontrol = fc;
1510 tx_seq_max = (swheader & SDPCM_WINDOW_MASK) >> SDPCM_WINDOW_SHIFT;
1511 if ((u8)(tx_seq_max - bus->tx_seq) > 0x40) {
1512 brcmf_err("seq %d: max tx seq number error\n", rx_seq);
1513 tx_seq_max = bus->tx_seq + 2;
1515 bus->tx_max = tx_seq_max;
1520 static inline void brcmf_sdio_update_hwhdr(u8 *header, u16 frm_length)
1522 *(__le16 *)header = cpu_to_le16(frm_length);
1523 *(((__le16 *)header) + 1) = cpu_to_le16(~frm_length);
1526 static void brcmf_sdio_hdpack(struct brcmf_sdio *bus, u8 *header,
1527 struct brcmf_sdio_hdrinfo *hd_info)
1532 brcmf_sdio_update_hwhdr(header, hd_info->len);
1533 hdr_offset = SDPCM_HWHDR_LEN;
1536 hdrval = (hd_info->len - hdr_offset) | (hd_info->lastfrm << 24);
1537 *((__le32 *)(header + hdr_offset)) = cpu_to_le32(hdrval);
1538 hdrval = (u16)hd_info->tail_pad << 16;
1539 *(((__le32 *)(header + hdr_offset)) + 1) = cpu_to_le32(hdrval);
1540 hdr_offset += SDPCM_HWEXT_LEN;
1543 hdrval = hd_info->seq_num;
1544 hdrval |= (hd_info->channel << SDPCM_CHANNEL_SHIFT) &
1546 hdrval |= (hd_info->dat_offset << SDPCM_DOFFSET_SHIFT) &
1548 *((__le32 *)(header + hdr_offset)) = cpu_to_le32(hdrval);
1549 *(((__le32 *)(header + hdr_offset)) + 1) = 0;
1550 trace_brcmf_sdpcm_hdr(SDPCM_TX + !!(bus->txglom), header);
1553 static u8 brcmf_sdio_rxglom(struct brcmf_sdio *bus, u8 rxseq)
1558 struct sk_buff *pfirst, *pnext;
1563 struct brcmf_sdio_hdrinfo rd_new;
1565 /* If packets, issue read(s) and send up packet chain */
1566 /* Return sequence numbers consumed? */
1568 brcmf_dbg(SDIO, "start: glomd %p glom %p\n",
1569 bus->glomd, skb_peek(&bus->glom));
1571 /* If there's a descriptor, generate the packet chain */
1573 pfirst = pnext = NULL;
1574 dlen = (u16) (bus->glomd->len);
1575 dptr = bus->glomd->data;
1576 if (!dlen || (dlen & 1)) {
1577 brcmf_err("bad glomd len(%d), ignore descriptor\n",
1582 for (totlen = num = 0; dlen; num++) {
1583 /* Get (and move past) next length */
1584 sublen = get_unaligned_le16(dptr);
1585 dlen -= sizeof(u16);
1586 dptr += sizeof(u16);
1587 if ((sublen < SDPCM_HDRLEN) ||
1588 ((num == 0) && (sublen < (2 * SDPCM_HDRLEN)))) {
1589 brcmf_err("descriptor len %d bad: %d\n",
1594 if (sublen % bus->sgentry_align) {
1595 brcmf_err("sublen %d not multiple of %d\n",
1596 sublen, bus->sgentry_align);
1600 /* For last frame, adjust read len so total
1601 is a block multiple */
1604 (roundup(totlen, bus->blocksize) - totlen);
1605 totlen = roundup(totlen, bus->blocksize);
1608 /* Allocate/chain packet for next subframe */
1609 pnext = brcmu_pkt_buf_get_skb(sublen + bus->sgentry_align);
1610 if (pnext == NULL) {
1611 brcmf_err("bcm_pkt_buf_get_skb failed, num %d len %d\n",
1615 skb_queue_tail(&bus->glom, pnext);
1617 /* Adhere to start alignment requirements */
1618 pkt_align(pnext, sublen, bus->sgentry_align);
1621 /* If all allocations succeeded, save packet chain
1624 brcmf_dbg(GLOM, "allocated %d-byte packet chain for %d subframes\n",
1626 if (BRCMF_GLOM_ON() && bus->cur_read.len &&
1627 totlen != bus->cur_read.len) {
1628 brcmf_dbg(GLOM, "glomdesc mismatch: nextlen %d glomdesc %d rxseq %d\n",
1629 bus->cur_read.len, totlen, rxseq);
1631 pfirst = pnext = NULL;
1633 brcmf_sdio_free_glom(bus);
1637 /* Done with descriptor packet */
1638 brcmu_pkt_buf_free_skb(bus->glomd);
1640 bus->cur_read.len = 0;
1643 /* Ok -- either we just generated a packet chain,
1644 or had one from before */
1645 if (!skb_queue_empty(&bus->glom)) {
1646 if (BRCMF_GLOM_ON()) {
1647 brcmf_dbg(GLOM, "try superframe read, packet chain:\n");
1648 skb_queue_walk(&bus->glom, pnext) {
1649 brcmf_dbg(GLOM, " %p: %p len 0x%04x (%d)\n",
1650 pnext, (u8 *) (pnext->data),
1651 pnext->len, pnext->len);
1655 pfirst = skb_peek(&bus->glom);
1656 dlen = (u16) brcmf_sdio_glom_len(bus);
1658 /* Do an SDIO read for the superframe. Configurable iovar to
1659 * read directly into the chained packet, or allocate a large
1660 * packet and and copy into the chain.
1662 sdio_claim_host(bus->sdiodev->func[1]);
1663 errcode = brcmf_sdiod_recv_chain(bus->sdiodev,
1665 sdio_release_host(bus->sdiodev->func[1]);
1666 bus->sdcnt.f2rxdata++;
1668 /* On failure, kill the superframe, allow a couple retries */
1670 brcmf_err("glom read of %d bytes failed: %d\n",
1673 sdio_claim_host(bus->sdiodev->func[1]);
1674 if (bus->glomerr++ < 3) {
1675 brcmf_sdio_rxfail(bus, true, true);
1678 brcmf_sdio_rxfail(bus, true, false);
1679 bus->sdcnt.rxglomfail++;
1680 brcmf_sdio_free_glom(bus);
1682 sdio_release_host(bus->sdiodev->func[1]);
1686 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
1687 pfirst->data, min_t(int, pfirst->len, 48),
1690 rd_new.seq_num = rxseq;
1692 sdio_claim_host(bus->sdiodev->func[1]);
1693 errcode = brcmf_sdio_hdparse(bus, pfirst->data, &rd_new,
1694 BRCMF_SDIO_FT_SUPER);
1695 sdio_release_host(bus->sdiodev->func[1]);
1696 bus->cur_read.len = rd_new.len_nxtfrm << 4;
1698 /* Remove superframe header, remember offset */
1699 skb_pull(pfirst, rd_new.dat_offset);
1700 sfdoff = rd_new.dat_offset;
1703 /* Validate all the subframe headers */
1704 skb_queue_walk(&bus->glom, pnext) {
1705 /* leave when invalid subframe is found */
1709 rd_new.len = pnext->len;
1710 rd_new.seq_num = rxseq++;
1711 sdio_claim_host(bus->sdiodev->func[1]);
1712 errcode = brcmf_sdio_hdparse(bus, pnext->data, &rd_new,
1714 sdio_release_host(bus->sdiodev->func[1]);
1715 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
1716 pnext->data, 32, "subframe:\n");
1722 /* Terminate frame on error, request
1724 sdio_claim_host(bus->sdiodev->func[1]);
1725 if (bus->glomerr++ < 3) {
1726 /* Restore superframe header space */
1727 skb_push(pfirst, sfdoff);
1728 brcmf_sdio_rxfail(bus, true, true);
1731 brcmf_sdio_rxfail(bus, true, false);
1732 bus->sdcnt.rxglomfail++;
1733 brcmf_sdio_free_glom(bus);
1735 sdio_release_host(bus->sdiodev->func[1]);
1736 bus->cur_read.len = 0;
1740 /* Basic SD framing looks ok - process each packet (header) */
1742 skb_queue_walk_safe(&bus->glom, pfirst, pnext) {
1743 dptr = (u8 *) (pfirst->data);
1744 sublen = get_unaligned_le16(dptr);
1745 doff = brcmf_sdio_getdatoffset(&dptr[SDPCM_HWHDR_LEN]);
1747 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_DATA_ON(),
1749 "Rx Subframe Data:\n");
1751 __skb_trim(pfirst, sublen);
1752 skb_pull(pfirst, doff);
1754 if (pfirst->len == 0) {
1755 skb_unlink(pfirst, &bus->glom);
1756 brcmu_pkt_buf_free_skb(pfirst);
1760 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
1762 min_t(int, pfirst->len, 32),
1763 "subframe %d to stack, %p (%p/%d) nxt/lnk %p/%p\n",
1764 bus->glom.qlen, pfirst, pfirst->data,
1765 pfirst->len, pfirst->next,
1767 skb_unlink(pfirst, &bus->glom);
1768 if (brcmf_sdio_fromevntchan(&dptr[SDPCM_HWHDR_LEN]))
1769 brcmf_rx_event(bus->sdiodev->dev, pfirst);
1771 brcmf_rx_frame(bus->sdiodev->dev, pfirst,
1773 bus->sdcnt.rxglompkts++;
1776 bus->sdcnt.rxglomframes++;
1781 static int brcmf_sdio_dcmd_resp_wait(struct brcmf_sdio *bus, uint *condition,
1784 DECLARE_WAITQUEUE(wait, current);
1785 int timeout = msecs_to_jiffies(DCMD_RESP_TIMEOUT);
1787 /* Wait until control frame is available */
1788 add_wait_queue(&bus->dcmd_resp_wait, &wait);
1789 set_current_state(TASK_INTERRUPTIBLE);
1791 while (!(*condition) && (!signal_pending(current) && timeout))
1792 timeout = schedule_timeout(timeout);
1794 if (signal_pending(current))
1797 set_current_state(TASK_RUNNING);
1798 remove_wait_queue(&bus->dcmd_resp_wait, &wait);
1803 static int brcmf_sdio_dcmd_resp_wake(struct brcmf_sdio *bus)
1805 if (waitqueue_active(&bus->dcmd_resp_wait))
1806 wake_up_interruptible(&bus->dcmd_resp_wait);
1811 brcmf_sdio_read_control(struct brcmf_sdio *bus, u8 *hdr, uint len, uint doff)
1814 u8 *buf = NULL, *rbuf;
1817 brcmf_dbg(TRACE, "Enter\n");
1820 buf = vzalloc(bus->rxblen);
1825 pad = ((unsigned long)rbuf % bus->head_align);
1827 rbuf += (bus->head_align - pad);
1829 /* Copy the already-read portion over */
1830 memcpy(buf, hdr, BRCMF_FIRSTREAD);
1831 if (len <= BRCMF_FIRSTREAD)
1834 /* Raise rdlen to next SDIO block to avoid tail command */
1835 rdlen = len - BRCMF_FIRSTREAD;
1836 if (bus->roundup && bus->blocksize && (rdlen > bus->blocksize)) {
1837 pad = bus->blocksize - (rdlen % bus->blocksize);
1838 if ((pad <= bus->roundup) && (pad < bus->blocksize) &&
1839 ((len + pad) < bus->sdiodev->bus_if->maxctl))
1841 } else if (rdlen % bus->head_align) {
1842 rdlen += bus->head_align - (rdlen % bus->head_align);
1845 /* Drop if the read is too big or it exceeds our maximum */
1846 if ((rdlen + BRCMF_FIRSTREAD) > bus->sdiodev->bus_if->maxctl) {
1847 brcmf_err("%d-byte control read exceeds %d-byte buffer\n",
1848 rdlen, bus->sdiodev->bus_if->maxctl);
1849 brcmf_sdio_rxfail(bus, false, false);
1853 if ((len - doff) > bus->sdiodev->bus_if->maxctl) {
1854 brcmf_err("%d-byte ctl frame (%d-byte ctl data) exceeds %d-byte limit\n",
1855 len, len - doff, bus->sdiodev->bus_if->maxctl);
1856 bus->sdcnt.rx_toolong++;
1857 brcmf_sdio_rxfail(bus, false, false);
1861 /* Read remain of frame body */
1862 sdret = brcmf_sdiod_recv_buf(bus->sdiodev, rbuf, rdlen);
1863 bus->sdcnt.f2rxdata++;
1865 /* Control frame failures need retransmission */
1867 brcmf_err("read %d control bytes failed: %d\n",
1869 bus->sdcnt.rxc_errors++;
1870 brcmf_sdio_rxfail(bus, true, true);
1873 memcpy(buf + BRCMF_FIRSTREAD, rbuf, rdlen);
1877 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_CTL_ON(),
1878 buf, len, "RxCtrl:\n");
1880 /* Point to valid data and indicate its length */
1881 spin_lock_bh(&bus->rxctl_lock);
1883 brcmf_err("last control frame is being processed.\n");
1884 spin_unlock_bh(&bus->rxctl_lock);
1888 bus->rxctl = buf + doff;
1889 bus->rxctl_orig = buf;
1890 bus->rxlen = len - doff;
1891 spin_unlock_bh(&bus->rxctl_lock);
1894 /* Awake any waiters */
1895 brcmf_sdio_dcmd_resp_wake(bus);
1898 /* Pad read to blocksize for efficiency */
1899 static void brcmf_sdio_pad(struct brcmf_sdio *bus, u16 *pad, u16 *rdlen)
1901 if (bus->roundup && bus->blocksize && *rdlen > bus->blocksize) {
1902 *pad = bus->blocksize - (*rdlen % bus->blocksize);
1903 if (*pad <= bus->roundup && *pad < bus->blocksize &&
1904 *rdlen + *pad + BRCMF_FIRSTREAD < MAX_RX_DATASZ)
1906 } else if (*rdlen % bus->head_align) {
1907 *rdlen += bus->head_align - (*rdlen % bus->head_align);
1911 static uint brcmf_sdio_readframes(struct brcmf_sdio *bus, uint maxframes)
1913 struct sk_buff *pkt; /* Packet for event or data frames */
1914 u16 pad; /* Number of pad bytes to read */
1915 uint rxleft = 0; /* Remaining number of frames allowed */
1916 int ret; /* Return code from calls */
1917 uint rxcount = 0; /* Total frames read */
1918 struct brcmf_sdio_hdrinfo *rd = &bus->cur_read, rd_new;
1921 brcmf_dbg(TRACE, "Enter\n");
1923 /* Not finished unless we encounter no more frames indication */
1924 bus->rxpending = true;
1926 for (rd->seq_num = bus->rx_seq, rxleft = maxframes;
1927 !bus->rxskip && rxleft && bus->sdiodev->state == BRCMF_SDIOD_DATA;
1928 rd->seq_num++, rxleft--) {
1930 /* Handle glomming separately */
1931 if (bus->glomd || !skb_queue_empty(&bus->glom)) {
1933 brcmf_dbg(GLOM, "calling rxglom: glomd %p, glom %p\n",
1934 bus->glomd, skb_peek(&bus->glom));
1935 cnt = brcmf_sdio_rxglom(bus, rd->seq_num);
1936 brcmf_dbg(GLOM, "rxglom returned %d\n", cnt);
1937 rd->seq_num += cnt - 1;
1938 rxleft = (rxleft > cnt) ? (rxleft - cnt) : 1;
1942 rd->len_left = rd->len;
1943 /* read header first for unknow frame length */
1944 sdio_claim_host(bus->sdiodev->func[1]);
1946 ret = brcmf_sdiod_recv_buf(bus->sdiodev,
1947 bus->rxhdr, BRCMF_FIRSTREAD);
1948 bus->sdcnt.f2rxhdrs++;
1950 brcmf_err("RXHEADER FAILED: %d\n",
1952 bus->sdcnt.rx_hdrfail++;
1953 brcmf_sdio_rxfail(bus, true, true);
1954 sdio_release_host(bus->sdiodev->func[1]);
1958 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() || BRCMF_HDRS_ON(),
1959 bus->rxhdr, SDPCM_HDRLEN,
1962 if (brcmf_sdio_hdparse(bus, bus->rxhdr, rd,
1963 BRCMF_SDIO_FT_NORMAL)) {
1964 sdio_release_host(bus->sdiodev->func[1]);
1965 if (!bus->rxpending)
1971 if (rd->channel == SDPCM_CONTROL_CHANNEL) {
1972 brcmf_sdio_read_control(bus, bus->rxhdr,
1975 /* prepare the descriptor for the next read */
1976 rd->len = rd->len_nxtfrm << 4;
1978 /* treat all packet as event if we don't know */
1979 rd->channel = SDPCM_EVENT_CHANNEL;
1980 sdio_release_host(bus->sdiodev->func[1]);
1983 rd->len_left = rd->len > BRCMF_FIRSTREAD ?
1984 rd->len - BRCMF_FIRSTREAD : 0;
1985 head_read = BRCMF_FIRSTREAD;
1988 brcmf_sdio_pad(bus, &pad, &rd->len_left);
1990 pkt = brcmu_pkt_buf_get_skb(rd->len_left + head_read +
1993 /* Give up on data, request rtx of events */
1994 brcmf_err("brcmu_pkt_buf_get_skb failed\n");
1995 brcmf_sdio_rxfail(bus, false,
1996 RETRYCHAN(rd->channel));
1997 sdio_release_host(bus->sdiodev->func[1]);
2000 skb_pull(pkt, head_read);
2001 pkt_align(pkt, rd->len_left, bus->head_align);
2003 ret = brcmf_sdiod_recv_pkt(bus->sdiodev, pkt);
2004 bus->sdcnt.f2rxdata++;
2005 sdio_release_host(bus->sdiodev->func[1]);
2008 brcmf_err("read %d bytes from channel %d failed: %d\n",
2009 rd->len, rd->channel, ret);
2010 brcmu_pkt_buf_free_skb(pkt);
2011 sdio_claim_host(bus->sdiodev->func[1]);
2012 brcmf_sdio_rxfail(bus, true,
2013 RETRYCHAN(rd->channel));
2014 sdio_release_host(bus->sdiodev->func[1]);
2019 skb_push(pkt, head_read);
2020 memcpy(pkt->data, bus->rxhdr, head_read);
2023 memcpy(bus->rxhdr, pkt->data, SDPCM_HDRLEN);
2024 rd_new.seq_num = rd->seq_num;
2025 sdio_claim_host(bus->sdiodev->func[1]);
2026 if (brcmf_sdio_hdparse(bus, bus->rxhdr, &rd_new,
2027 BRCMF_SDIO_FT_NORMAL)) {
2029 brcmu_pkt_buf_free_skb(pkt);
2031 bus->sdcnt.rx_readahead_cnt++;
2032 if (rd->len != roundup(rd_new.len, 16)) {
2033 brcmf_err("frame length mismatch:read %d, should be %d\n",
2035 roundup(rd_new.len, 16) >> 4);
2037 brcmf_sdio_rxfail(bus, true, true);
2038 sdio_release_host(bus->sdiodev->func[1]);
2039 brcmu_pkt_buf_free_skb(pkt);
2042 sdio_release_host(bus->sdiodev->func[1]);
2043 rd->len_nxtfrm = rd_new.len_nxtfrm;
2044 rd->channel = rd_new.channel;
2045 rd->dat_offset = rd_new.dat_offset;
2047 brcmf_dbg_hex_dump(!(BRCMF_BYTES_ON() &&
2050 bus->rxhdr, SDPCM_HDRLEN,
2053 if (rd_new.channel == SDPCM_CONTROL_CHANNEL) {
2054 brcmf_err("readahead on control packet %d?\n",
2056 /* Force retry w/normal header read */
2058 sdio_claim_host(bus->sdiodev->func[1]);
2059 brcmf_sdio_rxfail(bus, false, true);
2060 sdio_release_host(bus->sdiodev->func[1]);
2061 brcmu_pkt_buf_free_skb(pkt);
2066 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_DATA_ON(),
2067 pkt->data, rd->len, "Rx Data:\n");
2069 /* Save superframe descriptor and allocate packet frame */
2070 if (rd->channel == SDPCM_GLOM_CHANNEL) {
2071 if (SDPCM_GLOMDESC(&bus->rxhdr[SDPCM_HWHDR_LEN])) {
2072 brcmf_dbg(GLOM, "glom descriptor, %d bytes:\n",
2074 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
2077 __skb_trim(pkt, rd->len);
2078 skb_pull(pkt, SDPCM_HDRLEN);
2081 brcmf_err("%s: glom superframe w/o "
2082 "descriptor!\n", __func__);
2083 sdio_claim_host(bus->sdiodev->func[1]);
2084 brcmf_sdio_rxfail(bus, false, false);
2085 sdio_release_host(bus->sdiodev->func[1]);
2087 /* prepare the descriptor for the next read */
2088 rd->len = rd->len_nxtfrm << 4;
2090 /* treat all packet as event if we don't know */
2091 rd->channel = SDPCM_EVENT_CHANNEL;
2095 /* Fill in packet len and prio, deliver upward */
2096 __skb_trim(pkt, rd->len);
2097 skb_pull(pkt, rd->dat_offset);
2100 brcmu_pkt_buf_free_skb(pkt);
2101 else if (rd->channel == SDPCM_EVENT_CHANNEL)
2102 brcmf_rx_event(bus->sdiodev->dev, pkt);
2104 brcmf_rx_frame(bus->sdiodev->dev, pkt,
2107 /* prepare the descriptor for the next read */
2108 rd->len = rd->len_nxtfrm << 4;
2110 /* treat all packet as event if we don't know */
2111 rd->channel = SDPCM_EVENT_CHANNEL;
2114 rxcount = maxframes - rxleft;
2115 /* Message if we hit the limit */
2117 brcmf_dbg(DATA, "hit rx limit of %d frames\n", maxframes);
2119 brcmf_dbg(DATA, "processed %d frames\n", rxcount);
2120 /* Back off rxseq if awaiting rtx, update rx_seq */
2123 bus->rx_seq = rd->seq_num;
2129 brcmf_sdio_wait_event_wakeup(struct brcmf_sdio *bus)
2131 if (waitqueue_active(&bus->ctrl_wait))
2132 wake_up_interruptible(&bus->ctrl_wait);
2136 static int brcmf_sdio_txpkt_hdalign(struct brcmf_sdio *bus, struct sk_buff *pkt)
2141 dat_buf = (u8 *)(pkt->data);
2143 /* Check head padding */
2144 head_pad = ((unsigned long)dat_buf % bus->head_align);
2146 if (skb_headroom(pkt) < head_pad) {
2147 bus->sdiodev->bus_if->tx_realloc++;
2149 if (skb_cow(pkt, head_pad))
2152 skb_push(pkt, head_pad);
2153 dat_buf = (u8 *)(pkt->data);
2154 memset(dat_buf, 0, head_pad + bus->tx_hdrlen);
2160 * struct brcmf_skbuff_cb reserves first two bytes in sk_buff::cb for
2163 /* flag marking a dummy skb added for DMA alignment requirement */
2164 #define ALIGN_SKB_FLAG 0x8000
2165 /* bit mask of data length chopped from the previous packet */
2166 #define ALIGN_SKB_CHOP_LEN_MASK 0x7fff
2168 static int brcmf_sdio_txpkt_prep_sg(struct brcmf_sdio *bus,
2169 struct sk_buff_head *pktq,
2170 struct sk_buff *pkt, u16 total_len)
2172 struct brcmf_sdio_dev *sdiodev;
2173 struct sk_buff *pkt_pad;
2174 u16 tail_pad, tail_chop, chain_pad;
2175 unsigned int blksize;
2179 sdiodev = bus->sdiodev;
2180 blksize = sdiodev->func[SDIO_FUNC_2]->cur_blksize;
2181 /* sg entry alignment should be a divisor of block size */
2182 WARN_ON(blksize % bus->sgentry_align);
2184 /* Check tail padding */
2185 lastfrm = skb_queue_is_last(pktq, pkt);
2187 tail_chop = pkt->len % bus->sgentry_align;
2189 tail_pad = bus->sgentry_align - tail_chop;
2190 chain_pad = (total_len + tail_pad) % blksize;
2191 if (lastfrm && chain_pad)
2192 tail_pad += blksize - chain_pad;
2193 if (skb_tailroom(pkt) < tail_pad && pkt->len > blksize) {
2194 pkt_pad = brcmu_pkt_buf_get_skb(tail_pad + tail_chop +
2196 if (pkt_pad == NULL)
2198 ret = brcmf_sdio_txpkt_hdalign(bus, pkt_pad);
2199 if (unlikely(ret < 0)) {
2203 memcpy(pkt_pad->data,
2204 pkt->data + pkt->len - tail_chop,
2206 *(u16 *)(pkt_pad->cb) = ALIGN_SKB_FLAG + tail_chop;
2207 skb_trim(pkt, pkt->len - tail_chop);
2208 skb_trim(pkt_pad, tail_pad + tail_chop);
2209 __skb_queue_after(pktq, pkt, pkt_pad);
2211 ntail = pkt->data_len + tail_pad -
2212 (pkt->end - pkt->tail);
2213 if (skb_cloned(pkt) || ntail > 0)
2214 if (pskb_expand_head(pkt, 0, ntail, GFP_ATOMIC))
2216 if (skb_linearize(pkt))
2218 __skb_put(pkt, tail_pad);
2225 * brcmf_sdio_txpkt_prep - packet preparation for transmit
2226 * @bus: brcmf_sdio structure pointer
2227 * @pktq: packet list pointer
2228 * @chan: virtual channel to transmit the packet
2230 * Processes to be applied to the packet
2231 * - Align data buffer pointer
2232 * - Align data buffer length
2234 * Return: negative value if there is error
2237 brcmf_sdio_txpkt_prep(struct brcmf_sdio *bus, struct sk_buff_head *pktq,
2240 u16 head_pad, total_len;
2241 struct sk_buff *pkt_next;
2244 struct brcmf_sdio_hdrinfo hd_info = {0};
2246 txseq = bus->tx_seq;
2248 skb_queue_walk(pktq, pkt_next) {
2249 /* alignment packet inserted in previous
2250 * loop cycle can be skipped as it is
2251 * already properly aligned and does not
2252 * need an sdpcm header.
2254 if (*(u16 *)(pkt_next->cb) & ALIGN_SKB_FLAG)
2257 /* align packet data pointer */
2258 ret = brcmf_sdio_txpkt_hdalign(bus, pkt_next);
2261 head_pad = (u16)ret;
2263 memset(pkt_next->data + bus->tx_hdrlen, 0, head_pad);
2265 total_len += pkt_next->len;
2267 hd_info.len = pkt_next->len;
2268 hd_info.lastfrm = skb_queue_is_last(pktq, pkt_next);
2269 if (bus->txglom && pktq->qlen > 1) {
2270 ret = brcmf_sdio_txpkt_prep_sg(bus, pktq,
2271 pkt_next, total_len);
2274 hd_info.tail_pad = (u16)ret;
2275 total_len += (u16)ret;
2278 hd_info.channel = chan;
2279 hd_info.dat_offset = head_pad + bus->tx_hdrlen;
2280 hd_info.seq_num = txseq++;
2282 /* Now fill the header */
2283 brcmf_sdio_hdpack(bus, pkt_next->data, &hd_info);
2285 if (BRCMF_BYTES_ON() &&
2286 ((BRCMF_CTL_ON() && chan == SDPCM_CONTROL_CHANNEL) ||
2287 (BRCMF_DATA_ON() && chan != SDPCM_CONTROL_CHANNEL)))
2288 brcmf_dbg_hex_dump(true, pkt_next->data, hd_info.len,
2290 else if (BRCMF_HDRS_ON())
2291 brcmf_dbg_hex_dump(true, pkt_next->data,
2292 head_pad + bus->tx_hdrlen,
2295 /* Hardware length tag of the first packet should be total
2296 * length of the chain (including padding)
2299 brcmf_sdio_update_hwhdr(pktq->next->data, total_len);
2304 * brcmf_sdio_txpkt_postp - packet post processing for transmit
2305 * @bus: brcmf_sdio structure pointer
2306 * @pktq: packet list pointer
2308 * Processes to be applied to the packet
2309 * - Remove head padding
2310 * - Remove tail padding
2313 brcmf_sdio_txpkt_postp(struct brcmf_sdio *bus, struct sk_buff_head *pktq)
2318 u16 dummy_flags, chop_len;
2319 struct sk_buff *pkt_next, *tmp, *pkt_prev;
2321 skb_queue_walk_safe(pktq, pkt_next, tmp) {
2322 dummy_flags = *(u16 *)(pkt_next->cb);
2323 if (dummy_flags & ALIGN_SKB_FLAG) {
2324 chop_len = dummy_flags & ALIGN_SKB_CHOP_LEN_MASK;
2326 pkt_prev = pkt_next->prev;
2327 skb_put(pkt_prev, chop_len);
2329 __skb_unlink(pkt_next, pktq);
2330 brcmu_pkt_buf_free_skb(pkt_next);
2332 hdr = pkt_next->data + bus->tx_hdrlen - SDPCM_SWHDR_LEN;
2333 dat_offset = le32_to_cpu(*(__le32 *)hdr);
2334 dat_offset = (dat_offset & SDPCM_DOFFSET_MASK) >>
2335 SDPCM_DOFFSET_SHIFT;
2336 skb_pull(pkt_next, dat_offset);
2338 tail_pad = le16_to_cpu(*(__le16 *)(hdr - 2));
2339 skb_trim(pkt_next, pkt_next->len - tail_pad);
2345 /* Writes a HW/SW header into the packet and sends it. */
2346 /* Assumes: (a) header space already there, (b) caller holds lock */
2347 static int brcmf_sdio_txpkt(struct brcmf_sdio *bus, struct sk_buff_head *pktq,
2351 struct sk_buff *pkt_next, *tmp;
2353 brcmf_dbg(TRACE, "Enter\n");
2355 ret = brcmf_sdio_txpkt_prep(bus, pktq, chan);
2359 sdio_claim_host(bus->sdiodev->func[1]);
2360 ret = brcmf_sdiod_send_pkt(bus->sdiodev, pktq);
2361 bus->sdcnt.f2txdata++;
2364 brcmf_sdio_txfail(bus);
2366 sdio_release_host(bus->sdiodev->func[1]);
2369 brcmf_sdio_txpkt_postp(bus, pktq);
2371 bus->tx_seq = (bus->tx_seq + pktq->qlen) % SDPCM_SEQ_WRAP;
2372 skb_queue_walk_safe(pktq, pkt_next, tmp) {
2373 __skb_unlink(pkt_next, pktq);
2374 brcmf_txcomplete(bus->sdiodev->dev, pkt_next, ret == 0);
2379 static uint brcmf_sdio_sendfromq(struct brcmf_sdio *bus, uint maxframes)
2381 struct sk_buff *pkt;
2382 struct sk_buff_head pktq;
2384 int ret = 0, prec_out, i;
2386 u8 tx_prec_map, pkt_num;
2388 brcmf_dbg(TRACE, "Enter\n");
2390 tx_prec_map = ~bus->flowcontrol;
2392 /* Send frames until the limit or some other event */
2393 for (cnt = 0; (cnt < maxframes) && data_ok(bus);) {
2396 pkt_num = min_t(u8, bus->tx_max - bus->tx_seq,
2397 bus->sdiodev->txglomsz);
2398 pkt_num = min_t(u32, pkt_num,
2399 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol));
2400 __skb_queue_head_init(&pktq);
2401 spin_lock_bh(&bus->txq_lock);
2402 for (i = 0; i < pkt_num; i++) {
2403 pkt = brcmu_pktq_mdeq(&bus->txq, tx_prec_map,
2407 __skb_queue_tail(&pktq, pkt);
2409 spin_unlock_bh(&bus->txq_lock);
2413 ret = brcmf_sdio_txpkt(bus, &pktq, SDPCM_DATA_CHANNEL);
2417 /* In poll mode, need to check for other events */
2419 /* Check device status, signal pending interrupt */
2420 sdio_claim_host(bus->sdiodev->func[1]);
2421 ret = r_sdreg32(bus, &intstatus,
2422 offsetof(struct sdpcmd_regs,
2424 sdio_release_host(bus->sdiodev->func[1]);
2425 bus->sdcnt.f2txdata++;
2428 if (intstatus & bus->hostintmask)
2429 atomic_set(&bus->ipend, 1);
2433 /* Deflow-control stack if needed */
2434 if ((bus->sdiodev->state == BRCMF_SDIOD_DATA) &&
2435 bus->txoff && (pktq_len(&bus->txq) < TXLOW)) {
2437 brcmf_txflowblock(bus->sdiodev->dev, false);
2443 static int brcmf_sdio_tx_ctrlframe(struct brcmf_sdio *bus, u8 *frame, u16 len)
2448 struct brcmf_sdio_hdrinfo hd_info = {0};
2451 brcmf_dbg(TRACE, "Enter\n");
2453 /* Back the pointer to make room for bus header */
2454 frame -= bus->tx_hdrlen;
2455 len += bus->tx_hdrlen;
2457 /* Add alignment padding (optional for ctl frames) */
2458 doff = ((unsigned long)frame % bus->head_align);
2462 memset(frame + bus->tx_hdrlen, 0, doff);
2465 /* Round send length to next SDIO block */
2467 if (bus->roundup && bus->blocksize && (len > bus->blocksize)) {
2468 pad = bus->blocksize - (len % bus->blocksize);
2469 if ((pad > bus->roundup) || (pad >= bus->blocksize))
2471 } else if (len % bus->head_align) {
2472 pad = bus->head_align - (len % bus->head_align);
2476 hd_info.len = len - pad;
2477 hd_info.channel = SDPCM_CONTROL_CHANNEL;
2478 hd_info.dat_offset = doff + bus->tx_hdrlen;
2479 hd_info.seq_num = bus->tx_seq;
2480 hd_info.lastfrm = true;
2481 hd_info.tail_pad = pad;
2482 brcmf_sdio_hdpack(bus, frame, &hd_info);
2485 brcmf_sdio_update_hwhdr(frame, len);
2487 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_CTL_ON(),
2488 frame, len, "Tx Frame:\n");
2489 brcmf_dbg_hex_dump(!(BRCMF_BYTES_ON() && BRCMF_CTL_ON()) &&
2491 frame, min_t(u16, len, 16), "TxHdr:\n");
2494 ret = brcmf_sdiod_send_buf(bus->sdiodev, frame, len);
2497 brcmf_sdio_txfail(bus);
2499 bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQ_WRAP;
2500 } while (ret < 0 && retries++ < TXRETRIES);
2505 static void brcmf_sdio_bus_stop(struct device *dev)
2507 u32 local_hostintmask;
2510 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
2511 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
2512 struct brcmf_sdio *bus = sdiodev->bus;
2514 brcmf_dbg(TRACE, "Enter\n");
2516 if (bus->watchdog_tsk) {
2517 send_sig(SIGTERM, bus->watchdog_tsk, 1);
2518 kthread_stop(bus->watchdog_tsk);
2519 bus->watchdog_tsk = NULL;
2522 if (sdiodev->state != BRCMF_SDIOD_NOMEDIUM) {
2523 sdio_claim_host(sdiodev->func[1]);
2525 /* Enable clock for device interrupts */
2526 brcmf_sdio_bus_sleep(bus, false, false);
2528 /* Disable and clear interrupts at the chip level also */
2529 w_sdreg32(bus, 0, offsetof(struct sdpcmd_regs, hostintmask));
2530 local_hostintmask = bus->hostintmask;
2531 bus->hostintmask = 0;
2533 /* Force backplane clocks to assure F2 interrupt propagates */
2534 saveclk = brcmf_sdiod_regrb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
2537 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
2538 (saveclk | SBSDIO_FORCE_HT), &err);
2540 brcmf_err("Failed to force clock for F2: err %d\n",
2543 /* Turn off the bus (F2), free any pending packets */
2544 brcmf_dbg(INTR, "disable SDIO interrupts\n");
2545 sdio_disable_func(sdiodev->func[SDIO_FUNC_2]);
2547 /* Clear any pending interrupts now that F2 is disabled */
2548 w_sdreg32(bus, local_hostintmask,
2549 offsetof(struct sdpcmd_regs, intstatus));
2551 sdio_release_host(sdiodev->func[1]);
2553 /* Clear the data packet queues */
2554 brcmu_pktq_flush(&bus->txq, true, NULL, NULL);
2556 /* Clear any held glomming stuff */
2557 brcmu_pkt_buf_free_skb(bus->glomd);
2558 brcmf_sdio_free_glom(bus);
2560 /* Clear rx control and wake any waiters */
2561 spin_lock_bh(&bus->rxctl_lock);
2563 spin_unlock_bh(&bus->rxctl_lock);
2564 brcmf_sdio_dcmd_resp_wake(bus);
2566 /* Reset some F2 state stuff */
2567 bus->rxskip = false;
2568 bus->tx_seq = bus->rx_seq = 0;
2571 static inline void brcmf_sdio_clrintr(struct brcmf_sdio *bus)
2573 unsigned long flags;
2575 if (bus->sdiodev->oob_irq_requested) {
2576 spin_lock_irqsave(&bus->sdiodev->irq_en_lock, flags);
2577 if (!bus->sdiodev->irq_en && !atomic_read(&bus->ipend)) {
2578 enable_irq(bus->sdiodev->pdata->oob_irq_nr);
2579 bus->sdiodev->irq_en = true;
2581 spin_unlock_irqrestore(&bus->sdiodev->irq_en_lock, flags);
2585 static int brcmf_sdio_intr_rstatus(struct brcmf_sdio *bus)
2587 struct brcmf_core *buscore;
2592 buscore = brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV);
2593 addr = buscore->base + offsetof(struct sdpcmd_regs, intstatus);
2595 val = brcmf_sdiod_regrl(bus->sdiodev, addr, &ret);
2596 bus->sdcnt.f1regdata++;
2600 val &= bus->hostintmask;
2601 atomic_set(&bus->fcstate, !!(val & I_HMB_FC_STATE));
2603 /* Clear interrupts */
2605 brcmf_sdiod_regwl(bus->sdiodev, addr, val, &ret);
2606 bus->sdcnt.f1regdata++;
2607 atomic_or(val, &bus->intstatus);
2613 static void brcmf_sdio_dpc(struct brcmf_sdio *bus)
2616 unsigned long intstatus;
2617 uint txlimit = bus->txbound; /* Tx frames to send before resched */
2618 uint framecnt; /* Temporary counter of tx/rx frames */
2621 brcmf_dbg(TRACE, "Enter\n");
2623 sdio_claim_host(bus->sdiodev->func[1]);
2625 /* If waiting for HTAVAIL, check status */
2626 if (!bus->sr_enabled && bus->clkstate == CLK_PENDING) {
2627 u8 clkctl, devctl = 0;
2630 /* Check for inconsistent device control */
2631 devctl = brcmf_sdiod_regrb(bus->sdiodev,
2632 SBSDIO_DEVICE_CTL, &err);
2635 /* Read CSR, if clock on switch to AVAIL, else ignore */
2636 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
2637 SBSDIO_FUNC1_CHIPCLKCSR, &err);
2639 brcmf_dbg(SDIO, "DPC: PENDING, devctl 0x%02x clkctl 0x%02x\n",
2642 if (SBSDIO_HTAV(clkctl)) {
2643 devctl = brcmf_sdiod_regrb(bus->sdiodev,
2644 SBSDIO_DEVICE_CTL, &err);
2645 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
2646 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
2648 bus->clkstate = CLK_AVAIL;
2652 /* Make sure backplane clock is on */
2653 brcmf_sdio_bus_sleep(bus, false, true);
2655 /* Pending interrupt indicates new device status */
2656 if (atomic_read(&bus->ipend) > 0) {
2657 atomic_set(&bus->ipend, 0);
2658 err = brcmf_sdio_intr_rstatus(bus);
2661 /* Start with leftover status bits */
2662 intstatus = atomic_xchg(&bus->intstatus, 0);
2664 /* Handle flow-control change: read new state in case our ack
2665 * crossed another change interrupt. If change still set, assume
2666 * FC ON for safety, let next loop through do the debounce.
2668 if (intstatus & I_HMB_FC_CHANGE) {
2669 intstatus &= ~I_HMB_FC_CHANGE;
2670 err = w_sdreg32(bus, I_HMB_FC_CHANGE,
2671 offsetof(struct sdpcmd_regs, intstatus));
2673 err = r_sdreg32(bus, &newstatus,
2674 offsetof(struct sdpcmd_regs, intstatus));
2675 bus->sdcnt.f1regdata += 2;
2676 atomic_set(&bus->fcstate,
2677 !!(newstatus & (I_HMB_FC_STATE | I_HMB_FC_CHANGE)));
2678 intstatus |= (newstatus & bus->hostintmask);
2681 /* Handle host mailbox indication */
2682 if (intstatus & I_HMB_HOST_INT) {
2683 intstatus &= ~I_HMB_HOST_INT;
2684 intstatus |= brcmf_sdio_hostmail(bus);
2687 sdio_release_host(bus->sdiodev->func[1]);
2689 /* Generally don't ask for these, can get CRC errors... */
2690 if (intstatus & I_WR_OOSYNC) {
2691 brcmf_err("Dongle reports WR_OOSYNC\n");
2692 intstatus &= ~I_WR_OOSYNC;
2695 if (intstatus & I_RD_OOSYNC) {
2696 brcmf_err("Dongle reports RD_OOSYNC\n");
2697 intstatus &= ~I_RD_OOSYNC;
2700 if (intstatus & I_SBINT) {
2701 brcmf_err("Dongle reports SBINT\n");
2702 intstatus &= ~I_SBINT;
2705 /* Would be active due to wake-wlan in gSPI */
2706 if (intstatus & I_CHIPACTIVE) {
2707 brcmf_dbg(INFO, "Dongle reports CHIPACTIVE\n");
2708 intstatus &= ~I_CHIPACTIVE;
2711 /* Ignore frame indications if rxskip is set */
2713 intstatus &= ~I_HMB_FRAME_IND;
2715 /* On frame indication, read available frames */
2716 if ((intstatus & I_HMB_FRAME_IND) && (bus->clkstate == CLK_AVAIL)) {
2717 brcmf_sdio_readframes(bus, bus->rxbound);
2718 if (!bus->rxpending)
2719 intstatus &= ~I_HMB_FRAME_IND;
2722 /* Keep still-pending events for next scheduling */
2724 atomic_or(intstatus, &bus->intstatus);
2726 brcmf_sdio_clrintr(bus);
2728 if (bus->ctrl_frame_stat && (bus->clkstate == CLK_AVAIL) &&
2730 sdio_claim_host(bus->sdiodev->func[1]);
2731 if (bus->ctrl_frame_stat) {
2732 err = brcmf_sdio_tx_ctrlframe(bus, bus->ctrl_frame_buf,
2733 bus->ctrl_frame_len);
2734 bus->ctrl_frame_err = err;
2736 bus->ctrl_frame_stat = false;
2738 sdio_release_host(bus->sdiodev->func[1]);
2739 brcmf_sdio_wait_event_wakeup(bus);
2741 /* Send queued frames (limit 1 if rx may still be pending) */
2742 if ((bus->clkstate == CLK_AVAIL) && !atomic_read(&bus->fcstate) &&
2743 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) && txlimit &&
2745 framecnt = bus->rxpending ? min(txlimit, bus->txminmax) :
2747 brcmf_sdio_sendfromq(bus, framecnt);
2750 if ((bus->sdiodev->state != BRCMF_SDIOD_DATA) || (err != 0)) {
2751 brcmf_err("failed backplane access over SDIO, halting operation\n");
2752 atomic_set(&bus->intstatus, 0);
2753 if (bus->ctrl_frame_stat) {
2754 sdio_claim_host(bus->sdiodev->func[1]);
2755 if (bus->ctrl_frame_stat) {
2756 bus->ctrl_frame_err = -ENODEV;
2758 bus->ctrl_frame_stat = false;
2759 brcmf_sdio_wait_event_wakeup(bus);
2761 sdio_release_host(bus->sdiodev->func[1]);
2763 } else if (atomic_read(&bus->intstatus) ||
2764 atomic_read(&bus->ipend) > 0 ||
2765 (!atomic_read(&bus->fcstate) &&
2766 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) &&
2768 bus->dpc_triggered = true;
2772 static struct pktq *brcmf_sdio_bus_gettxq(struct device *dev)
2774 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
2775 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
2776 struct brcmf_sdio *bus = sdiodev->bus;
2781 static bool brcmf_sdio_prec_enq(struct pktq *q, struct sk_buff *pkt, int prec)
2784 int eprec = -1; /* precedence to evict from */
2786 /* Fast case, precedence queue is not full and we are also not
2787 * exceeding total queue length
2789 if (!pktq_pfull(q, prec) && !pktq_full(q)) {
2790 brcmu_pktq_penq(q, prec, pkt);
2794 /* Determine precedence from which to evict packet, if any */
2795 if (pktq_pfull(q, prec)) {
2797 } else if (pktq_full(q)) {
2798 p = brcmu_pktq_peek_tail(q, &eprec);
2803 /* Evict if needed */
2805 /* Detect queueing to unconfigured precedence */
2807 return false; /* refuse newer (incoming) packet */
2808 /* Evict packet according to discard policy */
2809 p = brcmu_pktq_pdeq_tail(q, eprec);
2811 brcmf_err("brcmu_pktq_pdeq_tail() failed\n");
2812 brcmu_pkt_buf_free_skb(p);
2816 p = brcmu_pktq_penq(q, prec, pkt);
2818 brcmf_err("brcmu_pktq_penq() failed\n");
2823 static int brcmf_sdio_bus_txdata(struct device *dev, struct sk_buff *pkt)
2827 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
2828 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
2829 struct brcmf_sdio *bus = sdiodev->bus;
2831 brcmf_dbg(TRACE, "Enter: pkt: data %p len %d\n", pkt->data, pkt->len);
2832 if (sdiodev->state != BRCMF_SDIOD_DATA)
2835 /* Add space for the header */
2836 skb_push(pkt, bus->tx_hdrlen);
2837 /* precondition: IS_ALIGNED((unsigned long)(pkt->data), 2) */
2839 prec = prio2prec((pkt->priority & PRIOMASK));
2841 /* Check for existing queue, current flow-control,
2842 pending event, or pending clock */
2843 brcmf_dbg(TRACE, "deferring pktq len %d\n", pktq_len(&bus->txq));
2844 bus->sdcnt.fcqueued++;
2846 /* Priority based enq */
2847 spin_lock_bh(&bus->txq_lock);
2848 /* reset bus_flags in packet cb */
2849 *(u16 *)(pkt->cb) = 0;
2850 if (!brcmf_sdio_prec_enq(&bus->txq, pkt, prec)) {
2851 skb_pull(pkt, bus->tx_hdrlen);
2852 brcmf_err("out of bus->txq !!!\n");
2858 if (pktq_len(&bus->txq) >= TXHI) {
2860 brcmf_txflowblock(dev, true);
2862 spin_unlock_bh(&bus->txq_lock);
2865 if (pktq_plen(&bus->txq, prec) > qcount[prec])
2866 qcount[prec] = pktq_plen(&bus->txq, prec);
2869 brcmf_sdio_trigger_dpc(bus);
2874 #define CONSOLE_LINE_MAX 192
2876 static int brcmf_sdio_readconsole(struct brcmf_sdio *bus)
2878 struct brcmf_console *c = &bus->console;
2879 u8 line[CONSOLE_LINE_MAX], ch;
2883 /* Don't do anything until FWREADY updates console address */
2884 if (bus->console_addr == 0)
2887 /* Read console log struct */
2888 addr = bus->console_addr + offsetof(struct rte_console, log_le);
2889 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, (u8 *)&c->log_le,
2894 /* Allocate console buffer (one time only) */
2895 if (c->buf == NULL) {
2896 c->bufsize = le32_to_cpu(c->log_le.buf_size);
2897 c->buf = kmalloc(c->bufsize, GFP_ATOMIC);
2902 idx = le32_to_cpu(c->log_le.idx);
2904 /* Protect against corrupt value */
2905 if (idx > c->bufsize)
2908 /* Skip reading the console buffer if the index pointer
2913 /* Read the console buffer */
2914 addr = le32_to_cpu(c->log_le.buf);
2915 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, c->buf, c->bufsize);
2919 while (c->last != idx) {
2920 for (n = 0; n < CONSOLE_LINE_MAX - 2; n++) {
2921 if (c->last == idx) {
2922 /* This would output a partial line.
2924 * the buffer pointer and output this
2925 * line next time around.
2930 c->last = c->bufsize - n;
2933 ch = c->buf[c->last];
2934 c->last = (c->last + 1) % c->bufsize;
2941 if (line[n - 1] == '\r')
2944 pr_debug("CONSOLE: %s\n", line);
2954 brcmf_sdio_bus_txctl(struct device *dev, unsigned char *msg, uint msglen)
2956 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
2957 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
2958 struct brcmf_sdio *bus = sdiodev->bus;
2961 brcmf_dbg(TRACE, "Enter\n");
2962 if (sdiodev->state != BRCMF_SDIOD_DATA)
2966 bus->ctrl_frame_buf = msg;
2967 bus->ctrl_frame_len = msglen;
2969 bus->ctrl_frame_stat = true;
2971 brcmf_sdio_trigger_dpc(bus);
2972 wait_event_interruptible_timeout(bus->ctrl_wait, !bus->ctrl_frame_stat,
2973 msecs_to_jiffies(CTL_DONE_TIMEOUT));
2975 if (bus->ctrl_frame_stat) {
2976 sdio_claim_host(bus->sdiodev->func[1]);
2977 if (bus->ctrl_frame_stat) {
2978 brcmf_dbg(SDIO, "ctrl_frame timeout\n");
2979 bus->ctrl_frame_stat = false;
2982 sdio_release_host(bus->sdiodev->func[1]);
2985 brcmf_dbg(SDIO, "ctrl_frame complete, err=%d\n",
2986 bus->ctrl_frame_err);
2988 ret = bus->ctrl_frame_err;
2992 bus->sdcnt.tx_ctlerrs++;
2994 bus->sdcnt.tx_ctlpkts++;
3000 static int brcmf_sdio_dump_console(struct seq_file *seq, struct brcmf_sdio *bus,
3001 struct sdpcm_shared *sh)
3003 u32 addr, console_ptr, console_size, console_index;
3004 char *conbuf = NULL;
3008 /* obtain console information from device memory */
3009 addr = sh->console_addr + offsetof(struct rte_console, log_le);
3010 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
3011 (u8 *)&sh_val, sizeof(u32));
3014 console_ptr = le32_to_cpu(sh_val);
3016 addr = sh->console_addr + offsetof(struct rte_console, log_le.buf_size);
3017 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
3018 (u8 *)&sh_val, sizeof(u32));
3021 console_size = le32_to_cpu(sh_val);
3023 addr = sh->console_addr + offsetof(struct rte_console, log_le.idx);
3024 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
3025 (u8 *)&sh_val, sizeof(u32));
3028 console_index = le32_to_cpu(sh_val);
3030 /* allocate buffer for console data */
3031 if (console_size <= CONSOLE_BUFFER_MAX)
3032 conbuf = vzalloc(console_size+1);
3037 /* obtain the console data from device */
3038 conbuf[console_size] = '\0';
3039 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, console_ptr, (u8 *)conbuf,
3044 rv = seq_write(seq, conbuf + console_index,
3045 console_size - console_index);
3049 if (console_index > 0)
3050 rv = seq_write(seq, conbuf, console_index - 1);
3057 static int brcmf_sdio_trap_info(struct seq_file *seq, struct brcmf_sdio *bus,
3058 struct sdpcm_shared *sh)
3061 struct brcmf_trap_info tr;
3063 if ((sh->flags & SDPCM_SHARED_TRAP) == 0) {
3064 brcmf_dbg(INFO, "no trap in firmware\n");
3068 error = brcmf_sdiod_ramrw(bus->sdiodev, false, sh->trap_addr, (u8 *)&tr,
3069 sizeof(struct brcmf_trap_info));
3074 "dongle trap info: type 0x%x @ epc 0x%08x\n"
3075 " cpsr 0x%08x spsr 0x%08x sp 0x%08x\n"
3076 " lr 0x%08x pc 0x%08x offset 0x%x\n"
3077 " r0 0x%08x r1 0x%08x r2 0x%08x r3 0x%08x\n"
3078 " r4 0x%08x r5 0x%08x r6 0x%08x r7 0x%08x\n",
3079 le32_to_cpu(tr.type), le32_to_cpu(tr.epc),
3080 le32_to_cpu(tr.cpsr), le32_to_cpu(tr.spsr),
3081 le32_to_cpu(tr.r13), le32_to_cpu(tr.r14),
3082 le32_to_cpu(tr.pc), sh->trap_addr,
3083 le32_to_cpu(tr.r0), le32_to_cpu(tr.r1),
3084 le32_to_cpu(tr.r2), le32_to_cpu(tr.r3),
3085 le32_to_cpu(tr.r4), le32_to_cpu(tr.r5),
3086 le32_to_cpu(tr.r6), le32_to_cpu(tr.r7));
3091 static int brcmf_sdio_assert_info(struct seq_file *seq, struct brcmf_sdio *bus,
3092 struct sdpcm_shared *sh)
3095 char file[80] = "?";
3096 char expr[80] = "<???>";
3098 if ((sh->flags & SDPCM_SHARED_ASSERT_BUILT) == 0) {
3099 brcmf_dbg(INFO, "firmware not built with -assert\n");
3101 } else if ((sh->flags & SDPCM_SHARED_ASSERT) == 0) {
3102 brcmf_dbg(INFO, "no assert in dongle\n");
3106 sdio_claim_host(bus->sdiodev->func[1]);
3107 if (sh->assert_file_addr != 0) {
3108 error = brcmf_sdiod_ramrw(bus->sdiodev, false,
3109 sh->assert_file_addr, (u8 *)file, 80);
3113 if (sh->assert_exp_addr != 0) {
3114 error = brcmf_sdiod_ramrw(bus->sdiodev, false,
3115 sh->assert_exp_addr, (u8 *)expr, 80);
3119 sdio_release_host(bus->sdiodev->func[1]);
3121 seq_printf(seq, "dongle assert: %s:%d: assert(%s)\n",
3122 file, sh->assert_line, expr);
3126 static int brcmf_sdio_checkdied(struct brcmf_sdio *bus)
3129 struct sdpcm_shared sh;
3131 error = brcmf_sdio_readshared(bus, &sh);
3136 if ((sh.flags & SDPCM_SHARED_ASSERT_BUILT) == 0)
3137 brcmf_dbg(INFO, "firmware not built with -assert\n");
3138 else if (sh.flags & SDPCM_SHARED_ASSERT)
3139 brcmf_err("assertion in dongle\n");
3141 if (sh.flags & SDPCM_SHARED_TRAP)
3142 brcmf_err("firmware trap in dongle\n");
3147 static int brcmf_sdio_died_dump(struct seq_file *seq, struct brcmf_sdio *bus)
3150 struct sdpcm_shared sh;
3152 error = brcmf_sdio_readshared(bus, &sh);
3156 error = brcmf_sdio_assert_info(seq, bus, &sh);
3160 error = brcmf_sdio_trap_info(seq, bus, &sh);
3164 error = brcmf_sdio_dump_console(seq, bus, &sh);
3170 static int brcmf_sdio_forensic_read(struct seq_file *seq, void *data)
3172 struct brcmf_bus *bus_if = dev_get_drvdata(seq->private);
3173 struct brcmf_sdio *bus = bus_if->bus_priv.sdio->bus;
3175 return brcmf_sdio_died_dump(seq, bus);
3178 static int brcmf_debugfs_sdio_count_read(struct seq_file *seq, void *data)
3180 struct brcmf_bus *bus_if = dev_get_drvdata(seq->private);
3181 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3182 struct brcmf_sdio_count *sdcnt = &sdiodev->bus->sdcnt;
3185 "intrcount: %u\nlastintrs: %u\n"
3186 "pollcnt: %u\nregfails: %u\n"
3187 "tx_sderrs: %u\nfcqueued: %u\n"
3188 "rxrtx: %u\nrx_toolong: %u\n"
3189 "rxc_errors: %u\nrx_hdrfail: %u\n"
3190 "rx_badhdr: %u\nrx_badseq: %u\n"
3191 "fc_rcvd: %u\nfc_xoff: %u\n"
3192 "fc_xon: %u\nrxglomfail: %u\n"
3193 "rxglomframes: %u\nrxglompkts: %u\n"
3194 "f2rxhdrs: %u\nf2rxdata: %u\n"
3195 "f2txdata: %u\nf1regdata: %u\n"
3196 "tickcnt: %u\ntx_ctlerrs: %lu\n"
3197 "tx_ctlpkts: %lu\nrx_ctlerrs: %lu\n"
3198 "rx_ctlpkts: %lu\nrx_readahead: %lu\n",
3199 sdcnt->intrcount, sdcnt->lastintrs,
3200 sdcnt->pollcnt, sdcnt->regfails,
3201 sdcnt->tx_sderrs, sdcnt->fcqueued,
3202 sdcnt->rxrtx, sdcnt->rx_toolong,
3203 sdcnt->rxc_errors, sdcnt->rx_hdrfail,
3204 sdcnt->rx_badhdr, sdcnt->rx_badseq,
3205 sdcnt->fc_rcvd, sdcnt->fc_xoff,
3206 sdcnt->fc_xon, sdcnt->rxglomfail,
3207 sdcnt->rxglomframes, sdcnt->rxglompkts,
3208 sdcnt->f2rxhdrs, sdcnt->f2rxdata,
3209 sdcnt->f2txdata, sdcnt->f1regdata,
3210 sdcnt->tickcnt, sdcnt->tx_ctlerrs,
3211 sdcnt->tx_ctlpkts, sdcnt->rx_ctlerrs,
3212 sdcnt->rx_ctlpkts, sdcnt->rx_readahead_cnt);
3217 static void brcmf_sdio_debugfs_create(struct brcmf_sdio *bus)
3219 struct brcmf_pub *drvr = bus->sdiodev->bus_if->drvr;
3220 struct dentry *dentry = brcmf_debugfs_get_devdir(drvr);
3222 if (IS_ERR_OR_NULL(dentry))
3225 bus->console_interval = BRCMF_CONSOLE;
3227 brcmf_debugfs_add_entry(drvr, "forensics", brcmf_sdio_forensic_read);
3228 brcmf_debugfs_add_entry(drvr, "counters",
3229 brcmf_debugfs_sdio_count_read);
3230 debugfs_create_u32("console_interval", 0644, dentry,
3231 &bus->console_interval);
3234 static int brcmf_sdio_checkdied(struct brcmf_sdio *bus)
3239 static void brcmf_sdio_debugfs_create(struct brcmf_sdio *bus)
3245 brcmf_sdio_bus_rxctl(struct device *dev, unsigned char *msg, uint msglen)
3251 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3252 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3253 struct brcmf_sdio *bus = sdiodev->bus;
3255 brcmf_dbg(TRACE, "Enter\n");
3256 if (sdiodev->state != BRCMF_SDIOD_DATA)
3259 /* Wait until control frame is available */
3260 timeleft = brcmf_sdio_dcmd_resp_wait(bus, &bus->rxlen, &pending);
3262 spin_lock_bh(&bus->rxctl_lock);
3264 memcpy(msg, bus->rxctl, min(msglen, rxlen));
3266 buf = bus->rxctl_orig;
3267 bus->rxctl_orig = NULL;
3269 spin_unlock_bh(&bus->rxctl_lock);
3273 brcmf_dbg(CTL, "resumed on rxctl frame, got %d expected %d\n",
3275 } else if (timeleft == 0) {
3276 brcmf_err("resumed on timeout\n");
3277 brcmf_sdio_checkdied(bus);
3278 } else if (pending) {
3279 brcmf_dbg(CTL, "cancelled\n");
3280 return -ERESTARTSYS;
3282 brcmf_dbg(CTL, "resumed for unknown reason?\n");
3283 brcmf_sdio_checkdied(bus);
3287 bus->sdcnt.rx_ctlpkts++;
3289 bus->sdcnt.rx_ctlerrs++;
3291 return rxlen ? (int)rxlen : -ETIMEDOUT;
3296 brcmf_sdio_verifymemory(struct brcmf_sdio_dev *sdiodev, u32 ram_addr,
3297 u8 *ram_data, uint ram_sz)
3306 /* read back and verify */
3307 brcmf_dbg(INFO, "Compare RAM dl & ul at 0x%08x; size=%d\n", ram_addr,
3309 ram_cmp = kmalloc(MEMBLOCK, GFP_KERNEL);
3310 /* do not proceed while no memory but */
3316 while (offset < ram_sz) {
3317 len = ((offset + MEMBLOCK) < ram_sz) ? MEMBLOCK :
3319 err = brcmf_sdiod_ramrw(sdiodev, false, address, ram_cmp, len);
3321 brcmf_err("error %d on reading %d membytes at 0x%08x\n",
3325 } else if (memcmp(ram_cmp, &ram_data[offset], len)) {
3326 brcmf_err("Downloaded RAM image is corrupted, block offset is %d, len is %d\n",
3341 brcmf_sdio_verifymemory(struct brcmf_sdio_dev *sdiodev, u32 ram_addr,
3342 u8 *ram_data, uint ram_sz)
3348 static int brcmf_sdio_download_code_file(struct brcmf_sdio *bus,
3349 const struct firmware *fw)
3353 brcmf_dbg(TRACE, "Enter\n");
3355 err = brcmf_sdiod_ramrw(bus->sdiodev, true, bus->ci->rambase,
3356 (u8 *)fw->data, fw->size);
3358 brcmf_err("error %d on writing %d membytes at 0x%08x\n",
3359 err, (int)fw->size, bus->ci->rambase);
3360 else if (!brcmf_sdio_verifymemory(bus->sdiodev, bus->ci->rambase,
3361 (u8 *)fw->data, fw->size))
3367 static int brcmf_sdio_download_nvram(struct brcmf_sdio *bus,
3368 void *vars, u32 varsz)
3373 brcmf_dbg(TRACE, "Enter\n");
3375 address = bus->ci->ramsize - varsz + bus->ci->rambase;
3376 err = brcmf_sdiod_ramrw(bus->sdiodev, true, address, vars, varsz);
3378 brcmf_err("error %d on writing %d nvram bytes at 0x%08x\n",
3379 err, varsz, address);
3380 else if (!brcmf_sdio_verifymemory(bus->sdiodev, address, vars, varsz))
3386 static int brcmf_sdio_download_firmware(struct brcmf_sdio *bus,
3387 const struct firmware *fw,
3388 void *nvram, u32 nvlen)
3390 int bcmerror = -EFAULT;
3393 sdio_claim_host(bus->sdiodev->func[1]);
3394 brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
3396 rstvec = get_unaligned_le32(fw->data);
3397 brcmf_dbg(SDIO, "firmware rstvec: %x\n", rstvec);
3399 bcmerror = brcmf_sdio_download_code_file(bus, fw);
3400 release_firmware(fw);
3402 brcmf_err("dongle image file download failed\n");
3403 brcmf_fw_nvram_free(nvram);
3407 bcmerror = brcmf_sdio_download_nvram(bus, nvram, nvlen);
3408 brcmf_fw_nvram_free(nvram);
3410 brcmf_err("dongle nvram file download failed\n");
3414 /* Take arm out of reset */
3415 if (!brcmf_chip_set_active(bus->ci, rstvec)) {
3416 brcmf_err("error getting out of ARM core reset\n");
3420 /* Allow full data communication using DPC from now on. */
3421 brcmf_sdiod_change_state(bus->sdiodev, BRCMF_SDIOD_DATA);
3425 brcmf_sdio_clkctl(bus, CLK_SDONLY, false);
3426 sdio_release_host(bus->sdiodev->func[1]);
3430 static void brcmf_sdio_sr_init(struct brcmf_sdio *bus)
3435 brcmf_dbg(TRACE, "Enter\n");
3437 val = brcmf_sdiod_regrb(bus->sdiodev, SBSDIO_FUNC1_WAKEUPCTRL, &err);
3439 brcmf_err("error reading SBSDIO_FUNC1_WAKEUPCTRL\n");
3443 val |= 1 << SBSDIO_FUNC1_WCTRL_HTWAIT_SHIFT;
3444 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_WAKEUPCTRL, val, &err);
3446 brcmf_err("error writing SBSDIO_FUNC1_WAKEUPCTRL\n");
3450 /* Add CMD14 Support */
3451 brcmf_sdiod_regwb(bus->sdiodev, SDIO_CCCR_BRCM_CARDCAP,
3452 (SDIO_CCCR_BRCM_CARDCAP_CMD14_SUPPORT |
3453 SDIO_CCCR_BRCM_CARDCAP_CMD14_EXT),
3456 brcmf_err("error writing SDIO_CCCR_BRCM_CARDCAP\n");
3460 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3461 SBSDIO_FORCE_HT, &err);
3463 brcmf_err("error writing SBSDIO_FUNC1_CHIPCLKCSR\n");
3468 bus->sr_enabled = true;
3469 brcmf_dbg(INFO, "SR enabled\n");
3472 /* enable KSO bit */
3473 static int brcmf_sdio_kso_init(struct brcmf_sdio *bus)
3478 brcmf_dbg(TRACE, "Enter\n");
3480 /* KSO bit added in SDIO core rev 12 */
3481 if (brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV)->rev < 12)
3484 val = brcmf_sdiod_regrb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR, &err);
3486 brcmf_err("error reading SBSDIO_FUNC1_SLEEPCSR\n");
3490 if (!(val & SBSDIO_FUNC1_SLEEPCSR_KSO_MASK)) {
3491 val |= (SBSDIO_FUNC1_SLEEPCSR_KSO_EN <<
3492 SBSDIO_FUNC1_SLEEPCSR_KSO_SHIFT);
3493 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
3496 brcmf_err("error writing SBSDIO_FUNC1_SLEEPCSR\n");
3505 static int brcmf_sdio_bus_preinit(struct device *dev)
3507 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3508 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3509 struct brcmf_sdio *bus = sdiodev->bus;
3514 /* the commands below use the terms tx and rx from
3515 * a device perspective, ie. bus:txglom affects the
3516 * bus transfers from device to host.
3518 if (brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV)->rev < 12) {
3519 /* for sdio core rev < 12, disable txgloming */
3521 err = brcmf_iovar_data_set(dev, "bus:txglom", &value,
3524 /* otherwise, set txglomalign */
3527 value = sdiodev->pdata->sd_sgentry_align;
3528 /* SDIO ADMA requires at least 32 bit alignment */
3529 value = max_t(u32, value, 4);
3530 err = brcmf_iovar_data_set(dev, "bus:txglomalign", &value,
3537 bus->tx_hdrlen = SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN;
3538 if (sdiodev->sg_support) {
3539 bus->txglom = false;
3541 pad_size = bus->sdiodev->func[2]->cur_blksize << 1;
3542 err = brcmf_iovar_data_set(bus->sdiodev->dev, "bus:rxglom",
3543 &value, sizeof(u32));
3545 /* bus:rxglom is allowed to fail */
3549 bus->tx_hdrlen += SDPCM_HWEXT_LEN;
3552 brcmf_bus_add_txhdrlen(bus->sdiodev->dev, bus->tx_hdrlen);
3558 static size_t brcmf_sdio_bus_get_ramsize(struct device *dev)
3560 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3561 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3562 struct brcmf_sdio *bus = sdiodev->bus;
3564 return bus->ci->ramsize - bus->ci->srsize;
3567 static int brcmf_sdio_bus_get_memdump(struct device *dev, void *data,
3570 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3571 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3572 struct brcmf_sdio *bus = sdiodev->bus;
3578 brcmf_dbg(INFO, "dump at 0x%08x: size=%zu\n", bus->ci->rambase,
3581 address = bus->ci->rambase;
3583 sdio_claim_host(sdiodev->func[1]);
3584 while (offset < mem_size) {
3585 len = ((offset + MEMBLOCK) < mem_size) ? MEMBLOCK :
3587 err = brcmf_sdiod_ramrw(sdiodev, false, address, data, len);
3589 brcmf_err("error %d on reading %d membytes at 0x%08x\n",
3599 sdio_release_host(sdiodev->func[1]);
3603 void brcmf_sdio_trigger_dpc(struct brcmf_sdio *bus)
3605 if (!bus->dpc_triggered) {
3606 bus->dpc_triggered = true;
3607 queue_work(bus->brcmf_wq, &bus->datawork);
3611 void brcmf_sdio_isr(struct brcmf_sdio *bus)
3613 brcmf_dbg(TRACE, "Enter\n");
3616 brcmf_err("bus is null pointer, exiting\n");
3620 /* Count the interrupt call */
3621 bus->sdcnt.intrcount++;
3623 atomic_set(&bus->ipend, 1);
3625 if (brcmf_sdio_intr_rstatus(bus)) {
3626 brcmf_err("failed backplane access\n");
3629 /* Disable additional interrupts (is this needed now)? */
3631 brcmf_err("isr w/o interrupt configured!\n");
3633 bus->dpc_triggered = true;
3634 queue_work(bus->brcmf_wq, &bus->datawork);
3637 static void brcmf_sdio_bus_watchdog(struct brcmf_sdio *bus)
3639 brcmf_dbg(TIMER, "Enter\n");
3641 /* Poll period: check device if appropriate. */
3642 if (!bus->sr_enabled &&
3643 bus->poll && (++bus->polltick >= bus->pollrate)) {
3646 /* Reset poll tick */
3649 /* Check device if no interrupts */
3651 (bus->sdcnt.intrcount == bus->sdcnt.lastintrs)) {
3653 if (!bus->dpc_triggered) {
3656 sdio_claim_host(bus->sdiodev->func[1]);
3657 devpend = brcmf_sdiod_regrb(bus->sdiodev,
3660 sdio_release_host(bus->sdiodev->func[1]);
3661 intstatus = devpend & (INTR_STATUS_FUNC1 |
3665 /* If there is something, make like the ISR and
3668 bus->sdcnt.pollcnt++;
3669 atomic_set(&bus->ipend, 1);
3671 bus->dpc_triggered = true;
3672 queue_work(bus->brcmf_wq, &bus->datawork);
3676 /* Update interrupt tracking */
3677 bus->sdcnt.lastintrs = bus->sdcnt.intrcount;
3680 /* Poll for console output periodically */
3681 if (bus->sdiodev->state == BRCMF_SDIOD_DATA && BRCMF_FWCON_ON() &&
3682 bus->console_interval != 0) {
3683 bus->console.count += BRCMF_WD_POLL_MS;
3684 if (bus->console.count >= bus->console_interval) {
3685 bus->console.count -= bus->console_interval;
3686 sdio_claim_host(bus->sdiodev->func[1]);
3687 /* Make sure backplane clock is on */
3688 brcmf_sdio_bus_sleep(bus, false, false);
3689 if (brcmf_sdio_readconsole(bus) < 0)
3691 bus->console_interval = 0;
3692 sdio_release_host(bus->sdiodev->func[1]);
3697 /* On idle timeout clear activity flag and/or turn off clock */
3698 if (!bus->dpc_triggered) {
3700 if ((!bus->dpc_running) && (bus->idletime > 0) &&
3701 (bus->clkstate == CLK_AVAIL)) {
3703 if (bus->idlecount > bus->idletime) {
3704 brcmf_dbg(SDIO, "idle\n");
3705 sdio_claim_host(bus->sdiodev->func[1]);
3706 brcmf_sdio_wd_timer(bus, 0);
3708 brcmf_sdio_bus_sleep(bus, true, false);
3709 sdio_release_host(bus->sdiodev->func[1]);
3719 static void brcmf_sdio_dataworker(struct work_struct *work)
3721 struct brcmf_sdio *bus = container_of(work, struct brcmf_sdio,
3724 bus->dpc_running = true;
3726 while (ACCESS_ONCE(bus->dpc_triggered)) {
3727 bus->dpc_triggered = false;
3728 brcmf_sdio_dpc(bus);
3731 bus->dpc_running = false;
3732 if (brcmf_sdiod_freezing(bus->sdiodev)) {
3733 brcmf_sdiod_change_state(bus->sdiodev, BRCMF_SDIOD_DOWN);
3734 brcmf_sdiod_try_freeze(bus->sdiodev);
3735 brcmf_sdiod_change_state(bus->sdiodev, BRCMF_SDIOD_DATA);
3740 brcmf_sdio_drivestrengthinit(struct brcmf_sdio_dev *sdiodev,
3741 struct brcmf_chip *ci, u32 drivestrength)
3743 const struct sdiod_drive_str *str_tab = NULL;
3748 u32 drivestrength_sel = 0;
3752 if (!(ci->cc_caps & CC_CAP_PMU))
3755 switch (SDIOD_DRVSTR_KEY(ci->chip, ci->pmurev)) {
3756 case SDIOD_DRVSTR_KEY(BRCM_CC_4330_CHIP_ID, 12):
3757 str_tab = sdiod_drvstr_tab1_1v8;
3758 str_mask = 0x00003800;
3761 case SDIOD_DRVSTR_KEY(BRCM_CC_4334_CHIP_ID, 17):
3762 str_tab = sdiod_drvstr_tab6_1v8;
3763 str_mask = 0x00001800;
3766 case SDIOD_DRVSTR_KEY(BRCM_CC_43143_CHIP_ID, 17):
3767 /* note: 43143 does not support tristate */
3768 i = ARRAY_SIZE(sdiod_drvstr_tab2_3v3) - 1;
3769 if (drivestrength >= sdiod_drvstr_tab2_3v3[i].strength) {
3770 str_tab = sdiod_drvstr_tab2_3v3;
3771 str_mask = 0x00000007;
3774 brcmf_err("Invalid SDIO Drive strength for chip %s, strength=%d\n",
3775 ci->name, drivestrength);
3777 case SDIOD_DRVSTR_KEY(BRCM_CC_43362_CHIP_ID, 13):
3778 str_tab = sdiod_drive_strength_tab5_1v8;
3779 str_mask = 0x00003800;
3783 brcmf_err("No SDIO Drive strength init done for chip %s rev %d pmurev %d\n",
3784 ci->name, ci->chiprev, ci->pmurev);
3788 if (str_tab != NULL) {
3789 for (i = 0; str_tab[i].strength != 0; i++) {
3790 if (drivestrength >= str_tab[i].strength) {
3791 drivestrength_sel = str_tab[i].sel;
3795 base = brcmf_chip_get_chipcommon(ci)->base;
3796 addr = CORE_CC_REG(base, chipcontrol_addr);
3797 brcmf_sdiod_regwl(sdiodev, addr, 1, NULL);
3798 cc_data_temp = brcmf_sdiod_regrl(sdiodev, addr, NULL);
3799 cc_data_temp &= ~str_mask;
3800 drivestrength_sel <<= str_shift;
3801 cc_data_temp |= drivestrength_sel;
3802 brcmf_sdiod_regwl(sdiodev, addr, cc_data_temp, NULL);
3804 brcmf_dbg(INFO, "SDIO: %d mA (req=%d mA) drive strength selected, set to 0x%08x\n",
3805 str_tab[i].strength, drivestrength, cc_data_temp);
3809 static int brcmf_sdio_buscoreprep(void *ctx)
3811 struct brcmf_sdio_dev *sdiodev = ctx;
3815 /* Try forcing SDIO core to do ALPAvail request only */
3816 clkset = SBSDIO_FORCE_HW_CLKREQ_OFF | SBSDIO_ALP_AVAIL_REQ;
3817 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, clkset, &err);
3819 brcmf_err("error writing for HT off\n");
3823 /* If register supported, wait for ALPAvail and then force ALP */
3824 /* This may take up to 15 milliseconds */
3825 clkval = brcmf_sdiod_regrb(sdiodev,
3826 SBSDIO_FUNC1_CHIPCLKCSR, NULL);
3828 if ((clkval & ~SBSDIO_AVBITS) != clkset) {
3829 brcmf_err("ChipClkCSR access: wrote 0x%02x read 0x%02x\n",
3834 SPINWAIT(((clkval = brcmf_sdiod_regrb(sdiodev,
3835 SBSDIO_FUNC1_CHIPCLKCSR, NULL)),
3836 !SBSDIO_ALPAV(clkval)),
3837 PMU_MAX_TRANSITION_DLY);
3838 if (!SBSDIO_ALPAV(clkval)) {
3839 brcmf_err("timeout on ALPAV wait, clkval 0x%02x\n",
3844 clkset = SBSDIO_FORCE_HW_CLKREQ_OFF | SBSDIO_FORCE_ALP;
3845 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, clkset, &err);
3848 /* Also, disable the extra SDIO pull-ups */
3849 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_SDIOPULLUP, 0, NULL);
3854 static void brcmf_sdio_buscore_activate(void *ctx, struct brcmf_chip *chip,
3857 struct brcmf_sdio_dev *sdiodev = ctx;
3858 struct brcmf_core *core;
3861 /* clear all interrupts */
3862 core = brcmf_chip_get_core(chip, BCMA_CORE_SDIO_DEV);
3863 reg_addr = core->base + offsetof(struct sdpcmd_regs, intstatus);
3864 brcmf_sdiod_regwl(sdiodev, reg_addr, 0xFFFFFFFF, NULL);
3867 /* Write reset vector to address 0 */
3868 brcmf_sdiod_ramrw(sdiodev, true, 0, (void *)&rstvec,
3872 static u32 brcmf_sdio_buscore_read32(void *ctx, u32 addr)
3874 struct brcmf_sdio_dev *sdiodev = ctx;
3877 val = brcmf_sdiod_regrl(sdiodev, addr, NULL);
3878 if (sdiodev->func[0]->device == SDIO_DEVICE_ID_BROADCOM_4335_4339 &&
3879 addr == CORE_CC_REG(SI_ENUM_BASE, chipid)) {
3880 rev = (val & CID_REV_MASK) >> CID_REV_SHIFT;
3882 val &= ~CID_ID_MASK;
3883 val |= BRCM_CC_4339_CHIP_ID;
3889 static void brcmf_sdio_buscore_write32(void *ctx, u32 addr, u32 val)
3891 struct brcmf_sdio_dev *sdiodev = ctx;
3893 brcmf_sdiod_regwl(sdiodev, addr, val, NULL);
3896 static const struct brcmf_buscore_ops brcmf_sdio_buscore_ops = {
3897 .prepare = brcmf_sdio_buscoreprep,
3898 .activate = brcmf_sdio_buscore_activate,
3899 .read32 = brcmf_sdio_buscore_read32,
3900 .write32 = brcmf_sdio_buscore_write32,
3904 brcmf_sdio_probe_attach(struct brcmf_sdio *bus)
3912 sdio_claim_host(bus->sdiodev->func[1]);
3914 pr_debug("F1 signature read @0x18000000=0x%4x\n",
3915 brcmf_sdiod_regrl(bus->sdiodev, SI_ENUM_BASE, NULL));
3918 * Force PLL off until brcmf_chip_attach()
3919 * programs PLL control regs
3922 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3923 BRCMF_INIT_CLKCTL1, &err);
3925 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
3926 SBSDIO_FUNC1_CHIPCLKCSR, &err);
3928 if (err || ((clkctl & ~SBSDIO_AVBITS) != BRCMF_INIT_CLKCTL1)) {
3929 brcmf_err("ChipClkCSR access: err %d wrote 0x%02x read 0x%02x\n",
3930 err, BRCMF_INIT_CLKCTL1, clkctl);
3934 bus->ci = brcmf_chip_attach(bus->sdiodev, &brcmf_sdio_buscore_ops);
3935 if (IS_ERR(bus->ci)) {
3936 brcmf_err("brcmf_chip_attach failed!\n");
3941 if (brcmf_sdio_kso_init(bus)) {
3942 brcmf_err("error enabling KSO\n");
3946 if ((bus->sdiodev->pdata) && (bus->sdiodev->pdata->drive_strength))
3947 drivestrength = bus->sdiodev->pdata->drive_strength;
3949 drivestrength = DEFAULT_SDIO_DRIVE_STRENGTH;
3950 brcmf_sdio_drivestrengthinit(bus->sdiodev, bus->ci, drivestrength);
3952 /* Set card control so an SDIO card reset does a WLAN backplane reset */
3953 reg_val = brcmf_sdiod_regrb(bus->sdiodev,
3954 SDIO_CCCR_BRCM_CARDCTRL, &err);
3958 reg_val |= SDIO_CCCR_BRCM_CARDCTRL_WLANRESET;
3960 brcmf_sdiod_regwb(bus->sdiodev,
3961 SDIO_CCCR_BRCM_CARDCTRL, reg_val, &err);
3965 /* set PMUControl so a backplane reset does PMU state reload */
3966 reg_addr = CORE_CC_REG(brcmf_chip_get_chipcommon(bus->ci)->base,
3968 reg_val = brcmf_sdiod_regrl(bus->sdiodev, reg_addr, &err);
3972 reg_val |= (BCMA_CC_PMU_CTL_RES_RELOAD << BCMA_CC_PMU_CTL_RES_SHIFT);
3974 brcmf_sdiod_regwl(bus->sdiodev, reg_addr, reg_val, &err);
3978 sdio_release_host(bus->sdiodev->func[1]);
3980 brcmu_pktq_init(&bus->txq, (PRIOMASK + 1), TXQLEN);
3982 /* allocate header buffer */
3983 bus->hdrbuf = kzalloc(MAX_HDR_READ + bus->head_align, GFP_KERNEL);
3986 /* Locate an appropriately-aligned portion of hdrbuf */
3987 bus->rxhdr = (u8 *) roundup((unsigned long)&bus->hdrbuf[0],
3990 /* Set the poll and/or interrupt flags */
3999 sdio_release_host(bus->sdiodev->func[1]);
4004 brcmf_sdio_watchdog_thread(void *data)
4006 struct brcmf_sdio *bus = (struct brcmf_sdio *)data;
4009 allow_signal(SIGTERM);
4010 /* Run until signal received */
4011 brcmf_sdiod_freezer_count(bus->sdiodev);
4013 if (kthread_should_stop())
4015 brcmf_sdiod_freezer_uncount(bus->sdiodev);
4016 wait = wait_for_completion_interruptible(&bus->watchdog_wait);
4017 brcmf_sdiod_freezer_count(bus->sdiodev);
4018 brcmf_sdiod_try_freeze(bus->sdiodev);
4020 brcmf_sdio_bus_watchdog(bus);
4021 /* Count the tick for reference */
4022 bus->sdcnt.tickcnt++;
4023 reinit_completion(&bus->watchdog_wait);
4031 brcmf_sdio_watchdog(unsigned long data)
4033 struct brcmf_sdio *bus = (struct brcmf_sdio *)data;
4035 if (bus->watchdog_tsk) {
4036 complete(&bus->watchdog_wait);
4037 /* Reschedule the watchdog */
4038 if (bus->wd_timer_valid)
4039 mod_timer(&bus->timer,
4040 jiffies + msecs_to_jiffies(BRCMF_WD_POLL_MS));
4044 static struct brcmf_bus_ops brcmf_sdio_bus_ops = {
4045 .stop = brcmf_sdio_bus_stop,
4046 .preinit = brcmf_sdio_bus_preinit,
4047 .txdata = brcmf_sdio_bus_txdata,
4048 .txctl = brcmf_sdio_bus_txctl,
4049 .rxctl = brcmf_sdio_bus_rxctl,
4050 .gettxq = brcmf_sdio_bus_gettxq,
4051 .wowl_config = brcmf_sdio_wowl_config,
4052 .get_ramsize = brcmf_sdio_bus_get_ramsize,
4053 .get_memdump = brcmf_sdio_bus_get_memdump,
4056 static void brcmf_sdio_firmware_callback(struct device *dev,
4057 const struct firmware *code,
4058 void *nvram, u32 nvram_len)
4060 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
4061 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
4062 struct brcmf_sdio *bus = sdiodev->bus;
4066 brcmf_dbg(TRACE, "Enter: dev=%s\n", dev_name(dev));
4071 /* try to download image and nvram to the dongle */
4072 bus->alp_only = true;
4073 err = brcmf_sdio_download_firmware(bus, code, nvram, nvram_len);
4076 bus->alp_only = false;
4078 /* Start the watchdog timer */
4079 bus->sdcnt.tickcnt = 0;
4080 brcmf_sdio_wd_timer(bus, BRCMF_WD_POLL_MS);
4082 sdio_claim_host(sdiodev->func[1]);
4084 /* Make sure backplane clock is on, needed to generate F2 interrupt */
4085 brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
4086 if (bus->clkstate != CLK_AVAIL)
4089 /* Force clocks on backplane to be sure F2 interrupt propagates */
4090 saveclk = brcmf_sdiod_regrb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, &err);
4092 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
4093 (saveclk | SBSDIO_FORCE_HT), &err);
4096 brcmf_err("Failed to force clock for F2: err %d\n", err);
4100 /* Enable function 2 (frame transfers) */
4101 w_sdreg32(bus, SDPCM_PROT_VERSION << SMB_DATA_VERSION_SHIFT,
4102 offsetof(struct sdpcmd_regs, tosbmailboxdata));
4103 err = sdio_enable_func(sdiodev->func[SDIO_FUNC_2]);
4106 brcmf_dbg(INFO, "enable F2: err=%d\n", err);
4108 /* If F2 successfully enabled, set core and enable interrupts */
4110 /* Set up the interrupt mask and enable interrupts */
4111 bus->hostintmask = HOSTINTMASK;
4112 w_sdreg32(bus, bus->hostintmask,
4113 offsetof(struct sdpcmd_regs, hostintmask));
4115 brcmf_sdiod_regwb(sdiodev, SBSDIO_WATERMARK, 8, &err);
4117 /* Disable F2 again */
4118 sdio_disable_func(sdiodev->func[SDIO_FUNC_2]);
4122 if (brcmf_chip_sr_capable(bus->ci)) {
4123 brcmf_sdio_sr_init(bus);
4125 /* Restore previous clock setting */
4126 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
4131 err = brcmf_sdiod_intr_register(sdiodev);
4133 brcmf_err("intr register failed:%d\n", err);
4136 /* If we didn't come up, turn off backplane clock */
4138 brcmf_sdio_clkctl(bus, CLK_NONE, false);
4140 sdio_release_host(sdiodev->func[1]);
4142 err = brcmf_bus_start(dev);
4144 brcmf_err("dongle is not responding\n");
4150 sdio_release_host(sdiodev->func[1]);
4152 brcmf_dbg(TRACE, "failed: dev=%s, err=%d\n", dev_name(dev), err);
4153 device_release_driver(dev);
4156 struct brcmf_sdio *brcmf_sdio_probe(struct brcmf_sdio_dev *sdiodev)
4159 struct brcmf_sdio *bus;
4160 struct workqueue_struct *wq;
4162 brcmf_dbg(TRACE, "Enter\n");
4164 /* Allocate private bus interface state */
4165 bus = kzalloc(sizeof(struct brcmf_sdio), GFP_ATOMIC);
4169 bus->sdiodev = sdiodev;
4171 skb_queue_head_init(&bus->glom);
4172 bus->txbound = BRCMF_TXBOUND;
4173 bus->rxbound = BRCMF_RXBOUND;
4174 bus->txminmax = BRCMF_TXMINMAX;
4175 bus->tx_seq = SDPCM_SEQ_WRAP - 1;
4177 /* platform specific configuration:
4178 * alignments must be at least 4 bytes for ADMA
4180 bus->head_align = ALIGNMENT;
4181 bus->sgentry_align = ALIGNMENT;
4182 if (sdiodev->pdata) {
4183 if (sdiodev->pdata->sd_head_align > ALIGNMENT)
4184 bus->head_align = sdiodev->pdata->sd_head_align;
4185 if (sdiodev->pdata->sd_sgentry_align > ALIGNMENT)
4186 bus->sgentry_align = sdiodev->pdata->sd_sgentry_align;
4189 /* single-threaded workqueue */
4190 wq = alloc_ordered_workqueue("brcmf_wq/%s", WQ_MEM_RECLAIM,
4191 dev_name(&sdiodev->func[1]->dev));
4193 brcmf_err("insufficient memory to create txworkqueue\n");
4196 brcmf_sdiod_freezer_count(sdiodev);
4197 INIT_WORK(&bus->datawork, brcmf_sdio_dataworker);
4200 /* attempt to attach to the dongle */
4201 if (!(brcmf_sdio_probe_attach(bus))) {
4202 brcmf_err("brcmf_sdio_probe_attach failed\n");
4206 spin_lock_init(&bus->rxctl_lock);
4207 spin_lock_init(&bus->txq_lock);
4208 init_waitqueue_head(&bus->ctrl_wait);
4209 init_waitqueue_head(&bus->dcmd_resp_wait);
4211 /* Set up the watchdog timer */
4212 init_timer(&bus->timer);
4213 bus->timer.data = (unsigned long)bus;
4214 bus->timer.function = brcmf_sdio_watchdog;
4216 /* Initialize watchdog thread */
4217 init_completion(&bus->watchdog_wait);
4218 bus->watchdog_tsk = kthread_run(brcmf_sdio_watchdog_thread,
4219 bus, "brcmf_wdog/%s",
4220 dev_name(&sdiodev->func[1]->dev));
4221 if (IS_ERR(bus->watchdog_tsk)) {
4222 pr_warn("brcmf_watchdog thread failed to start\n");
4223 bus->watchdog_tsk = NULL;
4225 /* Initialize DPC thread */
4226 bus->dpc_triggered = false;
4227 bus->dpc_running = false;
4229 /* Assign bus interface call back */
4230 bus->sdiodev->bus_if->dev = bus->sdiodev->dev;
4231 bus->sdiodev->bus_if->ops = &brcmf_sdio_bus_ops;
4232 bus->sdiodev->bus_if->chip = bus->ci->chip;
4233 bus->sdiodev->bus_if->chiprev = bus->ci->chiprev;
4235 /* default sdio bus header length for tx packet */
4236 bus->tx_hdrlen = SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN;
4238 /* Attach to the common layer, reserve hdr space */
4239 ret = brcmf_attach(bus->sdiodev->dev);
4241 brcmf_err("brcmf_attach failed\n");
4245 /* Query the F2 block size, set roundup accordingly */
4246 bus->blocksize = bus->sdiodev->func[2]->cur_blksize;
4247 bus->roundup = min(max_roundup, bus->blocksize);
4249 /* Allocate buffers */
4250 if (bus->sdiodev->bus_if->maxctl) {
4251 bus->sdiodev->bus_if->maxctl += bus->roundup;
4253 roundup((bus->sdiodev->bus_if->maxctl + SDPCM_HDRLEN),
4254 ALIGNMENT) + bus->head_align;
4255 bus->rxbuf = kmalloc(bus->rxblen, GFP_ATOMIC);
4256 if (!(bus->rxbuf)) {
4257 brcmf_err("rxbuf allocation failed\n");
4262 sdio_claim_host(bus->sdiodev->func[1]);
4264 /* Disable F2 to clear any intermediate frame state on the dongle */
4265 sdio_disable_func(bus->sdiodev->func[SDIO_FUNC_2]);
4267 bus->rxflow = false;
4269 /* Done with backplane-dependent accesses, can drop clock... */
4270 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, 0, NULL);
4272 sdio_release_host(bus->sdiodev->func[1]);
4274 /* ...and initialize clock/power states */
4275 bus->clkstate = CLK_SDONLY;
4276 bus->idletime = BRCMF_IDLE_INTERVAL;
4277 bus->idleclock = BRCMF_IDLE_ACTIVE;
4280 bus->sr_enabled = false;
4282 brcmf_sdio_debugfs_create(bus);
4283 brcmf_dbg(INFO, "completed!!\n");
4285 ret = brcmf_sdio_get_fwnames(bus->ci, sdiodev);
4289 ret = brcmf_fw_get_firmwares(sdiodev->dev, BRCMF_FW_REQUEST_NVRAM,
4290 sdiodev->fw_name, sdiodev->nvram_name,
4291 brcmf_sdio_firmware_callback);
4293 brcmf_err("async firmware request failed: %d\n", ret);
4300 brcmf_sdio_remove(bus);
4304 /* Detach and free everything */
4305 void brcmf_sdio_remove(struct brcmf_sdio *bus)
4307 brcmf_dbg(TRACE, "Enter\n");
4310 /* Stop watchdog task */
4311 if (bus->watchdog_tsk) {
4312 send_sig(SIGTERM, bus->watchdog_tsk, 1);
4313 kthread_stop(bus->watchdog_tsk);
4314 bus->watchdog_tsk = NULL;
4317 /* De-register interrupt handler */
4318 brcmf_sdiod_intr_unregister(bus->sdiodev);
4320 brcmf_detach(bus->sdiodev->dev);
4322 cancel_work_sync(&bus->datawork);
4324 destroy_workqueue(bus->brcmf_wq);
4327 if (bus->sdiodev->state != BRCMF_SDIOD_NOMEDIUM) {
4328 sdio_claim_host(bus->sdiodev->func[1]);
4329 brcmf_sdio_wd_timer(bus, 0);
4330 brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
4331 /* Leave the device in state where it is
4332 * 'passive'. This is done by resetting all
4336 brcmf_chip_set_passive(bus->ci);
4337 brcmf_sdio_clkctl(bus, CLK_NONE, false);
4338 sdio_release_host(bus->sdiodev->func[1]);
4340 brcmf_chip_detach(bus->ci);
4348 brcmf_dbg(TRACE, "Disconnected\n");
4351 void brcmf_sdio_wd_timer(struct brcmf_sdio *bus, uint wdtick)
4353 /* Totally stop the timer */
4354 if (!wdtick && bus->wd_timer_valid) {
4355 del_timer_sync(&bus->timer);
4356 bus->wd_timer_valid = false;
4357 bus->save_ms = wdtick;
4361 /* don't start the wd until fw is loaded */
4362 if (bus->sdiodev->state != BRCMF_SDIOD_DATA)
4366 if (bus->save_ms != BRCMF_WD_POLL_MS) {
4367 if (bus->wd_timer_valid)
4368 /* Stop timer and restart at new value */
4369 del_timer_sync(&bus->timer);
4371 /* Create timer again when watchdog period is
4372 dynamically changed or in the first instance
4374 bus->timer.expires =
4375 jiffies + msecs_to_jiffies(BRCMF_WD_POLL_MS);
4376 add_timer(&bus->timer);
4379 /* Re arm the timer, at last watchdog period */
4380 mod_timer(&bus->timer,
4381 jiffies + msecs_to_jiffies(BRCMF_WD_POLL_MS));
4384 bus->wd_timer_valid = true;
4385 bus->save_ms = wdtick;
4389 int brcmf_sdio_sleep(struct brcmf_sdio *bus, bool sleep)
4393 sdio_claim_host(bus->sdiodev->func[1]);
4394 ret = brcmf_sdio_bus_sleep(bus, sleep, false);
4395 sdio_release_host(bus->sdiodev->func[1]);