blob: b9d5f735a71b917e17ac34ebd9fe844bfb32b422 [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 */
372 struct chip_info *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 */
460 struct sk_buff *txglom_sgpad; /* scatter-gather padding buffer */
Arend van Spriele217d1c2013-11-29 12:25:13 +0100461 u16 head_align; /* buffer pointer alignment */
462 u16 sgentry_align; /* scatter-gather buffer alignment */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200463};
464
Arend van Spriel5b435de2011-10-05 13:19:03 +0200465/* clkstate */
466#define CLK_NONE 0
467#define CLK_SDONLY 1
Piotr Haber4a3da992013-04-11 13:28:46 +0200468#define CLK_PENDING 2
Arend van Spriel5b435de2011-10-05 13:19:03 +0200469#define CLK_AVAIL 3
470
Joe Perches8ae74652012-01-15 00:38:38 -0800471#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200472static int qcount[NUMPRIO];
Joe Perches8ae74652012-01-15 00:38:38 -0800473#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200474
Hante Meuleman668761a2013-04-12 10:55:55 +0200475#define DEFAULT_SDIO_DRIVE_STRENGTH 6 /* in milliamps */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200476
477#define RETRYCHAN(chan) ((chan) == SDPCM_EVENT_CHANNEL)
478
479/* Retry count for register access failures */
480static const uint retry_limit = 2;
481
482/* Limit on rounding up frames */
483static const uint max_roundup = 512;
484
485#define ALIGNMENT 4
486
Franky Lin8da9d2c2013-11-29 12:25:10 +0100487static int brcmf_sdio_txglomsz = BRCMF_DEFAULT_TXGLOM_SIZE;
488module_param_named(txglomsz, brcmf_sdio_txglomsz, int, 0);
489MODULE_PARM_DESC(txglomsz, "maximum tx packet chain size [SDIO]");
490
Franky Lin9d7d6f92012-10-22 10:36:24 -0700491enum brcmf_sdio_frmtype {
492 BRCMF_SDIO_FT_NORMAL,
493 BRCMF_SDIO_FT_SUPER,
494 BRCMF_SDIO_FT_SUB,
495};
496
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200497#define BCM43143_FIRMWARE_NAME "brcm/brcmfmac43143-sdio.bin"
498#define BCM43143_NVRAM_NAME "brcm/brcmfmac43143-sdio.txt"
499#define BCM43241B0_FIRMWARE_NAME "brcm/brcmfmac43241b0-sdio.bin"
500#define BCM43241B0_NVRAM_NAME "brcm/brcmfmac43241b0-sdio.txt"
501#define BCM43241B4_FIRMWARE_NAME "brcm/brcmfmac43241b4-sdio.bin"
502#define BCM43241B4_NVRAM_NAME "brcm/brcmfmac43241b4-sdio.txt"
503#define BCM4329_FIRMWARE_NAME "brcm/brcmfmac4329-sdio.bin"
504#define BCM4329_NVRAM_NAME "brcm/brcmfmac4329-sdio.txt"
505#define BCM4330_FIRMWARE_NAME "brcm/brcmfmac4330-sdio.bin"
506#define BCM4330_NVRAM_NAME "brcm/brcmfmac4330-sdio.txt"
507#define BCM4334_FIRMWARE_NAME "brcm/brcmfmac4334-sdio.bin"
508#define BCM4334_NVRAM_NAME "brcm/brcmfmac4334-sdio.txt"
509#define BCM4335_FIRMWARE_NAME "brcm/brcmfmac4335-sdio.bin"
510#define BCM4335_NVRAM_NAME "brcm/brcmfmac4335-sdio.txt"
Arend van Spriel11e69c32014-01-08 10:49:33 +0100511#define BCM43362_FIRMWARE_NAME "brcm/brcmfmac43362-sdio.bin"
512#define BCM43362_NVRAM_NAME "brcm/brcmfmac43362-sdio.txt"
Franky Linbed89b62013-11-29 12:25:09 +0100513#define BCM4339_FIRMWARE_NAME "brcm/brcmfmac4339-sdio.bin"
514#define BCM4339_NVRAM_NAME "brcm/brcmfmac4339-sdio.txt"
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200515
516MODULE_FIRMWARE(BCM43143_FIRMWARE_NAME);
517MODULE_FIRMWARE(BCM43143_NVRAM_NAME);
518MODULE_FIRMWARE(BCM43241B0_FIRMWARE_NAME);
519MODULE_FIRMWARE(BCM43241B0_NVRAM_NAME);
520MODULE_FIRMWARE(BCM43241B4_FIRMWARE_NAME);
521MODULE_FIRMWARE(BCM43241B4_NVRAM_NAME);
522MODULE_FIRMWARE(BCM4329_FIRMWARE_NAME);
523MODULE_FIRMWARE(BCM4329_NVRAM_NAME);
524MODULE_FIRMWARE(BCM4330_FIRMWARE_NAME);
525MODULE_FIRMWARE(BCM4330_NVRAM_NAME);
526MODULE_FIRMWARE(BCM4334_FIRMWARE_NAME);
527MODULE_FIRMWARE(BCM4334_NVRAM_NAME);
528MODULE_FIRMWARE(BCM4335_FIRMWARE_NAME);
529MODULE_FIRMWARE(BCM4335_NVRAM_NAME);
Arend van Spriel11e69c32014-01-08 10:49:33 +0100530MODULE_FIRMWARE(BCM43362_FIRMWARE_NAME);
531MODULE_FIRMWARE(BCM43362_NVRAM_NAME);
Franky Linbed89b62013-11-29 12:25:09 +0100532MODULE_FIRMWARE(BCM4339_FIRMWARE_NAME);
533MODULE_FIRMWARE(BCM4339_NVRAM_NAME);
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200534
535struct brcmf_firmware_names {
536 u32 chipid;
537 u32 revmsk;
538 const char *bin;
539 const char *nv;
540};
541
542enum brcmf_firmware_type {
543 BRCMF_FIRMWARE_BIN,
544 BRCMF_FIRMWARE_NVRAM
545};
546
547#define BRCMF_FIRMWARE_NVRAM(name) \
548 name ## _FIRMWARE_NAME, name ## _NVRAM_NAME
549
550static const struct brcmf_firmware_names brcmf_fwname_data[] = {
551 { BCM43143_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM43143) },
552 { BCM43241_CHIP_ID, 0x0000001F, BRCMF_FIRMWARE_NVRAM(BCM43241B0) },
553 { BCM43241_CHIP_ID, 0xFFFFFFE0, BRCMF_FIRMWARE_NVRAM(BCM43241B4) },
554 { BCM4329_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM4329) },
555 { BCM4330_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM4330) },
556 { BCM4334_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM4334) },
Franky Linbed89b62013-11-29 12:25:09 +0100557 { BCM4335_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM4335) },
Arend van Spriel11e69c32014-01-08 10:49:33 +0100558 { BCM43362_CHIP_ID, 0xFFFFFFFE, BRCMF_FIRMWARE_NVRAM(BCM43362) },
Franky Linbed89b62013-11-29 12:25:09 +0100559 { BCM4339_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM4339) }
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200560};
561
562
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100563static const struct firmware *brcmf_sdio_get_fw(struct brcmf_sdio *bus,
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200564 enum brcmf_firmware_type type)
565{
566 const struct firmware *fw;
567 const char *name;
568 int err, i;
569
570 for (i = 0; i < ARRAY_SIZE(brcmf_fwname_data); i++) {
571 if (brcmf_fwname_data[i].chipid == bus->ci->chip &&
572 brcmf_fwname_data[i].revmsk & BIT(bus->ci->chiprev)) {
573 switch (type) {
574 case BRCMF_FIRMWARE_BIN:
575 name = brcmf_fwname_data[i].bin;
576 break;
577 case BRCMF_FIRMWARE_NVRAM:
578 name = brcmf_fwname_data[i].nv;
579 break;
580 default:
581 brcmf_err("invalid firmware type (%d)\n", type);
582 return NULL;
583 }
584 goto found;
585 }
586 }
587 brcmf_err("Unknown chipid %d [%d]\n",
588 bus->ci->chip, bus->ci->chiprev);
589 return NULL;
590
591found:
592 err = request_firmware(&fw, name, &bus->sdiodev->func[2]->dev);
593 if ((err) || (!fw)) {
594 brcmf_err("fail to request firmware %s (%d)\n", name, err);
595 return NULL;
596 }
597
598 return fw;
599}
600
Arend van Spriel5b435de2011-10-05 13:19:03 +0200601static void pkt_align(struct sk_buff *p, int len, int align)
602{
603 uint datalign;
604 datalign = (unsigned long)(p->data);
605 datalign = roundup(datalign, (align)) - datalign;
606 if (datalign)
607 skb_pull(p, datalign);
608 __skb_trim(p, len);
609}
610
611/* To check if there's window offered */
Franky Line92eedf2011-11-22 17:21:50 -0800612static bool data_ok(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200613{
614 return (u8)(bus->tx_max - bus->tx_seq) != 0 &&
615 ((u8)(bus->tx_max - bus->tx_seq) & 0x80) == 0;
616}
617
618/*
619 * Reads a register in the SDIO hardware block. This block occupies a series of
620 * adresses on the 32 bit backplane bus.
621 */
Franky Lin58692752012-05-04 18:27:36 -0700622static int
623r_sdreg32(struct brcmf_sdio *bus, u32 *regvar, u32 offset)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200624{
Franky Lin99ba15c2011-11-04 22:23:42 +0100625 u8 idx = brcmf_sdio_chip_getinfidx(bus->ci, BCMA_CORE_SDIO_DEV);
Franky Lin79ae3952012-05-04 18:27:34 -0700626 int ret;
Franky Lin58692752012-05-04 18:27:36 -0700627
Arend van Spriela39be272013-12-12 11:58:58 +0100628 *regvar = brcmf_sdiod_regrl(bus->sdiodev,
629 bus->ci->c_inf[idx].base + offset, &ret);
Franky Lin58692752012-05-04 18:27:36 -0700630
631 return ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200632}
633
Franky Lin58692752012-05-04 18:27:36 -0700634static int
635w_sdreg32(struct brcmf_sdio *bus, u32 regval, u32 reg_offset)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200636{
Franky Lin99ba15c2011-11-04 22:23:42 +0100637 u8 idx = brcmf_sdio_chip_getinfidx(bus->ci, BCMA_CORE_SDIO_DEV);
Franky Line13ce262012-05-04 18:27:35 -0700638 int ret;
Franky Lin58692752012-05-04 18:27:36 -0700639
Arend van Spriela39be272013-12-12 11:58:58 +0100640 brcmf_sdiod_regwl(bus->sdiodev,
641 bus->ci->c_inf[idx].base + reg_offset,
642 regval, &ret);
Franky Lin58692752012-05-04 18:27:36 -0700643
644 return ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200645}
646
Piotr Haber4a3da992013-04-11 13:28:46 +0200647static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100648brcmf_sdio_kso_control(struct brcmf_sdio *bus, bool on)
Piotr Haber4a3da992013-04-11 13:28:46 +0200649{
650 u8 wr_val = 0, rd_val, cmp_val, bmask;
651 int err = 0;
652 int try_cnt = 0;
653
654 brcmf_dbg(TRACE, "Enter\n");
655
656 wr_val = (on << SBSDIO_FUNC1_SLEEPCSR_KSO_SHIFT);
657 /* 1st KSO write goes to AOS wake up core if device is asleep */
Arend van Spriela39be272013-12-12 11:58:58 +0100658 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
659 wr_val, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +0200660 if (err) {
661 brcmf_err("SDIO_AOS KSO write error: %d\n", err);
662 return err;
663 }
664
665 if (on) {
666 /* device WAKEUP through KSO:
667 * write bit 0 & read back until
668 * both bits 0 (kso bit) & 1 (dev on status) are set
669 */
670 cmp_val = SBSDIO_FUNC1_SLEEPCSR_KSO_MASK |
671 SBSDIO_FUNC1_SLEEPCSR_DEVON_MASK;
672 bmask = cmp_val;
673 usleep_range(2000, 3000);
674 } else {
675 /* Put device to sleep, turn off KSO */
676 cmp_val = 0;
677 /* only check for bit0, bit1(dev on status) may not
678 * get cleared right away
679 */
680 bmask = SBSDIO_FUNC1_SLEEPCSR_KSO_MASK;
681 }
682
683 do {
684 /* reliable KSO bit set/clr:
685 * the sdiod sleep write access is synced to PMU 32khz clk
686 * just one write attempt may fail,
687 * read it back until it matches written value
688 */
Arend van Spriela39be272013-12-12 11:58:58 +0100689 rd_val = brcmf_sdiod_regrb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
690 &err);
Piotr Haber4a3da992013-04-11 13:28:46 +0200691 if (((rd_val & bmask) == cmp_val) && !err)
692 break;
693 brcmf_dbg(SDIO, "KSO wr/rd retry:%d (max: %d) ERR:%x\n",
694 try_cnt, MAX_KSO_ATTEMPTS, err);
695 udelay(KSO_WAIT_US);
Arend van Spriela39be272013-12-12 11:58:58 +0100696 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
697 wr_val, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +0200698 } while (try_cnt++ < MAX_KSO_ATTEMPTS);
699
700 return err;
701}
702
Arend van Spriel5b435de2011-10-05 13:19:03 +0200703#define PKT_AVAILABLE() (intstatus & I_HMB_FRAME_IND)
704
705#define HOSTINTMASK (I_HMB_SW_MASK | I_CHIPACTIVE)
706
Arend van Spriel5b435de2011-10-05 13:19:03 +0200707/* Turn backplane clock on or off */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100708static int brcmf_sdio_htclk(struct brcmf_sdio *bus, bool on, bool pendok)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200709{
710 int err;
711 u8 clkctl, clkreq, devctl;
712 unsigned long timeout;
713
Arend van Sprielc3203372013-04-03 12:40:44 +0200714 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200715
716 clkctl = 0;
717
Piotr Haber4a3da992013-04-11 13:28:46 +0200718 if (bus->sr_enabled) {
719 bus->clkstate = (on ? CLK_AVAIL : CLK_SDONLY);
720 return 0;
721 }
722
Arend van Spriel5b435de2011-10-05 13:19:03 +0200723 if (on) {
724 /* Request HT Avail */
725 clkreq =
726 bus->alp_only ? SBSDIO_ALP_AVAIL_REQ : SBSDIO_HT_AVAIL_REQ;
727
Arend van Spriela39be272013-12-12 11:58:58 +0100728 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
729 clkreq, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200730 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100731 brcmf_err("HT Avail request error: %d\n", err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200732 return -EBADE;
733 }
734
Arend van Spriel5b435de2011-10-05 13:19:03 +0200735 /* Check current status */
Arend van Spriela39be272013-12-12 11:58:58 +0100736 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
737 SBSDIO_FUNC1_CHIPCLKCSR, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200738 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100739 brcmf_err("HT Avail read error: %d\n", err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200740 return -EBADE;
741 }
742
743 /* Go to pending and await interrupt if appropriate */
744 if (!SBSDIO_CLKAV(clkctl, bus->alp_only) && pendok) {
745 /* Allow only clock-available interrupt */
Arend van Spriela39be272013-12-12 11:58:58 +0100746 devctl = brcmf_sdiod_regrb(bus->sdiodev,
747 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200748 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100749 brcmf_err("Devctl error setting CA: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200750 err);
751 return -EBADE;
752 }
753
754 devctl |= SBSDIO_DEVCTL_CA_INT_ONLY;
Arend van Spriela39be272013-12-12 11:58:58 +0100755 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
756 devctl, &err);
Arend van Sprielc3203372013-04-03 12:40:44 +0200757 brcmf_dbg(SDIO, "CLKCTL: set PENDING\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200758 bus->clkstate = CLK_PENDING;
759
760 return 0;
761 } else if (bus->clkstate == CLK_PENDING) {
762 /* Cancel CA-only interrupt filter */
Arend van Spriela39be272013-12-12 11:58:58 +0100763 devctl = brcmf_sdiod_regrb(bus->sdiodev,
764 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200765 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
Arend van Spriela39be272013-12-12 11:58:58 +0100766 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
767 devctl, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200768 }
769
770 /* Otherwise, wait here (polling) for HT Avail */
771 timeout = jiffies +
772 msecs_to_jiffies(PMU_MAX_TRANSITION_DLY/1000);
773 while (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
Arend van Spriela39be272013-12-12 11:58:58 +0100774 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
775 SBSDIO_FUNC1_CHIPCLKCSR,
776 &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200777 if (time_after(jiffies, timeout))
778 break;
779 else
780 usleep_range(5000, 10000);
781 }
782 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100783 brcmf_err("HT Avail request error: %d\n", err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200784 return -EBADE;
785 }
786 if (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100787 brcmf_err("HT Avail timeout (%d): clkctl 0x%02x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200788 PMU_MAX_TRANSITION_DLY, clkctl);
789 return -EBADE;
790 }
791
792 /* Mark clock available */
793 bus->clkstate = CLK_AVAIL;
Arend van Sprielc3203372013-04-03 12:40:44 +0200794 brcmf_dbg(SDIO, "CLKCTL: turned ON\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200795
Joe Perches8ae74652012-01-15 00:38:38 -0800796#if defined(DEBUG)
Joe Perches23677ce2012-02-09 11:17:23 +0000797 if (!bus->alp_only) {
Arend van Spriel5b435de2011-10-05 13:19:03 +0200798 if (SBSDIO_ALPONLY(clkctl))
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100799 brcmf_err("HT Clock should be on\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200800 }
Joe Perches8ae74652012-01-15 00:38:38 -0800801#endif /* defined (DEBUG) */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200802
803 bus->activity = true;
804 } else {
805 clkreq = 0;
806
807 if (bus->clkstate == CLK_PENDING) {
808 /* Cancel CA-only interrupt filter */
Arend van Spriela39be272013-12-12 11:58:58 +0100809 devctl = brcmf_sdiod_regrb(bus->sdiodev,
810 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200811 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
Arend van Spriela39be272013-12-12 11:58:58 +0100812 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
813 devctl, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200814 }
815
816 bus->clkstate = CLK_SDONLY;
Arend van Spriela39be272013-12-12 11:58:58 +0100817 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
818 clkreq, &err);
Arend van Sprielc3203372013-04-03 12:40:44 +0200819 brcmf_dbg(SDIO, "CLKCTL: turned OFF\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200820 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100821 brcmf_err("Failed access turning clock off: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200822 err);
823 return -EBADE;
824 }
825 }
826 return 0;
827}
828
829/* Change idle/active SD state */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100830static int brcmf_sdio_sdclk(struct brcmf_sdio *bus, bool on)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200831{
Arend van Sprielc3203372013-04-03 12:40:44 +0200832 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200833
834 if (on)
835 bus->clkstate = CLK_SDONLY;
836 else
837 bus->clkstate = CLK_NONE;
838
839 return 0;
840}
841
842/* Transition SD and backplane clock readiness */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100843static int brcmf_sdio_clkctl(struct brcmf_sdio *bus, uint target, bool pendok)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200844{
Joe Perches8ae74652012-01-15 00:38:38 -0800845#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200846 uint oldstate = bus->clkstate;
Joe Perches8ae74652012-01-15 00:38:38 -0800847#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200848
Arend van Sprielc3203372013-04-03 12:40:44 +0200849 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200850
851 /* Early exit if we're already there */
852 if (bus->clkstate == target) {
853 if (target == CLK_AVAIL) {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100854 brcmf_sdio_wd_timer(bus, BRCMF_WD_POLL_MS);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200855 bus->activity = true;
856 }
857 return 0;
858 }
859
860 switch (target) {
861 case CLK_AVAIL:
862 /* Make sure SD clock is available */
863 if (bus->clkstate == CLK_NONE)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100864 brcmf_sdio_sdclk(bus, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200865 /* Now request HT Avail on the backplane */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100866 brcmf_sdio_htclk(bus, true, pendok);
867 brcmf_sdio_wd_timer(bus, BRCMF_WD_POLL_MS);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200868 bus->activity = true;
869 break;
870
871 case CLK_SDONLY:
872 /* Remove HT request, or bring up SD clock */
873 if (bus->clkstate == CLK_NONE)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100874 brcmf_sdio_sdclk(bus, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200875 else if (bus->clkstate == CLK_AVAIL)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100876 brcmf_sdio_htclk(bus, false, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200877 else
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100878 brcmf_err("request for %d -> %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200879 bus->clkstate, target);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100880 brcmf_sdio_wd_timer(bus, BRCMF_WD_POLL_MS);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200881 break;
882
883 case CLK_NONE:
884 /* Make sure to remove HT request */
885 if (bus->clkstate == CLK_AVAIL)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100886 brcmf_sdio_htclk(bus, false, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200887 /* Now remove the SD clock */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100888 brcmf_sdio_sdclk(bus, false);
889 brcmf_sdio_wd_timer(bus, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200890 break;
891 }
Joe Perches8ae74652012-01-15 00:38:38 -0800892#ifdef DEBUG
Arend van Sprielc3203372013-04-03 12:40:44 +0200893 brcmf_dbg(SDIO, "%d -> %d\n", oldstate, bus->clkstate);
Joe Perches8ae74652012-01-15 00:38:38 -0800894#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200895
896 return 0;
897}
898
Piotr Haber4a3da992013-04-11 13:28:46 +0200899static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100900brcmf_sdio_bus_sleep(struct brcmf_sdio *bus, bool sleep, bool pendok)
Piotr Haber4a3da992013-04-11 13:28:46 +0200901{
902 int err = 0;
903 brcmf_dbg(TRACE, "Enter\n");
904 brcmf_dbg(SDIO, "request %s currently %s\n",
905 (sleep ? "SLEEP" : "WAKE"),
906 (bus->sleeping ? "SLEEP" : "WAKE"));
907
908 /* If SR is enabled control bus state with KSO */
909 if (bus->sr_enabled) {
910 /* Done if we're already in the requested state */
911 if (sleep == bus->sleeping)
912 goto end;
913
914 /* Going to sleep */
915 if (sleep) {
916 /* Don't sleep if something is pending */
917 if (atomic_read(&bus->intstatus) ||
918 atomic_read(&bus->ipend) > 0 ||
919 (!atomic_read(&bus->fcstate) &&
920 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) &&
921 data_ok(bus)))
922 return -EBUSY;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100923 err = brcmf_sdio_kso_control(bus, false);
Piotr Haber4a3da992013-04-11 13:28:46 +0200924 /* disable watchdog */
925 if (!err)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100926 brcmf_sdio_wd_timer(bus, 0);
Piotr Haber4a3da992013-04-11 13:28:46 +0200927 } else {
928 bus->idlecount = 0;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100929 err = brcmf_sdio_kso_control(bus, true);
Piotr Haber4a3da992013-04-11 13:28:46 +0200930 }
931 if (!err) {
932 /* Change state */
933 bus->sleeping = sleep;
934 brcmf_dbg(SDIO, "new state %s\n",
935 (sleep ? "SLEEP" : "WAKE"));
936 } else {
937 brcmf_err("error while changing bus sleep state %d\n",
938 err);
939 return err;
940 }
941 }
942
943end:
944 /* control clocks */
945 if (sleep) {
946 if (!bus->sr_enabled)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100947 brcmf_sdio_clkctl(bus, CLK_NONE, pendok);
Piotr Haber4a3da992013-04-11 13:28:46 +0200948 } else {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100949 brcmf_sdio_clkctl(bus, CLK_AVAIL, pendok);
Piotr Haber4a3da992013-04-11 13:28:46 +0200950 }
951
952 return err;
953
954}
955
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100956static u32 brcmf_sdio_hostmail(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200957{
958 u32 intstatus = 0;
959 u32 hmb_data;
960 u8 fcbits;
Franky Lin58692752012-05-04 18:27:36 -0700961 int ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200962
Arend van Sprielc3203372013-04-03 12:40:44 +0200963 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200964
965 /* Read mailbox data and ack that we did so */
Franky Lin58692752012-05-04 18:27:36 -0700966 ret = r_sdreg32(bus, &hmb_data,
967 offsetof(struct sdpcmd_regs, tohostmailboxdata));
Arend van Spriel5b435de2011-10-05 13:19:03 +0200968
Franky Lin58692752012-05-04 18:27:36 -0700969 if (ret == 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200970 w_sdreg32(bus, SMB_INT_ACK,
Franky Lin58692752012-05-04 18:27:36 -0700971 offsetof(struct sdpcmd_regs, tosbmailbox));
Arend van Spriel80969832012-06-09 22:51:44 +0200972 bus->sdcnt.f1regdata += 2;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200973
974 /* Dongle recomposed rx frames, accept them again */
975 if (hmb_data & HMB_DATA_NAKHANDLED) {
Arend van Sprielc3203372013-04-03 12:40:44 +0200976 brcmf_dbg(SDIO, "Dongle reports NAK handled, expect rtx of %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200977 bus->rx_seq);
978 if (!bus->rxskip)
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100979 brcmf_err("unexpected NAKHANDLED!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200980
981 bus->rxskip = false;
982 intstatus |= I_HMB_FRAME_IND;
983 }
984
985 /*
986 * DEVREADY does not occur with gSPI.
987 */
988 if (hmb_data & (HMB_DATA_DEVREADY | HMB_DATA_FWREADY)) {
989 bus->sdpcm_ver =
990 (hmb_data & HMB_DATA_VERSION_MASK) >>
991 HMB_DATA_VERSION_SHIFT;
992 if (bus->sdpcm_ver != SDPCM_PROT_VERSION)
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100993 brcmf_err("Version mismatch, dongle reports %d, "
Arend van Spriel5b435de2011-10-05 13:19:03 +0200994 "expecting %d\n",
995 bus->sdpcm_ver, SDPCM_PROT_VERSION);
996 else
Arend van Sprielc3203372013-04-03 12:40:44 +0200997 brcmf_dbg(SDIO, "Dongle ready, protocol version %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200998 bus->sdpcm_ver);
999 }
1000
1001 /*
1002 * Flow Control has been moved into the RX headers and this out of band
1003 * method isn't used any more.
1004 * remaining backward compatible with older dongles.
1005 */
1006 if (hmb_data & HMB_DATA_FC) {
1007 fcbits = (hmb_data & HMB_DATA_FCDATA_MASK) >>
1008 HMB_DATA_FCDATA_SHIFT;
1009
1010 if (fcbits & ~bus->flowcontrol)
Arend van Spriel80969832012-06-09 22:51:44 +02001011 bus->sdcnt.fc_xoff++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001012
1013 if (bus->flowcontrol & ~fcbits)
Arend van Spriel80969832012-06-09 22:51:44 +02001014 bus->sdcnt.fc_xon++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001015
Arend van Spriel80969832012-06-09 22:51:44 +02001016 bus->sdcnt.fc_rcvd++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001017 bus->flowcontrol = fcbits;
1018 }
1019
1020 /* Shouldn't be any others */
1021 if (hmb_data & ~(HMB_DATA_DEVREADY |
1022 HMB_DATA_NAKHANDLED |
1023 HMB_DATA_FC |
1024 HMB_DATA_FWREADY |
1025 HMB_DATA_FCDATA_MASK | HMB_DATA_VERSION_MASK))
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001026 brcmf_err("Unknown mailbox data content: 0x%02x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001027 hmb_data);
1028
1029 return intstatus;
1030}
1031
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001032static void brcmf_sdio_rxfail(struct brcmf_sdio *bus, bool abort, bool rtx)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001033{
1034 uint retries = 0;
1035 u16 lastrbc;
1036 u8 hi, lo;
1037 int err;
1038
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001039 brcmf_err("%sterminate frame%s\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001040 abort ? "abort command, " : "",
1041 rtx ? ", send NAK" : "");
1042
1043 if (abort)
Arend van Spriela39be272013-12-12 11:58:58 +01001044 brcmf_sdiod_abort(bus->sdiodev, SDIO_FUNC_2);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001045
Arend van Spriela39be272013-12-12 11:58:58 +01001046 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL,
1047 SFC_RF_TERM, &err);
Arend van Spriel80969832012-06-09 22:51:44 +02001048 bus->sdcnt.f1regdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001049
1050 /* Wait until the packet has been flushed (device/FIFO stable) */
1051 for (lastrbc = retries = 0xffff; retries > 0; retries--) {
Arend van Spriela39be272013-12-12 11:58:58 +01001052 hi = brcmf_sdiod_regrb(bus->sdiodev,
1053 SBSDIO_FUNC1_RFRAMEBCHI, &err);
1054 lo = brcmf_sdiod_regrb(bus->sdiodev,
1055 SBSDIO_FUNC1_RFRAMEBCLO, &err);
Arend van Spriel80969832012-06-09 22:51:44 +02001056 bus->sdcnt.f1regdata += 2;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001057
1058 if ((hi == 0) && (lo == 0))
1059 break;
1060
1061 if ((hi > (lastrbc >> 8)) && (lo > (lastrbc & 0x00ff))) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001062 brcmf_err("count growing: last 0x%04x now 0x%04x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001063 lastrbc, (hi << 8) + lo);
1064 }
1065 lastrbc = (hi << 8) + lo;
1066 }
1067
1068 if (!retries)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001069 brcmf_err("count never zeroed: last 0x%04x\n", lastrbc);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001070 else
Arend van Sprielc3203372013-04-03 12:40:44 +02001071 brcmf_dbg(SDIO, "flush took %d iterations\n", 0xffff - retries);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001072
1073 if (rtx) {
Arend van Spriel80969832012-06-09 22:51:44 +02001074 bus->sdcnt.rxrtx++;
Franky Lin58692752012-05-04 18:27:36 -07001075 err = w_sdreg32(bus, SMB_NAK,
1076 offsetof(struct sdpcmd_regs, tosbmailbox));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001077
Arend van Spriel80969832012-06-09 22:51:44 +02001078 bus->sdcnt.f1regdata++;
Franky Lin58692752012-05-04 18:27:36 -07001079 if (err == 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001080 bus->rxskip = true;
1081 }
1082
1083 /* Clear partial in any case */
Franky Lin4754fce2012-09-19 22:21:11 +02001084 bus->cur_read.len = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001085
1086 /* If we can't reach the device, signal failure */
Franky Lin5c15c232012-05-04 18:27:37 -07001087 if (err)
Franky Lin712ac5b2011-12-16 18:37:09 -08001088 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001089}
1090
Arend van Spriel9a95e602011-11-10 20:30:29 +01001091/* return total length of buffer chain */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001092static uint brcmf_sdio_glom_len(struct brcmf_sdio *bus)
Arend van Spriel9a95e602011-11-10 20:30:29 +01001093{
1094 struct sk_buff *p;
1095 uint total;
1096
1097 total = 0;
1098 skb_queue_walk(&bus->glom, p)
1099 total += p->len;
1100 return total;
1101}
1102
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001103static void brcmf_sdio_free_glom(struct brcmf_sdio *bus)
Arend van Spriel046808d2011-11-10 20:30:31 +01001104{
1105 struct sk_buff *cur, *next;
1106
1107 skb_queue_walk_safe(&bus->glom, cur, next) {
1108 skb_unlink(cur, &bus->glom);
1109 brcmu_pkt_buf_free_skb(cur);
1110 }
1111}
1112
Franky Lin6bc52312013-08-10 12:27:28 +02001113/**
1114 * brcmfmac sdio bus specific header
1115 * This is the lowest layer header wrapped on the packets transmitted between
1116 * host and WiFi dongle which contains information needed for SDIO core and
1117 * firmware
1118 *
Franky Lin8da9d2c2013-11-29 12:25:10 +01001119 * It consists of 3 parts: hardware header, hardware extension header and
1120 * software header
Franky Lin6bc52312013-08-10 12:27:28 +02001121 * hardware header (frame tag) - 4 bytes
1122 * Byte 0~1: Frame length
1123 * Byte 2~3: Checksum, bit-wise inverse of frame length
Franky Lin8da9d2c2013-11-29 12:25:10 +01001124 * hardware extension header - 8 bytes
1125 * Tx glom mode only, N/A for Rx or normal Tx
1126 * Byte 0~1: Packet length excluding hw frame tag
1127 * Byte 2: Reserved
1128 * Byte 3: Frame flags, bit 0: last frame indication
1129 * Byte 4~5: Reserved
1130 * Byte 6~7: Tail padding length
Franky Lin6bc52312013-08-10 12:27:28 +02001131 * software header - 8 bytes
1132 * Byte 0: Rx/Tx sequence number
1133 * Byte 1: 4 MSB Channel number, 4 LSB arbitrary flag
1134 * Byte 2: Length of next data frame, reserved for Tx
1135 * Byte 3: Data offset
1136 * Byte 4: Flow control bits, reserved for Tx
1137 * Byte 5: Maximum Sequence number allowed by firmware for Tx, N/A for Tx packet
1138 * Byte 6~7: Reserved
1139 */
1140#define SDPCM_HWHDR_LEN 4
Franky Lin8da9d2c2013-11-29 12:25:10 +01001141#define SDPCM_HWEXT_LEN 8
Franky Lin6bc52312013-08-10 12:27:28 +02001142#define SDPCM_SWHDR_LEN 8
1143#define SDPCM_HDRLEN (SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN)
Franky Lin6bc52312013-08-10 12:27:28 +02001144/* software header */
1145#define SDPCM_SEQ_MASK 0x000000ff
1146#define SDPCM_SEQ_WRAP 256
1147#define SDPCM_CHANNEL_MASK 0x00000f00
1148#define SDPCM_CHANNEL_SHIFT 8
1149#define SDPCM_CONTROL_CHANNEL 0 /* Control */
1150#define SDPCM_EVENT_CHANNEL 1 /* Asyc Event Indication */
1151#define SDPCM_DATA_CHANNEL 2 /* Data Xmit/Recv */
1152#define SDPCM_GLOM_CHANNEL 3 /* Coalesced packets */
1153#define SDPCM_TEST_CHANNEL 15 /* Test/debug packets */
1154#define SDPCM_GLOMDESC(p) (((u8 *)p)[1] & 0x80)
1155#define SDPCM_NEXTLEN_MASK 0x00ff0000
1156#define SDPCM_NEXTLEN_SHIFT 16
1157#define SDPCM_DOFFSET_MASK 0xff000000
1158#define SDPCM_DOFFSET_SHIFT 24
1159#define SDPCM_FCMASK_MASK 0x000000ff
1160#define SDPCM_WINDOW_MASK 0x0000ff00
1161#define SDPCM_WINDOW_SHIFT 8
1162
1163static inline u8 brcmf_sdio_getdatoffset(u8 *swheader)
1164{
1165 u32 hdrvalue;
1166 hdrvalue = *(u32 *)swheader;
1167 return (u8)((hdrvalue & SDPCM_DOFFSET_MASK) >> SDPCM_DOFFSET_SHIFT);
1168}
1169
1170static int brcmf_sdio_hdparse(struct brcmf_sdio *bus, u8 *header,
1171 struct brcmf_sdio_hdrinfo *rd,
1172 enum brcmf_sdio_frmtype type)
Franky Lin4754fce2012-09-19 22:21:11 +02001173{
1174 u16 len, checksum;
1175 u8 rx_seq, fc, tx_seq_max;
Franky Lin6bc52312013-08-10 12:27:28 +02001176 u32 swheader;
Franky Lin4754fce2012-09-19 22:21:11 +02001177
Arend van Spriel4b776962013-11-29 12:25:06 +01001178 trace_brcmf_sdpcm_hdr(SDPCM_RX, header);
Arend van Spriel76584ec2013-10-15 15:44:47 +02001179
Franky Lin6bc52312013-08-10 12:27:28 +02001180 /* hw header */
Franky Lin4754fce2012-09-19 22:21:11 +02001181 len = get_unaligned_le16(header);
1182 checksum = get_unaligned_le16(header + sizeof(u16));
1183 /* All zero means no more to read */
1184 if (!(len | checksum)) {
1185 bus->rxpending = false;
Franky Lin10510582012-11-05 16:22:26 -08001186 return -ENODATA;
Franky Lin4754fce2012-09-19 22:21:11 +02001187 }
1188 if ((u16)(~(len ^ checksum))) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001189 brcmf_err("HW header checksum error\n");
Franky Lin4754fce2012-09-19 22:21:11 +02001190 bus->sdcnt.rx_badhdr++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001191 brcmf_sdio_rxfail(bus, false, false);
Franky Lin10510582012-11-05 16:22:26 -08001192 return -EIO;
Franky Lin4754fce2012-09-19 22:21:11 +02001193 }
1194 if (len < SDPCM_HDRLEN) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001195 brcmf_err("HW header length error\n");
Franky Lin10510582012-11-05 16:22:26 -08001196 return -EPROTO;
Franky Lin4754fce2012-09-19 22:21:11 +02001197 }
Franky Lin9d7d6f92012-10-22 10:36:24 -07001198 if (type == BRCMF_SDIO_FT_SUPER &&
1199 (roundup(len, bus->blocksize) != rd->len)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001200 brcmf_err("HW superframe header length error\n");
Franky Lin10510582012-11-05 16:22:26 -08001201 return -EPROTO;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001202 }
1203 if (type == BRCMF_SDIO_FT_SUB && len > rd->len) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001204 brcmf_err("HW subframe header length error\n");
Franky Lin10510582012-11-05 16:22:26 -08001205 return -EPROTO;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001206 }
Franky Lin4754fce2012-09-19 22:21:11 +02001207 rd->len = len;
1208
Franky Lin6bc52312013-08-10 12:27:28 +02001209 /* software header */
1210 header += SDPCM_HWHDR_LEN;
1211 swheader = le32_to_cpu(*(__le32 *)header);
1212 if (type == BRCMF_SDIO_FT_SUPER && SDPCM_GLOMDESC(header)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001213 brcmf_err("Glom descriptor found in superframe head\n");
Franky Lin9d7d6f92012-10-22 10:36:24 -07001214 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001215 return -EINVAL;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001216 }
Franky Lin6bc52312013-08-10 12:27:28 +02001217 rx_seq = (u8)(swheader & SDPCM_SEQ_MASK);
1218 rd->channel = (swheader & SDPCM_CHANNEL_MASK) >> SDPCM_CHANNEL_SHIFT;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001219 if (len > MAX_RX_DATASZ && rd->channel != SDPCM_CONTROL_CHANNEL &&
1220 type != BRCMF_SDIO_FT_SUPER) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001221 brcmf_err("HW header length too long\n");
Franky Lin4754fce2012-09-19 22:21:11 +02001222 bus->sdcnt.rx_toolong++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001223 brcmf_sdio_rxfail(bus, false, false);
Franky Lin4754fce2012-09-19 22:21:11 +02001224 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001225 return -EPROTO;
Franky Lin4754fce2012-09-19 22:21:11 +02001226 }
Franky Lin9d7d6f92012-10-22 10:36:24 -07001227 if (type == BRCMF_SDIO_FT_SUPER && rd->channel != SDPCM_GLOM_CHANNEL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001228 brcmf_err("Wrong channel for superframe\n");
Franky Lin9d7d6f92012-10-22 10:36:24 -07001229 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001230 return -EINVAL;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001231 }
1232 if (type == BRCMF_SDIO_FT_SUB && rd->channel != SDPCM_DATA_CHANNEL &&
1233 rd->channel != SDPCM_EVENT_CHANNEL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001234 brcmf_err("Wrong channel for subframe\n");
Franky Lin9d7d6f92012-10-22 10:36:24 -07001235 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001236 return -EINVAL;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001237 }
Franky Lin6bc52312013-08-10 12:27:28 +02001238 rd->dat_offset = brcmf_sdio_getdatoffset(header);
Franky Lin4754fce2012-09-19 22:21:11 +02001239 if (rd->dat_offset < SDPCM_HDRLEN || rd->dat_offset > rd->len) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001240 brcmf_err("seq %d: bad data offset\n", rx_seq);
Franky Lin4754fce2012-09-19 22:21:11 +02001241 bus->sdcnt.rx_badhdr++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001242 brcmf_sdio_rxfail(bus, false, false);
Franky Lin4754fce2012-09-19 22:21:11 +02001243 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001244 return -ENXIO;
Franky Lin4754fce2012-09-19 22:21:11 +02001245 }
1246 if (rd->seq_num != rx_seq) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001247 brcmf_err("seq %d: sequence number error, expect %d\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001248 rx_seq, rd->seq_num);
1249 bus->sdcnt.rx_badseq++;
1250 rd->seq_num = rx_seq;
1251 }
Franky Lin9d7d6f92012-10-22 10:36:24 -07001252 /* no need to check the reset for subframe */
1253 if (type == BRCMF_SDIO_FT_SUB)
Franky Lin10510582012-11-05 16:22:26 -08001254 return 0;
Franky Lin6bc52312013-08-10 12:27:28 +02001255 rd->len_nxtfrm = (swheader & SDPCM_NEXTLEN_MASK) >> SDPCM_NEXTLEN_SHIFT;
Franky Lin4754fce2012-09-19 22:21:11 +02001256 if (rd->len_nxtfrm << 4 > MAX_RX_DATASZ) {
1257 /* only warm for NON glom packet */
1258 if (rd->channel != SDPCM_GLOM_CHANNEL)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001259 brcmf_err("seq %d: next length error\n", rx_seq);
Franky Lin4754fce2012-09-19 22:21:11 +02001260 rd->len_nxtfrm = 0;
1261 }
Franky Lin6bc52312013-08-10 12:27:28 +02001262 swheader = le32_to_cpu(*(__le32 *)(header + 4));
1263 fc = swheader & SDPCM_FCMASK_MASK;
Franky Lin4754fce2012-09-19 22:21:11 +02001264 if (bus->flowcontrol != fc) {
1265 if (~bus->flowcontrol & fc)
1266 bus->sdcnt.fc_xoff++;
1267 if (bus->flowcontrol & ~fc)
1268 bus->sdcnt.fc_xon++;
1269 bus->sdcnt.fc_rcvd++;
1270 bus->flowcontrol = fc;
1271 }
Franky Lin6bc52312013-08-10 12:27:28 +02001272 tx_seq_max = (swheader & SDPCM_WINDOW_MASK) >> SDPCM_WINDOW_SHIFT;
Franky Lin4754fce2012-09-19 22:21:11 +02001273 if ((u8)(tx_seq_max - bus->tx_seq) > 0x40) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001274 brcmf_err("seq %d: max tx seq number error\n", rx_seq);
Franky Lin4754fce2012-09-19 22:21:11 +02001275 tx_seq_max = bus->tx_seq + 2;
1276 }
1277 bus->tx_max = tx_seq_max;
1278
Franky Lin10510582012-11-05 16:22:26 -08001279 return 0;
Franky Lin4754fce2012-09-19 22:21:11 +02001280}
1281
Franky Lin6bc52312013-08-10 12:27:28 +02001282static inline void brcmf_sdio_update_hwhdr(u8 *header, u16 frm_length)
1283{
1284 *(__le16 *)header = cpu_to_le16(frm_length);
1285 *(((__le16 *)header) + 1) = cpu_to_le16(~frm_length);
1286}
1287
1288static void brcmf_sdio_hdpack(struct brcmf_sdio *bus, u8 *header,
1289 struct brcmf_sdio_hdrinfo *hd_info)
1290{
Franky Lin8da9d2c2013-11-29 12:25:10 +01001291 u32 hdrval;
1292 u8 hdr_offset;
Franky Lin6bc52312013-08-10 12:27:28 +02001293
1294 brcmf_sdio_update_hwhdr(header, hd_info->len);
Franky Lin8da9d2c2013-11-29 12:25:10 +01001295 hdr_offset = SDPCM_HWHDR_LEN;
Franky Lin6bc52312013-08-10 12:27:28 +02001296
Franky Lin8da9d2c2013-11-29 12:25:10 +01001297 if (bus->txglom) {
1298 hdrval = (hd_info->len - hdr_offset) | (hd_info->lastfrm << 24);
1299 *((__le32 *)(header + hdr_offset)) = cpu_to_le32(hdrval);
1300 hdrval = (u16)hd_info->tail_pad << 16;
1301 *(((__le32 *)(header + hdr_offset)) + 1) = cpu_to_le32(hdrval);
1302 hdr_offset += SDPCM_HWEXT_LEN;
1303 }
1304
1305 hdrval = hd_info->seq_num;
1306 hdrval |= (hd_info->channel << SDPCM_CHANNEL_SHIFT) &
1307 SDPCM_CHANNEL_MASK;
1308 hdrval |= (hd_info->dat_offset << SDPCM_DOFFSET_SHIFT) &
1309 SDPCM_DOFFSET_MASK;
1310 *((__le32 *)(header + hdr_offset)) = cpu_to_le32(hdrval);
1311 *(((__le32 *)(header + hdr_offset)) + 1) = 0;
1312 trace_brcmf_sdpcm_hdr(SDPCM_TX + !!(bus->txglom), header);
Franky Lin6bc52312013-08-10 12:27:28 +02001313}
1314
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001315static u8 brcmf_sdio_rxglom(struct brcmf_sdio *bus, u8 rxseq)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001316{
1317 u16 dlen, totlen;
1318 u8 *dptr, num = 0;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001319 u16 sublen;
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001320 struct sk_buff *pfirst, *pnext;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001321
1322 int errcode;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001323 u8 doff, sfdoff;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001324
Franky Lin6bc52312013-08-10 12:27:28 +02001325 struct brcmf_sdio_hdrinfo rd_new;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001326
1327 /* If packets, issue read(s) and send up packet chain */
1328 /* Return sequence numbers consumed? */
1329
Arend van Sprielc3203372013-04-03 12:40:44 +02001330 brcmf_dbg(SDIO, "start: glomd %p glom %p\n",
Arend van Sprielb83db862011-10-19 12:51:09 +02001331 bus->glomd, skb_peek(&bus->glom));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001332
1333 /* If there's a descriptor, generate the packet chain */
1334 if (bus->glomd) {
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001335 pfirst = pnext = NULL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001336 dlen = (u16) (bus->glomd->len);
1337 dptr = bus->glomd->data;
1338 if (!dlen || (dlen & 1)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001339 brcmf_err("bad glomd len(%d), ignore descriptor\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001340 dlen);
1341 dlen = 0;
1342 }
1343
1344 for (totlen = num = 0; dlen; num++) {
1345 /* Get (and move past) next length */
1346 sublen = get_unaligned_le16(dptr);
1347 dlen -= sizeof(u16);
1348 dptr += sizeof(u16);
1349 if ((sublen < SDPCM_HDRLEN) ||
1350 ((num == 0) && (sublen < (2 * SDPCM_HDRLEN)))) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001351 brcmf_err("descriptor len %d bad: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001352 num, sublen);
1353 pnext = NULL;
1354 break;
1355 }
Arend van Spriele217d1c2013-11-29 12:25:13 +01001356 if (sublen % bus->sgentry_align) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001357 brcmf_err("sublen %d not multiple of %d\n",
Arend van Spriele217d1c2013-11-29 12:25:13 +01001358 sublen, bus->sgentry_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001359 }
1360 totlen += sublen;
1361
1362 /* For last frame, adjust read len so total
1363 is a block multiple */
1364 if (!dlen) {
1365 sublen +=
1366 (roundup(totlen, bus->blocksize) - totlen);
1367 totlen = roundup(totlen, bus->blocksize);
1368 }
1369
1370 /* Allocate/chain packet for next subframe */
Arend van Spriele217d1c2013-11-29 12:25:13 +01001371 pnext = brcmu_pkt_buf_get_skb(sublen + bus->sgentry_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001372 if (pnext == NULL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001373 brcmf_err("bcm_pkt_buf_get_skb failed, num %d len %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001374 num, sublen);
1375 break;
1376 }
Arend van Sprielb83db862011-10-19 12:51:09 +02001377 skb_queue_tail(&bus->glom, pnext);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001378
1379 /* Adhere to start alignment requirements */
Arend van Spriele217d1c2013-11-29 12:25:13 +01001380 pkt_align(pnext, sublen, bus->sgentry_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001381 }
1382
1383 /* If all allocations succeeded, save packet chain
1384 in bus structure */
1385 if (pnext) {
1386 brcmf_dbg(GLOM, "allocated %d-byte packet chain for %d subframes\n",
1387 totlen, num);
Franky Lin4754fce2012-09-19 22:21:11 +02001388 if (BRCMF_GLOM_ON() && bus->cur_read.len &&
1389 totlen != bus->cur_read.len) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001390 brcmf_dbg(GLOM, "glomdesc mismatch: nextlen %d glomdesc %d rxseq %d\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001391 bus->cur_read.len, totlen, rxseq);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001392 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02001393 pfirst = pnext = NULL;
1394 } else {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001395 brcmf_sdio_free_glom(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001396 num = 0;
1397 }
1398
1399 /* Done with descriptor packet */
1400 brcmu_pkt_buf_free_skb(bus->glomd);
1401 bus->glomd = NULL;
Franky Lin4754fce2012-09-19 22:21:11 +02001402 bus->cur_read.len = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001403 }
1404
1405 /* Ok -- either we just generated a packet chain,
1406 or had one from before */
Arend van Sprielb83db862011-10-19 12:51:09 +02001407 if (!skb_queue_empty(&bus->glom)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001408 if (BRCMF_GLOM_ON()) {
1409 brcmf_dbg(GLOM, "try superframe read, packet chain:\n");
Arend van Sprielb83db862011-10-19 12:51:09 +02001410 skb_queue_walk(&bus->glom, pnext) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001411 brcmf_dbg(GLOM, " %p: %p len 0x%04x (%d)\n",
1412 pnext, (u8 *) (pnext->data),
1413 pnext->len, pnext->len);
1414 }
1415 }
1416
Arend van Sprielb83db862011-10-19 12:51:09 +02001417 pfirst = skb_peek(&bus->glom);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001418 dlen = (u16) brcmf_sdio_glom_len(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001419
1420 /* Do an SDIO read for the superframe. Configurable iovar to
1421 * read directly into the chained packet, or allocate a large
1422 * packet and and copy into the chain.
1423 */
Franky Lin38b0b0d2012-11-05 16:22:24 -08001424 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriela39be272013-12-12 11:58:58 +01001425 errcode = brcmf_sdiod_recv_chain(bus->sdiodev,
Arend van Spriela39be272013-12-12 11:58:58 +01001426 &bus->glom, dlen);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001427 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel80969832012-06-09 22:51:44 +02001428 bus->sdcnt.f2rxdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001429
1430 /* On failure, kill the superframe, allow a couple retries */
1431 if (errcode < 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001432 brcmf_err("glom read of %d bytes failed: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001433 dlen, errcode);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001434
Franky Lin38b0b0d2012-11-05 16:22:24 -08001435 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001436 if (bus->glomerr++ < 3) {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001437 brcmf_sdio_rxfail(bus, true, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001438 } else {
1439 bus->glomerr = 0;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001440 brcmf_sdio_rxfail(bus, true, false);
Arend van Spriel80969832012-06-09 22:51:44 +02001441 bus->sdcnt.rxglomfail++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001442 brcmf_sdio_free_glom(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001443 }
Franky Lin38b0b0d2012-11-05 16:22:24 -08001444 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001445 return 0;
1446 }
Joe Perches1e023822012-01-15 00:38:40 -08001447
1448 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
1449 pfirst->data, min_t(int, pfirst->len, 48),
1450 "SUPERFRAME:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001451
Franky Lin9d7d6f92012-10-22 10:36:24 -07001452 rd_new.seq_num = rxseq;
1453 rd_new.len = dlen;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001454 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin6bc52312013-08-10 12:27:28 +02001455 errcode = brcmf_sdio_hdparse(bus, pfirst->data, &rd_new,
1456 BRCMF_SDIO_FT_SUPER);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001457 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin9d7d6f92012-10-22 10:36:24 -07001458 bus->cur_read.len = rd_new.len_nxtfrm << 4;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001459
1460 /* Remove superframe header, remember offset */
Franky Lin9d7d6f92012-10-22 10:36:24 -07001461 skb_pull(pfirst, rd_new.dat_offset);
1462 sfdoff = rd_new.dat_offset;
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001463 num = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001464
1465 /* Validate all the subframe headers */
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001466 skb_queue_walk(&bus->glom, pnext) {
1467 /* leave when invalid subframe is found */
1468 if (errcode)
1469 break;
1470
Franky Lin9d7d6f92012-10-22 10:36:24 -07001471 rd_new.len = pnext->len;
1472 rd_new.seq_num = rxseq++;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001473 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin6bc52312013-08-10 12:27:28 +02001474 errcode = brcmf_sdio_hdparse(bus, pnext->data, &rd_new,
1475 BRCMF_SDIO_FT_SUB);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001476 sdio_release_host(bus->sdiodev->func[1]);
Joe Perches1e023822012-01-15 00:38:40 -08001477 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
Franky Lin9d7d6f92012-10-22 10:36:24 -07001478 pnext->data, 32, "subframe:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001479
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001480 num++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001481 }
1482
1483 if (errcode) {
1484 /* Terminate frame on error, request
1485 a couple retries */
Franky Lin38b0b0d2012-11-05 16:22:24 -08001486 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001487 if (bus->glomerr++ < 3) {
1488 /* Restore superframe header space */
1489 skb_push(pfirst, sfdoff);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001490 brcmf_sdio_rxfail(bus, true, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001491 } else {
1492 bus->glomerr = 0;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001493 brcmf_sdio_rxfail(bus, true, false);
Arend van Spriel80969832012-06-09 22:51:44 +02001494 bus->sdcnt.rxglomfail++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001495 brcmf_sdio_free_glom(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001496 }
Franky Lin38b0b0d2012-11-05 16:22:24 -08001497 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001498 bus->cur_read.len = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001499 return 0;
1500 }
1501
1502 /* Basic SD framing looks ok - process each packet (header) */
Arend van Spriel5b435de2011-10-05 13:19:03 +02001503
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001504 skb_queue_walk_safe(&bus->glom, pfirst, pnext) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001505 dptr = (u8 *) (pfirst->data);
1506 sublen = get_unaligned_le16(dptr);
Franky Lin6bc52312013-08-10 12:27:28 +02001507 doff = brcmf_sdio_getdatoffset(&dptr[SDPCM_HWHDR_LEN]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001508
Joe Perches1e023822012-01-15 00:38:40 -08001509 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_DATA_ON(),
Franky Lin9d7d6f92012-10-22 10:36:24 -07001510 dptr, pfirst->len,
1511 "Rx Subframe Data:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001512
1513 __skb_trim(pfirst, sublen);
1514 skb_pull(pfirst, doff);
1515
1516 if (pfirst->len == 0) {
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001517 skb_unlink(pfirst, &bus->glom);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001518 brcmu_pkt_buf_free_skb(pfirst);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001519 continue;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001520 }
1521
Joe Perches1e023822012-01-15 00:38:40 -08001522 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
1523 pfirst->data,
1524 min_t(int, pfirst->len, 32),
1525 "subframe %d to stack, %p (%p/%d) nxt/lnk %p/%p\n",
1526 bus->glom.qlen, pfirst, pfirst->data,
1527 pfirst->len, pfirst->next,
1528 pfirst->prev);
Arend van Spriel05f38202013-09-25 13:05:46 +02001529 skb_unlink(pfirst, &bus->glom);
1530 brcmf_rx_frame(bus->sdiodev->dev, pfirst);
1531 bus->sdcnt.rxglompkts++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001532 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02001533
Arend van Spriel80969832012-06-09 22:51:44 +02001534 bus->sdcnt.rxglomframes++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001535 }
1536 return num;
1537}
1538
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001539static int brcmf_sdio_dcmd_resp_wait(struct brcmf_sdio *bus, uint *condition,
1540 bool *pending)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001541{
1542 DECLARE_WAITQUEUE(wait, current);
1543 int timeout = msecs_to_jiffies(DCMD_RESP_TIMEOUT);
1544
1545 /* Wait until control frame is available */
1546 add_wait_queue(&bus->dcmd_resp_wait, &wait);
1547 set_current_state(TASK_INTERRUPTIBLE);
1548
1549 while (!(*condition) && (!signal_pending(current) && timeout))
1550 timeout = schedule_timeout(timeout);
1551
1552 if (signal_pending(current))
1553 *pending = true;
1554
1555 set_current_state(TASK_RUNNING);
1556 remove_wait_queue(&bus->dcmd_resp_wait, &wait);
1557
1558 return timeout;
1559}
1560
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001561static int brcmf_sdio_dcmd_resp_wake(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001562{
1563 if (waitqueue_active(&bus->dcmd_resp_wait))
1564 wake_up_interruptible(&bus->dcmd_resp_wait);
1565
1566 return 0;
1567}
1568static void
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001569brcmf_sdio_read_control(struct brcmf_sdio *bus, u8 *hdr, uint len, uint doff)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001570{
1571 uint rdlen, pad;
Franky Lindd43a012012-11-05 16:22:22 -08001572 u8 *buf = NULL, *rbuf;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001573 int sdret;
1574
1575 brcmf_dbg(TRACE, "Enter\n");
1576
Franky Lindd43a012012-11-05 16:22:22 -08001577 if (bus->rxblen)
1578 buf = vzalloc(bus->rxblen);
Joe Perches14f8dc42013-02-07 11:46:27 +00001579 if (!buf)
Franky Lindd43a012012-11-05 16:22:22 -08001580 goto done;
Joe Perches14f8dc42013-02-07 11:46:27 +00001581
Franky Lindd43a012012-11-05 16:22:22 -08001582 rbuf = bus->rxbuf;
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01001583 pad = ((unsigned long)rbuf % bus->head_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001584 if (pad)
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01001585 rbuf += (bus->head_align - pad);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001586
1587 /* Copy the already-read portion over */
Franky Lindd43a012012-11-05 16:22:22 -08001588 memcpy(buf, hdr, BRCMF_FIRSTREAD);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001589 if (len <= BRCMF_FIRSTREAD)
1590 goto gotpkt;
1591
1592 /* Raise rdlen to next SDIO block to avoid tail command */
1593 rdlen = len - BRCMF_FIRSTREAD;
1594 if (bus->roundup && bus->blocksize && (rdlen > bus->blocksize)) {
1595 pad = bus->blocksize - (rdlen % bus->blocksize);
1596 if ((pad <= bus->roundup) && (pad < bus->blocksize) &&
Franky Linb01a6b32011-12-16 18:37:03 -08001597 ((len + pad) < bus->sdiodev->bus_if->maxctl))
Arend van Spriel5b435de2011-10-05 13:19:03 +02001598 rdlen += pad;
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01001599 } else if (rdlen % bus->head_align) {
1600 rdlen += bus->head_align - (rdlen % bus->head_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001601 }
1602
Arend van Spriel5b435de2011-10-05 13:19:03 +02001603 /* Drop if the read is too big or it exceeds our maximum */
Franky Linb01a6b32011-12-16 18:37:03 -08001604 if ((rdlen + BRCMF_FIRSTREAD) > bus->sdiodev->bus_if->maxctl) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001605 brcmf_err("%d-byte control read exceeds %d-byte buffer\n",
Franky Linb01a6b32011-12-16 18:37:03 -08001606 rdlen, bus->sdiodev->bus_if->maxctl);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001607 brcmf_sdio_rxfail(bus, false, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001608 goto done;
1609 }
1610
Franky Linb01a6b32011-12-16 18:37:03 -08001611 if ((len - doff) > bus->sdiodev->bus_if->maxctl) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001612 brcmf_err("%d-byte ctl frame (%d-byte ctl data) exceeds %d-byte limit\n",
Franky Linb01a6b32011-12-16 18:37:03 -08001613 len, len - doff, bus->sdiodev->bus_if->maxctl);
Arend van Spriel80969832012-06-09 22:51:44 +02001614 bus->sdcnt.rx_toolong++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001615 brcmf_sdio_rxfail(bus, false, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001616 goto done;
1617 }
1618
Franky Lindd43a012012-11-05 16:22:22 -08001619 /* Read remain of frame body */
Arend van Spriela7cdd822013-12-12 11:58:59 +01001620 sdret = brcmf_sdiod_recv_buf(bus->sdiodev, rbuf, rdlen);
Arend van Spriel80969832012-06-09 22:51:44 +02001621 bus->sdcnt.f2rxdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001622
1623 /* Control frame failures need retransmission */
1624 if (sdret < 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001625 brcmf_err("read %d control bytes failed: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001626 rdlen, sdret);
Arend van Spriel80969832012-06-09 22:51:44 +02001627 bus->sdcnt.rxc_errors++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001628 brcmf_sdio_rxfail(bus, true, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001629 goto done;
Franky Lindd43a012012-11-05 16:22:22 -08001630 } else
1631 memcpy(buf + BRCMF_FIRSTREAD, rbuf, rdlen);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001632
1633gotpkt:
1634
Joe Perches1e023822012-01-15 00:38:40 -08001635 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_CTL_ON(),
Franky Lindd43a012012-11-05 16:22:22 -08001636 buf, len, "RxCtrl:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001637
1638 /* Point to valid data and indicate its length */
Franky Lindd43a012012-11-05 16:22:22 -08001639 spin_lock_bh(&bus->rxctl_lock);
1640 if (bus->rxctl) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001641 brcmf_err("last control frame is being processed.\n");
Franky Lindd43a012012-11-05 16:22:22 -08001642 spin_unlock_bh(&bus->rxctl_lock);
1643 vfree(buf);
1644 goto done;
1645 }
1646 bus->rxctl = buf + doff;
1647 bus->rxctl_orig = buf;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001648 bus->rxlen = len - doff;
Franky Lindd43a012012-11-05 16:22:22 -08001649 spin_unlock_bh(&bus->rxctl_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001650
1651done:
1652 /* Awake any waiters */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001653 brcmf_sdio_dcmd_resp_wake(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001654}
1655
1656/* Pad read to blocksize for efficiency */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001657static void brcmf_sdio_pad(struct brcmf_sdio *bus, u16 *pad, u16 *rdlen)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001658{
1659 if (bus->roundup && bus->blocksize && *rdlen > bus->blocksize) {
1660 *pad = bus->blocksize - (*rdlen % bus->blocksize);
1661 if (*pad <= bus->roundup && *pad < bus->blocksize &&
1662 *rdlen + *pad + BRCMF_FIRSTREAD < MAX_RX_DATASZ)
1663 *rdlen += *pad;
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01001664 } else if (*rdlen % bus->head_align) {
1665 *rdlen += bus->head_align - (*rdlen % bus->head_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001666 }
1667}
1668
Franky Lin4754fce2012-09-19 22:21:11 +02001669static uint brcmf_sdio_readframes(struct brcmf_sdio *bus, uint maxframes)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001670{
Arend van Spriel5b435de2011-10-05 13:19:03 +02001671 struct sk_buff *pkt; /* Packet for event or data frames */
1672 u16 pad; /* Number of pad bytes to read */
Arend van Spriel5b435de2011-10-05 13:19:03 +02001673 uint rxleft = 0; /* Remaining number of frames allowed */
Arend van Spriel349e7102013-03-03 12:45:28 +01001674 int ret; /* Return code from calls */
Arend van Spriel5b435de2011-10-05 13:19:03 +02001675 uint rxcount = 0; /* Total frames read */
Franky Lin6bc52312013-08-10 12:27:28 +02001676 struct brcmf_sdio_hdrinfo *rd = &bus->cur_read, rd_new;
Franky Lin4754fce2012-09-19 22:21:11 +02001677 u8 head_read = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001678
1679 brcmf_dbg(TRACE, "Enter\n");
1680
1681 /* Not finished unless we encounter no more frames indication */
Franky Lin4754fce2012-09-19 22:21:11 +02001682 bus->rxpending = true;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001683
Franky Lin4754fce2012-09-19 22:21:11 +02001684 for (rd->seq_num = bus->rx_seq, rxleft = maxframes;
Franky Lin8d169aa2011-11-22 17:21:52 -08001685 !bus->rxskip && rxleft &&
Franky Lin712ac5b2011-12-16 18:37:09 -08001686 bus->sdiodev->bus_if->state != BRCMF_BUS_DOWN;
Franky Lin4754fce2012-09-19 22:21:11 +02001687 rd->seq_num++, rxleft--) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001688
1689 /* Handle glomming separately */
Arend van Sprielb83db862011-10-19 12:51:09 +02001690 if (bus->glomd || !skb_queue_empty(&bus->glom)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001691 u8 cnt;
1692 brcmf_dbg(GLOM, "calling rxglom: glomd %p, glom %p\n",
Arend van Sprielb83db862011-10-19 12:51:09 +02001693 bus->glomd, skb_peek(&bus->glom));
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001694 cnt = brcmf_sdio_rxglom(bus, rd->seq_num);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001695 brcmf_dbg(GLOM, "rxglom returned %d\n", cnt);
Franky Lin4754fce2012-09-19 22:21:11 +02001696 rd->seq_num += cnt - 1;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001697 rxleft = (rxleft > cnt) ? (rxleft - cnt) : 1;
1698 continue;
1699 }
1700
Franky Lin4754fce2012-09-19 22:21:11 +02001701 rd->len_left = rd->len;
1702 /* read header first for unknow frame length */
Franky Lin38b0b0d2012-11-05 16:22:24 -08001703 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001704 if (!rd->len) {
Arend van Spriela39be272013-12-12 11:58:58 +01001705 ret = brcmf_sdiod_recv_buf(bus->sdiodev,
Arend van Spriela39be272013-12-12 11:58:58 +01001706 bus->rxhdr, BRCMF_FIRSTREAD);
Franky Lin4754fce2012-09-19 22:21:11 +02001707 bus->sdcnt.f2rxhdrs++;
Arend van Spriel349e7102013-03-03 12:45:28 +01001708 if (ret < 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001709 brcmf_err("RXHEADER FAILED: %d\n",
Arend van Spriel349e7102013-03-03 12:45:28 +01001710 ret);
Franky Lin4754fce2012-09-19 22:21:11 +02001711 bus->sdcnt.rx_hdrfail++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001712 brcmf_sdio_rxfail(bus, true, true);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001713 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001714 continue;
1715 }
1716
Franky Lin4754fce2012-09-19 22:21:11 +02001717 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() || BRCMF_HDRS_ON(),
1718 bus->rxhdr, SDPCM_HDRLEN,
1719 "RxHdr:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001720
Franky Lin6bc52312013-08-10 12:27:28 +02001721 if (brcmf_sdio_hdparse(bus, bus->rxhdr, rd,
1722 BRCMF_SDIO_FT_NORMAL)) {
Franky Lin38b0b0d2012-11-05 16:22:24 -08001723 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001724 if (!bus->rxpending)
1725 break;
1726 else
1727 continue;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001728 }
1729
Franky Lin4754fce2012-09-19 22:21:11 +02001730 if (rd->channel == SDPCM_CONTROL_CHANNEL) {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001731 brcmf_sdio_read_control(bus, bus->rxhdr,
1732 rd->len,
1733 rd->dat_offset);
Franky Lin4754fce2012-09-19 22:21:11 +02001734 /* prepare the descriptor for the next read */
1735 rd->len = rd->len_nxtfrm << 4;
1736 rd->len_nxtfrm = 0;
1737 /* treat all packet as event if we don't know */
1738 rd->channel = SDPCM_EVENT_CHANNEL;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001739 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001740 continue;
1741 }
1742 rd->len_left = rd->len > BRCMF_FIRSTREAD ?
1743 rd->len - BRCMF_FIRSTREAD : 0;
1744 head_read = BRCMF_FIRSTREAD;
1745 }
1746
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001747 brcmf_sdio_pad(bus, &pad, &rd->len_left);
Franky Lin4754fce2012-09-19 22:21:11 +02001748
1749 pkt = brcmu_pkt_buf_get_skb(rd->len_left + head_read +
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01001750 bus->head_align);
Franky Lin4754fce2012-09-19 22:21:11 +02001751 if (!pkt) {
1752 /* Give up on data, request rtx of events */
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001753 brcmf_err("brcmu_pkt_buf_get_skb failed\n");
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001754 brcmf_sdio_rxfail(bus, false,
Franky Lin4754fce2012-09-19 22:21:11 +02001755 RETRYCHAN(rd->channel));
Franky Lin38b0b0d2012-11-05 16:22:24 -08001756 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001757 continue;
1758 }
1759 skb_pull(pkt, head_read);
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01001760 pkt_align(pkt, rd->len_left, bus->head_align);
Franky Lin4754fce2012-09-19 22:21:11 +02001761
Arend van Spriela7cdd822013-12-12 11:58:59 +01001762 ret = brcmf_sdiod_recv_pkt(bus->sdiodev, pkt);
Franky Lin4754fce2012-09-19 22:21:11 +02001763 bus->sdcnt.f2rxdata++;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001764 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001765
Arend van Spriel349e7102013-03-03 12:45:28 +01001766 if (ret < 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001767 brcmf_err("read %d bytes from channel %d failed: %d\n",
Arend van Spriel349e7102013-03-03 12:45:28 +01001768 rd->len, rd->channel, ret);
Franky Lin4754fce2012-09-19 22:21:11 +02001769 brcmu_pkt_buf_free_skb(pkt);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001770 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001771 brcmf_sdio_rxfail(bus, true,
Franky Lin4754fce2012-09-19 22:21:11 +02001772 RETRYCHAN(rd->channel));
Franky Lin38b0b0d2012-11-05 16:22:24 -08001773 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001774 continue;
1775 }
1776
1777 if (head_read) {
1778 skb_push(pkt, head_read);
1779 memcpy(pkt->data, bus->rxhdr, head_read);
1780 head_read = 0;
1781 } else {
1782 memcpy(bus->rxhdr, pkt->data, SDPCM_HDRLEN);
1783 rd_new.seq_num = rd->seq_num;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001784 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin6bc52312013-08-10 12:27:28 +02001785 if (brcmf_sdio_hdparse(bus, bus->rxhdr, &rd_new,
1786 BRCMF_SDIO_FT_NORMAL)) {
Franky Lin4754fce2012-09-19 22:21:11 +02001787 rd->len = 0;
1788 brcmu_pkt_buf_free_skb(pkt);
1789 }
Arend van Spriel80969832012-06-09 22:51:44 +02001790 bus->sdcnt.rx_readahead_cnt++;
Franky Lin4754fce2012-09-19 22:21:11 +02001791 if (rd->len != roundup(rd_new.len, 16)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001792 brcmf_err("frame length mismatch:read %d, should be %d\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001793 rd->len,
1794 roundup(rd_new.len, 16) >> 4);
1795 rd->len = 0;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001796 brcmf_sdio_rxfail(bus, true, true);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001797 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001798 brcmu_pkt_buf_free_skb(pkt);
1799 continue;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001800 }
Franky Lin38b0b0d2012-11-05 16:22:24 -08001801 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001802 rd->len_nxtfrm = rd_new.len_nxtfrm;
1803 rd->channel = rd_new.channel;
1804 rd->dat_offset = rd_new.dat_offset;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001805
Joe Perches1e023822012-01-15 00:38:40 -08001806 brcmf_dbg_hex_dump(!(BRCMF_BYTES_ON() &&
1807 BRCMF_DATA_ON()) &&
1808 BRCMF_HDRS_ON(),
1809 bus->rxhdr, SDPCM_HDRLEN,
1810 "RxHdr:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001811
Franky Lin4754fce2012-09-19 22:21:11 +02001812 if (rd_new.channel == SDPCM_CONTROL_CHANNEL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001813 brcmf_err("readahead on control packet %d?\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001814 rd_new.seq_num);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001815 /* Force retry w/normal header read */
Franky Lin4754fce2012-09-19 22:21:11 +02001816 rd->len = 0;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001817 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001818 brcmf_sdio_rxfail(bus, false, true);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001819 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001820 brcmu_pkt_buf_free_skb(pkt);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001821 continue;
1822 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02001823 }
1824
Joe Perches1e023822012-01-15 00:38:40 -08001825 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_DATA_ON(),
Franky Lin4754fce2012-09-19 22:21:11 +02001826 pkt->data, rd->len, "Rx Data:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001827
Arend van Spriel5b435de2011-10-05 13:19:03 +02001828 /* Save superframe descriptor and allocate packet frame */
Franky Lin4754fce2012-09-19 22:21:11 +02001829 if (rd->channel == SDPCM_GLOM_CHANNEL) {
Franky Lin6bc52312013-08-10 12:27:28 +02001830 if (SDPCM_GLOMDESC(&bus->rxhdr[SDPCM_HWHDR_LEN])) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001831 brcmf_dbg(GLOM, "glom descriptor, %d bytes:\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001832 rd->len);
Joe Perches1e023822012-01-15 00:38:40 -08001833 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
Franky Lin4754fce2012-09-19 22:21:11 +02001834 pkt->data, rd->len,
Joe Perches1e023822012-01-15 00:38:40 -08001835 "Glom Data:\n");
Franky Lin4754fce2012-09-19 22:21:11 +02001836 __skb_trim(pkt, rd->len);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001837 skb_pull(pkt, SDPCM_HDRLEN);
1838 bus->glomd = pkt;
1839 } else {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001840 brcmf_err("%s: glom superframe w/o "
Arend van Spriel5b435de2011-10-05 13:19:03 +02001841 "descriptor!\n", __func__);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001842 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001843 brcmf_sdio_rxfail(bus, false, false);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001844 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001845 }
Franky Lin4754fce2012-09-19 22:21:11 +02001846 /* prepare the descriptor for the next read */
1847 rd->len = rd->len_nxtfrm << 4;
1848 rd->len_nxtfrm = 0;
1849 /* treat all packet as event if we don't know */
1850 rd->channel = SDPCM_EVENT_CHANNEL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001851 continue;
1852 }
1853
1854 /* Fill in packet len and prio, deliver upward */
Franky Lin4754fce2012-09-19 22:21:11 +02001855 __skb_trim(pkt, rd->len);
1856 skb_pull(pkt, rd->dat_offset);
1857
1858 /* prepare the descriptor for the next read */
1859 rd->len = rd->len_nxtfrm << 4;
1860 rd->len_nxtfrm = 0;
1861 /* treat all packet as event if we don't know */
1862 rd->channel = SDPCM_EVENT_CHANNEL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001863
1864 if (pkt->len == 0) {
1865 brcmu_pkt_buf_free_skb(pkt);
1866 continue;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001867 }
1868
Arend van Spriel05f38202013-09-25 13:05:46 +02001869 brcmf_rx_frame(bus->sdiodev->dev, pkt);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001870 }
Franky Lin4754fce2012-09-19 22:21:11 +02001871
Arend van Spriel5b435de2011-10-05 13:19:03 +02001872 rxcount = maxframes - rxleft;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001873 /* Message if we hit the limit */
1874 if (!rxleft)
Franky Lin4754fce2012-09-19 22:21:11 +02001875 brcmf_dbg(DATA, "hit rx limit of %d frames\n", maxframes);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001876 else
Arend van Spriel5b435de2011-10-05 13:19:03 +02001877 brcmf_dbg(DATA, "processed %d frames\n", rxcount);
1878 /* Back off rxseq if awaiting rtx, update rx_seq */
1879 if (bus->rxskip)
Franky Lin4754fce2012-09-19 22:21:11 +02001880 rd->seq_num--;
1881 bus->rx_seq = rd->seq_num;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001882
1883 return rxcount;
1884}
1885
Arend van Spriel5b435de2011-10-05 13:19:03 +02001886static void
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001887brcmf_sdio_wait_event_wakeup(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001888{
1889 if (waitqueue_active(&bus->ctrl_wait))
1890 wake_up_interruptible(&bus->ctrl_wait);
1891 return;
1892}
1893
Franky Lin8da9d2c2013-11-29 12:25:10 +01001894static int brcmf_sdio_txpkt_hdalign(struct brcmf_sdio *bus, struct sk_buff *pkt)
1895{
Arend van Spriele217d1c2013-11-29 12:25:13 +01001896 u16 head_pad;
Franky Lin8da9d2c2013-11-29 12:25:10 +01001897 u8 *dat_buf;
1898
Franky Lin8da9d2c2013-11-29 12:25:10 +01001899 dat_buf = (u8 *)(pkt->data);
1900
1901 /* Check head padding */
Arend van Spriele217d1c2013-11-29 12:25:13 +01001902 head_pad = ((unsigned long)dat_buf % bus->head_align);
Franky Lin8da9d2c2013-11-29 12:25:10 +01001903 if (head_pad) {
1904 if (skb_headroom(pkt) < head_pad) {
1905 bus->sdiodev->bus_if->tx_realloc++;
1906 head_pad = 0;
1907 if (skb_cow(pkt, head_pad))
1908 return -ENOMEM;
1909 }
1910 skb_push(pkt, head_pad);
1911 dat_buf = (u8 *)(pkt->data);
1912 memset(dat_buf, 0, head_pad + bus->tx_hdrlen);
1913 }
1914 return head_pad;
1915}
1916
Franky Lin5491c112013-09-25 13:05:43 +02001917/**
1918 * struct brcmf_skbuff_cb reserves first two bytes in sk_buff::cb for
1919 * bus layer usage.
1920 */
Franky Linb05e9252013-08-10 12:27:26 +02001921/* flag marking a dummy skb added for DMA alignment requirement */
Franky Lin5491c112013-09-25 13:05:43 +02001922#define ALIGN_SKB_FLAG 0x8000
Franky Linb05e9252013-08-10 12:27:26 +02001923/* bit mask of data length chopped from the previous packet */
Franky Lin5491c112013-09-25 13:05:43 +02001924#define ALIGN_SKB_CHOP_LEN_MASK 0x7fff
1925
Franky Lin8da9d2c2013-11-29 12:25:10 +01001926static int brcmf_sdio_txpkt_prep_sg(struct brcmf_sdio *bus,
Arend van Spriela64304f2013-10-15 15:44:56 +02001927 struct sk_buff_head *pktq,
Franky Lin8da9d2c2013-11-29 12:25:10 +01001928 struct sk_buff *pkt, u16 total_len)
Arend van Spriela64304f2013-10-15 15:44:56 +02001929{
Franky Lin8da9d2c2013-11-29 12:25:10 +01001930 struct brcmf_sdio_dev *sdiodev;
Arend van Spriela64304f2013-10-15 15:44:56 +02001931 struct sk_buff *pkt_pad;
Arend van Spriele217d1c2013-11-29 12:25:13 +01001932 u16 tail_pad, tail_chop, chain_pad;
Arend van Spriela64304f2013-10-15 15:44:56 +02001933 unsigned int blksize;
Franky Lin8da9d2c2013-11-29 12:25:10 +01001934 bool lastfrm;
1935 int ntail, ret;
Arend van Spriela64304f2013-10-15 15:44:56 +02001936
Franky Lin8da9d2c2013-11-29 12:25:10 +01001937 sdiodev = bus->sdiodev;
Arend van Spriela64304f2013-10-15 15:44:56 +02001938 blksize = sdiodev->func[SDIO_FUNC_2]->cur_blksize;
Arend van Spriela64304f2013-10-15 15:44:56 +02001939 /* sg entry alignment should be a divisor of block size */
Arend van Spriele217d1c2013-11-29 12:25:13 +01001940 WARN_ON(blksize % bus->sgentry_align);
Arend van Spriela64304f2013-10-15 15:44:56 +02001941
1942 /* Check tail padding */
Franky Lin8da9d2c2013-11-29 12:25:10 +01001943 lastfrm = skb_queue_is_last(pktq, pkt);
1944 tail_pad = 0;
Arend van Spriele217d1c2013-11-29 12:25:13 +01001945 tail_chop = pkt->len % bus->sgentry_align;
Franky Lin8da9d2c2013-11-29 12:25:10 +01001946 if (tail_chop)
Arend van Spriele217d1c2013-11-29 12:25:13 +01001947 tail_pad = bus->sgentry_align - tail_chop;
Franky Lin8da9d2c2013-11-29 12:25:10 +01001948 chain_pad = (total_len + tail_pad) % blksize;
1949 if (lastfrm && chain_pad)
1950 tail_pad += blksize - chain_pad;
Arend van Spriela64304f2013-10-15 15:44:56 +02001951 if (skb_tailroom(pkt) < tail_pad && pkt->len > blksize) {
Franky Lin8da9d2c2013-11-29 12:25:10 +01001952 pkt_pad = bus->txglom_sgpad;
1953 if (pkt_pad == NULL)
1954 brcmu_pkt_buf_get_skb(tail_pad + tail_chop);
Arend van Spriela64304f2013-10-15 15:44:56 +02001955 if (pkt_pad == NULL)
1956 return -ENOMEM;
Franky Lin8da9d2c2013-11-29 12:25:10 +01001957 ret = brcmf_sdio_txpkt_hdalign(bus, pkt_pad);
1958 if (unlikely(ret < 0))
1959 return ret;
Arend van Spriela64304f2013-10-15 15:44:56 +02001960 memcpy(pkt_pad->data,
1961 pkt->data + pkt->len - tail_chop,
1962 tail_chop);
1963 *(u32 *)(pkt_pad->cb) = ALIGN_SKB_FLAG + tail_chop;
1964 skb_trim(pkt, pkt->len - tail_chop);
1965 __skb_queue_after(pktq, pkt, pkt_pad);
1966 } else {
1967 ntail = pkt->data_len + tail_pad -
1968 (pkt->end - pkt->tail);
1969 if (skb_cloned(pkt) || ntail > 0)
1970 if (pskb_expand_head(pkt, 0, ntail, GFP_ATOMIC))
1971 return -ENOMEM;
1972 if (skb_linearize(pkt))
1973 return -ENOMEM;
Arend van Spriela64304f2013-10-15 15:44:56 +02001974 __skb_put(pkt, tail_pad);
1975 }
1976
Franky Lin8da9d2c2013-11-29 12:25:10 +01001977 return tail_pad;
Arend van Spriela64304f2013-10-15 15:44:56 +02001978}
1979
Franky Linb05e9252013-08-10 12:27:26 +02001980/**
1981 * brcmf_sdio_txpkt_prep - packet preparation for transmit
1982 * @bus: brcmf_sdio structure pointer
1983 * @pktq: packet list pointer
1984 * @chan: virtual channel to transmit the packet
1985 *
1986 * Processes to be applied to the packet
1987 * - Align data buffer pointer
1988 * - Align data buffer length
1989 * - Prepare header
1990 * Return: negative value if there is error
1991 */
1992static int
1993brcmf_sdio_txpkt_prep(struct brcmf_sdio *bus, struct sk_buff_head *pktq,
1994 uint chan)
1995{
Franky Lin8da9d2c2013-11-29 12:25:10 +01001996 u16 head_pad, total_len;
Arend van Spriela64304f2013-10-15 15:44:56 +02001997 struct sk_buff *pkt_next;
Franky Lin8da9d2c2013-11-29 12:25:10 +01001998 u8 txseq;
1999 int ret;
Franky Lin6bc52312013-08-10 12:27:28 +02002000 struct brcmf_sdio_hdrinfo hd_info = {0};
Franky Linb05e9252013-08-10 12:27:26 +02002001
Franky Lin8da9d2c2013-11-29 12:25:10 +01002002 txseq = bus->tx_seq;
2003 total_len = 0;
2004 skb_queue_walk(pktq, pkt_next) {
2005 /* alignment packet inserted in previous
2006 * loop cycle can be skipped as it is
2007 * already properly aligned and does not
2008 * need an sdpcm header.
2009 */
2010 if (*(u32 *)(pkt_next->cb) & ALIGN_SKB_FLAG)
2011 continue;
Franky Linb05e9252013-08-10 12:27:26 +02002012
Franky Lin8da9d2c2013-11-29 12:25:10 +01002013 /* align packet data pointer */
2014 ret = brcmf_sdio_txpkt_hdalign(bus, pkt_next);
2015 if (ret < 0)
2016 return ret;
2017 head_pad = (u16)ret;
2018 if (head_pad)
2019 memset(pkt_next->data, 0, head_pad + bus->tx_hdrlen);
Franky Linb05e9252013-08-10 12:27:26 +02002020
Franky Lin8da9d2c2013-11-29 12:25:10 +01002021 total_len += pkt_next->len;
Franky Linb05e9252013-08-10 12:27:26 +02002022
Arend van Spriela64304f2013-10-15 15:44:56 +02002023 hd_info.len = pkt_next->len;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002024 hd_info.lastfrm = skb_queue_is_last(pktq, pkt_next);
2025 if (bus->txglom && pktq->qlen > 1) {
2026 ret = brcmf_sdio_txpkt_prep_sg(bus, pktq,
2027 pkt_next, total_len);
2028 if (ret < 0)
2029 return ret;
2030 hd_info.tail_pad = (u16)ret;
2031 total_len += (u16)ret;
2032 }
2033
2034 hd_info.channel = chan;
2035 hd_info.dat_offset = head_pad + bus->tx_hdrlen;
2036 hd_info.seq_num = txseq++;
2037
2038 /* Now fill the header */
2039 brcmf_sdio_hdpack(bus, pkt_next->data, &hd_info);
2040
2041 if (BRCMF_BYTES_ON() &&
2042 ((BRCMF_CTL_ON() && chan == SDPCM_CONTROL_CHANNEL) ||
2043 (BRCMF_DATA_ON() && chan != SDPCM_CONTROL_CHANNEL)))
2044 brcmf_dbg_hex_dump(true, pkt_next, hd_info.len,
2045 "Tx Frame:\n");
2046 else if (BRCMF_HDRS_ON())
2047 brcmf_dbg_hex_dump(true, pkt_next,
2048 head_pad + bus->tx_hdrlen,
2049 "Tx Header:\n");
Franky Linb05e9252013-08-10 12:27:26 +02002050 }
Franky Lin8da9d2c2013-11-29 12:25:10 +01002051 /* Hardware length tag of the first packet should be total
2052 * length of the chain (including padding)
2053 */
2054 if (bus->txglom)
2055 brcmf_sdio_update_hwhdr(pktq->next->data, total_len);
Franky Linb05e9252013-08-10 12:27:26 +02002056 return 0;
2057}
2058
2059/**
2060 * brcmf_sdio_txpkt_postp - packet post processing for transmit
2061 * @bus: brcmf_sdio structure pointer
2062 * @pktq: packet list pointer
2063 *
2064 * Processes to be applied to the packet
2065 * - Remove head padding
2066 * - Remove tail padding
2067 */
2068static void
2069brcmf_sdio_txpkt_postp(struct brcmf_sdio *bus, struct sk_buff_head *pktq)
2070{
2071 u8 *hdr;
2072 u32 dat_offset;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002073 u16 tail_pad;
Franky Linb05e9252013-08-10 12:27:26 +02002074 u32 dummy_flags, chop_len;
2075 struct sk_buff *pkt_next, *tmp, *pkt_prev;
2076
2077 skb_queue_walk_safe(pktq, pkt_next, tmp) {
2078 dummy_flags = *(u32 *)(pkt_next->cb);
Franky Lin5491c112013-09-25 13:05:43 +02002079 if (dummy_flags & ALIGN_SKB_FLAG) {
2080 chop_len = dummy_flags & ALIGN_SKB_CHOP_LEN_MASK;
Franky Linb05e9252013-08-10 12:27:26 +02002081 if (chop_len) {
2082 pkt_prev = pkt_next->prev;
Franky Linb05e9252013-08-10 12:27:26 +02002083 skb_put(pkt_prev, chop_len);
2084 }
2085 __skb_unlink(pkt_next, pktq);
2086 brcmu_pkt_buf_free_skb(pkt_next);
2087 } else {
Franky Lin8da9d2c2013-11-29 12:25:10 +01002088 hdr = pkt_next->data + bus->tx_hdrlen - SDPCM_SWHDR_LEN;
Franky Linb05e9252013-08-10 12:27:26 +02002089 dat_offset = le32_to_cpu(*(__le32 *)hdr);
2090 dat_offset = (dat_offset & SDPCM_DOFFSET_MASK) >>
2091 SDPCM_DOFFSET_SHIFT;
2092 skb_pull(pkt_next, dat_offset);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002093 if (bus->txglom) {
2094 tail_pad = le16_to_cpu(*(__le16 *)(hdr - 2));
2095 skb_trim(pkt_next, pkt_next->len - tail_pad);
2096 }
Franky Linb05e9252013-08-10 12:27:26 +02002097 }
2098 }
2099}
2100
Arend van Spriel5b435de2011-10-05 13:19:03 +02002101/* Writes a HW/SW header into the packet and sends it. */
2102/* Assumes: (a) header space already there, (b) caller holds lock */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002103static int brcmf_sdio_txpkt(struct brcmf_sdio *bus, struct sk_buff_head *pktq,
2104 uint chan)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002105{
2106 int ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002107 int i;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002108 struct sk_buff *pkt_next, *tmp;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002109
2110 brcmf_dbg(TRACE, "Enter\n");
2111
Franky Lin8da9d2c2013-11-29 12:25:10 +01002112 ret = brcmf_sdio_txpkt_prep(bus, pktq, chan);
Franky Linb05e9252013-08-10 12:27:26 +02002113 if (ret)
2114 goto done;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002115
Franky Lin38b0b0d2012-11-05 16:22:24 -08002116 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriela7cdd822013-12-12 11:58:59 +01002117 ret = brcmf_sdiod_send_pkt(bus->sdiodev, pktq);
Arend van Spriel80969832012-06-09 22:51:44 +02002118 bus->sdcnt.f2txdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002119
2120 if (ret < 0) {
2121 /* On failure, abort the command and terminate the frame */
2122 brcmf_dbg(INFO, "sdio error %d, abort command and terminate frame\n",
2123 ret);
Arend van Spriel80969832012-06-09 22:51:44 +02002124 bus->sdcnt.tx_sderrs++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002125
Arend van Spriela39be272013-12-12 11:58:58 +01002126 brcmf_sdiod_abort(bus->sdiodev, SDIO_FUNC_2);
2127 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL,
2128 SFC_WF_TERM, NULL);
Arend van Spriel80969832012-06-09 22:51:44 +02002129 bus->sdcnt.f1regdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002130
2131 for (i = 0; i < 3; i++) {
2132 u8 hi, lo;
Arend van Spriela39be272013-12-12 11:58:58 +01002133 hi = brcmf_sdiod_regrb(bus->sdiodev,
2134 SBSDIO_FUNC1_WFRAMEBCHI, NULL);
2135 lo = brcmf_sdiod_regrb(bus->sdiodev,
2136 SBSDIO_FUNC1_WFRAMEBCLO, NULL);
Arend van Spriel80969832012-06-09 22:51:44 +02002137 bus->sdcnt.f1regdata += 2;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002138 if ((hi == 0) && (lo == 0))
2139 break;
2140 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002141 }
Franky Lin38b0b0d2012-11-05 16:22:24 -08002142 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002143
2144done:
Franky Lin8da9d2c2013-11-29 12:25:10 +01002145 brcmf_sdio_txpkt_postp(bus, pktq);
2146 if (ret == 0)
2147 bus->tx_seq = (bus->tx_seq + pktq->qlen) % SDPCM_SEQ_WRAP;
2148 skb_queue_walk_safe(pktq, pkt_next, tmp) {
2149 __skb_unlink(pkt_next, pktq);
2150 brcmf_txcomplete(bus->sdiodev->dev, pkt_next, ret == 0);
2151 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002152 return ret;
2153}
2154
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002155static uint brcmf_sdio_sendfromq(struct brcmf_sdio *bus, uint maxframes)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002156{
2157 struct sk_buff *pkt;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002158 struct sk_buff_head pktq;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002159 u32 intstatus = 0;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002160 int ret = 0, prec_out, i;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002161 uint cnt = 0;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002162 u8 tx_prec_map, pkt_num;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002163
Arend van Spriel5b435de2011-10-05 13:19:03 +02002164 brcmf_dbg(TRACE, "Enter\n");
2165
2166 tx_prec_map = ~bus->flowcontrol;
2167
2168 /* Send frames until the limit or some other event */
Franky Lin8da9d2c2013-11-29 12:25:10 +01002169 for (cnt = 0; (cnt < maxframes) && data_ok(bus);) {
2170 pkt_num = 1;
2171 __skb_queue_head_init(&pktq);
2172 if (bus->txglom)
2173 pkt_num = min_t(u8, bus->tx_max - bus->tx_seq,
2174 brcmf_sdio_txglomsz);
2175 pkt_num = min_t(u32, pkt_num,
2176 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002177 spin_lock_bh(&bus->txqlock);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002178 for (i = 0; i < pkt_num; i++) {
2179 pkt = brcmu_pktq_mdeq(&bus->txq, tx_prec_map,
2180 &prec_out);
2181 if (pkt == NULL)
2182 break;
2183 __skb_queue_tail(&pktq, pkt);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002184 }
2185 spin_unlock_bh(&bus->txqlock);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002186 if (i == 0)
2187 break;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002188
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002189 ret = brcmf_sdio_txpkt(bus, &pktq, SDPCM_DATA_CHANNEL);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002190 cnt += i;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002191
2192 /* In poll mode, need to check for other events */
2193 if (!bus->intr && cnt) {
2194 /* Check device status, signal pending interrupt */
Franky Lin38b0b0d2012-11-05 16:22:24 -08002195 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin5c15c232012-05-04 18:27:37 -07002196 ret = r_sdreg32(bus, &intstatus,
2197 offsetof(struct sdpcmd_regs,
2198 intstatus));
Franky Lin38b0b0d2012-11-05 16:22:24 -08002199 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel80969832012-06-09 22:51:44 +02002200 bus->sdcnt.f2txdata++;
Franky Lin5c15c232012-05-04 18:27:37 -07002201 if (ret != 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002202 break;
2203 if (intstatus & bus->hostintmask)
Franky Lin1d382272012-09-13 21:11:59 +02002204 atomic_set(&bus->ipend, 1);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002205 }
2206 }
2207
2208 /* Deflow-control stack if needed */
Hante Meuleman05dde972013-02-06 18:40:36 +01002209 if ((bus->sdiodev->bus_if->state == BRCMF_BUS_DATA) &&
Franky Linc8bf3482011-12-16 18:37:08 -08002210 bus->txoff && (pktq_len(&bus->txq) < TXLOW)) {
Hante Meuleman90d03ff2012-09-11 21:18:46 +02002211 bus->txoff = false;
2212 brcmf_txflowblock(bus->sdiodev->dev, false);
Franky Linc8bf3482011-12-16 18:37:08 -08002213 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002214
2215 return cnt;
2216}
2217
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002218static void brcmf_sdio_bus_stop(struct device *dev)
Franky Lina9ffda82011-12-16 18:37:12 -08002219{
2220 u32 local_hostintmask;
2221 u8 saveclk;
Franky Lina9ffda82011-12-16 18:37:12 -08002222 int err;
2223 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01002224 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Lina9ffda82011-12-16 18:37:12 -08002225 struct brcmf_sdio *bus = sdiodev->bus;
2226
2227 brcmf_dbg(TRACE, "Enter\n");
2228
2229 if (bus->watchdog_tsk) {
2230 send_sig(SIGTERM, bus->watchdog_tsk, 1);
2231 kthread_stop(bus->watchdog_tsk);
2232 bus->watchdog_tsk = NULL;
2233 }
2234
Franky Lin38b0b0d2012-11-05 16:22:24 -08002235 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lina9ffda82011-12-16 18:37:12 -08002236
Franky Lina9ffda82011-12-16 18:37:12 -08002237 /* Enable clock for device interrupts */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002238 brcmf_sdio_bus_sleep(bus, false, false);
Franky Lina9ffda82011-12-16 18:37:12 -08002239
2240 /* Disable and clear interrupts at the chip level also */
Franky Lin58692752012-05-04 18:27:36 -07002241 w_sdreg32(bus, 0, offsetof(struct sdpcmd_regs, hostintmask));
Franky Lina9ffda82011-12-16 18:37:12 -08002242 local_hostintmask = bus->hostintmask;
2243 bus->hostintmask = 0;
2244
2245 /* Change our idea of bus state */
2246 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
2247
2248 /* Force clocks on backplane to be sure F2 interrupt propagates */
Arend van Spriela39be272013-12-12 11:58:58 +01002249 saveclk = brcmf_sdiod_regrb(bus->sdiodev,
2250 SBSDIO_FUNC1_CHIPCLKCSR, &err);
Franky Lina9ffda82011-12-16 18:37:12 -08002251 if (!err) {
Arend van Spriela39be272013-12-12 11:58:58 +01002252 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
2253 (saveclk | SBSDIO_FORCE_HT), &err);
Franky Lina9ffda82011-12-16 18:37:12 -08002254 }
2255 if (err)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002256 brcmf_err("Failed to force clock for F2: err %d\n", err);
Franky Lina9ffda82011-12-16 18:37:12 -08002257
2258 /* Turn off the bus (F2), free any pending packets */
2259 brcmf_dbg(INTR, "disable SDIO interrupts\n");
Arend van Spriel71370eb2013-12-12 11:58:53 +01002260 sdio_disable_func(bus->sdiodev->func[SDIO_FUNC_2]);
Franky Lina9ffda82011-12-16 18:37:12 -08002261
2262 /* Clear any pending interrupts now that F2 is disabled */
2263 w_sdreg32(bus, local_hostintmask,
Franky Lin58692752012-05-04 18:27:36 -07002264 offsetof(struct sdpcmd_regs, intstatus));
Franky Lina9ffda82011-12-16 18:37:12 -08002265
Franky Lin38b0b0d2012-11-05 16:22:24 -08002266 sdio_release_host(bus->sdiodev->func[1]);
Franky Lina9ffda82011-12-16 18:37:12 -08002267
2268 /* Clear the data packet queues */
2269 brcmu_pktq_flush(&bus->txq, true, NULL, NULL);
2270
2271 /* Clear any held glomming stuff */
2272 if (bus->glomd)
2273 brcmu_pkt_buf_free_skb(bus->glomd);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002274 brcmf_sdio_free_glom(bus);
Franky Lina9ffda82011-12-16 18:37:12 -08002275
2276 /* Clear rx control and wake any waiters */
Franky Lindd43a012012-11-05 16:22:22 -08002277 spin_lock_bh(&bus->rxctl_lock);
Franky Lina9ffda82011-12-16 18:37:12 -08002278 bus->rxlen = 0;
Franky Lindd43a012012-11-05 16:22:22 -08002279 spin_unlock_bh(&bus->rxctl_lock);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002280 brcmf_sdio_dcmd_resp_wake(bus);
Franky Lina9ffda82011-12-16 18:37:12 -08002281
2282 /* Reset some F2 state stuff */
2283 bus->rxskip = false;
2284 bus->tx_seq = bus->rx_seq = 0;
Franky Lina9ffda82011-12-16 18:37:12 -08002285}
2286
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002287static inline void brcmf_sdio_clrintr(struct brcmf_sdio *bus)
Franky Linba89bf12012-04-27 18:56:59 -07002288{
2289 unsigned long flags;
2290
Hante Meuleman668761a2013-04-12 10:55:55 +02002291 if (bus->sdiodev->oob_irq_requested) {
2292 spin_lock_irqsave(&bus->sdiodev->irq_en_lock, flags);
2293 if (!bus->sdiodev->irq_en && !atomic_read(&bus->ipend)) {
2294 enable_irq(bus->sdiodev->pdata->oob_irq_nr);
2295 bus->sdiodev->irq_en = true;
2296 }
2297 spin_unlock_irqrestore(&bus->sdiodev->irq_en_lock, flags);
Franky Linba89bf12012-04-27 18:56:59 -07002298 }
Franky Linba89bf12012-04-27 18:56:59 -07002299}
Franky Linba89bf12012-04-27 18:56:59 -07002300
Franky Lin45316032012-09-13 21:12:03 +02002301static int brcmf_sdio_intr_rstatus(struct brcmf_sdio *bus)
2302{
2303 u8 idx;
2304 u32 addr;
2305 unsigned long val;
2306 int n, ret;
2307
2308 idx = brcmf_sdio_chip_getinfidx(bus->ci, BCMA_CORE_SDIO_DEV);
2309 addr = bus->ci->c_inf[idx].base +
2310 offsetof(struct sdpcmd_regs, intstatus);
2311
Arend van Spriela39be272013-12-12 11:58:58 +01002312 val = brcmf_sdiod_regrl(bus->sdiodev, addr, &ret);
Franky Lin45316032012-09-13 21:12:03 +02002313 bus->sdcnt.f1regdata++;
2314 if (ret != 0)
2315 val = 0;
2316
2317 val &= bus->hostintmask;
2318 atomic_set(&bus->fcstate, !!(val & I_HMB_FC_STATE));
2319
2320 /* Clear interrupts */
2321 if (val) {
Arend van Spriela39be272013-12-12 11:58:58 +01002322 brcmf_sdiod_regwl(bus->sdiodev, addr, val, &ret);
Franky Lin45316032012-09-13 21:12:03 +02002323 bus->sdcnt.f1regdata++;
2324 }
2325
2326 if (ret) {
2327 atomic_set(&bus->intstatus, 0);
2328 } else if (val) {
2329 for_each_set_bit(n, &val, 32)
2330 set_bit(n, (unsigned long *)&bus->intstatus.counter);
2331 }
2332
2333 return ret;
2334}
2335
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002336static void brcmf_sdio_dpc(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002337{
Franky Lin45316032012-09-13 21:12:03 +02002338 u32 newstatus = 0;
2339 unsigned long intstatus;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002340 uint rxlimit = bus->rxbound; /* Rx frames to read before resched */
2341 uint txlimit = bus->txbound; /* Tx frames to send before resched */
2342 uint framecnt = 0; /* Temporary counter of tx/rx frames */
Franky Lin45316032012-09-13 21:12:03 +02002343 int err = 0, n;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002344
2345 brcmf_dbg(TRACE, "Enter\n");
2346
Franky Lin38b0b0d2012-11-05 16:22:24 -08002347 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002348
2349 /* If waiting for HTAVAIL, check status */
Piotr Haber4a3da992013-04-11 13:28:46 +02002350 if (!bus->sr_enabled && bus->clkstate == CLK_PENDING) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02002351 u8 clkctl, devctl = 0;
2352
Joe Perches8ae74652012-01-15 00:38:38 -08002353#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02002354 /* Check for inconsistent device control */
Arend van Spriela39be272013-12-12 11:58:58 +01002355 devctl = brcmf_sdiod_regrb(bus->sdiodev,
2356 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002357 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002358 brcmf_err("error reading DEVCTL: %d\n", err);
Franky Lin712ac5b2011-12-16 18:37:09 -08002359 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002360 }
Joe Perches8ae74652012-01-15 00:38:38 -08002361#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02002362
2363 /* Read CSR, if clock on switch to AVAIL, else ignore */
Arend van Spriela39be272013-12-12 11:58:58 +01002364 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
2365 SBSDIO_FUNC1_CHIPCLKCSR, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002366 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002367 brcmf_err("error reading CSR: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02002368 err);
Franky Lin712ac5b2011-12-16 18:37:09 -08002369 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002370 }
2371
Arend van Sprielc3203372013-04-03 12:40:44 +02002372 brcmf_dbg(SDIO, "DPC: PENDING, devctl 0x%02x clkctl 0x%02x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02002373 devctl, clkctl);
2374
2375 if (SBSDIO_HTAV(clkctl)) {
Arend van Spriela39be272013-12-12 11:58:58 +01002376 devctl = brcmf_sdiod_regrb(bus->sdiodev,
2377 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002378 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002379 brcmf_err("error reading DEVCTL: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02002380 err);
Franky Lin712ac5b2011-12-16 18:37:09 -08002381 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002382 }
2383 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
Arend van Spriela39be272013-12-12 11:58:58 +01002384 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
2385 devctl, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002386 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002387 brcmf_err("error writing DEVCTL: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02002388 err);
Franky Lin712ac5b2011-12-16 18:37:09 -08002389 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002390 }
2391 bus->clkstate = CLK_AVAIL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002392 }
2393 }
2394
Arend van Spriel5b435de2011-10-05 13:19:03 +02002395 /* Make sure backplane clock is on */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002396 brcmf_sdio_bus_sleep(bus, false, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002397
2398 /* Pending interrupt indicates new device status */
Franky Lin1d382272012-09-13 21:11:59 +02002399 if (atomic_read(&bus->ipend) > 0) {
2400 atomic_set(&bus->ipend, 0);
Franky Lin45316032012-09-13 21:12:03 +02002401 err = brcmf_sdio_intr_rstatus(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002402 }
2403
Franky Lin45316032012-09-13 21:12:03 +02002404 /* Start with leftover status bits */
2405 intstatus = atomic_xchg(&bus->intstatus, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002406
2407 /* Handle flow-control change: read new state in case our ack
2408 * crossed another change interrupt. If change still set, assume
2409 * FC ON for safety, let next loop through do the debounce.
2410 */
2411 if (intstatus & I_HMB_FC_CHANGE) {
2412 intstatus &= ~I_HMB_FC_CHANGE;
Franky Lin5c15c232012-05-04 18:27:37 -07002413 err = w_sdreg32(bus, I_HMB_FC_CHANGE,
2414 offsetof(struct sdpcmd_regs, intstatus));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002415
Franky Lin5c15c232012-05-04 18:27:37 -07002416 err = r_sdreg32(bus, &newstatus,
2417 offsetof(struct sdpcmd_regs, intstatus));
Arend van Spriel80969832012-06-09 22:51:44 +02002418 bus->sdcnt.f1regdata += 2;
Franky Lin45316032012-09-13 21:12:03 +02002419 atomic_set(&bus->fcstate,
2420 !!(newstatus & (I_HMB_FC_STATE | I_HMB_FC_CHANGE)));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002421 intstatus |= (newstatus & bus->hostintmask);
2422 }
2423
2424 /* Handle host mailbox indication */
2425 if (intstatus & I_HMB_HOST_INT) {
2426 intstatus &= ~I_HMB_HOST_INT;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002427 intstatus |= brcmf_sdio_hostmail(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002428 }
2429
Franky Lin38b0b0d2012-11-05 16:22:24 -08002430 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin7cdf57d2012-11-05 16:22:23 -08002431
Arend van Spriel5b435de2011-10-05 13:19:03 +02002432 /* Generally don't ask for these, can get CRC errors... */
2433 if (intstatus & I_WR_OOSYNC) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002434 brcmf_err("Dongle reports WR_OOSYNC\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002435 intstatus &= ~I_WR_OOSYNC;
2436 }
2437
2438 if (intstatus & I_RD_OOSYNC) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002439 brcmf_err("Dongle reports RD_OOSYNC\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002440 intstatus &= ~I_RD_OOSYNC;
2441 }
2442
2443 if (intstatus & I_SBINT) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002444 brcmf_err("Dongle reports SBINT\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002445 intstatus &= ~I_SBINT;
2446 }
2447
2448 /* Would be active due to wake-wlan in gSPI */
2449 if (intstatus & I_CHIPACTIVE) {
2450 brcmf_dbg(INFO, "Dongle reports CHIPACTIVE\n");
2451 intstatus &= ~I_CHIPACTIVE;
2452 }
2453
2454 /* Ignore frame indications if rxskip is set */
2455 if (bus->rxskip)
2456 intstatus &= ~I_HMB_FRAME_IND;
2457
2458 /* On frame indication, read available frames */
Franky Lin03d5c362012-09-13 21:12:01 +02002459 if (PKT_AVAILABLE() && bus->clkstate == CLK_AVAIL) {
Franky Lin4754fce2012-09-19 22:21:11 +02002460 framecnt = brcmf_sdio_readframes(bus, rxlimit);
2461 if (!bus->rxpending)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002462 intstatus &= ~I_HMB_FRAME_IND;
2463 rxlimit -= min(framecnt, rxlimit);
2464 }
2465
2466 /* Keep still-pending events for next scheduling */
Franky Lin45316032012-09-13 21:12:03 +02002467 if (intstatus) {
2468 for_each_set_bit(n, &intstatus, 32)
2469 set_bit(n, (unsigned long *)&bus->intstatus.counter);
2470 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002471
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002472 brcmf_sdio_clrintr(bus);
Franky Linba89bf12012-04-27 18:56:59 -07002473
Arend van Spriel5b435de2011-10-05 13:19:03 +02002474 if (data_ok(bus) && bus->ctrl_frame_stat &&
2475 (bus->clkstate == CLK_AVAIL)) {
Franky Lin03d5c362012-09-13 21:12:01 +02002476 int i;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002477
Franky Lin38b0b0d2012-11-05 16:22:24 -08002478 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriela7cdd822013-12-12 11:58:59 +01002479 err = brcmf_sdiod_send_buf(bus->sdiodev, bus->ctrl_frame_buf,
Arend van Spriela39be272013-12-12 11:58:58 +01002480 (u32)bus->ctrl_frame_len);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002481
Franky Lin03d5c362012-09-13 21:12:01 +02002482 if (err < 0) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02002483 /* On failure, abort the command and
2484 terminate the frame */
2485 brcmf_dbg(INFO, "sdio error %d, abort command and terminate frame\n",
Franky Lin03d5c362012-09-13 21:12:01 +02002486 err);
Arend van Spriel80969832012-06-09 22:51:44 +02002487 bus->sdcnt.tx_sderrs++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002488
Arend van Spriela39be272013-12-12 11:58:58 +01002489 brcmf_sdiod_abort(bus->sdiodev, SDIO_FUNC_2);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002490
Arend van Spriela39be272013-12-12 11:58:58 +01002491 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL,
2492 SFC_WF_TERM, &err);
Arend van Spriel80969832012-06-09 22:51:44 +02002493 bus->sdcnt.f1regdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002494
2495 for (i = 0; i < 3; i++) {
2496 u8 hi, lo;
Arend van Spriela39be272013-12-12 11:58:58 +01002497 hi = brcmf_sdiod_regrb(bus->sdiodev,
2498 SBSDIO_FUNC1_WFRAMEBCHI,
2499 &err);
2500 lo = brcmf_sdiod_regrb(bus->sdiodev,
2501 SBSDIO_FUNC1_WFRAMEBCLO,
2502 &err);
Arend van Spriel80969832012-06-09 22:51:44 +02002503 bus->sdcnt.f1regdata += 2;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002504 if ((hi == 0) && (lo == 0))
2505 break;
2506 }
2507
Franky Lin03d5c362012-09-13 21:12:01 +02002508 } else {
Franky Lin6bc52312013-08-10 12:27:28 +02002509 bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQ_WRAP;
Franky Lin03d5c362012-09-13 21:12:01 +02002510 }
Franky Lin38b0b0d2012-11-05 16:22:24 -08002511 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002512 bus->ctrl_frame_stat = false;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002513 brcmf_sdio_wait_event_wakeup(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002514 }
2515 /* Send queued frames (limit 1 if rx may still be pending) */
Franky Lin45316032012-09-13 21:12:03 +02002516 else if ((bus->clkstate == CLK_AVAIL) && !atomic_read(&bus->fcstate) &&
Arend van Spriel5b435de2011-10-05 13:19:03 +02002517 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) && txlimit
2518 && data_ok(bus)) {
Franky Lin4754fce2012-09-19 22:21:11 +02002519 framecnt = bus->rxpending ? min(txlimit, bus->txminmax) :
2520 txlimit;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002521 framecnt = brcmf_sdio_sendfromq(bus, framecnt);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002522 txlimit -= framecnt;
2523 }
2524
Franky Lin5c15c232012-05-04 18:27:37 -07002525 if ((bus->sdiodev->bus_if->state == BRCMF_BUS_DOWN) || (err != 0)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002526 brcmf_err("failed backplane access over SDIO, halting operation\n");
Franky Lin712ac5b2011-12-16 18:37:09 -08002527 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
Franky Lin45316032012-09-13 21:12:03 +02002528 atomic_set(&bus->intstatus, 0);
2529 } else if (atomic_read(&bus->intstatus) ||
2530 atomic_read(&bus->ipend) > 0 ||
2531 (!atomic_read(&bus->fcstate) &&
2532 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) &&
2533 data_ok(bus)) || PKT_AVAILABLE()) {
Arend van Sprielfccfe932013-06-18 13:29:26 +02002534 atomic_inc(&bus->dpc_tskcnt);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002535 }
2536
Arend van Spriel5b435de2011-10-05 13:19:03 +02002537 /* If we're done for now, turn off clock request. */
2538 if ((bus->clkstate != CLK_PENDING)
2539 && bus->idletime == BRCMF_IDLE_IMMEDIATE) {
2540 bus->activity = false;
Piotr Haber4a3da992013-04-11 13:28:46 +02002541 brcmf_dbg(SDIO, "idle state\n");
Franky Lin38b0b0d2012-11-05 16:22:24 -08002542 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002543 brcmf_sdio_bus_sleep(bus, true, false);
Franky Lin38b0b0d2012-11-05 16:22:24 -08002544 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002545 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002546}
2547
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002548static struct pktq *brcmf_sdio_bus_gettxq(struct device *dev)
Arend van Spriele2432b62013-04-03 12:40:38 +02002549{
2550 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
2551 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
2552 struct brcmf_sdio *bus = sdiodev->bus;
2553
2554 return &bus->txq;
2555}
2556
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002557static int brcmf_sdio_bus_txdata(struct device *dev, struct sk_buff *pkt)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002558{
2559 int ret = -EBADE;
2560 uint datalen, prec;
Franky Linbf347bb2011-11-22 17:21:56 -08002561 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01002562 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Linbf347bb2011-11-22 17:21:56 -08002563 struct brcmf_sdio *bus = sdiodev->bus;
Arend van Spriel4061f892013-08-10 12:27:19 +02002564 ulong flags;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002565
2566 brcmf_dbg(TRACE, "Enter\n");
2567
2568 datalen = pkt->len;
2569
2570 /* Add space for the header */
Franky Lin706478c2013-08-10 12:27:29 +02002571 skb_push(pkt, bus->tx_hdrlen);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002572 /* precondition: IS_ALIGNED((unsigned long)(pkt->data), 2) */
2573
2574 prec = prio2prec((pkt->priority & PRIOMASK));
2575
2576 /* Check for existing queue, current flow-control,
2577 pending event, or pending clock */
2578 brcmf_dbg(TRACE, "deferring pktq len %d\n", pktq_len(&bus->txq));
Arend van Spriel80969832012-06-09 22:51:44 +02002579 bus->sdcnt.fcqueued++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002580
2581 /* Priority based enq */
Arend van Spriel4061f892013-08-10 12:27:19 +02002582 spin_lock_irqsave(&bus->txqlock, flags);
Joe Perches23677ce2012-02-09 11:17:23 +00002583 if (!brcmf_c_prec_enq(bus->sdiodev->dev, &bus->txq, pkt, prec)) {
Franky Lin706478c2013-08-10 12:27:29 +02002584 skb_pull(pkt, bus->tx_hdrlen);
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002585 brcmf_err("out of bus->txq !!!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002586 ret = -ENOSR;
2587 } else {
2588 ret = 0;
2589 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002590
Franky Linc8bf3482011-12-16 18:37:08 -08002591 if (pktq_len(&bus->txq) >= TXHI) {
Hante Meuleman90d03ff2012-09-11 21:18:46 +02002592 bus->txoff = true;
2593 brcmf_txflowblock(bus->sdiodev->dev, true);
Franky Linc8bf3482011-12-16 18:37:08 -08002594 }
Arend van Spriel4061f892013-08-10 12:27:19 +02002595 spin_unlock_irqrestore(&bus->txqlock, flags);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002596
Joe Perches8ae74652012-01-15 00:38:38 -08002597#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02002598 if (pktq_plen(&bus->txq, prec) > qcount[prec])
2599 qcount[prec] = pktq_plen(&bus->txq, prec);
2600#endif
Franky Linf1e68c22012-09-13 21:12:00 +02002601
Arend van Sprielfccfe932013-06-18 13:29:26 +02002602 if (atomic_read(&bus->dpc_tskcnt) == 0) {
2603 atomic_inc(&bus->dpc_tskcnt);
Franky Linf1e68c22012-09-13 21:12:00 +02002604 queue_work(bus->brcmf_wq, &bus->datawork);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002605 }
2606
2607 return ret;
2608}
2609
Joe Perches8ae74652012-01-15 00:38:38 -08002610#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02002611#define CONSOLE_LINE_MAX 192
2612
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002613static int brcmf_sdio_readconsole(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002614{
2615 struct brcmf_console *c = &bus->console;
2616 u8 line[CONSOLE_LINE_MAX], ch;
2617 u32 n, idx, addr;
2618 int rv;
2619
2620 /* Don't do anything until FWREADY updates console address */
2621 if (bus->console_addr == 0)
2622 return 0;
2623
2624 /* Read console log struct */
2625 addr = bus->console_addr + offsetof(struct rte_console, log_le);
Arend van Spriela39be272013-12-12 11:58:58 +01002626 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, (u8 *)&c->log_le,
2627 sizeof(c->log_le));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002628 if (rv < 0)
2629 return rv;
2630
2631 /* Allocate console buffer (one time only) */
2632 if (c->buf == NULL) {
2633 c->bufsize = le32_to_cpu(c->log_le.buf_size);
2634 c->buf = kmalloc(c->bufsize, GFP_ATOMIC);
2635 if (c->buf == NULL)
2636 return -ENOMEM;
2637 }
2638
2639 idx = le32_to_cpu(c->log_le.idx);
2640
2641 /* Protect against corrupt value */
2642 if (idx > c->bufsize)
2643 return -EBADE;
2644
2645 /* Skip reading the console buffer if the index pointer
2646 has not moved */
2647 if (idx == c->last)
2648 return 0;
2649
2650 /* Read the console buffer */
2651 addr = le32_to_cpu(c->log_le.buf);
Arend van Spriela39be272013-12-12 11:58:58 +01002652 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, c->buf, c->bufsize);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002653 if (rv < 0)
2654 return rv;
2655
2656 while (c->last != idx) {
2657 for (n = 0; n < CONSOLE_LINE_MAX - 2; n++) {
2658 if (c->last == idx) {
2659 /* This would output a partial line.
2660 * Instead, back up
2661 * the buffer pointer and output this
2662 * line next time around.
2663 */
2664 if (c->last >= n)
2665 c->last -= n;
2666 else
2667 c->last = c->bufsize - n;
2668 goto break2;
2669 }
2670 ch = c->buf[c->last];
2671 c->last = (c->last + 1) % c->bufsize;
2672 if (ch == '\n')
2673 break;
2674 line[n] = ch;
2675 }
2676
2677 if (n > 0) {
2678 if (line[n - 1] == '\r')
2679 n--;
2680 line[n] = 0;
Joe Perches18aad4f2012-01-15 00:38:42 -08002681 pr_debug("CONSOLE: %s\n", line);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002682 }
2683 }
2684break2:
2685
2686 return 0;
2687}
Joe Perches8ae74652012-01-15 00:38:38 -08002688#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02002689
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002690static int brcmf_sdio_tx_frame(struct brcmf_sdio *bus, u8 *frame, u16 len)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002691{
2692 int i;
2693 int ret;
2694
2695 bus->ctrl_frame_stat = false;
Arend van Spriela7cdd822013-12-12 11:58:59 +01002696 ret = brcmf_sdiod_send_buf(bus->sdiodev, frame, len);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002697
2698 if (ret < 0) {
2699 /* On failure, abort the command and terminate the frame */
2700 brcmf_dbg(INFO, "sdio error %d, abort command and terminate frame\n",
2701 ret);
Arend van Spriel80969832012-06-09 22:51:44 +02002702 bus->sdcnt.tx_sderrs++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002703
Arend van Spriela39be272013-12-12 11:58:58 +01002704 brcmf_sdiod_abort(bus->sdiodev, SDIO_FUNC_2);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002705
Arend van Spriela39be272013-12-12 11:58:58 +01002706 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL,
2707 SFC_WF_TERM, NULL);
Arend van Spriel80969832012-06-09 22:51:44 +02002708 bus->sdcnt.f1regdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002709
2710 for (i = 0; i < 3; i++) {
2711 u8 hi, lo;
Arend van Spriela39be272013-12-12 11:58:58 +01002712 hi = brcmf_sdiod_regrb(bus->sdiodev,
2713 SBSDIO_FUNC1_WFRAMEBCHI, NULL);
2714 lo = brcmf_sdiod_regrb(bus->sdiodev,
2715 SBSDIO_FUNC1_WFRAMEBCLO, NULL);
Arend van Spriel80969832012-06-09 22:51:44 +02002716 bus->sdcnt.f1regdata += 2;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002717 if (hi == 0 && lo == 0)
2718 break;
2719 }
2720 return ret;
2721 }
2722
Franky Lin6bc52312013-08-10 12:27:28 +02002723 bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQ_WRAP;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002724
2725 return ret;
2726}
2727
Franky Linfcf094f2011-12-16 18:37:16 -08002728static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002729brcmf_sdio_bus_txctl(struct device *dev, unsigned char *msg, uint msglen)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002730{
2731 u8 *frame;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002732 u16 len, pad;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002733 uint retries = 0;
2734 u8 doff = 0;
2735 int ret = -1;
Franky Lin47a1ce72011-11-22 17:21:55 -08002736 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01002737 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Lin47a1ce72011-11-22 17:21:55 -08002738 struct brcmf_sdio *bus = sdiodev->bus;
Franky Lin6bc52312013-08-10 12:27:28 +02002739 struct brcmf_sdio_hdrinfo hd_info = {0};
Arend van Spriel5b435de2011-10-05 13:19:03 +02002740
2741 brcmf_dbg(TRACE, "Enter\n");
2742
2743 /* Back the pointer to make a room for bus header */
Franky Lin706478c2013-08-10 12:27:29 +02002744 frame = msg - bus->tx_hdrlen;
2745 len = (msglen += bus->tx_hdrlen);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002746
2747 /* Add alignment padding (optional for ctl frames) */
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01002748 doff = ((unsigned long)frame % bus->head_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002749 if (doff) {
2750 frame -= doff;
2751 len += doff;
2752 msglen += doff;
Franky Lin706478c2013-08-10 12:27:29 +02002753 memset(frame, 0, doff + bus->tx_hdrlen);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002754 }
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01002755 /* precondition: doff < bus->head_align */
Franky Lin706478c2013-08-10 12:27:29 +02002756 doff += bus->tx_hdrlen;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002757
2758 /* Round send length to next SDIO block */
Franky Lin8da9d2c2013-11-29 12:25:10 +01002759 pad = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002760 if (bus->roundup && bus->blocksize && (len > bus->blocksize)) {
Franky Lin8da9d2c2013-11-29 12:25:10 +01002761 pad = bus->blocksize - (len % bus->blocksize);
2762 if ((pad > bus->roundup) || (pad >= bus->blocksize))
2763 pad = 0;
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01002764 } else if (len % bus->head_align) {
2765 pad = bus->head_align - (len % bus->head_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002766 }
Franky Lin8da9d2c2013-11-29 12:25:10 +01002767 len += pad;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002768
Arend van Spriel5b435de2011-10-05 13:19:03 +02002769 /* precondition: IS_ALIGNED((unsigned long)frame, 2) */
2770
Arend van Spriel5b435de2011-10-05 13:19:03 +02002771 /* Make sure backplane clock is on */
Franky Lin38b0b0d2012-11-05 16:22:24 -08002772 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002773 brcmf_sdio_bus_sleep(bus, false, false);
Franky Lin38b0b0d2012-11-05 16:22:24 -08002774 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002775
Franky Lin6bc52312013-08-10 12:27:28 +02002776 hd_info.len = (u16)msglen;
2777 hd_info.channel = SDPCM_CONTROL_CHANNEL;
2778 hd_info.dat_offset = doff;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002779 hd_info.seq_num = bus->tx_seq;
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01002780 hd_info.lastfrm = true;
2781 hd_info.tail_pad = pad;
Franky Lin6bc52312013-08-10 12:27:28 +02002782 brcmf_sdio_hdpack(bus, frame, &hd_info);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002783
Franky Lin8da9d2c2013-11-29 12:25:10 +01002784 if (bus->txglom)
2785 brcmf_sdio_update_hwhdr(frame, len);
2786
Arend van Spriel5b435de2011-10-05 13:19:03 +02002787 if (!data_ok(bus)) {
2788 brcmf_dbg(INFO, "No bus credit bus->tx_max %d, bus->tx_seq %d\n",
2789 bus->tx_max, bus->tx_seq);
2790 bus->ctrl_frame_stat = true;
2791 /* Send from dpc */
2792 bus->ctrl_frame_buf = frame;
2793 bus->ctrl_frame_len = len;
2794
Franky Linfd67dc82012-11-05 16:22:25 -08002795 wait_event_interruptible_timeout(bus->ctrl_wait,
2796 !bus->ctrl_frame_stat,
2797 msecs_to_jiffies(2000));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002798
Joe Perches23677ce2012-02-09 11:17:23 +00002799 if (!bus->ctrl_frame_stat) {
Arend van Sprielc3203372013-04-03 12:40:44 +02002800 brcmf_dbg(SDIO, "ctrl_frame_stat == false\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002801 ret = 0;
2802 } else {
Arend van Sprielc3203372013-04-03 12:40:44 +02002803 brcmf_dbg(SDIO, "ctrl_frame_stat == true\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002804 ret = -1;
2805 }
2806 }
2807
2808 if (ret == -1) {
Joe Perches1e023822012-01-15 00:38:40 -08002809 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_CTL_ON(),
2810 frame, len, "Tx Frame:\n");
2811 brcmf_dbg_hex_dump(!(BRCMF_BYTES_ON() && BRCMF_CTL_ON()) &&
2812 BRCMF_HDRS_ON(),
2813 frame, min_t(u16, len, 16), "TxHdr:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002814
2815 do {
Franky Lin38b0b0d2012-11-05 16:22:24 -08002816 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002817 ret = brcmf_sdio_tx_frame(bus, frame, len);
Franky Lin38b0b0d2012-11-05 16:22:24 -08002818 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002819 } while (ret < 0 && retries++ < TXRETRIES);
2820 }
2821
Franky Linf1e68c22012-09-13 21:12:00 +02002822 if ((bus->idletime == BRCMF_IDLE_IMMEDIATE) &&
Arend van Sprielfccfe932013-06-18 13:29:26 +02002823 atomic_read(&bus->dpc_tskcnt) == 0) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02002824 bus->activity = false;
Franky Lin38b0b0d2012-11-05 16:22:24 -08002825 sdio_claim_host(bus->sdiodev->func[1]);
Piotr Haber4a3da992013-04-11 13:28:46 +02002826 brcmf_dbg(INFO, "idle\n");
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002827 brcmf_sdio_clkctl(bus, CLK_NONE, true);
Franky Lin38b0b0d2012-11-05 16:22:24 -08002828 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002829 }
2830
Arend van Spriel5b435de2011-10-05 13:19:03 +02002831 if (ret)
Arend van Spriel80969832012-06-09 22:51:44 +02002832 bus->sdcnt.tx_ctlerrs++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002833 else
Arend van Spriel80969832012-06-09 22:51:44 +02002834 bus->sdcnt.tx_ctlpkts++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002835
2836 return ret ? -EIO : 0;
2837}
2838
Arend van Spriel80969832012-06-09 22:51:44 +02002839#ifdef DEBUG
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002840static inline bool brcmf_sdio_valid_shared_address(u32 addr)
2841{
2842 return !(addr == 0 || ((~addr >> 16) & 0xffff) == (addr & 0xffff));
2843}
2844
2845static int brcmf_sdio_readshared(struct brcmf_sdio *bus,
2846 struct sdpcm_shared *sh)
2847{
2848 u32 addr;
2849 int rv;
2850 u32 shaddr = 0;
2851 struct sdpcm_shared_le sh_le;
2852 __le32 addr_le;
2853
Franky Lin1640f282013-04-11 13:28:51 +02002854 shaddr = bus->ci->rambase + bus->ramsize - 4;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002855
2856 /*
2857 * Read last word in socram to determine
2858 * address of sdpcm_shared structure
2859 */
Franky Lin38b0b0d2012-11-05 16:22:24 -08002860 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002861 brcmf_sdio_bus_sleep(bus, false, false);
Arend van Spriela39be272013-12-12 11:58:58 +01002862 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, shaddr, (u8 *)&addr_le, 4);
Piotr Haberb55de972013-02-08 12:06:33 +01002863 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002864 if (rv < 0)
2865 return rv;
2866
2867 addr = le32_to_cpu(addr_le);
2868
Arend van Sprielc3203372013-04-03 12:40:44 +02002869 brcmf_dbg(SDIO, "sdpcm_shared address 0x%08X\n", addr);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002870
2871 /*
2872 * Check if addr is valid.
2873 * NVRAM length at the end of memory should have been overwritten.
2874 */
2875 if (!brcmf_sdio_valid_shared_address(addr)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002876 brcmf_err("invalid sdpcm_shared address 0x%08X\n",
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002877 addr);
2878 return -EINVAL;
2879 }
2880
2881 /* Read hndrte_shared structure */
Arend van Spriela39be272013-12-12 11:58:58 +01002882 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, (u8 *)&sh_le,
2883 sizeof(struct sdpcm_shared_le));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002884 if (rv < 0)
2885 return rv;
2886
2887 /* Endianness */
2888 sh->flags = le32_to_cpu(sh_le.flags);
2889 sh->trap_addr = le32_to_cpu(sh_le.trap_addr);
2890 sh->assert_exp_addr = le32_to_cpu(sh_le.assert_exp_addr);
2891 sh->assert_file_addr = le32_to_cpu(sh_le.assert_file_addr);
2892 sh->assert_line = le32_to_cpu(sh_le.assert_line);
2893 sh->console_addr = le32_to_cpu(sh_le.console_addr);
2894 sh->msgtrace_addr = le32_to_cpu(sh_le.msgtrace_addr);
2895
Piotr Haber86dcd932013-04-05 10:57:43 +02002896 if ((sh->flags & SDPCM_SHARED_VERSION_MASK) > SDPCM_SHARED_VERSION) {
2897 brcmf_err("sdpcm shared version unsupported: dhd %d dongle %d\n",
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002898 SDPCM_SHARED_VERSION,
2899 sh->flags & SDPCM_SHARED_VERSION_MASK);
2900 return -EPROTO;
2901 }
2902
2903 return 0;
2904}
2905
2906static int brcmf_sdio_dump_console(struct brcmf_sdio *bus,
2907 struct sdpcm_shared *sh, char __user *data,
2908 size_t count)
2909{
2910 u32 addr, console_ptr, console_size, console_index;
2911 char *conbuf = NULL;
2912 __le32 sh_val;
2913 int rv;
2914 loff_t pos = 0;
2915 int nbytes = 0;
2916
2917 /* obtain console information from device memory */
2918 addr = sh->console_addr + offsetof(struct rte_console, log_le);
Arend van Spriela39be272013-12-12 11:58:58 +01002919 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
2920 (u8 *)&sh_val, sizeof(u32));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002921 if (rv < 0)
2922 return rv;
2923 console_ptr = le32_to_cpu(sh_val);
2924
2925 addr = sh->console_addr + offsetof(struct rte_console, log_le.buf_size);
Arend van Spriela39be272013-12-12 11:58:58 +01002926 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
2927 (u8 *)&sh_val, sizeof(u32));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002928 if (rv < 0)
2929 return rv;
2930 console_size = le32_to_cpu(sh_val);
2931
2932 addr = sh->console_addr + offsetof(struct rte_console, log_le.idx);
Arend van Spriela39be272013-12-12 11:58:58 +01002933 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
2934 (u8 *)&sh_val, sizeof(u32));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002935 if (rv < 0)
2936 return rv;
2937 console_index = le32_to_cpu(sh_val);
2938
2939 /* allocate buffer for console data */
2940 if (console_size <= CONSOLE_BUFFER_MAX)
2941 conbuf = vzalloc(console_size+1);
2942
2943 if (!conbuf)
2944 return -ENOMEM;
2945
2946 /* obtain the console data from device */
2947 conbuf[console_size] = '\0';
Arend van Spriela39be272013-12-12 11:58:58 +01002948 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, console_ptr, (u8 *)conbuf,
2949 console_size);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002950 if (rv < 0)
2951 goto done;
2952
2953 rv = simple_read_from_buffer(data, count, &pos,
2954 conbuf + console_index,
2955 console_size - console_index);
2956 if (rv < 0)
2957 goto done;
2958
2959 nbytes = rv;
2960 if (console_index > 0) {
2961 pos = 0;
2962 rv = simple_read_from_buffer(data+nbytes, count, &pos,
2963 conbuf, console_index - 1);
2964 if (rv < 0)
2965 goto done;
2966 rv += nbytes;
2967 }
2968done:
2969 vfree(conbuf);
2970 return rv;
2971}
2972
2973static int brcmf_sdio_trap_info(struct brcmf_sdio *bus, struct sdpcm_shared *sh,
2974 char __user *data, size_t count)
2975{
2976 int error, res;
2977 char buf[350];
2978 struct brcmf_trap_info tr;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002979 loff_t pos = 0;
2980
Piotr Haberbaa9e602013-04-05 10:57:42 +02002981 if ((sh->flags & SDPCM_SHARED_TRAP) == 0) {
2982 brcmf_dbg(INFO, "no trap in firmware\n");
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002983 return 0;
Piotr Haberbaa9e602013-04-05 10:57:42 +02002984 }
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002985
Arend van Spriela39be272013-12-12 11:58:58 +01002986 error = brcmf_sdiod_ramrw(bus->sdiodev, false, sh->trap_addr, (u8 *)&tr,
2987 sizeof(struct brcmf_trap_info));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002988 if (error < 0)
2989 return error;
2990
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002991 res = scnprintf(buf, sizeof(buf),
2992 "dongle trap info: type 0x%x @ epc 0x%08x\n"
2993 " cpsr 0x%08x spsr 0x%08x sp 0x%08x\n"
2994 " lr 0x%08x pc 0x%08x offset 0x%x\n"
2995 " r0 0x%08x r1 0x%08x r2 0x%08x r3 0x%08x\n"
2996 " r4 0x%08x r5 0x%08x r6 0x%08x r7 0x%08x\n",
2997 le32_to_cpu(tr.type), le32_to_cpu(tr.epc),
2998 le32_to_cpu(tr.cpsr), le32_to_cpu(tr.spsr),
2999 le32_to_cpu(tr.r13), le32_to_cpu(tr.r14),
Arend van Spriel9bd02c62012-06-26 21:26:40 +02003000 le32_to_cpu(tr.pc), sh->trap_addr,
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003001 le32_to_cpu(tr.r0), le32_to_cpu(tr.r1),
3002 le32_to_cpu(tr.r2), le32_to_cpu(tr.r3),
3003 le32_to_cpu(tr.r4), le32_to_cpu(tr.r5),
3004 le32_to_cpu(tr.r6), le32_to_cpu(tr.r7));
3005
Piotr Haberbaa9e602013-04-05 10:57:42 +02003006 return simple_read_from_buffer(data, count, &pos, buf, res);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003007}
3008
3009static int brcmf_sdio_assert_info(struct brcmf_sdio *bus,
3010 struct sdpcm_shared *sh, char __user *data,
3011 size_t count)
3012{
3013 int error = 0;
3014 char buf[200];
3015 char file[80] = "?";
3016 char expr[80] = "<???>";
3017 int res;
3018 loff_t pos = 0;
3019
3020 if ((sh->flags & SDPCM_SHARED_ASSERT_BUILT) == 0) {
3021 brcmf_dbg(INFO, "firmware not built with -assert\n");
3022 return 0;
3023 } else if ((sh->flags & SDPCM_SHARED_ASSERT) == 0) {
3024 brcmf_dbg(INFO, "no assert in dongle\n");
3025 return 0;
3026 }
3027
Franky Lin38b0b0d2012-11-05 16:22:24 -08003028 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003029 if (sh->assert_file_addr != 0) {
Arend van Spriela39be272013-12-12 11:58:58 +01003030 error = brcmf_sdiod_ramrw(bus->sdiodev, false,
3031 sh->assert_file_addr, (u8 *)file, 80);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003032 if (error < 0)
3033 return error;
3034 }
3035 if (sh->assert_exp_addr != 0) {
Arend van Spriela39be272013-12-12 11:58:58 +01003036 error = brcmf_sdiod_ramrw(bus->sdiodev, false,
3037 sh->assert_exp_addr, (u8 *)expr, 80);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003038 if (error < 0)
3039 return error;
3040 }
Franky Lin38b0b0d2012-11-05 16:22:24 -08003041 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003042
3043 res = scnprintf(buf, sizeof(buf),
3044 "dongle assert: %s:%d: assert(%s)\n",
3045 file, sh->assert_line, expr);
3046 return simple_read_from_buffer(data, count, &pos, buf, res);
3047}
3048
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003049static int brcmf_sdio_checkdied(struct brcmf_sdio *bus)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003050{
3051 int error;
3052 struct sdpcm_shared sh;
3053
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003054 error = brcmf_sdio_readshared(bus, &sh);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003055
3056 if (error < 0)
3057 return error;
3058
3059 if ((sh.flags & SDPCM_SHARED_ASSERT_BUILT) == 0)
3060 brcmf_dbg(INFO, "firmware not built with -assert\n");
3061 else if (sh.flags & SDPCM_SHARED_ASSERT)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003062 brcmf_err("assertion in dongle\n");
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003063
3064 if (sh.flags & SDPCM_SHARED_TRAP)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003065 brcmf_err("firmware trap in dongle\n");
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003066
3067 return 0;
3068}
3069
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003070static int brcmf_sdio_died_dump(struct brcmf_sdio *bus, char __user *data,
3071 size_t count, loff_t *ppos)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003072{
3073 int error = 0;
3074 struct sdpcm_shared sh;
3075 int nbytes = 0;
3076 loff_t pos = *ppos;
3077
3078 if (pos != 0)
3079 return 0;
3080
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003081 error = brcmf_sdio_readshared(bus, &sh);
3082 if (error < 0)
3083 goto done;
3084
3085 error = brcmf_sdio_assert_info(bus, &sh, data, count);
3086 if (error < 0)
3087 goto done;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003088 nbytes = error;
Piotr Haberbaa9e602013-04-05 10:57:42 +02003089
3090 error = brcmf_sdio_trap_info(bus, &sh, data+nbytes, count);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003091 if (error < 0)
3092 goto done;
Piotr Haberbaa9e602013-04-05 10:57:42 +02003093 nbytes += error;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003094
Piotr Haberbaa9e602013-04-05 10:57:42 +02003095 error = brcmf_sdio_dump_console(bus, &sh, data+nbytes, count);
3096 if (error < 0)
3097 goto done;
3098 nbytes += error;
3099
3100 error = nbytes;
3101 *ppos += nbytes;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003102done:
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003103 return error;
3104}
3105
3106static ssize_t brcmf_sdio_forensic_read(struct file *f, char __user *data,
3107 size_t count, loff_t *ppos)
3108{
3109 struct brcmf_sdio *bus = f->private_data;
3110 int res;
3111
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003112 res = brcmf_sdio_died_dump(bus, data, count, ppos);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003113 if (res > 0)
3114 *ppos += res;
3115 return (ssize_t)res;
3116}
3117
3118static const struct file_operations brcmf_sdio_forensic_ops = {
3119 .owner = THIS_MODULE,
3120 .open = simple_open,
3121 .read = brcmf_sdio_forensic_read
3122};
3123
Arend van Spriel80969832012-06-09 22:51:44 +02003124static void brcmf_sdio_debugfs_create(struct brcmf_sdio *bus)
3125{
3126 struct brcmf_pub *drvr = bus->sdiodev->bus_if->drvr;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003127 struct dentry *dentry = brcmf_debugfs_get_devdir(drvr);
Arend van Spriel80969832012-06-09 22:51:44 +02003128
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003129 if (IS_ERR_OR_NULL(dentry))
3130 return;
3131
3132 debugfs_create_file("forensics", S_IRUGO, dentry, bus,
3133 &brcmf_sdio_forensic_ops);
Arend van Spriel80969832012-06-09 22:51:44 +02003134 brcmf_debugfs_create_sdio_count(drvr, &bus->sdcnt);
3135}
3136#else
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003137static int brcmf_sdio_checkdied(struct brcmf_sdio *bus)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003138{
3139 return 0;
3140}
3141
Arend van Spriel80969832012-06-09 22:51:44 +02003142static void brcmf_sdio_debugfs_create(struct brcmf_sdio *bus)
3143{
3144}
3145#endif /* DEBUG */
3146
Franky Linfcf094f2011-12-16 18:37:16 -08003147static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003148brcmf_sdio_bus_rxctl(struct device *dev, unsigned char *msg, uint msglen)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003149{
3150 int timeleft;
3151 uint rxlen = 0;
3152 bool pending;
Franky Lindd43a012012-11-05 16:22:22 -08003153 u8 *buf;
Franky Lin532cdd32011-11-22 17:21:54 -08003154 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01003155 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Lin532cdd32011-11-22 17:21:54 -08003156 struct brcmf_sdio *bus = sdiodev->bus;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003157
3158 brcmf_dbg(TRACE, "Enter\n");
3159
3160 /* Wait until control frame is available */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003161 timeleft = brcmf_sdio_dcmd_resp_wait(bus, &bus->rxlen, &pending);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003162
Franky Lindd43a012012-11-05 16:22:22 -08003163 spin_lock_bh(&bus->rxctl_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003164 rxlen = bus->rxlen;
3165 memcpy(msg, bus->rxctl, min(msglen, rxlen));
Franky Lindd43a012012-11-05 16:22:22 -08003166 bus->rxctl = NULL;
3167 buf = bus->rxctl_orig;
3168 bus->rxctl_orig = NULL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003169 bus->rxlen = 0;
Franky Lindd43a012012-11-05 16:22:22 -08003170 spin_unlock_bh(&bus->rxctl_lock);
3171 vfree(buf);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003172
3173 if (rxlen) {
3174 brcmf_dbg(CTL, "resumed on rxctl frame, got %d expected %d\n",
3175 rxlen, msglen);
3176 } else if (timeleft == 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003177 brcmf_err("resumed on timeout\n");
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003178 brcmf_sdio_checkdied(bus);
Joe Perches23677ce2012-02-09 11:17:23 +00003179 } else if (pending) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003180 brcmf_dbg(CTL, "cancelled\n");
3181 return -ERESTARTSYS;
3182 } else {
3183 brcmf_dbg(CTL, "resumed for unknown reason?\n");
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003184 brcmf_sdio_checkdied(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003185 }
3186
3187 if (rxlen)
Arend van Spriel80969832012-06-09 22:51:44 +02003188 bus->sdcnt.rx_ctlpkts++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003189 else
Arend van Spriel80969832012-06-09 22:51:44 +02003190 bus->sdcnt.rx_ctlerrs++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003191
3192 return rxlen ? (int)rxlen : -ETIMEDOUT;
3193}
3194
Hante Meulemana74d0362014-01-13 22:20:22 +01003195#ifdef DEBUG
3196static bool
3197brcmf_sdio_verifymemory(struct brcmf_sdio_dev *sdiodev, u32 ram_addr,
3198 u8 *ram_data, uint ram_sz)
3199{
3200 char *ram_cmp;
3201 int err;
3202 bool ret = true;
3203 int address;
3204 int offset;
3205 int len;
3206
3207 /* read back and verify */
3208 brcmf_dbg(INFO, "Compare RAM dl & ul at 0x%08x; size=%d\n", ram_addr,
3209 ram_sz);
3210 ram_cmp = kmalloc(MEMBLOCK, GFP_KERNEL);
3211 /* do not proceed while no memory but */
3212 if (!ram_cmp)
3213 return true;
3214
3215 address = ram_addr;
3216 offset = 0;
3217 while (offset < ram_sz) {
3218 len = ((offset + MEMBLOCK) < ram_sz) ? MEMBLOCK :
3219 ram_sz - offset;
3220 err = brcmf_sdiod_ramrw(sdiodev, false, address, ram_cmp, len);
3221 if (err) {
3222 brcmf_err("error %d on reading %d membytes at 0x%08x\n",
3223 err, len, address);
3224 ret = false;
3225 break;
3226 } else if (memcmp(ram_cmp, &ram_data[offset], len)) {
3227 brcmf_err("Downloaded RAM image is corrupted, block offset is %d, len is %d\n",
3228 offset, len);
3229 ret = false;
3230 break;
3231 }
3232 offset += len;
3233 address += len;
3234 }
3235
3236 kfree(ram_cmp);
3237
3238 return ret;
3239}
3240#else /* DEBUG */
3241static bool
3242brcmf_sdio_verifymemory(struct brcmf_sdio_dev *sdiodev, u32 ram_addr,
3243 u8 *ram_data, uint ram_sz)
3244{
3245 return true;
3246}
3247#endif /* DEBUG */
3248
Arend van Spriel33556502014-01-13 22:20:25 +01003249static int brcmf_sdio_download_code_file(struct brcmf_sdio *bus,
3250 const struct firmware *fw)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003251{
Hante Meulemanf2c44fe2013-09-25 13:05:45 +02003252 int err;
Franky Lin1640f282013-04-11 13:28:51 +02003253 int offset;
Hante Meulemanf2c44fe2013-09-25 13:05:45 +02003254 int address;
3255 int len;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003256
Hante Meulemana74d0362014-01-13 22:20:22 +01003257 brcmf_dbg(TRACE, "Enter\n");
3258
Hante Meulemanf2c44fe2013-09-25 13:05:45 +02003259 err = 0;
3260 offset = 0;
3261 address = bus->ci->rambase;
3262 while (offset < fw->size) {
3263 len = ((offset + MEMBLOCK) < fw->size) ? MEMBLOCK :
3264 fw->size - offset;
Arend van Spriela39be272013-12-12 11:58:58 +01003265 err = brcmf_sdiod_ramrw(bus->sdiodev, true, address,
3266 (u8 *)&fw->data[offset], len);
Hante Meulemanf2c44fe2013-09-25 13:05:45 +02003267 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003268 brcmf_err("error %d on writing %d membytes at 0x%08x\n",
Hante Meulemanf2c44fe2013-09-25 13:05:45 +02003269 err, len, address);
Arend van Spriel33556502014-01-13 22:20:25 +01003270 return err;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003271 }
Hante Meulemanf2c44fe2013-09-25 13:05:45 +02003272 offset += len;
3273 address += len;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003274 }
Hante Meulemana74d0362014-01-13 22:20:22 +01003275 if (!err)
3276 if (!brcmf_sdio_verifymemory(bus->sdiodev, bus->ci->rambase,
3277 (u8 *)fw->data, fw->size))
3278 err = -EIO;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003279
Hante Meulemanf2c44fe2013-09-25 13:05:45 +02003280 return err;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003281}
3282
Arend van Spriel33556502014-01-13 22:20:25 +01003283static int brcmf_sdio_download_nvram(struct brcmf_sdio *bus,
3284 const struct firmware *nv)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003285{
Hante Meulemana74d0362014-01-13 22:20:22 +01003286 void *vars;
3287 u32 varsz;
3288 int address;
3289 int err;
3290
3291 brcmf_dbg(TRACE, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003292
Hante Meulemana74d0362014-01-13 22:20:22 +01003293 vars = brcmf_nvram_strip(nv, &varsz);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003294
Hante Meulemana74d0362014-01-13 22:20:22 +01003295 if (vars == NULL)
3296 return -EINVAL;
3297
3298 address = bus->ci->ramsize - varsz + bus->ci->rambase;
3299 err = brcmf_sdiod_ramrw(bus->sdiodev, true, address, vars, varsz);
3300 if (err)
3301 brcmf_err("error %d on writing %d nvram bytes at 0x%08x\n",
3302 err, varsz, address);
3303 else if (!brcmf_sdio_verifymemory(bus->sdiodev, address, vars, varsz))
3304 err = -EIO;
3305
3306 brcmf_nvram_free(vars);
3307
3308 return err;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003309}
3310
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003311static int brcmf_sdio_download_firmware(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003312{
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003313 int bcmerror = -EFAULT;
Arend van Spriel33556502014-01-13 22:20:25 +01003314 const struct firmware *fw;
3315 u32 rstvec;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003316
3317 sdio_claim_host(bus->sdiodev->func[1]);
3318 brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003319
3320 /* Keep arm in reset */
Arend van Spriel33556502014-01-13 22:20:25 +01003321 brcmf_sdio_chip_enter_download(bus->sdiodev, bus->ci);
3322
3323 fw = brcmf_sdio_get_fw(bus, BRCMF_FIRMWARE_BIN);
3324 if (fw == NULL) {
3325 bcmerror = -ENOENT;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003326 goto err;
3327 }
3328
Arend van Spriel33556502014-01-13 22:20:25 +01003329 rstvec = get_unaligned_le32(fw->data);
3330 brcmf_dbg(SDIO, "firmware rstvec: %x\n", rstvec);
3331
3332 bcmerror = brcmf_sdio_download_code_file(bus, fw);
3333 release_firmware(fw);
3334 if (bcmerror) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003335 brcmf_err("dongle image file download failed\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003336 goto err;
3337 }
3338
Arend van Spriel33556502014-01-13 22:20:25 +01003339 fw = brcmf_sdio_get_fw(bus, BRCMF_FIRMWARE_NVRAM);
3340 if (fw == NULL) {
3341 bcmerror = -ENOENT;
3342 goto err;
3343 }
3344
3345 bcmerror = brcmf_sdio_download_nvram(bus, fw);
3346 release_firmware(fw);
3347 if (bcmerror) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003348 brcmf_err("dongle nvram file download failed\n");
Franky Lin3eaa9562013-04-02 21:06:20 +02003349 goto err;
3350 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02003351
3352 /* Take arm out of reset */
Arend van Spriel33556502014-01-13 22:20:25 +01003353 if (!brcmf_sdio_chip_exit_download(bus->sdiodev, bus->ci, rstvec)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003354 brcmf_err("error getting out of ARM core reset\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003355 goto err;
3356 }
3357
Arend van Spriel33556502014-01-13 22:20:25 +01003358 /* Allow HT Clock now that the ARM is running. */
3359 bus->sdiodev->bus_if->state = BRCMF_BUS_LOAD;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003360 bcmerror = 0;
3361
3362err:
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003363 brcmf_sdio_clkctl(bus, CLK_SDONLY, false);
3364 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003365 return bcmerror;
3366}
3367
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003368static bool brcmf_sdio_sr_capable(struct brcmf_sdio *bus)
Piotr Haber4a3da992013-04-11 13:28:46 +02003369{
Arend van Spriel3bd44d92014-01-06 12:40:43 +01003370 u32 addr, reg, pmu_cc3_mask = ~0;
3371 int err;
Piotr Haber4a3da992013-04-11 13:28:46 +02003372
3373 brcmf_dbg(TRACE, "Enter\n");
3374
3375 /* old chips with PMU version less than 17 don't support save restore */
3376 if (bus->ci->pmurev < 17)
3377 return false;
3378
Arend van Spriel3bd44d92014-01-06 12:40:43 +01003379 switch (bus->ci->chip) {
3380 case BCM43241_CHIP_ID:
3381 case BCM4335_CHIP_ID:
3382 case BCM4339_CHIP_ID:
3383 /* read PMU chipcontrol register 3 */
3384 addr = CORE_CC_REG(bus->ci->c_inf[0].base, chipcontrol_addr);
3385 brcmf_sdiod_regwl(bus->sdiodev, addr, 3, NULL);
3386 addr = CORE_CC_REG(bus->ci->c_inf[0].base, chipcontrol_data);
3387 reg = brcmf_sdiod_regrl(bus->sdiodev, addr, NULL);
3388 return (reg & pmu_cc3_mask) != 0;
3389 default:
3390 addr = CORE_CC_REG(bus->ci->c_inf[0].base, pmucapabilities_ext);
3391 reg = brcmf_sdiod_regrl(bus->sdiodev, addr, &err);
3392 if ((reg & PCAPEXT_SR_SUPPORTED_MASK) == 0)
3393 return false;
Piotr Haber4a3da992013-04-11 13:28:46 +02003394
Arend van Spriel3bd44d92014-01-06 12:40:43 +01003395 addr = CORE_CC_REG(bus->ci->c_inf[0].base, retention_ctl);
3396 reg = brcmf_sdiod_regrl(bus->sdiodev, addr, NULL);
3397 return (reg & (PMU_RCTL_MACPHY_DISABLE_MASK |
3398 PMU_RCTL_LOGIC_DISABLE_MASK)) == 0;
3399 }
Piotr Haber4a3da992013-04-11 13:28:46 +02003400}
3401
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003402static void brcmf_sdio_sr_init(struct brcmf_sdio *bus)
Piotr Haber4a3da992013-04-11 13:28:46 +02003403{
3404 int err = 0;
3405 u8 val;
3406
3407 brcmf_dbg(TRACE, "Enter\n");
3408
Arend van Spriela39be272013-12-12 11:58:58 +01003409 val = brcmf_sdiod_regrb(bus->sdiodev, SBSDIO_FUNC1_WAKEUPCTRL, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003410 if (err) {
3411 brcmf_err("error reading SBSDIO_FUNC1_WAKEUPCTRL\n");
3412 return;
3413 }
3414
3415 val |= 1 << SBSDIO_FUNC1_WCTRL_HTWAIT_SHIFT;
Arend van Spriela39be272013-12-12 11:58:58 +01003416 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_WAKEUPCTRL, val, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003417 if (err) {
3418 brcmf_err("error writing SBSDIO_FUNC1_WAKEUPCTRL\n");
3419 return;
3420 }
3421
3422 /* Add CMD14 Support */
Arend van Spriela39be272013-12-12 11:58:58 +01003423 brcmf_sdiod_regwb(bus->sdiodev, SDIO_CCCR_BRCM_CARDCAP,
3424 (SDIO_CCCR_BRCM_CARDCAP_CMD14_SUPPORT |
3425 SDIO_CCCR_BRCM_CARDCAP_CMD14_EXT),
3426 &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003427 if (err) {
3428 brcmf_err("error writing SDIO_CCCR_BRCM_CARDCAP\n");
3429 return;
3430 }
3431
Arend van Spriela39be272013-12-12 11:58:58 +01003432 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3433 SBSDIO_FORCE_HT, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003434 if (err) {
3435 brcmf_err("error writing SBSDIO_FUNC1_CHIPCLKCSR\n");
3436 return;
3437 }
3438
3439 /* set flag */
3440 bus->sr_enabled = true;
3441 brcmf_dbg(INFO, "SR enabled\n");
3442}
3443
3444/* enable KSO bit */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003445static int brcmf_sdio_kso_init(struct brcmf_sdio *bus)
Piotr Haber4a3da992013-04-11 13:28:46 +02003446{
3447 u8 val;
3448 int err = 0;
3449
3450 brcmf_dbg(TRACE, "Enter\n");
3451
3452 /* KSO bit added in SDIO core rev 12 */
3453 if (bus->ci->c_inf[1].rev < 12)
3454 return 0;
3455
Arend van Spriela39be272013-12-12 11:58:58 +01003456 val = brcmf_sdiod_regrb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003457 if (err) {
3458 brcmf_err("error reading SBSDIO_FUNC1_SLEEPCSR\n");
3459 return err;
3460 }
3461
3462 if (!(val & SBSDIO_FUNC1_SLEEPCSR_KSO_MASK)) {
3463 val |= (SBSDIO_FUNC1_SLEEPCSR_KSO_EN <<
3464 SBSDIO_FUNC1_SLEEPCSR_KSO_SHIFT);
Arend van Spriela39be272013-12-12 11:58:58 +01003465 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
3466 val, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003467 if (err) {
3468 brcmf_err("error writing SBSDIO_FUNC1_SLEEPCSR\n");
3469 return err;
3470 }
3471 }
3472
3473 return 0;
3474}
3475
3476
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003477static int brcmf_sdio_bus_preinit(struct device *dev)
Arend van Sprielcf458282013-11-29 11:48:15 +01003478{
3479 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3480 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3481 struct brcmf_sdio *bus = sdiodev->bus;
Franky Lin8da9d2c2013-11-29 12:25:10 +01003482 uint pad_size;
Arend van Sprielcf458282013-11-29 11:48:15 +01003483 u32 value;
3484 u8 idx;
3485 int err;
3486
Franky Lin8da9d2c2013-11-29 12:25:10 +01003487 /* the commands below use the terms tx and rx from
3488 * a device perspective, ie. bus:txglom affects the
3489 * bus transfers from device to host.
3490 */
Arend van Sprielcf458282013-11-29 11:48:15 +01003491 idx = brcmf_sdio_chip_getinfidx(bus->ci, BCMA_CORE_SDIO_DEV);
3492 if (bus->ci->c_inf[idx].rev < 12) {
3493 /* for sdio core rev < 12, disable txgloming */
3494 value = 0;
3495 err = brcmf_iovar_data_set(dev, "bus:txglom", &value,
3496 sizeof(u32));
3497 } else {
3498 /* otherwise, set txglomalign */
3499 value = 4;
3500 if (sdiodev->pdata)
3501 value = sdiodev->pdata->sd_sgentry_align;
3502 /* SDIO ADMA requires at least 32 bit alignment */
3503 value = max_t(u32, value, 4);
3504 err = brcmf_iovar_data_set(dev, "bus:txglomalign", &value,
3505 sizeof(u32));
3506 }
Franky Lin8da9d2c2013-11-29 12:25:10 +01003507
3508 if (err < 0)
3509 goto done;
3510
3511 bus->tx_hdrlen = SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN;
3512 if (sdiodev->sg_support) {
3513 bus->txglom = false;
3514 value = 1;
3515 pad_size = bus->sdiodev->func[2]->cur_blksize << 1;
3516 bus->txglom_sgpad = brcmu_pkt_buf_get_skb(pad_size);
3517 if (!bus->txglom_sgpad)
3518 brcmf_err("allocating txglom padding skb failed, reduced performance\n");
3519
3520 err = brcmf_iovar_data_set(bus->sdiodev->dev, "bus:rxglom",
3521 &value, sizeof(u32));
3522 if (err < 0) {
3523 /* bus:rxglom is allowed to fail */
3524 err = 0;
3525 } else {
3526 bus->txglom = true;
3527 bus->tx_hdrlen += SDPCM_HWEXT_LEN;
3528 }
3529 }
3530 brcmf_bus_add_txhdrlen(bus->sdiodev->dev, bus->tx_hdrlen);
3531
3532done:
Arend van Sprielcf458282013-11-29 11:48:15 +01003533 return err;
3534}
3535
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003536static int brcmf_sdio_bus_init(struct device *dev)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003537{
Franky Linfa20b912011-11-22 17:21:57 -08003538 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01003539 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Linfa20b912011-11-22 17:21:57 -08003540 struct brcmf_sdio *bus = sdiodev->bus;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003541 int err, ret = 0;
3542 u8 saveclk;
3543
3544 brcmf_dbg(TRACE, "Enter\n");
3545
3546 /* try to download image and nvram to the dongle */
Franky Linfa20b912011-11-22 17:21:57 -08003547 if (bus_if->state == BRCMF_BUS_DOWN) {
Arend van Spriel33556502014-01-13 22:20:25 +01003548 bus->alp_only = true;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003549 err = brcmf_sdio_download_firmware(bus);
3550 if (err)
3551 return err;
Arend van Spriel33556502014-01-13 22:20:25 +01003552 bus->alp_only = false;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003553 }
3554
Franky Lin712ac5b2011-12-16 18:37:09 -08003555 if (!bus->sdiodev->bus_if->drvr)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003556 return 0;
3557
3558 /* Start the watchdog timer */
Arend van Spriel80969832012-06-09 22:51:44 +02003559 bus->sdcnt.tickcnt = 0;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003560 brcmf_sdio_wd_timer(bus, BRCMF_WD_POLL_MS);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003561
Franky Lin38b0b0d2012-11-05 16:22:24 -08003562 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003563
3564 /* Make sure backplane clock is on, needed to generate F2 interrupt */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003565 brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003566 if (bus->clkstate != CLK_AVAIL)
3567 goto exit;
3568
3569 /* Force clocks on backplane to be sure F2 interrupt propagates */
Arend van Spriela39be272013-12-12 11:58:58 +01003570 saveclk = brcmf_sdiod_regrb(bus->sdiodev,
3571 SBSDIO_FUNC1_CHIPCLKCSR, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003572 if (!err) {
Arend van Spriela39be272013-12-12 11:58:58 +01003573 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3574 (saveclk | SBSDIO_FORCE_HT), &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003575 }
3576 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003577 brcmf_err("Failed to force clock for F2: err %d\n", err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003578 goto exit;
3579 }
3580
3581 /* Enable function 2 (frame transfers) */
3582 w_sdreg32(bus, SDPCM_PROT_VERSION << SMB_DATA_VERSION_SHIFT,
Franky Lin58692752012-05-04 18:27:36 -07003583 offsetof(struct sdpcmd_regs, tosbmailboxdata));
Arend van Spriel71370eb2013-12-12 11:58:53 +01003584 err = sdio_enable_func(bus->sdiodev->func[SDIO_FUNC_2]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003585
Arend van Spriel5b435de2011-10-05 13:19:03 +02003586
Arend van Spriel71370eb2013-12-12 11:58:53 +01003587 brcmf_dbg(INFO, "enable F2: err=%d\n", err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003588
3589 /* If F2 successfully enabled, set core and enable interrupts */
Arend van Spriel71370eb2013-12-12 11:58:53 +01003590 if (!err) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003591 /* Set up the interrupt mask and enable interrupts */
3592 bus->hostintmask = HOSTINTMASK;
3593 w_sdreg32(bus, bus->hostintmask,
Franky Lin58692752012-05-04 18:27:36 -07003594 offsetof(struct sdpcmd_regs, hostintmask));
Arend van Spriel5b435de2011-10-05 13:19:03 +02003595
Arend van Spriela39be272013-12-12 11:58:58 +01003596 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_WATERMARK, 8, &err);
Arend van Sprielc0e89f02012-02-09 21:09:04 +01003597 } else {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003598 /* Disable F2 again */
Arend van Spriel71370eb2013-12-12 11:58:53 +01003599 sdio_disable_func(bus->sdiodev->func[SDIO_FUNC_2]);
Arend van Sprielc0e89f02012-02-09 21:09:04 +01003600 ret = -ENODEV;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003601 }
3602
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003603 if (brcmf_sdio_sr_capable(bus)) {
3604 brcmf_sdio_sr_init(bus);
Piotr Haber4a3da992013-04-11 13:28:46 +02003605 } else {
3606 /* Restore previous clock setting */
Arend van Spriela39be272013-12-12 11:58:58 +01003607 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3608 saveclk, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003609 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02003610
Franky Line2f93cc2012-04-27 18:56:58 -07003611 if (ret == 0) {
Arend van Spriela39be272013-12-12 11:58:58 +01003612 ret = brcmf_sdiod_intr_register(bus->sdiodev);
Franky Line2f93cc2012-04-27 18:56:58 -07003613 if (ret != 0)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003614 brcmf_err("intr register failed:%d\n", ret);
Franky Line2f93cc2012-04-27 18:56:58 -07003615 }
3616
Arend van Spriel5b435de2011-10-05 13:19:03 +02003617 /* If we didn't come up, turn off backplane clock */
Arend van Spriel76a4c682014-01-06 12:40:45 +01003618 if (ret != 0)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003619 brcmf_sdio_clkctl(bus, CLK_NONE, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003620
3621exit:
Franky Lin38b0b0d2012-11-05 16:22:24 -08003622 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003623
3624 return ret;
3625}
3626
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003627void brcmf_sdio_isr(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003628{
Arend van Spriel5b435de2011-10-05 13:19:03 +02003629 brcmf_dbg(TRACE, "Enter\n");
3630
3631 if (!bus) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003632 brcmf_err("bus is null pointer, exiting\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003633 return;
3634 }
3635
Franky Lin712ac5b2011-12-16 18:37:09 -08003636 if (bus->sdiodev->bus_if->state == BRCMF_BUS_DOWN) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003637 brcmf_err("bus is down. we have nothing to do\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003638 return;
3639 }
3640 /* Count the interrupt call */
Arend van Spriel80969832012-06-09 22:51:44 +02003641 bus->sdcnt.intrcount++;
Franky Lin45316032012-09-13 21:12:03 +02003642 if (in_interrupt())
3643 atomic_set(&bus->ipend, 1);
3644 else
3645 if (brcmf_sdio_intr_rstatus(bus)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003646 brcmf_err("failed backplane access\n");
Franky Lin45316032012-09-13 21:12:03 +02003647 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
3648 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02003649
Arend van Spriel5b435de2011-10-05 13:19:03 +02003650 /* Disable additional interrupts (is this needed now)? */
3651 if (!bus->intr)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003652 brcmf_err("isr w/o interrupt configured!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003653
Arend van Sprielfccfe932013-06-18 13:29:26 +02003654 atomic_inc(&bus->dpc_tskcnt);
Franky Linf1e68c22012-09-13 21:12:00 +02003655 queue_work(bus->brcmf_wq, &bus->datawork);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003656}
3657
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003658static bool brcmf_sdio_bus_watchdog(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003659{
Joe Perches8ae74652012-01-15 00:38:38 -08003660#ifdef DEBUG
Franky Lincad2b262011-11-22 17:21:53 -08003661 struct brcmf_bus *bus_if = dev_get_drvdata(bus->sdiodev->dev);
Joe Perches8ae74652012-01-15 00:38:38 -08003662#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02003663
3664 brcmf_dbg(TIMER, "Enter\n");
3665
Arend van Spriel5b435de2011-10-05 13:19:03 +02003666 /* Poll period: check device if appropriate. */
Piotr Haber4a3da992013-04-11 13:28:46 +02003667 if (!bus->sr_enabled &&
3668 bus->poll && (++bus->polltick >= bus->pollrate)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003669 u32 intstatus = 0;
3670
3671 /* Reset poll tick */
3672 bus->polltick = 0;
3673
3674 /* Check device if no interrupts */
Arend van Spriel80969832012-06-09 22:51:44 +02003675 if (!bus->intr ||
3676 (bus->sdcnt.intrcount == bus->sdcnt.lastintrs)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003677
Arend van Sprielfccfe932013-06-18 13:29:26 +02003678 if (atomic_read(&bus->dpc_tskcnt) == 0) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003679 u8 devpend;
Arend van Sprielfccfe932013-06-18 13:29:26 +02003680
Franky Lin38b0b0d2012-11-05 16:22:24 -08003681 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriela39be272013-12-12 11:58:58 +01003682 devpend = brcmf_sdiod_regrb(bus->sdiodev,
3683 SDIO_CCCR_INTx,
3684 NULL);
Franky Lin38b0b0d2012-11-05 16:22:24 -08003685 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003686 intstatus =
3687 devpend & (INTR_STATUS_FUNC1 |
3688 INTR_STATUS_FUNC2);
3689 }
3690
3691 /* If there is something, make like the ISR and
3692 schedule the DPC */
3693 if (intstatus) {
Arend van Spriel80969832012-06-09 22:51:44 +02003694 bus->sdcnt.pollcnt++;
Franky Lin1d382272012-09-13 21:11:59 +02003695 atomic_set(&bus->ipend, 1);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003696
Arend van Sprielfccfe932013-06-18 13:29:26 +02003697 atomic_inc(&bus->dpc_tskcnt);
Franky Linf1e68c22012-09-13 21:12:00 +02003698 queue_work(bus->brcmf_wq, &bus->datawork);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003699 }
3700 }
3701
3702 /* Update interrupt tracking */
Arend van Spriel80969832012-06-09 22:51:44 +02003703 bus->sdcnt.lastintrs = bus->sdcnt.intrcount;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003704 }
Joe Perches8ae74652012-01-15 00:38:38 -08003705#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02003706 /* Poll for console output periodically */
Hante Meuleman2def5c12012-11-14 18:46:19 -08003707 if (bus_if && bus_if->state == BRCMF_BUS_DATA &&
Franky Lin8d169aa2011-11-22 17:21:52 -08003708 bus->console_interval != 0) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003709 bus->console.count += BRCMF_WD_POLL_MS;
3710 if (bus->console.count >= bus->console_interval) {
3711 bus->console.count -= bus->console_interval;
Franky Lin38b0b0d2012-11-05 16:22:24 -08003712 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003713 /* Make sure backplane clock is on */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003714 brcmf_sdio_bus_sleep(bus, false, false);
3715 if (brcmf_sdio_readconsole(bus) < 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003716 /* stop on error */
3717 bus->console_interval = 0;
Franky Lin38b0b0d2012-11-05 16:22:24 -08003718 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003719 }
3720 }
Joe Perches8ae74652012-01-15 00:38:38 -08003721#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02003722
3723 /* On idle timeout clear activity flag and/or turn off clock */
3724 if ((bus->idletime > 0) && (bus->clkstate == CLK_AVAIL)) {
3725 if (++bus->idlecount >= bus->idletime) {
3726 bus->idlecount = 0;
3727 if (bus->activity) {
3728 bus->activity = false;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003729 brcmf_sdio_wd_timer(bus, BRCMF_WD_POLL_MS);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003730 } else {
Piotr Haber4a3da992013-04-11 13:28:46 +02003731 brcmf_dbg(SDIO, "idle\n");
Franky Lin38b0b0d2012-11-05 16:22:24 -08003732 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003733 brcmf_sdio_bus_sleep(bus, true, false);
Franky Lin38b0b0d2012-11-05 16:22:24 -08003734 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003735 }
3736 }
3737 }
3738
Franky Lin1d382272012-09-13 21:11:59 +02003739 return (atomic_read(&bus->ipend) > 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003740}
3741
Franky Linf1e68c22012-09-13 21:12:00 +02003742static void brcmf_sdio_dataworker(struct work_struct *work)
3743{
3744 struct brcmf_sdio *bus = container_of(work, struct brcmf_sdio,
3745 datawork);
Franky Linf1e68c22012-09-13 21:12:00 +02003746
Arend van Sprielfccfe932013-06-18 13:29:26 +02003747 while (atomic_read(&bus->dpc_tskcnt)) {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003748 brcmf_sdio_dpc(bus);
Arend van Sprielfccfe932013-06-18 13:29:26 +02003749 atomic_dec(&bus->dpc_tskcnt);
Franky Linf1e68c22012-09-13 21:12:00 +02003750 }
Franky Linf1e68c22012-09-13 21:12:00 +02003751}
3752
Arend van Spriel5b435de2011-10-05 13:19:03 +02003753static bool
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003754brcmf_sdio_probe_attach(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003755{
3756 u8 clkctl = 0;
3757 int err = 0;
3758 int reg_addr;
3759 u32 reg_val;
Hante Meuleman668761a2013-04-12 10:55:55 +02003760 u32 drivestrength;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003761
Franky Lin38b0b0d2012-11-05 16:22:24 -08003762 sdio_claim_host(bus->sdiodev->func[1]);
3763
Joe Perches18aad4f2012-01-15 00:38:42 -08003764 pr_debug("F1 signature read @0x18000000=0x%4x\n",
Arend van Spriela39be272013-12-12 11:58:58 +01003765 brcmf_sdiod_regrl(bus->sdiodev, SI_ENUM_BASE, NULL));
Arend van Spriel5b435de2011-10-05 13:19:03 +02003766
3767 /*
Franky Lina97e4fc2011-11-04 22:23:35 +01003768 * Force PLL off until brcmf_sdio_chip_attach()
Arend van Spriel5b435de2011-10-05 13:19:03 +02003769 * programs PLL control regs
3770 */
3771
Arend van Spriela39be272013-12-12 11:58:58 +01003772 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3773 BRCMF_INIT_CLKCTL1, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003774 if (!err)
Arend van Spriela39be272013-12-12 11:58:58 +01003775 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
3776 SBSDIO_FUNC1_CHIPCLKCSR, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003777
3778 if (err || ((clkctl & ~SBSDIO_AVBITS) != BRCMF_INIT_CLKCTL1)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003779 brcmf_err("ChipClkCSR access: err %d wrote 0x%02x read 0x%02x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02003780 err, BRCMF_INIT_CLKCTL1, clkctl);
3781 goto fail;
3782 }
3783
Arend van Spriel4744d162013-12-12 11:58:55 +01003784 if (brcmf_sdio_chip_attach(bus->sdiodev, &bus->ci)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003785 brcmf_err("brcmf_sdio_chip_attach failed!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003786 goto fail;
3787 }
3788
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003789 if (brcmf_sdio_kso_init(bus)) {
Piotr Haber4a3da992013-04-11 13:28:46 +02003790 brcmf_err("error enabling KSO\n");
3791 goto fail;
3792 }
3793
Hante Meuleman668761a2013-04-12 10:55:55 +02003794 if ((bus->sdiodev->pdata) && (bus->sdiodev->pdata->drive_strength))
3795 drivestrength = bus->sdiodev->pdata->drive_strength;
3796 else
3797 drivestrength = DEFAULT_SDIO_DRIVE_STRENGTH;
3798 brcmf_sdio_chip_drivestrengthinit(bus->sdiodev, bus->ci, drivestrength);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003799
Franky Lin454d2a82011-11-04 22:23:37 +01003800 /* Get info on the SOCRAM cores... */
Arend van Spriel5b435de2011-10-05 13:19:03 +02003801 bus->ramsize = bus->ci->ramsize;
3802 if (!(bus->ramsize)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003803 brcmf_err("failed to find SOCRAM memory!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003804 goto fail;
3805 }
3806
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003807 /* Set card control so an SDIO card reset does a WLAN backplane reset */
Arend van Spriela39be272013-12-12 11:58:58 +01003808 reg_val = brcmf_sdiod_regrb(bus->sdiodev,
3809 SDIO_CCCR_BRCM_CARDCTRL, &err);
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003810 if (err)
3811 goto fail;
3812
3813 reg_val |= SDIO_CCCR_BRCM_CARDCTRL_WLANRESET;
3814
Arend van Spriela39be272013-12-12 11:58:58 +01003815 brcmf_sdiod_regwb(bus->sdiodev,
3816 SDIO_CCCR_BRCM_CARDCTRL, reg_val, &err);
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003817 if (err)
3818 goto fail;
3819
3820 /* set PMUControl so a backplane reset does PMU state reload */
3821 reg_addr = CORE_CC_REG(bus->ci->c_inf[0].base,
3822 pmucontrol);
Arend van Spriela39be272013-12-12 11:58:58 +01003823 reg_val = brcmf_sdiod_regrl(bus->sdiodev,
3824 reg_addr,
3825 &err);
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003826 if (err)
3827 goto fail;
3828
3829 reg_val |= (BCMA_CC_PMU_CTL_RES_RELOAD << BCMA_CC_PMU_CTL_RES_SHIFT);
3830
Arend van Spriela39be272013-12-12 11:58:58 +01003831 brcmf_sdiod_regwl(bus->sdiodev,
3832 reg_addr,
3833 reg_val,
3834 &err);
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003835 if (err)
3836 goto fail;
3837
Arend van Spriel5b435de2011-10-05 13:19:03 +02003838
Franky Lin38b0b0d2012-11-05 16:22:24 -08003839 sdio_release_host(bus->sdiodev->func[1]);
3840
Arend van Spriel5b435de2011-10-05 13:19:03 +02003841 brcmu_pktq_init(&bus->txq, (PRIOMASK + 1), TXQLEN);
3842
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01003843 /* allocate header buffer */
3844 bus->hdrbuf = kzalloc(MAX_HDR_READ + bus->head_align, GFP_KERNEL);
3845 if (!bus->hdrbuf)
3846 return false;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003847 /* Locate an appropriately-aligned portion of hdrbuf */
3848 bus->rxhdr = (u8 *) roundup((unsigned long)&bus->hdrbuf[0],
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01003849 bus->head_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003850
3851 /* Set the poll and/or interrupt flags */
3852 bus->intr = true;
3853 bus->poll = false;
3854 if (bus->poll)
3855 bus->pollrate = 1;
3856
3857 return true;
3858
3859fail:
Franky Lin38b0b0d2012-11-05 16:22:24 -08003860 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003861 return false;
3862}
3863
Arend van Spriel5b435de2011-10-05 13:19:03 +02003864static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003865brcmf_sdio_watchdog_thread(void *data)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003866{
Franky Line92eedf2011-11-22 17:21:50 -08003867 struct brcmf_sdio *bus = (struct brcmf_sdio *)data;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003868
3869 allow_signal(SIGTERM);
3870 /* Run until signal received */
3871 while (1) {
3872 if (kthread_should_stop())
3873 break;
3874 if (!wait_for_completion_interruptible(&bus->watchdog_wait)) {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003875 brcmf_sdio_bus_watchdog(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003876 /* Count the tick for reference */
Arend van Spriel80969832012-06-09 22:51:44 +02003877 bus->sdcnt.tickcnt++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003878 } else
3879 break;
3880 }
3881 return 0;
3882}
3883
3884static void
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003885brcmf_sdio_watchdog(unsigned long data)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003886{
Franky Line92eedf2011-11-22 17:21:50 -08003887 struct brcmf_sdio *bus = (struct brcmf_sdio *)data;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003888
3889 if (bus->watchdog_tsk) {
3890 complete(&bus->watchdog_wait);
3891 /* Reschedule the watchdog */
3892 if (bus->wd_timer_valid)
3893 mod_timer(&bus->timer,
3894 jiffies + BRCMF_WD_POLL_MS * HZ / 1000);
3895 }
3896}
3897
Arend van Sprield9cb2592012-12-05 15:25:54 +01003898static struct brcmf_bus_ops brcmf_sdio_bus_ops = {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003899 .stop = brcmf_sdio_bus_stop,
3900 .preinit = brcmf_sdio_bus_preinit,
3901 .init = brcmf_sdio_bus_init,
3902 .txdata = brcmf_sdio_bus_txdata,
3903 .txctl = brcmf_sdio_bus_txctl,
3904 .rxctl = brcmf_sdio_bus_rxctl,
3905 .gettxq = brcmf_sdio_bus_gettxq,
Arend van Sprield9cb2592012-12-05 15:25:54 +01003906};
3907
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003908struct brcmf_sdio *brcmf_sdio_probe(struct brcmf_sdio_dev *sdiodev)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003909{
3910 int ret;
Franky Line92eedf2011-11-22 17:21:50 -08003911 struct brcmf_sdio *bus;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003912
Arend van Spriel5b435de2011-10-05 13:19:03 +02003913 brcmf_dbg(TRACE, "Enter\n");
3914
Arend van Spriel5b435de2011-10-05 13:19:03 +02003915 /* Allocate private bus interface state */
Franky Line92eedf2011-11-22 17:21:50 -08003916 bus = kzalloc(sizeof(struct brcmf_sdio), GFP_ATOMIC);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003917 if (!bus)
3918 goto fail;
3919
3920 bus->sdiodev = sdiodev;
3921 sdiodev->bus = bus;
Arend van Sprielb83db862011-10-19 12:51:09 +02003922 skb_queue_head_init(&bus->glom);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003923 bus->txbound = BRCMF_TXBOUND;
3924 bus->rxbound = BRCMF_RXBOUND;
3925 bus->txminmax = BRCMF_TXMINMAX;
Franky Lin6bc52312013-08-10 12:27:28 +02003926 bus->tx_seq = SDPCM_SEQ_WRAP - 1;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003927
Arend van Spriele217d1c2013-11-29 12:25:13 +01003928 /* platform specific configuration:
3929 * alignments must be at least 4 bytes for ADMA
3930 */
3931 bus->head_align = ALIGNMENT;
3932 bus->sgentry_align = ALIGNMENT;
3933 if (sdiodev->pdata) {
3934 if (sdiodev->pdata->sd_head_align > ALIGNMENT)
3935 bus->head_align = sdiodev->pdata->sd_head_align;
3936 if (sdiodev->pdata->sd_sgentry_align > ALIGNMENT)
3937 bus->sgentry_align = sdiodev->pdata->sd_sgentry_align;
3938 }
3939
Hante Meuleman37ac5782012-11-14 18:46:18 -08003940 INIT_WORK(&bus->datawork, brcmf_sdio_dataworker);
3941 bus->brcmf_wq = create_singlethread_workqueue("brcmf_wq");
3942 if (bus->brcmf_wq == NULL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003943 brcmf_err("insufficient memory to create txworkqueue\n");
Hante Meuleman37ac5782012-11-14 18:46:18 -08003944 goto fail;
3945 }
3946
Arend van Spriel5b435de2011-10-05 13:19:03 +02003947 /* attempt to attach to the dongle */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003948 if (!(brcmf_sdio_probe_attach(bus))) {
3949 brcmf_err("brcmf_sdio_probe_attach failed\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003950 goto fail;
3951 }
3952
Franky Lindd43a012012-11-05 16:22:22 -08003953 spin_lock_init(&bus->rxctl_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003954 spin_lock_init(&bus->txqlock);
3955 init_waitqueue_head(&bus->ctrl_wait);
3956 init_waitqueue_head(&bus->dcmd_resp_wait);
3957
3958 /* Set up the watchdog timer */
3959 init_timer(&bus->timer);
3960 bus->timer.data = (unsigned long)bus;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003961 bus->timer.function = brcmf_sdio_watchdog;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003962
Arend van Spriel5b435de2011-10-05 13:19:03 +02003963 /* Initialize watchdog thread */
3964 init_completion(&bus->watchdog_wait);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003965 bus->watchdog_tsk = kthread_run(brcmf_sdio_watchdog_thread,
Arend van Spriel5b435de2011-10-05 13:19:03 +02003966 bus, "brcmf_watchdog");
3967 if (IS_ERR(bus->watchdog_tsk)) {
Joe Perches02f77192012-01-15 00:38:44 -08003968 pr_warn("brcmf_watchdog thread failed to start\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003969 bus->watchdog_tsk = NULL;
3970 }
3971 /* Initialize DPC thread */
Arend van Sprielfccfe932013-06-18 13:29:26 +02003972 atomic_set(&bus->dpc_tskcnt, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003973
Franky Lina9ffda82011-12-16 18:37:12 -08003974 /* Assign bus interface call back */
Arend van Sprield9cb2592012-12-05 15:25:54 +01003975 bus->sdiodev->bus_if->dev = bus->sdiodev->dev;
3976 bus->sdiodev->bus_if->ops = &brcmf_sdio_bus_ops;
Arend van Spriel75d907d2013-02-06 18:40:45 +01003977 bus->sdiodev->bus_if->chip = bus->ci->chip;
3978 bus->sdiodev->bus_if->chiprev = bus->ci->chiprev;
Arend van Sprield9cb2592012-12-05 15:25:54 +01003979
Franky Lin706478c2013-08-10 12:27:29 +02003980 /* default sdio bus header length for tx packet */
3981 bus->tx_hdrlen = SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN;
3982
3983 /* Attach to the common layer, reserve hdr space */
Arend van Spriel8dee77b2013-11-29 11:48:14 +01003984 ret = brcmf_attach(bus->sdiodev->dev);
Franky Lin712ac5b2011-12-16 18:37:09 -08003985 if (ret != 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003986 brcmf_err("brcmf_attach failed\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003987 goto fail;
3988 }
3989
3990 /* Allocate buffers */
Arend van Sprielfad13222014-01-06 12:40:40 +01003991 if (bus->sdiodev->bus_if->maxctl) {
3992 bus->rxblen =
3993 roundup((bus->sdiodev->bus_if->maxctl + SDPCM_HDRLEN),
3994 ALIGNMENT) + bus->head_align;
3995 bus->rxbuf = kmalloc(bus->rxblen, GFP_ATOMIC);
3996 if (!(bus->rxbuf)) {
3997 brcmf_err("rxbuf allocation failed\n");
3998 goto fail;
3999 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02004000 }
4001
Arend van Sprielfad13222014-01-06 12:40:40 +01004002 sdio_claim_host(bus->sdiodev->func[1]);
4003
4004 /* Disable F2 to clear any intermediate frame state on the dongle */
4005 sdio_disable_func(bus->sdiodev->func[SDIO_FUNC_2]);
4006
4007 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
4008 bus->rxflow = false;
4009
4010 /* Done with backplane-dependent accesses, can drop clock... */
4011 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, 0, NULL);
4012
4013 sdio_release_host(bus->sdiodev->func[1]);
4014
4015 /* ...and initialize clock/power states */
4016 bus->clkstate = CLK_SDONLY;
4017 bus->idletime = BRCMF_IDLE_INTERVAL;
4018 bus->idleclock = BRCMF_IDLE_ACTIVE;
4019
4020 /* Query the F2 block size, set roundup accordingly */
4021 bus->blocksize = bus->sdiodev->func[2]->cur_blksize;
4022 bus->roundup = min(max_roundup, bus->blocksize);
4023
4024 /* SR state */
4025 bus->sleeping = false;
4026 bus->sr_enabled = false;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004027
Arend van Spriel80969832012-06-09 22:51:44 +02004028 brcmf_sdio_debugfs_create(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004029 brcmf_dbg(INFO, "completed!!\n");
4030
4031 /* if firmware path present try to download and bring up bus */
Franky Lined683c92011-12-16 18:36:55 -08004032 ret = brcmf_bus_start(bus->sdiodev->dev);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004033 if (ret != 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01004034 brcmf_err("dongle is not responding\n");
Hante Meuleman1799ddf2012-11-14 18:46:17 -08004035 goto fail;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004036 }
Franky Lin15d45b62011-10-21 16:16:32 +02004037
Arend van Spriel5b435de2011-10-05 13:19:03 +02004038 return bus;
4039
4040fail:
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004041 brcmf_sdio_remove(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004042 return NULL;
4043}
4044
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004045/* Detach and free everything */
4046void brcmf_sdio_remove(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004047{
Arend van Spriel5b435de2011-10-05 13:19:03 +02004048 brcmf_dbg(TRACE, "Enter\n");
4049
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004050 if (bus) {
4051 /* De-register interrupt handler */
4052 brcmf_sdiod_intr_unregister(bus->sdiodev);
4053
4054 cancel_work_sync(&bus->datawork);
4055 if (bus->brcmf_wq)
4056 destroy_workqueue(bus->brcmf_wq);
4057
4058 if (bus->sdiodev->bus_if->drvr) {
4059 brcmf_detach(bus->sdiodev->dev);
Arend van Sprielbfad4a02014-01-06 12:40:39 +01004060 }
4061
4062 if (bus->ci) {
4063 sdio_claim_host(bus->sdiodev->func[1]);
4064 brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
Hante Meuleman53036262014-01-13 22:20:23 +01004065 /* Leave the device in state where it is 'quiet'. This
4066 * is done by putting it in download_state which
4067 * essentially resets all necessary cores
4068 */
4069 msleep(20);
Arend van Spriel33556502014-01-13 22:20:25 +01004070 brcmf_sdio_chip_enter_download(bus->sdiodev, bus->ci);
Arend van Sprielbfad4a02014-01-06 12:40:39 +01004071 brcmf_sdio_clkctl(bus, CLK_NONE, false);
4072 sdio_release_host(bus->sdiodev->func[1]);
4073 brcmf_sdio_chip_detach(&bus->ci);
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004074 }
4075
4076 brcmu_pkt_buf_free_skb(bus->txglom_sgpad);
Arend van Sprielbfad4a02014-01-06 12:40:39 +01004077 kfree(bus->rxbuf);
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004078 kfree(bus->hdrbuf);
4079 kfree(bus);
4080 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02004081
4082 brcmf_dbg(TRACE, "Disconnected\n");
4083}
4084
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01004085void brcmf_sdio_wd_timer(struct brcmf_sdio *bus, uint wdtick)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004086{
Arend van Spriel5b435de2011-10-05 13:19:03 +02004087 /* Totally stop the timer */
Joe Perches23677ce2012-02-09 11:17:23 +00004088 if (!wdtick && bus->wd_timer_valid) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02004089 del_timer_sync(&bus->timer);
4090 bus->wd_timer_valid = false;
4091 bus->save_ms = wdtick;
4092 return;
4093 }
4094
Franky Linece960e2011-10-21 16:16:19 +02004095 /* don't start the wd until fw is loaded */
Arend van Sprield6ae2c52014-01-06 12:40:44 +01004096 if (bus->sdiodev->bus_if->state != BRCMF_BUS_DATA)
Franky Linece960e2011-10-21 16:16:19 +02004097 return;
4098
Arend van Spriel5b435de2011-10-05 13:19:03 +02004099 if (wdtick) {
4100 if (bus->save_ms != BRCMF_WD_POLL_MS) {
Joe Perches23677ce2012-02-09 11:17:23 +00004101 if (bus->wd_timer_valid)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004102 /* Stop timer and restart at new value */
4103 del_timer_sync(&bus->timer);
4104
4105 /* Create timer again when watchdog period is
4106 dynamically changed or in the first instance
4107 */
4108 bus->timer.expires =
4109 jiffies + BRCMF_WD_POLL_MS * HZ / 1000;
4110 add_timer(&bus->timer);
4111
4112 } else {
4113 /* Re arm the timer, at last watchdog period */
4114 mod_timer(&bus->timer,
4115 jiffies + BRCMF_WD_POLL_MS * HZ / 1000);
4116 }
4117
4118 bus->wd_timer_valid = true;
4119 bus->save_ms = wdtick;
4120 }
4121}