blob: 5b1b4c2ef6d16c0eccee2305ddd06fc4e4e7162a [file] [log] [blame]
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001/*
2 * Copyright (c) 2010 Broadcom Corporation
3 *
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.
7 *
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.
15 */
16
Greg Kroah-Hartmana1c16ed2010-10-21 11:17:44 -070017#include <linux/types.h>
Arend van Spriel34227312011-05-10 22:25:32 +020018#include <linux/kernel.h>
19#include <linux/printk.h>
Stanislav Fomichevbe1c09f2011-03-28 01:31:36 +040020#include <linux/pci_ids.h>
Brett Rudleyc6ac24e2010-10-26 11:55:23 -070021#include <linux/netdevice.h>
Arend van Spriel34227312011-05-10 22:25:32 +020022#include <bcmdefs.h>
Henry Ptasinskicf2b4482010-09-20 22:33:12 -070023#include <bcmsdh.h>
24
25#ifdef BCMEMBEDIMAGE
26#include BCMEMBEDIMAGE
27#endif /* BCMEMBEDIMAGE */
28
29#include <bcmdefs.h>
30#include <bcmutils.h>
Henry Ptasinskicf2b4482010-09-20 22:33:12 -070031#include <bcmdevs.h>
32
Roland Vossen189aed02011-06-01 13:45:07 +020033#include <bcmsoc.h>
Henry Ptasinskicf2b4482010-09-20 22:33:12 -070034#ifdef DHD_DEBUG
Roland Vossenf1c7a082011-06-01 13:45:09 +020035
36/* ARM trap handling */
37
38/* Trap types defined by ARM (see arminc.h) */
39
40/* Trap locations in lo memory */
41#define TRAP_STRIDE 4
42#define FIRST_TRAP TR_RST
43#define LAST_TRAP (TR_FIQ * TRAP_STRIDE)
44
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__ */
50
51/* The trap structure is defined here as offsets for assembly */
52#define TR_TYPE 0x00
53#define TR_EPC 0x04
54#define TR_CPSR 0x08
55#define TR_SPSR 0x0c
56#define TR_REGS 0x10
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)
61
62#define TRAP_T_SIZE 80
63
64#ifndef _LANGUAGE_ASSEMBLY
65
66typedef struct _trap_struct {
67 u32 type;
68 u32 epc;
69 u32 cpsr;
70 u32 spsr;
71 u32 r0;
72 u32 r1;
73 u32 r2;
74 u32 r3;
75 u32 r4;
76 u32 r5;
77 u32 r6;
78 u32 r7;
79 u32 r8;
80 u32 r9;
81 u32 r10;
82 u32 r11;
83 u32 r12;
84 u32 r13;
85 u32 r14;
86 u32 pc;
87} trap_t;
88
89#endif /* !_LANGUAGE_ASSEMBLY */
90
91#define CBUF_LEN (128)
92
93#define LOG_BUF_LEN 1024
94
95typedef struct {
96 u32 buf; /* Can't be pointer on (64-bit) hosts */
97 uint buf_size;
98 uint idx;
99 char *_buf_compat; /* Redundant pointer for backward compat. */
100} rte_log_t;
101
102typedef struct {
103 /* Virtual UART
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.
111 */
112 volatile uint vcons_in;
113 volatile uint vcons_out;
114
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
119 * polls.
120 */
121 rte_log_t log;
122
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.
128 */
129 uint cbuf_idx;
130 char cbuf[CBUF_LEN];
131} rte_cons_t;
132
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700133#endif /* DHD_DEBUG */
Roland Vossen745c9e62011-06-01 13:45:30 +0200134#include <chipcommon.h>
Roland Vossen189aed02011-06-01 13:45:07 +0200135#include <sbdma.h>
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700136
137#include <sdio.h>
138#include <sbsdio.h>
139#include <sbsdpcmdev.h>
140#include <bcmsdpcm.h>
141
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700142#include <dngl_stats.h>
143#include <dhd.h>
144#include <dhd_bus.h>
145#include <dhd_proto.h>
146#include <dhd_dbg.h>
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700147#include <sdiovar.h>
Franky Lincb63e4c2011-04-25 15:45:08 -0700148#include <bcmchip.h>
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700149
150#ifndef DHDSDIO_MEM_DUMP_FNAME
151#define DHDSDIO_MEM_DUMP_FNAME "mem_dump"
152#endif
153
Grant Grundler26a71a42011-03-09 10:41:25 -0800154#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 */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700157#define PRIOMASK 7
158
159#define TXRETRIES 2 /* # of retries for tx frames */
160
161#if defined(CONFIG_MACH_SANDGATE2G)
162#define DHD_RXBOUND 250 /* Default for max rx frames in
163 one scheduling */
164#else
165#define DHD_RXBOUND 50 /* Default for max rx frames in
166 one scheduling */
167#endif /* defined(CONFIG_MACH_SANDGATE2G) */
168
169#define DHD_TXBOUND 20 /* Default for max tx frames in
170 one scheduling */
171
172#define DHD_TXMINMAX 1 /* Max tx frames if rx still pending */
173
174#define MEMBLOCK 2048 /* Block size used for downloading
175 of dongle image */
176#define MAX_DATA_BUF (32 * 1024) /* Must be large enough to hold
177 biggest possible glom */
178
179/* Packet alignment for most efficient SDIO (can change based on platform) */
180#ifndef DHD_SDALIGN
181#define DHD_SDALIGN 32
182#endif
183#if !ISPOWEROF2(DHD_SDALIGN)
184#error DHD_SDALIGN is not a power of 2!
185#endif
186
187#ifndef DHD_FIRSTREAD
188#define DHD_FIRSTREAD 32
189#endif
190#if !ISPOWEROF2(DHD_FIRSTREAD)
191#error DHD_FIRSTREAD is not a power of 2!
192#endif
193
194/* Total length of frame header for dongle protocol */
195#define SDPCM_HDRLEN (SDPCM_FRAMETAG_LEN + SDPCM_SWHEADER_LEN)
196#ifdef SDTEST
197#define SDPCM_RESERVE (SDPCM_HDRLEN + SDPCM_TEST_HDRLEN + DHD_SDALIGN)
198#else
199#define SDPCM_RESERVE (SDPCM_HDRLEN + DHD_SDALIGN)
200#endif
201
202/* Space for header read, limit for data packets */
203#ifndef MAX_HDR_READ
204#define MAX_HDR_READ 32
205#endif
206#if !ISPOWEROF2(MAX_HDR_READ)
207#error MAX_HDR_READ is not a power of 2!
208#endif
209
210#define MAX_RX_DATASZ 2048
211
212/* Maximum milliseconds to wait for F2 to come up */
213#define DHD_WAIT_F2RDY 3000
214
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).
219 */
220#if (PMU_MAX_TRANSITION_DLY <= 1000000)
221#undef PMU_MAX_TRANSITION_DLY
222#define PMU_MAX_TRANSITION_DLY 1000000
223#endif
224
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)
229
230/* Flags for SDH calls */
231#define F2SYNC (SDIO_REQ_4BYTE | SDIO_REQ_FIXED)
232
Roland Vossenb6fe70c2011-06-01 13:45:19 +0200233/* sbimstate */
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) */
238
239/* sbtmstatelow */
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 */
244
245#define SBTML_SICF_SHIFT 16 /* Shift to locate the SI control flags in sbtml */
246
247/* sbtmstatehigh */
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) */
252
253#define SBTMH_SISF_SHIFT 16 /* Shift to locate the SI status flags in sbtmh */
254
255/* sbidlow */
256#define SBIDL_INIT 0x80 /* initiator */
257
258/* sbidhigh */
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
268
Arend van Spriel70dfb582011-02-25 16:39:17 +0100269/*
270 * Conversion of 802.1D priority to precedence level
271 */
272#define PRIO2PREC(prio) \
273 (((prio) == PRIO_8021D_NONE || (prio) == PRIO_8021D_BE) ? \
274 ((prio^2)) : (prio))
275
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700276DHD_SPINWAIT_SLEEP_INIT(sdioh_spinwait_sleep);
277extern int dhdcdc_set_ioctl(dhd_pub_t *dhd, int ifidx, uint cmd, void *buf,
278 uint len);
279
Roland Vossenb6fe70c2011-06-01 13:45:19 +0200280/* 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))
285
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700286#ifdef DHD_DEBUG
287/* Device console log buffer state */
288typedef struct dhd_console {
289 uint count; /* Poll interval msec counter */
290 uint log_addr; /* Log struct address (fixed) */
Roland Vossen70963f92011-06-01 13:45:08 +0200291 rte_log_t log; /* Log struct (host copy) */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700292 uint bufsize; /* Size of log buffer */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700293 u8 *buf; /* Log buffer (host copy) */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700294 uint last; /* Last buffer read index */
295} dhd_console_t;
296#endif /* DHD_DEBUG */
297
Franky Lincb63e4c2011-04-25 15:45:08 -0700298/* misc chip info needed by some of the routines */
299struct chip_info {
300 u32 chip;
301 u32 chiprev;
302 u32 cccorebase;
303 u32 ccrev;
304 u32 cccaps;
305 u32 buscorebase;
306 u32 buscorerev;
307 u32 buscoretype;
308 u32 ramcorebase;
309 u32 armcorebase;
310 u32 pmurev;
Franky Linc05df632011-04-25 19:34:07 -0700311 u32 ramsize;
Franky Lincb63e4c2011-04-25 15:45:08 -0700312};
313
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700314/* Private data for SDIO bus interaction */
315typedef struct dhd_bus {
316 dhd_pub_t *dhd;
317
318 bcmsdh_info_t *sdh; /* Handle for BCMSDH calls */
Franky Lincb63e4c2011-04-25 15:45:08 -0700319 struct chip_info *ci; /* Chip info struct */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700320 char *vars; /* Variables (from CIS and/or other) */
321 uint varsz; /* Size of variables buffer */
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -0700322 u32 sbaddr; /* Current SB window pointer (-1, invalid) */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700323
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 */
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -0700328 u32 ramsize; /* Size of RAM in SOCRAM (bytes) */
329 u32 orig_ramsize; /* Size of RAM in SOCRAM (bytes) */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700330
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -0700331 u32 bus; /* gSPI or SDIO bus */
332 u32 hostintmask; /* Copy of Host Interrupt Mask */
333 u32 intstatus; /* Intstatus bits (events) pending */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700334 bool dpc_sched; /* Indicates DPC schedule (intrpt rcvd) */
335 bool fcstate; /* State of dongle flow-control */
336
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -0700337 u16 cl_devid; /* cached devid for dhdsdio_probe_attach() */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700338 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. */
341
342 uint blocksize; /* Block size of SDIO transfers */
343 uint roundup; /* Max roundup limit */
344
345 struct pktq txq; /* Queue length used for flow-control */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700346 u8 flowcontrol; /* per prio flow control bitmask */
347 u8 tx_seq; /* Transmit sequence number (next) */
348 u8 tx_max; /* Maximum transmit sequence allowed */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700349
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700350 u8 hdrbuf[MAX_HDR_READ + DHD_SDALIGN];
351 u8 *rxhdr; /* Header of current rx frame (in hdrbuf) */
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -0700352 u16 nextlen; /* Next Read Len from last header */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700353 u8 rx_seq; /* Receive sequence number (expected) */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700354 bool rxskip; /* Skip receive (awaiting NAK ACK) */
355
Arend van Sprielc26b1372010-11-23 14:06:23 +0100356 struct sk_buff *glomd; /* Packet containing glomming descriptor */
357 struct sk_buff *glom; /* Packet chain for glommed superframe */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700358 uint glomerr; /* Glom packet read errors */
359
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700360 u8 *rxbuf; /* Buffer for receiving control packets */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700361 uint rxblen; /* Allocated length of rxbuf */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700362 u8 *rxctl; /* Aligned pointer into rxbuf */
363 u8 *databuf; /* Buffer for receiving big glom packet */
364 u8 *dataptr; /* Aligned pointer into databuf */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700365 uint rxlen; /* Length of valid data in buffer */
366
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700367 u8 sdpcm_ver; /* Bus protocol reported by dongle */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700368
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 */
379
380#ifdef DHD_DEBUG
381 dhd_console_t console; /* Console output polling support */
382 uint console_addr; /* Console address from shared struct */
383#endif /* DHD_DEBUG */
384
385 uint regfails; /* Count of R_REG/W_REG failures */
386
387 uint clkstate; /* State of sd and backplane clock(s) */
388 bool activity; /* Activity flag for clock down */
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -0700389 s32 idletime; /* Control for activity timeout */
390 s32 idlecount; /* Activity timeout counter */
391 s32 idleclock; /* How to set bus driver when idle */
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -0700392 s32 sd_rxchain; /* If bcmsdh api accepts PKT chains */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700393 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 */
401 bool usebufpool;
402
403#ifdef SDTEST
404 /* external loopback */
405 bool ext_loop;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700406 u8 loopid;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700407
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 */
417
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 */
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -0700424 u16 pktgen_len; /* Length of next packet to send */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700425#endif /* SDTEST */
426
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 */
446
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700447 u8 *ctrl_frame_buf;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -0700448 u32 ctrl_frame_len;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700449 bool ctrl_frame_stat;
450} dhd_bus_t;
451
Roland Vossenb6fe70c2011-06-01 13:45:19 +0200452#ifndef _LANGUAGE_ASSEMBLY
453
454typedef volatile struct _sbconfig {
455 u32 PAD[2];
456 u32 sbipsflag; /* initiator port ocp slave flag */
457 u32 PAD[3];
458 u32 sbtpsflag; /* target port ocp slave flag */
459 u32 PAD[11];
460 u32 sbtmerrloga; /* (sonics >= 2.3) */
461 u32 PAD;
462 u32 sbtmerrlog; /* (sonics >= 2.3) */
463 u32 PAD[3];
464 u32 sbadmatch3; /* address match3 */
465 u32 PAD;
466 u32 sbadmatch2; /* address match2 */
467 u32 PAD;
468 u32 sbadmatch1; /* address match1 */
469 u32 PAD[7];
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 */
475 u32 PAD;
476 u32 sbimconfiglow; /* initiator configuration */
477 u32 sbimconfighigh; /* initiator configuration */
478 u32 sbadmatch0; /* address match0 */
479 u32 PAD;
480 u32 sbtmconfiglow; /* target configuration */
481 u32 sbtmconfighigh; /* target configuration */
482 u32 sbbconfig; /* broadcast configuration */
483 u32 PAD;
484 u32 sbbstate; /* broadcast state */
485 u32 PAD[3];
486 u32 sbactcnfg; /* activate configuration */
487 u32 PAD[3];
488 u32 sbflagst; /* current sbflags */
489 u32 PAD[3];
490 u32 sbidlow; /* identification */
491 u32 sbidhigh; /* identification */
492} sbconfig_t;
493
494#endif /* _LANGUAGE_ASSEMBLY */
495
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700496/* clkstate */
497#define CLK_NONE 0
498#define CLK_SDONLY 1
499#define CLK_PENDING 2 /* Not used yet */
500#define CLK_AVAIL 3
501
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -0700502#define DHD_NOPMU(dhd) (false)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700503
504#ifdef DHD_DEBUG
505static int qcount[NUMPRIO];
506static int tx_packets[NUMPRIO];
507#endif /* DHD_DEBUG */
508
509/* Deferred transmit */
510const uint dhd_deferred_tx = 1;
511
512extern uint dhd_watchdog_ms;
513extern void dhd_os_wd_timer(void *bus, uint wdtick);
514
515/* Tx/Rx bounds */
516uint dhd_txbound;
517uint dhd_rxbound;
518uint dhd_txminmax;
519
520/* override the RAM size if possible */
521#define DONGLE_MIN_MEMSIZE (128 * 1024)
522int dhd_dongle_memsize;
523
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700524static bool dhd_alignctl;
525
526static bool sd1idle;
527
528static bool retrydata;
529#define RETRYCHAN(chan) (((chan) == SDPCM_EVENT_CHANNEL) || retrydata)
530
531static const uint watermark = 8;
532static const uint firstread = DHD_FIRSTREAD;
533
534#define HDATLEN (firstread - (SDPCM_HDRLEN))
535
536/* Retry count for register access failures */
537static const uint retry_limit = 2;
538
539/* Force even SD lengths (some host controllers mess up on odd bytes) */
540static bool forcealign;
541
542#define ALIGNMENT 4
543
544#if defined(OOB_INTR_ONLY) && defined(HW_OOB)
545extern void bcmsdh_enable_hw_oob_intr(void *sdh, bool enable);
546#endif
547
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) */
Arend van Spriel3c9d4c32011-03-02 21:18:48 +0100551#define PKTALIGN(_p, _len, _align) \
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700552 do { \
553 uint datalign; \
Arend van Spriel54991ad2010-11-23 14:06:24 +0100554 datalign = (unsigned long)((_p)->data); \
555 datalign = roundup(datalign, (_align)) - datalign; \
556 ASSERT(datalign < (_align)); \
557 ASSERT((_p)->len >= ((_len) + datalign)); \
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700558 if (datalign) \
Arend van Spriel54991ad2010-11-23 14:06:24 +0100559 skb_pull((_p), datalign); \
560 __skb_trim((_p), (_len)); \
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700561 } while (0)
562
563/* Limit on rounding up frames */
564static const uint max_roundup = 512;
565
566/* Try doing readahead */
567static bool dhd_readahead;
568
569/* To check if there's window offered */
570#define DATAOK(bus) \
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700571 (((u8)(bus->tx_max - bus->tx_seq) != 0) && \
572 (((u8)(bus->tx_max - bus->tx_seq) & 0x80) == 0))
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700573
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) \
577do { \
578 retryvar = 0; \
579 do { \
Arend van Sprielff31c542011-03-01 10:56:54 +0100580 regvar = R_REG(regaddr); \
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700581 } while (bcmsdh_regfail(bus->sdh) && (++retryvar <= retry_limit)); \
582 if (retryvar) { \
583 bus->regfails += (retryvar-1); \
584 if (retryvar > retry_limit) { \
585 DHD_ERROR(("%s: FAILED" #regvar "READ, LINE %d\n", \
586 __func__, __LINE__)); \
587 regvar = 0; \
588 } \
589 } \
590} while (0)
591
592#define W_SDREG(regval, regaddr, retryvar) \
593do { \
594 retryvar = 0; \
595 do { \
Arend van Sprielff31c542011-03-01 10:56:54 +0100596 W_REG(regaddr, regval); \
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700597 } while (bcmsdh_regfail(bus->sdh) && (++retryvar <= retry_limit)); \
598 if (retryvar) { \
599 bus->regfails += (retryvar-1); \
600 if (retryvar > retry_limit) \
601 DHD_ERROR(("%s: FAILED REGISTER WRITE, LINE %d\n", \
602 __func__, __LINE__)); \
603 } \
604} while (0)
605
606#define DHD_BUS SDIO_BUS
607
608#define PKT_AVAILABLE() (intstatus & I_HMB_FRAME_IND)
609
610#define HOSTINTMASK (I_HMB_SW_MASK | I_CHIPACTIVE)
611
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700612#ifdef SDTEST
613static void dhdsdio_testrcv(dhd_bus_t *bus, void *pkt, uint seq);
614static void dhdsdio_sdtest_set(dhd_bus_t *bus, bool start);
615#endif
616
617#ifdef DHD_DEBUG
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700618static int dhdsdio_checkdied(dhd_bus_t *bus, u8 *data, uint size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700619static int dhdsdio_mem_dump(dhd_bus_t *bus);
620#endif /* DHD_DEBUG */
621static int dhdsdio_download_state(dhd_bus_t *bus, bool enter);
622
Arend van Spriel3c9d4c32011-03-02 21:18:48 +0100623static void dhdsdio_release(dhd_bus_t *bus);
Arend van Spriel8da4a3a2011-03-02 21:18:42 +0100624static void dhdsdio_release_malloc(dhd_bus_t *bus);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700625static void dhdsdio_disconnect(void *ptr);
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -0700626static bool dhdsdio_chipmatch(u16 chipid);
Arend van Spriel8da4a3a2011-03-02 21:18:42 +0100627static bool dhdsdio_probe_attach(dhd_bus_t *bus, void *sdh,
628 void *regsva, u16 devid);
629static bool dhdsdio_probe_malloc(dhd_bus_t *bus, void *sdh);
630static bool dhdsdio_probe_init(dhd_bus_t *bus, void *sdh);
631static void dhdsdio_release_dongle(dhd_bus_t *bus);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700632
633static uint process_nvram_vars(char *varbuf, uint len);
634
635static void dhd_dongle_setmemsize(struct dhd_bus *bus, int mem_size);
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -0700636static int dhd_bcmsdh_send_buf(dhd_bus_t *bus, u32 addr, uint fn,
Arend van Sprielc26b1372010-11-23 14:06:23 +0100637 uint flags, u8 *buf, uint nbytes,
638 struct sk_buff *pkt, bcmsdh_cmplt_fn_t complete,
639 void *handle);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700640
Arend van Spriel8da4a3a2011-03-02 21:18:42 +0100641static bool dhdsdio_download_firmware(struct dhd_bus *bus, void *sdh);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700642static int _dhdsdio_download_firmware(struct dhd_bus *bus);
643
644static int dhdsdio_download_code_file(struct dhd_bus *bus, char *image_path);
645static int dhdsdio_download_nvram(struct dhd_bus *bus);
646#ifdef BCMEMBEDIMAGE
647static int dhdsdio_download_code_array(struct dhd_bus *bus);
648#endif
Franky Lineb5dc512011-04-25 19:34:04 -0700649static void dhdsdio_chip_disablecore(bcmsdh_info_t *sdh, u32 corebase);
Franky Lincb63e4c2011-04-25 15:45:08 -0700650static int dhdsdio_chip_attach(struct dhd_bus *bus, void *regs);
Franky Lineb5dc512011-04-25 19:34:04 -0700651static void dhdsdio_chip_resetcore(bcmsdh_info_t *sdh, u32 corebase);
Franky Lin5d0d7a92011-04-25 19:34:05 -0700652static void dhdsdio_sdiod_drive_strength_init(struct dhd_bus *bus,
653 u32 drivestrength);
Franky Lincee3cf42011-04-25 19:34:06 -0700654static void dhdsdio_chip_detach(struct dhd_bus *bus);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700655
Grant Grundler4b455e02011-05-04 09:59:47 -0700656/* Packet free applicable unconditionally for sdio and sdspi.
657 * Conditional if bufpool was present for gspi bus.
658 */
659static void dhdsdio_pktfree2(dhd_bus_t *bus, struct sk_buff *pkt)
660{
661 dhd_os_sdlock_rxq(bus->dhd);
662 if ((bus->bus != SPI_BUS) || bus->usebufpool)
Henry Ptasinskib33f0e22011-05-10 22:25:29 +0200663 bcm_pkt_buf_free_skb(pkt);
Grant Grundler4b455e02011-05-04 09:59:47 -0700664 dhd_os_sdunlock_rxq(bus->dhd);
665}
666
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700667static void dhd_dongle_setmemsize(struct dhd_bus *bus, int mem_size)
668{
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -0700669 s32 min_size = DONGLE_MIN_MEMSIZE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700670 /* 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) &&
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -0700674 (dhd_dongle_memsize < (s32) bus->orig_ramsize))
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700675 bus->ramsize = dhd_dongle_memsize;
676}
677
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -0700678static int dhdsdio_set_siaddr_window(dhd_bus_t *bus, u32 address)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700679{
680 int err = 0;
681 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_SBADDRLOW,
682 (address >> 8) & SBSDIO_SBADDRLOW_MASK, &err);
683 if (!err)
684 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_SBADDRMID,
685 (address >> 16) & SBSDIO_SBADDRMID_MASK, &err);
686 if (!err)
687 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_SBADDRHIGH,
688 (address >> 24) & SBSDIO_SBADDRHIGH_MASK,
689 &err);
690 return err;
691}
692
693/* Turn backplane clock on or off */
694static int dhdsdio_htclk(dhd_bus_t *bus, bool on, bool pendok)
695{
696 int err;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700697 u8 clkctl, clkreq, devctl;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700698 bcmsdh_info_t *sdh;
699
700 DHD_TRACE(("%s: Enter\n", __func__));
701
702#if defined(OOB_INTR_ONLY)
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -0700703 pendok = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700704#endif
705 clkctl = 0;
706 sdh = bus->sdh;
707
708 if (on) {
709 /* Request HT Avail */
710 clkreq =
711 bus->alp_only ? SBSDIO_ALP_AVAIL_REQ : SBSDIO_HT_AVAIL_REQ;
712
Franky Linc05df632011-04-25 19:34:07 -0700713 if ((bus->ci->chip == BCM4329_CHIP_ID)
714 && (bus->ci->chiprev == 0))
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700715 clkreq |= SBSDIO_FORCE_ALP;
716
717 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
718 clkreq, &err);
719 if (err) {
720 DHD_ERROR(("%s: HT Avail request error: %d\n",
721 __func__, err));
Roland Vossenb74ac122011-05-03 11:35:20 +0200722 return -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700723 }
724
Franky Linc05df632011-04-25 19:34:07 -0700725 if (pendok && ((bus->ci->buscoretype == PCMCIA_CORE_ID)
726 && (bus->ci->buscorerev == 9))) {
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -0700727 u32 dummy, retries;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700728 R_SDREG(dummy, &bus->regs->clockctlstatus, retries);
729 }
730
731 /* Check current status */
732 clkctl =
733 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
734 &err);
735 if (err) {
736 DHD_ERROR(("%s: HT Avail read error: %d\n",
737 __func__, err));
Roland Vossenb74ac122011-05-03 11:35:20 +0200738 return -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700739 }
740
741 /* Go to pending and await interrupt if appropriate */
742 if (!SBSDIO_CLKAV(clkctl, bus->alp_only) && pendok) {
743 /* Allow only clock-available interrupt */
744 devctl =
745 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
746 &err);
747 if (err) {
748 DHD_ERROR(("%s: Devctl error setting CA: %d\n",
749 __func__, err));
Roland Vossenb74ac122011-05-03 11:35:20 +0200750 return -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700751 }
752
753 devctl |= SBSDIO_DEVCTL_CA_INT_ONLY;
754 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
755 devctl, &err);
756 DHD_INFO(("CLKCTL: set PENDING\n"));
757 bus->clkstate = CLK_PENDING;
758
Roland Vossena1c5ad82011-04-11 15:16:24 +0200759 return 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700760 } else if (bus->clkstate == CLK_PENDING) {
761 /* Cancel CA-only interrupt filter */
762 devctl =
763 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
764 &err);
765 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
766 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
767 devctl, &err);
768 }
769
770 /* Otherwise, wait here (polling) for HT Avail */
771 if (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
772 SPINWAIT_SLEEP(sdioh_spinwait_sleep,
773 ((clkctl =
774 bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
775 SBSDIO_FUNC1_CHIPCLKCSR,
776 &err)),
777 !SBSDIO_CLKAV(clkctl, bus->alp_only)),
778 PMU_MAX_TRANSITION_DLY);
779 }
780 if (err) {
781 DHD_ERROR(("%s: HT Avail request error: %d\n",
782 __func__, err));
Roland Vossenb74ac122011-05-03 11:35:20 +0200783 return -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700784 }
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));
Roland Vossenb74ac122011-05-03 11:35:20 +0200788 return -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700789 }
790
791 /* Mark clock available */
792 bus->clkstate = CLK_AVAIL;
793 DHD_INFO(("CLKCTL: turned ON\n"));
794
795#if defined(DHD_DEBUG)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -0700796 if (bus->alp_only == true) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700797#if !defined(BCMLXSDMMC)
798 if (!SBSDIO_ALPONLY(clkctl)) {
799 DHD_ERROR(("%s: HT Clock, when ALP Only\n",
800 __func__));
801 }
802#endif /* !defined(BCMLXSDMMC) */
803 } else {
804 if (SBSDIO_ALPONLY(clkctl)) {
805 DHD_ERROR(("%s: HT Clock should be on.\n",
806 __func__));
807 }
808 }
809#endif /* defined (DHD_DEBUG) */
810
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -0700811 bus->activity = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700812 } else {
813 clkreq = 0;
814
815 if (bus->clkstate == CLK_PENDING) {
816 /* Cancel CA-only interrupt filter */
817 devctl =
818 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
819 &err);
820 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
821 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
822 devctl, &err);
823 }
824
825 bus->clkstate = CLK_SDONLY;
826 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
827 clkreq, &err);
828 DHD_INFO(("CLKCTL: turned OFF\n"));
829 if (err) {
830 DHD_ERROR(("%s: Failed access turning clock off: %d\n",
831 __func__, err));
Roland Vossenb74ac122011-05-03 11:35:20 +0200832 return -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700833 }
834 }
Roland Vossena1c5ad82011-04-11 15:16:24 +0200835 return 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700836}
837
838/* Change idle/active SD state */
839static int dhdsdio_sdclk(dhd_bus_t *bus, bool on)
840{
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700841 DHD_TRACE(("%s: Enter\n", __func__));
842
Franky Lin602a8ab2011-06-01 13:45:04 +0200843 if (on)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700844 bus->clkstate = CLK_SDONLY;
Franky Lin602a8ab2011-06-01 13:45:04 +0200845 else
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700846 bus->clkstate = CLK_NONE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700847
Roland Vossena1c5ad82011-04-11 15:16:24 +0200848 return 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700849}
850
851/* Transition SD and backplane clock readiness */
852static int dhdsdio_clkctl(dhd_bus_t *bus, uint target, bool pendok)
853{
854#ifdef DHD_DEBUG
855 uint oldstate = bus->clkstate;
856#endif /* DHD_DEBUG */
857
858 DHD_TRACE(("%s: Enter\n", __func__));
859
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);
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -0700864 bus->activity = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700865 }
Roland Vossena1c5ad82011-04-11 15:16:24 +0200866 return 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700867 }
868
869 switch (target) {
870 case CLK_AVAIL:
871 /* Make sure SD clock is available */
872 if (bus->clkstate == CLK_NONE)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -0700873 dhdsdio_sdclk(bus, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700874 /* Now request HT Avail on the backplane */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -0700875 dhdsdio_htclk(bus, true, pendok);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700876 dhd_os_wd_timer(bus->dhd, dhd_watchdog_ms);
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -0700877 bus->activity = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700878 break;
879
880 case CLK_SDONLY:
881 /* Remove HT request, or bring up SD clock */
882 if (bus->clkstate == CLK_NONE)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -0700883 dhdsdio_sdclk(bus, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700884 else if (bus->clkstate == CLK_AVAIL)
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -0700885 dhdsdio_htclk(bus, false, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700886 else
887 DHD_ERROR(("dhdsdio_clkctl: request for %d -> %d\n",
888 bus->clkstate, target));
889 dhd_os_wd_timer(bus->dhd, dhd_watchdog_ms);
890 break;
891
892 case CLK_NONE:
893 /* Make sure to remove HT request */
894 if (bus->clkstate == CLK_AVAIL)
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -0700895 dhdsdio_htclk(bus, false, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700896 /* Now remove the SD clock */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -0700897 dhdsdio_sdclk(bus, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700898 dhd_os_wd_timer(bus->dhd, 0);
899 break;
900 }
901#ifdef DHD_DEBUG
902 DHD_INFO(("dhdsdio_clkctl: %d -> %d\n", oldstate, bus->clkstate));
903#endif /* DHD_DEBUG */
904
Roland Vossena1c5ad82011-04-11 15:16:24 +0200905 return 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700906}
907
908int dhdsdio_bussleep(dhd_bus_t *bus, bool sleep)
909{
910 bcmsdh_info_t *sdh = bus->sdh;
911 sdpcmd_regs_t *regs = bus->regs;
912 uint retries = 0;
913
914 DHD_INFO(("dhdsdio_bussleep: request %s (currently %s)\n",
915 (sleep ? "SLEEP" : "WAKE"),
916 (bus->sleeping ? "SLEEP" : "WAKE")));
917
918 /* Done if we're already in the requested state */
919 if (sleep == bus->sleeping)
Roland Vossena1c5ad82011-04-11 15:16:24 +0200920 return 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700921
922 /* Going to sleep: set the alarm and turn off the lights... */
923 if (sleep) {
924 /* Don't sleep if something is pending */
925 if (bus->dpc_sched || bus->rxskip || pktq_len(&bus->txq))
Roland Vossene10d82d2011-05-03 11:35:19 +0200926 return -EBUSY;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700927
928 /* Disable SDIO interrupts (no longer interested) */
929 bcmsdh_intr_disable(bus->sdh);
930
931 /* Make sure the controller has the bus up */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -0700932 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700933
934 /* Tell device to start using OOB wakeup */
935 W_SDREG(SMB_USE_OOB, &regs->tosbmailbox, retries);
936 if (retries > retry_limit)
937 DHD_ERROR(("CANNOT SIGNAL CHIP, WILL NOT WAKE UP!!\n"));
938
939 /* Turn off our contribution to the HT clock request */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -0700940 dhdsdio_clkctl(bus, CLK_SDONLY, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700941
942 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
943 SBSDIO_FORCE_HW_CLKREQ_OFF, NULL);
944
945 /* Isolate the bus */
Franky Linc05df632011-04-25 19:34:07 -0700946 if (bus->ci->chip != BCM4329_CHIP_ID
947 && bus->ci->chip != BCM4319_CHIP_ID) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700948 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
949 SBSDIO_DEVCTL_PADS_ISO, NULL);
950 }
951
952 /* Change state */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -0700953 bus->sleeping = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700954
955 } else {
956 /* Waking up: bus power up is ok, set local state */
957
958 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
959 0, NULL);
960
961 /* Force pad isolation off if possible
962 (in case power never toggled) */
Franky Linc05df632011-04-25 19:34:07 -0700963 if ((bus->ci->buscoretype == PCMCIA_CORE_ID)
964 && (bus->ci->buscorerev >= 10))
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700965 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL, 0,
966 NULL);
967
968 /* Make sure the controller has the bus up */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -0700969 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700970
971 /* Send misc interrupt to indicate OOB not needed */
972 W_SDREG(0, &regs->tosbmailboxdata, retries);
973 if (retries <= retry_limit)
974 W_SDREG(SMB_DEV_INT, &regs->tosbmailbox, retries);
975
976 if (retries > retry_limit)
977 DHD_ERROR(("CANNOT SIGNAL CHIP TO CLEAR OOB!!\n"));
978
979 /* Make sure we have SD bus access */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -0700980 dhdsdio_clkctl(bus, CLK_SDONLY, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700981
982 /* Change state */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -0700983 bus->sleeping = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700984
985 /* Enable interrupts again */
986 if (bus->intr && (bus->dhd->busstate == DHD_BUS_DATA)) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -0700987 bus->intdis = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700988 bcmsdh_intr_enable(bus->sdh);
989 }
990 }
991
Roland Vossena1c5ad82011-04-11 15:16:24 +0200992 return 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700993}
994
995#if defined(OOB_INTR_ONLY)
996void dhd_enable_oob_intr(struct dhd_bus *bus, bool enable)
997{
998#if defined(HW_OOB)
999 bcmsdh_enable_hw_oob_intr(bus->sdh, enable);
1000#else
1001 sdpcmd_regs_t *regs = bus->regs;
1002 uint retries = 0;
1003
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001004 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001005 if (enable == true) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001006
1007 /* Tell device to start using OOB wakeup */
1008 W_SDREG(SMB_USE_OOB, &regs->tosbmailbox, retries);
1009 if (retries > retry_limit)
1010 DHD_ERROR(("CANNOT SIGNAL CHIP, WILL NOT WAKE UP!!\n"));
1011
1012 } else {
1013 /* Send misc interrupt to indicate OOB not needed */
1014 W_SDREG(0, &regs->tosbmailboxdata, retries);
1015 if (retries <= retry_limit)
1016 W_SDREG(SMB_DEV_INT, &regs->tosbmailbox, retries);
1017 }
1018
1019 /* Turn off our contribution to the HT clock request */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001020 dhdsdio_clkctl(bus, CLK_SDONLY, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001021#endif /* !defined(HW_OOB) */
1022}
1023#endif /* defined(OOB_INTR_ONLY) */
1024
1025#define BUS_WAKE(bus) \
1026 do { \
1027 if ((bus)->sleeping) \
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001028 dhdsdio_bussleep((bus), false); \
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001029 } while (0);
1030
1031/* Writes a HW/SW header into the packet and sends it. */
1032/* Assumes: (a) header space already there, (b) caller holds lock */
Arend van Sprielc26b1372010-11-23 14:06:23 +01001033static int dhdsdio_txpkt(dhd_bus_t *bus, struct sk_buff *pkt, uint chan,
1034 bool free_pkt)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001035{
1036 int ret;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001037 u8 *frame;
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07001038 u16 len, pad = 0;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07001039 u32 swheader;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001040 uint retries = 0;
1041 bcmsdh_info_t *sdh;
Arend van Sprielc26b1372010-11-23 14:06:23 +01001042 struct sk_buff *new;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001043 int i;
1044
1045 DHD_TRACE(("%s: Enter\n", __func__));
1046
1047 sdh = bus->sdh;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001048
1049 if (bus->dhd->dongle_reset) {
Roland Vossenb74ac122011-05-03 11:35:20 +02001050 ret = -EPERM;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001051 goto done;
1052 }
1053
Arend van Spriel54991ad2010-11-23 14:06:24 +01001054 frame = (u8 *) (pkt->data);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001055
1056 /* Add alignment padding, allocate new packet if needed */
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07001057 pad = ((unsigned long)frame % DHD_SDALIGN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001058 if (pad) {
Arend van Spriel3be727c2010-11-23 22:20:30 +01001059 if (skb_headroom(pkt) < pad) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001060 DHD_INFO(("%s: insufficient headroom %d for %d pad\n",
Arend van Spriel3be727c2010-11-23 22:20:30 +01001061 __func__, skb_headroom(pkt), pad));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001062 bus->dhd->tx_realloc++;
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02001063 new = bcm_pkt_buf_get_skb(pkt->len + DHD_SDALIGN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001064 if (!new) {
1065 DHD_ERROR(("%s: couldn't allocate new %d-byte "
1066 "packet\n",
Arend van Spriel54991ad2010-11-23 14:06:24 +01001067 __func__, pkt->len + DHD_SDALIGN));
Roland Vossene10d82d2011-05-03 11:35:19 +02001068 ret = -ENOMEM;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001069 goto done;
1070 }
1071
Arend van Spriel3c9d4c32011-03-02 21:18:48 +01001072 PKTALIGN(new, pkt->len, DHD_SDALIGN);
Stanislav Fomichev02160692011-02-15 01:05:10 +03001073 memcpy(new->data, pkt->data, pkt->len);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001074 if (free_pkt)
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02001075 bcm_pkt_buf_free_skb(pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001076 /* free the pkt if canned one is not used */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001077 free_pkt = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001078 pkt = new;
Arend van Spriel54991ad2010-11-23 14:06:24 +01001079 frame = (u8 *) (pkt->data);
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07001080 ASSERT(((unsigned long)frame % DHD_SDALIGN) == 0);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001081 pad = 0;
1082 } else {
Arend van Sprielc303ecb2010-11-18 20:46:43 +01001083 skb_push(pkt, pad);
Arend van Spriel54991ad2010-11-23 14:06:24 +01001084 frame = (u8 *) (pkt->data);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001085
Arend van Spriel54991ad2010-11-23 14:06:24 +01001086 ASSERT((pad + SDPCM_HDRLEN) <= (int)(pkt->len));
Brett Rudley9249ede2010-11-30 20:09:49 -08001087 memset(frame, 0, pad + SDPCM_HDRLEN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001088 }
1089 }
1090 ASSERT(pad < DHD_SDALIGN);
1091
1092 /* Hardware tag: 2 byte len followed by 2 byte ~len check (all LE) */
Arend van Spriel54991ad2010-11-23 14:06:24 +01001093 len = (u16) (pkt->len);
Stanislav Fomichev628f10b2011-02-20 21:43:32 +03001094 *(u16 *) frame = cpu_to_le16(len);
1095 *(((u16 *) frame) + 1) = cpu_to_le16(~len);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001096
1097 /* Software tag: channel, sequence number, data offset */
1098 swheader =
1099 ((chan << SDPCM_CHANNEL_SHIFT) & SDPCM_CHANNEL_MASK) | bus->tx_seq |
1100 (((pad +
1101 SDPCM_HDRLEN) << SDPCM_DOFFSET_SHIFT) & SDPCM_DOFFSET_MASK);
Stanislav Fomichev56dfe3c2011-02-20 21:43:33 +03001102
1103 put_unaligned_le32(swheader, frame + SDPCM_FRAMETAG_LEN);
1104 put_unaligned_le32(0, frame + SDPCM_FRAMETAG_LEN + sizeof(swheader));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001105
1106#ifdef DHD_DEBUG
Arend van Spriel54991ad2010-11-23 14:06:24 +01001107 tx_packets[pkt->priority]++;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001108 if (DHD_BYTES_ON() &&
1109 (((DHD_CTL_ON() && (chan == SDPCM_CONTROL_CHANNEL)) ||
1110 (DHD_DATA_ON() && (chan != SDPCM_CONTROL_CHANNEL))))) {
Arend van Spriel34227312011-05-10 22:25:32 +02001111 printk(KERN_DEBUG "Tx Frame:\n");
1112 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, frame, len);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001113 } else if (DHD_HDRS_ON()) {
Arend van Spriel34227312011-05-10 22:25:32 +02001114 printk(KERN_DEBUG "TxHdr:\n");
1115 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
1116 frame, min_t(u16, len, 16));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001117 }
1118#endif
1119
1120 /* Raise len to next SDIO block to eliminate tail command */
1121 if (bus->roundup && bus->blocksize && (len > bus->blocksize)) {
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07001122 u16 pad = bus->blocksize - (len % bus->blocksize);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001123 if ((pad <= bus->roundup) && (pad < bus->blocksize))
1124#ifdef NOTUSED
Arend van Spriel3be727c2010-11-23 22:20:30 +01001125 if (pad <= skb_tailroom(pkt))
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001126#endif /* NOTUSED */
1127 len += pad;
1128 } else if (len % DHD_SDALIGN) {
1129 len += DHD_SDALIGN - (len % DHD_SDALIGN);
1130 }
1131
1132 /* Some controllers have trouble with odd bytes -- round to even */
1133 if (forcealign && (len & (ALIGNMENT - 1))) {
1134#ifdef NOTUSED
Arend van Spriel3be727c2010-11-23 22:20:30 +01001135 if (skb_tailroom(pkt))
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001136#endif
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07001137 len = roundup(len, ALIGNMENT);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001138#ifdef NOTUSED
1139 else
1140 DHD_ERROR(("%s: sending unrounded %d-byte packet\n",
1141 __func__, len));
1142#endif
1143 }
1144
1145 do {
1146 ret =
1147 dhd_bcmsdh_send_buf(bus, bcmsdh_cur_sbwad(sdh), SDIO_FUNC_2,
1148 F2SYNC, frame, len, pkt, NULL, NULL);
1149 bus->f2txdata++;
Roland Vossenb7ef2a92011-05-03 11:35:02 +02001150 ASSERT(ret != -BCME_PENDING);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001151
1152 if (ret < 0) {
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));
1157 bus->tx_sderrs++;
1158
1159 bcmsdh_abort(sdh, SDIO_FUNC_2);
1160 bcmsdh_cfg_write(sdh, SDIO_FUNC_1,
1161 SBSDIO_FUNC1_FRAMECTRL, SFC_WF_TERM,
1162 NULL);
1163 bus->f1regdata++;
1164
1165 for (i = 0; i < 3; i++) {
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001166 u8 hi, lo;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001167 hi = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
1168 SBSDIO_FUNC1_WFRAMEBCHI,
1169 NULL);
1170 lo = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
1171 SBSDIO_FUNC1_WFRAMEBCLO,
1172 NULL);
1173 bus->f1regdata += 2;
1174 if ((hi == 0) && (lo == 0))
1175 break;
1176 }
1177
1178 }
1179 if (ret == 0)
1180 bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQUENCE_WRAP;
1181
1182 } while ((ret < 0) && retrydata && retries++ < TXRETRIES);
1183
1184done:
1185 /* restore pkt buffer pointer before calling tx complete routine */
Arend van Sprielc303ecb2010-11-18 20:46:43 +01001186 skb_pull(pkt, SDPCM_HDRLEN + pad);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001187 dhd_os_sdunlock(bus->dhd);
1188 dhd_txcomplete(bus->dhd, pkt, ret != 0);
1189 dhd_os_sdlock(bus->dhd);
1190
1191 if (free_pkt)
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02001192 bcm_pkt_buf_free_skb(pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001193
1194 return ret;
1195}
1196
Arend van Sprielc26b1372010-11-23 14:06:23 +01001197int dhd_bus_txdata(struct dhd_bus *bus, struct sk_buff *pkt)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001198{
Roland Vossenb74ac122011-05-03 11:35:20 +02001199 int ret = -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001200 uint datalen, prec;
1201
1202 DHD_TRACE(("%s: Enter\n", __func__));
1203
Arend van Spriel54991ad2010-11-23 14:06:24 +01001204 datalen = pkt->len;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001205
1206#ifdef SDTEST
1207 /* Push the test header if doing loopback */
1208 if (bus->ext_loop) {
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001209 u8 *data;
Arend van Sprielc303ecb2010-11-18 20:46:43 +01001210 skb_push(pkt, SDPCM_TEST_HDRLEN);
Arend van Spriel54991ad2010-11-23 14:06:24 +01001211 data = pkt->data;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001212 *data++ = SDPCM_TEST_ECHOREQ;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001213 *data++ = (u8) bus->loopid++;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001214 *data++ = (datalen >> 0);
1215 *data++ = (datalen >> 8);
1216 datalen += SDPCM_TEST_HDRLEN;
1217 }
1218#endif /* SDTEST */
1219
1220 /* Add space for the header */
Arend van Sprielc303ecb2010-11-18 20:46:43 +01001221 skb_push(pkt, SDPCM_HDRLEN);
Arend van Spriel54991ad2010-11-23 14:06:24 +01001222 ASSERT(IS_ALIGNED((unsigned long)(pkt->data), 2));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001223
Arend van Spriel54991ad2010-11-23 14:06:24 +01001224 prec = PRIO2PREC((pkt->priority & PRIOMASK));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001225
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)));
1234 bus->fcqueued++;
1235
1236 /* Priority based enq */
1237 dhd_os_sdlock_txq(bus->dhd);
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001238 if (dhd_prec_enq(bus->dhd, &bus->txq, pkt, prec) == false) {
Arend van Sprielc303ecb2010-11-18 20:46:43 +01001239 skb_pull(pkt, SDPCM_HDRLEN);
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001240 dhd_txcomplete(bus->dhd, pkt, false);
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02001241 bcm_pkt_buf_free_skb(pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001242 DHD_ERROR(("%s: out of bus->txq !!!\n", __func__));
Roland Vossene10d82d2011-05-03 11:35:19 +02001243 ret = -ENOSR;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001244 } else {
Roland Vossena1c5ad82011-04-11 15:16:24 +02001245 ret = 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001246 }
1247 dhd_os_sdunlock_txq(bus->dhd);
1248
Grant Grundler7c316072011-03-09 15:04:15 -08001249 if (pktq_len(&bus->txq) >= TXHI)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001250 dhd_txflowcontrol(bus->dhd, 0, ON);
1251
1252#ifdef DHD_DEBUG
1253 if (pktq_plen(&bus->txq, prec) > qcount[prec])
1254 qcount[prec] = pktq_plen(&bus->txq, prec);
1255#endif
1256 /* Schedule DPC if needed to send queued packet(s) */
1257 if (dhd_deferred_tx && !bus->dpc_sched) {
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001258 bus->dpc_sched = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001259 dhd_sched_dpc(bus->dhd);
1260 }
1261 } else {
1262 /* Lock: we're about to use shared data/code (and SDIO) */
1263 dhd_os_sdlock(bus->dhd);
1264
1265 /* Otherwise, send it now */
1266 BUS_WAKE(bus);
1267 /* Make sure back plane ht clk is on, no pending allowed */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001268 dhdsdio_clkctl(bus, CLK_AVAIL, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001269
1270#ifndef SDTEST
1271 DHD_TRACE(("%s: calling txpkt\n", __func__));
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001272 ret = dhdsdio_txpkt(bus, pkt, SDPCM_DATA_CHANNEL, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001273#else
1274 ret = dhdsdio_txpkt(bus, pkt,
1275 (bus->ext_loop ? SDPCM_TEST_CHANNEL :
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001276 SDPCM_DATA_CHANNEL), true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001277#endif
1278 if (ret)
1279 bus->dhd->tx_errors++;
1280 else
1281 bus->dhd->dstats.tx_bytes += datalen;
1282
1283 if ((bus->idletime == DHD_IDLE_IMMEDIATE) && !bus->dpc_sched) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001284 bus->activity = false;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001285 dhdsdio_clkctl(bus, CLK_NONE, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001286 }
1287
1288 dhd_os_sdunlock(bus->dhd);
1289 }
1290
1291 return ret;
1292}
1293
1294static uint dhdsdio_sendfromq(dhd_bus_t *bus, uint maxframes)
1295{
Arend van Sprielc26b1372010-11-23 14:06:23 +01001296 struct sk_buff *pkt;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07001297 u32 intstatus = 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001298 uint retries = 0;
1299 int ret = 0, prec_out;
1300 uint cnt = 0;
1301 uint datalen;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001302 u8 tx_prec_map;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001303
1304 dhd_pub_t *dhd = bus->dhd;
1305 sdpcmd_regs_t *regs = bus->regs;
1306
1307 DHD_TRACE(("%s: Enter\n", __func__));
1308
1309 tx_prec_map = ~bus->flowcontrol;
1310
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);
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02001314 pkt = bcm_pktq_mdeq(&bus->txq, tx_prec_map, &prec_out);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001315 if (pkt == NULL) {
1316 dhd_os_sdunlock_txq(bus->dhd);
1317 break;
1318 }
1319 dhd_os_sdunlock_txq(bus->dhd);
Arend van Spriel54991ad2010-11-23 14:06:24 +01001320 datalen = pkt->len - SDPCM_HDRLEN;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001321
1322#ifndef SDTEST
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001323 ret = dhdsdio_txpkt(bus, pkt, SDPCM_DATA_CHANNEL, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001324#else
1325 ret = dhdsdio_txpkt(bus, pkt,
1326 (bus->ext_loop ? SDPCM_TEST_CHANNEL :
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001327 SDPCM_DATA_CHANNEL), true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001328#endif
1329 if (ret)
1330 bus->dhd->tx_errors++;
1331 else
1332 bus->dhd->dstats.tx_bytes += datalen;
1333
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, &regs->intstatus, retries);
1338 bus->f2txdata++;
1339 if (bcmsdh_regfail(bus->sdh))
1340 break;
1341 if (intstatus & bus->hostintmask)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001342 bus->ipend = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001343 }
1344 }
1345
1346 /* Deflow-control stack if needed */
Grant Grundler7c316072011-03-09 15:04:15 -08001347 if (dhd->up && (dhd->busstate == DHD_BUS_DATA) &&
Grant Grundler26a71a42011-03-09 10:41:25 -08001348 dhd->txoff && (pktq_len(&bus->txq) < TXLOW))
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001349 dhd_txflowcontrol(dhd, 0, OFF);
1350
1351 return cnt;
1352}
1353
Greg Kroah-Hartman580a0bd2010-10-05 11:09:48 -07001354int dhd_bus_txctl(struct dhd_bus *bus, unsigned char *msg, uint msglen)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001355{
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001356 u8 *frame;
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07001357 u16 len;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07001358 u32 swheader;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001359 uint retries = 0;
1360 bcmsdh_info_t *sdh = bus->sdh;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001361 u8 doff = 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001362 int ret = -1;
1363 int i;
1364
1365 DHD_TRACE(("%s: Enter\n", __func__));
1366
1367 if (bus->dhd->dongle_reset)
1368 return -EIO;
1369
1370 /* Back the pointer to make a room for bus header */
1371 frame = msg - SDPCM_HDRLEN;
1372 len = (msglen += SDPCM_HDRLEN);
1373
1374 /* Add alignment padding (optional for ctl frames) */
1375 if (dhd_alignctl) {
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07001376 doff = ((unsigned long)frame % DHD_SDALIGN);
Jason Cooper9b890322010-09-30 15:15:39 -04001377 if (doff) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001378 frame -= doff;
1379 len += doff;
1380 msglen += doff;
Brett Rudley9249ede2010-11-30 20:09:49 -08001381 memset(frame, 0, doff + SDPCM_HDRLEN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001382 }
1383 ASSERT(doff < DHD_SDALIGN);
1384 }
1385 doff += SDPCM_HDRLEN;
1386
1387 /* Round send length to next SDIO block */
1388 if (bus->roundup && bus->blocksize && (len > bus->blocksize)) {
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07001389 u16 pad = bus->blocksize - (len % bus->blocksize);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001390 if ((pad <= bus->roundup) && (pad < bus->blocksize))
1391 len += pad;
1392 } else if (len % DHD_SDALIGN) {
1393 len += DHD_SDALIGN - (len % DHD_SDALIGN);
1394 }
1395
1396 /* Satisfy length-alignment requirements */
1397 if (forcealign && (len & (ALIGNMENT - 1)))
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07001398 len = roundup(len, ALIGNMENT);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001399
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07001400 ASSERT(IS_ALIGNED((unsigned long)frame, 2));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001401
1402 /* Need to lock here to protect txseq and SDIO tx calls */
1403 dhd_os_sdlock(bus->dhd);
1404
1405 BUS_WAKE(bus);
1406
1407 /* Make sure backplane clock is on */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001408 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001409
1410 /* Hardware tag: 2 byte len followed by 2 byte ~len check (all LE) */
Stanislav Fomichev628f10b2011-02-20 21:43:32 +03001411 *(u16 *) frame = cpu_to_le16((u16) msglen);
1412 *(((u16 *) frame) + 1) = cpu_to_le16(~msglen);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001413
1414 /* Software tag: channel, sequence number, data offset */
1415 swheader =
1416 ((SDPCM_CONTROL_CHANNEL << SDPCM_CHANNEL_SHIFT) &
1417 SDPCM_CHANNEL_MASK)
1418 | bus->tx_seq | ((doff << SDPCM_DOFFSET_SHIFT) &
1419 SDPCM_DOFFSET_MASK);
Stanislav Fomichev56dfe3c2011-02-20 21:43:33 +03001420 put_unaligned_le32(swheader, frame + SDPCM_FRAMETAG_LEN);
1421 put_unaligned_le32(0, frame + SDPCM_FRAMETAG_LEN + sizeof(swheader));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001422
1423 if (!DATAOK(bus)) {
1424 DHD_INFO(("%s: No bus credit bus->tx_max %d, bus->tx_seq %d\n",
1425 __func__, bus->tx_max, bus->tx_seq));
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001426 bus->ctrl_frame_stat = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001427 /* Send from dpc */
1428 bus->ctrl_frame_buf = frame;
1429 bus->ctrl_frame_len = len;
1430
1431 dhd_wait_for_event(bus->dhd, &bus->ctrl_frame_stat);
1432
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001433 if (bus->ctrl_frame_stat == false) {
1434 DHD_INFO(("%s: ctrl_frame_stat == false\n", __func__));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001435 ret = 0;
1436 } else {
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001437 DHD_INFO(("%s: ctrl_frame_stat == true\n", __func__));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001438 ret = -1;
1439 }
1440 }
1441
1442 if (ret == -1) {
1443#ifdef DHD_DEBUG
Arend van Spriel34227312011-05-10 22:25:32 +02001444 if (DHD_BYTES_ON() && DHD_CTL_ON()) {
1445 printk(KERN_DEBUG "Tx Frame:\n");
1446 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
1447 frame, len);
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));
1452 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001453#endif
1454
1455 do {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001456 bus->ctrl_frame_stat = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001457 ret =
1458 dhd_bcmsdh_send_buf(bus, bcmsdh_cur_sbwad(sdh),
1459 SDIO_FUNC_2, F2SYNC, frame, len,
1460 NULL, NULL, NULL);
1461
Roland Vossenb7ef2a92011-05-03 11:35:02 +02001462 ASSERT(ret != -BCME_PENDING);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001463
1464 if (ret < 0) {
1465 /* On failure, abort the command and
1466 terminate the frame */
1467 DHD_INFO(("%s: sdio error %d, abort command and terminate frame.\n",
1468 __func__, ret));
1469 bus->tx_sderrs++;
1470
1471 bcmsdh_abort(sdh, SDIO_FUNC_2);
1472
1473 bcmsdh_cfg_write(sdh, SDIO_FUNC_1,
1474 SBSDIO_FUNC1_FRAMECTRL,
1475 SFC_WF_TERM, NULL);
1476 bus->f1regdata++;
1477
1478 for (i = 0; i < 3; i++) {
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001479 u8 hi, lo;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001480 hi = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
1481 SBSDIO_FUNC1_WFRAMEBCHI,
1482 NULL);
1483 lo = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
1484 SBSDIO_FUNC1_WFRAMEBCLO,
1485 NULL);
1486 bus->f1regdata += 2;
1487 if ((hi == 0) && (lo == 0))
1488 break;
1489 }
1490
1491 }
1492 if (ret == 0) {
1493 bus->tx_seq =
1494 (bus->tx_seq + 1) % SDPCM_SEQUENCE_WRAP;
1495 }
1496 } while ((ret < 0) && retries++ < TXRETRIES);
1497 }
1498
1499 if ((bus->idletime == DHD_IDLE_IMMEDIATE) && !bus->dpc_sched) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001500 bus->activity = false;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001501 dhdsdio_clkctl(bus, CLK_NONE, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001502 }
1503
1504 dhd_os_sdunlock(bus->dhd);
1505
1506 if (ret)
1507 bus->dhd->tx_ctlerrs++;
1508 else
1509 bus->dhd->tx_ctlpkts++;
1510
1511 return ret ? -EIO : 0;
1512}
1513
Greg Kroah-Hartman580a0bd2010-10-05 11:09:48 -07001514int dhd_bus_rxctl(struct dhd_bus *bus, unsigned char *msg, uint msglen)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001515{
1516 int timeleft;
1517 uint rxlen = 0;
1518 bool pending;
1519
1520 DHD_TRACE(("%s: Enter\n", __func__));
1521
1522 if (bus->dhd->dongle_reset)
1523 return -EIO;
1524
1525 /* Wait until control frame is available */
1526 timeleft = dhd_os_ioctl_resp_wait(bus->dhd, &bus->rxlen, &pending);
1527
1528 dhd_os_sdlock(bus->dhd);
1529 rxlen = bus->rxlen;
Stanislav Fomichev02160692011-02-15 01:05:10 +03001530 memcpy(msg, bus->rxctl, min(msglen, rxlen));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001531 bus->rxlen = 0;
1532 dhd_os_sdunlock(bus->dhd);
1533
1534 if (rxlen) {
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__));
1539#ifdef DHD_DEBUG
1540 dhd_os_sdlock(bus->dhd);
1541 dhdsdio_checkdied(bus, NULL, 0);
1542 dhd_os_sdunlock(bus->dhd);
1543#endif /* DHD_DEBUG */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001544 } else if (pending == true) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001545 DHD_CTL(("%s: cancelled\n", __func__));
1546 return -ERESTARTSYS;
1547 } else {
1548 DHD_CTL(("%s: resumed for unknown reason?\n", __func__));
1549#ifdef DHD_DEBUG
1550 dhd_os_sdlock(bus->dhd);
1551 dhdsdio_checkdied(bus, NULL, 0);
1552 dhd_os_sdunlock(bus->dhd);
1553#endif /* DHD_DEBUG */
1554 }
1555
1556 if (rxlen)
1557 bus->dhd->rx_ctlpkts++;
1558 else
1559 bus->dhd->rx_ctlerrs++;
1560
Jason Coopere9887c92010-10-06 10:08:02 -04001561 return rxlen ? (int)rxlen : -ETIMEDOUT;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001562}
1563
1564/* IOVar table */
1565enum {
1566 IOV_INTR = 1,
1567 IOV_POLLRATE,
1568 IOV_SDREG,
1569 IOV_SBREG,
1570 IOV_SDCIS,
1571 IOV_MEMBYTES,
1572 IOV_MEMSIZE,
1573#ifdef DHD_DEBUG
1574 IOV_CHECKDIED,
1575#endif
1576 IOV_DOWNLOAD,
1577 IOV_FORCEEVEN,
1578 IOV_SDIOD_DRIVE,
1579 IOV_READAHEAD,
1580 IOV_SDRXCHAIN,
1581 IOV_ALIGNCTL,
1582 IOV_SDALIGN,
1583 IOV_DEVRESET,
1584 IOV_CPU,
1585#ifdef SDTEST
1586 IOV_PKTGEN,
1587 IOV_EXTLOOP,
1588#endif /* SDTEST */
1589 IOV_SPROM,
1590 IOV_TXBOUND,
1591 IOV_RXBOUND,
1592 IOV_TXMINMAX,
1593 IOV_IDLETIME,
1594 IOV_IDLECLOCK,
1595 IOV_SD1IDLE,
1596 IOV_SLEEP,
1597 IOV_VARS
1598};
1599
1600const 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},
1617#ifdef DHD_DEBUG
1618 {"sdreg", IOV_SDREG, 0, IOVT_BUFFER, sizeof(sdreg_t)}
1619 ,
1620 {"sbreg", IOV_SBREG, 0, IOVT_BUFFER, sizeof(sdreg_t)}
1621 ,
1622 {"sd_cis", IOV_SDCIS, 0, IOVT_BUFFER, DHD_IOCTL_MAXLEN}
1623 ,
1624 {"forcealign", IOV_FORCEEVEN, 0, IOVT_BOOL, 0}
1625 ,
1626 {"txbound", IOV_TXBOUND, 0, IOVT_UINT32, 0}
1627 ,
1628 {"rxbound", IOV_RXBOUND, 0, IOVT_UINT32, 0}
1629 ,
1630 {"txminmax", IOV_TXMINMAX, 0, IOVT_UINT32, 0}
1631 ,
1632 {"cpu", IOV_CPU, 0, IOVT_BOOL, 0}
1633 ,
1634#ifdef DHD_DEBUG
1635 {"checkdied", IOV_CHECKDIED, 0, IOVT_BUFFER, 0}
1636 ,
1637#endif /* DHD_DEBUG */
1638#endif /* DHD_DEBUG */
1639#ifdef SDTEST
1640 {"extloop", IOV_EXTLOOP, 0, IOVT_BOOL, 0}
1641 ,
1642 {"pktgen", IOV_PKTGEN, 0, IOVT_BUFFER, sizeof(dhd_pktgen_t)}
1643 ,
1644#endif /* SDTEST */
1645
1646 {NULL, 0, 0, 0, 0}
1647};
1648
1649static void
1650dhd_dump_pct(struct bcmstrbuf *strbuf, char *desc, uint num, uint div)
1651{
1652 uint q1, q2;
1653
1654 if (!div) {
1655 bcm_bprintf(strbuf, "%s N/A", desc);
1656 } else {
1657 q1 = num / div;
1658 q2 = (100 * (num - (q1 * div))) / div;
1659 bcm_bprintf(strbuf, "%s %d.%02d", desc, q1, q2);
1660 }
1661}
1662
1663void dhd_bus_dump(dhd_pub_t *dhdp, struct bcmstrbuf *strbuf)
1664{
1665 dhd_bus_t *bus = dhdp->bus;
1666
1667 bcm_bprintf(strbuf, "Bus SDIO structure:\n");
1668 bcm_bprintf(strbuf,
1669 "hostintmask 0x%08x intstatus 0x%08x sdpcm_ver %d\n",
1670 bus->hostintmask, bus->intstatus, bus->sdpcm_ver);
1671 bcm_bprintf(strbuf,
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);
1679
1680 bcm_bprintf(strbuf, "\nAdditional counters:\n");
1681 bcm_bprintf(strbuf,
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,
1684 bus->rxc_errors);
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);
1694 {
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,
1698 bus->f1regdata);
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,
1702 bus->intrcount);
1703 bcm_bprintf(strbuf, "\n");
1704
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,
1708 bus->rxglomframes);
1709 bcm_bprintf(strbuf, "\n");
1710
1711 dhd_dump_pct(strbuf, "Tx: pkts/f2wr", bus->dhd->tx_packets,
1712 bus->f2txdata);
1713 dhd_dump_pct(strbuf, ", pkts/f1sd", bus->dhd->tx_packets,
1714 bus->f1regdata);
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,
1718 bus->intrcount);
1719 bcm_bprintf(strbuf, "\n");
1720
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),
1726 bus->f1regdata);
1727 dhd_dump_pct(strbuf, ", pkts/sd",
1728 (bus->dhd->tx_packets + bus->dhd->rx_packets),
1729 (bus->f2txdata + bus->f2rxhdrs + bus->f2rxdata +
1730 bus->f1regdata));
1731 dhd_dump_pct(strbuf, ", pkts/int",
1732 (bus->dhd->tx_packets + bus->dhd->rx_packets),
1733 bus->intrcount);
1734 bcm_bprintf(strbuf, "\n\n");
1735 }
1736
1737#ifdef SDTEST
1738 if (bus->pktgen_count) {
1739 bcm_bprintf(strbuf, "pktgen config and count:\n");
1740 bcm_bprintf(strbuf,
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,
1747 bus->pktgen_fail);
1748 }
1749#endif /* SDTEST */
1750#ifdef DHD_DEBUG
1751 bcm_bprintf(strbuf, "dpc_sched %d host interrupt%spending\n",
1752 bus->dpc_sched,
1753 (bcmsdh_intr_pending(bus->sdh) ? " " : " not "));
1754 bcm_bprintf(strbuf, "blocksize %d roundup %d\n", bus->blocksize,
1755 bus->roundup);
1756#endif /* DHD_DEBUG */
1757 bcm_bprintf(strbuf,
1758 "clkstate %d activity %d idletime %d idlecount %d sleeping %d\n",
1759 bus->clkstate, bus->activity, bus->idletime, bus->idlecount,
1760 bus->sleeping);
1761}
1762
1763void dhd_bus_clearcounts(dhd_pub_t *dhdp)
1764{
1765 dhd_bus_t *bus = (dhd_bus_t *) dhdp->bus;
1766
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;
1773}
1774
1775#ifdef SDTEST
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001776static int dhdsdio_pktgen_get(dhd_bus_t *bus, u8 *arg)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001777{
1778 dhd_pktgen_t pktgen;
1779
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;
1792
Stanislav Fomichev02160692011-02-15 01:05:10 +03001793 memcpy(arg, &pktgen, sizeof(pktgen));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001794
1795 return 0;
1796}
1797
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001798static int dhdsdio_pktgen_set(dhd_bus_t *bus, u8 *arg)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001799{
1800 dhd_pktgen_t pktgen;
1801 uint oldcnt, oldmode;
1802
Stanislav Fomichev02160692011-02-15 01:05:10 +03001803 memcpy(&pktgen, arg, sizeof(pktgen));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001804 if (pktgen.version != DHD_PKTGEN_VERSION)
Roland Vossene10d82d2011-05-03 11:35:19 +02001805 return -EINVAL;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001806
1807 oldcnt = bus->pktgen_count;
1808 oldmode = bus->pktgen_mode;
1809
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;
1818
1819 bus->pktgen_tick = bus->pktgen_ptick = 0;
Greg Kroah-Hartman3ea2f4d2010-10-08 11:39:43 -07001820 bus->pktgen_len = max(bus->pktgen_len, bus->pktgen_minlen);
Greg Kroah-Hartman7068c2f2010-10-08 11:34:59 -07001821 bus->pktgen_len = min(bus->pktgen_len, bus->pktgen_maxlen);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001822
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;
1826
1827 return 0;
1828}
1829#endif /* SDTEST */
1830
1831static int
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07001832dhdsdio_membytes(dhd_bus_t *bus, bool write, u32 address, u8 *data,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001833 uint size)
1834{
1835 int bcmerror = 0;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07001836 u32 sdaddr;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001837 uint dsize;
1838
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);
1843 else
1844 dsize = size;
1845
1846 /* Set the backplane window to include the start address */
1847 bcmerror = dhdsdio_set_siaddr_window(bus, address);
1848 if (bcmerror) {
1849 DHD_ERROR(("%s: window change failed\n", __func__));
1850 goto xfer_done;
1851 }
1852
1853 /* Do the transfer(s) */
1854 while (size) {
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)));
1858 bcmerror =
1859 bcmsdh_rwdata(bus->sdh, write, sdaddr, data, dsize);
1860 if (bcmerror) {
1861 DHD_ERROR(("%s: membytes transfer failed\n", __func__));
1862 break;
1863 }
1864
1865 /* Adjust for next transfer (if any) */
1866 size -= dsize;
1867 if (size) {
1868 data += dsize;
1869 address += dsize;
1870 bcmerror = dhdsdio_set_siaddr_window(bus, address);
1871 if (bcmerror) {
1872 DHD_ERROR(("%s: window change failed\n",
1873 __func__));
1874 break;
1875 }
1876 sdaddr = 0;
Greg Kroah-Hartmanb61640d2010-10-08 12:37:39 -07001877 dsize = min_t(uint, SBSDIO_SB_OFT_ADDR_LIMIT, size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001878 }
1879 }
1880
1881xfer_done:
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)));
1886 }
1887
1888 return bcmerror;
1889}
1890
1891#ifdef DHD_DEBUG
1892static int dhdsdio_readshared(dhd_bus_t *bus, sdpcm_shared_t *sh)
1893{
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07001894 u32 addr;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001895 int rv;
1896
1897 /* Read last word in memory to determine address of
1898 sdpcm_shared structure */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001899 rv = dhdsdio_membytes(bus, false, bus->ramsize - 4, (u8 *)&addr, 4);
Jason Cooper9b890322010-09-30 15:15:39 -04001900 if (rv < 0)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001901 return rv;
1902
Stanislav Fomichev628f10b2011-02-20 21:43:32 +03001903 addr = le32_to_cpu(addr);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001904
1905 DHD_INFO(("sdpcm_shared address 0x%08X\n", addr));
1906
1907 /*
1908 * Check if addr is valid.
1909 * NVRAM length at the end of memory should have been overwritten.
1910 */
1911 if (addr == 0 || ((~addr >> 16) & 0xffff) == (addr & 0xffff)) {
1912 DHD_ERROR(("%s: address (0x%08x) of sdpcm_shared invalid\n",
1913 __func__, addr));
Roland Vossenb74ac122011-05-03 11:35:20 +02001914 return -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001915 }
1916
Roland Vossen70963f92011-06-01 13:45:08 +02001917 /* Read rte_shared structure */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001918 rv = dhdsdio_membytes(bus, false, addr, (u8 *) sh,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001919 sizeof(sdpcm_shared_t));
1920 if (rv < 0)
1921 return rv;
1922
1923 /* Endianness */
Stanislav Fomichev628f10b2011-02-20 21:43:32 +03001924 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);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001931
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));
Roland Vossenb74ac122011-05-03 11:35:20 +02001937 return -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001938 }
1939
Roland Vossena1c5ad82011-04-11 15:16:24 +02001940 return 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001941}
1942
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001943static int dhdsdio_checkdied(dhd_bus_t *bus, u8 *data, uint size)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001944{
1945 int bcmerror = 0;
1946 uint msize = 512;
1947 char *mbuffer = NULL;
1948 uint maxstrlen = 256;
1949 char *str = NULL;
1950 trap_t tr;
1951 sdpcm_shared_t sdpcm_shared;
1952 struct bcmstrbuf strbuf;
1953
1954 DHD_TRACE(("%s: Enter\n", __func__));
1955
1956 if (data == NULL) {
1957 /*
1958 * Called after a rx ctrl timeout. "data" is NULL.
1959 * allocate memory to trace the trap or assert.
1960 */
1961 size = msize;
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02001962 mbuffer = data = kmalloc(msize, GFP_ATOMIC);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001963 if (mbuffer == NULL) {
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02001964 DHD_ERROR(("%s: kmalloc(%d) failed\n", __func__,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001965 msize));
Roland Vossene10d82d2011-05-03 11:35:19 +02001966 bcmerror = -ENOMEM;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001967 goto done;
1968 }
1969 }
1970
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02001971 str = kmalloc(maxstrlen, GFP_ATOMIC);
Jason Cooper9b890322010-09-30 15:15:39 -04001972 if (str == NULL) {
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02001973 DHD_ERROR(("%s: kmalloc(%d) failed\n", __func__, maxstrlen));
Roland Vossene10d82d2011-05-03 11:35:19 +02001974 bcmerror = -ENOMEM;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001975 goto done;
1976 }
1977
Jason Cooper9b890322010-09-30 15:15:39 -04001978 bcmerror = dhdsdio_readshared(bus, &sdpcm_shared);
1979 if (bcmerror < 0)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001980 goto done;
1981
1982 bcm_binit(&strbuf, data, size);
1983
1984 bcm_bprintf(&strbuf,
1985 "msgtrace address : 0x%08X\nconsole address : 0x%08X\n",
1986 sdpcm_shared.msgtrace_addr, sdpcm_shared.console_addr);
1987
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.)
1992 */
1993 bcm_bprintf(&strbuf, "Assrt not built in dongle\n");
1994 }
1995
1996 if ((sdpcm_shared.flags & (SDPCM_SHARED_ASSERT | SDPCM_SHARED_TRAP)) ==
1997 0) {
1998 /* NOTE: Misspelled assert is intentional - DO NOT FIX.
1999 * (Avoids conflict with real asserts for programmatic
2000 * parsing of output.)
2001 */
2002 bcm_bprintf(&strbuf, "No trap%s in dongle",
2003 (sdpcm_shared.flags & SDPCM_SHARED_ASSERT_BUILT)
2004 ? "/assrt" : "");
2005 } else {
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) {
2010 str[0] = '\0';
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002011 bcmerror = dhdsdio_membytes(bus, false,
Jason Cooper9b890322010-09-30 15:15:39 -04002012 sdpcm_shared.assert_exp_addr,
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002013 (u8 *) str, maxstrlen);
Jason Cooper9b890322010-09-30 15:15:39 -04002014 if (bcmerror < 0)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002015 goto done;
2016
2017 str[maxstrlen - 1] = '\0';
2018 bcm_bprintf(&strbuf, " expr \"%s\"", str);
2019 }
2020
2021 if (sdpcm_shared.assert_file_addr != 0) {
2022 str[0] = '\0';
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002023 bcmerror = dhdsdio_membytes(bus, false,
Jason Cooper9b890322010-09-30 15:15:39 -04002024 sdpcm_shared.assert_file_addr,
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002025 (u8 *) str, maxstrlen);
Jason Cooper9b890322010-09-30 15:15:39 -04002026 if (bcmerror < 0)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002027 goto done;
2028
2029 str[maxstrlen - 1] = '\0';
2030 bcm_bprintf(&strbuf, " file \"%s\"", str);
2031 }
2032
2033 bcm_bprintf(&strbuf, " line %d ",
2034 sdpcm_shared.assert_line);
2035 }
2036
2037 if (sdpcm_shared.flags & SDPCM_SHARED_TRAP) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002038 bcmerror = dhdsdio_membytes(bus, false,
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002039 sdpcm_shared.trap_addr, (u8 *)&tr,
Jason Cooper9b890322010-09-30 15:15:39 -04002040 sizeof(trap_t));
2041 if (bcmerror < 0)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002042 goto done;
2043
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,
2051 tr.r6, tr.r7);
2052 }
2053 }
2054
2055 if (sdpcm_shared.flags & (SDPCM_SHARED_ASSERT | SDPCM_SHARED_TRAP))
2056 DHD_ERROR(("%s: %s\n", __func__, strbuf.origbuf));
2057
2058#ifdef DHD_DEBUG
2059 if (sdpcm_shared.flags & SDPCM_SHARED_TRAP) {
2060 /* Mem dump to a file on device */
2061 dhdsdio_mem_dump(bus);
2062 }
2063#endif /* DHD_DEBUG */
2064
2065done:
Ilia Mirkin46d994b2011-03-13 00:28:56 -05002066 kfree(mbuffer);
2067 kfree(str);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002068
2069 return bcmerror;
2070}
2071
2072static int dhdsdio_mem_dump(dhd_bus_t *bus)
2073{
2074 int ret = 0;
2075 int size; /* Full mem size */
2076 int start = 0; /* Start address */
2077 int read_size = 0; /* Read size of each iteration */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002078 u8 *buf = NULL, *databuf = NULL;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002079
2080 /* Get full mem size */
2081 size = bus->ramsize;
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02002082 buf = kmalloc(size, GFP_ATOMIC);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002083 if (!buf) {
Arend van Spriel0bef7742011-02-10 12:03:44 +01002084 DHD_ERROR(("%s: Out of memory (%d bytes)\n", __func__, size));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002085 return -1;
2086 }
2087
2088 /* Read mem content */
Arend van Spriel0bef7742011-02-10 12:03:44 +01002089 printk(KERN_DEBUG "Dump dongle memory");
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002090 databuf = buf;
2091 while (size) {
Greg Kroah-Hartman7068c2f2010-10-08 11:34:59 -07002092 read_size = min(MEMBLOCK, size);
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002093 ret = dhdsdio_membytes(bus, false, start, databuf, read_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002094 if (ret) {
Arend van Spriel0bef7742011-02-10 12:03:44 +01002095 DHD_ERROR(("%s: Error membytes %d\n", __func__, ret));
Ilia Mirkin46d994b2011-03-13 00:28:56 -05002096 kfree(buf);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002097 return -1;
2098 }
Arend van Spriel0bef7742011-02-10 12:03:44 +01002099 printk(".");
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002100
2101 /* Decrement size and increment start address */
2102 size -= read_size;
2103 start += read_size;
2104 databuf += read_size;
2105 }
Arend van Spriel0bef7742011-02-10 12:03:44 +01002106 printk(KERN_DEBUG "Done\n");
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002107
2108 /* free buf before return !!! */
2109 if (write_to_file(bus->dhd, buf, bus->ramsize)) {
Arend van Spriel0bef7742011-02-10 12:03:44 +01002110 DHD_ERROR(("%s: Error writing to files\n", __func__));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002111 return -1;
2112 }
2113
2114 /* buf free handled in write_to_file, not here */
2115 return 0;
2116}
2117
2118#define CONSOLE_LINE_MAX 192
2119
2120static int dhdsdio_readconsole(dhd_bus_t *bus)
2121{
2122 dhd_console_t *c = &bus->console;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002123 u8 line[CONSOLE_LINE_MAX], ch;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002124 u32 n, idx, addr;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002125 int rv;
2126
2127 /* Don't do anything until FWREADY updates console address */
2128 if (bus->console_addr == 0)
2129 return 0;
2130
2131 /* Read console log struct */
Roland Vossen70963f92011-06-01 13:45:08 +02002132 addr = bus->console_addr + offsetof(rte_cons_t, log);
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002133 rv = dhdsdio_membytes(bus, false, addr, (u8 *)&c->log,
Jason Cooper9b890322010-09-30 15:15:39 -04002134 sizeof(c->log));
2135 if (rv < 0)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002136 return rv;
2137
2138 /* Allocate console buffer (one time only) */
2139 if (c->buf == NULL) {
Stanislav Fomichev628f10b2011-02-20 21:43:32 +03002140 c->bufsize = le32_to_cpu(c->log.buf_size);
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02002141 c->buf = kmalloc(c->bufsize, GFP_ATOMIC);
Jason Cooper9b890322010-09-30 15:15:39 -04002142 if (c->buf == NULL)
Roland Vossene10d82d2011-05-03 11:35:19 +02002143 return -ENOMEM;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002144 }
2145
Stanislav Fomichev628f10b2011-02-20 21:43:32 +03002146 idx = le32_to_cpu(c->log.idx);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002147
2148 /* Protect against corrupt value */
2149 if (idx > c->bufsize)
Roland Vossenb74ac122011-05-03 11:35:20 +02002150 return -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002151
2152 /* Skip reading the console buffer if the index pointer
2153 has not moved */
2154 if (idx == c->last)
Roland Vossena1c5ad82011-04-11 15:16:24 +02002155 return 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002156
2157 /* Read the console buffer */
Stanislav Fomichev628f10b2011-02-20 21:43:32 +03002158 addr = le32_to_cpu(c->log.buf);
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002159 rv = dhdsdio_membytes(bus, false, addr, c->buf, c->bufsize);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002160 if (rv < 0)
2161 return rv;
2162
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.
2167 * Instead, back up
2168 * the buffer pointer and output this
2169 * line next time around.
2170 */
2171 if (c->last >= n)
2172 c->last -= n;
2173 else
2174 c->last = c->bufsize - n;
2175 goto break2;
2176 }
2177 ch = c->buf[c->last];
2178 c->last = (c->last + 1) % c->bufsize;
2179 if (ch == '\n')
2180 break;
2181 line[n] = ch;
2182 }
2183
2184 if (n > 0) {
2185 if (line[n - 1] == '\r')
2186 n--;
2187 line[n] = 0;
Arend van Spriel0bef7742011-02-10 12:03:44 +01002188 printk(KERN_DEBUG "CONSOLE: %s\n", line);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002189 }
2190 }
2191break2:
2192
Roland Vossena1c5ad82011-04-11 15:16:24 +02002193 return 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002194}
2195#endif /* DHD_DEBUG */
2196
2197int dhdsdio_downloadvars(dhd_bus_t *bus, void *arg, int len)
2198{
Roland Vossena1c5ad82011-04-11 15:16:24 +02002199 int bcmerror = 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002200
2201 DHD_TRACE(("%s: Enter\n", __func__));
2202
2203 /* Basic sanity checks */
2204 if (bus->dhd->up) {
Roland Vossenb74ac122011-05-03 11:35:20 +02002205 bcmerror = -EISCONN;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002206 goto err;
2207 }
2208 if (!len) {
Roland Vossene10d82d2011-05-03 11:35:19 +02002209 bcmerror = -EOVERFLOW;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002210 goto err;
2211 }
2212
2213 /* Free the old ones and replace with passed variables */
Ilia Mirkin46d994b2011-03-13 00:28:56 -05002214 kfree(bus->vars);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002215
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02002216 bus->vars = kmalloc(len, GFP_ATOMIC);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002217 bus->varsz = bus->vars ? len : 0;
2218 if (bus->vars == NULL) {
Roland Vossene10d82d2011-05-03 11:35:19 +02002219 bcmerror = -ENOMEM;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002220 goto err;
2221 }
2222
2223 /* Copy the passed variables, which should include the
2224 terminating double-null */
Stanislav Fomichev02160692011-02-15 01:05:10 +03002225 memcpy(bus->vars, arg, bus->varsz);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002226err:
2227 return bcmerror;
2228}
2229
2230static int
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002231dhdsdio_doiovar(dhd_bus_t *bus, const bcm_iovar_t *vi, u32 actionid,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002232 const char *name, void *params, int plen, void *arg, int len,
2233 int val_size)
2234{
2235 int bcmerror = 0;
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002236 s32 int_val = 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002237 bool bool_val = 0;
2238
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));
2242
2243 bcmerror = bcm_iovar_lencheck(vi, arg, len, IOV_ISSET(actionid));
2244 if (bcmerror != 0)
2245 goto exit;
2246
2247 if (plen >= (int)sizeof(int_val))
Stanislav Fomichev02160692011-02-15 01:05:10 +03002248 memcpy(&int_val, params, sizeof(int_val));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002249
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002250 bool_val = (int_val != 0) ? true : false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002251
2252 /* Some ioctls use the bus */
2253 dhd_os_sdlock(bus->dhd);
2254
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))) {
Roland Vossenb74ac122011-05-03 11:35:20 +02002258 bcmerror = -EPERM;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002259 goto exit;
2260 }
2261
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);
2266 } else {
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002267 int_val = (s32) bus->sleeping;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002268 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002269 }
2270 goto exit;
2271 }
2272
2273 /* Request clock to allow SDIO accesses */
2274 if (!bus->dhd->dongle_reset) {
2275 BUS_WAKE(bus);
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002276 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002277 }
2278
2279 switch (actionid) {
2280 case IOV_GVAL(IOV_INTR):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002281 int_val = (s32) bus->intr;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002282 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002283 break;
2284
2285 case IOV_SVAL(IOV_INTR):
2286 bus->intr = bool_val;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002287 bus->intdis = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002288 if (bus->dhd->up) {
2289 if (bus->intr) {
2290 DHD_INTR(("%s: enable SDIO device interrupts\n",
2291 __func__));
2292 bcmsdh_intr_enable(bus->sdh);
2293 } else {
2294 DHD_INTR(("%s: disable SDIO interrupts\n",
2295 __func__));
2296 bcmsdh_intr_disable(bus->sdh);
2297 }
2298 }
2299 break;
2300
2301 case IOV_GVAL(IOV_POLLRATE):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002302 int_val = (s32) bus->pollrate;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002303 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002304 break;
2305
2306 case IOV_SVAL(IOV_POLLRATE):
2307 bus->pollrate = (uint) int_val;
2308 bus->poll = (bus->pollrate != 0);
2309 break;
2310
2311 case IOV_GVAL(IOV_IDLETIME):
2312 int_val = bus->idletime;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002313 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002314 break;
2315
2316 case IOV_SVAL(IOV_IDLETIME):
2317 if ((int_val < 0) && (int_val != DHD_IDLE_IMMEDIATE))
Roland Vossene10d82d2011-05-03 11:35:19 +02002318 bcmerror = -EINVAL;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002319 else
2320 bus->idletime = int_val;
2321 break;
2322
2323 case IOV_GVAL(IOV_IDLECLOCK):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002324 int_val = (s32) bus->idleclock;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002325 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002326 break;
2327
2328 case IOV_SVAL(IOV_IDLECLOCK):
2329 bus->idleclock = int_val;
2330 break;
2331
2332 case IOV_GVAL(IOV_SD1IDLE):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002333 int_val = (s32) sd1idle;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002334 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002335 break;
2336
2337 case IOV_SVAL(IOV_SD1IDLE):
2338 sd1idle = bool_val;
2339 break;
2340
2341 case IOV_SVAL(IOV_MEMBYTES):
2342 case IOV_GVAL(IOV_MEMBYTES):
2343 {
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002344 u32 address;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002345 uint size, dsize;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002346 u8 *data;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002347
2348 bool set = (actionid == IOV_SVAL(IOV_MEMBYTES));
2349
2350 ASSERT(plen >= 2 * sizeof(int));
2351
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002352 address = (u32) int_val;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002353 memcpy(&int_val, (char *)params + sizeof(int_val),
2354 sizeof(int_val));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002355 size = (uint) int_val;
2356
2357 /* Do some validation */
2358 dsize = set ? plen - (2 * sizeof(int)) : len;
2359 if (dsize < size) {
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));
Roland Vossene10d82d2011-05-03 11:35:19 +02002364 bcmerror = -EINVAL;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002365 break;
2366 }
2367
2368 DHD_INFO(("%s: Request to %s %d bytes at address "
2369 "0x%08x\n",
2370 __func__, (set ? "write" : "read"), size, address));
2371
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));
Roland Vossene10d82d2011-05-03 11:35:19 +02002379 bcmerror = -EINVAL;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002380 break;
2381 }
2382
2383 /* Generate the actual data pointer */
2384 data =
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002385 set ? (u8 *) params +
2386 2 * sizeof(int) : (u8 *) arg;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002387
2388 /* Call to do the transfer */
2389 bcmerror =
2390 dhdsdio_membytes(bus, set, address, data, size);
2391
2392 break;
2393 }
2394
2395 case IOV_GVAL(IOV_MEMSIZE):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002396 int_val = (s32) bus->ramsize;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002397 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002398 break;
2399
2400 case IOV_GVAL(IOV_SDIOD_DRIVE):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002401 int_val = (s32) dhd_sdiod_drive_strength;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002402 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002403 break;
2404
2405 case IOV_SVAL(IOV_SDIOD_DRIVE):
2406 dhd_sdiod_drive_strength = int_val;
Franky Lin5d0d7a92011-04-25 19:34:05 -07002407 dhdsdio_sdiod_drive_strength_init(bus,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002408 dhd_sdiod_drive_strength);
2409 break;
2410
2411 case IOV_SVAL(IOV_DOWNLOAD):
2412 bcmerror = dhdsdio_download_state(bus, bool_val);
2413 break;
2414
2415 case IOV_SVAL(IOV_VARS):
2416 bcmerror = dhdsdio_downloadvars(bus, arg, len);
2417 break;
2418
2419 case IOV_GVAL(IOV_READAHEAD):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002420 int_val = (s32) dhd_readahead;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002421 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002422 break;
2423
2424 case IOV_SVAL(IOV_READAHEAD):
2425 if (bool_val && !dhd_readahead)
2426 bus->nextlen = 0;
2427 dhd_readahead = bool_val;
2428 break;
2429
2430 case IOV_GVAL(IOV_SDRXCHAIN):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002431 int_val = (s32) bus->use_rxchain;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002432 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002433 break;
2434
2435 case IOV_SVAL(IOV_SDRXCHAIN):
2436 if (bool_val && !bus->sd_rxchain)
Roland Vossene10d82d2011-05-03 11:35:19 +02002437 bcmerror = -ENOTSUPP;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002438 else
2439 bus->use_rxchain = bool_val;
2440 break;
2441 case IOV_GVAL(IOV_ALIGNCTL):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002442 int_val = (s32) dhd_alignctl;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002443 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002444 break;
2445
2446 case IOV_SVAL(IOV_ALIGNCTL):
2447 dhd_alignctl = bool_val;
2448 break;
2449
2450 case IOV_GVAL(IOV_SDALIGN):
2451 int_val = DHD_SDALIGN;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002452 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002453 break;
2454
2455#ifdef DHD_DEBUG
2456 case IOV_GVAL(IOV_VARS):
2457 if (bus->varsz < (uint) len)
Stanislav Fomichev02160692011-02-15 01:05:10 +03002458 memcpy(arg, bus->vars, bus->varsz);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002459 else
Roland Vossene10d82d2011-05-03 11:35:19 +02002460 bcmerror = -EOVERFLOW;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002461 break;
2462#endif /* DHD_DEBUG */
2463
2464#ifdef DHD_DEBUG
2465 case IOV_GVAL(IOV_SDREG):
2466 {
2467 sdreg_t *sd_ptr;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002468 u32 addr, size;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002469
2470 sd_ptr = (sdreg_t *) params;
2471
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07002472 addr = (unsigned long)bus->regs + sd_ptr->offset;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002473 size = sd_ptr->func;
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002474 int_val = (s32) bcmsdh_reg_read(bus->sdh, addr, size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002475 if (bcmsdh_regfail(bus->sdh))
Roland Vossenb74ac122011-05-03 11:35:20 +02002476 bcmerror = -EIO;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002477 memcpy(arg, &int_val, sizeof(s32));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002478 break;
2479 }
2480
2481 case IOV_SVAL(IOV_SDREG):
2482 {
2483 sdreg_t *sd_ptr;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002484 u32 addr, size;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002485
2486 sd_ptr = (sdreg_t *) params;
2487
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07002488 addr = (unsigned long)bus->regs + sd_ptr->offset;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002489 size = sd_ptr->func;
2490 bcmsdh_reg_write(bus->sdh, addr, size, sd_ptr->value);
2491 if (bcmsdh_regfail(bus->sdh))
Roland Vossenb74ac122011-05-03 11:35:20 +02002492 bcmerror = -EIO;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002493 break;
2494 }
2495
2496 /* Same as above, but offset is not backplane
2497 (not SDIO core) */
2498 case IOV_GVAL(IOV_SBREG):
2499 {
2500 sdreg_t sdreg;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002501 u32 addr, size;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002502
Stanislav Fomichev02160692011-02-15 01:05:10 +03002503 memcpy(&sdreg, params, sizeof(sdreg));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002504
2505 addr = SI_ENUM_BASE + sdreg.offset;
2506 size = sdreg.func;
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002507 int_val = (s32) bcmsdh_reg_read(bus->sdh, addr, size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002508 if (bcmsdh_regfail(bus->sdh))
Roland Vossenb74ac122011-05-03 11:35:20 +02002509 bcmerror = -EIO;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002510 memcpy(arg, &int_val, sizeof(s32));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002511 break;
2512 }
2513
2514 case IOV_SVAL(IOV_SBREG):
2515 {
2516 sdreg_t sdreg;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002517 u32 addr, size;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002518
Stanislav Fomichev02160692011-02-15 01:05:10 +03002519 memcpy(&sdreg, params, sizeof(sdreg));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002520
2521 addr = SI_ENUM_BASE + sdreg.offset;
2522 size = sdreg.func;
2523 bcmsdh_reg_write(bus->sdh, addr, size, sdreg.value);
2524 if (bcmsdh_regfail(bus->sdh))
Roland Vossenb74ac122011-05-03 11:35:20 +02002525 bcmerror = -EIO;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002526 break;
2527 }
2528
2529 case IOV_GVAL(IOV_SDCIS):
2530 {
2531 *(char *)arg = 0;
2532
nohee koea3b8a22010-10-09 10:34:38 -07002533 strcat(arg, "\nFunc 0\n");
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002534 bcmsdh_cis_read(bus->sdh, 0x10,
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002535 (u8 *) arg + strlen(arg),
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002536 SBSDIO_CIS_SIZE_LIMIT);
nohee koea3b8a22010-10-09 10:34:38 -07002537 strcat(arg, "\nFunc 1\n");
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002538 bcmsdh_cis_read(bus->sdh, 0x11,
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002539 (u8 *) arg + strlen(arg),
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002540 SBSDIO_CIS_SIZE_LIMIT);
nohee koea3b8a22010-10-09 10:34:38 -07002541 strcat(arg, "\nFunc 2\n");
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002542 bcmsdh_cis_read(bus->sdh, 0x12,
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002543 (u8 *) arg + strlen(arg),
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002544 SBSDIO_CIS_SIZE_LIMIT);
2545 break;
2546 }
2547
2548 case IOV_GVAL(IOV_FORCEEVEN):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002549 int_val = (s32) forcealign;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002550 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002551 break;
2552
2553 case IOV_SVAL(IOV_FORCEEVEN):
2554 forcealign = bool_val;
2555 break;
2556
2557 case IOV_GVAL(IOV_TXBOUND):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002558 int_val = (s32) dhd_txbound;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002559 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002560 break;
2561
2562 case IOV_SVAL(IOV_TXBOUND):
2563 dhd_txbound = (uint) int_val;
2564 break;
2565
2566 case IOV_GVAL(IOV_RXBOUND):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002567 int_val = (s32) dhd_rxbound;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002568 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002569 break;
2570
2571 case IOV_SVAL(IOV_RXBOUND):
2572 dhd_rxbound = (uint) int_val;
2573 break;
2574
2575 case IOV_GVAL(IOV_TXMINMAX):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002576 int_val = (s32) dhd_txminmax;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002577 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002578 break;
2579
2580 case IOV_SVAL(IOV_TXMINMAX):
2581 dhd_txminmax = (uint) int_val;
2582 break;
2583#endif /* DHD_DEBUG */
2584
2585#ifdef SDTEST
2586 case IOV_GVAL(IOV_EXTLOOP):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002587 int_val = (s32) bus->ext_loop;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002588 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002589 break;
2590
2591 case IOV_SVAL(IOV_EXTLOOP):
2592 bus->ext_loop = bool_val;
2593 break;
2594
2595 case IOV_GVAL(IOV_PKTGEN):
2596 bcmerror = dhdsdio_pktgen_get(bus, arg);
2597 break;
2598
2599 case IOV_SVAL(IOV_PKTGEN):
2600 bcmerror = dhdsdio_pktgen_set(bus, arg);
2601 break;
2602#endif /* SDTEST */
2603
2604 case IOV_SVAL(IOV_DEVRESET):
2605 DHD_TRACE(("%s: Called set IOV_DEVRESET=%d dongle_reset=%d "
2606 "busstate=%d\n",
2607 __func__, bool_val, bus->dhd->dongle_reset,
2608 bus->dhd->busstate));
2609
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002610 dhd_bus_devreset(bus->dhd, (u8) bool_val);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002611
2612 break;
2613
2614 case IOV_GVAL(IOV_DEVRESET):
2615 DHD_TRACE(("%s: Called get IOV_DEVRESET\n", __func__));
2616
2617 /* Get its status */
2618 int_val = (bool) bus->dhd->dongle_reset;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002619 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002620
2621 break;
2622
2623 default:
Roland Vossene10d82d2011-05-03 11:35:19 +02002624 bcmerror = -ENOTSUPP;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002625 break;
2626 }
2627
2628exit:
2629 if ((bus->idletime == DHD_IDLE_IMMEDIATE) && !bus->dpc_sched) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002630 bus->activity = false;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07002631 dhdsdio_clkctl(bus, CLK_NONE, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002632 }
2633
2634 dhd_os_sdunlock(bus->dhd);
2635
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002636 if (actionid == IOV_SVAL(IOV_DEVRESET) && bool_val == false)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002637 dhd_preinit_ioctls((dhd_pub_t *) bus->dhd);
2638
2639 return bcmerror;
2640}
2641
2642static int dhdsdio_write_vars(dhd_bus_t *bus)
2643{
2644 int bcmerror = 0;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002645 u32 varsize;
2646 u32 varaddr;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002647 u8 *vbuffer;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002648 u32 varsizew;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002649#ifdef DHD_DEBUG
2650 char *nvram_ularray;
2651#endif /* DHD_DEBUG */
2652
2653 /* Even if there are no vars are to be written, we still
2654 need to set the ramsize. */
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07002655 varsize = bus->varsz ? roundup(bus->varsz, 4) : 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002656 varaddr = (bus->ramsize - 4) - varsize;
2657
2658 if (bus->vars) {
Alexander Beregalov12d0eb42011-03-09 03:53:35 +03002659 vbuffer = kzalloc(varsize, GFP_ATOMIC);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002660 if (!vbuffer)
Roland Vossene10d82d2011-05-03 11:35:19 +02002661 return -ENOMEM;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002662
Stanislav Fomichev02160692011-02-15 01:05:10 +03002663 memcpy(vbuffer, bus->vars, bus->varsz);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002664
2665 /* Write the vars list */
2666 bcmerror =
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07002667 dhdsdio_membytes(bus, true, varaddr, vbuffer, varsize);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002668#ifdef DHD_DEBUG
2669 /* Verify NVRAM bytes */
2670 DHD_INFO(("Compare NVRAM dl & ul; varsize=%d\n", varsize));
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02002671 nvram_ularray = kmalloc(varsize, GFP_ATOMIC);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002672 if (!nvram_ularray)
Roland Vossene10d82d2011-05-03 11:35:19 +02002673 return -ENOMEM;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002674
2675 /* Upload image to verify downloaded contents. */
2676 memset(nvram_ularray, 0xaa, varsize);
2677
2678 /* Read the vars list to temp buffer for comparison */
2679 bcmerror =
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002680 dhdsdio_membytes(bus, false, varaddr, nvram_ularray,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002681 varsize);
2682 if (bcmerror) {
2683 DHD_ERROR(("%s: error %d on reading %d nvram bytes at "
2684 "0x%08x\n", __func__, bcmerror, varsize, varaddr));
2685 }
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",
2689 __func__));
2690 } else
2691 DHD_ERROR(("%s: Download/Upload/Compare of NVRAM ok.\n",
2692 __func__));
2693
mike.rapoport@gmail.com182acb32010-10-13 00:09:12 +02002694 kfree(nvram_ularray);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002695#endif /* DHD_DEBUG */
2696
mike.rapoport@gmail.com182acb32010-10-13 00:09:12 +02002697 kfree(vbuffer);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002698 }
2699
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);
2705
2706 /*
2707 * Determine the length token:
2708 * Varsize, converted to words, in lower 16-bits, checksum
2709 * in upper 16-bits.
2710 */
2711 if (bcmerror) {
2712 varsizew = 0;
2713 } else {
2714 varsizew = varsize / 4;
2715 varsizew = (~varsizew << 16) | (varsizew & 0x0000FFFF);
Stanislav Fomichev628f10b2011-02-20 21:43:32 +03002716 varsizew = cpu_to_le32(varsizew);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002717 }
2718
2719 DHD_INFO(("New varsize is %d, length token=0x%08x\n", varsize,
2720 varsizew));
2721
2722 /* Write the length token to the last word */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07002723 bcmerror = dhdsdio_membytes(bus, true, (bus->orig_ramsize - 4),
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002724 (u8 *)&varsizew, 4);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002725
2726 return bcmerror;
2727}
2728
2729static int dhdsdio_download_state(dhd_bus_t *bus, bool enter)
2730{
2731 uint retries;
Franky Lineb5dc512011-04-25 19:34:04 -07002732 u32 regdata;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002733 int bcmerror = 0;
2734
2735 /* To enter download state, disable ARM and reset SOCRAM.
2736 * To exit download state, simply reset ARM (default is RAM boot).
2737 */
2738 if (enter) {
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07002739 bus->alp_only = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002740
Franky Lineb5dc512011-04-25 19:34:04 -07002741 dhdsdio_chip_disablecore(bus->sdh, bus->ci->armcorebase);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002742
Franky Lineb5dc512011-04-25 19:34:04 -07002743 dhdsdio_chip_resetcore(bus->sdh, bus->ci->ramcorebase);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002744
2745 /* Clear the top bit of memory */
2746 if (bus->ramsize) {
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002747 u32 zeros = 0;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07002748 dhdsdio_membytes(bus, true, bus->ramsize - 4,
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002749 (u8 *)&zeros, 4);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002750 }
2751 } else {
Franky Lineb5dc512011-04-25 19:34:04 -07002752 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) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002757 DHD_ERROR(("%s: SOCRAM core is down after reset?\n",
2758 __func__));
Roland Vossenb74ac122011-05-03 11:35:20 +02002759 bcmerror = -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002760 goto fail;
2761 }
2762
2763 bcmerror = dhdsdio_write_vars(bus);
2764 if (bcmerror) {
2765 DHD_ERROR(("%s: no vars written to RAM\n", __func__));
2766 bcmerror = 0;
2767 }
2768
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002769 W_SDREG(0xFFFFFFFF, &bus->regs->intstatus, retries);
2770
Franky Lineb5dc512011-04-25 19:34:04 -07002771 dhdsdio_chip_resetcore(bus->sdh, bus->ci->armcorebase);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002772
2773 /* Allow HT Clock now that the ARM is running. */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002774 bus->alp_only = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002775
2776 bus->dhd->busstate = DHD_BUS_LOAD;
2777 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002778fail:
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002779 return bcmerror;
2780}
2781
2782int
2783dhd_bus_iovar_op(dhd_pub_t *dhdp, const char *name,
2784 void *params, int plen, void *arg, int len, bool set)
2785{
2786 dhd_bus_t *bus = dhdp->bus;
2787 const bcm_iovar_t *vi = NULL;
2788 int bcmerror = 0;
2789 int val_size;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002790 u32 actionid;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002791
2792 DHD_TRACE(("%s: Enter\n", __func__));
2793
2794 ASSERT(name);
2795 ASSERT(len >= 0);
2796
2797 /* Get MUST have return space */
2798 ASSERT(set || (arg && len));
2799
2800 /* Set does NOT take qualifiers */
2801 ASSERT(!set || (!params && !plen));
2802
2803 /* Look up var locally; if not found pass to host driver */
2804 vi = bcm_iovar_lookup(dhdsdio_iovars, name);
2805 if (vi == NULL) {
2806 dhd_os_sdlock(bus->dhd);
2807
2808 BUS_WAKE(bus);
2809
2810 /* Turn on clock in case SD command needs backplane */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002811 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002812
2813 bcmerror =
2814 bcmsdh_iovar_op(bus->sdh, name, params, plen, arg, len,
2815 set);
2816
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002817 /* Similar check for blocksize change */
2818 if (set && strcmp(name, "sd_blocksize") == 0) {
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002819 s32 fnum = 2;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002820 if (bcmsdh_iovar_op
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002821 (bus->sdh, "sd_blocksize", &fnum, sizeof(s32),
2822 &bus->blocksize, sizeof(s32),
Roland Vossena1c5ad82011-04-11 15:16:24 +02002823 false) != 0) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002824 bus->blocksize = 0;
2825 DHD_ERROR(("%s: fail on %s get\n", __func__,
2826 "sd_blocksize"));
2827 } else {
2828 DHD_INFO(("%s: noted %s update, value now %d\n",
2829 __func__, "sd_blocksize",
2830 bus->blocksize));
2831 }
2832 }
Greg Kroah-Hartman7068c2f2010-10-08 11:34:59 -07002833 bus->roundup = min(max_roundup, bus->blocksize);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002834
2835 if ((bus->idletime == DHD_IDLE_IMMEDIATE) && !bus->dpc_sched) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002836 bus->activity = false;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07002837 dhdsdio_clkctl(bus, CLK_NONE, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002838 }
2839
2840 dhd_os_sdunlock(bus->dhd);
2841 goto exit;
2842 }
2843
2844 DHD_CTL(("%s: %s %s, len %d plen %d\n", __func__,
2845 name, (set ? "set" : "get"), len, plen));
2846
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
2849 */
2850 if (params == NULL) {
2851 params = arg;
2852 plen = len;
2853 }
2854
2855 if (vi->type == IOVT_VOID)
2856 val_size = 0;
2857 else if (vi->type == IOVT_BUFFER)
2858 val_size = len;
2859 else
2860 /* all other types are integer sized */
2861 val_size = sizeof(int);
2862
2863 actionid = set ? IOV_SVAL(vi->varid) : IOV_GVAL(vi->varid);
2864 bcmerror =
2865 dhdsdio_doiovar(bus, vi, actionid, name, params, plen, arg, len,
2866 val_size);
2867
2868exit:
2869 return bcmerror;
2870}
2871
2872void dhd_bus_stop(struct dhd_bus *bus, bool enforce_mutex)
2873{
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002874 u32 local_hostintmask;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002875 u8 saveclk;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002876 uint retries;
2877 int err;
2878
2879 DHD_TRACE(("%s: Enter\n", __func__));
2880
2881 if (enforce_mutex)
2882 dhd_os_sdlock(bus->dhd);
2883
2884 BUS_WAKE(bus);
2885
2886 /* Enable clock for device interrupts */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002887 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002888
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;
2893
2894 /* Change our idea of bus state */
2895 bus->dhd->busstate = DHD_BUS_DOWN;
2896
2897 /* Force clocks on backplane to be sure F2 interrupt propagates */
2898 saveclk =
2899 bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
2900 &err);
2901 if (!err) {
2902 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
2903 (saveclk | SBSDIO_FORCE_HT), &err);
2904 }
2905 if (err) {
2906 DHD_ERROR(("%s: Failed to force clock for F2: err %d\n",
2907 __func__, err));
2908 }
2909
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);
2915
2916 /* Clear any pending interrupts now that F2 is disabled */
2917 W_SDREG(local_hostintmask, &bus->regs->intstatus, retries);
2918
2919 /* Turn off the backplane clock (only) */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002920 dhdsdio_clkctl(bus, CLK_SDONLY, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002921
2922 /* Clear the data packet queues */
Clemens Noss84067de2011-05-15 22:45:30 +02002923 bcm_pktq_flush(&bus->txq, true, NULL, NULL);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002924
2925 /* Clear any held glomming stuff */
2926 if (bus->glomd)
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02002927 bcm_pkt_buf_free_skb(bus->glomd);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002928
2929 if (bus->glom)
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02002930 bcm_pkt_buf_free_skb(bus->glom);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002931
2932 bus->glom = bus->glomd = NULL;
2933
2934 /* Clear rx control and wake any waiters */
2935 bus->rxlen = 0;
2936 dhd_os_ioctl_resp_wake(bus->dhd);
2937
2938 /* Reset some F2 state stuff */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002939 bus->rxskip = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002940 bus->tx_seq = bus->rx_seq = 0;
2941
2942 if (enforce_mutex)
2943 dhd_os_sdunlock(bus->dhd);
2944}
2945
2946int dhd_bus_init(dhd_pub_t *dhdp, bool enforce_mutex)
2947{
2948 dhd_bus_t *bus = dhdp->bus;
2949 dhd_timeout_t tmo;
2950 uint retries = 0;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002951 u8 ready, enable;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002952 int err, ret = 0;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002953 u8 saveclk;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002954
2955 DHD_TRACE(("%s: Enter\n", __func__));
2956
2957 ASSERT(bus->dhd);
2958 if (!bus->dhd)
2959 return 0;
2960
2961 if (enforce_mutex)
2962 dhd_os_sdlock(bus->dhd);
2963
2964 /* Make sure backplane clock is on, needed to generate F2 interrupt */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002965 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002966 if (bus->clkstate != CLK_AVAIL)
2967 goto exit;
2968
2969 /* Force clocks on backplane to be sure F2 interrupt propagates */
2970 saveclk =
2971 bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
2972 &err);
2973 if (!err) {
2974 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
2975 (saveclk | SBSDIO_FORCE_HT), &err);
2976 }
2977 if (err) {
2978 DHD_ERROR(("%s: Failed to force clock for F2: err %d\n",
2979 __func__, err));
2980 goto exit;
2981 }
2982
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);
2987
2988 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_0, SDIOD_CCCR_IOEN, enable, NULL);
2989
2990 /* Give the dongle some time to do its thing and set IOR2 */
2991 dhd_timeout_start(&tmo, DHD_WAIT_F2RDY * 1000);
2992
2993 ready = 0;
2994 while (ready != enable && !dhd_timeout_expired(&tmo))
2995 ready =
2996 bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_0, SDIOD_CCCR_IORDY,
2997 NULL);
2998
2999 DHD_INFO(("%s: enable 0x%02x, ready 0x%02x (waited %uus)\n",
3000 __func__, enable, ready, tmo.elapsed));
3001
3002 /* If F2 successfully enabled, set core and enable interrupts */
3003 if (ready == enable) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003004 /* Set up the interrupt mask and enable interrupts */
3005 bus->hostintmask = HOSTINTMASK;
Franky Linc05df632011-04-25 19:34:07 -07003006 W_SDREG(bus->hostintmask,
3007 (unsigned int *)CORE_BUS_REG(bus->ci->buscorebase,
3008 hostintmask), retries);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003009
3010 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_WATERMARK,
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003011 (u8) watermark, &err);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003012
3013 /* Set bus state according to enable result */
3014 dhdp->busstate = DHD_BUS_DATA;
3015
3016 /* bcmsdh_intr_unmask(bus->sdh); */
3017
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003018 bus->intdis = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003019 if (bus->intr) {
3020 DHD_INTR(("%s: enable SDIO device interrupts\n",
3021 __func__));
3022 bcmsdh_intr_enable(bus->sdh);
3023 } else {
3024 DHD_INTR(("%s: disable SDIO interrupts\n", __func__));
3025 bcmsdh_intr_disable(bus->sdh);
3026 }
3027
3028 }
3029
3030 else {
3031 /* Disable F2 again */
3032 enable = SDIO_FUNC_ENABLE_1;
3033 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_0, SDIOD_CCCR_IOEN, enable,
3034 NULL);
3035 }
3036
3037 /* Restore previous clock setting */
3038 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
3039 saveclk, &err);
3040
3041 /* If we didn't come up, turn off backplane clock */
3042 if (dhdp->busstate != DHD_BUS_DATA)
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003043 dhdsdio_clkctl(bus, CLK_NONE, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003044
3045exit:
3046 if (enforce_mutex)
3047 dhd_os_sdunlock(bus->dhd);
3048
3049 return ret;
3050}
3051
3052static void dhdsdio_rxfail(dhd_bus_t *bus, bool abort, bool rtx)
3053{
3054 bcmsdh_info_t *sdh = bus->sdh;
3055 sdpcmd_regs_t *regs = bus->regs;
3056 uint retries = 0;
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07003057 u16 lastrbc;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003058 u8 hi, lo;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003059 int err;
3060
3061 DHD_ERROR(("%s: %sterminate frame%s\n", __func__,
3062 (abort ? "abort command, " : ""),
3063 (rtx ? ", send NAK" : "")));
3064
3065 if (abort)
3066 bcmsdh_abort(sdh, SDIO_FUNC_2);
3067
3068 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_FRAMECTRL, SFC_RF_TERM,
3069 &err);
3070 bus->f1regdata++;
3071
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,
3075 NULL);
3076 lo = bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_RFRAMEBCLO,
3077 NULL);
3078 bus->f1regdata += 2;
3079
3080 if ((hi == 0) && (lo == 0))
3081 break;
3082
3083 if ((hi > (lastrbc >> 8)) && (lo > (lastrbc & 0x00ff))) {
3084 DHD_ERROR(("%s: count growing: last 0x%04x now "
3085 "0x%04x\n",
3086 __func__, lastrbc, ((hi << 8) + lo)));
3087 }
3088 lastrbc = (hi << 8) + lo;
3089 }
3090
3091 if (!retries) {
3092 DHD_ERROR(("%s: count never zeroed: last 0x%04x\n",
3093 __func__, lastrbc));
3094 } else {
3095 DHD_INFO(("%s: flush took %d iterations\n", __func__,
3096 (0xffff - retries)));
3097 }
3098
3099 if (rtx) {
3100 bus->rxrtx++;
3101 W_SDREG(SMB_NAK, &regs->tosbmailbox, retries);
3102 bus->f1regdata++;
3103 if (retries <= retry_limit)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003104 bus->rxskip = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003105 }
3106
3107 /* Clear partial in any case */
3108 bus->nextlen = 0;
3109
3110 /* If we can't reach the device, signal failure */
3111 if (err || bcmsdh_regfail(sdh))
3112 bus->dhd->busstate = DHD_BUS_DOWN;
3113}
3114
3115static void
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003116dhdsdio_read_control(dhd_bus_t *bus, u8 *hdr, uint len, uint doff)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003117{
3118 bcmsdh_info_t *sdh = bus->sdh;
3119 uint rdlen, pad;
3120
3121 int sdret;
3122
3123 DHD_TRACE(("%s: Enter\n", __func__));
3124
3125 /* Control data already received in aligned rxctl */
3126 if ((bus->bus == SPI_BUS) && (!bus->usebufpool))
3127 goto gotpkt;
3128
3129 ASSERT(bus->rxbuf);
3130 /* Set rxctl for frame (w/optional alignment) */
3131 bus->rxctl = bus->rxbuf;
3132 if (dhd_alignctl) {
3133 bus->rxctl += firstread;
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07003134 pad = ((unsigned long)bus->rxctl % DHD_SDALIGN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003135 if (pad)
3136 bus->rxctl += (DHD_SDALIGN - pad);
3137 bus->rxctl -= firstread;
3138 }
3139 ASSERT(bus->rxctl >= bus->rxbuf);
3140
3141 /* Copy the already-read portion over */
Stanislav Fomichev02160692011-02-15 01:05:10 +03003142 memcpy(bus->rxctl, hdr, firstread);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003143 if (len <= firstread)
3144 goto gotpkt;
3145
3146 /* Copy the full data pkt in gSPI case and process ioctl. */
3147 if (bus->bus == SPI_BUS) {
Stanislav Fomichev02160692011-02-15 01:05:10 +03003148 memcpy(bus->rxctl, hdr, len);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003149 goto gotpkt;
3150 }
3151
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))
3158 rdlen += pad;
3159 } else if (rdlen % DHD_SDALIGN) {
3160 rdlen += DHD_SDALIGN - (rdlen % DHD_SDALIGN);
3161 }
3162
3163 /* Satisfy length-alignment requirements */
3164 if (forcealign && (rdlen & (ALIGNMENT - 1)))
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07003165 rdlen = roundup(rdlen, ALIGNMENT);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003166
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++;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003172 dhdsdio_rxfail(bus, false, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003173 goto done;
3174 }
3175
3176 if ((len - doff) > bus->dhd->maxctl) {
3177 DHD_ERROR(("%s: %d-byte ctl frame (%d-byte ctl data) exceeds "
3178 "%d-byte limit\n",
3179 __func__, len, (len - doff), bus->dhd->maxctl));
3180 bus->dhd->rx_errors++;
3181 bus->rx_toolong++;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003182 dhdsdio_rxfail(bus, false, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003183 goto done;
3184 }
3185
3186 /* Read remainder of frame body into the rxctl buffer */
Grant Grundler4b455e02011-05-04 09:59:47 -07003187 sdret = bcmsdh_recv_buf(bus, bcmsdh_cur_sbwad(sdh), SDIO_FUNC_2,
3188 F2SYNC, (bus->rxctl + firstread), rdlen,
3189 NULL, NULL, NULL);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003190 bus->f2rxdata++;
Roland Vossenb7ef2a92011-05-03 11:35:02 +02003191 ASSERT(sdret != -BCME_PENDING);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003192
3193 /* Control frame failures need retransmission */
3194 if (sdret < 0) {
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 */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003198 dhdsdio_rxfail(bus, true, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003199 goto done;
3200 }
3201
3202gotpkt:
3203
3204#ifdef DHD_DEBUG
Arend van Spriel34227312011-05-10 22:25:32 +02003205 if (DHD_BYTES_ON() && DHD_CTL_ON()) {
3206 printk(KERN_DEBUG "RxCtrl:\n");
3207 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, bus->rxctl, len);
3208 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003209#endif
3210
3211 /* Point to valid data and indicate its length */
3212 bus->rxctl += doff;
3213 bus->rxlen = len - doff;
3214
3215done:
3216 /* Awake any waiters */
3217 dhd_os_ioctl_resp_wake(bus->dhd);
3218}
3219
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003220static u8 dhdsdio_rxglom(dhd_bus_t *bus, u8 rxseq)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003221{
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07003222 u16 dlen, totlen;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003223 u8 *dptr, num = 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003224
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07003225 u16 sublen, check;
Arend van Sprielc26b1372010-11-23 14:06:23 +01003226 struct sk_buff *pfirst, *plast, *pnext, *save_pfirst;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003227
3228 int errcode;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003229 u8 chan, seq, doff, sfdoff;
3230 u8 txmax;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003231
3232 int ifidx = 0;
3233 bool usechain = bus->use_rxchain;
3234
3235 /* If packets, issue read(s) and send up packet chain */
3236 /* Return sequence numbers consumed? */
3237
3238 DHD_TRACE(("dhdsdio_rxglom: start: glomd %p glom %p\n", bus->glomd,
3239 bus->glom));
3240
3241 /* If there's a descriptor, generate the packet chain */
3242 if (bus->glomd) {
3243 dhd_os_sdlock_rxq(bus->dhd);
3244
3245 pfirst = plast = pnext = NULL;
Arend van Spriel54991ad2010-11-23 14:06:24 +01003246 dlen = (u16) (bus->glomd->len);
3247 dptr = bus->glomd->data;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003248 if (!dlen || (dlen & 1)) {
3249 DHD_ERROR(("%s: bad glomd len(%d), ignore descriptor\n",
3250 __func__, dlen));
3251 dlen = 0;
3252 }
3253
3254 for (totlen = num = 0; dlen; num++) {
3255 /* Get (and move past) next length */
Stanislav Fomichev56dfe3c2011-02-20 21:43:33 +03003256 sublen = get_unaligned_le16(dptr);
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07003257 dlen -= sizeof(u16);
3258 dptr += sizeof(u16);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003259 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));
3263 pnext = NULL;
3264 break;
3265 }
3266 if (sublen % DHD_SDALIGN) {
3267 DHD_ERROR(("%s: sublen %d not multiple of %d\n",
3268 __func__, sublen, DHD_SDALIGN));
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003269 usechain = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003270 }
3271 totlen += sublen;
3272
3273 /* For last frame, adjust read len so total
3274 is a block multiple */
3275 if (!dlen) {
3276 sublen +=
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07003277 (roundup(totlen, bus->blocksize) - totlen);
3278 totlen = roundup(totlen, bus->blocksize);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003279 }
3280
3281 /* Allocate/chain packet for next subframe */
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02003282 pnext = bcm_pkt_buf_get_skb(sublen + DHD_SDALIGN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003283 if (pnext == NULL) {
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02003284 DHD_ERROR(("%s: bcm_pkt_buf_get_skb failed, "
3285 "num %d len %d\n", __func__,
3286 num, sublen));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003287 break;
3288 }
Arend van Spriel54991ad2010-11-23 14:06:24 +01003289 ASSERT(!(pnext->prev));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003290 if (!pfirst) {
3291 ASSERT(!plast);
3292 pfirst = plast = pnext;
3293 } else {
3294 ASSERT(plast);
Arend van Spriel54991ad2010-11-23 14:06:24 +01003295 plast->next = pnext;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003296 plast = pnext;
3297 }
3298
3299 /* Adhere to start alignment requirements */
Arend van Spriel3c9d4c32011-03-02 21:18:48 +01003300 PKTALIGN(pnext, sublen, DHD_SDALIGN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003301 }
3302
3303 /* If all allocations succeeded, save packet chain
3304 in bus structure */
3305 if (pnext) {
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,
3312 totlen, rxseq));
3313 }
3314 }
3315 bus->glom = pfirst;
3316 pfirst = pnext = NULL;
3317 } else {
3318 if (pfirst)
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02003319 bcm_pkt_buf_free_skb(pfirst);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003320 bus->glom = NULL;
3321 num = 0;
3322 }
3323
3324 /* Done with descriptor packet */
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02003325 bcm_pkt_buf_free_skb(bus->glomd);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003326 bus->glomd = NULL;
3327 bus->nextlen = 0;
3328
3329 dhd_os_sdunlock_rxq(bus->dhd);
3330 }
3331
3332 /* Ok -- either we just generated a packet chain,
3333 or had one from before */
3334 if (bus->glom) {
3335 if (DHD_GLOM_ON()) {
3336 DHD_GLOM(("%s: try superframe read, packet chain:\n",
3337 __func__));
Arend van Spriel54991ad2010-11-23 14:06:24 +01003338 for (pnext = bus->glom; pnext; pnext = pnext->next) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003339 DHD_GLOM((" %p: %p len 0x%04x (%d)\n",
Arend van Spriel54991ad2010-11-23 14:06:24 +01003340 pnext, (u8 *) (pnext->data),
3341 pnext->len, pnext->len));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003342 }
3343 }
3344
3345 pfirst = bus->glom;
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02003346 dlen = (u16) bcm_pkttotlen(pfirst);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003347
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.
3351 */
3352 if (usechain) {
Grant Grundler4b455e02011-05-04 09:59:47 -07003353 errcode = bcmsdh_recv_buf(bus,
3354 bcmsdh_cur_sbwad(bus->sdh), SDIO_FUNC_2,
3355 F2SYNC, (u8 *) pfirst->data, dlen,
3356 pfirst, NULL, NULL);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003357 } else if (bus->dataptr) {
Grant Grundler4b455e02011-05-04 09:59:47 -07003358 errcode = bcmsdh_recv_buf(bus,
3359 bcmsdh_cur_sbwad(bus->sdh), SDIO_FUNC_2,
3360 F2SYNC, bus->dataptr, dlen,
3361 NULL, NULL, NULL);
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02003362 sublen = (u16) bcm_pktfrombuf(pfirst, 0, dlen,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003363 bus->dataptr);
3364 if (sublen != dlen) {
3365 DHD_ERROR(("%s: FAILED TO COPY, dlen %d sublen %d\n",
3366 __func__, dlen, sublen));
3367 errcode = -1;
3368 }
3369 pnext = NULL;
3370 } else {
3371 DHD_ERROR(("COULDN'T ALLOC %d-BYTE GLOM, FORCE FAILURE\n",
3372 dlen));
3373 errcode = -1;
3374 }
3375 bus->f2rxdata++;
Roland Vossenb7ef2a92011-05-03 11:35:02 +02003376 ASSERT(errcode != -BCME_PENDING);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003377
3378 /* On failure, kill the superframe, allow a couple retries */
3379 if (errcode < 0) {
3380 DHD_ERROR(("%s: glom read of %d bytes failed: %d\n",
3381 __func__, dlen, errcode));
3382 bus->dhd->rx_errors++;
3383
3384 if (bus->glomerr++ < 3) {
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003385 dhdsdio_rxfail(bus, true, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003386 } else {
3387 bus->glomerr = 0;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003388 dhdsdio_rxfail(bus, true, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003389 dhd_os_sdlock_rxq(bus->dhd);
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02003390 bcm_pkt_buf_free_skb(bus->glom);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003391 dhd_os_sdunlock_rxq(bus->dhd);
3392 bus->rxglomfail++;
3393 bus->glom = NULL;
3394 }
3395 return 0;
3396 }
3397#ifdef DHD_DEBUG
3398 if (DHD_GLOM_ON()) {
Arend van Spriel34227312011-05-10 22:25:32 +02003399 printk(KERN_DEBUG "SUPERFRAME:\n");
3400 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
3401 pfirst->data, min_t(int, pfirst->len, 48));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003402 }
3403#endif
3404
3405 /* Validate the superframe header */
Arend van Spriel54991ad2010-11-23 14:06:24 +01003406 dptr = (u8 *) (pfirst->data);
Stanislav Fomichev56dfe3c2011-02-20 21:43:33 +03003407 sublen = get_unaligned_le16(dptr);
3408 check = get_unaligned_le16(dptr + sizeof(u16));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003409
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));
3416 bus->nextlen = 0;
3417 }
3418 doff = SDPCM_DOFFSET_VALUE(&dptr[SDPCM_FRAMETAG_LEN]);
3419 txmax = SDPCM_WINDOW_VALUE(&dptr[SDPCM_FRAMETAG_LEN]);
3420
3421 errcode = 0;
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07003422 if ((u16)~(sublen ^ check)) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003423 DHD_ERROR(("%s (superframe): HW hdr error: len/check "
3424 "0x%04x/0x%04x\n", __func__, sublen, check));
3425 errcode = -1;
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07003426 } else if (roundup(sublen, bus->blocksize) != dlen) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003427 DHD_ERROR(("%s (superframe): len 0x%04x, rounded "
3428 "0x%04x, expect 0x%04x\n",
3429 __func__, sublen,
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07003430 roundup(sublen, bus->blocksize), dlen));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003431 errcode = -1;
3432 } else if (SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]) !=
3433 SDPCM_GLOM_CHANNEL) {
3434 DHD_ERROR(("%s (superframe): bad channel %d\n",
3435 __func__,
3436 SDPCM_PACKET_CHANNEL(&dptr
3437 [SDPCM_FRAMETAG_LEN])));
3438 errcode = -1;
3439 } else if (SDPCM_GLOMDESC(&dptr[SDPCM_FRAMETAG_LEN])) {
3440 DHD_ERROR(("%s (superframe): got second descriptor?\n",
3441 __func__));
3442 errcode = -1;
3443 } else if ((doff < SDPCM_HDRLEN) ||
Arend van Spriel54991ad2010-11-23 14:06:24 +01003444 (doff > (pfirst->len - SDPCM_HDRLEN))) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003445 DHD_ERROR(("%s (superframe): Bad data offset %d: HW %d "
3446 "pkt %d min %d\n",
3447 __func__, doff, sublen,
Arend van Spriel54991ad2010-11-23 14:06:24 +01003448 pfirst->len, SDPCM_HDRLEN));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003449 errcode = -1;
3450 }
3451
3452 /* Check sequence number of superframe SW header */
3453 if (rxseq != seq) {
3454 DHD_INFO(("%s: (superframe) rx_seq %d, expected %d\n",
3455 __func__, seq, rxseq));
3456 bus->rx_badseq++;
3457 rxseq = seq;
3458 }
3459
3460 /* Check window for sanity */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003461 if ((u8) (txmax - bus->tx_seq) > 0x40) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003462 DHD_ERROR(("%s: unlikely tx max %d with tx_seq %d\n",
3463 __func__, txmax, bus->tx_seq));
3464 txmax = bus->tx_seq + 2;
3465 }
3466 bus->tx_max = txmax;
3467
3468 /* Remove superframe header, remember offset */
Arend van Sprielc303ecb2010-11-18 20:46:43 +01003469 skb_pull(pfirst, doff);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003470 sfdoff = doff;
3471
3472 /* Validate all the subframe headers */
3473 for (num = 0, pnext = pfirst; pnext && !errcode;
Arend van Spriel54991ad2010-11-23 14:06:24 +01003474 num++, pnext = pnext->next) {
3475 dptr = (u8 *) (pnext->data);
3476 dlen = (u16) (pnext->len);
Stanislav Fomichev56dfe3c2011-02-20 21:43:33 +03003477 sublen = get_unaligned_le16(dptr);
3478 check = get_unaligned_le16(dptr + sizeof(u16));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003479 chan = SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]);
3480 doff = SDPCM_DOFFSET_VALUE(&dptr[SDPCM_FRAMETAG_LEN]);
3481#ifdef DHD_DEBUG
Arend van Spriel34227312011-05-10 22:25:32 +02003482 if (DHD_GLOM_ON()) {
3483 printk(KERN_DEBUG "subframe:\n");
3484 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
3485 dptr, 32);
3486 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003487#endif
3488
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07003489 if ((u16)~(sublen ^ check)) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003490 DHD_ERROR(("%s (subframe %d): HW hdr error: "
3491 "len/check 0x%04x/0x%04x\n",
3492 __func__, num, sublen, check));
3493 errcode = -1;
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));
3498 errcode = -1;
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));
3503 errcode = -1;
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,
3507 SDPCM_HDRLEN));
3508 errcode = -1;
3509 }
3510 }
3511
3512 if (errcode) {
3513 /* Terminate frame on error, request
3514 a couple retries */
3515 if (bus->glomerr++ < 3) {
3516 /* Restore superframe header space */
Arend van Sprielc303ecb2010-11-18 20:46:43 +01003517 skb_push(pfirst, sfdoff);
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003518 dhdsdio_rxfail(bus, true, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003519 } else {
3520 bus->glomerr = 0;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003521 dhdsdio_rxfail(bus, true, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003522 dhd_os_sdlock_rxq(bus->dhd);
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02003523 bcm_pkt_buf_free_skb(bus->glom);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003524 dhd_os_sdunlock_rxq(bus->dhd);
3525 bus->rxglomfail++;
3526 bus->glom = NULL;
3527 }
3528 bus->nextlen = 0;
3529 return 0;
3530 }
3531
3532 /* Basic SD framing looks ok - process each packet (header) */
3533 save_pfirst = pfirst;
3534 bus->glom = NULL;
3535 plast = NULL;
3536
3537 dhd_os_sdlock_rxq(bus->dhd);
3538 for (num = 0; pfirst; rxseq++, pfirst = pnext) {
Arend van Spriel54991ad2010-11-23 14:06:24 +01003539 pnext = pfirst->next;
3540 pfirst->next = NULL;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003541
Arend van Spriel54991ad2010-11-23 14:06:24 +01003542 dptr = (u8 *) (pfirst->data);
Stanislav Fomichev56dfe3c2011-02-20 21:43:33 +03003543 sublen = get_unaligned_le16(dptr);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003544 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]);
3547
3548 DHD_GLOM(("%s: Get subframe %d, %p(%p/%d), sublen %d "
3549 "chan %d seq %d\n",
Arend van Spriel54991ad2010-11-23 14:06:24 +01003550 __func__, num, pfirst, pfirst->data,
3551 pfirst->len, sublen, chan, seq));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003552
3553 ASSERT((chan == SDPCM_DATA_CHANNEL)
3554 || (chan == SDPCM_EVENT_CHANNEL));
3555
3556 if (rxseq != seq) {
3557 DHD_GLOM(("%s: rx_seq %d, expected %d\n",
3558 __func__, seq, rxseq));
3559 bus->rx_badseq++;
3560 rxseq = seq;
3561 }
3562#ifdef DHD_DEBUG
Arend van Spriel34227312011-05-10 22:25:32 +02003563 if (DHD_BYTES_ON() && DHD_DATA_ON()) {
3564 printk(KERN_DEBUG "Rx Subframe Data:\n");
3565 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
3566 dptr, dlen);
3567 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003568#endif
3569
Arend van Spriel2cb8ada2010-11-18 20:46:44 +01003570 __skb_trim(pfirst, sublen);
Arend van Sprielc303ecb2010-11-18 20:46:43 +01003571 skb_pull(pfirst, doff);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003572
Arend van Spriel54991ad2010-11-23 14:06:24 +01003573 if (pfirst->len == 0) {
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02003574 bcm_pkt_buf_free_skb(pfirst);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003575 if (plast) {
Arend van Spriel54991ad2010-11-23 14:06:24 +01003576 plast->next = pnext;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003577 } else {
3578 ASSERT(save_pfirst == pfirst);
3579 save_pfirst = pnext;
3580 }
3581 continue;
3582 } else if (dhd_prot_hdrpull(bus->dhd, &ifidx, pfirst) !=
3583 0) {
3584 DHD_ERROR(("%s: rx protocol error\n",
3585 __func__));
3586 bus->dhd->rx_errors++;
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02003587 bcm_pkt_buf_free_skb(pfirst);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003588 if (plast) {
Arend van Spriel54991ad2010-11-23 14:06:24 +01003589 plast->next = pnext;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003590 } else {
3591 ASSERT(save_pfirst == pfirst);
3592 save_pfirst = pnext;
3593 }
3594 continue;
3595 }
3596
3597 /* this packet will go up, link back into
3598 chain and count it */
Arend van Spriel54991ad2010-11-23 14:06:24 +01003599 pfirst->next = pnext;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003600 plast = pfirst;
3601 num++;
3602
3603#ifdef DHD_DEBUG
3604 if (DHD_GLOM_ON()) {
3605 DHD_GLOM(("%s subframe %d to stack, %p(%p/%d) "
3606 "nxt/lnk %p/%p\n",
Arend van Spriel54991ad2010-11-23 14:06:24 +01003607 __func__, num, pfirst, pfirst->data,
3608 pfirst->len, pfirst->next,
3609 pfirst->prev));
Arend van Spriel34227312011-05-10 22:25:32 +02003610 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
3611 pfirst->data,
3612 min_t(int, pfirst->len, 32));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003613 }
3614#endif /* DHD_DEBUG */
3615 }
3616 dhd_os_sdunlock_rxq(bus->dhd);
3617 if (num) {
3618 dhd_os_sdunlock(bus->dhd);
3619 dhd_rx_frame(bus->dhd, ifidx, save_pfirst, num);
3620 dhd_os_sdlock(bus->dhd);
3621 }
3622
3623 bus->rxglomframes++;
3624 bus->rxglompkts += num;
3625 }
3626 return num;
3627}
3628
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003629/* Return true if there may be more frames to read */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003630static uint dhdsdio_readframes(dhd_bus_t *bus, uint maxframes, bool *finished)
3631{
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003632 bcmsdh_info_t *sdh = bus->sdh;
3633
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07003634 u16 len, check; /* Extracted hardware header fields */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003635 u8 chan, seq, doff; /* Extracted software header fields */
3636 u8 fcbits; /* Extracted fcbits from software header */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003637
Arend van Sprielc26b1372010-11-23 14:06:23 +01003638 struct sk_buff *pkt; /* Packet for event or data frames */
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07003639 u16 pad; /* Number of pad bytes to read */
3640 u16 rdlen; /* Total number of bytes to read */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003641 u8 rxseq; /* Next sequence number to expect */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003642 uint rxleft = 0; /* Remaining number of frames allowed */
3643 int sdret; /* Return code from bcmsdh calls */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003644 u8 txmax; /* Maximum tx sequence offered */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003645 bool len_consistent; /* Result of comparing readahead len and
3646 len from hw-hdr */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003647 u8 *rxbuf;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003648 int ifidx = 0;
3649 uint rxcount = 0; /* Total frames read */
3650
3651#if defined(DHD_DEBUG) || defined(SDTEST)
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003652 bool sdtest = false; /* To limit message spew from test mode */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003653#endif
3654
3655 DHD_TRACE(("%s: Enter\n", __func__));
3656
3657 ASSERT(maxframes);
3658
3659#ifdef SDTEST
3660 /* Allow pktgen to override maxframes */
3661 if (bus->pktgen_count && (bus->pktgen_mode == DHD_PKTGEN_RECV)) {
3662 maxframes = bus->pktgen_count;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003663 sdtest = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003664 }
3665#endif
3666
3667 /* Not finished unless we encounter no more frames indication */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003668 *finished = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003669
3670 for (rxseq = bus->rx_seq, rxleft = maxframes;
3671 !bus->rxskip && rxleft && bus->dhd->busstate != DHD_BUS_DOWN;
3672 rxseq++, rxleft--) {
3673
3674 /* Handle glomming separately */
3675 if (bus->glom || bus->glomd) {
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003676 u8 cnt;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003677 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));
3681 rxseq += cnt - 1;
3682 rxleft = (rxleft > cnt) ? (rxleft - cnt) : 1;
3683 continue;
3684 }
3685
3686 /* Try doing single read if we can */
3687 if (dhd_readahead && bus->nextlen) {
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07003688 u16 nextlen = bus->nextlen;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003689 bus->nextlen = 0;
3690
3691 if (bus->bus == SPI_BUS) {
3692 rdlen = len = nextlen;
3693 } else {
3694 rdlen = len = nextlen << 4;
3695
3696 /* Pad read to blocksize for efficiency */
3697 if (bus->roundup && bus->blocksize
3698 && (rdlen > bus->blocksize)) {
3699 pad =
3700 bus->blocksize -
3701 (rdlen % bus->blocksize);
3702 if ((pad <= bus->roundup)
3703 && (pad < bus->blocksize)
3704 && ((rdlen + pad + firstread) <
3705 MAX_RX_DATASZ))
3706 rdlen += pad;
3707 } else if (rdlen % DHD_SDALIGN) {
3708 rdlen +=
3709 DHD_SDALIGN - (rdlen % DHD_SDALIGN);
3710 }
3711 }
3712
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.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003717 * This is required because dhd receives full
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003718 * 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.
3722 */
3723 /* Allocate a packet buffer */
3724 dhd_os_sdlock_rxq(bus->dhd);
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02003725 pkt = bcm_pkt_buf_get_skb(rdlen + DHD_SDALIGN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003726 if (!pkt) {
3727 if (bus->bus == SPI_BUS) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003728 bus->usebufpool = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003729 bus->rxctl = bus->rxbuf;
3730 if (dhd_alignctl) {
3731 bus->rxctl += firstread;
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07003732 pad = ((unsigned long)bus->rxctl %
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003733 DHD_SDALIGN);
3734 if (pad)
3735 bus->rxctl +=
3736 (DHD_SDALIGN - pad);
3737 bus->rxctl -= firstread;
3738 }
3739 ASSERT(bus->rxctl >= bus->rxbuf);
3740 rxbuf = bus->rxctl;
3741 /* Read the entire frame */
Grant Grundler4b455e02011-05-04 09:59:47 -07003742 sdret = bcmsdh_recv_buf(bus,
3743 bcmsdh_cur_sbwad(sdh),
3744 SDIO_FUNC_2, F2SYNC,
3745 rxbuf, rdlen,
3746 NULL, NULL, NULL);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003747 bus->f2rxdata++;
Roland Vossenb7ef2a92011-05-03 11:35:02 +02003748 ASSERT(sdret != -BCME_PENDING);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003749
3750 /* Control frame failures need
3751 retransmission */
3752 if (sdret < 0) {
3753 DHD_ERROR(("%s: read %d control bytes failed: %d\n",
3754 __func__,
3755 rdlen, sdret));
3756 /* dhd.rx_ctlerrs is higher */
3757 bus->rxc_errors++;
3758 dhd_os_sdunlock_rxq(bus->dhd);
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003759 dhdsdio_rxfail(bus, true,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003760 (bus->bus ==
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003761 SPI_BUS) ? false
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003762 : true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003763 continue;
3764 }
3765 } else {
3766 /* Give up on data,
3767 request rtx of events */
Arend van Sprielcda64a52011-05-10 22:25:33 +02003768 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));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003773 /* Just go try again w/normal
3774 header read */
3775 dhd_os_sdunlock_rxq(bus->dhd);
3776 continue;
3777 }
3778 } else {
3779 if (bus->bus == SPI_BUS)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003780 bus->usebufpool = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003781
Arend van Spriel54991ad2010-11-23 14:06:24 +01003782 ASSERT(!(pkt->prev));
Arend van Spriel3c9d4c32011-03-02 21:18:48 +01003783 PKTALIGN(pkt, rdlen, DHD_SDALIGN);
Arend van Spriel54991ad2010-11-23 14:06:24 +01003784 rxbuf = (u8 *) (pkt->data);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003785 /* Read the entire frame */
Grant Grundler4b455e02011-05-04 09:59:47 -07003786 sdret = bcmsdh_recv_buf(bus,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003787 bcmsdh_cur_sbwad(sdh),
3788 SDIO_FUNC_2, F2SYNC,
Grant Grundler4b455e02011-05-04 09:59:47 -07003789 rxbuf, rdlen,
3790 pkt, NULL, NULL);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003791 bus->f2rxdata++;
Roland Vossenb7ef2a92011-05-03 11:35:02 +02003792 ASSERT(sdret != -BCME_PENDING);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003793
3794 if (sdret < 0) {
3795 DHD_ERROR(("%s (nextlen): read %d bytes failed: %d\n",
3796 __func__, rdlen, sdret));
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02003797 bcm_pkt_buf_free_skb(pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003798 bus->dhd->rx_errors++;
3799 dhd_os_sdunlock_rxq(bus->dhd);
3800 /* Force retry w/normal header read.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003801 * Don't attempt NAK for
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003802 * gSPI
3803 */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003804 dhdsdio_rxfail(bus, true,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003805 (bus->bus ==
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003806 SPI_BUS) ? false :
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003807 true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003808 continue;
3809 }
3810 }
3811 dhd_os_sdunlock_rxq(bus->dhd);
3812
3813 /* Now check the header */
Stanislav Fomichev02160692011-02-15 01:05:10 +03003814 memcpy(bus->rxhdr, rxbuf, SDPCM_HDRLEN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003815
3816 /* Extract hardware header fields */
Stanislav Fomichev56dfe3c2011-02-20 21:43:33 +03003817 len = get_unaligned_le16(bus->rxhdr);
3818 check = get_unaligned_le16(bus->rxhdr + sizeof(u16));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003819
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__));
Grant Grundler4b455e02011-05-04 09:59:47 -07003824 dhdsdio_pktfree2(bus, pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003825 continue;
3826 }
3827
3828 /* Validate check bytes */
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07003829 if ((u16)~(len ^ check)) {
Grant Grundler4b455e02011-05-04 09:59:47 -07003830 DHD_ERROR(("%s (nextlen): HW hdr error:"
3831 " nextlen/len/check"
3832 " 0x%04x/0x%04x/0x%04x\n",
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003833 __func__, nextlen, len, check));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003834 bus->rx_badhdr++;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003835 dhdsdio_rxfail(bus, false, false);
Grant Grundler4b455e02011-05-04 09:59:47 -07003836 dhdsdio_pktfree2(bus, pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003837 continue;
3838 }
3839
3840 /* Validate frame length */
3841 if (len < SDPCM_HDRLEN) {
3842 DHD_ERROR(("%s (nextlen): HW hdr length "
3843 "invalid: %d\n", __func__, len));
Grant Grundler4b455e02011-05-04 09:59:47 -07003844 dhdsdio_pktfree2(bus, pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003845 continue;
3846 }
3847
3848 /* Check for consistency withreadahead info */
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07003849 len_consistent = (nextlen != (roundup(len, 16) >> 4));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003850 if (len_consistent) {
3851 /* Mismatch, force retry w/normal
3852 header (may be >4K) */
Grant Grundler4b455e02011-05-04 09:59:47 -07003853 DHD_ERROR(("%s (nextlen): mismatch, "
3854 "nextlen %d len %d rnd %d; "
3855 "expected rxseq %d\n",
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003856 __func__, nextlen,
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07003857 len, roundup(len, 16), rxseq));
Grant Grundler4b455e02011-05-04 09:59:47 -07003858 dhdsdio_rxfail(bus, true, (bus->bus != SPI_BUS));
3859 dhdsdio_pktfree2(bus, pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003860 continue;
3861 }
3862
3863 /* Extract software header fields */
Grant Grundler4b455e02011-05-04 09:59:47 -07003864 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]);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003872
3873 bus->nextlen =
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));
3879 bus->nextlen = 0;
3880 }
3881
3882 bus->dhd->rx_readahead_cnt++;
Grant Grundler4b455e02011-05-04 09:59:47 -07003883
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003884 /* Handle Flow Control */
Grant Grundler4b455e02011-05-04 09:59:47 -07003885 fcbits = SDPCM_FCMASK_VALUE(
3886 &bus->rxhdr[SDPCM_FRAMETAG_LEN]);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003887
Grant Grundler4b455e02011-05-04 09:59:47 -07003888 if (bus->flowcontrol != fcbits) {
3889 if (~bus->flowcontrol & fcbits)
3890 bus->fc_xoff++;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003891
Grant Grundler4b455e02011-05-04 09:59:47 -07003892 if (bus->flowcontrol & ~fcbits)
3893 bus->fc_xon++;
3894
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003895 bus->fc_rcvd++;
3896 bus->flowcontrol = fcbits;
3897 }
3898
3899 /* Check and update sequence number */
3900 if (rxseq != seq) {
3901 DHD_INFO(("%s (nextlen): rx_seq %d, expected "
3902 "%d\n", __func__, seq, rxseq));
3903 bus->rx_badseq++;
3904 rxseq = seq;
3905 }
3906
3907 /* Check window for sanity */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003908 if ((u8) (txmax - bus->tx_seq) > 0x40) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003909 DHD_ERROR(("%s: got unlikely tx max %d with "
3910 "tx_seq %d\n",
3911 __func__, txmax, bus->tx_seq));
3912 txmax = bus->tx_seq + 2;
3913 }
3914 bus->tx_max = txmax;
3915
3916#ifdef DHD_DEBUG
Arend van Spriel34227312011-05-10 22:25:32 +02003917 if (DHD_BYTES_ON() && DHD_DATA_ON()) {
3918 printk(KERN_DEBUG "Rx Data:\n");
3919 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
3920 rxbuf, len);
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);
3925 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003926#endif
3927
3928 if (chan == SDPCM_CONTROL_CHANNEL) {
3929 if (bus->bus == SPI_BUS) {
3930 dhdsdio_read_control(bus, rxbuf, len,
3931 doff);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003932 } else {
3933 DHD_ERROR(("%s (nextlen): readahead on control" " packet %d?\n",
3934 __func__, seq));
3935 /* Force retry w/normal header read */
3936 bus->nextlen = 0;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003937 dhdsdio_rxfail(bus, false, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003938 }
Grant Grundler4b455e02011-05-04 09:59:47 -07003939 dhdsdio_pktfree2(bus, pkt);
3940 continue;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003941 }
3942
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",
3945 len, chan));
3946 continue;
3947 }
3948
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));
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003953 dhdsdio_rxfail(bus, false, false);
Grant Grundler4b455e02011-05-04 09:59:47 -07003954 dhdsdio_pktfree2(bus, pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003955 continue;
3956 }
3957
3958 /* All done with this one -- now deliver the packet */
3959 goto deliver;
3960 }
3961 /* gSPI frames should not be handled in fractions */
3962 if (bus->bus == SPI_BUS)
3963 break;
3964
3965 /* Read frame header (hardware and software) */
Grant Grundler4b455e02011-05-04 09:59:47 -07003966 sdret = bcmsdh_recv_buf(bus, bcmsdh_cur_sbwad(sdh),
3967 SDIO_FUNC_2, F2SYNC, bus->rxhdr, firstread,
3968 NULL, NULL, NULL);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003969 bus->f2rxhdrs++;
Roland Vossenb7ef2a92011-05-03 11:35:02 +02003970 ASSERT(sdret != -BCME_PENDING);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003971
3972 if (sdret < 0) {
3973 DHD_ERROR(("%s: RXHEADER FAILED: %d\n", __func__,
3974 sdret));
3975 bus->rx_hdrfail++;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003976 dhdsdio_rxfail(bus, true, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003977 continue;
3978 }
3979#ifdef DHD_DEBUG
Arend van Spriel34227312011-05-10 22:25:32 +02003980 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);
3984 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003985#endif
3986
3987 /* Extract hardware header fields */
Stanislav Fomichev56dfe3c2011-02-20 21:43:33 +03003988 len = get_unaligned_le16(bus->rxhdr);
3989 check = get_unaligned_le16(bus->rxhdr + sizeof(u16));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003990
3991 /* All zeros means no more frames */
3992 if (!(len | check)) {
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003993 *finished = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003994 break;
3995 }
3996
3997 /* Validate check bytes */
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07003998 if ((u16) ~(len ^ check)) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003999 DHD_ERROR(("%s: HW hdr err: len/check 0x%04x/0x%04x\n",
4000 __func__, len, check));
4001 bus->rx_badhdr++;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004002 dhdsdio_rxfail(bus, false, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004003 continue;
4004 }
4005
4006 /* Validate frame length */
4007 if (len < SDPCM_HDRLEN) {
4008 DHD_ERROR(("%s: HW hdr length invalid: %d\n",
4009 __func__, len));
4010 continue;
4011 }
4012
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]);
4018
4019 /* Validate data offset */
4020 if ((doff < SDPCM_HDRLEN) || (doff > len)) {
4021 DHD_ERROR(("%s: Bad data offset %d: HW len %d, min %d "
4022 "seq %d\n",
4023 __func__, doff, len, SDPCM_HDRLEN, seq));
4024 bus->rx_badhdr++;
4025 ASSERT(0);
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004026 dhdsdio_rxfail(bus, false, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004027 continue;
4028 }
4029
4030 /* Save the readahead length if there is one */
4031 bus->nextlen =
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 "
4035 "(%d), seq %d\n",
4036 __func__, bus->nextlen, seq));
4037 bus->nextlen = 0;
4038 }
4039
4040 /* Handle Flow Control */
4041 fcbits = SDPCM_FCMASK_VALUE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
4042
Grant Grundler4b455e02011-05-04 09:59:47 -07004043 if (bus->flowcontrol != fcbits) {
4044 if (~bus->flowcontrol & fcbits)
4045 bus->fc_xoff++;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004046
Grant Grundler4b455e02011-05-04 09:59:47 -07004047 if (bus->flowcontrol & ~fcbits)
4048 bus->fc_xon++;
4049
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004050 bus->fc_rcvd++;
4051 bus->flowcontrol = fcbits;
4052 }
4053
4054 /* Check and update sequence number */
4055 if (rxseq != seq) {
4056 DHD_INFO(("%s: rx_seq %d, expected %d\n", __func__,
4057 seq, rxseq));
4058 bus->rx_badseq++;
4059 rxseq = seq;
4060 }
4061
4062 /* Check window for sanity */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004063 if ((u8) (txmax - bus->tx_seq) > 0x40) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004064 DHD_ERROR(("%s: unlikely tx max %d with tx_seq %d\n",
4065 __func__, txmax, bus->tx_seq));
4066 txmax = bus->tx_seq + 2;
4067 }
4068 bus->tx_max = txmax;
4069
4070 /* Call a separate function for control frames */
4071 if (chan == SDPCM_CONTROL_CHANNEL) {
4072 dhdsdio_read_control(bus, bus->rxhdr, len, doff);
4073 continue;
4074 }
4075
4076 ASSERT((chan == SDPCM_DATA_CHANNEL)
4077 || (chan == SDPCM_EVENT_CHANNEL)
4078 || (chan == SDPCM_TEST_CHANNEL)
4079 || (chan == SDPCM_GLOM_CHANNEL));
4080
4081 /* Length to read */
4082 rdlen = (len > firstread) ? (len - firstread) : 0;
4083
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))
4090 rdlen += pad;
4091 } else if (rdlen % DHD_SDALIGN) {
4092 rdlen += DHD_SDALIGN - (rdlen % DHD_SDALIGN);
4093 }
4094
4095 /* Satisfy length-alignment requirements */
4096 if (forcealign && (rdlen & (ALIGNMENT - 1)))
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07004097 rdlen = roundup(rdlen, ALIGNMENT);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004098
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++;
4104 bus->rx_toolong++;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004105 dhdsdio_rxfail(bus, false, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004106 continue;
4107 }
4108
4109 dhd_os_sdlock_rxq(bus->dhd);
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004110 pkt = bcm_pkt_buf_get_skb(rdlen + firstread + DHD_SDALIGN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004111 if (!pkt) {
4112 /* Give up on data, request rtx of events */
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004113 DHD_ERROR(("%s: bcm_pkt_buf_get_skb failed: rdlen %d "
4114 "chan %d\n", __func__, rdlen, chan));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004115 bus->dhd->rx_dropped++;
4116 dhd_os_sdunlock_rxq(bus->dhd);
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004117 dhdsdio_rxfail(bus, false, RETRYCHAN(chan));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004118 continue;
4119 }
4120 dhd_os_sdunlock_rxq(bus->dhd);
4121
Arend van Spriel54991ad2010-11-23 14:06:24 +01004122 ASSERT(!(pkt->prev));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004123
4124 /* Leave room for what we already read, and align remainder */
Arend van Spriel54991ad2010-11-23 14:06:24 +01004125 ASSERT(firstread < pkt->len);
Arend van Sprielc303ecb2010-11-18 20:46:43 +01004126 skb_pull(pkt, firstread);
Arend van Spriel3c9d4c32011-03-02 21:18:48 +01004127 PKTALIGN(pkt, rdlen, DHD_SDALIGN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004128
4129 /* Read the remaining frame data */
Grant Grundler4b455e02011-05-04 09:59:47 -07004130 sdret = bcmsdh_recv_buf(bus, bcmsdh_cur_sbwad(sdh), SDIO_FUNC_2,
Arend van Spriel54991ad2010-11-23 14:06:24 +01004131 F2SYNC, ((u8 *) (pkt->data)), rdlen,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004132 pkt, NULL, NULL);
4133 bus->f2rxdata++;
Roland Vossenb7ef2a92011-05-03 11:35:02 +02004134 ASSERT(sdret != -BCME_PENDING);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004135
4136 if (sdret < 0) {
4137 DHD_ERROR(("%s: read %d %s bytes failed: %d\n",
4138 __func__, rdlen,
4139 ((chan ==
4140 SDPCM_EVENT_CHANNEL) ? "event" : ((chan ==
4141 SDPCM_DATA_CHANNEL)
4142 ? "data" : "test")),
4143 sdret));
4144 dhd_os_sdlock_rxq(bus->dhd);
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004145 bcm_pkt_buf_free_skb(pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004146 dhd_os_sdunlock_rxq(bus->dhd);
4147 bus->dhd->rx_errors++;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004148 dhdsdio_rxfail(bus, true, RETRYCHAN(chan));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004149 continue;
4150 }
4151
4152 /* Copy the already-read portion */
Arend van Sprielc303ecb2010-11-18 20:46:43 +01004153 skb_push(pkt, firstread);
Stanislav Fomichev02160692011-02-15 01:05:10 +03004154 memcpy(pkt->data, bus->rxhdr, firstread);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004155
4156#ifdef DHD_DEBUG
Arend van Spriel34227312011-05-10 22:25:32 +02004157 if (DHD_BYTES_ON() && DHD_DATA_ON()) {
4158 printk(KERN_DEBUG "Rx Data:\n");
4159 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
4160 pkt->data, len);
4161 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004162#endif
4163
4164deliver:
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",
4169 __func__, len));
4170#ifdef DHD_DEBUG
4171 if (DHD_GLOM_ON()) {
Arend van Spriel34227312011-05-10 22:25:32 +02004172 printk(KERN_DEBUG "Glom Data:\n");
4173 print_hex_dump_bytes("",
4174 DUMP_PREFIX_OFFSET,
4175 pkt->data, len);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004176 }
4177#endif
Arend van Spriel2cb8ada2010-11-18 20:46:44 +01004178 __skb_trim(pkt, len);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004179 ASSERT(doff == SDPCM_HDRLEN);
Arend van Sprielc303ecb2010-11-18 20:46:43 +01004180 skb_pull(pkt, SDPCM_HDRLEN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004181 bus->glomd = pkt;
4182 } else {
4183 DHD_ERROR(("%s: glom superframe w/o "
4184 "descriptor!\n", __func__));
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004185 dhdsdio_rxfail(bus, false, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004186 }
4187 continue;
4188 }
4189
4190 /* Fill in packet len and prio, deliver upward */
Arend van Spriel2cb8ada2010-11-18 20:46:44 +01004191 __skb_trim(pkt, len);
Arend van Sprielc303ecb2010-11-18 20:46:43 +01004192 skb_pull(pkt, doff);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004193
4194#ifdef SDTEST
4195 /* Test channel packets are processed separately */
4196 if (chan == SDPCM_TEST_CHANNEL) {
4197 dhdsdio_testrcv(bus, pkt, seq);
4198 continue;
4199 }
4200#endif /* SDTEST */
4201
Arend van Spriel54991ad2010-11-23 14:06:24 +01004202 if (pkt->len == 0) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004203 dhd_os_sdlock_rxq(bus->dhd);
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004204 bcm_pkt_buf_free_skb(pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004205 dhd_os_sdunlock_rxq(bus->dhd);
4206 continue;
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);
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004210 bcm_pkt_buf_free_skb(pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004211 dhd_os_sdunlock_rxq(bus->dhd);
4212 bus->dhd->rx_errors++;
4213 continue;
4214 }
4215
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);
4220 }
4221 rxcount = maxframes - rxleft;
4222#ifdef DHD_DEBUG
4223 /* Message if we hit the limit */
4224 if (!rxleft && !sdtest)
4225 DHD_DATA(("%s: hit rx limit of %d frames\n", __func__,
4226 maxframes));
4227 else
4228#endif /* DHD_DEBUG */
4229 DHD_DATA(("%s: processed %d frames\n", __func__, rxcount));
4230 /* Back off rxseq if awaiting rtx, update rx_seq */
4231 if (bus->rxskip)
4232 rxseq--;
4233 bus->rx_seq = rxseq;
4234
4235 return rxcount;
4236}
4237
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07004238static u32 dhdsdio_hostmail(dhd_bus_t *bus)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004239{
4240 sdpcmd_regs_t *regs = bus->regs;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07004241 u32 intstatus = 0;
4242 u32 hmb_data;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004243 u8 fcbits;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004244 uint retries = 0;
4245
4246 DHD_TRACE(("%s: Enter\n", __func__));
4247
4248 /* Read mailbox data and ack that we did so */
4249 R_SDREG(hmb_data, &regs->tohostmailboxdata, retries);
4250 if (retries <= retry_limit)
4251 W_SDREG(SMB_INT_ACK, &regs->tosbmailbox, retries);
4252 bus->f1regdata += 2;
4253
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",
4257 bus->rx_seq));
4258 if (!bus->rxskip)
4259 DHD_ERROR(("%s: unexpected NAKHANDLED!\n", __func__));
4260
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004261 bus->rxskip = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004262 intstatus |= I_HMB_FRAME_IND;
4263 }
4264
4265 /*
4266 * DEVREADY does not occur with gSPI.
4267 */
4268 if (hmb_data & (HMB_DATA_DEVREADY | HMB_DATA_FWREADY)) {
4269 bus->sdpcm_ver =
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, "
4274 "expecting %d\n",
4275 bus->sdpcm_ver, SDPCM_PROT_VERSION));
4276 else
4277 DHD_INFO(("Dongle ready, protocol version %d\n",
4278 bus->sdpcm_ver));
4279 }
4280
4281 /*
4282 * Flow Control has been moved into the RX headers and this out of band
Grant Grundler4b455e02011-05-04 09:59:47 -07004283 * method isn't used any more.
4284 * remaining backward compatible with older dongles.
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004285 */
4286 if (hmb_data & HMB_DATA_FC) {
Grant Grundler4b455e02011-05-04 09:59:47 -07004287 fcbits = (hmb_data & HMB_DATA_FCDATA_MASK) >>
4288 HMB_DATA_FCDATA_SHIFT;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004289
4290 if (fcbits & ~bus->flowcontrol)
4291 bus->fc_xoff++;
Grant Grundler4b455e02011-05-04 09:59:47 -07004292
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004293 if (bus->flowcontrol & ~fcbits)
4294 bus->fc_xon++;
4295
4296 bus->fc_rcvd++;
4297 bus->flowcontrol = fcbits;
4298 }
4299
4300 /* Shouldn't be any others */
4301 if (hmb_data & ~(HMB_DATA_DEVREADY |
4302 HMB_DATA_NAKHANDLED |
4303 HMB_DATA_FC |
4304 HMB_DATA_FWREADY |
4305 HMB_DATA_FCDATA_MASK | HMB_DATA_VERSION_MASK)) {
4306 DHD_ERROR(("Unknown mailbox data content: 0x%02x\n", hmb_data));
4307 }
4308
4309 return intstatus;
4310}
4311
4312bool dhdsdio_dpc(dhd_bus_t *bus)
4313{
4314 bcmsdh_info_t *sdh = bus->sdh;
4315 sdpcmd_regs_t *regs = bus->regs;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07004316 u32 intstatus, newstatus = 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004317 uint retries = 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 */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004321 bool rxdone = true; /* Flag for no more read data */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004322 bool resched = false; /* Flag indicating resched wanted */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004323
4324 DHD_TRACE(("%s: Enter\n", __func__));
4325
4326 /* Start with leftover status bits */
4327 intstatus = bus->intstatus;
4328
4329 dhd_os_sdlock(bus->dhd);
4330
4331 /* If waiting for HTAVAIL, check status */
4332 if (bus->clkstate == CLK_PENDING) {
4333 int err;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004334 u8 clkctl, devctl = 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004335
4336#ifdef DHD_DEBUG
4337 /* Check for inconsistent device control */
4338 devctl =
4339 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL, &err);
4340 if (err) {
4341 DHD_ERROR(("%s: error reading DEVCTL: %d\n",
4342 __func__, err));
4343 bus->dhd->busstate = DHD_BUS_DOWN;
4344 } else {
4345 ASSERT(devctl & SBSDIO_DEVCTL_CA_INT_ONLY);
4346 }
4347#endif /* DHD_DEBUG */
4348
4349 /* Read CSR, if clock on switch to AVAIL, else ignore */
4350 clkctl =
4351 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
4352 &err);
4353 if (err) {
4354 DHD_ERROR(("%s: error reading CSR: %d\n", __func__,
4355 err));
4356 bus->dhd->busstate = DHD_BUS_DOWN;
4357 }
4358
4359 DHD_INFO(("DPC: PENDING, devctl 0x%02x clkctl 0x%02x\n", devctl,
4360 clkctl));
4361
4362 if (SBSDIO_HTAV(clkctl)) {
4363 devctl =
4364 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
4365 &err);
4366 if (err) {
4367 DHD_ERROR(("%s: error reading DEVCTL: %d\n",
4368 __func__, err));
4369 bus->dhd->busstate = DHD_BUS_DOWN;
4370 }
4371 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
4372 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
4373 devctl, &err);
4374 if (err) {
4375 DHD_ERROR(("%s: error writing DEVCTL: %d\n",
4376 __func__, err));
4377 bus->dhd->busstate = DHD_BUS_DOWN;
4378 }
4379 bus->clkstate = CLK_AVAIL;
4380 } else {
4381 goto clkwait;
4382 }
4383 }
4384
4385 BUS_WAKE(bus);
4386
4387 /* Make sure backplane clock is on */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004388 dhdsdio_clkctl(bus, CLK_AVAIL, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004389 if (bus->clkstate == CLK_PENDING)
4390 goto clkwait;
4391
4392 /* Pending interrupt indicates new device status */
4393 if (bus->ipend) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004394 bus->ipend = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004395 R_SDREG(newstatus, &regs->intstatus, retries);
4396 bus->f1regdata++;
4397 if (bcmsdh_regfail(bus->sdh))
4398 newstatus = 0;
4399 newstatus &= bus->hostintmask;
4400 bus->fcstate = !!(newstatus & I_HMB_FC_STATE);
4401 if (newstatus) {
4402 W_SDREG(newstatus, &regs->intstatus, retries);
4403 bus->f1regdata++;
4404 }
4405 }
4406
4407 /* Merge new bits with previous */
4408 intstatus |= newstatus;
4409 bus->intstatus = 0;
4410
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.
4414 */
4415 if (intstatus & I_HMB_FC_CHANGE) {
4416 intstatus &= ~I_HMB_FC_CHANGE;
4417 W_SDREG(I_HMB_FC_CHANGE, &regs->intstatus, retries);
4418 R_SDREG(newstatus, &regs->intstatus, retries);
4419 bus->f1regdata += 2;
4420 bus->fcstate =
4421 !!(newstatus & (I_HMB_FC_STATE | I_HMB_FC_CHANGE));
4422 intstatus |= (newstatus & bus->hostintmask);
4423 }
4424
4425 /* Handle host mailbox indication */
4426 if (intstatus & I_HMB_HOST_INT) {
4427 intstatus &= ~I_HMB_HOST_INT;
4428 intstatus |= dhdsdio_hostmail(bus);
4429 }
4430
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;
4435 }
4436
4437 if (intstatus & I_RD_OOSYNC) {
4438 DHD_ERROR(("Dongle reports RD_OOSYNC\n"));
4439 intstatus &= ~I_RD_OOSYNC;
4440 }
4441
4442 if (intstatus & I_SBINT) {
4443 DHD_ERROR(("Dongle reports SBINT\n"));
4444 intstatus &= ~I_SBINT;
4445 }
4446
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;
4451 }
4452
4453 /* Ignore frame indications if rxskip is set */
4454 if (bus->rxskip)
4455 intstatus &= ~I_HMB_FRAME_IND;
4456
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;
Greg Kroah-Hartman7068c2f2010-10-08 11:34:59 -07004462 rxlimit -= min(framecnt, rxlimit);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004463 }
4464
4465 /* Keep still-pending events for next scheduling */
4466 bus->intstatus = intstatus;
4467
4468clkwait:
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...)
4475 */
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));
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004479 bus->intdis = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004480 bcmsdh_intr_enable(sdh);
4481 }
4482
4483 if (DATAOK(bus) && bus->ctrl_frame_stat &&
4484 (bus->clkstate == CLK_AVAIL)) {
4485 int ret, i;
4486
4487 ret =
4488 dhd_bcmsdh_send_buf(bus, bcmsdh_cur_sbwad(sdh), SDIO_FUNC_2,
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004489 F2SYNC, (u8 *) bus->ctrl_frame_buf,
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07004490 (u32) bus->ctrl_frame_len, NULL,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004491 NULL, NULL);
Roland Vossenb7ef2a92011-05-03 11:35:02 +02004492 ASSERT(ret != -BCME_PENDING);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004493
4494 if (ret < 0) {
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));
4499 bus->tx_sderrs++;
4500
4501 bcmsdh_abort(sdh, SDIO_FUNC_2);
4502
4503 bcmsdh_cfg_write(sdh, SDIO_FUNC_1,
4504 SBSDIO_FUNC1_FRAMECTRL, SFC_WF_TERM,
4505 NULL);
4506 bus->f1regdata++;
4507
4508 for (i = 0; i < 3; i++) {
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004509 u8 hi, lo;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004510 hi = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
4511 SBSDIO_FUNC1_WFRAMEBCHI,
4512 NULL);
4513 lo = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
4514 SBSDIO_FUNC1_WFRAMEBCLO,
4515 NULL);
4516 bus->f1regdata += 2;
4517 if ((hi == 0) && (lo == 0))
4518 break;
4519 }
4520
4521 }
4522 if (ret == 0)
4523 bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQUENCE_WRAP;
4524
Arend van Spriel0bef7742011-02-10 12:03:44 +01004525 DHD_INFO(("Return_dpc value is : %d\n", ret));
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004526 bus->ctrl_frame_stat = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004527 dhd_wait_event_wakeup(bus->dhd);
4528 }
4529 /* Send queued frames (limit 1 if rx may still be pending) */
4530 else if ((bus->clkstate == CLK_AVAIL) && !bus->fcstate &&
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004531 bcm_pktq_mlen(&bus->txq, ~bus->flowcontrol) && txlimit
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004532 && DATAOK(bus)) {
Greg Kroah-Hartman7068c2f2010-10-08 11:34:59 -07004533 framecnt = rxdone ? txlimit : min(txlimit, dhd_txminmax);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004534 framecnt = dhdsdio_sendfromq(bus, framecnt);
4535 txlimit -= framecnt;
4536 }
4537
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;
4546 bus->intstatus = 0;
4547 } else if (bus->clkstate == CLK_PENDING) {
4548 DHD_INFO(("%s: rescheduled due to CLK_PENDING awaiting "
4549 "I_CHIPACTIVE interrupt\n", __func__));
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004550 resched = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004551 } else if (bus->intstatus || bus->ipend ||
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004552 (!bus->fcstate && bcm_pktq_mlen(&bus->txq, ~bus->flowcontrol) &&
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004553 DATAOK(bus)) || PKT_AVAILABLE()) {
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004554 resched = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004555 }
4556
4557 bus->dpc_sched = resched;
4558
4559 /* If we're done for now, turn off clock request. */
4560 if ((bus->clkstate != CLK_PENDING)
4561 && bus->idletime == DHD_IDLE_IMMEDIATE) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004562 bus->activity = false;
4563 dhdsdio_clkctl(bus, CLK_NONE, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004564 }
4565
4566 dhd_os_sdunlock(bus->dhd);
4567
4568 return resched;
4569}
4570
4571bool dhd_bus_dpc(struct dhd_bus *bus)
4572{
4573 bool resched;
4574
4575 /* Call the DPC directly. */
4576 DHD_TRACE(("Calling dhdsdio_dpc() from %s\n", __func__));
4577 resched = dhdsdio_dpc(bus);
4578
4579 return resched;
4580}
4581
4582void dhdsdio_isr(void *arg)
4583{
4584 dhd_bus_t *bus = (dhd_bus_t *) arg;
4585 bcmsdh_info_t *sdh;
4586
4587 DHD_TRACE(("%s: Enter\n", __func__));
4588
4589 if (!bus) {
4590 DHD_ERROR(("%s : bus is null pointer , exit\n", __func__));
4591 return;
4592 }
4593 sdh = bus->sdh;
4594
4595 if (bus->dhd->busstate == DHD_BUS_DOWN) {
4596 DHD_ERROR(("%s : bus is down. we have nothing to do\n",
4597 __func__));
4598 return;
4599 }
4600 /* Count the interrupt call */
4601 bus->intrcount++;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004602 bus->ipend = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004603
4604 /* Shouldn't get this interrupt if we're sleeping? */
4605 if (bus->sleeping) {
4606 DHD_ERROR(("INTERRUPT WHILE SLEEPING??\n"));
4607 return;
4608 }
4609
4610 /* Disable additional interrupts (is this needed now)? */
4611 if (bus->intr)
4612 DHD_INTR(("%s: disable SDIO interrupts\n", __func__));
4613 else
4614 DHD_ERROR(("dhdsdio_isr() w/o interrupt configured!\n"));
4615
4616 bcmsdh_intr_disable(sdh);
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004617 bus->intdis = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004618
4619#if defined(SDIO_ISR_THREAD)
4620 DHD_TRACE(("Calling dhdsdio_dpc() from %s\n", __func__));
4621 while (dhdsdio_dpc(bus))
4622 ;
4623#else
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004624 bus->dpc_sched = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004625 dhd_sched_dpc(bus->dhd);
4626#endif
4627
4628}
4629
4630#ifdef SDTEST
4631static void dhdsdio_pktgen_init(dhd_bus_t *bus)
4632{
4633 /* Default to specified length, or full range */
4634 if (dhd_pktgen_len) {
Greg Kroah-Hartman7068c2f2010-10-08 11:34:59 -07004635 bus->pktgen_maxlen = min(dhd_pktgen_len, MAX_PKTGEN_LEN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004636 bus->pktgen_minlen = bus->pktgen_maxlen;
4637 } else {
4638 bus->pktgen_maxlen = MAX_PKTGEN_LEN;
4639 bus->pktgen_minlen = 0;
4640 }
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07004641 bus->pktgen_len = (u16) bus->pktgen_minlen;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004642
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;
4647
4648 /* Default to echo mode */
4649 bus->pktgen_mode = DHD_PKTGEN_ECHO;
4650 bus->pktgen_stop = 1;
4651}
4652
4653static void dhdsdio_pktgen(dhd_bus_t *bus)
4654{
Arend van Sprielc26b1372010-11-23 14:06:23 +01004655 struct sk_buff *pkt;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004656 u8 *data;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004657 uint pktcount;
4658 uint fillbyte;
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07004659 u16 len;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004660
4661 /* Display current count if appropriate */
4662 if (bus->pktgen_print && (++bus->pktgen_ptick >= bus->pktgen_print)) {
4663 bus->pktgen_ptick = 0;
Arend van Spriel0bef7742011-02-10 12:03:44 +01004664 printk(KERN_DEBUG "%s: send attempts %d rcvd %d\n",
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004665 __func__, bus->pktgen_sent, bus->pktgen_rcvd);
4666 }
4667
4668 /* For recv mode, just make sure dongle has started sending */
4669 if (bus->pktgen_mode == DHD_PKTGEN_RECV) {
4670 if (!bus->pktgen_rcvd)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004671 dhdsdio_sdtest_set(bus, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004672 return;
4673 }
4674
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;
4681 break;
4682 }
4683
4684 /* Allocate an appropriate-sized packet */
4685 len = bus->pktgen_len;
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004686 pkt = bcm_pkt_buf_get_skb(
Jason Cooper9b890322010-09-30 15:15:39 -04004687 (len + SDPCM_HDRLEN + SDPCM_TEST_HDRLEN + DHD_SDALIGN),
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004688 true);
Jason Cooper9b890322010-09-30 15:15:39 -04004689 if (!pkt) {
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004690 DHD_ERROR(("%s: bcm_pkt_buf_get_skb failed!\n",
4691 __func__));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004692 break;
4693 }
Arend van Spriel3c9d4c32011-03-02 21:18:48 +01004694 PKTALIGN(pkt, (len + SDPCM_HDRLEN + SDPCM_TEST_HDRLEN),
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004695 DHD_SDALIGN);
Arend van Spriel54991ad2010-11-23 14:06:24 +01004696 data = (u8 *) (pkt->data) + SDPCM_HDRLEN;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004697
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;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004702 *data++ = (u8) bus->pktgen_sent;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004703 break;
4704
4705 case DHD_PKTGEN_SEND:
4706 *data++ = SDPCM_TEST_DISCARD;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004707 *data++ = (u8) bus->pktgen_sent;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004708 break;
4709
4710 case DHD_PKTGEN_RXBURST:
4711 *data++ = SDPCM_TEST_BURST;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004712 *data++ = (u8) bus->pktgen_count;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004713 break;
4714
4715 default:
4716 DHD_ERROR(("Unrecognized pktgen mode %d\n",
4717 bus->pktgen_mode));
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004718 bcm_pkt_buf_free_skb(pkt, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004719 bus->pktgen_count = 0;
4720 return;
4721 }
4722
4723 /* Write test header length field */
4724 *data++ = (len >> 0);
4725 *data++ = (len >> 8);
4726
4727 /* Then fill in the remainder -- N/A for burst,
4728 but who cares... */
4729 for (fillbyte = 0; fillbyte < len; fillbyte++)
4730 *data++ =
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004731 SDPCM_TEST_FILL(fillbyte, (u8) bus->pktgen_sent);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004732
4733#ifdef DHD_DEBUG
4734 if (DHD_BYTES_ON() && DHD_DATA_ON()) {
Arend van Spriel54991ad2010-11-23 14:06:24 +01004735 data = (u8 *) (pkt->data) + SDPCM_HDRLEN;
Arend van Spriel34227312011-05-10 22:25:32 +02004736 printk(KERN_DEBUG "dhdsdio_pktgen: Tx Data:\n");
4737 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, data,
4738 pkt->len - SDPCM_HDRLEN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004739 }
4740#endif
4741
4742 /* Send it */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004743 if (dhdsdio_txpkt(bus, pkt, SDPCM_TEST_CHANNEL, true)) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004744 bus->pktgen_fail++;
4745 if (bus->pktgen_stop
4746 && bus->pktgen_stop == bus->pktgen_fail)
4747 bus->pktgen_count = 0;
4748 }
4749 bus->pktgen_sent++;
4750
4751 /* Bump length if not fixed, wrap at max */
4752 if (++bus->pktgen_len > bus->pktgen_maxlen)
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07004753 bus->pktgen_len = (u16) bus->pktgen_minlen;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004754
4755 /* Special case for burst mode: just send one request! */
4756 if (bus->pktgen_mode == DHD_PKTGEN_RXBURST)
4757 break;
4758 }
4759}
4760
4761static void dhdsdio_sdtest_set(dhd_bus_t *bus, bool start)
4762{
Arend van Sprielc26b1372010-11-23 14:06:23 +01004763 struct sk_buff *pkt;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004764 u8 *data;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004765
4766 /* Allocate the packet */
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004767 pkt = bcm_pkt_buf_get_skb(SDPCM_HDRLEN + SDPCM_TEST_HDRLEN +
4768 DHD_SDALIGN, true);
Jason Cooper9b890322010-09-30 15:15:39 -04004769 if (!pkt) {
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004770 DHD_ERROR(("%s: bcm_pkt_buf_get_skb failed!\n", __func__));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004771 return;
4772 }
Arend van Spriel3c9d4c32011-03-02 21:18:48 +01004773 PKTALIGN(pkt, (SDPCM_HDRLEN + SDPCM_TEST_HDRLEN), DHD_SDALIGN);
Arend van Spriel54991ad2010-11-23 14:06:24 +01004774 data = (u8 *) (pkt->data) + SDPCM_HDRLEN;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004775
4776 /* Fill in the test header */
4777 *data++ = SDPCM_TEST_SEND;
4778 *data++ = start;
4779 *data++ = (bus->pktgen_maxlen >> 0);
4780 *data++ = (bus->pktgen_maxlen >> 8);
4781
4782 /* Send it */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004783 if (dhdsdio_txpkt(bus, pkt, SDPCM_TEST_CHANNEL, true))
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004784 bus->pktgen_fail++;
4785}
4786
Arend van Sprielc26b1372010-11-23 14:06:23 +01004787static void dhdsdio_testrcv(dhd_bus_t *bus, struct sk_buff *pkt, uint seq)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004788{
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004789 u8 *data;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004790 uint pktlen;
4791
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004792 u8 cmd;
4793 u8 extra;
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07004794 u16 len;
4795 u16 offset;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004796
4797 /* Check for min length */
Arend van Spriel54991ad2010-11-23 14:06:24 +01004798 pktlen = pkt->len;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004799 if (pktlen < SDPCM_TEST_HDRLEN) {
4800 DHD_ERROR(("dhdsdio_restrcv: toss runt frame, pktlen %d\n",
4801 pktlen));
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004802 bcm_pkt_buf_free_skb(pkt, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004803 return;
4804 }
4805
4806 /* Extract header fields */
Arend van Spriel54991ad2010-11-23 14:06:24 +01004807 data = pkt->data;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004808 cmd = *data++;
4809 extra = *data++;
4810 len = *data++;
4811 len += *data++ << 8;
4812
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));
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004820 bcm_pkt_buf_free_skb(pkt, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004821 return;
4822 }
4823 }
4824
4825 /* Process as per command */
4826 switch (cmd) {
4827 case SDPCM_TEST_ECHOREQ:
4828 /* Rx->Tx turnaround ok (even on NDIS w/current
4829 implementation) */
Arend van Spriel54991ad2010-11-23 14:06:24 +01004830 *(u8 *) (pkt->data) = SDPCM_TEST_ECHORSP;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004831 if (dhdsdio_txpkt(bus, pkt, SDPCM_TEST_CHANNEL, true) == 0) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004832 bus->pktgen_sent++;
4833 } else {
4834 bus->pktgen_fail++;
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004835 bcm_pkt_buf_free_skb(pkt, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004836 }
4837 bus->pktgen_rcvd++;
4838 break;
4839
4840 case SDPCM_TEST_ECHORSP:
4841 if (bus->ext_loop) {
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004842 bcm_pkt_buf_free_skb(pkt, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004843 bus->pktgen_rcvd++;
4844 break;
4845 }
4846
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",
4850 offset, len,
4851 SDPCM_TEST_FILL(offset, extra), *data));
4852 break;
4853 }
4854 }
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004855 bcm_pkt_buf_free_skb(pkt, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004856 bus->pktgen_rcvd++;
4857 break;
4858
4859 case SDPCM_TEST_DISCARD:
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004860 bcm_pkt_buf_free_skb(pkt, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004861 bus->pktgen_rcvd++;
4862 break;
4863
4864 case SDPCM_TEST_BURST:
4865 case SDPCM_TEST_SEND:
4866 default:
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));
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004870 bcm_pkt_buf_free_skb(pkt, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004871 break;
4872 }
4873
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;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004879 dhdsdio_sdtest_set(bus, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004880 }
4881 }
4882}
4883#endif /* SDTEST */
4884
4885extern bool dhd_bus_watchdog(dhd_pub_t *dhdp)
4886{
4887 dhd_bus_t *bus;
4888
4889 DHD_TIMER(("%s: Enter\n", __func__));
4890
4891 bus = dhdp->bus;
4892
4893 if (bus->dhd->dongle_reset)
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004894 return false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004895
4896 /* Ignore the timer if simulating bus down */
4897 if (bus->sleeping)
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004898 return false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004899
4900 dhd_os_sdlock(bus->dhd);
4901
4902 /* Poll period: check device if appropriate. */
4903 if (bus->poll && (++bus->polltick >= bus->pollrate)) {
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07004904 u32 intstatus = 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004905
4906 /* Reset poll tick */
4907 bus->polltick = 0;
4908
4909 /* Check device if no interrupts */
4910 if (!bus->intr || (bus->intrcount == bus->lastintrs)) {
4911
4912 if (!bus->dpc_sched) {
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004913 u8 devpend;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004914 devpend = bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_0,
4915 SDIOD_CCCR_INTPEND,
4916 NULL);
4917 intstatus =
4918 devpend & (INTR_STATUS_FUNC1 |
4919 INTR_STATUS_FUNC2);
4920 }
4921
4922 /* If there is something, make like the ISR and
4923 schedule the DPC */
4924 if (intstatus) {
4925 bus->pollcnt++;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004926 bus->ipend = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004927 if (bus->intr)
4928 bcmsdh_intr_disable(bus->sdh);
4929
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004930 bus->dpc_sched = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004931 dhd_sched_dpc(bus->dhd);
4932
4933 }
4934 }
4935
4936 /* Update interrupt tracking */
4937 bus->lastintrs = bus->intrcount;
4938 }
4939#ifdef DHD_DEBUG
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 */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004946 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004947 if (dhdsdio_readconsole(bus) < 0)
4948 dhd_console_ms = 0; /* On error,
4949 stop trying */
4950 }
4951 }
4952#endif /* DHD_DEBUG */
4953
4954#ifdef SDTEST
4955 /* Generate packets if configured */
4956 if (bus->pktgen_count && (++bus->pktgen_tick >= bus->pktgen_freq)) {
4957 /* Make sure backplane clock is on */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004958 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004959 bus->pktgen_tick = 0;
4960 dhdsdio_pktgen(bus);
4961 }
4962#endif
4963
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) {
4967 bus->idlecount = 0;
4968 if (bus->activity) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004969 bus->activity = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004970 dhd_os_wd_timer(bus->dhd, dhd_watchdog_ms);
4971 } else {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004972 dhdsdio_clkctl(bus, CLK_NONE, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004973 }
4974 }
4975 }
4976
4977 dhd_os_sdunlock(bus->dhd);
4978
4979 return bus->ipend;
4980}
4981
4982#ifdef DHD_DEBUG
Greg Kroah-Hartman580a0bd2010-10-05 11:09:48 -07004983extern int dhd_bus_console_in(dhd_pub_t *dhdp, unsigned char *msg, uint msglen)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004984{
4985 dhd_bus_t *bus = dhdp->bus;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07004986 u32 addr, val;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004987 int rv;
Arend van Sprielc26b1372010-11-23 14:06:23 +01004988 struct sk_buff *pkt;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004989
4990 /* Address could be zero if CONSOLE := 0 in dongle Makefile */
4991 if (bus->console_addr == 0)
Roland Vossene10d82d2011-05-03 11:35:19 +02004992 return -ENOTSUPP;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004993
4994 /* Exclusive bus access */
4995 dhd_os_sdlock(bus->dhd);
4996
4997 /* Don't allow input if dongle is in reset */
4998 if (bus->dhd->dongle_reset) {
4999 dhd_os_sdunlock(bus->dhd);
Roland Vossenb74ac122011-05-03 11:35:20 +02005000 return -EPERM;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005001 }
5002
5003 /* Request clock to allow SDIO accesses */
5004 BUS_WAKE(bus);
5005 /* No pend allowed since txpkt is called later, ht clk has to be on */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005006 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005007
5008 /* Zero cbuf_index */
Roland Vossen70963f92011-06-01 13:45:08 +02005009 addr = bus->console_addr + offsetof(rte_cons_t, cbuf_idx);
Stanislav Fomichev628f10b2011-02-20 21:43:32 +03005010 val = cpu_to_le32(0);
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005011 rv = dhdsdio_membytes(bus, true, addr, (u8 *)&val, sizeof(val));
Jason Cooper9b890322010-09-30 15:15:39 -04005012 if (rv < 0)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005013 goto done;
5014
5015 /* Write message into cbuf */
Roland Vossen70963f92011-06-01 13:45:08 +02005016 addr = bus->console_addr + offsetof(rte_cons_t, cbuf);
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005017 rv = dhdsdio_membytes(bus, true, addr, (u8 *)msg, msglen);
Jason Cooper9b890322010-09-30 15:15:39 -04005018 if (rv < 0)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005019 goto done;
5020
5021 /* Write length into vcons_in */
Roland Vossen70963f92011-06-01 13:45:08 +02005022 addr = bus->console_addr + offsetof(rte_cons_t, vcons_in);
Stanislav Fomichev628f10b2011-02-20 21:43:32 +03005023 val = cpu_to_le32(msglen);
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005024 rv = dhdsdio_membytes(bus, true, addr, (u8 *)&val, sizeof(val));
Jason Cooper9b890322010-09-30 15:15:39 -04005025 if (rv < 0)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005026 goto done;
5027
5028 /* Bump dongle by sending an empty event pkt.
5029 * sdpcm_sendup (RX) checks for virtual console input.
5030 */
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02005031 pkt = bcm_pkt_buf_get_skb(4 + SDPCM_RESERVE);
Jason Cooper9b890322010-09-30 15:15:39 -04005032 if ((pkt != NULL) && bus->clkstate == CLK_AVAIL)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005033 dhdsdio_txpkt(bus, pkt, SDPCM_EVENT_CHANNEL, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005034
5035done:
5036 if ((bus->idletime == DHD_IDLE_IMMEDIATE) && !bus->dpc_sched) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005037 bus->activity = false;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005038 dhdsdio_clkctl(bus, CLK_NONE, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005039 }
5040
5041 dhd_os_sdunlock(bus->dhd);
5042
5043 return rv;
5044}
5045#endif /* DHD_DEBUG */
5046
5047#ifdef DHD_DEBUG
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07005048static void dhd_dump_cis(uint fn, u8 *cis)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005049{
5050 uint byte, tag, tdata;
5051 DHD_INFO(("Function %d CIS:\n", fn));
5052
5053 for (tdata = byte = 0; byte < SBSDIO_CIS_SIZE_LIMIT; byte++) {
5054 if ((byte % 16) == 0)
5055 DHD_INFO((" "));
5056 DHD_INFO(("%02x ", cis[byte]));
5057 if ((byte % 16) == 15)
5058 DHD_INFO(("\n"));
5059 if (!tdata--) {
5060 tag = cis[byte];
5061 if (tag == 0xff)
5062 break;
5063 else if (!tag)
5064 tdata = 0;
5065 else if ((byte + 1) < SBSDIO_CIS_SIZE_LIMIT)
5066 tdata = cis[byte + 1] + 1;
5067 else
5068 DHD_INFO(("]"));
5069 }
5070 }
5071 if ((byte % 16) != 15)
5072 DHD_INFO(("\n"));
5073}
5074#endif /* DHD_DEBUG */
5075
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07005076static bool dhdsdio_chipmatch(u16 chipid)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005077{
5078 if (chipid == BCM4325_CHIP_ID)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005079 return true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005080 if (chipid == BCM4329_CHIP_ID)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005081 return true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005082 if (chipid == BCM4319_CHIP_ID)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005083 return true;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005084 return false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005085}
5086
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07005087static void *dhdsdio_probe(u16 venid, u16 devid, u16 bus_no,
5088 u16 slot, u16 func, uint bustype, void *regsva,
Arend van Spriel3c9d4c32011-03-02 21:18:48 +01005089 void *sdh)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005090{
5091 int ret;
5092 dhd_bus_t *bus;
5093
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.
5096 * Initialization
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
5100 * initializations.
5101 */
5102 dhd_txbound = DHD_TXBOUND;
5103 dhd_rxbound = DHD_RXBOUND;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005104 dhd_alignctl = true;
5105 sd1idle = true;
5106 dhd_readahead = true;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005107 retrydata = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005108 dhd_dongle_memsize = 0;
5109 dhd_txminmax = DHD_TXMINMAX;
5110
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005111 forcealign = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005112
5113 dhd_common_init();
5114
5115 DHD_TRACE(("%s: Enter\n", __func__));
5116 DHD_INFO(("%s: venid 0x%04x devid 0x%04x\n", __func__, venid, devid));
5117
5118 /* We make assumptions about address window mappings */
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07005119 ASSERT((unsigned long)regsva == SI_ENUM_BASE);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005120
5121 /* BCMSDH passes venid and devid based on CIS parsing -- but
5122 * low-power start
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.
5125 */
5126 /* Check the Vendor ID */
5127 switch (venid) {
5128 case 0x0000:
Stanislav Fomichevbe1c09f2011-03-28 01:31:36 +04005129 case PCI_VENDOR_ID_BROADCOM:
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005130 break;
5131 default:
5132 DHD_ERROR(("%s: unknown vendor: 0x%04x\n", __func__, venid));
5133 return NULL;
5134 }
5135
5136 /* Check the Device ID and make sure it's one that we support */
5137 switch (devid) {
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__));
5142 break;
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 */
5146 case 0x4329:
5147 DHD_INFO(("%s: found 4329 Dongle\n", __func__));
5148 break;
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__));
5153 break;
5154 case 0:
5155 DHD_INFO(("%s: allow device id 0, will check chip internals\n",
5156 __func__));
5157 break;
5158
5159 default:
5160 DHD_ERROR(("%s: skipping 0x%04x/0x%04x, not a dongle\n",
5161 __func__, venid, devid));
5162 return NULL;
5163 }
5164
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005165 /* Allocate private bus interface state */
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02005166 bus = kzalloc(sizeof(dhd_bus_t), GFP_ATOMIC);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005167 if (!bus) {
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02005168 DHD_ERROR(("%s: kmalloc of dhd_bus_t failed\n", __func__));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005169 goto fail;
5170 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005171 bus->sdh = sdh;
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07005172 bus->cl_devid = (u16) devid;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005173 bus->bus = DHD_BUS;
5174 bus->tx_seq = SDPCM_SEQUENCE_WRAP - 1;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005175 bus->usebufpool = false; /* Use bufpool if allocated,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005176 else use locally malloced rxbuf */
5177
5178 /* attempt to attach to the dongle */
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005179 if (!(dhdsdio_probe_attach(bus, sdh, regsva, devid))) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005180 DHD_ERROR(("%s: dhdsdio_probe_attach failed\n", __func__));
5181 goto fail;
5182 }
5183
5184 /* Attach to the dhd/OS/network interface */
Arend van Spriel3c9d4c32011-03-02 21:18:48 +01005185 bus->dhd = dhd_attach(bus, SDPCM_RESERVE);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005186 if (!bus->dhd) {
5187 DHD_ERROR(("%s: dhd_attach failed\n", __func__));
5188 goto fail;
5189 }
5190
5191 /* Allocate buffers */
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005192 if (!(dhdsdio_probe_malloc(bus, sdh))) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005193 DHD_ERROR(("%s: dhdsdio_probe_malloc failed\n", __func__));
5194 goto fail;
5195 }
5196
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005197 if (!(dhdsdio_probe_init(bus, sdh))) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005198 DHD_ERROR(("%s: dhdsdio_probe_init failed\n", __func__));
5199 goto fail;
5200 }
5201
5202 /* Register interrupt callback, but mask it (not operational yet). */
5203 DHD_INTR(("%s: disable SDIO interrupts (not interested yet)\n",
5204 __func__));
5205 bcmsdh_intr_disable(sdh);
5206 ret = bcmsdh_intr_reg(sdh, dhdsdio_isr, bus);
5207 if (ret != 0) {
5208 DHD_ERROR(("%s: FAILED: bcmsdh_intr_reg returned %d\n",
5209 __func__, ret));
5210 goto fail;
5211 }
5212 DHD_INTR(("%s: registered SDIO interrupt function ok\n", __func__));
5213
5214 DHD_INFO(("%s: completed!!\n", __func__));
5215
5216 /* if firmware path present try to download and bring up bus */
Jason Cooper9b890322010-09-30 15:15:39 -04005217 ret = dhd_bus_start(bus->dhd);
5218 if (ret != 0) {
Roland Vossene10d82d2011-05-03 11:35:19 +02005219 if (ret == -ENOLINK) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005220 DHD_ERROR(("%s: dongle is not responding\n", __func__));
5221 goto fail;
5222 }
5223 }
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__));
5227 goto fail;
5228 }
5229
5230 return bus;
5231
5232fail:
Arend van Spriel3c9d4c32011-03-02 21:18:48 +01005233 dhdsdio_release(bus);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005234 return NULL;
5235}
5236
5237static bool
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005238dhdsdio_probe_attach(struct dhd_bus *bus, void *sdh, void *regsva, u16 devid)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005239{
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07005240 u8 clkctl = 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005241 int err = 0;
5242
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005243 bus->alp_only = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005244
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__));
5248
5249#ifdef DHD_DEBUG
Arend van Spriel0bef7742011-02-10 12:03:44 +01005250 printk(KERN_DEBUG "F1 signature read @0x18000000=0x%4x\n",
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005251 bcmsdh_reg_read(bus->sdh, SI_ENUM_BASE, 4));
5252
5253#endif /* DHD_DEBUG */
5254
Franky Linc05df632011-04-25 19:34:07 -07005255 /*
5256 * Force PLL off until dhdsdio_chip_attach()
5257 * programs PLL control regs
5258 */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005259
5260 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
5261 DHD_INIT_CLKCTL1, &err);
5262 if (!err)
5263 clkctl =
5264 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
5265 &err);
5266
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));
5271 goto fail;
5272 }
5273#ifdef DHD_DEBUG
5274 if (DHD_INFO_ON()) {
5275 uint fn, numfn;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07005276 u8 *cis[SDIOD_MAX_IOFUNCS];
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005277 int err = 0;
5278
5279 numfn = bcmsdh_query_iofnum(sdh);
5280 ASSERT(numfn <= SDIOD_MAX_IOFUNCS);
5281
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,
5285 NULL)),
5286 !SBSDIO_ALPAV(clkctl)), PMU_MAX_TRANSITION_DLY);
5287
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);
mike.rapoport@gmail.com73831412010-10-13 00:09:07 +02005291 udelay(65);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005292
5293 for (fn = 0; fn <= numfn; fn++) {
Alexander Beregalov12d0eb42011-03-09 03:53:35 +03005294 cis[fn] = kzalloc(SBSDIO_CIS_SIZE_LIMIT, GFP_ATOMIC);
Jason Cooper9b890322010-09-30 15:15:39 -04005295 if (!cis[fn]) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005296 DHD_INFO(("dhdsdio_probe: fn %d cis malloc "
5297 "failed\n", fn));
5298 break;
5299 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005300
Jason Cooper9b890322010-09-30 15:15:39 -04005301 err = bcmsdh_cis_read(sdh, fn, cis[fn],
5302 SBSDIO_CIS_SIZE_LIMIT);
5303 if (err) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005304 DHD_INFO(("dhdsdio_probe: fn %d cis read "
5305 "err %d\n", fn, err));
mike.rapoport@gmail.com182acb32010-10-13 00:09:12 +02005306 kfree(cis[fn]);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005307 break;
5308 }
5309 dhd_dump_cis(fn, cis[fn]);
5310 }
5311
5312 while (fn-- > 0) {
5313 ASSERT(cis[fn]);
mike.rapoport@gmail.com182acb32010-10-13 00:09:12 +02005314 kfree(cis[fn]);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005315 }
5316
5317 if (err) {
5318 DHD_ERROR(("dhdsdio_probe: error read/parsing CIS\n"));
5319 goto fail;
5320 }
5321 }
5322#endif /* DHD_DEBUG */
5323
Franky Lincb63e4c2011-04-25 15:45:08 -07005324 if (dhdsdio_chip_attach(bus, regsva)) {
5325 DHD_ERROR(("%s: dhdsdio_chip_attach failed!\n", __func__));
5326 goto fail;
5327 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005328
Franky Linc05df632011-04-25 19:34:07 -07005329 bcmsdh_chipinfo(sdh, bus->ci->chip, bus->ci->chiprev);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005330
Franky Linc05df632011-04-25 19:34:07 -07005331 if (!dhdsdio_chipmatch((u16) bus->ci->chip)) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005332 DHD_ERROR(("%s: unsupported chip: 0x%04x\n",
Franky Linc05df632011-04-25 19:34:07 -07005333 __func__, bus->ci->chip));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005334 goto fail;
5335 }
5336
Franky Lin5d0d7a92011-04-25 19:34:05 -07005337 dhdsdio_sdiod_drive_strength_init(bus, dhd_sdiod_drive_strength);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005338
5339 /* Get info on the ARM and SOCRAM cores... */
5340 if (!DHD_NOPMU(bus)) {
Franky Linc05df632011-04-25 19:34:07 -07005341 bus->armrev = SBCOREREV(bcmsdh_reg_read(bus->sdh,
5342 CORE_SB(bus->ci->armcorebase, sbidhigh), 4));
5343 bus->orig_ramsize = bus->ci->ramsize;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005344 if (!(bus->orig_ramsize)) {
5345 DHD_ERROR(("%s: failed to find SOCRAM memory!\n",
5346 __func__));
5347 goto fail;
5348 }
5349 bus->ramsize = bus->orig_ramsize;
5350 if (dhd_dongle_memsize)
5351 dhd_dongle_setmemsize(bus, dhd_dongle_memsize);
5352
5353 DHD_ERROR(("DHD: dongle ram size is set to %d(orig %d)\n",
5354 bus->ramsize, bus->orig_ramsize));
5355 }
5356
Franky Linc05df632011-04-25 19:34:07 -07005357 bus->regs = (void *)bus->ci->buscorebase;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005358
5359 /* Set core control so an SDIO reset does a backplane reset */
Arend van Sprielff31c542011-03-01 10:56:54 +01005360 OR_REG(&bus->regs->corecontrol, CC_BPRESEN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005361
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02005362 bcm_pktq_init(&bus->txq, (PRIOMASK + 1), TXQLEN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005363
5364 /* Locate an appropriately-aligned portion of hdrbuf */
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07005365 bus->rxhdr = (u8 *) roundup((unsigned long)&bus->hdrbuf[0], DHD_SDALIGN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005366
5367 /* Set the poll and/or interrupt flags */
5368 bus->intr = (bool) dhd_intr;
Jason Cooper9b890322010-09-30 15:15:39 -04005369 bus->poll = (bool) dhd_poll;
5370 if (bus->poll)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005371 bus->pollrate = 1;
5372
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005373 return true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005374
5375fail:
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005376 return false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005377}
5378
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005379static bool dhdsdio_probe_malloc(dhd_bus_t *bus, void *sdh)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005380{
5381 DHD_TRACE(("%s: Enter\n", __func__));
5382
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005383 if (bus->dhd->maxctl) {
5384 bus->rxblen =
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07005385 roundup((bus->dhd->maxctl + SDPCM_HDRLEN),
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005386 ALIGNMENT) + DHD_SDALIGN;
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02005387 bus->rxbuf = kmalloc(bus->rxblen, GFP_ATOMIC);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005388 if (!(bus->rxbuf)) {
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02005389 DHD_ERROR(("%s: kmalloc of %d-byte rxbuf failed\n",
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005390 __func__, bus->rxblen));
5391 goto fail;
5392 }
5393 }
5394
5395 /* Allocate buffer to receive glomed packet */
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02005396 bus->databuf = kmalloc(MAX_DATA_BUF, GFP_ATOMIC);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005397 if (!(bus->databuf)) {
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02005398 DHD_ERROR(("%s: kmalloc of %d-byte databuf failed\n",
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005399 __func__, MAX_DATA_BUF));
5400 /* release rxbuf which was already located as above */
5401 if (!bus->rxblen)
mike.rapoport@gmail.com182acb32010-10-13 00:09:12 +02005402 kfree(bus->rxbuf);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005403 goto fail;
5404 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005405
5406 /* Align the buffer */
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07005407 if ((unsigned long)bus->databuf % DHD_SDALIGN)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005408 bus->dataptr =
5409 bus->databuf + (DHD_SDALIGN -
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07005410 ((unsigned long)bus->databuf % DHD_SDALIGN));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005411 else
5412 bus->dataptr = bus->databuf;
5413
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005414 return true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005415
5416fail:
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005417 return false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005418}
5419
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005420static bool dhdsdio_probe_init(dhd_bus_t *bus, void *sdh)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005421{
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07005422 s32 fnum;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005423
5424 DHD_TRACE(("%s: Enter\n", __func__));
5425
5426#ifdef SDTEST
5427 dhdsdio_pktgen_init(bus);
5428#endif /* SDTEST */
5429
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,
5432 NULL);
5433
5434 bus->dhd->busstate = DHD_BUS_DOWN;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005435 bus->sleeping = false;
5436 bus->rxflow = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005437 bus->prev_rxlim_hit = 0;
5438
5439 /* Done with backplane-dependent accesses, can drop clock... */
5440 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR, 0, NULL);
5441
5442 /* ...and initialize clock/power states */
5443 bus->clkstate = CLK_SDONLY;
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07005444 bus->idletime = (s32) dhd_idletime;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005445 bus->idleclock = DHD_IDLE_ACTIVE;
5446
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005447 /* Query the F2 block size, set roundup accordingly */
5448 fnum = 2;
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07005449 if (bcmsdh_iovar_op(sdh, "sd_blocksize", &fnum, sizeof(s32),
Roland Vossena1c5ad82011-04-11 15:16:24 +02005450 &bus->blocksize, sizeof(s32), false) != 0) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005451 bus->blocksize = 0;
5452 DHD_ERROR(("%s: fail on %s get\n", __func__, "sd_blocksize"));
5453 } else {
5454 DHD_INFO(("%s: Initial value for %s is %d\n",
5455 __func__, "sd_blocksize", bus->blocksize));
5456 }
Greg Kroah-Hartman7068c2f2010-10-08 11:34:59 -07005457 bus->roundup = min(max_roundup, bus->blocksize);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005458
5459 /* Query if bus module supports packet chaining,
5460 default to use if supported */
5461 if (bcmsdh_iovar_op(sdh, "sd_rxchain", NULL, 0,
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07005462 &bus->sd_rxchain, sizeof(s32),
Roland Vossena1c5ad82011-04-11 15:16:24 +02005463 false) != 0) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005464 bus->sd_rxchain = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005465 } else {
5466 DHD_INFO(("%s: bus module (through bcmsdh API) %s chaining\n",
5467 __func__,
5468 (bus->sd_rxchain ? "supports" : "does not support")));
5469 }
5470 bus->use_rxchain = (bool) bus->sd_rxchain;
5471
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005472 return true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005473}
5474
5475bool
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005476dhd_bus_download_firmware(struct dhd_bus *bus, char *fw_path, char *nv_path)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005477{
5478 bool ret;
5479 bus->fw_path = fw_path;
5480 bus->nv_path = nv_path;
5481
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005482 ret = dhdsdio_download_firmware(bus, bus->sdh);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005483
5484 return ret;
5485}
5486
5487static bool
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005488dhdsdio_download_firmware(struct dhd_bus *bus, void *sdh)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005489{
5490 bool ret;
5491
5492 /* Download the firmware */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005493 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005494
5495 ret = _dhdsdio_download_firmware(bus) == 0;
5496
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005497 dhdsdio_clkctl(bus, CLK_SDONLY, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005498
5499 return ret;
5500}
5501
5502/* Detach and free everything */
Arend van Spriel3c9d4c32011-03-02 21:18:48 +01005503static void dhdsdio_release(dhd_bus_t *bus)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005504{
5505 DHD_TRACE(("%s: Enter\n", __func__));
5506
5507 if (bus) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005508 /* De-register interrupt handler */
5509 bcmsdh_intr_disable(bus->sdh);
5510 bcmsdh_intr_dereg(bus->sdh);
5511
5512 if (bus->dhd) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005513 dhd_detach(bus->dhd);
Franky Lincee3cf42011-04-25 19:34:06 -07005514 dhdsdio_release_dongle(bus);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005515 bus->dhd = NULL;
5516 }
5517
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005518 dhdsdio_release_malloc(bus);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005519
mike.rapoport@gmail.com182acb32010-10-13 00:09:12 +02005520 kfree(bus);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005521 }
5522
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005523 DHD_TRACE(("%s: Disconnected\n", __func__));
5524}
5525
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005526static void dhdsdio_release_malloc(dhd_bus_t *bus)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005527{
5528 DHD_TRACE(("%s: Enter\n", __func__));
5529
5530 if (bus->dhd && bus->dhd->dongle_reset)
5531 return;
5532
5533 if (bus->rxbuf) {
mike.rapoport@gmail.com182acb32010-10-13 00:09:12 +02005534 kfree(bus->rxbuf);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005535 bus->rxctl = bus->rxbuf = NULL;
5536 bus->rxlen = 0;
5537 }
5538
Ilia Mirkin46d994b2011-03-13 00:28:56 -05005539 kfree(bus->databuf);
5540 bus->databuf = NULL;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005541}
5542
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005543static void dhdsdio_release_dongle(dhd_bus_t *bus)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005544{
5545 DHD_TRACE(("%s: Enter\n", __func__));
5546
5547 if (bus->dhd && bus->dhd->dongle_reset)
5548 return;
5549
Franky Linc05df632011-04-25 19:34:07 -07005550 if (bus->ci) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005551 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005552 dhdsdio_clkctl(bus, CLK_NONE, false);
Franky Lincee3cf42011-04-25 19:34:06 -07005553 dhdsdio_chip_detach(bus);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005554 if (bus->vars && bus->varsz)
mike.rapoport@gmail.com182acb32010-10-13 00:09:12 +02005555 kfree(bus->vars);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005556 bus->vars = NULL;
5557 }
5558
5559 DHD_TRACE(("%s: Disconnected\n", __func__));
5560}
5561
5562static void dhdsdio_disconnect(void *ptr)
5563{
5564 dhd_bus_t *bus = (dhd_bus_t *)ptr;
5565
5566 DHD_TRACE(("%s: Enter\n", __func__));
5567
5568 if (bus) {
5569 ASSERT(bus->dhd);
Arend van Spriel3c9d4c32011-03-02 21:18:48 +01005570 dhdsdio_release(bus);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005571 }
5572
5573 DHD_TRACE(("%s: Disconnected\n", __func__));
5574}
5575
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.
5579 */
5580
5581static bcmsdh_driver_t dhd_sdio = {
5582 dhdsdio_probe,
5583 dhdsdio_disconnect
5584};
5585
5586int dhd_bus_register(void)
5587{
5588 DHD_TRACE(("%s: Enter\n", __func__));
5589
5590 return bcmsdh_register(&dhd_sdio);
5591}
5592
5593void dhd_bus_unregister(void)
5594{
5595 DHD_TRACE(("%s: Enter\n", __func__));
5596
5597 bcmsdh_unregister();
5598}
5599
5600#ifdef BCMEMBEDIMAGE
5601static int dhdsdio_download_code_array(struct dhd_bus *bus)
5602{
5603 int bcmerror = -1;
5604 int offset = 0;
5605
5606 DHD_INFO(("%s: download embedded firmware...\n", __func__));
5607
5608 /* Download image */
5609 while ((offset + MEMBLOCK) < sizeof(dlarray)) {
5610 bcmerror =
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005611 dhdsdio_membytes(bus, true, offset, dlarray + offset,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005612 MEMBLOCK);
5613 if (bcmerror) {
5614 DHD_ERROR(("%s: error %d on writing %d membytes at "
5615 "0x%08x\n",
5616 __func__, bcmerror, MEMBLOCK, offset));
5617 goto err;
5618 }
5619
5620 offset += MEMBLOCK;
5621 }
5622
5623 if (offset < sizeof(dlarray)) {
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005624 bcmerror = dhdsdio_membytes(bus, true, offset,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005625 dlarray + offset,
5626 sizeof(dlarray) - offset);
5627 if (bcmerror) {
5628 DHD_ERROR(("%s: error %d on writing %d membytes at "
5629 "0x%08x\n", __func__, bcmerror,
5630 sizeof(dlarray) - offset, offset));
5631 goto err;
5632 }
5633 }
5634#ifdef DHD_DEBUG
5635 /* Upload and compare the downloaded code */
5636 {
5637 unsigned char *ularray;
5638
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02005639 ularray = kmalloc(bus->ramsize, GFP_ATOMIC);
Alexander Beregalov570edd32011-03-13 21:58:47 +03005640 if (!ularray) {
Roland Vossene10d82d2011-05-03 11:35:19 +02005641 bcmerror = -ENOMEM;
Alexander Beregalov570edd32011-03-13 21:58:47 +03005642 goto err;
5643 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005644 /* Upload image to verify downloaded contents. */
5645 offset = 0;
5646 memset(ularray, 0xaa, bus->ramsize);
5647 while ((offset + MEMBLOCK) < sizeof(dlarray)) {
5648 bcmerror =
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005649 dhdsdio_membytes(bus, false, offset,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005650 ularray + offset, MEMBLOCK);
5651 if (bcmerror) {
5652 DHD_ERROR(("%s: error %d on reading %d membytes"
5653 " at 0x%08x\n",
5654 __func__, bcmerror, MEMBLOCK, offset));
Alexander Beregalov570edd32011-03-13 21:58:47 +03005655 goto free;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005656 }
5657
5658 offset += MEMBLOCK;
5659 }
5660
5661 if (offset < sizeof(dlarray)) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005662 bcmerror = dhdsdio_membytes(bus, false, offset,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005663 ularray + offset,
5664 sizeof(dlarray) - offset);
5665 if (bcmerror) {
5666 DHD_ERROR(("%s: error %d on reading %d membytes at 0x%08x\n",
5667 __func__, bcmerror,
5668 sizeof(dlarray) - offset, offset));
Alexander Beregalov570edd32011-03-13 21:58:47 +03005669 goto free;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005670 }
5671 }
5672
5673 if (memcmp(dlarray, ularray, sizeof(dlarray))) {
5674 DHD_ERROR(("%s: Downloaded image is corrupted.\n",
5675 __func__));
5676 ASSERT(0);
Alexander Beregalov570edd32011-03-13 21:58:47 +03005677 goto free;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005678 } else
5679 DHD_ERROR(("%s: Download/Upload/Compare succeeded.\n",
5680 __func__));
Alexander Beregalov570edd32011-03-13 21:58:47 +03005681free:
mike.rapoport@gmail.com182acb32010-10-13 00:09:12 +02005682 kfree(ularray);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005683 }
5684#endif /* DHD_DEBUG */
5685
5686err:
5687 return bcmerror;
5688}
5689#endif /* BCMEMBEDIMAGE */
5690
5691static int dhdsdio_download_code_file(struct dhd_bus *bus, char *fw_path)
5692{
5693 int bcmerror = -1;
5694 int offset = 0;
5695 uint len;
5696 void *image = NULL;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07005697 u8 *memblock = NULL, *memptr;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005698
5699 DHD_INFO(("%s: download firmware %s\n", __func__, fw_path));
5700
5701 image = dhd_os_open_image(fw_path);
5702 if (image == NULL)
5703 goto err;
5704
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02005705 memptr = memblock = kmalloc(MEMBLOCK + DHD_SDALIGN, GFP_ATOMIC);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005706 if (memblock == NULL) {
5707 DHD_ERROR(("%s: Failed to allocate memory %d bytes\n",
5708 __func__, MEMBLOCK));
5709 goto err;
5710 }
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07005711 if ((u32)(unsigned long)memblock % DHD_SDALIGN)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005712 memptr +=
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07005713 (DHD_SDALIGN - ((u32)(unsigned long)memblock % DHD_SDALIGN));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005714
5715 /* Download image */
5716 while ((len =
5717 dhd_os_get_image_block((char *)memptr, MEMBLOCK, image))) {
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005718 bcmerror = dhdsdio_membytes(bus, true, offset, memptr, len);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005719 if (bcmerror) {
5720 DHD_ERROR(("%s: error %d on writing %d membytes at "
5721 "0x%08x\n", __func__, bcmerror, MEMBLOCK, offset));
5722 goto err;
5723 }
5724
5725 offset += MEMBLOCK;
5726 }
5727
5728err:
Ilia Mirkin46d994b2011-03-13 00:28:56 -05005729 kfree(memblock);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005730
5731 if (image)
5732 dhd_os_close_image(image);
5733
5734 return bcmerror;
5735}
5736
5737/*
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
5741 * newlines to NULs.
5742 * Shortens buffer as needed and pads with NULs. End of buffer is marked
5743 * by two NULs.
5744*/
5745
5746static uint process_nvram_vars(char *varbuf, uint len)
5747{
5748 char *dp;
5749 bool findNewline;
5750 int column;
5751 uint buf_len, n;
5752
5753 dp = varbuf;
5754
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005755 findNewline = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005756 column = 0;
5757
5758 for (n = 0; n < len; n++) {
5759 if (varbuf[n] == 0)
5760 break;
5761 if (varbuf[n] == '\r')
5762 continue;
5763 if (findNewline && varbuf[n] != '\n')
5764 continue;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005765 findNewline = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005766 if (varbuf[n] == '#') {
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005767 findNewline = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005768 continue;
5769 }
5770 if (varbuf[n] == '\n') {
5771 if (column == 0)
5772 continue;
5773 *dp++ = 0;
5774 column = 0;
5775 continue;
5776 }
5777 *dp++ = varbuf[n];
5778 column++;
5779 }
5780 buf_len = dp - varbuf;
5781
5782 while (dp < varbuf + n)
5783 *dp++ = 0;
5784
5785 return buf_len;
5786}
5787
5788/*
5789 EXAMPLE: nvram_array
5790 nvram_arry format:
5791 name=value
5792 Use carriage return at the end of each assignment,
5793 and an empty string with
5794 carriage return at the end of array.
5795
5796 For example:
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.
5800
5801 Search "EXAMPLE: nvram_array" to see how the array is activated.
5802*/
5803
5804void dhd_bus_set_nvram_params(struct dhd_bus *bus, const char *nvram_params)
5805{
5806 bus->nvram_params = nvram_params;
5807}
5808
5809static int dhdsdio_download_nvram(struct dhd_bus *bus)
5810{
5811 int bcmerror = -1;
5812 uint len;
5813 void *image = NULL;
5814 char *memblock = NULL;
5815 char *bufp;
5816 char *nv_path;
5817 bool nvram_file_exists;
5818
5819 nv_path = bus->nv_path;
5820
5821 nvram_file_exists = ((nv_path != NULL) && (nv_path[0] != '\0'));
5822 if (!nvram_file_exists && (bus->nvram_params == NULL))
5823 return 0;
5824
5825 if (nvram_file_exists) {
5826 image = dhd_os_open_image(nv_path);
5827 if (image == NULL)
5828 goto err;
5829 }
5830
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02005831 memblock = kmalloc(MEMBLOCK, GFP_ATOMIC);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005832 if (memblock == NULL) {
5833 DHD_ERROR(("%s: Failed to allocate memory %d bytes\n",
5834 __func__, MEMBLOCK));
5835 goto err;
5836 }
5837
5838 /* Download variables */
5839 if (nvram_file_exists) {
5840 len = dhd_os_get_image_block(memblock, MEMBLOCK, image);
5841 } else {
5842 len = strlen(bus->nvram_params);
5843 ASSERT(len <= MEMBLOCK);
5844 if (len > MEMBLOCK)
5845 len = MEMBLOCK;
5846 memcpy(memblock, bus->nvram_params, len);
5847 }
5848
5849 if (len > 0 && len < MEMBLOCK) {
5850 bufp = (char *)memblock;
5851 bufp[len] = 0;
5852 len = process_nvram_vars(bufp, len);
5853 bufp += len;
5854 *bufp++ = 0;
5855 if (len)
5856 bcmerror = dhdsdio_downloadvars(bus, memblock, len + 1);
5857 if (bcmerror) {
5858 DHD_ERROR(("%s: error downloading vars: %d\n",
5859 __func__, bcmerror));
5860 }
5861 } else {
5862 DHD_ERROR(("%s: error reading nvram file: %d\n",
5863 __func__, len));
Roland Vossenb74ac122011-05-03 11:35:20 +02005864 bcmerror = -EIO;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005865 }
5866
5867err:
Ilia Mirkin46d994b2011-03-13 00:28:56 -05005868 kfree(memblock);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005869
5870 if (image)
5871 dhd_os_close_image(image);
5872
5873 return bcmerror;
5874}
5875
5876static int _dhdsdio_download_firmware(struct dhd_bus *bus)
5877{
5878 int bcmerror = -1;
5879
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005880 bool embed = false; /* download embedded firmware */
5881 bool dlok = false; /* download firmware succeeded */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005882
5883 /* Out immediately if no image to download */
5884 if ((bus->fw_path == NULL) || (bus->fw_path[0] == '\0')) {
5885#ifdef BCMEMBEDIMAGE
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005886 embed = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005887#else
5888 return bcmerror;
5889#endif
5890 }
5891
5892 /* Keep arm in reset */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005893 if (dhdsdio_download_state(bus, true)) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005894 DHD_ERROR(("%s: error placing ARM core in reset\n", __func__));
5895 goto err;
5896 }
5897
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",
5902 __func__));
5903#ifdef BCMEMBEDIMAGE
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005904 embed = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005905#else
5906 goto err;
5907#endif
5908 } else {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005909 embed = false;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005910 dlok = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005911 }
5912 }
5913#ifdef BCMEMBEDIMAGE
5914 if (embed) {
5915 if (dhdsdio_download_code_array(bus)) {
5916 DHD_ERROR(("%s: dongle image array download failed\n",
5917 __func__));
5918 goto err;
5919 } else {
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005920 dlok = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005921 }
5922 }
5923#endif
5924 if (!dlok) {
5925 DHD_ERROR(("%s: dongle image download failed\n", __func__));
5926 goto err;
5927 }
5928
5929 /* EXAMPLE: nvram_array */
5930 /* If a valid nvram_arry is specified as above, it can be passed
5931 down to dongle */
5932 /* dhd_bus_set_nvram_params(bus, (char *)&nvram_array); */
5933
5934 /* External nvram takes precedence if specified */
5935 if (dhdsdio_download_nvram(bus)) {
5936 DHD_ERROR(("%s: dongle nvram file download failed\n",
5937 __func__));
5938 }
5939
5940 /* Take arm out of reset */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005941 if (dhdsdio_download_state(bus, false)) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005942 DHD_ERROR(("%s: error getting out of ARM core reset\n",
5943 __func__));
5944 goto err;
5945 }
5946
5947 bcmerror = 0;
5948
5949err:
5950 return bcmerror;
5951}
5952
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005953
5954static int
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07005955dhd_bcmsdh_send_buf(dhd_bus_t *bus, u32 addr, uint fn, uint flags,
Arend van Sprielc26b1372010-11-23 14:06:23 +01005956 u8 *buf, uint nbytes, struct sk_buff *pkt,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005957 bcmsdh_cmplt_fn_t complete, void *handle)
5958{
5959 return bcmsdh_send_buf
5960 (bus->sdh, addr, fn, flags, buf, nbytes, pkt, complete,
5961 handle);
5962}
5963
5964uint dhd_bus_chip(struct dhd_bus *bus)
5965{
Franky Linc05df632011-04-25 19:34:07 -07005966 ASSERT(bus->ci != NULL);
5967 return bus->ci->chip;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005968}
5969
5970void *dhd_bus_pub(struct dhd_bus *bus)
5971{
5972 return bus->dhd;
5973}
5974
5975void *dhd_bus_txq(struct dhd_bus *bus)
5976{
5977 return &bus->txq;
5978}
5979
5980uint dhd_bus_hdrlen(struct dhd_bus *bus)
5981{
5982 return SDPCM_HDRLEN;
5983}
5984
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07005985int dhd_bus_devreset(dhd_pub_t *dhdp, u8 flag)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005986{
5987 int bcmerror = 0;
5988 dhd_bus_t *bus;
5989
5990 bus = dhdp->bus;
5991
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005992 if (flag == true) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005993 if (!bus->dhd->dongle_reset) {
5994 /* Expect app to have torn down any
5995 connection before calling */
5996 /* Stop the bus, disable F2 */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005997 dhd_bus_stop(bus, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005998
5999 /* Clean tx/rx buffer pointers,
6000 detach from the dongle */
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01006001 dhdsdio_release_dongle(bus);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07006002
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07006003 bus->dhd->dongle_reset = true;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07006004 bus->dhd->up = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07006005
6006 DHD_TRACE(("%s: WLAN OFF DONE\n", __func__));
6007 /* App can now remove power from device */
6008 } else
Roland Vossenb74ac122011-05-03 11:35:20 +02006009 bcmerror = -EIO;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07006010 } else {
6011 /* App must have restored power to device before calling */
6012
6013 DHD_TRACE(("\n\n%s: == WLAN ON ==\n", __func__));
6014
6015 if (bus->dhd->dongle_reset) {
6016 /* Turn on WLAN */
6017 /* Reset SD client */
6018 bcmsdh_reset(bus->sdh);
6019
6020 /* Attempt to re-attach & download */
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01006021 if (dhdsdio_probe_attach(bus, bus->sdh,
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07006022 (u32 *) SI_ENUM_BASE,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07006023 bus->cl_devid)) {
6024 /* Attempt to download binary to the dongle */
6025 if (dhdsdio_probe_init
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01006026 (bus, bus->sdh)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07006027 && dhdsdio_download_firmware(bus,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07006028 bus->sdh)) {
6029
6030 /* Re-init bus, enable F2 transfer */
6031 dhd_bus_init((dhd_pub_t *) bus->dhd,
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07006032 false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07006033
6034#if defined(OOB_INTR_ONLY)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07006035 dhd_enable_oob_intr(bus, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07006036#endif /* defined(OOB_INTR_ONLY) */
6037
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07006038 bus->dhd->dongle_reset = false;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07006039 bus->dhd->up = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07006040
6041 DHD_TRACE(("%s: WLAN ON DONE\n",
6042 __func__));
6043 } else
Roland Vossenb74ac122011-05-03 11:35:20 +02006044 bcmerror = -EIO;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07006045 } else
Roland Vossenb74ac122011-05-03 11:35:20 +02006046 bcmerror = -EIO;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07006047 } else {
Roland Vossenb74ac122011-05-03 11:35:20 +02006048 bcmerror = -EISCONN;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07006049 DHD_ERROR(("%s: Set DEVRESET=false invoked when device "
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07006050 "is on\n", __func__));
Roland Vossenb74ac122011-05-03 11:35:20 +02006051 bcmerror = -EIO;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07006052 }
6053 }
6054 return bcmerror;
6055}
Franky Lincb63e4c2011-04-25 15:45:08 -07006056
6057static int
6058dhdsdio_chip_recognition(bcmsdh_info_t *sdh, struct chip_info *ci, void *regs)
6059{
6060 u32 regdata;
6061
6062 /*
6063 * Get CC core rev
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.
6067 */
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;
6072
6073 DHD_INFO(("%s: chipid=0x%x chiprev=%d\n",
6074 __func__, ci->chip, ci->chiprev));
6075
6076 /* Address of cores for new chips should be added here */
6077 switch (ci->chip) {
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;
Franky Linc05df632011-04-25 19:34:07 -07006082 ci->ramsize = BCM4329_RAMSIZE;
Franky Lincb63e4c2011-04-25 15:45:08 -07006083 break;
6084 default:
6085 DHD_ERROR(("%s: chipid 0x%x is not supported\n",
6086 __func__, ci->chip));
6087 return -ENODEV;
6088 }
6089
6090 regdata = bcmsdh_reg_read(sdh,
6091 CORE_SB(ci->cccorebase, sbidhigh), 4);
6092 ci->ccrev = SBCOREREV(regdata);
6093
6094 regdata = bcmsdh_reg_read(sdh,
6095 CORE_CC_REG(ci->cccorebase, pmucapabilities), 4);
6096 ci->pmurev = regdata & PCAP_REV_MASK;
6097
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;
6101
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));
6105
6106 /* get chipcommon capabilites */
6107 ci->cccaps = bcmsdh_reg_read(sdh,
6108 CORE_CC_REG(ci->cccorebase, capabilities), 4);
6109
6110 return 0;
6111}
6112
6113static void
6114dhdsdio_chip_disablecore(bcmsdh_info_t *sdh, u32 corebase)
6115{
6116 u32 regdata;
6117
6118 regdata = bcmsdh_reg_read(sdh,
6119 CORE_SB(corebase, sbtmstatelow), 4);
6120 if (regdata & SBTML_RESET)
6121 return;
6122
6123 regdata = bcmsdh_reg_read(sdh,
6124 CORE_SB(corebase, sbtmstatelow), 4);
6125 if ((regdata & (SICF_CLOCK_EN << SBTML_SICF_SHIFT)) != 0) {
6126 /*
6127 * set target reject and spin until busy is clear
6128 * (preserve core-specific bits)
6129 */
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);
6134
6135 regdata = bcmsdh_reg_read(sdh,
6136 CORE_SB(corebase, sbtmstatelow), 4);
6137 udelay(1);
6138 SPINWAIT((bcmsdh_reg_read(sdh,
6139 CORE_SB(corebase, sbtmstatehigh), 4) &
6140 SBTMH_BUSY), 100000);
6141
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__));
6146
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) |
6152 SBIM_RJ;
6153 bcmsdh_reg_write(sdh,
6154 CORE_SB(corebase, sbimstate), 4,
6155 regdata);
6156 regdata = bcmsdh_reg_read(sdh,
6157 CORE_SB(corebase, sbimstate), 4);
6158 udelay(1);
6159 SPINWAIT((bcmsdh_reg_read(sdh,
6160 CORE_SB(corebase, sbimstate), 4) &
6161 SBIM_BY), 100000);
6162 }
6163
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);
6171 udelay(10);
6172
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) &
6179 ~SBIM_RJ;
6180 bcmsdh_reg_write(sdh,
6181 CORE_SB(corebase, sbimstate), 4,
6182 regdata);
6183 }
6184 }
6185
6186 /* leave reset and reject asserted */
6187 bcmsdh_reg_write(sdh, CORE_SB(corebase, sbtmstatelow), 4,
6188 (SBTML_REJ | SBTML_RESET));
6189 udelay(1);
6190}
6191
6192static int
6193dhdsdio_chip_attach(struct dhd_bus *bus, void *regs)
6194{
6195 struct chip_info *ci;
6196 int err;
6197 u8 clkval, clkset;
6198
6199 DHD_TRACE(("%s: Enter\n", __func__));
6200
6201 /* alloc chip_info_t */
6202 ci = kmalloc(sizeof(struct chip_info), GFP_ATOMIC);
6203 if (NULL == ci) {
6204 DHD_ERROR(("%s: malloc failed!\n", __func__));
6205 return -ENOMEM;
6206 }
6207
6208 memset((unsigned char *)ci, 0, sizeof(struct chip_info));
6209
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,
6214 clkset, &err);
6215 if (err) {
6216 DHD_ERROR(("%s: error writing for HT off\n", __func__));
6217 goto fail;
6218 }
6219
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) {
6225 SPINWAIT(((clkval =
6226 bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1,
6227 SBSDIO_FUNC1_CHIPCLKCSR,
6228 NULL)),
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",
6233 __func__, clkval));
6234 err = -EBUSY;
6235 goto fail;
6236 }
6237 clkset = SBSDIO_FORCE_HW_CLKREQ_OFF |
6238 SBSDIO_FORCE_ALP;
6239 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1,
6240 SBSDIO_FUNC1_CHIPCLKCSR,
6241 clkset, &err);
6242 udelay(65);
6243 } else {
6244 DHD_ERROR(("%s: ChipClkCSR access: wrote 0x%02x read 0x%02x\n",
6245 __func__, clkset, clkval));
6246 err = -EACCES;
6247 goto fail;
6248 }
6249
6250 /* Also, disable the extra SDIO pull-ups */
6251 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_SDIOPULLUP, 0,
6252 NULL);
6253
6254 err = dhdsdio_chip_recognition(bus->sdh, ci, regs);
6255 if (err)
6256 goto fail;
6257
6258 /*
6259 * Make sure any on-chip ARM is off (in case strapping is wrong),
6260 * or downloaded code was already running.
6261 */
6262 dhdsdio_chip_disablecore(bus->sdh, ci->armcorebase);
6263
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);
6268
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);
6272
6273 /* WAR: cmd52 backplane read so core HW will drop ALPReq */
6274 clkval = bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1,
6275 0, NULL);
6276
6277 /* Done with backplane-dependent accesses, can drop clock... */
6278 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR, 0,
6279 NULL);
6280
6281 bus->ci = ci;
6282 return 0;
6283fail:
6284 bus->ci = NULL;
6285 kfree(ci);
6286 return err;
6287}
Franky Lineb5dc512011-04-25 19:34:04 -07006288
6289static void
6290dhdsdio_chip_resetcore(bcmsdh_info_t *sdh, u32 corebase)
6291{
6292 u32 regdata;
6293
6294 /*
6295 * Must do the disable sequence first to work for
6296 * arbitrary current core state.
6297 */
6298 dhdsdio_chip_disablecore(sdh, corebase);
6299
6300 /*
6301 * Now do the initialization sequence.
6302 * set reset while enabling the clock and
6303 * forcing them on throughout the core
6304 */
6305 bcmsdh_reg_write(sdh, CORE_SB(corebase, sbtmstatelow), 4,
6306 ((SICF_FGC | SICF_CLOCK_EN) << SBTML_SICF_SHIFT) |
6307 SBTML_RESET);
6308 udelay(1);
6309
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);
6313
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));
6318
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));
6323 udelay(1);
6324
6325 /* leave clock enabled */
6326 bcmsdh_reg_write(sdh, CORE_SB(corebase, sbtmstatelow), 4,
6327 (SICF_CLOCK_EN << SBTML_SICF_SHIFT));
6328 udelay(1);
6329}
Franky Lin5d0d7a92011-04-25 19:34:05 -07006330
6331/* SDIO Pad drive strength to select value mappings */
6332struct sdiod_drive_str {
6333 u8 strength; /* Pad Drive Strength in mA */
6334 u8 sel; /* Chip-specific select value */
6335};
6336
6337/* SDIO Drive Strength to sel value table for PMU Rev 1 */
6338static const struct sdiod_drive_str sdiod_drive_strength_tab1[] = {
6339 {
6340 4, 0x2}, {
6341 2, 0x3}, {
6342 1, 0x0}, {
6343 0, 0x0}
6344 };
6345
6346/* SDIO Drive Strength to sel value table for PMU Rev 2, 3 */
6347static const struct sdiod_drive_str sdiod_drive_strength_tab2[] = {
6348 {
6349 12, 0x7}, {
6350 10, 0x6}, {
6351 8, 0x5}, {
6352 6, 0x4}, {
6353 4, 0x2}, {
6354 2, 0x1}, {
6355 0, 0x0}
6356 };
6357
6358/* SDIO Drive Strength to sel value table for PMU Rev 8 (1.8V) */
6359static const struct sdiod_drive_str sdiod_drive_strength_tab3[] = {
6360 {
6361 32, 0x7}, {
6362 26, 0x6}, {
6363 22, 0x5}, {
6364 16, 0x4}, {
6365 12, 0x3}, {
6366 8, 0x2}, {
6367 4, 0x1}, {
6368 0, 0x0}
6369 };
6370
6371#define SDIOD_DRVSTR_KEY(chip, pmu) (((chip) << 16) | (pmu))
6372
6373static void
6374dhdsdio_sdiod_drive_strength_init(struct dhd_bus *bus, u32 drivestrength) {
6375 struct sdiod_drive_str *str_tab = NULL;
6376 u32 str_mask = 0;
6377 u32 str_shift = 0;
Franky Lin5d0d7a92011-04-25 19:34:05 -07006378 char chn[8];
Franky Lin5d0d7a92011-04-25 19:34:05 -07006379
6380 if (!(bus->ci->cccaps & CC_CAP_PMU))
6381 return;
6382
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;
6387 str_shift = 28;
6388 break;
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;
6393 str_shift = 11;
6394 break;
6395 case SDIOD_DRVSTR_KEY(BCM4336_CHIP_ID, 8):
6396 str_tab = (struct sdiod_drive_str *)&sdiod_drive_strength_tab3;
6397 str_mask = 0x00003800;
6398 str_shift = 11;
6399 break;
6400 default:
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));
6405 break;
6406 }
6407
6408 if (str_tab != NULL) {
6409 u32 drivestrength_sel = 0;
6410 u32 cc_data_temp;
6411 int i;
6412
6413 for (i = 0; str_tab[i].strength != 0; i++) {
6414 if (drivestrength >= str_tab[i].strength) {
6415 drivestrength_sel = str_tab[i].sel;
6416 break;
6417 }
6418 }
6419
6420 bcmsdh_reg_write(bus->sdh,
6421 CORE_CC_REG(bus->ci->cccorebase, chipcontrol_addr),
6422 4, 1);
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),
6430 4, cc_data_temp);
6431
6432 DHD_INFO(("SDIO: %dmA drive strength selected, set to 0x%08x\n",
6433 drivestrength, cc_data_temp));
6434 }
6435}
Franky Lincee3cf42011-04-25 19:34:06 -07006436
6437static void
6438dhdsdio_chip_detach(struct dhd_bus *bus)
6439{
6440 DHD_TRACE(("%s: Enter\n", __func__));
6441
6442 kfree(bus->ci);
6443 bus->ci = NULL;
6444}