blob: bf9bc2d8923019d3698a4e2e8aeccd613fa9bc9c [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>
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +010026#include <linux/mmc/sdio_ids.h>
Arend van Spriel5b435de2011-10-05 13:19:03 +020027#include <linux/mmc/sdio_func.h>
28#include <linux/mmc/card.h>
29#include <linux/semaphore.h>
30#include <linux/firmware.h>
Stephen Rothwellb7a57e72011-10-12 21:35:07 +020031#include <linux/module.h>
Franky Lin99ba15c2011-11-04 22:23:42 +010032#include <linux/bcma/bcma.h>
Arend van Spriel4fc0d012012-06-14 12:16:57 +020033#include <linux/debugfs.h>
Arend van Spriel8dc01812012-06-21 11:33:32 +020034#include <linux/vmalloc.h>
Hante Meuleman668761a2013-04-12 10:55:55 +020035#include <linux/platform_data/brcmfmac-sdio.h>
Franky Lin8da9d2c2013-11-29 12:25:10 +010036#include <linux/moduleparam.h>
Arend van Spriel5b435de2011-10-05 13:19:03 +020037#include <asm/unaligned.h>
38#include <defs.h>
39#include <brcmu_wifi.h>
40#include <brcmu_utils.h>
41#include <brcm_hw_ids.h>
42#include <soc.h>
43#include "sdio_host.h"
Arend van Spriel20c9c9b2014-01-29 15:32:15 +010044#include "chip.h"
Arend van Sprieldabedab2014-05-27 12:56:17 +020045#include "firmware.h"
Arend van Spriel5b435de2011-10-05 13:19:03 +020046
47#define DCMD_RESP_TIMEOUT 2000 /* In milli second */
48
Joe Perches8ae74652012-01-15 00:38:38 -080049#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +020050
51#define BRCMF_TRAP_INFO_SIZE 80
52
53#define CBUF_LEN (128)
54
Arend van Spriel4fc0d012012-06-14 12:16:57 +020055/* Device console log buffer state */
56#define CONSOLE_BUFFER_MAX 2024
57
Arend van Spriel5b435de2011-10-05 13:19:03 +020058struct rte_log_le {
59 __le32 buf; /* Can't be pointer on (64-bit) hosts */
60 __le32 buf_size;
61 __le32 idx;
62 char *_buf_compat; /* Redundant pointer for backward compat. */
63};
64
65struct rte_console {
66 /* Virtual UART
67 * When there is no UART (e.g. Quickturn),
68 * the host should write a complete
69 * input line directly into cbuf and then write
70 * the length into vcons_in.
71 * This may also be used when there is a real UART
72 * (at risk of conflicting with
73 * the real UART). vcons_out is currently unused.
74 */
75 uint vcons_in;
76 uint vcons_out;
77
78 /* Output (logging) buffer
79 * Console output is written to a ring buffer log_buf at index log_idx.
80 * The host may read the output when it sees log_idx advance.
81 * Output will be lost if the output wraps around faster than the host
82 * polls.
83 */
84 struct rte_log_le log_le;
85
86 /* Console input line buffer
87 * Characters are read one at a time into cbuf
88 * until <CR> is received, then
89 * the buffer is processed as a command line.
90 * Also used for virtual UART.
91 */
92 uint cbuf_idx;
93 char cbuf[CBUF_LEN];
94};
95
Joe Perches8ae74652012-01-15 00:38:38 -080096#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +020097#include <chipcommon.h>
98
Arend van Spriel5b435de2011-10-05 13:19:03 +020099#include "dhd_bus.h"
Arend van Spriel5b435de2011-10-05 13:19:03 +0200100#include "dhd_dbg.h"
Arend van Spriel40c1c242013-04-05 10:57:44 +0200101#include "tracepoint.h"
Arend van Spriel5b435de2011-10-05 13:19:03 +0200102
103#define TXQLEN 2048 /* bulk tx queue length */
104#define TXHI (TXQLEN - 256) /* turn on flow control above TXHI */
105#define TXLOW (TXHI - 256) /* turn off flow control below TXLOW */
106#define PRIOMASK 7
107
108#define TXRETRIES 2 /* # of retries for tx frames */
109
110#define BRCMF_RXBOUND 50 /* Default for max rx frames in
111 one scheduling */
112
113#define BRCMF_TXBOUND 20 /* Default for max tx frames in
114 one scheduling */
115
116#define BRCMF_TXMINMAX 1 /* Max tx frames if rx still pending */
117
118#define MEMBLOCK 2048 /* Block size used for downloading
119 of dongle image */
120#define MAX_DATA_BUF (32 * 1024) /* Must be large enough to hold
121 biggest possible glom */
122
123#define BRCMF_FIRSTREAD (1 << 6)
124
125
126/* SBSDIO_DEVICE_CTL */
127
128/* 1: device will assert busy signal when receiving CMD53 */
129#define SBSDIO_DEVCTL_SETBUSY 0x01
130/* 1: assertion of sdio interrupt is synchronous to the sdio clock */
131#define SBSDIO_DEVCTL_SPI_INTR_SYNC 0x02
132/* 1: mask all interrupts to host except the chipActive (rev 8) */
133#define SBSDIO_DEVCTL_CA_INT_ONLY 0x04
134/* 1: isolate internal sdio signals, put external pads in tri-state; requires
135 * sdio bus power cycle to clear (rev 9) */
136#define SBSDIO_DEVCTL_PADS_ISO 0x08
137/* Force SD->SB reset mapping (rev 11) */
138#define SBSDIO_DEVCTL_SB_RST_CTL 0x30
139/* Determined by CoreControl bit */
140#define SBSDIO_DEVCTL_RST_CORECTL 0x00
141/* Force backplane reset */
142#define SBSDIO_DEVCTL_RST_BPRESET 0x10
143/* Force no backplane reset */
144#define SBSDIO_DEVCTL_RST_NOBPRESET 0x20
145
Arend van Spriel5b435de2011-10-05 13:19:03 +0200146/* direct(mapped) cis space */
147
148/* MAPPED common CIS address */
149#define SBSDIO_CIS_BASE_COMMON 0x1000
150/* maximum bytes in one CIS */
151#define SBSDIO_CIS_SIZE_LIMIT 0x200
152/* cis offset addr is < 17 bits */
153#define SBSDIO_CIS_OFT_ADDR_MASK 0x1FFFF
154
155/* manfid tuple length, include tuple, link bytes */
156#define SBSDIO_CIS_MANFID_TUPLE_LEN 6
157
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100158#define CORE_BUS_REG(base, field) \
159 (base + offsetof(struct sdpcmd_regs, field))
160
161/* SDIO function 1 register CHIPCLKCSR */
162/* Force ALP request to backplane */
163#define SBSDIO_FORCE_ALP 0x01
164/* Force HT request to backplane */
165#define SBSDIO_FORCE_HT 0x02
166/* Force ILP request to backplane */
167#define SBSDIO_FORCE_ILP 0x04
168/* Make ALP ready (power up xtal) */
169#define SBSDIO_ALP_AVAIL_REQ 0x08
170/* Make HT ready (power up PLL) */
171#define SBSDIO_HT_AVAIL_REQ 0x10
172/* Squelch clock requests from HW */
173#define SBSDIO_FORCE_HW_CLKREQ_OFF 0x20
174/* Status: ALP is ready */
175#define SBSDIO_ALP_AVAIL 0x40
176/* Status: HT is ready */
177#define SBSDIO_HT_AVAIL 0x80
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100178#define SBSDIO_CSR_MASK 0x1F
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100179#define SBSDIO_AVBITS (SBSDIO_HT_AVAIL | SBSDIO_ALP_AVAIL)
180#define SBSDIO_ALPAV(regval) ((regval) & SBSDIO_AVBITS)
181#define SBSDIO_HTAV(regval) (((regval) & SBSDIO_AVBITS) == SBSDIO_AVBITS)
182#define SBSDIO_ALPONLY(regval) (SBSDIO_ALPAV(regval) && !SBSDIO_HTAV(regval))
183#define SBSDIO_CLKAV(regval, alponly) \
184 (SBSDIO_ALPAV(regval) && (alponly ? 1 : SBSDIO_HTAV(regval)))
185
Arend van Spriel5b435de2011-10-05 13:19:03 +0200186/* intstatus */
187#define I_SMB_SW0 (1 << 0) /* To SB Mail S/W interrupt 0 */
188#define I_SMB_SW1 (1 << 1) /* To SB Mail S/W interrupt 1 */
189#define I_SMB_SW2 (1 << 2) /* To SB Mail S/W interrupt 2 */
190#define I_SMB_SW3 (1 << 3) /* To SB Mail S/W interrupt 3 */
191#define I_SMB_SW_MASK 0x0000000f /* To SB Mail S/W interrupts mask */
192#define I_SMB_SW_SHIFT 0 /* To SB Mail S/W interrupts shift */
193#define I_HMB_SW0 (1 << 4) /* To Host Mail S/W interrupt 0 */
194#define I_HMB_SW1 (1 << 5) /* To Host Mail S/W interrupt 1 */
195#define I_HMB_SW2 (1 << 6) /* To Host Mail S/W interrupt 2 */
196#define I_HMB_SW3 (1 << 7) /* To Host Mail S/W interrupt 3 */
197#define I_HMB_SW_MASK 0x000000f0 /* To Host Mail S/W interrupts mask */
198#define I_HMB_SW_SHIFT 4 /* To Host Mail S/W interrupts shift */
199#define I_WR_OOSYNC (1 << 8) /* Write Frame Out Of Sync */
200#define I_RD_OOSYNC (1 << 9) /* Read Frame Out Of Sync */
201#define I_PC (1 << 10) /* descriptor error */
202#define I_PD (1 << 11) /* data error */
203#define I_DE (1 << 12) /* Descriptor protocol Error */
204#define I_RU (1 << 13) /* Receive descriptor Underflow */
205#define I_RO (1 << 14) /* Receive fifo Overflow */
206#define I_XU (1 << 15) /* Transmit fifo Underflow */
207#define I_RI (1 << 16) /* Receive Interrupt */
208#define I_BUSPWR (1 << 17) /* SDIO Bus Power Change (rev 9) */
209#define I_XMTDATA_AVAIL (1 << 23) /* bits in fifo */
210#define I_XI (1 << 24) /* Transmit Interrupt */
211#define I_RF_TERM (1 << 25) /* Read Frame Terminate */
212#define I_WF_TERM (1 << 26) /* Write Frame Terminate */
213#define I_PCMCIA_XU (1 << 27) /* PCMCIA Transmit FIFO Underflow */
214#define I_SBINT (1 << 28) /* sbintstatus Interrupt */
215#define I_CHIPACTIVE (1 << 29) /* chip from doze to active state */
216#define I_SRESET (1 << 30) /* CCCR RES interrupt */
217#define I_IOE2 (1U << 31) /* CCCR IOE2 Bit Changed */
218#define I_ERRORS (I_PC | I_PD | I_DE | I_RU | I_RO | I_XU)
219#define I_DMA (I_RI | I_XI | I_ERRORS)
220
221/* corecontrol */
222#define CC_CISRDY (1 << 0) /* CIS Ready */
223#define CC_BPRESEN (1 << 1) /* CCCR RES signal */
224#define CC_F2RDY (1 << 2) /* set CCCR IOR2 bit */
225#define CC_CLRPADSISO (1 << 3) /* clear SDIO pads isolation */
226#define CC_XMTDATAAVAIL_MODE (1 << 4)
227#define CC_XMTDATAAVAIL_CTRL (1 << 5)
228
229/* SDA_FRAMECTRL */
230#define SFC_RF_TERM (1 << 0) /* Read Frame Terminate */
231#define SFC_WF_TERM (1 << 1) /* Write Frame Terminate */
232#define SFC_CRC4WOOS (1 << 2) /* CRC error for write out of sync */
233#define SFC_ABORTALL (1 << 3) /* Abort all in-progress frames */
234
Arend van Spriel5b435de2011-10-05 13:19:03 +0200235/*
236 * Software allocation of To SB Mailbox resources
237 */
238
239/* tosbmailbox bits corresponding to intstatus bits */
240#define SMB_NAK (1 << 0) /* Frame NAK */
241#define SMB_INT_ACK (1 << 1) /* Host Interrupt ACK */
242#define SMB_USE_OOB (1 << 2) /* Use OOB Wakeup */
243#define SMB_DEV_INT (1 << 3) /* Miscellaneous Interrupt */
244
245/* tosbmailboxdata */
246#define SMB_DATA_VERSION_SHIFT 16 /* host protocol version */
247
248/*
249 * Software allocation of To Host Mailbox resources
250 */
251
252/* intstatus bits */
253#define I_HMB_FC_STATE I_HMB_SW0 /* Flow Control State */
254#define I_HMB_FC_CHANGE I_HMB_SW1 /* Flow Control State Changed */
255#define I_HMB_FRAME_IND I_HMB_SW2 /* Frame Indication */
256#define I_HMB_HOST_INT I_HMB_SW3 /* Miscellaneous Interrupt */
257
258/* tohostmailboxdata */
259#define HMB_DATA_NAKHANDLED 1 /* retransmit NAK'd frame */
260#define HMB_DATA_DEVREADY 2 /* talk to host after enable */
261#define HMB_DATA_FC 4 /* per prio flowcontrol update flag */
262#define HMB_DATA_FWREADY 8 /* fw ready for protocol activity */
263
264#define HMB_DATA_FCDATA_MASK 0xff000000
265#define HMB_DATA_FCDATA_SHIFT 24
266
267#define HMB_DATA_VERSION_MASK 0x00ff0000
268#define HMB_DATA_VERSION_SHIFT 16
269
270/*
271 * Software-defined protocol header
272 */
273
274/* Current protocol version */
275#define SDPCM_PROT_VERSION 4
276
Arend van Spriel5b435de2011-10-05 13:19:03 +0200277/*
278 * Shared structure between dongle and the host.
279 * The structure contains pointers to trap or assert information.
280 */
Arend van Spriel4fc0d012012-06-14 12:16:57 +0200281#define SDPCM_SHARED_VERSION 0x0003
Arend van Spriel5b435de2011-10-05 13:19:03 +0200282#define SDPCM_SHARED_VERSION_MASK 0x00FF
283#define SDPCM_SHARED_ASSERT_BUILT 0x0100
284#define SDPCM_SHARED_ASSERT 0x0200
285#define SDPCM_SHARED_TRAP 0x0400
286
287/* Space for header read, limit for data packets */
288#define MAX_HDR_READ (1 << 6)
289#define MAX_RX_DATASZ 2048
290
Arend van Spriel5b435de2011-10-05 13:19:03 +0200291/* Bump up limit on waiting for HT to account for first startup;
292 * if the image is doing a CRC calculation before programming the PMU
293 * for HT availability, it could take a couple hundred ms more, so
294 * max out at a 1 second (1000000us).
295 */
296#undef PMU_MAX_TRANSITION_DLY
297#define PMU_MAX_TRANSITION_DLY 1000000
298
299/* Value for ChipClockCSR during initial setup */
300#define BRCMF_INIT_CLKCTL1 (SBSDIO_FORCE_HW_CLKREQ_OFF | \
301 SBSDIO_ALP_AVAIL_REQ)
302
303/* Flags for SDH calls */
304#define F2SYNC (SDIO_REQ_4BYTE | SDIO_REQ_FIXED)
305
Franky Lin382a9e02011-12-16 18:37:18 -0800306#define BRCMF_IDLE_ACTIVE 0 /* Do not request any SD clock change
307 * when idle
308 */
309#define BRCMF_IDLE_INTERVAL 1
310
Piotr Haber4a3da992013-04-11 13:28:46 +0200311#define KSO_WAIT_US 50
312#define MAX_KSO_ATTEMPTS (PMU_MAX_TRANSITION_DLY/KSO_WAIT_US)
313
Arend van Spriel5b435de2011-10-05 13:19:03 +0200314/*
315 * Conversion of 802.1D priority to precedence level
316 */
317static uint prio2prec(u32 prio)
318{
319 return (prio == PRIO_8021D_NONE || prio == PRIO_8021D_BE) ?
320 (prio^2) : prio;
321}
322
Joe Perches8ae74652012-01-15 00:38:38 -0800323#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200324/* Device console log buffer state */
325struct brcmf_console {
326 uint count; /* Poll interval msec counter */
327 uint log_addr; /* Log struct address (fixed) */
328 struct rte_log_le log_le; /* Log struct (host copy) */
329 uint bufsize; /* Size of log buffer */
330 u8 *buf; /* Log buffer (host copy) */
331 uint last; /* Last buffer read index */
332};
Arend van Spriel4fc0d012012-06-14 12:16:57 +0200333
334struct brcmf_trap_info {
335 __le32 type;
336 __le32 epc;
337 __le32 cpsr;
338 __le32 spsr;
339 __le32 r0; /* a1 */
340 __le32 r1; /* a2 */
341 __le32 r2; /* a3 */
342 __le32 r3; /* a4 */
343 __le32 r4; /* v1 */
344 __le32 r5; /* v2 */
345 __le32 r6; /* v3 */
346 __le32 r7; /* v4 */
347 __le32 r8; /* v5 */
348 __le32 r9; /* sb/v6 */
349 __le32 r10; /* sl/v7 */
350 __le32 r11; /* fp/v8 */
351 __le32 r12; /* ip */
352 __le32 r13; /* sp */
353 __le32 r14; /* lr */
354 __le32 pc; /* r15 */
355};
Joe Perches8ae74652012-01-15 00:38:38 -0800356#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200357
358struct sdpcm_shared {
359 u32 flags;
360 u32 trap_addr;
361 u32 assert_exp_addr;
362 u32 assert_file_addr;
363 u32 assert_line;
364 u32 console_addr; /* Address of struct rte_console */
365 u32 msgtrace_addr;
366 u8 tag[32];
Arend van Spriel4fc0d012012-06-14 12:16:57 +0200367 u32 brpt_addr;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200368};
369
370struct sdpcm_shared_le {
371 __le32 flags;
372 __le32 trap_addr;
373 __le32 assert_exp_addr;
374 __le32 assert_file_addr;
375 __le32 assert_line;
376 __le32 console_addr; /* Address of struct rte_console */
377 __le32 msgtrace_addr;
378 u8 tag[32];
Arend van Spriel4fc0d012012-06-14 12:16:57 +0200379 __le32 brpt_addr;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200380};
381
Franky Lin6bc52312013-08-10 12:27:28 +0200382/* dongle SDIO bus specific header info */
383struct brcmf_sdio_hdrinfo {
Franky Lin4754fce2012-09-19 22:21:11 +0200384 u8 seq_num;
385 u8 channel;
386 u16 len;
387 u16 len_left;
388 u16 len_nxtfrm;
389 u8 dat_offset;
Franky Lin8da9d2c2013-11-29 12:25:10 +0100390 bool lastfrm;
391 u16 tail_pad;
Franky Lin4754fce2012-09-19 22:21:11 +0200392};
Arend van Spriel5b435de2011-10-05 13:19:03 +0200393
394/* misc chip info needed by some of the routines */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200395/* Private data for SDIO bus interaction */
Franky Line92eedf2011-11-22 17:21:50 -0800396struct brcmf_sdio {
Arend van Spriel5b435de2011-10-05 13:19:03 +0200397 struct brcmf_sdio_dev *sdiodev; /* sdio device handler */
Arend van Spriel9cf218f2014-01-13 22:20:27 +0100398 struct brcmf_chip *ci; /* Chip info struct */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200399
400 u32 ramsize; /* Size of RAM in SOCRAM (bytes) */
401
402 u32 hostintmask; /* Copy of Host Interrupt Mask */
Franky Lin45316032012-09-13 21:12:03 +0200403 atomic_t intstatus; /* Intstatus bits (events) pending */
404 atomic_t fcstate; /* State of dongle flow-control */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200405
406 uint blocksize; /* Block size of SDIO transfers */
407 uint roundup; /* Max roundup limit */
408
409 struct pktq txq; /* Queue length used for flow-control */
410 u8 flowcontrol; /* per prio flow control bitmask */
411 u8 tx_seq; /* Transmit sequence number (next) */
412 u8 tx_max; /* Maximum transmit sequence allowed */
413
Arend van Spriel9b2d2f2a82013-11-29 12:25:14 +0100414 u8 *hdrbuf; /* buffer for handling rx frame */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200415 u8 *rxhdr; /* Header of current rx frame (in hdrbuf) */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200416 u8 rx_seq; /* Receive sequence number (expected) */
Franky Lin6bc52312013-08-10 12:27:28 +0200417 struct brcmf_sdio_hdrinfo cur_read;
Franky Lin4754fce2012-09-19 22:21:11 +0200418 /* info of current read frame */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200419 bool rxskip; /* Skip receive (awaiting NAK ACK) */
Franky Lin4754fce2012-09-19 22:21:11 +0200420 bool rxpending; /* Data frame pending in dongle */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200421
422 uint rxbound; /* Rx frames to read before resched */
423 uint txbound; /* Tx frames to send before resched */
424 uint txminmax;
425
426 struct sk_buff *glomd; /* Packet containing glomming descriptor */
Arend van Sprielb83db862011-10-19 12:51:09 +0200427 struct sk_buff_head glom; /* Packet list for glommed superframe */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200428 uint glomerr; /* Glom packet read errors */
429
430 u8 *rxbuf; /* Buffer for receiving control packets */
431 uint rxblen; /* Allocated length of rxbuf */
432 u8 *rxctl; /* Aligned pointer into rxbuf */
Franky Lindd43a012012-11-05 16:22:22 -0800433 u8 *rxctl_orig; /* pointer for freeing rxctl */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200434 uint rxlen; /* Length of valid data in buffer */
Franky Lindd43a012012-11-05 16:22:22 -0800435 spinlock_t rxctl_lock; /* protection lock for ctrl frame resources */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200436
437 u8 sdpcm_ver; /* Bus protocol reported by dongle */
438
439 bool intr; /* Use interrupts */
440 bool poll; /* Use polling */
Franky Lin1d382272012-09-13 21:11:59 +0200441 atomic_t ipend; /* Device interrupt is pending */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200442 uint spurious; /* Count of spurious interrupts */
443 uint pollrate; /* Ticks between device polls */
444 uint polltick; /* Tick counter */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200445
Joe Perches8ae74652012-01-15 00:38:38 -0800446#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200447 uint console_interval;
448 struct brcmf_console console; /* Console output polling support */
449 uint console_addr; /* Console address from shared struct */
Joe Perches8ae74652012-01-15 00:38:38 -0800450#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200451
Arend van Spriel5b435de2011-10-05 13:19:03 +0200452 uint clkstate; /* State of sd and backplane clock(s) */
453 bool activity; /* Activity flag for clock down */
454 s32 idletime; /* Control for activity timeout */
455 s32 idlecount; /* Activity timeout counter */
456 s32 idleclock; /* How to set bus driver when idle */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200457 bool rxflow_mode; /* Rx flow control mode */
458 bool rxflow; /* Is rx flow control on */
459 bool alp_only; /* Don't use HT clock (ALP only) */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200460
Arend van Spriel5b435de2011-10-05 13:19:03 +0200461 u8 *ctrl_frame_buf;
Hante Meulemanfed7ec42014-03-15 17:18:18 +0100462 u16 ctrl_frame_len;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200463 bool ctrl_frame_stat;
464
Hante Meulemanfed7ec42014-03-15 17:18:18 +0100465 spinlock_t txq_lock; /* protect bus->txq */
466 struct semaphore tx_seq_lock; /* protect bus->tx_seq */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200467 wait_queue_head_t ctrl_wait;
468 wait_queue_head_t dcmd_resp_wait;
469
470 struct timer_list timer;
471 struct completion watchdog_wait;
472 struct task_struct *watchdog_tsk;
473 bool wd_timer_valid;
474 uint save_ms;
475
Franky Linf1e68c22012-09-13 21:12:00 +0200476 struct workqueue_struct *brcmf_wq;
477 struct work_struct datawork;
Arend van Sprielfccfe932013-06-18 13:29:26 +0200478 atomic_t dpc_tskcnt;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200479
Franky Linc8bf3482011-12-16 18:37:08 -0800480 bool txoff; /* Transmit flow-controlled */
Arend van Spriel80969832012-06-09 22:51:44 +0200481 struct brcmf_sdio_count sdcnt;
Piotr Haber4a3da992013-04-11 13:28:46 +0200482 bool sr_enabled; /* SaveRestore enabled */
483 bool sleeping; /* SDIO bus sleeping */
Franky Lin706478c2013-08-10 12:27:29 +0200484
485 u8 tx_hdrlen; /* sdio bus header length for tx packet */
Franky Lin8da9d2c2013-11-29 12:25:10 +0100486 bool txglom; /* host tx glomming enable flag */
Arend van Spriele217d1c2013-11-29 12:25:13 +0100487 u16 head_align; /* buffer pointer alignment */
488 u16 sgentry_align; /* scatter-gather buffer alignment */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200489};
490
Arend van Spriel5b435de2011-10-05 13:19:03 +0200491/* clkstate */
492#define CLK_NONE 0
493#define CLK_SDONLY 1
Piotr Haber4a3da992013-04-11 13:28:46 +0200494#define CLK_PENDING 2
Arend van Spriel5b435de2011-10-05 13:19:03 +0200495#define CLK_AVAIL 3
496
Joe Perches8ae74652012-01-15 00:38:38 -0800497#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200498static int qcount[NUMPRIO];
Joe Perches8ae74652012-01-15 00:38:38 -0800499#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200500
Hante Meuleman668761a2013-04-12 10:55:55 +0200501#define DEFAULT_SDIO_DRIVE_STRENGTH 6 /* in milliamps */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200502
503#define RETRYCHAN(chan) ((chan) == SDPCM_EVENT_CHANNEL)
504
505/* Retry count for register access failures */
506static const uint retry_limit = 2;
507
508/* Limit on rounding up frames */
509static const uint max_roundup = 512;
510
511#define ALIGNMENT 4
512
Franky Lin9d7d6f92012-10-22 10:36:24 -0700513enum brcmf_sdio_frmtype {
514 BRCMF_SDIO_FT_NORMAL,
515 BRCMF_SDIO_FT_SUPER,
516 BRCMF_SDIO_FT_SUB,
517};
518
Arend van Spriel65d80d02014-01-29 15:32:14 +0100519#define SDIOD_DRVSTR_KEY(chip, pmu) (((chip) << 16) | (pmu))
520
521/* SDIO Pad drive strength to select value mappings */
522struct sdiod_drive_str {
523 u8 strength; /* Pad Drive Strength in mA */
524 u8 sel; /* Chip-specific select value */
525};
526
527/* SDIO Drive Strength to sel value table for PMU Rev 11 (1.8V) */
528static const struct sdiod_drive_str sdiod_drvstr_tab1_1v8[] = {
529 {32, 0x6},
530 {26, 0x7},
531 {22, 0x4},
532 {16, 0x5},
533 {12, 0x2},
534 {8, 0x3},
535 {4, 0x0},
536 {0, 0x1}
537};
538
539/* SDIO Drive Strength to sel value table for PMU Rev 13 (1.8v) */
540static const struct sdiod_drive_str sdiod_drive_strength_tab5_1v8[] = {
541 {6, 0x7},
542 {5, 0x6},
543 {4, 0x5},
544 {3, 0x4},
545 {2, 0x2},
546 {1, 0x1},
547 {0, 0x0}
548};
549
550/* SDIO Drive Strength to sel value table for PMU Rev 17 (1.8v) */
551static const struct sdiod_drive_str sdiod_drvstr_tab6_1v8[] = {
552 {3, 0x3},
553 {2, 0x2},
554 {1, 0x1},
555 {0, 0x0} };
556
557/* SDIO Drive Strength to sel value table for 43143 PMU Rev 17 (3.3V) */
558static const struct sdiod_drive_str sdiod_drvstr_tab2_3v3[] = {
559 {16, 0x7},
560 {12, 0x5},
561 {8, 0x3},
562 {4, 0x1}
563};
564
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200565#define BCM43143_FIRMWARE_NAME "brcm/brcmfmac43143-sdio.bin"
566#define BCM43143_NVRAM_NAME "brcm/brcmfmac43143-sdio.txt"
567#define BCM43241B0_FIRMWARE_NAME "brcm/brcmfmac43241b0-sdio.bin"
568#define BCM43241B0_NVRAM_NAME "brcm/brcmfmac43241b0-sdio.txt"
569#define BCM43241B4_FIRMWARE_NAME "brcm/brcmfmac43241b4-sdio.bin"
570#define BCM43241B4_NVRAM_NAME "brcm/brcmfmac43241b4-sdio.txt"
571#define BCM4329_FIRMWARE_NAME "brcm/brcmfmac4329-sdio.bin"
572#define BCM4329_NVRAM_NAME "brcm/brcmfmac4329-sdio.txt"
573#define BCM4330_FIRMWARE_NAME "brcm/brcmfmac4330-sdio.bin"
574#define BCM4330_NVRAM_NAME "brcm/brcmfmac4330-sdio.txt"
575#define BCM4334_FIRMWARE_NAME "brcm/brcmfmac4334-sdio.bin"
576#define BCM4334_NVRAM_NAME "brcm/brcmfmac4334-sdio.txt"
577#define BCM4335_FIRMWARE_NAME "brcm/brcmfmac4335-sdio.bin"
578#define BCM4335_NVRAM_NAME "brcm/brcmfmac4335-sdio.txt"
Arend van Spriel11e69c32014-01-08 10:49:33 +0100579#define BCM43362_FIRMWARE_NAME "brcm/brcmfmac43362-sdio.bin"
580#define BCM43362_NVRAM_NAME "brcm/brcmfmac43362-sdio.txt"
Franky Linbed89b62013-11-29 12:25:09 +0100581#define BCM4339_FIRMWARE_NAME "brcm/brcmfmac4339-sdio.bin"
582#define BCM4339_NVRAM_NAME "brcm/brcmfmac4339-sdio.txt"
Franky Lina797ca12014-03-15 17:18:17 +0100583#define BCM4354_FIRMWARE_NAME "brcm/brcmfmac4354-sdio.bin"
584#define BCM4354_NVRAM_NAME "brcm/brcmfmac4354-sdio.txt"
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200585
586MODULE_FIRMWARE(BCM43143_FIRMWARE_NAME);
587MODULE_FIRMWARE(BCM43143_NVRAM_NAME);
588MODULE_FIRMWARE(BCM43241B0_FIRMWARE_NAME);
589MODULE_FIRMWARE(BCM43241B0_NVRAM_NAME);
590MODULE_FIRMWARE(BCM43241B4_FIRMWARE_NAME);
591MODULE_FIRMWARE(BCM43241B4_NVRAM_NAME);
592MODULE_FIRMWARE(BCM4329_FIRMWARE_NAME);
593MODULE_FIRMWARE(BCM4329_NVRAM_NAME);
594MODULE_FIRMWARE(BCM4330_FIRMWARE_NAME);
595MODULE_FIRMWARE(BCM4330_NVRAM_NAME);
596MODULE_FIRMWARE(BCM4334_FIRMWARE_NAME);
597MODULE_FIRMWARE(BCM4334_NVRAM_NAME);
598MODULE_FIRMWARE(BCM4335_FIRMWARE_NAME);
599MODULE_FIRMWARE(BCM4335_NVRAM_NAME);
Arend van Spriel11e69c32014-01-08 10:49:33 +0100600MODULE_FIRMWARE(BCM43362_FIRMWARE_NAME);
601MODULE_FIRMWARE(BCM43362_NVRAM_NAME);
Franky Linbed89b62013-11-29 12:25:09 +0100602MODULE_FIRMWARE(BCM4339_FIRMWARE_NAME);
603MODULE_FIRMWARE(BCM4339_NVRAM_NAME);
Franky Lina797ca12014-03-15 17:18:17 +0100604MODULE_FIRMWARE(BCM4354_FIRMWARE_NAME);
605MODULE_FIRMWARE(BCM4354_NVRAM_NAME);
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200606
607struct brcmf_firmware_names {
608 u32 chipid;
609 u32 revmsk;
610 const char *bin;
611 const char *nv;
612};
613
614enum brcmf_firmware_type {
615 BRCMF_FIRMWARE_BIN,
616 BRCMF_FIRMWARE_NVRAM
617};
618
619#define BRCMF_FIRMWARE_NVRAM(name) \
620 name ## _FIRMWARE_NAME, name ## _NVRAM_NAME
621
622static const struct brcmf_firmware_names brcmf_fwname_data[] = {
Hante Meuleman5779ae62014-07-12 08:49:34 +0200623 { BRCM_CC_43143_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM43143) },
624 { BRCM_CC_43241_CHIP_ID, 0x0000001F, BRCMF_FIRMWARE_NVRAM(BCM43241B0) },
625 { BRCM_CC_43241_CHIP_ID, 0xFFFFFFE0, BRCMF_FIRMWARE_NVRAM(BCM43241B4) },
626 { BRCM_CC_4329_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM4329) },
627 { BRCM_CC_4330_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM4330) },
628 { BRCM_CC_4334_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM4334) },
629 { BRCM_CC_4335_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM4335) },
630 { BRCM_CC_43362_CHIP_ID, 0xFFFFFFFE, BRCMF_FIRMWARE_NVRAM(BCM43362) },
631 { BRCM_CC_4339_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM4339) },
632 { BRCM_CC_4354_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM4354) }
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200633};
634
Arend van Sprielbd0e1b12014-05-27 12:56:22 +0200635static const char *brcmf_sdio_get_fwname(struct brcmf_chip *ci,
636 enum brcmf_firmware_type type)
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200637{
Arend van Sprielbd0e1b12014-05-27 12:56:22 +0200638 int i;
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200639
640 for (i = 0; i < ARRAY_SIZE(brcmf_fwname_data); i++) {
Arend van Sprielbd0e1b12014-05-27 12:56:22 +0200641 if (brcmf_fwname_data[i].chipid == ci->chip &&
642 brcmf_fwname_data[i].revmsk & BIT(ci->chiprev)) {
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200643 switch (type) {
644 case BRCMF_FIRMWARE_BIN:
Arend van Sprielbd0e1b12014-05-27 12:56:22 +0200645 return brcmf_fwname_data[i].bin;
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200646 case BRCMF_FIRMWARE_NVRAM:
Arend van Sprielbd0e1b12014-05-27 12:56:22 +0200647 return brcmf_fwname_data[i].nv;
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200648 default:
649 brcmf_err("invalid firmware type (%d)\n", type);
650 return NULL;
651 }
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200652 }
653 }
654 brcmf_err("Unknown chipid %d [%d]\n",
Arend van Sprielbd0e1b12014-05-27 12:56:22 +0200655 ci->chip, ci->chiprev);
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200656 return NULL;
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200657}
658
Arend van Spriel5b435de2011-10-05 13:19:03 +0200659static void pkt_align(struct sk_buff *p, int len, int align)
660{
661 uint datalign;
662 datalign = (unsigned long)(p->data);
663 datalign = roundup(datalign, (align)) - datalign;
664 if (datalign)
665 skb_pull(p, datalign);
666 __skb_trim(p, len);
667}
668
669/* To check if there's window offered */
Franky Line92eedf2011-11-22 17:21:50 -0800670static bool data_ok(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200671{
672 return (u8)(bus->tx_max - bus->tx_seq) != 0 &&
673 ((u8)(bus->tx_max - bus->tx_seq) & 0x80) == 0;
674}
675
676/*
677 * Reads a register in the SDIO hardware block. This block occupies a series of
678 * adresses on the 32 bit backplane bus.
679 */
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100680static int r_sdreg32(struct brcmf_sdio *bus, u32 *regvar, u32 offset)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200681{
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100682 struct brcmf_core *core;
Franky Lin79ae3952012-05-04 18:27:34 -0700683 int ret;
Franky Lin58692752012-05-04 18:27:36 -0700684
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100685 core = brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV);
686 *regvar = brcmf_sdiod_regrl(bus->sdiodev, core->base + offset, &ret);
Franky Lin58692752012-05-04 18:27:36 -0700687
688 return ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200689}
690
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100691static int w_sdreg32(struct brcmf_sdio *bus, u32 regval, u32 reg_offset)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200692{
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100693 struct brcmf_core *core;
Franky Line13ce262012-05-04 18:27:35 -0700694 int ret;
Franky Lin58692752012-05-04 18:27:36 -0700695
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100696 core = brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV);
697 brcmf_sdiod_regwl(bus->sdiodev, core->base + reg_offset, regval, &ret);
Franky Lin58692752012-05-04 18:27:36 -0700698
699 return ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200700}
701
Piotr Haber4a3da992013-04-11 13:28:46 +0200702static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100703brcmf_sdio_kso_control(struct brcmf_sdio *bus, bool on)
Piotr Haber4a3da992013-04-11 13:28:46 +0200704{
705 u8 wr_val = 0, rd_val, cmp_val, bmask;
706 int err = 0;
707 int try_cnt = 0;
708
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100709 brcmf_dbg(TRACE, "Enter: on=%d\n", on);
Piotr Haber4a3da992013-04-11 13:28:46 +0200710
711 wr_val = (on << SBSDIO_FUNC1_SLEEPCSR_KSO_SHIFT);
712 /* 1st KSO write goes to AOS wake up core if device is asleep */
Arend van Spriela39be272013-12-12 11:58:58 +0100713 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
714 wr_val, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +0200715
716 if (on) {
717 /* device WAKEUP through KSO:
718 * write bit 0 & read back until
719 * both bits 0 (kso bit) & 1 (dev on status) are set
720 */
721 cmp_val = SBSDIO_FUNC1_SLEEPCSR_KSO_MASK |
722 SBSDIO_FUNC1_SLEEPCSR_DEVON_MASK;
723 bmask = cmp_val;
724 usleep_range(2000, 3000);
725 } else {
726 /* Put device to sleep, turn off KSO */
727 cmp_val = 0;
728 /* only check for bit0, bit1(dev on status) may not
729 * get cleared right away
730 */
731 bmask = SBSDIO_FUNC1_SLEEPCSR_KSO_MASK;
732 }
733
734 do {
735 /* reliable KSO bit set/clr:
736 * the sdiod sleep write access is synced to PMU 32khz clk
737 * just one write attempt may fail,
738 * read it back until it matches written value
739 */
Arend van Spriela39be272013-12-12 11:58:58 +0100740 rd_val = brcmf_sdiod_regrb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
741 &err);
Piotr Haber4a3da992013-04-11 13:28:46 +0200742 if (((rd_val & bmask) == cmp_val) && !err)
743 break;
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100744
Piotr Haber4a3da992013-04-11 13:28:46 +0200745 udelay(KSO_WAIT_US);
Arend van Spriela39be272013-12-12 11:58:58 +0100746 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
747 wr_val, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +0200748 } while (try_cnt++ < MAX_KSO_ATTEMPTS);
749
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100750 if (try_cnt > 2)
751 brcmf_dbg(SDIO, "try_cnt=%d rd_val=0x%x err=%d\n", try_cnt,
752 rd_val, err);
753
754 if (try_cnt > MAX_KSO_ATTEMPTS)
755 brcmf_err("max tries: rd_val=0x%x err=%d\n", rd_val, err);
756
Piotr Haber4a3da992013-04-11 13:28:46 +0200757 return err;
758}
759
Arend van Spriel5b435de2011-10-05 13:19:03 +0200760#define HOSTINTMASK (I_HMB_SW_MASK | I_CHIPACTIVE)
761
Arend van Spriel5b435de2011-10-05 13:19:03 +0200762/* Turn backplane clock on or off */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100763static int brcmf_sdio_htclk(struct brcmf_sdio *bus, bool on, bool pendok)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200764{
765 int err;
766 u8 clkctl, clkreq, devctl;
767 unsigned long timeout;
768
Arend van Sprielc3203372013-04-03 12:40:44 +0200769 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200770
771 clkctl = 0;
772
Piotr Haber4a3da992013-04-11 13:28:46 +0200773 if (bus->sr_enabled) {
774 bus->clkstate = (on ? CLK_AVAIL : CLK_SDONLY);
775 return 0;
776 }
777
Arend van Spriel5b435de2011-10-05 13:19:03 +0200778 if (on) {
779 /* Request HT Avail */
780 clkreq =
781 bus->alp_only ? SBSDIO_ALP_AVAIL_REQ : SBSDIO_HT_AVAIL_REQ;
782
Arend van Spriela39be272013-12-12 11:58:58 +0100783 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
784 clkreq, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200785 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100786 brcmf_err("HT Avail request error: %d\n", err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200787 return -EBADE;
788 }
789
Arend van Spriel5b435de2011-10-05 13:19:03 +0200790 /* Check current status */
Arend van Spriela39be272013-12-12 11:58:58 +0100791 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
792 SBSDIO_FUNC1_CHIPCLKCSR, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200793 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100794 brcmf_err("HT Avail read error: %d\n", err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200795 return -EBADE;
796 }
797
798 /* Go to pending and await interrupt if appropriate */
799 if (!SBSDIO_CLKAV(clkctl, bus->alp_only) && pendok) {
800 /* Allow only clock-available interrupt */
Arend van Spriela39be272013-12-12 11:58:58 +0100801 devctl = brcmf_sdiod_regrb(bus->sdiodev,
802 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200803 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100804 brcmf_err("Devctl error setting CA: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200805 err);
806 return -EBADE;
807 }
808
809 devctl |= SBSDIO_DEVCTL_CA_INT_ONLY;
Arend van Spriela39be272013-12-12 11:58:58 +0100810 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
811 devctl, &err);
Arend van Sprielc3203372013-04-03 12:40:44 +0200812 brcmf_dbg(SDIO, "CLKCTL: set PENDING\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200813 bus->clkstate = CLK_PENDING;
814
815 return 0;
816 } else if (bus->clkstate == CLK_PENDING) {
817 /* Cancel CA-only interrupt filter */
Arend van Spriela39be272013-12-12 11:58:58 +0100818 devctl = brcmf_sdiod_regrb(bus->sdiodev,
819 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200820 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
Arend van Spriela39be272013-12-12 11:58:58 +0100821 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
822 devctl, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200823 }
824
825 /* Otherwise, wait here (polling) for HT Avail */
826 timeout = jiffies +
827 msecs_to_jiffies(PMU_MAX_TRANSITION_DLY/1000);
828 while (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
Arend van Spriela39be272013-12-12 11:58:58 +0100829 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
830 SBSDIO_FUNC1_CHIPCLKCSR,
831 &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200832 if (time_after(jiffies, timeout))
833 break;
834 else
835 usleep_range(5000, 10000);
836 }
837 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100838 brcmf_err("HT Avail request error: %d\n", err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200839 return -EBADE;
840 }
841 if (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100842 brcmf_err("HT Avail timeout (%d): clkctl 0x%02x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200843 PMU_MAX_TRANSITION_DLY, clkctl);
844 return -EBADE;
845 }
846
847 /* Mark clock available */
848 bus->clkstate = CLK_AVAIL;
Arend van Sprielc3203372013-04-03 12:40:44 +0200849 brcmf_dbg(SDIO, "CLKCTL: turned ON\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200850
Joe Perches8ae74652012-01-15 00:38:38 -0800851#if defined(DEBUG)
Joe Perches23677ce2012-02-09 11:17:23 +0000852 if (!bus->alp_only) {
Arend van Spriel5b435de2011-10-05 13:19:03 +0200853 if (SBSDIO_ALPONLY(clkctl))
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100854 brcmf_err("HT Clock should be on\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200855 }
Joe Perches8ae74652012-01-15 00:38:38 -0800856#endif /* defined (DEBUG) */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200857
Arend van Spriel5b435de2011-10-05 13:19:03 +0200858 } else {
859 clkreq = 0;
860
861 if (bus->clkstate == CLK_PENDING) {
862 /* Cancel CA-only interrupt filter */
Arend van Spriela39be272013-12-12 11:58:58 +0100863 devctl = brcmf_sdiod_regrb(bus->sdiodev,
864 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200865 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
Arend van Spriela39be272013-12-12 11:58:58 +0100866 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
867 devctl, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200868 }
869
870 bus->clkstate = CLK_SDONLY;
Arend van Spriela39be272013-12-12 11:58:58 +0100871 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
872 clkreq, &err);
Arend van Sprielc3203372013-04-03 12:40:44 +0200873 brcmf_dbg(SDIO, "CLKCTL: turned OFF\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200874 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100875 brcmf_err("Failed access turning clock off: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200876 err);
877 return -EBADE;
878 }
879 }
880 return 0;
881}
882
883/* Change idle/active SD state */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100884static int brcmf_sdio_sdclk(struct brcmf_sdio *bus, bool on)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200885{
Arend van Sprielc3203372013-04-03 12:40:44 +0200886 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200887
888 if (on)
889 bus->clkstate = CLK_SDONLY;
890 else
891 bus->clkstate = CLK_NONE;
892
893 return 0;
894}
895
896/* Transition SD and backplane clock readiness */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100897static int brcmf_sdio_clkctl(struct brcmf_sdio *bus, uint target, bool pendok)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200898{
Joe Perches8ae74652012-01-15 00:38:38 -0800899#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200900 uint oldstate = bus->clkstate;
Joe Perches8ae74652012-01-15 00:38:38 -0800901#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200902
Arend van Sprielc3203372013-04-03 12:40:44 +0200903 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200904
905 /* Early exit if we're already there */
906 if (bus->clkstate == target) {
907 if (target == CLK_AVAIL) {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100908 brcmf_sdio_wd_timer(bus, BRCMF_WD_POLL_MS);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200909 bus->activity = true;
910 }
911 return 0;
912 }
913
914 switch (target) {
915 case CLK_AVAIL:
916 /* Make sure SD clock is available */
917 if (bus->clkstate == CLK_NONE)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100918 brcmf_sdio_sdclk(bus, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200919 /* Now request HT Avail on the backplane */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100920 brcmf_sdio_htclk(bus, true, pendok);
921 brcmf_sdio_wd_timer(bus, BRCMF_WD_POLL_MS);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200922 bus->activity = true;
923 break;
924
925 case CLK_SDONLY:
926 /* Remove HT request, or bring up SD clock */
927 if (bus->clkstate == CLK_NONE)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100928 brcmf_sdio_sdclk(bus, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200929 else if (bus->clkstate == CLK_AVAIL)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100930 brcmf_sdio_htclk(bus, false, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200931 else
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100932 brcmf_err("request for %d -> %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200933 bus->clkstate, target);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100934 brcmf_sdio_wd_timer(bus, BRCMF_WD_POLL_MS);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200935 break;
936
937 case CLK_NONE:
938 /* Make sure to remove HT request */
939 if (bus->clkstate == CLK_AVAIL)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100940 brcmf_sdio_htclk(bus, false, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200941 /* Now remove the SD clock */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100942 brcmf_sdio_sdclk(bus, false);
943 brcmf_sdio_wd_timer(bus, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200944 break;
945 }
Joe Perches8ae74652012-01-15 00:38:38 -0800946#ifdef DEBUG
Arend van Sprielc3203372013-04-03 12:40:44 +0200947 brcmf_dbg(SDIO, "%d -> %d\n", oldstate, bus->clkstate);
Joe Perches8ae74652012-01-15 00:38:38 -0800948#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200949
950 return 0;
951}
952
Piotr Haber4a3da992013-04-11 13:28:46 +0200953static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100954brcmf_sdio_bus_sleep(struct brcmf_sdio *bus, bool sleep, bool pendok)
Piotr Haber4a3da992013-04-11 13:28:46 +0200955{
956 int err = 0;
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100957 u8 clkcsr;
Arend van Spriel82030d62014-01-29 15:32:20 +0100958
959 brcmf_dbg(SDIO, "Enter: request %s currently %s\n",
Piotr Haber4a3da992013-04-11 13:28:46 +0200960 (sleep ? "SLEEP" : "WAKE"),
961 (bus->sleeping ? "SLEEP" : "WAKE"));
962
963 /* If SR is enabled control bus state with KSO */
964 if (bus->sr_enabled) {
965 /* Done if we're already in the requested state */
966 if (sleep == bus->sleeping)
967 goto end;
968
969 /* Going to sleep */
970 if (sleep) {
971 /* Don't sleep if something is pending */
972 if (atomic_read(&bus->intstatus) ||
973 atomic_read(&bus->ipend) > 0 ||
974 (!atomic_read(&bus->fcstate) &&
975 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) &&
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100976 data_ok(bus))) {
977 err = -EBUSY;
978 goto done;
979 }
980
981 clkcsr = brcmf_sdiod_regrb(bus->sdiodev,
982 SBSDIO_FUNC1_CHIPCLKCSR,
983 &err);
984 if ((clkcsr & SBSDIO_CSR_MASK) == 0) {
985 brcmf_dbg(SDIO, "no clock, set ALP\n");
986 brcmf_sdiod_regwb(bus->sdiodev,
987 SBSDIO_FUNC1_CHIPCLKCSR,
988 SBSDIO_ALP_AVAIL_REQ, &err);
989 }
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100990 err = brcmf_sdio_kso_control(bus, false);
Piotr Haber4a3da992013-04-11 13:28:46 +0200991 /* disable watchdog */
992 if (!err)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100993 brcmf_sdio_wd_timer(bus, 0);
Piotr Haber4a3da992013-04-11 13:28:46 +0200994 } else {
995 bus->idlecount = 0;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100996 err = brcmf_sdio_kso_control(bus, true);
Piotr Haber4a3da992013-04-11 13:28:46 +0200997 }
998 if (!err) {
999 /* Change state */
1000 bus->sleeping = sleep;
1001 brcmf_dbg(SDIO, "new state %s\n",
1002 (sleep ? "SLEEP" : "WAKE"));
1003 } else {
1004 brcmf_err("error while changing bus sleep state %d\n",
1005 err);
Arend van Spriel8a385ba2014-03-15 17:18:20 +01001006 goto done;
Piotr Haber4a3da992013-04-11 13:28:46 +02001007 }
1008 }
1009
1010end:
1011 /* control clocks */
1012 if (sleep) {
1013 if (!bus->sr_enabled)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001014 brcmf_sdio_clkctl(bus, CLK_NONE, pendok);
Piotr Haber4a3da992013-04-11 13:28:46 +02001015 } else {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001016 brcmf_sdio_clkctl(bus, CLK_AVAIL, pendok);
Piotr Haber4a3da992013-04-11 13:28:46 +02001017 }
Arend van Spriel8a385ba2014-03-15 17:18:20 +01001018done:
1019 brcmf_dbg(SDIO, "Exit: err=%d\n", err);
Piotr Haber4a3da992013-04-11 13:28:46 +02001020 return err;
1021
1022}
1023
Daniel Kim0801e6c2014-01-29 15:32:13 +01001024#ifdef DEBUG
1025static inline bool brcmf_sdio_valid_shared_address(u32 addr)
1026{
1027 return !(addr == 0 || ((~addr >> 16) & 0xffff) == (addr & 0xffff));
1028}
1029
1030static int brcmf_sdio_readshared(struct brcmf_sdio *bus,
1031 struct sdpcm_shared *sh)
1032{
1033 u32 addr;
1034 int rv;
1035 u32 shaddr = 0;
1036 struct sdpcm_shared_le sh_le;
1037 __le32 addr_le;
1038
1039 shaddr = bus->ci->rambase + bus->ramsize - 4;
1040
1041 /*
1042 * Read last word in socram to determine
1043 * address of sdpcm_shared structure
1044 */
1045 sdio_claim_host(bus->sdiodev->func[1]);
1046 brcmf_sdio_bus_sleep(bus, false, false);
1047 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, shaddr, (u8 *)&addr_le, 4);
1048 sdio_release_host(bus->sdiodev->func[1]);
1049 if (rv < 0)
1050 return rv;
1051
1052 addr = le32_to_cpu(addr_le);
1053
1054 brcmf_dbg(SDIO, "sdpcm_shared address 0x%08X\n", addr);
1055
1056 /*
1057 * Check if addr is valid.
1058 * NVRAM length at the end of memory should have been overwritten.
1059 */
1060 if (!brcmf_sdio_valid_shared_address(addr)) {
1061 brcmf_err("invalid sdpcm_shared address 0x%08X\n",
1062 addr);
1063 return -EINVAL;
1064 }
1065
1066 /* Read hndrte_shared structure */
1067 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, (u8 *)&sh_le,
1068 sizeof(struct sdpcm_shared_le));
1069 if (rv < 0)
1070 return rv;
1071
1072 /* Endianness */
1073 sh->flags = le32_to_cpu(sh_le.flags);
1074 sh->trap_addr = le32_to_cpu(sh_le.trap_addr);
1075 sh->assert_exp_addr = le32_to_cpu(sh_le.assert_exp_addr);
1076 sh->assert_file_addr = le32_to_cpu(sh_le.assert_file_addr);
1077 sh->assert_line = le32_to_cpu(sh_le.assert_line);
1078 sh->console_addr = le32_to_cpu(sh_le.console_addr);
1079 sh->msgtrace_addr = le32_to_cpu(sh_le.msgtrace_addr);
1080
1081 if ((sh->flags & SDPCM_SHARED_VERSION_MASK) > SDPCM_SHARED_VERSION) {
1082 brcmf_err("sdpcm shared version unsupported: dhd %d dongle %d\n",
1083 SDPCM_SHARED_VERSION,
1084 sh->flags & SDPCM_SHARED_VERSION_MASK);
1085 return -EPROTO;
1086 }
1087
1088 return 0;
1089}
1090
1091static void brcmf_sdio_get_console_addr(struct brcmf_sdio *bus)
1092{
1093 struct sdpcm_shared sh;
1094
1095 if (brcmf_sdio_readshared(bus, &sh) == 0)
1096 bus->console_addr = sh.console_addr;
1097}
1098#else
1099static void brcmf_sdio_get_console_addr(struct brcmf_sdio *bus)
1100{
1101}
1102#endif /* DEBUG */
1103
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001104static u32 brcmf_sdio_hostmail(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001105{
1106 u32 intstatus = 0;
1107 u32 hmb_data;
1108 u8 fcbits;
Franky Lin58692752012-05-04 18:27:36 -07001109 int ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001110
Arend van Sprielc3203372013-04-03 12:40:44 +02001111 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001112
1113 /* Read mailbox data and ack that we did so */
Franky Lin58692752012-05-04 18:27:36 -07001114 ret = r_sdreg32(bus, &hmb_data,
1115 offsetof(struct sdpcmd_regs, tohostmailboxdata));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001116
Franky Lin58692752012-05-04 18:27:36 -07001117 if (ret == 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001118 w_sdreg32(bus, SMB_INT_ACK,
Franky Lin58692752012-05-04 18:27:36 -07001119 offsetof(struct sdpcmd_regs, tosbmailbox));
Arend van Spriel80969832012-06-09 22:51:44 +02001120 bus->sdcnt.f1regdata += 2;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001121
1122 /* Dongle recomposed rx frames, accept them again */
1123 if (hmb_data & HMB_DATA_NAKHANDLED) {
Arend van Sprielc3203372013-04-03 12:40:44 +02001124 brcmf_dbg(SDIO, "Dongle reports NAK handled, expect rtx of %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001125 bus->rx_seq);
1126 if (!bus->rxskip)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001127 brcmf_err("unexpected NAKHANDLED!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001128
1129 bus->rxskip = false;
1130 intstatus |= I_HMB_FRAME_IND;
1131 }
1132
1133 /*
1134 * DEVREADY does not occur with gSPI.
1135 */
1136 if (hmb_data & (HMB_DATA_DEVREADY | HMB_DATA_FWREADY)) {
1137 bus->sdpcm_ver =
1138 (hmb_data & HMB_DATA_VERSION_MASK) >>
1139 HMB_DATA_VERSION_SHIFT;
1140 if (bus->sdpcm_ver != SDPCM_PROT_VERSION)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001141 brcmf_err("Version mismatch, dongle reports %d, "
Arend van Spriel5b435de2011-10-05 13:19:03 +02001142 "expecting %d\n",
1143 bus->sdpcm_ver, SDPCM_PROT_VERSION);
1144 else
Arend van Sprielc3203372013-04-03 12:40:44 +02001145 brcmf_dbg(SDIO, "Dongle ready, protocol version %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001146 bus->sdpcm_ver);
Daniel Kim0801e6c2014-01-29 15:32:13 +01001147
1148 /*
1149 * Retrieve console state address now that firmware should have
1150 * updated it.
1151 */
1152 brcmf_sdio_get_console_addr(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001153 }
1154
1155 /*
1156 * Flow Control has been moved into the RX headers and this out of band
1157 * method isn't used any more.
1158 * remaining backward compatible with older dongles.
1159 */
1160 if (hmb_data & HMB_DATA_FC) {
1161 fcbits = (hmb_data & HMB_DATA_FCDATA_MASK) >>
1162 HMB_DATA_FCDATA_SHIFT;
1163
1164 if (fcbits & ~bus->flowcontrol)
Arend van Spriel80969832012-06-09 22:51:44 +02001165 bus->sdcnt.fc_xoff++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001166
1167 if (bus->flowcontrol & ~fcbits)
Arend van Spriel80969832012-06-09 22:51:44 +02001168 bus->sdcnt.fc_xon++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001169
Arend van Spriel80969832012-06-09 22:51:44 +02001170 bus->sdcnt.fc_rcvd++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001171 bus->flowcontrol = fcbits;
1172 }
1173
1174 /* Shouldn't be any others */
1175 if (hmb_data & ~(HMB_DATA_DEVREADY |
1176 HMB_DATA_NAKHANDLED |
1177 HMB_DATA_FC |
1178 HMB_DATA_FWREADY |
1179 HMB_DATA_FCDATA_MASK | HMB_DATA_VERSION_MASK))
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001180 brcmf_err("Unknown mailbox data content: 0x%02x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001181 hmb_data);
1182
1183 return intstatus;
1184}
1185
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001186static void brcmf_sdio_rxfail(struct brcmf_sdio *bus, bool abort, bool rtx)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001187{
1188 uint retries = 0;
1189 u16 lastrbc;
1190 u8 hi, lo;
1191 int err;
1192
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001193 brcmf_err("%sterminate frame%s\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001194 abort ? "abort command, " : "",
1195 rtx ? ", send NAK" : "");
1196
1197 if (abort)
Arend van Spriela39be272013-12-12 11:58:58 +01001198 brcmf_sdiod_abort(bus->sdiodev, SDIO_FUNC_2);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001199
Arend van Spriela39be272013-12-12 11:58:58 +01001200 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL,
1201 SFC_RF_TERM, &err);
Arend van Spriel80969832012-06-09 22:51:44 +02001202 bus->sdcnt.f1regdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001203
1204 /* Wait until the packet has been flushed (device/FIFO stable) */
1205 for (lastrbc = retries = 0xffff; retries > 0; retries--) {
Arend van Spriela39be272013-12-12 11:58:58 +01001206 hi = brcmf_sdiod_regrb(bus->sdiodev,
1207 SBSDIO_FUNC1_RFRAMEBCHI, &err);
1208 lo = brcmf_sdiod_regrb(bus->sdiodev,
1209 SBSDIO_FUNC1_RFRAMEBCLO, &err);
Arend van Spriel80969832012-06-09 22:51:44 +02001210 bus->sdcnt.f1regdata += 2;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001211
1212 if ((hi == 0) && (lo == 0))
1213 break;
1214
1215 if ((hi > (lastrbc >> 8)) && (lo > (lastrbc & 0x00ff))) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001216 brcmf_err("count growing: last 0x%04x now 0x%04x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001217 lastrbc, (hi << 8) + lo);
1218 }
1219 lastrbc = (hi << 8) + lo;
1220 }
1221
1222 if (!retries)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001223 brcmf_err("count never zeroed: last 0x%04x\n", lastrbc);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001224 else
Arend van Sprielc3203372013-04-03 12:40:44 +02001225 brcmf_dbg(SDIO, "flush took %d iterations\n", 0xffff - retries);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001226
1227 if (rtx) {
Arend van Spriel80969832012-06-09 22:51:44 +02001228 bus->sdcnt.rxrtx++;
Franky Lin58692752012-05-04 18:27:36 -07001229 err = w_sdreg32(bus, SMB_NAK,
1230 offsetof(struct sdpcmd_regs, tosbmailbox));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001231
Arend van Spriel80969832012-06-09 22:51:44 +02001232 bus->sdcnt.f1regdata++;
Franky Lin58692752012-05-04 18:27:36 -07001233 if (err == 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001234 bus->rxskip = true;
1235 }
1236
1237 /* Clear partial in any case */
Franky Lin4754fce2012-09-19 22:21:11 +02001238 bus->cur_read.len = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001239}
1240
Hante Meuleman81c78832014-02-25 20:30:35 +01001241static void brcmf_sdio_txfail(struct brcmf_sdio *bus)
1242{
1243 struct brcmf_sdio_dev *sdiodev = bus->sdiodev;
1244 u8 i, hi, lo;
1245
1246 /* On failure, abort the command and terminate the frame */
1247 brcmf_err("sdio error, abort command and terminate frame\n");
1248 bus->sdcnt.tx_sderrs++;
1249
1250 brcmf_sdiod_abort(sdiodev, SDIO_FUNC_2);
1251 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_FRAMECTRL, SFC_WF_TERM, NULL);
1252 bus->sdcnt.f1regdata++;
1253
1254 for (i = 0; i < 3; i++) {
1255 hi = brcmf_sdiod_regrb(sdiodev, SBSDIO_FUNC1_WFRAMEBCHI, NULL);
1256 lo = brcmf_sdiod_regrb(sdiodev, SBSDIO_FUNC1_WFRAMEBCLO, NULL);
1257 bus->sdcnt.f1regdata += 2;
1258 if ((hi == 0) && (lo == 0))
1259 break;
1260 }
1261}
1262
Arend van Spriel9a95e602011-11-10 20:30:29 +01001263/* return total length of buffer chain */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001264static uint brcmf_sdio_glom_len(struct brcmf_sdio *bus)
Arend van Spriel9a95e602011-11-10 20:30:29 +01001265{
1266 struct sk_buff *p;
1267 uint total;
1268
1269 total = 0;
1270 skb_queue_walk(&bus->glom, p)
1271 total += p->len;
1272 return total;
1273}
1274
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001275static void brcmf_sdio_free_glom(struct brcmf_sdio *bus)
Arend van Spriel046808d2011-11-10 20:30:31 +01001276{
1277 struct sk_buff *cur, *next;
1278
1279 skb_queue_walk_safe(&bus->glom, cur, next) {
1280 skb_unlink(cur, &bus->glom);
1281 brcmu_pkt_buf_free_skb(cur);
1282 }
1283}
1284
Franky Lin6bc52312013-08-10 12:27:28 +02001285/**
1286 * brcmfmac sdio bus specific header
1287 * This is the lowest layer header wrapped on the packets transmitted between
1288 * host and WiFi dongle which contains information needed for SDIO core and
1289 * firmware
1290 *
Franky Lin8da9d2c2013-11-29 12:25:10 +01001291 * It consists of 3 parts: hardware header, hardware extension header and
1292 * software header
Franky Lin6bc52312013-08-10 12:27:28 +02001293 * hardware header (frame tag) - 4 bytes
1294 * Byte 0~1: Frame length
1295 * Byte 2~3: Checksum, bit-wise inverse of frame length
Franky Lin8da9d2c2013-11-29 12:25:10 +01001296 * hardware extension header - 8 bytes
1297 * Tx glom mode only, N/A for Rx or normal Tx
1298 * Byte 0~1: Packet length excluding hw frame tag
1299 * Byte 2: Reserved
1300 * Byte 3: Frame flags, bit 0: last frame indication
1301 * Byte 4~5: Reserved
1302 * Byte 6~7: Tail padding length
Franky Lin6bc52312013-08-10 12:27:28 +02001303 * software header - 8 bytes
1304 * Byte 0: Rx/Tx sequence number
1305 * Byte 1: 4 MSB Channel number, 4 LSB arbitrary flag
1306 * Byte 2: Length of next data frame, reserved for Tx
1307 * Byte 3: Data offset
1308 * Byte 4: Flow control bits, reserved for Tx
1309 * Byte 5: Maximum Sequence number allowed by firmware for Tx, N/A for Tx packet
1310 * Byte 6~7: Reserved
1311 */
1312#define SDPCM_HWHDR_LEN 4
Franky Lin8da9d2c2013-11-29 12:25:10 +01001313#define SDPCM_HWEXT_LEN 8
Franky Lin6bc52312013-08-10 12:27:28 +02001314#define SDPCM_SWHDR_LEN 8
1315#define SDPCM_HDRLEN (SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN)
Franky Lin6bc52312013-08-10 12:27:28 +02001316/* software header */
1317#define SDPCM_SEQ_MASK 0x000000ff
1318#define SDPCM_SEQ_WRAP 256
1319#define SDPCM_CHANNEL_MASK 0x00000f00
1320#define SDPCM_CHANNEL_SHIFT 8
1321#define SDPCM_CONTROL_CHANNEL 0 /* Control */
1322#define SDPCM_EVENT_CHANNEL 1 /* Asyc Event Indication */
1323#define SDPCM_DATA_CHANNEL 2 /* Data Xmit/Recv */
1324#define SDPCM_GLOM_CHANNEL 3 /* Coalesced packets */
1325#define SDPCM_TEST_CHANNEL 15 /* Test/debug packets */
1326#define SDPCM_GLOMDESC(p) (((u8 *)p)[1] & 0x80)
1327#define SDPCM_NEXTLEN_MASK 0x00ff0000
1328#define SDPCM_NEXTLEN_SHIFT 16
1329#define SDPCM_DOFFSET_MASK 0xff000000
1330#define SDPCM_DOFFSET_SHIFT 24
1331#define SDPCM_FCMASK_MASK 0x000000ff
1332#define SDPCM_WINDOW_MASK 0x0000ff00
1333#define SDPCM_WINDOW_SHIFT 8
1334
1335static inline u8 brcmf_sdio_getdatoffset(u8 *swheader)
1336{
1337 u32 hdrvalue;
1338 hdrvalue = *(u32 *)swheader;
1339 return (u8)((hdrvalue & SDPCM_DOFFSET_MASK) >> SDPCM_DOFFSET_SHIFT);
1340}
1341
1342static int brcmf_sdio_hdparse(struct brcmf_sdio *bus, u8 *header,
1343 struct brcmf_sdio_hdrinfo *rd,
1344 enum brcmf_sdio_frmtype type)
Franky Lin4754fce2012-09-19 22:21:11 +02001345{
1346 u16 len, checksum;
1347 u8 rx_seq, fc, tx_seq_max;
Franky Lin6bc52312013-08-10 12:27:28 +02001348 u32 swheader;
Franky Lin4754fce2012-09-19 22:21:11 +02001349
Arend van Spriel4b776962013-11-29 12:25:06 +01001350 trace_brcmf_sdpcm_hdr(SDPCM_RX, header);
Arend van Spriel76584ec2013-10-15 15:44:47 +02001351
Franky Lin6bc52312013-08-10 12:27:28 +02001352 /* hw header */
Franky Lin4754fce2012-09-19 22:21:11 +02001353 len = get_unaligned_le16(header);
1354 checksum = get_unaligned_le16(header + sizeof(u16));
1355 /* All zero means no more to read */
1356 if (!(len | checksum)) {
1357 bus->rxpending = false;
Franky Lin10510582012-11-05 16:22:26 -08001358 return -ENODATA;
Franky Lin4754fce2012-09-19 22:21:11 +02001359 }
1360 if ((u16)(~(len ^ checksum))) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001361 brcmf_err("HW header checksum error\n");
Franky Lin4754fce2012-09-19 22:21:11 +02001362 bus->sdcnt.rx_badhdr++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001363 brcmf_sdio_rxfail(bus, false, false);
Franky Lin10510582012-11-05 16:22:26 -08001364 return -EIO;
Franky Lin4754fce2012-09-19 22:21:11 +02001365 }
1366 if (len < SDPCM_HDRLEN) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001367 brcmf_err("HW header length error\n");
Franky Lin10510582012-11-05 16:22:26 -08001368 return -EPROTO;
Franky Lin4754fce2012-09-19 22:21:11 +02001369 }
Franky Lin9d7d6f92012-10-22 10:36:24 -07001370 if (type == BRCMF_SDIO_FT_SUPER &&
1371 (roundup(len, bus->blocksize) != rd->len)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001372 brcmf_err("HW superframe header length error\n");
Franky Lin10510582012-11-05 16:22:26 -08001373 return -EPROTO;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001374 }
1375 if (type == BRCMF_SDIO_FT_SUB && len > rd->len) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001376 brcmf_err("HW subframe header length error\n");
Franky Lin10510582012-11-05 16:22:26 -08001377 return -EPROTO;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001378 }
Franky Lin4754fce2012-09-19 22:21:11 +02001379 rd->len = len;
1380
Franky Lin6bc52312013-08-10 12:27:28 +02001381 /* software header */
1382 header += SDPCM_HWHDR_LEN;
1383 swheader = le32_to_cpu(*(__le32 *)header);
1384 if (type == BRCMF_SDIO_FT_SUPER && SDPCM_GLOMDESC(header)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001385 brcmf_err("Glom descriptor found in superframe head\n");
Franky Lin9d7d6f92012-10-22 10:36:24 -07001386 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001387 return -EINVAL;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001388 }
Franky Lin6bc52312013-08-10 12:27:28 +02001389 rx_seq = (u8)(swheader & SDPCM_SEQ_MASK);
1390 rd->channel = (swheader & SDPCM_CHANNEL_MASK) >> SDPCM_CHANNEL_SHIFT;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001391 if (len > MAX_RX_DATASZ && rd->channel != SDPCM_CONTROL_CHANNEL &&
1392 type != BRCMF_SDIO_FT_SUPER) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001393 brcmf_err("HW header length too long\n");
Franky Lin4754fce2012-09-19 22:21:11 +02001394 bus->sdcnt.rx_toolong++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001395 brcmf_sdio_rxfail(bus, false, false);
Franky Lin4754fce2012-09-19 22:21:11 +02001396 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001397 return -EPROTO;
Franky Lin4754fce2012-09-19 22:21:11 +02001398 }
Franky Lin9d7d6f92012-10-22 10:36:24 -07001399 if (type == BRCMF_SDIO_FT_SUPER && rd->channel != SDPCM_GLOM_CHANNEL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001400 brcmf_err("Wrong channel for superframe\n");
Franky Lin9d7d6f92012-10-22 10:36:24 -07001401 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001402 return -EINVAL;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001403 }
1404 if (type == BRCMF_SDIO_FT_SUB && rd->channel != SDPCM_DATA_CHANNEL &&
1405 rd->channel != SDPCM_EVENT_CHANNEL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001406 brcmf_err("Wrong channel for subframe\n");
Franky Lin9d7d6f92012-10-22 10:36:24 -07001407 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001408 return -EINVAL;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001409 }
Franky Lin6bc52312013-08-10 12:27:28 +02001410 rd->dat_offset = brcmf_sdio_getdatoffset(header);
Franky Lin4754fce2012-09-19 22:21:11 +02001411 if (rd->dat_offset < SDPCM_HDRLEN || rd->dat_offset > rd->len) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001412 brcmf_err("seq %d: bad data offset\n", rx_seq);
Franky Lin4754fce2012-09-19 22:21:11 +02001413 bus->sdcnt.rx_badhdr++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001414 brcmf_sdio_rxfail(bus, false, false);
Franky Lin4754fce2012-09-19 22:21:11 +02001415 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001416 return -ENXIO;
Franky Lin4754fce2012-09-19 22:21:11 +02001417 }
1418 if (rd->seq_num != rx_seq) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001419 brcmf_err("seq %d: sequence number error, expect %d\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001420 rx_seq, rd->seq_num);
1421 bus->sdcnt.rx_badseq++;
1422 rd->seq_num = rx_seq;
1423 }
Franky Lin9d7d6f92012-10-22 10:36:24 -07001424 /* no need to check the reset for subframe */
1425 if (type == BRCMF_SDIO_FT_SUB)
Franky Lin10510582012-11-05 16:22:26 -08001426 return 0;
Franky Lin6bc52312013-08-10 12:27:28 +02001427 rd->len_nxtfrm = (swheader & SDPCM_NEXTLEN_MASK) >> SDPCM_NEXTLEN_SHIFT;
Franky Lin4754fce2012-09-19 22:21:11 +02001428 if (rd->len_nxtfrm << 4 > MAX_RX_DATASZ) {
1429 /* only warm for NON glom packet */
1430 if (rd->channel != SDPCM_GLOM_CHANNEL)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001431 brcmf_err("seq %d: next length error\n", rx_seq);
Franky Lin4754fce2012-09-19 22:21:11 +02001432 rd->len_nxtfrm = 0;
1433 }
Franky Lin6bc52312013-08-10 12:27:28 +02001434 swheader = le32_to_cpu(*(__le32 *)(header + 4));
1435 fc = swheader & SDPCM_FCMASK_MASK;
Franky Lin4754fce2012-09-19 22:21:11 +02001436 if (bus->flowcontrol != fc) {
1437 if (~bus->flowcontrol & fc)
1438 bus->sdcnt.fc_xoff++;
1439 if (bus->flowcontrol & ~fc)
1440 bus->sdcnt.fc_xon++;
1441 bus->sdcnt.fc_rcvd++;
1442 bus->flowcontrol = fc;
1443 }
Franky Lin6bc52312013-08-10 12:27:28 +02001444 tx_seq_max = (swheader & SDPCM_WINDOW_MASK) >> SDPCM_WINDOW_SHIFT;
Franky Lin4754fce2012-09-19 22:21:11 +02001445 if ((u8)(tx_seq_max - bus->tx_seq) > 0x40) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001446 brcmf_err("seq %d: max tx seq number error\n", rx_seq);
Franky Lin4754fce2012-09-19 22:21:11 +02001447 tx_seq_max = bus->tx_seq + 2;
1448 }
1449 bus->tx_max = tx_seq_max;
1450
Franky Lin10510582012-11-05 16:22:26 -08001451 return 0;
Franky Lin4754fce2012-09-19 22:21:11 +02001452}
1453
Franky Lin6bc52312013-08-10 12:27:28 +02001454static inline void brcmf_sdio_update_hwhdr(u8 *header, u16 frm_length)
1455{
1456 *(__le16 *)header = cpu_to_le16(frm_length);
1457 *(((__le16 *)header) + 1) = cpu_to_le16(~frm_length);
1458}
1459
1460static void brcmf_sdio_hdpack(struct brcmf_sdio *bus, u8 *header,
1461 struct brcmf_sdio_hdrinfo *hd_info)
1462{
Franky Lin8da9d2c2013-11-29 12:25:10 +01001463 u32 hdrval;
1464 u8 hdr_offset;
Franky Lin6bc52312013-08-10 12:27:28 +02001465
1466 brcmf_sdio_update_hwhdr(header, hd_info->len);
Franky Lin8da9d2c2013-11-29 12:25:10 +01001467 hdr_offset = SDPCM_HWHDR_LEN;
Franky Lin6bc52312013-08-10 12:27:28 +02001468
Franky Lin8da9d2c2013-11-29 12:25:10 +01001469 if (bus->txglom) {
1470 hdrval = (hd_info->len - hdr_offset) | (hd_info->lastfrm << 24);
1471 *((__le32 *)(header + hdr_offset)) = cpu_to_le32(hdrval);
1472 hdrval = (u16)hd_info->tail_pad << 16;
1473 *(((__le32 *)(header + hdr_offset)) + 1) = cpu_to_le32(hdrval);
1474 hdr_offset += SDPCM_HWEXT_LEN;
1475 }
1476
1477 hdrval = hd_info->seq_num;
1478 hdrval |= (hd_info->channel << SDPCM_CHANNEL_SHIFT) &
1479 SDPCM_CHANNEL_MASK;
1480 hdrval |= (hd_info->dat_offset << SDPCM_DOFFSET_SHIFT) &
1481 SDPCM_DOFFSET_MASK;
1482 *((__le32 *)(header + hdr_offset)) = cpu_to_le32(hdrval);
1483 *(((__le32 *)(header + hdr_offset)) + 1) = 0;
1484 trace_brcmf_sdpcm_hdr(SDPCM_TX + !!(bus->txglom), header);
Franky Lin6bc52312013-08-10 12:27:28 +02001485}
1486
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001487static u8 brcmf_sdio_rxglom(struct brcmf_sdio *bus, u8 rxseq)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001488{
1489 u16 dlen, totlen;
1490 u8 *dptr, num = 0;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001491 u16 sublen;
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001492 struct sk_buff *pfirst, *pnext;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001493
1494 int errcode;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001495 u8 doff, sfdoff;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001496
Franky Lin6bc52312013-08-10 12:27:28 +02001497 struct brcmf_sdio_hdrinfo rd_new;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001498
1499 /* If packets, issue read(s) and send up packet chain */
1500 /* Return sequence numbers consumed? */
1501
Arend van Sprielc3203372013-04-03 12:40:44 +02001502 brcmf_dbg(SDIO, "start: glomd %p glom %p\n",
Arend van Sprielb83db862011-10-19 12:51:09 +02001503 bus->glomd, skb_peek(&bus->glom));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001504
1505 /* If there's a descriptor, generate the packet chain */
1506 if (bus->glomd) {
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001507 pfirst = pnext = NULL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001508 dlen = (u16) (bus->glomd->len);
1509 dptr = bus->glomd->data;
1510 if (!dlen || (dlen & 1)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001511 brcmf_err("bad glomd len(%d), ignore descriptor\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001512 dlen);
1513 dlen = 0;
1514 }
1515
1516 for (totlen = num = 0; dlen; num++) {
1517 /* Get (and move past) next length */
1518 sublen = get_unaligned_le16(dptr);
1519 dlen -= sizeof(u16);
1520 dptr += sizeof(u16);
1521 if ((sublen < SDPCM_HDRLEN) ||
1522 ((num == 0) && (sublen < (2 * SDPCM_HDRLEN)))) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001523 brcmf_err("descriptor len %d bad: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001524 num, sublen);
1525 pnext = NULL;
1526 break;
1527 }
Arend van Spriele217d1c2013-11-29 12:25:13 +01001528 if (sublen % bus->sgentry_align) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001529 brcmf_err("sublen %d not multiple of %d\n",
Arend van Spriele217d1c2013-11-29 12:25:13 +01001530 sublen, bus->sgentry_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001531 }
1532 totlen += sublen;
1533
1534 /* For last frame, adjust read len so total
1535 is a block multiple */
1536 if (!dlen) {
1537 sublen +=
1538 (roundup(totlen, bus->blocksize) - totlen);
1539 totlen = roundup(totlen, bus->blocksize);
1540 }
1541
1542 /* Allocate/chain packet for next subframe */
Arend van Spriele217d1c2013-11-29 12:25:13 +01001543 pnext = brcmu_pkt_buf_get_skb(sublen + bus->sgentry_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001544 if (pnext == NULL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001545 brcmf_err("bcm_pkt_buf_get_skb failed, num %d len %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001546 num, sublen);
1547 break;
1548 }
Arend van Sprielb83db862011-10-19 12:51:09 +02001549 skb_queue_tail(&bus->glom, pnext);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001550
1551 /* Adhere to start alignment requirements */
Arend van Spriele217d1c2013-11-29 12:25:13 +01001552 pkt_align(pnext, sublen, bus->sgentry_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001553 }
1554
1555 /* If all allocations succeeded, save packet chain
1556 in bus structure */
1557 if (pnext) {
1558 brcmf_dbg(GLOM, "allocated %d-byte packet chain for %d subframes\n",
1559 totlen, num);
Franky Lin4754fce2012-09-19 22:21:11 +02001560 if (BRCMF_GLOM_ON() && bus->cur_read.len &&
1561 totlen != bus->cur_read.len) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001562 brcmf_dbg(GLOM, "glomdesc mismatch: nextlen %d glomdesc %d rxseq %d\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001563 bus->cur_read.len, totlen, rxseq);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001564 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02001565 pfirst = pnext = NULL;
1566 } else {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001567 brcmf_sdio_free_glom(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001568 num = 0;
1569 }
1570
1571 /* Done with descriptor packet */
1572 brcmu_pkt_buf_free_skb(bus->glomd);
1573 bus->glomd = NULL;
Franky Lin4754fce2012-09-19 22:21:11 +02001574 bus->cur_read.len = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001575 }
1576
1577 /* Ok -- either we just generated a packet chain,
1578 or had one from before */
Arend van Sprielb83db862011-10-19 12:51:09 +02001579 if (!skb_queue_empty(&bus->glom)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001580 if (BRCMF_GLOM_ON()) {
1581 brcmf_dbg(GLOM, "try superframe read, packet chain:\n");
Arend van Sprielb83db862011-10-19 12:51:09 +02001582 skb_queue_walk(&bus->glom, pnext) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001583 brcmf_dbg(GLOM, " %p: %p len 0x%04x (%d)\n",
1584 pnext, (u8 *) (pnext->data),
1585 pnext->len, pnext->len);
1586 }
1587 }
1588
Arend van Sprielb83db862011-10-19 12:51:09 +02001589 pfirst = skb_peek(&bus->glom);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001590 dlen = (u16) brcmf_sdio_glom_len(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001591
1592 /* Do an SDIO read for the superframe. Configurable iovar to
1593 * read directly into the chained packet, or allocate a large
1594 * packet and and copy into the chain.
1595 */
Franky Lin38b0b0d2012-11-05 16:22:24 -08001596 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriela39be272013-12-12 11:58:58 +01001597 errcode = brcmf_sdiod_recv_chain(bus->sdiodev,
Arend van Spriela39be272013-12-12 11:58:58 +01001598 &bus->glom, dlen);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001599 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel80969832012-06-09 22:51:44 +02001600 bus->sdcnt.f2rxdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001601
1602 /* On failure, kill the superframe, allow a couple retries */
1603 if (errcode < 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001604 brcmf_err("glom read of %d bytes failed: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001605 dlen, errcode);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001606
Franky Lin38b0b0d2012-11-05 16:22:24 -08001607 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001608 if (bus->glomerr++ < 3) {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001609 brcmf_sdio_rxfail(bus, true, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001610 } else {
1611 bus->glomerr = 0;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001612 brcmf_sdio_rxfail(bus, true, false);
Arend van Spriel80969832012-06-09 22:51:44 +02001613 bus->sdcnt.rxglomfail++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001614 brcmf_sdio_free_glom(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001615 }
Franky Lin38b0b0d2012-11-05 16:22:24 -08001616 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001617 return 0;
1618 }
Joe Perches1e023822012-01-15 00:38:40 -08001619
1620 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
1621 pfirst->data, min_t(int, pfirst->len, 48),
1622 "SUPERFRAME:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001623
Franky Lin9d7d6f92012-10-22 10:36:24 -07001624 rd_new.seq_num = rxseq;
1625 rd_new.len = dlen;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001626 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin6bc52312013-08-10 12:27:28 +02001627 errcode = brcmf_sdio_hdparse(bus, pfirst->data, &rd_new,
1628 BRCMF_SDIO_FT_SUPER);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001629 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin9d7d6f92012-10-22 10:36:24 -07001630 bus->cur_read.len = rd_new.len_nxtfrm << 4;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001631
1632 /* Remove superframe header, remember offset */
Franky Lin9d7d6f92012-10-22 10:36:24 -07001633 skb_pull(pfirst, rd_new.dat_offset);
1634 sfdoff = rd_new.dat_offset;
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001635 num = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001636
1637 /* Validate all the subframe headers */
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001638 skb_queue_walk(&bus->glom, pnext) {
1639 /* leave when invalid subframe is found */
1640 if (errcode)
1641 break;
1642
Franky Lin9d7d6f92012-10-22 10:36:24 -07001643 rd_new.len = pnext->len;
1644 rd_new.seq_num = rxseq++;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001645 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin6bc52312013-08-10 12:27:28 +02001646 errcode = brcmf_sdio_hdparse(bus, pnext->data, &rd_new,
1647 BRCMF_SDIO_FT_SUB);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001648 sdio_release_host(bus->sdiodev->func[1]);
Joe Perches1e023822012-01-15 00:38:40 -08001649 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
Franky Lin9d7d6f92012-10-22 10:36:24 -07001650 pnext->data, 32, "subframe:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001651
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001652 num++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001653 }
1654
1655 if (errcode) {
1656 /* Terminate frame on error, request
1657 a couple retries */
Franky Lin38b0b0d2012-11-05 16:22:24 -08001658 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001659 if (bus->glomerr++ < 3) {
1660 /* Restore superframe header space */
1661 skb_push(pfirst, sfdoff);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001662 brcmf_sdio_rxfail(bus, true, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001663 } else {
1664 bus->glomerr = 0;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001665 brcmf_sdio_rxfail(bus, true, false);
Arend van Spriel80969832012-06-09 22:51:44 +02001666 bus->sdcnt.rxglomfail++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001667 brcmf_sdio_free_glom(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001668 }
Franky Lin38b0b0d2012-11-05 16:22:24 -08001669 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001670 bus->cur_read.len = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001671 return 0;
1672 }
1673
1674 /* Basic SD framing looks ok - process each packet (header) */
Arend van Spriel5b435de2011-10-05 13:19:03 +02001675
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001676 skb_queue_walk_safe(&bus->glom, pfirst, pnext) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001677 dptr = (u8 *) (pfirst->data);
1678 sublen = get_unaligned_le16(dptr);
Franky Lin6bc52312013-08-10 12:27:28 +02001679 doff = brcmf_sdio_getdatoffset(&dptr[SDPCM_HWHDR_LEN]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001680
Joe Perches1e023822012-01-15 00:38:40 -08001681 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_DATA_ON(),
Franky Lin9d7d6f92012-10-22 10:36:24 -07001682 dptr, pfirst->len,
1683 "Rx Subframe Data:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001684
1685 __skb_trim(pfirst, sublen);
1686 skb_pull(pfirst, doff);
1687
1688 if (pfirst->len == 0) {
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001689 skb_unlink(pfirst, &bus->glom);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001690 brcmu_pkt_buf_free_skb(pfirst);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001691 continue;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001692 }
1693
Joe Perches1e023822012-01-15 00:38:40 -08001694 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
1695 pfirst->data,
1696 min_t(int, pfirst->len, 32),
1697 "subframe %d to stack, %p (%p/%d) nxt/lnk %p/%p\n",
1698 bus->glom.qlen, pfirst, pfirst->data,
1699 pfirst->len, pfirst->next,
1700 pfirst->prev);
Arend van Spriel05f38202013-09-25 13:05:46 +02001701 skb_unlink(pfirst, &bus->glom);
1702 brcmf_rx_frame(bus->sdiodev->dev, pfirst);
1703 bus->sdcnt.rxglompkts++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001704 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02001705
Arend van Spriel80969832012-06-09 22:51:44 +02001706 bus->sdcnt.rxglomframes++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001707 }
1708 return num;
1709}
1710
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001711static int brcmf_sdio_dcmd_resp_wait(struct brcmf_sdio *bus, uint *condition,
1712 bool *pending)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001713{
1714 DECLARE_WAITQUEUE(wait, current);
1715 int timeout = msecs_to_jiffies(DCMD_RESP_TIMEOUT);
1716
1717 /* Wait until control frame is available */
1718 add_wait_queue(&bus->dcmd_resp_wait, &wait);
1719 set_current_state(TASK_INTERRUPTIBLE);
1720
1721 while (!(*condition) && (!signal_pending(current) && timeout))
1722 timeout = schedule_timeout(timeout);
1723
1724 if (signal_pending(current))
1725 *pending = true;
1726
1727 set_current_state(TASK_RUNNING);
1728 remove_wait_queue(&bus->dcmd_resp_wait, &wait);
1729
1730 return timeout;
1731}
1732
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001733static int brcmf_sdio_dcmd_resp_wake(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001734{
1735 if (waitqueue_active(&bus->dcmd_resp_wait))
1736 wake_up_interruptible(&bus->dcmd_resp_wait);
1737
1738 return 0;
1739}
1740static void
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001741brcmf_sdio_read_control(struct brcmf_sdio *bus, u8 *hdr, uint len, uint doff)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001742{
1743 uint rdlen, pad;
Franky Lindd43a012012-11-05 16:22:22 -08001744 u8 *buf = NULL, *rbuf;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001745 int sdret;
1746
1747 brcmf_dbg(TRACE, "Enter\n");
1748
Franky Lindd43a012012-11-05 16:22:22 -08001749 if (bus->rxblen)
1750 buf = vzalloc(bus->rxblen);
Joe Perches14f8dc42013-02-07 11:46:27 +00001751 if (!buf)
Franky Lindd43a012012-11-05 16:22:22 -08001752 goto done;
Joe Perches14f8dc42013-02-07 11:46:27 +00001753
Franky Lindd43a012012-11-05 16:22:22 -08001754 rbuf = bus->rxbuf;
Arend van Spriel9b2d2f2a82013-11-29 12:25:14 +01001755 pad = ((unsigned long)rbuf % bus->head_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001756 if (pad)
Arend van Spriel9b2d2f2a82013-11-29 12:25:14 +01001757 rbuf += (bus->head_align - pad);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001758
1759 /* Copy the already-read portion over */
Franky Lindd43a012012-11-05 16:22:22 -08001760 memcpy(buf, hdr, BRCMF_FIRSTREAD);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001761 if (len <= BRCMF_FIRSTREAD)
1762 goto gotpkt;
1763
1764 /* Raise rdlen to next SDIO block to avoid tail command */
1765 rdlen = len - BRCMF_FIRSTREAD;
1766 if (bus->roundup && bus->blocksize && (rdlen > bus->blocksize)) {
1767 pad = bus->blocksize - (rdlen % bus->blocksize);
1768 if ((pad <= bus->roundup) && (pad < bus->blocksize) &&
Franky Linb01a6b32011-12-16 18:37:03 -08001769 ((len + pad) < bus->sdiodev->bus_if->maxctl))
Arend van Spriel5b435de2011-10-05 13:19:03 +02001770 rdlen += pad;
Arend van Spriel9b2d2f2a82013-11-29 12:25:14 +01001771 } else if (rdlen % bus->head_align) {
1772 rdlen += bus->head_align - (rdlen % bus->head_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001773 }
1774
Arend van Spriel5b435de2011-10-05 13:19:03 +02001775 /* Drop if the read is too big or it exceeds our maximum */
Franky Linb01a6b32011-12-16 18:37:03 -08001776 if ((rdlen + BRCMF_FIRSTREAD) > bus->sdiodev->bus_if->maxctl) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001777 brcmf_err("%d-byte control read exceeds %d-byte buffer\n",
Franky Linb01a6b32011-12-16 18:37:03 -08001778 rdlen, bus->sdiodev->bus_if->maxctl);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001779 brcmf_sdio_rxfail(bus, false, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001780 goto done;
1781 }
1782
Franky Linb01a6b32011-12-16 18:37:03 -08001783 if ((len - doff) > bus->sdiodev->bus_if->maxctl) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001784 brcmf_err("%d-byte ctl frame (%d-byte ctl data) exceeds %d-byte limit\n",
Franky Linb01a6b32011-12-16 18:37:03 -08001785 len, len - doff, bus->sdiodev->bus_if->maxctl);
Arend van Spriel80969832012-06-09 22:51:44 +02001786 bus->sdcnt.rx_toolong++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001787 brcmf_sdio_rxfail(bus, false, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001788 goto done;
1789 }
1790
Franky Lindd43a012012-11-05 16:22:22 -08001791 /* Read remain of frame body */
Arend van Spriela7cdd822013-12-12 11:58:59 +01001792 sdret = brcmf_sdiod_recv_buf(bus->sdiodev, rbuf, rdlen);
Arend van Spriel80969832012-06-09 22:51:44 +02001793 bus->sdcnt.f2rxdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001794
1795 /* Control frame failures need retransmission */
1796 if (sdret < 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001797 brcmf_err("read %d control bytes failed: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001798 rdlen, sdret);
Arend van Spriel80969832012-06-09 22:51:44 +02001799 bus->sdcnt.rxc_errors++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001800 brcmf_sdio_rxfail(bus, true, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001801 goto done;
Franky Lindd43a012012-11-05 16:22:22 -08001802 } else
1803 memcpy(buf + BRCMF_FIRSTREAD, rbuf, rdlen);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001804
1805gotpkt:
1806
Joe Perches1e023822012-01-15 00:38:40 -08001807 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_CTL_ON(),
Franky Lindd43a012012-11-05 16:22:22 -08001808 buf, len, "RxCtrl:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001809
1810 /* Point to valid data and indicate its length */
Franky Lindd43a012012-11-05 16:22:22 -08001811 spin_lock_bh(&bus->rxctl_lock);
1812 if (bus->rxctl) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001813 brcmf_err("last control frame is being processed.\n");
Franky Lindd43a012012-11-05 16:22:22 -08001814 spin_unlock_bh(&bus->rxctl_lock);
1815 vfree(buf);
1816 goto done;
1817 }
1818 bus->rxctl = buf + doff;
1819 bus->rxctl_orig = buf;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001820 bus->rxlen = len - doff;
Franky Lindd43a012012-11-05 16:22:22 -08001821 spin_unlock_bh(&bus->rxctl_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001822
1823done:
1824 /* Awake any waiters */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001825 brcmf_sdio_dcmd_resp_wake(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001826}
1827
1828/* Pad read to blocksize for efficiency */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001829static void brcmf_sdio_pad(struct brcmf_sdio *bus, u16 *pad, u16 *rdlen)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001830{
1831 if (bus->roundup && bus->blocksize && *rdlen > bus->blocksize) {
1832 *pad = bus->blocksize - (*rdlen % bus->blocksize);
1833 if (*pad <= bus->roundup && *pad < bus->blocksize &&
1834 *rdlen + *pad + BRCMF_FIRSTREAD < MAX_RX_DATASZ)
1835 *rdlen += *pad;
Arend van Spriel9b2d2f2a82013-11-29 12:25:14 +01001836 } else if (*rdlen % bus->head_align) {
1837 *rdlen += bus->head_align - (*rdlen % bus->head_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001838 }
1839}
1840
Franky Lin4754fce2012-09-19 22:21:11 +02001841static uint brcmf_sdio_readframes(struct brcmf_sdio *bus, uint maxframes)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001842{
Arend van Spriel5b435de2011-10-05 13:19:03 +02001843 struct sk_buff *pkt; /* Packet for event or data frames */
1844 u16 pad; /* Number of pad bytes to read */
Arend van Spriel5b435de2011-10-05 13:19:03 +02001845 uint rxleft = 0; /* Remaining number of frames allowed */
Arend van Spriel349e7102013-03-03 12:45:28 +01001846 int ret; /* Return code from calls */
Arend van Spriel5b435de2011-10-05 13:19:03 +02001847 uint rxcount = 0; /* Total frames read */
Franky Lin6bc52312013-08-10 12:27:28 +02001848 struct brcmf_sdio_hdrinfo *rd = &bus->cur_read, rd_new;
Franky Lin4754fce2012-09-19 22:21:11 +02001849 u8 head_read = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001850
1851 brcmf_dbg(TRACE, "Enter\n");
1852
1853 /* Not finished unless we encounter no more frames indication */
Franky Lin4754fce2012-09-19 22:21:11 +02001854 bus->rxpending = true;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001855
Franky Lin4754fce2012-09-19 22:21:11 +02001856 for (rd->seq_num = bus->rx_seq, rxleft = maxframes;
Arend van Sprielbb350712014-01-13 22:20:29 +01001857 !bus->rxskip && rxleft && brcmf_bus_ready(bus->sdiodev->bus_if);
Franky Lin4754fce2012-09-19 22:21:11 +02001858 rd->seq_num++, rxleft--) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001859
1860 /* Handle glomming separately */
Arend van Sprielb83db862011-10-19 12:51:09 +02001861 if (bus->glomd || !skb_queue_empty(&bus->glom)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001862 u8 cnt;
1863 brcmf_dbg(GLOM, "calling rxglom: glomd %p, glom %p\n",
Arend van Sprielb83db862011-10-19 12:51:09 +02001864 bus->glomd, skb_peek(&bus->glom));
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001865 cnt = brcmf_sdio_rxglom(bus, rd->seq_num);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001866 brcmf_dbg(GLOM, "rxglom returned %d\n", cnt);
Franky Lin4754fce2012-09-19 22:21:11 +02001867 rd->seq_num += cnt - 1;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001868 rxleft = (rxleft > cnt) ? (rxleft - cnt) : 1;
1869 continue;
1870 }
1871
Franky Lin4754fce2012-09-19 22:21:11 +02001872 rd->len_left = rd->len;
1873 /* read header first for unknow frame length */
Franky Lin38b0b0d2012-11-05 16:22:24 -08001874 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001875 if (!rd->len) {
Arend van Spriela39be272013-12-12 11:58:58 +01001876 ret = brcmf_sdiod_recv_buf(bus->sdiodev,
Arend van Spriela39be272013-12-12 11:58:58 +01001877 bus->rxhdr, BRCMF_FIRSTREAD);
Franky Lin4754fce2012-09-19 22:21:11 +02001878 bus->sdcnt.f2rxhdrs++;
Arend van Spriel349e7102013-03-03 12:45:28 +01001879 if (ret < 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001880 brcmf_err("RXHEADER FAILED: %d\n",
Arend van Spriel349e7102013-03-03 12:45:28 +01001881 ret);
Franky Lin4754fce2012-09-19 22:21:11 +02001882 bus->sdcnt.rx_hdrfail++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001883 brcmf_sdio_rxfail(bus, true, true);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001884 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001885 continue;
1886 }
1887
Franky Lin4754fce2012-09-19 22:21:11 +02001888 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() || BRCMF_HDRS_ON(),
1889 bus->rxhdr, SDPCM_HDRLEN,
1890 "RxHdr:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001891
Franky Lin6bc52312013-08-10 12:27:28 +02001892 if (brcmf_sdio_hdparse(bus, bus->rxhdr, rd,
1893 BRCMF_SDIO_FT_NORMAL)) {
Franky Lin38b0b0d2012-11-05 16:22:24 -08001894 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001895 if (!bus->rxpending)
1896 break;
1897 else
1898 continue;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001899 }
1900
Franky Lin4754fce2012-09-19 22:21:11 +02001901 if (rd->channel == SDPCM_CONTROL_CHANNEL) {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001902 brcmf_sdio_read_control(bus, bus->rxhdr,
1903 rd->len,
1904 rd->dat_offset);
Franky Lin4754fce2012-09-19 22:21:11 +02001905 /* prepare the descriptor for the next read */
1906 rd->len = rd->len_nxtfrm << 4;
1907 rd->len_nxtfrm = 0;
1908 /* treat all packet as event if we don't know */
1909 rd->channel = SDPCM_EVENT_CHANNEL;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001910 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001911 continue;
1912 }
1913 rd->len_left = rd->len > BRCMF_FIRSTREAD ?
1914 rd->len - BRCMF_FIRSTREAD : 0;
1915 head_read = BRCMF_FIRSTREAD;
1916 }
1917
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001918 brcmf_sdio_pad(bus, &pad, &rd->len_left);
Franky Lin4754fce2012-09-19 22:21:11 +02001919
1920 pkt = brcmu_pkt_buf_get_skb(rd->len_left + head_read +
Arend van Spriel9b2d2f2a82013-11-29 12:25:14 +01001921 bus->head_align);
Franky Lin4754fce2012-09-19 22:21:11 +02001922 if (!pkt) {
1923 /* Give up on data, request rtx of events */
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001924 brcmf_err("brcmu_pkt_buf_get_skb failed\n");
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001925 brcmf_sdio_rxfail(bus, false,
Franky Lin4754fce2012-09-19 22:21:11 +02001926 RETRYCHAN(rd->channel));
Franky Lin38b0b0d2012-11-05 16:22:24 -08001927 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001928 continue;
1929 }
1930 skb_pull(pkt, head_read);
Arend van Spriel9b2d2f2a82013-11-29 12:25:14 +01001931 pkt_align(pkt, rd->len_left, bus->head_align);
Franky Lin4754fce2012-09-19 22:21:11 +02001932
Arend van Spriela7cdd822013-12-12 11:58:59 +01001933 ret = brcmf_sdiod_recv_pkt(bus->sdiodev, pkt);
Franky Lin4754fce2012-09-19 22:21:11 +02001934 bus->sdcnt.f2rxdata++;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001935 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001936
Arend van Spriel349e7102013-03-03 12:45:28 +01001937 if (ret < 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001938 brcmf_err("read %d bytes from channel %d failed: %d\n",
Arend van Spriel349e7102013-03-03 12:45:28 +01001939 rd->len, rd->channel, ret);
Franky Lin4754fce2012-09-19 22:21:11 +02001940 brcmu_pkt_buf_free_skb(pkt);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001941 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001942 brcmf_sdio_rxfail(bus, true,
Franky Lin4754fce2012-09-19 22:21:11 +02001943 RETRYCHAN(rd->channel));
Franky Lin38b0b0d2012-11-05 16:22:24 -08001944 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001945 continue;
1946 }
1947
1948 if (head_read) {
1949 skb_push(pkt, head_read);
1950 memcpy(pkt->data, bus->rxhdr, head_read);
1951 head_read = 0;
1952 } else {
1953 memcpy(bus->rxhdr, pkt->data, SDPCM_HDRLEN);
1954 rd_new.seq_num = rd->seq_num;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001955 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin6bc52312013-08-10 12:27:28 +02001956 if (brcmf_sdio_hdparse(bus, bus->rxhdr, &rd_new,
1957 BRCMF_SDIO_FT_NORMAL)) {
Franky Lin4754fce2012-09-19 22:21:11 +02001958 rd->len = 0;
1959 brcmu_pkt_buf_free_skb(pkt);
1960 }
Arend van Spriel80969832012-06-09 22:51:44 +02001961 bus->sdcnt.rx_readahead_cnt++;
Franky Lin4754fce2012-09-19 22:21:11 +02001962 if (rd->len != roundup(rd_new.len, 16)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001963 brcmf_err("frame length mismatch:read %d, should be %d\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001964 rd->len,
1965 roundup(rd_new.len, 16) >> 4);
1966 rd->len = 0;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001967 brcmf_sdio_rxfail(bus, true, true);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001968 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001969 brcmu_pkt_buf_free_skb(pkt);
1970 continue;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001971 }
Franky Lin38b0b0d2012-11-05 16:22:24 -08001972 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001973 rd->len_nxtfrm = rd_new.len_nxtfrm;
1974 rd->channel = rd_new.channel;
1975 rd->dat_offset = rd_new.dat_offset;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001976
Joe Perches1e023822012-01-15 00:38:40 -08001977 brcmf_dbg_hex_dump(!(BRCMF_BYTES_ON() &&
1978 BRCMF_DATA_ON()) &&
1979 BRCMF_HDRS_ON(),
1980 bus->rxhdr, SDPCM_HDRLEN,
1981 "RxHdr:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001982
Franky Lin4754fce2012-09-19 22:21:11 +02001983 if (rd_new.channel == SDPCM_CONTROL_CHANNEL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001984 brcmf_err("readahead on control packet %d?\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001985 rd_new.seq_num);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001986 /* Force retry w/normal header read */
Franky Lin4754fce2012-09-19 22:21:11 +02001987 rd->len = 0;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001988 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001989 brcmf_sdio_rxfail(bus, false, true);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001990 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001991 brcmu_pkt_buf_free_skb(pkt);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001992 continue;
1993 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02001994 }
1995
Joe Perches1e023822012-01-15 00:38:40 -08001996 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_DATA_ON(),
Franky Lin4754fce2012-09-19 22:21:11 +02001997 pkt->data, rd->len, "Rx Data:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001998
Arend van Spriel5b435de2011-10-05 13:19:03 +02001999 /* Save superframe descriptor and allocate packet frame */
Franky Lin4754fce2012-09-19 22:21:11 +02002000 if (rd->channel == SDPCM_GLOM_CHANNEL) {
Franky Lin6bc52312013-08-10 12:27:28 +02002001 if (SDPCM_GLOMDESC(&bus->rxhdr[SDPCM_HWHDR_LEN])) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02002002 brcmf_dbg(GLOM, "glom descriptor, %d bytes:\n",
Franky Lin4754fce2012-09-19 22:21:11 +02002003 rd->len);
Joe Perches1e023822012-01-15 00:38:40 -08002004 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
Franky Lin4754fce2012-09-19 22:21:11 +02002005 pkt->data, rd->len,
Joe Perches1e023822012-01-15 00:38:40 -08002006 "Glom Data:\n");
Franky Lin4754fce2012-09-19 22:21:11 +02002007 __skb_trim(pkt, rd->len);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002008 skb_pull(pkt, SDPCM_HDRLEN);
2009 bus->glomd = pkt;
2010 } else {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002011 brcmf_err("%s: glom superframe w/o "
Arend van Spriel5b435de2011-10-05 13:19:03 +02002012 "descriptor!\n", __func__);
Franky Lin38b0b0d2012-11-05 16:22:24 -08002013 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002014 brcmf_sdio_rxfail(bus, false, false);
Franky Lin38b0b0d2012-11-05 16:22:24 -08002015 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002016 }
Franky Lin4754fce2012-09-19 22:21:11 +02002017 /* prepare the descriptor for the next read */
2018 rd->len = rd->len_nxtfrm << 4;
2019 rd->len_nxtfrm = 0;
2020 /* treat all packet as event if we don't know */
2021 rd->channel = SDPCM_EVENT_CHANNEL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002022 continue;
2023 }
2024
2025 /* Fill in packet len and prio, deliver upward */
Franky Lin4754fce2012-09-19 22:21:11 +02002026 __skb_trim(pkt, rd->len);
2027 skb_pull(pkt, rd->dat_offset);
2028
2029 /* prepare the descriptor for the next read */
2030 rd->len = rd->len_nxtfrm << 4;
2031 rd->len_nxtfrm = 0;
2032 /* treat all packet as event if we don't know */
2033 rd->channel = SDPCM_EVENT_CHANNEL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002034
2035 if (pkt->len == 0) {
2036 brcmu_pkt_buf_free_skb(pkt);
2037 continue;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002038 }
2039
Arend van Spriel05f38202013-09-25 13:05:46 +02002040 brcmf_rx_frame(bus->sdiodev->dev, pkt);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002041 }
Franky Lin4754fce2012-09-19 22:21:11 +02002042
Arend van Spriel5b435de2011-10-05 13:19:03 +02002043 rxcount = maxframes - rxleft;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002044 /* Message if we hit the limit */
2045 if (!rxleft)
Franky Lin4754fce2012-09-19 22:21:11 +02002046 brcmf_dbg(DATA, "hit rx limit of %d frames\n", maxframes);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002047 else
Arend van Spriel5b435de2011-10-05 13:19:03 +02002048 brcmf_dbg(DATA, "processed %d frames\n", rxcount);
2049 /* Back off rxseq if awaiting rtx, update rx_seq */
2050 if (bus->rxskip)
Franky Lin4754fce2012-09-19 22:21:11 +02002051 rd->seq_num--;
2052 bus->rx_seq = rd->seq_num;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002053
2054 return rxcount;
2055}
2056
Arend van Spriel5b435de2011-10-05 13:19:03 +02002057static void
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002058brcmf_sdio_wait_event_wakeup(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002059{
2060 if (waitqueue_active(&bus->ctrl_wait))
2061 wake_up_interruptible(&bus->ctrl_wait);
2062 return;
2063}
2064
Franky Lin8da9d2c2013-11-29 12:25:10 +01002065static int brcmf_sdio_txpkt_hdalign(struct brcmf_sdio *bus, struct sk_buff *pkt)
2066{
Arend van Spriele217d1c2013-11-29 12:25:13 +01002067 u16 head_pad;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002068 u8 *dat_buf;
2069
Franky Lin8da9d2c2013-11-29 12:25:10 +01002070 dat_buf = (u8 *)(pkt->data);
2071
2072 /* Check head padding */
Arend van Spriele217d1c2013-11-29 12:25:13 +01002073 head_pad = ((unsigned long)dat_buf % bus->head_align);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002074 if (head_pad) {
2075 if (skb_headroom(pkt) < head_pad) {
2076 bus->sdiodev->bus_if->tx_realloc++;
2077 head_pad = 0;
2078 if (skb_cow(pkt, head_pad))
2079 return -ENOMEM;
2080 }
2081 skb_push(pkt, head_pad);
2082 dat_buf = (u8 *)(pkt->data);
2083 memset(dat_buf, 0, head_pad + bus->tx_hdrlen);
2084 }
2085 return head_pad;
2086}
2087
Franky Lin5491c112013-09-25 13:05:43 +02002088/**
2089 * struct brcmf_skbuff_cb reserves first two bytes in sk_buff::cb for
2090 * bus layer usage.
2091 */
Franky Linb05e9252013-08-10 12:27:26 +02002092/* flag marking a dummy skb added for DMA alignment requirement */
Franky Lin5491c112013-09-25 13:05:43 +02002093#define ALIGN_SKB_FLAG 0x8000
Franky Linb05e9252013-08-10 12:27:26 +02002094/* bit mask of data length chopped from the previous packet */
Franky Lin5491c112013-09-25 13:05:43 +02002095#define ALIGN_SKB_CHOP_LEN_MASK 0x7fff
2096
Franky Lin8da9d2c2013-11-29 12:25:10 +01002097static int brcmf_sdio_txpkt_prep_sg(struct brcmf_sdio *bus,
Arend van Spriela64304f2013-10-15 15:44:56 +02002098 struct sk_buff_head *pktq,
Franky Lin8da9d2c2013-11-29 12:25:10 +01002099 struct sk_buff *pkt, u16 total_len)
Arend van Spriela64304f2013-10-15 15:44:56 +02002100{
Franky Lin8da9d2c2013-11-29 12:25:10 +01002101 struct brcmf_sdio_dev *sdiodev;
Arend van Spriela64304f2013-10-15 15:44:56 +02002102 struct sk_buff *pkt_pad;
Arend van Spriele217d1c2013-11-29 12:25:13 +01002103 u16 tail_pad, tail_chop, chain_pad;
Arend van Spriela64304f2013-10-15 15:44:56 +02002104 unsigned int blksize;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002105 bool lastfrm;
2106 int ntail, ret;
Arend van Spriela64304f2013-10-15 15:44:56 +02002107
Franky Lin8da9d2c2013-11-29 12:25:10 +01002108 sdiodev = bus->sdiodev;
Arend van Spriela64304f2013-10-15 15:44:56 +02002109 blksize = sdiodev->func[SDIO_FUNC_2]->cur_blksize;
Arend van Spriela64304f2013-10-15 15:44:56 +02002110 /* sg entry alignment should be a divisor of block size */
Arend van Spriele217d1c2013-11-29 12:25:13 +01002111 WARN_ON(blksize % bus->sgentry_align);
Arend van Spriela64304f2013-10-15 15:44:56 +02002112
2113 /* Check tail padding */
Franky Lin8da9d2c2013-11-29 12:25:10 +01002114 lastfrm = skb_queue_is_last(pktq, pkt);
2115 tail_pad = 0;
Arend van Spriele217d1c2013-11-29 12:25:13 +01002116 tail_chop = pkt->len % bus->sgentry_align;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002117 if (tail_chop)
Arend van Spriele217d1c2013-11-29 12:25:13 +01002118 tail_pad = bus->sgentry_align - tail_chop;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002119 chain_pad = (total_len + tail_pad) % blksize;
2120 if (lastfrm && chain_pad)
2121 tail_pad += blksize - chain_pad;
Arend van Spriela64304f2013-10-15 15:44:56 +02002122 if (skb_tailroom(pkt) < tail_pad && pkt->len > blksize) {
Arend van Spriel1eb43012014-02-20 18:55:55 +01002123 pkt_pad = brcmu_pkt_buf_get_skb(tail_pad + tail_chop +
2124 bus->head_align);
Arend van Spriela64304f2013-10-15 15:44:56 +02002125 if (pkt_pad == NULL)
2126 return -ENOMEM;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002127 ret = brcmf_sdio_txpkt_hdalign(bus, pkt_pad);
Dave Jones2dc3a8e2014-03-06 15:51:08 -05002128 if (unlikely(ret < 0)) {
2129 kfree_skb(pkt_pad);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002130 return ret;
Dave Jones2dc3a8e2014-03-06 15:51:08 -05002131 }
Arend van Spriela64304f2013-10-15 15:44:56 +02002132 memcpy(pkt_pad->data,
2133 pkt->data + pkt->len - tail_chop,
2134 tail_chop);
Arend van Spriel5aa9f0e2014-02-25 20:30:27 +01002135 *(u16 *)(pkt_pad->cb) = ALIGN_SKB_FLAG + tail_chop;
Arend van Spriela64304f2013-10-15 15:44:56 +02002136 skb_trim(pkt, pkt->len - tail_chop);
Arend van Spriel1eb43012014-02-20 18:55:55 +01002137 skb_trim(pkt_pad, tail_pad + tail_chop);
Arend van Spriela64304f2013-10-15 15:44:56 +02002138 __skb_queue_after(pktq, pkt, pkt_pad);
2139 } else {
2140 ntail = pkt->data_len + tail_pad -
2141 (pkt->end - pkt->tail);
2142 if (skb_cloned(pkt) || ntail > 0)
2143 if (pskb_expand_head(pkt, 0, ntail, GFP_ATOMIC))
2144 return -ENOMEM;
2145 if (skb_linearize(pkt))
2146 return -ENOMEM;
Arend van Spriela64304f2013-10-15 15:44:56 +02002147 __skb_put(pkt, tail_pad);
2148 }
2149
Franky Lin8da9d2c2013-11-29 12:25:10 +01002150 return tail_pad;
Arend van Spriela64304f2013-10-15 15:44:56 +02002151}
2152
Franky Linb05e9252013-08-10 12:27:26 +02002153/**
2154 * brcmf_sdio_txpkt_prep - packet preparation for transmit
2155 * @bus: brcmf_sdio structure pointer
2156 * @pktq: packet list pointer
2157 * @chan: virtual channel to transmit the packet
2158 *
2159 * Processes to be applied to the packet
2160 * - Align data buffer pointer
2161 * - Align data buffer length
2162 * - Prepare header
2163 * Return: negative value if there is error
2164 */
2165static int
2166brcmf_sdio_txpkt_prep(struct brcmf_sdio *bus, struct sk_buff_head *pktq,
2167 uint chan)
2168{
Franky Lin8da9d2c2013-11-29 12:25:10 +01002169 u16 head_pad, total_len;
Arend van Spriela64304f2013-10-15 15:44:56 +02002170 struct sk_buff *pkt_next;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002171 u8 txseq;
2172 int ret;
Franky Lin6bc52312013-08-10 12:27:28 +02002173 struct brcmf_sdio_hdrinfo hd_info = {0};
Franky Linb05e9252013-08-10 12:27:26 +02002174
Franky Lin8da9d2c2013-11-29 12:25:10 +01002175 txseq = bus->tx_seq;
2176 total_len = 0;
2177 skb_queue_walk(pktq, pkt_next) {
2178 /* alignment packet inserted in previous
2179 * loop cycle can be skipped as it is
2180 * already properly aligned and does not
2181 * need an sdpcm header.
2182 */
Arend van Spriel5aa9f0e2014-02-25 20:30:27 +01002183 if (*(u16 *)(pkt_next->cb) & ALIGN_SKB_FLAG)
Franky Lin8da9d2c2013-11-29 12:25:10 +01002184 continue;
Franky Linb05e9252013-08-10 12:27:26 +02002185
Franky Lin8da9d2c2013-11-29 12:25:10 +01002186 /* align packet data pointer */
2187 ret = brcmf_sdio_txpkt_hdalign(bus, pkt_next);
2188 if (ret < 0)
2189 return ret;
2190 head_pad = (u16)ret;
2191 if (head_pad)
Arend van Spriel1eb43012014-02-20 18:55:55 +01002192 memset(pkt_next->data + bus->tx_hdrlen, 0, head_pad);
Franky Linb05e9252013-08-10 12:27:26 +02002193
Franky Lin8da9d2c2013-11-29 12:25:10 +01002194 total_len += pkt_next->len;
Franky Linb05e9252013-08-10 12:27:26 +02002195
Arend van Spriela64304f2013-10-15 15:44:56 +02002196 hd_info.len = pkt_next->len;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002197 hd_info.lastfrm = skb_queue_is_last(pktq, pkt_next);
2198 if (bus->txglom && pktq->qlen > 1) {
2199 ret = brcmf_sdio_txpkt_prep_sg(bus, pktq,
2200 pkt_next, total_len);
2201 if (ret < 0)
2202 return ret;
2203 hd_info.tail_pad = (u16)ret;
2204 total_len += (u16)ret;
2205 }
2206
2207 hd_info.channel = chan;
2208 hd_info.dat_offset = head_pad + bus->tx_hdrlen;
2209 hd_info.seq_num = txseq++;
2210
2211 /* Now fill the header */
2212 brcmf_sdio_hdpack(bus, pkt_next->data, &hd_info);
2213
2214 if (BRCMF_BYTES_ON() &&
2215 ((BRCMF_CTL_ON() && chan == SDPCM_CONTROL_CHANNEL) ||
2216 (BRCMF_DATA_ON() && chan != SDPCM_CONTROL_CHANNEL)))
Hante Meuleman47ab4cd2014-02-25 20:30:29 +01002217 brcmf_dbg_hex_dump(true, pkt_next->data, hd_info.len,
Franky Lin8da9d2c2013-11-29 12:25:10 +01002218 "Tx Frame:\n");
2219 else if (BRCMF_HDRS_ON())
Hante Meuleman47ab4cd2014-02-25 20:30:29 +01002220 brcmf_dbg_hex_dump(true, pkt_next->data,
Franky Lin8da9d2c2013-11-29 12:25:10 +01002221 head_pad + bus->tx_hdrlen,
2222 "Tx Header:\n");
Franky Linb05e9252013-08-10 12:27:26 +02002223 }
Franky Lin8da9d2c2013-11-29 12:25:10 +01002224 /* Hardware length tag of the first packet should be total
2225 * length of the chain (including padding)
2226 */
2227 if (bus->txglom)
2228 brcmf_sdio_update_hwhdr(pktq->next->data, total_len);
Franky Linb05e9252013-08-10 12:27:26 +02002229 return 0;
2230}
2231
2232/**
2233 * brcmf_sdio_txpkt_postp - packet post processing for transmit
2234 * @bus: brcmf_sdio structure pointer
2235 * @pktq: packet list pointer
2236 *
2237 * Processes to be applied to the packet
2238 * - Remove head padding
2239 * - Remove tail padding
2240 */
2241static void
2242brcmf_sdio_txpkt_postp(struct brcmf_sdio *bus, struct sk_buff_head *pktq)
2243{
2244 u8 *hdr;
2245 u32 dat_offset;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002246 u16 tail_pad;
Arend van Spriel5aa9f0e2014-02-25 20:30:27 +01002247 u16 dummy_flags, chop_len;
Franky Linb05e9252013-08-10 12:27:26 +02002248 struct sk_buff *pkt_next, *tmp, *pkt_prev;
2249
2250 skb_queue_walk_safe(pktq, pkt_next, tmp) {
Arend van Spriel5aa9f0e2014-02-25 20:30:27 +01002251 dummy_flags = *(u16 *)(pkt_next->cb);
Franky Lin5491c112013-09-25 13:05:43 +02002252 if (dummy_flags & ALIGN_SKB_FLAG) {
2253 chop_len = dummy_flags & ALIGN_SKB_CHOP_LEN_MASK;
Franky Linb05e9252013-08-10 12:27:26 +02002254 if (chop_len) {
2255 pkt_prev = pkt_next->prev;
Franky Linb05e9252013-08-10 12:27:26 +02002256 skb_put(pkt_prev, chop_len);
2257 }
2258 __skb_unlink(pkt_next, pktq);
2259 brcmu_pkt_buf_free_skb(pkt_next);
2260 } else {
Franky Lin8da9d2c2013-11-29 12:25:10 +01002261 hdr = pkt_next->data + bus->tx_hdrlen - SDPCM_SWHDR_LEN;
Franky Linb05e9252013-08-10 12:27:26 +02002262 dat_offset = le32_to_cpu(*(__le32 *)hdr);
2263 dat_offset = (dat_offset & SDPCM_DOFFSET_MASK) >>
2264 SDPCM_DOFFSET_SHIFT;
2265 skb_pull(pkt_next, dat_offset);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002266 if (bus->txglom) {
2267 tail_pad = le16_to_cpu(*(__le16 *)(hdr - 2));
2268 skb_trim(pkt_next, pkt_next->len - tail_pad);
2269 }
Franky Linb05e9252013-08-10 12:27:26 +02002270 }
2271 }
2272}
2273
Arend van Spriel5b435de2011-10-05 13:19:03 +02002274/* Writes a HW/SW header into the packet and sends it. */
2275/* Assumes: (a) header space already there, (b) caller holds lock */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002276static int brcmf_sdio_txpkt(struct brcmf_sdio *bus, struct sk_buff_head *pktq,
2277 uint chan)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002278{
2279 int ret;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002280 struct sk_buff *pkt_next, *tmp;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002281
2282 brcmf_dbg(TRACE, "Enter\n");
2283
Franky Lin8da9d2c2013-11-29 12:25:10 +01002284 ret = brcmf_sdio_txpkt_prep(bus, pktq, chan);
Franky Linb05e9252013-08-10 12:27:26 +02002285 if (ret)
2286 goto done;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002287
Franky Lin38b0b0d2012-11-05 16:22:24 -08002288 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriela7cdd822013-12-12 11:58:59 +01002289 ret = brcmf_sdiod_send_pkt(bus->sdiodev, pktq);
Arend van Spriel80969832012-06-09 22:51:44 +02002290 bus->sdcnt.f2txdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002291
Hante Meuleman81c78832014-02-25 20:30:35 +01002292 if (ret < 0)
2293 brcmf_sdio_txfail(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002294
Franky Lin38b0b0d2012-11-05 16:22:24 -08002295 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002296
2297done:
Franky Lin8da9d2c2013-11-29 12:25:10 +01002298 brcmf_sdio_txpkt_postp(bus, pktq);
2299 if (ret == 0)
2300 bus->tx_seq = (bus->tx_seq + pktq->qlen) % SDPCM_SEQ_WRAP;
2301 skb_queue_walk_safe(pktq, pkt_next, tmp) {
2302 __skb_unlink(pkt_next, pktq);
2303 brcmf_txcomplete(bus->sdiodev->dev, pkt_next, ret == 0);
2304 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002305 return ret;
2306}
2307
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002308static uint brcmf_sdio_sendfromq(struct brcmf_sdio *bus, uint maxframes)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002309{
2310 struct sk_buff *pkt;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002311 struct sk_buff_head pktq;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002312 u32 intstatus = 0;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002313 int ret = 0, prec_out, i;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002314 uint cnt = 0;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002315 u8 tx_prec_map, pkt_num;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002316
Arend van Spriel5b435de2011-10-05 13:19:03 +02002317 brcmf_dbg(TRACE, "Enter\n");
2318
2319 tx_prec_map = ~bus->flowcontrol;
2320
2321 /* Send frames until the limit or some other event */
Franky Lin8da9d2c2013-11-29 12:25:10 +01002322 for (cnt = 0; (cnt < maxframes) && data_ok(bus);) {
2323 pkt_num = 1;
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002324 if (down_interruptible(&bus->tx_seq_lock))
2325 return cnt;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002326 if (bus->txglom)
2327 pkt_num = min_t(u8, bus->tx_max - bus->tx_seq,
Arend van Sprielaf1fa212014-02-27 19:25:00 +01002328 bus->sdiodev->txglomsz);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002329 pkt_num = min_t(u32, pkt_num,
2330 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol));
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002331 __skb_queue_head_init(&pktq);
2332 spin_lock_bh(&bus->txq_lock);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002333 for (i = 0; i < pkt_num; i++) {
2334 pkt = brcmu_pktq_mdeq(&bus->txq, tx_prec_map,
2335 &prec_out);
2336 if (pkt == NULL)
2337 break;
2338 __skb_queue_tail(&pktq, pkt);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002339 }
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002340 spin_unlock_bh(&bus->txq_lock);
2341 if (i == 0) {
2342 up(&bus->tx_seq_lock);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002343 break;
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002344 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002345
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002346 ret = brcmf_sdio_txpkt(bus, &pktq, SDPCM_DATA_CHANNEL);
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002347 up(&bus->tx_seq_lock);
2348
Franky Lin8da9d2c2013-11-29 12:25:10 +01002349 cnt += i;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002350
2351 /* In poll mode, need to check for other events */
Hante Meulemanb6a8cf22014-02-25 20:30:33 +01002352 if (!bus->intr) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02002353 /* Check device status, signal pending interrupt */
Franky Lin38b0b0d2012-11-05 16:22:24 -08002354 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin5c15c232012-05-04 18:27:37 -07002355 ret = r_sdreg32(bus, &intstatus,
2356 offsetof(struct sdpcmd_regs,
2357 intstatus));
Franky Lin38b0b0d2012-11-05 16:22:24 -08002358 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel80969832012-06-09 22:51:44 +02002359 bus->sdcnt.f2txdata++;
Franky Lin5c15c232012-05-04 18:27:37 -07002360 if (ret != 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002361 break;
2362 if (intstatus & bus->hostintmask)
Franky Lin1d382272012-09-13 21:11:59 +02002363 atomic_set(&bus->ipend, 1);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002364 }
2365 }
2366
2367 /* Deflow-control stack if needed */
Hante Meuleman05dde972013-02-06 18:40:36 +01002368 if ((bus->sdiodev->bus_if->state == BRCMF_BUS_DATA) &&
Franky Linc8bf3482011-12-16 18:37:08 -08002369 bus->txoff && (pktq_len(&bus->txq) < TXLOW)) {
Hante Meuleman90d03ff2012-09-11 21:18:46 +02002370 bus->txoff = false;
2371 brcmf_txflowblock(bus->sdiodev->dev, false);
Franky Linc8bf3482011-12-16 18:37:08 -08002372 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002373
2374 return cnt;
2375}
2376
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002377static int brcmf_sdio_tx_ctrlframe(struct brcmf_sdio *bus, u8 *frame, u16 len)
2378{
2379 u8 doff;
2380 u16 pad;
2381 uint retries = 0;
2382 struct brcmf_sdio_hdrinfo hd_info = {0};
2383 int ret;
2384
2385 brcmf_dbg(TRACE, "Enter\n");
2386
2387 /* Back the pointer to make room for bus header */
2388 frame -= bus->tx_hdrlen;
2389 len += bus->tx_hdrlen;
2390
2391 /* Add alignment padding (optional for ctl frames) */
2392 doff = ((unsigned long)frame % bus->head_align);
2393 if (doff) {
2394 frame -= doff;
2395 len += doff;
2396 memset(frame + bus->tx_hdrlen, 0, doff);
2397 }
2398
2399 /* Round send length to next SDIO block */
2400 pad = 0;
2401 if (bus->roundup && bus->blocksize && (len > bus->blocksize)) {
2402 pad = bus->blocksize - (len % bus->blocksize);
2403 if ((pad > bus->roundup) || (pad >= bus->blocksize))
2404 pad = 0;
2405 } else if (len % bus->head_align) {
2406 pad = bus->head_align - (len % bus->head_align);
2407 }
2408 len += pad;
2409
2410 hd_info.len = len - pad;
2411 hd_info.channel = SDPCM_CONTROL_CHANNEL;
2412 hd_info.dat_offset = doff + bus->tx_hdrlen;
2413 hd_info.seq_num = bus->tx_seq;
2414 hd_info.lastfrm = true;
2415 hd_info.tail_pad = pad;
2416 brcmf_sdio_hdpack(bus, frame, &hd_info);
2417
2418 if (bus->txglom)
2419 brcmf_sdio_update_hwhdr(frame, len);
2420
2421 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_CTL_ON(),
2422 frame, len, "Tx Frame:\n");
2423 brcmf_dbg_hex_dump(!(BRCMF_BYTES_ON() && BRCMF_CTL_ON()) &&
2424 BRCMF_HDRS_ON(),
2425 frame, min_t(u16, len, 16), "TxHdr:\n");
2426
2427 do {
2428 ret = brcmf_sdiod_send_buf(bus->sdiodev, frame, len);
2429
2430 if (ret < 0)
2431 brcmf_sdio_txfail(bus);
2432 else
2433 bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQ_WRAP;
2434 } while (ret < 0 && retries++ < TXRETRIES);
2435
2436 return ret;
2437}
2438
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002439static void brcmf_sdio_bus_stop(struct device *dev)
Franky Lina9ffda82011-12-16 18:37:12 -08002440{
2441 u32 local_hostintmask;
2442 u8 saveclk;
Franky Lina9ffda82011-12-16 18:37:12 -08002443 int err;
2444 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01002445 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Lina9ffda82011-12-16 18:37:12 -08002446 struct brcmf_sdio *bus = sdiodev->bus;
2447
2448 brcmf_dbg(TRACE, "Enter\n");
2449
2450 if (bus->watchdog_tsk) {
2451 send_sig(SIGTERM, bus->watchdog_tsk, 1);
2452 kthread_stop(bus->watchdog_tsk);
2453 bus->watchdog_tsk = NULL;
2454 }
2455
Arend van Sprielbb350712014-01-13 22:20:29 +01002456 if (bus_if->state == BRCMF_BUS_DOWN) {
2457 sdio_claim_host(sdiodev->func[1]);
Franky Lina9ffda82011-12-16 18:37:12 -08002458
Arend van Sprielbb350712014-01-13 22:20:29 +01002459 /* Enable clock for device interrupts */
2460 brcmf_sdio_bus_sleep(bus, false, false);
Franky Lina9ffda82011-12-16 18:37:12 -08002461
Arend van Sprielbb350712014-01-13 22:20:29 +01002462 /* Disable and clear interrupts at the chip level also */
2463 w_sdreg32(bus, 0, offsetof(struct sdpcmd_regs, hostintmask));
2464 local_hostintmask = bus->hostintmask;
2465 bus->hostintmask = 0;
Franky Lina9ffda82011-12-16 18:37:12 -08002466
Arend van Sprielbb350712014-01-13 22:20:29 +01002467 /* Force backplane clocks to assure F2 interrupt propagates */
2468 saveclk = brcmf_sdiod_regrb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
2469 &err);
2470 if (!err)
2471 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
2472 (saveclk | SBSDIO_FORCE_HT), &err);
2473 if (err)
2474 brcmf_err("Failed to force clock for F2: err %d\n",
2475 err);
Franky Lina9ffda82011-12-16 18:37:12 -08002476
Arend van Sprielbb350712014-01-13 22:20:29 +01002477 /* Turn off the bus (F2), free any pending packets */
2478 brcmf_dbg(INTR, "disable SDIO interrupts\n");
2479 sdio_disable_func(sdiodev->func[SDIO_FUNC_2]);
2480
2481 /* Clear any pending interrupts now that F2 is disabled */
2482 w_sdreg32(bus, local_hostintmask,
2483 offsetof(struct sdpcmd_regs, intstatus));
2484
2485 sdio_release_host(sdiodev->func[1]);
Franky Lina9ffda82011-12-16 18:37:12 -08002486 }
Franky Lina9ffda82011-12-16 18:37:12 -08002487 /* Clear the data packet queues */
2488 brcmu_pktq_flush(&bus->txq, true, NULL, NULL);
2489
2490 /* Clear any held glomming stuff */
2491 if (bus->glomd)
2492 brcmu_pkt_buf_free_skb(bus->glomd);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002493 brcmf_sdio_free_glom(bus);
Franky Lina9ffda82011-12-16 18:37:12 -08002494
2495 /* Clear rx control and wake any waiters */
Franky Lindd43a012012-11-05 16:22:22 -08002496 spin_lock_bh(&bus->rxctl_lock);
Franky Lina9ffda82011-12-16 18:37:12 -08002497 bus->rxlen = 0;
Franky Lindd43a012012-11-05 16:22:22 -08002498 spin_unlock_bh(&bus->rxctl_lock);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002499 brcmf_sdio_dcmd_resp_wake(bus);
Franky Lina9ffda82011-12-16 18:37:12 -08002500
2501 /* Reset some F2 state stuff */
2502 bus->rxskip = false;
2503 bus->tx_seq = bus->rx_seq = 0;
Franky Lina9ffda82011-12-16 18:37:12 -08002504}
2505
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002506static inline void brcmf_sdio_clrintr(struct brcmf_sdio *bus)
Franky Linba89bf12012-04-27 18:56:59 -07002507{
2508 unsigned long flags;
2509
Hante Meuleman668761a2013-04-12 10:55:55 +02002510 if (bus->sdiodev->oob_irq_requested) {
2511 spin_lock_irqsave(&bus->sdiodev->irq_en_lock, flags);
2512 if (!bus->sdiodev->irq_en && !atomic_read(&bus->ipend)) {
2513 enable_irq(bus->sdiodev->pdata->oob_irq_nr);
2514 bus->sdiodev->irq_en = true;
2515 }
2516 spin_unlock_irqrestore(&bus->sdiodev->irq_en_lock, flags);
Franky Linba89bf12012-04-27 18:56:59 -07002517 }
Franky Linba89bf12012-04-27 18:56:59 -07002518}
Franky Linba89bf12012-04-27 18:56:59 -07002519
Hante Meuleman5cbb9c22014-03-06 10:16:12 +01002520static void atomic_orr(int val, atomic_t *v)
2521{
2522 int old_val;
2523
2524 old_val = atomic_read(v);
2525 while (atomic_cmpxchg(v, old_val, val | old_val) != old_val)
2526 old_val = atomic_read(v);
2527}
2528
Franky Lin45316032012-09-13 21:12:03 +02002529static int brcmf_sdio_intr_rstatus(struct brcmf_sdio *bus)
2530{
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01002531 struct brcmf_core *buscore;
Franky Lin45316032012-09-13 21:12:03 +02002532 u32 addr;
2533 unsigned long val;
Hante Meuleman5cbb9c22014-03-06 10:16:12 +01002534 int ret;
Franky Lin45316032012-09-13 21:12:03 +02002535
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01002536 buscore = brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV);
2537 addr = buscore->base + offsetof(struct sdpcmd_regs, intstatus);
Franky Lin45316032012-09-13 21:12:03 +02002538
Arend van Spriela39be272013-12-12 11:58:58 +01002539 val = brcmf_sdiod_regrl(bus->sdiodev, addr, &ret);
Franky Lin45316032012-09-13 21:12:03 +02002540 bus->sdcnt.f1regdata++;
2541 if (ret != 0)
Hante Meuleman5cbb9c22014-03-06 10:16:12 +01002542 return ret;
Franky Lin45316032012-09-13 21:12:03 +02002543
2544 val &= bus->hostintmask;
2545 atomic_set(&bus->fcstate, !!(val & I_HMB_FC_STATE));
2546
2547 /* Clear interrupts */
2548 if (val) {
Arend van Spriela39be272013-12-12 11:58:58 +01002549 brcmf_sdiod_regwl(bus->sdiodev, addr, val, &ret);
Franky Lin45316032012-09-13 21:12:03 +02002550 bus->sdcnt.f1regdata++;
Hante Meuleman5cbb9c22014-03-06 10:16:12 +01002551 atomic_orr(val, &bus->intstatus);
Franky Lin45316032012-09-13 21:12:03 +02002552 }
2553
2554 return ret;
2555}
2556
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002557static void brcmf_sdio_dpc(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002558{
Franky Lin45316032012-09-13 21:12:03 +02002559 u32 newstatus = 0;
2560 unsigned long intstatus;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002561 uint txlimit = bus->txbound; /* Tx frames to send before resched */
Hante Meulemanb6a8cf22014-02-25 20:30:33 +01002562 uint framecnt; /* Temporary counter of tx/rx frames */
Hante Meuleman5cbb9c22014-03-06 10:16:12 +01002563 int err = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002564
2565 brcmf_dbg(TRACE, "Enter\n");
2566
Franky Lin38b0b0d2012-11-05 16:22:24 -08002567 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002568
2569 /* If waiting for HTAVAIL, check status */
Piotr Haber4a3da992013-04-11 13:28:46 +02002570 if (!bus->sr_enabled && bus->clkstate == CLK_PENDING) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02002571 u8 clkctl, devctl = 0;
2572
Joe Perches8ae74652012-01-15 00:38:38 -08002573#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02002574 /* Check for inconsistent device control */
Arend van Spriela39be272013-12-12 11:58:58 +01002575 devctl = brcmf_sdiod_regrb(bus->sdiodev,
2576 SBSDIO_DEVICE_CTL, &err);
Joe Perches8ae74652012-01-15 00:38:38 -08002577#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02002578
2579 /* Read CSR, if clock on switch to AVAIL, else ignore */
Arend van Spriela39be272013-12-12 11:58:58 +01002580 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
2581 SBSDIO_FUNC1_CHIPCLKCSR, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002582
Arend van Sprielc3203372013-04-03 12:40:44 +02002583 brcmf_dbg(SDIO, "DPC: PENDING, devctl 0x%02x clkctl 0x%02x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02002584 devctl, clkctl);
2585
2586 if (SBSDIO_HTAV(clkctl)) {
Arend van Spriela39be272013-12-12 11:58:58 +01002587 devctl = brcmf_sdiod_regrb(bus->sdiodev,
2588 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002589 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
Arend van Spriela39be272013-12-12 11:58:58 +01002590 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
2591 devctl, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002592 bus->clkstate = CLK_AVAIL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002593 }
2594 }
2595
Arend van Spriel5b435de2011-10-05 13:19:03 +02002596 /* Make sure backplane clock is on */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002597 brcmf_sdio_bus_sleep(bus, false, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002598
2599 /* Pending interrupt indicates new device status */
Franky Lin1d382272012-09-13 21:11:59 +02002600 if (atomic_read(&bus->ipend) > 0) {
2601 atomic_set(&bus->ipend, 0);
Franky Lin45316032012-09-13 21:12:03 +02002602 err = brcmf_sdio_intr_rstatus(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002603 }
2604
Franky Lin45316032012-09-13 21:12:03 +02002605 /* Start with leftover status bits */
2606 intstatus = atomic_xchg(&bus->intstatus, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002607
2608 /* Handle flow-control change: read new state in case our ack
2609 * crossed another change interrupt. If change still set, assume
2610 * FC ON for safety, let next loop through do the debounce.
2611 */
2612 if (intstatus & I_HMB_FC_CHANGE) {
2613 intstatus &= ~I_HMB_FC_CHANGE;
Franky Lin5c15c232012-05-04 18:27:37 -07002614 err = w_sdreg32(bus, I_HMB_FC_CHANGE,
2615 offsetof(struct sdpcmd_regs, intstatus));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002616
Franky Lin5c15c232012-05-04 18:27:37 -07002617 err = r_sdreg32(bus, &newstatus,
2618 offsetof(struct sdpcmd_regs, intstatus));
Arend van Spriel80969832012-06-09 22:51:44 +02002619 bus->sdcnt.f1regdata += 2;
Franky Lin45316032012-09-13 21:12:03 +02002620 atomic_set(&bus->fcstate,
2621 !!(newstatus & (I_HMB_FC_STATE | I_HMB_FC_CHANGE)));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002622 intstatus |= (newstatus & bus->hostintmask);
2623 }
2624
2625 /* Handle host mailbox indication */
2626 if (intstatus & I_HMB_HOST_INT) {
2627 intstatus &= ~I_HMB_HOST_INT;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002628 intstatus |= brcmf_sdio_hostmail(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002629 }
2630
Franky Lin38b0b0d2012-11-05 16:22:24 -08002631 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin7cdf57d2012-11-05 16:22:23 -08002632
Arend van Spriel5b435de2011-10-05 13:19:03 +02002633 /* Generally don't ask for these, can get CRC errors... */
2634 if (intstatus & I_WR_OOSYNC) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002635 brcmf_err("Dongle reports WR_OOSYNC\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002636 intstatus &= ~I_WR_OOSYNC;
2637 }
2638
2639 if (intstatus & I_RD_OOSYNC) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002640 brcmf_err("Dongle reports RD_OOSYNC\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002641 intstatus &= ~I_RD_OOSYNC;
2642 }
2643
2644 if (intstatus & I_SBINT) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002645 brcmf_err("Dongle reports SBINT\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002646 intstatus &= ~I_SBINT;
2647 }
2648
2649 /* Would be active due to wake-wlan in gSPI */
2650 if (intstatus & I_CHIPACTIVE) {
2651 brcmf_dbg(INFO, "Dongle reports CHIPACTIVE\n");
2652 intstatus &= ~I_CHIPACTIVE;
2653 }
2654
2655 /* Ignore frame indications if rxskip is set */
2656 if (bus->rxskip)
2657 intstatus &= ~I_HMB_FRAME_IND;
2658
2659 /* On frame indication, read available frames */
Hante Meulemanb6a8cf22014-02-25 20:30:33 +01002660 if ((intstatus & I_HMB_FRAME_IND) && (bus->clkstate == CLK_AVAIL)) {
2661 brcmf_sdio_readframes(bus, bus->rxbound);
Franky Lin4754fce2012-09-19 22:21:11 +02002662 if (!bus->rxpending)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002663 intstatus &= ~I_HMB_FRAME_IND;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002664 }
2665
2666 /* Keep still-pending events for next scheduling */
Hante Meuleman5cbb9c22014-03-06 10:16:12 +01002667 if (intstatus)
2668 atomic_orr(intstatus, &bus->intstatus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002669
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002670 brcmf_sdio_clrintr(bus);
Franky Linba89bf12012-04-27 18:56:59 -07002671
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002672 if (bus->ctrl_frame_stat && (bus->clkstate == CLK_AVAIL) &&
2673 (down_interruptible(&bus->tx_seq_lock) == 0)) {
2674 if (data_ok(bus)) {
2675 sdio_claim_host(bus->sdiodev->func[1]);
2676 err = brcmf_sdio_tx_ctrlframe(bus, bus->ctrl_frame_buf,
2677 bus->ctrl_frame_len);
2678 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002679
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002680 bus->ctrl_frame_stat = false;
2681 brcmf_sdio_wait_event_wakeup(bus);
2682 }
2683 up(&bus->tx_seq_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002684 }
2685 /* Send queued frames (limit 1 if rx may still be pending) */
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002686 if ((bus->clkstate == CLK_AVAIL) && !atomic_read(&bus->fcstate) &&
2687 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) && txlimit &&
2688 data_ok(bus)) {
Franky Lin4754fce2012-09-19 22:21:11 +02002689 framecnt = bus->rxpending ? min(txlimit, bus->txminmax) :
2690 txlimit;
Hante Meulemanb6a8cf22014-02-25 20:30:33 +01002691 brcmf_sdio_sendfromq(bus, framecnt);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002692 }
2693
Arend van Sprielbb350712014-01-13 22:20:29 +01002694 if (!brcmf_bus_ready(bus->sdiodev->bus_if) || (err != 0)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002695 brcmf_err("failed backplane access over SDIO, halting operation\n");
Franky Lin45316032012-09-13 21:12:03 +02002696 atomic_set(&bus->intstatus, 0);
2697 } else if (atomic_read(&bus->intstatus) ||
2698 atomic_read(&bus->ipend) > 0 ||
2699 (!atomic_read(&bus->fcstate) &&
2700 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) &&
Hante Meulemanb6a8cf22014-02-25 20:30:33 +01002701 data_ok(bus))) {
Arend van Sprielfccfe932013-06-18 13:29:26 +02002702 atomic_inc(&bus->dpc_tskcnt);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002703 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002704}
2705
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002706static struct pktq *brcmf_sdio_bus_gettxq(struct device *dev)
Arend van Spriele2432b62013-04-03 12:40:38 +02002707{
2708 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
2709 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
2710 struct brcmf_sdio *bus = sdiodev->bus;
2711
2712 return &bus->txq;
2713}
2714
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002715static int brcmf_sdio_bus_txdata(struct device *dev, struct sk_buff *pkt)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002716{
2717 int ret = -EBADE;
Arend van Spriel44ff5662014-02-25 20:30:28 +01002718 uint prec;
Franky Linbf347bb2011-11-22 17:21:56 -08002719 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01002720 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Linbf347bb2011-11-22 17:21:56 -08002721 struct brcmf_sdio *bus = sdiodev->bus;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002722
Arend van Spriel44ff5662014-02-25 20:30:28 +01002723 brcmf_dbg(TRACE, "Enter: pkt: data %p len %d\n", pkt->data, pkt->len);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002724
2725 /* Add space for the header */
Franky Lin706478c2013-08-10 12:27:29 +02002726 skb_push(pkt, bus->tx_hdrlen);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002727 /* precondition: IS_ALIGNED((unsigned long)(pkt->data), 2) */
2728
2729 prec = prio2prec((pkt->priority & PRIOMASK));
2730
2731 /* Check for existing queue, current flow-control,
2732 pending event, or pending clock */
2733 brcmf_dbg(TRACE, "deferring pktq len %d\n", pktq_len(&bus->txq));
Arend van Spriel80969832012-06-09 22:51:44 +02002734 bus->sdcnt.fcqueued++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002735
2736 /* Priority based enq */
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002737 spin_lock_bh(&bus->txq_lock);
Arend van Spriel5aa9f0e2014-02-25 20:30:27 +01002738 /* reset bus_flags in packet cb */
2739 *(u16 *)(pkt->cb) = 0;
Joe Perches23677ce2012-02-09 11:17:23 +00002740 if (!brcmf_c_prec_enq(bus->sdiodev->dev, &bus->txq, pkt, prec)) {
Franky Lin706478c2013-08-10 12:27:29 +02002741 skb_pull(pkt, bus->tx_hdrlen);
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002742 brcmf_err("out of bus->txq !!!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002743 ret = -ENOSR;
2744 } else {
2745 ret = 0;
2746 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002747
Franky Linc8bf3482011-12-16 18:37:08 -08002748 if (pktq_len(&bus->txq) >= TXHI) {
Hante Meuleman90d03ff2012-09-11 21:18:46 +02002749 bus->txoff = true;
2750 brcmf_txflowblock(bus->sdiodev->dev, true);
Franky Linc8bf3482011-12-16 18:37:08 -08002751 }
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002752 spin_unlock_bh(&bus->txq_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002753
Joe Perches8ae74652012-01-15 00:38:38 -08002754#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02002755 if (pktq_plen(&bus->txq, prec) > qcount[prec])
2756 qcount[prec] = pktq_plen(&bus->txq, prec);
2757#endif
Franky Linf1e68c22012-09-13 21:12:00 +02002758
Arend van Sprielfccfe932013-06-18 13:29:26 +02002759 if (atomic_read(&bus->dpc_tskcnt) == 0) {
2760 atomic_inc(&bus->dpc_tskcnt);
Franky Linf1e68c22012-09-13 21:12:00 +02002761 queue_work(bus->brcmf_wq, &bus->datawork);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002762 }
2763
2764 return ret;
2765}
2766
Joe Perches8ae74652012-01-15 00:38:38 -08002767#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02002768#define CONSOLE_LINE_MAX 192
2769
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002770static int brcmf_sdio_readconsole(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002771{
2772 struct brcmf_console *c = &bus->console;
2773 u8 line[CONSOLE_LINE_MAX], ch;
2774 u32 n, idx, addr;
2775 int rv;
2776
2777 /* Don't do anything until FWREADY updates console address */
2778 if (bus->console_addr == 0)
2779 return 0;
2780
2781 /* Read console log struct */
2782 addr = bus->console_addr + offsetof(struct rte_console, log_le);
Arend van Spriela39be272013-12-12 11:58:58 +01002783 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, (u8 *)&c->log_le,
2784 sizeof(c->log_le));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002785 if (rv < 0)
2786 return rv;
2787
2788 /* Allocate console buffer (one time only) */
2789 if (c->buf == NULL) {
2790 c->bufsize = le32_to_cpu(c->log_le.buf_size);
2791 c->buf = kmalloc(c->bufsize, GFP_ATOMIC);
2792 if (c->buf == NULL)
2793 return -ENOMEM;
2794 }
2795
2796 idx = le32_to_cpu(c->log_le.idx);
2797
2798 /* Protect against corrupt value */
2799 if (idx > c->bufsize)
2800 return -EBADE;
2801
2802 /* Skip reading the console buffer if the index pointer
2803 has not moved */
2804 if (idx == c->last)
2805 return 0;
2806
2807 /* Read the console buffer */
2808 addr = le32_to_cpu(c->log_le.buf);
Arend van Spriela39be272013-12-12 11:58:58 +01002809 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, c->buf, c->bufsize);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002810 if (rv < 0)
2811 return rv;
2812
2813 while (c->last != idx) {
2814 for (n = 0; n < CONSOLE_LINE_MAX - 2; n++) {
2815 if (c->last == idx) {
2816 /* This would output a partial line.
2817 * Instead, back up
2818 * the buffer pointer and output this
2819 * line next time around.
2820 */
2821 if (c->last >= n)
2822 c->last -= n;
2823 else
2824 c->last = c->bufsize - n;
2825 goto break2;
2826 }
2827 ch = c->buf[c->last];
2828 c->last = (c->last + 1) % c->bufsize;
2829 if (ch == '\n')
2830 break;
2831 line[n] = ch;
2832 }
2833
2834 if (n > 0) {
2835 if (line[n - 1] == '\r')
2836 n--;
2837 line[n] = 0;
Joe Perches18aad4f2012-01-15 00:38:42 -08002838 pr_debug("CONSOLE: %s\n", line);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002839 }
2840 }
2841break2:
2842
2843 return 0;
2844}
Joe Perches8ae74652012-01-15 00:38:38 -08002845#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02002846
Franky Linfcf094f2011-12-16 18:37:16 -08002847static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002848brcmf_sdio_bus_txctl(struct device *dev, unsigned char *msg, uint msglen)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002849{
Franky Lin47a1ce72011-11-22 17:21:55 -08002850 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01002851 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Lin47a1ce72011-11-22 17:21:55 -08002852 struct brcmf_sdio *bus = sdiodev->bus;
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002853 int ret = -1;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002854
2855 brcmf_dbg(TRACE, "Enter\n");
2856
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002857 if (down_interruptible(&bus->tx_seq_lock))
2858 return -EINTR;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002859
Arend van Spriel5b435de2011-10-05 13:19:03 +02002860 if (!data_ok(bus)) {
2861 brcmf_dbg(INFO, "No bus credit bus->tx_max %d, bus->tx_seq %d\n",
2862 bus->tx_max, bus->tx_seq);
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002863 up(&bus->tx_seq_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002864 /* Send from dpc */
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002865 bus->ctrl_frame_buf = msg;
2866 bus->ctrl_frame_len = msglen;
2867 bus->ctrl_frame_stat = true;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002868
Franky Linfd67dc82012-11-05 16:22:25 -08002869 wait_event_interruptible_timeout(bus->ctrl_wait,
2870 !bus->ctrl_frame_stat,
2871 msecs_to_jiffies(2000));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002872
Joe Perches23677ce2012-02-09 11:17:23 +00002873 if (!bus->ctrl_frame_stat) {
Arend van Sprielc3203372013-04-03 12:40:44 +02002874 brcmf_dbg(SDIO, "ctrl_frame_stat == false\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002875 ret = 0;
2876 } else {
Arend van Sprielc3203372013-04-03 12:40:44 +02002877 brcmf_dbg(SDIO, "ctrl_frame_stat == true\n");
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002878 bus->ctrl_frame_stat = false;
2879 if (down_interruptible(&bus->tx_seq_lock))
2880 return -EINTR;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002881 ret = -1;
2882 }
2883 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002884 if (ret == -1) {
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002885 sdio_claim_host(bus->sdiodev->func[1]);
2886 brcmf_sdio_bus_sleep(bus, false, false);
2887 ret = brcmf_sdio_tx_ctrlframe(bus, msg, msglen);
2888 sdio_release_host(bus->sdiodev->func[1]);
2889 up(&bus->tx_seq_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002890 }
2891
Arend van Spriel5b435de2011-10-05 13:19:03 +02002892 if (ret)
Arend van Spriel80969832012-06-09 22:51:44 +02002893 bus->sdcnt.tx_ctlerrs++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002894 else
Arend van Spriel80969832012-06-09 22:51:44 +02002895 bus->sdcnt.tx_ctlpkts++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002896
2897 return ret ? -EIO : 0;
2898}
2899
Arend van Spriel80969832012-06-09 22:51:44 +02002900#ifdef DEBUG
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002901static int brcmf_sdio_dump_console(struct brcmf_sdio *bus,
2902 struct sdpcm_shared *sh, char __user *data,
2903 size_t count)
2904{
2905 u32 addr, console_ptr, console_size, console_index;
2906 char *conbuf = NULL;
2907 __le32 sh_val;
2908 int rv;
2909 loff_t pos = 0;
2910 int nbytes = 0;
2911
2912 /* obtain console information from device memory */
2913 addr = sh->console_addr + offsetof(struct rte_console, log_le);
Arend van Spriela39be272013-12-12 11:58:58 +01002914 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
2915 (u8 *)&sh_val, sizeof(u32));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002916 if (rv < 0)
2917 return rv;
2918 console_ptr = le32_to_cpu(sh_val);
2919
2920 addr = sh->console_addr + offsetof(struct rte_console, log_le.buf_size);
Arend van Spriela39be272013-12-12 11:58:58 +01002921 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
2922 (u8 *)&sh_val, sizeof(u32));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002923 if (rv < 0)
2924 return rv;
2925 console_size = le32_to_cpu(sh_val);
2926
2927 addr = sh->console_addr + offsetof(struct rte_console, log_le.idx);
Arend van Spriela39be272013-12-12 11:58:58 +01002928 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
2929 (u8 *)&sh_val, sizeof(u32));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002930 if (rv < 0)
2931 return rv;
2932 console_index = le32_to_cpu(sh_val);
2933
2934 /* allocate buffer for console data */
2935 if (console_size <= CONSOLE_BUFFER_MAX)
2936 conbuf = vzalloc(console_size+1);
2937
2938 if (!conbuf)
2939 return -ENOMEM;
2940
2941 /* obtain the console data from device */
2942 conbuf[console_size] = '\0';
Arend van Spriela39be272013-12-12 11:58:58 +01002943 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, console_ptr, (u8 *)conbuf,
2944 console_size);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002945 if (rv < 0)
2946 goto done;
2947
2948 rv = simple_read_from_buffer(data, count, &pos,
2949 conbuf + console_index,
2950 console_size - console_index);
2951 if (rv < 0)
2952 goto done;
2953
2954 nbytes = rv;
2955 if (console_index > 0) {
2956 pos = 0;
2957 rv = simple_read_from_buffer(data+nbytes, count, &pos,
2958 conbuf, console_index - 1);
2959 if (rv < 0)
2960 goto done;
2961 rv += nbytes;
2962 }
2963done:
2964 vfree(conbuf);
2965 return rv;
2966}
2967
2968static int brcmf_sdio_trap_info(struct brcmf_sdio *bus, struct sdpcm_shared *sh,
2969 char __user *data, size_t count)
2970{
2971 int error, res;
2972 char buf[350];
2973 struct brcmf_trap_info tr;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002974 loff_t pos = 0;
2975
Piotr Haberbaa9e602013-04-05 10:57:42 +02002976 if ((sh->flags & SDPCM_SHARED_TRAP) == 0) {
2977 brcmf_dbg(INFO, "no trap in firmware\n");
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002978 return 0;
Piotr Haberbaa9e602013-04-05 10:57:42 +02002979 }
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002980
Arend van Spriela39be272013-12-12 11:58:58 +01002981 error = brcmf_sdiod_ramrw(bus->sdiodev, false, sh->trap_addr, (u8 *)&tr,
2982 sizeof(struct brcmf_trap_info));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002983 if (error < 0)
2984 return error;
2985
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002986 res = scnprintf(buf, sizeof(buf),
2987 "dongle trap info: type 0x%x @ epc 0x%08x\n"
2988 " cpsr 0x%08x spsr 0x%08x sp 0x%08x\n"
2989 " lr 0x%08x pc 0x%08x offset 0x%x\n"
2990 " r0 0x%08x r1 0x%08x r2 0x%08x r3 0x%08x\n"
2991 " r4 0x%08x r5 0x%08x r6 0x%08x r7 0x%08x\n",
2992 le32_to_cpu(tr.type), le32_to_cpu(tr.epc),
2993 le32_to_cpu(tr.cpsr), le32_to_cpu(tr.spsr),
2994 le32_to_cpu(tr.r13), le32_to_cpu(tr.r14),
Arend van Spriel9bd02c62012-06-26 21:26:40 +02002995 le32_to_cpu(tr.pc), sh->trap_addr,
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002996 le32_to_cpu(tr.r0), le32_to_cpu(tr.r1),
2997 le32_to_cpu(tr.r2), le32_to_cpu(tr.r3),
2998 le32_to_cpu(tr.r4), le32_to_cpu(tr.r5),
2999 le32_to_cpu(tr.r6), le32_to_cpu(tr.r7));
3000
Piotr Haberbaa9e602013-04-05 10:57:42 +02003001 return simple_read_from_buffer(data, count, &pos, buf, res);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003002}
3003
3004static int brcmf_sdio_assert_info(struct brcmf_sdio *bus,
3005 struct sdpcm_shared *sh, char __user *data,
3006 size_t count)
3007{
3008 int error = 0;
3009 char buf[200];
3010 char file[80] = "?";
3011 char expr[80] = "<???>";
3012 int res;
3013 loff_t pos = 0;
3014
3015 if ((sh->flags & SDPCM_SHARED_ASSERT_BUILT) == 0) {
3016 brcmf_dbg(INFO, "firmware not built with -assert\n");
3017 return 0;
3018 } else if ((sh->flags & SDPCM_SHARED_ASSERT) == 0) {
3019 brcmf_dbg(INFO, "no assert in dongle\n");
3020 return 0;
3021 }
3022
Franky Lin38b0b0d2012-11-05 16:22:24 -08003023 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003024 if (sh->assert_file_addr != 0) {
Arend van Spriela39be272013-12-12 11:58:58 +01003025 error = brcmf_sdiod_ramrw(bus->sdiodev, false,
3026 sh->assert_file_addr, (u8 *)file, 80);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003027 if (error < 0)
3028 return error;
3029 }
3030 if (sh->assert_exp_addr != 0) {
Arend van Spriela39be272013-12-12 11:58:58 +01003031 error = brcmf_sdiod_ramrw(bus->sdiodev, false,
3032 sh->assert_exp_addr, (u8 *)expr, 80);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003033 if (error < 0)
3034 return error;
3035 }
Franky Lin38b0b0d2012-11-05 16:22:24 -08003036 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003037
3038 res = scnprintf(buf, sizeof(buf),
3039 "dongle assert: %s:%d: assert(%s)\n",
3040 file, sh->assert_line, expr);
3041 return simple_read_from_buffer(data, count, &pos, buf, res);
3042}
3043
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003044static int brcmf_sdio_checkdied(struct brcmf_sdio *bus)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003045{
3046 int error;
3047 struct sdpcm_shared sh;
3048
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003049 error = brcmf_sdio_readshared(bus, &sh);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003050
3051 if (error < 0)
3052 return error;
3053
3054 if ((sh.flags & SDPCM_SHARED_ASSERT_BUILT) == 0)
3055 brcmf_dbg(INFO, "firmware not built with -assert\n");
3056 else if (sh.flags & SDPCM_SHARED_ASSERT)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003057 brcmf_err("assertion in dongle\n");
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003058
3059 if (sh.flags & SDPCM_SHARED_TRAP)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003060 brcmf_err("firmware trap in dongle\n");
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003061
3062 return 0;
3063}
3064
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003065static int brcmf_sdio_died_dump(struct brcmf_sdio *bus, char __user *data,
3066 size_t count, loff_t *ppos)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003067{
3068 int error = 0;
3069 struct sdpcm_shared sh;
3070 int nbytes = 0;
3071 loff_t pos = *ppos;
3072
3073 if (pos != 0)
3074 return 0;
3075
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003076 error = brcmf_sdio_readshared(bus, &sh);
3077 if (error < 0)
3078 goto done;
3079
3080 error = brcmf_sdio_assert_info(bus, &sh, data, count);
3081 if (error < 0)
3082 goto done;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003083 nbytes = error;
Piotr Haberbaa9e602013-04-05 10:57:42 +02003084
3085 error = brcmf_sdio_trap_info(bus, &sh, data+nbytes, count);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003086 if (error < 0)
3087 goto done;
Piotr Haberbaa9e602013-04-05 10:57:42 +02003088 nbytes += error;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003089
Piotr Haberbaa9e602013-04-05 10:57:42 +02003090 error = brcmf_sdio_dump_console(bus, &sh, data+nbytes, count);
3091 if (error < 0)
3092 goto done;
3093 nbytes += error;
3094
3095 error = nbytes;
3096 *ppos += nbytes;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003097done:
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003098 return error;
3099}
3100
3101static ssize_t brcmf_sdio_forensic_read(struct file *f, char __user *data,
3102 size_t count, loff_t *ppos)
3103{
3104 struct brcmf_sdio *bus = f->private_data;
3105 int res;
3106
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003107 res = brcmf_sdio_died_dump(bus, data, count, ppos);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003108 if (res > 0)
3109 *ppos += res;
3110 return (ssize_t)res;
3111}
3112
3113static const struct file_operations brcmf_sdio_forensic_ops = {
3114 .owner = THIS_MODULE,
3115 .open = simple_open,
3116 .read = brcmf_sdio_forensic_read
3117};
3118
Arend van Spriel80969832012-06-09 22:51:44 +02003119static void brcmf_sdio_debugfs_create(struct brcmf_sdio *bus)
3120{
3121 struct brcmf_pub *drvr = bus->sdiodev->bus_if->drvr;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003122 struct dentry *dentry = brcmf_debugfs_get_devdir(drvr);
Arend van Spriel80969832012-06-09 22:51:44 +02003123
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003124 if (IS_ERR_OR_NULL(dentry))
3125 return;
3126
3127 debugfs_create_file("forensics", S_IRUGO, dentry, bus,
3128 &brcmf_sdio_forensic_ops);
Arend van Spriel80969832012-06-09 22:51:44 +02003129 brcmf_debugfs_create_sdio_count(drvr, &bus->sdcnt);
Daniel Kim0801e6c2014-01-29 15:32:13 +01003130 debugfs_create_u32("console_interval", 0644, dentry,
3131 &bus->console_interval);
Arend van Spriel80969832012-06-09 22:51:44 +02003132}
3133#else
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003134static int brcmf_sdio_checkdied(struct brcmf_sdio *bus)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003135{
3136 return 0;
3137}
3138
Arend van Spriel80969832012-06-09 22:51:44 +02003139static void brcmf_sdio_debugfs_create(struct brcmf_sdio *bus)
3140{
3141}
3142#endif /* DEBUG */
3143
Franky Linfcf094f2011-12-16 18:37:16 -08003144static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003145brcmf_sdio_bus_rxctl(struct device *dev, unsigned char *msg, uint msglen)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003146{
3147 int timeleft;
3148 uint rxlen = 0;
3149 bool pending;
Franky Lindd43a012012-11-05 16:22:22 -08003150 u8 *buf;
Franky Lin532cdd32011-11-22 17:21:54 -08003151 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01003152 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Lin532cdd32011-11-22 17:21:54 -08003153 struct brcmf_sdio *bus = sdiodev->bus;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003154
3155 brcmf_dbg(TRACE, "Enter\n");
3156
3157 /* Wait until control frame is available */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003158 timeleft = brcmf_sdio_dcmd_resp_wait(bus, &bus->rxlen, &pending);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003159
Franky Lindd43a012012-11-05 16:22:22 -08003160 spin_lock_bh(&bus->rxctl_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003161 rxlen = bus->rxlen;
3162 memcpy(msg, bus->rxctl, min(msglen, rxlen));
Franky Lindd43a012012-11-05 16:22:22 -08003163 bus->rxctl = NULL;
3164 buf = bus->rxctl_orig;
3165 bus->rxctl_orig = NULL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003166 bus->rxlen = 0;
Franky Lindd43a012012-11-05 16:22:22 -08003167 spin_unlock_bh(&bus->rxctl_lock);
3168 vfree(buf);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003169
3170 if (rxlen) {
3171 brcmf_dbg(CTL, "resumed on rxctl frame, got %d expected %d\n",
3172 rxlen, msglen);
3173 } else if (timeleft == 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003174 brcmf_err("resumed on timeout\n");
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003175 brcmf_sdio_checkdied(bus);
Joe Perches23677ce2012-02-09 11:17:23 +00003176 } else if (pending) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003177 brcmf_dbg(CTL, "cancelled\n");
3178 return -ERESTARTSYS;
3179 } else {
3180 brcmf_dbg(CTL, "resumed for unknown reason?\n");
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003181 brcmf_sdio_checkdied(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003182 }
3183
3184 if (rxlen)
Arend van Spriel80969832012-06-09 22:51:44 +02003185 bus->sdcnt.rx_ctlpkts++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003186 else
Arend van Spriel80969832012-06-09 22:51:44 +02003187 bus->sdcnt.rx_ctlerrs++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003188
3189 return rxlen ? (int)rxlen : -ETIMEDOUT;
3190}
3191
Hante Meulemana74d0362014-01-13 22:20:22 +01003192#ifdef DEBUG
3193static bool
3194brcmf_sdio_verifymemory(struct brcmf_sdio_dev *sdiodev, u32 ram_addr,
3195 u8 *ram_data, uint ram_sz)
3196{
3197 char *ram_cmp;
3198 int err;
3199 bool ret = true;
3200 int address;
3201 int offset;
3202 int len;
3203
3204 /* read back and verify */
3205 brcmf_dbg(INFO, "Compare RAM dl & ul at 0x%08x; size=%d\n", ram_addr,
3206 ram_sz);
3207 ram_cmp = kmalloc(MEMBLOCK, GFP_KERNEL);
3208 /* do not proceed while no memory but */
3209 if (!ram_cmp)
3210 return true;
3211
3212 address = ram_addr;
3213 offset = 0;
3214 while (offset < ram_sz) {
3215 len = ((offset + MEMBLOCK) < ram_sz) ? MEMBLOCK :
3216 ram_sz - offset;
3217 err = brcmf_sdiod_ramrw(sdiodev, false, address, ram_cmp, len);
3218 if (err) {
3219 brcmf_err("error %d on reading %d membytes at 0x%08x\n",
3220 err, len, address);
3221 ret = false;
3222 break;
3223 } else if (memcmp(ram_cmp, &ram_data[offset], len)) {
3224 brcmf_err("Downloaded RAM image is corrupted, block offset is %d, len is %d\n",
3225 offset, len);
3226 ret = false;
3227 break;
3228 }
3229 offset += len;
3230 address += len;
3231 }
3232
3233 kfree(ram_cmp);
3234
3235 return ret;
3236}
3237#else /* DEBUG */
3238static bool
3239brcmf_sdio_verifymemory(struct brcmf_sdio_dev *sdiodev, u32 ram_addr,
3240 u8 *ram_data, uint ram_sz)
3241{
3242 return true;
3243}
3244#endif /* DEBUG */
3245
Arend van Spriel33556502014-01-13 22:20:25 +01003246static int brcmf_sdio_download_code_file(struct brcmf_sdio *bus,
3247 const struct firmware *fw)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003248{
Hante Meulemanf2c44fe2013-09-25 13:05:45 +02003249 int err;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003250
Hante Meulemana74d0362014-01-13 22:20:22 +01003251 brcmf_dbg(TRACE, "Enter\n");
3252
Hante Meulemanf9951c12014-01-29 15:32:17 +01003253 err = brcmf_sdiod_ramrw(bus->sdiodev, true, bus->ci->rambase,
3254 (u8 *)fw->data, fw->size);
3255 if (err)
3256 brcmf_err("error %d on writing %d membytes at 0x%08x\n",
3257 err, (int)fw->size, bus->ci->rambase);
3258 else if (!brcmf_sdio_verifymemory(bus->sdiodev, bus->ci->rambase,
3259 (u8 *)fw->data, fw->size))
3260 err = -EIO;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003261
Hante Meulemanf2c44fe2013-09-25 13:05:45 +02003262 return err;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003263}
3264
Arend van Spriel33556502014-01-13 22:20:25 +01003265static int brcmf_sdio_download_nvram(struct brcmf_sdio *bus,
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003266 void *vars, u32 varsz)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003267{
Hante Meulemana74d0362014-01-13 22:20:22 +01003268 int address;
3269 int err;
3270
3271 brcmf_dbg(TRACE, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003272
Hante Meulemana74d0362014-01-13 22:20:22 +01003273 address = bus->ci->ramsize - varsz + bus->ci->rambase;
3274 err = brcmf_sdiod_ramrw(bus->sdiodev, true, address, vars, varsz);
3275 if (err)
3276 brcmf_err("error %d on writing %d nvram bytes at 0x%08x\n",
3277 err, varsz, address);
3278 else if (!brcmf_sdio_verifymemory(bus->sdiodev, address, vars, varsz))
3279 err = -EIO;
3280
Hante Meulemana74d0362014-01-13 22:20:22 +01003281 return err;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003282}
3283
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003284static int brcmf_sdio_download_firmware(struct brcmf_sdio *bus,
3285 const struct firmware *fw,
3286 void *nvram, u32 nvlen)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003287{
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003288 int bcmerror = -EFAULT;
Arend van Spriel33556502014-01-13 22:20:25 +01003289 u32 rstvec;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003290
3291 sdio_claim_host(bus->sdiodev->func[1]);
3292 brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003293
3294 /* Keep arm in reset */
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003295 brcmf_chip_enter_download(bus->ci);
Arend van Spriel33556502014-01-13 22:20:25 +01003296
Arend van Spriel33556502014-01-13 22:20:25 +01003297 rstvec = get_unaligned_le32(fw->data);
3298 brcmf_dbg(SDIO, "firmware rstvec: %x\n", rstvec);
3299
3300 bcmerror = brcmf_sdio_download_code_file(bus, fw);
3301 release_firmware(fw);
3302 if (bcmerror) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003303 brcmf_err("dongle image file download failed\n");
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003304 brcmf_fw_nvram_free(nvram);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003305 goto err;
3306 }
3307
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003308 bcmerror = brcmf_sdio_download_nvram(bus, nvram, nvlen);
3309 brcmf_fw_nvram_free(nvram);
Arend van Spriel33556502014-01-13 22:20:25 +01003310 if (bcmerror) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003311 brcmf_err("dongle nvram file download failed\n");
Franky Lin3eaa9562013-04-02 21:06:20 +02003312 goto err;
3313 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02003314
3315 /* Take arm out of reset */
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003316 if (!brcmf_chip_exit_download(bus->ci, rstvec)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003317 brcmf_err("error getting out of ARM core reset\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003318 goto err;
3319 }
3320
Arend van Spriel33556502014-01-13 22:20:25 +01003321 /* Allow HT Clock now that the ARM is running. */
Arend van Sprielbb350712014-01-13 22:20:29 +01003322 brcmf_bus_change_state(bus->sdiodev->bus_if, BRCMF_BUS_LOAD);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003323 bcmerror = 0;
3324
3325err:
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003326 brcmf_sdio_clkctl(bus, CLK_SDONLY, false);
3327 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003328 return bcmerror;
3329}
3330
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003331static void brcmf_sdio_sr_init(struct brcmf_sdio *bus)
Piotr Haber4a3da992013-04-11 13:28:46 +02003332{
3333 int err = 0;
3334 u8 val;
3335
3336 brcmf_dbg(TRACE, "Enter\n");
3337
Arend van Spriela39be272013-12-12 11:58:58 +01003338 val = brcmf_sdiod_regrb(bus->sdiodev, SBSDIO_FUNC1_WAKEUPCTRL, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003339 if (err) {
3340 brcmf_err("error reading SBSDIO_FUNC1_WAKEUPCTRL\n");
3341 return;
3342 }
3343
3344 val |= 1 << SBSDIO_FUNC1_WCTRL_HTWAIT_SHIFT;
Arend van Spriela39be272013-12-12 11:58:58 +01003345 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_WAKEUPCTRL, val, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003346 if (err) {
3347 brcmf_err("error writing SBSDIO_FUNC1_WAKEUPCTRL\n");
3348 return;
3349 }
3350
3351 /* Add CMD14 Support */
Arend van Spriela39be272013-12-12 11:58:58 +01003352 brcmf_sdiod_regwb(bus->sdiodev, SDIO_CCCR_BRCM_CARDCAP,
3353 (SDIO_CCCR_BRCM_CARDCAP_CMD14_SUPPORT |
3354 SDIO_CCCR_BRCM_CARDCAP_CMD14_EXT),
3355 &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003356 if (err) {
3357 brcmf_err("error writing SDIO_CCCR_BRCM_CARDCAP\n");
3358 return;
3359 }
3360
Arend van Spriela39be272013-12-12 11:58:58 +01003361 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3362 SBSDIO_FORCE_HT, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003363 if (err) {
3364 brcmf_err("error writing SBSDIO_FUNC1_CHIPCLKCSR\n");
3365 return;
3366 }
3367
3368 /* set flag */
3369 bus->sr_enabled = true;
3370 brcmf_dbg(INFO, "SR enabled\n");
3371}
3372
3373/* enable KSO bit */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003374static int brcmf_sdio_kso_init(struct brcmf_sdio *bus)
Piotr Haber4a3da992013-04-11 13:28:46 +02003375{
3376 u8 val;
3377 int err = 0;
3378
3379 brcmf_dbg(TRACE, "Enter\n");
3380
3381 /* KSO bit added in SDIO core rev 12 */
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003382 if (brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV)->rev < 12)
Piotr Haber4a3da992013-04-11 13:28:46 +02003383 return 0;
3384
Arend van Spriela39be272013-12-12 11:58:58 +01003385 val = brcmf_sdiod_regrb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003386 if (err) {
3387 brcmf_err("error reading SBSDIO_FUNC1_SLEEPCSR\n");
3388 return err;
3389 }
3390
3391 if (!(val & SBSDIO_FUNC1_SLEEPCSR_KSO_MASK)) {
3392 val |= (SBSDIO_FUNC1_SLEEPCSR_KSO_EN <<
3393 SBSDIO_FUNC1_SLEEPCSR_KSO_SHIFT);
Arend van Spriela39be272013-12-12 11:58:58 +01003394 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
3395 val, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003396 if (err) {
3397 brcmf_err("error writing SBSDIO_FUNC1_SLEEPCSR\n");
3398 return err;
3399 }
3400 }
3401
3402 return 0;
3403}
3404
3405
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003406static int brcmf_sdio_bus_preinit(struct device *dev)
Arend van Sprielcf458282013-11-29 11:48:15 +01003407{
3408 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3409 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3410 struct brcmf_sdio *bus = sdiodev->bus;
Franky Lin8da9d2c2013-11-29 12:25:10 +01003411 uint pad_size;
Arend van Sprielcf458282013-11-29 11:48:15 +01003412 u32 value;
Arend van Sprielcf458282013-11-29 11:48:15 +01003413 int err;
3414
Franky Lin8da9d2c2013-11-29 12:25:10 +01003415 /* the commands below use the terms tx and rx from
3416 * a device perspective, ie. bus:txglom affects the
3417 * bus transfers from device to host.
3418 */
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003419 if (brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV)->rev < 12) {
Arend van Sprielcf458282013-11-29 11:48:15 +01003420 /* for sdio core rev < 12, disable txgloming */
3421 value = 0;
3422 err = brcmf_iovar_data_set(dev, "bus:txglom", &value,
3423 sizeof(u32));
3424 } else {
3425 /* otherwise, set txglomalign */
3426 value = 4;
3427 if (sdiodev->pdata)
3428 value = sdiodev->pdata->sd_sgentry_align;
3429 /* SDIO ADMA requires at least 32 bit alignment */
3430 value = max_t(u32, value, 4);
3431 err = brcmf_iovar_data_set(dev, "bus:txglomalign", &value,
3432 sizeof(u32));
3433 }
Franky Lin8da9d2c2013-11-29 12:25:10 +01003434
3435 if (err < 0)
3436 goto done;
3437
3438 bus->tx_hdrlen = SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN;
3439 if (sdiodev->sg_support) {
3440 bus->txglom = false;
3441 value = 1;
3442 pad_size = bus->sdiodev->func[2]->cur_blksize << 1;
Franky Lin8da9d2c2013-11-29 12:25:10 +01003443 err = brcmf_iovar_data_set(bus->sdiodev->dev, "bus:rxglom",
3444 &value, sizeof(u32));
3445 if (err < 0) {
3446 /* bus:rxglom is allowed to fail */
3447 err = 0;
3448 } else {
3449 bus->txglom = true;
3450 bus->tx_hdrlen += SDPCM_HWEXT_LEN;
3451 }
3452 }
3453 brcmf_bus_add_txhdrlen(bus->sdiodev->dev, bus->tx_hdrlen);
3454
3455done:
Arend van Sprielcf458282013-11-29 11:48:15 +01003456 return err;
3457}
3458
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003459void brcmf_sdio_isr(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003460{
Arend van Spriel5b435de2011-10-05 13:19:03 +02003461 brcmf_dbg(TRACE, "Enter\n");
3462
3463 if (!bus) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003464 brcmf_err("bus is null pointer, exiting\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003465 return;
3466 }
3467
Arend van Sprielbb350712014-01-13 22:20:29 +01003468 if (!brcmf_bus_ready(bus->sdiodev->bus_if)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003469 brcmf_err("bus is down. we have nothing to do\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003470 return;
3471 }
3472 /* Count the interrupt call */
Arend van Spriel80969832012-06-09 22:51:44 +02003473 bus->sdcnt.intrcount++;
Franky Lin45316032012-09-13 21:12:03 +02003474 if (in_interrupt())
3475 atomic_set(&bus->ipend, 1);
3476 else
3477 if (brcmf_sdio_intr_rstatus(bus)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003478 brcmf_err("failed backplane access\n");
Franky Lin45316032012-09-13 21:12:03 +02003479 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02003480
Arend van Spriel5b435de2011-10-05 13:19:03 +02003481 /* Disable additional interrupts (is this needed now)? */
3482 if (!bus->intr)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003483 brcmf_err("isr w/o interrupt configured!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003484
Arend van Sprielfccfe932013-06-18 13:29:26 +02003485 atomic_inc(&bus->dpc_tskcnt);
Franky Linf1e68c22012-09-13 21:12:00 +02003486 queue_work(bus->brcmf_wq, &bus->datawork);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003487}
3488
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003489static bool brcmf_sdio_bus_watchdog(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003490{
Joe Perches8ae74652012-01-15 00:38:38 -08003491#ifdef DEBUG
Franky Lincad2b262011-11-22 17:21:53 -08003492 struct brcmf_bus *bus_if = dev_get_drvdata(bus->sdiodev->dev);
Joe Perches8ae74652012-01-15 00:38:38 -08003493#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02003494
3495 brcmf_dbg(TIMER, "Enter\n");
3496
Arend van Spriel5b435de2011-10-05 13:19:03 +02003497 /* Poll period: check device if appropriate. */
Piotr Haber4a3da992013-04-11 13:28:46 +02003498 if (!bus->sr_enabled &&
3499 bus->poll && (++bus->polltick >= bus->pollrate)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003500 u32 intstatus = 0;
3501
3502 /* Reset poll tick */
3503 bus->polltick = 0;
3504
3505 /* Check device if no interrupts */
Arend van Spriel80969832012-06-09 22:51:44 +02003506 if (!bus->intr ||
3507 (bus->sdcnt.intrcount == bus->sdcnt.lastintrs)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003508
Arend van Sprielfccfe932013-06-18 13:29:26 +02003509 if (atomic_read(&bus->dpc_tskcnt) == 0) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003510 u8 devpend;
Arend van Sprielfccfe932013-06-18 13:29:26 +02003511
Franky Lin38b0b0d2012-11-05 16:22:24 -08003512 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriela39be272013-12-12 11:58:58 +01003513 devpend = brcmf_sdiod_regrb(bus->sdiodev,
3514 SDIO_CCCR_INTx,
3515 NULL);
Franky Lin38b0b0d2012-11-05 16:22:24 -08003516 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003517 intstatus =
3518 devpend & (INTR_STATUS_FUNC1 |
3519 INTR_STATUS_FUNC2);
3520 }
3521
3522 /* If there is something, make like the ISR and
3523 schedule the DPC */
3524 if (intstatus) {
Arend van Spriel80969832012-06-09 22:51:44 +02003525 bus->sdcnt.pollcnt++;
Franky Lin1d382272012-09-13 21:11:59 +02003526 atomic_set(&bus->ipend, 1);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003527
Arend van Sprielfccfe932013-06-18 13:29:26 +02003528 atomic_inc(&bus->dpc_tskcnt);
Franky Linf1e68c22012-09-13 21:12:00 +02003529 queue_work(bus->brcmf_wq, &bus->datawork);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003530 }
3531 }
3532
3533 /* Update interrupt tracking */
Arend van Spriel80969832012-06-09 22:51:44 +02003534 bus->sdcnt.lastintrs = bus->sdcnt.intrcount;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003535 }
Joe Perches8ae74652012-01-15 00:38:38 -08003536#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02003537 /* Poll for console output periodically */
Hante Meuleman2def5c12012-11-14 18:46:19 -08003538 if (bus_if && bus_if->state == BRCMF_BUS_DATA &&
Franky Lin8d169aa2011-11-22 17:21:52 -08003539 bus->console_interval != 0) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003540 bus->console.count += BRCMF_WD_POLL_MS;
3541 if (bus->console.count >= bus->console_interval) {
3542 bus->console.count -= bus->console_interval;
Franky Lin38b0b0d2012-11-05 16:22:24 -08003543 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003544 /* Make sure backplane clock is on */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003545 brcmf_sdio_bus_sleep(bus, false, false);
3546 if (brcmf_sdio_readconsole(bus) < 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003547 /* stop on error */
3548 bus->console_interval = 0;
Franky Lin38b0b0d2012-11-05 16:22:24 -08003549 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003550 }
3551 }
Joe Perches8ae74652012-01-15 00:38:38 -08003552#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02003553
3554 /* On idle timeout clear activity flag and/or turn off clock */
3555 if ((bus->idletime > 0) && (bus->clkstate == CLK_AVAIL)) {
3556 if (++bus->idlecount >= bus->idletime) {
3557 bus->idlecount = 0;
3558 if (bus->activity) {
3559 bus->activity = false;
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 } else {
Piotr Haber4a3da992013-04-11 13:28:46 +02003562 brcmf_dbg(SDIO, "idle\n");
Franky Lin38b0b0d2012-11-05 16:22:24 -08003563 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003564 brcmf_sdio_bus_sleep(bus, true, false);
Franky Lin38b0b0d2012-11-05 16:22:24 -08003565 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003566 }
3567 }
3568 }
3569
Franky Lin1d382272012-09-13 21:11:59 +02003570 return (atomic_read(&bus->ipend) > 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003571}
3572
Franky Linf1e68c22012-09-13 21:12:00 +02003573static void brcmf_sdio_dataworker(struct work_struct *work)
3574{
3575 struct brcmf_sdio *bus = container_of(work, struct brcmf_sdio,
3576 datawork);
Franky Linf1e68c22012-09-13 21:12:00 +02003577
Arend van Sprielfccfe932013-06-18 13:29:26 +02003578 while (atomic_read(&bus->dpc_tskcnt)) {
Hante Meuleman71abdc02014-02-25 20:30:31 +01003579 atomic_set(&bus->dpc_tskcnt, 0);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003580 brcmf_sdio_dpc(bus);
Franky Linf1e68c22012-09-13 21:12:00 +02003581 }
Franky Linf1e68c22012-09-13 21:12:00 +02003582}
3583
Arend van Spriel65d80d02014-01-29 15:32:14 +01003584static void
3585brcmf_sdio_drivestrengthinit(struct brcmf_sdio_dev *sdiodev,
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003586 struct brcmf_chip *ci, u32 drivestrength)
Arend van Spriel65d80d02014-01-29 15:32:14 +01003587{
3588 const struct sdiod_drive_str *str_tab = NULL;
3589 u32 str_mask;
3590 u32 str_shift;
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003591 u32 base;
Arend van Spriel65d80d02014-01-29 15:32:14 +01003592 u32 i;
3593 u32 drivestrength_sel = 0;
3594 u32 cc_data_temp;
3595 u32 addr;
3596
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003597 if (!(ci->cc_caps & CC_CAP_PMU))
Arend van Spriel65d80d02014-01-29 15:32:14 +01003598 return;
3599
3600 switch (SDIOD_DRVSTR_KEY(ci->chip, ci->pmurev)) {
Hante Meuleman5779ae62014-07-12 08:49:34 +02003601 case SDIOD_DRVSTR_KEY(BRCM_CC_4330_CHIP_ID, 12):
Arend van Spriel65d80d02014-01-29 15:32:14 +01003602 str_tab = sdiod_drvstr_tab1_1v8;
3603 str_mask = 0x00003800;
3604 str_shift = 11;
3605 break;
Hante Meuleman5779ae62014-07-12 08:49:34 +02003606 case SDIOD_DRVSTR_KEY(BRCM_CC_4334_CHIP_ID, 17):
Arend van Spriel65d80d02014-01-29 15:32:14 +01003607 str_tab = sdiod_drvstr_tab6_1v8;
3608 str_mask = 0x00001800;
3609 str_shift = 11;
3610 break;
Hante Meuleman5779ae62014-07-12 08:49:34 +02003611 case SDIOD_DRVSTR_KEY(BRCM_CC_43143_CHIP_ID, 17):
Arend van Spriel65d80d02014-01-29 15:32:14 +01003612 /* note: 43143 does not support tristate */
3613 i = ARRAY_SIZE(sdiod_drvstr_tab2_3v3) - 1;
3614 if (drivestrength >= sdiod_drvstr_tab2_3v3[i].strength) {
3615 str_tab = sdiod_drvstr_tab2_3v3;
3616 str_mask = 0x00000007;
3617 str_shift = 0;
3618 } else
3619 brcmf_err("Invalid SDIO Drive strength for chip %s, strength=%d\n",
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003620 ci->name, drivestrength);
Arend van Spriel65d80d02014-01-29 15:32:14 +01003621 break;
Hante Meuleman5779ae62014-07-12 08:49:34 +02003622 case SDIOD_DRVSTR_KEY(BRCM_CC_43362_CHIP_ID, 13):
Arend van Spriel65d80d02014-01-29 15:32:14 +01003623 str_tab = sdiod_drive_strength_tab5_1v8;
3624 str_mask = 0x00003800;
3625 str_shift = 11;
3626 break;
3627 default:
3628 brcmf_err("No SDIO Drive strength init done for chip %s rev %d pmurev %d\n",
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003629 ci->name, ci->chiprev, ci->pmurev);
Arend van Spriel65d80d02014-01-29 15:32:14 +01003630 break;
3631 }
3632
3633 if (str_tab != NULL) {
3634 for (i = 0; str_tab[i].strength != 0; i++) {
3635 if (drivestrength >= str_tab[i].strength) {
3636 drivestrength_sel = str_tab[i].sel;
3637 break;
3638 }
3639 }
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003640 base = brcmf_chip_get_chipcommon(ci)->base;
Arend van Spriel65d80d02014-01-29 15:32:14 +01003641 addr = CORE_CC_REG(base, chipcontrol_addr);
3642 brcmf_sdiod_regwl(sdiodev, addr, 1, NULL);
3643 cc_data_temp = brcmf_sdiod_regrl(sdiodev, addr, NULL);
3644 cc_data_temp &= ~str_mask;
3645 drivestrength_sel <<= str_shift;
3646 cc_data_temp |= drivestrength_sel;
3647 brcmf_sdiod_regwl(sdiodev, addr, cc_data_temp, NULL);
3648
3649 brcmf_dbg(INFO, "SDIO: %d mA (req=%d mA) drive strength selected, set to 0x%08x\n",
3650 str_tab[i].strength, drivestrength, cc_data_temp);
3651 }
3652}
3653
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003654static int brcmf_sdio_buscoreprep(void *ctx)
Arend van Spriel65d80d02014-01-29 15:32:14 +01003655{
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003656 struct brcmf_sdio_dev *sdiodev = ctx;
Arend van Spriel65d80d02014-01-29 15:32:14 +01003657 int err = 0;
3658 u8 clkval, clkset;
3659
3660 /* Try forcing SDIO core to do ALPAvail request only */
3661 clkset = SBSDIO_FORCE_HW_CLKREQ_OFF | SBSDIO_ALP_AVAIL_REQ;
3662 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, clkset, &err);
3663 if (err) {
3664 brcmf_err("error writing for HT off\n");
3665 return err;
3666 }
3667
3668 /* If register supported, wait for ALPAvail and then force ALP */
3669 /* This may take up to 15 milliseconds */
3670 clkval = brcmf_sdiod_regrb(sdiodev,
3671 SBSDIO_FUNC1_CHIPCLKCSR, NULL);
3672
3673 if ((clkval & ~SBSDIO_AVBITS) != clkset) {
3674 brcmf_err("ChipClkCSR access: wrote 0x%02x read 0x%02x\n",
3675 clkset, clkval);
3676 return -EACCES;
3677 }
3678
3679 SPINWAIT(((clkval = brcmf_sdiod_regrb(sdiodev,
3680 SBSDIO_FUNC1_CHIPCLKCSR, NULL)),
3681 !SBSDIO_ALPAV(clkval)),
3682 PMU_MAX_TRANSITION_DLY);
3683 if (!SBSDIO_ALPAV(clkval)) {
3684 brcmf_err("timeout on ALPAV wait, clkval 0x%02x\n",
3685 clkval);
3686 return -EBUSY;
3687 }
3688
3689 clkset = SBSDIO_FORCE_HW_CLKREQ_OFF | SBSDIO_FORCE_ALP;
3690 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, clkset, &err);
3691 udelay(65);
3692
3693 /* Also, disable the extra SDIO pull-ups */
3694 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_SDIOPULLUP, 0, NULL);
3695
3696 return 0;
3697}
3698
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003699static void brcmf_sdio_buscore_exitdl(void *ctx, struct brcmf_chip *chip,
3700 u32 rstvec)
3701{
3702 struct brcmf_sdio_dev *sdiodev = ctx;
3703 struct brcmf_core *core;
3704 u32 reg_addr;
3705
3706 /* clear all interrupts */
3707 core = brcmf_chip_get_core(chip, BCMA_CORE_SDIO_DEV);
3708 reg_addr = core->base + offsetof(struct sdpcmd_regs, intstatus);
3709 brcmf_sdiod_regwl(sdiodev, reg_addr, 0xFFFFFFFF, NULL);
3710
3711 if (rstvec)
3712 /* Write reset vector to address 0 */
3713 brcmf_sdiod_ramrw(sdiodev, true, 0, (void *)&rstvec,
3714 sizeof(rstvec));
3715}
3716
3717static u32 brcmf_sdio_buscore_read32(void *ctx, u32 addr)
3718{
3719 struct brcmf_sdio_dev *sdiodev = ctx;
3720 u32 val, rev;
3721
3722 val = brcmf_sdiod_regrl(sdiodev, addr, NULL);
Hante Meuleman5779ae62014-07-12 08:49:34 +02003723 if (sdiodev->func[0]->device == BRCM_SDIO_4335_4339_DEVICE_ID &&
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003724 addr == CORE_CC_REG(SI_ENUM_BASE, chipid)) {
3725 rev = (val & CID_REV_MASK) >> CID_REV_SHIFT;
3726 if (rev >= 2) {
3727 val &= ~CID_ID_MASK;
Hante Meuleman5779ae62014-07-12 08:49:34 +02003728 val |= BRCM_CC_4339_CHIP_ID;
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003729 }
3730 }
3731 return val;
3732}
3733
3734static void brcmf_sdio_buscore_write32(void *ctx, u32 addr, u32 val)
3735{
3736 struct brcmf_sdio_dev *sdiodev = ctx;
3737
3738 brcmf_sdiod_regwl(sdiodev, addr, val, NULL);
3739}
3740
3741static const struct brcmf_buscore_ops brcmf_sdio_buscore_ops = {
3742 .prepare = brcmf_sdio_buscoreprep,
3743 .exit_dl = brcmf_sdio_buscore_exitdl,
3744 .read32 = brcmf_sdio_buscore_read32,
3745 .write32 = brcmf_sdio_buscore_write32,
3746};
3747
Arend van Spriel5b435de2011-10-05 13:19:03 +02003748static bool
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003749brcmf_sdio_probe_attach(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003750{
3751 u8 clkctl = 0;
3752 int err = 0;
3753 int reg_addr;
3754 u32 reg_val;
Hante Meuleman668761a2013-04-12 10:55:55 +02003755 u32 drivestrength;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003756
Franky Lin38b0b0d2012-11-05 16:22:24 -08003757 sdio_claim_host(bus->sdiodev->func[1]);
3758
Joe Perches18aad4f2012-01-15 00:38:42 -08003759 pr_debug("F1 signature read @0x18000000=0x%4x\n",
Arend van Spriela39be272013-12-12 11:58:58 +01003760 brcmf_sdiod_regrl(bus->sdiodev, SI_ENUM_BASE, NULL));
Arend van Spriel5b435de2011-10-05 13:19:03 +02003761
3762 /*
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003763 * Force PLL off until brcmf_chip_attach()
Arend van Spriel5b435de2011-10-05 13:19:03 +02003764 * programs PLL control regs
3765 */
3766
Arend van Spriela39be272013-12-12 11:58:58 +01003767 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3768 BRCMF_INIT_CLKCTL1, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003769 if (!err)
Arend van Spriela39be272013-12-12 11:58:58 +01003770 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
3771 SBSDIO_FUNC1_CHIPCLKCSR, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003772
3773 if (err || ((clkctl & ~SBSDIO_AVBITS) != BRCMF_INIT_CLKCTL1)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003774 brcmf_err("ChipClkCSR access: err %d wrote 0x%02x read 0x%02x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02003775 err, BRCMF_INIT_CLKCTL1, clkctl);
3776 goto fail;
3777 }
3778
Arend van Sprielbb350712014-01-13 22:20:29 +01003779 /* SDIO register access works so moving
3780 * state from UNKNOWN to DOWN.
3781 */
3782 brcmf_bus_change_state(bus->sdiodev->bus_if, BRCMF_BUS_DOWN);
3783
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003784 bus->ci = brcmf_chip_attach(bus->sdiodev, &brcmf_sdio_buscore_ops);
3785 if (IS_ERR(bus->ci)) {
3786 brcmf_err("brcmf_chip_attach failed!\n");
3787 bus->ci = NULL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003788 goto fail;
3789 }
3790
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003791 if (brcmf_sdio_kso_init(bus)) {
Piotr Haber4a3da992013-04-11 13:28:46 +02003792 brcmf_err("error enabling KSO\n");
3793 goto fail;
3794 }
3795
Hante Meuleman668761a2013-04-12 10:55:55 +02003796 if ((bus->sdiodev->pdata) && (bus->sdiodev->pdata->drive_strength))
3797 drivestrength = bus->sdiodev->pdata->drive_strength;
3798 else
3799 drivestrength = DEFAULT_SDIO_DRIVE_STRENGTH;
Arend van Spriel65d80d02014-01-29 15:32:14 +01003800 brcmf_sdio_drivestrengthinit(bus->sdiodev, bus->ci, drivestrength);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003801
Franky Lin454d2a82011-11-04 22:23:37 +01003802 /* Get info on the SOCRAM cores... */
Arend van Spriel5b435de2011-10-05 13:19:03 +02003803 bus->ramsize = bus->ci->ramsize;
3804 if (!(bus->ramsize)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003805 brcmf_err("failed to find SOCRAM memory!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003806 goto fail;
3807 }
3808
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003809 /* Set card control so an SDIO card reset does a WLAN backplane reset */
Arend van Spriela39be272013-12-12 11:58:58 +01003810 reg_val = brcmf_sdiod_regrb(bus->sdiodev,
3811 SDIO_CCCR_BRCM_CARDCTRL, &err);
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003812 if (err)
3813 goto fail;
3814
3815 reg_val |= SDIO_CCCR_BRCM_CARDCTRL_WLANRESET;
3816
Arend van Spriela39be272013-12-12 11:58:58 +01003817 brcmf_sdiod_regwb(bus->sdiodev,
3818 SDIO_CCCR_BRCM_CARDCTRL, reg_val, &err);
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003819 if (err)
3820 goto fail;
3821
3822 /* set PMUControl so a backplane reset does PMU state reload */
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003823 reg_addr = CORE_CC_REG(brcmf_chip_get_chipcommon(bus->ci)->base,
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003824 pmucontrol);
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003825 reg_val = brcmf_sdiod_regrl(bus->sdiodev, reg_addr, &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 Sprielcb7cf7b2014-01-29 15:32:19 +01003831 brcmf_sdiod_regwl(bus->sdiodev, reg_addr, reg_val, &err);
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003832 if (err)
3833 goto fail;
3834
Franky Lin38b0b0d2012-11-05 16:22:24 -08003835 sdio_release_host(bus->sdiodev->func[1]);
3836
Arend van Spriel5b435de2011-10-05 13:19:03 +02003837 brcmu_pktq_init(&bus->txq, (PRIOMASK + 1), TXQLEN);
3838
Arend van Spriel9b2d2f2a82013-11-29 12:25:14 +01003839 /* allocate header buffer */
3840 bus->hdrbuf = kzalloc(MAX_HDR_READ + bus->head_align, GFP_KERNEL);
3841 if (!bus->hdrbuf)
3842 return false;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003843 /* Locate an appropriately-aligned portion of hdrbuf */
3844 bus->rxhdr = (u8 *) roundup((unsigned long)&bus->hdrbuf[0],
Arend van Spriel9b2d2f2a82013-11-29 12:25:14 +01003845 bus->head_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003846
3847 /* Set the poll and/or interrupt flags */
3848 bus->intr = true;
3849 bus->poll = false;
3850 if (bus->poll)
3851 bus->pollrate = 1;
3852
3853 return true;
3854
3855fail:
Franky Lin38b0b0d2012-11-05 16:22:24 -08003856 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003857 return false;
3858}
3859
Arend van Spriel5b435de2011-10-05 13:19:03 +02003860static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003861brcmf_sdio_watchdog_thread(void *data)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003862{
Franky Line92eedf2011-11-22 17:21:50 -08003863 struct brcmf_sdio *bus = (struct brcmf_sdio *)data;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003864
3865 allow_signal(SIGTERM);
3866 /* Run until signal received */
3867 while (1) {
3868 if (kthread_should_stop())
3869 break;
3870 if (!wait_for_completion_interruptible(&bus->watchdog_wait)) {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003871 brcmf_sdio_bus_watchdog(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003872 /* Count the tick for reference */
Arend van Spriel80969832012-06-09 22:51:44 +02003873 bus->sdcnt.tickcnt++;
Arend van Spriel58e9df42014-03-20 10:18:01 +01003874 reinit_completion(&bus->watchdog_wait);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003875 } else
3876 break;
3877 }
3878 return 0;
3879}
3880
3881static void
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003882brcmf_sdio_watchdog(unsigned long data)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003883{
Franky Line92eedf2011-11-22 17:21:50 -08003884 struct brcmf_sdio *bus = (struct brcmf_sdio *)data;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003885
3886 if (bus->watchdog_tsk) {
3887 complete(&bus->watchdog_wait);
3888 /* Reschedule the watchdog */
3889 if (bus->wd_timer_valid)
3890 mod_timer(&bus->timer,
3891 jiffies + BRCMF_WD_POLL_MS * HZ / 1000);
3892 }
3893}
3894
Arend van Sprield9cb2592012-12-05 15:25:54 +01003895static struct brcmf_bus_ops brcmf_sdio_bus_ops = {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003896 .stop = brcmf_sdio_bus_stop,
3897 .preinit = brcmf_sdio_bus_preinit,
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003898 .txdata = brcmf_sdio_bus_txdata,
3899 .txctl = brcmf_sdio_bus_txctl,
3900 .rxctl = brcmf_sdio_bus_rxctl,
3901 .gettxq = brcmf_sdio_bus_gettxq,
Arend van Sprield9cb2592012-12-05 15:25:54 +01003902};
3903
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003904static void brcmf_sdio_firmware_callback(struct device *dev,
3905 const struct firmware *code,
3906 void *nvram, u32 nvram_len)
3907{
3908 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3909 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3910 struct brcmf_sdio *bus = sdiodev->bus;
3911 int err = 0;
3912 u8 saveclk;
3913
3914 brcmf_dbg(TRACE, "Enter: dev=%s\n", dev_name(dev));
3915
3916 /* try to download image and nvram to the dongle */
3917 if (bus_if->state == BRCMF_BUS_DOWN) {
3918 bus->alp_only = true;
3919 err = brcmf_sdio_download_firmware(bus, code, nvram, nvram_len);
3920 if (err)
3921 goto fail;
3922 bus->alp_only = false;
3923 }
3924
3925 if (!bus_if->drvr)
3926 return;
3927
3928 /* Start the watchdog timer */
3929 bus->sdcnt.tickcnt = 0;
3930 brcmf_sdio_wd_timer(bus, BRCMF_WD_POLL_MS);
3931
3932 sdio_claim_host(sdiodev->func[1]);
3933
3934 /* Make sure backplane clock is on, needed to generate F2 interrupt */
3935 brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
3936 if (bus->clkstate != CLK_AVAIL)
3937 goto release;
3938
3939 /* Force clocks on backplane to be sure F2 interrupt propagates */
3940 saveclk = brcmf_sdiod_regrb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, &err);
3941 if (!err) {
3942 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3943 (saveclk | SBSDIO_FORCE_HT), &err);
3944 }
3945 if (err) {
3946 brcmf_err("Failed to force clock for F2: err %d\n", err);
3947 goto release;
3948 }
3949
3950 /* Enable function 2 (frame transfers) */
3951 w_sdreg32(bus, SDPCM_PROT_VERSION << SMB_DATA_VERSION_SHIFT,
3952 offsetof(struct sdpcmd_regs, tosbmailboxdata));
3953 err = sdio_enable_func(sdiodev->func[SDIO_FUNC_2]);
3954
3955
3956 brcmf_dbg(INFO, "enable F2: err=%d\n", err);
3957
3958 /* If F2 successfully enabled, set core and enable interrupts */
3959 if (!err) {
3960 /* Set up the interrupt mask and enable interrupts */
3961 bus->hostintmask = HOSTINTMASK;
3962 w_sdreg32(bus, bus->hostintmask,
3963 offsetof(struct sdpcmd_regs, hostintmask));
3964
3965 brcmf_sdiod_regwb(sdiodev, SBSDIO_WATERMARK, 8, &err);
3966 } else {
3967 /* Disable F2 again */
3968 sdio_disable_func(sdiodev->func[SDIO_FUNC_2]);
3969 goto release;
3970 }
3971
3972 if (brcmf_chip_sr_capable(bus->ci)) {
3973 brcmf_sdio_sr_init(bus);
3974 } else {
3975 /* Restore previous clock setting */
3976 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3977 saveclk, &err);
3978 }
3979
3980 if (err == 0) {
3981 err = brcmf_sdiod_intr_register(sdiodev);
3982 if (err != 0)
3983 brcmf_err("intr register failed:%d\n", err);
3984 }
3985
3986 /* If we didn't come up, turn off backplane clock */
3987 if (err != 0)
3988 brcmf_sdio_clkctl(bus, CLK_NONE, false);
3989
3990 sdio_release_host(sdiodev->func[1]);
3991
3992 err = brcmf_bus_start(dev);
3993 if (err != 0) {
3994 brcmf_err("dongle is not responding\n");
3995 goto fail;
3996 }
3997 return;
3998
3999release:
4000 sdio_release_host(sdiodev->func[1]);
4001fail:
4002 brcmf_dbg(TRACE, "failed: dev=%s, err=%d\n", dev_name(dev), err);
4003 device_release_driver(dev);
4004}
4005
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01004006struct brcmf_sdio *brcmf_sdio_probe(struct brcmf_sdio_dev *sdiodev)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004007{
4008 int ret;
Franky Line92eedf2011-11-22 17:21:50 -08004009 struct brcmf_sdio *bus;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004010
Arend van Spriel5b435de2011-10-05 13:19:03 +02004011 brcmf_dbg(TRACE, "Enter\n");
4012
Arend van Spriel5b435de2011-10-05 13:19:03 +02004013 /* Allocate private bus interface state */
Franky Line92eedf2011-11-22 17:21:50 -08004014 bus = kzalloc(sizeof(struct brcmf_sdio), GFP_ATOMIC);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004015 if (!bus)
4016 goto fail;
4017
4018 bus->sdiodev = sdiodev;
4019 sdiodev->bus = bus;
Arend van Sprielb83db862011-10-19 12:51:09 +02004020 skb_queue_head_init(&bus->glom);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004021 bus->txbound = BRCMF_TXBOUND;
4022 bus->rxbound = BRCMF_RXBOUND;
4023 bus->txminmax = BRCMF_TXMINMAX;
Franky Lin6bc52312013-08-10 12:27:28 +02004024 bus->tx_seq = SDPCM_SEQ_WRAP - 1;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004025
Arend van Spriele217d1c2013-11-29 12:25:13 +01004026 /* platform specific configuration:
4027 * alignments must be at least 4 bytes for ADMA
4028 */
4029 bus->head_align = ALIGNMENT;
4030 bus->sgentry_align = ALIGNMENT;
4031 if (sdiodev->pdata) {
4032 if (sdiodev->pdata->sd_head_align > ALIGNMENT)
4033 bus->head_align = sdiodev->pdata->sd_head_align;
4034 if (sdiodev->pdata->sd_sgentry_align > ALIGNMENT)
4035 bus->sgentry_align = sdiodev->pdata->sd_sgentry_align;
4036 }
4037
Hante Meuleman37ac5782012-11-14 18:46:18 -08004038 INIT_WORK(&bus->datawork, brcmf_sdio_dataworker);
4039 bus->brcmf_wq = create_singlethread_workqueue("brcmf_wq");
4040 if (bus->brcmf_wq == NULL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01004041 brcmf_err("insufficient memory to create txworkqueue\n");
Hante Meuleman37ac5782012-11-14 18:46:18 -08004042 goto fail;
4043 }
4044
Arend van Spriel5b435de2011-10-05 13:19:03 +02004045 /* attempt to attach to the dongle */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01004046 if (!(brcmf_sdio_probe_attach(bus))) {
4047 brcmf_err("brcmf_sdio_probe_attach failed\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02004048 goto fail;
4049 }
4050
Franky Lindd43a012012-11-05 16:22:22 -08004051 spin_lock_init(&bus->rxctl_lock);
Hante Meulemanfed7ec42014-03-15 17:18:18 +01004052 spin_lock_init(&bus->txq_lock);
4053 sema_init(&bus->tx_seq_lock, 1);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004054 init_waitqueue_head(&bus->ctrl_wait);
4055 init_waitqueue_head(&bus->dcmd_resp_wait);
4056
4057 /* Set up the watchdog timer */
4058 init_timer(&bus->timer);
4059 bus->timer.data = (unsigned long)bus;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01004060 bus->timer.function = brcmf_sdio_watchdog;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004061
Arend van Spriel5b435de2011-10-05 13:19:03 +02004062 /* Initialize watchdog thread */
4063 init_completion(&bus->watchdog_wait);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01004064 bus->watchdog_tsk = kthread_run(brcmf_sdio_watchdog_thread,
Arend van Spriel5b435de2011-10-05 13:19:03 +02004065 bus, "brcmf_watchdog");
4066 if (IS_ERR(bus->watchdog_tsk)) {
Joe Perches02f77192012-01-15 00:38:44 -08004067 pr_warn("brcmf_watchdog thread failed to start\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02004068 bus->watchdog_tsk = NULL;
4069 }
4070 /* Initialize DPC thread */
Arend van Sprielfccfe932013-06-18 13:29:26 +02004071 atomic_set(&bus->dpc_tskcnt, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004072
Franky Lina9ffda82011-12-16 18:37:12 -08004073 /* Assign bus interface call back */
Arend van Sprield9cb2592012-12-05 15:25:54 +01004074 bus->sdiodev->bus_if->dev = bus->sdiodev->dev;
4075 bus->sdiodev->bus_if->ops = &brcmf_sdio_bus_ops;
Arend van Spriel75d907d2013-02-06 18:40:45 +01004076 bus->sdiodev->bus_if->chip = bus->ci->chip;
4077 bus->sdiodev->bus_if->chiprev = bus->ci->chiprev;
Arend van Sprield9cb2592012-12-05 15:25:54 +01004078
Franky Lin706478c2013-08-10 12:27:29 +02004079 /* default sdio bus header length for tx packet */
4080 bus->tx_hdrlen = SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN;
4081
4082 /* Attach to the common layer, reserve hdr space */
Arend van Spriel8dee77b2013-11-29 11:48:14 +01004083 ret = brcmf_attach(bus->sdiodev->dev);
Franky Lin712ac5b2011-12-16 18:37:09 -08004084 if (ret != 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01004085 brcmf_err("brcmf_attach failed\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02004086 goto fail;
4087 }
4088
Daniel Kim7dd3abc2014-05-27 12:56:26 +02004089 /* Query the F2 block size, set roundup accordingly */
4090 bus->blocksize = bus->sdiodev->func[2]->cur_blksize;
4091 bus->roundup = min(max_roundup, bus->blocksize);
4092
Arend van Spriel5b435de2011-10-05 13:19:03 +02004093 /* Allocate buffers */
Arend van Sprielfad13222014-01-06 12:40:40 +01004094 if (bus->sdiodev->bus_if->maxctl) {
Daniel Kim7dd3abc2014-05-27 12:56:26 +02004095 bus->sdiodev->bus_if->maxctl += bus->roundup;
Arend van Sprielfad13222014-01-06 12:40:40 +01004096 bus->rxblen =
4097 roundup((bus->sdiodev->bus_if->maxctl + SDPCM_HDRLEN),
4098 ALIGNMENT) + bus->head_align;
4099 bus->rxbuf = kmalloc(bus->rxblen, GFP_ATOMIC);
4100 if (!(bus->rxbuf)) {
4101 brcmf_err("rxbuf allocation failed\n");
4102 goto fail;
4103 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02004104 }
4105
Arend van Sprielfad13222014-01-06 12:40:40 +01004106 sdio_claim_host(bus->sdiodev->func[1]);
4107
4108 /* Disable F2 to clear any intermediate frame state on the dongle */
4109 sdio_disable_func(bus->sdiodev->func[SDIO_FUNC_2]);
4110
Arend van Sprielfad13222014-01-06 12:40:40 +01004111 bus->rxflow = false;
4112
4113 /* Done with backplane-dependent accesses, can drop clock... */
4114 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, 0, NULL);
4115
4116 sdio_release_host(bus->sdiodev->func[1]);
4117
4118 /* ...and initialize clock/power states */
4119 bus->clkstate = CLK_SDONLY;
4120 bus->idletime = BRCMF_IDLE_INTERVAL;
4121 bus->idleclock = BRCMF_IDLE_ACTIVE;
4122
Arend van Sprielfad13222014-01-06 12:40:40 +01004123 /* SR state */
4124 bus->sleeping = false;
4125 bus->sr_enabled = false;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004126
Arend van Spriel80969832012-06-09 22:51:44 +02004127 brcmf_sdio_debugfs_create(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004128 brcmf_dbg(INFO, "completed!!\n");
4129
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02004130 ret = brcmf_fw_get_firmwares(sdiodev->dev, BRCMF_FW_REQUEST_NVRAM,
4131 brcmf_sdio_get_fwname(bus->ci,
4132 BRCMF_FIRMWARE_BIN),
4133 brcmf_sdio_get_fwname(bus->ci,
4134 BRCMF_FIRMWARE_NVRAM),
4135 brcmf_sdio_firmware_callback);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004136 if (ret != 0) {
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02004137 brcmf_err("async firmware request failed: %d\n", ret);
Hante Meuleman1799ddf2012-11-14 18:46:17 -08004138 goto fail;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004139 }
Franky Lin15d45b62011-10-21 16:16:32 +02004140
Arend van Spriel5b435de2011-10-05 13:19:03 +02004141 return bus;
4142
4143fail:
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004144 brcmf_sdio_remove(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004145 return NULL;
4146}
4147
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004148/* Detach and free everything */
4149void brcmf_sdio_remove(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004150{
Arend van Spriel5b435de2011-10-05 13:19:03 +02004151 brcmf_dbg(TRACE, "Enter\n");
4152
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004153 if (bus) {
4154 /* De-register interrupt handler */
4155 brcmf_sdiod_intr_unregister(bus->sdiodev);
4156
Arend van Spriel4faf28b2014-05-27 12:56:18 +02004157 brcmf_detach(bus->sdiodev->dev);
Arend van Sprielbfad4a02014-01-06 12:40:39 +01004158
Hante Meulemane0c180ec2014-01-29 15:32:18 +01004159 cancel_work_sync(&bus->datawork);
4160 if (bus->brcmf_wq)
4161 destroy_workqueue(bus->brcmf_wq);
4162
Arend van Sprielbfad4a02014-01-06 12:40:39 +01004163 if (bus->ci) {
Arend van Sprielbb350712014-01-13 22:20:29 +01004164 if (bus->sdiodev->bus_if->state == BRCMF_BUS_DOWN) {
4165 sdio_claim_host(bus->sdiodev->func[1]);
4166 brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
4167 /* Leave the device in state where it is
4168 * 'quiet'. This is done by putting it in
4169 * download_state which essentially resets
4170 * all necessary cores.
4171 */
4172 msleep(20);
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01004173 brcmf_chip_enter_download(bus->ci);
Arend van Sprielbb350712014-01-13 22:20:29 +01004174 brcmf_sdio_clkctl(bus, CLK_NONE, false);
4175 sdio_release_host(bus->sdiodev->func[1]);
4176 }
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01004177 brcmf_chip_detach(bus->ci);
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004178 }
4179
Arend van Sprielbfad4a02014-01-06 12:40:39 +01004180 kfree(bus->rxbuf);
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004181 kfree(bus->hdrbuf);
4182 kfree(bus);
4183 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02004184
4185 brcmf_dbg(TRACE, "Disconnected\n");
4186}
4187
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01004188void brcmf_sdio_wd_timer(struct brcmf_sdio *bus, uint wdtick)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004189{
Arend van Spriel5b435de2011-10-05 13:19:03 +02004190 /* Totally stop the timer */
Joe Perches23677ce2012-02-09 11:17:23 +00004191 if (!wdtick && bus->wd_timer_valid) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02004192 del_timer_sync(&bus->timer);
4193 bus->wd_timer_valid = false;
4194 bus->save_ms = wdtick;
4195 return;
4196 }
4197
Franky Linece960e2011-10-21 16:16:19 +02004198 /* don't start the wd until fw is loaded */
Arend van Sprield6ae2c52014-01-06 12:40:44 +01004199 if (bus->sdiodev->bus_if->state != BRCMF_BUS_DATA)
Franky Linece960e2011-10-21 16:16:19 +02004200 return;
4201
Arend van Spriel5b435de2011-10-05 13:19:03 +02004202 if (wdtick) {
4203 if (bus->save_ms != BRCMF_WD_POLL_MS) {
Joe Perches23677ce2012-02-09 11:17:23 +00004204 if (bus->wd_timer_valid)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004205 /* Stop timer and restart at new value */
4206 del_timer_sync(&bus->timer);
4207
4208 /* Create timer again when watchdog period is
4209 dynamically changed or in the first instance
4210 */
4211 bus->timer.expires =
4212 jiffies + BRCMF_WD_POLL_MS * HZ / 1000;
4213 add_timer(&bus->timer);
4214
4215 } else {
4216 /* Re arm the timer, at last watchdog period */
4217 mod_timer(&bus->timer,
4218 jiffies + BRCMF_WD_POLL_MS * HZ / 1000);
4219 }
4220
4221 bus->wd_timer_valid = true;
4222 bus->save_ms = wdtick;
4223 }
4224}