blob: 119ee6eaf1c3df944b6482957be5350220433753 [file] [log] [blame]
Arend van Spriel5b435de2011-10-05 13:19:03 +02001/*
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
17#include <linux/types.h>
18#include <linux/kernel.h>
19#include <linux/kthread.h>
20#include <linux/printk.h>
21#include <linux/pci_ids.h>
22#include <linux/netdevice.h>
23#include <linux/interrupt.h>
24#include <linux/sched.h>
25#include <linux/mmc/sdio.h>
26#include <linux/mmc/sdio_func.h>
27#include <linux/mmc/card.h>
28#include <linux/semaphore.h>
29#include <linux/firmware.h>
Stephen Rothwellb7a57e72011-10-12 21:35:07 +020030#include <linux/module.h>
Franky Lin99ba15c2011-11-04 22:23:42 +010031#include <linux/bcma/bcma.h>
Arend van Spriel4fc0d012012-06-14 12:16:57 +020032#include <linux/debugfs.h>
Arend van Spriel8dc01812012-06-21 11:33:32 +020033#include <linux/vmalloc.h>
Hante Meuleman668761a2013-04-12 10:55:55 +020034#include <linux/platform_data/brcmfmac-sdio.h>
Franky Lin8da9d2c2013-11-29 12:25:10 +010035#include <linux/moduleparam.h>
Arend van Spriel5b435de2011-10-05 13:19:03 +020036#include <asm/unaligned.h>
37#include <defs.h>
38#include <brcmu_wifi.h>
39#include <brcmu_utils.h>
40#include <brcm_hw_ids.h>
41#include <soc.h>
42#include "sdio_host.h"
Franky Lina83369b2011-11-04 22:23:28 +010043#include "sdio_chip.h"
Hante Meulemana74d0362014-01-13 22:20:22 +010044#include "nvram.h"
Arend van Spriel5b435de2011-10-05 13:19:03 +020045
46#define DCMD_RESP_TIMEOUT 2000 /* In milli second */
47
Joe Perches8ae74652012-01-15 00:38:38 -080048#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +020049
50#define BRCMF_TRAP_INFO_SIZE 80
51
52#define CBUF_LEN (128)
53
Arend van Spriel4fc0d012012-06-14 12:16:57 +020054/* Device console log buffer state */
55#define CONSOLE_BUFFER_MAX 2024
56
Arend van Spriel5b435de2011-10-05 13:19:03 +020057struct rte_log_le {
58 __le32 buf; /* Can't be pointer on (64-bit) hosts */
59 __le32 buf_size;
60 __le32 idx;
61 char *_buf_compat; /* Redundant pointer for backward compat. */
62};
63
64struct rte_console {
65 /* Virtual UART
66 * When there is no UART (e.g. Quickturn),
67 * the host should write a complete
68 * input line directly into cbuf and then write
69 * the length into vcons_in.
70 * This may also be used when there is a real UART
71 * (at risk of conflicting with
72 * the real UART). vcons_out is currently unused.
73 */
74 uint vcons_in;
75 uint vcons_out;
76
77 /* Output (logging) buffer
78 * Console output is written to a ring buffer log_buf at index log_idx.
79 * The host may read the output when it sees log_idx advance.
80 * Output will be lost if the output wraps around faster than the host
81 * polls.
82 */
83 struct rte_log_le log_le;
84
85 /* Console input line buffer
86 * Characters are read one at a time into cbuf
87 * until <CR> is received, then
88 * the buffer is processed as a command line.
89 * Also used for virtual UART.
90 */
91 uint cbuf_idx;
92 char cbuf[CBUF_LEN];
93};
94
Joe Perches8ae74652012-01-15 00:38:38 -080095#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +020096#include <chipcommon.h>
97
Arend van Spriel5b435de2011-10-05 13:19:03 +020098#include "dhd_bus.h"
Arend van Spriel5b435de2011-10-05 13:19:03 +020099#include "dhd_dbg.h"
Arend van Spriel40c1c242013-04-05 10:57:44 +0200100#include "tracepoint.h"
Arend van Spriel5b435de2011-10-05 13:19:03 +0200101
102#define TXQLEN 2048 /* bulk tx queue length */
103#define TXHI (TXQLEN - 256) /* turn on flow control above TXHI */
104#define TXLOW (TXHI - 256) /* turn off flow control below TXLOW */
105#define PRIOMASK 7
106
107#define TXRETRIES 2 /* # of retries for tx frames */
108
109#define BRCMF_RXBOUND 50 /* Default for max rx frames in
110 one scheduling */
111
112#define BRCMF_TXBOUND 20 /* Default for max tx frames in
113 one scheduling */
114
Franky Lin8da9d2c2013-11-29 12:25:10 +0100115#define BRCMF_DEFAULT_TXGLOM_SIZE 32 /* max tx frames in glom chain */
116
Arend van Spriel5b435de2011-10-05 13:19:03 +0200117#define BRCMF_TXMINMAX 1 /* Max tx frames if rx still pending */
118
119#define MEMBLOCK 2048 /* Block size used for downloading
120 of dongle image */
121#define MAX_DATA_BUF (32 * 1024) /* Must be large enough to hold
122 biggest possible glom */
123
124#define BRCMF_FIRSTREAD (1 << 6)
125
126
127/* SBSDIO_DEVICE_CTL */
128
129/* 1: device will assert busy signal when receiving CMD53 */
130#define SBSDIO_DEVCTL_SETBUSY 0x01
131/* 1: assertion of sdio interrupt is synchronous to the sdio clock */
132#define SBSDIO_DEVCTL_SPI_INTR_SYNC 0x02
133/* 1: mask all interrupts to host except the chipActive (rev 8) */
134#define SBSDIO_DEVCTL_CA_INT_ONLY 0x04
135/* 1: isolate internal sdio signals, put external pads in tri-state; requires
136 * sdio bus power cycle to clear (rev 9) */
137#define SBSDIO_DEVCTL_PADS_ISO 0x08
138/* Force SD->SB reset mapping (rev 11) */
139#define SBSDIO_DEVCTL_SB_RST_CTL 0x30
140/* Determined by CoreControl bit */
141#define SBSDIO_DEVCTL_RST_CORECTL 0x00
142/* Force backplane reset */
143#define SBSDIO_DEVCTL_RST_BPRESET 0x10
144/* Force no backplane reset */
145#define SBSDIO_DEVCTL_RST_NOBPRESET 0x20
146
Arend van Spriel5b435de2011-10-05 13:19:03 +0200147/* direct(mapped) cis space */
148
149/* MAPPED common CIS address */
150#define SBSDIO_CIS_BASE_COMMON 0x1000
151/* maximum bytes in one CIS */
152#define SBSDIO_CIS_SIZE_LIMIT 0x200
153/* cis offset addr is < 17 bits */
154#define SBSDIO_CIS_OFT_ADDR_MASK 0x1FFFF
155
156/* manfid tuple length, include tuple, link bytes */
157#define SBSDIO_CIS_MANFID_TUPLE_LEN 6
158
159/* intstatus */
160#define I_SMB_SW0 (1 << 0) /* To SB Mail S/W interrupt 0 */
161#define I_SMB_SW1 (1 << 1) /* To SB Mail S/W interrupt 1 */
162#define I_SMB_SW2 (1 << 2) /* To SB Mail S/W interrupt 2 */
163#define I_SMB_SW3 (1 << 3) /* To SB Mail S/W interrupt 3 */
164#define I_SMB_SW_MASK 0x0000000f /* To SB Mail S/W interrupts mask */
165#define I_SMB_SW_SHIFT 0 /* To SB Mail S/W interrupts shift */
166#define I_HMB_SW0 (1 << 4) /* To Host Mail S/W interrupt 0 */
167#define I_HMB_SW1 (1 << 5) /* To Host Mail S/W interrupt 1 */
168#define I_HMB_SW2 (1 << 6) /* To Host Mail S/W interrupt 2 */
169#define I_HMB_SW3 (1 << 7) /* To Host Mail S/W interrupt 3 */
170#define I_HMB_SW_MASK 0x000000f0 /* To Host Mail S/W interrupts mask */
171#define I_HMB_SW_SHIFT 4 /* To Host Mail S/W interrupts shift */
172#define I_WR_OOSYNC (1 << 8) /* Write Frame Out Of Sync */
173#define I_RD_OOSYNC (1 << 9) /* Read Frame Out Of Sync */
174#define I_PC (1 << 10) /* descriptor error */
175#define I_PD (1 << 11) /* data error */
176#define I_DE (1 << 12) /* Descriptor protocol Error */
177#define I_RU (1 << 13) /* Receive descriptor Underflow */
178#define I_RO (1 << 14) /* Receive fifo Overflow */
179#define I_XU (1 << 15) /* Transmit fifo Underflow */
180#define I_RI (1 << 16) /* Receive Interrupt */
181#define I_BUSPWR (1 << 17) /* SDIO Bus Power Change (rev 9) */
182#define I_XMTDATA_AVAIL (1 << 23) /* bits in fifo */
183#define I_XI (1 << 24) /* Transmit Interrupt */
184#define I_RF_TERM (1 << 25) /* Read Frame Terminate */
185#define I_WF_TERM (1 << 26) /* Write Frame Terminate */
186#define I_PCMCIA_XU (1 << 27) /* PCMCIA Transmit FIFO Underflow */
187#define I_SBINT (1 << 28) /* sbintstatus Interrupt */
188#define I_CHIPACTIVE (1 << 29) /* chip from doze to active state */
189#define I_SRESET (1 << 30) /* CCCR RES interrupt */
190#define I_IOE2 (1U << 31) /* CCCR IOE2 Bit Changed */
191#define I_ERRORS (I_PC | I_PD | I_DE | I_RU | I_RO | I_XU)
192#define I_DMA (I_RI | I_XI | I_ERRORS)
193
194/* corecontrol */
195#define CC_CISRDY (1 << 0) /* CIS Ready */
196#define CC_BPRESEN (1 << 1) /* CCCR RES signal */
197#define CC_F2RDY (1 << 2) /* set CCCR IOR2 bit */
198#define CC_CLRPADSISO (1 << 3) /* clear SDIO pads isolation */
199#define CC_XMTDATAAVAIL_MODE (1 << 4)
200#define CC_XMTDATAAVAIL_CTRL (1 << 5)
201
202/* SDA_FRAMECTRL */
203#define SFC_RF_TERM (1 << 0) /* Read Frame Terminate */
204#define SFC_WF_TERM (1 << 1) /* Write Frame Terminate */
205#define SFC_CRC4WOOS (1 << 2) /* CRC error for write out of sync */
206#define SFC_ABORTALL (1 << 3) /* Abort all in-progress frames */
207
Arend van Spriel5b435de2011-10-05 13:19:03 +0200208/*
209 * Software allocation of To SB Mailbox resources
210 */
211
212/* tosbmailbox bits corresponding to intstatus bits */
213#define SMB_NAK (1 << 0) /* Frame NAK */
214#define SMB_INT_ACK (1 << 1) /* Host Interrupt ACK */
215#define SMB_USE_OOB (1 << 2) /* Use OOB Wakeup */
216#define SMB_DEV_INT (1 << 3) /* Miscellaneous Interrupt */
217
218/* tosbmailboxdata */
219#define SMB_DATA_VERSION_SHIFT 16 /* host protocol version */
220
221/*
222 * Software allocation of To Host Mailbox resources
223 */
224
225/* intstatus bits */
226#define I_HMB_FC_STATE I_HMB_SW0 /* Flow Control State */
227#define I_HMB_FC_CHANGE I_HMB_SW1 /* Flow Control State Changed */
228#define I_HMB_FRAME_IND I_HMB_SW2 /* Frame Indication */
229#define I_HMB_HOST_INT I_HMB_SW3 /* Miscellaneous Interrupt */
230
231/* tohostmailboxdata */
232#define HMB_DATA_NAKHANDLED 1 /* retransmit NAK'd frame */
233#define HMB_DATA_DEVREADY 2 /* talk to host after enable */
234#define HMB_DATA_FC 4 /* per prio flowcontrol update flag */
235#define HMB_DATA_FWREADY 8 /* fw ready for protocol activity */
236
237#define HMB_DATA_FCDATA_MASK 0xff000000
238#define HMB_DATA_FCDATA_SHIFT 24
239
240#define HMB_DATA_VERSION_MASK 0x00ff0000
241#define HMB_DATA_VERSION_SHIFT 16
242
243/*
244 * Software-defined protocol header
245 */
246
247/* Current protocol version */
248#define SDPCM_PROT_VERSION 4
249
Arend van Spriel5b435de2011-10-05 13:19:03 +0200250/*
251 * Shared structure between dongle and the host.
252 * The structure contains pointers to trap or assert information.
253 */
Arend van Spriel4fc0d012012-06-14 12:16:57 +0200254#define SDPCM_SHARED_VERSION 0x0003
Arend van Spriel5b435de2011-10-05 13:19:03 +0200255#define SDPCM_SHARED_VERSION_MASK 0x00FF
256#define SDPCM_SHARED_ASSERT_BUILT 0x0100
257#define SDPCM_SHARED_ASSERT 0x0200
258#define SDPCM_SHARED_TRAP 0x0400
259
260/* Space for header read, limit for data packets */
261#define MAX_HDR_READ (1 << 6)
262#define MAX_RX_DATASZ 2048
263
Arend van Spriel5b435de2011-10-05 13:19:03 +0200264/* Bump up limit on waiting for HT to account for first startup;
265 * if the image is doing a CRC calculation before programming the PMU
266 * for HT availability, it could take a couple hundred ms more, so
267 * max out at a 1 second (1000000us).
268 */
269#undef PMU_MAX_TRANSITION_DLY
270#define PMU_MAX_TRANSITION_DLY 1000000
271
272/* Value for ChipClockCSR during initial setup */
273#define BRCMF_INIT_CLKCTL1 (SBSDIO_FORCE_HW_CLKREQ_OFF | \
274 SBSDIO_ALP_AVAIL_REQ)
275
276/* Flags for SDH calls */
277#define F2SYNC (SDIO_REQ_4BYTE | SDIO_REQ_FIXED)
278
Franky Lin382a9e02011-12-16 18:37:18 -0800279#define BRCMF_IDLE_IMMEDIATE (-1) /* Enter idle immediately */
280#define BRCMF_IDLE_ACTIVE 0 /* Do not request any SD clock change
281 * when idle
282 */
283#define BRCMF_IDLE_INTERVAL 1
284
Piotr Haber4a3da992013-04-11 13:28:46 +0200285#define KSO_WAIT_US 50
286#define MAX_KSO_ATTEMPTS (PMU_MAX_TRANSITION_DLY/KSO_WAIT_US)
287
Arend van Spriel5b435de2011-10-05 13:19:03 +0200288/*
289 * Conversion of 802.1D priority to precedence level
290 */
291static uint prio2prec(u32 prio)
292{
293 return (prio == PRIO_8021D_NONE || prio == PRIO_8021D_BE) ?
294 (prio^2) : prio;
295}
296
Joe Perches8ae74652012-01-15 00:38:38 -0800297#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200298/* Device console log buffer state */
299struct brcmf_console {
300 uint count; /* Poll interval msec counter */
301 uint log_addr; /* Log struct address (fixed) */
302 struct rte_log_le log_le; /* Log struct (host copy) */
303 uint bufsize; /* Size of log buffer */
304 u8 *buf; /* Log buffer (host copy) */
305 uint last; /* Last buffer read index */
306};
Arend van Spriel4fc0d012012-06-14 12:16:57 +0200307
308struct brcmf_trap_info {
309 __le32 type;
310 __le32 epc;
311 __le32 cpsr;
312 __le32 spsr;
313 __le32 r0; /* a1 */
314 __le32 r1; /* a2 */
315 __le32 r2; /* a3 */
316 __le32 r3; /* a4 */
317 __le32 r4; /* v1 */
318 __le32 r5; /* v2 */
319 __le32 r6; /* v3 */
320 __le32 r7; /* v4 */
321 __le32 r8; /* v5 */
322 __le32 r9; /* sb/v6 */
323 __le32 r10; /* sl/v7 */
324 __le32 r11; /* fp/v8 */
325 __le32 r12; /* ip */
326 __le32 r13; /* sp */
327 __le32 r14; /* lr */
328 __le32 pc; /* r15 */
329};
Joe Perches8ae74652012-01-15 00:38:38 -0800330#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200331
332struct sdpcm_shared {
333 u32 flags;
334 u32 trap_addr;
335 u32 assert_exp_addr;
336 u32 assert_file_addr;
337 u32 assert_line;
338 u32 console_addr; /* Address of struct rte_console */
339 u32 msgtrace_addr;
340 u8 tag[32];
Arend van Spriel4fc0d012012-06-14 12:16:57 +0200341 u32 brpt_addr;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200342};
343
344struct sdpcm_shared_le {
345 __le32 flags;
346 __le32 trap_addr;
347 __le32 assert_exp_addr;
348 __le32 assert_file_addr;
349 __le32 assert_line;
350 __le32 console_addr; /* Address of struct rte_console */
351 __le32 msgtrace_addr;
352 u8 tag[32];
Arend van Spriel4fc0d012012-06-14 12:16:57 +0200353 __le32 brpt_addr;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200354};
355
Franky Lin6bc52312013-08-10 12:27:28 +0200356/* dongle SDIO bus specific header info */
357struct brcmf_sdio_hdrinfo {
Franky Lin4754fce2012-09-19 22:21:11 +0200358 u8 seq_num;
359 u8 channel;
360 u16 len;
361 u16 len_left;
362 u16 len_nxtfrm;
363 u8 dat_offset;
Franky Lin8da9d2c2013-11-29 12:25:10 +0100364 bool lastfrm;
365 u16 tail_pad;
Franky Lin4754fce2012-09-19 22:21:11 +0200366};
Arend van Spriel5b435de2011-10-05 13:19:03 +0200367
368/* misc chip info needed by some of the routines */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200369/* Private data for SDIO bus interaction */
Franky Line92eedf2011-11-22 17:21:50 -0800370struct brcmf_sdio {
Arend van Spriel5b435de2011-10-05 13:19:03 +0200371 struct brcmf_sdio_dev *sdiodev; /* sdio device handler */
Arend van Spriel9cf218f2014-01-13 22:20:27 +0100372 struct brcmf_chip *ci; /* Chip info struct */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200373
374 u32 ramsize; /* Size of RAM in SOCRAM (bytes) */
375
376 u32 hostintmask; /* Copy of Host Interrupt Mask */
Franky Lin45316032012-09-13 21:12:03 +0200377 atomic_t intstatus; /* Intstatus bits (events) pending */
378 atomic_t fcstate; /* State of dongle flow-control */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200379
380 uint blocksize; /* Block size of SDIO transfers */
381 uint roundup; /* Max roundup limit */
382
383 struct pktq txq; /* Queue length used for flow-control */
384 u8 flowcontrol; /* per prio flow control bitmask */
385 u8 tx_seq; /* Transmit sequence number (next) */
386 u8 tx_max; /* Maximum transmit sequence allowed */
387
Arend van Spriel9b2d2f22013-11-29 12:25:14 +0100388 u8 *hdrbuf; /* buffer for handling rx frame */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200389 u8 *rxhdr; /* Header of current rx frame (in hdrbuf) */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200390 u8 rx_seq; /* Receive sequence number (expected) */
Franky Lin6bc52312013-08-10 12:27:28 +0200391 struct brcmf_sdio_hdrinfo cur_read;
Franky Lin4754fce2012-09-19 22:21:11 +0200392 /* info of current read frame */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200393 bool rxskip; /* Skip receive (awaiting NAK ACK) */
Franky Lin4754fce2012-09-19 22:21:11 +0200394 bool rxpending; /* Data frame pending in dongle */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200395
396 uint rxbound; /* Rx frames to read before resched */
397 uint txbound; /* Tx frames to send before resched */
398 uint txminmax;
399
400 struct sk_buff *glomd; /* Packet containing glomming descriptor */
Arend van Sprielb83db862011-10-19 12:51:09 +0200401 struct sk_buff_head glom; /* Packet list for glommed superframe */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200402 uint glomerr; /* Glom packet read errors */
403
404 u8 *rxbuf; /* Buffer for receiving control packets */
405 uint rxblen; /* Allocated length of rxbuf */
406 u8 *rxctl; /* Aligned pointer into rxbuf */
Franky Lindd43a012012-11-05 16:22:22 -0800407 u8 *rxctl_orig; /* pointer for freeing rxctl */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200408 uint rxlen; /* Length of valid data in buffer */
Franky Lindd43a012012-11-05 16:22:22 -0800409 spinlock_t rxctl_lock; /* protection lock for ctrl frame resources */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200410
411 u8 sdpcm_ver; /* Bus protocol reported by dongle */
412
413 bool intr; /* Use interrupts */
414 bool poll; /* Use polling */
Franky Lin1d382272012-09-13 21:11:59 +0200415 atomic_t ipend; /* Device interrupt is pending */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200416 uint spurious; /* Count of spurious interrupts */
417 uint pollrate; /* Ticks between device polls */
418 uint polltick; /* Tick counter */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200419
Joe Perches8ae74652012-01-15 00:38:38 -0800420#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200421 uint console_interval;
422 struct brcmf_console console; /* Console output polling support */
423 uint console_addr; /* Console address from shared struct */
Joe Perches8ae74652012-01-15 00:38:38 -0800424#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200425
Arend van Spriel5b435de2011-10-05 13:19:03 +0200426 uint clkstate; /* State of sd and backplane clock(s) */
427 bool activity; /* Activity flag for clock down */
428 s32 idletime; /* Control for activity timeout */
429 s32 idlecount; /* Activity timeout counter */
430 s32 idleclock; /* How to set bus driver when idle */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200431 bool rxflow_mode; /* Rx flow control mode */
432 bool rxflow; /* Is rx flow control on */
433 bool alp_only; /* Don't use HT clock (ALP only) */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200434
Arend van Spriel5b435de2011-10-05 13:19:03 +0200435 u8 *ctrl_frame_buf;
436 u32 ctrl_frame_len;
437 bool ctrl_frame_stat;
438
439 spinlock_t txqlock;
440 wait_queue_head_t ctrl_wait;
441 wait_queue_head_t dcmd_resp_wait;
442
443 struct timer_list timer;
444 struct completion watchdog_wait;
445 struct task_struct *watchdog_tsk;
446 bool wd_timer_valid;
447 uint save_ms;
448
Franky Linf1e68c22012-09-13 21:12:00 +0200449 struct workqueue_struct *brcmf_wq;
450 struct work_struct datawork;
Arend van Sprielfccfe932013-06-18 13:29:26 +0200451 atomic_t dpc_tskcnt;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200452
Franky Linc8bf3482011-12-16 18:37:08 -0800453 bool txoff; /* Transmit flow-controlled */
Arend van Spriel80969832012-06-09 22:51:44 +0200454 struct brcmf_sdio_count sdcnt;
Piotr Haber4a3da992013-04-11 13:28:46 +0200455 bool sr_enabled; /* SaveRestore enabled */
456 bool sleeping; /* SDIO bus sleeping */
Franky Lin706478c2013-08-10 12:27:29 +0200457
458 u8 tx_hdrlen; /* sdio bus header length for tx packet */
Franky Lin8da9d2c2013-11-29 12:25:10 +0100459 bool txglom; /* host tx glomming enable flag */
Arend van Spriele217d1c2013-11-29 12:25:13 +0100460 u16 head_align; /* buffer pointer alignment */
461 u16 sgentry_align; /* scatter-gather buffer alignment */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200462};
463
Arend van Spriel5b435de2011-10-05 13:19:03 +0200464/* clkstate */
465#define CLK_NONE 0
466#define CLK_SDONLY 1
Piotr Haber4a3da992013-04-11 13:28:46 +0200467#define CLK_PENDING 2
Arend van Spriel5b435de2011-10-05 13:19:03 +0200468#define CLK_AVAIL 3
469
Joe Perches8ae74652012-01-15 00:38:38 -0800470#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200471static int qcount[NUMPRIO];
Joe Perches8ae74652012-01-15 00:38:38 -0800472#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200473
Hante Meuleman668761a2013-04-12 10:55:55 +0200474#define DEFAULT_SDIO_DRIVE_STRENGTH 6 /* in milliamps */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200475
476#define RETRYCHAN(chan) ((chan) == SDPCM_EVENT_CHANNEL)
477
478/* Retry count for register access failures */
479static const uint retry_limit = 2;
480
481/* Limit on rounding up frames */
482static const uint max_roundup = 512;
483
484#define ALIGNMENT 4
485
Franky Lin8da9d2c2013-11-29 12:25:10 +0100486static int brcmf_sdio_txglomsz = BRCMF_DEFAULT_TXGLOM_SIZE;
487module_param_named(txglomsz, brcmf_sdio_txglomsz, int, 0);
488MODULE_PARM_DESC(txglomsz, "maximum tx packet chain size [SDIO]");
489
Franky Lin9d7d6f92012-10-22 10:36:24 -0700490enum brcmf_sdio_frmtype {
491 BRCMF_SDIO_FT_NORMAL,
492 BRCMF_SDIO_FT_SUPER,
493 BRCMF_SDIO_FT_SUB,
494};
495
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200496#define BCM43143_FIRMWARE_NAME "brcm/brcmfmac43143-sdio.bin"
497#define BCM43143_NVRAM_NAME "brcm/brcmfmac43143-sdio.txt"
498#define BCM43241B0_FIRMWARE_NAME "brcm/brcmfmac43241b0-sdio.bin"
499#define BCM43241B0_NVRAM_NAME "brcm/brcmfmac43241b0-sdio.txt"
500#define BCM43241B4_FIRMWARE_NAME "brcm/brcmfmac43241b4-sdio.bin"
501#define BCM43241B4_NVRAM_NAME "brcm/brcmfmac43241b4-sdio.txt"
502#define BCM4329_FIRMWARE_NAME "brcm/brcmfmac4329-sdio.bin"
503#define BCM4329_NVRAM_NAME "brcm/brcmfmac4329-sdio.txt"
504#define BCM4330_FIRMWARE_NAME "brcm/brcmfmac4330-sdio.bin"
505#define BCM4330_NVRAM_NAME "brcm/brcmfmac4330-sdio.txt"
506#define BCM4334_FIRMWARE_NAME "brcm/brcmfmac4334-sdio.bin"
507#define BCM4334_NVRAM_NAME "brcm/brcmfmac4334-sdio.txt"
508#define BCM4335_FIRMWARE_NAME "brcm/brcmfmac4335-sdio.bin"
509#define BCM4335_NVRAM_NAME "brcm/brcmfmac4335-sdio.txt"
Arend van Spriel11e69c32014-01-08 10:49:33 +0100510#define BCM43362_FIRMWARE_NAME "brcm/brcmfmac43362-sdio.bin"
511#define BCM43362_NVRAM_NAME "brcm/brcmfmac43362-sdio.txt"
Franky Linbed89b62013-11-29 12:25:09 +0100512#define BCM4339_FIRMWARE_NAME "brcm/brcmfmac4339-sdio.bin"
513#define BCM4339_NVRAM_NAME "brcm/brcmfmac4339-sdio.txt"
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200514
515MODULE_FIRMWARE(BCM43143_FIRMWARE_NAME);
516MODULE_FIRMWARE(BCM43143_NVRAM_NAME);
517MODULE_FIRMWARE(BCM43241B0_FIRMWARE_NAME);
518MODULE_FIRMWARE(BCM43241B0_NVRAM_NAME);
519MODULE_FIRMWARE(BCM43241B4_FIRMWARE_NAME);
520MODULE_FIRMWARE(BCM43241B4_NVRAM_NAME);
521MODULE_FIRMWARE(BCM4329_FIRMWARE_NAME);
522MODULE_FIRMWARE(BCM4329_NVRAM_NAME);
523MODULE_FIRMWARE(BCM4330_FIRMWARE_NAME);
524MODULE_FIRMWARE(BCM4330_NVRAM_NAME);
525MODULE_FIRMWARE(BCM4334_FIRMWARE_NAME);
526MODULE_FIRMWARE(BCM4334_NVRAM_NAME);
527MODULE_FIRMWARE(BCM4335_FIRMWARE_NAME);
528MODULE_FIRMWARE(BCM4335_NVRAM_NAME);
Arend van Spriel11e69c32014-01-08 10:49:33 +0100529MODULE_FIRMWARE(BCM43362_FIRMWARE_NAME);
530MODULE_FIRMWARE(BCM43362_NVRAM_NAME);
Franky Linbed89b62013-11-29 12:25:09 +0100531MODULE_FIRMWARE(BCM4339_FIRMWARE_NAME);
532MODULE_FIRMWARE(BCM4339_NVRAM_NAME);
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200533
534struct brcmf_firmware_names {
535 u32 chipid;
536 u32 revmsk;
537 const char *bin;
538 const char *nv;
539};
540
541enum brcmf_firmware_type {
542 BRCMF_FIRMWARE_BIN,
543 BRCMF_FIRMWARE_NVRAM
544};
545
546#define BRCMF_FIRMWARE_NVRAM(name) \
547 name ## _FIRMWARE_NAME, name ## _NVRAM_NAME
548
549static const struct brcmf_firmware_names brcmf_fwname_data[] = {
550 { BCM43143_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM43143) },
551 { BCM43241_CHIP_ID, 0x0000001F, BRCMF_FIRMWARE_NVRAM(BCM43241B0) },
552 { BCM43241_CHIP_ID, 0xFFFFFFE0, BRCMF_FIRMWARE_NVRAM(BCM43241B4) },
553 { BCM4329_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM4329) },
554 { BCM4330_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM4330) },
555 { BCM4334_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM4334) },
Franky Linbed89b62013-11-29 12:25:09 +0100556 { BCM4335_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM4335) },
Arend van Spriel11e69c32014-01-08 10:49:33 +0100557 { BCM43362_CHIP_ID, 0xFFFFFFFE, BRCMF_FIRMWARE_NVRAM(BCM43362) },
Franky Linbed89b62013-11-29 12:25:09 +0100558 { BCM4339_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM4339) }
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200559};
560
561
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100562static const struct firmware *brcmf_sdio_get_fw(struct brcmf_sdio *bus,
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200563 enum brcmf_firmware_type type)
564{
565 const struct firmware *fw;
566 const char *name;
567 int err, i;
568
569 for (i = 0; i < ARRAY_SIZE(brcmf_fwname_data); i++) {
570 if (brcmf_fwname_data[i].chipid == bus->ci->chip &&
571 brcmf_fwname_data[i].revmsk & BIT(bus->ci->chiprev)) {
572 switch (type) {
573 case BRCMF_FIRMWARE_BIN:
574 name = brcmf_fwname_data[i].bin;
575 break;
576 case BRCMF_FIRMWARE_NVRAM:
577 name = brcmf_fwname_data[i].nv;
578 break;
579 default:
580 brcmf_err("invalid firmware type (%d)\n", type);
581 return NULL;
582 }
583 goto found;
584 }
585 }
586 brcmf_err("Unknown chipid %d [%d]\n",
587 bus->ci->chip, bus->ci->chiprev);
588 return NULL;
589
590found:
591 err = request_firmware(&fw, name, &bus->sdiodev->func[2]->dev);
592 if ((err) || (!fw)) {
593 brcmf_err("fail to request firmware %s (%d)\n", name, err);
594 return NULL;
595 }
596
597 return fw;
598}
599
Arend van Spriel5b435de2011-10-05 13:19:03 +0200600static void pkt_align(struct sk_buff *p, int len, int align)
601{
602 uint datalign;
603 datalign = (unsigned long)(p->data);
604 datalign = roundup(datalign, (align)) - datalign;
605 if (datalign)
606 skb_pull(p, datalign);
607 __skb_trim(p, len);
608}
609
610/* To check if there's window offered */
Franky Line92eedf2011-11-22 17:21:50 -0800611static bool data_ok(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200612{
613 return (u8)(bus->tx_max - bus->tx_seq) != 0 &&
614 ((u8)(bus->tx_max - bus->tx_seq) & 0x80) == 0;
615}
616
617/*
618 * Reads a register in the SDIO hardware block. This block occupies a series of
619 * adresses on the 32 bit backplane bus.
620 */
Franky Lin58692752012-05-04 18:27:36 -0700621static int
622r_sdreg32(struct brcmf_sdio *bus, u32 *regvar, u32 offset)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200623{
Franky Lin99ba15c2011-11-04 22:23:42 +0100624 u8 idx = brcmf_sdio_chip_getinfidx(bus->ci, BCMA_CORE_SDIO_DEV);
Franky Lin79ae3952012-05-04 18:27:34 -0700625 int ret;
Franky Lin58692752012-05-04 18:27:36 -0700626
Arend van Spriela39be272013-12-12 11:58:58 +0100627 *regvar = brcmf_sdiod_regrl(bus->sdiodev,
628 bus->ci->c_inf[idx].base + offset, &ret);
Franky Lin58692752012-05-04 18:27:36 -0700629
630 return ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200631}
632
Franky Lin58692752012-05-04 18:27:36 -0700633static int
634w_sdreg32(struct brcmf_sdio *bus, u32 regval, u32 reg_offset)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200635{
Franky Lin99ba15c2011-11-04 22:23:42 +0100636 u8 idx = brcmf_sdio_chip_getinfidx(bus->ci, BCMA_CORE_SDIO_DEV);
Franky Line13ce262012-05-04 18:27:35 -0700637 int ret;
Franky Lin58692752012-05-04 18:27:36 -0700638
Arend van Spriela39be272013-12-12 11:58:58 +0100639 brcmf_sdiod_regwl(bus->sdiodev,
640 bus->ci->c_inf[idx].base + reg_offset,
641 regval, &ret);
Franky Lin58692752012-05-04 18:27:36 -0700642
643 return ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200644}
645
Piotr Haber4a3da992013-04-11 13:28:46 +0200646static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100647brcmf_sdio_kso_control(struct brcmf_sdio *bus, bool on)
Piotr Haber4a3da992013-04-11 13:28:46 +0200648{
649 u8 wr_val = 0, rd_val, cmp_val, bmask;
650 int err = 0;
651 int try_cnt = 0;
652
653 brcmf_dbg(TRACE, "Enter\n");
654
655 wr_val = (on << SBSDIO_FUNC1_SLEEPCSR_KSO_SHIFT);
656 /* 1st KSO write goes to AOS wake up core if device is asleep */
Arend van Spriela39be272013-12-12 11:58:58 +0100657 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
658 wr_val, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +0200659 if (err) {
660 brcmf_err("SDIO_AOS KSO write error: %d\n", err);
661 return err;
662 }
663
664 if (on) {
665 /* device WAKEUP through KSO:
666 * write bit 0 & read back until
667 * both bits 0 (kso bit) & 1 (dev on status) are set
668 */
669 cmp_val = SBSDIO_FUNC1_SLEEPCSR_KSO_MASK |
670 SBSDIO_FUNC1_SLEEPCSR_DEVON_MASK;
671 bmask = cmp_val;
672 usleep_range(2000, 3000);
673 } else {
674 /* Put device to sleep, turn off KSO */
675 cmp_val = 0;
676 /* only check for bit0, bit1(dev on status) may not
677 * get cleared right away
678 */
679 bmask = SBSDIO_FUNC1_SLEEPCSR_KSO_MASK;
680 }
681
682 do {
683 /* reliable KSO bit set/clr:
684 * the sdiod sleep write access is synced to PMU 32khz clk
685 * just one write attempt may fail,
686 * read it back until it matches written value
687 */
Arend van Spriela39be272013-12-12 11:58:58 +0100688 rd_val = brcmf_sdiod_regrb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
689 &err);
Piotr Haber4a3da992013-04-11 13:28:46 +0200690 if (((rd_val & bmask) == cmp_val) && !err)
691 break;
692 brcmf_dbg(SDIO, "KSO wr/rd retry:%d (max: %d) ERR:%x\n",
693 try_cnt, MAX_KSO_ATTEMPTS, err);
694 udelay(KSO_WAIT_US);
Arend van Spriela39be272013-12-12 11:58:58 +0100695 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
696 wr_val, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +0200697 } while (try_cnt++ < MAX_KSO_ATTEMPTS);
698
699 return err;
700}
701
Arend van Spriel5b435de2011-10-05 13:19:03 +0200702#define PKT_AVAILABLE() (intstatus & I_HMB_FRAME_IND)
703
704#define HOSTINTMASK (I_HMB_SW_MASK | I_CHIPACTIVE)
705
Arend van Spriel5b435de2011-10-05 13:19:03 +0200706/* Turn backplane clock on or off */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100707static int brcmf_sdio_htclk(struct brcmf_sdio *bus, bool on, bool pendok)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200708{
709 int err;
710 u8 clkctl, clkreq, devctl;
711 unsigned long timeout;
712
Arend van Sprielc3203372013-04-03 12:40:44 +0200713 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200714
715 clkctl = 0;
716
Piotr Haber4a3da992013-04-11 13:28:46 +0200717 if (bus->sr_enabled) {
718 bus->clkstate = (on ? CLK_AVAIL : CLK_SDONLY);
719 return 0;
720 }
721
Arend van Spriel5b435de2011-10-05 13:19:03 +0200722 if (on) {
723 /* Request HT Avail */
724 clkreq =
725 bus->alp_only ? SBSDIO_ALP_AVAIL_REQ : SBSDIO_HT_AVAIL_REQ;
726
Arend van Spriela39be272013-12-12 11:58:58 +0100727 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
728 clkreq, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200729 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100730 brcmf_err("HT Avail request error: %d\n", err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200731 return -EBADE;
732 }
733
Arend van Spriel5b435de2011-10-05 13:19:03 +0200734 /* Check current status */
Arend van Spriela39be272013-12-12 11:58:58 +0100735 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
736 SBSDIO_FUNC1_CHIPCLKCSR, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200737 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100738 brcmf_err("HT Avail read error: %d\n", err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200739 return -EBADE;
740 }
741
742 /* Go to pending and await interrupt if appropriate */
743 if (!SBSDIO_CLKAV(clkctl, bus->alp_only) && pendok) {
744 /* Allow only clock-available interrupt */
Arend van Spriela39be272013-12-12 11:58:58 +0100745 devctl = brcmf_sdiod_regrb(bus->sdiodev,
746 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200747 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100748 brcmf_err("Devctl error setting CA: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200749 err);
750 return -EBADE;
751 }
752
753 devctl |= SBSDIO_DEVCTL_CA_INT_ONLY;
Arend van Spriela39be272013-12-12 11:58:58 +0100754 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
755 devctl, &err);
Arend van Sprielc3203372013-04-03 12:40:44 +0200756 brcmf_dbg(SDIO, "CLKCTL: set PENDING\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200757 bus->clkstate = CLK_PENDING;
758
759 return 0;
760 } else if (bus->clkstate == CLK_PENDING) {
761 /* Cancel CA-only interrupt filter */
Arend van Spriela39be272013-12-12 11:58:58 +0100762 devctl = brcmf_sdiod_regrb(bus->sdiodev,
763 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200764 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
Arend van Spriela39be272013-12-12 11:58:58 +0100765 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
766 devctl, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200767 }
768
769 /* Otherwise, wait here (polling) for HT Avail */
770 timeout = jiffies +
771 msecs_to_jiffies(PMU_MAX_TRANSITION_DLY/1000);
772 while (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
Arend van Spriela39be272013-12-12 11:58:58 +0100773 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
774 SBSDIO_FUNC1_CHIPCLKCSR,
775 &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200776 if (time_after(jiffies, timeout))
777 break;
778 else
779 usleep_range(5000, 10000);
780 }
781 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100782 brcmf_err("HT Avail request error: %d\n", err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200783 return -EBADE;
784 }
785 if (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100786 brcmf_err("HT Avail timeout (%d): clkctl 0x%02x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200787 PMU_MAX_TRANSITION_DLY, clkctl);
788 return -EBADE;
789 }
790
791 /* Mark clock available */
792 bus->clkstate = CLK_AVAIL;
Arend van Sprielc3203372013-04-03 12:40:44 +0200793 brcmf_dbg(SDIO, "CLKCTL: turned ON\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200794
Joe Perches8ae74652012-01-15 00:38:38 -0800795#if defined(DEBUG)
Joe Perches23677ce2012-02-09 11:17:23 +0000796 if (!bus->alp_only) {
Arend van Spriel5b435de2011-10-05 13:19:03 +0200797 if (SBSDIO_ALPONLY(clkctl))
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100798 brcmf_err("HT Clock should be on\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200799 }
Joe Perches8ae74652012-01-15 00:38:38 -0800800#endif /* defined (DEBUG) */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200801
802 bus->activity = true;
803 } else {
804 clkreq = 0;
805
806 if (bus->clkstate == CLK_PENDING) {
807 /* Cancel CA-only interrupt filter */
Arend van Spriela39be272013-12-12 11:58:58 +0100808 devctl = brcmf_sdiod_regrb(bus->sdiodev,
809 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200810 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
Arend van Spriela39be272013-12-12 11:58:58 +0100811 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
812 devctl, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200813 }
814
815 bus->clkstate = CLK_SDONLY;
Arend van Spriela39be272013-12-12 11:58:58 +0100816 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
817 clkreq, &err);
Arend van Sprielc3203372013-04-03 12:40:44 +0200818 brcmf_dbg(SDIO, "CLKCTL: turned OFF\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200819 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100820 brcmf_err("Failed access turning clock off: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200821 err);
822 return -EBADE;
823 }
824 }
825 return 0;
826}
827
828/* Change idle/active SD state */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100829static int brcmf_sdio_sdclk(struct brcmf_sdio *bus, bool on)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200830{
Arend van Sprielc3203372013-04-03 12:40:44 +0200831 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200832
833 if (on)
834 bus->clkstate = CLK_SDONLY;
835 else
836 bus->clkstate = CLK_NONE;
837
838 return 0;
839}
840
841/* Transition SD and backplane clock readiness */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100842static int brcmf_sdio_clkctl(struct brcmf_sdio *bus, uint target, bool pendok)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200843{
Joe Perches8ae74652012-01-15 00:38:38 -0800844#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200845 uint oldstate = bus->clkstate;
Joe Perches8ae74652012-01-15 00:38:38 -0800846#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200847
Arend van Sprielc3203372013-04-03 12:40:44 +0200848 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200849
850 /* Early exit if we're already there */
851 if (bus->clkstate == target) {
852 if (target == CLK_AVAIL) {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100853 brcmf_sdio_wd_timer(bus, BRCMF_WD_POLL_MS);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200854 bus->activity = true;
855 }
856 return 0;
857 }
858
859 switch (target) {
860 case CLK_AVAIL:
861 /* Make sure SD clock is available */
862 if (bus->clkstate == CLK_NONE)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100863 brcmf_sdio_sdclk(bus, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200864 /* Now request HT Avail on the backplane */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100865 brcmf_sdio_htclk(bus, true, pendok);
866 brcmf_sdio_wd_timer(bus, BRCMF_WD_POLL_MS);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200867 bus->activity = true;
868 break;
869
870 case CLK_SDONLY:
871 /* Remove HT request, or bring up SD clock */
872 if (bus->clkstate == CLK_NONE)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100873 brcmf_sdio_sdclk(bus, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200874 else if (bus->clkstate == CLK_AVAIL)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100875 brcmf_sdio_htclk(bus, false, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200876 else
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100877 brcmf_err("request for %d -> %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200878 bus->clkstate, target);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100879 brcmf_sdio_wd_timer(bus, BRCMF_WD_POLL_MS);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200880 break;
881
882 case CLK_NONE:
883 /* Make sure to remove HT request */
884 if (bus->clkstate == CLK_AVAIL)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100885 brcmf_sdio_htclk(bus, false, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200886 /* Now remove the SD clock */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100887 brcmf_sdio_sdclk(bus, false);
888 brcmf_sdio_wd_timer(bus, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200889 break;
890 }
Joe Perches8ae74652012-01-15 00:38:38 -0800891#ifdef DEBUG
Arend van Sprielc3203372013-04-03 12:40:44 +0200892 brcmf_dbg(SDIO, "%d -> %d\n", oldstate, bus->clkstate);
Joe Perches8ae74652012-01-15 00:38:38 -0800893#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200894
895 return 0;
896}
897
Piotr Haber4a3da992013-04-11 13:28:46 +0200898static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100899brcmf_sdio_bus_sleep(struct brcmf_sdio *bus, bool sleep, bool pendok)
Piotr Haber4a3da992013-04-11 13:28:46 +0200900{
901 int err = 0;
902 brcmf_dbg(TRACE, "Enter\n");
903 brcmf_dbg(SDIO, "request %s currently %s\n",
904 (sleep ? "SLEEP" : "WAKE"),
905 (bus->sleeping ? "SLEEP" : "WAKE"));
906
907 /* If SR is enabled control bus state with KSO */
908 if (bus->sr_enabled) {
909 /* Done if we're already in the requested state */
910 if (sleep == bus->sleeping)
911 goto end;
912
913 /* Going to sleep */
914 if (sleep) {
915 /* Don't sleep if something is pending */
916 if (atomic_read(&bus->intstatus) ||
917 atomic_read(&bus->ipend) > 0 ||
918 (!atomic_read(&bus->fcstate) &&
919 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) &&
920 data_ok(bus)))
921 return -EBUSY;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100922 err = brcmf_sdio_kso_control(bus, false);
Piotr Haber4a3da992013-04-11 13:28:46 +0200923 /* disable watchdog */
924 if (!err)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100925 brcmf_sdio_wd_timer(bus, 0);
Piotr Haber4a3da992013-04-11 13:28:46 +0200926 } else {
927 bus->idlecount = 0;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100928 err = brcmf_sdio_kso_control(bus, true);
Piotr Haber4a3da992013-04-11 13:28:46 +0200929 }
930 if (!err) {
931 /* Change state */
932 bus->sleeping = sleep;
933 brcmf_dbg(SDIO, "new state %s\n",
934 (sleep ? "SLEEP" : "WAKE"));
935 } else {
936 brcmf_err("error while changing bus sleep state %d\n",
937 err);
938 return err;
939 }
940 }
941
942end:
943 /* control clocks */
944 if (sleep) {
945 if (!bus->sr_enabled)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100946 brcmf_sdio_clkctl(bus, CLK_NONE, pendok);
Piotr Haber4a3da992013-04-11 13:28:46 +0200947 } else {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100948 brcmf_sdio_clkctl(bus, CLK_AVAIL, pendok);
Piotr Haber4a3da992013-04-11 13:28:46 +0200949 }
950
951 return err;
952
953}
954
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100955static u32 brcmf_sdio_hostmail(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200956{
957 u32 intstatus = 0;
958 u32 hmb_data;
959 u8 fcbits;
Franky Lin58692752012-05-04 18:27:36 -0700960 int ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200961
Arend van Sprielc3203372013-04-03 12:40:44 +0200962 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200963
964 /* Read mailbox data and ack that we did so */
Franky Lin58692752012-05-04 18:27:36 -0700965 ret = r_sdreg32(bus, &hmb_data,
966 offsetof(struct sdpcmd_regs, tohostmailboxdata));
Arend van Spriel5b435de2011-10-05 13:19:03 +0200967
Franky Lin58692752012-05-04 18:27:36 -0700968 if (ret == 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200969 w_sdreg32(bus, SMB_INT_ACK,
Franky Lin58692752012-05-04 18:27:36 -0700970 offsetof(struct sdpcmd_regs, tosbmailbox));
Arend van Spriel80969832012-06-09 22:51:44 +0200971 bus->sdcnt.f1regdata += 2;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200972
973 /* Dongle recomposed rx frames, accept them again */
974 if (hmb_data & HMB_DATA_NAKHANDLED) {
Arend van Sprielc3203372013-04-03 12:40:44 +0200975 brcmf_dbg(SDIO, "Dongle reports NAK handled, expect rtx of %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200976 bus->rx_seq);
977 if (!bus->rxskip)
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100978 brcmf_err("unexpected NAKHANDLED!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200979
980 bus->rxskip = false;
981 intstatus |= I_HMB_FRAME_IND;
982 }
983
984 /*
985 * DEVREADY does not occur with gSPI.
986 */
987 if (hmb_data & (HMB_DATA_DEVREADY | HMB_DATA_FWREADY)) {
988 bus->sdpcm_ver =
989 (hmb_data & HMB_DATA_VERSION_MASK) >>
990 HMB_DATA_VERSION_SHIFT;
991 if (bus->sdpcm_ver != SDPCM_PROT_VERSION)
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100992 brcmf_err("Version mismatch, dongle reports %d, "
Arend van Spriel5b435de2011-10-05 13:19:03 +0200993 "expecting %d\n",
994 bus->sdpcm_ver, SDPCM_PROT_VERSION);
995 else
Arend van Sprielc3203372013-04-03 12:40:44 +0200996 brcmf_dbg(SDIO, "Dongle ready, protocol version %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200997 bus->sdpcm_ver);
998 }
999
1000 /*
1001 * Flow Control has been moved into the RX headers and this out of band
1002 * method isn't used any more.
1003 * remaining backward compatible with older dongles.
1004 */
1005 if (hmb_data & HMB_DATA_FC) {
1006 fcbits = (hmb_data & HMB_DATA_FCDATA_MASK) >>
1007 HMB_DATA_FCDATA_SHIFT;
1008
1009 if (fcbits & ~bus->flowcontrol)
Arend van Spriel80969832012-06-09 22:51:44 +02001010 bus->sdcnt.fc_xoff++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001011
1012 if (bus->flowcontrol & ~fcbits)
Arend van Spriel80969832012-06-09 22:51:44 +02001013 bus->sdcnt.fc_xon++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001014
Arend van Spriel80969832012-06-09 22:51:44 +02001015 bus->sdcnt.fc_rcvd++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001016 bus->flowcontrol = fcbits;
1017 }
1018
1019 /* Shouldn't be any others */
1020 if (hmb_data & ~(HMB_DATA_DEVREADY |
1021 HMB_DATA_NAKHANDLED |
1022 HMB_DATA_FC |
1023 HMB_DATA_FWREADY |
1024 HMB_DATA_FCDATA_MASK | HMB_DATA_VERSION_MASK))
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001025 brcmf_err("Unknown mailbox data content: 0x%02x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001026 hmb_data);
1027
1028 return intstatus;
1029}
1030
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001031static void brcmf_sdio_rxfail(struct brcmf_sdio *bus, bool abort, bool rtx)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001032{
1033 uint retries = 0;
1034 u16 lastrbc;
1035 u8 hi, lo;
1036 int err;
1037
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001038 brcmf_err("%sterminate frame%s\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001039 abort ? "abort command, " : "",
1040 rtx ? ", send NAK" : "");
1041
1042 if (abort)
Arend van Spriela39be272013-12-12 11:58:58 +01001043 brcmf_sdiod_abort(bus->sdiodev, SDIO_FUNC_2);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001044
Arend van Spriela39be272013-12-12 11:58:58 +01001045 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL,
1046 SFC_RF_TERM, &err);
Arend van Spriel80969832012-06-09 22:51:44 +02001047 bus->sdcnt.f1regdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001048
1049 /* Wait until the packet has been flushed (device/FIFO stable) */
1050 for (lastrbc = retries = 0xffff; retries > 0; retries--) {
Arend van Spriela39be272013-12-12 11:58:58 +01001051 hi = brcmf_sdiod_regrb(bus->sdiodev,
1052 SBSDIO_FUNC1_RFRAMEBCHI, &err);
1053 lo = brcmf_sdiod_regrb(bus->sdiodev,
1054 SBSDIO_FUNC1_RFRAMEBCLO, &err);
Arend van Spriel80969832012-06-09 22:51:44 +02001055 bus->sdcnt.f1regdata += 2;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001056
1057 if ((hi == 0) && (lo == 0))
1058 break;
1059
1060 if ((hi > (lastrbc >> 8)) && (lo > (lastrbc & 0x00ff))) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001061 brcmf_err("count growing: last 0x%04x now 0x%04x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001062 lastrbc, (hi << 8) + lo);
1063 }
1064 lastrbc = (hi << 8) + lo;
1065 }
1066
1067 if (!retries)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001068 brcmf_err("count never zeroed: last 0x%04x\n", lastrbc);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001069 else
Arend van Sprielc3203372013-04-03 12:40:44 +02001070 brcmf_dbg(SDIO, "flush took %d iterations\n", 0xffff - retries);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001071
1072 if (rtx) {
Arend van Spriel80969832012-06-09 22:51:44 +02001073 bus->sdcnt.rxrtx++;
Franky Lin58692752012-05-04 18:27:36 -07001074 err = w_sdreg32(bus, SMB_NAK,
1075 offsetof(struct sdpcmd_regs, tosbmailbox));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001076
Arend van Spriel80969832012-06-09 22:51:44 +02001077 bus->sdcnt.f1regdata++;
Franky Lin58692752012-05-04 18:27:36 -07001078 if (err == 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001079 bus->rxskip = true;
1080 }
1081
1082 /* Clear partial in any case */
Franky Lin4754fce2012-09-19 22:21:11 +02001083 bus->cur_read.len = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001084}
1085
Arend van Spriel9a95e602011-11-10 20:30:29 +01001086/* return total length of buffer chain */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001087static uint brcmf_sdio_glom_len(struct brcmf_sdio *bus)
Arend van Spriel9a95e602011-11-10 20:30:29 +01001088{
1089 struct sk_buff *p;
1090 uint total;
1091
1092 total = 0;
1093 skb_queue_walk(&bus->glom, p)
1094 total += p->len;
1095 return total;
1096}
1097
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001098static void brcmf_sdio_free_glom(struct brcmf_sdio *bus)
Arend van Spriel046808d2011-11-10 20:30:31 +01001099{
1100 struct sk_buff *cur, *next;
1101
1102 skb_queue_walk_safe(&bus->glom, cur, next) {
1103 skb_unlink(cur, &bus->glom);
1104 brcmu_pkt_buf_free_skb(cur);
1105 }
1106}
1107
Franky Lin6bc52312013-08-10 12:27:28 +02001108/**
1109 * brcmfmac sdio bus specific header
1110 * This is the lowest layer header wrapped on the packets transmitted between
1111 * host and WiFi dongle which contains information needed for SDIO core and
1112 * firmware
1113 *
Franky Lin8da9d2c2013-11-29 12:25:10 +01001114 * It consists of 3 parts: hardware header, hardware extension header and
1115 * software header
Franky Lin6bc52312013-08-10 12:27:28 +02001116 * hardware header (frame tag) - 4 bytes
1117 * Byte 0~1: Frame length
1118 * Byte 2~3: Checksum, bit-wise inverse of frame length
Franky Lin8da9d2c2013-11-29 12:25:10 +01001119 * hardware extension header - 8 bytes
1120 * Tx glom mode only, N/A for Rx or normal Tx
1121 * Byte 0~1: Packet length excluding hw frame tag
1122 * Byte 2: Reserved
1123 * Byte 3: Frame flags, bit 0: last frame indication
1124 * Byte 4~5: Reserved
1125 * Byte 6~7: Tail padding length
Franky Lin6bc52312013-08-10 12:27:28 +02001126 * software header - 8 bytes
1127 * Byte 0: Rx/Tx sequence number
1128 * Byte 1: 4 MSB Channel number, 4 LSB arbitrary flag
1129 * Byte 2: Length of next data frame, reserved for Tx
1130 * Byte 3: Data offset
1131 * Byte 4: Flow control bits, reserved for Tx
1132 * Byte 5: Maximum Sequence number allowed by firmware for Tx, N/A for Tx packet
1133 * Byte 6~7: Reserved
1134 */
1135#define SDPCM_HWHDR_LEN 4
Franky Lin8da9d2c2013-11-29 12:25:10 +01001136#define SDPCM_HWEXT_LEN 8
Franky Lin6bc52312013-08-10 12:27:28 +02001137#define SDPCM_SWHDR_LEN 8
1138#define SDPCM_HDRLEN (SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN)
Franky Lin6bc52312013-08-10 12:27:28 +02001139/* software header */
1140#define SDPCM_SEQ_MASK 0x000000ff
1141#define SDPCM_SEQ_WRAP 256
1142#define SDPCM_CHANNEL_MASK 0x00000f00
1143#define SDPCM_CHANNEL_SHIFT 8
1144#define SDPCM_CONTROL_CHANNEL 0 /* Control */
1145#define SDPCM_EVENT_CHANNEL 1 /* Asyc Event Indication */
1146#define SDPCM_DATA_CHANNEL 2 /* Data Xmit/Recv */
1147#define SDPCM_GLOM_CHANNEL 3 /* Coalesced packets */
1148#define SDPCM_TEST_CHANNEL 15 /* Test/debug packets */
1149#define SDPCM_GLOMDESC(p) (((u8 *)p)[1] & 0x80)
1150#define SDPCM_NEXTLEN_MASK 0x00ff0000
1151#define SDPCM_NEXTLEN_SHIFT 16
1152#define SDPCM_DOFFSET_MASK 0xff000000
1153#define SDPCM_DOFFSET_SHIFT 24
1154#define SDPCM_FCMASK_MASK 0x000000ff
1155#define SDPCM_WINDOW_MASK 0x0000ff00
1156#define SDPCM_WINDOW_SHIFT 8
1157
1158static inline u8 brcmf_sdio_getdatoffset(u8 *swheader)
1159{
1160 u32 hdrvalue;
1161 hdrvalue = *(u32 *)swheader;
1162 return (u8)((hdrvalue & SDPCM_DOFFSET_MASK) >> SDPCM_DOFFSET_SHIFT);
1163}
1164
1165static int brcmf_sdio_hdparse(struct brcmf_sdio *bus, u8 *header,
1166 struct brcmf_sdio_hdrinfo *rd,
1167 enum brcmf_sdio_frmtype type)
Franky Lin4754fce2012-09-19 22:21:11 +02001168{
1169 u16 len, checksum;
1170 u8 rx_seq, fc, tx_seq_max;
Franky Lin6bc52312013-08-10 12:27:28 +02001171 u32 swheader;
Franky Lin4754fce2012-09-19 22:21:11 +02001172
Arend van Spriel4b776962013-11-29 12:25:06 +01001173 trace_brcmf_sdpcm_hdr(SDPCM_RX, header);
Arend van Spriel76584ec2013-10-15 15:44:47 +02001174
Franky Lin6bc52312013-08-10 12:27:28 +02001175 /* hw header */
Franky Lin4754fce2012-09-19 22:21:11 +02001176 len = get_unaligned_le16(header);
1177 checksum = get_unaligned_le16(header + sizeof(u16));
1178 /* All zero means no more to read */
1179 if (!(len | checksum)) {
1180 bus->rxpending = false;
Franky Lin10510582012-11-05 16:22:26 -08001181 return -ENODATA;
Franky Lin4754fce2012-09-19 22:21:11 +02001182 }
1183 if ((u16)(~(len ^ checksum))) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001184 brcmf_err("HW header checksum error\n");
Franky Lin4754fce2012-09-19 22:21:11 +02001185 bus->sdcnt.rx_badhdr++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001186 brcmf_sdio_rxfail(bus, false, false);
Franky Lin10510582012-11-05 16:22:26 -08001187 return -EIO;
Franky Lin4754fce2012-09-19 22:21:11 +02001188 }
1189 if (len < SDPCM_HDRLEN) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001190 brcmf_err("HW header length error\n");
Franky Lin10510582012-11-05 16:22:26 -08001191 return -EPROTO;
Franky Lin4754fce2012-09-19 22:21:11 +02001192 }
Franky Lin9d7d6f92012-10-22 10:36:24 -07001193 if (type == BRCMF_SDIO_FT_SUPER &&
1194 (roundup(len, bus->blocksize) != rd->len)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001195 brcmf_err("HW superframe header length error\n");
Franky Lin10510582012-11-05 16:22:26 -08001196 return -EPROTO;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001197 }
1198 if (type == BRCMF_SDIO_FT_SUB && len > rd->len) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001199 brcmf_err("HW subframe header length error\n");
Franky Lin10510582012-11-05 16:22:26 -08001200 return -EPROTO;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001201 }
Franky Lin4754fce2012-09-19 22:21:11 +02001202 rd->len = len;
1203
Franky Lin6bc52312013-08-10 12:27:28 +02001204 /* software header */
1205 header += SDPCM_HWHDR_LEN;
1206 swheader = le32_to_cpu(*(__le32 *)header);
1207 if (type == BRCMF_SDIO_FT_SUPER && SDPCM_GLOMDESC(header)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001208 brcmf_err("Glom descriptor found in superframe head\n");
Franky Lin9d7d6f92012-10-22 10:36:24 -07001209 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001210 return -EINVAL;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001211 }
Franky Lin6bc52312013-08-10 12:27:28 +02001212 rx_seq = (u8)(swheader & SDPCM_SEQ_MASK);
1213 rd->channel = (swheader & SDPCM_CHANNEL_MASK) >> SDPCM_CHANNEL_SHIFT;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001214 if (len > MAX_RX_DATASZ && rd->channel != SDPCM_CONTROL_CHANNEL &&
1215 type != BRCMF_SDIO_FT_SUPER) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001216 brcmf_err("HW header length too long\n");
Franky Lin4754fce2012-09-19 22:21:11 +02001217 bus->sdcnt.rx_toolong++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001218 brcmf_sdio_rxfail(bus, false, false);
Franky Lin4754fce2012-09-19 22:21:11 +02001219 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001220 return -EPROTO;
Franky Lin4754fce2012-09-19 22:21:11 +02001221 }
Franky Lin9d7d6f92012-10-22 10:36:24 -07001222 if (type == BRCMF_SDIO_FT_SUPER && rd->channel != SDPCM_GLOM_CHANNEL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001223 brcmf_err("Wrong channel for superframe\n");
Franky Lin9d7d6f92012-10-22 10:36:24 -07001224 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001225 return -EINVAL;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001226 }
1227 if (type == BRCMF_SDIO_FT_SUB && rd->channel != SDPCM_DATA_CHANNEL &&
1228 rd->channel != SDPCM_EVENT_CHANNEL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001229 brcmf_err("Wrong channel for subframe\n");
Franky Lin9d7d6f92012-10-22 10:36:24 -07001230 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001231 return -EINVAL;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001232 }
Franky Lin6bc52312013-08-10 12:27:28 +02001233 rd->dat_offset = brcmf_sdio_getdatoffset(header);
Franky Lin4754fce2012-09-19 22:21:11 +02001234 if (rd->dat_offset < SDPCM_HDRLEN || rd->dat_offset > rd->len) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001235 brcmf_err("seq %d: bad data offset\n", rx_seq);
Franky Lin4754fce2012-09-19 22:21:11 +02001236 bus->sdcnt.rx_badhdr++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001237 brcmf_sdio_rxfail(bus, false, false);
Franky Lin4754fce2012-09-19 22:21:11 +02001238 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001239 return -ENXIO;
Franky Lin4754fce2012-09-19 22:21:11 +02001240 }
1241 if (rd->seq_num != rx_seq) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001242 brcmf_err("seq %d: sequence number error, expect %d\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001243 rx_seq, rd->seq_num);
1244 bus->sdcnt.rx_badseq++;
1245 rd->seq_num = rx_seq;
1246 }
Franky Lin9d7d6f92012-10-22 10:36:24 -07001247 /* no need to check the reset for subframe */
1248 if (type == BRCMF_SDIO_FT_SUB)
Franky Lin10510582012-11-05 16:22:26 -08001249 return 0;
Franky Lin6bc52312013-08-10 12:27:28 +02001250 rd->len_nxtfrm = (swheader & SDPCM_NEXTLEN_MASK) >> SDPCM_NEXTLEN_SHIFT;
Franky Lin4754fce2012-09-19 22:21:11 +02001251 if (rd->len_nxtfrm << 4 > MAX_RX_DATASZ) {
1252 /* only warm for NON glom packet */
1253 if (rd->channel != SDPCM_GLOM_CHANNEL)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001254 brcmf_err("seq %d: next length error\n", rx_seq);
Franky Lin4754fce2012-09-19 22:21:11 +02001255 rd->len_nxtfrm = 0;
1256 }
Franky Lin6bc52312013-08-10 12:27:28 +02001257 swheader = le32_to_cpu(*(__le32 *)(header + 4));
1258 fc = swheader & SDPCM_FCMASK_MASK;
Franky Lin4754fce2012-09-19 22:21:11 +02001259 if (bus->flowcontrol != fc) {
1260 if (~bus->flowcontrol & fc)
1261 bus->sdcnt.fc_xoff++;
1262 if (bus->flowcontrol & ~fc)
1263 bus->sdcnt.fc_xon++;
1264 bus->sdcnt.fc_rcvd++;
1265 bus->flowcontrol = fc;
1266 }
Franky Lin6bc52312013-08-10 12:27:28 +02001267 tx_seq_max = (swheader & SDPCM_WINDOW_MASK) >> SDPCM_WINDOW_SHIFT;
Franky Lin4754fce2012-09-19 22:21:11 +02001268 if ((u8)(tx_seq_max - bus->tx_seq) > 0x40) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001269 brcmf_err("seq %d: max tx seq number error\n", rx_seq);
Franky Lin4754fce2012-09-19 22:21:11 +02001270 tx_seq_max = bus->tx_seq + 2;
1271 }
1272 bus->tx_max = tx_seq_max;
1273
Franky Lin10510582012-11-05 16:22:26 -08001274 return 0;
Franky Lin4754fce2012-09-19 22:21:11 +02001275}
1276
Franky Lin6bc52312013-08-10 12:27:28 +02001277static inline void brcmf_sdio_update_hwhdr(u8 *header, u16 frm_length)
1278{
1279 *(__le16 *)header = cpu_to_le16(frm_length);
1280 *(((__le16 *)header) + 1) = cpu_to_le16(~frm_length);
1281}
1282
1283static void brcmf_sdio_hdpack(struct brcmf_sdio *bus, u8 *header,
1284 struct brcmf_sdio_hdrinfo *hd_info)
1285{
Franky Lin8da9d2c2013-11-29 12:25:10 +01001286 u32 hdrval;
1287 u8 hdr_offset;
Franky Lin6bc52312013-08-10 12:27:28 +02001288
1289 brcmf_sdio_update_hwhdr(header, hd_info->len);
Franky Lin8da9d2c2013-11-29 12:25:10 +01001290 hdr_offset = SDPCM_HWHDR_LEN;
Franky Lin6bc52312013-08-10 12:27:28 +02001291
Franky Lin8da9d2c2013-11-29 12:25:10 +01001292 if (bus->txglom) {
1293 hdrval = (hd_info->len - hdr_offset) | (hd_info->lastfrm << 24);
1294 *((__le32 *)(header + hdr_offset)) = cpu_to_le32(hdrval);
1295 hdrval = (u16)hd_info->tail_pad << 16;
1296 *(((__le32 *)(header + hdr_offset)) + 1) = cpu_to_le32(hdrval);
1297 hdr_offset += SDPCM_HWEXT_LEN;
1298 }
1299
1300 hdrval = hd_info->seq_num;
1301 hdrval |= (hd_info->channel << SDPCM_CHANNEL_SHIFT) &
1302 SDPCM_CHANNEL_MASK;
1303 hdrval |= (hd_info->dat_offset << SDPCM_DOFFSET_SHIFT) &
1304 SDPCM_DOFFSET_MASK;
1305 *((__le32 *)(header + hdr_offset)) = cpu_to_le32(hdrval);
1306 *(((__le32 *)(header + hdr_offset)) + 1) = 0;
1307 trace_brcmf_sdpcm_hdr(SDPCM_TX + !!(bus->txglom), header);
Franky Lin6bc52312013-08-10 12:27:28 +02001308}
1309
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001310static u8 brcmf_sdio_rxglom(struct brcmf_sdio *bus, u8 rxseq)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001311{
1312 u16 dlen, totlen;
1313 u8 *dptr, num = 0;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001314 u16 sublen;
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001315 struct sk_buff *pfirst, *pnext;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001316
1317 int errcode;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001318 u8 doff, sfdoff;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001319
Franky Lin6bc52312013-08-10 12:27:28 +02001320 struct brcmf_sdio_hdrinfo rd_new;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001321
1322 /* If packets, issue read(s) and send up packet chain */
1323 /* Return sequence numbers consumed? */
1324
Arend van Sprielc3203372013-04-03 12:40:44 +02001325 brcmf_dbg(SDIO, "start: glomd %p glom %p\n",
Arend van Sprielb83db862011-10-19 12:51:09 +02001326 bus->glomd, skb_peek(&bus->glom));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001327
1328 /* If there's a descriptor, generate the packet chain */
1329 if (bus->glomd) {
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001330 pfirst = pnext = NULL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001331 dlen = (u16) (bus->glomd->len);
1332 dptr = bus->glomd->data;
1333 if (!dlen || (dlen & 1)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001334 brcmf_err("bad glomd len(%d), ignore descriptor\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001335 dlen);
1336 dlen = 0;
1337 }
1338
1339 for (totlen = num = 0; dlen; num++) {
1340 /* Get (and move past) next length */
1341 sublen = get_unaligned_le16(dptr);
1342 dlen -= sizeof(u16);
1343 dptr += sizeof(u16);
1344 if ((sublen < SDPCM_HDRLEN) ||
1345 ((num == 0) && (sublen < (2 * SDPCM_HDRLEN)))) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001346 brcmf_err("descriptor len %d bad: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001347 num, sublen);
1348 pnext = NULL;
1349 break;
1350 }
Arend van Spriele217d1c2013-11-29 12:25:13 +01001351 if (sublen % bus->sgentry_align) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001352 brcmf_err("sublen %d not multiple of %d\n",
Arend van Spriele217d1c2013-11-29 12:25:13 +01001353 sublen, bus->sgentry_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001354 }
1355 totlen += sublen;
1356
1357 /* For last frame, adjust read len so total
1358 is a block multiple */
1359 if (!dlen) {
1360 sublen +=
1361 (roundup(totlen, bus->blocksize) - totlen);
1362 totlen = roundup(totlen, bus->blocksize);
1363 }
1364
1365 /* Allocate/chain packet for next subframe */
Arend van Spriele217d1c2013-11-29 12:25:13 +01001366 pnext = brcmu_pkt_buf_get_skb(sublen + bus->sgentry_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001367 if (pnext == NULL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001368 brcmf_err("bcm_pkt_buf_get_skb failed, num %d len %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001369 num, sublen);
1370 break;
1371 }
Arend van Sprielb83db862011-10-19 12:51:09 +02001372 skb_queue_tail(&bus->glom, pnext);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001373
1374 /* Adhere to start alignment requirements */
Arend van Spriele217d1c2013-11-29 12:25:13 +01001375 pkt_align(pnext, sublen, bus->sgentry_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001376 }
1377
1378 /* If all allocations succeeded, save packet chain
1379 in bus structure */
1380 if (pnext) {
1381 brcmf_dbg(GLOM, "allocated %d-byte packet chain for %d subframes\n",
1382 totlen, num);
Franky Lin4754fce2012-09-19 22:21:11 +02001383 if (BRCMF_GLOM_ON() && bus->cur_read.len &&
1384 totlen != bus->cur_read.len) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001385 brcmf_dbg(GLOM, "glomdesc mismatch: nextlen %d glomdesc %d rxseq %d\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001386 bus->cur_read.len, totlen, rxseq);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001387 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02001388 pfirst = pnext = NULL;
1389 } else {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001390 brcmf_sdio_free_glom(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001391 num = 0;
1392 }
1393
1394 /* Done with descriptor packet */
1395 brcmu_pkt_buf_free_skb(bus->glomd);
1396 bus->glomd = NULL;
Franky Lin4754fce2012-09-19 22:21:11 +02001397 bus->cur_read.len = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001398 }
1399
1400 /* Ok -- either we just generated a packet chain,
1401 or had one from before */
Arend van Sprielb83db862011-10-19 12:51:09 +02001402 if (!skb_queue_empty(&bus->glom)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001403 if (BRCMF_GLOM_ON()) {
1404 brcmf_dbg(GLOM, "try superframe read, packet chain:\n");
Arend van Sprielb83db862011-10-19 12:51:09 +02001405 skb_queue_walk(&bus->glom, pnext) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001406 brcmf_dbg(GLOM, " %p: %p len 0x%04x (%d)\n",
1407 pnext, (u8 *) (pnext->data),
1408 pnext->len, pnext->len);
1409 }
1410 }
1411
Arend van Sprielb83db862011-10-19 12:51:09 +02001412 pfirst = skb_peek(&bus->glom);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001413 dlen = (u16) brcmf_sdio_glom_len(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001414
1415 /* Do an SDIO read for the superframe. Configurable iovar to
1416 * read directly into the chained packet, or allocate a large
1417 * packet and and copy into the chain.
1418 */
Franky Lin38b0b0d2012-11-05 16:22:24 -08001419 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriela39be272013-12-12 11:58:58 +01001420 errcode = brcmf_sdiod_recv_chain(bus->sdiodev,
Arend van Spriela39be272013-12-12 11:58:58 +01001421 &bus->glom, dlen);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001422 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel80969832012-06-09 22:51:44 +02001423 bus->sdcnt.f2rxdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001424
1425 /* On failure, kill the superframe, allow a couple retries */
1426 if (errcode < 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001427 brcmf_err("glom read of %d bytes failed: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001428 dlen, errcode);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001429
Franky Lin38b0b0d2012-11-05 16:22:24 -08001430 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001431 if (bus->glomerr++ < 3) {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001432 brcmf_sdio_rxfail(bus, true, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001433 } else {
1434 bus->glomerr = 0;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001435 brcmf_sdio_rxfail(bus, true, false);
Arend van Spriel80969832012-06-09 22:51:44 +02001436 bus->sdcnt.rxglomfail++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001437 brcmf_sdio_free_glom(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001438 }
Franky Lin38b0b0d2012-11-05 16:22:24 -08001439 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001440 return 0;
1441 }
Joe Perches1e023822012-01-15 00:38:40 -08001442
1443 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
1444 pfirst->data, min_t(int, pfirst->len, 48),
1445 "SUPERFRAME:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001446
Franky Lin9d7d6f92012-10-22 10:36:24 -07001447 rd_new.seq_num = rxseq;
1448 rd_new.len = dlen;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001449 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin6bc52312013-08-10 12:27:28 +02001450 errcode = brcmf_sdio_hdparse(bus, pfirst->data, &rd_new,
1451 BRCMF_SDIO_FT_SUPER);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001452 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin9d7d6f92012-10-22 10:36:24 -07001453 bus->cur_read.len = rd_new.len_nxtfrm << 4;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001454
1455 /* Remove superframe header, remember offset */
Franky Lin9d7d6f92012-10-22 10:36:24 -07001456 skb_pull(pfirst, rd_new.dat_offset);
1457 sfdoff = rd_new.dat_offset;
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001458 num = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001459
1460 /* Validate all the subframe headers */
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001461 skb_queue_walk(&bus->glom, pnext) {
1462 /* leave when invalid subframe is found */
1463 if (errcode)
1464 break;
1465
Franky Lin9d7d6f92012-10-22 10:36:24 -07001466 rd_new.len = pnext->len;
1467 rd_new.seq_num = rxseq++;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001468 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin6bc52312013-08-10 12:27:28 +02001469 errcode = brcmf_sdio_hdparse(bus, pnext->data, &rd_new,
1470 BRCMF_SDIO_FT_SUB);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001471 sdio_release_host(bus->sdiodev->func[1]);
Joe Perches1e023822012-01-15 00:38:40 -08001472 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
Franky Lin9d7d6f92012-10-22 10:36:24 -07001473 pnext->data, 32, "subframe:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001474
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001475 num++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001476 }
1477
1478 if (errcode) {
1479 /* Terminate frame on error, request
1480 a couple retries */
Franky Lin38b0b0d2012-11-05 16:22:24 -08001481 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001482 if (bus->glomerr++ < 3) {
1483 /* Restore superframe header space */
1484 skb_push(pfirst, sfdoff);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001485 brcmf_sdio_rxfail(bus, true, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001486 } else {
1487 bus->glomerr = 0;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001488 brcmf_sdio_rxfail(bus, true, false);
Arend van Spriel80969832012-06-09 22:51:44 +02001489 bus->sdcnt.rxglomfail++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001490 brcmf_sdio_free_glom(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001491 }
Franky Lin38b0b0d2012-11-05 16:22:24 -08001492 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001493 bus->cur_read.len = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001494 return 0;
1495 }
1496
1497 /* Basic SD framing looks ok - process each packet (header) */
Arend van Spriel5b435de2011-10-05 13:19:03 +02001498
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001499 skb_queue_walk_safe(&bus->glom, pfirst, pnext) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001500 dptr = (u8 *) (pfirst->data);
1501 sublen = get_unaligned_le16(dptr);
Franky Lin6bc52312013-08-10 12:27:28 +02001502 doff = brcmf_sdio_getdatoffset(&dptr[SDPCM_HWHDR_LEN]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001503
Joe Perches1e023822012-01-15 00:38:40 -08001504 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_DATA_ON(),
Franky Lin9d7d6f92012-10-22 10:36:24 -07001505 dptr, pfirst->len,
1506 "Rx Subframe Data:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001507
1508 __skb_trim(pfirst, sublen);
1509 skb_pull(pfirst, doff);
1510
1511 if (pfirst->len == 0) {
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001512 skb_unlink(pfirst, &bus->glom);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001513 brcmu_pkt_buf_free_skb(pfirst);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001514 continue;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001515 }
1516
Joe Perches1e023822012-01-15 00:38:40 -08001517 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
1518 pfirst->data,
1519 min_t(int, pfirst->len, 32),
1520 "subframe %d to stack, %p (%p/%d) nxt/lnk %p/%p\n",
1521 bus->glom.qlen, pfirst, pfirst->data,
1522 pfirst->len, pfirst->next,
1523 pfirst->prev);
Arend van Spriel05f38202013-09-25 13:05:46 +02001524 skb_unlink(pfirst, &bus->glom);
1525 brcmf_rx_frame(bus->sdiodev->dev, pfirst);
1526 bus->sdcnt.rxglompkts++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001527 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02001528
Arend van Spriel80969832012-06-09 22:51:44 +02001529 bus->sdcnt.rxglomframes++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001530 }
1531 return num;
1532}
1533
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001534static int brcmf_sdio_dcmd_resp_wait(struct brcmf_sdio *bus, uint *condition,
1535 bool *pending)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001536{
1537 DECLARE_WAITQUEUE(wait, current);
1538 int timeout = msecs_to_jiffies(DCMD_RESP_TIMEOUT);
1539
1540 /* Wait until control frame is available */
1541 add_wait_queue(&bus->dcmd_resp_wait, &wait);
1542 set_current_state(TASK_INTERRUPTIBLE);
1543
1544 while (!(*condition) && (!signal_pending(current) && timeout))
1545 timeout = schedule_timeout(timeout);
1546
1547 if (signal_pending(current))
1548 *pending = true;
1549
1550 set_current_state(TASK_RUNNING);
1551 remove_wait_queue(&bus->dcmd_resp_wait, &wait);
1552
1553 return timeout;
1554}
1555
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001556static int brcmf_sdio_dcmd_resp_wake(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001557{
1558 if (waitqueue_active(&bus->dcmd_resp_wait))
1559 wake_up_interruptible(&bus->dcmd_resp_wait);
1560
1561 return 0;
1562}
1563static void
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001564brcmf_sdio_read_control(struct brcmf_sdio *bus, u8 *hdr, uint len, uint doff)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001565{
1566 uint rdlen, pad;
Franky Lindd43a012012-11-05 16:22:22 -08001567 u8 *buf = NULL, *rbuf;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001568 int sdret;
1569
1570 brcmf_dbg(TRACE, "Enter\n");
1571
Franky Lindd43a012012-11-05 16:22:22 -08001572 if (bus->rxblen)
1573 buf = vzalloc(bus->rxblen);
Joe Perches14f8dc42013-02-07 11:46:27 +00001574 if (!buf)
Franky Lindd43a012012-11-05 16:22:22 -08001575 goto done;
Joe Perches14f8dc42013-02-07 11:46:27 +00001576
Franky Lindd43a012012-11-05 16:22:22 -08001577 rbuf = bus->rxbuf;
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01001578 pad = ((unsigned long)rbuf % bus->head_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001579 if (pad)
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01001580 rbuf += (bus->head_align - pad);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001581
1582 /* Copy the already-read portion over */
Franky Lindd43a012012-11-05 16:22:22 -08001583 memcpy(buf, hdr, BRCMF_FIRSTREAD);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001584 if (len <= BRCMF_FIRSTREAD)
1585 goto gotpkt;
1586
1587 /* Raise rdlen to next SDIO block to avoid tail command */
1588 rdlen = len - BRCMF_FIRSTREAD;
1589 if (bus->roundup && bus->blocksize && (rdlen > bus->blocksize)) {
1590 pad = bus->blocksize - (rdlen % bus->blocksize);
1591 if ((pad <= bus->roundup) && (pad < bus->blocksize) &&
Franky Linb01a6b32011-12-16 18:37:03 -08001592 ((len + pad) < bus->sdiodev->bus_if->maxctl))
Arend van Spriel5b435de2011-10-05 13:19:03 +02001593 rdlen += pad;
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01001594 } else if (rdlen % bus->head_align) {
1595 rdlen += bus->head_align - (rdlen % bus->head_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001596 }
1597
Arend van Spriel5b435de2011-10-05 13:19:03 +02001598 /* Drop if the read is too big or it exceeds our maximum */
Franky Linb01a6b32011-12-16 18:37:03 -08001599 if ((rdlen + BRCMF_FIRSTREAD) > bus->sdiodev->bus_if->maxctl) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001600 brcmf_err("%d-byte control read exceeds %d-byte buffer\n",
Franky Linb01a6b32011-12-16 18:37:03 -08001601 rdlen, bus->sdiodev->bus_if->maxctl);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001602 brcmf_sdio_rxfail(bus, false, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001603 goto done;
1604 }
1605
Franky Linb01a6b32011-12-16 18:37:03 -08001606 if ((len - doff) > bus->sdiodev->bus_if->maxctl) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001607 brcmf_err("%d-byte ctl frame (%d-byte ctl data) exceeds %d-byte limit\n",
Franky Linb01a6b32011-12-16 18:37:03 -08001608 len, len - doff, bus->sdiodev->bus_if->maxctl);
Arend van Spriel80969832012-06-09 22:51:44 +02001609 bus->sdcnt.rx_toolong++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001610 brcmf_sdio_rxfail(bus, false, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001611 goto done;
1612 }
1613
Franky Lindd43a012012-11-05 16:22:22 -08001614 /* Read remain of frame body */
Arend van Spriela7cdd822013-12-12 11:58:59 +01001615 sdret = brcmf_sdiod_recv_buf(bus->sdiodev, rbuf, rdlen);
Arend van Spriel80969832012-06-09 22:51:44 +02001616 bus->sdcnt.f2rxdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001617
1618 /* Control frame failures need retransmission */
1619 if (sdret < 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001620 brcmf_err("read %d control bytes failed: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001621 rdlen, sdret);
Arend van Spriel80969832012-06-09 22:51:44 +02001622 bus->sdcnt.rxc_errors++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001623 brcmf_sdio_rxfail(bus, true, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001624 goto done;
Franky Lindd43a012012-11-05 16:22:22 -08001625 } else
1626 memcpy(buf + BRCMF_FIRSTREAD, rbuf, rdlen);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001627
1628gotpkt:
1629
Joe Perches1e023822012-01-15 00:38:40 -08001630 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_CTL_ON(),
Franky Lindd43a012012-11-05 16:22:22 -08001631 buf, len, "RxCtrl:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001632
1633 /* Point to valid data and indicate its length */
Franky Lindd43a012012-11-05 16:22:22 -08001634 spin_lock_bh(&bus->rxctl_lock);
1635 if (bus->rxctl) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001636 brcmf_err("last control frame is being processed.\n");
Franky Lindd43a012012-11-05 16:22:22 -08001637 spin_unlock_bh(&bus->rxctl_lock);
1638 vfree(buf);
1639 goto done;
1640 }
1641 bus->rxctl = buf + doff;
1642 bus->rxctl_orig = buf;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001643 bus->rxlen = len - doff;
Franky Lindd43a012012-11-05 16:22:22 -08001644 spin_unlock_bh(&bus->rxctl_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001645
1646done:
1647 /* Awake any waiters */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001648 brcmf_sdio_dcmd_resp_wake(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001649}
1650
1651/* Pad read to blocksize for efficiency */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001652static void brcmf_sdio_pad(struct brcmf_sdio *bus, u16 *pad, u16 *rdlen)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001653{
1654 if (bus->roundup && bus->blocksize && *rdlen > bus->blocksize) {
1655 *pad = bus->blocksize - (*rdlen % bus->blocksize);
1656 if (*pad <= bus->roundup && *pad < bus->blocksize &&
1657 *rdlen + *pad + BRCMF_FIRSTREAD < MAX_RX_DATASZ)
1658 *rdlen += *pad;
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01001659 } else if (*rdlen % bus->head_align) {
1660 *rdlen += bus->head_align - (*rdlen % bus->head_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001661 }
1662}
1663
Franky Lin4754fce2012-09-19 22:21:11 +02001664static uint brcmf_sdio_readframes(struct brcmf_sdio *bus, uint maxframes)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001665{
Arend van Spriel5b435de2011-10-05 13:19:03 +02001666 struct sk_buff *pkt; /* Packet for event or data frames */
1667 u16 pad; /* Number of pad bytes to read */
Arend van Spriel5b435de2011-10-05 13:19:03 +02001668 uint rxleft = 0; /* Remaining number of frames allowed */
Arend van Spriel349e7102013-03-03 12:45:28 +01001669 int ret; /* Return code from calls */
Arend van Spriel5b435de2011-10-05 13:19:03 +02001670 uint rxcount = 0; /* Total frames read */
Franky Lin6bc52312013-08-10 12:27:28 +02001671 struct brcmf_sdio_hdrinfo *rd = &bus->cur_read, rd_new;
Franky Lin4754fce2012-09-19 22:21:11 +02001672 u8 head_read = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001673
1674 brcmf_dbg(TRACE, "Enter\n");
1675
1676 /* Not finished unless we encounter no more frames indication */
Franky Lin4754fce2012-09-19 22:21:11 +02001677 bus->rxpending = true;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001678
Franky Lin4754fce2012-09-19 22:21:11 +02001679 for (rd->seq_num = bus->rx_seq, rxleft = maxframes;
Arend van Sprielbb350712014-01-13 22:20:29 +01001680 !bus->rxskip && rxleft && brcmf_bus_ready(bus->sdiodev->bus_if);
Franky Lin4754fce2012-09-19 22:21:11 +02001681 rd->seq_num++, rxleft--) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001682
1683 /* Handle glomming separately */
Arend van Sprielb83db862011-10-19 12:51:09 +02001684 if (bus->glomd || !skb_queue_empty(&bus->glom)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001685 u8 cnt;
1686 brcmf_dbg(GLOM, "calling rxglom: glomd %p, glom %p\n",
Arend van Sprielb83db862011-10-19 12:51:09 +02001687 bus->glomd, skb_peek(&bus->glom));
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001688 cnt = brcmf_sdio_rxglom(bus, rd->seq_num);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001689 brcmf_dbg(GLOM, "rxglom returned %d\n", cnt);
Franky Lin4754fce2012-09-19 22:21:11 +02001690 rd->seq_num += cnt - 1;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001691 rxleft = (rxleft > cnt) ? (rxleft - cnt) : 1;
1692 continue;
1693 }
1694
Franky Lin4754fce2012-09-19 22:21:11 +02001695 rd->len_left = rd->len;
1696 /* read header first for unknow frame length */
Franky Lin38b0b0d2012-11-05 16:22:24 -08001697 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001698 if (!rd->len) {
Arend van Spriela39be272013-12-12 11:58:58 +01001699 ret = brcmf_sdiod_recv_buf(bus->sdiodev,
Arend van Spriela39be272013-12-12 11:58:58 +01001700 bus->rxhdr, BRCMF_FIRSTREAD);
Franky Lin4754fce2012-09-19 22:21:11 +02001701 bus->sdcnt.f2rxhdrs++;
Arend van Spriel349e7102013-03-03 12:45:28 +01001702 if (ret < 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001703 brcmf_err("RXHEADER FAILED: %d\n",
Arend van Spriel349e7102013-03-03 12:45:28 +01001704 ret);
Franky Lin4754fce2012-09-19 22:21:11 +02001705 bus->sdcnt.rx_hdrfail++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001706 brcmf_sdio_rxfail(bus, true, true);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001707 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001708 continue;
1709 }
1710
Franky Lin4754fce2012-09-19 22:21:11 +02001711 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() || BRCMF_HDRS_ON(),
1712 bus->rxhdr, SDPCM_HDRLEN,
1713 "RxHdr:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001714
Franky Lin6bc52312013-08-10 12:27:28 +02001715 if (brcmf_sdio_hdparse(bus, bus->rxhdr, rd,
1716 BRCMF_SDIO_FT_NORMAL)) {
Franky Lin38b0b0d2012-11-05 16:22:24 -08001717 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001718 if (!bus->rxpending)
1719 break;
1720 else
1721 continue;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001722 }
1723
Franky Lin4754fce2012-09-19 22:21:11 +02001724 if (rd->channel == SDPCM_CONTROL_CHANNEL) {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001725 brcmf_sdio_read_control(bus, bus->rxhdr,
1726 rd->len,
1727 rd->dat_offset);
Franky Lin4754fce2012-09-19 22:21:11 +02001728 /* prepare the descriptor for the next read */
1729 rd->len = rd->len_nxtfrm << 4;
1730 rd->len_nxtfrm = 0;
1731 /* treat all packet as event if we don't know */
1732 rd->channel = SDPCM_EVENT_CHANNEL;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001733 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001734 continue;
1735 }
1736 rd->len_left = rd->len > BRCMF_FIRSTREAD ?
1737 rd->len - BRCMF_FIRSTREAD : 0;
1738 head_read = BRCMF_FIRSTREAD;
1739 }
1740
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001741 brcmf_sdio_pad(bus, &pad, &rd->len_left);
Franky Lin4754fce2012-09-19 22:21:11 +02001742
1743 pkt = brcmu_pkt_buf_get_skb(rd->len_left + head_read +
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01001744 bus->head_align);
Franky Lin4754fce2012-09-19 22:21:11 +02001745 if (!pkt) {
1746 /* Give up on data, request rtx of events */
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001747 brcmf_err("brcmu_pkt_buf_get_skb failed\n");
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001748 brcmf_sdio_rxfail(bus, false,
Franky Lin4754fce2012-09-19 22:21:11 +02001749 RETRYCHAN(rd->channel));
Franky Lin38b0b0d2012-11-05 16:22:24 -08001750 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001751 continue;
1752 }
1753 skb_pull(pkt, head_read);
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01001754 pkt_align(pkt, rd->len_left, bus->head_align);
Franky Lin4754fce2012-09-19 22:21:11 +02001755
Arend van Spriela7cdd822013-12-12 11:58:59 +01001756 ret = brcmf_sdiod_recv_pkt(bus->sdiodev, pkt);
Franky Lin4754fce2012-09-19 22:21:11 +02001757 bus->sdcnt.f2rxdata++;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001758 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001759
Arend van Spriel349e7102013-03-03 12:45:28 +01001760 if (ret < 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001761 brcmf_err("read %d bytes from channel %d failed: %d\n",
Arend van Spriel349e7102013-03-03 12:45:28 +01001762 rd->len, rd->channel, ret);
Franky Lin4754fce2012-09-19 22:21:11 +02001763 brcmu_pkt_buf_free_skb(pkt);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001764 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001765 brcmf_sdio_rxfail(bus, true,
Franky Lin4754fce2012-09-19 22:21:11 +02001766 RETRYCHAN(rd->channel));
Franky Lin38b0b0d2012-11-05 16:22:24 -08001767 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001768 continue;
1769 }
1770
1771 if (head_read) {
1772 skb_push(pkt, head_read);
1773 memcpy(pkt->data, bus->rxhdr, head_read);
1774 head_read = 0;
1775 } else {
1776 memcpy(bus->rxhdr, pkt->data, SDPCM_HDRLEN);
1777 rd_new.seq_num = rd->seq_num;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001778 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin6bc52312013-08-10 12:27:28 +02001779 if (brcmf_sdio_hdparse(bus, bus->rxhdr, &rd_new,
1780 BRCMF_SDIO_FT_NORMAL)) {
Franky Lin4754fce2012-09-19 22:21:11 +02001781 rd->len = 0;
1782 brcmu_pkt_buf_free_skb(pkt);
1783 }
Arend van Spriel80969832012-06-09 22:51:44 +02001784 bus->sdcnt.rx_readahead_cnt++;
Franky Lin4754fce2012-09-19 22:21:11 +02001785 if (rd->len != roundup(rd_new.len, 16)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001786 brcmf_err("frame length mismatch:read %d, should be %d\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001787 rd->len,
1788 roundup(rd_new.len, 16) >> 4);
1789 rd->len = 0;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001790 brcmf_sdio_rxfail(bus, true, true);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001791 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001792 brcmu_pkt_buf_free_skb(pkt);
1793 continue;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001794 }
Franky Lin38b0b0d2012-11-05 16:22:24 -08001795 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001796 rd->len_nxtfrm = rd_new.len_nxtfrm;
1797 rd->channel = rd_new.channel;
1798 rd->dat_offset = rd_new.dat_offset;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001799
Joe Perches1e023822012-01-15 00:38:40 -08001800 brcmf_dbg_hex_dump(!(BRCMF_BYTES_ON() &&
1801 BRCMF_DATA_ON()) &&
1802 BRCMF_HDRS_ON(),
1803 bus->rxhdr, SDPCM_HDRLEN,
1804 "RxHdr:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001805
Franky Lin4754fce2012-09-19 22:21:11 +02001806 if (rd_new.channel == SDPCM_CONTROL_CHANNEL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001807 brcmf_err("readahead on control packet %d?\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001808 rd_new.seq_num);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001809 /* Force retry w/normal header read */
Franky Lin4754fce2012-09-19 22:21:11 +02001810 rd->len = 0;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001811 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001812 brcmf_sdio_rxfail(bus, false, true);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001813 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001814 brcmu_pkt_buf_free_skb(pkt);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001815 continue;
1816 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02001817 }
1818
Joe Perches1e023822012-01-15 00:38:40 -08001819 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_DATA_ON(),
Franky Lin4754fce2012-09-19 22:21:11 +02001820 pkt->data, rd->len, "Rx Data:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001821
Arend van Spriel5b435de2011-10-05 13:19:03 +02001822 /* Save superframe descriptor and allocate packet frame */
Franky Lin4754fce2012-09-19 22:21:11 +02001823 if (rd->channel == SDPCM_GLOM_CHANNEL) {
Franky Lin6bc52312013-08-10 12:27:28 +02001824 if (SDPCM_GLOMDESC(&bus->rxhdr[SDPCM_HWHDR_LEN])) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001825 brcmf_dbg(GLOM, "glom descriptor, %d bytes:\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001826 rd->len);
Joe Perches1e023822012-01-15 00:38:40 -08001827 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
Franky Lin4754fce2012-09-19 22:21:11 +02001828 pkt->data, rd->len,
Joe Perches1e023822012-01-15 00:38:40 -08001829 "Glom Data:\n");
Franky Lin4754fce2012-09-19 22:21:11 +02001830 __skb_trim(pkt, rd->len);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001831 skb_pull(pkt, SDPCM_HDRLEN);
1832 bus->glomd = pkt;
1833 } else {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001834 brcmf_err("%s: glom superframe w/o "
Arend van Spriel5b435de2011-10-05 13:19:03 +02001835 "descriptor!\n", __func__);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001836 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001837 brcmf_sdio_rxfail(bus, false, false);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001838 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001839 }
Franky Lin4754fce2012-09-19 22:21:11 +02001840 /* prepare the descriptor for the next read */
1841 rd->len = rd->len_nxtfrm << 4;
1842 rd->len_nxtfrm = 0;
1843 /* treat all packet as event if we don't know */
1844 rd->channel = SDPCM_EVENT_CHANNEL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001845 continue;
1846 }
1847
1848 /* Fill in packet len and prio, deliver upward */
Franky Lin4754fce2012-09-19 22:21:11 +02001849 __skb_trim(pkt, rd->len);
1850 skb_pull(pkt, rd->dat_offset);
1851
1852 /* prepare the descriptor for the next read */
1853 rd->len = rd->len_nxtfrm << 4;
1854 rd->len_nxtfrm = 0;
1855 /* treat all packet as event if we don't know */
1856 rd->channel = SDPCM_EVENT_CHANNEL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001857
1858 if (pkt->len == 0) {
1859 brcmu_pkt_buf_free_skb(pkt);
1860 continue;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001861 }
1862
Arend van Spriel05f38202013-09-25 13:05:46 +02001863 brcmf_rx_frame(bus->sdiodev->dev, pkt);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001864 }
Franky Lin4754fce2012-09-19 22:21:11 +02001865
Arend van Spriel5b435de2011-10-05 13:19:03 +02001866 rxcount = maxframes - rxleft;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001867 /* Message if we hit the limit */
1868 if (!rxleft)
Franky Lin4754fce2012-09-19 22:21:11 +02001869 brcmf_dbg(DATA, "hit rx limit of %d frames\n", maxframes);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001870 else
Arend van Spriel5b435de2011-10-05 13:19:03 +02001871 brcmf_dbg(DATA, "processed %d frames\n", rxcount);
1872 /* Back off rxseq if awaiting rtx, update rx_seq */
1873 if (bus->rxskip)
Franky Lin4754fce2012-09-19 22:21:11 +02001874 rd->seq_num--;
1875 bus->rx_seq = rd->seq_num;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001876
1877 return rxcount;
1878}
1879
Arend van Spriel5b435de2011-10-05 13:19:03 +02001880static void
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001881brcmf_sdio_wait_event_wakeup(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001882{
1883 if (waitqueue_active(&bus->ctrl_wait))
1884 wake_up_interruptible(&bus->ctrl_wait);
1885 return;
1886}
1887
Franky Lin8da9d2c2013-11-29 12:25:10 +01001888static int brcmf_sdio_txpkt_hdalign(struct brcmf_sdio *bus, struct sk_buff *pkt)
1889{
Arend van Spriele217d1c2013-11-29 12:25:13 +01001890 u16 head_pad;
Franky Lin8da9d2c2013-11-29 12:25:10 +01001891 u8 *dat_buf;
1892
Franky Lin8da9d2c2013-11-29 12:25:10 +01001893 dat_buf = (u8 *)(pkt->data);
1894
1895 /* Check head padding */
Arend van Spriele217d1c2013-11-29 12:25:13 +01001896 head_pad = ((unsigned long)dat_buf % bus->head_align);
Franky Lin8da9d2c2013-11-29 12:25:10 +01001897 if (head_pad) {
1898 if (skb_headroom(pkt) < head_pad) {
1899 bus->sdiodev->bus_if->tx_realloc++;
1900 head_pad = 0;
1901 if (skb_cow(pkt, head_pad))
1902 return -ENOMEM;
1903 }
1904 skb_push(pkt, head_pad);
1905 dat_buf = (u8 *)(pkt->data);
1906 memset(dat_buf, 0, head_pad + bus->tx_hdrlen);
1907 }
1908 return head_pad;
1909}
1910
Franky Lin5491c112013-09-25 13:05:43 +02001911/**
1912 * struct brcmf_skbuff_cb reserves first two bytes in sk_buff::cb for
1913 * bus layer usage.
1914 */
Franky Linb05e9252013-08-10 12:27:26 +02001915/* flag marking a dummy skb added for DMA alignment requirement */
Franky Lin5491c112013-09-25 13:05:43 +02001916#define ALIGN_SKB_FLAG 0x8000
Franky Linb05e9252013-08-10 12:27:26 +02001917/* bit mask of data length chopped from the previous packet */
Franky Lin5491c112013-09-25 13:05:43 +02001918#define ALIGN_SKB_CHOP_LEN_MASK 0x7fff
1919
Franky Lin8da9d2c2013-11-29 12:25:10 +01001920static int brcmf_sdio_txpkt_prep_sg(struct brcmf_sdio *bus,
Arend van Spriela64304f2013-10-15 15:44:56 +02001921 struct sk_buff_head *pktq,
Franky Lin8da9d2c2013-11-29 12:25:10 +01001922 struct sk_buff *pkt, u16 total_len)
Arend van Spriela64304f2013-10-15 15:44:56 +02001923{
Franky Lin8da9d2c2013-11-29 12:25:10 +01001924 struct brcmf_sdio_dev *sdiodev;
Arend van Spriela64304f2013-10-15 15:44:56 +02001925 struct sk_buff *pkt_pad;
Arend van Spriele217d1c2013-11-29 12:25:13 +01001926 u16 tail_pad, tail_chop, chain_pad;
Arend van Spriela64304f2013-10-15 15:44:56 +02001927 unsigned int blksize;
Franky Lin8da9d2c2013-11-29 12:25:10 +01001928 bool lastfrm;
1929 int ntail, ret;
Arend van Spriela64304f2013-10-15 15:44:56 +02001930
Franky Lin8da9d2c2013-11-29 12:25:10 +01001931 sdiodev = bus->sdiodev;
Arend van Spriela64304f2013-10-15 15:44:56 +02001932 blksize = sdiodev->func[SDIO_FUNC_2]->cur_blksize;
Arend van Spriela64304f2013-10-15 15:44:56 +02001933 /* sg entry alignment should be a divisor of block size */
Arend van Spriele217d1c2013-11-29 12:25:13 +01001934 WARN_ON(blksize % bus->sgentry_align);
Arend van Spriela64304f2013-10-15 15:44:56 +02001935
1936 /* Check tail padding */
Franky Lin8da9d2c2013-11-29 12:25:10 +01001937 lastfrm = skb_queue_is_last(pktq, pkt);
1938 tail_pad = 0;
Arend van Spriele217d1c2013-11-29 12:25:13 +01001939 tail_chop = pkt->len % bus->sgentry_align;
Franky Lin8da9d2c2013-11-29 12:25:10 +01001940 if (tail_chop)
Arend van Spriele217d1c2013-11-29 12:25:13 +01001941 tail_pad = bus->sgentry_align - tail_chop;
Franky Lin8da9d2c2013-11-29 12:25:10 +01001942 chain_pad = (total_len + tail_pad) % blksize;
1943 if (lastfrm && chain_pad)
1944 tail_pad += blksize - chain_pad;
Arend van Spriela64304f2013-10-15 15:44:56 +02001945 if (skb_tailroom(pkt) < tail_pad && pkt->len > blksize) {
Arend van Spriel1eb43012014-02-20 18:55:55 +01001946 pkt_pad = brcmu_pkt_buf_get_skb(tail_pad + tail_chop +
1947 bus->head_align);
Arend van Spriela64304f2013-10-15 15:44:56 +02001948 if (pkt_pad == NULL)
1949 return -ENOMEM;
Franky Lin8da9d2c2013-11-29 12:25:10 +01001950 ret = brcmf_sdio_txpkt_hdalign(bus, pkt_pad);
1951 if (unlikely(ret < 0))
1952 return ret;
Arend van Spriela64304f2013-10-15 15:44:56 +02001953 memcpy(pkt_pad->data,
1954 pkt->data + pkt->len - tail_chop,
1955 tail_chop);
1956 *(u32 *)(pkt_pad->cb) = ALIGN_SKB_FLAG + tail_chop;
1957 skb_trim(pkt, pkt->len - tail_chop);
Arend van Spriel1eb43012014-02-20 18:55:55 +01001958 skb_trim(pkt_pad, tail_pad + tail_chop);
Arend van Spriela64304f2013-10-15 15:44:56 +02001959 __skb_queue_after(pktq, pkt, pkt_pad);
1960 } else {
1961 ntail = pkt->data_len + tail_pad -
1962 (pkt->end - pkt->tail);
1963 if (skb_cloned(pkt) || ntail > 0)
1964 if (pskb_expand_head(pkt, 0, ntail, GFP_ATOMIC))
1965 return -ENOMEM;
1966 if (skb_linearize(pkt))
1967 return -ENOMEM;
Arend van Spriela64304f2013-10-15 15:44:56 +02001968 __skb_put(pkt, tail_pad);
1969 }
1970
Franky Lin8da9d2c2013-11-29 12:25:10 +01001971 return tail_pad;
Arend van Spriela64304f2013-10-15 15:44:56 +02001972}
1973
Franky Linb05e9252013-08-10 12:27:26 +02001974/**
1975 * brcmf_sdio_txpkt_prep - packet preparation for transmit
1976 * @bus: brcmf_sdio structure pointer
1977 * @pktq: packet list pointer
1978 * @chan: virtual channel to transmit the packet
1979 *
1980 * Processes to be applied to the packet
1981 * - Align data buffer pointer
1982 * - Align data buffer length
1983 * - Prepare header
1984 * Return: negative value if there is error
1985 */
1986static int
1987brcmf_sdio_txpkt_prep(struct brcmf_sdio *bus, struct sk_buff_head *pktq,
1988 uint chan)
1989{
Franky Lin8da9d2c2013-11-29 12:25:10 +01001990 u16 head_pad, total_len;
Arend van Spriela64304f2013-10-15 15:44:56 +02001991 struct sk_buff *pkt_next;
Franky Lin8da9d2c2013-11-29 12:25:10 +01001992 u8 txseq;
1993 int ret;
Franky Lin6bc52312013-08-10 12:27:28 +02001994 struct brcmf_sdio_hdrinfo hd_info = {0};
Franky Linb05e9252013-08-10 12:27:26 +02001995
Franky Lin8da9d2c2013-11-29 12:25:10 +01001996 txseq = bus->tx_seq;
1997 total_len = 0;
1998 skb_queue_walk(pktq, pkt_next) {
1999 /* alignment packet inserted in previous
2000 * loop cycle can be skipped as it is
2001 * already properly aligned and does not
2002 * need an sdpcm header.
2003 */
2004 if (*(u32 *)(pkt_next->cb) & ALIGN_SKB_FLAG)
2005 continue;
Franky Linb05e9252013-08-10 12:27:26 +02002006
Franky Lin8da9d2c2013-11-29 12:25:10 +01002007 /* align packet data pointer */
2008 ret = brcmf_sdio_txpkt_hdalign(bus, pkt_next);
2009 if (ret < 0)
2010 return ret;
2011 head_pad = (u16)ret;
2012 if (head_pad)
Arend van Spriel1eb43012014-02-20 18:55:55 +01002013 memset(pkt_next->data + bus->tx_hdrlen, 0, head_pad);
Franky Linb05e9252013-08-10 12:27:26 +02002014
Franky Lin8da9d2c2013-11-29 12:25:10 +01002015 total_len += pkt_next->len;
Franky Linb05e9252013-08-10 12:27:26 +02002016
Arend van Spriela64304f2013-10-15 15:44:56 +02002017 hd_info.len = pkt_next->len;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002018 hd_info.lastfrm = skb_queue_is_last(pktq, pkt_next);
2019 if (bus->txglom && pktq->qlen > 1) {
2020 ret = brcmf_sdio_txpkt_prep_sg(bus, pktq,
2021 pkt_next, total_len);
2022 if (ret < 0)
2023 return ret;
2024 hd_info.tail_pad = (u16)ret;
2025 total_len += (u16)ret;
2026 }
2027
2028 hd_info.channel = chan;
2029 hd_info.dat_offset = head_pad + bus->tx_hdrlen;
2030 hd_info.seq_num = txseq++;
2031
2032 /* Now fill the header */
2033 brcmf_sdio_hdpack(bus, pkt_next->data, &hd_info);
2034
2035 if (BRCMF_BYTES_ON() &&
2036 ((BRCMF_CTL_ON() && chan == SDPCM_CONTROL_CHANNEL) ||
2037 (BRCMF_DATA_ON() && chan != SDPCM_CONTROL_CHANNEL)))
2038 brcmf_dbg_hex_dump(true, pkt_next, hd_info.len,
2039 "Tx Frame:\n");
2040 else if (BRCMF_HDRS_ON())
2041 brcmf_dbg_hex_dump(true, pkt_next,
2042 head_pad + bus->tx_hdrlen,
2043 "Tx Header:\n");
Franky Linb05e9252013-08-10 12:27:26 +02002044 }
Franky Lin8da9d2c2013-11-29 12:25:10 +01002045 /* Hardware length tag of the first packet should be total
2046 * length of the chain (including padding)
2047 */
2048 if (bus->txglom)
2049 brcmf_sdio_update_hwhdr(pktq->next->data, total_len);
Franky Linb05e9252013-08-10 12:27:26 +02002050 return 0;
2051}
2052
2053/**
2054 * brcmf_sdio_txpkt_postp - packet post processing for transmit
2055 * @bus: brcmf_sdio structure pointer
2056 * @pktq: packet list pointer
2057 *
2058 * Processes to be applied to the packet
2059 * - Remove head padding
2060 * - Remove tail padding
2061 */
2062static void
2063brcmf_sdio_txpkt_postp(struct brcmf_sdio *bus, struct sk_buff_head *pktq)
2064{
2065 u8 *hdr;
2066 u32 dat_offset;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002067 u16 tail_pad;
Franky Linb05e9252013-08-10 12:27:26 +02002068 u32 dummy_flags, chop_len;
2069 struct sk_buff *pkt_next, *tmp, *pkt_prev;
2070
2071 skb_queue_walk_safe(pktq, pkt_next, tmp) {
2072 dummy_flags = *(u32 *)(pkt_next->cb);
Franky Lin5491c112013-09-25 13:05:43 +02002073 if (dummy_flags & ALIGN_SKB_FLAG) {
2074 chop_len = dummy_flags & ALIGN_SKB_CHOP_LEN_MASK;
Franky Linb05e9252013-08-10 12:27:26 +02002075 if (chop_len) {
2076 pkt_prev = pkt_next->prev;
Franky Linb05e9252013-08-10 12:27:26 +02002077 skb_put(pkt_prev, chop_len);
2078 }
2079 __skb_unlink(pkt_next, pktq);
2080 brcmu_pkt_buf_free_skb(pkt_next);
2081 } else {
Franky Lin8da9d2c2013-11-29 12:25:10 +01002082 hdr = pkt_next->data + bus->tx_hdrlen - SDPCM_SWHDR_LEN;
Franky Linb05e9252013-08-10 12:27:26 +02002083 dat_offset = le32_to_cpu(*(__le32 *)hdr);
2084 dat_offset = (dat_offset & SDPCM_DOFFSET_MASK) >>
2085 SDPCM_DOFFSET_SHIFT;
2086 skb_pull(pkt_next, dat_offset);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002087 if (bus->txglom) {
2088 tail_pad = le16_to_cpu(*(__le16 *)(hdr - 2));
2089 skb_trim(pkt_next, pkt_next->len - tail_pad);
2090 }
Franky Linb05e9252013-08-10 12:27:26 +02002091 }
2092 }
2093}
2094
Arend van Spriel5b435de2011-10-05 13:19:03 +02002095/* Writes a HW/SW header into the packet and sends it. */
2096/* Assumes: (a) header space already there, (b) caller holds lock */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002097static int brcmf_sdio_txpkt(struct brcmf_sdio *bus, struct sk_buff_head *pktq,
2098 uint chan)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002099{
2100 int ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002101 int i;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002102 struct sk_buff *pkt_next, *tmp;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002103
2104 brcmf_dbg(TRACE, "Enter\n");
2105
Franky Lin8da9d2c2013-11-29 12:25:10 +01002106 ret = brcmf_sdio_txpkt_prep(bus, pktq, chan);
Franky Linb05e9252013-08-10 12:27:26 +02002107 if (ret)
2108 goto done;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002109
Franky Lin38b0b0d2012-11-05 16:22:24 -08002110 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriela7cdd822013-12-12 11:58:59 +01002111 ret = brcmf_sdiod_send_pkt(bus->sdiodev, pktq);
Arend van Spriel80969832012-06-09 22:51:44 +02002112 bus->sdcnt.f2txdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002113
2114 if (ret < 0) {
2115 /* On failure, abort the command and terminate the frame */
2116 brcmf_dbg(INFO, "sdio error %d, abort command and terminate frame\n",
2117 ret);
Arend van Spriel80969832012-06-09 22:51:44 +02002118 bus->sdcnt.tx_sderrs++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002119
Arend van Spriela39be272013-12-12 11:58:58 +01002120 brcmf_sdiod_abort(bus->sdiodev, SDIO_FUNC_2);
2121 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL,
2122 SFC_WF_TERM, NULL);
Arend van Spriel80969832012-06-09 22:51:44 +02002123 bus->sdcnt.f1regdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002124
2125 for (i = 0; i < 3; i++) {
2126 u8 hi, lo;
Arend van Spriela39be272013-12-12 11:58:58 +01002127 hi = brcmf_sdiod_regrb(bus->sdiodev,
2128 SBSDIO_FUNC1_WFRAMEBCHI, NULL);
2129 lo = brcmf_sdiod_regrb(bus->sdiodev,
2130 SBSDIO_FUNC1_WFRAMEBCLO, NULL);
Arend van Spriel80969832012-06-09 22:51:44 +02002131 bus->sdcnt.f1regdata += 2;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002132 if ((hi == 0) && (lo == 0))
2133 break;
2134 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002135 }
Franky Lin38b0b0d2012-11-05 16:22:24 -08002136 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002137
2138done:
Franky Lin8da9d2c2013-11-29 12:25:10 +01002139 brcmf_sdio_txpkt_postp(bus, pktq);
2140 if (ret == 0)
2141 bus->tx_seq = (bus->tx_seq + pktq->qlen) % SDPCM_SEQ_WRAP;
2142 skb_queue_walk_safe(pktq, pkt_next, tmp) {
2143 __skb_unlink(pkt_next, pktq);
2144 brcmf_txcomplete(bus->sdiodev->dev, pkt_next, ret == 0);
2145 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002146 return ret;
2147}
2148
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002149static uint brcmf_sdio_sendfromq(struct brcmf_sdio *bus, uint maxframes)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002150{
2151 struct sk_buff *pkt;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002152 struct sk_buff_head pktq;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002153 u32 intstatus = 0;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002154 int ret = 0, prec_out, i;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002155 uint cnt = 0;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002156 u8 tx_prec_map, pkt_num;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002157
Arend van Spriel5b435de2011-10-05 13:19:03 +02002158 brcmf_dbg(TRACE, "Enter\n");
2159
2160 tx_prec_map = ~bus->flowcontrol;
2161
2162 /* Send frames until the limit or some other event */
Franky Lin8da9d2c2013-11-29 12:25:10 +01002163 for (cnt = 0; (cnt < maxframes) && data_ok(bus);) {
2164 pkt_num = 1;
2165 __skb_queue_head_init(&pktq);
2166 if (bus->txglom)
2167 pkt_num = min_t(u8, bus->tx_max - bus->tx_seq,
2168 brcmf_sdio_txglomsz);
2169 pkt_num = min_t(u32, pkt_num,
2170 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002171 spin_lock_bh(&bus->txqlock);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002172 for (i = 0; i < pkt_num; i++) {
2173 pkt = brcmu_pktq_mdeq(&bus->txq, tx_prec_map,
2174 &prec_out);
2175 if (pkt == NULL)
2176 break;
2177 __skb_queue_tail(&pktq, pkt);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002178 }
2179 spin_unlock_bh(&bus->txqlock);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002180 if (i == 0)
2181 break;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002182
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002183 ret = brcmf_sdio_txpkt(bus, &pktq, SDPCM_DATA_CHANNEL);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002184 cnt += i;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002185
2186 /* In poll mode, need to check for other events */
2187 if (!bus->intr && cnt) {
2188 /* Check device status, signal pending interrupt */
Franky Lin38b0b0d2012-11-05 16:22:24 -08002189 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin5c15c232012-05-04 18:27:37 -07002190 ret = r_sdreg32(bus, &intstatus,
2191 offsetof(struct sdpcmd_regs,
2192 intstatus));
Franky Lin38b0b0d2012-11-05 16:22:24 -08002193 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel80969832012-06-09 22:51:44 +02002194 bus->sdcnt.f2txdata++;
Franky Lin5c15c232012-05-04 18:27:37 -07002195 if (ret != 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002196 break;
2197 if (intstatus & bus->hostintmask)
Franky Lin1d382272012-09-13 21:11:59 +02002198 atomic_set(&bus->ipend, 1);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002199 }
2200 }
2201
2202 /* Deflow-control stack if needed */
Hante Meuleman05dde972013-02-06 18:40:36 +01002203 if ((bus->sdiodev->bus_if->state == BRCMF_BUS_DATA) &&
Franky Linc8bf3482011-12-16 18:37:08 -08002204 bus->txoff && (pktq_len(&bus->txq) < TXLOW)) {
Hante Meuleman90d03ff2012-09-11 21:18:46 +02002205 bus->txoff = false;
2206 brcmf_txflowblock(bus->sdiodev->dev, false);
Franky Linc8bf3482011-12-16 18:37:08 -08002207 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002208
2209 return cnt;
2210}
2211
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002212static void brcmf_sdio_bus_stop(struct device *dev)
Franky Lina9ffda82011-12-16 18:37:12 -08002213{
2214 u32 local_hostintmask;
2215 u8 saveclk;
Franky Lina9ffda82011-12-16 18:37:12 -08002216 int err;
2217 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01002218 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Lina9ffda82011-12-16 18:37:12 -08002219 struct brcmf_sdio *bus = sdiodev->bus;
2220
2221 brcmf_dbg(TRACE, "Enter\n");
2222
2223 if (bus->watchdog_tsk) {
2224 send_sig(SIGTERM, bus->watchdog_tsk, 1);
2225 kthread_stop(bus->watchdog_tsk);
2226 bus->watchdog_tsk = NULL;
2227 }
2228
Arend van Sprielbb350712014-01-13 22:20:29 +01002229 if (bus_if->state == BRCMF_BUS_DOWN) {
2230 sdio_claim_host(sdiodev->func[1]);
Franky Lina9ffda82011-12-16 18:37:12 -08002231
Arend van Sprielbb350712014-01-13 22:20:29 +01002232 /* Enable clock for device interrupts */
2233 brcmf_sdio_bus_sleep(bus, false, false);
Franky Lina9ffda82011-12-16 18:37:12 -08002234
Arend van Sprielbb350712014-01-13 22:20:29 +01002235 /* Disable and clear interrupts at the chip level also */
2236 w_sdreg32(bus, 0, offsetof(struct sdpcmd_regs, hostintmask));
2237 local_hostintmask = bus->hostintmask;
2238 bus->hostintmask = 0;
Franky Lina9ffda82011-12-16 18:37:12 -08002239
Arend van Sprielbb350712014-01-13 22:20:29 +01002240 /* Force backplane clocks to assure F2 interrupt propagates */
2241 saveclk = brcmf_sdiod_regrb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
2242 &err);
2243 if (!err)
2244 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
2245 (saveclk | SBSDIO_FORCE_HT), &err);
2246 if (err)
2247 brcmf_err("Failed to force clock for F2: err %d\n",
2248 err);
Franky Lina9ffda82011-12-16 18:37:12 -08002249
Arend van Sprielbb350712014-01-13 22:20:29 +01002250 /* Turn off the bus (F2), free any pending packets */
2251 brcmf_dbg(INTR, "disable SDIO interrupts\n");
2252 sdio_disable_func(sdiodev->func[SDIO_FUNC_2]);
2253
2254 /* Clear any pending interrupts now that F2 is disabled */
2255 w_sdreg32(bus, local_hostintmask,
2256 offsetof(struct sdpcmd_regs, intstatus));
2257
2258 sdio_release_host(sdiodev->func[1]);
Franky Lina9ffda82011-12-16 18:37:12 -08002259 }
Franky Lina9ffda82011-12-16 18:37:12 -08002260 /* Clear the data packet queues */
2261 brcmu_pktq_flush(&bus->txq, true, NULL, NULL);
2262
2263 /* Clear any held glomming stuff */
2264 if (bus->glomd)
2265 brcmu_pkt_buf_free_skb(bus->glomd);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002266 brcmf_sdio_free_glom(bus);
Franky Lina9ffda82011-12-16 18:37:12 -08002267
2268 /* Clear rx control and wake any waiters */
Franky Lindd43a012012-11-05 16:22:22 -08002269 spin_lock_bh(&bus->rxctl_lock);
Franky Lina9ffda82011-12-16 18:37:12 -08002270 bus->rxlen = 0;
Franky Lindd43a012012-11-05 16:22:22 -08002271 spin_unlock_bh(&bus->rxctl_lock);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002272 brcmf_sdio_dcmd_resp_wake(bus);
Franky Lina9ffda82011-12-16 18:37:12 -08002273
2274 /* Reset some F2 state stuff */
2275 bus->rxskip = false;
2276 bus->tx_seq = bus->rx_seq = 0;
Franky Lina9ffda82011-12-16 18:37:12 -08002277}
2278
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002279static inline void brcmf_sdio_clrintr(struct brcmf_sdio *bus)
Franky Linba89bf12012-04-27 18:56:59 -07002280{
2281 unsigned long flags;
2282
Hante Meuleman668761a2013-04-12 10:55:55 +02002283 if (bus->sdiodev->oob_irq_requested) {
2284 spin_lock_irqsave(&bus->sdiodev->irq_en_lock, flags);
2285 if (!bus->sdiodev->irq_en && !atomic_read(&bus->ipend)) {
2286 enable_irq(bus->sdiodev->pdata->oob_irq_nr);
2287 bus->sdiodev->irq_en = true;
2288 }
2289 spin_unlock_irqrestore(&bus->sdiodev->irq_en_lock, flags);
Franky Linba89bf12012-04-27 18:56:59 -07002290 }
Franky Linba89bf12012-04-27 18:56:59 -07002291}
Franky Linba89bf12012-04-27 18:56:59 -07002292
Franky Lin45316032012-09-13 21:12:03 +02002293static int brcmf_sdio_intr_rstatus(struct brcmf_sdio *bus)
2294{
2295 u8 idx;
2296 u32 addr;
2297 unsigned long val;
2298 int n, ret;
2299
2300 idx = brcmf_sdio_chip_getinfidx(bus->ci, BCMA_CORE_SDIO_DEV);
2301 addr = bus->ci->c_inf[idx].base +
2302 offsetof(struct sdpcmd_regs, intstatus);
2303
Arend van Spriela39be272013-12-12 11:58:58 +01002304 val = brcmf_sdiod_regrl(bus->sdiodev, addr, &ret);
Franky Lin45316032012-09-13 21:12:03 +02002305 bus->sdcnt.f1regdata++;
2306 if (ret != 0)
2307 val = 0;
2308
2309 val &= bus->hostintmask;
2310 atomic_set(&bus->fcstate, !!(val & I_HMB_FC_STATE));
2311
2312 /* Clear interrupts */
2313 if (val) {
Arend van Spriela39be272013-12-12 11:58:58 +01002314 brcmf_sdiod_regwl(bus->sdiodev, addr, val, &ret);
Franky Lin45316032012-09-13 21:12:03 +02002315 bus->sdcnt.f1regdata++;
2316 }
2317
2318 if (ret) {
2319 atomic_set(&bus->intstatus, 0);
2320 } else if (val) {
2321 for_each_set_bit(n, &val, 32)
2322 set_bit(n, (unsigned long *)&bus->intstatus.counter);
2323 }
2324
2325 return ret;
2326}
2327
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002328static void brcmf_sdio_dpc(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002329{
Franky Lin45316032012-09-13 21:12:03 +02002330 u32 newstatus = 0;
2331 unsigned long intstatus;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002332 uint rxlimit = bus->rxbound; /* Rx frames to read before resched */
2333 uint txlimit = bus->txbound; /* Tx frames to send before resched */
2334 uint framecnt = 0; /* Temporary counter of tx/rx frames */
Franky Lin45316032012-09-13 21:12:03 +02002335 int err = 0, n;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002336
2337 brcmf_dbg(TRACE, "Enter\n");
2338
Franky Lin38b0b0d2012-11-05 16:22:24 -08002339 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002340
2341 /* If waiting for HTAVAIL, check status */
Piotr Haber4a3da992013-04-11 13:28:46 +02002342 if (!bus->sr_enabled && bus->clkstate == CLK_PENDING) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02002343 u8 clkctl, devctl = 0;
2344
Joe Perches8ae74652012-01-15 00:38:38 -08002345#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02002346 /* Check for inconsistent device control */
Arend van Spriela39be272013-12-12 11:58:58 +01002347 devctl = brcmf_sdiod_regrb(bus->sdiodev,
2348 SBSDIO_DEVICE_CTL, &err);
Joe Perches8ae74652012-01-15 00:38:38 -08002349#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02002350
2351 /* Read CSR, if clock on switch to AVAIL, else ignore */
Arend van Spriela39be272013-12-12 11:58:58 +01002352 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
2353 SBSDIO_FUNC1_CHIPCLKCSR, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002354
Arend van Sprielc3203372013-04-03 12:40:44 +02002355 brcmf_dbg(SDIO, "DPC: PENDING, devctl 0x%02x clkctl 0x%02x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02002356 devctl, clkctl);
2357
2358 if (SBSDIO_HTAV(clkctl)) {
Arend van Spriela39be272013-12-12 11:58:58 +01002359 devctl = brcmf_sdiod_regrb(bus->sdiodev,
2360 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002361 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
Arend van Spriela39be272013-12-12 11:58:58 +01002362 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
2363 devctl, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002364 bus->clkstate = CLK_AVAIL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002365 }
2366 }
2367
Arend van Spriel5b435de2011-10-05 13:19:03 +02002368 /* Make sure backplane clock is on */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002369 brcmf_sdio_bus_sleep(bus, false, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002370
2371 /* Pending interrupt indicates new device status */
Franky Lin1d382272012-09-13 21:11:59 +02002372 if (atomic_read(&bus->ipend) > 0) {
2373 atomic_set(&bus->ipend, 0);
Franky Lin45316032012-09-13 21:12:03 +02002374 err = brcmf_sdio_intr_rstatus(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002375 }
2376
Franky Lin45316032012-09-13 21:12:03 +02002377 /* Start with leftover status bits */
2378 intstatus = atomic_xchg(&bus->intstatus, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002379
2380 /* Handle flow-control change: read new state in case our ack
2381 * crossed another change interrupt. If change still set, assume
2382 * FC ON for safety, let next loop through do the debounce.
2383 */
2384 if (intstatus & I_HMB_FC_CHANGE) {
2385 intstatus &= ~I_HMB_FC_CHANGE;
Franky Lin5c15c232012-05-04 18:27:37 -07002386 err = w_sdreg32(bus, I_HMB_FC_CHANGE,
2387 offsetof(struct sdpcmd_regs, intstatus));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002388
Franky Lin5c15c232012-05-04 18:27:37 -07002389 err = r_sdreg32(bus, &newstatus,
2390 offsetof(struct sdpcmd_regs, intstatus));
Arend van Spriel80969832012-06-09 22:51:44 +02002391 bus->sdcnt.f1regdata += 2;
Franky Lin45316032012-09-13 21:12:03 +02002392 atomic_set(&bus->fcstate,
2393 !!(newstatus & (I_HMB_FC_STATE | I_HMB_FC_CHANGE)));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002394 intstatus |= (newstatus & bus->hostintmask);
2395 }
2396
2397 /* Handle host mailbox indication */
2398 if (intstatus & I_HMB_HOST_INT) {
2399 intstatus &= ~I_HMB_HOST_INT;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002400 intstatus |= brcmf_sdio_hostmail(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002401 }
2402
Franky Lin38b0b0d2012-11-05 16:22:24 -08002403 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin7cdf57d2012-11-05 16:22:23 -08002404
Arend van Spriel5b435de2011-10-05 13:19:03 +02002405 /* Generally don't ask for these, can get CRC errors... */
2406 if (intstatus & I_WR_OOSYNC) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002407 brcmf_err("Dongle reports WR_OOSYNC\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002408 intstatus &= ~I_WR_OOSYNC;
2409 }
2410
2411 if (intstatus & I_RD_OOSYNC) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002412 brcmf_err("Dongle reports RD_OOSYNC\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002413 intstatus &= ~I_RD_OOSYNC;
2414 }
2415
2416 if (intstatus & I_SBINT) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002417 brcmf_err("Dongle reports SBINT\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002418 intstatus &= ~I_SBINT;
2419 }
2420
2421 /* Would be active due to wake-wlan in gSPI */
2422 if (intstatus & I_CHIPACTIVE) {
2423 brcmf_dbg(INFO, "Dongle reports CHIPACTIVE\n");
2424 intstatus &= ~I_CHIPACTIVE;
2425 }
2426
2427 /* Ignore frame indications if rxskip is set */
2428 if (bus->rxskip)
2429 intstatus &= ~I_HMB_FRAME_IND;
2430
2431 /* On frame indication, read available frames */
Franky Lin03d5c362012-09-13 21:12:01 +02002432 if (PKT_AVAILABLE() && bus->clkstate == CLK_AVAIL) {
Franky Lin4754fce2012-09-19 22:21:11 +02002433 framecnt = brcmf_sdio_readframes(bus, rxlimit);
2434 if (!bus->rxpending)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002435 intstatus &= ~I_HMB_FRAME_IND;
2436 rxlimit -= min(framecnt, rxlimit);
2437 }
2438
2439 /* Keep still-pending events for next scheduling */
Franky Lin45316032012-09-13 21:12:03 +02002440 if (intstatus) {
2441 for_each_set_bit(n, &intstatus, 32)
2442 set_bit(n, (unsigned long *)&bus->intstatus.counter);
2443 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002444
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002445 brcmf_sdio_clrintr(bus);
Franky Linba89bf12012-04-27 18:56:59 -07002446
Arend van Spriel5b435de2011-10-05 13:19:03 +02002447 if (data_ok(bus) && bus->ctrl_frame_stat &&
2448 (bus->clkstate == CLK_AVAIL)) {
Franky Lin03d5c362012-09-13 21:12:01 +02002449 int i;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002450
Franky Lin38b0b0d2012-11-05 16:22:24 -08002451 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriela7cdd822013-12-12 11:58:59 +01002452 err = brcmf_sdiod_send_buf(bus->sdiodev, bus->ctrl_frame_buf,
Arend van Spriela39be272013-12-12 11:58:58 +01002453 (u32)bus->ctrl_frame_len);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002454
Franky Lin03d5c362012-09-13 21:12:01 +02002455 if (err < 0) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02002456 /* On failure, abort the command and
2457 terminate the frame */
2458 brcmf_dbg(INFO, "sdio error %d, abort command and terminate frame\n",
Franky Lin03d5c362012-09-13 21:12:01 +02002459 err);
Arend van Spriel80969832012-06-09 22:51:44 +02002460 bus->sdcnt.tx_sderrs++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002461
Arend van Spriela39be272013-12-12 11:58:58 +01002462 brcmf_sdiod_abort(bus->sdiodev, SDIO_FUNC_2);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002463
Arend van Spriela39be272013-12-12 11:58:58 +01002464 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL,
2465 SFC_WF_TERM, &err);
Arend van Spriel80969832012-06-09 22:51:44 +02002466 bus->sdcnt.f1regdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002467
2468 for (i = 0; i < 3; i++) {
2469 u8 hi, lo;
Arend van Spriela39be272013-12-12 11:58:58 +01002470 hi = brcmf_sdiod_regrb(bus->sdiodev,
2471 SBSDIO_FUNC1_WFRAMEBCHI,
2472 &err);
2473 lo = brcmf_sdiod_regrb(bus->sdiodev,
2474 SBSDIO_FUNC1_WFRAMEBCLO,
2475 &err);
Arend van Spriel80969832012-06-09 22:51:44 +02002476 bus->sdcnt.f1regdata += 2;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002477 if ((hi == 0) && (lo == 0))
2478 break;
2479 }
2480
Franky Lin03d5c362012-09-13 21:12:01 +02002481 } else {
Franky Lin6bc52312013-08-10 12:27:28 +02002482 bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQ_WRAP;
Franky Lin03d5c362012-09-13 21:12:01 +02002483 }
Franky Lin38b0b0d2012-11-05 16:22:24 -08002484 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002485 bus->ctrl_frame_stat = false;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002486 brcmf_sdio_wait_event_wakeup(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002487 }
2488 /* Send queued frames (limit 1 if rx may still be pending) */
Franky Lin45316032012-09-13 21:12:03 +02002489 else if ((bus->clkstate == CLK_AVAIL) && !atomic_read(&bus->fcstate) &&
Arend van Spriel5b435de2011-10-05 13:19:03 +02002490 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) && txlimit
2491 && data_ok(bus)) {
Franky Lin4754fce2012-09-19 22:21:11 +02002492 framecnt = bus->rxpending ? min(txlimit, bus->txminmax) :
2493 txlimit;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002494 framecnt = brcmf_sdio_sendfromq(bus, framecnt);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002495 txlimit -= framecnt;
2496 }
2497
Arend van Sprielbb350712014-01-13 22:20:29 +01002498 if (!brcmf_bus_ready(bus->sdiodev->bus_if) || (err != 0)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002499 brcmf_err("failed backplane access over SDIO, halting operation\n");
Franky Lin45316032012-09-13 21:12:03 +02002500 atomic_set(&bus->intstatus, 0);
2501 } else if (atomic_read(&bus->intstatus) ||
2502 atomic_read(&bus->ipend) > 0 ||
2503 (!atomic_read(&bus->fcstate) &&
2504 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) &&
2505 data_ok(bus)) || PKT_AVAILABLE()) {
Arend van Sprielfccfe932013-06-18 13:29:26 +02002506 atomic_inc(&bus->dpc_tskcnt);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002507 }
2508
Arend van Spriel5b435de2011-10-05 13:19:03 +02002509 /* If we're done for now, turn off clock request. */
2510 if ((bus->clkstate != CLK_PENDING)
2511 && bus->idletime == BRCMF_IDLE_IMMEDIATE) {
2512 bus->activity = false;
Piotr Haber4a3da992013-04-11 13:28:46 +02002513 brcmf_dbg(SDIO, "idle state\n");
Franky Lin38b0b0d2012-11-05 16:22:24 -08002514 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002515 brcmf_sdio_bus_sleep(bus, true, false);
Franky Lin38b0b0d2012-11-05 16:22:24 -08002516 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002517 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002518}
2519
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002520static struct pktq *brcmf_sdio_bus_gettxq(struct device *dev)
Arend van Spriele2432b62013-04-03 12:40:38 +02002521{
2522 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
2523 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
2524 struct brcmf_sdio *bus = sdiodev->bus;
2525
2526 return &bus->txq;
2527}
2528
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002529static int brcmf_sdio_bus_txdata(struct device *dev, struct sk_buff *pkt)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002530{
2531 int ret = -EBADE;
2532 uint datalen, prec;
Franky Linbf347bb2011-11-22 17:21:56 -08002533 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01002534 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Linbf347bb2011-11-22 17:21:56 -08002535 struct brcmf_sdio *bus = sdiodev->bus;
Arend van Spriel4061f892013-08-10 12:27:19 +02002536 ulong flags;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002537
2538 brcmf_dbg(TRACE, "Enter\n");
2539
2540 datalen = pkt->len;
2541
2542 /* Add space for the header */
Franky Lin706478c2013-08-10 12:27:29 +02002543 skb_push(pkt, bus->tx_hdrlen);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002544 /* precondition: IS_ALIGNED((unsigned long)(pkt->data), 2) */
2545
2546 prec = prio2prec((pkt->priority & PRIOMASK));
2547
2548 /* Check for existing queue, current flow-control,
2549 pending event, or pending clock */
2550 brcmf_dbg(TRACE, "deferring pktq len %d\n", pktq_len(&bus->txq));
Arend van Spriel80969832012-06-09 22:51:44 +02002551 bus->sdcnt.fcqueued++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002552
2553 /* Priority based enq */
Arend van Spriel4061f892013-08-10 12:27:19 +02002554 spin_lock_irqsave(&bus->txqlock, flags);
Joe Perches23677ce2012-02-09 11:17:23 +00002555 if (!brcmf_c_prec_enq(bus->sdiodev->dev, &bus->txq, pkt, prec)) {
Franky Lin706478c2013-08-10 12:27:29 +02002556 skb_pull(pkt, bus->tx_hdrlen);
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002557 brcmf_err("out of bus->txq !!!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002558 ret = -ENOSR;
2559 } else {
2560 ret = 0;
2561 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002562
Franky Linc8bf3482011-12-16 18:37:08 -08002563 if (pktq_len(&bus->txq) >= TXHI) {
Hante Meuleman90d03ff2012-09-11 21:18:46 +02002564 bus->txoff = true;
2565 brcmf_txflowblock(bus->sdiodev->dev, true);
Franky Linc8bf3482011-12-16 18:37:08 -08002566 }
Arend van Spriel4061f892013-08-10 12:27:19 +02002567 spin_unlock_irqrestore(&bus->txqlock, flags);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002568
Joe Perches8ae74652012-01-15 00:38:38 -08002569#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02002570 if (pktq_plen(&bus->txq, prec) > qcount[prec])
2571 qcount[prec] = pktq_plen(&bus->txq, prec);
2572#endif
Franky Linf1e68c22012-09-13 21:12:00 +02002573
Arend van Sprielfccfe932013-06-18 13:29:26 +02002574 if (atomic_read(&bus->dpc_tskcnt) == 0) {
2575 atomic_inc(&bus->dpc_tskcnt);
Franky Linf1e68c22012-09-13 21:12:00 +02002576 queue_work(bus->brcmf_wq, &bus->datawork);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002577 }
2578
2579 return ret;
2580}
2581
Joe Perches8ae74652012-01-15 00:38:38 -08002582#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02002583#define CONSOLE_LINE_MAX 192
2584
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002585static int brcmf_sdio_readconsole(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002586{
2587 struct brcmf_console *c = &bus->console;
2588 u8 line[CONSOLE_LINE_MAX], ch;
2589 u32 n, idx, addr;
2590 int rv;
2591
2592 /* Don't do anything until FWREADY updates console address */
2593 if (bus->console_addr == 0)
2594 return 0;
2595
2596 /* Read console log struct */
2597 addr = bus->console_addr + offsetof(struct rte_console, log_le);
Arend van Spriela39be272013-12-12 11:58:58 +01002598 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, (u8 *)&c->log_le,
2599 sizeof(c->log_le));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002600 if (rv < 0)
2601 return rv;
2602
2603 /* Allocate console buffer (one time only) */
2604 if (c->buf == NULL) {
2605 c->bufsize = le32_to_cpu(c->log_le.buf_size);
2606 c->buf = kmalloc(c->bufsize, GFP_ATOMIC);
2607 if (c->buf == NULL)
2608 return -ENOMEM;
2609 }
2610
2611 idx = le32_to_cpu(c->log_le.idx);
2612
2613 /* Protect against corrupt value */
2614 if (idx > c->bufsize)
2615 return -EBADE;
2616
2617 /* Skip reading the console buffer if the index pointer
2618 has not moved */
2619 if (idx == c->last)
2620 return 0;
2621
2622 /* Read the console buffer */
2623 addr = le32_to_cpu(c->log_le.buf);
Arend van Spriela39be272013-12-12 11:58:58 +01002624 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, c->buf, c->bufsize);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002625 if (rv < 0)
2626 return rv;
2627
2628 while (c->last != idx) {
2629 for (n = 0; n < CONSOLE_LINE_MAX - 2; n++) {
2630 if (c->last == idx) {
2631 /* This would output a partial line.
2632 * Instead, back up
2633 * the buffer pointer and output this
2634 * line next time around.
2635 */
2636 if (c->last >= n)
2637 c->last -= n;
2638 else
2639 c->last = c->bufsize - n;
2640 goto break2;
2641 }
2642 ch = c->buf[c->last];
2643 c->last = (c->last + 1) % c->bufsize;
2644 if (ch == '\n')
2645 break;
2646 line[n] = ch;
2647 }
2648
2649 if (n > 0) {
2650 if (line[n - 1] == '\r')
2651 n--;
2652 line[n] = 0;
Joe Perches18aad4f2012-01-15 00:38:42 -08002653 pr_debug("CONSOLE: %s\n", line);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002654 }
2655 }
2656break2:
2657
2658 return 0;
2659}
Joe Perches8ae74652012-01-15 00:38:38 -08002660#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02002661
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002662static int brcmf_sdio_tx_frame(struct brcmf_sdio *bus, u8 *frame, u16 len)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002663{
2664 int i;
2665 int ret;
2666
2667 bus->ctrl_frame_stat = false;
Arend van Spriela7cdd822013-12-12 11:58:59 +01002668 ret = brcmf_sdiod_send_buf(bus->sdiodev, frame, len);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002669
2670 if (ret < 0) {
2671 /* On failure, abort the command and terminate the frame */
2672 brcmf_dbg(INFO, "sdio error %d, abort command and terminate frame\n",
2673 ret);
Arend van Spriel80969832012-06-09 22:51:44 +02002674 bus->sdcnt.tx_sderrs++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002675
Arend van Spriela39be272013-12-12 11:58:58 +01002676 brcmf_sdiod_abort(bus->sdiodev, SDIO_FUNC_2);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002677
Arend van Spriela39be272013-12-12 11:58:58 +01002678 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL,
2679 SFC_WF_TERM, NULL);
Arend van Spriel80969832012-06-09 22:51:44 +02002680 bus->sdcnt.f1regdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002681
2682 for (i = 0; i < 3; i++) {
2683 u8 hi, lo;
Arend van Spriela39be272013-12-12 11:58:58 +01002684 hi = brcmf_sdiod_regrb(bus->sdiodev,
2685 SBSDIO_FUNC1_WFRAMEBCHI, NULL);
2686 lo = brcmf_sdiod_regrb(bus->sdiodev,
2687 SBSDIO_FUNC1_WFRAMEBCLO, NULL);
Arend van Spriel80969832012-06-09 22:51:44 +02002688 bus->sdcnt.f1regdata += 2;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002689 if (hi == 0 && lo == 0)
2690 break;
2691 }
2692 return ret;
2693 }
2694
Franky Lin6bc52312013-08-10 12:27:28 +02002695 bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQ_WRAP;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002696
2697 return ret;
2698}
2699
Franky Linfcf094f2011-12-16 18:37:16 -08002700static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002701brcmf_sdio_bus_txctl(struct device *dev, unsigned char *msg, uint msglen)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002702{
2703 u8 *frame;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002704 u16 len, pad;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002705 uint retries = 0;
2706 u8 doff = 0;
2707 int ret = -1;
Franky Lin47a1ce72011-11-22 17:21:55 -08002708 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01002709 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Lin47a1ce72011-11-22 17:21:55 -08002710 struct brcmf_sdio *bus = sdiodev->bus;
Franky Lin6bc52312013-08-10 12:27:28 +02002711 struct brcmf_sdio_hdrinfo hd_info = {0};
Arend van Spriel5b435de2011-10-05 13:19:03 +02002712
2713 brcmf_dbg(TRACE, "Enter\n");
2714
2715 /* Back the pointer to make a room for bus header */
Franky Lin706478c2013-08-10 12:27:29 +02002716 frame = msg - bus->tx_hdrlen;
2717 len = (msglen += bus->tx_hdrlen);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002718
2719 /* Add alignment padding (optional for ctl frames) */
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01002720 doff = ((unsigned long)frame % bus->head_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002721 if (doff) {
2722 frame -= doff;
2723 len += doff;
2724 msglen += doff;
Franky Lin706478c2013-08-10 12:27:29 +02002725 memset(frame, 0, doff + bus->tx_hdrlen);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002726 }
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01002727 /* precondition: doff < bus->head_align */
Franky Lin706478c2013-08-10 12:27:29 +02002728 doff += bus->tx_hdrlen;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002729
2730 /* Round send length to next SDIO block */
Franky Lin8da9d2c2013-11-29 12:25:10 +01002731 pad = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002732 if (bus->roundup && bus->blocksize && (len > bus->blocksize)) {
Franky Lin8da9d2c2013-11-29 12:25:10 +01002733 pad = bus->blocksize - (len % bus->blocksize);
2734 if ((pad > bus->roundup) || (pad >= bus->blocksize))
2735 pad = 0;
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01002736 } else if (len % bus->head_align) {
2737 pad = bus->head_align - (len % bus->head_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002738 }
Franky Lin8da9d2c2013-11-29 12:25:10 +01002739 len += pad;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002740
Arend van Spriel5b435de2011-10-05 13:19:03 +02002741 /* precondition: IS_ALIGNED((unsigned long)frame, 2) */
2742
Arend van Spriel5b435de2011-10-05 13:19:03 +02002743 /* Make sure backplane clock is on */
Franky Lin38b0b0d2012-11-05 16:22:24 -08002744 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002745 brcmf_sdio_bus_sleep(bus, false, false);
Franky Lin38b0b0d2012-11-05 16:22:24 -08002746 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002747
Franky Lin6bc52312013-08-10 12:27:28 +02002748 hd_info.len = (u16)msglen;
2749 hd_info.channel = SDPCM_CONTROL_CHANNEL;
2750 hd_info.dat_offset = doff;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002751 hd_info.seq_num = bus->tx_seq;
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01002752 hd_info.lastfrm = true;
2753 hd_info.tail_pad = pad;
Franky Lin6bc52312013-08-10 12:27:28 +02002754 brcmf_sdio_hdpack(bus, frame, &hd_info);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002755
Franky Lin8da9d2c2013-11-29 12:25:10 +01002756 if (bus->txglom)
2757 brcmf_sdio_update_hwhdr(frame, len);
2758
Arend van Spriel5b435de2011-10-05 13:19:03 +02002759 if (!data_ok(bus)) {
2760 brcmf_dbg(INFO, "No bus credit bus->tx_max %d, bus->tx_seq %d\n",
2761 bus->tx_max, bus->tx_seq);
2762 bus->ctrl_frame_stat = true;
2763 /* Send from dpc */
2764 bus->ctrl_frame_buf = frame;
2765 bus->ctrl_frame_len = len;
2766
Franky Linfd67dc82012-11-05 16:22:25 -08002767 wait_event_interruptible_timeout(bus->ctrl_wait,
2768 !bus->ctrl_frame_stat,
2769 msecs_to_jiffies(2000));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002770
Joe Perches23677ce2012-02-09 11:17:23 +00002771 if (!bus->ctrl_frame_stat) {
Arend van Sprielc3203372013-04-03 12:40:44 +02002772 brcmf_dbg(SDIO, "ctrl_frame_stat == false\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002773 ret = 0;
2774 } else {
Arend van Sprielc3203372013-04-03 12:40:44 +02002775 brcmf_dbg(SDIO, "ctrl_frame_stat == true\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002776 ret = -1;
2777 }
2778 }
2779
2780 if (ret == -1) {
Joe Perches1e023822012-01-15 00:38:40 -08002781 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_CTL_ON(),
2782 frame, len, "Tx Frame:\n");
2783 brcmf_dbg_hex_dump(!(BRCMF_BYTES_ON() && BRCMF_CTL_ON()) &&
2784 BRCMF_HDRS_ON(),
2785 frame, min_t(u16, len, 16), "TxHdr:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002786
2787 do {
Franky Lin38b0b0d2012-11-05 16:22:24 -08002788 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002789 ret = brcmf_sdio_tx_frame(bus, frame, len);
Franky Lin38b0b0d2012-11-05 16:22:24 -08002790 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002791 } while (ret < 0 && retries++ < TXRETRIES);
2792 }
2793
Franky Linf1e68c22012-09-13 21:12:00 +02002794 if ((bus->idletime == BRCMF_IDLE_IMMEDIATE) &&
Arend van Sprielfccfe932013-06-18 13:29:26 +02002795 atomic_read(&bus->dpc_tskcnt) == 0) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02002796 bus->activity = false;
Franky Lin38b0b0d2012-11-05 16:22:24 -08002797 sdio_claim_host(bus->sdiodev->func[1]);
Piotr Haber4a3da992013-04-11 13:28:46 +02002798 brcmf_dbg(INFO, "idle\n");
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002799 brcmf_sdio_clkctl(bus, CLK_NONE, true);
Franky Lin38b0b0d2012-11-05 16:22:24 -08002800 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002801 }
2802
Arend van Spriel5b435de2011-10-05 13:19:03 +02002803 if (ret)
Arend van Spriel80969832012-06-09 22:51:44 +02002804 bus->sdcnt.tx_ctlerrs++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002805 else
Arend van Spriel80969832012-06-09 22:51:44 +02002806 bus->sdcnt.tx_ctlpkts++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002807
2808 return ret ? -EIO : 0;
2809}
2810
Arend van Spriel80969832012-06-09 22:51:44 +02002811#ifdef DEBUG
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002812static inline bool brcmf_sdio_valid_shared_address(u32 addr)
2813{
2814 return !(addr == 0 || ((~addr >> 16) & 0xffff) == (addr & 0xffff));
2815}
2816
2817static int brcmf_sdio_readshared(struct brcmf_sdio *bus,
2818 struct sdpcm_shared *sh)
2819{
2820 u32 addr;
2821 int rv;
2822 u32 shaddr = 0;
2823 struct sdpcm_shared_le sh_le;
2824 __le32 addr_le;
2825
Franky Lin1640f282013-04-11 13:28:51 +02002826 shaddr = bus->ci->rambase + bus->ramsize - 4;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002827
2828 /*
2829 * Read last word in socram to determine
2830 * address of sdpcm_shared structure
2831 */
Franky Lin38b0b0d2012-11-05 16:22:24 -08002832 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002833 brcmf_sdio_bus_sleep(bus, false, false);
Arend van Spriela39be272013-12-12 11:58:58 +01002834 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, shaddr, (u8 *)&addr_le, 4);
Piotr Haberb55de972013-02-08 12:06:33 +01002835 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002836 if (rv < 0)
2837 return rv;
2838
2839 addr = le32_to_cpu(addr_le);
2840
Arend van Sprielc3203372013-04-03 12:40:44 +02002841 brcmf_dbg(SDIO, "sdpcm_shared address 0x%08X\n", addr);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002842
2843 /*
2844 * Check if addr is valid.
2845 * NVRAM length at the end of memory should have been overwritten.
2846 */
2847 if (!brcmf_sdio_valid_shared_address(addr)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002848 brcmf_err("invalid sdpcm_shared address 0x%08X\n",
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002849 addr);
2850 return -EINVAL;
2851 }
2852
2853 /* Read hndrte_shared structure */
Arend van Spriela39be272013-12-12 11:58:58 +01002854 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, (u8 *)&sh_le,
2855 sizeof(struct sdpcm_shared_le));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002856 if (rv < 0)
2857 return rv;
2858
2859 /* Endianness */
2860 sh->flags = le32_to_cpu(sh_le.flags);
2861 sh->trap_addr = le32_to_cpu(sh_le.trap_addr);
2862 sh->assert_exp_addr = le32_to_cpu(sh_le.assert_exp_addr);
2863 sh->assert_file_addr = le32_to_cpu(sh_le.assert_file_addr);
2864 sh->assert_line = le32_to_cpu(sh_le.assert_line);
2865 sh->console_addr = le32_to_cpu(sh_le.console_addr);
2866 sh->msgtrace_addr = le32_to_cpu(sh_le.msgtrace_addr);
2867
Piotr Haber86dcd932013-04-05 10:57:43 +02002868 if ((sh->flags & SDPCM_SHARED_VERSION_MASK) > SDPCM_SHARED_VERSION) {
2869 brcmf_err("sdpcm shared version unsupported: dhd %d dongle %d\n",
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002870 SDPCM_SHARED_VERSION,
2871 sh->flags & SDPCM_SHARED_VERSION_MASK);
2872 return -EPROTO;
2873 }
2874
2875 return 0;
2876}
2877
2878static int brcmf_sdio_dump_console(struct brcmf_sdio *bus,
2879 struct sdpcm_shared *sh, char __user *data,
2880 size_t count)
2881{
2882 u32 addr, console_ptr, console_size, console_index;
2883 char *conbuf = NULL;
2884 __le32 sh_val;
2885 int rv;
2886 loff_t pos = 0;
2887 int nbytes = 0;
2888
2889 /* obtain console information from device memory */
2890 addr = sh->console_addr + offsetof(struct rte_console, log_le);
Arend van Spriela39be272013-12-12 11:58:58 +01002891 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
2892 (u8 *)&sh_val, sizeof(u32));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002893 if (rv < 0)
2894 return rv;
2895 console_ptr = le32_to_cpu(sh_val);
2896
2897 addr = sh->console_addr + offsetof(struct rte_console, log_le.buf_size);
Arend van Spriela39be272013-12-12 11:58:58 +01002898 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
2899 (u8 *)&sh_val, sizeof(u32));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002900 if (rv < 0)
2901 return rv;
2902 console_size = le32_to_cpu(sh_val);
2903
2904 addr = sh->console_addr + offsetof(struct rte_console, log_le.idx);
Arend van Spriela39be272013-12-12 11:58:58 +01002905 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
2906 (u8 *)&sh_val, sizeof(u32));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002907 if (rv < 0)
2908 return rv;
2909 console_index = le32_to_cpu(sh_val);
2910
2911 /* allocate buffer for console data */
2912 if (console_size <= CONSOLE_BUFFER_MAX)
2913 conbuf = vzalloc(console_size+1);
2914
2915 if (!conbuf)
2916 return -ENOMEM;
2917
2918 /* obtain the console data from device */
2919 conbuf[console_size] = '\0';
Arend van Spriela39be272013-12-12 11:58:58 +01002920 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, console_ptr, (u8 *)conbuf,
2921 console_size);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002922 if (rv < 0)
2923 goto done;
2924
2925 rv = simple_read_from_buffer(data, count, &pos,
2926 conbuf + console_index,
2927 console_size - console_index);
2928 if (rv < 0)
2929 goto done;
2930
2931 nbytes = rv;
2932 if (console_index > 0) {
2933 pos = 0;
2934 rv = simple_read_from_buffer(data+nbytes, count, &pos,
2935 conbuf, console_index - 1);
2936 if (rv < 0)
2937 goto done;
2938 rv += nbytes;
2939 }
2940done:
2941 vfree(conbuf);
2942 return rv;
2943}
2944
2945static int brcmf_sdio_trap_info(struct brcmf_sdio *bus, struct sdpcm_shared *sh,
2946 char __user *data, size_t count)
2947{
2948 int error, res;
2949 char buf[350];
2950 struct brcmf_trap_info tr;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002951 loff_t pos = 0;
2952
Piotr Haberbaa9e602013-04-05 10:57:42 +02002953 if ((sh->flags & SDPCM_SHARED_TRAP) == 0) {
2954 brcmf_dbg(INFO, "no trap in firmware\n");
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002955 return 0;
Piotr Haberbaa9e602013-04-05 10:57:42 +02002956 }
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002957
Arend van Spriela39be272013-12-12 11:58:58 +01002958 error = brcmf_sdiod_ramrw(bus->sdiodev, false, sh->trap_addr, (u8 *)&tr,
2959 sizeof(struct brcmf_trap_info));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002960 if (error < 0)
2961 return error;
2962
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002963 res = scnprintf(buf, sizeof(buf),
2964 "dongle trap info: type 0x%x @ epc 0x%08x\n"
2965 " cpsr 0x%08x spsr 0x%08x sp 0x%08x\n"
2966 " lr 0x%08x pc 0x%08x offset 0x%x\n"
2967 " r0 0x%08x r1 0x%08x r2 0x%08x r3 0x%08x\n"
2968 " r4 0x%08x r5 0x%08x r6 0x%08x r7 0x%08x\n",
2969 le32_to_cpu(tr.type), le32_to_cpu(tr.epc),
2970 le32_to_cpu(tr.cpsr), le32_to_cpu(tr.spsr),
2971 le32_to_cpu(tr.r13), le32_to_cpu(tr.r14),
Arend van Spriel9bd02c62012-06-26 21:26:40 +02002972 le32_to_cpu(tr.pc), sh->trap_addr,
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002973 le32_to_cpu(tr.r0), le32_to_cpu(tr.r1),
2974 le32_to_cpu(tr.r2), le32_to_cpu(tr.r3),
2975 le32_to_cpu(tr.r4), le32_to_cpu(tr.r5),
2976 le32_to_cpu(tr.r6), le32_to_cpu(tr.r7));
2977
Piotr Haberbaa9e602013-04-05 10:57:42 +02002978 return simple_read_from_buffer(data, count, &pos, buf, res);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002979}
2980
2981static int brcmf_sdio_assert_info(struct brcmf_sdio *bus,
2982 struct sdpcm_shared *sh, char __user *data,
2983 size_t count)
2984{
2985 int error = 0;
2986 char buf[200];
2987 char file[80] = "?";
2988 char expr[80] = "<???>";
2989 int res;
2990 loff_t pos = 0;
2991
2992 if ((sh->flags & SDPCM_SHARED_ASSERT_BUILT) == 0) {
2993 brcmf_dbg(INFO, "firmware not built with -assert\n");
2994 return 0;
2995 } else if ((sh->flags & SDPCM_SHARED_ASSERT) == 0) {
2996 brcmf_dbg(INFO, "no assert in dongle\n");
2997 return 0;
2998 }
2999
Franky Lin38b0b0d2012-11-05 16:22:24 -08003000 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003001 if (sh->assert_file_addr != 0) {
Arend van Spriela39be272013-12-12 11:58:58 +01003002 error = brcmf_sdiod_ramrw(bus->sdiodev, false,
3003 sh->assert_file_addr, (u8 *)file, 80);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003004 if (error < 0)
3005 return error;
3006 }
3007 if (sh->assert_exp_addr != 0) {
Arend van Spriela39be272013-12-12 11:58:58 +01003008 error = brcmf_sdiod_ramrw(bus->sdiodev, false,
3009 sh->assert_exp_addr, (u8 *)expr, 80);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003010 if (error < 0)
3011 return error;
3012 }
Franky Lin38b0b0d2012-11-05 16:22:24 -08003013 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003014
3015 res = scnprintf(buf, sizeof(buf),
3016 "dongle assert: %s:%d: assert(%s)\n",
3017 file, sh->assert_line, expr);
3018 return simple_read_from_buffer(data, count, &pos, buf, res);
3019}
3020
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003021static int brcmf_sdio_checkdied(struct brcmf_sdio *bus)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003022{
3023 int error;
3024 struct sdpcm_shared sh;
3025
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003026 error = brcmf_sdio_readshared(bus, &sh);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003027
3028 if (error < 0)
3029 return error;
3030
3031 if ((sh.flags & SDPCM_SHARED_ASSERT_BUILT) == 0)
3032 brcmf_dbg(INFO, "firmware not built with -assert\n");
3033 else if (sh.flags & SDPCM_SHARED_ASSERT)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003034 brcmf_err("assertion in dongle\n");
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003035
3036 if (sh.flags & SDPCM_SHARED_TRAP)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003037 brcmf_err("firmware trap in dongle\n");
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003038
3039 return 0;
3040}
3041
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003042static int brcmf_sdio_died_dump(struct brcmf_sdio *bus, char __user *data,
3043 size_t count, loff_t *ppos)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003044{
3045 int error = 0;
3046 struct sdpcm_shared sh;
3047 int nbytes = 0;
3048 loff_t pos = *ppos;
3049
3050 if (pos != 0)
3051 return 0;
3052
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003053 error = brcmf_sdio_readshared(bus, &sh);
3054 if (error < 0)
3055 goto done;
3056
3057 error = brcmf_sdio_assert_info(bus, &sh, data, count);
3058 if (error < 0)
3059 goto done;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003060 nbytes = error;
Piotr Haberbaa9e602013-04-05 10:57:42 +02003061
3062 error = brcmf_sdio_trap_info(bus, &sh, data+nbytes, count);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003063 if (error < 0)
3064 goto done;
Piotr Haberbaa9e602013-04-05 10:57:42 +02003065 nbytes += error;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003066
Piotr Haberbaa9e602013-04-05 10:57:42 +02003067 error = brcmf_sdio_dump_console(bus, &sh, data+nbytes, count);
3068 if (error < 0)
3069 goto done;
3070 nbytes += error;
3071
3072 error = nbytes;
3073 *ppos += nbytes;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003074done:
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003075 return error;
3076}
3077
3078static ssize_t brcmf_sdio_forensic_read(struct file *f, char __user *data,
3079 size_t count, loff_t *ppos)
3080{
3081 struct brcmf_sdio *bus = f->private_data;
3082 int res;
3083
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003084 res = brcmf_sdio_died_dump(bus, data, count, ppos);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003085 if (res > 0)
3086 *ppos += res;
3087 return (ssize_t)res;
3088}
3089
3090static const struct file_operations brcmf_sdio_forensic_ops = {
3091 .owner = THIS_MODULE,
3092 .open = simple_open,
3093 .read = brcmf_sdio_forensic_read
3094};
3095
Arend van Spriel80969832012-06-09 22:51:44 +02003096static void brcmf_sdio_debugfs_create(struct brcmf_sdio *bus)
3097{
3098 struct brcmf_pub *drvr = bus->sdiodev->bus_if->drvr;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003099 struct dentry *dentry = brcmf_debugfs_get_devdir(drvr);
Arend van Spriel80969832012-06-09 22:51:44 +02003100
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003101 if (IS_ERR_OR_NULL(dentry))
3102 return;
3103
3104 debugfs_create_file("forensics", S_IRUGO, dentry, bus,
3105 &brcmf_sdio_forensic_ops);
Arend van Spriel80969832012-06-09 22:51:44 +02003106 brcmf_debugfs_create_sdio_count(drvr, &bus->sdcnt);
3107}
3108#else
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003109static int brcmf_sdio_checkdied(struct brcmf_sdio *bus)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003110{
3111 return 0;
3112}
3113
Arend van Spriel80969832012-06-09 22:51:44 +02003114static void brcmf_sdio_debugfs_create(struct brcmf_sdio *bus)
3115{
3116}
3117#endif /* DEBUG */
3118
Franky Linfcf094f2011-12-16 18:37:16 -08003119static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003120brcmf_sdio_bus_rxctl(struct device *dev, unsigned char *msg, uint msglen)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003121{
3122 int timeleft;
3123 uint rxlen = 0;
3124 bool pending;
Franky Lindd43a012012-11-05 16:22:22 -08003125 u8 *buf;
Franky Lin532cdd32011-11-22 17:21:54 -08003126 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01003127 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Lin532cdd32011-11-22 17:21:54 -08003128 struct brcmf_sdio *bus = sdiodev->bus;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003129
3130 brcmf_dbg(TRACE, "Enter\n");
3131
3132 /* Wait until control frame is available */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003133 timeleft = brcmf_sdio_dcmd_resp_wait(bus, &bus->rxlen, &pending);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003134
Franky Lindd43a012012-11-05 16:22:22 -08003135 spin_lock_bh(&bus->rxctl_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003136 rxlen = bus->rxlen;
3137 memcpy(msg, bus->rxctl, min(msglen, rxlen));
Franky Lindd43a012012-11-05 16:22:22 -08003138 bus->rxctl = NULL;
3139 buf = bus->rxctl_orig;
3140 bus->rxctl_orig = NULL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003141 bus->rxlen = 0;
Franky Lindd43a012012-11-05 16:22:22 -08003142 spin_unlock_bh(&bus->rxctl_lock);
3143 vfree(buf);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003144
3145 if (rxlen) {
3146 brcmf_dbg(CTL, "resumed on rxctl frame, got %d expected %d\n",
3147 rxlen, msglen);
3148 } else if (timeleft == 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003149 brcmf_err("resumed on timeout\n");
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003150 brcmf_sdio_checkdied(bus);
Joe Perches23677ce2012-02-09 11:17:23 +00003151 } else if (pending) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003152 brcmf_dbg(CTL, "cancelled\n");
3153 return -ERESTARTSYS;
3154 } else {
3155 brcmf_dbg(CTL, "resumed for unknown reason?\n");
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003156 brcmf_sdio_checkdied(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003157 }
3158
3159 if (rxlen)
Arend van Spriel80969832012-06-09 22:51:44 +02003160 bus->sdcnt.rx_ctlpkts++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003161 else
Arend van Spriel80969832012-06-09 22:51:44 +02003162 bus->sdcnt.rx_ctlerrs++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003163
3164 return rxlen ? (int)rxlen : -ETIMEDOUT;
3165}
3166
Hante Meulemana74d0362014-01-13 22:20:22 +01003167#ifdef DEBUG
3168static bool
3169brcmf_sdio_verifymemory(struct brcmf_sdio_dev *sdiodev, u32 ram_addr,
3170 u8 *ram_data, uint ram_sz)
3171{
3172 char *ram_cmp;
3173 int err;
3174 bool ret = true;
3175 int address;
3176 int offset;
3177 int len;
3178
3179 /* read back and verify */
3180 brcmf_dbg(INFO, "Compare RAM dl & ul at 0x%08x; size=%d\n", ram_addr,
3181 ram_sz);
3182 ram_cmp = kmalloc(MEMBLOCK, GFP_KERNEL);
3183 /* do not proceed while no memory but */
3184 if (!ram_cmp)
3185 return true;
3186
3187 address = ram_addr;
3188 offset = 0;
3189 while (offset < ram_sz) {
3190 len = ((offset + MEMBLOCK) < ram_sz) ? MEMBLOCK :
3191 ram_sz - offset;
3192 err = brcmf_sdiod_ramrw(sdiodev, false, address, ram_cmp, len);
3193 if (err) {
3194 brcmf_err("error %d on reading %d membytes at 0x%08x\n",
3195 err, len, address);
3196 ret = false;
3197 break;
3198 } else if (memcmp(ram_cmp, &ram_data[offset], len)) {
3199 brcmf_err("Downloaded RAM image is corrupted, block offset is %d, len is %d\n",
3200 offset, len);
3201 ret = false;
3202 break;
3203 }
3204 offset += len;
3205 address += len;
3206 }
3207
3208 kfree(ram_cmp);
3209
3210 return ret;
3211}
3212#else /* DEBUG */
3213static bool
3214brcmf_sdio_verifymemory(struct brcmf_sdio_dev *sdiodev, u32 ram_addr,
3215 u8 *ram_data, uint ram_sz)
3216{
3217 return true;
3218}
3219#endif /* DEBUG */
3220
Arend van Spriel33556502014-01-13 22:20:25 +01003221static int brcmf_sdio_download_code_file(struct brcmf_sdio *bus,
3222 const struct firmware *fw)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003223{
Hante Meulemanf2c44fe2013-09-25 13:05:45 +02003224 int err;
Franky Lin1640f282013-04-11 13:28:51 +02003225 int offset;
Hante Meulemanf2c44fe2013-09-25 13:05:45 +02003226 int address;
3227 int len;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003228
Hante Meulemana74d0362014-01-13 22:20:22 +01003229 brcmf_dbg(TRACE, "Enter\n");
3230
Hante Meulemanf2c44fe2013-09-25 13:05:45 +02003231 err = 0;
3232 offset = 0;
3233 address = bus->ci->rambase;
3234 while (offset < fw->size) {
3235 len = ((offset + MEMBLOCK) < fw->size) ? MEMBLOCK :
3236 fw->size - offset;
Arend van Spriela39be272013-12-12 11:58:58 +01003237 err = brcmf_sdiod_ramrw(bus->sdiodev, true, address,
3238 (u8 *)&fw->data[offset], len);
Hante Meulemanf2c44fe2013-09-25 13:05:45 +02003239 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003240 brcmf_err("error %d on writing %d membytes at 0x%08x\n",
Hante Meulemanf2c44fe2013-09-25 13:05:45 +02003241 err, len, address);
Arend van Spriel33556502014-01-13 22:20:25 +01003242 return err;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003243 }
Hante Meulemanf2c44fe2013-09-25 13:05:45 +02003244 offset += len;
3245 address += len;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003246 }
Hante Meulemana74d0362014-01-13 22:20:22 +01003247 if (!err)
3248 if (!brcmf_sdio_verifymemory(bus->sdiodev, bus->ci->rambase,
3249 (u8 *)fw->data, fw->size))
3250 err = -EIO;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003251
Hante Meulemanf2c44fe2013-09-25 13:05:45 +02003252 return err;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003253}
3254
Arend van Spriel33556502014-01-13 22:20:25 +01003255static int brcmf_sdio_download_nvram(struct brcmf_sdio *bus,
3256 const struct firmware *nv)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003257{
Hante Meulemana74d0362014-01-13 22:20:22 +01003258 void *vars;
3259 u32 varsz;
3260 int address;
3261 int err;
3262
3263 brcmf_dbg(TRACE, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003264
Hante Meulemana74d0362014-01-13 22:20:22 +01003265 vars = brcmf_nvram_strip(nv, &varsz);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003266
Hante Meulemana74d0362014-01-13 22:20:22 +01003267 if (vars == NULL)
3268 return -EINVAL;
3269
3270 address = bus->ci->ramsize - varsz + bus->ci->rambase;
3271 err = brcmf_sdiod_ramrw(bus->sdiodev, true, address, vars, varsz);
3272 if (err)
3273 brcmf_err("error %d on writing %d nvram bytes at 0x%08x\n",
3274 err, varsz, address);
3275 else if (!brcmf_sdio_verifymemory(bus->sdiodev, address, vars, varsz))
3276 err = -EIO;
3277
3278 brcmf_nvram_free(vars);
3279
3280 return err;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003281}
3282
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003283static int brcmf_sdio_download_firmware(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003284{
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003285 int bcmerror = -EFAULT;
Arend van Spriel33556502014-01-13 22:20:25 +01003286 const struct firmware *fw;
3287 u32 rstvec;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003288
3289 sdio_claim_host(bus->sdiodev->func[1]);
3290 brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003291
3292 /* Keep arm in reset */
Arend van Spriel33556502014-01-13 22:20:25 +01003293 brcmf_sdio_chip_enter_download(bus->sdiodev, bus->ci);
3294
3295 fw = brcmf_sdio_get_fw(bus, BRCMF_FIRMWARE_BIN);
3296 if (fw == NULL) {
3297 bcmerror = -ENOENT;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003298 goto err;
3299 }
3300
Arend van Spriel33556502014-01-13 22:20:25 +01003301 rstvec = get_unaligned_le32(fw->data);
3302 brcmf_dbg(SDIO, "firmware rstvec: %x\n", rstvec);
3303
3304 bcmerror = brcmf_sdio_download_code_file(bus, fw);
3305 release_firmware(fw);
3306 if (bcmerror) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003307 brcmf_err("dongle image file download failed\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003308 goto err;
3309 }
3310
Arend van Spriel33556502014-01-13 22:20:25 +01003311 fw = brcmf_sdio_get_fw(bus, BRCMF_FIRMWARE_NVRAM);
3312 if (fw == NULL) {
3313 bcmerror = -ENOENT;
3314 goto err;
3315 }
3316
3317 bcmerror = brcmf_sdio_download_nvram(bus, fw);
3318 release_firmware(fw);
3319 if (bcmerror) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003320 brcmf_err("dongle nvram file download failed\n");
Franky Lin3eaa9562013-04-02 21:06:20 +02003321 goto err;
3322 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02003323
3324 /* Take arm out of reset */
Arend van Spriel33556502014-01-13 22:20:25 +01003325 if (!brcmf_sdio_chip_exit_download(bus->sdiodev, bus->ci, rstvec)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003326 brcmf_err("error getting out of ARM core reset\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003327 goto err;
3328 }
3329
Arend van Spriel33556502014-01-13 22:20:25 +01003330 /* Allow HT Clock now that the ARM is running. */
Arend van Sprielbb350712014-01-13 22:20:29 +01003331 brcmf_bus_change_state(bus->sdiodev->bus_if, BRCMF_BUS_LOAD);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003332 bcmerror = 0;
3333
3334err:
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003335 brcmf_sdio_clkctl(bus, CLK_SDONLY, false);
3336 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003337 return bcmerror;
3338}
3339
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003340static bool brcmf_sdio_sr_capable(struct brcmf_sdio *bus)
Piotr Haber4a3da992013-04-11 13:28:46 +02003341{
Arend van Spriel3bd44d92014-01-06 12:40:43 +01003342 u32 addr, reg, pmu_cc3_mask = ~0;
3343 int err;
Piotr Haber4a3da992013-04-11 13:28:46 +02003344
3345 brcmf_dbg(TRACE, "Enter\n");
3346
3347 /* old chips with PMU version less than 17 don't support save restore */
3348 if (bus->ci->pmurev < 17)
3349 return false;
3350
Arend van Spriel3bd44d92014-01-06 12:40:43 +01003351 switch (bus->ci->chip) {
3352 case BCM43241_CHIP_ID:
3353 case BCM4335_CHIP_ID:
3354 case BCM4339_CHIP_ID:
3355 /* read PMU chipcontrol register 3 */
3356 addr = CORE_CC_REG(bus->ci->c_inf[0].base, chipcontrol_addr);
3357 brcmf_sdiod_regwl(bus->sdiodev, addr, 3, NULL);
3358 addr = CORE_CC_REG(bus->ci->c_inf[0].base, chipcontrol_data);
3359 reg = brcmf_sdiod_regrl(bus->sdiodev, addr, NULL);
3360 return (reg & pmu_cc3_mask) != 0;
3361 default:
3362 addr = CORE_CC_REG(bus->ci->c_inf[0].base, pmucapabilities_ext);
3363 reg = brcmf_sdiod_regrl(bus->sdiodev, addr, &err);
3364 if ((reg & PCAPEXT_SR_SUPPORTED_MASK) == 0)
3365 return false;
Piotr Haber4a3da992013-04-11 13:28:46 +02003366
Arend van Spriel3bd44d92014-01-06 12:40:43 +01003367 addr = CORE_CC_REG(bus->ci->c_inf[0].base, retention_ctl);
3368 reg = brcmf_sdiod_regrl(bus->sdiodev, addr, NULL);
3369 return (reg & (PMU_RCTL_MACPHY_DISABLE_MASK |
3370 PMU_RCTL_LOGIC_DISABLE_MASK)) == 0;
3371 }
Piotr Haber4a3da992013-04-11 13:28:46 +02003372}
3373
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003374static void brcmf_sdio_sr_init(struct brcmf_sdio *bus)
Piotr Haber4a3da992013-04-11 13:28:46 +02003375{
3376 int err = 0;
3377 u8 val;
3378
3379 brcmf_dbg(TRACE, "Enter\n");
3380
Arend van Spriela39be272013-12-12 11:58:58 +01003381 val = brcmf_sdiod_regrb(bus->sdiodev, SBSDIO_FUNC1_WAKEUPCTRL, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003382 if (err) {
3383 brcmf_err("error reading SBSDIO_FUNC1_WAKEUPCTRL\n");
3384 return;
3385 }
3386
3387 val |= 1 << SBSDIO_FUNC1_WCTRL_HTWAIT_SHIFT;
Arend van Spriela39be272013-12-12 11:58:58 +01003388 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_WAKEUPCTRL, val, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003389 if (err) {
3390 brcmf_err("error writing SBSDIO_FUNC1_WAKEUPCTRL\n");
3391 return;
3392 }
3393
3394 /* Add CMD14 Support */
Arend van Spriela39be272013-12-12 11:58:58 +01003395 brcmf_sdiod_regwb(bus->sdiodev, SDIO_CCCR_BRCM_CARDCAP,
3396 (SDIO_CCCR_BRCM_CARDCAP_CMD14_SUPPORT |
3397 SDIO_CCCR_BRCM_CARDCAP_CMD14_EXT),
3398 &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003399 if (err) {
3400 brcmf_err("error writing SDIO_CCCR_BRCM_CARDCAP\n");
3401 return;
3402 }
3403
Arend van Spriela39be272013-12-12 11:58:58 +01003404 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3405 SBSDIO_FORCE_HT, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003406 if (err) {
3407 brcmf_err("error writing SBSDIO_FUNC1_CHIPCLKCSR\n");
3408 return;
3409 }
3410
3411 /* set flag */
3412 bus->sr_enabled = true;
3413 brcmf_dbg(INFO, "SR enabled\n");
3414}
3415
3416/* enable KSO bit */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003417static int brcmf_sdio_kso_init(struct brcmf_sdio *bus)
Piotr Haber4a3da992013-04-11 13:28:46 +02003418{
3419 u8 val;
3420 int err = 0;
3421
3422 brcmf_dbg(TRACE, "Enter\n");
3423
3424 /* KSO bit added in SDIO core rev 12 */
3425 if (bus->ci->c_inf[1].rev < 12)
3426 return 0;
3427
Arend van Spriela39be272013-12-12 11:58:58 +01003428 val = brcmf_sdiod_regrb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003429 if (err) {
3430 brcmf_err("error reading SBSDIO_FUNC1_SLEEPCSR\n");
3431 return err;
3432 }
3433
3434 if (!(val & SBSDIO_FUNC1_SLEEPCSR_KSO_MASK)) {
3435 val |= (SBSDIO_FUNC1_SLEEPCSR_KSO_EN <<
3436 SBSDIO_FUNC1_SLEEPCSR_KSO_SHIFT);
Arend van Spriela39be272013-12-12 11:58:58 +01003437 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
3438 val, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003439 if (err) {
3440 brcmf_err("error writing SBSDIO_FUNC1_SLEEPCSR\n");
3441 return err;
3442 }
3443 }
3444
3445 return 0;
3446}
3447
3448
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003449static int brcmf_sdio_bus_preinit(struct device *dev)
Arend van Sprielcf458282013-11-29 11:48:15 +01003450{
3451 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3452 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3453 struct brcmf_sdio *bus = sdiodev->bus;
Franky Lin8da9d2c2013-11-29 12:25:10 +01003454 uint pad_size;
Arend van Sprielcf458282013-11-29 11:48:15 +01003455 u32 value;
3456 u8 idx;
3457 int err;
3458
Franky Lin8da9d2c2013-11-29 12:25:10 +01003459 /* the commands below use the terms tx and rx from
3460 * a device perspective, ie. bus:txglom affects the
3461 * bus transfers from device to host.
3462 */
Arend van Sprielcf458282013-11-29 11:48:15 +01003463 idx = brcmf_sdio_chip_getinfidx(bus->ci, BCMA_CORE_SDIO_DEV);
3464 if (bus->ci->c_inf[idx].rev < 12) {
3465 /* for sdio core rev < 12, disable txgloming */
3466 value = 0;
3467 err = brcmf_iovar_data_set(dev, "bus:txglom", &value,
3468 sizeof(u32));
3469 } else {
3470 /* otherwise, set txglomalign */
3471 value = 4;
3472 if (sdiodev->pdata)
3473 value = sdiodev->pdata->sd_sgentry_align;
3474 /* SDIO ADMA requires at least 32 bit alignment */
3475 value = max_t(u32, value, 4);
3476 err = brcmf_iovar_data_set(dev, "bus:txglomalign", &value,
3477 sizeof(u32));
3478 }
Franky Lin8da9d2c2013-11-29 12:25:10 +01003479
3480 if (err < 0)
3481 goto done;
3482
3483 bus->tx_hdrlen = SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN;
3484 if (sdiodev->sg_support) {
3485 bus->txglom = false;
3486 value = 1;
3487 pad_size = bus->sdiodev->func[2]->cur_blksize << 1;
Franky Lin8da9d2c2013-11-29 12:25:10 +01003488 err = brcmf_iovar_data_set(bus->sdiodev->dev, "bus:rxglom",
3489 &value, sizeof(u32));
3490 if (err < 0) {
3491 /* bus:rxglom is allowed to fail */
3492 err = 0;
3493 } else {
3494 bus->txglom = true;
3495 bus->tx_hdrlen += SDPCM_HWEXT_LEN;
3496 }
3497 }
3498 brcmf_bus_add_txhdrlen(bus->sdiodev->dev, bus->tx_hdrlen);
3499
3500done:
Arend van Sprielcf458282013-11-29 11:48:15 +01003501 return err;
3502}
3503
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003504static int brcmf_sdio_bus_init(struct device *dev)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003505{
Franky Linfa20b912011-11-22 17:21:57 -08003506 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01003507 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Linfa20b912011-11-22 17:21:57 -08003508 struct brcmf_sdio *bus = sdiodev->bus;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003509 int err, ret = 0;
3510 u8 saveclk;
3511
3512 brcmf_dbg(TRACE, "Enter\n");
3513
3514 /* try to download image and nvram to the dongle */
Franky Linfa20b912011-11-22 17:21:57 -08003515 if (bus_if->state == BRCMF_BUS_DOWN) {
Arend van Spriel33556502014-01-13 22:20:25 +01003516 bus->alp_only = true;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003517 err = brcmf_sdio_download_firmware(bus);
3518 if (err)
3519 return err;
Arend van Spriel33556502014-01-13 22:20:25 +01003520 bus->alp_only = false;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003521 }
3522
Franky Lin712ac5b2011-12-16 18:37:09 -08003523 if (!bus->sdiodev->bus_if->drvr)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003524 return 0;
3525
3526 /* Start the watchdog timer */
Arend van Spriel80969832012-06-09 22:51:44 +02003527 bus->sdcnt.tickcnt = 0;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003528 brcmf_sdio_wd_timer(bus, BRCMF_WD_POLL_MS);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003529
Franky Lin38b0b0d2012-11-05 16:22:24 -08003530 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003531
3532 /* Make sure backplane clock is on, needed to generate F2 interrupt */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003533 brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003534 if (bus->clkstate != CLK_AVAIL)
3535 goto exit;
3536
3537 /* Force clocks on backplane to be sure F2 interrupt propagates */
Arend van Spriela39be272013-12-12 11:58:58 +01003538 saveclk = brcmf_sdiod_regrb(bus->sdiodev,
3539 SBSDIO_FUNC1_CHIPCLKCSR, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003540 if (!err) {
Arend van Spriela39be272013-12-12 11:58:58 +01003541 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3542 (saveclk | SBSDIO_FORCE_HT), &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003543 }
3544 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003545 brcmf_err("Failed to force clock for F2: err %d\n", err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003546 goto exit;
3547 }
3548
3549 /* Enable function 2 (frame transfers) */
3550 w_sdreg32(bus, SDPCM_PROT_VERSION << SMB_DATA_VERSION_SHIFT,
Franky Lin58692752012-05-04 18:27:36 -07003551 offsetof(struct sdpcmd_regs, tosbmailboxdata));
Arend van Spriel71370eb2013-12-12 11:58:53 +01003552 err = sdio_enable_func(bus->sdiodev->func[SDIO_FUNC_2]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003553
Arend van Spriel5b435de2011-10-05 13:19:03 +02003554
Arend van Spriel71370eb2013-12-12 11:58:53 +01003555 brcmf_dbg(INFO, "enable F2: err=%d\n", err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003556
3557 /* If F2 successfully enabled, set core and enable interrupts */
Arend van Spriel71370eb2013-12-12 11:58:53 +01003558 if (!err) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003559 /* Set up the interrupt mask and enable interrupts */
3560 bus->hostintmask = HOSTINTMASK;
3561 w_sdreg32(bus, bus->hostintmask,
Franky Lin58692752012-05-04 18:27:36 -07003562 offsetof(struct sdpcmd_regs, hostintmask));
Arend van Spriel5b435de2011-10-05 13:19:03 +02003563
Arend van Spriela39be272013-12-12 11:58:58 +01003564 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_WATERMARK, 8, &err);
Arend van Sprielc0e89f02012-02-09 21:09:04 +01003565 } else {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003566 /* Disable F2 again */
Arend van Spriel71370eb2013-12-12 11:58:53 +01003567 sdio_disable_func(bus->sdiodev->func[SDIO_FUNC_2]);
Arend van Sprielc0e89f02012-02-09 21:09:04 +01003568 ret = -ENODEV;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003569 }
3570
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003571 if (brcmf_sdio_sr_capable(bus)) {
3572 brcmf_sdio_sr_init(bus);
Piotr Haber4a3da992013-04-11 13:28:46 +02003573 } else {
3574 /* Restore previous clock setting */
Arend van Spriela39be272013-12-12 11:58:58 +01003575 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3576 saveclk, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003577 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02003578
Franky Line2f93cc2012-04-27 18:56:58 -07003579 if (ret == 0) {
Arend van Spriela39be272013-12-12 11:58:58 +01003580 ret = brcmf_sdiod_intr_register(bus->sdiodev);
Franky Line2f93cc2012-04-27 18:56:58 -07003581 if (ret != 0)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003582 brcmf_err("intr register failed:%d\n", ret);
Franky Line2f93cc2012-04-27 18:56:58 -07003583 }
3584
Arend van Spriel5b435de2011-10-05 13:19:03 +02003585 /* If we didn't come up, turn off backplane clock */
Arend van Spriel76a4c682014-01-06 12:40:45 +01003586 if (ret != 0)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003587 brcmf_sdio_clkctl(bus, CLK_NONE, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003588
3589exit:
Franky Lin38b0b0d2012-11-05 16:22:24 -08003590 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003591
3592 return ret;
3593}
3594
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003595void brcmf_sdio_isr(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003596{
Arend van Spriel5b435de2011-10-05 13:19:03 +02003597 brcmf_dbg(TRACE, "Enter\n");
3598
3599 if (!bus) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003600 brcmf_err("bus is null pointer, exiting\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003601 return;
3602 }
3603
Arend van Sprielbb350712014-01-13 22:20:29 +01003604 if (!brcmf_bus_ready(bus->sdiodev->bus_if)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003605 brcmf_err("bus is down. we have nothing to do\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003606 return;
3607 }
3608 /* Count the interrupt call */
Arend van Spriel80969832012-06-09 22:51:44 +02003609 bus->sdcnt.intrcount++;
Franky Lin45316032012-09-13 21:12:03 +02003610 if (in_interrupt())
3611 atomic_set(&bus->ipend, 1);
3612 else
3613 if (brcmf_sdio_intr_rstatus(bus)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003614 brcmf_err("failed backplane access\n");
Franky Lin45316032012-09-13 21:12:03 +02003615 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02003616
Arend van Spriel5b435de2011-10-05 13:19:03 +02003617 /* Disable additional interrupts (is this needed now)? */
3618 if (!bus->intr)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003619 brcmf_err("isr w/o interrupt configured!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003620
Arend van Sprielfccfe932013-06-18 13:29:26 +02003621 atomic_inc(&bus->dpc_tskcnt);
Franky Linf1e68c22012-09-13 21:12:00 +02003622 queue_work(bus->brcmf_wq, &bus->datawork);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003623}
3624
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003625static bool brcmf_sdio_bus_watchdog(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003626{
Joe Perches8ae74652012-01-15 00:38:38 -08003627#ifdef DEBUG
Franky Lincad2b262011-11-22 17:21:53 -08003628 struct brcmf_bus *bus_if = dev_get_drvdata(bus->sdiodev->dev);
Joe Perches8ae74652012-01-15 00:38:38 -08003629#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02003630
3631 brcmf_dbg(TIMER, "Enter\n");
3632
Arend van Spriel5b435de2011-10-05 13:19:03 +02003633 /* Poll period: check device if appropriate. */
Piotr Haber4a3da992013-04-11 13:28:46 +02003634 if (!bus->sr_enabled &&
3635 bus->poll && (++bus->polltick >= bus->pollrate)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003636 u32 intstatus = 0;
3637
3638 /* Reset poll tick */
3639 bus->polltick = 0;
3640
3641 /* Check device if no interrupts */
Arend van Spriel80969832012-06-09 22:51:44 +02003642 if (!bus->intr ||
3643 (bus->sdcnt.intrcount == bus->sdcnt.lastintrs)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003644
Arend van Sprielfccfe932013-06-18 13:29:26 +02003645 if (atomic_read(&bus->dpc_tskcnt) == 0) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003646 u8 devpend;
Arend van Sprielfccfe932013-06-18 13:29:26 +02003647
Franky Lin38b0b0d2012-11-05 16:22:24 -08003648 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriela39be272013-12-12 11:58:58 +01003649 devpend = brcmf_sdiod_regrb(bus->sdiodev,
3650 SDIO_CCCR_INTx,
3651 NULL);
Franky Lin38b0b0d2012-11-05 16:22:24 -08003652 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003653 intstatus =
3654 devpend & (INTR_STATUS_FUNC1 |
3655 INTR_STATUS_FUNC2);
3656 }
3657
3658 /* If there is something, make like the ISR and
3659 schedule the DPC */
3660 if (intstatus) {
Arend van Spriel80969832012-06-09 22:51:44 +02003661 bus->sdcnt.pollcnt++;
Franky Lin1d382272012-09-13 21:11:59 +02003662 atomic_set(&bus->ipend, 1);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003663
Arend van Sprielfccfe932013-06-18 13:29:26 +02003664 atomic_inc(&bus->dpc_tskcnt);
Franky Linf1e68c22012-09-13 21:12:00 +02003665 queue_work(bus->brcmf_wq, &bus->datawork);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003666 }
3667 }
3668
3669 /* Update interrupt tracking */
Arend van Spriel80969832012-06-09 22:51:44 +02003670 bus->sdcnt.lastintrs = bus->sdcnt.intrcount;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003671 }
Joe Perches8ae74652012-01-15 00:38:38 -08003672#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02003673 /* Poll for console output periodically */
Hante Meuleman2def5c12012-11-14 18:46:19 -08003674 if (bus_if && bus_if->state == BRCMF_BUS_DATA &&
Franky Lin8d169aa2011-11-22 17:21:52 -08003675 bus->console_interval != 0) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003676 bus->console.count += BRCMF_WD_POLL_MS;
3677 if (bus->console.count >= bus->console_interval) {
3678 bus->console.count -= bus->console_interval;
Franky Lin38b0b0d2012-11-05 16:22:24 -08003679 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003680 /* Make sure backplane clock is on */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003681 brcmf_sdio_bus_sleep(bus, false, false);
3682 if (brcmf_sdio_readconsole(bus) < 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003683 /* stop on error */
3684 bus->console_interval = 0;
Franky Lin38b0b0d2012-11-05 16:22:24 -08003685 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003686 }
3687 }
Joe Perches8ae74652012-01-15 00:38:38 -08003688#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02003689
3690 /* On idle timeout clear activity flag and/or turn off clock */
3691 if ((bus->idletime > 0) && (bus->clkstate == CLK_AVAIL)) {
3692 if (++bus->idlecount >= bus->idletime) {
3693 bus->idlecount = 0;
3694 if (bus->activity) {
3695 bus->activity = false;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003696 brcmf_sdio_wd_timer(bus, BRCMF_WD_POLL_MS);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003697 } else {
Piotr Haber4a3da992013-04-11 13:28:46 +02003698 brcmf_dbg(SDIO, "idle\n");
Franky Lin38b0b0d2012-11-05 16:22:24 -08003699 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003700 brcmf_sdio_bus_sleep(bus, true, false);
Franky Lin38b0b0d2012-11-05 16:22:24 -08003701 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003702 }
3703 }
3704 }
3705
Franky Lin1d382272012-09-13 21:11:59 +02003706 return (atomic_read(&bus->ipend) > 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003707}
3708
Franky Linf1e68c22012-09-13 21:12:00 +02003709static void brcmf_sdio_dataworker(struct work_struct *work)
3710{
3711 struct brcmf_sdio *bus = container_of(work, struct brcmf_sdio,
3712 datawork);
Franky Linf1e68c22012-09-13 21:12:00 +02003713
Arend van Sprielfccfe932013-06-18 13:29:26 +02003714 while (atomic_read(&bus->dpc_tskcnt)) {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003715 brcmf_sdio_dpc(bus);
Arend van Sprielfccfe932013-06-18 13:29:26 +02003716 atomic_dec(&bus->dpc_tskcnt);
Franky Linf1e68c22012-09-13 21:12:00 +02003717 }
Franky Linf1e68c22012-09-13 21:12:00 +02003718}
3719
Arend van Spriel5b435de2011-10-05 13:19:03 +02003720static bool
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003721brcmf_sdio_probe_attach(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003722{
3723 u8 clkctl = 0;
3724 int err = 0;
3725 int reg_addr;
3726 u32 reg_val;
Hante Meuleman668761a2013-04-12 10:55:55 +02003727 u32 drivestrength;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003728
Franky Lin38b0b0d2012-11-05 16:22:24 -08003729 sdio_claim_host(bus->sdiodev->func[1]);
3730
Joe Perches18aad4f2012-01-15 00:38:42 -08003731 pr_debug("F1 signature read @0x18000000=0x%4x\n",
Arend van Spriela39be272013-12-12 11:58:58 +01003732 brcmf_sdiod_regrl(bus->sdiodev, SI_ENUM_BASE, NULL));
Arend van Spriel5b435de2011-10-05 13:19:03 +02003733
3734 /*
Franky Lina97e4fc2011-11-04 22:23:35 +01003735 * Force PLL off until brcmf_sdio_chip_attach()
Arend van Spriel5b435de2011-10-05 13:19:03 +02003736 * programs PLL control regs
3737 */
3738
Arend van Spriela39be272013-12-12 11:58:58 +01003739 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3740 BRCMF_INIT_CLKCTL1, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003741 if (!err)
Arend van Spriela39be272013-12-12 11:58:58 +01003742 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
3743 SBSDIO_FUNC1_CHIPCLKCSR, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003744
3745 if (err || ((clkctl & ~SBSDIO_AVBITS) != BRCMF_INIT_CLKCTL1)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003746 brcmf_err("ChipClkCSR access: err %d wrote 0x%02x read 0x%02x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02003747 err, BRCMF_INIT_CLKCTL1, clkctl);
3748 goto fail;
3749 }
3750
Arend van Sprielbb350712014-01-13 22:20:29 +01003751 /* SDIO register access works so moving
3752 * state from UNKNOWN to DOWN.
3753 */
3754 brcmf_bus_change_state(bus->sdiodev->bus_if, BRCMF_BUS_DOWN);
3755
Arend van Spriel4744d162013-12-12 11:58:55 +01003756 if (brcmf_sdio_chip_attach(bus->sdiodev, &bus->ci)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003757 brcmf_err("brcmf_sdio_chip_attach failed!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003758 goto fail;
3759 }
3760
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003761 if (brcmf_sdio_kso_init(bus)) {
Piotr Haber4a3da992013-04-11 13:28:46 +02003762 brcmf_err("error enabling KSO\n");
3763 goto fail;
3764 }
3765
Hante Meuleman668761a2013-04-12 10:55:55 +02003766 if ((bus->sdiodev->pdata) && (bus->sdiodev->pdata->drive_strength))
3767 drivestrength = bus->sdiodev->pdata->drive_strength;
3768 else
3769 drivestrength = DEFAULT_SDIO_DRIVE_STRENGTH;
3770 brcmf_sdio_chip_drivestrengthinit(bus->sdiodev, bus->ci, drivestrength);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003771
Franky Lin454d2a82011-11-04 22:23:37 +01003772 /* Get info on the SOCRAM cores... */
Arend van Spriel5b435de2011-10-05 13:19:03 +02003773 bus->ramsize = bus->ci->ramsize;
3774 if (!(bus->ramsize)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003775 brcmf_err("failed to find SOCRAM memory!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003776 goto fail;
3777 }
3778
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003779 /* Set card control so an SDIO card reset does a WLAN backplane reset */
Arend van Spriela39be272013-12-12 11:58:58 +01003780 reg_val = brcmf_sdiod_regrb(bus->sdiodev,
3781 SDIO_CCCR_BRCM_CARDCTRL, &err);
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003782 if (err)
3783 goto fail;
3784
3785 reg_val |= SDIO_CCCR_BRCM_CARDCTRL_WLANRESET;
3786
Arend van Spriela39be272013-12-12 11:58:58 +01003787 brcmf_sdiod_regwb(bus->sdiodev,
3788 SDIO_CCCR_BRCM_CARDCTRL, reg_val, &err);
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003789 if (err)
3790 goto fail;
3791
3792 /* set PMUControl so a backplane reset does PMU state reload */
3793 reg_addr = CORE_CC_REG(bus->ci->c_inf[0].base,
3794 pmucontrol);
Arend van Spriela39be272013-12-12 11:58:58 +01003795 reg_val = brcmf_sdiod_regrl(bus->sdiodev,
3796 reg_addr,
3797 &err);
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003798 if (err)
3799 goto fail;
3800
3801 reg_val |= (BCMA_CC_PMU_CTL_RES_RELOAD << BCMA_CC_PMU_CTL_RES_SHIFT);
3802
Arend van Spriela39be272013-12-12 11:58:58 +01003803 brcmf_sdiod_regwl(bus->sdiodev,
3804 reg_addr,
3805 reg_val,
3806 &err);
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003807 if (err)
3808 goto fail;
3809
Arend van Spriel5b435de2011-10-05 13:19:03 +02003810
Franky Lin38b0b0d2012-11-05 16:22:24 -08003811 sdio_release_host(bus->sdiodev->func[1]);
3812
Arend van Spriel5b435de2011-10-05 13:19:03 +02003813 brcmu_pktq_init(&bus->txq, (PRIOMASK + 1), TXQLEN);
3814
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01003815 /* allocate header buffer */
3816 bus->hdrbuf = kzalloc(MAX_HDR_READ + bus->head_align, GFP_KERNEL);
3817 if (!bus->hdrbuf)
3818 return false;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003819 /* Locate an appropriately-aligned portion of hdrbuf */
3820 bus->rxhdr = (u8 *) roundup((unsigned long)&bus->hdrbuf[0],
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01003821 bus->head_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003822
3823 /* Set the poll and/or interrupt flags */
3824 bus->intr = true;
3825 bus->poll = false;
3826 if (bus->poll)
3827 bus->pollrate = 1;
3828
3829 return true;
3830
3831fail:
Franky Lin38b0b0d2012-11-05 16:22:24 -08003832 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003833 return false;
3834}
3835
Arend van Spriel5b435de2011-10-05 13:19:03 +02003836static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003837brcmf_sdio_watchdog_thread(void *data)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003838{
Franky Line92eedf2011-11-22 17:21:50 -08003839 struct brcmf_sdio *bus = (struct brcmf_sdio *)data;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003840
3841 allow_signal(SIGTERM);
3842 /* Run until signal received */
3843 while (1) {
3844 if (kthread_should_stop())
3845 break;
3846 if (!wait_for_completion_interruptible(&bus->watchdog_wait)) {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003847 brcmf_sdio_bus_watchdog(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003848 /* Count the tick for reference */
Arend van Spriel80969832012-06-09 22:51:44 +02003849 bus->sdcnt.tickcnt++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003850 } else
3851 break;
3852 }
3853 return 0;
3854}
3855
3856static void
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003857brcmf_sdio_watchdog(unsigned long data)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003858{
Franky Line92eedf2011-11-22 17:21:50 -08003859 struct brcmf_sdio *bus = (struct brcmf_sdio *)data;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003860
3861 if (bus->watchdog_tsk) {
3862 complete(&bus->watchdog_wait);
3863 /* Reschedule the watchdog */
3864 if (bus->wd_timer_valid)
3865 mod_timer(&bus->timer,
3866 jiffies + BRCMF_WD_POLL_MS * HZ / 1000);
3867 }
3868}
3869
Arend van Sprield9cb2592012-12-05 15:25:54 +01003870static struct brcmf_bus_ops brcmf_sdio_bus_ops = {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003871 .stop = brcmf_sdio_bus_stop,
3872 .preinit = brcmf_sdio_bus_preinit,
3873 .init = brcmf_sdio_bus_init,
3874 .txdata = brcmf_sdio_bus_txdata,
3875 .txctl = brcmf_sdio_bus_txctl,
3876 .rxctl = brcmf_sdio_bus_rxctl,
3877 .gettxq = brcmf_sdio_bus_gettxq,
Arend van Sprield9cb2592012-12-05 15:25:54 +01003878};
3879
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003880struct brcmf_sdio *brcmf_sdio_probe(struct brcmf_sdio_dev *sdiodev)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003881{
3882 int ret;
Franky Line92eedf2011-11-22 17:21:50 -08003883 struct brcmf_sdio *bus;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003884
Arend van Spriel5b435de2011-10-05 13:19:03 +02003885 brcmf_dbg(TRACE, "Enter\n");
3886
Arend van Spriel5b435de2011-10-05 13:19:03 +02003887 /* Allocate private bus interface state */
Franky Line92eedf2011-11-22 17:21:50 -08003888 bus = kzalloc(sizeof(struct brcmf_sdio), GFP_ATOMIC);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003889 if (!bus)
3890 goto fail;
3891
3892 bus->sdiodev = sdiodev;
3893 sdiodev->bus = bus;
Arend van Sprielb83db862011-10-19 12:51:09 +02003894 skb_queue_head_init(&bus->glom);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003895 bus->txbound = BRCMF_TXBOUND;
3896 bus->rxbound = BRCMF_RXBOUND;
3897 bus->txminmax = BRCMF_TXMINMAX;
Franky Lin6bc52312013-08-10 12:27:28 +02003898 bus->tx_seq = SDPCM_SEQ_WRAP - 1;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003899
Arend van Spriele217d1c2013-11-29 12:25:13 +01003900 /* platform specific configuration:
3901 * alignments must be at least 4 bytes for ADMA
3902 */
3903 bus->head_align = ALIGNMENT;
3904 bus->sgentry_align = ALIGNMENT;
3905 if (sdiodev->pdata) {
3906 if (sdiodev->pdata->sd_head_align > ALIGNMENT)
3907 bus->head_align = sdiodev->pdata->sd_head_align;
3908 if (sdiodev->pdata->sd_sgentry_align > ALIGNMENT)
3909 bus->sgentry_align = sdiodev->pdata->sd_sgentry_align;
3910 }
3911
Hante Meuleman37ac5782012-11-14 18:46:18 -08003912 INIT_WORK(&bus->datawork, brcmf_sdio_dataworker);
3913 bus->brcmf_wq = create_singlethread_workqueue("brcmf_wq");
3914 if (bus->brcmf_wq == NULL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003915 brcmf_err("insufficient memory to create txworkqueue\n");
Hante Meuleman37ac5782012-11-14 18:46:18 -08003916 goto fail;
3917 }
3918
Arend van Spriel5b435de2011-10-05 13:19:03 +02003919 /* attempt to attach to the dongle */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003920 if (!(brcmf_sdio_probe_attach(bus))) {
3921 brcmf_err("brcmf_sdio_probe_attach failed\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003922 goto fail;
3923 }
3924
Franky Lindd43a012012-11-05 16:22:22 -08003925 spin_lock_init(&bus->rxctl_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003926 spin_lock_init(&bus->txqlock);
3927 init_waitqueue_head(&bus->ctrl_wait);
3928 init_waitqueue_head(&bus->dcmd_resp_wait);
3929
3930 /* Set up the watchdog timer */
3931 init_timer(&bus->timer);
3932 bus->timer.data = (unsigned long)bus;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003933 bus->timer.function = brcmf_sdio_watchdog;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003934
Arend van Spriel5b435de2011-10-05 13:19:03 +02003935 /* Initialize watchdog thread */
3936 init_completion(&bus->watchdog_wait);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003937 bus->watchdog_tsk = kthread_run(brcmf_sdio_watchdog_thread,
Arend van Spriel5b435de2011-10-05 13:19:03 +02003938 bus, "brcmf_watchdog");
3939 if (IS_ERR(bus->watchdog_tsk)) {
Joe Perches02f77192012-01-15 00:38:44 -08003940 pr_warn("brcmf_watchdog thread failed to start\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003941 bus->watchdog_tsk = NULL;
3942 }
3943 /* Initialize DPC thread */
Arend van Sprielfccfe932013-06-18 13:29:26 +02003944 atomic_set(&bus->dpc_tskcnt, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003945
Franky Lina9ffda82011-12-16 18:37:12 -08003946 /* Assign bus interface call back */
Arend van Sprield9cb2592012-12-05 15:25:54 +01003947 bus->sdiodev->bus_if->dev = bus->sdiodev->dev;
3948 bus->sdiodev->bus_if->ops = &brcmf_sdio_bus_ops;
Arend van Spriel75d907d2013-02-06 18:40:45 +01003949 bus->sdiodev->bus_if->chip = bus->ci->chip;
3950 bus->sdiodev->bus_if->chiprev = bus->ci->chiprev;
Arend van Sprield9cb2592012-12-05 15:25:54 +01003951
Franky Lin706478c2013-08-10 12:27:29 +02003952 /* default sdio bus header length for tx packet */
3953 bus->tx_hdrlen = SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN;
3954
3955 /* Attach to the common layer, reserve hdr space */
Arend van Spriel8dee77b2013-11-29 11:48:14 +01003956 ret = brcmf_attach(bus->sdiodev->dev);
Franky Lin712ac5b2011-12-16 18:37:09 -08003957 if (ret != 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003958 brcmf_err("brcmf_attach failed\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003959 goto fail;
3960 }
3961
3962 /* Allocate buffers */
Arend van Sprielfad13222014-01-06 12:40:40 +01003963 if (bus->sdiodev->bus_if->maxctl) {
3964 bus->rxblen =
3965 roundup((bus->sdiodev->bus_if->maxctl + SDPCM_HDRLEN),
3966 ALIGNMENT) + bus->head_align;
3967 bus->rxbuf = kmalloc(bus->rxblen, GFP_ATOMIC);
3968 if (!(bus->rxbuf)) {
3969 brcmf_err("rxbuf allocation failed\n");
3970 goto fail;
3971 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02003972 }
3973
Arend van Sprielfad13222014-01-06 12:40:40 +01003974 sdio_claim_host(bus->sdiodev->func[1]);
3975
3976 /* Disable F2 to clear any intermediate frame state on the dongle */
3977 sdio_disable_func(bus->sdiodev->func[SDIO_FUNC_2]);
3978
Arend van Sprielfad13222014-01-06 12:40:40 +01003979 bus->rxflow = false;
3980
3981 /* Done with backplane-dependent accesses, can drop clock... */
3982 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, 0, NULL);
3983
3984 sdio_release_host(bus->sdiodev->func[1]);
3985
3986 /* ...and initialize clock/power states */
3987 bus->clkstate = CLK_SDONLY;
3988 bus->idletime = BRCMF_IDLE_INTERVAL;
3989 bus->idleclock = BRCMF_IDLE_ACTIVE;
3990
3991 /* Query the F2 block size, set roundup accordingly */
3992 bus->blocksize = bus->sdiodev->func[2]->cur_blksize;
3993 bus->roundup = min(max_roundup, bus->blocksize);
3994
3995 /* SR state */
3996 bus->sleeping = false;
3997 bus->sr_enabled = false;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003998
Arend van Spriel80969832012-06-09 22:51:44 +02003999 brcmf_sdio_debugfs_create(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004000 brcmf_dbg(INFO, "completed!!\n");
4001
4002 /* if firmware path present try to download and bring up bus */
Franky Lined683c92011-12-16 18:36:55 -08004003 ret = brcmf_bus_start(bus->sdiodev->dev);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004004 if (ret != 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01004005 brcmf_err("dongle is not responding\n");
Hante Meuleman1799ddf2012-11-14 18:46:17 -08004006 goto fail;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004007 }
Franky Lin15d45b62011-10-21 16:16:32 +02004008
Arend van Spriel5b435de2011-10-05 13:19:03 +02004009 return bus;
4010
4011fail:
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004012 brcmf_sdio_remove(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004013 return NULL;
4014}
4015
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004016/* Detach and free everything */
4017void brcmf_sdio_remove(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004018{
Arend van Spriel5b435de2011-10-05 13:19:03 +02004019 brcmf_dbg(TRACE, "Enter\n");
4020
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004021 if (bus) {
4022 /* De-register interrupt handler */
4023 brcmf_sdiod_intr_unregister(bus->sdiodev);
4024
4025 cancel_work_sync(&bus->datawork);
4026 if (bus->brcmf_wq)
4027 destroy_workqueue(bus->brcmf_wq);
4028
4029 if (bus->sdiodev->bus_if->drvr) {
4030 brcmf_detach(bus->sdiodev->dev);
Arend van Sprielbfad4a02014-01-06 12:40:39 +01004031 }
4032
4033 if (bus->ci) {
Arend van Sprielbb350712014-01-13 22:20:29 +01004034 if (bus->sdiodev->bus_if->state == BRCMF_BUS_DOWN) {
4035 sdio_claim_host(bus->sdiodev->func[1]);
4036 brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
4037 /* Leave the device in state where it is
4038 * 'quiet'. This is done by putting it in
4039 * download_state which essentially resets
4040 * all necessary cores.
4041 */
4042 msleep(20);
4043 brcmf_sdio_chip_enter_download(bus->sdiodev,
4044 bus->ci);
4045 brcmf_sdio_clkctl(bus, CLK_NONE, false);
4046 sdio_release_host(bus->sdiodev->func[1]);
4047 }
Arend van Sprielbfad4a02014-01-06 12:40:39 +01004048 brcmf_sdio_chip_detach(&bus->ci);
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004049 }
4050
Arend van Sprielbfad4a02014-01-06 12:40:39 +01004051 kfree(bus->rxbuf);
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004052 kfree(bus->hdrbuf);
4053 kfree(bus);
4054 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02004055
4056 brcmf_dbg(TRACE, "Disconnected\n");
4057}
4058
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01004059void brcmf_sdio_wd_timer(struct brcmf_sdio *bus, uint wdtick)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004060{
Arend van Spriel5b435de2011-10-05 13:19:03 +02004061 /* Totally stop the timer */
Joe Perches23677ce2012-02-09 11:17:23 +00004062 if (!wdtick && bus->wd_timer_valid) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02004063 del_timer_sync(&bus->timer);
4064 bus->wd_timer_valid = false;
4065 bus->save_ms = wdtick;
4066 return;
4067 }
4068
Franky Linece960e2011-10-21 16:16:19 +02004069 /* don't start the wd until fw is loaded */
Arend van Sprield6ae2c52014-01-06 12:40:44 +01004070 if (bus->sdiodev->bus_if->state != BRCMF_BUS_DATA)
Franky Linece960e2011-10-21 16:16:19 +02004071 return;
4072
Arend van Spriel5b435de2011-10-05 13:19:03 +02004073 if (wdtick) {
4074 if (bus->save_ms != BRCMF_WD_POLL_MS) {
Joe Perches23677ce2012-02-09 11:17:23 +00004075 if (bus->wd_timer_valid)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004076 /* Stop timer and restart at new value */
4077 del_timer_sync(&bus->timer);
4078
4079 /* Create timer again when watchdog period is
4080 dynamically changed or in the first instance
4081 */
4082 bus->timer.expires =
4083 jiffies + BRCMF_WD_POLL_MS * HZ / 1000;
4084 add_timer(&bus->timer);
4085
4086 } else {
4087 /* Re arm the timer, at last watchdog period */
4088 mod_timer(&bus->timer,
4089 jiffies + BRCMF_WD_POLL_MS * HZ / 1000);
4090 }
4091
4092 bus->wd_timer_valid = true;
4093 bus->save_ms = wdtick;
4094 }
4095}