2 * Copyright (c) 2010 Broadcom Corporation
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <linux/types.h>
18 #include <linux/kernel.h>
19 #include <linux/printk.h>
20 #include <linux/pci_ids.h>
21 #include <linux/netdevice.h>
26 #include BCMEMBEDIMAGE
27 #endif /* BCMEMBEDIMAGE */
36 /* ARM trap handling */
38 /* Trap types defined by ARM (see arminc.h) */
40 /* Trap locations in lo memory */
42 #define FIRST_TRAP TR_RST
43 #define LAST_TRAP (TR_FIQ * TRAP_STRIDE)
45 #if defined(__ARM_ARCH_4T__)
46 #define MAX_TRAP_TYPE (TR_FIQ + 1)
47 #elif defined(__ARM_ARCH_7M__)
48 #define MAX_TRAP_TYPE (TR_ISR + ARMCM3_NUMINTS)
49 #endif /* __ARM_ARCH_7M__ */
51 /* The trap structure is defined here as offsets for assembly */
57 #define TR_REG(n) (TR_REGS + (n) * 4)
58 #define TR_SP TR_REG(13)
59 #define TR_LR TR_REG(14)
60 #define TR_PC TR_REG(15)
62 #define TRAP_T_SIZE 80
64 #ifndef _LANGUAGE_ASSEMBLY
66 typedef struct _trap_struct {
89 #endif /* !_LANGUAGE_ASSEMBLY */
91 #define CBUF_LEN (128)
93 #define LOG_BUF_LEN 1024
96 u32 buf; /* Can't be pointer on (64-bit) hosts */
99 char *_buf_compat; /* Redundant pointer for backward compat. */
104 * When there is no UART (e.g. Quickturn),
105 * the host should write a complete
106 * input line directly into cbuf and then write
107 * the length into vcons_in.
108 * This may also be used when there is a real UART
109 * (at risk of conflicting with
110 * the real UART). vcons_out is currently unused.
112 volatile uint vcons_in;
113 volatile uint vcons_out;
115 /* Output (logging) buffer
116 * Console output is written to a ring buffer log_buf at index log_idx.
117 * The host may read the output when it sees log_idx advance.
118 * Output will be lost if the output wraps around faster than the host
123 /* Console input line buffer
124 * Characters are read one at a time into cbuf
125 * until <CR> is received, then
126 * the buffer is processed as a command line.
127 * Also used for virtual UART.
133 #endif /* DHD_DEBUG */
139 #include <sbsdpcmdev.h>
140 #include <bcmsdpcm.h>
142 #include <dngl_stats.h>
145 #include <dhd_proto.h>
150 #ifndef DHDSDIO_MEM_DUMP_FNAME
151 #define DHDSDIO_MEM_DUMP_FNAME "mem_dump"
154 #define TXQLEN 2048 /* bulk tx queue length */
155 #define TXHI (TXQLEN - 256) /* turn on flow control above TXHI */
156 #define TXLOW (TXHI - 256) /* turn off flow control below TXLOW */
159 #define TXRETRIES 2 /* # of retries for tx frames */
161 #if defined(CONFIG_MACH_SANDGATE2G)
162 #define DHD_RXBOUND 250 /* Default for max rx frames in
165 #define DHD_RXBOUND 50 /* Default for max rx frames in
167 #endif /* defined(CONFIG_MACH_SANDGATE2G) */
169 #define DHD_TXBOUND 20 /* Default for max tx frames in
172 #define DHD_TXMINMAX 1 /* Max tx frames if rx still pending */
174 #define MEMBLOCK 2048 /* Block size used for downloading
176 #define MAX_DATA_BUF (32 * 1024) /* Must be large enough to hold
177 biggest possible glom */
179 /* Packet alignment for most efficient SDIO (can change based on platform) */
181 #define DHD_SDALIGN 32
183 #if !ISPOWEROF2(DHD_SDALIGN)
184 #error DHD_SDALIGN is not a power of 2!
187 #ifndef DHD_FIRSTREAD
188 #define DHD_FIRSTREAD 32
190 #if !ISPOWEROF2(DHD_FIRSTREAD)
191 #error DHD_FIRSTREAD is not a power of 2!
194 /* Total length of frame header for dongle protocol */
195 #define SDPCM_HDRLEN (SDPCM_FRAMETAG_LEN + SDPCM_SWHEADER_LEN)
197 #define SDPCM_RESERVE (SDPCM_HDRLEN + SDPCM_TEST_HDRLEN + DHD_SDALIGN)
199 #define SDPCM_RESERVE (SDPCM_HDRLEN + DHD_SDALIGN)
202 /* Space for header read, limit for data packets */
204 #define MAX_HDR_READ 32
206 #if !ISPOWEROF2(MAX_HDR_READ)
207 #error MAX_HDR_READ is not a power of 2!
210 #define MAX_RX_DATASZ 2048
212 /* Maximum milliseconds to wait for F2 to come up */
213 #define DHD_WAIT_F2RDY 3000
215 /* Bump up limit on waiting for HT to account for first startup;
216 * if the image is doing a CRC calculation before programming the PMU
217 * for HT availability, it could take a couple hundred ms more, so
218 * max out at a 1 second (1000000us).
220 #if (PMU_MAX_TRANSITION_DLY <= 1000000)
221 #undef PMU_MAX_TRANSITION_DLY
222 #define PMU_MAX_TRANSITION_DLY 1000000
225 /* Value for ChipClockCSR during initial setup */
226 #define DHD_INIT_CLKCTL1 (SBSDIO_FORCE_HW_CLKREQ_OFF | \
227 SBSDIO_ALP_AVAIL_REQ)
228 #define DHD_INIT_CLKCTL2 (SBSDIO_FORCE_HW_CLKREQ_OFF | SBSDIO_FORCE_ALP)
230 /* Flags for SDH calls */
231 #define F2SYNC (SDIO_REQ_4BYTE | SDIO_REQ_FIXED)
234 #define SBIM_IBE 0x20000 /* inbanderror */
235 #define SBIM_TO 0x40000 /* timeout */
236 #define SBIM_BY 0x01800000 /* busy (sonics >= 2.3) */
237 #define SBIM_RJ 0x02000000 /* reject (sonics >= 2.3) */
240 #define SBTML_RESET 0x0001 /* reset */
241 #define SBTML_REJ_MASK 0x0006 /* reject field */
242 #define SBTML_REJ 0x0002 /* reject */
243 #define SBTML_TMPREJ 0x0004 /* temporary reject, for error recovery */
245 #define SBTML_SICF_SHIFT 16 /* Shift to locate the SI control flags in sbtml */
248 #define SBTMH_SERR 0x0001 /* serror */
249 #define SBTMH_INT 0x0002 /* interrupt */
250 #define SBTMH_BUSY 0x0004 /* busy */
251 #define SBTMH_TO 0x0020 /* timeout (sonics >= 2.3) */
253 #define SBTMH_SISF_SHIFT 16 /* Shift to locate the SI status flags in sbtmh */
256 #define SBIDL_INIT 0x80 /* initiator */
259 #define SBIDH_RC_MASK 0x000f /* revision code */
260 #define SBIDH_RCE_MASK 0x7000 /* revision code extension field */
261 #define SBIDH_RCE_SHIFT 8
262 #define SBCOREREV(sbidh) \
263 ((((sbidh) & SBIDH_RCE_MASK) >> SBIDH_RCE_SHIFT) | ((sbidh) & SBIDH_RC_MASK))
264 #define SBIDH_CC_MASK 0x8ff0 /* core code */
265 #define SBIDH_CC_SHIFT 4
266 #define SBIDH_VC_MASK 0xffff0000 /* vendor code */
267 #define SBIDH_VC_SHIFT 16
270 * Conversion of 802.1D priority to precedence level
272 #define PRIO2PREC(prio) \
273 (((prio) == PRIO_8021D_NONE || (prio) == PRIO_8021D_BE) ? \
276 DHD_SPINWAIT_SLEEP_INIT(sdioh_spinwait_sleep);
277 extern int dhdcdc_set_ioctl(dhd_pub_t *dhd, int ifidx, uint cmd, void *buf,
280 /* Core reg address translation */
281 #define CORE_CC_REG(base, field) (base + offsetof(chipcregs_t, field))
282 #define CORE_BUS_REG(base, field) (base + offsetof(sdpcmd_regs_t, field))
283 #define CORE_SB(base, field) \
284 (base + SBCONFIGOFF + offsetof(sbconfig_t, field))
287 /* Device console log buffer state */
288 typedef struct dhd_console {
289 uint count; /* Poll interval msec counter */
290 uint log_addr; /* Log struct address (fixed) */
291 rte_log_t log; /* Log struct (host copy) */
292 uint bufsize; /* Size of log buffer */
293 u8 *buf; /* Log buffer (host copy) */
294 uint last; /* Last buffer read index */
296 #endif /* DHD_DEBUG */
298 /* misc chip info needed by some of the routines */
314 /* Private data for SDIO bus interaction */
315 typedef struct dhd_bus {
318 bcmsdh_info_t *sdh; /* Handle for BCMSDH calls */
319 struct chip_info *ci; /* Chip info struct */
320 char *vars; /* Variables (from CIS and/or other) */
321 uint varsz; /* Size of variables buffer */
322 u32 sbaddr; /* Current SB window pointer (-1, invalid) */
324 sdpcmd_regs_t *regs; /* Registers for SDIO core */
325 uint sdpcmrev; /* SDIO core revision */
326 uint armrev; /* CPU core revision */
327 uint ramrev; /* SOCRAM core revision */
328 u32 ramsize; /* Size of RAM in SOCRAM (bytes) */
329 u32 orig_ramsize; /* Size of RAM in SOCRAM (bytes) */
331 u32 bus; /* gSPI or SDIO bus */
332 u32 hostintmask; /* Copy of Host Interrupt Mask */
333 u32 intstatus; /* Intstatus bits (events) pending */
334 bool dpc_sched; /* Indicates DPC schedule (intrpt rcvd) */
335 bool fcstate; /* State of dongle flow-control */
337 u16 cl_devid; /* cached devid for dhdsdio_probe_attach() */
338 char *fw_path; /* module_param: path to firmware image */
339 char *nv_path; /* module_param: path to nvram vars file */
340 const char *nvram_params; /* user specified nvram params. */
342 uint blocksize; /* Block size of SDIO transfers */
343 uint roundup; /* Max roundup limit */
345 struct pktq txq; /* Queue length used for flow-control */
346 u8 flowcontrol; /* per prio flow control bitmask */
347 u8 tx_seq; /* Transmit sequence number (next) */
348 u8 tx_max; /* Maximum transmit sequence allowed */
350 u8 hdrbuf[MAX_HDR_READ + DHD_SDALIGN];
351 u8 *rxhdr; /* Header of current rx frame (in hdrbuf) */
352 u16 nextlen; /* Next Read Len from last header */
353 u8 rx_seq; /* Receive sequence number (expected) */
354 bool rxskip; /* Skip receive (awaiting NAK ACK) */
356 struct sk_buff *glomd; /* Packet containing glomming descriptor */
357 struct sk_buff *glom; /* Packet chain for glommed superframe */
358 uint glomerr; /* Glom packet read errors */
360 u8 *rxbuf; /* Buffer for receiving control packets */
361 uint rxblen; /* Allocated length of rxbuf */
362 u8 *rxctl; /* Aligned pointer into rxbuf */
363 u8 *databuf; /* Buffer for receiving big glom packet */
364 u8 *dataptr; /* Aligned pointer into databuf */
365 uint rxlen; /* Length of valid data in buffer */
367 u8 sdpcm_ver; /* Bus protocol reported by dongle */
369 bool intr; /* Use interrupts */
370 bool poll; /* Use polling */
371 bool ipend; /* Device interrupt is pending */
372 bool intdis; /* Interrupts disabled by isr */
373 uint intrcount; /* Count of device interrupt callbacks */
374 uint lastintrs; /* Count as of last watchdog timer */
375 uint spurious; /* Count of spurious interrupts */
376 uint pollrate; /* Ticks between device polls */
377 uint polltick; /* Tick counter */
378 uint pollcnt; /* Count of active polls */
381 dhd_console_t console; /* Console output polling support */
382 uint console_addr; /* Console address from shared struct */
383 #endif /* DHD_DEBUG */
385 uint regfails; /* Count of R_REG/W_REG failures */
387 uint clkstate; /* State of sd and backplane clock(s) */
388 bool activity; /* Activity flag for clock down */
389 s32 idletime; /* Control for activity timeout */
390 s32 idlecount; /* Activity timeout counter */
391 s32 idleclock; /* How to set bus driver when idle */
392 s32 sd_rxchain; /* If bcmsdh api accepts PKT chains */
393 bool use_rxchain; /* If dhd should use PKT chains */
394 bool sleeping; /* Is SDIO bus sleeping? */
395 bool rxflow_mode; /* Rx flow control mode */
396 bool rxflow; /* Is rx flow control on */
397 uint prev_rxlim_hit; /* Is prev rx limit exceeded
398 (per dpc schedule) */
399 bool alp_only; /* Don't use HT clock (ALP only) */
400 /* Field to decide if rx of control frames happen in rxbuf or lb-pool */
404 /* external loopback */
408 /* pktgen configuration */
409 uint pktgen_freq; /* Ticks between bursts */
410 uint pktgen_count; /* Packets to send each burst */
411 uint pktgen_print; /* Bursts between count displays */
412 uint pktgen_total; /* Stop after this many */
413 uint pktgen_minlen; /* Minimum packet data len */
414 uint pktgen_maxlen; /* Maximum packet data len */
415 uint pktgen_mode; /* Configured mode: tx, rx, or echo */
416 uint pktgen_stop; /* Number of tx failures causing stop */
418 /* active pktgen fields */
419 uint pktgen_tick; /* Tick counter for bursts */
420 uint pktgen_ptick; /* Burst counter for printing */
421 uint pktgen_sent; /* Number of test packets generated */
422 uint pktgen_rcvd; /* Number of test packets received */
423 uint pktgen_fail; /* Number of failed send attempts */
424 u16 pktgen_len; /* Length of next packet to send */
427 /* Some additional counters */
428 uint tx_sderrs; /* Count of tx attempts with sd errors */
429 uint fcqueued; /* Tx packets that got queued */
430 uint rxrtx; /* Count of rtx requests (NAK to dongle) */
431 uint rx_toolong; /* Receive frames too long to receive */
432 uint rxc_errors; /* SDIO errors when reading control frames */
433 uint rx_hdrfail; /* SDIO errors on header reads */
434 uint rx_badhdr; /* Bad received headers (roosync?) */
435 uint rx_badseq; /* Mismatched rx sequence number */
436 uint fc_rcvd; /* Number of flow-control events received */
437 uint fc_xoff; /* Number which turned on flow-control */
438 uint fc_xon; /* Number which turned off flow-control */
439 uint rxglomfail; /* Failed deglom attempts */
440 uint rxglomframes; /* Number of glom frames (superframes) */
441 uint rxglompkts; /* Number of packets from glom frames */
442 uint f2rxhdrs; /* Number of header reads */
443 uint f2rxdata; /* Number of frame data reads */
444 uint f2txdata; /* Number of f2 frame writes */
445 uint f1regdata; /* Number of f1 register accesses */
449 bool ctrl_frame_stat;
452 #ifndef _LANGUAGE_ASSEMBLY
454 typedef volatile struct _sbconfig {
456 u32 sbipsflag; /* initiator port ocp slave flag */
458 u32 sbtpsflag; /* target port ocp slave flag */
460 u32 sbtmerrloga; /* (sonics >= 2.3) */
462 u32 sbtmerrlog; /* (sonics >= 2.3) */
464 u32 sbadmatch3; /* address match3 */
466 u32 sbadmatch2; /* address match2 */
468 u32 sbadmatch1; /* address match1 */
470 u32 sbimstate; /* initiator agent state */
471 u32 sbintvec; /* interrupt mask */
472 u32 sbtmstatelow; /* target state */
473 u32 sbtmstatehigh; /* target state */
474 u32 sbbwa0; /* bandwidth allocation table0 */
476 u32 sbimconfiglow; /* initiator configuration */
477 u32 sbimconfighigh; /* initiator configuration */
478 u32 sbadmatch0; /* address match0 */
480 u32 sbtmconfiglow; /* target configuration */
481 u32 sbtmconfighigh; /* target configuration */
482 u32 sbbconfig; /* broadcast configuration */
484 u32 sbbstate; /* broadcast state */
486 u32 sbactcnfg; /* activate configuration */
488 u32 sbflagst; /* current sbflags */
490 u32 sbidlow; /* identification */
491 u32 sbidhigh; /* identification */
494 #endif /* _LANGUAGE_ASSEMBLY */
499 #define CLK_PENDING 2 /* Not used yet */
502 #define DHD_NOPMU(dhd) (false)
505 static int qcount[NUMPRIO];
506 static int tx_packets[NUMPRIO];
507 #endif /* DHD_DEBUG */
509 /* Deferred transmit */
510 const uint dhd_deferred_tx = 1;
512 extern uint dhd_watchdog_ms;
513 extern void dhd_os_wd_timer(void *bus, uint wdtick);
520 /* override the RAM size if possible */
521 #define DONGLE_MIN_MEMSIZE (128 * 1024)
522 int dhd_dongle_memsize;
524 static bool dhd_alignctl;
528 static bool retrydata;
529 #define RETRYCHAN(chan) (((chan) == SDPCM_EVENT_CHANNEL) || retrydata)
531 static const uint watermark = 8;
532 static const uint firstread = DHD_FIRSTREAD;
534 #define HDATLEN (firstread - (SDPCM_HDRLEN))
536 /* Retry count for register access failures */
537 static const uint retry_limit = 2;
539 /* Force even SD lengths (some host controllers mess up on odd bytes) */
540 static bool forcealign;
544 #if defined(OOB_INTR_ONLY) && defined(HW_OOB)
545 extern void bcmsdh_enable_hw_oob_intr(void *sdh, bool enable);
548 #if defined(OOB_INTR_ONLY) && defined(SDIO_ISR_THREAD)
549 #error OOB_INTR_ONLY is NOT working with SDIO_ISR_THREAD
550 #endif /* defined(OOB_INTR_ONLY) && defined(SDIO_ISR_THREAD) */
551 #define PKTALIGN(_p, _len, _align) \
554 datalign = (unsigned long)((_p)->data); \
555 datalign = roundup(datalign, (_align)) - datalign; \
556 ASSERT(datalign < (_align)); \
557 ASSERT((_p)->len >= ((_len) + datalign)); \
559 skb_pull((_p), datalign); \
560 __skb_trim((_p), (_len)); \
563 /* Limit on rounding up frames */
564 static const uint max_roundup = 512;
566 /* Try doing readahead */
567 static bool dhd_readahead;
569 /* To check if there's window offered */
570 #define DATAOK(bus) \
571 (((u8)(bus->tx_max - bus->tx_seq) != 0) && \
572 (((u8)(bus->tx_max - bus->tx_seq) & 0x80) == 0))
574 /* Macros to get register read/write status */
575 /* NOTE: these assume a local dhdsdio_bus_t *bus! */
576 #define R_SDREG(regvar, regaddr, retryvar) \
580 regvar = R_REG(regaddr); \
581 } while (bcmsdh_regfail(bus->sdh) && (++retryvar <= retry_limit)); \
583 bus->regfails += (retryvar-1); \
584 if (retryvar > retry_limit) { \
585 DHD_ERROR(("%s: FAILED" #regvar "READ, LINE %d\n", \
586 __func__, __LINE__)); \
592 #define W_SDREG(regval, regaddr, retryvar) \
596 W_REG(regaddr, regval); \
597 } while (bcmsdh_regfail(bus->sdh) && (++retryvar <= retry_limit)); \
599 bus->regfails += (retryvar-1); \
600 if (retryvar > retry_limit) \
601 DHD_ERROR(("%s: FAILED REGISTER WRITE, LINE %d\n", \
602 __func__, __LINE__)); \
606 #define DHD_BUS SDIO_BUS
608 #define PKT_AVAILABLE() (intstatus & I_HMB_FRAME_IND)
610 #define HOSTINTMASK (I_HMB_SW_MASK | I_CHIPACTIVE)
613 static void dhdsdio_testrcv(dhd_bus_t *bus, void *pkt, uint seq);
614 static void dhdsdio_sdtest_set(dhd_bus_t *bus, bool start);
618 static int dhdsdio_checkdied(dhd_bus_t *bus, u8 *data, uint size);
619 static int dhdsdio_mem_dump(dhd_bus_t *bus);
620 #endif /* DHD_DEBUG */
621 static int dhdsdio_download_state(dhd_bus_t *bus, bool enter);
623 static void dhdsdio_release(dhd_bus_t *bus);
624 static void dhdsdio_release_malloc(dhd_bus_t *bus);
625 static void dhdsdio_disconnect(void *ptr);
626 static bool dhdsdio_chipmatch(u16 chipid);
627 static bool dhdsdio_probe_attach(dhd_bus_t *bus, void *sdh,
628 void *regsva, u16 devid);
629 static bool dhdsdio_probe_malloc(dhd_bus_t *bus, void *sdh);
630 static bool dhdsdio_probe_init(dhd_bus_t *bus, void *sdh);
631 static void dhdsdio_release_dongle(dhd_bus_t *bus);
633 static uint process_nvram_vars(char *varbuf, uint len);
635 static void dhd_dongle_setmemsize(struct dhd_bus *bus, int mem_size);
636 static int dhd_bcmsdh_send_buf(dhd_bus_t *bus, u32 addr, uint fn,
637 uint flags, u8 *buf, uint nbytes,
638 struct sk_buff *pkt, bcmsdh_cmplt_fn_t complete,
641 static bool dhdsdio_download_firmware(struct dhd_bus *bus, void *sdh);
642 static int _dhdsdio_download_firmware(struct dhd_bus *bus);
644 static int dhdsdio_download_code_file(struct dhd_bus *bus, char *image_path);
645 static int dhdsdio_download_nvram(struct dhd_bus *bus);
647 static int dhdsdio_download_code_array(struct dhd_bus *bus);
649 static void dhdsdio_chip_disablecore(bcmsdh_info_t *sdh, u32 corebase);
650 static int dhdsdio_chip_attach(struct dhd_bus *bus, void *regs);
651 static void dhdsdio_chip_resetcore(bcmsdh_info_t *sdh, u32 corebase);
652 static void dhdsdio_sdiod_drive_strength_init(struct dhd_bus *bus,
654 static void dhdsdio_chip_detach(struct dhd_bus *bus);
656 /* Packet free applicable unconditionally for sdio and sdspi.
657 * Conditional if bufpool was present for gspi bus.
659 static void dhdsdio_pktfree2(dhd_bus_t *bus, struct sk_buff *pkt)
661 dhd_os_sdlock_rxq(bus->dhd);
662 if ((bus->bus != SPI_BUS) || bus->usebufpool)
663 bcm_pkt_buf_free_skb(pkt);
664 dhd_os_sdunlock_rxq(bus->dhd);
667 static void dhd_dongle_setmemsize(struct dhd_bus *bus, int mem_size)
669 s32 min_size = DONGLE_MIN_MEMSIZE;
670 /* Restrict the memsize to user specified limit */
671 DHD_ERROR(("user: Restrict the dongle ram size to %d, min %d\n",
672 dhd_dongle_memsize, min_size));
673 if ((dhd_dongle_memsize > min_size) &&
674 (dhd_dongle_memsize < (s32) bus->orig_ramsize))
675 bus->ramsize = dhd_dongle_memsize;
678 static int dhdsdio_set_siaddr_window(dhd_bus_t *bus, u32 address)
681 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_SBADDRLOW,
682 (address >> 8) & SBSDIO_SBADDRLOW_MASK, &err);
684 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_SBADDRMID,
685 (address >> 16) & SBSDIO_SBADDRMID_MASK, &err);
687 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_SBADDRHIGH,
688 (address >> 24) & SBSDIO_SBADDRHIGH_MASK,
693 /* Turn backplane clock on or off */
694 static int dhdsdio_htclk(dhd_bus_t *bus, bool on, bool pendok)
697 u8 clkctl, clkreq, devctl;
700 DHD_TRACE(("%s: Enter\n", __func__));
702 #if defined(OOB_INTR_ONLY)
709 /* Request HT Avail */
711 bus->alp_only ? SBSDIO_ALP_AVAIL_REQ : SBSDIO_HT_AVAIL_REQ;
713 if ((bus->ci->chip == BCM4329_CHIP_ID)
714 && (bus->ci->chiprev == 0))
715 clkreq |= SBSDIO_FORCE_ALP;
717 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
720 DHD_ERROR(("%s: HT Avail request error: %d\n",
725 if (pendok && ((bus->ci->buscoretype == PCMCIA_CORE_ID)
726 && (bus->ci->buscorerev == 9))) {
728 R_SDREG(dummy, &bus->regs->clockctlstatus, retries);
731 /* Check current status */
733 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
736 DHD_ERROR(("%s: HT Avail read error: %d\n",
741 /* Go to pending and await interrupt if appropriate */
742 if (!SBSDIO_CLKAV(clkctl, bus->alp_only) && pendok) {
743 /* Allow only clock-available interrupt */
745 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
748 DHD_ERROR(("%s: Devctl error setting CA: %d\n",
753 devctl |= SBSDIO_DEVCTL_CA_INT_ONLY;
754 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
756 DHD_INFO(("CLKCTL: set PENDING\n"));
757 bus->clkstate = CLK_PENDING;
760 } else if (bus->clkstate == CLK_PENDING) {
761 /* Cancel CA-only interrupt filter */
763 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
765 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
766 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
770 /* Otherwise, wait here (polling) for HT Avail */
771 if (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
772 SPINWAIT_SLEEP(sdioh_spinwait_sleep,
774 bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
775 SBSDIO_FUNC1_CHIPCLKCSR,
777 !SBSDIO_CLKAV(clkctl, bus->alp_only)),
778 PMU_MAX_TRANSITION_DLY);
781 DHD_ERROR(("%s: HT Avail request error: %d\n",
785 if (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
786 DHD_ERROR(("%s: HT Avail timeout (%d): clkctl 0x%02x\n",
787 __func__, PMU_MAX_TRANSITION_DLY, clkctl));
791 /* Mark clock available */
792 bus->clkstate = CLK_AVAIL;
793 DHD_INFO(("CLKCTL: turned ON\n"));
795 #if defined(DHD_DEBUG)
796 if (bus->alp_only == true) {
797 #if !defined(BCMLXSDMMC)
798 if (!SBSDIO_ALPONLY(clkctl)) {
799 DHD_ERROR(("%s: HT Clock, when ALP Only\n",
802 #endif /* !defined(BCMLXSDMMC) */
804 if (SBSDIO_ALPONLY(clkctl)) {
805 DHD_ERROR(("%s: HT Clock should be on.\n",
809 #endif /* defined (DHD_DEBUG) */
811 bus->activity = true;
815 if (bus->clkstate == CLK_PENDING) {
816 /* Cancel CA-only interrupt filter */
818 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
820 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
821 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
825 bus->clkstate = CLK_SDONLY;
826 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
828 DHD_INFO(("CLKCTL: turned OFF\n"));
830 DHD_ERROR(("%s: Failed access turning clock off: %d\n",
838 /* Change idle/active SD state */
839 static int dhdsdio_sdclk(dhd_bus_t *bus, bool on)
841 DHD_TRACE(("%s: Enter\n", __func__));
844 bus->clkstate = CLK_SDONLY;
846 bus->clkstate = CLK_NONE;
851 /* Transition SD and backplane clock readiness */
852 static int dhdsdio_clkctl(dhd_bus_t *bus, uint target, bool pendok)
855 uint oldstate = bus->clkstate;
856 #endif /* DHD_DEBUG */
858 DHD_TRACE(("%s: Enter\n", __func__));
860 /* Early exit if we're already there */
861 if (bus->clkstate == target) {
862 if (target == CLK_AVAIL) {
863 dhd_os_wd_timer(bus->dhd, dhd_watchdog_ms);
864 bus->activity = true;
871 /* Make sure SD clock is available */
872 if (bus->clkstate == CLK_NONE)
873 dhdsdio_sdclk(bus, true);
874 /* Now request HT Avail on the backplane */
875 dhdsdio_htclk(bus, true, pendok);
876 dhd_os_wd_timer(bus->dhd, dhd_watchdog_ms);
877 bus->activity = true;
881 /* Remove HT request, or bring up SD clock */
882 if (bus->clkstate == CLK_NONE)
883 dhdsdio_sdclk(bus, true);
884 else if (bus->clkstate == CLK_AVAIL)
885 dhdsdio_htclk(bus, false, false);
887 DHD_ERROR(("dhdsdio_clkctl: request for %d -> %d\n",
888 bus->clkstate, target));
889 dhd_os_wd_timer(bus->dhd, dhd_watchdog_ms);
893 /* Make sure to remove HT request */
894 if (bus->clkstate == CLK_AVAIL)
895 dhdsdio_htclk(bus, false, false);
896 /* Now remove the SD clock */
897 dhdsdio_sdclk(bus, false);
898 dhd_os_wd_timer(bus->dhd, 0);
902 DHD_INFO(("dhdsdio_clkctl: %d -> %d\n", oldstate, bus->clkstate));
903 #endif /* DHD_DEBUG */
908 int dhdsdio_bussleep(dhd_bus_t *bus, bool sleep)
910 bcmsdh_info_t *sdh = bus->sdh;
911 sdpcmd_regs_t *regs = bus->regs;
914 DHD_INFO(("dhdsdio_bussleep: request %s (currently %s)\n",
915 (sleep ? "SLEEP" : "WAKE"),
916 (bus->sleeping ? "SLEEP" : "WAKE")));
918 /* Done if we're already in the requested state */
919 if (sleep == bus->sleeping)
922 /* Going to sleep: set the alarm and turn off the lights... */
924 /* Don't sleep if something is pending */
925 if (bus->dpc_sched || bus->rxskip || pktq_len(&bus->txq))
928 /* Disable SDIO interrupts (no longer interested) */
929 bcmsdh_intr_disable(bus->sdh);
931 /* Make sure the controller has the bus up */
932 dhdsdio_clkctl(bus, CLK_AVAIL, false);
934 /* Tell device to start using OOB wakeup */
935 W_SDREG(SMB_USE_OOB, ®s->tosbmailbox, retries);
936 if (retries > retry_limit)
937 DHD_ERROR(("CANNOT SIGNAL CHIP, WILL NOT WAKE UP!!\n"));
939 /* Turn off our contribution to the HT clock request */
940 dhdsdio_clkctl(bus, CLK_SDONLY, false);
942 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
943 SBSDIO_FORCE_HW_CLKREQ_OFF, NULL);
945 /* Isolate the bus */
946 if (bus->ci->chip != BCM4329_CHIP_ID
947 && bus->ci->chip != BCM4319_CHIP_ID) {
948 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
949 SBSDIO_DEVCTL_PADS_ISO, NULL);
953 bus->sleeping = true;
956 /* Waking up: bus power up is ok, set local state */
958 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
961 /* Force pad isolation off if possible
962 (in case power never toggled) */
963 if ((bus->ci->buscoretype == PCMCIA_CORE_ID)
964 && (bus->ci->buscorerev >= 10))
965 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL, 0,
968 /* Make sure the controller has the bus up */
969 dhdsdio_clkctl(bus, CLK_AVAIL, false);
971 /* Send misc interrupt to indicate OOB not needed */
972 W_SDREG(0, ®s->tosbmailboxdata, retries);
973 if (retries <= retry_limit)
974 W_SDREG(SMB_DEV_INT, ®s->tosbmailbox, retries);
976 if (retries > retry_limit)
977 DHD_ERROR(("CANNOT SIGNAL CHIP TO CLEAR OOB!!\n"));
979 /* Make sure we have SD bus access */
980 dhdsdio_clkctl(bus, CLK_SDONLY, false);
983 bus->sleeping = false;
985 /* Enable interrupts again */
986 if (bus->intr && (bus->dhd->busstate == DHD_BUS_DATA)) {
988 bcmsdh_intr_enable(bus->sdh);
995 #if defined(OOB_INTR_ONLY)
996 void dhd_enable_oob_intr(struct dhd_bus *bus, bool enable)
999 bcmsdh_enable_hw_oob_intr(bus->sdh, enable);
1001 sdpcmd_regs_t *regs = bus->regs;
1004 dhdsdio_clkctl(bus, CLK_AVAIL, false);
1005 if (enable == true) {
1007 /* Tell device to start using OOB wakeup */
1008 W_SDREG(SMB_USE_OOB, ®s->tosbmailbox, retries);
1009 if (retries > retry_limit)
1010 DHD_ERROR(("CANNOT SIGNAL CHIP, WILL NOT WAKE UP!!\n"));
1013 /* Send misc interrupt to indicate OOB not needed */
1014 W_SDREG(0, ®s->tosbmailboxdata, retries);
1015 if (retries <= retry_limit)
1016 W_SDREG(SMB_DEV_INT, ®s->tosbmailbox, retries);
1019 /* Turn off our contribution to the HT clock request */
1020 dhdsdio_clkctl(bus, CLK_SDONLY, false);
1021 #endif /* !defined(HW_OOB) */
1023 #endif /* defined(OOB_INTR_ONLY) */
1025 #define BUS_WAKE(bus) \
1027 if ((bus)->sleeping) \
1028 dhdsdio_bussleep((bus), false); \
1031 /* Writes a HW/SW header into the packet and sends it. */
1032 /* Assumes: (a) header space already there, (b) caller holds lock */
1033 static int dhdsdio_txpkt(dhd_bus_t *bus, struct sk_buff *pkt, uint chan,
1042 struct sk_buff *new;
1045 DHD_TRACE(("%s: Enter\n", __func__));
1049 if (bus->dhd->dongle_reset) {
1054 frame = (u8 *) (pkt->data);
1056 /* Add alignment padding, allocate new packet if needed */
1057 pad = ((unsigned long)frame % DHD_SDALIGN);
1059 if (skb_headroom(pkt) < pad) {
1060 DHD_INFO(("%s: insufficient headroom %d for %d pad\n",
1061 __func__, skb_headroom(pkt), pad));
1062 bus->dhd->tx_realloc++;
1063 new = bcm_pkt_buf_get_skb(pkt->len + DHD_SDALIGN);
1065 DHD_ERROR(("%s: couldn't allocate new %d-byte "
1067 __func__, pkt->len + DHD_SDALIGN));
1072 PKTALIGN(new, pkt->len, DHD_SDALIGN);
1073 memcpy(new->data, pkt->data, pkt->len);
1075 bcm_pkt_buf_free_skb(pkt);
1076 /* free the pkt if canned one is not used */
1079 frame = (u8 *) (pkt->data);
1080 ASSERT(((unsigned long)frame % DHD_SDALIGN) == 0);
1084 frame = (u8 *) (pkt->data);
1086 ASSERT((pad + SDPCM_HDRLEN) <= (int)(pkt->len));
1087 memset(frame, 0, pad + SDPCM_HDRLEN);
1090 ASSERT(pad < DHD_SDALIGN);
1092 /* Hardware tag: 2 byte len followed by 2 byte ~len check (all LE) */
1093 len = (u16) (pkt->len);
1094 *(u16 *) frame = cpu_to_le16(len);
1095 *(((u16 *) frame) + 1) = cpu_to_le16(~len);
1097 /* Software tag: channel, sequence number, data offset */
1099 ((chan << SDPCM_CHANNEL_SHIFT) & SDPCM_CHANNEL_MASK) | bus->tx_seq |
1101 SDPCM_HDRLEN) << SDPCM_DOFFSET_SHIFT) & SDPCM_DOFFSET_MASK);
1103 put_unaligned_le32(swheader, frame + SDPCM_FRAMETAG_LEN);
1104 put_unaligned_le32(0, frame + SDPCM_FRAMETAG_LEN + sizeof(swheader));
1107 tx_packets[pkt->priority]++;
1108 if (DHD_BYTES_ON() &&
1109 (((DHD_CTL_ON() && (chan == SDPCM_CONTROL_CHANNEL)) ||
1110 (DHD_DATA_ON() && (chan != SDPCM_CONTROL_CHANNEL))))) {
1111 printk(KERN_DEBUG "Tx Frame:\n");
1112 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, frame, len);
1113 } else if (DHD_HDRS_ON()) {
1114 printk(KERN_DEBUG "TxHdr:\n");
1115 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
1116 frame, min_t(u16, len, 16));
1120 /* Raise len to next SDIO block to eliminate tail command */
1121 if (bus->roundup && bus->blocksize && (len > bus->blocksize)) {
1122 u16 pad = bus->blocksize - (len % bus->blocksize);
1123 if ((pad <= bus->roundup) && (pad < bus->blocksize))
1125 if (pad <= skb_tailroom(pkt))
1126 #endif /* NOTUSED */
1128 } else if (len % DHD_SDALIGN) {
1129 len += DHD_SDALIGN - (len % DHD_SDALIGN);
1132 /* Some controllers have trouble with odd bytes -- round to even */
1133 if (forcealign && (len & (ALIGNMENT - 1))) {
1135 if (skb_tailroom(pkt))
1137 len = roundup(len, ALIGNMENT);
1140 DHD_ERROR(("%s: sending unrounded %d-byte packet\n",
1147 dhd_bcmsdh_send_buf(bus, bcmsdh_cur_sbwad(sdh), SDIO_FUNC_2,
1148 F2SYNC, frame, len, pkt, NULL, NULL);
1150 ASSERT(ret != -BCME_PENDING);
1153 /* On failure, abort the command
1154 and terminate the frame */
1155 DHD_INFO(("%s: sdio error %d, abort command and "
1156 "terminate frame.\n", __func__, ret));
1159 bcmsdh_abort(sdh, SDIO_FUNC_2);
1160 bcmsdh_cfg_write(sdh, SDIO_FUNC_1,
1161 SBSDIO_FUNC1_FRAMECTRL, SFC_WF_TERM,
1165 for (i = 0; i < 3; i++) {
1167 hi = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
1168 SBSDIO_FUNC1_WFRAMEBCHI,
1170 lo = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
1171 SBSDIO_FUNC1_WFRAMEBCLO,
1173 bus->f1regdata += 2;
1174 if ((hi == 0) && (lo == 0))
1180 bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQUENCE_WRAP;
1182 } while ((ret < 0) && retrydata && retries++ < TXRETRIES);
1185 /* restore pkt buffer pointer before calling tx complete routine */
1186 skb_pull(pkt, SDPCM_HDRLEN + pad);
1187 dhd_os_sdunlock(bus->dhd);
1188 dhd_txcomplete(bus->dhd, pkt, ret != 0);
1189 dhd_os_sdlock(bus->dhd);
1192 bcm_pkt_buf_free_skb(pkt);
1197 int dhd_bus_txdata(struct dhd_bus *bus, struct sk_buff *pkt)
1202 DHD_TRACE(("%s: Enter\n", __func__));
1207 /* Push the test header if doing loopback */
1208 if (bus->ext_loop) {
1210 skb_push(pkt, SDPCM_TEST_HDRLEN);
1212 *data++ = SDPCM_TEST_ECHOREQ;
1213 *data++ = (u8) bus->loopid++;
1214 *data++ = (datalen >> 0);
1215 *data++ = (datalen >> 8);
1216 datalen += SDPCM_TEST_HDRLEN;
1220 /* Add space for the header */
1221 skb_push(pkt, SDPCM_HDRLEN);
1222 ASSERT(IS_ALIGNED((unsigned long)(pkt->data), 2));
1224 prec = PRIO2PREC((pkt->priority & PRIOMASK));
1226 /* Check for existing queue, current flow-control,
1227 pending event, or pending clock */
1228 if (dhd_deferred_tx || bus->fcstate || pktq_len(&bus->txq)
1229 || bus->dpc_sched || (!DATAOK(bus))
1230 || (bus->flowcontrol & NBITVAL(prec))
1231 || (bus->clkstate != CLK_AVAIL)) {
1232 DHD_TRACE(("%s: deferring pktq len %d\n", __func__,
1233 pktq_len(&bus->txq)));
1236 /* Priority based enq */
1237 dhd_os_sdlock_txq(bus->dhd);
1238 if (dhd_prec_enq(bus->dhd, &bus->txq, pkt, prec) == false) {
1239 skb_pull(pkt, SDPCM_HDRLEN);
1240 dhd_txcomplete(bus->dhd, pkt, false);
1241 bcm_pkt_buf_free_skb(pkt);
1242 DHD_ERROR(("%s: out of bus->txq !!!\n", __func__));
1247 dhd_os_sdunlock_txq(bus->dhd);
1249 if (pktq_len(&bus->txq) >= TXHI)
1250 dhd_txflowcontrol(bus->dhd, 0, ON);
1253 if (pktq_plen(&bus->txq, prec) > qcount[prec])
1254 qcount[prec] = pktq_plen(&bus->txq, prec);
1256 /* Schedule DPC if needed to send queued packet(s) */
1257 if (dhd_deferred_tx && !bus->dpc_sched) {
1258 bus->dpc_sched = true;
1259 dhd_sched_dpc(bus->dhd);
1262 /* Lock: we're about to use shared data/code (and SDIO) */
1263 dhd_os_sdlock(bus->dhd);
1265 /* Otherwise, send it now */
1267 /* Make sure back plane ht clk is on, no pending allowed */
1268 dhdsdio_clkctl(bus, CLK_AVAIL, true);
1271 DHD_TRACE(("%s: calling txpkt\n", __func__));
1272 ret = dhdsdio_txpkt(bus, pkt, SDPCM_DATA_CHANNEL, true);
1274 ret = dhdsdio_txpkt(bus, pkt,
1275 (bus->ext_loop ? SDPCM_TEST_CHANNEL :
1276 SDPCM_DATA_CHANNEL), true);
1279 bus->dhd->tx_errors++;
1281 bus->dhd->dstats.tx_bytes += datalen;
1283 if ((bus->idletime == DHD_IDLE_IMMEDIATE) && !bus->dpc_sched) {
1284 bus->activity = false;
1285 dhdsdio_clkctl(bus, CLK_NONE, true);
1288 dhd_os_sdunlock(bus->dhd);
1294 static uint dhdsdio_sendfromq(dhd_bus_t *bus, uint maxframes)
1296 struct sk_buff *pkt;
1299 int ret = 0, prec_out;
1304 dhd_pub_t *dhd = bus->dhd;
1305 sdpcmd_regs_t *regs = bus->regs;
1307 DHD_TRACE(("%s: Enter\n", __func__));
1309 tx_prec_map = ~bus->flowcontrol;
1311 /* Send frames until the limit or some other event */
1312 for (cnt = 0; (cnt < maxframes) && DATAOK(bus); cnt++) {
1313 dhd_os_sdlock_txq(bus->dhd);
1314 pkt = bcm_pktq_mdeq(&bus->txq, tx_prec_map, &prec_out);
1316 dhd_os_sdunlock_txq(bus->dhd);
1319 dhd_os_sdunlock_txq(bus->dhd);
1320 datalen = pkt->len - SDPCM_HDRLEN;
1323 ret = dhdsdio_txpkt(bus, pkt, SDPCM_DATA_CHANNEL, true);
1325 ret = dhdsdio_txpkt(bus, pkt,
1326 (bus->ext_loop ? SDPCM_TEST_CHANNEL :
1327 SDPCM_DATA_CHANNEL), true);
1330 bus->dhd->tx_errors++;
1332 bus->dhd->dstats.tx_bytes += datalen;
1334 /* In poll mode, need to check for other events */
1335 if (!bus->intr && cnt) {
1336 /* Check device status, signal pending interrupt */
1337 R_SDREG(intstatus, ®s->intstatus, retries);
1339 if (bcmsdh_regfail(bus->sdh))
1341 if (intstatus & bus->hostintmask)
1346 /* Deflow-control stack if needed */
1347 if (dhd->up && (dhd->busstate == DHD_BUS_DATA) &&
1348 dhd->txoff && (pktq_len(&bus->txq) < TXLOW))
1349 dhd_txflowcontrol(dhd, 0, OFF);
1354 int dhd_bus_txctl(struct dhd_bus *bus, unsigned char *msg, uint msglen)
1360 bcmsdh_info_t *sdh = bus->sdh;
1365 DHD_TRACE(("%s: Enter\n", __func__));
1367 if (bus->dhd->dongle_reset)
1370 /* Back the pointer to make a room for bus header */
1371 frame = msg - SDPCM_HDRLEN;
1372 len = (msglen += SDPCM_HDRLEN);
1374 /* Add alignment padding (optional for ctl frames) */
1376 doff = ((unsigned long)frame % DHD_SDALIGN);
1381 memset(frame, 0, doff + SDPCM_HDRLEN);
1383 ASSERT(doff < DHD_SDALIGN);
1385 doff += SDPCM_HDRLEN;
1387 /* Round send length to next SDIO block */
1388 if (bus->roundup && bus->blocksize && (len > bus->blocksize)) {
1389 u16 pad = bus->blocksize - (len % bus->blocksize);
1390 if ((pad <= bus->roundup) && (pad < bus->blocksize))
1392 } else if (len % DHD_SDALIGN) {
1393 len += DHD_SDALIGN - (len % DHD_SDALIGN);
1396 /* Satisfy length-alignment requirements */
1397 if (forcealign && (len & (ALIGNMENT - 1)))
1398 len = roundup(len, ALIGNMENT);
1400 ASSERT(IS_ALIGNED((unsigned long)frame, 2));
1402 /* Need to lock here to protect txseq and SDIO tx calls */
1403 dhd_os_sdlock(bus->dhd);
1407 /* Make sure backplane clock is on */
1408 dhdsdio_clkctl(bus, CLK_AVAIL, false);
1410 /* Hardware tag: 2 byte len followed by 2 byte ~len check (all LE) */
1411 *(u16 *) frame = cpu_to_le16((u16) msglen);
1412 *(((u16 *) frame) + 1) = cpu_to_le16(~msglen);
1414 /* Software tag: channel, sequence number, data offset */
1416 ((SDPCM_CONTROL_CHANNEL << SDPCM_CHANNEL_SHIFT) &
1418 | bus->tx_seq | ((doff << SDPCM_DOFFSET_SHIFT) &
1419 SDPCM_DOFFSET_MASK);
1420 put_unaligned_le32(swheader, frame + SDPCM_FRAMETAG_LEN);
1421 put_unaligned_le32(0, frame + SDPCM_FRAMETAG_LEN + sizeof(swheader));
1424 DHD_INFO(("%s: No bus credit bus->tx_max %d, bus->tx_seq %d\n",
1425 __func__, bus->tx_max, bus->tx_seq));
1426 bus->ctrl_frame_stat = true;
1428 bus->ctrl_frame_buf = frame;
1429 bus->ctrl_frame_len = len;
1431 dhd_wait_for_event(bus->dhd, &bus->ctrl_frame_stat);
1433 if (bus->ctrl_frame_stat == false) {
1434 DHD_INFO(("%s: ctrl_frame_stat == false\n", __func__));
1437 DHD_INFO(("%s: ctrl_frame_stat == true\n", __func__));
1444 if (DHD_BYTES_ON() && DHD_CTL_ON()) {
1445 printk(KERN_DEBUG "Tx Frame:\n");
1446 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
1448 } else if (DHD_HDRS_ON()) {
1449 printk(KERN_DEBUG "TxHdr:\n");
1450 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
1451 frame, min_t(u16, len, 16));
1456 bus->ctrl_frame_stat = false;
1458 dhd_bcmsdh_send_buf(bus, bcmsdh_cur_sbwad(sdh),
1459 SDIO_FUNC_2, F2SYNC, frame, len,
1462 ASSERT(ret != -BCME_PENDING);
1465 /* On failure, abort the command and
1466 terminate the frame */
1467 DHD_INFO(("%s: sdio error %d, abort command and terminate frame.\n",
1471 bcmsdh_abort(sdh, SDIO_FUNC_2);
1473 bcmsdh_cfg_write(sdh, SDIO_FUNC_1,
1474 SBSDIO_FUNC1_FRAMECTRL,
1478 for (i = 0; i < 3; i++) {
1480 hi = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
1481 SBSDIO_FUNC1_WFRAMEBCHI,
1483 lo = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
1484 SBSDIO_FUNC1_WFRAMEBCLO,
1486 bus->f1regdata += 2;
1487 if ((hi == 0) && (lo == 0))
1494 (bus->tx_seq + 1) % SDPCM_SEQUENCE_WRAP;
1496 } while ((ret < 0) && retries++ < TXRETRIES);
1499 if ((bus->idletime == DHD_IDLE_IMMEDIATE) && !bus->dpc_sched) {
1500 bus->activity = false;
1501 dhdsdio_clkctl(bus, CLK_NONE, true);
1504 dhd_os_sdunlock(bus->dhd);
1507 bus->dhd->tx_ctlerrs++;
1509 bus->dhd->tx_ctlpkts++;
1511 return ret ? -EIO : 0;
1514 int dhd_bus_rxctl(struct dhd_bus *bus, unsigned char *msg, uint msglen)
1520 DHD_TRACE(("%s: Enter\n", __func__));
1522 if (bus->dhd->dongle_reset)
1525 /* Wait until control frame is available */
1526 timeleft = dhd_os_ioctl_resp_wait(bus->dhd, &bus->rxlen, &pending);
1528 dhd_os_sdlock(bus->dhd);
1530 memcpy(msg, bus->rxctl, min(msglen, rxlen));
1532 dhd_os_sdunlock(bus->dhd);
1535 DHD_CTL(("%s: resumed on rxctl frame, got %d expected %d\n",
1536 __func__, rxlen, msglen));
1537 } else if (timeleft == 0) {
1538 DHD_ERROR(("%s: resumed on timeout\n", __func__));
1540 dhd_os_sdlock(bus->dhd);
1541 dhdsdio_checkdied(bus, NULL, 0);
1542 dhd_os_sdunlock(bus->dhd);
1543 #endif /* DHD_DEBUG */
1544 } else if (pending == true) {
1545 DHD_CTL(("%s: cancelled\n", __func__));
1546 return -ERESTARTSYS;
1548 DHD_CTL(("%s: resumed for unknown reason?\n", __func__));
1550 dhd_os_sdlock(bus->dhd);
1551 dhdsdio_checkdied(bus, NULL, 0);
1552 dhd_os_sdunlock(bus->dhd);
1553 #endif /* DHD_DEBUG */
1557 bus->dhd->rx_ctlpkts++;
1559 bus->dhd->rx_ctlerrs++;
1561 return rxlen ? (int)rxlen : -ETIMEDOUT;
1600 const bcm_iovar_t dhdsdio_iovars[] = {
1601 {"intr", IOV_INTR, 0, IOVT_BOOL, 0},
1602 {"sleep", IOV_SLEEP, 0, IOVT_BOOL, 0},
1603 {"pollrate", IOV_POLLRATE, 0, IOVT_UINT32, 0},
1604 {"idletime", IOV_IDLETIME, 0, IOVT_INT32, 0},
1605 {"idleclock", IOV_IDLECLOCK, 0, IOVT_INT32, 0},
1606 {"sd1idle", IOV_SD1IDLE, 0, IOVT_BOOL, 0},
1607 {"membytes", IOV_MEMBYTES, 0, IOVT_BUFFER, 2 * sizeof(int)},
1608 {"memsize", IOV_MEMSIZE, 0, IOVT_UINT32, 0},
1609 {"download", IOV_DOWNLOAD, 0, IOVT_BOOL, 0},
1610 {"vars", IOV_VARS, 0, IOVT_BUFFER, 0},
1611 {"sdiod_drive", IOV_SDIOD_DRIVE, 0, IOVT_UINT32, 0},
1612 {"readahead", IOV_READAHEAD, 0, IOVT_BOOL, 0},
1613 {"sdrxchain", IOV_SDRXCHAIN, 0, IOVT_BOOL, 0},
1614 {"alignctl", IOV_ALIGNCTL, 0, IOVT_BOOL, 0},
1615 {"sdalign", IOV_SDALIGN, 0, IOVT_BOOL, 0},
1616 {"devreset", IOV_DEVRESET, 0, IOVT_BOOL, 0},
1618 {"sdreg", IOV_SDREG, 0, IOVT_BUFFER, sizeof(sdreg_t)}
1620 {"sbreg", IOV_SBREG, 0, IOVT_BUFFER, sizeof(sdreg_t)}
1622 {"sd_cis", IOV_SDCIS, 0, IOVT_BUFFER, DHD_IOCTL_MAXLEN}
1624 {"forcealign", IOV_FORCEEVEN, 0, IOVT_BOOL, 0}
1626 {"txbound", IOV_TXBOUND, 0, IOVT_UINT32, 0}
1628 {"rxbound", IOV_RXBOUND, 0, IOVT_UINT32, 0}
1630 {"txminmax", IOV_TXMINMAX, 0, IOVT_UINT32, 0}
1632 {"cpu", IOV_CPU, 0, IOVT_BOOL, 0}
1635 {"checkdied", IOV_CHECKDIED, 0, IOVT_BUFFER, 0}
1637 #endif /* DHD_DEBUG */
1638 #endif /* DHD_DEBUG */
1640 {"extloop", IOV_EXTLOOP, 0, IOVT_BOOL, 0}
1642 {"pktgen", IOV_PKTGEN, 0, IOVT_BUFFER, sizeof(dhd_pktgen_t)}
1650 dhd_dump_pct(struct bcmstrbuf *strbuf, char *desc, uint num, uint div)
1655 bcm_bprintf(strbuf, "%s N/A", desc);
1658 q2 = (100 * (num - (q1 * div))) / div;
1659 bcm_bprintf(strbuf, "%s %d.%02d", desc, q1, q2);
1663 void dhd_bus_dump(dhd_pub_t *dhdp, struct bcmstrbuf *strbuf)
1665 dhd_bus_t *bus = dhdp->bus;
1667 bcm_bprintf(strbuf, "Bus SDIO structure:\n");
1669 "hostintmask 0x%08x intstatus 0x%08x sdpcm_ver %d\n",
1670 bus->hostintmask, bus->intstatus, bus->sdpcm_ver);
1672 "fcstate %d qlen %d tx_seq %d, max %d, rxskip %d rxlen %d rx_seq %d\n",
1673 bus->fcstate, pktq_len(&bus->txq), bus->tx_seq, bus->tx_max,
1674 bus->rxskip, bus->rxlen, bus->rx_seq);
1675 bcm_bprintf(strbuf, "intr %d intrcount %d lastintrs %d spurious %d\n",
1676 bus->intr, bus->intrcount, bus->lastintrs, bus->spurious);
1677 bcm_bprintf(strbuf, "pollrate %d pollcnt %d regfails %d\n",
1678 bus->pollrate, bus->pollcnt, bus->regfails);
1680 bcm_bprintf(strbuf, "\nAdditional counters:\n");
1682 "tx_sderrs %d fcqueued %d rxrtx %d rx_toolong %d rxc_errors %d\n",
1683 bus->tx_sderrs, bus->fcqueued, bus->rxrtx, bus->rx_toolong,
1685 bcm_bprintf(strbuf, "rx_hdrfail %d badhdr %d badseq %d\n",
1686 bus->rx_hdrfail, bus->rx_badhdr, bus->rx_badseq);
1687 bcm_bprintf(strbuf, "fc_rcvd %d, fc_xoff %d, fc_xon %d\n", bus->fc_rcvd,
1688 bus->fc_xoff, bus->fc_xon);
1689 bcm_bprintf(strbuf, "rxglomfail %d, rxglomframes %d, rxglompkts %d\n",
1690 bus->rxglomfail, bus->rxglomframes, bus->rxglompkts);
1691 bcm_bprintf(strbuf, "f2rx (hdrs/data) %d (%d/%d), f2tx %d f1regs %d\n",
1692 (bus->f2rxhdrs + bus->f2rxdata), bus->f2rxhdrs,
1693 bus->f2rxdata, bus->f2txdata, bus->f1regdata);
1695 dhd_dump_pct(strbuf, "\nRx: pkts/f2rd", bus->dhd->rx_packets,
1696 (bus->f2rxhdrs + bus->f2rxdata));
1697 dhd_dump_pct(strbuf, ", pkts/f1sd", bus->dhd->rx_packets,
1699 dhd_dump_pct(strbuf, ", pkts/sd", bus->dhd->rx_packets,
1700 (bus->f2rxhdrs + bus->f2rxdata + bus->f1regdata));
1701 dhd_dump_pct(strbuf, ", pkts/int", bus->dhd->rx_packets,
1703 bcm_bprintf(strbuf, "\n");
1705 dhd_dump_pct(strbuf, "Rx: glom pct", (100 * bus->rxglompkts),
1706 bus->dhd->rx_packets);
1707 dhd_dump_pct(strbuf, ", pkts/glom", bus->rxglompkts,
1709 bcm_bprintf(strbuf, "\n");
1711 dhd_dump_pct(strbuf, "Tx: pkts/f2wr", bus->dhd->tx_packets,
1713 dhd_dump_pct(strbuf, ", pkts/f1sd", bus->dhd->tx_packets,
1715 dhd_dump_pct(strbuf, ", pkts/sd", bus->dhd->tx_packets,
1716 (bus->f2txdata + bus->f1regdata));
1717 dhd_dump_pct(strbuf, ", pkts/int", bus->dhd->tx_packets,
1719 bcm_bprintf(strbuf, "\n");
1721 dhd_dump_pct(strbuf, "Total: pkts/f2rw",
1722 (bus->dhd->tx_packets + bus->dhd->rx_packets),
1723 (bus->f2txdata + bus->f2rxhdrs + bus->f2rxdata));
1724 dhd_dump_pct(strbuf, ", pkts/f1sd",
1725 (bus->dhd->tx_packets + bus->dhd->rx_packets),
1727 dhd_dump_pct(strbuf, ", pkts/sd",
1728 (bus->dhd->tx_packets + bus->dhd->rx_packets),
1729 (bus->f2txdata + bus->f2rxhdrs + bus->f2rxdata +
1731 dhd_dump_pct(strbuf, ", pkts/int",
1732 (bus->dhd->tx_packets + bus->dhd->rx_packets),
1734 bcm_bprintf(strbuf, "\n\n");
1738 if (bus->pktgen_count) {
1739 bcm_bprintf(strbuf, "pktgen config and count:\n");
1741 "freq %d count %d print %d total %d min %d len %d\n",
1742 bus->pktgen_freq, bus->pktgen_count,
1743 bus->pktgen_print, bus->pktgen_total,
1744 bus->pktgen_minlen, bus->pktgen_maxlen);
1745 bcm_bprintf(strbuf, "send attempts %d rcvd %d fail %d\n",
1746 bus->pktgen_sent, bus->pktgen_rcvd,
1751 bcm_bprintf(strbuf, "dpc_sched %d host interrupt%spending\n",
1753 (bcmsdh_intr_pending(bus->sdh) ? " " : " not "));
1754 bcm_bprintf(strbuf, "blocksize %d roundup %d\n", bus->blocksize,
1756 #endif /* DHD_DEBUG */
1758 "clkstate %d activity %d idletime %d idlecount %d sleeping %d\n",
1759 bus->clkstate, bus->activity, bus->idletime, bus->idlecount,
1763 void dhd_bus_clearcounts(dhd_pub_t *dhdp)
1765 dhd_bus_t *bus = (dhd_bus_t *) dhdp->bus;
1767 bus->intrcount = bus->lastintrs = bus->spurious = bus->regfails = 0;
1768 bus->rxrtx = bus->rx_toolong = bus->rxc_errors = 0;
1769 bus->rx_hdrfail = bus->rx_badhdr = bus->rx_badseq = 0;
1770 bus->tx_sderrs = bus->fc_rcvd = bus->fc_xoff = bus->fc_xon = 0;
1771 bus->rxglomfail = bus->rxglomframes = bus->rxglompkts = 0;
1772 bus->f2rxhdrs = bus->f2rxdata = bus->f2txdata = bus->f1regdata = 0;
1776 static int dhdsdio_pktgen_get(dhd_bus_t *bus, u8 *arg)
1778 dhd_pktgen_t pktgen;
1780 pktgen.version = DHD_PKTGEN_VERSION;
1781 pktgen.freq = bus->pktgen_freq;
1782 pktgen.count = bus->pktgen_count;
1783 pktgen.print = bus->pktgen_print;
1784 pktgen.total = bus->pktgen_total;
1785 pktgen.minlen = bus->pktgen_minlen;
1786 pktgen.maxlen = bus->pktgen_maxlen;
1787 pktgen.numsent = bus->pktgen_sent;
1788 pktgen.numrcvd = bus->pktgen_rcvd;
1789 pktgen.numfail = bus->pktgen_fail;
1790 pktgen.mode = bus->pktgen_mode;
1791 pktgen.stop = bus->pktgen_stop;
1793 memcpy(arg, &pktgen, sizeof(pktgen));
1798 static int dhdsdio_pktgen_set(dhd_bus_t *bus, u8 *arg)
1800 dhd_pktgen_t pktgen;
1801 uint oldcnt, oldmode;
1803 memcpy(&pktgen, arg, sizeof(pktgen));
1804 if (pktgen.version != DHD_PKTGEN_VERSION)
1807 oldcnt = bus->pktgen_count;
1808 oldmode = bus->pktgen_mode;
1810 bus->pktgen_freq = pktgen.freq;
1811 bus->pktgen_count = pktgen.count;
1812 bus->pktgen_print = pktgen.print;
1813 bus->pktgen_total = pktgen.total;
1814 bus->pktgen_minlen = pktgen.minlen;
1815 bus->pktgen_maxlen = pktgen.maxlen;
1816 bus->pktgen_mode = pktgen.mode;
1817 bus->pktgen_stop = pktgen.stop;
1819 bus->pktgen_tick = bus->pktgen_ptick = 0;
1820 bus->pktgen_len = max(bus->pktgen_len, bus->pktgen_minlen);
1821 bus->pktgen_len = min(bus->pktgen_len, bus->pktgen_maxlen);
1823 /* Clear counts for a new pktgen (mode change, or was stopped) */
1824 if (bus->pktgen_count && (!oldcnt || oldmode != bus->pktgen_mode))
1825 bus->pktgen_sent = bus->pktgen_rcvd = bus->pktgen_fail = 0;
1832 dhdsdio_membytes(dhd_bus_t *bus, bool write, u32 address, u8 *data,
1839 /* Determine initial transfer parameters */
1840 sdaddr = address & SBSDIO_SB_OFT_ADDR_MASK;
1841 if ((sdaddr + size) & SBSDIO_SBWINDOW_MASK)
1842 dsize = (SBSDIO_SB_OFT_ADDR_LIMIT - sdaddr);
1846 /* Set the backplane window to include the start address */
1847 bcmerror = dhdsdio_set_siaddr_window(bus, address);
1849 DHD_ERROR(("%s: window change failed\n", __func__));
1853 /* Do the transfer(s) */
1855 DHD_INFO(("%s: %s %d bytes at offset 0x%08x in window 0x%08x\n",
1856 __func__, (write ? "write" : "read"), dsize,
1857 sdaddr, (address & SBSDIO_SBWINDOW_MASK)));
1859 bcmsdh_rwdata(bus->sdh, write, sdaddr, data, dsize);
1861 DHD_ERROR(("%s: membytes transfer failed\n", __func__));
1865 /* Adjust for next transfer (if any) */
1870 bcmerror = dhdsdio_set_siaddr_window(bus, address);
1872 DHD_ERROR(("%s: window change failed\n",
1877 dsize = min_t(uint, SBSDIO_SB_OFT_ADDR_LIMIT, size);
1882 /* Return the window to backplane enumeration space for core access */
1883 if (dhdsdio_set_siaddr_window(bus, bcmsdh_cur_sbwad(bus->sdh))) {
1884 DHD_ERROR(("%s: FAILED to set window back to 0x%x\n",
1885 __func__, bcmsdh_cur_sbwad(bus->sdh)));
1892 static int dhdsdio_readshared(dhd_bus_t *bus, sdpcm_shared_t *sh)
1897 /* Read last word in memory to determine address of
1898 sdpcm_shared structure */
1899 rv = dhdsdio_membytes(bus, false, bus->ramsize - 4, (u8 *)&addr, 4);
1903 addr = le32_to_cpu(addr);
1905 DHD_INFO(("sdpcm_shared address 0x%08X\n", addr));
1908 * Check if addr is valid.
1909 * NVRAM length at the end of memory should have been overwritten.
1911 if (addr == 0 || ((~addr >> 16) & 0xffff) == (addr & 0xffff)) {
1912 DHD_ERROR(("%s: address (0x%08x) of sdpcm_shared invalid\n",
1917 /* Read rte_shared structure */
1918 rv = dhdsdio_membytes(bus, false, addr, (u8 *) sh,
1919 sizeof(sdpcm_shared_t));
1924 sh->flags = le32_to_cpu(sh->flags);
1925 sh->trap_addr = le32_to_cpu(sh->trap_addr);
1926 sh->assert_exp_addr = le32_to_cpu(sh->assert_exp_addr);
1927 sh->assert_file_addr = le32_to_cpu(sh->assert_file_addr);
1928 sh->assert_line = le32_to_cpu(sh->assert_line);
1929 sh->console_addr = le32_to_cpu(sh->console_addr);
1930 sh->msgtrace_addr = le32_to_cpu(sh->msgtrace_addr);
1932 if ((sh->flags & SDPCM_SHARED_VERSION_MASK) != SDPCM_SHARED_VERSION) {
1933 DHD_ERROR(("%s: sdpcm_shared version %d in dhd "
1934 "is different than sdpcm_shared version %d in dongle\n",
1935 __func__, SDPCM_SHARED_VERSION,
1936 sh->flags & SDPCM_SHARED_VERSION_MASK));
1943 static int dhdsdio_checkdied(dhd_bus_t *bus, u8 *data, uint size)
1947 char *mbuffer = NULL;
1948 uint maxstrlen = 256;
1951 sdpcm_shared_t sdpcm_shared;
1952 struct bcmstrbuf strbuf;
1954 DHD_TRACE(("%s: Enter\n", __func__));
1958 * Called after a rx ctrl timeout. "data" is NULL.
1959 * allocate memory to trace the trap or assert.
1962 mbuffer = data = kmalloc(msize, GFP_ATOMIC);
1963 if (mbuffer == NULL) {
1964 DHD_ERROR(("%s: kmalloc(%d) failed\n", __func__,
1971 str = kmalloc(maxstrlen, GFP_ATOMIC);
1973 DHD_ERROR(("%s: kmalloc(%d) failed\n", __func__, maxstrlen));
1978 bcmerror = dhdsdio_readshared(bus, &sdpcm_shared);
1982 bcm_binit(&strbuf, data, size);
1984 bcm_bprintf(&strbuf,
1985 "msgtrace address : 0x%08X\nconsole address : 0x%08X\n",
1986 sdpcm_shared.msgtrace_addr, sdpcm_shared.console_addr);
1988 if ((sdpcm_shared.flags & SDPCM_SHARED_ASSERT_BUILT) == 0) {
1989 /* NOTE: Misspelled assert is intentional - DO NOT FIX.
1990 * (Avoids conflict with real asserts for programmatic
1991 * parsing of output.)
1993 bcm_bprintf(&strbuf, "Assrt not built in dongle\n");
1996 if ((sdpcm_shared.flags & (SDPCM_SHARED_ASSERT | SDPCM_SHARED_TRAP)) ==
1998 /* NOTE: Misspelled assert is intentional - DO NOT FIX.
1999 * (Avoids conflict with real asserts for programmatic
2000 * parsing of output.)
2002 bcm_bprintf(&strbuf, "No trap%s in dongle",
2003 (sdpcm_shared.flags & SDPCM_SHARED_ASSERT_BUILT)
2006 if (sdpcm_shared.flags & SDPCM_SHARED_ASSERT) {
2007 /* Download assert */
2008 bcm_bprintf(&strbuf, "Dongle assert");
2009 if (sdpcm_shared.assert_exp_addr != 0) {
2011 bcmerror = dhdsdio_membytes(bus, false,
2012 sdpcm_shared.assert_exp_addr,
2013 (u8 *) str, maxstrlen);
2017 str[maxstrlen - 1] = '\0';
2018 bcm_bprintf(&strbuf, " expr \"%s\"", str);
2021 if (sdpcm_shared.assert_file_addr != 0) {
2023 bcmerror = dhdsdio_membytes(bus, false,
2024 sdpcm_shared.assert_file_addr,
2025 (u8 *) str, maxstrlen);
2029 str[maxstrlen - 1] = '\0';
2030 bcm_bprintf(&strbuf, " file \"%s\"", str);
2033 bcm_bprintf(&strbuf, " line %d ",
2034 sdpcm_shared.assert_line);
2037 if (sdpcm_shared.flags & SDPCM_SHARED_TRAP) {
2038 bcmerror = dhdsdio_membytes(bus, false,
2039 sdpcm_shared.trap_addr, (u8 *)&tr,
2044 bcm_bprintf(&strbuf,
2045 "Dongle trap type 0x%x @ epc 0x%x, cpsr 0x%x, spsr 0x%x, sp 0x%x,"
2046 "lp 0x%x, rpc 0x%x Trap offset 0x%x, "
2047 "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",
2048 tr.type, tr.epc, tr.cpsr, tr.spsr, tr.r13,
2049 tr.r14, tr.pc, sdpcm_shared.trap_addr,
2050 tr.r0, tr.r1, tr.r2, tr.r3, tr.r4, tr.r5,
2055 if (sdpcm_shared.flags & (SDPCM_SHARED_ASSERT | SDPCM_SHARED_TRAP))
2056 DHD_ERROR(("%s: %s\n", __func__, strbuf.origbuf));
2059 if (sdpcm_shared.flags & SDPCM_SHARED_TRAP) {
2060 /* Mem dump to a file on device */
2061 dhdsdio_mem_dump(bus);
2063 #endif /* DHD_DEBUG */
2072 static int dhdsdio_mem_dump(dhd_bus_t *bus)
2075 int size; /* Full mem size */
2076 int start = 0; /* Start address */
2077 int read_size = 0; /* Read size of each iteration */
2078 u8 *buf = NULL, *databuf = NULL;
2080 /* Get full mem size */
2081 size = bus->ramsize;
2082 buf = kmalloc(size, GFP_ATOMIC);
2084 DHD_ERROR(("%s: Out of memory (%d bytes)\n", __func__, size));
2088 /* Read mem content */
2089 printk(KERN_DEBUG "Dump dongle memory");
2092 read_size = min(MEMBLOCK, size);
2093 ret = dhdsdio_membytes(bus, false, start, databuf, read_size);
2095 DHD_ERROR(("%s: Error membytes %d\n", __func__, ret));
2101 /* Decrement size and increment start address */
2104 databuf += read_size;
2106 printk(KERN_DEBUG "Done\n");
2108 /* free buf before return !!! */
2109 if (write_to_file(bus->dhd, buf, bus->ramsize)) {
2110 DHD_ERROR(("%s: Error writing to files\n", __func__));
2114 /* buf free handled in write_to_file, not here */
2118 #define CONSOLE_LINE_MAX 192
2120 static int dhdsdio_readconsole(dhd_bus_t *bus)
2122 dhd_console_t *c = &bus->console;
2123 u8 line[CONSOLE_LINE_MAX], ch;
2127 /* Don't do anything until FWREADY updates console address */
2128 if (bus->console_addr == 0)
2131 /* Read console log struct */
2132 addr = bus->console_addr + offsetof(rte_cons_t, log);
2133 rv = dhdsdio_membytes(bus, false, addr, (u8 *)&c->log,
2138 /* Allocate console buffer (one time only) */
2139 if (c->buf == NULL) {
2140 c->bufsize = le32_to_cpu(c->log.buf_size);
2141 c->buf = kmalloc(c->bufsize, GFP_ATOMIC);
2146 idx = le32_to_cpu(c->log.idx);
2148 /* Protect against corrupt value */
2149 if (idx > c->bufsize)
2152 /* Skip reading the console buffer if the index pointer
2157 /* Read the console buffer */
2158 addr = le32_to_cpu(c->log.buf);
2159 rv = dhdsdio_membytes(bus, false, addr, c->buf, c->bufsize);
2163 while (c->last != idx) {
2164 for (n = 0; n < CONSOLE_LINE_MAX - 2; n++) {
2165 if (c->last == idx) {
2166 /* This would output a partial line.
2168 * the buffer pointer and output this
2169 * line next time around.
2174 c->last = c->bufsize - n;
2177 ch = c->buf[c->last];
2178 c->last = (c->last + 1) % c->bufsize;
2185 if (line[n - 1] == '\r')
2188 printk(KERN_DEBUG "CONSOLE: %s\n", line);
2195 #endif /* DHD_DEBUG */
2197 int dhdsdio_downloadvars(dhd_bus_t *bus, void *arg, int len)
2201 DHD_TRACE(("%s: Enter\n", __func__));
2203 /* Basic sanity checks */
2205 bcmerror = -EISCONN;
2209 bcmerror = -EOVERFLOW;
2213 /* Free the old ones and replace with passed variables */
2216 bus->vars = kmalloc(len, GFP_ATOMIC);
2217 bus->varsz = bus->vars ? len : 0;
2218 if (bus->vars == NULL) {
2223 /* Copy the passed variables, which should include the
2224 terminating double-null */
2225 memcpy(bus->vars, arg, bus->varsz);
2231 dhdsdio_doiovar(dhd_bus_t *bus, const bcm_iovar_t *vi, u32 actionid,
2232 const char *name, void *params, int plen, void *arg, int len,
2239 DHD_TRACE(("%s: Enter, action %d name %s params %p plen %d arg %p "
2240 "len %d val_size %d\n",
2241 __func__, actionid, name, params, plen, arg, len, val_size));
2243 bcmerror = bcm_iovar_lencheck(vi, arg, len, IOV_ISSET(actionid));
2247 if (plen >= (int)sizeof(int_val))
2248 memcpy(&int_val, params, sizeof(int_val));
2250 bool_val = (int_val != 0) ? true : false;
2252 /* Some ioctls use the bus */
2253 dhd_os_sdlock(bus->dhd);
2255 /* Check if dongle is in reset. If so, only allow DEVRESET iovars */
2256 if (bus->dhd->dongle_reset && !(actionid == IOV_SVAL(IOV_DEVRESET) ||
2257 actionid == IOV_GVAL(IOV_DEVRESET))) {
2262 /* Handle sleep stuff before any clock mucking */
2263 if (vi->varid == IOV_SLEEP) {
2264 if (IOV_ISSET(actionid)) {
2265 bcmerror = dhdsdio_bussleep(bus, bool_val);
2267 int_val = (s32) bus->sleeping;
2268 memcpy(arg, &int_val, val_size);
2273 /* Request clock to allow SDIO accesses */
2274 if (!bus->dhd->dongle_reset) {
2276 dhdsdio_clkctl(bus, CLK_AVAIL, false);
2280 case IOV_GVAL(IOV_INTR):
2281 int_val = (s32) bus->intr;
2282 memcpy(arg, &int_val, val_size);
2285 case IOV_SVAL(IOV_INTR):
2286 bus->intr = bool_val;
2287 bus->intdis = false;
2290 DHD_INTR(("%s: enable SDIO device interrupts\n",
2292 bcmsdh_intr_enable(bus->sdh);
2294 DHD_INTR(("%s: disable SDIO interrupts\n",
2296 bcmsdh_intr_disable(bus->sdh);
2301 case IOV_GVAL(IOV_POLLRATE):
2302 int_val = (s32) bus->pollrate;
2303 memcpy(arg, &int_val, val_size);
2306 case IOV_SVAL(IOV_POLLRATE):
2307 bus->pollrate = (uint) int_val;
2308 bus->poll = (bus->pollrate != 0);
2311 case IOV_GVAL(IOV_IDLETIME):
2312 int_val = bus->idletime;
2313 memcpy(arg, &int_val, val_size);
2316 case IOV_SVAL(IOV_IDLETIME):
2317 if ((int_val < 0) && (int_val != DHD_IDLE_IMMEDIATE))
2320 bus->idletime = int_val;
2323 case IOV_GVAL(IOV_IDLECLOCK):
2324 int_val = (s32) bus->idleclock;
2325 memcpy(arg, &int_val, val_size);
2328 case IOV_SVAL(IOV_IDLECLOCK):
2329 bus->idleclock = int_val;
2332 case IOV_GVAL(IOV_SD1IDLE):
2333 int_val = (s32) sd1idle;
2334 memcpy(arg, &int_val, val_size);
2337 case IOV_SVAL(IOV_SD1IDLE):
2341 case IOV_SVAL(IOV_MEMBYTES):
2342 case IOV_GVAL(IOV_MEMBYTES):
2348 bool set = (actionid == IOV_SVAL(IOV_MEMBYTES));
2350 ASSERT(plen >= 2 * sizeof(int));
2352 address = (u32) int_val;
2353 memcpy(&int_val, (char *)params + sizeof(int_val),
2355 size = (uint) int_val;
2357 /* Do some validation */
2358 dsize = set ? plen - (2 * sizeof(int)) : len;
2360 DHD_ERROR(("%s: error on %s membytes, addr "
2361 "0x%08x size %d dsize %d\n",
2362 __func__, (set ? "set" : "get"),
2363 address, size, dsize));
2368 DHD_INFO(("%s: Request to %s %d bytes at address "
2370 __func__, (set ? "write" : "read"), size, address));
2372 /* If we know about SOCRAM, check for a fit */
2373 if ((bus->orig_ramsize) &&
2374 ((address > bus->orig_ramsize)
2375 || (address + size > bus->orig_ramsize))) {
2376 DHD_ERROR(("%s: ramsize 0x%08x doesn't have %d "
2377 "bytes at 0x%08x\n",
2378 __func__, bus->orig_ramsize, size, address));
2383 /* Generate the actual data pointer */
2385 set ? (u8 *) params +
2386 2 * sizeof(int) : (u8 *) arg;
2388 /* Call to do the transfer */
2390 dhdsdio_membytes(bus, set, address, data, size);
2395 case IOV_GVAL(IOV_MEMSIZE):
2396 int_val = (s32) bus->ramsize;
2397 memcpy(arg, &int_val, val_size);
2400 case IOV_GVAL(IOV_SDIOD_DRIVE):
2401 int_val = (s32) dhd_sdiod_drive_strength;
2402 memcpy(arg, &int_val, val_size);
2405 case IOV_SVAL(IOV_SDIOD_DRIVE):
2406 dhd_sdiod_drive_strength = int_val;
2407 dhdsdio_sdiod_drive_strength_init(bus,
2408 dhd_sdiod_drive_strength);
2411 case IOV_SVAL(IOV_DOWNLOAD):
2412 bcmerror = dhdsdio_download_state(bus, bool_val);
2415 case IOV_SVAL(IOV_VARS):
2416 bcmerror = dhdsdio_downloadvars(bus, arg, len);
2419 case IOV_GVAL(IOV_READAHEAD):
2420 int_val = (s32) dhd_readahead;
2421 memcpy(arg, &int_val, val_size);
2424 case IOV_SVAL(IOV_READAHEAD):
2425 if (bool_val && !dhd_readahead)
2427 dhd_readahead = bool_val;
2430 case IOV_GVAL(IOV_SDRXCHAIN):
2431 int_val = (s32) bus->use_rxchain;
2432 memcpy(arg, &int_val, val_size);
2435 case IOV_SVAL(IOV_SDRXCHAIN):
2436 if (bool_val && !bus->sd_rxchain)
2437 bcmerror = -ENOTSUPP;
2439 bus->use_rxchain = bool_val;
2441 case IOV_GVAL(IOV_ALIGNCTL):
2442 int_val = (s32) dhd_alignctl;
2443 memcpy(arg, &int_val, val_size);
2446 case IOV_SVAL(IOV_ALIGNCTL):
2447 dhd_alignctl = bool_val;
2450 case IOV_GVAL(IOV_SDALIGN):
2451 int_val = DHD_SDALIGN;
2452 memcpy(arg, &int_val, val_size);
2456 case IOV_GVAL(IOV_VARS):
2457 if (bus->varsz < (uint) len)
2458 memcpy(arg, bus->vars, bus->varsz);
2460 bcmerror = -EOVERFLOW;
2462 #endif /* DHD_DEBUG */
2465 case IOV_GVAL(IOV_SDREG):
2470 sd_ptr = (sdreg_t *) params;
2472 addr = (unsigned long)bus->regs + sd_ptr->offset;
2473 size = sd_ptr->func;
2474 int_val = (s32) bcmsdh_reg_read(bus->sdh, addr, size);
2475 if (bcmsdh_regfail(bus->sdh))
2477 memcpy(arg, &int_val, sizeof(s32));
2481 case IOV_SVAL(IOV_SDREG):
2486 sd_ptr = (sdreg_t *) params;
2488 addr = (unsigned long)bus->regs + sd_ptr->offset;
2489 size = sd_ptr->func;
2490 bcmsdh_reg_write(bus->sdh, addr, size, sd_ptr->value);
2491 if (bcmsdh_regfail(bus->sdh))
2496 /* Same as above, but offset is not backplane
2498 case IOV_GVAL(IOV_SBREG):
2503 memcpy(&sdreg, params, sizeof(sdreg));
2505 addr = SI_ENUM_BASE + sdreg.offset;
2507 int_val = (s32) bcmsdh_reg_read(bus->sdh, addr, size);
2508 if (bcmsdh_regfail(bus->sdh))
2510 memcpy(arg, &int_val, sizeof(s32));
2514 case IOV_SVAL(IOV_SBREG):
2519 memcpy(&sdreg, params, sizeof(sdreg));
2521 addr = SI_ENUM_BASE + sdreg.offset;
2523 bcmsdh_reg_write(bus->sdh, addr, size, sdreg.value);
2524 if (bcmsdh_regfail(bus->sdh))
2529 case IOV_GVAL(IOV_SDCIS):
2533 strcat(arg, "\nFunc 0\n");
2534 bcmsdh_cis_read(bus->sdh, 0x10,
2535 (u8 *) arg + strlen(arg),
2536 SBSDIO_CIS_SIZE_LIMIT);
2537 strcat(arg, "\nFunc 1\n");
2538 bcmsdh_cis_read(bus->sdh, 0x11,
2539 (u8 *) arg + strlen(arg),
2540 SBSDIO_CIS_SIZE_LIMIT);
2541 strcat(arg, "\nFunc 2\n");
2542 bcmsdh_cis_read(bus->sdh, 0x12,
2543 (u8 *) arg + strlen(arg),
2544 SBSDIO_CIS_SIZE_LIMIT);
2548 case IOV_GVAL(IOV_FORCEEVEN):
2549 int_val = (s32) forcealign;
2550 memcpy(arg, &int_val, val_size);
2553 case IOV_SVAL(IOV_FORCEEVEN):
2554 forcealign = bool_val;
2557 case IOV_GVAL(IOV_TXBOUND):
2558 int_val = (s32) dhd_txbound;
2559 memcpy(arg, &int_val, val_size);
2562 case IOV_SVAL(IOV_TXBOUND):
2563 dhd_txbound = (uint) int_val;
2566 case IOV_GVAL(IOV_RXBOUND):
2567 int_val = (s32) dhd_rxbound;
2568 memcpy(arg, &int_val, val_size);
2571 case IOV_SVAL(IOV_RXBOUND):
2572 dhd_rxbound = (uint) int_val;
2575 case IOV_GVAL(IOV_TXMINMAX):
2576 int_val = (s32) dhd_txminmax;
2577 memcpy(arg, &int_val, val_size);
2580 case IOV_SVAL(IOV_TXMINMAX):
2581 dhd_txminmax = (uint) int_val;
2583 #endif /* DHD_DEBUG */
2586 case IOV_GVAL(IOV_EXTLOOP):
2587 int_val = (s32) bus->ext_loop;
2588 memcpy(arg, &int_val, val_size);
2591 case IOV_SVAL(IOV_EXTLOOP):
2592 bus->ext_loop = bool_val;
2595 case IOV_GVAL(IOV_PKTGEN):
2596 bcmerror = dhdsdio_pktgen_get(bus, arg);
2599 case IOV_SVAL(IOV_PKTGEN):
2600 bcmerror = dhdsdio_pktgen_set(bus, arg);
2604 case IOV_SVAL(IOV_DEVRESET):
2605 DHD_TRACE(("%s: Called set IOV_DEVRESET=%d dongle_reset=%d "
2607 __func__, bool_val, bus->dhd->dongle_reset,
2608 bus->dhd->busstate));
2610 dhd_bus_devreset(bus->dhd, (u8) bool_val);
2614 case IOV_GVAL(IOV_DEVRESET):
2615 DHD_TRACE(("%s: Called get IOV_DEVRESET\n", __func__));
2617 /* Get its status */
2618 int_val = (bool) bus->dhd->dongle_reset;
2619 memcpy(arg, &int_val, val_size);
2624 bcmerror = -ENOTSUPP;
2629 if ((bus->idletime == DHD_IDLE_IMMEDIATE) && !bus->dpc_sched) {
2630 bus->activity = false;
2631 dhdsdio_clkctl(bus, CLK_NONE, true);
2634 dhd_os_sdunlock(bus->dhd);
2636 if (actionid == IOV_SVAL(IOV_DEVRESET) && bool_val == false)
2637 dhd_preinit_ioctls((dhd_pub_t *) bus->dhd);
2642 static int dhdsdio_write_vars(dhd_bus_t *bus)
2650 char *nvram_ularray;
2651 #endif /* DHD_DEBUG */
2653 /* Even if there are no vars are to be written, we still
2654 need to set the ramsize. */
2655 varsize = bus->varsz ? roundup(bus->varsz, 4) : 0;
2656 varaddr = (bus->ramsize - 4) - varsize;
2659 vbuffer = kzalloc(varsize, GFP_ATOMIC);
2663 memcpy(vbuffer, bus->vars, bus->varsz);
2665 /* Write the vars list */
2667 dhdsdio_membytes(bus, true, varaddr, vbuffer, varsize);
2669 /* Verify NVRAM bytes */
2670 DHD_INFO(("Compare NVRAM dl & ul; varsize=%d\n", varsize));
2671 nvram_ularray = kmalloc(varsize, GFP_ATOMIC);
2675 /* Upload image to verify downloaded contents. */
2676 memset(nvram_ularray, 0xaa, varsize);
2678 /* Read the vars list to temp buffer for comparison */
2680 dhdsdio_membytes(bus, false, varaddr, nvram_ularray,
2683 DHD_ERROR(("%s: error %d on reading %d nvram bytes at "
2684 "0x%08x\n", __func__, bcmerror, varsize, varaddr));
2686 /* Compare the org NVRAM with the one read from RAM */
2687 if (memcmp(vbuffer, nvram_ularray, varsize)) {
2688 DHD_ERROR(("%s: Downloaded NVRAM image is corrupted.\n",
2691 DHD_ERROR(("%s: Download/Upload/Compare of NVRAM ok.\n",
2694 kfree(nvram_ularray);
2695 #endif /* DHD_DEBUG */
2700 /* adjust to the user specified RAM */
2701 DHD_INFO(("Physical memory size: %d, usable memory size: %d\n",
2702 bus->orig_ramsize, bus->ramsize));
2703 DHD_INFO(("Vars are at %d, orig varsize is %d\n", varaddr, varsize));
2704 varsize = ((bus->orig_ramsize - 4) - varaddr);
2707 * Determine the length token:
2708 * Varsize, converted to words, in lower 16-bits, checksum
2714 varsizew = varsize / 4;
2715 varsizew = (~varsizew << 16) | (varsizew & 0x0000FFFF);
2716 varsizew = cpu_to_le32(varsizew);
2719 DHD_INFO(("New varsize is %d, length token=0x%08x\n", varsize,
2722 /* Write the length token to the last word */
2723 bcmerror = dhdsdio_membytes(bus, true, (bus->orig_ramsize - 4),
2724 (u8 *)&varsizew, 4);
2729 static int dhdsdio_download_state(dhd_bus_t *bus, bool enter)
2735 /* To enter download state, disable ARM and reset SOCRAM.
2736 * To exit download state, simply reset ARM (default is RAM boot).
2739 bus->alp_only = true;
2741 dhdsdio_chip_disablecore(bus->sdh, bus->ci->armcorebase);
2743 dhdsdio_chip_resetcore(bus->sdh, bus->ci->ramcorebase);
2745 /* Clear the top bit of memory */
2748 dhdsdio_membytes(bus, true, bus->ramsize - 4,
2752 regdata = bcmsdh_reg_read(bus->sdh,
2753 CORE_SB(bus->ci->ramcorebase, sbtmstatelow), 4);
2754 regdata &= (SBTML_RESET | SBTML_REJ_MASK |
2755 (SICF_CLOCK_EN << SBTML_SICF_SHIFT));
2756 if ((SICF_CLOCK_EN << SBTML_SICF_SHIFT) != regdata) {
2757 DHD_ERROR(("%s: SOCRAM core is down after reset?\n",
2763 bcmerror = dhdsdio_write_vars(bus);
2765 DHD_ERROR(("%s: no vars written to RAM\n", __func__));
2769 W_SDREG(0xFFFFFFFF, &bus->regs->intstatus, retries);
2771 dhdsdio_chip_resetcore(bus->sdh, bus->ci->armcorebase);
2773 /* Allow HT Clock now that the ARM is running. */
2774 bus->alp_only = false;
2776 bus->dhd->busstate = DHD_BUS_LOAD;
2783 dhd_bus_iovar_op(dhd_pub_t *dhdp, const char *name,
2784 void *params, int plen, void *arg, int len, bool set)
2786 dhd_bus_t *bus = dhdp->bus;
2787 const bcm_iovar_t *vi = NULL;
2792 DHD_TRACE(("%s: Enter\n", __func__));
2797 /* Get MUST have return space */
2798 ASSERT(set || (arg && len));
2800 /* Set does NOT take qualifiers */
2801 ASSERT(!set || (!params && !plen));
2803 /* Look up var locally; if not found pass to host driver */
2804 vi = bcm_iovar_lookup(dhdsdio_iovars, name);
2806 dhd_os_sdlock(bus->dhd);
2810 /* Turn on clock in case SD command needs backplane */
2811 dhdsdio_clkctl(bus, CLK_AVAIL, false);
2814 bcmsdh_iovar_op(bus->sdh, name, params, plen, arg, len,
2817 /* Similar check for blocksize change */
2818 if (set && strcmp(name, "sd_blocksize") == 0) {
2821 (bus->sdh, "sd_blocksize", &fnum, sizeof(s32),
2822 &bus->blocksize, sizeof(s32),
2825 DHD_ERROR(("%s: fail on %s get\n", __func__,
2828 DHD_INFO(("%s: noted %s update, value now %d\n",
2829 __func__, "sd_blocksize",
2833 bus->roundup = min(max_roundup, bus->blocksize);
2835 if ((bus->idletime == DHD_IDLE_IMMEDIATE) && !bus->dpc_sched) {
2836 bus->activity = false;
2837 dhdsdio_clkctl(bus, CLK_NONE, true);
2840 dhd_os_sdunlock(bus->dhd);
2844 DHD_CTL(("%s: %s %s, len %d plen %d\n", __func__,
2845 name, (set ? "set" : "get"), len, plen));
2847 /* set up 'params' pointer in case this is a set command so that
2848 * the convenience int and bool code can be common to set and get
2850 if (params == NULL) {
2855 if (vi->type == IOVT_VOID)
2857 else if (vi->type == IOVT_BUFFER)
2860 /* all other types are integer sized */
2861 val_size = sizeof(int);
2863 actionid = set ? IOV_SVAL(vi->varid) : IOV_GVAL(vi->varid);
2865 dhdsdio_doiovar(bus, vi, actionid, name, params, plen, arg, len,
2872 void dhd_bus_stop(struct dhd_bus *bus, bool enforce_mutex)
2874 u32 local_hostintmask;
2879 DHD_TRACE(("%s: Enter\n", __func__));
2882 dhd_os_sdlock(bus->dhd);
2886 /* Enable clock for device interrupts */
2887 dhdsdio_clkctl(bus, CLK_AVAIL, false);
2889 /* Disable and clear interrupts at the chip level also */
2890 W_SDREG(0, &bus->regs->hostintmask, retries);
2891 local_hostintmask = bus->hostintmask;
2892 bus->hostintmask = 0;
2894 /* Change our idea of bus state */
2895 bus->dhd->busstate = DHD_BUS_DOWN;
2897 /* Force clocks on backplane to be sure F2 interrupt propagates */
2899 bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
2902 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
2903 (saveclk | SBSDIO_FORCE_HT), &err);
2906 DHD_ERROR(("%s: Failed to force clock for F2: err %d\n",
2910 /* Turn off the bus (F2), free any pending packets */
2911 DHD_INTR(("%s: disable SDIO interrupts\n", __func__));
2912 bcmsdh_intr_disable(bus->sdh);
2913 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_0, SDIOD_CCCR_IOEN,
2914 SDIO_FUNC_ENABLE_1, NULL);
2916 /* Clear any pending interrupts now that F2 is disabled */
2917 W_SDREG(local_hostintmask, &bus->regs->intstatus, retries);
2919 /* Turn off the backplane clock (only) */
2920 dhdsdio_clkctl(bus, CLK_SDONLY, false);
2922 /* Clear the data packet queues */
2923 bcm_pktq_flush(&bus->txq, true, NULL, NULL);
2925 /* Clear any held glomming stuff */
2927 bcm_pkt_buf_free_skb(bus->glomd);
2930 bcm_pkt_buf_free_skb(bus->glom);
2932 bus->glom = bus->glomd = NULL;
2934 /* Clear rx control and wake any waiters */
2936 dhd_os_ioctl_resp_wake(bus->dhd);
2938 /* Reset some F2 state stuff */
2939 bus->rxskip = false;
2940 bus->tx_seq = bus->rx_seq = 0;
2943 dhd_os_sdunlock(bus->dhd);
2946 int dhd_bus_init(dhd_pub_t *dhdp, bool enforce_mutex)
2948 dhd_bus_t *bus = dhdp->bus;
2955 DHD_TRACE(("%s: Enter\n", __func__));
2962 dhd_os_sdlock(bus->dhd);
2964 /* Make sure backplane clock is on, needed to generate F2 interrupt */
2965 dhdsdio_clkctl(bus, CLK_AVAIL, false);
2966 if (bus->clkstate != CLK_AVAIL)
2969 /* Force clocks on backplane to be sure F2 interrupt propagates */
2971 bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
2974 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
2975 (saveclk | SBSDIO_FORCE_HT), &err);
2978 DHD_ERROR(("%s: Failed to force clock for F2: err %d\n",
2983 /* Enable function 2 (frame transfers) */
2984 W_SDREG((SDPCM_PROT_VERSION << SMB_DATA_VERSION_SHIFT),
2985 &bus->regs->tosbmailboxdata, retries);
2986 enable = (SDIO_FUNC_ENABLE_1 | SDIO_FUNC_ENABLE_2);
2988 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_0, SDIOD_CCCR_IOEN, enable, NULL);
2990 /* Give the dongle some time to do its thing and set IOR2 */
2991 dhd_timeout_start(&tmo, DHD_WAIT_F2RDY * 1000);
2994 while (ready != enable && !dhd_timeout_expired(&tmo))
2996 bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_0, SDIOD_CCCR_IORDY,
2999 DHD_INFO(("%s: enable 0x%02x, ready 0x%02x (waited %uus)\n",
3000 __func__, enable, ready, tmo.elapsed));
3002 /* If F2 successfully enabled, set core and enable interrupts */
3003 if (ready == enable) {
3004 /* Set up the interrupt mask and enable interrupts */
3005 bus->hostintmask = HOSTINTMASK;
3006 W_SDREG(bus->hostintmask,
3007 (unsigned int *)CORE_BUS_REG(bus->ci->buscorebase,
3008 hostintmask), retries);
3010 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_WATERMARK,
3011 (u8) watermark, &err);
3013 /* Set bus state according to enable result */
3014 dhdp->busstate = DHD_BUS_DATA;
3016 /* bcmsdh_intr_unmask(bus->sdh); */
3018 bus->intdis = false;
3020 DHD_INTR(("%s: enable SDIO device interrupts\n",
3022 bcmsdh_intr_enable(bus->sdh);
3024 DHD_INTR(("%s: disable SDIO interrupts\n", __func__));
3025 bcmsdh_intr_disable(bus->sdh);
3031 /* Disable F2 again */
3032 enable = SDIO_FUNC_ENABLE_1;
3033 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_0, SDIOD_CCCR_IOEN, enable,
3037 /* Restore previous clock setting */
3038 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
3041 /* If we didn't come up, turn off backplane clock */
3042 if (dhdp->busstate != DHD_BUS_DATA)
3043 dhdsdio_clkctl(bus, CLK_NONE, false);
3047 dhd_os_sdunlock(bus->dhd);
3052 static void dhdsdio_rxfail(dhd_bus_t *bus, bool abort, bool rtx)
3054 bcmsdh_info_t *sdh = bus->sdh;
3055 sdpcmd_regs_t *regs = bus->regs;
3061 DHD_ERROR(("%s: %sterminate frame%s\n", __func__,
3062 (abort ? "abort command, " : ""),
3063 (rtx ? ", send NAK" : "")));
3066 bcmsdh_abort(sdh, SDIO_FUNC_2);
3068 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_FRAMECTRL, SFC_RF_TERM,
3072 /* Wait until the packet has been flushed (device/FIFO stable) */
3073 for (lastrbc = retries = 0xffff; retries > 0; retries--) {
3074 hi = bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_RFRAMEBCHI,
3076 lo = bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_RFRAMEBCLO,
3078 bus->f1regdata += 2;
3080 if ((hi == 0) && (lo == 0))
3083 if ((hi > (lastrbc >> 8)) && (lo > (lastrbc & 0x00ff))) {
3084 DHD_ERROR(("%s: count growing: last 0x%04x now "
3086 __func__, lastrbc, ((hi << 8) + lo)));
3088 lastrbc = (hi << 8) + lo;
3092 DHD_ERROR(("%s: count never zeroed: last 0x%04x\n",
3093 __func__, lastrbc));
3095 DHD_INFO(("%s: flush took %d iterations\n", __func__,
3096 (0xffff - retries)));
3101 W_SDREG(SMB_NAK, ®s->tosbmailbox, retries);
3103 if (retries <= retry_limit)
3107 /* Clear partial in any case */
3110 /* If we can't reach the device, signal failure */
3111 if (err || bcmsdh_regfail(sdh))
3112 bus->dhd->busstate = DHD_BUS_DOWN;
3116 dhdsdio_read_control(dhd_bus_t *bus, u8 *hdr, uint len, uint doff)
3118 bcmsdh_info_t *sdh = bus->sdh;
3123 DHD_TRACE(("%s: Enter\n", __func__));
3125 /* Control data already received in aligned rxctl */
3126 if ((bus->bus == SPI_BUS) && (!bus->usebufpool))
3130 /* Set rxctl for frame (w/optional alignment) */
3131 bus->rxctl = bus->rxbuf;
3133 bus->rxctl += firstread;
3134 pad = ((unsigned long)bus->rxctl % DHD_SDALIGN);
3136 bus->rxctl += (DHD_SDALIGN - pad);
3137 bus->rxctl -= firstread;
3139 ASSERT(bus->rxctl >= bus->rxbuf);
3141 /* Copy the already-read portion over */
3142 memcpy(bus->rxctl, hdr, firstread);
3143 if (len <= firstread)
3146 /* Copy the full data pkt in gSPI case and process ioctl. */
3147 if (bus->bus == SPI_BUS) {
3148 memcpy(bus->rxctl, hdr, len);
3152 /* Raise rdlen to next SDIO block to avoid tail command */
3153 rdlen = len - firstread;
3154 if (bus->roundup && bus->blocksize && (rdlen > bus->blocksize)) {
3155 pad = bus->blocksize - (rdlen % bus->blocksize);
3156 if ((pad <= bus->roundup) && (pad < bus->blocksize) &&
3157 ((len + pad) < bus->dhd->maxctl))
3159 } else if (rdlen % DHD_SDALIGN) {
3160 rdlen += DHD_SDALIGN - (rdlen % DHD_SDALIGN);
3163 /* Satisfy length-alignment requirements */
3164 if (forcealign && (rdlen & (ALIGNMENT - 1)))
3165 rdlen = roundup(rdlen, ALIGNMENT);
3167 /* Drop if the read is too big or it exceeds our maximum */
3168 if ((rdlen + firstread) > bus->dhd->maxctl) {
3169 DHD_ERROR(("%s: %d-byte control read exceeds %d-byte buffer\n",
3170 __func__, rdlen, bus->dhd->maxctl));
3171 bus->dhd->rx_errors++;
3172 dhdsdio_rxfail(bus, false, false);
3176 if ((len - doff) > bus->dhd->maxctl) {
3177 DHD_ERROR(("%s: %d-byte ctl frame (%d-byte ctl data) exceeds "
3179 __func__, len, (len - doff), bus->dhd->maxctl));
3180 bus->dhd->rx_errors++;
3182 dhdsdio_rxfail(bus, false, false);
3186 /* Read remainder of frame body into the rxctl buffer */
3187 sdret = bcmsdh_recv_buf(bus, bcmsdh_cur_sbwad(sdh), SDIO_FUNC_2,
3188 F2SYNC, (bus->rxctl + firstread), rdlen,
3191 ASSERT(sdret != -BCME_PENDING);
3193 /* Control frame failures need retransmission */
3195 DHD_ERROR(("%s: read %d control bytes failed: %d\n",
3196 __func__, rdlen, sdret));
3197 bus->rxc_errors++; /* dhd.rx_ctlerrs is higher level */
3198 dhdsdio_rxfail(bus, true, true);
3205 if (DHD_BYTES_ON() && DHD_CTL_ON()) {
3206 printk(KERN_DEBUG "RxCtrl:\n");
3207 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, bus->rxctl, len);
3211 /* Point to valid data and indicate its length */
3213 bus->rxlen = len - doff;
3216 /* Awake any waiters */
3217 dhd_os_ioctl_resp_wake(bus->dhd);
3220 static u8 dhdsdio_rxglom(dhd_bus_t *bus, u8 rxseq)
3226 struct sk_buff *pfirst, *plast, *pnext, *save_pfirst;
3229 u8 chan, seq, doff, sfdoff;
3233 bool usechain = bus->use_rxchain;
3235 /* If packets, issue read(s) and send up packet chain */
3236 /* Return sequence numbers consumed? */
3238 DHD_TRACE(("dhdsdio_rxglom: start: glomd %p glom %p\n", bus->glomd,
3241 /* If there's a descriptor, generate the packet chain */
3243 dhd_os_sdlock_rxq(bus->dhd);
3245 pfirst = plast = pnext = NULL;
3246 dlen = (u16) (bus->glomd->len);
3247 dptr = bus->glomd->data;
3248 if (!dlen || (dlen & 1)) {
3249 DHD_ERROR(("%s: bad glomd len(%d), ignore descriptor\n",
3254 for (totlen = num = 0; dlen; num++) {
3255 /* Get (and move past) next length */
3256 sublen = get_unaligned_le16(dptr);
3257 dlen -= sizeof(u16);
3258 dptr += sizeof(u16);
3259 if ((sublen < SDPCM_HDRLEN) ||
3260 ((num == 0) && (sublen < (2 * SDPCM_HDRLEN)))) {
3261 DHD_ERROR(("%s: descriptor len %d bad: %d\n",
3262 __func__, num, sublen));
3266 if (sublen % DHD_SDALIGN) {
3267 DHD_ERROR(("%s: sublen %d not multiple of %d\n",
3268 __func__, sublen, DHD_SDALIGN));
3273 /* For last frame, adjust read len so total
3274 is a block multiple */
3277 (roundup(totlen, bus->blocksize) - totlen);
3278 totlen = roundup(totlen, bus->blocksize);
3281 /* Allocate/chain packet for next subframe */
3282 pnext = bcm_pkt_buf_get_skb(sublen + DHD_SDALIGN);
3283 if (pnext == NULL) {
3284 DHD_ERROR(("%s: bcm_pkt_buf_get_skb failed, "
3285 "num %d len %d\n", __func__,
3289 ASSERT(!(pnext->prev));
3292 pfirst = plast = pnext;
3295 plast->next = pnext;
3299 /* Adhere to start alignment requirements */
3300 PKTALIGN(pnext, sublen, DHD_SDALIGN);
3303 /* If all allocations succeeded, save packet chain
3306 DHD_GLOM(("%s: allocated %d-byte packet chain for %d "
3307 "subframes\n", __func__, totlen, num));
3308 if (DHD_GLOM_ON() && bus->nextlen) {
3309 if (totlen != bus->nextlen) {
3310 DHD_GLOM(("%s: glomdesc mismatch: nextlen %d glomdesc %d " "rxseq %d\n",
3311 __func__, bus->nextlen,
3316 pfirst = pnext = NULL;
3319 bcm_pkt_buf_free_skb(pfirst);
3324 /* Done with descriptor packet */
3325 bcm_pkt_buf_free_skb(bus->glomd);
3329 dhd_os_sdunlock_rxq(bus->dhd);
3332 /* Ok -- either we just generated a packet chain,
3333 or had one from before */
3335 if (DHD_GLOM_ON()) {
3336 DHD_GLOM(("%s: try superframe read, packet chain:\n",
3338 for (pnext = bus->glom; pnext; pnext = pnext->next) {
3339 DHD_GLOM((" %p: %p len 0x%04x (%d)\n",
3340 pnext, (u8 *) (pnext->data),
3341 pnext->len, pnext->len));
3346 dlen = (u16) bcm_pkttotlen(pfirst);
3348 /* Do an SDIO read for the superframe. Configurable iovar to
3349 * read directly into the chained packet, or allocate a large
3350 * packet and and copy into the chain.
3353 errcode = bcmsdh_recv_buf(bus,
3354 bcmsdh_cur_sbwad(bus->sdh), SDIO_FUNC_2,
3355 F2SYNC, (u8 *) pfirst->data, dlen,
3356 pfirst, NULL, NULL);
3357 } else if (bus->dataptr) {
3358 errcode = bcmsdh_recv_buf(bus,
3359 bcmsdh_cur_sbwad(bus->sdh), SDIO_FUNC_2,
3360 F2SYNC, bus->dataptr, dlen,
3362 sublen = (u16) bcm_pktfrombuf(pfirst, 0, dlen,
3364 if (sublen != dlen) {
3365 DHD_ERROR(("%s: FAILED TO COPY, dlen %d sublen %d\n",
3366 __func__, dlen, sublen));
3371 DHD_ERROR(("COULDN'T ALLOC %d-BYTE GLOM, FORCE FAILURE\n",
3376 ASSERT(errcode != -BCME_PENDING);
3378 /* On failure, kill the superframe, allow a couple retries */
3380 DHD_ERROR(("%s: glom read of %d bytes failed: %d\n",
3381 __func__, dlen, errcode));
3382 bus->dhd->rx_errors++;
3384 if (bus->glomerr++ < 3) {
3385 dhdsdio_rxfail(bus, true, true);
3388 dhdsdio_rxfail(bus, true, false);
3389 dhd_os_sdlock_rxq(bus->dhd);
3390 bcm_pkt_buf_free_skb(bus->glom);
3391 dhd_os_sdunlock_rxq(bus->dhd);
3398 if (DHD_GLOM_ON()) {
3399 printk(KERN_DEBUG "SUPERFRAME:\n");
3400 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
3401 pfirst->data, min_t(int, pfirst->len, 48));
3405 /* Validate the superframe header */
3406 dptr = (u8 *) (pfirst->data);
3407 sublen = get_unaligned_le16(dptr);
3408 check = get_unaligned_le16(dptr + sizeof(u16));
3410 chan = SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]);
3411 seq = SDPCM_PACKET_SEQUENCE(&dptr[SDPCM_FRAMETAG_LEN]);
3412 bus->nextlen = dptr[SDPCM_FRAMETAG_LEN + SDPCM_NEXTLEN_OFFSET];
3413 if ((bus->nextlen << 4) > MAX_RX_DATASZ) {
3414 DHD_INFO(("%s: nextlen too large (%d) seq %d\n",
3415 __func__, bus->nextlen, seq));
3418 doff = SDPCM_DOFFSET_VALUE(&dptr[SDPCM_FRAMETAG_LEN]);
3419 txmax = SDPCM_WINDOW_VALUE(&dptr[SDPCM_FRAMETAG_LEN]);
3422 if ((u16)~(sublen ^ check)) {
3423 DHD_ERROR(("%s (superframe): HW hdr error: len/check "
3424 "0x%04x/0x%04x\n", __func__, sublen, check));
3426 } else if (roundup(sublen, bus->blocksize) != dlen) {
3427 DHD_ERROR(("%s (superframe): len 0x%04x, rounded "
3428 "0x%04x, expect 0x%04x\n",
3430 roundup(sublen, bus->blocksize), dlen));
3432 } else if (SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]) !=
3433 SDPCM_GLOM_CHANNEL) {
3434 DHD_ERROR(("%s (superframe): bad channel %d\n",
3436 SDPCM_PACKET_CHANNEL(&dptr
3437 [SDPCM_FRAMETAG_LEN])));
3439 } else if (SDPCM_GLOMDESC(&dptr[SDPCM_FRAMETAG_LEN])) {
3440 DHD_ERROR(("%s (superframe): got second descriptor?\n",
3443 } else if ((doff < SDPCM_HDRLEN) ||
3444 (doff > (pfirst->len - SDPCM_HDRLEN))) {
3445 DHD_ERROR(("%s (superframe): Bad data offset %d: HW %d "
3447 __func__, doff, sublen,
3448 pfirst->len, SDPCM_HDRLEN));
3452 /* Check sequence number of superframe SW header */
3454 DHD_INFO(("%s: (superframe) rx_seq %d, expected %d\n",
3455 __func__, seq, rxseq));
3460 /* Check window for sanity */
3461 if ((u8) (txmax - bus->tx_seq) > 0x40) {
3462 DHD_ERROR(("%s: unlikely tx max %d with tx_seq %d\n",
3463 __func__, txmax, bus->tx_seq));
3464 txmax = bus->tx_seq + 2;
3466 bus->tx_max = txmax;
3468 /* Remove superframe header, remember offset */
3469 skb_pull(pfirst, doff);
3472 /* Validate all the subframe headers */
3473 for (num = 0, pnext = pfirst; pnext && !errcode;
3474 num++, pnext = pnext->next) {
3475 dptr = (u8 *) (pnext->data);
3476 dlen = (u16) (pnext->len);
3477 sublen = get_unaligned_le16(dptr);
3478 check = get_unaligned_le16(dptr + sizeof(u16));
3479 chan = SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]);
3480 doff = SDPCM_DOFFSET_VALUE(&dptr[SDPCM_FRAMETAG_LEN]);
3482 if (DHD_GLOM_ON()) {
3483 printk(KERN_DEBUG "subframe:\n");
3484 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
3489 if ((u16)~(sublen ^ check)) {
3490 DHD_ERROR(("%s (subframe %d): HW hdr error: "
3491 "len/check 0x%04x/0x%04x\n",
3492 __func__, num, sublen, check));
3494 } else if ((sublen > dlen) || (sublen < SDPCM_HDRLEN)) {
3495 DHD_ERROR(("%s (subframe %d): length mismatch: "
3496 "len 0x%04x, expect 0x%04x\n",
3497 __func__, num, sublen, dlen));
3499 } else if ((chan != SDPCM_DATA_CHANNEL) &&
3500 (chan != SDPCM_EVENT_CHANNEL)) {
3501 DHD_ERROR(("%s (subframe %d): bad channel %d\n",
3502 __func__, num, chan));
3504 } else if ((doff < SDPCM_HDRLEN) || (doff > sublen)) {
3505 DHD_ERROR(("%s (subframe %d): Bad data offset %d: HW %d min %d\n",
3506 __func__, num, doff, sublen,
3513 /* Terminate frame on error, request
3515 if (bus->glomerr++ < 3) {
3516 /* Restore superframe header space */
3517 skb_push(pfirst, sfdoff);
3518 dhdsdio_rxfail(bus, true, true);
3521 dhdsdio_rxfail(bus, true, false);
3522 dhd_os_sdlock_rxq(bus->dhd);
3523 bcm_pkt_buf_free_skb(bus->glom);
3524 dhd_os_sdunlock_rxq(bus->dhd);
3532 /* Basic SD framing looks ok - process each packet (header) */
3533 save_pfirst = pfirst;
3537 dhd_os_sdlock_rxq(bus->dhd);
3538 for (num = 0; pfirst; rxseq++, pfirst = pnext) {
3539 pnext = pfirst->next;
3540 pfirst->next = NULL;
3542 dptr = (u8 *) (pfirst->data);
3543 sublen = get_unaligned_le16(dptr);
3544 chan = SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]);
3545 seq = SDPCM_PACKET_SEQUENCE(&dptr[SDPCM_FRAMETAG_LEN]);
3546 doff = SDPCM_DOFFSET_VALUE(&dptr[SDPCM_FRAMETAG_LEN]);
3548 DHD_GLOM(("%s: Get subframe %d, %p(%p/%d), sublen %d "
3550 __func__, num, pfirst, pfirst->data,
3551 pfirst->len, sublen, chan, seq));
3553 ASSERT((chan == SDPCM_DATA_CHANNEL)
3554 || (chan == SDPCM_EVENT_CHANNEL));
3557 DHD_GLOM(("%s: rx_seq %d, expected %d\n",
3558 __func__, seq, rxseq));
3563 if (DHD_BYTES_ON() && DHD_DATA_ON()) {
3564 printk(KERN_DEBUG "Rx Subframe Data:\n");
3565 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
3570 __skb_trim(pfirst, sublen);
3571 skb_pull(pfirst, doff);
3573 if (pfirst->len == 0) {
3574 bcm_pkt_buf_free_skb(pfirst);
3576 plast->next = pnext;
3578 ASSERT(save_pfirst == pfirst);
3579 save_pfirst = pnext;
3582 } else if (dhd_prot_hdrpull(bus->dhd, &ifidx, pfirst) !=
3584 DHD_ERROR(("%s: rx protocol error\n",
3586 bus->dhd->rx_errors++;
3587 bcm_pkt_buf_free_skb(pfirst);
3589 plast->next = pnext;
3591 ASSERT(save_pfirst == pfirst);
3592 save_pfirst = pnext;
3597 /* this packet will go up, link back into
3598 chain and count it */
3599 pfirst->next = pnext;
3604 if (DHD_GLOM_ON()) {
3605 DHD_GLOM(("%s subframe %d to stack, %p(%p/%d) "
3607 __func__, num, pfirst, pfirst->data,
3608 pfirst->len, pfirst->next,
3610 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
3612 min_t(int, pfirst->len, 32));
3614 #endif /* DHD_DEBUG */
3616 dhd_os_sdunlock_rxq(bus->dhd);
3618 dhd_os_sdunlock(bus->dhd);
3619 dhd_rx_frame(bus->dhd, ifidx, save_pfirst, num);
3620 dhd_os_sdlock(bus->dhd);
3623 bus->rxglomframes++;
3624 bus->rxglompkts += num;
3629 /* Return true if there may be more frames to read */
3630 static uint dhdsdio_readframes(dhd_bus_t *bus, uint maxframes, bool *finished)
3632 bcmsdh_info_t *sdh = bus->sdh;
3634 u16 len, check; /* Extracted hardware header fields */
3635 u8 chan, seq, doff; /* Extracted software header fields */
3636 u8 fcbits; /* Extracted fcbits from software header */
3638 struct sk_buff *pkt; /* Packet for event or data frames */
3639 u16 pad; /* Number of pad bytes to read */
3640 u16 rdlen; /* Total number of bytes to read */
3641 u8 rxseq; /* Next sequence number to expect */
3642 uint rxleft = 0; /* Remaining number of frames allowed */
3643 int sdret; /* Return code from bcmsdh calls */
3644 u8 txmax; /* Maximum tx sequence offered */
3645 bool len_consistent; /* Result of comparing readahead len and
3649 uint rxcount = 0; /* Total frames read */
3651 #if defined(DHD_DEBUG) || defined(SDTEST)
3652 bool sdtest = false; /* To limit message spew from test mode */
3655 DHD_TRACE(("%s: Enter\n", __func__));
3660 /* Allow pktgen to override maxframes */
3661 if (bus->pktgen_count && (bus->pktgen_mode == DHD_PKTGEN_RECV)) {
3662 maxframes = bus->pktgen_count;
3667 /* Not finished unless we encounter no more frames indication */
3670 for (rxseq = bus->rx_seq, rxleft = maxframes;
3671 !bus->rxskip && rxleft && bus->dhd->busstate != DHD_BUS_DOWN;
3672 rxseq++, rxleft--) {
3674 /* Handle glomming separately */
3675 if (bus->glom || bus->glomd) {
3677 DHD_GLOM(("%s: calling rxglom: glomd %p, glom %p\n",
3678 __func__, bus->glomd, bus->glom));
3679 cnt = dhdsdio_rxglom(bus, rxseq);
3680 DHD_GLOM(("%s: rxglom returned %d\n", __func__, cnt));
3682 rxleft = (rxleft > cnt) ? (rxleft - cnt) : 1;
3686 /* Try doing single read if we can */
3687 if (dhd_readahead && bus->nextlen) {
3688 u16 nextlen = bus->nextlen;
3691 if (bus->bus == SPI_BUS) {
3692 rdlen = len = nextlen;
3694 rdlen = len = nextlen << 4;
3696 /* Pad read to blocksize for efficiency */
3697 if (bus->roundup && bus->blocksize
3698 && (rdlen > bus->blocksize)) {
3701 (rdlen % bus->blocksize);
3702 if ((pad <= bus->roundup)
3703 && (pad < bus->blocksize)
3704 && ((rdlen + pad + firstread) <
3707 } else if (rdlen % DHD_SDALIGN) {
3709 DHD_SDALIGN - (rdlen % DHD_SDALIGN);
3713 /* We use bus->rxctl buffer in WinXP for initial
3714 * control pkt receives.
3715 * Later we use buffer-poll for data as well
3716 * as control packets.
3717 * This is required because dhd receives full
3718 * frame in gSPI unlike SDIO.
3719 * After the frame is received we have to
3720 * distinguish whether it is data
3721 * or non-data frame.
3723 /* Allocate a packet buffer */
3724 dhd_os_sdlock_rxq(bus->dhd);
3725 pkt = bcm_pkt_buf_get_skb(rdlen + DHD_SDALIGN);
3727 if (bus->bus == SPI_BUS) {
3728 bus->usebufpool = false;
3729 bus->rxctl = bus->rxbuf;
3731 bus->rxctl += firstread;
3732 pad = ((unsigned long)bus->rxctl %
3736 (DHD_SDALIGN - pad);
3737 bus->rxctl -= firstread;
3739 ASSERT(bus->rxctl >= bus->rxbuf);
3741 /* Read the entire frame */
3742 sdret = bcmsdh_recv_buf(bus,
3743 bcmsdh_cur_sbwad(sdh),
3744 SDIO_FUNC_2, F2SYNC,
3748 ASSERT(sdret != -BCME_PENDING);
3750 /* Control frame failures need
3753 DHD_ERROR(("%s: read %d control bytes failed: %d\n",
3756 /* dhd.rx_ctlerrs is higher */
3758 dhd_os_sdunlock_rxq(bus->dhd);
3759 dhdsdio_rxfail(bus, true,
3767 request rtx of events */
3768 DHD_ERROR(("%s (nextlen): "
3769 "bcm_pkt_buf_get_skb failed:"
3770 " len %d rdlen %d expected"
3771 " rxseq %d\n", __func__,
3772 len, rdlen, rxseq));
3773 /* Just go try again w/normal
3775 dhd_os_sdunlock_rxq(bus->dhd);
3779 if (bus->bus == SPI_BUS)
3780 bus->usebufpool = true;
3782 ASSERT(!(pkt->prev));
3783 PKTALIGN(pkt, rdlen, DHD_SDALIGN);
3784 rxbuf = (u8 *) (pkt->data);
3785 /* Read the entire frame */
3786 sdret = bcmsdh_recv_buf(bus,
3787 bcmsdh_cur_sbwad(sdh),
3788 SDIO_FUNC_2, F2SYNC,
3792 ASSERT(sdret != -BCME_PENDING);
3795 DHD_ERROR(("%s (nextlen): read %d bytes failed: %d\n",
3796 __func__, rdlen, sdret));
3797 bcm_pkt_buf_free_skb(pkt);
3798 bus->dhd->rx_errors++;
3799 dhd_os_sdunlock_rxq(bus->dhd);
3800 /* Force retry w/normal header read.
3801 * Don't attempt NAK for
3804 dhdsdio_rxfail(bus, true,
3811 dhd_os_sdunlock_rxq(bus->dhd);
3813 /* Now check the header */
3814 memcpy(bus->rxhdr, rxbuf, SDPCM_HDRLEN);
3816 /* Extract hardware header fields */
3817 len = get_unaligned_le16(bus->rxhdr);
3818 check = get_unaligned_le16(bus->rxhdr + sizeof(u16));
3820 /* All zeros means readahead info was bad */
3821 if (!(len | check)) {
3822 DHD_INFO(("%s (nextlen): read zeros in HW "
3823 "header???\n", __func__));
3824 dhdsdio_pktfree2(bus, pkt);
3828 /* Validate check bytes */
3829 if ((u16)~(len ^ check)) {
3830 DHD_ERROR(("%s (nextlen): HW hdr error:"
3831 " nextlen/len/check"
3832 " 0x%04x/0x%04x/0x%04x\n",
3833 __func__, nextlen, len, check));
3835 dhdsdio_rxfail(bus, false, false);
3836 dhdsdio_pktfree2(bus, pkt);
3840 /* Validate frame length */
3841 if (len < SDPCM_HDRLEN) {
3842 DHD_ERROR(("%s (nextlen): HW hdr length "
3843 "invalid: %d\n", __func__, len));
3844 dhdsdio_pktfree2(bus, pkt);
3848 /* Check for consistency withreadahead info */
3849 len_consistent = (nextlen != (roundup(len, 16) >> 4));
3850 if (len_consistent) {
3851 /* Mismatch, force retry w/normal
3852 header (may be >4K) */
3853 DHD_ERROR(("%s (nextlen): mismatch, "
3854 "nextlen %d len %d rnd %d; "
3855 "expected rxseq %d\n",
3857 len, roundup(len, 16), rxseq));
3858 dhdsdio_rxfail(bus, true, (bus->bus != SPI_BUS));
3859 dhdsdio_pktfree2(bus, pkt);
3863 /* Extract software header fields */
3864 chan = SDPCM_PACKET_CHANNEL(
3865 &bus->rxhdr[SDPCM_FRAMETAG_LEN]);
3866 seq = SDPCM_PACKET_SEQUENCE(
3867 &bus->rxhdr[SDPCM_FRAMETAG_LEN]);
3868 doff = SDPCM_DOFFSET_VALUE(
3869 &bus->rxhdr[SDPCM_FRAMETAG_LEN]);
3870 txmax = SDPCM_WINDOW_VALUE(
3871 &bus->rxhdr[SDPCM_FRAMETAG_LEN]);
3874 bus->rxhdr[SDPCM_FRAMETAG_LEN +
3875 SDPCM_NEXTLEN_OFFSET];
3876 if ((bus->nextlen << 4) > MAX_RX_DATASZ) {
3877 DHD_INFO(("%s (nextlen): got frame w/nextlen too large" " (%d), seq %d\n",
3878 __func__, bus->nextlen, seq));
3882 bus->dhd->rx_readahead_cnt++;
3884 /* Handle Flow Control */
3885 fcbits = SDPCM_FCMASK_VALUE(
3886 &bus->rxhdr[SDPCM_FRAMETAG_LEN]);
3888 if (bus->flowcontrol != fcbits) {
3889 if (~bus->flowcontrol & fcbits)
3892 if (bus->flowcontrol & ~fcbits)
3896 bus->flowcontrol = fcbits;
3899 /* Check and update sequence number */
3901 DHD_INFO(("%s (nextlen): rx_seq %d, expected "
3902 "%d\n", __func__, seq, rxseq));
3907 /* Check window for sanity */
3908 if ((u8) (txmax - bus->tx_seq) > 0x40) {
3909 DHD_ERROR(("%s: got unlikely tx max %d with "
3911 __func__, txmax, bus->tx_seq));
3912 txmax = bus->tx_seq + 2;
3914 bus->tx_max = txmax;
3917 if (DHD_BYTES_ON() && DHD_DATA_ON()) {
3918 printk(KERN_DEBUG "Rx Data:\n");
3919 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
3921 } else if (DHD_HDRS_ON()) {
3922 printk(KERN_DEBUG "RxHdr:\n");
3923 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
3924 bus->rxhdr, SDPCM_HDRLEN);
3928 if (chan == SDPCM_CONTROL_CHANNEL) {
3929 if (bus->bus == SPI_BUS) {
3930 dhdsdio_read_control(bus, rxbuf, len,
3933 DHD_ERROR(("%s (nextlen): readahead on control" " packet %d?\n",
3935 /* Force retry w/normal header read */
3937 dhdsdio_rxfail(bus, false, true);
3939 dhdsdio_pktfree2(bus, pkt);
3943 if ((bus->bus == SPI_BUS) && !bus->usebufpool) {
3944 DHD_ERROR(("Received %d bytes on %d channel. Running out of " "rx pktbuf's or not yet malloced.\n",
3949 /* Validate data offset */
3950 if ((doff < SDPCM_HDRLEN) || (doff > len)) {
3951 DHD_ERROR(("%s (nextlen): bad data offset %d: HW len %d min %d\n",
3952 __func__, doff, len, SDPCM_HDRLEN));
3953 dhdsdio_rxfail(bus, false, false);
3954 dhdsdio_pktfree2(bus, pkt);
3958 /* All done with this one -- now deliver the packet */
3961 /* gSPI frames should not be handled in fractions */
3962 if (bus->bus == SPI_BUS)
3965 /* Read frame header (hardware and software) */
3966 sdret = bcmsdh_recv_buf(bus, bcmsdh_cur_sbwad(sdh),
3967 SDIO_FUNC_2, F2SYNC, bus->rxhdr, firstread,
3970 ASSERT(sdret != -BCME_PENDING);
3973 DHD_ERROR(("%s: RXHEADER FAILED: %d\n", __func__,
3976 dhdsdio_rxfail(bus, true, true);
3980 if (DHD_BYTES_ON() || DHD_HDRS_ON()) {
3981 printk(KERN_DEBUG "RxHdr:\n");
3982 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
3983 bus->rxhdr, SDPCM_HDRLEN);
3987 /* Extract hardware header fields */
3988 len = get_unaligned_le16(bus->rxhdr);
3989 check = get_unaligned_le16(bus->rxhdr + sizeof(u16));
3991 /* All zeros means no more frames */
3992 if (!(len | check)) {
3997 /* Validate check bytes */
3998 if ((u16) ~(len ^ check)) {
3999 DHD_ERROR(("%s: HW hdr err: len/check 0x%04x/0x%04x\n",
4000 __func__, len, check));
4002 dhdsdio_rxfail(bus, false, false);
4006 /* Validate frame length */
4007 if (len < SDPCM_HDRLEN) {
4008 DHD_ERROR(("%s: HW hdr length invalid: %d\n",
4013 /* Extract software header fields */
4014 chan = SDPCM_PACKET_CHANNEL(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
4015 seq = SDPCM_PACKET_SEQUENCE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
4016 doff = SDPCM_DOFFSET_VALUE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
4017 txmax = SDPCM_WINDOW_VALUE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
4019 /* Validate data offset */
4020 if ((doff < SDPCM_HDRLEN) || (doff > len)) {
4021 DHD_ERROR(("%s: Bad data offset %d: HW len %d, min %d "
4023 __func__, doff, len, SDPCM_HDRLEN, seq));
4026 dhdsdio_rxfail(bus, false, false);
4030 /* Save the readahead length if there is one */
4032 bus->rxhdr[SDPCM_FRAMETAG_LEN + SDPCM_NEXTLEN_OFFSET];
4033 if ((bus->nextlen << 4) > MAX_RX_DATASZ) {
4034 DHD_INFO(("%s (nextlen): got frame w/nextlen too large "
4036 __func__, bus->nextlen, seq));
4040 /* Handle Flow Control */
4041 fcbits = SDPCM_FCMASK_VALUE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
4043 if (bus->flowcontrol != fcbits) {
4044 if (~bus->flowcontrol & fcbits)
4047 if (bus->flowcontrol & ~fcbits)
4051 bus->flowcontrol = fcbits;
4054 /* Check and update sequence number */
4056 DHD_INFO(("%s: rx_seq %d, expected %d\n", __func__,
4062 /* Check window for sanity */
4063 if ((u8) (txmax - bus->tx_seq) > 0x40) {
4064 DHD_ERROR(("%s: unlikely tx max %d with tx_seq %d\n",
4065 __func__, txmax, bus->tx_seq));
4066 txmax = bus->tx_seq + 2;
4068 bus->tx_max = txmax;
4070 /* Call a separate function for control frames */
4071 if (chan == SDPCM_CONTROL_CHANNEL) {
4072 dhdsdio_read_control(bus, bus->rxhdr, len, doff);
4076 ASSERT((chan == SDPCM_DATA_CHANNEL)
4077 || (chan == SDPCM_EVENT_CHANNEL)
4078 || (chan == SDPCM_TEST_CHANNEL)
4079 || (chan == SDPCM_GLOM_CHANNEL));
4081 /* Length to read */
4082 rdlen = (len > firstread) ? (len - firstread) : 0;
4084 /* May pad read to blocksize for efficiency */
4085 if (bus->roundup && bus->blocksize &&
4086 (rdlen > bus->blocksize)) {
4087 pad = bus->blocksize - (rdlen % bus->blocksize);
4088 if ((pad <= bus->roundup) && (pad < bus->blocksize) &&
4089 ((rdlen + pad + firstread) < MAX_RX_DATASZ))
4091 } else if (rdlen % DHD_SDALIGN) {
4092 rdlen += DHD_SDALIGN - (rdlen % DHD_SDALIGN);
4095 /* Satisfy length-alignment requirements */
4096 if (forcealign && (rdlen & (ALIGNMENT - 1)))
4097 rdlen = roundup(rdlen, ALIGNMENT);
4099 if ((rdlen + firstread) > MAX_RX_DATASZ) {
4100 /* Too long -- skip this frame */
4101 DHD_ERROR(("%s: too long: len %d rdlen %d\n",
4102 __func__, len, rdlen));
4103 bus->dhd->rx_errors++;
4105 dhdsdio_rxfail(bus, false, false);
4109 dhd_os_sdlock_rxq(bus->dhd);
4110 pkt = bcm_pkt_buf_get_skb(rdlen + firstread + DHD_SDALIGN);
4112 /* Give up on data, request rtx of events */
4113 DHD_ERROR(("%s: bcm_pkt_buf_get_skb failed: rdlen %d "
4114 "chan %d\n", __func__, rdlen, chan));
4115 bus->dhd->rx_dropped++;
4116 dhd_os_sdunlock_rxq(bus->dhd);
4117 dhdsdio_rxfail(bus, false, RETRYCHAN(chan));
4120 dhd_os_sdunlock_rxq(bus->dhd);
4122 ASSERT(!(pkt->prev));
4124 /* Leave room for what we already read, and align remainder */
4125 ASSERT(firstread < pkt->len);
4126 skb_pull(pkt, firstread);
4127 PKTALIGN(pkt, rdlen, DHD_SDALIGN);
4129 /* Read the remaining frame data */
4130 sdret = bcmsdh_recv_buf(bus, bcmsdh_cur_sbwad(sdh), SDIO_FUNC_2,
4131 F2SYNC, ((u8 *) (pkt->data)), rdlen,
4134 ASSERT(sdret != -BCME_PENDING);
4137 DHD_ERROR(("%s: read %d %s bytes failed: %d\n",
4140 SDPCM_EVENT_CHANNEL) ? "event" : ((chan ==
4142 ? "data" : "test")),
4144 dhd_os_sdlock_rxq(bus->dhd);
4145 bcm_pkt_buf_free_skb(pkt);
4146 dhd_os_sdunlock_rxq(bus->dhd);
4147 bus->dhd->rx_errors++;
4148 dhdsdio_rxfail(bus, true, RETRYCHAN(chan));
4152 /* Copy the already-read portion */
4153 skb_push(pkt, firstread);
4154 memcpy(pkt->data, bus->rxhdr, firstread);
4157 if (DHD_BYTES_ON() && DHD_DATA_ON()) {
4158 printk(KERN_DEBUG "Rx Data:\n");
4159 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
4165 /* Save superframe descriptor and allocate packet frame */
4166 if (chan == SDPCM_GLOM_CHANNEL) {
4167 if (SDPCM_GLOMDESC(&bus->rxhdr[SDPCM_FRAMETAG_LEN])) {
4168 DHD_GLOM(("%s: glom descriptor, %d bytes:\n",
4171 if (DHD_GLOM_ON()) {
4172 printk(KERN_DEBUG "Glom Data:\n");
4173 print_hex_dump_bytes("",
4178 __skb_trim(pkt, len);
4179 ASSERT(doff == SDPCM_HDRLEN);
4180 skb_pull(pkt, SDPCM_HDRLEN);
4183 DHD_ERROR(("%s: glom superframe w/o "
4184 "descriptor!\n", __func__));
4185 dhdsdio_rxfail(bus, false, false);
4190 /* Fill in packet len and prio, deliver upward */
4191 __skb_trim(pkt, len);
4192 skb_pull(pkt, doff);
4195 /* Test channel packets are processed separately */
4196 if (chan == SDPCM_TEST_CHANNEL) {
4197 dhdsdio_testrcv(bus, pkt, seq);
4202 if (pkt->len == 0) {
4203 dhd_os_sdlock_rxq(bus->dhd);
4204 bcm_pkt_buf_free_skb(pkt);
4205 dhd_os_sdunlock_rxq(bus->dhd);
4207 } else if (dhd_prot_hdrpull(bus->dhd, &ifidx, pkt) != 0) {
4208 DHD_ERROR(("%s: rx protocol error\n", __func__));
4209 dhd_os_sdlock_rxq(bus->dhd);
4210 bcm_pkt_buf_free_skb(pkt);
4211 dhd_os_sdunlock_rxq(bus->dhd);
4212 bus->dhd->rx_errors++;
4216 /* Unlock during rx call */
4217 dhd_os_sdunlock(bus->dhd);
4218 dhd_rx_frame(bus->dhd, ifidx, pkt, 1);
4219 dhd_os_sdlock(bus->dhd);
4221 rxcount = maxframes - rxleft;
4223 /* Message if we hit the limit */
4224 if (!rxleft && !sdtest)
4225 DHD_DATA(("%s: hit rx limit of %d frames\n", __func__,
4228 #endif /* DHD_DEBUG */
4229 DHD_DATA(("%s: processed %d frames\n", __func__, rxcount));
4230 /* Back off rxseq if awaiting rtx, update rx_seq */
4233 bus->rx_seq = rxseq;
4238 static u32 dhdsdio_hostmail(dhd_bus_t *bus)
4240 sdpcmd_regs_t *regs = bus->regs;
4246 DHD_TRACE(("%s: Enter\n", __func__));
4248 /* Read mailbox data and ack that we did so */
4249 R_SDREG(hmb_data, ®s->tohostmailboxdata, retries);
4250 if (retries <= retry_limit)
4251 W_SDREG(SMB_INT_ACK, ®s->tosbmailbox, retries);
4252 bus->f1regdata += 2;
4254 /* Dongle recomposed rx frames, accept them again */
4255 if (hmb_data & HMB_DATA_NAKHANDLED) {
4256 DHD_INFO(("Dongle reports NAK handled, expect rtx of %d\n",
4259 DHD_ERROR(("%s: unexpected NAKHANDLED!\n", __func__));
4261 bus->rxskip = false;
4262 intstatus |= I_HMB_FRAME_IND;
4266 * DEVREADY does not occur with gSPI.
4268 if (hmb_data & (HMB_DATA_DEVREADY | HMB_DATA_FWREADY)) {
4270 (hmb_data & HMB_DATA_VERSION_MASK) >>
4271 HMB_DATA_VERSION_SHIFT;
4272 if (bus->sdpcm_ver != SDPCM_PROT_VERSION)
4273 DHD_ERROR(("Version mismatch, dongle reports %d, "
4275 bus->sdpcm_ver, SDPCM_PROT_VERSION));
4277 DHD_INFO(("Dongle ready, protocol version %d\n",
4282 * Flow Control has been moved into the RX headers and this out of band
4283 * method isn't used any more.
4284 * remaining backward compatible with older dongles.
4286 if (hmb_data & HMB_DATA_FC) {
4287 fcbits = (hmb_data & HMB_DATA_FCDATA_MASK) >>
4288 HMB_DATA_FCDATA_SHIFT;
4290 if (fcbits & ~bus->flowcontrol)
4293 if (bus->flowcontrol & ~fcbits)
4297 bus->flowcontrol = fcbits;
4300 /* Shouldn't be any others */
4301 if (hmb_data & ~(HMB_DATA_DEVREADY |
4302 HMB_DATA_NAKHANDLED |
4305 HMB_DATA_FCDATA_MASK | HMB_DATA_VERSION_MASK)) {
4306 DHD_ERROR(("Unknown mailbox data content: 0x%02x\n", hmb_data));
4312 bool dhdsdio_dpc(dhd_bus_t *bus)
4314 bcmsdh_info_t *sdh = bus->sdh;
4315 sdpcmd_regs_t *regs = bus->regs;
4316 u32 intstatus, newstatus = 0;
4318 uint rxlimit = dhd_rxbound; /* Rx frames to read before resched */
4319 uint txlimit = dhd_txbound; /* Tx frames to send before resched */
4320 uint framecnt = 0; /* Temporary counter of tx/rx frames */
4321 bool rxdone = true; /* Flag for no more read data */
4322 bool resched = false; /* Flag indicating resched wanted */
4324 DHD_TRACE(("%s: Enter\n", __func__));
4326 /* Start with leftover status bits */
4327 intstatus = bus->intstatus;
4329 dhd_os_sdlock(bus->dhd);
4331 /* If waiting for HTAVAIL, check status */
4332 if (bus->clkstate == CLK_PENDING) {
4334 u8 clkctl, devctl = 0;
4337 /* Check for inconsistent device control */
4339 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL, &err);
4341 DHD_ERROR(("%s: error reading DEVCTL: %d\n",
4343 bus->dhd->busstate = DHD_BUS_DOWN;
4345 ASSERT(devctl & SBSDIO_DEVCTL_CA_INT_ONLY);
4347 #endif /* DHD_DEBUG */
4349 /* Read CSR, if clock on switch to AVAIL, else ignore */
4351 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
4354 DHD_ERROR(("%s: error reading CSR: %d\n", __func__,
4356 bus->dhd->busstate = DHD_BUS_DOWN;
4359 DHD_INFO(("DPC: PENDING, devctl 0x%02x clkctl 0x%02x\n", devctl,
4362 if (SBSDIO_HTAV(clkctl)) {
4364 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
4367 DHD_ERROR(("%s: error reading DEVCTL: %d\n",
4369 bus->dhd->busstate = DHD_BUS_DOWN;
4371 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
4372 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
4375 DHD_ERROR(("%s: error writing DEVCTL: %d\n",
4377 bus->dhd->busstate = DHD_BUS_DOWN;
4379 bus->clkstate = CLK_AVAIL;
4387 /* Make sure backplane clock is on */
4388 dhdsdio_clkctl(bus, CLK_AVAIL, true);
4389 if (bus->clkstate == CLK_PENDING)
4392 /* Pending interrupt indicates new device status */
4395 R_SDREG(newstatus, ®s->intstatus, retries);
4397 if (bcmsdh_regfail(bus->sdh))
4399 newstatus &= bus->hostintmask;
4400 bus->fcstate = !!(newstatus & I_HMB_FC_STATE);
4402 W_SDREG(newstatus, ®s->intstatus, retries);
4407 /* Merge new bits with previous */
4408 intstatus |= newstatus;
4411 /* Handle flow-control change: read new state in case our ack
4412 * crossed another change interrupt. If change still set, assume
4413 * FC ON for safety, let next loop through do the debounce.
4415 if (intstatus & I_HMB_FC_CHANGE) {
4416 intstatus &= ~I_HMB_FC_CHANGE;
4417 W_SDREG(I_HMB_FC_CHANGE, ®s->intstatus, retries);
4418 R_SDREG(newstatus, ®s->intstatus, retries);
4419 bus->f1regdata += 2;
4421 !!(newstatus & (I_HMB_FC_STATE | I_HMB_FC_CHANGE));
4422 intstatus |= (newstatus & bus->hostintmask);
4425 /* Handle host mailbox indication */
4426 if (intstatus & I_HMB_HOST_INT) {
4427 intstatus &= ~I_HMB_HOST_INT;
4428 intstatus |= dhdsdio_hostmail(bus);
4431 /* Generally don't ask for these, can get CRC errors... */
4432 if (intstatus & I_WR_OOSYNC) {
4433 DHD_ERROR(("Dongle reports WR_OOSYNC\n"));
4434 intstatus &= ~I_WR_OOSYNC;
4437 if (intstatus & I_RD_OOSYNC) {
4438 DHD_ERROR(("Dongle reports RD_OOSYNC\n"));
4439 intstatus &= ~I_RD_OOSYNC;
4442 if (intstatus & I_SBINT) {
4443 DHD_ERROR(("Dongle reports SBINT\n"));
4444 intstatus &= ~I_SBINT;
4447 /* Would be active due to wake-wlan in gSPI */
4448 if (intstatus & I_CHIPACTIVE) {
4449 DHD_INFO(("Dongle reports CHIPACTIVE\n"));
4450 intstatus &= ~I_CHIPACTIVE;
4453 /* Ignore frame indications if rxskip is set */
4455 intstatus &= ~I_HMB_FRAME_IND;
4457 /* On frame indication, read available frames */
4458 if (PKT_AVAILABLE()) {
4459 framecnt = dhdsdio_readframes(bus, rxlimit, &rxdone);
4460 if (rxdone || bus->rxskip)
4461 intstatus &= ~I_HMB_FRAME_IND;
4462 rxlimit -= min(framecnt, rxlimit);
4465 /* Keep still-pending events for next scheduling */
4466 bus->intstatus = intstatus;
4469 #if defined(OOB_INTR_ONLY)
4470 bcmsdh_oob_intr_set(1);
4471 #endif /* (OOB_INTR_ONLY) */
4472 /* Re-enable interrupts to detect new device events (mailbox, rx frame)
4473 * or clock availability. (Allows tx loop to check ipend if desired.)
4474 * (Unless register access seems hosed, as we may not be able to ACK...)
4476 if (bus->intr && bus->intdis && !bcmsdh_regfail(sdh)) {
4477 DHD_INTR(("%s: enable SDIO interrupts, rxdone %d framecnt %d\n",
4478 __func__, rxdone, framecnt));
4479 bus->intdis = false;
4480 bcmsdh_intr_enable(sdh);
4483 if (DATAOK(bus) && bus->ctrl_frame_stat &&
4484 (bus->clkstate == CLK_AVAIL)) {
4488 dhd_bcmsdh_send_buf(bus, bcmsdh_cur_sbwad(sdh), SDIO_FUNC_2,
4489 F2SYNC, (u8 *) bus->ctrl_frame_buf,
4490 (u32) bus->ctrl_frame_len, NULL,
4492 ASSERT(ret != -BCME_PENDING);
4495 /* On failure, abort the command and
4496 terminate the frame */
4497 DHD_INFO(("%s: sdio error %d, abort command and "
4498 "terminate frame.\n", __func__, ret));
4501 bcmsdh_abort(sdh, SDIO_FUNC_2);
4503 bcmsdh_cfg_write(sdh, SDIO_FUNC_1,
4504 SBSDIO_FUNC1_FRAMECTRL, SFC_WF_TERM,
4508 for (i = 0; i < 3; i++) {
4510 hi = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
4511 SBSDIO_FUNC1_WFRAMEBCHI,
4513 lo = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
4514 SBSDIO_FUNC1_WFRAMEBCLO,
4516 bus->f1regdata += 2;
4517 if ((hi == 0) && (lo == 0))
4523 bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQUENCE_WRAP;
4525 DHD_INFO(("Return_dpc value is : %d\n", ret));
4526 bus->ctrl_frame_stat = false;
4527 dhd_wait_event_wakeup(bus->dhd);
4529 /* Send queued frames (limit 1 if rx may still be pending) */
4530 else if ((bus->clkstate == CLK_AVAIL) && !bus->fcstate &&
4531 bcm_pktq_mlen(&bus->txq, ~bus->flowcontrol) && txlimit
4533 framecnt = rxdone ? txlimit : min(txlimit, dhd_txminmax);
4534 framecnt = dhdsdio_sendfromq(bus, framecnt);
4535 txlimit -= framecnt;
4538 /* Resched if events or tx frames are pending,
4539 else await next interrupt */
4540 /* On failed register access, all bets are off:
4541 no resched or interrupts */
4542 if ((bus->dhd->busstate == DHD_BUS_DOWN) || bcmsdh_regfail(sdh)) {
4543 DHD_ERROR(("%s: failed backplane access over SDIO, halting "
4544 "operation %d\n", __func__, bcmsdh_regfail(sdh)));
4545 bus->dhd->busstate = DHD_BUS_DOWN;
4547 } else if (bus->clkstate == CLK_PENDING) {
4548 DHD_INFO(("%s: rescheduled due to CLK_PENDING awaiting "
4549 "I_CHIPACTIVE interrupt\n", __func__));
4551 } else if (bus->intstatus || bus->ipend ||
4552 (!bus->fcstate && bcm_pktq_mlen(&bus->txq, ~bus->flowcontrol) &&
4553 DATAOK(bus)) || PKT_AVAILABLE()) {
4557 bus->dpc_sched = resched;
4559 /* If we're done for now, turn off clock request. */
4560 if ((bus->clkstate != CLK_PENDING)
4561 && bus->idletime == DHD_IDLE_IMMEDIATE) {
4562 bus->activity = false;
4563 dhdsdio_clkctl(bus, CLK_NONE, false);
4566 dhd_os_sdunlock(bus->dhd);
4571 bool dhd_bus_dpc(struct dhd_bus *bus)
4575 /* Call the DPC directly. */
4576 DHD_TRACE(("Calling dhdsdio_dpc() from %s\n", __func__));
4577 resched = dhdsdio_dpc(bus);
4582 void dhdsdio_isr(void *arg)
4584 dhd_bus_t *bus = (dhd_bus_t *) arg;
4587 DHD_TRACE(("%s: Enter\n", __func__));
4590 DHD_ERROR(("%s : bus is null pointer , exit\n", __func__));
4595 if (bus->dhd->busstate == DHD_BUS_DOWN) {
4596 DHD_ERROR(("%s : bus is down. we have nothing to do\n",
4600 /* Count the interrupt call */
4604 /* Shouldn't get this interrupt if we're sleeping? */
4605 if (bus->sleeping) {
4606 DHD_ERROR(("INTERRUPT WHILE SLEEPING??\n"));
4610 /* Disable additional interrupts (is this needed now)? */
4612 DHD_INTR(("%s: disable SDIO interrupts\n", __func__));
4614 DHD_ERROR(("dhdsdio_isr() w/o interrupt configured!\n"));
4616 bcmsdh_intr_disable(sdh);
4619 #if defined(SDIO_ISR_THREAD)
4620 DHD_TRACE(("Calling dhdsdio_dpc() from %s\n", __func__));
4621 while (dhdsdio_dpc(bus))
4624 bus->dpc_sched = true;
4625 dhd_sched_dpc(bus->dhd);
4631 static void dhdsdio_pktgen_init(dhd_bus_t *bus)
4633 /* Default to specified length, or full range */
4634 if (dhd_pktgen_len) {
4635 bus->pktgen_maxlen = min(dhd_pktgen_len, MAX_PKTGEN_LEN);
4636 bus->pktgen_minlen = bus->pktgen_maxlen;
4638 bus->pktgen_maxlen = MAX_PKTGEN_LEN;
4639 bus->pktgen_minlen = 0;
4641 bus->pktgen_len = (u16) bus->pktgen_minlen;
4643 /* Default to per-watchdog burst with 10s print time */
4644 bus->pktgen_freq = 1;
4645 bus->pktgen_print = 10000 / dhd_watchdog_ms;
4646 bus->pktgen_count = (dhd_pktgen * dhd_watchdog_ms + 999) / 1000;
4648 /* Default to echo mode */
4649 bus->pktgen_mode = DHD_PKTGEN_ECHO;
4650 bus->pktgen_stop = 1;
4653 static void dhdsdio_pktgen(dhd_bus_t *bus)
4655 struct sk_buff *pkt;
4661 /* Display current count if appropriate */
4662 if (bus->pktgen_print && (++bus->pktgen_ptick >= bus->pktgen_print)) {
4663 bus->pktgen_ptick = 0;
4664 printk(KERN_DEBUG "%s: send attempts %d rcvd %d\n",
4665 __func__, bus->pktgen_sent, bus->pktgen_rcvd);
4668 /* For recv mode, just make sure dongle has started sending */
4669 if (bus->pktgen_mode == DHD_PKTGEN_RECV) {
4670 if (!bus->pktgen_rcvd)
4671 dhdsdio_sdtest_set(bus, true);
4675 /* Otherwise, generate or request the specified number of packets */
4676 for (pktcount = 0; pktcount < bus->pktgen_count; pktcount++) {
4677 /* Stop if total has been reached */
4678 if (bus->pktgen_total
4679 && (bus->pktgen_sent >= bus->pktgen_total)) {
4680 bus->pktgen_count = 0;
4684 /* Allocate an appropriate-sized packet */
4685 len = bus->pktgen_len;
4686 pkt = bcm_pkt_buf_get_skb(
4687 (len + SDPCM_HDRLEN + SDPCM_TEST_HDRLEN + DHD_SDALIGN),
4690 DHD_ERROR(("%s: bcm_pkt_buf_get_skb failed!\n",
4694 PKTALIGN(pkt, (len + SDPCM_HDRLEN + SDPCM_TEST_HDRLEN),
4696 data = (u8 *) (pkt->data) + SDPCM_HDRLEN;
4698 /* Write test header cmd and extra based on mode */
4699 switch (bus->pktgen_mode) {
4700 case DHD_PKTGEN_ECHO:
4701 *data++ = SDPCM_TEST_ECHOREQ;
4702 *data++ = (u8) bus->pktgen_sent;
4705 case DHD_PKTGEN_SEND:
4706 *data++ = SDPCM_TEST_DISCARD;
4707 *data++ = (u8) bus->pktgen_sent;
4710 case DHD_PKTGEN_RXBURST:
4711 *data++ = SDPCM_TEST_BURST;
4712 *data++ = (u8) bus->pktgen_count;
4716 DHD_ERROR(("Unrecognized pktgen mode %d\n",
4718 bcm_pkt_buf_free_skb(pkt, true);
4719 bus->pktgen_count = 0;
4723 /* Write test header length field */
4724 *data++ = (len >> 0);
4725 *data++ = (len >> 8);
4727 /* Then fill in the remainder -- N/A for burst,
4729 for (fillbyte = 0; fillbyte < len; fillbyte++)
4731 SDPCM_TEST_FILL(fillbyte, (u8) bus->pktgen_sent);
4734 if (DHD_BYTES_ON() && DHD_DATA_ON()) {
4735 data = (u8 *) (pkt->data) + SDPCM_HDRLEN;
4736 printk(KERN_DEBUG "dhdsdio_pktgen: Tx Data:\n");
4737 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, data,
4738 pkt->len - SDPCM_HDRLEN);
4743 if (dhdsdio_txpkt(bus, pkt, SDPCM_TEST_CHANNEL, true)) {
4745 if (bus->pktgen_stop
4746 && bus->pktgen_stop == bus->pktgen_fail)
4747 bus->pktgen_count = 0;
4751 /* Bump length if not fixed, wrap at max */
4752 if (++bus->pktgen_len > bus->pktgen_maxlen)
4753 bus->pktgen_len = (u16) bus->pktgen_minlen;
4755 /* Special case for burst mode: just send one request! */
4756 if (bus->pktgen_mode == DHD_PKTGEN_RXBURST)
4761 static void dhdsdio_sdtest_set(dhd_bus_t *bus, bool start)
4763 struct sk_buff *pkt;
4766 /* Allocate the packet */
4767 pkt = bcm_pkt_buf_get_skb(SDPCM_HDRLEN + SDPCM_TEST_HDRLEN +
4770 DHD_ERROR(("%s: bcm_pkt_buf_get_skb failed!\n", __func__));
4773 PKTALIGN(pkt, (SDPCM_HDRLEN + SDPCM_TEST_HDRLEN), DHD_SDALIGN);
4774 data = (u8 *) (pkt->data) + SDPCM_HDRLEN;
4776 /* Fill in the test header */
4777 *data++ = SDPCM_TEST_SEND;
4779 *data++ = (bus->pktgen_maxlen >> 0);
4780 *data++ = (bus->pktgen_maxlen >> 8);
4783 if (dhdsdio_txpkt(bus, pkt, SDPCM_TEST_CHANNEL, true))
4787 static void dhdsdio_testrcv(dhd_bus_t *bus, struct sk_buff *pkt, uint seq)
4797 /* Check for min length */
4799 if (pktlen < SDPCM_TEST_HDRLEN) {
4800 DHD_ERROR(("dhdsdio_restrcv: toss runt frame, pktlen %d\n",
4802 bcm_pkt_buf_free_skb(pkt, false);
4806 /* Extract header fields */
4811 len += *data++ << 8;
4813 /* Check length for relevant commands */
4814 if (cmd == SDPCM_TEST_DISCARD || cmd == SDPCM_TEST_ECHOREQ
4815 || cmd == SDPCM_TEST_ECHORSP) {
4816 if (pktlen != len + SDPCM_TEST_HDRLEN) {
4817 DHD_ERROR(("dhdsdio_testrcv: frame length mismatch, "
4818 "pktlen %d seq %d" " cmd %d extra %d len %d\n",
4819 pktlen, seq, cmd, extra, len));
4820 bcm_pkt_buf_free_skb(pkt, false);
4825 /* Process as per command */
4827 case SDPCM_TEST_ECHOREQ:
4828 /* Rx->Tx turnaround ok (even on NDIS w/current
4830 *(u8 *) (pkt->data) = SDPCM_TEST_ECHORSP;
4831 if (dhdsdio_txpkt(bus, pkt, SDPCM_TEST_CHANNEL, true) == 0) {
4835 bcm_pkt_buf_free_skb(pkt, false);
4840 case SDPCM_TEST_ECHORSP:
4841 if (bus->ext_loop) {
4842 bcm_pkt_buf_free_skb(pkt, false);
4847 for (offset = 0; offset < len; offset++, data++) {
4848 if (*data != SDPCM_TEST_FILL(offset, extra)) {
4849 DHD_ERROR(("dhdsdio_testrcv: echo data mismatch: " "offset %d (len %d) expect 0x%02x rcvd 0x%02x\n",
4851 SDPCM_TEST_FILL(offset, extra), *data));
4855 bcm_pkt_buf_free_skb(pkt, false);
4859 case SDPCM_TEST_DISCARD:
4860 bcm_pkt_buf_free_skb(pkt, false);
4864 case SDPCM_TEST_BURST:
4865 case SDPCM_TEST_SEND:
4867 DHD_INFO(("dhdsdio_testrcv: unsupported or unknown command, "
4868 "pktlen %d seq %d" " cmd %d extra %d len %d\n",
4869 pktlen, seq, cmd, extra, len));
4870 bcm_pkt_buf_free_skb(pkt, false);
4874 /* For recv mode, stop at limie (and tell dongle to stop sending) */
4875 if (bus->pktgen_mode == DHD_PKTGEN_RECV) {
4876 if (bus->pktgen_total
4877 && (bus->pktgen_rcvd >= bus->pktgen_total)) {
4878 bus->pktgen_count = 0;
4879 dhdsdio_sdtest_set(bus, false);
4885 extern bool dhd_bus_watchdog(dhd_pub_t *dhdp)
4889 DHD_TIMER(("%s: Enter\n", __func__));
4893 if (bus->dhd->dongle_reset)
4896 /* Ignore the timer if simulating bus down */
4900 dhd_os_sdlock(bus->dhd);
4902 /* Poll period: check device if appropriate. */
4903 if (bus->poll && (++bus->polltick >= bus->pollrate)) {
4906 /* Reset poll tick */
4909 /* Check device if no interrupts */
4910 if (!bus->intr || (bus->intrcount == bus->lastintrs)) {
4912 if (!bus->dpc_sched) {
4914 devpend = bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_0,
4918 devpend & (INTR_STATUS_FUNC1 |
4922 /* If there is something, make like the ISR and
4928 bcmsdh_intr_disable(bus->sdh);
4930 bus->dpc_sched = true;
4931 dhd_sched_dpc(bus->dhd);
4936 /* Update interrupt tracking */
4937 bus->lastintrs = bus->intrcount;
4940 /* Poll for console output periodically */
4941 if (dhdp->busstate == DHD_BUS_DATA && dhd_console_ms != 0) {
4942 bus->console.count += dhd_watchdog_ms;
4943 if (bus->console.count >= dhd_console_ms) {
4944 bus->console.count -= dhd_console_ms;
4945 /* Make sure backplane clock is on */
4946 dhdsdio_clkctl(bus, CLK_AVAIL, false);
4947 if (dhdsdio_readconsole(bus) < 0)
4948 dhd_console_ms = 0; /* On error,
4952 #endif /* DHD_DEBUG */
4955 /* Generate packets if configured */
4956 if (bus->pktgen_count && (++bus->pktgen_tick >= bus->pktgen_freq)) {
4957 /* Make sure backplane clock is on */
4958 dhdsdio_clkctl(bus, CLK_AVAIL, false);
4959 bus->pktgen_tick = 0;
4960 dhdsdio_pktgen(bus);
4964 /* On idle timeout clear activity flag and/or turn off clock */
4965 if ((bus->idletime > 0) && (bus->clkstate == CLK_AVAIL)) {
4966 if (++bus->idlecount >= bus->idletime) {
4968 if (bus->activity) {
4969 bus->activity = false;
4970 dhd_os_wd_timer(bus->dhd, dhd_watchdog_ms);
4972 dhdsdio_clkctl(bus, CLK_NONE, false);
4977 dhd_os_sdunlock(bus->dhd);
4983 extern int dhd_bus_console_in(dhd_pub_t *dhdp, unsigned char *msg, uint msglen)
4985 dhd_bus_t *bus = dhdp->bus;
4988 struct sk_buff *pkt;
4990 /* Address could be zero if CONSOLE := 0 in dongle Makefile */
4991 if (bus->console_addr == 0)
4994 /* Exclusive bus access */
4995 dhd_os_sdlock(bus->dhd);
4997 /* Don't allow input if dongle is in reset */
4998 if (bus->dhd->dongle_reset) {
4999 dhd_os_sdunlock(bus->dhd);
5003 /* Request clock to allow SDIO accesses */
5005 /* No pend allowed since txpkt is called later, ht clk has to be on */
5006 dhdsdio_clkctl(bus, CLK_AVAIL, false);
5008 /* Zero cbuf_index */
5009 addr = bus->console_addr + offsetof(rte_cons_t, cbuf_idx);
5010 val = cpu_to_le32(0);
5011 rv = dhdsdio_membytes(bus, true, addr, (u8 *)&val, sizeof(val));
5015 /* Write message into cbuf */
5016 addr = bus->console_addr + offsetof(rte_cons_t, cbuf);
5017 rv = dhdsdio_membytes(bus, true, addr, (u8 *)msg, msglen);
5021 /* Write length into vcons_in */
5022 addr = bus->console_addr + offsetof(rte_cons_t, vcons_in);
5023 val = cpu_to_le32(msglen);
5024 rv = dhdsdio_membytes(bus, true, addr, (u8 *)&val, sizeof(val));
5028 /* Bump dongle by sending an empty event pkt.
5029 * sdpcm_sendup (RX) checks for virtual console input.
5031 pkt = bcm_pkt_buf_get_skb(4 + SDPCM_RESERVE);
5032 if ((pkt != NULL) && bus->clkstate == CLK_AVAIL)
5033 dhdsdio_txpkt(bus, pkt, SDPCM_EVENT_CHANNEL, true);
5036 if ((bus->idletime == DHD_IDLE_IMMEDIATE) && !bus->dpc_sched) {
5037 bus->activity = false;
5038 dhdsdio_clkctl(bus, CLK_NONE, true);
5041 dhd_os_sdunlock(bus->dhd);
5045 #endif /* DHD_DEBUG */
5048 static void dhd_dump_cis(uint fn, u8 *cis)
5050 uint byte, tag, tdata;
5051 DHD_INFO(("Function %d CIS:\n", fn));
5053 for (tdata = byte = 0; byte < SBSDIO_CIS_SIZE_LIMIT; byte++) {
5054 if ((byte % 16) == 0)
5056 DHD_INFO(("%02x ", cis[byte]));
5057 if ((byte % 16) == 15)
5065 else if ((byte + 1) < SBSDIO_CIS_SIZE_LIMIT)
5066 tdata = cis[byte + 1] + 1;
5071 if ((byte % 16) != 15)
5074 #endif /* DHD_DEBUG */
5076 static bool dhdsdio_chipmatch(u16 chipid)
5078 if (chipid == BCM4325_CHIP_ID)
5080 if (chipid == BCM4329_CHIP_ID)
5082 if (chipid == BCM4319_CHIP_ID)
5087 static void *dhdsdio_probe(u16 venid, u16 devid, u16 bus_no,
5088 u16 slot, u16 func, uint bustype, void *regsva,
5094 /* Init global variables at run-time, not as part of the declaration.
5095 * This is required to support init/de-init of the driver.
5097 * of globals as part of the declaration results in non-deterministic
5098 * behavior since the value of the globals may be different on the
5099 * first time that the driver is initialized vs subsequent
5102 dhd_txbound = DHD_TXBOUND;
5103 dhd_rxbound = DHD_RXBOUND;
5104 dhd_alignctl = true;
5106 dhd_readahead = true;
5108 dhd_dongle_memsize = 0;
5109 dhd_txminmax = DHD_TXMINMAX;
5115 DHD_TRACE(("%s: Enter\n", __func__));
5116 DHD_INFO(("%s: venid 0x%04x devid 0x%04x\n", __func__, venid, devid));
5118 /* We make assumptions about address window mappings */
5119 ASSERT((unsigned long)regsva == SI_ENUM_BASE);
5121 /* BCMSDH passes venid and devid based on CIS parsing -- but
5123 * means early parse could fail, so here we should get either an ID
5124 * we recognize OR (-1) indicating we must request power first.
5126 /* Check the Vendor ID */
5129 case PCI_VENDOR_ID_BROADCOM:
5132 DHD_ERROR(("%s: unknown vendor: 0x%04x\n", __func__, venid));
5136 /* Check the Device ID and make sure it's one that we support */
5138 case BCM4325_D11DUAL_ID: /* 4325 802.11a/g id */
5139 case BCM4325_D11G_ID: /* 4325 802.11g 2.4Ghz band id */
5140 case BCM4325_D11A_ID: /* 4325 802.11a 5Ghz band id */
5141 DHD_INFO(("%s: found 4325 Dongle\n", __func__));
5143 case BCM4329_D11NDUAL_ID: /* 4329 802.11n dualband device */
5144 case BCM4329_D11N2G_ID: /* 4329 802.11n 2.4G device */
5145 case BCM4329_D11N5G_ID: /* 4329 802.11n 5G device */
5147 DHD_INFO(("%s: found 4329 Dongle\n", __func__));
5149 case BCM4319_D11N_ID: /* 4319 802.11n id */
5150 case BCM4319_D11N2G_ID: /* 4319 802.11n2g id */
5151 case BCM4319_D11N5G_ID: /* 4319 802.11n5g id */
5152 DHD_INFO(("%s: found 4319 Dongle\n", __func__));
5155 DHD_INFO(("%s: allow device id 0, will check chip internals\n",
5160 DHD_ERROR(("%s: skipping 0x%04x/0x%04x, not a dongle\n",
5161 __func__, venid, devid));
5165 /* Allocate private bus interface state */
5166 bus = kzalloc(sizeof(dhd_bus_t), GFP_ATOMIC);
5168 DHD_ERROR(("%s: kmalloc of dhd_bus_t failed\n", __func__));
5172 bus->cl_devid = (u16) devid;
5174 bus->tx_seq = SDPCM_SEQUENCE_WRAP - 1;
5175 bus->usebufpool = false; /* Use bufpool if allocated,
5176 else use locally malloced rxbuf */
5178 /* attempt to attach to the dongle */
5179 if (!(dhdsdio_probe_attach(bus, sdh, regsva, devid))) {
5180 DHD_ERROR(("%s: dhdsdio_probe_attach failed\n", __func__));
5184 /* Attach to the dhd/OS/network interface */
5185 bus->dhd = dhd_attach(bus, SDPCM_RESERVE);
5187 DHD_ERROR(("%s: dhd_attach failed\n", __func__));
5191 /* Allocate buffers */
5192 if (!(dhdsdio_probe_malloc(bus, sdh))) {
5193 DHD_ERROR(("%s: dhdsdio_probe_malloc failed\n", __func__));
5197 if (!(dhdsdio_probe_init(bus, sdh))) {
5198 DHD_ERROR(("%s: dhdsdio_probe_init failed\n", __func__));
5202 /* Register interrupt callback, but mask it (not operational yet). */
5203 DHD_INTR(("%s: disable SDIO interrupts (not interested yet)\n",
5205 bcmsdh_intr_disable(sdh);
5206 ret = bcmsdh_intr_reg(sdh, dhdsdio_isr, bus);
5208 DHD_ERROR(("%s: FAILED: bcmsdh_intr_reg returned %d\n",
5212 DHD_INTR(("%s: registered SDIO interrupt function ok\n", __func__));
5214 DHD_INFO(("%s: completed!!\n", __func__));
5216 /* if firmware path present try to download and bring up bus */
5217 ret = dhd_bus_start(bus->dhd);
5219 if (ret == -ENOLINK) {
5220 DHD_ERROR(("%s: dongle is not responding\n", __func__));
5224 /* Ok, have the per-port tell the stack we're open for business */
5225 if (dhd_net_attach(bus->dhd, 0) != 0) {
5226 DHD_ERROR(("%s: Net attach failed!!\n", __func__));
5233 dhdsdio_release(bus);
5238 dhdsdio_probe_attach(struct dhd_bus *bus, void *sdh, void *regsva, u16 devid)
5243 bus->alp_only = true;
5245 /* Return the window to backplane enumeration space for core access */
5246 if (dhdsdio_set_siaddr_window(bus, SI_ENUM_BASE))
5247 DHD_ERROR(("%s: FAILED to return to SI_ENUM_BASE\n", __func__));
5250 printk(KERN_DEBUG "F1 signature read @0x18000000=0x%4x\n",
5251 bcmsdh_reg_read(bus->sdh, SI_ENUM_BASE, 4));
5253 #endif /* DHD_DEBUG */
5256 * Force PLL off until dhdsdio_chip_attach()
5257 * programs PLL control regs
5260 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
5261 DHD_INIT_CLKCTL1, &err);
5264 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
5267 if (err || ((clkctl & ~SBSDIO_AVBITS) != DHD_INIT_CLKCTL1)) {
5268 DHD_ERROR(("dhdsdio_probe: ChipClkCSR access: err %d wrote "
5269 "0x%02x read 0x%02x\n",
5270 err, DHD_INIT_CLKCTL1, clkctl));
5274 if (DHD_INFO_ON()) {
5276 u8 *cis[SDIOD_MAX_IOFUNCS];
5279 numfn = bcmsdh_query_iofnum(sdh);
5280 ASSERT(numfn <= SDIOD_MAX_IOFUNCS);
5282 /* Make sure ALP is available before trying to read CIS */
5283 SPINWAIT(((clkctl = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
5284 SBSDIO_FUNC1_CHIPCLKCSR,
5286 !SBSDIO_ALPAV(clkctl)), PMU_MAX_TRANSITION_DLY);
5288 /* Now request ALP be put on the bus */
5289 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
5290 DHD_INIT_CLKCTL2, &err);
5293 for (fn = 0; fn <= numfn; fn++) {
5294 cis[fn] = kzalloc(SBSDIO_CIS_SIZE_LIMIT, GFP_ATOMIC);
5296 DHD_INFO(("dhdsdio_probe: fn %d cis malloc "
5301 err = bcmsdh_cis_read(sdh, fn, cis[fn],
5302 SBSDIO_CIS_SIZE_LIMIT);
5304 DHD_INFO(("dhdsdio_probe: fn %d cis read "
5305 "err %d\n", fn, err));
5309 dhd_dump_cis(fn, cis[fn]);
5318 DHD_ERROR(("dhdsdio_probe: error read/parsing CIS\n"));
5322 #endif /* DHD_DEBUG */
5324 if (dhdsdio_chip_attach(bus, regsva)) {
5325 DHD_ERROR(("%s: dhdsdio_chip_attach failed!\n", __func__));
5329 bcmsdh_chipinfo(sdh, bus->ci->chip, bus->ci->chiprev);
5331 if (!dhdsdio_chipmatch((u16) bus->ci->chip)) {
5332 DHD_ERROR(("%s: unsupported chip: 0x%04x\n",
5333 __func__, bus->ci->chip));
5337 dhdsdio_sdiod_drive_strength_init(bus, dhd_sdiod_drive_strength);
5339 /* Get info on the ARM and SOCRAM cores... */
5340 if (!DHD_NOPMU(bus)) {
5341 bus->armrev = SBCOREREV(bcmsdh_reg_read(bus->sdh,
5342 CORE_SB(bus->ci->armcorebase, sbidhigh), 4));
5343 bus->orig_ramsize = bus->ci->ramsize;
5344 if (!(bus->orig_ramsize)) {
5345 DHD_ERROR(("%s: failed to find SOCRAM memory!\n",
5349 bus->ramsize = bus->orig_ramsize;
5350 if (dhd_dongle_memsize)
5351 dhd_dongle_setmemsize(bus, dhd_dongle_memsize);
5353 DHD_ERROR(("DHD: dongle ram size is set to %d(orig %d)\n",
5354 bus->ramsize, bus->orig_ramsize));
5357 bus->regs = (void *)bus->ci->buscorebase;
5359 /* Set core control so an SDIO reset does a backplane reset */
5360 OR_REG(&bus->regs->corecontrol, CC_BPRESEN);
5362 bcm_pktq_init(&bus->txq, (PRIOMASK + 1), TXQLEN);
5364 /* Locate an appropriately-aligned portion of hdrbuf */
5365 bus->rxhdr = (u8 *) roundup((unsigned long)&bus->hdrbuf[0], DHD_SDALIGN);
5367 /* Set the poll and/or interrupt flags */
5368 bus->intr = (bool) dhd_intr;
5369 bus->poll = (bool) dhd_poll;
5379 static bool dhdsdio_probe_malloc(dhd_bus_t *bus, void *sdh)
5381 DHD_TRACE(("%s: Enter\n", __func__));
5383 if (bus->dhd->maxctl) {
5385 roundup((bus->dhd->maxctl + SDPCM_HDRLEN),
5386 ALIGNMENT) + DHD_SDALIGN;
5387 bus->rxbuf = kmalloc(bus->rxblen, GFP_ATOMIC);
5388 if (!(bus->rxbuf)) {
5389 DHD_ERROR(("%s: kmalloc of %d-byte rxbuf failed\n",
5390 __func__, bus->rxblen));
5395 /* Allocate buffer to receive glomed packet */
5396 bus->databuf = kmalloc(MAX_DATA_BUF, GFP_ATOMIC);
5397 if (!(bus->databuf)) {
5398 DHD_ERROR(("%s: kmalloc of %d-byte databuf failed\n",
5399 __func__, MAX_DATA_BUF));
5400 /* release rxbuf which was already located as above */
5406 /* Align the buffer */
5407 if ((unsigned long)bus->databuf % DHD_SDALIGN)
5409 bus->databuf + (DHD_SDALIGN -
5410 ((unsigned long)bus->databuf % DHD_SDALIGN));
5412 bus->dataptr = bus->databuf;
5420 static bool dhdsdio_probe_init(dhd_bus_t *bus, void *sdh)
5424 DHD_TRACE(("%s: Enter\n", __func__));
5427 dhdsdio_pktgen_init(bus);
5430 /* Disable F2 to clear any intermediate frame state on the dongle */
5431 bcmsdh_cfg_write(sdh, SDIO_FUNC_0, SDIOD_CCCR_IOEN, SDIO_FUNC_ENABLE_1,
5434 bus->dhd->busstate = DHD_BUS_DOWN;
5435 bus->sleeping = false;
5436 bus->rxflow = false;
5437 bus->prev_rxlim_hit = 0;
5439 /* Done with backplane-dependent accesses, can drop clock... */
5440 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR, 0, NULL);
5442 /* ...and initialize clock/power states */
5443 bus->clkstate = CLK_SDONLY;
5444 bus->idletime = (s32) dhd_idletime;
5445 bus->idleclock = DHD_IDLE_ACTIVE;
5447 /* Query the F2 block size, set roundup accordingly */
5449 if (bcmsdh_iovar_op(sdh, "sd_blocksize", &fnum, sizeof(s32),
5450 &bus->blocksize, sizeof(s32), false) != 0) {
5452 DHD_ERROR(("%s: fail on %s get\n", __func__, "sd_blocksize"));
5454 DHD_INFO(("%s: Initial value for %s is %d\n",
5455 __func__, "sd_blocksize", bus->blocksize));
5457 bus->roundup = min(max_roundup, bus->blocksize);
5459 /* Query if bus module supports packet chaining,
5460 default to use if supported */
5461 if (bcmsdh_iovar_op(sdh, "sd_rxchain", NULL, 0,
5462 &bus->sd_rxchain, sizeof(s32),
5464 bus->sd_rxchain = false;
5466 DHD_INFO(("%s: bus module (through bcmsdh API) %s chaining\n",
5468 (bus->sd_rxchain ? "supports" : "does not support")));
5470 bus->use_rxchain = (bool) bus->sd_rxchain;
5476 dhd_bus_download_firmware(struct dhd_bus *bus, char *fw_path, char *nv_path)
5479 bus->fw_path = fw_path;
5480 bus->nv_path = nv_path;
5482 ret = dhdsdio_download_firmware(bus, bus->sdh);
5488 dhdsdio_download_firmware(struct dhd_bus *bus, void *sdh)
5492 /* Download the firmware */
5493 dhdsdio_clkctl(bus, CLK_AVAIL, false);
5495 ret = _dhdsdio_download_firmware(bus) == 0;
5497 dhdsdio_clkctl(bus, CLK_SDONLY, false);
5502 /* Detach and free everything */
5503 static void dhdsdio_release(dhd_bus_t *bus)
5505 DHD_TRACE(("%s: Enter\n", __func__));
5508 /* De-register interrupt handler */
5509 bcmsdh_intr_disable(bus->sdh);
5510 bcmsdh_intr_dereg(bus->sdh);
5513 dhd_detach(bus->dhd);
5514 dhdsdio_release_dongle(bus);
5518 dhdsdio_release_malloc(bus);
5523 DHD_TRACE(("%s: Disconnected\n", __func__));
5526 static void dhdsdio_release_malloc(dhd_bus_t *bus)
5528 DHD_TRACE(("%s: Enter\n", __func__));
5530 if (bus->dhd && bus->dhd->dongle_reset)
5535 bus->rxctl = bus->rxbuf = NULL;
5539 kfree(bus->databuf);
5540 bus->databuf = NULL;
5543 static void dhdsdio_release_dongle(dhd_bus_t *bus)
5545 DHD_TRACE(("%s: Enter\n", __func__));
5547 if (bus->dhd && bus->dhd->dongle_reset)
5551 dhdsdio_clkctl(bus, CLK_AVAIL, false);
5552 dhdsdio_clkctl(bus, CLK_NONE, false);
5553 dhdsdio_chip_detach(bus);
5554 if (bus->vars && bus->varsz)
5559 DHD_TRACE(("%s: Disconnected\n", __func__));
5562 static void dhdsdio_disconnect(void *ptr)
5564 dhd_bus_t *bus = (dhd_bus_t *)ptr;
5566 DHD_TRACE(("%s: Enter\n", __func__));
5570 dhdsdio_release(bus);
5573 DHD_TRACE(("%s: Disconnected\n", __func__));
5576 /* Register/Unregister functions are called by the main DHD entry
5577 * point (e.g. module insertion) to link with the bus driver, in
5578 * order to look for or await the device.
5581 static bcmsdh_driver_t dhd_sdio = {
5586 int dhd_bus_register(void)
5588 DHD_TRACE(("%s: Enter\n", __func__));
5590 return bcmsdh_register(&dhd_sdio);
5593 void dhd_bus_unregister(void)
5595 DHD_TRACE(("%s: Enter\n", __func__));
5597 bcmsdh_unregister();
5600 #ifdef BCMEMBEDIMAGE
5601 static int dhdsdio_download_code_array(struct dhd_bus *bus)
5606 DHD_INFO(("%s: download embedded firmware...\n", __func__));
5608 /* Download image */
5609 while ((offset + MEMBLOCK) < sizeof(dlarray)) {
5611 dhdsdio_membytes(bus, true, offset, dlarray + offset,
5614 DHD_ERROR(("%s: error %d on writing %d membytes at "
5616 __func__, bcmerror, MEMBLOCK, offset));
5623 if (offset < sizeof(dlarray)) {
5624 bcmerror = dhdsdio_membytes(bus, true, offset,
5626 sizeof(dlarray) - offset);
5628 DHD_ERROR(("%s: error %d on writing %d membytes at "
5629 "0x%08x\n", __func__, bcmerror,
5630 sizeof(dlarray) - offset, offset));
5635 /* Upload and compare the downloaded code */
5637 unsigned char *ularray;
5639 ularray = kmalloc(bus->ramsize, GFP_ATOMIC);
5644 /* Upload image to verify downloaded contents. */
5646 memset(ularray, 0xaa, bus->ramsize);
5647 while ((offset + MEMBLOCK) < sizeof(dlarray)) {
5649 dhdsdio_membytes(bus, false, offset,
5650 ularray + offset, MEMBLOCK);
5652 DHD_ERROR(("%s: error %d on reading %d membytes"
5654 __func__, bcmerror, MEMBLOCK, offset));
5661 if (offset < sizeof(dlarray)) {
5662 bcmerror = dhdsdio_membytes(bus, false, offset,
5664 sizeof(dlarray) - offset);
5666 DHD_ERROR(("%s: error %d on reading %d membytes at 0x%08x\n",
5668 sizeof(dlarray) - offset, offset));
5673 if (memcmp(dlarray, ularray, sizeof(dlarray))) {
5674 DHD_ERROR(("%s: Downloaded image is corrupted.\n",
5679 DHD_ERROR(("%s: Download/Upload/Compare succeeded.\n",
5684 #endif /* DHD_DEBUG */
5689 #endif /* BCMEMBEDIMAGE */
5691 static int dhdsdio_download_code_file(struct dhd_bus *bus, char *fw_path)
5697 u8 *memblock = NULL, *memptr;
5699 DHD_INFO(("%s: download firmware %s\n", __func__, fw_path));
5701 image = dhd_os_open_image(fw_path);
5705 memptr = memblock = kmalloc(MEMBLOCK + DHD_SDALIGN, GFP_ATOMIC);
5706 if (memblock == NULL) {
5707 DHD_ERROR(("%s: Failed to allocate memory %d bytes\n",
5708 __func__, MEMBLOCK));
5711 if ((u32)(unsigned long)memblock % DHD_SDALIGN)
5713 (DHD_SDALIGN - ((u32)(unsigned long)memblock % DHD_SDALIGN));
5715 /* Download image */
5717 dhd_os_get_image_block((char *)memptr, MEMBLOCK, image))) {
5718 bcmerror = dhdsdio_membytes(bus, true, offset, memptr, len);
5720 DHD_ERROR(("%s: error %d on writing %d membytes at "
5721 "0x%08x\n", __func__, bcmerror, MEMBLOCK, offset));
5732 dhd_os_close_image(image);
5738 * ProcessVars:Takes a buffer of "<var>=<value>\n" lines read from a file
5739 * and ending in a NUL.
5740 * Removes carriage returns, empty lines, comment lines, and converts
5742 * Shortens buffer as needed and pads with NULs. End of buffer is marked
5746 static uint process_nvram_vars(char *varbuf, uint len)
5755 findNewline = false;
5758 for (n = 0; n < len; n++) {
5761 if (varbuf[n] == '\r')
5763 if (findNewline && varbuf[n] != '\n')
5765 findNewline = false;
5766 if (varbuf[n] == '#') {
5770 if (varbuf[n] == '\n') {
5780 buf_len = dp - varbuf;
5782 while (dp < varbuf + n)
5789 EXAMPLE: nvram_array
5792 Use carriage return at the end of each assignment,
5793 and an empty string with
5794 carriage return at the end of array.
5797 unsigned char nvram_array[] = {"name1=value1\n",
5798 "name2=value2\n", "\n"};
5799 Hex values start with 0x, and mac addr format: xx:xx:xx:xx:xx:xx.
5801 Search "EXAMPLE: nvram_array" to see how the array is activated.
5804 void dhd_bus_set_nvram_params(struct dhd_bus *bus, const char *nvram_params)
5806 bus->nvram_params = nvram_params;
5809 static int dhdsdio_download_nvram(struct dhd_bus *bus)
5814 char *memblock = NULL;
5817 bool nvram_file_exists;
5819 nv_path = bus->nv_path;
5821 nvram_file_exists = ((nv_path != NULL) && (nv_path[0] != '\0'));
5822 if (!nvram_file_exists && (bus->nvram_params == NULL))
5825 if (nvram_file_exists) {
5826 image = dhd_os_open_image(nv_path);
5831 memblock = kmalloc(MEMBLOCK, GFP_ATOMIC);
5832 if (memblock == NULL) {
5833 DHD_ERROR(("%s: Failed to allocate memory %d bytes\n",
5834 __func__, MEMBLOCK));
5838 /* Download variables */
5839 if (nvram_file_exists) {
5840 len = dhd_os_get_image_block(memblock, MEMBLOCK, image);
5842 len = strlen(bus->nvram_params);
5843 ASSERT(len <= MEMBLOCK);
5846 memcpy(memblock, bus->nvram_params, len);
5849 if (len > 0 && len < MEMBLOCK) {
5850 bufp = (char *)memblock;
5852 len = process_nvram_vars(bufp, len);
5856 bcmerror = dhdsdio_downloadvars(bus, memblock, len + 1);
5858 DHD_ERROR(("%s: error downloading vars: %d\n",
5859 __func__, bcmerror));
5862 DHD_ERROR(("%s: error reading nvram file: %d\n",
5871 dhd_os_close_image(image);
5876 static int _dhdsdio_download_firmware(struct dhd_bus *bus)
5880 bool embed = false; /* download embedded firmware */
5881 bool dlok = false; /* download firmware succeeded */
5883 /* Out immediately if no image to download */
5884 if ((bus->fw_path == NULL) || (bus->fw_path[0] == '\0')) {
5885 #ifdef BCMEMBEDIMAGE
5892 /* Keep arm in reset */
5893 if (dhdsdio_download_state(bus, true)) {
5894 DHD_ERROR(("%s: error placing ARM core in reset\n", __func__));
5898 /* External image takes precedence if specified */
5899 if ((bus->fw_path != NULL) && (bus->fw_path[0] != '\0')) {
5900 if (dhdsdio_download_code_file(bus, bus->fw_path)) {
5901 DHD_ERROR(("%s: dongle image file download failed\n",
5903 #ifdef BCMEMBEDIMAGE
5913 #ifdef BCMEMBEDIMAGE
5915 if (dhdsdio_download_code_array(bus)) {
5916 DHD_ERROR(("%s: dongle image array download failed\n",
5925 DHD_ERROR(("%s: dongle image download failed\n", __func__));
5929 /* EXAMPLE: nvram_array */
5930 /* If a valid nvram_arry is specified as above, it can be passed
5932 /* dhd_bus_set_nvram_params(bus, (char *)&nvram_array); */
5934 /* External nvram takes precedence if specified */
5935 if (dhdsdio_download_nvram(bus)) {
5936 DHD_ERROR(("%s: dongle nvram file download failed\n",
5940 /* Take arm out of reset */
5941 if (dhdsdio_download_state(bus, false)) {
5942 DHD_ERROR(("%s: error getting out of ARM core reset\n",
5955 dhd_bcmsdh_send_buf(dhd_bus_t *bus, u32 addr, uint fn, uint flags,
5956 u8 *buf, uint nbytes, struct sk_buff *pkt,
5957 bcmsdh_cmplt_fn_t complete, void *handle)
5959 return bcmsdh_send_buf
5960 (bus->sdh, addr, fn, flags, buf, nbytes, pkt, complete,
5964 uint dhd_bus_chip(struct dhd_bus *bus)
5966 ASSERT(bus->ci != NULL);
5967 return bus->ci->chip;
5970 void *dhd_bus_pub(struct dhd_bus *bus)
5975 void *dhd_bus_txq(struct dhd_bus *bus)
5980 uint dhd_bus_hdrlen(struct dhd_bus *bus)
5982 return SDPCM_HDRLEN;
5985 int dhd_bus_devreset(dhd_pub_t *dhdp, u8 flag)
5993 if (!bus->dhd->dongle_reset) {
5994 /* Expect app to have torn down any
5995 connection before calling */
5996 /* Stop the bus, disable F2 */
5997 dhd_bus_stop(bus, false);
5999 /* Clean tx/rx buffer pointers,
6000 detach from the dongle */
6001 dhdsdio_release_dongle(bus);
6003 bus->dhd->dongle_reset = true;
6004 bus->dhd->up = false;
6006 DHD_TRACE(("%s: WLAN OFF DONE\n", __func__));
6007 /* App can now remove power from device */
6011 /* App must have restored power to device before calling */
6013 DHD_TRACE(("\n\n%s: == WLAN ON ==\n", __func__));
6015 if (bus->dhd->dongle_reset) {
6017 /* Reset SD client */
6018 bcmsdh_reset(bus->sdh);
6020 /* Attempt to re-attach & download */
6021 if (dhdsdio_probe_attach(bus, bus->sdh,
6022 (u32 *) SI_ENUM_BASE,
6024 /* Attempt to download binary to the dongle */
6025 if (dhdsdio_probe_init
6027 && dhdsdio_download_firmware(bus,
6030 /* Re-init bus, enable F2 transfer */
6031 dhd_bus_init((dhd_pub_t *) bus->dhd,
6034 #if defined(OOB_INTR_ONLY)
6035 dhd_enable_oob_intr(bus, true);
6036 #endif /* defined(OOB_INTR_ONLY) */
6038 bus->dhd->dongle_reset = false;
6039 bus->dhd->up = true;
6041 DHD_TRACE(("%s: WLAN ON DONE\n",
6048 bcmerror = -EISCONN;
6049 DHD_ERROR(("%s: Set DEVRESET=false invoked when device "
6050 "is on\n", __func__));
6058 dhdsdio_chip_recognition(bcmsdh_info_t *sdh, struct chip_info *ci, void *regs)
6064 * Chipid is assume to be at offset 0 from regs arg
6065 * For different chiptypes or old sdio hosts w/o chipcommon,
6066 * other ways of recognition should be added here.
6068 ci->cccorebase = (u32)regs;
6069 regdata = bcmsdh_reg_read(sdh, CORE_CC_REG(ci->cccorebase, chipid), 4);
6070 ci->chip = regdata & CID_ID_MASK;
6071 ci->chiprev = (regdata & CID_REV_MASK) >> CID_REV_SHIFT;
6073 DHD_INFO(("%s: chipid=0x%x chiprev=%d\n",
6074 __func__, ci->chip, ci->chiprev));
6076 /* Address of cores for new chips should be added here */
6078 case BCM4329_CHIP_ID:
6079 ci->buscorebase = BCM4329_CORE_BUS_BASE;
6080 ci->ramcorebase = BCM4329_CORE_SOCRAM_BASE;
6081 ci->armcorebase = BCM4329_CORE_ARM_BASE;
6082 ci->ramsize = BCM4329_RAMSIZE;
6085 DHD_ERROR(("%s: chipid 0x%x is not supported\n",
6086 __func__, ci->chip));
6090 regdata = bcmsdh_reg_read(sdh,
6091 CORE_SB(ci->cccorebase, sbidhigh), 4);
6092 ci->ccrev = SBCOREREV(regdata);
6094 regdata = bcmsdh_reg_read(sdh,
6095 CORE_CC_REG(ci->cccorebase, pmucapabilities), 4);
6096 ci->pmurev = regdata & PCAP_REV_MASK;
6098 regdata = bcmsdh_reg_read(sdh, CORE_SB(ci->buscorebase, sbidhigh), 4);
6099 ci->buscorerev = SBCOREREV(regdata);
6100 ci->buscoretype = (regdata & SBIDH_CC_MASK) >> SBIDH_CC_SHIFT;
6102 DHD_INFO(("%s: ccrev=%d, pmurev=%d, buscore rev/type=%d/0x%x\n",
6103 __func__, ci->ccrev, ci->pmurev,
6104 ci->buscorerev, ci->buscoretype));
6106 /* get chipcommon capabilites */
6107 ci->cccaps = bcmsdh_reg_read(sdh,
6108 CORE_CC_REG(ci->cccorebase, capabilities), 4);
6114 dhdsdio_chip_disablecore(bcmsdh_info_t *sdh, u32 corebase)
6118 regdata = bcmsdh_reg_read(sdh,
6119 CORE_SB(corebase, sbtmstatelow), 4);
6120 if (regdata & SBTML_RESET)
6123 regdata = bcmsdh_reg_read(sdh,
6124 CORE_SB(corebase, sbtmstatelow), 4);
6125 if ((regdata & (SICF_CLOCK_EN << SBTML_SICF_SHIFT)) != 0) {
6127 * set target reject and spin until busy is clear
6128 * (preserve core-specific bits)
6130 regdata = bcmsdh_reg_read(sdh,
6131 CORE_SB(corebase, sbtmstatelow), 4);
6132 bcmsdh_reg_write(sdh, CORE_SB(corebase, sbtmstatelow), 4,
6133 regdata | SBTML_REJ);
6135 regdata = bcmsdh_reg_read(sdh,
6136 CORE_SB(corebase, sbtmstatelow), 4);
6138 SPINWAIT((bcmsdh_reg_read(sdh,
6139 CORE_SB(corebase, sbtmstatehigh), 4) &
6140 SBTMH_BUSY), 100000);
6142 regdata = bcmsdh_reg_read(sdh,
6143 CORE_SB(corebase, sbtmstatehigh), 4);
6144 if (regdata & SBTMH_BUSY)
6145 DHD_ERROR(("%s: ARM core still busy\n", __func__));
6147 regdata = bcmsdh_reg_read(sdh,
6148 CORE_SB(corebase, sbidlow), 4);
6149 if (regdata & SBIDL_INIT) {
6150 regdata = bcmsdh_reg_read(sdh,
6151 CORE_SB(corebase, sbimstate), 4) |
6153 bcmsdh_reg_write(sdh,
6154 CORE_SB(corebase, sbimstate), 4,
6156 regdata = bcmsdh_reg_read(sdh,
6157 CORE_SB(corebase, sbimstate), 4);
6159 SPINWAIT((bcmsdh_reg_read(sdh,
6160 CORE_SB(corebase, sbimstate), 4) &
6164 /* set reset and reject while enabling the clocks */
6165 bcmsdh_reg_write(sdh,
6166 CORE_SB(corebase, sbtmstatelow), 4,
6167 (((SICF_FGC | SICF_CLOCK_EN) << SBTML_SICF_SHIFT) |
6168 SBTML_REJ | SBTML_RESET));
6169 regdata = bcmsdh_reg_read(sdh,
6170 CORE_SB(corebase, sbtmstatelow), 4);
6173 /* clear the initiator reject bit */
6174 regdata = bcmsdh_reg_read(sdh,
6175 CORE_SB(corebase, sbidlow), 4);
6176 if (regdata & SBIDL_INIT) {
6177 regdata = bcmsdh_reg_read(sdh,
6178 CORE_SB(corebase, sbimstate), 4) &
6180 bcmsdh_reg_write(sdh,
6181 CORE_SB(corebase, sbimstate), 4,
6186 /* leave reset and reject asserted */
6187 bcmsdh_reg_write(sdh, CORE_SB(corebase, sbtmstatelow), 4,
6188 (SBTML_REJ | SBTML_RESET));
6193 dhdsdio_chip_attach(struct dhd_bus *bus, void *regs)
6195 struct chip_info *ci;
6199 DHD_TRACE(("%s: Enter\n", __func__));
6201 /* alloc chip_info_t */
6202 ci = kmalloc(sizeof(struct chip_info), GFP_ATOMIC);
6204 DHD_ERROR(("%s: malloc failed!\n", __func__));
6208 memset((unsigned char *)ci, 0, sizeof(struct chip_info));
6210 /* bus/core/clk setup for register access */
6211 /* Try forcing SDIO core to do ALPAvail request only */
6212 clkset = SBSDIO_FORCE_HW_CLKREQ_OFF | SBSDIO_ALP_AVAIL_REQ;
6213 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
6216 DHD_ERROR(("%s: error writing for HT off\n", __func__));
6220 /* If register supported, wait for ALPAvail and then force ALP */
6221 /* This may take up to 15 milliseconds */
6222 clkval = bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1,
6223 SBSDIO_FUNC1_CHIPCLKCSR, NULL);
6224 if ((clkval & ~SBSDIO_AVBITS) == clkset) {
6226 bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1,
6227 SBSDIO_FUNC1_CHIPCLKCSR,
6229 !SBSDIO_ALPAV(clkval)),
6230 PMU_MAX_TRANSITION_DLY);
6231 if (!SBSDIO_ALPAV(clkval)) {
6232 DHD_ERROR(("%s: timeout on ALPAV wait, clkval 0x%02x\n",
6237 clkset = SBSDIO_FORCE_HW_CLKREQ_OFF |
6239 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1,
6240 SBSDIO_FUNC1_CHIPCLKCSR,
6244 DHD_ERROR(("%s: ChipClkCSR access: wrote 0x%02x read 0x%02x\n",
6245 __func__, clkset, clkval));
6250 /* Also, disable the extra SDIO pull-ups */
6251 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_SDIOPULLUP, 0,
6254 err = dhdsdio_chip_recognition(bus->sdh, ci, regs);
6259 * Make sure any on-chip ARM is off (in case strapping is wrong),
6260 * or downloaded code was already running.
6262 dhdsdio_chip_disablecore(bus->sdh, ci->armcorebase);
6264 bcmsdh_reg_write(bus->sdh,
6265 CORE_CC_REG(ci->cccorebase, gpiopullup), 4, 0);
6266 bcmsdh_reg_write(bus->sdh,
6267 CORE_CC_REG(ci->cccorebase, gpiopulldown), 4, 0);
6269 /* Disable F2 to clear any intermediate frame state on the dongle */
6270 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_0, SDIOD_CCCR_IOEN,
6271 SDIO_FUNC_ENABLE_1, NULL);
6273 /* WAR: cmd52 backplane read so core HW will drop ALPReq */
6274 clkval = bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1,
6277 /* Done with backplane-dependent accesses, can drop clock... */
6278 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR, 0,
6290 dhdsdio_chip_resetcore(bcmsdh_info_t *sdh, u32 corebase)
6295 * Must do the disable sequence first to work for
6296 * arbitrary current core state.
6298 dhdsdio_chip_disablecore(sdh, corebase);
6301 * Now do the initialization sequence.
6302 * set reset while enabling the clock and
6303 * forcing them on throughout the core
6305 bcmsdh_reg_write(sdh, CORE_SB(corebase, sbtmstatelow), 4,
6306 ((SICF_FGC | SICF_CLOCK_EN) << SBTML_SICF_SHIFT) |
6310 regdata = bcmsdh_reg_read(sdh, CORE_SB(corebase, sbtmstatehigh), 4);
6311 if (regdata & SBTMH_SERR)
6312 bcmsdh_reg_write(sdh, CORE_SB(corebase, sbtmstatehigh), 4, 0);
6314 regdata = bcmsdh_reg_read(sdh, CORE_SB(corebase, sbimstate), 4);
6315 if (regdata & (SBIM_IBE | SBIM_TO))
6316 bcmsdh_reg_write(sdh, CORE_SB(corebase, sbimstate), 4,
6317 regdata & ~(SBIM_IBE | SBIM_TO));
6319 /* clear reset and allow it to propagate throughout the core */
6320 bcmsdh_reg_write(sdh, CORE_SB(corebase, sbtmstatelow), 4,
6321 (SICF_FGC << SBTML_SICF_SHIFT) |
6322 (SICF_CLOCK_EN << SBTML_SICF_SHIFT));
6325 /* leave clock enabled */
6326 bcmsdh_reg_write(sdh, CORE_SB(corebase, sbtmstatelow), 4,
6327 (SICF_CLOCK_EN << SBTML_SICF_SHIFT));
6331 /* SDIO Pad drive strength to select value mappings */
6332 struct sdiod_drive_str {
6333 u8 strength; /* Pad Drive Strength in mA */
6334 u8 sel; /* Chip-specific select value */
6337 /* SDIO Drive Strength to sel value table for PMU Rev 1 */
6338 static const struct sdiod_drive_str sdiod_drive_strength_tab1[] = {
6346 /* SDIO Drive Strength to sel value table for PMU Rev 2, 3 */
6347 static const struct sdiod_drive_str sdiod_drive_strength_tab2[] = {
6358 /* SDIO Drive Strength to sel value table for PMU Rev 8 (1.8V) */
6359 static const struct sdiod_drive_str sdiod_drive_strength_tab3[] = {
6371 #define SDIOD_DRVSTR_KEY(chip, pmu) (((chip) << 16) | (pmu))
6374 dhdsdio_sdiod_drive_strength_init(struct dhd_bus *bus, u32 drivestrength) {
6375 struct sdiod_drive_str *str_tab = NULL;
6380 if (!(bus->ci->cccaps & CC_CAP_PMU))
6383 switch (SDIOD_DRVSTR_KEY(bus->ci->chip, bus->ci->pmurev)) {
6384 case SDIOD_DRVSTR_KEY(BCM4325_CHIP_ID, 1):
6385 str_tab = (struct sdiod_drive_str *)&sdiod_drive_strength_tab1;
6386 str_mask = 0x30000000;
6389 case SDIOD_DRVSTR_KEY(BCM4325_CHIP_ID, 2):
6390 case SDIOD_DRVSTR_KEY(BCM4325_CHIP_ID, 3):
6391 str_tab = (struct sdiod_drive_str *)&sdiod_drive_strength_tab2;
6392 str_mask = 0x00003800;
6395 case SDIOD_DRVSTR_KEY(BCM4336_CHIP_ID, 8):
6396 str_tab = (struct sdiod_drive_str *)&sdiod_drive_strength_tab3;
6397 str_mask = 0x00003800;
6401 DHD_ERROR(("No SDIO Drive strength init"
6402 "done for chip %s rev %d pmurev %d\n",
6403 bcm_chipname(bus->ci->chip, chn, 8),
6404 bus->ci->chiprev, bus->ci->pmurev));
6408 if (str_tab != NULL) {
6409 u32 drivestrength_sel = 0;
6413 for (i = 0; str_tab[i].strength != 0; i++) {
6414 if (drivestrength >= str_tab[i].strength) {
6415 drivestrength_sel = str_tab[i].sel;
6420 bcmsdh_reg_write(bus->sdh,
6421 CORE_CC_REG(bus->ci->cccorebase, chipcontrol_addr),
6423 cc_data_temp = bcmsdh_reg_read(bus->sdh,
6424 CORE_CC_REG(bus->ci->cccorebase, chipcontrol_addr), 4);
6425 cc_data_temp &= ~str_mask;
6426 drivestrength_sel <<= str_shift;
6427 cc_data_temp |= drivestrength_sel;
6428 bcmsdh_reg_write(bus->sdh,
6429 CORE_CC_REG(bus->ci->cccorebase, chipcontrol_addr),
6432 DHD_INFO(("SDIO: %dmA drive strength selected, set to 0x%08x\n",
6433 drivestrength, cc_data_temp));
6438 dhdsdio_chip_detach(struct dhd_bus *bus)
6440 DHD_TRACE(("%s: Enter\n", __func__));