blob: 48550f350376dcde534ee02737885b28e0500ce5 [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>
Roland Vossencc3cea52011-06-01 13:45:59 +020025#include <defs.h>
Roland Vossenf97e9562011-06-01 13:45:52 +020026#include <brcmu_wifi.h>
27#include <brcmu_utils.h>
Roland Vossencc3cea52011-06-01 13:45:59 +020028#include <brcm_hw_ids.h>
Roland Vossencc3cea52011-06-01 13:45:59 +020029#include <soc.h>
Roland Vossenc4daa842011-06-09 16:44:50 +020030#include "sdio_host.h"
Roland Vossen44895182011-06-01 13:45:55 +020031
32/* register access macros */
Roland Vossen44895182011-06-01 13:45:55 +020033#ifndef __BIG_ENDIAN
34#ifndef __mips__
Roland Vossenb61a4be2011-06-01 13:45:56 +020035#define R_REG(r) \
Roland Vossen25036342011-06-01 13:45:57 +020036 bcmsdh_reg_read(NULL, (unsigned long)(r), sizeof(*(r)))
Roland Vossen44895182011-06-01 13:45:55 +020037#else /* __mips__ */
Roland Vossenb61a4be2011-06-01 13:45:56 +020038#define R_REG(r) \
39 ({ \
40 __typeof(*(r)) __osl_v; \
41 __asm__ __volatile__("sync"); \
Roland Vossen25036342011-06-01 13:45:57 +020042 __osl_v = bcmsdh_reg_read(NULL, (unsigned long)(r),\
43 sizeof(*(r))); \
Roland Vossenb61a4be2011-06-01 13:45:56 +020044 __asm__ __volatile__("sync"); \
45 __osl_v; \
46 })
Roland Vossen44895182011-06-01 13:45:55 +020047#endif /* __mips__ */
48
49#define W_REG(r, v) do { \
Roland Vossen25036342011-06-01 13:45:57 +020050 bcmsdh_reg_write(NULL, (unsigned long)(r), sizeof(*(r)), (v)); \
Roland Vossen44895182011-06-01 13:45:55 +020051 } while (0)
52#else /* __BIG_ENDIAN */
Roland Vossenb61a4be2011-06-01 13:45:56 +020053#define R_REG(r) \
Roland Vossen25036342011-06-01 13:45:57 +020054 bcmsdh_reg_read(NULL, (unsigned long)(r), sizeof(*(r)))
Roland Vossen44895182011-06-01 13:45:55 +020055#define W_REG(r, v) do { \
Roland Vossen25036342011-06-01 13:45:57 +020056 bcmsdh_reg_write(NULL, (unsigned long)(r), sizeof(*(r)), (v)); \
Roland Vossen44895182011-06-01 13:45:55 +020057 } while (0)
58#endif /* __BIG_ENDIAN */
59
Roland Vossen44895182011-06-01 13:45:55 +020060#define AND_REG(r, v) W_REG((r), R_REG(r) & (v))
61#define OR_REG(r, v) W_REG((r), R_REG(r) | (v))
62
63#define SET_REG(r, mask, val) \
64 W_REG((r), ((R_REG(r) & ~(mask)) | (val)))
65
Henry Ptasinskicf2b4482010-09-20 22:33:12 -070066#ifdef DHD_DEBUG
Roland Vossenf1c7a082011-06-01 13:45:09 +020067
68/* ARM trap handling */
69
70/* Trap types defined by ARM (see arminc.h) */
71
72/* Trap locations in lo memory */
73#define TRAP_STRIDE 4
74#define FIRST_TRAP TR_RST
75#define LAST_TRAP (TR_FIQ * TRAP_STRIDE)
76
77#if defined(__ARM_ARCH_4T__)
78#define MAX_TRAP_TYPE (TR_FIQ + 1)
79#elif defined(__ARM_ARCH_7M__)
80#define MAX_TRAP_TYPE (TR_ISR + ARMCM3_NUMINTS)
81#endif /* __ARM_ARCH_7M__ */
82
83/* The trap structure is defined here as offsets for assembly */
84#define TR_TYPE 0x00
85#define TR_EPC 0x04
86#define TR_CPSR 0x08
87#define TR_SPSR 0x0c
88#define TR_REGS 0x10
89#define TR_REG(n) (TR_REGS + (n) * 4)
90#define TR_SP TR_REG(13)
91#define TR_LR TR_REG(14)
92#define TR_PC TR_REG(15)
93
94#define TRAP_T_SIZE 80
95
Roland Vossenf1c7a082011-06-01 13:45:09 +020096typedef struct _trap_struct {
97 u32 type;
98 u32 epc;
99 u32 cpsr;
100 u32 spsr;
101 u32 r0;
102 u32 r1;
103 u32 r2;
104 u32 r3;
105 u32 r4;
106 u32 r5;
107 u32 r6;
108 u32 r7;
109 u32 r8;
110 u32 r9;
111 u32 r10;
112 u32 r11;
113 u32 r12;
114 u32 r13;
115 u32 r14;
116 u32 pc;
117} trap_t;
118
Roland Vossenf1c7a082011-06-01 13:45:09 +0200119#define CBUF_LEN (128)
120
121#define LOG_BUF_LEN 1024
122
123typedef struct {
124 u32 buf; /* Can't be pointer on (64-bit) hosts */
125 uint buf_size;
126 uint idx;
127 char *_buf_compat; /* Redundant pointer for backward compat. */
128} rte_log_t;
129
130typedef struct {
131 /* Virtual UART
132 * When there is no UART (e.g. Quickturn),
133 * the host should write a complete
134 * input line directly into cbuf and then write
135 * the length into vcons_in.
136 * This may also be used when there is a real UART
137 * (at risk of conflicting with
138 * the real UART). vcons_out is currently unused.
139 */
140 volatile uint vcons_in;
141 volatile uint vcons_out;
142
143 /* Output (logging) buffer
144 * Console output is written to a ring buffer log_buf at index log_idx.
145 * The host may read the output when it sees log_idx advance.
146 * Output will be lost if the output wraps around faster than the host
147 * polls.
148 */
149 rte_log_t log;
150
151 /* Console input line buffer
152 * Characters are read one at a time into cbuf
153 * until <CR> is received, then
154 * the buffer is processed as a command line.
155 * Also used for virtual UART.
156 */
157 uint cbuf_idx;
158 char cbuf[CBUF_LEN];
159} rte_cons_t;
160
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700161#endif /* DHD_DEBUG */
Roland Vossen745c9e62011-06-01 13:45:30 +0200162#include <chipcommon.h>
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700163
Roland Vossenc4daa842011-06-09 16:44:50 +0200164#include "sbsdio.h"
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700165
Roland Vossenc4daa842011-06-09 16:44:50 +0200166#include "dngl_stats.h"
167#include "dhd.h"
168#include "dhd_bus.h"
169#include "dhd_proto.h"
170#include "dhd_dbg.h"
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700171#include <sdiovar.h>
Franky Lincb63e4c2011-04-25 15:45:08 -0700172#include <bcmchip.h>
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700173
174#ifndef DHDSDIO_MEM_DUMP_FNAME
175#define DHDSDIO_MEM_DUMP_FNAME "mem_dump"
176#endif
177
Grant Grundler26a71a42011-03-09 10:41:25 -0800178#define TXQLEN 2048 /* bulk tx queue length */
179#define TXHI (TXQLEN - 256) /* turn on flow control above TXHI */
180#define TXLOW (TXHI - 256) /* turn off flow control below TXLOW */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700181#define PRIOMASK 7
182
183#define TXRETRIES 2 /* # of retries for tx frames */
184
185#if defined(CONFIG_MACH_SANDGATE2G)
186#define DHD_RXBOUND 250 /* Default for max rx frames in
187 one scheduling */
188#else
189#define DHD_RXBOUND 50 /* Default for max rx frames in
190 one scheduling */
191#endif /* defined(CONFIG_MACH_SANDGATE2G) */
192
193#define DHD_TXBOUND 20 /* Default for max tx frames in
194 one scheduling */
195
196#define DHD_TXMINMAX 1 /* Max tx frames if rx still pending */
197
198#define MEMBLOCK 2048 /* Block size used for downloading
199 of dongle image */
200#define MAX_DATA_BUF (32 * 1024) /* Must be large enough to hold
201 biggest possible glom */
202
203/* Packet alignment for most efficient SDIO (can change based on platform) */
204#ifndef DHD_SDALIGN
205#define DHD_SDALIGN 32
206#endif
207#if !ISPOWEROF2(DHD_SDALIGN)
208#error DHD_SDALIGN is not a power of 2!
209#endif
210
211#ifndef DHD_FIRSTREAD
212#define DHD_FIRSTREAD 32
213#endif
214#if !ISPOWEROF2(DHD_FIRSTREAD)
215#error DHD_FIRSTREAD is not a power of 2!
216#endif
217
218/* Total length of frame header for dongle protocol */
219#define SDPCM_HDRLEN (SDPCM_FRAMETAG_LEN + SDPCM_SWHEADER_LEN)
220#ifdef SDTEST
221#define SDPCM_RESERVE (SDPCM_HDRLEN + SDPCM_TEST_HDRLEN + DHD_SDALIGN)
222#else
223#define SDPCM_RESERVE (SDPCM_HDRLEN + DHD_SDALIGN)
224#endif
225
Franky Linb49b14d2011-06-01 13:45:37 +0200226/*
227 * Software allocation of To SB Mailbox resources
228 */
229
230/* tosbmailbox bits corresponding to intstatus bits */
231#define SMB_NAK (1 << 0) /* Frame NAK */
232#define SMB_INT_ACK (1 << 1) /* Host Interrupt ACK */
233#define SMB_USE_OOB (1 << 2) /* Use OOB Wakeup */
234#define SMB_DEV_INT (1 << 3) /* Miscellaneous Interrupt */
235
236/* tosbmailboxdata */
237#define SMB_DATA_VERSION_SHIFT 16 /* host protocol version */
238
239/*
240 * Software allocation of To Host Mailbox resources
241 */
242
243/* intstatus bits */
244#define I_HMB_FC_STATE I_HMB_SW0 /* Flow Control State */
245#define I_HMB_FC_CHANGE I_HMB_SW1 /* Flow Control State Changed */
246#define I_HMB_FRAME_IND I_HMB_SW2 /* Frame Indication */
247#define I_HMB_HOST_INT I_HMB_SW3 /* Miscellaneous Interrupt */
248
249/* tohostmailboxdata */
250#define HMB_DATA_NAKHANDLED 1 /* retransmit NAK'd frame */
251#define HMB_DATA_DEVREADY 2 /* talk to host after enable */
252#define HMB_DATA_FC 4 /* per prio flowcontrol update flag */
253#define HMB_DATA_FWREADY 8 /* fw ready for protocol activity */
254
255#define HMB_DATA_FCDATA_MASK 0xff000000
256#define HMB_DATA_FCDATA_SHIFT 24
257
258#define HMB_DATA_VERSION_MASK 0x00ff0000
259#define HMB_DATA_VERSION_SHIFT 16
260
261/*
262 * Software-defined protocol header
263 */
264
265/* Current protocol version */
266#define SDPCM_PROT_VERSION 4
267
268/* SW frame header */
269#define SDPCM_PACKET_SEQUENCE(p) (((u8 *)p)[0] & 0xff)
270
271#define SDPCM_CHANNEL_MASK 0x00000f00
272#define SDPCM_CHANNEL_SHIFT 8
273#define SDPCM_PACKET_CHANNEL(p) (((u8 *)p)[1] & 0x0f)
274
275#define SDPCM_NEXTLEN_OFFSET 2
276
277/* Data Offset from SOF (HW Tag, SW Tag, Pad) */
278#define SDPCM_DOFFSET_OFFSET 3 /* Data Offset */
279#define SDPCM_DOFFSET_VALUE(p) (((u8 *)p)[SDPCM_DOFFSET_OFFSET] & 0xff)
280#define SDPCM_DOFFSET_MASK 0xff000000
281#define SDPCM_DOFFSET_SHIFT 24
282#define SDPCM_FCMASK_OFFSET 4 /* Flow control */
283#define SDPCM_FCMASK_VALUE(p) (((u8 *)p)[SDPCM_FCMASK_OFFSET] & 0xff)
284#define SDPCM_WINDOW_OFFSET 5 /* Credit based fc */
285#define SDPCM_WINDOW_VALUE(p) (((u8 *)p)[SDPCM_WINDOW_OFFSET] & 0xff)
286
287#define SDPCM_SWHEADER_LEN 8 /* SW header is 64 bits */
288
289/* logical channel numbers */
290#define SDPCM_CONTROL_CHANNEL 0 /* Control channel Id */
291#define SDPCM_EVENT_CHANNEL 1 /* Asyc Event Indication Channel Id */
292#define SDPCM_DATA_CHANNEL 2 /* Data Xmit/Recv Channel Id */
293#define SDPCM_GLOM_CHANNEL 3 /* For coalesced packets */
294#define SDPCM_TEST_CHANNEL 15 /* Reserved for test/debug packets */
295
296#define SDPCM_SEQUENCE_WRAP 256 /* wrap-around val for 8bit frame seq */
297
298#define SDPCM_GLOMDESC(p) (((u8 *)p)[1] & 0x80)
299
300/* For TEST_CHANNEL packets, define another 4-byte header */
301#define SDPCM_TEST_HDRLEN 4 /*
302 * Generally: Cmd(1), Ext(1), Len(2);
303 * Semantics of Ext byte depend on
304 * command. Len is current or requested
305 * frame length, not including test
306 * header; sent little-endian.
307 */
308#define SDPCM_TEST_DISCARD 0x01 /* Receiver discards. Ext:pattern id. */
309#define SDPCM_TEST_ECHOREQ 0x02 /* Echo request. Ext:pattern id. */
310#define SDPCM_TEST_ECHORSP 0x03 /* Echo response. Ext:pattern id. */
311#define SDPCM_TEST_BURST 0x04 /*
312 * Receiver to send a burst.
313 * Ext is a frame count
314 */
315#define SDPCM_TEST_SEND 0x05 /*
316 * Receiver sets send mode.
317 * Ext is boolean on/off
318 */
319
320/* Handy macro for filling in datagen packets with a pattern */
321#define SDPCM_TEST_FILL(byteno, id) ((u8)(id + byteno))
322
323/*
324 * Shared structure between dongle and the host.
325 * The structure contains pointers to trap or assert information.
326 */
327#define SDPCM_SHARED_VERSION 0x0002
328#define SDPCM_SHARED_VERSION_MASK 0x00FF
329#define SDPCM_SHARED_ASSERT_BUILT 0x0100
330#define SDPCM_SHARED_ASSERT 0x0200
331#define SDPCM_SHARED_TRAP 0x0400
332
333
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700334/* Space for header read, limit for data packets */
335#ifndef MAX_HDR_READ
336#define MAX_HDR_READ 32
337#endif
338#if !ISPOWEROF2(MAX_HDR_READ)
339#error MAX_HDR_READ is not a power of 2!
340#endif
341
342#define MAX_RX_DATASZ 2048
343
344/* Maximum milliseconds to wait for F2 to come up */
345#define DHD_WAIT_F2RDY 3000
346
347/* Bump up limit on waiting for HT to account for first startup;
348 * if the image is doing a CRC calculation before programming the PMU
349 * for HT availability, it could take a couple hundred ms more, so
350 * max out at a 1 second (1000000us).
351 */
352#if (PMU_MAX_TRANSITION_DLY <= 1000000)
353#undef PMU_MAX_TRANSITION_DLY
354#define PMU_MAX_TRANSITION_DLY 1000000
355#endif
356
357/* Value for ChipClockCSR during initial setup */
358#define DHD_INIT_CLKCTL1 (SBSDIO_FORCE_HW_CLKREQ_OFF | \
359 SBSDIO_ALP_AVAIL_REQ)
360#define DHD_INIT_CLKCTL2 (SBSDIO_FORCE_HW_CLKREQ_OFF | SBSDIO_FORCE_ALP)
361
362/* Flags for SDH calls */
363#define F2SYNC (SDIO_REQ_4BYTE | SDIO_REQ_FIXED)
364
Roland Vossenb6fe70c2011-06-01 13:45:19 +0200365/* sbimstate */
366#define SBIM_IBE 0x20000 /* inbanderror */
367#define SBIM_TO 0x40000 /* timeout */
368#define SBIM_BY 0x01800000 /* busy (sonics >= 2.3) */
369#define SBIM_RJ 0x02000000 /* reject (sonics >= 2.3) */
370
371/* sbtmstatelow */
372#define SBTML_RESET 0x0001 /* reset */
373#define SBTML_REJ_MASK 0x0006 /* reject field */
374#define SBTML_REJ 0x0002 /* reject */
375#define SBTML_TMPREJ 0x0004 /* temporary reject, for error recovery */
376
377#define SBTML_SICF_SHIFT 16 /* Shift to locate the SI control flags in sbtml */
378
379/* sbtmstatehigh */
380#define SBTMH_SERR 0x0001 /* serror */
381#define SBTMH_INT 0x0002 /* interrupt */
382#define SBTMH_BUSY 0x0004 /* busy */
383#define SBTMH_TO 0x0020 /* timeout (sonics >= 2.3) */
384
385#define SBTMH_SISF_SHIFT 16 /* Shift to locate the SI status flags in sbtmh */
386
387/* sbidlow */
388#define SBIDL_INIT 0x80 /* initiator */
389
390/* sbidhigh */
391#define SBIDH_RC_MASK 0x000f /* revision code */
392#define SBIDH_RCE_MASK 0x7000 /* revision code extension field */
393#define SBIDH_RCE_SHIFT 8
394#define SBCOREREV(sbidh) \
395 ((((sbidh) & SBIDH_RCE_MASK) >> SBIDH_RCE_SHIFT) | ((sbidh) & SBIDH_RC_MASK))
396#define SBIDH_CC_MASK 0x8ff0 /* core code */
397#define SBIDH_CC_SHIFT 4
398#define SBIDH_VC_MASK 0xffff0000 /* vendor code */
399#define SBIDH_VC_SHIFT 16
400
Arend van Spriel70dfb582011-02-25 16:39:17 +0100401/*
402 * Conversion of 802.1D priority to precedence level
403 */
404#define PRIO2PREC(prio) \
405 (((prio) == PRIO_8021D_NONE || (prio) == PRIO_8021D_BE) ? \
406 ((prio^2)) : (prio))
407
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700408DHD_SPINWAIT_SLEEP_INIT(sdioh_spinwait_sleep);
409extern int dhdcdc_set_ioctl(dhd_pub_t *dhd, int ifidx, uint cmd, void *buf,
410 uint len);
411
Roland Vossenb6fe70c2011-06-01 13:45:19 +0200412/* Core reg address translation */
413#define CORE_CC_REG(base, field) (base + offsetof(chipcregs_t, field))
Franky Lin597600a2011-06-01 13:45:39 +0200414#define CORE_BUS_REG(base, field) \
415 (base + offsetof(struct sdpcmd_regs, field))
Roland Vossenb6fe70c2011-06-01 13:45:19 +0200416#define CORE_SB(base, field) \
417 (base + SBCONFIGOFF + offsetof(sbconfig_t, field))
418
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700419#ifdef DHD_DEBUG
420/* Device console log buffer state */
421typedef struct dhd_console {
422 uint count; /* Poll interval msec counter */
423 uint log_addr; /* Log struct address (fixed) */
Roland Vossen70963f92011-06-01 13:45:08 +0200424 rte_log_t log; /* Log struct (host copy) */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700425 uint bufsize; /* Size of log buffer */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700426 u8 *buf; /* Log buffer (host copy) */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700427 uint last; /* Last buffer read index */
428} dhd_console_t;
429#endif /* DHD_DEBUG */
430
Franky Linb49b14d2011-06-01 13:45:37 +0200431struct sdpcm_shared {
432 u32 flags;
433 u32 trap_addr;
434 u32 assert_exp_addr;
435 u32 assert_file_addr;
436 u32 assert_line;
437 u32 console_addr; /* Address of rte_cons_t */
438 u32 msgtrace_addr;
439 u8 tag[32];
440};
441
442
Franky Lincb63e4c2011-04-25 15:45:08 -0700443/* misc chip info needed by some of the routines */
444struct chip_info {
445 u32 chip;
446 u32 chiprev;
447 u32 cccorebase;
448 u32 ccrev;
449 u32 cccaps;
450 u32 buscorebase;
451 u32 buscorerev;
452 u32 buscoretype;
453 u32 ramcorebase;
454 u32 armcorebase;
455 u32 pmurev;
Franky Linc05df632011-04-25 19:34:07 -0700456 u32 ramsize;
Franky Lincb63e4c2011-04-25 15:45:08 -0700457};
458
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700459/* Private data for SDIO bus interaction */
460typedef struct dhd_bus {
461 dhd_pub_t *dhd;
462
463 bcmsdh_info_t *sdh; /* Handle for BCMSDH calls */
Franky Lincb63e4c2011-04-25 15:45:08 -0700464 struct chip_info *ci; /* Chip info struct */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700465 char *vars; /* Variables (from CIS and/or other) */
466 uint varsz; /* Size of variables buffer */
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -0700467 u32 sbaddr; /* Current SB window pointer (-1, invalid) */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700468
Franky Lin597600a2011-06-01 13:45:39 +0200469 struct sdpcmd_regs *regs; /* SDIO core */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700470 uint sdpcmrev; /* SDIO core revision */
471 uint armrev; /* CPU core revision */
472 uint ramrev; /* SOCRAM core revision */
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -0700473 u32 ramsize; /* Size of RAM in SOCRAM (bytes) */
474 u32 orig_ramsize; /* Size of RAM in SOCRAM (bytes) */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700475
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -0700476 u32 bus; /* gSPI or SDIO bus */
477 u32 hostintmask; /* Copy of Host Interrupt Mask */
478 u32 intstatus; /* Intstatus bits (events) pending */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700479 bool dpc_sched; /* Indicates DPC schedule (intrpt rcvd) */
480 bool fcstate; /* State of dongle flow-control */
481
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -0700482 u16 cl_devid; /* cached devid for dhdsdio_probe_attach() */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700483 char *fw_path; /* module_param: path to firmware image */
484 char *nv_path; /* module_param: path to nvram vars file */
485 const char *nvram_params; /* user specified nvram params. */
486
487 uint blocksize; /* Block size of SDIO transfers */
488 uint roundup; /* Max roundup limit */
489
490 struct pktq txq; /* Queue length used for flow-control */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700491 u8 flowcontrol; /* per prio flow control bitmask */
492 u8 tx_seq; /* Transmit sequence number (next) */
493 u8 tx_max; /* Maximum transmit sequence allowed */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700494
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700495 u8 hdrbuf[MAX_HDR_READ + DHD_SDALIGN];
496 u8 *rxhdr; /* Header of current rx frame (in hdrbuf) */
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -0700497 u16 nextlen; /* Next Read Len from last header */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700498 u8 rx_seq; /* Receive sequence number (expected) */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700499 bool rxskip; /* Skip receive (awaiting NAK ACK) */
500
Arend van Sprielc26b1372010-11-23 14:06:23 +0100501 struct sk_buff *glomd; /* Packet containing glomming descriptor */
502 struct sk_buff *glom; /* Packet chain for glommed superframe */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700503 uint glomerr; /* Glom packet read errors */
504
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700505 u8 *rxbuf; /* Buffer for receiving control packets */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700506 uint rxblen; /* Allocated length of rxbuf */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700507 u8 *rxctl; /* Aligned pointer into rxbuf */
508 u8 *databuf; /* Buffer for receiving big glom packet */
509 u8 *dataptr; /* Aligned pointer into databuf */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700510 uint rxlen; /* Length of valid data in buffer */
511
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700512 u8 sdpcm_ver; /* Bus protocol reported by dongle */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700513
514 bool intr; /* Use interrupts */
515 bool poll; /* Use polling */
516 bool ipend; /* Device interrupt is pending */
517 bool intdis; /* Interrupts disabled by isr */
518 uint intrcount; /* Count of device interrupt callbacks */
519 uint lastintrs; /* Count as of last watchdog timer */
520 uint spurious; /* Count of spurious interrupts */
521 uint pollrate; /* Ticks between device polls */
522 uint polltick; /* Tick counter */
523 uint pollcnt; /* Count of active polls */
524
525#ifdef DHD_DEBUG
526 dhd_console_t console; /* Console output polling support */
527 uint console_addr; /* Console address from shared struct */
528#endif /* DHD_DEBUG */
529
530 uint regfails; /* Count of R_REG/W_REG failures */
531
532 uint clkstate; /* State of sd and backplane clock(s) */
533 bool activity; /* Activity flag for clock down */
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -0700534 s32 idletime; /* Control for activity timeout */
535 s32 idlecount; /* Activity timeout counter */
536 s32 idleclock; /* How to set bus driver when idle */
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -0700537 s32 sd_rxchain; /* If bcmsdh api accepts PKT chains */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700538 bool use_rxchain; /* If dhd should use PKT chains */
539 bool sleeping; /* Is SDIO bus sleeping? */
540 bool rxflow_mode; /* Rx flow control mode */
541 bool rxflow; /* Is rx flow control on */
542 uint prev_rxlim_hit; /* Is prev rx limit exceeded
543 (per dpc schedule) */
544 bool alp_only; /* Don't use HT clock (ALP only) */
545/* Field to decide if rx of control frames happen in rxbuf or lb-pool */
546 bool usebufpool;
547
548#ifdef SDTEST
549 /* external loopback */
550 bool ext_loop;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700551 u8 loopid;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700552
553 /* pktgen configuration */
554 uint pktgen_freq; /* Ticks between bursts */
555 uint pktgen_count; /* Packets to send each burst */
556 uint pktgen_print; /* Bursts between count displays */
557 uint pktgen_total; /* Stop after this many */
558 uint pktgen_minlen; /* Minimum packet data len */
559 uint pktgen_maxlen; /* Maximum packet data len */
560 uint pktgen_mode; /* Configured mode: tx, rx, or echo */
561 uint pktgen_stop; /* Number of tx failures causing stop */
562
563 /* active pktgen fields */
564 uint pktgen_tick; /* Tick counter for bursts */
565 uint pktgen_ptick; /* Burst counter for printing */
566 uint pktgen_sent; /* Number of test packets generated */
567 uint pktgen_rcvd; /* Number of test packets received */
568 uint pktgen_fail; /* Number of failed send attempts */
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -0700569 u16 pktgen_len; /* Length of next packet to send */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700570#endif /* SDTEST */
571
572 /* Some additional counters */
573 uint tx_sderrs; /* Count of tx attempts with sd errors */
574 uint fcqueued; /* Tx packets that got queued */
575 uint rxrtx; /* Count of rtx requests (NAK to dongle) */
576 uint rx_toolong; /* Receive frames too long to receive */
577 uint rxc_errors; /* SDIO errors when reading control frames */
578 uint rx_hdrfail; /* SDIO errors on header reads */
579 uint rx_badhdr; /* Bad received headers (roosync?) */
580 uint rx_badseq; /* Mismatched rx sequence number */
581 uint fc_rcvd; /* Number of flow-control events received */
582 uint fc_xoff; /* Number which turned on flow-control */
583 uint fc_xon; /* Number which turned off flow-control */
584 uint rxglomfail; /* Failed deglom attempts */
585 uint rxglomframes; /* Number of glom frames (superframes) */
586 uint rxglompkts; /* Number of packets from glom frames */
587 uint f2rxhdrs; /* Number of header reads */
588 uint f2rxdata; /* Number of frame data reads */
589 uint f2txdata; /* Number of f2 frame writes */
590 uint f1regdata; /* Number of f1 register accesses */
591
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700592 u8 *ctrl_frame_buf;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -0700593 u32 ctrl_frame_len;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700594 bool ctrl_frame_stat;
Franky Lin0f1a87c2011-06-29 16:46:32 -0700595
596 spinlock_t txqlock;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700597} dhd_bus_t;
598
Roland Vossenb6fe70c2011-06-01 13:45:19 +0200599typedef volatile struct _sbconfig {
600 u32 PAD[2];
601 u32 sbipsflag; /* initiator port ocp slave flag */
602 u32 PAD[3];
603 u32 sbtpsflag; /* target port ocp slave flag */
604 u32 PAD[11];
605 u32 sbtmerrloga; /* (sonics >= 2.3) */
606 u32 PAD;
607 u32 sbtmerrlog; /* (sonics >= 2.3) */
608 u32 PAD[3];
609 u32 sbadmatch3; /* address match3 */
610 u32 PAD;
611 u32 sbadmatch2; /* address match2 */
612 u32 PAD;
613 u32 sbadmatch1; /* address match1 */
614 u32 PAD[7];
615 u32 sbimstate; /* initiator agent state */
616 u32 sbintvec; /* interrupt mask */
617 u32 sbtmstatelow; /* target state */
618 u32 sbtmstatehigh; /* target state */
619 u32 sbbwa0; /* bandwidth allocation table0 */
620 u32 PAD;
621 u32 sbimconfiglow; /* initiator configuration */
622 u32 sbimconfighigh; /* initiator configuration */
623 u32 sbadmatch0; /* address match0 */
624 u32 PAD;
625 u32 sbtmconfiglow; /* target configuration */
626 u32 sbtmconfighigh; /* target configuration */
627 u32 sbbconfig; /* broadcast configuration */
628 u32 PAD;
629 u32 sbbstate; /* broadcast state */
630 u32 PAD[3];
631 u32 sbactcnfg; /* activate configuration */
632 u32 PAD[3];
633 u32 sbflagst; /* current sbflags */
634 u32 PAD[3];
635 u32 sbidlow; /* identification */
636 u32 sbidhigh; /* identification */
637} sbconfig_t;
638
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700639/* clkstate */
640#define CLK_NONE 0
641#define CLK_SDONLY 1
642#define CLK_PENDING 2 /* Not used yet */
643#define CLK_AVAIL 3
644
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -0700645#define DHD_NOPMU(dhd) (false)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700646
647#ifdef DHD_DEBUG
648static int qcount[NUMPRIO];
649static int tx_packets[NUMPRIO];
650#endif /* DHD_DEBUG */
651
652/* Deferred transmit */
653const uint dhd_deferred_tx = 1;
654
655extern uint dhd_watchdog_ms;
656extern void dhd_os_wd_timer(void *bus, uint wdtick);
657
658/* Tx/Rx bounds */
659uint dhd_txbound;
660uint dhd_rxbound;
661uint dhd_txminmax;
662
663/* override the RAM size if possible */
664#define DONGLE_MIN_MEMSIZE (128 * 1024)
665int dhd_dongle_memsize;
666
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700667static bool dhd_alignctl;
668
669static bool sd1idle;
670
671static bool retrydata;
672#define RETRYCHAN(chan) (((chan) == SDPCM_EVENT_CHANNEL) || retrydata)
673
674static const uint watermark = 8;
675static const uint firstread = DHD_FIRSTREAD;
676
677#define HDATLEN (firstread - (SDPCM_HDRLEN))
678
679/* Retry count for register access failures */
680static const uint retry_limit = 2;
681
682/* Force even SD lengths (some host controllers mess up on odd bytes) */
683static bool forcealign;
684
685#define ALIGNMENT 4
686
687#if defined(OOB_INTR_ONLY) && defined(HW_OOB)
688extern void bcmsdh_enable_hw_oob_intr(void *sdh, bool enable);
689#endif
690
691#if defined(OOB_INTR_ONLY) && defined(SDIO_ISR_THREAD)
692#error OOB_INTR_ONLY is NOT working with SDIO_ISR_THREAD
693#endif /* defined(OOB_INTR_ONLY) && defined(SDIO_ISR_THREAD) */
Arend van Spriel3c9d4c32011-03-02 21:18:48 +0100694#define PKTALIGN(_p, _len, _align) \
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700695 do { \
696 uint datalign; \
Arend van Spriel54991ad2010-11-23 14:06:24 +0100697 datalign = (unsigned long)((_p)->data); \
698 datalign = roundup(datalign, (_align)) - datalign; \
699 ASSERT(datalign < (_align)); \
700 ASSERT((_p)->len >= ((_len) + datalign)); \
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700701 if (datalign) \
Arend van Spriel54991ad2010-11-23 14:06:24 +0100702 skb_pull((_p), datalign); \
703 __skb_trim((_p), (_len)); \
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700704 } while (0)
705
706/* Limit on rounding up frames */
707static const uint max_roundup = 512;
708
709/* Try doing readahead */
710static bool dhd_readahead;
711
712/* To check if there's window offered */
713#define DATAOK(bus) \
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700714 (((u8)(bus->tx_max - bus->tx_seq) != 0) && \
715 (((u8)(bus->tx_max - bus->tx_seq) & 0x80) == 0))
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700716
717/* Macros to get register read/write status */
718/* NOTE: these assume a local dhdsdio_bus_t *bus! */
719#define R_SDREG(regvar, regaddr, retryvar) \
720do { \
721 retryvar = 0; \
722 do { \
Arend van Sprielff31c542011-03-01 10:56:54 +0100723 regvar = R_REG(regaddr); \
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700724 } while (bcmsdh_regfail(bus->sdh) && (++retryvar <= retry_limit)); \
725 if (retryvar) { \
726 bus->regfails += (retryvar-1); \
727 if (retryvar > retry_limit) { \
728 DHD_ERROR(("%s: FAILED" #regvar "READ, LINE %d\n", \
729 __func__, __LINE__)); \
730 regvar = 0; \
731 } \
732 } \
733} while (0)
734
735#define W_SDREG(regval, regaddr, retryvar) \
736do { \
737 retryvar = 0; \
738 do { \
Arend van Sprielff31c542011-03-01 10:56:54 +0100739 W_REG(regaddr, regval); \
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700740 } while (bcmsdh_regfail(bus->sdh) && (++retryvar <= retry_limit)); \
741 if (retryvar) { \
742 bus->regfails += (retryvar-1); \
743 if (retryvar > retry_limit) \
744 DHD_ERROR(("%s: FAILED REGISTER WRITE, LINE %d\n", \
745 __func__, __LINE__)); \
746 } \
747} while (0)
748
749#define DHD_BUS SDIO_BUS
750
751#define PKT_AVAILABLE() (intstatus & I_HMB_FRAME_IND)
752
753#define HOSTINTMASK (I_HMB_SW_MASK | I_CHIPACTIVE)
754
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700755#ifdef SDTEST
756static void dhdsdio_testrcv(dhd_bus_t *bus, void *pkt, uint seq);
757static void dhdsdio_sdtest_set(dhd_bus_t *bus, bool start);
758#endif
759
760#ifdef DHD_DEBUG
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700761static int dhdsdio_checkdied(dhd_bus_t *bus, u8 *data, uint size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700762static int dhdsdio_mem_dump(dhd_bus_t *bus);
763#endif /* DHD_DEBUG */
764static int dhdsdio_download_state(dhd_bus_t *bus, bool enter);
765
Arend van Spriel3c9d4c32011-03-02 21:18:48 +0100766static void dhdsdio_release(dhd_bus_t *bus);
Arend van Spriel8da4a3a2011-03-02 21:18:42 +0100767static void dhdsdio_release_malloc(dhd_bus_t *bus);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700768static void dhdsdio_disconnect(void *ptr);
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -0700769static bool dhdsdio_chipmatch(u16 chipid);
Arend van Spriel8da4a3a2011-03-02 21:18:42 +0100770static bool dhdsdio_probe_attach(dhd_bus_t *bus, void *sdh,
771 void *regsva, u16 devid);
772static bool dhdsdio_probe_malloc(dhd_bus_t *bus, void *sdh);
773static bool dhdsdio_probe_init(dhd_bus_t *bus, void *sdh);
774static void dhdsdio_release_dongle(dhd_bus_t *bus);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700775
776static uint process_nvram_vars(char *varbuf, uint len);
777
778static void dhd_dongle_setmemsize(struct dhd_bus *bus, int mem_size);
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -0700779static int dhd_bcmsdh_send_buf(dhd_bus_t *bus, u32 addr, uint fn,
Arend van Sprielc26b1372010-11-23 14:06:23 +0100780 uint flags, u8 *buf, uint nbytes,
781 struct sk_buff *pkt, bcmsdh_cmplt_fn_t complete,
782 void *handle);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700783
Arend van Spriel8da4a3a2011-03-02 21:18:42 +0100784static bool dhdsdio_download_firmware(struct dhd_bus *bus, void *sdh);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700785static int _dhdsdio_download_firmware(struct dhd_bus *bus);
786
787static int dhdsdio_download_code_file(struct dhd_bus *bus, char *image_path);
788static int dhdsdio_download_nvram(struct dhd_bus *bus);
Franky Lineb5dc512011-04-25 19:34:04 -0700789static void dhdsdio_chip_disablecore(bcmsdh_info_t *sdh, u32 corebase);
Franky Lincb63e4c2011-04-25 15:45:08 -0700790static int dhdsdio_chip_attach(struct dhd_bus *bus, void *regs);
Franky Lineb5dc512011-04-25 19:34:04 -0700791static void dhdsdio_chip_resetcore(bcmsdh_info_t *sdh, u32 corebase);
Franky Lin5d0d7a92011-04-25 19:34:05 -0700792static void dhdsdio_sdiod_drive_strength_init(struct dhd_bus *bus,
793 u32 drivestrength);
Franky Lincee3cf42011-04-25 19:34:06 -0700794static void dhdsdio_chip_detach(struct dhd_bus *bus);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700795
Grant Grundler4b455e02011-05-04 09:59:47 -0700796/* Packet free applicable unconditionally for sdio and sdspi.
797 * Conditional if bufpool was present for gspi bus.
798 */
799static void dhdsdio_pktfree2(dhd_bus_t *bus, struct sk_buff *pkt)
800{
Grant Grundler4b455e02011-05-04 09:59:47 -0700801 if ((bus->bus != SPI_BUS) || bus->usebufpool)
Roland Vossen67ad48b2011-06-01 13:45:51 +0200802 brcmu_pkt_buf_free_skb(pkt);
Grant Grundler4b455e02011-05-04 09:59:47 -0700803}
804
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700805static void dhd_dongle_setmemsize(struct dhd_bus *bus, int mem_size)
806{
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -0700807 s32 min_size = DONGLE_MIN_MEMSIZE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700808 /* Restrict the memsize to user specified limit */
809 DHD_ERROR(("user: Restrict the dongle ram size to %d, min %d\n",
810 dhd_dongle_memsize, min_size));
811 if ((dhd_dongle_memsize > min_size) &&
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -0700812 (dhd_dongle_memsize < (s32) bus->orig_ramsize))
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700813 bus->ramsize = dhd_dongle_memsize;
814}
815
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -0700816static int dhdsdio_set_siaddr_window(dhd_bus_t *bus, u32 address)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700817{
818 int err = 0;
819 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_SBADDRLOW,
820 (address >> 8) & SBSDIO_SBADDRLOW_MASK, &err);
821 if (!err)
822 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_SBADDRMID,
823 (address >> 16) & SBSDIO_SBADDRMID_MASK, &err);
824 if (!err)
825 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_SBADDRHIGH,
826 (address >> 24) & SBSDIO_SBADDRHIGH_MASK,
827 &err);
828 return err;
829}
830
831/* Turn backplane clock on or off */
832static int dhdsdio_htclk(dhd_bus_t *bus, bool on, bool pendok)
833{
834 int err;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700835 u8 clkctl, clkreq, devctl;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700836 bcmsdh_info_t *sdh;
837
838 DHD_TRACE(("%s: Enter\n", __func__));
839
840#if defined(OOB_INTR_ONLY)
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -0700841 pendok = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700842#endif
843 clkctl = 0;
844 sdh = bus->sdh;
845
846 if (on) {
847 /* Request HT Avail */
848 clkreq =
849 bus->alp_only ? SBSDIO_ALP_AVAIL_REQ : SBSDIO_HT_AVAIL_REQ;
850
Franky Linc05df632011-04-25 19:34:07 -0700851 if ((bus->ci->chip == BCM4329_CHIP_ID)
852 && (bus->ci->chiprev == 0))
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700853 clkreq |= SBSDIO_FORCE_ALP;
854
855 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
856 clkreq, &err);
857 if (err) {
858 DHD_ERROR(("%s: HT Avail request error: %d\n",
859 __func__, err));
Roland Vossenb74ac122011-05-03 11:35:20 +0200860 return -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700861 }
862
Franky Linc05df632011-04-25 19:34:07 -0700863 if (pendok && ((bus->ci->buscoretype == PCMCIA_CORE_ID)
864 && (bus->ci->buscorerev == 9))) {
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -0700865 u32 dummy, retries;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700866 R_SDREG(dummy, &bus->regs->clockctlstatus, retries);
867 }
868
869 /* Check current status */
870 clkctl =
871 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
872 &err);
873 if (err) {
874 DHD_ERROR(("%s: HT Avail read error: %d\n",
875 __func__, err));
Roland Vossenb74ac122011-05-03 11:35:20 +0200876 return -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700877 }
878
879 /* Go to pending and await interrupt if appropriate */
880 if (!SBSDIO_CLKAV(clkctl, bus->alp_only) && pendok) {
881 /* Allow only clock-available interrupt */
882 devctl =
883 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
884 &err);
885 if (err) {
886 DHD_ERROR(("%s: Devctl error setting CA: %d\n",
887 __func__, err));
Roland Vossenb74ac122011-05-03 11:35:20 +0200888 return -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700889 }
890
891 devctl |= SBSDIO_DEVCTL_CA_INT_ONLY;
892 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
893 devctl, &err);
894 DHD_INFO(("CLKCTL: set PENDING\n"));
895 bus->clkstate = CLK_PENDING;
896
Roland Vossena1c5ad82011-04-11 15:16:24 +0200897 return 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700898 } else if (bus->clkstate == CLK_PENDING) {
899 /* Cancel CA-only interrupt filter */
900 devctl =
901 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
902 &err);
903 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
904 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
905 devctl, &err);
906 }
907
908 /* Otherwise, wait here (polling) for HT Avail */
909 if (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
910 SPINWAIT_SLEEP(sdioh_spinwait_sleep,
911 ((clkctl =
912 bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
913 SBSDIO_FUNC1_CHIPCLKCSR,
914 &err)),
915 !SBSDIO_CLKAV(clkctl, bus->alp_only)),
916 PMU_MAX_TRANSITION_DLY);
917 }
918 if (err) {
919 DHD_ERROR(("%s: HT Avail request error: %d\n",
920 __func__, err));
Roland Vossenb74ac122011-05-03 11:35:20 +0200921 return -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700922 }
923 if (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
924 DHD_ERROR(("%s: HT Avail timeout (%d): clkctl 0x%02x\n",
925 __func__, PMU_MAX_TRANSITION_DLY, clkctl));
Roland Vossenb74ac122011-05-03 11:35:20 +0200926 return -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700927 }
928
929 /* Mark clock available */
930 bus->clkstate = CLK_AVAIL;
931 DHD_INFO(("CLKCTL: turned ON\n"));
932
933#if defined(DHD_DEBUG)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -0700934 if (bus->alp_only == true) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700935#if !defined(BCMLXSDMMC)
936 if (!SBSDIO_ALPONLY(clkctl)) {
937 DHD_ERROR(("%s: HT Clock, when ALP Only\n",
938 __func__));
939 }
940#endif /* !defined(BCMLXSDMMC) */
941 } else {
942 if (SBSDIO_ALPONLY(clkctl)) {
943 DHD_ERROR(("%s: HT Clock should be on.\n",
944 __func__));
945 }
946 }
947#endif /* defined (DHD_DEBUG) */
948
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -0700949 bus->activity = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700950 } else {
951 clkreq = 0;
952
953 if (bus->clkstate == CLK_PENDING) {
954 /* Cancel CA-only interrupt filter */
955 devctl =
956 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
957 &err);
958 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
959 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
960 devctl, &err);
961 }
962
963 bus->clkstate = CLK_SDONLY;
964 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
965 clkreq, &err);
966 DHD_INFO(("CLKCTL: turned OFF\n"));
967 if (err) {
968 DHD_ERROR(("%s: Failed access turning clock off: %d\n",
969 __func__, err));
Roland Vossenb74ac122011-05-03 11:35:20 +0200970 return -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700971 }
972 }
Roland Vossena1c5ad82011-04-11 15:16:24 +0200973 return 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700974}
975
976/* Change idle/active SD state */
977static int dhdsdio_sdclk(dhd_bus_t *bus, bool on)
978{
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700979 DHD_TRACE(("%s: Enter\n", __func__));
980
Franky Lin602a8ab2011-06-01 13:45:04 +0200981 if (on)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700982 bus->clkstate = CLK_SDONLY;
Franky Lin602a8ab2011-06-01 13:45:04 +0200983 else
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700984 bus->clkstate = CLK_NONE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700985
Roland Vossena1c5ad82011-04-11 15:16:24 +0200986 return 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700987}
988
989/* Transition SD and backplane clock readiness */
990static int dhdsdio_clkctl(dhd_bus_t *bus, uint target, bool pendok)
991{
992#ifdef DHD_DEBUG
993 uint oldstate = bus->clkstate;
994#endif /* DHD_DEBUG */
995
996 DHD_TRACE(("%s: Enter\n", __func__));
997
998 /* Early exit if we're already there */
999 if (bus->clkstate == target) {
1000 if (target == CLK_AVAIL) {
1001 dhd_os_wd_timer(bus->dhd, dhd_watchdog_ms);
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001002 bus->activity = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001003 }
Roland Vossena1c5ad82011-04-11 15:16:24 +02001004 return 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001005 }
1006
1007 switch (target) {
1008 case CLK_AVAIL:
1009 /* Make sure SD clock is available */
1010 if (bus->clkstate == CLK_NONE)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001011 dhdsdio_sdclk(bus, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001012 /* Now request HT Avail on the backplane */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001013 dhdsdio_htclk(bus, true, pendok);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001014 dhd_os_wd_timer(bus->dhd, dhd_watchdog_ms);
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001015 bus->activity = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001016 break;
1017
1018 case CLK_SDONLY:
1019 /* Remove HT request, or bring up SD clock */
1020 if (bus->clkstate == CLK_NONE)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001021 dhdsdio_sdclk(bus, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001022 else if (bus->clkstate == CLK_AVAIL)
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001023 dhdsdio_htclk(bus, false, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001024 else
1025 DHD_ERROR(("dhdsdio_clkctl: request for %d -> %d\n",
1026 bus->clkstate, target));
1027 dhd_os_wd_timer(bus->dhd, dhd_watchdog_ms);
1028 break;
1029
1030 case CLK_NONE:
1031 /* Make sure to remove HT request */
1032 if (bus->clkstate == CLK_AVAIL)
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001033 dhdsdio_htclk(bus, false, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001034 /* Now remove the SD clock */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001035 dhdsdio_sdclk(bus, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001036 dhd_os_wd_timer(bus->dhd, 0);
1037 break;
1038 }
1039#ifdef DHD_DEBUG
1040 DHD_INFO(("dhdsdio_clkctl: %d -> %d\n", oldstate, bus->clkstate));
1041#endif /* DHD_DEBUG */
1042
Roland Vossena1c5ad82011-04-11 15:16:24 +02001043 return 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001044}
1045
1046int dhdsdio_bussleep(dhd_bus_t *bus, bool sleep)
1047{
1048 bcmsdh_info_t *sdh = bus->sdh;
Franky Lin597600a2011-06-01 13:45:39 +02001049 struct sdpcmd_regs *regs = bus->regs;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001050 uint retries = 0;
1051
1052 DHD_INFO(("dhdsdio_bussleep: request %s (currently %s)\n",
1053 (sleep ? "SLEEP" : "WAKE"),
1054 (bus->sleeping ? "SLEEP" : "WAKE")));
1055
1056 /* Done if we're already in the requested state */
1057 if (sleep == bus->sleeping)
Roland Vossena1c5ad82011-04-11 15:16:24 +02001058 return 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001059
1060 /* Going to sleep: set the alarm and turn off the lights... */
1061 if (sleep) {
1062 /* Don't sleep if something is pending */
1063 if (bus->dpc_sched || bus->rxskip || pktq_len(&bus->txq))
Roland Vossene10d82d2011-05-03 11:35:19 +02001064 return -EBUSY;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001065
1066 /* Disable SDIO interrupts (no longer interested) */
1067 bcmsdh_intr_disable(bus->sdh);
1068
1069 /* Make sure the controller has the bus up */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001070 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001071
1072 /* Tell device to start using OOB wakeup */
1073 W_SDREG(SMB_USE_OOB, &regs->tosbmailbox, retries);
1074 if (retries > retry_limit)
1075 DHD_ERROR(("CANNOT SIGNAL CHIP, WILL NOT WAKE UP!!\n"));
1076
1077 /* Turn off our contribution to the HT clock request */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001078 dhdsdio_clkctl(bus, CLK_SDONLY, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001079
1080 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
1081 SBSDIO_FORCE_HW_CLKREQ_OFF, NULL);
1082
1083 /* Isolate the bus */
Franky Linc05df632011-04-25 19:34:07 -07001084 if (bus->ci->chip != BCM4329_CHIP_ID
1085 && bus->ci->chip != BCM4319_CHIP_ID) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001086 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
1087 SBSDIO_DEVCTL_PADS_ISO, NULL);
1088 }
1089
1090 /* Change state */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001091 bus->sleeping = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001092
1093 } else {
1094 /* Waking up: bus power up is ok, set local state */
1095
1096 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
1097 0, NULL);
1098
1099 /* Force pad isolation off if possible
1100 (in case power never toggled) */
Franky Linc05df632011-04-25 19:34:07 -07001101 if ((bus->ci->buscoretype == PCMCIA_CORE_ID)
1102 && (bus->ci->buscorerev >= 10))
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001103 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL, 0,
1104 NULL);
1105
1106 /* Make sure the controller has the bus up */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001107 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001108
1109 /* Send misc interrupt to indicate OOB not needed */
1110 W_SDREG(0, &regs->tosbmailboxdata, retries);
1111 if (retries <= retry_limit)
1112 W_SDREG(SMB_DEV_INT, &regs->tosbmailbox, retries);
1113
1114 if (retries > retry_limit)
1115 DHD_ERROR(("CANNOT SIGNAL CHIP TO CLEAR OOB!!\n"));
1116
1117 /* Make sure we have SD bus access */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001118 dhdsdio_clkctl(bus, CLK_SDONLY, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001119
1120 /* Change state */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001121 bus->sleeping = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001122
1123 /* Enable interrupts again */
1124 if (bus->intr && (bus->dhd->busstate == DHD_BUS_DATA)) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001125 bus->intdis = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001126 bcmsdh_intr_enable(bus->sdh);
1127 }
1128 }
1129
Roland Vossena1c5ad82011-04-11 15:16:24 +02001130 return 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001131}
1132
1133#if defined(OOB_INTR_ONLY)
1134void dhd_enable_oob_intr(struct dhd_bus *bus, bool enable)
1135{
1136#if defined(HW_OOB)
1137 bcmsdh_enable_hw_oob_intr(bus->sdh, enable);
1138#else
1139 sdpcmd_regs_t *regs = bus->regs;
1140 uint retries = 0;
1141
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001142 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001143 if (enable == true) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001144
1145 /* Tell device to start using OOB wakeup */
1146 W_SDREG(SMB_USE_OOB, &regs->tosbmailbox, retries);
1147 if (retries > retry_limit)
1148 DHD_ERROR(("CANNOT SIGNAL CHIP, WILL NOT WAKE UP!!\n"));
1149
1150 } else {
1151 /* Send misc interrupt to indicate OOB not needed */
1152 W_SDREG(0, &regs->tosbmailboxdata, retries);
1153 if (retries <= retry_limit)
1154 W_SDREG(SMB_DEV_INT, &regs->tosbmailbox, retries);
1155 }
1156
1157 /* Turn off our contribution to the HT clock request */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001158 dhdsdio_clkctl(bus, CLK_SDONLY, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001159#endif /* !defined(HW_OOB) */
1160}
1161#endif /* defined(OOB_INTR_ONLY) */
1162
1163#define BUS_WAKE(bus) \
1164 do { \
1165 if ((bus)->sleeping) \
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001166 dhdsdio_bussleep((bus), false); \
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001167 } while (0);
1168
1169/* Writes a HW/SW header into the packet and sends it. */
1170/* Assumes: (a) header space already there, (b) caller holds lock */
Arend van Sprielc26b1372010-11-23 14:06:23 +01001171static int dhdsdio_txpkt(dhd_bus_t *bus, struct sk_buff *pkt, uint chan,
1172 bool free_pkt)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001173{
1174 int ret;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001175 u8 *frame;
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07001176 u16 len, pad = 0;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07001177 u32 swheader;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001178 uint retries = 0;
1179 bcmsdh_info_t *sdh;
Arend van Sprielc26b1372010-11-23 14:06:23 +01001180 struct sk_buff *new;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001181 int i;
1182
1183 DHD_TRACE(("%s: Enter\n", __func__));
1184
1185 sdh = bus->sdh;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001186
1187 if (bus->dhd->dongle_reset) {
Roland Vossenb74ac122011-05-03 11:35:20 +02001188 ret = -EPERM;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001189 goto done;
1190 }
1191
Arend van Spriel54991ad2010-11-23 14:06:24 +01001192 frame = (u8 *) (pkt->data);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001193
1194 /* Add alignment padding, allocate new packet if needed */
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07001195 pad = ((unsigned long)frame % DHD_SDALIGN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001196 if (pad) {
Arend van Spriel3be727c2010-11-23 22:20:30 +01001197 if (skb_headroom(pkt) < pad) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001198 DHD_INFO(("%s: insufficient headroom %d for %d pad\n",
Arend van Spriel3be727c2010-11-23 22:20:30 +01001199 __func__, skb_headroom(pkt), pad));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001200 bus->dhd->tx_realloc++;
Roland Vossen67ad48b2011-06-01 13:45:51 +02001201 new = brcmu_pkt_buf_get_skb(pkt->len + DHD_SDALIGN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001202 if (!new) {
1203 DHD_ERROR(("%s: couldn't allocate new %d-byte "
1204 "packet\n",
Arend van Spriel54991ad2010-11-23 14:06:24 +01001205 __func__, pkt->len + DHD_SDALIGN));
Roland Vossene10d82d2011-05-03 11:35:19 +02001206 ret = -ENOMEM;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001207 goto done;
1208 }
1209
Arend van Spriel3c9d4c32011-03-02 21:18:48 +01001210 PKTALIGN(new, pkt->len, DHD_SDALIGN);
Stanislav Fomichev02160692011-02-15 01:05:10 +03001211 memcpy(new->data, pkt->data, pkt->len);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001212 if (free_pkt)
Roland Vossen67ad48b2011-06-01 13:45:51 +02001213 brcmu_pkt_buf_free_skb(pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001214 /* free the pkt if canned one is not used */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001215 free_pkt = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001216 pkt = new;
Arend van Spriel54991ad2010-11-23 14:06:24 +01001217 frame = (u8 *) (pkt->data);
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07001218 ASSERT(((unsigned long)frame % DHD_SDALIGN) == 0);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001219 pad = 0;
1220 } else {
Arend van Sprielc303ecb2010-11-18 20:46:43 +01001221 skb_push(pkt, pad);
Arend van Spriel54991ad2010-11-23 14:06:24 +01001222 frame = (u8 *) (pkt->data);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001223
Arend van Spriel54991ad2010-11-23 14:06:24 +01001224 ASSERT((pad + SDPCM_HDRLEN) <= (int)(pkt->len));
Brett Rudley9249ede2010-11-30 20:09:49 -08001225 memset(frame, 0, pad + SDPCM_HDRLEN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001226 }
1227 }
1228 ASSERT(pad < DHD_SDALIGN);
1229
1230 /* Hardware tag: 2 byte len followed by 2 byte ~len check (all LE) */
Arend van Spriel54991ad2010-11-23 14:06:24 +01001231 len = (u16) (pkt->len);
Stanislav Fomichev628f10b2011-02-20 21:43:32 +03001232 *(u16 *) frame = cpu_to_le16(len);
1233 *(((u16 *) frame) + 1) = cpu_to_le16(~len);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001234
1235 /* Software tag: channel, sequence number, data offset */
1236 swheader =
1237 ((chan << SDPCM_CHANNEL_SHIFT) & SDPCM_CHANNEL_MASK) | bus->tx_seq |
1238 (((pad +
1239 SDPCM_HDRLEN) << SDPCM_DOFFSET_SHIFT) & SDPCM_DOFFSET_MASK);
Stanislav Fomichev56dfe3c2011-02-20 21:43:33 +03001240
1241 put_unaligned_le32(swheader, frame + SDPCM_FRAMETAG_LEN);
1242 put_unaligned_le32(0, frame + SDPCM_FRAMETAG_LEN + sizeof(swheader));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001243
1244#ifdef DHD_DEBUG
Arend van Spriel54991ad2010-11-23 14:06:24 +01001245 tx_packets[pkt->priority]++;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001246 if (DHD_BYTES_ON() &&
1247 (((DHD_CTL_ON() && (chan == SDPCM_CONTROL_CHANNEL)) ||
1248 (DHD_DATA_ON() && (chan != SDPCM_CONTROL_CHANNEL))))) {
Arend van Spriel34227312011-05-10 22:25:32 +02001249 printk(KERN_DEBUG "Tx Frame:\n");
1250 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, frame, len);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001251 } else if (DHD_HDRS_ON()) {
Arend van Spriel34227312011-05-10 22:25:32 +02001252 printk(KERN_DEBUG "TxHdr:\n");
1253 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
1254 frame, min_t(u16, len, 16));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001255 }
1256#endif
1257
1258 /* Raise len to next SDIO block to eliminate tail command */
1259 if (bus->roundup && bus->blocksize && (len > bus->blocksize)) {
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07001260 u16 pad = bus->blocksize - (len % bus->blocksize);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001261 if ((pad <= bus->roundup) && (pad < bus->blocksize))
1262#ifdef NOTUSED
Arend van Spriel3be727c2010-11-23 22:20:30 +01001263 if (pad <= skb_tailroom(pkt))
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001264#endif /* NOTUSED */
1265 len += pad;
1266 } else if (len % DHD_SDALIGN) {
1267 len += DHD_SDALIGN - (len % DHD_SDALIGN);
1268 }
1269
1270 /* Some controllers have trouble with odd bytes -- round to even */
1271 if (forcealign && (len & (ALIGNMENT - 1))) {
1272#ifdef NOTUSED
Arend van Spriel3be727c2010-11-23 22:20:30 +01001273 if (skb_tailroom(pkt))
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001274#endif
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07001275 len = roundup(len, ALIGNMENT);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001276#ifdef NOTUSED
1277 else
1278 DHD_ERROR(("%s: sending unrounded %d-byte packet\n",
1279 __func__, len));
1280#endif
1281 }
1282
1283 do {
1284 ret =
1285 dhd_bcmsdh_send_buf(bus, bcmsdh_cur_sbwad(sdh), SDIO_FUNC_2,
1286 F2SYNC, frame, len, pkt, NULL, NULL);
1287 bus->f2txdata++;
Roland Vossenb7ef2a92011-05-03 11:35:02 +02001288 ASSERT(ret != -BCME_PENDING);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001289
1290 if (ret < 0) {
1291 /* On failure, abort the command
1292 and terminate the frame */
1293 DHD_INFO(("%s: sdio error %d, abort command and "
1294 "terminate frame.\n", __func__, ret));
1295 bus->tx_sderrs++;
1296
1297 bcmsdh_abort(sdh, SDIO_FUNC_2);
1298 bcmsdh_cfg_write(sdh, SDIO_FUNC_1,
1299 SBSDIO_FUNC1_FRAMECTRL, SFC_WF_TERM,
1300 NULL);
1301 bus->f1regdata++;
1302
1303 for (i = 0; i < 3; i++) {
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001304 u8 hi, lo;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001305 hi = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
1306 SBSDIO_FUNC1_WFRAMEBCHI,
1307 NULL);
1308 lo = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
1309 SBSDIO_FUNC1_WFRAMEBCLO,
1310 NULL);
1311 bus->f1regdata += 2;
1312 if ((hi == 0) && (lo == 0))
1313 break;
1314 }
1315
1316 }
1317 if (ret == 0)
1318 bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQUENCE_WRAP;
1319
1320 } while ((ret < 0) && retrydata && retries++ < TXRETRIES);
1321
1322done:
1323 /* restore pkt buffer pointer before calling tx complete routine */
Arend van Sprielc303ecb2010-11-18 20:46:43 +01001324 skb_pull(pkt, SDPCM_HDRLEN + pad);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001325 dhd_os_sdunlock(bus->dhd);
1326 dhd_txcomplete(bus->dhd, pkt, ret != 0);
1327 dhd_os_sdlock(bus->dhd);
1328
1329 if (free_pkt)
Roland Vossen67ad48b2011-06-01 13:45:51 +02001330 brcmu_pkt_buf_free_skb(pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001331
1332 return ret;
1333}
1334
Arend van Sprielc26b1372010-11-23 14:06:23 +01001335int dhd_bus_txdata(struct dhd_bus *bus, struct sk_buff *pkt)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001336{
Roland Vossenb74ac122011-05-03 11:35:20 +02001337 int ret = -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001338 uint datalen, prec;
1339
1340 DHD_TRACE(("%s: Enter\n", __func__));
1341
Arend van Spriel54991ad2010-11-23 14:06:24 +01001342 datalen = pkt->len;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001343
1344#ifdef SDTEST
1345 /* Push the test header if doing loopback */
1346 if (bus->ext_loop) {
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001347 u8 *data;
Arend van Sprielc303ecb2010-11-18 20:46:43 +01001348 skb_push(pkt, SDPCM_TEST_HDRLEN);
Arend van Spriel54991ad2010-11-23 14:06:24 +01001349 data = pkt->data;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001350 *data++ = SDPCM_TEST_ECHOREQ;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001351 *data++ = (u8) bus->loopid++;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001352 *data++ = (datalen >> 0);
1353 *data++ = (datalen >> 8);
1354 datalen += SDPCM_TEST_HDRLEN;
1355 }
1356#endif /* SDTEST */
1357
1358 /* Add space for the header */
Arend van Sprielc303ecb2010-11-18 20:46:43 +01001359 skb_push(pkt, SDPCM_HDRLEN);
Arend van Spriel54991ad2010-11-23 14:06:24 +01001360 ASSERT(IS_ALIGNED((unsigned long)(pkt->data), 2));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001361
Arend van Spriel54991ad2010-11-23 14:06:24 +01001362 prec = PRIO2PREC((pkt->priority & PRIOMASK));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001363
1364 /* Check for existing queue, current flow-control,
1365 pending event, or pending clock */
1366 if (dhd_deferred_tx || bus->fcstate || pktq_len(&bus->txq)
1367 || bus->dpc_sched || (!DATAOK(bus))
1368 || (bus->flowcontrol & NBITVAL(prec))
1369 || (bus->clkstate != CLK_AVAIL)) {
1370 DHD_TRACE(("%s: deferring pktq len %d\n", __func__,
1371 pktq_len(&bus->txq)));
1372 bus->fcqueued++;
1373
1374 /* Priority based enq */
Franky Lin0f1a87c2011-06-29 16:46:32 -07001375 spin_lock_bh(&bus->txqlock);
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001376 if (dhd_prec_enq(bus->dhd, &bus->txq, pkt, prec) == false) {
Arend van Sprielc303ecb2010-11-18 20:46:43 +01001377 skb_pull(pkt, SDPCM_HDRLEN);
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001378 dhd_txcomplete(bus->dhd, pkt, false);
Roland Vossen67ad48b2011-06-01 13:45:51 +02001379 brcmu_pkt_buf_free_skb(pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001380 DHD_ERROR(("%s: out of bus->txq !!!\n", __func__));
Roland Vossene10d82d2011-05-03 11:35:19 +02001381 ret = -ENOSR;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001382 } else {
Roland Vossena1c5ad82011-04-11 15:16:24 +02001383 ret = 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001384 }
Franky Lin0f1a87c2011-06-29 16:46:32 -07001385 spin_unlock_bh(&bus->txqlock);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001386
Grant Grundler7c316072011-03-09 15:04:15 -08001387 if (pktq_len(&bus->txq) >= TXHI)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001388 dhd_txflowcontrol(bus->dhd, 0, ON);
1389
1390#ifdef DHD_DEBUG
1391 if (pktq_plen(&bus->txq, prec) > qcount[prec])
1392 qcount[prec] = pktq_plen(&bus->txq, prec);
1393#endif
1394 /* Schedule DPC if needed to send queued packet(s) */
1395 if (dhd_deferred_tx && !bus->dpc_sched) {
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001396 bus->dpc_sched = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001397 dhd_sched_dpc(bus->dhd);
1398 }
1399 } else {
1400 /* Lock: we're about to use shared data/code (and SDIO) */
1401 dhd_os_sdlock(bus->dhd);
1402
1403 /* Otherwise, send it now */
1404 BUS_WAKE(bus);
1405 /* Make sure back plane ht clk is on, no pending allowed */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001406 dhdsdio_clkctl(bus, CLK_AVAIL, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001407
1408#ifndef SDTEST
1409 DHD_TRACE(("%s: calling txpkt\n", __func__));
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001410 ret = dhdsdio_txpkt(bus, pkt, SDPCM_DATA_CHANNEL, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001411#else
1412 ret = dhdsdio_txpkt(bus, pkt,
1413 (bus->ext_loop ? SDPCM_TEST_CHANNEL :
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001414 SDPCM_DATA_CHANNEL), true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001415#endif
1416 if (ret)
1417 bus->dhd->tx_errors++;
1418 else
1419 bus->dhd->dstats.tx_bytes += datalen;
1420
1421 if ((bus->idletime == DHD_IDLE_IMMEDIATE) && !bus->dpc_sched) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001422 bus->activity = false;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001423 dhdsdio_clkctl(bus, CLK_NONE, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001424 }
1425
1426 dhd_os_sdunlock(bus->dhd);
1427 }
1428
1429 return ret;
1430}
1431
1432static uint dhdsdio_sendfromq(dhd_bus_t *bus, uint maxframes)
1433{
Arend van Sprielc26b1372010-11-23 14:06:23 +01001434 struct sk_buff *pkt;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07001435 u32 intstatus = 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001436 uint retries = 0;
1437 int ret = 0, prec_out;
1438 uint cnt = 0;
1439 uint datalen;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001440 u8 tx_prec_map;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001441
1442 dhd_pub_t *dhd = bus->dhd;
Franky Lin597600a2011-06-01 13:45:39 +02001443 struct sdpcmd_regs *regs = bus->regs;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001444
1445 DHD_TRACE(("%s: Enter\n", __func__));
1446
1447 tx_prec_map = ~bus->flowcontrol;
1448
1449 /* Send frames until the limit or some other event */
1450 for (cnt = 0; (cnt < maxframes) && DATAOK(bus); cnt++) {
Franky Lin0f1a87c2011-06-29 16:46:32 -07001451 spin_lock_bh(&bus->txqlock);
Roland Vossen67ad48b2011-06-01 13:45:51 +02001452 pkt = brcmu_pktq_mdeq(&bus->txq, tx_prec_map, &prec_out);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001453 if (pkt == NULL) {
Franky Lin0f1a87c2011-06-29 16:46:32 -07001454 spin_unlock_bh(&bus->txqlock);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001455 break;
1456 }
Franky Lin0f1a87c2011-06-29 16:46:32 -07001457 spin_unlock_bh(&bus->txqlock);
Arend van Spriel54991ad2010-11-23 14:06:24 +01001458 datalen = pkt->len - SDPCM_HDRLEN;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001459
1460#ifndef SDTEST
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001461 ret = dhdsdio_txpkt(bus, pkt, SDPCM_DATA_CHANNEL, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001462#else
1463 ret = dhdsdio_txpkt(bus, pkt,
1464 (bus->ext_loop ? SDPCM_TEST_CHANNEL :
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001465 SDPCM_DATA_CHANNEL), true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001466#endif
1467 if (ret)
1468 bus->dhd->tx_errors++;
1469 else
1470 bus->dhd->dstats.tx_bytes += datalen;
1471
1472 /* In poll mode, need to check for other events */
1473 if (!bus->intr && cnt) {
1474 /* Check device status, signal pending interrupt */
1475 R_SDREG(intstatus, &regs->intstatus, retries);
1476 bus->f2txdata++;
1477 if (bcmsdh_regfail(bus->sdh))
1478 break;
1479 if (intstatus & bus->hostintmask)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001480 bus->ipend = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001481 }
1482 }
1483
1484 /* Deflow-control stack if needed */
Grant Grundler7c316072011-03-09 15:04:15 -08001485 if (dhd->up && (dhd->busstate == DHD_BUS_DATA) &&
Grant Grundler26a71a42011-03-09 10:41:25 -08001486 dhd->txoff && (pktq_len(&bus->txq) < TXLOW))
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001487 dhd_txflowcontrol(dhd, 0, OFF);
1488
1489 return cnt;
1490}
1491
Greg Kroah-Hartman580a0bd2010-10-05 11:09:48 -07001492int dhd_bus_txctl(struct dhd_bus *bus, unsigned char *msg, uint msglen)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001493{
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001494 u8 *frame;
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07001495 u16 len;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07001496 u32 swheader;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001497 uint retries = 0;
1498 bcmsdh_info_t *sdh = bus->sdh;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001499 u8 doff = 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001500 int ret = -1;
1501 int i;
1502
1503 DHD_TRACE(("%s: Enter\n", __func__));
1504
1505 if (bus->dhd->dongle_reset)
1506 return -EIO;
1507
1508 /* Back the pointer to make a room for bus header */
1509 frame = msg - SDPCM_HDRLEN;
1510 len = (msglen += SDPCM_HDRLEN);
1511
1512 /* Add alignment padding (optional for ctl frames) */
1513 if (dhd_alignctl) {
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07001514 doff = ((unsigned long)frame % DHD_SDALIGN);
Jason Cooper9b890322010-09-30 15:15:39 -04001515 if (doff) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001516 frame -= doff;
1517 len += doff;
1518 msglen += doff;
Brett Rudley9249ede2010-11-30 20:09:49 -08001519 memset(frame, 0, doff + SDPCM_HDRLEN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001520 }
1521 ASSERT(doff < DHD_SDALIGN);
1522 }
1523 doff += SDPCM_HDRLEN;
1524
1525 /* Round send length to next SDIO block */
1526 if (bus->roundup && bus->blocksize && (len > bus->blocksize)) {
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07001527 u16 pad = bus->blocksize - (len % bus->blocksize);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001528 if ((pad <= bus->roundup) && (pad < bus->blocksize))
1529 len += pad;
1530 } else if (len % DHD_SDALIGN) {
1531 len += DHD_SDALIGN - (len % DHD_SDALIGN);
1532 }
1533
1534 /* Satisfy length-alignment requirements */
1535 if (forcealign && (len & (ALIGNMENT - 1)))
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07001536 len = roundup(len, ALIGNMENT);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001537
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07001538 ASSERT(IS_ALIGNED((unsigned long)frame, 2));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001539
1540 /* Need to lock here to protect txseq and SDIO tx calls */
1541 dhd_os_sdlock(bus->dhd);
1542
1543 BUS_WAKE(bus);
1544
1545 /* Make sure backplane clock is on */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001546 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001547
1548 /* Hardware tag: 2 byte len followed by 2 byte ~len check (all LE) */
Stanislav Fomichev628f10b2011-02-20 21:43:32 +03001549 *(u16 *) frame = cpu_to_le16((u16) msglen);
1550 *(((u16 *) frame) + 1) = cpu_to_le16(~msglen);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001551
1552 /* Software tag: channel, sequence number, data offset */
1553 swheader =
1554 ((SDPCM_CONTROL_CHANNEL << SDPCM_CHANNEL_SHIFT) &
1555 SDPCM_CHANNEL_MASK)
1556 | bus->tx_seq | ((doff << SDPCM_DOFFSET_SHIFT) &
1557 SDPCM_DOFFSET_MASK);
Stanislav Fomichev56dfe3c2011-02-20 21:43:33 +03001558 put_unaligned_le32(swheader, frame + SDPCM_FRAMETAG_LEN);
1559 put_unaligned_le32(0, frame + SDPCM_FRAMETAG_LEN + sizeof(swheader));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001560
1561 if (!DATAOK(bus)) {
1562 DHD_INFO(("%s: No bus credit bus->tx_max %d, bus->tx_seq %d\n",
1563 __func__, bus->tx_max, bus->tx_seq));
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001564 bus->ctrl_frame_stat = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001565 /* Send from dpc */
1566 bus->ctrl_frame_buf = frame;
1567 bus->ctrl_frame_len = len;
1568
1569 dhd_wait_for_event(bus->dhd, &bus->ctrl_frame_stat);
1570
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001571 if (bus->ctrl_frame_stat == false) {
1572 DHD_INFO(("%s: ctrl_frame_stat == false\n", __func__));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001573 ret = 0;
1574 } else {
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001575 DHD_INFO(("%s: ctrl_frame_stat == true\n", __func__));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001576 ret = -1;
1577 }
1578 }
1579
1580 if (ret == -1) {
1581#ifdef DHD_DEBUG
Arend van Spriel34227312011-05-10 22:25:32 +02001582 if (DHD_BYTES_ON() && DHD_CTL_ON()) {
1583 printk(KERN_DEBUG "Tx Frame:\n");
1584 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
1585 frame, len);
1586 } else if (DHD_HDRS_ON()) {
1587 printk(KERN_DEBUG "TxHdr:\n");
1588 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
1589 frame, min_t(u16, len, 16));
1590 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001591#endif
1592
1593 do {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001594 bus->ctrl_frame_stat = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001595 ret =
1596 dhd_bcmsdh_send_buf(bus, bcmsdh_cur_sbwad(sdh),
1597 SDIO_FUNC_2, F2SYNC, frame, len,
1598 NULL, NULL, NULL);
1599
Roland Vossenb7ef2a92011-05-03 11:35:02 +02001600 ASSERT(ret != -BCME_PENDING);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001601
1602 if (ret < 0) {
1603 /* On failure, abort the command and
1604 terminate the frame */
1605 DHD_INFO(("%s: sdio error %d, abort command and terminate frame.\n",
1606 __func__, ret));
1607 bus->tx_sderrs++;
1608
1609 bcmsdh_abort(sdh, SDIO_FUNC_2);
1610
1611 bcmsdh_cfg_write(sdh, SDIO_FUNC_1,
1612 SBSDIO_FUNC1_FRAMECTRL,
1613 SFC_WF_TERM, NULL);
1614 bus->f1regdata++;
1615
1616 for (i = 0; i < 3; i++) {
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001617 u8 hi, lo;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001618 hi = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
1619 SBSDIO_FUNC1_WFRAMEBCHI,
1620 NULL);
1621 lo = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
1622 SBSDIO_FUNC1_WFRAMEBCLO,
1623 NULL);
1624 bus->f1regdata += 2;
1625 if ((hi == 0) && (lo == 0))
1626 break;
1627 }
1628
1629 }
1630 if (ret == 0) {
1631 bus->tx_seq =
1632 (bus->tx_seq + 1) % SDPCM_SEQUENCE_WRAP;
1633 }
1634 } while ((ret < 0) && retries++ < TXRETRIES);
1635 }
1636
1637 if ((bus->idletime == DHD_IDLE_IMMEDIATE) && !bus->dpc_sched) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001638 bus->activity = false;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001639 dhdsdio_clkctl(bus, CLK_NONE, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001640 }
1641
1642 dhd_os_sdunlock(bus->dhd);
1643
1644 if (ret)
1645 bus->dhd->tx_ctlerrs++;
1646 else
1647 bus->dhd->tx_ctlpkts++;
1648
1649 return ret ? -EIO : 0;
1650}
1651
Greg Kroah-Hartman580a0bd2010-10-05 11:09:48 -07001652int dhd_bus_rxctl(struct dhd_bus *bus, unsigned char *msg, uint msglen)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001653{
1654 int timeleft;
1655 uint rxlen = 0;
1656 bool pending;
1657
1658 DHD_TRACE(("%s: Enter\n", __func__));
1659
1660 if (bus->dhd->dongle_reset)
1661 return -EIO;
1662
1663 /* Wait until control frame is available */
1664 timeleft = dhd_os_ioctl_resp_wait(bus->dhd, &bus->rxlen, &pending);
1665
1666 dhd_os_sdlock(bus->dhd);
1667 rxlen = bus->rxlen;
Stanislav Fomichev02160692011-02-15 01:05:10 +03001668 memcpy(msg, bus->rxctl, min(msglen, rxlen));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001669 bus->rxlen = 0;
1670 dhd_os_sdunlock(bus->dhd);
1671
1672 if (rxlen) {
1673 DHD_CTL(("%s: resumed on rxctl frame, got %d expected %d\n",
1674 __func__, rxlen, msglen));
1675 } else if (timeleft == 0) {
1676 DHD_ERROR(("%s: resumed on timeout\n", __func__));
1677#ifdef DHD_DEBUG
1678 dhd_os_sdlock(bus->dhd);
1679 dhdsdio_checkdied(bus, NULL, 0);
1680 dhd_os_sdunlock(bus->dhd);
1681#endif /* DHD_DEBUG */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001682 } else if (pending == true) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001683 DHD_CTL(("%s: cancelled\n", __func__));
1684 return -ERESTARTSYS;
1685 } else {
1686 DHD_CTL(("%s: resumed for unknown reason?\n", __func__));
1687#ifdef DHD_DEBUG
1688 dhd_os_sdlock(bus->dhd);
1689 dhdsdio_checkdied(bus, NULL, 0);
1690 dhd_os_sdunlock(bus->dhd);
1691#endif /* DHD_DEBUG */
1692 }
1693
1694 if (rxlen)
1695 bus->dhd->rx_ctlpkts++;
1696 else
1697 bus->dhd->rx_ctlerrs++;
1698
Jason Coopere9887c92010-10-06 10:08:02 -04001699 return rxlen ? (int)rxlen : -ETIMEDOUT;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001700}
1701
1702/* IOVar table */
1703enum {
1704 IOV_INTR = 1,
1705 IOV_POLLRATE,
1706 IOV_SDREG,
1707 IOV_SBREG,
1708 IOV_SDCIS,
1709 IOV_MEMBYTES,
1710 IOV_MEMSIZE,
1711#ifdef DHD_DEBUG
1712 IOV_CHECKDIED,
1713#endif
1714 IOV_DOWNLOAD,
1715 IOV_FORCEEVEN,
1716 IOV_SDIOD_DRIVE,
1717 IOV_READAHEAD,
1718 IOV_SDRXCHAIN,
1719 IOV_ALIGNCTL,
1720 IOV_SDALIGN,
1721 IOV_DEVRESET,
1722 IOV_CPU,
1723#ifdef SDTEST
1724 IOV_PKTGEN,
1725 IOV_EXTLOOP,
1726#endif /* SDTEST */
1727 IOV_SPROM,
1728 IOV_TXBOUND,
1729 IOV_RXBOUND,
1730 IOV_TXMINMAX,
1731 IOV_IDLETIME,
1732 IOV_IDLECLOCK,
1733 IOV_SD1IDLE,
1734 IOV_SLEEP,
1735 IOV_VARS
1736};
1737
Roland Vossen67ad48b2011-06-01 13:45:51 +02001738const struct brcmu_iovar dhdsdio_iovars[] = {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001739 {"intr", IOV_INTR, 0, IOVT_BOOL, 0},
1740 {"sleep", IOV_SLEEP, 0, IOVT_BOOL, 0},
1741 {"pollrate", IOV_POLLRATE, 0, IOVT_UINT32, 0},
1742 {"idletime", IOV_IDLETIME, 0, IOVT_INT32, 0},
1743 {"idleclock", IOV_IDLECLOCK, 0, IOVT_INT32, 0},
1744 {"sd1idle", IOV_SD1IDLE, 0, IOVT_BOOL, 0},
1745 {"membytes", IOV_MEMBYTES, 0, IOVT_BUFFER, 2 * sizeof(int)},
1746 {"memsize", IOV_MEMSIZE, 0, IOVT_UINT32, 0},
1747 {"download", IOV_DOWNLOAD, 0, IOVT_BOOL, 0},
1748 {"vars", IOV_VARS, 0, IOVT_BUFFER, 0},
1749 {"sdiod_drive", IOV_SDIOD_DRIVE, 0, IOVT_UINT32, 0},
1750 {"readahead", IOV_READAHEAD, 0, IOVT_BOOL, 0},
1751 {"sdrxchain", IOV_SDRXCHAIN, 0, IOVT_BOOL, 0},
1752 {"alignctl", IOV_ALIGNCTL, 0, IOVT_BOOL, 0},
1753 {"sdalign", IOV_SDALIGN, 0, IOVT_BOOL, 0},
1754 {"devreset", IOV_DEVRESET, 0, IOVT_BOOL, 0},
1755#ifdef DHD_DEBUG
1756 {"sdreg", IOV_SDREG, 0, IOVT_BUFFER, sizeof(sdreg_t)}
1757 ,
1758 {"sbreg", IOV_SBREG, 0, IOVT_BUFFER, sizeof(sdreg_t)}
1759 ,
1760 {"sd_cis", IOV_SDCIS, 0, IOVT_BUFFER, DHD_IOCTL_MAXLEN}
1761 ,
1762 {"forcealign", IOV_FORCEEVEN, 0, IOVT_BOOL, 0}
1763 ,
1764 {"txbound", IOV_TXBOUND, 0, IOVT_UINT32, 0}
1765 ,
1766 {"rxbound", IOV_RXBOUND, 0, IOVT_UINT32, 0}
1767 ,
1768 {"txminmax", IOV_TXMINMAX, 0, IOVT_UINT32, 0}
1769 ,
1770 {"cpu", IOV_CPU, 0, IOVT_BOOL, 0}
1771 ,
1772#ifdef DHD_DEBUG
1773 {"checkdied", IOV_CHECKDIED, 0, IOVT_BUFFER, 0}
1774 ,
1775#endif /* DHD_DEBUG */
1776#endif /* DHD_DEBUG */
1777#ifdef SDTEST
1778 {"extloop", IOV_EXTLOOP, 0, IOVT_BOOL, 0}
1779 ,
1780 {"pktgen", IOV_PKTGEN, 0, IOVT_BUFFER, sizeof(dhd_pktgen_t)}
1781 ,
1782#endif /* SDTEST */
1783
1784 {NULL, 0, 0, 0, 0}
1785};
1786
1787static void
Roland Vossen67ad48b2011-06-01 13:45:51 +02001788dhd_dump_pct(struct brcmu_strbuf *strbuf, char *desc, uint num, uint div)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001789{
1790 uint q1, q2;
1791
1792 if (!div) {
Roland Vossen67ad48b2011-06-01 13:45:51 +02001793 brcmu_bprintf(strbuf, "%s N/A", desc);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001794 } else {
1795 q1 = num / div;
1796 q2 = (100 * (num - (q1 * div))) / div;
Roland Vossen67ad48b2011-06-01 13:45:51 +02001797 brcmu_bprintf(strbuf, "%s %d.%02d", desc, q1, q2);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001798 }
1799}
1800
Roland Vossen67ad48b2011-06-01 13:45:51 +02001801void dhd_bus_dump(dhd_pub_t *dhdp, struct brcmu_strbuf *strbuf)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001802{
1803 dhd_bus_t *bus = dhdp->bus;
1804
Roland Vossen67ad48b2011-06-01 13:45:51 +02001805 brcmu_bprintf(strbuf, "Bus SDIO structure:\n");
1806 brcmu_bprintf(strbuf,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001807 "hostintmask 0x%08x intstatus 0x%08x sdpcm_ver %d\n",
1808 bus->hostintmask, bus->intstatus, bus->sdpcm_ver);
Roland Vossen67ad48b2011-06-01 13:45:51 +02001809 brcmu_bprintf(strbuf,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001810 "fcstate %d qlen %d tx_seq %d, max %d, rxskip %d rxlen %d rx_seq %d\n",
1811 bus->fcstate, pktq_len(&bus->txq), bus->tx_seq, bus->tx_max,
1812 bus->rxskip, bus->rxlen, bus->rx_seq);
Roland Vossen67ad48b2011-06-01 13:45:51 +02001813 brcmu_bprintf(strbuf, "intr %d intrcount %d lastintrs %d spurious %d\n",
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001814 bus->intr, bus->intrcount, bus->lastintrs, bus->spurious);
Roland Vossen67ad48b2011-06-01 13:45:51 +02001815 brcmu_bprintf(strbuf, "pollrate %d pollcnt %d regfails %d\n",
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001816 bus->pollrate, bus->pollcnt, bus->regfails);
1817
Roland Vossen67ad48b2011-06-01 13:45:51 +02001818 brcmu_bprintf(strbuf, "\nAdditional counters:\n");
1819 brcmu_bprintf(strbuf,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001820 "tx_sderrs %d fcqueued %d rxrtx %d rx_toolong %d rxc_errors %d\n",
1821 bus->tx_sderrs, bus->fcqueued, bus->rxrtx, bus->rx_toolong,
1822 bus->rxc_errors);
Roland Vossen67ad48b2011-06-01 13:45:51 +02001823 brcmu_bprintf(strbuf, "rx_hdrfail %d badhdr %d badseq %d\n",
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001824 bus->rx_hdrfail, bus->rx_badhdr, bus->rx_badseq);
Roland Vossen67ad48b2011-06-01 13:45:51 +02001825 brcmu_bprintf(strbuf, "fc_rcvd %d, fc_xoff %d, fc_xon %d\n",
1826 bus->fc_rcvd, bus->fc_xoff, bus->fc_xon);
1827 brcmu_bprintf(strbuf, "rxglomfail %d, rxglomframes %d, rxglompkts %d\n",
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001828 bus->rxglomfail, bus->rxglomframes, bus->rxglompkts);
Roland Vossen67ad48b2011-06-01 13:45:51 +02001829 brcmu_bprintf(strbuf, "f2rx (hdrs/data) %d (%d/%d), f2tx %d f1regs"
1830 " %d\n",
1831 (bus->f2rxhdrs + bus->f2rxdata), bus->f2rxhdrs,
1832 bus->f2rxdata, bus->f2txdata, bus->f1regdata);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001833 {
1834 dhd_dump_pct(strbuf, "\nRx: pkts/f2rd", bus->dhd->rx_packets,
1835 (bus->f2rxhdrs + bus->f2rxdata));
1836 dhd_dump_pct(strbuf, ", pkts/f1sd", bus->dhd->rx_packets,
1837 bus->f1regdata);
1838 dhd_dump_pct(strbuf, ", pkts/sd", bus->dhd->rx_packets,
1839 (bus->f2rxhdrs + bus->f2rxdata + bus->f1regdata));
1840 dhd_dump_pct(strbuf, ", pkts/int", bus->dhd->rx_packets,
1841 bus->intrcount);
Roland Vossen67ad48b2011-06-01 13:45:51 +02001842 brcmu_bprintf(strbuf, "\n");
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001843
1844 dhd_dump_pct(strbuf, "Rx: glom pct", (100 * bus->rxglompkts),
1845 bus->dhd->rx_packets);
1846 dhd_dump_pct(strbuf, ", pkts/glom", bus->rxglompkts,
1847 bus->rxglomframes);
Roland Vossen67ad48b2011-06-01 13:45:51 +02001848 brcmu_bprintf(strbuf, "\n");
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001849
1850 dhd_dump_pct(strbuf, "Tx: pkts/f2wr", bus->dhd->tx_packets,
1851 bus->f2txdata);
1852 dhd_dump_pct(strbuf, ", pkts/f1sd", bus->dhd->tx_packets,
1853 bus->f1regdata);
1854 dhd_dump_pct(strbuf, ", pkts/sd", bus->dhd->tx_packets,
1855 (bus->f2txdata + bus->f1regdata));
1856 dhd_dump_pct(strbuf, ", pkts/int", bus->dhd->tx_packets,
1857 bus->intrcount);
Roland Vossen67ad48b2011-06-01 13:45:51 +02001858 brcmu_bprintf(strbuf, "\n");
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001859
1860 dhd_dump_pct(strbuf, "Total: pkts/f2rw",
1861 (bus->dhd->tx_packets + bus->dhd->rx_packets),
1862 (bus->f2txdata + bus->f2rxhdrs + bus->f2rxdata));
1863 dhd_dump_pct(strbuf, ", pkts/f1sd",
1864 (bus->dhd->tx_packets + bus->dhd->rx_packets),
1865 bus->f1regdata);
1866 dhd_dump_pct(strbuf, ", pkts/sd",
1867 (bus->dhd->tx_packets + bus->dhd->rx_packets),
1868 (bus->f2txdata + bus->f2rxhdrs + bus->f2rxdata +
1869 bus->f1regdata));
1870 dhd_dump_pct(strbuf, ", pkts/int",
1871 (bus->dhd->tx_packets + bus->dhd->rx_packets),
1872 bus->intrcount);
Roland Vossen67ad48b2011-06-01 13:45:51 +02001873 brcmu_bprintf(strbuf, "\n\n");
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001874 }
1875
1876#ifdef SDTEST
1877 if (bus->pktgen_count) {
Roland Vossen67ad48b2011-06-01 13:45:51 +02001878 brcmu_bprintf(strbuf, "pktgen config and count:\n");
1879 brcmu_bprintf(strbuf,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001880 "freq %d count %d print %d total %d min %d len %d\n",
1881 bus->pktgen_freq, bus->pktgen_count,
1882 bus->pktgen_print, bus->pktgen_total,
1883 bus->pktgen_minlen, bus->pktgen_maxlen);
Roland Vossen67ad48b2011-06-01 13:45:51 +02001884 brcmu_bprintf(strbuf, "send attempts %d rcvd %d fail %d\n",
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001885 bus->pktgen_sent, bus->pktgen_rcvd,
1886 bus->pktgen_fail);
1887 }
1888#endif /* SDTEST */
1889#ifdef DHD_DEBUG
Roland Vossen67ad48b2011-06-01 13:45:51 +02001890 brcmu_bprintf(strbuf, "dpc_sched %d host interrupt%spending\n",
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001891 bus->dpc_sched,
1892 (bcmsdh_intr_pending(bus->sdh) ? " " : " not "));
Roland Vossen67ad48b2011-06-01 13:45:51 +02001893 brcmu_bprintf(strbuf, "blocksize %d roundup %d\n", bus->blocksize,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001894 bus->roundup);
1895#endif /* DHD_DEBUG */
Roland Vossen67ad48b2011-06-01 13:45:51 +02001896 brcmu_bprintf(strbuf,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001897 "clkstate %d activity %d idletime %d idlecount %d sleeping %d\n",
1898 bus->clkstate, bus->activity, bus->idletime, bus->idlecount,
1899 bus->sleeping);
1900}
1901
1902void dhd_bus_clearcounts(dhd_pub_t *dhdp)
1903{
1904 dhd_bus_t *bus = (dhd_bus_t *) dhdp->bus;
1905
1906 bus->intrcount = bus->lastintrs = bus->spurious = bus->regfails = 0;
1907 bus->rxrtx = bus->rx_toolong = bus->rxc_errors = 0;
1908 bus->rx_hdrfail = bus->rx_badhdr = bus->rx_badseq = 0;
1909 bus->tx_sderrs = bus->fc_rcvd = bus->fc_xoff = bus->fc_xon = 0;
1910 bus->rxglomfail = bus->rxglomframes = bus->rxglompkts = 0;
1911 bus->f2rxhdrs = bus->f2rxdata = bus->f2txdata = bus->f1regdata = 0;
1912}
1913
1914#ifdef SDTEST
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001915static int dhdsdio_pktgen_get(dhd_bus_t *bus, u8 *arg)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001916{
1917 dhd_pktgen_t pktgen;
1918
1919 pktgen.version = DHD_PKTGEN_VERSION;
1920 pktgen.freq = bus->pktgen_freq;
1921 pktgen.count = bus->pktgen_count;
1922 pktgen.print = bus->pktgen_print;
1923 pktgen.total = bus->pktgen_total;
1924 pktgen.minlen = bus->pktgen_minlen;
1925 pktgen.maxlen = bus->pktgen_maxlen;
1926 pktgen.numsent = bus->pktgen_sent;
1927 pktgen.numrcvd = bus->pktgen_rcvd;
1928 pktgen.numfail = bus->pktgen_fail;
1929 pktgen.mode = bus->pktgen_mode;
1930 pktgen.stop = bus->pktgen_stop;
1931
Stanislav Fomichev02160692011-02-15 01:05:10 +03001932 memcpy(arg, &pktgen, sizeof(pktgen));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001933
1934 return 0;
1935}
1936
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001937static int dhdsdio_pktgen_set(dhd_bus_t *bus, u8 *arg)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001938{
1939 dhd_pktgen_t pktgen;
1940 uint oldcnt, oldmode;
1941
Stanislav Fomichev02160692011-02-15 01:05:10 +03001942 memcpy(&pktgen, arg, sizeof(pktgen));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001943 if (pktgen.version != DHD_PKTGEN_VERSION)
Roland Vossene10d82d2011-05-03 11:35:19 +02001944 return -EINVAL;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001945
1946 oldcnt = bus->pktgen_count;
1947 oldmode = bus->pktgen_mode;
1948
1949 bus->pktgen_freq = pktgen.freq;
1950 bus->pktgen_count = pktgen.count;
1951 bus->pktgen_print = pktgen.print;
1952 bus->pktgen_total = pktgen.total;
1953 bus->pktgen_minlen = pktgen.minlen;
1954 bus->pktgen_maxlen = pktgen.maxlen;
1955 bus->pktgen_mode = pktgen.mode;
1956 bus->pktgen_stop = pktgen.stop;
1957
1958 bus->pktgen_tick = bus->pktgen_ptick = 0;
Greg Kroah-Hartman3ea2f4d2010-10-08 11:39:43 -07001959 bus->pktgen_len = max(bus->pktgen_len, bus->pktgen_minlen);
Greg Kroah-Hartman7068c2f2010-10-08 11:34:59 -07001960 bus->pktgen_len = min(bus->pktgen_len, bus->pktgen_maxlen);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001961
1962 /* Clear counts for a new pktgen (mode change, or was stopped) */
1963 if (bus->pktgen_count && (!oldcnt || oldmode != bus->pktgen_mode))
1964 bus->pktgen_sent = bus->pktgen_rcvd = bus->pktgen_fail = 0;
1965
1966 return 0;
1967}
1968#endif /* SDTEST */
1969
1970static int
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07001971dhdsdio_membytes(dhd_bus_t *bus, bool write, u32 address, u8 *data,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001972 uint size)
1973{
1974 int bcmerror = 0;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07001975 u32 sdaddr;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001976 uint dsize;
1977
1978 /* Determine initial transfer parameters */
1979 sdaddr = address & SBSDIO_SB_OFT_ADDR_MASK;
1980 if ((sdaddr + size) & SBSDIO_SBWINDOW_MASK)
1981 dsize = (SBSDIO_SB_OFT_ADDR_LIMIT - sdaddr);
1982 else
1983 dsize = size;
1984
1985 /* Set the backplane window to include the start address */
1986 bcmerror = dhdsdio_set_siaddr_window(bus, address);
1987 if (bcmerror) {
1988 DHD_ERROR(("%s: window change failed\n", __func__));
1989 goto xfer_done;
1990 }
1991
1992 /* Do the transfer(s) */
1993 while (size) {
1994 DHD_INFO(("%s: %s %d bytes at offset 0x%08x in window 0x%08x\n",
1995 __func__, (write ? "write" : "read"), dsize,
1996 sdaddr, (address & SBSDIO_SBWINDOW_MASK)));
1997 bcmerror =
1998 bcmsdh_rwdata(bus->sdh, write, sdaddr, data, dsize);
1999 if (bcmerror) {
2000 DHD_ERROR(("%s: membytes transfer failed\n", __func__));
2001 break;
2002 }
2003
2004 /* Adjust for next transfer (if any) */
2005 size -= dsize;
2006 if (size) {
2007 data += dsize;
2008 address += dsize;
2009 bcmerror = dhdsdio_set_siaddr_window(bus, address);
2010 if (bcmerror) {
2011 DHD_ERROR(("%s: window change failed\n",
2012 __func__));
2013 break;
2014 }
2015 sdaddr = 0;
Greg Kroah-Hartmanb61640d2010-10-08 12:37:39 -07002016 dsize = min_t(uint, SBSDIO_SB_OFT_ADDR_LIMIT, size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002017 }
2018 }
2019
2020xfer_done:
2021 /* Return the window to backplane enumeration space for core access */
2022 if (dhdsdio_set_siaddr_window(bus, bcmsdh_cur_sbwad(bus->sdh))) {
2023 DHD_ERROR(("%s: FAILED to set window back to 0x%x\n",
2024 __func__, bcmsdh_cur_sbwad(bus->sdh)));
2025 }
2026
2027 return bcmerror;
2028}
2029
2030#ifdef DHD_DEBUG
Franky Linb49b14d2011-06-01 13:45:37 +02002031static int dhdsdio_readshared(dhd_bus_t *bus, struct sdpcm_shared *sh)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002032{
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002033 u32 addr;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002034 int rv;
2035
2036 /* Read last word in memory to determine address of
2037 sdpcm_shared structure */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002038 rv = dhdsdio_membytes(bus, false, bus->ramsize - 4, (u8 *)&addr, 4);
Jason Cooper9b890322010-09-30 15:15:39 -04002039 if (rv < 0)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002040 return rv;
2041
Stanislav Fomichev628f10b2011-02-20 21:43:32 +03002042 addr = le32_to_cpu(addr);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002043
2044 DHD_INFO(("sdpcm_shared address 0x%08X\n", addr));
2045
2046 /*
2047 * Check if addr is valid.
2048 * NVRAM length at the end of memory should have been overwritten.
2049 */
2050 if (addr == 0 || ((~addr >> 16) & 0xffff) == (addr & 0xffff)) {
2051 DHD_ERROR(("%s: address (0x%08x) of sdpcm_shared invalid\n",
2052 __func__, addr));
Roland Vossenb74ac122011-05-03 11:35:20 +02002053 return -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002054 }
2055
Roland Vossen70963f92011-06-01 13:45:08 +02002056 /* Read rte_shared structure */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002057 rv = dhdsdio_membytes(bus, false, addr, (u8 *) sh,
Franky Linb49b14d2011-06-01 13:45:37 +02002058 sizeof(struct sdpcm_shared));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002059 if (rv < 0)
2060 return rv;
2061
2062 /* Endianness */
Stanislav Fomichev628f10b2011-02-20 21:43:32 +03002063 sh->flags = le32_to_cpu(sh->flags);
2064 sh->trap_addr = le32_to_cpu(sh->trap_addr);
2065 sh->assert_exp_addr = le32_to_cpu(sh->assert_exp_addr);
2066 sh->assert_file_addr = le32_to_cpu(sh->assert_file_addr);
2067 sh->assert_line = le32_to_cpu(sh->assert_line);
2068 sh->console_addr = le32_to_cpu(sh->console_addr);
2069 sh->msgtrace_addr = le32_to_cpu(sh->msgtrace_addr);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002070
2071 if ((sh->flags & SDPCM_SHARED_VERSION_MASK) != SDPCM_SHARED_VERSION) {
2072 DHD_ERROR(("%s: sdpcm_shared version %d in dhd "
2073 "is different than sdpcm_shared version %d in dongle\n",
2074 __func__, SDPCM_SHARED_VERSION,
2075 sh->flags & SDPCM_SHARED_VERSION_MASK));
Roland Vossenb74ac122011-05-03 11:35:20 +02002076 return -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002077 }
2078
Roland Vossena1c5ad82011-04-11 15:16:24 +02002079 return 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002080}
2081
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002082static int dhdsdio_checkdied(dhd_bus_t *bus, u8 *data, uint size)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002083{
2084 int bcmerror = 0;
2085 uint msize = 512;
2086 char *mbuffer = NULL;
2087 uint maxstrlen = 256;
2088 char *str = NULL;
2089 trap_t tr;
Franky Linb49b14d2011-06-01 13:45:37 +02002090 struct sdpcm_shared sdpcm_shared;
Roland Vossen67ad48b2011-06-01 13:45:51 +02002091 struct brcmu_strbuf strbuf;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002092
2093 DHD_TRACE(("%s: Enter\n", __func__));
2094
2095 if (data == NULL) {
2096 /*
2097 * Called after a rx ctrl timeout. "data" is NULL.
2098 * allocate memory to trace the trap or assert.
2099 */
2100 size = msize;
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02002101 mbuffer = data = kmalloc(msize, GFP_ATOMIC);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002102 if (mbuffer == NULL) {
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02002103 DHD_ERROR(("%s: kmalloc(%d) failed\n", __func__,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002104 msize));
Roland Vossene10d82d2011-05-03 11:35:19 +02002105 bcmerror = -ENOMEM;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002106 goto done;
2107 }
2108 }
2109
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02002110 str = kmalloc(maxstrlen, GFP_ATOMIC);
Jason Cooper9b890322010-09-30 15:15:39 -04002111 if (str == NULL) {
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02002112 DHD_ERROR(("%s: kmalloc(%d) failed\n", __func__, maxstrlen));
Roland Vossene10d82d2011-05-03 11:35:19 +02002113 bcmerror = -ENOMEM;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002114 goto done;
2115 }
2116
Jason Cooper9b890322010-09-30 15:15:39 -04002117 bcmerror = dhdsdio_readshared(bus, &sdpcm_shared);
2118 if (bcmerror < 0)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002119 goto done;
2120
Roland Vossen67ad48b2011-06-01 13:45:51 +02002121 brcmu_binit(&strbuf, data, size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002122
Roland Vossen67ad48b2011-06-01 13:45:51 +02002123 brcmu_bprintf(&strbuf,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002124 "msgtrace address : 0x%08X\nconsole address : 0x%08X\n",
2125 sdpcm_shared.msgtrace_addr, sdpcm_shared.console_addr);
2126
2127 if ((sdpcm_shared.flags & SDPCM_SHARED_ASSERT_BUILT) == 0) {
2128 /* NOTE: Misspelled assert is intentional - DO NOT FIX.
2129 * (Avoids conflict with real asserts for programmatic
2130 * parsing of output.)
2131 */
Roland Vossen67ad48b2011-06-01 13:45:51 +02002132 brcmu_bprintf(&strbuf, "Assrt not built in dongle\n");
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002133 }
2134
2135 if ((sdpcm_shared.flags & (SDPCM_SHARED_ASSERT | SDPCM_SHARED_TRAP)) ==
2136 0) {
2137 /* NOTE: Misspelled assert is intentional - DO NOT FIX.
2138 * (Avoids conflict with real asserts for programmatic
2139 * parsing of output.)
2140 */
Roland Vossen67ad48b2011-06-01 13:45:51 +02002141 brcmu_bprintf(&strbuf, "No trap%s in dongle",
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002142 (sdpcm_shared.flags & SDPCM_SHARED_ASSERT_BUILT)
2143 ? "/assrt" : "");
2144 } else {
2145 if (sdpcm_shared.flags & SDPCM_SHARED_ASSERT) {
2146 /* Download assert */
Roland Vossen67ad48b2011-06-01 13:45:51 +02002147 brcmu_bprintf(&strbuf, "Dongle assert");
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002148 if (sdpcm_shared.assert_exp_addr != 0) {
2149 str[0] = '\0';
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002150 bcmerror = dhdsdio_membytes(bus, false,
Jason Cooper9b890322010-09-30 15:15:39 -04002151 sdpcm_shared.assert_exp_addr,
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002152 (u8 *) str, maxstrlen);
Jason Cooper9b890322010-09-30 15:15:39 -04002153 if (bcmerror < 0)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002154 goto done;
2155
2156 str[maxstrlen - 1] = '\0';
Roland Vossen67ad48b2011-06-01 13:45:51 +02002157 brcmu_bprintf(&strbuf, " expr \"%s\"", str);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002158 }
2159
2160 if (sdpcm_shared.assert_file_addr != 0) {
2161 str[0] = '\0';
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002162 bcmerror = dhdsdio_membytes(bus, false,
Jason Cooper9b890322010-09-30 15:15:39 -04002163 sdpcm_shared.assert_file_addr,
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002164 (u8 *) str, maxstrlen);
Jason Cooper9b890322010-09-30 15:15:39 -04002165 if (bcmerror < 0)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002166 goto done;
2167
2168 str[maxstrlen - 1] = '\0';
Roland Vossen67ad48b2011-06-01 13:45:51 +02002169 brcmu_bprintf(&strbuf, " file \"%s\"", str);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002170 }
2171
Roland Vossen67ad48b2011-06-01 13:45:51 +02002172 brcmu_bprintf(&strbuf, " line %d ",
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002173 sdpcm_shared.assert_line);
2174 }
2175
2176 if (sdpcm_shared.flags & SDPCM_SHARED_TRAP) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002177 bcmerror = dhdsdio_membytes(bus, false,
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002178 sdpcm_shared.trap_addr, (u8 *)&tr,
Jason Cooper9b890322010-09-30 15:15:39 -04002179 sizeof(trap_t));
2180 if (bcmerror < 0)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002181 goto done;
2182
Roland Vossen67ad48b2011-06-01 13:45:51 +02002183 brcmu_bprintf(&strbuf,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002184 "Dongle trap type 0x%x @ epc 0x%x, cpsr 0x%x, spsr 0x%x, sp 0x%x,"
2185 "lp 0x%x, rpc 0x%x Trap offset 0x%x, "
2186 "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",
2187 tr.type, tr.epc, tr.cpsr, tr.spsr, tr.r13,
2188 tr.r14, tr.pc, sdpcm_shared.trap_addr,
2189 tr.r0, tr.r1, tr.r2, tr.r3, tr.r4, tr.r5,
2190 tr.r6, tr.r7);
2191 }
2192 }
2193
2194 if (sdpcm_shared.flags & (SDPCM_SHARED_ASSERT | SDPCM_SHARED_TRAP))
2195 DHD_ERROR(("%s: %s\n", __func__, strbuf.origbuf));
2196
2197#ifdef DHD_DEBUG
2198 if (sdpcm_shared.flags & SDPCM_SHARED_TRAP) {
2199 /* Mem dump to a file on device */
2200 dhdsdio_mem_dump(bus);
2201 }
2202#endif /* DHD_DEBUG */
2203
2204done:
Ilia Mirkin46d994b2011-03-13 00:28:56 -05002205 kfree(mbuffer);
2206 kfree(str);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002207
2208 return bcmerror;
2209}
2210
2211static int dhdsdio_mem_dump(dhd_bus_t *bus)
2212{
2213 int ret = 0;
2214 int size; /* Full mem size */
2215 int start = 0; /* Start address */
2216 int read_size = 0; /* Read size of each iteration */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002217 u8 *buf = NULL, *databuf = NULL;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002218
2219 /* Get full mem size */
2220 size = bus->ramsize;
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02002221 buf = kmalloc(size, GFP_ATOMIC);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002222 if (!buf) {
Arend van Spriel0bef7742011-02-10 12:03:44 +01002223 DHD_ERROR(("%s: Out of memory (%d bytes)\n", __func__, size));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002224 return -1;
2225 }
2226
2227 /* Read mem content */
Arend van Spriel0bef7742011-02-10 12:03:44 +01002228 printk(KERN_DEBUG "Dump dongle memory");
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002229 databuf = buf;
2230 while (size) {
Greg Kroah-Hartman7068c2f2010-10-08 11:34:59 -07002231 read_size = min(MEMBLOCK, size);
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002232 ret = dhdsdio_membytes(bus, false, start, databuf, read_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002233 if (ret) {
Arend van Spriel0bef7742011-02-10 12:03:44 +01002234 DHD_ERROR(("%s: Error membytes %d\n", __func__, ret));
Ilia Mirkin46d994b2011-03-13 00:28:56 -05002235 kfree(buf);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002236 return -1;
2237 }
Arend van Spriel0bef7742011-02-10 12:03:44 +01002238 printk(".");
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002239
2240 /* Decrement size and increment start address */
2241 size -= read_size;
2242 start += read_size;
2243 databuf += read_size;
2244 }
Arend van Spriel0bef7742011-02-10 12:03:44 +01002245 printk(KERN_DEBUG "Done\n");
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002246
2247 /* free buf before return !!! */
2248 if (write_to_file(bus->dhd, buf, bus->ramsize)) {
Arend van Spriel0bef7742011-02-10 12:03:44 +01002249 DHD_ERROR(("%s: Error writing to files\n", __func__));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002250 return -1;
2251 }
2252
2253 /* buf free handled in write_to_file, not here */
2254 return 0;
2255}
2256
2257#define CONSOLE_LINE_MAX 192
2258
2259static int dhdsdio_readconsole(dhd_bus_t *bus)
2260{
2261 dhd_console_t *c = &bus->console;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002262 u8 line[CONSOLE_LINE_MAX], ch;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002263 u32 n, idx, addr;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002264 int rv;
2265
2266 /* Don't do anything until FWREADY updates console address */
2267 if (bus->console_addr == 0)
2268 return 0;
2269
2270 /* Read console log struct */
Roland Vossen70963f92011-06-01 13:45:08 +02002271 addr = bus->console_addr + offsetof(rte_cons_t, log);
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002272 rv = dhdsdio_membytes(bus, false, addr, (u8 *)&c->log,
Jason Cooper9b890322010-09-30 15:15:39 -04002273 sizeof(c->log));
2274 if (rv < 0)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002275 return rv;
2276
2277 /* Allocate console buffer (one time only) */
2278 if (c->buf == NULL) {
Stanislav Fomichev628f10b2011-02-20 21:43:32 +03002279 c->bufsize = le32_to_cpu(c->log.buf_size);
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02002280 c->buf = kmalloc(c->bufsize, GFP_ATOMIC);
Jason Cooper9b890322010-09-30 15:15:39 -04002281 if (c->buf == NULL)
Roland Vossene10d82d2011-05-03 11:35:19 +02002282 return -ENOMEM;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002283 }
2284
Stanislav Fomichev628f10b2011-02-20 21:43:32 +03002285 idx = le32_to_cpu(c->log.idx);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002286
2287 /* Protect against corrupt value */
2288 if (idx > c->bufsize)
Roland Vossenb74ac122011-05-03 11:35:20 +02002289 return -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002290
2291 /* Skip reading the console buffer if the index pointer
2292 has not moved */
2293 if (idx == c->last)
Roland Vossena1c5ad82011-04-11 15:16:24 +02002294 return 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002295
2296 /* Read the console buffer */
Stanislav Fomichev628f10b2011-02-20 21:43:32 +03002297 addr = le32_to_cpu(c->log.buf);
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002298 rv = dhdsdio_membytes(bus, false, addr, c->buf, c->bufsize);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002299 if (rv < 0)
2300 return rv;
2301
2302 while (c->last != idx) {
2303 for (n = 0; n < CONSOLE_LINE_MAX - 2; n++) {
2304 if (c->last == idx) {
2305 /* This would output a partial line.
2306 * Instead, back up
2307 * the buffer pointer and output this
2308 * line next time around.
2309 */
2310 if (c->last >= n)
2311 c->last -= n;
2312 else
2313 c->last = c->bufsize - n;
2314 goto break2;
2315 }
2316 ch = c->buf[c->last];
2317 c->last = (c->last + 1) % c->bufsize;
2318 if (ch == '\n')
2319 break;
2320 line[n] = ch;
2321 }
2322
2323 if (n > 0) {
2324 if (line[n - 1] == '\r')
2325 n--;
2326 line[n] = 0;
Arend van Spriel0bef7742011-02-10 12:03:44 +01002327 printk(KERN_DEBUG "CONSOLE: %s\n", line);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002328 }
2329 }
2330break2:
2331
Roland Vossena1c5ad82011-04-11 15:16:24 +02002332 return 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002333}
2334#endif /* DHD_DEBUG */
2335
2336int dhdsdio_downloadvars(dhd_bus_t *bus, void *arg, int len)
2337{
Roland Vossena1c5ad82011-04-11 15:16:24 +02002338 int bcmerror = 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002339
2340 DHD_TRACE(("%s: Enter\n", __func__));
2341
2342 /* Basic sanity checks */
2343 if (bus->dhd->up) {
Roland Vossenb74ac122011-05-03 11:35:20 +02002344 bcmerror = -EISCONN;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002345 goto err;
2346 }
2347 if (!len) {
Roland Vossene10d82d2011-05-03 11:35:19 +02002348 bcmerror = -EOVERFLOW;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002349 goto err;
2350 }
2351
2352 /* Free the old ones and replace with passed variables */
Ilia Mirkin46d994b2011-03-13 00:28:56 -05002353 kfree(bus->vars);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002354
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02002355 bus->vars = kmalloc(len, GFP_ATOMIC);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002356 bus->varsz = bus->vars ? len : 0;
2357 if (bus->vars == NULL) {
Roland Vossene10d82d2011-05-03 11:35:19 +02002358 bcmerror = -ENOMEM;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002359 goto err;
2360 }
2361
2362 /* Copy the passed variables, which should include the
2363 terminating double-null */
Stanislav Fomichev02160692011-02-15 01:05:10 +03002364 memcpy(bus->vars, arg, bus->varsz);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002365err:
2366 return bcmerror;
2367}
2368
2369static int
Roland Vossen67ad48b2011-06-01 13:45:51 +02002370dhdsdio_doiovar(dhd_bus_t *bus, const struct brcmu_iovar *vi, u32 actionid,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002371 const char *name, void *params, int plen, void *arg, int len,
2372 int val_size)
2373{
2374 int bcmerror = 0;
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002375 s32 int_val = 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002376 bool bool_val = 0;
2377
2378 DHD_TRACE(("%s: Enter, action %d name %s params %p plen %d arg %p "
2379 "len %d val_size %d\n",
2380 __func__, actionid, name, params, plen, arg, len, val_size));
2381
Roland Vossen67ad48b2011-06-01 13:45:51 +02002382 bcmerror = brcmu_iovar_lencheck(vi, arg, len, IOV_ISSET(actionid));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002383 if (bcmerror != 0)
2384 goto exit;
2385
2386 if (plen >= (int)sizeof(int_val))
Stanislav Fomichev02160692011-02-15 01:05:10 +03002387 memcpy(&int_val, params, sizeof(int_val));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002388
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002389 bool_val = (int_val != 0) ? true : false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002390
2391 /* Some ioctls use the bus */
2392 dhd_os_sdlock(bus->dhd);
2393
2394 /* Check if dongle is in reset. If so, only allow DEVRESET iovars */
2395 if (bus->dhd->dongle_reset && !(actionid == IOV_SVAL(IOV_DEVRESET) ||
2396 actionid == IOV_GVAL(IOV_DEVRESET))) {
Roland Vossenb74ac122011-05-03 11:35:20 +02002397 bcmerror = -EPERM;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002398 goto exit;
2399 }
2400
2401 /* Handle sleep stuff before any clock mucking */
2402 if (vi->varid == IOV_SLEEP) {
2403 if (IOV_ISSET(actionid)) {
2404 bcmerror = dhdsdio_bussleep(bus, bool_val);
2405 } else {
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002406 int_val = (s32) bus->sleeping;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002407 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002408 }
2409 goto exit;
2410 }
2411
2412 /* Request clock to allow SDIO accesses */
2413 if (!bus->dhd->dongle_reset) {
2414 BUS_WAKE(bus);
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002415 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002416 }
2417
2418 switch (actionid) {
2419 case IOV_GVAL(IOV_INTR):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002420 int_val = (s32) bus->intr;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002421 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002422 break;
2423
2424 case IOV_SVAL(IOV_INTR):
2425 bus->intr = bool_val;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002426 bus->intdis = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002427 if (bus->dhd->up) {
2428 if (bus->intr) {
2429 DHD_INTR(("%s: enable SDIO device interrupts\n",
2430 __func__));
2431 bcmsdh_intr_enable(bus->sdh);
2432 } else {
2433 DHD_INTR(("%s: disable SDIO interrupts\n",
2434 __func__));
2435 bcmsdh_intr_disable(bus->sdh);
2436 }
2437 }
2438 break;
2439
2440 case IOV_GVAL(IOV_POLLRATE):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002441 int_val = (s32) bus->pollrate;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002442 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002443 break;
2444
2445 case IOV_SVAL(IOV_POLLRATE):
2446 bus->pollrate = (uint) int_val;
2447 bus->poll = (bus->pollrate != 0);
2448 break;
2449
2450 case IOV_GVAL(IOV_IDLETIME):
2451 int_val = bus->idletime;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002452 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002453 break;
2454
2455 case IOV_SVAL(IOV_IDLETIME):
2456 if ((int_val < 0) && (int_val != DHD_IDLE_IMMEDIATE))
Roland Vossene10d82d2011-05-03 11:35:19 +02002457 bcmerror = -EINVAL;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002458 else
2459 bus->idletime = int_val;
2460 break;
2461
2462 case IOV_GVAL(IOV_IDLECLOCK):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002463 int_val = (s32) bus->idleclock;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002464 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002465 break;
2466
2467 case IOV_SVAL(IOV_IDLECLOCK):
2468 bus->idleclock = int_val;
2469 break;
2470
2471 case IOV_GVAL(IOV_SD1IDLE):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002472 int_val = (s32) sd1idle;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002473 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002474 break;
2475
2476 case IOV_SVAL(IOV_SD1IDLE):
2477 sd1idle = bool_val;
2478 break;
2479
2480 case IOV_SVAL(IOV_MEMBYTES):
2481 case IOV_GVAL(IOV_MEMBYTES):
2482 {
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002483 u32 address;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002484 uint size, dsize;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002485 u8 *data;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002486
2487 bool set = (actionid == IOV_SVAL(IOV_MEMBYTES));
2488
2489 ASSERT(plen >= 2 * sizeof(int));
2490
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002491 address = (u32) int_val;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002492 memcpy(&int_val, (char *)params + sizeof(int_val),
2493 sizeof(int_val));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002494 size = (uint) int_val;
2495
2496 /* Do some validation */
2497 dsize = set ? plen - (2 * sizeof(int)) : len;
2498 if (dsize < size) {
2499 DHD_ERROR(("%s: error on %s membytes, addr "
2500 "0x%08x size %d dsize %d\n",
2501 __func__, (set ? "set" : "get"),
2502 address, size, dsize));
Roland Vossene10d82d2011-05-03 11:35:19 +02002503 bcmerror = -EINVAL;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002504 break;
2505 }
2506
2507 DHD_INFO(("%s: Request to %s %d bytes at address "
2508 "0x%08x\n",
2509 __func__, (set ? "write" : "read"), size, address));
2510
2511 /* If we know about SOCRAM, check for a fit */
2512 if ((bus->orig_ramsize) &&
2513 ((address > bus->orig_ramsize)
2514 || (address + size > bus->orig_ramsize))) {
2515 DHD_ERROR(("%s: ramsize 0x%08x doesn't have %d "
2516 "bytes at 0x%08x\n",
2517 __func__, bus->orig_ramsize, size, address));
Roland Vossene10d82d2011-05-03 11:35:19 +02002518 bcmerror = -EINVAL;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002519 break;
2520 }
2521
2522 /* Generate the actual data pointer */
2523 data =
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002524 set ? (u8 *) params +
2525 2 * sizeof(int) : (u8 *) arg;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002526
2527 /* Call to do the transfer */
2528 bcmerror =
2529 dhdsdio_membytes(bus, set, address, data, size);
2530
2531 break;
2532 }
2533
2534 case IOV_GVAL(IOV_MEMSIZE):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002535 int_val = (s32) bus->ramsize;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002536 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002537 break;
2538
2539 case IOV_GVAL(IOV_SDIOD_DRIVE):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002540 int_val = (s32) dhd_sdiod_drive_strength;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002541 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002542 break;
2543
2544 case IOV_SVAL(IOV_SDIOD_DRIVE):
2545 dhd_sdiod_drive_strength = int_val;
Franky Lin5d0d7a92011-04-25 19:34:05 -07002546 dhdsdio_sdiod_drive_strength_init(bus,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002547 dhd_sdiod_drive_strength);
2548 break;
2549
2550 case IOV_SVAL(IOV_DOWNLOAD):
2551 bcmerror = dhdsdio_download_state(bus, bool_val);
2552 break;
2553
2554 case IOV_SVAL(IOV_VARS):
2555 bcmerror = dhdsdio_downloadvars(bus, arg, len);
2556 break;
2557
2558 case IOV_GVAL(IOV_READAHEAD):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002559 int_val = (s32) dhd_readahead;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002560 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002561 break;
2562
2563 case IOV_SVAL(IOV_READAHEAD):
2564 if (bool_val && !dhd_readahead)
2565 bus->nextlen = 0;
2566 dhd_readahead = bool_val;
2567 break;
2568
2569 case IOV_GVAL(IOV_SDRXCHAIN):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002570 int_val = (s32) bus->use_rxchain;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002571 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002572 break;
2573
2574 case IOV_SVAL(IOV_SDRXCHAIN):
2575 if (bool_val && !bus->sd_rxchain)
Roland Vossene10d82d2011-05-03 11:35:19 +02002576 bcmerror = -ENOTSUPP;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002577 else
2578 bus->use_rxchain = bool_val;
2579 break;
2580 case IOV_GVAL(IOV_ALIGNCTL):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002581 int_val = (s32) dhd_alignctl;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002582 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002583 break;
2584
2585 case IOV_SVAL(IOV_ALIGNCTL):
2586 dhd_alignctl = bool_val;
2587 break;
2588
2589 case IOV_GVAL(IOV_SDALIGN):
2590 int_val = DHD_SDALIGN;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002591 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002592 break;
2593
2594#ifdef DHD_DEBUG
2595 case IOV_GVAL(IOV_VARS):
2596 if (bus->varsz < (uint) len)
Stanislav Fomichev02160692011-02-15 01:05:10 +03002597 memcpy(arg, bus->vars, bus->varsz);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002598 else
Roland Vossene10d82d2011-05-03 11:35:19 +02002599 bcmerror = -EOVERFLOW;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002600 break;
2601#endif /* DHD_DEBUG */
2602
2603#ifdef DHD_DEBUG
2604 case IOV_GVAL(IOV_SDREG):
2605 {
2606 sdreg_t *sd_ptr;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002607 u32 addr, size;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002608
2609 sd_ptr = (sdreg_t *) params;
2610
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07002611 addr = (unsigned long)bus->regs + sd_ptr->offset;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002612 size = sd_ptr->func;
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002613 int_val = (s32) bcmsdh_reg_read(bus->sdh, addr, size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002614 if (bcmsdh_regfail(bus->sdh))
Roland Vossenb74ac122011-05-03 11:35:20 +02002615 bcmerror = -EIO;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002616 memcpy(arg, &int_val, sizeof(s32));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002617 break;
2618 }
2619
2620 case IOV_SVAL(IOV_SDREG):
2621 {
2622 sdreg_t *sd_ptr;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002623 u32 addr, size;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002624
2625 sd_ptr = (sdreg_t *) params;
2626
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07002627 addr = (unsigned long)bus->regs + sd_ptr->offset;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002628 size = sd_ptr->func;
2629 bcmsdh_reg_write(bus->sdh, addr, size, sd_ptr->value);
2630 if (bcmsdh_regfail(bus->sdh))
Roland Vossenb74ac122011-05-03 11:35:20 +02002631 bcmerror = -EIO;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002632 break;
2633 }
2634
2635 /* Same as above, but offset is not backplane
2636 (not SDIO core) */
2637 case IOV_GVAL(IOV_SBREG):
2638 {
2639 sdreg_t sdreg;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002640 u32 addr, size;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002641
Stanislav Fomichev02160692011-02-15 01:05:10 +03002642 memcpy(&sdreg, params, sizeof(sdreg));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002643
2644 addr = SI_ENUM_BASE + sdreg.offset;
2645 size = sdreg.func;
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002646 int_val = (s32) bcmsdh_reg_read(bus->sdh, addr, size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002647 if (bcmsdh_regfail(bus->sdh))
Roland Vossenb74ac122011-05-03 11:35:20 +02002648 bcmerror = -EIO;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002649 memcpy(arg, &int_val, sizeof(s32));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002650 break;
2651 }
2652
2653 case IOV_SVAL(IOV_SBREG):
2654 {
2655 sdreg_t sdreg;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002656 u32 addr, size;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002657
Stanislav Fomichev02160692011-02-15 01:05:10 +03002658 memcpy(&sdreg, params, sizeof(sdreg));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002659
2660 addr = SI_ENUM_BASE + sdreg.offset;
2661 size = sdreg.func;
2662 bcmsdh_reg_write(bus->sdh, addr, size, sdreg.value);
2663 if (bcmsdh_regfail(bus->sdh))
Roland Vossenb74ac122011-05-03 11:35:20 +02002664 bcmerror = -EIO;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002665 break;
2666 }
2667
2668 case IOV_GVAL(IOV_SDCIS):
2669 {
2670 *(char *)arg = 0;
2671
nohee koea3b8a22010-10-09 10:34:38 -07002672 strcat(arg, "\nFunc 0\n");
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002673 bcmsdh_cis_read(bus->sdh, 0x10,
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002674 (u8 *) arg + strlen(arg),
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002675 SBSDIO_CIS_SIZE_LIMIT);
nohee koea3b8a22010-10-09 10:34:38 -07002676 strcat(arg, "\nFunc 1\n");
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002677 bcmsdh_cis_read(bus->sdh, 0x11,
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002678 (u8 *) arg + strlen(arg),
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002679 SBSDIO_CIS_SIZE_LIMIT);
nohee koea3b8a22010-10-09 10:34:38 -07002680 strcat(arg, "\nFunc 2\n");
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002681 bcmsdh_cis_read(bus->sdh, 0x12,
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002682 (u8 *) arg + strlen(arg),
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002683 SBSDIO_CIS_SIZE_LIMIT);
2684 break;
2685 }
2686
2687 case IOV_GVAL(IOV_FORCEEVEN):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002688 int_val = (s32) forcealign;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002689 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002690 break;
2691
2692 case IOV_SVAL(IOV_FORCEEVEN):
2693 forcealign = bool_val;
2694 break;
2695
2696 case IOV_GVAL(IOV_TXBOUND):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002697 int_val = (s32) dhd_txbound;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002698 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002699 break;
2700
2701 case IOV_SVAL(IOV_TXBOUND):
2702 dhd_txbound = (uint) int_val;
2703 break;
2704
2705 case IOV_GVAL(IOV_RXBOUND):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002706 int_val = (s32) dhd_rxbound;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002707 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002708 break;
2709
2710 case IOV_SVAL(IOV_RXBOUND):
2711 dhd_rxbound = (uint) int_val;
2712 break;
2713
2714 case IOV_GVAL(IOV_TXMINMAX):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002715 int_val = (s32) dhd_txminmax;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002716 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002717 break;
2718
2719 case IOV_SVAL(IOV_TXMINMAX):
2720 dhd_txminmax = (uint) int_val;
2721 break;
2722#endif /* DHD_DEBUG */
2723
2724#ifdef SDTEST
2725 case IOV_GVAL(IOV_EXTLOOP):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002726 int_val = (s32) bus->ext_loop;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002727 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002728 break;
2729
2730 case IOV_SVAL(IOV_EXTLOOP):
2731 bus->ext_loop = bool_val;
2732 break;
2733
2734 case IOV_GVAL(IOV_PKTGEN):
2735 bcmerror = dhdsdio_pktgen_get(bus, arg);
2736 break;
2737
2738 case IOV_SVAL(IOV_PKTGEN):
2739 bcmerror = dhdsdio_pktgen_set(bus, arg);
2740 break;
2741#endif /* SDTEST */
2742
2743 case IOV_SVAL(IOV_DEVRESET):
2744 DHD_TRACE(("%s: Called set IOV_DEVRESET=%d dongle_reset=%d "
2745 "busstate=%d\n",
2746 __func__, bool_val, bus->dhd->dongle_reset,
2747 bus->dhd->busstate));
2748
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002749 dhd_bus_devreset(bus->dhd, (u8) bool_val);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002750
2751 break;
2752
2753 case IOV_GVAL(IOV_DEVRESET):
2754 DHD_TRACE(("%s: Called get IOV_DEVRESET\n", __func__));
2755
2756 /* Get its status */
2757 int_val = (bool) bus->dhd->dongle_reset;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002758 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002759
2760 break;
2761
2762 default:
Roland Vossene10d82d2011-05-03 11:35:19 +02002763 bcmerror = -ENOTSUPP;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002764 break;
2765 }
2766
2767exit:
2768 if ((bus->idletime == DHD_IDLE_IMMEDIATE) && !bus->dpc_sched) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002769 bus->activity = false;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07002770 dhdsdio_clkctl(bus, CLK_NONE, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002771 }
2772
2773 dhd_os_sdunlock(bus->dhd);
2774
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002775 if (actionid == IOV_SVAL(IOV_DEVRESET) && bool_val == false)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002776 dhd_preinit_ioctls((dhd_pub_t *) bus->dhd);
2777
2778 return bcmerror;
2779}
2780
2781static int dhdsdio_write_vars(dhd_bus_t *bus)
2782{
2783 int bcmerror = 0;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002784 u32 varsize;
2785 u32 varaddr;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002786 u8 *vbuffer;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002787 u32 varsizew;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002788#ifdef DHD_DEBUG
2789 char *nvram_ularray;
2790#endif /* DHD_DEBUG */
2791
2792 /* Even if there are no vars are to be written, we still
2793 need to set the ramsize. */
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07002794 varsize = bus->varsz ? roundup(bus->varsz, 4) : 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002795 varaddr = (bus->ramsize - 4) - varsize;
2796
2797 if (bus->vars) {
Alexander Beregalov12d0eb42011-03-09 03:53:35 +03002798 vbuffer = kzalloc(varsize, GFP_ATOMIC);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002799 if (!vbuffer)
Roland Vossene10d82d2011-05-03 11:35:19 +02002800 return -ENOMEM;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002801
Stanislav Fomichev02160692011-02-15 01:05:10 +03002802 memcpy(vbuffer, bus->vars, bus->varsz);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002803
2804 /* Write the vars list */
2805 bcmerror =
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07002806 dhdsdio_membytes(bus, true, varaddr, vbuffer, varsize);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002807#ifdef DHD_DEBUG
2808 /* Verify NVRAM bytes */
2809 DHD_INFO(("Compare NVRAM dl & ul; varsize=%d\n", varsize));
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02002810 nvram_ularray = kmalloc(varsize, GFP_ATOMIC);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002811 if (!nvram_ularray)
Roland Vossene10d82d2011-05-03 11:35:19 +02002812 return -ENOMEM;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002813
2814 /* Upload image to verify downloaded contents. */
2815 memset(nvram_ularray, 0xaa, varsize);
2816
2817 /* Read the vars list to temp buffer for comparison */
2818 bcmerror =
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002819 dhdsdio_membytes(bus, false, varaddr, nvram_ularray,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002820 varsize);
2821 if (bcmerror) {
2822 DHD_ERROR(("%s: error %d on reading %d nvram bytes at "
2823 "0x%08x\n", __func__, bcmerror, varsize, varaddr));
2824 }
2825 /* Compare the org NVRAM with the one read from RAM */
2826 if (memcmp(vbuffer, nvram_ularray, varsize)) {
2827 DHD_ERROR(("%s: Downloaded NVRAM image is corrupted.\n",
2828 __func__));
2829 } else
2830 DHD_ERROR(("%s: Download/Upload/Compare of NVRAM ok.\n",
2831 __func__));
2832
mike.rapoport@gmail.com182acb32010-10-13 00:09:12 +02002833 kfree(nvram_ularray);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002834#endif /* DHD_DEBUG */
2835
mike.rapoport@gmail.com182acb32010-10-13 00:09:12 +02002836 kfree(vbuffer);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002837 }
2838
2839 /* adjust to the user specified RAM */
2840 DHD_INFO(("Physical memory size: %d, usable memory size: %d\n",
2841 bus->orig_ramsize, bus->ramsize));
2842 DHD_INFO(("Vars are at %d, orig varsize is %d\n", varaddr, varsize));
2843 varsize = ((bus->orig_ramsize - 4) - varaddr);
2844
2845 /*
2846 * Determine the length token:
2847 * Varsize, converted to words, in lower 16-bits, checksum
2848 * in upper 16-bits.
2849 */
2850 if (bcmerror) {
2851 varsizew = 0;
2852 } else {
2853 varsizew = varsize / 4;
2854 varsizew = (~varsizew << 16) | (varsizew & 0x0000FFFF);
Stanislav Fomichev628f10b2011-02-20 21:43:32 +03002855 varsizew = cpu_to_le32(varsizew);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002856 }
2857
2858 DHD_INFO(("New varsize is %d, length token=0x%08x\n", varsize,
2859 varsizew));
2860
2861 /* Write the length token to the last word */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07002862 bcmerror = dhdsdio_membytes(bus, true, (bus->orig_ramsize - 4),
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002863 (u8 *)&varsizew, 4);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002864
2865 return bcmerror;
2866}
2867
2868static int dhdsdio_download_state(dhd_bus_t *bus, bool enter)
2869{
2870 uint retries;
Franky Lineb5dc512011-04-25 19:34:04 -07002871 u32 regdata;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002872 int bcmerror = 0;
2873
2874 /* To enter download state, disable ARM and reset SOCRAM.
2875 * To exit download state, simply reset ARM (default is RAM boot).
2876 */
2877 if (enter) {
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07002878 bus->alp_only = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002879
Franky Lineb5dc512011-04-25 19:34:04 -07002880 dhdsdio_chip_disablecore(bus->sdh, bus->ci->armcorebase);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002881
Franky Lineb5dc512011-04-25 19:34:04 -07002882 dhdsdio_chip_resetcore(bus->sdh, bus->ci->ramcorebase);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002883
2884 /* Clear the top bit of memory */
2885 if (bus->ramsize) {
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002886 u32 zeros = 0;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07002887 dhdsdio_membytes(bus, true, bus->ramsize - 4,
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002888 (u8 *)&zeros, 4);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002889 }
2890 } else {
Franky Lineb5dc512011-04-25 19:34:04 -07002891 regdata = bcmsdh_reg_read(bus->sdh,
2892 CORE_SB(bus->ci->ramcorebase, sbtmstatelow), 4);
2893 regdata &= (SBTML_RESET | SBTML_REJ_MASK |
2894 (SICF_CLOCK_EN << SBTML_SICF_SHIFT));
2895 if ((SICF_CLOCK_EN << SBTML_SICF_SHIFT) != regdata) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002896 DHD_ERROR(("%s: SOCRAM core is down after reset?\n",
2897 __func__));
Roland Vossenb74ac122011-05-03 11:35:20 +02002898 bcmerror = -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002899 goto fail;
2900 }
2901
2902 bcmerror = dhdsdio_write_vars(bus);
2903 if (bcmerror) {
2904 DHD_ERROR(("%s: no vars written to RAM\n", __func__));
2905 bcmerror = 0;
2906 }
2907
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002908 W_SDREG(0xFFFFFFFF, &bus->regs->intstatus, retries);
2909
Franky Lineb5dc512011-04-25 19:34:04 -07002910 dhdsdio_chip_resetcore(bus->sdh, bus->ci->armcorebase);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002911
2912 /* Allow HT Clock now that the ARM is running. */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002913 bus->alp_only = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002914
2915 bus->dhd->busstate = DHD_BUS_LOAD;
2916 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002917fail:
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002918 return bcmerror;
2919}
2920
2921int
2922dhd_bus_iovar_op(dhd_pub_t *dhdp, const char *name,
2923 void *params, int plen, void *arg, int len, bool set)
2924{
2925 dhd_bus_t *bus = dhdp->bus;
Roland Vossen67ad48b2011-06-01 13:45:51 +02002926 const struct brcmu_iovar *vi = NULL;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002927 int bcmerror = 0;
2928 int val_size;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002929 u32 actionid;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002930
2931 DHD_TRACE(("%s: Enter\n", __func__));
2932
2933 ASSERT(name);
2934 ASSERT(len >= 0);
2935
2936 /* Get MUST have return space */
2937 ASSERT(set || (arg && len));
2938
2939 /* Set does NOT take qualifiers */
2940 ASSERT(!set || (!params && !plen));
2941
2942 /* Look up var locally; if not found pass to host driver */
Roland Vossen67ad48b2011-06-01 13:45:51 +02002943 vi = brcmu_iovar_lookup(dhdsdio_iovars, name);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002944 if (vi == NULL) {
2945 dhd_os_sdlock(bus->dhd);
2946
2947 BUS_WAKE(bus);
2948
2949 /* Turn on clock in case SD command needs backplane */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002950 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002951
2952 bcmerror =
2953 bcmsdh_iovar_op(bus->sdh, name, params, plen, arg, len,
2954 set);
2955
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002956 /* Similar check for blocksize change */
2957 if (set && strcmp(name, "sd_blocksize") == 0) {
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002958 s32 fnum = 2;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002959 if (bcmsdh_iovar_op
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002960 (bus->sdh, "sd_blocksize", &fnum, sizeof(s32),
2961 &bus->blocksize, sizeof(s32),
Roland Vossena1c5ad82011-04-11 15:16:24 +02002962 false) != 0) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002963 bus->blocksize = 0;
2964 DHD_ERROR(("%s: fail on %s get\n", __func__,
2965 "sd_blocksize"));
2966 } else {
2967 DHD_INFO(("%s: noted %s update, value now %d\n",
2968 __func__, "sd_blocksize",
2969 bus->blocksize));
2970 }
2971 }
Greg Kroah-Hartman7068c2f2010-10-08 11:34:59 -07002972 bus->roundup = min(max_roundup, bus->blocksize);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002973
2974 if ((bus->idletime == DHD_IDLE_IMMEDIATE) && !bus->dpc_sched) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002975 bus->activity = false;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07002976 dhdsdio_clkctl(bus, CLK_NONE, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002977 }
2978
2979 dhd_os_sdunlock(bus->dhd);
2980 goto exit;
2981 }
2982
2983 DHD_CTL(("%s: %s %s, len %d plen %d\n", __func__,
2984 name, (set ? "set" : "get"), len, plen));
2985
2986 /* set up 'params' pointer in case this is a set command so that
2987 * the convenience int and bool code can be common to set and get
2988 */
2989 if (params == NULL) {
2990 params = arg;
2991 plen = len;
2992 }
2993
2994 if (vi->type == IOVT_VOID)
2995 val_size = 0;
2996 else if (vi->type == IOVT_BUFFER)
2997 val_size = len;
2998 else
2999 /* all other types are integer sized */
3000 val_size = sizeof(int);
3001
3002 actionid = set ? IOV_SVAL(vi->varid) : IOV_GVAL(vi->varid);
3003 bcmerror =
3004 dhdsdio_doiovar(bus, vi, actionid, name, params, plen, arg, len,
3005 val_size);
3006
3007exit:
3008 return bcmerror;
3009}
3010
3011void dhd_bus_stop(struct dhd_bus *bus, bool enforce_mutex)
3012{
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07003013 u32 local_hostintmask;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003014 u8 saveclk;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003015 uint retries;
3016 int err;
3017
3018 DHD_TRACE(("%s: Enter\n", __func__));
3019
3020 if (enforce_mutex)
3021 dhd_os_sdlock(bus->dhd);
3022
3023 BUS_WAKE(bus);
3024
3025 /* Enable clock for device interrupts */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003026 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003027
3028 /* Disable and clear interrupts at the chip level also */
3029 W_SDREG(0, &bus->regs->hostintmask, retries);
3030 local_hostintmask = bus->hostintmask;
3031 bus->hostintmask = 0;
3032
3033 /* Change our idea of bus state */
3034 bus->dhd->busstate = DHD_BUS_DOWN;
3035
3036 /* Force clocks on backplane to be sure F2 interrupt propagates */
3037 saveclk =
3038 bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
3039 &err);
3040 if (!err) {
3041 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
3042 (saveclk | SBSDIO_FORCE_HT), &err);
3043 }
3044 if (err) {
3045 DHD_ERROR(("%s: Failed to force clock for F2: err %d\n",
3046 __func__, err));
3047 }
3048
3049 /* Turn off the bus (F2), free any pending packets */
3050 DHD_INTR(("%s: disable SDIO interrupts\n", __func__));
3051 bcmsdh_intr_disable(bus->sdh);
Franky Lin0df46042011-06-01 13:45:40 +02003052 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_0, SDIO_CCCR_IOEx,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003053 SDIO_FUNC_ENABLE_1, NULL);
3054
3055 /* Clear any pending interrupts now that F2 is disabled */
3056 W_SDREG(local_hostintmask, &bus->regs->intstatus, retries);
3057
3058 /* Turn off the backplane clock (only) */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003059 dhdsdio_clkctl(bus, CLK_SDONLY, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003060
3061 /* Clear the data packet queues */
Roland Vossen67ad48b2011-06-01 13:45:51 +02003062 brcmu_pktq_flush(&bus->txq, true, NULL, NULL);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003063
3064 /* Clear any held glomming stuff */
3065 if (bus->glomd)
Roland Vossen67ad48b2011-06-01 13:45:51 +02003066 brcmu_pkt_buf_free_skb(bus->glomd);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003067
3068 if (bus->glom)
Roland Vossen67ad48b2011-06-01 13:45:51 +02003069 brcmu_pkt_buf_free_skb(bus->glom);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003070
3071 bus->glom = bus->glomd = NULL;
3072
3073 /* Clear rx control and wake any waiters */
3074 bus->rxlen = 0;
3075 dhd_os_ioctl_resp_wake(bus->dhd);
3076
3077 /* Reset some F2 state stuff */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003078 bus->rxskip = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003079 bus->tx_seq = bus->rx_seq = 0;
3080
3081 if (enforce_mutex)
3082 dhd_os_sdunlock(bus->dhd);
3083}
3084
3085int dhd_bus_init(dhd_pub_t *dhdp, bool enforce_mutex)
3086{
3087 dhd_bus_t *bus = dhdp->bus;
3088 dhd_timeout_t tmo;
3089 uint retries = 0;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003090 u8 ready, enable;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003091 int err, ret = 0;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003092 u8 saveclk;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003093
3094 DHD_TRACE(("%s: Enter\n", __func__));
3095
3096 ASSERT(bus->dhd);
3097 if (!bus->dhd)
3098 return 0;
3099
3100 if (enforce_mutex)
3101 dhd_os_sdlock(bus->dhd);
3102
3103 /* Make sure backplane clock is on, needed to generate F2 interrupt */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003104 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003105 if (bus->clkstate != CLK_AVAIL)
3106 goto exit;
3107
3108 /* Force clocks on backplane to be sure F2 interrupt propagates */
3109 saveclk =
3110 bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
3111 &err);
3112 if (!err) {
3113 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
3114 (saveclk | SBSDIO_FORCE_HT), &err);
3115 }
3116 if (err) {
3117 DHD_ERROR(("%s: Failed to force clock for F2: err %d\n",
3118 __func__, err));
3119 goto exit;
3120 }
3121
3122 /* Enable function 2 (frame transfers) */
3123 W_SDREG((SDPCM_PROT_VERSION << SMB_DATA_VERSION_SHIFT),
3124 &bus->regs->tosbmailboxdata, retries);
3125 enable = (SDIO_FUNC_ENABLE_1 | SDIO_FUNC_ENABLE_2);
3126
Franky Lin0df46042011-06-01 13:45:40 +02003127 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_0, SDIO_CCCR_IOEx, enable, NULL);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003128
3129 /* Give the dongle some time to do its thing and set IOR2 */
3130 dhd_timeout_start(&tmo, DHD_WAIT_F2RDY * 1000);
3131
3132 ready = 0;
3133 while (ready != enable && !dhd_timeout_expired(&tmo))
3134 ready =
Franky Lin0df46042011-06-01 13:45:40 +02003135 bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_0, SDIO_CCCR_IORx,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003136 NULL);
3137
3138 DHD_INFO(("%s: enable 0x%02x, ready 0x%02x (waited %uus)\n",
3139 __func__, enable, ready, tmo.elapsed));
3140
3141 /* If F2 successfully enabled, set core and enable interrupts */
3142 if (ready == enable) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003143 /* Set up the interrupt mask and enable interrupts */
3144 bus->hostintmask = HOSTINTMASK;
Franky Linc05df632011-04-25 19:34:07 -07003145 W_SDREG(bus->hostintmask,
3146 (unsigned int *)CORE_BUS_REG(bus->ci->buscorebase,
3147 hostintmask), retries);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003148
3149 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_WATERMARK,
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003150 (u8) watermark, &err);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003151
3152 /* Set bus state according to enable result */
3153 dhdp->busstate = DHD_BUS_DATA;
3154
3155 /* bcmsdh_intr_unmask(bus->sdh); */
3156
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003157 bus->intdis = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003158 if (bus->intr) {
3159 DHD_INTR(("%s: enable SDIO device interrupts\n",
3160 __func__));
3161 bcmsdh_intr_enable(bus->sdh);
3162 } else {
3163 DHD_INTR(("%s: disable SDIO interrupts\n", __func__));
3164 bcmsdh_intr_disable(bus->sdh);
3165 }
3166
3167 }
3168
3169 else {
3170 /* Disable F2 again */
3171 enable = SDIO_FUNC_ENABLE_1;
Franky Lin0df46042011-06-01 13:45:40 +02003172 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_0, SDIO_CCCR_IOEx, enable,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003173 NULL);
3174 }
3175
3176 /* Restore previous clock setting */
3177 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
3178 saveclk, &err);
3179
3180 /* If we didn't come up, turn off backplane clock */
3181 if (dhdp->busstate != DHD_BUS_DATA)
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003182 dhdsdio_clkctl(bus, CLK_NONE, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003183
3184exit:
3185 if (enforce_mutex)
3186 dhd_os_sdunlock(bus->dhd);
3187
3188 return ret;
3189}
3190
3191static void dhdsdio_rxfail(dhd_bus_t *bus, bool abort, bool rtx)
3192{
3193 bcmsdh_info_t *sdh = bus->sdh;
Franky Lin597600a2011-06-01 13:45:39 +02003194 struct sdpcmd_regs *regs = bus->regs;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003195 uint retries = 0;
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07003196 u16 lastrbc;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003197 u8 hi, lo;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003198 int err;
3199
3200 DHD_ERROR(("%s: %sterminate frame%s\n", __func__,
3201 (abort ? "abort command, " : ""),
3202 (rtx ? ", send NAK" : "")));
3203
3204 if (abort)
3205 bcmsdh_abort(sdh, SDIO_FUNC_2);
3206
3207 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_FRAMECTRL, SFC_RF_TERM,
3208 &err);
3209 bus->f1regdata++;
3210
3211 /* Wait until the packet has been flushed (device/FIFO stable) */
3212 for (lastrbc = retries = 0xffff; retries > 0; retries--) {
3213 hi = bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_RFRAMEBCHI,
3214 NULL);
3215 lo = bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_RFRAMEBCLO,
3216 NULL);
3217 bus->f1regdata += 2;
3218
3219 if ((hi == 0) && (lo == 0))
3220 break;
3221
3222 if ((hi > (lastrbc >> 8)) && (lo > (lastrbc & 0x00ff))) {
3223 DHD_ERROR(("%s: count growing: last 0x%04x now "
3224 "0x%04x\n",
3225 __func__, lastrbc, ((hi << 8) + lo)));
3226 }
3227 lastrbc = (hi << 8) + lo;
3228 }
3229
3230 if (!retries) {
3231 DHD_ERROR(("%s: count never zeroed: last 0x%04x\n",
3232 __func__, lastrbc));
3233 } else {
3234 DHD_INFO(("%s: flush took %d iterations\n", __func__,
3235 (0xffff - retries)));
3236 }
3237
3238 if (rtx) {
3239 bus->rxrtx++;
3240 W_SDREG(SMB_NAK, &regs->tosbmailbox, retries);
3241 bus->f1regdata++;
3242 if (retries <= retry_limit)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003243 bus->rxskip = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003244 }
3245
3246 /* Clear partial in any case */
3247 bus->nextlen = 0;
3248
3249 /* If we can't reach the device, signal failure */
3250 if (err || bcmsdh_regfail(sdh))
3251 bus->dhd->busstate = DHD_BUS_DOWN;
3252}
3253
3254static void
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003255dhdsdio_read_control(dhd_bus_t *bus, u8 *hdr, uint len, uint doff)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003256{
3257 bcmsdh_info_t *sdh = bus->sdh;
3258 uint rdlen, pad;
3259
3260 int sdret;
3261
3262 DHD_TRACE(("%s: Enter\n", __func__));
3263
3264 /* Control data already received in aligned rxctl */
3265 if ((bus->bus == SPI_BUS) && (!bus->usebufpool))
3266 goto gotpkt;
3267
3268 ASSERT(bus->rxbuf);
3269 /* Set rxctl for frame (w/optional alignment) */
3270 bus->rxctl = bus->rxbuf;
3271 if (dhd_alignctl) {
3272 bus->rxctl += firstread;
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07003273 pad = ((unsigned long)bus->rxctl % DHD_SDALIGN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003274 if (pad)
3275 bus->rxctl += (DHD_SDALIGN - pad);
3276 bus->rxctl -= firstread;
3277 }
3278 ASSERT(bus->rxctl >= bus->rxbuf);
3279
3280 /* Copy the already-read portion over */
Stanislav Fomichev02160692011-02-15 01:05:10 +03003281 memcpy(bus->rxctl, hdr, firstread);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003282 if (len <= firstread)
3283 goto gotpkt;
3284
3285 /* Copy the full data pkt in gSPI case and process ioctl. */
3286 if (bus->bus == SPI_BUS) {
Stanislav Fomichev02160692011-02-15 01:05:10 +03003287 memcpy(bus->rxctl, hdr, len);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003288 goto gotpkt;
3289 }
3290
3291 /* Raise rdlen to next SDIO block to avoid tail command */
3292 rdlen = len - firstread;
3293 if (bus->roundup && bus->blocksize && (rdlen > bus->blocksize)) {
3294 pad = bus->blocksize - (rdlen % bus->blocksize);
3295 if ((pad <= bus->roundup) && (pad < bus->blocksize) &&
3296 ((len + pad) < bus->dhd->maxctl))
3297 rdlen += pad;
3298 } else if (rdlen % DHD_SDALIGN) {
3299 rdlen += DHD_SDALIGN - (rdlen % DHD_SDALIGN);
3300 }
3301
3302 /* Satisfy length-alignment requirements */
3303 if (forcealign && (rdlen & (ALIGNMENT - 1)))
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07003304 rdlen = roundup(rdlen, ALIGNMENT);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003305
3306 /* Drop if the read is too big or it exceeds our maximum */
3307 if ((rdlen + firstread) > bus->dhd->maxctl) {
3308 DHD_ERROR(("%s: %d-byte control read exceeds %d-byte buffer\n",
3309 __func__, rdlen, bus->dhd->maxctl));
3310 bus->dhd->rx_errors++;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003311 dhdsdio_rxfail(bus, false, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003312 goto done;
3313 }
3314
3315 if ((len - doff) > bus->dhd->maxctl) {
3316 DHD_ERROR(("%s: %d-byte ctl frame (%d-byte ctl data) exceeds "
3317 "%d-byte limit\n",
3318 __func__, len, (len - doff), bus->dhd->maxctl));
3319 bus->dhd->rx_errors++;
3320 bus->rx_toolong++;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003321 dhdsdio_rxfail(bus, false, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003322 goto done;
3323 }
3324
3325 /* Read remainder of frame body into the rxctl buffer */
Grant Grundler4b455e02011-05-04 09:59:47 -07003326 sdret = bcmsdh_recv_buf(bus, bcmsdh_cur_sbwad(sdh), SDIO_FUNC_2,
3327 F2SYNC, (bus->rxctl + firstread), rdlen,
3328 NULL, NULL, NULL);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003329 bus->f2rxdata++;
Roland Vossenb7ef2a92011-05-03 11:35:02 +02003330 ASSERT(sdret != -BCME_PENDING);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003331
3332 /* Control frame failures need retransmission */
3333 if (sdret < 0) {
3334 DHD_ERROR(("%s: read %d control bytes failed: %d\n",
3335 __func__, rdlen, sdret));
3336 bus->rxc_errors++; /* dhd.rx_ctlerrs is higher level */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003337 dhdsdio_rxfail(bus, true, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003338 goto done;
3339 }
3340
3341gotpkt:
3342
3343#ifdef DHD_DEBUG
Arend van Spriel34227312011-05-10 22:25:32 +02003344 if (DHD_BYTES_ON() && DHD_CTL_ON()) {
3345 printk(KERN_DEBUG "RxCtrl:\n");
3346 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, bus->rxctl, len);
3347 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003348#endif
3349
3350 /* Point to valid data and indicate its length */
3351 bus->rxctl += doff;
3352 bus->rxlen = len - doff;
3353
3354done:
3355 /* Awake any waiters */
3356 dhd_os_ioctl_resp_wake(bus->dhd);
3357}
3358
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003359static u8 dhdsdio_rxglom(dhd_bus_t *bus, u8 rxseq)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003360{
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07003361 u16 dlen, totlen;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003362 u8 *dptr, num = 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003363
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07003364 u16 sublen, check;
Arend van Sprielc26b1372010-11-23 14:06:23 +01003365 struct sk_buff *pfirst, *plast, *pnext, *save_pfirst;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003366
3367 int errcode;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003368 u8 chan, seq, doff, sfdoff;
3369 u8 txmax;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003370
3371 int ifidx = 0;
3372 bool usechain = bus->use_rxchain;
3373
3374 /* If packets, issue read(s) and send up packet chain */
3375 /* Return sequence numbers consumed? */
3376
3377 DHD_TRACE(("dhdsdio_rxglom: start: glomd %p glom %p\n", bus->glomd,
3378 bus->glom));
3379
3380 /* If there's a descriptor, generate the packet chain */
3381 if (bus->glomd) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003382 pfirst = plast = pnext = NULL;
Arend van Spriel54991ad2010-11-23 14:06:24 +01003383 dlen = (u16) (bus->glomd->len);
3384 dptr = bus->glomd->data;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003385 if (!dlen || (dlen & 1)) {
3386 DHD_ERROR(("%s: bad glomd len(%d), ignore descriptor\n",
3387 __func__, dlen));
3388 dlen = 0;
3389 }
3390
3391 for (totlen = num = 0; dlen; num++) {
3392 /* Get (and move past) next length */
Stanislav Fomichev56dfe3c2011-02-20 21:43:33 +03003393 sublen = get_unaligned_le16(dptr);
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07003394 dlen -= sizeof(u16);
3395 dptr += sizeof(u16);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003396 if ((sublen < SDPCM_HDRLEN) ||
3397 ((num == 0) && (sublen < (2 * SDPCM_HDRLEN)))) {
3398 DHD_ERROR(("%s: descriptor len %d bad: %d\n",
3399 __func__, num, sublen));
3400 pnext = NULL;
3401 break;
3402 }
3403 if (sublen % DHD_SDALIGN) {
3404 DHD_ERROR(("%s: sublen %d not multiple of %d\n",
3405 __func__, sublen, DHD_SDALIGN));
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003406 usechain = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003407 }
3408 totlen += sublen;
3409
3410 /* For last frame, adjust read len so total
3411 is a block multiple */
3412 if (!dlen) {
3413 sublen +=
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07003414 (roundup(totlen, bus->blocksize) - totlen);
3415 totlen = roundup(totlen, bus->blocksize);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003416 }
3417
3418 /* Allocate/chain packet for next subframe */
Roland Vossen67ad48b2011-06-01 13:45:51 +02003419 pnext = brcmu_pkt_buf_get_skb(sublen + DHD_SDALIGN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003420 if (pnext == NULL) {
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02003421 DHD_ERROR(("%s: bcm_pkt_buf_get_skb failed, "
3422 "num %d len %d\n", __func__,
3423 num, sublen));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003424 break;
3425 }
Arend van Spriel54991ad2010-11-23 14:06:24 +01003426 ASSERT(!(pnext->prev));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003427 if (!pfirst) {
3428 ASSERT(!plast);
3429 pfirst = plast = pnext;
3430 } else {
3431 ASSERT(plast);
Arend van Spriel54991ad2010-11-23 14:06:24 +01003432 plast->next = pnext;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003433 plast = pnext;
3434 }
3435
3436 /* Adhere to start alignment requirements */
Arend van Spriel3c9d4c32011-03-02 21:18:48 +01003437 PKTALIGN(pnext, sublen, DHD_SDALIGN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003438 }
3439
3440 /* If all allocations succeeded, save packet chain
3441 in bus structure */
3442 if (pnext) {
3443 DHD_GLOM(("%s: allocated %d-byte packet chain for %d "
3444 "subframes\n", __func__, totlen, num));
3445 if (DHD_GLOM_ON() && bus->nextlen) {
3446 if (totlen != bus->nextlen) {
3447 DHD_GLOM(("%s: glomdesc mismatch: nextlen %d glomdesc %d " "rxseq %d\n",
3448 __func__, bus->nextlen,
3449 totlen, rxseq));
3450 }
3451 }
3452 bus->glom = pfirst;
3453 pfirst = pnext = NULL;
3454 } else {
3455 if (pfirst)
Roland Vossen67ad48b2011-06-01 13:45:51 +02003456 brcmu_pkt_buf_free_skb(pfirst);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003457 bus->glom = NULL;
3458 num = 0;
3459 }
3460
3461 /* Done with descriptor packet */
Roland Vossen67ad48b2011-06-01 13:45:51 +02003462 brcmu_pkt_buf_free_skb(bus->glomd);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003463 bus->glomd = NULL;
3464 bus->nextlen = 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003465 }
3466
3467 /* Ok -- either we just generated a packet chain,
3468 or had one from before */
3469 if (bus->glom) {
3470 if (DHD_GLOM_ON()) {
3471 DHD_GLOM(("%s: try superframe read, packet chain:\n",
3472 __func__));
Arend van Spriel54991ad2010-11-23 14:06:24 +01003473 for (pnext = bus->glom; pnext; pnext = pnext->next) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003474 DHD_GLOM((" %p: %p len 0x%04x (%d)\n",
Arend van Spriel54991ad2010-11-23 14:06:24 +01003475 pnext, (u8 *) (pnext->data),
3476 pnext->len, pnext->len));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003477 }
3478 }
3479
3480 pfirst = bus->glom;
Roland Vossen67ad48b2011-06-01 13:45:51 +02003481 dlen = (u16) brcmu_pkttotlen(pfirst);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003482
3483 /* Do an SDIO read for the superframe. Configurable iovar to
3484 * read directly into the chained packet, or allocate a large
3485 * packet and and copy into the chain.
3486 */
3487 if (usechain) {
Grant Grundler4b455e02011-05-04 09:59:47 -07003488 errcode = bcmsdh_recv_buf(bus,
3489 bcmsdh_cur_sbwad(bus->sdh), SDIO_FUNC_2,
3490 F2SYNC, (u8 *) pfirst->data, dlen,
3491 pfirst, NULL, NULL);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003492 } else if (bus->dataptr) {
Grant Grundler4b455e02011-05-04 09:59:47 -07003493 errcode = bcmsdh_recv_buf(bus,
3494 bcmsdh_cur_sbwad(bus->sdh), SDIO_FUNC_2,
3495 F2SYNC, bus->dataptr, dlen,
3496 NULL, NULL, NULL);
Roland Vossen67ad48b2011-06-01 13:45:51 +02003497 sublen = (u16) brcmu_pktfrombuf(pfirst, 0, dlen,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003498 bus->dataptr);
3499 if (sublen != dlen) {
3500 DHD_ERROR(("%s: FAILED TO COPY, dlen %d sublen %d\n",
3501 __func__, dlen, sublen));
3502 errcode = -1;
3503 }
3504 pnext = NULL;
3505 } else {
3506 DHD_ERROR(("COULDN'T ALLOC %d-BYTE GLOM, FORCE FAILURE\n",
3507 dlen));
3508 errcode = -1;
3509 }
3510 bus->f2rxdata++;
Roland Vossenb7ef2a92011-05-03 11:35:02 +02003511 ASSERT(errcode != -BCME_PENDING);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003512
3513 /* On failure, kill the superframe, allow a couple retries */
3514 if (errcode < 0) {
3515 DHD_ERROR(("%s: glom read of %d bytes failed: %d\n",
3516 __func__, dlen, errcode));
3517 bus->dhd->rx_errors++;
3518
3519 if (bus->glomerr++ < 3) {
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003520 dhdsdio_rxfail(bus, true, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003521 } else {
3522 bus->glomerr = 0;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003523 dhdsdio_rxfail(bus, true, false);
Roland Vossen67ad48b2011-06-01 13:45:51 +02003524 brcmu_pkt_buf_free_skb(bus->glom);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003525 bus->rxglomfail++;
3526 bus->glom = NULL;
3527 }
3528 return 0;
3529 }
3530#ifdef DHD_DEBUG
3531 if (DHD_GLOM_ON()) {
Arend van Spriel34227312011-05-10 22:25:32 +02003532 printk(KERN_DEBUG "SUPERFRAME:\n");
3533 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
3534 pfirst->data, min_t(int, pfirst->len, 48));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003535 }
3536#endif
3537
3538 /* Validate the superframe header */
Arend van Spriel54991ad2010-11-23 14:06:24 +01003539 dptr = (u8 *) (pfirst->data);
Stanislav Fomichev56dfe3c2011-02-20 21:43:33 +03003540 sublen = get_unaligned_le16(dptr);
3541 check = get_unaligned_le16(dptr + sizeof(u16));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003542
3543 chan = SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]);
3544 seq = SDPCM_PACKET_SEQUENCE(&dptr[SDPCM_FRAMETAG_LEN]);
3545 bus->nextlen = dptr[SDPCM_FRAMETAG_LEN + SDPCM_NEXTLEN_OFFSET];
3546 if ((bus->nextlen << 4) > MAX_RX_DATASZ) {
3547 DHD_INFO(("%s: nextlen too large (%d) seq %d\n",
3548 __func__, bus->nextlen, seq));
3549 bus->nextlen = 0;
3550 }
3551 doff = SDPCM_DOFFSET_VALUE(&dptr[SDPCM_FRAMETAG_LEN]);
3552 txmax = SDPCM_WINDOW_VALUE(&dptr[SDPCM_FRAMETAG_LEN]);
3553
3554 errcode = 0;
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07003555 if ((u16)~(sublen ^ check)) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003556 DHD_ERROR(("%s (superframe): HW hdr error: len/check "
3557 "0x%04x/0x%04x\n", __func__, sublen, check));
3558 errcode = -1;
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07003559 } else if (roundup(sublen, bus->blocksize) != dlen) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003560 DHD_ERROR(("%s (superframe): len 0x%04x, rounded "
3561 "0x%04x, expect 0x%04x\n",
3562 __func__, sublen,
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07003563 roundup(sublen, bus->blocksize), dlen));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003564 errcode = -1;
3565 } else if (SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]) !=
3566 SDPCM_GLOM_CHANNEL) {
3567 DHD_ERROR(("%s (superframe): bad channel %d\n",
3568 __func__,
3569 SDPCM_PACKET_CHANNEL(&dptr
3570 [SDPCM_FRAMETAG_LEN])));
3571 errcode = -1;
3572 } else if (SDPCM_GLOMDESC(&dptr[SDPCM_FRAMETAG_LEN])) {
3573 DHD_ERROR(("%s (superframe): got second descriptor?\n",
3574 __func__));
3575 errcode = -1;
3576 } else if ((doff < SDPCM_HDRLEN) ||
Arend van Spriel54991ad2010-11-23 14:06:24 +01003577 (doff > (pfirst->len - SDPCM_HDRLEN))) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003578 DHD_ERROR(("%s (superframe): Bad data offset %d: HW %d "
3579 "pkt %d min %d\n",
3580 __func__, doff, sublen,
Arend van Spriel54991ad2010-11-23 14:06:24 +01003581 pfirst->len, SDPCM_HDRLEN));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003582 errcode = -1;
3583 }
3584
3585 /* Check sequence number of superframe SW header */
3586 if (rxseq != seq) {
3587 DHD_INFO(("%s: (superframe) rx_seq %d, expected %d\n",
3588 __func__, seq, rxseq));
3589 bus->rx_badseq++;
3590 rxseq = seq;
3591 }
3592
3593 /* Check window for sanity */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003594 if ((u8) (txmax - bus->tx_seq) > 0x40) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003595 DHD_ERROR(("%s: unlikely tx max %d with tx_seq %d\n",
3596 __func__, txmax, bus->tx_seq));
3597 txmax = bus->tx_seq + 2;
3598 }
3599 bus->tx_max = txmax;
3600
3601 /* Remove superframe header, remember offset */
Arend van Sprielc303ecb2010-11-18 20:46:43 +01003602 skb_pull(pfirst, doff);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003603 sfdoff = doff;
3604
3605 /* Validate all the subframe headers */
3606 for (num = 0, pnext = pfirst; pnext && !errcode;
Arend van Spriel54991ad2010-11-23 14:06:24 +01003607 num++, pnext = pnext->next) {
3608 dptr = (u8 *) (pnext->data);
3609 dlen = (u16) (pnext->len);
Stanislav Fomichev56dfe3c2011-02-20 21:43:33 +03003610 sublen = get_unaligned_le16(dptr);
3611 check = get_unaligned_le16(dptr + sizeof(u16));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003612 chan = SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]);
3613 doff = SDPCM_DOFFSET_VALUE(&dptr[SDPCM_FRAMETAG_LEN]);
3614#ifdef DHD_DEBUG
Arend van Spriel34227312011-05-10 22:25:32 +02003615 if (DHD_GLOM_ON()) {
3616 printk(KERN_DEBUG "subframe:\n");
3617 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
3618 dptr, 32);
3619 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003620#endif
3621
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07003622 if ((u16)~(sublen ^ check)) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003623 DHD_ERROR(("%s (subframe %d): HW hdr error: "
3624 "len/check 0x%04x/0x%04x\n",
3625 __func__, num, sublen, check));
3626 errcode = -1;
3627 } else if ((sublen > dlen) || (sublen < SDPCM_HDRLEN)) {
3628 DHD_ERROR(("%s (subframe %d): length mismatch: "
3629 "len 0x%04x, expect 0x%04x\n",
3630 __func__, num, sublen, dlen));
3631 errcode = -1;
3632 } else if ((chan != SDPCM_DATA_CHANNEL) &&
3633 (chan != SDPCM_EVENT_CHANNEL)) {
3634 DHD_ERROR(("%s (subframe %d): bad channel %d\n",
3635 __func__, num, chan));
3636 errcode = -1;
3637 } else if ((doff < SDPCM_HDRLEN) || (doff > sublen)) {
3638 DHD_ERROR(("%s (subframe %d): Bad data offset %d: HW %d min %d\n",
3639 __func__, num, doff, sublen,
3640 SDPCM_HDRLEN));
3641 errcode = -1;
3642 }
3643 }
3644
3645 if (errcode) {
3646 /* Terminate frame on error, request
3647 a couple retries */
3648 if (bus->glomerr++ < 3) {
3649 /* Restore superframe header space */
Arend van Sprielc303ecb2010-11-18 20:46:43 +01003650 skb_push(pfirst, sfdoff);
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003651 dhdsdio_rxfail(bus, true, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003652 } else {
3653 bus->glomerr = 0;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003654 dhdsdio_rxfail(bus, true, false);
Roland Vossen67ad48b2011-06-01 13:45:51 +02003655 brcmu_pkt_buf_free_skb(bus->glom);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003656 bus->rxglomfail++;
3657 bus->glom = NULL;
3658 }
3659 bus->nextlen = 0;
3660 return 0;
3661 }
3662
3663 /* Basic SD framing looks ok - process each packet (header) */
3664 save_pfirst = pfirst;
3665 bus->glom = NULL;
3666 plast = NULL;
3667
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003668 for (num = 0; pfirst; rxseq++, pfirst = pnext) {
Arend van Spriel54991ad2010-11-23 14:06:24 +01003669 pnext = pfirst->next;
3670 pfirst->next = NULL;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003671
Arend van Spriel54991ad2010-11-23 14:06:24 +01003672 dptr = (u8 *) (pfirst->data);
Stanislav Fomichev56dfe3c2011-02-20 21:43:33 +03003673 sublen = get_unaligned_le16(dptr);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003674 chan = SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]);
3675 seq = SDPCM_PACKET_SEQUENCE(&dptr[SDPCM_FRAMETAG_LEN]);
3676 doff = SDPCM_DOFFSET_VALUE(&dptr[SDPCM_FRAMETAG_LEN]);
3677
3678 DHD_GLOM(("%s: Get subframe %d, %p(%p/%d), sublen %d "
3679 "chan %d seq %d\n",
Arend van Spriel54991ad2010-11-23 14:06:24 +01003680 __func__, num, pfirst, pfirst->data,
3681 pfirst->len, sublen, chan, seq));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003682
3683 ASSERT((chan == SDPCM_DATA_CHANNEL)
3684 || (chan == SDPCM_EVENT_CHANNEL));
3685
3686 if (rxseq != seq) {
3687 DHD_GLOM(("%s: rx_seq %d, expected %d\n",
3688 __func__, seq, rxseq));
3689 bus->rx_badseq++;
3690 rxseq = seq;
3691 }
3692#ifdef DHD_DEBUG
Arend van Spriel34227312011-05-10 22:25:32 +02003693 if (DHD_BYTES_ON() && DHD_DATA_ON()) {
3694 printk(KERN_DEBUG "Rx Subframe Data:\n");
3695 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
3696 dptr, dlen);
3697 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003698#endif
3699
Arend van Spriel2cb8ada2010-11-18 20:46:44 +01003700 __skb_trim(pfirst, sublen);
Arend van Sprielc303ecb2010-11-18 20:46:43 +01003701 skb_pull(pfirst, doff);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003702
Arend van Spriel54991ad2010-11-23 14:06:24 +01003703 if (pfirst->len == 0) {
Roland Vossen67ad48b2011-06-01 13:45:51 +02003704 brcmu_pkt_buf_free_skb(pfirst);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003705 if (plast) {
Arend van Spriel54991ad2010-11-23 14:06:24 +01003706 plast->next = pnext;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003707 } else {
3708 ASSERT(save_pfirst == pfirst);
3709 save_pfirst = pnext;
3710 }
3711 continue;
3712 } else if (dhd_prot_hdrpull(bus->dhd, &ifidx, pfirst) !=
3713 0) {
3714 DHD_ERROR(("%s: rx protocol error\n",
3715 __func__));
3716 bus->dhd->rx_errors++;
Roland Vossen67ad48b2011-06-01 13:45:51 +02003717 brcmu_pkt_buf_free_skb(pfirst);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003718 if (plast) {
Arend van Spriel54991ad2010-11-23 14:06:24 +01003719 plast->next = pnext;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003720 } else {
3721 ASSERT(save_pfirst == pfirst);
3722 save_pfirst = pnext;
3723 }
3724 continue;
3725 }
3726
3727 /* this packet will go up, link back into
3728 chain and count it */
Arend van Spriel54991ad2010-11-23 14:06:24 +01003729 pfirst->next = pnext;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003730 plast = pfirst;
3731 num++;
3732
3733#ifdef DHD_DEBUG
3734 if (DHD_GLOM_ON()) {
3735 DHD_GLOM(("%s subframe %d to stack, %p(%p/%d) "
3736 "nxt/lnk %p/%p\n",
Arend van Spriel54991ad2010-11-23 14:06:24 +01003737 __func__, num, pfirst, pfirst->data,
3738 pfirst->len, pfirst->next,
3739 pfirst->prev));
Arend van Spriel34227312011-05-10 22:25:32 +02003740 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
3741 pfirst->data,
3742 min_t(int, pfirst->len, 32));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003743 }
3744#endif /* DHD_DEBUG */
3745 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003746 if (num) {
3747 dhd_os_sdunlock(bus->dhd);
3748 dhd_rx_frame(bus->dhd, ifidx, save_pfirst, num);
3749 dhd_os_sdlock(bus->dhd);
3750 }
3751
3752 bus->rxglomframes++;
3753 bus->rxglompkts += num;
3754 }
3755 return num;
3756}
3757
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003758/* Return true if there may be more frames to read */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003759static uint dhdsdio_readframes(dhd_bus_t *bus, uint maxframes, bool *finished)
3760{
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003761 bcmsdh_info_t *sdh = bus->sdh;
3762
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07003763 u16 len, check; /* Extracted hardware header fields */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003764 u8 chan, seq, doff; /* Extracted software header fields */
3765 u8 fcbits; /* Extracted fcbits from software header */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003766
Arend van Sprielc26b1372010-11-23 14:06:23 +01003767 struct sk_buff *pkt; /* Packet for event or data frames */
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07003768 u16 pad; /* Number of pad bytes to read */
3769 u16 rdlen; /* Total number of bytes to read */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003770 u8 rxseq; /* Next sequence number to expect */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003771 uint rxleft = 0; /* Remaining number of frames allowed */
3772 int sdret; /* Return code from bcmsdh calls */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003773 u8 txmax; /* Maximum tx sequence offered */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003774 bool len_consistent; /* Result of comparing readahead len and
3775 len from hw-hdr */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003776 u8 *rxbuf;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003777 int ifidx = 0;
3778 uint rxcount = 0; /* Total frames read */
3779
3780#if defined(DHD_DEBUG) || defined(SDTEST)
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003781 bool sdtest = false; /* To limit message spew from test mode */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003782#endif
3783
3784 DHD_TRACE(("%s: Enter\n", __func__));
3785
3786 ASSERT(maxframes);
3787
3788#ifdef SDTEST
3789 /* Allow pktgen to override maxframes */
3790 if (bus->pktgen_count && (bus->pktgen_mode == DHD_PKTGEN_RECV)) {
3791 maxframes = bus->pktgen_count;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003792 sdtest = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003793 }
3794#endif
3795
3796 /* Not finished unless we encounter no more frames indication */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003797 *finished = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003798
3799 for (rxseq = bus->rx_seq, rxleft = maxframes;
3800 !bus->rxskip && rxleft && bus->dhd->busstate != DHD_BUS_DOWN;
3801 rxseq++, rxleft--) {
3802
3803 /* Handle glomming separately */
3804 if (bus->glom || bus->glomd) {
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003805 u8 cnt;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003806 DHD_GLOM(("%s: calling rxglom: glomd %p, glom %p\n",
3807 __func__, bus->glomd, bus->glom));
3808 cnt = dhdsdio_rxglom(bus, rxseq);
3809 DHD_GLOM(("%s: rxglom returned %d\n", __func__, cnt));
3810 rxseq += cnt - 1;
3811 rxleft = (rxleft > cnt) ? (rxleft - cnt) : 1;
3812 continue;
3813 }
3814
3815 /* Try doing single read if we can */
3816 if (dhd_readahead && bus->nextlen) {
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07003817 u16 nextlen = bus->nextlen;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003818 bus->nextlen = 0;
3819
3820 if (bus->bus == SPI_BUS) {
3821 rdlen = len = nextlen;
3822 } else {
3823 rdlen = len = nextlen << 4;
3824
3825 /* Pad read to blocksize for efficiency */
3826 if (bus->roundup && bus->blocksize
3827 && (rdlen > bus->blocksize)) {
3828 pad =
3829 bus->blocksize -
3830 (rdlen % bus->blocksize);
3831 if ((pad <= bus->roundup)
3832 && (pad < bus->blocksize)
3833 && ((rdlen + pad + firstread) <
3834 MAX_RX_DATASZ))
3835 rdlen += pad;
3836 } else if (rdlen % DHD_SDALIGN) {
3837 rdlen +=
3838 DHD_SDALIGN - (rdlen % DHD_SDALIGN);
3839 }
3840 }
3841
3842 /* We use bus->rxctl buffer in WinXP for initial
3843 * control pkt receives.
3844 * Later we use buffer-poll for data as well
3845 * as control packets.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003846 * This is required because dhd receives full
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003847 * frame in gSPI unlike SDIO.
3848 * After the frame is received we have to
3849 * distinguish whether it is data
3850 * or non-data frame.
3851 */
3852 /* Allocate a packet buffer */
Roland Vossen67ad48b2011-06-01 13:45:51 +02003853 pkt = brcmu_pkt_buf_get_skb(rdlen + DHD_SDALIGN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003854 if (!pkt) {
3855 if (bus->bus == SPI_BUS) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003856 bus->usebufpool = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003857 bus->rxctl = bus->rxbuf;
3858 if (dhd_alignctl) {
3859 bus->rxctl += firstread;
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07003860 pad = ((unsigned long)bus->rxctl %
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003861 DHD_SDALIGN);
3862 if (pad)
3863 bus->rxctl +=
3864 (DHD_SDALIGN - pad);
3865 bus->rxctl -= firstread;
3866 }
3867 ASSERT(bus->rxctl >= bus->rxbuf);
3868 rxbuf = bus->rxctl;
3869 /* Read the entire frame */
Grant Grundler4b455e02011-05-04 09:59:47 -07003870 sdret = bcmsdh_recv_buf(bus,
3871 bcmsdh_cur_sbwad(sdh),
3872 SDIO_FUNC_2, F2SYNC,
3873 rxbuf, rdlen,
3874 NULL, NULL, NULL);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003875 bus->f2rxdata++;
Roland Vossenb7ef2a92011-05-03 11:35:02 +02003876 ASSERT(sdret != -BCME_PENDING);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003877
3878 /* Control frame failures need
3879 retransmission */
3880 if (sdret < 0) {
3881 DHD_ERROR(("%s: read %d control bytes failed: %d\n",
3882 __func__,
3883 rdlen, sdret));
3884 /* dhd.rx_ctlerrs is higher */
3885 bus->rxc_errors++;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003886 dhdsdio_rxfail(bus, true,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003887 (bus->bus ==
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003888 SPI_BUS) ? false
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003889 : true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003890 continue;
3891 }
3892 } else {
3893 /* Give up on data,
3894 request rtx of events */
Arend van Sprielcda64a52011-05-10 22:25:33 +02003895 DHD_ERROR(("%s (nextlen): "
Roland Vossen67ad48b2011-06-01 13:45:51 +02003896 "brcmu_pkt_buf_get_skb "
3897 "failed:"
Arend van Sprielcda64a52011-05-10 22:25:33 +02003898 " len %d rdlen %d expected"
3899 " rxseq %d\n", __func__,
3900 len, rdlen, rxseq));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003901 continue;
3902 }
3903 } else {
3904 if (bus->bus == SPI_BUS)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003905 bus->usebufpool = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003906
Arend van Spriel54991ad2010-11-23 14:06:24 +01003907 ASSERT(!(pkt->prev));
Arend van Spriel3c9d4c32011-03-02 21:18:48 +01003908 PKTALIGN(pkt, rdlen, DHD_SDALIGN);
Arend van Spriel54991ad2010-11-23 14:06:24 +01003909 rxbuf = (u8 *) (pkt->data);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003910 /* Read the entire frame */
Grant Grundler4b455e02011-05-04 09:59:47 -07003911 sdret = bcmsdh_recv_buf(bus,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003912 bcmsdh_cur_sbwad(sdh),
3913 SDIO_FUNC_2, F2SYNC,
Grant Grundler4b455e02011-05-04 09:59:47 -07003914 rxbuf, rdlen,
3915 pkt, NULL, NULL);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003916 bus->f2rxdata++;
Roland Vossenb7ef2a92011-05-03 11:35:02 +02003917 ASSERT(sdret != -BCME_PENDING);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003918
3919 if (sdret < 0) {
3920 DHD_ERROR(("%s (nextlen): read %d bytes failed: %d\n",
3921 __func__, rdlen, sdret));
Roland Vossen67ad48b2011-06-01 13:45:51 +02003922 brcmu_pkt_buf_free_skb(pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003923 bus->dhd->rx_errors++;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003924 /* Force retry w/normal header read.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003925 * Don't attempt NAK for
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003926 * gSPI
3927 */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003928 dhdsdio_rxfail(bus, true,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003929 (bus->bus ==
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003930 SPI_BUS) ? false :
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003931 true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003932 continue;
3933 }
3934 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003935
3936 /* Now check the header */
Stanislav Fomichev02160692011-02-15 01:05:10 +03003937 memcpy(bus->rxhdr, rxbuf, SDPCM_HDRLEN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003938
3939 /* Extract hardware header fields */
Stanislav Fomichev56dfe3c2011-02-20 21:43:33 +03003940 len = get_unaligned_le16(bus->rxhdr);
3941 check = get_unaligned_le16(bus->rxhdr + sizeof(u16));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003942
3943 /* All zeros means readahead info was bad */
3944 if (!(len | check)) {
3945 DHD_INFO(("%s (nextlen): read zeros in HW "
3946 "header???\n", __func__));
Grant Grundler4b455e02011-05-04 09:59:47 -07003947 dhdsdio_pktfree2(bus, pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003948 continue;
3949 }
3950
3951 /* Validate check bytes */
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07003952 if ((u16)~(len ^ check)) {
Grant Grundler4b455e02011-05-04 09:59:47 -07003953 DHD_ERROR(("%s (nextlen): HW hdr error:"
3954 " nextlen/len/check"
3955 " 0x%04x/0x%04x/0x%04x\n",
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003956 __func__, nextlen, len, check));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003957 bus->rx_badhdr++;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003958 dhdsdio_rxfail(bus, false, false);
Grant Grundler4b455e02011-05-04 09:59:47 -07003959 dhdsdio_pktfree2(bus, pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003960 continue;
3961 }
3962
3963 /* Validate frame length */
3964 if (len < SDPCM_HDRLEN) {
3965 DHD_ERROR(("%s (nextlen): HW hdr length "
3966 "invalid: %d\n", __func__, len));
Grant Grundler4b455e02011-05-04 09:59:47 -07003967 dhdsdio_pktfree2(bus, pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003968 continue;
3969 }
3970
3971 /* Check for consistency withreadahead info */
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07003972 len_consistent = (nextlen != (roundup(len, 16) >> 4));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003973 if (len_consistent) {
3974 /* Mismatch, force retry w/normal
3975 header (may be >4K) */
Grant Grundler4b455e02011-05-04 09:59:47 -07003976 DHD_ERROR(("%s (nextlen): mismatch, "
3977 "nextlen %d len %d rnd %d; "
3978 "expected rxseq %d\n",
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003979 __func__, nextlen,
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07003980 len, roundup(len, 16), rxseq));
Grant Grundler4b455e02011-05-04 09:59:47 -07003981 dhdsdio_rxfail(bus, true, (bus->bus != SPI_BUS));
3982 dhdsdio_pktfree2(bus, pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003983 continue;
3984 }
3985
3986 /* Extract software header fields */
Grant Grundler4b455e02011-05-04 09:59:47 -07003987 chan = SDPCM_PACKET_CHANNEL(
3988 &bus->rxhdr[SDPCM_FRAMETAG_LEN]);
3989 seq = SDPCM_PACKET_SEQUENCE(
3990 &bus->rxhdr[SDPCM_FRAMETAG_LEN]);
3991 doff = SDPCM_DOFFSET_VALUE(
3992 &bus->rxhdr[SDPCM_FRAMETAG_LEN]);
3993 txmax = SDPCM_WINDOW_VALUE(
3994 &bus->rxhdr[SDPCM_FRAMETAG_LEN]);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003995
3996 bus->nextlen =
3997 bus->rxhdr[SDPCM_FRAMETAG_LEN +
3998 SDPCM_NEXTLEN_OFFSET];
3999 if ((bus->nextlen << 4) > MAX_RX_DATASZ) {
4000 DHD_INFO(("%s (nextlen): got frame w/nextlen too large" " (%d), seq %d\n",
4001 __func__, bus->nextlen, seq));
4002 bus->nextlen = 0;
4003 }
4004
4005 bus->dhd->rx_readahead_cnt++;
Grant Grundler4b455e02011-05-04 09:59:47 -07004006
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004007 /* Handle Flow Control */
Grant Grundler4b455e02011-05-04 09:59:47 -07004008 fcbits = SDPCM_FCMASK_VALUE(
4009 &bus->rxhdr[SDPCM_FRAMETAG_LEN]);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004010
Grant Grundler4b455e02011-05-04 09:59:47 -07004011 if (bus->flowcontrol != fcbits) {
4012 if (~bus->flowcontrol & fcbits)
4013 bus->fc_xoff++;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004014
Grant Grundler4b455e02011-05-04 09:59:47 -07004015 if (bus->flowcontrol & ~fcbits)
4016 bus->fc_xon++;
4017
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004018 bus->fc_rcvd++;
4019 bus->flowcontrol = fcbits;
4020 }
4021
4022 /* Check and update sequence number */
4023 if (rxseq != seq) {
4024 DHD_INFO(("%s (nextlen): rx_seq %d, expected "
4025 "%d\n", __func__, seq, rxseq));
4026 bus->rx_badseq++;
4027 rxseq = seq;
4028 }
4029
4030 /* Check window for sanity */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004031 if ((u8) (txmax - bus->tx_seq) > 0x40) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004032 DHD_ERROR(("%s: got unlikely tx max %d with "
4033 "tx_seq %d\n",
4034 __func__, txmax, bus->tx_seq));
4035 txmax = bus->tx_seq + 2;
4036 }
4037 bus->tx_max = txmax;
4038
4039#ifdef DHD_DEBUG
Arend van Spriel34227312011-05-10 22:25:32 +02004040 if (DHD_BYTES_ON() && DHD_DATA_ON()) {
4041 printk(KERN_DEBUG "Rx Data:\n");
4042 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
4043 rxbuf, len);
4044 } else if (DHD_HDRS_ON()) {
4045 printk(KERN_DEBUG "RxHdr:\n");
4046 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
4047 bus->rxhdr, SDPCM_HDRLEN);
4048 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004049#endif
4050
4051 if (chan == SDPCM_CONTROL_CHANNEL) {
4052 if (bus->bus == SPI_BUS) {
4053 dhdsdio_read_control(bus, rxbuf, len,
4054 doff);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004055 } else {
4056 DHD_ERROR(("%s (nextlen): readahead on control" " packet %d?\n",
4057 __func__, seq));
4058 /* Force retry w/normal header read */
4059 bus->nextlen = 0;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004060 dhdsdio_rxfail(bus, false, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004061 }
Grant Grundler4b455e02011-05-04 09:59:47 -07004062 dhdsdio_pktfree2(bus, pkt);
4063 continue;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004064 }
4065
4066 if ((bus->bus == SPI_BUS) && !bus->usebufpool) {
4067 DHD_ERROR(("Received %d bytes on %d channel. Running out of " "rx pktbuf's or not yet malloced.\n",
4068 len, chan));
4069 continue;
4070 }
4071
4072 /* Validate data offset */
4073 if ((doff < SDPCM_HDRLEN) || (doff > len)) {
4074 DHD_ERROR(("%s (nextlen): bad data offset %d: HW len %d min %d\n",
4075 __func__, doff, len, SDPCM_HDRLEN));
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004076 dhdsdio_rxfail(bus, false, false);
Grant Grundler4b455e02011-05-04 09:59:47 -07004077 dhdsdio_pktfree2(bus, pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004078 continue;
4079 }
4080
4081 /* All done with this one -- now deliver the packet */
4082 goto deliver;
4083 }
4084 /* gSPI frames should not be handled in fractions */
4085 if (bus->bus == SPI_BUS)
4086 break;
4087
4088 /* Read frame header (hardware and software) */
Grant Grundler4b455e02011-05-04 09:59:47 -07004089 sdret = bcmsdh_recv_buf(bus, bcmsdh_cur_sbwad(sdh),
4090 SDIO_FUNC_2, F2SYNC, bus->rxhdr, firstread,
4091 NULL, NULL, NULL);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004092 bus->f2rxhdrs++;
Roland Vossenb7ef2a92011-05-03 11:35:02 +02004093 ASSERT(sdret != -BCME_PENDING);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004094
4095 if (sdret < 0) {
4096 DHD_ERROR(("%s: RXHEADER FAILED: %d\n", __func__,
4097 sdret));
4098 bus->rx_hdrfail++;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004099 dhdsdio_rxfail(bus, true, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004100 continue;
4101 }
4102#ifdef DHD_DEBUG
Arend van Spriel34227312011-05-10 22:25:32 +02004103 if (DHD_BYTES_ON() || DHD_HDRS_ON()) {
4104 printk(KERN_DEBUG "RxHdr:\n");
4105 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
4106 bus->rxhdr, SDPCM_HDRLEN);
4107 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004108#endif
4109
4110 /* Extract hardware header fields */
Stanislav Fomichev56dfe3c2011-02-20 21:43:33 +03004111 len = get_unaligned_le16(bus->rxhdr);
4112 check = get_unaligned_le16(bus->rxhdr + sizeof(u16));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004113
4114 /* All zeros means no more frames */
4115 if (!(len | check)) {
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004116 *finished = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004117 break;
4118 }
4119
4120 /* Validate check bytes */
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07004121 if ((u16) ~(len ^ check)) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004122 DHD_ERROR(("%s: HW hdr err: len/check 0x%04x/0x%04x\n",
4123 __func__, len, check));
4124 bus->rx_badhdr++;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004125 dhdsdio_rxfail(bus, false, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004126 continue;
4127 }
4128
4129 /* Validate frame length */
4130 if (len < SDPCM_HDRLEN) {
4131 DHD_ERROR(("%s: HW hdr length invalid: %d\n",
4132 __func__, len));
4133 continue;
4134 }
4135
4136 /* Extract software header fields */
4137 chan = SDPCM_PACKET_CHANNEL(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
4138 seq = SDPCM_PACKET_SEQUENCE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
4139 doff = SDPCM_DOFFSET_VALUE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
4140 txmax = SDPCM_WINDOW_VALUE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
4141
4142 /* Validate data offset */
4143 if ((doff < SDPCM_HDRLEN) || (doff > len)) {
4144 DHD_ERROR(("%s: Bad data offset %d: HW len %d, min %d "
4145 "seq %d\n",
4146 __func__, doff, len, SDPCM_HDRLEN, seq));
4147 bus->rx_badhdr++;
4148 ASSERT(0);
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004149 dhdsdio_rxfail(bus, false, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004150 continue;
4151 }
4152
4153 /* Save the readahead length if there is one */
4154 bus->nextlen =
4155 bus->rxhdr[SDPCM_FRAMETAG_LEN + SDPCM_NEXTLEN_OFFSET];
4156 if ((bus->nextlen << 4) > MAX_RX_DATASZ) {
4157 DHD_INFO(("%s (nextlen): got frame w/nextlen too large "
4158 "(%d), seq %d\n",
4159 __func__, bus->nextlen, seq));
4160 bus->nextlen = 0;
4161 }
4162
4163 /* Handle Flow Control */
4164 fcbits = SDPCM_FCMASK_VALUE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
4165
Grant Grundler4b455e02011-05-04 09:59:47 -07004166 if (bus->flowcontrol != fcbits) {
4167 if (~bus->flowcontrol & fcbits)
4168 bus->fc_xoff++;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004169
Grant Grundler4b455e02011-05-04 09:59:47 -07004170 if (bus->flowcontrol & ~fcbits)
4171 bus->fc_xon++;
4172
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004173 bus->fc_rcvd++;
4174 bus->flowcontrol = fcbits;
4175 }
4176
4177 /* Check and update sequence number */
4178 if (rxseq != seq) {
4179 DHD_INFO(("%s: rx_seq %d, expected %d\n", __func__,
4180 seq, rxseq));
4181 bus->rx_badseq++;
4182 rxseq = seq;
4183 }
4184
4185 /* Check window for sanity */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004186 if ((u8) (txmax - bus->tx_seq) > 0x40) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004187 DHD_ERROR(("%s: unlikely tx max %d with tx_seq %d\n",
4188 __func__, txmax, bus->tx_seq));
4189 txmax = bus->tx_seq + 2;
4190 }
4191 bus->tx_max = txmax;
4192
4193 /* Call a separate function for control frames */
4194 if (chan == SDPCM_CONTROL_CHANNEL) {
4195 dhdsdio_read_control(bus, bus->rxhdr, len, doff);
4196 continue;
4197 }
4198
4199 ASSERT((chan == SDPCM_DATA_CHANNEL)
4200 || (chan == SDPCM_EVENT_CHANNEL)
4201 || (chan == SDPCM_TEST_CHANNEL)
4202 || (chan == SDPCM_GLOM_CHANNEL));
4203
4204 /* Length to read */
4205 rdlen = (len > firstread) ? (len - firstread) : 0;
4206
4207 /* May pad read to blocksize for efficiency */
4208 if (bus->roundup && bus->blocksize &&
4209 (rdlen > bus->blocksize)) {
4210 pad = bus->blocksize - (rdlen % bus->blocksize);
4211 if ((pad <= bus->roundup) && (pad < bus->blocksize) &&
4212 ((rdlen + pad + firstread) < MAX_RX_DATASZ))
4213 rdlen += pad;
4214 } else if (rdlen % DHD_SDALIGN) {
4215 rdlen += DHD_SDALIGN - (rdlen % DHD_SDALIGN);
4216 }
4217
4218 /* Satisfy length-alignment requirements */
4219 if (forcealign && (rdlen & (ALIGNMENT - 1)))
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07004220 rdlen = roundup(rdlen, ALIGNMENT);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004221
4222 if ((rdlen + firstread) > MAX_RX_DATASZ) {
4223 /* Too long -- skip this frame */
4224 DHD_ERROR(("%s: too long: len %d rdlen %d\n",
4225 __func__, len, rdlen));
4226 bus->dhd->rx_errors++;
4227 bus->rx_toolong++;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004228 dhdsdio_rxfail(bus, false, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004229 continue;
4230 }
4231
Roland Vossen67ad48b2011-06-01 13:45:51 +02004232 pkt = brcmu_pkt_buf_get_skb(rdlen + firstread + DHD_SDALIGN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004233 if (!pkt) {
4234 /* Give up on data, request rtx of events */
Roland Vossen67ad48b2011-06-01 13:45:51 +02004235 DHD_ERROR(("%s: brcmu_pkt_buf_get_skb failed: rdlen %d"
4236 " chan %d\n", __func__, rdlen, chan));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004237 bus->dhd->rx_dropped++;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004238 dhdsdio_rxfail(bus, false, RETRYCHAN(chan));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004239 continue;
4240 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004241
Arend van Spriel54991ad2010-11-23 14:06:24 +01004242 ASSERT(!(pkt->prev));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004243
4244 /* Leave room for what we already read, and align remainder */
Arend van Spriel54991ad2010-11-23 14:06:24 +01004245 ASSERT(firstread < pkt->len);
Arend van Sprielc303ecb2010-11-18 20:46:43 +01004246 skb_pull(pkt, firstread);
Arend van Spriel3c9d4c32011-03-02 21:18:48 +01004247 PKTALIGN(pkt, rdlen, DHD_SDALIGN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004248
4249 /* Read the remaining frame data */
Grant Grundler4b455e02011-05-04 09:59:47 -07004250 sdret = bcmsdh_recv_buf(bus, bcmsdh_cur_sbwad(sdh), SDIO_FUNC_2,
Arend van Spriel54991ad2010-11-23 14:06:24 +01004251 F2SYNC, ((u8 *) (pkt->data)), rdlen,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004252 pkt, NULL, NULL);
4253 bus->f2rxdata++;
Roland Vossenb7ef2a92011-05-03 11:35:02 +02004254 ASSERT(sdret != -BCME_PENDING);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004255
4256 if (sdret < 0) {
4257 DHD_ERROR(("%s: read %d %s bytes failed: %d\n",
4258 __func__, rdlen,
4259 ((chan ==
4260 SDPCM_EVENT_CHANNEL) ? "event" : ((chan ==
4261 SDPCM_DATA_CHANNEL)
4262 ? "data" : "test")),
4263 sdret));
Roland Vossen67ad48b2011-06-01 13:45:51 +02004264 brcmu_pkt_buf_free_skb(pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004265 bus->dhd->rx_errors++;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004266 dhdsdio_rxfail(bus, true, RETRYCHAN(chan));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004267 continue;
4268 }
4269
4270 /* Copy the already-read portion */
Arend van Sprielc303ecb2010-11-18 20:46:43 +01004271 skb_push(pkt, firstread);
Stanislav Fomichev02160692011-02-15 01:05:10 +03004272 memcpy(pkt->data, bus->rxhdr, firstread);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004273
4274#ifdef DHD_DEBUG
Arend van Spriel34227312011-05-10 22:25:32 +02004275 if (DHD_BYTES_ON() && DHD_DATA_ON()) {
4276 printk(KERN_DEBUG "Rx Data:\n");
4277 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
4278 pkt->data, len);
4279 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004280#endif
4281
4282deliver:
4283 /* Save superframe descriptor and allocate packet frame */
4284 if (chan == SDPCM_GLOM_CHANNEL) {
4285 if (SDPCM_GLOMDESC(&bus->rxhdr[SDPCM_FRAMETAG_LEN])) {
4286 DHD_GLOM(("%s: glom descriptor, %d bytes:\n",
4287 __func__, len));
4288#ifdef DHD_DEBUG
4289 if (DHD_GLOM_ON()) {
Arend van Spriel34227312011-05-10 22:25:32 +02004290 printk(KERN_DEBUG "Glom Data:\n");
4291 print_hex_dump_bytes("",
4292 DUMP_PREFIX_OFFSET,
4293 pkt->data, len);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004294 }
4295#endif
Arend van Spriel2cb8ada2010-11-18 20:46:44 +01004296 __skb_trim(pkt, len);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004297 ASSERT(doff == SDPCM_HDRLEN);
Arend van Sprielc303ecb2010-11-18 20:46:43 +01004298 skb_pull(pkt, SDPCM_HDRLEN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004299 bus->glomd = pkt;
4300 } else {
4301 DHD_ERROR(("%s: glom superframe w/o "
4302 "descriptor!\n", __func__));
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004303 dhdsdio_rxfail(bus, false, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004304 }
4305 continue;
4306 }
4307
4308 /* Fill in packet len and prio, deliver upward */
Arend van Spriel2cb8ada2010-11-18 20:46:44 +01004309 __skb_trim(pkt, len);
Arend van Sprielc303ecb2010-11-18 20:46:43 +01004310 skb_pull(pkt, doff);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004311
4312#ifdef SDTEST
4313 /* Test channel packets are processed separately */
4314 if (chan == SDPCM_TEST_CHANNEL) {
4315 dhdsdio_testrcv(bus, pkt, seq);
4316 continue;
4317 }
4318#endif /* SDTEST */
4319
Arend van Spriel54991ad2010-11-23 14:06:24 +01004320 if (pkt->len == 0) {
Roland Vossen67ad48b2011-06-01 13:45:51 +02004321 brcmu_pkt_buf_free_skb(pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004322 continue;
4323 } else if (dhd_prot_hdrpull(bus->dhd, &ifidx, pkt) != 0) {
4324 DHD_ERROR(("%s: rx protocol error\n", __func__));
Roland Vossen67ad48b2011-06-01 13:45:51 +02004325 brcmu_pkt_buf_free_skb(pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004326 bus->dhd->rx_errors++;
4327 continue;
4328 }
4329
4330 /* Unlock during rx call */
4331 dhd_os_sdunlock(bus->dhd);
4332 dhd_rx_frame(bus->dhd, ifidx, pkt, 1);
4333 dhd_os_sdlock(bus->dhd);
4334 }
4335 rxcount = maxframes - rxleft;
4336#ifdef DHD_DEBUG
4337 /* Message if we hit the limit */
4338 if (!rxleft && !sdtest)
4339 DHD_DATA(("%s: hit rx limit of %d frames\n", __func__,
4340 maxframes));
4341 else
4342#endif /* DHD_DEBUG */
4343 DHD_DATA(("%s: processed %d frames\n", __func__, rxcount));
4344 /* Back off rxseq if awaiting rtx, update rx_seq */
4345 if (bus->rxskip)
4346 rxseq--;
4347 bus->rx_seq = rxseq;
4348
4349 return rxcount;
4350}
4351
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07004352static u32 dhdsdio_hostmail(dhd_bus_t *bus)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004353{
Franky Lin597600a2011-06-01 13:45:39 +02004354 struct sdpcmd_regs *regs = bus->regs;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07004355 u32 intstatus = 0;
4356 u32 hmb_data;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004357 u8 fcbits;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004358 uint retries = 0;
4359
4360 DHD_TRACE(("%s: Enter\n", __func__));
4361
4362 /* Read mailbox data and ack that we did so */
4363 R_SDREG(hmb_data, &regs->tohostmailboxdata, retries);
4364 if (retries <= retry_limit)
4365 W_SDREG(SMB_INT_ACK, &regs->tosbmailbox, retries);
4366 bus->f1regdata += 2;
4367
4368 /* Dongle recomposed rx frames, accept them again */
4369 if (hmb_data & HMB_DATA_NAKHANDLED) {
4370 DHD_INFO(("Dongle reports NAK handled, expect rtx of %d\n",
4371 bus->rx_seq));
4372 if (!bus->rxskip)
4373 DHD_ERROR(("%s: unexpected NAKHANDLED!\n", __func__));
4374
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004375 bus->rxskip = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004376 intstatus |= I_HMB_FRAME_IND;
4377 }
4378
4379 /*
4380 * DEVREADY does not occur with gSPI.
4381 */
4382 if (hmb_data & (HMB_DATA_DEVREADY | HMB_DATA_FWREADY)) {
4383 bus->sdpcm_ver =
4384 (hmb_data & HMB_DATA_VERSION_MASK) >>
4385 HMB_DATA_VERSION_SHIFT;
4386 if (bus->sdpcm_ver != SDPCM_PROT_VERSION)
4387 DHD_ERROR(("Version mismatch, dongle reports %d, "
4388 "expecting %d\n",
4389 bus->sdpcm_ver, SDPCM_PROT_VERSION));
4390 else
4391 DHD_INFO(("Dongle ready, protocol version %d\n",
4392 bus->sdpcm_ver));
4393 }
4394
4395 /*
4396 * Flow Control has been moved into the RX headers and this out of band
Grant Grundler4b455e02011-05-04 09:59:47 -07004397 * method isn't used any more.
4398 * remaining backward compatible with older dongles.
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004399 */
4400 if (hmb_data & HMB_DATA_FC) {
Grant Grundler4b455e02011-05-04 09:59:47 -07004401 fcbits = (hmb_data & HMB_DATA_FCDATA_MASK) >>
4402 HMB_DATA_FCDATA_SHIFT;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004403
4404 if (fcbits & ~bus->flowcontrol)
4405 bus->fc_xoff++;
Grant Grundler4b455e02011-05-04 09:59:47 -07004406
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004407 if (bus->flowcontrol & ~fcbits)
4408 bus->fc_xon++;
4409
4410 bus->fc_rcvd++;
4411 bus->flowcontrol = fcbits;
4412 }
4413
4414 /* Shouldn't be any others */
4415 if (hmb_data & ~(HMB_DATA_DEVREADY |
4416 HMB_DATA_NAKHANDLED |
4417 HMB_DATA_FC |
4418 HMB_DATA_FWREADY |
4419 HMB_DATA_FCDATA_MASK | HMB_DATA_VERSION_MASK)) {
4420 DHD_ERROR(("Unknown mailbox data content: 0x%02x\n", hmb_data));
4421 }
4422
4423 return intstatus;
4424}
4425
4426bool dhdsdio_dpc(dhd_bus_t *bus)
4427{
4428 bcmsdh_info_t *sdh = bus->sdh;
Franky Lin597600a2011-06-01 13:45:39 +02004429 struct sdpcmd_regs *regs = bus->regs;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07004430 u32 intstatus, newstatus = 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004431 uint retries = 0;
4432 uint rxlimit = dhd_rxbound; /* Rx frames to read before resched */
4433 uint txlimit = dhd_txbound; /* Tx frames to send before resched */
4434 uint framecnt = 0; /* Temporary counter of tx/rx frames */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004435 bool rxdone = true; /* Flag for no more read data */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004436 bool resched = false; /* Flag indicating resched wanted */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004437
4438 DHD_TRACE(("%s: Enter\n", __func__));
4439
4440 /* Start with leftover status bits */
4441 intstatus = bus->intstatus;
4442
4443 dhd_os_sdlock(bus->dhd);
4444
4445 /* If waiting for HTAVAIL, check status */
4446 if (bus->clkstate == CLK_PENDING) {
4447 int err;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004448 u8 clkctl, devctl = 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004449
4450#ifdef DHD_DEBUG
4451 /* Check for inconsistent device control */
4452 devctl =
4453 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL, &err);
4454 if (err) {
4455 DHD_ERROR(("%s: error reading DEVCTL: %d\n",
4456 __func__, err));
4457 bus->dhd->busstate = DHD_BUS_DOWN;
4458 } else {
4459 ASSERT(devctl & SBSDIO_DEVCTL_CA_INT_ONLY);
4460 }
4461#endif /* DHD_DEBUG */
4462
4463 /* Read CSR, if clock on switch to AVAIL, else ignore */
4464 clkctl =
4465 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
4466 &err);
4467 if (err) {
4468 DHD_ERROR(("%s: error reading CSR: %d\n", __func__,
4469 err));
4470 bus->dhd->busstate = DHD_BUS_DOWN;
4471 }
4472
4473 DHD_INFO(("DPC: PENDING, devctl 0x%02x clkctl 0x%02x\n", devctl,
4474 clkctl));
4475
4476 if (SBSDIO_HTAV(clkctl)) {
4477 devctl =
4478 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
4479 &err);
4480 if (err) {
4481 DHD_ERROR(("%s: error reading DEVCTL: %d\n",
4482 __func__, err));
4483 bus->dhd->busstate = DHD_BUS_DOWN;
4484 }
4485 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
4486 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
4487 devctl, &err);
4488 if (err) {
4489 DHD_ERROR(("%s: error writing DEVCTL: %d\n",
4490 __func__, err));
4491 bus->dhd->busstate = DHD_BUS_DOWN;
4492 }
4493 bus->clkstate = CLK_AVAIL;
4494 } else {
4495 goto clkwait;
4496 }
4497 }
4498
4499 BUS_WAKE(bus);
4500
4501 /* Make sure backplane clock is on */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004502 dhdsdio_clkctl(bus, CLK_AVAIL, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004503 if (bus->clkstate == CLK_PENDING)
4504 goto clkwait;
4505
4506 /* Pending interrupt indicates new device status */
4507 if (bus->ipend) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004508 bus->ipend = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004509 R_SDREG(newstatus, &regs->intstatus, retries);
4510 bus->f1regdata++;
4511 if (bcmsdh_regfail(bus->sdh))
4512 newstatus = 0;
4513 newstatus &= bus->hostintmask;
4514 bus->fcstate = !!(newstatus & I_HMB_FC_STATE);
4515 if (newstatus) {
4516 W_SDREG(newstatus, &regs->intstatus, retries);
4517 bus->f1regdata++;
4518 }
4519 }
4520
4521 /* Merge new bits with previous */
4522 intstatus |= newstatus;
4523 bus->intstatus = 0;
4524
4525 /* Handle flow-control change: read new state in case our ack
4526 * crossed another change interrupt. If change still set, assume
4527 * FC ON for safety, let next loop through do the debounce.
4528 */
4529 if (intstatus & I_HMB_FC_CHANGE) {
4530 intstatus &= ~I_HMB_FC_CHANGE;
4531 W_SDREG(I_HMB_FC_CHANGE, &regs->intstatus, retries);
4532 R_SDREG(newstatus, &regs->intstatus, retries);
4533 bus->f1regdata += 2;
4534 bus->fcstate =
4535 !!(newstatus & (I_HMB_FC_STATE | I_HMB_FC_CHANGE));
4536 intstatus |= (newstatus & bus->hostintmask);
4537 }
4538
4539 /* Handle host mailbox indication */
4540 if (intstatus & I_HMB_HOST_INT) {
4541 intstatus &= ~I_HMB_HOST_INT;
4542 intstatus |= dhdsdio_hostmail(bus);
4543 }
4544
4545 /* Generally don't ask for these, can get CRC errors... */
4546 if (intstatus & I_WR_OOSYNC) {
4547 DHD_ERROR(("Dongle reports WR_OOSYNC\n"));
4548 intstatus &= ~I_WR_OOSYNC;
4549 }
4550
4551 if (intstatus & I_RD_OOSYNC) {
4552 DHD_ERROR(("Dongle reports RD_OOSYNC\n"));
4553 intstatus &= ~I_RD_OOSYNC;
4554 }
4555
4556 if (intstatus & I_SBINT) {
4557 DHD_ERROR(("Dongle reports SBINT\n"));
4558 intstatus &= ~I_SBINT;
4559 }
4560
4561 /* Would be active due to wake-wlan in gSPI */
4562 if (intstatus & I_CHIPACTIVE) {
4563 DHD_INFO(("Dongle reports CHIPACTIVE\n"));
4564 intstatus &= ~I_CHIPACTIVE;
4565 }
4566
4567 /* Ignore frame indications if rxskip is set */
4568 if (bus->rxskip)
4569 intstatus &= ~I_HMB_FRAME_IND;
4570
4571 /* On frame indication, read available frames */
4572 if (PKT_AVAILABLE()) {
4573 framecnt = dhdsdio_readframes(bus, rxlimit, &rxdone);
4574 if (rxdone || bus->rxskip)
4575 intstatus &= ~I_HMB_FRAME_IND;
Greg Kroah-Hartman7068c2f2010-10-08 11:34:59 -07004576 rxlimit -= min(framecnt, rxlimit);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004577 }
4578
4579 /* Keep still-pending events for next scheduling */
4580 bus->intstatus = intstatus;
4581
4582clkwait:
4583#if defined(OOB_INTR_ONLY)
4584 bcmsdh_oob_intr_set(1);
4585#endif /* (OOB_INTR_ONLY) */
4586 /* Re-enable interrupts to detect new device events (mailbox, rx frame)
4587 * or clock availability. (Allows tx loop to check ipend if desired.)
4588 * (Unless register access seems hosed, as we may not be able to ACK...)
4589 */
4590 if (bus->intr && bus->intdis && !bcmsdh_regfail(sdh)) {
4591 DHD_INTR(("%s: enable SDIO interrupts, rxdone %d framecnt %d\n",
4592 __func__, rxdone, framecnt));
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004593 bus->intdis = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004594 bcmsdh_intr_enable(sdh);
4595 }
4596
4597 if (DATAOK(bus) && bus->ctrl_frame_stat &&
4598 (bus->clkstate == CLK_AVAIL)) {
4599 int ret, i;
4600
4601 ret =
4602 dhd_bcmsdh_send_buf(bus, bcmsdh_cur_sbwad(sdh), SDIO_FUNC_2,
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004603 F2SYNC, (u8 *) bus->ctrl_frame_buf,
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07004604 (u32) bus->ctrl_frame_len, NULL,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004605 NULL, NULL);
Roland Vossenb7ef2a92011-05-03 11:35:02 +02004606 ASSERT(ret != -BCME_PENDING);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004607
4608 if (ret < 0) {
4609 /* On failure, abort the command and
4610 terminate the frame */
4611 DHD_INFO(("%s: sdio error %d, abort command and "
4612 "terminate frame.\n", __func__, ret));
4613 bus->tx_sderrs++;
4614
4615 bcmsdh_abort(sdh, SDIO_FUNC_2);
4616
4617 bcmsdh_cfg_write(sdh, SDIO_FUNC_1,
4618 SBSDIO_FUNC1_FRAMECTRL, SFC_WF_TERM,
4619 NULL);
4620 bus->f1regdata++;
4621
4622 for (i = 0; i < 3; i++) {
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004623 u8 hi, lo;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004624 hi = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
4625 SBSDIO_FUNC1_WFRAMEBCHI,
4626 NULL);
4627 lo = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
4628 SBSDIO_FUNC1_WFRAMEBCLO,
4629 NULL);
4630 bus->f1regdata += 2;
4631 if ((hi == 0) && (lo == 0))
4632 break;
4633 }
4634
4635 }
4636 if (ret == 0)
4637 bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQUENCE_WRAP;
4638
Arend van Spriel0bef7742011-02-10 12:03:44 +01004639 DHD_INFO(("Return_dpc value is : %d\n", ret));
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004640 bus->ctrl_frame_stat = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004641 dhd_wait_event_wakeup(bus->dhd);
4642 }
4643 /* Send queued frames (limit 1 if rx may still be pending) */
4644 else if ((bus->clkstate == CLK_AVAIL) && !bus->fcstate &&
Roland Vossen67ad48b2011-06-01 13:45:51 +02004645 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) && txlimit
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004646 && DATAOK(bus)) {
Greg Kroah-Hartman7068c2f2010-10-08 11:34:59 -07004647 framecnt = rxdone ? txlimit : min(txlimit, dhd_txminmax);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004648 framecnt = dhdsdio_sendfromq(bus, framecnt);
4649 txlimit -= framecnt;
4650 }
4651
4652 /* Resched if events or tx frames are pending,
4653 else await next interrupt */
4654 /* On failed register access, all bets are off:
4655 no resched or interrupts */
4656 if ((bus->dhd->busstate == DHD_BUS_DOWN) || bcmsdh_regfail(sdh)) {
4657 DHD_ERROR(("%s: failed backplane access over SDIO, halting "
4658 "operation %d\n", __func__, bcmsdh_regfail(sdh)));
4659 bus->dhd->busstate = DHD_BUS_DOWN;
4660 bus->intstatus = 0;
4661 } else if (bus->clkstate == CLK_PENDING) {
4662 DHD_INFO(("%s: rescheduled due to CLK_PENDING awaiting "
4663 "I_CHIPACTIVE interrupt\n", __func__));
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004664 resched = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004665 } else if (bus->intstatus || bus->ipend ||
Roland Vossen67ad48b2011-06-01 13:45:51 +02004666 (!bus->fcstate && brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol)
4667 && DATAOK(bus)) || PKT_AVAILABLE()) {
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004668 resched = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004669 }
4670
4671 bus->dpc_sched = resched;
4672
4673 /* If we're done for now, turn off clock request. */
4674 if ((bus->clkstate != CLK_PENDING)
4675 && bus->idletime == DHD_IDLE_IMMEDIATE) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004676 bus->activity = false;
4677 dhdsdio_clkctl(bus, CLK_NONE, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004678 }
4679
4680 dhd_os_sdunlock(bus->dhd);
4681
4682 return resched;
4683}
4684
4685bool dhd_bus_dpc(struct dhd_bus *bus)
4686{
4687 bool resched;
4688
4689 /* Call the DPC directly. */
4690 DHD_TRACE(("Calling dhdsdio_dpc() from %s\n", __func__));
4691 resched = dhdsdio_dpc(bus);
4692
4693 return resched;
4694}
4695
4696void dhdsdio_isr(void *arg)
4697{
4698 dhd_bus_t *bus = (dhd_bus_t *) arg;
4699 bcmsdh_info_t *sdh;
4700
4701 DHD_TRACE(("%s: Enter\n", __func__));
4702
4703 if (!bus) {
4704 DHD_ERROR(("%s : bus is null pointer , exit\n", __func__));
4705 return;
4706 }
4707 sdh = bus->sdh;
4708
4709 if (bus->dhd->busstate == DHD_BUS_DOWN) {
4710 DHD_ERROR(("%s : bus is down. we have nothing to do\n",
4711 __func__));
4712 return;
4713 }
4714 /* Count the interrupt call */
4715 bus->intrcount++;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004716 bus->ipend = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004717
4718 /* Shouldn't get this interrupt if we're sleeping? */
4719 if (bus->sleeping) {
4720 DHD_ERROR(("INTERRUPT WHILE SLEEPING??\n"));
4721 return;
4722 }
4723
4724 /* Disable additional interrupts (is this needed now)? */
4725 if (bus->intr)
4726 DHD_INTR(("%s: disable SDIO interrupts\n", __func__));
4727 else
4728 DHD_ERROR(("dhdsdio_isr() w/o interrupt configured!\n"));
4729
4730 bcmsdh_intr_disable(sdh);
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004731 bus->intdis = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004732
4733#if defined(SDIO_ISR_THREAD)
4734 DHD_TRACE(("Calling dhdsdio_dpc() from %s\n", __func__));
4735 while (dhdsdio_dpc(bus))
4736 ;
4737#else
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004738 bus->dpc_sched = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004739 dhd_sched_dpc(bus->dhd);
4740#endif
4741
4742}
4743
4744#ifdef SDTEST
4745static void dhdsdio_pktgen_init(dhd_bus_t *bus)
4746{
4747 /* Default to specified length, or full range */
4748 if (dhd_pktgen_len) {
Greg Kroah-Hartman7068c2f2010-10-08 11:34:59 -07004749 bus->pktgen_maxlen = min(dhd_pktgen_len, MAX_PKTGEN_LEN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004750 bus->pktgen_minlen = bus->pktgen_maxlen;
4751 } else {
4752 bus->pktgen_maxlen = MAX_PKTGEN_LEN;
4753 bus->pktgen_minlen = 0;
4754 }
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07004755 bus->pktgen_len = (u16) bus->pktgen_minlen;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004756
4757 /* Default to per-watchdog burst with 10s print time */
4758 bus->pktgen_freq = 1;
4759 bus->pktgen_print = 10000 / dhd_watchdog_ms;
4760 bus->pktgen_count = (dhd_pktgen * dhd_watchdog_ms + 999) / 1000;
4761
4762 /* Default to echo mode */
4763 bus->pktgen_mode = DHD_PKTGEN_ECHO;
4764 bus->pktgen_stop = 1;
4765}
4766
4767static void dhdsdio_pktgen(dhd_bus_t *bus)
4768{
Arend van Sprielc26b1372010-11-23 14:06:23 +01004769 struct sk_buff *pkt;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004770 u8 *data;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004771 uint pktcount;
4772 uint fillbyte;
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07004773 u16 len;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004774
4775 /* Display current count if appropriate */
4776 if (bus->pktgen_print && (++bus->pktgen_ptick >= bus->pktgen_print)) {
4777 bus->pktgen_ptick = 0;
Arend van Spriel0bef7742011-02-10 12:03:44 +01004778 printk(KERN_DEBUG "%s: send attempts %d rcvd %d\n",
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004779 __func__, bus->pktgen_sent, bus->pktgen_rcvd);
4780 }
4781
4782 /* For recv mode, just make sure dongle has started sending */
4783 if (bus->pktgen_mode == DHD_PKTGEN_RECV) {
4784 if (!bus->pktgen_rcvd)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004785 dhdsdio_sdtest_set(bus, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004786 return;
4787 }
4788
4789 /* Otherwise, generate or request the specified number of packets */
4790 for (pktcount = 0; pktcount < bus->pktgen_count; pktcount++) {
4791 /* Stop if total has been reached */
4792 if (bus->pktgen_total
4793 && (bus->pktgen_sent >= bus->pktgen_total)) {
4794 bus->pktgen_count = 0;
4795 break;
4796 }
4797
4798 /* Allocate an appropriate-sized packet */
4799 len = bus->pktgen_len;
Roland Vossen67ad48b2011-06-01 13:45:51 +02004800 pkt = brcmu_pkt_buf_get_skb(
Jason Cooper9b890322010-09-30 15:15:39 -04004801 (len + SDPCM_HDRLEN + SDPCM_TEST_HDRLEN + DHD_SDALIGN),
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004802 true);
Jason Cooper9b890322010-09-30 15:15:39 -04004803 if (!pkt) {
Roland Vossen67ad48b2011-06-01 13:45:51 +02004804 DHD_ERROR(("%s: brcmu_pkt_buf_get_skb failed!\n",
4805 __func__));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004806 break;
4807 }
Arend van Spriel3c9d4c32011-03-02 21:18:48 +01004808 PKTALIGN(pkt, (len + SDPCM_HDRLEN + SDPCM_TEST_HDRLEN),
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004809 DHD_SDALIGN);
Arend van Spriel54991ad2010-11-23 14:06:24 +01004810 data = (u8 *) (pkt->data) + SDPCM_HDRLEN;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004811
4812 /* Write test header cmd and extra based on mode */
4813 switch (bus->pktgen_mode) {
4814 case DHD_PKTGEN_ECHO:
4815 *data++ = SDPCM_TEST_ECHOREQ;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004816 *data++ = (u8) bus->pktgen_sent;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004817 break;
4818
4819 case DHD_PKTGEN_SEND:
4820 *data++ = SDPCM_TEST_DISCARD;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004821 *data++ = (u8) bus->pktgen_sent;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004822 break;
4823
4824 case DHD_PKTGEN_RXBURST:
4825 *data++ = SDPCM_TEST_BURST;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004826 *data++ = (u8) bus->pktgen_count;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004827 break;
4828
4829 default:
4830 DHD_ERROR(("Unrecognized pktgen mode %d\n",
4831 bus->pktgen_mode));
Roland Vossen67ad48b2011-06-01 13:45:51 +02004832 brcmu_pkt_buf_free_skb(pkt, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004833 bus->pktgen_count = 0;
4834 return;
4835 }
4836
4837 /* Write test header length field */
4838 *data++ = (len >> 0);
4839 *data++ = (len >> 8);
4840
4841 /* Then fill in the remainder -- N/A for burst,
4842 but who cares... */
4843 for (fillbyte = 0; fillbyte < len; fillbyte++)
4844 *data++ =
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004845 SDPCM_TEST_FILL(fillbyte, (u8) bus->pktgen_sent);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004846
4847#ifdef DHD_DEBUG
4848 if (DHD_BYTES_ON() && DHD_DATA_ON()) {
Arend van Spriel54991ad2010-11-23 14:06:24 +01004849 data = (u8 *) (pkt->data) + SDPCM_HDRLEN;
Arend van Spriel34227312011-05-10 22:25:32 +02004850 printk(KERN_DEBUG "dhdsdio_pktgen: Tx Data:\n");
4851 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, data,
4852 pkt->len - SDPCM_HDRLEN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004853 }
4854#endif
4855
4856 /* Send it */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004857 if (dhdsdio_txpkt(bus, pkt, SDPCM_TEST_CHANNEL, true)) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004858 bus->pktgen_fail++;
4859 if (bus->pktgen_stop
4860 && bus->pktgen_stop == bus->pktgen_fail)
4861 bus->pktgen_count = 0;
4862 }
4863 bus->pktgen_sent++;
4864
4865 /* Bump length if not fixed, wrap at max */
4866 if (++bus->pktgen_len > bus->pktgen_maxlen)
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07004867 bus->pktgen_len = (u16) bus->pktgen_minlen;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004868
4869 /* Special case for burst mode: just send one request! */
4870 if (bus->pktgen_mode == DHD_PKTGEN_RXBURST)
4871 break;
4872 }
4873}
4874
4875static void dhdsdio_sdtest_set(dhd_bus_t *bus, bool start)
4876{
Arend van Sprielc26b1372010-11-23 14:06:23 +01004877 struct sk_buff *pkt;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004878 u8 *data;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004879
4880 /* Allocate the packet */
Roland Vossen67ad48b2011-06-01 13:45:51 +02004881 pkt = brcmu_pkt_buf_get_skb(SDPCM_HDRLEN + SDPCM_TEST_HDRLEN +
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004882 DHD_SDALIGN, true);
Jason Cooper9b890322010-09-30 15:15:39 -04004883 if (!pkt) {
Roland Vossen67ad48b2011-06-01 13:45:51 +02004884 DHD_ERROR(("%s: brcmu_pkt_buf_get_skb failed!\n", __func__));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004885 return;
4886 }
Arend van Spriel3c9d4c32011-03-02 21:18:48 +01004887 PKTALIGN(pkt, (SDPCM_HDRLEN + SDPCM_TEST_HDRLEN), DHD_SDALIGN);
Arend van Spriel54991ad2010-11-23 14:06:24 +01004888 data = (u8 *) (pkt->data) + SDPCM_HDRLEN;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004889
4890 /* Fill in the test header */
4891 *data++ = SDPCM_TEST_SEND;
4892 *data++ = start;
4893 *data++ = (bus->pktgen_maxlen >> 0);
4894 *data++ = (bus->pktgen_maxlen >> 8);
4895
4896 /* Send it */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004897 if (dhdsdio_txpkt(bus, pkt, SDPCM_TEST_CHANNEL, true))
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004898 bus->pktgen_fail++;
4899}
4900
Arend van Sprielc26b1372010-11-23 14:06:23 +01004901static void dhdsdio_testrcv(dhd_bus_t *bus, struct sk_buff *pkt, uint seq)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004902{
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004903 u8 *data;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004904 uint pktlen;
4905
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004906 u8 cmd;
4907 u8 extra;
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07004908 u16 len;
4909 u16 offset;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004910
4911 /* Check for min length */
Arend van Spriel54991ad2010-11-23 14:06:24 +01004912 pktlen = pkt->len;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004913 if (pktlen < SDPCM_TEST_HDRLEN) {
4914 DHD_ERROR(("dhdsdio_restrcv: toss runt frame, pktlen %d\n",
4915 pktlen));
Roland Vossen67ad48b2011-06-01 13:45:51 +02004916 brcmu_pkt_buf_free_skb(pkt, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004917 return;
4918 }
4919
4920 /* Extract header fields */
Arend van Spriel54991ad2010-11-23 14:06:24 +01004921 data = pkt->data;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004922 cmd = *data++;
4923 extra = *data++;
4924 len = *data++;
4925 len += *data++ << 8;
4926
4927 /* Check length for relevant commands */
4928 if (cmd == SDPCM_TEST_DISCARD || cmd == SDPCM_TEST_ECHOREQ
4929 || cmd == SDPCM_TEST_ECHORSP) {
4930 if (pktlen != len + SDPCM_TEST_HDRLEN) {
4931 DHD_ERROR(("dhdsdio_testrcv: frame length mismatch, "
4932 "pktlen %d seq %d" " cmd %d extra %d len %d\n",
4933 pktlen, seq, cmd, extra, len));
Roland Vossen67ad48b2011-06-01 13:45:51 +02004934 brcmu_pkt_buf_free_skb(pkt, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004935 return;
4936 }
4937 }
4938
4939 /* Process as per command */
4940 switch (cmd) {
4941 case SDPCM_TEST_ECHOREQ:
4942 /* Rx->Tx turnaround ok (even on NDIS w/current
4943 implementation) */
Arend van Spriel54991ad2010-11-23 14:06:24 +01004944 *(u8 *) (pkt->data) = SDPCM_TEST_ECHORSP;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004945 if (dhdsdio_txpkt(bus, pkt, SDPCM_TEST_CHANNEL, true) == 0) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004946 bus->pktgen_sent++;
4947 } else {
4948 bus->pktgen_fail++;
Roland Vossen67ad48b2011-06-01 13:45:51 +02004949 brcmu_pkt_buf_free_skb(pkt, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004950 }
4951 bus->pktgen_rcvd++;
4952 break;
4953
4954 case SDPCM_TEST_ECHORSP:
4955 if (bus->ext_loop) {
Roland Vossen67ad48b2011-06-01 13:45:51 +02004956 brcmu_pkt_buf_free_skb(pkt, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004957 bus->pktgen_rcvd++;
4958 break;
4959 }
4960
4961 for (offset = 0; offset < len; offset++, data++) {
4962 if (*data != SDPCM_TEST_FILL(offset, extra)) {
4963 DHD_ERROR(("dhdsdio_testrcv: echo data mismatch: " "offset %d (len %d) expect 0x%02x rcvd 0x%02x\n",
4964 offset, len,
4965 SDPCM_TEST_FILL(offset, extra), *data));
4966 break;
4967 }
4968 }
Roland Vossen67ad48b2011-06-01 13:45:51 +02004969 brcmu_pkt_buf_free_skb(pkt, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004970 bus->pktgen_rcvd++;
4971 break;
4972
4973 case SDPCM_TEST_DISCARD:
Roland Vossen67ad48b2011-06-01 13:45:51 +02004974 brcmu_pkt_buf_free_skb(pkt, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004975 bus->pktgen_rcvd++;
4976 break;
4977
4978 case SDPCM_TEST_BURST:
4979 case SDPCM_TEST_SEND:
4980 default:
4981 DHD_INFO(("dhdsdio_testrcv: unsupported or unknown command, "
4982 "pktlen %d seq %d" " cmd %d extra %d len %d\n",
4983 pktlen, seq, cmd, extra, len));
Roland Vossen67ad48b2011-06-01 13:45:51 +02004984 brcmu_pkt_buf_free_skb(pkt, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004985 break;
4986 }
4987
4988 /* For recv mode, stop at limie (and tell dongle to stop sending) */
4989 if (bus->pktgen_mode == DHD_PKTGEN_RECV) {
4990 if (bus->pktgen_total
4991 && (bus->pktgen_rcvd >= bus->pktgen_total)) {
4992 bus->pktgen_count = 0;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004993 dhdsdio_sdtest_set(bus, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004994 }
4995 }
4996}
4997#endif /* SDTEST */
4998
4999extern bool dhd_bus_watchdog(dhd_pub_t *dhdp)
5000{
5001 dhd_bus_t *bus;
5002
5003 DHD_TIMER(("%s: Enter\n", __func__));
5004
5005 bus = dhdp->bus;
5006
5007 if (bus->dhd->dongle_reset)
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005008 return false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005009
5010 /* Ignore the timer if simulating bus down */
5011 if (bus->sleeping)
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005012 return false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005013
5014 dhd_os_sdlock(bus->dhd);
5015
5016 /* Poll period: check device if appropriate. */
5017 if (bus->poll && (++bus->polltick >= bus->pollrate)) {
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07005018 u32 intstatus = 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005019
5020 /* Reset poll tick */
5021 bus->polltick = 0;
5022
5023 /* Check device if no interrupts */
5024 if (!bus->intr || (bus->intrcount == bus->lastintrs)) {
5025
5026 if (!bus->dpc_sched) {
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07005027 u8 devpend;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005028 devpend = bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_0,
Franky Lin0df46042011-06-01 13:45:40 +02005029 SDIO_CCCR_INTx,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005030 NULL);
5031 intstatus =
5032 devpend & (INTR_STATUS_FUNC1 |
5033 INTR_STATUS_FUNC2);
5034 }
5035
5036 /* If there is something, make like the ISR and
5037 schedule the DPC */
5038 if (intstatus) {
5039 bus->pollcnt++;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005040 bus->ipend = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005041 if (bus->intr)
5042 bcmsdh_intr_disable(bus->sdh);
5043
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005044 bus->dpc_sched = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005045 dhd_sched_dpc(bus->dhd);
5046
5047 }
5048 }
5049
5050 /* Update interrupt tracking */
5051 bus->lastintrs = bus->intrcount;
5052 }
5053#ifdef DHD_DEBUG
5054 /* Poll for console output periodically */
5055 if (dhdp->busstate == DHD_BUS_DATA && dhd_console_ms != 0) {
5056 bus->console.count += dhd_watchdog_ms;
5057 if (bus->console.count >= dhd_console_ms) {
5058 bus->console.count -= dhd_console_ms;
5059 /* Make sure backplane clock is on */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005060 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005061 if (dhdsdio_readconsole(bus) < 0)
5062 dhd_console_ms = 0; /* On error,
5063 stop trying */
5064 }
5065 }
5066#endif /* DHD_DEBUG */
5067
5068#ifdef SDTEST
5069 /* Generate packets if configured */
5070 if (bus->pktgen_count && (++bus->pktgen_tick >= bus->pktgen_freq)) {
5071 /* Make sure backplane clock is on */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005072 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005073 bus->pktgen_tick = 0;
5074 dhdsdio_pktgen(bus);
5075 }
5076#endif
5077
5078 /* On idle timeout clear activity flag and/or turn off clock */
5079 if ((bus->idletime > 0) && (bus->clkstate == CLK_AVAIL)) {
5080 if (++bus->idlecount >= bus->idletime) {
5081 bus->idlecount = 0;
5082 if (bus->activity) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005083 bus->activity = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005084 dhd_os_wd_timer(bus->dhd, dhd_watchdog_ms);
5085 } else {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005086 dhdsdio_clkctl(bus, CLK_NONE, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005087 }
5088 }
5089 }
5090
5091 dhd_os_sdunlock(bus->dhd);
5092
5093 return bus->ipend;
5094}
5095
5096#ifdef DHD_DEBUG
Greg Kroah-Hartman580a0bd2010-10-05 11:09:48 -07005097extern int dhd_bus_console_in(dhd_pub_t *dhdp, unsigned char *msg, uint msglen)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005098{
5099 dhd_bus_t *bus = dhdp->bus;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07005100 u32 addr, val;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005101 int rv;
Arend van Sprielc26b1372010-11-23 14:06:23 +01005102 struct sk_buff *pkt;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005103
5104 /* Address could be zero if CONSOLE := 0 in dongle Makefile */
5105 if (bus->console_addr == 0)
Roland Vossene10d82d2011-05-03 11:35:19 +02005106 return -ENOTSUPP;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005107
5108 /* Exclusive bus access */
5109 dhd_os_sdlock(bus->dhd);
5110
5111 /* Don't allow input if dongle is in reset */
5112 if (bus->dhd->dongle_reset) {
5113 dhd_os_sdunlock(bus->dhd);
Roland Vossenb74ac122011-05-03 11:35:20 +02005114 return -EPERM;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005115 }
5116
5117 /* Request clock to allow SDIO accesses */
5118 BUS_WAKE(bus);
5119 /* No pend allowed since txpkt is called later, ht clk has to be on */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005120 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005121
5122 /* Zero cbuf_index */
Roland Vossen70963f92011-06-01 13:45:08 +02005123 addr = bus->console_addr + offsetof(rte_cons_t, cbuf_idx);
Stanislav Fomichev628f10b2011-02-20 21:43:32 +03005124 val = cpu_to_le32(0);
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005125 rv = dhdsdio_membytes(bus, true, addr, (u8 *)&val, sizeof(val));
Jason Cooper9b890322010-09-30 15:15:39 -04005126 if (rv < 0)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005127 goto done;
5128
5129 /* Write message into cbuf */
Roland Vossen70963f92011-06-01 13:45:08 +02005130 addr = bus->console_addr + offsetof(rte_cons_t, cbuf);
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005131 rv = dhdsdio_membytes(bus, true, addr, (u8 *)msg, msglen);
Jason Cooper9b890322010-09-30 15:15:39 -04005132 if (rv < 0)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005133 goto done;
5134
5135 /* Write length into vcons_in */
Roland Vossen70963f92011-06-01 13:45:08 +02005136 addr = bus->console_addr + offsetof(rte_cons_t, vcons_in);
Stanislav Fomichev628f10b2011-02-20 21:43:32 +03005137 val = cpu_to_le32(msglen);
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005138 rv = dhdsdio_membytes(bus, true, addr, (u8 *)&val, sizeof(val));
Jason Cooper9b890322010-09-30 15:15:39 -04005139 if (rv < 0)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005140 goto done;
5141
5142 /* Bump dongle by sending an empty event pkt.
5143 * sdpcm_sendup (RX) checks for virtual console input.
5144 */
Roland Vossen67ad48b2011-06-01 13:45:51 +02005145 pkt = brcmu_pkt_buf_get_skb(4 + SDPCM_RESERVE);
Jason Cooper9b890322010-09-30 15:15:39 -04005146 if ((pkt != NULL) && bus->clkstate == CLK_AVAIL)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005147 dhdsdio_txpkt(bus, pkt, SDPCM_EVENT_CHANNEL, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005148
5149done:
5150 if ((bus->idletime == DHD_IDLE_IMMEDIATE) && !bus->dpc_sched) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005151 bus->activity = false;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005152 dhdsdio_clkctl(bus, CLK_NONE, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005153 }
5154
5155 dhd_os_sdunlock(bus->dhd);
5156
5157 return rv;
5158}
5159#endif /* DHD_DEBUG */
5160
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07005161static bool dhdsdio_chipmatch(u16 chipid)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005162{
5163 if (chipid == BCM4325_CHIP_ID)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005164 return true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005165 if (chipid == BCM4329_CHIP_ID)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005166 return true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005167 if (chipid == BCM4319_CHIP_ID)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005168 return true;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005169 return false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005170}
5171
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07005172static void *dhdsdio_probe(u16 venid, u16 devid, u16 bus_no,
5173 u16 slot, u16 func, uint bustype, void *regsva,
Arend van Spriel3c9d4c32011-03-02 21:18:48 +01005174 void *sdh)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005175{
5176 int ret;
5177 dhd_bus_t *bus;
5178
5179 /* Init global variables at run-time, not as part of the declaration.
5180 * This is required to support init/de-init of the driver.
5181 * Initialization
5182 * of globals as part of the declaration results in non-deterministic
5183 * behavior since the value of the globals may be different on the
5184 * first time that the driver is initialized vs subsequent
5185 * initializations.
5186 */
5187 dhd_txbound = DHD_TXBOUND;
5188 dhd_rxbound = DHD_RXBOUND;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005189 dhd_alignctl = true;
5190 sd1idle = true;
5191 dhd_readahead = true;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005192 retrydata = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005193 dhd_dongle_memsize = 0;
5194 dhd_txminmax = DHD_TXMINMAX;
5195
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005196 forcealign = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005197
5198 dhd_common_init();
5199
5200 DHD_TRACE(("%s: Enter\n", __func__));
5201 DHD_INFO(("%s: venid 0x%04x devid 0x%04x\n", __func__, venid, devid));
5202
5203 /* We make assumptions about address window mappings */
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07005204 ASSERT((unsigned long)regsva == SI_ENUM_BASE);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005205
5206 /* BCMSDH passes venid and devid based on CIS parsing -- but
5207 * low-power start
5208 * means early parse could fail, so here we should get either an ID
5209 * we recognize OR (-1) indicating we must request power first.
5210 */
5211 /* Check the Vendor ID */
5212 switch (venid) {
5213 case 0x0000:
Stanislav Fomichevbe1c09f2011-03-28 01:31:36 +04005214 case PCI_VENDOR_ID_BROADCOM:
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005215 break;
5216 default:
5217 DHD_ERROR(("%s: unknown vendor: 0x%04x\n", __func__, venid));
5218 return NULL;
5219 }
5220
5221 /* Check the Device ID and make sure it's one that we support */
5222 switch (devid) {
5223 case BCM4325_D11DUAL_ID: /* 4325 802.11a/g id */
5224 case BCM4325_D11G_ID: /* 4325 802.11g 2.4Ghz band id */
5225 case BCM4325_D11A_ID: /* 4325 802.11a 5Ghz band id */
5226 DHD_INFO(("%s: found 4325 Dongle\n", __func__));
5227 break;
5228 case BCM4329_D11NDUAL_ID: /* 4329 802.11n dualband device */
5229 case BCM4329_D11N2G_ID: /* 4329 802.11n 2.4G device */
5230 case BCM4329_D11N5G_ID: /* 4329 802.11n 5G device */
5231 case 0x4329:
5232 DHD_INFO(("%s: found 4329 Dongle\n", __func__));
5233 break;
5234 case BCM4319_D11N_ID: /* 4319 802.11n id */
5235 case BCM4319_D11N2G_ID: /* 4319 802.11n2g id */
5236 case BCM4319_D11N5G_ID: /* 4319 802.11n5g id */
5237 DHD_INFO(("%s: found 4319 Dongle\n", __func__));
5238 break;
5239 case 0:
5240 DHD_INFO(("%s: allow device id 0, will check chip internals\n",
5241 __func__));
5242 break;
5243
5244 default:
5245 DHD_ERROR(("%s: skipping 0x%04x/0x%04x, not a dongle\n",
5246 __func__, venid, devid));
5247 return NULL;
5248 }
5249
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005250 /* Allocate private bus interface state */
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02005251 bus = kzalloc(sizeof(dhd_bus_t), GFP_ATOMIC);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005252 if (!bus) {
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02005253 DHD_ERROR(("%s: kmalloc of dhd_bus_t failed\n", __func__));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005254 goto fail;
5255 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005256 bus->sdh = sdh;
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07005257 bus->cl_devid = (u16) devid;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005258 bus->bus = DHD_BUS;
5259 bus->tx_seq = SDPCM_SEQUENCE_WRAP - 1;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005260 bus->usebufpool = false; /* Use bufpool if allocated,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005261 else use locally malloced rxbuf */
5262
5263 /* attempt to attach to the dongle */
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005264 if (!(dhdsdio_probe_attach(bus, sdh, regsva, devid))) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005265 DHD_ERROR(("%s: dhdsdio_probe_attach failed\n", __func__));
5266 goto fail;
5267 }
5268
Franky Lin0f1a87c2011-06-29 16:46:32 -07005269 spin_lock_init(&bus->txqlock);
5270
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005271 /* Attach to the dhd/OS/network interface */
Arend van Spriel3c9d4c32011-03-02 21:18:48 +01005272 bus->dhd = dhd_attach(bus, SDPCM_RESERVE);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005273 if (!bus->dhd) {
5274 DHD_ERROR(("%s: dhd_attach failed\n", __func__));
5275 goto fail;
5276 }
5277
5278 /* Allocate buffers */
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005279 if (!(dhdsdio_probe_malloc(bus, sdh))) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005280 DHD_ERROR(("%s: dhdsdio_probe_malloc failed\n", __func__));
5281 goto fail;
5282 }
5283
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005284 if (!(dhdsdio_probe_init(bus, sdh))) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005285 DHD_ERROR(("%s: dhdsdio_probe_init failed\n", __func__));
5286 goto fail;
5287 }
5288
5289 /* Register interrupt callback, but mask it (not operational yet). */
5290 DHD_INTR(("%s: disable SDIO interrupts (not interested yet)\n",
5291 __func__));
5292 bcmsdh_intr_disable(sdh);
5293 ret = bcmsdh_intr_reg(sdh, dhdsdio_isr, bus);
5294 if (ret != 0) {
5295 DHD_ERROR(("%s: FAILED: bcmsdh_intr_reg returned %d\n",
5296 __func__, ret));
5297 goto fail;
5298 }
5299 DHD_INTR(("%s: registered SDIO interrupt function ok\n", __func__));
5300
5301 DHD_INFO(("%s: completed!!\n", __func__));
5302
5303 /* if firmware path present try to download and bring up bus */
Jason Cooper9b890322010-09-30 15:15:39 -04005304 ret = dhd_bus_start(bus->dhd);
5305 if (ret != 0) {
Roland Vossene10d82d2011-05-03 11:35:19 +02005306 if (ret == -ENOLINK) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005307 DHD_ERROR(("%s: dongle is not responding\n", __func__));
5308 goto fail;
5309 }
5310 }
5311 /* Ok, have the per-port tell the stack we're open for business */
5312 if (dhd_net_attach(bus->dhd, 0) != 0) {
5313 DHD_ERROR(("%s: Net attach failed!!\n", __func__));
5314 goto fail;
5315 }
5316
5317 return bus;
5318
5319fail:
Arend van Spriel3c9d4c32011-03-02 21:18:48 +01005320 dhdsdio_release(bus);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005321 return NULL;
5322}
5323
5324static bool
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005325dhdsdio_probe_attach(struct dhd_bus *bus, void *sdh, void *regsva, u16 devid)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005326{
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07005327 u8 clkctl = 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005328 int err = 0;
5329
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005330 bus->alp_only = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005331
5332 /* Return the window to backplane enumeration space for core access */
5333 if (dhdsdio_set_siaddr_window(bus, SI_ENUM_BASE))
5334 DHD_ERROR(("%s: FAILED to return to SI_ENUM_BASE\n", __func__));
5335
5336#ifdef DHD_DEBUG
Arend van Spriel0bef7742011-02-10 12:03:44 +01005337 printk(KERN_DEBUG "F1 signature read @0x18000000=0x%4x\n",
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005338 bcmsdh_reg_read(bus->sdh, SI_ENUM_BASE, 4));
5339
5340#endif /* DHD_DEBUG */
5341
Franky Linc05df632011-04-25 19:34:07 -07005342 /*
5343 * Force PLL off until dhdsdio_chip_attach()
5344 * programs PLL control regs
5345 */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005346
5347 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
5348 DHD_INIT_CLKCTL1, &err);
5349 if (!err)
5350 clkctl =
5351 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
5352 &err);
5353
5354 if (err || ((clkctl & ~SBSDIO_AVBITS) != DHD_INIT_CLKCTL1)) {
5355 DHD_ERROR(("dhdsdio_probe: ChipClkCSR access: err %d wrote "
5356 "0x%02x read 0x%02x\n",
5357 err, DHD_INIT_CLKCTL1, clkctl));
5358 goto fail;
5359 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005360
Franky Lincb63e4c2011-04-25 15:45:08 -07005361 if (dhdsdio_chip_attach(bus, regsva)) {
5362 DHD_ERROR(("%s: dhdsdio_chip_attach failed!\n", __func__));
5363 goto fail;
5364 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005365
Franky Linc05df632011-04-25 19:34:07 -07005366 bcmsdh_chipinfo(sdh, bus->ci->chip, bus->ci->chiprev);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005367
Franky Linc05df632011-04-25 19:34:07 -07005368 if (!dhdsdio_chipmatch((u16) bus->ci->chip)) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005369 DHD_ERROR(("%s: unsupported chip: 0x%04x\n",
Franky Linc05df632011-04-25 19:34:07 -07005370 __func__, bus->ci->chip));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005371 goto fail;
5372 }
5373
Franky Lin5d0d7a92011-04-25 19:34:05 -07005374 dhdsdio_sdiod_drive_strength_init(bus, dhd_sdiod_drive_strength);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005375
5376 /* Get info on the ARM and SOCRAM cores... */
5377 if (!DHD_NOPMU(bus)) {
Franky Linc05df632011-04-25 19:34:07 -07005378 bus->armrev = SBCOREREV(bcmsdh_reg_read(bus->sdh,
5379 CORE_SB(bus->ci->armcorebase, sbidhigh), 4));
5380 bus->orig_ramsize = bus->ci->ramsize;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005381 if (!(bus->orig_ramsize)) {
5382 DHD_ERROR(("%s: failed to find SOCRAM memory!\n",
5383 __func__));
5384 goto fail;
5385 }
5386 bus->ramsize = bus->orig_ramsize;
5387 if (dhd_dongle_memsize)
5388 dhd_dongle_setmemsize(bus, dhd_dongle_memsize);
5389
5390 DHD_ERROR(("DHD: dongle ram size is set to %d(orig %d)\n",
5391 bus->ramsize, bus->orig_ramsize));
5392 }
5393
Franky Linc05df632011-04-25 19:34:07 -07005394 bus->regs = (void *)bus->ci->buscorebase;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005395
5396 /* Set core control so an SDIO reset does a backplane reset */
Arend van Sprielff31c542011-03-01 10:56:54 +01005397 OR_REG(&bus->regs->corecontrol, CC_BPRESEN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005398
Roland Vossen67ad48b2011-06-01 13:45:51 +02005399 brcmu_pktq_init(&bus->txq, (PRIOMASK + 1), TXQLEN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005400
5401 /* Locate an appropriately-aligned portion of hdrbuf */
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07005402 bus->rxhdr = (u8 *) roundup((unsigned long)&bus->hdrbuf[0], DHD_SDALIGN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005403
5404 /* Set the poll and/or interrupt flags */
5405 bus->intr = (bool) dhd_intr;
Jason Cooper9b890322010-09-30 15:15:39 -04005406 bus->poll = (bool) dhd_poll;
5407 if (bus->poll)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005408 bus->pollrate = 1;
5409
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005410 return true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005411
5412fail:
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005413 return false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005414}
5415
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005416static bool dhdsdio_probe_malloc(dhd_bus_t *bus, void *sdh)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005417{
5418 DHD_TRACE(("%s: Enter\n", __func__));
5419
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005420 if (bus->dhd->maxctl) {
5421 bus->rxblen =
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07005422 roundup((bus->dhd->maxctl + SDPCM_HDRLEN),
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005423 ALIGNMENT) + DHD_SDALIGN;
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02005424 bus->rxbuf = kmalloc(bus->rxblen, GFP_ATOMIC);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005425 if (!(bus->rxbuf)) {
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02005426 DHD_ERROR(("%s: kmalloc of %d-byte rxbuf failed\n",
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005427 __func__, bus->rxblen));
5428 goto fail;
5429 }
5430 }
5431
5432 /* Allocate buffer to receive glomed packet */
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02005433 bus->databuf = kmalloc(MAX_DATA_BUF, GFP_ATOMIC);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005434 if (!(bus->databuf)) {
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02005435 DHD_ERROR(("%s: kmalloc of %d-byte databuf failed\n",
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005436 __func__, MAX_DATA_BUF));
5437 /* release rxbuf which was already located as above */
5438 if (!bus->rxblen)
mike.rapoport@gmail.com182acb32010-10-13 00:09:12 +02005439 kfree(bus->rxbuf);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005440 goto fail;
5441 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005442
5443 /* Align the buffer */
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07005444 if ((unsigned long)bus->databuf % DHD_SDALIGN)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005445 bus->dataptr =
5446 bus->databuf + (DHD_SDALIGN -
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07005447 ((unsigned long)bus->databuf % DHD_SDALIGN));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005448 else
5449 bus->dataptr = bus->databuf;
5450
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005451 return true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005452
5453fail:
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005454 return false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005455}
5456
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005457static bool dhdsdio_probe_init(dhd_bus_t *bus, void *sdh)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005458{
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07005459 s32 fnum;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005460
5461 DHD_TRACE(("%s: Enter\n", __func__));
5462
5463#ifdef SDTEST
5464 dhdsdio_pktgen_init(bus);
5465#endif /* SDTEST */
5466
5467 /* Disable F2 to clear any intermediate frame state on the dongle */
Franky Lin0df46042011-06-01 13:45:40 +02005468 bcmsdh_cfg_write(sdh, SDIO_FUNC_0, SDIO_CCCR_IOEx, SDIO_FUNC_ENABLE_1,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005469 NULL);
5470
5471 bus->dhd->busstate = DHD_BUS_DOWN;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005472 bus->sleeping = false;
5473 bus->rxflow = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005474 bus->prev_rxlim_hit = 0;
5475
5476 /* Done with backplane-dependent accesses, can drop clock... */
5477 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR, 0, NULL);
5478
5479 /* ...and initialize clock/power states */
5480 bus->clkstate = CLK_SDONLY;
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07005481 bus->idletime = (s32) dhd_idletime;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005482 bus->idleclock = DHD_IDLE_ACTIVE;
5483
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005484 /* Query the F2 block size, set roundup accordingly */
5485 fnum = 2;
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07005486 if (bcmsdh_iovar_op(sdh, "sd_blocksize", &fnum, sizeof(s32),
Roland Vossena1c5ad82011-04-11 15:16:24 +02005487 &bus->blocksize, sizeof(s32), false) != 0) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005488 bus->blocksize = 0;
5489 DHD_ERROR(("%s: fail on %s get\n", __func__, "sd_blocksize"));
5490 } else {
5491 DHD_INFO(("%s: Initial value for %s is %d\n",
5492 __func__, "sd_blocksize", bus->blocksize));
5493 }
Greg Kroah-Hartman7068c2f2010-10-08 11:34:59 -07005494 bus->roundup = min(max_roundup, bus->blocksize);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005495
5496 /* Query if bus module supports packet chaining,
5497 default to use if supported */
5498 if (bcmsdh_iovar_op(sdh, "sd_rxchain", NULL, 0,
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07005499 &bus->sd_rxchain, sizeof(s32),
Roland Vossena1c5ad82011-04-11 15:16:24 +02005500 false) != 0) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005501 bus->sd_rxchain = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005502 } else {
5503 DHD_INFO(("%s: bus module (through bcmsdh API) %s chaining\n",
5504 __func__,
5505 (bus->sd_rxchain ? "supports" : "does not support")));
5506 }
5507 bus->use_rxchain = (bool) bus->sd_rxchain;
5508
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005509 return true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005510}
5511
5512bool
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005513dhd_bus_download_firmware(struct dhd_bus *bus, char *fw_path, char *nv_path)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005514{
5515 bool ret;
5516 bus->fw_path = fw_path;
5517 bus->nv_path = nv_path;
5518
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005519 ret = dhdsdio_download_firmware(bus, bus->sdh);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005520
5521 return ret;
5522}
5523
5524static bool
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005525dhdsdio_download_firmware(struct dhd_bus *bus, void *sdh)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005526{
5527 bool ret;
5528
5529 /* Download the firmware */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005530 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005531
5532 ret = _dhdsdio_download_firmware(bus) == 0;
5533
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005534 dhdsdio_clkctl(bus, CLK_SDONLY, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005535
5536 return ret;
5537}
5538
5539/* Detach and free everything */
Arend van Spriel3c9d4c32011-03-02 21:18:48 +01005540static void dhdsdio_release(dhd_bus_t *bus)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005541{
5542 DHD_TRACE(("%s: Enter\n", __func__));
5543
5544 if (bus) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005545 /* De-register interrupt handler */
5546 bcmsdh_intr_disable(bus->sdh);
5547 bcmsdh_intr_dereg(bus->sdh);
5548
5549 if (bus->dhd) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005550 dhd_detach(bus->dhd);
Franky Lincee3cf42011-04-25 19:34:06 -07005551 dhdsdio_release_dongle(bus);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005552 bus->dhd = NULL;
5553 }
5554
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005555 dhdsdio_release_malloc(bus);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005556
mike.rapoport@gmail.com182acb32010-10-13 00:09:12 +02005557 kfree(bus);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005558 }
5559
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005560 DHD_TRACE(("%s: Disconnected\n", __func__));
5561}
5562
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005563static void dhdsdio_release_malloc(dhd_bus_t *bus)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005564{
5565 DHD_TRACE(("%s: Enter\n", __func__));
5566
5567 if (bus->dhd && bus->dhd->dongle_reset)
5568 return;
5569
5570 if (bus->rxbuf) {
mike.rapoport@gmail.com182acb32010-10-13 00:09:12 +02005571 kfree(bus->rxbuf);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005572 bus->rxctl = bus->rxbuf = NULL;
5573 bus->rxlen = 0;
5574 }
5575
Ilia Mirkin46d994b2011-03-13 00:28:56 -05005576 kfree(bus->databuf);
5577 bus->databuf = NULL;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005578}
5579
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005580static void dhdsdio_release_dongle(dhd_bus_t *bus)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005581{
5582 DHD_TRACE(("%s: Enter\n", __func__));
5583
5584 if (bus->dhd && bus->dhd->dongle_reset)
5585 return;
5586
Franky Linc05df632011-04-25 19:34:07 -07005587 if (bus->ci) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005588 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005589 dhdsdio_clkctl(bus, CLK_NONE, false);
Franky Lincee3cf42011-04-25 19:34:06 -07005590 dhdsdio_chip_detach(bus);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005591 if (bus->vars && bus->varsz)
mike.rapoport@gmail.com182acb32010-10-13 00:09:12 +02005592 kfree(bus->vars);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005593 bus->vars = NULL;
5594 }
5595
5596 DHD_TRACE(("%s: Disconnected\n", __func__));
5597}
5598
5599static void dhdsdio_disconnect(void *ptr)
5600{
5601 dhd_bus_t *bus = (dhd_bus_t *)ptr;
5602
5603 DHD_TRACE(("%s: Enter\n", __func__));
5604
5605 if (bus) {
5606 ASSERT(bus->dhd);
Arend van Spriel3c9d4c32011-03-02 21:18:48 +01005607 dhdsdio_release(bus);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005608 }
5609
5610 DHD_TRACE(("%s: Disconnected\n", __func__));
5611}
5612
5613/* Register/Unregister functions are called by the main DHD entry
5614 * point (e.g. module insertion) to link with the bus driver, in
5615 * order to look for or await the device.
5616 */
5617
5618static bcmsdh_driver_t dhd_sdio = {
5619 dhdsdio_probe,
5620 dhdsdio_disconnect
5621};
5622
5623int dhd_bus_register(void)
5624{
5625 DHD_TRACE(("%s: Enter\n", __func__));
5626
5627 return bcmsdh_register(&dhd_sdio);
5628}
5629
5630void dhd_bus_unregister(void)
5631{
5632 DHD_TRACE(("%s: Enter\n", __func__));
5633
5634 bcmsdh_unregister();
5635}
5636
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005637static int dhdsdio_download_code_file(struct dhd_bus *bus, char *fw_path)
5638{
5639 int bcmerror = -1;
5640 int offset = 0;
5641 uint len;
5642 void *image = NULL;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07005643 u8 *memblock = NULL, *memptr;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005644
5645 DHD_INFO(("%s: download firmware %s\n", __func__, fw_path));
5646
5647 image = dhd_os_open_image(fw_path);
5648 if (image == NULL)
5649 goto err;
5650
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02005651 memptr = memblock = kmalloc(MEMBLOCK + DHD_SDALIGN, GFP_ATOMIC);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005652 if (memblock == NULL) {
5653 DHD_ERROR(("%s: Failed to allocate memory %d bytes\n",
5654 __func__, MEMBLOCK));
5655 goto err;
5656 }
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07005657 if ((u32)(unsigned long)memblock % DHD_SDALIGN)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005658 memptr +=
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07005659 (DHD_SDALIGN - ((u32)(unsigned long)memblock % DHD_SDALIGN));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005660
5661 /* Download image */
5662 while ((len =
5663 dhd_os_get_image_block((char *)memptr, MEMBLOCK, image))) {
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005664 bcmerror = dhdsdio_membytes(bus, true, offset, memptr, len);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005665 if (bcmerror) {
5666 DHD_ERROR(("%s: error %d on writing %d membytes at "
5667 "0x%08x\n", __func__, bcmerror, MEMBLOCK, offset));
5668 goto err;
5669 }
5670
5671 offset += MEMBLOCK;
5672 }
5673
5674err:
Ilia Mirkin46d994b2011-03-13 00:28:56 -05005675 kfree(memblock);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005676
5677 if (image)
5678 dhd_os_close_image(image);
5679
5680 return bcmerror;
5681}
5682
5683/*
5684 * ProcessVars:Takes a buffer of "<var>=<value>\n" lines read from a file
5685 * and ending in a NUL.
5686 * Removes carriage returns, empty lines, comment lines, and converts
5687 * newlines to NULs.
5688 * Shortens buffer as needed and pads with NULs. End of buffer is marked
5689 * by two NULs.
5690*/
5691
5692static uint process_nvram_vars(char *varbuf, uint len)
5693{
5694 char *dp;
5695 bool findNewline;
5696 int column;
5697 uint buf_len, n;
5698
5699 dp = varbuf;
5700
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005701 findNewline = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005702 column = 0;
5703
5704 for (n = 0; n < len; n++) {
5705 if (varbuf[n] == 0)
5706 break;
5707 if (varbuf[n] == '\r')
5708 continue;
5709 if (findNewline && varbuf[n] != '\n')
5710 continue;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005711 findNewline = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005712 if (varbuf[n] == '#') {
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005713 findNewline = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005714 continue;
5715 }
5716 if (varbuf[n] == '\n') {
5717 if (column == 0)
5718 continue;
5719 *dp++ = 0;
5720 column = 0;
5721 continue;
5722 }
5723 *dp++ = varbuf[n];
5724 column++;
5725 }
5726 buf_len = dp - varbuf;
5727
5728 while (dp < varbuf + n)
5729 *dp++ = 0;
5730
5731 return buf_len;
5732}
5733
5734/*
5735 EXAMPLE: nvram_array
5736 nvram_arry format:
5737 name=value
5738 Use carriage return at the end of each assignment,
5739 and an empty string with
5740 carriage return at the end of array.
5741
5742 For example:
5743 unsigned char nvram_array[] = {"name1=value1\n",
5744 "name2=value2\n", "\n"};
5745 Hex values start with 0x, and mac addr format: xx:xx:xx:xx:xx:xx.
5746
5747 Search "EXAMPLE: nvram_array" to see how the array is activated.
5748*/
5749
5750void dhd_bus_set_nvram_params(struct dhd_bus *bus, const char *nvram_params)
5751{
5752 bus->nvram_params = nvram_params;
5753}
5754
5755static int dhdsdio_download_nvram(struct dhd_bus *bus)
5756{
5757 int bcmerror = -1;
5758 uint len;
5759 void *image = NULL;
5760 char *memblock = NULL;
5761 char *bufp;
5762 char *nv_path;
5763 bool nvram_file_exists;
5764
5765 nv_path = bus->nv_path;
5766
5767 nvram_file_exists = ((nv_path != NULL) && (nv_path[0] != '\0'));
5768 if (!nvram_file_exists && (bus->nvram_params == NULL))
5769 return 0;
5770
5771 if (nvram_file_exists) {
5772 image = dhd_os_open_image(nv_path);
5773 if (image == NULL)
5774 goto err;
5775 }
5776
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02005777 memblock = kmalloc(MEMBLOCK, GFP_ATOMIC);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005778 if (memblock == NULL) {
5779 DHD_ERROR(("%s: Failed to allocate memory %d bytes\n",
5780 __func__, MEMBLOCK));
5781 goto err;
5782 }
5783
5784 /* Download variables */
5785 if (nvram_file_exists) {
5786 len = dhd_os_get_image_block(memblock, MEMBLOCK, image);
5787 } else {
5788 len = strlen(bus->nvram_params);
5789 ASSERT(len <= MEMBLOCK);
5790 if (len > MEMBLOCK)
5791 len = MEMBLOCK;
5792 memcpy(memblock, bus->nvram_params, len);
5793 }
5794
5795 if (len > 0 && len < MEMBLOCK) {
5796 bufp = (char *)memblock;
5797 bufp[len] = 0;
5798 len = process_nvram_vars(bufp, len);
5799 bufp += len;
5800 *bufp++ = 0;
5801 if (len)
5802 bcmerror = dhdsdio_downloadvars(bus, memblock, len + 1);
5803 if (bcmerror) {
5804 DHD_ERROR(("%s: error downloading vars: %d\n",
5805 __func__, bcmerror));
5806 }
5807 } else {
5808 DHD_ERROR(("%s: error reading nvram file: %d\n",
5809 __func__, len));
Roland Vossenb74ac122011-05-03 11:35:20 +02005810 bcmerror = -EIO;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005811 }
5812
5813err:
Ilia Mirkin46d994b2011-03-13 00:28:56 -05005814 kfree(memblock);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005815
5816 if (image)
5817 dhd_os_close_image(image);
5818
5819 return bcmerror;
5820}
5821
5822static int _dhdsdio_download_firmware(struct dhd_bus *bus)
5823{
5824 int bcmerror = -1;
5825
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005826 bool embed = false; /* download embedded firmware */
5827 bool dlok = false; /* download firmware succeeded */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005828
5829 /* Out immediately if no image to download */
Franky Lina4181fb2011-06-01 13:45:34 +02005830 if ((bus->fw_path == NULL) || (bus->fw_path[0] == '\0'))
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005831 return bcmerror;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005832
5833 /* Keep arm in reset */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005834 if (dhdsdio_download_state(bus, true)) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005835 DHD_ERROR(("%s: error placing ARM core in reset\n", __func__));
5836 goto err;
5837 }
5838
5839 /* External image takes precedence if specified */
5840 if ((bus->fw_path != NULL) && (bus->fw_path[0] != '\0')) {
5841 if (dhdsdio_download_code_file(bus, bus->fw_path)) {
5842 DHD_ERROR(("%s: dongle image file download failed\n",
5843 __func__));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005844 goto err;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005845 } else {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005846 embed = false;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005847 dlok = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005848 }
5849 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005850 if (!dlok) {
5851 DHD_ERROR(("%s: dongle image download failed\n", __func__));
5852 goto err;
5853 }
5854
5855 /* EXAMPLE: nvram_array */
5856 /* If a valid nvram_arry is specified as above, it can be passed
5857 down to dongle */
5858 /* dhd_bus_set_nvram_params(bus, (char *)&nvram_array); */
5859
5860 /* External nvram takes precedence if specified */
5861 if (dhdsdio_download_nvram(bus)) {
5862 DHD_ERROR(("%s: dongle nvram file download failed\n",
5863 __func__));
5864 }
5865
5866 /* Take arm out of reset */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005867 if (dhdsdio_download_state(bus, false)) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005868 DHD_ERROR(("%s: error getting out of ARM core reset\n",
5869 __func__));
5870 goto err;
5871 }
5872
5873 bcmerror = 0;
5874
5875err:
5876 return bcmerror;
5877}
5878
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005879
5880static int
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07005881dhd_bcmsdh_send_buf(dhd_bus_t *bus, u32 addr, uint fn, uint flags,
Arend van Sprielc26b1372010-11-23 14:06:23 +01005882 u8 *buf, uint nbytes, struct sk_buff *pkt,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005883 bcmsdh_cmplt_fn_t complete, void *handle)
5884{
5885 return bcmsdh_send_buf
5886 (bus->sdh, addr, fn, flags, buf, nbytes, pkt, complete,
5887 handle);
5888}
5889
5890uint dhd_bus_chip(struct dhd_bus *bus)
5891{
Franky Linc05df632011-04-25 19:34:07 -07005892 ASSERT(bus->ci != NULL);
5893 return bus->ci->chip;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005894}
5895
5896void *dhd_bus_pub(struct dhd_bus *bus)
5897{
5898 return bus->dhd;
5899}
5900
5901void *dhd_bus_txq(struct dhd_bus *bus)
5902{
5903 return &bus->txq;
5904}
5905
5906uint dhd_bus_hdrlen(struct dhd_bus *bus)
5907{
5908 return SDPCM_HDRLEN;
5909}
5910
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07005911int dhd_bus_devreset(dhd_pub_t *dhdp, u8 flag)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005912{
5913 int bcmerror = 0;
5914 dhd_bus_t *bus;
5915
5916 bus = dhdp->bus;
5917
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005918 if (flag == true) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005919 if (!bus->dhd->dongle_reset) {
5920 /* Expect app to have torn down any
5921 connection before calling */
5922 /* Stop the bus, disable F2 */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005923 dhd_bus_stop(bus, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005924
5925 /* Clean tx/rx buffer pointers,
5926 detach from the dongle */
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005927 dhdsdio_release_dongle(bus);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005928
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005929 bus->dhd->dongle_reset = true;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005930 bus->dhd->up = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005931
5932 DHD_TRACE(("%s: WLAN OFF DONE\n", __func__));
5933 /* App can now remove power from device */
5934 } else
Roland Vossenb74ac122011-05-03 11:35:20 +02005935 bcmerror = -EIO;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005936 } else {
5937 /* App must have restored power to device before calling */
5938
5939 DHD_TRACE(("\n\n%s: == WLAN ON ==\n", __func__));
5940
5941 if (bus->dhd->dongle_reset) {
5942 /* Turn on WLAN */
5943 /* Reset SD client */
5944 bcmsdh_reset(bus->sdh);
5945
5946 /* Attempt to re-attach & download */
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005947 if (dhdsdio_probe_attach(bus, bus->sdh,
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07005948 (u32 *) SI_ENUM_BASE,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005949 bus->cl_devid)) {
5950 /* Attempt to download binary to the dongle */
5951 if (dhdsdio_probe_init
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005952 (bus, bus->sdh)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005953 && dhdsdio_download_firmware(bus,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005954 bus->sdh)) {
5955
5956 /* Re-init bus, enable F2 transfer */
5957 dhd_bus_init((dhd_pub_t *) bus->dhd,
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005958 false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005959
5960#if defined(OOB_INTR_ONLY)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005961 dhd_enable_oob_intr(bus, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005962#endif /* defined(OOB_INTR_ONLY) */
5963
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005964 bus->dhd->dongle_reset = false;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005965 bus->dhd->up = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005966
5967 DHD_TRACE(("%s: WLAN ON DONE\n",
5968 __func__));
5969 } else
Roland Vossenb74ac122011-05-03 11:35:20 +02005970 bcmerror = -EIO;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005971 } else
Roland Vossenb74ac122011-05-03 11:35:20 +02005972 bcmerror = -EIO;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005973 } else {
Roland Vossenb74ac122011-05-03 11:35:20 +02005974 bcmerror = -EISCONN;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005975 DHD_ERROR(("%s: Set DEVRESET=false invoked when device "
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005976 "is on\n", __func__));
Roland Vossenb74ac122011-05-03 11:35:20 +02005977 bcmerror = -EIO;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005978 }
5979 }
5980 return bcmerror;
5981}
Franky Lincb63e4c2011-04-25 15:45:08 -07005982
5983static int
5984dhdsdio_chip_recognition(bcmsdh_info_t *sdh, struct chip_info *ci, void *regs)
5985{
5986 u32 regdata;
5987
5988 /*
5989 * Get CC core rev
5990 * Chipid is assume to be at offset 0 from regs arg
5991 * For different chiptypes or old sdio hosts w/o chipcommon,
5992 * other ways of recognition should be added here.
5993 */
5994 ci->cccorebase = (u32)regs;
5995 regdata = bcmsdh_reg_read(sdh, CORE_CC_REG(ci->cccorebase, chipid), 4);
5996 ci->chip = regdata & CID_ID_MASK;
5997 ci->chiprev = (regdata & CID_REV_MASK) >> CID_REV_SHIFT;
5998
5999 DHD_INFO(("%s: chipid=0x%x chiprev=%d\n",
6000 __func__, ci->chip, ci->chiprev));
6001
6002 /* Address of cores for new chips should be added here */
6003 switch (ci->chip) {
6004 case BCM4329_CHIP_ID:
6005 ci->buscorebase = BCM4329_CORE_BUS_BASE;
6006 ci->ramcorebase = BCM4329_CORE_SOCRAM_BASE;
6007 ci->armcorebase = BCM4329_CORE_ARM_BASE;
Franky Linc05df632011-04-25 19:34:07 -07006008 ci->ramsize = BCM4329_RAMSIZE;
Franky Lincb63e4c2011-04-25 15:45:08 -07006009 break;
6010 default:
6011 DHD_ERROR(("%s: chipid 0x%x is not supported\n",
6012 __func__, ci->chip));
6013 return -ENODEV;
6014 }
6015
6016 regdata = bcmsdh_reg_read(sdh,
6017 CORE_SB(ci->cccorebase, sbidhigh), 4);
6018 ci->ccrev = SBCOREREV(regdata);
6019
6020 regdata = bcmsdh_reg_read(sdh,
6021 CORE_CC_REG(ci->cccorebase, pmucapabilities), 4);
6022 ci->pmurev = regdata & PCAP_REV_MASK;
6023
6024 regdata = bcmsdh_reg_read(sdh, CORE_SB(ci->buscorebase, sbidhigh), 4);
6025 ci->buscorerev = SBCOREREV(regdata);
6026 ci->buscoretype = (regdata & SBIDH_CC_MASK) >> SBIDH_CC_SHIFT;
6027
6028 DHD_INFO(("%s: ccrev=%d, pmurev=%d, buscore rev/type=%d/0x%x\n",
6029 __func__, ci->ccrev, ci->pmurev,
6030 ci->buscorerev, ci->buscoretype));
6031
6032 /* get chipcommon capabilites */
6033 ci->cccaps = bcmsdh_reg_read(sdh,
6034 CORE_CC_REG(ci->cccorebase, capabilities), 4);
6035
6036 return 0;
6037}
6038
6039static void
6040dhdsdio_chip_disablecore(bcmsdh_info_t *sdh, u32 corebase)
6041{
6042 u32 regdata;
6043
6044 regdata = bcmsdh_reg_read(sdh,
6045 CORE_SB(corebase, sbtmstatelow), 4);
6046 if (regdata & SBTML_RESET)
6047 return;
6048
6049 regdata = bcmsdh_reg_read(sdh,
6050 CORE_SB(corebase, sbtmstatelow), 4);
6051 if ((regdata & (SICF_CLOCK_EN << SBTML_SICF_SHIFT)) != 0) {
6052 /*
6053 * set target reject and spin until busy is clear
6054 * (preserve core-specific bits)
6055 */
6056 regdata = bcmsdh_reg_read(sdh,
6057 CORE_SB(corebase, sbtmstatelow), 4);
6058 bcmsdh_reg_write(sdh, CORE_SB(corebase, sbtmstatelow), 4,
6059 regdata | SBTML_REJ);
6060
6061 regdata = bcmsdh_reg_read(sdh,
6062 CORE_SB(corebase, sbtmstatelow), 4);
6063 udelay(1);
6064 SPINWAIT((bcmsdh_reg_read(sdh,
6065 CORE_SB(corebase, sbtmstatehigh), 4) &
6066 SBTMH_BUSY), 100000);
6067
6068 regdata = bcmsdh_reg_read(sdh,
6069 CORE_SB(corebase, sbtmstatehigh), 4);
6070 if (regdata & SBTMH_BUSY)
6071 DHD_ERROR(("%s: ARM core still busy\n", __func__));
6072
6073 regdata = bcmsdh_reg_read(sdh,
6074 CORE_SB(corebase, sbidlow), 4);
6075 if (regdata & SBIDL_INIT) {
6076 regdata = bcmsdh_reg_read(sdh,
6077 CORE_SB(corebase, sbimstate), 4) |
6078 SBIM_RJ;
6079 bcmsdh_reg_write(sdh,
6080 CORE_SB(corebase, sbimstate), 4,
6081 regdata);
6082 regdata = bcmsdh_reg_read(sdh,
6083 CORE_SB(corebase, sbimstate), 4);
6084 udelay(1);
6085 SPINWAIT((bcmsdh_reg_read(sdh,
6086 CORE_SB(corebase, sbimstate), 4) &
6087 SBIM_BY), 100000);
6088 }
6089
6090 /* set reset and reject while enabling the clocks */
6091 bcmsdh_reg_write(sdh,
6092 CORE_SB(corebase, sbtmstatelow), 4,
6093 (((SICF_FGC | SICF_CLOCK_EN) << SBTML_SICF_SHIFT) |
6094 SBTML_REJ | SBTML_RESET));
6095 regdata = bcmsdh_reg_read(sdh,
6096 CORE_SB(corebase, sbtmstatelow), 4);
6097 udelay(10);
6098
6099 /* clear the initiator reject bit */
6100 regdata = bcmsdh_reg_read(sdh,
6101 CORE_SB(corebase, sbidlow), 4);
6102 if (regdata & SBIDL_INIT) {
6103 regdata = bcmsdh_reg_read(sdh,
6104 CORE_SB(corebase, sbimstate), 4) &
6105 ~SBIM_RJ;
6106 bcmsdh_reg_write(sdh,
6107 CORE_SB(corebase, sbimstate), 4,
6108 regdata);
6109 }
6110 }
6111
6112 /* leave reset and reject asserted */
6113 bcmsdh_reg_write(sdh, CORE_SB(corebase, sbtmstatelow), 4,
6114 (SBTML_REJ | SBTML_RESET));
6115 udelay(1);
6116}
6117
6118static int
6119dhdsdio_chip_attach(struct dhd_bus *bus, void *regs)
6120{
6121 struct chip_info *ci;
6122 int err;
6123 u8 clkval, clkset;
6124
6125 DHD_TRACE(("%s: Enter\n", __func__));
6126
6127 /* alloc chip_info_t */
6128 ci = kmalloc(sizeof(struct chip_info), GFP_ATOMIC);
6129 if (NULL == ci) {
6130 DHD_ERROR(("%s: malloc failed!\n", __func__));
6131 return -ENOMEM;
6132 }
6133
6134 memset((unsigned char *)ci, 0, sizeof(struct chip_info));
6135
6136 /* bus/core/clk setup for register access */
6137 /* Try forcing SDIO core to do ALPAvail request only */
6138 clkset = SBSDIO_FORCE_HW_CLKREQ_OFF | SBSDIO_ALP_AVAIL_REQ;
6139 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
6140 clkset, &err);
6141 if (err) {
6142 DHD_ERROR(("%s: error writing for HT off\n", __func__));
6143 goto fail;
6144 }
6145
6146 /* If register supported, wait for ALPAvail and then force ALP */
6147 /* This may take up to 15 milliseconds */
6148 clkval = bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1,
6149 SBSDIO_FUNC1_CHIPCLKCSR, NULL);
6150 if ((clkval & ~SBSDIO_AVBITS) == clkset) {
6151 SPINWAIT(((clkval =
6152 bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1,
6153 SBSDIO_FUNC1_CHIPCLKCSR,
6154 NULL)),
6155 !SBSDIO_ALPAV(clkval)),
6156 PMU_MAX_TRANSITION_DLY);
6157 if (!SBSDIO_ALPAV(clkval)) {
6158 DHD_ERROR(("%s: timeout on ALPAV wait, clkval 0x%02x\n",
6159 __func__, clkval));
6160 err = -EBUSY;
6161 goto fail;
6162 }
6163 clkset = SBSDIO_FORCE_HW_CLKREQ_OFF |
6164 SBSDIO_FORCE_ALP;
6165 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1,
6166 SBSDIO_FUNC1_CHIPCLKCSR,
6167 clkset, &err);
6168 udelay(65);
6169 } else {
6170 DHD_ERROR(("%s: ChipClkCSR access: wrote 0x%02x read 0x%02x\n",
6171 __func__, clkset, clkval));
6172 err = -EACCES;
6173 goto fail;
6174 }
6175
6176 /* Also, disable the extra SDIO pull-ups */
6177 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_SDIOPULLUP, 0,
6178 NULL);
6179
6180 err = dhdsdio_chip_recognition(bus->sdh, ci, regs);
6181 if (err)
6182 goto fail;
6183
6184 /*
6185 * Make sure any on-chip ARM is off (in case strapping is wrong),
6186 * or downloaded code was already running.
6187 */
6188 dhdsdio_chip_disablecore(bus->sdh, ci->armcorebase);
6189
6190 bcmsdh_reg_write(bus->sdh,
6191 CORE_CC_REG(ci->cccorebase, gpiopullup), 4, 0);
6192 bcmsdh_reg_write(bus->sdh,
6193 CORE_CC_REG(ci->cccorebase, gpiopulldown), 4, 0);
6194
6195 /* Disable F2 to clear any intermediate frame state on the dongle */
Franky Lin0df46042011-06-01 13:45:40 +02006196 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_0, SDIO_CCCR_IOEx,
Franky Lincb63e4c2011-04-25 15:45:08 -07006197 SDIO_FUNC_ENABLE_1, NULL);
6198
6199 /* WAR: cmd52 backplane read so core HW will drop ALPReq */
6200 clkval = bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1,
6201 0, NULL);
6202
6203 /* Done with backplane-dependent accesses, can drop clock... */
6204 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR, 0,
6205 NULL);
6206
6207 bus->ci = ci;
6208 return 0;
6209fail:
6210 bus->ci = NULL;
6211 kfree(ci);
6212 return err;
6213}
Franky Lineb5dc512011-04-25 19:34:04 -07006214
6215static void
6216dhdsdio_chip_resetcore(bcmsdh_info_t *sdh, u32 corebase)
6217{
6218 u32 regdata;
6219
6220 /*
6221 * Must do the disable sequence first to work for
6222 * arbitrary current core state.
6223 */
6224 dhdsdio_chip_disablecore(sdh, corebase);
6225
6226 /*
6227 * Now do the initialization sequence.
6228 * set reset while enabling the clock and
6229 * forcing them on throughout the core
6230 */
6231 bcmsdh_reg_write(sdh, CORE_SB(corebase, sbtmstatelow), 4,
6232 ((SICF_FGC | SICF_CLOCK_EN) << SBTML_SICF_SHIFT) |
6233 SBTML_RESET);
6234 udelay(1);
6235
6236 regdata = bcmsdh_reg_read(sdh, CORE_SB(corebase, sbtmstatehigh), 4);
6237 if (regdata & SBTMH_SERR)
6238 bcmsdh_reg_write(sdh, CORE_SB(corebase, sbtmstatehigh), 4, 0);
6239
6240 regdata = bcmsdh_reg_read(sdh, CORE_SB(corebase, sbimstate), 4);
6241 if (regdata & (SBIM_IBE | SBIM_TO))
6242 bcmsdh_reg_write(sdh, CORE_SB(corebase, sbimstate), 4,
6243 regdata & ~(SBIM_IBE | SBIM_TO));
6244
6245 /* clear reset and allow it to propagate throughout the core */
6246 bcmsdh_reg_write(sdh, CORE_SB(corebase, sbtmstatelow), 4,
6247 (SICF_FGC << SBTML_SICF_SHIFT) |
6248 (SICF_CLOCK_EN << SBTML_SICF_SHIFT));
6249 udelay(1);
6250
6251 /* leave clock enabled */
6252 bcmsdh_reg_write(sdh, CORE_SB(corebase, sbtmstatelow), 4,
6253 (SICF_CLOCK_EN << SBTML_SICF_SHIFT));
6254 udelay(1);
6255}
Franky Lin5d0d7a92011-04-25 19:34:05 -07006256
6257/* SDIO Pad drive strength to select value mappings */
6258struct sdiod_drive_str {
6259 u8 strength; /* Pad Drive Strength in mA */
6260 u8 sel; /* Chip-specific select value */
6261};
6262
6263/* SDIO Drive Strength to sel value table for PMU Rev 1 */
6264static const struct sdiod_drive_str sdiod_drive_strength_tab1[] = {
6265 {
6266 4, 0x2}, {
6267 2, 0x3}, {
6268 1, 0x0}, {
6269 0, 0x0}
6270 };
6271
6272/* SDIO Drive Strength to sel value table for PMU Rev 2, 3 */
6273static const struct sdiod_drive_str sdiod_drive_strength_tab2[] = {
6274 {
6275 12, 0x7}, {
6276 10, 0x6}, {
6277 8, 0x5}, {
6278 6, 0x4}, {
6279 4, 0x2}, {
6280 2, 0x1}, {
6281 0, 0x0}
6282 };
6283
6284/* SDIO Drive Strength to sel value table for PMU Rev 8 (1.8V) */
6285static const struct sdiod_drive_str sdiod_drive_strength_tab3[] = {
6286 {
6287 32, 0x7}, {
6288 26, 0x6}, {
6289 22, 0x5}, {
6290 16, 0x4}, {
6291 12, 0x3}, {
6292 8, 0x2}, {
6293 4, 0x1}, {
6294 0, 0x0}
6295 };
6296
6297#define SDIOD_DRVSTR_KEY(chip, pmu) (((chip) << 16) | (pmu))
6298
6299static void
6300dhdsdio_sdiod_drive_strength_init(struct dhd_bus *bus, u32 drivestrength) {
6301 struct sdiod_drive_str *str_tab = NULL;
6302 u32 str_mask = 0;
6303 u32 str_shift = 0;
Franky Lin5d0d7a92011-04-25 19:34:05 -07006304 char chn[8];
Franky Lin5d0d7a92011-04-25 19:34:05 -07006305
6306 if (!(bus->ci->cccaps & CC_CAP_PMU))
6307 return;
6308
6309 switch (SDIOD_DRVSTR_KEY(bus->ci->chip, bus->ci->pmurev)) {
6310 case SDIOD_DRVSTR_KEY(BCM4325_CHIP_ID, 1):
6311 str_tab = (struct sdiod_drive_str *)&sdiod_drive_strength_tab1;
6312 str_mask = 0x30000000;
6313 str_shift = 28;
6314 break;
6315 case SDIOD_DRVSTR_KEY(BCM4325_CHIP_ID, 2):
6316 case SDIOD_DRVSTR_KEY(BCM4325_CHIP_ID, 3):
6317 str_tab = (struct sdiod_drive_str *)&sdiod_drive_strength_tab2;
6318 str_mask = 0x00003800;
6319 str_shift = 11;
6320 break;
6321 case SDIOD_DRVSTR_KEY(BCM4336_CHIP_ID, 8):
6322 str_tab = (struct sdiod_drive_str *)&sdiod_drive_strength_tab3;
6323 str_mask = 0x00003800;
6324 str_shift = 11;
6325 break;
6326 default:
6327 DHD_ERROR(("No SDIO Drive strength init"
6328 "done for chip %s rev %d pmurev %d\n",
Roland Vossen67ad48b2011-06-01 13:45:51 +02006329 brcmu_chipname(bus->ci->chip, chn, 8),
Franky Lin5d0d7a92011-04-25 19:34:05 -07006330 bus->ci->chiprev, bus->ci->pmurev));
6331 break;
6332 }
6333
6334 if (str_tab != NULL) {
6335 u32 drivestrength_sel = 0;
6336 u32 cc_data_temp;
6337 int i;
6338
6339 for (i = 0; str_tab[i].strength != 0; i++) {
6340 if (drivestrength >= str_tab[i].strength) {
6341 drivestrength_sel = str_tab[i].sel;
6342 break;
6343 }
6344 }
6345
6346 bcmsdh_reg_write(bus->sdh,
6347 CORE_CC_REG(bus->ci->cccorebase, chipcontrol_addr),
6348 4, 1);
6349 cc_data_temp = bcmsdh_reg_read(bus->sdh,
6350 CORE_CC_REG(bus->ci->cccorebase, chipcontrol_addr), 4);
6351 cc_data_temp &= ~str_mask;
6352 drivestrength_sel <<= str_shift;
6353 cc_data_temp |= drivestrength_sel;
6354 bcmsdh_reg_write(bus->sdh,
6355 CORE_CC_REG(bus->ci->cccorebase, chipcontrol_addr),
6356 4, cc_data_temp);
6357
6358 DHD_INFO(("SDIO: %dmA drive strength selected, set to 0x%08x\n",
6359 drivestrength, cc_data_temp));
6360 }
6361}
Franky Lincee3cf42011-04-25 19:34:06 -07006362
6363static void
6364dhdsdio_chip_detach(struct dhd_bus *bus)
6365{
6366 DHD_TRACE(("%s: Enter\n", __func__));
6367
6368 kfree(bus->ci);
6369 bus->ci = NULL;
6370}