blob: 7f574f26cdef657a3c243acb7f1f5e301da9cce3 [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>
Hauke Mehrtensa32be012015-09-19 12:47:20 +020018#include <linux/atomic.h>
Arend van Spriel5b435de2011-10-05 13:19:03 +020019#include <linux/kernel.h>
20#include <linux/kthread.h>
21#include <linux/printk.h>
22#include <linux/pci_ids.h>
23#include <linux/netdevice.h>
24#include <linux/interrupt.h>
25#include <linux/sched.h>
26#include <linux/mmc/sdio.h>
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +010027#include <linux/mmc/sdio_ids.h>
Arend van Spriel5b435de2011-10-05 13:19:03 +020028#include <linux/mmc/sdio_func.h>
29#include <linux/mmc/card.h>
30#include <linux/semaphore.h>
31#include <linux/firmware.h>
Stephen Rothwellb7a57e72011-10-12 21:35:07 +020032#include <linux/module.h>
Franky Lin99ba15c2011-11-04 22:23:42 +010033#include <linux/bcma/bcma.h>
Arend van Spriel4fc0d012012-06-14 12:16:57 +020034#include <linux/debugfs.h>
Arend van Spriel8dc01812012-06-21 11:33:32 +020035#include <linux/vmalloc.h>
Hante Meuleman668761a2013-04-12 10:55:55 +020036#include <linux/platform_data/brcmfmac-sdio.h>
Franky Lin8da9d2c2013-11-29 12:25:10 +010037#include <linux/moduleparam.h>
Arend van Spriel5b435de2011-10-05 13:19:03 +020038#include <asm/unaligned.h>
39#include <defs.h>
40#include <brcmu_wifi.h>
41#include <brcmu_utils.h>
42#include <brcm_hw_ids.h>
43#include <soc.h>
Hante Meuleman888bf76e2014-10-28 14:56:17 +010044#include "sdio.h"
Arend van Spriel20c9c9b2014-01-29 15:32:15 +010045#include "chip.h"
Arend van Sprieldabedab2014-05-27 12:56:17 +020046#include "firmware.h"
Arend van Spriel5b435de2011-10-05 13:19:03 +020047
Hante Meuleman4dd8b262015-01-25 20:31:32 +010048#define DCMD_RESP_TIMEOUT 2000 /* In milli second */
49#define CTL_DONE_TIMEOUT 2000 /* In milli second */
Arend van Spriel5b435de2011-10-05 13:19:03 +020050
Joe Perches8ae74652012-01-15 00:38:38 -080051#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +020052
53#define BRCMF_TRAP_INFO_SIZE 80
54
55#define CBUF_LEN (128)
56
Arend van Spriel4fc0d012012-06-14 12:16:57 +020057/* Device console log buffer state */
58#define CONSOLE_BUFFER_MAX 2024
59
Arend van Spriel5b435de2011-10-05 13:19:03 +020060struct rte_log_le {
61 __le32 buf; /* Can't be pointer on (64-bit) hosts */
62 __le32 buf_size;
63 __le32 idx;
64 char *_buf_compat; /* Redundant pointer for backward compat. */
65};
66
67struct rte_console {
68 /* Virtual UART
69 * When there is no UART (e.g. Quickturn),
70 * the host should write a complete
71 * input line directly into cbuf and then write
72 * the length into vcons_in.
73 * This may also be used when there is a real UART
74 * (at risk of conflicting with
75 * the real UART). vcons_out is currently unused.
76 */
77 uint vcons_in;
78 uint vcons_out;
79
80 /* Output (logging) buffer
81 * Console output is written to a ring buffer log_buf at index log_idx.
82 * The host may read the output when it sees log_idx advance.
83 * Output will be lost if the output wraps around faster than the host
84 * polls.
85 */
86 struct rte_log_le log_le;
87
88 /* Console input line buffer
89 * Characters are read one at a time into cbuf
90 * until <CR> is received, then
91 * the buffer is processed as a command line.
92 * Also used for virtual UART.
93 */
94 uint cbuf_idx;
95 char cbuf[CBUF_LEN];
96};
97
Joe Perches8ae74652012-01-15 00:38:38 -080098#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +020099#include <chipcommon.h>
100
Hante Meulemand14f78b2014-10-28 14:56:14 +0100101#include "bus.h"
Hante Meulemana8e8ed32014-10-28 14:56:13 +0100102#include "debug.h"
Arend van Spriel40c1c242013-04-05 10:57:44 +0200103#include "tracepoint.h"
Arend van Spriel5b435de2011-10-05 13:19:03 +0200104
105#define TXQLEN 2048 /* bulk tx queue length */
106#define TXHI (TXQLEN - 256) /* turn on flow control above TXHI */
107#define TXLOW (TXHI - 256) /* turn off flow control below TXLOW */
108#define PRIOMASK 7
109
110#define TXRETRIES 2 /* # of retries for tx frames */
111
112#define BRCMF_RXBOUND 50 /* Default for max rx frames in
113 one scheduling */
114
115#define BRCMF_TXBOUND 20 /* Default for max tx frames in
116 one scheduling */
117
118#define BRCMF_TXMINMAX 1 /* Max tx frames if rx still pending */
119
120#define MEMBLOCK 2048 /* Block size used for downloading
121 of dongle image */
122#define MAX_DATA_BUF (32 * 1024) /* Must be large enough to hold
123 biggest possible glom */
124
125#define BRCMF_FIRSTREAD (1 << 6)
126
Arend van Spriel9d6c1dc2015-08-26 22:15:01 +0200127#define BRCMF_CONSOLE 10 /* watchdog interval to poll console */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200128
129/* SBSDIO_DEVICE_CTL */
130
131/* 1: device will assert busy signal when receiving CMD53 */
132#define SBSDIO_DEVCTL_SETBUSY 0x01
133/* 1: assertion of sdio interrupt is synchronous to the sdio clock */
134#define SBSDIO_DEVCTL_SPI_INTR_SYNC 0x02
135/* 1: mask all interrupts to host except the chipActive (rev 8) */
136#define SBSDIO_DEVCTL_CA_INT_ONLY 0x04
137/* 1: isolate internal sdio signals, put external pads in tri-state; requires
138 * sdio bus power cycle to clear (rev 9) */
139#define SBSDIO_DEVCTL_PADS_ISO 0x08
140/* Force SD->SB reset mapping (rev 11) */
141#define SBSDIO_DEVCTL_SB_RST_CTL 0x30
142/* Determined by CoreControl bit */
143#define SBSDIO_DEVCTL_RST_CORECTL 0x00
144/* Force backplane reset */
145#define SBSDIO_DEVCTL_RST_BPRESET 0x10
146/* Force no backplane reset */
147#define SBSDIO_DEVCTL_RST_NOBPRESET 0x20
148
Arend van Spriel5b435de2011-10-05 13:19:03 +0200149/* direct(mapped) cis space */
150
151/* MAPPED common CIS address */
152#define SBSDIO_CIS_BASE_COMMON 0x1000
153/* maximum bytes in one CIS */
154#define SBSDIO_CIS_SIZE_LIMIT 0x200
155/* cis offset addr is < 17 bits */
156#define SBSDIO_CIS_OFT_ADDR_MASK 0x1FFFF
157
158/* manfid tuple length, include tuple, link bytes */
159#define SBSDIO_CIS_MANFID_TUPLE_LEN 6
160
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100161#define CORE_BUS_REG(base, field) \
162 (base + offsetof(struct sdpcmd_regs, field))
163
164/* SDIO function 1 register CHIPCLKCSR */
165/* Force ALP request to backplane */
166#define SBSDIO_FORCE_ALP 0x01
167/* Force HT request to backplane */
168#define SBSDIO_FORCE_HT 0x02
169/* Force ILP request to backplane */
170#define SBSDIO_FORCE_ILP 0x04
171/* Make ALP ready (power up xtal) */
172#define SBSDIO_ALP_AVAIL_REQ 0x08
173/* Make HT ready (power up PLL) */
174#define SBSDIO_HT_AVAIL_REQ 0x10
175/* Squelch clock requests from HW */
176#define SBSDIO_FORCE_HW_CLKREQ_OFF 0x20
177/* Status: ALP is ready */
178#define SBSDIO_ALP_AVAIL 0x40
179/* Status: HT is ready */
180#define SBSDIO_HT_AVAIL 0x80
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100181#define SBSDIO_CSR_MASK 0x1F
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100182#define SBSDIO_AVBITS (SBSDIO_HT_AVAIL | SBSDIO_ALP_AVAIL)
183#define SBSDIO_ALPAV(regval) ((regval) & SBSDIO_AVBITS)
184#define SBSDIO_HTAV(regval) (((regval) & SBSDIO_AVBITS) == SBSDIO_AVBITS)
185#define SBSDIO_ALPONLY(regval) (SBSDIO_ALPAV(regval) && !SBSDIO_HTAV(regval))
186#define SBSDIO_CLKAV(regval, alponly) \
187 (SBSDIO_ALPAV(regval) && (alponly ? 1 : SBSDIO_HTAV(regval)))
188
Arend van Spriel5b435de2011-10-05 13:19:03 +0200189/* intstatus */
190#define I_SMB_SW0 (1 << 0) /* To SB Mail S/W interrupt 0 */
191#define I_SMB_SW1 (1 << 1) /* To SB Mail S/W interrupt 1 */
192#define I_SMB_SW2 (1 << 2) /* To SB Mail S/W interrupt 2 */
193#define I_SMB_SW3 (1 << 3) /* To SB Mail S/W interrupt 3 */
194#define I_SMB_SW_MASK 0x0000000f /* To SB Mail S/W interrupts mask */
195#define I_SMB_SW_SHIFT 0 /* To SB Mail S/W interrupts shift */
196#define I_HMB_SW0 (1 << 4) /* To Host Mail S/W interrupt 0 */
197#define I_HMB_SW1 (1 << 5) /* To Host Mail S/W interrupt 1 */
198#define I_HMB_SW2 (1 << 6) /* To Host Mail S/W interrupt 2 */
199#define I_HMB_SW3 (1 << 7) /* To Host Mail S/W interrupt 3 */
200#define I_HMB_SW_MASK 0x000000f0 /* To Host Mail S/W interrupts mask */
201#define I_HMB_SW_SHIFT 4 /* To Host Mail S/W interrupts shift */
202#define I_WR_OOSYNC (1 << 8) /* Write Frame Out Of Sync */
203#define I_RD_OOSYNC (1 << 9) /* Read Frame Out Of Sync */
204#define I_PC (1 << 10) /* descriptor error */
205#define I_PD (1 << 11) /* data error */
206#define I_DE (1 << 12) /* Descriptor protocol Error */
207#define I_RU (1 << 13) /* Receive descriptor Underflow */
208#define I_RO (1 << 14) /* Receive fifo Overflow */
209#define I_XU (1 << 15) /* Transmit fifo Underflow */
210#define I_RI (1 << 16) /* Receive Interrupt */
211#define I_BUSPWR (1 << 17) /* SDIO Bus Power Change (rev 9) */
212#define I_XMTDATA_AVAIL (1 << 23) /* bits in fifo */
213#define I_XI (1 << 24) /* Transmit Interrupt */
214#define I_RF_TERM (1 << 25) /* Read Frame Terminate */
215#define I_WF_TERM (1 << 26) /* Write Frame Terminate */
216#define I_PCMCIA_XU (1 << 27) /* PCMCIA Transmit FIFO Underflow */
217#define I_SBINT (1 << 28) /* sbintstatus Interrupt */
218#define I_CHIPACTIVE (1 << 29) /* chip from doze to active state */
219#define I_SRESET (1 << 30) /* CCCR RES interrupt */
220#define I_IOE2 (1U << 31) /* CCCR IOE2 Bit Changed */
221#define I_ERRORS (I_PC | I_PD | I_DE | I_RU | I_RO | I_XU)
222#define I_DMA (I_RI | I_XI | I_ERRORS)
223
224/* corecontrol */
225#define CC_CISRDY (1 << 0) /* CIS Ready */
226#define CC_BPRESEN (1 << 1) /* CCCR RES signal */
227#define CC_F2RDY (1 << 2) /* set CCCR IOR2 bit */
228#define CC_CLRPADSISO (1 << 3) /* clear SDIO pads isolation */
229#define CC_XMTDATAAVAIL_MODE (1 << 4)
230#define CC_XMTDATAAVAIL_CTRL (1 << 5)
231
232/* SDA_FRAMECTRL */
233#define SFC_RF_TERM (1 << 0) /* Read Frame Terminate */
234#define SFC_WF_TERM (1 << 1) /* Write Frame Terminate */
235#define SFC_CRC4WOOS (1 << 2) /* CRC error for write out of sync */
236#define SFC_ABORTALL (1 << 3) /* Abort all in-progress frames */
237
Arend van Spriel5b435de2011-10-05 13:19:03 +0200238/*
239 * Software allocation of To SB Mailbox resources
240 */
241
242/* tosbmailbox bits corresponding to intstatus bits */
243#define SMB_NAK (1 << 0) /* Frame NAK */
244#define SMB_INT_ACK (1 << 1) /* Host Interrupt ACK */
245#define SMB_USE_OOB (1 << 2) /* Use OOB Wakeup */
246#define SMB_DEV_INT (1 << 3) /* Miscellaneous Interrupt */
247
248/* tosbmailboxdata */
249#define SMB_DATA_VERSION_SHIFT 16 /* host protocol version */
250
251/*
252 * Software allocation of To Host Mailbox resources
253 */
254
255/* intstatus bits */
256#define I_HMB_FC_STATE I_HMB_SW0 /* Flow Control State */
257#define I_HMB_FC_CHANGE I_HMB_SW1 /* Flow Control State Changed */
258#define I_HMB_FRAME_IND I_HMB_SW2 /* Frame Indication */
259#define I_HMB_HOST_INT I_HMB_SW3 /* Miscellaneous Interrupt */
260
261/* tohostmailboxdata */
262#define HMB_DATA_NAKHANDLED 1 /* retransmit NAK'd frame */
263#define HMB_DATA_DEVREADY 2 /* talk to host after enable */
264#define HMB_DATA_FC 4 /* per prio flowcontrol update flag */
265#define HMB_DATA_FWREADY 8 /* fw ready for protocol activity */
266
267#define HMB_DATA_FCDATA_MASK 0xff000000
268#define HMB_DATA_FCDATA_SHIFT 24
269
270#define HMB_DATA_VERSION_MASK 0x00ff0000
271#define HMB_DATA_VERSION_SHIFT 16
272
273/*
274 * Software-defined protocol header
275 */
276
277/* Current protocol version */
278#define SDPCM_PROT_VERSION 4
279
Arend van Spriel5b435de2011-10-05 13:19:03 +0200280/*
281 * Shared structure between dongle and the host.
282 * The structure contains pointers to trap or assert information.
283 */
Arend van Spriel4fc0d012012-06-14 12:16:57 +0200284#define SDPCM_SHARED_VERSION 0x0003
Arend van Spriel5b435de2011-10-05 13:19:03 +0200285#define SDPCM_SHARED_VERSION_MASK 0x00FF
286#define SDPCM_SHARED_ASSERT_BUILT 0x0100
287#define SDPCM_SHARED_ASSERT 0x0200
288#define SDPCM_SHARED_TRAP 0x0400
289
290/* Space for header read, limit for data packets */
291#define MAX_HDR_READ (1 << 6)
292#define MAX_RX_DATASZ 2048
293
Arend van Spriel5b435de2011-10-05 13:19:03 +0200294/* Bump up limit on waiting for HT to account for first startup;
295 * if the image is doing a CRC calculation before programming the PMU
296 * for HT availability, it could take a couple hundred ms more, so
297 * max out at a 1 second (1000000us).
298 */
299#undef PMU_MAX_TRANSITION_DLY
300#define PMU_MAX_TRANSITION_DLY 1000000
301
302/* Value for ChipClockCSR during initial setup */
303#define BRCMF_INIT_CLKCTL1 (SBSDIO_FORCE_HW_CLKREQ_OFF | \
304 SBSDIO_ALP_AVAIL_REQ)
305
306/* Flags for SDH calls */
307#define F2SYNC (SDIO_REQ_4BYTE | SDIO_REQ_FIXED)
308
Franky Lin382a9e02011-12-16 18:37:18 -0800309#define BRCMF_IDLE_ACTIVE 0 /* Do not request any SD clock change
310 * when idle
311 */
312#define BRCMF_IDLE_INTERVAL 1
313
Piotr Haber4a3da992013-04-11 13:28:46 +0200314#define KSO_WAIT_US 50
315#define MAX_KSO_ATTEMPTS (PMU_MAX_TRANSITION_DLY/KSO_WAIT_US)
316
Arend van Spriel5b435de2011-10-05 13:19:03 +0200317/*
318 * Conversion of 802.1D priority to precedence level
319 */
320static uint prio2prec(u32 prio)
321{
322 return (prio == PRIO_8021D_NONE || prio == PRIO_8021D_BE) ?
323 (prio^2) : prio;
324}
325
Joe Perches8ae74652012-01-15 00:38:38 -0800326#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200327/* Device console log buffer state */
328struct brcmf_console {
329 uint count; /* Poll interval msec counter */
330 uint log_addr; /* Log struct address (fixed) */
331 struct rte_log_le log_le; /* Log struct (host copy) */
332 uint bufsize; /* Size of log buffer */
333 u8 *buf; /* Log buffer (host copy) */
334 uint last; /* Last buffer read index */
335};
Arend van Spriel4fc0d012012-06-14 12:16:57 +0200336
337struct brcmf_trap_info {
338 __le32 type;
339 __le32 epc;
340 __le32 cpsr;
341 __le32 spsr;
342 __le32 r0; /* a1 */
343 __le32 r1; /* a2 */
344 __le32 r2; /* a3 */
345 __le32 r3; /* a4 */
346 __le32 r4; /* v1 */
347 __le32 r5; /* v2 */
348 __le32 r6; /* v3 */
349 __le32 r7; /* v4 */
350 __le32 r8; /* v5 */
351 __le32 r9; /* sb/v6 */
352 __le32 r10; /* sl/v7 */
353 __le32 r11; /* fp/v8 */
354 __le32 r12; /* ip */
355 __le32 r13; /* sp */
356 __le32 r14; /* lr */
357 __le32 pc; /* r15 */
358};
Joe Perches8ae74652012-01-15 00:38:38 -0800359#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200360
361struct sdpcm_shared {
362 u32 flags;
363 u32 trap_addr;
364 u32 assert_exp_addr;
365 u32 assert_file_addr;
366 u32 assert_line;
367 u32 console_addr; /* Address of struct rte_console */
368 u32 msgtrace_addr;
369 u8 tag[32];
Arend van Spriel4fc0d012012-06-14 12:16:57 +0200370 u32 brpt_addr;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200371};
372
373struct sdpcm_shared_le {
374 __le32 flags;
375 __le32 trap_addr;
376 __le32 assert_exp_addr;
377 __le32 assert_file_addr;
378 __le32 assert_line;
379 __le32 console_addr; /* Address of struct rte_console */
380 __le32 msgtrace_addr;
381 u8 tag[32];
Arend van Spriel4fc0d012012-06-14 12:16:57 +0200382 __le32 brpt_addr;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200383};
384
Franky Lin6bc52312013-08-10 12:27:28 +0200385/* dongle SDIO bus specific header info */
386struct brcmf_sdio_hdrinfo {
Franky Lin4754fce2012-09-19 22:21:11 +0200387 u8 seq_num;
388 u8 channel;
389 u16 len;
390 u16 len_left;
391 u16 len_nxtfrm;
392 u8 dat_offset;
Franky Lin8da9d2c2013-11-29 12:25:10 +0100393 bool lastfrm;
394 u16 tail_pad;
Franky Lin4754fce2012-09-19 22:21:11 +0200395};
Arend van Spriel5b435de2011-10-05 13:19:03 +0200396
Arend van Spriel82d957e2014-07-12 08:49:36 +0200397/*
398 * hold counter variables
399 */
400struct brcmf_sdio_count {
401 uint intrcount; /* Count of device interrupt callbacks */
402 uint lastintrs; /* Count as of last watchdog timer */
403 uint pollcnt; /* Count of active polls */
404 uint regfails; /* Count of R_REG failures */
405 uint tx_sderrs; /* Count of tx attempts with sd errors */
406 uint fcqueued; /* Tx packets that got queued */
407 uint rxrtx; /* Count of rtx requests (NAK to dongle) */
408 uint rx_toolong; /* Receive frames too long to receive */
409 uint rxc_errors; /* SDIO errors when reading control frames */
410 uint rx_hdrfail; /* SDIO errors on header reads */
411 uint rx_badhdr; /* Bad received headers (roosync?) */
412 uint rx_badseq; /* Mismatched rx sequence number */
413 uint fc_rcvd; /* Number of flow-control events received */
414 uint fc_xoff; /* Number which turned on flow-control */
415 uint fc_xon; /* Number which turned off flow-control */
416 uint rxglomfail; /* Failed deglom attempts */
417 uint rxglomframes; /* Number of glom frames (superframes) */
418 uint rxglompkts; /* Number of packets from glom frames */
419 uint f2rxhdrs; /* Number of header reads */
420 uint f2rxdata; /* Number of frame data reads */
421 uint f2txdata; /* Number of f2 frame writes */
422 uint f1regdata; /* Number of f1 register accesses */
423 uint tickcnt; /* Number of watchdog been schedule */
424 ulong tx_ctlerrs; /* Err of sending ctrl frames */
425 ulong tx_ctlpkts; /* Ctrl frames sent to dongle */
426 ulong rx_ctlerrs; /* Err of processing rx ctrl frames */
427 ulong rx_ctlpkts; /* Ctrl frames processed from dongle */
428 ulong rx_readahead_cnt; /* packets where header read-ahead was used */
429};
430
Arend van Spriel5b435de2011-10-05 13:19:03 +0200431/* misc chip info needed by some of the routines */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200432/* Private data for SDIO bus interaction */
Franky Line92eedf2011-11-22 17:21:50 -0800433struct brcmf_sdio {
Arend van Spriel5b435de2011-10-05 13:19:03 +0200434 struct brcmf_sdio_dev *sdiodev; /* sdio device handler */
Arend van Spriel9cf218f2014-01-13 22:20:27 +0100435 struct brcmf_chip *ci; /* Chip info struct */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200436
Arend van Spriel5b435de2011-10-05 13:19:03 +0200437 u32 hostintmask; /* Copy of Host Interrupt Mask */
Franky Lin45316032012-09-13 21:12:03 +0200438 atomic_t intstatus; /* Intstatus bits (events) pending */
439 atomic_t fcstate; /* State of dongle flow-control */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200440
441 uint blocksize; /* Block size of SDIO transfers */
442 uint roundup; /* Max roundup limit */
443
444 struct pktq txq; /* Queue length used for flow-control */
445 u8 flowcontrol; /* per prio flow control bitmask */
446 u8 tx_seq; /* Transmit sequence number (next) */
447 u8 tx_max; /* Maximum transmit sequence allowed */
448
Arend van Spriel9b2d2f22013-11-29 12:25:14 +0100449 u8 *hdrbuf; /* buffer for handling rx frame */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200450 u8 *rxhdr; /* Header of current rx frame (in hdrbuf) */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200451 u8 rx_seq; /* Receive sequence number (expected) */
Franky Lin6bc52312013-08-10 12:27:28 +0200452 struct brcmf_sdio_hdrinfo cur_read;
Franky Lin4754fce2012-09-19 22:21:11 +0200453 /* info of current read frame */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200454 bool rxskip; /* Skip receive (awaiting NAK ACK) */
Franky Lin4754fce2012-09-19 22:21:11 +0200455 bool rxpending; /* Data frame pending in dongle */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200456
457 uint rxbound; /* Rx frames to read before resched */
458 uint txbound; /* Tx frames to send before resched */
459 uint txminmax;
460
461 struct sk_buff *glomd; /* Packet containing glomming descriptor */
Arend van Sprielb83db862011-10-19 12:51:09 +0200462 struct sk_buff_head glom; /* Packet list for glommed superframe */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200463 uint glomerr; /* Glom packet read errors */
464
465 u8 *rxbuf; /* Buffer for receiving control packets */
466 uint rxblen; /* Allocated length of rxbuf */
467 u8 *rxctl; /* Aligned pointer into rxbuf */
Franky Lindd43a012012-11-05 16:22:22 -0800468 u8 *rxctl_orig; /* pointer for freeing rxctl */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200469 uint rxlen; /* Length of valid data in buffer */
Franky Lindd43a012012-11-05 16:22:22 -0800470 spinlock_t rxctl_lock; /* protection lock for ctrl frame resources */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200471
472 u8 sdpcm_ver; /* Bus protocol reported by dongle */
473
474 bool intr; /* Use interrupts */
475 bool poll; /* Use polling */
Franky Lin1d382272012-09-13 21:11:59 +0200476 atomic_t ipend; /* Device interrupt is pending */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200477 uint spurious; /* Count of spurious interrupts */
478 uint pollrate; /* Ticks between device polls */
479 uint polltick; /* Tick counter */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200480
Joe Perches8ae74652012-01-15 00:38:38 -0800481#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200482 uint console_interval;
483 struct brcmf_console console; /* Console output polling support */
484 uint console_addr; /* Console address from shared struct */
Joe Perches8ae74652012-01-15 00:38:38 -0800485#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200486
Arend van Spriel5b435de2011-10-05 13:19:03 +0200487 uint clkstate; /* State of sd and backplane clock(s) */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200488 s32 idletime; /* Control for activity timeout */
Hante Meulemanb441ba82015-03-06 18:40:39 +0100489 s32 idlecount; /* Activity timeout counter */
490 s32 idleclock; /* How to set bus driver when idle */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200491 bool rxflow_mode; /* Rx flow control mode */
492 bool rxflow; /* Is rx flow control on */
493 bool alp_only; /* Don't use HT clock (ALP only) */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200494
Arend van Spriel5b435de2011-10-05 13:19:03 +0200495 u8 *ctrl_frame_buf;
Hante Meulemanfed7ec42014-03-15 17:18:18 +0100496 u16 ctrl_frame_len;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200497 bool ctrl_frame_stat;
Hante Meuleman4dd8b262015-01-25 20:31:32 +0100498 int ctrl_frame_err;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200499
Hante Meulemanfed7ec42014-03-15 17:18:18 +0100500 spinlock_t txq_lock; /* protect bus->txq */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200501 wait_queue_head_t ctrl_wait;
502 wait_queue_head_t dcmd_resp_wait;
503
504 struct timer_list timer;
505 struct completion watchdog_wait;
506 struct task_struct *watchdog_tsk;
507 bool wd_timer_valid;
508 uint save_ms;
509
Franky Linf1e68c22012-09-13 21:12:00 +0200510 struct workqueue_struct *brcmf_wq;
511 struct work_struct datawork;
Hante Meuleman2c64e162015-03-18 13:25:22 +0100512 bool dpc_triggered;
513 bool dpc_running;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200514
Franky Linc8bf3482011-12-16 18:37:08 -0800515 bool txoff; /* Transmit flow-controlled */
Arend van Spriel80969832012-06-09 22:51:44 +0200516 struct brcmf_sdio_count sdcnt;
Piotr Haber4a3da992013-04-11 13:28:46 +0200517 bool sr_enabled; /* SaveRestore enabled */
Arend van Spriel99824642015-02-06 18:36:45 +0100518 bool sleeping;
Franky Lin706478c2013-08-10 12:27:29 +0200519
520 u8 tx_hdrlen; /* sdio bus header length for tx packet */
Franky Lin8da9d2c2013-11-29 12:25:10 +0100521 bool txglom; /* host tx glomming enable flag */
Arend van Spriele217d1c2013-11-29 12:25:13 +0100522 u16 head_align; /* buffer pointer alignment */
523 u16 sgentry_align; /* scatter-gather buffer alignment */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200524};
525
Arend van Spriel5b435de2011-10-05 13:19:03 +0200526/* clkstate */
527#define CLK_NONE 0
528#define CLK_SDONLY 1
Piotr Haber4a3da992013-04-11 13:28:46 +0200529#define CLK_PENDING 2
Arend van Spriel5b435de2011-10-05 13:19:03 +0200530#define CLK_AVAIL 3
531
Joe Perches8ae74652012-01-15 00:38:38 -0800532#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200533static int qcount[NUMPRIO];
Joe Perches8ae74652012-01-15 00:38:38 -0800534#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200535
Hante Meuleman668761a2013-04-12 10:55:55 +0200536#define DEFAULT_SDIO_DRIVE_STRENGTH 6 /* in milliamps */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200537
538#define RETRYCHAN(chan) ((chan) == SDPCM_EVENT_CHANNEL)
539
540/* Retry count for register access failures */
541static const uint retry_limit = 2;
542
543/* Limit on rounding up frames */
544static const uint max_roundup = 512;
545
546#define ALIGNMENT 4
547
Franky Lin9d7d6f92012-10-22 10:36:24 -0700548enum brcmf_sdio_frmtype {
549 BRCMF_SDIO_FT_NORMAL,
550 BRCMF_SDIO_FT_SUPER,
551 BRCMF_SDIO_FT_SUB,
552};
553
Arend van Spriel65d80d02014-01-29 15:32:14 +0100554#define SDIOD_DRVSTR_KEY(chip, pmu) (((chip) << 16) | (pmu))
555
556/* SDIO Pad drive strength to select value mappings */
557struct sdiod_drive_str {
558 u8 strength; /* Pad Drive Strength in mA */
559 u8 sel; /* Chip-specific select value */
560};
561
562/* SDIO Drive Strength to sel value table for PMU Rev 11 (1.8V) */
563static const struct sdiod_drive_str sdiod_drvstr_tab1_1v8[] = {
564 {32, 0x6},
565 {26, 0x7},
566 {22, 0x4},
567 {16, 0x5},
568 {12, 0x2},
569 {8, 0x3},
570 {4, 0x0},
571 {0, 0x1}
572};
573
574/* SDIO Drive Strength to sel value table for PMU Rev 13 (1.8v) */
575static const struct sdiod_drive_str sdiod_drive_strength_tab5_1v8[] = {
576 {6, 0x7},
577 {5, 0x6},
578 {4, 0x5},
579 {3, 0x4},
580 {2, 0x2},
581 {1, 0x1},
582 {0, 0x0}
583};
584
585/* SDIO Drive Strength to sel value table for PMU Rev 17 (1.8v) */
586static const struct sdiod_drive_str sdiod_drvstr_tab6_1v8[] = {
587 {3, 0x3},
588 {2, 0x2},
589 {1, 0x1},
590 {0, 0x0} };
591
592/* SDIO Drive Strength to sel value table for 43143 PMU Rev 17 (3.3V) */
593static const struct sdiod_drive_str sdiod_drvstr_tab2_3v3[] = {
594 {16, 0x7},
595 {12, 0x5},
596 {8, 0x3},
597 {4, 0x1}
598};
599
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200600#define BCM43143_FIRMWARE_NAME "brcm/brcmfmac43143-sdio.bin"
601#define BCM43143_NVRAM_NAME "brcm/brcmfmac43143-sdio.txt"
602#define BCM43241B0_FIRMWARE_NAME "brcm/brcmfmac43241b0-sdio.bin"
603#define BCM43241B0_NVRAM_NAME "brcm/brcmfmac43241b0-sdio.txt"
604#define BCM43241B4_FIRMWARE_NAME "brcm/brcmfmac43241b4-sdio.bin"
605#define BCM43241B4_NVRAM_NAME "brcm/brcmfmac43241b4-sdio.txt"
Arend van Sprielba5e8662015-04-14 20:10:28 +0200606#define BCM43241B5_FIRMWARE_NAME "brcm/brcmfmac43241b5-sdio.bin"
607#define BCM43241B5_NVRAM_NAME "brcm/brcmfmac43241b5-sdio.txt"
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200608#define BCM4329_FIRMWARE_NAME "brcm/brcmfmac4329-sdio.bin"
609#define BCM4329_NVRAM_NAME "brcm/brcmfmac4329-sdio.txt"
610#define BCM4330_FIRMWARE_NAME "brcm/brcmfmac4330-sdio.bin"
611#define BCM4330_NVRAM_NAME "brcm/brcmfmac4330-sdio.txt"
612#define BCM4334_FIRMWARE_NAME "brcm/brcmfmac4334-sdio.bin"
613#define BCM4334_NVRAM_NAME "brcm/brcmfmac4334-sdio.txt"
Arend van Spriel8b3a38d2014-12-23 19:04:23 +0100614#define BCM43340_FIRMWARE_NAME "brcm/brcmfmac43340-sdio.bin"
615#define BCM43340_NVRAM_NAME "brcm/brcmfmac43340-sdio.txt"
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200616#define BCM4335_FIRMWARE_NAME "brcm/brcmfmac4335-sdio.bin"
617#define BCM4335_NVRAM_NAME "brcm/brcmfmac4335-sdio.txt"
Arend van Spriel11e69c32014-01-08 10:49:33 +0100618#define BCM43362_FIRMWARE_NAME "brcm/brcmfmac43362-sdio.bin"
619#define BCM43362_NVRAM_NAME "brcm/brcmfmac43362-sdio.txt"
Franky Linbed89b62013-11-29 12:25:09 +0100620#define BCM4339_FIRMWARE_NAME "brcm/brcmfmac4339-sdio.bin"
621#define BCM4339_NVRAM_NAME "brcm/brcmfmac4339-sdio.txt"
Arend van Spriel25911552015-03-18 13:25:26 +0100622#define BCM43430_FIRMWARE_NAME "brcm/brcmfmac43430-sdio.bin"
623#define BCM43430_NVRAM_NAME "brcm/brcmfmac43430-sdio.txt"
Arend van Spriel228a7172015-03-18 13:25:27 +0100624#define BCM43455_FIRMWARE_NAME "brcm/brcmfmac43455-sdio.bin"
625#define BCM43455_NVRAM_NAME "brcm/brcmfmac43455-sdio.txt"
Franky Lina797ca12014-03-15 17:18:17 +0100626#define BCM4354_FIRMWARE_NAME "brcm/brcmfmac4354-sdio.bin"
627#define BCM4354_NVRAM_NAME "brcm/brcmfmac4354-sdio.txt"
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200628
629MODULE_FIRMWARE(BCM43143_FIRMWARE_NAME);
630MODULE_FIRMWARE(BCM43143_NVRAM_NAME);
631MODULE_FIRMWARE(BCM43241B0_FIRMWARE_NAME);
632MODULE_FIRMWARE(BCM43241B0_NVRAM_NAME);
633MODULE_FIRMWARE(BCM43241B4_FIRMWARE_NAME);
634MODULE_FIRMWARE(BCM43241B4_NVRAM_NAME);
Arend van Sprielba5e8662015-04-14 20:10:28 +0200635MODULE_FIRMWARE(BCM43241B5_FIRMWARE_NAME);
636MODULE_FIRMWARE(BCM43241B5_NVRAM_NAME);
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200637MODULE_FIRMWARE(BCM4329_FIRMWARE_NAME);
638MODULE_FIRMWARE(BCM4329_NVRAM_NAME);
639MODULE_FIRMWARE(BCM4330_FIRMWARE_NAME);
640MODULE_FIRMWARE(BCM4330_NVRAM_NAME);
641MODULE_FIRMWARE(BCM4334_FIRMWARE_NAME);
642MODULE_FIRMWARE(BCM4334_NVRAM_NAME);
Arend van Spriel8b3a38d2014-12-23 19:04:23 +0100643MODULE_FIRMWARE(BCM43340_FIRMWARE_NAME);
644MODULE_FIRMWARE(BCM43340_NVRAM_NAME);
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200645MODULE_FIRMWARE(BCM4335_FIRMWARE_NAME);
646MODULE_FIRMWARE(BCM4335_NVRAM_NAME);
Arend van Spriel11e69c32014-01-08 10:49:33 +0100647MODULE_FIRMWARE(BCM43362_FIRMWARE_NAME);
648MODULE_FIRMWARE(BCM43362_NVRAM_NAME);
Franky Linbed89b62013-11-29 12:25:09 +0100649MODULE_FIRMWARE(BCM4339_FIRMWARE_NAME);
650MODULE_FIRMWARE(BCM4339_NVRAM_NAME);
Arend van Spriel25911552015-03-18 13:25:26 +0100651MODULE_FIRMWARE(BCM43430_FIRMWARE_NAME);
652MODULE_FIRMWARE(BCM43430_NVRAM_NAME);
Arend van Spriel228a7172015-03-18 13:25:27 +0100653MODULE_FIRMWARE(BCM43455_FIRMWARE_NAME);
654MODULE_FIRMWARE(BCM43455_NVRAM_NAME);
Franky Lina797ca12014-03-15 17:18:17 +0100655MODULE_FIRMWARE(BCM4354_FIRMWARE_NAME);
656MODULE_FIRMWARE(BCM4354_NVRAM_NAME);
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200657
658struct brcmf_firmware_names {
659 u32 chipid;
660 u32 revmsk;
661 const char *bin;
662 const char *nv;
663};
664
665enum brcmf_firmware_type {
666 BRCMF_FIRMWARE_BIN,
667 BRCMF_FIRMWARE_NVRAM
668};
669
670#define BRCMF_FIRMWARE_NVRAM(name) \
671 name ## _FIRMWARE_NAME, name ## _NVRAM_NAME
672
673static const struct brcmf_firmware_names brcmf_fwname_data[] = {
Hante Meuleman5779ae62014-07-12 08:49:34 +0200674 { BRCM_CC_43143_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM43143) },
675 { BRCM_CC_43241_CHIP_ID, 0x0000001F, BRCMF_FIRMWARE_NVRAM(BCM43241B0) },
Arend van Sprielba5e8662015-04-14 20:10:28 +0200676 { BRCM_CC_43241_CHIP_ID, 0x00000020, BRCMF_FIRMWARE_NVRAM(BCM43241B4) },
677 { BRCM_CC_43241_CHIP_ID, 0xFFFFFFC0, BRCMF_FIRMWARE_NVRAM(BCM43241B5) },
Hante Meuleman5779ae62014-07-12 08:49:34 +0200678 { BRCM_CC_4329_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM4329) },
679 { BRCM_CC_4330_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM4330) },
680 { BRCM_CC_4334_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM4334) },
Arend van Spriel8b3a38d2014-12-23 19:04:23 +0100681 { BRCM_CC_43340_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM43340) },
Hante Meuleman5779ae62014-07-12 08:49:34 +0200682 { BRCM_CC_4335_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM4335) },
683 { BRCM_CC_43362_CHIP_ID, 0xFFFFFFFE, BRCMF_FIRMWARE_NVRAM(BCM43362) },
684 { BRCM_CC_4339_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM4339) },
Arend van Spriel25911552015-03-18 13:25:26 +0100685 { BRCM_CC_43430_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM43430) },
Arend van Spriel228a7172015-03-18 13:25:27 +0100686 { BRCM_CC_4345_CHIP_ID, 0xFFFFFFC0, BRCMF_FIRMWARE_NVRAM(BCM43455) },
Hante Meuleman5779ae62014-07-12 08:49:34 +0200687 { BRCM_CC_4354_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM4354) }
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200688};
689
Daniel Kimc1b20532014-07-12 08:49:37 +0200690static int brcmf_sdio_get_fwnames(struct brcmf_chip *ci,
691 struct brcmf_sdio_dev *sdiodev)
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200692{
Arend van Sprielbd0e1b12014-05-27 12:56:22 +0200693 int i;
Daniel Kim46de0682014-07-30 13:20:00 +0200694 char end;
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200695
696 for (i = 0; i < ARRAY_SIZE(brcmf_fwname_data); i++) {
Arend van Sprielbd0e1b12014-05-27 12:56:22 +0200697 if (brcmf_fwname_data[i].chipid == ci->chip &&
Daniel Kimc1b20532014-07-12 08:49:37 +0200698 brcmf_fwname_data[i].revmsk & BIT(ci->chiprev))
699 break;
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200700 }
Daniel Kimc1b20532014-07-12 08:49:37 +0200701
702 if (i == ARRAY_SIZE(brcmf_fwname_data)) {
703 brcmf_err("Unknown chipid %d [%d]\n", ci->chip, ci->chiprev);
704 return -ENODEV;
705 }
706
707 /* check if firmware path is provided by module parameter */
708 if (brcmf_firmware_path[0] != '\0') {
Rickard Strandqvist59dfdd92014-10-12 13:42:14 +0200709 strlcpy(sdiodev->fw_name, brcmf_firmware_path,
710 sizeof(sdiodev->fw_name));
711 strlcpy(sdiodev->nvram_name, brcmf_firmware_path,
712 sizeof(sdiodev->nvram_name));
Daniel Kimc1b20532014-07-12 08:49:37 +0200713
Daniel Kim46de0682014-07-30 13:20:00 +0200714 end = brcmf_firmware_path[strlen(brcmf_firmware_path) - 1];
715 if (end != '/') {
Rickard Strandqvist59dfdd92014-10-12 13:42:14 +0200716 strlcat(sdiodev->fw_name, "/",
717 sizeof(sdiodev->fw_name));
718 strlcat(sdiodev->nvram_name, "/",
719 sizeof(sdiodev->nvram_name));
Daniel Kim46de0682014-07-30 13:20:00 +0200720 }
Daniel Kimc1b20532014-07-12 08:49:37 +0200721 }
Rickard Strandqvist59dfdd92014-10-12 13:42:14 +0200722 strlcat(sdiodev->fw_name, brcmf_fwname_data[i].bin,
723 sizeof(sdiodev->fw_name));
724 strlcat(sdiodev->nvram_name, brcmf_fwname_data[i].nv,
725 sizeof(sdiodev->nvram_name));
Daniel Kimc1b20532014-07-12 08:49:37 +0200726
727 return 0;
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200728}
729
Arend van Spriel5b435de2011-10-05 13:19:03 +0200730static void pkt_align(struct sk_buff *p, int len, int align)
731{
732 uint datalign;
733 datalign = (unsigned long)(p->data);
734 datalign = roundup(datalign, (align)) - datalign;
735 if (datalign)
736 skb_pull(p, datalign);
737 __skb_trim(p, len);
738}
739
740/* To check if there's window offered */
Franky Line92eedf2011-11-22 17:21:50 -0800741static bool data_ok(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200742{
743 return (u8)(bus->tx_max - bus->tx_seq) != 0 &&
744 ((u8)(bus->tx_max - bus->tx_seq) & 0x80) == 0;
745}
746
747/*
748 * Reads a register in the SDIO hardware block. This block occupies a series of
749 * adresses on the 32 bit backplane bus.
750 */
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100751static int r_sdreg32(struct brcmf_sdio *bus, u32 *regvar, u32 offset)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200752{
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100753 struct brcmf_core *core;
Franky Lin79ae3952012-05-04 18:27:34 -0700754 int ret;
Franky Lin58692752012-05-04 18:27:36 -0700755
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100756 core = brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV);
757 *regvar = brcmf_sdiod_regrl(bus->sdiodev, core->base + offset, &ret);
Franky Lin58692752012-05-04 18:27:36 -0700758
759 return ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200760}
761
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100762static int w_sdreg32(struct brcmf_sdio *bus, u32 regval, u32 reg_offset)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200763{
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100764 struct brcmf_core *core;
Franky Line13ce262012-05-04 18:27:35 -0700765 int ret;
Franky Lin58692752012-05-04 18:27:36 -0700766
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100767 core = brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV);
768 brcmf_sdiod_regwl(bus->sdiodev, core->base + reg_offset, regval, &ret);
Franky Lin58692752012-05-04 18:27:36 -0700769
770 return ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200771}
772
Piotr Haber4a3da992013-04-11 13:28:46 +0200773static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100774brcmf_sdio_kso_control(struct brcmf_sdio *bus, bool on)
Piotr Haber4a3da992013-04-11 13:28:46 +0200775{
776 u8 wr_val = 0, rd_val, cmp_val, bmask;
777 int err = 0;
778 int try_cnt = 0;
779
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100780 brcmf_dbg(TRACE, "Enter: on=%d\n", on);
Piotr Haber4a3da992013-04-11 13:28:46 +0200781
782 wr_val = (on << SBSDIO_FUNC1_SLEEPCSR_KSO_SHIFT);
783 /* 1st KSO write goes to AOS wake up core if device is asleep */
Arend van Spriela39be272013-12-12 11:58:58 +0100784 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
785 wr_val, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +0200786
787 if (on) {
788 /* device WAKEUP through KSO:
789 * write bit 0 & read back until
790 * both bits 0 (kso bit) & 1 (dev on status) are set
791 */
792 cmp_val = SBSDIO_FUNC1_SLEEPCSR_KSO_MASK |
793 SBSDIO_FUNC1_SLEEPCSR_DEVON_MASK;
794 bmask = cmp_val;
795 usleep_range(2000, 3000);
796 } else {
797 /* Put device to sleep, turn off KSO */
798 cmp_val = 0;
799 /* only check for bit0, bit1(dev on status) may not
800 * get cleared right away
801 */
802 bmask = SBSDIO_FUNC1_SLEEPCSR_KSO_MASK;
803 }
804
805 do {
806 /* reliable KSO bit set/clr:
807 * the sdiod sleep write access is synced to PMU 32khz clk
808 * just one write attempt may fail,
809 * read it back until it matches written value
810 */
Arend van Spriela39be272013-12-12 11:58:58 +0100811 rd_val = brcmf_sdiod_regrb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
812 &err);
Piotr Haber4a3da992013-04-11 13:28:46 +0200813 if (((rd_val & bmask) == cmp_val) && !err)
814 break;
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100815
Piotr Haber4a3da992013-04-11 13:28:46 +0200816 udelay(KSO_WAIT_US);
Arend van Spriela39be272013-12-12 11:58:58 +0100817 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
818 wr_val, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +0200819 } while (try_cnt++ < MAX_KSO_ATTEMPTS);
820
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100821 if (try_cnt > 2)
822 brcmf_dbg(SDIO, "try_cnt=%d rd_val=0x%x err=%d\n", try_cnt,
823 rd_val, err);
824
825 if (try_cnt > MAX_KSO_ATTEMPTS)
826 brcmf_err("max tries: rd_val=0x%x err=%d\n", rd_val, err);
827
Piotr Haber4a3da992013-04-11 13:28:46 +0200828 return err;
829}
830
Arend van Spriel5b435de2011-10-05 13:19:03 +0200831#define HOSTINTMASK (I_HMB_SW_MASK | I_CHIPACTIVE)
832
Arend van Spriel5b435de2011-10-05 13:19:03 +0200833/* Turn backplane clock on or off */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100834static int brcmf_sdio_htclk(struct brcmf_sdio *bus, bool on, bool pendok)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200835{
836 int err;
837 u8 clkctl, clkreq, devctl;
838 unsigned long timeout;
839
Arend van Sprielc3203372013-04-03 12:40:44 +0200840 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200841
842 clkctl = 0;
843
Piotr Haber4a3da992013-04-11 13:28:46 +0200844 if (bus->sr_enabled) {
845 bus->clkstate = (on ? CLK_AVAIL : CLK_SDONLY);
846 return 0;
847 }
848
Arend van Spriel5b435de2011-10-05 13:19:03 +0200849 if (on) {
850 /* Request HT Avail */
851 clkreq =
852 bus->alp_only ? SBSDIO_ALP_AVAIL_REQ : SBSDIO_HT_AVAIL_REQ;
853
Arend van Spriela39be272013-12-12 11:58:58 +0100854 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
855 clkreq, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200856 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100857 brcmf_err("HT Avail request error: %d\n", err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200858 return -EBADE;
859 }
860
Arend van Spriel5b435de2011-10-05 13:19:03 +0200861 /* Check current status */
Arend van Spriela39be272013-12-12 11:58:58 +0100862 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
863 SBSDIO_FUNC1_CHIPCLKCSR, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200864 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100865 brcmf_err("HT Avail read error: %d\n", err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200866 return -EBADE;
867 }
868
869 /* Go to pending and await interrupt if appropriate */
870 if (!SBSDIO_CLKAV(clkctl, bus->alp_only) && pendok) {
871 /* Allow only clock-available interrupt */
Arend van Spriela39be272013-12-12 11:58:58 +0100872 devctl = brcmf_sdiod_regrb(bus->sdiodev,
873 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200874 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100875 brcmf_err("Devctl error setting CA: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200876 err);
877 return -EBADE;
878 }
879
880 devctl |= SBSDIO_DEVCTL_CA_INT_ONLY;
Arend van Spriela39be272013-12-12 11:58:58 +0100881 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
882 devctl, &err);
Arend van Sprielc3203372013-04-03 12:40:44 +0200883 brcmf_dbg(SDIO, "CLKCTL: set PENDING\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200884 bus->clkstate = CLK_PENDING;
885
886 return 0;
887 } else if (bus->clkstate == CLK_PENDING) {
888 /* Cancel CA-only interrupt filter */
Arend van Spriela39be272013-12-12 11:58:58 +0100889 devctl = brcmf_sdiod_regrb(bus->sdiodev,
890 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200891 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
Arend van Spriela39be272013-12-12 11:58:58 +0100892 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
893 devctl, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200894 }
895
896 /* Otherwise, wait here (polling) for HT Avail */
897 timeout = jiffies +
898 msecs_to_jiffies(PMU_MAX_TRANSITION_DLY/1000);
899 while (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
Arend van Spriela39be272013-12-12 11:58:58 +0100900 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
901 SBSDIO_FUNC1_CHIPCLKCSR,
902 &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200903 if (time_after(jiffies, timeout))
904 break;
905 else
906 usleep_range(5000, 10000);
907 }
908 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100909 brcmf_err("HT Avail request error: %d\n", err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200910 return -EBADE;
911 }
912 if (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100913 brcmf_err("HT Avail timeout (%d): clkctl 0x%02x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200914 PMU_MAX_TRANSITION_DLY, clkctl);
915 return -EBADE;
916 }
917
918 /* Mark clock available */
919 bus->clkstate = CLK_AVAIL;
Arend van Sprielc3203372013-04-03 12:40:44 +0200920 brcmf_dbg(SDIO, "CLKCTL: turned ON\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200921
Joe Perches8ae74652012-01-15 00:38:38 -0800922#if defined(DEBUG)
Joe Perches23677ce2012-02-09 11:17:23 +0000923 if (!bus->alp_only) {
Arend van Spriel5b435de2011-10-05 13:19:03 +0200924 if (SBSDIO_ALPONLY(clkctl))
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100925 brcmf_err("HT Clock should be on\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200926 }
Joe Perches8ae74652012-01-15 00:38:38 -0800927#endif /* defined (DEBUG) */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200928
Arend van Spriel5b435de2011-10-05 13:19:03 +0200929 } else {
930 clkreq = 0;
931
932 if (bus->clkstate == CLK_PENDING) {
933 /* Cancel CA-only interrupt filter */
Arend van Spriela39be272013-12-12 11:58:58 +0100934 devctl = brcmf_sdiod_regrb(bus->sdiodev,
935 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200936 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
Arend van Spriela39be272013-12-12 11:58:58 +0100937 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
938 devctl, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200939 }
940
941 bus->clkstate = CLK_SDONLY;
Arend van Spriela39be272013-12-12 11:58:58 +0100942 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
943 clkreq, &err);
Arend van Sprielc3203372013-04-03 12:40:44 +0200944 brcmf_dbg(SDIO, "CLKCTL: turned OFF\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200945 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100946 brcmf_err("Failed access turning clock off: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200947 err);
948 return -EBADE;
949 }
950 }
951 return 0;
952}
953
954/* Change idle/active SD state */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100955static int brcmf_sdio_sdclk(struct brcmf_sdio *bus, bool on)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200956{
Arend van Sprielc3203372013-04-03 12:40:44 +0200957 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200958
959 if (on)
960 bus->clkstate = CLK_SDONLY;
961 else
962 bus->clkstate = CLK_NONE;
963
964 return 0;
965}
966
967/* Transition SD and backplane clock readiness */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100968static int brcmf_sdio_clkctl(struct brcmf_sdio *bus, uint target, bool pendok)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200969{
Joe Perches8ae74652012-01-15 00:38:38 -0800970#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200971 uint oldstate = bus->clkstate;
Joe Perches8ae74652012-01-15 00:38:38 -0800972#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200973
Arend van Sprielc3203372013-04-03 12:40:44 +0200974 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200975
976 /* Early exit if we're already there */
Hante Meulemanb441ba82015-03-06 18:40:39 +0100977 if (bus->clkstate == target)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200978 return 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200979
980 switch (target) {
981 case CLK_AVAIL:
982 /* Make sure SD clock is available */
983 if (bus->clkstate == CLK_NONE)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100984 brcmf_sdio_sdclk(bus, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200985 /* Now request HT Avail on the backplane */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100986 brcmf_sdio_htclk(bus, true, pendok);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200987 break;
988
989 case CLK_SDONLY:
990 /* Remove HT request, or bring up SD clock */
991 if (bus->clkstate == CLK_NONE)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100992 brcmf_sdio_sdclk(bus, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200993 else if (bus->clkstate == CLK_AVAIL)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100994 brcmf_sdio_htclk(bus, false, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200995 else
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100996 brcmf_err("request for %d -> %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200997 bus->clkstate, target);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200998 break;
999
1000 case CLK_NONE:
1001 /* Make sure to remove HT request */
1002 if (bus->clkstate == CLK_AVAIL)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001003 brcmf_sdio_htclk(bus, false, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001004 /* Now remove the SD clock */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001005 brcmf_sdio_sdclk(bus, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001006 break;
1007 }
Joe Perches8ae74652012-01-15 00:38:38 -08001008#ifdef DEBUG
Arend van Sprielc3203372013-04-03 12:40:44 +02001009 brcmf_dbg(SDIO, "%d -> %d\n", oldstate, bus->clkstate);
Joe Perches8ae74652012-01-15 00:38:38 -08001010#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02001011
1012 return 0;
1013}
1014
Piotr Haber4a3da992013-04-11 13:28:46 +02001015static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001016brcmf_sdio_bus_sleep(struct brcmf_sdio *bus, bool sleep, bool pendok)
Piotr Haber4a3da992013-04-11 13:28:46 +02001017{
1018 int err = 0;
Arend van Spriel8a385ba2014-03-15 17:18:20 +01001019 u8 clkcsr;
Arend van Spriel82030d62014-01-29 15:32:20 +01001020
1021 brcmf_dbg(SDIO, "Enter: request %s currently %s\n",
Piotr Haber4a3da992013-04-11 13:28:46 +02001022 (sleep ? "SLEEP" : "WAKE"),
Arend van Spriel99824642015-02-06 18:36:45 +01001023 (bus->sleeping ? "SLEEP" : "WAKE"));
Piotr Haber4a3da992013-04-11 13:28:46 +02001024
1025 /* If SR is enabled control bus state with KSO */
1026 if (bus->sr_enabled) {
1027 /* Done if we're already in the requested state */
Arend van Spriel99824642015-02-06 18:36:45 +01001028 if (sleep == bus->sleeping)
Piotr Haber4a3da992013-04-11 13:28:46 +02001029 goto end;
1030
1031 /* Going to sleep */
1032 if (sleep) {
Arend van Spriel8a385ba2014-03-15 17:18:20 +01001033 clkcsr = brcmf_sdiod_regrb(bus->sdiodev,
1034 SBSDIO_FUNC1_CHIPCLKCSR,
1035 &err);
1036 if ((clkcsr & SBSDIO_CSR_MASK) == 0) {
1037 brcmf_dbg(SDIO, "no clock, set ALP\n");
1038 brcmf_sdiod_regwb(bus->sdiodev,
1039 SBSDIO_FUNC1_CHIPCLKCSR,
1040 SBSDIO_ALP_AVAIL_REQ, &err);
1041 }
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001042 err = brcmf_sdio_kso_control(bus, false);
Piotr Haber4a3da992013-04-11 13:28:46 +02001043 } else {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001044 err = brcmf_sdio_kso_control(bus, true);
Piotr Haber4a3da992013-04-11 13:28:46 +02001045 }
Arend van Spriel8982cd42015-01-25 20:31:34 +01001046 if (err) {
Piotr Haber4a3da992013-04-11 13:28:46 +02001047 brcmf_err("error while changing bus sleep state %d\n",
1048 err);
Arend van Spriel8a385ba2014-03-15 17:18:20 +01001049 goto done;
Piotr Haber4a3da992013-04-11 13:28:46 +02001050 }
1051 }
1052
1053end:
1054 /* control clocks */
1055 if (sleep) {
1056 if (!bus->sr_enabled)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001057 brcmf_sdio_clkctl(bus, CLK_NONE, pendok);
Piotr Haber4a3da992013-04-11 13:28:46 +02001058 } else {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001059 brcmf_sdio_clkctl(bus, CLK_AVAIL, pendok);
Arend van Sprielcf459322015-03-11 16:11:33 +01001060 brcmf_sdio_wd_timer(bus, BRCMF_WD_POLL_MS);
Piotr Haber4a3da992013-04-11 13:28:46 +02001061 }
Arend van Spriel99824642015-02-06 18:36:45 +01001062 bus->sleeping = sleep;
Arend van Spriel8982cd42015-01-25 20:31:34 +01001063 brcmf_dbg(SDIO, "new state %s\n",
1064 (sleep ? "SLEEP" : "WAKE"));
Arend van Spriel8a385ba2014-03-15 17:18:20 +01001065done:
1066 brcmf_dbg(SDIO, "Exit: err=%d\n", err);
Piotr Haber4a3da992013-04-11 13:28:46 +02001067 return err;
1068
1069}
1070
Daniel Kim0801e6c2014-01-29 15:32:13 +01001071#ifdef DEBUG
1072static inline bool brcmf_sdio_valid_shared_address(u32 addr)
1073{
1074 return !(addr == 0 || ((~addr >> 16) & 0xffff) == (addr & 0xffff));
1075}
1076
1077static int brcmf_sdio_readshared(struct brcmf_sdio *bus,
1078 struct sdpcm_shared *sh)
1079{
Arend van Spriel9819a902015-03-11 16:11:32 +01001080 u32 addr = 0;
Daniel Kim0801e6c2014-01-29 15:32:13 +01001081 int rv;
1082 u32 shaddr = 0;
1083 struct sdpcm_shared_le sh_le;
1084 __le32 addr_le;
1085
Arend van Spriel9819a902015-03-11 16:11:32 +01001086 sdio_claim_host(bus->sdiodev->func[1]);
1087 brcmf_sdio_bus_sleep(bus, false, false);
Daniel Kim0801e6c2014-01-29 15:32:13 +01001088
1089 /*
1090 * Read last word in socram to determine
1091 * address of sdpcm_shared structure
1092 */
Arend van Spriel9819a902015-03-11 16:11:32 +01001093 shaddr = bus->ci->rambase + bus->ci->ramsize - 4;
1094 if (!bus->ci->rambase && brcmf_chip_sr_capable(bus->ci))
1095 shaddr -= bus->ci->srsize;
1096 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, shaddr,
1097 (u8 *)&addr_le, 4);
Daniel Kim0801e6c2014-01-29 15:32:13 +01001098 if (rv < 0)
Arend van Spriel9819a902015-03-11 16:11:32 +01001099 goto fail;
Daniel Kim0801e6c2014-01-29 15:32:13 +01001100
1101 /*
1102 * Check if addr is valid.
1103 * NVRAM length at the end of memory should have been overwritten.
1104 */
Arend van Spriel9819a902015-03-11 16:11:32 +01001105 addr = le32_to_cpu(addr_le);
Daniel Kim0801e6c2014-01-29 15:32:13 +01001106 if (!brcmf_sdio_valid_shared_address(addr)) {
Arend van Spriel9819a902015-03-11 16:11:32 +01001107 brcmf_err("invalid sdpcm_shared address 0x%08X\n", addr);
1108 rv = -EINVAL;
1109 goto fail;
Daniel Kim0801e6c2014-01-29 15:32:13 +01001110 }
1111
Arend van Spriel9819a902015-03-11 16:11:32 +01001112 brcmf_dbg(INFO, "sdpcm_shared address 0x%08X\n", addr);
1113
Daniel Kim0801e6c2014-01-29 15:32:13 +01001114 /* Read hndrte_shared structure */
1115 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, (u8 *)&sh_le,
1116 sizeof(struct sdpcm_shared_le));
1117 if (rv < 0)
Arend van Spriel9819a902015-03-11 16:11:32 +01001118 goto fail;
1119
1120 sdio_release_host(bus->sdiodev->func[1]);
Daniel Kim0801e6c2014-01-29 15:32:13 +01001121
1122 /* Endianness */
1123 sh->flags = le32_to_cpu(sh_le.flags);
1124 sh->trap_addr = le32_to_cpu(sh_le.trap_addr);
1125 sh->assert_exp_addr = le32_to_cpu(sh_le.assert_exp_addr);
1126 sh->assert_file_addr = le32_to_cpu(sh_le.assert_file_addr);
1127 sh->assert_line = le32_to_cpu(sh_le.assert_line);
1128 sh->console_addr = le32_to_cpu(sh_le.console_addr);
1129 sh->msgtrace_addr = le32_to_cpu(sh_le.msgtrace_addr);
1130
1131 if ((sh->flags & SDPCM_SHARED_VERSION_MASK) > SDPCM_SHARED_VERSION) {
1132 brcmf_err("sdpcm shared version unsupported: dhd %d dongle %d\n",
1133 SDPCM_SHARED_VERSION,
1134 sh->flags & SDPCM_SHARED_VERSION_MASK);
1135 return -EPROTO;
1136 }
Daniel Kim0801e6c2014-01-29 15:32:13 +01001137 return 0;
Arend van Spriel9819a902015-03-11 16:11:32 +01001138
1139fail:
1140 brcmf_err("unable to obtain sdpcm_shared info: rv=%d (addr=0x%x)\n",
1141 rv, addr);
1142 sdio_release_host(bus->sdiodev->func[1]);
1143 return rv;
Daniel Kim0801e6c2014-01-29 15:32:13 +01001144}
1145
1146static void brcmf_sdio_get_console_addr(struct brcmf_sdio *bus)
1147{
1148 struct sdpcm_shared sh;
1149
1150 if (brcmf_sdio_readshared(bus, &sh) == 0)
1151 bus->console_addr = sh.console_addr;
1152}
1153#else
1154static void brcmf_sdio_get_console_addr(struct brcmf_sdio *bus)
1155{
1156}
1157#endif /* DEBUG */
1158
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001159static u32 brcmf_sdio_hostmail(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001160{
1161 u32 intstatus = 0;
1162 u32 hmb_data;
1163 u8 fcbits;
Franky Lin58692752012-05-04 18:27:36 -07001164 int ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001165
Arend van Sprielc3203372013-04-03 12:40:44 +02001166 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001167
1168 /* Read mailbox data and ack that we did so */
Franky Lin58692752012-05-04 18:27:36 -07001169 ret = r_sdreg32(bus, &hmb_data,
1170 offsetof(struct sdpcmd_regs, tohostmailboxdata));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001171
Franky Lin58692752012-05-04 18:27:36 -07001172 if (ret == 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001173 w_sdreg32(bus, SMB_INT_ACK,
Franky Lin58692752012-05-04 18:27:36 -07001174 offsetof(struct sdpcmd_regs, tosbmailbox));
Arend van Spriel80969832012-06-09 22:51:44 +02001175 bus->sdcnt.f1regdata += 2;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001176
1177 /* Dongle recomposed rx frames, accept them again */
1178 if (hmb_data & HMB_DATA_NAKHANDLED) {
Arend van Sprielc3203372013-04-03 12:40:44 +02001179 brcmf_dbg(SDIO, "Dongle reports NAK handled, expect rtx of %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001180 bus->rx_seq);
1181 if (!bus->rxskip)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001182 brcmf_err("unexpected NAKHANDLED!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001183
1184 bus->rxskip = false;
1185 intstatus |= I_HMB_FRAME_IND;
1186 }
1187
1188 /*
1189 * DEVREADY does not occur with gSPI.
1190 */
1191 if (hmb_data & (HMB_DATA_DEVREADY | HMB_DATA_FWREADY)) {
1192 bus->sdpcm_ver =
1193 (hmb_data & HMB_DATA_VERSION_MASK) >>
1194 HMB_DATA_VERSION_SHIFT;
1195 if (bus->sdpcm_ver != SDPCM_PROT_VERSION)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001196 brcmf_err("Version mismatch, dongle reports %d, "
Arend van Spriel5b435de2011-10-05 13:19:03 +02001197 "expecting %d\n",
1198 bus->sdpcm_ver, SDPCM_PROT_VERSION);
1199 else
Arend van Sprielc3203372013-04-03 12:40:44 +02001200 brcmf_dbg(SDIO, "Dongle ready, protocol version %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001201 bus->sdpcm_ver);
Daniel Kim0801e6c2014-01-29 15:32:13 +01001202
1203 /*
1204 * Retrieve console state address now that firmware should have
1205 * updated it.
1206 */
1207 brcmf_sdio_get_console_addr(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001208 }
1209
1210 /*
1211 * Flow Control has been moved into the RX headers and this out of band
1212 * method isn't used any more.
1213 * remaining backward compatible with older dongles.
1214 */
1215 if (hmb_data & HMB_DATA_FC) {
1216 fcbits = (hmb_data & HMB_DATA_FCDATA_MASK) >>
1217 HMB_DATA_FCDATA_SHIFT;
1218
1219 if (fcbits & ~bus->flowcontrol)
Arend van Spriel80969832012-06-09 22:51:44 +02001220 bus->sdcnt.fc_xoff++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001221
1222 if (bus->flowcontrol & ~fcbits)
Arend van Spriel80969832012-06-09 22:51:44 +02001223 bus->sdcnt.fc_xon++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001224
Arend van Spriel80969832012-06-09 22:51:44 +02001225 bus->sdcnt.fc_rcvd++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001226 bus->flowcontrol = fcbits;
1227 }
1228
1229 /* Shouldn't be any others */
1230 if (hmb_data & ~(HMB_DATA_DEVREADY |
1231 HMB_DATA_NAKHANDLED |
1232 HMB_DATA_FC |
1233 HMB_DATA_FWREADY |
1234 HMB_DATA_FCDATA_MASK | HMB_DATA_VERSION_MASK))
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001235 brcmf_err("Unknown mailbox data content: 0x%02x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001236 hmb_data);
1237
1238 return intstatus;
1239}
1240
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001241static void brcmf_sdio_rxfail(struct brcmf_sdio *bus, bool abort, bool rtx)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001242{
1243 uint retries = 0;
1244 u16 lastrbc;
1245 u8 hi, lo;
1246 int err;
1247
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001248 brcmf_err("%sterminate frame%s\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001249 abort ? "abort command, " : "",
1250 rtx ? ", send NAK" : "");
1251
1252 if (abort)
Arend van Spriela39be272013-12-12 11:58:58 +01001253 brcmf_sdiod_abort(bus->sdiodev, SDIO_FUNC_2);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001254
Arend van Spriela39be272013-12-12 11:58:58 +01001255 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL,
1256 SFC_RF_TERM, &err);
Arend van Spriel80969832012-06-09 22:51:44 +02001257 bus->sdcnt.f1regdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001258
1259 /* Wait until the packet has been flushed (device/FIFO stable) */
1260 for (lastrbc = retries = 0xffff; retries > 0; retries--) {
Arend van Spriela39be272013-12-12 11:58:58 +01001261 hi = brcmf_sdiod_regrb(bus->sdiodev,
1262 SBSDIO_FUNC1_RFRAMEBCHI, &err);
1263 lo = brcmf_sdiod_regrb(bus->sdiodev,
1264 SBSDIO_FUNC1_RFRAMEBCLO, &err);
Arend van Spriel80969832012-06-09 22:51:44 +02001265 bus->sdcnt.f1regdata += 2;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001266
1267 if ((hi == 0) && (lo == 0))
1268 break;
1269
1270 if ((hi > (lastrbc >> 8)) && (lo > (lastrbc & 0x00ff))) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001271 brcmf_err("count growing: last 0x%04x now 0x%04x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001272 lastrbc, (hi << 8) + lo);
1273 }
1274 lastrbc = (hi << 8) + lo;
1275 }
1276
1277 if (!retries)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001278 brcmf_err("count never zeroed: last 0x%04x\n", lastrbc);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001279 else
Arend van Sprielc3203372013-04-03 12:40:44 +02001280 brcmf_dbg(SDIO, "flush took %d iterations\n", 0xffff - retries);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001281
1282 if (rtx) {
Arend van Spriel80969832012-06-09 22:51:44 +02001283 bus->sdcnt.rxrtx++;
Franky Lin58692752012-05-04 18:27:36 -07001284 err = w_sdreg32(bus, SMB_NAK,
1285 offsetof(struct sdpcmd_regs, tosbmailbox));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001286
Arend van Spriel80969832012-06-09 22:51:44 +02001287 bus->sdcnt.f1regdata++;
Franky Lin58692752012-05-04 18:27:36 -07001288 if (err == 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001289 bus->rxskip = true;
1290 }
1291
1292 /* Clear partial in any case */
Franky Lin4754fce2012-09-19 22:21:11 +02001293 bus->cur_read.len = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001294}
1295
Hante Meuleman81c78832014-02-25 20:30:35 +01001296static void brcmf_sdio_txfail(struct brcmf_sdio *bus)
1297{
1298 struct brcmf_sdio_dev *sdiodev = bus->sdiodev;
1299 u8 i, hi, lo;
1300
1301 /* On failure, abort the command and terminate the frame */
1302 brcmf_err("sdio error, abort command and terminate frame\n");
1303 bus->sdcnt.tx_sderrs++;
1304
1305 brcmf_sdiod_abort(sdiodev, SDIO_FUNC_2);
1306 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_FRAMECTRL, SFC_WF_TERM, NULL);
1307 bus->sdcnt.f1regdata++;
1308
1309 for (i = 0; i < 3; i++) {
1310 hi = brcmf_sdiod_regrb(sdiodev, SBSDIO_FUNC1_WFRAMEBCHI, NULL);
1311 lo = brcmf_sdiod_regrb(sdiodev, SBSDIO_FUNC1_WFRAMEBCLO, NULL);
1312 bus->sdcnt.f1regdata += 2;
1313 if ((hi == 0) && (lo == 0))
1314 break;
1315 }
1316}
1317
Arend van Spriel9a95e602011-11-10 20:30:29 +01001318/* return total length of buffer chain */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001319static uint brcmf_sdio_glom_len(struct brcmf_sdio *bus)
Arend van Spriel9a95e602011-11-10 20:30:29 +01001320{
1321 struct sk_buff *p;
1322 uint total;
1323
1324 total = 0;
1325 skb_queue_walk(&bus->glom, p)
1326 total += p->len;
1327 return total;
1328}
1329
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001330static void brcmf_sdio_free_glom(struct brcmf_sdio *bus)
Arend van Spriel046808d2011-11-10 20:30:31 +01001331{
1332 struct sk_buff *cur, *next;
1333
1334 skb_queue_walk_safe(&bus->glom, cur, next) {
1335 skb_unlink(cur, &bus->glom);
1336 brcmu_pkt_buf_free_skb(cur);
1337 }
1338}
1339
Franky Lin6bc52312013-08-10 12:27:28 +02001340/**
1341 * brcmfmac sdio bus specific header
1342 * This is the lowest layer header wrapped on the packets transmitted between
1343 * host and WiFi dongle which contains information needed for SDIO core and
1344 * firmware
1345 *
Franky Lin8da9d2c2013-11-29 12:25:10 +01001346 * It consists of 3 parts: hardware header, hardware extension header and
1347 * software header
Franky Lin6bc52312013-08-10 12:27:28 +02001348 * hardware header (frame tag) - 4 bytes
1349 * Byte 0~1: Frame length
1350 * Byte 2~3: Checksum, bit-wise inverse of frame length
Franky Lin8da9d2c2013-11-29 12:25:10 +01001351 * hardware extension header - 8 bytes
1352 * Tx glom mode only, N/A for Rx or normal Tx
1353 * Byte 0~1: Packet length excluding hw frame tag
1354 * Byte 2: Reserved
1355 * Byte 3: Frame flags, bit 0: last frame indication
1356 * Byte 4~5: Reserved
1357 * Byte 6~7: Tail padding length
Franky Lin6bc52312013-08-10 12:27:28 +02001358 * software header - 8 bytes
1359 * Byte 0: Rx/Tx sequence number
1360 * Byte 1: 4 MSB Channel number, 4 LSB arbitrary flag
1361 * Byte 2: Length of next data frame, reserved for Tx
1362 * Byte 3: Data offset
1363 * Byte 4: Flow control bits, reserved for Tx
1364 * Byte 5: Maximum Sequence number allowed by firmware for Tx, N/A for Tx packet
1365 * Byte 6~7: Reserved
1366 */
1367#define SDPCM_HWHDR_LEN 4
Franky Lin8da9d2c2013-11-29 12:25:10 +01001368#define SDPCM_HWEXT_LEN 8
Franky Lin6bc52312013-08-10 12:27:28 +02001369#define SDPCM_SWHDR_LEN 8
1370#define SDPCM_HDRLEN (SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN)
Franky Lin6bc52312013-08-10 12:27:28 +02001371/* software header */
1372#define SDPCM_SEQ_MASK 0x000000ff
1373#define SDPCM_SEQ_WRAP 256
1374#define SDPCM_CHANNEL_MASK 0x00000f00
1375#define SDPCM_CHANNEL_SHIFT 8
1376#define SDPCM_CONTROL_CHANNEL 0 /* Control */
1377#define SDPCM_EVENT_CHANNEL 1 /* Asyc Event Indication */
1378#define SDPCM_DATA_CHANNEL 2 /* Data Xmit/Recv */
1379#define SDPCM_GLOM_CHANNEL 3 /* Coalesced packets */
1380#define SDPCM_TEST_CHANNEL 15 /* Test/debug packets */
1381#define SDPCM_GLOMDESC(p) (((u8 *)p)[1] & 0x80)
1382#define SDPCM_NEXTLEN_MASK 0x00ff0000
1383#define SDPCM_NEXTLEN_SHIFT 16
1384#define SDPCM_DOFFSET_MASK 0xff000000
1385#define SDPCM_DOFFSET_SHIFT 24
1386#define SDPCM_FCMASK_MASK 0x000000ff
1387#define SDPCM_WINDOW_MASK 0x0000ff00
1388#define SDPCM_WINDOW_SHIFT 8
1389
1390static inline u8 brcmf_sdio_getdatoffset(u8 *swheader)
1391{
1392 u32 hdrvalue;
1393 hdrvalue = *(u32 *)swheader;
1394 return (u8)((hdrvalue & SDPCM_DOFFSET_MASK) >> SDPCM_DOFFSET_SHIFT);
1395}
1396
1397static int brcmf_sdio_hdparse(struct brcmf_sdio *bus, u8 *header,
1398 struct brcmf_sdio_hdrinfo *rd,
1399 enum brcmf_sdio_frmtype type)
Franky Lin4754fce2012-09-19 22:21:11 +02001400{
1401 u16 len, checksum;
1402 u8 rx_seq, fc, tx_seq_max;
Franky Lin6bc52312013-08-10 12:27:28 +02001403 u32 swheader;
Franky Lin4754fce2012-09-19 22:21:11 +02001404
Arend van Spriel4b776962013-11-29 12:25:06 +01001405 trace_brcmf_sdpcm_hdr(SDPCM_RX, header);
Arend van Spriel76584ec2013-10-15 15:44:47 +02001406
Franky Lin6bc52312013-08-10 12:27:28 +02001407 /* hw header */
Franky Lin4754fce2012-09-19 22:21:11 +02001408 len = get_unaligned_le16(header);
1409 checksum = get_unaligned_le16(header + sizeof(u16));
1410 /* All zero means no more to read */
1411 if (!(len | checksum)) {
1412 bus->rxpending = false;
Franky Lin10510582012-11-05 16:22:26 -08001413 return -ENODATA;
Franky Lin4754fce2012-09-19 22:21:11 +02001414 }
1415 if ((u16)(~(len ^ checksum))) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001416 brcmf_err("HW header checksum error\n");
Franky Lin4754fce2012-09-19 22:21:11 +02001417 bus->sdcnt.rx_badhdr++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001418 brcmf_sdio_rxfail(bus, false, false);
Franky Lin10510582012-11-05 16:22:26 -08001419 return -EIO;
Franky Lin4754fce2012-09-19 22:21:11 +02001420 }
1421 if (len < SDPCM_HDRLEN) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001422 brcmf_err("HW header length error\n");
Franky Lin10510582012-11-05 16:22:26 -08001423 return -EPROTO;
Franky Lin4754fce2012-09-19 22:21:11 +02001424 }
Franky Lin9d7d6f92012-10-22 10:36:24 -07001425 if (type == BRCMF_SDIO_FT_SUPER &&
1426 (roundup(len, bus->blocksize) != rd->len)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001427 brcmf_err("HW superframe header length error\n");
Franky Lin10510582012-11-05 16:22:26 -08001428 return -EPROTO;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001429 }
1430 if (type == BRCMF_SDIO_FT_SUB && len > rd->len) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001431 brcmf_err("HW subframe header length error\n");
Franky Lin10510582012-11-05 16:22:26 -08001432 return -EPROTO;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001433 }
Franky Lin4754fce2012-09-19 22:21:11 +02001434 rd->len = len;
1435
Franky Lin6bc52312013-08-10 12:27:28 +02001436 /* software header */
1437 header += SDPCM_HWHDR_LEN;
1438 swheader = le32_to_cpu(*(__le32 *)header);
1439 if (type == BRCMF_SDIO_FT_SUPER && SDPCM_GLOMDESC(header)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001440 brcmf_err("Glom descriptor found in superframe head\n");
Franky Lin9d7d6f92012-10-22 10:36:24 -07001441 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001442 return -EINVAL;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001443 }
Franky Lin6bc52312013-08-10 12:27:28 +02001444 rx_seq = (u8)(swheader & SDPCM_SEQ_MASK);
1445 rd->channel = (swheader & SDPCM_CHANNEL_MASK) >> SDPCM_CHANNEL_SHIFT;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001446 if (len > MAX_RX_DATASZ && rd->channel != SDPCM_CONTROL_CHANNEL &&
1447 type != BRCMF_SDIO_FT_SUPER) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001448 brcmf_err("HW header length too long\n");
Franky Lin4754fce2012-09-19 22:21:11 +02001449 bus->sdcnt.rx_toolong++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001450 brcmf_sdio_rxfail(bus, false, false);
Franky Lin4754fce2012-09-19 22:21:11 +02001451 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001452 return -EPROTO;
Franky Lin4754fce2012-09-19 22:21:11 +02001453 }
Franky Lin9d7d6f92012-10-22 10:36:24 -07001454 if (type == BRCMF_SDIO_FT_SUPER && rd->channel != SDPCM_GLOM_CHANNEL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001455 brcmf_err("Wrong channel for superframe\n");
Franky Lin9d7d6f92012-10-22 10:36:24 -07001456 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001457 return -EINVAL;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001458 }
1459 if (type == BRCMF_SDIO_FT_SUB && rd->channel != SDPCM_DATA_CHANNEL &&
1460 rd->channel != SDPCM_EVENT_CHANNEL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001461 brcmf_err("Wrong channel for subframe\n");
Franky Lin9d7d6f92012-10-22 10:36:24 -07001462 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001463 return -EINVAL;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001464 }
Franky Lin6bc52312013-08-10 12:27:28 +02001465 rd->dat_offset = brcmf_sdio_getdatoffset(header);
Franky Lin4754fce2012-09-19 22:21:11 +02001466 if (rd->dat_offset < SDPCM_HDRLEN || rd->dat_offset > rd->len) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001467 brcmf_err("seq %d: bad data offset\n", rx_seq);
Franky Lin4754fce2012-09-19 22:21:11 +02001468 bus->sdcnt.rx_badhdr++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001469 brcmf_sdio_rxfail(bus, false, false);
Franky Lin4754fce2012-09-19 22:21:11 +02001470 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001471 return -ENXIO;
Franky Lin4754fce2012-09-19 22:21:11 +02001472 }
1473 if (rd->seq_num != rx_seq) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001474 brcmf_err("seq %d: sequence number error, expect %d\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001475 rx_seq, rd->seq_num);
1476 bus->sdcnt.rx_badseq++;
1477 rd->seq_num = rx_seq;
1478 }
Franky Lin9d7d6f92012-10-22 10:36:24 -07001479 /* no need to check the reset for subframe */
1480 if (type == BRCMF_SDIO_FT_SUB)
Franky Lin10510582012-11-05 16:22:26 -08001481 return 0;
Franky Lin6bc52312013-08-10 12:27:28 +02001482 rd->len_nxtfrm = (swheader & SDPCM_NEXTLEN_MASK) >> SDPCM_NEXTLEN_SHIFT;
Franky Lin4754fce2012-09-19 22:21:11 +02001483 if (rd->len_nxtfrm << 4 > MAX_RX_DATASZ) {
1484 /* only warm for NON glom packet */
1485 if (rd->channel != SDPCM_GLOM_CHANNEL)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001486 brcmf_err("seq %d: next length error\n", rx_seq);
Franky Lin4754fce2012-09-19 22:21:11 +02001487 rd->len_nxtfrm = 0;
1488 }
Franky Lin6bc52312013-08-10 12:27:28 +02001489 swheader = le32_to_cpu(*(__le32 *)(header + 4));
1490 fc = swheader & SDPCM_FCMASK_MASK;
Franky Lin4754fce2012-09-19 22:21:11 +02001491 if (bus->flowcontrol != fc) {
1492 if (~bus->flowcontrol & fc)
1493 bus->sdcnt.fc_xoff++;
1494 if (bus->flowcontrol & ~fc)
1495 bus->sdcnt.fc_xon++;
1496 bus->sdcnt.fc_rcvd++;
1497 bus->flowcontrol = fc;
1498 }
Franky Lin6bc52312013-08-10 12:27:28 +02001499 tx_seq_max = (swheader & SDPCM_WINDOW_MASK) >> SDPCM_WINDOW_SHIFT;
Franky Lin4754fce2012-09-19 22:21:11 +02001500 if ((u8)(tx_seq_max - bus->tx_seq) > 0x40) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001501 brcmf_err("seq %d: max tx seq number error\n", rx_seq);
Franky Lin4754fce2012-09-19 22:21:11 +02001502 tx_seq_max = bus->tx_seq + 2;
1503 }
1504 bus->tx_max = tx_seq_max;
1505
Franky Lin10510582012-11-05 16:22:26 -08001506 return 0;
Franky Lin4754fce2012-09-19 22:21:11 +02001507}
1508
Franky Lin6bc52312013-08-10 12:27:28 +02001509static inline void brcmf_sdio_update_hwhdr(u8 *header, u16 frm_length)
1510{
1511 *(__le16 *)header = cpu_to_le16(frm_length);
1512 *(((__le16 *)header) + 1) = cpu_to_le16(~frm_length);
1513}
1514
1515static void brcmf_sdio_hdpack(struct brcmf_sdio *bus, u8 *header,
1516 struct brcmf_sdio_hdrinfo *hd_info)
1517{
Franky Lin8da9d2c2013-11-29 12:25:10 +01001518 u32 hdrval;
1519 u8 hdr_offset;
Franky Lin6bc52312013-08-10 12:27:28 +02001520
1521 brcmf_sdio_update_hwhdr(header, hd_info->len);
Franky Lin8da9d2c2013-11-29 12:25:10 +01001522 hdr_offset = SDPCM_HWHDR_LEN;
Franky Lin6bc52312013-08-10 12:27:28 +02001523
Franky Lin8da9d2c2013-11-29 12:25:10 +01001524 if (bus->txglom) {
1525 hdrval = (hd_info->len - hdr_offset) | (hd_info->lastfrm << 24);
1526 *((__le32 *)(header + hdr_offset)) = cpu_to_le32(hdrval);
1527 hdrval = (u16)hd_info->tail_pad << 16;
1528 *(((__le32 *)(header + hdr_offset)) + 1) = cpu_to_le32(hdrval);
1529 hdr_offset += SDPCM_HWEXT_LEN;
1530 }
1531
1532 hdrval = hd_info->seq_num;
1533 hdrval |= (hd_info->channel << SDPCM_CHANNEL_SHIFT) &
1534 SDPCM_CHANNEL_MASK;
1535 hdrval |= (hd_info->dat_offset << SDPCM_DOFFSET_SHIFT) &
1536 SDPCM_DOFFSET_MASK;
1537 *((__le32 *)(header + hdr_offset)) = cpu_to_le32(hdrval);
1538 *(((__le32 *)(header + hdr_offset)) + 1) = 0;
1539 trace_brcmf_sdpcm_hdr(SDPCM_TX + !!(bus->txglom), header);
Franky Lin6bc52312013-08-10 12:27:28 +02001540}
1541
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001542static u8 brcmf_sdio_rxglom(struct brcmf_sdio *bus, u8 rxseq)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001543{
1544 u16 dlen, totlen;
1545 u8 *dptr, num = 0;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001546 u16 sublen;
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001547 struct sk_buff *pfirst, *pnext;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001548
1549 int errcode;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001550 u8 doff, sfdoff;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001551
Franky Lin6bc52312013-08-10 12:27:28 +02001552 struct brcmf_sdio_hdrinfo rd_new;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001553
1554 /* If packets, issue read(s) and send up packet chain */
1555 /* Return sequence numbers consumed? */
1556
Arend van Sprielc3203372013-04-03 12:40:44 +02001557 brcmf_dbg(SDIO, "start: glomd %p glom %p\n",
Arend van Sprielb83db862011-10-19 12:51:09 +02001558 bus->glomd, skb_peek(&bus->glom));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001559
1560 /* If there's a descriptor, generate the packet chain */
1561 if (bus->glomd) {
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001562 pfirst = pnext = NULL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001563 dlen = (u16) (bus->glomd->len);
1564 dptr = bus->glomd->data;
1565 if (!dlen || (dlen & 1)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001566 brcmf_err("bad glomd len(%d), ignore descriptor\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001567 dlen);
1568 dlen = 0;
1569 }
1570
1571 for (totlen = num = 0; dlen; num++) {
1572 /* Get (and move past) next length */
1573 sublen = get_unaligned_le16(dptr);
1574 dlen -= sizeof(u16);
1575 dptr += sizeof(u16);
1576 if ((sublen < SDPCM_HDRLEN) ||
1577 ((num == 0) && (sublen < (2 * SDPCM_HDRLEN)))) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001578 brcmf_err("descriptor len %d bad: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001579 num, sublen);
1580 pnext = NULL;
1581 break;
1582 }
Arend van Spriele217d1c2013-11-29 12:25:13 +01001583 if (sublen % bus->sgentry_align) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001584 brcmf_err("sublen %d not multiple of %d\n",
Arend van Spriele217d1c2013-11-29 12:25:13 +01001585 sublen, bus->sgentry_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001586 }
1587 totlen += sublen;
1588
1589 /* For last frame, adjust read len so total
1590 is a block multiple */
1591 if (!dlen) {
1592 sublen +=
1593 (roundup(totlen, bus->blocksize) - totlen);
1594 totlen = roundup(totlen, bus->blocksize);
1595 }
1596
1597 /* Allocate/chain packet for next subframe */
Arend van Spriele217d1c2013-11-29 12:25:13 +01001598 pnext = brcmu_pkt_buf_get_skb(sublen + bus->sgentry_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001599 if (pnext == NULL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001600 brcmf_err("bcm_pkt_buf_get_skb failed, num %d len %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001601 num, sublen);
1602 break;
1603 }
Arend van Sprielb83db862011-10-19 12:51:09 +02001604 skb_queue_tail(&bus->glom, pnext);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001605
1606 /* Adhere to start alignment requirements */
Arend van Spriele217d1c2013-11-29 12:25:13 +01001607 pkt_align(pnext, sublen, bus->sgentry_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001608 }
1609
1610 /* If all allocations succeeded, save packet chain
1611 in bus structure */
1612 if (pnext) {
1613 brcmf_dbg(GLOM, "allocated %d-byte packet chain for %d subframes\n",
1614 totlen, num);
Franky Lin4754fce2012-09-19 22:21:11 +02001615 if (BRCMF_GLOM_ON() && bus->cur_read.len &&
1616 totlen != bus->cur_read.len) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001617 brcmf_dbg(GLOM, "glomdesc mismatch: nextlen %d glomdesc %d rxseq %d\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001618 bus->cur_read.len, totlen, rxseq);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001619 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02001620 pfirst = pnext = NULL;
1621 } else {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001622 brcmf_sdio_free_glom(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001623 num = 0;
1624 }
1625
1626 /* Done with descriptor packet */
1627 brcmu_pkt_buf_free_skb(bus->glomd);
1628 bus->glomd = NULL;
Franky Lin4754fce2012-09-19 22:21:11 +02001629 bus->cur_read.len = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001630 }
1631
1632 /* Ok -- either we just generated a packet chain,
1633 or had one from before */
Arend van Sprielb83db862011-10-19 12:51:09 +02001634 if (!skb_queue_empty(&bus->glom)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001635 if (BRCMF_GLOM_ON()) {
1636 brcmf_dbg(GLOM, "try superframe read, packet chain:\n");
Arend van Sprielb83db862011-10-19 12:51:09 +02001637 skb_queue_walk(&bus->glom, pnext) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001638 brcmf_dbg(GLOM, " %p: %p len 0x%04x (%d)\n",
1639 pnext, (u8 *) (pnext->data),
1640 pnext->len, pnext->len);
1641 }
1642 }
1643
Arend van Sprielb83db862011-10-19 12:51:09 +02001644 pfirst = skb_peek(&bus->glom);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001645 dlen = (u16) brcmf_sdio_glom_len(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001646
1647 /* Do an SDIO read for the superframe. Configurable iovar to
1648 * read directly into the chained packet, or allocate a large
1649 * packet and and copy into the chain.
1650 */
Franky Lin38b0b0d2012-11-05 16:22:24 -08001651 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriela39be272013-12-12 11:58:58 +01001652 errcode = brcmf_sdiod_recv_chain(bus->sdiodev,
Arend van Spriela39be272013-12-12 11:58:58 +01001653 &bus->glom, dlen);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001654 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel80969832012-06-09 22:51:44 +02001655 bus->sdcnt.f2rxdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001656
1657 /* On failure, kill the superframe, allow a couple retries */
1658 if (errcode < 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001659 brcmf_err("glom read of %d bytes failed: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001660 dlen, errcode);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001661
Franky Lin38b0b0d2012-11-05 16:22:24 -08001662 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001663 if (bus->glomerr++ < 3) {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001664 brcmf_sdio_rxfail(bus, true, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001665 } else {
1666 bus->glomerr = 0;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001667 brcmf_sdio_rxfail(bus, true, false);
Arend van Spriel80969832012-06-09 22:51:44 +02001668 bus->sdcnt.rxglomfail++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001669 brcmf_sdio_free_glom(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001670 }
Franky Lin38b0b0d2012-11-05 16:22:24 -08001671 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001672 return 0;
1673 }
Joe Perches1e023822012-01-15 00:38:40 -08001674
1675 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
1676 pfirst->data, min_t(int, pfirst->len, 48),
1677 "SUPERFRAME:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001678
Franky Lin9d7d6f92012-10-22 10:36:24 -07001679 rd_new.seq_num = rxseq;
1680 rd_new.len = dlen;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001681 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin6bc52312013-08-10 12:27:28 +02001682 errcode = brcmf_sdio_hdparse(bus, pfirst->data, &rd_new,
1683 BRCMF_SDIO_FT_SUPER);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001684 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin9d7d6f92012-10-22 10:36:24 -07001685 bus->cur_read.len = rd_new.len_nxtfrm << 4;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001686
1687 /* Remove superframe header, remember offset */
Franky Lin9d7d6f92012-10-22 10:36:24 -07001688 skb_pull(pfirst, rd_new.dat_offset);
1689 sfdoff = rd_new.dat_offset;
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001690 num = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001691
1692 /* Validate all the subframe headers */
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001693 skb_queue_walk(&bus->glom, pnext) {
1694 /* leave when invalid subframe is found */
1695 if (errcode)
1696 break;
1697
Franky Lin9d7d6f92012-10-22 10:36:24 -07001698 rd_new.len = pnext->len;
1699 rd_new.seq_num = rxseq++;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001700 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin6bc52312013-08-10 12:27:28 +02001701 errcode = brcmf_sdio_hdparse(bus, pnext->data, &rd_new,
1702 BRCMF_SDIO_FT_SUB);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001703 sdio_release_host(bus->sdiodev->func[1]);
Joe Perches1e023822012-01-15 00:38:40 -08001704 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
Franky Lin9d7d6f92012-10-22 10:36:24 -07001705 pnext->data, 32, "subframe:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001706
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001707 num++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001708 }
1709
1710 if (errcode) {
1711 /* Terminate frame on error, request
1712 a couple retries */
Franky Lin38b0b0d2012-11-05 16:22:24 -08001713 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001714 if (bus->glomerr++ < 3) {
1715 /* Restore superframe header space */
1716 skb_push(pfirst, sfdoff);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001717 brcmf_sdio_rxfail(bus, true, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001718 } else {
1719 bus->glomerr = 0;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001720 brcmf_sdio_rxfail(bus, true, false);
Arend van Spriel80969832012-06-09 22:51:44 +02001721 bus->sdcnt.rxglomfail++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001722 brcmf_sdio_free_glom(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001723 }
Franky Lin38b0b0d2012-11-05 16:22:24 -08001724 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001725 bus->cur_read.len = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001726 return 0;
1727 }
1728
1729 /* Basic SD framing looks ok - process each packet (header) */
Arend van Spriel5b435de2011-10-05 13:19:03 +02001730
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001731 skb_queue_walk_safe(&bus->glom, pfirst, pnext) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001732 dptr = (u8 *) (pfirst->data);
1733 sublen = get_unaligned_le16(dptr);
Franky Lin6bc52312013-08-10 12:27:28 +02001734 doff = brcmf_sdio_getdatoffset(&dptr[SDPCM_HWHDR_LEN]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001735
Joe Perches1e023822012-01-15 00:38:40 -08001736 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_DATA_ON(),
Franky Lin9d7d6f92012-10-22 10:36:24 -07001737 dptr, pfirst->len,
1738 "Rx Subframe Data:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001739
1740 __skb_trim(pfirst, sublen);
1741 skb_pull(pfirst, doff);
1742
1743 if (pfirst->len == 0) {
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001744 skb_unlink(pfirst, &bus->glom);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001745 brcmu_pkt_buf_free_skb(pfirst);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001746 continue;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001747 }
1748
Joe Perches1e023822012-01-15 00:38:40 -08001749 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
1750 pfirst->data,
1751 min_t(int, pfirst->len, 32),
1752 "subframe %d to stack, %p (%p/%d) nxt/lnk %p/%p\n",
1753 bus->glom.qlen, pfirst, pfirst->data,
1754 pfirst->len, pfirst->next,
1755 pfirst->prev);
Arend van Spriel05f38202013-09-25 13:05:46 +02001756 skb_unlink(pfirst, &bus->glom);
1757 brcmf_rx_frame(bus->sdiodev->dev, pfirst);
1758 bus->sdcnt.rxglompkts++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001759 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02001760
Arend van Spriel80969832012-06-09 22:51:44 +02001761 bus->sdcnt.rxglomframes++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001762 }
1763 return num;
1764}
1765
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001766static int brcmf_sdio_dcmd_resp_wait(struct brcmf_sdio *bus, uint *condition,
1767 bool *pending)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001768{
1769 DECLARE_WAITQUEUE(wait, current);
1770 int timeout = msecs_to_jiffies(DCMD_RESP_TIMEOUT);
1771
1772 /* Wait until control frame is available */
1773 add_wait_queue(&bus->dcmd_resp_wait, &wait);
1774 set_current_state(TASK_INTERRUPTIBLE);
1775
1776 while (!(*condition) && (!signal_pending(current) && timeout))
1777 timeout = schedule_timeout(timeout);
1778
1779 if (signal_pending(current))
1780 *pending = true;
1781
1782 set_current_state(TASK_RUNNING);
1783 remove_wait_queue(&bus->dcmd_resp_wait, &wait);
1784
1785 return timeout;
1786}
1787
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001788static int brcmf_sdio_dcmd_resp_wake(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001789{
1790 if (waitqueue_active(&bus->dcmd_resp_wait))
1791 wake_up_interruptible(&bus->dcmd_resp_wait);
1792
1793 return 0;
1794}
1795static void
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001796brcmf_sdio_read_control(struct brcmf_sdio *bus, u8 *hdr, uint len, uint doff)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001797{
1798 uint rdlen, pad;
Franky Lindd43a012012-11-05 16:22:22 -08001799 u8 *buf = NULL, *rbuf;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001800 int sdret;
1801
1802 brcmf_dbg(TRACE, "Enter\n");
1803
Franky Lindd43a012012-11-05 16:22:22 -08001804 if (bus->rxblen)
1805 buf = vzalloc(bus->rxblen);
Joe Perches14f8dc42013-02-07 11:46:27 +00001806 if (!buf)
Franky Lindd43a012012-11-05 16:22:22 -08001807 goto done;
Joe Perches14f8dc42013-02-07 11:46:27 +00001808
Franky Lindd43a012012-11-05 16:22:22 -08001809 rbuf = bus->rxbuf;
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01001810 pad = ((unsigned long)rbuf % bus->head_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001811 if (pad)
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01001812 rbuf += (bus->head_align - pad);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001813
1814 /* Copy the already-read portion over */
Franky Lindd43a012012-11-05 16:22:22 -08001815 memcpy(buf, hdr, BRCMF_FIRSTREAD);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001816 if (len <= BRCMF_FIRSTREAD)
1817 goto gotpkt;
1818
1819 /* Raise rdlen to next SDIO block to avoid tail command */
1820 rdlen = len - BRCMF_FIRSTREAD;
1821 if (bus->roundup && bus->blocksize && (rdlen > bus->blocksize)) {
1822 pad = bus->blocksize - (rdlen % bus->blocksize);
1823 if ((pad <= bus->roundup) && (pad < bus->blocksize) &&
Franky Linb01a6b32011-12-16 18:37:03 -08001824 ((len + pad) < bus->sdiodev->bus_if->maxctl))
Arend van Spriel5b435de2011-10-05 13:19:03 +02001825 rdlen += pad;
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01001826 } else if (rdlen % bus->head_align) {
1827 rdlen += bus->head_align - (rdlen % bus->head_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001828 }
1829
Arend van Spriel5b435de2011-10-05 13:19:03 +02001830 /* Drop if the read is too big or it exceeds our maximum */
Franky Linb01a6b32011-12-16 18:37:03 -08001831 if ((rdlen + BRCMF_FIRSTREAD) > bus->sdiodev->bus_if->maxctl) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001832 brcmf_err("%d-byte control read exceeds %d-byte buffer\n",
Franky Linb01a6b32011-12-16 18:37:03 -08001833 rdlen, bus->sdiodev->bus_if->maxctl);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001834 brcmf_sdio_rxfail(bus, false, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001835 goto done;
1836 }
1837
Franky Linb01a6b32011-12-16 18:37:03 -08001838 if ((len - doff) > bus->sdiodev->bus_if->maxctl) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001839 brcmf_err("%d-byte ctl frame (%d-byte ctl data) exceeds %d-byte limit\n",
Franky Linb01a6b32011-12-16 18:37:03 -08001840 len, len - doff, bus->sdiodev->bus_if->maxctl);
Arend van Spriel80969832012-06-09 22:51:44 +02001841 bus->sdcnt.rx_toolong++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001842 brcmf_sdio_rxfail(bus, false, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001843 goto done;
1844 }
1845
Franky Lindd43a012012-11-05 16:22:22 -08001846 /* Read remain of frame body */
Arend van Spriela7cdd822013-12-12 11:58:59 +01001847 sdret = brcmf_sdiod_recv_buf(bus->sdiodev, rbuf, rdlen);
Arend van Spriel80969832012-06-09 22:51:44 +02001848 bus->sdcnt.f2rxdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001849
1850 /* Control frame failures need retransmission */
1851 if (sdret < 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001852 brcmf_err("read %d control bytes failed: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001853 rdlen, sdret);
Arend van Spriel80969832012-06-09 22:51:44 +02001854 bus->sdcnt.rxc_errors++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001855 brcmf_sdio_rxfail(bus, true, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001856 goto done;
Franky Lindd43a012012-11-05 16:22:22 -08001857 } else
1858 memcpy(buf + BRCMF_FIRSTREAD, rbuf, rdlen);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001859
1860gotpkt:
1861
Joe Perches1e023822012-01-15 00:38:40 -08001862 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_CTL_ON(),
Franky Lindd43a012012-11-05 16:22:22 -08001863 buf, len, "RxCtrl:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001864
1865 /* Point to valid data and indicate its length */
Franky Lindd43a012012-11-05 16:22:22 -08001866 spin_lock_bh(&bus->rxctl_lock);
1867 if (bus->rxctl) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001868 brcmf_err("last control frame is being processed.\n");
Franky Lindd43a012012-11-05 16:22:22 -08001869 spin_unlock_bh(&bus->rxctl_lock);
1870 vfree(buf);
1871 goto done;
1872 }
1873 bus->rxctl = buf + doff;
1874 bus->rxctl_orig = buf;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001875 bus->rxlen = len - doff;
Franky Lindd43a012012-11-05 16:22:22 -08001876 spin_unlock_bh(&bus->rxctl_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001877
1878done:
1879 /* Awake any waiters */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001880 brcmf_sdio_dcmd_resp_wake(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001881}
1882
1883/* Pad read to blocksize for efficiency */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001884static void brcmf_sdio_pad(struct brcmf_sdio *bus, u16 *pad, u16 *rdlen)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001885{
1886 if (bus->roundup && bus->blocksize && *rdlen > bus->blocksize) {
1887 *pad = bus->blocksize - (*rdlen % bus->blocksize);
1888 if (*pad <= bus->roundup && *pad < bus->blocksize &&
1889 *rdlen + *pad + BRCMF_FIRSTREAD < MAX_RX_DATASZ)
1890 *rdlen += *pad;
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01001891 } else if (*rdlen % bus->head_align) {
1892 *rdlen += bus->head_align - (*rdlen % bus->head_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001893 }
1894}
1895
Franky Lin4754fce2012-09-19 22:21:11 +02001896static uint brcmf_sdio_readframes(struct brcmf_sdio *bus, uint maxframes)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001897{
Arend van Spriel5b435de2011-10-05 13:19:03 +02001898 struct sk_buff *pkt; /* Packet for event or data frames */
1899 u16 pad; /* Number of pad bytes to read */
Arend van Spriel5b435de2011-10-05 13:19:03 +02001900 uint rxleft = 0; /* Remaining number of frames allowed */
Arend van Spriel349e7102013-03-03 12:45:28 +01001901 int ret; /* Return code from calls */
Arend van Spriel5b435de2011-10-05 13:19:03 +02001902 uint rxcount = 0; /* Total frames read */
Franky Lin6bc52312013-08-10 12:27:28 +02001903 struct brcmf_sdio_hdrinfo *rd = &bus->cur_read, rd_new;
Franky Lin4754fce2012-09-19 22:21:11 +02001904 u8 head_read = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001905
1906 brcmf_dbg(TRACE, "Enter\n");
1907
1908 /* Not finished unless we encounter no more frames indication */
Franky Lin4754fce2012-09-19 22:21:11 +02001909 bus->rxpending = true;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001910
Franky Lin4754fce2012-09-19 22:21:11 +02001911 for (rd->seq_num = bus->rx_seq, rxleft = maxframes;
Arend van Spriela1ce7a02015-02-06 18:36:42 +01001912 !bus->rxskip && rxleft && bus->sdiodev->state == BRCMF_SDIOD_DATA;
Franky Lin4754fce2012-09-19 22:21:11 +02001913 rd->seq_num++, rxleft--) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001914
1915 /* Handle glomming separately */
Arend van Sprielb83db862011-10-19 12:51:09 +02001916 if (bus->glomd || !skb_queue_empty(&bus->glom)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001917 u8 cnt;
1918 brcmf_dbg(GLOM, "calling rxglom: glomd %p, glom %p\n",
Arend van Sprielb83db862011-10-19 12:51:09 +02001919 bus->glomd, skb_peek(&bus->glom));
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001920 cnt = brcmf_sdio_rxglom(bus, rd->seq_num);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001921 brcmf_dbg(GLOM, "rxglom returned %d\n", cnt);
Franky Lin4754fce2012-09-19 22:21:11 +02001922 rd->seq_num += cnt - 1;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001923 rxleft = (rxleft > cnt) ? (rxleft - cnt) : 1;
1924 continue;
1925 }
1926
Franky Lin4754fce2012-09-19 22:21:11 +02001927 rd->len_left = rd->len;
1928 /* read header first for unknow frame length */
Franky Lin38b0b0d2012-11-05 16:22:24 -08001929 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001930 if (!rd->len) {
Arend van Spriela39be272013-12-12 11:58:58 +01001931 ret = brcmf_sdiod_recv_buf(bus->sdiodev,
Arend van Spriela39be272013-12-12 11:58:58 +01001932 bus->rxhdr, BRCMF_FIRSTREAD);
Franky Lin4754fce2012-09-19 22:21:11 +02001933 bus->sdcnt.f2rxhdrs++;
Arend van Spriel349e7102013-03-03 12:45:28 +01001934 if (ret < 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001935 brcmf_err("RXHEADER FAILED: %d\n",
Arend van Spriel349e7102013-03-03 12:45:28 +01001936 ret);
Franky Lin4754fce2012-09-19 22:21:11 +02001937 bus->sdcnt.rx_hdrfail++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001938 brcmf_sdio_rxfail(bus, true, true);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001939 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001940 continue;
1941 }
1942
Franky Lin4754fce2012-09-19 22:21:11 +02001943 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() || BRCMF_HDRS_ON(),
1944 bus->rxhdr, SDPCM_HDRLEN,
1945 "RxHdr:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001946
Franky Lin6bc52312013-08-10 12:27:28 +02001947 if (brcmf_sdio_hdparse(bus, bus->rxhdr, rd,
1948 BRCMF_SDIO_FT_NORMAL)) {
Franky Lin38b0b0d2012-11-05 16:22:24 -08001949 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001950 if (!bus->rxpending)
1951 break;
1952 else
1953 continue;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001954 }
1955
Franky Lin4754fce2012-09-19 22:21:11 +02001956 if (rd->channel == SDPCM_CONTROL_CHANNEL) {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001957 brcmf_sdio_read_control(bus, bus->rxhdr,
1958 rd->len,
1959 rd->dat_offset);
Franky Lin4754fce2012-09-19 22:21:11 +02001960 /* prepare the descriptor for the next read */
1961 rd->len = rd->len_nxtfrm << 4;
1962 rd->len_nxtfrm = 0;
1963 /* treat all packet as event if we don't know */
1964 rd->channel = SDPCM_EVENT_CHANNEL;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001965 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001966 continue;
1967 }
1968 rd->len_left = rd->len > BRCMF_FIRSTREAD ?
1969 rd->len - BRCMF_FIRSTREAD : 0;
1970 head_read = BRCMF_FIRSTREAD;
1971 }
1972
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001973 brcmf_sdio_pad(bus, &pad, &rd->len_left);
Franky Lin4754fce2012-09-19 22:21:11 +02001974
1975 pkt = brcmu_pkt_buf_get_skb(rd->len_left + head_read +
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01001976 bus->head_align);
Franky Lin4754fce2012-09-19 22:21:11 +02001977 if (!pkt) {
1978 /* Give up on data, request rtx of events */
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001979 brcmf_err("brcmu_pkt_buf_get_skb failed\n");
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001980 brcmf_sdio_rxfail(bus, false,
Franky Lin4754fce2012-09-19 22:21:11 +02001981 RETRYCHAN(rd->channel));
Franky Lin38b0b0d2012-11-05 16:22:24 -08001982 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001983 continue;
1984 }
1985 skb_pull(pkt, head_read);
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01001986 pkt_align(pkt, rd->len_left, bus->head_align);
Franky Lin4754fce2012-09-19 22:21:11 +02001987
Arend van Spriela7cdd822013-12-12 11:58:59 +01001988 ret = brcmf_sdiod_recv_pkt(bus->sdiodev, pkt);
Franky Lin4754fce2012-09-19 22:21:11 +02001989 bus->sdcnt.f2rxdata++;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001990 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001991
Arend van Spriel349e7102013-03-03 12:45:28 +01001992 if (ret < 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001993 brcmf_err("read %d bytes from channel %d failed: %d\n",
Arend van Spriel349e7102013-03-03 12:45:28 +01001994 rd->len, rd->channel, ret);
Franky Lin4754fce2012-09-19 22:21:11 +02001995 brcmu_pkt_buf_free_skb(pkt);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001996 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001997 brcmf_sdio_rxfail(bus, true,
Franky Lin4754fce2012-09-19 22:21:11 +02001998 RETRYCHAN(rd->channel));
Franky Lin38b0b0d2012-11-05 16:22:24 -08001999 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02002000 continue;
2001 }
2002
2003 if (head_read) {
2004 skb_push(pkt, head_read);
2005 memcpy(pkt->data, bus->rxhdr, head_read);
2006 head_read = 0;
2007 } else {
2008 memcpy(bus->rxhdr, pkt->data, SDPCM_HDRLEN);
2009 rd_new.seq_num = rd->seq_num;
Franky Lin38b0b0d2012-11-05 16:22:24 -08002010 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin6bc52312013-08-10 12:27:28 +02002011 if (brcmf_sdio_hdparse(bus, bus->rxhdr, &rd_new,
2012 BRCMF_SDIO_FT_NORMAL)) {
Franky Lin4754fce2012-09-19 22:21:11 +02002013 rd->len = 0;
2014 brcmu_pkt_buf_free_skb(pkt);
2015 }
Arend van Spriel80969832012-06-09 22:51:44 +02002016 bus->sdcnt.rx_readahead_cnt++;
Franky Lin4754fce2012-09-19 22:21:11 +02002017 if (rd->len != roundup(rd_new.len, 16)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002018 brcmf_err("frame length mismatch:read %d, should be %d\n",
Franky Lin4754fce2012-09-19 22:21:11 +02002019 rd->len,
2020 roundup(rd_new.len, 16) >> 4);
2021 rd->len = 0;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002022 brcmf_sdio_rxfail(bus, true, true);
Franky Lin38b0b0d2012-11-05 16:22:24 -08002023 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02002024 brcmu_pkt_buf_free_skb(pkt);
2025 continue;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002026 }
Franky Lin38b0b0d2012-11-05 16:22:24 -08002027 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02002028 rd->len_nxtfrm = rd_new.len_nxtfrm;
2029 rd->channel = rd_new.channel;
2030 rd->dat_offset = rd_new.dat_offset;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002031
Joe Perches1e023822012-01-15 00:38:40 -08002032 brcmf_dbg_hex_dump(!(BRCMF_BYTES_ON() &&
2033 BRCMF_DATA_ON()) &&
2034 BRCMF_HDRS_ON(),
2035 bus->rxhdr, SDPCM_HDRLEN,
2036 "RxHdr:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002037
Franky Lin4754fce2012-09-19 22:21:11 +02002038 if (rd_new.channel == SDPCM_CONTROL_CHANNEL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002039 brcmf_err("readahead on control packet %d?\n",
Franky Lin4754fce2012-09-19 22:21:11 +02002040 rd_new.seq_num);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002041 /* Force retry w/normal header read */
Franky Lin4754fce2012-09-19 22:21:11 +02002042 rd->len = 0;
Franky Lin38b0b0d2012-11-05 16:22:24 -08002043 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002044 brcmf_sdio_rxfail(bus, false, true);
Franky Lin38b0b0d2012-11-05 16:22:24 -08002045 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02002046 brcmu_pkt_buf_free_skb(pkt);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002047 continue;
2048 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002049 }
2050
Joe Perches1e023822012-01-15 00:38:40 -08002051 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_DATA_ON(),
Franky Lin4754fce2012-09-19 22:21:11 +02002052 pkt->data, rd->len, "Rx Data:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002053
Arend van Spriel5b435de2011-10-05 13:19:03 +02002054 /* Save superframe descriptor and allocate packet frame */
Franky Lin4754fce2012-09-19 22:21:11 +02002055 if (rd->channel == SDPCM_GLOM_CHANNEL) {
Franky Lin6bc52312013-08-10 12:27:28 +02002056 if (SDPCM_GLOMDESC(&bus->rxhdr[SDPCM_HWHDR_LEN])) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02002057 brcmf_dbg(GLOM, "glom descriptor, %d bytes:\n",
Franky Lin4754fce2012-09-19 22:21:11 +02002058 rd->len);
Joe Perches1e023822012-01-15 00:38:40 -08002059 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
Franky Lin4754fce2012-09-19 22:21:11 +02002060 pkt->data, rd->len,
Joe Perches1e023822012-01-15 00:38:40 -08002061 "Glom Data:\n");
Franky Lin4754fce2012-09-19 22:21:11 +02002062 __skb_trim(pkt, rd->len);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002063 skb_pull(pkt, SDPCM_HDRLEN);
2064 bus->glomd = pkt;
2065 } else {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002066 brcmf_err("%s: glom superframe w/o "
Arend van Spriel5b435de2011-10-05 13:19:03 +02002067 "descriptor!\n", __func__);
Franky Lin38b0b0d2012-11-05 16:22:24 -08002068 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002069 brcmf_sdio_rxfail(bus, false, false);
Franky Lin38b0b0d2012-11-05 16:22:24 -08002070 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002071 }
Franky Lin4754fce2012-09-19 22:21:11 +02002072 /* prepare the descriptor for the next read */
2073 rd->len = rd->len_nxtfrm << 4;
2074 rd->len_nxtfrm = 0;
2075 /* treat all packet as event if we don't know */
2076 rd->channel = SDPCM_EVENT_CHANNEL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002077 continue;
2078 }
2079
2080 /* Fill in packet len and prio, deliver upward */
Franky Lin4754fce2012-09-19 22:21:11 +02002081 __skb_trim(pkt, rd->len);
2082 skb_pull(pkt, rd->dat_offset);
2083
2084 /* prepare the descriptor for the next read */
2085 rd->len = rd->len_nxtfrm << 4;
2086 rd->len_nxtfrm = 0;
2087 /* treat all packet as event if we don't know */
2088 rd->channel = SDPCM_EVENT_CHANNEL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002089
2090 if (pkt->len == 0) {
2091 brcmu_pkt_buf_free_skb(pkt);
2092 continue;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002093 }
2094
Arend van Spriel05f38202013-09-25 13:05:46 +02002095 brcmf_rx_frame(bus->sdiodev->dev, pkt);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002096 }
Franky Lin4754fce2012-09-19 22:21:11 +02002097
Arend van Spriel5b435de2011-10-05 13:19:03 +02002098 rxcount = maxframes - rxleft;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002099 /* Message if we hit the limit */
2100 if (!rxleft)
Franky Lin4754fce2012-09-19 22:21:11 +02002101 brcmf_dbg(DATA, "hit rx limit of %d frames\n", maxframes);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002102 else
Arend van Spriel5b435de2011-10-05 13:19:03 +02002103 brcmf_dbg(DATA, "processed %d frames\n", rxcount);
2104 /* Back off rxseq if awaiting rtx, update rx_seq */
2105 if (bus->rxskip)
Franky Lin4754fce2012-09-19 22:21:11 +02002106 rd->seq_num--;
2107 bus->rx_seq = rd->seq_num;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002108
2109 return rxcount;
2110}
2111
Arend van Spriel5b435de2011-10-05 13:19:03 +02002112static void
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002113brcmf_sdio_wait_event_wakeup(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002114{
2115 if (waitqueue_active(&bus->ctrl_wait))
2116 wake_up_interruptible(&bus->ctrl_wait);
2117 return;
2118}
2119
Franky Lin8da9d2c2013-11-29 12:25:10 +01002120static int brcmf_sdio_txpkt_hdalign(struct brcmf_sdio *bus, struct sk_buff *pkt)
2121{
Arend van Spriele217d1c2013-11-29 12:25:13 +01002122 u16 head_pad;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002123 u8 *dat_buf;
2124
Franky Lin8da9d2c2013-11-29 12:25:10 +01002125 dat_buf = (u8 *)(pkt->data);
2126
2127 /* Check head padding */
Arend van Spriele217d1c2013-11-29 12:25:13 +01002128 head_pad = ((unsigned long)dat_buf % bus->head_align);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002129 if (head_pad) {
2130 if (skb_headroom(pkt) < head_pad) {
2131 bus->sdiodev->bus_if->tx_realloc++;
2132 head_pad = 0;
2133 if (skb_cow(pkt, head_pad))
2134 return -ENOMEM;
2135 }
2136 skb_push(pkt, head_pad);
2137 dat_buf = (u8 *)(pkt->data);
2138 memset(dat_buf, 0, head_pad + bus->tx_hdrlen);
2139 }
2140 return head_pad;
2141}
2142
Franky Lin5491c112013-09-25 13:05:43 +02002143/**
2144 * struct brcmf_skbuff_cb reserves first two bytes in sk_buff::cb for
2145 * bus layer usage.
2146 */
Franky Linb05e9252013-08-10 12:27:26 +02002147/* flag marking a dummy skb added for DMA alignment requirement */
Franky Lin5491c112013-09-25 13:05:43 +02002148#define ALIGN_SKB_FLAG 0x8000
Franky Linb05e9252013-08-10 12:27:26 +02002149/* bit mask of data length chopped from the previous packet */
Franky Lin5491c112013-09-25 13:05:43 +02002150#define ALIGN_SKB_CHOP_LEN_MASK 0x7fff
2151
Franky Lin8da9d2c2013-11-29 12:25:10 +01002152static int brcmf_sdio_txpkt_prep_sg(struct brcmf_sdio *bus,
Arend van Spriela64304f2013-10-15 15:44:56 +02002153 struct sk_buff_head *pktq,
Franky Lin8da9d2c2013-11-29 12:25:10 +01002154 struct sk_buff *pkt, u16 total_len)
Arend van Spriela64304f2013-10-15 15:44:56 +02002155{
Franky Lin8da9d2c2013-11-29 12:25:10 +01002156 struct brcmf_sdio_dev *sdiodev;
Arend van Spriela64304f2013-10-15 15:44:56 +02002157 struct sk_buff *pkt_pad;
Arend van Spriele217d1c2013-11-29 12:25:13 +01002158 u16 tail_pad, tail_chop, chain_pad;
Arend van Spriela64304f2013-10-15 15:44:56 +02002159 unsigned int blksize;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002160 bool lastfrm;
2161 int ntail, ret;
Arend van Spriela64304f2013-10-15 15:44:56 +02002162
Franky Lin8da9d2c2013-11-29 12:25:10 +01002163 sdiodev = bus->sdiodev;
Arend van Spriela64304f2013-10-15 15:44:56 +02002164 blksize = sdiodev->func[SDIO_FUNC_2]->cur_blksize;
Arend van Spriela64304f2013-10-15 15:44:56 +02002165 /* sg entry alignment should be a divisor of block size */
Arend van Spriele217d1c2013-11-29 12:25:13 +01002166 WARN_ON(blksize % bus->sgentry_align);
Arend van Spriela64304f2013-10-15 15:44:56 +02002167
2168 /* Check tail padding */
Franky Lin8da9d2c2013-11-29 12:25:10 +01002169 lastfrm = skb_queue_is_last(pktq, pkt);
2170 tail_pad = 0;
Arend van Spriele217d1c2013-11-29 12:25:13 +01002171 tail_chop = pkt->len % bus->sgentry_align;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002172 if (tail_chop)
Arend van Spriele217d1c2013-11-29 12:25:13 +01002173 tail_pad = bus->sgentry_align - tail_chop;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002174 chain_pad = (total_len + tail_pad) % blksize;
2175 if (lastfrm && chain_pad)
2176 tail_pad += blksize - chain_pad;
Arend van Spriela64304f2013-10-15 15:44:56 +02002177 if (skb_tailroom(pkt) < tail_pad && pkt->len > blksize) {
Arend van Spriel1eb43012014-02-20 18:55:55 +01002178 pkt_pad = brcmu_pkt_buf_get_skb(tail_pad + tail_chop +
2179 bus->head_align);
Arend van Spriela64304f2013-10-15 15:44:56 +02002180 if (pkt_pad == NULL)
2181 return -ENOMEM;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002182 ret = brcmf_sdio_txpkt_hdalign(bus, pkt_pad);
Dave Jones2dc3a8e2014-03-06 15:51:08 -05002183 if (unlikely(ret < 0)) {
2184 kfree_skb(pkt_pad);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002185 return ret;
Dave Jones2dc3a8e2014-03-06 15:51:08 -05002186 }
Arend van Spriela64304f2013-10-15 15:44:56 +02002187 memcpy(pkt_pad->data,
2188 pkt->data + pkt->len - tail_chop,
2189 tail_chop);
Arend van Spriel5aa9f0e2014-02-25 20:30:27 +01002190 *(u16 *)(pkt_pad->cb) = ALIGN_SKB_FLAG + tail_chop;
Arend van Spriela64304f2013-10-15 15:44:56 +02002191 skb_trim(pkt, pkt->len - tail_chop);
Arend van Spriel1eb43012014-02-20 18:55:55 +01002192 skb_trim(pkt_pad, tail_pad + tail_chop);
Arend van Spriela64304f2013-10-15 15:44:56 +02002193 __skb_queue_after(pktq, pkt, pkt_pad);
2194 } else {
2195 ntail = pkt->data_len + tail_pad -
2196 (pkt->end - pkt->tail);
2197 if (skb_cloned(pkt) || ntail > 0)
2198 if (pskb_expand_head(pkt, 0, ntail, GFP_ATOMIC))
2199 return -ENOMEM;
2200 if (skb_linearize(pkt))
2201 return -ENOMEM;
Arend van Spriela64304f2013-10-15 15:44:56 +02002202 __skb_put(pkt, tail_pad);
2203 }
2204
Franky Lin8da9d2c2013-11-29 12:25:10 +01002205 return tail_pad;
Arend van Spriela64304f2013-10-15 15:44:56 +02002206}
2207
Franky Linb05e9252013-08-10 12:27:26 +02002208/**
2209 * brcmf_sdio_txpkt_prep - packet preparation for transmit
2210 * @bus: brcmf_sdio structure pointer
2211 * @pktq: packet list pointer
2212 * @chan: virtual channel to transmit the packet
2213 *
2214 * Processes to be applied to the packet
2215 * - Align data buffer pointer
2216 * - Align data buffer length
2217 * - Prepare header
2218 * Return: negative value if there is error
2219 */
2220static int
2221brcmf_sdio_txpkt_prep(struct brcmf_sdio *bus, struct sk_buff_head *pktq,
2222 uint chan)
2223{
Franky Lin8da9d2c2013-11-29 12:25:10 +01002224 u16 head_pad, total_len;
Arend van Spriela64304f2013-10-15 15:44:56 +02002225 struct sk_buff *pkt_next;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002226 u8 txseq;
2227 int ret;
Franky Lin6bc52312013-08-10 12:27:28 +02002228 struct brcmf_sdio_hdrinfo hd_info = {0};
Franky Linb05e9252013-08-10 12:27:26 +02002229
Franky Lin8da9d2c2013-11-29 12:25:10 +01002230 txseq = bus->tx_seq;
2231 total_len = 0;
2232 skb_queue_walk(pktq, pkt_next) {
2233 /* alignment packet inserted in previous
2234 * loop cycle can be skipped as it is
2235 * already properly aligned and does not
2236 * need an sdpcm header.
2237 */
Arend van Spriel5aa9f0e2014-02-25 20:30:27 +01002238 if (*(u16 *)(pkt_next->cb) & ALIGN_SKB_FLAG)
Franky Lin8da9d2c2013-11-29 12:25:10 +01002239 continue;
Franky Linb05e9252013-08-10 12:27:26 +02002240
Franky Lin8da9d2c2013-11-29 12:25:10 +01002241 /* align packet data pointer */
2242 ret = brcmf_sdio_txpkt_hdalign(bus, pkt_next);
2243 if (ret < 0)
2244 return ret;
2245 head_pad = (u16)ret;
2246 if (head_pad)
Arend van Spriel1eb43012014-02-20 18:55:55 +01002247 memset(pkt_next->data + bus->tx_hdrlen, 0, head_pad);
Franky Linb05e9252013-08-10 12:27:26 +02002248
Franky Lin8da9d2c2013-11-29 12:25:10 +01002249 total_len += pkt_next->len;
Franky Linb05e9252013-08-10 12:27:26 +02002250
Arend van Spriela64304f2013-10-15 15:44:56 +02002251 hd_info.len = pkt_next->len;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002252 hd_info.lastfrm = skb_queue_is_last(pktq, pkt_next);
2253 if (bus->txglom && pktq->qlen > 1) {
2254 ret = brcmf_sdio_txpkt_prep_sg(bus, pktq,
2255 pkt_next, total_len);
2256 if (ret < 0)
2257 return ret;
2258 hd_info.tail_pad = (u16)ret;
2259 total_len += (u16)ret;
2260 }
2261
2262 hd_info.channel = chan;
2263 hd_info.dat_offset = head_pad + bus->tx_hdrlen;
2264 hd_info.seq_num = txseq++;
2265
2266 /* Now fill the header */
2267 brcmf_sdio_hdpack(bus, pkt_next->data, &hd_info);
2268
2269 if (BRCMF_BYTES_ON() &&
2270 ((BRCMF_CTL_ON() && chan == SDPCM_CONTROL_CHANNEL) ||
2271 (BRCMF_DATA_ON() && chan != SDPCM_CONTROL_CHANNEL)))
Hante Meuleman47ab4cd2014-02-25 20:30:29 +01002272 brcmf_dbg_hex_dump(true, pkt_next->data, hd_info.len,
Franky Lin8da9d2c2013-11-29 12:25:10 +01002273 "Tx Frame:\n");
2274 else if (BRCMF_HDRS_ON())
Hante Meuleman47ab4cd2014-02-25 20:30:29 +01002275 brcmf_dbg_hex_dump(true, pkt_next->data,
Franky Lin8da9d2c2013-11-29 12:25:10 +01002276 head_pad + bus->tx_hdrlen,
2277 "Tx Header:\n");
Franky Linb05e9252013-08-10 12:27:26 +02002278 }
Franky Lin8da9d2c2013-11-29 12:25:10 +01002279 /* Hardware length tag of the first packet should be total
2280 * length of the chain (including padding)
2281 */
2282 if (bus->txglom)
2283 brcmf_sdio_update_hwhdr(pktq->next->data, total_len);
Franky Linb05e9252013-08-10 12:27:26 +02002284 return 0;
2285}
2286
2287/**
2288 * brcmf_sdio_txpkt_postp - packet post processing for transmit
2289 * @bus: brcmf_sdio structure pointer
2290 * @pktq: packet list pointer
2291 *
2292 * Processes to be applied to the packet
2293 * - Remove head padding
2294 * - Remove tail padding
2295 */
2296static void
2297brcmf_sdio_txpkt_postp(struct brcmf_sdio *bus, struct sk_buff_head *pktq)
2298{
2299 u8 *hdr;
2300 u32 dat_offset;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002301 u16 tail_pad;
Arend van Spriel5aa9f0e2014-02-25 20:30:27 +01002302 u16 dummy_flags, chop_len;
Franky Linb05e9252013-08-10 12:27:26 +02002303 struct sk_buff *pkt_next, *tmp, *pkt_prev;
2304
2305 skb_queue_walk_safe(pktq, pkt_next, tmp) {
Arend van Spriel5aa9f0e2014-02-25 20:30:27 +01002306 dummy_flags = *(u16 *)(pkt_next->cb);
Franky Lin5491c112013-09-25 13:05:43 +02002307 if (dummy_flags & ALIGN_SKB_FLAG) {
2308 chop_len = dummy_flags & ALIGN_SKB_CHOP_LEN_MASK;
Franky Linb05e9252013-08-10 12:27:26 +02002309 if (chop_len) {
2310 pkt_prev = pkt_next->prev;
Franky Linb05e9252013-08-10 12:27:26 +02002311 skb_put(pkt_prev, chop_len);
2312 }
2313 __skb_unlink(pkt_next, pktq);
2314 brcmu_pkt_buf_free_skb(pkt_next);
2315 } else {
Franky Lin8da9d2c2013-11-29 12:25:10 +01002316 hdr = pkt_next->data + bus->tx_hdrlen - SDPCM_SWHDR_LEN;
Franky Linb05e9252013-08-10 12:27:26 +02002317 dat_offset = le32_to_cpu(*(__le32 *)hdr);
2318 dat_offset = (dat_offset & SDPCM_DOFFSET_MASK) >>
2319 SDPCM_DOFFSET_SHIFT;
2320 skb_pull(pkt_next, dat_offset);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002321 if (bus->txglom) {
2322 tail_pad = le16_to_cpu(*(__le16 *)(hdr - 2));
2323 skb_trim(pkt_next, pkt_next->len - tail_pad);
2324 }
Franky Linb05e9252013-08-10 12:27:26 +02002325 }
2326 }
2327}
2328
Arend van Spriel5b435de2011-10-05 13:19:03 +02002329/* Writes a HW/SW header into the packet and sends it. */
2330/* Assumes: (a) header space already there, (b) caller holds lock */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002331static int brcmf_sdio_txpkt(struct brcmf_sdio *bus, struct sk_buff_head *pktq,
2332 uint chan)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002333{
2334 int ret;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002335 struct sk_buff *pkt_next, *tmp;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002336
2337 brcmf_dbg(TRACE, "Enter\n");
2338
Franky Lin8da9d2c2013-11-29 12:25:10 +01002339 ret = brcmf_sdio_txpkt_prep(bus, pktq, chan);
Franky Linb05e9252013-08-10 12:27:26 +02002340 if (ret)
2341 goto done;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002342
Franky Lin38b0b0d2012-11-05 16:22:24 -08002343 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriela7cdd822013-12-12 11:58:59 +01002344 ret = brcmf_sdiod_send_pkt(bus->sdiodev, pktq);
Arend van Spriel80969832012-06-09 22:51:44 +02002345 bus->sdcnt.f2txdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002346
Hante Meuleman81c78832014-02-25 20:30:35 +01002347 if (ret < 0)
2348 brcmf_sdio_txfail(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002349
Franky Lin38b0b0d2012-11-05 16:22:24 -08002350 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002351
2352done:
Franky Lin8da9d2c2013-11-29 12:25:10 +01002353 brcmf_sdio_txpkt_postp(bus, pktq);
2354 if (ret == 0)
2355 bus->tx_seq = (bus->tx_seq + pktq->qlen) % SDPCM_SEQ_WRAP;
2356 skb_queue_walk_safe(pktq, pkt_next, tmp) {
2357 __skb_unlink(pkt_next, pktq);
2358 brcmf_txcomplete(bus->sdiodev->dev, pkt_next, ret == 0);
2359 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002360 return ret;
2361}
2362
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002363static uint brcmf_sdio_sendfromq(struct brcmf_sdio *bus, uint maxframes)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002364{
2365 struct sk_buff *pkt;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002366 struct sk_buff_head pktq;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002367 u32 intstatus = 0;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002368 int ret = 0, prec_out, i;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002369 uint cnt = 0;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002370 u8 tx_prec_map, pkt_num;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002371
Arend van Spriel5b435de2011-10-05 13:19:03 +02002372 brcmf_dbg(TRACE, "Enter\n");
2373
2374 tx_prec_map = ~bus->flowcontrol;
2375
2376 /* Send frames until the limit or some other event */
Franky Lin8da9d2c2013-11-29 12:25:10 +01002377 for (cnt = 0; (cnt < maxframes) && data_ok(bus);) {
2378 pkt_num = 1;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002379 if (bus->txglom)
2380 pkt_num = min_t(u8, bus->tx_max - bus->tx_seq,
Arend van Sprielaf1fa212014-02-27 19:25:00 +01002381 bus->sdiodev->txglomsz);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002382 pkt_num = min_t(u32, pkt_num,
2383 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol));
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002384 __skb_queue_head_init(&pktq);
2385 spin_lock_bh(&bus->txq_lock);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002386 for (i = 0; i < pkt_num; i++) {
2387 pkt = brcmu_pktq_mdeq(&bus->txq, tx_prec_map,
2388 &prec_out);
2389 if (pkt == NULL)
2390 break;
2391 __skb_queue_tail(&pktq, pkt);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002392 }
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002393 spin_unlock_bh(&bus->txq_lock);
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002394 if (i == 0)
Franky Lin8da9d2c2013-11-29 12:25:10 +01002395 break;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002396
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002397 ret = brcmf_sdio_txpkt(bus, &pktq, SDPCM_DATA_CHANNEL);
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002398
Franky Lin8da9d2c2013-11-29 12:25:10 +01002399 cnt += i;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002400
2401 /* In poll mode, need to check for other events */
Hante Meulemanb6a8cf22014-02-25 20:30:33 +01002402 if (!bus->intr) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02002403 /* Check device status, signal pending interrupt */
Franky Lin38b0b0d2012-11-05 16:22:24 -08002404 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin5c15c232012-05-04 18:27:37 -07002405 ret = r_sdreg32(bus, &intstatus,
2406 offsetof(struct sdpcmd_regs,
2407 intstatus));
Franky Lin38b0b0d2012-11-05 16:22:24 -08002408 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel80969832012-06-09 22:51:44 +02002409 bus->sdcnt.f2txdata++;
Franky Lin5c15c232012-05-04 18:27:37 -07002410 if (ret != 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002411 break;
2412 if (intstatus & bus->hostintmask)
Franky Lin1d382272012-09-13 21:11:59 +02002413 atomic_set(&bus->ipend, 1);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002414 }
2415 }
2416
2417 /* Deflow-control stack if needed */
Arend van Spriela1ce7a02015-02-06 18:36:42 +01002418 if ((bus->sdiodev->state == BRCMF_SDIOD_DATA) &&
Franky Linc8bf3482011-12-16 18:37:08 -08002419 bus->txoff && (pktq_len(&bus->txq) < TXLOW)) {
Hante Meuleman90d03ff2012-09-11 21:18:46 +02002420 bus->txoff = false;
2421 brcmf_txflowblock(bus->sdiodev->dev, false);
Franky Linc8bf3482011-12-16 18:37:08 -08002422 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002423
2424 return cnt;
2425}
2426
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002427static int brcmf_sdio_tx_ctrlframe(struct brcmf_sdio *bus, u8 *frame, u16 len)
2428{
2429 u8 doff;
2430 u16 pad;
2431 uint retries = 0;
2432 struct brcmf_sdio_hdrinfo hd_info = {0};
2433 int ret;
2434
2435 brcmf_dbg(TRACE, "Enter\n");
2436
2437 /* Back the pointer to make room for bus header */
2438 frame -= bus->tx_hdrlen;
2439 len += bus->tx_hdrlen;
2440
2441 /* Add alignment padding (optional for ctl frames) */
2442 doff = ((unsigned long)frame % bus->head_align);
2443 if (doff) {
2444 frame -= doff;
2445 len += doff;
2446 memset(frame + bus->tx_hdrlen, 0, doff);
2447 }
2448
2449 /* Round send length to next SDIO block */
2450 pad = 0;
2451 if (bus->roundup && bus->blocksize && (len > bus->blocksize)) {
2452 pad = bus->blocksize - (len % bus->blocksize);
2453 if ((pad > bus->roundup) || (pad >= bus->blocksize))
2454 pad = 0;
2455 } else if (len % bus->head_align) {
2456 pad = bus->head_align - (len % bus->head_align);
2457 }
2458 len += pad;
2459
2460 hd_info.len = len - pad;
2461 hd_info.channel = SDPCM_CONTROL_CHANNEL;
2462 hd_info.dat_offset = doff + bus->tx_hdrlen;
2463 hd_info.seq_num = bus->tx_seq;
2464 hd_info.lastfrm = true;
2465 hd_info.tail_pad = pad;
2466 brcmf_sdio_hdpack(bus, frame, &hd_info);
2467
2468 if (bus->txglom)
2469 brcmf_sdio_update_hwhdr(frame, len);
2470
2471 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_CTL_ON(),
2472 frame, len, "Tx Frame:\n");
2473 brcmf_dbg_hex_dump(!(BRCMF_BYTES_ON() && BRCMF_CTL_ON()) &&
2474 BRCMF_HDRS_ON(),
2475 frame, min_t(u16, len, 16), "TxHdr:\n");
2476
2477 do {
2478 ret = brcmf_sdiod_send_buf(bus->sdiodev, frame, len);
2479
2480 if (ret < 0)
2481 brcmf_sdio_txfail(bus);
2482 else
2483 bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQ_WRAP;
2484 } while (ret < 0 && retries++ < TXRETRIES);
2485
2486 return ret;
2487}
2488
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002489static void brcmf_sdio_bus_stop(struct device *dev)
Franky Lina9ffda82011-12-16 18:37:12 -08002490{
2491 u32 local_hostintmask;
2492 u8 saveclk;
Franky Lina9ffda82011-12-16 18:37:12 -08002493 int err;
2494 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01002495 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Lina9ffda82011-12-16 18:37:12 -08002496 struct brcmf_sdio *bus = sdiodev->bus;
2497
2498 brcmf_dbg(TRACE, "Enter\n");
2499
2500 if (bus->watchdog_tsk) {
2501 send_sig(SIGTERM, bus->watchdog_tsk, 1);
2502 kthread_stop(bus->watchdog_tsk);
2503 bus->watchdog_tsk = NULL;
2504 }
2505
Arend van Spriela1ce7a02015-02-06 18:36:42 +01002506 if (sdiodev->state != BRCMF_SDIOD_NOMEDIUM) {
Arend van Sprielbb350712014-01-13 22:20:29 +01002507 sdio_claim_host(sdiodev->func[1]);
Franky Lina9ffda82011-12-16 18:37:12 -08002508
Arend van Sprielbb350712014-01-13 22:20:29 +01002509 /* Enable clock for device interrupts */
2510 brcmf_sdio_bus_sleep(bus, false, false);
Franky Lina9ffda82011-12-16 18:37:12 -08002511
Arend van Sprielbb350712014-01-13 22:20:29 +01002512 /* Disable and clear interrupts at the chip level also */
2513 w_sdreg32(bus, 0, offsetof(struct sdpcmd_regs, hostintmask));
2514 local_hostintmask = bus->hostintmask;
2515 bus->hostintmask = 0;
Franky Lina9ffda82011-12-16 18:37:12 -08002516
Arend van Sprielbb350712014-01-13 22:20:29 +01002517 /* Force backplane clocks to assure F2 interrupt propagates */
2518 saveclk = brcmf_sdiod_regrb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
2519 &err);
2520 if (!err)
2521 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
2522 (saveclk | SBSDIO_FORCE_HT), &err);
2523 if (err)
2524 brcmf_err("Failed to force clock for F2: err %d\n",
2525 err);
Franky Lina9ffda82011-12-16 18:37:12 -08002526
Arend van Sprielbb350712014-01-13 22:20:29 +01002527 /* Turn off the bus (F2), free any pending packets */
2528 brcmf_dbg(INTR, "disable SDIO interrupts\n");
2529 sdio_disable_func(sdiodev->func[SDIO_FUNC_2]);
2530
2531 /* Clear any pending interrupts now that F2 is disabled */
2532 w_sdreg32(bus, local_hostintmask,
2533 offsetof(struct sdpcmd_regs, intstatus));
2534
2535 sdio_release_host(sdiodev->func[1]);
Franky Lina9ffda82011-12-16 18:37:12 -08002536 }
Franky Lina9ffda82011-12-16 18:37:12 -08002537 /* Clear the data packet queues */
2538 brcmu_pktq_flush(&bus->txq, true, NULL, NULL);
2539
2540 /* Clear any held glomming stuff */
Markus Elfring297540f2015-02-04 20:28:49 +01002541 brcmu_pkt_buf_free_skb(bus->glomd);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002542 brcmf_sdio_free_glom(bus);
Franky Lina9ffda82011-12-16 18:37:12 -08002543
2544 /* Clear rx control and wake any waiters */
Franky Lindd43a012012-11-05 16:22:22 -08002545 spin_lock_bh(&bus->rxctl_lock);
Franky Lina9ffda82011-12-16 18:37:12 -08002546 bus->rxlen = 0;
Franky Lindd43a012012-11-05 16:22:22 -08002547 spin_unlock_bh(&bus->rxctl_lock);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002548 brcmf_sdio_dcmd_resp_wake(bus);
Franky Lina9ffda82011-12-16 18:37:12 -08002549
2550 /* Reset some F2 state stuff */
2551 bus->rxskip = false;
2552 bus->tx_seq = bus->rx_seq = 0;
Franky Lina9ffda82011-12-16 18:37:12 -08002553}
2554
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002555static inline void brcmf_sdio_clrintr(struct brcmf_sdio *bus)
Franky Linba89bf12012-04-27 18:56:59 -07002556{
2557 unsigned long flags;
2558
Hante Meuleman668761a2013-04-12 10:55:55 +02002559 if (bus->sdiodev->oob_irq_requested) {
2560 spin_lock_irqsave(&bus->sdiodev->irq_en_lock, flags);
2561 if (!bus->sdiodev->irq_en && !atomic_read(&bus->ipend)) {
2562 enable_irq(bus->sdiodev->pdata->oob_irq_nr);
2563 bus->sdiodev->irq_en = true;
2564 }
2565 spin_unlock_irqrestore(&bus->sdiodev->irq_en_lock, flags);
Franky Linba89bf12012-04-27 18:56:59 -07002566 }
Franky Linba89bf12012-04-27 18:56:59 -07002567}
Franky Linba89bf12012-04-27 18:56:59 -07002568
Franky Lin45316032012-09-13 21:12:03 +02002569static int brcmf_sdio_intr_rstatus(struct brcmf_sdio *bus)
2570{
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01002571 struct brcmf_core *buscore;
Franky Lin45316032012-09-13 21:12:03 +02002572 u32 addr;
2573 unsigned long val;
Hante Meuleman5cbb9c22014-03-06 10:16:12 +01002574 int ret;
Franky Lin45316032012-09-13 21:12:03 +02002575
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01002576 buscore = brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV);
2577 addr = buscore->base + offsetof(struct sdpcmd_regs, intstatus);
Franky Lin45316032012-09-13 21:12:03 +02002578
Arend van Spriela39be272013-12-12 11:58:58 +01002579 val = brcmf_sdiod_regrl(bus->sdiodev, addr, &ret);
Franky Lin45316032012-09-13 21:12:03 +02002580 bus->sdcnt.f1regdata++;
2581 if (ret != 0)
Hante Meuleman5cbb9c22014-03-06 10:16:12 +01002582 return ret;
Franky Lin45316032012-09-13 21:12:03 +02002583
2584 val &= bus->hostintmask;
2585 atomic_set(&bus->fcstate, !!(val & I_HMB_FC_STATE));
2586
2587 /* Clear interrupts */
2588 if (val) {
Arend van Spriela39be272013-12-12 11:58:58 +01002589 brcmf_sdiod_regwl(bus->sdiodev, addr, val, &ret);
Franky Lin45316032012-09-13 21:12:03 +02002590 bus->sdcnt.f1regdata++;
Vineet Guptad3928d02015-07-09 13:43:18 +05302591 atomic_or(val, &bus->intstatus);
Franky Lin45316032012-09-13 21:12:03 +02002592 }
2593
2594 return ret;
2595}
2596
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002597static void brcmf_sdio_dpc(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002598{
Franky Lin45316032012-09-13 21:12:03 +02002599 u32 newstatus = 0;
2600 unsigned long intstatus;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002601 uint txlimit = bus->txbound; /* Tx frames to send before resched */
Hante Meulemanb6a8cf22014-02-25 20:30:33 +01002602 uint framecnt; /* Temporary counter of tx/rx frames */
Hante Meuleman5cbb9c22014-03-06 10:16:12 +01002603 int err = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002604
2605 brcmf_dbg(TRACE, "Enter\n");
2606
Franky Lin38b0b0d2012-11-05 16:22:24 -08002607 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002608
2609 /* If waiting for HTAVAIL, check status */
Piotr Haber4a3da992013-04-11 13:28:46 +02002610 if (!bus->sr_enabled && bus->clkstate == CLK_PENDING) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02002611 u8 clkctl, devctl = 0;
2612
Joe Perches8ae74652012-01-15 00:38:38 -08002613#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02002614 /* Check for inconsistent device control */
Arend van Spriela39be272013-12-12 11:58:58 +01002615 devctl = brcmf_sdiod_regrb(bus->sdiodev,
2616 SBSDIO_DEVICE_CTL, &err);
Joe Perches8ae74652012-01-15 00:38:38 -08002617#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02002618
2619 /* Read CSR, if clock on switch to AVAIL, else ignore */
Arend van Spriela39be272013-12-12 11:58:58 +01002620 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
2621 SBSDIO_FUNC1_CHIPCLKCSR, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002622
Arend van Sprielc3203372013-04-03 12:40:44 +02002623 brcmf_dbg(SDIO, "DPC: PENDING, devctl 0x%02x clkctl 0x%02x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02002624 devctl, clkctl);
2625
2626 if (SBSDIO_HTAV(clkctl)) {
Arend van Spriela39be272013-12-12 11:58:58 +01002627 devctl = brcmf_sdiod_regrb(bus->sdiodev,
2628 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002629 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
Arend van Spriela39be272013-12-12 11:58:58 +01002630 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
2631 devctl, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002632 bus->clkstate = CLK_AVAIL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002633 }
2634 }
2635
Arend van Spriel5b435de2011-10-05 13:19:03 +02002636 /* Make sure backplane clock is on */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002637 brcmf_sdio_bus_sleep(bus, false, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002638
2639 /* Pending interrupt indicates new device status */
Franky Lin1d382272012-09-13 21:11:59 +02002640 if (atomic_read(&bus->ipend) > 0) {
2641 atomic_set(&bus->ipend, 0);
Franky Lin45316032012-09-13 21:12:03 +02002642 err = brcmf_sdio_intr_rstatus(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002643 }
2644
Franky Lin45316032012-09-13 21:12:03 +02002645 /* Start with leftover status bits */
2646 intstatus = atomic_xchg(&bus->intstatus, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002647
2648 /* Handle flow-control change: read new state in case our ack
2649 * crossed another change interrupt. If change still set, assume
2650 * FC ON for safety, let next loop through do the debounce.
2651 */
2652 if (intstatus & I_HMB_FC_CHANGE) {
2653 intstatus &= ~I_HMB_FC_CHANGE;
Franky Lin5c15c232012-05-04 18:27:37 -07002654 err = w_sdreg32(bus, I_HMB_FC_CHANGE,
2655 offsetof(struct sdpcmd_regs, intstatus));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002656
Franky Lin5c15c232012-05-04 18:27:37 -07002657 err = r_sdreg32(bus, &newstatus,
2658 offsetof(struct sdpcmd_regs, intstatus));
Arend van Spriel80969832012-06-09 22:51:44 +02002659 bus->sdcnt.f1regdata += 2;
Franky Lin45316032012-09-13 21:12:03 +02002660 atomic_set(&bus->fcstate,
2661 !!(newstatus & (I_HMB_FC_STATE | I_HMB_FC_CHANGE)));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002662 intstatus |= (newstatus & bus->hostintmask);
2663 }
2664
2665 /* Handle host mailbox indication */
2666 if (intstatus & I_HMB_HOST_INT) {
2667 intstatus &= ~I_HMB_HOST_INT;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002668 intstatus |= brcmf_sdio_hostmail(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002669 }
2670
Franky Lin38b0b0d2012-11-05 16:22:24 -08002671 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin7cdf57d2012-11-05 16:22:23 -08002672
Arend van Spriel5b435de2011-10-05 13:19:03 +02002673 /* Generally don't ask for these, can get CRC errors... */
2674 if (intstatus & I_WR_OOSYNC) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002675 brcmf_err("Dongle reports WR_OOSYNC\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002676 intstatus &= ~I_WR_OOSYNC;
2677 }
2678
2679 if (intstatus & I_RD_OOSYNC) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002680 brcmf_err("Dongle reports RD_OOSYNC\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002681 intstatus &= ~I_RD_OOSYNC;
2682 }
2683
2684 if (intstatus & I_SBINT) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002685 brcmf_err("Dongle reports SBINT\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002686 intstatus &= ~I_SBINT;
2687 }
2688
2689 /* Would be active due to wake-wlan in gSPI */
2690 if (intstatus & I_CHIPACTIVE) {
2691 brcmf_dbg(INFO, "Dongle reports CHIPACTIVE\n");
2692 intstatus &= ~I_CHIPACTIVE;
2693 }
2694
2695 /* Ignore frame indications if rxskip is set */
2696 if (bus->rxskip)
2697 intstatus &= ~I_HMB_FRAME_IND;
2698
2699 /* On frame indication, read available frames */
Hante Meulemanb6a8cf22014-02-25 20:30:33 +01002700 if ((intstatus & I_HMB_FRAME_IND) && (bus->clkstate == CLK_AVAIL)) {
2701 brcmf_sdio_readframes(bus, bus->rxbound);
Franky Lin4754fce2012-09-19 22:21:11 +02002702 if (!bus->rxpending)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002703 intstatus &= ~I_HMB_FRAME_IND;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002704 }
2705
2706 /* Keep still-pending events for next scheduling */
Hante Meuleman5cbb9c22014-03-06 10:16:12 +01002707 if (intstatus)
Vineet Guptad3928d02015-07-09 13:43:18 +05302708 atomic_or(intstatus, &bus->intstatus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002709
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002710 brcmf_sdio_clrintr(bus);
Franky Linba89bf12012-04-27 18:56:59 -07002711
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002712 if (bus->ctrl_frame_stat && (bus->clkstate == CLK_AVAIL) &&
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002713 data_ok(bus)) {
2714 sdio_claim_host(bus->sdiodev->func[1]);
Hante Meuleman449e58b2015-03-06 18:40:40 +01002715 if (bus->ctrl_frame_stat) {
2716 err = brcmf_sdio_tx_ctrlframe(bus, bus->ctrl_frame_buf,
2717 bus->ctrl_frame_len);
2718 bus->ctrl_frame_err = err;
Hante Meuleman2c64e162015-03-18 13:25:22 +01002719 wmb();
Hante Meuleman449e58b2015-03-06 18:40:40 +01002720 bus->ctrl_frame_stat = false;
2721 }
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002722 sdio_release_host(bus->sdiodev->func[1]);
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002723 brcmf_sdio_wait_event_wakeup(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002724 }
2725 /* Send queued frames (limit 1 if rx may still be pending) */
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002726 if ((bus->clkstate == CLK_AVAIL) && !atomic_read(&bus->fcstate) &&
2727 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) && txlimit &&
2728 data_ok(bus)) {
Franky Lin4754fce2012-09-19 22:21:11 +02002729 framecnt = bus->rxpending ? min(txlimit, bus->txminmax) :
2730 txlimit;
Hante Meulemanb6a8cf22014-02-25 20:30:33 +01002731 brcmf_sdio_sendfromq(bus, framecnt);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002732 }
2733
Arend van Spriela1ce7a02015-02-06 18:36:42 +01002734 if ((bus->sdiodev->state != BRCMF_SDIOD_DATA) || (err != 0)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002735 brcmf_err("failed backplane access over SDIO, halting operation\n");
Franky Lin45316032012-09-13 21:12:03 +02002736 atomic_set(&bus->intstatus, 0);
Hante Meulemande6878c2015-03-06 18:40:38 +01002737 if (bus->ctrl_frame_stat) {
Hante Meuleman449e58b2015-03-06 18:40:40 +01002738 sdio_claim_host(bus->sdiodev->func[1]);
2739 if (bus->ctrl_frame_stat) {
2740 bus->ctrl_frame_err = -ENODEV;
Hante Meuleman2c64e162015-03-18 13:25:22 +01002741 wmb();
Hante Meuleman449e58b2015-03-06 18:40:40 +01002742 bus->ctrl_frame_stat = false;
2743 brcmf_sdio_wait_event_wakeup(bus);
2744 }
2745 sdio_release_host(bus->sdiodev->func[1]);
Hante Meulemande6878c2015-03-06 18:40:38 +01002746 }
Franky Lin45316032012-09-13 21:12:03 +02002747 } else if (atomic_read(&bus->intstatus) ||
2748 atomic_read(&bus->ipend) > 0 ||
2749 (!atomic_read(&bus->fcstate) &&
2750 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) &&
Hante Meulemanb6a8cf22014-02-25 20:30:33 +01002751 data_ok(bus))) {
Hante Meuleman2c64e162015-03-18 13:25:22 +01002752 bus->dpc_triggered = true;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002753 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002754}
2755
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002756static struct pktq *brcmf_sdio_bus_gettxq(struct device *dev)
Arend van Spriele2432b62013-04-03 12:40:38 +02002757{
2758 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
2759 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
2760 struct brcmf_sdio *bus = sdiodev->bus;
2761
2762 return &bus->txq;
2763}
2764
Hante Meuleman84936622014-10-28 14:56:19 +01002765static bool brcmf_sdio_prec_enq(struct pktq *q, struct sk_buff *pkt, int prec)
2766{
2767 struct sk_buff *p;
2768 int eprec = -1; /* precedence to evict from */
2769
2770 /* Fast case, precedence queue is not full and we are also not
2771 * exceeding total queue length
2772 */
2773 if (!pktq_pfull(q, prec) && !pktq_full(q)) {
2774 brcmu_pktq_penq(q, prec, pkt);
2775 return true;
2776 }
2777
2778 /* Determine precedence from which to evict packet, if any */
2779 if (pktq_pfull(q, prec)) {
2780 eprec = prec;
2781 } else if (pktq_full(q)) {
2782 p = brcmu_pktq_peek_tail(q, &eprec);
2783 if (eprec > prec)
2784 return false;
2785 }
2786
2787 /* Evict if needed */
2788 if (eprec >= 0) {
2789 /* Detect queueing to unconfigured precedence */
2790 if (eprec == prec)
2791 return false; /* refuse newer (incoming) packet */
2792 /* Evict packet according to discard policy */
2793 p = brcmu_pktq_pdeq_tail(q, eprec);
2794 if (p == NULL)
2795 brcmf_err("brcmu_pktq_pdeq_tail() failed\n");
2796 brcmu_pkt_buf_free_skb(p);
2797 }
2798
2799 /* Enqueue */
2800 p = brcmu_pktq_penq(q, prec, pkt);
2801 if (p == NULL)
2802 brcmf_err("brcmu_pktq_penq() failed\n");
2803
2804 return p != NULL;
2805}
2806
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002807static int brcmf_sdio_bus_txdata(struct device *dev, struct sk_buff *pkt)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002808{
2809 int ret = -EBADE;
Arend van Spriel44ff5662014-02-25 20:30:28 +01002810 uint prec;
Franky Linbf347bb2011-11-22 17:21:56 -08002811 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01002812 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Linbf347bb2011-11-22 17:21:56 -08002813 struct brcmf_sdio *bus = sdiodev->bus;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002814
Arend van Spriel44ff5662014-02-25 20:30:28 +01002815 brcmf_dbg(TRACE, "Enter: pkt: data %p len %d\n", pkt->data, pkt->len);
Arend van Spriel5768f312015-06-11 00:12:20 +02002816 if (sdiodev->state != BRCMF_SDIOD_DATA)
2817 return -EIO;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002818
2819 /* Add space for the header */
Franky Lin706478c2013-08-10 12:27:29 +02002820 skb_push(pkt, bus->tx_hdrlen);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002821 /* precondition: IS_ALIGNED((unsigned long)(pkt->data), 2) */
2822
2823 prec = prio2prec((pkt->priority & PRIOMASK));
2824
2825 /* Check for existing queue, current flow-control,
2826 pending event, or pending clock */
2827 brcmf_dbg(TRACE, "deferring pktq len %d\n", pktq_len(&bus->txq));
Arend van Spriel80969832012-06-09 22:51:44 +02002828 bus->sdcnt.fcqueued++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002829
2830 /* Priority based enq */
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002831 spin_lock_bh(&bus->txq_lock);
Arend van Spriel5aa9f0e2014-02-25 20:30:27 +01002832 /* reset bus_flags in packet cb */
2833 *(u16 *)(pkt->cb) = 0;
Hante Meuleman84936622014-10-28 14:56:19 +01002834 if (!brcmf_sdio_prec_enq(&bus->txq, pkt, prec)) {
Franky Lin706478c2013-08-10 12:27:29 +02002835 skb_pull(pkt, bus->tx_hdrlen);
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002836 brcmf_err("out of bus->txq !!!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002837 ret = -ENOSR;
2838 } else {
2839 ret = 0;
2840 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002841
Franky Linc8bf3482011-12-16 18:37:08 -08002842 if (pktq_len(&bus->txq) >= TXHI) {
Hante Meuleman90d03ff2012-09-11 21:18:46 +02002843 bus->txoff = true;
Hante Meuleman84936622014-10-28 14:56:19 +01002844 brcmf_txflowblock(dev, true);
Franky Linc8bf3482011-12-16 18:37:08 -08002845 }
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002846 spin_unlock_bh(&bus->txq_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002847
Joe Perches8ae74652012-01-15 00:38:38 -08002848#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02002849 if (pktq_plen(&bus->txq, prec) > qcount[prec])
2850 qcount[prec] = pktq_plen(&bus->txq, prec);
2851#endif
Franky Linf1e68c22012-09-13 21:12:00 +02002852
Arend van Spriel99824642015-02-06 18:36:45 +01002853 brcmf_sdio_trigger_dpc(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002854 return ret;
2855}
2856
Joe Perches8ae74652012-01-15 00:38:38 -08002857#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02002858#define CONSOLE_LINE_MAX 192
2859
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002860static int brcmf_sdio_readconsole(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002861{
2862 struct brcmf_console *c = &bus->console;
2863 u8 line[CONSOLE_LINE_MAX], ch;
2864 u32 n, idx, addr;
2865 int rv;
2866
2867 /* Don't do anything until FWREADY updates console address */
2868 if (bus->console_addr == 0)
2869 return 0;
2870
2871 /* Read console log struct */
2872 addr = bus->console_addr + offsetof(struct rte_console, log_le);
Arend van Spriela39be272013-12-12 11:58:58 +01002873 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, (u8 *)&c->log_le,
2874 sizeof(c->log_le));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002875 if (rv < 0)
2876 return rv;
2877
2878 /* Allocate console buffer (one time only) */
2879 if (c->buf == NULL) {
2880 c->bufsize = le32_to_cpu(c->log_le.buf_size);
2881 c->buf = kmalloc(c->bufsize, GFP_ATOMIC);
2882 if (c->buf == NULL)
2883 return -ENOMEM;
2884 }
2885
2886 idx = le32_to_cpu(c->log_le.idx);
2887
2888 /* Protect against corrupt value */
2889 if (idx > c->bufsize)
2890 return -EBADE;
2891
2892 /* Skip reading the console buffer if the index pointer
2893 has not moved */
2894 if (idx == c->last)
2895 return 0;
2896
2897 /* Read the console buffer */
2898 addr = le32_to_cpu(c->log_le.buf);
Arend van Spriela39be272013-12-12 11:58:58 +01002899 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, c->buf, c->bufsize);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002900 if (rv < 0)
2901 return rv;
2902
2903 while (c->last != idx) {
2904 for (n = 0; n < CONSOLE_LINE_MAX - 2; n++) {
2905 if (c->last == idx) {
2906 /* This would output a partial line.
2907 * Instead, back up
2908 * the buffer pointer and output this
2909 * line next time around.
2910 */
2911 if (c->last >= n)
2912 c->last -= n;
2913 else
2914 c->last = c->bufsize - n;
2915 goto break2;
2916 }
2917 ch = c->buf[c->last];
2918 c->last = (c->last + 1) % c->bufsize;
2919 if (ch == '\n')
2920 break;
2921 line[n] = ch;
2922 }
2923
2924 if (n > 0) {
2925 if (line[n - 1] == '\r')
2926 n--;
2927 line[n] = 0;
Joe Perches18aad4f2012-01-15 00:38:42 -08002928 pr_debug("CONSOLE: %s\n", line);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002929 }
2930 }
2931break2:
2932
2933 return 0;
2934}
Joe Perches8ae74652012-01-15 00:38:38 -08002935#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02002936
Franky Linfcf094f2011-12-16 18:37:16 -08002937static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002938brcmf_sdio_bus_txctl(struct device *dev, unsigned char *msg, uint msglen)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002939{
Franky Lin47a1ce72011-11-22 17:21:55 -08002940 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01002941 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Lin47a1ce72011-11-22 17:21:55 -08002942 struct brcmf_sdio *bus = sdiodev->bus;
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002943 int ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002944
2945 brcmf_dbg(TRACE, "Enter\n");
Arend van Spriel5768f312015-06-11 00:12:20 +02002946 if (sdiodev->state != BRCMF_SDIOD_DATA)
2947 return -EIO;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002948
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002949 /* Send from dpc */
2950 bus->ctrl_frame_buf = msg;
2951 bus->ctrl_frame_len = msglen;
Hante Meuleman2c64e162015-03-18 13:25:22 +01002952 wmb();
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002953 bus->ctrl_frame_stat = true;
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002954
Arend van Spriel99824642015-02-06 18:36:45 +01002955 brcmf_sdio_trigger_dpc(bus);
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002956 wait_event_interruptible_timeout(bus->ctrl_wait, !bus->ctrl_frame_stat,
2957 msecs_to_jiffies(CTL_DONE_TIMEOUT));
Hante Meuleman449e58b2015-03-06 18:40:40 +01002958 ret = 0;
2959 if (bus->ctrl_frame_stat) {
2960 sdio_claim_host(bus->sdiodev->func[1]);
2961 if (bus->ctrl_frame_stat) {
2962 brcmf_dbg(SDIO, "ctrl_frame timeout\n");
2963 bus->ctrl_frame_stat = false;
2964 ret = -ETIMEDOUT;
2965 }
2966 sdio_release_host(bus->sdiodev->func[1]);
2967 }
2968 if (!ret) {
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002969 brcmf_dbg(SDIO, "ctrl_frame complete, err=%d\n",
2970 bus->ctrl_frame_err);
Hante Meuleman2c64e162015-03-18 13:25:22 +01002971 rmb();
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002972 ret = bus->ctrl_frame_err;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002973 }
2974
Arend van Spriel5b435de2011-10-05 13:19:03 +02002975 if (ret)
Arend van Spriel80969832012-06-09 22:51:44 +02002976 bus->sdcnt.tx_ctlerrs++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002977 else
Arend van Spriel80969832012-06-09 22:51:44 +02002978 bus->sdcnt.tx_ctlpkts++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002979
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002980 return ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002981}
2982
Arend van Spriel80969832012-06-09 22:51:44 +02002983#ifdef DEBUG
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002984static int brcmf_sdio_dump_console(struct seq_file *seq, struct brcmf_sdio *bus,
2985 struct sdpcm_shared *sh)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002986{
2987 u32 addr, console_ptr, console_size, console_index;
2988 char *conbuf = NULL;
2989 __le32 sh_val;
2990 int rv;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002991
2992 /* obtain console information from device memory */
2993 addr = sh->console_addr + offsetof(struct rte_console, log_le);
Arend van Spriela39be272013-12-12 11:58:58 +01002994 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
2995 (u8 *)&sh_val, sizeof(u32));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002996 if (rv < 0)
2997 return rv;
2998 console_ptr = le32_to_cpu(sh_val);
2999
3000 addr = sh->console_addr + offsetof(struct rte_console, log_le.buf_size);
Arend van Spriela39be272013-12-12 11:58:58 +01003001 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
3002 (u8 *)&sh_val, sizeof(u32));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003003 if (rv < 0)
3004 return rv;
3005 console_size = le32_to_cpu(sh_val);
3006
3007 addr = sh->console_addr + offsetof(struct rte_console, log_le.idx);
Arend van Spriela39be272013-12-12 11:58:58 +01003008 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
3009 (u8 *)&sh_val, sizeof(u32));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003010 if (rv < 0)
3011 return rv;
3012 console_index = le32_to_cpu(sh_val);
3013
3014 /* allocate buffer for console data */
3015 if (console_size <= CONSOLE_BUFFER_MAX)
3016 conbuf = vzalloc(console_size+1);
3017
3018 if (!conbuf)
3019 return -ENOMEM;
3020
3021 /* obtain the console data from device */
3022 conbuf[console_size] = '\0';
Arend van Spriela39be272013-12-12 11:58:58 +01003023 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, console_ptr, (u8 *)conbuf,
3024 console_size);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003025 if (rv < 0)
3026 goto done;
3027
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003028 rv = seq_write(seq, conbuf + console_index,
3029 console_size - console_index);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003030 if (rv < 0)
3031 goto done;
3032
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003033 if (console_index > 0)
3034 rv = seq_write(seq, conbuf, console_index - 1);
3035
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003036done:
3037 vfree(conbuf);
3038 return rv;
3039}
3040
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003041static int brcmf_sdio_trap_info(struct seq_file *seq, struct brcmf_sdio *bus,
3042 struct sdpcm_shared *sh)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003043{
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003044 int error;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003045 struct brcmf_trap_info tr;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003046
Piotr Haberbaa9e602013-04-05 10:57:42 +02003047 if ((sh->flags & SDPCM_SHARED_TRAP) == 0) {
3048 brcmf_dbg(INFO, "no trap in firmware\n");
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003049 return 0;
Piotr Haberbaa9e602013-04-05 10:57:42 +02003050 }
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003051
Arend van Spriela39be272013-12-12 11:58:58 +01003052 error = brcmf_sdiod_ramrw(bus->sdiodev, false, sh->trap_addr, (u8 *)&tr,
3053 sizeof(struct brcmf_trap_info));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003054 if (error < 0)
3055 return error;
3056
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003057 seq_printf(seq,
3058 "dongle trap info: type 0x%x @ epc 0x%08x\n"
3059 " cpsr 0x%08x spsr 0x%08x sp 0x%08x\n"
3060 " lr 0x%08x pc 0x%08x offset 0x%x\n"
3061 " r0 0x%08x r1 0x%08x r2 0x%08x r3 0x%08x\n"
3062 " r4 0x%08x r5 0x%08x r6 0x%08x r7 0x%08x\n",
3063 le32_to_cpu(tr.type), le32_to_cpu(tr.epc),
3064 le32_to_cpu(tr.cpsr), le32_to_cpu(tr.spsr),
3065 le32_to_cpu(tr.r13), le32_to_cpu(tr.r14),
3066 le32_to_cpu(tr.pc), sh->trap_addr,
3067 le32_to_cpu(tr.r0), le32_to_cpu(tr.r1),
3068 le32_to_cpu(tr.r2), le32_to_cpu(tr.r3),
3069 le32_to_cpu(tr.r4), le32_to_cpu(tr.r5),
3070 le32_to_cpu(tr.r6), le32_to_cpu(tr.r7));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003071
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003072 return 0;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003073}
3074
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003075static int brcmf_sdio_assert_info(struct seq_file *seq, struct brcmf_sdio *bus,
3076 struct sdpcm_shared *sh)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003077{
3078 int error = 0;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003079 char file[80] = "?";
3080 char expr[80] = "<???>";
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003081
3082 if ((sh->flags & SDPCM_SHARED_ASSERT_BUILT) == 0) {
3083 brcmf_dbg(INFO, "firmware not built with -assert\n");
3084 return 0;
3085 } else if ((sh->flags & SDPCM_SHARED_ASSERT) == 0) {
3086 brcmf_dbg(INFO, "no assert in dongle\n");
3087 return 0;
3088 }
3089
Franky Lin38b0b0d2012-11-05 16:22:24 -08003090 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003091 if (sh->assert_file_addr != 0) {
Arend van Spriela39be272013-12-12 11:58:58 +01003092 error = brcmf_sdiod_ramrw(bus->sdiodev, false,
3093 sh->assert_file_addr, (u8 *)file, 80);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003094 if (error < 0)
3095 return error;
3096 }
3097 if (sh->assert_exp_addr != 0) {
Arend van Spriela39be272013-12-12 11:58:58 +01003098 error = brcmf_sdiod_ramrw(bus->sdiodev, false,
3099 sh->assert_exp_addr, (u8 *)expr, 80);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003100 if (error < 0)
3101 return error;
3102 }
Franky Lin38b0b0d2012-11-05 16:22:24 -08003103 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003104
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003105 seq_printf(seq, "dongle assert: %s:%d: assert(%s)\n",
3106 file, sh->assert_line, expr);
3107 return 0;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003108}
3109
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003110static int brcmf_sdio_checkdied(struct brcmf_sdio *bus)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003111{
3112 int error;
3113 struct sdpcm_shared sh;
3114
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003115 error = brcmf_sdio_readshared(bus, &sh);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003116
3117 if (error < 0)
3118 return error;
3119
3120 if ((sh.flags & SDPCM_SHARED_ASSERT_BUILT) == 0)
3121 brcmf_dbg(INFO, "firmware not built with -assert\n");
3122 else if (sh.flags & SDPCM_SHARED_ASSERT)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003123 brcmf_err("assertion in dongle\n");
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003124
3125 if (sh.flags & SDPCM_SHARED_TRAP)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003126 brcmf_err("firmware trap in dongle\n");
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003127
3128 return 0;
3129}
3130
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003131static int brcmf_sdio_died_dump(struct seq_file *seq, struct brcmf_sdio *bus)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003132{
3133 int error = 0;
3134 struct sdpcm_shared sh;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003135
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003136 error = brcmf_sdio_readshared(bus, &sh);
3137 if (error < 0)
3138 goto done;
3139
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003140 error = brcmf_sdio_assert_info(seq, bus, &sh);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003141 if (error < 0)
3142 goto done;
Piotr Haberbaa9e602013-04-05 10:57:42 +02003143
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003144 error = brcmf_sdio_trap_info(seq, bus, &sh);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003145 if (error < 0)
3146 goto done;
3147
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003148 error = brcmf_sdio_dump_console(seq, bus, &sh);
Piotr Haberbaa9e602013-04-05 10:57:42 +02003149
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003150done:
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003151 return error;
3152}
3153
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003154static int brcmf_sdio_forensic_read(struct seq_file *seq, void *data)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003155{
Arend van Spriel82d957e2014-07-12 08:49:36 +02003156 struct brcmf_bus *bus_if = dev_get_drvdata(seq->private);
3157 struct brcmf_sdio *bus = bus_if->bus_priv.sdio->bus;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003158
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003159 return brcmf_sdio_died_dump(seq, bus);
3160}
3161
Arend van Spriel82d957e2014-07-12 08:49:36 +02003162static int brcmf_debugfs_sdio_count_read(struct seq_file *seq, void *data)
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003163{
Arend van Spriel82d957e2014-07-12 08:49:36 +02003164 struct brcmf_bus *bus_if = dev_get_drvdata(seq->private);
3165 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3166 struct brcmf_sdio_count *sdcnt = &sdiodev->bus->sdcnt;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003167
Arend van Spriel82d957e2014-07-12 08:49:36 +02003168 seq_printf(seq,
3169 "intrcount: %u\nlastintrs: %u\n"
3170 "pollcnt: %u\nregfails: %u\n"
3171 "tx_sderrs: %u\nfcqueued: %u\n"
3172 "rxrtx: %u\nrx_toolong: %u\n"
3173 "rxc_errors: %u\nrx_hdrfail: %u\n"
3174 "rx_badhdr: %u\nrx_badseq: %u\n"
3175 "fc_rcvd: %u\nfc_xoff: %u\n"
3176 "fc_xon: %u\nrxglomfail: %u\n"
3177 "rxglomframes: %u\nrxglompkts: %u\n"
3178 "f2rxhdrs: %u\nf2rxdata: %u\n"
3179 "f2txdata: %u\nf1regdata: %u\n"
3180 "tickcnt: %u\ntx_ctlerrs: %lu\n"
3181 "tx_ctlpkts: %lu\nrx_ctlerrs: %lu\n"
3182 "rx_ctlpkts: %lu\nrx_readahead: %lu\n",
3183 sdcnt->intrcount, sdcnt->lastintrs,
3184 sdcnt->pollcnt, sdcnt->regfails,
3185 sdcnt->tx_sderrs, sdcnt->fcqueued,
3186 sdcnt->rxrtx, sdcnt->rx_toolong,
3187 sdcnt->rxc_errors, sdcnt->rx_hdrfail,
3188 sdcnt->rx_badhdr, sdcnt->rx_badseq,
3189 sdcnt->fc_rcvd, sdcnt->fc_xoff,
3190 sdcnt->fc_xon, sdcnt->rxglomfail,
3191 sdcnt->rxglomframes, sdcnt->rxglompkts,
3192 sdcnt->f2rxhdrs, sdcnt->f2rxdata,
3193 sdcnt->f2txdata, sdcnt->f1regdata,
3194 sdcnt->tickcnt, sdcnt->tx_ctlerrs,
3195 sdcnt->tx_ctlpkts, sdcnt->rx_ctlerrs,
3196 sdcnt->rx_ctlpkts, sdcnt->rx_readahead_cnt);
3197
3198 return 0;
3199}
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003200
Arend van Spriel80969832012-06-09 22:51:44 +02003201static void brcmf_sdio_debugfs_create(struct brcmf_sdio *bus)
3202{
3203 struct brcmf_pub *drvr = bus->sdiodev->bus_if->drvr;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003204 struct dentry *dentry = brcmf_debugfs_get_devdir(drvr);
Arend van Spriel80969832012-06-09 22:51:44 +02003205
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003206 if (IS_ERR_OR_NULL(dentry))
3207 return;
3208
Arend van Spriel9d6c1dc2015-08-26 22:15:01 +02003209 bus->console_interval = BRCMF_CONSOLE;
3210
Arend van Spriel82d957e2014-07-12 08:49:36 +02003211 brcmf_debugfs_add_entry(drvr, "forensics", brcmf_sdio_forensic_read);
3212 brcmf_debugfs_add_entry(drvr, "counters",
3213 brcmf_debugfs_sdio_count_read);
Daniel Kim0801e6c2014-01-29 15:32:13 +01003214 debugfs_create_u32("console_interval", 0644, dentry,
3215 &bus->console_interval);
Arend van Spriel80969832012-06-09 22:51:44 +02003216}
3217#else
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003218static int brcmf_sdio_checkdied(struct brcmf_sdio *bus)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003219{
3220 return 0;
3221}
3222
Arend van Spriel80969832012-06-09 22:51:44 +02003223static void brcmf_sdio_debugfs_create(struct brcmf_sdio *bus)
3224{
3225}
3226#endif /* DEBUG */
3227
Franky Linfcf094f2011-12-16 18:37:16 -08003228static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003229brcmf_sdio_bus_rxctl(struct device *dev, unsigned char *msg, uint msglen)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003230{
3231 int timeleft;
3232 uint rxlen = 0;
3233 bool pending;
Franky Lindd43a012012-11-05 16:22:22 -08003234 u8 *buf;
Franky Lin532cdd32011-11-22 17:21:54 -08003235 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01003236 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Lin532cdd32011-11-22 17:21:54 -08003237 struct brcmf_sdio *bus = sdiodev->bus;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003238
3239 brcmf_dbg(TRACE, "Enter\n");
Arend van Spriel5768f312015-06-11 00:12:20 +02003240 if (sdiodev->state != BRCMF_SDIOD_DATA)
3241 return -EIO;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003242
3243 /* Wait until control frame is available */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003244 timeleft = brcmf_sdio_dcmd_resp_wait(bus, &bus->rxlen, &pending);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003245
Franky Lindd43a012012-11-05 16:22:22 -08003246 spin_lock_bh(&bus->rxctl_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003247 rxlen = bus->rxlen;
3248 memcpy(msg, bus->rxctl, min(msglen, rxlen));
Franky Lindd43a012012-11-05 16:22:22 -08003249 bus->rxctl = NULL;
3250 buf = bus->rxctl_orig;
3251 bus->rxctl_orig = NULL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003252 bus->rxlen = 0;
Franky Lindd43a012012-11-05 16:22:22 -08003253 spin_unlock_bh(&bus->rxctl_lock);
3254 vfree(buf);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003255
3256 if (rxlen) {
3257 brcmf_dbg(CTL, "resumed on rxctl frame, got %d expected %d\n",
3258 rxlen, msglen);
3259 } else if (timeleft == 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003260 brcmf_err("resumed on timeout\n");
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003261 brcmf_sdio_checkdied(bus);
Joe Perches23677ce2012-02-09 11:17:23 +00003262 } else if (pending) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003263 brcmf_dbg(CTL, "cancelled\n");
3264 return -ERESTARTSYS;
3265 } else {
3266 brcmf_dbg(CTL, "resumed for unknown reason?\n");
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003267 brcmf_sdio_checkdied(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003268 }
3269
3270 if (rxlen)
Arend van Spriel80969832012-06-09 22:51:44 +02003271 bus->sdcnt.rx_ctlpkts++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003272 else
Arend van Spriel80969832012-06-09 22:51:44 +02003273 bus->sdcnt.rx_ctlerrs++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003274
3275 return rxlen ? (int)rxlen : -ETIMEDOUT;
3276}
3277
Hante Meulemana74d0362014-01-13 22:20:22 +01003278#ifdef DEBUG
3279static bool
3280brcmf_sdio_verifymemory(struct brcmf_sdio_dev *sdiodev, u32 ram_addr,
3281 u8 *ram_data, uint ram_sz)
3282{
3283 char *ram_cmp;
3284 int err;
3285 bool ret = true;
3286 int address;
3287 int offset;
3288 int len;
3289
3290 /* read back and verify */
3291 brcmf_dbg(INFO, "Compare RAM dl & ul at 0x%08x; size=%d\n", ram_addr,
3292 ram_sz);
3293 ram_cmp = kmalloc(MEMBLOCK, GFP_KERNEL);
3294 /* do not proceed while no memory but */
3295 if (!ram_cmp)
3296 return true;
3297
3298 address = ram_addr;
3299 offset = 0;
3300 while (offset < ram_sz) {
3301 len = ((offset + MEMBLOCK) < ram_sz) ? MEMBLOCK :
3302 ram_sz - offset;
3303 err = brcmf_sdiod_ramrw(sdiodev, false, address, ram_cmp, len);
3304 if (err) {
3305 brcmf_err("error %d on reading %d membytes at 0x%08x\n",
3306 err, len, address);
3307 ret = false;
3308 break;
3309 } else if (memcmp(ram_cmp, &ram_data[offset], len)) {
3310 brcmf_err("Downloaded RAM image is corrupted, block offset is %d, len is %d\n",
3311 offset, len);
3312 ret = false;
3313 break;
3314 }
3315 offset += len;
3316 address += len;
3317 }
3318
3319 kfree(ram_cmp);
3320
3321 return ret;
3322}
3323#else /* DEBUG */
3324static bool
3325brcmf_sdio_verifymemory(struct brcmf_sdio_dev *sdiodev, u32 ram_addr,
3326 u8 *ram_data, uint ram_sz)
3327{
3328 return true;
3329}
3330#endif /* DEBUG */
3331
Arend van Spriel33556502014-01-13 22:20:25 +01003332static int brcmf_sdio_download_code_file(struct brcmf_sdio *bus,
3333 const struct firmware *fw)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003334{
Hante Meulemanf2c44fe2013-09-25 13:05:45 +02003335 int err;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003336
Hante Meulemana74d0362014-01-13 22:20:22 +01003337 brcmf_dbg(TRACE, "Enter\n");
3338
Hante Meulemanf9951c12014-01-29 15:32:17 +01003339 err = brcmf_sdiod_ramrw(bus->sdiodev, true, bus->ci->rambase,
3340 (u8 *)fw->data, fw->size);
3341 if (err)
3342 brcmf_err("error %d on writing %d membytes at 0x%08x\n",
3343 err, (int)fw->size, bus->ci->rambase);
3344 else if (!brcmf_sdio_verifymemory(bus->sdiodev, bus->ci->rambase,
3345 (u8 *)fw->data, fw->size))
3346 err = -EIO;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003347
Hante Meulemanf2c44fe2013-09-25 13:05:45 +02003348 return err;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003349}
3350
Arend van Spriel33556502014-01-13 22:20:25 +01003351static int brcmf_sdio_download_nvram(struct brcmf_sdio *bus,
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003352 void *vars, u32 varsz)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003353{
Hante Meulemana74d0362014-01-13 22:20:22 +01003354 int address;
3355 int err;
3356
3357 brcmf_dbg(TRACE, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003358
Hante Meulemana74d0362014-01-13 22:20:22 +01003359 address = bus->ci->ramsize - varsz + bus->ci->rambase;
3360 err = brcmf_sdiod_ramrw(bus->sdiodev, true, address, vars, varsz);
3361 if (err)
3362 brcmf_err("error %d on writing %d nvram bytes at 0x%08x\n",
3363 err, varsz, address);
3364 else if (!brcmf_sdio_verifymemory(bus->sdiodev, address, vars, varsz))
3365 err = -EIO;
3366
Hante Meulemana74d0362014-01-13 22:20:22 +01003367 return err;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003368}
3369
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003370static int brcmf_sdio_download_firmware(struct brcmf_sdio *bus,
3371 const struct firmware *fw,
3372 void *nvram, u32 nvlen)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003373{
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003374 int bcmerror = -EFAULT;
Arend van Spriel33556502014-01-13 22:20:25 +01003375 u32 rstvec;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003376
3377 sdio_claim_host(bus->sdiodev->func[1]);
3378 brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003379
Arend van Spriel33556502014-01-13 22:20:25 +01003380 rstvec = get_unaligned_le32(fw->data);
3381 brcmf_dbg(SDIO, "firmware rstvec: %x\n", rstvec);
3382
3383 bcmerror = brcmf_sdio_download_code_file(bus, fw);
3384 release_firmware(fw);
3385 if (bcmerror) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003386 brcmf_err("dongle image file download failed\n");
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003387 brcmf_fw_nvram_free(nvram);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003388 goto err;
3389 }
3390
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003391 bcmerror = brcmf_sdio_download_nvram(bus, nvram, nvlen);
3392 brcmf_fw_nvram_free(nvram);
Arend van Spriel33556502014-01-13 22:20:25 +01003393 if (bcmerror) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003394 brcmf_err("dongle nvram file download failed\n");
Franky Lin3eaa9562013-04-02 21:06:20 +02003395 goto err;
3396 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02003397
3398 /* Take arm out of reset */
Arend van Sprield380ebc2015-03-11 16:11:29 +01003399 if (!brcmf_chip_set_active(bus->ci, rstvec)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003400 brcmf_err("error getting out of ARM core reset\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003401 goto err;
3402 }
3403
Hante Meulemana1cee862015-01-25 20:31:35 +01003404 /* Allow full data communication using DPC from now on. */
Arend van Spriela1ce7a02015-02-06 18:36:42 +01003405 brcmf_sdiod_change_state(bus->sdiodev, BRCMF_SDIOD_DATA);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003406 bcmerror = 0;
3407
3408err:
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003409 brcmf_sdio_clkctl(bus, CLK_SDONLY, false);
3410 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003411 return bcmerror;
3412}
3413
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003414static void brcmf_sdio_sr_init(struct brcmf_sdio *bus)
Piotr Haber4a3da992013-04-11 13:28:46 +02003415{
3416 int err = 0;
3417 u8 val;
3418
3419 brcmf_dbg(TRACE, "Enter\n");
3420
Arend van Spriela39be272013-12-12 11:58:58 +01003421 val = brcmf_sdiod_regrb(bus->sdiodev, SBSDIO_FUNC1_WAKEUPCTRL, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003422 if (err) {
3423 brcmf_err("error reading SBSDIO_FUNC1_WAKEUPCTRL\n");
3424 return;
3425 }
3426
3427 val |= 1 << SBSDIO_FUNC1_WCTRL_HTWAIT_SHIFT;
Arend van Spriela39be272013-12-12 11:58:58 +01003428 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_WAKEUPCTRL, val, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003429 if (err) {
3430 brcmf_err("error writing SBSDIO_FUNC1_WAKEUPCTRL\n");
3431 return;
3432 }
3433
3434 /* Add CMD14 Support */
Arend van Spriela39be272013-12-12 11:58:58 +01003435 brcmf_sdiod_regwb(bus->sdiodev, SDIO_CCCR_BRCM_CARDCAP,
3436 (SDIO_CCCR_BRCM_CARDCAP_CMD14_SUPPORT |
3437 SDIO_CCCR_BRCM_CARDCAP_CMD14_EXT),
3438 &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003439 if (err) {
3440 brcmf_err("error writing SDIO_CCCR_BRCM_CARDCAP\n");
3441 return;
3442 }
3443
Arend van Spriela39be272013-12-12 11:58:58 +01003444 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3445 SBSDIO_FORCE_HT, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003446 if (err) {
3447 brcmf_err("error writing SBSDIO_FUNC1_CHIPCLKCSR\n");
3448 return;
3449 }
3450
3451 /* set flag */
3452 bus->sr_enabled = true;
3453 brcmf_dbg(INFO, "SR enabled\n");
3454}
3455
3456/* enable KSO bit */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003457static int brcmf_sdio_kso_init(struct brcmf_sdio *bus)
Piotr Haber4a3da992013-04-11 13:28:46 +02003458{
3459 u8 val;
3460 int err = 0;
3461
3462 brcmf_dbg(TRACE, "Enter\n");
3463
3464 /* KSO bit added in SDIO core rev 12 */
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003465 if (brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV)->rev < 12)
Piotr Haber4a3da992013-04-11 13:28:46 +02003466 return 0;
3467
Arend van Spriela39be272013-12-12 11:58:58 +01003468 val = brcmf_sdiod_regrb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003469 if (err) {
3470 brcmf_err("error reading SBSDIO_FUNC1_SLEEPCSR\n");
3471 return err;
3472 }
3473
3474 if (!(val & SBSDIO_FUNC1_SLEEPCSR_KSO_MASK)) {
3475 val |= (SBSDIO_FUNC1_SLEEPCSR_KSO_EN <<
3476 SBSDIO_FUNC1_SLEEPCSR_KSO_SHIFT);
Arend van Spriela39be272013-12-12 11:58:58 +01003477 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
3478 val, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003479 if (err) {
3480 brcmf_err("error writing SBSDIO_FUNC1_SLEEPCSR\n");
3481 return err;
3482 }
3483 }
3484
3485 return 0;
3486}
3487
3488
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003489static int brcmf_sdio_bus_preinit(struct device *dev)
Arend van Sprielcf458282013-11-29 11:48:15 +01003490{
3491 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3492 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3493 struct brcmf_sdio *bus = sdiodev->bus;
Franky Lin8da9d2c2013-11-29 12:25:10 +01003494 uint pad_size;
Arend van Sprielcf458282013-11-29 11:48:15 +01003495 u32 value;
Arend van Sprielcf458282013-11-29 11:48:15 +01003496 int err;
3497
Franky Lin8da9d2c2013-11-29 12:25:10 +01003498 /* the commands below use the terms tx and rx from
3499 * a device perspective, ie. bus:txglom affects the
3500 * bus transfers from device to host.
3501 */
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003502 if (brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV)->rev < 12) {
Arend van Sprielcf458282013-11-29 11:48:15 +01003503 /* for sdio core rev < 12, disable txgloming */
3504 value = 0;
3505 err = brcmf_iovar_data_set(dev, "bus:txglom", &value,
3506 sizeof(u32));
3507 } else {
3508 /* otherwise, set txglomalign */
3509 value = 4;
3510 if (sdiodev->pdata)
3511 value = sdiodev->pdata->sd_sgentry_align;
3512 /* SDIO ADMA requires at least 32 bit alignment */
3513 value = max_t(u32, value, 4);
3514 err = brcmf_iovar_data_set(dev, "bus:txglomalign", &value,
3515 sizeof(u32));
3516 }
Franky Lin8da9d2c2013-11-29 12:25:10 +01003517
3518 if (err < 0)
3519 goto done;
3520
3521 bus->tx_hdrlen = SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN;
3522 if (sdiodev->sg_support) {
3523 bus->txglom = false;
3524 value = 1;
3525 pad_size = bus->sdiodev->func[2]->cur_blksize << 1;
Franky Lin8da9d2c2013-11-29 12:25:10 +01003526 err = brcmf_iovar_data_set(bus->sdiodev->dev, "bus:rxglom",
3527 &value, sizeof(u32));
3528 if (err < 0) {
3529 /* bus:rxglom is allowed to fail */
3530 err = 0;
3531 } else {
3532 bus->txglom = true;
3533 bus->tx_hdrlen += SDPCM_HWEXT_LEN;
3534 }
3535 }
3536 brcmf_bus_add_txhdrlen(bus->sdiodev->dev, bus->tx_hdrlen);
3537
3538done:
Arend van Sprielcf458282013-11-29 11:48:15 +01003539 return err;
3540}
3541
Arend van Spriel99824642015-02-06 18:36:45 +01003542void brcmf_sdio_trigger_dpc(struct brcmf_sdio *bus)
3543{
Hante Meuleman2c64e162015-03-18 13:25:22 +01003544 if (!bus->dpc_triggered) {
3545 bus->dpc_triggered = true;
Arend van Spriel99824642015-02-06 18:36:45 +01003546 queue_work(bus->brcmf_wq, &bus->datawork);
3547 }
3548}
3549
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003550void brcmf_sdio_isr(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003551{
Arend van Spriel5b435de2011-10-05 13:19:03 +02003552 brcmf_dbg(TRACE, "Enter\n");
3553
3554 if (!bus) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003555 brcmf_err("bus is null pointer, exiting\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003556 return;
3557 }
3558
Arend van Spriel5b435de2011-10-05 13:19:03 +02003559 /* Count the interrupt call */
Arend van Spriel80969832012-06-09 22:51:44 +02003560 bus->sdcnt.intrcount++;
Franky Lin45316032012-09-13 21:12:03 +02003561 if (in_interrupt())
3562 atomic_set(&bus->ipend, 1);
3563 else
3564 if (brcmf_sdio_intr_rstatus(bus)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003565 brcmf_err("failed backplane access\n");
Franky Lin45316032012-09-13 21:12:03 +02003566 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02003567
Arend van Spriel5b435de2011-10-05 13:19:03 +02003568 /* Disable additional interrupts (is this needed now)? */
3569 if (!bus->intr)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003570 brcmf_err("isr w/o interrupt configured!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003571
Hante Meuleman2c64e162015-03-18 13:25:22 +01003572 bus->dpc_triggered = true;
Franky Linf1e68c22012-09-13 21:12:00 +02003573 queue_work(bus->brcmf_wq, &bus->datawork);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003574}
3575
Hante Meulemanb441ba82015-03-06 18:40:39 +01003576static void brcmf_sdio_bus_watchdog(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003577{
Arend van Spriel5b435de2011-10-05 13:19:03 +02003578 brcmf_dbg(TIMER, "Enter\n");
3579
Arend van Spriel5b435de2011-10-05 13:19:03 +02003580 /* Poll period: check device if appropriate. */
Piotr Haber4a3da992013-04-11 13:28:46 +02003581 if (!bus->sr_enabled &&
3582 bus->poll && (++bus->polltick >= bus->pollrate)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003583 u32 intstatus = 0;
3584
3585 /* Reset poll tick */
3586 bus->polltick = 0;
3587
3588 /* Check device if no interrupts */
Arend van Spriel80969832012-06-09 22:51:44 +02003589 if (!bus->intr ||
3590 (bus->sdcnt.intrcount == bus->sdcnt.lastintrs)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003591
Hante Meuleman2c64e162015-03-18 13:25:22 +01003592 if (!bus->dpc_triggered) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003593 u8 devpend;
Arend van Sprielfccfe932013-06-18 13:29:26 +02003594
Franky Lin38b0b0d2012-11-05 16:22:24 -08003595 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriela39be272013-12-12 11:58:58 +01003596 devpend = brcmf_sdiod_regrb(bus->sdiodev,
3597 SDIO_CCCR_INTx,
3598 NULL);
Franky Lin38b0b0d2012-11-05 16:22:24 -08003599 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel99824642015-02-06 18:36:45 +01003600 intstatus = devpend & (INTR_STATUS_FUNC1 |
3601 INTR_STATUS_FUNC2);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003602 }
3603
3604 /* If there is something, make like the ISR and
3605 schedule the DPC */
3606 if (intstatus) {
Arend van Spriel80969832012-06-09 22:51:44 +02003607 bus->sdcnt.pollcnt++;
Franky Lin1d382272012-09-13 21:11:59 +02003608 atomic_set(&bus->ipend, 1);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003609
Hante Meuleman2c64e162015-03-18 13:25:22 +01003610 bus->dpc_triggered = true;
Franky Linf1e68c22012-09-13 21:12:00 +02003611 queue_work(bus->brcmf_wq, &bus->datawork);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003612 }
3613 }
3614
3615 /* Update interrupt tracking */
Arend van Spriel80969832012-06-09 22:51:44 +02003616 bus->sdcnt.lastintrs = bus->sdcnt.intrcount;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003617 }
Joe Perches8ae74652012-01-15 00:38:38 -08003618#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02003619 /* Poll for console output periodically */
Arend van Spriel9d6c1dc2015-08-26 22:15:01 +02003620 if (bus->sdiodev->state == BRCMF_SDIOD_DATA && BRCMF_FWCON_ON() &&
Franky Lin8d169aa2011-11-22 17:21:52 -08003621 bus->console_interval != 0) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003622 bus->console.count += BRCMF_WD_POLL_MS;
3623 if (bus->console.count >= bus->console_interval) {
3624 bus->console.count -= bus->console_interval;
Franky Lin38b0b0d2012-11-05 16:22:24 -08003625 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003626 /* Make sure backplane clock is on */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003627 brcmf_sdio_bus_sleep(bus, false, false);
3628 if (brcmf_sdio_readconsole(bus) < 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003629 /* stop on error */
3630 bus->console_interval = 0;
Franky Lin38b0b0d2012-11-05 16:22:24 -08003631 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003632 }
3633 }
Joe Perches8ae74652012-01-15 00:38:38 -08003634#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02003635
3636 /* On idle timeout clear activity flag and/or turn off clock */
Hante Meuleman2c64e162015-03-18 13:25:22 +01003637 if (!bus->dpc_triggered) {
3638 rmb();
3639 if ((!bus->dpc_running) && (bus->idletime > 0) &&
3640 (bus->clkstate == CLK_AVAIL)) {
3641 bus->idlecount++;
3642 if (bus->idlecount > bus->idletime) {
3643 brcmf_dbg(SDIO, "idle\n");
3644 sdio_claim_host(bus->sdiodev->func[1]);
3645 brcmf_sdio_wd_timer(bus, 0);
3646 bus->idlecount = 0;
3647 brcmf_sdio_bus_sleep(bus, true, false);
3648 sdio_release_host(bus->sdiodev->func[1]);
3649 }
3650 } else {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003651 bus->idlecount = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003652 }
Hante Meulemanb441ba82015-03-06 18:40:39 +01003653 } else {
3654 bus->idlecount = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003655 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02003656}
3657
Franky Linf1e68c22012-09-13 21:12:00 +02003658static void brcmf_sdio_dataworker(struct work_struct *work)
3659{
3660 struct brcmf_sdio *bus = container_of(work, struct brcmf_sdio,
3661 datawork);
Franky Linf1e68c22012-09-13 21:12:00 +02003662
Hante Meuleman2c64e162015-03-18 13:25:22 +01003663 bus->dpc_running = true;
3664 wmb();
3665 while (ACCESS_ONCE(bus->dpc_triggered)) {
3666 bus->dpc_triggered = false;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003667 brcmf_sdio_dpc(bus);
Hante Meulemanb441ba82015-03-06 18:40:39 +01003668 bus->idlecount = 0;
Franky Linf1e68c22012-09-13 21:12:00 +02003669 }
Hante Meuleman2c64e162015-03-18 13:25:22 +01003670 bus->dpc_running = false;
Arend van Spriel99824642015-02-06 18:36:45 +01003671 if (brcmf_sdiod_freezing(bus->sdiodev)) {
3672 brcmf_sdiod_change_state(bus->sdiodev, BRCMF_SDIOD_DOWN);
3673 brcmf_sdiod_try_freeze(bus->sdiodev);
3674 brcmf_sdiod_change_state(bus->sdiodev, BRCMF_SDIOD_DATA);
3675 }
Franky Linf1e68c22012-09-13 21:12:00 +02003676}
3677
Arend van Spriel65d80d02014-01-29 15:32:14 +01003678static void
3679brcmf_sdio_drivestrengthinit(struct brcmf_sdio_dev *sdiodev,
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003680 struct brcmf_chip *ci, u32 drivestrength)
Arend van Spriel65d80d02014-01-29 15:32:14 +01003681{
3682 const struct sdiod_drive_str *str_tab = NULL;
3683 u32 str_mask;
3684 u32 str_shift;
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003685 u32 base;
Arend van Spriel65d80d02014-01-29 15:32:14 +01003686 u32 i;
3687 u32 drivestrength_sel = 0;
3688 u32 cc_data_temp;
3689 u32 addr;
3690
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003691 if (!(ci->cc_caps & CC_CAP_PMU))
Arend van Spriel65d80d02014-01-29 15:32:14 +01003692 return;
3693
3694 switch (SDIOD_DRVSTR_KEY(ci->chip, ci->pmurev)) {
Hante Meuleman5779ae62014-07-12 08:49:34 +02003695 case SDIOD_DRVSTR_KEY(BRCM_CC_4330_CHIP_ID, 12):
Arend van Spriel65d80d02014-01-29 15:32:14 +01003696 str_tab = sdiod_drvstr_tab1_1v8;
3697 str_mask = 0x00003800;
3698 str_shift = 11;
3699 break;
Hante Meuleman5779ae62014-07-12 08:49:34 +02003700 case SDIOD_DRVSTR_KEY(BRCM_CC_4334_CHIP_ID, 17):
Arend van Spriel65d80d02014-01-29 15:32:14 +01003701 str_tab = sdiod_drvstr_tab6_1v8;
3702 str_mask = 0x00001800;
3703 str_shift = 11;
3704 break;
Hante Meuleman5779ae62014-07-12 08:49:34 +02003705 case SDIOD_DRVSTR_KEY(BRCM_CC_43143_CHIP_ID, 17):
Arend van Spriel65d80d02014-01-29 15:32:14 +01003706 /* note: 43143 does not support tristate */
3707 i = ARRAY_SIZE(sdiod_drvstr_tab2_3v3) - 1;
3708 if (drivestrength >= sdiod_drvstr_tab2_3v3[i].strength) {
3709 str_tab = sdiod_drvstr_tab2_3v3;
3710 str_mask = 0x00000007;
3711 str_shift = 0;
3712 } else
3713 brcmf_err("Invalid SDIO Drive strength for chip %s, strength=%d\n",
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003714 ci->name, drivestrength);
Arend van Spriel65d80d02014-01-29 15:32:14 +01003715 break;
Hante Meuleman5779ae62014-07-12 08:49:34 +02003716 case SDIOD_DRVSTR_KEY(BRCM_CC_43362_CHIP_ID, 13):
Arend van Spriel65d80d02014-01-29 15:32:14 +01003717 str_tab = sdiod_drive_strength_tab5_1v8;
3718 str_mask = 0x00003800;
3719 str_shift = 11;
3720 break;
3721 default:
3722 brcmf_err("No SDIO Drive strength init done for chip %s rev %d pmurev %d\n",
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003723 ci->name, ci->chiprev, ci->pmurev);
Arend van Spriel65d80d02014-01-29 15:32:14 +01003724 break;
3725 }
3726
3727 if (str_tab != NULL) {
3728 for (i = 0; str_tab[i].strength != 0; i++) {
3729 if (drivestrength >= str_tab[i].strength) {
3730 drivestrength_sel = str_tab[i].sel;
3731 break;
3732 }
3733 }
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003734 base = brcmf_chip_get_chipcommon(ci)->base;
Arend van Spriel65d80d02014-01-29 15:32:14 +01003735 addr = CORE_CC_REG(base, chipcontrol_addr);
3736 brcmf_sdiod_regwl(sdiodev, addr, 1, NULL);
3737 cc_data_temp = brcmf_sdiod_regrl(sdiodev, addr, NULL);
3738 cc_data_temp &= ~str_mask;
3739 drivestrength_sel <<= str_shift;
3740 cc_data_temp |= drivestrength_sel;
3741 brcmf_sdiod_regwl(sdiodev, addr, cc_data_temp, NULL);
3742
3743 brcmf_dbg(INFO, "SDIO: %d mA (req=%d mA) drive strength selected, set to 0x%08x\n",
3744 str_tab[i].strength, drivestrength, cc_data_temp);
3745 }
3746}
3747
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003748static int brcmf_sdio_buscoreprep(void *ctx)
Arend van Spriel65d80d02014-01-29 15:32:14 +01003749{
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003750 struct brcmf_sdio_dev *sdiodev = ctx;
Arend van Spriel65d80d02014-01-29 15:32:14 +01003751 int err = 0;
3752 u8 clkval, clkset;
3753
3754 /* Try forcing SDIO core to do ALPAvail request only */
3755 clkset = SBSDIO_FORCE_HW_CLKREQ_OFF | SBSDIO_ALP_AVAIL_REQ;
3756 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, clkset, &err);
3757 if (err) {
3758 brcmf_err("error writing for HT off\n");
3759 return err;
3760 }
3761
3762 /* If register supported, wait for ALPAvail and then force ALP */
3763 /* This may take up to 15 milliseconds */
3764 clkval = brcmf_sdiod_regrb(sdiodev,
3765 SBSDIO_FUNC1_CHIPCLKCSR, NULL);
3766
3767 if ((clkval & ~SBSDIO_AVBITS) != clkset) {
3768 brcmf_err("ChipClkCSR access: wrote 0x%02x read 0x%02x\n",
3769 clkset, clkval);
3770 return -EACCES;
3771 }
3772
3773 SPINWAIT(((clkval = brcmf_sdiod_regrb(sdiodev,
3774 SBSDIO_FUNC1_CHIPCLKCSR, NULL)),
3775 !SBSDIO_ALPAV(clkval)),
3776 PMU_MAX_TRANSITION_DLY);
3777 if (!SBSDIO_ALPAV(clkval)) {
3778 brcmf_err("timeout on ALPAV wait, clkval 0x%02x\n",
3779 clkval);
3780 return -EBUSY;
3781 }
3782
3783 clkset = SBSDIO_FORCE_HW_CLKREQ_OFF | SBSDIO_FORCE_ALP;
3784 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, clkset, &err);
3785 udelay(65);
3786
3787 /* Also, disable the extra SDIO pull-ups */
3788 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_SDIOPULLUP, 0, NULL);
3789
3790 return 0;
3791}
3792
Arend van Sprield380ebc2015-03-11 16:11:29 +01003793static void brcmf_sdio_buscore_activate(void *ctx, struct brcmf_chip *chip,
3794 u32 rstvec)
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003795{
3796 struct brcmf_sdio_dev *sdiodev = ctx;
3797 struct brcmf_core *core;
3798 u32 reg_addr;
3799
3800 /* clear all interrupts */
3801 core = brcmf_chip_get_core(chip, BCMA_CORE_SDIO_DEV);
3802 reg_addr = core->base + offsetof(struct sdpcmd_regs, intstatus);
3803 brcmf_sdiod_regwl(sdiodev, reg_addr, 0xFFFFFFFF, NULL);
3804
3805 if (rstvec)
3806 /* Write reset vector to address 0 */
3807 brcmf_sdiod_ramrw(sdiodev, true, 0, (void *)&rstvec,
3808 sizeof(rstvec));
3809}
3810
3811static u32 brcmf_sdio_buscore_read32(void *ctx, u32 addr)
3812{
3813 struct brcmf_sdio_dev *sdiodev = ctx;
3814 u32 val, rev;
3815
3816 val = brcmf_sdiod_regrl(sdiodev, addr, NULL);
Arend van Spriel8bd61f82015-01-06 23:02:54 +01003817 if (sdiodev->func[0]->device == SDIO_DEVICE_ID_BROADCOM_4335_4339 &&
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003818 addr == CORE_CC_REG(SI_ENUM_BASE, chipid)) {
3819 rev = (val & CID_REV_MASK) >> CID_REV_SHIFT;
3820 if (rev >= 2) {
3821 val &= ~CID_ID_MASK;
Hante Meuleman5779ae62014-07-12 08:49:34 +02003822 val |= BRCM_CC_4339_CHIP_ID;
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003823 }
3824 }
3825 return val;
3826}
3827
3828static void brcmf_sdio_buscore_write32(void *ctx, u32 addr, u32 val)
3829{
3830 struct brcmf_sdio_dev *sdiodev = ctx;
3831
3832 brcmf_sdiod_regwl(sdiodev, addr, val, NULL);
3833}
3834
3835static const struct brcmf_buscore_ops brcmf_sdio_buscore_ops = {
3836 .prepare = brcmf_sdio_buscoreprep,
Arend van Sprield380ebc2015-03-11 16:11:29 +01003837 .activate = brcmf_sdio_buscore_activate,
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003838 .read32 = brcmf_sdio_buscore_read32,
3839 .write32 = brcmf_sdio_buscore_write32,
3840};
3841
Arend van Spriel5b435de2011-10-05 13:19:03 +02003842static bool
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003843brcmf_sdio_probe_attach(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003844{
3845 u8 clkctl = 0;
3846 int err = 0;
3847 int reg_addr;
3848 u32 reg_val;
Hante Meuleman668761a2013-04-12 10:55:55 +02003849 u32 drivestrength;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003850
Franky Lin38b0b0d2012-11-05 16:22:24 -08003851 sdio_claim_host(bus->sdiodev->func[1]);
3852
Joe Perches18aad4f2012-01-15 00:38:42 -08003853 pr_debug("F1 signature read @0x18000000=0x%4x\n",
Arend van Spriela39be272013-12-12 11:58:58 +01003854 brcmf_sdiod_regrl(bus->sdiodev, SI_ENUM_BASE, NULL));
Arend van Spriel5b435de2011-10-05 13:19:03 +02003855
3856 /*
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003857 * Force PLL off until brcmf_chip_attach()
Arend van Spriel5b435de2011-10-05 13:19:03 +02003858 * programs PLL control regs
3859 */
3860
Arend van Spriela39be272013-12-12 11:58:58 +01003861 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3862 BRCMF_INIT_CLKCTL1, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003863 if (!err)
Arend van Spriela39be272013-12-12 11:58:58 +01003864 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
3865 SBSDIO_FUNC1_CHIPCLKCSR, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003866
3867 if (err || ((clkctl & ~SBSDIO_AVBITS) != BRCMF_INIT_CLKCTL1)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003868 brcmf_err("ChipClkCSR access: err %d wrote 0x%02x read 0x%02x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02003869 err, BRCMF_INIT_CLKCTL1, clkctl);
3870 goto fail;
3871 }
3872
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003873 bus->ci = brcmf_chip_attach(bus->sdiodev, &brcmf_sdio_buscore_ops);
3874 if (IS_ERR(bus->ci)) {
3875 brcmf_err("brcmf_chip_attach failed!\n");
3876 bus->ci = NULL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003877 goto fail;
3878 }
3879
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003880 if (brcmf_sdio_kso_init(bus)) {
Piotr Haber4a3da992013-04-11 13:28:46 +02003881 brcmf_err("error enabling KSO\n");
3882 goto fail;
3883 }
3884
Hante Meuleman668761a2013-04-12 10:55:55 +02003885 if ((bus->sdiodev->pdata) && (bus->sdiodev->pdata->drive_strength))
3886 drivestrength = bus->sdiodev->pdata->drive_strength;
3887 else
3888 drivestrength = DEFAULT_SDIO_DRIVE_STRENGTH;
Arend van Spriel65d80d02014-01-29 15:32:14 +01003889 brcmf_sdio_drivestrengthinit(bus->sdiodev, bus->ci, drivestrength);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003890
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003891 /* Set card control so an SDIO card reset does a WLAN backplane reset */
Arend van Spriela39be272013-12-12 11:58:58 +01003892 reg_val = brcmf_sdiod_regrb(bus->sdiodev,
3893 SDIO_CCCR_BRCM_CARDCTRL, &err);
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003894 if (err)
3895 goto fail;
3896
3897 reg_val |= SDIO_CCCR_BRCM_CARDCTRL_WLANRESET;
3898
Arend van Spriela39be272013-12-12 11:58:58 +01003899 brcmf_sdiod_regwb(bus->sdiodev,
3900 SDIO_CCCR_BRCM_CARDCTRL, reg_val, &err);
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003901 if (err)
3902 goto fail;
3903
3904 /* set PMUControl so a backplane reset does PMU state reload */
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003905 reg_addr = CORE_CC_REG(brcmf_chip_get_chipcommon(bus->ci)->base,
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003906 pmucontrol);
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003907 reg_val = brcmf_sdiod_regrl(bus->sdiodev, reg_addr, &err);
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003908 if (err)
3909 goto fail;
3910
3911 reg_val |= (BCMA_CC_PMU_CTL_RES_RELOAD << BCMA_CC_PMU_CTL_RES_SHIFT);
3912
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003913 brcmf_sdiod_regwl(bus->sdiodev, reg_addr, reg_val, &err);
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003914 if (err)
3915 goto fail;
3916
Franky Lin38b0b0d2012-11-05 16:22:24 -08003917 sdio_release_host(bus->sdiodev->func[1]);
3918
Arend van Spriel5b435de2011-10-05 13:19:03 +02003919 brcmu_pktq_init(&bus->txq, (PRIOMASK + 1), TXQLEN);
3920
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01003921 /* allocate header buffer */
3922 bus->hdrbuf = kzalloc(MAX_HDR_READ + bus->head_align, GFP_KERNEL);
3923 if (!bus->hdrbuf)
3924 return false;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003925 /* Locate an appropriately-aligned portion of hdrbuf */
3926 bus->rxhdr = (u8 *) roundup((unsigned long)&bus->hdrbuf[0],
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01003927 bus->head_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003928
3929 /* Set the poll and/or interrupt flags */
3930 bus->intr = true;
3931 bus->poll = false;
3932 if (bus->poll)
3933 bus->pollrate = 1;
3934
3935 return true;
3936
3937fail:
Franky Lin38b0b0d2012-11-05 16:22:24 -08003938 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003939 return false;
3940}
3941
Arend van Spriel5b435de2011-10-05 13:19:03 +02003942static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003943brcmf_sdio_watchdog_thread(void *data)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003944{
Franky Line92eedf2011-11-22 17:21:50 -08003945 struct brcmf_sdio *bus = (struct brcmf_sdio *)data;
Arend van Spriel99824642015-02-06 18:36:45 +01003946 int wait;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003947
3948 allow_signal(SIGTERM);
3949 /* Run until signal received */
Arend van Spriel99824642015-02-06 18:36:45 +01003950 brcmf_sdiod_freezer_count(bus->sdiodev);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003951 while (1) {
3952 if (kthread_should_stop())
3953 break;
Arend van Spriel99824642015-02-06 18:36:45 +01003954 brcmf_sdiod_freezer_uncount(bus->sdiodev);
3955 wait = wait_for_completion_interruptible(&bus->watchdog_wait);
3956 brcmf_sdiod_freezer_count(bus->sdiodev);
3957 brcmf_sdiod_try_freeze(bus->sdiodev);
3958 if (!wait) {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003959 brcmf_sdio_bus_watchdog(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003960 /* Count the tick for reference */
Arend van Spriel80969832012-06-09 22:51:44 +02003961 bus->sdcnt.tickcnt++;
Arend van Spriel58e9df42014-03-20 10:18:01 +01003962 reinit_completion(&bus->watchdog_wait);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003963 } else
3964 break;
3965 }
3966 return 0;
3967}
3968
3969static void
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003970brcmf_sdio_watchdog(unsigned long data)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003971{
Franky Line92eedf2011-11-22 17:21:50 -08003972 struct brcmf_sdio *bus = (struct brcmf_sdio *)data;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003973
3974 if (bus->watchdog_tsk) {
3975 complete(&bus->watchdog_wait);
3976 /* Reschedule the watchdog */
3977 if (bus->wd_timer_valid)
3978 mod_timer(&bus->timer,
Nicholas Mc Guire187d3c32015-02-06 05:26:45 -05003979 jiffies + msecs_to_jiffies(BRCMF_WD_POLL_MS));
Arend van Spriel5b435de2011-10-05 13:19:03 +02003980 }
3981}
3982
Arend van Sprield9cb2592012-12-05 15:25:54 +01003983static struct brcmf_bus_ops brcmf_sdio_bus_ops = {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003984 .stop = brcmf_sdio_bus_stop,
3985 .preinit = brcmf_sdio_bus_preinit,
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003986 .txdata = brcmf_sdio_bus_txdata,
3987 .txctl = brcmf_sdio_bus_txctl,
3988 .rxctl = brcmf_sdio_bus_rxctl,
3989 .gettxq = brcmf_sdio_bus_gettxq,
Hante Meuleman330b4e42014-10-28 14:56:05 +01003990 .wowl_config = brcmf_sdio_wowl_config
Arend van Sprield9cb2592012-12-05 15:25:54 +01003991};
3992
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003993static void brcmf_sdio_firmware_callback(struct device *dev,
3994 const struct firmware *code,
3995 void *nvram, u32 nvram_len)
3996{
3997 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3998 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3999 struct brcmf_sdio *bus = sdiodev->bus;
4000 int err = 0;
4001 u8 saveclk;
4002
4003 brcmf_dbg(TRACE, "Enter: dev=%s\n", dev_name(dev));
4004
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02004005 if (!bus_if->drvr)
4006 return;
4007
Hante Meulemana1cee862015-01-25 20:31:35 +01004008 /* try to download image and nvram to the dongle */
4009 bus->alp_only = true;
4010 err = brcmf_sdio_download_firmware(bus, code, nvram, nvram_len);
4011 if (err)
4012 goto fail;
4013 bus->alp_only = false;
4014
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02004015 /* Start the watchdog timer */
4016 bus->sdcnt.tickcnt = 0;
4017 brcmf_sdio_wd_timer(bus, BRCMF_WD_POLL_MS);
4018
4019 sdio_claim_host(sdiodev->func[1]);
4020
4021 /* Make sure backplane clock is on, needed to generate F2 interrupt */
4022 brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
4023 if (bus->clkstate != CLK_AVAIL)
4024 goto release;
4025
4026 /* Force clocks on backplane to be sure F2 interrupt propagates */
4027 saveclk = brcmf_sdiod_regrb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, &err);
4028 if (!err) {
4029 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
4030 (saveclk | SBSDIO_FORCE_HT), &err);
4031 }
4032 if (err) {
4033 brcmf_err("Failed to force clock for F2: err %d\n", err);
4034 goto release;
4035 }
4036
4037 /* Enable function 2 (frame transfers) */
4038 w_sdreg32(bus, SDPCM_PROT_VERSION << SMB_DATA_VERSION_SHIFT,
4039 offsetof(struct sdpcmd_regs, tosbmailboxdata));
4040 err = sdio_enable_func(sdiodev->func[SDIO_FUNC_2]);
4041
4042
4043 brcmf_dbg(INFO, "enable F2: err=%d\n", err);
4044
4045 /* If F2 successfully enabled, set core and enable interrupts */
4046 if (!err) {
4047 /* Set up the interrupt mask and enable interrupts */
4048 bus->hostintmask = HOSTINTMASK;
4049 w_sdreg32(bus, bus->hostintmask,
4050 offsetof(struct sdpcmd_regs, hostintmask));
4051
4052 brcmf_sdiod_regwb(sdiodev, SBSDIO_WATERMARK, 8, &err);
4053 } else {
4054 /* Disable F2 again */
4055 sdio_disable_func(sdiodev->func[SDIO_FUNC_2]);
4056 goto release;
4057 }
4058
4059 if (brcmf_chip_sr_capable(bus->ci)) {
4060 brcmf_sdio_sr_init(bus);
4061 } else {
4062 /* Restore previous clock setting */
4063 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
4064 saveclk, &err);
4065 }
4066
4067 if (err == 0) {
4068 err = brcmf_sdiod_intr_register(sdiodev);
4069 if (err != 0)
4070 brcmf_err("intr register failed:%d\n", err);
4071 }
4072
4073 /* If we didn't come up, turn off backplane clock */
4074 if (err != 0)
4075 brcmf_sdio_clkctl(bus, CLK_NONE, false);
4076
4077 sdio_release_host(sdiodev->func[1]);
4078
4079 err = brcmf_bus_start(dev);
4080 if (err != 0) {
4081 brcmf_err("dongle is not responding\n");
4082 goto fail;
4083 }
4084 return;
4085
4086release:
4087 sdio_release_host(sdiodev->func[1]);
4088fail:
4089 brcmf_dbg(TRACE, "failed: dev=%s, err=%d\n", dev_name(dev), err);
4090 device_release_driver(dev);
4091}
4092
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01004093struct brcmf_sdio *brcmf_sdio_probe(struct brcmf_sdio_dev *sdiodev)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004094{
4095 int ret;
Franky Line92eedf2011-11-22 17:21:50 -08004096 struct brcmf_sdio *bus;
Arend van Spriel99824642015-02-06 18:36:45 +01004097 struct workqueue_struct *wq;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004098
Arend van Spriel5b435de2011-10-05 13:19:03 +02004099 brcmf_dbg(TRACE, "Enter\n");
4100
Arend van Spriel5b435de2011-10-05 13:19:03 +02004101 /* Allocate private bus interface state */
Franky Line92eedf2011-11-22 17:21:50 -08004102 bus = kzalloc(sizeof(struct brcmf_sdio), GFP_ATOMIC);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004103 if (!bus)
4104 goto fail;
4105
4106 bus->sdiodev = sdiodev;
4107 sdiodev->bus = bus;
Arend van Sprielb83db862011-10-19 12:51:09 +02004108 skb_queue_head_init(&bus->glom);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004109 bus->txbound = BRCMF_TXBOUND;
4110 bus->rxbound = BRCMF_RXBOUND;
4111 bus->txminmax = BRCMF_TXMINMAX;
Franky Lin6bc52312013-08-10 12:27:28 +02004112 bus->tx_seq = SDPCM_SEQ_WRAP - 1;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004113
Arend van Spriele217d1c2013-11-29 12:25:13 +01004114 /* platform specific configuration:
4115 * alignments must be at least 4 bytes for ADMA
Hante Meuleman888bf76e2014-10-28 14:56:17 +01004116 */
Arend van Spriele217d1c2013-11-29 12:25:13 +01004117 bus->head_align = ALIGNMENT;
4118 bus->sgentry_align = ALIGNMENT;
4119 if (sdiodev->pdata) {
4120 if (sdiodev->pdata->sd_head_align > ALIGNMENT)
4121 bus->head_align = sdiodev->pdata->sd_head_align;
4122 if (sdiodev->pdata->sd_sgentry_align > ALIGNMENT)
4123 bus->sgentry_align = sdiodev->pdata->sd_sgentry_align;
4124 }
4125
Arend van Spriel99824642015-02-06 18:36:45 +01004126 /* single-threaded workqueue */
4127 wq = alloc_ordered_workqueue("brcmf_wq/%s", WQ_MEM_RECLAIM,
4128 dev_name(&sdiodev->func[1]->dev));
4129 if (!wq) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01004130 brcmf_err("insufficient memory to create txworkqueue\n");
Hante Meuleman37ac5782012-11-14 18:46:18 -08004131 goto fail;
4132 }
Arend van Spriel99824642015-02-06 18:36:45 +01004133 brcmf_sdiod_freezer_count(sdiodev);
4134 INIT_WORK(&bus->datawork, brcmf_sdio_dataworker);
4135 bus->brcmf_wq = wq;
Hante Meuleman37ac5782012-11-14 18:46:18 -08004136
Arend van Spriel5b435de2011-10-05 13:19:03 +02004137 /* attempt to attach to the dongle */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01004138 if (!(brcmf_sdio_probe_attach(bus))) {
4139 brcmf_err("brcmf_sdio_probe_attach failed\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02004140 goto fail;
4141 }
4142
Franky Lindd43a012012-11-05 16:22:22 -08004143 spin_lock_init(&bus->rxctl_lock);
Hante Meulemanfed7ec42014-03-15 17:18:18 +01004144 spin_lock_init(&bus->txq_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004145 init_waitqueue_head(&bus->ctrl_wait);
4146 init_waitqueue_head(&bus->dcmd_resp_wait);
4147
4148 /* Set up the watchdog timer */
4149 init_timer(&bus->timer);
4150 bus->timer.data = (unsigned long)bus;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01004151 bus->timer.function = brcmf_sdio_watchdog;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004152
Arend van Spriel5b435de2011-10-05 13:19:03 +02004153 /* Initialize watchdog thread */
4154 init_completion(&bus->watchdog_wait);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01004155 bus->watchdog_tsk = kthread_run(brcmf_sdio_watchdog_thread,
Arend van Spriel99824642015-02-06 18:36:45 +01004156 bus, "brcmf_wdog/%s",
4157 dev_name(&sdiodev->func[1]->dev));
Arend van Spriel5b435de2011-10-05 13:19:03 +02004158 if (IS_ERR(bus->watchdog_tsk)) {
Joe Perches02f77192012-01-15 00:38:44 -08004159 pr_warn("brcmf_watchdog thread failed to start\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02004160 bus->watchdog_tsk = NULL;
4161 }
4162 /* Initialize DPC thread */
Hante Meuleman2c64e162015-03-18 13:25:22 +01004163 bus->dpc_triggered = false;
4164 bus->dpc_running = false;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004165
Franky Lina9ffda82011-12-16 18:37:12 -08004166 /* Assign bus interface call back */
Arend van Sprield9cb2592012-12-05 15:25:54 +01004167 bus->sdiodev->bus_if->dev = bus->sdiodev->dev;
4168 bus->sdiodev->bus_if->ops = &brcmf_sdio_bus_ops;
Arend van Spriel75d907d2013-02-06 18:40:45 +01004169 bus->sdiodev->bus_if->chip = bus->ci->chip;
4170 bus->sdiodev->bus_if->chiprev = bus->ci->chiprev;
Arend van Sprield9cb2592012-12-05 15:25:54 +01004171
Franky Lin706478c2013-08-10 12:27:29 +02004172 /* default sdio bus header length for tx packet */
4173 bus->tx_hdrlen = SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN;
4174
4175 /* Attach to the common layer, reserve hdr space */
Arend van Spriel8dee77b2013-11-29 11:48:14 +01004176 ret = brcmf_attach(bus->sdiodev->dev);
Franky Lin712ac5b2011-12-16 18:37:09 -08004177 if (ret != 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01004178 brcmf_err("brcmf_attach failed\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02004179 goto fail;
4180 }
4181
Daniel Kim7dd3abc2014-05-27 12:56:26 +02004182 /* Query the F2 block size, set roundup accordingly */
4183 bus->blocksize = bus->sdiodev->func[2]->cur_blksize;
4184 bus->roundup = min(max_roundup, bus->blocksize);
4185
Arend van Spriel5b435de2011-10-05 13:19:03 +02004186 /* Allocate buffers */
Arend van Sprielfad13222014-01-06 12:40:40 +01004187 if (bus->sdiodev->bus_if->maxctl) {
Daniel Kim7dd3abc2014-05-27 12:56:26 +02004188 bus->sdiodev->bus_if->maxctl += bus->roundup;
Arend van Sprielfad13222014-01-06 12:40:40 +01004189 bus->rxblen =
4190 roundup((bus->sdiodev->bus_if->maxctl + SDPCM_HDRLEN),
4191 ALIGNMENT) + bus->head_align;
4192 bus->rxbuf = kmalloc(bus->rxblen, GFP_ATOMIC);
4193 if (!(bus->rxbuf)) {
4194 brcmf_err("rxbuf allocation failed\n");
4195 goto fail;
4196 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02004197 }
4198
Arend van Sprielfad13222014-01-06 12:40:40 +01004199 sdio_claim_host(bus->sdiodev->func[1]);
4200
4201 /* Disable F2 to clear any intermediate frame state on the dongle */
4202 sdio_disable_func(bus->sdiodev->func[SDIO_FUNC_2]);
4203
Arend van Sprielfad13222014-01-06 12:40:40 +01004204 bus->rxflow = false;
4205
4206 /* Done with backplane-dependent accesses, can drop clock... */
4207 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, 0, NULL);
4208
4209 sdio_release_host(bus->sdiodev->func[1]);
4210
4211 /* ...and initialize clock/power states */
4212 bus->clkstate = CLK_SDONLY;
4213 bus->idletime = BRCMF_IDLE_INTERVAL;
4214 bus->idleclock = BRCMF_IDLE_ACTIVE;
4215
Arend van Sprielfad13222014-01-06 12:40:40 +01004216 /* SR state */
Arend van Sprielfad13222014-01-06 12:40:40 +01004217 bus->sr_enabled = false;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004218
Arend van Spriel80969832012-06-09 22:51:44 +02004219 brcmf_sdio_debugfs_create(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004220 brcmf_dbg(INFO, "completed!!\n");
4221
Daniel Kimc1b20532014-07-12 08:49:37 +02004222 ret = brcmf_sdio_get_fwnames(bus->ci, sdiodev);
4223 if (ret)
4224 goto fail;
4225
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02004226 ret = brcmf_fw_get_firmwares(sdiodev->dev, BRCMF_FW_REQUEST_NVRAM,
Daniel Kimc1b20532014-07-12 08:49:37 +02004227 sdiodev->fw_name, sdiodev->nvram_name,
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02004228 brcmf_sdio_firmware_callback);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004229 if (ret != 0) {
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02004230 brcmf_err("async firmware request failed: %d\n", ret);
Hante Meuleman1799ddf2012-11-14 18:46:17 -08004231 goto fail;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004232 }
Franky Lin15d45b62011-10-21 16:16:32 +02004233
Arend van Spriel5b435de2011-10-05 13:19:03 +02004234 return bus;
4235
4236fail:
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004237 brcmf_sdio_remove(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004238 return NULL;
4239}
4240
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004241/* Detach and free everything */
4242void brcmf_sdio_remove(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004243{
Arend van Spriel5b435de2011-10-05 13:19:03 +02004244 brcmf_dbg(TRACE, "Enter\n");
4245
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004246 if (bus) {
4247 /* De-register interrupt handler */
4248 brcmf_sdiod_intr_unregister(bus->sdiodev);
4249
Arend van Spriel4faf28b2014-05-27 12:56:18 +02004250 brcmf_detach(bus->sdiodev->dev);
Arend van Sprielbfad4a02014-01-06 12:40:39 +01004251
Hante Meulemane0c180ec2014-01-29 15:32:18 +01004252 cancel_work_sync(&bus->datawork);
4253 if (bus->brcmf_wq)
4254 destroy_workqueue(bus->brcmf_wq);
4255
Arend van Sprielbfad4a02014-01-06 12:40:39 +01004256 if (bus->ci) {
Arend van Spriela1ce7a02015-02-06 18:36:42 +01004257 if (bus->sdiodev->state != BRCMF_SDIOD_NOMEDIUM) {
Arend van Sprielbb350712014-01-13 22:20:29 +01004258 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Sprielcf459322015-03-11 16:11:33 +01004259 brcmf_sdio_wd_timer(bus, 0);
Arend van Sprielbb350712014-01-13 22:20:29 +01004260 brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
4261 /* Leave the device in state where it is
Arend van Sprield380ebc2015-03-11 16:11:29 +01004262 * 'passive'. This is done by resetting all
4263 * necessary cores.
Arend van Sprielbb350712014-01-13 22:20:29 +01004264 */
4265 msleep(20);
Arend van Sprield380ebc2015-03-11 16:11:29 +01004266 brcmf_chip_set_passive(bus->ci);
Arend van Sprielbb350712014-01-13 22:20:29 +01004267 brcmf_sdio_clkctl(bus, CLK_NONE, false);
4268 sdio_release_host(bus->sdiodev->func[1]);
4269 }
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01004270 brcmf_chip_detach(bus->ci);
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004271 }
4272
Arend van Sprielbfad4a02014-01-06 12:40:39 +01004273 kfree(bus->rxbuf);
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004274 kfree(bus->hdrbuf);
4275 kfree(bus);
4276 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02004277
4278 brcmf_dbg(TRACE, "Disconnected\n");
4279}
4280
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01004281void brcmf_sdio_wd_timer(struct brcmf_sdio *bus, uint wdtick)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004282{
Arend van Spriel5b435de2011-10-05 13:19:03 +02004283 /* Totally stop the timer */
Joe Perches23677ce2012-02-09 11:17:23 +00004284 if (!wdtick && bus->wd_timer_valid) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02004285 del_timer_sync(&bus->timer);
4286 bus->wd_timer_valid = false;
4287 bus->save_ms = wdtick;
4288 return;
4289 }
4290
Franky Linece960e2011-10-21 16:16:19 +02004291 /* don't start the wd until fw is loaded */
Arend van Spriela1ce7a02015-02-06 18:36:42 +01004292 if (bus->sdiodev->state != BRCMF_SDIOD_DATA)
Franky Linece960e2011-10-21 16:16:19 +02004293 return;
4294
Arend van Spriel5b435de2011-10-05 13:19:03 +02004295 if (wdtick) {
4296 if (bus->save_ms != BRCMF_WD_POLL_MS) {
Joe Perches23677ce2012-02-09 11:17:23 +00004297 if (bus->wd_timer_valid)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004298 /* Stop timer and restart at new value */
4299 del_timer_sync(&bus->timer);
4300
4301 /* Create timer again when watchdog period is
4302 dynamically changed or in the first instance
4303 */
4304 bus->timer.expires =
Nicholas Mc Guire187d3c32015-02-06 05:26:45 -05004305 jiffies + msecs_to_jiffies(BRCMF_WD_POLL_MS);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004306 add_timer(&bus->timer);
4307
4308 } else {
4309 /* Re arm the timer, at last watchdog period */
4310 mod_timer(&bus->timer,
Nicholas Mc Guire187d3c32015-02-06 05:26:45 -05004311 jiffies + msecs_to_jiffies(BRCMF_WD_POLL_MS));
Arend van Spriel5b435de2011-10-05 13:19:03 +02004312 }
4313
4314 bus->wd_timer_valid = true;
4315 bus->save_ms = wdtick;
4316 }
4317}
Arend van Spriel99824642015-02-06 18:36:45 +01004318
4319int brcmf_sdio_sleep(struct brcmf_sdio *bus, bool sleep)
4320{
4321 int ret;
4322
4323 sdio_claim_host(bus->sdiodev->func[1]);
4324 ret = brcmf_sdio_bus_sleep(bus, sleep, false);
4325 sdio_release_host(bus->sdiodev->func[1]);
4326
4327 return ret;
4328}
4329