blob: 0c98a7942a0ff230c9bbc65071312d0e84bd9d34 [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 Spriel1b1e4e92014-07-12 08:49:35 +02002901static int brcmf_sdio_dump_console(struct seq_file *seq, struct brcmf_sdio *bus,
2902 struct sdpcm_shared *sh)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002903{
2904 u32 addr, console_ptr, console_size, console_index;
2905 char *conbuf = NULL;
2906 __le32 sh_val;
2907 int rv;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002908
2909 /* obtain console information from device memory */
2910 addr = sh->console_addr + offsetof(struct rte_console, log_le);
Arend van Spriela39be272013-12-12 11:58:58 +01002911 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
2912 (u8 *)&sh_val, sizeof(u32));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002913 if (rv < 0)
2914 return rv;
2915 console_ptr = le32_to_cpu(sh_val);
2916
2917 addr = sh->console_addr + offsetof(struct rte_console, log_le.buf_size);
Arend van Spriela39be272013-12-12 11:58:58 +01002918 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
2919 (u8 *)&sh_val, sizeof(u32));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002920 if (rv < 0)
2921 return rv;
2922 console_size = le32_to_cpu(sh_val);
2923
2924 addr = sh->console_addr + offsetof(struct rte_console, log_le.idx);
Arend van Spriela39be272013-12-12 11:58:58 +01002925 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
2926 (u8 *)&sh_val, sizeof(u32));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002927 if (rv < 0)
2928 return rv;
2929 console_index = le32_to_cpu(sh_val);
2930
2931 /* allocate buffer for console data */
2932 if (console_size <= CONSOLE_BUFFER_MAX)
2933 conbuf = vzalloc(console_size+1);
2934
2935 if (!conbuf)
2936 return -ENOMEM;
2937
2938 /* obtain the console data from device */
2939 conbuf[console_size] = '\0';
Arend van Spriela39be272013-12-12 11:58:58 +01002940 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, console_ptr, (u8 *)conbuf,
2941 console_size);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002942 if (rv < 0)
2943 goto done;
2944
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002945 rv = seq_write(seq, conbuf + console_index,
2946 console_size - console_index);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002947 if (rv < 0)
2948 goto done;
2949
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002950 if (console_index > 0)
2951 rv = seq_write(seq, conbuf, console_index - 1);
2952
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002953done:
2954 vfree(conbuf);
2955 return rv;
2956}
2957
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002958static int brcmf_sdio_trap_info(struct seq_file *seq, struct brcmf_sdio *bus,
2959 struct sdpcm_shared *sh)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002960{
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002961 int error;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002962 struct brcmf_trap_info tr;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002963
Piotr Haberbaa9e602013-04-05 10:57:42 +02002964 if ((sh->flags & SDPCM_SHARED_TRAP) == 0) {
2965 brcmf_dbg(INFO, "no trap in firmware\n");
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002966 return 0;
Piotr Haberbaa9e602013-04-05 10:57:42 +02002967 }
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002968
Arend van Spriela39be272013-12-12 11:58:58 +01002969 error = brcmf_sdiod_ramrw(bus->sdiodev, false, sh->trap_addr, (u8 *)&tr,
2970 sizeof(struct brcmf_trap_info));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002971 if (error < 0)
2972 return error;
2973
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002974 seq_printf(seq,
2975 "dongle trap info: type 0x%x @ epc 0x%08x\n"
2976 " cpsr 0x%08x spsr 0x%08x sp 0x%08x\n"
2977 " lr 0x%08x pc 0x%08x offset 0x%x\n"
2978 " r0 0x%08x r1 0x%08x r2 0x%08x r3 0x%08x\n"
2979 " r4 0x%08x r5 0x%08x r6 0x%08x r7 0x%08x\n",
2980 le32_to_cpu(tr.type), le32_to_cpu(tr.epc),
2981 le32_to_cpu(tr.cpsr), le32_to_cpu(tr.spsr),
2982 le32_to_cpu(tr.r13), le32_to_cpu(tr.r14),
2983 le32_to_cpu(tr.pc), sh->trap_addr,
2984 le32_to_cpu(tr.r0), le32_to_cpu(tr.r1),
2985 le32_to_cpu(tr.r2), le32_to_cpu(tr.r3),
2986 le32_to_cpu(tr.r4), le32_to_cpu(tr.r5),
2987 le32_to_cpu(tr.r6), le32_to_cpu(tr.r7));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002988
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002989 return 0;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002990}
2991
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002992static int brcmf_sdio_assert_info(struct seq_file *seq, struct brcmf_sdio *bus,
2993 struct sdpcm_shared *sh)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002994{
2995 int error = 0;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002996 char file[80] = "?";
2997 char expr[80] = "<???>";
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002998
2999 if ((sh->flags & SDPCM_SHARED_ASSERT_BUILT) == 0) {
3000 brcmf_dbg(INFO, "firmware not built with -assert\n");
3001 return 0;
3002 } else if ((sh->flags & SDPCM_SHARED_ASSERT) == 0) {
3003 brcmf_dbg(INFO, "no assert in dongle\n");
3004 return 0;
3005 }
3006
Franky Lin38b0b0d2012-11-05 16:22:24 -08003007 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003008 if (sh->assert_file_addr != 0) {
Arend van Spriela39be272013-12-12 11:58:58 +01003009 error = brcmf_sdiod_ramrw(bus->sdiodev, false,
3010 sh->assert_file_addr, (u8 *)file, 80);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003011 if (error < 0)
3012 return error;
3013 }
3014 if (sh->assert_exp_addr != 0) {
Arend van Spriela39be272013-12-12 11:58:58 +01003015 error = brcmf_sdiod_ramrw(bus->sdiodev, false,
3016 sh->assert_exp_addr, (u8 *)expr, 80);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003017 if (error < 0)
3018 return error;
3019 }
Franky Lin38b0b0d2012-11-05 16:22:24 -08003020 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003021
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003022 seq_printf(seq, "dongle assert: %s:%d: assert(%s)\n",
3023 file, sh->assert_line, expr);
3024 return 0;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003025}
3026
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003027static int brcmf_sdio_checkdied(struct brcmf_sdio *bus)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003028{
3029 int error;
3030 struct sdpcm_shared sh;
3031
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003032 error = brcmf_sdio_readshared(bus, &sh);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003033
3034 if (error < 0)
3035 return error;
3036
3037 if ((sh.flags & SDPCM_SHARED_ASSERT_BUILT) == 0)
3038 brcmf_dbg(INFO, "firmware not built with -assert\n");
3039 else if (sh.flags & SDPCM_SHARED_ASSERT)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003040 brcmf_err("assertion in dongle\n");
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003041
3042 if (sh.flags & SDPCM_SHARED_TRAP)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003043 brcmf_err("firmware trap in dongle\n");
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003044
3045 return 0;
3046}
3047
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003048static int brcmf_sdio_died_dump(struct seq_file *seq, struct brcmf_sdio *bus)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003049{
3050 int error = 0;
3051 struct sdpcm_shared sh;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003052
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003053 error = brcmf_sdio_readshared(bus, &sh);
3054 if (error < 0)
3055 goto done;
3056
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003057 error = brcmf_sdio_assert_info(seq, bus, &sh);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003058 if (error < 0)
3059 goto done;
Piotr Haberbaa9e602013-04-05 10:57:42 +02003060
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003061 error = brcmf_sdio_trap_info(seq, bus, &sh);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003062 if (error < 0)
3063 goto done;
3064
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003065 error = brcmf_sdio_dump_console(seq, bus, &sh);
Piotr Haberbaa9e602013-04-05 10:57:42 +02003066
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003067done:
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003068 return error;
3069}
3070
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003071static int brcmf_sdio_forensic_read(struct seq_file *seq, void *data)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003072{
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003073 struct brcmf_sdio *bus = seq->private;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003074
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003075 return brcmf_sdio_died_dump(seq, bus);
3076}
3077
3078static int brcmf_sdio_forensic_open(struct inode *inode, struct file *f)
3079{
3080 return single_open(f, brcmf_sdio_forensic_read, inode->i_private);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003081}
3082
3083static const struct file_operations brcmf_sdio_forensic_ops = {
3084 .owner = THIS_MODULE,
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003085 .open = brcmf_sdio_forensic_open,
3086 .release = single_release,
3087 .read = seq_read,
3088 .llseek = seq_lseek
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003089};
3090
Arend van Spriel80969832012-06-09 22:51:44 +02003091static void brcmf_sdio_debugfs_create(struct brcmf_sdio *bus)
3092{
3093 struct brcmf_pub *drvr = bus->sdiodev->bus_if->drvr;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003094 struct dentry *dentry = brcmf_debugfs_get_devdir(drvr);
Arend van Spriel80969832012-06-09 22:51:44 +02003095
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003096 if (IS_ERR_OR_NULL(dentry))
3097 return;
3098
3099 debugfs_create_file("forensics", S_IRUGO, dentry, bus,
3100 &brcmf_sdio_forensic_ops);
Arend van Spriel80969832012-06-09 22:51:44 +02003101 brcmf_debugfs_create_sdio_count(drvr, &bus->sdcnt);
Daniel Kim0801e6c2014-01-29 15:32:13 +01003102 debugfs_create_u32("console_interval", 0644, dentry,
3103 &bus->console_interval);
Arend van Spriel80969832012-06-09 22:51:44 +02003104}
3105#else
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003106static int brcmf_sdio_checkdied(struct brcmf_sdio *bus)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003107{
3108 return 0;
3109}
3110
Arend van Spriel80969832012-06-09 22:51:44 +02003111static void brcmf_sdio_debugfs_create(struct brcmf_sdio *bus)
3112{
3113}
3114#endif /* DEBUG */
3115
Franky Linfcf094f2011-12-16 18:37:16 -08003116static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003117brcmf_sdio_bus_rxctl(struct device *dev, unsigned char *msg, uint msglen)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003118{
3119 int timeleft;
3120 uint rxlen = 0;
3121 bool pending;
Franky Lindd43a012012-11-05 16:22:22 -08003122 u8 *buf;
Franky Lin532cdd32011-11-22 17:21:54 -08003123 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01003124 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Lin532cdd32011-11-22 17:21:54 -08003125 struct brcmf_sdio *bus = sdiodev->bus;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003126
3127 brcmf_dbg(TRACE, "Enter\n");
3128
3129 /* Wait until control frame is available */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003130 timeleft = brcmf_sdio_dcmd_resp_wait(bus, &bus->rxlen, &pending);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003131
Franky Lindd43a012012-11-05 16:22:22 -08003132 spin_lock_bh(&bus->rxctl_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003133 rxlen = bus->rxlen;
3134 memcpy(msg, bus->rxctl, min(msglen, rxlen));
Franky Lindd43a012012-11-05 16:22:22 -08003135 bus->rxctl = NULL;
3136 buf = bus->rxctl_orig;
3137 bus->rxctl_orig = NULL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003138 bus->rxlen = 0;
Franky Lindd43a012012-11-05 16:22:22 -08003139 spin_unlock_bh(&bus->rxctl_lock);
3140 vfree(buf);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003141
3142 if (rxlen) {
3143 brcmf_dbg(CTL, "resumed on rxctl frame, got %d expected %d\n",
3144 rxlen, msglen);
3145 } else if (timeleft == 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003146 brcmf_err("resumed on timeout\n");
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003147 brcmf_sdio_checkdied(bus);
Joe Perches23677ce2012-02-09 11:17:23 +00003148 } else if (pending) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003149 brcmf_dbg(CTL, "cancelled\n");
3150 return -ERESTARTSYS;
3151 } else {
3152 brcmf_dbg(CTL, "resumed for unknown reason?\n");
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003153 brcmf_sdio_checkdied(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003154 }
3155
3156 if (rxlen)
Arend van Spriel80969832012-06-09 22:51:44 +02003157 bus->sdcnt.rx_ctlpkts++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003158 else
Arend van Spriel80969832012-06-09 22:51:44 +02003159 bus->sdcnt.rx_ctlerrs++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003160
3161 return rxlen ? (int)rxlen : -ETIMEDOUT;
3162}
3163
Hante Meulemana74d0362014-01-13 22:20:22 +01003164#ifdef DEBUG
3165static bool
3166brcmf_sdio_verifymemory(struct brcmf_sdio_dev *sdiodev, u32 ram_addr,
3167 u8 *ram_data, uint ram_sz)
3168{
3169 char *ram_cmp;
3170 int err;
3171 bool ret = true;
3172 int address;
3173 int offset;
3174 int len;
3175
3176 /* read back and verify */
3177 brcmf_dbg(INFO, "Compare RAM dl & ul at 0x%08x; size=%d\n", ram_addr,
3178 ram_sz);
3179 ram_cmp = kmalloc(MEMBLOCK, GFP_KERNEL);
3180 /* do not proceed while no memory but */
3181 if (!ram_cmp)
3182 return true;
3183
3184 address = ram_addr;
3185 offset = 0;
3186 while (offset < ram_sz) {
3187 len = ((offset + MEMBLOCK) < ram_sz) ? MEMBLOCK :
3188 ram_sz - offset;
3189 err = brcmf_sdiod_ramrw(sdiodev, false, address, ram_cmp, len);
3190 if (err) {
3191 brcmf_err("error %d on reading %d membytes at 0x%08x\n",
3192 err, len, address);
3193 ret = false;
3194 break;
3195 } else if (memcmp(ram_cmp, &ram_data[offset], len)) {
3196 brcmf_err("Downloaded RAM image is corrupted, block offset is %d, len is %d\n",
3197 offset, len);
3198 ret = false;
3199 break;
3200 }
3201 offset += len;
3202 address += len;
3203 }
3204
3205 kfree(ram_cmp);
3206
3207 return ret;
3208}
3209#else /* DEBUG */
3210static bool
3211brcmf_sdio_verifymemory(struct brcmf_sdio_dev *sdiodev, u32 ram_addr,
3212 u8 *ram_data, uint ram_sz)
3213{
3214 return true;
3215}
3216#endif /* DEBUG */
3217
Arend van Spriel33556502014-01-13 22:20:25 +01003218static int brcmf_sdio_download_code_file(struct brcmf_sdio *bus,
3219 const struct firmware *fw)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003220{
Hante Meulemanf2c44fe2013-09-25 13:05:45 +02003221 int err;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003222
Hante Meulemana74d0362014-01-13 22:20:22 +01003223 brcmf_dbg(TRACE, "Enter\n");
3224
Hante Meulemanf9951c12014-01-29 15:32:17 +01003225 err = brcmf_sdiod_ramrw(bus->sdiodev, true, bus->ci->rambase,
3226 (u8 *)fw->data, fw->size);
3227 if (err)
3228 brcmf_err("error %d on writing %d membytes at 0x%08x\n",
3229 err, (int)fw->size, bus->ci->rambase);
3230 else if (!brcmf_sdio_verifymemory(bus->sdiodev, bus->ci->rambase,
3231 (u8 *)fw->data, fw->size))
3232 err = -EIO;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003233
Hante Meulemanf2c44fe2013-09-25 13:05:45 +02003234 return err;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003235}
3236
Arend van Spriel33556502014-01-13 22:20:25 +01003237static int brcmf_sdio_download_nvram(struct brcmf_sdio *bus,
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003238 void *vars, u32 varsz)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003239{
Hante Meulemana74d0362014-01-13 22:20:22 +01003240 int address;
3241 int err;
3242
3243 brcmf_dbg(TRACE, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003244
Hante Meulemana74d0362014-01-13 22:20:22 +01003245 address = bus->ci->ramsize - varsz + bus->ci->rambase;
3246 err = brcmf_sdiod_ramrw(bus->sdiodev, true, address, vars, varsz);
3247 if (err)
3248 brcmf_err("error %d on writing %d nvram bytes at 0x%08x\n",
3249 err, varsz, address);
3250 else if (!brcmf_sdio_verifymemory(bus->sdiodev, address, vars, varsz))
3251 err = -EIO;
3252
Hante Meulemana74d0362014-01-13 22:20:22 +01003253 return err;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003254}
3255
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003256static int brcmf_sdio_download_firmware(struct brcmf_sdio *bus,
3257 const struct firmware *fw,
3258 void *nvram, u32 nvlen)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003259{
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003260 int bcmerror = -EFAULT;
Arend van Spriel33556502014-01-13 22:20:25 +01003261 u32 rstvec;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003262
3263 sdio_claim_host(bus->sdiodev->func[1]);
3264 brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003265
3266 /* Keep arm in reset */
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003267 brcmf_chip_enter_download(bus->ci);
Arend van Spriel33556502014-01-13 22:20:25 +01003268
Arend van Spriel33556502014-01-13 22:20:25 +01003269 rstvec = get_unaligned_le32(fw->data);
3270 brcmf_dbg(SDIO, "firmware rstvec: %x\n", rstvec);
3271
3272 bcmerror = brcmf_sdio_download_code_file(bus, fw);
3273 release_firmware(fw);
3274 if (bcmerror) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003275 brcmf_err("dongle image file download failed\n");
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003276 brcmf_fw_nvram_free(nvram);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003277 goto err;
3278 }
3279
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003280 bcmerror = brcmf_sdio_download_nvram(bus, nvram, nvlen);
3281 brcmf_fw_nvram_free(nvram);
Arend van Spriel33556502014-01-13 22:20:25 +01003282 if (bcmerror) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003283 brcmf_err("dongle nvram file download failed\n");
Franky Lin3eaa9562013-04-02 21:06:20 +02003284 goto err;
3285 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02003286
3287 /* Take arm out of reset */
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003288 if (!brcmf_chip_exit_download(bus->ci, rstvec)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003289 brcmf_err("error getting out of ARM core reset\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003290 goto err;
3291 }
3292
Arend van Spriel33556502014-01-13 22:20:25 +01003293 /* Allow HT Clock now that the ARM is running. */
Arend van Sprielbb350712014-01-13 22:20:29 +01003294 brcmf_bus_change_state(bus->sdiodev->bus_if, BRCMF_BUS_LOAD);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003295 bcmerror = 0;
3296
3297err:
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003298 brcmf_sdio_clkctl(bus, CLK_SDONLY, false);
3299 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003300 return bcmerror;
3301}
3302
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003303static void brcmf_sdio_sr_init(struct brcmf_sdio *bus)
Piotr Haber4a3da992013-04-11 13:28:46 +02003304{
3305 int err = 0;
3306 u8 val;
3307
3308 brcmf_dbg(TRACE, "Enter\n");
3309
Arend van Spriela39be272013-12-12 11:58:58 +01003310 val = brcmf_sdiod_regrb(bus->sdiodev, SBSDIO_FUNC1_WAKEUPCTRL, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003311 if (err) {
3312 brcmf_err("error reading SBSDIO_FUNC1_WAKEUPCTRL\n");
3313 return;
3314 }
3315
3316 val |= 1 << SBSDIO_FUNC1_WCTRL_HTWAIT_SHIFT;
Arend van Spriela39be272013-12-12 11:58:58 +01003317 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_WAKEUPCTRL, val, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003318 if (err) {
3319 brcmf_err("error writing SBSDIO_FUNC1_WAKEUPCTRL\n");
3320 return;
3321 }
3322
3323 /* Add CMD14 Support */
Arend van Spriela39be272013-12-12 11:58:58 +01003324 brcmf_sdiod_regwb(bus->sdiodev, SDIO_CCCR_BRCM_CARDCAP,
3325 (SDIO_CCCR_BRCM_CARDCAP_CMD14_SUPPORT |
3326 SDIO_CCCR_BRCM_CARDCAP_CMD14_EXT),
3327 &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003328 if (err) {
3329 brcmf_err("error writing SDIO_CCCR_BRCM_CARDCAP\n");
3330 return;
3331 }
3332
Arend van Spriela39be272013-12-12 11:58:58 +01003333 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3334 SBSDIO_FORCE_HT, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003335 if (err) {
3336 brcmf_err("error writing SBSDIO_FUNC1_CHIPCLKCSR\n");
3337 return;
3338 }
3339
3340 /* set flag */
3341 bus->sr_enabled = true;
3342 brcmf_dbg(INFO, "SR enabled\n");
3343}
3344
3345/* enable KSO bit */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003346static int brcmf_sdio_kso_init(struct brcmf_sdio *bus)
Piotr Haber4a3da992013-04-11 13:28:46 +02003347{
3348 u8 val;
3349 int err = 0;
3350
3351 brcmf_dbg(TRACE, "Enter\n");
3352
3353 /* KSO bit added in SDIO core rev 12 */
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003354 if (brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV)->rev < 12)
Piotr Haber4a3da992013-04-11 13:28:46 +02003355 return 0;
3356
Arend van Spriela39be272013-12-12 11:58:58 +01003357 val = brcmf_sdiod_regrb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003358 if (err) {
3359 brcmf_err("error reading SBSDIO_FUNC1_SLEEPCSR\n");
3360 return err;
3361 }
3362
3363 if (!(val & SBSDIO_FUNC1_SLEEPCSR_KSO_MASK)) {
3364 val |= (SBSDIO_FUNC1_SLEEPCSR_KSO_EN <<
3365 SBSDIO_FUNC1_SLEEPCSR_KSO_SHIFT);
Arend van Spriela39be272013-12-12 11:58:58 +01003366 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
3367 val, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003368 if (err) {
3369 brcmf_err("error writing SBSDIO_FUNC1_SLEEPCSR\n");
3370 return err;
3371 }
3372 }
3373
3374 return 0;
3375}
3376
3377
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003378static int brcmf_sdio_bus_preinit(struct device *dev)
Arend van Sprielcf458282013-11-29 11:48:15 +01003379{
3380 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3381 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3382 struct brcmf_sdio *bus = sdiodev->bus;
Franky Lin8da9d2c2013-11-29 12:25:10 +01003383 uint pad_size;
Arend van Sprielcf458282013-11-29 11:48:15 +01003384 u32 value;
Arend van Sprielcf458282013-11-29 11:48:15 +01003385 int err;
3386
Franky Lin8da9d2c2013-11-29 12:25:10 +01003387 /* the commands below use the terms tx and rx from
3388 * a device perspective, ie. bus:txglom affects the
3389 * bus transfers from device to host.
3390 */
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003391 if (brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV)->rev < 12) {
Arend van Sprielcf458282013-11-29 11:48:15 +01003392 /* for sdio core rev < 12, disable txgloming */
3393 value = 0;
3394 err = brcmf_iovar_data_set(dev, "bus:txglom", &value,
3395 sizeof(u32));
3396 } else {
3397 /* otherwise, set txglomalign */
3398 value = 4;
3399 if (sdiodev->pdata)
3400 value = sdiodev->pdata->sd_sgentry_align;
3401 /* SDIO ADMA requires at least 32 bit alignment */
3402 value = max_t(u32, value, 4);
3403 err = brcmf_iovar_data_set(dev, "bus:txglomalign", &value,
3404 sizeof(u32));
3405 }
Franky Lin8da9d2c2013-11-29 12:25:10 +01003406
3407 if (err < 0)
3408 goto done;
3409
3410 bus->tx_hdrlen = SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN;
3411 if (sdiodev->sg_support) {
3412 bus->txglom = false;
3413 value = 1;
3414 pad_size = bus->sdiodev->func[2]->cur_blksize << 1;
Franky Lin8da9d2c2013-11-29 12:25:10 +01003415 err = brcmf_iovar_data_set(bus->sdiodev->dev, "bus:rxglom",
3416 &value, sizeof(u32));
3417 if (err < 0) {
3418 /* bus:rxglom is allowed to fail */
3419 err = 0;
3420 } else {
3421 bus->txglom = true;
3422 bus->tx_hdrlen += SDPCM_HWEXT_LEN;
3423 }
3424 }
3425 brcmf_bus_add_txhdrlen(bus->sdiodev->dev, bus->tx_hdrlen);
3426
3427done:
Arend van Sprielcf458282013-11-29 11:48:15 +01003428 return err;
3429}
3430
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003431void brcmf_sdio_isr(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003432{
Arend van Spriel5b435de2011-10-05 13:19:03 +02003433 brcmf_dbg(TRACE, "Enter\n");
3434
3435 if (!bus) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003436 brcmf_err("bus is null pointer, exiting\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003437 return;
3438 }
3439
Arend van Sprielbb350712014-01-13 22:20:29 +01003440 if (!brcmf_bus_ready(bus->sdiodev->bus_if)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003441 brcmf_err("bus is down. we have nothing to do\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003442 return;
3443 }
3444 /* Count the interrupt call */
Arend van Spriel80969832012-06-09 22:51:44 +02003445 bus->sdcnt.intrcount++;
Franky Lin45316032012-09-13 21:12:03 +02003446 if (in_interrupt())
3447 atomic_set(&bus->ipend, 1);
3448 else
3449 if (brcmf_sdio_intr_rstatus(bus)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003450 brcmf_err("failed backplane access\n");
Franky Lin45316032012-09-13 21:12:03 +02003451 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02003452
Arend van Spriel5b435de2011-10-05 13:19:03 +02003453 /* Disable additional interrupts (is this needed now)? */
3454 if (!bus->intr)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003455 brcmf_err("isr w/o interrupt configured!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003456
Arend van Sprielfccfe932013-06-18 13:29:26 +02003457 atomic_inc(&bus->dpc_tskcnt);
Franky Linf1e68c22012-09-13 21:12:00 +02003458 queue_work(bus->brcmf_wq, &bus->datawork);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003459}
3460
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003461static bool brcmf_sdio_bus_watchdog(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003462{
Joe Perches8ae74652012-01-15 00:38:38 -08003463#ifdef DEBUG
Franky Lincad2b262011-11-22 17:21:53 -08003464 struct brcmf_bus *bus_if = dev_get_drvdata(bus->sdiodev->dev);
Joe Perches8ae74652012-01-15 00:38:38 -08003465#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02003466
3467 brcmf_dbg(TIMER, "Enter\n");
3468
Arend van Spriel5b435de2011-10-05 13:19:03 +02003469 /* Poll period: check device if appropriate. */
Piotr Haber4a3da992013-04-11 13:28:46 +02003470 if (!bus->sr_enabled &&
3471 bus->poll && (++bus->polltick >= bus->pollrate)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003472 u32 intstatus = 0;
3473
3474 /* Reset poll tick */
3475 bus->polltick = 0;
3476
3477 /* Check device if no interrupts */
Arend van Spriel80969832012-06-09 22:51:44 +02003478 if (!bus->intr ||
3479 (bus->sdcnt.intrcount == bus->sdcnt.lastintrs)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003480
Arend van Sprielfccfe932013-06-18 13:29:26 +02003481 if (atomic_read(&bus->dpc_tskcnt) == 0) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003482 u8 devpend;
Arend van Sprielfccfe932013-06-18 13:29:26 +02003483
Franky Lin38b0b0d2012-11-05 16:22:24 -08003484 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriela39be272013-12-12 11:58:58 +01003485 devpend = brcmf_sdiod_regrb(bus->sdiodev,
3486 SDIO_CCCR_INTx,
3487 NULL);
Franky Lin38b0b0d2012-11-05 16:22:24 -08003488 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003489 intstatus =
3490 devpend & (INTR_STATUS_FUNC1 |
3491 INTR_STATUS_FUNC2);
3492 }
3493
3494 /* If there is something, make like the ISR and
3495 schedule the DPC */
3496 if (intstatus) {
Arend van Spriel80969832012-06-09 22:51:44 +02003497 bus->sdcnt.pollcnt++;
Franky Lin1d382272012-09-13 21:11:59 +02003498 atomic_set(&bus->ipend, 1);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003499
Arend van Sprielfccfe932013-06-18 13:29:26 +02003500 atomic_inc(&bus->dpc_tskcnt);
Franky Linf1e68c22012-09-13 21:12:00 +02003501 queue_work(bus->brcmf_wq, &bus->datawork);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003502 }
3503 }
3504
3505 /* Update interrupt tracking */
Arend van Spriel80969832012-06-09 22:51:44 +02003506 bus->sdcnt.lastintrs = bus->sdcnt.intrcount;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003507 }
Joe Perches8ae74652012-01-15 00:38:38 -08003508#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02003509 /* Poll for console output periodically */
Hante Meuleman2def5c12012-11-14 18:46:19 -08003510 if (bus_if && bus_if->state == BRCMF_BUS_DATA &&
Franky Lin8d169aa2011-11-22 17:21:52 -08003511 bus->console_interval != 0) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003512 bus->console.count += BRCMF_WD_POLL_MS;
3513 if (bus->console.count >= bus->console_interval) {
3514 bus->console.count -= bus->console_interval;
Franky Lin38b0b0d2012-11-05 16:22:24 -08003515 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003516 /* Make sure backplane clock is on */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003517 brcmf_sdio_bus_sleep(bus, false, false);
3518 if (brcmf_sdio_readconsole(bus) < 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003519 /* stop on error */
3520 bus->console_interval = 0;
Franky Lin38b0b0d2012-11-05 16:22:24 -08003521 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003522 }
3523 }
Joe Perches8ae74652012-01-15 00:38:38 -08003524#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02003525
3526 /* On idle timeout clear activity flag and/or turn off clock */
3527 if ((bus->idletime > 0) && (bus->clkstate == CLK_AVAIL)) {
3528 if (++bus->idlecount >= bus->idletime) {
3529 bus->idlecount = 0;
3530 if (bus->activity) {
3531 bus->activity = false;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003532 brcmf_sdio_wd_timer(bus, BRCMF_WD_POLL_MS);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003533 } else {
Piotr Haber4a3da992013-04-11 13:28:46 +02003534 brcmf_dbg(SDIO, "idle\n");
Franky Lin38b0b0d2012-11-05 16:22:24 -08003535 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003536 brcmf_sdio_bus_sleep(bus, true, false);
Franky Lin38b0b0d2012-11-05 16:22:24 -08003537 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003538 }
3539 }
3540 }
3541
Franky Lin1d382272012-09-13 21:11:59 +02003542 return (atomic_read(&bus->ipend) > 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003543}
3544
Franky Linf1e68c22012-09-13 21:12:00 +02003545static void brcmf_sdio_dataworker(struct work_struct *work)
3546{
3547 struct brcmf_sdio *bus = container_of(work, struct brcmf_sdio,
3548 datawork);
Franky Linf1e68c22012-09-13 21:12:00 +02003549
Arend van Sprielfccfe932013-06-18 13:29:26 +02003550 while (atomic_read(&bus->dpc_tskcnt)) {
Hante Meuleman71abdc02014-02-25 20:30:31 +01003551 atomic_set(&bus->dpc_tskcnt, 0);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003552 brcmf_sdio_dpc(bus);
Franky Linf1e68c22012-09-13 21:12:00 +02003553 }
Franky Linf1e68c22012-09-13 21:12:00 +02003554}
3555
Arend van Spriel65d80d02014-01-29 15:32:14 +01003556static void
3557brcmf_sdio_drivestrengthinit(struct brcmf_sdio_dev *sdiodev,
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003558 struct brcmf_chip *ci, u32 drivestrength)
Arend van Spriel65d80d02014-01-29 15:32:14 +01003559{
3560 const struct sdiod_drive_str *str_tab = NULL;
3561 u32 str_mask;
3562 u32 str_shift;
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003563 u32 base;
Arend van Spriel65d80d02014-01-29 15:32:14 +01003564 u32 i;
3565 u32 drivestrength_sel = 0;
3566 u32 cc_data_temp;
3567 u32 addr;
3568
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003569 if (!(ci->cc_caps & CC_CAP_PMU))
Arend van Spriel65d80d02014-01-29 15:32:14 +01003570 return;
3571
3572 switch (SDIOD_DRVSTR_KEY(ci->chip, ci->pmurev)) {
Hante Meuleman5779ae62014-07-12 08:49:34 +02003573 case SDIOD_DRVSTR_KEY(BRCM_CC_4330_CHIP_ID, 12):
Arend van Spriel65d80d02014-01-29 15:32:14 +01003574 str_tab = sdiod_drvstr_tab1_1v8;
3575 str_mask = 0x00003800;
3576 str_shift = 11;
3577 break;
Hante Meuleman5779ae62014-07-12 08:49:34 +02003578 case SDIOD_DRVSTR_KEY(BRCM_CC_4334_CHIP_ID, 17):
Arend van Spriel65d80d02014-01-29 15:32:14 +01003579 str_tab = sdiod_drvstr_tab6_1v8;
3580 str_mask = 0x00001800;
3581 str_shift = 11;
3582 break;
Hante Meuleman5779ae62014-07-12 08:49:34 +02003583 case SDIOD_DRVSTR_KEY(BRCM_CC_43143_CHIP_ID, 17):
Arend van Spriel65d80d02014-01-29 15:32:14 +01003584 /* note: 43143 does not support tristate */
3585 i = ARRAY_SIZE(sdiod_drvstr_tab2_3v3) - 1;
3586 if (drivestrength >= sdiod_drvstr_tab2_3v3[i].strength) {
3587 str_tab = sdiod_drvstr_tab2_3v3;
3588 str_mask = 0x00000007;
3589 str_shift = 0;
3590 } else
3591 brcmf_err("Invalid SDIO Drive strength for chip %s, strength=%d\n",
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003592 ci->name, drivestrength);
Arend van Spriel65d80d02014-01-29 15:32:14 +01003593 break;
Hante Meuleman5779ae62014-07-12 08:49:34 +02003594 case SDIOD_DRVSTR_KEY(BRCM_CC_43362_CHIP_ID, 13):
Arend van Spriel65d80d02014-01-29 15:32:14 +01003595 str_tab = sdiod_drive_strength_tab5_1v8;
3596 str_mask = 0x00003800;
3597 str_shift = 11;
3598 break;
3599 default:
3600 brcmf_err("No SDIO Drive strength init done for chip %s rev %d pmurev %d\n",
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003601 ci->name, ci->chiprev, ci->pmurev);
Arend van Spriel65d80d02014-01-29 15:32:14 +01003602 break;
3603 }
3604
3605 if (str_tab != NULL) {
3606 for (i = 0; str_tab[i].strength != 0; i++) {
3607 if (drivestrength >= str_tab[i].strength) {
3608 drivestrength_sel = str_tab[i].sel;
3609 break;
3610 }
3611 }
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003612 base = brcmf_chip_get_chipcommon(ci)->base;
Arend van Spriel65d80d02014-01-29 15:32:14 +01003613 addr = CORE_CC_REG(base, chipcontrol_addr);
3614 brcmf_sdiod_regwl(sdiodev, addr, 1, NULL);
3615 cc_data_temp = brcmf_sdiod_regrl(sdiodev, addr, NULL);
3616 cc_data_temp &= ~str_mask;
3617 drivestrength_sel <<= str_shift;
3618 cc_data_temp |= drivestrength_sel;
3619 brcmf_sdiod_regwl(sdiodev, addr, cc_data_temp, NULL);
3620
3621 brcmf_dbg(INFO, "SDIO: %d mA (req=%d mA) drive strength selected, set to 0x%08x\n",
3622 str_tab[i].strength, drivestrength, cc_data_temp);
3623 }
3624}
3625
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003626static int brcmf_sdio_buscoreprep(void *ctx)
Arend van Spriel65d80d02014-01-29 15:32:14 +01003627{
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003628 struct brcmf_sdio_dev *sdiodev = ctx;
Arend van Spriel65d80d02014-01-29 15:32:14 +01003629 int err = 0;
3630 u8 clkval, clkset;
3631
3632 /* Try forcing SDIO core to do ALPAvail request only */
3633 clkset = SBSDIO_FORCE_HW_CLKREQ_OFF | SBSDIO_ALP_AVAIL_REQ;
3634 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, clkset, &err);
3635 if (err) {
3636 brcmf_err("error writing for HT off\n");
3637 return err;
3638 }
3639
3640 /* If register supported, wait for ALPAvail and then force ALP */
3641 /* This may take up to 15 milliseconds */
3642 clkval = brcmf_sdiod_regrb(sdiodev,
3643 SBSDIO_FUNC1_CHIPCLKCSR, NULL);
3644
3645 if ((clkval & ~SBSDIO_AVBITS) != clkset) {
3646 brcmf_err("ChipClkCSR access: wrote 0x%02x read 0x%02x\n",
3647 clkset, clkval);
3648 return -EACCES;
3649 }
3650
3651 SPINWAIT(((clkval = brcmf_sdiod_regrb(sdiodev,
3652 SBSDIO_FUNC1_CHIPCLKCSR, NULL)),
3653 !SBSDIO_ALPAV(clkval)),
3654 PMU_MAX_TRANSITION_DLY);
3655 if (!SBSDIO_ALPAV(clkval)) {
3656 brcmf_err("timeout on ALPAV wait, clkval 0x%02x\n",
3657 clkval);
3658 return -EBUSY;
3659 }
3660
3661 clkset = SBSDIO_FORCE_HW_CLKREQ_OFF | SBSDIO_FORCE_ALP;
3662 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, clkset, &err);
3663 udelay(65);
3664
3665 /* Also, disable the extra SDIO pull-ups */
3666 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_SDIOPULLUP, 0, NULL);
3667
3668 return 0;
3669}
3670
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003671static void brcmf_sdio_buscore_exitdl(void *ctx, struct brcmf_chip *chip,
3672 u32 rstvec)
3673{
3674 struct brcmf_sdio_dev *sdiodev = ctx;
3675 struct brcmf_core *core;
3676 u32 reg_addr;
3677
3678 /* clear all interrupts */
3679 core = brcmf_chip_get_core(chip, BCMA_CORE_SDIO_DEV);
3680 reg_addr = core->base + offsetof(struct sdpcmd_regs, intstatus);
3681 brcmf_sdiod_regwl(sdiodev, reg_addr, 0xFFFFFFFF, NULL);
3682
3683 if (rstvec)
3684 /* Write reset vector to address 0 */
3685 brcmf_sdiod_ramrw(sdiodev, true, 0, (void *)&rstvec,
3686 sizeof(rstvec));
3687}
3688
3689static u32 brcmf_sdio_buscore_read32(void *ctx, u32 addr)
3690{
3691 struct brcmf_sdio_dev *sdiodev = ctx;
3692 u32 val, rev;
3693
3694 val = brcmf_sdiod_regrl(sdiodev, addr, NULL);
Hante Meuleman5779ae62014-07-12 08:49:34 +02003695 if (sdiodev->func[0]->device == BRCM_SDIO_4335_4339_DEVICE_ID &&
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003696 addr == CORE_CC_REG(SI_ENUM_BASE, chipid)) {
3697 rev = (val & CID_REV_MASK) >> CID_REV_SHIFT;
3698 if (rev >= 2) {
3699 val &= ~CID_ID_MASK;
Hante Meuleman5779ae62014-07-12 08:49:34 +02003700 val |= BRCM_CC_4339_CHIP_ID;
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003701 }
3702 }
3703 return val;
3704}
3705
3706static void brcmf_sdio_buscore_write32(void *ctx, u32 addr, u32 val)
3707{
3708 struct brcmf_sdio_dev *sdiodev = ctx;
3709
3710 brcmf_sdiod_regwl(sdiodev, addr, val, NULL);
3711}
3712
3713static const struct brcmf_buscore_ops brcmf_sdio_buscore_ops = {
3714 .prepare = brcmf_sdio_buscoreprep,
3715 .exit_dl = brcmf_sdio_buscore_exitdl,
3716 .read32 = brcmf_sdio_buscore_read32,
3717 .write32 = brcmf_sdio_buscore_write32,
3718};
3719
Arend van Spriel5b435de2011-10-05 13:19:03 +02003720static bool
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003721brcmf_sdio_probe_attach(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003722{
3723 u8 clkctl = 0;
3724 int err = 0;
3725 int reg_addr;
3726 u32 reg_val;
Hante Meuleman668761a2013-04-12 10:55:55 +02003727 u32 drivestrength;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003728
Franky Lin38b0b0d2012-11-05 16:22:24 -08003729 sdio_claim_host(bus->sdiodev->func[1]);
3730
Joe Perches18aad4f2012-01-15 00:38:42 -08003731 pr_debug("F1 signature read @0x18000000=0x%4x\n",
Arend van Spriela39be272013-12-12 11:58:58 +01003732 brcmf_sdiod_regrl(bus->sdiodev, SI_ENUM_BASE, NULL));
Arend van Spriel5b435de2011-10-05 13:19:03 +02003733
3734 /*
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003735 * Force PLL off until brcmf_chip_attach()
Arend van Spriel5b435de2011-10-05 13:19:03 +02003736 * programs PLL control regs
3737 */
3738
Arend van Spriela39be272013-12-12 11:58:58 +01003739 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3740 BRCMF_INIT_CLKCTL1, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003741 if (!err)
Arend van Spriela39be272013-12-12 11:58:58 +01003742 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
3743 SBSDIO_FUNC1_CHIPCLKCSR, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003744
3745 if (err || ((clkctl & ~SBSDIO_AVBITS) != BRCMF_INIT_CLKCTL1)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003746 brcmf_err("ChipClkCSR access: err %d wrote 0x%02x read 0x%02x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02003747 err, BRCMF_INIT_CLKCTL1, clkctl);
3748 goto fail;
3749 }
3750
Arend van Sprielbb350712014-01-13 22:20:29 +01003751 /* SDIO register access works so moving
3752 * state from UNKNOWN to DOWN.
3753 */
3754 brcmf_bus_change_state(bus->sdiodev->bus_if, BRCMF_BUS_DOWN);
3755
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003756 bus->ci = brcmf_chip_attach(bus->sdiodev, &brcmf_sdio_buscore_ops);
3757 if (IS_ERR(bus->ci)) {
3758 brcmf_err("brcmf_chip_attach failed!\n");
3759 bus->ci = NULL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003760 goto fail;
3761 }
3762
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003763 if (brcmf_sdio_kso_init(bus)) {
Piotr Haber4a3da992013-04-11 13:28:46 +02003764 brcmf_err("error enabling KSO\n");
3765 goto fail;
3766 }
3767
Hante Meuleman668761a2013-04-12 10:55:55 +02003768 if ((bus->sdiodev->pdata) && (bus->sdiodev->pdata->drive_strength))
3769 drivestrength = bus->sdiodev->pdata->drive_strength;
3770 else
3771 drivestrength = DEFAULT_SDIO_DRIVE_STRENGTH;
Arend van Spriel65d80d02014-01-29 15:32:14 +01003772 brcmf_sdio_drivestrengthinit(bus->sdiodev, bus->ci, drivestrength);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003773
Franky Lin454d2a82011-11-04 22:23:37 +01003774 /* Get info on the SOCRAM cores... */
Arend van Spriel5b435de2011-10-05 13:19:03 +02003775 bus->ramsize = bus->ci->ramsize;
3776 if (!(bus->ramsize)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003777 brcmf_err("failed to find SOCRAM memory!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003778 goto fail;
3779 }
3780
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003781 /* Set card control so an SDIO card reset does a WLAN backplane reset */
Arend van Spriela39be272013-12-12 11:58:58 +01003782 reg_val = brcmf_sdiod_regrb(bus->sdiodev,
3783 SDIO_CCCR_BRCM_CARDCTRL, &err);
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003784 if (err)
3785 goto fail;
3786
3787 reg_val |= SDIO_CCCR_BRCM_CARDCTRL_WLANRESET;
3788
Arend van Spriela39be272013-12-12 11:58:58 +01003789 brcmf_sdiod_regwb(bus->sdiodev,
3790 SDIO_CCCR_BRCM_CARDCTRL, reg_val, &err);
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003791 if (err)
3792 goto fail;
3793
3794 /* set PMUControl so a backplane reset does PMU state reload */
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003795 reg_addr = CORE_CC_REG(brcmf_chip_get_chipcommon(bus->ci)->base,
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003796 pmucontrol);
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003797 reg_val = brcmf_sdiod_regrl(bus->sdiodev, reg_addr, &err);
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003798 if (err)
3799 goto fail;
3800
3801 reg_val |= (BCMA_CC_PMU_CTL_RES_RELOAD << BCMA_CC_PMU_CTL_RES_SHIFT);
3802
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003803 brcmf_sdiod_regwl(bus->sdiodev, reg_addr, reg_val, &err);
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003804 if (err)
3805 goto fail;
3806
Franky Lin38b0b0d2012-11-05 16:22:24 -08003807 sdio_release_host(bus->sdiodev->func[1]);
3808
Arend van Spriel5b435de2011-10-05 13:19:03 +02003809 brcmu_pktq_init(&bus->txq, (PRIOMASK + 1), TXQLEN);
3810
Arend van Spriel9b2d2f2a82013-11-29 12:25:14 +01003811 /* allocate header buffer */
3812 bus->hdrbuf = kzalloc(MAX_HDR_READ + bus->head_align, GFP_KERNEL);
3813 if (!bus->hdrbuf)
3814 return false;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003815 /* Locate an appropriately-aligned portion of hdrbuf */
3816 bus->rxhdr = (u8 *) roundup((unsigned long)&bus->hdrbuf[0],
Arend van Spriel9b2d2f2a82013-11-29 12:25:14 +01003817 bus->head_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003818
3819 /* Set the poll and/or interrupt flags */
3820 bus->intr = true;
3821 bus->poll = false;
3822 if (bus->poll)
3823 bus->pollrate = 1;
3824
3825 return true;
3826
3827fail:
Franky Lin38b0b0d2012-11-05 16:22:24 -08003828 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003829 return false;
3830}
3831
Arend van Spriel5b435de2011-10-05 13:19:03 +02003832static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003833brcmf_sdio_watchdog_thread(void *data)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003834{
Franky Line92eedf2011-11-22 17:21:50 -08003835 struct brcmf_sdio *bus = (struct brcmf_sdio *)data;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003836
3837 allow_signal(SIGTERM);
3838 /* Run until signal received */
3839 while (1) {
3840 if (kthread_should_stop())
3841 break;
3842 if (!wait_for_completion_interruptible(&bus->watchdog_wait)) {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003843 brcmf_sdio_bus_watchdog(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003844 /* Count the tick for reference */
Arend van Spriel80969832012-06-09 22:51:44 +02003845 bus->sdcnt.tickcnt++;
Arend van Spriel58e9df42014-03-20 10:18:01 +01003846 reinit_completion(&bus->watchdog_wait);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003847 } else
3848 break;
3849 }
3850 return 0;
3851}
3852
3853static void
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003854brcmf_sdio_watchdog(unsigned long data)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003855{
Franky Line92eedf2011-11-22 17:21:50 -08003856 struct brcmf_sdio *bus = (struct brcmf_sdio *)data;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003857
3858 if (bus->watchdog_tsk) {
3859 complete(&bus->watchdog_wait);
3860 /* Reschedule the watchdog */
3861 if (bus->wd_timer_valid)
3862 mod_timer(&bus->timer,
3863 jiffies + BRCMF_WD_POLL_MS * HZ / 1000);
3864 }
3865}
3866
Arend van Sprield9cb2592012-12-05 15:25:54 +01003867static struct brcmf_bus_ops brcmf_sdio_bus_ops = {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003868 .stop = brcmf_sdio_bus_stop,
3869 .preinit = brcmf_sdio_bus_preinit,
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003870 .txdata = brcmf_sdio_bus_txdata,
3871 .txctl = brcmf_sdio_bus_txctl,
3872 .rxctl = brcmf_sdio_bus_rxctl,
3873 .gettxq = brcmf_sdio_bus_gettxq,
Arend van Sprield9cb2592012-12-05 15:25:54 +01003874};
3875
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003876static void brcmf_sdio_firmware_callback(struct device *dev,
3877 const struct firmware *code,
3878 void *nvram, u32 nvram_len)
3879{
3880 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3881 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3882 struct brcmf_sdio *bus = sdiodev->bus;
3883 int err = 0;
3884 u8 saveclk;
3885
3886 brcmf_dbg(TRACE, "Enter: dev=%s\n", dev_name(dev));
3887
3888 /* try to download image and nvram to the dongle */
3889 if (bus_if->state == BRCMF_BUS_DOWN) {
3890 bus->alp_only = true;
3891 err = brcmf_sdio_download_firmware(bus, code, nvram, nvram_len);
3892 if (err)
3893 goto fail;
3894 bus->alp_only = false;
3895 }
3896
3897 if (!bus_if->drvr)
3898 return;
3899
3900 /* Start the watchdog timer */
3901 bus->sdcnt.tickcnt = 0;
3902 brcmf_sdio_wd_timer(bus, BRCMF_WD_POLL_MS);
3903
3904 sdio_claim_host(sdiodev->func[1]);
3905
3906 /* Make sure backplane clock is on, needed to generate F2 interrupt */
3907 brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
3908 if (bus->clkstate != CLK_AVAIL)
3909 goto release;
3910
3911 /* Force clocks on backplane to be sure F2 interrupt propagates */
3912 saveclk = brcmf_sdiod_regrb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, &err);
3913 if (!err) {
3914 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3915 (saveclk | SBSDIO_FORCE_HT), &err);
3916 }
3917 if (err) {
3918 brcmf_err("Failed to force clock for F2: err %d\n", err);
3919 goto release;
3920 }
3921
3922 /* Enable function 2 (frame transfers) */
3923 w_sdreg32(bus, SDPCM_PROT_VERSION << SMB_DATA_VERSION_SHIFT,
3924 offsetof(struct sdpcmd_regs, tosbmailboxdata));
3925 err = sdio_enable_func(sdiodev->func[SDIO_FUNC_2]);
3926
3927
3928 brcmf_dbg(INFO, "enable F2: err=%d\n", err);
3929
3930 /* If F2 successfully enabled, set core and enable interrupts */
3931 if (!err) {
3932 /* Set up the interrupt mask and enable interrupts */
3933 bus->hostintmask = HOSTINTMASK;
3934 w_sdreg32(bus, bus->hostintmask,
3935 offsetof(struct sdpcmd_regs, hostintmask));
3936
3937 brcmf_sdiod_regwb(sdiodev, SBSDIO_WATERMARK, 8, &err);
3938 } else {
3939 /* Disable F2 again */
3940 sdio_disable_func(sdiodev->func[SDIO_FUNC_2]);
3941 goto release;
3942 }
3943
3944 if (brcmf_chip_sr_capable(bus->ci)) {
3945 brcmf_sdio_sr_init(bus);
3946 } else {
3947 /* Restore previous clock setting */
3948 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3949 saveclk, &err);
3950 }
3951
3952 if (err == 0) {
3953 err = brcmf_sdiod_intr_register(sdiodev);
3954 if (err != 0)
3955 brcmf_err("intr register failed:%d\n", err);
3956 }
3957
3958 /* If we didn't come up, turn off backplane clock */
3959 if (err != 0)
3960 brcmf_sdio_clkctl(bus, CLK_NONE, false);
3961
3962 sdio_release_host(sdiodev->func[1]);
3963
3964 err = brcmf_bus_start(dev);
3965 if (err != 0) {
3966 brcmf_err("dongle is not responding\n");
3967 goto fail;
3968 }
3969 return;
3970
3971release:
3972 sdio_release_host(sdiodev->func[1]);
3973fail:
3974 brcmf_dbg(TRACE, "failed: dev=%s, err=%d\n", dev_name(dev), err);
3975 device_release_driver(dev);
3976}
3977
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003978struct brcmf_sdio *brcmf_sdio_probe(struct brcmf_sdio_dev *sdiodev)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003979{
3980 int ret;
Franky Line92eedf2011-11-22 17:21:50 -08003981 struct brcmf_sdio *bus;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003982
Arend van Spriel5b435de2011-10-05 13:19:03 +02003983 brcmf_dbg(TRACE, "Enter\n");
3984
Arend van Spriel5b435de2011-10-05 13:19:03 +02003985 /* Allocate private bus interface state */
Franky Line92eedf2011-11-22 17:21:50 -08003986 bus = kzalloc(sizeof(struct brcmf_sdio), GFP_ATOMIC);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003987 if (!bus)
3988 goto fail;
3989
3990 bus->sdiodev = sdiodev;
3991 sdiodev->bus = bus;
Arend van Sprielb83db862011-10-19 12:51:09 +02003992 skb_queue_head_init(&bus->glom);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003993 bus->txbound = BRCMF_TXBOUND;
3994 bus->rxbound = BRCMF_RXBOUND;
3995 bus->txminmax = BRCMF_TXMINMAX;
Franky Lin6bc52312013-08-10 12:27:28 +02003996 bus->tx_seq = SDPCM_SEQ_WRAP - 1;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003997
Arend van Spriele217d1c2013-11-29 12:25:13 +01003998 /* platform specific configuration:
3999 * alignments must be at least 4 bytes for ADMA
4000 */
4001 bus->head_align = ALIGNMENT;
4002 bus->sgentry_align = ALIGNMENT;
4003 if (sdiodev->pdata) {
4004 if (sdiodev->pdata->sd_head_align > ALIGNMENT)
4005 bus->head_align = sdiodev->pdata->sd_head_align;
4006 if (sdiodev->pdata->sd_sgentry_align > ALIGNMENT)
4007 bus->sgentry_align = sdiodev->pdata->sd_sgentry_align;
4008 }
4009
Hante Meuleman37ac5782012-11-14 18:46:18 -08004010 INIT_WORK(&bus->datawork, brcmf_sdio_dataworker);
4011 bus->brcmf_wq = create_singlethread_workqueue("brcmf_wq");
4012 if (bus->brcmf_wq == NULL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01004013 brcmf_err("insufficient memory to create txworkqueue\n");
Hante Meuleman37ac5782012-11-14 18:46:18 -08004014 goto fail;
4015 }
4016
Arend van Spriel5b435de2011-10-05 13:19:03 +02004017 /* attempt to attach to the dongle */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01004018 if (!(brcmf_sdio_probe_attach(bus))) {
4019 brcmf_err("brcmf_sdio_probe_attach failed\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02004020 goto fail;
4021 }
4022
Franky Lindd43a012012-11-05 16:22:22 -08004023 spin_lock_init(&bus->rxctl_lock);
Hante Meulemanfed7ec42014-03-15 17:18:18 +01004024 spin_lock_init(&bus->txq_lock);
4025 sema_init(&bus->tx_seq_lock, 1);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004026 init_waitqueue_head(&bus->ctrl_wait);
4027 init_waitqueue_head(&bus->dcmd_resp_wait);
4028
4029 /* Set up the watchdog timer */
4030 init_timer(&bus->timer);
4031 bus->timer.data = (unsigned long)bus;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01004032 bus->timer.function = brcmf_sdio_watchdog;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004033
Arend van Spriel5b435de2011-10-05 13:19:03 +02004034 /* Initialize watchdog thread */
4035 init_completion(&bus->watchdog_wait);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01004036 bus->watchdog_tsk = kthread_run(brcmf_sdio_watchdog_thread,
Arend van Spriel5b435de2011-10-05 13:19:03 +02004037 bus, "brcmf_watchdog");
4038 if (IS_ERR(bus->watchdog_tsk)) {
Joe Perches02f77192012-01-15 00:38:44 -08004039 pr_warn("brcmf_watchdog thread failed to start\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02004040 bus->watchdog_tsk = NULL;
4041 }
4042 /* Initialize DPC thread */
Arend van Sprielfccfe932013-06-18 13:29:26 +02004043 atomic_set(&bus->dpc_tskcnt, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004044
Franky Lina9ffda82011-12-16 18:37:12 -08004045 /* Assign bus interface call back */
Arend van Sprield9cb2592012-12-05 15:25:54 +01004046 bus->sdiodev->bus_if->dev = bus->sdiodev->dev;
4047 bus->sdiodev->bus_if->ops = &brcmf_sdio_bus_ops;
Arend van Spriel75d907d2013-02-06 18:40:45 +01004048 bus->sdiodev->bus_if->chip = bus->ci->chip;
4049 bus->sdiodev->bus_if->chiprev = bus->ci->chiprev;
Arend van Sprield9cb2592012-12-05 15:25:54 +01004050
Franky Lin706478c2013-08-10 12:27:29 +02004051 /* default sdio bus header length for tx packet */
4052 bus->tx_hdrlen = SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN;
4053
4054 /* Attach to the common layer, reserve hdr space */
Arend van Spriel8dee77b2013-11-29 11:48:14 +01004055 ret = brcmf_attach(bus->sdiodev->dev);
Franky Lin712ac5b2011-12-16 18:37:09 -08004056 if (ret != 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01004057 brcmf_err("brcmf_attach failed\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02004058 goto fail;
4059 }
4060
Daniel Kim7dd3abc2014-05-27 12:56:26 +02004061 /* Query the F2 block size, set roundup accordingly */
4062 bus->blocksize = bus->sdiodev->func[2]->cur_blksize;
4063 bus->roundup = min(max_roundup, bus->blocksize);
4064
Arend van Spriel5b435de2011-10-05 13:19:03 +02004065 /* Allocate buffers */
Arend van Sprielfad13222014-01-06 12:40:40 +01004066 if (bus->sdiodev->bus_if->maxctl) {
Daniel Kim7dd3abc2014-05-27 12:56:26 +02004067 bus->sdiodev->bus_if->maxctl += bus->roundup;
Arend van Sprielfad13222014-01-06 12:40:40 +01004068 bus->rxblen =
4069 roundup((bus->sdiodev->bus_if->maxctl + SDPCM_HDRLEN),
4070 ALIGNMENT) + bus->head_align;
4071 bus->rxbuf = kmalloc(bus->rxblen, GFP_ATOMIC);
4072 if (!(bus->rxbuf)) {
4073 brcmf_err("rxbuf allocation failed\n");
4074 goto fail;
4075 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02004076 }
4077
Arend van Sprielfad13222014-01-06 12:40:40 +01004078 sdio_claim_host(bus->sdiodev->func[1]);
4079
4080 /* Disable F2 to clear any intermediate frame state on the dongle */
4081 sdio_disable_func(bus->sdiodev->func[SDIO_FUNC_2]);
4082
Arend van Sprielfad13222014-01-06 12:40:40 +01004083 bus->rxflow = false;
4084
4085 /* Done with backplane-dependent accesses, can drop clock... */
4086 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, 0, NULL);
4087
4088 sdio_release_host(bus->sdiodev->func[1]);
4089
4090 /* ...and initialize clock/power states */
4091 bus->clkstate = CLK_SDONLY;
4092 bus->idletime = BRCMF_IDLE_INTERVAL;
4093 bus->idleclock = BRCMF_IDLE_ACTIVE;
4094
Arend van Sprielfad13222014-01-06 12:40:40 +01004095 /* SR state */
4096 bus->sleeping = false;
4097 bus->sr_enabled = false;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004098
Arend van Spriel80969832012-06-09 22:51:44 +02004099 brcmf_sdio_debugfs_create(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004100 brcmf_dbg(INFO, "completed!!\n");
4101
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02004102 ret = brcmf_fw_get_firmwares(sdiodev->dev, BRCMF_FW_REQUEST_NVRAM,
4103 brcmf_sdio_get_fwname(bus->ci,
4104 BRCMF_FIRMWARE_BIN),
4105 brcmf_sdio_get_fwname(bus->ci,
4106 BRCMF_FIRMWARE_NVRAM),
4107 brcmf_sdio_firmware_callback);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004108 if (ret != 0) {
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02004109 brcmf_err("async firmware request failed: %d\n", ret);
Hante Meuleman1799ddf2012-11-14 18:46:17 -08004110 goto fail;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004111 }
Franky Lin15d45b62011-10-21 16:16:32 +02004112
Arend van Spriel5b435de2011-10-05 13:19:03 +02004113 return bus;
4114
4115fail:
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004116 brcmf_sdio_remove(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004117 return NULL;
4118}
4119
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004120/* Detach and free everything */
4121void brcmf_sdio_remove(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004122{
Arend van Spriel5b435de2011-10-05 13:19:03 +02004123 brcmf_dbg(TRACE, "Enter\n");
4124
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004125 if (bus) {
4126 /* De-register interrupt handler */
4127 brcmf_sdiod_intr_unregister(bus->sdiodev);
4128
Arend van Spriel4faf28b2014-05-27 12:56:18 +02004129 brcmf_detach(bus->sdiodev->dev);
Arend van Sprielbfad4a02014-01-06 12:40:39 +01004130
Hante Meulemane0c180ec2014-01-29 15:32:18 +01004131 cancel_work_sync(&bus->datawork);
4132 if (bus->brcmf_wq)
4133 destroy_workqueue(bus->brcmf_wq);
4134
Arend van Sprielbfad4a02014-01-06 12:40:39 +01004135 if (bus->ci) {
Arend van Sprielbb350712014-01-13 22:20:29 +01004136 if (bus->sdiodev->bus_if->state == BRCMF_BUS_DOWN) {
4137 sdio_claim_host(bus->sdiodev->func[1]);
4138 brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
4139 /* Leave the device in state where it is
4140 * 'quiet'. This is done by putting it in
4141 * download_state which essentially resets
4142 * all necessary cores.
4143 */
4144 msleep(20);
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01004145 brcmf_chip_enter_download(bus->ci);
Arend van Sprielbb350712014-01-13 22:20:29 +01004146 brcmf_sdio_clkctl(bus, CLK_NONE, false);
4147 sdio_release_host(bus->sdiodev->func[1]);
4148 }
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01004149 brcmf_chip_detach(bus->ci);
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004150 }
4151
Arend van Sprielbfad4a02014-01-06 12:40:39 +01004152 kfree(bus->rxbuf);
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004153 kfree(bus->hdrbuf);
4154 kfree(bus);
4155 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02004156
4157 brcmf_dbg(TRACE, "Disconnected\n");
4158}
4159
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01004160void brcmf_sdio_wd_timer(struct brcmf_sdio *bus, uint wdtick)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004161{
Arend van Spriel5b435de2011-10-05 13:19:03 +02004162 /* Totally stop the timer */
Joe Perches23677ce2012-02-09 11:17:23 +00004163 if (!wdtick && bus->wd_timer_valid) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02004164 del_timer_sync(&bus->timer);
4165 bus->wd_timer_valid = false;
4166 bus->save_ms = wdtick;
4167 return;
4168 }
4169
Franky Linece960e2011-10-21 16:16:19 +02004170 /* don't start the wd until fw is loaded */
Arend van Sprield6ae2c52014-01-06 12:40:44 +01004171 if (bus->sdiodev->bus_if->state != BRCMF_BUS_DATA)
Franky Linece960e2011-10-21 16:16:19 +02004172 return;
4173
Arend van Spriel5b435de2011-10-05 13:19:03 +02004174 if (wdtick) {
4175 if (bus->save_ms != BRCMF_WD_POLL_MS) {
Joe Perches23677ce2012-02-09 11:17:23 +00004176 if (bus->wd_timer_valid)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004177 /* Stop timer and restart at new value */
4178 del_timer_sync(&bus->timer);
4179
4180 /* Create timer again when watchdog period is
4181 dynamically changed or in the first instance
4182 */
4183 bus->timer.expires =
4184 jiffies + BRCMF_WD_POLL_MS * HZ / 1000;
4185 add_timer(&bus->timer);
4186
4187 } else {
4188 /* Re arm the timer, at last watchdog period */
4189 mod_timer(&bus->timer,
4190 jiffies + BRCMF_WD_POLL_MS * HZ / 1000);
4191 }
4192
4193 bus->wd_timer_valid = true;
4194 bus->save_ms = wdtick;
4195 }
4196}