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/printk.h>
20 #include <linux/pci_ids.h>
21 #include <linux/netdevice.h>
22 #include <linux/sched.h>
23 #include <linux/mmc/sdio.h>
24 #include <asm/unaligned.h>
26 #include <brcmu_wifi.h>
27 #include <brcmu_utils.h>
28 #include <brcm_hw_ids.h>
30 #include "sdio_host.h"
32 /* register access macros */
36 brcmf_sdcard_reg_read(NULL, (unsigned long)(r), sizeof(*(r)))
40 __typeof(*(r)) __osl_v; \
41 __asm__ __volatile__("sync"); \
42 __osl_v = brcmf_sdcard_reg_read(NULL, (unsigned long)(r),\
44 __asm__ __volatile__("sync"); \
49 #define W_REG(r, v) do { \
50 brcmf_sdcard_reg_write(NULL, (unsigned long)(r), sizeof(*(r)), \
53 #else /* __BIG_ENDIAN */
55 brcmf_sdcard_reg_read(NULL, (unsigned long)(r), sizeof(*(r)))
56 #define W_REG(r, v) do { \
57 brcmf_sdcard_reg_write(NULL, (unsigned long)(r), sizeof(*(r)), \
60 #endif /* __BIG_ENDIAN */
62 #define AND_REG(r, v) W_REG((r), R_REG(r) & (v))
63 #define OR_REG(r, v) W_REG((r), R_REG(r) | (v))
65 #define SET_REG(r, mask, val) \
66 W_REG((r), ((R_REG(r) & ~(mask)) | (val)))
70 /* ARM trap handling */
72 /* Trap types defined by ARM (see arminc.h) */
74 /* Trap locations in lo memory */
76 #define FIRST_TRAP TR_RST
77 #define LAST_TRAP (TR_FIQ * TRAP_STRIDE)
79 #if defined(__ARM_ARCH_4T__)
80 #define MAX_TRAP_TYPE (TR_FIQ + 1)
81 #elif defined(__ARM_ARCH_7M__)
82 #define MAX_TRAP_TYPE (TR_ISR + ARMCM3_NUMINTS)
83 #endif /* __ARM_ARCH_7M__ */
85 /* The trap structure is defined here as offsets for assembly */
91 #define TR_REG(n) (TR_REGS + (n) * 4)
92 #define TR_SP TR_REG(13)
93 #define TR_LR TR_REG(14)
94 #define TR_PC TR_REG(15)
96 #define TRAP_T_SIZE 80
121 #define CBUF_LEN (128)
123 #define LOG_BUF_LEN 1024
126 u32 buf; /* Can't be pointer on (64-bit) hosts */
129 char *_buf_compat; /* Redundant pointer for backward compat. */
134 * When there is no UART (e.g. Quickturn),
135 * the host should write a complete
136 * input line directly into cbuf and then write
137 * the length into vcons_in.
138 * This may also be used when there is a real UART
139 * (at risk of conflicting with
140 * the real UART). vcons_out is currently unused.
142 volatile uint vcons_in;
143 volatile uint vcons_out;
145 /* Output (logging) buffer
146 * Console output is written to a ring buffer log_buf at index log_idx.
147 * The host may read the output when it sees log_idx advance.
148 * Output will be lost if the output wraps around faster than the host
153 /* Console input line buffer
154 * Characters are read one at a time into cbuf
155 * until <CR> is received, then
156 * the buffer is processed as a command line.
157 * Also used for virtual UART.
164 #include <chipcommon.h>
168 #include "dngl_stats.h"
171 #include "dhd_proto.h"
176 #ifndef DHDSDIO_MEM_DUMP_FNAME
177 #define DHDSDIO_MEM_DUMP_FNAME "mem_dump"
180 #define TXQLEN 2048 /* bulk tx queue length */
181 #define TXHI (TXQLEN - 256) /* turn on flow control above TXHI */
182 #define TXLOW (TXHI - 256) /* turn off flow control below TXLOW */
185 #define TXRETRIES 2 /* # of retries for tx frames */
187 #if defined(CONFIG_MACH_SANDGATE2G)
188 #define DHD_RXBOUND 250 /* Default for max rx frames in
191 #define DHD_RXBOUND 50 /* Default for max rx frames in
193 #endif /* defined(CONFIG_MACH_SANDGATE2G) */
195 #define DHD_TXBOUND 20 /* Default for max tx frames in
198 #define DHD_TXMINMAX 1 /* Max tx frames if rx still pending */
200 #define MEMBLOCK 2048 /* Block size used for downloading
202 #define MAX_DATA_BUF (32 * 1024) /* Must be large enough to hold
203 biggest possible glom */
205 #ifndef BRCMF_FIRSTREAD
206 #define BRCMF_FIRSTREAD 32
208 #if !ISPOWEROF2(BRCMF_FIRSTREAD)
209 #error BRCMF_FIRSTREAD is not a power of 2!
212 /* Total length of frame header for dongle protocol */
213 #define SDPCM_HDRLEN (SDPCM_FRAMETAG_LEN + SDPCM_SWHEADER_LEN)
215 #define SDPCM_RESERVE (SDPCM_HDRLEN + SDPCM_TEST_HDRLEN + BRCMF_SDALIGN)
217 #define SDPCM_RESERVE (SDPCM_HDRLEN + BRCMF_SDALIGN)
221 * Software allocation of To SB Mailbox resources
224 /* tosbmailbox bits corresponding to intstatus bits */
225 #define SMB_NAK (1 << 0) /* Frame NAK */
226 #define SMB_INT_ACK (1 << 1) /* Host Interrupt ACK */
227 #define SMB_USE_OOB (1 << 2) /* Use OOB Wakeup */
228 #define SMB_DEV_INT (1 << 3) /* Miscellaneous Interrupt */
230 /* tosbmailboxdata */
231 #define SMB_DATA_VERSION_SHIFT 16 /* host protocol version */
234 * Software allocation of To Host Mailbox resources
238 #define I_HMB_FC_STATE I_HMB_SW0 /* Flow Control State */
239 #define I_HMB_FC_CHANGE I_HMB_SW1 /* Flow Control State Changed */
240 #define I_HMB_FRAME_IND I_HMB_SW2 /* Frame Indication */
241 #define I_HMB_HOST_INT I_HMB_SW3 /* Miscellaneous Interrupt */
243 /* tohostmailboxdata */
244 #define HMB_DATA_NAKHANDLED 1 /* retransmit NAK'd frame */
245 #define HMB_DATA_DEVREADY 2 /* talk to host after enable */
246 #define HMB_DATA_FC 4 /* per prio flowcontrol update flag */
247 #define HMB_DATA_FWREADY 8 /* fw ready for protocol activity */
249 #define HMB_DATA_FCDATA_MASK 0xff000000
250 #define HMB_DATA_FCDATA_SHIFT 24
252 #define HMB_DATA_VERSION_MASK 0x00ff0000
253 #define HMB_DATA_VERSION_SHIFT 16
256 * Software-defined protocol header
259 /* Current protocol version */
260 #define SDPCM_PROT_VERSION 4
262 /* SW frame header */
263 #define SDPCM_PACKET_SEQUENCE(p) (((u8 *)p)[0] & 0xff)
265 #define SDPCM_CHANNEL_MASK 0x00000f00
266 #define SDPCM_CHANNEL_SHIFT 8
267 #define SDPCM_PACKET_CHANNEL(p) (((u8 *)p)[1] & 0x0f)
269 #define SDPCM_NEXTLEN_OFFSET 2
271 /* Data Offset from SOF (HW Tag, SW Tag, Pad) */
272 #define SDPCM_DOFFSET_OFFSET 3 /* Data Offset */
273 #define SDPCM_DOFFSET_VALUE(p) (((u8 *)p)[SDPCM_DOFFSET_OFFSET] & 0xff)
274 #define SDPCM_DOFFSET_MASK 0xff000000
275 #define SDPCM_DOFFSET_SHIFT 24
276 #define SDPCM_FCMASK_OFFSET 4 /* Flow control */
277 #define SDPCM_FCMASK_VALUE(p) (((u8 *)p)[SDPCM_FCMASK_OFFSET] & 0xff)
278 #define SDPCM_WINDOW_OFFSET 5 /* Credit based fc */
279 #define SDPCM_WINDOW_VALUE(p) (((u8 *)p)[SDPCM_WINDOW_OFFSET] & 0xff)
281 #define SDPCM_SWHEADER_LEN 8 /* SW header is 64 bits */
283 /* logical channel numbers */
284 #define SDPCM_CONTROL_CHANNEL 0 /* Control channel Id */
285 #define SDPCM_EVENT_CHANNEL 1 /* Asyc Event Indication Channel Id */
286 #define SDPCM_DATA_CHANNEL 2 /* Data Xmit/Recv Channel Id */
287 #define SDPCM_GLOM_CHANNEL 3 /* For coalesced packets */
288 #define SDPCM_TEST_CHANNEL 15 /* Reserved for test/debug packets */
290 #define SDPCM_SEQUENCE_WRAP 256 /* wrap-around val for 8bit frame seq */
292 #define SDPCM_GLOMDESC(p) (((u8 *)p)[1] & 0x80)
294 /* For TEST_CHANNEL packets, define another 4-byte header */
295 #define SDPCM_TEST_HDRLEN 4 /*
296 * Generally: Cmd(1), Ext(1), Len(2);
297 * Semantics of Ext byte depend on
298 * command. Len is current or requested
299 * frame length, not including test
300 * header; sent little-endian.
302 #define SDPCM_TEST_DISCARD 0x01 /* Receiver discards. Ext:pattern id. */
303 #define SDPCM_TEST_ECHOREQ 0x02 /* Echo request. Ext:pattern id. */
304 #define SDPCM_TEST_ECHORSP 0x03 /* Echo response. Ext:pattern id. */
305 #define SDPCM_TEST_BURST 0x04 /*
306 * Receiver to send a burst.
307 * Ext is a frame count
309 #define SDPCM_TEST_SEND 0x05 /*
310 * Receiver sets send mode.
311 * Ext is boolean on/off
314 /* Handy macro for filling in datagen packets with a pattern */
315 #define SDPCM_TEST_FILL(byteno, id) ((u8)(id + byteno))
318 * Shared structure between dongle and the host.
319 * The structure contains pointers to trap or assert information.
321 #define SDPCM_SHARED_VERSION 0x0002
322 #define SDPCM_SHARED_VERSION_MASK 0x00FF
323 #define SDPCM_SHARED_ASSERT_BUILT 0x0100
324 #define SDPCM_SHARED_ASSERT 0x0200
325 #define SDPCM_SHARED_TRAP 0x0400
328 /* Space for header read, limit for data packets */
330 #define MAX_HDR_READ 32
332 #if !ISPOWEROF2(MAX_HDR_READ)
333 #error MAX_HDR_READ is not a power of 2!
336 #define MAX_RX_DATASZ 2048
338 /* Maximum milliseconds to wait for F2 to come up */
339 #define DHD_WAIT_F2RDY 3000
341 /* Bump up limit on waiting for HT to account for first startup;
342 * if the image is doing a CRC calculation before programming the PMU
343 * for HT availability, it could take a couple hundred ms more, so
344 * max out at a 1 second (1000000us).
346 #if (PMU_MAX_TRANSITION_DLY <= 1000000)
347 #undef PMU_MAX_TRANSITION_DLY
348 #define PMU_MAX_TRANSITION_DLY 1000000
351 /* Value for ChipClockCSR during initial setup */
352 #define DHD_INIT_CLKCTL1 (SBSDIO_FORCE_HW_CLKREQ_OFF | \
353 SBSDIO_ALP_AVAIL_REQ)
354 #define DHD_INIT_CLKCTL2 (SBSDIO_FORCE_HW_CLKREQ_OFF | SBSDIO_FORCE_ALP)
356 /* Flags for SDH calls */
357 #define F2SYNC (SDIO_REQ_4BYTE | SDIO_REQ_FIXED)
360 #define SBIM_IBE 0x20000 /* inbanderror */
361 #define SBIM_TO 0x40000 /* timeout */
362 #define SBIM_BY 0x01800000 /* busy (sonics >= 2.3) */
363 #define SBIM_RJ 0x02000000 /* reject (sonics >= 2.3) */
366 #define SBTML_RESET 0x0001 /* reset */
367 #define SBTML_REJ_MASK 0x0006 /* reject field */
368 #define SBTML_REJ 0x0002 /* reject */
369 #define SBTML_TMPREJ 0x0004 /* temporary reject, for error recovery */
371 #define SBTML_SICF_SHIFT 16 /* Shift to locate the SI control flags in sbtml */
374 #define SBTMH_SERR 0x0001 /* serror */
375 #define SBTMH_INT 0x0002 /* interrupt */
376 #define SBTMH_BUSY 0x0004 /* busy */
377 #define SBTMH_TO 0x0020 /* timeout (sonics >= 2.3) */
379 #define SBTMH_SISF_SHIFT 16 /* Shift to locate the SI status flags in sbtmh */
382 #define SBIDL_INIT 0x80 /* initiator */
385 #define SBIDH_RC_MASK 0x000f /* revision code */
386 #define SBIDH_RCE_MASK 0x7000 /* revision code extension field */
387 #define SBIDH_RCE_SHIFT 8
388 #define SBCOREREV(sbidh) \
389 ((((sbidh) & SBIDH_RCE_MASK) >> SBIDH_RCE_SHIFT) | ((sbidh) & SBIDH_RC_MASK))
390 #define SBIDH_CC_MASK 0x8ff0 /* core code */
391 #define SBIDH_CC_SHIFT 4
392 #define SBIDH_VC_MASK 0xffff0000 /* vendor code */
393 #define SBIDH_VC_SHIFT 16
396 * Conversion of 802.1D priority to precedence level
398 #define PRIO2PREC(prio) \
399 (((prio) == PRIO_8021D_NONE || (prio) == PRIO_8021D_BE) ? \
402 DHD_SPINWAIT_SLEEP_INIT(sdioh_spinwait_sleep);
404 /* Core reg address translation */
405 #define CORE_CC_REG(base, field) (base + offsetof(chipcregs_t, field))
406 #define CORE_BUS_REG(base, field) \
407 (base + offsetof(struct sdpcmd_regs, field))
408 #define CORE_SB(base, field) \
409 (base + SBCONFIGOFF + offsetof(sbconfig_t, field))
412 /* Device console log buffer state */
414 uint count; /* Poll interval msec counter */
415 uint log_addr; /* Log struct address (fixed) */
416 struct rte_log log; /* Log struct (host copy) */
417 uint bufsize; /* Size of log buffer */
418 u8 *buf; /* Log buffer (host copy) */
419 uint last; /* Last buffer read index */
423 struct sdpcm_shared {
427 u32 assert_file_addr;
429 u32 console_addr; /* Address of struct rte_console */
435 /* misc chip info needed by some of the routines */
451 /* Private data for SDIO bus interaction */
452 typedef struct dhd_bus {
455 struct brcmf_sdio *sdh; /* Handle for BCMSDH calls */
456 struct chip_info *ci; /* Chip info struct */
457 char *vars; /* Variables (from CIS and/or other) */
458 uint varsz; /* Size of variables buffer */
459 u32 sbaddr; /* Current SB window pointer (-1, invalid) */
461 struct sdpcmd_regs *regs; /* SDIO core */
462 uint sdpcmrev; /* SDIO core revision */
463 uint armrev; /* CPU core revision */
464 uint ramrev; /* SOCRAM core revision */
465 u32 ramsize; /* Size of RAM in SOCRAM (bytes) */
466 u32 orig_ramsize; /* Size of RAM in SOCRAM (bytes) */
468 u32 bus; /* gSPI or SDIO bus */
469 u32 hostintmask; /* Copy of Host Interrupt Mask */
470 u32 intstatus; /* Intstatus bits (events) pending */
471 bool dpc_sched; /* Indicates DPC schedule (intrpt rcvd) */
472 bool fcstate; /* State of dongle flow-control */
474 u16 cl_devid; /* cached devid for brcmf_sdio_probe_attach() */
475 char *fw_path; /* module_param: path to firmware image */
476 char *nv_path; /* module_param: path to nvram vars file */
477 const char *nvram_params; /* user specified nvram params. */
479 uint blocksize; /* Block size of SDIO transfers */
480 uint roundup; /* Max roundup limit */
482 struct pktq txq; /* Queue length used for flow-control */
483 u8 flowcontrol; /* per prio flow control bitmask */
484 u8 tx_seq; /* Transmit sequence number (next) */
485 u8 tx_max; /* Maximum transmit sequence allowed */
487 u8 hdrbuf[MAX_HDR_READ + BRCMF_SDALIGN];
488 u8 *rxhdr; /* Header of current rx frame (in hdrbuf) */
489 u16 nextlen; /* Next Read Len from last header */
490 u8 rx_seq; /* Receive sequence number (expected) */
491 bool rxskip; /* Skip receive (awaiting NAK ACK) */
493 struct sk_buff *glomd; /* Packet containing glomming descriptor */
494 struct sk_buff *glom; /* Packet chain for glommed superframe */
495 uint glomerr; /* Glom packet read errors */
497 u8 *rxbuf; /* Buffer for receiving control packets */
498 uint rxblen; /* Allocated length of rxbuf */
499 u8 *rxctl; /* Aligned pointer into rxbuf */
500 u8 *databuf; /* Buffer for receiving big glom packet */
501 u8 *dataptr; /* Aligned pointer into databuf */
502 uint rxlen; /* Length of valid data in buffer */
504 u8 sdpcm_ver; /* Bus protocol reported by dongle */
506 bool intr; /* Use interrupts */
507 bool poll; /* Use polling */
508 bool ipend; /* Device interrupt is pending */
509 bool intdis; /* Interrupts disabled by isr */
510 uint intrcount; /* Count of device interrupt callbacks */
511 uint lastintrs; /* Count as of last watchdog timer */
512 uint spurious; /* Count of spurious interrupts */
513 uint pollrate; /* Ticks between device polls */
514 uint polltick; /* Tick counter */
515 uint pollcnt; /* Count of active polls */
518 struct dhd_console console; /* Console output polling support */
519 uint console_addr; /* Console address from shared struct */
522 uint regfails; /* Count of R_REG/W_REG failures */
524 uint clkstate; /* State of sd and backplane clock(s) */
525 bool activity; /* Activity flag for clock down */
526 s32 idletime; /* Control for activity timeout */
527 s32 idlecount; /* Activity timeout counter */
528 s32 idleclock; /* How to set bus driver when idle */
529 s32 sd_rxchain; /* If bcmsdh api accepts PKT chains */
530 bool use_rxchain; /* If dhd should use PKT chains */
531 bool sleeping; /* Is SDIO bus sleeping? */
532 bool rxflow_mode; /* Rx flow control mode */
533 bool rxflow; /* Is rx flow control on */
534 uint prev_rxlim_hit; /* Is prev rx limit exceeded
535 (per dpc schedule) */
536 bool alp_only; /* Don't use HT clock (ALP only) */
537 /* Field to decide if rx of control frames happen in rxbuf or lb-pool */
541 /* external loopback */
545 /* pktgen configuration */
546 uint pktgen_freq; /* Ticks between bursts */
547 uint pktgen_count; /* Packets to send each burst */
548 uint pktgen_print; /* Bursts between count displays */
549 uint pktgen_total; /* Stop after this many */
550 uint pktgen_minlen; /* Minimum packet data len */
551 uint pktgen_maxlen; /* Maximum packet data len */
552 uint pktgen_mode; /* Configured mode: tx, rx, or echo */
553 uint pktgen_stop; /* Number of tx failures causing stop */
555 /* active pktgen fields */
556 uint pktgen_tick; /* Tick counter for bursts */
557 uint pktgen_ptick; /* Burst counter for printing */
558 uint pktgen_sent; /* Number of test packets generated */
559 uint pktgen_rcvd; /* Number of test packets received */
560 uint pktgen_fail; /* Number of failed send attempts */
561 u16 pktgen_len; /* Length of next packet to send */
564 /* Some additional counters */
565 uint tx_sderrs; /* Count of tx attempts with sd errors */
566 uint fcqueued; /* Tx packets that got queued */
567 uint rxrtx; /* Count of rtx requests (NAK to dongle) */
568 uint rx_toolong; /* Receive frames too long to receive */
569 uint rxc_errors; /* SDIO errors when reading control frames */
570 uint rx_hdrfail; /* SDIO errors on header reads */
571 uint rx_badhdr; /* Bad received headers (roosync?) */
572 uint rx_badseq; /* Mismatched rx sequence number */
573 uint fc_rcvd; /* Number of flow-control events received */
574 uint fc_xoff; /* Number which turned on flow-control */
575 uint fc_xon; /* Number which turned off flow-control */
576 uint rxglomfail; /* Failed deglom attempts */
577 uint rxglomframes; /* Number of glom frames (superframes) */
578 uint rxglompkts; /* Number of packets from glom frames */
579 uint f2rxhdrs; /* Number of header reads */
580 uint f2rxdata; /* Number of frame data reads */
581 uint f2txdata; /* Number of f2 frame writes */
582 uint f1regdata; /* Number of f1 register accesses */
586 bool ctrl_frame_stat;
589 wait_queue_head_t ctrl_wait;
592 typedef volatile struct _sbconfig {
594 u32 sbipsflag; /* initiator port ocp slave flag */
596 u32 sbtpsflag; /* target port ocp slave flag */
598 u32 sbtmerrloga; /* (sonics >= 2.3) */
600 u32 sbtmerrlog; /* (sonics >= 2.3) */
602 u32 sbadmatch3; /* address match3 */
604 u32 sbadmatch2; /* address match2 */
606 u32 sbadmatch1; /* address match1 */
608 u32 sbimstate; /* initiator agent state */
609 u32 sbintvec; /* interrupt mask */
610 u32 sbtmstatelow; /* target state */
611 u32 sbtmstatehigh; /* target state */
612 u32 sbbwa0; /* bandwidth allocation table0 */
614 u32 sbimconfiglow; /* initiator configuration */
615 u32 sbimconfighigh; /* initiator configuration */
616 u32 sbadmatch0; /* address match0 */
618 u32 sbtmconfiglow; /* target configuration */
619 u32 sbtmconfighigh; /* target configuration */
620 u32 sbbconfig; /* broadcast configuration */
622 u32 sbbstate; /* broadcast state */
624 u32 sbactcnfg; /* activate configuration */
626 u32 sbflagst; /* current sbflags */
628 u32 sbidlow; /* identification */
629 u32 sbidhigh; /* identification */
635 #define CLK_PENDING 2 /* Not used yet */
638 #define DHD_NOPMU(dhd) (false)
641 static int qcount[NUMPRIO];
642 static int tx_packets[NUMPRIO];
645 /* Deferred transmit */
646 const uint brcmf_deferred_tx = 1;
653 /* override the RAM size if possible */
654 #define DONGLE_MIN_MEMSIZE (128 * 1024)
655 int brcmf_dongle_memsize;
657 static bool dhd_alignctl;
661 static bool retrydata;
662 #define RETRYCHAN(chan) (((chan) == SDPCM_EVENT_CHANNEL) || retrydata)
664 static const uint watermark = 8;
665 static const uint firstread = BRCMF_FIRSTREAD;
667 #define HDATLEN (firstread - (SDPCM_HDRLEN))
669 /* Retry count for register access failures */
670 static const uint retry_limit = 2;
672 /* Force even SD lengths (some host controllers mess up on odd bytes) */
673 static bool forcealign;
677 #define PKTALIGN(_p, _len, _align) \
680 datalign = (unsigned long)((_p)->data); \
681 datalign = roundup(datalign, (_align)) - datalign; \
682 ASSERT(datalign < (_align)); \
683 ASSERT((_p)->len >= ((_len) + datalign)); \
685 skb_pull((_p), datalign); \
686 __skb_trim((_p), (_len)); \
689 /* Limit on rounding up frames */
690 static const uint max_roundup = 512;
692 /* Try doing readahead */
693 static bool dhd_readahead;
695 /* To check if there's window offered */
696 #define DATAOK(bus) \
697 (((u8)(bus->tx_max - bus->tx_seq) != 0) && \
698 (((u8)(bus->tx_max - bus->tx_seq) & 0x80) == 0))
700 /* Macros to get register read/write status */
701 /* NOTE: these assume a local dhdsdio_bus_t *bus! */
702 #define R_SDREG(regvar, regaddr, retryvar) \
706 regvar = R_REG(regaddr); \
707 } while (brcmf_sdcard_regfail(bus->sdh) && \
708 (++retryvar <= retry_limit)); \
710 bus->regfails += (retryvar-1); \
711 if (retryvar > retry_limit) { \
712 DHD_ERROR(("%s: FAILED" #regvar "READ, LINE %d\n", \
713 __func__, __LINE__)); \
719 #define W_SDREG(regval, regaddr, retryvar) \
723 W_REG(regaddr, regval); \
724 } while (brcmf_sdcard_regfail(bus->sdh) && \
725 (++retryvar <= retry_limit)); \
727 bus->regfails += (retryvar-1); \
728 if (retryvar > retry_limit) \
729 DHD_ERROR(("%s: FAILED REGISTER WRITE, LINE %d\n", \
730 __func__, __LINE__)); \
734 #define DHD_BUS SDIO_BUS
736 #define PKT_AVAILABLE() (intstatus & I_HMB_FRAME_IND)
738 #define HOSTINTMASK (I_HMB_SW_MASK | I_CHIPACTIVE)
741 static void brcmf_sdbrcm_checkdied(dhd_bus_t *bus, void *pkt, uint seq);
742 static void brcmf_sdbrcm_sdtest_set(dhd_bus_t *bus, bool start);
746 static int brcmf_sdbrcm_checkdied(dhd_bus_t *bus, u8 *data, uint size);
747 static int brcmf_sdbrcm_mem_dump(dhd_bus_t *bus);
749 static int brcmf_sdbrcm_download_state(dhd_bus_t *bus, bool enter);
751 static void brcmf_sdbrcm_release(dhd_bus_t *bus);
752 static void brcmf_sdbrcm_release_malloc(dhd_bus_t *bus);
753 static void brcmf_sdbrcm_disconnect(void *ptr);
754 static bool brcmf_sdbrcm_chipmatch(u16 chipid);
755 static bool brcmf_sdbrcm_probe_attach(dhd_bus_t *bus, void *sdh,
756 void *regsva, u16 devid);
757 static bool brcmf_sdbrcm_probe_malloc(dhd_bus_t *bus, void *sdh);
758 static bool brcmf_sdbrcm_probe_init(dhd_bus_t *bus, void *sdh);
759 static void brcmf_sdbrcm_release_dongle(dhd_bus_t *bus);
761 static uint brcmf_process_nvram_vars(char *varbuf, uint len);
763 static void brcmf_sdbrcm_setmemsize(struct dhd_bus *bus, int mem_size);
764 static int brcmf_sdbrcm_send_buf(dhd_bus_t *bus, u32 addr, uint fn,
765 uint flags, u8 *buf, uint nbytes,
766 struct sk_buff *pkt, bcmsdh_cmplt_fn_t complete,
769 static bool brcmf_sdbrcm_download_firmware(struct dhd_bus *bus, void *sdh);
770 static int _brcmf_sdbrcm_download_firmware(struct dhd_bus *bus);
773 brcmf_sdbrcm_download_code_file(struct dhd_bus *bus, char *image_path);
774 static int brcmf_sdbrcm_download_nvram(struct dhd_bus *bus);
775 static void brcmf_sdbrcm_chip_disablecore(struct brcmf_sdio *sdh, u32 corebase);
776 static int brcmf_sdbrcm_chip_attach(struct dhd_bus *bus, void *regs);
777 static void brcmf_sdbrcm_chip_resetcore(struct brcmf_sdio *sdh, u32 corebase);
778 static void brcmf_sdbrcm_sdiod_drive_strength_init(struct dhd_bus *bus,
780 static void brcmf_sdbrcm_chip_detach(struct dhd_bus *bus);
781 static void brcmf_sdbrcm_wait_for_event(dhd_pub_t *dhd, bool *lockvar);
782 static void brcmf_sdbrcm_wait_event_wakeup(dhd_bus_t *bus);
784 /* Packet free applicable unconditionally for sdio and sdspi.
785 * Conditional if bufpool was present for gspi bus.
787 static void brcmf_sdbrcm_pktfree2(dhd_bus_t *bus, struct sk_buff *pkt)
789 if ((bus->bus != SPI_BUS) || bus->usebufpool)
790 brcmu_pkt_buf_free_skb(pkt);
793 static void brcmf_sdbrcm_setmemsize(struct dhd_bus *bus, int mem_size)
795 s32 min_size = DONGLE_MIN_MEMSIZE;
796 /* Restrict the memsize to user specified limit */
797 DHD_ERROR(("user: Restrict the dongle ram size to %d, min %d\n",
798 brcmf_dongle_memsize, min_size));
799 if ((brcmf_dongle_memsize > min_size) &&
800 (brcmf_dongle_memsize < (s32) bus->orig_ramsize))
801 bus->ramsize = brcmf_dongle_memsize;
804 static int brcmf_sdbrcm_set_siaddr_window(dhd_bus_t *bus, u32 address)
807 brcmf_sdcard_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_SBADDRLOW,
808 (address >> 8) & SBSDIO_SBADDRLOW_MASK, &err);
810 brcmf_sdcard_cfg_write(bus->sdh, SDIO_FUNC_1,
811 SBSDIO_FUNC1_SBADDRMID,
812 (address >> 16) & SBSDIO_SBADDRMID_MASK, &err);
814 brcmf_sdcard_cfg_write(bus->sdh, SDIO_FUNC_1,
815 SBSDIO_FUNC1_SBADDRHIGH,
816 (address >> 24) & SBSDIO_SBADDRHIGH_MASK,
821 /* Turn backplane clock on or off */
822 static int brcmf_sdbrcm_htclk(dhd_bus_t *bus, bool on, bool pendok)
825 u8 clkctl, clkreq, devctl;
826 struct brcmf_sdio *sdh;
828 DHD_TRACE(("%s: Enter\n", __func__));
834 /* Request HT Avail */
836 bus->alp_only ? SBSDIO_ALP_AVAIL_REQ : SBSDIO_HT_AVAIL_REQ;
838 if ((bus->ci->chip == BCM4329_CHIP_ID)
839 && (bus->ci->chiprev == 0))
840 clkreq |= SBSDIO_FORCE_ALP;
842 brcmf_sdcard_cfg_write(sdh, SDIO_FUNC_1,
843 SBSDIO_FUNC1_CHIPCLKCSR, clkreq, &err);
845 DHD_ERROR(("%s: HT Avail request error: %d\n",
850 if (pendok && ((bus->ci->buscoretype == PCMCIA_CORE_ID)
851 && (bus->ci->buscorerev == 9))) {
853 R_SDREG(dummy, &bus->regs->clockctlstatus, retries);
856 /* Check current status */
857 clkctl = brcmf_sdcard_cfg_read(sdh, SDIO_FUNC_1,
858 SBSDIO_FUNC1_CHIPCLKCSR, &err);
860 DHD_ERROR(("%s: HT Avail read error: %d\n",
865 /* Go to pending and await interrupt if appropriate */
866 if (!SBSDIO_CLKAV(clkctl, bus->alp_only) && pendok) {
867 /* Allow only clock-available interrupt */
868 devctl = brcmf_sdcard_cfg_read(sdh, SDIO_FUNC_1,
869 SBSDIO_DEVICE_CTL, &err);
871 DHD_ERROR(("%s: Devctl error setting CA: %d\n",
876 devctl |= SBSDIO_DEVCTL_CA_INT_ONLY;
877 brcmf_sdcard_cfg_write(sdh, SDIO_FUNC_1,
878 SBSDIO_DEVICE_CTL, devctl, &err);
879 DHD_INFO(("CLKCTL: set PENDING\n"));
880 bus->clkstate = CLK_PENDING;
883 } else if (bus->clkstate == CLK_PENDING) {
884 /* Cancel CA-only interrupt filter */
886 brcmf_sdcard_cfg_read(sdh, SDIO_FUNC_1,
887 SBSDIO_DEVICE_CTL, &err);
888 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
889 brcmf_sdcard_cfg_write(sdh, SDIO_FUNC_1,
890 SBSDIO_DEVICE_CTL, devctl, &err);
893 /* Otherwise, wait here (polling) for HT Avail */
894 if (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
895 SPINWAIT_SLEEP(sdioh_spinwait_sleep,
897 brcmf_sdcard_cfg_read(sdh, SDIO_FUNC_1,
898 SBSDIO_FUNC1_CHIPCLKCSR,
900 !SBSDIO_CLKAV(clkctl, bus->alp_only)),
901 PMU_MAX_TRANSITION_DLY);
904 DHD_ERROR(("%s: HT Avail request error: %d\n",
908 if (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
909 DHD_ERROR(("%s: HT Avail timeout (%d): clkctl 0x%02x\n",
910 __func__, PMU_MAX_TRANSITION_DLY, clkctl));
914 /* Mark clock available */
915 bus->clkstate = CLK_AVAIL;
916 DHD_INFO(("CLKCTL: turned ON\n"));
919 if (bus->alp_only != true) {
920 if (SBSDIO_ALPONLY(clkctl)) {
921 DHD_ERROR(("%s: HT Clock should be on.\n",
925 #endif /* defined (BCMDBG) */
927 bus->activity = true;
931 if (bus->clkstate == CLK_PENDING) {
932 /* Cancel CA-only interrupt filter */
933 devctl = brcmf_sdcard_cfg_read(sdh, SDIO_FUNC_1,
934 SBSDIO_DEVICE_CTL, &err);
935 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
936 brcmf_sdcard_cfg_write(sdh, SDIO_FUNC_1,
937 SBSDIO_DEVICE_CTL, devctl, &err);
940 bus->clkstate = CLK_SDONLY;
941 brcmf_sdcard_cfg_write(sdh, SDIO_FUNC_1,
942 SBSDIO_FUNC1_CHIPCLKCSR, clkreq, &err);
943 DHD_INFO(("CLKCTL: turned OFF\n"));
945 DHD_ERROR(("%s: Failed access turning clock off: %d\n",
953 /* Change idle/active SD state */
954 static int brcmf_sdbrcm_sdclk(dhd_bus_t *bus, bool on)
956 DHD_TRACE(("%s: Enter\n", __func__));
959 bus->clkstate = CLK_SDONLY;
961 bus->clkstate = CLK_NONE;
966 /* Transition SD and backplane clock readiness */
967 static int brcmf_sdbrcm_clkctl(dhd_bus_t *bus, uint target, bool pendok)
970 uint oldstate = bus->clkstate;
973 DHD_TRACE(("%s: Enter\n", __func__));
975 /* Early exit if we're already there */
976 if (bus->clkstate == target) {
977 if (target == CLK_AVAIL) {
978 brcmf_os_wd_timer(bus->dhd, brcmf_watchdog_ms);
979 bus->activity = true;
986 /* Make sure SD clock is available */
987 if (bus->clkstate == CLK_NONE)
988 brcmf_sdbrcm_sdclk(bus, true);
989 /* Now request HT Avail on the backplane */
990 brcmf_sdbrcm_htclk(bus, true, pendok);
991 brcmf_os_wd_timer(bus->dhd, brcmf_watchdog_ms);
992 bus->activity = true;
996 /* Remove HT request, or bring up SD clock */
997 if (bus->clkstate == CLK_NONE)
998 brcmf_sdbrcm_sdclk(bus, true);
999 else if (bus->clkstate == CLK_AVAIL)
1000 brcmf_sdbrcm_htclk(bus, false, false);
1002 DHD_ERROR(("brcmf_sdbrcm_clkctl: request for %d -> %d"
1003 "\n", bus->clkstate, target));
1004 brcmf_os_wd_timer(bus->dhd, brcmf_watchdog_ms);
1008 /* Make sure to remove HT request */
1009 if (bus->clkstate == CLK_AVAIL)
1010 brcmf_sdbrcm_htclk(bus, false, false);
1011 /* Now remove the SD clock */
1012 brcmf_sdbrcm_sdclk(bus, false);
1013 brcmf_os_wd_timer(bus->dhd, 0);
1017 DHD_INFO(("brcmf_sdbrcm_clkctl: %d -> %d\n", oldstate, bus->clkstate));
1023 int brcmf_sdbrcm_bussleep(dhd_bus_t *bus, bool sleep)
1025 struct brcmf_sdio *sdh = bus->sdh;
1026 struct sdpcmd_regs *regs = bus->regs;
1029 DHD_INFO(("brcmf_sdbrcm_bussleep: request %s (currently %s)\n",
1030 (sleep ? "SLEEP" : "WAKE"),
1031 (bus->sleeping ? "SLEEP" : "WAKE")));
1033 /* Done if we're already in the requested state */
1034 if (sleep == bus->sleeping)
1037 /* Going to sleep: set the alarm and turn off the lights... */
1039 /* Don't sleep if something is pending */
1040 if (bus->dpc_sched || bus->rxskip || pktq_len(&bus->txq))
1043 /* Disable SDIO interrupts (no longer interested) */
1044 brcmf_sdcard_intr_disable(bus->sdh);
1046 /* Make sure the controller has the bus up */
1047 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
1049 /* Tell device to start using OOB wakeup */
1050 W_SDREG(SMB_USE_OOB, ®s->tosbmailbox, retries);
1051 if (retries > retry_limit)
1052 DHD_ERROR(("CANNOT SIGNAL CHIP, WILL NOT WAKE UP!!\n"));
1054 /* Turn off our contribution to the HT clock request */
1055 brcmf_sdbrcm_clkctl(bus, CLK_SDONLY, false);
1057 brcmf_sdcard_cfg_write(sdh, SDIO_FUNC_1,
1058 SBSDIO_FUNC1_CHIPCLKCSR,
1059 SBSDIO_FORCE_HW_CLKREQ_OFF, NULL);
1061 /* Isolate the bus */
1062 if (bus->ci->chip != BCM4329_CHIP_ID
1063 && bus->ci->chip != BCM4319_CHIP_ID) {
1064 brcmf_sdcard_cfg_write(sdh, SDIO_FUNC_1,
1066 SBSDIO_DEVCTL_PADS_ISO, NULL);
1070 bus->sleeping = true;
1073 /* Waking up: bus power up is ok, set local state */
1075 brcmf_sdcard_cfg_write(sdh, SDIO_FUNC_1,
1076 SBSDIO_FUNC1_CHIPCLKCSR, 0, NULL);
1078 /* Force pad isolation off if possible
1079 (in case power never toggled) */
1080 if ((bus->ci->buscoretype == PCMCIA_CORE_ID)
1081 && (bus->ci->buscorerev >= 10))
1082 brcmf_sdcard_cfg_write(sdh, SDIO_FUNC_1,
1083 SBSDIO_DEVICE_CTL, 0, NULL);
1085 /* Make sure the controller has the bus up */
1086 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
1088 /* Send misc interrupt to indicate OOB not needed */
1089 W_SDREG(0, ®s->tosbmailboxdata, retries);
1090 if (retries <= retry_limit)
1091 W_SDREG(SMB_DEV_INT, ®s->tosbmailbox, retries);
1093 if (retries > retry_limit)
1094 DHD_ERROR(("CANNOT SIGNAL CHIP TO CLEAR OOB!!\n"));
1096 /* Make sure we have SD bus access */
1097 brcmf_sdbrcm_clkctl(bus, CLK_SDONLY, false);
1100 bus->sleeping = false;
1102 /* Enable interrupts again */
1103 if (bus->intr && (bus->dhd->busstate == DHD_BUS_DATA)) {
1104 bus->intdis = false;
1105 brcmf_sdcard_intr_enable(bus->sdh);
1112 #define BUS_WAKE(bus) \
1114 if ((bus)->sleeping) \
1115 brcmf_sdbrcm_bussleep((bus), false); \
1118 /* Writes a HW/SW header into the packet and sends it. */
1119 /* Assumes: (a) header space already there, (b) caller holds lock */
1120 static int brcmf_sdbrcm_txpkt(dhd_bus_t *bus, struct sk_buff *pkt, uint chan,
1128 struct brcmf_sdio *sdh;
1129 struct sk_buff *new;
1132 DHD_TRACE(("%s: Enter\n", __func__));
1136 if (bus->dhd->dongle_reset) {
1141 frame = (u8 *) (pkt->data);
1143 /* Add alignment padding, allocate new packet if needed */
1144 pad = ((unsigned long)frame % BRCMF_SDALIGN);
1146 if (skb_headroom(pkt) < pad) {
1147 DHD_INFO(("%s: insufficient headroom %d for %d pad\n",
1148 __func__, skb_headroom(pkt), pad));
1149 bus->dhd->tx_realloc++;
1150 new = brcmu_pkt_buf_get_skb(pkt->len + BRCMF_SDALIGN);
1152 DHD_ERROR(("%s: couldn't allocate new %d-byte "
1154 __func__, pkt->len + BRCMF_SDALIGN));
1159 PKTALIGN(new, pkt->len, BRCMF_SDALIGN);
1160 memcpy(new->data, pkt->data, pkt->len);
1162 brcmu_pkt_buf_free_skb(pkt);
1163 /* free the pkt if canned one is not used */
1166 frame = (u8 *) (pkt->data);
1167 ASSERT(((unsigned long)frame % BRCMF_SDALIGN) == 0);
1171 frame = (u8 *) (pkt->data);
1173 ASSERT((pad + SDPCM_HDRLEN) <= (int)(pkt->len));
1174 memset(frame, 0, pad + SDPCM_HDRLEN);
1177 ASSERT(pad < BRCMF_SDALIGN);
1179 /* Hardware tag: 2 byte len followed by 2 byte ~len check (all LE) */
1180 len = (u16) (pkt->len);
1181 *(u16 *) frame = cpu_to_le16(len);
1182 *(((u16 *) frame) + 1) = cpu_to_le16(~len);
1184 /* Software tag: channel, sequence number, data offset */
1186 ((chan << SDPCM_CHANNEL_SHIFT) & SDPCM_CHANNEL_MASK) | bus->tx_seq |
1188 SDPCM_HDRLEN) << SDPCM_DOFFSET_SHIFT) & SDPCM_DOFFSET_MASK);
1190 put_unaligned_le32(swheader, frame + SDPCM_FRAMETAG_LEN);
1191 put_unaligned_le32(0, frame + SDPCM_FRAMETAG_LEN + sizeof(swheader));
1194 tx_packets[pkt->priority]++;
1195 if (DHD_BYTES_ON() &&
1196 (((DHD_CTL_ON() && (chan == SDPCM_CONTROL_CHANNEL)) ||
1197 (DHD_DATA_ON() && (chan != SDPCM_CONTROL_CHANNEL))))) {
1198 printk(KERN_DEBUG "Tx Frame:\n");
1199 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, frame, len);
1200 } else if (DHD_HDRS_ON()) {
1201 printk(KERN_DEBUG "TxHdr:\n");
1202 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
1203 frame, min_t(u16, len, 16));
1207 /* Raise len to next SDIO block to eliminate tail command */
1208 if (bus->roundup && bus->blocksize && (len > bus->blocksize)) {
1209 u16 pad = bus->blocksize - (len % bus->blocksize);
1210 if ((pad <= bus->roundup) && (pad < bus->blocksize))
1212 } else if (len % BRCMF_SDALIGN) {
1213 len += BRCMF_SDALIGN - (len % BRCMF_SDALIGN);
1216 /* Some controllers have trouble with odd bytes -- round to even */
1217 if (forcealign && (len & (ALIGNMENT - 1))) {
1218 len = roundup(len, ALIGNMENT);
1222 ret = brcmf_sdbrcm_send_buf(bus, brcmf_sdcard_cur_sbwad(sdh),
1223 SDIO_FUNC_2, F2SYNC, frame, len, pkt, NULL, NULL);
1225 ASSERT(ret != -BCME_PENDING);
1228 /* On failure, abort the command
1229 and terminate the frame */
1230 DHD_INFO(("%s: sdio error %d, abort command and "
1231 "terminate frame.\n", __func__, ret));
1234 brcmf_sdcard_abort(sdh, SDIO_FUNC_2);
1235 brcmf_sdcard_cfg_write(sdh, SDIO_FUNC_1,
1236 SBSDIO_FUNC1_FRAMECTRL, SFC_WF_TERM,
1240 for (i = 0; i < 3; i++) {
1242 hi = brcmf_sdcard_cfg_read(sdh, SDIO_FUNC_1,
1243 SBSDIO_FUNC1_WFRAMEBCHI,
1245 lo = brcmf_sdcard_cfg_read(sdh, SDIO_FUNC_1,
1246 SBSDIO_FUNC1_WFRAMEBCLO,
1248 bus->f1regdata += 2;
1249 if ((hi == 0) && (lo == 0))
1255 bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQUENCE_WRAP;
1257 } while ((ret < 0) && retrydata && retries++ < TXRETRIES);
1260 /* restore pkt buffer pointer before calling tx complete routine */
1261 skb_pull(pkt, SDPCM_HDRLEN + pad);
1262 brcmf_os_sdunlock(bus->dhd);
1263 brcmf_txcomplete(bus->dhd, pkt, ret != 0);
1264 brcmf_os_sdlock(bus->dhd);
1267 brcmu_pkt_buf_free_skb(pkt);
1272 int brcmf_sdbrcm_bus_txdata(struct dhd_bus *bus, struct sk_buff *pkt)
1277 DHD_TRACE(("%s: Enter\n", __func__));
1282 /* Push the test header if doing loopback */
1283 if (bus->ext_loop) {
1285 skb_push(pkt, SDPCM_TEST_HDRLEN);
1287 *data++ = SDPCM_TEST_ECHOREQ;
1288 *data++ = (u8) bus->loopid++;
1289 *data++ = (datalen >> 0);
1290 *data++ = (datalen >> 8);
1291 datalen += SDPCM_TEST_HDRLEN;
1295 /* Add space for the header */
1296 skb_push(pkt, SDPCM_HDRLEN);
1297 ASSERT(IS_ALIGNED((unsigned long)(pkt->data), 2));
1299 prec = PRIO2PREC((pkt->priority & PRIOMASK));
1301 /* Check for existing queue, current flow-control,
1302 pending event, or pending clock */
1303 if (brcmf_deferred_tx || bus->fcstate || pktq_len(&bus->txq)
1304 || bus->dpc_sched || (!DATAOK(bus))
1305 || (bus->flowcontrol & NBITVAL(prec))
1306 || (bus->clkstate != CLK_AVAIL)) {
1307 DHD_TRACE(("%s: deferring pktq len %d\n", __func__,
1308 pktq_len(&bus->txq)));
1311 /* Priority based enq */
1312 spin_lock_bh(&bus->txqlock);
1313 if (brcmf_c_prec_enq(bus->dhd, &bus->txq, pkt, prec) == false) {
1314 skb_pull(pkt, SDPCM_HDRLEN);
1315 brcmf_txcomplete(bus->dhd, pkt, false);
1316 brcmu_pkt_buf_free_skb(pkt);
1317 DHD_ERROR(("%s: out of bus->txq !!!\n", __func__));
1322 spin_unlock_bh(&bus->txqlock);
1324 if (pktq_len(&bus->txq) >= TXHI)
1325 brcmf_txflowcontrol(bus->dhd, 0, ON);
1328 if (pktq_plen(&bus->txq, prec) > qcount[prec])
1329 qcount[prec] = pktq_plen(&bus->txq, prec);
1331 /* Schedule DPC if needed to send queued packet(s) */
1332 if (brcmf_deferred_tx && !bus->dpc_sched) {
1333 bus->dpc_sched = true;
1334 brcmf_sched_dpc(bus->dhd);
1337 /* Lock: we're about to use shared data/code (and SDIO) */
1338 brcmf_os_sdlock(bus->dhd);
1340 /* Otherwise, send it now */
1342 /* Make sure back plane ht clk is on, no pending allowed */
1343 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, true);
1346 DHD_TRACE(("%s: calling txpkt\n", __func__));
1347 ret = brcmf_sdbrcm_txpkt(bus, pkt, SDPCM_DATA_CHANNEL, true);
1349 ret = brcmf_sdbrcm_txpkt(bus, pkt,
1350 (bus->ext_loop ? SDPCM_TEST_CHANNEL :
1351 SDPCM_DATA_CHANNEL), true);
1354 bus->dhd->tx_errors++;
1356 bus->dhd->dstats.tx_bytes += datalen;
1358 if ((bus->idletime == DHD_IDLE_IMMEDIATE) && !bus->dpc_sched) {
1359 bus->activity = false;
1360 brcmf_sdbrcm_clkctl(bus, CLK_NONE, true);
1363 brcmf_os_sdunlock(bus->dhd);
1369 static uint brcmf_sdbrcm_sendfromq(dhd_bus_t *bus, uint maxframes)
1371 struct sk_buff *pkt;
1374 int ret = 0, prec_out;
1379 dhd_pub_t *dhd = bus->dhd;
1380 struct sdpcmd_regs *regs = bus->regs;
1382 DHD_TRACE(("%s: Enter\n", __func__));
1384 tx_prec_map = ~bus->flowcontrol;
1386 /* Send frames until the limit or some other event */
1387 for (cnt = 0; (cnt < maxframes) && DATAOK(bus); cnt++) {
1388 spin_lock_bh(&bus->txqlock);
1389 pkt = brcmu_pktq_mdeq(&bus->txq, tx_prec_map, &prec_out);
1391 spin_unlock_bh(&bus->txqlock);
1394 spin_unlock_bh(&bus->txqlock);
1395 datalen = pkt->len - SDPCM_HDRLEN;
1398 ret = brcmf_sdbrcm_txpkt(bus, pkt, SDPCM_DATA_CHANNEL, true);
1400 ret = brcmf_sdbrcm_txpkt(bus, pkt,
1401 (bus->ext_loop ? SDPCM_TEST_CHANNEL :
1402 SDPCM_DATA_CHANNEL), true);
1405 bus->dhd->tx_errors++;
1407 bus->dhd->dstats.tx_bytes += datalen;
1409 /* In poll mode, need to check for other events */
1410 if (!bus->intr && cnt) {
1411 /* Check device status, signal pending interrupt */
1412 R_SDREG(intstatus, ®s->intstatus, retries);
1414 if (brcmf_sdcard_regfail(bus->sdh))
1416 if (intstatus & bus->hostintmask)
1421 /* Deflow-control stack if needed */
1422 if (dhd->up && (dhd->busstate == DHD_BUS_DATA) &&
1423 dhd->txoff && (pktq_len(&bus->txq) < TXLOW))
1424 brcmf_txflowcontrol(dhd, 0, OFF);
1430 brcmf_sdbrcm_bus_txctl(struct dhd_bus *bus, unsigned char *msg, uint msglen)
1436 struct brcmf_sdio *sdh = bus->sdh;
1441 DHD_TRACE(("%s: Enter\n", __func__));
1443 if (bus->dhd->dongle_reset)
1446 /* Back the pointer to make a room for bus header */
1447 frame = msg - SDPCM_HDRLEN;
1448 len = (msglen += SDPCM_HDRLEN);
1450 /* Add alignment padding (optional for ctl frames) */
1452 doff = ((unsigned long)frame % BRCMF_SDALIGN);
1457 memset(frame, 0, doff + SDPCM_HDRLEN);
1459 ASSERT(doff < BRCMF_SDALIGN);
1461 doff += SDPCM_HDRLEN;
1463 /* Round send length to next SDIO block */
1464 if (bus->roundup && bus->blocksize && (len > bus->blocksize)) {
1465 u16 pad = bus->blocksize - (len % bus->blocksize);
1466 if ((pad <= bus->roundup) && (pad < bus->blocksize))
1468 } else if (len % BRCMF_SDALIGN) {
1469 len += BRCMF_SDALIGN - (len % BRCMF_SDALIGN);
1472 /* Satisfy length-alignment requirements */
1473 if (forcealign && (len & (ALIGNMENT - 1)))
1474 len = roundup(len, ALIGNMENT);
1476 ASSERT(IS_ALIGNED((unsigned long)frame, 2));
1478 /* Need to lock here to protect txseq and SDIO tx calls */
1479 brcmf_os_sdlock(bus->dhd);
1483 /* Make sure backplane clock is on */
1484 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
1486 /* Hardware tag: 2 byte len followed by 2 byte ~len check (all LE) */
1487 *(u16 *) frame = cpu_to_le16((u16) msglen);
1488 *(((u16 *) frame) + 1) = cpu_to_le16(~msglen);
1490 /* Software tag: channel, sequence number, data offset */
1492 ((SDPCM_CONTROL_CHANNEL << SDPCM_CHANNEL_SHIFT) &
1494 | bus->tx_seq | ((doff << SDPCM_DOFFSET_SHIFT) &
1495 SDPCM_DOFFSET_MASK);
1496 put_unaligned_le32(swheader, frame + SDPCM_FRAMETAG_LEN);
1497 put_unaligned_le32(0, frame + SDPCM_FRAMETAG_LEN + sizeof(swheader));
1500 DHD_INFO(("%s: No bus credit bus->tx_max %d, bus->tx_seq %d\n",
1501 __func__, bus->tx_max, bus->tx_seq));
1502 bus->ctrl_frame_stat = true;
1504 bus->ctrl_frame_buf = frame;
1505 bus->ctrl_frame_len = len;
1507 brcmf_sdbrcm_wait_for_event(bus->dhd, &bus->ctrl_frame_stat);
1509 if (bus->ctrl_frame_stat == false) {
1510 DHD_INFO(("%s: ctrl_frame_stat == false\n", __func__));
1513 DHD_INFO(("%s: ctrl_frame_stat == true\n", __func__));
1520 if (DHD_BYTES_ON() && DHD_CTL_ON()) {
1521 printk(KERN_DEBUG "Tx Frame:\n");
1522 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
1524 } else if (DHD_HDRS_ON()) {
1525 printk(KERN_DEBUG "TxHdr:\n");
1526 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
1527 frame, min_t(u16, len, 16));
1532 bus->ctrl_frame_stat = false;
1533 ret = brcmf_sdbrcm_send_buf(bus,
1534 brcmf_sdcard_cur_sbwad(sdh), SDIO_FUNC_2,
1535 F2SYNC, frame, len, NULL, NULL, NULL);
1537 ASSERT(ret != -BCME_PENDING);
1540 /* On failure, abort the command and
1541 terminate the frame */
1542 DHD_INFO(("%s: sdio error %d, abort command and terminate frame.\n",
1546 brcmf_sdcard_abort(sdh, SDIO_FUNC_2);
1548 brcmf_sdcard_cfg_write(sdh, SDIO_FUNC_1,
1549 SBSDIO_FUNC1_FRAMECTRL,
1553 for (i = 0; i < 3; i++) {
1555 hi = brcmf_sdcard_cfg_read(sdh,
1557 SBSDIO_FUNC1_WFRAMEBCHI,
1559 lo = brcmf_sdcard_cfg_read(sdh,
1561 SBSDIO_FUNC1_WFRAMEBCLO,
1563 bus->f1regdata += 2;
1564 if ((hi == 0) && (lo == 0))
1571 (bus->tx_seq + 1) % SDPCM_SEQUENCE_WRAP;
1573 } while ((ret < 0) && retries++ < TXRETRIES);
1576 if ((bus->idletime == DHD_IDLE_IMMEDIATE) && !bus->dpc_sched) {
1577 bus->activity = false;
1578 brcmf_sdbrcm_clkctl(bus, CLK_NONE, true);
1581 brcmf_os_sdunlock(bus->dhd);
1584 bus->dhd->tx_ctlerrs++;
1586 bus->dhd->tx_ctlpkts++;
1588 return ret ? -EIO : 0;
1591 int brcmf_sdbrcm_bus_rxctl(struct dhd_bus *bus, unsigned char *msg, uint msglen)
1597 DHD_TRACE(("%s: Enter\n", __func__));
1599 if (bus->dhd->dongle_reset)
1602 /* Wait until control frame is available */
1603 timeleft = brcmf_os_ioctl_resp_wait(bus->dhd, &bus->rxlen, &pending);
1605 brcmf_os_sdlock(bus->dhd);
1607 memcpy(msg, bus->rxctl, min(msglen, rxlen));
1609 brcmf_os_sdunlock(bus->dhd);
1612 DHD_CTL(("%s: resumed on rxctl frame, got %d expected %d\n",
1613 __func__, rxlen, msglen));
1614 } else if (timeleft == 0) {
1615 DHD_ERROR(("%s: resumed on timeout\n", __func__));
1617 brcmf_os_sdlock(bus->dhd);
1618 brcmf_sdbrcm_checkdied(bus, NULL, 0);
1619 brcmf_os_sdunlock(bus->dhd);
1621 } else if (pending == true) {
1622 DHD_CTL(("%s: cancelled\n", __func__));
1623 return -ERESTARTSYS;
1625 DHD_CTL(("%s: resumed for unknown reason?\n", __func__));
1627 brcmf_os_sdlock(bus->dhd);
1628 brcmf_sdbrcm_checkdied(bus, NULL, 0);
1629 brcmf_os_sdunlock(bus->dhd);
1634 bus->dhd->rx_ctlpkts++;
1636 bus->dhd->rx_ctlerrs++;
1638 return rxlen ? (int)rxlen : -ETIMEDOUT;
1677 const struct brcmu_iovar dhdsdio_iovars[] = {
1678 {"intr", IOV_INTR, 0, IOVT_BOOL, 0},
1679 {"sleep", IOV_SLEEP, 0, IOVT_BOOL, 0},
1680 {"pollrate", IOV_POLLRATE, 0, IOVT_UINT32, 0},
1681 {"idletime", IOV_IDLETIME, 0, IOVT_INT32, 0},
1682 {"idleclock", IOV_IDLECLOCK, 0, IOVT_INT32, 0},
1683 {"sd1idle", IOV_SD1IDLE, 0, IOVT_BOOL, 0},
1684 {"membytes", IOV_MEMBYTES, 0, IOVT_BUFFER, 2 * sizeof(int)},
1685 {"memsize", IOV_MEMSIZE, 0, IOVT_UINT32, 0},
1686 {"download", IOV_DOWNLOAD, 0, IOVT_BOOL, 0},
1687 {"vars", IOV_VARS, 0, IOVT_BUFFER, 0},
1688 {"sdiod_drive", IOV_SDIOD_DRIVE, 0, IOVT_UINT32, 0},
1689 {"readahead", IOV_READAHEAD, 0, IOVT_BOOL, 0},
1690 {"sdrxchain", IOV_SDRXCHAIN, 0, IOVT_BOOL, 0},
1691 {"alignctl", IOV_ALIGNCTL, 0, IOVT_BOOL, 0},
1692 {"sdalign", IOV_SDALIGN, 0, IOVT_BOOL, 0},
1693 {"devreset", IOV_DEVRESET, 0, IOVT_BOOL, 0},
1695 {"sdreg", IOV_SDREG, 0, IOVT_BUFFER, sizeof(struct brcmf_sdreg)}
1697 {"sbreg", IOV_SBREG, 0, IOVT_BUFFER, sizeof(struct brcmf_sdreg)}
1699 {"sd_cis", IOV_SDCIS, 0, IOVT_BUFFER, DHD_IOCTL_MAXLEN}
1701 {"forcealign", IOV_FORCEEVEN, 0, IOVT_BOOL, 0}
1703 {"txbound", IOV_TXBOUND, 0, IOVT_UINT32, 0}
1705 {"rxbound", IOV_RXBOUND, 0, IOVT_UINT32, 0}
1707 {"txminmax", IOV_TXMINMAX, 0, IOVT_UINT32, 0}
1709 {"cpu", IOV_CPU, 0, IOVT_BOOL, 0}
1711 {"checkdied", IOV_CHECKDIED, 0, IOVT_BUFFER, 0}
1715 {"extloop", IOV_EXTLOOP, 0, IOVT_BOOL, 0}
1717 {"pktgen", IOV_PKTGEN, 0, IOVT_BUFFER, sizeof(brcmf_pktgen_t)}
1725 dhd_dump_pct(struct brcmu_strbuf *strbuf, char *desc, uint num, uint div)
1730 brcmu_bprintf(strbuf, "%s N/A", desc);
1733 q2 = (100 * (num - (q1 * div))) / div;
1734 brcmu_bprintf(strbuf, "%s %d.%02d", desc, q1, q2);
1738 void brcmf_sdbrcm_bus_dump(dhd_pub_t *dhdp, struct brcmu_strbuf *strbuf)
1740 dhd_bus_t *bus = dhdp->bus;
1742 brcmu_bprintf(strbuf, "Bus SDIO structure:\n");
1743 brcmu_bprintf(strbuf,
1744 "hostintmask 0x%08x intstatus 0x%08x sdpcm_ver %d\n",
1745 bus->hostintmask, bus->intstatus, bus->sdpcm_ver);
1746 brcmu_bprintf(strbuf,
1747 "fcstate %d qlen %d tx_seq %d, max %d, rxskip %d rxlen %d rx_seq %d\n",
1748 bus->fcstate, pktq_len(&bus->txq), bus->tx_seq, bus->tx_max,
1749 bus->rxskip, bus->rxlen, bus->rx_seq);
1750 brcmu_bprintf(strbuf, "intr %d intrcount %d lastintrs %d spurious %d\n",
1751 bus->intr, bus->intrcount, bus->lastintrs, bus->spurious);
1752 brcmu_bprintf(strbuf, "pollrate %d pollcnt %d regfails %d\n",
1753 bus->pollrate, bus->pollcnt, bus->regfails);
1755 brcmu_bprintf(strbuf, "\nAdditional counters:\n");
1756 brcmu_bprintf(strbuf,
1757 "tx_sderrs %d fcqueued %d rxrtx %d rx_toolong %d rxc_errors %d\n",
1758 bus->tx_sderrs, bus->fcqueued, bus->rxrtx, bus->rx_toolong,
1760 brcmu_bprintf(strbuf, "rx_hdrfail %d badhdr %d badseq %d\n",
1761 bus->rx_hdrfail, bus->rx_badhdr, bus->rx_badseq);
1762 brcmu_bprintf(strbuf, "fc_rcvd %d, fc_xoff %d, fc_xon %d\n",
1763 bus->fc_rcvd, bus->fc_xoff, bus->fc_xon);
1764 brcmu_bprintf(strbuf, "rxglomfail %d, rxglomframes %d, rxglompkts %d\n",
1765 bus->rxglomfail, bus->rxglomframes, bus->rxglompkts);
1766 brcmu_bprintf(strbuf, "f2rx (hdrs/data) %d (%d/%d), f2tx %d f1regs"
1768 (bus->f2rxhdrs + bus->f2rxdata), bus->f2rxhdrs,
1769 bus->f2rxdata, bus->f2txdata, bus->f1regdata);
1771 dhd_dump_pct(strbuf, "\nRx: pkts/f2rd", bus->dhd->rx_packets,
1772 (bus->f2rxhdrs + bus->f2rxdata));
1773 dhd_dump_pct(strbuf, ", pkts/f1sd", bus->dhd->rx_packets,
1775 dhd_dump_pct(strbuf, ", pkts/sd", bus->dhd->rx_packets,
1776 (bus->f2rxhdrs + bus->f2rxdata + bus->f1regdata));
1777 dhd_dump_pct(strbuf, ", pkts/int", bus->dhd->rx_packets,
1779 brcmu_bprintf(strbuf, "\n");
1781 dhd_dump_pct(strbuf, "Rx: glom pct", (100 * bus->rxglompkts),
1782 bus->dhd->rx_packets);
1783 dhd_dump_pct(strbuf, ", pkts/glom", bus->rxglompkts,
1785 brcmu_bprintf(strbuf, "\n");
1787 dhd_dump_pct(strbuf, "Tx: pkts/f2wr", bus->dhd->tx_packets,
1789 dhd_dump_pct(strbuf, ", pkts/f1sd", bus->dhd->tx_packets,
1791 dhd_dump_pct(strbuf, ", pkts/sd", bus->dhd->tx_packets,
1792 (bus->f2txdata + bus->f1regdata));
1793 dhd_dump_pct(strbuf, ", pkts/int", bus->dhd->tx_packets,
1795 brcmu_bprintf(strbuf, "\n");
1797 dhd_dump_pct(strbuf, "Total: pkts/f2rw",
1798 (bus->dhd->tx_packets + bus->dhd->rx_packets),
1799 (bus->f2txdata + bus->f2rxhdrs + bus->f2rxdata));
1800 dhd_dump_pct(strbuf, ", pkts/f1sd",
1801 (bus->dhd->tx_packets + bus->dhd->rx_packets),
1803 dhd_dump_pct(strbuf, ", pkts/sd",
1804 (bus->dhd->tx_packets + bus->dhd->rx_packets),
1805 (bus->f2txdata + bus->f2rxhdrs + bus->f2rxdata +
1807 dhd_dump_pct(strbuf, ", pkts/int",
1808 (bus->dhd->tx_packets + bus->dhd->rx_packets),
1810 brcmu_bprintf(strbuf, "\n\n");
1814 if (bus->pktgen_count) {
1815 brcmu_bprintf(strbuf, "pktgen config and count:\n");
1816 brcmu_bprintf(strbuf,
1817 "freq %d count %d print %d total %d min %d len %d\n",
1818 bus->pktgen_freq, bus->pktgen_count,
1819 bus->pktgen_print, bus->pktgen_total,
1820 bus->pktgen_minlen, bus->pktgen_maxlen);
1821 brcmu_bprintf(strbuf, "send attempts %d rcvd %d fail %d\n",
1822 bus->pktgen_sent, bus->pktgen_rcvd,
1827 brcmu_bprintf(strbuf, "dpc_sched %d host interrupt%spending\n",
1829 (brcmf_sdcard_intr_pending(bus->sdh) ? " " : " not "));
1830 brcmu_bprintf(strbuf, "blocksize %d roundup %d\n", bus->blocksize,
1833 brcmu_bprintf(strbuf,
1834 "clkstate %d activity %d idletime %d idlecount %d sleeping %d\n",
1835 bus->clkstate, bus->activity, bus->idletime, bus->idlecount,
1839 void dhd_bus_clearcounts(dhd_pub_t *dhdp)
1841 dhd_bus_t *bus = (dhd_bus_t *) dhdp->bus;
1843 bus->intrcount = bus->lastintrs = bus->spurious = bus->regfails = 0;
1844 bus->rxrtx = bus->rx_toolong = bus->rxc_errors = 0;
1845 bus->rx_hdrfail = bus->rx_badhdr = bus->rx_badseq = 0;
1846 bus->tx_sderrs = bus->fc_rcvd = bus->fc_xoff = bus->fc_xon = 0;
1847 bus->rxglomfail = bus->rxglomframes = bus->rxglompkts = 0;
1848 bus->f2rxhdrs = bus->f2rxdata = bus->f2txdata = bus->f1regdata = 0;
1852 static int brcmf_sdbrcm_pktgen_get(dhd_bus_t *bus, u8 *arg)
1854 brcmf_pktgen_t pktgen;
1856 pktgen.version = DHD_PKTGEN_VERSION;
1857 pktgen.freq = bus->pktgen_freq;
1858 pktgen.count = bus->pktgen_count;
1859 pktgen.print = bus->pktgen_print;
1860 pktgen.total = bus->pktgen_total;
1861 pktgen.minlen = bus->pktgen_minlen;
1862 pktgen.maxlen = bus->pktgen_maxlen;
1863 pktgen.numsent = bus->pktgen_sent;
1864 pktgen.numrcvd = bus->pktgen_rcvd;
1865 pktgen.numfail = bus->pktgen_fail;
1866 pktgen.mode = bus->pktgen_mode;
1867 pktgen.stop = bus->pktgen_stop;
1869 memcpy(arg, &pktgen, sizeof(pktgen));
1874 static int brcmf_sdbrcm_pktgen_set(dhd_bus_t *bus, u8 *arg)
1876 brcmf_pktgen_t pktgen;
1877 uint oldcnt, oldmode;
1879 memcpy(&pktgen, arg, sizeof(pktgen));
1880 if (pktgen.version != DHD_PKTGEN_VERSION)
1883 oldcnt = bus->pktgen_count;
1884 oldmode = bus->pktgen_mode;
1886 bus->pktgen_freq = pktgen.freq;
1887 bus->pktgen_count = pktgen.count;
1888 bus->pktgen_print = pktgen.print;
1889 bus->pktgen_total = pktgen.total;
1890 bus->pktgen_minlen = pktgen.minlen;
1891 bus->pktgen_maxlen = pktgen.maxlen;
1892 bus->pktgen_mode = pktgen.mode;
1893 bus->pktgen_stop = pktgen.stop;
1895 bus->pktgen_tick = bus->pktgen_ptick = 0;
1896 bus->pktgen_len = max(bus->pktgen_len, bus->pktgen_minlen);
1897 bus->pktgen_len = min(bus->pktgen_len, bus->pktgen_maxlen);
1899 /* Clear counts for a new pktgen (mode change, or was stopped) */
1900 if (bus->pktgen_count && (!oldcnt || oldmode != bus->pktgen_mode))
1901 bus->pktgen_sent = bus->pktgen_rcvd = bus->pktgen_fail = 0;
1908 brcmf_sdbrcm_membytes(dhd_bus_t *bus, bool write, u32 address, u8 *data,
1915 /* Determine initial transfer parameters */
1916 sdaddr = address & SBSDIO_SB_OFT_ADDR_MASK;
1917 if ((sdaddr + size) & SBSDIO_SBWINDOW_MASK)
1918 dsize = (SBSDIO_SB_OFT_ADDR_LIMIT - sdaddr);
1922 /* Set the backplane window to include the start address */
1923 bcmerror = brcmf_sdbrcm_set_siaddr_window(bus, address);
1925 DHD_ERROR(("%s: window change failed\n", __func__));
1929 /* Do the transfer(s) */
1931 DHD_INFO(("%s: %s %d bytes at offset 0x%08x in window 0x%08x\n",
1932 __func__, (write ? "write" : "read"), dsize,
1933 sdaddr, (address & SBSDIO_SBWINDOW_MASK)));
1935 brcmf_sdcard_rwdata(bus->sdh, write, sdaddr, data, dsize);
1937 DHD_ERROR(("%s: membytes transfer failed\n", __func__));
1941 /* Adjust for next transfer (if any) */
1946 bcmerror = brcmf_sdbrcm_set_siaddr_window(bus, address);
1948 DHD_ERROR(("%s: window change failed\n",
1953 dsize = min_t(uint, SBSDIO_SB_OFT_ADDR_LIMIT, size);
1958 /* Return the window to backplane enumeration space for core access */
1959 if (brcmf_sdbrcm_set_siaddr_window(bus,
1960 brcmf_sdcard_cur_sbwad(bus->sdh))) {
1961 DHD_ERROR(("%s: FAILED to set window back to 0x%x\n",
1962 __func__, brcmf_sdcard_cur_sbwad(bus->sdh)));
1969 static int brcmf_sdbrcm_readshared(dhd_bus_t *bus, struct sdpcm_shared *sh)
1974 /* Read last word in memory to determine address of
1975 sdpcm_shared structure */
1976 rv = brcmf_sdbrcm_membytes(bus, false, bus->ramsize - 4, (u8 *)&addr,
1981 addr = le32_to_cpu(addr);
1983 DHD_INFO(("sdpcm_shared address 0x%08X\n", addr));
1986 * Check if addr is valid.
1987 * NVRAM length at the end of memory should have been overwritten.
1989 if (addr == 0 || ((~addr >> 16) & 0xffff) == (addr & 0xffff)) {
1990 DHD_ERROR(("%s: address (0x%08x) of sdpcm_shared invalid\n",
1995 /* Read rte_shared structure */
1996 rv = brcmf_sdbrcm_membytes(bus, false, addr, (u8 *) sh,
1997 sizeof(struct sdpcm_shared));
2002 sh->flags = le32_to_cpu(sh->flags);
2003 sh->trap_addr = le32_to_cpu(sh->trap_addr);
2004 sh->assert_exp_addr = le32_to_cpu(sh->assert_exp_addr);
2005 sh->assert_file_addr = le32_to_cpu(sh->assert_file_addr);
2006 sh->assert_line = le32_to_cpu(sh->assert_line);
2007 sh->console_addr = le32_to_cpu(sh->console_addr);
2008 sh->msgtrace_addr = le32_to_cpu(sh->msgtrace_addr);
2010 if ((sh->flags & SDPCM_SHARED_VERSION_MASK) != SDPCM_SHARED_VERSION) {
2011 DHD_ERROR(("%s: sdpcm_shared version %d in dhd "
2012 "is different than sdpcm_shared version %d in dongle\n",
2013 __func__, SDPCM_SHARED_VERSION,
2014 sh->flags & SDPCM_SHARED_VERSION_MASK));
2021 static int brcmf_sdbrcm_checkdied(dhd_bus_t *bus, u8 *data, uint size)
2025 char *mbuffer = NULL;
2026 uint maxstrlen = 256;
2028 struct brcmf_trap tr;
2029 struct sdpcm_shared sdpcm_shared;
2030 struct brcmu_strbuf strbuf;
2032 DHD_TRACE(("%s: Enter\n", __func__));
2036 * Called after a rx ctrl timeout. "data" is NULL.
2037 * allocate memory to trace the trap or assert.
2040 mbuffer = data = kmalloc(msize, GFP_ATOMIC);
2041 if (mbuffer == NULL) {
2042 DHD_ERROR(("%s: kmalloc(%d) failed\n", __func__,
2049 str = kmalloc(maxstrlen, GFP_ATOMIC);
2051 DHD_ERROR(("%s: kmalloc(%d) failed\n", __func__, maxstrlen));
2056 bcmerror = brcmf_sdbrcm_readshared(bus, &sdpcm_shared);
2060 brcmu_binit(&strbuf, data, size);
2062 brcmu_bprintf(&strbuf,
2063 "msgtrace address : 0x%08X\nconsole address : 0x%08X\n",
2064 sdpcm_shared.msgtrace_addr, sdpcm_shared.console_addr);
2066 if ((sdpcm_shared.flags & SDPCM_SHARED_ASSERT_BUILT) == 0) {
2067 /* NOTE: Misspelled assert is intentional - DO NOT FIX.
2068 * (Avoids conflict with real asserts for programmatic
2069 * parsing of output.)
2071 brcmu_bprintf(&strbuf, "Assrt not built in dongle\n");
2074 if ((sdpcm_shared.flags & (SDPCM_SHARED_ASSERT | SDPCM_SHARED_TRAP)) ==
2076 /* NOTE: Misspelled assert is intentional - DO NOT FIX.
2077 * (Avoids conflict with real asserts for programmatic
2078 * parsing of output.)
2080 brcmu_bprintf(&strbuf, "No trap%s in dongle",
2081 (sdpcm_shared.flags & SDPCM_SHARED_ASSERT_BUILT)
2084 if (sdpcm_shared.flags & SDPCM_SHARED_ASSERT) {
2085 /* Download assert */
2086 brcmu_bprintf(&strbuf, "Dongle assert");
2087 if (sdpcm_shared.assert_exp_addr != 0) {
2089 bcmerror = brcmf_sdbrcm_membytes(bus, false,
2090 sdpcm_shared.assert_exp_addr,
2091 (u8 *) str, maxstrlen);
2095 str[maxstrlen - 1] = '\0';
2096 brcmu_bprintf(&strbuf, " expr \"%s\"", str);
2099 if (sdpcm_shared.assert_file_addr != 0) {
2101 bcmerror = brcmf_sdbrcm_membytes(bus, false,
2102 sdpcm_shared.assert_file_addr,
2103 (u8 *) str, maxstrlen);
2107 str[maxstrlen - 1] = '\0';
2108 brcmu_bprintf(&strbuf, " file \"%s\"", str);
2111 brcmu_bprintf(&strbuf, " line %d ",
2112 sdpcm_shared.assert_line);
2115 if (sdpcm_shared.flags & SDPCM_SHARED_TRAP) {
2116 bcmerror = brcmf_sdbrcm_membytes(bus, false,
2117 sdpcm_shared.trap_addr, (u8 *)&tr,
2118 sizeof(struct brcmf_trap));
2122 brcmu_bprintf(&strbuf,
2123 "Dongle trap type 0x%x @ epc 0x%x, cpsr 0x%x, spsr 0x%x, sp 0x%x,"
2124 "lp 0x%x, rpc 0x%x Trap offset 0x%x, "
2125 "r0 0x%x, r1 0x%x, r2 0x%x, r3 0x%x, r4 0x%x, r5 0x%x, r6 0x%x, r7 0x%x\n",
2126 tr.type, tr.epc, tr.cpsr, tr.spsr, tr.r13,
2127 tr.r14, tr.pc, sdpcm_shared.trap_addr,
2128 tr.r0, tr.r1, tr.r2, tr.r3, tr.r4, tr.r5,
2133 if (sdpcm_shared.flags & (SDPCM_SHARED_ASSERT | SDPCM_SHARED_TRAP))
2134 DHD_ERROR(("%s: %s\n", __func__, strbuf.origbuf));
2137 if (sdpcm_shared.flags & SDPCM_SHARED_TRAP) {
2138 /* Mem dump to a file on device */
2139 brcmf_sdbrcm_mem_dump(bus);
2150 static int brcmf_sdbrcm_mem_dump(dhd_bus_t *bus)
2153 int size; /* Full mem size */
2154 int start = 0; /* Start address */
2155 int read_size = 0; /* Read size of each iteration */
2156 u8 *buf = NULL, *databuf = NULL;
2158 /* Get full mem size */
2159 size = bus->ramsize;
2160 buf = kmalloc(size, GFP_ATOMIC);
2162 DHD_ERROR(("%s: Out of memory (%d bytes)\n", __func__, size));
2166 /* Read mem content */
2167 printk(KERN_DEBUG "Dump dongle memory");
2170 read_size = min(MEMBLOCK, size);
2171 ret = brcmf_sdbrcm_membytes(bus, false, start, databuf,
2174 DHD_ERROR(("%s: Error membytes %d\n", __func__, ret));
2180 /* Decrement size and increment start address */
2183 databuf += read_size;
2185 printk(KERN_DEBUG "Done\n");
2187 /* free buf before return !!! */
2188 if (brcmf_write_to_file(bus->dhd, buf, bus->ramsize)) {
2189 DHD_ERROR(("%s: Error writing to files\n", __func__));
2193 /* buf free handled in brcmf_write_to_file, not here */
2197 #define CONSOLE_LINE_MAX 192
2199 static int brcmf_sdbrcm_readconsole(dhd_bus_t *bus)
2201 struct dhd_console *c = &bus->console;
2202 u8 line[CONSOLE_LINE_MAX], ch;
2206 /* Don't do anything until FWREADY updates console address */
2207 if (bus->console_addr == 0)
2210 /* Read console log struct */
2211 addr = bus->console_addr + offsetof(struct rte_console, log);
2212 rv = brcmf_sdbrcm_membytes(bus, false, addr, (u8 *)&c->log,
2217 /* Allocate console buffer (one time only) */
2218 if (c->buf == NULL) {
2219 c->bufsize = le32_to_cpu(c->log.buf_size);
2220 c->buf = kmalloc(c->bufsize, GFP_ATOMIC);
2225 idx = le32_to_cpu(c->log.idx);
2227 /* Protect against corrupt value */
2228 if (idx > c->bufsize)
2231 /* Skip reading the console buffer if the index pointer
2236 /* Read the console buffer */
2237 addr = le32_to_cpu(c->log.buf);
2238 rv = brcmf_sdbrcm_membytes(bus, false, addr, c->buf, c->bufsize);
2242 while (c->last != idx) {
2243 for (n = 0; n < CONSOLE_LINE_MAX - 2; n++) {
2244 if (c->last == idx) {
2245 /* This would output a partial line.
2247 * the buffer pointer and output this
2248 * line next time around.
2253 c->last = c->bufsize - n;
2256 ch = c->buf[c->last];
2257 c->last = (c->last + 1) % c->bufsize;
2264 if (line[n - 1] == '\r')
2267 printk(KERN_DEBUG "CONSOLE: %s\n", line);
2276 int brcmf_sdbrcm_downloadvars(dhd_bus_t *bus, void *arg, int len)
2280 DHD_TRACE(("%s: Enter\n", __func__));
2282 /* Basic sanity checks */
2284 bcmerror = -EISCONN;
2288 bcmerror = -EOVERFLOW;
2292 /* Free the old ones and replace with passed variables */
2295 bus->vars = kmalloc(len, GFP_ATOMIC);
2296 bus->varsz = bus->vars ? len : 0;
2297 if (bus->vars == NULL) {
2302 /* Copy the passed variables, which should include the
2303 terminating double-null */
2304 memcpy(bus->vars, arg, bus->varsz);
2310 brcmf_sdbrcm_doiovar(dhd_bus_t *bus, const struct brcmu_iovar *vi, u32 actionid,
2311 const char *name, void *params, int plen, void *arg, int len,
2318 DHD_TRACE(("%s: Enter, action %d name %s params %p plen %d arg %p "
2319 "len %d val_size %d\n",
2320 __func__, actionid, name, params, plen, arg, len, val_size));
2322 bcmerror = brcmu_iovar_lencheck(vi, arg, len, IOV_ISSET(actionid));
2326 if (plen >= (int)sizeof(int_val))
2327 memcpy(&int_val, params, sizeof(int_val));
2329 bool_val = (int_val != 0) ? true : false;
2331 /* Some ioctls use the bus */
2332 brcmf_os_sdlock(bus->dhd);
2334 /* Check if dongle is in reset. If so, only allow DEVRESET iovars */
2335 if (bus->dhd->dongle_reset && !(actionid == IOV_SVAL(IOV_DEVRESET) ||
2336 actionid == IOV_GVAL(IOV_DEVRESET))) {
2341 /* Handle sleep stuff before any clock mucking */
2342 if (vi->varid == IOV_SLEEP) {
2343 if (IOV_ISSET(actionid)) {
2344 bcmerror = brcmf_sdbrcm_bussleep(bus, bool_val);
2346 int_val = (s32) bus->sleeping;
2347 memcpy(arg, &int_val, val_size);
2352 /* Request clock to allow SDIO accesses */
2353 if (!bus->dhd->dongle_reset) {
2355 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
2359 case IOV_GVAL(IOV_INTR):
2360 int_val = (s32) bus->intr;
2361 memcpy(arg, &int_val, val_size);
2364 case IOV_SVAL(IOV_INTR):
2365 bus->intr = bool_val;
2366 bus->intdis = false;
2369 DHD_INTR(("%s: enable SDIO device interrupts\n",
2371 brcmf_sdcard_intr_enable(bus->sdh);
2373 DHD_INTR(("%s: disable SDIO interrupts\n",
2375 brcmf_sdcard_intr_disable(bus->sdh);
2380 case IOV_GVAL(IOV_POLLRATE):
2381 int_val = (s32) bus->pollrate;
2382 memcpy(arg, &int_val, val_size);
2385 case IOV_SVAL(IOV_POLLRATE):
2386 bus->pollrate = (uint) int_val;
2387 bus->poll = (bus->pollrate != 0);
2390 case IOV_GVAL(IOV_IDLETIME):
2391 int_val = bus->idletime;
2392 memcpy(arg, &int_val, val_size);
2395 case IOV_SVAL(IOV_IDLETIME):
2396 if ((int_val < 0) && (int_val != DHD_IDLE_IMMEDIATE))
2399 bus->idletime = int_val;
2402 case IOV_GVAL(IOV_IDLECLOCK):
2403 int_val = (s32) bus->idleclock;
2404 memcpy(arg, &int_val, val_size);
2407 case IOV_SVAL(IOV_IDLECLOCK):
2408 bus->idleclock = int_val;
2411 case IOV_GVAL(IOV_SD1IDLE):
2412 int_val = (s32) sd1idle;
2413 memcpy(arg, &int_val, val_size);
2416 case IOV_SVAL(IOV_SD1IDLE):
2420 case IOV_SVAL(IOV_MEMBYTES):
2421 case IOV_GVAL(IOV_MEMBYTES):
2427 bool set = (actionid == IOV_SVAL(IOV_MEMBYTES));
2429 ASSERT(plen >= 2 * sizeof(int));
2431 address = (u32) int_val;
2432 memcpy(&int_val, (char *)params + sizeof(int_val),
2434 size = (uint) int_val;
2436 /* Do some validation */
2437 dsize = set ? plen - (2 * sizeof(int)) : len;
2439 DHD_ERROR(("%s: error on %s membytes, addr "
2440 "0x%08x size %d dsize %d\n",
2441 __func__, (set ? "set" : "get"),
2442 address, size, dsize));
2447 DHD_INFO(("%s: Request to %s %d bytes at address "
2449 __func__, (set ? "write" : "read"), size, address));
2451 /* If we know about SOCRAM, check for a fit */
2452 if ((bus->orig_ramsize) &&
2453 ((address > bus->orig_ramsize)
2454 || (address + size > bus->orig_ramsize))) {
2455 DHD_ERROR(("%s: ramsize 0x%08x doesn't have %d "
2456 "bytes at 0x%08x\n",
2457 __func__, bus->orig_ramsize, size, address));
2462 /* Generate the actual data pointer */
2464 set ? (u8 *) params +
2465 2 * sizeof(int) : (u8 *) arg;
2467 /* Call to do the transfer */
2468 bcmerror = brcmf_sdbrcm_membytes(bus, set, address,
2474 case IOV_GVAL(IOV_MEMSIZE):
2475 int_val = (s32) bus->ramsize;
2476 memcpy(arg, &int_val, val_size);
2479 case IOV_GVAL(IOV_SDIOD_DRIVE):
2480 int_val = (s32) brcmf_sdiod_drive_strength;
2481 memcpy(arg, &int_val, val_size);
2484 case IOV_SVAL(IOV_SDIOD_DRIVE):
2485 brcmf_sdiod_drive_strength = int_val;
2486 brcmf_sdbrcm_sdiod_drive_strength_init(bus,
2487 brcmf_sdiod_drive_strength);
2490 case IOV_SVAL(IOV_DOWNLOAD):
2491 bcmerror = brcmf_sdbrcm_download_state(bus, bool_val);
2494 case IOV_SVAL(IOV_VARS):
2495 bcmerror = brcmf_sdbrcm_downloadvars(bus, arg, len);
2498 case IOV_GVAL(IOV_READAHEAD):
2499 int_val = (s32) dhd_readahead;
2500 memcpy(arg, &int_val, val_size);
2503 case IOV_SVAL(IOV_READAHEAD):
2504 if (bool_val && !dhd_readahead)
2506 dhd_readahead = bool_val;
2509 case IOV_GVAL(IOV_SDRXCHAIN):
2510 int_val = (s32) bus->use_rxchain;
2511 memcpy(arg, &int_val, val_size);
2514 case IOV_SVAL(IOV_SDRXCHAIN):
2515 if (bool_val && !bus->sd_rxchain)
2516 bcmerror = -ENOTSUPP;
2518 bus->use_rxchain = bool_val;
2520 case IOV_GVAL(IOV_ALIGNCTL):
2521 int_val = (s32) dhd_alignctl;
2522 memcpy(arg, &int_val, val_size);
2525 case IOV_SVAL(IOV_ALIGNCTL):
2526 dhd_alignctl = bool_val;
2529 case IOV_GVAL(IOV_SDALIGN):
2530 int_val = BRCMF_SDALIGN;
2531 memcpy(arg, &int_val, val_size);
2535 case IOV_GVAL(IOV_VARS):
2536 if (bus->varsz < (uint) len)
2537 memcpy(arg, bus->vars, bus->varsz);
2539 bcmerror = -EOVERFLOW;
2544 case IOV_GVAL(IOV_SDREG):
2546 struct brcmf_sdreg *sd_ptr;
2549 sd_ptr = (struct brcmf_sdreg *) params;
2551 addr = (unsigned long)bus->regs + sd_ptr->offset;
2552 size = sd_ptr->func;
2553 int_val = (s32) brcmf_sdcard_reg_read(bus->sdh, addr,
2555 if (brcmf_sdcard_regfail(bus->sdh))
2557 memcpy(arg, &int_val, sizeof(s32));
2561 case IOV_SVAL(IOV_SDREG):
2563 struct brcmf_sdreg *sd_ptr;
2566 sd_ptr = (struct brcmf_sdreg *) params;
2568 addr = (unsigned long)bus->regs + sd_ptr->offset;
2569 size = sd_ptr->func;
2570 brcmf_sdcard_reg_write(bus->sdh, addr, size,
2572 if (brcmf_sdcard_regfail(bus->sdh))
2577 /* Same as above, but offset is not backplane
2579 case IOV_GVAL(IOV_SBREG):
2581 struct brcmf_sdreg sdreg;
2584 memcpy(&sdreg, params, sizeof(sdreg));
2586 addr = SI_ENUM_BASE + sdreg.offset;
2588 int_val = (s32) brcmf_sdcard_reg_read(bus->sdh, addr,
2590 if (brcmf_sdcard_regfail(bus->sdh))
2592 memcpy(arg, &int_val, sizeof(s32));
2596 case IOV_SVAL(IOV_SBREG):
2598 struct brcmf_sdreg sdreg;
2601 memcpy(&sdreg, params, sizeof(sdreg));
2603 addr = SI_ENUM_BASE + sdreg.offset;
2605 brcmf_sdcard_reg_write(bus->sdh, addr, size,
2607 if (brcmf_sdcard_regfail(bus->sdh))
2612 case IOV_GVAL(IOV_SDCIS):
2616 strcat(arg, "\nFunc 0\n");
2617 brcmf_sdcard_cis_read(bus->sdh, 0x10,
2618 (u8 *) arg + strlen(arg),
2619 SBSDIO_CIS_SIZE_LIMIT);
2620 strcat(arg, "\nFunc 1\n");
2621 brcmf_sdcard_cis_read(bus->sdh, 0x11,
2622 (u8 *) arg + strlen(arg),
2623 SBSDIO_CIS_SIZE_LIMIT);
2624 strcat(arg, "\nFunc 2\n");
2625 brcmf_sdcard_cis_read(bus->sdh, 0x12,
2626 (u8 *) arg + strlen(arg),
2627 SBSDIO_CIS_SIZE_LIMIT);
2631 case IOV_GVAL(IOV_FORCEEVEN):
2632 int_val = (s32) forcealign;
2633 memcpy(arg, &int_val, val_size);
2636 case IOV_SVAL(IOV_FORCEEVEN):
2637 forcealign = bool_val;
2640 case IOV_GVAL(IOV_TXBOUND):
2641 int_val = (s32) brcmf_txbound;
2642 memcpy(arg, &int_val, val_size);
2645 case IOV_SVAL(IOV_TXBOUND):
2646 brcmf_txbound = (uint) int_val;
2649 case IOV_GVAL(IOV_RXBOUND):
2650 int_val = (s32) brcmf_rxbound;
2651 memcpy(arg, &int_val, val_size);
2654 case IOV_SVAL(IOV_RXBOUND):
2655 brcmf_rxbound = (uint) int_val;
2658 case IOV_GVAL(IOV_TXMINMAX):
2659 int_val = (s32) dhd_txminmax;
2660 memcpy(arg, &int_val, val_size);
2663 case IOV_SVAL(IOV_TXMINMAX):
2664 dhd_txminmax = (uint) int_val;
2669 case IOV_GVAL(IOV_EXTLOOP):
2670 int_val = (s32) bus->ext_loop;
2671 memcpy(arg, &int_val, val_size);
2674 case IOV_SVAL(IOV_EXTLOOP):
2675 bus->ext_loop = bool_val;
2678 case IOV_GVAL(IOV_PKTGEN):
2679 bcmerror = brcmf_sdbrcm_pktgen_get(bus, arg);
2682 case IOV_SVAL(IOV_PKTGEN):
2683 bcmerror = brcmf_sdbrcm_pktgen_set(bus, arg);
2687 case IOV_SVAL(IOV_DEVRESET):
2688 DHD_TRACE(("%s: Called set IOV_DEVRESET=%d dongle_reset=%d "
2690 __func__, bool_val, bus->dhd->dongle_reset,
2691 bus->dhd->busstate));
2693 brcmf_bus_devreset(bus->dhd, (u8) bool_val);
2697 case IOV_GVAL(IOV_DEVRESET):
2698 DHD_TRACE(("%s: Called get IOV_DEVRESET\n", __func__));
2700 /* Get its status */
2701 int_val = (bool) bus->dhd->dongle_reset;
2702 memcpy(arg, &int_val, val_size);
2707 bcmerror = -ENOTSUPP;
2712 if ((bus->idletime == DHD_IDLE_IMMEDIATE) && !bus->dpc_sched) {
2713 bus->activity = false;
2714 brcmf_sdbrcm_clkctl(bus, CLK_NONE, true);
2717 brcmf_os_sdunlock(bus->dhd);
2719 if (actionid == IOV_SVAL(IOV_DEVRESET) && bool_val == false)
2720 brcmf_c_preinit_ioctls((dhd_pub_t *) bus->dhd);
2725 static int brcmf_sdbrcm_write_vars(dhd_bus_t *bus)
2733 char *nvram_ularray;
2736 /* Even if there are no vars are to be written, we still
2737 need to set the ramsize. */
2738 varsize = bus->varsz ? roundup(bus->varsz, 4) : 0;
2739 varaddr = (bus->ramsize - 4) - varsize;
2742 vbuffer = kzalloc(varsize, GFP_ATOMIC);
2746 memcpy(vbuffer, bus->vars, bus->varsz);
2748 /* Write the vars list */
2750 brcmf_sdbrcm_membytes(bus, true, varaddr, vbuffer, varsize);
2752 /* Verify NVRAM bytes */
2753 DHD_INFO(("Compare NVRAM dl & ul; varsize=%d\n", varsize));
2754 nvram_ularray = kmalloc(varsize, GFP_ATOMIC);
2758 /* Upload image to verify downloaded contents. */
2759 memset(nvram_ularray, 0xaa, varsize);
2761 /* Read the vars list to temp buffer for comparison */
2763 brcmf_sdbrcm_membytes(bus, false, varaddr, nvram_ularray,
2766 DHD_ERROR(("%s: error %d on reading %d nvram bytes at "
2767 "0x%08x\n", __func__, bcmerror, varsize, varaddr));
2769 /* Compare the org NVRAM with the one read from RAM */
2770 if (memcmp(vbuffer, nvram_ularray, varsize)) {
2771 DHD_ERROR(("%s: Downloaded NVRAM image is corrupted.\n",
2774 DHD_ERROR(("%s: Download/Upload/Compare of NVRAM ok.\n",
2777 kfree(nvram_ularray);
2783 /* adjust to the user specified RAM */
2784 DHD_INFO(("Physical memory size: %d, usable memory size: %d\n",
2785 bus->orig_ramsize, bus->ramsize));
2786 DHD_INFO(("Vars are at %d, orig varsize is %d\n", varaddr, varsize));
2787 varsize = ((bus->orig_ramsize - 4) - varaddr);
2790 * Determine the length token:
2791 * Varsize, converted to words, in lower 16-bits, checksum
2797 varsizew = varsize / 4;
2798 varsizew = (~varsizew << 16) | (varsizew & 0x0000FFFF);
2799 varsizew = cpu_to_le32(varsizew);
2802 DHD_INFO(("New varsize is %d, length token=0x%08x\n", varsize,
2805 /* Write the length token to the last word */
2806 bcmerror = brcmf_sdbrcm_membytes(bus, true, (bus->orig_ramsize - 4),
2807 (u8 *)&varsizew, 4);
2812 static int brcmf_sdbrcm_download_state(dhd_bus_t *bus, bool enter)
2818 /* To enter download state, disable ARM and reset SOCRAM.
2819 * To exit download state, simply reset ARM (default is RAM boot).
2822 bus->alp_only = true;
2824 brcmf_sdbrcm_chip_disablecore(bus->sdh, bus->ci->armcorebase);
2826 brcmf_sdbrcm_chip_resetcore(bus->sdh, bus->ci->ramcorebase);
2828 /* Clear the top bit of memory */
2831 brcmf_sdbrcm_membytes(bus, true, bus->ramsize - 4,
2835 regdata = brcmf_sdcard_reg_read(bus->sdh,
2836 CORE_SB(bus->ci->ramcorebase, sbtmstatelow), 4);
2837 regdata &= (SBTML_RESET | SBTML_REJ_MASK |
2838 (SICF_CLOCK_EN << SBTML_SICF_SHIFT));
2839 if ((SICF_CLOCK_EN << SBTML_SICF_SHIFT) != regdata) {
2840 DHD_ERROR(("%s: SOCRAM core is down after reset?\n",
2846 bcmerror = brcmf_sdbrcm_write_vars(bus);
2848 DHD_ERROR(("%s: no vars written to RAM\n", __func__));
2852 W_SDREG(0xFFFFFFFF, &bus->regs->intstatus, retries);
2854 brcmf_sdbrcm_chip_resetcore(bus->sdh, bus->ci->armcorebase);
2856 /* Allow HT Clock now that the ARM is running. */
2857 bus->alp_only = false;
2859 bus->dhd->busstate = DHD_BUS_LOAD;
2866 brcmf_sdbrcm_bus_iovar_op(dhd_pub_t *dhdp, const char *name,
2867 void *params, int plen, void *arg, int len, bool set)
2869 dhd_bus_t *bus = dhdp->bus;
2870 const struct brcmu_iovar *vi = NULL;
2875 DHD_TRACE(("%s: Enter\n", __func__));
2880 /* Get MUST have return space */
2881 ASSERT(set || (arg && len));
2883 /* Set does NOT take qualifiers */
2884 ASSERT(!set || (!params && !plen));
2886 /* Look up var locally; if not found pass to host driver */
2887 vi = brcmu_iovar_lookup(dhdsdio_iovars, name);
2889 brcmf_os_sdlock(bus->dhd);
2893 /* Turn on clock in case SD command needs backplane */
2894 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
2896 bcmerror = brcmf_sdcard_iovar_op(bus->sdh, name, params, plen,
2899 /* Similar check for blocksize change */
2900 if (set && strcmp(name, "sd_blocksize") == 0) {
2902 if (brcmf_sdcard_iovar_op
2903 (bus->sdh, "sd_blocksize", &fnum, sizeof(s32),
2904 &bus->blocksize, sizeof(s32),
2907 DHD_ERROR(("%s: fail on %s get\n", __func__,
2910 DHD_INFO(("%s: noted %s update, value now %d\n",
2911 __func__, "sd_blocksize",
2915 bus->roundup = min(max_roundup, bus->blocksize);
2917 if ((bus->idletime == DHD_IDLE_IMMEDIATE) && !bus->dpc_sched) {
2918 bus->activity = false;
2919 brcmf_sdbrcm_clkctl(bus, CLK_NONE, true);
2922 brcmf_os_sdunlock(bus->dhd);
2926 DHD_CTL(("%s: %s %s, len %d plen %d\n", __func__,
2927 name, (set ? "set" : "get"), len, plen));
2929 /* set up 'params' pointer in case this is a set command so that
2930 * the convenience int and bool code can be common to set and get
2932 if (params == NULL) {
2937 if (vi->type == IOVT_VOID)
2939 else if (vi->type == IOVT_BUFFER)
2942 /* all other types are integer sized */
2943 val_size = sizeof(int);
2945 actionid = set ? IOV_SVAL(vi->varid) : IOV_GVAL(vi->varid);
2946 bcmerror = brcmf_sdbrcm_doiovar(bus, vi, actionid, name, params, plen,
2947 arg, len, val_size);
2953 void brcmf_sdbrcm_bus_stop(struct dhd_bus *bus, bool enforce_mutex)
2955 u32 local_hostintmask;
2960 DHD_TRACE(("%s: Enter\n", __func__));
2963 brcmf_os_sdlock(bus->dhd);
2967 /* Enable clock for device interrupts */
2968 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
2970 /* Disable and clear interrupts at the chip level also */
2971 W_SDREG(0, &bus->regs->hostintmask, retries);
2972 local_hostintmask = bus->hostintmask;
2973 bus->hostintmask = 0;
2975 /* Change our idea of bus state */
2976 bus->dhd->busstate = DHD_BUS_DOWN;
2978 /* Force clocks on backplane to be sure F2 interrupt propagates */
2979 saveclk = brcmf_sdcard_cfg_read(bus->sdh, SDIO_FUNC_1,
2980 SBSDIO_FUNC1_CHIPCLKCSR, &err);
2982 brcmf_sdcard_cfg_write(bus->sdh, SDIO_FUNC_1,
2983 SBSDIO_FUNC1_CHIPCLKCSR,
2984 (saveclk | SBSDIO_FORCE_HT), &err);
2987 DHD_ERROR(("%s: Failed to force clock for F2: err %d\n",
2991 /* Turn off the bus (F2), free any pending packets */
2992 DHD_INTR(("%s: disable SDIO interrupts\n", __func__));
2993 brcmf_sdcard_intr_disable(bus->sdh);
2994 brcmf_sdcard_cfg_write(bus->sdh, SDIO_FUNC_0, SDIO_CCCR_IOEx,
2995 SDIO_FUNC_ENABLE_1, NULL);
2997 /* Clear any pending interrupts now that F2 is disabled */
2998 W_SDREG(local_hostintmask, &bus->regs->intstatus, retries);
3000 /* Turn off the backplane clock (only) */
3001 brcmf_sdbrcm_clkctl(bus, CLK_SDONLY, false);
3003 /* Clear the data packet queues */
3004 brcmu_pktq_flush(&bus->txq, true, NULL, NULL);
3006 /* Clear any held glomming stuff */
3008 brcmu_pkt_buf_free_skb(bus->glomd);
3011 brcmu_pkt_buf_free_skb(bus->glom);
3013 bus->glom = bus->glomd = NULL;
3015 /* Clear rx control and wake any waiters */
3017 brcmf_os_ioctl_resp_wake(bus->dhd);
3019 /* Reset some F2 state stuff */
3020 bus->rxskip = false;
3021 bus->tx_seq = bus->rx_seq = 0;
3024 brcmf_os_sdunlock(bus->dhd);
3027 int brcmf_sdbrcm_bus_init(dhd_pub_t *dhdp, bool enforce_mutex)
3029 dhd_bus_t *bus = dhdp->bus;
3036 DHD_TRACE(("%s: Enter\n", __func__));
3043 brcmf_os_sdlock(bus->dhd);
3045 /* Make sure backplane clock is on, needed to generate F2 interrupt */
3046 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
3047 if (bus->clkstate != CLK_AVAIL)
3050 /* Force clocks on backplane to be sure F2 interrupt propagates */
3052 brcmf_sdcard_cfg_read(bus->sdh, SDIO_FUNC_1,
3053 SBSDIO_FUNC1_CHIPCLKCSR, &err);
3055 brcmf_sdcard_cfg_write(bus->sdh, SDIO_FUNC_1,
3056 SBSDIO_FUNC1_CHIPCLKCSR,
3057 (saveclk | SBSDIO_FORCE_HT), &err);
3060 DHD_ERROR(("%s: Failed to force clock for F2: err %d\n",
3065 /* Enable function 2 (frame transfers) */
3066 W_SDREG((SDPCM_PROT_VERSION << SMB_DATA_VERSION_SHIFT),
3067 &bus->regs->tosbmailboxdata, retries);
3068 enable = (SDIO_FUNC_ENABLE_1 | SDIO_FUNC_ENABLE_2);
3070 brcmf_sdcard_cfg_write(bus->sdh, SDIO_FUNC_0, SDIO_CCCR_IOEx, enable,
3073 /* Give the dongle some time to do its thing and set IOR2 */
3074 brcmf_timeout_start(&tmo, DHD_WAIT_F2RDY * 1000);
3077 while (ready != enable && !brcmf_timeout_expired(&tmo))
3079 brcmf_sdcard_cfg_read(bus->sdh, SDIO_FUNC_0, SDIO_CCCR_IORx,
3082 DHD_INFO(("%s: enable 0x%02x, ready 0x%02x (waited %uus)\n",
3083 __func__, enable, ready, tmo.elapsed));
3085 /* If F2 successfully enabled, set core and enable interrupts */
3086 if (ready == enable) {
3087 /* Set up the interrupt mask and enable interrupts */
3088 bus->hostintmask = HOSTINTMASK;
3089 W_SDREG(bus->hostintmask,
3090 (unsigned int *)CORE_BUS_REG(bus->ci->buscorebase,
3091 hostintmask), retries);
3093 brcmf_sdcard_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_WATERMARK,
3094 (u8) watermark, &err);
3096 /* Set bus state according to enable result */
3097 dhdp->busstate = DHD_BUS_DATA;
3099 /* bcmsdh_intr_unmask(bus->sdh); */
3101 bus->intdis = false;
3103 DHD_INTR(("%s: enable SDIO device interrupts\n",
3105 brcmf_sdcard_intr_enable(bus->sdh);
3107 DHD_INTR(("%s: disable SDIO interrupts\n", __func__));
3108 brcmf_sdcard_intr_disable(bus->sdh);
3114 /* Disable F2 again */
3115 enable = SDIO_FUNC_ENABLE_1;
3116 brcmf_sdcard_cfg_write(bus->sdh, SDIO_FUNC_0, SDIO_CCCR_IOEx,
3120 /* Restore previous clock setting */
3121 brcmf_sdcard_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
3124 /* If we didn't come up, turn off backplane clock */
3125 if (dhdp->busstate != DHD_BUS_DATA)
3126 brcmf_sdbrcm_clkctl(bus, CLK_NONE, false);
3130 brcmf_os_sdunlock(bus->dhd);
3135 static void brcmf_sdbrcm_rxfail(dhd_bus_t *bus, bool abort, bool rtx)
3137 struct brcmf_sdio *sdh = bus->sdh;
3138 struct sdpcmd_regs *regs = bus->regs;
3144 DHD_ERROR(("%s: %sterminate frame%s\n", __func__,
3145 (abort ? "abort command, " : ""),
3146 (rtx ? ", send NAK" : "")));
3149 brcmf_sdcard_abort(sdh, SDIO_FUNC_2);
3151 brcmf_sdcard_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_FRAMECTRL,
3155 /* Wait until the packet has been flushed (device/FIFO stable) */
3156 for (lastrbc = retries = 0xffff; retries > 0; retries--) {
3157 hi = brcmf_sdcard_cfg_read(sdh, SDIO_FUNC_1,
3158 SBSDIO_FUNC1_RFRAMEBCHI, NULL);
3159 lo = brcmf_sdcard_cfg_read(sdh, SDIO_FUNC_1,
3160 SBSDIO_FUNC1_RFRAMEBCLO, NULL);
3161 bus->f1regdata += 2;
3163 if ((hi == 0) && (lo == 0))
3166 if ((hi > (lastrbc >> 8)) && (lo > (lastrbc & 0x00ff))) {
3167 DHD_ERROR(("%s: count growing: last 0x%04x now "
3169 __func__, lastrbc, ((hi << 8) + lo)));
3171 lastrbc = (hi << 8) + lo;
3175 DHD_ERROR(("%s: count never zeroed: last 0x%04x\n",
3176 __func__, lastrbc));
3178 DHD_INFO(("%s: flush took %d iterations\n", __func__,
3179 (0xffff - retries)));
3184 W_SDREG(SMB_NAK, ®s->tosbmailbox, retries);
3186 if (retries <= retry_limit)
3190 /* Clear partial in any case */
3193 /* If we can't reach the device, signal failure */
3194 if (err || brcmf_sdcard_regfail(sdh))
3195 bus->dhd->busstate = DHD_BUS_DOWN;
3199 brcmf_sdbrcm_read_control(dhd_bus_t *bus, u8 *hdr, uint len, uint doff)
3201 struct brcmf_sdio *sdh = bus->sdh;
3206 DHD_TRACE(("%s: Enter\n", __func__));
3208 /* Control data already received in aligned rxctl */
3209 if ((bus->bus == SPI_BUS) && (!bus->usebufpool))
3213 /* Set rxctl for frame (w/optional alignment) */
3214 bus->rxctl = bus->rxbuf;
3216 bus->rxctl += firstread;
3217 pad = ((unsigned long)bus->rxctl % BRCMF_SDALIGN);
3219 bus->rxctl += (BRCMF_SDALIGN - pad);
3220 bus->rxctl -= firstread;
3222 ASSERT(bus->rxctl >= bus->rxbuf);
3224 /* Copy the already-read portion over */
3225 memcpy(bus->rxctl, hdr, firstread);
3226 if (len <= firstread)
3229 /* Copy the full data pkt in gSPI case and process ioctl. */
3230 if (bus->bus == SPI_BUS) {
3231 memcpy(bus->rxctl, hdr, len);
3235 /* Raise rdlen to next SDIO block to avoid tail command */
3236 rdlen = len - firstread;
3237 if (bus->roundup && bus->blocksize && (rdlen > bus->blocksize)) {
3238 pad = bus->blocksize - (rdlen % bus->blocksize);
3239 if ((pad <= bus->roundup) && (pad < bus->blocksize) &&
3240 ((len + pad) < bus->dhd->maxctl))
3242 } else if (rdlen % BRCMF_SDALIGN) {
3243 rdlen += BRCMF_SDALIGN - (rdlen % BRCMF_SDALIGN);
3246 /* Satisfy length-alignment requirements */
3247 if (forcealign && (rdlen & (ALIGNMENT - 1)))
3248 rdlen = roundup(rdlen, ALIGNMENT);
3250 /* Drop if the read is too big or it exceeds our maximum */
3251 if ((rdlen + firstread) > bus->dhd->maxctl) {
3252 DHD_ERROR(("%s: %d-byte control read exceeds %d-byte buffer\n",
3253 __func__, rdlen, bus->dhd->maxctl));
3254 bus->dhd->rx_errors++;
3255 brcmf_sdbrcm_rxfail(bus, false, false);
3259 if ((len - doff) > bus->dhd->maxctl) {
3260 DHD_ERROR(("%s: %d-byte ctl frame (%d-byte ctl data) exceeds "
3262 __func__, len, (len - doff), bus->dhd->maxctl));
3263 bus->dhd->rx_errors++;
3265 brcmf_sdbrcm_rxfail(bus, false, false);
3269 /* Read remainder of frame body into the rxctl buffer */
3270 sdret = brcmf_sdcard_recv_buf(sdh, brcmf_sdcard_cur_sbwad(sdh),
3272 F2SYNC, (bus->rxctl + firstread), rdlen,
3275 ASSERT(sdret != -BCME_PENDING);
3277 /* Control frame failures need retransmission */
3279 DHD_ERROR(("%s: read %d control bytes failed: %d\n",
3280 __func__, rdlen, sdret));
3281 bus->rxc_errors++; /* dhd.rx_ctlerrs is higher level */
3282 brcmf_sdbrcm_rxfail(bus, true, true);
3289 if (DHD_BYTES_ON() && DHD_CTL_ON()) {
3290 printk(KERN_DEBUG "RxCtrl:\n");
3291 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, bus->rxctl, len);
3295 /* Point to valid data and indicate its length */
3297 bus->rxlen = len - doff;
3300 /* Awake any waiters */
3301 brcmf_os_ioctl_resp_wake(bus->dhd);
3304 static u8 brcmf_sdbrcm_rxglom(dhd_bus_t *bus, u8 rxseq)
3310 struct sk_buff *pfirst, *plast, *pnext, *save_pfirst;
3313 u8 chan, seq, doff, sfdoff;
3317 bool usechain = bus->use_rxchain;
3319 /* If packets, issue read(s) and send up packet chain */
3320 /* Return sequence numbers consumed? */
3322 DHD_TRACE(("brcmf_sdbrcm_rxglom: start: glomd %p glom %p\n", bus->glomd,
3325 /* If there's a descriptor, generate the packet chain */
3327 pfirst = plast = pnext = NULL;
3328 dlen = (u16) (bus->glomd->len);
3329 dptr = bus->glomd->data;
3330 if (!dlen || (dlen & 1)) {
3331 DHD_ERROR(("%s: bad glomd len(%d), ignore descriptor\n",
3336 for (totlen = num = 0; dlen; num++) {
3337 /* Get (and move past) next length */
3338 sublen = get_unaligned_le16(dptr);
3339 dlen -= sizeof(u16);
3340 dptr += sizeof(u16);
3341 if ((sublen < SDPCM_HDRLEN) ||
3342 ((num == 0) && (sublen < (2 * SDPCM_HDRLEN)))) {
3343 DHD_ERROR(("%s: descriptor len %d bad: %d\n",
3344 __func__, num, sublen));
3348 if (sublen % BRCMF_SDALIGN) {
3349 DHD_ERROR(("%s: sublen %d not multiple of %d\n",
3350 __func__, sublen, BRCMF_SDALIGN));
3355 /* For last frame, adjust read len so total
3356 is a block multiple */
3359 (roundup(totlen, bus->blocksize) - totlen);
3360 totlen = roundup(totlen, bus->blocksize);
3363 /* Allocate/chain packet for next subframe */
3364 pnext = brcmu_pkt_buf_get_skb(sublen + BRCMF_SDALIGN);
3365 if (pnext == NULL) {
3366 DHD_ERROR(("%s: bcm_pkt_buf_get_skb failed, "
3367 "num %d len %d\n", __func__,
3371 ASSERT(!(pnext->prev));
3374 pfirst = plast = pnext;
3377 plast->next = pnext;
3381 /* Adhere to start alignment requirements */
3382 PKTALIGN(pnext, sublen, BRCMF_SDALIGN);
3385 /* If all allocations succeeded, save packet chain
3388 DHD_GLOM(("%s: allocated %d-byte packet chain for %d "
3389 "subframes\n", __func__, totlen, num));
3390 if (DHD_GLOM_ON() && bus->nextlen) {
3391 if (totlen != bus->nextlen) {
3392 DHD_GLOM(("%s: glomdesc mismatch: nextlen %d glomdesc %d " "rxseq %d\n",
3393 __func__, bus->nextlen,
3398 pfirst = pnext = NULL;
3401 brcmu_pkt_buf_free_skb(pfirst);
3406 /* Done with descriptor packet */
3407 brcmu_pkt_buf_free_skb(bus->glomd);
3412 /* Ok -- either we just generated a packet chain,
3413 or had one from before */
3415 if (DHD_GLOM_ON()) {
3416 DHD_GLOM(("%s: try superframe read, packet chain:\n",
3418 for (pnext = bus->glom; pnext; pnext = pnext->next) {
3419 DHD_GLOM((" %p: %p len 0x%04x (%d)\n",
3420 pnext, (u8 *) (pnext->data),
3421 pnext->len, pnext->len));
3426 dlen = (u16) brcmu_pkttotlen(pfirst);
3428 /* Do an SDIO read for the superframe. Configurable iovar to
3429 * read directly into the chained packet, or allocate a large
3430 * packet and and copy into the chain.
3433 errcode = brcmf_sdcard_recv_buf(bus->sdh,
3434 brcmf_sdcard_cur_sbwad(bus->sdh),
3436 F2SYNC, (u8 *) pfirst->data, dlen,
3437 pfirst, NULL, NULL);
3438 } else if (bus->dataptr) {
3439 errcode = brcmf_sdcard_recv_buf(bus->sdh,
3440 brcmf_sdcard_cur_sbwad(bus->sdh),
3442 F2SYNC, bus->dataptr, dlen,
3444 sublen = (u16) brcmu_pktfrombuf(pfirst, 0, dlen,
3446 if (sublen != dlen) {
3447 DHD_ERROR(("%s: FAILED TO COPY, dlen %d sublen %d\n",
3448 __func__, dlen, sublen));
3453 DHD_ERROR(("COULDN'T ALLOC %d-BYTE GLOM, FORCE FAILURE\n",
3458 ASSERT(errcode != -BCME_PENDING);
3460 /* On failure, kill the superframe, allow a couple retries */
3462 DHD_ERROR(("%s: glom read of %d bytes failed: %d\n",
3463 __func__, dlen, errcode));
3464 bus->dhd->rx_errors++;
3466 if (bus->glomerr++ < 3) {
3467 brcmf_sdbrcm_rxfail(bus, true, true);
3470 brcmf_sdbrcm_rxfail(bus, true, false);
3471 brcmu_pkt_buf_free_skb(bus->glom);
3478 if (DHD_GLOM_ON()) {
3479 printk(KERN_DEBUG "SUPERFRAME:\n");
3480 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
3481 pfirst->data, min_t(int, pfirst->len, 48));
3485 /* Validate the superframe header */
3486 dptr = (u8 *) (pfirst->data);
3487 sublen = get_unaligned_le16(dptr);
3488 check = get_unaligned_le16(dptr + sizeof(u16));
3490 chan = SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]);
3491 seq = SDPCM_PACKET_SEQUENCE(&dptr[SDPCM_FRAMETAG_LEN]);
3492 bus->nextlen = dptr[SDPCM_FRAMETAG_LEN + SDPCM_NEXTLEN_OFFSET];
3493 if ((bus->nextlen << 4) > MAX_RX_DATASZ) {
3494 DHD_INFO(("%s: nextlen too large (%d) seq %d\n",
3495 __func__, bus->nextlen, seq));
3498 doff = SDPCM_DOFFSET_VALUE(&dptr[SDPCM_FRAMETAG_LEN]);
3499 txmax = SDPCM_WINDOW_VALUE(&dptr[SDPCM_FRAMETAG_LEN]);
3502 if ((u16)~(sublen ^ check)) {
3503 DHD_ERROR(("%s (superframe): HW hdr error: len/check "
3504 "0x%04x/0x%04x\n", __func__, sublen, check));
3506 } else if (roundup(sublen, bus->blocksize) != dlen) {
3507 DHD_ERROR(("%s (superframe): len 0x%04x, rounded "
3508 "0x%04x, expect 0x%04x\n",
3510 roundup(sublen, bus->blocksize), dlen));
3512 } else if (SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]) !=
3513 SDPCM_GLOM_CHANNEL) {
3514 DHD_ERROR(("%s (superframe): bad channel %d\n",
3516 SDPCM_PACKET_CHANNEL(&dptr
3517 [SDPCM_FRAMETAG_LEN])));
3519 } else if (SDPCM_GLOMDESC(&dptr[SDPCM_FRAMETAG_LEN])) {
3520 DHD_ERROR(("%s (superframe): got second descriptor?\n",
3523 } else if ((doff < SDPCM_HDRLEN) ||
3524 (doff > (pfirst->len - SDPCM_HDRLEN))) {
3525 DHD_ERROR(("%s (superframe): Bad data offset %d: HW %d "
3527 __func__, doff, sublen,
3528 pfirst->len, SDPCM_HDRLEN));
3532 /* Check sequence number of superframe SW header */
3534 DHD_INFO(("%s: (superframe) rx_seq %d, expected %d\n",
3535 __func__, seq, rxseq));
3540 /* Check window for sanity */
3541 if ((u8) (txmax - bus->tx_seq) > 0x40) {
3542 DHD_ERROR(("%s: unlikely tx max %d with tx_seq %d\n",
3543 __func__, txmax, bus->tx_seq));
3544 txmax = bus->tx_seq + 2;
3546 bus->tx_max = txmax;
3548 /* Remove superframe header, remember offset */
3549 skb_pull(pfirst, doff);
3552 /* Validate all the subframe headers */
3553 for (num = 0, pnext = pfirst; pnext && !errcode;
3554 num++, pnext = pnext->next) {
3555 dptr = (u8 *) (pnext->data);
3556 dlen = (u16) (pnext->len);
3557 sublen = get_unaligned_le16(dptr);
3558 check = get_unaligned_le16(dptr + sizeof(u16));
3559 chan = SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]);
3560 doff = SDPCM_DOFFSET_VALUE(&dptr[SDPCM_FRAMETAG_LEN]);
3562 if (DHD_GLOM_ON()) {
3563 printk(KERN_DEBUG "subframe:\n");
3564 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
3569 if ((u16)~(sublen ^ check)) {
3570 DHD_ERROR(("%s (subframe %d): HW hdr error: "
3571 "len/check 0x%04x/0x%04x\n",
3572 __func__, num, sublen, check));
3574 } else if ((sublen > dlen) || (sublen < SDPCM_HDRLEN)) {
3575 DHD_ERROR(("%s (subframe %d): length mismatch: "
3576 "len 0x%04x, expect 0x%04x\n",
3577 __func__, num, sublen, dlen));
3579 } else if ((chan != SDPCM_DATA_CHANNEL) &&
3580 (chan != SDPCM_EVENT_CHANNEL)) {
3581 DHD_ERROR(("%s (subframe %d): bad channel %d\n",
3582 __func__, num, chan));
3584 } else if ((doff < SDPCM_HDRLEN) || (doff > sublen)) {
3585 DHD_ERROR(("%s (subframe %d): Bad data offset %d: HW %d min %d\n",
3586 __func__, num, doff, sublen,
3593 /* Terminate frame on error, request
3595 if (bus->glomerr++ < 3) {
3596 /* Restore superframe header space */
3597 skb_push(pfirst, sfdoff);
3598 brcmf_sdbrcm_rxfail(bus, true, true);
3601 brcmf_sdbrcm_rxfail(bus, true, false);
3602 brcmu_pkt_buf_free_skb(bus->glom);
3610 /* Basic SD framing looks ok - process each packet (header) */
3611 save_pfirst = pfirst;
3615 for (num = 0; pfirst; rxseq++, pfirst = pnext) {
3616 pnext = pfirst->next;
3617 pfirst->next = NULL;
3619 dptr = (u8 *) (pfirst->data);
3620 sublen = get_unaligned_le16(dptr);
3621 chan = SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]);
3622 seq = SDPCM_PACKET_SEQUENCE(&dptr[SDPCM_FRAMETAG_LEN]);
3623 doff = SDPCM_DOFFSET_VALUE(&dptr[SDPCM_FRAMETAG_LEN]);
3625 DHD_GLOM(("%s: Get subframe %d, %p(%p/%d), sublen %d "
3627 __func__, num, pfirst, pfirst->data,
3628 pfirst->len, sublen, chan, seq));
3630 ASSERT((chan == SDPCM_DATA_CHANNEL)
3631 || (chan == SDPCM_EVENT_CHANNEL));
3634 DHD_GLOM(("%s: rx_seq %d, expected %d\n",
3635 __func__, seq, rxseq));
3640 if (DHD_BYTES_ON() && DHD_DATA_ON()) {
3641 printk(KERN_DEBUG "Rx Subframe Data:\n");
3642 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
3647 __skb_trim(pfirst, sublen);
3648 skb_pull(pfirst, doff);
3650 if (pfirst->len == 0) {
3651 brcmu_pkt_buf_free_skb(pfirst);
3653 plast->next = pnext;
3655 ASSERT(save_pfirst == pfirst);
3656 save_pfirst = pnext;
3659 } else if (brcmf_proto_hdrpull(bus->dhd, &ifidx, pfirst)
3661 DHD_ERROR(("%s: rx protocol error\n",
3663 bus->dhd->rx_errors++;
3664 brcmu_pkt_buf_free_skb(pfirst);
3666 plast->next = pnext;
3668 ASSERT(save_pfirst == pfirst);
3669 save_pfirst = pnext;
3674 /* this packet will go up, link back into
3675 chain and count it */
3676 pfirst->next = pnext;
3681 if (DHD_GLOM_ON()) {
3682 DHD_GLOM(("%s subframe %d to stack, %p(%p/%d) "
3684 __func__, num, pfirst, pfirst->data,
3685 pfirst->len, pfirst->next,
3687 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
3689 min_t(int, pfirst->len, 32));
3694 brcmf_os_sdunlock(bus->dhd);
3695 brcmf_rx_frame(bus->dhd, ifidx, save_pfirst, num);
3696 brcmf_os_sdlock(bus->dhd);
3699 bus->rxglomframes++;
3700 bus->rxglompkts += num;
3705 /* Return true if there may be more frames to read */
3707 brcmf_sdbrcm_readframes(dhd_bus_t *bus, uint maxframes, bool *finished)
3709 struct brcmf_sdio *sdh = bus->sdh;
3711 u16 len, check; /* Extracted hardware header fields */
3712 u8 chan, seq, doff; /* Extracted software header fields */
3713 u8 fcbits; /* Extracted fcbits from software header */
3715 struct sk_buff *pkt; /* Packet for event or data frames */
3716 u16 pad; /* Number of pad bytes to read */
3717 u16 rdlen; /* Total number of bytes to read */
3718 u8 rxseq; /* Next sequence number to expect */
3719 uint rxleft = 0; /* Remaining number of frames allowed */
3720 int sdret; /* Return code from bcmsdh calls */
3721 u8 txmax; /* Maximum tx sequence offered */
3722 bool len_consistent; /* Result of comparing readahead len and
3726 uint rxcount = 0; /* Total frames read */
3728 #if defined(BCMDBG) || defined(SDTEST)
3729 bool sdtest = false; /* To limit message spew from test mode */
3732 DHD_TRACE(("%s: Enter\n", __func__));
3737 /* Allow pktgen to override maxframes */
3738 if (bus->pktgen_count && (bus->pktgen_mode == DHD_PKTGEN_RECV)) {
3739 maxframes = bus->pktgen_count;
3744 /* Not finished unless we encounter no more frames indication */
3747 for (rxseq = bus->rx_seq, rxleft = maxframes;
3748 !bus->rxskip && rxleft && bus->dhd->busstate != DHD_BUS_DOWN;
3749 rxseq++, rxleft--) {
3751 /* Handle glomming separately */
3752 if (bus->glom || bus->glomd) {
3754 DHD_GLOM(("%s: calling rxglom: glomd %p, glom %p\n",
3755 __func__, bus->glomd, bus->glom));
3756 cnt = brcmf_sdbrcm_rxglom(bus, rxseq);
3757 DHD_GLOM(("%s: rxglom returned %d\n", __func__, cnt));
3759 rxleft = (rxleft > cnt) ? (rxleft - cnt) : 1;
3763 /* Try doing single read if we can */
3764 if (dhd_readahead && bus->nextlen) {
3765 u16 nextlen = bus->nextlen;
3768 if (bus->bus == SPI_BUS) {
3769 rdlen = len = nextlen;
3771 rdlen = len = nextlen << 4;
3773 /* Pad read to blocksize for efficiency */
3774 if (bus->roundup && bus->blocksize
3775 && (rdlen > bus->blocksize)) {
3778 (rdlen % bus->blocksize);
3779 if ((pad <= bus->roundup)
3780 && (pad < bus->blocksize)
3781 && ((rdlen + pad + firstread) <
3784 } else if (rdlen % BRCMF_SDALIGN) {
3786 BRCMF_SDALIGN - (rdlen % BRCMF_SDALIGN);
3790 /* We use bus->rxctl buffer in WinXP for initial
3791 * control pkt receives.
3792 * Later we use buffer-poll for data as well
3793 * as control packets.
3794 * This is required because dhd receives full
3795 * frame in gSPI unlike SDIO.
3796 * After the frame is received we have to
3797 * distinguish whether it is data
3798 * or non-data frame.
3800 /* Allocate a packet buffer */
3801 pkt = brcmu_pkt_buf_get_skb(rdlen + BRCMF_SDALIGN);
3803 if (bus->bus == SPI_BUS) {
3804 bus->usebufpool = false;
3805 bus->rxctl = bus->rxbuf;
3807 bus->rxctl += firstread;
3808 pad = ((unsigned long)bus->rxctl %
3812 (BRCMF_SDALIGN - pad);
3813 bus->rxctl -= firstread;
3815 ASSERT(bus->rxctl >= bus->rxbuf);
3817 /* Read the entire frame */
3818 sdret = brcmf_sdcard_recv_buf(sdh,
3819 brcmf_sdcard_cur_sbwad(sdh),
3820 SDIO_FUNC_2, F2SYNC,
3824 ASSERT(sdret != -BCME_PENDING);
3826 /* Control frame failures need
3829 DHD_ERROR(("%s: read %d control bytes failed: %d\n",
3832 /* dhd.rx_ctlerrs is higher */
3834 brcmf_sdbrcm_rxfail(bus, true,
3842 request rtx of events */
3843 DHD_ERROR(("%s (nextlen): "
3844 "brcmu_pkt_buf_get_skb "
3846 " len %d rdlen %d expected"
3847 " rxseq %d\n", __func__,
3848 len, rdlen, rxseq));
3852 if (bus->bus == SPI_BUS)
3853 bus->usebufpool = true;
3855 ASSERT(!(pkt->prev));
3856 PKTALIGN(pkt, rdlen, BRCMF_SDALIGN);
3857 rxbuf = (u8 *) (pkt->data);
3858 /* Read the entire frame */
3859 sdret = brcmf_sdcard_recv_buf(sdh,
3860 brcmf_sdcard_cur_sbwad(sdh),
3861 SDIO_FUNC_2, F2SYNC,
3865 ASSERT(sdret != -BCME_PENDING);
3868 DHD_ERROR(("%s (nextlen): read %d bytes failed: %d\n",
3869 __func__, rdlen, sdret));
3870 brcmu_pkt_buf_free_skb(pkt);
3871 bus->dhd->rx_errors++;
3872 /* Force retry w/normal header read.
3873 * Don't attempt NAK for
3876 brcmf_sdbrcm_rxfail(bus, true,
3884 /* Now check the header */
3885 memcpy(bus->rxhdr, rxbuf, SDPCM_HDRLEN);
3887 /* Extract hardware header fields */
3888 len = get_unaligned_le16(bus->rxhdr);
3889 check = get_unaligned_le16(bus->rxhdr + sizeof(u16));
3891 /* All zeros means readahead info was bad */
3892 if (!(len | check)) {
3893 DHD_INFO(("%s (nextlen): read zeros in HW "
3894 "header???\n", __func__));
3895 brcmf_sdbrcm_pktfree2(bus, pkt);
3899 /* Validate check bytes */
3900 if ((u16)~(len ^ check)) {
3901 DHD_ERROR(("%s (nextlen): HW hdr error:"
3902 " nextlen/len/check"
3903 " 0x%04x/0x%04x/0x%04x\n",
3904 __func__, nextlen, len, check));
3906 brcmf_sdbrcm_rxfail(bus, false, false);
3907 brcmf_sdbrcm_pktfree2(bus, pkt);
3911 /* Validate frame length */
3912 if (len < SDPCM_HDRLEN) {
3913 DHD_ERROR(("%s (nextlen): HW hdr length "
3914 "invalid: %d\n", __func__, len));
3915 brcmf_sdbrcm_pktfree2(bus, pkt);
3919 /* Check for consistency withreadahead info */
3920 len_consistent = (nextlen != (roundup(len, 16) >> 4));
3921 if (len_consistent) {
3922 /* Mismatch, force retry w/normal
3923 header (may be >4K) */
3924 DHD_ERROR(("%s (nextlen): mismatch, "
3925 "nextlen %d len %d rnd %d; "
3926 "expected rxseq %d\n",
3928 len, roundup(len, 16), rxseq));
3929 brcmf_sdbrcm_rxfail(bus, true,
3930 bus->bus != SPI_BUS);
3931 brcmf_sdbrcm_pktfree2(bus, pkt);
3935 /* Extract software header fields */
3936 chan = SDPCM_PACKET_CHANNEL(
3937 &bus->rxhdr[SDPCM_FRAMETAG_LEN]);
3938 seq = SDPCM_PACKET_SEQUENCE(
3939 &bus->rxhdr[SDPCM_FRAMETAG_LEN]);
3940 doff = SDPCM_DOFFSET_VALUE(
3941 &bus->rxhdr[SDPCM_FRAMETAG_LEN]);
3942 txmax = SDPCM_WINDOW_VALUE(
3943 &bus->rxhdr[SDPCM_FRAMETAG_LEN]);
3946 bus->rxhdr[SDPCM_FRAMETAG_LEN +
3947 SDPCM_NEXTLEN_OFFSET];
3948 if ((bus->nextlen << 4) > MAX_RX_DATASZ) {
3949 DHD_INFO(("%s (nextlen): got frame w/nextlen too large" " (%d), seq %d\n",
3950 __func__, bus->nextlen, seq));
3954 bus->dhd->rx_readahead_cnt++;
3956 /* Handle Flow Control */
3957 fcbits = SDPCM_FCMASK_VALUE(
3958 &bus->rxhdr[SDPCM_FRAMETAG_LEN]);
3960 if (bus->flowcontrol != fcbits) {
3961 if (~bus->flowcontrol & fcbits)
3964 if (bus->flowcontrol & ~fcbits)
3968 bus->flowcontrol = fcbits;
3971 /* Check and update sequence number */
3973 DHD_INFO(("%s (nextlen): rx_seq %d, expected "
3974 "%d\n", __func__, seq, rxseq));
3979 /* Check window for sanity */
3980 if ((u8) (txmax - bus->tx_seq) > 0x40) {
3981 DHD_ERROR(("%s: got unlikely tx max %d with "
3983 __func__, txmax, bus->tx_seq));
3984 txmax = bus->tx_seq + 2;
3986 bus->tx_max = txmax;
3989 if (DHD_BYTES_ON() && DHD_DATA_ON()) {
3990 printk(KERN_DEBUG "Rx Data:\n");
3991 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
3993 } else if (DHD_HDRS_ON()) {
3994 printk(KERN_DEBUG "RxHdr:\n");
3995 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
3996 bus->rxhdr, SDPCM_HDRLEN);
4000 if (chan == SDPCM_CONTROL_CHANNEL) {
4001 if (bus->bus == SPI_BUS) {
4002 brcmf_sdbrcm_read_control(bus, rxbuf,
4005 DHD_ERROR(("%s (nextlen): readahead on control" " packet %d?\n",
4007 /* Force retry w/normal header read */
4009 brcmf_sdbrcm_rxfail(bus, false, true);
4011 brcmf_sdbrcm_pktfree2(bus, pkt);
4015 if ((bus->bus == SPI_BUS) && !bus->usebufpool) {
4016 DHD_ERROR(("Received %d bytes on %d channel. Running out of " "rx pktbuf's or not yet malloced.\n",
4021 /* Validate data offset */
4022 if ((doff < SDPCM_HDRLEN) || (doff > len)) {
4023 DHD_ERROR(("%s (nextlen): bad data offset %d: HW len %d min %d\n",
4024 __func__, doff, len, SDPCM_HDRLEN));
4025 brcmf_sdbrcm_rxfail(bus, false, false);
4026 brcmf_sdbrcm_pktfree2(bus, pkt);
4030 /* All done with this one -- now deliver the packet */
4033 /* gSPI frames should not be handled in fractions */
4034 if (bus->bus == SPI_BUS)
4037 /* Read frame header (hardware and software) */
4038 sdret = brcmf_sdcard_recv_buf(sdh, brcmf_sdcard_cur_sbwad(sdh),
4039 SDIO_FUNC_2, F2SYNC, bus->rxhdr, firstread,
4042 ASSERT(sdret != -BCME_PENDING);
4045 DHD_ERROR(("%s: RXHEADER FAILED: %d\n", __func__,
4048 brcmf_sdbrcm_rxfail(bus, true, true);
4052 if (DHD_BYTES_ON() || DHD_HDRS_ON()) {
4053 printk(KERN_DEBUG "RxHdr:\n");
4054 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
4055 bus->rxhdr, SDPCM_HDRLEN);
4059 /* Extract hardware header fields */
4060 len = get_unaligned_le16(bus->rxhdr);
4061 check = get_unaligned_le16(bus->rxhdr + sizeof(u16));
4063 /* All zeros means no more frames */
4064 if (!(len | check)) {
4069 /* Validate check bytes */
4070 if ((u16) ~(len ^ check)) {
4071 DHD_ERROR(("%s: HW hdr err: len/check 0x%04x/0x%04x\n",
4072 __func__, len, check));
4074 brcmf_sdbrcm_rxfail(bus, false, false);
4078 /* Validate frame length */
4079 if (len < SDPCM_HDRLEN) {
4080 DHD_ERROR(("%s: HW hdr length invalid: %d\n",
4085 /* Extract software header fields */
4086 chan = SDPCM_PACKET_CHANNEL(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
4087 seq = SDPCM_PACKET_SEQUENCE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
4088 doff = SDPCM_DOFFSET_VALUE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
4089 txmax = SDPCM_WINDOW_VALUE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
4091 /* Validate data offset */
4092 if ((doff < SDPCM_HDRLEN) || (doff > len)) {
4093 DHD_ERROR(("%s: Bad data offset %d: HW len %d, min %d "
4095 __func__, doff, len, SDPCM_HDRLEN, seq));
4098 brcmf_sdbrcm_rxfail(bus, false, false);
4102 /* Save the readahead length if there is one */
4104 bus->rxhdr[SDPCM_FRAMETAG_LEN + SDPCM_NEXTLEN_OFFSET];
4105 if ((bus->nextlen << 4) > MAX_RX_DATASZ) {
4106 DHD_INFO(("%s (nextlen): got frame w/nextlen too large "
4108 __func__, bus->nextlen, seq));
4112 /* Handle Flow Control */
4113 fcbits = SDPCM_FCMASK_VALUE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
4115 if (bus->flowcontrol != fcbits) {
4116 if (~bus->flowcontrol & fcbits)
4119 if (bus->flowcontrol & ~fcbits)
4123 bus->flowcontrol = fcbits;
4126 /* Check and update sequence number */
4128 DHD_INFO(("%s: rx_seq %d, expected %d\n", __func__,
4134 /* Check window for sanity */
4135 if ((u8) (txmax - bus->tx_seq) > 0x40) {
4136 DHD_ERROR(("%s: unlikely tx max %d with tx_seq %d\n",
4137 __func__, txmax, bus->tx_seq));
4138 txmax = bus->tx_seq + 2;
4140 bus->tx_max = txmax;
4142 /* Call a separate function for control frames */
4143 if (chan == SDPCM_CONTROL_CHANNEL) {
4144 brcmf_sdbrcm_read_control(bus, bus->rxhdr, len, doff);
4148 ASSERT((chan == SDPCM_DATA_CHANNEL)
4149 || (chan == SDPCM_EVENT_CHANNEL)
4150 || (chan == SDPCM_TEST_CHANNEL)
4151 || (chan == SDPCM_GLOM_CHANNEL));
4153 /* Length to read */
4154 rdlen = (len > firstread) ? (len - firstread) : 0;
4156 /* May pad read to blocksize for efficiency */
4157 if (bus->roundup && bus->blocksize &&
4158 (rdlen > bus->blocksize)) {
4159 pad = bus->blocksize - (rdlen % bus->blocksize);
4160 if ((pad <= bus->roundup) && (pad < bus->blocksize) &&
4161 ((rdlen + pad + firstread) < MAX_RX_DATASZ))
4163 } else if (rdlen % BRCMF_SDALIGN) {
4164 rdlen += BRCMF_SDALIGN - (rdlen % BRCMF_SDALIGN);
4167 /* Satisfy length-alignment requirements */
4168 if (forcealign && (rdlen & (ALIGNMENT - 1)))
4169 rdlen = roundup(rdlen, ALIGNMENT);
4171 if ((rdlen + firstread) > MAX_RX_DATASZ) {
4172 /* Too long -- skip this frame */
4173 DHD_ERROR(("%s: too long: len %d rdlen %d\n",
4174 __func__, len, rdlen));
4175 bus->dhd->rx_errors++;
4177 brcmf_sdbrcm_rxfail(bus, false, false);
4181 pkt = brcmu_pkt_buf_get_skb(rdlen + firstread + BRCMF_SDALIGN);
4183 /* Give up on data, request rtx of events */
4184 DHD_ERROR(("%s: brcmu_pkt_buf_get_skb failed: rdlen %d"
4185 " chan %d\n", __func__, rdlen, chan));
4186 bus->dhd->rx_dropped++;
4187 brcmf_sdbrcm_rxfail(bus, false, RETRYCHAN(chan));
4191 ASSERT(!(pkt->prev));
4193 /* Leave room for what we already read, and align remainder */
4194 ASSERT(firstread < pkt->len);
4195 skb_pull(pkt, firstread);
4196 PKTALIGN(pkt, rdlen, BRCMF_SDALIGN);
4198 /* Read the remaining frame data */
4199 sdret = brcmf_sdcard_recv_buf(sdh, brcmf_sdcard_cur_sbwad(sdh),
4200 SDIO_FUNC_2, F2SYNC, ((u8 *) (pkt->data)),
4201 rdlen, pkt, NULL, NULL);
4203 ASSERT(sdret != -BCME_PENDING);
4206 DHD_ERROR(("%s: read %d %s bytes failed: %d\n",
4209 SDPCM_EVENT_CHANNEL) ? "event" : ((chan ==
4211 ? "data" : "test")),
4213 brcmu_pkt_buf_free_skb(pkt);
4214 bus->dhd->rx_errors++;
4215 brcmf_sdbrcm_rxfail(bus, true, RETRYCHAN(chan));
4219 /* Copy the already-read portion */
4220 skb_push(pkt, firstread);
4221 memcpy(pkt->data, bus->rxhdr, firstread);
4224 if (DHD_BYTES_ON() && DHD_DATA_ON()) {
4225 printk(KERN_DEBUG "Rx Data:\n");
4226 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
4232 /* Save superframe descriptor and allocate packet frame */
4233 if (chan == SDPCM_GLOM_CHANNEL) {
4234 if (SDPCM_GLOMDESC(&bus->rxhdr[SDPCM_FRAMETAG_LEN])) {
4235 DHD_GLOM(("%s: glom descriptor, %d bytes:\n",
4238 if (DHD_GLOM_ON()) {
4239 printk(KERN_DEBUG "Glom Data:\n");
4240 print_hex_dump_bytes("",
4245 __skb_trim(pkt, len);
4246 ASSERT(doff == SDPCM_HDRLEN);
4247 skb_pull(pkt, SDPCM_HDRLEN);
4250 DHD_ERROR(("%s: glom superframe w/o "
4251 "descriptor!\n", __func__));
4252 brcmf_sdbrcm_rxfail(bus, false, false);
4257 /* Fill in packet len and prio, deliver upward */
4258 __skb_trim(pkt, len);
4259 skb_pull(pkt, doff);
4262 /* Test channel packets are processed separately */
4263 if (chan == SDPCM_TEST_CHANNEL) {
4264 brcmf_sdbrcm_checkdied(bus, pkt, seq);
4269 if (pkt->len == 0) {
4270 brcmu_pkt_buf_free_skb(pkt);
4272 } else if (brcmf_proto_hdrpull(bus->dhd, &ifidx, pkt) != 0) {
4273 DHD_ERROR(("%s: rx protocol error\n", __func__));
4274 brcmu_pkt_buf_free_skb(pkt);
4275 bus->dhd->rx_errors++;
4279 /* Unlock during rx call */
4280 brcmf_os_sdunlock(bus->dhd);
4281 brcmf_rx_frame(bus->dhd, ifidx, pkt, 1);
4282 brcmf_os_sdlock(bus->dhd);
4284 rxcount = maxframes - rxleft;
4286 /* Message if we hit the limit */
4287 if (!rxleft && !sdtest)
4288 DHD_DATA(("%s: hit rx limit of %d frames\n", __func__,
4292 DHD_DATA(("%s: processed %d frames\n", __func__, rxcount));
4293 /* Back off rxseq if awaiting rtx, update rx_seq */
4296 bus->rx_seq = rxseq;
4301 static u32 brcmf_sdbrcm_hostmail(dhd_bus_t *bus)
4303 struct sdpcmd_regs *regs = bus->regs;
4309 DHD_TRACE(("%s: Enter\n", __func__));
4311 /* Read mailbox data and ack that we did so */
4312 R_SDREG(hmb_data, ®s->tohostmailboxdata, retries);
4313 if (retries <= retry_limit)
4314 W_SDREG(SMB_INT_ACK, ®s->tosbmailbox, retries);
4315 bus->f1regdata += 2;
4317 /* Dongle recomposed rx frames, accept them again */
4318 if (hmb_data & HMB_DATA_NAKHANDLED) {
4319 DHD_INFO(("Dongle reports NAK handled, expect rtx of %d\n",
4322 DHD_ERROR(("%s: unexpected NAKHANDLED!\n", __func__));
4324 bus->rxskip = false;
4325 intstatus |= I_HMB_FRAME_IND;
4329 * DEVREADY does not occur with gSPI.
4331 if (hmb_data & (HMB_DATA_DEVREADY | HMB_DATA_FWREADY)) {
4333 (hmb_data & HMB_DATA_VERSION_MASK) >>
4334 HMB_DATA_VERSION_SHIFT;
4335 if (bus->sdpcm_ver != SDPCM_PROT_VERSION)
4336 DHD_ERROR(("Version mismatch, dongle reports %d, "
4338 bus->sdpcm_ver, SDPCM_PROT_VERSION));
4340 DHD_INFO(("Dongle ready, protocol version %d\n",
4345 * Flow Control has been moved into the RX headers and this out of band
4346 * method isn't used any more.
4347 * remaining backward compatible with older dongles.
4349 if (hmb_data & HMB_DATA_FC) {
4350 fcbits = (hmb_data & HMB_DATA_FCDATA_MASK) >>
4351 HMB_DATA_FCDATA_SHIFT;
4353 if (fcbits & ~bus->flowcontrol)
4356 if (bus->flowcontrol & ~fcbits)
4360 bus->flowcontrol = fcbits;
4363 /* Shouldn't be any others */
4364 if (hmb_data & ~(HMB_DATA_DEVREADY |
4365 HMB_DATA_NAKHANDLED |
4368 HMB_DATA_FCDATA_MASK | HMB_DATA_VERSION_MASK)) {
4369 DHD_ERROR(("Unknown mailbox data content: 0x%02x\n", hmb_data));
4375 bool brcmf_sdbrcm_dpc(dhd_bus_t *bus)
4377 struct brcmf_sdio *sdh = bus->sdh;
4378 struct sdpcmd_regs *regs = bus->regs;
4379 u32 intstatus, newstatus = 0;
4381 uint rxlimit = brcmf_rxbound; /* Rx frames to read before resched */
4382 uint txlimit = brcmf_txbound; /* Tx frames to send before resched */
4383 uint framecnt = 0; /* Temporary counter of tx/rx frames */
4384 bool rxdone = true; /* Flag for no more read data */
4385 bool resched = false; /* Flag indicating resched wanted */
4387 DHD_TRACE(("%s: Enter\n", __func__));
4389 /* Start with leftover status bits */
4390 intstatus = bus->intstatus;
4392 brcmf_os_sdlock(bus->dhd);
4394 /* If waiting for HTAVAIL, check status */
4395 if (bus->clkstate == CLK_PENDING) {
4397 u8 clkctl, devctl = 0;
4400 /* Check for inconsistent device control */
4401 devctl = brcmf_sdcard_cfg_read(sdh, SDIO_FUNC_1,
4402 SBSDIO_DEVICE_CTL, &err);
4404 DHD_ERROR(("%s: error reading DEVCTL: %d\n",
4406 bus->dhd->busstate = DHD_BUS_DOWN;
4408 ASSERT(devctl & SBSDIO_DEVCTL_CA_INT_ONLY);
4412 /* Read CSR, if clock on switch to AVAIL, else ignore */
4413 clkctl = brcmf_sdcard_cfg_read(sdh, SDIO_FUNC_1,
4414 SBSDIO_FUNC1_CHIPCLKCSR, &err);
4416 DHD_ERROR(("%s: error reading CSR: %d\n", __func__,
4418 bus->dhd->busstate = DHD_BUS_DOWN;
4421 DHD_INFO(("DPC: PENDING, devctl 0x%02x clkctl 0x%02x\n", devctl,
4424 if (SBSDIO_HTAV(clkctl)) {
4425 devctl = brcmf_sdcard_cfg_read(sdh, SDIO_FUNC_1,
4426 SBSDIO_DEVICE_CTL, &err);
4428 DHD_ERROR(("%s: error reading DEVCTL: %d\n",
4430 bus->dhd->busstate = DHD_BUS_DOWN;
4432 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
4433 brcmf_sdcard_cfg_write(sdh, SDIO_FUNC_1,
4434 SBSDIO_DEVICE_CTL, devctl, &err);
4436 DHD_ERROR(("%s: error writing DEVCTL: %d\n",
4438 bus->dhd->busstate = DHD_BUS_DOWN;
4440 bus->clkstate = CLK_AVAIL;
4448 /* Make sure backplane clock is on */
4449 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, true);
4450 if (bus->clkstate == CLK_PENDING)
4453 /* Pending interrupt indicates new device status */
4456 R_SDREG(newstatus, ®s->intstatus, retries);
4458 if (brcmf_sdcard_regfail(bus->sdh))
4460 newstatus &= bus->hostintmask;
4461 bus->fcstate = !!(newstatus & I_HMB_FC_STATE);
4463 W_SDREG(newstatus, ®s->intstatus, retries);
4468 /* Merge new bits with previous */
4469 intstatus |= newstatus;
4472 /* Handle flow-control change: read new state in case our ack
4473 * crossed another change interrupt. If change still set, assume
4474 * FC ON for safety, let next loop through do the debounce.
4476 if (intstatus & I_HMB_FC_CHANGE) {
4477 intstatus &= ~I_HMB_FC_CHANGE;
4478 W_SDREG(I_HMB_FC_CHANGE, ®s->intstatus, retries);
4479 R_SDREG(newstatus, ®s->intstatus, retries);
4480 bus->f1regdata += 2;
4482 !!(newstatus & (I_HMB_FC_STATE | I_HMB_FC_CHANGE));
4483 intstatus |= (newstatus & bus->hostintmask);
4486 /* Handle host mailbox indication */
4487 if (intstatus & I_HMB_HOST_INT) {
4488 intstatus &= ~I_HMB_HOST_INT;
4489 intstatus |= brcmf_sdbrcm_hostmail(bus);
4492 /* Generally don't ask for these, can get CRC errors... */
4493 if (intstatus & I_WR_OOSYNC) {
4494 DHD_ERROR(("Dongle reports WR_OOSYNC\n"));
4495 intstatus &= ~I_WR_OOSYNC;
4498 if (intstatus & I_RD_OOSYNC) {
4499 DHD_ERROR(("Dongle reports RD_OOSYNC\n"));
4500 intstatus &= ~I_RD_OOSYNC;
4503 if (intstatus & I_SBINT) {
4504 DHD_ERROR(("Dongle reports SBINT\n"));
4505 intstatus &= ~I_SBINT;
4508 /* Would be active due to wake-wlan in gSPI */
4509 if (intstatus & I_CHIPACTIVE) {
4510 DHD_INFO(("Dongle reports CHIPACTIVE\n"));
4511 intstatus &= ~I_CHIPACTIVE;
4514 /* Ignore frame indications if rxskip is set */
4516 intstatus &= ~I_HMB_FRAME_IND;
4518 /* On frame indication, read available frames */
4519 if (PKT_AVAILABLE()) {
4520 framecnt = brcmf_sdbrcm_readframes(bus, rxlimit, &rxdone);
4521 if (rxdone || bus->rxskip)
4522 intstatus &= ~I_HMB_FRAME_IND;
4523 rxlimit -= min(framecnt, rxlimit);
4526 /* Keep still-pending events for next scheduling */
4527 bus->intstatus = intstatus;
4530 /* Re-enable interrupts to detect new device events (mailbox, rx frame)
4531 * or clock availability. (Allows tx loop to check ipend if desired.)
4532 * (Unless register access seems hosed, as we may not be able to ACK...)
4534 if (bus->intr && bus->intdis && !brcmf_sdcard_regfail(sdh)) {
4535 DHD_INTR(("%s: enable SDIO interrupts, rxdone %d framecnt %d\n",
4536 __func__, rxdone, framecnt));
4537 bus->intdis = false;
4538 brcmf_sdcard_intr_enable(sdh);
4541 if (DATAOK(bus) && bus->ctrl_frame_stat &&
4542 (bus->clkstate == CLK_AVAIL)) {
4545 ret = brcmf_sdbrcm_send_buf(bus, brcmf_sdcard_cur_sbwad(sdh),
4546 SDIO_FUNC_2, F2SYNC, (u8 *) bus->ctrl_frame_buf,
4547 (u32) bus->ctrl_frame_len, NULL, NULL, NULL);
4548 ASSERT(ret != -BCME_PENDING);
4551 /* On failure, abort the command and
4552 terminate the frame */
4553 DHD_INFO(("%s: sdio error %d, abort command and "
4554 "terminate frame.\n", __func__, ret));
4557 brcmf_sdcard_abort(sdh, SDIO_FUNC_2);
4559 brcmf_sdcard_cfg_write(sdh, SDIO_FUNC_1,
4560 SBSDIO_FUNC1_FRAMECTRL, SFC_WF_TERM,
4564 for (i = 0; i < 3; i++) {
4566 hi = brcmf_sdcard_cfg_read(sdh, SDIO_FUNC_1,
4567 SBSDIO_FUNC1_WFRAMEBCHI,
4569 lo = brcmf_sdcard_cfg_read(sdh, SDIO_FUNC_1,
4570 SBSDIO_FUNC1_WFRAMEBCLO,
4572 bus->f1regdata += 2;
4573 if ((hi == 0) && (lo == 0))
4579 bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQUENCE_WRAP;
4581 DHD_INFO(("Return_dpc value is : %d\n", ret));
4582 bus->ctrl_frame_stat = false;
4583 brcmf_sdbrcm_wait_event_wakeup(bus);
4585 /* Send queued frames (limit 1 if rx may still be pending) */
4586 else if ((bus->clkstate == CLK_AVAIL) && !bus->fcstate &&
4587 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) && txlimit
4589 framecnt = rxdone ? txlimit : min(txlimit, dhd_txminmax);
4590 framecnt = brcmf_sdbrcm_sendfromq(bus, framecnt);
4591 txlimit -= framecnt;
4594 /* Resched if events or tx frames are pending,
4595 else await next interrupt */
4596 /* On failed register access, all bets are off:
4597 no resched or interrupts */
4598 if ((bus->dhd->busstate == DHD_BUS_DOWN) || brcmf_sdcard_regfail(sdh)) {
4599 DHD_ERROR(("%s: failed backplane access over SDIO, halting "
4600 "operation %d\n", __func__, brcmf_sdcard_regfail(sdh)));
4601 bus->dhd->busstate = DHD_BUS_DOWN;
4603 } else if (bus->clkstate == CLK_PENDING) {
4604 DHD_INFO(("%s: rescheduled due to CLK_PENDING awaiting "
4605 "I_CHIPACTIVE interrupt\n", __func__));
4607 } else if (bus->intstatus || bus->ipend ||
4608 (!bus->fcstate && brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol)
4609 && DATAOK(bus)) || PKT_AVAILABLE()) {
4613 bus->dpc_sched = resched;
4615 /* If we're done for now, turn off clock request. */
4616 if ((bus->clkstate != CLK_PENDING)
4617 && bus->idletime == DHD_IDLE_IMMEDIATE) {
4618 bus->activity = false;
4619 brcmf_sdbrcm_clkctl(bus, CLK_NONE, false);
4622 brcmf_os_sdunlock(bus->dhd);
4627 bool dhd_bus_dpc(struct dhd_bus *bus)
4631 /* Call the DPC directly. */
4632 DHD_TRACE(("Calling brcmf_sdbrcm_dpc() from %s\n", __func__));
4633 resched = brcmf_sdbrcm_dpc(bus);
4638 void brcmf_sdbrcm_isr(void *arg)
4640 dhd_bus_t *bus = (dhd_bus_t *) arg;
4641 struct brcmf_sdio *sdh;
4643 DHD_TRACE(("%s: Enter\n", __func__));
4646 DHD_ERROR(("%s : bus is null pointer , exit\n", __func__));
4651 if (bus->dhd->busstate == DHD_BUS_DOWN) {
4652 DHD_ERROR(("%s : bus is down. we have nothing to do\n",
4656 /* Count the interrupt call */
4660 /* Shouldn't get this interrupt if we're sleeping? */
4661 if (bus->sleeping) {
4662 DHD_ERROR(("INTERRUPT WHILE SLEEPING??\n"));
4666 /* Disable additional interrupts (is this needed now)? */
4668 DHD_INTR(("%s: disable SDIO interrupts\n", __func__));
4670 DHD_ERROR(("brcmf_sdbrcm_isr() w/o interrupt configured!\n"));
4672 brcmf_sdcard_intr_disable(sdh);
4675 #if defined(SDIO_ISR_THREAD)
4676 DHD_TRACE(("Calling brcmf_sdbrcm_dpc() from %s\n", __func__));
4677 while (brcmf_sdbrcm_dpc(bus))
4680 bus->dpc_sched = true;
4681 brcmf_sched_dpc(bus->dhd);
4687 static void brcmf_sdbrcm_pktgen_init(dhd_bus_t *bus)
4689 /* Default to specified length, or full range */
4690 if (brcmf_pktgen_len) {
4691 bus->pktgen_maxlen = min(brcmf_pktgen_len,
4692 BRCMF_MAX_PKTGEN_LEN);
4693 bus->pktgen_minlen = bus->pktgen_maxlen;
4695 bus->pktgen_maxlen = BRCMF_MAX_PKTGEN_LEN;
4696 bus->pktgen_minlen = 0;
4698 bus->pktgen_len = (u16) bus->pktgen_minlen;
4700 /* Default to per-watchdog burst with 10s print time */
4701 bus->pktgen_freq = 1;
4702 bus->pktgen_print = 10000 / brcmf_watchdog_ms;
4703 bus->pktgen_count = (brcmf_pktgen * brcmf_watchdog_ms + 999) / 1000;
4705 /* Default to echo mode */
4706 bus->pktgen_mode = DHD_PKTGEN_ECHO;
4707 bus->pktgen_stop = 1;
4710 static void brcmf_sdbrcm_pktgen(dhd_bus_t *bus)
4712 struct sk_buff *pkt;
4718 /* Display current count if appropriate */
4719 if (bus->pktgen_print && (++bus->pktgen_ptick >= bus->pktgen_print)) {
4720 bus->pktgen_ptick = 0;
4721 printk(KERN_DEBUG "%s: send attempts %d rcvd %d\n",
4722 __func__, bus->pktgen_sent, bus->pktgen_rcvd);
4725 /* For recv mode, just make sure dongle has started sending */
4726 if (bus->pktgen_mode == DHD_PKTGEN_RECV) {
4727 if (!bus->pktgen_rcvd)
4728 brcmf_sdbrcm_sdtest_set(bus, true);
4732 /* Otherwise, generate or request the specified number of packets */
4733 for (pktcount = 0; pktcount < bus->pktgen_count; pktcount++) {
4734 /* Stop if total has been reached */
4735 if (bus->pktgen_total
4736 && (bus->pktgen_sent >= bus->pktgen_total)) {
4737 bus->pktgen_count = 0;
4741 /* Allocate an appropriate-sized packet */
4742 len = bus->pktgen_len;
4743 pkt = brcmu_pkt_buf_get_skb(
4744 (len + SDPCM_HDRLEN + SDPCM_TEST_HDRLEN + BRCMF_SDALIGN),
4747 DHD_ERROR(("%s: brcmu_pkt_buf_get_skb failed!\n",
4751 PKTALIGN(pkt, (len + SDPCM_HDRLEN + SDPCM_TEST_HDRLEN),
4753 data = (u8 *) (pkt->data) + SDPCM_HDRLEN;
4755 /* Write test header cmd and extra based on mode */
4756 switch (bus->pktgen_mode) {
4757 case DHD_PKTGEN_ECHO:
4758 *data++ = SDPCM_TEST_ECHOREQ;
4759 *data++ = (u8) bus->pktgen_sent;
4762 case DHD_PKTGEN_SEND:
4763 *data++ = SDPCM_TEST_DISCARD;
4764 *data++ = (u8) bus->pktgen_sent;
4767 case DHD_PKTGEN_RXBURST:
4768 *data++ = SDPCM_TEST_BURST;
4769 *data++ = (u8) bus->pktgen_count;
4773 DHD_ERROR(("Unrecognized pktgen mode %d\n",
4775 brcmu_pkt_buf_free_skb(pkt, true);
4776 bus->pktgen_count = 0;
4780 /* Write test header length field */
4781 *data++ = (len >> 0);
4782 *data++ = (len >> 8);
4784 /* Then fill in the remainder -- N/A for burst,
4786 for (fillbyte = 0; fillbyte < len; fillbyte++)
4788 SDPCM_TEST_FILL(fillbyte, (u8) bus->pktgen_sent);
4791 if (DHD_BYTES_ON() && DHD_DATA_ON()) {
4792 data = (u8 *) (pkt->data) + SDPCM_HDRLEN;
4793 printk(KERN_DEBUG "brcmf_sdbrcm_pktgen: Tx Data:\n");
4794 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, data,
4795 pkt->len - SDPCM_HDRLEN);
4800 if (brcmf_sdbrcm_txpkt(bus, pkt, SDPCM_TEST_CHANNEL, true)) {
4802 if (bus->pktgen_stop
4803 && bus->pktgen_stop == bus->pktgen_fail)
4804 bus->pktgen_count = 0;
4808 /* Bump length if not fixed, wrap at max */
4809 if (++bus->pktgen_len > bus->pktgen_maxlen)
4810 bus->pktgen_len = (u16) bus->pktgen_minlen;
4812 /* Special case for burst mode: just send one request! */
4813 if (bus->pktgen_mode == DHD_PKTGEN_RXBURST)
4818 static void brcmf_sdbrcm_sdtest_set(dhd_bus_t *bus, bool start)
4820 struct sk_buff *pkt;
4823 /* Allocate the packet */
4824 pkt = brcmu_pkt_buf_get_skb(SDPCM_HDRLEN + SDPCM_TEST_HDRLEN +
4825 BRCMF_SDALIGN, true);
4827 DHD_ERROR(("%s: brcmu_pkt_buf_get_skb failed!\n", __func__));
4830 PKTALIGN(pkt, (SDPCM_HDRLEN + SDPCM_TEST_HDRLEN), BRCMF_SDALIGN);
4831 data = (u8 *) (pkt->data) + SDPCM_HDRLEN;
4833 /* Fill in the test header */
4834 *data++ = SDPCM_TEST_SEND;
4836 *data++ = (bus->pktgen_maxlen >> 0);
4837 *data++ = (bus->pktgen_maxlen >> 8);
4840 if (brcmf_sdbrcm_txpkt(bus, pkt, SDPCM_TEST_CHANNEL, true))
4845 brcmf_sdbrcm_checkdied(dhd_bus_t *bus, struct sk_buff *pkt, uint seq)
4855 /* Check for min length */
4857 if (pktlen < SDPCM_TEST_HDRLEN) {
4858 DHD_ERROR(("brcmf_sdbrcm_checkdied: toss runt frame, pktlen "
4860 brcmu_pkt_buf_free_skb(pkt, false);
4864 /* Extract header fields */
4869 len += *data++ << 8;
4871 /* Check length for relevant commands */
4872 if (cmd == SDPCM_TEST_DISCARD || cmd == SDPCM_TEST_ECHOREQ
4873 || cmd == SDPCM_TEST_ECHORSP) {
4874 if (pktlen != len + SDPCM_TEST_HDRLEN) {
4875 DHD_ERROR(("brcmf_sdbrcm_checkdied: frame length "
4876 "mismatch, pktlen %d seq %d" " cmd %d extra %d "
4878 pktlen, seq, cmd, extra, len));
4879 brcmu_pkt_buf_free_skb(pkt, false);
4884 /* Process as per command */
4886 case SDPCM_TEST_ECHOREQ:
4887 /* Rx->Tx turnaround ok (even on NDIS w/current
4889 *(u8 *) (pkt->data) = SDPCM_TEST_ECHORSP;
4890 if (brcmf_sdbrcm_txpkt(bus, pkt, SDPCM_TEST_CHANNEL, true) == 0)
4894 brcmu_pkt_buf_free_skb(pkt, false);
4899 case SDPCM_TEST_ECHORSP:
4900 if (bus->ext_loop) {
4901 brcmu_pkt_buf_free_skb(pkt, false);
4906 for (offset = 0; offset < len; offset++, data++) {
4907 if (*data != SDPCM_TEST_FILL(offset, extra)) {
4908 DHD_ERROR(("brcmf_sdbrcm_checkdied: echo data "
4909 "mismatch: " "offset %d (len %d) "
4910 "expect 0x%02x rcvd 0x%02x\n",
4912 SDPCM_TEST_FILL(offset, extra),
4917 brcmu_pkt_buf_free_skb(pkt, false);
4921 case SDPCM_TEST_DISCARD:
4922 brcmu_pkt_buf_free_skb(pkt, false);
4926 case SDPCM_TEST_BURST:
4927 case SDPCM_TEST_SEND:
4929 DHD_INFO(("brcmf_sdbrcm_checkdied: unsupported or unknown "
4930 "command, pktlen %d seq %d" " cmd %d extra %d len %d\n",
4931 pktlen, seq, cmd, extra, len));
4932 brcmu_pkt_buf_free_skb(pkt, false);
4936 /* For recv mode, stop at limie (and tell dongle to stop sending) */
4937 if (bus->pktgen_mode == DHD_PKTGEN_RECV) {
4938 if (bus->pktgen_total
4939 && (bus->pktgen_rcvd >= bus->pktgen_total)) {
4940 bus->pktgen_count = 0;
4941 brcmf_sdbrcm_sdtest_set(bus, false);
4947 extern bool brcmf_sdbrcm_bus_watchdog(dhd_pub_t *dhdp)
4951 DHD_TIMER(("%s: Enter\n", __func__));
4955 if (bus->dhd->dongle_reset)
4958 /* Ignore the timer if simulating bus down */
4962 brcmf_os_sdlock(bus->dhd);
4964 /* Poll period: check device if appropriate. */
4965 if (bus->poll && (++bus->polltick >= bus->pollrate)) {
4968 /* Reset poll tick */
4971 /* Check device if no interrupts */
4972 if (!bus->intr || (bus->intrcount == bus->lastintrs)) {
4974 if (!bus->dpc_sched) {
4976 devpend = brcmf_sdcard_cfg_read(bus->sdh,
4977 SDIO_FUNC_0, SDIO_CCCR_INTx,
4980 devpend & (INTR_STATUS_FUNC1 |
4984 /* If there is something, make like the ISR and
4990 brcmf_sdcard_intr_disable(bus->sdh);
4992 bus->dpc_sched = true;
4993 brcmf_sched_dpc(bus->dhd);
4998 /* Update interrupt tracking */
4999 bus->lastintrs = bus->intrcount;
5002 /* Poll for console output periodically */
5003 if (dhdp->busstate == DHD_BUS_DATA && brcmf_console_ms != 0) {
5004 bus->console.count += brcmf_watchdog_ms;
5005 if (bus->console.count >= brcmf_console_ms) {
5006 bus->console.count -= brcmf_console_ms;
5007 /* Make sure backplane clock is on */
5008 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
5009 if (brcmf_sdbrcm_readconsole(bus) < 0)
5010 brcmf_console_ms = 0; /* On error,
5017 /* Generate packets if configured */
5018 if (bus->pktgen_count && (++bus->pktgen_tick >= bus->pktgen_freq)) {
5019 /* Make sure backplane clock is on */
5020 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
5021 bus->pktgen_tick = 0;
5022 brcmf_sdbrcm_pktgen(bus);
5026 /* On idle timeout clear activity flag and/or turn off clock */
5027 if ((bus->idletime > 0) && (bus->clkstate == CLK_AVAIL)) {
5028 if (++bus->idlecount >= bus->idletime) {
5030 if (bus->activity) {
5031 bus->activity = false;
5032 brcmf_os_wd_timer(bus->dhd, brcmf_watchdog_ms);
5034 brcmf_sdbrcm_clkctl(bus, CLK_NONE, false);
5039 brcmf_os_sdunlock(bus->dhd);
5045 extern int brcmf_sdbrcm_bus_console_in(dhd_pub_t *dhdp, unsigned char *msg,
5048 dhd_bus_t *bus = dhdp->bus;
5051 struct sk_buff *pkt;
5053 /* Address could be zero if CONSOLE := 0 in dongle Makefile */
5054 if (bus->console_addr == 0)
5057 /* Exclusive bus access */
5058 brcmf_os_sdlock(bus->dhd);
5060 /* Don't allow input if dongle is in reset */
5061 if (bus->dhd->dongle_reset) {
5062 brcmf_os_sdunlock(bus->dhd);
5066 /* Request clock to allow SDIO accesses */
5068 /* No pend allowed since txpkt is called later, ht clk has to be on */
5069 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
5071 /* Zero cbuf_index */
5072 addr = bus->console_addr + offsetof(struct rte_console, cbuf_idx);
5073 val = cpu_to_le32(0);
5074 rv = brcmf_sdbrcm_membytes(bus, true, addr, (u8 *)&val, sizeof(val));
5078 /* Write message into cbuf */
5079 addr = bus->console_addr + offsetof(struct rte_console, cbuf);
5080 rv = brcmf_sdbrcm_membytes(bus, true, addr, (u8 *)msg, msglen);
5084 /* Write length into vcons_in */
5085 addr = bus->console_addr + offsetof(struct rte_console, vcons_in);
5086 val = cpu_to_le32(msglen);
5087 rv = brcmf_sdbrcm_membytes(bus, true, addr, (u8 *)&val, sizeof(val));
5091 /* Bump dongle by sending an empty event pkt.
5092 * sdpcm_sendup (RX) checks for virtual console input.
5094 pkt = brcmu_pkt_buf_get_skb(4 + SDPCM_RESERVE);
5095 if ((pkt != NULL) && bus->clkstate == CLK_AVAIL)
5096 brcmf_sdbrcm_txpkt(bus, pkt, SDPCM_EVENT_CHANNEL, true);
5099 if ((bus->idletime == DHD_IDLE_IMMEDIATE) && !bus->dpc_sched) {
5100 bus->activity = false;
5101 brcmf_sdbrcm_clkctl(bus, CLK_NONE, true);
5104 brcmf_os_sdunlock(bus->dhd);
5110 static bool brcmf_sdbrcm_chipmatch(u16 chipid)
5112 if (chipid == BCM4325_CHIP_ID)
5114 if (chipid == BCM4329_CHIP_ID)
5116 if (chipid == BCM4319_CHIP_ID)
5121 static void *brcmf_sdbrcm_probe(u16 venid, u16 devid, u16 bus_no,
5122 u16 slot, u16 func, uint bustype, void *regsva,
5128 /* Init global variables at run-time, not as part of the declaration.
5129 * This is required to support init/de-init of the driver.
5131 * of globals as part of the declaration results in non-deterministic
5132 * behavior since the value of the globals may be different on the
5133 * first time that the driver is initialized vs subsequent
5136 brcmf_txbound = DHD_TXBOUND;
5137 brcmf_rxbound = DHD_RXBOUND;
5138 dhd_alignctl = true;
5140 dhd_readahead = true;
5142 brcmf_dongle_memsize = 0;
5143 dhd_txminmax = DHD_TXMINMAX;
5149 DHD_TRACE(("%s: Enter\n", __func__));
5150 DHD_INFO(("%s: venid 0x%04x devid 0x%04x\n", __func__, venid, devid));
5152 /* We make assumptions about address window mappings */
5153 ASSERT((unsigned long)regsva == SI_ENUM_BASE);
5155 /* BCMSDH passes venid and devid based on CIS parsing -- but
5157 * means early parse could fail, so here we should get either an ID
5158 * we recognize OR (-1) indicating we must request power first.
5160 /* Check the Vendor ID */
5163 case PCI_VENDOR_ID_BROADCOM:
5166 DHD_ERROR(("%s: unknown vendor: 0x%04x\n", __func__, venid));
5170 /* Check the Device ID and make sure it's one that we support */
5172 case BCM4325_D11DUAL_ID: /* 4325 802.11a/g id */
5173 case BCM4325_D11G_ID: /* 4325 802.11g 2.4Ghz band id */
5174 case BCM4325_D11A_ID: /* 4325 802.11a 5Ghz band id */
5175 DHD_INFO(("%s: found 4325 Dongle\n", __func__));
5177 case BCM4329_D11NDUAL_ID: /* 4329 802.11n dualband device */
5178 case BCM4329_D11N2G_ID: /* 4329 802.11n 2.4G device */
5179 case BCM4329_D11N5G_ID: /* 4329 802.11n 5G device */
5181 DHD_INFO(("%s: found 4329 Dongle\n", __func__));
5183 case BCM4319_D11N_ID: /* 4319 802.11n id */
5184 case BCM4319_D11N2G_ID: /* 4319 802.11n2g id */
5185 case BCM4319_D11N5G_ID: /* 4319 802.11n5g id */
5186 DHD_INFO(("%s: found 4319 Dongle\n", __func__));
5189 DHD_INFO(("%s: allow device id 0, will check chip internals\n",
5194 DHD_ERROR(("%s: skipping 0x%04x/0x%04x, not a dongle\n",
5195 __func__, venid, devid));
5199 /* Allocate private bus interface state */
5200 bus = kzalloc(sizeof(dhd_bus_t), GFP_ATOMIC);
5202 DHD_ERROR(("%s: kmalloc of dhd_bus_t failed\n", __func__));
5206 bus->cl_devid = (u16) devid;
5208 bus->tx_seq = SDPCM_SEQUENCE_WRAP - 1;
5209 bus->usebufpool = false; /* Use bufpool if allocated,
5210 else use locally malloced rxbuf */
5212 /* attempt to attach to the dongle */
5213 if (!(brcmf_sdbrcm_probe_attach(bus, sdh, regsva, devid))) {
5214 DHD_ERROR(("%s: brcmf_sdbrcm_probe_attach failed\n", __func__));
5218 spin_lock_init(&bus->txqlock);
5219 init_waitqueue_head(&bus->ctrl_wait);
5221 /* Attach to the dhd/OS/network interface */
5222 bus->dhd = brcmf_attach(bus, SDPCM_RESERVE);
5224 DHD_ERROR(("%s: dhd_attach failed\n", __func__));
5228 /* Allocate buffers */
5229 if (!(brcmf_sdbrcm_probe_malloc(bus, sdh))) {
5230 DHD_ERROR(("%s: brcmf_sdbrcm_probe_malloc failed\n", __func__));
5234 if (!(brcmf_sdbrcm_probe_init(bus, sdh))) {
5235 DHD_ERROR(("%s: brcmf_sdbrcm_probe_init failed\n", __func__));
5239 /* Register interrupt callback, but mask it (not operational yet). */
5240 DHD_INTR(("%s: disable SDIO interrupts (not interested yet)\n",
5242 brcmf_sdcard_intr_disable(sdh);
5243 ret = brcmf_sdcard_intr_reg(sdh, brcmf_sdbrcm_isr, bus);
5245 DHD_ERROR(("%s: FAILED: bcmsdh_intr_reg returned %d\n",
5249 DHD_INTR(("%s: registered SDIO interrupt function ok\n", __func__));
5251 DHD_INFO(("%s: completed!!\n", __func__));
5253 /* if firmware path present try to download and bring up bus */
5254 ret = brcmf_bus_start(bus->dhd);
5256 if (ret == -ENOLINK) {
5257 DHD_ERROR(("%s: dongle is not responding\n", __func__));
5261 /* Ok, have the per-port tell the stack we're open for business */
5262 if (brcmf_net_attach(bus->dhd, 0) != 0) {
5263 DHD_ERROR(("%s: Net attach failed!!\n", __func__));
5270 brcmf_sdbrcm_release(bus);
5275 brcmf_sdbrcm_probe_attach(struct dhd_bus *bus, void *sdh, void *regsva,
5281 bus->alp_only = true;
5283 /* Return the window to backplane enumeration space for core access */
5284 if (brcmf_sdbrcm_set_siaddr_window(bus, SI_ENUM_BASE))
5285 DHD_ERROR(("%s: FAILED to return to SI_ENUM_BASE\n", __func__));
5288 printk(KERN_DEBUG "F1 signature read @0x18000000=0x%4x\n",
5289 brcmf_sdcard_reg_read(bus->sdh, SI_ENUM_BASE, 4));
5294 * Force PLL off until brcmf_sdbrcm_chip_attach()
5295 * programs PLL control regs
5298 brcmf_sdcard_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
5299 DHD_INIT_CLKCTL1, &err);
5302 brcmf_sdcard_cfg_read(sdh, SDIO_FUNC_1,
5303 SBSDIO_FUNC1_CHIPCLKCSR, &err);
5305 if (err || ((clkctl & ~SBSDIO_AVBITS) != DHD_INIT_CLKCTL1)) {
5306 DHD_ERROR(("brcmf_sdbrcm_probe: ChipClkCSR access: err %d wrote"
5307 " 0x%02x read 0x%02x\n",
5308 err, DHD_INIT_CLKCTL1, clkctl));
5312 if (brcmf_sdbrcm_chip_attach(bus, regsva)) {
5313 DHD_ERROR(("%s: brcmf_sdbrcm_chip_attach failed!\n", __func__));
5317 brcmf_sdcard_chipinfo(sdh, bus->ci->chip, bus->ci->chiprev);
5319 if (!brcmf_sdbrcm_chipmatch((u16) bus->ci->chip)) {
5320 DHD_ERROR(("%s: unsupported chip: 0x%04x\n",
5321 __func__, bus->ci->chip));
5325 brcmf_sdbrcm_sdiod_drive_strength_init(bus, brcmf_sdiod_drive_strength);
5327 /* Get info on the ARM and SOCRAM cores... */
5328 if (!DHD_NOPMU(bus)) {
5329 bus->armrev = SBCOREREV(brcmf_sdcard_reg_read(bus->sdh,
5330 CORE_SB(bus->ci->armcorebase, sbidhigh), 4));
5331 bus->orig_ramsize = bus->ci->ramsize;
5332 if (!(bus->orig_ramsize)) {
5333 DHD_ERROR(("%s: failed to find SOCRAM memory!\n",
5337 bus->ramsize = bus->orig_ramsize;
5338 if (brcmf_dongle_memsize)
5339 brcmf_sdbrcm_setmemsize(bus, brcmf_dongle_memsize);
5341 DHD_ERROR(("DHD: dongle ram size is set to %d(orig %d)\n",
5342 bus->ramsize, bus->orig_ramsize));
5345 bus->regs = (void *)bus->ci->buscorebase;
5347 /* Set core control so an SDIO reset does a backplane reset */
5348 OR_REG(&bus->regs->corecontrol, CC_BPRESEN);
5350 brcmu_pktq_init(&bus->txq, (PRIOMASK + 1), TXQLEN);
5352 /* Locate an appropriately-aligned portion of hdrbuf */
5353 bus->rxhdr = (u8 *) roundup((unsigned long)&bus->hdrbuf[0], BRCMF_SDALIGN);
5355 /* Set the poll and/or interrupt flags */
5356 bus->intr = (bool) brcmf_intr;
5357 bus->poll = (bool) brcmf_poll;
5367 static bool brcmf_sdbrcm_probe_malloc(dhd_bus_t *bus, void *sdh)
5369 DHD_TRACE(("%s: Enter\n", __func__));
5371 if (bus->dhd->maxctl) {
5373 roundup((bus->dhd->maxctl + SDPCM_HDRLEN),
5374 ALIGNMENT) + BRCMF_SDALIGN;
5375 bus->rxbuf = kmalloc(bus->rxblen, GFP_ATOMIC);
5376 if (!(bus->rxbuf)) {
5377 DHD_ERROR(("%s: kmalloc of %d-byte rxbuf failed\n",
5378 __func__, bus->rxblen));
5383 /* Allocate buffer to receive glomed packet */
5384 bus->databuf = kmalloc(MAX_DATA_BUF, GFP_ATOMIC);
5385 if (!(bus->databuf)) {
5386 DHD_ERROR(("%s: kmalloc of %d-byte databuf failed\n",
5387 __func__, MAX_DATA_BUF));
5388 /* release rxbuf which was already located as above */
5394 /* Align the buffer */
5395 if ((unsigned long)bus->databuf % BRCMF_SDALIGN)
5397 bus->databuf + (BRCMF_SDALIGN -
5398 ((unsigned long)bus->databuf % BRCMF_SDALIGN));
5400 bus->dataptr = bus->databuf;
5408 static bool brcmf_sdbrcm_probe_init(dhd_bus_t *bus, void *sdh)
5412 DHD_TRACE(("%s: Enter\n", __func__));
5415 brcmf_sdbrcm_pktgen_init(bus);
5418 /* Disable F2 to clear any intermediate frame state on the dongle */
5419 brcmf_sdcard_cfg_write(sdh, SDIO_FUNC_0, SDIO_CCCR_IOEx,
5420 SDIO_FUNC_ENABLE_1, NULL);
5422 bus->dhd->busstate = DHD_BUS_DOWN;
5423 bus->sleeping = false;
5424 bus->rxflow = false;
5425 bus->prev_rxlim_hit = 0;
5427 /* Done with backplane-dependent accesses, can drop clock... */
5428 brcmf_sdcard_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR, 0,
5431 /* ...and initialize clock/power states */
5432 bus->clkstate = CLK_SDONLY;
5433 bus->idletime = (s32) brcmf_idletime;
5434 bus->idleclock = DHD_IDLE_ACTIVE;
5436 /* Query the F2 block size, set roundup accordingly */
5438 if (brcmf_sdcard_iovar_op(sdh, "sd_blocksize", &fnum, sizeof(s32),
5439 &bus->blocksize, sizeof(s32), false) != 0) {
5441 DHD_ERROR(("%s: fail on %s get\n", __func__, "sd_blocksize"));
5443 DHD_INFO(("%s: Initial value for %s is %d\n",
5444 __func__, "sd_blocksize", bus->blocksize));
5446 bus->roundup = min(max_roundup, bus->blocksize);
5448 /* Query if bus module supports packet chaining,
5449 default to use if supported */
5450 if (brcmf_sdcard_iovar_op(sdh, "sd_rxchain", NULL, 0,
5451 &bus->sd_rxchain, sizeof(s32),
5453 bus->sd_rxchain = false;
5455 DHD_INFO(("%s: bus module (through bcmsdh API) %s chaining\n",
5457 (bus->sd_rxchain ? "supports" : "does not support")));
5459 bus->use_rxchain = (bool) bus->sd_rxchain;
5465 dhd_bus_download_firmware(struct dhd_bus *bus, char *fw_path, char *nv_path)
5468 bus->fw_path = fw_path;
5469 bus->nv_path = nv_path;
5471 ret = brcmf_sdbrcm_download_firmware(bus, bus->sdh);
5477 brcmf_sdbrcm_download_firmware(struct dhd_bus *bus, void *sdh)
5481 /* Download the firmware */
5482 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
5484 ret = _brcmf_sdbrcm_download_firmware(bus) == 0;
5486 brcmf_sdbrcm_clkctl(bus, CLK_SDONLY, false);
5491 /* Detach and free everything */
5492 static void brcmf_sdbrcm_release(dhd_bus_t *bus)
5494 DHD_TRACE(("%s: Enter\n", __func__));
5497 /* De-register interrupt handler */
5498 brcmf_sdcard_intr_disable(bus->sdh);
5499 brcmf_sdcard_intr_dereg(bus->sdh);
5502 brcmf_detach(bus->dhd);
5503 brcmf_sdbrcm_release_dongle(bus);
5507 brcmf_sdbrcm_release_malloc(bus);
5512 DHD_TRACE(("%s: Disconnected\n", __func__));
5515 static void brcmf_sdbrcm_release_malloc(dhd_bus_t *bus)
5517 DHD_TRACE(("%s: Enter\n", __func__));
5519 if (bus->dhd && bus->dhd->dongle_reset)
5523 bus->rxctl = bus->rxbuf = NULL;
5526 kfree(bus->databuf);
5527 bus->databuf = NULL;
5530 static void brcmf_sdbrcm_release_dongle(dhd_bus_t *bus)
5532 DHD_TRACE(("%s: Enter\n", __func__));
5534 if (bus->dhd && bus->dhd->dongle_reset)
5538 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
5539 brcmf_sdbrcm_clkctl(bus, CLK_NONE, false);
5540 brcmf_sdbrcm_chip_detach(bus);
5541 if (bus->vars && bus->varsz)
5546 DHD_TRACE(("%s: Disconnected\n", __func__));
5549 static void brcmf_sdbrcm_disconnect(void *ptr)
5551 dhd_bus_t *bus = (dhd_bus_t *)ptr;
5553 DHD_TRACE(("%s: Enter\n", __func__));
5557 brcmf_sdbrcm_release(bus);
5560 DHD_TRACE(("%s: Disconnected\n", __func__));
5563 /* Register/Unregister functions are called by the main DHD entry
5564 * point (e.g. module insertion) to link with the bus driver, in
5565 * order to look for or await the device.
5568 static struct brcmf_sdioh_driver dhd_sdio = {
5570 brcmf_sdbrcm_disconnect
5573 int dhd_bus_register(void)
5575 DHD_TRACE(("%s: Enter\n", __func__));
5577 return brcmf_sdio_register(&dhd_sdio);
5580 void dhd_bus_unregister(void)
5582 DHD_TRACE(("%s: Enter\n", __func__));
5584 brcmf_sdio_unregister();
5587 static int brcmf_sdbrcm_download_code_file(struct dhd_bus *bus, char *fw_path)
5593 u8 *memblock = NULL, *memptr;
5595 DHD_INFO(("%s: download firmware %s\n", __func__, brcmf_fw_path));
5597 image = brcmf_os_open_image(fw_path);
5601 memptr = memblock = kmalloc(MEMBLOCK + BRCMF_SDALIGN, GFP_ATOMIC);
5602 if (memblock == NULL) {
5603 DHD_ERROR(("%s: Failed to allocate memory %d bytes\n",
5604 __func__, MEMBLOCK));
5607 if ((u32)(unsigned long)memblock % BRCMF_SDALIGN)
5609 (BRCMF_SDALIGN - ((u32)(unsigned long)memblock % BRCMF_SDALIGN));
5611 /* Download image */
5613 brcmf_os_get_image_block((char *)memptr, MEMBLOCK, image))) {
5614 bcmerror = brcmf_sdbrcm_membytes(bus, true, offset, memptr,
5617 DHD_ERROR(("%s: error %d on writing %d membytes at "
5618 "0x%08x\n", __func__, bcmerror, MEMBLOCK, offset));
5629 brcmf_os_close_image(image);
5635 * ProcessVars:Takes a buffer of "<var>=<value>\n" lines read from a file
5636 * and ending in a NUL.
5637 * Removes carriage returns, empty lines, comment lines, and converts
5639 * Shortens buffer as needed and pads with NULs. End of buffer is marked
5643 static uint brcmf_process_nvram_vars(char *varbuf, uint len)
5652 findNewline = false;
5655 for (n = 0; n < len; n++) {
5658 if (varbuf[n] == '\r')
5660 if (findNewline && varbuf[n] != '\n')
5662 findNewline = false;
5663 if (varbuf[n] == '#') {
5667 if (varbuf[n] == '\n') {
5677 buf_len = dp - varbuf;
5679 while (dp < varbuf + n)
5686 EXAMPLE: nvram_array
5689 Use carriage return at the end of each assignment,
5690 and an empty string with
5691 carriage return at the end of array.
5694 unsigned char nvram_array[] = {"name1=value1\n",
5695 "name2=value2\n", "\n"};
5696 Hex values start with 0x, and mac addr format: xx:xx:xx:xx:xx:xx.
5698 Search "EXAMPLE: nvram_array" to see how the array is activated.
5701 void dhd_bus_set_nvram_params(struct dhd_bus *bus, const char *nvram_params)
5703 bus->nvram_params = nvram_params;
5706 static int brcmf_sdbrcm_download_nvram(struct dhd_bus *bus)
5711 char *memblock = NULL;
5714 bool nvram_file_exists;
5716 nv_path = bus->nv_path;
5718 nvram_file_exists = ((nv_path != NULL) && (nv_path[0] != '\0'));
5719 if (!nvram_file_exists && (bus->nvram_params == NULL))
5722 if (nvram_file_exists) {
5723 image = brcmf_os_open_image(nv_path);
5728 memblock = kmalloc(MEMBLOCK, GFP_ATOMIC);
5729 if (memblock == NULL) {
5730 DHD_ERROR(("%s: Failed to allocate memory %d bytes\n",
5731 __func__, MEMBLOCK));
5735 /* Download variables */
5736 if (nvram_file_exists) {
5737 len = brcmf_os_get_image_block(memblock, MEMBLOCK, image);
5739 len = strlen(bus->nvram_params);
5740 ASSERT(len <= MEMBLOCK);
5743 memcpy(memblock, bus->nvram_params, len);
5746 if (len > 0 && len < MEMBLOCK) {
5747 bufp = (char *)memblock;
5749 len = brcmf_process_nvram_vars(bufp, len);
5753 bcmerror = brcmf_sdbrcm_downloadvars(bus, memblock,
5756 DHD_ERROR(("%s: error downloading vars: %d\n",
5757 __func__, bcmerror));
5760 DHD_ERROR(("%s: error reading nvram file: %d\n",
5769 brcmf_os_close_image(image);
5774 static int _brcmf_sdbrcm_download_firmware(struct dhd_bus *bus)
5778 bool embed = false; /* download embedded firmware */
5779 bool dlok = false; /* download firmware succeeded */
5781 /* Out immediately if no image to download */
5782 if ((bus->fw_path == NULL) || (bus->fw_path[0] == '\0'))
5785 /* Keep arm in reset */
5786 if (brcmf_sdbrcm_download_state(bus, true)) {
5787 DHD_ERROR(("%s: error placing ARM core in reset\n", __func__));
5791 /* External image takes precedence if specified */
5792 if ((bus->fw_path != NULL) && (bus->fw_path[0] != '\0')) {
5793 if (brcmf_sdbrcm_download_code_file(bus, bus->fw_path)) {
5794 DHD_ERROR(("%s: dongle image file download failed\n",
5803 DHD_ERROR(("%s: dongle image download failed\n", __func__));
5807 /* EXAMPLE: nvram_array */
5808 /* If a valid nvram_arry is specified as above, it can be passed
5810 /* dhd_bus_set_nvram_params(bus, (char *)&nvram_array); */
5812 /* External nvram takes precedence if specified */
5813 if (brcmf_sdbrcm_download_nvram(bus)) {
5814 DHD_ERROR(("%s: dongle nvram file download failed\n",
5818 /* Take arm out of reset */
5819 if (brcmf_sdbrcm_download_state(bus, false)) {
5820 DHD_ERROR(("%s: error getting out of ARM core reset\n",
5833 brcmf_sdbrcm_send_buf(dhd_bus_t *bus, u32 addr, uint fn, uint flags,
5834 u8 *buf, uint nbytes, struct sk_buff *pkt,
5835 bcmsdh_cmplt_fn_t complete, void *handle)
5837 return brcmf_sdcard_send_buf
5838 (bus->sdh, addr, fn, flags, buf, nbytes, pkt, complete,
5842 uint dhd_bus_chip(struct dhd_bus *bus)
5844 ASSERT(bus->ci != NULL);
5845 return bus->ci->chip;
5848 void *dhd_bus_pub(struct dhd_bus *bus)
5853 void *dhd_bus_txq(struct dhd_bus *bus)
5858 uint dhd_bus_hdrlen(struct dhd_bus *bus)
5860 return SDPCM_HDRLEN;
5863 int brcmf_bus_devreset(dhd_pub_t *dhdp, u8 flag)
5871 if (!bus->dhd->dongle_reset) {
5872 /* Expect app to have torn down any
5873 connection before calling */
5874 /* Stop the bus, disable F2 */
5875 brcmf_sdbrcm_bus_stop(bus, false);
5877 /* Clean tx/rx buffer pointers,
5878 detach from the dongle */
5879 brcmf_sdbrcm_release_dongle(bus);
5881 bus->dhd->dongle_reset = true;
5882 bus->dhd->up = false;
5884 DHD_TRACE(("%s: WLAN OFF DONE\n", __func__));
5885 /* App can now remove power from device */
5889 /* App must have restored power to device before calling */
5891 DHD_TRACE(("\n\n%s: == WLAN ON ==\n", __func__));
5893 if (bus->dhd->dongle_reset) {
5895 /* Reset SD client */
5896 brcmf_sdcard_reset(bus->sdh);
5898 /* Attempt to re-attach & download */
5899 if (brcmf_sdbrcm_probe_attach(bus, bus->sdh,
5900 (u32 *) SI_ENUM_BASE,
5902 /* Attempt to download binary to the dongle */
5903 if (brcmf_sdbrcm_probe_init
5905 && brcmf_sdbrcm_download_firmware(bus,
5908 /* Re-init bus, enable F2 transfer */
5909 brcmf_sdbrcm_bus_init(
5910 (dhd_pub_t *) bus->dhd, false);
5912 bus->dhd->dongle_reset = false;
5913 bus->dhd->up = true;
5915 DHD_TRACE(("%s: WLAN ON DONE\n",
5922 bcmerror = -EISCONN;
5923 DHD_ERROR(("%s: Set DEVRESET=false invoked when device "
5924 "is on\n", __func__));
5932 brcmf_sdbrcm_chip_recognition(struct brcmf_sdio *sdh, struct chip_info *ci,
5939 * Chipid is assume to be at offset 0 from regs arg
5940 * For different chiptypes or old sdio hosts w/o chipcommon,
5941 * other ways of recognition should be added here.
5943 ci->cccorebase = (u32)regs;
5944 regdata = brcmf_sdcard_reg_read(sdh,
5945 CORE_CC_REG(ci->cccorebase, chipid), 4);
5946 ci->chip = regdata & CID_ID_MASK;
5947 ci->chiprev = (regdata & CID_REV_MASK) >> CID_REV_SHIFT;
5949 DHD_INFO(("%s: chipid=0x%x chiprev=%d\n",
5950 __func__, ci->chip, ci->chiprev));
5952 /* Address of cores for new chips should be added here */
5954 case BCM4329_CHIP_ID:
5955 ci->buscorebase = BCM4329_CORE_BUS_BASE;
5956 ci->ramcorebase = BCM4329_CORE_SOCRAM_BASE;
5957 ci->armcorebase = BCM4329_CORE_ARM_BASE;
5958 ci->ramsize = BCM4329_RAMSIZE;
5961 DHD_ERROR(("%s: chipid 0x%x is not supported\n",
5962 __func__, ci->chip));
5966 regdata = brcmf_sdcard_reg_read(sdh,
5967 CORE_SB(ci->cccorebase, sbidhigh), 4);
5968 ci->ccrev = SBCOREREV(regdata);
5970 regdata = brcmf_sdcard_reg_read(sdh,
5971 CORE_CC_REG(ci->cccorebase, pmucapabilities), 4);
5972 ci->pmurev = regdata & PCAP_REV_MASK;
5974 regdata = brcmf_sdcard_reg_read(sdh,
5975 CORE_SB(ci->buscorebase, sbidhigh), 4);
5976 ci->buscorerev = SBCOREREV(regdata);
5977 ci->buscoretype = (regdata & SBIDH_CC_MASK) >> SBIDH_CC_SHIFT;
5979 DHD_INFO(("%s: ccrev=%d, pmurev=%d, buscore rev/type=%d/0x%x\n",
5980 __func__, ci->ccrev, ci->pmurev,
5981 ci->buscorerev, ci->buscoretype));
5983 /* get chipcommon capabilites */
5984 ci->cccaps = brcmf_sdcard_reg_read(sdh,
5985 CORE_CC_REG(ci->cccorebase, capabilities), 4);
5991 brcmf_sdbrcm_chip_disablecore(struct brcmf_sdio *sdh, u32 corebase)
5995 regdata = brcmf_sdcard_reg_read(sdh,
5996 CORE_SB(corebase, sbtmstatelow), 4);
5997 if (regdata & SBTML_RESET)
6000 regdata = brcmf_sdcard_reg_read(sdh,
6001 CORE_SB(corebase, sbtmstatelow), 4);
6002 if ((regdata & (SICF_CLOCK_EN << SBTML_SICF_SHIFT)) != 0) {
6004 * set target reject and spin until busy is clear
6005 * (preserve core-specific bits)
6007 regdata = brcmf_sdcard_reg_read(sdh,
6008 CORE_SB(corebase, sbtmstatelow), 4);
6009 brcmf_sdcard_reg_write(sdh, CORE_SB(corebase, sbtmstatelow), 4,
6010 regdata | SBTML_REJ);
6012 regdata = brcmf_sdcard_reg_read(sdh,
6013 CORE_SB(corebase, sbtmstatelow), 4);
6015 SPINWAIT((brcmf_sdcard_reg_read(sdh,
6016 CORE_SB(corebase, sbtmstatehigh), 4) &
6017 SBTMH_BUSY), 100000);
6019 regdata = brcmf_sdcard_reg_read(sdh,
6020 CORE_SB(corebase, sbtmstatehigh), 4);
6021 if (regdata & SBTMH_BUSY)
6022 DHD_ERROR(("%s: ARM core still busy\n", __func__));
6024 regdata = brcmf_sdcard_reg_read(sdh,
6025 CORE_SB(corebase, sbidlow), 4);
6026 if (regdata & SBIDL_INIT) {
6027 regdata = brcmf_sdcard_reg_read(sdh,
6028 CORE_SB(corebase, sbimstate), 4) |
6030 brcmf_sdcard_reg_write(sdh,
6031 CORE_SB(corebase, sbimstate), 4,
6033 regdata = brcmf_sdcard_reg_read(sdh,
6034 CORE_SB(corebase, sbimstate), 4);
6036 SPINWAIT((brcmf_sdcard_reg_read(sdh,
6037 CORE_SB(corebase, sbimstate), 4) &
6041 /* set reset and reject while enabling the clocks */
6042 brcmf_sdcard_reg_write(sdh,
6043 CORE_SB(corebase, sbtmstatelow), 4,
6044 (((SICF_FGC | SICF_CLOCK_EN) << SBTML_SICF_SHIFT) |
6045 SBTML_REJ | SBTML_RESET));
6046 regdata = brcmf_sdcard_reg_read(sdh,
6047 CORE_SB(corebase, sbtmstatelow), 4);
6050 /* clear the initiator reject bit */
6051 regdata = brcmf_sdcard_reg_read(sdh,
6052 CORE_SB(corebase, sbidlow), 4);
6053 if (regdata & SBIDL_INIT) {
6054 regdata = brcmf_sdcard_reg_read(sdh,
6055 CORE_SB(corebase, sbimstate), 4) &
6057 brcmf_sdcard_reg_write(sdh,
6058 CORE_SB(corebase, sbimstate), 4,
6063 /* leave reset and reject asserted */
6064 brcmf_sdcard_reg_write(sdh, CORE_SB(corebase, sbtmstatelow), 4,
6065 (SBTML_REJ | SBTML_RESET));
6070 brcmf_sdbrcm_chip_attach(struct dhd_bus *bus, void *regs)
6072 struct chip_info *ci;
6076 DHD_TRACE(("%s: Enter\n", __func__));
6078 /* alloc chip_info_t */
6079 ci = kmalloc(sizeof(struct chip_info), GFP_ATOMIC);
6081 DHD_ERROR(("%s: malloc failed!\n", __func__));
6085 memset((unsigned char *)ci, 0, sizeof(struct chip_info));
6087 /* bus/core/clk setup for register access */
6088 /* Try forcing SDIO core to do ALPAvail request only */
6089 clkset = SBSDIO_FORCE_HW_CLKREQ_OFF | SBSDIO_ALP_AVAIL_REQ;
6090 brcmf_sdcard_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
6093 DHD_ERROR(("%s: error writing for HT off\n", __func__));
6097 /* If register supported, wait for ALPAvail and then force ALP */
6098 /* This may take up to 15 milliseconds */
6099 clkval = brcmf_sdcard_cfg_read(bus->sdh, SDIO_FUNC_1,
6100 SBSDIO_FUNC1_CHIPCLKCSR, NULL);
6101 if ((clkval & ~SBSDIO_AVBITS) == clkset) {
6103 brcmf_sdcard_cfg_read(bus->sdh, SDIO_FUNC_1,
6104 SBSDIO_FUNC1_CHIPCLKCSR,
6106 !SBSDIO_ALPAV(clkval)),
6107 PMU_MAX_TRANSITION_DLY);
6108 if (!SBSDIO_ALPAV(clkval)) {
6109 DHD_ERROR(("%s: timeout on ALPAV wait, clkval 0x%02x\n",
6114 clkset = SBSDIO_FORCE_HW_CLKREQ_OFF |
6116 brcmf_sdcard_cfg_write(bus->sdh, SDIO_FUNC_1,
6117 SBSDIO_FUNC1_CHIPCLKCSR,
6121 DHD_ERROR(("%s: ChipClkCSR access: wrote 0x%02x read 0x%02x\n",
6122 __func__, clkset, clkval));
6127 /* Also, disable the extra SDIO pull-ups */
6128 brcmf_sdcard_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_SDIOPULLUP,
6131 err = brcmf_sdbrcm_chip_recognition(bus->sdh, ci, regs);
6136 * Make sure any on-chip ARM is off (in case strapping is wrong),
6137 * or downloaded code was already running.
6139 brcmf_sdbrcm_chip_disablecore(bus->sdh, ci->armcorebase);
6141 brcmf_sdcard_reg_write(bus->sdh,
6142 CORE_CC_REG(ci->cccorebase, gpiopullup), 4, 0);
6143 brcmf_sdcard_reg_write(bus->sdh,
6144 CORE_CC_REG(ci->cccorebase, gpiopulldown), 4, 0);
6146 /* Disable F2 to clear any intermediate frame state on the dongle */
6147 brcmf_sdcard_cfg_write(bus->sdh, SDIO_FUNC_0, SDIO_CCCR_IOEx,
6148 SDIO_FUNC_ENABLE_1, NULL);
6150 /* WAR: cmd52 backplane read so core HW will drop ALPReq */
6151 clkval = brcmf_sdcard_cfg_read(bus->sdh, SDIO_FUNC_1,
6154 /* Done with backplane-dependent accesses, can drop clock... */
6155 brcmf_sdcard_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
6167 brcmf_sdbrcm_chip_resetcore(struct brcmf_sdio *sdh, u32 corebase)
6172 * Must do the disable sequence first to work for
6173 * arbitrary current core state.
6175 brcmf_sdbrcm_chip_disablecore(sdh, corebase);
6178 * Now do the initialization sequence.
6179 * set reset while enabling the clock and
6180 * forcing them on throughout the core
6182 brcmf_sdcard_reg_write(sdh, CORE_SB(corebase, sbtmstatelow), 4,
6183 ((SICF_FGC | SICF_CLOCK_EN) << SBTML_SICF_SHIFT) |
6187 regdata = brcmf_sdcard_reg_read(sdh, CORE_SB(corebase, sbtmstatehigh),
6189 if (regdata & SBTMH_SERR)
6190 brcmf_sdcard_reg_write(sdh, CORE_SB(corebase, sbtmstatehigh),
6193 regdata = brcmf_sdcard_reg_read(sdh, CORE_SB(corebase, sbimstate), 4);
6194 if (regdata & (SBIM_IBE | SBIM_TO))
6195 brcmf_sdcard_reg_write(sdh, CORE_SB(corebase, sbimstate), 4,
6196 regdata & ~(SBIM_IBE | SBIM_TO));
6198 /* clear reset and allow it to propagate throughout the core */
6199 brcmf_sdcard_reg_write(sdh, CORE_SB(corebase, sbtmstatelow), 4,
6200 (SICF_FGC << SBTML_SICF_SHIFT) |
6201 (SICF_CLOCK_EN << SBTML_SICF_SHIFT));
6204 /* leave clock enabled */
6205 brcmf_sdcard_reg_write(sdh, CORE_SB(corebase, sbtmstatelow), 4,
6206 (SICF_CLOCK_EN << SBTML_SICF_SHIFT));
6210 /* SDIO Pad drive strength to select value mappings */
6211 struct sdiod_drive_str {
6212 u8 strength; /* Pad Drive Strength in mA */
6213 u8 sel; /* Chip-specific select value */
6216 /* SDIO Drive Strength to sel value table for PMU Rev 1 */
6217 static const struct sdiod_drive_str sdiod_drive_strength_tab1[] = {
6225 /* SDIO Drive Strength to sel value table for PMU Rev 2, 3 */
6226 static const struct sdiod_drive_str sdiod_drive_strength_tab2[] = {
6237 /* SDIO Drive Strength to sel value table for PMU Rev 8 (1.8V) */
6238 static const struct sdiod_drive_str sdiod_drive_strength_tab3[] = {
6250 #define SDIOD_DRVSTR_KEY(chip, pmu) (((chip) << 16) | (pmu))
6253 brcmf_sdbrcm_sdiod_drive_strength_init(struct dhd_bus *bus, u32 drivestrength) {
6254 struct sdiod_drive_str *str_tab = NULL;
6259 if (!(bus->ci->cccaps & CC_CAP_PMU))
6262 switch (SDIOD_DRVSTR_KEY(bus->ci->chip, bus->ci->pmurev)) {
6263 case SDIOD_DRVSTR_KEY(BCM4325_CHIP_ID, 1):
6264 str_tab = (struct sdiod_drive_str *)&sdiod_drive_strength_tab1;
6265 str_mask = 0x30000000;
6268 case SDIOD_DRVSTR_KEY(BCM4325_CHIP_ID, 2):
6269 case SDIOD_DRVSTR_KEY(BCM4325_CHIP_ID, 3):
6270 str_tab = (struct sdiod_drive_str *)&sdiod_drive_strength_tab2;
6271 str_mask = 0x00003800;
6274 case SDIOD_DRVSTR_KEY(BCM4336_CHIP_ID, 8):
6275 str_tab = (struct sdiod_drive_str *)&sdiod_drive_strength_tab3;
6276 str_mask = 0x00003800;
6280 DHD_ERROR(("No SDIO Drive strength init"
6281 "done for chip %s rev %d pmurev %d\n",
6282 brcmu_chipname(bus->ci->chip, chn, 8),
6283 bus->ci->chiprev, bus->ci->pmurev));
6287 if (str_tab != NULL) {
6288 u32 drivestrength_sel = 0;
6292 for (i = 0; str_tab[i].strength != 0; i++) {
6293 if (drivestrength >= str_tab[i].strength) {
6294 drivestrength_sel = str_tab[i].sel;
6299 brcmf_sdcard_reg_write(bus->sdh,
6300 CORE_CC_REG(bus->ci->cccorebase, chipcontrol_addr),
6302 cc_data_temp = brcmf_sdcard_reg_read(bus->sdh,
6303 CORE_CC_REG(bus->ci->cccorebase, chipcontrol_addr), 4);
6304 cc_data_temp &= ~str_mask;
6305 drivestrength_sel <<= str_shift;
6306 cc_data_temp |= drivestrength_sel;
6307 brcmf_sdcard_reg_write(bus->sdh,
6308 CORE_CC_REG(bus->ci->cccorebase, chipcontrol_addr),
6311 DHD_INFO(("SDIO: %dmA drive strength selected, set to 0x%08x\n",
6312 drivestrength, cc_data_temp));
6317 brcmf_sdbrcm_chip_detach(struct dhd_bus *bus)
6319 DHD_TRACE(("%s: Enter\n", __func__));
6326 brcmf_sdbrcm_wait_for_event(dhd_pub_t *dhd, bool *lockvar)
6328 brcmf_os_sdunlock(dhd);
6329 wait_event_interruptible_timeout(dhd->bus->ctrl_wait,
6330 (*lockvar == false), HZ * 2);
6331 brcmf_os_sdlock(dhd);
6336 brcmf_sdbrcm_wait_event_wakeup(dhd_bus_t *bus)
6338 if (waitqueue_active(&bus->ctrl_wait))
6339 wake_up_interruptible(&bus->ctrl_wait);