2 * Copyright (c) 2010 Broadcom Corporation
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <linux/types.h>
18 #include <linux/kernel.h>
19 #include <linux/kthread.h>
20 #include <linux/printk.h>
21 #include <linux/pci_ids.h>
22 #include <linux/netdevice.h>
23 #include <linux/interrupt.h>
24 #include <linux/sched.h>
25 #include <linux/mmc/sdio.h>
26 #include <linux/mmc/sdio_func.h>
27 #include <linux/mmc/card.h>
28 #include <linux/semaphore.h>
29 #include <linux/firmware.h>
30 #include <linux/module.h>
31 #include <linux/bcma/bcma.h>
32 #include <linux/debugfs.h>
33 #include <linux/vmalloc.h>
34 #include <linux/platform_data/brcmfmac-sdio.h>
35 #include <asm/unaligned.h>
37 #include <brcmu_wifi.h>
38 #include <brcmu_utils.h>
39 #include <brcm_hw_ids.h>
41 #include "sdio_host.h"
42 #include "sdio_chip.h"
44 #define DCMD_RESP_TIMEOUT 2000 /* In milli second */
48 #define BRCMF_TRAP_INFO_SIZE 80
50 #define CBUF_LEN (128)
52 /* Device console log buffer state */
53 #define CONSOLE_BUFFER_MAX 2024
56 __le32 buf; /* Can't be pointer on (64-bit) hosts */
59 char *_buf_compat; /* Redundant pointer for backward compat. */
64 * When there is no UART (e.g. Quickturn),
65 * the host should write a complete
66 * input line directly into cbuf and then write
67 * the length into vcons_in.
68 * This may also be used when there is a real UART
69 * (at risk of conflicting with
70 * the real UART). vcons_out is currently unused.
75 /* Output (logging) buffer
76 * Console output is written to a ring buffer log_buf at index log_idx.
77 * The host may read the output when it sees log_idx advance.
78 * Output will be lost if the output wraps around faster than the host
81 struct rte_log_le log_le;
83 /* Console input line buffer
84 * Characters are read one at a time into cbuf
85 * until <CR> is received, then
86 * the buffer is processed as a command line.
87 * Also used for virtual UART.
94 #include <chipcommon.h>
98 #include "tracepoint.h"
100 #define TXQLEN 2048 /* bulk tx queue length */
101 #define TXHI (TXQLEN - 256) /* turn on flow control above TXHI */
102 #define TXLOW (TXHI - 256) /* turn off flow control below TXLOW */
105 #define TXRETRIES 2 /* # of retries for tx frames */
107 #define BRCMF_RXBOUND 50 /* Default for max rx frames in
110 #define BRCMF_TXBOUND 20 /* Default for max tx frames in
113 #define BRCMF_TXMINMAX 1 /* Max tx frames if rx still pending */
115 #define MEMBLOCK 2048 /* Block size used for downloading
117 #define MAX_DATA_BUF (32 * 1024) /* Must be large enough to hold
118 biggest possible glom */
120 #define BRCMF_FIRSTREAD (1 << 6)
123 /* SBSDIO_DEVICE_CTL */
125 /* 1: device will assert busy signal when receiving CMD53 */
126 #define SBSDIO_DEVCTL_SETBUSY 0x01
127 /* 1: assertion of sdio interrupt is synchronous to the sdio clock */
128 #define SBSDIO_DEVCTL_SPI_INTR_SYNC 0x02
129 /* 1: mask all interrupts to host except the chipActive (rev 8) */
130 #define SBSDIO_DEVCTL_CA_INT_ONLY 0x04
131 /* 1: isolate internal sdio signals, put external pads in tri-state; requires
132 * sdio bus power cycle to clear (rev 9) */
133 #define SBSDIO_DEVCTL_PADS_ISO 0x08
134 /* Force SD->SB reset mapping (rev 11) */
135 #define SBSDIO_DEVCTL_SB_RST_CTL 0x30
136 /* Determined by CoreControl bit */
137 #define SBSDIO_DEVCTL_RST_CORECTL 0x00
138 /* Force backplane reset */
139 #define SBSDIO_DEVCTL_RST_BPRESET 0x10
140 /* Force no backplane reset */
141 #define SBSDIO_DEVCTL_RST_NOBPRESET 0x20
143 /* direct(mapped) cis space */
145 /* MAPPED common CIS address */
146 #define SBSDIO_CIS_BASE_COMMON 0x1000
147 /* maximum bytes in one CIS */
148 #define SBSDIO_CIS_SIZE_LIMIT 0x200
149 /* cis offset addr is < 17 bits */
150 #define SBSDIO_CIS_OFT_ADDR_MASK 0x1FFFF
152 /* manfid tuple length, include tuple, link bytes */
153 #define SBSDIO_CIS_MANFID_TUPLE_LEN 6
156 #define I_SMB_SW0 (1 << 0) /* To SB Mail S/W interrupt 0 */
157 #define I_SMB_SW1 (1 << 1) /* To SB Mail S/W interrupt 1 */
158 #define I_SMB_SW2 (1 << 2) /* To SB Mail S/W interrupt 2 */
159 #define I_SMB_SW3 (1 << 3) /* To SB Mail S/W interrupt 3 */
160 #define I_SMB_SW_MASK 0x0000000f /* To SB Mail S/W interrupts mask */
161 #define I_SMB_SW_SHIFT 0 /* To SB Mail S/W interrupts shift */
162 #define I_HMB_SW0 (1 << 4) /* To Host Mail S/W interrupt 0 */
163 #define I_HMB_SW1 (1 << 5) /* To Host Mail S/W interrupt 1 */
164 #define I_HMB_SW2 (1 << 6) /* To Host Mail S/W interrupt 2 */
165 #define I_HMB_SW3 (1 << 7) /* To Host Mail S/W interrupt 3 */
166 #define I_HMB_SW_MASK 0x000000f0 /* To Host Mail S/W interrupts mask */
167 #define I_HMB_SW_SHIFT 4 /* To Host Mail S/W interrupts shift */
168 #define I_WR_OOSYNC (1 << 8) /* Write Frame Out Of Sync */
169 #define I_RD_OOSYNC (1 << 9) /* Read Frame Out Of Sync */
170 #define I_PC (1 << 10) /* descriptor error */
171 #define I_PD (1 << 11) /* data error */
172 #define I_DE (1 << 12) /* Descriptor protocol Error */
173 #define I_RU (1 << 13) /* Receive descriptor Underflow */
174 #define I_RO (1 << 14) /* Receive fifo Overflow */
175 #define I_XU (1 << 15) /* Transmit fifo Underflow */
176 #define I_RI (1 << 16) /* Receive Interrupt */
177 #define I_BUSPWR (1 << 17) /* SDIO Bus Power Change (rev 9) */
178 #define I_XMTDATA_AVAIL (1 << 23) /* bits in fifo */
179 #define I_XI (1 << 24) /* Transmit Interrupt */
180 #define I_RF_TERM (1 << 25) /* Read Frame Terminate */
181 #define I_WF_TERM (1 << 26) /* Write Frame Terminate */
182 #define I_PCMCIA_XU (1 << 27) /* PCMCIA Transmit FIFO Underflow */
183 #define I_SBINT (1 << 28) /* sbintstatus Interrupt */
184 #define I_CHIPACTIVE (1 << 29) /* chip from doze to active state */
185 #define I_SRESET (1 << 30) /* CCCR RES interrupt */
186 #define I_IOE2 (1U << 31) /* CCCR IOE2 Bit Changed */
187 #define I_ERRORS (I_PC | I_PD | I_DE | I_RU | I_RO | I_XU)
188 #define I_DMA (I_RI | I_XI | I_ERRORS)
191 #define CC_CISRDY (1 << 0) /* CIS Ready */
192 #define CC_BPRESEN (1 << 1) /* CCCR RES signal */
193 #define CC_F2RDY (1 << 2) /* set CCCR IOR2 bit */
194 #define CC_CLRPADSISO (1 << 3) /* clear SDIO pads isolation */
195 #define CC_XMTDATAAVAIL_MODE (1 << 4)
196 #define CC_XMTDATAAVAIL_CTRL (1 << 5)
199 #define SFC_RF_TERM (1 << 0) /* Read Frame Terminate */
200 #define SFC_WF_TERM (1 << 1) /* Write Frame Terminate */
201 #define SFC_CRC4WOOS (1 << 2) /* CRC error for write out of sync */
202 #define SFC_ABORTALL (1 << 3) /* Abort all in-progress frames */
205 #define SDPCM_FRAMETAG_LEN 4 /* 2 bytes len, 2 bytes check val */
207 /* Total length of frame header for dongle protocol */
208 #define SDPCM_HDRLEN (SDPCM_FRAMETAG_LEN + SDPCM_SWHEADER_LEN)
209 #define SDPCM_RESERVE (SDPCM_HDRLEN + BRCMF_SDALIGN)
212 * Software allocation of To SB Mailbox resources
215 /* tosbmailbox bits corresponding to intstatus bits */
216 #define SMB_NAK (1 << 0) /* Frame NAK */
217 #define SMB_INT_ACK (1 << 1) /* Host Interrupt ACK */
218 #define SMB_USE_OOB (1 << 2) /* Use OOB Wakeup */
219 #define SMB_DEV_INT (1 << 3) /* Miscellaneous Interrupt */
221 /* tosbmailboxdata */
222 #define SMB_DATA_VERSION_SHIFT 16 /* host protocol version */
225 * Software allocation of To Host Mailbox resources
229 #define I_HMB_FC_STATE I_HMB_SW0 /* Flow Control State */
230 #define I_HMB_FC_CHANGE I_HMB_SW1 /* Flow Control State Changed */
231 #define I_HMB_FRAME_IND I_HMB_SW2 /* Frame Indication */
232 #define I_HMB_HOST_INT I_HMB_SW3 /* Miscellaneous Interrupt */
234 /* tohostmailboxdata */
235 #define HMB_DATA_NAKHANDLED 1 /* retransmit NAK'd frame */
236 #define HMB_DATA_DEVREADY 2 /* talk to host after enable */
237 #define HMB_DATA_FC 4 /* per prio flowcontrol update flag */
238 #define HMB_DATA_FWREADY 8 /* fw ready for protocol activity */
240 #define HMB_DATA_FCDATA_MASK 0xff000000
241 #define HMB_DATA_FCDATA_SHIFT 24
243 #define HMB_DATA_VERSION_MASK 0x00ff0000
244 #define HMB_DATA_VERSION_SHIFT 16
247 * Software-defined protocol header
250 /* Current protocol version */
251 #define SDPCM_PROT_VERSION 4
253 /* SW frame header */
254 #define SDPCM_PACKET_SEQUENCE(p) (((u8 *)p)[0] & 0xff)
256 #define SDPCM_CHANNEL_MASK 0x00000f00
257 #define SDPCM_CHANNEL_SHIFT 8
258 #define SDPCM_PACKET_CHANNEL(p) (((u8 *)p)[1] & 0x0f)
260 #define SDPCM_NEXTLEN_OFFSET 2
262 /* Data Offset from SOF (HW Tag, SW Tag, Pad) */
263 #define SDPCM_DOFFSET_OFFSET 3 /* Data Offset */
264 #define SDPCM_DOFFSET_VALUE(p) (((u8 *)p)[SDPCM_DOFFSET_OFFSET] & 0xff)
265 #define SDPCM_DOFFSET_MASK 0xff000000
266 #define SDPCM_DOFFSET_SHIFT 24
267 #define SDPCM_FCMASK_OFFSET 4 /* Flow control */
268 #define SDPCM_FCMASK_VALUE(p) (((u8 *)p)[SDPCM_FCMASK_OFFSET] & 0xff)
269 #define SDPCM_WINDOW_OFFSET 5 /* Credit based fc */
270 #define SDPCM_WINDOW_VALUE(p) (((u8 *)p)[SDPCM_WINDOW_OFFSET] & 0xff)
272 #define SDPCM_SWHEADER_LEN 8 /* SW header is 64 bits */
274 /* logical channel numbers */
275 #define SDPCM_CONTROL_CHANNEL 0 /* Control channel Id */
276 #define SDPCM_EVENT_CHANNEL 1 /* Asyc Event Indication Channel Id */
277 #define SDPCM_DATA_CHANNEL 2 /* Data Xmit/Recv Channel Id */
278 #define SDPCM_GLOM_CHANNEL 3 /* For coalesced packets */
279 #define SDPCM_TEST_CHANNEL 15 /* Reserved for test/debug packets */
281 #define SDPCM_SEQUENCE_WRAP 256 /* wrap-around val for 8bit frame seq */
283 #define SDPCM_GLOMDESC(p) (((u8 *)p)[1] & 0x80)
286 * Shared structure between dongle and the host.
287 * The structure contains pointers to trap or assert information.
289 #define SDPCM_SHARED_VERSION 0x0003
290 #define SDPCM_SHARED_VERSION_MASK 0x00FF
291 #define SDPCM_SHARED_ASSERT_BUILT 0x0100
292 #define SDPCM_SHARED_ASSERT 0x0200
293 #define SDPCM_SHARED_TRAP 0x0400
295 /* Space for header read, limit for data packets */
296 #define MAX_HDR_READ (1 << 6)
297 #define MAX_RX_DATASZ 2048
299 /* Maximum milliseconds to wait for F2 to come up */
300 #define BRCMF_WAIT_F2RDY 3000
302 /* Bump up limit on waiting for HT to account for first startup;
303 * if the image is doing a CRC calculation before programming the PMU
304 * for HT availability, it could take a couple hundred ms more, so
305 * max out at a 1 second (1000000us).
307 #undef PMU_MAX_TRANSITION_DLY
308 #define PMU_MAX_TRANSITION_DLY 1000000
310 /* Value for ChipClockCSR during initial setup */
311 #define BRCMF_INIT_CLKCTL1 (SBSDIO_FORCE_HW_CLKREQ_OFF | \
312 SBSDIO_ALP_AVAIL_REQ)
314 /* Flags for SDH calls */
315 #define F2SYNC (SDIO_REQ_4BYTE | SDIO_REQ_FIXED)
317 #define BRCMF_SDIO_FW_NAME "brcm/brcmfmac-sdio.bin"
318 #define BRCMF_SDIO_NV_NAME "brcm/brcmfmac-sdio.txt"
319 MODULE_FIRMWARE(BRCMF_SDIO_FW_NAME);
320 MODULE_FIRMWARE(BRCMF_SDIO_NV_NAME);
322 #define BRCMF_IDLE_IMMEDIATE (-1) /* Enter idle immediately */
323 #define BRCMF_IDLE_ACTIVE 0 /* Do not request any SD clock change
326 #define BRCMF_IDLE_INTERVAL 1
328 #define KSO_WAIT_US 50
329 #define MAX_KSO_ATTEMPTS (PMU_MAX_TRANSITION_DLY/KSO_WAIT_US)
332 * Conversion of 802.1D priority to precedence level
334 static uint prio2prec(u32 prio)
336 return (prio == PRIO_8021D_NONE || prio == PRIO_8021D_BE) ?
341 /* Device console log buffer state */
342 struct brcmf_console {
343 uint count; /* Poll interval msec counter */
344 uint log_addr; /* Log struct address (fixed) */
345 struct rte_log_le log_le; /* Log struct (host copy) */
346 uint bufsize; /* Size of log buffer */
347 u8 *buf; /* Log buffer (host copy) */
348 uint last; /* Last buffer read index */
351 struct brcmf_trap_info {
365 __le32 r9; /* sb/v6 */
366 __le32 r10; /* sl/v7 */
367 __le32 r11; /* fp/v8 */
375 struct sdpcm_shared {
379 u32 assert_file_addr;
381 u32 console_addr; /* Address of struct rte_console */
387 struct sdpcm_shared_le {
390 __le32 assert_exp_addr;
391 __le32 assert_file_addr;
393 __le32 console_addr; /* Address of struct rte_console */
394 __le32 msgtrace_addr;
399 /* SDIO read frame info */
400 struct brcmf_sdio_read {
409 /* misc chip info needed by some of the routines */
410 /* Private data for SDIO bus interaction */
412 struct brcmf_sdio_dev *sdiodev; /* sdio device handler */
413 struct chip_info *ci; /* Chip info struct */
414 char *vars; /* Variables (from CIS and/or other) */
415 uint varsz; /* Size of variables buffer */
417 u32 ramsize; /* Size of RAM in SOCRAM (bytes) */
419 u32 hostintmask; /* Copy of Host Interrupt Mask */
420 atomic_t intstatus; /* Intstatus bits (events) pending */
421 atomic_t fcstate; /* State of dongle flow-control */
423 uint blocksize; /* Block size of SDIO transfers */
424 uint roundup; /* Max roundup limit */
426 struct pktq txq; /* Queue length used for flow-control */
427 u8 flowcontrol; /* per prio flow control bitmask */
428 u8 tx_seq; /* Transmit sequence number (next) */
429 u8 tx_max; /* Maximum transmit sequence allowed */
431 u8 hdrbuf[MAX_HDR_READ + BRCMF_SDALIGN];
432 u8 *rxhdr; /* Header of current rx frame (in hdrbuf) */
433 u8 rx_seq; /* Receive sequence number (expected) */
434 struct brcmf_sdio_read cur_read;
435 /* info of current read frame */
436 bool rxskip; /* Skip receive (awaiting NAK ACK) */
437 bool rxpending; /* Data frame pending in dongle */
439 uint rxbound; /* Rx frames to read before resched */
440 uint txbound; /* Tx frames to send before resched */
443 struct sk_buff *glomd; /* Packet containing glomming descriptor */
444 struct sk_buff_head glom; /* Packet list for glommed superframe */
445 uint glomerr; /* Glom packet read errors */
447 u8 *rxbuf; /* Buffer for receiving control packets */
448 uint rxblen; /* Allocated length of rxbuf */
449 u8 *rxctl; /* Aligned pointer into rxbuf */
450 u8 *rxctl_orig; /* pointer for freeing rxctl */
451 uint rxlen; /* Length of valid data in buffer */
452 spinlock_t rxctl_lock; /* protection lock for ctrl frame resources */
454 u8 sdpcm_ver; /* Bus protocol reported by dongle */
456 bool intr; /* Use interrupts */
457 bool poll; /* Use polling */
458 atomic_t ipend; /* Device interrupt is pending */
459 uint spurious; /* Count of spurious interrupts */
460 uint pollrate; /* Ticks between device polls */
461 uint polltick; /* Tick counter */
464 uint console_interval;
465 struct brcmf_console console; /* Console output polling support */
466 uint console_addr; /* Console address from shared struct */
469 uint clkstate; /* State of sd and backplane clock(s) */
470 bool activity; /* Activity flag for clock down */
471 s32 idletime; /* Control for activity timeout */
472 s32 idlecount; /* Activity timeout counter */
473 s32 idleclock; /* How to set bus driver when idle */
474 bool rxflow_mode; /* Rx flow control mode */
475 bool rxflow; /* Is rx flow control on */
476 bool alp_only; /* Don't use HT clock (ALP only) */
480 bool ctrl_frame_stat;
483 wait_queue_head_t ctrl_wait;
484 wait_queue_head_t dcmd_resp_wait;
486 struct timer_list timer;
487 struct completion watchdog_wait;
488 struct task_struct *watchdog_tsk;
492 struct workqueue_struct *brcmf_wq;
493 struct work_struct datawork;
496 const struct firmware *firmware;
499 bool txoff; /* Transmit flow-controlled */
500 struct brcmf_sdio_count sdcnt;
501 bool sr_enabled; /* SaveRestore enabled */
502 bool sleeping; /* SDIO bus sleeping */
508 #define CLK_PENDING 2
512 static int qcount[NUMPRIO];
513 static int tx_packets[NUMPRIO];
516 #define DEFAULT_SDIO_DRIVE_STRENGTH 6 /* in milliamps */
518 #define RETRYCHAN(chan) ((chan) == SDPCM_EVENT_CHANNEL)
520 /* Retry count for register access failures */
521 static const uint retry_limit = 2;
523 /* Limit on rounding up frames */
524 static const uint max_roundup = 512;
528 enum brcmf_sdio_frmtype {
529 BRCMF_SDIO_FT_NORMAL,
534 static void pkt_align(struct sk_buff *p, int len, int align)
537 datalign = (unsigned long)(p->data);
538 datalign = roundup(datalign, (align)) - datalign;
540 skb_pull(p, datalign);
544 /* To check if there's window offered */
545 static bool data_ok(struct brcmf_sdio *bus)
547 return (u8)(bus->tx_max - bus->tx_seq) != 0 &&
548 ((u8)(bus->tx_max - bus->tx_seq) & 0x80) == 0;
552 * Reads a register in the SDIO hardware block. This block occupies a series of
553 * adresses on the 32 bit backplane bus.
556 r_sdreg32(struct brcmf_sdio *bus, u32 *regvar, u32 offset)
558 u8 idx = brcmf_sdio_chip_getinfidx(bus->ci, BCMA_CORE_SDIO_DEV);
561 *regvar = brcmf_sdio_regrl(bus->sdiodev,
562 bus->ci->c_inf[idx].base + offset, &ret);
568 w_sdreg32(struct brcmf_sdio *bus, u32 regval, u32 reg_offset)
570 u8 idx = brcmf_sdio_chip_getinfidx(bus->ci, BCMA_CORE_SDIO_DEV);
573 brcmf_sdio_regwl(bus->sdiodev,
574 bus->ci->c_inf[idx].base + reg_offset,
581 brcmf_sdbrcm_kso_control(struct brcmf_sdio *bus, bool on)
583 u8 wr_val = 0, rd_val, cmp_val, bmask;
587 brcmf_dbg(TRACE, "Enter\n");
589 wr_val = (on << SBSDIO_FUNC1_SLEEPCSR_KSO_SHIFT);
590 /* 1st KSO write goes to AOS wake up core if device is asleep */
591 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
594 brcmf_err("SDIO_AOS KSO write error: %d\n", err);
599 /* device WAKEUP through KSO:
600 * write bit 0 & read back until
601 * both bits 0 (kso bit) & 1 (dev on status) are set
603 cmp_val = SBSDIO_FUNC1_SLEEPCSR_KSO_MASK |
604 SBSDIO_FUNC1_SLEEPCSR_DEVON_MASK;
606 usleep_range(2000, 3000);
608 /* Put device to sleep, turn off KSO */
610 /* only check for bit0, bit1(dev on status) may not
611 * get cleared right away
613 bmask = SBSDIO_FUNC1_SLEEPCSR_KSO_MASK;
617 /* reliable KSO bit set/clr:
618 * the sdiod sleep write access is synced to PMU 32khz clk
619 * just one write attempt may fail,
620 * read it back until it matches written value
622 rd_val = brcmf_sdio_regrb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
624 if (((rd_val & bmask) == cmp_val) && !err)
626 brcmf_dbg(SDIO, "KSO wr/rd retry:%d (max: %d) ERR:%x\n",
627 try_cnt, MAX_KSO_ATTEMPTS, err);
629 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
631 } while (try_cnt++ < MAX_KSO_ATTEMPTS);
636 #define PKT_AVAILABLE() (intstatus & I_HMB_FRAME_IND)
638 #define HOSTINTMASK (I_HMB_SW_MASK | I_CHIPACTIVE)
640 /* Turn backplane clock on or off */
641 static int brcmf_sdbrcm_htclk(struct brcmf_sdio *bus, bool on, bool pendok)
644 u8 clkctl, clkreq, devctl;
645 unsigned long timeout;
647 brcmf_dbg(SDIO, "Enter\n");
651 if (bus->sr_enabled) {
652 bus->clkstate = (on ? CLK_AVAIL : CLK_SDONLY);
657 /* Request HT Avail */
659 bus->alp_only ? SBSDIO_ALP_AVAIL_REQ : SBSDIO_HT_AVAIL_REQ;
661 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
664 brcmf_err("HT Avail request error: %d\n", err);
668 /* Check current status */
669 clkctl = brcmf_sdio_regrb(bus->sdiodev,
670 SBSDIO_FUNC1_CHIPCLKCSR, &err);
672 brcmf_err("HT Avail read error: %d\n", err);
676 /* Go to pending and await interrupt if appropriate */
677 if (!SBSDIO_CLKAV(clkctl, bus->alp_only) && pendok) {
678 /* Allow only clock-available interrupt */
679 devctl = brcmf_sdio_regrb(bus->sdiodev,
680 SBSDIO_DEVICE_CTL, &err);
682 brcmf_err("Devctl error setting CA: %d\n",
687 devctl |= SBSDIO_DEVCTL_CA_INT_ONLY;
688 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
690 brcmf_dbg(SDIO, "CLKCTL: set PENDING\n");
691 bus->clkstate = CLK_PENDING;
694 } else if (bus->clkstate == CLK_PENDING) {
695 /* Cancel CA-only interrupt filter */
696 devctl = brcmf_sdio_regrb(bus->sdiodev,
697 SBSDIO_DEVICE_CTL, &err);
698 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
699 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
703 /* Otherwise, wait here (polling) for HT Avail */
705 msecs_to_jiffies(PMU_MAX_TRANSITION_DLY/1000);
706 while (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
707 clkctl = brcmf_sdio_regrb(bus->sdiodev,
708 SBSDIO_FUNC1_CHIPCLKCSR,
710 if (time_after(jiffies, timeout))
713 usleep_range(5000, 10000);
716 brcmf_err("HT Avail request error: %d\n", err);
719 if (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
720 brcmf_err("HT Avail timeout (%d): clkctl 0x%02x\n",
721 PMU_MAX_TRANSITION_DLY, clkctl);
725 /* Mark clock available */
726 bus->clkstate = CLK_AVAIL;
727 brcmf_dbg(SDIO, "CLKCTL: turned ON\n");
730 if (!bus->alp_only) {
731 if (SBSDIO_ALPONLY(clkctl))
732 brcmf_err("HT Clock should be on\n");
734 #endif /* defined (DEBUG) */
736 bus->activity = true;
740 if (bus->clkstate == CLK_PENDING) {
741 /* Cancel CA-only interrupt filter */
742 devctl = brcmf_sdio_regrb(bus->sdiodev,
743 SBSDIO_DEVICE_CTL, &err);
744 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
745 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
749 bus->clkstate = CLK_SDONLY;
750 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
752 brcmf_dbg(SDIO, "CLKCTL: turned OFF\n");
754 brcmf_err("Failed access turning clock off: %d\n",
762 /* Change idle/active SD state */
763 static int brcmf_sdbrcm_sdclk(struct brcmf_sdio *bus, bool on)
765 brcmf_dbg(SDIO, "Enter\n");
768 bus->clkstate = CLK_SDONLY;
770 bus->clkstate = CLK_NONE;
775 /* Transition SD and backplane clock readiness */
776 static int brcmf_sdbrcm_clkctl(struct brcmf_sdio *bus, uint target, bool pendok)
779 uint oldstate = bus->clkstate;
782 brcmf_dbg(SDIO, "Enter\n");
784 /* Early exit if we're already there */
785 if (bus->clkstate == target) {
786 if (target == CLK_AVAIL) {
787 brcmf_sdbrcm_wd_timer(bus, BRCMF_WD_POLL_MS);
788 bus->activity = true;
795 /* Make sure SD clock is available */
796 if (bus->clkstate == CLK_NONE)
797 brcmf_sdbrcm_sdclk(bus, true);
798 /* Now request HT Avail on the backplane */
799 brcmf_sdbrcm_htclk(bus, true, pendok);
800 brcmf_sdbrcm_wd_timer(bus, BRCMF_WD_POLL_MS);
801 bus->activity = true;
805 /* Remove HT request, or bring up SD clock */
806 if (bus->clkstate == CLK_NONE)
807 brcmf_sdbrcm_sdclk(bus, true);
808 else if (bus->clkstate == CLK_AVAIL)
809 brcmf_sdbrcm_htclk(bus, false, false);
811 brcmf_err("request for %d -> %d\n",
812 bus->clkstate, target);
813 brcmf_sdbrcm_wd_timer(bus, BRCMF_WD_POLL_MS);
817 /* Make sure to remove HT request */
818 if (bus->clkstate == CLK_AVAIL)
819 brcmf_sdbrcm_htclk(bus, false, false);
820 /* Now remove the SD clock */
821 brcmf_sdbrcm_sdclk(bus, false);
822 brcmf_sdbrcm_wd_timer(bus, 0);
826 brcmf_dbg(SDIO, "%d -> %d\n", oldstate, bus->clkstate);
833 brcmf_sdbrcm_bus_sleep(struct brcmf_sdio *bus, bool sleep, bool pendok)
836 brcmf_dbg(TRACE, "Enter\n");
837 brcmf_dbg(SDIO, "request %s currently %s\n",
838 (sleep ? "SLEEP" : "WAKE"),
839 (bus->sleeping ? "SLEEP" : "WAKE"));
841 /* If SR is enabled control bus state with KSO */
842 if (bus->sr_enabled) {
843 /* Done if we're already in the requested state */
844 if (sleep == bus->sleeping)
849 /* Don't sleep if something is pending */
850 if (atomic_read(&bus->intstatus) ||
851 atomic_read(&bus->ipend) > 0 ||
852 (!atomic_read(&bus->fcstate) &&
853 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) &&
856 err = brcmf_sdbrcm_kso_control(bus, false);
857 /* disable watchdog */
859 brcmf_sdbrcm_wd_timer(bus, 0);
862 err = brcmf_sdbrcm_kso_control(bus, true);
866 bus->sleeping = sleep;
867 brcmf_dbg(SDIO, "new state %s\n",
868 (sleep ? "SLEEP" : "WAKE"));
870 brcmf_err("error while changing bus sleep state %d\n",
879 if (!bus->sr_enabled)
880 brcmf_sdbrcm_clkctl(bus, CLK_NONE, pendok);
882 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, pendok);
889 static u32 brcmf_sdbrcm_hostmail(struct brcmf_sdio *bus)
896 brcmf_dbg(SDIO, "Enter\n");
898 /* Read mailbox data and ack that we did so */
899 ret = r_sdreg32(bus, &hmb_data,
900 offsetof(struct sdpcmd_regs, tohostmailboxdata));
903 w_sdreg32(bus, SMB_INT_ACK,
904 offsetof(struct sdpcmd_regs, tosbmailbox));
905 bus->sdcnt.f1regdata += 2;
907 /* Dongle recomposed rx frames, accept them again */
908 if (hmb_data & HMB_DATA_NAKHANDLED) {
909 brcmf_dbg(SDIO, "Dongle reports NAK handled, expect rtx of %d\n",
912 brcmf_err("unexpected NAKHANDLED!\n");
915 intstatus |= I_HMB_FRAME_IND;
919 * DEVREADY does not occur with gSPI.
921 if (hmb_data & (HMB_DATA_DEVREADY | HMB_DATA_FWREADY)) {
923 (hmb_data & HMB_DATA_VERSION_MASK) >>
924 HMB_DATA_VERSION_SHIFT;
925 if (bus->sdpcm_ver != SDPCM_PROT_VERSION)
926 brcmf_err("Version mismatch, dongle reports %d, "
928 bus->sdpcm_ver, SDPCM_PROT_VERSION);
930 brcmf_dbg(SDIO, "Dongle ready, protocol version %d\n",
935 * Flow Control has been moved into the RX headers and this out of band
936 * method isn't used any more.
937 * remaining backward compatible with older dongles.
939 if (hmb_data & HMB_DATA_FC) {
940 fcbits = (hmb_data & HMB_DATA_FCDATA_MASK) >>
941 HMB_DATA_FCDATA_SHIFT;
943 if (fcbits & ~bus->flowcontrol)
944 bus->sdcnt.fc_xoff++;
946 if (bus->flowcontrol & ~fcbits)
949 bus->sdcnt.fc_rcvd++;
950 bus->flowcontrol = fcbits;
953 /* Shouldn't be any others */
954 if (hmb_data & ~(HMB_DATA_DEVREADY |
955 HMB_DATA_NAKHANDLED |
958 HMB_DATA_FCDATA_MASK | HMB_DATA_VERSION_MASK))
959 brcmf_err("Unknown mailbox data content: 0x%02x\n",
965 static void brcmf_sdbrcm_rxfail(struct brcmf_sdio *bus, bool abort, bool rtx)
972 brcmf_err("%sterminate frame%s\n",
973 abort ? "abort command, " : "",
974 rtx ? ", send NAK" : "");
977 brcmf_sdcard_abort(bus->sdiodev, SDIO_FUNC_2);
979 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL,
981 bus->sdcnt.f1regdata++;
983 /* Wait until the packet has been flushed (device/FIFO stable) */
984 for (lastrbc = retries = 0xffff; retries > 0; retries--) {
985 hi = brcmf_sdio_regrb(bus->sdiodev,
986 SBSDIO_FUNC1_RFRAMEBCHI, &err);
987 lo = brcmf_sdio_regrb(bus->sdiodev,
988 SBSDIO_FUNC1_RFRAMEBCLO, &err);
989 bus->sdcnt.f1regdata += 2;
991 if ((hi == 0) && (lo == 0))
994 if ((hi > (lastrbc >> 8)) && (lo > (lastrbc & 0x00ff))) {
995 brcmf_err("count growing: last 0x%04x now 0x%04x\n",
996 lastrbc, (hi << 8) + lo);
998 lastrbc = (hi << 8) + lo;
1002 brcmf_err("count never zeroed: last 0x%04x\n", lastrbc);
1004 brcmf_dbg(SDIO, "flush took %d iterations\n", 0xffff - retries);
1008 err = w_sdreg32(bus, SMB_NAK,
1009 offsetof(struct sdpcmd_regs, tosbmailbox));
1011 bus->sdcnt.f1regdata++;
1016 /* Clear partial in any case */
1017 bus->cur_read.len = 0;
1019 /* If we can't reach the device, signal failure */
1021 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
1024 /* return total length of buffer chain */
1025 static uint brcmf_sdbrcm_glom_len(struct brcmf_sdio *bus)
1031 skb_queue_walk(&bus->glom, p)
1036 static void brcmf_sdbrcm_free_glom(struct brcmf_sdio *bus)
1038 struct sk_buff *cur, *next;
1040 skb_queue_walk_safe(&bus->glom, cur, next) {
1041 skb_unlink(cur, &bus->glom);
1042 brcmu_pkt_buf_free_skb(cur);
1046 static int brcmf_sdio_hdparser(struct brcmf_sdio *bus, u8 *header,
1047 struct brcmf_sdio_read *rd,
1048 enum brcmf_sdio_frmtype type)
1051 u8 rx_seq, fc, tx_seq_max;
1054 * 4 bytes hardware header (frame tag)
1055 * Byte 0~1: Frame length
1056 * Byte 2~3: Checksum, bit-wise inverse of frame length
1058 len = get_unaligned_le16(header);
1059 checksum = get_unaligned_le16(header + sizeof(u16));
1060 /* All zero means no more to read */
1061 if (!(len | checksum)) {
1062 bus->rxpending = false;
1065 if ((u16)(~(len ^ checksum))) {
1066 brcmf_err("HW header checksum error\n");
1067 bus->sdcnt.rx_badhdr++;
1068 brcmf_sdbrcm_rxfail(bus, false, false);
1071 if (len < SDPCM_HDRLEN) {
1072 brcmf_err("HW header length error\n");
1075 if (type == BRCMF_SDIO_FT_SUPER &&
1076 (roundup(len, bus->blocksize) != rd->len)) {
1077 brcmf_err("HW superframe header length error\n");
1080 if (type == BRCMF_SDIO_FT_SUB && len > rd->len) {
1081 brcmf_err("HW subframe header length error\n");
1087 * 8 bytes hardware header
1088 * Byte 0: Rx sequence number
1089 * Byte 1: 4 MSB Channel number, 4 LSB arbitrary flag
1090 * Byte 2: Length of next data frame
1091 * Byte 3: Data offset
1092 * Byte 4: Flow control bits
1093 * Byte 5: Maximum Sequence number allow for Tx
1094 * Byte 6~7: Reserved
1096 if (type == BRCMF_SDIO_FT_SUPER &&
1097 SDPCM_GLOMDESC(&header[SDPCM_FRAMETAG_LEN])) {
1098 brcmf_err("Glom descriptor found in superframe head\n");
1102 rx_seq = SDPCM_PACKET_SEQUENCE(&header[SDPCM_FRAMETAG_LEN]);
1103 rd->channel = SDPCM_PACKET_CHANNEL(&header[SDPCM_FRAMETAG_LEN]);
1104 if (len > MAX_RX_DATASZ && rd->channel != SDPCM_CONTROL_CHANNEL &&
1105 type != BRCMF_SDIO_FT_SUPER) {
1106 brcmf_err("HW header length too long\n");
1107 bus->sdcnt.rx_toolong++;
1108 brcmf_sdbrcm_rxfail(bus, false, false);
1112 if (type == BRCMF_SDIO_FT_SUPER && rd->channel != SDPCM_GLOM_CHANNEL) {
1113 brcmf_err("Wrong channel for superframe\n");
1117 if (type == BRCMF_SDIO_FT_SUB && rd->channel != SDPCM_DATA_CHANNEL &&
1118 rd->channel != SDPCM_EVENT_CHANNEL) {
1119 brcmf_err("Wrong channel for subframe\n");
1123 rd->dat_offset = SDPCM_DOFFSET_VALUE(&header[SDPCM_FRAMETAG_LEN]);
1124 if (rd->dat_offset < SDPCM_HDRLEN || rd->dat_offset > rd->len) {
1125 brcmf_err("seq %d: bad data offset\n", rx_seq);
1126 bus->sdcnt.rx_badhdr++;
1127 brcmf_sdbrcm_rxfail(bus, false, false);
1131 if (rd->seq_num != rx_seq) {
1132 brcmf_err("seq %d: sequence number error, expect %d\n",
1133 rx_seq, rd->seq_num);
1134 bus->sdcnt.rx_badseq++;
1135 rd->seq_num = rx_seq;
1137 /* no need to check the reset for subframe */
1138 if (type == BRCMF_SDIO_FT_SUB)
1140 rd->len_nxtfrm = header[SDPCM_FRAMETAG_LEN + SDPCM_NEXTLEN_OFFSET];
1141 if (rd->len_nxtfrm << 4 > MAX_RX_DATASZ) {
1142 /* only warm for NON glom packet */
1143 if (rd->channel != SDPCM_GLOM_CHANNEL)
1144 brcmf_err("seq %d: next length error\n", rx_seq);
1147 fc = SDPCM_FCMASK_VALUE(&header[SDPCM_FRAMETAG_LEN]);
1148 if (bus->flowcontrol != fc) {
1149 if (~bus->flowcontrol & fc)
1150 bus->sdcnt.fc_xoff++;
1151 if (bus->flowcontrol & ~fc)
1152 bus->sdcnt.fc_xon++;
1153 bus->sdcnt.fc_rcvd++;
1154 bus->flowcontrol = fc;
1156 tx_seq_max = SDPCM_WINDOW_VALUE(&header[SDPCM_FRAMETAG_LEN]);
1157 if ((u8)(tx_seq_max - bus->tx_seq) > 0x40) {
1158 brcmf_err("seq %d: max tx seq number error\n", rx_seq);
1159 tx_seq_max = bus->tx_seq + 2;
1161 bus->tx_max = tx_seq_max;
1166 static u8 brcmf_sdbrcm_rxglom(struct brcmf_sdio *bus, u8 rxseq)
1172 struct sk_buff *pfirst, *pnext;
1177 struct brcmf_sdio_read rd_new;
1179 /* If packets, issue read(s) and send up packet chain */
1180 /* Return sequence numbers consumed? */
1182 brcmf_dbg(SDIO, "start: glomd %p glom %p\n",
1183 bus->glomd, skb_peek(&bus->glom));
1185 /* If there's a descriptor, generate the packet chain */
1187 pfirst = pnext = NULL;
1188 dlen = (u16) (bus->glomd->len);
1189 dptr = bus->glomd->data;
1190 if (!dlen || (dlen & 1)) {
1191 brcmf_err("bad glomd len(%d), ignore descriptor\n",
1196 for (totlen = num = 0; dlen; num++) {
1197 /* Get (and move past) next length */
1198 sublen = get_unaligned_le16(dptr);
1199 dlen -= sizeof(u16);
1200 dptr += sizeof(u16);
1201 if ((sublen < SDPCM_HDRLEN) ||
1202 ((num == 0) && (sublen < (2 * SDPCM_HDRLEN)))) {
1203 brcmf_err("descriptor len %d bad: %d\n",
1208 if (sublen % BRCMF_SDALIGN) {
1209 brcmf_err("sublen %d not multiple of %d\n",
1210 sublen, BRCMF_SDALIGN);
1214 /* For last frame, adjust read len so total
1215 is a block multiple */
1218 (roundup(totlen, bus->blocksize) - totlen);
1219 totlen = roundup(totlen, bus->blocksize);
1222 /* Allocate/chain packet for next subframe */
1223 pnext = brcmu_pkt_buf_get_skb(sublen + BRCMF_SDALIGN);
1224 if (pnext == NULL) {
1225 brcmf_err("bcm_pkt_buf_get_skb failed, num %d len %d\n",
1229 skb_queue_tail(&bus->glom, pnext);
1231 /* Adhere to start alignment requirements */
1232 pkt_align(pnext, sublen, BRCMF_SDALIGN);
1235 /* If all allocations succeeded, save packet chain
1238 brcmf_dbg(GLOM, "allocated %d-byte packet chain for %d subframes\n",
1240 if (BRCMF_GLOM_ON() && bus->cur_read.len &&
1241 totlen != bus->cur_read.len) {
1242 brcmf_dbg(GLOM, "glomdesc mismatch: nextlen %d glomdesc %d rxseq %d\n",
1243 bus->cur_read.len, totlen, rxseq);
1245 pfirst = pnext = NULL;
1247 brcmf_sdbrcm_free_glom(bus);
1251 /* Done with descriptor packet */
1252 brcmu_pkt_buf_free_skb(bus->glomd);
1254 bus->cur_read.len = 0;
1257 /* Ok -- either we just generated a packet chain,
1258 or had one from before */
1259 if (!skb_queue_empty(&bus->glom)) {
1260 if (BRCMF_GLOM_ON()) {
1261 brcmf_dbg(GLOM, "try superframe read, packet chain:\n");
1262 skb_queue_walk(&bus->glom, pnext) {
1263 brcmf_dbg(GLOM, " %p: %p len 0x%04x (%d)\n",
1264 pnext, (u8 *) (pnext->data),
1265 pnext->len, pnext->len);
1269 pfirst = skb_peek(&bus->glom);
1270 dlen = (u16) brcmf_sdbrcm_glom_len(bus);
1272 /* Do an SDIO read for the superframe. Configurable iovar to
1273 * read directly into the chained packet, or allocate a large
1274 * packet and and copy into the chain.
1276 sdio_claim_host(bus->sdiodev->func[1]);
1277 errcode = brcmf_sdcard_recv_chain(bus->sdiodev,
1278 bus->sdiodev->sbwad,
1279 SDIO_FUNC_2, F2SYNC, &bus->glom);
1280 sdio_release_host(bus->sdiodev->func[1]);
1281 bus->sdcnt.f2rxdata++;
1283 /* On failure, kill the superframe, allow a couple retries */
1285 brcmf_err("glom read of %d bytes failed: %d\n",
1288 sdio_claim_host(bus->sdiodev->func[1]);
1289 if (bus->glomerr++ < 3) {
1290 brcmf_sdbrcm_rxfail(bus, true, true);
1293 brcmf_sdbrcm_rxfail(bus, true, false);
1294 bus->sdcnt.rxglomfail++;
1295 brcmf_sdbrcm_free_glom(bus);
1297 sdio_release_host(bus->sdiodev->func[1]);
1301 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
1302 pfirst->data, min_t(int, pfirst->len, 48),
1305 rd_new.seq_num = rxseq;
1307 sdio_claim_host(bus->sdiodev->func[1]);
1308 errcode = brcmf_sdio_hdparser(bus, pfirst->data, &rd_new,
1309 BRCMF_SDIO_FT_SUPER);
1310 sdio_release_host(bus->sdiodev->func[1]);
1311 bus->cur_read.len = rd_new.len_nxtfrm << 4;
1313 /* Remove superframe header, remember offset */
1314 skb_pull(pfirst, rd_new.dat_offset);
1315 sfdoff = rd_new.dat_offset;
1318 /* Validate all the subframe headers */
1319 skb_queue_walk(&bus->glom, pnext) {
1320 /* leave when invalid subframe is found */
1324 rd_new.len = pnext->len;
1325 rd_new.seq_num = rxseq++;
1326 sdio_claim_host(bus->sdiodev->func[1]);
1327 errcode = brcmf_sdio_hdparser(bus, pnext->data, &rd_new,
1329 sdio_release_host(bus->sdiodev->func[1]);
1330 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
1331 pnext->data, 32, "subframe:\n");
1337 /* Terminate frame on error, request
1339 sdio_claim_host(bus->sdiodev->func[1]);
1340 if (bus->glomerr++ < 3) {
1341 /* Restore superframe header space */
1342 skb_push(pfirst, sfdoff);
1343 brcmf_sdbrcm_rxfail(bus, true, true);
1346 brcmf_sdbrcm_rxfail(bus, true, false);
1347 bus->sdcnt.rxglomfail++;
1348 brcmf_sdbrcm_free_glom(bus);
1350 sdio_release_host(bus->sdiodev->func[1]);
1351 bus->cur_read.len = 0;
1355 /* Basic SD framing looks ok - process each packet (header) */
1357 skb_queue_walk_safe(&bus->glom, pfirst, pnext) {
1358 dptr = (u8 *) (pfirst->data);
1359 sublen = get_unaligned_le16(dptr);
1360 doff = SDPCM_DOFFSET_VALUE(&dptr[SDPCM_FRAMETAG_LEN]);
1362 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_DATA_ON(),
1364 "Rx Subframe Data:\n");
1366 __skb_trim(pfirst, sublen);
1367 skb_pull(pfirst, doff);
1369 if (pfirst->len == 0) {
1370 skb_unlink(pfirst, &bus->glom);
1371 brcmu_pkt_buf_free_skb(pfirst);
1375 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
1377 min_t(int, pfirst->len, 32),
1378 "subframe %d to stack, %p (%p/%d) nxt/lnk %p/%p\n",
1379 bus->glom.qlen, pfirst, pfirst->data,
1380 pfirst->len, pfirst->next,
1383 /* sent any remaining packets up */
1385 brcmf_rx_frames(bus->sdiodev->dev, &bus->glom);
1387 bus->sdcnt.rxglomframes++;
1388 bus->sdcnt.rxglompkts += bus->glom.qlen;
1393 static int brcmf_sdbrcm_dcmd_resp_wait(struct brcmf_sdio *bus, uint *condition,
1396 DECLARE_WAITQUEUE(wait, current);
1397 int timeout = msecs_to_jiffies(DCMD_RESP_TIMEOUT);
1399 /* Wait until control frame is available */
1400 add_wait_queue(&bus->dcmd_resp_wait, &wait);
1401 set_current_state(TASK_INTERRUPTIBLE);
1403 while (!(*condition) && (!signal_pending(current) && timeout))
1404 timeout = schedule_timeout(timeout);
1406 if (signal_pending(current))
1409 set_current_state(TASK_RUNNING);
1410 remove_wait_queue(&bus->dcmd_resp_wait, &wait);
1415 static int brcmf_sdbrcm_dcmd_resp_wake(struct brcmf_sdio *bus)
1417 if (waitqueue_active(&bus->dcmd_resp_wait))
1418 wake_up_interruptible(&bus->dcmd_resp_wait);
1423 brcmf_sdbrcm_read_control(struct brcmf_sdio *bus, u8 *hdr, uint len, uint doff)
1426 u8 *buf = NULL, *rbuf;
1429 brcmf_dbg(TRACE, "Enter\n");
1432 buf = vzalloc(bus->rxblen);
1437 pad = ((unsigned long)rbuf % BRCMF_SDALIGN);
1439 rbuf += (BRCMF_SDALIGN - pad);
1441 /* Copy the already-read portion over */
1442 memcpy(buf, hdr, BRCMF_FIRSTREAD);
1443 if (len <= BRCMF_FIRSTREAD)
1446 /* Raise rdlen to next SDIO block to avoid tail command */
1447 rdlen = len - BRCMF_FIRSTREAD;
1448 if (bus->roundup && bus->blocksize && (rdlen > bus->blocksize)) {
1449 pad = bus->blocksize - (rdlen % bus->blocksize);
1450 if ((pad <= bus->roundup) && (pad < bus->blocksize) &&
1451 ((len + pad) < bus->sdiodev->bus_if->maxctl))
1453 } else if (rdlen % BRCMF_SDALIGN) {
1454 rdlen += BRCMF_SDALIGN - (rdlen % BRCMF_SDALIGN);
1457 /* Satisfy length-alignment requirements */
1458 if (rdlen & (ALIGNMENT - 1))
1459 rdlen = roundup(rdlen, ALIGNMENT);
1461 /* Drop if the read is too big or it exceeds our maximum */
1462 if ((rdlen + BRCMF_FIRSTREAD) > bus->sdiodev->bus_if->maxctl) {
1463 brcmf_err("%d-byte control read exceeds %d-byte buffer\n",
1464 rdlen, bus->sdiodev->bus_if->maxctl);
1465 brcmf_sdbrcm_rxfail(bus, false, false);
1469 if ((len - doff) > bus->sdiodev->bus_if->maxctl) {
1470 brcmf_err("%d-byte ctl frame (%d-byte ctl data) exceeds %d-byte limit\n",
1471 len, len - doff, bus->sdiodev->bus_if->maxctl);
1472 bus->sdcnt.rx_toolong++;
1473 brcmf_sdbrcm_rxfail(bus, false, false);
1477 /* Read remain of frame body */
1478 sdret = brcmf_sdcard_recv_buf(bus->sdiodev,
1479 bus->sdiodev->sbwad,
1481 F2SYNC, rbuf, rdlen);
1482 bus->sdcnt.f2rxdata++;
1484 /* Control frame failures need retransmission */
1486 brcmf_err("read %d control bytes failed: %d\n",
1488 bus->sdcnt.rxc_errors++;
1489 brcmf_sdbrcm_rxfail(bus, true, true);
1492 memcpy(buf + BRCMF_FIRSTREAD, rbuf, rdlen);
1496 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_CTL_ON(),
1497 buf, len, "RxCtrl:\n");
1499 /* Point to valid data and indicate its length */
1500 spin_lock_bh(&bus->rxctl_lock);
1502 brcmf_err("last control frame is being processed.\n");
1503 spin_unlock_bh(&bus->rxctl_lock);
1507 bus->rxctl = buf + doff;
1508 bus->rxctl_orig = buf;
1509 bus->rxlen = len - doff;
1510 spin_unlock_bh(&bus->rxctl_lock);
1513 /* Awake any waiters */
1514 brcmf_sdbrcm_dcmd_resp_wake(bus);
1517 /* Pad read to blocksize for efficiency */
1518 static void brcmf_pad(struct brcmf_sdio *bus, u16 *pad, u16 *rdlen)
1520 if (bus->roundup && bus->blocksize && *rdlen > bus->blocksize) {
1521 *pad = bus->blocksize - (*rdlen % bus->blocksize);
1522 if (*pad <= bus->roundup && *pad < bus->blocksize &&
1523 *rdlen + *pad + BRCMF_FIRSTREAD < MAX_RX_DATASZ)
1525 } else if (*rdlen % BRCMF_SDALIGN) {
1526 *rdlen += BRCMF_SDALIGN - (*rdlen % BRCMF_SDALIGN);
1530 static uint brcmf_sdio_readframes(struct brcmf_sdio *bus, uint maxframes)
1532 struct sk_buff *pkt; /* Packet for event or data frames */
1533 struct sk_buff_head pktlist; /* needed for bus interface */
1534 u16 pad; /* Number of pad bytes to read */
1535 uint rxleft = 0; /* Remaining number of frames allowed */
1536 int ret; /* Return code from calls */
1537 uint rxcount = 0; /* Total frames read */
1538 struct brcmf_sdio_read *rd = &bus->cur_read, rd_new;
1541 brcmf_dbg(TRACE, "Enter\n");
1543 /* Not finished unless we encounter no more frames indication */
1544 bus->rxpending = true;
1546 for (rd->seq_num = bus->rx_seq, rxleft = maxframes;
1547 !bus->rxskip && rxleft &&
1548 bus->sdiodev->bus_if->state != BRCMF_BUS_DOWN;
1549 rd->seq_num++, rxleft--) {
1551 /* Handle glomming separately */
1552 if (bus->glomd || !skb_queue_empty(&bus->glom)) {
1554 brcmf_dbg(GLOM, "calling rxglom: glomd %p, glom %p\n",
1555 bus->glomd, skb_peek(&bus->glom));
1556 cnt = brcmf_sdbrcm_rxglom(bus, rd->seq_num);
1557 brcmf_dbg(GLOM, "rxglom returned %d\n", cnt);
1558 rd->seq_num += cnt - 1;
1559 rxleft = (rxleft > cnt) ? (rxleft - cnt) : 1;
1563 rd->len_left = rd->len;
1564 /* read header first for unknow frame length */
1565 sdio_claim_host(bus->sdiodev->func[1]);
1567 ret = brcmf_sdcard_recv_buf(bus->sdiodev,
1568 bus->sdiodev->sbwad,
1569 SDIO_FUNC_2, F2SYNC,
1572 bus->sdcnt.f2rxhdrs++;
1574 brcmf_err("RXHEADER FAILED: %d\n",
1576 bus->sdcnt.rx_hdrfail++;
1577 brcmf_sdbrcm_rxfail(bus, true, true);
1578 sdio_release_host(bus->sdiodev->func[1]);
1582 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() || BRCMF_HDRS_ON(),
1583 bus->rxhdr, SDPCM_HDRLEN,
1586 if (brcmf_sdio_hdparser(bus, bus->rxhdr, rd,
1587 BRCMF_SDIO_FT_NORMAL)) {
1588 sdio_release_host(bus->sdiodev->func[1]);
1589 if (!bus->rxpending)
1595 if (rd->channel == SDPCM_CONTROL_CHANNEL) {
1596 brcmf_sdbrcm_read_control(bus, bus->rxhdr,
1599 /* prepare the descriptor for the next read */
1600 rd->len = rd->len_nxtfrm << 4;
1602 /* treat all packet as event if we don't know */
1603 rd->channel = SDPCM_EVENT_CHANNEL;
1604 sdio_release_host(bus->sdiodev->func[1]);
1607 rd->len_left = rd->len > BRCMF_FIRSTREAD ?
1608 rd->len - BRCMF_FIRSTREAD : 0;
1609 head_read = BRCMF_FIRSTREAD;
1612 brcmf_pad(bus, &pad, &rd->len_left);
1614 pkt = brcmu_pkt_buf_get_skb(rd->len_left + head_read +
1617 /* Give up on data, request rtx of events */
1618 brcmf_err("brcmu_pkt_buf_get_skb failed\n");
1619 brcmf_sdbrcm_rxfail(bus, false,
1620 RETRYCHAN(rd->channel));
1621 sdio_release_host(bus->sdiodev->func[1]);
1624 skb_pull(pkt, head_read);
1625 pkt_align(pkt, rd->len_left, BRCMF_SDALIGN);
1627 ret = brcmf_sdcard_recv_pkt(bus->sdiodev, bus->sdiodev->sbwad,
1628 SDIO_FUNC_2, F2SYNC, pkt);
1629 bus->sdcnt.f2rxdata++;
1630 sdio_release_host(bus->sdiodev->func[1]);
1633 brcmf_err("read %d bytes from channel %d failed: %d\n",
1634 rd->len, rd->channel, ret);
1635 brcmu_pkt_buf_free_skb(pkt);
1636 sdio_claim_host(bus->sdiodev->func[1]);
1637 brcmf_sdbrcm_rxfail(bus, true,
1638 RETRYCHAN(rd->channel));
1639 sdio_release_host(bus->sdiodev->func[1]);
1644 skb_push(pkt, head_read);
1645 memcpy(pkt->data, bus->rxhdr, head_read);
1648 memcpy(bus->rxhdr, pkt->data, SDPCM_HDRLEN);
1649 rd_new.seq_num = rd->seq_num;
1650 sdio_claim_host(bus->sdiodev->func[1]);
1651 if (brcmf_sdio_hdparser(bus, bus->rxhdr, &rd_new,
1652 BRCMF_SDIO_FT_NORMAL)) {
1654 brcmu_pkt_buf_free_skb(pkt);
1656 bus->sdcnt.rx_readahead_cnt++;
1657 if (rd->len != roundup(rd_new.len, 16)) {
1658 brcmf_err("frame length mismatch:read %d, should be %d\n",
1660 roundup(rd_new.len, 16) >> 4);
1662 brcmf_sdbrcm_rxfail(bus, true, true);
1663 sdio_release_host(bus->sdiodev->func[1]);
1664 brcmu_pkt_buf_free_skb(pkt);
1667 sdio_release_host(bus->sdiodev->func[1]);
1668 rd->len_nxtfrm = rd_new.len_nxtfrm;
1669 rd->channel = rd_new.channel;
1670 rd->dat_offset = rd_new.dat_offset;
1672 brcmf_dbg_hex_dump(!(BRCMF_BYTES_ON() &&
1675 bus->rxhdr, SDPCM_HDRLEN,
1678 if (rd_new.channel == SDPCM_CONTROL_CHANNEL) {
1679 brcmf_err("readahead on control packet %d?\n",
1681 /* Force retry w/normal header read */
1683 sdio_claim_host(bus->sdiodev->func[1]);
1684 brcmf_sdbrcm_rxfail(bus, false, true);
1685 sdio_release_host(bus->sdiodev->func[1]);
1686 brcmu_pkt_buf_free_skb(pkt);
1691 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_DATA_ON(),
1692 pkt->data, rd->len, "Rx Data:\n");
1694 /* Save superframe descriptor and allocate packet frame */
1695 if (rd->channel == SDPCM_GLOM_CHANNEL) {
1696 if (SDPCM_GLOMDESC(&bus->rxhdr[SDPCM_FRAMETAG_LEN])) {
1697 brcmf_dbg(GLOM, "glom descriptor, %d bytes:\n",
1699 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
1702 __skb_trim(pkt, rd->len);
1703 skb_pull(pkt, SDPCM_HDRLEN);
1706 brcmf_err("%s: glom superframe w/o "
1707 "descriptor!\n", __func__);
1708 sdio_claim_host(bus->sdiodev->func[1]);
1709 brcmf_sdbrcm_rxfail(bus, false, false);
1710 sdio_release_host(bus->sdiodev->func[1]);
1712 /* prepare the descriptor for the next read */
1713 rd->len = rd->len_nxtfrm << 4;
1715 /* treat all packet as event if we don't know */
1716 rd->channel = SDPCM_EVENT_CHANNEL;
1720 /* Fill in packet len and prio, deliver upward */
1721 __skb_trim(pkt, rd->len);
1722 skb_pull(pkt, rd->dat_offset);
1724 /* prepare the descriptor for the next read */
1725 rd->len = rd->len_nxtfrm << 4;
1727 /* treat all packet as event if we don't know */
1728 rd->channel = SDPCM_EVENT_CHANNEL;
1730 if (pkt->len == 0) {
1731 brcmu_pkt_buf_free_skb(pkt);
1735 skb_queue_head_init(&pktlist);
1736 skb_queue_tail(&pktlist, pkt);
1737 brcmf_rx_frames(bus->sdiodev->dev, &pktlist);
1740 rxcount = maxframes - rxleft;
1741 /* Message if we hit the limit */
1743 brcmf_dbg(DATA, "hit rx limit of %d frames\n", maxframes);
1745 brcmf_dbg(DATA, "processed %d frames\n", rxcount);
1746 /* Back off rxseq if awaiting rtx, update rx_seq */
1749 bus->rx_seq = rd->seq_num;
1755 brcmf_sdbrcm_wait_event_wakeup(struct brcmf_sdio *bus)
1757 if (waitqueue_active(&bus->ctrl_wait))
1758 wake_up_interruptible(&bus->ctrl_wait);
1762 /* Writes a HW/SW header into the packet and sends it. */
1763 /* Assumes: (a) header space already there, (b) caller holds lock */
1764 static int brcmf_sdbrcm_txpkt(struct brcmf_sdio *bus, struct sk_buff *pkt,
1773 brcmf_dbg(TRACE, "Enter\n");
1775 frame = (u8 *) (pkt->data);
1777 /* Add alignment padding, allocate new packet if needed */
1778 pad = ((unsigned long)frame % BRCMF_SDALIGN);
1780 if (skb_headroom(pkt) < pad) {
1781 brcmf_dbg(INFO, "insufficient headroom %d for %d pad\n",
1782 skb_headroom(pkt), pad);
1783 bus->sdiodev->bus_if->tx_realloc++;
1784 ret = skb_cow(pkt, BRCMF_SDALIGN);
1787 pad = ((unsigned long)frame % BRCMF_SDALIGN);
1790 frame = (u8 *) (pkt->data);
1791 memset(frame, 0, pad + SDPCM_HDRLEN);
1793 /* precondition: pad < BRCMF_SDALIGN */
1795 /* Hardware tag: 2 byte len followed by 2 byte ~len check (all LE) */
1796 len = (u16) (pkt->len);
1797 *(__le16 *) frame = cpu_to_le16(len);
1798 *(((__le16 *) frame) + 1) = cpu_to_le16(~len);
1800 /* Software tag: channel, sequence number, data offset */
1802 ((chan << SDPCM_CHANNEL_SHIFT) & SDPCM_CHANNEL_MASK) | bus->tx_seq |
1804 SDPCM_HDRLEN) << SDPCM_DOFFSET_SHIFT) & SDPCM_DOFFSET_MASK);
1806 *(((__le32 *) frame) + 1) = cpu_to_le32(swheader);
1807 *(((__le32 *) frame) + 2) = 0;
1810 tx_packets[pkt->priority]++;
1813 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() &&
1814 ((BRCMF_CTL_ON() && chan == SDPCM_CONTROL_CHANNEL) ||
1815 (BRCMF_DATA_ON() && chan != SDPCM_CONTROL_CHANNEL)),
1816 frame, len, "Tx Frame:\n");
1817 brcmf_dbg_hex_dump(!(BRCMF_BYTES_ON() &&
1819 chan == SDPCM_CONTROL_CHANNEL) ||
1821 chan != SDPCM_CONTROL_CHANNEL))) &&
1823 frame, min_t(u16, len, 16), "TxHdr:\n");
1825 /* Raise len to next SDIO block to eliminate tail command */
1826 if (bus->roundup && bus->blocksize && (len > bus->blocksize)) {
1827 u16 pad = bus->blocksize - (len % bus->blocksize);
1828 if ((pad <= bus->roundup) && (pad < bus->blocksize))
1830 } else if (len % BRCMF_SDALIGN) {
1831 len += BRCMF_SDALIGN - (len % BRCMF_SDALIGN);
1834 /* Some controllers have trouble with odd bytes -- round to even */
1835 if (len & (ALIGNMENT - 1))
1836 len = roundup(len, ALIGNMENT);
1838 sdio_claim_host(bus->sdiodev->func[1]);
1839 ret = brcmf_sdcard_send_pkt(bus->sdiodev, bus->sdiodev->sbwad,
1840 SDIO_FUNC_2, F2SYNC, pkt);
1841 bus->sdcnt.f2txdata++;
1844 /* On failure, abort the command and terminate the frame */
1845 brcmf_dbg(INFO, "sdio error %d, abort command and terminate frame\n",
1847 bus->sdcnt.tx_sderrs++;
1849 brcmf_sdcard_abort(bus->sdiodev, SDIO_FUNC_2);
1850 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL,
1852 bus->sdcnt.f1regdata++;
1854 for (i = 0; i < 3; i++) {
1856 hi = brcmf_sdio_regrb(bus->sdiodev,
1857 SBSDIO_FUNC1_WFRAMEBCHI, NULL);
1858 lo = brcmf_sdio_regrb(bus->sdiodev,
1859 SBSDIO_FUNC1_WFRAMEBCLO, NULL);
1860 bus->sdcnt.f1regdata += 2;
1861 if ((hi == 0) && (lo == 0))
1866 sdio_release_host(bus->sdiodev->func[1]);
1868 bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQUENCE_WRAP;
1871 /* restore pkt buffer pointer before calling tx complete routine */
1872 skb_pull(pkt, SDPCM_HDRLEN + pad);
1873 brcmf_txcomplete(bus->sdiodev->dev, pkt, ret == 0);
1877 static uint brcmf_sdbrcm_sendfromq(struct brcmf_sdio *bus, uint maxframes)
1879 struct sk_buff *pkt;
1881 int ret = 0, prec_out;
1886 brcmf_dbg(TRACE, "Enter\n");
1888 tx_prec_map = ~bus->flowcontrol;
1890 /* Send frames until the limit or some other event */
1891 for (cnt = 0; (cnt < maxframes) && data_ok(bus); cnt++) {
1892 spin_lock_bh(&bus->txqlock);
1893 pkt = brcmu_pktq_mdeq(&bus->txq, tx_prec_map, &prec_out);
1895 spin_unlock_bh(&bus->txqlock);
1898 spin_unlock_bh(&bus->txqlock);
1899 datalen = pkt->len - SDPCM_HDRLEN;
1901 ret = brcmf_sdbrcm_txpkt(bus, pkt, SDPCM_DATA_CHANNEL);
1903 /* In poll mode, need to check for other events */
1904 if (!bus->intr && cnt) {
1905 /* Check device status, signal pending interrupt */
1906 sdio_claim_host(bus->sdiodev->func[1]);
1907 ret = r_sdreg32(bus, &intstatus,
1908 offsetof(struct sdpcmd_regs,
1910 sdio_release_host(bus->sdiodev->func[1]);
1911 bus->sdcnt.f2txdata++;
1914 if (intstatus & bus->hostintmask)
1915 atomic_set(&bus->ipend, 1);
1919 /* Deflow-control stack if needed */
1920 if ((bus->sdiodev->bus_if->state == BRCMF_BUS_DATA) &&
1921 bus->txoff && (pktq_len(&bus->txq) < TXLOW)) {
1923 brcmf_txflowblock(bus->sdiodev->dev, false);
1929 static void brcmf_sdbrcm_bus_stop(struct device *dev)
1931 u32 local_hostintmask;
1934 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1935 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
1936 struct brcmf_sdio *bus = sdiodev->bus;
1938 brcmf_dbg(TRACE, "Enter\n");
1940 if (bus->watchdog_tsk) {
1941 send_sig(SIGTERM, bus->watchdog_tsk, 1);
1942 kthread_stop(bus->watchdog_tsk);
1943 bus->watchdog_tsk = NULL;
1946 sdio_claim_host(bus->sdiodev->func[1]);
1948 /* Enable clock for device interrupts */
1949 brcmf_sdbrcm_bus_sleep(bus, false, false);
1951 /* Disable and clear interrupts at the chip level also */
1952 w_sdreg32(bus, 0, offsetof(struct sdpcmd_regs, hostintmask));
1953 local_hostintmask = bus->hostintmask;
1954 bus->hostintmask = 0;
1956 /* Change our idea of bus state */
1957 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
1959 /* Force clocks on backplane to be sure F2 interrupt propagates */
1960 saveclk = brcmf_sdio_regrb(bus->sdiodev,
1961 SBSDIO_FUNC1_CHIPCLKCSR, &err);
1963 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
1964 (saveclk | SBSDIO_FORCE_HT), &err);
1967 brcmf_err("Failed to force clock for F2: err %d\n", err);
1969 /* Turn off the bus (F2), free any pending packets */
1970 brcmf_dbg(INTR, "disable SDIO interrupts\n");
1971 brcmf_sdio_regwb(bus->sdiodev, SDIO_CCCR_IOEx, SDIO_FUNC_ENABLE_1,
1974 /* Clear any pending interrupts now that F2 is disabled */
1975 w_sdreg32(bus, local_hostintmask,
1976 offsetof(struct sdpcmd_regs, intstatus));
1978 /* Turn off the backplane clock (only) */
1979 brcmf_sdbrcm_clkctl(bus, CLK_SDONLY, false);
1980 sdio_release_host(bus->sdiodev->func[1]);
1982 /* Clear the data packet queues */
1983 brcmu_pktq_flush(&bus->txq, true, NULL, NULL);
1985 /* Clear any held glomming stuff */
1987 brcmu_pkt_buf_free_skb(bus->glomd);
1988 brcmf_sdbrcm_free_glom(bus);
1990 /* Clear rx control and wake any waiters */
1991 spin_lock_bh(&bus->rxctl_lock);
1993 spin_unlock_bh(&bus->rxctl_lock);
1994 brcmf_sdbrcm_dcmd_resp_wake(bus);
1996 /* Reset some F2 state stuff */
1997 bus->rxskip = false;
1998 bus->tx_seq = bus->rx_seq = 0;
2001 static inline void brcmf_sdbrcm_clrintr(struct brcmf_sdio *bus)
2003 unsigned long flags;
2005 if (bus->sdiodev->oob_irq_requested) {
2006 spin_lock_irqsave(&bus->sdiodev->irq_en_lock, flags);
2007 if (!bus->sdiodev->irq_en && !atomic_read(&bus->ipend)) {
2008 enable_irq(bus->sdiodev->pdata->oob_irq_nr);
2009 bus->sdiodev->irq_en = true;
2011 spin_unlock_irqrestore(&bus->sdiodev->irq_en_lock, flags);
2015 static int brcmf_sdio_intr_rstatus(struct brcmf_sdio *bus)
2022 idx = brcmf_sdio_chip_getinfidx(bus->ci, BCMA_CORE_SDIO_DEV);
2023 addr = bus->ci->c_inf[idx].base +
2024 offsetof(struct sdpcmd_regs, intstatus);
2026 ret = brcmf_sdio_regrw_helper(bus->sdiodev, addr, &val, false);
2027 bus->sdcnt.f1regdata++;
2031 val &= bus->hostintmask;
2032 atomic_set(&bus->fcstate, !!(val & I_HMB_FC_STATE));
2034 /* Clear interrupts */
2036 ret = brcmf_sdio_regrw_helper(bus->sdiodev, addr, &val, true);
2037 bus->sdcnt.f1regdata++;
2041 atomic_set(&bus->intstatus, 0);
2043 for_each_set_bit(n, &val, 32)
2044 set_bit(n, (unsigned long *)&bus->intstatus.counter);
2050 static void brcmf_sdbrcm_dpc(struct brcmf_sdio *bus)
2053 unsigned long intstatus;
2054 uint rxlimit = bus->rxbound; /* Rx frames to read before resched */
2055 uint txlimit = bus->txbound; /* Tx frames to send before resched */
2056 uint framecnt = 0; /* Temporary counter of tx/rx frames */
2059 brcmf_dbg(TRACE, "Enter\n");
2061 sdio_claim_host(bus->sdiodev->func[1]);
2063 /* If waiting for HTAVAIL, check status */
2064 if (!bus->sr_enabled && bus->clkstate == CLK_PENDING) {
2065 u8 clkctl, devctl = 0;
2068 /* Check for inconsistent device control */
2069 devctl = brcmf_sdio_regrb(bus->sdiodev,
2070 SBSDIO_DEVICE_CTL, &err);
2072 brcmf_err("error reading DEVCTL: %d\n", err);
2073 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
2077 /* Read CSR, if clock on switch to AVAIL, else ignore */
2078 clkctl = brcmf_sdio_regrb(bus->sdiodev,
2079 SBSDIO_FUNC1_CHIPCLKCSR, &err);
2081 brcmf_err("error reading CSR: %d\n",
2083 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
2086 brcmf_dbg(SDIO, "DPC: PENDING, devctl 0x%02x clkctl 0x%02x\n",
2089 if (SBSDIO_HTAV(clkctl)) {
2090 devctl = brcmf_sdio_regrb(bus->sdiodev,
2091 SBSDIO_DEVICE_CTL, &err);
2093 brcmf_err("error reading DEVCTL: %d\n",
2095 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
2097 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
2098 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
2101 brcmf_err("error writing DEVCTL: %d\n",
2103 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
2105 bus->clkstate = CLK_AVAIL;
2109 /* Make sure backplane clock is on */
2110 brcmf_sdbrcm_bus_sleep(bus, false, true);
2112 /* Pending interrupt indicates new device status */
2113 if (atomic_read(&bus->ipend) > 0) {
2114 atomic_set(&bus->ipend, 0);
2115 err = brcmf_sdio_intr_rstatus(bus);
2118 /* Start with leftover status bits */
2119 intstatus = atomic_xchg(&bus->intstatus, 0);
2121 /* Handle flow-control change: read new state in case our ack
2122 * crossed another change interrupt. If change still set, assume
2123 * FC ON for safety, let next loop through do the debounce.
2125 if (intstatus & I_HMB_FC_CHANGE) {
2126 intstatus &= ~I_HMB_FC_CHANGE;
2127 err = w_sdreg32(bus, I_HMB_FC_CHANGE,
2128 offsetof(struct sdpcmd_regs, intstatus));
2130 err = r_sdreg32(bus, &newstatus,
2131 offsetof(struct sdpcmd_regs, intstatus));
2132 bus->sdcnt.f1regdata += 2;
2133 atomic_set(&bus->fcstate,
2134 !!(newstatus & (I_HMB_FC_STATE | I_HMB_FC_CHANGE)));
2135 intstatus |= (newstatus & bus->hostintmask);
2138 /* Handle host mailbox indication */
2139 if (intstatus & I_HMB_HOST_INT) {
2140 intstatus &= ~I_HMB_HOST_INT;
2141 intstatus |= brcmf_sdbrcm_hostmail(bus);
2144 sdio_release_host(bus->sdiodev->func[1]);
2146 /* Generally don't ask for these, can get CRC errors... */
2147 if (intstatus & I_WR_OOSYNC) {
2148 brcmf_err("Dongle reports WR_OOSYNC\n");
2149 intstatus &= ~I_WR_OOSYNC;
2152 if (intstatus & I_RD_OOSYNC) {
2153 brcmf_err("Dongle reports RD_OOSYNC\n");
2154 intstatus &= ~I_RD_OOSYNC;
2157 if (intstatus & I_SBINT) {
2158 brcmf_err("Dongle reports SBINT\n");
2159 intstatus &= ~I_SBINT;
2162 /* Would be active due to wake-wlan in gSPI */
2163 if (intstatus & I_CHIPACTIVE) {
2164 brcmf_dbg(INFO, "Dongle reports CHIPACTIVE\n");
2165 intstatus &= ~I_CHIPACTIVE;
2168 /* Ignore frame indications if rxskip is set */
2170 intstatus &= ~I_HMB_FRAME_IND;
2172 /* On frame indication, read available frames */
2173 if (PKT_AVAILABLE() && bus->clkstate == CLK_AVAIL) {
2174 framecnt = brcmf_sdio_readframes(bus, rxlimit);
2175 if (!bus->rxpending)
2176 intstatus &= ~I_HMB_FRAME_IND;
2177 rxlimit -= min(framecnt, rxlimit);
2180 /* Keep still-pending events for next scheduling */
2182 for_each_set_bit(n, &intstatus, 32)
2183 set_bit(n, (unsigned long *)&bus->intstatus.counter);
2186 brcmf_sdbrcm_clrintr(bus);
2188 if (data_ok(bus) && bus->ctrl_frame_stat &&
2189 (bus->clkstate == CLK_AVAIL)) {
2192 sdio_claim_host(bus->sdiodev->func[1]);
2193 err = brcmf_sdcard_send_buf(bus->sdiodev, bus->sdiodev->sbwad,
2194 SDIO_FUNC_2, F2SYNC, bus->ctrl_frame_buf,
2195 (u32) bus->ctrl_frame_len);
2198 /* On failure, abort the command and
2199 terminate the frame */
2200 brcmf_dbg(INFO, "sdio error %d, abort command and terminate frame\n",
2202 bus->sdcnt.tx_sderrs++;
2204 brcmf_sdcard_abort(bus->sdiodev, SDIO_FUNC_2);
2206 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL,
2208 bus->sdcnt.f1regdata++;
2210 for (i = 0; i < 3; i++) {
2212 hi = brcmf_sdio_regrb(bus->sdiodev,
2213 SBSDIO_FUNC1_WFRAMEBCHI,
2215 lo = brcmf_sdio_regrb(bus->sdiodev,
2216 SBSDIO_FUNC1_WFRAMEBCLO,
2218 bus->sdcnt.f1regdata += 2;
2219 if ((hi == 0) && (lo == 0))
2224 bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQUENCE_WRAP;
2226 sdio_release_host(bus->sdiodev->func[1]);
2227 bus->ctrl_frame_stat = false;
2228 brcmf_sdbrcm_wait_event_wakeup(bus);
2230 /* Send queued frames (limit 1 if rx may still be pending) */
2231 else if ((bus->clkstate == CLK_AVAIL) && !atomic_read(&bus->fcstate) &&
2232 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) && txlimit
2234 framecnt = bus->rxpending ? min(txlimit, bus->txminmax) :
2236 framecnt = brcmf_sdbrcm_sendfromq(bus, framecnt);
2237 txlimit -= framecnt;
2240 if ((bus->sdiodev->bus_if->state == BRCMF_BUS_DOWN) || (err != 0)) {
2241 brcmf_err("failed backplane access over SDIO, halting operation\n");
2242 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
2243 atomic_set(&bus->intstatus, 0);
2244 } else if (atomic_read(&bus->intstatus) ||
2245 atomic_read(&bus->ipend) > 0 ||
2246 (!atomic_read(&bus->fcstate) &&
2247 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) &&
2248 data_ok(bus)) || PKT_AVAILABLE()) {
2249 atomic_inc(&bus->dpc_tskcnt);
2252 /* If we're done for now, turn off clock request. */
2253 if ((bus->clkstate != CLK_PENDING)
2254 && bus->idletime == BRCMF_IDLE_IMMEDIATE) {
2255 bus->activity = false;
2256 brcmf_dbg(SDIO, "idle state\n");
2257 sdio_claim_host(bus->sdiodev->func[1]);
2258 brcmf_sdbrcm_bus_sleep(bus, true, false);
2259 sdio_release_host(bus->sdiodev->func[1]);
2263 static struct pktq *brcmf_sdbrcm_bus_gettxq(struct device *dev)
2265 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
2266 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
2267 struct brcmf_sdio *bus = sdiodev->bus;
2272 static int brcmf_sdbrcm_bus_txdata(struct device *dev, struct sk_buff *pkt)
2276 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
2277 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
2278 struct brcmf_sdio *bus = sdiodev->bus;
2281 brcmf_dbg(TRACE, "Enter\n");
2285 /* Add space for the header */
2286 skb_push(pkt, SDPCM_HDRLEN);
2287 /* precondition: IS_ALIGNED((unsigned long)(pkt->data), 2) */
2289 prec = prio2prec((pkt->priority & PRIOMASK));
2291 /* Check for existing queue, current flow-control,
2292 pending event, or pending clock */
2293 brcmf_dbg(TRACE, "deferring pktq len %d\n", pktq_len(&bus->txq));
2294 bus->sdcnt.fcqueued++;
2296 /* Priority based enq */
2297 spin_lock_irqsave(&bus->txqlock, flags);
2298 if (!brcmf_c_prec_enq(bus->sdiodev->dev, &bus->txq, pkt, prec)) {
2299 skb_pull(pkt, SDPCM_HDRLEN);
2300 brcmf_txcomplete(bus->sdiodev->dev, pkt, false);
2301 brcmf_err("out of bus->txq !!!\n");
2307 if (pktq_len(&bus->txq) >= TXHI) {
2309 brcmf_txflowblock(bus->sdiodev->dev, true);
2311 spin_unlock_irqrestore(&bus->txqlock, flags);
2314 if (pktq_plen(&bus->txq, prec) > qcount[prec])
2315 qcount[prec] = pktq_plen(&bus->txq, prec);
2318 if (atomic_read(&bus->dpc_tskcnt) == 0) {
2319 atomic_inc(&bus->dpc_tskcnt);
2320 queue_work(bus->brcmf_wq, &bus->datawork);
2327 #define CONSOLE_LINE_MAX 192
2329 static int brcmf_sdbrcm_readconsole(struct brcmf_sdio *bus)
2331 struct brcmf_console *c = &bus->console;
2332 u8 line[CONSOLE_LINE_MAX], ch;
2336 /* Don't do anything until FWREADY updates console address */
2337 if (bus->console_addr == 0)
2340 /* Read console log struct */
2341 addr = bus->console_addr + offsetof(struct rte_console, log_le);
2342 rv = brcmf_sdio_ramrw(bus->sdiodev, false, addr, (u8 *)&c->log_le,
2347 /* Allocate console buffer (one time only) */
2348 if (c->buf == NULL) {
2349 c->bufsize = le32_to_cpu(c->log_le.buf_size);
2350 c->buf = kmalloc(c->bufsize, GFP_ATOMIC);
2355 idx = le32_to_cpu(c->log_le.idx);
2357 /* Protect against corrupt value */
2358 if (idx > c->bufsize)
2361 /* Skip reading the console buffer if the index pointer
2366 /* Read the console buffer */
2367 addr = le32_to_cpu(c->log_le.buf);
2368 rv = brcmf_sdio_ramrw(bus->sdiodev, false, addr, c->buf, c->bufsize);
2372 while (c->last != idx) {
2373 for (n = 0; n < CONSOLE_LINE_MAX - 2; n++) {
2374 if (c->last == idx) {
2375 /* This would output a partial line.
2377 * the buffer pointer and output this
2378 * line next time around.
2383 c->last = c->bufsize - n;
2386 ch = c->buf[c->last];
2387 c->last = (c->last + 1) % c->bufsize;
2394 if (line[n - 1] == '\r')
2397 pr_debug("CONSOLE: %s\n", line);
2406 static int brcmf_tx_frame(struct brcmf_sdio *bus, u8 *frame, u16 len)
2411 bus->ctrl_frame_stat = false;
2412 ret = brcmf_sdcard_send_buf(bus->sdiodev, bus->sdiodev->sbwad,
2413 SDIO_FUNC_2, F2SYNC, frame, len);
2416 /* On failure, abort the command and terminate the frame */
2417 brcmf_dbg(INFO, "sdio error %d, abort command and terminate frame\n",
2419 bus->sdcnt.tx_sderrs++;
2421 brcmf_sdcard_abort(bus->sdiodev, SDIO_FUNC_2);
2423 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL,
2425 bus->sdcnt.f1regdata++;
2427 for (i = 0; i < 3; i++) {
2429 hi = brcmf_sdio_regrb(bus->sdiodev,
2430 SBSDIO_FUNC1_WFRAMEBCHI, NULL);
2431 lo = brcmf_sdio_regrb(bus->sdiodev,
2432 SBSDIO_FUNC1_WFRAMEBCLO, NULL);
2433 bus->sdcnt.f1regdata += 2;
2434 if (hi == 0 && lo == 0)
2440 bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQUENCE_WRAP;
2446 brcmf_sdbrcm_bus_txctl(struct device *dev, unsigned char *msg, uint msglen)
2454 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
2455 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
2456 struct brcmf_sdio *bus = sdiodev->bus;
2458 brcmf_dbg(TRACE, "Enter\n");
2460 /* Back the pointer to make a room for bus header */
2461 frame = msg - SDPCM_HDRLEN;
2462 len = (msglen += SDPCM_HDRLEN);
2464 /* Add alignment padding (optional for ctl frames) */
2465 doff = ((unsigned long)frame % BRCMF_SDALIGN);
2470 memset(frame, 0, doff + SDPCM_HDRLEN);
2472 /* precondition: doff < BRCMF_SDALIGN */
2473 doff += SDPCM_HDRLEN;
2475 /* Round send length to next SDIO block */
2476 if (bus->roundup && bus->blocksize && (len > bus->blocksize)) {
2477 u16 pad = bus->blocksize - (len % bus->blocksize);
2478 if ((pad <= bus->roundup) && (pad < bus->blocksize))
2480 } else if (len % BRCMF_SDALIGN) {
2481 len += BRCMF_SDALIGN - (len % BRCMF_SDALIGN);
2484 /* Satisfy length-alignment requirements */
2485 if (len & (ALIGNMENT - 1))
2486 len = roundup(len, ALIGNMENT);
2488 /* precondition: IS_ALIGNED((unsigned long)frame, 2) */
2490 /* Make sure backplane clock is on */
2491 sdio_claim_host(bus->sdiodev->func[1]);
2492 brcmf_sdbrcm_bus_sleep(bus, false, false);
2493 sdio_release_host(bus->sdiodev->func[1]);
2495 /* Hardware tag: 2 byte len followed by 2 byte ~len check (all LE) */
2496 *(__le16 *) frame = cpu_to_le16((u16) msglen);
2497 *(((__le16 *) frame) + 1) = cpu_to_le16(~msglen);
2499 /* Software tag: channel, sequence number, data offset */
2501 ((SDPCM_CONTROL_CHANNEL << SDPCM_CHANNEL_SHIFT) &
2503 | bus->tx_seq | ((doff << SDPCM_DOFFSET_SHIFT) &
2504 SDPCM_DOFFSET_MASK);
2505 put_unaligned_le32(swheader, frame + SDPCM_FRAMETAG_LEN);
2506 put_unaligned_le32(0, frame + SDPCM_FRAMETAG_LEN + sizeof(swheader));
2508 if (!data_ok(bus)) {
2509 brcmf_dbg(INFO, "No bus credit bus->tx_max %d, bus->tx_seq %d\n",
2510 bus->tx_max, bus->tx_seq);
2511 bus->ctrl_frame_stat = true;
2513 bus->ctrl_frame_buf = frame;
2514 bus->ctrl_frame_len = len;
2516 wait_event_interruptible_timeout(bus->ctrl_wait,
2517 !bus->ctrl_frame_stat,
2518 msecs_to_jiffies(2000));
2520 if (!bus->ctrl_frame_stat) {
2521 brcmf_dbg(SDIO, "ctrl_frame_stat == false\n");
2524 brcmf_dbg(SDIO, "ctrl_frame_stat == true\n");
2530 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_CTL_ON(),
2531 frame, len, "Tx Frame:\n");
2532 brcmf_dbg_hex_dump(!(BRCMF_BYTES_ON() && BRCMF_CTL_ON()) &&
2534 frame, min_t(u16, len, 16), "TxHdr:\n");
2537 sdio_claim_host(bus->sdiodev->func[1]);
2538 ret = brcmf_tx_frame(bus, frame, len);
2539 sdio_release_host(bus->sdiodev->func[1]);
2540 } while (ret < 0 && retries++ < TXRETRIES);
2543 if ((bus->idletime == BRCMF_IDLE_IMMEDIATE) &&
2544 atomic_read(&bus->dpc_tskcnt) == 0) {
2545 bus->activity = false;
2546 sdio_claim_host(bus->sdiodev->func[1]);
2547 brcmf_dbg(INFO, "idle\n");
2548 brcmf_sdbrcm_clkctl(bus, CLK_NONE, true);
2549 sdio_release_host(bus->sdiodev->func[1]);
2553 bus->sdcnt.tx_ctlerrs++;
2555 bus->sdcnt.tx_ctlpkts++;
2557 return ret ? -EIO : 0;
2561 static inline bool brcmf_sdio_valid_shared_address(u32 addr)
2563 return !(addr == 0 || ((~addr >> 16) & 0xffff) == (addr & 0xffff));
2566 static int brcmf_sdio_readshared(struct brcmf_sdio *bus,
2567 struct sdpcm_shared *sh)
2572 struct sdpcm_shared_le sh_le;
2575 shaddr = bus->ci->rambase + bus->ramsize - 4;
2578 * Read last word in socram to determine
2579 * address of sdpcm_shared structure
2581 sdio_claim_host(bus->sdiodev->func[1]);
2582 brcmf_sdbrcm_bus_sleep(bus, false, false);
2583 rv = brcmf_sdio_ramrw(bus->sdiodev, false, shaddr, (u8 *)&addr_le, 4);
2584 sdio_release_host(bus->sdiodev->func[1]);
2588 addr = le32_to_cpu(addr_le);
2590 brcmf_dbg(SDIO, "sdpcm_shared address 0x%08X\n", addr);
2593 * Check if addr is valid.
2594 * NVRAM length at the end of memory should have been overwritten.
2596 if (!brcmf_sdio_valid_shared_address(addr)) {
2597 brcmf_err("invalid sdpcm_shared address 0x%08X\n",
2602 /* Read hndrte_shared structure */
2603 rv = brcmf_sdio_ramrw(bus->sdiodev, false, addr, (u8 *)&sh_le,
2604 sizeof(struct sdpcm_shared_le));
2609 sh->flags = le32_to_cpu(sh_le.flags);
2610 sh->trap_addr = le32_to_cpu(sh_le.trap_addr);
2611 sh->assert_exp_addr = le32_to_cpu(sh_le.assert_exp_addr);
2612 sh->assert_file_addr = le32_to_cpu(sh_le.assert_file_addr);
2613 sh->assert_line = le32_to_cpu(sh_le.assert_line);
2614 sh->console_addr = le32_to_cpu(sh_le.console_addr);
2615 sh->msgtrace_addr = le32_to_cpu(sh_le.msgtrace_addr);
2617 if ((sh->flags & SDPCM_SHARED_VERSION_MASK) > SDPCM_SHARED_VERSION) {
2618 brcmf_err("sdpcm shared version unsupported: dhd %d dongle %d\n",
2619 SDPCM_SHARED_VERSION,
2620 sh->flags & SDPCM_SHARED_VERSION_MASK);
2627 static int brcmf_sdio_dump_console(struct brcmf_sdio *bus,
2628 struct sdpcm_shared *sh, char __user *data,
2631 u32 addr, console_ptr, console_size, console_index;
2632 char *conbuf = NULL;
2638 /* obtain console information from device memory */
2639 addr = sh->console_addr + offsetof(struct rte_console, log_le);
2640 rv = brcmf_sdio_ramrw(bus->sdiodev, false, addr,
2641 (u8 *)&sh_val, sizeof(u32));
2644 console_ptr = le32_to_cpu(sh_val);
2646 addr = sh->console_addr + offsetof(struct rte_console, log_le.buf_size);
2647 rv = brcmf_sdio_ramrw(bus->sdiodev, false, addr,
2648 (u8 *)&sh_val, sizeof(u32));
2651 console_size = le32_to_cpu(sh_val);
2653 addr = sh->console_addr + offsetof(struct rte_console, log_le.idx);
2654 rv = brcmf_sdio_ramrw(bus->sdiodev, false, addr,
2655 (u8 *)&sh_val, sizeof(u32));
2658 console_index = le32_to_cpu(sh_val);
2660 /* allocate buffer for console data */
2661 if (console_size <= CONSOLE_BUFFER_MAX)
2662 conbuf = vzalloc(console_size+1);
2667 /* obtain the console data from device */
2668 conbuf[console_size] = '\0';
2669 rv = brcmf_sdio_ramrw(bus->sdiodev, false, console_ptr, (u8 *)conbuf,
2674 rv = simple_read_from_buffer(data, count, &pos,
2675 conbuf + console_index,
2676 console_size - console_index);
2681 if (console_index > 0) {
2683 rv = simple_read_from_buffer(data+nbytes, count, &pos,
2684 conbuf, console_index - 1);
2694 static int brcmf_sdio_trap_info(struct brcmf_sdio *bus, struct sdpcm_shared *sh,
2695 char __user *data, size_t count)
2699 struct brcmf_trap_info tr;
2702 if ((sh->flags & SDPCM_SHARED_TRAP) == 0) {
2703 brcmf_dbg(INFO, "no trap in firmware\n");
2707 error = brcmf_sdio_ramrw(bus->sdiodev, false, sh->trap_addr, (u8 *)&tr,
2708 sizeof(struct brcmf_trap_info));
2712 res = scnprintf(buf, sizeof(buf),
2713 "dongle trap info: type 0x%x @ epc 0x%08x\n"
2714 " cpsr 0x%08x spsr 0x%08x sp 0x%08x\n"
2715 " lr 0x%08x pc 0x%08x offset 0x%x\n"
2716 " r0 0x%08x r1 0x%08x r2 0x%08x r3 0x%08x\n"
2717 " r4 0x%08x r5 0x%08x r6 0x%08x r7 0x%08x\n",
2718 le32_to_cpu(tr.type), le32_to_cpu(tr.epc),
2719 le32_to_cpu(tr.cpsr), le32_to_cpu(tr.spsr),
2720 le32_to_cpu(tr.r13), le32_to_cpu(tr.r14),
2721 le32_to_cpu(tr.pc), sh->trap_addr,
2722 le32_to_cpu(tr.r0), le32_to_cpu(tr.r1),
2723 le32_to_cpu(tr.r2), le32_to_cpu(tr.r3),
2724 le32_to_cpu(tr.r4), le32_to_cpu(tr.r5),
2725 le32_to_cpu(tr.r6), le32_to_cpu(tr.r7));
2727 return simple_read_from_buffer(data, count, &pos, buf, res);
2730 static int brcmf_sdio_assert_info(struct brcmf_sdio *bus,
2731 struct sdpcm_shared *sh, char __user *data,
2736 char file[80] = "?";
2737 char expr[80] = "<???>";
2741 if ((sh->flags & SDPCM_SHARED_ASSERT_BUILT) == 0) {
2742 brcmf_dbg(INFO, "firmware not built with -assert\n");
2744 } else if ((sh->flags & SDPCM_SHARED_ASSERT) == 0) {
2745 brcmf_dbg(INFO, "no assert in dongle\n");
2749 sdio_claim_host(bus->sdiodev->func[1]);
2750 if (sh->assert_file_addr != 0) {
2751 error = brcmf_sdio_ramrw(bus->sdiodev, false,
2752 sh->assert_file_addr, (u8 *)file, 80);
2756 if (sh->assert_exp_addr != 0) {
2757 error = brcmf_sdio_ramrw(bus->sdiodev, false,
2758 sh->assert_exp_addr, (u8 *)expr, 80);
2762 sdio_release_host(bus->sdiodev->func[1]);
2764 res = scnprintf(buf, sizeof(buf),
2765 "dongle assert: %s:%d: assert(%s)\n",
2766 file, sh->assert_line, expr);
2767 return simple_read_from_buffer(data, count, &pos, buf, res);
2770 static int brcmf_sdbrcm_checkdied(struct brcmf_sdio *bus)
2773 struct sdpcm_shared sh;
2775 error = brcmf_sdio_readshared(bus, &sh);
2780 if ((sh.flags & SDPCM_SHARED_ASSERT_BUILT) == 0)
2781 brcmf_dbg(INFO, "firmware not built with -assert\n");
2782 else if (sh.flags & SDPCM_SHARED_ASSERT)
2783 brcmf_err("assertion in dongle\n");
2785 if (sh.flags & SDPCM_SHARED_TRAP)
2786 brcmf_err("firmware trap in dongle\n");
2791 static int brcmf_sdbrcm_died_dump(struct brcmf_sdio *bus, char __user *data,
2792 size_t count, loff_t *ppos)
2795 struct sdpcm_shared sh;
2802 error = brcmf_sdio_readshared(bus, &sh);
2806 error = brcmf_sdio_assert_info(bus, &sh, data, count);
2811 error = brcmf_sdio_trap_info(bus, &sh, data+nbytes, count);
2816 error = brcmf_sdio_dump_console(bus, &sh, data+nbytes, count);
2827 static ssize_t brcmf_sdio_forensic_read(struct file *f, char __user *data,
2828 size_t count, loff_t *ppos)
2830 struct brcmf_sdio *bus = f->private_data;
2833 res = brcmf_sdbrcm_died_dump(bus, data, count, ppos);
2836 return (ssize_t)res;
2839 static const struct file_operations brcmf_sdio_forensic_ops = {
2840 .owner = THIS_MODULE,
2841 .open = simple_open,
2842 .read = brcmf_sdio_forensic_read
2845 static void brcmf_sdio_debugfs_create(struct brcmf_sdio *bus)
2847 struct brcmf_pub *drvr = bus->sdiodev->bus_if->drvr;
2848 struct dentry *dentry = brcmf_debugfs_get_devdir(drvr);
2850 if (IS_ERR_OR_NULL(dentry))
2853 debugfs_create_file("forensics", S_IRUGO, dentry, bus,
2854 &brcmf_sdio_forensic_ops);
2855 brcmf_debugfs_create_sdio_count(drvr, &bus->sdcnt);
2858 static int brcmf_sdbrcm_checkdied(struct brcmf_sdio *bus)
2863 static void brcmf_sdio_debugfs_create(struct brcmf_sdio *bus)
2869 brcmf_sdbrcm_bus_rxctl(struct device *dev, unsigned char *msg, uint msglen)
2875 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
2876 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
2877 struct brcmf_sdio *bus = sdiodev->bus;
2879 brcmf_dbg(TRACE, "Enter\n");
2881 /* Wait until control frame is available */
2882 timeleft = brcmf_sdbrcm_dcmd_resp_wait(bus, &bus->rxlen, &pending);
2884 spin_lock_bh(&bus->rxctl_lock);
2886 memcpy(msg, bus->rxctl, min(msglen, rxlen));
2888 buf = bus->rxctl_orig;
2889 bus->rxctl_orig = NULL;
2891 spin_unlock_bh(&bus->rxctl_lock);
2895 brcmf_dbg(CTL, "resumed on rxctl frame, got %d expected %d\n",
2897 } else if (timeleft == 0) {
2898 brcmf_err("resumed on timeout\n");
2899 brcmf_sdbrcm_checkdied(bus);
2900 } else if (pending) {
2901 brcmf_dbg(CTL, "cancelled\n");
2902 return -ERESTARTSYS;
2904 brcmf_dbg(CTL, "resumed for unknown reason?\n");
2905 brcmf_sdbrcm_checkdied(bus);
2909 bus->sdcnt.rx_ctlpkts++;
2911 bus->sdcnt.rx_ctlerrs++;
2913 return rxlen ? (int)rxlen : -ETIMEDOUT;
2916 static bool brcmf_sdbrcm_download_state(struct brcmf_sdio *bus, bool enter)
2918 struct chip_info *ci = bus->ci;
2920 /* To enter download state, disable ARM and reset SOCRAM.
2921 * To exit download state, simply reset ARM (default is RAM boot).
2924 bus->alp_only = true;
2926 brcmf_sdio_chip_enter_download(bus->sdiodev, ci);
2928 if (!brcmf_sdio_chip_exit_download(bus->sdiodev, ci, bus->vars,
2932 /* Allow HT Clock now that the ARM is running. */
2933 bus->alp_only = false;
2935 bus->sdiodev->bus_if->state = BRCMF_BUS_LOAD;
2941 static int brcmf_sdbrcm_get_image(char *buf, int len, struct brcmf_sdio *bus)
2943 if (bus->firmware->size < bus->fw_ptr + len)
2944 len = bus->firmware->size - bus->fw_ptr;
2946 memcpy(buf, &bus->firmware->data[bus->fw_ptr], len);
2951 static int brcmf_sdbrcm_download_code_file(struct brcmf_sdio *bus)
2955 u8 *memblock = NULL, *memptr;
2959 brcmf_dbg(INFO, "Enter\n");
2961 ret = request_firmware(&bus->firmware, BRCMF_SDIO_FW_NAME,
2962 &bus->sdiodev->func[2]->dev);
2964 brcmf_err("Fail to request firmware %d\n", ret);
2969 memptr = memblock = kmalloc(MEMBLOCK + BRCMF_SDALIGN, GFP_ATOMIC);
2970 if (memblock == NULL) {
2974 if ((u32)(unsigned long)memblock % BRCMF_SDALIGN)
2975 memptr += (BRCMF_SDALIGN -
2976 ((u32)(unsigned long)memblock % BRCMF_SDALIGN));
2978 offset = bus->ci->rambase;
2980 /* Download image */
2981 len = brcmf_sdbrcm_get_image((char *)memptr, MEMBLOCK, bus);
2982 idx = brcmf_sdio_chip_getinfidx(bus->ci, BCMA_CORE_ARM_CR4);
2983 if (BRCMF_MAX_CORENUM != idx)
2984 memcpy(&bus->ci->rst_vec, memptr, sizeof(bus->ci->rst_vec));
2986 ret = brcmf_sdio_ramrw(bus->sdiodev, true, offset, memptr, len);
2988 brcmf_err("error %d on writing %d membytes at 0x%08x\n",
2989 ret, MEMBLOCK, offset);
2994 len = brcmf_sdbrcm_get_image((char *)memptr, MEMBLOCK, bus);
3000 release_firmware(bus->firmware);
3007 * ProcessVars:Takes a buffer of "<var>=<value>\n" lines read from a file
3008 * and ending in a NUL.
3009 * Removes carriage returns, empty lines, comment lines, and converts
3011 * Shortens buffer as needed and pads with NULs. End of buffer is marked
3015 static int brcmf_process_nvram_vars(struct brcmf_sdio *bus)
3022 uint buf_len, n, len;
3024 len = bus->firmware->size;
3025 varbuf = vmalloc(len);
3029 memcpy(varbuf, bus->firmware->data, len);
3032 findNewline = false;
3035 for (n = 0; n < len; n++) {
3038 if (varbuf[n] == '\r')
3040 if (findNewline && varbuf[n] != '\n')
3042 findNewline = false;
3043 if (varbuf[n] == '#') {
3047 if (varbuf[n] == '\n') {
3057 buf_len = dp - varbuf;
3058 while (dp < varbuf + n)
3062 /* roundup needed for download to device */
3063 bus->varsz = roundup(buf_len + 1, 4);
3064 bus->vars = kmalloc(bus->varsz, GFP_KERNEL);
3065 if (bus->vars == NULL) {
3071 /* copy the processed variables and add null termination */
3072 memcpy(bus->vars, varbuf, buf_len);
3073 bus->vars[buf_len] = 0;
3079 static int brcmf_sdbrcm_download_nvram(struct brcmf_sdio *bus)
3083 ret = request_firmware(&bus->firmware, BRCMF_SDIO_NV_NAME,
3084 &bus->sdiodev->func[2]->dev);
3086 brcmf_err("Fail to request nvram %d\n", ret);
3090 ret = brcmf_process_nvram_vars(bus);
3092 release_firmware(bus->firmware);
3097 static int _brcmf_sdbrcm_download_firmware(struct brcmf_sdio *bus)
3101 /* Keep arm in reset */
3102 if (!brcmf_sdbrcm_download_state(bus, true)) {
3103 brcmf_err("error placing ARM core in reset\n");
3107 if (brcmf_sdbrcm_download_code_file(bus)) {
3108 brcmf_err("dongle image file download failed\n");
3112 if (brcmf_sdbrcm_download_nvram(bus)) {
3113 brcmf_err("dongle nvram file download failed\n");
3117 /* Take arm out of reset */
3118 if (!brcmf_sdbrcm_download_state(bus, false)) {
3119 brcmf_err("error getting out of ARM core reset\n");
3129 static bool brcmf_sdbrcm_sr_capable(struct brcmf_sdio *bus)
3133 brcmf_dbg(TRACE, "Enter\n");
3135 /* old chips with PMU version less than 17 don't support save restore */
3136 if (bus->ci->pmurev < 17)
3139 /* read PMU chipcontrol register 3*/
3140 addr = CORE_CC_REG(bus->ci->c_inf[0].base, chipcontrol_addr);
3141 brcmf_sdio_regwl(bus->sdiodev, addr, 3, NULL);
3142 addr = CORE_CC_REG(bus->ci->c_inf[0].base, chipcontrol_data);
3143 reg = brcmf_sdio_regrl(bus->sdiodev, addr, NULL);
3148 static void brcmf_sdbrcm_sr_init(struct brcmf_sdio *bus)
3153 brcmf_dbg(TRACE, "Enter\n");
3155 val = brcmf_sdio_regrb(bus->sdiodev, SBSDIO_FUNC1_WAKEUPCTRL,
3158 brcmf_err("error reading SBSDIO_FUNC1_WAKEUPCTRL\n");
3162 val |= 1 << SBSDIO_FUNC1_WCTRL_HTWAIT_SHIFT;
3163 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_WAKEUPCTRL,
3166 brcmf_err("error writing SBSDIO_FUNC1_WAKEUPCTRL\n");
3170 /* Add CMD14 Support */
3171 brcmf_sdio_regwb(bus->sdiodev, SDIO_CCCR_BRCM_CARDCAP,
3172 (SDIO_CCCR_BRCM_CARDCAP_CMD14_SUPPORT |
3173 SDIO_CCCR_BRCM_CARDCAP_CMD14_EXT),
3176 brcmf_err("error writing SDIO_CCCR_BRCM_CARDCAP\n");
3180 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3181 SBSDIO_FORCE_HT, &err);
3183 brcmf_err("error writing SBSDIO_FUNC1_CHIPCLKCSR\n");
3188 bus->sr_enabled = true;
3189 brcmf_dbg(INFO, "SR enabled\n");
3192 /* enable KSO bit */
3193 static int brcmf_sdbrcm_kso_init(struct brcmf_sdio *bus)
3198 brcmf_dbg(TRACE, "Enter\n");
3200 /* KSO bit added in SDIO core rev 12 */
3201 if (bus->ci->c_inf[1].rev < 12)
3204 val = brcmf_sdio_regrb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
3207 brcmf_err("error reading SBSDIO_FUNC1_SLEEPCSR\n");
3211 if (!(val & SBSDIO_FUNC1_SLEEPCSR_KSO_MASK)) {
3212 val |= (SBSDIO_FUNC1_SLEEPCSR_KSO_EN <<
3213 SBSDIO_FUNC1_SLEEPCSR_KSO_SHIFT);
3214 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
3217 brcmf_err("error writing SBSDIO_FUNC1_SLEEPCSR\n");
3227 brcmf_sdbrcm_download_firmware(struct brcmf_sdio *bus)
3231 sdio_claim_host(bus->sdiodev->func[1]);
3233 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
3235 ret = _brcmf_sdbrcm_download_firmware(bus) == 0;
3237 brcmf_sdbrcm_clkctl(bus, CLK_SDONLY, false);
3239 sdio_release_host(bus->sdiodev->func[1]);
3244 static int brcmf_sdbrcm_bus_init(struct device *dev)
3246 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3247 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3248 struct brcmf_sdio *bus = sdiodev->bus;
3249 unsigned long timeout;
3254 brcmf_dbg(TRACE, "Enter\n");
3256 /* try to download image and nvram to the dongle */
3257 if (bus_if->state == BRCMF_BUS_DOWN) {
3258 if (!(brcmf_sdbrcm_download_firmware(bus)))
3262 if (!bus->sdiodev->bus_if->drvr)
3265 /* Start the watchdog timer */
3266 bus->sdcnt.tickcnt = 0;
3267 brcmf_sdbrcm_wd_timer(bus, BRCMF_WD_POLL_MS);
3269 sdio_claim_host(bus->sdiodev->func[1]);
3271 /* Make sure backplane clock is on, needed to generate F2 interrupt */
3272 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
3273 if (bus->clkstate != CLK_AVAIL)
3276 /* Force clocks on backplane to be sure F2 interrupt propagates */
3277 saveclk = brcmf_sdio_regrb(bus->sdiodev,
3278 SBSDIO_FUNC1_CHIPCLKCSR, &err);
3280 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3281 (saveclk | SBSDIO_FORCE_HT), &err);
3284 brcmf_err("Failed to force clock for F2: err %d\n", err);
3288 /* Enable function 2 (frame transfers) */
3289 w_sdreg32(bus, SDPCM_PROT_VERSION << SMB_DATA_VERSION_SHIFT,
3290 offsetof(struct sdpcmd_regs, tosbmailboxdata));
3291 enable = (SDIO_FUNC_ENABLE_1 | SDIO_FUNC_ENABLE_2);
3293 brcmf_sdio_regwb(bus->sdiodev, SDIO_CCCR_IOEx, enable, NULL);
3295 timeout = jiffies + msecs_to_jiffies(BRCMF_WAIT_F2RDY);
3297 while (enable != ready) {
3298 ready = brcmf_sdio_regrb(bus->sdiodev,
3299 SDIO_CCCR_IORx, NULL);
3300 if (time_after(jiffies, timeout))
3302 else if (time_after(jiffies, timeout - BRCMF_WAIT_F2RDY + 50))
3303 /* prevent busy waiting if it takes too long */
3304 msleep_interruptible(20);
3307 brcmf_dbg(INFO, "enable 0x%02x, ready 0x%02x\n", enable, ready);
3309 /* If F2 successfully enabled, set core and enable interrupts */
3310 if (ready == enable) {
3311 /* Set up the interrupt mask and enable interrupts */
3312 bus->hostintmask = HOSTINTMASK;
3313 w_sdreg32(bus, bus->hostintmask,
3314 offsetof(struct sdpcmd_regs, hostintmask));
3316 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_WATERMARK, 8, &err);
3318 /* Disable F2 again */
3319 enable = SDIO_FUNC_ENABLE_1;
3320 brcmf_sdio_regwb(bus->sdiodev, SDIO_CCCR_IOEx, enable, NULL);
3324 if (brcmf_sdbrcm_sr_capable(bus)) {
3325 brcmf_sdbrcm_sr_init(bus);
3327 /* Restore previous clock setting */
3328 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3333 ret = brcmf_sdio_intr_register(bus->sdiodev);
3335 brcmf_err("intr register failed:%d\n", ret);
3338 /* If we didn't come up, turn off backplane clock */
3339 if (bus_if->state != BRCMF_BUS_DATA)
3340 brcmf_sdbrcm_clkctl(bus, CLK_NONE, false);
3343 sdio_release_host(bus->sdiodev->func[1]);
3348 void brcmf_sdbrcm_isr(void *arg)
3350 struct brcmf_sdio *bus = (struct brcmf_sdio *) arg;
3352 brcmf_dbg(TRACE, "Enter\n");
3355 brcmf_err("bus is null pointer, exiting\n");
3359 if (bus->sdiodev->bus_if->state == BRCMF_BUS_DOWN) {
3360 brcmf_err("bus is down. we have nothing to do\n");
3363 /* Count the interrupt call */
3364 bus->sdcnt.intrcount++;
3366 atomic_set(&bus->ipend, 1);
3368 if (brcmf_sdio_intr_rstatus(bus)) {
3369 brcmf_err("failed backplane access\n");
3370 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
3373 /* Disable additional interrupts (is this needed now)? */
3375 brcmf_err("isr w/o interrupt configured!\n");
3377 atomic_inc(&bus->dpc_tskcnt);
3378 queue_work(bus->brcmf_wq, &bus->datawork);
3381 static bool brcmf_sdbrcm_bus_watchdog(struct brcmf_sdio *bus)
3384 struct brcmf_bus *bus_if = dev_get_drvdata(bus->sdiodev->dev);
3387 brcmf_dbg(TIMER, "Enter\n");
3389 /* Poll period: check device if appropriate. */
3390 if (!bus->sr_enabled &&
3391 bus->poll && (++bus->polltick >= bus->pollrate)) {
3394 /* Reset poll tick */
3397 /* Check device if no interrupts */
3399 (bus->sdcnt.intrcount == bus->sdcnt.lastintrs)) {
3401 if (atomic_read(&bus->dpc_tskcnt) == 0) {
3404 sdio_claim_host(bus->sdiodev->func[1]);
3405 devpend = brcmf_sdio_regrb(bus->sdiodev,
3408 sdio_release_host(bus->sdiodev->func[1]);
3410 devpend & (INTR_STATUS_FUNC1 |
3414 /* If there is something, make like the ISR and
3417 bus->sdcnt.pollcnt++;
3418 atomic_set(&bus->ipend, 1);
3420 atomic_inc(&bus->dpc_tskcnt);
3421 queue_work(bus->brcmf_wq, &bus->datawork);
3425 /* Update interrupt tracking */
3426 bus->sdcnt.lastintrs = bus->sdcnt.intrcount;
3429 /* Poll for console output periodically */
3430 if (bus_if && bus_if->state == BRCMF_BUS_DATA &&
3431 bus->console_interval != 0) {
3432 bus->console.count += BRCMF_WD_POLL_MS;
3433 if (bus->console.count >= bus->console_interval) {
3434 bus->console.count -= bus->console_interval;
3435 sdio_claim_host(bus->sdiodev->func[1]);
3436 /* Make sure backplane clock is on */
3437 brcmf_sdbrcm_bus_sleep(bus, false, false);
3438 if (brcmf_sdbrcm_readconsole(bus) < 0)
3440 bus->console_interval = 0;
3441 sdio_release_host(bus->sdiodev->func[1]);
3446 /* On idle timeout clear activity flag and/or turn off clock */
3447 if ((bus->idletime > 0) && (bus->clkstate == CLK_AVAIL)) {
3448 if (++bus->idlecount >= bus->idletime) {
3450 if (bus->activity) {
3451 bus->activity = false;
3452 brcmf_sdbrcm_wd_timer(bus, BRCMF_WD_POLL_MS);
3454 brcmf_dbg(SDIO, "idle\n");
3455 sdio_claim_host(bus->sdiodev->func[1]);
3456 brcmf_sdbrcm_bus_sleep(bus, true, false);
3457 sdio_release_host(bus->sdiodev->func[1]);
3462 return (atomic_read(&bus->ipend) > 0);
3465 static void brcmf_sdio_dataworker(struct work_struct *work)
3467 struct brcmf_sdio *bus = container_of(work, struct brcmf_sdio,
3470 while (atomic_read(&bus->dpc_tskcnt)) {
3471 brcmf_sdbrcm_dpc(bus);
3472 atomic_dec(&bus->dpc_tskcnt);
3476 static void brcmf_sdbrcm_release_malloc(struct brcmf_sdio *bus)
3478 brcmf_dbg(TRACE, "Enter\n");
3481 bus->rxctl = bus->rxbuf = NULL;
3485 static bool brcmf_sdbrcm_probe_malloc(struct brcmf_sdio *bus)
3487 brcmf_dbg(TRACE, "Enter\n");
3489 if (bus->sdiodev->bus_if->maxctl) {
3491 roundup((bus->sdiodev->bus_if->maxctl + SDPCM_HDRLEN),
3492 ALIGNMENT) + BRCMF_SDALIGN;
3493 bus->rxbuf = kmalloc(bus->rxblen, GFP_ATOMIC);
3502 brcmf_sdbrcm_probe_attach(struct brcmf_sdio *bus, u32 regsva)
3510 bus->alp_only = true;
3512 sdio_claim_host(bus->sdiodev->func[1]);
3514 pr_debug("F1 signature read @0x18000000=0x%4x\n",
3515 brcmf_sdio_regrl(bus->sdiodev, SI_ENUM_BASE, NULL));
3518 * Force PLL off until brcmf_sdio_chip_attach()
3519 * programs PLL control regs
3522 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3523 BRCMF_INIT_CLKCTL1, &err);
3525 clkctl = brcmf_sdio_regrb(bus->sdiodev,
3526 SBSDIO_FUNC1_CHIPCLKCSR, &err);
3528 if (err || ((clkctl & ~SBSDIO_AVBITS) != BRCMF_INIT_CLKCTL1)) {
3529 brcmf_err("ChipClkCSR access: err %d wrote 0x%02x read 0x%02x\n",
3530 err, BRCMF_INIT_CLKCTL1, clkctl);
3534 if (brcmf_sdio_chip_attach(bus->sdiodev, &bus->ci, regsva)) {
3535 brcmf_err("brcmf_sdio_chip_attach failed!\n");
3539 if (brcmf_sdbrcm_kso_init(bus)) {
3540 brcmf_err("error enabling KSO\n");
3544 if ((bus->sdiodev->pdata) && (bus->sdiodev->pdata->drive_strength))
3545 drivestrength = bus->sdiodev->pdata->drive_strength;
3547 drivestrength = DEFAULT_SDIO_DRIVE_STRENGTH;
3548 brcmf_sdio_chip_drivestrengthinit(bus->sdiodev, bus->ci, drivestrength);
3550 /* Get info on the SOCRAM cores... */
3551 bus->ramsize = bus->ci->ramsize;
3552 if (!(bus->ramsize)) {
3553 brcmf_err("failed to find SOCRAM memory!\n");
3557 /* Set card control so an SDIO card reset does a WLAN backplane reset */
3558 reg_val = brcmf_sdio_regrb(bus->sdiodev,
3559 SDIO_CCCR_BRCM_CARDCTRL, &err);
3563 reg_val |= SDIO_CCCR_BRCM_CARDCTRL_WLANRESET;
3565 brcmf_sdio_regwb(bus->sdiodev,
3566 SDIO_CCCR_BRCM_CARDCTRL, reg_val, &err);
3570 /* set PMUControl so a backplane reset does PMU state reload */
3571 reg_addr = CORE_CC_REG(bus->ci->c_inf[0].base,
3573 reg_val = brcmf_sdio_regrl(bus->sdiodev,
3579 reg_val |= (BCMA_CC_PMU_CTL_RES_RELOAD << BCMA_CC_PMU_CTL_RES_SHIFT);
3581 brcmf_sdio_regwl(bus->sdiodev,
3589 sdio_release_host(bus->sdiodev->func[1]);
3591 brcmu_pktq_init(&bus->txq, (PRIOMASK + 1), TXQLEN);
3593 /* Locate an appropriately-aligned portion of hdrbuf */
3594 bus->rxhdr = (u8 *) roundup((unsigned long)&bus->hdrbuf[0],
3597 /* Set the poll and/or interrupt flags */
3606 sdio_release_host(bus->sdiodev->func[1]);
3610 static bool brcmf_sdbrcm_probe_init(struct brcmf_sdio *bus)
3612 brcmf_dbg(TRACE, "Enter\n");
3614 sdio_claim_host(bus->sdiodev->func[1]);
3616 /* Disable F2 to clear any intermediate frame state on the dongle */
3617 brcmf_sdio_regwb(bus->sdiodev, SDIO_CCCR_IOEx,
3618 SDIO_FUNC_ENABLE_1, NULL);
3620 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
3621 bus->rxflow = false;
3623 /* Done with backplane-dependent accesses, can drop clock... */
3624 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, 0, NULL);
3626 sdio_release_host(bus->sdiodev->func[1]);
3628 /* ...and initialize clock/power states */
3629 bus->clkstate = CLK_SDONLY;
3630 bus->idletime = BRCMF_IDLE_INTERVAL;
3631 bus->idleclock = BRCMF_IDLE_ACTIVE;
3633 /* Query the F2 block size, set roundup accordingly */
3634 bus->blocksize = bus->sdiodev->func[2]->cur_blksize;
3635 bus->roundup = min(max_roundup, bus->blocksize);
3638 bus->sleeping = false;
3639 bus->sr_enabled = false;
3645 brcmf_sdbrcm_watchdog_thread(void *data)
3647 struct brcmf_sdio *bus = (struct brcmf_sdio *)data;
3649 allow_signal(SIGTERM);
3650 /* Run until signal received */
3652 if (kthread_should_stop())
3654 if (!wait_for_completion_interruptible(&bus->watchdog_wait)) {
3655 brcmf_sdbrcm_bus_watchdog(bus);
3656 /* Count the tick for reference */
3657 bus->sdcnt.tickcnt++;
3665 brcmf_sdbrcm_watchdog(unsigned long data)
3667 struct brcmf_sdio *bus = (struct brcmf_sdio *)data;
3669 if (bus->watchdog_tsk) {
3670 complete(&bus->watchdog_wait);
3671 /* Reschedule the watchdog */
3672 if (bus->wd_timer_valid)
3673 mod_timer(&bus->timer,
3674 jiffies + BRCMF_WD_POLL_MS * HZ / 1000);
3678 static void brcmf_sdbrcm_release_dongle(struct brcmf_sdio *bus)
3680 brcmf_dbg(TRACE, "Enter\n");
3683 sdio_claim_host(bus->sdiodev->func[1]);
3684 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
3685 brcmf_sdbrcm_clkctl(bus, CLK_NONE, false);
3686 sdio_release_host(bus->sdiodev->func[1]);
3687 brcmf_sdio_chip_detach(&bus->ci);
3688 if (bus->vars && bus->varsz)
3693 brcmf_dbg(TRACE, "Disconnected\n");
3696 /* Detach and free everything */
3697 static void brcmf_sdbrcm_release(struct brcmf_sdio *bus)
3699 brcmf_dbg(TRACE, "Enter\n");
3702 /* De-register interrupt handler */
3703 brcmf_sdio_intr_unregister(bus->sdiodev);
3705 cancel_work_sync(&bus->datawork);
3707 destroy_workqueue(bus->brcmf_wq);
3709 if (bus->sdiodev->bus_if->drvr) {
3710 brcmf_detach(bus->sdiodev->dev);
3711 brcmf_sdbrcm_release_dongle(bus);
3714 brcmf_sdbrcm_release_malloc(bus);
3719 brcmf_dbg(TRACE, "Disconnected\n");
3722 static struct brcmf_bus_ops brcmf_sdio_bus_ops = {
3723 .stop = brcmf_sdbrcm_bus_stop,
3724 .init = brcmf_sdbrcm_bus_init,
3725 .txdata = brcmf_sdbrcm_bus_txdata,
3726 .txctl = brcmf_sdbrcm_bus_txctl,
3727 .rxctl = brcmf_sdbrcm_bus_rxctl,
3728 .gettxq = brcmf_sdbrcm_bus_gettxq,
3731 void *brcmf_sdbrcm_probe(u32 regsva, struct brcmf_sdio_dev *sdiodev)
3734 struct brcmf_sdio *bus;
3735 struct brcmf_bus_dcmd *dlst;
3737 u32 dngl_txglomalign;
3740 brcmf_dbg(TRACE, "Enter\n");
3742 /* We make an assumption about address window mappings:
3743 * regsva == SI_ENUM_BASE*/
3745 /* Allocate private bus interface state */
3746 bus = kzalloc(sizeof(struct brcmf_sdio), GFP_ATOMIC);
3750 bus->sdiodev = sdiodev;
3752 skb_queue_head_init(&bus->glom);
3753 bus->txbound = BRCMF_TXBOUND;
3754 bus->rxbound = BRCMF_RXBOUND;
3755 bus->txminmax = BRCMF_TXMINMAX;
3756 bus->tx_seq = SDPCM_SEQUENCE_WRAP - 1;
3758 INIT_WORK(&bus->datawork, brcmf_sdio_dataworker);
3759 bus->brcmf_wq = create_singlethread_workqueue("brcmf_wq");
3760 if (bus->brcmf_wq == NULL) {
3761 brcmf_err("insufficient memory to create txworkqueue\n");
3765 /* attempt to attach to the dongle */
3766 if (!(brcmf_sdbrcm_probe_attach(bus, regsva))) {
3767 brcmf_err("brcmf_sdbrcm_probe_attach failed\n");
3771 spin_lock_init(&bus->rxctl_lock);
3772 spin_lock_init(&bus->txqlock);
3773 init_waitqueue_head(&bus->ctrl_wait);
3774 init_waitqueue_head(&bus->dcmd_resp_wait);
3776 /* Set up the watchdog timer */
3777 init_timer(&bus->timer);
3778 bus->timer.data = (unsigned long)bus;
3779 bus->timer.function = brcmf_sdbrcm_watchdog;
3781 /* Initialize watchdog thread */
3782 init_completion(&bus->watchdog_wait);
3783 bus->watchdog_tsk = kthread_run(brcmf_sdbrcm_watchdog_thread,
3784 bus, "brcmf_watchdog");
3785 if (IS_ERR(bus->watchdog_tsk)) {
3786 pr_warn("brcmf_watchdog thread failed to start\n");
3787 bus->watchdog_tsk = NULL;
3789 /* Initialize DPC thread */
3790 atomic_set(&bus->dpc_tskcnt, 0);
3792 /* Assign bus interface call back */
3793 bus->sdiodev->bus_if->dev = bus->sdiodev->dev;
3794 bus->sdiodev->bus_if->ops = &brcmf_sdio_bus_ops;
3795 bus->sdiodev->bus_if->chip = bus->ci->chip;
3796 bus->sdiodev->bus_if->chiprev = bus->ci->chiprev;
3798 /* Attach to the brcmf/OS/network interface */
3799 ret = brcmf_attach(SDPCM_RESERVE, bus->sdiodev->dev);
3801 brcmf_err("brcmf_attach failed\n");
3805 /* Allocate buffers */
3806 if (!(brcmf_sdbrcm_probe_malloc(bus))) {
3807 brcmf_err("brcmf_sdbrcm_probe_malloc failed\n");
3811 if (!(brcmf_sdbrcm_probe_init(bus))) {
3812 brcmf_err("brcmf_sdbrcm_probe_init failed\n");
3816 brcmf_sdio_debugfs_create(bus);
3817 brcmf_dbg(INFO, "completed!!\n");
3819 /* sdio bus core specific dcmd */
3820 idx = brcmf_sdio_chip_getinfidx(bus->ci, BCMA_CORE_SDIO_DEV);
3821 dlst = kzalloc(sizeof(struct brcmf_bus_dcmd), GFP_KERNEL);
3823 if (bus->ci->c_inf[idx].rev < 12) {
3824 /* for sdio core rev < 12, disable txgloming */
3826 dlst->name = "bus:txglom";
3827 dlst->param = (char *)&dngl_txglom;
3828 dlst->param_len = sizeof(u32);
3830 /* otherwise, set txglomalign */
3831 dngl_txglomalign = bus->sdiodev->bus_if->align;
3832 dlst->name = "bus:txglomalign";
3833 dlst->param = (char *)&dngl_txglomalign;
3834 dlst->param_len = sizeof(u32);
3836 list_add(&dlst->list, &bus->sdiodev->bus_if->dcmd_list);
3839 /* if firmware path present try to download and bring up bus */
3840 ret = brcmf_bus_start(bus->sdiodev->dev);
3842 brcmf_err("dongle is not responding\n");
3849 brcmf_sdbrcm_release(bus);
3853 void brcmf_sdbrcm_disconnect(void *ptr)
3855 struct brcmf_sdio *bus = (struct brcmf_sdio *)ptr;
3857 brcmf_dbg(TRACE, "Enter\n");
3860 brcmf_sdbrcm_release(bus);
3862 brcmf_dbg(TRACE, "Disconnected\n");
3866 brcmf_sdbrcm_wd_timer(struct brcmf_sdio *bus, uint wdtick)
3868 /* Totally stop the timer */
3869 if (!wdtick && bus->wd_timer_valid) {
3870 del_timer_sync(&bus->timer);
3871 bus->wd_timer_valid = false;
3872 bus->save_ms = wdtick;
3876 /* don't start the wd until fw is loaded */
3877 if (bus->sdiodev->bus_if->state == BRCMF_BUS_DOWN)
3881 if (bus->save_ms != BRCMF_WD_POLL_MS) {
3882 if (bus->wd_timer_valid)
3883 /* Stop timer and restart at new value */
3884 del_timer_sync(&bus->timer);
3886 /* Create timer again when watchdog period is
3887 dynamically changed or in the first instance
3889 bus->timer.expires =
3890 jiffies + BRCMF_WD_POLL_MS * HZ / 1000;
3891 add_timer(&bus->timer);
3894 /* Re arm the timer, at last watchdog period */
3895 mod_timer(&bus->timer,
3896 jiffies + BRCMF_WD_POLL_MS * HZ / 1000);
3899 bus->wd_timer_valid = true;
3900 bus->save_ms = wdtick;