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/pci_ids.h>
20 #include <linux/netdevice.h>
24 #include BCMEMBEDIMAGE
25 #endif /* BCMEMBEDIMAGE */
33 #include <hndrte_armtrap.h>
34 #include <hndrte_cons.h>
35 #endif /* DHD_DEBUG */
41 #include <sbsdpcmdev.h>
44 #include <proto/802.11.h>
46 #include <dngl_stats.h>
49 #include <dhd_proto.h>
55 #ifndef DHDSDIO_MEM_DUMP_FNAME
56 #define DHDSDIO_MEM_DUMP_FNAME "mem_dump"
59 #define TXQLEN 2048 /* bulk tx queue length */
60 #define TXHI (TXQLEN - 256) /* turn on flow control above TXHI */
61 #define TXLOW (TXHI - 256) /* turn off flow control below TXLOW */
64 #define TXRETRIES 2 /* # of retries for tx frames */
66 #if defined(CONFIG_MACH_SANDGATE2G)
67 #define DHD_RXBOUND 250 /* Default for max rx frames in
70 #define DHD_RXBOUND 50 /* Default for max rx frames in
72 #endif /* defined(CONFIG_MACH_SANDGATE2G) */
74 #define DHD_TXBOUND 20 /* Default for max tx frames in
77 #define DHD_TXMINMAX 1 /* Max tx frames if rx still pending */
79 #define MEMBLOCK 2048 /* Block size used for downloading
81 #define MAX_DATA_BUF (32 * 1024) /* Must be large enough to hold
82 biggest possible glom */
84 /* Packet alignment for most efficient SDIO (can change based on platform) */
86 #define DHD_SDALIGN 32
88 #if !ISPOWEROF2(DHD_SDALIGN)
89 #error DHD_SDALIGN is not a power of 2!
93 #define DHD_FIRSTREAD 32
95 #if !ISPOWEROF2(DHD_FIRSTREAD)
96 #error DHD_FIRSTREAD is not a power of 2!
99 /* Total length of frame header for dongle protocol */
100 #define SDPCM_HDRLEN (SDPCM_FRAMETAG_LEN + SDPCM_SWHEADER_LEN)
102 #define SDPCM_RESERVE (SDPCM_HDRLEN + SDPCM_TEST_HDRLEN + DHD_SDALIGN)
104 #define SDPCM_RESERVE (SDPCM_HDRLEN + DHD_SDALIGN)
107 /* Space for header read, limit for data packets */
109 #define MAX_HDR_READ 32
111 #if !ISPOWEROF2(MAX_HDR_READ)
112 #error MAX_HDR_READ is not a power of 2!
115 #define MAX_RX_DATASZ 2048
117 /* Maximum milliseconds to wait for F2 to come up */
118 #define DHD_WAIT_F2RDY 3000
120 /* Bump up limit on waiting for HT to account for first startup;
121 * if the image is doing a CRC calculation before programming the PMU
122 * for HT availability, it could take a couple hundred ms more, so
123 * max out at a 1 second (1000000us).
125 #if (PMU_MAX_TRANSITION_DLY <= 1000000)
126 #undef PMU_MAX_TRANSITION_DLY
127 #define PMU_MAX_TRANSITION_DLY 1000000
130 /* Value for ChipClockCSR during initial setup */
131 #define DHD_INIT_CLKCTL1 (SBSDIO_FORCE_HW_CLKREQ_OFF | \
132 SBSDIO_ALP_AVAIL_REQ)
133 #define DHD_INIT_CLKCTL2 (SBSDIO_FORCE_HW_CLKREQ_OFF | SBSDIO_FORCE_ALP)
135 /* Flags for SDH calls */
136 #define F2SYNC (SDIO_REQ_4BYTE | SDIO_REQ_FIXED)
138 /* Packet free applicable unconditionally for sdio and sdspi. Conditional if
139 * bufpool was present for gspi bus.
141 #define PKTFREE2() if ((bus->bus != SPI_BUS) || bus->usebufpool) \
142 pkt_buf_free_skb(pkt);
145 * Conversion of 802.1D priority to precedence level
147 #define PRIO2PREC(prio) \
148 (((prio) == PRIO_8021D_NONE || (prio) == PRIO_8021D_BE) ? \
151 DHD_SPINWAIT_SLEEP_INIT(sdioh_spinwait_sleep);
152 extern int dhdcdc_set_ioctl(dhd_pub_t *dhd, int ifidx, uint cmd, void *buf,
156 /* Device console log buffer state */
157 typedef struct dhd_console {
158 uint count; /* Poll interval msec counter */
159 uint log_addr; /* Log struct address (fixed) */
160 hndrte_log_t log; /* Log struct (host copy) */
161 uint bufsize; /* Size of log buffer */
162 u8 *buf; /* Log buffer (host copy) */
163 uint last; /* Last buffer read index */
165 #endif /* DHD_DEBUG */
167 /* misc chip info needed by some of the routines */
183 /* Private data for SDIO bus interaction */
184 typedef struct dhd_bus {
187 bcmsdh_info_t *sdh; /* Handle for BCMSDH calls */
188 struct chip_info *ci; /* Chip info struct */
189 char *vars; /* Variables (from CIS and/or other) */
190 uint varsz; /* Size of variables buffer */
191 u32 sbaddr; /* Current SB window pointer (-1, invalid) */
193 sdpcmd_regs_t *regs; /* Registers for SDIO core */
194 uint sdpcmrev; /* SDIO core revision */
195 uint armrev; /* CPU core revision */
196 uint ramrev; /* SOCRAM core revision */
197 u32 ramsize; /* Size of RAM in SOCRAM (bytes) */
198 u32 orig_ramsize; /* Size of RAM in SOCRAM (bytes) */
200 u32 bus; /* gSPI or SDIO bus */
201 u32 hostintmask; /* Copy of Host Interrupt Mask */
202 u32 intstatus; /* Intstatus bits (events) pending */
203 bool dpc_sched; /* Indicates DPC schedule (intrpt rcvd) */
204 bool fcstate; /* State of dongle flow-control */
206 u16 cl_devid; /* cached devid for dhdsdio_probe_attach() */
207 char *fw_path; /* module_param: path to firmware image */
208 char *nv_path; /* module_param: path to nvram vars file */
209 const char *nvram_params; /* user specified nvram params. */
211 uint blocksize; /* Block size of SDIO transfers */
212 uint roundup; /* Max roundup limit */
214 struct pktq txq; /* Queue length used for flow-control */
215 u8 flowcontrol; /* per prio flow control bitmask */
216 u8 tx_seq; /* Transmit sequence number (next) */
217 u8 tx_max; /* Maximum transmit sequence allowed */
219 u8 hdrbuf[MAX_HDR_READ + DHD_SDALIGN];
220 u8 *rxhdr; /* Header of current rx frame (in hdrbuf) */
221 u16 nextlen; /* Next Read Len from last header */
222 u8 rx_seq; /* Receive sequence number (expected) */
223 bool rxskip; /* Skip receive (awaiting NAK ACK) */
225 struct sk_buff *glomd; /* Packet containing glomming descriptor */
226 struct sk_buff *glom; /* Packet chain for glommed superframe */
227 uint glomerr; /* Glom packet read errors */
229 u8 *rxbuf; /* Buffer for receiving control packets */
230 uint rxblen; /* Allocated length of rxbuf */
231 u8 *rxctl; /* Aligned pointer into rxbuf */
232 u8 *databuf; /* Buffer for receiving big glom packet */
233 u8 *dataptr; /* Aligned pointer into databuf */
234 uint rxlen; /* Length of valid data in buffer */
236 u8 sdpcm_ver; /* Bus protocol reported by dongle */
238 bool intr; /* Use interrupts */
239 bool poll; /* Use polling */
240 bool ipend; /* Device interrupt is pending */
241 bool intdis; /* Interrupts disabled by isr */
242 uint intrcount; /* Count of device interrupt callbacks */
243 uint lastintrs; /* Count as of last watchdog timer */
244 uint spurious; /* Count of spurious interrupts */
245 uint pollrate; /* Ticks between device polls */
246 uint polltick; /* Tick counter */
247 uint pollcnt; /* Count of active polls */
250 dhd_console_t console; /* Console output polling support */
251 uint console_addr; /* Console address from shared struct */
252 #endif /* DHD_DEBUG */
254 uint regfails; /* Count of R_REG/W_REG failures */
256 uint clkstate; /* State of sd and backplane clock(s) */
257 bool activity; /* Activity flag for clock down */
258 s32 idletime; /* Control for activity timeout */
259 s32 idlecount; /* Activity timeout counter */
260 s32 idleclock; /* How to set bus driver when idle */
261 s32 sd_divisor; /* Speed control to bus driver */
262 s32 sd_mode; /* Mode control to bus driver */
263 s32 sd_rxchain; /* If bcmsdh api accepts PKT chains */
264 bool use_rxchain; /* If dhd should use PKT chains */
265 bool sleeping; /* Is SDIO bus sleeping? */
266 bool rxflow_mode; /* Rx flow control mode */
267 bool rxflow; /* Is rx flow control on */
268 uint prev_rxlim_hit; /* Is prev rx limit exceeded
269 (per dpc schedule) */
270 bool alp_only; /* Don't use HT clock (ALP only) */
271 /* Field to decide if rx of control frames happen in rxbuf or lb-pool */
275 /* external loopback */
279 /* pktgen configuration */
280 uint pktgen_freq; /* Ticks between bursts */
281 uint pktgen_count; /* Packets to send each burst */
282 uint pktgen_print; /* Bursts between count displays */
283 uint pktgen_total; /* Stop after this many */
284 uint pktgen_minlen; /* Minimum packet data len */
285 uint pktgen_maxlen; /* Maximum packet data len */
286 uint pktgen_mode; /* Configured mode: tx, rx, or echo */
287 uint pktgen_stop; /* Number of tx failures causing stop */
289 /* active pktgen fields */
290 uint pktgen_tick; /* Tick counter for bursts */
291 uint pktgen_ptick; /* Burst counter for printing */
292 uint pktgen_sent; /* Number of test packets generated */
293 uint pktgen_rcvd; /* Number of test packets received */
294 uint pktgen_fail; /* Number of failed send attempts */
295 u16 pktgen_len; /* Length of next packet to send */
298 /* Some additional counters */
299 uint tx_sderrs; /* Count of tx attempts with sd errors */
300 uint fcqueued; /* Tx packets that got queued */
301 uint rxrtx; /* Count of rtx requests (NAK to dongle) */
302 uint rx_toolong; /* Receive frames too long to receive */
303 uint rxc_errors; /* SDIO errors when reading control frames */
304 uint rx_hdrfail; /* SDIO errors on header reads */
305 uint rx_badhdr; /* Bad received headers (roosync?) */
306 uint rx_badseq; /* Mismatched rx sequence number */
307 uint fc_rcvd; /* Number of flow-control events received */
308 uint fc_xoff; /* Number which turned on flow-control */
309 uint fc_xon; /* Number which turned off flow-control */
310 uint rxglomfail; /* Failed deglom attempts */
311 uint rxglomframes; /* Number of glom frames (superframes) */
312 uint rxglompkts; /* Number of packets from glom frames */
313 uint f2rxhdrs; /* Number of header reads */
314 uint f2rxdata; /* Number of frame data reads */
315 uint f2txdata; /* Number of f2 frame writes */
316 uint f1regdata; /* Number of f1 register accesses */
320 bool ctrl_frame_stat;
326 #define CLK_PENDING 2 /* Not used yet */
329 #define DHD_NOPMU(dhd) (false)
332 static int qcount[NUMPRIO];
333 static int tx_packets[NUMPRIO];
334 #endif /* DHD_DEBUG */
336 /* Deferred transmit */
337 const uint dhd_deferred_tx = 1;
339 extern uint dhd_watchdog_ms;
340 extern void dhd_os_wd_timer(void *bus, uint wdtick);
347 /* override the RAM size if possible */
348 #define DONGLE_MIN_MEMSIZE (128 * 1024)
349 int dhd_dongle_memsize;
351 static bool dhd_alignctl;
355 static bool retrydata;
356 #define RETRYCHAN(chan) (((chan) == SDPCM_EVENT_CHANNEL) || retrydata)
358 static const uint watermark = 8;
359 static const uint firstread = DHD_FIRSTREAD;
361 #define HDATLEN (firstread - (SDPCM_HDRLEN))
363 /* Retry count for register access failures */
364 static const uint retry_limit = 2;
366 /* Force even SD lengths (some host controllers mess up on odd bytes) */
367 static bool forcealign;
371 #if defined(OOB_INTR_ONLY) && defined(HW_OOB)
372 extern void bcmsdh_enable_hw_oob_intr(void *sdh, bool enable);
375 #if defined(OOB_INTR_ONLY) && defined(SDIO_ISR_THREAD)
376 #error OOB_INTR_ONLY is NOT working with SDIO_ISR_THREAD
377 #endif /* defined(OOB_INTR_ONLY) && defined(SDIO_ISR_THREAD) */
378 #define PKTALIGN(_p, _len, _align) \
381 datalign = (unsigned long)((_p)->data); \
382 datalign = roundup(datalign, (_align)) - datalign; \
383 ASSERT(datalign < (_align)); \
384 ASSERT((_p)->len >= ((_len) + datalign)); \
386 skb_pull((_p), datalign); \
387 __skb_trim((_p), (_len)); \
390 /* Limit on rounding up frames */
391 static const uint max_roundup = 512;
393 /* Try doing readahead */
394 static bool dhd_readahead;
396 /* To check if there's window offered */
397 #define DATAOK(bus) \
398 (((u8)(bus->tx_max - bus->tx_seq) != 0) && \
399 (((u8)(bus->tx_max - bus->tx_seq) & 0x80) == 0))
401 /* Macros to get register read/write status */
402 /* NOTE: these assume a local dhdsdio_bus_t *bus! */
403 #define R_SDREG(regvar, regaddr, retryvar) \
407 regvar = R_REG(regaddr); \
408 } while (bcmsdh_regfail(bus->sdh) && (++retryvar <= retry_limit)); \
410 bus->regfails += (retryvar-1); \
411 if (retryvar > retry_limit) { \
412 DHD_ERROR(("%s: FAILED" #regvar "READ, LINE %d\n", \
413 __func__, __LINE__)); \
419 #define W_SDREG(regval, regaddr, retryvar) \
423 W_REG(regaddr, regval); \
424 } while (bcmsdh_regfail(bus->sdh) && (++retryvar <= retry_limit)); \
426 bus->regfails += (retryvar-1); \
427 if (retryvar > retry_limit) \
428 DHD_ERROR(("%s: FAILED REGISTER WRITE, LINE %d\n", \
429 __func__, __LINE__)); \
433 #define DHD_BUS SDIO_BUS
435 #define PKT_AVAILABLE() (intstatus & I_HMB_FRAME_IND)
437 #define HOSTINTMASK (I_HMB_SW_MASK | I_CHIPACTIVE)
439 #define GSPI_PR55150_BAILOUT
442 static void dhdsdio_testrcv(dhd_bus_t *bus, void *pkt, uint seq);
443 static void dhdsdio_sdtest_set(dhd_bus_t *bus, bool start);
447 static int dhdsdio_checkdied(dhd_bus_t *bus, u8 *data, uint size);
448 static int dhdsdio_mem_dump(dhd_bus_t *bus);
449 #endif /* DHD_DEBUG */
450 static int dhdsdio_download_state(dhd_bus_t *bus, bool enter);
452 static void dhdsdio_release(dhd_bus_t *bus);
453 static void dhdsdio_release_malloc(dhd_bus_t *bus);
454 static void dhdsdio_disconnect(void *ptr);
455 static bool dhdsdio_chipmatch(u16 chipid);
456 static bool dhdsdio_probe_attach(dhd_bus_t *bus, void *sdh,
457 void *regsva, u16 devid);
458 static bool dhdsdio_probe_malloc(dhd_bus_t *bus, void *sdh);
459 static bool dhdsdio_probe_init(dhd_bus_t *bus, void *sdh);
460 static void dhdsdio_release_dongle(dhd_bus_t *bus);
462 static uint process_nvram_vars(char *varbuf, uint len);
464 static void dhd_dongle_setmemsize(struct dhd_bus *bus, int mem_size);
465 static int dhd_bcmsdh_recv_buf(dhd_bus_t *bus, u32 addr, uint fn,
466 uint flags, u8 *buf, uint nbytes,
467 struct sk_buff *pkt, bcmsdh_cmplt_fn_t complete,
469 static int dhd_bcmsdh_send_buf(dhd_bus_t *bus, u32 addr, uint fn,
470 uint flags, u8 *buf, uint nbytes,
471 struct sk_buff *pkt, bcmsdh_cmplt_fn_t complete,
474 static bool dhdsdio_download_firmware(struct dhd_bus *bus, void *sdh);
475 static int _dhdsdio_download_firmware(struct dhd_bus *bus);
477 static int dhdsdio_download_code_file(struct dhd_bus *bus, char *image_path);
478 static int dhdsdio_download_nvram(struct dhd_bus *bus);
480 static int dhdsdio_download_code_array(struct dhd_bus *bus);
482 static void dhdsdio_chip_disablecore(bcmsdh_info_t *sdh, u32 corebase);
483 static int dhdsdio_chip_attach(struct dhd_bus *bus, void *regs);
484 static void dhdsdio_chip_resetcore(bcmsdh_info_t *sdh, u32 corebase);
485 static void dhdsdio_sdiod_drive_strength_init(struct dhd_bus *bus,
487 static void dhdsdio_chip_detach(struct dhd_bus *bus);
489 static void dhd_dongle_setmemsize(struct dhd_bus *bus, int mem_size)
491 s32 min_size = DONGLE_MIN_MEMSIZE;
492 /* Restrict the memsize to user specified limit */
493 DHD_ERROR(("user: Restrict the dongle ram size to %d, min %d\n",
494 dhd_dongle_memsize, min_size));
495 if ((dhd_dongle_memsize > min_size) &&
496 (dhd_dongle_memsize < (s32) bus->orig_ramsize))
497 bus->ramsize = dhd_dongle_memsize;
500 static int dhdsdio_set_siaddr_window(dhd_bus_t *bus, u32 address)
503 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_SBADDRLOW,
504 (address >> 8) & SBSDIO_SBADDRLOW_MASK, &err);
506 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_SBADDRMID,
507 (address >> 16) & SBSDIO_SBADDRMID_MASK, &err);
509 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_SBADDRHIGH,
510 (address >> 24) & SBSDIO_SBADDRHIGH_MASK,
515 /* Turn backplane clock on or off */
516 static int dhdsdio_htclk(dhd_bus_t *bus, bool on, bool pendok)
519 u8 clkctl, clkreq, devctl;
522 DHD_TRACE(("%s: Enter\n", __func__));
524 #if defined(OOB_INTR_ONLY)
531 /* Request HT Avail */
533 bus->alp_only ? SBSDIO_ALP_AVAIL_REQ : SBSDIO_HT_AVAIL_REQ;
535 if ((bus->ci->chip == BCM4329_CHIP_ID)
536 && (bus->ci->chiprev == 0))
537 clkreq |= SBSDIO_FORCE_ALP;
539 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
542 DHD_ERROR(("%s: HT Avail request error: %d\n",
547 if (pendok && ((bus->ci->buscoretype == PCMCIA_CORE_ID)
548 && (bus->ci->buscorerev == 9))) {
550 R_SDREG(dummy, &bus->regs->clockctlstatus, retries);
553 /* Check current status */
555 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
558 DHD_ERROR(("%s: HT Avail read error: %d\n",
563 /* Go to pending and await interrupt if appropriate */
564 if (!SBSDIO_CLKAV(clkctl, bus->alp_only) && pendok) {
565 /* Allow only clock-available interrupt */
567 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
570 DHD_ERROR(("%s: Devctl error setting CA: %d\n",
575 devctl |= SBSDIO_DEVCTL_CA_INT_ONLY;
576 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
578 DHD_INFO(("CLKCTL: set PENDING\n"));
579 bus->clkstate = CLK_PENDING;
582 } else if (bus->clkstate == CLK_PENDING) {
583 /* Cancel CA-only interrupt filter */
585 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
587 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
588 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
592 /* Otherwise, wait here (polling) for HT Avail */
593 if (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
594 SPINWAIT_SLEEP(sdioh_spinwait_sleep,
596 bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
597 SBSDIO_FUNC1_CHIPCLKCSR,
599 !SBSDIO_CLKAV(clkctl, bus->alp_only)),
600 PMU_MAX_TRANSITION_DLY);
603 DHD_ERROR(("%s: HT Avail request error: %d\n",
607 if (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
608 DHD_ERROR(("%s: HT Avail timeout (%d): clkctl 0x%02x\n",
609 __func__, PMU_MAX_TRANSITION_DLY, clkctl));
613 /* Mark clock available */
614 bus->clkstate = CLK_AVAIL;
615 DHD_INFO(("CLKCTL: turned ON\n"));
617 #if defined(DHD_DEBUG)
618 if (bus->alp_only == true) {
619 #if !defined(BCMLXSDMMC)
620 if (!SBSDIO_ALPONLY(clkctl)) {
621 DHD_ERROR(("%s: HT Clock, when ALP Only\n",
624 #endif /* !defined(BCMLXSDMMC) */
626 if (SBSDIO_ALPONLY(clkctl)) {
627 DHD_ERROR(("%s: HT Clock should be on.\n",
631 #endif /* defined (DHD_DEBUG) */
633 bus->activity = true;
637 if (bus->clkstate == CLK_PENDING) {
638 /* Cancel CA-only interrupt filter */
640 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
642 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
643 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
647 bus->clkstate = CLK_SDONLY;
648 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
650 DHD_INFO(("CLKCTL: turned OFF\n"));
652 DHD_ERROR(("%s: Failed access turning clock off: %d\n",
660 /* Change idle/active SD state */
661 static int dhdsdio_sdclk(dhd_bus_t *bus, bool on)
666 DHD_TRACE(("%s: Enter\n", __func__));
669 if (bus->idleclock == DHD_IDLE_STOP) {
670 /* Turn on clock and restore mode */
672 err = bcmsdh_iovar_op(bus->sdh, "sd_clock", NULL, 0,
673 &iovalue, sizeof(iovalue), true);
675 DHD_ERROR(("%s: error enabling sd_clock: %d\n",
680 iovalue = bus->sd_mode;
681 err = bcmsdh_iovar_op(bus->sdh, "sd_mode", NULL, 0,
682 &iovalue, sizeof(iovalue), true);
684 DHD_ERROR(("%s: error changing sd_mode: %d\n",
688 } else if (bus->idleclock != DHD_IDLE_ACTIVE) {
689 /* Restore clock speed */
690 iovalue = bus->sd_divisor;
691 err = bcmsdh_iovar_op(bus->sdh, "sd_divisor", NULL, 0,
692 &iovalue, sizeof(iovalue), true);
694 DHD_ERROR(("%s: error restoring sd_divisor: %d\n",
699 bus->clkstate = CLK_SDONLY;
701 /* Stop or slow the SD clock itself */
702 if ((bus->sd_divisor == -1) || (bus->sd_mode == -1)) {
703 DHD_TRACE(("%s: can't idle clock, divisor %d mode %d\n",
704 __func__, bus->sd_divisor, bus->sd_mode));
707 if (bus->idleclock == DHD_IDLE_STOP) {
709 /* Change to SD1 mode and turn off clock */
712 bcmsdh_iovar_op(bus->sdh, "sd_mode", NULL,
714 sizeof(iovalue), true);
716 DHD_ERROR(("%s: error changing sd_clock: %d\n",
723 err = bcmsdh_iovar_op(bus->sdh, "sd_clock", NULL, 0,
724 &iovalue, sizeof(iovalue), true);
726 DHD_ERROR(("%s: error disabling sd_clock: %d\n",
730 } else if (bus->idleclock != DHD_IDLE_ACTIVE) {
731 /* Set divisor to idle value */
732 iovalue = bus->idleclock;
733 err = bcmsdh_iovar_op(bus->sdh, "sd_divisor", NULL, 0,
734 &iovalue, sizeof(iovalue), true);
736 DHD_ERROR(("%s: error changing sd_divisor: %d\n",
741 bus->clkstate = CLK_NONE;
747 /* Transition SD and backplane clock readiness */
748 static int dhdsdio_clkctl(dhd_bus_t *bus, uint target, bool pendok)
751 uint oldstate = bus->clkstate;
752 #endif /* DHD_DEBUG */
754 DHD_TRACE(("%s: Enter\n", __func__));
756 /* Early exit if we're already there */
757 if (bus->clkstate == target) {
758 if (target == CLK_AVAIL) {
759 dhd_os_wd_timer(bus->dhd, dhd_watchdog_ms);
760 bus->activity = true;
767 /* Make sure SD clock is available */
768 if (bus->clkstate == CLK_NONE)
769 dhdsdio_sdclk(bus, true);
770 /* Now request HT Avail on the backplane */
771 dhdsdio_htclk(bus, true, pendok);
772 dhd_os_wd_timer(bus->dhd, dhd_watchdog_ms);
773 bus->activity = true;
777 /* Remove HT request, or bring up SD clock */
778 if (bus->clkstate == CLK_NONE)
779 dhdsdio_sdclk(bus, true);
780 else if (bus->clkstate == CLK_AVAIL)
781 dhdsdio_htclk(bus, false, false);
783 DHD_ERROR(("dhdsdio_clkctl: request for %d -> %d\n",
784 bus->clkstate, target));
785 dhd_os_wd_timer(bus->dhd, dhd_watchdog_ms);
789 /* Make sure to remove HT request */
790 if (bus->clkstate == CLK_AVAIL)
791 dhdsdio_htclk(bus, false, false);
792 /* Now remove the SD clock */
793 dhdsdio_sdclk(bus, false);
794 dhd_os_wd_timer(bus->dhd, 0);
798 DHD_INFO(("dhdsdio_clkctl: %d -> %d\n", oldstate, bus->clkstate));
799 #endif /* DHD_DEBUG */
804 int dhdsdio_bussleep(dhd_bus_t *bus, bool sleep)
806 bcmsdh_info_t *sdh = bus->sdh;
807 sdpcmd_regs_t *regs = bus->regs;
810 DHD_INFO(("dhdsdio_bussleep: request %s (currently %s)\n",
811 (sleep ? "SLEEP" : "WAKE"),
812 (bus->sleeping ? "SLEEP" : "WAKE")));
814 /* Done if we're already in the requested state */
815 if (sleep == bus->sleeping)
818 /* Going to sleep: set the alarm and turn off the lights... */
820 /* Don't sleep if something is pending */
821 if (bus->dpc_sched || bus->rxskip || pktq_len(&bus->txq))
824 /* Disable SDIO interrupts (no longer interested) */
825 bcmsdh_intr_disable(bus->sdh);
827 /* Make sure the controller has the bus up */
828 dhdsdio_clkctl(bus, CLK_AVAIL, false);
830 /* Tell device to start using OOB wakeup */
831 W_SDREG(SMB_USE_OOB, ®s->tosbmailbox, retries);
832 if (retries > retry_limit)
833 DHD_ERROR(("CANNOT SIGNAL CHIP, WILL NOT WAKE UP!!\n"));
835 /* Turn off our contribution to the HT clock request */
836 dhdsdio_clkctl(bus, CLK_SDONLY, false);
838 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
839 SBSDIO_FORCE_HW_CLKREQ_OFF, NULL);
841 /* Isolate the bus */
842 if (bus->ci->chip != BCM4329_CHIP_ID
843 && bus->ci->chip != BCM4319_CHIP_ID) {
844 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
845 SBSDIO_DEVCTL_PADS_ISO, NULL);
849 bus->sleeping = true;
852 /* Waking up: bus power up is ok, set local state */
854 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
857 /* Force pad isolation off if possible
858 (in case power never toggled) */
859 if ((bus->ci->buscoretype == PCMCIA_CORE_ID)
860 && (bus->ci->buscorerev >= 10))
861 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL, 0,
864 /* Make sure the controller has the bus up */
865 dhdsdio_clkctl(bus, CLK_AVAIL, false);
867 /* Send misc interrupt to indicate OOB not needed */
868 W_SDREG(0, ®s->tosbmailboxdata, retries);
869 if (retries <= retry_limit)
870 W_SDREG(SMB_DEV_INT, ®s->tosbmailbox, retries);
872 if (retries > retry_limit)
873 DHD_ERROR(("CANNOT SIGNAL CHIP TO CLEAR OOB!!\n"));
875 /* Make sure we have SD bus access */
876 dhdsdio_clkctl(bus, CLK_SDONLY, false);
879 bus->sleeping = false;
881 /* Enable interrupts again */
882 if (bus->intr && (bus->dhd->busstate == DHD_BUS_DATA)) {
884 bcmsdh_intr_enable(bus->sdh);
891 #if defined(OOB_INTR_ONLY)
892 void dhd_enable_oob_intr(struct dhd_bus *bus, bool enable)
895 bcmsdh_enable_hw_oob_intr(bus->sdh, enable);
897 sdpcmd_regs_t *regs = bus->regs;
900 dhdsdio_clkctl(bus, CLK_AVAIL, false);
901 if (enable == true) {
903 /* Tell device to start using OOB wakeup */
904 W_SDREG(SMB_USE_OOB, ®s->tosbmailbox, retries);
905 if (retries > retry_limit)
906 DHD_ERROR(("CANNOT SIGNAL CHIP, WILL NOT WAKE UP!!\n"));
909 /* Send misc interrupt to indicate OOB not needed */
910 W_SDREG(0, ®s->tosbmailboxdata, retries);
911 if (retries <= retry_limit)
912 W_SDREG(SMB_DEV_INT, ®s->tosbmailbox, retries);
915 /* Turn off our contribution to the HT clock request */
916 dhdsdio_clkctl(bus, CLK_SDONLY, false);
917 #endif /* !defined(HW_OOB) */
919 #endif /* defined(OOB_INTR_ONLY) */
921 #define BUS_WAKE(bus) \
923 if ((bus)->sleeping) \
924 dhdsdio_bussleep((bus), false); \
927 /* Writes a HW/SW header into the packet and sends it. */
928 /* Assumes: (a) header space already there, (b) caller holds lock */
929 static int dhdsdio_txpkt(dhd_bus_t *bus, struct sk_buff *pkt, uint chan,
941 DHD_TRACE(("%s: Enter\n", __func__));
945 if (bus->dhd->dongle_reset) {
946 ret = -BCME_NOTREADY;
950 frame = (u8 *) (pkt->data);
952 /* Add alignment padding, allocate new packet if needed */
953 pad = ((unsigned long)frame % DHD_SDALIGN);
955 if (skb_headroom(pkt) < pad) {
956 DHD_INFO(("%s: insufficient headroom %d for %d pad\n",
957 __func__, skb_headroom(pkt), pad));
958 bus->dhd->tx_realloc++;
959 new = pkt_buf_get_skb(pkt->len + DHD_SDALIGN);
961 DHD_ERROR(("%s: couldn't allocate new %d-byte "
963 __func__, pkt->len + DHD_SDALIGN));
968 PKTALIGN(new, pkt->len, DHD_SDALIGN);
969 memcpy(new->data, pkt->data, pkt->len);
971 pkt_buf_free_skb(pkt);
972 /* free the pkt if canned one is not used */
975 frame = (u8 *) (pkt->data);
976 ASSERT(((unsigned long)frame % DHD_SDALIGN) == 0);
980 frame = (u8 *) (pkt->data);
982 ASSERT((pad + SDPCM_HDRLEN) <= (int)(pkt->len));
983 memset(frame, 0, pad + SDPCM_HDRLEN);
986 ASSERT(pad < DHD_SDALIGN);
988 /* Hardware tag: 2 byte len followed by 2 byte ~len check (all LE) */
989 len = (u16) (pkt->len);
990 *(u16 *) frame = cpu_to_le16(len);
991 *(((u16 *) frame) + 1) = cpu_to_le16(~len);
993 /* Software tag: channel, sequence number, data offset */
995 ((chan << SDPCM_CHANNEL_SHIFT) & SDPCM_CHANNEL_MASK) | bus->tx_seq |
997 SDPCM_HDRLEN) << SDPCM_DOFFSET_SHIFT) & SDPCM_DOFFSET_MASK);
999 put_unaligned_le32(swheader, frame + SDPCM_FRAMETAG_LEN);
1000 put_unaligned_le32(0, frame + SDPCM_FRAMETAG_LEN + sizeof(swheader));
1003 tx_packets[pkt->priority]++;
1004 if (DHD_BYTES_ON() &&
1005 (((DHD_CTL_ON() && (chan == SDPCM_CONTROL_CHANNEL)) ||
1006 (DHD_DATA_ON() && (chan != SDPCM_CONTROL_CHANNEL))))) {
1007 prhex("Tx Frame", frame, len);
1008 } else if (DHD_HDRS_ON()) {
1009 prhex("TxHdr", frame, min_t(u16, len, 16));
1013 /* Raise len to next SDIO block to eliminate tail command */
1014 if (bus->roundup && bus->blocksize && (len > bus->blocksize)) {
1015 u16 pad = bus->blocksize - (len % bus->blocksize);
1016 if ((pad <= bus->roundup) && (pad < bus->blocksize))
1018 if (pad <= skb_tailroom(pkt))
1019 #endif /* NOTUSED */
1021 } else if (len % DHD_SDALIGN) {
1022 len += DHD_SDALIGN - (len % DHD_SDALIGN);
1025 /* Some controllers have trouble with odd bytes -- round to even */
1026 if (forcealign && (len & (ALIGNMENT - 1))) {
1028 if (skb_tailroom(pkt))
1030 len = roundup(len, ALIGNMENT);
1033 DHD_ERROR(("%s: sending unrounded %d-byte packet\n",
1040 dhd_bcmsdh_send_buf(bus, bcmsdh_cur_sbwad(sdh), SDIO_FUNC_2,
1041 F2SYNC, frame, len, pkt, NULL, NULL);
1043 ASSERT(ret != -BCME_PENDING);
1046 /* On failure, abort the command
1047 and terminate the frame */
1048 DHD_INFO(("%s: sdio error %d, abort command and "
1049 "terminate frame.\n", __func__, ret));
1052 bcmsdh_abort(sdh, SDIO_FUNC_2);
1053 bcmsdh_cfg_write(sdh, SDIO_FUNC_1,
1054 SBSDIO_FUNC1_FRAMECTRL, SFC_WF_TERM,
1058 for (i = 0; i < 3; i++) {
1060 hi = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
1061 SBSDIO_FUNC1_WFRAMEBCHI,
1063 lo = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
1064 SBSDIO_FUNC1_WFRAMEBCLO,
1066 bus->f1regdata += 2;
1067 if ((hi == 0) && (lo == 0))
1073 bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQUENCE_WRAP;
1075 } while ((ret < 0) && retrydata && retries++ < TXRETRIES);
1078 /* restore pkt buffer pointer before calling tx complete routine */
1079 skb_pull(pkt, SDPCM_HDRLEN + pad);
1080 dhd_os_sdunlock(bus->dhd);
1081 dhd_txcomplete(bus->dhd, pkt, ret != 0);
1082 dhd_os_sdlock(bus->dhd);
1085 pkt_buf_free_skb(pkt);
1090 int dhd_bus_txdata(struct dhd_bus *bus, struct sk_buff *pkt)
1092 int ret = -BCME_ERROR;
1095 DHD_TRACE(("%s: Enter\n", __func__));
1100 /* Push the test header if doing loopback */
1101 if (bus->ext_loop) {
1103 skb_push(pkt, SDPCM_TEST_HDRLEN);
1105 *data++ = SDPCM_TEST_ECHOREQ;
1106 *data++ = (u8) bus->loopid++;
1107 *data++ = (datalen >> 0);
1108 *data++ = (datalen >> 8);
1109 datalen += SDPCM_TEST_HDRLEN;
1113 /* Add space for the header */
1114 skb_push(pkt, SDPCM_HDRLEN);
1115 ASSERT(IS_ALIGNED((unsigned long)(pkt->data), 2));
1117 prec = PRIO2PREC((pkt->priority & PRIOMASK));
1119 /* Check for existing queue, current flow-control,
1120 pending event, or pending clock */
1121 if (dhd_deferred_tx || bus->fcstate || pktq_len(&bus->txq)
1122 || bus->dpc_sched || (!DATAOK(bus))
1123 || (bus->flowcontrol & NBITVAL(prec))
1124 || (bus->clkstate != CLK_AVAIL)) {
1125 DHD_TRACE(("%s: deferring pktq len %d\n", __func__,
1126 pktq_len(&bus->txq)));
1129 /* Priority based enq */
1130 dhd_os_sdlock_txq(bus->dhd);
1131 if (dhd_prec_enq(bus->dhd, &bus->txq, pkt, prec) == false) {
1132 skb_pull(pkt, SDPCM_HDRLEN);
1133 dhd_txcomplete(bus->dhd, pkt, false);
1134 pkt_buf_free_skb(pkt);
1135 DHD_ERROR(("%s: out of bus->txq !!!\n", __func__));
1140 dhd_os_sdunlock_txq(bus->dhd);
1142 if (pktq_len(&bus->txq) >= TXHI)
1143 dhd_txflowcontrol(bus->dhd, 0, ON);
1146 if (pktq_plen(&bus->txq, prec) > qcount[prec])
1147 qcount[prec] = pktq_plen(&bus->txq, prec);
1149 /* Schedule DPC if needed to send queued packet(s) */
1150 if (dhd_deferred_tx && !bus->dpc_sched) {
1151 bus->dpc_sched = true;
1152 dhd_sched_dpc(bus->dhd);
1155 /* Lock: we're about to use shared data/code (and SDIO) */
1156 dhd_os_sdlock(bus->dhd);
1158 /* Otherwise, send it now */
1160 /* Make sure back plane ht clk is on, no pending allowed */
1161 dhdsdio_clkctl(bus, CLK_AVAIL, true);
1164 DHD_TRACE(("%s: calling txpkt\n", __func__));
1165 ret = dhdsdio_txpkt(bus, pkt, SDPCM_DATA_CHANNEL, true);
1167 ret = dhdsdio_txpkt(bus, pkt,
1168 (bus->ext_loop ? SDPCM_TEST_CHANNEL :
1169 SDPCM_DATA_CHANNEL), true);
1172 bus->dhd->tx_errors++;
1174 bus->dhd->dstats.tx_bytes += datalen;
1176 if ((bus->idletime == DHD_IDLE_IMMEDIATE) && !bus->dpc_sched) {
1177 bus->activity = false;
1178 dhdsdio_clkctl(bus, CLK_NONE, true);
1181 dhd_os_sdunlock(bus->dhd);
1187 static uint dhdsdio_sendfromq(dhd_bus_t *bus, uint maxframes)
1189 struct sk_buff *pkt;
1192 int ret = 0, prec_out;
1197 dhd_pub_t *dhd = bus->dhd;
1198 sdpcmd_regs_t *regs = bus->regs;
1200 DHD_TRACE(("%s: Enter\n", __func__));
1202 tx_prec_map = ~bus->flowcontrol;
1204 /* Send frames until the limit or some other event */
1205 for (cnt = 0; (cnt < maxframes) && DATAOK(bus); cnt++) {
1206 dhd_os_sdlock_txq(bus->dhd);
1207 pkt = pktq_mdeq(&bus->txq, tx_prec_map, &prec_out);
1209 dhd_os_sdunlock_txq(bus->dhd);
1212 dhd_os_sdunlock_txq(bus->dhd);
1213 datalen = pkt->len - SDPCM_HDRLEN;
1216 ret = dhdsdio_txpkt(bus, pkt, SDPCM_DATA_CHANNEL, true);
1218 ret = dhdsdio_txpkt(bus, pkt,
1219 (bus->ext_loop ? SDPCM_TEST_CHANNEL :
1220 SDPCM_DATA_CHANNEL), true);
1223 bus->dhd->tx_errors++;
1225 bus->dhd->dstats.tx_bytes += datalen;
1227 /* In poll mode, need to check for other events */
1228 if (!bus->intr && cnt) {
1229 /* Check device status, signal pending interrupt */
1230 R_SDREG(intstatus, ®s->intstatus, retries);
1232 if (bcmsdh_regfail(bus->sdh))
1234 if (intstatus & bus->hostintmask)
1239 /* Deflow-control stack if needed */
1240 if (dhd->up && (dhd->busstate == DHD_BUS_DATA) &&
1241 dhd->txoff && (pktq_len(&bus->txq) < TXLOW))
1242 dhd_txflowcontrol(dhd, 0, OFF);
1247 int dhd_bus_txctl(struct dhd_bus *bus, unsigned char *msg, uint msglen)
1253 bcmsdh_info_t *sdh = bus->sdh;
1258 DHD_TRACE(("%s: Enter\n", __func__));
1260 if (bus->dhd->dongle_reset)
1263 /* Back the pointer to make a room for bus header */
1264 frame = msg - SDPCM_HDRLEN;
1265 len = (msglen += SDPCM_HDRLEN);
1267 /* Add alignment padding (optional for ctl frames) */
1269 doff = ((unsigned long)frame % DHD_SDALIGN);
1274 memset(frame, 0, doff + SDPCM_HDRLEN);
1276 ASSERT(doff < DHD_SDALIGN);
1278 doff += SDPCM_HDRLEN;
1280 /* Round send length to next SDIO block */
1281 if (bus->roundup && bus->blocksize && (len > bus->blocksize)) {
1282 u16 pad = bus->blocksize - (len % bus->blocksize);
1283 if ((pad <= bus->roundup) && (pad < bus->blocksize))
1285 } else if (len % DHD_SDALIGN) {
1286 len += DHD_SDALIGN - (len % DHD_SDALIGN);
1289 /* Satisfy length-alignment requirements */
1290 if (forcealign && (len & (ALIGNMENT - 1)))
1291 len = roundup(len, ALIGNMENT);
1293 ASSERT(IS_ALIGNED((unsigned long)frame, 2));
1295 /* Need to lock here to protect txseq and SDIO tx calls */
1296 dhd_os_sdlock(bus->dhd);
1300 /* Make sure backplane clock is on */
1301 dhdsdio_clkctl(bus, CLK_AVAIL, false);
1303 /* Hardware tag: 2 byte len followed by 2 byte ~len check (all LE) */
1304 *(u16 *) frame = cpu_to_le16((u16) msglen);
1305 *(((u16 *) frame) + 1) = cpu_to_le16(~msglen);
1307 /* Software tag: channel, sequence number, data offset */
1309 ((SDPCM_CONTROL_CHANNEL << SDPCM_CHANNEL_SHIFT) &
1311 | bus->tx_seq | ((doff << SDPCM_DOFFSET_SHIFT) &
1312 SDPCM_DOFFSET_MASK);
1313 put_unaligned_le32(swheader, frame + SDPCM_FRAMETAG_LEN);
1314 put_unaligned_le32(0, frame + SDPCM_FRAMETAG_LEN + sizeof(swheader));
1317 DHD_INFO(("%s: No bus credit bus->tx_max %d, bus->tx_seq %d\n",
1318 __func__, bus->tx_max, bus->tx_seq));
1319 bus->ctrl_frame_stat = true;
1321 bus->ctrl_frame_buf = frame;
1322 bus->ctrl_frame_len = len;
1324 dhd_wait_for_event(bus->dhd, &bus->ctrl_frame_stat);
1326 if (bus->ctrl_frame_stat == false) {
1327 DHD_INFO(("%s: ctrl_frame_stat == false\n", __func__));
1330 DHD_INFO(("%s: ctrl_frame_stat == true\n", __func__));
1337 if (DHD_BYTES_ON() && DHD_CTL_ON())
1338 prhex("Tx Frame", frame, len);
1339 else if (DHD_HDRS_ON())
1340 prhex("TxHdr", frame, min_t(u16, len, 16));
1344 bus->ctrl_frame_stat = false;
1346 dhd_bcmsdh_send_buf(bus, bcmsdh_cur_sbwad(sdh),
1347 SDIO_FUNC_2, F2SYNC, frame, len,
1350 ASSERT(ret != -BCME_PENDING);
1353 /* On failure, abort the command and
1354 terminate the frame */
1355 DHD_INFO(("%s: sdio error %d, abort command and terminate frame.\n",
1359 bcmsdh_abort(sdh, SDIO_FUNC_2);
1361 bcmsdh_cfg_write(sdh, SDIO_FUNC_1,
1362 SBSDIO_FUNC1_FRAMECTRL,
1366 for (i = 0; i < 3; i++) {
1368 hi = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
1369 SBSDIO_FUNC1_WFRAMEBCHI,
1371 lo = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
1372 SBSDIO_FUNC1_WFRAMEBCLO,
1374 bus->f1regdata += 2;
1375 if ((hi == 0) && (lo == 0))
1382 (bus->tx_seq + 1) % SDPCM_SEQUENCE_WRAP;
1384 } while ((ret < 0) && retries++ < TXRETRIES);
1387 if ((bus->idletime == DHD_IDLE_IMMEDIATE) && !bus->dpc_sched) {
1388 bus->activity = false;
1389 dhdsdio_clkctl(bus, CLK_NONE, true);
1392 dhd_os_sdunlock(bus->dhd);
1395 bus->dhd->tx_ctlerrs++;
1397 bus->dhd->tx_ctlpkts++;
1399 return ret ? -EIO : 0;
1402 int dhd_bus_rxctl(struct dhd_bus *bus, unsigned char *msg, uint msglen)
1408 DHD_TRACE(("%s: Enter\n", __func__));
1410 if (bus->dhd->dongle_reset)
1413 /* Wait until control frame is available */
1414 timeleft = dhd_os_ioctl_resp_wait(bus->dhd, &bus->rxlen, &pending);
1416 dhd_os_sdlock(bus->dhd);
1418 memcpy(msg, bus->rxctl, min(msglen, rxlen));
1420 dhd_os_sdunlock(bus->dhd);
1423 DHD_CTL(("%s: resumed on rxctl frame, got %d expected %d\n",
1424 __func__, rxlen, msglen));
1425 } else if (timeleft == 0) {
1426 DHD_ERROR(("%s: resumed on timeout\n", __func__));
1428 dhd_os_sdlock(bus->dhd);
1429 dhdsdio_checkdied(bus, NULL, 0);
1430 dhd_os_sdunlock(bus->dhd);
1431 #endif /* DHD_DEBUG */
1432 } else if (pending == true) {
1433 DHD_CTL(("%s: cancelled\n", __func__));
1434 return -ERESTARTSYS;
1436 DHD_CTL(("%s: resumed for unknown reason?\n", __func__));
1438 dhd_os_sdlock(bus->dhd);
1439 dhdsdio_checkdied(bus, NULL, 0);
1440 dhd_os_sdunlock(bus->dhd);
1441 #endif /* DHD_DEBUG */
1445 bus->dhd->rx_ctlpkts++;
1447 bus->dhd->rx_ctlerrs++;
1449 return rxlen ? (int)rxlen : -ETIMEDOUT;
1488 const bcm_iovar_t dhdsdio_iovars[] = {
1489 {"intr", IOV_INTR, 0, IOVT_BOOL, 0},
1490 {"sleep", IOV_SLEEP, 0, IOVT_BOOL, 0},
1491 {"pollrate", IOV_POLLRATE, 0, IOVT_UINT32, 0},
1492 {"idletime", IOV_IDLETIME, 0, IOVT_INT32, 0},
1493 {"idleclock", IOV_IDLECLOCK, 0, IOVT_INT32, 0},
1494 {"sd1idle", IOV_SD1IDLE, 0, IOVT_BOOL, 0},
1495 {"membytes", IOV_MEMBYTES, 0, IOVT_BUFFER, 2 * sizeof(int)},
1496 {"memsize", IOV_MEMSIZE, 0, IOVT_UINT32, 0},
1497 {"download", IOV_DOWNLOAD, 0, IOVT_BOOL, 0},
1498 {"vars", IOV_VARS, 0, IOVT_BUFFER, 0},
1499 {"sdiod_drive", IOV_SDIOD_DRIVE, 0, IOVT_UINT32, 0},
1500 {"readahead", IOV_READAHEAD, 0, IOVT_BOOL, 0},
1501 {"sdrxchain", IOV_SDRXCHAIN, 0, IOVT_BOOL, 0},
1502 {"alignctl", IOV_ALIGNCTL, 0, IOVT_BOOL, 0},
1503 {"sdalign", IOV_SDALIGN, 0, IOVT_BOOL, 0},
1504 {"devreset", IOV_DEVRESET, 0, IOVT_BOOL, 0},
1506 {"sdreg", IOV_SDREG, 0, IOVT_BUFFER, sizeof(sdreg_t)}
1508 {"sbreg", IOV_SBREG, 0, IOVT_BUFFER, sizeof(sdreg_t)}
1510 {"sd_cis", IOV_SDCIS, 0, IOVT_BUFFER, DHD_IOCTL_MAXLEN}
1512 {"forcealign", IOV_FORCEEVEN, 0, IOVT_BOOL, 0}
1514 {"txbound", IOV_TXBOUND, 0, IOVT_UINT32, 0}
1516 {"rxbound", IOV_RXBOUND, 0, IOVT_UINT32, 0}
1518 {"txminmax", IOV_TXMINMAX, 0, IOVT_UINT32, 0}
1520 {"cpu", IOV_CPU, 0, IOVT_BOOL, 0}
1523 {"checkdied", IOV_CHECKDIED, 0, IOVT_BUFFER, 0}
1525 #endif /* DHD_DEBUG */
1526 #endif /* DHD_DEBUG */
1528 {"extloop", IOV_EXTLOOP, 0, IOVT_BOOL, 0}
1530 {"pktgen", IOV_PKTGEN, 0, IOVT_BUFFER, sizeof(dhd_pktgen_t)}
1538 dhd_dump_pct(struct bcmstrbuf *strbuf, char *desc, uint num, uint div)
1543 bcm_bprintf(strbuf, "%s N/A", desc);
1546 q2 = (100 * (num - (q1 * div))) / div;
1547 bcm_bprintf(strbuf, "%s %d.%02d", desc, q1, q2);
1551 void dhd_bus_dump(dhd_pub_t *dhdp, struct bcmstrbuf *strbuf)
1553 dhd_bus_t *bus = dhdp->bus;
1555 bcm_bprintf(strbuf, "Bus SDIO structure:\n");
1557 "hostintmask 0x%08x intstatus 0x%08x sdpcm_ver %d\n",
1558 bus->hostintmask, bus->intstatus, bus->sdpcm_ver);
1560 "fcstate %d qlen %d tx_seq %d, max %d, rxskip %d rxlen %d rx_seq %d\n",
1561 bus->fcstate, pktq_len(&bus->txq), bus->tx_seq, bus->tx_max,
1562 bus->rxskip, bus->rxlen, bus->rx_seq);
1563 bcm_bprintf(strbuf, "intr %d intrcount %d lastintrs %d spurious %d\n",
1564 bus->intr, bus->intrcount, bus->lastintrs, bus->spurious);
1565 bcm_bprintf(strbuf, "pollrate %d pollcnt %d regfails %d\n",
1566 bus->pollrate, bus->pollcnt, bus->regfails);
1568 bcm_bprintf(strbuf, "\nAdditional counters:\n");
1570 "tx_sderrs %d fcqueued %d rxrtx %d rx_toolong %d rxc_errors %d\n",
1571 bus->tx_sderrs, bus->fcqueued, bus->rxrtx, bus->rx_toolong,
1573 bcm_bprintf(strbuf, "rx_hdrfail %d badhdr %d badseq %d\n",
1574 bus->rx_hdrfail, bus->rx_badhdr, bus->rx_badseq);
1575 bcm_bprintf(strbuf, "fc_rcvd %d, fc_xoff %d, fc_xon %d\n", bus->fc_rcvd,
1576 bus->fc_xoff, bus->fc_xon);
1577 bcm_bprintf(strbuf, "rxglomfail %d, rxglomframes %d, rxglompkts %d\n",
1578 bus->rxglomfail, bus->rxglomframes, bus->rxglompkts);
1579 bcm_bprintf(strbuf, "f2rx (hdrs/data) %d (%d/%d), f2tx %d f1regs %d\n",
1580 (bus->f2rxhdrs + bus->f2rxdata), bus->f2rxhdrs,
1581 bus->f2rxdata, bus->f2txdata, bus->f1regdata);
1583 dhd_dump_pct(strbuf, "\nRx: pkts/f2rd", bus->dhd->rx_packets,
1584 (bus->f2rxhdrs + bus->f2rxdata));
1585 dhd_dump_pct(strbuf, ", pkts/f1sd", bus->dhd->rx_packets,
1587 dhd_dump_pct(strbuf, ", pkts/sd", bus->dhd->rx_packets,
1588 (bus->f2rxhdrs + bus->f2rxdata + bus->f1regdata));
1589 dhd_dump_pct(strbuf, ", pkts/int", bus->dhd->rx_packets,
1591 bcm_bprintf(strbuf, "\n");
1593 dhd_dump_pct(strbuf, "Rx: glom pct", (100 * bus->rxglompkts),
1594 bus->dhd->rx_packets);
1595 dhd_dump_pct(strbuf, ", pkts/glom", bus->rxglompkts,
1597 bcm_bprintf(strbuf, "\n");
1599 dhd_dump_pct(strbuf, "Tx: pkts/f2wr", bus->dhd->tx_packets,
1601 dhd_dump_pct(strbuf, ", pkts/f1sd", bus->dhd->tx_packets,
1603 dhd_dump_pct(strbuf, ", pkts/sd", bus->dhd->tx_packets,
1604 (bus->f2txdata + bus->f1regdata));
1605 dhd_dump_pct(strbuf, ", pkts/int", bus->dhd->tx_packets,
1607 bcm_bprintf(strbuf, "\n");
1609 dhd_dump_pct(strbuf, "Total: pkts/f2rw",
1610 (bus->dhd->tx_packets + bus->dhd->rx_packets),
1611 (bus->f2txdata + bus->f2rxhdrs + bus->f2rxdata));
1612 dhd_dump_pct(strbuf, ", pkts/f1sd",
1613 (bus->dhd->tx_packets + bus->dhd->rx_packets),
1615 dhd_dump_pct(strbuf, ", pkts/sd",
1616 (bus->dhd->tx_packets + bus->dhd->rx_packets),
1617 (bus->f2txdata + bus->f2rxhdrs + bus->f2rxdata +
1619 dhd_dump_pct(strbuf, ", pkts/int",
1620 (bus->dhd->tx_packets + bus->dhd->rx_packets),
1622 bcm_bprintf(strbuf, "\n\n");
1626 if (bus->pktgen_count) {
1627 bcm_bprintf(strbuf, "pktgen config and count:\n");
1629 "freq %d count %d print %d total %d min %d len %d\n",
1630 bus->pktgen_freq, bus->pktgen_count,
1631 bus->pktgen_print, bus->pktgen_total,
1632 bus->pktgen_minlen, bus->pktgen_maxlen);
1633 bcm_bprintf(strbuf, "send attempts %d rcvd %d fail %d\n",
1634 bus->pktgen_sent, bus->pktgen_rcvd,
1639 bcm_bprintf(strbuf, "dpc_sched %d host interrupt%spending\n",
1641 (bcmsdh_intr_pending(bus->sdh) ? " " : " not "));
1642 bcm_bprintf(strbuf, "blocksize %d roundup %d\n", bus->blocksize,
1644 #endif /* DHD_DEBUG */
1646 "clkstate %d activity %d idletime %d idlecount %d sleeping %d\n",
1647 bus->clkstate, bus->activity, bus->idletime, bus->idlecount,
1651 void dhd_bus_clearcounts(dhd_pub_t *dhdp)
1653 dhd_bus_t *bus = (dhd_bus_t *) dhdp->bus;
1655 bus->intrcount = bus->lastintrs = bus->spurious = bus->regfails = 0;
1656 bus->rxrtx = bus->rx_toolong = bus->rxc_errors = 0;
1657 bus->rx_hdrfail = bus->rx_badhdr = bus->rx_badseq = 0;
1658 bus->tx_sderrs = bus->fc_rcvd = bus->fc_xoff = bus->fc_xon = 0;
1659 bus->rxglomfail = bus->rxglomframes = bus->rxglompkts = 0;
1660 bus->f2rxhdrs = bus->f2rxdata = bus->f2txdata = bus->f1regdata = 0;
1664 static int dhdsdio_pktgen_get(dhd_bus_t *bus, u8 *arg)
1666 dhd_pktgen_t pktgen;
1668 pktgen.version = DHD_PKTGEN_VERSION;
1669 pktgen.freq = bus->pktgen_freq;
1670 pktgen.count = bus->pktgen_count;
1671 pktgen.print = bus->pktgen_print;
1672 pktgen.total = bus->pktgen_total;
1673 pktgen.minlen = bus->pktgen_minlen;
1674 pktgen.maxlen = bus->pktgen_maxlen;
1675 pktgen.numsent = bus->pktgen_sent;
1676 pktgen.numrcvd = bus->pktgen_rcvd;
1677 pktgen.numfail = bus->pktgen_fail;
1678 pktgen.mode = bus->pktgen_mode;
1679 pktgen.stop = bus->pktgen_stop;
1681 memcpy(arg, &pktgen, sizeof(pktgen));
1686 static int dhdsdio_pktgen_set(dhd_bus_t *bus, u8 *arg)
1688 dhd_pktgen_t pktgen;
1689 uint oldcnt, oldmode;
1691 memcpy(&pktgen, arg, sizeof(pktgen));
1692 if (pktgen.version != DHD_PKTGEN_VERSION)
1695 oldcnt = bus->pktgen_count;
1696 oldmode = bus->pktgen_mode;
1698 bus->pktgen_freq = pktgen.freq;
1699 bus->pktgen_count = pktgen.count;
1700 bus->pktgen_print = pktgen.print;
1701 bus->pktgen_total = pktgen.total;
1702 bus->pktgen_minlen = pktgen.minlen;
1703 bus->pktgen_maxlen = pktgen.maxlen;
1704 bus->pktgen_mode = pktgen.mode;
1705 bus->pktgen_stop = pktgen.stop;
1707 bus->pktgen_tick = bus->pktgen_ptick = 0;
1708 bus->pktgen_len = max(bus->pktgen_len, bus->pktgen_minlen);
1709 bus->pktgen_len = min(bus->pktgen_len, bus->pktgen_maxlen);
1711 /* Clear counts for a new pktgen (mode change, or was stopped) */
1712 if (bus->pktgen_count && (!oldcnt || oldmode != bus->pktgen_mode))
1713 bus->pktgen_sent = bus->pktgen_rcvd = bus->pktgen_fail = 0;
1720 dhdsdio_membytes(dhd_bus_t *bus, bool write, u32 address, u8 *data,
1727 /* Determine initial transfer parameters */
1728 sdaddr = address & SBSDIO_SB_OFT_ADDR_MASK;
1729 if ((sdaddr + size) & SBSDIO_SBWINDOW_MASK)
1730 dsize = (SBSDIO_SB_OFT_ADDR_LIMIT - sdaddr);
1734 /* Set the backplane window to include the start address */
1735 bcmerror = dhdsdio_set_siaddr_window(bus, address);
1737 DHD_ERROR(("%s: window change failed\n", __func__));
1741 /* Do the transfer(s) */
1743 DHD_INFO(("%s: %s %d bytes at offset 0x%08x in window 0x%08x\n",
1744 __func__, (write ? "write" : "read"), dsize,
1745 sdaddr, (address & SBSDIO_SBWINDOW_MASK)));
1747 bcmsdh_rwdata(bus->sdh, write, sdaddr, data, dsize);
1749 DHD_ERROR(("%s: membytes transfer failed\n", __func__));
1753 /* Adjust for next transfer (if any) */
1758 bcmerror = dhdsdio_set_siaddr_window(bus, address);
1760 DHD_ERROR(("%s: window change failed\n",
1765 dsize = min_t(uint, SBSDIO_SB_OFT_ADDR_LIMIT, size);
1770 /* Return the window to backplane enumeration space for core access */
1771 if (dhdsdio_set_siaddr_window(bus, bcmsdh_cur_sbwad(bus->sdh))) {
1772 DHD_ERROR(("%s: FAILED to set window back to 0x%x\n",
1773 __func__, bcmsdh_cur_sbwad(bus->sdh)));
1780 static int dhdsdio_readshared(dhd_bus_t *bus, sdpcm_shared_t *sh)
1785 /* Read last word in memory to determine address of
1786 sdpcm_shared structure */
1787 rv = dhdsdio_membytes(bus, false, bus->ramsize - 4, (u8 *)&addr, 4);
1791 addr = le32_to_cpu(addr);
1793 DHD_INFO(("sdpcm_shared address 0x%08X\n", addr));
1796 * Check if addr is valid.
1797 * NVRAM length at the end of memory should have been overwritten.
1799 if (addr == 0 || ((~addr >> 16) & 0xffff) == (addr & 0xffff)) {
1800 DHD_ERROR(("%s: address (0x%08x) of sdpcm_shared invalid\n",
1805 /* Read hndrte_shared structure */
1806 rv = dhdsdio_membytes(bus, false, addr, (u8 *) sh,
1807 sizeof(sdpcm_shared_t));
1812 sh->flags = le32_to_cpu(sh->flags);
1813 sh->trap_addr = le32_to_cpu(sh->trap_addr);
1814 sh->assert_exp_addr = le32_to_cpu(sh->assert_exp_addr);
1815 sh->assert_file_addr = le32_to_cpu(sh->assert_file_addr);
1816 sh->assert_line = le32_to_cpu(sh->assert_line);
1817 sh->console_addr = le32_to_cpu(sh->console_addr);
1818 sh->msgtrace_addr = le32_to_cpu(sh->msgtrace_addr);
1820 if ((sh->flags & SDPCM_SHARED_VERSION_MASK) != SDPCM_SHARED_VERSION) {
1821 DHD_ERROR(("%s: sdpcm_shared version %d in dhd "
1822 "is different than sdpcm_shared version %d in dongle\n",
1823 __func__, SDPCM_SHARED_VERSION,
1824 sh->flags & SDPCM_SHARED_VERSION_MASK));
1831 static int dhdsdio_checkdied(dhd_bus_t *bus, u8 *data, uint size)
1835 char *mbuffer = NULL;
1836 uint maxstrlen = 256;
1839 sdpcm_shared_t sdpcm_shared;
1840 struct bcmstrbuf strbuf;
1842 DHD_TRACE(("%s: Enter\n", __func__));
1846 * Called after a rx ctrl timeout. "data" is NULL.
1847 * allocate memory to trace the trap or assert.
1850 mbuffer = data = kmalloc(msize, GFP_ATOMIC);
1851 if (mbuffer == NULL) {
1852 DHD_ERROR(("%s: kmalloc(%d) failed\n", __func__,
1859 str = kmalloc(maxstrlen, GFP_ATOMIC);
1861 DHD_ERROR(("%s: kmalloc(%d) failed\n", __func__, maxstrlen));
1866 bcmerror = dhdsdio_readshared(bus, &sdpcm_shared);
1870 bcm_binit(&strbuf, data, size);
1872 bcm_bprintf(&strbuf,
1873 "msgtrace address : 0x%08X\nconsole address : 0x%08X\n",
1874 sdpcm_shared.msgtrace_addr, sdpcm_shared.console_addr);
1876 if ((sdpcm_shared.flags & SDPCM_SHARED_ASSERT_BUILT) == 0) {
1877 /* NOTE: Misspelled assert is intentional - DO NOT FIX.
1878 * (Avoids conflict with real asserts for programmatic
1879 * parsing of output.)
1881 bcm_bprintf(&strbuf, "Assrt not built in dongle\n");
1884 if ((sdpcm_shared.flags & (SDPCM_SHARED_ASSERT | SDPCM_SHARED_TRAP)) ==
1886 /* NOTE: Misspelled assert is intentional - DO NOT FIX.
1887 * (Avoids conflict with real asserts for programmatic
1888 * parsing of output.)
1890 bcm_bprintf(&strbuf, "No trap%s in dongle",
1891 (sdpcm_shared.flags & SDPCM_SHARED_ASSERT_BUILT)
1894 if (sdpcm_shared.flags & SDPCM_SHARED_ASSERT) {
1895 /* Download assert */
1896 bcm_bprintf(&strbuf, "Dongle assert");
1897 if (sdpcm_shared.assert_exp_addr != 0) {
1899 bcmerror = dhdsdio_membytes(bus, false,
1900 sdpcm_shared.assert_exp_addr,
1901 (u8 *) str, maxstrlen);
1905 str[maxstrlen - 1] = '\0';
1906 bcm_bprintf(&strbuf, " expr \"%s\"", str);
1909 if (sdpcm_shared.assert_file_addr != 0) {
1911 bcmerror = dhdsdio_membytes(bus, false,
1912 sdpcm_shared.assert_file_addr,
1913 (u8 *) str, maxstrlen);
1917 str[maxstrlen - 1] = '\0';
1918 bcm_bprintf(&strbuf, " file \"%s\"", str);
1921 bcm_bprintf(&strbuf, " line %d ",
1922 sdpcm_shared.assert_line);
1925 if (sdpcm_shared.flags & SDPCM_SHARED_TRAP) {
1926 bcmerror = dhdsdio_membytes(bus, false,
1927 sdpcm_shared.trap_addr, (u8 *)&tr,
1932 bcm_bprintf(&strbuf,
1933 "Dongle trap type 0x%x @ epc 0x%x, cpsr 0x%x, spsr 0x%x, sp 0x%x,"
1934 "lp 0x%x, rpc 0x%x Trap offset 0x%x, "
1935 "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",
1936 tr.type, tr.epc, tr.cpsr, tr.spsr, tr.r13,
1937 tr.r14, tr.pc, sdpcm_shared.trap_addr,
1938 tr.r0, tr.r1, tr.r2, tr.r3, tr.r4, tr.r5,
1943 if (sdpcm_shared.flags & (SDPCM_SHARED_ASSERT | SDPCM_SHARED_TRAP))
1944 DHD_ERROR(("%s: %s\n", __func__, strbuf.origbuf));
1947 if (sdpcm_shared.flags & SDPCM_SHARED_TRAP) {
1948 /* Mem dump to a file on device */
1949 dhdsdio_mem_dump(bus);
1951 #endif /* DHD_DEBUG */
1960 static int dhdsdio_mem_dump(dhd_bus_t *bus)
1963 int size; /* Full mem size */
1964 int start = 0; /* Start address */
1965 int read_size = 0; /* Read size of each iteration */
1966 u8 *buf = NULL, *databuf = NULL;
1968 /* Get full mem size */
1969 size = bus->ramsize;
1970 buf = kmalloc(size, GFP_ATOMIC);
1972 DHD_ERROR(("%s: Out of memory (%d bytes)\n", __func__, size));
1976 /* Read mem content */
1977 printk(KERN_DEBUG "Dump dongle memory");
1980 read_size = min(MEMBLOCK, size);
1981 ret = dhdsdio_membytes(bus, false, start, databuf, read_size);
1983 DHD_ERROR(("%s: Error membytes %d\n", __func__, ret));
1989 /* Decrement size and increment start address */
1992 databuf += read_size;
1994 printk(KERN_DEBUG "Done\n");
1996 /* free buf before return !!! */
1997 if (write_to_file(bus->dhd, buf, bus->ramsize)) {
1998 DHD_ERROR(("%s: Error writing to files\n", __func__));
2002 /* buf free handled in write_to_file, not here */
2006 #define CONSOLE_LINE_MAX 192
2008 static int dhdsdio_readconsole(dhd_bus_t *bus)
2010 dhd_console_t *c = &bus->console;
2011 u8 line[CONSOLE_LINE_MAX], ch;
2015 /* Don't do anything until FWREADY updates console address */
2016 if (bus->console_addr == 0)
2019 /* Read console log struct */
2020 addr = bus->console_addr + offsetof(hndrte_cons_t, log);
2021 rv = dhdsdio_membytes(bus, false, addr, (u8 *)&c->log,
2026 /* Allocate console buffer (one time only) */
2027 if (c->buf == NULL) {
2028 c->bufsize = le32_to_cpu(c->log.buf_size);
2029 c->buf = kmalloc(c->bufsize, GFP_ATOMIC);
2034 idx = le32_to_cpu(c->log.idx);
2036 /* Protect against corrupt value */
2037 if (idx > c->bufsize)
2040 /* Skip reading the console buffer if the index pointer
2045 /* Read the console buffer */
2046 addr = le32_to_cpu(c->log.buf);
2047 rv = dhdsdio_membytes(bus, false, addr, c->buf, c->bufsize);
2051 while (c->last != idx) {
2052 for (n = 0; n < CONSOLE_LINE_MAX - 2; n++) {
2053 if (c->last == idx) {
2054 /* This would output a partial line.
2056 * the buffer pointer and output this
2057 * line next time around.
2062 c->last = c->bufsize - n;
2065 ch = c->buf[c->last];
2066 c->last = (c->last + 1) % c->bufsize;
2073 if (line[n - 1] == '\r')
2076 printk(KERN_DEBUG "CONSOLE: %s\n", line);
2083 #endif /* DHD_DEBUG */
2085 int dhdsdio_downloadvars(dhd_bus_t *bus, void *arg, int len)
2089 DHD_TRACE(("%s: Enter\n", __func__));
2091 /* Basic sanity checks */
2093 bcmerror = -BCME_NOTDOWN;
2097 bcmerror = -EOVERFLOW;
2101 /* Free the old ones and replace with passed variables */
2104 bus->vars = kmalloc(len, GFP_ATOMIC);
2105 bus->varsz = bus->vars ? len : 0;
2106 if (bus->vars == NULL) {
2111 /* Copy the passed variables, which should include the
2112 terminating double-null */
2113 memcpy(bus->vars, arg, bus->varsz);
2119 dhdsdio_doiovar(dhd_bus_t *bus, const bcm_iovar_t *vi, u32 actionid,
2120 const char *name, void *params, int plen, void *arg, int len,
2127 DHD_TRACE(("%s: Enter, action %d name %s params %p plen %d arg %p "
2128 "len %d val_size %d\n",
2129 __func__, actionid, name, params, plen, arg, len, val_size));
2131 bcmerror = bcm_iovar_lencheck(vi, arg, len, IOV_ISSET(actionid));
2135 if (plen >= (int)sizeof(int_val))
2136 memcpy(&int_val, params, sizeof(int_val));
2138 bool_val = (int_val != 0) ? true : false;
2140 /* Some ioctls use the bus */
2141 dhd_os_sdlock(bus->dhd);
2143 /* Check if dongle is in reset. If so, only allow DEVRESET iovars */
2144 if (bus->dhd->dongle_reset && !(actionid == IOV_SVAL(IOV_DEVRESET) ||
2145 actionid == IOV_GVAL(IOV_DEVRESET))) {
2146 bcmerror = -BCME_NOTREADY;
2150 /* Handle sleep stuff before any clock mucking */
2151 if (vi->varid == IOV_SLEEP) {
2152 if (IOV_ISSET(actionid)) {
2153 bcmerror = dhdsdio_bussleep(bus, bool_val);
2155 int_val = (s32) bus->sleeping;
2156 memcpy(arg, &int_val, val_size);
2161 /* Request clock to allow SDIO accesses */
2162 if (!bus->dhd->dongle_reset) {
2164 dhdsdio_clkctl(bus, CLK_AVAIL, false);
2168 case IOV_GVAL(IOV_INTR):
2169 int_val = (s32) bus->intr;
2170 memcpy(arg, &int_val, val_size);
2173 case IOV_SVAL(IOV_INTR):
2174 bus->intr = bool_val;
2175 bus->intdis = false;
2178 DHD_INTR(("%s: enable SDIO device interrupts\n",
2180 bcmsdh_intr_enable(bus->sdh);
2182 DHD_INTR(("%s: disable SDIO interrupts\n",
2184 bcmsdh_intr_disable(bus->sdh);
2189 case IOV_GVAL(IOV_POLLRATE):
2190 int_val = (s32) bus->pollrate;
2191 memcpy(arg, &int_val, val_size);
2194 case IOV_SVAL(IOV_POLLRATE):
2195 bus->pollrate = (uint) int_val;
2196 bus->poll = (bus->pollrate != 0);
2199 case IOV_GVAL(IOV_IDLETIME):
2200 int_val = bus->idletime;
2201 memcpy(arg, &int_val, val_size);
2204 case IOV_SVAL(IOV_IDLETIME):
2205 if ((int_val < 0) && (int_val != DHD_IDLE_IMMEDIATE))
2208 bus->idletime = int_val;
2211 case IOV_GVAL(IOV_IDLECLOCK):
2212 int_val = (s32) bus->idleclock;
2213 memcpy(arg, &int_val, val_size);
2216 case IOV_SVAL(IOV_IDLECLOCK):
2217 bus->idleclock = int_val;
2220 case IOV_GVAL(IOV_SD1IDLE):
2221 int_val = (s32) sd1idle;
2222 memcpy(arg, &int_val, val_size);
2225 case IOV_SVAL(IOV_SD1IDLE):
2229 case IOV_SVAL(IOV_MEMBYTES):
2230 case IOV_GVAL(IOV_MEMBYTES):
2236 bool set = (actionid == IOV_SVAL(IOV_MEMBYTES));
2238 ASSERT(plen >= 2 * sizeof(int));
2240 address = (u32) int_val;
2241 memcpy(&int_val, (char *)params + sizeof(int_val),
2243 size = (uint) int_val;
2245 /* Do some validation */
2246 dsize = set ? plen - (2 * sizeof(int)) : len;
2248 DHD_ERROR(("%s: error on %s membytes, addr "
2249 "0x%08x size %d dsize %d\n",
2250 __func__, (set ? "set" : "get"),
2251 address, size, dsize));
2256 DHD_INFO(("%s: Request to %s %d bytes at address "
2258 __func__, (set ? "write" : "read"), size, address));
2260 /* If we know about SOCRAM, check for a fit */
2261 if ((bus->orig_ramsize) &&
2262 ((address > bus->orig_ramsize)
2263 || (address + size > bus->orig_ramsize))) {
2264 DHD_ERROR(("%s: ramsize 0x%08x doesn't have %d "
2265 "bytes at 0x%08x\n",
2266 __func__, bus->orig_ramsize, size, address));
2271 /* Generate the actual data pointer */
2273 set ? (u8 *) params +
2274 2 * sizeof(int) : (u8 *) arg;
2276 /* Call to do the transfer */
2278 dhdsdio_membytes(bus, set, address, data, size);
2283 case IOV_GVAL(IOV_MEMSIZE):
2284 int_val = (s32) bus->ramsize;
2285 memcpy(arg, &int_val, val_size);
2288 case IOV_GVAL(IOV_SDIOD_DRIVE):
2289 int_val = (s32) dhd_sdiod_drive_strength;
2290 memcpy(arg, &int_val, val_size);
2293 case IOV_SVAL(IOV_SDIOD_DRIVE):
2294 dhd_sdiod_drive_strength = int_val;
2295 dhdsdio_sdiod_drive_strength_init(bus,
2296 dhd_sdiod_drive_strength);
2299 case IOV_SVAL(IOV_DOWNLOAD):
2300 bcmerror = dhdsdio_download_state(bus, bool_val);
2303 case IOV_SVAL(IOV_VARS):
2304 bcmerror = dhdsdio_downloadvars(bus, arg, len);
2307 case IOV_GVAL(IOV_READAHEAD):
2308 int_val = (s32) dhd_readahead;
2309 memcpy(arg, &int_val, val_size);
2312 case IOV_SVAL(IOV_READAHEAD):
2313 if (bool_val && !dhd_readahead)
2315 dhd_readahead = bool_val;
2318 case IOV_GVAL(IOV_SDRXCHAIN):
2319 int_val = (s32) bus->use_rxchain;
2320 memcpy(arg, &int_val, val_size);
2323 case IOV_SVAL(IOV_SDRXCHAIN):
2324 if (bool_val && !bus->sd_rxchain)
2325 bcmerror = -ENOTSUPP;
2327 bus->use_rxchain = bool_val;
2329 case IOV_GVAL(IOV_ALIGNCTL):
2330 int_val = (s32) dhd_alignctl;
2331 memcpy(arg, &int_val, val_size);
2334 case IOV_SVAL(IOV_ALIGNCTL):
2335 dhd_alignctl = bool_val;
2338 case IOV_GVAL(IOV_SDALIGN):
2339 int_val = DHD_SDALIGN;
2340 memcpy(arg, &int_val, val_size);
2344 case IOV_GVAL(IOV_VARS):
2345 if (bus->varsz < (uint) len)
2346 memcpy(arg, bus->vars, bus->varsz);
2348 bcmerror = -EOVERFLOW;
2350 #endif /* DHD_DEBUG */
2353 case IOV_GVAL(IOV_SDREG):
2358 sd_ptr = (sdreg_t *) params;
2360 addr = (unsigned long)bus->regs + sd_ptr->offset;
2361 size = sd_ptr->func;
2362 int_val = (s32) bcmsdh_reg_read(bus->sdh, addr, size);
2363 if (bcmsdh_regfail(bus->sdh))
2364 bcmerror = -BCME_SDIO_ERROR;
2365 memcpy(arg, &int_val, sizeof(s32));
2369 case IOV_SVAL(IOV_SDREG):
2374 sd_ptr = (sdreg_t *) params;
2376 addr = (unsigned long)bus->regs + sd_ptr->offset;
2377 size = sd_ptr->func;
2378 bcmsdh_reg_write(bus->sdh, addr, size, sd_ptr->value);
2379 if (bcmsdh_regfail(bus->sdh))
2380 bcmerror = -BCME_SDIO_ERROR;
2384 /* Same as above, but offset is not backplane
2386 case IOV_GVAL(IOV_SBREG):
2391 memcpy(&sdreg, params, sizeof(sdreg));
2393 addr = SI_ENUM_BASE + sdreg.offset;
2395 int_val = (s32) bcmsdh_reg_read(bus->sdh, addr, size);
2396 if (bcmsdh_regfail(bus->sdh))
2397 bcmerror = -BCME_SDIO_ERROR;
2398 memcpy(arg, &int_val, sizeof(s32));
2402 case IOV_SVAL(IOV_SBREG):
2407 memcpy(&sdreg, params, sizeof(sdreg));
2409 addr = SI_ENUM_BASE + sdreg.offset;
2411 bcmsdh_reg_write(bus->sdh, addr, size, sdreg.value);
2412 if (bcmsdh_regfail(bus->sdh))
2413 bcmerror = -BCME_SDIO_ERROR;
2417 case IOV_GVAL(IOV_SDCIS):
2421 strcat(arg, "\nFunc 0\n");
2422 bcmsdh_cis_read(bus->sdh, 0x10,
2423 (u8 *) arg + strlen(arg),
2424 SBSDIO_CIS_SIZE_LIMIT);
2425 strcat(arg, "\nFunc 1\n");
2426 bcmsdh_cis_read(bus->sdh, 0x11,
2427 (u8 *) arg + strlen(arg),
2428 SBSDIO_CIS_SIZE_LIMIT);
2429 strcat(arg, "\nFunc 2\n");
2430 bcmsdh_cis_read(bus->sdh, 0x12,
2431 (u8 *) arg + strlen(arg),
2432 SBSDIO_CIS_SIZE_LIMIT);
2436 case IOV_GVAL(IOV_FORCEEVEN):
2437 int_val = (s32) forcealign;
2438 memcpy(arg, &int_val, val_size);
2441 case IOV_SVAL(IOV_FORCEEVEN):
2442 forcealign = bool_val;
2445 case IOV_GVAL(IOV_TXBOUND):
2446 int_val = (s32) dhd_txbound;
2447 memcpy(arg, &int_val, val_size);
2450 case IOV_SVAL(IOV_TXBOUND):
2451 dhd_txbound = (uint) int_val;
2454 case IOV_GVAL(IOV_RXBOUND):
2455 int_val = (s32) dhd_rxbound;
2456 memcpy(arg, &int_val, val_size);
2459 case IOV_SVAL(IOV_RXBOUND):
2460 dhd_rxbound = (uint) int_val;
2463 case IOV_GVAL(IOV_TXMINMAX):
2464 int_val = (s32) dhd_txminmax;
2465 memcpy(arg, &int_val, val_size);
2468 case IOV_SVAL(IOV_TXMINMAX):
2469 dhd_txminmax = (uint) int_val;
2471 #endif /* DHD_DEBUG */
2474 case IOV_GVAL(IOV_EXTLOOP):
2475 int_val = (s32) bus->ext_loop;
2476 memcpy(arg, &int_val, val_size);
2479 case IOV_SVAL(IOV_EXTLOOP):
2480 bus->ext_loop = bool_val;
2483 case IOV_GVAL(IOV_PKTGEN):
2484 bcmerror = dhdsdio_pktgen_get(bus, arg);
2487 case IOV_SVAL(IOV_PKTGEN):
2488 bcmerror = dhdsdio_pktgen_set(bus, arg);
2492 case IOV_SVAL(IOV_DEVRESET):
2493 DHD_TRACE(("%s: Called set IOV_DEVRESET=%d dongle_reset=%d "
2495 __func__, bool_val, bus->dhd->dongle_reset,
2496 bus->dhd->busstate));
2498 dhd_bus_devreset(bus->dhd, (u8) bool_val);
2502 case IOV_GVAL(IOV_DEVRESET):
2503 DHD_TRACE(("%s: Called get IOV_DEVRESET\n", __func__));
2505 /* Get its status */
2506 int_val = (bool) bus->dhd->dongle_reset;
2507 memcpy(arg, &int_val, val_size);
2512 bcmerror = -ENOTSUPP;
2517 if ((bus->idletime == DHD_IDLE_IMMEDIATE) && !bus->dpc_sched) {
2518 bus->activity = false;
2519 dhdsdio_clkctl(bus, CLK_NONE, true);
2522 dhd_os_sdunlock(bus->dhd);
2524 if (actionid == IOV_SVAL(IOV_DEVRESET) && bool_val == false)
2525 dhd_preinit_ioctls((dhd_pub_t *) bus->dhd);
2530 static int dhdsdio_write_vars(dhd_bus_t *bus)
2538 char *nvram_ularray;
2539 #endif /* DHD_DEBUG */
2541 /* Even if there are no vars are to be written, we still
2542 need to set the ramsize. */
2543 varsize = bus->varsz ? roundup(bus->varsz, 4) : 0;
2544 varaddr = (bus->ramsize - 4) - varsize;
2547 vbuffer = kzalloc(varsize, GFP_ATOMIC);
2551 memcpy(vbuffer, bus->vars, bus->varsz);
2553 /* Write the vars list */
2555 dhdsdio_membytes(bus, true, varaddr, vbuffer, varsize);
2557 /* Verify NVRAM bytes */
2558 DHD_INFO(("Compare NVRAM dl & ul; varsize=%d\n", varsize));
2559 nvram_ularray = kmalloc(varsize, GFP_ATOMIC);
2563 /* Upload image to verify downloaded contents. */
2564 memset(nvram_ularray, 0xaa, varsize);
2566 /* Read the vars list to temp buffer for comparison */
2568 dhdsdio_membytes(bus, false, varaddr, nvram_ularray,
2571 DHD_ERROR(("%s: error %d on reading %d nvram bytes at "
2572 "0x%08x\n", __func__, bcmerror, varsize, varaddr));
2574 /* Compare the org NVRAM with the one read from RAM */
2575 if (memcmp(vbuffer, nvram_ularray, varsize)) {
2576 DHD_ERROR(("%s: Downloaded NVRAM image is corrupted.\n",
2579 DHD_ERROR(("%s: Download/Upload/Compare of NVRAM ok.\n",
2582 kfree(nvram_ularray);
2583 #endif /* DHD_DEBUG */
2588 /* adjust to the user specified RAM */
2589 DHD_INFO(("Physical memory size: %d, usable memory size: %d\n",
2590 bus->orig_ramsize, bus->ramsize));
2591 DHD_INFO(("Vars are at %d, orig varsize is %d\n", varaddr, varsize));
2592 varsize = ((bus->orig_ramsize - 4) - varaddr);
2595 * Determine the length token:
2596 * Varsize, converted to words, in lower 16-bits, checksum
2602 varsizew = varsize / 4;
2603 varsizew = (~varsizew << 16) | (varsizew & 0x0000FFFF);
2604 varsizew = cpu_to_le32(varsizew);
2607 DHD_INFO(("New varsize is %d, length token=0x%08x\n", varsize,
2610 /* Write the length token to the last word */
2611 bcmerror = dhdsdio_membytes(bus, true, (bus->orig_ramsize - 4),
2612 (u8 *)&varsizew, 4);
2617 static int dhdsdio_download_state(dhd_bus_t *bus, bool enter)
2623 /* To enter download state, disable ARM and reset SOCRAM.
2624 * To exit download state, simply reset ARM (default is RAM boot).
2627 bus->alp_only = true;
2629 dhdsdio_chip_disablecore(bus->sdh, bus->ci->armcorebase);
2631 dhdsdio_chip_resetcore(bus->sdh, bus->ci->ramcorebase);
2633 /* Clear the top bit of memory */
2636 dhdsdio_membytes(bus, true, bus->ramsize - 4,
2640 regdata = bcmsdh_reg_read(bus->sdh,
2641 CORE_SB(bus->ci->ramcorebase, sbtmstatelow), 4);
2642 regdata &= (SBTML_RESET | SBTML_REJ_MASK |
2643 (SICF_CLOCK_EN << SBTML_SICF_SHIFT));
2644 if ((SICF_CLOCK_EN << SBTML_SICF_SHIFT) != regdata) {
2645 DHD_ERROR(("%s: SOCRAM core is down after reset?\n",
2647 bcmerror = -BCME_ERROR;
2651 bcmerror = dhdsdio_write_vars(bus);
2653 DHD_ERROR(("%s: no vars written to RAM\n", __func__));
2657 W_SDREG(0xFFFFFFFF, &bus->regs->intstatus, retries);
2659 dhdsdio_chip_resetcore(bus->sdh, bus->ci->armcorebase);
2661 /* Allow HT Clock now that the ARM is running. */
2662 bus->alp_only = false;
2664 bus->dhd->busstate = DHD_BUS_LOAD;
2671 dhd_bus_iovar_op(dhd_pub_t *dhdp, const char *name,
2672 void *params, int plen, void *arg, int len, bool set)
2674 dhd_bus_t *bus = dhdp->bus;
2675 const bcm_iovar_t *vi = NULL;
2680 DHD_TRACE(("%s: Enter\n", __func__));
2685 /* Get MUST have return space */
2686 ASSERT(set || (arg && len));
2688 /* Set does NOT take qualifiers */
2689 ASSERT(!set || (!params && !plen));
2691 /* Look up var locally; if not found pass to host driver */
2692 vi = bcm_iovar_lookup(dhdsdio_iovars, name);
2694 dhd_os_sdlock(bus->dhd);
2698 /* Turn on clock in case SD command needs backplane */
2699 dhdsdio_clkctl(bus, CLK_AVAIL, false);
2702 bcmsdh_iovar_op(bus->sdh, name, params, plen, arg, len,
2705 /* Check for bus configuration changes of interest */
2707 /* If it was divisor change, read the new one */
2708 if (set && strcmp(name, "sd_divisor") == 0) {
2709 if (bcmsdh_iovar_op(bus->sdh, "sd_divisor", NULL, 0,
2710 &bus->sd_divisor, sizeof(s32),
2712 bus->sd_divisor = -1;
2713 DHD_ERROR(("%s: fail on %s get\n", __func__,
2716 DHD_INFO(("%s: noted %s update, value now %d\n",
2717 __func__, name, bus->sd_divisor));
2720 /* If it was a mode change, read the new one */
2721 if (set && strcmp(name, "sd_mode") == 0) {
2722 if (bcmsdh_iovar_op(bus->sdh, "sd_mode", NULL, 0,
2723 &bus->sd_mode, sizeof(s32),
2726 DHD_ERROR(("%s: fail on %s get\n", __func__,
2729 DHD_INFO(("%s: noted %s update, value now %d\n",
2730 __func__, name, bus->sd_mode));
2733 /* Similar check for blocksize change */
2734 if (set && strcmp(name, "sd_blocksize") == 0) {
2737 (bus->sdh, "sd_blocksize", &fnum, sizeof(s32),
2738 &bus->blocksize, sizeof(s32),
2741 DHD_ERROR(("%s: fail on %s get\n", __func__,
2744 DHD_INFO(("%s: noted %s update, value now %d\n",
2745 __func__, "sd_blocksize",
2749 bus->roundup = min(max_roundup, bus->blocksize);
2751 if ((bus->idletime == DHD_IDLE_IMMEDIATE) && !bus->dpc_sched) {
2752 bus->activity = false;
2753 dhdsdio_clkctl(bus, CLK_NONE, true);
2756 dhd_os_sdunlock(bus->dhd);
2760 DHD_CTL(("%s: %s %s, len %d plen %d\n", __func__,
2761 name, (set ? "set" : "get"), len, plen));
2763 /* set up 'params' pointer in case this is a set command so that
2764 * the convenience int and bool code can be common to set and get
2766 if (params == NULL) {
2771 if (vi->type == IOVT_VOID)
2773 else if (vi->type == IOVT_BUFFER)
2776 /* all other types are integer sized */
2777 val_size = sizeof(int);
2779 actionid = set ? IOV_SVAL(vi->varid) : IOV_GVAL(vi->varid);
2781 dhdsdio_doiovar(bus, vi, actionid, name, params, plen, arg, len,
2788 void dhd_bus_stop(struct dhd_bus *bus, bool enforce_mutex)
2790 u32 local_hostintmask;
2795 DHD_TRACE(("%s: Enter\n", __func__));
2798 dhd_os_sdlock(bus->dhd);
2802 /* Enable clock for device interrupts */
2803 dhdsdio_clkctl(bus, CLK_AVAIL, false);
2805 /* Disable and clear interrupts at the chip level also */
2806 W_SDREG(0, &bus->regs->hostintmask, retries);
2807 local_hostintmask = bus->hostintmask;
2808 bus->hostintmask = 0;
2810 /* Change our idea of bus state */
2811 bus->dhd->busstate = DHD_BUS_DOWN;
2813 /* Force clocks on backplane to be sure F2 interrupt propagates */
2815 bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
2818 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
2819 (saveclk | SBSDIO_FORCE_HT), &err);
2822 DHD_ERROR(("%s: Failed to force clock for F2: err %d\n",
2826 /* Turn off the bus (F2), free any pending packets */
2827 DHD_INTR(("%s: disable SDIO interrupts\n", __func__));
2828 bcmsdh_intr_disable(bus->sdh);
2829 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_0, SDIOD_CCCR_IOEN,
2830 SDIO_FUNC_ENABLE_1, NULL);
2832 /* Clear any pending interrupts now that F2 is disabled */
2833 W_SDREG(local_hostintmask, &bus->regs->intstatus, retries);
2835 /* Turn off the backplane clock (only) */
2836 dhdsdio_clkctl(bus, CLK_SDONLY, false);
2838 /* Clear the data packet queues */
2839 pktq_flush(&bus->txq, true);
2841 /* Clear any held glomming stuff */
2843 pkt_buf_free_skb(bus->glomd);
2846 pkt_buf_free_skb(bus->glom);
2848 bus->glom = bus->glomd = NULL;
2850 /* Clear rx control and wake any waiters */
2852 dhd_os_ioctl_resp_wake(bus->dhd);
2854 /* Reset some F2 state stuff */
2855 bus->rxskip = false;
2856 bus->tx_seq = bus->rx_seq = 0;
2859 dhd_os_sdunlock(bus->dhd);
2862 int dhd_bus_init(dhd_pub_t *dhdp, bool enforce_mutex)
2864 dhd_bus_t *bus = dhdp->bus;
2871 DHD_TRACE(("%s: Enter\n", __func__));
2878 dhd_os_sdlock(bus->dhd);
2880 /* Make sure backplane clock is on, needed to generate F2 interrupt */
2881 dhdsdio_clkctl(bus, CLK_AVAIL, false);
2882 if (bus->clkstate != CLK_AVAIL)
2885 /* Force clocks on backplane to be sure F2 interrupt propagates */
2887 bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
2890 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
2891 (saveclk | SBSDIO_FORCE_HT), &err);
2894 DHD_ERROR(("%s: Failed to force clock for F2: err %d\n",
2899 /* Enable function 2 (frame transfers) */
2900 W_SDREG((SDPCM_PROT_VERSION << SMB_DATA_VERSION_SHIFT),
2901 &bus->regs->tosbmailboxdata, retries);
2902 enable = (SDIO_FUNC_ENABLE_1 | SDIO_FUNC_ENABLE_2);
2904 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_0, SDIOD_CCCR_IOEN, enable, NULL);
2906 /* Give the dongle some time to do its thing and set IOR2 */
2907 dhd_timeout_start(&tmo, DHD_WAIT_F2RDY * 1000);
2910 while (ready != enable && !dhd_timeout_expired(&tmo))
2912 bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_0, SDIOD_CCCR_IORDY,
2915 DHD_INFO(("%s: enable 0x%02x, ready 0x%02x (waited %uus)\n",
2916 __func__, enable, ready, tmo.elapsed));
2918 /* If F2 successfully enabled, set core and enable interrupts */
2919 if (ready == enable) {
2920 /* Set up the interrupt mask and enable interrupts */
2921 bus->hostintmask = HOSTINTMASK;
2922 W_SDREG(bus->hostintmask,
2923 (unsigned int *)CORE_BUS_REG(bus->ci->buscorebase,
2924 hostintmask), retries);
2926 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_WATERMARK,
2927 (u8) watermark, &err);
2929 /* Set bus state according to enable result */
2930 dhdp->busstate = DHD_BUS_DATA;
2932 /* bcmsdh_intr_unmask(bus->sdh); */
2934 bus->intdis = false;
2936 DHD_INTR(("%s: enable SDIO device interrupts\n",
2938 bcmsdh_intr_enable(bus->sdh);
2940 DHD_INTR(("%s: disable SDIO interrupts\n", __func__));
2941 bcmsdh_intr_disable(bus->sdh);
2947 /* Disable F2 again */
2948 enable = SDIO_FUNC_ENABLE_1;
2949 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_0, SDIOD_CCCR_IOEN, enable,
2953 /* Restore previous clock setting */
2954 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
2957 /* If we didn't come up, turn off backplane clock */
2958 if (dhdp->busstate != DHD_BUS_DATA)
2959 dhdsdio_clkctl(bus, CLK_NONE, false);
2963 dhd_os_sdunlock(bus->dhd);
2968 static void dhdsdio_rxfail(dhd_bus_t *bus, bool abort, bool rtx)
2970 bcmsdh_info_t *sdh = bus->sdh;
2971 sdpcmd_regs_t *regs = bus->regs;
2977 DHD_ERROR(("%s: %sterminate frame%s\n", __func__,
2978 (abort ? "abort command, " : ""),
2979 (rtx ? ", send NAK" : "")));
2982 bcmsdh_abort(sdh, SDIO_FUNC_2);
2984 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_FRAMECTRL, SFC_RF_TERM,
2988 /* Wait until the packet has been flushed (device/FIFO stable) */
2989 for (lastrbc = retries = 0xffff; retries > 0; retries--) {
2990 hi = bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_RFRAMEBCHI,
2992 lo = bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_RFRAMEBCLO,
2994 bus->f1regdata += 2;
2996 if ((hi == 0) && (lo == 0))
2999 if ((hi > (lastrbc >> 8)) && (lo > (lastrbc & 0x00ff))) {
3000 DHD_ERROR(("%s: count growing: last 0x%04x now "
3002 __func__, lastrbc, ((hi << 8) + lo)));
3004 lastrbc = (hi << 8) + lo;
3008 DHD_ERROR(("%s: count never zeroed: last 0x%04x\n",
3009 __func__, lastrbc));
3011 DHD_INFO(("%s: flush took %d iterations\n", __func__,
3012 (0xffff - retries)));
3017 W_SDREG(SMB_NAK, ®s->tosbmailbox, retries);
3019 if (retries <= retry_limit)
3023 /* Clear partial in any case */
3026 /* If we can't reach the device, signal failure */
3027 if (err || bcmsdh_regfail(sdh))
3028 bus->dhd->busstate = DHD_BUS_DOWN;
3032 dhdsdio_read_control(dhd_bus_t *bus, u8 *hdr, uint len, uint doff)
3034 bcmsdh_info_t *sdh = bus->sdh;
3039 DHD_TRACE(("%s: Enter\n", __func__));
3041 /* Control data already received in aligned rxctl */
3042 if ((bus->bus == SPI_BUS) && (!bus->usebufpool))
3046 /* Set rxctl for frame (w/optional alignment) */
3047 bus->rxctl = bus->rxbuf;
3049 bus->rxctl += firstread;
3050 pad = ((unsigned long)bus->rxctl % DHD_SDALIGN);
3052 bus->rxctl += (DHD_SDALIGN - pad);
3053 bus->rxctl -= firstread;
3055 ASSERT(bus->rxctl >= bus->rxbuf);
3057 /* Copy the already-read portion over */
3058 memcpy(bus->rxctl, hdr, firstread);
3059 if (len <= firstread)
3062 /* Copy the full data pkt in gSPI case and process ioctl. */
3063 if (bus->bus == SPI_BUS) {
3064 memcpy(bus->rxctl, hdr, len);
3068 /* Raise rdlen to next SDIO block to avoid tail command */
3069 rdlen = len - firstread;
3070 if (bus->roundup && bus->blocksize && (rdlen > bus->blocksize)) {
3071 pad = bus->blocksize - (rdlen % bus->blocksize);
3072 if ((pad <= bus->roundup) && (pad < bus->blocksize) &&
3073 ((len + pad) < bus->dhd->maxctl))
3075 } else if (rdlen % DHD_SDALIGN) {
3076 rdlen += DHD_SDALIGN - (rdlen % DHD_SDALIGN);
3079 /* Satisfy length-alignment requirements */
3080 if (forcealign && (rdlen & (ALIGNMENT - 1)))
3081 rdlen = roundup(rdlen, ALIGNMENT);
3083 /* Drop if the read is too big or it exceeds our maximum */
3084 if ((rdlen + firstread) > bus->dhd->maxctl) {
3085 DHD_ERROR(("%s: %d-byte control read exceeds %d-byte buffer\n",
3086 __func__, rdlen, bus->dhd->maxctl));
3087 bus->dhd->rx_errors++;
3088 dhdsdio_rxfail(bus, false, false);
3092 if ((len - doff) > bus->dhd->maxctl) {
3093 DHD_ERROR(("%s: %d-byte ctl frame (%d-byte ctl data) exceeds "
3095 __func__, len, (len - doff), bus->dhd->maxctl));
3096 bus->dhd->rx_errors++;
3098 dhdsdio_rxfail(bus, false, false);
3102 /* Read remainder of frame body into the rxctl buffer */
3104 dhd_bcmsdh_recv_buf(bus, bcmsdh_cur_sbwad(sdh), SDIO_FUNC_2, F2SYNC,
3105 (bus->rxctl + firstread), rdlen, NULL, NULL,
3108 ASSERT(sdret != -BCME_PENDING);
3110 /* Control frame failures need retransmission */
3112 DHD_ERROR(("%s: read %d control bytes failed: %d\n",
3113 __func__, rdlen, sdret));
3114 bus->rxc_errors++; /* dhd.rx_ctlerrs is higher level */
3115 dhdsdio_rxfail(bus, true, true);
3122 if (DHD_BYTES_ON() && DHD_CTL_ON())
3123 prhex("RxCtrl", bus->rxctl, len);
3126 /* Point to valid data and indicate its length */
3128 bus->rxlen = len - doff;
3131 /* Awake any waiters */
3132 dhd_os_ioctl_resp_wake(bus->dhd);
3135 static u8 dhdsdio_rxglom(dhd_bus_t *bus, u8 rxseq)
3141 struct sk_buff *pfirst, *plast, *pnext, *save_pfirst;
3144 u8 chan, seq, doff, sfdoff;
3148 bool usechain = bus->use_rxchain;
3150 /* If packets, issue read(s) and send up packet chain */
3151 /* Return sequence numbers consumed? */
3153 DHD_TRACE(("dhdsdio_rxglom: start: glomd %p glom %p\n", bus->glomd,
3156 /* If there's a descriptor, generate the packet chain */
3158 dhd_os_sdlock_rxq(bus->dhd);
3160 pfirst = plast = pnext = NULL;
3161 dlen = (u16) (bus->glomd->len);
3162 dptr = bus->glomd->data;
3163 if (!dlen || (dlen & 1)) {
3164 DHD_ERROR(("%s: bad glomd len(%d), ignore descriptor\n",
3169 for (totlen = num = 0; dlen; num++) {
3170 /* Get (and move past) next length */
3171 sublen = get_unaligned_le16(dptr);
3172 dlen -= sizeof(u16);
3173 dptr += sizeof(u16);
3174 if ((sublen < SDPCM_HDRLEN) ||
3175 ((num == 0) && (sublen < (2 * SDPCM_HDRLEN)))) {
3176 DHD_ERROR(("%s: descriptor len %d bad: %d\n",
3177 __func__, num, sublen));
3181 if (sublen % DHD_SDALIGN) {
3182 DHD_ERROR(("%s: sublen %d not multiple of %d\n",
3183 __func__, sublen, DHD_SDALIGN));
3188 /* For last frame, adjust read len so total
3189 is a block multiple */
3192 (roundup(totlen, bus->blocksize) - totlen);
3193 totlen = roundup(totlen, bus->blocksize);
3196 /* Allocate/chain packet for next subframe */
3197 pnext = pkt_buf_get_skb(sublen + DHD_SDALIGN);
3198 if (pnext == NULL) {
3199 DHD_ERROR(("%s: pkt_buf_get_skb failed, num %d len %d\n",
3200 __func__, num, sublen));
3203 ASSERT(!(pnext->prev));
3206 pfirst = plast = pnext;
3209 plast->next = pnext;
3213 /* Adhere to start alignment requirements */
3214 PKTALIGN(pnext, sublen, DHD_SDALIGN);
3217 /* If all allocations succeeded, save packet chain
3220 DHD_GLOM(("%s: allocated %d-byte packet chain for %d "
3221 "subframes\n", __func__, totlen, num));
3222 if (DHD_GLOM_ON() && bus->nextlen) {
3223 if (totlen != bus->nextlen) {
3224 DHD_GLOM(("%s: glomdesc mismatch: nextlen %d glomdesc %d " "rxseq %d\n",
3225 __func__, bus->nextlen,
3230 pfirst = pnext = NULL;
3233 pkt_buf_free_skb(pfirst);
3238 /* Done with descriptor packet */
3239 pkt_buf_free_skb(bus->glomd);
3243 dhd_os_sdunlock_rxq(bus->dhd);
3246 /* Ok -- either we just generated a packet chain,
3247 or had one from before */
3249 if (DHD_GLOM_ON()) {
3250 DHD_GLOM(("%s: try superframe read, packet chain:\n",
3252 for (pnext = bus->glom; pnext; pnext = pnext->next) {
3253 DHD_GLOM((" %p: %p len 0x%04x (%d)\n",
3254 pnext, (u8 *) (pnext->data),
3255 pnext->len, pnext->len));
3260 dlen = (u16) pkttotlen(pfirst);
3262 /* Do an SDIO read for the superframe. Configurable iovar to
3263 * read directly into the chained packet, or allocate a large
3264 * packet and and copy into the chain.
3267 errcode = dhd_bcmsdh_recv_buf(bus,
3269 (bus->sdh), SDIO_FUNC_2,
3271 (u8 *) pfirst->data,
3272 dlen, pfirst, NULL, NULL);
3273 } else if (bus->dataptr) {
3274 errcode = dhd_bcmsdh_recv_buf(bus,
3276 (bus->sdh), SDIO_FUNC_2,
3277 F2SYNC, bus->dataptr,
3278 dlen, NULL, NULL, NULL);
3280 (u16) pktfrombuf(pfirst, 0, dlen,
3282 if (sublen != dlen) {
3283 DHD_ERROR(("%s: FAILED TO COPY, dlen %d sublen %d\n",
3284 __func__, dlen, sublen));
3289 DHD_ERROR(("COULDN'T ALLOC %d-BYTE GLOM, FORCE FAILURE\n",
3294 ASSERT(errcode != -BCME_PENDING);
3296 /* On failure, kill the superframe, allow a couple retries */
3298 DHD_ERROR(("%s: glom read of %d bytes failed: %d\n",
3299 __func__, dlen, errcode));
3300 bus->dhd->rx_errors++;
3302 if (bus->glomerr++ < 3) {
3303 dhdsdio_rxfail(bus, true, true);
3306 dhdsdio_rxfail(bus, true, false);
3307 dhd_os_sdlock_rxq(bus->dhd);
3308 pkt_buf_free_skb(bus->glom);
3309 dhd_os_sdunlock_rxq(bus->dhd);
3316 if (DHD_GLOM_ON()) {
3317 prhex("SUPERFRAME", pfirst->data,
3318 min_t(int, pfirst->len, 48));
3322 /* Validate the superframe header */
3323 dptr = (u8 *) (pfirst->data);
3324 sublen = get_unaligned_le16(dptr);
3325 check = get_unaligned_le16(dptr + sizeof(u16));
3327 chan = SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]);
3328 seq = SDPCM_PACKET_SEQUENCE(&dptr[SDPCM_FRAMETAG_LEN]);
3329 bus->nextlen = dptr[SDPCM_FRAMETAG_LEN + SDPCM_NEXTLEN_OFFSET];
3330 if ((bus->nextlen << 4) > MAX_RX_DATASZ) {
3331 DHD_INFO(("%s: nextlen too large (%d) seq %d\n",
3332 __func__, bus->nextlen, seq));
3335 doff = SDPCM_DOFFSET_VALUE(&dptr[SDPCM_FRAMETAG_LEN]);
3336 txmax = SDPCM_WINDOW_VALUE(&dptr[SDPCM_FRAMETAG_LEN]);
3339 if ((u16)~(sublen ^ check)) {
3340 DHD_ERROR(("%s (superframe): HW hdr error: len/check "
3341 "0x%04x/0x%04x\n", __func__, sublen, check));
3343 } else if (roundup(sublen, bus->blocksize) != dlen) {
3344 DHD_ERROR(("%s (superframe): len 0x%04x, rounded "
3345 "0x%04x, expect 0x%04x\n",
3347 roundup(sublen, bus->blocksize), dlen));
3349 } else if (SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]) !=
3350 SDPCM_GLOM_CHANNEL) {
3351 DHD_ERROR(("%s (superframe): bad channel %d\n",
3353 SDPCM_PACKET_CHANNEL(&dptr
3354 [SDPCM_FRAMETAG_LEN])));
3356 } else if (SDPCM_GLOMDESC(&dptr[SDPCM_FRAMETAG_LEN])) {
3357 DHD_ERROR(("%s (superframe): got second descriptor?\n",
3360 } else if ((doff < SDPCM_HDRLEN) ||
3361 (doff > (pfirst->len - SDPCM_HDRLEN))) {
3362 DHD_ERROR(("%s (superframe): Bad data offset %d: HW %d "
3364 __func__, doff, sublen,
3365 pfirst->len, SDPCM_HDRLEN));
3369 /* Check sequence number of superframe SW header */
3371 DHD_INFO(("%s: (superframe) rx_seq %d, expected %d\n",
3372 __func__, seq, rxseq));
3377 /* Check window for sanity */
3378 if ((u8) (txmax - bus->tx_seq) > 0x40) {
3379 DHD_ERROR(("%s: unlikely tx max %d with tx_seq %d\n",
3380 __func__, txmax, bus->tx_seq));
3381 txmax = bus->tx_seq + 2;
3383 bus->tx_max = txmax;
3385 /* Remove superframe header, remember offset */
3386 skb_pull(pfirst, doff);
3389 /* Validate all the subframe headers */
3390 for (num = 0, pnext = pfirst; pnext && !errcode;
3391 num++, pnext = pnext->next) {
3392 dptr = (u8 *) (pnext->data);
3393 dlen = (u16) (pnext->len);
3394 sublen = get_unaligned_le16(dptr);
3395 check = get_unaligned_le16(dptr + sizeof(u16));
3396 chan = SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]);
3397 doff = SDPCM_DOFFSET_VALUE(&dptr[SDPCM_FRAMETAG_LEN]);
3400 prhex("subframe", dptr, 32);
3403 if ((u16)~(sublen ^ check)) {
3404 DHD_ERROR(("%s (subframe %d): HW hdr error: "
3405 "len/check 0x%04x/0x%04x\n",
3406 __func__, num, sublen, check));
3408 } else if ((sublen > dlen) || (sublen < SDPCM_HDRLEN)) {
3409 DHD_ERROR(("%s (subframe %d): length mismatch: "
3410 "len 0x%04x, expect 0x%04x\n",
3411 __func__, num, sublen, dlen));
3413 } else if ((chan != SDPCM_DATA_CHANNEL) &&
3414 (chan != SDPCM_EVENT_CHANNEL)) {
3415 DHD_ERROR(("%s (subframe %d): bad channel %d\n",
3416 __func__, num, chan));
3418 } else if ((doff < SDPCM_HDRLEN) || (doff > sublen)) {
3419 DHD_ERROR(("%s (subframe %d): Bad data offset %d: HW %d min %d\n",
3420 __func__, num, doff, sublen,
3427 /* Terminate frame on error, request
3429 if (bus->glomerr++ < 3) {
3430 /* Restore superframe header space */
3431 skb_push(pfirst, sfdoff);
3432 dhdsdio_rxfail(bus, true, true);
3435 dhdsdio_rxfail(bus, true, false);
3436 dhd_os_sdlock_rxq(bus->dhd);
3437 pkt_buf_free_skb(bus->glom);
3438 dhd_os_sdunlock_rxq(bus->dhd);
3446 /* Basic SD framing looks ok - process each packet (header) */
3447 save_pfirst = pfirst;
3451 dhd_os_sdlock_rxq(bus->dhd);
3452 for (num = 0; pfirst; rxseq++, pfirst = pnext) {
3453 pnext = pfirst->next;
3454 pfirst->next = NULL;
3456 dptr = (u8 *) (pfirst->data);
3457 sublen = get_unaligned_le16(dptr);
3458 chan = SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]);
3459 seq = SDPCM_PACKET_SEQUENCE(&dptr[SDPCM_FRAMETAG_LEN]);
3460 doff = SDPCM_DOFFSET_VALUE(&dptr[SDPCM_FRAMETAG_LEN]);
3462 DHD_GLOM(("%s: Get subframe %d, %p(%p/%d), sublen %d "
3464 __func__, num, pfirst, pfirst->data,
3465 pfirst->len, sublen, chan, seq));
3467 ASSERT((chan == SDPCM_DATA_CHANNEL)
3468 || (chan == SDPCM_EVENT_CHANNEL));
3471 DHD_GLOM(("%s: rx_seq %d, expected %d\n",
3472 __func__, seq, rxseq));
3477 if (DHD_BYTES_ON() && DHD_DATA_ON())
3478 prhex("Rx Subframe Data", dptr, dlen);
3481 __skb_trim(pfirst, sublen);
3482 skb_pull(pfirst, doff);
3484 if (pfirst->len == 0) {
3485 pkt_buf_free_skb(pfirst);
3487 plast->next = pnext;
3489 ASSERT(save_pfirst == pfirst);
3490 save_pfirst = pnext;
3493 } else if (dhd_prot_hdrpull(bus->dhd, &ifidx, pfirst) !=
3495 DHD_ERROR(("%s: rx protocol error\n",
3497 bus->dhd->rx_errors++;
3498 pkt_buf_free_skb(pfirst);
3500 plast->next = pnext;
3502 ASSERT(save_pfirst == pfirst);
3503 save_pfirst = pnext;
3508 /* this packet will go up, link back into
3509 chain and count it */
3510 pfirst->next = pnext;
3515 if (DHD_GLOM_ON()) {
3516 DHD_GLOM(("%s subframe %d to stack, %p(%p/%d) "
3518 __func__, num, pfirst, pfirst->data,
3519 pfirst->len, pfirst->next,
3521 prhex("", (u8 *) pfirst->data,
3522 min_t(int, pfirst->len, 32));
3524 #endif /* DHD_DEBUG */
3526 dhd_os_sdunlock_rxq(bus->dhd);
3528 dhd_os_sdunlock(bus->dhd);
3529 dhd_rx_frame(bus->dhd, ifidx, save_pfirst, num);
3530 dhd_os_sdlock(bus->dhd);
3533 bus->rxglomframes++;
3534 bus->rxglompkts += num;
3539 /* Return true if there may be more frames to read */
3540 static uint dhdsdio_readframes(dhd_bus_t *bus, uint maxframes, bool *finished)
3542 bcmsdh_info_t *sdh = bus->sdh;
3544 u16 len, check; /* Extracted hardware header fields */
3545 u8 chan, seq, doff; /* Extracted software header fields */
3546 u8 fcbits; /* Extracted fcbits from software header */
3549 struct sk_buff *pkt; /* Packet for event or data frames */
3550 u16 pad; /* Number of pad bytes to read */
3551 u16 rdlen; /* Total number of bytes to read */
3552 u8 rxseq; /* Next sequence number to expect */
3553 uint rxleft = 0; /* Remaining number of frames allowed */
3554 int sdret; /* Return code from bcmsdh calls */
3555 u8 txmax; /* Maximum tx sequence offered */
3556 bool len_consistent; /* Result of comparing readahead len and
3560 uint rxcount = 0; /* Total frames read */
3562 #if defined(DHD_DEBUG) || defined(SDTEST)
3563 bool sdtest = false; /* To limit message spew from test mode */
3566 DHD_TRACE(("%s: Enter\n", __func__));
3571 /* Allow pktgen to override maxframes */
3572 if (bus->pktgen_count && (bus->pktgen_mode == DHD_PKTGEN_RECV)) {
3573 maxframes = bus->pktgen_count;
3578 /* Not finished unless we encounter no more frames indication */
3581 for (rxseq = bus->rx_seq, rxleft = maxframes;
3582 !bus->rxskip && rxleft && bus->dhd->busstate != DHD_BUS_DOWN;
3583 rxseq++, rxleft--) {
3585 /* Handle glomming separately */
3586 if (bus->glom || bus->glomd) {
3588 DHD_GLOM(("%s: calling rxglom: glomd %p, glom %p\n",
3589 __func__, bus->glomd, bus->glom));
3590 cnt = dhdsdio_rxglom(bus, rxseq);
3591 DHD_GLOM(("%s: rxglom returned %d\n", __func__, cnt));
3593 rxleft = (rxleft > cnt) ? (rxleft - cnt) : 1;
3597 /* Try doing single read if we can */
3598 if (dhd_readahead && bus->nextlen) {
3599 u16 nextlen = bus->nextlen;
3602 if (bus->bus == SPI_BUS) {
3603 rdlen = len = nextlen;
3605 rdlen = len = nextlen << 4;
3607 /* Pad read to blocksize for efficiency */
3608 if (bus->roundup && bus->blocksize
3609 && (rdlen > bus->blocksize)) {
3612 (rdlen % bus->blocksize);
3613 if ((pad <= bus->roundup)
3614 && (pad < bus->blocksize)
3615 && ((rdlen + pad + firstread) <
3618 } else if (rdlen % DHD_SDALIGN) {
3620 DHD_SDALIGN - (rdlen % DHD_SDALIGN);
3624 /* We use bus->rxctl buffer in WinXP for initial
3625 * control pkt receives.
3626 * Later we use buffer-poll for data as well
3627 * as control packets.
3628 * This is required because dhd receives full
3629 * frame in gSPI unlike SDIO.
3630 * After the frame is received we have to
3631 * distinguish whether it is data
3632 * or non-data frame.
3634 /* Allocate a packet buffer */
3635 dhd_os_sdlock_rxq(bus->dhd);
3636 pkt = pkt_buf_get_skb(rdlen + DHD_SDALIGN);
3638 if (bus->bus == SPI_BUS) {
3639 bus->usebufpool = false;
3640 bus->rxctl = bus->rxbuf;
3642 bus->rxctl += firstread;
3643 pad = ((unsigned long)bus->rxctl %
3647 (DHD_SDALIGN - pad);
3648 bus->rxctl -= firstread;
3650 ASSERT(bus->rxctl >= bus->rxbuf);
3652 /* Read the entire frame */
3653 sdret = dhd_bcmsdh_recv_buf(bus,
3662 ASSERT(sdret != -BCME_PENDING);
3664 /* Control frame failures need
3667 DHD_ERROR(("%s: read %d control bytes failed: %d\n",
3670 /* dhd.rx_ctlerrs is higher */
3672 dhd_os_sdunlock_rxq(bus->dhd);
3673 dhdsdio_rxfail(bus, true,
3681 request rtx of events */
3682 DHD_ERROR(("%s (nextlen): pkt_buf_get_skb failed: len %d rdlen %d " "expected rxseq %d\n",
3683 __func__, len, rdlen, rxseq));
3684 /* Just go try again w/normal
3686 dhd_os_sdunlock_rxq(bus->dhd);
3690 if (bus->bus == SPI_BUS)
3691 bus->usebufpool = true;
3693 ASSERT(!(pkt->prev));
3694 PKTALIGN(pkt, rdlen, DHD_SDALIGN);
3695 rxbuf = (u8 *) (pkt->data);
3696 /* Read the entire frame */
3698 dhd_bcmsdh_recv_buf(bus,
3699 bcmsdh_cur_sbwad(sdh),
3700 SDIO_FUNC_2, F2SYNC,
3701 rxbuf, rdlen, pkt, NULL,
3704 ASSERT(sdret != -BCME_PENDING);
3707 DHD_ERROR(("%s (nextlen): read %d bytes failed: %d\n",
3708 __func__, rdlen, sdret));
3709 pkt_buf_free_skb(pkt);
3710 bus->dhd->rx_errors++;
3711 dhd_os_sdunlock_rxq(bus->dhd);
3712 /* Force retry w/normal header read.
3713 * Don't attempt NAK for
3716 dhdsdio_rxfail(bus, true,
3723 dhd_os_sdunlock_rxq(bus->dhd);
3725 /* Now check the header */
3726 memcpy(bus->rxhdr, rxbuf, SDPCM_HDRLEN);
3728 /* Extract hardware header fields */
3729 len = get_unaligned_le16(bus->rxhdr);
3730 check = get_unaligned_le16(bus->rxhdr + sizeof(u16));
3732 /* All zeros means readahead info was bad */
3733 if (!(len | check)) {
3734 DHD_INFO(("%s (nextlen): read zeros in HW "
3735 "header???\n", __func__));
3736 dhd_os_sdlock_rxq(bus->dhd);
3738 dhd_os_sdunlock_rxq(bus->dhd);
3739 GSPI_PR55150_BAILOUT;
3743 /* Validate check bytes */
3744 if ((u16)~(len ^ check)) {
3745 DHD_ERROR(("%s (nextlen): HW hdr error: nextlen/len/check" " 0x%04x/0x%04x/0x%04x\n",
3746 __func__, nextlen, len, check));
3747 dhd_os_sdlock_rxq(bus->dhd);
3749 dhd_os_sdunlock_rxq(bus->dhd);
3751 dhdsdio_rxfail(bus, false, false);
3752 GSPI_PR55150_BAILOUT;
3756 /* Validate frame length */
3757 if (len < SDPCM_HDRLEN) {
3758 DHD_ERROR(("%s (nextlen): HW hdr length "
3759 "invalid: %d\n", __func__, len));
3760 dhd_os_sdlock_rxq(bus->dhd);
3762 dhd_os_sdunlock_rxq(bus->dhd);
3763 GSPI_PR55150_BAILOUT;
3767 /* Check for consistency withreadahead info */
3768 len_consistent = (nextlen != (roundup(len, 16) >> 4));
3769 if (len_consistent) {
3770 /* Mismatch, force retry w/normal
3771 header (may be >4K) */
3772 DHD_ERROR(("%s (nextlen): mismatch, nextlen %d len %d rnd %d; " "expected rxseq %d\n",
3774 len, roundup(len, 16), rxseq));
3775 dhd_os_sdlock_rxq(bus->dhd);
3777 dhd_os_sdunlock_rxq(bus->dhd);
3778 dhdsdio_rxfail(bus, true,
3780 SPI_BUS) ? false : true);
3781 GSPI_PR55150_BAILOUT;
3785 /* Extract software header fields */
3787 SDPCM_PACKET_CHANNEL(&bus->rxhdr
3788 [SDPCM_FRAMETAG_LEN]);
3790 SDPCM_PACKET_SEQUENCE(&bus->rxhdr
3791 [SDPCM_FRAMETAG_LEN]);
3793 SDPCM_DOFFSET_VALUE(&bus->rxhdr
3794 [SDPCM_FRAMETAG_LEN]);
3796 SDPCM_WINDOW_VALUE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
3799 bus->rxhdr[SDPCM_FRAMETAG_LEN +
3800 SDPCM_NEXTLEN_OFFSET];
3801 if ((bus->nextlen << 4) > MAX_RX_DATASZ) {
3802 DHD_INFO(("%s (nextlen): got frame w/nextlen too large" " (%d), seq %d\n",
3803 __func__, bus->nextlen, seq));
3807 bus->dhd->rx_readahead_cnt++;
3808 /* Handle Flow Control */
3810 SDPCM_FCMASK_VALUE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
3813 if (~bus->flowcontrol & fcbits) {
3817 if (bus->flowcontrol & ~fcbits) {
3824 bus->flowcontrol = fcbits;
3827 /* Check and update sequence number */
3829 DHD_INFO(("%s (nextlen): rx_seq %d, expected "
3830 "%d\n", __func__, seq, rxseq));
3835 /* Check window for sanity */
3836 if ((u8) (txmax - bus->tx_seq) > 0x40) {
3837 DHD_ERROR(("%s: got unlikely tx max %d with "
3839 __func__, txmax, bus->tx_seq));
3840 txmax = bus->tx_seq + 2;
3842 bus->tx_max = txmax;
3845 if (DHD_BYTES_ON() && DHD_DATA_ON())
3846 prhex("Rx Data", rxbuf, len);
3847 else if (DHD_HDRS_ON())
3848 prhex("RxHdr", bus->rxhdr, SDPCM_HDRLEN);
3851 if (chan == SDPCM_CONTROL_CHANNEL) {
3852 if (bus->bus == SPI_BUS) {
3853 dhdsdio_read_control(bus, rxbuf, len,
3855 if (bus->usebufpool) {
3856 dhd_os_sdlock_rxq(bus->dhd);
3857 pkt_buf_free_skb(pkt);
3858 dhd_os_sdunlock_rxq(bus->dhd);
3862 DHD_ERROR(("%s (nextlen): readahead on control" " packet %d?\n",
3864 /* Force retry w/normal header read */
3866 dhdsdio_rxfail(bus, false, true);
3867 dhd_os_sdlock_rxq(bus->dhd);
3869 dhd_os_sdunlock_rxq(bus->dhd);
3874 if ((bus->bus == SPI_BUS) && !bus->usebufpool) {
3875 DHD_ERROR(("Received %d bytes on %d channel. Running out of " "rx pktbuf's or not yet malloced.\n",
3880 /* Validate data offset */
3881 if ((doff < SDPCM_HDRLEN) || (doff > len)) {
3882 DHD_ERROR(("%s (nextlen): bad data offset %d: HW len %d min %d\n",
3883 __func__, doff, len, SDPCM_HDRLEN));
3884 dhd_os_sdlock_rxq(bus->dhd);
3886 dhd_os_sdunlock_rxq(bus->dhd);
3888 dhdsdio_rxfail(bus, false, false);
3892 /* All done with this one -- now deliver the packet */
3895 /* gSPI frames should not be handled in fractions */
3896 if (bus->bus == SPI_BUS)
3899 /* Read frame header (hardware and software) */
3901 dhd_bcmsdh_recv_buf(bus, bcmsdh_cur_sbwad(sdh), SDIO_FUNC_2,
3902 F2SYNC, bus->rxhdr, firstread, NULL,
3905 ASSERT(sdret != -BCME_PENDING);
3908 DHD_ERROR(("%s: RXHEADER FAILED: %d\n", __func__,
3911 dhdsdio_rxfail(bus, true, true);
3915 if (DHD_BYTES_ON() || DHD_HDRS_ON())
3916 prhex("RxHdr", bus->rxhdr, SDPCM_HDRLEN);
3919 /* Extract hardware header fields */
3920 len = get_unaligned_le16(bus->rxhdr);
3921 check = get_unaligned_le16(bus->rxhdr + sizeof(u16));
3923 /* All zeros means no more frames */
3924 if (!(len | check)) {
3929 /* Validate check bytes */
3930 if ((u16) ~(len ^ check)) {
3931 DHD_ERROR(("%s: HW hdr err: len/check 0x%04x/0x%04x\n",
3932 __func__, len, check));
3934 dhdsdio_rxfail(bus, false, false);
3938 /* Validate frame length */
3939 if (len < SDPCM_HDRLEN) {
3940 DHD_ERROR(("%s: HW hdr length invalid: %d\n",
3945 /* Extract software header fields */
3946 chan = SDPCM_PACKET_CHANNEL(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
3947 seq = SDPCM_PACKET_SEQUENCE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
3948 doff = SDPCM_DOFFSET_VALUE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
3949 txmax = SDPCM_WINDOW_VALUE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
3951 /* Validate data offset */
3952 if ((doff < SDPCM_HDRLEN) || (doff > len)) {
3953 DHD_ERROR(("%s: Bad data offset %d: HW len %d, min %d "
3955 __func__, doff, len, SDPCM_HDRLEN, seq));
3958 dhdsdio_rxfail(bus, false, false);
3962 /* Save the readahead length if there is one */
3964 bus->rxhdr[SDPCM_FRAMETAG_LEN + SDPCM_NEXTLEN_OFFSET];
3965 if ((bus->nextlen << 4) > MAX_RX_DATASZ) {
3966 DHD_INFO(("%s (nextlen): got frame w/nextlen too large "
3968 __func__, bus->nextlen, seq));
3972 /* Handle Flow Control */
3973 fcbits = SDPCM_FCMASK_VALUE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
3976 if (~bus->flowcontrol & fcbits) {
3980 if (bus->flowcontrol & ~fcbits) {
3987 bus->flowcontrol = fcbits;
3990 /* Check and update sequence number */
3992 DHD_INFO(("%s: rx_seq %d, expected %d\n", __func__,
3998 /* Check window for sanity */
3999 if ((u8) (txmax - bus->tx_seq) > 0x40) {
4000 DHD_ERROR(("%s: unlikely tx max %d with tx_seq %d\n",
4001 __func__, txmax, bus->tx_seq));
4002 txmax = bus->tx_seq + 2;
4004 bus->tx_max = txmax;
4006 /* Call a separate function for control frames */
4007 if (chan == SDPCM_CONTROL_CHANNEL) {
4008 dhdsdio_read_control(bus, bus->rxhdr, len, doff);
4012 ASSERT((chan == SDPCM_DATA_CHANNEL)
4013 || (chan == SDPCM_EVENT_CHANNEL)
4014 || (chan == SDPCM_TEST_CHANNEL)
4015 || (chan == SDPCM_GLOM_CHANNEL));
4017 /* Length to read */
4018 rdlen = (len > firstread) ? (len - firstread) : 0;
4020 /* May pad read to blocksize for efficiency */
4021 if (bus->roundup && bus->blocksize &&
4022 (rdlen > bus->blocksize)) {
4023 pad = bus->blocksize - (rdlen % bus->blocksize);
4024 if ((pad <= bus->roundup) && (pad < bus->blocksize) &&
4025 ((rdlen + pad + firstread) < MAX_RX_DATASZ))
4027 } else if (rdlen % DHD_SDALIGN) {
4028 rdlen += DHD_SDALIGN - (rdlen % DHD_SDALIGN);
4031 /* Satisfy length-alignment requirements */
4032 if (forcealign && (rdlen & (ALIGNMENT - 1)))
4033 rdlen = roundup(rdlen, ALIGNMENT);
4035 if ((rdlen + firstread) > MAX_RX_DATASZ) {
4036 /* Too long -- skip this frame */
4037 DHD_ERROR(("%s: too long: len %d rdlen %d\n",
4038 __func__, len, rdlen));
4039 bus->dhd->rx_errors++;
4041 dhdsdio_rxfail(bus, false, false);
4045 dhd_os_sdlock_rxq(bus->dhd);
4046 pkt = pkt_buf_get_skb(rdlen + firstread + DHD_SDALIGN);
4048 /* Give up on data, request rtx of events */
4049 DHD_ERROR(("%s: pkt_buf_get_skb failed: rdlen %d chan %d\n",
4050 __func__, rdlen, chan));
4051 bus->dhd->rx_dropped++;
4052 dhd_os_sdunlock_rxq(bus->dhd);
4053 dhdsdio_rxfail(bus, false, RETRYCHAN(chan));
4056 dhd_os_sdunlock_rxq(bus->dhd);
4058 ASSERT(!(pkt->prev));
4060 /* Leave room for what we already read, and align remainder */
4061 ASSERT(firstread < pkt->len);
4062 skb_pull(pkt, firstread);
4063 PKTALIGN(pkt, rdlen, DHD_SDALIGN);
4065 /* Read the remaining frame data */
4067 dhd_bcmsdh_recv_buf(bus, bcmsdh_cur_sbwad(sdh), SDIO_FUNC_2,
4068 F2SYNC, ((u8 *) (pkt->data)), rdlen,
4071 ASSERT(sdret != -BCME_PENDING);
4074 DHD_ERROR(("%s: read %d %s bytes failed: %d\n",
4077 SDPCM_EVENT_CHANNEL) ? "event" : ((chan ==
4079 ? "data" : "test")),
4081 dhd_os_sdlock_rxq(bus->dhd);
4082 pkt_buf_free_skb(pkt);
4083 dhd_os_sdunlock_rxq(bus->dhd);
4084 bus->dhd->rx_errors++;
4085 dhdsdio_rxfail(bus, true, RETRYCHAN(chan));
4089 /* Copy the already-read portion */
4090 skb_push(pkt, firstread);
4091 memcpy(pkt->data, bus->rxhdr, firstread);
4094 if (DHD_BYTES_ON() && DHD_DATA_ON())
4095 prhex("Rx Data", pkt->data, len);
4099 /* Save superframe descriptor and allocate packet frame */
4100 if (chan == SDPCM_GLOM_CHANNEL) {
4101 if (SDPCM_GLOMDESC(&bus->rxhdr[SDPCM_FRAMETAG_LEN])) {
4102 DHD_GLOM(("%s: glom descriptor, %d bytes:\n",
4105 if (DHD_GLOM_ON()) {
4106 prhex("Glom Data", pkt->data, len);
4109 __skb_trim(pkt, len);
4110 ASSERT(doff == SDPCM_HDRLEN);
4111 skb_pull(pkt, SDPCM_HDRLEN);
4114 DHD_ERROR(("%s: glom superframe w/o "
4115 "descriptor!\n", __func__));
4116 dhdsdio_rxfail(bus, false, false);
4121 /* Fill in packet len and prio, deliver upward */
4122 __skb_trim(pkt, len);
4123 skb_pull(pkt, doff);
4126 /* Test channel packets are processed separately */
4127 if (chan == SDPCM_TEST_CHANNEL) {
4128 dhdsdio_testrcv(bus, pkt, seq);
4133 if (pkt->len == 0) {
4134 dhd_os_sdlock_rxq(bus->dhd);
4135 pkt_buf_free_skb(pkt);
4136 dhd_os_sdunlock_rxq(bus->dhd);
4138 } else if (dhd_prot_hdrpull(bus->dhd, &ifidx, pkt) != 0) {
4139 DHD_ERROR(("%s: rx protocol error\n", __func__));
4140 dhd_os_sdlock_rxq(bus->dhd);
4141 pkt_buf_free_skb(pkt);
4142 dhd_os_sdunlock_rxq(bus->dhd);
4143 bus->dhd->rx_errors++;
4147 /* Unlock during rx call */
4148 dhd_os_sdunlock(bus->dhd);
4149 dhd_rx_frame(bus->dhd, ifidx, pkt, 1);
4150 dhd_os_sdlock(bus->dhd);
4152 rxcount = maxframes - rxleft;
4154 /* Message if we hit the limit */
4155 if (!rxleft && !sdtest)
4156 DHD_DATA(("%s: hit rx limit of %d frames\n", __func__,
4159 #endif /* DHD_DEBUG */
4160 DHD_DATA(("%s: processed %d frames\n", __func__, rxcount));
4161 /* Back off rxseq if awaiting rtx, update rx_seq */
4164 bus->rx_seq = rxseq;
4169 static u32 dhdsdio_hostmail(dhd_bus_t *bus)
4171 sdpcmd_regs_t *regs = bus->regs;
4177 DHD_TRACE(("%s: Enter\n", __func__));
4179 /* Read mailbox data and ack that we did so */
4180 R_SDREG(hmb_data, ®s->tohostmailboxdata, retries);
4181 if (retries <= retry_limit)
4182 W_SDREG(SMB_INT_ACK, ®s->tosbmailbox, retries);
4183 bus->f1regdata += 2;
4185 /* Dongle recomposed rx frames, accept them again */
4186 if (hmb_data & HMB_DATA_NAKHANDLED) {
4187 DHD_INFO(("Dongle reports NAK handled, expect rtx of %d\n",
4190 DHD_ERROR(("%s: unexpected NAKHANDLED!\n", __func__));
4192 bus->rxskip = false;
4193 intstatus |= I_HMB_FRAME_IND;
4197 * DEVREADY does not occur with gSPI.
4199 if (hmb_data & (HMB_DATA_DEVREADY | HMB_DATA_FWREADY)) {
4201 (hmb_data & HMB_DATA_VERSION_MASK) >>
4202 HMB_DATA_VERSION_SHIFT;
4203 if (bus->sdpcm_ver != SDPCM_PROT_VERSION)
4204 DHD_ERROR(("Version mismatch, dongle reports %d, "
4206 bus->sdpcm_ver, SDPCM_PROT_VERSION));
4208 DHD_INFO(("Dongle ready, protocol version %d\n",
4213 * Flow Control has been moved into the RX headers and this out of band
4214 * method isn't used any more. Leae this here for possibly
4215 * remaining backward
4216 * compatible with older dongles
4218 if (hmb_data & HMB_DATA_FC) {
4220 (hmb_data & HMB_DATA_FCDATA_MASK) >> HMB_DATA_FCDATA_SHIFT;
4222 if (fcbits & ~bus->flowcontrol)
4224 if (bus->flowcontrol & ~fcbits)
4228 bus->flowcontrol = fcbits;
4231 /* Shouldn't be any others */
4232 if (hmb_data & ~(HMB_DATA_DEVREADY |
4233 HMB_DATA_NAKHANDLED |
4236 HMB_DATA_FCDATA_MASK | HMB_DATA_VERSION_MASK)) {
4237 DHD_ERROR(("Unknown mailbox data content: 0x%02x\n", hmb_data));
4243 bool dhdsdio_dpc(dhd_bus_t *bus)
4245 bcmsdh_info_t *sdh = bus->sdh;
4246 sdpcmd_regs_t *regs = bus->regs;
4247 u32 intstatus, newstatus = 0;
4249 uint rxlimit = dhd_rxbound; /* Rx frames to read before resched */
4250 uint txlimit = dhd_txbound; /* Tx frames to send before resched */
4251 uint framecnt = 0; /* Temporary counter of tx/rx frames */
4252 bool rxdone = true; /* Flag for no more read data */
4253 bool resched = false; /* Flag indicating resched wanted */
4255 DHD_TRACE(("%s: Enter\n", __func__));
4257 /* Start with leftover status bits */
4258 intstatus = bus->intstatus;
4260 dhd_os_sdlock(bus->dhd);
4262 /* If waiting for HTAVAIL, check status */
4263 if (bus->clkstate == CLK_PENDING) {
4265 u8 clkctl, devctl = 0;
4268 /* Check for inconsistent device control */
4270 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL, &err);
4272 DHD_ERROR(("%s: error reading DEVCTL: %d\n",
4274 bus->dhd->busstate = DHD_BUS_DOWN;
4276 ASSERT(devctl & SBSDIO_DEVCTL_CA_INT_ONLY);
4278 #endif /* DHD_DEBUG */
4280 /* Read CSR, if clock on switch to AVAIL, else ignore */
4282 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
4285 DHD_ERROR(("%s: error reading CSR: %d\n", __func__,
4287 bus->dhd->busstate = DHD_BUS_DOWN;
4290 DHD_INFO(("DPC: PENDING, devctl 0x%02x clkctl 0x%02x\n", devctl,
4293 if (SBSDIO_HTAV(clkctl)) {
4295 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
4298 DHD_ERROR(("%s: error reading DEVCTL: %d\n",
4300 bus->dhd->busstate = DHD_BUS_DOWN;
4302 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
4303 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
4306 DHD_ERROR(("%s: error writing DEVCTL: %d\n",
4308 bus->dhd->busstate = DHD_BUS_DOWN;
4310 bus->clkstate = CLK_AVAIL;
4318 /* Make sure backplane clock is on */
4319 dhdsdio_clkctl(bus, CLK_AVAIL, true);
4320 if (bus->clkstate == CLK_PENDING)
4323 /* Pending interrupt indicates new device status */
4326 R_SDREG(newstatus, ®s->intstatus, retries);
4328 if (bcmsdh_regfail(bus->sdh))
4330 newstatus &= bus->hostintmask;
4331 bus->fcstate = !!(newstatus & I_HMB_FC_STATE);
4333 W_SDREG(newstatus, ®s->intstatus, retries);
4338 /* Merge new bits with previous */
4339 intstatus |= newstatus;
4342 /* Handle flow-control change: read new state in case our ack
4343 * crossed another change interrupt. If change still set, assume
4344 * FC ON for safety, let next loop through do the debounce.
4346 if (intstatus & I_HMB_FC_CHANGE) {
4347 intstatus &= ~I_HMB_FC_CHANGE;
4348 W_SDREG(I_HMB_FC_CHANGE, ®s->intstatus, retries);
4349 R_SDREG(newstatus, ®s->intstatus, retries);
4350 bus->f1regdata += 2;
4352 !!(newstatus & (I_HMB_FC_STATE | I_HMB_FC_CHANGE));
4353 intstatus |= (newstatus & bus->hostintmask);
4356 /* Handle host mailbox indication */
4357 if (intstatus & I_HMB_HOST_INT) {
4358 intstatus &= ~I_HMB_HOST_INT;
4359 intstatus |= dhdsdio_hostmail(bus);
4362 /* Generally don't ask for these, can get CRC errors... */
4363 if (intstatus & I_WR_OOSYNC) {
4364 DHD_ERROR(("Dongle reports WR_OOSYNC\n"));
4365 intstatus &= ~I_WR_OOSYNC;
4368 if (intstatus & I_RD_OOSYNC) {
4369 DHD_ERROR(("Dongle reports RD_OOSYNC\n"));
4370 intstatus &= ~I_RD_OOSYNC;
4373 if (intstatus & I_SBINT) {
4374 DHD_ERROR(("Dongle reports SBINT\n"));
4375 intstatus &= ~I_SBINT;
4378 /* Would be active due to wake-wlan in gSPI */
4379 if (intstatus & I_CHIPACTIVE) {
4380 DHD_INFO(("Dongle reports CHIPACTIVE\n"));
4381 intstatus &= ~I_CHIPACTIVE;
4384 /* Ignore frame indications if rxskip is set */
4386 intstatus &= ~I_HMB_FRAME_IND;
4388 /* On frame indication, read available frames */
4389 if (PKT_AVAILABLE()) {
4390 framecnt = dhdsdio_readframes(bus, rxlimit, &rxdone);
4391 if (rxdone || bus->rxskip)
4392 intstatus &= ~I_HMB_FRAME_IND;
4393 rxlimit -= min(framecnt, rxlimit);
4396 /* Keep still-pending events for next scheduling */
4397 bus->intstatus = intstatus;
4400 #if defined(OOB_INTR_ONLY)
4401 bcmsdh_oob_intr_set(1);
4402 #endif /* (OOB_INTR_ONLY) */
4403 /* Re-enable interrupts to detect new device events (mailbox, rx frame)
4404 * or clock availability. (Allows tx loop to check ipend if desired.)
4405 * (Unless register access seems hosed, as we may not be able to ACK...)
4407 if (bus->intr && bus->intdis && !bcmsdh_regfail(sdh)) {
4408 DHD_INTR(("%s: enable SDIO interrupts, rxdone %d framecnt %d\n",
4409 __func__, rxdone, framecnt));
4410 bus->intdis = false;
4411 bcmsdh_intr_enable(sdh);
4414 if (DATAOK(bus) && bus->ctrl_frame_stat &&
4415 (bus->clkstate == CLK_AVAIL)) {
4419 dhd_bcmsdh_send_buf(bus, bcmsdh_cur_sbwad(sdh), SDIO_FUNC_2,
4420 F2SYNC, (u8 *) bus->ctrl_frame_buf,
4421 (u32) bus->ctrl_frame_len, NULL,
4423 ASSERT(ret != -BCME_PENDING);
4426 /* On failure, abort the command and
4427 terminate the frame */
4428 DHD_INFO(("%s: sdio error %d, abort command and "
4429 "terminate frame.\n", __func__, ret));
4432 bcmsdh_abort(sdh, SDIO_FUNC_2);
4434 bcmsdh_cfg_write(sdh, SDIO_FUNC_1,
4435 SBSDIO_FUNC1_FRAMECTRL, SFC_WF_TERM,
4439 for (i = 0; i < 3; i++) {
4441 hi = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
4442 SBSDIO_FUNC1_WFRAMEBCHI,
4444 lo = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
4445 SBSDIO_FUNC1_WFRAMEBCLO,
4447 bus->f1regdata += 2;
4448 if ((hi == 0) && (lo == 0))
4454 bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQUENCE_WRAP;
4456 DHD_INFO(("Return_dpc value is : %d\n", ret));
4457 bus->ctrl_frame_stat = false;
4458 dhd_wait_event_wakeup(bus->dhd);
4460 /* Send queued frames (limit 1 if rx may still be pending) */
4461 else if ((bus->clkstate == CLK_AVAIL) && !bus->fcstate &&
4462 pktq_mlen(&bus->txq, ~bus->flowcontrol) && txlimit
4464 framecnt = rxdone ? txlimit : min(txlimit, dhd_txminmax);
4465 framecnt = dhdsdio_sendfromq(bus, framecnt);
4466 txlimit -= framecnt;
4469 /* Resched if events or tx frames are pending,
4470 else await next interrupt */
4471 /* On failed register access, all bets are off:
4472 no resched or interrupts */
4473 if ((bus->dhd->busstate == DHD_BUS_DOWN) || bcmsdh_regfail(sdh)) {
4474 DHD_ERROR(("%s: failed backplane access over SDIO, halting "
4475 "operation %d\n", __func__, bcmsdh_regfail(sdh)));
4476 bus->dhd->busstate = DHD_BUS_DOWN;
4478 } else if (bus->clkstate == CLK_PENDING) {
4479 DHD_INFO(("%s: rescheduled due to CLK_PENDING awaiting "
4480 "I_CHIPACTIVE interrupt\n", __func__));
4482 } else if (bus->intstatus || bus->ipend ||
4483 (!bus->fcstate && pktq_mlen(&bus->txq, ~bus->flowcontrol) &&
4484 DATAOK(bus)) || PKT_AVAILABLE()) {
4488 bus->dpc_sched = resched;
4490 /* If we're done for now, turn off clock request. */
4491 if ((bus->clkstate != CLK_PENDING)
4492 && bus->idletime == DHD_IDLE_IMMEDIATE) {
4493 bus->activity = false;
4494 dhdsdio_clkctl(bus, CLK_NONE, false);
4497 dhd_os_sdunlock(bus->dhd);
4502 bool dhd_bus_dpc(struct dhd_bus *bus)
4506 /* Call the DPC directly. */
4507 DHD_TRACE(("Calling dhdsdio_dpc() from %s\n", __func__));
4508 resched = dhdsdio_dpc(bus);
4513 void dhdsdio_isr(void *arg)
4515 dhd_bus_t *bus = (dhd_bus_t *) arg;
4518 DHD_TRACE(("%s: Enter\n", __func__));
4521 DHD_ERROR(("%s : bus is null pointer , exit\n", __func__));
4526 if (bus->dhd->busstate == DHD_BUS_DOWN) {
4527 DHD_ERROR(("%s : bus is down. we have nothing to do\n",
4531 /* Count the interrupt call */
4535 /* Shouldn't get this interrupt if we're sleeping? */
4536 if (bus->sleeping) {
4537 DHD_ERROR(("INTERRUPT WHILE SLEEPING??\n"));
4541 /* Disable additional interrupts (is this needed now)? */
4543 DHD_INTR(("%s: disable SDIO interrupts\n", __func__));
4545 DHD_ERROR(("dhdsdio_isr() w/o interrupt configured!\n"));
4547 bcmsdh_intr_disable(sdh);
4550 #if defined(SDIO_ISR_THREAD)
4551 DHD_TRACE(("Calling dhdsdio_dpc() from %s\n", __func__));
4552 while (dhdsdio_dpc(bus))
4555 bus->dpc_sched = true;
4556 dhd_sched_dpc(bus->dhd);
4562 static void dhdsdio_pktgen_init(dhd_bus_t *bus)
4564 /* Default to specified length, or full range */
4565 if (dhd_pktgen_len) {
4566 bus->pktgen_maxlen = min(dhd_pktgen_len, MAX_PKTGEN_LEN);
4567 bus->pktgen_minlen = bus->pktgen_maxlen;
4569 bus->pktgen_maxlen = MAX_PKTGEN_LEN;
4570 bus->pktgen_minlen = 0;
4572 bus->pktgen_len = (u16) bus->pktgen_minlen;
4574 /* Default to per-watchdog burst with 10s print time */
4575 bus->pktgen_freq = 1;
4576 bus->pktgen_print = 10000 / dhd_watchdog_ms;
4577 bus->pktgen_count = (dhd_pktgen * dhd_watchdog_ms + 999) / 1000;
4579 /* Default to echo mode */
4580 bus->pktgen_mode = DHD_PKTGEN_ECHO;
4581 bus->pktgen_stop = 1;
4584 static void dhdsdio_pktgen(dhd_bus_t *bus)
4586 struct sk_buff *pkt;
4592 /* Display current count if appropriate */
4593 if (bus->pktgen_print && (++bus->pktgen_ptick >= bus->pktgen_print)) {
4594 bus->pktgen_ptick = 0;
4595 printk(KERN_DEBUG "%s: send attempts %d rcvd %d\n",
4596 __func__, bus->pktgen_sent, bus->pktgen_rcvd);
4599 /* For recv mode, just make sure dongle has started sending */
4600 if (bus->pktgen_mode == DHD_PKTGEN_RECV) {
4601 if (!bus->pktgen_rcvd)
4602 dhdsdio_sdtest_set(bus, true);
4606 /* Otherwise, generate or request the specified number of packets */
4607 for (pktcount = 0; pktcount < bus->pktgen_count; pktcount++) {
4608 /* Stop if total has been reached */
4609 if (bus->pktgen_total
4610 && (bus->pktgen_sent >= bus->pktgen_total)) {
4611 bus->pktgen_count = 0;
4615 /* Allocate an appropriate-sized packet */
4616 len = bus->pktgen_len;
4617 pkt = pkt_buf_get_skb(
4618 (len + SDPCM_HDRLEN + SDPCM_TEST_HDRLEN + DHD_SDALIGN),
4621 DHD_ERROR(("%s: pkt_buf_get_skb failed!\n", __func__));
4624 PKTALIGN(pkt, (len + SDPCM_HDRLEN + SDPCM_TEST_HDRLEN),
4626 data = (u8 *) (pkt->data) + SDPCM_HDRLEN;
4628 /* Write test header cmd and extra based on mode */
4629 switch (bus->pktgen_mode) {
4630 case DHD_PKTGEN_ECHO:
4631 *data++ = SDPCM_TEST_ECHOREQ;
4632 *data++ = (u8) bus->pktgen_sent;
4635 case DHD_PKTGEN_SEND:
4636 *data++ = SDPCM_TEST_DISCARD;
4637 *data++ = (u8) bus->pktgen_sent;
4640 case DHD_PKTGEN_RXBURST:
4641 *data++ = SDPCM_TEST_BURST;
4642 *data++ = (u8) bus->pktgen_count;
4646 DHD_ERROR(("Unrecognized pktgen mode %d\n",
4648 pkt_buf_free_skb(pkt, true);
4649 bus->pktgen_count = 0;
4653 /* Write test header length field */
4654 *data++ = (len >> 0);
4655 *data++ = (len >> 8);
4657 /* Then fill in the remainder -- N/A for burst,
4659 for (fillbyte = 0; fillbyte < len; fillbyte++)
4661 SDPCM_TEST_FILL(fillbyte, (u8) bus->pktgen_sent);
4664 if (DHD_BYTES_ON() && DHD_DATA_ON()) {
4665 data = (u8 *) (pkt->data) + SDPCM_HDRLEN;
4666 prhex("dhdsdio_pktgen: Tx Data", data,
4667 pkt->len - SDPCM_HDRLEN);
4672 if (dhdsdio_txpkt(bus, pkt, SDPCM_TEST_CHANNEL, true)) {
4674 if (bus->pktgen_stop
4675 && bus->pktgen_stop == bus->pktgen_fail)
4676 bus->pktgen_count = 0;
4680 /* Bump length if not fixed, wrap at max */
4681 if (++bus->pktgen_len > bus->pktgen_maxlen)
4682 bus->pktgen_len = (u16) bus->pktgen_minlen;
4684 /* Special case for burst mode: just send one request! */
4685 if (bus->pktgen_mode == DHD_PKTGEN_RXBURST)
4690 static void dhdsdio_sdtest_set(dhd_bus_t *bus, bool start)
4692 struct sk_buff *pkt;
4695 /* Allocate the packet */
4696 pkt = pkt_buf_get_skb(SDPCM_HDRLEN + SDPCM_TEST_HDRLEN + DHD_SDALIGN,
4699 DHD_ERROR(("%s: pkt_buf_get_skb failed!\n", __func__));
4702 PKTALIGN(pkt, (SDPCM_HDRLEN + SDPCM_TEST_HDRLEN), DHD_SDALIGN);
4703 data = (u8 *) (pkt->data) + SDPCM_HDRLEN;
4705 /* Fill in the test header */
4706 *data++ = SDPCM_TEST_SEND;
4708 *data++ = (bus->pktgen_maxlen >> 0);
4709 *data++ = (bus->pktgen_maxlen >> 8);
4712 if (dhdsdio_txpkt(bus, pkt, SDPCM_TEST_CHANNEL, true))
4716 static void dhdsdio_testrcv(dhd_bus_t *bus, struct sk_buff *pkt, uint seq)
4726 /* Check for min length */
4728 if (pktlen < SDPCM_TEST_HDRLEN) {
4729 DHD_ERROR(("dhdsdio_restrcv: toss runt frame, pktlen %d\n",
4731 pkt_buf_free_skb(pkt, false);
4735 /* Extract header fields */
4740 len += *data++ << 8;
4742 /* Check length for relevant commands */
4743 if (cmd == SDPCM_TEST_DISCARD || cmd == SDPCM_TEST_ECHOREQ
4744 || cmd == SDPCM_TEST_ECHORSP) {
4745 if (pktlen != len + SDPCM_TEST_HDRLEN) {
4746 DHD_ERROR(("dhdsdio_testrcv: frame length mismatch, "
4747 "pktlen %d seq %d" " cmd %d extra %d len %d\n",
4748 pktlen, seq, cmd, extra, len));
4749 pkt_buf_free_skb(pkt, false);
4754 /* Process as per command */
4756 case SDPCM_TEST_ECHOREQ:
4757 /* Rx->Tx turnaround ok (even on NDIS w/current
4759 *(u8 *) (pkt->data) = SDPCM_TEST_ECHORSP;
4760 if (dhdsdio_txpkt(bus, pkt, SDPCM_TEST_CHANNEL, true) == 0) {
4764 pkt_buf_free_skb(pkt, false);
4769 case SDPCM_TEST_ECHORSP:
4770 if (bus->ext_loop) {
4771 pkt_buf_free_skb(pkt, false);
4776 for (offset = 0; offset < len; offset++, data++) {
4777 if (*data != SDPCM_TEST_FILL(offset, extra)) {
4778 DHD_ERROR(("dhdsdio_testrcv: echo data mismatch: " "offset %d (len %d) expect 0x%02x rcvd 0x%02x\n",
4780 SDPCM_TEST_FILL(offset, extra), *data));
4784 pkt_buf_free_skb(pkt, false);
4788 case SDPCM_TEST_DISCARD:
4789 pkt_buf_free_skb(pkt, false);
4793 case SDPCM_TEST_BURST:
4794 case SDPCM_TEST_SEND:
4796 DHD_INFO(("dhdsdio_testrcv: unsupported or unknown command, "
4797 "pktlen %d seq %d" " cmd %d extra %d len %d\n",
4798 pktlen, seq, cmd, extra, len));
4799 pkt_buf_free_skb(pkt, false);
4803 /* For recv mode, stop at limie (and tell dongle to stop sending) */
4804 if (bus->pktgen_mode == DHD_PKTGEN_RECV) {
4805 if (bus->pktgen_total
4806 && (bus->pktgen_rcvd >= bus->pktgen_total)) {
4807 bus->pktgen_count = 0;
4808 dhdsdio_sdtest_set(bus, false);
4814 extern bool dhd_bus_watchdog(dhd_pub_t *dhdp)
4818 DHD_TIMER(("%s: Enter\n", __func__));
4822 if (bus->dhd->dongle_reset)
4825 /* Ignore the timer if simulating bus down */
4829 dhd_os_sdlock(bus->dhd);
4831 /* Poll period: check device if appropriate. */
4832 if (bus->poll && (++bus->polltick >= bus->pollrate)) {
4835 /* Reset poll tick */
4838 /* Check device if no interrupts */
4839 if (!bus->intr || (bus->intrcount == bus->lastintrs)) {
4841 if (!bus->dpc_sched) {
4843 devpend = bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_0,
4847 devpend & (INTR_STATUS_FUNC1 |
4851 /* If there is something, make like the ISR and
4857 bcmsdh_intr_disable(bus->sdh);
4859 bus->dpc_sched = true;
4860 dhd_sched_dpc(bus->dhd);
4865 /* Update interrupt tracking */
4866 bus->lastintrs = bus->intrcount;
4869 /* Poll for console output periodically */
4870 if (dhdp->busstate == DHD_BUS_DATA && dhd_console_ms != 0) {
4871 bus->console.count += dhd_watchdog_ms;
4872 if (bus->console.count >= dhd_console_ms) {
4873 bus->console.count -= dhd_console_ms;
4874 /* Make sure backplane clock is on */
4875 dhdsdio_clkctl(bus, CLK_AVAIL, false);
4876 if (dhdsdio_readconsole(bus) < 0)
4877 dhd_console_ms = 0; /* On error,
4881 #endif /* DHD_DEBUG */
4884 /* Generate packets if configured */
4885 if (bus->pktgen_count && (++bus->pktgen_tick >= bus->pktgen_freq)) {
4886 /* Make sure backplane clock is on */
4887 dhdsdio_clkctl(bus, CLK_AVAIL, false);
4888 bus->pktgen_tick = 0;
4889 dhdsdio_pktgen(bus);
4893 /* On idle timeout clear activity flag and/or turn off clock */
4894 if ((bus->idletime > 0) && (bus->clkstate == CLK_AVAIL)) {
4895 if (++bus->idlecount >= bus->idletime) {
4897 if (bus->activity) {
4898 bus->activity = false;
4899 dhd_os_wd_timer(bus->dhd, dhd_watchdog_ms);
4901 dhdsdio_clkctl(bus, CLK_NONE, false);
4906 dhd_os_sdunlock(bus->dhd);
4912 extern int dhd_bus_console_in(dhd_pub_t *dhdp, unsigned char *msg, uint msglen)
4914 dhd_bus_t *bus = dhdp->bus;
4917 struct sk_buff *pkt;
4919 /* Address could be zero if CONSOLE := 0 in dongle Makefile */
4920 if (bus->console_addr == 0)
4923 /* Exclusive bus access */
4924 dhd_os_sdlock(bus->dhd);
4926 /* Don't allow input if dongle is in reset */
4927 if (bus->dhd->dongle_reset) {
4928 dhd_os_sdunlock(bus->dhd);
4929 return -BCME_NOTREADY;
4932 /* Request clock to allow SDIO accesses */
4934 /* No pend allowed since txpkt is called later, ht clk has to be on */
4935 dhdsdio_clkctl(bus, CLK_AVAIL, false);
4937 /* Zero cbuf_index */
4938 addr = bus->console_addr + offsetof(hndrte_cons_t, cbuf_idx);
4939 val = cpu_to_le32(0);
4940 rv = dhdsdio_membytes(bus, true, addr, (u8 *)&val, sizeof(val));
4944 /* Write message into cbuf */
4945 addr = bus->console_addr + offsetof(hndrte_cons_t, cbuf);
4946 rv = dhdsdio_membytes(bus, true, addr, (u8 *)msg, msglen);
4950 /* Write length into vcons_in */
4951 addr = bus->console_addr + offsetof(hndrte_cons_t, vcons_in);
4952 val = cpu_to_le32(msglen);
4953 rv = dhdsdio_membytes(bus, true, addr, (u8 *)&val, sizeof(val));
4957 /* Bump dongle by sending an empty event pkt.
4958 * sdpcm_sendup (RX) checks for virtual console input.
4960 pkt = pkt_buf_get_skb(4 + SDPCM_RESERVE);
4961 if ((pkt != NULL) && bus->clkstate == CLK_AVAIL)
4962 dhdsdio_txpkt(bus, pkt, SDPCM_EVENT_CHANNEL, true);
4965 if ((bus->idletime == DHD_IDLE_IMMEDIATE) && !bus->dpc_sched) {
4966 bus->activity = false;
4967 dhdsdio_clkctl(bus, CLK_NONE, true);
4970 dhd_os_sdunlock(bus->dhd);
4974 #endif /* DHD_DEBUG */
4977 static void dhd_dump_cis(uint fn, u8 *cis)
4979 uint byte, tag, tdata;
4980 DHD_INFO(("Function %d CIS:\n", fn));
4982 for (tdata = byte = 0; byte < SBSDIO_CIS_SIZE_LIMIT; byte++) {
4983 if ((byte % 16) == 0)
4985 DHD_INFO(("%02x ", cis[byte]));
4986 if ((byte % 16) == 15)
4994 else if ((byte + 1) < SBSDIO_CIS_SIZE_LIMIT)
4995 tdata = cis[byte + 1] + 1;
5000 if ((byte % 16) != 15)
5003 #endif /* DHD_DEBUG */
5005 static bool dhdsdio_chipmatch(u16 chipid)
5007 if (chipid == BCM4325_CHIP_ID)
5009 if (chipid == BCM4329_CHIP_ID)
5011 if (chipid == BCM4319_CHIP_ID)
5016 static void *dhdsdio_probe(u16 venid, u16 devid, u16 bus_no,
5017 u16 slot, u16 func, uint bustype, void *regsva,
5023 /* Init global variables at run-time, not as part of the declaration.
5024 * This is required to support init/de-init of the driver.
5026 * of globals as part of the declaration results in non-deterministic
5027 * behavior since the value of the globals may be different on the
5028 * first time that the driver is initialized vs subsequent
5031 dhd_txbound = DHD_TXBOUND;
5032 dhd_rxbound = DHD_RXBOUND;
5033 dhd_alignctl = true;
5035 dhd_readahead = true;
5037 dhd_dongle_memsize = 0;
5038 dhd_txminmax = DHD_TXMINMAX;
5044 DHD_TRACE(("%s: Enter\n", __func__));
5045 DHD_INFO(("%s: venid 0x%04x devid 0x%04x\n", __func__, venid, devid));
5047 /* We make assumptions about address window mappings */
5048 ASSERT((unsigned long)regsva == SI_ENUM_BASE);
5050 /* BCMSDH passes venid and devid based on CIS parsing -- but
5052 * means early parse could fail, so here we should get either an ID
5053 * we recognize OR (-1) indicating we must request power first.
5055 /* Check the Vendor ID */
5058 case PCI_VENDOR_ID_BROADCOM:
5061 DHD_ERROR(("%s: unknown vendor: 0x%04x\n", __func__, venid));
5065 /* Check the Device ID and make sure it's one that we support */
5067 case BCM4325_D11DUAL_ID: /* 4325 802.11a/g id */
5068 case BCM4325_D11G_ID: /* 4325 802.11g 2.4Ghz band id */
5069 case BCM4325_D11A_ID: /* 4325 802.11a 5Ghz band id */
5070 DHD_INFO(("%s: found 4325 Dongle\n", __func__));
5072 case BCM4329_D11NDUAL_ID: /* 4329 802.11n dualband device */
5073 case BCM4329_D11N2G_ID: /* 4329 802.11n 2.4G device */
5074 case BCM4329_D11N5G_ID: /* 4329 802.11n 5G device */
5076 DHD_INFO(("%s: found 4329 Dongle\n", __func__));
5078 case BCM4319_D11N_ID: /* 4319 802.11n id */
5079 case BCM4319_D11N2G_ID: /* 4319 802.11n2g id */
5080 case BCM4319_D11N5G_ID: /* 4319 802.11n5g id */
5081 DHD_INFO(("%s: found 4319 Dongle\n", __func__));
5084 DHD_INFO(("%s: allow device id 0, will check chip internals\n",
5089 DHD_ERROR(("%s: skipping 0x%04x/0x%04x, not a dongle\n",
5090 __func__, venid, devid));
5094 /* Allocate private bus interface state */
5095 bus = kzalloc(sizeof(dhd_bus_t), GFP_ATOMIC);
5097 DHD_ERROR(("%s: kmalloc of dhd_bus_t failed\n", __func__));
5101 bus->cl_devid = (u16) devid;
5103 bus->tx_seq = SDPCM_SEQUENCE_WRAP - 1;
5104 bus->usebufpool = false; /* Use bufpool if allocated,
5105 else use locally malloced rxbuf */
5107 /* attempt to attach to the dongle */
5108 if (!(dhdsdio_probe_attach(bus, sdh, regsva, devid))) {
5109 DHD_ERROR(("%s: dhdsdio_probe_attach failed\n", __func__));
5113 /* Attach to the dhd/OS/network interface */
5114 bus->dhd = dhd_attach(bus, SDPCM_RESERVE);
5116 DHD_ERROR(("%s: dhd_attach failed\n", __func__));
5120 /* Allocate buffers */
5121 if (!(dhdsdio_probe_malloc(bus, sdh))) {
5122 DHD_ERROR(("%s: dhdsdio_probe_malloc failed\n", __func__));
5126 if (!(dhdsdio_probe_init(bus, sdh))) {
5127 DHD_ERROR(("%s: dhdsdio_probe_init failed\n", __func__));
5131 /* Register interrupt callback, but mask it (not operational yet). */
5132 DHD_INTR(("%s: disable SDIO interrupts (not interested yet)\n",
5134 bcmsdh_intr_disable(sdh);
5135 ret = bcmsdh_intr_reg(sdh, dhdsdio_isr, bus);
5137 DHD_ERROR(("%s: FAILED: bcmsdh_intr_reg returned %d\n",
5141 DHD_INTR(("%s: registered SDIO interrupt function ok\n", __func__));
5143 DHD_INFO(("%s: completed!!\n", __func__));
5145 /* if firmware path present try to download and bring up bus */
5146 ret = dhd_bus_start(bus->dhd);
5148 if (ret == -ENOLINK) {
5149 DHD_ERROR(("%s: dongle is not responding\n", __func__));
5153 /* Ok, have the per-port tell the stack we're open for business */
5154 if (dhd_net_attach(bus->dhd, 0) != 0) {
5155 DHD_ERROR(("%s: Net attach failed!!\n", __func__));
5162 dhdsdio_release(bus);
5167 dhdsdio_probe_attach(struct dhd_bus *bus, void *sdh, void *regsva, u16 devid)
5172 bus->alp_only = true;
5174 /* Return the window to backplane enumeration space for core access */
5175 if (dhdsdio_set_siaddr_window(bus, SI_ENUM_BASE))
5176 DHD_ERROR(("%s: FAILED to return to SI_ENUM_BASE\n", __func__));
5179 printk(KERN_DEBUG "F1 signature read @0x18000000=0x%4x\n",
5180 bcmsdh_reg_read(bus->sdh, SI_ENUM_BASE, 4));
5182 #endif /* DHD_DEBUG */
5185 * Force PLL off until dhdsdio_chip_attach()
5186 * programs PLL control regs
5189 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
5190 DHD_INIT_CLKCTL1, &err);
5193 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
5196 if (err || ((clkctl & ~SBSDIO_AVBITS) != DHD_INIT_CLKCTL1)) {
5197 DHD_ERROR(("dhdsdio_probe: ChipClkCSR access: err %d wrote "
5198 "0x%02x read 0x%02x\n",
5199 err, DHD_INIT_CLKCTL1, clkctl));
5203 if (DHD_INFO_ON()) {
5205 u8 *cis[SDIOD_MAX_IOFUNCS];
5208 numfn = bcmsdh_query_iofnum(sdh);
5209 ASSERT(numfn <= SDIOD_MAX_IOFUNCS);
5211 /* Make sure ALP is available before trying to read CIS */
5212 SPINWAIT(((clkctl = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
5213 SBSDIO_FUNC1_CHIPCLKCSR,
5215 !SBSDIO_ALPAV(clkctl)), PMU_MAX_TRANSITION_DLY);
5217 /* Now request ALP be put on the bus */
5218 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
5219 DHD_INIT_CLKCTL2, &err);
5222 for (fn = 0; fn <= numfn; fn++) {
5223 cis[fn] = kzalloc(SBSDIO_CIS_SIZE_LIMIT, GFP_ATOMIC);
5225 DHD_INFO(("dhdsdio_probe: fn %d cis malloc "
5230 err = bcmsdh_cis_read(sdh, fn, cis[fn],
5231 SBSDIO_CIS_SIZE_LIMIT);
5233 DHD_INFO(("dhdsdio_probe: fn %d cis read "
5234 "err %d\n", fn, err));
5238 dhd_dump_cis(fn, cis[fn]);
5247 DHD_ERROR(("dhdsdio_probe: error read/parsing CIS\n"));
5251 #endif /* DHD_DEBUG */
5253 if (dhdsdio_chip_attach(bus, regsva)) {
5254 DHD_ERROR(("%s: dhdsdio_chip_attach failed!\n", __func__));
5258 bcmsdh_chipinfo(sdh, bus->ci->chip, bus->ci->chiprev);
5260 if (!dhdsdio_chipmatch((u16) bus->ci->chip)) {
5261 DHD_ERROR(("%s: unsupported chip: 0x%04x\n",
5262 __func__, bus->ci->chip));
5266 dhdsdio_sdiod_drive_strength_init(bus, dhd_sdiod_drive_strength);
5268 /* Get info on the ARM and SOCRAM cores... */
5269 if (!DHD_NOPMU(bus)) {
5270 bus->armrev = SBCOREREV(bcmsdh_reg_read(bus->sdh,
5271 CORE_SB(bus->ci->armcorebase, sbidhigh), 4));
5272 bus->orig_ramsize = bus->ci->ramsize;
5273 if (!(bus->orig_ramsize)) {
5274 DHD_ERROR(("%s: failed to find SOCRAM memory!\n",
5278 bus->ramsize = bus->orig_ramsize;
5279 if (dhd_dongle_memsize)
5280 dhd_dongle_setmemsize(bus, dhd_dongle_memsize);
5282 DHD_ERROR(("DHD: dongle ram size is set to %d(orig %d)\n",
5283 bus->ramsize, bus->orig_ramsize));
5286 bus->regs = (void *)bus->ci->buscorebase;
5288 /* Set core control so an SDIO reset does a backplane reset */
5289 OR_REG(&bus->regs->corecontrol, CC_BPRESEN);
5291 pktq_init(&bus->txq, (PRIOMASK + 1), TXQLEN);
5293 /* Locate an appropriately-aligned portion of hdrbuf */
5294 bus->rxhdr = (u8 *) roundup((unsigned long)&bus->hdrbuf[0], DHD_SDALIGN);
5296 /* Set the poll and/or interrupt flags */
5297 bus->intr = (bool) dhd_intr;
5298 bus->poll = (bool) dhd_poll;
5308 static bool dhdsdio_probe_malloc(dhd_bus_t *bus, void *sdh)
5310 DHD_TRACE(("%s: Enter\n", __func__));
5312 if (bus->dhd->maxctl) {
5314 roundup((bus->dhd->maxctl + SDPCM_HDRLEN),
5315 ALIGNMENT) + DHD_SDALIGN;
5316 bus->rxbuf = kmalloc(bus->rxblen, GFP_ATOMIC);
5317 if (!(bus->rxbuf)) {
5318 DHD_ERROR(("%s: kmalloc of %d-byte rxbuf failed\n",
5319 __func__, bus->rxblen));
5324 /* Allocate buffer to receive glomed packet */
5325 bus->databuf = kmalloc(MAX_DATA_BUF, GFP_ATOMIC);
5326 if (!(bus->databuf)) {
5327 DHD_ERROR(("%s: kmalloc of %d-byte databuf failed\n",
5328 __func__, MAX_DATA_BUF));
5329 /* release rxbuf which was already located as above */
5335 /* Align the buffer */
5336 if ((unsigned long)bus->databuf % DHD_SDALIGN)
5338 bus->databuf + (DHD_SDALIGN -
5339 ((unsigned long)bus->databuf % DHD_SDALIGN));
5341 bus->dataptr = bus->databuf;
5349 static bool dhdsdio_probe_init(dhd_bus_t *bus, void *sdh)
5353 DHD_TRACE(("%s: Enter\n", __func__));
5356 dhdsdio_pktgen_init(bus);
5359 /* Disable F2 to clear any intermediate frame state on the dongle */
5360 bcmsdh_cfg_write(sdh, SDIO_FUNC_0, SDIOD_CCCR_IOEN, SDIO_FUNC_ENABLE_1,
5363 bus->dhd->busstate = DHD_BUS_DOWN;
5364 bus->sleeping = false;
5365 bus->rxflow = false;
5366 bus->prev_rxlim_hit = 0;
5368 /* Done with backplane-dependent accesses, can drop clock... */
5369 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR, 0, NULL);
5371 /* ...and initialize clock/power states */
5372 bus->clkstate = CLK_SDONLY;
5373 bus->idletime = (s32) dhd_idletime;
5374 bus->idleclock = DHD_IDLE_ACTIVE;
5376 /* Query the SD clock speed */
5377 if (bcmsdh_iovar_op(sdh, "sd_divisor", NULL, 0,
5378 &bus->sd_divisor, sizeof(s32),
5380 DHD_ERROR(("%s: fail on %s get\n", __func__, "sd_divisor"));
5381 bus->sd_divisor = -1;
5383 DHD_INFO(("%s: Initial value for %s is %d\n",
5384 __func__, "sd_divisor", bus->sd_divisor));
5387 /* Query the SD bus mode */
5388 if (bcmsdh_iovar_op(sdh, "sd_mode", NULL, 0,
5389 &bus->sd_mode, sizeof(s32), false) != 0) {
5390 DHD_ERROR(("%s: fail on %s get\n", __func__, "sd_mode"));
5393 DHD_INFO(("%s: Initial value for %s is %d\n",
5394 __func__, "sd_mode", bus->sd_mode));
5397 /* Query the F2 block size, set roundup accordingly */
5399 if (bcmsdh_iovar_op(sdh, "sd_blocksize", &fnum, sizeof(s32),
5400 &bus->blocksize, sizeof(s32), false) != 0) {
5402 DHD_ERROR(("%s: fail on %s get\n", __func__, "sd_blocksize"));
5404 DHD_INFO(("%s: Initial value for %s is %d\n",
5405 __func__, "sd_blocksize", bus->blocksize));
5407 bus->roundup = min(max_roundup, bus->blocksize);
5409 /* Query if bus module supports packet chaining,
5410 default to use if supported */
5411 if (bcmsdh_iovar_op(sdh, "sd_rxchain", NULL, 0,
5412 &bus->sd_rxchain, sizeof(s32),
5414 bus->sd_rxchain = false;
5416 DHD_INFO(("%s: bus module (through bcmsdh API) %s chaining\n",
5418 (bus->sd_rxchain ? "supports" : "does not support")));
5420 bus->use_rxchain = (bool) bus->sd_rxchain;
5426 dhd_bus_download_firmware(struct dhd_bus *bus, char *fw_path, char *nv_path)
5429 bus->fw_path = fw_path;
5430 bus->nv_path = nv_path;
5432 ret = dhdsdio_download_firmware(bus, bus->sdh);
5438 dhdsdio_download_firmware(struct dhd_bus *bus, void *sdh)
5442 /* Download the firmware */
5443 dhdsdio_clkctl(bus, CLK_AVAIL, false);
5445 ret = _dhdsdio_download_firmware(bus) == 0;
5447 dhdsdio_clkctl(bus, CLK_SDONLY, false);
5452 /* Detach and free everything */
5453 static void dhdsdio_release(dhd_bus_t *bus)
5455 DHD_TRACE(("%s: Enter\n", __func__));
5458 /* De-register interrupt handler */
5459 bcmsdh_intr_disable(bus->sdh);
5460 bcmsdh_intr_dereg(bus->sdh);
5463 dhd_detach(bus->dhd);
5464 dhdsdio_release_dongle(bus);
5468 dhdsdio_release_malloc(bus);
5473 DHD_TRACE(("%s: Disconnected\n", __func__));
5476 static void dhdsdio_release_malloc(dhd_bus_t *bus)
5478 DHD_TRACE(("%s: Enter\n", __func__));
5480 if (bus->dhd && bus->dhd->dongle_reset)
5485 bus->rxctl = bus->rxbuf = NULL;
5489 kfree(bus->databuf);
5490 bus->databuf = NULL;
5493 static void dhdsdio_release_dongle(dhd_bus_t *bus)
5495 DHD_TRACE(("%s: Enter\n", __func__));
5497 if (bus->dhd && bus->dhd->dongle_reset)
5501 dhdsdio_clkctl(bus, CLK_AVAIL, false);
5502 dhdsdio_clkctl(bus, CLK_NONE, false);
5503 dhdsdio_chip_detach(bus);
5504 if (bus->vars && bus->varsz)
5509 DHD_TRACE(("%s: Disconnected\n", __func__));
5512 static void dhdsdio_disconnect(void *ptr)
5514 dhd_bus_t *bus = (dhd_bus_t *)ptr;
5516 DHD_TRACE(("%s: Enter\n", __func__));
5520 dhdsdio_release(bus);
5523 DHD_TRACE(("%s: Disconnected\n", __func__));
5526 /* Register/Unregister functions are called by the main DHD entry
5527 * point (e.g. module insertion) to link with the bus driver, in
5528 * order to look for or await the device.
5531 static bcmsdh_driver_t dhd_sdio = {
5536 int dhd_bus_register(void)
5538 DHD_TRACE(("%s: Enter\n", __func__));
5540 return bcmsdh_register(&dhd_sdio);
5543 void dhd_bus_unregister(void)
5545 DHD_TRACE(("%s: Enter\n", __func__));
5547 bcmsdh_unregister();
5550 #ifdef BCMEMBEDIMAGE
5551 static int dhdsdio_download_code_array(struct dhd_bus *bus)
5556 DHD_INFO(("%s: download embedded firmware...\n", __func__));
5558 /* Download image */
5559 while ((offset + MEMBLOCK) < sizeof(dlarray)) {
5561 dhdsdio_membytes(bus, true, offset, dlarray + offset,
5564 DHD_ERROR(("%s: error %d on writing %d membytes at "
5566 __func__, bcmerror, MEMBLOCK, offset));
5573 if (offset < sizeof(dlarray)) {
5574 bcmerror = dhdsdio_membytes(bus, true, offset,
5576 sizeof(dlarray) - offset);
5578 DHD_ERROR(("%s: error %d on writing %d membytes at "
5579 "0x%08x\n", __func__, bcmerror,
5580 sizeof(dlarray) - offset, offset));
5585 /* Upload and compare the downloaded code */
5587 unsigned char *ularray;
5589 ularray = kmalloc(bus->ramsize, GFP_ATOMIC);
5594 /* Upload image to verify downloaded contents. */
5596 memset(ularray, 0xaa, bus->ramsize);
5597 while ((offset + MEMBLOCK) < sizeof(dlarray)) {
5599 dhdsdio_membytes(bus, false, offset,
5600 ularray + offset, MEMBLOCK);
5602 DHD_ERROR(("%s: error %d on reading %d membytes"
5604 __func__, bcmerror, MEMBLOCK, offset));
5611 if (offset < sizeof(dlarray)) {
5612 bcmerror = dhdsdio_membytes(bus, false, offset,
5614 sizeof(dlarray) - offset);
5616 DHD_ERROR(("%s: error %d on reading %d membytes at 0x%08x\n",
5618 sizeof(dlarray) - offset, offset));
5623 if (memcmp(dlarray, ularray, sizeof(dlarray))) {
5624 DHD_ERROR(("%s: Downloaded image is corrupted.\n",
5629 DHD_ERROR(("%s: Download/Upload/Compare succeeded.\n",
5634 #endif /* DHD_DEBUG */
5639 #endif /* BCMEMBEDIMAGE */
5641 static int dhdsdio_download_code_file(struct dhd_bus *bus, char *fw_path)
5647 u8 *memblock = NULL, *memptr;
5649 DHD_INFO(("%s: download firmware %s\n", __func__, fw_path));
5651 image = dhd_os_open_image(fw_path);
5655 memptr = memblock = kmalloc(MEMBLOCK + DHD_SDALIGN, GFP_ATOMIC);
5656 if (memblock == NULL) {
5657 DHD_ERROR(("%s: Failed to allocate memory %d bytes\n",
5658 __func__, MEMBLOCK));
5661 if ((u32)(unsigned long)memblock % DHD_SDALIGN)
5663 (DHD_SDALIGN - ((u32)(unsigned long)memblock % DHD_SDALIGN));
5665 /* Download image */
5667 dhd_os_get_image_block((char *)memptr, MEMBLOCK, image))) {
5668 bcmerror = dhdsdio_membytes(bus, true, offset, memptr, len);
5670 DHD_ERROR(("%s: error %d on writing %d membytes at "
5671 "0x%08x\n", __func__, bcmerror, MEMBLOCK, offset));
5682 dhd_os_close_image(image);
5688 * ProcessVars:Takes a buffer of "<var>=<value>\n" lines read from a file
5689 * and ending in a NUL.
5690 * Removes carriage returns, empty lines, comment lines, and converts
5692 * Shortens buffer as needed and pads with NULs. End of buffer is marked
5696 static uint process_nvram_vars(char *varbuf, uint len)
5705 findNewline = false;
5708 for (n = 0; n < len; n++) {
5711 if (varbuf[n] == '\r')
5713 if (findNewline && varbuf[n] != '\n')
5715 findNewline = false;
5716 if (varbuf[n] == '#') {
5720 if (varbuf[n] == '\n') {
5730 buf_len = dp - varbuf;
5732 while (dp < varbuf + n)
5739 EXAMPLE: nvram_array
5742 Use carriage return at the end of each assignment,
5743 and an empty string with
5744 carriage return at the end of array.
5747 unsigned char nvram_array[] = {"name1=value1\n",
5748 "name2=value2\n", "\n"};
5749 Hex values start with 0x, and mac addr format: xx:xx:xx:xx:xx:xx.
5751 Search "EXAMPLE: nvram_array" to see how the array is activated.
5754 void dhd_bus_set_nvram_params(struct dhd_bus *bus, const char *nvram_params)
5756 bus->nvram_params = nvram_params;
5759 static int dhdsdio_download_nvram(struct dhd_bus *bus)
5764 char *memblock = NULL;
5767 bool nvram_file_exists;
5769 nv_path = bus->nv_path;
5771 nvram_file_exists = ((nv_path != NULL) && (nv_path[0] != '\0'));
5772 if (!nvram_file_exists && (bus->nvram_params == NULL))
5775 if (nvram_file_exists) {
5776 image = dhd_os_open_image(nv_path);
5781 memblock = kmalloc(MEMBLOCK, GFP_ATOMIC);
5782 if (memblock == NULL) {
5783 DHD_ERROR(("%s: Failed to allocate memory %d bytes\n",
5784 __func__, MEMBLOCK));
5788 /* Download variables */
5789 if (nvram_file_exists) {
5790 len = dhd_os_get_image_block(memblock, MEMBLOCK, image);
5792 len = strlen(bus->nvram_params);
5793 ASSERT(len <= MEMBLOCK);
5796 memcpy(memblock, bus->nvram_params, len);
5799 if (len > 0 && len < MEMBLOCK) {
5800 bufp = (char *)memblock;
5802 len = process_nvram_vars(bufp, len);
5806 bcmerror = dhdsdio_downloadvars(bus, memblock, len + 1);
5808 DHD_ERROR(("%s: error downloading vars: %d\n",
5809 __func__, bcmerror));
5812 DHD_ERROR(("%s: error reading nvram file: %d\n",
5814 bcmerror = -BCME_SDIO_ERROR;
5821 dhd_os_close_image(image);
5826 static int _dhdsdio_download_firmware(struct dhd_bus *bus)
5830 bool embed = false; /* download embedded firmware */
5831 bool dlok = false; /* download firmware succeeded */
5833 /* Out immediately if no image to download */
5834 if ((bus->fw_path == NULL) || (bus->fw_path[0] == '\0')) {
5835 #ifdef BCMEMBEDIMAGE
5842 /* Keep arm in reset */
5843 if (dhdsdio_download_state(bus, true)) {
5844 DHD_ERROR(("%s: error placing ARM core in reset\n", __func__));
5848 /* External image takes precedence if specified */
5849 if ((bus->fw_path != NULL) && (bus->fw_path[0] != '\0')) {
5850 if (dhdsdio_download_code_file(bus, bus->fw_path)) {
5851 DHD_ERROR(("%s: dongle image file download failed\n",
5853 #ifdef BCMEMBEDIMAGE
5863 #ifdef BCMEMBEDIMAGE
5865 if (dhdsdio_download_code_array(bus)) {
5866 DHD_ERROR(("%s: dongle image array download failed\n",
5875 DHD_ERROR(("%s: dongle image download failed\n", __func__));
5879 /* EXAMPLE: nvram_array */
5880 /* If a valid nvram_arry is specified as above, it can be passed
5882 /* dhd_bus_set_nvram_params(bus, (char *)&nvram_array); */
5884 /* External nvram takes precedence if specified */
5885 if (dhdsdio_download_nvram(bus)) {
5886 DHD_ERROR(("%s: dongle nvram file download failed\n",
5890 /* Take arm out of reset */
5891 if (dhdsdio_download_state(bus, false)) {
5892 DHD_ERROR(("%s: error getting out of ARM core reset\n",
5904 dhd_bcmsdh_recv_buf(dhd_bus_t *bus, u32 addr, uint fn, uint flags,
5905 u8 *buf, uint nbytes, struct sk_buff *pkt,
5906 bcmsdh_cmplt_fn_t complete, void *handle)
5910 /* 4329: GSPI check */
5912 bcmsdh_recv_buf(bus->sdh, addr, fn, flags, buf, nbytes, pkt,
5918 dhd_bcmsdh_send_buf(dhd_bus_t *bus, u32 addr, uint fn, uint flags,
5919 u8 *buf, uint nbytes, struct sk_buff *pkt,
5920 bcmsdh_cmplt_fn_t complete, void *handle)
5922 return bcmsdh_send_buf
5923 (bus->sdh, addr, fn, flags, buf, nbytes, pkt, complete,
5927 uint dhd_bus_chip(struct dhd_bus *bus)
5929 ASSERT(bus->ci != NULL);
5930 return bus->ci->chip;
5933 void *dhd_bus_pub(struct dhd_bus *bus)
5938 void *dhd_bus_txq(struct dhd_bus *bus)
5943 uint dhd_bus_hdrlen(struct dhd_bus *bus)
5945 return SDPCM_HDRLEN;
5948 int dhd_bus_devreset(dhd_pub_t *dhdp, u8 flag)
5956 if (!bus->dhd->dongle_reset) {
5957 /* Expect app to have torn down any
5958 connection before calling */
5959 /* Stop the bus, disable F2 */
5960 dhd_bus_stop(bus, false);
5962 /* Clean tx/rx buffer pointers,
5963 detach from the dongle */
5964 dhdsdio_release_dongle(bus);
5966 bus->dhd->dongle_reset = true;
5967 bus->dhd->up = false;
5969 DHD_TRACE(("%s: WLAN OFF DONE\n", __func__));
5970 /* App can now remove power from device */
5972 bcmerror = -BCME_SDIO_ERROR;
5974 /* App must have restored power to device before calling */
5976 DHD_TRACE(("\n\n%s: == WLAN ON ==\n", __func__));
5978 if (bus->dhd->dongle_reset) {
5980 /* Reset SD client */
5981 bcmsdh_reset(bus->sdh);
5983 /* Attempt to re-attach & download */
5984 if (dhdsdio_probe_attach(bus, bus->sdh,
5985 (u32 *) SI_ENUM_BASE,
5987 /* Attempt to download binary to the dongle */
5988 if (dhdsdio_probe_init
5990 && dhdsdio_download_firmware(bus,
5993 /* Re-init bus, enable F2 transfer */
5994 dhd_bus_init((dhd_pub_t *) bus->dhd,
5997 #if defined(OOB_INTR_ONLY)
5998 dhd_enable_oob_intr(bus, true);
5999 #endif /* defined(OOB_INTR_ONLY) */
6001 bus->dhd->dongle_reset = false;
6002 bus->dhd->up = true;
6004 DHD_TRACE(("%s: WLAN ON DONE\n",
6007 bcmerror = -BCME_SDIO_ERROR;
6009 bcmerror = -BCME_SDIO_ERROR;
6011 bcmerror = -BCME_NOTDOWN;
6012 DHD_ERROR(("%s: Set DEVRESET=false invoked when device "
6013 "is on\n", __func__));
6014 bcmerror = -BCME_SDIO_ERROR;
6021 dhdsdio_chip_recognition(bcmsdh_info_t *sdh, struct chip_info *ci, void *regs)
6027 * Chipid is assume to be at offset 0 from regs arg
6028 * For different chiptypes or old sdio hosts w/o chipcommon,
6029 * other ways of recognition should be added here.
6031 ci->cccorebase = (u32)regs;
6032 regdata = bcmsdh_reg_read(sdh, CORE_CC_REG(ci->cccorebase, chipid), 4);
6033 ci->chip = regdata & CID_ID_MASK;
6034 ci->chiprev = (regdata & CID_REV_MASK) >> CID_REV_SHIFT;
6036 DHD_INFO(("%s: chipid=0x%x chiprev=%d\n",
6037 __func__, ci->chip, ci->chiprev));
6039 /* Address of cores for new chips should be added here */
6041 case BCM4329_CHIP_ID:
6042 ci->buscorebase = BCM4329_CORE_BUS_BASE;
6043 ci->ramcorebase = BCM4329_CORE_SOCRAM_BASE;
6044 ci->armcorebase = BCM4329_CORE_ARM_BASE;
6045 ci->ramsize = BCM4329_RAMSIZE;
6048 DHD_ERROR(("%s: chipid 0x%x is not supported\n",
6049 __func__, ci->chip));
6053 regdata = bcmsdh_reg_read(sdh,
6054 CORE_SB(ci->cccorebase, sbidhigh), 4);
6055 ci->ccrev = SBCOREREV(regdata);
6057 regdata = bcmsdh_reg_read(sdh,
6058 CORE_CC_REG(ci->cccorebase, pmucapabilities), 4);
6059 ci->pmurev = regdata & PCAP_REV_MASK;
6061 regdata = bcmsdh_reg_read(sdh, CORE_SB(ci->buscorebase, sbidhigh), 4);
6062 ci->buscorerev = SBCOREREV(regdata);
6063 ci->buscoretype = (regdata & SBIDH_CC_MASK) >> SBIDH_CC_SHIFT;
6065 DHD_INFO(("%s: ccrev=%d, pmurev=%d, buscore rev/type=%d/0x%x\n",
6066 __func__, ci->ccrev, ci->pmurev,
6067 ci->buscorerev, ci->buscoretype));
6069 /* get chipcommon capabilites */
6070 ci->cccaps = bcmsdh_reg_read(sdh,
6071 CORE_CC_REG(ci->cccorebase, capabilities), 4);
6077 dhdsdio_chip_disablecore(bcmsdh_info_t *sdh, u32 corebase)
6081 regdata = bcmsdh_reg_read(sdh,
6082 CORE_SB(corebase, sbtmstatelow), 4);
6083 if (regdata & SBTML_RESET)
6086 regdata = bcmsdh_reg_read(sdh,
6087 CORE_SB(corebase, sbtmstatelow), 4);
6088 if ((regdata & (SICF_CLOCK_EN << SBTML_SICF_SHIFT)) != 0) {
6090 * set target reject and spin until busy is clear
6091 * (preserve core-specific bits)
6093 regdata = bcmsdh_reg_read(sdh,
6094 CORE_SB(corebase, sbtmstatelow), 4);
6095 bcmsdh_reg_write(sdh, CORE_SB(corebase, sbtmstatelow), 4,
6096 regdata | SBTML_REJ);
6098 regdata = bcmsdh_reg_read(sdh,
6099 CORE_SB(corebase, sbtmstatelow), 4);
6101 SPINWAIT((bcmsdh_reg_read(sdh,
6102 CORE_SB(corebase, sbtmstatehigh), 4) &
6103 SBTMH_BUSY), 100000);
6105 regdata = bcmsdh_reg_read(sdh,
6106 CORE_SB(corebase, sbtmstatehigh), 4);
6107 if (regdata & SBTMH_BUSY)
6108 DHD_ERROR(("%s: ARM core still busy\n", __func__));
6110 regdata = bcmsdh_reg_read(sdh,
6111 CORE_SB(corebase, sbidlow), 4);
6112 if (regdata & SBIDL_INIT) {
6113 regdata = bcmsdh_reg_read(sdh,
6114 CORE_SB(corebase, sbimstate), 4) |
6116 bcmsdh_reg_write(sdh,
6117 CORE_SB(corebase, sbimstate), 4,
6119 regdata = bcmsdh_reg_read(sdh,
6120 CORE_SB(corebase, sbimstate), 4);
6122 SPINWAIT((bcmsdh_reg_read(sdh,
6123 CORE_SB(corebase, sbimstate), 4) &
6127 /* set reset and reject while enabling the clocks */
6128 bcmsdh_reg_write(sdh,
6129 CORE_SB(corebase, sbtmstatelow), 4,
6130 (((SICF_FGC | SICF_CLOCK_EN) << SBTML_SICF_SHIFT) |
6131 SBTML_REJ | SBTML_RESET));
6132 regdata = bcmsdh_reg_read(sdh,
6133 CORE_SB(corebase, sbtmstatelow), 4);
6136 /* clear the initiator reject bit */
6137 regdata = bcmsdh_reg_read(sdh,
6138 CORE_SB(corebase, sbidlow), 4);
6139 if (regdata & SBIDL_INIT) {
6140 regdata = bcmsdh_reg_read(sdh,
6141 CORE_SB(corebase, sbimstate), 4) &
6143 bcmsdh_reg_write(sdh,
6144 CORE_SB(corebase, sbimstate), 4,
6149 /* leave reset and reject asserted */
6150 bcmsdh_reg_write(sdh, CORE_SB(corebase, sbtmstatelow), 4,
6151 (SBTML_REJ | SBTML_RESET));
6156 dhdsdio_chip_attach(struct dhd_bus *bus, void *regs)
6158 struct chip_info *ci;
6162 DHD_TRACE(("%s: Enter\n", __func__));
6164 /* alloc chip_info_t */
6165 ci = kmalloc(sizeof(struct chip_info), GFP_ATOMIC);
6167 DHD_ERROR(("%s: malloc failed!\n", __func__));
6171 memset((unsigned char *)ci, 0, sizeof(struct chip_info));
6173 /* bus/core/clk setup for register access */
6174 /* Try forcing SDIO core to do ALPAvail request only */
6175 clkset = SBSDIO_FORCE_HW_CLKREQ_OFF | SBSDIO_ALP_AVAIL_REQ;
6176 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
6179 DHD_ERROR(("%s: error writing for HT off\n", __func__));
6183 /* If register supported, wait for ALPAvail and then force ALP */
6184 /* This may take up to 15 milliseconds */
6185 clkval = bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1,
6186 SBSDIO_FUNC1_CHIPCLKCSR, NULL);
6187 if ((clkval & ~SBSDIO_AVBITS) == clkset) {
6189 bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1,
6190 SBSDIO_FUNC1_CHIPCLKCSR,
6192 !SBSDIO_ALPAV(clkval)),
6193 PMU_MAX_TRANSITION_DLY);
6194 if (!SBSDIO_ALPAV(clkval)) {
6195 DHD_ERROR(("%s: timeout on ALPAV wait, clkval 0x%02x\n",
6200 clkset = SBSDIO_FORCE_HW_CLKREQ_OFF |
6202 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1,
6203 SBSDIO_FUNC1_CHIPCLKCSR,
6207 DHD_ERROR(("%s: ChipClkCSR access: wrote 0x%02x read 0x%02x\n",
6208 __func__, clkset, clkval));
6213 /* Also, disable the extra SDIO pull-ups */
6214 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_SDIOPULLUP, 0,
6217 err = dhdsdio_chip_recognition(bus->sdh, ci, regs);
6222 * Make sure any on-chip ARM is off (in case strapping is wrong),
6223 * or downloaded code was already running.
6225 dhdsdio_chip_disablecore(bus->sdh, ci->armcorebase);
6227 bcmsdh_reg_write(bus->sdh,
6228 CORE_CC_REG(ci->cccorebase, gpiopullup), 4, 0);
6229 bcmsdh_reg_write(bus->sdh,
6230 CORE_CC_REG(ci->cccorebase, gpiopulldown), 4, 0);
6232 /* Disable F2 to clear any intermediate frame state on the dongle */
6233 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_0, SDIOD_CCCR_IOEN,
6234 SDIO_FUNC_ENABLE_1, NULL);
6236 /* WAR: cmd52 backplane read so core HW will drop ALPReq */
6237 clkval = bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1,
6240 /* Done with backplane-dependent accesses, can drop clock... */
6241 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR, 0,
6253 dhdsdio_chip_resetcore(bcmsdh_info_t *sdh, u32 corebase)
6258 * Must do the disable sequence first to work for
6259 * arbitrary current core state.
6261 dhdsdio_chip_disablecore(sdh, corebase);
6264 * Now do the initialization sequence.
6265 * set reset while enabling the clock and
6266 * forcing them on throughout the core
6268 bcmsdh_reg_write(sdh, CORE_SB(corebase, sbtmstatelow), 4,
6269 ((SICF_FGC | SICF_CLOCK_EN) << SBTML_SICF_SHIFT) |
6273 regdata = bcmsdh_reg_read(sdh, CORE_SB(corebase, sbtmstatehigh), 4);
6274 if (regdata & SBTMH_SERR)
6275 bcmsdh_reg_write(sdh, CORE_SB(corebase, sbtmstatehigh), 4, 0);
6277 regdata = bcmsdh_reg_read(sdh, CORE_SB(corebase, sbimstate), 4);
6278 if (regdata & (SBIM_IBE | SBIM_TO))
6279 bcmsdh_reg_write(sdh, CORE_SB(corebase, sbimstate), 4,
6280 regdata & ~(SBIM_IBE | SBIM_TO));
6282 /* clear reset and allow it to propagate throughout the core */
6283 bcmsdh_reg_write(sdh, CORE_SB(corebase, sbtmstatelow), 4,
6284 (SICF_FGC << SBTML_SICF_SHIFT) |
6285 (SICF_CLOCK_EN << SBTML_SICF_SHIFT));
6288 /* leave clock enabled */
6289 bcmsdh_reg_write(sdh, CORE_SB(corebase, sbtmstatelow), 4,
6290 (SICF_CLOCK_EN << SBTML_SICF_SHIFT));
6294 /* SDIO Pad drive strength to select value mappings */
6295 struct sdiod_drive_str {
6296 u8 strength; /* Pad Drive Strength in mA */
6297 u8 sel; /* Chip-specific select value */
6300 /* SDIO Drive Strength to sel value table for PMU Rev 1 */
6301 static const struct sdiod_drive_str sdiod_drive_strength_tab1[] = {
6309 /* SDIO Drive Strength to sel value table for PMU Rev 2, 3 */
6310 static const struct sdiod_drive_str sdiod_drive_strength_tab2[] = {
6321 /* SDIO Drive Strength to sel value table for PMU Rev 8 (1.8V) */
6322 static const struct sdiod_drive_str sdiod_drive_strength_tab3[] = {
6334 #define SDIOD_DRVSTR_KEY(chip, pmu) (((chip) << 16) | (pmu))
6337 dhdsdio_sdiod_drive_strength_init(struct dhd_bus *bus, u32 drivestrength) {
6338 struct sdiod_drive_str *str_tab = NULL;
6343 if (!(bus->ci->cccaps & CC_CAP_PMU))
6346 switch (SDIOD_DRVSTR_KEY(bus->ci->chip, bus->ci->pmurev)) {
6347 case SDIOD_DRVSTR_KEY(BCM4325_CHIP_ID, 1):
6348 str_tab = (struct sdiod_drive_str *)&sdiod_drive_strength_tab1;
6349 str_mask = 0x30000000;
6352 case SDIOD_DRVSTR_KEY(BCM4325_CHIP_ID, 2):
6353 case SDIOD_DRVSTR_KEY(BCM4325_CHIP_ID, 3):
6354 str_tab = (struct sdiod_drive_str *)&sdiod_drive_strength_tab2;
6355 str_mask = 0x00003800;
6358 case SDIOD_DRVSTR_KEY(BCM4336_CHIP_ID, 8):
6359 str_tab = (struct sdiod_drive_str *)&sdiod_drive_strength_tab3;
6360 str_mask = 0x00003800;
6364 DHD_ERROR(("No SDIO Drive strength init"
6365 "done for chip %s rev %d pmurev %d\n",
6366 bcm_chipname(bus->ci->chip, chn, 8),
6367 bus->ci->chiprev, bus->ci->pmurev));
6371 if (str_tab != NULL) {
6372 u32 drivestrength_sel = 0;
6376 for (i = 0; str_tab[i].strength != 0; i++) {
6377 if (drivestrength >= str_tab[i].strength) {
6378 drivestrength_sel = str_tab[i].sel;
6383 bcmsdh_reg_write(bus->sdh,
6384 CORE_CC_REG(bus->ci->cccorebase, chipcontrol_addr),
6386 cc_data_temp = bcmsdh_reg_read(bus->sdh,
6387 CORE_CC_REG(bus->ci->cccorebase, chipcontrol_addr), 4);
6388 cc_data_temp &= ~str_mask;
6389 drivestrength_sel <<= str_shift;
6390 cc_data_temp |= drivestrength_sel;
6391 bcmsdh_reg_write(bus->sdh,
6392 CORE_CC_REG(bus->ci->cccorebase, chipcontrol_addr),
6395 DHD_INFO(("SDIO: %dmA drive strength selected, set to 0x%08x\n",
6396 drivestrength, cc_data_temp));
6401 dhdsdio_chip_detach(struct dhd_bus *bus)
6403 DHD_TRACE(("%s: Enter\n", __func__));