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;
2280 brcmf_dbg(TRACE, "Enter\n");
2284 /* Add space for the header */
2285 skb_push(pkt, SDPCM_HDRLEN);
2286 /* precondition: IS_ALIGNED((unsigned long)(pkt->data), 2) */
2288 prec = prio2prec((pkt->priority & PRIOMASK));
2290 /* Check for existing queue, current flow-control,
2291 pending event, or pending clock */
2292 brcmf_dbg(TRACE, "deferring pktq len %d\n", pktq_len(&bus->txq));
2293 bus->sdcnt.fcqueued++;
2295 /* Priority based enq */
2296 spin_lock_bh(&bus->txqlock);
2297 if (!brcmf_c_prec_enq(bus->sdiodev->dev, &bus->txq, pkt, prec)) {
2298 skb_pull(pkt, SDPCM_HDRLEN);
2299 brcmf_txcomplete(bus->sdiodev->dev, pkt, false);
2300 brcmf_err("out of bus->txq !!!\n");
2306 if (pktq_len(&bus->txq) >= TXHI) {
2308 brcmf_txflowblock(bus->sdiodev->dev, true);
2310 spin_unlock_bh(&bus->txqlock);
2313 if (pktq_plen(&bus->txq, prec) > qcount[prec])
2314 qcount[prec] = pktq_plen(&bus->txq, prec);
2317 if (atomic_read(&bus->dpc_tskcnt) == 0) {
2318 atomic_inc(&bus->dpc_tskcnt);
2319 queue_work(bus->brcmf_wq, &bus->datawork);
2326 #define CONSOLE_LINE_MAX 192
2328 static int brcmf_sdbrcm_readconsole(struct brcmf_sdio *bus)
2330 struct brcmf_console *c = &bus->console;
2331 u8 line[CONSOLE_LINE_MAX], ch;
2335 /* Don't do anything until FWREADY updates console address */
2336 if (bus->console_addr == 0)
2339 /* Read console log struct */
2340 addr = bus->console_addr + offsetof(struct rte_console, log_le);
2341 rv = brcmf_sdio_ramrw(bus->sdiodev, false, addr, (u8 *)&c->log_le,
2346 /* Allocate console buffer (one time only) */
2347 if (c->buf == NULL) {
2348 c->bufsize = le32_to_cpu(c->log_le.buf_size);
2349 c->buf = kmalloc(c->bufsize, GFP_ATOMIC);
2354 idx = le32_to_cpu(c->log_le.idx);
2356 /* Protect against corrupt value */
2357 if (idx > c->bufsize)
2360 /* Skip reading the console buffer if the index pointer
2365 /* Read the console buffer */
2366 addr = le32_to_cpu(c->log_le.buf);
2367 rv = brcmf_sdio_ramrw(bus->sdiodev, false, addr, c->buf, c->bufsize);
2371 while (c->last != idx) {
2372 for (n = 0; n < CONSOLE_LINE_MAX - 2; n++) {
2373 if (c->last == idx) {
2374 /* This would output a partial line.
2376 * the buffer pointer and output this
2377 * line next time around.
2382 c->last = c->bufsize - n;
2385 ch = c->buf[c->last];
2386 c->last = (c->last + 1) % c->bufsize;
2393 if (line[n - 1] == '\r')
2396 pr_debug("CONSOLE: %s\n", line);
2405 static int brcmf_tx_frame(struct brcmf_sdio *bus, u8 *frame, u16 len)
2410 bus->ctrl_frame_stat = false;
2411 ret = brcmf_sdcard_send_buf(bus->sdiodev, bus->sdiodev->sbwad,
2412 SDIO_FUNC_2, F2SYNC, frame, len);
2415 /* On failure, abort the command and terminate the frame */
2416 brcmf_dbg(INFO, "sdio error %d, abort command and terminate frame\n",
2418 bus->sdcnt.tx_sderrs++;
2420 brcmf_sdcard_abort(bus->sdiodev, SDIO_FUNC_2);
2422 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL,
2424 bus->sdcnt.f1regdata++;
2426 for (i = 0; i < 3; i++) {
2428 hi = brcmf_sdio_regrb(bus->sdiodev,
2429 SBSDIO_FUNC1_WFRAMEBCHI, NULL);
2430 lo = brcmf_sdio_regrb(bus->sdiodev,
2431 SBSDIO_FUNC1_WFRAMEBCLO, NULL);
2432 bus->sdcnt.f1regdata += 2;
2433 if (hi == 0 && lo == 0)
2439 bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQUENCE_WRAP;
2445 brcmf_sdbrcm_bus_txctl(struct device *dev, unsigned char *msg, uint msglen)
2453 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
2454 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
2455 struct brcmf_sdio *bus = sdiodev->bus;
2457 brcmf_dbg(TRACE, "Enter\n");
2459 /* Back the pointer to make a room for bus header */
2460 frame = msg - SDPCM_HDRLEN;
2461 len = (msglen += SDPCM_HDRLEN);
2463 /* Add alignment padding (optional for ctl frames) */
2464 doff = ((unsigned long)frame % BRCMF_SDALIGN);
2469 memset(frame, 0, doff + SDPCM_HDRLEN);
2471 /* precondition: doff < BRCMF_SDALIGN */
2472 doff += SDPCM_HDRLEN;
2474 /* Round send length to next SDIO block */
2475 if (bus->roundup && bus->blocksize && (len > bus->blocksize)) {
2476 u16 pad = bus->blocksize - (len % bus->blocksize);
2477 if ((pad <= bus->roundup) && (pad < bus->blocksize))
2479 } else if (len % BRCMF_SDALIGN) {
2480 len += BRCMF_SDALIGN - (len % BRCMF_SDALIGN);
2483 /* Satisfy length-alignment requirements */
2484 if (len & (ALIGNMENT - 1))
2485 len = roundup(len, ALIGNMENT);
2487 /* precondition: IS_ALIGNED((unsigned long)frame, 2) */
2489 /* Make sure backplane clock is on */
2490 sdio_claim_host(bus->sdiodev->func[1]);
2491 brcmf_sdbrcm_bus_sleep(bus, false, false);
2492 sdio_release_host(bus->sdiodev->func[1]);
2494 /* Hardware tag: 2 byte len followed by 2 byte ~len check (all LE) */
2495 *(__le16 *) frame = cpu_to_le16((u16) msglen);
2496 *(((__le16 *) frame) + 1) = cpu_to_le16(~msglen);
2498 /* Software tag: channel, sequence number, data offset */
2500 ((SDPCM_CONTROL_CHANNEL << SDPCM_CHANNEL_SHIFT) &
2502 | bus->tx_seq | ((doff << SDPCM_DOFFSET_SHIFT) &
2503 SDPCM_DOFFSET_MASK);
2504 put_unaligned_le32(swheader, frame + SDPCM_FRAMETAG_LEN);
2505 put_unaligned_le32(0, frame + SDPCM_FRAMETAG_LEN + sizeof(swheader));
2507 if (!data_ok(bus)) {
2508 brcmf_dbg(INFO, "No bus credit bus->tx_max %d, bus->tx_seq %d\n",
2509 bus->tx_max, bus->tx_seq);
2510 bus->ctrl_frame_stat = true;
2512 bus->ctrl_frame_buf = frame;
2513 bus->ctrl_frame_len = len;
2515 wait_event_interruptible_timeout(bus->ctrl_wait,
2516 !bus->ctrl_frame_stat,
2517 msecs_to_jiffies(2000));
2519 if (!bus->ctrl_frame_stat) {
2520 brcmf_dbg(SDIO, "ctrl_frame_stat == false\n");
2523 brcmf_dbg(SDIO, "ctrl_frame_stat == true\n");
2529 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_CTL_ON(),
2530 frame, len, "Tx Frame:\n");
2531 brcmf_dbg_hex_dump(!(BRCMF_BYTES_ON() && BRCMF_CTL_ON()) &&
2533 frame, min_t(u16, len, 16), "TxHdr:\n");
2536 sdio_claim_host(bus->sdiodev->func[1]);
2537 ret = brcmf_tx_frame(bus, frame, len);
2538 sdio_release_host(bus->sdiodev->func[1]);
2539 } while (ret < 0 && retries++ < TXRETRIES);
2542 if ((bus->idletime == BRCMF_IDLE_IMMEDIATE) &&
2543 atomic_read(&bus->dpc_tskcnt) == 0) {
2544 bus->activity = false;
2545 sdio_claim_host(bus->sdiodev->func[1]);
2546 brcmf_dbg(INFO, "idle\n");
2547 brcmf_sdbrcm_clkctl(bus, CLK_NONE, true);
2548 sdio_release_host(bus->sdiodev->func[1]);
2552 bus->sdcnt.tx_ctlerrs++;
2554 bus->sdcnt.tx_ctlpkts++;
2556 return ret ? -EIO : 0;
2560 static inline bool brcmf_sdio_valid_shared_address(u32 addr)
2562 return !(addr == 0 || ((~addr >> 16) & 0xffff) == (addr & 0xffff));
2565 static int brcmf_sdio_readshared(struct brcmf_sdio *bus,
2566 struct sdpcm_shared *sh)
2571 struct sdpcm_shared_le sh_le;
2574 shaddr = bus->ci->rambase + bus->ramsize - 4;
2577 * Read last word in socram to determine
2578 * address of sdpcm_shared structure
2580 sdio_claim_host(bus->sdiodev->func[1]);
2581 brcmf_sdbrcm_bus_sleep(bus, false, false);
2582 rv = brcmf_sdio_ramrw(bus->sdiodev, false, shaddr, (u8 *)&addr_le, 4);
2583 sdio_release_host(bus->sdiodev->func[1]);
2587 addr = le32_to_cpu(addr_le);
2589 brcmf_dbg(SDIO, "sdpcm_shared address 0x%08X\n", addr);
2592 * Check if addr is valid.
2593 * NVRAM length at the end of memory should have been overwritten.
2595 if (!brcmf_sdio_valid_shared_address(addr)) {
2596 brcmf_err("invalid sdpcm_shared address 0x%08X\n",
2601 /* Read hndrte_shared structure */
2602 rv = brcmf_sdio_ramrw(bus->sdiodev, false, addr, (u8 *)&sh_le,
2603 sizeof(struct sdpcm_shared_le));
2608 sh->flags = le32_to_cpu(sh_le.flags);
2609 sh->trap_addr = le32_to_cpu(sh_le.trap_addr);
2610 sh->assert_exp_addr = le32_to_cpu(sh_le.assert_exp_addr);
2611 sh->assert_file_addr = le32_to_cpu(sh_le.assert_file_addr);
2612 sh->assert_line = le32_to_cpu(sh_le.assert_line);
2613 sh->console_addr = le32_to_cpu(sh_le.console_addr);
2614 sh->msgtrace_addr = le32_to_cpu(sh_le.msgtrace_addr);
2616 if ((sh->flags & SDPCM_SHARED_VERSION_MASK) > SDPCM_SHARED_VERSION) {
2617 brcmf_err("sdpcm shared version unsupported: dhd %d dongle %d\n",
2618 SDPCM_SHARED_VERSION,
2619 sh->flags & SDPCM_SHARED_VERSION_MASK);
2626 static int brcmf_sdio_dump_console(struct brcmf_sdio *bus,
2627 struct sdpcm_shared *sh, char __user *data,
2630 u32 addr, console_ptr, console_size, console_index;
2631 char *conbuf = NULL;
2637 /* obtain console information from device memory */
2638 addr = sh->console_addr + offsetof(struct rte_console, log_le);
2639 rv = brcmf_sdio_ramrw(bus->sdiodev, false, addr,
2640 (u8 *)&sh_val, sizeof(u32));
2643 console_ptr = le32_to_cpu(sh_val);
2645 addr = sh->console_addr + offsetof(struct rte_console, log_le.buf_size);
2646 rv = brcmf_sdio_ramrw(bus->sdiodev, false, addr,
2647 (u8 *)&sh_val, sizeof(u32));
2650 console_size = le32_to_cpu(sh_val);
2652 addr = sh->console_addr + offsetof(struct rte_console, log_le.idx);
2653 rv = brcmf_sdio_ramrw(bus->sdiodev, false, addr,
2654 (u8 *)&sh_val, sizeof(u32));
2657 console_index = le32_to_cpu(sh_val);
2659 /* allocate buffer for console data */
2660 if (console_size <= CONSOLE_BUFFER_MAX)
2661 conbuf = vzalloc(console_size+1);
2666 /* obtain the console data from device */
2667 conbuf[console_size] = '\0';
2668 rv = brcmf_sdio_ramrw(bus->sdiodev, false, console_ptr, (u8 *)conbuf,
2673 rv = simple_read_from_buffer(data, count, &pos,
2674 conbuf + console_index,
2675 console_size - console_index);
2680 if (console_index > 0) {
2682 rv = simple_read_from_buffer(data+nbytes, count, &pos,
2683 conbuf, console_index - 1);
2693 static int brcmf_sdio_trap_info(struct brcmf_sdio *bus, struct sdpcm_shared *sh,
2694 char __user *data, size_t count)
2698 struct brcmf_trap_info tr;
2701 if ((sh->flags & SDPCM_SHARED_TRAP) == 0) {
2702 brcmf_dbg(INFO, "no trap in firmware\n");
2706 error = brcmf_sdio_ramrw(bus->sdiodev, false, sh->trap_addr, (u8 *)&tr,
2707 sizeof(struct brcmf_trap_info));
2711 res = scnprintf(buf, sizeof(buf),
2712 "dongle trap info: type 0x%x @ epc 0x%08x\n"
2713 " cpsr 0x%08x spsr 0x%08x sp 0x%08x\n"
2714 " lr 0x%08x pc 0x%08x offset 0x%x\n"
2715 " r0 0x%08x r1 0x%08x r2 0x%08x r3 0x%08x\n"
2716 " r4 0x%08x r5 0x%08x r6 0x%08x r7 0x%08x\n",
2717 le32_to_cpu(tr.type), le32_to_cpu(tr.epc),
2718 le32_to_cpu(tr.cpsr), le32_to_cpu(tr.spsr),
2719 le32_to_cpu(tr.r13), le32_to_cpu(tr.r14),
2720 le32_to_cpu(tr.pc), sh->trap_addr,
2721 le32_to_cpu(tr.r0), le32_to_cpu(tr.r1),
2722 le32_to_cpu(tr.r2), le32_to_cpu(tr.r3),
2723 le32_to_cpu(tr.r4), le32_to_cpu(tr.r5),
2724 le32_to_cpu(tr.r6), le32_to_cpu(tr.r7));
2726 return simple_read_from_buffer(data, count, &pos, buf, res);
2729 static int brcmf_sdio_assert_info(struct brcmf_sdio *bus,
2730 struct sdpcm_shared *sh, char __user *data,
2735 char file[80] = "?";
2736 char expr[80] = "<???>";
2740 if ((sh->flags & SDPCM_SHARED_ASSERT_BUILT) == 0) {
2741 brcmf_dbg(INFO, "firmware not built with -assert\n");
2743 } else if ((sh->flags & SDPCM_SHARED_ASSERT) == 0) {
2744 brcmf_dbg(INFO, "no assert in dongle\n");
2748 sdio_claim_host(bus->sdiodev->func[1]);
2749 if (sh->assert_file_addr != 0) {
2750 error = brcmf_sdio_ramrw(bus->sdiodev, false,
2751 sh->assert_file_addr, (u8 *)file, 80);
2755 if (sh->assert_exp_addr != 0) {
2756 error = brcmf_sdio_ramrw(bus->sdiodev, false,
2757 sh->assert_exp_addr, (u8 *)expr, 80);
2761 sdio_release_host(bus->sdiodev->func[1]);
2763 res = scnprintf(buf, sizeof(buf),
2764 "dongle assert: %s:%d: assert(%s)\n",
2765 file, sh->assert_line, expr);
2766 return simple_read_from_buffer(data, count, &pos, buf, res);
2769 static int brcmf_sdbrcm_checkdied(struct brcmf_sdio *bus)
2772 struct sdpcm_shared sh;
2774 error = brcmf_sdio_readshared(bus, &sh);
2779 if ((sh.flags & SDPCM_SHARED_ASSERT_BUILT) == 0)
2780 brcmf_dbg(INFO, "firmware not built with -assert\n");
2781 else if (sh.flags & SDPCM_SHARED_ASSERT)
2782 brcmf_err("assertion in dongle\n");
2784 if (sh.flags & SDPCM_SHARED_TRAP)
2785 brcmf_err("firmware trap in dongle\n");
2790 static int brcmf_sdbrcm_died_dump(struct brcmf_sdio *bus, char __user *data,
2791 size_t count, loff_t *ppos)
2794 struct sdpcm_shared sh;
2801 error = brcmf_sdio_readshared(bus, &sh);
2805 error = brcmf_sdio_assert_info(bus, &sh, data, count);
2810 error = brcmf_sdio_trap_info(bus, &sh, data+nbytes, count);
2815 error = brcmf_sdio_dump_console(bus, &sh, data+nbytes, count);
2826 static ssize_t brcmf_sdio_forensic_read(struct file *f, char __user *data,
2827 size_t count, loff_t *ppos)
2829 struct brcmf_sdio *bus = f->private_data;
2832 res = brcmf_sdbrcm_died_dump(bus, data, count, ppos);
2835 return (ssize_t)res;
2838 static const struct file_operations brcmf_sdio_forensic_ops = {
2839 .owner = THIS_MODULE,
2840 .open = simple_open,
2841 .read = brcmf_sdio_forensic_read
2844 static void brcmf_sdio_debugfs_create(struct brcmf_sdio *bus)
2846 struct brcmf_pub *drvr = bus->sdiodev->bus_if->drvr;
2847 struct dentry *dentry = brcmf_debugfs_get_devdir(drvr);
2849 if (IS_ERR_OR_NULL(dentry))
2852 debugfs_create_file("forensics", S_IRUGO, dentry, bus,
2853 &brcmf_sdio_forensic_ops);
2854 brcmf_debugfs_create_sdio_count(drvr, &bus->sdcnt);
2857 static int brcmf_sdbrcm_checkdied(struct brcmf_sdio *bus)
2862 static void brcmf_sdio_debugfs_create(struct brcmf_sdio *bus)
2868 brcmf_sdbrcm_bus_rxctl(struct device *dev, unsigned char *msg, uint msglen)
2874 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
2875 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
2876 struct brcmf_sdio *bus = sdiodev->bus;
2878 brcmf_dbg(TRACE, "Enter\n");
2880 /* Wait until control frame is available */
2881 timeleft = brcmf_sdbrcm_dcmd_resp_wait(bus, &bus->rxlen, &pending);
2883 spin_lock_bh(&bus->rxctl_lock);
2885 memcpy(msg, bus->rxctl, min(msglen, rxlen));
2887 buf = bus->rxctl_orig;
2888 bus->rxctl_orig = NULL;
2890 spin_unlock_bh(&bus->rxctl_lock);
2894 brcmf_dbg(CTL, "resumed on rxctl frame, got %d expected %d\n",
2896 } else if (timeleft == 0) {
2897 brcmf_err("resumed on timeout\n");
2898 brcmf_sdbrcm_checkdied(bus);
2899 } else if (pending) {
2900 brcmf_dbg(CTL, "cancelled\n");
2901 return -ERESTARTSYS;
2903 brcmf_dbg(CTL, "resumed for unknown reason?\n");
2904 brcmf_sdbrcm_checkdied(bus);
2908 bus->sdcnt.rx_ctlpkts++;
2910 bus->sdcnt.rx_ctlerrs++;
2912 return rxlen ? (int)rxlen : -ETIMEDOUT;
2915 static bool brcmf_sdbrcm_download_state(struct brcmf_sdio *bus, bool enter)
2917 struct chip_info *ci = bus->ci;
2919 /* To enter download state, disable ARM and reset SOCRAM.
2920 * To exit download state, simply reset ARM (default is RAM boot).
2923 bus->alp_only = true;
2925 brcmf_sdio_chip_enter_download(bus->sdiodev, ci);
2927 if (!brcmf_sdio_chip_exit_download(bus->sdiodev, ci, bus->vars,
2931 /* Allow HT Clock now that the ARM is running. */
2932 bus->alp_only = false;
2934 bus->sdiodev->bus_if->state = BRCMF_BUS_LOAD;
2940 static int brcmf_sdbrcm_get_image(char *buf, int len, struct brcmf_sdio *bus)
2942 if (bus->firmware->size < bus->fw_ptr + len)
2943 len = bus->firmware->size - bus->fw_ptr;
2945 memcpy(buf, &bus->firmware->data[bus->fw_ptr], len);
2950 static int brcmf_sdbrcm_download_code_file(struct brcmf_sdio *bus)
2954 u8 *memblock = NULL, *memptr;
2958 brcmf_dbg(INFO, "Enter\n");
2960 ret = request_firmware(&bus->firmware, BRCMF_SDIO_FW_NAME,
2961 &bus->sdiodev->func[2]->dev);
2963 brcmf_err("Fail to request firmware %d\n", ret);
2968 memptr = memblock = kmalloc(MEMBLOCK + BRCMF_SDALIGN, GFP_ATOMIC);
2969 if (memblock == NULL) {
2973 if ((u32)(unsigned long)memblock % BRCMF_SDALIGN)
2974 memptr += (BRCMF_SDALIGN -
2975 ((u32)(unsigned long)memblock % BRCMF_SDALIGN));
2977 offset = bus->ci->rambase;
2979 /* Download image */
2980 len = brcmf_sdbrcm_get_image((char *)memptr, MEMBLOCK, bus);
2981 idx = brcmf_sdio_chip_getinfidx(bus->ci, BCMA_CORE_ARM_CR4);
2982 if (BRCMF_MAX_CORENUM != idx)
2983 memcpy(&bus->ci->rst_vec, memptr, sizeof(bus->ci->rst_vec));
2985 ret = brcmf_sdio_ramrw(bus->sdiodev, true, offset, memptr, len);
2987 brcmf_err("error %d on writing %d membytes at 0x%08x\n",
2988 ret, MEMBLOCK, offset);
2993 len = brcmf_sdbrcm_get_image((char *)memptr, MEMBLOCK, bus);
2999 release_firmware(bus->firmware);
3006 * ProcessVars:Takes a buffer of "<var>=<value>\n" lines read from a file
3007 * and ending in a NUL.
3008 * Removes carriage returns, empty lines, comment lines, and converts
3010 * Shortens buffer as needed and pads with NULs. End of buffer is marked
3014 static int brcmf_process_nvram_vars(struct brcmf_sdio *bus)
3021 uint buf_len, n, len;
3023 len = bus->firmware->size;
3024 varbuf = vmalloc(len);
3028 memcpy(varbuf, bus->firmware->data, len);
3031 findNewline = false;
3034 for (n = 0; n < len; n++) {
3037 if (varbuf[n] == '\r')
3039 if (findNewline && varbuf[n] != '\n')
3041 findNewline = false;
3042 if (varbuf[n] == '#') {
3046 if (varbuf[n] == '\n') {
3056 buf_len = dp - varbuf;
3057 while (dp < varbuf + n)
3061 /* roundup needed for download to device */
3062 bus->varsz = roundup(buf_len + 1, 4);
3063 bus->vars = kmalloc(bus->varsz, GFP_KERNEL);
3064 if (bus->vars == NULL) {
3070 /* copy the processed variables and add null termination */
3071 memcpy(bus->vars, varbuf, buf_len);
3072 bus->vars[buf_len] = 0;
3078 static int brcmf_sdbrcm_download_nvram(struct brcmf_sdio *bus)
3082 ret = request_firmware(&bus->firmware, BRCMF_SDIO_NV_NAME,
3083 &bus->sdiodev->func[2]->dev);
3085 brcmf_err("Fail to request nvram %d\n", ret);
3089 ret = brcmf_process_nvram_vars(bus);
3091 release_firmware(bus->firmware);
3096 static int _brcmf_sdbrcm_download_firmware(struct brcmf_sdio *bus)
3100 /* Keep arm in reset */
3101 if (!brcmf_sdbrcm_download_state(bus, true)) {
3102 brcmf_err("error placing ARM core in reset\n");
3106 if (brcmf_sdbrcm_download_code_file(bus)) {
3107 brcmf_err("dongle image file download failed\n");
3111 if (brcmf_sdbrcm_download_nvram(bus)) {
3112 brcmf_err("dongle nvram file download failed\n");
3116 /* Take arm out of reset */
3117 if (!brcmf_sdbrcm_download_state(bus, false)) {
3118 brcmf_err("error getting out of ARM core reset\n");
3128 static bool brcmf_sdbrcm_sr_capable(struct brcmf_sdio *bus)
3132 brcmf_dbg(TRACE, "Enter\n");
3134 /* old chips with PMU version less than 17 don't support save restore */
3135 if (bus->ci->pmurev < 17)
3138 /* read PMU chipcontrol register 3*/
3139 addr = CORE_CC_REG(bus->ci->c_inf[0].base, chipcontrol_addr);
3140 brcmf_sdio_regwl(bus->sdiodev, addr, 3, NULL);
3141 addr = CORE_CC_REG(bus->ci->c_inf[0].base, chipcontrol_data);
3142 reg = brcmf_sdio_regrl(bus->sdiodev, addr, NULL);
3147 static void brcmf_sdbrcm_sr_init(struct brcmf_sdio *bus)
3152 brcmf_dbg(TRACE, "Enter\n");
3154 val = brcmf_sdio_regrb(bus->sdiodev, SBSDIO_FUNC1_WAKEUPCTRL,
3157 brcmf_err("error reading SBSDIO_FUNC1_WAKEUPCTRL\n");
3161 val |= 1 << SBSDIO_FUNC1_WCTRL_HTWAIT_SHIFT;
3162 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_WAKEUPCTRL,
3165 brcmf_err("error writing SBSDIO_FUNC1_WAKEUPCTRL\n");
3169 /* Add CMD14 Support */
3170 brcmf_sdio_regwb(bus->sdiodev, SDIO_CCCR_BRCM_CARDCAP,
3171 (SDIO_CCCR_BRCM_CARDCAP_CMD14_SUPPORT |
3172 SDIO_CCCR_BRCM_CARDCAP_CMD14_EXT),
3175 brcmf_err("error writing SDIO_CCCR_BRCM_CARDCAP\n");
3179 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3180 SBSDIO_FORCE_HT, &err);
3182 brcmf_err("error writing SBSDIO_FUNC1_CHIPCLKCSR\n");
3187 bus->sr_enabled = true;
3188 brcmf_dbg(INFO, "SR enabled\n");
3191 /* enable KSO bit */
3192 static int brcmf_sdbrcm_kso_init(struct brcmf_sdio *bus)
3197 brcmf_dbg(TRACE, "Enter\n");
3199 /* KSO bit added in SDIO core rev 12 */
3200 if (bus->ci->c_inf[1].rev < 12)
3203 val = brcmf_sdio_regrb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
3206 brcmf_err("error reading SBSDIO_FUNC1_SLEEPCSR\n");
3210 if (!(val & SBSDIO_FUNC1_SLEEPCSR_KSO_MASK)) {
3211 val |= (SBSDIO_FUNC1_SLEEPCSR_KSO_EN <<
3212 SBSDIO_FUNC1_SLEEPCSR_KSO_SHIFT);
3213 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
3216 brcmf_err("error writing SBSDIO_FUNC1_SLEEPCSR\n");
3226 brcmf_sdbrcm_download_firmware(struct brcmf_sdio *bus)
3230 sdio_claim_host(bus->sdiodev->func[1]);
3232 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
3234 ret = _brcmf_sdbrcm_download_firmware(bus) == 0;
3236 brcmf_sdbrcm_clkctl(bus, CLK_SDONLY, false);
3238 sdio_release_host(bus->sdiodev->func[1]);
3243 static int brcmf_sdbrcm_bus_init(struct device *dev)
3245 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3246 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3247 struct brcmf_sdio *bus = sdiodev->bus;
3248 unsigned long timeout;
3253 brcmf_dbg(TRACE, "Enter\n");
3255 /* try to download image and nvram to the dongle */
3256 if (bus_if->state == BRCMF_BUS_DOWN) {
3257 if (!(brcmf_sdbrcm_download_firmware(bus)))
3261 if (!bus->sdiodev->bus_if->drvr)
3264 /* Start the watchdog timer */
3265 bus->sdcnt.tickcnt = 0;
3266 brcmf_sdbrcm_wd_timer(bus, BRCMF_WD_POLL_MS);
3268 sdio_claim_host(bus->sdiodev->func[1]);
3270 /* Make sure backplane clock is on, needed to generate F2 interrupt */
3271 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
3272 if (bus->clkstate != CLK_AVAIL)
3275 /* Force clocks on backplane to be sure F2 interrupt propagates */
3276 saveclk = brcmf_sdio_regrb(bus->sdiodev,
3277 SBSDIO_FUNC1_CHIPCLKCSR, &err);
3279 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3280 (saveclk | SBSDIO_FORCE_HT), &err);
3283 brcmf_err("Failed to force clock for F2: err %d\n", err);
3287 /* Enable function 2 (frame transfers) */
3288 w_sdreg32(bus, SDPCM_PROT_VERSION << SMB_DATA_VERSION_SHIFT,
3289 offsetof(struct sdpcmd_regs, tosbmailboxdata));
3290 enable = (SDIO_FUNC_ENABLE_1 | SDIO_FUNC_ENABLE_2);
3292 brcmf_sdio_regwb(bus->sdiodev, SDIO_CCCR_IOEx, enable, NULL);
3294 timeout = jiffies + msecs_to_jiffies(BRCMF_WAIT_F2RDY);
3296 while (enable != ready) {
3297 ready = brcmf_sdio_regrb(bus->sdiodev,
3298 SDIO_CCCR_IORx, NULL);
3299 if (time_after(jiffies, timeout))
3301 else if (time_after(jiffies, timeout - BRCMF_WAIT_F2RDY + 50))
3302 /* prevent busy waiting if it takes too long */
3303 msleep_interruptible(20);
3306 brcmf_dbg(INFO, "enable 0x%02x, ready 0x%02x\n", enable, ready);
3308 /* If F2 successfully enabled, set core and enable interrupts */
3309 if (ready == enable) {
3310 /* Set up the interrupt mask and enable interrupts */
3311 bus->hostintmask = HOSTINTMASK;
3312 w_sdreg32(bus, bus->hostintmask,
3313 offsetof(struct sdpcmd_regs, hostintmask));
3315 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_WATERMARK, 8, &err);
3317 /* Disable F2 again */
3318 enable = SDIO_FUNC_ENABLE_1;
3319 brcmf_sdio_regwb(bus->sdiodev, SDIO_CCCR_IOEx, enable, NULL);
3323 if (brcmf_sdbrcm_sr_capable(bus)) {
3324 brcmf_sdbrcm_sr_init(bus);
3326 /* Restore previous clock setting */
3327 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3332 ret = brcmf_sdio_intr_register(bus->sdiodev);
3334 brcmf_err("intr register failed:%d\n", ret);
3337 /* If we didn't come up, turn off backplane clock */
3338 if (bus_if->state != BRCMF_BUS_DATA)
3339 brcmf_sdbrcm_clkctl(bus, CLK_NONE, false);
3342 sdio_release_host(bus->sdiodev->func[1]);
3347 void brcmf_sdbrcm_isr(void *arg)
3349 struct brcmf_sdio *bus = (struct brcmf_sdio *) arg;
3351 brcmf_dbg(TRACE, "Enter\n");
3354 brcmf_err("bus is null pointer, exiting\n");
3358 if (bus->sdiodev->bus_if->state == BRCMF_BUS_DOWN) {
3359 brcmf_err("bus is down. we have nothing to do\n");
3362 /* Count the interrupt call */
3363 bus->sdcnt.intrcount++;
3365 atomic_set(&bus->ipend, 1);
3367 if (brcmf_sdio_intr_rstatus(bus)) {
3368 brcmf_err("failed backplane access\n");
3369 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
3372 /* Disable additional interrupts (is this needed now)? */
3374 brcmf_err("isr w/o interrupt configured!\n");
3376 atomic_inc(&bus->dpc_tskcnt);
3377 queue_work(bus->brcmf_wq, &bus->datawork);
3380 static bool brcmf_sdbrcm_bus_watchdog(struct brcmf_sdio *bus)
3383 struct brcmf_bus *bus_if = dev_get_drvdata(bus->sdiodev->dev);
3386 brcmf_dbg(TIMER, "Enter\n");
3388 /* Poll period: check device if appropriate. */
3389 if (!bus->sr_enabled &&
3390 bus->poll && (++bus->polltick >= bus->pollrate)) {
3393 /* Reset poll tick */
3396 /* Check device if no interrupts */
3398 (bus->sdcnt.intrcount == bus->sdcnt.lastintrs)) {
3400 if (atomic_read(&bus->dpc_tskcnt) == 0) {
3403 sdio_claim_host(bus->sdiodev->func[1]);
3404 devpend = brcmf_sdio_regrb(bus->sdiodev,
3407 sdio_release_host(bus->sdiodev->func[1]);
3409 devpend & (INTR_STATUS_FUNC1 |
3413 /* If there is something, make like the ISR and
3416 bus->sdcnt.pollcnt++;
3417 atomic_set(&bus->ipend, 1);
3419 atomic_inc(&bus->dpc_tskcnt);
3420 queue_work(bus->brcmf_wq, &bus->datawork);
3424 /* Update interrupt tracking */
3425 bus->sdcnt.lastintrs = bus->sdcnt.intrcount;
3428 /* Poll for console output periodically */
3429 if (bus_if && bus_if->state == BRCMF_BUS_DATA &&
3430 bus->console_interval != 0) {
3431 bus->console.count += BRCMF_WD_POLL_MS;
3432 if (bus->console.count >= bus->console_interval) {
3433 bus->console.count -= bus->console_interval;
3434 sdio_claim_host(bus->sdiodev->func[1]);
3435 /* Make sure backplane clock is on */
3436 brcmf_sdbrcm_bus_sleep(bus, false, false);
3437 if (brcmf_sdbrcm_readconsole(bus) < 0)
3439 bus->console_interval = 0;
3440 sdio_release_host(bus->sdiodev->func[1]);
3445 /* On idle timeout clear activity flag and/or turn off clock */
3446 if ((bus->idletime > 0) && (bus->clkstate == CLK_AVAIL)) {
3447 if (++bus->idlecount >= bus->idletime) {
3449 if (bus->activity) {
3450 bus->activity = false;
3451 brcmf_sdbrcm_wd_timer(bus, BRCMF_WD_POLL_MS);
3453 brcmf_dbg(SDIO, "idle\n");
3454 sdio_claim_host(bus->sdiodev->func[1]);
3455 brcmf_sdbrcm_bus_sleep(bus, true, false);
3456 sdio_release_host(bus->sdiodev->func[1]);
3461 return (atomic_read(&bus->ipend) > 0);
3464 static void brcmf_sdio_dataworker(struct work_struct *work)
3466 struct brcmf_sdio *bus = container_of(work, struct brcmf_sdio,
3469 while (atomic_read(&bus->dpc_tskcnt)) {
3470 brcmf_sdbrcm_dpc(bus);
3471 atomic_dec(&bus->dpc_tskcnt);
3475 static void brcmf_sdbrcm_release_malloc(struct brcmf_sdio *bus)
3477 brcmf_dbg(TRACE, "Enter\n");
3480 bus->rxctl = bus->rxbuf = NULL;
3484 static bool brcmf_sdbrcm_probe_malloc(struct brcmf_sdio *bus)
3486 brcmf_dbg(TRACE, "Enter\n");
3488 if (bus->sdiodev->bus_if->maxctl) {
3490 roundup((bus->sdiodev->bus_if->maxctl + SDPCM_HDRLEN),
3491 ALIGNMENT) + BRCMF_SDALIGN;
3492 bus->rxbuf = kmalloc(bus->rxblen, GFP_ATOMIC);
3501 brcmf_sdbrcm_probe_attach(struct brcmf_sdio *bus, u32 regsva)
3509 bus->alp_only = true;
3511 sdio_claim_host(bus->sdiodev->func[1]);
3513 pr_debug("F1 signature read @0x18000000=0x%4x\n",
3514 brcmf_sdio_regrl(bus->sdiodev, SI_ENUM_BASE, NULL));
3517 * Force PLL off until brcmf_sdio_chip_attach()
3518 * programs PLL control regs
3521 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3522 BRCMF_INIT_CLKCTL1, &err);
3524 clkctl = brcmf_sdio_regrb(bus->sdiodev,
3525 SBSDIO_FUNC1_CHIPCLKCSR, &err);
3527 if (err || ((clkctl & ~SBSDIO_AVBITS) != BRCMF_INIT_CLKCTL1)) {
3528 brcmf_err("ChipClkCSR access: err %d wrote 0x%02x read 0x%02x\n",
3529 err, BRCMF_INIT_CLKCTL1, clkctl);
3533 if (brcmf_sdio_chip_attach(bus->sdiodev, &bus->ci, regsva)) {
3534 brcmf_err("brcmf_sdio_chip_attach failed!\n");
3538 if (brcmf_sdbrcm_kso_init(bus)) {
3539 brcmf_err("error enabling KSO\n");
3543 if ((bus->sdiodev->pdata) && (bus->sdiodev->pdata->drive_strength))
3544 drivestrength = bus->sdiodev->pdata->drive_strength;
3546 drivestrength = DEFAULT_SDIO_DRIVE_STRENGTH;
3547 brcmf_sdio_chip_drivestrengthinit(bus->sdiodev, bus->ci, drivestrength);
3549 /* Get info on the SOCRAM cores... */
3550 bus->ramsize = bus->ci->ramsize;
3551 if (!(bus->ramsize)) {
3552 brcmf_err("failed to find SOCRAM memory!\n");
3556 /* Set card control so an SDIO card reset does a WLAN backplane reset */
3557 reg_val = brcmf_sdio_regrb(bus->sdiodev,
3558 SDIO_CCCR_BRCM_CARDCTRL, &err);
3562 reg_val |= SDIO_CCCR_BRCM_CARDCTRL_WLANRESET;
3564 brcmf_sdio_regwb(bus->sdiodev,
3565 SDIO_CCCR_BRCM_CARDCTRL, reg_val, &err);
3569 /* set PMUControl so a backplane reset does PMU state reload */
3570 reg_addr = CORE_CC_REG(bus->ci->c_inf[0].base,
3572 reg_val = brcmf_sdio_regrl(bus->sdiodev,
3578 reg_val |= (BCMA_CC_PMU_CTL_RES_RELOAD << BCMA_CC_PMU_CTL_RES_SHIFT);
3580 brcmf_sdio_regwl(bus->sdiodev,
3588 sdio_release_host(bus->sdiodev->func[1]);
3590 brcmu_pktq_init(&bus->txq, (PRIOMASK + 1), TXQLEN);
3592 /* Locate an appropriately-aligned portion of hdrbuf */
3593 bus->rxhdr = (u8 *) roundup((unsigned long)&bus->hdrbuf[0],
3596 /* Set the poll and/or interrupt flags */
3605 sdio_release_host(bus->sdiodev->func[1]);
3609 static bool brcmf_sdbrcm_probe_init(struct brcmf_sdio *bus)
3611 brcmf_dbg(TRACE, "Enter\n");
3613 sdio_claim_host(bus->sdiodev->func[1]);
3615 /* Disable F2 to clear any intermediate frame state on the dongle */
3616 brcmf_sdio_regwb(bus->sdiodev, SDIO_CCCR_IOEx,
3617 SDIO_FUNC_ENABLE_1, NULL);
3619 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
3620 bus->rxflow = false;
3622 /* Done with backplane-dependent accesses, can drop clock... */
3623 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, 0, NULL);
3625 sdio_release_host(bus->sdiodev->func[1]);
3627 /* ...and initialize clock/power states */
3628 bus->clkstate = CLK_SDONLY;
3629 bus->idletime = BRCMF_IDLE_INTERVAL;
3630 bus->idleclock = BRCMF_IDLE_ACTIVE;
3632 /* Query the F2 block size, set roundup accordingly */
3633 bus->blocksize = bus->sdiodev->func[2]->cur_blksize;
3634 bus->roundup = min(max_roundup, bus->blocksize);
3637 bus->sleeping = false;
3638 bus->sr_enabled = false;
3644 brcmf_sdbrcm_watchdog_thread(void *data)
3646 struct brcmf_sdio *bus = (struct brcmf_sdio *)data;
3648 allow_signal(SIGTERM);
3649 /* Run until signal received */
3651 if (kthread_should_stop())
3653 if (!wait_for_completion_interruptible(&bus->watchdog_wait)) {
3654 brcmf_sdbrcm_bus_watchdog(bus);
3655 /* Count the tick for reference */
3656 bus->sdcnt.tickcnt++;
3664 brcmf_sdbrcm_watchdog(unsigned long data)
3666 struct brcmf_sdio *bus = (struct brcmf_sdio *)data;
3668 if (bus->watchdog_tsk) {
3669 complete(&bus->watchdog_wait);
3670 /* Reschedule the watchdog */
3671 if (bus->wd_timer_valid)
3672 mod_timer(&bus->timer,
3673 jiffies + BRCMF_WD_POLL_MS * HZ / 1000);
3677 static void brcmf_sdbrcm_release_dongle(struct brcmf_sdio *bus)
3679 brcmf_dbg(TRACE, "Enter\n");
3682 sdio_claim_host(bus->sdiodev->func[1]);
3683 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
3684 brcmf_sdbrcm_clkctl(bus, CLK_NONE, false);
3685 sdio_release_host(bus->sdiodev->func[1]);
3686 brcmf_sdio_chip_detach(&bus->ci);
3687 if (bus->vars && bus->varsz)
3692 brcmf_dbg(TRACE, "Disconnected\n");
3695 /* Detach and free everything */
3696 static void brcmf_sdbrcm_release(struct brcmf_sdio *bus)
3698 brcmf_dbg(TRACE, "Enter\n");
3701 /* De-register interrupt handler */
3702 brcmf_sdio_intr_unregister(bus->sdiodev);
3704 cancel_work_sync(&bus->datawork);
3706 destroy_workqueue(bus->brcmf_wq);
3708 if (bus->sdiodev->bus_if->drvr) {
3709 brcmf_detach(bus->sdiodev->dev);
3710 brcmf_sdbrcm_release_dongle(bus);
3713 brcmf_sdbrcm_release_malloc(bus);
3718 brcmf_dbg(TRACE, "Disconnected\n");
3721 static struct brcmf_bus_ops brcmf_sdio_bus_ops = {
3722 .stop = brcmf_sdbrcm_bus_stop,
3723 .init = brcmf_sdbrcm_bus_init,
3724 .txdata = brcmf_sdbrcm_bus_txdata,
3725 .txctl = brcmf_sdbrcm_bus_txctl,
3726 .rxctl = brcmf_sdbrcm_bus_rxctl,
3727 .gettxq = brcmf_sdbrcm_bus_gettxq,
3730 void *brcmf_sdbrcm_probe(u32 regsva, struct brcmf_sdio_dev *sdiodev)
3733 struct brcmf_sdio *bus;
3734 struct brcmf_bus_dcmd *dlst;
3736 u32 dngl_txglomalign;
3739 brcmf_dbg(TRACE, "Enter\n");
3741 /* We make an assumption about address window mappings:
3742 * regsva == SI_ENUM_BASE*/
3744 /* Allocate private bus interface state */
3745 bus = kzalloc(sizeof(struct brcmf_sdio), GFP_ATOMIC);
3749 bus->sdiodev = sdiodev;
3751 skb_queue_head_init(&bus->glom);
3752 bus->txbound = BRCMF_TXBOUND;
3753 bus->rxbound = BRCMF_RXBOUND;
3754 bus->txminmax = BRCMF_TXMINMAX;
3755 bus->tx_seq = SDPCM_SEQUENCE_WRAP - 1;
3757 INIT_WORK(&bus->datawork, brcmf_sdio_dataworker);
3758 bus->brcmf_wq = create_singlethread_workqueue("brcmf_wq");
3759 if (bus->brcmf_wq == NULL) {
3760 brcmf_err("insufficient memory to create txworkqueue\n");
3764 /* attempt to attach to the dongle */
3765 if (!(brcmf_sdbrcm_probe_attach(bus, regsva))) {
3766 brcmf_err("brcmf_sdbrcm_probe_attach failed\n");
3770 spin_lock_init(&bus->rxctl_lock);
3771 spin_lock_init(&bus->txqlock);
3772 init_waitqueue_head(&bus->ctrl_wait);
3773 init_waitqueue_head(&bus->dcmd_resp_wait);
3775 /* Set up the watchdog timer */
3776 init_timer(&bus->timer);
3777 bus->timer.data = (unsigned long)bus;
3778 bus->timer.function = brcmf_sdbrcm_watchdog;
3780 /* Initialize watchdog thread */
3781 init_completion(&bus->watchdog_wait);
3782 bus->watchdog_tsk = kthread_run(brcmf_sdbrcm_watchdog_thread,
3783 bus, "brcmf_watchdog");
3784 if (IS_ERR(bus->watchdog_tsk)) {
3785 pr_warn("brcmf_watchdog thread failed to start\n");
3786 bus->watchdog_tsk = NULL;
3788 /* Initialize DPC thread */
3789 atomic_set(&bus->dpc_tskcnt, 0);
3791 /* Assign bus interface call back */
3792 bus->sdiodev->bus_if->dev = bus->sdiodev->dev;
3793 bus->sdiodev->bus_if->ops = &brcmf_sdio_bus_ops;
3794 bus->sdiodev->bus_if->chip = bus->ci->chip;
3795 bus->sdiodev->bus_if->chiprev = bus->ci->chiprev;
3797 /* Attach to the brcmf/OS/network interface */
3798 ret = brcmf_attach(SDPCM_RESERVE, bus->sdiodev->dev);
3800 brcmf_err("brcmf_attach failed\n");
3804 /* Allocate buffers */
3805 if (!(brcmf_sdbrcm_probe_malloc(bus))) {
3806 brcmf_err("brcmf_sdbrcm_probe_malloc failed\n");
3810 if (!(brcmf_sdbrcm_probe_init(bus))) {
3811 brcmf_err("brcmf_sdbrcm_probe_init failed\n");
3815 brcmf_sdio_debugfs_create(bus);
3816 brcmf_dbg(INFO, "completed!!\n");
3818 /* sdio bus core specific dcmd */
3819 idx = brcmf_sdio_chip_getinfidx(bus->ci, BCMA_CORE_SDIO_DEV);
3820 dlst = kzalloc(sizeof(struct brcmf_bus_dcmd), GFP_KERNEL);
3822 if (bus->ci->c_inf[idx].rev < 12) {
3823 /* for sdio core rev < 12, disable txgloming */
3825 dlst->name = "bus:txglom";
3826 dlst->param = (char *)&dngl_txglom;
3827 dlst->param_len = sizeof(u32);
3829 /* otherwise, set txglomalign */
3830 dngl_txglomalign = bus->sdiodev->bus_if->align;
3831 dlst->name = "bus:txglomalign";
3832 dlst->param = (char *)&dngl_txglomalign;
3833 dlst->param_len = sizeof(u32);
3835 list_add(&dlst->list, &bus->sdiodev->bus_if->dcmd_list);
3838 /* if firmware path present try to download and bring up bus */
3839 ret = brcmf_bus_start(bus->sdiodev->dev);
3841 brcmf_err("dongle is not responding\n");
3848 brcmf_sdbrcm_release(bus);
3852 void brcmf_sdbrcm_disconnect(void *ptr)
3854 struct brcmf_sdio *bus = (struct brcmf_sdio *)ptr;
3856 brcmf_dbg(TRACE, "Enter\n");
3859 brcmf_sdbrcm_release(bus);
3861 brcmf_dbg(TRACE, "Disconnected\n");
3865 brcmf_sdbrcm_wd_timer(struct brcmf_sdio *bus, uint wdtick)
3867 /* Totally stop the timer */
3868 if (!wdtick && bus->wd_timer_valid) {
3869 del_timer_sync(&bus->timer);
3870 bus->wd_timer_valid = false;
3871 bus->save_ms = wdtick;
3875 /* don't start the wd until fw is loaded */
3876 if (bus->sdiodev->bus_if->state == BRCMF_BUS_DOWN)
3880 if (bus->save_ms != BRCMF_WD_POLL_MS) {
3881 if (bus->wd_timer_valid)
3882 /* Stop timer and restart at new value */
3883 del_timer_sync(&bus->timer);
3885 /* Create timer again when watchdog period is
3886 dynamically changed or in the first instance
3888 bus->timer.expires =
3889 jiffies + BRCMF_WD_POLL_MS * HZ / 1000;
3890 add_timer(&bus->timer);
3893 /* Re arm the timer, at last watchdog period */
3894 mod_timer(&bus->timer,
3895 jiffies + BRCMF_WD_POLL_MS * HZ / 1000);
3898 bus->wd_timer_valid = true;
3899 bus->save_ms = wdtick;