blob: cc3e47da3ba04fa64ba259383d0b94f68bb0608f [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>
Franky Lin7c6100e2011-06-01 13:45:36 +020022#include <linux/sched.h>
Franky Lin0df46042011-06-01 13:45:40 +020023#include <linux/mmc/sdio.h>
Franky Lin7c6100e2011-06-01 13:45:36 +020024#include <asm/unaligned.h>
Arend van Spriel34227312011-05-10 22:25:32 +020025#include <bcmdefs.h>
Henry Ptasinskicf2b4482010-09-20 22:33:12 -070026#include <bcmsdh.h>
27
Henry Ptasinskicf2b4482010-09-20 22:33:12 -070028#include <bcmdefs.h>
Franky Lin7c6100e2011-06-01 13:45:36 +020029#include <bcmwifi.h>
Henry Ptasinskicf2b4482010-09-20 22:33:12 -070030#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
Roland Vossenf1c7a082011-06-01 13:45:09 +020064typedef struct _trap_struct {
65 u32 type;
66 u32 epc;
67 u32 cpsr;
68 u32 spsr;
69 u32 r0;
70 u32 r1;
71 u32 r2;
72 u32 r3;
73 u32 r4;
74 u32 r5;
75 u32 r6;
76 u32 r7;
77 u32 r8;
78 u32 r9;
79 u32 r10;
80 u32 r11;
81 u32 r12;
82 u32 r13;
83 u32 r14;
84 u32 pc;
85} trap_t;
86
Roland Vossenf1c7a082011-06-01 13:45:09 +020087#define CBUF_LEN (128)
88
89#define LOG_BUF_LEN 1024
90
91typedef struct {
92 u32 buf; /* Can't be pointer on (64-bit) hosts */
93 uint buf_size;
94 uint idx;
95 char *_buf_compat; /* Redundant pointer for backward compat. */
96} rte_log_t;
97
98typedef struct {
99 /* Virtual UART
100 * When there is no UART (e.g. Quickturn),
101 * the host should write a complete
102 * input line directly into cbuf and then write
103 * the length into vcons_in.
104 * This may also be used when there is a real UART
105 * (at risk of conflicting with
106 * the real UART). vcons_out is currently unused.
107 */
108 volatile uint vcons_in;
109 volatile uint vcons_out;
110
111 /* Output (logging) buffer
112 * Console output is written to a ring buffer log_buf at index log_idx.
113 * The host may read the output when it sees log_idx advance.
114 * Output will be lost if the output wraps around faster than the host
115 * polls.
116 */
117 rte_log_t log;
118
119 /* Console input line buffer
120 * Characters are read one at a time into cbuf
121 * until <CR> is received, then
122 * the buffer is processed as a command line.
123 * Also used for virtual UART.
124 */
125 uint cbuf_idx;
126 char cbuf[CBUF_LEN];
127} rte_cons_t;
128
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700129#endif /* DHD_DEBUG */
Roland Vossen745c9e62011-06-01 13:45:30 +0200130#include <chipcommon.h>
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700131
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700132#include <sbsdio.h>
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700133
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700134#include <dngl_stats.h>
135#include <dhd.h>
136#include <dhd_bus.h>
137#include <dhd_proto.h>
138#include <dhd_dbg.h>
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700139#include <sdiovar.h>
Franky Lincb63e4c2011-04-25 15:45:08 -0700140#include <bcmchip.h>
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700141
142#ifndef DHDSDIO_MEM_DUMP_FNAME
143#define DHDSDIO_MEM_DUMP_FNAME "mem_dump"
144#endif
145
Grant Grundler26a71a42011-03-09 10:41:25 -0800146#define TXQLEN 2048 /* bulk tx queue length */
147#define TXHI (TXQLEN - 256) /* turn on flow control above TXHI */
148#define TXLOW (TXHI - 256) /* turn off flow control below TXLOW */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700149#define PRIOMASK 7
150
151#define TXRETRIES 2 /* # of retries for tx frames */
152
153#if defined(CONFIG_MACH_SANDGATE2G)
154#define DHD_RXBOUND 250 /* Default for max rx frames in
155 one scheduling */
156#else
157#define DHD_RXBOUND 50 /* Default for max rx frames in
158 one scheduling */
159#endif /* defined(CONFIG_MACH_SANDGATE2G) */
160
161#define DHD_TXBOUND 20 /* Default for max tx frames in
162 one scheduling */
163
164#define DHD_TXMINMAX 1 /* Max tx frames if rx still pending */
165
166#define MEMBLOCK 2048 /* Block size used for downloading
167 of dongle image */
168#define MAX_DATA_BUF (32 * 1024) /* Must be large enough to hold
169 biggest possible glom */
170
171/* Packet alignment for most efficient SDIO (can change based on platform) */
172#ifndef DHD_SDALIGN
173#define DHD_SDALIGN 32
174#endif
175#if !ISPOWEROF2(DHD_SDALIGN)
176#error DHD_SDALIGN is not a power of 2!
177#endif
178
179#ifndef DHD_FIRSTREAD
180#define DHD_FIRSTREAD 32
181#endif
182#if !ISPOWEROF2(DHD_FIRSTREAD)
183#error DHD_FIRSTREAD is not a power of 2!
184#endif
185
186/* Total length of frame header for dongle protocol */
187#define SDPCM_HDRLEN (SDPCM_FRAMETAG_LEN + SDPCM_SWHEADER_LEN)
188#ifdef SDTEST
189#define SDPCM_RESERVE (SDPCM_HDRLEN + SDPCM_TEST_HDRLEN + DHD_SDALIGN)
190#else
191#define SDPCM_RESERVE (SDPCM_HDRLEN + DHD_SDALIGN)
192#endif
193
Franky Linb49b14d2011-06-01 13:45:37 +0200194/*
195 * Software allocation of To SB Mailbox resources
196 */
197
198/* tosbmailbox bits corresponding to intstatus bits */
199#define SMB_NAK (1 << 0) /* Frame NAK */
200#define SMB_INT_ACK (1 << 1) /* Host Interrupt ACK */
201#define SMB_USE_OOB (1 << 2) /* Use OOB Wakeup */
202#define SMB_DEV_INT (1 << 3) /* Miscellaneous Interrupt */
203
204/* tosbmailboxdata */
205#define SMB_DATA_VERSION_SHIFT 16 /* host protocol version */
206
207/*
208 * Software allocation of To Host Mailbox resources
209 */
210
211/* intstatus bits */
212#define I_HMB_FC_STATE I_HMB_SW0 /* Flow Control State */
213#define I_HMB_FC_CHANGE I_HMB_SW1 /* Flow Control State Changed */
214#define I_HMB_FRAME_IND I_HMB_SW2 /* Frame Indication */
215#define I_HMB_HOST_INT I_HMB_SW3 /* Miscellaneous Interrupt */
216
217/* tohostmailboxdata */
218#define HMB_DATA_NAKHANDLED 1 /* retransmit NAK'd frame */
219#define HMB_DATA_DEVREADY 2 /* talk to host after enable */
220#define HMB_DATA_FC 4 /* per prio flowcontrol update flag */
221#define HMB_DATA_FWREADY 8 /* fw ready for protocol activity */
222
223#define HMB_DATA_FCDATA_MASK 0xff000000
224#define HMB_DATA_FCDATA_SHIFT 24
225
226#define HMB_DATA_VERSION_MASK 0x00ff0000
227#define HMB_DATA_VERSION_SHIFT 16
228
229/*
230 * Software-defined protocol header
231 */
232
233/* Current protocol version */
234#define SDPCM_PROT_VERSION 4
235
236/* SW frame header */
237#define SDPCM_PACKET_SEQUENCE(p) (((u8 *)p)[0] & 0xff)
238
239#define SDPCM_CHANNEL_MASK 0x00000f00
240#define SDPCM_CHANNEL_SHIFT 8
241#define SDPCM_PACKET_CHANNEL(p) (((u8 *)p)[1] & 0x0f)
242
243#define SDPCM_NEXTLEN_OFFSET 2
244
245/* Data Offset from SOF (HW Tag, SW Tag, Pad) */
246#define SDPCM_DOFFSET_OFFSET 3 /* Data Offset */
247#define SDPCM_DOFFSET_VALUE(p) (((u8 *)p)[SDPCM_DOFFSET_OFFSET] & 0xff)
248#define SDPCM_DOFFSET_MASK 0xff000000
249#define SDPCM_DOFFSET_SHIFT 24
250#define SDPCM_FCMASK_OFFSET 4 /* Flow control */
251#define SDPCM_FCMASK_VALUE(p) (((u8 *)p)[SDPCM_FCMASK_OFFSET] & 0xff)
252#define SDPCM_WINDOW_OFFSET 5 /* Credit based fc */
253#define SDPCM_WINDOW_VALUE(p) (((u8 *)p)[SDPCM_WINDOW_OFFSET] & 0xff)
254
255#define SDPCM_SWHEADER_LEN 8 /* SW header is 64 bits */
256
257/* logical channel numbers */
258#define SDPCM_CONTROL_CHANNEL 0 /* Control channel Id */
259#define SDPCM_EVENT_CHANNEL 1 /* Asyc Event Indication Channel Id */
260#define SDPCM_DATA_CHANNEL 2 /* Data Xmit/Recv Channel Id */
261#define SDPCM_GLOM_CHANNEL 3 /* For coalesced packets */
262#define SDPCM_TEST_CHANNEL 15 /* Reserved for test/debug packets */
263
264#define SDPCM_SEQUENCE_WRAP 256 /* wrap-around val for 8bit frame seq */
265
266#define SDPCM_GLOMDESC(p) (((u8 *)p)[1] & 0x80)
267
268/* For TEST_CHANNEL packets, define another 4-byte header */
269#define SDPCM_TEST_HDRLEN 4 /*
270 * Generally: Cmd(1), Ext(1), Len(2);
271 * Semantics of Ext byte depend on
272 * command. Len is current or requested
273 * frame length, not including test
274 * header; sent little-endian.
275 */
276#define SDPCM_TEST_DISCARD 0x01 /* Receiver discards. Ext:pattern id. */
277#define SDPCM_TEST_ECHOREQ 0x02 /* Echo request. Ext:pattern id. */
278#define SDPCM_TEST_ECHORSP 0x03 /* Echo response. Ext:pattern id. */
279#define SDPCM_TEST_BURST 0x04 /*
280 * Receiver to send a burst.
281 * Ext is a frame count
282 */
283#define SDPCM_TEST_SEND 0x05 /*
284 * Receiver sets send mode.
285 * Ext is boolean on/off
286 */
287
288/* Handy macro for filling in datagen packets with a pattern */
289#define SDPCM_TEST_FILL(byteno, id) ((u8)(id + byteno))
290
291/*
292 * Shared structure between dongle and the host.
293 * The structure contains pointers to trap or assert information.
294 */
295#define SDPCM_SHARED_VERSION 0x0002
296#define SDPCM_SHARED_VERSION_MASK 0x00FF
297#define SDPCM_SHARED_ASSERT_BUILT 0x0100
298#define SDPCM_SHARED_ASSERT 0x0200
299#define SDPCM_SHARED_TRAP 0x0400
300
301
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700302/* Space for header read, limit for data packets */
303#ifndef MAX_HDR_READ
304#define MAX_HDR_READ 32
305#endif
306#if !ISPOWEROF2(MAX_HDR_READ)
307#error MAX_HDR_READ is not a power of 2!
308#endif
309
310#define MAX_RX_DATASZ 2048
311
312/* Maximum milliseconds to wait for F2 to come up */
313#define DHD_WAIT_F2RDY 3000
314
315/* Bump up limit on waiting for HT to account for first startup;
316 * if the image is doing a CRC calculation before programming the PMU
317 * for HT availability, it could take a couple hundred ms more, so
318 * max out at a 1 second (1000000us).
319 */
320#if (PMU_MAX_TRANSITION_DLY <= 1000000)
321#undef PMU_MAX_TRANSITION_DLY
322#define PMU_MAX_TRANSITION_DLY 1000000
323#endif
324
325/* Value for ChipClockCSR during initial setup */
326#define DHD_INIT_CLKCTL1 (SBSDIO_FORCE_HW_CLKREQ_OFF | \
327 SBSDIO_ALP_AVAIL_REQ)
328#define DHD_INIT_CLKCTL2 (SBSDIO_FORCE_HW_CLKREQ_OFF | SBSDIO_FORCE_ALP)
329
330/* Flags for SDH calls */
331#define F2SYNC (SDIO_REQ_4BYTE | SDIO_REQ_FIXED)
332
Roland Vossenb6fe70c2011-06-01 13:45:19 +0200333/* sbimstate */
334#define SBIM_IBE 0x20000 /* inbanderror */
335#define SBIM_TO 0x40000 /* timeout */
336#define SBIM_BY 0x01800000 /* busy (sonics >= 2.3) */
337#define SBIM_RJ 0x02000000 /* reject (sonics >= 2.3) */
338
339/* sbtmstatelow */
340#define SBTML_RESET 0x0001 /* reset */
341#define SBTML_REJ_MASK 0x0006 /* reject field */
342#define SBTML_REJ 0x0002 /* reject */
343#define SBTML_TMPREJ 0x0004 /* temporary reject, for error recovery */
344
345#define SBTML_SICF_SHIFT 16 /* Shift to locate the SI control flags in sbtml */
346
347/* sbtmstatehigh */
348#define SBTMH_SERR 0x0001 /* serror */
349#define SBTMH_INT 0x0002 /* interrupt */
350#define SBTMH_BUSY 0x0004 /* busy */
351#define SBTMH_TO 0x0020 /* timeout (sonics >= 2.3) */
352
353#define SBTMH_SISF_SHIFT 16 /* Shift to locate the SI status flags in sbtmh */
354
355/* sbidlow */
356#define SBIDL_INIT 0x80 /* initiator */
357
358/* sbidhigh */
359#define SBIDH_RC_MASK 0x000f /* revision code */
360#define SBIDH_RCE_MASK 0x7000 /* revision code extension field */
361#define SBIDH_RCE_SHIFT 8
362#define SBCOREREV(sbidh) \
363 ((((sbidh) & SBIDH_RCE_MASK) >> SBIDH_RCE_SHIFT) | ((sbidh) & SBIDH_RC_MASK))
364#define SBIDH_CC_MASK 0x8ff0 /* core code */
365#define SBIDH_CC_SHIFT 4
366#define SBIDH_VC_MASK 0xffff0000 /* vendor code */
367#define SBIDH_VC_SHIFT 16
368
Arend van Spriel70dfb582011-02-25 16:39:17 +0100369/*
370 * Conversion of 802.1D priority to precedence level
371 */
372#define PRIO2PREC(prio) \
373 (((prio) == PRIO_8021D_NONE || (prio) == PRIO_8021D_BE) ? \
374 ((prio^2)) : (prio))
375
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700376DHD_SPINWAIT_SLEEP_INIT(sdioh_spinwait_sleep);
377extern int dhdcdc_set_ioctl(dhd_pub_t *dhd, int ifidx, uint cmd, void *buf,
378 uint len);
379
Roland Vossenb6fe70c2011-06-01 13:45:19 +0200380/* Core reg address translation */
381#define CORE_CC_REG(base, field) (base + offsetof(chipcregs_t, field))
Franky Lin597600a2011-06-01 13:45:39 +0200382#define CORE_BUS_REG(base, field) \
383 (base + offsetof(struct sdpcmd_regs, field))
Roland Vossenb6fe70c2011-06-01 13:45:19 +0200384#define CORE_SB(base, field) \
385 (base + SBCONFIGOFF + offsetof(sbconfig_t, field))
386
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700387#ifdef DHD_DEBUG
388/* Device console log buffer state */
389typedef struct dhd_console {
390 uint count; /* Poll interval msec counter */
391 uint log_addr; /* Log struct address (fixed) */
Roland Vossen70963f92011-06-01 13:45:08 +0200392 rte_log_t log; /* Log struct (host copy) */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700393 uint bufsize; /* Size of log buffer */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700394 u8 *buf; /* Log buffer (host copy) */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700395 uint last; /* Last buffer read index */
396} dhd_console_t;
397#endif /* DHD_DEBUG */
398
Franky Linb49b14d2011-06-01 13:45:37 +0200399struct sdpcm_shared {
400 u32 flags;
401 u32 trap_addr;
402 u32 assert_exp_addr;
403 u32 assert_file_addr;
404 u32 assert_line;
405 u32 console_addr; /* Address of rte_cons_t */
406 u32 msgtrace_addr;
407 u8 tag[32];
408};
409
410
Franky Lincb63e4c2011-04-25 15:45:08 -0700411/* misc chip info needed by some of the routines */
412struct chip_info {
413 u32 chip;
414 u32 chiprev;
415 u32 cccorebase;
416 u32 ccrev;
417 u32 cccaps;
418 u32 buscorebase;
419 u32 buscorerev;
420 u32 buscoretype;
421 u32 ramcorebase;
422 u32 armcorebase;
423 u32 pmurev;
Franky Linc05df632011-04-25 19:34:07 -0700424 u32 ramsize;
Franky Lincb63e4c2011-04-25 15:45:08 -0700425};
426
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700427/* Private data for SDIO bus interaction */
428typedef struct dhd_bus {
429 dhd_pub_t *dhd;
430
431 bcmsdh_info_t *sdh; /* Handle for BCMSDH calls */
Franky Lincb63e4c2011-04-25 15:45:08 -0700432 struct chip_info *ci; /* Chip info struct */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700433 char *vars; /* Variables (from CIS and/or other) */
434 uint varsz; /* Size of variables buffer */
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -0700435 u32 sbaddr; /* Current SB window pointer (-1, invalid) */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700436
Franky Lin597600a2011-06-01 13:45:39 +0200437 struct sdpcmd_regs *regs; /* SDIO core */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700438 uint sdpcmrev; /* SDIO core revision */
439 uint armrev; /* CPU core revision */
440 uint ramrev; /* SOCRAM core revision */
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -0700441 u32 ramsize; /* Size of RAM in SOCRAM (bytes) */
442 u32 orig_ramsize; /* Size of RAM in SOCRAM (bytes) */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700443
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -0700444 u32 bus; /* gSPI or SDIO bus */
445 u32 hostintmask; /* Copy of Host Interrupt Mask */
446 u32 intstatus; /* Intstatus bits (events) pending */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700447 bool dpc_sched; /* Indicates DPC schedule (intrpt rcvd) */
448 bool fcstate; /* State of dongle flow-control */
449
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -0700450 u16 cl_devid; /* cached devid for dhdsdio_probe_attach() */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700451 char *fw_path; /* module_param: path to firmware image */
452 char *nv_path; /* module_param: path to nvram vars file */
453 const char *nvram_params; /* user specified nvram params. */
454
455 uint blocksize; /* Block size of SDIO transfers */
456 uint roundup; /* Max roundup limit */
457
458 struct pktq txq; /* Queue length used for flow-control */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700459 u8 flowcontrol; /* per prio flow control bitmask */
460 u8 tx_seq; /* Transmit sequence number (next) */
461 u8 tx_max; /* Maximum transmit sequence allowed */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700462
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700463 u8 hdrbuf[MAX_HDR_READ + DHD_SDALIGN];
464 u8 *rxhdr; /* Header of current rx frame (in hdrbuf) */
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -0700465 u16 nextlen; /* Next Read Len from last header */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700466 u8 rx_seq; /* Receive sequence number (expected) */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700467 bool rxskip; /* Skip receive (awaiting NAK ACK) */
468
Arend van Sprielc26b1372010-11-23 14:06:23 +0100469 struct sk_buff *glomd; /* Packet containing glomming descriptor */
470 struct sk_buff *glom; /* Packet chain for glommed superframe */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700471 uint glomerr; /* Glom packet read errors */
472
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700473 u8 *rxbuf; /* Buffer for receiving control packets */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700474 uint rxblen; /* Allocated length of rxbuf */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700475 u8 *rxctl; /* Aligned pointer into rxbuf */
476 u8 *databuf; /* Buffer for receiving big glom packet */
477 u8 *dataptr; /* Aligned pointer into databuf */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700478 uint rxlen; /* Length of valid data in buffer */
479
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700480 u8 sdpcm_ver; /* Bus protocol reported by dongle */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700481
482 bool intr; /* Use interrupts */
483 bool poll; /* Use polling */
484 bool ipend; /* Device interrupt is pending */
485 bool intdis; /* Interrupts disabled by isr */
486 uint intrcount; /* Count of device interrupt callbacks */
487 uint lastintrs; /* Count as of last watchdog timer */
488 uint spurious; /* Count of spurious interrupts */
489 uint pollrate; /* Ticks between device polls */
490 uint polltick; /* Tick counter */
491 uint pollcnt; /* Count of active polls */
492
493#ifdef DHD_DEBUG
494 dhd_console_t console; /* Console output polling support */
495 uint console_addr; /* Console address from shared struct */
496#endif /* DHD_DEBUG */
497
498 uint regfails; /* Count of R_REG/W_REG failures */
499
500 uint clkstate; /* State of sd and backplane clock(s) */
501 bool activity; /* Activity flag for clock down */
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -0700502 s32 idletime; /* Control for activity timeout */
503 s32 idlecount; /* Activity timeout counter */
504 s32 idleclock; /* How to set bus driver when idle */
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -0700505 s32 sd_rxchain; /* If bcmsdh api accepts PKT chains */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700506 bool use_rxchain; /* If dhd should use PKT chains */
507 bool sleeping; /* Is SDIO bus sleeping? */
508 bool rxflow_mode; /* Rx flow control mode */
509 bool rxflow; /* Is rx flow control on */
510 uint prev_rxlim_hit; /* Is prev rx limit exceeded
511 (per dpc schedule) */
512 bool alp_only; /* Don't use HT clock (ALP only) */
513/* Field to decide if rx of control frames happen in rxbuf or lb-pool */
514 bool usebufpool;
515
516#ifdef SDTEST
517 /* external loopback */
518 bool ext_loop;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700519 u8 loopid;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700520
521 /* pktgen configuration */
522 uint pktgen_freq; /* Ticks between bursts */
523 uint pktgen_count; /* Packets to send each burst */
524 uint pktgen_print; /* Bursts between count displays */
525 uint pktgen_total; /* Stop after this many */
526 uint pktgen_minlen; /* Minimum packet data len */
527 uint pktgen_maxlen; /* Maximum packet data len */
528 uint pktgen_mode; /* Configured mode: tx, rx, or echo */
529 uint pktgen_stop; /* Number of tx failures causing stop */
530
531 /* active pktgen fields */
532 uint pktgen_tick; /* Tick counter for bursts */
533 uint pktgen_ptick; /* Burst counter for printing */
534 uint pktgen_sent; /* Number of test packets generated */
535 uint pktgen_rcvd; /* Number of test packets received */
536 uint pktgen_fail; /* Number of failed send attempts */
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -0700537 u16 pktgen_len; /* Length of next packet to send */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700538#endif /* SDTEST */
539
540 /* Some additional counters */
541 uint tx_sderrs; /* Count of tx attempts with sd errors */
542 uint fcqueued; /* Tx packets that got queued */
543 uint rxrtx; /* Count of rtx requests (NAK to dongle) */
544 uint rx_toolong; /* Receive frames too long to receive */
545 uint rxc_errors; /* SDIO errors when reading control frames */
546 uint rx_hdrfail; /* SDIO errors on header reads */
547 uint rx_badhdr; /* Bad received headers (roosync?) */
548 uint rx_badseq; /* Mismatched rx sequence number */
549 uint fc_rcvd; /* Number of flow-control events received */
550 uint fc_xoff; /* Number which turned on flow-control */
551 uint fc_xon; /* Number which turned off flow-control */
552 uint rxglomfail; /* Failed deglom attempts */
553 uint rxglomframes; /* Number of glom frames (superframes) */
554 uint rxglompkts; /* Number of packets from glom frames */
555 uint f2rxhdrs; /* Number of header reads */
556 uint f2rxdata; /* Number of frame data reads */
557 uint f2txdata; /* Number of f2 frame writes */
558 uint f1regdata; /* Number of f1 register accesses */
559
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700560 u8 *ctrl_frame_buf;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -0700561 u32 ctrl_frame_len;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700562 bool ctrl_frame_stat;
563} dhd_bus_t;
564
Roland Vossenb6fe70c2011-06-01 13:45:19 +0200565typedef volatile struct _sbconfig {
566 u32 PAD[2];
567 u32 sbipsflag; /* initiator port ocp slave flag */
568 u32 PAD[3];
569 u32 sbtpsflag; /* target port ocp slave flag */
570 u32 PAD[11];
571 u32 sbtmerrloga; /* (sonics >= 2.3) */
572 u32 PAD;
573 u32 sbtmerrlog; /* (sonics >= 2.3) */
574 u32 PAD[3];
575 u32 sbadmatch3; /* address match3 */
576 u32 PAD;
577 u32 sbadmatch2; /* address match2 */
578 u32 PAD;
579 u32 sbadmatch1; /* address match1 */
580 u32 PAD[7];
581 u32 sbimstate; /* initiator agent state */
582 u32 sbintvec; /* interrupt mask */
583 u32 sbtmstatelow; /* target state */
584 u32 sbtmstatehigh; /* target state */
585 u32 sbbwa0; /* bandwidth allocation table0 */
586 u32 PAD;
587 u32 sbimconfiglow; /* initiator configuration */
588 u32 sbimconfighigh; /* initiator configuration */
589 u32 sbadmatch0; /* address match0 */
590 u32 PAD;
591 u32 sbtmconfiglow; /* target configuration */
592 u32 sbtmconfighigh; /* target configuration */
593 u32 sbbconfig; /* broadcast configuration */
594 u32 PAD;
595 u32 sbbstate; /* broadcast state */
596 u32 PAD[3];
597 u32 sbactcnfg; /* activate configuration */
598 u32 PAD[3];
599 u32 sbflagst; /* current sbflags */
600 u32 PAD[3];
601 u32 sbidlow; /* identification */
602 u32 sbidhigh; /* identification */
603} sbconfig_t;
604
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700605/* clkstate */
606#define CLK_NONE 0
607#define CLK_SDONLY 1
608#define CLK_PENDING 2 /* Not used yet */
609#define CLK_AVAIL 3
610
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -0700611#define DHD_NOPMU(dhd) (false)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700612
613#ifdef DHD_DEBUG
614static int qcount[NUMPRIO];
615static int tx_packets[NUMPRIO];
616#endif /* DHD_DEBUG */
617
618/* Deferred transmit */
619const uint dhd_deferred_tx = 1;
620
621extern uint dhd_watchdog_ms;
622extern void dhd_os_wd_timer(void *bus, uint wdtick);
623
624/* Tx/Rx bounds */
625uint dhd_txbound;
626uint dhd_rxbound;
627uint dhd_txminmax;
628
629/* override the RAM size if possible */
630#define DONGLE_MIN_MEMSIZE (128 * 1024)
631int dhd_dongle_memsize;
632
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700633static bool dhd_alignctl;
634
635static bool sd1idle;
636
637static bool retrydata;
638#define RETRYCHAN(chan) (((chan) == SDPCM_EVENT_CHANNEL) || retrydata)
639
640static const uint watermark = 8;
641static const uint firstread = DHD_FIRSTREAD;
642
643#define HDATLEN (firstread - (SDPCM_HDRLEN))
644
645/* Retry count for register access failures */
646static const uint retry_limit = 2;
647
648/* Force even SD lengths (some host controllers mess up on odd bytes) */
649static bool forcealign;
650
651#define ALIGNMENT 4
652
653#if defined(OOB_INTR_ONLY) && defined(HW_OOB)
654extern void bcmsdh_enable_hw_oob_intr(void *sdh, bool enable);
655#endif
656
657#if defined(OOB_INTR_ONLY) && defined(SDIO_ISR_THREAD)
658#error OOB_INTR_ONLY is NOT working with SDIO_ISR_THREAD
659#endif /* defined(OOB_INTR_ONLY) && defined(SDIO_ISR_THREAD) */
Arend van Spriel3c9d4c32011-03-02 21:18:48 +0100660#define PKTALIGN(_p, _len, _align) \
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700661 do { \
662 uint datalign; \
Arend van Spriel54991ad2010-11-23 14:06:24 +0100663 datalign = (unsigned long)((_p)->data); \
664 datalign = roundup(datalign, (_align)) - datalign; \
665 ASSERT(datalign < (_align)); \
666 ASSERT((_p)->len >= ((_len) + datalign)); \
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700667 if (datalign) \
Arend van Spriel54991ad2010-11-23 14:06:24 +0100668 skb_pull((_p), datalign); \
669 __skb_trim((_p), (_len)); \
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700670 } while (0)
671
672/* Limit on rounding up frames */
673static const uint max_roundup = 512;
674
675/* Try doing readahead */
676static bool dhd_readahead;
677
678/* To check if there's window offered */
679#define DATAOK(bus) \
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700680 (((u8)(bus->tx_max - bus->tx_seq) != 0) && \
681 (((u8)(bus->tx_max - bus->tx_seq) & 0x80) == 0))
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700682
683/* Macros to get register read/write status */
684/* NOTE: these assume a local dhdsdio_bus_t *bus! */
685#define R_SDREG(regvar, regaddr, retryvar) \
686do { \
687 retryvar = 0; \
688 do { \
Arend van Sprielff31c542011-03-01 10:56:54 +0100689 regvar = R_REG(regaddr); \
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700690 } while (bcmsdh_regfail(bus->sdh) && (++retryvar <= retry_limit)); \
691 if (retryvar) { \
692 bus->regfails += (retryvar-1); \
693 if (retryvar > retry_limit) { \
694 DHD_ERROR(("%s: FAILED" #regvar "READ, LINE %d\n", \
695 __func__, __LINE__)); \
696 regvar = 0; \
697 } \
698 } \
699} while (0)
700
701#define W_SDREG(regval, regaddr, retryvar) \
702do { \
703 retryvar = 0; \
704 do { \
Arend van Sprielff31c542011-03-01 10:56:54 +0100705 W_REG(regaddr, regval); \
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700706 } while (bcmsdh_regfail(bus->sdh) && (++retryvar <= retry_limit)); \
707 if (retryvar) { \
708 bus->regfails += (retryvar-1); \
709 if (retryvar > retry_limit) \
710 DHD_ERROR(("%s: FAILED REGISTER WRITE, LINE %d\n", \
711 __func__, __LINE__)); \
712 } \
713} while (0)
714
715#define DHD_BUS SDIO_BUS
716
717#define PKT_AVAILABLE() (intstatus & I_HMB_FRAME_IND)
718
719#define HOSTINTMASK (I_HMB_SW_MASK | I_CHIPACTIVE)
720
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700721#ifdef SDTEST
722static void dhdsdio_testrcv(dhd_bus_t *bus, void *pkt, uint seq);
723static void dhdsdio_sdtest_set(dhd_bus_t *bus, bool start);
724#endif
725
726#ifdef DHD_DEBUG
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700727static int dhdsdio_checkdied(dhd_bus_t *bus, u8 *data, uint size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700728static int dhdsdio_mem_dump(dhd_bus_t *bus);
729#endif /* DHD_DEBUG */
730static int dhdsdio_download_state(dhd_bus_t *bus, bool enter);
731
Arend van Spriel3c9d4c32011-03-02 21:18:48 +0100732static void dhdsdio_release(dhd_bus_t *bus);
Arend van Spriel8da4a3a2011-03-02 21:18:42 +0100733static void dhdsdio_release_malloc(dhd_bus_t *bus);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700734static void dhdsdio_disconnect(void *ptr);
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -0700735static bool dhdsdio_chipmatch(u16 chipid);
Arend van Spriel8da4a3a2011-03-02 21:18:42 +0100736static bool dhdsdio_probe_attach(dhd_bus_t *bus, void *sdh,
737 void *regsva, u16 devid);
738static bool dhdsdio_probe_malloc(dhd_bus_t *bus, void *sdh);
739static bool dhdsdio_probe_init(dhd_bus_t *bus, void *sdh);
740static void dhdsdio_release_dongle(dhd_bus_t *bus);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700741
742static uint process_nvram_vars(char *varbuf, uint len);
743
744static void dhd_dongle_setmemsize(struct dhd_bus *bus, int mem_size);
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -0700745static int dhd_bcmsdh_send_buf(dhd_bus_t *bus, u32 addr, uint fn,
Arend van Sprielc26b1372010-11-23 14:06:23 +0100746 uint flags, u8 *buf, uint nbytes,
747 struct sk_buff *pkt, bcmsdh_cmplt_fn_t complete,
748 void *handle);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700749
Arend van Spriel8da4a3a2011-03-02 21:18:42 +0100750static bool dhdsdio_download_firmware(struct dhd_bus *bus, void *sdh);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700751static int _dhdsdio_download_firmware(struct dhd_bus *bus);
752
753static int dhdsdio_download_code_file(struct dhd_bus *bus, char *image_path);
754static int dhdsdio_download_nvram(struct dhd_bus *bus);
Franky Lineb5dc512011-04-25 19:34:04 -0700755static void dhdsdio_chip_disablecore(bcmsdh_info_t *sdh, u32 corebase);
Franky Lincb63e4c2011-04-25 15:45:08 -0700756static int dhdsdio_chip_attach(struct dhd_bus *bus, void *regs);
Franky Lineb5dc512011-04-25 19:34:04 -0700757static void dhdsdio_chip_resetcore(bcmsdh_info_t *sdh, u32 corebase);
Franky Lin5d0d7a92011-04-25 19:34:05 -0700758static void dhdsdio_sdiod_drive_strength_init(struct dhd_bus *bus,
759 u32 drivestrength);
Franky Lincee3cf42011-04-25 19:34:06 -0700760static void dhdsdio_chip_detach(struct dhd_bus *bus);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700761
Grant Grundler4b455e02011-05-04 09:59:47 -0700762/* Packet free applicable unconditionally for sdio and sdspi.
763 * Conditional if bufpool was present for gspi bus.
764 */
765static void dhdsdio_pktfree2(dhd_bus_t *bus, struct sk_buff *pkt)
766{
767 dhd_os_sdlock_rxq(bus->dhd);
768 if ((bus->bus != SPI_BUS) || bus->usebufpool)
Roland Vossen67ad48b2011-06-01 13:45:51 +0200769 brcmu_pkt_buf_free_skb(pkt);
Grant Grundler4b455e02011-05-04 09:59:47 -0700770 dhd_os_sdunlock_rxq(bus->dhd);
771}
772
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700773static void dhd_dongle_setmemsize(struct dhd_bus *bus, int mem_size)
774{
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -0700775 s32 min_size = DONGLE_MIN_MEMSIZE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700776 /* Restrict the memsize to user specified limit */
777 DHD_ERROR(("user: Restrict the dongle ram size to %d, min %d\n",
778 dhd_dongle_memsize, min_size));
779 if ((dhd_dongle_memsize > min_size) &&
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -0700780 (dhd_dongle_memsize < (s32) bus->orig_ramsize))
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700781 bus->ramsize = dhd_dongle_memsize;
782}
783
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -0700784static int dhdsdio_set_siaddr_window(dhd_bus_t *bus, u32 address)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700785{
786 int err = 0;
787 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_SBADDRLOW,
788 (address >> 8) & SBSDIO_SBADDRLOW_MASK, &err);
789 if (!err)
790 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_SBADDRMID,
791 (address >> 16) & SBSDIO_SBADDRMID_MASK, &err);
792 if (!err)
793 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_SBADDRHIGH,
794 (address >> 24) & SBSDIO_SBADDRHIGH_MASK,
795 &err);
796 return err;
797}
798
799/* Turn backplane clock on or off */
800static int dhdsdio_htclk(dhd_bus_t *bus, bool on, bool pendok)
801{
802 int err;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700803 u8 clkctl, clkreq, devctl;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700804 bcmsdh_info_t *sdh;
805
806 DHD_TRACE(("%s: Enter\n", __func__));
807
808#if defined(OOB_INTR_ONLY)
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -0700809 pendok = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700810#endif
811 clkctl = 0;
812 sdh = bus->sdh;
813
814 if (on) {
815 /* Request HT Avail */
816 clkreq =
817 bus->alp_only ? SBSDIO_ALP_AVAIL_REQ : SBSDIO_HT_AVAIL_REQ;
818
Franky Linc05df632011-04-25 19:34:07 -0700819 if ((bus->ci->chip == BCM4329_CHIP_ID)
820 && (bus->ci->chiprev == 0))
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700821 clkreq |= SBSDIO_FORCE_ALP;
822
823 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
824 clkreq, &err);
825 if (err) {
826 DHD_ERROR(("%s: HT Avail request error: %d\n",
827 __func__, err));
Roland Vossenb74ac122011-05-03 11:35:20 +0200828 return -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700829 }
830
Franky Linc05df632011-04-25 19:34:07 -0700831 if (pendok && ((bus->ci->buscoretype == PCMCIA_CORE_ID)
832 && (bus->ci->buscorerev == 9))) {
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -0700833 u32 dummy, retries;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700834 R_SDREG(dummy, &bus->regs->clockctlstatus, retries);
835 }
836
837 /* Check current status */
838 clkctl =
839 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
840 &err);
841 if (err) {
842 DHD_ERROR(("%s: HT Avail read error: %d\n",
843 __func__, err));
Roland Vossenb74ac122011-05-03 11:35:20 +0200844 return -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700845 }
846
847 /* Go to pending and await interrupt if appropriate */
848 if (!SBSDIO_CLKAV(clkctl, bus->alp_only) && pendok) {
849 /* Allow only clock-available interrupt */
850 devctl =
851 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
852 &err);
853 if (err) {
854 DHD_ERROR(("%s: Devctl error setting CA: %d\n",
855 __func__, err));
Roland Vossenb74ac122011-05-03 11:35:20 +0200856 return -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700857 }
858
859 devctl |= SBSDIO_DEVCTL_CA_INT_ONLY;
860 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
861 devctl, &err);
862 DHD_INFO(("CLKCTL: set PENDING\n"));
863 bus->clkstate = CLK_PENDING;
864
Roland Vossena1c5ad82011-04-11 15:16:24 +0200865 return 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700866 } else if (bus->clkstate == CLK_PENDING) {
867 /* Cancel CA-only interrupt filter */
868 devctl =
869 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
870 &err);
871 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
872 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
873 devctl, &err);
874 }
875
876 /* Otherwise, wait here (polling) for HT Avail */
877 if (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
878 SPINWAIT_SLEEP(sdioh_spinwait_sleep,
879 ((clkctl =
880 bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
881 SBSDIO_FUNC1_CHIPCLKCSR,
882 &err)),
883 !SBSDIO_CLKAV(clkctl, bus->alp_only)),
884 PMU_MAX_TRANSITION_DLY);
885 }
886 if (err) {
887 DHD_ERROR(("%s: HT Avail request error: %d\n",
888 __func__, err));
Roland Vossenb74ac122011-05-03 11:35:20 +0200889 return -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700890 }
891 if (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
892 DHD_ERROR(("%s: HT Avail timeout (%d): clkctl 0x%02x\n",
893 __func__, PMU_MAX_TRANSITION_DLY, clkctl));
Roland Vossenb74ac122011-05-03 11:35:20 +0200894 return -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700895 }
896
897 /* Mark clock available */
898 bus->clkstate = CLK_AVAIL;
899 DHD_INFO(("CLKCTL: turned ON\n"));
900
901#if defined(DHD_DEBUG)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -0700902 if (bus->alp_only == true) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700903#if !defined(BCMLXSDMMC)
904 if (!SBSDIO_ALPONLY(clkctl)) {
905 DHD_ERROR(("%s: HT Clock, when ALP Only\n",
906 __func__));
907 }
908#endif /* !defined(BCMLXSDMMC) */
909 } else {
910 if (SBSDIO_ALPONLY(clkctl)) {
911 DHD_ERROR(("%s: HT Clock should be on.\n",
912 __func__));
913 }
914 }
915#endif /* defined (DHD_DEBUG) */
916
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -0700917 bus->activity = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700918 } else {
919 clkreq = 0;
920
921 if (bus->clkstate == CLK_PENDING) {
922 /* Cancel CA-only interrupt filter */
923 devctl =
924 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
925 &err);
926 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
927 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
928 devctl, &err);
929 }
930
931 bus->clkstate = CLK_SDONLY;
932 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
933 clkreq, &err);
934 DHD_INFO(("CLKCTL: turned OFF\n"));
935 if (err) {
936 DHD_ERROR(("%s: Failed access turning clock off: %d\n",
937 __func__, err));
Roland Vossenb74ac122011-05-03 11:35:20 +0200938 return -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700939 }
940 }
Roland Vossena1c5ad82011-04-11 15:16:24 +0200941 return 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700942}
943
944/* Change idle/active SD state */
945static int dhdsdio_sdclk(dhd_bus_t *bus, bool on)
946{
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700947 DHD_TRACE(("%s: Enter\n", __func__));
948
Franky Lin602a8ab2011-06-01 13:45:04 +0200949 if (on)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700950 bus->clkstate = CLK_SDONLY;
Franky Lin602a8ab2011-06-01 13:45:04 +0200951 else
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700952 bus->clkstate = CLK_NONE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700953
Roland Vossena1c5ad82011-04-11 15:16:24 +0200954 return 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700955}
956
957/* Transition SD and backplane clock readiness */
958static int dhdsdio_clkctl(dhd_bus_t *bus, uint target, bool pendok)
959{
960#ifdef DHD_DEBUG
961 uint oldstate = bus->clkstate;
962#endif /* DHD_DEBUG */
963
964 DHD_TRACE(("%s: Enter\n", __func__));
965
966 /* Early exit if we're already there */
967 if (bus->clkstate == target) {
968 if (target == CLK_AVAIL) {
969 dhd_os_wd_timer(bus->dhd, dhd_watchdog_ms);
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -0700970 bus->activity = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700971 }
Roland Vossena1c5ad82011-04-11 15:16:24 +0200972 return 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700973 }
974
975 switch (target) {
976 case CLK_AVAIL:
977 /* Make sure SD clock is available */
978 if (bus->clkstate == CLK_NONE)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -0700979 dhdsdio_sdclk(bus, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700980 /* Now request HT Avail on the backplane */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -0700981 dhdsdio_htclk(bus, true, pendok);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700982 dhd_os_wd_timer(bus->dhd, dhd_watchdog_ms);
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -0700983 bus->activity = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700984 break;
985
986 case CLK_SDONLY:
987 /* Remove HT request, or bring up SD clock */
988 if (bus->clkstate == CLK_NONE)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -0700989 dhdsdio_sdclk(bus, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700990 else if (bus->clkstate == CLK_AVAIL)
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -0700991 dhdsdio_htclk(bus, false, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700992 else
993 DHD_ERROR(("dhdsdio_clkctl: request for %d -> %d\n",
994 bus->clkstate, target));
995 dhd_os_wd_timer(bus->dhd, dhd_watchdog_ms);
996 break;
997
998 case CLK_NONE:
999 /* Make sure to remove HT request */
1000 if (bus->clkstate == CLK_AVAIL)
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001001 dhdsdio_htclk(bus, false, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001002 /* Now remove the SD clock */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001003 dhdsdio_sdclk(bus, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001004 dhd_os_wd_timer(bus->dhd, 0);
1005 break;
1006 }
1007#ifdef DHD_DEBUG
1008 DHD_INFO(("dhdsdio_clkctl: %d -> %d\n", oldstate, bus->clkstate));
1009#endif /* DHD_DEBUG */
1010
Roland Vossena1c5ad82011-04-11 15:16:24 +02001011 return 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001012}
1013
1014int dhdsdio_bussleep(dhd_bus_t *bus, bool sleep)
1015{
1016 bcmsdh_info_t *sdh = bus->sdh;
Franky Lin597600a2011-06-01 13:45:39 +02001017 struct sdpcmd_regs *regs = bus->regs;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001018 uint retries = 0;
1019
1020 DHD_INFO(("dhdsdio_bussleep: request %s (currently %s)\n",
1021 (sleep ? "SLEEP" : "WAKE"),
1022 (bus->sleeping ? "SLEEP" : "WAKE")));
1023
1024 /* Done if we're already in the requested state */
1025 if (sleep == bus->sleeping)
Roland Vossena1c5ad82011-04-11 15:16:24 +02001026 return 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001027
1028 /* Going to sleep: set the alarm and turn off the lights... */
1029 if (sleep) {
1030 /* Don't sleep if something is pending */
1031 if (bus->dpc_sched || bus->rxskip || pktq_len(&bus->txq))
Roland Vossene10d82d2011-05-03 11:35:19 +02001032 return -EBUSY;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001033
1034 /* Disable SDIO interrupts (no longer interested) */
1035 bcmsdh_intr_disable(bus->sdh);
1036
1037 /* Make sure the controller has the bus up */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001038 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001039
1040 /* Tell device to start using OOB wakeup */
1041 W_SDREG(SMB_USE_OOB, &regs->tosbmailbox, retries);
1042 if (retries > retry_limit)
1043 DHD_ERROR(("CANNOT SIGNAL CHIP, WILL NOT WAKE UP!!\n"));
1044
1045 /* Turn off our contribution to the HT clock request */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001046 dhdsdio_clkctl(bus, CLK_SDONLY, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001047
1048 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
1049 SBSDIO_FORCE_HW_CLKREQ_OFF, NULL);
1050
1051 /* Isolate the bus */
Franky Linc05df632011-04-25 19:34:07 -07001052 if (bus->ci->chip != BCM4329_CHIP_ID
1053 && bus->ci->chip != BCM4319_CHIP_ID) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001054 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
1055 SBSDIO_DEVCTL_PADS_ISO, NULL);
1056 }
1057
1058 /* Change state */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001059 bus->sleeping = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001060
1061 } else {
1062 /* Waking up: bus power up is ok, set local state */
1063
1064 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
1065 0, NULL);
1066
1067 /* Force pad isolation off if possible
1068 (in case power never toggled) */
Franky Linc05df632011-04-25 19:34:07 -07001069 if ((bus->ci->buscoretype == PCMCIA_CORE_ID)
1070 && (bus->ci->buscorerev >= 10))
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001071 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL, 0,
1072 NULL);
1073
1074 /* Make sure the controller has the bus up */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001075 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001076
1077 /* Send misc interrupt to indicate OOB not needed */
1078 W_SDREG(0, &regs->tosbmailboxdata, retries);
1079 if (retries <= retry_limit)
1080 W_SDREG(SMB_DEV_INT, &regs->tosbmailbox, retries);
1081
1082 if (retries > retry_limit)
1083 DHD_ERROR(("CANNOT SIGNAL CHIP TO CLEAR OOB!!\n"));
1084
1085 /* Make sure we have SD bus access */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001086 dhdsdio_clkctl(bus, CLK_SDONLY, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001087
1088 /* Change state */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001089 bus->sleeping = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001090
1091 /* Enable interrupts again */
1092 if (bus->intr && (bus->dhd->busstate == DHD_BUS_DATA)) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001093 bus->intdis = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001094 bcmsdh_intr_enable(bus->sdh);
1095 }
1096 }
1097
Roland Vossena1c5ad82011-04-11 15:16:24 +02001098 return 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001099}
1100
1101#if defined(OOB_INTR_ONLY)
1102void dhd_enable_oob_intr(struct dhd_bus *bus, bool enable)
1103{
1104#if defined(HW_OOB)
1105 bcmsdh_enable_hw_oob_intr(bus->sdh, enable);
1106#else
1107 sdpcmd_regs_t *regs = bus->regs;
1108 uint retries = 0;
1109
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001110 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001111 if (enable == true) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001112
1113 /* Tell device to start using OOB wakeup */
1114 W_SDREG(SMB_USE_OOB, &regs->tosbmailbox, retries);
1115 if (retries > retry_limit)
1116 DHD_ERROR(("CANNOT SIGNAL CHIP, WILL NOT WAKE UP!!\n"));
1117
1118 } else {
1119 /* Send misc interrupt to indicate OOB not needed */
1120 W_SDREG(0, &regs->tosbmailboxdata, retries);
1121 if (retries <= retry_limit)
1122 W_SDREG(SMB_DEV_INT, &regs->tosbmailbox, retries);
1123 }
1124
1125 /* Turn off our contribution to the HT clock request */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001126 dhdsdio_clkctl(bus, CLK_SDONLY, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001127#endif /* !defined(HW_OOB) */
1128}
1129#endif /* defined(OOB_INTR_ONLY) */
1130
1131#define BUS_WAKE(bus) \
1132 do { \
1133 if ((bus)->sleeping) \
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001134 dhdsdio_bussleep((bus), false); \
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001135 } while (0);
1136
1137/* Writes a HW/SW header into the packet and sends it. */
1138/* Assumes: (a) header space already there, (b) caller holds lock */
Arend van Sprielc26b1372010-11-23 14:06:23 +01001139static int dhdsdio_txpkt(dhd_bus_t *bus, struct sk_buff *pkt, uint chan,
1140 bool free_pkt)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001141{
1142 int ret;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001143 u8 *frame;
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07001144 u16 len, pad = 0;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07001145 u32 swheader;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001146 uint retries = 0;
1147 bcmsdh_info_t *sdh;
Arend van Sprielc26b1372010-11-23 14:06:23 +01001148 struct sk_buff *new;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001149 int i;
1150
1151 DHD_TRACE(("%s: Enter\n", __func__));
1152
1153 sdh = bus->sdh;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001154
1155 if (bus->dhd->dongle_reset) {
Roland Vossenb74ac122011-05-03 11:35:20 +02001156 ret = -EPERM;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001157 goto done;
1158 }
1159
Arend van Spriel54991ad2010-11-23 14:06:24 +01001160 frame = (u8 *) (pkt->data);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001161
1162 /* Add alignment padding, allocate new packet if needed */
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07001163 pad = ((unsigned long)frame % DHD_SDALIGN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001164 if (pad) {
Arend van Spriel3be727c2010-11-23 22:20:30 +01001165 if (skb_headroom(pkt) < pad) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001166 DHD_INFO(("%s: insufficient headroom %d for %d pad\n",
Arend van Spriel3be727c2010-11-23 22:20:30 +01001167 __func__, skb_headroom(pkt), pad));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001168 bus->dhd->tx_realloc++;
Roland Vossen67ad48b2011-06-01 13:45:51 +02001169 new = brcmu_pkt_buf_get_skb(pkt->len + DHD_SDALIGN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001170 if (!new) {
1171 DHD_ERROR(("%s: couldn't allocate new %d-byte "
1172 "packet\n",
Arend van Spriel54991ad2010-11-23 14:06:24 +01001173 __func__, pkt->len + DHD_SDALIGN));
Roland Vossene10d82d2011-05-03 11:35:19 +02001174 ret = -ENOMEM;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001175 goto done;
1176 }
1177
Arend van Spriel3c9d4c32011-03-02 21:18:48 +01001178 PKTALIGN(new, pkt->len, DHD_SDALIGN);
Stanislav Fomichev02160692011-02-15 01:05:10 +03001179 memcpy(new->data, pkt->data, pkt->len);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001180 if (free_pkt)
Roland Vossen67ad48b2011-06-01 13:45:51 +02001181 brcmu_pkt_buf_free_skb(pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001182 /* free the pkt if canned one is not used */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001183 free_pkt = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001184 pkt = new;
Arend van Spriel54991ad2010-11-23 14:06:24 +01001185 frame = (u8 *) (pkt->data);
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07001186 ASSERT(((unsigned long)frame % DHD_SDALIGN) == 0);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001187 pad = 0;
1188 } else {
Arend van Sprielc303ecb2010-11-18 20:46:43 +01001189 skb_push(pkt, pad);
Arend van Spriel54991ad2010-11-23 14:06:24 +01001190 frame = (u8 *) (pkt->data);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001191
Arend van Spriel54991ad2010-11-23 14:06:24 +01001192 ASSERT((pad + SDPCM_HDRLEN) <= (int)(pkt->len));
Brett Rudley9249ede2010-11-30 20:09:49 -08001193 memset(frame, 0, pad + SDPCM_HDRLEN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001194 }
1195 }
1196 ASSERT(pad < DHD_SDALIGN);
1197
1198 /* Hardware tag: 2 byte len followed by 2 byte ~len check (all LE) */
Arend van Spriel54991ad2010-11-23 14:06:24 +01001199 len = (u16) (pkt->len);
Stanislav Fomichev628f10b2011-02-20 21:43:32 +03001200 *(u16 *) frame = cpu_to_le16(len);
1201 *(((u16 *) frame) + 1) = cpu_to_le16(~len);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001202
1203 /* Software tag: channel, sequence number, data offset */
1204 swheader =
1205 ((chan << SDPCM_CHANNEL_SHIFT) & SDPCM_CHANNEL_MASK) | bus->tx_seq |
1206 (((pad +
1207 SDPCM_HDRLEN) << SDPCM_DOFFSET_SHIFT) & SDPCM_DOFFSET_MASK);
Stanislav Fomichev56dfe3c2011-02-20 21:43:33 +03001208
1209 put_unaligned_le32(swheader, frame + SDPCM_FRAMETAG_LEN);
1210 put_unaligned_le32(0, frame + SDPCM_FRAMETAG_LEN + sizeof(swheader));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001211
1212#ifdef DHD_DEBUG
Arend van Spriel54991ad2010-11-23 14:06:24 +01001213 tx_packets[pkt->priority]++;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001214 if (DHD_BYTES_ON() &&
1215 (((DHD_CTL_ON() && (chan == SDPCM_CONTROL_CHANNEL)) ||
1216 (DHD_DATA_ON() && (chan != SDPCM_CONTROL_CHANNEL))))) {
Arend van Spriel34227312011-05-10 22:25:32 +02001217 printk(KERN_DEBUG "Tx Frame:\n");
1218 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, frame, len);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001219 } else if (DHD_HDRS_ON()) {
Arend van Spriel34227312011-05-10 22:25:32 +02001220 printk(KERN_DEBUG "TxHdr:\n");
1221 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
1222 frame, min_t(u16, len, 16));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001223 }
1224#endif
1225
1226 /* Raise len to next SDIO block to eliminate tail command */
1227 if (bus->roundup && bus->blocksize && (len > bus->blocksize)) {
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07001228 u16 pad = bus->blocksize - (len % bus->blocksize);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001229 if ((pad <= bus->roundup) && (pad < bus->blocksize))
1230#ifdef NOTUSED
Arend van Spriel3be727c2010-11-23 22:20:30 +01001231 if (pad <= skb_tailroom(pkt))
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001232#endif /* NOTUSED */
1233 len += pad;
1234 } else if (len % DHD_SDALIGN) {
1235 len += DHD_SDALIGN - (len % DHD_SDALIGN);
1236 }
1237
1238 /* Some controllers have trouble with odd bytes -- round to even */
1239 if (forcealign && (len & (ALIGNMENT - 1))) {
1240#ifdef NOTUSED
Arend van Spriel3be727c2010-11-23 22:20:30 +01001241 if (skb_tailroom(pkt))
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001242#endif
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07001243 len = roundup(len, ALIGNMENT);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001244#ifdef NOTUSED
1245 else
1246 DHD_ERROR(("%s: sending unrounded %d-byte packet\n",
1247 __func__, len));
1248#endif
1249 }
1250
1251 do {
1252 ret =
1253 dhd_bcmsdh_send_buf(bus, bcmsdh_cur_sbwad(sdh), SDIO_FUNC_2,
1254 F2SYNC, frame, len, pkt, NULL, NULL);
1255 bus->f2txdata++;
Roland Vossenb7ef2a92011-05-03 11:35:02 +02001256 ASSERT(ret != -BCME_PENDING);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001257
1258 if (ret < 0) {
1259 /* On failure, abort the command
1260 and terminate the frame */
1261 DHD_INFO(("%s: sdio error %d, abort command and "
1262 "terminate frame.\n", __func__, ret));
1263 bus->tx_sderrs++;
1264
1265 bcmsdh_abort(sdh, SDIO_FUNC_2);
1266 bcmsdh_cfg_write(sdh, SDIO_FUNC_1,
1267 SBSDIO_FUNC1_FRAMECTRL, SFC_WF_TERM,
1268 NULL);
1269 bus->f1regdata++;
1270
1271 for (i = 0; i < 3; i++) {
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001272 u8 hi, lo;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001273 hi = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
1274 SBSDIO_FUNC1_WFRAMEBCHI,
1275 NULL);
1276 lo = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
1277 SBSDIO_FUNC1_WFRAMEBCLO,
1278 NULL);
1279 bus->f1regdata += 2;
1280 if ((hi == 0) && (lo == 0))
1281 break;
1282 }
1283
1284 }
1285 if (ret == 0)
1286 bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQUENCE_WRAP;
1287
1288 } while ((ret < 0) && retrydata && retries++ < TXRETRIES);
1289
1290done:
1291 /* restore pkt buffer pointer before calling tx complete routine */
Arend van Sprielc303ecb2010-11-18 20:46:43 +01001292 skb_pull(pkt, SDPCM_HDRLEN + pad);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001293 dhd_os_sdunlock(bus->dhd);
1294 dhd_txcomplete(bus->dhd, pkt, ret != 0);
1295 dhd_os_sdlock(bus->dhd);
1296
1297 if (free_pkt)
Roland Vossen67ad48b2011-06-01 13:45:51 +02001298 brcmu_pkt_buf_free_skb(pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001299
1300 return ret;
1301}
1302
Arend van Sprielc26b1372010-11-23 14:06:23 +01001303int dhd_bus_txdata(struct dhd_bus *bus, struct sk_buff *pkt)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001304{
Roland Vossenb74ac122011-05-03 11:35:20 +02001305 int ret = -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001306 uint datalen, prec;
1307
1308 DHD_TRACE(("%s: Enter\n", __func__));
1309
Arend van Spriel54991ad2010-11-23 14:06:24 +01001310 datalen = pkt->len;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001311
1312#ifdef SDTEST
1313 /* Push the test header if doing loopback */
1314 if (bus->ext_loop) {
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001315 u8 *data;
Arend van Sprielc303ecb2010-11-18 20:46:43 +01001316 skb_push(pkt, SDPCM_TEST_HDRLEN);
Arend van Spriel54991ad2010-11-23 14:06:24 +01001317 data = pkt->data;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001318 *data++ = SDPCM_TEST_ECHOREQ;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001319 *data++ = (u8) bus->loopid++;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001320 *data++ = (datalen >> 0);
1321 *data++ = (datalen >> 8);
1322 datalen += SDPCM_TEST_HDRLEN;
1323 }
1324#endif /* SDTEST */
1325
1326 /* Add space for the header */
Arend van Sprielc303ecb2010-11-18 20:46:43 +01001327 skb_push(pkt, SDPCM_HDRLEN);
Arend van Spriel54991ad2010-11-23 14:06:24 +01001328 ASSERT(IS_ALIGNED((unsigned long)(pkt->data), 2));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001329
Arend van Spriel54991ad2010-11-23 14:06:24 +01001330 prec = PRIO2PREC((pkt->priority & PRIOMASK));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001331
1332 /* Check for existing queue, current flow-control,
1333 pending event, or pending clock */
1334 if (dhd_deferred_tx || bus->fcstate || pktq_len(&bus->txq)
1335 || bus->dpc_sched || (!DATAOK(bus))
1336 || (bus->flowcontrol & NBITVAL(prec))
1337 || (bus->clkstate != CLK_AVAIL)) {
1338 DHD_TRACE(("%s: deferring pktq len %d\n", __func__,
1339 pktq_len(&bus->txq)));
1340 bus->fcqueued++;
1341
1342 /* Priority based enq */
1343 dhd_os_sdlock_txq(bus->dhd);
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001344 if (dhd_prec_enq(bus->dhd, &bus->txq, pkt, prec) == false) {
Arend van Sprielc303ecb2010-11-18 20:46:43 +01001345 skb_pull(pkt, SDPCM_HDRLEN);
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001346 dhd_txcomplete(bus->dhd, pkt, false);
Roland Vossen67ad48b2011-06-01 13:45:51 +02001347 brcmu_pkt_buf_free_skb(pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001348 DHD_ERROR(("%s: out of bus->txq !!!\n", __func__));
Roland Vossene10d82d2011-05-03 11:35:19 +02001349 ret = -ENOSR;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001350 } else {
Roland Vossena1c5ad82011-04-11 15:16:24 +02001351 ret = 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001352 }
1353 dhd_os_sdunlock_txq(bus->dhd);
1354
Grant Grundler7c316072011-03-09 15:04:15 -08001355 if (pktq_len(&bus->txq) >= TXHI)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001356 dhd_txflowcontrol(bus->dhd, 0, ON);
1357
1358#ifdef DHD_DEBUG
1359 if (pktq_plen(&bus->txq, prec) > qcount[prec])
1360 qcount[prec] = pktq_plen(&bus->txq, prec);
1361#endif
1362 /* Schedule DPC if needed to send queued packet(s) */
1363 if (dhd_deferred_tx && !bus->dpc_sched) {
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001364 bus->dpc_sched = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001365 dhd_sched_dpc(bus->dhd);
1366 }
1367 } else {
1368 /* Lock: we're about to use shared data/code (and SDIO) */
1369 dhd_os_sdlock(bus->dhd);
1370
1371 /* Otherwise, send it now */
1372 BUS_WAKE(bus);
1373 /* Make sure back plane ht clk is on, no pending allowed */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001374 dhdsdio_clkctl(bus, CLK_AVAIL, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001375
1376#ifndef SDTEST
1377 DHD_TRACE(("%s: calling txpkt\n", __func__));
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001378 ret = dhdsdio_txpkt(bus, pkt, SDPCM_DATA_CHANNEL, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001379#else
1380 ret = dhdsdio_txpkt(bus, pkt,
1381 (bus->ext_loop ? SDPCM_TEST_CHANNEL :
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001382 SDPCM_DATA_CHANNEL), true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001383#endif
1384 if (ret)
1385 bus->dhd->tx_errors++;
1386 else
1387 bus->dhd->dstats.tx_bytes += datalen;
1388
1389 if ((bus->idletime == DHD_IDLE_IMMEDIATE) && !bus->dpc_sched) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001390 bus->activity = false;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001391 dhdsdio_clkctl(bus, CLK_NONE, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001392 }
1393
1394 dhd_os_sdunlock(bus->dhd);
1395 }
1396
1397 return ret;
1398}
1399
1400static uint dhdsdio_sendfromq(dhd_bus_t *bus, uint maxframes)
1401{
Arend van Sprielc26b1372010-11-23 14:06:23 +01001402 struct sk_buff *pkt;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07001403 u32 intstatus = 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001404 uint retries = 0;
1405 int ret = 0, prec_out;
1406 uint cnt = 0;
1407 uint datalen;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001408 u8 tx_prec_map;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001409
1410 dhd_pub_t *dhd = bus->dhd;
Franky Lin597600a2011-06-01 13:45:39 +02001411 struct sdpcmd_regs *regs = bus->regs;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001412
1413 DHD_TRACE(("%s: Enter\n", __func__));
1414
1415 tx_prec_map = ~bus->flowcontrol;
1416
1417 /* Send frames until the limit or some other event */
1418 for (cnt = 0; (cnt < maxframes) && DATAOK(bus); cnt++) {
1419 dhd_os_sdlock_txq(bus->dhd);
Roland Vossen67ad48b2011-06-01 13:45:51 +02001420 pkt = brcmu_pktq_mdeq(&bus->txq, tx_prec_map, &prec_out);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001421 if (pkt == NULL) {
1422 dhd_os_sdunlock_txq(bus->dhd);
1423 break;
1424 }
1425 dhd_os_sdunlock_txq(bus->dhd);
Arend van Spriel54991ad2010-11-23 14:06:24 +01001426 datalen = pkt->len - SDPCM_HDRLEN;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001427
1428#ifndef SDTEST
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001429 ret = dhdsdio_txpkt(bus, pkt, SDPCM_DATA_CHANNEL, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001430#else
1431 ret = dhdsdio_txpkt(bus, pkt,
1432 (bus->ext_loop ? SDPCM_TEST_CHANNEL :
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001433 SDPCM_DATA_CHANNEL), true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001434#endif
1435 if (ret)
1436 bus->dhd->tx_errors++;
1437 else
1438 bus->dhd->dstats.tx_bytes += datalen;
1439
1440 /* In poll mode, need to check for other events */
1441 if (!bus->intr && cnt) {
1442 /* Check device status, signal pending interrupt */
1443 R_SDREG(intstatus, &regs->intstatus, retries);
1444 bus->f2txdata++;
1445 if (bcmsdh_regfail(bus->sdh))
1446 break;
1447 if (intstatus & bus->hostintmask)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001448 bus->ipend = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001449 }
1450 }
1451
1452 /* Deflow-control stack if needed */
Grant Grundler7c316072011-03-09 15:04:15 -08001453 if (dhd->up && (dhd->busstate == DHD_BUS_DATA) &&
Grant Grundler26a71a42011-03-09 10:41:25 -08001454 dhd->txoff && (pktq_len(&bus->txq) < TXLOW))
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001455 dhd_txflowcontrol(dhd, 0, OFF);
1456
1457 return cnt;
1458}
1459
Greg Kroah-Hartman580a0bd2010-10-05 11:09:48 -07001460int dhd_bus_txctl(struct dhd_bus *bus, unsigned char *msg, uint msglen)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001461{
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001462 u8 *frame;
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07001463 u16 len;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07001464 u32 swheader;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001465 uint retries = 0;
1466 bcmsdh_info_t *sdh = bus->sdh;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001467 u8 doff = 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001468 int ret = -1;
1469 int i;
1470
1471 DHD_TRACE(("%s: Enter\n", __func__));
1472
1473 if (bus->dhd->dongle_reset)
1474 return -EIO;
1475
1476 /* Back the pointer to make a room for bus header */
1477 frame = msg - SDPCM_HDRLEN;
1478 len = (msglen += SDPCM_HDRLEN);
1479
1480 /* Add alignment padding (optional for ctl frames) */
1481 if (dhd_alignctl) {
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07001482 doff = ((unsigned long)frame % DHD_SDALIGN);
Jason Cooper9b890322010-09-30 15:15:39 -04001483 if (doff) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001484 frame -= doff;
1485 len += doff;
1486 msglen += doff;
Brett Rudley9249ede2010-11-30 20:09:49 -08001487 memset(frame, 0, doff + SDPCM_HDRLEN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001488 }
1489 ASSERT(doff < DHD_SDALIGN);
1490 }
1491 doff += SDPCM_HDRLEN;
1492
1493 /* Round send length to next SDIO block */
1494 if (bus->roundup && bus->blocksize && (len > bus->blocksize)) {
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07001495 u16 pad = bus->blocksize - (len % bus->blocksize);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001496 if ((pad <= bus->roundup) && (pad < bus->blocksize))
1497 len += pad;
1498 } else if (len % DHD_SDALIGN) {
1499 len += DHD_SDALIGN - (len % DHD_SDALIGN);
1500 }
1501
1502 /* Satisfy length-alignment requirements */
1503 if (forcealign && (len & (ALIGNMENT - 1)))
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07001504 len = roundup(len, ALIGNMENT);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001505
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07001506 ASSERT(IS_ALIGNED((unsigned long)frame, 2));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001507
1508 /* Need to lock here to protect txseq and SDIO tx calls */
1509 dhd_os_sdlock(bus->dhd);
1510
1511 BUS_WAKE(bus);
1512
1513 /* Make sure backplane clock is on */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001514 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001515
1516 /* Hardware tag: 2 byte len followed by 2 byte ~len check (all LE) */
Stanislav Fomichev628f10b2011-02-20 21:43:32 +03001517 *(u16 *) frame = cpu_to_le16((u16) msglen);
1518 *(((u16 *) frame) + 1) = cpu_to_le16(~msglen);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001519
1520 /* Software tag: channel, sequence number, data offset */
1521 swheader =
1522 ((SDPCM_CONTROL_CHANNEL << SDPCM_CHANNEL_SHIFT) &
1523 SDPCM_CHANNEL_MASK)
1524 | bus->tx_seq | ((doff << SDPCM_DOFFSET_SHIFT) &
1525 SDPCM_DOFFSET_MASK);
Stanislav Fomichev56dfe3c2011-02-20 21:43:33 +03001526 put_unaligned_le32(swheader, frame + SDPCM_FRAMETAG_LEN);
1527 put_unaligned_le32(0, frame + SDPCM_FRAMETAG_LEN + sizeof(swheader));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001528
1529 if (!DATAOK(bus)) {
1530 DHD_INFO(("%s: No bus credit bus->tx_max %d, bus->tx_seq %d\n",
1531 __func__, bus->tx_max, bus->tx_seq));
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001532 bus->ctrl_frame_stat = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001533 /* Send from dpc */
1534 bus->ctrl_frame_buf = frame;
1535 bus->ctrl_frame_len = len;
1536
1537 dhd_wait_for_event(bus->dhd, &bus->ctrl_frame_stat);
1538
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001539 if (bus->ctrl_frame_stat == false) {
1540 DHD_INFO(("%s: ctrl_frame_stat == false\n", __func__));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001541 ret = 0;
1542 } else {
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001543 DHD_INFO(("%s: ctrl_frame_stat == true\n", __func__));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001544 ret = -1;
1545 }
1546 }
1547
1548 if (ret == -1) {
1549#ifdef DHD_DEBUG
Arend van Spriel34227312011-05-10 22:25:32 +02001550 if (DHD_BYTES_ON() && DHD_CTL_ON()) {
1551 printk(KERN_DEBUG "Tx Frame:\n");
1552 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
1553 frame, len);
1554 } else if (DHD_HDRS_ON()) {
1555 printk(KERN_DEBUG "TxHdr:\n");
1556 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
1557 frame, min_t(u16, len, 16));
1558 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001559#endif
1560
1561 do {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001562 bus->ctrl_frame_stat = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001563 ret =
1564 dhd_bcmsdh_send_buf(bus, bcmsdh_cur_sbwad(sdh),
1565 SDIO_FUNC_2, F2SYNC, frame, len,
1566 NULL, NULL, NULL);
1567
Roland Vossenb7ef2a92011-05-03 11:35:02 +02001568 ASSERT(ret != -BCME_PENDING);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001569
1570 if (ret < 0) {
1571 /* On failure, abort the command and
1572 terminate the frame */
1573 DHD_INFO(("%s: sdio error %d, abort command and terminate frame.\n",
1574 __func__, ret));
1575 bus->tx_sderrs++;
1576
1577 bcmsdh_abort(sdh, SDIO_FUNC_2);
1578
1579 bcmsdh_cfg_write(sdh, SDIO_FUNC_1,
1580 SBSDIO_FUNC1_FRAMECTRL,
1581 SFC_WF_TERM, NULL);
1582 bus->f1regdata++;
1583
1584 for (i = 0; i < 3; i++) {
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001585 u8 hi, lo;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001586 hi = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
1587 SBSDIO_FUNC1_WFRAMEBCHI,
1588 NULL);
1589 lo = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
1590 SBSDIO_FUNC1_WFRAMEBCLO,
1591 NULL);
1592 bus->f1regdata += 2;
1593 if ((hi == 0) && (lo == 0))
1594 break;
1595 }
1596
1597 }
1598 if (ret == 0) {
1599 bus->tx_seq =
1600 (bus->tx_seq + 1) % SDPCM_SEQUENCE_WRAP;
1601 }
1602 } while ((ret < 0) && retries++ < TXRETRIES);
1603 }
1604
1605 if ((bus->idletime == DHD_IDLE_IMMEDIATE) && !bus->dpc_sched) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001606 bus->activity = false;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001607 dhdsdio_clkctl(bus, CLK_NONE, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001608 }
1609
1610 dhd_os_sdunlock(bus->dhd);
1611
1612 if (ret)
1613 bus->dhd->tx_ctlerrs++;
1614 else
1615 bus->dhd->tx_ctlpkts++;
1616
1617 return ret ? -EIO : 0;
1618}
1619
Greg Kroah-Hartman580a0bd2010-10-05 11:09:48 -07001620int dhd_bus_rxctl(struct dhd_bus *bus, unsigned char *msg, uint msglen)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001621{
1622 int timeleft;
1623 uint rxlen = 0;
1624 bool pending;
1625
1626 DHD_TRACE(("%s: Enter\n", __func__));
1627
1628 if (bus->dhd->dongle_reset)
1629 return -EIO;
1630
1631 /* Wait until control frame is available */
1632 timeleft = dhd_os_ioctl_resp_wait(bus->dhd, &bus->rxlen, &pending);
1633
1634 dhd_os_sdlock(bus->dhd);
1635 rxlen = bus->rxlen;
Stanislav Fomichev02160692011-02-15 01:05:10 +03001636 memcpy(msg, bus->rxctl, min(msglen, rxlen));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001637 bus->rxlen = 0;
1638 dhd_os_sdunlock(bus->dhd);
1639
1640 if (rxlen) {
1641 DHD_CTL(("%s: resumed on rxctl frame, got %d expected %d\n",
1642 __func__, rxlen, msglen));
1643 } else if (timeleft == 0) {
1644 DHD_ERROR(("%s: resumed on timeout\n", __func__));
1645#ifdef DHD_DEBUG
1646 dhd_os_sdlock(bus->dhd);
1647 dhdsdio_checkdied(bus, NULL, 0);
1648 dhd_os_sdunlock(bus->dhd);
1649#endif /* DHD_DEBUG */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001650 } else if (pending == true) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001651 DHD_CTL(("%s: cancelled\n", __func__));
1652 return -ERESTARTSYS;
1653 } else {
1654 DHD_CTL(("%s: resumed for unknown reason?\n", __func__));
1655#ifdef DHD_DEBUG
1656 dhd_os_sdlock(bus->dhd);
1657 dhdsdio_checkdied(bus, NULL, 0);
1658 dhd_os_sdunlock(bus->dhd);
1659#endif /* DHD_DEBUG */
1660 }
1661
1662 if (rxlen)
1663 bus->dhd->rx_ctlpkts++;
1664 else
1665 bus->dhd->rx_ctlerrs++;
1666
Jason Coopere9887c92010-10-06 10:08:02 -04001667 return rxlen ? (int)rxlen : -ETIMEDOUT;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001668}
1669
1670/* IOVar table */
1671enum {
1672 IOV_INTR = 1,
1673 IOV_POLLRATE,
1674 IOV_SDREG,
1675 IOV_SBREG,
1676 IOV_SDCIS,
1677 IOV_MEMBYTES,
1678 IOV_MEMSIZE,
1679#ifdef DHD_DEBUG
1680 IOV_CHECKDIED,
1681#endif
1682 IOV_DOWNLOAD,
1683 IOV_FORCEEVEN,
1684 IOV_SDIOD_DRIVE,
1685 IOV_READAHEAD,
1686 IOV_SDRXCHAIN,
1687 IOV_ALIGNCTL,
1688 IOV_SDALIGN,
1689 IOV_DEVRESET,
1690 IOV_CPU,
1691#ifdef SDTEST
1692 IOV_PKTGEN,
1693 IOV_EXTLOOP,
1694#endif /* SDTEST */
1695 IOV_SPROM,
1696 IOV_TXBOUND,
1697 IOV_RXBOUND,
1698 IOV_TXMINMAX,
1699 IOV_IDLETIME,
1700 IOV_IDLECLOCK,
1701 IOV_SD1IDLE,
1702 IOV_SLEEP,
1703 IOV_VARS
1704};
1705
Roland Vossen67ad48b2011-06-01 13:45:51 +02001706const struct brcmu_iovar dhdsdio_iovars[] = {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001707 {"intr", IOV_INTR, 0, IOVT_BOOL, 0},
1708 {"sleep", IOV_SLEEP, 0, IOVT_BOOL, 0},
1709 {"pollrate", IOV_POLLRATE, 0, IOVT_UINT32, 0},
1710 {"idletime", IOV_IDLETIME, 0, IOVT_INT32, 0},
1711 {"idleclock", IOV_IDLECLOCK, 0, IOVT_INT32, 0},
1712 {"sd1idle", IOV_SD1IDLE, 0, IOVT_BOOL, 0},
1713 {"membytes", IOV_MEMBYTES, 0, IOVT_BUFFER, 2 * sizeof(int)},
1714 {"memsize", IOV_MEMSIZE, 0, IOVT_UINT32, 0},
1715 {"download", IOV_DOWNLOAD, 0, IOVT_BOOL, 0},
1716 {"vars", IOV_VARS, 0, IOVT_BUFFER, 0},
1717 {"sdiod_drive", IOV_SDIOD_DRIVE, 0, IOVT_UINT32, 0},
1718 {"readahead", IOV_READAHEAD, 0, IOVT_BOOL, 0},
1719 {"sdrxchain", IOV_SDRXCHAIN, 0, IOVT_BOOL, 0},
1720 {"alignctl", IOV_ALIGNCTL, 0, IOVT_BOOL, 0},
1721 {"sdalign", IOV_SDALIGN, 0, IOVT_BOOL, 0},
1722 {"devreset", IOV_DEVRESET, 0, IOVT_BOOL, 0},
1723#ifdef DHD_DEBUG
1724 {"sdreg", IOV_SDREG, 0, IOVT_BUFFER, sizeof(sdreg_t)}
1725 ,
1726 {"sbreg", IOV_SBREG, 0, IOVT_BUFFER, sizeof(sdreg_t)}
1727 ,
1728 {"sd_cis", IOV_SDCIS, 0, IOVT_BUFFER, DHD_IOCTL_MAXLEN}
1729 ,
1730 {"forcealign", IOV_FORCEEVEN, 0, IOVT_BOOL, 0}
1731 ,
1732 {"txbound", IOV_TXBOUND, 0, IOVT_UINT32, 0}
1733 ,
1734 {"rxbound", IOV_RXBOUND, 0, IOVT_UINT32, 0}
1735 ,
1736 {"txminmax", IOV_TXMINMAX, 0, IOVT_UINT32, 0}
1737 ,
1738 {"cpu", IOV_CPU, 0, IOVT_BOOL, 0}
1739 ,
1740#ifdef DHD_DEBUG
1741 {"checkdied", IOV_CHECKDIED, 0, IOVT_BUFFER, 0}
1742 ,
1743#endif /* DHD_DEBUG */
1744#endif /* DHD_DEBUG */
1745#ifdef SDTEST
1746 {"extloop", IOV_EXTLOOP, 0, IOVT_BOOL, 0}
1747 ,
1748 {"pktgen", IOV_PKTGEN, 0, IOVT_BUFFER, sizeof(dhd_pktgen_t)}
1749 ,
1750#endif /* SDTEST */
1751
1752 {NULL, 0, 0, 0, 0}
1753};
1754
1755static void
Roland Vossen67ad48b2011-06-01 13:45:51 +02001756dhd_dump_pct(struct brcmu_strbuf *strbuf, char *desc, uint num, uint div)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001757{
1758 uint q1, q2;
1759
1760 if (!div) {
Roland Vossen67ad48b2011-06-01 13:45:51 +02001761 brcmu_bprintf(strbuf, "%s N/A", desc);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001762 } else {
1763 q1 = num / div;
1764 q2 = (100 * (num - (q1 * div))) / div;
Roland Vossen67ad48b2011-06-01 13:45:51 +02001765 brcmu_bprintf(strbuf, "%s %d.%02d", desc, q1, q2);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001766 }
1767}
1768
Roland Vossen67ad48b2011-06-01 13:45:51 +02001769void dhd_bus_dump(dhd_pub_t *dhdp, struct brcmu_strbuf *strbuf)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001770{
1771 dhd_bus_t *bus = dhdp->bus;
1772
Roland Vossen67ad48b2011-06-01 13:45:51 +02001773 brcmu_bprintf(strbuf, "Bus SDIO structure:\n");
1774 brcmu_bprintf(strbuf,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001775 "hostintmask 0x%08x intstatus 0x%08x sdpcm_ver %d\n",
1776 bus->hostintmask, bus->intstatus, bus->sdpcm_ver);
Roland Vossen67ad48b2011-06-01 13:45:51 +02001777 brcmu_bprintf(strbuf,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001778 "fcstate %d qlen %d tx_seq %d, max %d, rxskip %d rxlen %d rx_seq %d\n",
1779 bus->fcstate, pktq_len(&bus->txq), bus->tx_seq, bus->tx_max,
1780 bus->rxskip, bus->rxlen, bus->rx_seq);
Roland Vossen67ad48b2011-06-01 13:45:51 +02001781 brcmu_bprintf(strbuf, "intr %d intrcount %d lastintrs %d spurious %d\n",
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001782 bus->intr, bus->intrcount, bus->lastintrs, bus->spurious);
Roland Vossen67ad48b2011-06-01 13:45:51 +02001783 brcmu_bprintf(strbuf, "pollrate %d pollcnt %d regfails %d\n",
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001784 bus->pollrate, bus->pollcnt, bus->regfails);
1785
Roland Vossen67ad48b2011-06-01 13:45:51 +02001786 brcmu_bprintf(strbuf, "\nAdditional counters:\n");
1787 brcmu_bprintf(strbuf,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001788 "tx_sderrs %d fcqueued %d rxrtx %d rx_toolong %d rxc_errors %d\n",
1789 bus->tx_sderrs, bus->fcqueued, bus->rxrtx, bus->rx_toolong,
1790 bus->rxc_errors);
Roland Vossen67ad48b2011-06-01 13:45:51 +02001791 brcmu_bprintf(strbuf, "rx_hdrfail %d badhdr %d badseq %d\n",
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001792 bus->rx_hdrfail, bus->rx_badhdr, bus->rx_badseq);
Roland Vossen67ad48b2011-06-01 13:45:51 +02001793 brcmu_bprintf(strbuf, "fc_rcvd %d, fc_xoff %d, fc_xon %d\n",
1794 bus->fc_rcvd, bus->fc_xoff, bus->fc_xon);
1795 brcmu_bprintf(strbuf, "rxglomfail %d, rxglomframes %d, rxglompkts %d\n",
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001796 bus->rxglomfail, bus->rxglomframes, bus->rxglompkts);
Roland Vossen67ad48b2011-06-01 13:45:51 +02001797 brcmu_bprintf(strbuf, "f2rx (hdrs/data) %d (%d/%d), f2tx %d f1regs"
1798 " %d\n",
1799 (bus->f2rxhdrs + bus->f2rxdata), bus->f2rxhdrs,
1800 bus->f2rxdata, bus->f2txdata, bus->f1regdata);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001801 {
1802 dhd_dump_pct(strbuf, "\nRx: pkts/f2rd", bus->dhd->rx_packets,
1803 (bus->f2rxhdrs + bus->f2rxdata));
1804 dhd_dump_pct(strbuf, ", pkts/f1sd", bus->dhd->rx_packets,
1805 bus->f1regdata);
1806 dhd_dump_pct(strbuf, ", pkts/sd", bus->dhd->rx_packets,
1807 (bus->f2rxhdrs + bus->f2rxdata + bus->f1regdata));
1808 dhd_dump_pct(strbuf, ", pkts/int", bus->dhd->rx_packets,
1809 bus->intrcount);
Roland Vossen67ad48b2011-06-01 13:45:51 +02001810 brcmu_bprintf(strbuf, "\n");
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001811
1812 dhd_dump_pct(strbuf, "Rx: glom pct", (100 * bus->rxglompkts),
1813 bus->dhd->rx_packets);
1814 dhd_dump_pct(strbuf, ", pkts/glom", bus->rxglompkts,
1815 bus->rxglomframes);
Roland Vossen67ad48b2011-06-01 13:45:51 +02001816 brcmu_bprintf(strbuf, "\n");
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001817
1818 dhd_dump_pct(strbuf, "Tx: pkts/f2wr", bus->dhd->tx_packets,
1819 bus->f2txdata);
1820 dhd_dump_pct(strbuf, ", pkts/f1sd", bus->dhd->tx_packets,
1821 bus->f1regdata);
1822 dhd_dump_pct(strbuf, ", pkts/sd", bus->dhd->tx_packets,
1823 (bus->f2txdata + bus->f1regdata));
1824 dhd_dump_pct(strbuf, ", pkts/int", bus->dhd->tx_packets,
1825 bus->intrcount);
Roland Vossen67ad48b2011-06-01 13:45:51 +02001826 brcmu_bprintf(strbuf, "\n");
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001827
1828 dhd_dump_pct(strbuf, "Total: pkts/f2rw",
1829 (bus->dhd->tx_packets + bus->dhd->rx_packets),
1830 (bus->f2txdata + bus->f2rxhdrs + bus->f2rxdata));
1831 dhd_dump_pct(strbuf, ", pkts/f1sd",
1832 (bus->dhd->tx_packets + bus->dhd->rx_packets),
1833 bus->f1regdata);
1834 dhd_dump_pct(strbuf, ", pkts/sd",
1835 (bus->dhd->tx_packets + bus->dhd->rx_packets),
1836 (bus->f2txdata + bus->f2rxhdrs + bus->f2rxdata +
1837 bus->f1regdata));
1838 dhd_dump_pct(strbuf, ", pkts/int",
1839 (bus->dhd->tx_packets + bus->dhd->rx_packets),
1840 bus->intrcount);
Roland Vossen67ad48b2011-06-01 13:45:51 +02001841 brcmu_bprintf(strbuf, "\n\n");
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001842 }
1843
1844#ifdef SDTEST
1845 if (bus->pktgen_count) {
Roland Vossen67ad48b2011-06-01 13:45:51 +02001846 brcmu_bprintf(strbuf, "pktgen config and count:\n");
1847 brcmu_bprintf(strbuf,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001848 "freq %d count %d print %d total %d min %d len %d\n",
1849 bus->pktgen_freq, bus->pktgen_count,
1850 bus->pktgen_print, bus->pktgen_total,
1851 bus->pktgen_minlen, bus->pktgen_maxlen);
Roland Vossen67ad48b2011-06-01 13:45:51 +02001852 brcmu_bprintf(strbuf, "send attempts %d rcvd %d fail %d\n",
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001853 bus->pktgen_sent, bus->pktgen_rcvd,
1854 bus->pktgen_fail);
1855 }
1856#endif /* SDTEST */
1857#ifdef DHD_DEBUG
Roland Vossen67ad48b2011-06-01 13:45:51 +02001858 brcmu_bprintf(strbuf, "dpc_sched %d host interrupt%spending\n",
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001859 bus->dpc_sched,
1860 (bcmsdh_intr_pending(bus->sdh) ? " " : " not "));
Roland Vossen67ad48b2011-06-01 13:45:51 +02001861 brcmu_bprintf(strbuf, "blocksize %d roundup %d\n", bus->blocksize,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001862 bus->roundup);
1863#endif /* DHD_DEBUG */
Roland Vossen67ad48b2011-06-01 13:45:51 +02001864 brcmu_bprintf(strbuf,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001865 "clkstate %d activity %d idletime %d idlecount %d sleeping %d\n",
1866 bus->clkstate, bus->activity, bus->idletime, bus->idlecount,
1867 bus->sleeping);
1868}
1869
1870void dhd_bus_clearcounts(dhd_pub_t *dhdp)
1871{
1872 dhd_bus_t *bus = (dhd_bus_t *) dhdp->bus;
1873
1874 bus->intrcount = bus->lastintrs = bus->spurious = bus->regfails = 0;
1875 bus->rxrtx = bus->rx_toolong = bus->rxc_errors = 0;
1876 bus->rx_hdrfail = bus->rx_badhdr = bus->rx_badseq = 0;
1877 bus->tx_sderrs = bus->fc_rcvd = bus->fc_xoff = bus->fc_xon = 0;
1878 bus->rxglomfail = bus->rxglomframes = bus->rxglompkts = 0;
1879 bus->f2rxhdrs = bus->f2rxdata = bus->f2txdata = bus->f1regdata = 0;
1880}
1881
1882#ifdef SDTEST
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001883static int dhdsdio_pktgen_get(dhd_bus_t *bus, u8 *arg)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001884{
1885 dhd_pktgen_t pktgen;
1886
1887 pktgen.version = DHD_PKTGEN_VERSION;
1888 pktgen.freq = bus->pktgen_freq;
1889 pktgen.count = bus->pktgen_count;
1890 pktgen.print = bus->pktgen_print;
1891 pktgen.total = bus->pktgen_total;
1892 pktgen.minlen = bus->pktgen_minlen;
1893 pktgen.maxlen = bus->pktgen_maxlen;
1894 pktgen.numsent = bus->pktgen_sent;
1895 pktgen.numrcvd = bus->pktgen_rcvd;
1896 pktgen.numfail = bus->pktgen_fail;
1897 pktgen.mode = bus->pktgen_mode;
1898 pktgen.stop = bus->pktgen_stop;
1899
Stanislav Fomichev02160692011-02-15 01:05:10 +03001900 memcpy(arg, &pktgen, sizeof(pktgen));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001901
1902 return 0;
1903}
1904
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001905static int dhdsdio_pktgen_set(dhd_bus_t *bus, u8 *arg)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001906{
1907 dhd_pktgen_t pktgen;
1908 uint oldcnt, oldmode;
1909
Stanislav Fomichev02160692011-02-15 01:05:10 +03001910 memcpy(&pktgen, arg, sizeof(pktgen));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001911 if (pktgen.version != DHD_PKTGEN_VERSION)
Roland Vossene10d82d2011-05-03 11:35:19 +02001912 return -EINVAL;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001913
1914 oldcnt = bus->pktgen_count;
1915 oldmode = bus->pktgen_mode;
1916
1917 bus->pktgen_freq = pktgen.freq;
1918 bus->pktgen_count = pktgen.count;
1919 bus->pktgen_print = pktgen.print;
1920 bus->pktgen_total = pktgen.total;
1921 bus->pktgen_minlen = pktgen.minlen;
1922 bus->pktgen_maxlen = pktgen.maxlen;
1923 bus->pktgen_mode = pktgen.mode;
1924 bus->pktgen_stop = pktgen.stop;
1925
1926 bus->pktgen_tick = bus->pktgen_ptick = 0;
Greg Kroah-Hartman3ea2f4d2010-10-08 11:39:43 -07001927 bus->pktgen_len = max(bus->pktgen_len, bus->pktgen_minlen);
Greg Kroah-Hartman7068c2f2010-10-08 11:34:59 -07001928 bus->pktgen_len = min(bus->pktgen_len, bus->pktgen_maxlen);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001929
1930 /* Clear counts for a new pktgen (mode change, or was stopped) */
1931 if (bus->pktgen_count && (!oldcnt || oldmode != bus->pktgen_mode))
1932 bus->pktgen_sent = bus->pktgen_rcvd = bus->pktgen_fail = 0;
1933
1934 return 0;
1935}
1936#endif /* SDTEST */
1937
1938static int
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07001939dhdsdio_membytes(dhd_bus_t *bus, bool write, u32 address, u8 *data,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001940 uint size)
1941{
1942 int bcmerror = 0;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07001943 u32 sdaddr;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001944 uint dsize;
1945
1946 /* Determine initial transfer parameters */
1947 sdaddr = address & SBSDIO_SB_OFT_ADDR_MASK;
1948 if ((sdaddr + size) & SBSDIO_SBWINDOW_MASK)
1949 dsize = (SBSDIO_SB_OFT_ADDR_LIMIT - sdaddr);
1950 else
1951 dsize = size;
1952
1953 /* Set the backplane window to include the start address */
1954 bcmerror = dhdsdio_set_siaddr_window(bus, address);
1955 if (bcmerror) {
1956 DHD_ERROR(("%s: window change failed\n", __func__));
1957 goto xfer_done;
1958 }
1959
1960 /* Do the transfer(s) */
1961 while (size) {
1962 DHD_INFO(("%s: %s %d bytes at offset 0x%08x in window 0x%08x\n",
1963 __func__, (write ? "write" : "read"), dsize,
1964 sdaddr, (address & SBSDIO_SBWINDOW_MASK)));
1965 bcmerror =
1966 bcmsdh_rwdata(bus->sdh, write, sdaddr, data, dsize);
1967 if (bcmerror) {
1968 DHD_ERROR(("%s: membytes transfer failed\n", __func__));
1969 break;
1970 }
1971
1972 /* Adjust for next transfer (if any) */
1973 size -= dsize;
1974 if (size) {
1975 data += dsize;
1976 address += dsize;
1977 bcmerror = dhdsdio_set_siaddr_window(bus, address);
1978 if (bcmerror) {
1979 DHD_ERROR(("%s: window change failed\n",
1980 __func__));
1981 break;
1982 }
1983 sdaddr = 0;
Greg Kroah-Hartmanb61640d2010-10-08 12:37:39 -07001984 dsize = min_t(uint, SBSDIO_SB_OFT_ADDR_LIMIT, size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001985 }
1986 }
1987
1988xfer_done:
1989 /* Return the window to backplane enumeration space for core access */
1990 if (dhdsdio_set_siaddr_window(bus, bcmsdh_cur_sbwad(bus->sdh))) {
1991 DHD_ERROR(("%s: FAILED to set window back to 0x%x\n",
1992 __func__, bcmsdh_cur_sbwad(bus->sdh)));
1993 }
1994
1995 return bcmerror;
1996}
1997
1998#ifdef DHD_DEBUG
Franky Linb49b14d2011-06-01 13:45:37 +02001999static int dhdsdio_readshared(dhd_bus_t *bus, struct sdpcm_shared *sh)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002000{
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002001 u32 addr;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002002 int rv;
2003
2004 /* Read last word in memory to determine address of
2005 sdpcm_shared structure */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002006 rv = dhdsdio_membytes(bus, false, bus->ramsize - 4, (u8 *)&addr, 4);
Jason Cooper9b890322010-09-30 15:15:39 -04002007 if (rv < 0)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002008 return rv;
2009
Stanislav Fomichev628f10b2011-02-20 21:43:32 +03002010 addr = le32_to_cpu(addr);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002011
2012 DHD_INFO(("sdpcm_shared address 0x%08X\n", addr));
2013
2014 /*
2015 * Check if addr is valid.
2016 * NVRAM length at the end of memory should have been overwritten.
2017 */
2018 if (addr == 0 || ((~addr >> 16) & 0xffff) == (addr & 0xffff)) {
2019 DHD_ERROR(("%s: address (0x%08x) of sdpcm_shared invalid\n",
2020 __func__, addr));
Roland Vossenb74ac122011-05-03 11:35:20 +02002021 return -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002022 }
2023
Roland Vossen70963f92011-06-01 13:45:08 +02002024 /* Read rte_shared structure */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002025 rv = dhdsdio_membytes(bus, false, addr, (u8 *) sh,
Franky Linb49b14d2011-06-01 13:45:37 +02002026 sizeof(struct sdpcm_shared));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002027 if (rv < 0)
2028 return rv;
2029
2030 /* Endianness */
Stanislav Fomichev628f10b2011-02-20 21:43:32 +03002031 sh->flags = le32_to_cpu(sh->flags);
2032 sh->trap_addr = le32_to_cpu(sh->trap_addr);
2033 sh->assert_exp_addr = le32_to_cpu(sh->assert_exp_addr);
2034 sh->assert_file_addr = le32_to_cpu(sh->assert_file_addr);
2035 sh->assert_line = le32_to_cpu(sh->assert_line);
2036 sh->console_addr = le32_to_cpu(sh->console_addr);
2037 sh->msgtrace_addr = le32_to_cpu(sh->msgtrace_addr);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002038
2039 if ((sh->flags & SDPCM_SHARED_VERSION_MASK) != SDPCM_SHARED_VERSION) {
2040 DHD_ERROR(("%s: sdpcm_shared version %d in dhd "
2041 "is different than sdpcm_shared version %d in dongle\n",
2042 __func__, SDPCM_SHARED_VERSION,
2043 sh->flags & SDPCM_SHARED_VERSION_MASK));
Roland Vossenb74ac122011-05-03 11:35:20 +02002044 return -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002045 }
2046
Roland Vossena1c5ad82011-04-11 15:16:24 +02002047 return 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002048}
2049
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002050static int dhdsdio_checkdied(dhd_bus_t *bus, u8 *data, uint size)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002051{
2052 int bcmerror = 0;
2053 uint msize = 512;
2054 char *mbuffer = NULL;
2055 uint maxstrlen = 256;
2056 char *str = NULL;
2057 trap_t tr;
Franky Linb49b14d2011-06-01 13:45:37 +02002058 struct sdpcm_shared sdpcm_shared;
Roland Vossen67ad48b2011-06-01 13:45:51 +02002059 struct brcmu_strbuf strbuf;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002060
2061 DHD_TRACE(("%s: Enter\n", __func__));
2062
2063 if (data == NULL) {
2064 /*
2065 * Called after a rx ctrl timeout. "data" is NULL.
2066 * allocate memory to trace the trap or assert.
2067 */
2068 size = msize;
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02002069 mbuffer = data = kmalloc(msize, GFP_ATOMIC);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002070 if (mbuffer == NULL) {
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02002071 DHD_ERROR(("%s: kmalloc(%d) failed\n", __func__,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002072 msize));
Roland Vossene10d82d2011-05-03 11:35:19 +02002073 bcmerror = -ENOMEM;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002074 goto done;
2075 }
2076 }
2077
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02002078 str = kmalloc(maxstrlen, GFP_ATOMIC);
Jason Cooper9b890322010-09-30 15:15:39 -04002079 if (str == NULL) {
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02002080 DHD_ERROR(("%s: kmalloc(%d) failed\n", __func__, maxstrlen));
Roland Vossene10d82d2011-05-03 11:35:19 +02002081 bcmerror = -ENOMEM;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002082 goto done;
2083 }
2084
Jason Cooper9b890322010-09-30 15:15:39 -04002085 bcmerror = dhdsdio_readshared(bus, &sdpcm_shared);
2086 if (bcmerror < 0)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002087 goto done;
2088
Roland Vossen67ad48b2011-06-01 13:45:51 +02002089 brcmu_binit(&strbuf, data, size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002090
Roland Vossen67ad48b2011-06-01 13:45:51 +02002091 brcmu_bprintf(&strbuf,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002092 "msgtrace address : 0x%08X\nconsole address : 0x%08X\n",
2093 sdpcm_shared.msgtrace_addr, sdpcm_shared.console_addr);
2094
2095 if ((sdpcm_shared.flags & SDPCM_SHARED_ASSERT_BUILT) == 0) {
2096 /* NOTE: Misspelled assert is intentional - DO NOT FIX.
2097 * (Avoids conflict with real asserts for programmatic
2098 * parsing of output.)
2099 */
Roland Vossen67ad48b2011-06-01 13:45:51 +02002100 brcmu_bprintf(&strbuf, "Assrt not built in dongle\n");
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002101 }
2102
2103 if ((sdpcm_shared.flags & (SDPCM_SHARED_ASSERT | SDPCM_SHARED_TRAP)) ==
2104 0) {
2105 /* NOTE: Misspelled assert is intentional - DO NOT FIX.
2106 * (Avoids conflict with real asserts for programmatic
2107 * parsing of output.)
2108 */
Roland Vossen67ad48b2011-06-01 13:45:51 +02002109 brcmu_bprintf(&strbuf, "No trap%s in dongle",
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002110 (sdpcm_shared.flags & SDPCM_SHARED_ASSERT_BUILT)
2111 ? "/assrt" : "");
2112 } else {
2113 if (sdpcm_shared.flags & SDPCM_SHARED_ASSERT) {
2114 /* Download assert */
Roland Vossen67ad48b2011-06-01 13:45:51 +02002115 brcmu_bprintf(&strbuf, "Dongle assert");
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002116 if (sdpcm_shared.assert_exp_addr != 0) {
2117 str[0] = '\0';
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002118 bcmerror = dhdsdio_membytes(bus, false,
Jason Cooper9b890322010-09-30 15:15:39 -04002119 sdpcm_shared.assert_exp_addr,
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002120 (u8 *) str, maxstrlen);
Jason Cooper9b890322010-09-30 15:15:39 -04002121 if (bcmerror < 0)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002122 goto done;
2123
2124 str[maxstrlen - 1] = '\0';
Roland Vossen67ad48b2011-06-01 13:45:51 +02002125 brcmu_bprintf(&strbuf, " expr \"%s\"", str);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002126 }
2127
2128 if (sdpcm_shared.assert_file_addr != 0) {
2129 str[0] = '\0';
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002130 bcmerror = dhdsdio_membytes(bus, false,
Jason Cooper9b890322010-09-30 15:15:39 -04002131 sdpcm_shared.assert_file_addr,
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002132 (u8 *) str, maxstrlen);
Jason Cooper9b890322010-09-30 15:15:39 -04002133 if (bcmerror < 0)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002134 goto done;
2135
2136 str[maxstrlen - 1] = '\0';
Roland Vossen67ad48b2011-06-01 13:45:51 +02002137 brcmu_bprintf(&strbuf, " file \"%s\"", str);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002138 }
2139
Roland Vossen67ad48b2011-06-01 13:45:51 +02002140 brcmu_bprintf(&strbuf, " line %d ",
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002141 sdpcm_shared.assert_line);
2142 }
2143
2144 if (sdpcm_shared.flags & SDPCM_SHARED_TRAP) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002145 bcmerror = dhdsdio_membytes(bus, false,
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002146 sdpcm_shared.trap_addr, (u8 *)&tr,
Jason Cooper9b890322010-09-30 15:15:39 -04002147 sizeof(trap_t));
2148 if (bcmerror < 0)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002149 goto done;
2150
Roland Vossen67ad48b2011-06-01 13:45:51 +02002151 brcmu_bprintf(&strbuf,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002152 "Dongle trap type 0x%x @ epc 0x%x, cpsr 0x%x, spsr 0x%x, sp 0x%x,"
2153 "lp 0x%x, rpc 0x%x Trap offset 0x%x, "
2154 "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",
2155 tr.type, tr.epc, tr.cpsr, tr.spsr, tr.r13,
2156 tr.r14, tr.pc, sdpcm_shared.trap_addr,
2157 tr.r0, tr.r1, tr.r2, tr.r3, tr.r4, tr.r5,
2158 tr.r6, tr.r7);
2159 }
2160 }
2161
2162 if (sdpcm_shared.flags & (SDPCM_SHARED_ASSERT | SDPCM_SHARED_TRAP))
2163 DHD_ERROR(("%s: %s\n", __func__, strbuf.origbuf));
2164
2165#ifdef DHD_DEBUG
2166 if (sdpcm_shared.flags & SDPCM_SHARED_TRAP) {
2167 /* Mem dump to a file on device */
2168 dhdsdio_mem_dump(bus);
2169 }
2170#endif /* DHD_DEBUG */
2171
2172done:
Ilia Mirkin46d994b2011-03-13 00:28:56 -05002173 kfree(mbuffer);
2174 kfree(str);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002175
2176 return bcmerror;
2177}
2178
2179static int dhdsdio_mem_dump(dhd_bus_t *bus)
2180{
2181 int ret = 0;
2182 int size; /* Full mem size */
2183 int start = 0; /* Start address */
2184 int read_size = 0; /* Read size of each iteration */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002185 u8 *buf = NULL, *databuf = NULL;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002186
2187 /* Get full mem size */
2188 size = bus->ramsize;
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02002189 buf = kmalloc(size, GFP_ATOMIC);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002190 if (!buf) {
Arend van Spriel0bef7742011-02-10 12:03:44 +01002191 DHD_ERROR(("%s: Out of memory (%d bytes)\n", __func__, size));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002192 return -1;
2193 }
2194
2195 /* Read mem content */
Arend van Spriel0bef7742011-02-10 12:03:44 +01002196 printk(KERN_DEBUG "Dump dongle memory");
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002197 databuf = buf;
2198 while (size) {
Greg Kroah-Hartman7068c2f2010-10-08 11:34:59 -07002199 read_size = min(MEMBLOCK, size);
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002200 ret = dhdsdio_membytes(bus, false, start, databuf, read_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002201 if (ret) {
Arend van Spriel0bef7742011-02-10 12:03:44 +01002202 DHD_ERROR(("%s: Error membytes %d\n", __func__, ret));
Ilia Mirkin46d994b2011-03-13 00:28:56 -05002203 kfree(buf);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002204 return -1;
2205 }
Arend van Spriel0bef7742011-02-10 12:03:44 +01002206 printk(".");
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002207
2208 /* Decrement size and increment start address */
2209 size -= read_size;
2210 start += read_size;
2211 databuf += read_size;
2212 }
Arend van Spriel0bef7742011-02-10 12:03:44 +01002213 printk(KERN_DEBUG "Done\n");
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002214
2215 /* free buf before return !!! */
2216 if (write_to_file(bus->dhd, buf, bus->ramsize)) {
Arend van Spriel0bef7742011-02-10 12:03:44 +01002217 DHD_ERROR(("%s: Error writing to files\n", __func__));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002218 return -1;
2219 }
2220
2221 /* buf free handled in write_to_file, not here */
2222 return 0;
2223}
2224
2225#define CONSOLE_LINE_MAX 192
2226
2227static int dhdsdio_readconsole(dhd_bus_t *bus)
2228{
2229 dhd_console_t *c = &bus->console;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002230 u8 line[CONSOLE_LINE_MAX], ch;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002231 u32 n, idx, addr;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002232 int rv;
2233
2234 /* Don't do anything until FWREADY updates console address */
2235 if (bus->console_addr == 0)
2236 return 0;
2237
2238 /* Read console log struct */
Roland Vossen70963f92011-06-01 13:45:08 +02002239 addr = bus->console_addr + offsetof(rte_cons_t, log);
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002240 rv = dhdsdio_membytes(bus, false, addr, (u8 *)&c->log,
Jason Cooper9b890322010-09-30 15:15:39 -04002241 sizeof(c->log));
2242 if (rv < 0)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002243 return rv;
2244
2245 /* Allocate console buffer (one time only) */
2246 if (c->buf == NULL) {
Stanislav Fomichev628f10b2011-02-20 21:43:32 +03002247 c->bufsize = le32_to_cpu(c->log.buf_size);
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02002248 c->buf = kmalloc(c->bufsize, GFP_ATOMIC);
Jason Cooper9b890322010-09-30 15:15:39 -04002249 if (c->buf == NULL)
Roland Vossene10d82d2011-05-03 11:35:19 +02002250 return -ENOMEM;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002251 }
2252
Stanislav Fomichev628f10b2011-02-20 21:43:32 +03002253 idx = le32_to_cpu(c->log.idx);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002254
2255 /* Protect against corrupt value */
2256 if (idx > c->bufsize)
Roland Vossenb74ac122011-05-03 11:35:20 +02002257 return -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002258
2259 /* Skip reading the console buffer if the index pointer
2260 has not moved */
2261 if (idx == c->last)
Roland Vossena1c5ad82011-04-11 15:16:24 +02002262 return 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002263
2264 /* Read the console buffer */
Stanislav Fomichev628f10b2011-02-20 21:43:32 +03002265 addr = le32_to_cpu(c->log.buf);
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002266 rv = dhdsdio_membytes(bus, false, addr, c->buf, c->bufsize);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002267 if (rv < 0)
2268 return rv;
2269
2270 while (c->last != idx) {
2271 for (n = 0; n < CONSOLE_LINE_MAX - 2; n++) {
2272 if (c->last == idx) {
2273 /* This would output a partial line.
2274 * Instead, back up
2275 * the buffer pointer and output this
2276 * line next time around.
2277 */
2278 if (c->last >= n)
2279 c->last -= n;
2280 else
2281 c->last = c->bufsize - n;
2282 goto break2;
2283 }
2284 ch = c->buf[c->last];
2285 c->last = (c->last + 1) % c->bufsize;
2286 if (ch == '\n')
2287 break;
2288 line[n] = ch;
2289 }
2290
2291 if (n > 0) {
2292 if (line[n - 1] == '\r')
2293 n--;
2294 line[n] = 0;
Arend van Spriel0bef7742011-02-10 12:03:44 +01002295 printk(KERN_DEBUG "CONSOLE: %s\n", line);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002296 }
2297 }
2298break2:
2299
Roland Vossena1c5ad82011-04-11 15:16:24 +02002300 return 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002301}
2302#endif /* DHD_DEBUG */
2303
2304int dhdsdio_downloadvars(dhd_bus_t *bus, void *arg, int len)
2305{
Roland Vossena1c5ad82011-04-11 15:16:24 +02002306 int bcmerror = 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002307
2308 DHD_TRACE(("%s: Enter\n", __func__));
2309
2310 /* Basic sanity checks */
2311 if (bus->dhd->up) {
Roland Vossenb74ac122011-05-03 11:35:20 +02002312 bcmerror = -EISCONN;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002313 goto err;
2314 }
2315 if (!len) {
Roland Vossene10d82d2011-05-03 11:35:19 +02002316 bcmerror = -EOVERFLOW;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002317 goto err;
2318 }
2319
2320 /* Free the old ones and replace with passed variables */
Ilia Mirkin46d994b2011-03-13 00:28:56 -05002321 kfree(bus->vars);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002322
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02002323 bus->vars = kmalloc(len, GFP_ATOMIC);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002324 bus->varsz = bus->vars ? len : 0;
2325 if (bus->vars == NULL) {
Roland Vossene10d82d2011-05-03 11:35:19 +02002326 bcmerror = -ENOMEM;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002327 goto err;
2328 }
2329
2330 /* Copy the passed variables, which should include the
2331 terminating double-null */
Stanislav Fomichev02160692011-02-15 01:05:10 +03002332 memcpy(bus->vars, arg, bus->varsz);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002333err:
2334 return bcmerror;
2335}
2336
2337static int
Roland Vossen67ad48b2011-06-01 13:45:51 +02002338dhdsdio_doiovar(dhd_bus_t *bus, const struct brcmu_iovar *vi, u32 actionid,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002339 const char *name, void *params, int plen, void *arg, int len,
2340 int val_size)
2341{
2342 int bcmerror = 0;
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002343 s32 int_val = 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002344 bool bool_val = 0;
2345
2346 DHD_TRACE(("%s: Enter, action %d name %s params %p plen %d arg %p "
2347 "len %d val_size %d\n",
2348 __func__, actionid, name, params, plen, arg, len, val_size));
2349
Roland Vossen67ad48b2011-06-01 13:45:51 +02002350 bcmerror = brcmu_iovar_lencheck(vi, arg, len, IOV_ISSET(actionid));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002351 if (bcmerror != 0)
2352 goto exit;
2353
2354 if (plen >= (int)sizeof(int_val))
Stanislav Fomichev02160692011-02-15 01:05:10 +03002355 memcpy(&int_val, params, sizeof(int_val));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002356
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002357 bool_val = (int_val != 0) ? true : false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002358
2359 /* Some ioctls use the bus */
2360 dhd_os_sdlock(bus->dhd);
2361
2362 /* Check if dongle is in reset. If so, only allow DEVRESET iovars */
2363 if (bus->dhd->dongle_reset && !(actionid == IOV_SVAL(IOV_DEVRESET) ||
2364 actionid == IOV_GVAL(IOV_DEVRESET))) {
Roland Vossenb74ac122011-05-03 11:35:20 +02002365 bcmerror = -EPERM;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002366 goto exit;
2367 }
2368
2369 /* Handle sleep stuff before any clock mucking */
2370 if (vi->varid == IOV_SLEEP) {
2371 if (IOV_ISSET(actionid)) {
2372 bcmerror = dhdsdio_bussleep(bus, bool_val);
2373 } else {
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002374 int_val = (s32) bus->sleeping;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002375 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002376 }
2377 goto exit;
2378 }
2379
2380 /* Request clock to allow SDIO accesses */
2381 if (!bus->dhd->dongle_reset) {
2382 BUS_WAKE(bus);
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002383 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002384 }
2385
2386 switch (actionid) {
2387 case IOV_GVAL(IOV_INTR):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002388 int_val = (s32) bus->intr;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002389 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002390 break;
2391
2392 case IOV_SVAL(IOV_INTR):
2393 bus->intr = bool_val;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002394 bus->intdis = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002395 if (bus->dhd->up) {
2396 if (bus->intr) {
2397 DHD_INTR(("%s: enable SDIO device interrupts\n",
2398 __func__));
2399 bcmsdh_intr_enable(bus->sdh);
2400 } else {
2401 DHD_INTR(("%s: disable SDIO interrupts\n",
2402 __func__));
2403 bcmsdh_intr_disable(bus->sdh);
2404 }
2405 }
2406 break;
2407
2408 case IOV_GVAL(IOV_POLLRATE):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002409 int_val = (s32) bus->pollrate;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002410 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002411 break;
2412
2413 case IOV_SVAL(IOV_POLLRATE):
2414 bus->pollrate = (uint) int_val;
2415 bus->poll = (bus->pollrate != 0);
2416 break;
2417
2418 case IOV_GVAL(IOV_IDLETIME):
2419 int_val = bus->idletime;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002420 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002421 break;
2422
2423 case IOV_SVAL(IOV_IDLETIME):
2424 if ((int_val < 0) && (int_val != DHD_IDLE_IMMEDIATE))
Roland Vossene10d82d2011-05-03 11:35:19 +02002425 bcmerror = -EINVAL;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002426 else
2427 bus->idletime = int_val;
2428 break;
2429
2430 case IOV_GVAL(IOV_IDLECLOCK):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002431 int_val = (s32) bus->idleclock;
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_IDLECLOCK):
2436 bus->idleclock = int_val;
2437 break;
2438
2439 case IOV_GVAL(IOV_SD1IDLE):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002440 int_val = (s32) sd1idle;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002441 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002442 break;
2443
2444 case IOV_SVAL(IOV_SD1IDLE):
2445 sd1idle = bool_val;
2446 break;
2447
2448 case IOV_SVAL(IOV_MEMBYTES):
2449 case IOV_GVAL(IOV_MEMBYTES):
2450 {
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002451 u32 address;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002452 uint size, dsize;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002453 u8 *data;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002454
2455 bool set = (actionid == IOV_SVAL(IOV_MEMBYTES));
2456
2457 ASSERT(plen >= 2 * sizeof(int));
2458
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002459 address = (u32) int_val;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002460 memcpy(&int_val, (char *)params + sizeof(int_val),
2461 sizeof(int_val));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002462 size = (uint) int_val;
2463
2464 /* Do some validation */
2465 dsize = set ? plen - (2 * sizeof(int)) : len;
2466 if (dsize < size) {
2467 DHD_ERROR(("%s: error on %s membytes, addr "
2468 "0x%08x size %d dsize %d\n",
2469 __func__, (set ? "set" : "get"),
2470 address, size, dsize));
Roland Vossene10d82d2011-05-03 11:35:19 +02002471 bcmerror = -EINVAL;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002472 break;
2473 }
2474
2475 DHD_INFO(("%s: Request to %s %d bytes at address "
2476 "0x%08x\n",
2477 __func__, (set ? "write" : "read"), size, address));
2478
2479 /* If we know about SOCRAM, check for a fit */
2480 if ((bus->orig_ramsize) &&
2481 ((address > bus->orig_ramsize)
2482 || (address + size > bus->orig_ramsize))) {
2483 DHD_ERROR(("%s: ramsize 0x%08x doesn't have %d "
2484 "bytes at 0x%08x\n",
2485 __func__, bus->orig_ramsize, size, address));
Roland Vossene10d82d2011-05-03 11:35:19 +02002486 bcmerror = -EINVAL;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002487 break;
2488 }
2489
2490 /* Generate the actual data pointer */
2491 data =
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002492 set ? (u8 *) params +
2493 2 * sizeof(int) : (u8 *) arg;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002494
2495 /* Call to do the transfer */
2496 bcmerror =
2497 dhdsdio_membytes(bus, set, address, data, size);
2498
2499 break;
2500 }
2501
2502 case IOV_GVAL(IOV_MEMSIZE):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002503 int_val = (s32) bus->ramsize;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002504 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002505 break;
2506
2507 case IOV_GVAL(IOV_SDIOD_DRIVE):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002508 int_val = (s32) dhd_sdiod_drive_strength;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002509 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002510 break;
2511
2512 case IOV_SVAL(IOV_SDIOD_DRIVE):
2513 dhd_sdiod_drive_strength = int_val;
Franky Lin5d0d7a92011-04-25 19:34:05 -07002514 dhdsdio_sdiod_drive_strength_init(bus,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002515 dhd_sdiod_drive_strength);
2516 break;
2517
2518 case IOV_SVAL(IOV_DOWNLOAD):
2519 bcmerror = dhdsdio_download_state(bus, bool_val);
2520 break;
2521
2522 case IOV_SVAL(IOV_VARS):
2523 bcmerror = dhdsdio_downloadvars(bus, arg, len);
2524 break;
2525
2526 case IOV_GVAL(IOV_READAHEAD):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002527 int_val = (s32) dhd_readahead;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002528 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002529 break;
2530
2531 case IOV_SVAL(IOV_READAHEAD):
2532 if (bool_val && !dhd_readahead)
2533 bus->nextlen = 0;
2534 dhd_readahead = bool_val;
2535 break;
2536
2537 case IOV_GVAL(IOV_SDRXCHAIN):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002538 int_val = (s32) bus->use_rxchain;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002539 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002540 break;
2541
2542 case IOV_SVAL(IOV_SDRXCHAIN):
2543 if (bool_val && !bus->sd_rxchain)
Roland Vossene10d82d2011-05-03 11:35:19 +02002544 bcmerror = -ENOTSUPP;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002545 else
2546 bus->use_rxchain = bool_val;
2547 break;
2548 case IOV_GVAL(IOV_ALIGNCTL):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002549 int_val = (s32) dhd_alignctl;
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_ALIGNCTL):
2554 dhd_alignctl = bool_val;
2555 break;
2556
2557 case IOV_GVAL(IOV_SDALIGN):
2558 int_val = DHD_SDALIGN;
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#ifdef DHD_DEBUG
2563 case IOV_GVAL(IOV_VARS):
2564 if (bus->varsz < (uint) len)
Stanislav Fomichev02160692011-02-15 01:05:10 +03002565 memcpy(arg, bus->vars, bus->varsz);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002566 else
Roland Vossene10d82d2011-05-03 11:35:19 +02002567 bcmerror = -EOVERFLOW;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002568 break;
2569#endif /* DHD_DEBUG */
2570
2571#ifdef DHD_DEBUG
2572 case IOV_GVAL(IOV_SDREG):
2573 {
2574 sdreg_t *sd_ptr;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002575 u32 addr, size;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002576
2577 sd_ptr = (sdreg_t *) params;
2578
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07002579 addr = (unsigned long)bus->regs + sd_ptr->offset;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002580 size = sd_ptr->func;
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002581 int_val = (s32) bcmsdh_reg_read(bus->sdh, addr, size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002582 if (bcmsdh_regfail(bus->sdh))
Roland Vossenb74ac122011-05-03 11:35:20 +02002583 bcmerror = -EIO;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002584 memcpy(arg, &int_val, sizeof(s32));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002585 break;
2586 }
2587
2588 case IOV_SVAL(IOV_SDREG):
2589 {
2590 sdreg_t *sd_ptr;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002591 u32 addr, size;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002592
2593 sd_ptr = (sdreg_t *) params;
2594
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07002595 addr = (unsigned long)bus->regs + sd_ptr->offset;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002596 size = sd_ptr->func;
2597 bcmsdh_reg_write(bus->sdh, addr, size, sd_ptr->value);
2598 if (bcmsdh_regfail(bus->sdh))
Roland Vossenb74ac122011-05-03 11:35:20 +02002599 bcmerror = -EIO;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002600 break;
2601 }
2602
2603 /* Same as above, but offset is not backplane
2604 (not SDIO core) */
2605 case IOV_GVAL(IOV_SBREG):
2606 {
2607 sdreg_t sdreg;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002608 u32 addr, size;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002609
Stanislav Fomichev02160692011-02-15 01:05:10 +03002610 memcpy(&sdreg, params, sizeof(sdreg));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002611
2612 addr = SI_ENUM_BASE + sdreg.offset;
2613 size = sdreg.func;
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002614 int_val = (s32) bcmsdh_reg_read(bus->sdh, addr, size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002615 if (bcmsdh_regfail(bus->sdh))
Roland Vossenb74ac122011-05-03 11:35:20 +02002616 bcmerror = -EIO;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002617 memcpy(arg, &int_val, sizeof(s32));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002618 break;
2619 }
2620
2621 case IOV_SVAL(IOV_SBREG):
2622 {
2623 sdreg_t sdreg;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002624 u32 addr, size;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002625
Stanislav Fomichev02160692011-02-15 01:05:10 +03002626 memcpy(&sdreg, params, sizeof(sdreg));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002627
2628 addr = SI_ENUM_BASE + sdreg.offset;
2629 size = sdreg.func;
2630 bcmsdh_reg_write(bus->sdh, addr, size, sdreg.value);
2631 if (bcmsdh_regfail(bus->sdh))
Roland Vossenb74ac122011-05-03 11:35:20 +02002632 bcmerror = -EIO;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002633 break;
2634 }
2635
2636 case IOV_GVAL(IOV_SDCIS):
2637 {
2638 *(char *)arg = 0;
2639
nohee koea3b8a22010-10-09 10:34:38 -07002640 strcat(arg, "\nFunc 0\n");
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002641 bcmsdh_cis_read(bus->sdh, 0x10,
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002642 (u8 *) arg + strlen(arg),
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002643 SBSDIO_CIS_SIZE_LIMIT);
nohee koea3b8a22010-10-09 10:34:38 -07002644 strcat(arg, "\nFunc 1\n");
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002645 bcmsdh_cis_read(bus->sdh, 0x11,
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002646 (u8 *) arg + strlen(arg),
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002647 SBSDIO_CIS_SIZE_LIMIT);
nohee koea3b8a22010-10-09 10:34:38 -07002648 strcat(arg, "\nFunc 2\n");
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002649 bcmsdh_cis_read(bus->sdh, 0x12,
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002650 (u8 *) arg + strlen(arg),
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002651 SBSDIO_CIS_SIZE_LIMIT);
2652 break;
2653 }
2654
2655 case IOV_GVAL(IOV_FORCEEVEN):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002656 int_val = (s32) forcealign;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002657 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002658 break;
2659
2660 case IOV_SVAL(IOV_FORCEEVEN):
2661 forcealign = bool_val;
2662 break;
2663
2664 case IOV_GVAL(IOV_TXBOUND):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002665 int_val = (s32) dhd_txbound;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002666 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002667 break;
2668
2669 case IOV_SVAL(IOV_TXBOUND):
2670 dhd_txbound = (uint) int_val;
2671 break;
2672
2673 case IOV_GVAL(IOV_RXBOUND):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002674 int_val = (s32) dhd_rxbound;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002675 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002676 break;
2677
2678 case IOV_SVAL(IOV_RXBOUND):
2679 dhd_rxbound = (uint) int_val;
2680 break;
2681
2682 case IOV_GVAL(IOV_TXMINMAX):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002683 int_val = (s32) dhd_txminmax;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002684 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002685 break;
2686
2687 case IOV_SVAL(IOV_TXMINMAX):
2688 dhd_txminmax = (uint) int_val;
2689 break;
2690#endif /* DHD_DEBUG */
2691
2692#ifdef SDTEST
2693 case IOV_GVAL(IOV_EXTLOOP):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002694 int_val = (s32) bus->ext_loop;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002695 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002696 break;
2697
2698 case IOV_SVAL(IOV_EXTLOOP):
2699 bus->ext_loop = bool_val;
2700 break;
2701
2702 case IOV_GVAL(IOV_PKTGEN):
2703 bcmerror = dhdsdio_pktgen_get(bus, arg);
2704 break;
2705
2706 case IOV_SVAL(IOV_PKTGEN):
2707 bcmerror = dhdsdio_pktgen_set(bus, arg);
2708 break;
2709#endif /* SDTEST */
2710
2711 case IOV_SVAL(IOV_DEVRESET):
2712 DHD_TRACE(("%s: Called set IOV_DEVRESET=%d dongle_reset=%d "
2713 "busstate=%d\n",
2714 __func__, bool_val, bus->dhd->dongle_reset,
2715 bus->dhd->busstate));
2716
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002717 dhd_bus_devreset(bus->dhd, (u8) bool_val);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002718
2719 break;
2720
2721 case IOV_GVAL(IOV_DEVRESET):
2722 DHD_TRACE(("%s: Called get IOV_DEVRESET\n", __func__));
2723
2724 /* Get its status */
2725 int_val = (bool) bus->dhd->dongle_reset;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002726 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002727
2728 break;
2729
2730 default:
Roland Vossene10d82d2011-05-03 11:35:19 +02002731 bcmerror = -ENOTSUPP;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002732 break;
2733 }
2734
2735exit:
2736 if ((bus->idletime == DHD_IDLE_IMMEDIATE) && !bus->dpc_sched) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002737 bus->activity = false;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07002738 dhdsdio_clkctl(bus, CLK_NONE, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002739 }
2740
2741 dhd_os_sdunlock(bus->dhd);
2742
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002743 if (actionid == IOV_SVAL(IOV_DEVRESET) && bool_val == false)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002744 dhd_preinit_ioctls((dhd_pub_t *) bus->dhd);
2745
2746 return bcmerror;
2747}
2748
2749static int dhdsdio_write_vars(dhd_bus_t *bus)
2750{
2751 int bcmerror = 0;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002752 u32 varsize;
2753 u32 varaddr;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002754 u8 *vbuffer;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002755 u32 varsizew;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002756#ifdef DHD_DEBUG
2757 char *nvram_ularray;
2758#endif /* DHD_DEBUG */
2759
2760 /* Even if there are no vars are to be written, we still
2761 need to set the ramsize. */
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07002762 varsize = bus->varsz ? roundup(bus->varsz, 4) : 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002763 varaddr = (bus->ramsize - 4) - varsize;
2764
2765 if (bus->vars) {
Alexander Beregalov12d0eb42011-03-09 03:53:35 +03002766 vbuffer = kzalloc(varsize, GFP_ATOMIC);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002767 if (!vbuffer)
Roland Vossene10d82d2011-05-03 11:35:19 +02002768 return -ENOMEM;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002769
Stanislav Fomichev02160692011-02-15 01:05:10 +03002770 memcpy(vbuffer, bus->vars, bus->varsz);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002771
2772 /* Write the vars list */
2773 bcmerror =
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07002774 dhdsdio_membytes(bus, true, varaddr, vbuffer, varsize);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002775#ifdef DHD_DEBUG
2776 /* Verify NVRAM bytes */
2777 DHD_INFO(("Compare NVRAM dl & ul; varsize=%d\n", varsize));
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02002778 nvram_ularray = kmalloc(varsize, GFP_ATOMIC);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002779 if (!nvram_ularray)
Roland Vossene10d82d2011-05-03 11:35:19 +02002780 return -ENOMEM;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002781
2782 /* Upload image to verify downloaded contents. */
2783 memset(nvram_ularray, 0xaa, varsize);
2784
2785 /* Read the vars list to temp buffer for comparison */
2786 bcmerror =
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002787 dhdsdio_membytes(bus, false, varaddr, nvram_ularray,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002788 varsize);
2789 if (bcmerror) {
2790 DHD_ERROR(("%s: error %d on reading %d nvram bytes at "
2791 "0x%08x\n", __func__, bcmerror, varsize, varaddr));
2792 }
2793 /* Compare the org NVRAM with the one read from RAM */
2794 if (memcmp(vbuffer, nvram_ularray, varsize)) {
2795 DHD_ERROR(("%s: Downloaded NVRAM image is corrupted.\n",
2796 __func__));
2797 } else
2798 DHD_ERROR(("%s: Download/Upload/Compare of NVRAM ok.\n",
2799 __func__));
2800
mike.rapoport@gmail.com182acb32010-10-13 00:09:12 +02002801 kfree(nvram_ularray);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002802#endif /* DHD_DEBUG */
2803
mike.rapoport@gmail.com182acb32010-10-13 00:09:12 +02002804 kfree(vbuffer);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002805 }
2806
2807 /* adjust to the user specified RAM */
2808 DHD_INFO(("Physical memory size: %d, usable memory size: %d\n",
2809 bus->orig_ramsize, bus->ramsize));
2810 DHD_INFO(("Vars are at %d, orig varsize is %d\n", varaddr, varsize));
2811 varsize = ((bus->orig_ramsize - 4) - varaddr);
2812
2813 /*
2814 * Determine the length token:
2815 * Varsize, converted to words, in lower 16-bits, checksum
2816 * in upper 16-bits.
2817 */
2818 if (bcmerror) {
2819 varsizew = 0;
2820 } else {
2821 varsizew = varsize / 4;
2822 varsizew = (~varsizew << 16) | (varsizew & 0x0000FFFF);
Stanislav Fomichev628f10b2011-02-20 21:43:32 +03002823 varsizew = cpu_to_le32(varsizew);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002824 }
2825
2826 DHD_INFO(("New varsize is %d, length token=0x%08x\n", varsize,
2827 varsizew));
2828
2829 /* Write the length token to the last word */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07002830 bcmerror = dhdsdio_membytes(bus, true, (bus->orig_ramsize - 4),
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002831 (u8 *)&varsizew, 4);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002832
2833 return bcmerror;
2834}
2835
2836static int dhdsdio_download_state(dhd_bus_t *bus, bool enter)
2837{
2838 uint retries;
Franky Lineb5dc512011-04-25 19:34:04 -07002839 u32 regdata;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002840 int bcmerror = 0;
2841
2842 /* To enter download state, disable ARM and reset SOCRAM.
2843 * To exit download state, simply reset ARM (default is RAM boot).
2844 */
2845 if (enter) {
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07002846 bus->alp_only = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002847
Franky Lineb5dc512011-04-25 19:34:04 -07002848 dhdsdio_chip_disablecore(bus->sdh, bus->ci->armcorebase);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002849
Franky Lineb5dc512011-04-25 19:34:04 -07002850 dhdsdio_chip_resetcore(bus->sdh, bus->ci->ramcorebase);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002851
2852 /* Clear the top bit of memory */
2853 if (bus->ramsize) {
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002854 u32 zeros = 0;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07002855 dhdsdio_membytes(bus, true, bus->ramsize - 4,
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002856 (u8 *)&zeros, 4);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002857 }
2858 } else {
Franky Lineb5dc512011-04-25 19:34:04 -07002859 regdata = bcmsdh_reg_read(bus->sdh,
2860 CORE_SB(bus->ci->ramcorebase, sbtmstatelow), 4);
2861 regdata &= (SBTML_RESET | SBTML_REJ_MASK |
2862 (SICF_CLOCK_EN << SBTML_SICF_SHIFT));
2863 if ((SICF_CLOCK_EN << SBTML_SICF_SHIFT) != regdata) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002864 DHD_ERROR(("%s: SOCRAM core is down after reset?\n",
2865 __func__));
Roland Vossenb74ac122011-05-03 11:35:20 +02002866 bcmerror = -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002867 goto fail;
2868 }
2869
2870 bcmerror = dhdsdio_write_vars(bus);
2871 if (bcmerror) {
2872 DHD_ERROR(("%s: no vars written to RAM\n", __func__));
2873 bcmerror = 0;
2874 }
2875
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002876 W_SDREG(0xFFFFFFFF, &bus->regs->intstatus, retries);
2877
Franky Lineb5dc512011-04-25 19:34:04 -07002878 dhdsdio_chip_resetcore(bus->sdh, bus->ci->armcorebase);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002879
2880 /* Allow HT Clock now that the ARM is running. */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002881 bus->alp_only = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002882
2883 bus->dhd->busstate = DHD_BUS_LOAD;
2884 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002885fail:
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002886 return bcmerror;
2887}
2888
2889int
2890dhd_bus_iovar_op(dhd_pub_t *dhdp, const char *name,
2891 void *params, int plen, void *arg, int len, bool set)
2892{
2893 dhd_bus_t *bus = dhdp->bus;
Roland Vossen67ad48b2011-06-01 13:45:51 +02002894 const struct brcmu_iovar *vi = NULL;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002895 int bcmerror = 0;
2896 int val_size;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002897 u32 actionid;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002898
2899 DHD_TRACE(("%s: Enter\n", __func__));
2900
2901 ASSERT(name);
2902 ASSERT(len >= 0);
2903
2904 /* Get MUST have return space */
2905 ASSERT(set || (arg && len));
2906
2907 /* Set does NOT take qualifiers */
2908 ASSERT(!set || (!params && !plen));
2909
2910 /* Look up var locally; if not found pass to host driver */
Roland Vossen67ad48b2011-06-01 13:45:51 +02002911 vi = brcmu_iovar_lookup(dhdsdio_iovars, name);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002912 if (vi == NULL) {
2913 dhd_os_sdlock(bus->dhd);
2914
2915 BUS_WAKE(bus);
2916
2917 /* Turn on clock in case SD command needs backplane */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002918 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002919
2920 bcmerror =
2921 bcmsdh_iovar_op(bus->sdh, name, params, plen, arg, len,
2922 set);
2923
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002924 /* Similar check for blocksize change */
2925 if (set && strcmp(name, "sd_blocksize") == 0) {
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002926 s32 fnum = 2;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002927 if (bcmsdh_iovar_op
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002928 (bus->sdh, "sd_blocksize", &fnum, sizeof(s32),
2929 &bus->blocksize, sizeof(s32),
Roland Vossena1c5ad82011-04-11 15:16:24 +02002930 false) != 0) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002931 bus->blocksize = 0;
2932 DHD_ERROR(("%s: fail on %s get\n", __func__,
2933 "sd_blocksize"));
2934 } else {
2935 DHD_INFO(("%s: noted %s update, value now %d\n",
2936 __func__, "sd_blocksize",
2937 bus->blocksize));
2938 }
2939 }
Greg Kroah-Hartman7068c2f2010-10-08 11:34:59 -07002940 bus->roundup = min(max_roundup, bus->blocksize);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002941
2942 if ((bus->idletime == DHD_IDLE_IMMEDIATE) && !bus->dpc_sched) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002943 bus->activity = false;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07002944 dhdsdio_clkctl(bus, CLK_NONE, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002945 }
2946
2947 dhd_os_sdunlock(bus->dhd);
2948 goto exit;
2949 }
2950
2951 DHD_CTL(("%s: %s %s, len %d plen %d\n", __func__,
2952 name, (set ? "set" : "get"), len, plen));
2953
2954 /* set up 'params' pointer in case this is a set command so that
2955 * the convenience int and bool code can be common to set and get
2956 */
2957 if (params == NULL) {
2958 params = arg;
2959 plen = len;
2960 }
2961
2962 if (vi->type == IOVT_VOID)
2963 val_size = 0;
2964 else if (vi->type == IOVT_BUFFER)
2965 val_size = len;
2966 else
2967 /* all other types are integer sized */
2968 val_size = sizeof(int);
2969
2970 actionid = set ? IOV_SVAL(vi->varid) : IOV_GVAL(vi->varid);
2971 bcmerror =
2972 dhdsdio_doiovar(bus, vi, actionid, name, params, plen, arg, len,
2973 val_size);
2974
2975exit:
2976 return bcmerror;
2977}
2978
2979void dhd_bus_stop(struct dhd_bus *bus, bool enforce_mutex)
2980{
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002981 u32 local_hostintmask;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002982 u8 saveclk;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002983 uint retries;
2984 int err;
2985
2986 DHD_TRACE(("%s: Enter\n", __func__));
2987
2988 if (enforce_mutex)
2989 dhd_os_sdlock(bus->dhd);
2990
2991 BUS_WAKE(bus);
2992
2993 /* Enable clock for device interrupts */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002994 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002995
2996 /* Disable and clear interrupts at the chip level also */
2997 W_SDREG(0, &bus->regs->hostintmask, retries);
2998 local_hostintmask = bus->hostintmask;
2999 bus->hostintmask = 0;
3000
3001 /* Change our idea of bus state */
3002 bus->dhd->busstate = DHD_BUS_DOWN;
3003
3004 /* Force clocks on backplane to be sure F2 interrupt propagates */
3005 saveclk =
3006 bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
3007 &err);
3008 if (!err) {
3009 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
3010 (saveclk | SBSDIO_FORCE_HT), &err);
3011 }
3012 if (err) {
3013 DHD_ERROR(("%s: Failed to force clock for F2: err %d\n",
3014 __func__, err));
3015 }
3016
3017 /* Turn off the bus (F2), free any pending packets */
3018 DHD_INTR(("%s: disable SDIO interrupts\n", __func__));
3019 bcmsdh_intr_disable(bus->sdh);
Franky Lin0df46042011-06-01 13:45:40 +02003020 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_0, SDIO_CCCR_IOEx,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003021 SDIO_FUNC_ENABLE_1, NULL);
3022
3023 /* Clear any pending interrupts now that F2 is disabled */
3024 W_SDREG(local_hostintmask, &bus->regs->intstatus, retries);
3025
3026 /* Turn off the backplane clock (only) */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003027 dhdsdio_clkctl(bus, CLK_SDONLY, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003028
3029 /* Clear the data packet queues */
Roland Vossen67ad48b2011-06-01 13:45:51 +02003030 brcmu_pktq_flush(&bus->txq, true, NULL, NULL);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003031
3032 /* Clear any held glomming stuff */
3033 if (bus->glomd)
Roland Vossen67ad48b2011-06-01 13:45:51 +02003034 brcmu_pkt_buf_free_skb(bus->glomd);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003035
3036 if (bus->glom)
Roland Vossen67ad48b2011-06-01 13:45:51 +02003037 brcmu_pkt_buf_free_skb(bus->glom);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003038
3039 bus->glom = bus->glomd = NULL;
3040
3041 /* Clear rx control and wake any waiters */
3042 bus->rxlen = 0;
3043 dhd_os_ioctl_resp_wake(bus->dhd);
3044
3045 /* Reset some F2 state stuff */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003046 bus->rxskip = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003047 bus->tx_seq = bus->rx_seq = 0;
3048
3049 if (enforce_mutex)
3050 dhd_os_sdunlock(bus->dhd);
3051}
3052
3053int dhd_bus_init(dhd_pub_t *dhdp, bool enforce_mutex)
3054{
3055 dhd_bus_t *bus = dhdp->bus;
3056 dhd_timeout_t tmo;
3057 uint retries = 0;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003058 u8 ready, enable;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003059 int err, ret = 0;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003060 u8 saveclk;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003061
3062 DHD_TRACE(("%s: Enter\n", __func__));
3063
3064 ASSERT(bus->dhd);
3065 if (!bus->dhd)
3066 return 0;
3067
3068 if (enforce_mutex)
3069 dhd_os_sdlock(bus->dhd);
3070
3071 /* Make sure backplane clock is on, needed to generate F2 interrupt */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003072 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003073 if (bus->clkstate != CLK_AVAIL)
3074 goto exit;
3075
3076 /* Force clocks on backplane to be sure F2 interrupt propagates */
3077 saveclk =
3078 bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
3079 &err);
3080 if (!err) {
3081 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
3082 (saveclk | SBSDIO_FORCE_HT), &err);
3083 }
3084 if (err) {
3085 DHD_ERROR(("%s: Failed to force clock for F2: err %d\n",
3086 __func__, err));
3087 goto exit;
3088 }
3089
3090 /* Enable function 2 (frame transfers) */
3091 W_SDREG((SDPCM_PROT_VERSION << SMB_DATA_VERSION_SHIFT),
3092 &bus->regs->tosbmailboxdata, retries);
3093 enable = (SDIO_FUNC_ENABLE_1 | SDIO_FUNC_ENABLE_2);
3094
Franky Lin0df46042011-06-01 13:45:40 +02003095 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_0, SDIO_CCCR_IOEx, enable, NULL);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003096
3097 /* Give the dongle some time to do its thing and set IOR2 */
3098 dhd_timeout_start(&tmo, DHD_WAIT_F2RDY * 1000);
3099
3100 ready = 0;
3101 while (ready != enable && !dhd_timeout_expired(&tmo))
3102 ready =
Franky Lin0df46042011-06-01 13:45:40 +02003103 bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_0, SDIO_CCCR_IORx,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003104 NULL);
3105
3106 DHD_INFO(("%s: enable 0x%02x, ready 0x%02x (waited %uus)\n",
3107 __func__, enable, ready, tmo.elapsed));
3108
3109 /* If F2 successfully enabled, set core and enable interrupts */
3110 if (ready == enable) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003111 /* Set up the interrupt mask and enable interrupts */
3112 bus->hostintmask = HOSTINTMASK;
Franky Linc05df632011-04-25 19:34:07 -07003113 W_SDREG(bus->hostintmask,
3114 (unsigned int *)CORE_BUS_REG(bus->ci->buscorebase,
3115 hostintmask), retries);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003116
3117 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_WATERMARK,
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003118 (u8) watermark, &err);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003119
3120 /* Set bus state according to enable result */
3121 dhdp->busstate = DHD_BUS_DATA;
3122
3123 /* bcmsdh_intr_unmask(bus->sdh); */
3124
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003125 bus->intdis = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003126 if (bus->intr) {
3127 DHD_INTR(("%s: enable SDIO device interrupts\n",
3128 __func__));
3129 bcmsdh_intr_enable(bus->sdh);
3130 } else {
3131 DHD_INTR(("%s: disable SDIO interrupts\n", __func__));
3132 bcmsdh_intr_disable(bus->sdh);
3133 }
3134
3135 }
3136
3137 else {
3138 /* Disable F2 again */
3139 enable = SDIO_FUNC_ENABLE_1;
Franky Lin0df46042011-06-01 13:45:40 +02003140 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_0, SDIO_CCCR_IOEx, enable,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003141 NULL);
3142 }
3143
3144 /* Restore previous clock setting */
3145 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
3146 saveclk, &err);
3147
3148 /* If we didn't come up, turn off backplane clock */
3149 if (dhdp->busstate != DHD_BUS_DATA)
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003150 dhdsdio_clkctl(bus, CLK_NONE, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003151
3152exit:
3153 if (enforce_mutex)
3154 dhd_os_sdunlock(bus->dhd);
3155
3156 return ret;
3157}
3158
3159static void dhdsdio_rxfail(dhd_bus_t *bus, bool abort, bool rtx)
3160{
3161 bcmsdh_info_t *sdh = bus->sdh;
Franky Lin597600a2011-06-01 13:45:39 +02003162 struct sdpcmd_regs *regs = bus->regs;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003163 uint retries = 0;
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07003164 u16 lastrbc;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003165 u8 hi, lo;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003166 int err;
3167
3168 DHD_ERROR(("%s: %sterminate frame%s\n", __func__,
3169 (abort ? "abort command, " : ""),
3170 (rtx ? ", send NAK" : "")));
3171
3172 if (abort)
3173 bcmsdh_abort(sdh, SDIO_FUNC_2);
3174
3175 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_FRAMECTRL, SFC_RF_TERM,
3176 &err);
3177 bus->f1regdata++;
3178
3179 /* Wait until the packet has been flushed (device/FIFO stable) */
3180 for (lastrbc = retries = 0xffff; retries > 0; retries--) {
3181 hi = bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_RFRAMEBCHI,
3182 NULL);
3183 lo = bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_RFRAMEBCLO,
3184 NULL);
3185 bus->f1regdata += 2;
3186
3187 if ((hi == 0) && (lo == 0))
3188 break;
3189
3190 if ((hi > (lastrbc >> 8)) && (lo > (lastrbc & 0x00ff))) {
3191 DHD_ERROR(("%s: count growing: last 0x%04x now "
3192 "0x%04x\n",
3193 __func__, lastrbc, ((hi << 8) + lo)));
3194 }
3195 lastrbc = (hi << 8) + lo;
3196 }
3197
3198 if (!retries) {
3199 DHD_ERROR(("%s: count never zeroed: last 0x%04x\n",
3200 __func__, lastrbc));
3201 } else {
3202 DHD_INFO(("%s: flush took %d iterations\n", __func__,
3203 (0xffff - retries)));
3204 }
3205
3206 if (rtx) {
3207 bus->rxrtx++;
3208 W_SDREG(SMB_NAK, &regs->tosbmailbox, retries);
3209 bus->f1regdata++;
3210 if (retries <= retry_limit)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003211 bus->rxskip = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003212 }
3213
3214 /* Clear partial in any case */
3215 bus->nextlen = 0;
3216
3217 /* If we can't reach the device, signal failure */
3218 if (err || bcmsdh_regfail(sdh))
3219 bus->dhd->busstate = DHD_BUS_DOWN;
3220}
3221
3222static void
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003223dhdsdio_read_control(dhd_bus_t *bus, u8 *hdr, uint len, uint doff)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003224{
3225 bcmsdh_info_t *sdh = bus->sdh;
3226 uint rdlen, pad;
3227
3228 int sdret;
3229
3230 DHD_TRACE(("%s: Enter\n", __func__));
3231
3232 /* Control data already received in aligned rxctl */
3233 if ((bus->bus == SPI_BUS) && (!bus->usebufpool))
3234 goto gotpkt;
3235
3236 ASSERT(bus->rxbuf);
3237 /* Set rxctl for frame (w/optional alignment) */
3238 bus->rxctl = bus->rxbuf;
3239 if (dhd_alignctl) {
3240 bus->rxctl += firstread;
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07003241 pad = ((unsigned long)bus->rxctl % DHD_SDALIGN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003242 if (pad)
3243 bus->rxctl += (DHD_SDALIGN - pad);
3244 bus->rxctl -= firstread;
3245 }
3246 ASSERT(bus->rxctl >= bus->rxbuf);
3247
3248 /* Copy the already-read portion over */
Stanislav Fomichev02160692011-02-15 01:05:10 +03003249 memcpy(bus->rxctl, hdr, firstread);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003250 if (len <= firstread)
3251 goto gotpkt;
3252
3253 /* Copy the full data pkt in gSPI case and process ioctl. */
3254 if (bus->bus == SPI_BUS) {
Stanislav Fomichev02160692011-02-15 01:05:10 +03003255 memcpy(bus->rxctl, hdr, len);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003256 goto gotpkt;
3257 }
3258
3259 /* Raise rdlen to next SDIO block to avoid tail command */
3260 rdlen = len - firstread;
3261 if (bus->roundup && bus->blocksize && (rdlen > bus->blocksize)) {
3262 pad = bus->blocksize - (rdlen % bus->blocksize);
3263 if ((pad <= bus->roundup) && (pad < bus->blocksize) &&
3264 ((len + pad) < bus->dhd->maxctl))
3265 rdlen += pad;
3266 } else if (rdlen % DHD_SDALIGN) {
3267 rdlen += DHD_SDALIGN - (rdlen % DHD_SDALIGN);
3268 }
3269
3270 /* Satisfy length-alignment requirements */
3271 if (forcealign && (rdlen & (ALIGNMENT - 1)))
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07003272 rdlen = roundup(rdlen, ALIGNMENT);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003273
3274 /* Drop if the read is too big or it exceeds our maximum */
3275 if ((rdlen + firstread) > bus->dhd->maxctl) {
3276 DHD_ERROR(("%s: %d-byte control read exceeds %d-byte buffer\n",
3277 __func__, rdlen, bus->dhd->maxctl));
3278 bus->dhd->rx_errors++;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003279 dhdsdio_rxfail(bus, false, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003280 goto done;
3281 }
3282
3283 if ((len - doff) > bus->dhd->maxctl) {
3284 DHD_ERROR(("%s: %d-byte ctl frame (%d-byte ctl data) exceeds "
3285 "%d-byte limit\n",
3286 __func__, len, (len - doff), bus->dhd->maxctl));
3287 bus->dhd->rx_errors++;
3288 bus->rx_toolong++;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003289 dhdsdio_rxfail(bus, false, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003290 goto done;
3291 }
3292
3293 /* Read remainder of frame body into the rxctl buffer */
Grant Grundler4b455e02011-05-04 09:59:47 -07003294 sdret = bcmsdh_recv_buf(bus, bcmsdh_cur_sbwad(sdh), SDIO_FUNC_2,
3295 F2SYNC, (bus->rxctl + firstread), rdlen,
3296 NULL, NULL, NULL);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003297 bus->f2rxdata++;
Roland Vossenb7ef2a92011-05-03 11:35:02 +02003298 ASSERT(sdret != -BCME_PENDING);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003299
3300 /* Control frame failures need retransmission */
3301 if (sdret < 0) {
3302 DHD_ERROR(("%s: read %d control bytes failed: %d\n",
3303 __func__, rdlen, sdret));
3304 bus->rxc_errors++; /* dhd.rx_ctlerrs is higher level */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003305 dhdsdio_rxfail(bus, true, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003306 goto done;
3307 }
3308
3309gotpkt:
3310
3311#ifdef DHD_DEBUG
Arend van Spriel34227312011-05-10 22:25:32 +02003312 if (DHD_BYTES_ON() && DHD_CTL_ON()) {
3313 printk(KERN_DEBUG "RxCtrl:\n");
3314 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, bus->rxctl, len);
3315 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003316#endif
3317
3318 /* Point to valid data and indicate its length */
3319 bus->rxctl += doff;
3320 bus->rxlen = len - doff;
3321
3322done:
3323 /* Awake any waiters */
3324 dhd_os_ioctl_resp_wake(bus->dhd);
3325}
3326
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003327static u8 dhdsdio_rxglom(dhd_bus_t *bus, u8 rxseq)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003328{
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07003329 u16 dlen, totlen;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003330 u8 *dptr, num = 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003331
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07003332 u16 sublen, check;
Arend van Sprielc26b1372010-11-23 14:06:23 +01003333 struct sk_buff *pfirst, *plast, *pnext, *save_pfirst;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003334
3335 int errcode;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003336 u8 chan, seq, doff, sfdoff;
3337 u8 txmax;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003338
3339 int ifidx = 0;
3340 bool usechain = bus->use_rxchain;
3341
3342 /* If packets, issue read(s) and send up packet chain */
3343 /* Return sequence numbers consumed? */
3344
3345 DHD_TRACE(("dhdsdio_rxglom: start: glomd %p glom %p\n", bus->glomd,
3346 bus->glom));
3347
3348 /* If there's a descriptor, generate the packet chain */
3349 if (bus->glomd) {
3350 dhd_os_sdlock_rxq(bus->dhd);
3351
3352 pfirst = plast = pnext = NULL;
Arend van Spriel54991ad2010-11-23 14:06:24 +01003353 dlen = (u16) (bus->glomd->len);
3354 dptr = bus->glomd->data;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003355 if (!dlen || (dlen & 1)) {
3356 DHD_ERROR(("%s: bad glomd len(%d), ignore descriptor\n",
3357 __func__, dlen));
3358 dlen = 0;
3359 }
3360
3361 for (totlen = num = 0; dlen; num++) {
3362 /* Get (and move past) next length */
Stanislav Fomichev56dfe3c2011-02-20 21:43:33 +03003363 sublen = get_unaligned_le16(dptr);
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07003364 dlen -= sizeof(u16);
3365 dptr += sizeof(u16);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003366 if ((sublen < SDPCM_HDRLEN) ||
3367 ((num == 0) && (sublen < (2 * SDPCM_HDRLEN)))) {
3368 DHD_ERROR(("%s: descriptor len %d bad: %d\n",
3369 __func__, num, sublen));
3370 pnext = NULL;
3371 break;
3372 }
3373 if (sublen % DHD_SDALIGN) {
3374 DHD_ERROR(("%s: sublen %d not multiple of %d\n",
3375 __func__, sublen, DHD_SDALIGN));
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003376 usechain = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003377 }
3378 totlen += sublen;
3379
3380 /* For last frame, adjust read len so total
3381 is a block multiple */
3382 if (!dlen) {
3383 sublen +=
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07003384 (roundup(totlen, bus->blocksize) - totlen);
3385 totlen = roundup(totlen, bus->blocksize);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003386 }
3387
3388 /* Allocate/chain packet for next subframe */
Roland Vossen67ad48b2011-06-01 13:45:51 +02003389 pnext = brcmu_pkt_buf_get_skb(sublen + DHD_SDALIGN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003390 if (pnext == NULL) {
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02003391 DHD_ERROR(("%s: bcm_pkt_buf_get_skb failed, "
3392 "num %d len %d\n", __func__,
3393 num, sublen));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003394 break;
3395 }
Arend van Spriel54991ad2010-11-23 14:06:24 +01003396 ASSERT(!(pnext->prev));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003397 if (!pfirst) {
3398 ASSERT(!plast);
3399 pfirst = plast = pnext;
3400 } else {
3401 ASSERT(plast);
Arend van Spriel54991ad2010-11-23 14:06:24 +01003402 plast->next = pnext;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003403 plast = pnext;
3404 }
3405
3406 /* Adhere to start alignment requirements */
Arend van Spriel3c9d4c32011-03-02 21:18:48 +01003407 PKTALIGN(pnext, sublen, DHD_SDALIGN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003408 }
3409
3410 /* If all allocations succeeded, save packet chain
3411 in bus structure */
3412 if (pnext) {
3413 DHD_GLOM(("%s: allocated %d-byte packet chain for %d "
3414 "subframes\n", __func__, totlen, num));
3415 if (DHD_GLOM_ON() && bus->nextlen) {
3416 if (totlen != bus->nextlen) {
3417 DHD_GLOM(("%s: glomdesc mismatch: nextlen %d glomdesc %d " "rxseq %d\n",
3418 __func__, bus->nextlen,
3419 totlen, rxseq));
3420 }
3421 }
3422 bus->glom = pfirst;
3423 pfirst = pnext = NULL;
3424 } else {
3425 if (pfirst)
Roland Vossen67ad48b2011-06-01 13:45:51 +02003426 brcmu_pkt_buf_free_skb(pfirst);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003427 bus->glom = NULL;
3428 num = 0;
3429 }
3430
3431 /* Done with descriptor packet */
Roland Vossen67ad48b2011-06-01 13:45:51 +02003432 brcmu_pkt_buf_free_skb(bus->glomd);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003433 bus->glomd = NULL;
3434 bus->nextlen = 0;
3435
3436 dhd_os_sdunlock_rxq(bus->dhd);
3437 }
3438
3439 /* Ok -- either we just generated a packet chain,
3440 or had one from before */
3441 if (bus->glom) {
3442 if (DHD_GLOM_ON()) {
3443 DHD_GLOM(("%s: try superframe read, packet chain:\n",
3444 __func__));
Arend van Spriel54991ad2010-11-23 14:06:24 +01003445 for (pnext = bus->glom; pnext; pnext = pnext->next) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003446 DHD_GLOM((" %p: %p len 0x%04x (%d)\n",
Arend van Spriel54991ad2010-11-23 14:06:24 +01003447 pnext, (u8 *) (pnext->data),
3448 pnext->len, pnext->len));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003449 }
3450 }
3451
3452 pfirst = bus->glom;
Roland Vossen67ad48b2011-06-01 13:45:51 +02003453 dlen = (u16) brcmu_pkttotlen(pfirst);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003454
3455 /* Do an SDIO read for the superframe. Configurable iovar to
3456 * read directly into the chained packet, or allocate a large
3457 * packet and and copy into the chain.
3458 */
3459 if (usechain) {
Grant Grundler4b455e02011-05-04 09:59:47 -07003460 errcode = bcmsdh_recv_buf(bus,
3461 bcmsdh_cur_sbwad(bus->sdh), SDIO_FUNC_2,
3462 F2SYNC, (u8 *) pfirst->data, dlen,
3463 pfirst, NULL, NULL);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003464 } else if (bus->dataptr) {
Grant Grundler4b455e02011-05-04 09:59:47 -07003465 errcode = bcmsdh_recv_buf(bus,
3466 bcmsdh_cur_sbwad(bus->sdh), SDIO_FUNC_2,
3467 F2SYNC, bus->dataptr, dlen,
3468 NULL, NULL, NULL);
Roland Vossen67ad48b2011-06-01 13:45:51 +02003469 sublen = (u16) brcmu_pktfrombuf(pfirst, 0, dlen,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003470 bus->dataptr);
3471 if (sublen != dlen) {
3472 DHD_ERROR(("%s: FAILED TO COPY, dlen %d sublen %d\n",
3473 __func__, dlen, sublen));
3474 errcode = -1;
3475 }
3476 pnext = NULL;
3477 } else {
3478 DHD_ERROR(("COULDN'T ALLOC %d-BYTE GLOM, FORCE FAILURE\n",
3479 dlen));
3480 errcode = -1;
3481 }
3482 bus->f2rxdata++;
Roland Vossenb7ef2a92011-05-03 11:35:02 +02003483 ASSERT(errcode != -BCME_PENDING);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003484
3485 /* On failure, kill the superframe, allow a couple retries */
3486 if (errcode < 0) {
3487 DHD_ERROR(("%s: glom read of %d bytes failed: %d\n",
3488 __func__, dlen, errcode));
3489 bus->dhd->rx_errors++;
3490
3491 if (bus->glomerr++ < 3) {
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003492 dhdsdio_rxfail(bus, true, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003493 } else {
3494 bus->glomerr = 0;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003495 dhdsdio_rxfail(bus, true, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003496 dhd_os_sdlock_rxq(bus->dhd);
Roland Vossen67ad48b2011-06-01 13:45:51 +02003497 brcmu_pkt_buf_free_skb(bus->glom);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003498 dhd_os_sdunlock_rxq(bus->dhd);
3499 bus->rxglomfail++;
3500 bus->glom = NULL;
3501 }
3502 return 0;
3503 }
3504#ifdef DHD_DEBUG
3505 if (DHD_GLOM_ON()) {
Arend van Spriel34227312011-05-10 22:25:32 +02003506 printk(KERN_DEBUG "SUPERFRAME:\n");
3507 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
3508 pfirst->data, min_t(int, pfirst->len, 48));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003509 }
3510#endif
3511
3512 /* Validate the superframe header */
Arend van Spriel54991ad2010-11-23 14:06:24 +01003513 dptr = (u8 *) (pfirst->data);
Stanislav Fomichev56dfe3c2011-02-20 21:43:33 +03003514 sublen = get_unaligned_le16(dptr);
3515 check = get_unaligned_le16(dptr + sizeof(u16));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003516
3517 chan = SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]);
3518 seq = SDPCM_PACKET_SEQUENCE(&dptr[SDPCM_FRAMETAG_LEN]);
3519 bus->nextlen = dptr[SDPCM_FRAMETAG_LEN + SDPCM_NEXTLEN_OFFSET];
3520 if ((bus->nextlen << 4) > MAX_RX_DATASZ) {
3521 DHD_INFO(("%s: nextlen too large (%d) seq %d\n",
3522 __func__, bus->nextlen, seq));
3523 bus->nextlen = 0;
3524 }
3525 doff = SDPCM_DOFFSET_VALUE(&dptr[SDPCM_FRAMETAG_LEN]);
3526 txmax = SDPCM_WINDOW_VALUE(&dptr[SDPCM_FRAMETAG_LEN]);
3527
3528 errcode = 0;
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07003529 if ((u16)~(sublen ^ check)) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003530 DHD_ERROR(("%s (superframe): HW hdr error: len/check "
3531 "0x%04x/0x%04x\n", __func__, sublen, check));
3532 errcode = -1;
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07003533 } else if (roundup(sublen, bus->blocksize) != dlen) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003534 DHD_ERROR(("%s (superframe): len 0x%04x, rounded "
3535 "0x%04x, expect 0x%04x\n",
3536 __func__, sublen,
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07003537 roundup(sublen, bus->blocksize), dlen));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003538 errcode = -1;
3539 } else if (SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]) !=
3540 SDPCM_GLOM_CHANNEL) {
3541 DHD_ERROR(("%s (superframe): bad channel %d\n",
3542 __func__,
3543 SDPCM_PACKET_CHANNEL(&dptr
3544 [SDPCM_FRAMETAG_LEN])));
3545 errcode = -1;
3546 } else if (SDPCM_GLOMDESC(&dptr[SDPCM_FRAMETAG_LEN])) {
3547 DHD_ERROR(("%s (superframe): got second descriptor?\n",
3548 __func__));
3549 errcode = -1;
3550 } else if ((doff < SDPCM_HDRLEN) ||
Arend van Spriel54991ad2010-11-23 14:06:24 +01003551 (doff > (pfirst->len - SDPCM_HDRLEN))) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003552 DHD_ERROR(("%s (superframe): Bad data offset %d: HW %d "
3553 "pkt %d min %d\n",
3554 __func__, doff, sublen,
Arend van Spriel54991ad2010-11-23 14:06:24 +01003555 pfirst->len, SDPCM_HDRLEN));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003556 errcode = -1;
3557 }
3558
3559 /* Check sequence number of superframe SW header */
3560 if (rxseq != seq) {
3561 DHD_INFO(("%s: (superframe) rx_seq %d, expected %d\n",
3562 __func__, seq, rxseq));
3563 bus->rx_badseq++;
3564 rxseq = seq;
3565 }
3566
3567 /* Check window for sanity */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003568 if ((u8) (txmax - bus->tx_seq) > 0x40) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003569 DHD_ERROR(("%s: unlikely tx max %d with tx_seq %d\n",
3570 __func__, txmax, bus->tx_seq));
3571 txmax = bus->tx_seq + 2;
3572 }
3573 bus->tx_max = txmax;
3574
3575 /* Remove superframe header, remember offset */
Arend van Sprielc303ecb2010-11-18 20:46:43 +01003576 skb_pull(pfirst, doff);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003577 sfdoff = doff;
3578
3579 /* Validate all the subframe headers */
3580 for (num = 0, pnext = pfirst; pnext && !errcode;
Arend van Spriel54991ad2010-11-23 14:06:24 +01003581 num++, pnext = pnext->next) {
3582 dptr = (u8 *) (pnext->data);
3583 dlen = (u16) (pnext->len);
Stanislav Fomichev56dfe3c2011-02-20 21:43:33 +03003584 sublen = get_unaligned_le16(dptr);
3585 check = get_unaligned_le16(dptr + sizeof(u16));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003586 chan = SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]);
3587 doff = SDPCM_DOFFSET_VALUE(&dptr[SDPCM_FRAMETAG_LEN]);
3588#ifdef DHD_DEBUG
Arend van Spriel34227312011-05-10 22:25:32 +02003589 if (DHD_GLOM_ON()) {
3590 printk(KERN_DEBUG "subframe:\n");
3591 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
3592 dptr, 32);
3593 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003594#endif
3595
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07003596 if ((u16)~(sublen ^ check)) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003597 DHD_ERROR(("%s (subframe %d): HW hdr error: "
3598 "len/check 0x%04x/0x%04x\n",
3599 __func__, num, sublen, check));
3600 errcode = -1;
3601 } else if ((sublen > dlen) || (sublen < SDPCM_HDRLEN)) {
3602 DHD_ERROR(("%s (subframe %d): length mismatch: "
3603 "len 0x%04x, expect 0x%04x\n",
3604 __func__, num, sublen, dlen));
3605 errcode = -1;
3606 } else if ((chan != SDPCM_DATA_CHANNEL) &&
3607 (chan != SDPCM_EVENT_CHANNEL)) {
3608 DHD_ERROR(("%s (subframe %d): bad channel %d\n",
3609 __func__, num, chan));
3610 errcode = -1;
3611 } else if ((doff < SDPCM_HDRLEN) || (doff > sublen)) {
3612 DHD_ERROR(("%s (subframe %d): Bad data offset %d: HW %d min %d\n",
3613 __func__, num, doff, sublen,
3614 SDPCM_HDRLEN));
3615 errcode = -1;
3616 }
3617 }
3618
3619 if (errcode) {
3620 /* Terminate frame on error, request
3621 a couple retries */
3622 if (bus->glomerr++ < 3) {
3623 /* Restore superframe header space */
Arend van Sprielc303ecb2010-11-18 20:46:43 +01003624 skb_push(pfirst, sfdoff);
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003625 dhdsdio_rxfail(bus, true, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003626 } else {
3627 bus->glomerr = 0;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003628 dhdsdio_rxfail(bus, true, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003629 dhd_os_sdlock_rxq(bus->dhd);
Roland Vossen67ad48b2011-06-01 13:45:51 +02003630 brcmu_pkt_buf_free_skb(bus->glom);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003631 dhd_os_sdunlock_rxq(bus->dhd);
3632 bus->rxglomfail++;
3633 bus->glom = NULL;
3634 }
3635 bus->nextlen = 0;
3636 return 0;
3637 }
3638
3639 /* Basic SD framing looks ok - process each packet (header) */
3640 save_pfirst = pfirst;
3641 bus->glom = NULL;
3642 plast = NULL;
3643
3644 dhd_os_sdlock_rxq(bus->dhd);
3645 for (num = 0; pfirst; rxseq++, pfirst = pnext) {
Arend van Spriel54991ad2010-11-23 14:06:24 +01003646 pnext = pfirst->next;
3647 pfirst->next = NULL;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003648
Arend van Spriel54991ad2010-11-23 14:06:24 +01003649 dptr = (u8 *) (pfirst->data);
Stanislav Fomichev56dfe3c2011-02-20 21:43:33 +03003650 sublen = get_unaligned_le16(dptr);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003651 chan = SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]);
3652 seq = SDPCM_PACKET_SEQUENCE(&dptr[SDPCM_FRAMETAG_LEN]);
3653 doff = SDPCM_DOFFSET_VALUE(&dptr[SDPCM_FRAMETAG_LEN]);
3654
3655 DHD_GLOM(("%s: Get subframe %d, %p(%p/%d), sublen %d "
3656 "chan %d seq %d\n",
Arend van Spriel54991ad2010-11-23 14:06:24 +01003657 __func__, num, pfirst, pfirst->data,
3658 pfirst->len, sublen, chan, seq));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003659
3660 ASSERT((chan == SDPCM_DATA_CHANNEL)
3661 || (chan == SDPCM_EVENT_CHANNEL));
3662
3663 if (rxseq != seq) {
3664 DHD_GLOM(("%s: rx_seq %d, expected %d\n",
3665 __func__, seq, rxseq));
3666 bus->rx_badseq++;
3667 rxseq = seq;
3668 }
3669#ifdef DHD_DEBUG
Arend van Spriel34227312011-05-10 22:25:32 +02003670 if (DHD_BYTES_ON() && DHD_DATA_ON()) {
3671 printk(KERN_DEBUG "Rx Subframe Data:\n");
3672 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
3673 dptr, dlen);
3674 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003675#endif
3676
Arend van Spriel2cb8ada2010-11-18 20:46:44 +01003677 __skb_trim(pfirst, sublen);
Arend van Sprielc303ecb2010-11-18 20:46:43 +01003678 skb_pull(pfirst, doff);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003679
Arend van Spriel54991ad2010-11-23 14:06:24 +01003680 if (pfirst->len == 0) {
Roland Vossen67ad48b2011-06-01 13:45:51 +02003681 brcmu_pkt_buf_free_skb(pfirst);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003682 if (plast) {
Arend van Spriel54991ad2010-11-23 14:06:24 +01003683 plast->next = pnext;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003684 } else {
3685 ASSERT(save_pfirst == pfirst);
3686 save_pfirst = pnext;
3687 }
3688 continue;
3689 } else if (dhd_prot_hdrpull(bus->dhd, &ifidx, pfirst) !=
3690 0) {
3691 DHD_ERROR(("%s: rx protocol error\n",
3692 __func__));
3693 bus->dhd->rx_errors++;
Roland Vossen67ad48b2011-06-01 13:45:51 +02003694 brcmu_pkt_buf_free_skb(pfirst);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003695 if (plast) {
Arend van Spriel54991ad2010-11-23 14:06:24 +01003696 plast->next = pnext;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003697 } else {
3698 ASSERT(save_pfirst == pfirst);
3699 save_pfirst = pnext;
3700 }
3701 continue;
3702 }
3703
3704 /* this packet will go up, link back into
3705 chain and count it */
Arend van Spriel54991ad2010-11-23 14:06:24 +01003706 pfirst->next = pnext;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003707 plast = pfirst;
3708 num++;
3709
3710#ifdef DHD_DEBUG
3711 if (DHD_GLOM_ON()) {
3712 DHD_GLOM(("%s subframe %d to stack, %p(%p/%d) "
3713 "nxt/lnk %p/%p\n",
Arend van Spriel54991ad2010-11-23 14:06:24 +01003714 __func__, num, pfirst, pfirst->data,
3715 pfirst->len, pfirst->next,
3716 pfirst->prev));
Arend van Spriel34227312011-05-10 22:25:32 +02003717 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
3718 pfirst->data,
3719 min_t(int, pfirst->len, 32));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003720 }
3721#endif /* DHD_DEBUG */
3722 }
3723 dhd_os_sdunlock_rxq(bus->dhd);
3724 if (num) {
3725 dhd_os_sdunlock(bus->dhd);
3726 dhd_rx_frame(bus->dhd, ifidx, save_pfirst, num);
3727 dhd_os_sdlock(bus->dhd);
3728 }
3729
3730 bus->rxglomframes++;
3731 bus->rxglompkts += num;
3732 }
3733 return num;
3734}
3735
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003736/* Return true if there may be more frames to read */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003737static uint dhdsdio_readframes(dhd_bus_t *bus, uint maxframes, bool *finished)
3738{
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003739 bcmsdh_info_t *sdh = bus->sdh;
3740
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07003741 u16 len, check; /* Extracted hardware header fields */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003742 u8 chan, seq, doff; /* Extracted software header fields */
3743 u8 fcbits; /* Extracted fcbits from software header */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003744
Arend van Sprielc26b1372010-11-23 14:06:23 +01003745 struct sk_buff *pkt; /* Packet for event or data frames */
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07003746 u16 pad; /* Number of pad bytes to read */
3747 u16 rdlen; /* Total number of bytes to read */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003748 u8 rxseq; /* Next sequence number to expect */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003749 uint rxleft = 0; /* Remaining number of frames allowed */
3750 int sdret; /* Return code from bcmsdh calls */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003751 u8 txmax; /* Maximum tx sequence offered */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003752 bool len_consistent; /* Result of comparing readahead len and
3753 len from hw-hdr */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003754 u8 *rxbuf;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003755 int ifidx = 0;
3756 uint rxcount = 0; /* Total frames read */
3757
3758#if defined(DHD_DEBUG) || defined(SDTEST)
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003759 bool sdtest = false; /* To limit message spew from test mode */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003760#endif
3761
3762 DHD_TRACE(("%s: Enter\n", __func__));
3763
3764 ASSERT(maxframes);
3765
3766#ifdef SDTEST
3767 /* Allow pktgen to override maxframes */
3768 if (bus->pktgen_count && (bus->pktgen_mode == DHD_PKTGEN_RECV)) {
3769 maxframes = bus->pktgen_count;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003770 sdtest = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003771 }
3772#endif
3773
3774 /* Not finished unless we encounter no more frames indication */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003775 *finished = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003776
3777 for (rxseq = bus->rx_seq, rxleft = maxframes;
3778 !bus->rxskip && rxleft && bus->dhd->busstate != DHD_BUS_DOWN;
3779 rxseq++, rxleft--) {
3780
3781 /* Handle glomming separately */
3782 if (bus->glom || bus->glomd) {
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003783 u8 cnt;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003784 DHD_GLOM(("%s: calling rxglom: glomd %p, glom %p\n",
3785 __func__, bus->glomd, bus->glom));
3786 cnt = dhdsdio_rxglom(bus, rxseq);
3787 DHD_GLOM(("%s: rxglom returned %d\n", __func__, cnt));
3788 rxseq += cnt - 1;
3789 rxleft = (rxleft > cnt) ? (rxleft - cnt) : 1;
3790 continue;
3791 }
3792
3793 /* Try doing single read if we can */
3794 if (dhd_readahead && bus->nextlen) {
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07003795 u16 nextlen = bus->nextlen;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003796 bus->nextlen = 0;
3797
3798 if (bus->bus == SPI_BUS) {
3799 rdlen = len = nextlen;
3800 } else {
3801 rdlen = len = nextlen << 4;
3802
3803 /* Pad read to blocksize for efficiency */
3804 if (bus->roundup && bus->blocksize
3805 && (rdlen > bus->blocksize)) {
3806 pad =
3807 bus->blocksize -
3808 (rdlen % bus->blocksize);
3809 if ((pad <= bus->roundup)
3810 && (pad < bus->blocksize)
3811 && ((rdlen + pad + firstread) <
3812 MAX_RX_DATASZ))
3813 rdlen += pad;
3814 } else if (rdlen % DHD_SDALIGN) {
3815 rdlen +=
3816 DHD_SDALIGN - (rdlen % DHD_SDALIGN);
3817 }
3818 }
3819
3820 /* We use bus->rxctl buffer in WinXP for initial
3821 * control pkt receives.
3822 * Later we use buffer-poll for data as well
3823 * as control packets.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003824 * This is required because dhd receives full
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003825 * frame in gSPI unlike SDIO.
3826 * After the frame is received we have to
3827 * distinguish whether it is data
3828 * or non-data frame.
3829 */
3830 /* Allocate a packet buffer */
3831 dhd_os_sdlock_rxq(bus->dhd);
Roland Vossen67ad48b2011-06-01 13:45:51 +02003832 pkt = brcmu_pkt_buf_get_skb(rdlen + DHD_SDALIGN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003833 if (!pkt) {
3834 if (bus->bus == SPI_BUS) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003835 bus->usebufpool = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003836 bus->rxctl = bus->rxbuf;
3837 if (dhd_alignctl) {
3838 bus->rxctl += firstread;
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07003839 pad = ((unsigned long)bus->rxctl %
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003840 DHD_SDALIGN);
3841 if (pad)
3842 bus->rxctl +=
3843 (DHD_SDALIGN - pad);
3844 bus->rxctl -= firstread;
3845 }
3846 ASSERT(bus->rxctl >= bus->rxbuf);
3847 rxbuf = bus->rxctl;
3848 /* Read the entire frame */
Grant Grundler4b455e02011-05-04 09:59:47 -07003849 sdret = bcmsdh_recv_buf(bus,
3850 bcmsdh_cur_sbwad(sdh),
3851 SDIO_FUNC_2, F2SYNC,
3852 rxbuf, rdlen,
3853 NULL, NULL, NULL);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003854 bus->f2rxdata++;
Roland Vossenb7ef2a92011-05-03 11:35:02 +02003855 ASSERT(sdret != -BCME_PENDING);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003856
3857 /* Control frame failures need
3858 retransmission */
3859 if (sdret < 0) {
3860 DHD_ERROR(("%s: read %d control bytes failed: %d\n",
3861 __func__,
3862 rdlen, sdret));
3863 /* dhd.rx_ctlerrs is higher */
3864 bus->rxc_errors++;
3865 dhd_os_sdunlock_rxq(bus->dhd);
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003866 dhdsdio_rxfail(bus, true,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003867 (bus->bus ==
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003868 SPI_BUS) ? false
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003869 : true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003870 continue;
3871 }
3872 } else {
3873 /* Give up on data,
3874 request rtx of events */
Arend van Sprielcda64a52011-05-10 22:25:33 +02003875 DHD_ERROR(("%s (nextlen): "
Roland Vossen67ad48b2011-06-01 13:45:51 +02003876 "brcmu_pkt_buf_get_skb "
3877 "failed:"
Arend van Sprielcda64a52011-05-10 22:25:33 +02003878 " len %d rdlen %d expected"
3879 " rxseq %d\n", __func__,
3880 len, rdlen, rxseq));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003881 /* Just go try again w/normal
3882 header read */
3883 dhd_os_sdunlock_rxq(bus->dhd);
3884 continue;
3885 }
3886 } else {
3887 if (bus->bus == SPI_BUS)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003888 bus->usebufpool = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003889
Arend van Spriel54991ad2010-11-23 14:06:24 +01003890 ASSERT(!(pkt->prev));
Arend van Spriel3c9d4c32011-03-02 21:18:48 +01003891 PKTALIGN(pkt, rdlen, DHD_SDALIGN);
Arend van Spriel54991ad2010-11-23 14:06:24 +01003892 rxbuf = (u8 *) (pkt->data);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003893 /* Read the entire frame */
Grant Grundler4b455e02011-05-04 09:59:47 -07003894 sdret = bcmsdh_recv_buf(bus,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003895 bcmsdh_cur_sbwad(sdh),
3896 SDIO_FUNC_2, F2SYNC,
Grant Grundler4b455e02011-05-04 09:59:47 -07003897 rxbuf, rdlen,
3898 pkt, NULL, NULL);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003899 bus->f2rxdata++;
Roland Vossenb7ef2a92011-05-03 11:35:02 +02003900 ASSERT(sdret != -BCME_PENDING);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003901
3902 if (sdret < 0) {
3903 DHD_ERROR(("%s (nextlen): read %d bytes failed: %d\n",
3904 __func__, rdlen, sdret));
Roland Vossen67ad48b2011-06-01 13:45:51 +02003905 brcmu_pkt_buf_free_skb(pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003906 bus->dhd->rx_errors++;
3907 dhd_os_sdunlock_rxq(bus->dhd);
3908 /* Force retry w/normal header read.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003909 * Don't attempt NAK for
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003910 * gSPI
3911 */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003912 dhdsdio_rxfail(bus, true,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003913 (bus->bus ==
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003914 SPI_BUS) ? false :
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003915 true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003916 continue;
3917 }
3918 }
3919 dhd_os_sdunlock_rxq(bus->dhd);
3920
3921 /* Now check the header */
Stanislav Fomichev02160692011-02-15 01:05:10 +03003922 memcpy(bus->rxhdr, rxbuf, SDPCM_HDRLEN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003923
3924 /* Extract hardware header fields */
Stanislav Fomichev56dfe3c2011-02-20 21:43:33 +03003925 len = get_unaligned_le16(bus->rxhdr);
3926 check = get_unaligned_le16(bus->rxhdr + sizeof(u16));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003927
3928 /* All zeros means readahead info was bad */
3929 if (!(len | check)) {
3930 DHD_INFO(("%s (nextlen): read zeros in HW "
3931 "header???\n", __func__));
Grant Grundler4b455e02011-05-04 09:59:47 -07003932 dhdsdio_pktfree2(bus, pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003933 continue;
3934 }
3935
3936 /* Validate check bytes */
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07003937 if ((u16)~(len ^ check)) {
Grant Grundler4b455e02011-05-04 09:59:47 -07003938 DHD_ERROR(("%s (nextlen): HW hdr error:"
3939 " nextlen/len/check"
3940 " 0x%04x/0x%04x/0x%04x\n",
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003941 __func__, nextlen, len, check));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003942 bus->rx_badhdr++;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003943 dhdsdio_rxfail(bus, false, false);
Grant Grundler4b455e02011-05-04 09:59:47 -07003944 dhdsdio_pktfree2(bus, pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003945 continue;
3946 }
3947
3948 /* Validate frame length */
3949 if (len < SDPCM_HDRLEN) {
3950 DHD_ERROR(("%s (nextlen): HW hdr length "
3951 "invalid: %d\n", __func__, len));
Grant Grundler4b455e02011-05-04 09:59:47 -07003952 dhdsdio_pktfree2(bus, pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003953 continue;
3954 }
3955
3956 /* Check for consistency withreadahead info */
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07003957 len_consistent = (nextlen != (roundup(len, 16) >> 4));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003958 if (len_consistent) {
3959 /* Mismatch, force retry w/normal
3960 header (may be >4K) */
Grant Grundler4b455e02011-05-04 09:59:47 -07003961 DHD_ERROR(("%s (nextlen): mismatch, "
3962 "nextlen %d len %d rnd %d; "
3963 "expected rxseq %d\n",
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003964 __func__, nextlen,
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07003965 len, roundup(len, 16), rxseq));
Grant Grundler4b455e02011-05-04 09:59:47 -07003966 dhdsdio_rxfail(bus, true, (bus->bus != SPI_BUS));
3967 dhdsdio_pktfree2(bus, pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003968 continue;
3969 }
3970
3971 /* Extract software header fields */
Grant Grundler4b455e02011-05-04 09:59:47 -07003972 chan = SDPCM_PACKET_CHANNEL(
3973 &bus->rxhdr[SDPCM_FRAMETAG_LEN]);
3974 seq = SDPCM_PACKET_SEQUENCE(
3975 &bus->rxhdr[SDPCM_FRAMETAG_LEN]);
3976 doff = SDPCM_DOFFSET_VALUE(
3977 &bus->rxhdr[SDPCM_FRAMETAG_LEN]);
3978 txmax = SDPCM_WINDOW_VALUE(
3979 &bus->rxhdr[SDPCM_FRAMETAG_LEN]);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003980
3981 bus->nextlen =
3982 bus->rxhdr[SDPCM_FRAMETAG_LEN +
3983 SDPCM_NEXTLEN_OFFSET];
3984 if ((bus->nextlen << 4) > MAX_RX_DATASZ) {
3985 DHD_INFO(("%s (nextlen): got frame w/nextlen too large" " (%d), seq %d\n",
3986 __func__, bus->nextlen, seq));
3987 bus->nextlen = 0;
3988 }
3989
3990 bus->dhd->rx_readahead_cnt++;
Grant Grundler4b455e02011-05-04 09:59:47 -07003991
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003992 /* Handle Flow Control */
Grant Grundler4b455e02011-05-04 09:59:47 -07003993 fcbits = SDPCM_FCMASK_VALUE(
3994 &bus->rxhdr[SDPCM_FRAMETAG_LEN]);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003995
Grant Grundler4b455e02011-05-04 09:59:47 -07003996 if (bus->flowcontrol != fcbits) {
3997 if (~bus->flowcontrol & fcbits)
3998 bus->fc_xoff++;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003999
Grant Grundler4b455e02011-05-04 09:59:47 -07004000 if (bus->flowcontrol & ~fcbits)
4001 bus->fc_xon++;
4002
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004003 bus->fc_rcvd++;
4004 bus->flowcontrol = fcbits;
4005 }
4006
4007 /* Check and update sequence number */
4008 if (rxseq != seq) {
4009 DHD_INFO(("%s (nextlen): rx_seq %d, expected "
4010 "%d\n", __func__, seq, rxseq));
4011 bus->rx_badseq++;
4012 rxseq = seq;
4013 }
4014
4015 /* Check window for sanity */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004016 if ((u8) (txmax - bus->tx_seq) > 0x40) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004017 DHD_ERROR(("%s: got unlikely tx max %d with "
4018 "tx_seq %d\n",
4019 __func__, txmax, bus->tx_seq));
4020 txmax = bus->tx_seq + 2;
4021 }
4022 bus->tx_max = txmax;
4023
4024#ifdef DHD_DEBUG
Arend van Spriel34227312011-05-10 22:25:32 +02004025 if (DHD_BYTES_ON() && DHD_DATA_ON()) {
4026 printk(KERN_DEBUG "Rx Data:\n");
4027 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
4028 rxbuf, len);
4029 } else if (DHD_HDRS_ON()) {
4030 printk(KERN_DEBUG "RxHdr:\n");
4031 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
4032 bus->rxhdr, SDPCM_HDRLEN);
4033 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004034#endif
4035
4036 if (chan == SDPCM_CONTROL_CHANNEL) {
4037 if (bus->bus == SPI_BUS) {
4038 dhdsdio_read_control(bus, rxbuf, len,
4039 doff);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004040 } else {
4041 DHD_ERROR(("%s (nextlen): readahead on control" " packet %d?\n",
4042 __func__, seq));
4043 /* Force retry w/normal header read */
4044 bus->nextlen = 0;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004045 dhdsdio_rxfail(bus, false, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004046 }
Grant Grundler4b455e02011-05-04 09:59:47 -07004047 dhdsdio_pktfree2(bus, pkt);
4048 continue;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004049 }
4050
4051 if ((bus->bus == SPI_BUS) && !bus->usebufpool) {
4052 DHD_ERROR(("Received %d bytes on %d channel. Running out of " "rx pktbuf's or not yet malloced.\n",
4053 len, chan));
4054 continue;
4055 }
4056
4057 /* Validate data offset */
4058 if ((doff < SDPCM_HDRLEN) || (doff > len)) {
4059 DHD_ERROR(("%s (nextlen): bad data offset %d: HW len %d min %d\n",
4060 __func__, doff, len, SDPCM_HDRLEN));
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004061 dhdsdio_rxfail(bus, false, false);
Grant Grundler4b455e02011-05-04 09:59:47 -07004062 dhdsdio_pktfree2(bus, pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004063 continue;
4064 }
4065
4066 /* All done with this one -- now deliver the packet */
4067 goto deliver;
4068 }
4069 /* gSPI frames should not be handled in fractions */
4070 if (bus->bus == SPI_BUS)
4071 break;
4072
4073 /* Read frame header (hardware and software) */
Grant Grundler4b455e02011-05-04 09:59:47 -07004074 sdret = bcmsdh_recv_buf(bus, bcmsdh_cur_sbwad(sdh),
4075 SDIO_FUNC_2, F2SYNC, bus->rxhdr, firstread,
4076 NULL, NULL, NULL);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004077 bus->f2rxhdrs++;
Roland Vossenb7ef2a92011-05-03 11:35:02 +02004078 ASSERT(sdret != -BCME_PENDING);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004079
4080 if (sdret < 0) {
4081 DHD_ERROR(("%s: RXHEADER FAILED: %d\n", __func__,
4082 sdret));
4083 bus->rx_hdrfail++;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004084 dhdsdio_rxfail(bus, true, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004085 continue;
4086 }
4087#ifdef DHD_DEBUG
Arend van Spriel34227312011-05-10 22:25:32 +02004088 if (DHD_BYTES_ON() || DHD_HDRS_ON()) {
4089 printk(KERN_DEBUG "RxHdr:\n");
4090 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
4091 bus->rxhdr, SDPCM_HDRLEN);
4092 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004093#endif
4094
4095 /* Extract hardware header fields */
Stanislav Fomichev56dfe3c2011-02-20 21:43:33 +03004096 len = get_unaligned_le16(bus->rxhdr);
4097 check = get_unaligned_le16(bus->rxhdr + sizeof(u16));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004098
4099 /* All zeros means no more frames */
4100 if (!(len | check)) {
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004101 *finished = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004102 break;
4103 }
4104
4105 /* Validate check bytes */
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07004106 if ((u16) ~(len ^ check)) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004107 DHD_ERROR(("%s: HW hdr err: len/check 0x%04x/0x%04x\n",
4108 __func__, len, check));
4109 bus->rx_badhdr++;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004110 dhdsdio_rxfail(bus, false, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004111 continue;
4112 }
4113
4114 /* Validate frame length */
4115 if (len < SDPCM_HDRLEN) {
4116 DHD_ERROR(("%s: HW hdr length invalid: %d\n",
4117 __func__, len));
4118 continue;
4119 }
4120
4121 /* Extract software header fields */
4122 chan = SDPCM_PACKET_CHANNEL(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
4123 seq = SDPCM_PACKET_SEQUENCE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
4124 doff = SDPCM_DOFFSET_VALUE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
4125 txmax = SDPCM_WINDOW_VALUE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
4126
4127 /* Validate data offset */
4128 if ((doff < SDPCM_HDRLEN) || (doff > len)) {
4129 DHD_ERROR(("%s: Bad data offset %d: HW len %d, min %d "
4130 "seq %d\n",
4131 __func__, doff, len, SDPCM_HDRLEN, seq));
4132 bus->rx_badhdr++;
4133 ASSERT(0);
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004134 dhdsdio_rxfail(bus, false, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004135 continue;
4136 }
4137
4138 /* Save the readahead length if there is one */
4139 bus->nextlen =
4140 bus->rxhdr[SDPCM_FRAMETAG_LEN + SDPCM_NEXTLEN_OFFSET];
4141 if ((bus->nextlen << 4) > MAX_RX_DATASZ) {
4142 DHD_INFO(("%s (nextlen): got frame w/nextlen too large "
4143 "(%d), seq %d\n",
4144 __func__, bus->nextlen, seq));
4145 bus->nextlen = 0;
4146 }
4147
4148 /* Handle Flow Control */
4149 fcbits = SDPCM_FCMASK_VALUE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
4150
Grant Grundler4b455e02011-05-04 09:59:47 -07004151 if (bus->flowcontrol != fcbits) {
4152 if (~bus->flowcontrol & fcbits)
4153 bus->fc_xoff++;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004154
Grant Grundler4b455e02011-05-04 09:59:47 -07004155 if (bus->flowcontrol & ~fcbits)
4156 bus->fc_xon++;
4157
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004158 bus->fc_rcvd++;
4159 bus->flowcontrol = fcbits;
4160 }
4161
4162 /* Check and update sequence number */
4163 if (rxseq != seq) {
4164 DHD_INFO(("%s: rx_seq %d, expected %d\n", __func__,
4165 seq, rxseq));
4166 bus->rx_badseq++;
4167 rxseq = seq;
4168 }
4169
4170 /* Check window for sanity */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004171 if ((u8) (txmax - bus->tx_seq) > 0x40) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004172 DHD_ERROR(("%s: unlikely tx max %d with tx_seq %d\n",
4173 __func__, txmax, bus->tx_seq));
4174 txmax = bus->tx_seq + 2;
4175 }
4176 bus->tx_max = txmax;
4177
4178 /* Call a separate function for control frames */
4179 if (chan == SDPCM_CONTROL_CHANNEL) {
4180 dhdsdio_read_control(bus, bus->rxhdr, len, doff);
4181 continue;
4182 }
4183
4184 ASSERT((chan == SDPCM_DATA_CHANNEL)
4185 || (chan == SDPCM_EVENT_CHANNEL)
4186 || (chan == SDPCM_TEST_CHANNEL)
4187 || (chan == SDPCM_GLOM_CHANNEL));
4188
4189 /* Length to read */
4190 rdlen = (len > firstread) ? (len - firstread) : 0;
4191
4192 /* May pad read to blocksize for efficiency */
4193 if (bus->roundup && bus->blocksize &&
4194 (rdlen > bus->blocksize)) {
4195 pad = bus->blocksize - (rdlen % bus->blocksize);
4196 if ((pad <= bus->roundup) && (pad < bus->blocksize) &&
4197 ((rdlen + pad + firstread) < MAX_RX_DATASZ))
4198 rdlen += pad;
4199 } else if (rdlen % DHD_SDALIGN) {
4200 rdlen += DHD_SDALIGN - (rdlen % DHD_SDALIGN);
4201 }
4202
4203 /* Satisfy length-alignment requirements */
4204 if (forcealign && (rdlen & (ALIGNMENT - 1)))
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07004205 rdlen = roundup(rdlen, ALIGNMENT);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004206
4207 if ((rdlen + firstread) > MAX_RX_DATASZ) {
4208 /* Too long -- skip this frame */
4209 DHD_ERROR(("%s: too long: len %d rdlen %d\n",
4210 __func__, len, rdlen));
4211 bus->dhd->rx_errors++;
4212 bus->rx_toolong++;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004213 dhdsdio_rxfail(bus, false, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004214 continue;
4215 }
4216
4217 dhd_os_sdlock_rxq(bus->dhd);
Roland Vossen67ad48b2011-06-01 13:45:51 +02004218 pkt = brcmu_pkt_buf_get_skb(rdlen + firstread + DHD_SDALIGN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004219 if (!pkt) {
4220 /* Give up on data, request rtx of events */
Roland Vossen67ad48b2011-06-01 13:45:51 +02004221 DHD_ERROR(("%s: brcmu_pkt_buf_get_skb failed: rdlen %d"
4222 " chan %d\n", __func__, rdlen, chan));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004223 bus->dhd->rx_dropped++;
4224 dhd_os_sdunlock_rxq(bus->dhd);
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004225 dhdsdio_rxfail(bus, false, RETRYCHAN(chan));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004226 continue;
4227 }
4228 dhd_os_sdunlock_rxq(bus->dhd);
4229
Arend van Spriel54991ad2010-11-23 14:06:24 +01004230 ASSERT(!(pkt->prev));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004231
4232 /* Leave room for what we already read, and align remainder */
Arend van Spriel54991ad2010-11-23 14:06:24 +01004233 ASSERT(firstread < pkt->len);
Arend van Sprielc303ecb2010-11-18 20:46:43 +01004234 skb_pull(pkt, firstread);
Arend van Spriel3c9d4c32011-03-02 21:18:48 +01004235 PKTALIGN(pkt, rdlen, DHD_SDALIGN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004236
4237 /* Read the remaining frame data */
Grant Grundler4b455e02011-05-04 09:59:47 -07004238 sdret = bcmsdh_recv_buf(bus, bcmsdh_cur_sbwad(sdh), SDIO_FUNC_2,
Arend van Spriel54991ad2010-11-23 14:06:24 +01004239 F2SYNC, ((u8 *) (pkt->data)), rdlen,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004240 pkt, NULL, NULL);
4241 bus->f2rxdata++;
Roland Vossenb7ef2a92011-05-03 11:35:02 +02004242 ASSERT(sdret != -BCME_PENDING);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004243
4244 if (sdret < 0) {
4245 DHD_ERROR(("%s: read %d %s bytes failed: %d\n",
4246 __func__, rdlen,
4247 ((chan ==
4248 SDPCM_EVENT_CHANNEL) ? "event" : ((chan ==
4249 SDPCM_DATA_CHANNEL)
4250 ? "data" : "test")),
4251 sdret));
4252 dhd_os_sdlock_rxq(bus->dhd);
Roland Vossen67ad48b2011-06-01 13:45:51 +02004253 brcmu_pkt_buf_free_skb(pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004254 dhd_os_sdunlock_rxq(bus->dhd);
4255 bus->dhd->rx_errors++;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004256 dhdsdio_rxfail(bus, true, RETRYCHAN(chan));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004257 continue;
4258 }
4259
4260 /* Copy the already-read portion */
Arend van Sprielc303ecb2010-11-18 20:46:43 +01004261 skb_push(pkt, firstread);
Stanislav Fomichev02160692011-02-15 01:05:10 +03004262 memcpy(pkt->data, bus->rxhdr, firstread);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004263
4264#ifdef DHD_DEBUG
Arend van Spriel34227312011-05-10 22:25:32 +02004265 if (DHD_BYTES_ON() && DHD_DATA_ON()) {
4266 printk(KERN_DEBUG "Rx Data:\n");
4267 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
4268 pkt->data, len);
4269 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004270#endif
4271
4272deliver:
4273 /* Save superframe descriptor and allocate packet frame */
4274 if (chan == SDPCM_GLOM_CHANNEL) {
4275 if (SDPCM_GLOMDESC(&bus->rxhdr[SDPCM_FRAMETAG_LEN])) {
4276 DHD_GLOM(("%s: glom descriptor, %d bytes:\n",
4277 __func__, len));
4278#ifdef DHD_DEBUG
4279 if (DHD_GLOM_ON()) {
Arend van Spriel34227312011-05-10 22:25:32 +02004280 printk(KERN_DEBUG "Glom Data:\n");
4281 print_hex_dump_bytes("",
4282 DUMP_PREFIX_OFFSET,
4283 pkt->data, len);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004284 }
4285#endif
Arend van Spriel2cb8ada2010-11-18 20:46:44 +01004286 __skb_trim(pkt, len);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004287 ASSERT(doff == SDPCM_HDRLEN);
Arend van Sprielc303ecb2010-11-18 20:46:43 +01004288 skb_pull(pkt, SDPCM_HDRLEN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004289 bus->glomd = pkt;
4290 } else {
4291 DHD_ERROR(("%s: glom superframe w/o "
4292 "descriptor!\n", __func__));
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004293 dhdsdio_rxfail(bus, false, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004294 }
4295 continue;
4296 }
4297
4298 /* Fill in packet len and prio, deliver upward */
Arend van Spriel2cb8ada2010-11-18 20:46:44 +01004299 __skb_trim(pkt, len);
Arend van Sprielc303ecb2010-11-18 20:46:43 +01004300 skb_pull(pkt, doff);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004301
4302#ifdef SDTEST
4303 /* Test channel packets are processed separately */
4304 if (chan == SDPCM_TEST_CHANNEL) {
4305 dhdsdio_testrcv(bus, pkt, seq);
4306 continue;
4307 }
4308#endif /* SDTEST */
4309
Arend van Spriel54991ad2010-11-23 14:06:24 +01004310 if (pkt->len == 0) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004311 dhd_os_sdlock_rxq(bus->dhd);
Roland Vossen67ad48b2011-06-01 13:45:51 +02004312 brcmu_pkt_buf_free_skb(pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004313 dhd_os_sdunlock_rxq(bus->dhd);
4314 continue;
4315 } else if (dhd_prot_hdrpull(bus->dhd, &ifidx, pkt) != 0) {
4316 DHD_ERROR(("%s: rx protocol error\n", __func__));
4317 dhd_os_sdlock_rxq(bus->dhd);
Roland Vossen67ad48b2011-06-01 13:45:51 +02004318 brcmu_pkt_buf_free_skb(pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004319 dhd_os_sdunlock_rxq(bus->dhd);
4320 bus->dhd->rx_errors++;
4321 continue;
4322 }
4323
4324 /* Unlock during rx call */
4325 dhd_os_sdunlock(bus->dhd);
4326 dhd_rx_frame(bus->dhd, ifidx, pkt, 1);
4327 dhd_os_sdlock(bus->dhd);
4328 }
4329 rxcount = maxframes - rxleft;
4330#ifdef DHD_DEBUG
4331 /* Message if we hit the limit */
4332 if (!rxleft && !sdtest)
4333 DHD_DATA(("%s: hit rx limit of %d frames\n", __func__,
4334 maxframes));
4335 else
4336#endif /* DHD_DEBUG */
4337 DHD_DATA(("%s: processed %d frames\n", __func__, rxcount));
4338 /* Back off rxseq if awaiting rtx, update rx_seq */
4339 if (bus->rxskip)
4340 rxseq--;
4341 bus->rx_seq = rxseq;
4342
4343 return rxcount;
4344}
4345
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07004346static u32 dhdsdio_hostmail(dhd_bus_t *bus)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004347{
Franky Lin597600a2011-06-01 13:45:39 +02004348 struct sdpcmd_regs *regs = bus->regs;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07004349 u32 intstatus = 0;
4350 u32 hmb_data;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004351 u8 fcbits;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004352 uint retries = 0;
4353
4354 DHD_TRACE(("%s: Enter\n", __func__));
4355
4356 /* Read mailbox data and ack that we did so */
4357 R_SDREG(hmb_data, &regs->tohostmailboxdata, retries);
4358 if (retries <= retry_limit)
4359 W_SDREG(SMB_INT_ACK, &regs->tosbmailbox, retries);
4360 bus->f1regdata += 2;
4361
4362 /* Dongle recomposed rx frames, accept them again */
4363 if (hmb_data & HMB_DATA_NAKHANDLED) {
4364 DHD_INFO(("Dongle reports NAK handled, expect rtx of %d\n",
4365 bus->rx_seq));
4366 if (!bus->rxskip)
4367 DHD_ERROR(("%s: unexpected NAKHANDLED!\n", __func__));
4368
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004369 bus->rxskip = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004370 intstatus |= I_HMB_FRAME_IND;
4371 }
4372
4373 /*
4374 * DEVREADY does not occur with gSPI.
4375 */
4376 if (hmb_data & (HMB_DATA_DEVREADY | HMB_DATA_FWREADY)) {
4377 bus->sdpcm_ver =
4378 (hmb_data & HMB_DATA_VERSION_MASK) >>
4379 HMB_DATA_VERSION_SHIFT;
4380 if (bus->sdpcm_ver != SDPCM_PROT_VERSION)
4381 DHD_ERROR(("Version mismatch, dongle reports %d, "
4382 "expecting %d\n",
4383 bus->sdpcm_ver, SDPCM_PROT_VERSION));
4384 else
4385 DHD_INFO(("Dongle ready, protocol version %d\n",
4386 bus->sdpcm_ver));
4387 }
4388
4389 /*
4390 * Flow Control has been moved into the RX headers and this out of band
Grant Grundler4b455e02011-05-04 09:59:47 -07004391 * method isn't used any more.
4392 * remaining backward compatible with older dongles.
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004393 */
4394 if (hmb_data & HMB_DATA_FC) {
Grant Grundler4b455e02011-05-04 09:59:47 -07004395 fcbits = (hmb_data & HMB_DATA_FCDATA_MASK) >>
4396 HMB_DATA_FCDATA_SHIFT;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004397
4398 if (fcbits & ~bus->flowcontrol)
4399 bus->fc_xoff++;
Grant Grundler4b455e02011-05-04 09:59:47 -07004400
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004401 if (bus->flowcontrol & ~fcbits)
4402 bus->fc_xon++;
4403
4404 bus->fc_rcvd++;
4405 bus->flowcontrol = fcbits;
4406 }
4407
4408 /* Shouldn't be any others */
4409 if (hmb_data & ~(HMB_DATA_DEVREADY |
4410 HMB_DATA_NAKHANDLED |
4411 HMB_DATA_FC |
4412 HMB_DATA_FWREADY |
4413 HMB_DATA_FCDATA_MASK | HMB_DATA_VERSION_MASK)) {
4414 DHD_ERROR(("Unknown mailbox data content: 0x%02x\n", hmb_data));
4415 }
4416
4417 return intstatus;
4418}
4419
4420bool dhdsdio_dpc(dhd_bus_t *bus)
4421{
4422 bcmsdh_info_t *sdh = bus->sdh;
Franky Lin597600a2011-06-01 13:45:39 +02004423 struct sdpcmd_regs *regs = bus->regs;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07004424 u32 intstatus, newstatus = 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004425 uint retries = 0;
4426 uint rxlimit = dhd_rxbound; /* Rx frames to read before resched */
4427 uint txlimit = dhd_txbound; /* Tx frames to send before resched */
4428 uint framecnt = 0; /* Temporary counter of tx/rx frames */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004429 bool rxdone = true; /* Flag for no more read data */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004430 bool resched = false; /* Flag indicating resched wanted */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004431
4432 DHD_TRACE(("%s: Enter\n", __func__));
4433
4434 /* Start with leftover status bits */
4435 intstatus = bus->intstatus;
4436
4437 dhd_os_sdlock(bus->dhd);
4438
4439 /* If waiting for HTAVAIL, check status */
4440 if (bus->clkstate == CLK_PENDING) {
4441 int err;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004442 u8 clkctl, devctl = 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004443
4444#ifdef DHD_DEBUG
4445 /* Check for inconsistent device control */
4446 devctl =
4447 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL, &err);
4448 if (err) {
4449 DHD_ERROR(("%s: error reading DEVCTL: %d\n",
4450 __func__, err));
4451 bus->dhd->busstate = DHD_BUS_DOWN;
4452 } else {
4453 ASSERT(devctl & SBSDIO_DEVCTL_CA_INT_ONLY);
4454 }
4455#endif /* DHD_DEBUG */
4456
4457 /* Read CSR, if clock on switch to AVAIL, else ignore */
4458 clkctl =
4459 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
4460 &err);
4461 if (err) {
4462 DHD_ERROR(("%s: error reading CSR: %d\n", __func__,
4463 err));
4464 bus->dhd->busstate = DHD_BUS_DOWN;
4465 }
4466
4467 DHD_INFO(("DPC: PENDING, devctl 0x%02x clkctl 0x%02x\n", devctl,
4468 clkctl));
4469
4470 if (SBSDIO_HTAV(clkctl)) {
4471 devctl =
4472 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
4473 &err);
4474 if (err) {
4475 DHD_ERROR(("%s: error reading DEVCTL: %d\n",
4476 __func__, err));
4477 bus->dhd->busstate = DHD_BUS_DOWN;
4478 }
4479 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
4480 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
4481 devctl, &err);
4482 if (err) {
4483 DHD_ERROR(("%s: error writing DEVCTL: %d\n",
4484 __func__, err));
4485 bus->dhd->busstate = DHD_BUS_DOWN;
4486 }
4487 bus->clkstate = CLK_AVAIL;
4488 } else {
4489 goto clkwait;
4490 }
4491 }
4492
4493 BUS_WAKE(bus);
4494
4495 /* Make sure backplane clock is on */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004496 dhdsdio_clkctl(bus, CLK_AVAIL, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004497 if (bus->clkstate == CLK_PENDING)
4498 goto clkwait;
4499
4500 /* Pending interrupt indicates new device status */
4501 if (bus->ipend) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004502 bus->ipend = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004503 R_SDREG(newstatus, &regs->intstatus, retries);
4504 bus->f1regdata++;
4505 if (bcmsdh_regfail(bus->sdh))
4506 newstatus = 0;
4507 newstatus &= bus->hostintmask;
4508 bus->fcstate = !!(newstatus & I_HMB_FC_STATE);
4509 if (newstatus) {
4510 W_SDREG(newstatus, &regs->intstatus, retries);
4511 bus->f1regdata++;
4512 }
4513 }
4514
4515 /* Merge new bits with previous */
4516 intstatus |= newstatus;
4517 bus->intstatus = 0;
4518
4519 /* Handle flow-control change: read new state in case our ack
4520 * crossed another change interrupt. If change still set, assume
4521 * FC ON for safety, let next loop through do the debounce.
4522 */
4523 if (intstatus & I_HMB_FC_CHANGE) {
4524 intstatus &= ~I_HMB_FC_CHANGE;
4525 W_SDREG(I_HMB_FC_CHANGE, &regs->intstatus, retries);
4526 R_SDREG(newstatus, &regs->intstatus, retries);
4527 bus->f1regdata += 2;
4528 bus->fcstate =
4529 !!(newstatus & (I_HMB_FC_STATE | I_HMB_FC_CHANGE));
4530 intstatus |= (newstatus & bus->hostintmask);
4531 }
4532
4533 /* Handle host mailbox indication */
4534 if (intstatus & I_HMB_HOST_INT) {
4535 intstatus &= ~I_HMB_HOST_INT;
4536 intstatus |= dhdsdio_hostmail(bus);
4537 }
4538
4539 /* Generally don't ask for these, can get CRC errors... */
4540 if (intstatus & I_WR_OOSYNC) {
4541 DHD_ERROR(("Dongle reports WR_OOSYNC\n"));
4542 intstatus &= ~I_WR_OOSYNC;
4543 }
4544
4545 if (intstatus & I_RD_OOSYNC) {
4546 DHD_ERROR(("Dongle reports RD_OOSYNC\n"));
4547 intstatus &= ~I_RD_OOSYNC;
4548 }
4549
4550 if (intstatus & I_SBINT) {
4551 DHD_ERROR(("Dongle reports SBINT\n"));
4552 intstatus &= ~I_SBINT;
4553 }
4554
4555 /* Would be active due to wake-wlan in gSPI */
4556 if (intstatus & I_CHIPACTIVE) {
4557 DHD_INFO(("Dongle reports CHIPACTIVE\n"));
4558 intstatus &= ~I_CHIPACTIVE;
4559 }
4560
4561 /* Ignore frame indications if rxskip is set */
4562 if (bus->rxskip)
4563 intstatus &= ~I_HMB_FRAME_IND;
4564
4565 /* On frame indication, read available frames */
4566 if (PKT_AVAILABLE()) {
4567 framecnt = dhdsdio_readframes(bus, rxlimit, &rxdone);
4568 if (rxdone || bus->rxskip)
4569 intstatus &= ~I_HMB_FRAME_IND;
Greg Kroah-Hartman7068c2f2010-10-08 11:34:59 -07004570 rxlimit -= min(framecnt, rxlimit);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004571 }
4572
4573 /* Keep still-pending events for next scheduling */
4574 bus->intstatus = intstatus;
4575
4576clkwait:
4577#if defined(OOB_INTR_ONLY)
4578 bcmsdh_oob_intr_set(1);
4579#endif /* (OOB_INTR_ONLY) */
4580 /* Re-enable interrupts to detect new device events (mailbox, rx frame)
4581 * or clock availability. (Allows tx loop to check ipend if desired.)
4582 * (Unless register access seems hosed, as we may not be able to ACK...)
4583 */
4584 if (bus->intr && bus->intdis && !bcmsdh_regfail(sdh)) {
4585 DHD_INTR(("%s: enable SDIO interrupts, rxdone %d framecnt %d\n",
4586 __func__, rxdone, framecnt));
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004587 bus->intdis = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004588 bcmsdh_intr_enable(sdh);
4589 }
4590
4591 if (DATAOK(bus) && bus->ctrl_frame_stat &&
4592 (bus->clkstate == CLK_AVAIL)) {
4593 int ret, i;
4594
4595 ret =
4596 dhd_bcmsdh_send_buf(bus, bcmsdh_cur_sbwad(sdh), SDIO_FUNC_2,
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004597 F2SYNC, (u8 *) bus->ctrl_frame_buf,
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07004598 (u32) bus->ctrl_frame_len, NULL,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004599 NULL, NULL);
Roland Vossenb7ef2a92011-05-03 11:35:02 +02004600 ASSERT(ret != -BCME_PENDING);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004601
4602 if (ret < 0) {
4603 /* On failure, abort the command and
4604 terminate the frame */
4605 DHD_INFO(("%s: sdio error %d, abort command and "
4606 "terminate frame.\n", __func__, ret));
4607 bus->tx_sderrs++;
4608
4609 bcmsdh_abort(sdh, SDIO_FUNC_2);
4610
4611 bcmsdh_cfg_write(sdh, SDIO_FUNC_1,
4612 SBSDIO_FUNC1_FRAMECTRL, SFC_WF_TERM,
4613 NULL);
4614 bus->f1regdata++;
4615
4616 for (i = 0; i < 3; i++) {
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004617 u8 hi, lo;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004618 hi = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
4619 SBSDIO_FUNC1_WFRAMEBCHI,
4620 NULL);
4621 lo = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
4622 SBSDIO_FUNC1_WFRAMEBCLO,
4623 NULL);
4624 bus->f1regdata += 2;
4625 if ((hi == 0) && (lo == 0))
4626 break;
4627 }
4628
4629 }
4630 if (ret == 0)
4631 bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQUENCE_WRAP;
4632
Arend van Spriel0bef7742011-02-10 12:03:44 +01004633 DHD_INFO(("Return_dpc value is : %d\n", ret));
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004634 bus->ctrl_frame_stat = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004635 dhd_wait_event_wakeup(bus->dhd);
4636 }
4637 /* Send queued frames (limit 1 if rx may still be pending) */
4638 else if ((bus->clkstate == CLK_AVAIL) && !bus->fcstate &&
Roland Vossen67ad48b2011-06-01 13:45:51 +02004639 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) && txlimit
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004640 && DATAOK(bus)) {
Greg Kroah-Hartman7068c2f2010-10-08 11:34:59 -07004641 framecnt = rxdone ? txlimit : min(txlimit, dhd_txminmax);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004642 framecnt = dhdsdio_sendfromq(bus, framecnt);
4643 txlimit -= framecnt;
4644 }
4645
4646 /* Resched if events or tx frames are pending,
4647 else await next interrupt */
4648 /* On failed register access, all bets are off:
4649 no resched or interrupts */
4650 if ((bus->dhd->busstate == DHD_BUS_DOWN) || bcmsdh_regfail(sdh)) {
4651 DHD_ERROR(("%s: failed backplane access over SDIO, halting "
4652 "operation %d\n", __func__, bcmsdh_regfail(sdh)));
4653 bus->dhd->busstate = DHD_BUS_DOWN;
4654 bus->intstatus = 0;
4655 } else if (bus->clkstate == CLK_PENDING) {
4656 DHD_INFO(("%s: rescheduled due to CLK_PENDING awaiting "
4657 "I_CHIPACTIVE interrupt\n", __func__));
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004658 resched = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004659 } else if (bus->intstatus || bus->ipend ||
Roland Vossen67ad48b2011-06-01 13:45:51 +02004660 (!bus->fcstate && brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol)
4661 && DATAOK(bus)) || PKT_AVAILABLE()) {
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004662 resched = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004663 }
4664
4665 bus->dpc_sched = resched;
4666
4667 /* If we're done for now, turn off clock request. */
4668 if ((bus->clkstate != CLK_PENDING)
4669 && bus->idletime == DHD_IDLE_IMMEDIATE) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004670 bus->activity = false;
4671 dhdsdio_clkctl(bus, CLK_NONE, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004672 }
4673
4674 dhd_os_sdunlock(bus->dhd);
4675
4676 return resched;
4677}
4678
4679bool dhd_bus_dpc(struct dhd_bus *bus)
4680{
4681 bool resched;
4682
4683 /* Call the DPC directly. */
4684 DHD_TRACE(("Calling dhdsdio_dpc() from %s\n", __func__));
4685 resched = dhdsdio_dpc(bus);
4686
4687 return resched;
4688}
4689
4690void dhdsdio_isr(void *arg)
4691{
4692 dhd_bus_t *bus = (dhd_bus_t *) arg;
4693 bcmsdh_info_t *sdh;
4694
4695 DHD_TRACE(("%s: Enter\n", __func__));
4696
4697 if (!bus) {
4698 DHD_ERROR(("%s : bus is null pointer , exit\n", __func__));
4699 return;
4700 }
4701 sdh = bus->sdh;
4702
4703 if (bus->dhd->busstate == DHD_BUS_DOWN) {
4704 DHD_ERROR(("%s : bus is down. we have nothing to do\n",
4705 __func__));
4706 return;
4707 }
4708 /* Count the interrupt call */
4709 bus->intrcount++;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004710 bus->ipend = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004711
4712 /* Shouldn't get this interrupt if we're sleeping? */
4713 if (bus->sleeping) {
4714 DHD_ERROR(("INTERRUPT WHILE SLEEPING??\n"));
4715 return;
4716 }
4717
4718 /* Disable additional interrupts (is this needed now)? */
4719 if (bus->intr)
4720 DHD_INTR(("%s: disable SDIO interrupts\n", __func__));
4721 else
4722 DHD_ERROR(("dhdsdio_isr() w/o interrupt configured!\n"));
4723
4724 bcmsdh_intr_disable(sdh);
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004725 bus->intdis = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004726
4727#if defined(SDIO_ISR_THREAD)
4728 DHD_TRACE(("Calling dhdsdio_dpc() from %s\n", __func__));
4729 while (dhdsdio_dpc(bus))
4730 ;
4731#else
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004732 bus->dpc_sched = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004733 dhd_sched_dpc(bus->dhd);
4734#endif
4735
4736}
4737
4738#ifdef SDTEST
4739static void dhdsdio_pktgen_init(dhd_bus_t *bus)
4740{
4741 /* Default to specified length, or full range */
4742 if (dhd_pktgen_len) {
Greg Kroah-Hartman7068c2f2010-10-08 11:34:59 -07004743 bus->pktgen_maxlen = min(dhd_pktgen_len, MAX_PKTGEN_LEN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004744 bus->pktgen_minlen = bus->pktgen_maxlen;
4745 } else {
4746 bus->pktgen_maxlen = MAX_PKTGEN_LEN;
4747 bus->pktgen_minlen = 0;
4748 }
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07004749 bus->pktgen_len = (u16) bus->pktgen_minlen;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004750
4751 /* Default to per-watchdog burst with 10s print time */
4752 bus->pktgen_freq = 1;
4753 bus->pktgen_print = 10000 / dhd_watchdog_ms;
4754 bus->pktgen_count = (dhd_pktgen * dhd_watchdog_ms + 999) / 1000;
4755
4756 /* Default to echo mode */
4757 bus->pktgen_mode = DHD_PKTGEN_ECHO;
4758 bus->pktgen_stop = 1;
4759}
4760
4761static void dhdsdio_pktgen(dhd_bus_t *bus)
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 uint pktcount;
4766 uint fillbyte;
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07004767 u16 len;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004768
4769 /* Display current count if appropriate */
4770 if (bus->pktgen_print && (++bus->pktgen_ptick >= bus->pktgen_print)) {
4771 bus->pktgen_ptick = 0;
Arend van Spriel0bef7742011-02-10 12:03:44 +01004772 printk(KERN_DEBUG "%s: send attempts %d rcvd %d\n",
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004773 __func__, bus->pktgen_sent, bus->pktgen_rcvd);
4774 }
4775
4776 /* For recv mode, just make sure dongle has started sending */
4777 if (bus->pktgen_mode == DHD_PKTGEN_RECV) {
4778 if (!bus->pktgen_rcvd)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004779 dhdsdio_sdtest_set(bus, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004780 return;
4781 }
4782
4783 /* Otherwise, generate or request the specified number of packets */
4784 for (pktcount = 0; pktcount < bus->pktgen_count; pktcount++) {
4785 /* Stop if total has been reached */
4786 if (bus->pktgen_total
4787 && (bus->pktgen_sent >= bus->pktgen_total)) {
4788 bus->pktgen_count = 0;
4789 break;
4790 }
4791
4792 /* Allocate an appropriate-sized packet */
4793 len = bus->pktgen_len;
Roland Vossen67ad48b2011-06-01 13:45:51 +02004794 pkt = brcmu_pkt_buf_get_skb(
Jason Cooper9b890322010-09-30 15:15:39 -04004795 (len + SDPCM_HDRLEN + SDPCM_TEST_HDRLEN + DHD_SDALIGN),
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004796 true);
Jason Cooper9b890322010-09-30 15:15:39 -04004797 if (!pkt) {
Roland Vossen67ad48b2011-06-01 13:45:51 +02004798 DHD_ERROR(("%s: brcmu_pkt_buf_get_skb failed!\n",
4799 __func__));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004800 break;
4801 }
Arend van Spriel3c9d4c32011-03-02 21:18:48 +01004802 PKTALIGN(pkt, (len + SDPCM_HDRLEN + SDPCM_TEST_HDRLEN),
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004803 DHD_SDALIGN);
Arend van Spriel54991ad2010-11-23 14:06:24 +01004804 data = (u8 *) (pkt->data) + SDPCM_HDRLEN;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004805
4806 /* Write test header cmd and extra based on mode */
4807 switch (bus->pktgen_mode) {
4808 case DHD_PKTGEN_ECHO:
4809 *data++ = SDPCM_TEST_ECHOREQ;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004810 *data++ = (u8) bus->pktgen_sent;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004811 break;
4812
4813 case DHD_PKTGEN_SEND:
4814 *data++ = SDPCM_TEST_DISCARD;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004815 *data++ = (u8) bus->pktgen_sent;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004816 break;
4817
4818 case DHD_PKTGEN_RXBURST:
4819 *data++ = SDPCM_TEST_BURST;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004820 *data++ = (u8) bus->pktgen_count;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004821 break;
4822
4823 default:
4824 DHD_ERROR(("Unrecognized pktgen mode %d\n",
4825 bus->pktgen_mode));
Roland Vossen67ad48b2011-06-01 13:45:51 +02004826 brcmu_pkt_buf_free_skb(pkt, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004827 bus->pktgen_count = 0;
4828 return;
4829 }
4830
4831 /* Write test header length field */
4832 *data++ = (len >> 0);
4833 *data++ = (len >> 8);
4834
4835 /* Then fill in the remainder -- N/A for burst,
4836 but who cares... */
4837 for (fillbyte = 0; fillbyte < len; fillbyte++)
4838 *data++ =
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004839 SDPCM_TEST_FILL(fillbyte, (u8) bus->pktgen_sent);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004840
4841#ifdef DHD_DEBUG
4842 if (DHD_BYTES_ON() && DHD_DATA_ON()) {
Arend van Spriel54991ad2010-11-23 14:06:24 +01004843 data = (u8 *) (pkt->data) + SDPCM_HDRLEN;
Arend van Spriel34227312011-05-10 22:25:32 +02004844 printk(KERN_DEBUG "dhdsdio_pktgen: Tx Data:\n");
4845 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, data,
4846 pkt->len - SDPCM_HDRLEN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004847 }
4848#endif
4849
4850 /* Send it */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004851 if (dhdsdio_txpkt(bus, pkt, SDPCM_TEST_CHANNEL, true)) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004852 bus->pktgen_fail++;
4853 if (bus->pktgen_stop
4854 && bus->pktgen_stop == bus->pktgen_fail)
4855 bus->pktgen_count = 0;
4856 }
4857 bus->pktgen_sent++;
4858
4859 /* Bump length if not fixed, wrap at max */
4860 if (++bus->pktgen_len > bus->pktgen_maxlen)
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07004861 bus->pktgen_len = (u16) bus->pktgen_minlen;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004862
4863 /* Special case for burst mode: just send one request! */
4864 if (bus->pktgen_mode == DHD_PKTGEN_RXBURST)
4865 break;
4866 }
4867}
4868
4869static void dhdsdio_sdtest_set(dhd_bus_t *bus, bool start)
4870{
Arend van Sprielc26b1372010-11-23 14:06:23 +01004871 struct sk_buff *pkt;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004872 u8 *data;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004873
4874 /* Allocate the packet */
Roland Vossen67ad48b2011-06-01 13:45:51 +02004875 pkt = brcmu_pkt_buf_get_skb(SDPCM_HDRLEN + SDPCM_TEST_HDRLEN +
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004876 DHD_SDALIGN, true);
Jason Cooper9b890322010-09-30 15:15:39 -04004877 if (!pkt) {
Roland Vossen67ad48b2011-06-01 13:45:51 +02004878 DHD_ERROR(("%s: brcmu_pkt_buf_get_skb failed!\n", __func__));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004879 return;
4880 }
Arend van Spriel3c9d4c32011-03-02 21:18:48 +01004881 PKTALIGN(pkt, (SDPCM_HDRLEN + SDPCM_TEST_HDRLEN), DHD_SDALIGN);
Arend van Spriel54991ad2010-11-23 14:06:24 +01004882 data = (u8 *) (pkt->data) + SDPCM_HDRLEN;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004883
4884 /* Fill in the test header */
4885 *data++ = SDPCM_TEST_SEND;
4886 *data++ = start;
4887 *data++ = (bus->pktgen_maxlen >> 0);
4888 *data++ = (bus->pktgen_maxlen >> 8);
4889
4890 /* Send it */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004891 if (dhdsdio_txpkt(bus, pkt, SDPCM_TEST_CHANNEL, true))
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004892 bus->pktgen_fail++;
4893}
4894
Arend van Sprielc26b1372010-11-23 14:06:23 +01004895static void dhdsdio_testrcv(dhd_bus_t *bus, struct sk_buff *pkt, uint seq)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004896{
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004897 u8 *data;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004898 uint pktlen;
4899
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004900 u8 cmd;
4901 u8 extra;
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07004902 u16 len;
4903 u16 offset;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004904
4905 /* Check for min length */
Arend van Spriel54991ad2010-11-23 14:06:24 +01004906 pktlen = pkt->len;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004907 if (pktlen < SDPCM_TEST_HDRLEN) {
4908 DHD_ERROR(("dhdsdio_restrcv: toss runt frame, pktlen %d\n",
4909 pktlen));
Roland Vossen67ad48b2011-06-01 13:45:51 +02004910 brcmu_pkt_buf_free_skb(pkt, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004911 return;
4912 }
4913
4914 /* Extract header fields */
Arend van Spriel54991ad2010-11-23 14:06:24 +01004915 data = pkt->data;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004916 cmd = *data++;
4917 extra = *data++;
4918 len = *data++;
4919 len += *data++ << 8;
4920
4921 /* Check length for relevant commands */
4922 if (cmd == SDPCM_TEST_DISCARD || cmd == SDPCM_TEST_ECHOREQ
4923 || cmd == SDPCM_TEST_ECHORSP) {
4924 if (pktlen != len + SDPCM_TEST_HDRLEN) {
4925 DHD_ERROR(("dhdsdio_testrcv: frame length mismatch, "
4926 "pktlen %d seq %d" " cmd %d extra %d len %d\n",
4927 pktlen, seq, cmd, extra, len));
Roland Vossen67ad48b2011-06-01 13:45:51 +02004928 brcmu_pkt_buf_free_skb(pkt, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004929 return;
4930 }
4931 }
4932
4933 /* Process as per command */
4934 switch (cmd) {
4935 case SDPCM_TEST_ECHOREQ:
4936 /* Rx->Tx turnaround ok (even on NDIS w/current
4937 implementation) */
Arend van Spriel54991ad2010-11-23 14:06:24 +01004938 *(u8 *) (pkt->data) = SDPCM_TEST_ECHORSP;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004939 if (dhdsdio_txpkt(bus, pkt, SDPCM_TEST_CHANNEL, true) == 0) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004940 bus->pktgen_sent++;
4941 } else {
4942 bus->pktgen_fail++;
Roland Vossen67ad48b2011-06-01 13:45:51 +02004943 brcmu_pkt_buf_free_skb(pkt, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004944 }
4945 bus->pktgen_rcvd++;
4946 break;
4947
4948 case SDPCM_TEST_ECHORSP:
4949 if (bus->ext_loop) {
Roland Vossen67ad48b2011-06-01 13:45:51 +02004950 brcmu_pkt_buf_free_skb(pkt, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004951 bus->pktgen_rcvd++;
4952 break;
4953 }
4954
4955 for (offset = 0; offset < len; offset++, data++) {
4956 if (*data != SDPCM_TEST_FILL(offset, extra)) {
4957 DHD_ERROR(("dhdsdio_testrcv: echo data mismatch: " "offset %d (len %d) expect 0x%02x rcvd 0x%02x\n",
4958 offset, len,
4959 SDPCM_TEST_FILL(offset, extra), *data));
4960 break;
4961 }
4962 }
Roland Vossen67ad48b2011-06-01 13:45:51 +02004963 brcmu_pkt_buf_free_skb(pkt, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004964 bus->pktgen_rcvd++;
4965 break;
4966
4967 case SDPCM_TEST_DISCARD:
Roland Vossen67ad48b2011-06-01 13:45:51 +02004968 brcmu_pkt_buf_free_skb(pkt, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004969 bus->pktgen_rcvd++;
4970 break;
4971
4972 case SDPCM_TEST_BURST:
4973 case SDPCM_TEST_SEND:
4974 default:
4975 DHD_INFO(("dhdsdio_testrcv: unsupported or unknown command, "
4976 "pktlen %d seq %d" " cmd %d extra %d len %d\n",
4977 pktlen, seq, cmd, extra, len));
Roland Vossen67ad48b2011-06-01 13:45:51 +02004978 brcmu_pkt_buf_free_skb(pkt, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004979 break;
4980 }
4981
4982 /* For recv mode, stop at limie (and tell dongle to stop sending) */
4983 if (bus->pktgen_mode == DHD_PKTGEN_RECV) {
4984 if (bus->pktgen_total
4985 && (bus->pktgen_rcvd >= bus->pktgen_total)) {
4986 bus->pktgen_count = 0;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004987 dhdsdio_sdtest_set(bus, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004988 }
4989 }
4990}
4991#endif /* SDTEST */
4992
4993extern bool dhd_bus_watchdog(dhd_pub_t *dhdp)
4994{
4995 dhd_bus_t *bus;
4996
4997 DHD_TIMER(("%s: Enter\n", __func__));
4998
4999 bus = dhdp->bus;
5000
5001 if (bus->dhd->dongle_reset)
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005002 return false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005003
5004 /* Ignore the timer if simulating bus down */
5005 if (bus->sleeping)
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005006 return false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005007
5008 dhd_os_sdlock(bus->dhd);
5009
5010 /* Poll period: check device if appropriate. */
5011 if (bus->poll && (++bus->polltick >= bus->pollrate)) {
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07005012 u32 intstatus = 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005013
5014 /* Reset poll tick */
5015 bus->polltick = 0;
5016
5017 /* Check device if no interrupts */
5018 if (!bus->intr || (bus->intrcount == bus->lastintrs)) {
5019
5020 if (!bus->dpc_sched) {
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07005021 u8 devpend;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005022 devpend = bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_0,
Franky Lin0df46042011-06-01 13:45:40 +02005023 SDIO_CCCR_INTx,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005024 NULL);
5025 intstatus =
5026 devpend & (INTR_STATUS_FUNC1 |
5027 INTR_STATUS_FUNC2);
5028 }
5029
5030 /* If there is something, make like the ISR and
5031 schedule the DPC */
5032 if (intstatus) {
5033 bus->pollcnt++;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005034 bus->ipend = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005035 if (bus->intr)
5036 bcmsdh_intr_disable(bus->sdh);
5037
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005038 bus->dpc_sched = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005039 dhd_sched_dpc(bus->dhd);
5040
5041 }
5042 }
5043
5044 /* Update interrupt tracking */
5045 bus->lastintrs = bus->intrcount;
5046 }
5047#ifdef DHD_DEBUG
5048 /* Poll for console output periodically */
5049 if (dhdp->busstate == DHD_BUS_DATA && dhd_console_ms != 0) {
5050 bus->console.count += dhd_watchdog_ms;
5051 if (bus->console.count >= dhd_console_ms) {
5052 bus->console.count -= dhd_console_ms;
5053 /* Make sure backplane clock is on */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005054 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005055 if (dhdsdio_readconsole(bus) < 0)
5056 dhd_console_ms = 0; /* On error,
5057 stop trying */
5058 }
5059 }
5060#endif /* DHD_DEBUG */
5061
5062#ifdef SDTEST
5063 /* Generate packets if configured */
5064 if (bus->pktgen_count && (++bus->pktgen_tick >= bus->pktgen_freq)) {
5065 /* Make sure backplane clock is on */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005066 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005067 bus->pktgen_tick = 0;
5068 dhdsdio_pktgen(bus);
5069 }
5070#endif
5071
5072 /* On idle timeout clear activity flag and/or turn off clock */
5073 if ((bus->idletime > 0) && (bus->clkstate == CLK_AVAIL)) {
5074 if (++bus->idlecount >= bus->idletime) {
5075 bus->idlecount = 0;
5076 if (bus->activity) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005077 bus->activity = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005078 dhd_os_wd_timer(bus->dhd, dhd_watchdog_ms);
5079 } else {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005080 dhdsdio_clkctl(bus, CLK_NONE, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005081 }
5082 }
5083 }
5084
5085 dhd_os_sdunlock(bus->dhd);
5086
5087 return bus->ipend;
5088}
5089
5090#ifdef DHD_DEBUG
Greg Kroah-Hartman580a0bd2010-10-05 11:09:48 -07005091extern int dhd_bus_console_in(dhd_pub_t *dhdp, unsigned char *msg, uint msglen)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005092{
5093 dhd_bus_t *bus = dhdp->bus;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07005094 u32 addr, val;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005095 int rv;
Arend van Sprielc26b1372010-11-23 14:06:23 +01005096 struct sk_buff *pkt;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005097
5098 /* Address could be zero if CONSOLE := 0 in dongle Makefile */
5099 if (bus->console_addr == 0)
Roland Vossene10d82d2011-05-03 11:35:19 +02005100 return -ENOTSUPP;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005101
5102 /* Exclusive bus access */
5103 dhd_os_sdlock(bus->dhd);
5104
5105 /* Don't allow input if dongle is in reset */
5106 if (bus->dhd->dongle_reset) {
5107 dhd_os_sdunlock(bus->dhd);
Roland Vossenb74ac122011-05-03 11:35:20 +02005108 return -EPERM;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005109 }
5110
5111 /* Request clock to allow SDIO accesses */
5112 BUS_WAKE(bus);
5113 /* No pend allowed since txpkt is called later, ht clk has to be on */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005114 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005115
5116 /* Zero cbuf_index */
Roland Vossen70963f92011-06-01 13:45:08 +02005117 addr = bus->console_addr + offsetof(rte_cons_t, cbuf_idx);
Stanislav Fomichev628f10b2011-02-20 21:43:32 +03005118 val = cpu_to_le32(0);
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005119 rv = dhdsdio_membytes(bus, true, addr, (u8 *)&val, sizeof(val));
Jason Cooper9b890322010-09-30 15:15:39 -04005120 if (rv < 0)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005121 goto done;
5122
5123 /* Write message into cbuf */
Roland Vossen70963f92011-06-01 13:45:08 +02005124 addr = bus->console_addr + offsetof(rte_cons_t, cbuf);
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005125 rv = dhdsdio_membytes(bus, true, addr, (u8 *)msg, msglen);
Jason Cooper9b890322010-09-30 15:15:39 -04005126 if (rv < 0)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005127 goto done;
5128
5129 /* Write length into vcons_in */
Roland Vossen70963f92011-06-01 13:45:08 +02005130 addr = bus->console_addr + offsetof(rte_cons_t, vcons_in);
Stanislav Fomichev628f10b2011-02-20 21:43:32 +03005131 val = cpu_to_le32(msglen);
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005132 rv = dhdsdio_membytes(bus, true, addr, (u8 *)&val, sizeof(val));
Jason Cooper9b890322010-09-30 15:15:39 -04005133 if (rv < 0)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005134 goto done;
5135
5136 /* Bump dongle by sending an empty event pkt.
5137 * sdpcm_sendup (RX) checks for virtual console input.
5138 */
Roland Vossen67ad48b2011-06-01 13:45:51 +02005139 pkt = brcmu_pkt_buf_get_skb(4 + SDPCM_RESERVE);
Jason Cooper9b890322010-09-30 15:15:39 -04005140 if ((pkt != NULL) && bus->clkstate == CLK_AVAIL)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005141 dhdsdio_txpkt(bus, pkt, SDPCM_EVENT_CHANNEL, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005142
5143done:
5144 if ((bus->idletime == DHD_IDLE_IMMEDIATE) && !bus->dpc_sched) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005145 bus->activity = false;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005146 dhdsdio_clkctl(bus, CLK_NONE, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005147 }
5148
5149 dhd_os_sdunlock(bus->dhd);
5150
5151 return rv;
5152}
5153#endif /* DHD_DEBUG */
5154
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07005155static bool dhdsdio_chipmatch(u16 chipid)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005156{
5157 if (chipid == BCM4325_CHIP_ID)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005158 return true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005159 if (chipid == BCM4329_CHIP_ID)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005160 return true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005161 if (chipid == BCM4319_CHIP_ID)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005162 return true;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005163 return false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005164}
5165
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07005166static void *dhdsdio_probe(u16 venid, u16 devid, u16 bus_no,
5167 u16 slot, u16 func, uint bustype, void *regsva,
Arend van Spriel3c9d4c32011-03-02 21:18:48 +01005168 void *sdh)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005169{
5170 int ret;
5171 dhd_bus_t *bus;
5172
5173 /* Init global variables at run-time, not as part of the declaration.
5174 * This is required to support init/de-init of the driver.
5175 * Initialization
5176 * of globals as part of the declaration results in non-deterministic
5177 * behavior since the value of the globals may be different on the
5178 * first time that the driver is initialized vs subsequent
5179 * initializations.
5180 */
5181 dhd_txbound = DHD_TXBOUND;
5182 dhd_rxbound = DHD_RXBOUND;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005183 dhd_alignctl = true;
5184 sd1idle = true;
5185 dhd_readahead = true;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005186 retrydata = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005187 dhd_dongle_memsize = 0;
5188 dhd_txminmax = DHD_TXMINMAX;
5189
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005190 forcealign = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005191
5192 dhd_common_init();
5193
5194 DHD_TRACE(("%s: Enter\n", __func__));
5195 DHD_INFO(("%s: venid 0x%04x devid 0x%04x\n", __func__, venid, devid));
5196
5197 /* We make assumptions about address window mappings */
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07005198 ASSERT((unsigned long)regsva == SI_ENUM_BASE);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005199
5200 /* BCMSDH passes venid and devid based on CIS parsing -- but
5201 * low-power start
5202 * means early parse could fail, so here we should get either an ID
5203 * we recognize OR (-1) indicating we must request power first.
5204 */
5205 /* Check the Vendor ID */
5206 switch (venid) {
5207 case 0x0000:
Stanislav Fomichevbe1c09f2011-03-28 01:31:36 +04005208 case PCI_VENDOR_ID_BROADCOM:
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005209 break;
5210 default:
5211 DHD_ERROR(("%s: unknown vendor: 0x%04x\n", __func__, venid));
5212 return NULL;
5213 }
5214
5215 /* Check the Device ID and make sure it's one that we support */
5216 switch (devid) {
5217 case BCM4325_D11DUAL_ID: /* 4325 802.11a/g id */
5218 case BCM4325_D11G_ID: /* 4325 802.11g 2.4Ghz band id */
5219 case BCM4325_D11A_ID: /* 4325 802.11a 5Ghz band id */
5220 DHD_INFO(("%s: found 4325 Dongle\n", __func__));
5221 break;
5222 case BCM4329_D11NDUAL_ID: /* 4329 802.11n dualband device */
5223 case BCM4329_D11N2G_ID: /* 4329 802.11n 2.4G device */
5224 case BCM4329_D11N5G_ID: /* 4329 802.11n 5G device */
5225 case 0x4329:
5226 DHD_INFO(("%s: found 4329 Dongle\n", __func__));
5227 break;
5228 case BCM4319_D11N_ID: /* 4319 802.11n id */
5229 case BCM4319_D11N2G_ID: /* 4319 802.11n2g id */
5230 case BCM4319_D11N5G_ID: /* 4319 802.11n5g id */
5231 DHD_INFO(("%s: found 4319 Dongle\n", __func__));
5232 break;
5233 case 0:
5234 DHD_INFO(("%s: allow device id 0, will check chip internals\n",
5235 __func__));
5236 break;
5237
5238 default:
5239 DHD_ERROR(("%s: skipping 0x%04x/0x%04x, not a dongle\n",
5240 __func__, venid, devid));
5241 return NULL;
5242 }
5243
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005244 /* Allocate private bus interface state */
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02005245 bus = kzalloc(sizeof(dhd_bus_t), GFP_ATOMIC);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005246 if (!bus) {
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02005247 DHD_ERROR(("%s: kmalloc of dhd_bus_t failed\n", __func__));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005248 goto fail;
5249 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005250 bus->sdh = sdh;
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07005251 bus->cl_devid = (u16) devid;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005252 bus->bus = DHD_BUS;
5253 bus->tx_seq = SDPCM_SEQUENCE_WRAP - 1;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005254 bus->usebufpool = false; /* Use bufpool if allocated,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005255 else use locally malloced rxbuf */
5256
5257 /* attempt to attach to the dongle */
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005258 if (!(dhdsdio_probe_attach(bus, sdh, regsva, devid))) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005259 DHD_ERROR(("%s: dhdsdio_probe_attach failed\n", __func__));
5260 goto fail;
5261 }
5262
5263 /* Attach to the dhd/OS/network interface */
Arend van Spriel3c9d4c32011-03-02 21:18:48 +01005264 bus->dhd = dhd_attach(bus, SDPCM_RESERVE);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005265 if (!bus->dhd) {
5266 DHD_ERROR(("%s: dhd_attach failed\n", __func__));
5267 goto fail;
5268 }
5269
5270 /* Allocate buffers */
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005271 if (!(dhdsdio_probe_malloc(bus, sdh))) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005272 DHD_ERROR(("%s: dhdsdio_probe_malloc failed\n", __func__));
5273 goto fail;
5274 }
5275
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005276 if (!(dhdsdio_probe_init(bus, sdh))) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005277 DHD_ERROR(("%s: dhdsdio_probe_init failed\n", __func__));
5278 goto fail;
5279 }
5280
5281 /* Register interrupt callback, but mask it (not operational yet). */
5282 DHD_INTR(("%s: disable SDIO interrupts (not interested yet)\n",
5283 __func__));
5284 bcmsdh_intr_disable(sdh);
5285 ret = bcmsdh_intr_reg(sdh, dhdsdio_isr, bus);
5286 if (ret != 0) {
5287 DHD_ERROR(("%s: FAILED: bcmsdh_intr_reg returned %d\n",
5288 __func__, ret));
5289 goto fail;
5290 }
5291 DHD_INTR(("%s: registered SDIO interrupt function ok\n", __func__));
5292
5293 DHD_INFO(("%s: completed!!\n", __func__));
5294
5295 /* if firmware path present try to download and bring up bus */
Jason Cooper9b890322010-09-30 15:15:39 -04005296 ret = dhd_bus_start(bus->dhd);
5297 if (ret != 0) {
Roland Vossene10d82d2011-05-03 11:35:19 +02005298 if (ret == -ENOLINK) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005299 DHD_ERROR(("%s: dongle is not responding\n", __func__));
5300 goto fail;
5301 }
5302 }
5303 /* Ok, have the per-port tell the stack we're open for business */
5304 if (dhd_net_attach(bus->dhd, 0) != 0) {
5305 DHD_ERROR(("%s: Net attach failed!!\n", __func__));
5306 goto fail;
5307 }
5308
5309 return bus;
5310
5311fail:
Arend van Spriel3c9d4c32011-03-02 21:18:48 +01005312 dhdsdio_release(bus);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005313 return NULL;
5314}
5315
5316static bool
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005317dhdsdio_probe_attach(struct dhd_bus *bus, void *sdh, void *regsva, u16 devid)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005318{
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07005319 u8 clkctl = 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005320 int err = 0;
5321
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005322 bus->alp_only = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005323
5324 /* Return the window to backplane enumeration space for core access */
5325 if (dhdsdio_set_siaddr_window(bus, SI_ENUM_BASE))
5326 DHD_ERROR(("%s: FAILED to return to SI_ENUM_BASE\n", __func__));
5327
5328#ifdef DHD_DEBUG
Arend van Spriel0bef7742011-02-10 12:03:44 +01005329 printk(KERN_DEBUG "F1 signature read @0x18000000=0x%4x\n",
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005330 bcmsdh_reg_read(bus->sdh, SI_ENUM_BASE, 4));
5331
5332#endif /* DHD_DEBUG */
5333
Franky Linc05df632011-04-25 19:34:07 -07005334 /*
5335 * Force PLL off until dhdsdio_chip_attach()
5336 * programs PLL control regs
5337 */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005338
5339 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
5340 DHD_INIT_CLKCTL1, &err);
5341 if (!err)
5342 clkctl =
5343 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
5344 &err);
5345
5346 if (err || ((clkctl & ~SBSDIO_AVBITS) != DHD_INIT_CLKCTL1)) {
5347 DHD_ERROR(("dhdsdio_probe: ChipClkCSR access: err %d wrote "
5348 "0x%02x read 0x%02x\n",
5349 err, DHD_INIT_CLKCTL1, clkctl));
5350 goto fail;
5351 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005352
Franky Lincb63e4c2011-04-25 15:45:08 -07005353 if (dhdsdio_chip_attach(bus, regsva)) {
5354 DHD_ERROR(("%s: dhdsdio_chip_attach failed!\n", __func__));
5355 goto fail;
5356 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005357
Franky Linc05df632011-04-25 19:34:07 -07005358 bcmsdh_chipinfo(sdh, bus->ci->chip, bus->ci->chiprev);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005359
Franky Linc05df632011-04-25 19:34:07 -07005360 if (!dhdsdio_chipmatch((u16) bus->ci->chip)) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005361 DHD_ERROR(("%s: unsupported chip: 0x%04x\n",
Franky Linc05df632011-04-25 19:34:07 -07005362 __func__, bus->ci->chip));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005363 goto fail;
5364 }
5365
Franky Lin5d0d7a92011-04-25 19:34:05 -07005366 dhdsdio_sdiod_drive_strength_init(bus, dhd_sdiod_drive_strength);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005367
5368 /* Get info on the ARM and SOCRAM cores... */
5369 if (!DHD_NOPMU(bus)) {
Franky Linc05df632011-04-25 19:34:07 -07005370 bus->armrev = SBCOREREV(bcmsdh_reg_read(bus->sdh,
5371 CORE_SB(bus->ci->armcorebase, sbidhigh), 4));
5372 bus->orig_ramsize = bus->ci->ramsize;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005373 if (!(bus->orig_ramsize)) {
5374 DHD_ERROR(("%s: failed to find SOCRAM memory!\n",
5375 __func__));
5376 goto fail;
5377 }
5378 bus->ramsize = bus->orig_ramsize;
5379 if (dhd_dongle_memsize)
5380 dhd_dongle_setmemsize(bus, dhd_dongle_memsize);
5381
5382 DHD_ERROR(("DHD: dongle ram size is set to %d(orig %d)\n",
5383 bus->ramsize, bus->orig_ramsize));
5384 }
5385
Franky Linc05df632011-04-25 19:34:07 -07005386 bus->regs = (void *)bus->ci->buscorebase;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005387
5388 /* Set core control so an SDIO reset does a backplane reset */
Arend van Sprielff31c542011-03-01 10:56:54 +01005389 OR_REG(&bus->regs->corecontrol, CC_BPRESEN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005390
Roland Vossen67ad48b2011-06-01 13:45:51 +02005391 brcmu_pktq_init(&bus->txq, (PRIOMASK + 1), TXQLEN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005392
5393 /* Locate an appropriately-aligned portion of hdrbuf */
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07005394 bus->rxhdr = (u8 *) roundup((unsigned long)&bus->hdrbuf[0], DHD_SDALIGN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005395
5396 /* Set the poll and/or interrupt flags */
5397 bus->intr = (bool) dhd_intr;
Jason Cooper9b890322010-09-30 15:15:39 -04005398 bus->poll = (bool) dhd_poll;
5399 if (bus->poll)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005400 bus->pollrate = 1;
5401
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005402 return true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005403
5404fail:
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005405 return false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005406}
5407
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005408static bool dhdsdio_probe_malloc(dhd_bus_t *bus, void *sdh)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005409{
5410 DHD_TRACE(("%s: Enter\n", __func__));
5411
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005412 if (bus->dhd->maxctl) {
5413 bus->rxblen =
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07005414 roundup((bus->dhd->maxctl + SDPCM_HDRLEN),
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005415 ALIGNMENT) + DHD_SDALIGN;
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02005416 bus->rxbuf = kmalloc(bus->rxblen, GFP_ATOMIC);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005417 if (!(bus->rxbuf)) {
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02005418 DHD_ERROR(("%s: kmalloc of %d-byte rxbuf failed\n",
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005419 __func__, bus->rxblen));
5420 goto fail;
5421 }
5422 }
5423
5424 /* Allocate buffer to receive glomed packet */
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02005425 bus->databuf = kmalloc(MAX_DATA_BUF, GFP_ATOMIC);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005426 if (!(bus->databuf)) {
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02005427 DHD_ERROR(("%s: kmalloc of %d-byte databuf failed\n",
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005428 __func__, MAX_DATA_BUF));
5429 /* release rxbuf which was already located as above */
5430 if (!bus->rxblen)
mike.rapoport@gmail.com182acb32010-10-13 00:09:12 +02005431 kfree(bus->rxbuf);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005432 goto fail;
5433 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005434
5435 /* Align the buffer */
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07005436 if ((unsigned long)bus->databuf % DHD_SDALIGN)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005437 bus->dataptr =
5438 bus->databuf + (DHD_SDALIGN -
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07005439 ((unsigned long)bus->databuf % DHD_SDALIGN));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005440 else
5441 bus->dataptr = bus->databuf;
5442
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005443 return true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005444
5445fail:
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005446 return false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005447}
5448
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005449static bool dhdsdio_probe_init(dhd_bus_t *bus, void *sdh)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005450{
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07005451 s32 fnum;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005452
5453 DHD_TRACE(("%s: Enter\n", __func__));
5454
5455#ifdef SDTEST
5456 dhdsdio_pktgen_init(bus);
5457#endif /* SDTEST */
5458
5459 /* Disable F2 to clear any intermediate frame state on the dongle */
Franky Lin0df46042011-06-01 13:45:40 +02005460 bcmsdh_cfg_write(sdh, SDIO_FUNC_0, SDIO_CCCR_IOEx, SDIO_FUNC_ENABLE_1,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005461 NULL);
5462
5463 bus->dhd->busstate = DHD_BUS_DOWN;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005464 bus->sleeping = false;
5465 bus->rxflow = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005466 bus->prev_rxlim_hit = 0;
5467
5468 /* Done with backplane-dependent accesses, can drop clock... */
5469 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR, 0, NULL);
5470
5471 /* ...and initialize clock/power states */
5472 bus->clkstate = CLK_SDONLY;
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07005473 bus->idletime = (s32) dhd_idletime;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005474 bus->idleclock = DHD_IDLE_ACTIVE;
5475
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005476 /* Query the F2 block size, set roundup accordingly */
5477 fnum = 2;
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07005478 if (bcmsdh_iovar_op(sdh, "sd_blocksize", &fnum, sizeof(s32),
Roland Vossena1c5ad82011-04-11 15:16:24 +02005479 &bus->blocksize, sizeof(s32), false) != 0) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005480 bus->blocksize = 0;
5481 DHD_ERROR(("%s: fail on %s get\n", __func__, "sd_blocksize"));
5482 } else {
5483 DHD_INFO(("%s: Initial value for %s is %d\n",
5484 __func__, "sd_blocksize", bus->blocksize));
5485 }
Greg Kroah-Hartman7068c2f2010-10-08 11:34:59 -07005486 bus->roundup = min(max_roundup, bus->blocksize);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005487
5488 /* Query if bus module supports packet chaining,
5489 default to use if supported */
5490 if (bcmsdh_iovar_op(sdh, "sd_rxchain", NULL, 0,
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07005491 &bus->sd_rxchain, sizeof(s32),
Roland Vossena1c5ad82011-04-11 15:16:24 +02005492 false) != 0) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005493 bus->sd_rxchain = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005494 } else {
5495 DHD_INFO(("%s: bus module (through bcmsdh API) %s chaining\n",
5496 __func__,
5497 (bus->sd_rxchain ? "supports" : "does not support")));
5498 }
5499 bus->use_rxchain = (bool) bus->sd_rxchain;
5500
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005501 return true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005502}
5503
5504bool
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005505dhd_bus_download_firmware(struct dhd_bus *bus, char *fw_path, char *nv_path)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005506{
5507 bool ret;
5508 bus->fw_path = fw_path;
5509 bus->nv_path = nv_path;
5510
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005511 ret = dhdsdio_download_firmware(bus, bus->sdh);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005512
5513 return ret;
5514}
5515
5516static bool
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005517dhdsdio_download_firmware(struct dhd_bus *bus, void *sdh)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005518{
5519 bool ret;
5520
5521 /* Download the firmware */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005522 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005523
5524 ret = _dhdsdio_download_firmware(bus) == 0;
5525
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005526 dhdsdio_clkctl(bus, CLK_SDONLY, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005527
5528 return ret;
5529}
5530
5531/* Detach and free everything */
Arend van Spriel3c9d4c32011-03-02 21:18:48 +01005532static void dhdsdio_release(dhd_bus_t *bus)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005533{
5534 DHD_TRACE(("%s: Enter\n", __func__));
5535
5536 if (bus) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005537 /* De-register interrupt handler */
5538 bcmsdh_intr_disable(bus->sdh);
5539 bcmsdh_intr_dereg(bus->sdh);
5540
5541 if (bus->dhd) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005542 dhd_detach(bus->dhd);
Franky Lincee3cf42011-04-25 19:34:06 -07005543 dhdsdio_release_dongle(bus);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005544 bus->dhd = NULL;
5545 }
5546
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005547 dhdsdio_release_malloc(bus);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005548
mike.rapoport@gmail.com182acb32010-10-13 00:09:12 +02005549 kfree(bus);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005550 }
5551
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005552 DHD_TRACE(("%s: Disconnected\n", __func__));
5553}
5554
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005555static void dhdsdio_release_malloc(dhd_bus_t *bus)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005556{
5557 DHD_TRACE(("%s: Enter\n", __func__));
5558
5559 if (bus->dhd && bus->dhd->dongle_reset)
5560 return;
5561
5562 if (bus->rxbuf) {
mike.rapoport@gmail.com182acb32010-10-13 00:09:12 +02005563 kfree(bus->rxbuf);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005564 bus->rxctl = bus->rxbuf = NULL;
5565 bus->rxlen = 0;
5566 }
5567
Ilia Mirkin46d994b2011-03-13 00:28:56 -05005568 kfree(bus->databuf);
5569 bus->databuf = NULL;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005570}
5571
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005572static void dhdsdio_release_dongle(dhd_bus_t *bus)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005573{
5574 DHD_TRACE(("%s: Enter\n", __func__));
5575
5576 if (bus->dhd && bus->dhd->dongle_reset)
5577 return;
5578
Franky Linc05df632011-04-25 19:34:07 -07005579 if (bus->ci) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005580 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005581 dhdsdio_clkctl(bus, CLK_NONE, false);
Franky Lincee3cf42011-04-25 19:34:06 -07005582 dhdsdio_chip_detach(bus);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005583 if (bus->vars && bus->varsz)
mike.rapoport@gmail.com182acb32010-10-13 00:09:12 +02005584 kfree(bus->vars);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005585 bus->vars = NULL;
5586 }
5587
5588 DHD_TRACE(("%s: Disconnected\n", __func__));
5589}
5590
5591static void dhdsdio_disconnect(void *ptr)
5592{
5593 dhd_bus_t *bus = (dhd_bus_t *)ptr;
5594
5595 DHD_TRACE(("%s: Enter\n", __func__));
5596
5597 if (bus) {
5598 ASSERT(bus->dhd);
Arend van Spriel3c9d4c32011-03-02 21:18:48 +01005599 dhdsdio_release(bus);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005600 }
5601
5602 DHD_TRACE(("%s: Disconnected\n", __func__));
5603}
5604
5605/* Register/Unregister functions are called by the main DHD entry
5606 * point (e.g. module insertion) to link with the bus driver, in
5607 * order to look for or await the device.
5608 */
5609
5610static bcmsdh_driver_t dhd_sdio = {
5611 dhdsdio_probe,
5612 dhdsdio_disconnect
5613};
5614
5615int dhd_bus_register(void)
5616{
5617 DHD_TRACE(("%s: Enter\n", __func__));
5618
5619 return bcmsdh_register(&dhd_sdio);
5620}
5621
5622void dhd_bus_unregister(void)
5623{
5624 DHD_TRACE(("%s: Enter\n", __func__));
5625
5626 bcmsdh_unregister();
5627}
5628
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005629static int dhdsdio_download_code_file(struct dhd_bus *bus, char *fw_path)
5630{
5631 int bcmerror = -1;
5632 int offset = 0;
5633 uint len;
5634 void *image = NULL;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07005635 u8 *memblock = NULL, *memptr;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005636
5637 DHD_INFO(("%s: download firmware %s\n", __func__, fw_path));
5638
5639 image = dhd_os_open_image(fw_path);
5640 if (image == NULL)
5641 goto err;
5642
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02005643 memptr = memblock = kmalloc(MEMBLOCK + DHD_SDALIGN, GFP_ATOMIC);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005644 if (memblock == NULL) {
5645 DHD_ERROR(("%s: Failed to allocate memory %d bytes\n",
5646 __func__, MEMBLOCK));
5647 goto err;
5648 }
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07005649 if ((u32)(unsigned long)memblock % DHD_SDALIGN)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005650 memptr +=
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07005651 (DHD_SDALIGN - ((u32)(unsigned long)memblock % DHD_SDALIGN));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005652
5653 /* Download image */
5654 while ((len =
5655 dhd_os_get_image_block((char *)memptr, MEMBLOCK, image))) {
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005656 bcmerror = dhdsdio_membytes(bus, true, offset, memptr, len);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005657 if (bcmerror) {
5658 DHD_ERROR(("%s: error %d on writing %d membytes at "
5659 "0x%08x\n", __func__, bcmerror, MEMBLOCK, offset));
5660 goto err;
5661 }
5662
5663 offset += MEMBLOCK;
5664 }
5665
5666err:
Ilia Mirkin46d994b2011-03-13 00:28:56 -05005667 kfree(memblock);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005668
5669 if (image)
5670 dhd_os_close_image(image);
5671
5672 return bcmerror;
5673}
5674
5675/*
5676 * ProcessVars:Takes a buffer of "<var>=<value>\n" lines read from a file
5677 * and ending in a NUL.
5678 * Removes carriage returns, empty lines, comment lines, and converts
5679 * newlines to NULs.
5680 * Shortens buffer as needed and pads with NULs. End of buffer is marked
5681 * by two NULs.
5682*/
5683
5684static uint process_nvram_vars(char *varbuf, uint len)
5685{
5686 char *dp;
5687 bool findNewline;
5688 int column;
5689 uint buf_len, n;
5690
5691 dp = varbuf;
5692
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005693 findNewline = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005694 column = 0;
5695
5696 for (n = 0; n < len; n++) {
5697 if (varbuf[n] == 0)
5698 break;
5699 if (varbuf[n] == '\r')
5700 continue;
5701 if (findNewline && varbuf[n] != '\n')
5702 continue;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005703 findNewline = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005704 if (varbuf[n] == '#') {
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005705 findNewline = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005706 continue;
5707 }
5708 if (varbuf[n] == '\n') {
5709 if (column == 0)
5710 continue;
5711 *dp++ = 0;
5712 column = 0;
5713 continue;
5714 }
5715 *dp++ = varbuf[n];
5716 column++;
5717 }
5718 buf_len = dp - varbuf;
5719
5720 while (dp < varbuf + n)
5721 *dp++ = 0;
5722
5723 return buf_len;
5724}
5725
5726/*
5727 EXAMPLE: nvram_array
5728 nvram_arry format:
5729 name=value
5730 Use carriage return at the end of each assignment,
5731 and an empty string with
5732 carriage return at the end of array.
5733
5734 For example:
5735 unsigned char nvram_array[] = {"name1=value1\n",
5736 "name2=value2\n", "\n"};
5737 Hex values start with 0x, and mac addr format: xx:xx:xx:xx:xx:xx.
5738
5739 Search "EXAMPLE: nvram_array" to see how the array is activated.
5740*/
5741
5742void dhd_bus_set_nvram_params(struct dhd_bus *bus, const char *nvram_params)
5743{
5744 bus->nvram_params = nvram_params;
5745}
5746
5747static int dhdsdio_download_nvram(struct dhd_bus *bus)
5748{
5749 int bcmerror = -1;
5750 uint len;
5751 void *image = NULL;
5752 char *memblock = NULL;
5753 char *bufp;
5754 char *nv_path;
5755 bool nvram_file_exists;
5756
5757 nv_path = bus->nv_path;
5758
5759 nvram_file_exists = ((nv_path != NULL) && (nv_path[0] != '\0'));
5760 if (!nvram_file_exists && (bus->nvram_params == NULL))
5761 return 0;
5762
5763 if (nvram_file_exists) {
5764 image = dhd_os_open_image(nv_path);
5765 if (image == NULL)
5766 goto err;
5767 }
5768
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02005769 memblock = kmalloc(MEMBLOCK, GFP_ATOMIC);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005770 if (memblock == NULL) {
5771 DHD_ERROR(("%s: Failed to allocate memory %d bytes\n",
5772 __func__, MEMBLOCK));
5773 goto err;
5774 }
5775
5776 /* Download variables */
5777 if (nvram_file_exists) {
5778 len = dhd_os_get_image_block(memblock, MEMBLOCK, image);
5779 } else {
5780 len = strlen(bus->nvram_params);
5781 ASSERT(len <= MEMBLOCK);
5782 if (len > MEMBLOCK)
5783 len = MEMBLOCK;
5784 memcpy(memblock, bus->nvram_params, len);
5785 }
5786
5787 if (len > 0 && len < MEMBLOCK) {
5788 bufp = (char *)memblock;
5789 bufp[len] = 0;
5790 len = process_nvram_vars(bufp, len);
5791 bufp += len;
5792 *bufp++ = 0;
5793 if (len)
5794 bcmerror = dhdsdio_downloadvars(bus, memblock, len + 1);
5795 if (bcmerror) {
5796 DHD_ERROR(("%s: error downloading vars: %d\n",
5797 __func__, bcmerror));
5798 }
5799 } else {
5800 DHD_ERROR(("%s: error reading nvram file: %d\n",
5801 __func__, len));
Roland Vossenb74ac122011-05-03 11:35:20 +02005802 bcmerror = -EIO;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005803 }
5804
5805err:
Ilia Mirkin46d994b2011-03-13 00:28:56 -05005806 kfree(memblock);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005807
5808 if (image)
5809 dhd_os_close_image(image);
5810
5811 return bcmerror;
5812}
5813
5814static int _dhdsdio_download_firmware(struct dhd_bus *bus)
5815{
5816 int bcmerror = -1;
5817
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005818 bool embed = false; /* download embedded firmware */
5819 bool dlok = false; /* download firmware succeeded */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005820
5821 /* Out immediately if no image to download */
Franky Lina4181fb2011-06-01 13:45:34 +02005822 if ((bus->fw_path == NULL) || (bus->fw_path[0] == '\0'))
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005823 return bcmerror;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005824
5825 /* Keep arm in reset */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005826 if (dhdsdio_download_state(bus, true)) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005827 DHD_ERROR(("%s: error placing ARM core in reset\n", __func__));
5828 goto err;
5829 }
5830
5831 /* External image takes precedence if specified */
5832 if ((bus->fw_path != NULL) && (bus->fw_path[0] != '\0')) {
5833 if (dhdsdio_download_code_file(bus, bus->fw_path)) {
5834 DHD_ERROR(("%s: dongle image file download failed\n",
5835 __func__));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005836 goto err;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005837 } else {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005838 embed = false;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005839 dlok = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005840 }
5841 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005842 if (!dlok) {
5843 DHD_ERROR(("%s: dongle image download failed\n", __func__));
5844 goto err;
5845 }
5846
5847 /* EXAMPLE: nvram_array */
5848 /* If a valid nvram_arry is specified as above, it can be passed
5849 down to dongle */
5850 /* dhd_bus_set_nvram_params(bus, (char *)&nvram_array); */
5851
5852 /* External nvram takes precedence if specified */
5853 if (dhdsdio_download_nvram(bus)) {
5854 DHD_ERROR(("%s: dongle nvram file download failed\n",
5855 __func__));
5856 }
5857
5858 /* Take arm out of reset */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005859 if (dhdsdio_download_state(bus, false)) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005860 DHD_ERROR(("%s: error getting out of ARM core reset\n",
5861 __func__));
5862 goto err;
5863 }
5864
5865 bcmerror = 0;
5866
5867err:
5868 return bcmerror;
5869}
5870
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005871
5872static int
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07005873dhd_bcmsdh_send_buf(dhd_bus_t *bus, u32 addr, uint fn, uint flags,
Arend van Sprielc26b1372010-11-23 14:06:23 +01005874 u8 *buf, uint nbytes, struct sk_buff *pkt,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005875 bcmsdh_cmplt_fn_t complete, void *handle)
5876{
5877 return bcmsdh_send_buf
5878 (bus->sdh, addr, fn, flags, buf, nbytes, pkt, complete,
5879 handle);
5880}
5881
5882uint dhd_bus_chip(struct dhd_bus *bus)
5883{
Franky Linc05df632011-04-25 19:34:07 -07005884 ASSERT(bus->ci != NULL);
5885 return bus->ci->chip;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005886}
5887
5888void *dhd_bus_pub(struct dhd_bus *bus)
5889{
5890 return bus->dhd;
5891}
5892
5893void *dhd_bus_txq(struct dhd_bus *bus)
5894{
5895 return &bus->txq;
5896}
5897
5898uint dhd_bus_hdrlen(struct dhd_bus *bus)
5899{
5900 return SDPCM_HDRLEN;
5901}
5902
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07005903int dhd_bus_devreset(dhd_pub_t *dhdp, u8 flag)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005904{
5905 int bcmerror = 0;
5906 dhd_bus_t *bus;
5907
5908 bus = dhdp->bus;
5909
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005910 if (flag == true) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005911 if (!bus->dhd->dongle_reset) {
5912 /* Expect app to have torn down any
5913 connection before calling */
5914 /* Stop the bus, disable F2 */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005915 dhd_bus_stop(bus, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005916
5917 /* Clean tx/rx buffer pointers,
5918 detach from the dongle */
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005919 dhdsdio_release_dongle(bus);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005920
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005921 bus->dhd->dongle_reset = true;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005922 bus->dhd->up = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005923
5924 DHD_TRACE(("%s: WLAN OFF DONE\n", __func__));
5925 /* App can now remove power from device */
5926 } else
Roland Vossenb74ac122011-05-03 11:35:20 +02005927 bcmerror = -EIO;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005928 } else {
5929 /* App must have restored power to device before calling */
5930
5931 DHD_TRACE(("\n\n%s: == WLAN ON ==\n", __func__));
5932
5933 if (bus->dhd->dongle_reset) {
5934 /* Turn on WLAN */
5935 /* Reset SD client */
5936 bcmsdh_reset(bus->sdh);
5937
5938 /* Attempt to re-attach & download */
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005939 if (dhdsdio_probe_attach(bus, bus->sdh,
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07005940 (u32 *) SI_ENUM_BASE,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005941 bus->cl_devid)) {
5942 /* Attempt to download binary to the dongle */
5943 if (dhdsdio_probe_init
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005944 (bus, bus->sdh)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005945 && dhdsdio_download_firmware(bus,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005946 bus->sdh)) {
5947
5948 /* Re-init bus, enable F2 transfer */
5949 dhd_bus_init((dhd_pub_t *) bus->dhd,
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005950 false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005951
5952#if defined(OOB_INTR_ONLY)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005953 dhd_enable_oob_intr(bus, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005954#endif /* defined(OOB_INTR_ONLY) */
5955
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005956 bus->dhd->dongle_reset = false;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005957 bus->dhd->up = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005958
5959 DHD_TRACE(("%s: WLAN ON DONE\n",
5960 __func__));
5961 } else
Roland Vossenb74ac122011-05-03 11:35:20 +02005962 bcmerror = -EIO;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005963 } else
Roland Vossenb74ac122011-05-03 11:35:20 +02005964 bcmerror = -EIO;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005965 } else {
Roland Vossenb74ac122011-05-03 11:35:20 +02005966 bcmerror = -EISCONN;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005967 DHD_ERROR(("%s: Set DEVRESET=false invoked when device "
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005968 "is on\n", __func__));
Roland Vossenb74ac122011-05-03 11:35:20 +02005969 bcmerror = -EIO;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005970 }
5971 }
5972 return bcmerror;
5973}
Franky Lincb63e4c2011-04-25 15:45:08 -07005974
5975static int
5976dhdsdio_chip_recognition(bcmsdh_info_t *sdh, struct chip_info *ci, void *regs)
5977{
5978 u32 regdata;
5979
5980 /*
5981 * Get CC core rev
5982 * Chipid is assume to be at offset 0 from regs arg
5983 * For different chiptypes or old sdio hosts w/o chipcommon,
5984 * other ways of recognition should be added here.
5985 */
5986 ci->cccorebase = (u32)regs;
5987 regdata = bcmsdh_reg_read(sdh, CORE_CC_REG(ci->cccorebase, chipid), 4);
5988 ci->chip = regdata & CID_ID_MASK;
5989 ci->chiprev = (regdata & CID_REV_MASK) >> CID_REV_SHIFT;
5990
5991 DHD_INFO(("%s: chipid=0x%x chiprev=%d\n",
5992 __func__, ci->chip, ci->chiprev));
5993
5994 /* Address of cores for new chips should be added here */
5995 switch (ci->chip) {
5996 case BCM4329_CHIP_ID:
5997 ci->buscorebase = BCM4329_CORE_BUS_BASE;
5998 ci->ramcorebase = BCM4329_CORE_SOCRAM_BASE;
5999 ci->armcorebase = BCM4329_CORE_ARM_BASE;
Franky Linc05df632011-04-25 19:34:07 -07006000 ci->ramsize = BCM4329_RAMSIZE;
Franky Lincb63e4c2011-04-25 15:45:08 -07006001 break;
6002 default:
6003 DHD_ERROR(("%s: chipid 0x%x is not supported\n",
6004 __func__, ci->chip));
6005 return -ENODEV;
6006 }
6007
6008 regdata = bcmsdh_reg_read(sdh,
6009 CORE_SB(ci->cccorebase, sbidhigh), 4);
6010 ci->ccrev = SBCOREREV(regdata);
6011
6012 regdata = bcmsdh_reg_read(sdh,
6013 CORE_CC_REG(ci->cccorebase, pmucapabilities), 4);
6014 ci->pmurev = regdata & PCAP_REV_MASK;
6015
6016 regdata = bcmsdh_reg_read(sdh, CORE_SB(ci->buscorebase, sbidhigh), 4);
6017 ci->buscorerev = SBCOREREV(regdata);
6018 ci->buscoretype = (regdata & SBIDH_CC_MASK) >> SBIDH_CC_SHIFT;
6019
6020 DHD_INFO(("%s: ccrev=%d, pmurev=%d, buscore rev/type=%d/0x%x\n",
6021 __func__, ci->ccrev, ci->pmurev,
6022 ci->buscorerev, ci->buscoretype));
6023
6024 /* get chipcommon capabilites */
6025 ci->cccaps = bcmsdh_reg_read(sdh,
6026 CORE_CC_REG(ci->cccorebase, capabilities), 4);
6027
6028 return 0;
6029}
6030
6031static void
6032dhdsdio_chip_disablecore(bcmsdh_info_t *sdh, u32 corebase)
6033{
6034 u32 regdata;
6035
6036 regdata = bcmsdh_reg_read(sdh,
6037 CORE_SB(corebase, sbtmstatelow), 4);
6038 if (regdata & SBTML_RESET)
6039 return;
6040
6041 regdata = bcmsdh_reg_read(sdh,
6042 CORE_SB(corebase, sbtmstatelow), 4);
6043 if ((regdata & (SICF_CLOCK_EN << SBTML_SICF_SHIFT)) != 0) {
6044 /*
6045 * set target reject and spin until busy is clear
6046 * (preserve core-specific bits)
6047 */
6048 regdata = bcmsdh_reg_read(sdh,
6049 CORE_SB(corebase, sbtmstatelow), 4);
6050 bcmsdh_reg_write(sdh, CORE_SB(corebase, sbtmstatelow), 4,
6051 regdata | SBTML_REJ);
6052
6053 regdata = bcmsdh_reg_read(sdh,
6054 CORE_SB(corebase, sbtmstatelow), 4);
6055 udelay(1);
6056 SPINWAIT((bcmsdh_reg_read(sdh,
6057 CORE_SB(corebase, sbtmstatehigh), 4) &
6058 SBTMH_BUSY), 100000);
6059
6060 regdata = bcmsdh_reg_read(sdh,
6061 CORE_SB(corebase, sbtmstatehigh), 4);
6062 if (regdata & SBTMH_BUSY)
6063 DHD_ERROR(("%s: ARM core still busy\n", __func__));
6064
6065 regdata = bcmsdh_reg_read(sdh,
6066 CORE_SB(corebase, sbidlow), 4);
6067 if (regdata & SBIDL_INIT) {
6068 regdata = bcmsdh_reg_read(sdh,
6069 CORE_SB(corebase, sbimstate), 4) |
6070 SBIM_RJ;
6071 bcmsdh_reg_write(sdh,
6072 CORE_SB(corebase, sbimstate), 4,
6073 regdata);
6074 regdata = bcmsdh_reg_read(sdh,
6075 CORE_SB(corebase, sbimstate), 4);
6076 udelay(1);
6077 SPINWAIT((bcmsdh_reg_read(sdh,
6078 CORE_SB(corebase, sbimstate), 4) &
6079 SBIM_BY), 100000);
6080 }
6081
6082 /* set reset and reject while enabling the clocks */
6083 bcmsdh_reg_write(sdh,
6084 CORE_SB(corebase, sbtmstatelow), 4,
6085 (((SICF_FGC | SICF_CLOCK_EN) << SBTML_SICF_SHIFT) |
6086 SBTML_REJ | SBTML_RESET));
6087 regdata = bcmsdh_reg_read(sdh,
6088 CORE_SB(corebase, sbtmstatelow), 4);
6089 udelay(10);
6090
6091 /* clear the initiator reject bit */
6092 regdata = bcmsdh_reg_read(sdh,
6093 CORE_SB(corebase, sbidlow), 4);
6094 if (regdata & SBIDL_INIT) {
6095 regdata = bcmsdh_reg_read(sdh,
6096 CORE_SB(corebase, sbimstate), 4) &
6097 ~SBIM_RJ;
6098 bcmsdh_reg_write(sdh,
6099 CORE_SB(corebase, sbimstate), 4,
6100 regdata);
6101 }
6102 }
6103
6104 /* leave reset and reject asserted */
6105 bcmsdh_reg_write(sdh, CORE_SB(corebase, sbtmstatelow), 4,
6106 (SBTML_REJ | SBTML_RESET));
6107 udelay(1);
6108}
6109
6110static int
6111dhdsdio_chip_attach(struct dhd_bus *bus, void *regs)
6112{
6113 struct chip_info *ci;
6114 int err;
6115 u8 clkval, clkset;
6116
6117 DHD_TRACE(("%s: Enter\n", __func__));
6118
6119 /* alloc chip_info_t */
6120 ci = kmalloc(sizeof(struct chip_info), GFP_ATOMIC);
6121 if (NULL == ci) {
6122 DHD_ERROR(("%s: malloc failed!\n", __func__));
6123 return -ENOMEM;
6124 }
6125
6126 memset((unsigned char *)ci, 0, sizeof(struct chip_info));
6127
6128 /* bus/core/clk setup for register access */
6129 /* Try forcing SDIO core to do ALPAvail request only */
6130 clkset = SBSDIO_FORCE_HW_CLKREQ_OFF | SBSDIO_ALP_AVAIL_REQ;
6131 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
6132 clkset, &err);
6133 if (err) {
6134 DHD_ERROR(("%s: error writing for HT off\n", __func__));
6135 goto fail;
6136 }
6137
6138 /* If register supported, wait for ALPAvail and then force ALP */
6139 /* This may take up to 15 milliseconds */
6140 clkval = bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1,
6141 SBSDIO_FUNC1_CHIPCLKCSR, NULL);
6142 if ((clkval & ~SBSDIO_AVBITS) == clkset) {
6143 SPINWAIT(((clkval =
6144 bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1,
6145 SBSDIO_FUNC1_CHIPCLKCSR,
6146 NULL)),
6147 !SBSDIO_ALPAV(clkval)),
6148 PMU_MAX_TRANSITION_DLY);
6149 if (!SBSDIO_ALPAV(clkval)) {
6150 DHD_ERROR(("%s: timeout on ALPAV wait, clkval 0x%02x\n",
6151 __func__, clkval));
6152 err = -EBUSY;
6153 goto fail;
6154 }
6155 clkset = SBSDIO_FORCE_HW_CLKREQ_OFF |
6156 SBSDIO_FORCE_ALP;
6157 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1,
6158 SBSDIO_FUNC1_CHIPCLKCSR,
6159 clkset, &err);
6160 udelay(65);
6161 } else {
6162 DHD_ERROR(("%s: ChipClkCSR access: wrote 0x%02x read 0x%02x\n",
6163 __func__, clkset, clkval));
6164 err = -EACCES;
6165 goto fail;
6166 }
6167
6168 /* Also, disable the extra SDIO pull-ups */
6169 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_SDIOPULLUP, 0,
6170 NULL);
6171
6172 err = dhdsdio_chip_recognition(bus->sdh, ci, regs);
6173 if (err)
6174 goto fail;
6175
6176 /*
6177 * Make sure any on-chip ARM is off (in case strapping is wrong),
6178 * or downloaded code was already running.
6179 */
6180 dhdsdio_chip_disablecore(bus->sdh, ci->armcorebase);
6181
6182 bcmsdh_reg_write(bus->sdh,
6183 CORE_CC_REG(ci->cccorebase, gpiopullup), 4, 0);
6184 bcmsdh_reg_write(bus->sdh,
6185 CORE_CC_REG(ci->cccorebase, gpiopulldown), 4, 0);
6186
6187 /* Disable F2 to clear any intermediate frame state on the dongle */
Franky Lin0df46042011-06-01 13:45:40 +02006188 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_0, SDIO_CCCR_IOEx,
Franky Lincb63e4c2011-04-25 15:45:08 -07006189 SDIO_FUNC_ENABLE_1, NULL);
6190
6191 /* WAR: cmd52 backplane read so core HW will drop ALPReq */
6192 clkval = bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1,
6193 0, NULL);
6194
6195 /* Done with backplane-dependent accesses, can drop clock... */
6196 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR, 0,
6197 NULL);
6198
6199 bus->ci = ci;
6200 return 0;
6201fail:
6202 bus->ci = NULL;
6203 kfree(ci);
6204 return err;
6205}
Franky Lineb5dc512011-04-25 19:34:04 -07006206
6207static void
6208dhdsdio_chip_resetcore(bcmsdh_info_t *sdh, u32 corebase)
6209{
6210 u32 regdata;
6211
6212 /*
6213 * Must do the disable sequence first to work for
6214 * arbitrary current core state.
6215 */
6216 dhdsdio_chip_disablecore(sdh, corebase);
6217
6218 /*
6219 * Now do the initialization sequence.
6220 * set reset while enabling the clock and
6221 * forcing them on throughout the core
6222 */
6223 bcmsdh_reg_write(sdh, CORE_SB(corebase, sbtmstatelow), 4,
6224 ((SICF_FGC | SICF_CLOCK_EN) << SBTML_SICF_SHIFT) |
6225 SBTML_RESET);
6226 udelay(1);
6227
6228 regdata = bcmsdh_reg_read(sdh, CORE_SB(corebase, sbtmstatehigh), 4);
6229 if (regdata & SBTMH_SERR)
6230 bcmsdh_reg_write(sdh, CORE_SB(corebase, sbtmstatehigh), 4, 0);
6231
6232 regdata = bcmsdh_reg_read(sdh, CORE_SB(corebase, sbimstate), 4);
6233 if (regdata & (SBIM_IBE | SBIM_TO))
6234 bcmsdh_reg_write(sdh, CORE_SB(corebase, sbimstate), 4,
6235 regdata & ~(SBIM_IBE | SBIM_TO));
6236
6237 /* clear reset and allow it to propagate throughout the core */
6238 bcmsdh_reg_write(sdh, CORE_SB(corebase, sbtmstatelow), 4,
6239 (SICF_FGC << SBTML_SICF_SHIFT) |
6240 (SICF_CLOCK_EN << SBTML_SICF_SHIFT));
6241 udelay(1);
6242
6243 /* leave clock enabled */
6244 bcmsdh_reg_write(sdh, CORE_SB(corebase, sbtmstatelow), 4,
6245 (SICF_CLOCK_EN << SBTML_SICF_SHIFT));
6246 udelay(1);
6247}
Franky Lin5d0d7a92011-04-25 19:34:05 -07006248
6249/* SDIO Pad drive strength to select value mappings */
6250struct sdiod_drive_str {
6251 u8 strength; /* Pad Drive Strength in mA */
6252 u8 sel; /* Chip-specific select value */
6253};
6254
6255/* SDIO Drive Strength to sel value table for PMU Rev 1 */
6256static const struct sdiod_drive_str sdiod_drive_strength_tab1[] = {
6257 {
6258 4, 0x2}, {
6259 2, 0x3}, {
6260 1, 0x0}, {
6261 0, 0x0}
6262 };
6263
6264/* SDIO Drive Strength to sel value table for PMU Rev 2, 3 */
6265static const struct sdiod_drive_str sdiod_drive_strength_tab2[] = {
6266 {
6267 12, 0x7}, {
6268 10, 0x6}, {
6269 8, 0x5}, {
6270 6, 0x4}, {
6271 4, 0x2}, {
6272 2, 0x1}, {
6273 0, 0x0}
6274 };
6275
6276/* SDIO Drive Strength to sel value table for PMU Rev 8 (1.8V) */
6277static const struct sdiod_drive_str sdiod_drive_strength_tab3[] = {
6278 {
6279 32, 0x7}, {
6280 26, 0x6}, {
6281 22, 0x5}, {
6282 16, 0x4}, {
6283 12, 0x3}, {
6284 8, 0x2}, {
6285 4, 0x1}, {
6286 0, 0x0}
6287 };
6288
6289#define SDIOD_DRVSTR_KEY(chip, pmu) (((chip) << 16) | (pmu))
6290
6291static void
6292dhdsdio_sdiod_drive_strength_init(struct dhd_bus *bus, u32 drivestrength) {
6293 struct sdiod_drive_str *str_tab = NULL;
6294 u32 str_mask = 0;
6295 u32 str_shift = 0;
Franky Lin5d0d7a92011-04-25 19:34:05 -07006296 char chn[8];
Franky Lin5d0d7a92011-04-25 19:34:05 -07006297
6298 if (!(bus->ci->cccaps & CC_CAP_PMU))
6299 return;
6300
6301 switch (SDIOD_DRVSTR_KEY(bus->ci->chip, bus->ci->pmurev)) {
6302 case SDIOD_DRVSTR_KEY(BCM4325_CHIP_ID, 1):
6303 str_tab = (struct sdiod_drive_str *)&sdiod_drive_strength_tab1;
6304 str_mask = 0x30000000;
6305 str_shift = 28;
6306 break;
6307 case SDIOD_DRVSTR_KEY(BCM4325_CHIP_ID, 2):
6308 case SDIOD_DRVSTR_KEY(BCM4325_CHIP_ID, 3):
6309 str_tab = (struct sdiod_drive_str *)&sdiod_drive_strength_tab2;
6310 str_mask = 0x00003800;
6311 str_shift = 11;
6312 break;
6313 case SDIOD_DRVSTR_KEY(BCM4336_CHIP_ID, 8):
6314 str_tab = (struct sdiod_drive_str *)&sdiod_drive_strength_tab3;
6315 str_mask = 0x00003800;
6316 str_shift = 11;
6317 break;
6318 default:
6319 DHD_ERROR(("No SDIO Drive strength init"
6320 "done for chip %s rev %d pmurev %d\n",
Roland Vossen67ad48b2011-06-01 13:45:51 +02006321 brcmu_chipname(bus->ci->chip, chn, 8),
Franky Lin5d0d7a92011-04-25 19:34:05 -07006322 bus->ci->chiprev, bus->ci->pmurev));
6323 break;
6324 }
6325
6326 if (str_tab != NULL) {
6327 u32 drivestrength_sel = 0;
6328 u32 cc_data_temp;
6329 int i;
6330
6331 for (i = 0; str_tab[i].strength != 0; i++) {
6332 if (drivestrength >= str_tab[i].strength) {
6333 drivestrength_sel = str_tab[i].sel;
6334 break;
6335 }
6336 }
6337
6338 bcmsdh_reg_write(bus->sdh,
6339 CORE_CC_REG(bus->ci->cccorebase, chipcontrol_addr),
6340 4, 1);
6341 cc_data_temp = bcmsdh_reg_read(bus->sdh,
6342 CORE_CC_REG(bus->ci->cccorebase, chipcontrol_addr), 4);
6343 cc_data_temp &= ~str_mask;
6344 drivestrength_sel <<= str_shift;
6345 cc_data_temp |= drivestrength_sel;
6346 bcmsdh_reg_write(bus->sdh,
6347 CORE_CC_REG(bus->ci->cccorebase, chipcontrol_addr),
6348 4, cc_data_temp);
6349
6350 DHD_INFO(("SDIO: %dmA drive strength selected, set to 0x%08x\n",
6351 drivestrength, cc_data_temp));
6352 }
6353}
Franky Lincee3cf42011-04-25 19:34:06 -07006354
6355static void
6356dhdsdio_chip_detach(struct dhd_bus *bus)
6357{
6358 DHD_TRACE(("%s: Enter\n", __func__));
6359
6360 kfree(bus->ci);
6361 bus->ci = NULL;
6362}