blob: 9fa3a3a0f12c69182025f8cc33051be0e3c81421 [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
464 u8 *rxbuf; /* Buffer for receiving control packets */
465 uint rxblen; /* Allocated length of rxbuf */
466 u8 *rxctl; /* Aligned pointer into rxbuf */
Franky Lindd43a012012-11-05 16:22:22 -0800467 u8 *rxctl_orig; /* pointer for freeing rxctl */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200468 uint rxlen; /* Length of valid data in buffer */
Franky Lindd43a012012-11-05 16:22:22 -0800469 spinlock_t rxctl_lock; /* protection lock for ctrl frame resources */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200470
471 u8 sdpcm_ver; /* Bus protocol reported by dongle */
472
473 bool intr; /* Use interrupts */
474 bool poll; /* Use polling */
Franky Lin1d382272012-09-13 21:11:59 +0200475 atomic_t ipend; /* Device interrupt is pending */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200476 uint spurious; /* Count of spurious interrupts */
477 uint pollrate; /* Ticks between device polls */
478 uint polltick; /* Tick counter */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200479
Joe Perches8ae74652012-01-15 00:38:38 -0800480#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200481 uint console_interval;
482 struct brcmf_console console; /* Console output polling support */
483 uint console_addr; /* Console address from shared struct */
Joe Perches8ae74652012-01-15 00:38:38 -0800484#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200485
Arend van Spriel5b435de2011-10-05 13:19:03 +0200486 uint clkstate; /* State of sd and backplane clock(s) */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200487 s32 idletime; /* Control for activity timeout */
Hante Meulemanb441ba82015-03-06 18:40:39 +0100488 s32 idlecount; /* Activity timeout counter */
489 s32 idleclock; /* How to set bus driver when idle */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200490 bool rxflow_mode; /* Rx flow control mode */
491 bool rxflow; /* Is rx flow control on */
492 bool alp_only; /* Don't use HT clock (ALP only) */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200493
Arend van Spriel5b435de2011-10-05 13:19:03 +0200494 u8 *ctrl_frame_buf;
Hante Meulemanfed7ec42014-03-15 17:18:18 +0100495 u16 ctrl_frame_len;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200496 bool ctrl_frame_stat;
Hante Meuleman4dd8b262015-01-25 20:31:32 +0100497 int ctrl_frame_err;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200498
Hante Meulemanfed7ec42014-03-15 17:18:18 +0100499 spinlock_t txq_lock; /* protect bus->txq */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200500 wait_queue_head_t ctrl_wait;
501 wait_queue_head_t dcmd_resp_wait;
502
503 struct timer_list timer;
504 struct completion watchdog_wait;
505 struct task_struct *watchdog_tsk;
506 bool wd_timer_valid;
507 uint save_ms;
508
Franky Linf1e68c22012-09-13 21:12:00 +0200509 struct workqueue_struct *brcmf_wq;
510 struct work_struct datawork;
Hante Meuleman2c64e162015-03-18 13:25:22 +0100511 bool dpc_triggered;
512 bool dpc_running;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200513
Franky Linc8bf3482011-12-16 18:37:08 -0800514 bool txoff; /* Transmit flow-controlled */
Arend van Spriel80969832012-06-09 22:51:44 +0200515 struct brcmf_sdio_count sdcnt;
Piotr Haber4a3da992013-04-11 13:28:46 +0200516 bool sr_enabled; /* SaveRestore enabled */
Arend van Spriel99824642015-02-06 18:36:45 +0100517 bool sleeping;
Franky Lin706478c2013-08-10 12:27:29 +0200518
519 u8 tx_hdrlen; /* sdio bus header length for tx packet */
Franky Lin8da9d2c2013-11-29 12:25:10 +0100520 bool txglom; /* host tx glomming enable flag */
Arend van Spriele217d1c2013-11-29 12:25:13 +0100521 u16 head_align; /* buffer pointer alignment */
522 u16 sgentry_align; /* scatter-gather buffer alignment */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200523};
524
Arend van Spriel5b435de2011-10-05 13:19:03 +0200525/* clkstate */
526#define CLK_NONE 0
527#define CLK_SDONLY 1
Piotr Haber4a3da992013-04-11 13:28:46 +0200528#define CLK_PENDING 2
Arend van Spriel5b435de2011-10-05 13:19:03 +0200529#define CLK_AVAIL 3
530
Joe Perches8ae74652012-01-15 00:38:38 -0800531#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200532static int qcount[NUMPRIO];
Joe Perches8ae74652012-01-15 00:38:38 -0800533#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200534
Hante Meuleman668761a2013-04-12 10:55:55 +0200535#define DEFAULT_SDIO_DRIVE_STRENGTH 6 /* in milliamps */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200536
537#define RETRYCHAN(chan) ((chan) == SDPCM_EVENT_CHANNEL)
538
539/* Retry count for register access failures */
540static const uint retry_limit = 2;
541
542/* Limit on rounding up frames */
543static const uint max_roundup = 512;
544
545#define ALIGNMENT 4
546
Franky Lin9d7d6f92012-10-22 10:36:24 -0700547enum brcmf_sdio_frmtype {
548 BRCMF_SDIO_FT_NORMAL,
549 BRCMF_SDIO_FT_SUPER,
550 BRCMF_SDIO_FT_SUB,
551};
552
Arend van Spriel65d80d02014-01-29 15:32:14 +0100553#define SDIOD_DRVSTR_KEY(chip, pmu) (((chip) << 16) | (pmu))
554
555/* SDIO Pad drive strength to select value mappings */
556struct sdiod_drive_str {
557 u8 strength; /* Pad Drive Strength in mA */
558 u8 sel; /* Chip-specific select value */
559};
560
561/* SDIO Drive Strength to sel value table for PMU Rev 11 (1.8V) */
562static const struct sdiod_drive_str sdiod_drvstr_tab1_1v8[] = {
563 {32, 0x6},
564 {26, 0x7},
565 {22, 0x4},
566 {16, 0x5},
567 {12, 0x2},
568 {8, 0x3},
569 {4, 0x0},
570 {0, 0x1}
571};
572
573/* SDIO Drive Strength to sel value table for PMU Rev 13 (1.8v) */
574static const struct sdiod_drive_str sdiod_drive_strength_tab5_1v8[] = {
575 {6, 0x7},
576 {5, 0x6},
577 {4, 0x5},
578 {3, 0x4},
579 {2, 0x2},
580 {1, 0x1},
581 {0, 0x0}
582};
583
584/* SDIO Drive Strength to sel value table for PMU Rev 17 (1.8v) */
585static const struct sdiod_drive_str sdiod_drvstr_tab6_1v8[] = {
586 {3, 0x3},
587 {2, 0x2},
588 {1, 0x1},
589 {0, 0x0} };
590
591/* SDIO Drive Strength to sel value table for 43143 PMU Rev 17 (3.3V) */
592static const struct sdiod_drive_str sdiod_drvstr_tab2_3v3[] = {
593 {16, 0x7},
594 {12, 0x5},
595 {8, 0x3},
596 {4, 0x1}
597};
598
Hante Meuleman46d703a2015-11-25 11:32:46 +0100599BRCMF_FW_NVRAM_DEF(43143, "brcmfmac43143-sdio.bin", "brcmfmac43143-sdio.txt");
600BRCMF_FW_NVRAM_DEF(43241B0, "brcmfmac43241b0-sdio.bin",
601 "brcmfmac43241b0-sdio.txt");
602BRCMF_FW_NVRAM_DEF(43241B4, "brcmfmac43241b4-sdio.bin",
603 "brcmfmac43241b4-sdio.txt");
604BRCMF_FW_NVRAM_DEF(43241B5, "brcmfmac43241b5-sdio.bin",
605 "brcmfmac43241b5-sdio.txt");
606BRCMF_FW_NVRAM_DEF(4329, "brcmfmac4329-sdio.bin", "brcmfmac4329-sdio.txt");
607BRCMF_FW_NVRAM_DEF(4330, "brcmfmac4330-sdio.bin", "brcmfmac4330-sdio.txt");
608BRCMF_FW_NVRAM_DEF(4334, "brcmfmac4334-sdio.bin", "brcmfmac4334-sdio.txt");
609BRCMF_FW_NVRAM_DEF(43340, "brcmfmac43340-sdio.bin", "brcmfmac43340-sdio.txt");
610BRCMF_FW_NVRAM_DEF(4335, "brcmfmac4335-sdio.bin", "brcmfmac4335-sdio.txt");
611BRCMF_FW_NVRAM_DEF(43362, "brcmfmac43362-sdio.bin", "brcmfmac43362-sdio.txt");
612BRCMF_FW_NVRAM_DEF(4339, "brcmfmac4339-sdio.bin", "brcmfmac4339-sdio.txt");
613BRCMF_FW_NVRAM_DEF(43430, "brcmfmac43430-sdio.bin", "brcmfmac43430-sdio.txt");
614BRCMF_FW_NVRAM_DEF(43455, "brcmfmac43455-sdio.bin", "brcmfmac43455-sdio.txt");
615BRCMF_FW_NVRAM_DEF(4354, "brcmfmac4354-sdio.bin", "brcmfmac4354-sdio.txt");
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200616
Hante Meuleman46d703a2015-11-25 11:32:46 +0100617static struct brcmf_firmware_mapping brcmf_sdio_fwnames[] = {
618 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_43143_CHIP_ID, 0xFFFFFFFF, 43143),
619 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_43241_CHIP_ID, 0x0000001F, 43241B0),
620 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_43241_CHIP_ID, 0x00000020, 43241B4),
621 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_43241_CHIP_ID, 0xFFFFFFC0, 43241B5),
622 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_4329_CHIP_ID, 0xFFFFFFFF, 4329),
623 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_4330_CHIP_ID, 0xFFFFFFFF, 4330),
624 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_4334_CHIP_ID, 0xFFFFFFFF, 4334),
625 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_43340_CHIP_ID, 0xFFFFFFFF, 43340),
626 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_4335_CHIP_ID, 0xFFFFFFFF, 4335),
627 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_43362_CHIP_ID, 0xFFFFFFFE, 43362),
628 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_4339_CHIP_ID, 0xFFFFFFFF, 4339),
629 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_43430_CHIP_ID, 0xFFFFFFFF, 43430),
630 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_4345_CHIP_ID, 0xFFFFFFC0, 43455),
631 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_4354_CHIP_ID, 0xFFFFFFFF, 4354)
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200632};
633
Arend van Spriel5b435de2011-10-05 13:19:03 +0200634static void pkt_align(struct sk_buff *p, int len, int align)
635{
636 uint datalign;
637 datalign = (unsigned long)(p->data);
638 datalign = roundup(datalign, (align)) - datalign;
639 if (datalign)
640 skb_pull(p, datalign);
641 __skb_trim(p, len);
642}
643
644/* To check if there's window offered */
Franky Line92eedf2011-11-22 17:21:50 -0800645static bool data_ok(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200646{
647 return (u8)(bus->tx_max - bus->tx_seq) != 0 &&
648 ((u8)(bus->tx_max - bus->tx_seq) & 0x80) == 0;
649}
650
651/*
652 * Reads a register in the SDIO hardware block. This block occupies a series of
653 * adresses on the 32 bit backplane bus.
654 */
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100655static int r_sdreg32(struct brcmf_sdio *bus, u32 *regvar, u32 offset)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200656{
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100657 struct brcmf_core *core;
Franky Lin79ae3952012-05-04 18:27:34 -0700658 int ret;
Franky Lin58692752012-05-04 18:27:36 -0700659
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100660 core = brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV);
661 *regvar = brcmf_sdiod_regrl(bus->sdiodev, core->base + offset, &ret);
Franky Lin58692752012-05-04 18:27:36 -0700662
663 return ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200664}
665
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100666static int w_sdreg32(struct brcmf_sdio *bus, u32 regval, u32 reg_offset)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200667{
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100668 struct brcmf_core *core;
Franky Line13ce262012-05-04 18:27:35 -0700669 int ret;
Franky Lin58692752012-05-04 18:27:36 -0700670
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100671 core = brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV);
672 brcmf_sdiod_regwl(bus->sdiodev, core->base + reg_offset, regval, &ret);
Franky Lin58692752012-05-04 18:27:36 -0700673
674 return ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200675}
676
Piotr Haber4a3da992013-04-11 13:28:46 +0200677static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100678brcmf_sdio_kso_control(struct brcmf_sdio *bus, bool on)
Piotr Haber4a3da992013-04-11 13:28:46 +0200679{
680 u8 wr_val = 0, rd_val, cmp_val, bmask;
681 int err = 0;
682 int try_cnt = 0;
683
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100684 brcmf_dbg(TRACE, "Enter: on=%d\n", on);
Piotr Haber4a3da992013-04-11 13:28:46 +0200685
686 wr_val = (on << SBSDIO_FUNC1_SLEEPCSR_KSO_SHIFT);
687 /* 1st KSO write goes to AOS wake up core if device is asleep */
Arend van Spriela39be272013-12-12 11:58:58 +0100688 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
689 wr_val, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +0200690
691 if (on) {
692 /* device WAKEUP through KSO:
693 * write bit 0 & read back until
694 * both bits 0 (kso bit) & 1 (dev on status) are set
695 */
696 cmp_val = SBSDIO_FUNC1_SLEEPCSR_KSO_MASK |
697 SBSDIO_FUNC1_SLEEPCSR_DEVON_MASK;
698 bmask = cmp_val;
699 usleep_range(2000, 3000);
700 } else {
701 /* Put device to sleep, turn off KSO */
702 cmp_val = 0;
703 /* only check for bit0, bit1(dev on status) may not
704 * get cleared right away
705 */
706 bmask = SBSDIO_FUNC1_SLEEPCSR_KSO_MASK;
707 }
708
709 do {
710 /* reliable KSO bit set/clr:
711 * the sdiod sleep write access is synced to PMU 32khz clk
712 * just one write attempt may fail,
713 * read it back until it matches written value
714 */
Arend van Spriela39be272013-12-12 11:58:58 +0100715 rd_val = brcmf_sdiod_regrb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
716 &err);
Piotr Haber4a3da992013-04-11 13:28:46 +0200717 if (((rd_val & bmask) == cmp_val) && !err)
718 break;
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100719
Piotr Haber4a3da992013-04-11 13:28:46 +0200720 udelay(KSO_WAIT_US);
Arend van Spriela39be272013-12-12 11:58:58 +0100721 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
722 wr_val, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +0200723 } while (try_cnt++ < MAX_KSO_ATTEMPTS);
724
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100725 if (try_cnt > 2)
726 brcmf_dbg(SDIO, "try_cnt=%d rd_val=0x%x err=%d\n", try_cnt,
727 rd_val, err);
728
729 if (try_cnt > MAX_KSO_ATTEMPTS)
730 brcmf_err("max tries: rd_val=0x%x err=%d\n", rd_val, err);
731
Piotr Haber4a3da992013-04-11 13:28:46 +0200732 return err;
733}
734
Arend van Spriel5b435de2011-10-05 13:19:03 +0200735#define HOSTINTMASK (I_HMB_SW_MASK | I_CHIPACTIVE)
736
Arend van Spriel5b435de2011-10-05 13:19:03 +0200737/* Turn backplane clock on or off */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100738static int brcmf_sdio_htclk(struct brcmf_sdio *bus, bool on, bool pendok)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200739{
740 int err;
741 u8 clkctl, clkreq, devctl;
742 unsigned long timeout;
743
Arend van Sprielc3203372013-04-03 12:40:44 +0200744 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200745
746 clkctl = 0;
747
Piotr Haber4a3da992013-04-11 13:28:46 +0200748 if (bus->sr_enabled) {
749 bus->clkstate = (on ? CLK_AVAIL : CLK_SDONLY);
750 return 0;
751 }
752
Arend van Spriel5b435de2011-10-05 13:19:03 +0200753 if (on) {
754 /* Request HT Avail */
755 clkreq =
756 bus->alp_only ? SBSDIO_ALP_AVAIL_REQ : SBSDIO_HT_AVAIL_REQ;
757
Arend van Spriela39be272013-12-12 11:58:58 +0100758 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
759 clkreq, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200760 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100761 brcmf_err("HT Avail request error: %d\n", err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200762 return -EBADE;
763 }
764
Arend van Spriel5b435de2011-10-05 13:19:03 +0200765 /* Check current status */
Arend van Spriela39be272013-12-12 11:58:58 +0100766 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
767 SBSDIO_FUNC1_CHIPCLKCSR, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200768 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100769 brcmf_err("HT Avail read error: %d\n", err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200770 return -EBADE;
771 }
772
773 /* Go to pending and await interrupt if appropriate */
774 if (!SBSDIO_CLKAV(clkctl, bus->alp_only) && pendok) {
775 /* Allow only clock-available interrupt */
Arend van Spriela39be272013-12-12 11:58:58 +0100776 devctl = brcmf_sdiod_regrb(bus->sdiodev,
777 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200778 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100779 brcmf_err("Devctl error setting CA: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200780 err);
781 return -EBADE;
782 }
783
784 devctl |= SBSDIO_DEVCTL_CA_INT_ONLY;
Arend van Spriela39be272013-12-12 11:58:58 +0100785 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
786 devctl, &err);
Arend van Sprielc3203372013-04-03 12:40:44 +0200787 brcmf_dbg(SDIO, "CLKCTL: set PENDING\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200788 bus->clkstate = CLK_PENDING;
789
790 return 0;
791 } else if (bus->clkstate == CLK_PENDING) {
792 /* Cancel CA-only interrupt filter */
Arend van Spriela39be272013-12-12 11:58:58 +0100793 devctl = brcmf_sdiod_regrb(bus->sdiodev,
794 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200795 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
Arend van Spriela39be272013-12-12 11:58:58 +0100796 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
797 devctl, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200798 }
799
800 /* Otherwise, wait here (polling) for HT Avail */
801 timeout = jiffies +
802 msecs_to_jiffies(PMU_MAX_TRANSITION_DLY/1000);
803 while (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
Arend van Spriela39be272013-12-12 11:58:58 +0100804 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
805 SBSDIO_FUNC1_CHIPCLKCSR,
806 &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200807 if (time_after(jiffies, timeout))
808 break;
809 else
810 usleep_range(5000, 10000);
811 }
812 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100813 brcmf_err("HT Avail request error: %d\n", err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200814 return -EBADE;
815 }
816 if (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100817 brcmf_err("HT Avail timeout (%d): clkctl 0x%02x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200818 PMU_MAX_TRANSITION_DLY, clkctl);
819 return -EBADE;
820 }
821
822 /* Mark clock available */
823 bus->clkstate = CLK_AVAIL;
Arend van Sprielc3203372013-04-03 12:40:44 +0200824 brcmf_dbg(SDIO, "CLKCTL: turned ON\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200825
Joe Perches8ae74652012-01-15 00:38:38 -0800826#if defined(DEBUG)
Joe Perches23677ce2012-02-09 11:17:23 +0000827 if (!bus->alp_only) {
Arend van Spriel5b435de2011-10-05 13:19:03 +0200828 if (SBSDIO_ALPONLY(clkctl))
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100829 brcmf_err("HT Clock should be on\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200830 }
Joe Perches8ae74652012-01-15 00:38:38 -0800831#endif /* defined (DEBUG) */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200832
Arend van Spriel5b435de2011-10-05 13:19:03 +0200833 } else {
834 clkreq = 0;
835
836 if (bus->clkstate == CLK_PENDING) {
837 /* Cancel CA-only interrupt filter */
Arend van Spriela39be272013-12-12 11:58:58 +0100838 devctl = brcmf_sdiod_regrb(bus->sdiodev,
839 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200840 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
Arend van Spriela39be272013-12-12 11:58:58 +0100841 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
842 devctl, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200843 }
844
845 bus->clkstate = CLK_SDONLY;
Arend van Spriela39be272013-12-12 11:58:58 +0100846 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
847 clkreq, &err);
Arend van Sprielc3203372013-04-03 12:40:44 +0200848 brcmf_dbg(SDIO, "CLKCTL: turned OFF\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200849 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100850 brcmf_err("Failed access turning clock off: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200851 err);
852 return -EBADE;
853 }
854 }
855 return 0;
856}
857
858/* Change idle/active SD state */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100859static int brcmf_sdio_sdclk(struct brcmf_sdio *bus, bool on)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200860{
Arend van Sprielc3203372013-04-03 12:40:44 +0200861 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200862
863 if (on)
864 bus->clkstate = CLK_SDONLY;
865 else
866 bus->clkstate = CLK_NONE;
867
868 return 0;
869}
870
871/* Transition SD and backplane clock readiness */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100872static int brcmf_sdio_clkctl(struct brcmf_sdio *bus, uint target, bool pendok)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200873{
Joe Perches8ae74652012-01-15 00:38:38 -0800874#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200875 uint oldstate = bus->clkstate;
Joe Perches8ae74652012-01-15 00:38:38 -0800876#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200877
Arend van Sprielc3203372013-04-03 12:40:44 +0200878 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200879
880 /* Early exit if we're already there */
Hante Meulemanb441ba82015-03-06 18:40:39 +0100881 if (bus->clkstate == target)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200882 return 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200883
884 switch (target) {
885 case CLK_AVAIL:
886 /* Make sure SD clock is available */
887 if (bus->clkstate == CLK_NONE)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100888 brcmf_sdio_sdclk(bus, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200889 /* Now request HT Avail on the backplane */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100890 brcmf_sdio_htclk(bus, true, pendok);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200891 break;
892
893 case CLK_SDONLY:
894 /* Remove HT request, or bring up SD clock */
895 if (bus->clkstate == CLK_NONE)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100896 brcmf_sdio_sdclk(bus, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200897 else if (bus->clkstate == CLK_AVAIL)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100898 brcmf_sdio_htclk(bus, false, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200899 else
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100900 brcmf_err("request for %d -> %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200901 bus->clkstate, target);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200902 break;
903
904 case CLK_NONE:
905 /* Make sure to remove HT request */
906 if (bus->clkstate == CLK_AVAIL)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100907 brcmf_sdio_htclk(bus, false, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200908 /* Now remove the SD clock */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100909 brcmf_sdio_sdclk(bus, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200910 break;
911 }
Joe Perches8ae74652012-01-15 00:38:38 -0800912#ifdef DEBUG
Arend van Sprielc3203372013-04-03 12:40:44 +0200913 brcmf_dbg(SDIO, "%d -> %d\n", oldstate, bus->clkstate);
Joe Perches8ae74652012-01-15 00:38:38 -0800914#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200915
916 return 0;
917}
918
Piotr Haber4a3da992013-04-11 13:28:46 +0200919static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100920brcmf_sdio_bus_sleep(struct brcmf_sdio *bus, bool sleep, bool pendok)
Piotr Haber4a3da992013-04-11 13:28:46 +0200921{
922 int err = 0;
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100923 u8 clkcsr;
Arend van Spriel82030d62014-01-29 15:32:20 +0100924
925 brcmf_dbg(SDIO, "Enter: request %s currently %s\n",
Piotr Haber4a3da992013-04-11 13:28:46 +0200926 (sleep ? "SLEEP" : "WAKE"),
Arend van Spriel99824642015-02-06 18:36:45 +0100927 (bus->sleeping ? "SLEEP" : "WAKE"));
Piotr Haber4a3da992013-04-11 13:28:46 +0200928
929 /* If SR is enabled control bus state with KSO */
930 if (bus->sr_enabled) {
931 /* Done if we're already in the requested state */
Arend van Spriel99824642015-02-06 18:36:45 +0100932 if (sleep == bus->sleeping)
Piotr Haber4a3da992013-04-11 13:28:46 +0200933 goto end;
934
935 /* Going to sleep */
936 if (sleep) {
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100937 clkcsr = brcmf_sdiod_regrb(bus->sdiodev,
938 SBSDIO_FUNC1_CHIPCLKCSR,
939 &err);
940 if ((clkcsr & SBSDIO_CSR_MASK) == 0) {
941 brcmf_dbg(SDIO, "no clock, set ALP\n");
942 brcmf_sdiod_regwb(bus->sdiodev,
943 SBSDIO_FUNC1_CHIPCLKCSR,
944 SBSDIO_ALP_AVAIL_REQ, &err);
945 }
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100946 err = brcmf_sdio_kso_control(bus, false);
Piotr Haber4a3da992013-04-11 13:28:46 +0200947 } else {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100948 err = brcmf_sdio_kso_control(bus, true);
Piotr Haber4a3da992013-04-11 13:28:46 +0200949 }
Arend van Spriel8982cd42015-01-25 20:31:34 +0100950 if (err) {
Piotr Haber4a3da992013-04-11 13:28:46 +0200951 brcmf_err("error while changing bus sleep state %d\n",
952 err);
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100953 goto done;
Piotr Haber4a3da992013-04-11 13:28:46 +0200954 }
955 }
956
957end:
958 /* control clocks */
959 if (sleep) {
960 if (!bus->sr_enabled)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100961 brcmf_sdio_clkctl(bus, CLK_NONE, pendok);
Piotr Haber4a3da992013-04-11 13:28:46 +0200962 } else {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100963 brcmf_sdio_clkctl(bus, CLK_AVAIL, pendok);
Arend van Sprielcf459322015-03-11 16:11:33 +0100964 brcmf_sdio_wd_timer(bus, BRCMF_WD_POLL_MS);
Piotr Haber4a3da992013-04-11 13:28:46 +0200965 }
Arend van Spriel99824642015-02-06 18:36:45 +0100966 bus->sleeping = sleep;
Arend van Spriel8982cd42015-01-25 20:31:34 +0100967 brcmf_dbg(SDIO, "new state %s\n",
968 (sleep ? "SLEEP" : "WAKE"));
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100969done:
970 brcmf_dbg(SDIO, "Exit: err=%d\n", err);
Piotr Haber4a3da992013-04-11 13:28:46 +0200971 return err;
972
973}
974
Daniel Kim0801e6c2014-01-29 15:32:13 +0100975#ifdef DEBUG
976static inline bool brcmf_sdio_valid_shared_address(u32 addr)
977{
978 return !(addr == 0 || ((~addr >> 16) & 0xffff) == (addr & 0xffff));
979}
980
981static int brcmf_sdio_readshared(struct brcmf_sdio *bus,
982 struct sdpcm_shared *sh)
983{
Arend van Spriel9819a902015-03-11 16:11:32 +0100984 u32 addr = 0;
Daniel Kim0801e6c2014-01-29 15:32:13 +0100985 int rv;
986 u32 shaddr = 0;
987 struct sdpcm_shared_le sh_le;
988 __le32 addr_le;
989
Arend van Spriel9819a902015-03-11 16:11:32 +0100990 sdio_claim_host(bus->sdiodev->func[1]);
991 brcmf_sdio_bus_sleep(bus, false, false);
Daniel Kim0801e6c2014-01-29 15:32:13 +0100992
993 /*
994 * Read last word in socram to determine
995 * address of sdpcm_shared structure
996 */
Arend van Spriel9819a902015-03-11 16:11:32 +0100997 shaddr = bus->ci->rambase + bus->ci->ramsize - 4;
998 if (!bus->ci->rambase && brcmf_chip_sr_capable(bus->ci))
999 shaddr -= bus->ci->srsize;
1000 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, shaddr,
1001 (u8 *)&addr_le, 4);
Daniel Kim0801e6c2014-01-29 15:32:13 +01001002 if (rv < 0)
Arend van Spriel9819a902015-03-11 16:11:32 +01001003 goto fail;
Daniel Kim0801e6c2014-01-29 15:32:13 +01001004
1005 /*
1006 * Check if addr is valid.
1007 * NVRAM length at the end of memory should have been overwritten.
1008 */
Arend van Spriel9819a902015-03-11 16:11:32 +01001009 addr = le32_to_cpu(addr_le);
Daniel Kim0801e6c2014-01-29 15:32:13 +01001010 if (!brcmf_sdio_valid_shared_address(addr)) {
Arend van Spriel9819a902015-03-11 16:11:32 +01001011 brcmf_err("invalid sdpcm_shared address 0x%08X\n", addr);
1012 rv = -EINVAL;
1013 goto fail;
Daniel Kim0801e6c2014-01-29 15:32:13 +01001014 }
1015
Arend van Spriel9819a902015-03-11 16:11:32 +01001016 brcmf_dbg(INFO, "sdpcm_shared address 0x%08X\n", addr);
1017
Daniel Kim0801e6c2014-01-29 15:32:13 +01001018 /* Read hndrte_shared structure */
1019 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, (u8 *)&sh_le,
1020 sizeof(struct sdpcm_shared_le));
1021 if (rv < 0)
Arend van Spriel9819a902015-03-11 16:11:32 +01001022 goto fail;
1023
1024 sdio_release_host(bus->sdiodev->func[1]);
Daniel Kim0801e6c2014-01-29 15:32:13 +01001025
1026 /* Endianness */
1027 sh->flags = le32_to_cpu(sh_le.flags);
1028 sh->trap_addr = le32_to_cpu(sh_le.trap_addr);
1029 sh->assert_exp_addr = le32_to_cpu(sh_le.assert_exp_addr);
1030 sh->assert_file_addr = le32_to_cpu(sh_le.assert_file_addr);
1031 sh->assert_line = le32_to_cpu(sh_le.assert_line);
1032 sh->console_addr = le32_to_cpu(sh_le.console_addr);
1033 sh->msgtrace_addr = le32_to_cpu(sh_le.msgtrace_addr);
1034
1035 if ((sh->flags & SDPCM_SHARED_VERSION_MASK) > SDPCM_SHARED_VERSION) {
1036 brcmf_err("sdpcm shared version unsupported: dhd %d dongle %d\n",
1037 SDPCM_SHARED_VERSION,
1038 sh->flags & SDPCM_SHARED_VERSION_MASK);
1039 return -EPROTO;
1040 }
Daniel Kim0801e6c2014-01-29 15:32:13 +01001041 return 0;
Arend van Spriel9819a902015-03-11 16:11:32 +01001042
1043fail:
1044 brcmf_err("unable to obtain sdpcm_shared info: rv=%d (addr=0x%x)\n",
1045 rv, addr);
1046 sdio_release_host(bus->sdiodev->func[1]);
1047 return rv;
Daniel Kim0801e6c2014-01-29 15:32:13 +01001048}
1049
1050static void brcmf_sdio_get_console_addr(struct brcmf_sdio *bus)
1051{
1052 struct sdpcm_shared sh;
1053
1054 if (brcmf_sdio_readshared(bus, &sh) == 0)
1055 bus->console_addr = sh.console_addr;
1056}
1057#else
1058static void brcmf_sdio_get_console_addr(struct brcmf_sdio *bus)
1059{
1060}
1061#endif /* DEBUG */
1062
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001063static u32 brcmf_sdio_hostmail(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001064{
1065 u32 intstatus = 0;
1066 u32 hmb_data;
1067 u8 fcbits;
Franky Lin58692752012-05-04 18:27:36 -07001068 int ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001069
Arend van Sprielc3203372013-04-03 12:40:44 +02001070 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001071
1072 /* Read mailbox data and ack that we did so */
Franky Lin58692752012-05-04 18:27:36 -07001073 ret = r_sdreg32(bus, &hmb_data,
1074 offsetof(struct sdpcmd_regs, tohostmailboxdata));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001075
Franky Lin58692752012-05-04 18:27:36 -07001076 if (ret == 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001077 w_sdreg32(bus, SMB_INT_ACK,
Franky Lin58692752012-05-04 18:27:36 -07001078 offsetof(struct sdpcmd_regs, tosbmailbox));
Arend van Spriel80969832012-06-09 22:51:44 +02001079 bus->sdcnt.f1regdata += 2;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001080
1081 /* Dongle recomposed rx frames, accept them again */
1082 if (hmb_data & HMB_DATA_NAKHANDLED) {
Arend van Sprielc3203372013-04-03 12:40:44 +02001083 brcmf_dbg(SDIO, "Dongle reports NAK handled, expect rtx of %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001084 bus->rx_seq);
1085 if (!bus->rxskip)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001086 brcmf_err("unexpected NAKHANDLED!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001087
1088 bus->rxskip = false;
1089 intstatus |= I_HMB_FRAME_IND;
1090 }
1091
1092 /*
1093 * DEVREADY does not occur with gSPI.
1094 */
1095 if (hmb_data & (HMB_DATA_DEVREADY | HMB_DATA_FWREADY)) {
1096 bus->sdpcm_ver =
1097 (hmb_data & HMB_DATA_VERSION_MASK) >>
1098 HMB_DATA_VERSION_SHIFT;
1099 if (bus->sdpcm_ver != SDPCM_PROT_VERSION)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001100 brcmf_err("Version mismatch, dongle reports %d, "
Arend van Spriel5b435de2011-10-05 13:19:03 +02001101 "expecting %d\n",
1102 bus->sdpcm_ver, SDPCM_PROT_VERSION);
1103 else
Arend van Sprielc3203372013-04-03 12:40:44 +02001104 brcmf_dbg(SDIO, "Dongle ready, protocol version %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001105 bus->sdpcm_ver);
Daniel Kim0801e6c2014-01-29 15:32:13 +01001106
1107 /*
1108 * Retrieve console state address now that firmware should have
1109 * updated it.
1110 */
1111 brcmf_sdio_get_console_addr(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001112 }
1113
1114 /*
1115 * Flow Control has been moved into the RX headers and this out of band
1116 * method isn't used any more.
1117 * remaining backward compatible with older dongles.
1118 */
1119 if (hmb_data & HMB_DATA_FC) {
1120 fcbits = (hmb_data & HMB_DATA_FCDATA_MASK) >>
1121 HMB_DATA_FCDATA_SHIFT;
1122
1123 if (fcbits & ~bus->flowcontrol)
Arend van Spriel80969832012-06-09 22:51:44 +02001124 bus->sdcnt.fc_xoff++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001125
1126 if (bus->flowcontrol & ~fcbits)
Arend van Spriel80969832012-06-09 22:51:44 +02001127 bus->sdcnt.fc_xon++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001128
Arend van Spriel80969832012-06-09 22:51:44 +02001129 bus->sdcnt.fc_rcvd++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001130 bus->flowcontrol = fcbits;
1131 }
1132
1133 /* Shouldn't be any others */
1134 if (hmb_data & ~(HMB_DATA_DEVREADY |
1135 HMB_DATA_NAKHANDLED |
1136 HMB_DATA_FC |
1137 HMB_DATA_FWREADY |
1138 HMB_DATA_FCDATA_MASK | HMB_DATA_VERSION_MASK))
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001139 brcmf_err("Unknown mailbox data content: 0x%02x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001140 hmb_data);
1141
1142 return intstatus;
1143}
1144
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001145static void brcmf_sdio_rxfail(struct brcmf_sdio *bus, bool abort, bool rtx)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001146{
1147 uint retries = 0;
1148 u16 lastrbc;
1149 u8 hi, lo;
1150 int err;
1151
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001152 brcmf_err("%sterminate frame%s\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001153 abort ? "abort command, " : "",
1154 rtx ? ", send NAK" : "");
1155
1156 if (abort)
Arend van Spriela39be272013-12-12 11:58:58 +01001157 brcmf_sdiod_abort(bus->sdiodev, SDIO_FUNC_2);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001158
Arend van Spriela39be272013-12-12 11:58:58 +01001159 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL,
1160 SFC_RF_TERM, &err);
Arend van Spriel80969832012-06-09 22:51:44 +02001161 bus->sdcnt.f1regdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001162
1163 /* Wait until the packet has been flushed (device/FIFO stable) */
1164 for (lastrbc = retries = 0xffff; retries > 0; retries--) {
Arend van Spriela39be272013-12-12 11:58:58 +01001165 hi = brcmf_sdiod_regrb(bus->sdiodev,
1166 SBSDIO_FUNC1_RFRAMEBCHI, &err);
1167 lo = brcmf_sdiod_regrb(bus->sdiodev,
1168 SBSDIO_FUNC1_RFRAMEBCLO, &err);
Arend van Spriel80969832012-06-09 22:51:44 +02001169 bus->sdcnt.f1regdata += 2;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001170
1171 if ((hi == 0) && (lo == 0))
1172 break;
1173
1174 if ((hi > (lastrbc >> 8)) && (lo > (lastrbc & 0x00ff))) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001175 brcmf_err("count growing: last 0x%04x now 0x%04x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001176 lastrbc, (hi << 8) + lo);
1177 }
1178 lastrbc = (hi << 8) + lo;
1179 }
1180
1181 if (!retries)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001182 brcmf_err("count never zeroed: last 0x%04x\n", lastrbc);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001183 else
Arend van Sprielc3203372013-04-03 12:40:44 +02001184 brcmf_dbg(SDIO, "flush took %d iterations\n", 0xffff - retries);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001185
1186 if (rtx) {
Arend van Spriel80969832012-06-09 22:51:44 +02001187 bus->sdcnt.rxrtx++;
Franky Lin58692752012-05-04 18:27:36 -07001188 err = w_sdreg32(bus, SMB_NAK,
1189 offsetof(struct sdpcmd_regs, tosbmailbox));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001190
Arend van Spriel80969832012-06-09 22:51:44 +02001191 bus->sdcnt.f1regdata++;
Franky Lin58692752012-05-04 18:27:36 -07001192 if (err == 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001193 bus->rxskip = true;
1194 }
1195
1196 /* Clear partial in any case */
Franky Lin4754fce2012-09-19 22:21:11 +02001197 bus->cur_read.len = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001198}
1199
Hante Meuleman81c78832014-02-25 20:30:35 +01001200static void brcmf_sdio_txfail(struct brcmf_sdio *bus)
1201{
1202 struct brcmf_sdio_dev *sdiodev = bus->sdiodev;
1203 u8 i, hi, lo;
1204
1205 /* On failure, abort the command and terminate the frame */
1206 brcmf_err("sdio error, abort command and terminate frame\n");
1207 bus->sdcnt.tx_sderrs++;
1208
1209 brcmf_sdiod_abort(sdiodev, SDIO_FUNC_2);
1210 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_FRAMECTRL, SFC_WF_TERM, NULL);
1211 bus->sdcnt.f1regdata++;
1212
1213 for (i = 0; i < 3; i++) {
1214 hi = brcmf_sdiod_regrb(sdiodev, SBSDIO_FUNC1_WFRAMEBCHI, NULL);
1215 lo = brcmf_sdiod_regrb(sdiodev, SBSDIO_FUNC1_WFRAMEBCLO, NULL);
1216 bus->sdcnt.f1regdata += 2;
1217 if ((hi == 0) && (lo == 0))
1218 break;
1219 }
1220}
1221
Arend van Spriel9a95e602011-11-10 20:30:29 +01001222/* return total length of buffer chain */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001223static uint brcmf_sdio_glom_len(struct brcmf_sdio *bus)
Arend van Spriel9a95e602011-11-10 20:30:29 +01001224{
1225 struct sk_buff *p;
1226 uint total;
1227
1228 total = 0;
1229 skb_queue_walk(&bus->glom, p)
1230 total += p->len;
1231 return total;
1232}
1233
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001234static void brcmf_sdio_free_glom(struct brcmf_sdio *bus)
Arend van Spriel046808d2011-11-10 20:30:31 +01001235{
1236 struct sk_buff *cur, *next;
1237
1238 skb_queue_walk_safe(&bus->glom, cur, next) {
1239 skb_unlink(cur, &bus->glom);
1240 brcmu_pkt_buf_free_skb(cur);
1241 }
1242}
1243
Franky Lin6bc52312013-08-10 12:27:28 +02001244/**
1245 * brcmfmac sdio bus specific header
1246 * This is the lowest layer header wrapped on the packets transmitted between
1247 * host and WiFi dongle which contains information needed for SDIO core and
1248 * firmware
1249 *
Franky Lin8da9d2c2013-11-29 12:25:10 +01001250 * It consists of 3 parts: hardware header, hardware extension header and
1251 * software header
Franky Lin6bc52312013-08-10 12:27:28 +02001252 * hardware header (frame tag) - 4 bytes
1253 * Byte 0~1: Frame length
1254 * Byte 2~3: Checksum, bit-wise inverse of frame length
Franky Lin8da9d2c2013-11-29 12:25:10 +01001255 * hardware extension header - 8 bytes
1256 * Tx glom mode only, N/A for Rx or normal Tx
1257 * Byte 0~1: Packet length excluding hw frame tag
1258 * Byte 2: Reserved
1259 * Byte 3: Frame flags, bit 0: last frame indication
1260 * Byte 4~5: Reserved
1261 * Byte 6~7: Tail padding length
Franky Lin6bc52312013-08-10 12:27:28 +02001262 * software header - 8 bytes
1263 * Byte 0: Rx/Tx sequence number
1264 * Byte 1: 4 MSB Channel number, 4 LSB arbitrary flag
1265 * Byte 2: Length of next data frame, reserved for Tx
1266 * Byte 3: Data offset
1267 * Byte 4: Flow control bits, reserved for Tx
1268 * Byte 5: Maximum Sequence number allowed by firmware for Tx, N/A for Tx packet
1269 * Byte 6~7: Reserved
1270 */
1271#define SDPCM_HWHDR_LEN 4
Franky Lin8da9d2c2013-11-29 12:25:10 +01001272#define SDPCM_HWEXT_LEN 8
Franky Lin6bc52312013-08-10 12:27:28 +02001273#define SDPCM_SWHDR_LEN 8
1274#define SDPCM_HDRLEN (SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN)
Franky Lin6bc52312013-08-10 12:27:28 +02001275/* software header */
1276#define SDPCM_SEQ_MASK 0x000000ff
1277#define SDPCM_SEQ_WRAP 256
1278#define SDPCM_CHANNEL_MASK 0x00000f00
1279#define SDPCM_CHANNEL_SHIFT 8
1280#define SDPCM_CONTROL_CHANNEL 0 /* Control */
1281#define SDPCM_EVENT_CHANNEL 1 /* Asyc Event Indication */
1282#define SDPCM_DATA_CHANNEL 2 /* Data Xmit/Recv */
1283#define SDPCM_GLOM_CHANNEL 3 /* Coalesced packets */
1284#define SDPCM_TEST_CHANNEL 15 /* Test/debug packets */
1285#define SDPCM_GLOMDESC(p) (((u8 *)p)[1] & 0x80)
1286#define SDPCM_NEXTLEN_MASK 0x00ff0000
1287#define SDPCM_NEXTLEN_SHIFT 16
1288#define SDPCM_DOFFSET_MASK 0xff000000
1289#define SDPCM_DOFFSET_SHIFT 24
1290#define SDPCM_FCMASK_MASK 0x000000ff
1291#define SDPCM_WINDOW_MASK 0x0000ff00
1292#define SDPCM_WINDOW_SHIFT 8
1293
1294static inline u8 brcmf_sdio_getdatoffset(u8 *swheader)
1295{
1296 u32 hdrvalue;
1297 hdrvalue = *(u32 *)swheader;
1298 return (u8)((hdrvalue & SDPCM_DOFFSET_MASK) >> SDPCM_DOFFSET_SHIFT);
1299}
1300
1301static int brcmf_sdio_hdparse(struct brcmf_sdio *bus, u8 *header,
1302 struct brcmf_sdio_hdrinfo *rd,
1303 enum brcmf_sdio_frmtype type)
Franky Lin4754fce2012-09-19 22:21:11 +02001304{
1305 u16 len, checksum;
1306 u8 rx_seq, fc, tx_seq_max;
Franky Lin6bc52312013-08-10 12:27:28 +02001307 u32 swheader;
Franky Lin4754fce2012-09-19 22:21:11 +02001308
Arend van Spriel4b776962013-11-29 12:25:06 +01001309 trace_brcmf_sdpcm_hdr(SDPCM_RX, header);
Arend van Spriel76584ec2013-10-15 15:44:47 +02001310
Franky Lin6bc52312013-08-10 12:27:28 +02001311 /* hw header */
Franky Lin4754fce2012-09-19 22:21:11 +02001312 len = get_unaligned_le16(header);
1313 checksum = get_unaligned_le16(header + sizeof(u16));
1314 /* All zero means no more to read */
1315 if (!(len | checksum)) {
1316 bus->rxpending = false;
Franky Lin10510582012-11-05 16:22:26 -08001317 return -ENODATA;
Franky Lin4754fce2012-09-19 22:21:11 +02001318 }
1319 if ((u16)(~(len ^ checksum))) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001320 brcmf_err("HW header checksum error\n");
Franky Lin4754fce2012-09-19 22:21:11 +02001321 bus->sdcnt.rx_badhdr++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001322 brcmf_sdio_rxfail(bus, false, false);
Franky Lin10510582012-11-05 16:22:26 -08001323 return -EIO;
Franky Lin4754fce2012-09-19 22:21:11 +02001324 }
1325 if (len < SDPCM_HDRLEN) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001326 brcmf_err("HW header length error\n");
Franky Lin10510582012-11-05 16:22:26 -08001327 return -EPROTO;
Franky Lin4754fce2012-09-19 22:21:11 +02001328 }
Franky Lin9d7d6f92012-10-22 10:36:24 -07001329 if (type == BRCMF_SDIO_FT_SUPER &&
1330 (roundup(len, bus->blocksize) != rd->len)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001331 brcmf_err("HW superframe header length error\n");
Franky Lin10510582012-11-05 16:22:26 -08001332 return -EPROTO;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001333 }
1334 if (type == BRCMF_SDIO_FT_SUB && len > rd->len) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001335 brcmf_err("HW subframe header length error\n");
Franky Lin10510582012-11-05 16:22:26 -08001336 return -EPROTO;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001337 }
Franky Lin4754fce2012-09-19 22:21:11 +02001338 rd->len = len;
1339
Franky Lin6bc52312013-08-10 12:27:28 +02001340 /* software header */
1341 header += SDPCM_HWHDR_LEN;
1342 swheader = le32_to_cpu(*(__le32 *)header);
1343 if (type == BRCMF_SDIO_FT_SUPER && SDPCM_GLOMDESC(header)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001344 brcmf_err("Glom descriptor found in superframe head\n");
Franky Lin9d7d6f92012-10-22 10:36:24 -07001345 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001346 return -EINVAL;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001347 }
Franky Lin6bc52312013-08-10 12:27:28 +02001348 rx_seq = (u8)(swheader & SDPCM_SEQ_MASK);
1349 rd->channel = (swheader & SDPCM_CHANNEL_MASK) >> SDPCM_CHANNEL_SHIFT;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001350 if (len > MAX_RX_DATASZ && rd->channel != SDPCM_CONTROL_CHANNEL &&
1351 type != BRCMF_SDIO_FT_SUPER) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001352 brcmf_err("HW header length too long\n");
Franky Lin4754fce2012-09-19 22:21:11 +02001353 bus->sdcnt.rx_toolong++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001354 brcmf_sdio_rxfail(bus, false, false);
Franky Lin4754fce2012-09-19 22:21:11 +02001355 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001356 return -EPROTO;
Franky Lin4754fce2012-09-19 22:21:11 +02001357 }
Franky Lin9d7d6f92012-10-22 10:36:24 -07001358 if (type == BRCMF_SDIO_FT_SUPER && rd->channel != SDPCM_GLOM_CHANNEL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001359 brcmf_err("Wrong channel for superframe\n");
Franky Lin9d7d6f92012-10-22 10:36:24 -07001360 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001361 return -EINVAL;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001362 }
1363 if (type == BRCMF_SDIO_FT_SUB && rd->channel != SDPCM_DATA_CHANNEL &&
1364 rd->channel != SDPCM_EVENT_CHANNEL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001365 brcmf_err("Wrong channel for subframe\n");
Franky Lin9d7d6f92012-10-22 10:36:24 -07001366 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001367 return -EINVAL;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001368 }
Franky Lin6bc52312013-08-10 12:27:28 +02001369 rd->dat_offset = brcmf_sdio_getdatoffset(header);
Franky Lin4754fce2012-09-19 22:21:11 +02001370 if (rd->dat_offset < SDPCM_HDRLEN || rd->dat_offset > rd->len) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001371 brcmf_err("seq %d: bad data offset\n", rx_seq);
Franky Lin4754fce2012-09-19 22:21:11 +02001372 bus->sdcnt.rx_badhdr++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001373 brcmf_sdio_rxfail(bus, false, false);
Franky Lin4754fce2012-09-19 22:21:11 +02001374 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001375 return -ENXIO;
Franky Lin4754fce2012-09-19 22:21:11 +02001376 }
1377 if (rd->seq_num != rx_seq) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001378 brcmf_err("seq %d: sequence number error, expect %d\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001379 rx_seq, rd->seq_num);
1380 bus->sdcnt.rx_badseq++;
1381 rd->seq_num = rx_seq;
1382 }
Franky Lin9d7d6f92012-10-22 10:36:24 -07001383 /* no need to check the reset for subframe */
1384 if (type == BRCMF_SDIO_FT_SUB)
Franky Lin10510582012-11-05 16:22:26 -08001385 return 0;
Franky Lin6bc52312013-08-10 12:27:28 +02001386 rd->len_nxtfrm = (swheader & SDPCM_NEXTLEN_MASK) >> SDPCM_NEXTLEN_SHIFT;
Franky Lin4754fce2012-09-19 22:21:11 +02001387 if (rd->len_nxtfrm << 4 > MAX_RX_DATASZ) {
1388 /* only warm for NON glom packet */
1389 if (rd->channel != SDPCM_GLOM_CHANNEL)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001390 brcmf_err("seq %d: next length error\n", rx_seq);
Franky Lin4754fce2012-09-19 22:21:11 +02001391 rd->len_nxtfrm = 0;
1392 }
Franky Lin6bc52312013-08-10 12:27:28 +02001393 swheader = le32_to_cpu(*(__le32 *)(header + 4));
1394 fc = swheader & SDPCM_FCMASK_MASK;
Franky Lin4754fce2012-09-19 22:21:11 +02001395 if (bus->flowcontrol != fc) {
1396 if (~bus->flowcontrol & fc)
1397 bus->sdcnt.fc_xoff++;
1398 if (bus->flowcontrol & ~fc)
1399 bus->sdcnt.fc_xon++;
1400 bus->sdcnt.fc_rcvd++;
1401 bus->flowcontrol = fc;
1402 }
Franky Lin6bc52312013-08-10 12:27:28 +02001403 tx_seq_max = (swheader & SDPCM_WINDOW_MASK) >> SDPCM_WINDOW_SHIFT;
Franky Lin4754fce2012-09-19 22:21:11 +02001404 if ((u8)(tx_seq_max - bus->tx_seq) > 0x40) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001405 brcmf_err("seq %d: max tx seq number error\n", rx_seq);
Franky Lin4754fce2012-09-19 22:21:11 +02001406 tx_seq_max = bus->tx_seq + 2;
1407 }
1408 bus->tx_max = tx_seq_max;
1409
Franky Lin10510582012-11-05 16:22:26 -08001410 return 0;
Franky Lin4754fce2012-09-19 22:21:11 +02001411}
1412
Franky Lin6bc52312013-08-10 12:27:28 +02001413static inline void brcmf_sdio_update_hwhdr(u8 *header, u16 frm_length)
1414{
1415 *(__le16 *)header = cpu_to_le16(frm_length);
1416 *(((__le16 *)header) + 1) = cpu_to_le16(~frm_length);
1417}
1418
1419static void brcmf_sdio_hdpack(struct brcmf_sdio *bus, u8 *header,
1420 struct brcmf_sdio_hdrinfo *hd_info)
1421{
Franky Lin8da9d2c2013-11-29 12:25:10 +01001422 u32 hdrval;
1423 u8 hdr_offset;
Franky Lin6bc52312013-08-10 12:27:28 +02001424
1425 brcmf_sdio_update_hwhdr(header, hd_info->len);
Franky Lin8da9d2c2013-11-29 12:25:10 +01001426 hdr_offset = SDPCM_HWHDR_LEN;
Franky Lin6bc52312013-08-10 12:27:28 +02001427
Franky Lin8da9d2c2013-11-29 12:25:10 +01001428 if (bus->txglom) {
1429 hdrval = (hd_info->len - hdr_offset) | (hd_info->lastfrm << 24);
1430 *((__le32 *)(header + hdr_offset)) = cpu_to_le32(hdrval);
1431 hdrval = (u16)hd_info->tail_pad << 16;
1432 *(((__le32 *)(header + hdr_offset)) + 1) = cpu_to_le32(hdrval);
1433 hdr_offset += SDPCM_HWEXT_LEN;
1434 }
1435
1436 hdrval = hd_info->seq_num;
1437 hdrval |= (hd_info->channel << SDPCM_CHANNEL_SHIFT) &
1438 SDPCM_CHANNEL_MASK;
1439 hdrval |= (hd_info->dat_offset << SDPCM_DOFFSET_SHIFT) &
1440 SDPCM_DOFFSET_MASK;
1441 *((__le32 *)(header + hdr_offset)) = cpu_to_le32(hdrval);
1442 *(((__le32 *)(header + hdr_offset)) + 1) = 0;
1443 trace_brcmf_sdpcm_hdr(SDPCM_TX + !!(bus->txglom), header);
Franky Lin6bc52312013-08-10 12:27:28 +02001444}
1445
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001446static u8 brcmf_sdio_rxglom(struct brcmf_sdio *bus, u8 rxseq)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001447{
1448 u16 dlen, totlen;
1449 u8 *dptr, num = 0;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001450 u16 sublen;
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001451 struct sk_buff *pfirst, *pnext;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001452
1453 int errcode;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001454 u8 doff, sfdoff;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001455
Franky Lin6bc52312013-08-10 12:27:28 +02001456 struct brcmf_sdio_hdrinfo rd_new;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001457
1458 /* If packets, issue read(s) and send up packet chain */
1459 /* Return sequence numbers consumed? */
1460
Arend van Sprielc3203372013-04-03 12:40:44 +02001461 brcmf_dbg(SDIO, "start: glomd %p glom %p\n",
Arend van Sprielb83db862011-10-19 12:51:09 +02001462 bus->glomd, skb_peek(&bus->glom));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001463
1464 /* If there's a descriptor, generate the packet chain */
1465 if (bus->glomd) {
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001466 pfirst = pnext = NULL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001467 dlen = (u16) (bus->glomd->len);
1468 dptr = bus->glomd->data;
1469 if (!dlen || (dlen & 1)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001470 brcmf_err("bad glomd len(%d), ignore descriptor\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001471 dlen);
1472 dlen = 0;
1473 }
1474
1475 for (totlen = num = 0; dlen; num++) {
1476 /* Get (and move past) next length */
1477 sublen = get_unaligned_le16(dptr);
1478 dlen -= sizeof(u16);
1479 dptr += sizeof(u16);
1480 if ((sublen < SDPCM_HDRLEN) ||
1481 ((num == 0) && (sublen < (2 * SDPCM_HDRLEN)))) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001482 brcmf_err("descriptor len %d bad: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001483 num, sublen);
1484 pnext = NULL;
1485 break;
1486 }
Arend van Spriele217d1c2013-11-29 12:25:13 +01001487 if (sublen % bus->sgentry_align) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001488 brcmf_err("sublen %d not multiple of %d\n",
Arend van Spriele217d1c2013-11-29 12:25:13 +01001489 sublen, bus->sgentry_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001490 }
1491 totlen += sublen;
1492
1493 /* For last frame, adjust read len so total
1494 is a block multiple */
1495 if (!dlen) {
1496 sublen +=
1497 (roundup(totlen, bus->blocksize) - totlen);
1498 totlen = roundup(totlen, bus->blocksize);
1499 }
1500
1501 /* Allocate/chain packet for next subframe */
Arend van Spriele217d1c2013-11-29 12:25:13 +01001502 pnext = brcmu_pkt_buf_get_skb(sublen + bus->sgentry_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001503 if (pnext == NULL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001504 brcmf_err("bcm_pkt_buf_get_skb failed, num %d len %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001505 num, sublen);
1506 break;
1507 }
Arend van Sprielb83db862011-10-19 12:51:09 +02001508 skb_queue_tail(&bus->glom, pnext);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001509
1510 /* Adhere to start alignment requirements */
Arend van Spriele217d1c2013-11-29 12:25:13 +01001511 pkt_align(pnext, sublen, bus->sgentry_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001512 }
1513
1514 /* If all allocations succeeded, save packet chain
1515 in bus structure */
1516 if (pnext) {
1517 brcmf_dbg(GLOM, "allocated %d-byte packet chain for %d subframes\n",
1518 totlen, num);
Franky Lin4754fce2012-09-19 22:21:11 +02001519 if (BRCMF_GLOM_ON() && bus->cur_read.len &&
1520 totlen != bus->cur_read.len) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001521 brcmf_dbg(GLOM, "glomdesc mismatch: nextlen %d glomdesc %d rxseq %d\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001522 bus->cur_read.len, totlen, rxseq);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001523 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02001524 pfirst = pnext = NULL;
1525 } else {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001526 brcmf_sdio_free_glom(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001527 num = 0;
1528 }
1529
1530 /* Done with descriptor packet */
1531 brcmu_pkt_buf_free_skb(bus->glomd);
1532 bus->glomd = NULL;
Franky Lin4754fce2012-09-19 22:21:11 +02001533 bus->cur_read.len = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001534 }
1535
1536 /* Ok -- either we just generated a packet chain,
1537 or had one from before */
Arend van Sprielb83db862011-10-19 12:51:09 +02001538 if (!skb_queue_empty(&bus->glom)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001539 if (BRCMF_GLOM_ON()) {
1540 brcmf_dbg(GLOM, "try superframe read, packet chain:\n");
Arend van Sprielb83db862011-10-19 12:51:09 +02001541 skb_queue_walk(&bus->glom, pnext) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001542 brcmf_dbg(GLOM, " %p: %p len 0x%04x (%d)\n",
1543 pnext, (u8 *) (pnext->data),
1544 pnext->len, pnext->len);
1545 }
1546 }
1547
Arend van Sprielb83db862011-10-19 12:51:09 +02001548 pfirst = skb_peek(&bus->glom);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001549 dlen = (u16) brcmf_sdio_glom_len(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001550
1551 /* Do an SDIO read for the superframe. Configurable iovar to
1552 * read directly into the chained packet, or allocate a large
1553 * packet and and copy into the chain.
1554 */
Franky Lin38b0b0d2012-11-05 16:22:24 -08001555 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriela39be272013-12-12 11:58:58 +01001556 errcode = brcmf_sdiod_recv_chain(bus->sdiodev,
Arend van Spriela39be272013-12-12 11:58:58 +01001557 &bus->glom, dlen);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001558 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel80969832012-06-09 22:51:44 +02001559 bus->sdcnt.f2rxdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001560
Franky Lin64d66c32015-11-25 11:32:40 +01001561 /* On failure, kill the superframe */
Arend van Spriel5b435de2011-10-05 13:19:03 +02001562 if (errcode < 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001563 brcmf_err("glom read of %d bytes failed: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001564 dlen, errcode);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001565
Franky Lin38b0b0d2012-11-05 16:22:24 -08001566 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin64d66c32015-11-25 11:32:40 +01001567 brcmf_sdio_rxfail(bus, true, false);
1568 bus->sdcnt.rxglomfail++;
1569 brcmf_sdio_free_glom(bus);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001570 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001571 return 0;
1572 }
Joe Perches1e023822012-01-15 00:38:40 -08001573
1574 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
1575 pfirst->data, min_t(int, pfirst->len, 48),
1576 "SUPERFRAME:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001577
Franky Lin9d7d6f92012-10-22 10:36:24 -07001578 rd_new.seq_num = rxseq;
1579 rd_new.len = dlen;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001580 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin6bc52312013-08-10 12:27:28 +02001581 errcode = brcmf_sdio_hdparse(bus, pfirst->data, &rd_new,
1582 BRCMF_SDIO_FT_SUPER);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001583 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin9d7d6f92012-10-22 10:36:24 -07001584 bus->cur_read.len = rd_new.len_nxtfrm << 4;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001585
1586 /* Remove superframe header, remember offset */
Franky Lin9d7d6f92012-10-22 10:36:24 -07001587 skb_pull(pfirst, rd_new.dat_offset);
1588 sfdoff = rd_new.dat_offset;
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001589 num = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001590
1591 /* Validate all the subframe headers */
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001592 skb_queue_walk(&bus->glom, pnext) {
1593 /* leave when invalid subframe is found */
1594 if (errcode)
1595 break;
1596
Franky Lin9d7d6f92012-10-22 10:36:24 -07001597 rd_new.len = pnext->len;
1598 rd_new.seq_num = rxseq++;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001599 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin6bc52312013-08-10 12:27:28 +02001600 errcode = brcmf_sdio_hdparse(bus, pnext->data, &rd_new,
1601 BRCMF_SDIO_FT_SUB);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001602 sdio_release_host(bus->sdiodev->func[1]);
Joe Perches1e023822012-01-15 00:38:40 -08001603 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
Franky Lin9d7d6f92012-10-22 10:36:24 -07001604 pnext->data, 32, "subframe:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001605
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001606 num++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001607 }
1608
1609 if (errcode) {
Franky Lin64d66c32015-11-25 11:32:40 +01001610 /* Terminate frame on error */
Franky Lin38b0b0d2012-11-05 16:22:24 -08001611 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin64d66c32015-11-25 11:32:40 +01001612 brcmf_sdio_rxfail(bus, true, false);
1613 bus->sdcnt.rxglomfail++;
1614 brcmf_sdio_free_glom(bus);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001615 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001616 bus->cur_read.len = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001617 return 0;
1618 }
1619
1620 /* Basic SD framing looks ok - process each packet (header) */
Arend van Spriel5b435de2011-10-05 13:19:03 +02001621
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001622 skb_queue_walk_safe(&bus->glom, pfirst, pnext) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001623 dptr = (u8 *) (pfirst->data);
1624 sublen = get_unaligned_le16(dptr);
Franky Lin6bc52312013-08-10 12:27:28 +02001625 doff = brcmf_sdio_getdatoffset(&dptr[SDPCM_HWHDR_LEN]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001626
Joe Perches1e023822012-01-15 00:38:40 -08001627 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_DATA_ON(),
Franky Lin9d7d6f92012-10-22 10:36:24 -07001628 dptr, pfirst->len,
1629 "Rx Subframe Data:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001630
1631 __skb_trim(pfirst, sublen);
1632 skb_pull(pfirst, doff);
1633
1634 if (pfirst->len == 0) {
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001635 skb_unlink(pfirst, &bus->glom);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001636 brcmu_pkt_buf_free_skb(pfirst);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001637 continue;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001638 }
1639
Joe Perches1e023822012-01-15 00:38:40 -08001640 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
1641 pfirst->data,
1642 min_t(int, pfirst->len, 32),
1643 "subframe %d to stack, %p (%p/%d) nxt/lnk %p/%p\n",
1644 bus->glom.qlen, pfirst, pfirst->data,
1645 pfirst->len, pfirst->next,
1646 pfirst->prev);
Arend van Spriel05f38202013-09-25 13:05:46 +02001647 skb_unlink(pfirst, &bus->glom);
1648 brcmf_rx_frame(bus->sdiodev->dev, pfirst);
1649 bus->sdcnt.rxglompkts++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001650 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02001651
Arend van Spriel80969832012-06-09 22:51:44 +02001652 bus->sdcnt.rxglomframes++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001653 }
1654 return num;
1655}
1656
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001657static int brcmf_sdio_dcmd_resp_wait(struct brcmf_sdio *bus, uint *condition,
1658 bool *pending)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001659{
1660 DECLARE_WAITQUEUE(wait, current);
1661 int timeout = msecs_to_jiffies(DCMD_RESP_TIMEOUT);
1662
1663 /* Wait until control frame is available */
1664 add_wait_queue(&bus->dcmd_resp_wait, &wait);
1665 set_current_state(TASK_INTERRUPTIBLE);
1666
1667 while (!(*condition) && (!signal_pending(current) && timeout))
1668 timeout = schedule_timeout(timeout);
1669
1670 if (signal_pending(current))
1671 *pending = true;
1672
1673 set_current_state(TASK_RUNNING);
1674 remove_wait_queue(&bus->dcmd_resp_wait, &wait);
1675
1676 return timeout;
1677}
1678
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001679static int brcmf_sdio_dcmd_resp_wake(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001680{
1681 if (waitqueue_active(&bus->dcmd_resp_wait))
1682 wake_up_interruptible(&bus->dcmd_resp_wait);
1683
1684 return 0;
1685}
1686static void
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001687brcmf_sdio_read_control(struct brcmf_sdio *bus, u8 *hdr, uint len, uint doff)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001688{
1689 uint rdlen, pad;
Franky Lindd43a012012-11-05 16:22:22 -08001690 u8 *buf = NULL, *rbuf;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001691 int sdret;
1692
1693 brcmf_dbg(TRACE, "Enter\n");
1694
Franky Lindd43a012012-11-05 16:22:22 -08001695 if (bus->rxblen)
1696 buf = vzalloc(bus->rxblen);
Joe Perches14f8dc42013-02-07 11:46:27 +00001697 if (!buf)
Franky Lindd43a012012-11-05 16:22:22 -08001698 goto done;
Joe Perches14f8dc42013-02-07 11:46:27 +00001699
Franky Lindd43a012012-11-05 16:22:22 -08001700 rbuf = bus->rxbuf;
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01001701 pad = ((unsigned long)rbuf % bus->head_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001702 if (pad)
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01001703 rbuf += (bus->head_align - pad);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001704
1705 /* Copy the already-read portion over */
Franky Lindd43a012012-11-05 16:22:22 -08001706 memcpy(buf, hdr, BRCMF_FIRSTREAD);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001707 if (len <= BRCMF_FIRSTREAD)
1708 goto gotpkt;
1709
1710 /* Raise rdlen to next SDIO block to avoid tail command */
1711 rdlen = len - BRCMF_FIRSTREAD;
1712 if (bus->roundup && bus->blocksize && (rdlen > bus->blocksize)) {
1713 pad = bus->blocksize - (rdlen % bus->blocksize);
1714 if ((pad <= bus->roundup) && (pad < bus->blocksize) &&
Franky Linb01a6b32011-12-16 18:37:03 -08001715 ((len + pad) < bus->sdiodev->bus_if->maxctl))
Arend van Spriel5b435de2011-10-05 13:19:03 +02001716 rdlen += pad;
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01001717 } else if (rdlen % bus->head_align) {
1718 rdlen += bus->head_align - (rdlen % bus->head_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001719 }
1720
Arend van Spriel5b435de2011-10-05 13:19:03 +02001721 /* Drop if the read is too big or it exceeds our maximum */
Franky Linb01a6b32011-12-16 18:37:03 -08001722 if ((rdlen + BRCMF_FIRSTREAD) > bus->sdiodev->bus_if->maxctl) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001723 brcmf_err("%d-byte control read exceeds %d-byte buffer\n",
Franky Linb01a6b32011-12-16 18:37:03 -08001724 rdlen, bus->sdiodev->bus_if->maxctl);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001725 brcmf_sdio_rxfail(bus, false, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001726 goto done;
1727 }
1728
Franky Linb01a6b32011-12-16 18:37:03 -08001729 if ((len - doff) > bus->sdiodev->bus_if->maxctl) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001730 brcmf_err("%d-byte ctl frame (%d-byte ctl data) exceeds %d-byte limit\n",
Franky Linb01a6b32011-12-16 18:37:03 -08001731 len, len - doff, bus->sdiodev->bus_if->maxctl);
Arend van Spriel80969832012-06-09 22:51:44 +02001732 bus->sdcnt.rx_toolong++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001733 brcmf_sdio_rxfail(bus, false, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001734 goto done;
1735 }
1736
Franky Lindd43a012012-11-05 16:22:22 -08001737 /* Read remain of frame body */
Arend van Spriela7cdd822013-12-12 11:58:59 +01001738 sdret = brcmf_sdiod_recv_buf(bus->sdiodev, rbuf, rdlen);
Arend van Spriel80969832012-06-09 22:51:44 +02001739 bus->sdcnt.f2rxdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001740
1741 /* Control frame failures need retransmission */
1742 if (sdret < 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001743 brcmf_err("read %d control bytes failed: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001744 rdlen, sdret);
Arend van Spriel80969832012-06-09 22:51:44 +02001745 bus->sdcnt.rxc_errors++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001746 brcmf_sdio_rxfail(bus, true, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001747 goto done;
Franky Lindd43a012012-11-05 16:22:22 -08001748 } else
1749 memcpy(buf + BRCMF_FIRSTREAD, rbuf, rdlen);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001750
1751gotpkt:
1752
Joe Perches1e023822012-01-15 00:38:40 -08001753 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_CTL_ON(),
Franky Lindd43a012012-11-05 16:22:22 -08001754 buf, len, "RxCtrl:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001755
1756 /* Point to valid data and indicate its length */
Franky Lindd43a012012-11-05 16:22:22 -08001757 spin_lock_bh(&bus->rxctl_lock);
1758 if (bus->rxctl) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001759 brcmf_err("last control frame is being processed.\n");
Franky Lindd43a012012-11-05 16:22:22 -08001760 spin_unlock_bh(&bus->rxctl_lock);
1761 vfree(buf);
1762 goto done;
1763 }
1764 bus->rxctl = buf + doff;
1765 bus->rxctl_orig = buf;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001766 bus->rxlen = len - doff;
Franky Lindd43a012012-11-05 16:22:22 -08001767 spin_unlock_bh(&bus->rxctl_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001768
1769done:
1770 /* Awake any waiters */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001771 brcmf_sdio_dcmd_resp_wake(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001772}
1773
1774/* Pad read to blocksize for efficiency */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001775static void brcmf_sdio_pad(struct brcmf_sdio *bus, u16 *pad, u16 *rdlen)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001776{
1777 if (bus->roundup && bus->blocksize && *rdlen > bus->blocksize) {
1778 *pad = bus->blocksize - (*rdlen % bus->blocksize);
1779 if (*pad <= bus->roundup && *pad < bus->blocksize &&
1780 *rdlen + *pad + BRCMF_FIRSTREAD < MAX_RX_DATASZ)
1781 *rdlen += *pad;
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01001782 } else if (*rdlen % bus->head_align) {
1783 *rdlen += bus->head_align - (*rdlen % bus->head_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001784 }
1785}
1786
Franky Lin4754fce2012-09-19 22:21:11 +02001787static uint brcmf_sdio_readframes(struct brcmf_sdio *bus, uint maxframes)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001788{
Arend van Spriel5b435de2011-10-05 13:19:03 +02001789 struct sk_buff *pkt; /* Packet for event or data frames */
1790 u16 pad; /* Number of pad bytes to read */
Arend van Spriel5b435de2011-10-05 13:19:03 +02001791 uint rxleft = 0; /* Remaining number of frames allowed */
Arend van Spriel349e7102013-03-03 12:45:28 +01001792 int ret; /* Return code from calls */
Arend van Spriel5b435de2011-10-05 13:19:03 +02001793 uint rxcount = 0; /* Total frames read */
Franky Lin6bc52312013-08-10 12:27:28 +02001794 struct brcmf_sdio_hdrinfo *rd = &bus->cur_read, rd_new;
Franky Lin4754fce2012-09-19 22:21:11 +02001795 u8 head_read = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001796
1797 brcmf_dbg(TRACE, "Enter\n");
1798
1799 /* Not finished unless we encounter no more frames indication */
Franky Lin4754fce2012-09-19 22:21:11 +02001800 bus->rxpending = true;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001801
Franky Lin4754fce2012-09-19 22:21:11 +02001802 for (rd->seq_num = bus->rx_seq, rxleft = maxframes;
Arend van Spriela1ce7a02015-02-06 18:36:42 +01001803 !bus->rxskip && rxleft && bus->sdiodev->state == BRCMF_SDIOD_DATA;
Franky Lin4754fce2012-09-19 22:21:11 +02001804 rd->seq_num++, rxleft--) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001805
1806 /* Handle glomming separately */
Arend van Sprielb83db862011-10-19 12:51:09 +02001807 if (bus->glomd || !skb_queue_empty(&bus->glom)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001808 u8 cnt;
1809 brcmf_dbg(GLOM, "calling rxglom: glomd %p, glom %p\n",
Arend van Sprielb83db862011-10-19 12:51:09 +02001810 bus->glomd, skb_peek(&bus->glom));
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001811 cnt = brcmf_sdio_rxglom(bus, rd->seq_num);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001812 brcmf_dbg(GLOM, "rxglom returned %d\n", cnt);
Franky Lin4754fce2012-09-19 22:21:11 +02001813 rd->seq_num += cnt - 1;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001814 rxleft = (rxleft > cnt) ? (rxleft - cnt) : 1;
1815 continue;
1816 }
1817
Franky Lin4754fce2012-09-19 22:21:11 +02001818 rd->len_left = rd->len;
1819 /* read header first for unknow frame length */
Franky Lin38b0b0d2012-11-05 16:22:24 -08001820 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001821 if (!rd->len) {
Arend van Spriela39be272013-12-12 11:58:58 +01001822 ret = brcmf_sdiod_recv_buf(bus->sdiodev,
Arend van Spriela39be272013-12-12 11:58:58 +01001823 bus->rxhdr, BRCMF_FIRSTREAD);
Franky Lin4754fce2012-09-19 22:21:11 +02001824 bus->sdcnt.f2rxhdrs++;
Arend van Spriel349e7102013-03-03 12:45:28 +01001825 if (ret < 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001826 brcmf_err("RXHEADER FAILED: %d\n",
Arend van Spriel349e7102013-03-03 12:45:28 +01001827 ret);
Franky Lin4754fce2012-09-19 22:21:11 +02001828 bus->sdcnt.rx_hdrfail++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001829 brcmf_sdio_rxfail(bus, true, true);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001830 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001831 continue;
1832 }
1833
Franky Lin4754fce2012-09-19 22:21:11 +02001834 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() || BRCMF_HDRS_ON(),
1835 bus->rxhdr, SDPCM_HDRLEN,
1836 "RxHdr:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001837
Franky Lin6bc52312013-08-10 12:27:28 +02001838 if (brcmf_sdio_hdparse(bus, bus->rxhdr, rd,
1839 BRCMF_SDIO_FT_NORMAL)) {
Franky Lin38b0b0d2012-11-05 16:22:24 -08001840 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001841 if (!bus->rxpending)
1842 break;
1843 else
1844 continue;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001845 }
1846
Franky Lin4754fce2012-09-19 22:21:11 +02001847 if (rd->channel == SDPCM_CONTROL_CHANNEL) {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001848 brcmf_sdio_read_control(bus, bus->rxhdr,
1849 rd->len,
1850 rd->dat_offset);
Franky Lin4754fce2012-09-19 22:21:11 +02001851 /* prepare the descriptor for the next read */
1852 rd->len = rd->len_nxtfrm << 4;
1853 rd->len_nxtfrm = 0;
1854 /* treat all packet as event if we don't know */
1855 rd->channel = SDPCM_EVENT_CHANNEL;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001856 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001857 continue;
1858 }
1859 rd->len_left = rd->len > BRCMF_FIRSTREAD ?
1860 rd->len - BRCMF_FIRSTREAD : 0;
1861 head_read = BRCMF_FIRSTREAD;
1862 }
1863
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001864 brcmf_sdio_pad(bus, &pad, &rd->len_left);
Franky Lin4754fce2012-09-19 22:21:11 +02001865
1866 pkt = brcmu_pkt_buf_get_skb(rd->len_left + head_read +
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01001867 bus->head_align);
Franky Lin4754fce2012-09-19 22:21:11 +02001868 if (!pkt) {
1869 /* Give up on data, request rtx of events */
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001870 brcmf_err("brcmu_pkt_buf_get_skb failed\n");
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001871 brcmf_sdio_rxfail(bus, false,
Franky Lin4754fce2012-09-19 22:21:11 +02001872 RETRYCHAN(rd->channel));
Franky Lin38b0b0d2012-11-05 16:22:24 -08001873 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001874 continue;
1875 }
1876 skb_pull(pkt, head_read);
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01001877 pkt_align(pkt, rd->len_left, bus->head_align);
Franky Lin4754fce2012-09-19 22:21:11 +02001878
Arend van Spriela7cdd822013-12-12 11:58:59 +01001879 ret = brcmf_sdiod_recv_pkt(bus->sdiodev, pkt);
Franky Lin4754fce2012-09-19 22:21:11 +02001880 bus->sdcnt.f2rxdata++;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001881 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001882
Arend van Spriel349e7102013-03-03 12:45:28 +01001883 if (ret < 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001884 brcmf_err("read %d bytes from channel %d failed: %d\n",
Arend van Spriel349e7102013-03-03 12:45:28 +01001885 rd->len, rd->channel, ret);
Franky Lin4754fce2012-09-19 22:21:11 +02001886 brcmu_pkt_buf_free_skb(pkt);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001887 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001888 brcmf_sdio_rxfail(bus, true,
Franky Lin4754fce2012-09-19 22:21:11 +02001889 RETRYCHAN(rd->channel));
Franky Lin38b0b0d2012-11-05 16:22:24 -08001890 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001891 continue;
1892 }
1893
1894 if (head_read) {
1895 skb_push(pkt, head_read);
1896 memcpy(pkt->data, bus->rxhdr, head_read);
1897 head_read = 0;
1898 } else {
1899 memcpy(bus->rxhdr, pkt->data, SDPCM_HDRLEN);
1900 rd_new.seq_num = rd->seq_num;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001901 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin6bc52312013-08-10 12:27:28 +02001902 if (brcmf_sdio_hdparse(bus, bus->rxhdr, &rd_new,
1903 BRCMF_SDIO_FT_NORMAL)) {
Franky Lin4754fce2012-09-19 22:21:11 +02001904 rd->len = 0;
1905 brcmu_pkt_buf_free_skb(pkt);
1906 }
Arend van Spriel80969832012-06-09 22:51:44 +02001907 bus->sdcnt.rx_readahead_cnt++;
Franky Lin4754fce2012-09-19 22:21:11 +02001908 if (rd->len != roundup(rd_new.len, 16)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001909 brcmf_err("frame length mismatch:read %d, should be %d\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001910 rd->len,
1911 roundup(rd_new.len, 16) >> 4);
1912 rd->len = 0;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001913 brcmf_sdio_rxfail(bus, true, true);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001914 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001915 brcmu_pkt_buf_free_skb(pkt);
1916 continue;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001917 }
Franky Lin38b0b0d2012-11-05 16:22:24 -08001918 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001919 rd->len_nxtfrm = rd_new.len_nxtfrm;
1920 rd->channel = rd_new.channel;
1921 rd->dat_offset = rd_new.dat_offset;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001922
Joe Perches1e023822012-01-15 00:38:40 -08001923 brcmf_dbg_hex_dump(!(BRCMF_BYTES_ON() &&
1924 BRCMF_DATA_ON()) &&
1925 BRCMF_HDRS_ON(),
1926 bus->rxhdr, SDPCM_HDRLEN,
1927 "RxHdr:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001928
Franky Lin4754fce2012-09-19 22:21:11 +02001929 if (rd_new.channel == SDPCM_CONTROL_CHANNEL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001930 brcmf_err("readahead on control packet %d?\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001931 rd_new.seq_num);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001932 /* Force retry w/normal header read */
Franky Lin4754fce2012-09-19 22:21:11 +02001933 rd->len = 0;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001934 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001935 brcmf_sdio_rxfail(bus, false, true);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001936 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001937 brcmu_pkt_buf_free_skb(pkt);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001938 continue;
1939 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02001940 }
1941
Joe Perches1e023822012-01-15 00:38:40 -08001942 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_DATA_ON(),
Franky Lin4754fce2012-09-19 22:21:11 +02001943 pkt->data, rd->len, "Rx Data:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001944
Arend van Spriel5b435de2011-10-05 13:19:03 +02001945 /* Save superframe descriptor and allocate packet frame */
Franky Lin4754fce2012-09-19 22:21:11 +02001946 if (rd->channel == SDPCM_GLOM_CHANNEL) {
Franky Lin6bc52312013-08-10 12:27:28 +02001947 if (SDPCM_GLOMDESC(&bus->rxhdr[SDPCM_HWHDR_LEN])) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001948 brcmf_dbg(GLOM, "glom descriptor, %d bytes:\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001949 rd->len);
Joe Perches1e023822012-01-15 00:38:40 -08001950 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
Franky Lin4754fce2012-09-19 22:21:11 +02001951 pkt->data, rd->len,
Joe Perches1e023822012-01-15 00:38:40 -08001952 "Glom Data:\n");
Franky Lin4754fce2012-09-19 22:21:11 +02001953 __skb_trim(pkt, rd->len);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001954 skb_pull(pkt, SDPCM_HDRLEN);
1955 bus->glomd = pkt;
1956 } else {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001957 brcmf_err("%s: glom superframe w/o "
Arend van Spriel5b435de2011-10-05 13:19:03 +02001958 "descriptor!\n", __func__);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001959 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001960 brcmf_sdio_rxfail(bus, false, false);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001961 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001962 }
Franky Lin4754fce2012-09-19 22:21:11 +02001963 /* prepare the descriptor for the next read */
1964 rd->len = rd->len_nxtfrm << 4;
1965 rd->len_nxtfrm = 0;
1966 /* treat all packet as event if we don't know */
1967 rd->channel = SDPCM_EVENT_CHANNEL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001968 continue;
1969 }
1970
1971 /* Fill in packet len and prio, deliver upward */
Franky Lin4754fce2012-09-19 22:21:11 +02001972 __skb_trim(pkt, rd->len);
1973 skb_pull(pkt, rd->dat_offset);
1974
1975 /* prepare the descriptor for the next read */
1976 rd->len = rd->len_nxtfrm << 4;
1977 rd->len_nxtfrm = 0;
1978 /* treat all packet as event if we don't know */
1979 rd->channel = SDPCM_EVENT_CHANNEL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001980
1981 if (pkt->len == 0) {
1982 brcmu_pkt_buf_free_skb(pkt);
1983 continue;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001984 }
1985
Arend van Spriel05f38202013-09-25 13:05:46 +02001986 brcmf_rx_frame(bus->sdiodev->dev, pkt);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001987 }
Franky Lin4754fce2012-09-19 22:21:11 +02001988
Arend van Spriel5b435de2011-10-05 13:19:03 +02001989 rxcount = maxframes - rxleft;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001990 /* Message if we hit the limit */
1991 if (!rxleft)
Franky Lin4754fce2012-09-19 22:21:11 +02001992 brcmf_dbg(DATA, "hit rx limit of %d frames\n", maxframes);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001993 else
Arend van Spriel5b435de2011-10-05 13:19:03 +02001994 brcmf_dbg(DATA, "processed %d frames\n", rxcount);
1995 /* Back off rxseq if awaiting rtx, update rx_seq */
1996 if (bus->rxskip)
Franky Lin4754fce2012-09-19 22:21:11 +02001997 rd->seq_num--;
1998 bus->rx_seq = rd->seq_num;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001999
2000 return rxcount;
2001}
2002
Arend van Spriel5b435de2011-10-05 13:19:03 +02002003static void
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002004brcmf_sdio_wait_event_wakeup(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002005{
2006 if (waitqueue_active(&bus->ctrl_wait))
2007 wake_up_interruptible(&bus->ctrl_wait);
2008 return;
2009}
2010
Franky Lin8da9d2c2013-11-29 12:25:10 +01002011static int brcmf_sdio_txpkt_hdalign(struct brcmf_sdio *bus, struct sk_buff *pkt)
2012{
Arend van Spriele217d1c2013-11-29 12:25:13 +01002013 u16 head_pad;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002014 u8 *dat_buf;
2015
Franky Lin8da9d2c2013-11-29 12:25:10 +01002016 dat_buf = (u8 *)(pkt->data);
2017
2018 /* Check head padding */
Arend van Spriele217d1c2013-11-29 12:25:13 +01002019 head_pad = ((unsigned long)dat_buf % bus->head_align);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002020 if (head_pad) {
2021 if (skb_headroom(pkt) < head_pad) {
2022 bus->sdiodev->bus_if->tx_realloc++;
2023 head_pad = 0;
2024 if (skb_cow(pkt, head_pad))
2025 return -ENOMEM;
2026 }
2027 skb_push(pkt, head_pad);
2028 dat_buf = (u8 *)(pkt->data);
2029 memset(dat_buf, 0, head_pad + bus->tx_hdrlen);
2030 }
2031 return head_pad;
2032}
2033
Franky Lin5491c112013-09-25 13:05:43 +02002034/**
2035 * struct brcmf_skbuff_cb reserves first two bytes in sk_buff::cb for
2036 * bus layer usage.
2037 */
Franky Linb05e9252013-08-10 12:27:26 +02002038/* flag marking a dummy skb added for DMA alignment requirement */
Franky Lin5491c112013-09-25 13:05:43 +02002039#define ALIGN_SKB_FLAG 0x8000
Franky Linb05e9252013-08-10 12:27:26 +02002040/* bit mask of data length chopped from the previous packet */
Franky Lin5491c112013-09-25 13:05:43 +02002041#define ALIGN_SKB_CHOP_LEN_MASK 0x7fff
2042
Franky Lin8da9d2c2013-11-29 12:25:10 +01002043static int brcmf_sdio_txpkt_prep_sg(struct brcmf_sdio *bus,
Arend van Spriela64304f2013-10-15 15:44:56 +02002044 struct sk_buff_head *pktq,
Franky Lin8da9d2c2013-11-29 12:25:10 +01002045 struct sk_buff *pkt, u16 total_len)
Arend van Spriela64304f2013-10-15 15:44:56 +02002046{
Franky Lin8da9d2c2013-11-29 12:25:10 +01002047 struct brcmf_sdio_dev *sdiodev;
Arend van Spriela64304f2013-10-15 15:44:56 +02002048 struct sk_buff *pkt_pad;
Arend van Spriele217d1c2013-11-29 12:25:13 +01002049 u16 tail_pad, tail_chop, chain_pad;
Arend van Spriela64304f2013-10-15 15:44:56 +02002050 unsigned int blksize;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002051 bool lastfrm;
2052 int ntail, ret;
Arend van Spriela64304f2013-10-15 15:44:56 +02002053
Franky Lin8da9d2c2013-11-29 12:25:10 +01002054 sdiodev = bus->sdiodev;
Arend van Spriela64304f2013-10-15 15:44:56 +02002055 blksize = sdiodev->func[SDIO_FUNC_2]->cur_blksize;
Arend van Spriela64304f2013-10-15 15:44:56 +02002056 /* sg entry alignment should be a divisor of block size */
Arend van Spriele217d1c2013-11-29 12:25:13 +01002057 WARN_ON(blksize % bus->sgentry_align);
Arend van Spriela64304f2013-10-15 15:44:56 +02002058
2059 /* Check tail padding */
Franky Lin8da9d2c2013-11-29 12:25:10 +01002060 lastfrm = skb_queue_is_last(pktq, pkt);
2061 tail_pad = 0;
Arend van Spriele217d1c2013-11-29 12:25:13 +01002062 tail_chop = pkt->len % bus->sgentry_align;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002063 if (tail_chop)
Arend van Spriele217d1c2013-11-29 12:25:13 +01002064 tail_pad = bus->sgentry_align - tail_chop;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002065 chain_pad = (total_len + tail_pad) % blksize;
2066 if (lastfrm && chain_pad)
2067 tail_pad += blksize - chain_pad;
Arend van Spriela64304f2013-10-15 15:44:56 +02002068 if (skb_tailroom(pkt) < tail_pad && pkt->len > blksize) {
Arend van Spriel1eb43012014-02-20 18:55:55 +01002069 pkt_pad = brcmu_pkt_buf_get_skb(tail_pad + tail_chop +
2070 bus->head_align);
Arend van Spriela64304f2013-10-15 15:44:56 +02002071 if (pkt_pad == NULL)
2072 return -ENOMEM;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002073 ret = brcmf_sdio_txpkt_hdalign(bus, pkt_pad);
Dave Jones2dc3a8e2014-03-06 15:51:08 -05002074 if (unlikely(ret < 0)) {
2075 kfree_skb(pkt_pad);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002076 return ret;
Dave Jones2dc3a8e2014-03-06 15:51:08 -05002077 }
Arend van Spriela64304f2013-10-15 15:44:56 +02002078 memcpy(pkt_pad->data,
2079 pkt->data + pkt->len - tail_chop,
2080 tail_chop);
Arend van Spriel5aa9f0e2014-02-25 20:30:27 +01002081 *(u16 *)(pkt_pad->cb) = ALIGN_SKB_FLAG + tail_chop;
Arend van Spriela64304f2013-10-15 15:44:56 +02002082 skb_trim(pkt, pkt->len - tail_chop);
Arend van Spriel1eb43012014-02-20 18:55:55 +01002083 skb_trim(pkt_pad, tail_pad + tail_chop);
Arend van Spriela64304f2013-10-15 15:44:56 +02002084 __skb_queue_after(pktq, pkt, pkt_pad);
2085 } else {
2086 ntail = pkt->data_len + tail_pad -
2087 (pkt->end - pkt->tail);
2088 if (skb_cloned(pkt) || ntail > 0)
2089 if (pskb_expand_head(pkt, 0, ntail, GFP_ATOMIC))
2090 return -ENOMEM;
2091 if (skb_linearize(pkt))
2092 return -ENOMEM;
Arend van Spriela64304f2013-10-15 15:44:56 +02002093 __skb_put(pkt, tail_pad);
2094 }
2095
Franky Lin8da9d2c2013-11-29 12:25:10 +01002096 return tail_pad;
Arend van Spriela64304f2013-10-15 15:44:56 +02002097}
2098
Franky Linb05e9252013-08-10 12:27:26 +02002099/**
2100 * brcmf_sdio_txpkt_prep - packet preparation for transmit
2101 * @bus: brcmf_sdio structure pointer
2102 * @pktq: packet list pointer
2103 * @chan: virtual channel to transmit the packet
2104 *
2105 * Processes to be applied to the packet
2106 * - Align data buffer pointer
2107 * - Align data buffer length
2108 * - Prepare header
2109 * Return: negative value if there is error
2110 */
2111static int
2112brcmf_sdio_txpkt_prep(struct brcmf_sdio *bus, struct sk_buff_head *pktq,
2113 uint chan)
2114{
Franky Lin8da9d2c2013-11-29 12:25:10 +01002115 u16 head_pad, total_len;
Arend van Spriela64304f2013-10-15 15:44:56 +02002116 struct sk_buff *pkt_next;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002117 u8 txseq;
2118 int ret;
Franky Lin6bc52312013-08-10 12:27:28 +02002119 struct brcmf_sdio_hdrinfo hd_info = {0};
Franky Linb05e9252013-08-10 12:27:26 +02002120
Franky Lin8da9d2c2013-11-29 12:25:10 +01002121 txseq = bus->tx_seq;
2122 total_len = 0;
2123 skb_queue_walk(pktq, pkt_next) {
2124 /* alignment packet inserted in previous
2125 * loop cycle can be skipped as it is
2126 * already properly aligned and does not
2127 * need an sdpcm header.
2128 */
Arend van Spriel5aa9f0e2014-02-25 20:30:27 +01002129 if (*(u16 *)(pkt_next->cb) & ALIGN_SKB_FLAG)
Franky Lin8da9d2c2013-11-29 12:25:10 +01002130 continue;
Franky Linb05e9252013-08-10 12:27:26 +02002131
Franky Lin8da9d2c2013-11-29 12:25:10 +01002132 /* align packet data pointer */
2133 ret = brcmf_sdio_txpkt_hdalign(bus, pkt_next);
2134 if (ret < 0)
2135 return ret;
2136 head_pad = (u16)ret;
2137 if (head_pad)
Arend van Spriel1eb43012014-02-20 18:55:55 +01002138 memset(pkt_next->data + bus->tx_hdrlen, 0, head_pad);
Franky Linb05e9252013-08-10 12:27:26 +02002139
Franky Lin8da9d2c2013-11-29 12:25:10 +01002140 total_len += pkt_next->len;
Franky Linb05e9252013-08-10 12:27:26 +02002141
Arend van Spriela64304f2013-10-15 15:44:56 +02002142 hd_info.len = pkt_next->len;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002143 hd_info.lastfrm = skb_queue_is_last(pktq, pkt_next);
2144 if (bus->txglom && pktq->qlen > 1) {
2145 ret = brcmf_sdio_txpkt_prep_sg(bus, pktq,
2146 pkt_next, total_len);
2147 if (ret < 0)
2148 return ret;
2149 hd_info.tail_pad = (u16)ret;
2150 total_len += (u16)ret;
2151 }
2152
2153 hd_info.channel = chan;
2154 hd_info.dat_offset = head_pad + bus->tx_hdrlen;
2155 hd_info.seq_num = txseq++;
2156
2157 /* Now fill the header */
2158 brcmf_sdio_hdpack(bus, pkt_next->data, &hd_info);
2159
2160 if (BRCMF_BYTES_ON() &&
2161 ((BRCMF_CTL_ON() && chan == SDPCM_CONTROL_CHANNEL) ||
2162 (BRCMF_DATA_ON() && chan != SDPCM_CONTROL_CHANNEL)))
Hante Meuleman47ab4cd2014-02-25 20:30:29 +01002163 brcmf_dbg_hex_dump(true, pkt_next->data, hd_info.len,
Franky Lin8da9d2c2013-11-29 12:25:10 +01002164 "Tx Frame:\n");
2165 else if (BRCMF_HDRS_ON())
Hante Meuleman47ab4cd2014-02-25 20:30:29 +01002166 brcmf_dbg_hex_dump(true, pkt_next->data,
Franky Lin8da9d2c2013-11-29 12:25:10 +01002167 head_pad + bus->tx_hdrlen,
2168 "Tx Header:\n");
Franky Linb05e9252013-08-10 12:27:26 +02002169 }
Franky Lin8da9d2c2013-11-29 12:25:10 +01002170 /* Hardware length tag of the first packet should be total
2171 * length of the chain (including padding)
2172 */
2173 if (bus->txglom)
2174 brcmf_sdio_update_hwhdr(pktq->next->data, total_len);
Franky Linb05e9252013-08-10 12:27:26 +02002175 return 0;
2176}
2177
2178/**
2179 * brcmf_sdio_txpkt_postp - packet post processing for transmit
2180 * @bus: brcmf_sdio structure pointer
2181 * @pktq: packet list pointer
2182 *
2183 * Processes to be applied to the packet
2184 * - Remove head padding
2185 * - Remove tail padding
2186 */
2187static void
2188brcmf_sdio_txpkt_postp(struct brcmf_sdio *bus, struct sk_buff_head *pktq)
2189{
2190 u8 *hdr;
2191 u32 dat_offset;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002192 u16 tail_pad;
Arend van Spriel5aa9f0e2014-02-25 20:30:27 +01002193 u16 dummy_flags, chop_len;
Franky Linb05e9252013-08-10 12:27:26 +02002194 struct sk_buff *pkt_next, *tmp, *pkt_prev;
2195
2196 skb_queue_walk_safe(pktq, pkt_next, tmp) {
Arend van Spriel5aa9f0e2014-02-25 20:30:27 +01002197 dummy_flags = *(u16 *)(pkt_next->cb);
Franky Lin5491c112013-09-25 13:05:43 +02002198 if (dummy_flags & ALIGN_SKB_FLAG) {
2199 chop_len = dummy_flags & ALIGN_SKB_CHOP_LEN_MASK;
Franky Linb05e9252013-08-10 12:27:26 +02002200 if (chop_len) {
2201 pkt_prev = pkt_next->prev;
Franky Linb05e9252013-08-10 12:27:26 +02002202 skb_put(pkt_prev, chop_len);
2203 }
2204 __skb_unlink(pkt_next, pktq);
2205 brcmu_pkt_buf_free_skb(pkt_next);
2206 } else {
Franky Lin8da9d2c2013-11-29 12:25:10 +01002207 hdr = pkt_next->data + bus->tx_hdrlen - SDPCM_SWHDR_LEN;
Franky Linb05e9252013-08-10 12:27:26 +02002208 dat_offset = le32_to_cpu(*(__le32 *)hdr);
2209 dat_offset = (dat_offset & SDPCM_DOFFSET_MASK) >>
2210 SDPCM_DOFFSET_SHIFT;
2211 skb_pull(pkt_next, dat_offset);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002212 if (bus->txglom) {
2213 tail_pad = le16_to_cpu(*(__le16 *)(hdr - 2));
2214 skb_trim(pkt_next, pkt_next->len - tail_pad);
2215 }
Franky Linb05e9252013-08-10 12:27:26 +02002216 }
2217 }
2218}
2219
Arend van Spriel5b435de2011-10-05 13:19:03 +02002220/* Writes a HW/SW header into the packet and sends it. */
2221/* Assumes: (a) header space already there, (b) caller holds lock */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002222static int brcmf_sdio_txpkt(struct brcmf_sdio *bus, struct sk_buff_head *pktq,
2223 uint chan)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002224{
2225 int ret;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002226 struct sk_buff *pkt_next, *tmp;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002227
2228 brcmf_dbg(TRACE, "Enter\n");
2229
Franky Lin8da9d2c2013-11-29 12:25:10 +01002230 ret = brcmf_sdio_txpkt_prep(bus, pktq, chan);
Franky Linb05e9252013-08-10 12:27:26 +02002231 if (ret)
2232 goto done;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002233
Franky Lin38b0b0d2012-11-05 16:22:24 -08002234 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriela7cdd822013-12-12 11:58:59 +01002235 ret = brcmf_sdiod_send_pkt(bus->sdiodev, pktq);
Arend van Spriel80969832012-06-09 22:51:44 +02002236 bus->sdcnt.f2txdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002237
Hante Meuleman81c78832014-02-25 20:30:35 +01002238 if (ret < 0)
2239 brcmf_sdio_txfail(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002240
Franky Lin38b0b0d2012-11-05 16:22:24 -08002241 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002242
2243done:
Franky Lin8da9d2c2013-11-29 12:25:10 +01002244 brcmf_sdio_txpkt_postp(bus, pktq);
2245 if (ret == 0)
2246 bus->tx_seq = (bus->tx_seq + pktq->qlen) % SDPCM_SEQ_WRAP;
2247 skb_queue_walk_safe(pktq, pkt_next, tmp) {
2248 __skb_unlink(pkt_next, pktq);
2249 brcmf_txcomplete(bus->sdiodev->dev, pkt_next, ret == 0);
2250 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002251 return ret;
2252}
2253
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002254static uint brcmf_sdio_sendfromq(struct brcmf_sdio *bus, uint maxframes)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002255{
2256 struct sk_buff *pkt;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002257 struct sk_buff_head pktq;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002258 u32 intstatus = 0;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002259 int ret = 0, prec_out, i;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002260 uint cnt = 0;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002261 u8 tx_prec_map, pkt_num;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002262
Arend van Spriel5b435de2011-10-05 13:19:03 +02002263 brcmf_dbg(TRACE, "Enter\n");
2264
2265 tx_prec_map = ~bus->flowcontrol;
2266
2267 /* Send frames until the limit or some other event */
Franky Lin8da9d2c2013-11-29 12:25:10 +01002268 for (cnt = 0; (cnt < maxframes) && data_ok(bus);) {
2269 pkt_num = 1;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002270 if (bus->txglom)
2271 pkt_num = min_t(u8, bus->tx_max - bus->tx_seq,
Arend van Sprielaf1fa212014-02-27 19:25:00 +01002272 bus->sdiodev->txglomsz);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002273 pkt_num = min_t(u32, pkt_num,
2274 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol));
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002275 __skb_queue_head_init(&pktq);
2276 spin_lock_bh(&bus->txq_lock);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002277 for (i = 0; i < pkt_num; i++) {
2278 pkt = brcmu_pktq_mdeq(&bus->txq, tx_prec_map,
2279 &prec_out);
2280 if (pkt == NULL)
2281 break;
2282 __skb_queue_tail(&pktq, pkt);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002283 }
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002284 spin_unlock_bh(&bus->txq_lock);
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002285 if (i == 0)
Franky Lin8da9d2c2013-11-29 12:25:10 +01002286 break;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002287
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002288 ret = brcmf_sdio_txpkt(bus, &pktq, SDPCM_DATA_CHANNEL);
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002289
Franky Lin8da9d2c2013-11-29 12:25:10 +01002290 cnt += i;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002291
2292 /* In poll mode, need to check for other events */
Hante Meulemanb6a8cf22014-02-25 20:30:33 +01002293 if (!bus->intr) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02002294 /* Check device status, signal pending interrupt */
Franky Lin38b0b0d2012-11-05 16:22:24 -08002295 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin5c15c232012-05-04 18:27:37 -07002296 ret = r_sdreg32(bus, &intstatus,
2297 offsetof(struct sdpcmd_regs,
2298 intstatus));
Franky Lin38b0b0d2012-11-05 16:22:24 -08002299 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel80969832012-06-09 22:51:44 +02002300 bus->sdcnt.f2txdata++;
Franky Lin5c15c232012-05-04 18:27:37 -07002301 if (ret != 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002302 break;
2303 if (intstatus & bus->hostintmask)
Franky Lin1d382272012-09-13 21:11:59 +02002304 atomic_set(&bus->ipend, 1);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002305 }
2306 }
2307
2308 /* Deflow-control stack if needed */
Arend van Spriela1ce7a02015-02-06 18:36:42 +01002309 if ((bus->sdiodev->state == BRCMF_SDIOD_DATA) &&
Franky Linc8bf3482011-12-16 18:37:08 -08002310 bus->txoff && (pktq_len(&bus->txq) < TXLOW)) {
Hante Meuleman90d03ff2012-09-11 21:18:46 +02002311 bus->txoff = false;
2312 brcmf_txflowblock(bus->sdiodev->dev, false);
Franky Linc8bf3482011-12-16 18:37:08 -08002313 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002314
2315 return cnt;
2316}
2317
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002318static int brcmf_sdio_tx_ctrlframe(struct brcmf_sdio *bus, u8 *frame, u16 len)
2319{
2320 u8 doff;
2321 u16 pad;
2322 uint retries = 0;
2323 struct brcmf_sdio_hdrinfo hd_info = {0};
2324 int ret;
2325
2326 brcmf_dbg(TRACE, "Enter\n");
2327
2328 /* Back the pointer to make room for bus header */
2329 frame -= bus->tx_hdrlen;
2330 len += bus->tx_hdrlen;
2331
2332 /* Add alignment padding (optional for ctl frames) */
2333 doff = ((unsigned long)frame % bus->head_align);
2334 if (doff) {
2335 frame -= doff;
2336 len += doff;
2337 memset(frame + bus->tx_hdrlen, 0, doff);
2338 }
2339
2340 /* Round send length to next SDIO block */
2341 pad = 0;
2342 if (bus->roundup && bus->blocksize && (len > bus->blocksize)) {
2343 pad = bus->blocksize - (len % bus->blocksize);
2344 if ((pad > bus->roundup) || (pad >= bus->blocksize))
2345 pad = 0;
2346 } else if (len % bus->head_align) {
2347 pad = bus->head_align - (len % bus->head_align);
2348 }
2349 len += pad;
2350
2351 hd_info.len = len - pad;
2352 hd_info.channel = SDPCM_CONTROL_CHANNEL;
2353 hd_info.dat_offset = doff + bus->tx_hdrlen;
2354 hd_info.seq_num = bus->tx_seq;
2355 hd_info.lastfrm = true;
2356 hd_info.tail_pad = pad;
2357 brcmf_sdio_hdpack(bus, frame, &hd_info);
2358
2359 if (bus->txglom)
2360 brcmf_sdio_update_hwhdr(frame, len);
2361
2362 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_CTL_ON(),
2363 frame, len, "Tx Frame:\n");
2364 brcmf_dbg_hex_dump(!(BRCMF_BYTES_ON() && BRCMF_CTL_ON()) &&
2365 BRCMF_HDRS_ON(),
2366 frame, min_t(u16, len, 16), "TxHdr:\n");
2367
2368 do {
2369 ret = brcmf_sdiod_send_buf(bus->sdiodev, frame, len);
2370
2371 if (ret < 0)
2372 brcmf_sdio_txfail(bus);
2373 else
2374 bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQ_WRAP;
2375 } while (ret < 0 && retries++ < TXRETRIES);
2376
2377 return ret;
2378}
2379
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002380static void brcmf_sdio_bus_stop(struct device *dev)
Franky Lina9ffda82011-12-16 18:37:12 -08002381{
2382 u32 local_hostintmask;
2383 u8 saveclk;
Franky Lina9ffda82011-12-16 18:37:12 -08002384 int err;
2385 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01002386 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Lina9ffda82011-12-16 18:37:12 -08002387 struct brcmf_sdio *bus = sdiodev->bus;
2388
2389 brcmf_dbg(TRACE, "Enter\n");
2390
2391 if (bus->watchdog_tsk) {
2392 send_sig(SIGTERM, bus->watchdog_tsk, 1);
2393 kthread_stop(bus->watchdog_tsk);
2394 bus->watchdog_tsk = NULL;
2395 }
2396
Arend van Spriela1ce7a02015-02-06 18:36:42 +01002397 if (sdiodev->state != BRCMF_SDIOD_NOMEDIUM) {
Arend van Sprielbb350712014-01-13 22:20:29 +01002398 sdio_claim_host(sdiodev->func[1]);
Franky Lina9ffda82011-12-16 18:37:12 -08002399
Arend van Sprielbb350712014-01-13 22:20:29 +01002400 /* Enable clock for device interrupts */
2401 brcmf_sdio_bus_sleep(bus, false, false);
Franky Lina9ffda82011-12-16 18:37:12 -08002402
Arend van Sprielbb350712014-01-13 22:20:29 +01002403 /* Disable and clear interrupts at the chip level also */
2404 w_sdreg32(bus, 0, offsetof(struct sdpcmd_regs, hostintmask));
2405 local_hostintmask = bus->hostintmask;
2406 bus->hostintmask = 0;
Franky Lina9ffda82011-12-16 18:37:12 -08002407
Arend van Sprielbb350712014-01-13 22:20:29 +01002408 /* Force backplane clocks to assure F2 interrupt propagates */
2409 saveclk = brcmf_sdiod_regrb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
2410 &err);
2411 if (!err)
2412 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
2413 (saveclk | SBSDIO_FORCE_HT), &err);
2414 if (err)
2415 brcmf_err("Failed to force clock for F2: err %d\n",
2416 err);
Franky Lina9ffda82011-12-16 18:37:12 -08002417
Arend van Sprielbb350712014-01-13 22:20:29 +01002418 /* Turn off the bus (F2), free any pending packets */
2419 brcmf_dbg(INTR, "disable SDIO interrupts\n");
2420 sdio_disable_func(sdiodev->func[SDIO_FUNC_2]);
2421
2422 /* Clear any pending interrupts now that F2 is disabled */
2423 w_sdreg32(bus, local_hostintmask,
2424 offsetof(struct sdpcmd_regs, intstatus));
2425
2426 sdio_release_host(sdiodev->func[1]);
Franky Lina9ffda82011-12-16 18:37:12 -08002427 }
Franky Lina9ffda82011-12-16 18:37:12 -08002428 /* Clear the data packet queues */
2429 brcmu_pktq_flush(&bus->txq, true, NULL, NULL);
2430
2431 /* Clear any held glomming stuff */
Markus Elfring297540f2015-02-04 20:28:49 +01002432 brcmu_pkt_buf_free_skb(bus->glomd);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002433 brcmf_sdio_free_glom(bus);
Franky Lina9ffda82011-12-16 18:37:12 -08002434
2435 /* Clear rx control and wake any waiters */
Franky Lindd43a012012-11-05 16:22:22 -08002436 spin_lock_bh(&bus->rxctl_lock);
Franky Lina9ffda82011-12-16 18:37:12 -08002437 bus->rxlen = 0;
Franky Lindd43a012012-11-05 16:22:22 -08002438 spin_unlock_bh(&bus->rxctl_lock);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002439 brcmf_sdio_dcmd_resp_wake(bus);
Franky Lina9ffda82011-12-16 18:37:12 -08002440
2441 /* Reset some F2 state stuff */
2442 bus->rxskip = false;
2443 bus->tx_seq = bus->rx_seq = 0;
Franky Lina9ffda82011-12-16 18:37:12 -08002444}
2445
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002446static inline void brcmf_sdio_clrintr(struct brcmf_sdio *bus)
Franky Linba89bf12012-04-27 18:56:59 -07002447{
2448 unsigned long flags;
2449
Hante Meuleman668761a2013-04-12 10:55:55 +02002450 if (bus->sdiodev->oob_irq_requested) {
2451 spin_lock_irqsave(&bus->sdiodev->irq_en_lock, flags);
2452 if (!bus->sdiodev->irq_en && !atomic_read(&bus->ipend)) {
2453 enable_irq(bus->sdiodev->pdata->oob_irq_nr);
2454 bus->sdiodev->irq_en = true;
2455 }
2456 spin_unlock_irqrestore(&bus->sdiodev->irq_en_lock, flags);
Franky Linba89bf12012-04-27 18:56:59 -07002457 }
Franky Linba89bf12012-04-27 18:56:59 -07002458}
Franky Linba89bf12012-04-27 18:56:59 -07002459
Franky Lin45316032012-09-13 21:12:03 +02002460static int brcmf_sdio_intr_rstatus(struct brcmf_sdio *bus)
2461{
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01002462 struct brcmf_core *buscore;
Franky Lin45316032012-09-13 21:12:03 +02002463 u32 addr;
2464 unsigned long val;
Hante Meuleman5cbb9c22014-03-06 10:16:12 +01002465 int ret;
Franky Lin45316032012-09-13 21:12:03 +02002466
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01002467 buscore = brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV);
2468 addr = buscore->base + offsetof(struct sdpcmd_regs, intstatus);
Franky Lin45316032012-09-13 21:12:03 +02002469
Arend van Spriela39be272013-12-12 11:58:58 +01002470 val = brcmf_sdiod_regrl(bus->sdiodev, addr, &ret);
Franky Lin45316032012-09-13 21:12:03 +02002471 bus->sdcnt.f1regdata++;
2472 if (ret != 0)
Hante Meuleman5cbb9c22014-03-06 10:16:12 +01002473 return ret;
Franky Lin45316032012-09-13 21:12:03 +02002474
2475 val &= bus->hostintmask;
2476 atomic_set(&bus->fcstate, !!(val & I_HMB_FC_STATE));
2477
2478 /* Clear interrupts */
2479 if (val) {
Arend van Spriela39be272013-12-12 11:58:58 +01002480 brcmf_sdiod_regwl(bus->sdiodev, addr, val, &ret);
Franky Lin45316032012-09-13 21:12:03 +02002481 bus->sdcnt.f1regdata++;
Vineet Guptad3928d02015-07-09 13:43:18 +05302482 atomic_or(val, &bus->intstatus);
Franky Lin45316032012-09-13 21:12:03 +02002483 }
2484
2485 return ret;
2486}
2487
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002488static void brcmf_sdio_dpc(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002489{
Franky Lin45316032012-09-13 21:12:03 +02002490 u32 newstatus = 0;
2491 unsigned long intstatus;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002492 uint txlimit = bus->txbound; /* Tx frames to send before resched */
Hante Meulemanb6a8cf22014-02-25 20:30:33 +01002493 uint framecnt; /* Temporary counter of tx/rx frames */
Hante Meuleman5cbb9c22014-03-06 10:16:12 +01002494 int err = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002495
2496 brcmf_dbg(TRACE, "Enter\n");
2497
Franky Lin38b0b0d2012-11-05 16:22:24 -08002498 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002499
2500 /* If waiting for HTAVAIL, check status */
Piotr Haber4a3da992013-04-11 13:28:46 +02002501 if (!bus->sr_enabled && bus->clkstate == CLK_PENDING) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02002502 u8 clkctl, devctl = 0;
2503
Joe Perches8ae74652012-01-15 00:38:38 -08002504#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02002505 /* Check for inconsistent device control */
Arend van Spriela39be272013-12-12 11:58:58 +01002506 devctl = brcmf_sdiod_regrb(bus->sdiodev,
2507 SBSDIO_DEVICE_CTL, &err);
Joe Perches8ae74652012-01-15 00:38:38 -08002508#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02002509
2510 /* Read CSR, if clock on switch to AVAIL, else ignore */
Arend van Spriela39be272013-12-12 11:58:58 +01002511 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
2512 SBSDIO_FUNC1_CHIPCLKCSR, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002513
Arend van Sprielc3203372013-04-03 12:40:44 +02002514 brcmf_dbg(SDIO, "DPC: PENDING, devctl 0x%02x clkctl 0x%02x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02002515 devctl, clkctl);
2516
2517 if (SBSDIO_HTAV(clkctl)) {
Arend van Spriela39be272013-12-12 11:58:58 +01002518 devctl = brcmf_sdiod_regrb(bus->sdiodev,
2519 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002520 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
Arend van Spriela39be272013-12-12 11:58:58 +01002521 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
2522 devctl, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002523 bus->clkstate = CLK_AVAIL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002524 }
2525 }
2526
Arend van Spriel5b435de2011-10-05 13:19:03 +02002527 /* Make sure backplane clock is on */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002528 brcmf_sdio_bus_sleep(bus, false, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002529
2530 /* Pending interrupt indicates new device status */
Franky Lin1d382272012-09-13 21:11:59 +02002531 if (atomic_read(&bus->ipend) > 0) {
2532 atomic_set(&bus->ipend, 0);
Franky Lin45316032012-09-13 21:12:03 +02002533 err = brcmf_sdio_intr_rstatus(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002534 }
2535
Franky Lin45316032012-09-13 21:12:03 +02002536 /* Start with leftover status bits */
2537 intstatus = atomic_xchg(&bus->intstatus, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002538
2539 /* Handle flow-control change: read new state in case our ack
2540 * crossed another change interrupt. If change still set, assume
2541 * FC ON for safety, let next loop through do the debounce.
2542 */
2543 if (intstatus & I_HMB_FC_CHANGE) {
2544 intstatus &= ~I_HMB_FC_CHANGE;
Franky Lin5c15c232012-05-04 18:27:37 -07002545 err = w_sdreg32(bus, I_HMB_FC_CHANGE,
2546 offsetof(struct sdpcmd_regs, intstatus));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002547
Franky Lin5c15c232012-05-04 18:27:37 -07002548 err = r_sdreg32(bus, &newstatus,
2549 offsetof(struct sdpcmd_regs, intstatus));
Arend van Spriel80969832012-06-09 22:51:44 +02002550 bus->sdcnt.f1regdata += 2;
Franky Lin45316032012-09-13 21:12:03 +02002551 atomic_set(&bus->fcstate,
2552 !!(newstatus & (I_HMB_FC_STATE | I_HMB_FC_CHANGE)));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002553 intstatus |= (newstatus & bus->hostintmask);
2554 }
2555
2556 /* Handle host mailbox indication */
2557 if (intstatus & I_HMB_HOST_INT) {
2558 intstatus &= ~I_HMB_HOST_INT;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002559 intstatus |= brcmf_sdio_hostmail(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002560 }
2561
Franky Lin38b0b0d2012-11-05 16:22:24 -08002562 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin7cdf57d2012-11-05 16:22:23 -08002563
Arend van Spriel5b435de2011-10-05 13:19:03 +02002564 /* Generally don't ask for these, can get CRC errors... */
2565 if (intstatus & I_WR_OOSYNC) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002566 brcmf_err("Dongle reports WR_OOSYNC\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002567 intstatus &= ~I_WR_OOSYNC;
2568 }
2569
2570 if (intstatus & I_RD_OOSYNC) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002571 brcmf_err("Dongle reports RD_OOSYNC\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002572 intstatus &= ~I_RD_OOSYNC;
2573 }
2574
2575 if (intstatus & I_SBINT) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002576 brcmf_err("Dongle reports SBINT\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002577 intstatus &= ~I_SBINT;
2578 }
2579
2580 /* Would be active due to wake-wlan in gSPI */
2581 if (intstatus & I_CHIPACTIVE) {
2582 brcmf_dbg(INFO, "Dongle reports CHIPACTIVE\n");
2583 intstatus &= ~I_CHIPACTIVE;
2584 }
2585
2586 /* Ignore frame indications if rxskip is set */
2587 if (bus->rxskip)
2588 intstatus &= ~I_HMB_FRAME_IND;
2589
2590 /* On frame indication, read available frames */
Hante Meulemanb6a8cf22014-02-25 20:30:33 +01002591 if ((intstatus & I_HMB_FRAME_IND) && (bus->clkstate == CLK_AVAIL)) {
2592 brcmf_sdio_readframes(bus, bus->rxbound);
Franky Lin4754fce2012-09-19 22:21:11 +02002593 if (!bus->rxpending)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002594 intstatus &= ~I_HMB_FRAME_IND;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002595 }
2596
2597 /* Keep still-pending events for next scheduling */
Hante Meuleman5cbb9c22014-03-06 10:16:12 +01002598 if (intstatus)
Vineet Guptad3928d02015-07-09 13:43:18 +05302599 atomic_or(intstatus, &bus->intstatus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002600
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002601 brcmf_sdio_clrintr(bus);
Franky Linba89bf12012-04-27 18:56:59 -07002602
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002603 if (bus->ctrl_frame_stat && (bus->clkstate == CLK_AVAIL) &&
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002604 data_ok(bus)) {
2605 sdio_claim_host(bus->sdiodev->func[1]);
Hante Meuleman449e58b2015-03-06 18:40:40 +01002606 if (bus->ctrl_frame_stat) {
2607 err = brcmf_sdio_tx_ctrlframe(bus, bus->ctrl_frame_buf,
2608 bus->ctrl_frame_len);
2609 bus->ctrl_frame_err = err;
Hante Meuleman2c64e162015-03-18 13:25:22 +01002610 wmb();
Hante Meuleman449e58b2015-03-06 18:40:40 +01002611 bus->ctrl_frame_stat = false;
2612 }
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002613 sdio_release_host(bus->sdiodev->func[1]);
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002614 brcmf_sdio_wait_event_wakeup(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002615 }
2616 /* Send queued frames (limit 1 if rx may still be pending) */
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002617 if ((bus->clkstate == CLK_AVAIL) && !atomic_read(&bus->fcstate) &&
2618 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) && txlimit &&
2619 data_ok(bus)) {
Franky Lin4754fce2012-09-19 22:21:11 +02002620 framecnt = bus->rxpending ? min(txlimit, bus->txminmax) :
2621 txlimit;
Hante Meulemanb6a8cf22014-02-25 20:30:33 +01002622 brcmf_sdio_sendfromq(bus, framecnt);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002623 }
2624
Arend van Spriela1ce7a02015-02-06 18:36:42 +01002625 if ((bus->sdiodev->state != BRCMF_SDIOD_DATA) || (err != 0)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002626 brcmf_err("failed backplane access over SDIO, halting operation\n");
Franky Lin45316032012-09-13 21:12:03 +02002627 atomic_set(&bus->intstatus, 0);
Hante Meulemande6878c2015-03-06 18:40:38 +01002628 if (bus->ctrl_frame_stat) {
Hante Meuleman449e58b2015-03-06 18:40:40 +01002629 sdio_claim_host(bus->sdiodev->func[1]);
2630 if (bus->ctrl_frame_stat) {
2631 bus->ctrl_frame_err = -ENODEV;
Hante Meuleman2c64e162015-03-18 13:25:22 +01002632 wmb();
Hante Meuleman449e58b2015-03-06 18:40:40 +01002633 bus->ctrl_frame_stat = false;
2634 brcmf_sdio_wait_event_wakeup(bus);
2635 }
2636 sdio_release_host(bus->sdiodev->func[1]);
Hante Meulemande6878c2015-03-06 18:40:38 +01002637 }
Franky Lin45316032012-09-13 21:12:03 +02002638 } else if (atomic_read(&bus->intstatus) ||
2639 atomic_read(&bus->ipend) > 0 ||
2640 (!atomic_read(&bus->fcstate) &&
2641 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) &&
Hante Meulemanb6a8cf22014-02-25 20:30:33 +01002642 data_ok(bus))) {
Hante Meuleman2c64e162015-03-18 13:25:22 +01002643 bus->dpc_triggered = true;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002644 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002645}
2646
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002647static struct pktq *brcmf_sdio_bus_gettxq(struct device *dev)
Arend van Spriele2432b62013-04-03 12:40:38 +02002648{
2649 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
2650 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
2651 struct brcmf_sdio *bus = sdiodev->bus;
2652
2653 return &bus->txq;
2654}
2655
Hante Meuleman84936622014-10-28 14:56:19 +01002656static bool brcmf_sdio_prec_enq(struct pktq *q, struct sk_buff *pkt, int prec)
2657{
2658 struct sk_buff *p;
2659 int eprec = -1; /* precedence to evict from */
2660
2661 /* Fast case, precedence queue is not full and we are also not
2662 * exceeding total queue length
2663 */
2664 if (!pktq_pfull(q, prec) && !pktq_full(q)) {
2665 brcmu_pktq_penq(q, prec, pkt);
2666 return true;
2667 }
2668
2669 /* Determine precedence from which to evict packet, if any */
2670 if (pktq_pfull(q, prec)) {
2671 eprec = prec;
2672 } else if (pktq_full(q)) {
2673 p = brcmu_pktq_peek_tail(q, &eprec);
2674 if (eprec > prec)
2675 return false;
2676 }
2677
2678 /* Evict if needed */
2679 if (eprec >= 0) {
2680 /* Detect queueing to unconfigured precedence */
2681 if (eprec == prec)
2682 return false; /* refuse newer (incoming) packet */
2683 /* Evict packet according to discard policy */
2684 p = brcmu_pktq_pdeq_tail(q, eprec);
2685 if (p == NULL)
2686 brcmf_err("brcmu_pktq_pdeq_tail() failed\n");
2687 brcmu_pkt_buf_free_skb(p);
2688 }
2689
2690 /* Enqueue */
2691 p = brcmu_pktq_penq(q, prec, pkt);
2692 if (p == NULL)
2693 brcmf_err("brcmu_pktq_penq() failed\n");
2694
2695 return p != NULL;
2696}
2697
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002698static int brcmf_sdio_bus_txdata(struct device *dev, struct sk_buff *pkt)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002699{
2700 int ret = -EBADE;
Arend van Spriel44ff5662014-02-25 20:30:28 +01002701 uint prec;
Franky Linbf347bb2011-11-22 17:21:56 -08002702 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01002703 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Linbf347bb2011-11-22 17:21:56 -08002704 struct brcmf_sdio *bus = sdiodev->bus;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002705
Arend van Spriel44ff5662014-02-25 20:30:28 +01002706 brcmf_dbg(TRACE, "Enter: pkt: data %p len %d\n", pkt->data, pkt->len);
Arend van Spriel5768f312015-06-11 00:12:20 +02002707 if (sdiodev->state != BRCMF_SDIOD_DATA)
2708 return -EIO;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002709
2710 /* Add space for the header */
Franky Lin706478c2013-08-10 12:27:29 +02002711 skb_push(pkt, bus->tx_hdrlen);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002712 /* precondition: IS_ALIGNED((unsigned long)(pkt->data), 2) */
2713
2714 prec = prio2prec((pkt->priority & PRIOMASK));
2715
2716 /* Check for existing queue, current flow-control,
2717 pending event, or pending clock */
2718 brcmf_dbg(TRACE, "deferring pktq len %d\n", pktq_len(&bus->txq));
Arend van Spriel80969832012-06-09 22:51:44 +02002719 bus->sdcnt.fcqueued++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002720
2721 /* Priority based enq */
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002722 spin_lock_bh(&bus->txq_lock);
Arend van Spriel5aa9f0e2014-02-25 20:30:27 +01002723 /* reset bus_flags in packet cb */
2724 *(u16 *)(pkt->cb) = 0;
Hante Meuleman84936622014-10-28 14:56:19 +01002725 if (!brcmf_sdio_prec_enq(&bus->txq, pkt, prec)) {
Franky Lin706478c2013-08-10 12:27:29 +02002726 skb_pull(pkt, bus->tx_hdrlen);
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002727 brcmf_err("out of bus->txq !!!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002728 ret = -ENOSR;
2729 } else {
2730 ret = 0;
2731 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002732
Franky Linc8bf3482011-12-16 18:37:08 -08002733 if (pktq_len(&bus->txq) >= TXHI) {
Hante Meuleman90d03ff2012-09-11 21:18:46 +02002734 bus->txoff = true;
Hante Meuleman84936622014-10-28 14:56:19 +01002735 brcmf_txflowblock(dev, true);
Franky Linc8bf3482011-12-16 18:37:08 -08002736 }
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002737 spin_unlock_bh(&bus->txq_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002738
Joe Perches8ae74652012-01-15 00:38:38 -08002739#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02002740 if (pktq_plen(&bus->txq, prec) > qcount[prec])
2741 qcount[prec] = pktq_plen(&bus->txq, prec);
2742#endif
Franky Linf1e68c22012-09-13 21:12:00 +02002743
Arend van Spriel99824642015-02-06 18:36:45 +01002744 brcmf_sdio_trigger_dpc(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002745 return ret;
2746}
2747
Joe Perches8ae74652012-01-15 00:38:38 -08002748#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02002749#define CONSOLE_LINE_MAX 192
2750
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002751static int brcmf_sdio_readconsole(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002752{
2753 struct brcmf_console *c = &bus->console;
2754 u8 line[CONSOLE_LINE_MAX], ch;
2755 u32 n, idx, addr;
2756 int rv;
2757
2758 /* Don't do anything until FWREADY updates console address */
2759 if (bus->console_addr == 0)
2760 return 0;
2761
2762 /* Read console log struct */
2763 addr = bus->console_addr + offsetof(struct rte_console, log_le);
Arend van Spriela39be272013-12-12 11:58:58 +01002764 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, (u8 *)&c->log_le,
2765 sizeof(c->log_le));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002766 if (rv < 0)
2767 return rv;
2768
2769 /* Allocate console buffer (one time only) */
2770 if (c->buf == NULL) {
2771 c->bufsize = le32_to_cpu(c->log_le.buf_size);
2772 c->buf = kmalloc(c->bufsize, GFP_ATOMIC);
2773 if (c->buf == NULL)
2774 return -ENOMEM;
2775 }
2776
2777 idx = le32_to_cpu(c->log_le.idx);
2778
2779 /* Protect against corrupt value */
2780 if (idx > c->bufsize)
2781 return -EBADE;
2782
2783 /* Skip reading the console buffer if the index pointer
2784 has not moved */
2785 if (idx == c->last)
2786 return 0;
2787
2788 /* Read the console buffer */
2789 addr = le32_to_cpu(c->log_le.buf);
Arend van Spriela39be272013-12-12 11:58:58 +01002790 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, c->buf, c->bufsize);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002791 if (rv < 0)
2792 return rv;
2793
2794 while (c->last != idx) {
2795 for (n = 0; n < CONSOLE_LINE_MAX - 2; n++) {
2796 if (c->last == idx) {
2797 /* This would output a partial line.
2798 * Instead, back up
2799 * the buffer pointer and output this
2800 * line next time around.
2801 */
2802 if (c->last >= n)
2803 c->last -= n;
2804 else
2805 c->last = c->bufsize - n;
2806 goto break2;
2807 }
2808 ch = c->buf[c->last];
2809 c->last = (c->last + 1) % c->bufsize;
2810 if (ch == '\n')
2811 break;
2812 line[n] = ch;
2813 }
2814
2815 if (n > 0) {
2816 if (line[n - 1] == '\r')
2817 n--;
2818 line[n] = 0;
Joe Perches18aad4f2012-01-15 00:38:42 -08002819 pr_debug("CONSOLE: %s\n", line);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002820 }
2821 }
2822break2:
2823
2824 return 0;
2825}
Joe Perches8ae74652012-01-15 00:38:38 -08002826#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02002827
Franky Linfcf094f2011-12-16 18:37:16 -08002828static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002829brcmf_sdio_bus_txctl(struct device *dev, unsigned char *msg, uint msglen)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002830{
Franky Lin47a1ce72011-11-22 17:21:55 -08002831 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01002832 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Lin47a1ce72011-11-22 17:21:55 -08002833 struct brcmf_sdio *bus = sdiodev->bus;
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002834 int ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002835
2836 brcmf_dbg(TRACE, "Enter\n");
Arend van Spriel5768f312015-06-11 00:12:20 +02002837 if (sdiodev->state != BRCMF_SDIOD_DATA)
2838 return -EIO;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002839
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002840 /* Send from dpc */
2841 bus->ctrl_frame_buf = msg;
2842 bus->ctrl_frame_len = msglen;
Hante Meuleman2c64e162015-03-18 13:25:22 +01002843 wmb();
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002844 bus->ctrl_frame_stat = true;
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002845
Arend van Spriel99824642015-02-06 18:36:45 +01002846 brcmf_sdio_trigger_dpc(bus);
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002847 wait_event_interruptible_timeout(bus->ctrl_wait, !bus->ctrl_frame_stat,
2848 msecs_to_jiffies(CTL_DONE_TIMEOUT));
Hante Meuleman449e58b2015-03-06 18:40:40 +01002849 ret = 0;
2850 if (bus->ctrl_frame_stat) {
2851 sdio_claim_host(bus->sdiodev->func[1]);
2852 if (bus->ctrl_frame_stat) {
2853 brcmf_dbg(SDIO, "ctrl_frame timeout\n");
2854 bus->ctrl_frame_stat = false;
2855 ret = -ETIMEDOUT;
2856 }
2857 sdio_release_host(bus->sdiodev->func[1]);
2858 }
2859 if (!ret) {
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002860 brcmf_dbg(SDIO, "ctrl_frame complete, err=%d\n",
2861 bus->ctrl_frame_err);
Hante Meuleman2c64e162015-03-18 13:25:22 +01002862 rmb();
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002863 ret = bus->ctrl_frame_err;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002864 }
2865
Arend van Spriel5b435de2011-10-05 13:19:03 +02002866 if (ret)
Arend van Spriel80969832012-06-09 22:51:44 +02002867 bus->sdcnt.tx_ctlerrs++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002868 else
Arend van Spriel80969832012-06-09 22:51:44 +02002869 bus->sdcnt.tx_ctlpkts++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002870
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002871 return ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002872}
2873
Arend van Spriel80969832012-06-09 22:51:44 +02002874#ifdef DEBUG
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002875static int brcmf_sdio_dump_console(struct seq_file *seq, struct brcmf_sdio *bus,
2876 struct sdpcm_shared *sh)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002877{
2878 u32 addr, console_ptr, console_size, console_index;
2879 char *conbuf = NULL;
2880 __le32 sh_val;
2881 int rv;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002882
2883 /* obtain console information from device memory */
2884 addr = sh->console_addr + offsetof(struct rte_console, log_le);
Arend van Spriela39be272013-12-12 11:58:58 +01002885 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
2886 (u8 *)&sh_val, sizeof(u32));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002887 if (rv < 0)
2888 return rv;
2889 console_ptr = le32_to_cpu(sh_val);
2890
2891 addr = sh->console_addr + offsetof(struct rte_console, log_le.buf_size);
Arend van Spriela39be272013-12-12 11:58:58 +01002892 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
2893 (u8 *)&sh_val, sizeof(u32));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002894 if (rv < 0)
2895 return rv;
2896 console_size = le32_to_cpu(sh_val);
2897
2898 addr = sh->console_addr + offsetof(struct rte_console, log_le.idx);
Arend van Spriela39be272013-12-12 11:58:58 +01002899 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
2900 (u8 *)&sh_val, sizeof(u32));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002901 if (rv < 0)
2902 return rv;
2903 console_index = le32_to_cpu(sh_val);
2904
2905 /* allocate buffer for console data */
2906 if (console_size <= CONSOLE_BUFFER_MAX)
2907 conbuf = vzalloc(console_size+1);
2908
2909 if (!conbuf)
2910 return -ENOMEM;
2911
2912 /* obtain the console data from device */
2913 conbuf[console_size] = '\0';
Arend van Spriela39be272013-12-12 11:58:58 +01002914 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, console_ptr, (u8 *)conbuf,
2915 console_size);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002916 if (rv < 0)
2917 goto done;
2918
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002919 rv = seq_write(seq, conbuf + console_index,
2920 console_size - console_index);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002921 if (rv < 0)
2922 goto done;
2923
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002924 if (console_index > 0)
2925 rv = seq_write(seq, conbuf, console_index - 1);
2926
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002927done:
2928 vfree(conbuf);
2929 return rv;
2930}
2931
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002932static int brcmf_sdio_trap_info(struct seq_file *seq, struct brcmf_sdio *bus,
2933 struct sdpcm_shared *sh)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002934{
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002935 int error;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002936 struct brcmf_trap_info tr;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002937
Piotr Haberbaa9e602013-04-05 10:57:42 +02002938 if ((sh->flags & SDPCM_SHARED_TRAP) == 0) {
2939 brcmf_dbg(INFO, "no trap in firmware\n");
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002940 return 0;
Piotr Haberbaa9e602013-04-05 10:57:42 +02002941 }
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002942
Arend van Spriela39be272013-12-12 11:58:58 +01002943 error = brcmf_sdiod_ramrw(bus->sdiodev, false, sh->trap_addr, (u8 *)&tr,
2944 sizeof(struct brcmf_trap_info));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002945 if (error < 0)
2946 return error;
2947
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002948 seq_printf(seq,
2949 "dongle trap info: type 0x%x @ epc 0x%08x\n"
2950 " cpsr 0x%08x spsr 0x%08x sp 0x%08x\n"
2951 " lr 0x%08x pc 0x%08x offset 0x%x\n"
2952 " r0 0x%08x r1 0x%08x r2 0x%08x r3 0x%08x\n"
2953 " r4 0x%08x r5 0x%08x r6 0x%08x r7 0x%08x\n",
2954 le32_to_cpu(tr.type), le32_to_cpu(tr.epc),
2955 le32_to_cpu(tr.cpsr), le32_to_cpu(tr.spsr),
2956 le32_to_cpu(tr.r13), le32_to_cpu(tr.r14),
2957 le32_to_cpu(tr.pc), sh->trap_addr,
2958 le32_to_cpu(tr.r0), le32_to_cpu(tr.r1),
2959 le32_to_cpu(tr.r2), le32_to_cpu(tr.r3),
2960 le32_to_cpu(tr.r4), le32_to_cpu(tr.r5),
2961 le32_to_cpu(tr.r6), le32_to_cpu(tr.r7));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002962
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002963 return 0;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002964}
2965
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002966static int brcmf_sdio_assert_info(struct seq_file *seq, struct brcmf_sdio *bus,
2967 struct sdpcm_shared *sh)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002968{
2969 int error = 0;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002970 char file[80] = "?";
2971 char expr[80] = "<???>";
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002972
2973 if ((sh->flags & SDPCM_SHARED_ASSERT_BUILT) == 0) {
2974 brcmf_dbg(INFO, "firmware not built with -assert\n");
2975 return 0;
2976 } else if ((sh->flags & SDPCM_SHARED_ASSERT) == 0) {
2977 brcmf_dbg(INFO, "no assert in dongle\n");
2978 return 0;
2979 }
2980
Franky Lin38b0b0d2012-11-05 16:22:24 -08002981 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002982 if (sh->assert_file_addr != 0) {
Arend van Spriela39be272013-12-12 11:58:58 +01002983 error = brcmf_sdiod_ramrw(bus->sdiodev, false,
2984 sh->assert_file_addr, (u8 *)file, 80);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002985 if (error < 0)
2986 return error;
2987 }
2988 if (sh->assert_exp_addr != 0) {
Arend van Spriela39be272013-12-12 11:58:58 +01002989 error = brcmf_sdiod_ramrw(bus->sdiodev, false,
2990 sh->assert_exp_addr, (u8 *)expr, 80);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002991 if (error < 0)
2992 return error;
2993 }
Franky Lin38b0b0d2012-11-05 16:22:24 -08002994 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002995
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002996 seq_printf(seq, "dongle assert: %s:%d: assert(%s)\n",
2997 file, sh->assert_line, expr);
2998 return 0;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002999}
3000
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003001static int brcmf_sdio_checkdied(struct brcmf_sdio *bus)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003002{
3003 int error;
3004 struct sdpcm_shared sh;
3005
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003006 error = brcmf_sdio_readshared(bus, &sh);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003007
3008 if (error < 0)
3009 return error;
3010
3011 if ((sh.flags & SDPCM_SHARED_ASSERT_BUILT) == 0)
3012 brcmf_dbg(INFO, "firmware not built with -assert\n");
3013 else if (sh.flags & SDPCM_SHARED_ASSERT)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003014 brcmf_err("assertion in dongle\n");
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003015
3016 if (sh.flags & SDPCM_SHARED_TRAP)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003017 brcmf_err("firmware trap in dongle\n");
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003018
3019 return 0;
3020}
3021
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003022static int brcmf_sdio_died_dump(struct seq_file *seq, struct brcmf_sdio *bus)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003023{
3024 int error = 0;
3025 struct sdpcm_shared sh;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003026
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003027 error = brcmf_sdio_readshared(bus, &sh);
3028 if (error < 0)
3029 goto done;
3030
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003031 error = brcmf_sdio_assert_info(seq, bus, &sh);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003032 if (error < 0)
3033 goto done;
Piotr Haberbaa9e602013-04-05 10:57:42 +02003034
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003035 error = brcmf_sdio_trap_info(seq, bus, &sh);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003036 if (error < 0)
3037 goto done;
3038
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003039 error = brcmf_sdio_dump_console(seq, bus, &sh);
Piotr Haberbaa9e602013-04-05 10:57:42 +02003040
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003041done:
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003042 return error;
3043}
3044
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003045static int brcmf_sdio_forensic_read(struct seq_file *seq, void *data)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003046{
Arend van Spriel82d957e2014-07-12 08:49:36 +02003047 struct brcmf_bus *bus_if = dev_get_drvdata(seq->private);
3048 struct brcmf_sdio *bus = bus_if->bus_priv.sdio->bus;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003049
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003050 return brcmf_sdio_died_dump(seq, bus);
3051}
3052
Arend van Spriel82d957e2014-07-12 08:49:36 +02003053static int brcmf_debugfs_sdio_count_read(struct seq_file *seq, void *data)
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003054{
Arend van Spriel82d957e2014-07-12 08:49:36 +02003055 struct brcmf_bus *bus_if = dev_get_drvdata(seq->private);
3056 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3057 struct brcmf_sdio_count *sdcnt = &sdiodev->bus->sdcnt;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003058
Arend van Spriel82d957e2014-07-12 08:49:36 +02003059 seq_printf(seq,
3060 "intrcount: %u\nlastintrs: %u\n"
3061 "pollcnt: %u\nregfails: %u\n"
3062 "tx_sderrs: %u\nfcqueued: %u\n"
3063 "rxrtx: %u\nrx_toolong: %u\n"
3064 "rxc_errors: %u\nrx_hdrfail: %u\n"
3065 "rx_badhdr: %u\nrx_badseq: %u\n"
3066 "fc_rcvd: %u\nfc_xoff: %u\n"
3067 "fc_xon: %u\nrxglomfail: %u\n"
3068 "rxglomframes: %u\nrxglompkts: %u\n"
3069 "f2rxhdrs: %u\nf2rxdata: %u\n"
3070 "f2txdata: %u\nf1regdata: %u\n"
3071 "tickcnt: %u\ntx_ctlerrs: %lu\n"
3072 "tx_ctlpkts: %lu\nrx_ctlerrs: %lu\n"
3073 "rx_ctlpkts: %lu\nrx_readahead: %lu\n",
3074 sdcnt->intrcount, sdcnt->lastintrs,
3075 sdcnt->pollcnt, sdcnt->regfails,
3076 sdcnt->tx_sderrs, sdcnt->fcqueued,
3077 sdcnt->rxrtx, sdcnt->rx_toolong,
3078 sdcnt->rxc_errors, sdcnt->rx_hdrfail,
3079 sdcnt->rx_badhdr, sdcnt->rx_badseq,
3080 sdcnt->fc_rcvd, sdcnt->fc_xoff,
3081 sdcnt->fc_xon, sdcnt->rxglomfail,
3082 sdcnt->rxglomframes, sdcnt->rxglompkts,
3083 sdcnt->f2rxhdrs, sdcnt->f2rxdata,
3084 sdcnt->f2txdata, sdcnt->f1regdata,
3085 sdcnt->tickcnt, sdcnt->tx_ctlerrs,
3086 sdcnt->tx_ctlpkts, sdcnt->rx_ctlerrs,
3087 sdcnt->rx_ctlpkts, sdcnt->rx_readahead_cnt);
3088
3089 return 0;
3090}
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003091
Arend van Spriel80969832012-06-09 22:51:44 +02003092static void brcmf_sdio_debugfs_create(struct brcmf_sdio *bus)
3093{
3094 struct brcmf_pub *drvr = bus->sdiodev->bus_if->drvr;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003095 struct dentry *dentry = brcmf_debugfs_get_devdir(drvr);
Arend van Spriel80969832012-06-09 22:51:44 +02003096
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003097 if (IS_ERR_OR_NULL(dentry))
3098 return;
3099
Arend van Spriel9d6c1dc2015-08-26 22:15:01 +02003100 bus->console_interval = BRCMF_CONSOLE;
3101
Arend van Spriel82d957e2014-07-12 08:49:36 +02003102 brcmf_debugfs_add_entry(drvr, "forensics", brcmf_sdio_forensic_read);
3103 brcmf_debugfs_add_entry(drvr, "counters",
3104 brcmf_debugfs_sdio_count_read);
Daniel Kim0801e6c2014-01-29 15:32:13 +01003105 debugfs_create_u32("console_interval", 0644, dentry,
3106 &bus->console_interval);
Arend van Spriel80969832012-06-09 22:51:44 +02003107}
3108#else
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003109static int brcmf_sdio_checkdied(struct brcmf_sdio *bus)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003110{
3111 return 0;
3112}
3113
Arend van Spriel80969832012-06-09 22:51:44 +02003114static void brcmf_sdio_debugfs_create(struct brcmf_sdio *bus)
3115{
3116}
3117#endif /* DEBUG */
3118
Franky Linfcf094f2011-12-16 18:37:16 -08003119static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003120brcmf_sdio_bus_rxctl(struct device *dev, unsigned char *msg, uint msglen)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003121{
3122 int timeleft;
3123 uint rxlen = 0;
3124 bool pending;
Franky Lindd43a012012-11-05 16:22:22 -08003125 u8 *buf;
Franky Lin532cdd32011-11-22 17:21:54 -08003126 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01003127 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Lin532cdd32011-11-22 17:21:54 -08003128 struct brcmf_sdio *bus = sdiodev->bus;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003129
3130 brcmf_dbg(TRACE, "Enter\n");
Arend van Spriel5768f312015-06-11 00:12:20 +02003131 if (sdiodev->state != BRCMF_SDIOD_DATA)
3132 return -EIO;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003133
3134 /* Wait until control frame is available */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003135 timeleft = brcmf_sdio_dcmd_resp_wait(bus, &bus->rxlen, &pending);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003136
Franky Lindd43a012012-11-05 16:22:22 -08003137 spin_lock_bh(&bus->rxctl_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003138 rxlen = bus->rxlen;
3139 memcpy(msg, bus->rxctl, min(msglen, rxlen));
Franky Lindd43a012012-11-05 16:22:22 -08003140 bus->rxctl = NULL;
3141 buf = bus->rxctl_orig;
3142 bus->rxctl_orig = NULL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003143 bus->rxlen = 0;
Franky Lindd43a012012-11-05 16:22:22 -08003144 spin_unlock_bh(&bus->rxctl_lock);
3145 vfree(buf);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003146
3147 if (rxlen) {
3148 brcmf_dbg(CTL, "resumed on rxctl frame, got %d expected %d\n",
3149 rxlen, msglen);
3150 } else if (timeleft == 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003151 brcmf_err("resumed on timeout\n");
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003152 brcmf_sdio_checkdied(bus);
Joe Perches23677ce2012-02-09 11:17:23 +00003153 } else if (pending) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003154 brcmf_dbg(CTL, "cancelled\n");
3155 return -ERESTARTSYS;
3156 } else {
3157 brcmf_dbg(CTL, "resumed for unknown reason?\n");
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003158 brcmf_sdio_checkdied(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003159 }
3160
3161 if (rxlen)
Arend van Spriel80969832012-06-09 22:51:44 +02003162 bus->sdcnt.rx_ctlpkts++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003163 else
Arend van Spriel80969832012-06-09 22:51:44 +02003164 bus->sdcnt.rx_ctlerrs++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003165
3166 return rxlen ? (int)rxlen : -ETIMEDOUT;
3167}
3168
Hante Meulemana74d0362014-01-13 22:20:22 +01003169#ifdef DEBUG
3170static bool
3171brcmf_sdio_verifymemory(struct brcmf_sdio_dev *sdiodev, u32 ram_addr,
3172 u8 *ram_data, uint ram_sz)
3173{
3174 char *ram_cmp;
3175 int err;
3176 bool ret = true;
3177 int address;
3178 int offset;
3179 int len;
3180
3181 /* read back and verify */
3182 brcmf_dbg(INFO, "Compare RAM dl & ul at 0x%08x; size=%d\n", ram_addr,
3183 ram_sz);
3184 ram_cmp = kmalloc(MEMBLOCK, GFP_KERNEL);
3185 /* do not proceed while no memory but */
3186 if (!ram_cmp)
3187 return true;
3188
3189 address = ram_addr;
3190 offset = 0;
3191 while (offset < ram_sz) {
3192 len = ((offset + MEMBLOCK) < ram_sz) ? MEMBLOCK :
3193 ram_sz - offset;
3194 err = brcmf_sdiod_ramrw(sdiodev, false, address, ram_cmp, len);
3195 if (err) {
3196 brcmf_err("error %d on reading %d membytes at 0x%08x\n",
3197 err, len, address);
3198 ret = false;
3199 break;
3200 } else if (memcmp(ram_cmp, &ram_data[offset], len)) {
3201 brcmf_err("Downloaded RAM image is corrupted, block offset is %d, len is %d\n",
3202 offset, len);
3203 ret = false;
3204 break;
3205 }
3206 offset += len;
3207 address += len;
3208 }
3209
3210 kfree(ram_cmp);
3211
3212 return ret;
3213}
3214#else /* DEBUG */
3215static bool
3216brcmf_sdio_verifymemory(struct brcmf_sdio_dev *sdiodev, u32 ram_addr,
3217 u8 *ram_data, uint ram_sz)
3218{
3219 return true;
3220}
3221#endif /* DEBUG */
3222
Arend van Spriel33556502014-01-13 22:20:25 +01003223static int brcmf_sdio_download_code_file(struct brcmf_sdio *bus,
3224 const struct firmware *fw)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003225{
Hante Meulemanf2c44fe2013-09-25 13:05:45 +02003226 int err;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003227
Hante Meulemana74d0362014-01-13 22:20:22 +01003228 brcmf_dbg(TRACE, "Enter\n");
3229
Hante Meulemanf9951c12014-01-29 15:32:17 +01003230 err = brcmf_sdiod_ramrw(bus->sdiodev, true, bus->ci->rambase,
3231 (u8 *)fw->data, fw->size);
3232 if (err)
3233 brcmf_err("error %d on writing %d membytes at 0x%08x\n",
3234 err, (int)fw->size, bus->ci->rambase);
3235 else if (!brcmf_sdio_verifymemory(bus->sdiodev, bus->ci->rambase,
3236 (u8 *)fw->data, fw->size))
3237 err = -EIO;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003238
Hante Meulemanf2c44fe2013-09-25 13:05:45 +02003239 return err;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003240}
3241
Arend van Spriel33556502014-01-13 22:20:25 +01003242static int brcmf_sdio_download_nvram(struct brcmf_sdio *bus,
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003243 void *vars, u32 varsz)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003244{
Hante Meulemana74d0362014-01-13 22:20:22 +01003245 int address;
3246 int err;
3247
3248 brcmf_dbg(TRACE, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003249
Hante Meulemana74d0362014-01-13 22:20:22 +01003250 address = bus->ci->ramsize - varsz + bus->ci->rambase;
3251 err = brcmf_sdiod_ramrw(bus->sdiodev, true, address, vars, varsz);
3252 if (err)
3253 brcmf_err("error %d on writing %d nvram bytes at 0x%08x\n",
3254 err, varsz, address);
3255 else if (!brcmf_sdio_verifymemory(bus->sdiodev, address, vars, varsz))
3256 err = -EIO;
3257
Hante Meulemana74d0362014-01-13 22:20:22 +01003258 return err;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003259}
3260
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003261static int brcmf_sdio_download_firmware(struct brcmf_sdio *bus,
3262 const struct firmware *fw,
3263 void *nvram, u32 nvlen)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003264{
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003265 int bcmerror = -EFAULT;
Arend van Spriel33556502014-01-13 22:20:25 +01003266 u32 rstvec;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003267
3268 sdio_claim_host(bus->sdiodev->func[1]);
3269 brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003270
Arend van Spriel33556502014-01-13 22:20:25 +01003271 rstvec = get_unaligned_le32(fw->data);
3272 brcmf_dbg(SDIO, "firmware rstvec: %x\n", rstvec);
3273
3274 bcmerror = brcmf_sdio_download_code_file(bus, fw);
3275 release_firmware(fw);
3276 if (bcmerror) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003277 brcmf_err("dongle image file download failed\n");
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003278 brcmf_fw_nvram_free(nvram);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003279 goto err;
3280 }
3281
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003282 bcmerror = brcmf_sdio_download_nvram(bus, nvram, nvlen);
3283 brcmf_fw_nvram_free(nvram);
Arend van Spriel33556502014-01-13 22:20:25 +01003284 if (bcmerror) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003285 brcmf_err("dongle nvram file download failed\n");
Franky Lin3eaa9562013-04-02 21:06:20 +02003286 goto err;
3287 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02003288
3289 /* Take arm out of reset */
Arend van Sprield380ebc2015-03-11 16:11:29 +01003290 if (!brcmf_chip_set_active(bus->ci, rstvec)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003291 brcmf_err("error getting out of ARM core reset\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003292 goto err;
3293 }
3294
Hante Meulemana1cee862015-01-25 20:31:35 +01003295 /* Allow full data communication using DPC from now on. */
Arend van Spriela1ce7a02015-02-06 18:36:42 +01003296 brcmf_sdiod_change_state(bus->sdiodev, BRCMF_SDIOD_DATA);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003297 bcmerror = 0;
3298
3299err:
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003300 brcmf_sdio_clkctl(bus, CLK_SDONLY, false);
3301 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003302 return bcmerror;
3303}
3304
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003305static void brcmf_sdio_sr_init(struct brcmf_sdio *bus)
Piotr Haber4a3da992013-04-11 13:28:46 +02003306{
3307 int err = 0;
3308 u8 val;
3309
3310 brcmf_dbg(TRACE, "Enter\n");
3311
Arend van Spriela39be272013-12-12 11:58:58 +01003312 val = brcmf_sdiod_regrb(bus->sdiodev, SBSDIO_FUNC1_WAKEUPCTRL, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003313 if (err) {
3314 brcmf_err("error reading SBSDIO_FUNC1_WAKEUPCTRL\n");
3315 return;
3316 }
3317
3318 val |= 1 << SBSDIO_FUNC1_WCTRL_HTWAIT_SHIFT;
Arend van Spriela39be272013-12-12 11:58:58 +01003319 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_WAKEUPCTRL, val, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003320 if (err) {
3321 brcmf_err("error writing SBSDIO_FUNC1_WAKEUPCTRL\n");
3322 return;
3323 }
3324
3325 /* Add CMD14 Support */
Arend van Spriela39be272013-12-12 11:58:58 +01003326 brcmf_sdiod_regwb(bus->sdiodev, SDIO_CCCR_BRCM_CARDCAP,
3327 (SDIO_CCCR_BRCM_CARDCAP_CMD14_SUPPORT |
3328 SDIO_CCCR_BRCM_CARDCAP_CMD14_EXT),
3329 &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003330 if (err) {
3331 brcmf_err("error writing SDIO_CCCR_BRCM_CARDCAP\n");
3332 return;
3333 }
3334
Arend van Spriela39be272013-12-12 11:58:58 +01003335 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3336 SBSDIO_FORCE_HT, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003337 if (err) {
3338 brcmf_err("error writing SBSDIO_FUNC1_CHIPCLKCSR\n");
3339 return;
3340 }
3341
3342 /* set flag */
3343 bus->sr_enabled = true;
3344 brcmf_dbg(INFO, "SR enabled\n");
3345}
3346
3347/* enable KSO bit */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003348static int brcmf_sdio_kso_init(struct brcmf_sdio *bus)
Piotr Haber4a3da992013-04-11 13:28:46 +02003349{
3350 u8 val;
3351 int err = 0;
3352
3353 brcmf_dbg(TRACE, "Enter\n");
3354
3355 /* KSO bit added in SDIO core rev 12 */
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003356 if (brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV)->rev < 12)
Piotr Haber4a3da992013-04-11 13:28:46 +02003357 return 0;
3358
Arend van Spriela39be272013-12-12 11:58:58 +01003359 val = brcmf_sdiod_regrb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003360 if (err) {
3361 brcmf_err("error reading SBSDIO_FUNC1_SLEEPCSR\n");
3362 return err;
3363 }
3364
3365 if (!(val & SBSDIO_FUNC1_SLEEPCSR_KSO_MASK)) {
3366 val |= (SBSDIO_FUNC1_SLEEPCSR_KSO_EN <<
3367 SBSDIO_FUNC1_SLEEPCSR_KSO_SHIFT);
Arend van Spriela39be272013-12-12 11:58:58 +01003368 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
3369 val, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003370 if (err) {
3371 brcmf_err("error writing SBSDIO_FUNC1_SLEEPCSR\n");
3372 return err;
3373 }
3374 }
3375
3376 return 0;
3377}
3378
3379
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003380static int brcmf_sdio_bus_preinit(struct device *dev)
Arend van Sprielcf458282013-11-29 11:48:15 +01003381{
3382 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3383 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3384 struct brcmf_sdio *bus = sdiodev->bus;
Franky Lin8da9d2c2013-11-29 12:25:10 +01003385 uint pad_size;
Arend van Sprielcf458282013-11-29 11:48:15 +01003386 u32 value;
Arend van Sprielcf458282013-11-29 11:48:15 +01003387 int err;
3388
Franky Lin8da9d2c2013-11-29 12:25:10 +01003389 /* the commands below use the terms tx and rx from
3390 * a device perspective, ie. bus:txglom affects the
3391 * bus transfers from device to host.
3392 */
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003393 if (brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV)->rev < 12) {
Arend van Sprielcf458282013-11-29 11:48:15 +01003394 /* for sdio core rev < 12, disable txgloming */
3395 value = 0;
3396 err = brcmf_iovar_data_set(dev, "bus:txglom", &value,
3397 sizeof(u32));
3398 } else {
3399 /* otherwise, set txglomalign */
3400 value = 4;
3401 if (sdiodev->pdata)
3402 value = sdiodev->pdata->sd_sgentry_align;
3403 /* SDIO ADMA requires at least 32 bit alignment */
3404 value = max_t(u32, value, 4);
3405 err = brcmf_iovar_data_set(dev, "bus:txglomalign", &value,
3406 sizeof(u32));
3407 }
Franky Lin8da9d2c2013-11-29 12:25:10 +01003408
3409 if (err < 0)
3410 goto done;
3411
3412 bus->tx_hdrlen = SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN;
3413 if (sdiodev->sg_support) {
3414 bus->txglom = false;
3415 value = 1;
3416 pad_size = bus->sdiodev->func[2]->cur_blksize << 1;
Franky Lin8da9d2c2013-11-29 12:25:10 +01003417 err = brcmf_iovar_data_set(bus->sdiodev->dev, "bus:rxglom",
3418 &value, sizeof(u32));
3419 if (err < 0) {
3420 /* bus:rxglom is allowed to fail */
3421 err = 0;
3422 } else {
3423 bus->txglom = true;
3424 bus->tx_hdrlen += SDPCM_HWEXT_LEN;
3425 }
3426 }
3427 brcmf_bus_add_txhdrlen(bus->sdiodev->dev, bus->tx_hdrlen);
3428
3429done:
Arend van Sprielcf458282013-11-29 11:48:15 +01003430 return err;
3431}
3432
Arend van Sprielff4445a2015-10-08 20:33:11 +02003433static size_t brcmf_sdio_bus_get_ramsize(struct device *dev)
3434{
3435 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3436 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3437 struct brcmf_sdio *bus = sdiodev->bus;
3438
3439 return bus->ci->ramsize - bus->ci->srsize;
3440}
3441
3442static int brcmf_sdio_bus_get_memdump(struct device *dev, void *data,
3443 size_t mem_size)
3444{
3445 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3446 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3447 struct brcmf_sdio *bus = sdiodev->bus;
3448 int err;
3449 int address;
3450 int offset;
3451 int len;
3452
3453 brcmf_dbg(INFO, "dump at 0x%08x: size=%zu\n", bus->ci->rambase,
3454 mem_size);
3455
3456 address = bus->ci->rambase;
3457 offset = err = 0;
3458 sdio_claim_host(sdiodev->func[1]);
3459 while (offset < mem_size) {
3460 len = ((offset + MEMBLOCK) < mem_size) ? MEMBLOCK :
3461 mem_size - offset;
3462 err = brcmf_sdiod_ramrw(sdiodev, false, address, data, len);
3463 if (err) {
3464 brcmf_err("error %d on reading %d membytes at 0x%08x\n",
3465 err, len, address);
3466 goto done;
3467 }
3468 data += len;
3469 offset += len;
3470 address += len;
3471 }
3472
3473done:
3474 sdio_release_host(sdiodev->func[1]);
3475 return err;
3476}
3477
Arend van Spriel99824642015-02-06 18:36:45 +01003478void brcmf_sdio_trigger_dpc(struct brcmf_sdio *bus)
3479{
Hante Meuleman2c64e162015-03-18 13:25:22 +01003480 if (!bus->dpc_triggered) {
3481 bus->dpc_triggered = true;
Arend van Spriel99824642015-02-06 18:36:45 +01003482 queue_work(bus->brcmf_wq, &bus->datawork);
3483 }
3484}
3485
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003486void brcmf_sdio_isr(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003487{
Arend van Spriel5b435de2011-10-05 13:19:03 +02003488 brcmf_dbg(TRACE, "Enter\n");
3489
3490 if (!bus) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003491 brcmf_err("bus is null pointer, exiting\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003492 return;
3493 }
3494
Arend van Spriel5b435de2011-10-05 13:19:03 +02003495 /* Count the interrupt call */
Arend van Spriel80969832012-06-09 22:51:44 +02003496 bus->sdcnt.intrcount++;
Franky Lin45316032012-09-13 21:12:03 +02003497 if (in_interrupt())
3498 atomic_set(&bus->ipend, 1);
3499 else
3500 if (brcmf_sdio_intr_rstatus(bus)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003501 brcmf_err("failed backplane access\n");
Franky Lin45316032012-09-13 21:12:03 +02003502 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02003503
Arend van Spriel5b435de2011-10-05 13:19:03 +02003504 /* Disable additional interrupts (is this needed now)? */
3505 if (!bus->intr)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003506 brcmf_err("isr w/o interrupt configured!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003507
Hante Meuleman2c64e162015-03-18 13:25:22 +01003508 bus->dpc_triggered = true;
Franky Linf1e68c22012-09-13 21:12:00 +02003509 queue_work(bus->brcmf_wq, &bus->datawork);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003510}
3511
Hante Meulemanb441ba82015-03-06 18:40:39 +01003512static void brcmf_sdio_bus_watchdog(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003513{
Arend van Spriel5b435de2011-10-05 13:19:03 +02003514 brcmf_dbg(TIMER, "Enter\n");
3515
Arend van Spriel5b435de2011-10-05 13:19:03 +02003516 /* Poll period: check device if appropriate. */
Piotr Haber4a3da992013-04-11 13:28:46 +02003517 if (!bus->sr_enabled &&
3518 bus->poll && (++bus->polltick >= bus->pollrate)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003519 u32 intstatus = 0;
3520
3521 /* Reset poll tick */
3522 bus->polltick = 0;
3523
3524 /* Check device if no interrupts */
Arend van Spriel80969832012-06-09 22:51:44 +02003525 if (!bus->intr ||
3526 (bus->sdcnt.intrcount == bus->sdcnt.lastintrs)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003527
Hante Meuleman2c64e162015-03-18 13:25:22 +01003528 if (!bus->dpc_triggered) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003529 u8 devpend;
Arend van Sprielfccfe932013-06-18 13:29:26 +02003530
Franky Lin38b0b0d2012-11-05 16:22:24 -08003531 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriela39be272013-12-12 11:58:58 +01003532 devpend = brcmf_sdiod_regrb(bus->sdiodev,
3533 SDIO_CCCR_INTx,
3534 NULL);
Franky Lin38b0b0d2012-11-05 16:22:24 -08003535 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel99824642015-02-06 18:36:45 +01003536 intstatus = devpend & (INTR_STATUS_FUNC1 |
3537 INTR_STATUS_FUNC2);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003538 }
3539
3540 /* If there is something, make like the ISR and
3541 schedule the DPC */
3542 if (intstatus) {
Arend van Spriel80969832012-06-09 22:51:44 +02003543 bus->sdcnt.pollcnt++;
Franky Lin1d382272012-09-13 21:11:59 +02003544 atomic_set(&bus->ipend, 1);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003545
Hante Meuleman2c64e162015-03-18 13:25:22 +01003546 bus->dpc_triggered = true;
Franky Linf1e68c22012-09-13 21:12:00 +02003547 queue_work(bus->brcmf_wq, &bus->datawork);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003548 }
3549 }
3550
3551 /* Update interrupt tracking */
Arend van Spriel80969832012-06-09 22:51:44 +02003552 bus->sdcnt.lastintrs = bus->sdcnt.intrcount;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003553 }
Joe Perches8ae74652012-01-15 00:38:38 -08003554#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02003555 /* Poll for console output periodically */
Arend van Spriel9d6c1dc2015-08-26 22:15:01 +02003556 if (bus->sdiodev->state == BRCMF_SDIOD_DATA && BRCMF_FWCON_ON() &&
Franky Lin8d169aa2011-11-22 17:21:52 -08003557 bus->console_interval != 0) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003558 bus->console.count += BRCMF_WD_POLL_MS;
3559 if (bus->console.count >= bus->console_interval) {
3560 bus->console.count -= bus->console_interval;
Franky Lin38b0b0d2012-11-05 16:22:24 -08003561 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003562 /* Make sure backplane clock is on */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003563 brcmf_sdio_bus_sleep(bus, false, false);
3564 if (brcmf_sdio_readconsole(bus) < 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003565 /* stop on error */
3566 bus->console_interval = 0;
Franky Lin38b0b0d2012-11-05 16:22:24 -08003567 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003568 }
3569 }
Joe Perches8ae74652012-01-15 00:38:38 -08003570#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02003571
3572 /* On idle timeout clear activity flag and/or turn off clock */
Hante Meuleman2c64e162015-03-18 13:25:22 +01003573 if (!bus->dpc_triggered) {
3574 rmb();
3575 if ((!bus->dpc_running) && (bus->idletime > 0) &&
3576 (bus->clkstate == CLK_AVAIL)) {
3577 bus->idlecount++;
3578 if (bus->idlecount > bus->idletime) {
3579 brcmf_dbg(SDIO, "idle\n");
3580 sdio_claim_host(bus->sdiodev->func[1]);
3581 brcmf_sdio_wd_timer(bus, 0);
3582 bus->idlecount = 0;
3583 brcmf_sdio_bus_sleep(bus, true, false);
3584 sdio_release_host(bus->sdiodev->func[1]);
3585 }
3586 } else {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003587 bus->idlecount = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003588 }
Hante Meulemanb441ba82015-03-06 18:40:39 +01003589 } else {
3590 bus->idlecount = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003591 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02003592}
3593
Franky Linf1e68c22012-09-13 21:12:00 +02003594static void brcmf_sdio_dataworker(struct work_struct *work)
3595{
3596 struct brcmf_sdio *bus = container_of(work, struct brcmf_sdio,
3597 datawork);
Franky Linf1e68c22012-09-13 21:12:00 +02003598
Hante Meuleman2c64e162015-03-18 13:25:22 +01003599 bus->dpc_running = true;
3600 wmb();
3601 while (ACCESS_ONCE(bus->dpc_triggered)) {
3602 bus->dpc_triggered = false;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003603 brcmf_sdio_dpc(bus);
Hante Meulemanb441ba82015-03-06 18:40:39 +01003604 bus->idlecount = 0;
Franky Linf1e68c22012-09-13 21:12:00 +02003605 }
Hante Meuleman2c64e162015-03-18 13:25:22 +01003606 bus->dpc_running = false;
Arend van Spriel99824642015-02-06 18:36:45 +01003607 if (brcmf_sdiod_freezing(bus->sdiodev)) {
3608 brcmf_sdiod_change_state(bus->sdiodev, BRCMF_SDIOD_DOWN);
3609 brcmf_sdiod_try_freeze(bus->sdiodev);
3610 brcmf_sdiod_change_state(bus->sdiodev, BRCMF_SDIOD_DATA);
3611 }
Franky Linf1e68c22012-09-13 21:12:00 +02003612}
3613
Arend van Spriel65d80d02014-01-29 15:32:14 +01003614static void
3615brcmf_sdio_drivestrengthinit(struct brcmf_sdio_dev *sdiodev,
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003616 struct brcmf_chip *ci, u32 drivestrength)
Arend van Spriel65d80d02014-01-29 15:32:14 +01003617{
3618 const struct sdiod_drive_str *str_tab = NULL;
3619 u32 str_mask;
3620 u32 str_shift;
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003621 u32 base;
Arend van Spriel65d80d02014-01-29 15:32:14 +01003622 u32 i;
3623 u32 drivestrength_sel = 0;
3624 u32 cc_data_temp;
3625 u32 addr;
3626
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003627 if (!(ci->cc_caps & CC_CAP_PMU))
Arend van Spriel65d80d02014-01-29 15:32:14 +01003628 return;
3629
3630 switch (SDIOD_DRVSTR_KEY(ci->chip, ci->pmurev)) {
Hante Meuleman5779ae62014-07-12 08:49:34 +02003631 case SDIOD_DRVSTR_KEY(BRCM_CC_4330_CHIP_ID, 12):
Arend van Spriel65d80d02014-01-29 15:32:14 +01003632 str_tab = sdiod_drvstr_tab1_1v8;
3633 str_mask = 0x00003800;
3634 str_shift = 11;
3635 break;
Hante Meuleman5779ae62014-07-12 08:49:34 +02003636 case SDIOD_DRVSTR_KEY(BRCM_CC_4334_CHIP_ID, 17):
Arend van Spriel65d80d02014-01-29 15:32:14 +01003637 str_tab = sdiod_drvstr_tab6_1v8;
3638 str_mask = 0x00001800;
3639 str_shift = 11;
3640 break;
Hante Meuleman5779ae62014-07-12 08:49:34 +02003641 case SDIOD_DRVSTR_KEY(BRCM_CC_43143_CHIP_ID, 17):
Arend van Spriel65d80d02014-01-29 15:32:14 +01003642 /* note: 43143 does not support tristate */
3643 i = ARRAY_SIZE(sdiod_drvstr_tab2_3v3) - 1;
3644 if (drivestrength >= sdiod_drvstr_tab2_3v3[i].strength) {
3645 str_tab = sdiod_drvstr_tab2_3v3;
3646 str_mask = 0x00000007;
3647 str_shift = 0;
3648 } else
3649 brcmf_err("Invalid SDIO Drive strength for chip %s, strength=%d\n",
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003650 ci->name, drivestrength);
Arend van Spriel65d80d02014-01-29 15:32:14 +01003651 break;
Hante Meuleman5779ae62014-07-12 08:49:34 +02003652 case SDIOD_DRVSTR_KEY(BRCM_CC_43362_CHIP_ID, 13):
Arend van Spriel65d80d02014-01-29 15:32:14 +01003653 str_tab = sdiod_drive_strength_tab5_1v8;
3654 str_mask = 0x00003800;
3655 str_shift = 11;
3656 break;
3657 default:
3658 brcmf_err("No SDIO Drive strength init done for chip %s rev %d pmurev %d\n",
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003659 ci->name, ci->chiprev, ci->pmurev);
Arend van Spriel65d80d02014-01-29 15:32:14 +01003660 break;
3661 }
3662
3663 if (str_tab != NULL) {
3664 for (i = 0; str_tab[i].strength != 0; i++) {
3665 if (drivestrength >= str_tab[i].strength) {
3666 drivestrength_sel = str_tab[i].sel;
3667 break;
3668 }
3669 }
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003670 base = brcmf_chip_get_chipcommon(ci)->base;
Arend van Spriel65d80d02014-01-29 15:32:14 +01003671 addr = CORE_CC_REG(base, chipcontrol_addr);
3672 brcmf_sdiod_regwl(sdiodev, addr, 1, NULL);
3673 cc_data_temp = brcmf_sdiod_regrl(sdiodev, addr, NULL);
3674 cc_data_temp &= ~str_mask;
3675 drivestrength_sel <<= str_shift;
3676 cc_data_temp |= drivestrength_sel;
3677 brcmf_sdiod_regwl(sdiodev, addr, cc_data_temp, NULL);
3678
3679 brcmf_dbg(INFO, "SDIO: %d mA (req=%d mA) drive strength selected, set to 0x%08x\n",
3680 str_tab[i].strength, drivestrength, cc_data_temp);
3681 }
3682}
3683
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003684static int brcmf_sdio_buscoreprep(void *ctx)
Arend van Spriel65d80d02014-01-29 15:32:14 +01003685{
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003686 struct brcmf_sdio_dev *sdiodev = ctx;
Arend van Spriel65d80d02014-01-29 15:32:14 +01003687 int err = 0;
3688 u8 clkval, clkset;
3689
3690 /* Try forcing SDIO core to do ALPAvail request only */
3691 clkset = SBSDIO_FORCE_HW_CLKREQ_OFF | SBSDIO_ALP_AVAIL_REQ;
3692 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, clkset, &err);
3693 if (err) {
3694 brcmf_err("error writing for HT off\n");
3695 return err;
3696 }
3697
3698 /* If register supported, wait for ALPAvail and then force ALP */
3699 /* This may take up to 15 milliseconds */
3700 clkval = brcmf_sdiod_regrb(sdiodev,
3701 SBSDIO_FUNC1_CHIPCLKCSR, NULL);
3702
3703 if ((clkval & ~SBSDIO_AVBITS) != clkset) {
3704 brcmf_err("ChipClkCSR access: wrote 0x%02x read 0x%02x\n",
3705 clkset, clkval);
3706 return -EACCES;
3707 }
3708
3709 SPINWAIT(((clkval = brcmf_sdiod_regrb(sdiodev,
3710 SBSDIO_FUNC1_CHIPCLKCSR, NULL)),
3711 !SBSDIO_ALPAV(clkval)),
3712 PMU_MAX_TRANSITION_DLY);
3713 if (!SBSDIO_ALPAV(clkval)) {
3714 brcmf_err("timeout on ALPAV wait, clkval 0x%02x\n",
3715 clkval);
3716 return -EBUSY;
3717 }
3718
3719 clkset = SBSDIO_FORCE_HW_CLKREQ_OFF | SBSDIO_FORCE_ALP;
3720 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, clkset, &err);
3721 udelay(65);
3722
3723 /* Also, disable the extra SDIO pull-ups */
3724 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_SDIOPULLUP, 0, NULL);
3725
3726 return 0;
3727}
3728
Arend van Sprield380ebc2015-03-11 16:11:29 +01003729static void brcmf_sdio_buscore_activate(void *ctx, struct brcmf_chip *chip,
3730 u32 rstvec)
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003731{
3732 struct brcmf_sdio_dev *sdiodev = ctx;
3733 struct brcmf_core *core;
3734 u32 reg_addr;
3735
3736 /* clear all interrupts */
3737 core = brcmf_chip_get_core(chip, BCMA_CORE_SDIO_DEV);
3738 reg_addr = core->base + offsetof(struct sdpcmd_regs, intstatus);
3739 brcmf_sdiod_regwl(sdiodev, reg_addr, 0xFFFFFFFF, NULL);
3740
3741 if (rstvec)
3742 /* Write reset vector to address 0 */
3743 brcmf_sdiod_ramrw(sdiodev, true, 0, (void *)&rstvec,
3744 sizeof(rstvec));
3745}
3746
3747static u32 brcmf_sdio_buscore_read32(void *ctx, u32 addr)
3748{
3749 struct brcmf_sdio_dev *sdiodev = ctx;
3750 u32 val, rev;
3751
3752 val = brcmf_sdiod_regrl(sdiodev, addr, NULL);
Arend van Spriel8bd61f82015-01-06 23:02:54 +01003753 if (sdiodev->func[0]->device == SDIO_DEVICE_ID_BROADCOM_4335_4339 &&
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003754 addr == CORE_CC_REG(SI_ENUM_BASE, chipid)) {
3755 rev = (val & CID_REV_MASK) >> CID_REV_SHIFT;
3756 if (rev >= 2) {
3757 val &= ~CID_ID_MASK;
Hante Meuleman5779ae62014-07-12 08:49:34 +02003758 val |= BRCM_CC_4339_CHIP_ID;
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003759 }
3760 }
3761 return val;
3762}
3763
3764static void brcmf_sdio_buscore_write32(void *ctx, u32 addr, u32 val)
3765{
3766 struct brcmf_sdio_dev *sdiodev = ctx;
3767
3768 brcmf_sdiod_regwl(sdiodev, addr, val, NULL);
3769}
3770
3771static const struct brcmf_buscore_ops brcmf_sdio_buscore_ops = {
3772 .prepare = brcmf_sdio_buscoreprep,
Arend van Sprield380ebc2015-03-11 16:11:29 +01003773 .activate = brcmf_sdio_buscore_activate,
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003774 .read32 = brcmf_sdio_buscore_read32,
3775 .write32 = brcmf_sdio_buscore_write32,
3776};
3777
Arend van Spriel5b435de2011-10-05 13:19:03 +02003778static bool
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003779brcmf_sdio_probe_attach(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003780{
3781 u8 clkctl = 0;
3782 int err = 0;
3783 int reg_addr;
3784 u32 reg_val;
Hante Meuleman668761a2013-04-12 10:55:55 +02003785 u32 drivestrength;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003786
Franky Lin38b0b0d2012-11-05 16:22:24 -08003787 sdio_claim_host(bus->sdiodev->func[1]);
3788
Joe Perches18aad4f2012-01-15 00:38:42 -08003789 pr_debug("F1 signature read @0x18000000=0x%4x\n",
Arend van Spriela39be272013-12-12 11:58:58 +01003790 brcmf_sdiod_regrl(bus->sdiodev, SI_ENUM_BASE, NULL));
Arend van Spriel5b435de2011-10-05 13:19:03 +02003791
3792 /*
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003793 * Force PLL off until brcmf_chip_attach()
Arend van Spriel5b435de2011-10-05 13:19:03 +02003794 * programs PLL control regs
3795 */
3796
Arend van Spriela39be272013-12-12 11:58:58 +01003797 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3798 BRCMF_INIT_CLKCTL1, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003799 if (!err)
Arend van Spriela39be272013-12-12 11:58:58 +01003800 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
3801 SBSDIO_FUNC1_CHIPCLKCSR, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003802
3803 if (err || ((clkctl & ~SBSDIO_AVBITS) != BRCMF_INIT_CLKCTL1)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003804 brcmf_err("ChipClkCSR access: err %d wrote 0x%02x read 0x%02x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02003805 err, BRCMF_INIT_CLKCTL1, clkctl);
3806 goto fail;
3807 }
3808
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003809 bus->ci = brcmf_chip_attach(bus->sdiodev, &brcmf_sdio_buscore_ops);
3810 if (IS_ERR(bus->ci)) {
3811 brcmf_err("brcmf_chip_attach failed!\n");
3812 bus->ci = NULL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003813 goto fail;
3814 }
3815
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003816 if (brcmf_sdio_kso_init(bus)) {
Piotr Haber4a3da992013-04-11 13:28:46 +02003817 brcmf_err("error enabling KSO\n");
3818 goto fail;
3819 }
3820
Hante Meuleman668761a2013-04-12 10:55:55 +02003821 if ((bus->sdiodev->pdata) && (bus->sdiodev->pdata->drive_strength))
3822 drivestrength = bus->sdiodev->pdata->drive_strength;
3823 else
3824 drivestrength = DEFAULT_SDIO_DRIVE_STRENGTH;
Arend van Spriel65d80d02014-01-29 15:32:14 +01003825 brcmf_sdio_drivestrengthinit(bus->sdiodev, bus->ci, drivestrength);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003826
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003827 /* Set card control so an SDIO card reset does a WLAN backplane reset */
Arend van Spriela39be272013-12-12 11:58:58 +01003828 reg_val = brcmf_sdiod_regrb(bus->sdiodev,
3829 SDIO_CCCR_BRCM_CARDCTRL, &err);
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003830 if (err)
3831 goto fail;
3832
3833 reg_val |= SDIO_CCCR_BRCM_CARDCTRL_WLANRESET;
3834
Arend van Spriela39be272013-12-12 11:58:58 +01003835 brcmf_sdiod_regwb(bus->sdiodev,
3836 SDIO_CCCR_BRCM_CARDCTRL, reg_val, &err);
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003837 if (err)
3838 goto fail;
3839
3840 /* set PMUControl so a backplane reset does PMU state reload */
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003841 reg_addr = CORE_CC_REG(brcmf_chip_get_chipcommon(bus->ci)->base,
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003842 pmucontrol);
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003843 reg_val = brcmf_sdiod_regrl(bus->sdiodev, reg_addr, &err);
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003844 if (err)
3845 goto fail;
3846
3847 reg_val |= (BCMA_CC_PMU_CTL_RES_RELOAD << BCMA_CC_PMU_CTL_RES_SHIFT);
3848
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003849 brcmf_sdiod_regwl(bus->sdiodev, reg_addr, reg_val, &err);
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003850 if (err)
3851 goto fail;
3852
Franky Lin38b0b0d2012-11-05 16:22:24 -08003853 sdio_release_host(bus->sdiodev->func[1]);
3854
Arend van Spriel5b435de2011-10-05 13:19:03 +02003855 brcmu_pktq_init(&bus->txq, (PRIOMASK + 1), TXQLEN);
3856
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01003857 /* allocate header buffer */
3858 bus->hdrbuf = kzalloc(MAX_HDR_READ + bus->head_align, GFP_KERNEL);
3859 if (!bus->hdrbuf)
3860 return false;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003861 /* Locate an appropriately-aligned portion of hdrbuf */
3862 bus->rxhdr = (u8 *) roundup((unsigned long)&bus->hdrbuf[0],
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01003863 bus->head_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003864
3865 /* Set the poll and/or interrupt flags */
3866 bus->intr = true;
3867 bus->poll = false;
3868 if (bus->poll)
3869 bus->pollrate = 1;
3870
3871 return true;
3872
3873fail:
Franky Lin38b0b0d2012-11-05 16:22:24 -08003874 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003875 return false;
3876}
3877
Arend van Spriel5b435de2011-10-05 13:19:03 +02003878static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003879brcmf_sdio_watchdog_thread(void *data)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003880{
Franky Line92eedf2011-11-22 17:21:50 -08003881 struct brcmf_sdio *bus = (struct brcmf_sdio *)data;
Arend van Spriel99824642015-02-06 18:36:45 +01003882 int wait;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003883
3884 allow_signal(SIGTERM);
3885 /* Run until signal received */
Arend van Spriel99824642015-02-06 18:36:45 +01003886 brcmf_sdiod_freezer_count(bus->sdiodev);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003887 while (1) {
3888 if (kthread_should_stop())
3889 break;
Arend van Spriel99824642015-02-06 18:36:45 +01003890 brcmf_sdiod_freezer_uncount(bus->sdiodev);
3891 wait = wait_for_completion_interruptible(&bus->watchdog_wait);
3892 brcmf_sdiod_freezer_count(bus->sdiodev);
3893 brcmf_sdiod_try_freeze(bus->sdiodev);
3894 if (!wait) {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003895 brcmf_sdio_bus_watchdog(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003896 /* Count the tick for reference */
Arend van Spriel80969832012-06-09 22:51:44 +02003897 bus->sdcnt.tickcnt++;
Arend van Spriel58e9df42014-03-20 10:18:01 +01003898 reinit_completion(&bus->watchdog_wait);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003899 } else
3900 break;
3901 }
3902 return 0;
3903}
3904
3905static void
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003906brcmf_sdio_watchdog(unsigned long data)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003907{
Franky Line92eedf2011-11-22 17:21:50 -08003908 struct brcmf_sdio *bus = (struct brcmf_sdio *)data;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003909
3910 if (bus->watchdog_tsk) {
3911 complete(&bus->watchdog_wait);
3912 /* Reschedule the watchdog */
3913 if (bus->wd_timer_valid)
3914 mod_timer(&bus->timer,
Nicholas Mc Guire187d3c32015-02-06 05:26:45 -05003915 jiffies + msecs_to_jiffies(BRCMF_WD_POLL_MS));
Arend van Spriel5b435de2011-10-05 13:19:03 +02003916 }
3917}
3918
Julia Lawall6866a642015-11-14 17:22:07 +01003919static const struct brcmf_bus_ops brcmf_sdio_bus_ops = {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003920 .stop = brcmf_sdio_bus_stop,
3921 .preinit = brcmf_sdio_bus_preinit,
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003922 .txdata = brcmf_sdio_bus_txdata,
3923 .txctl = brcmf_sdio_bus_txctl,
3924 .rxctl = brcmf_sdio_bus_rxctl,
3925 .gettxq = brcmf_sdio_bus_gettxq,
Arend van Sprielff4445a2015-10-08 20:33:11 +02003926 .wowl_config = brcmf_sdio_wowl_config,
3927 .get_ramsize = brcmf_sdio_bus_get_ramsize,
3928 .get_memdump = brcmf_sdio_bus_get_memdump,
Arend van Sprield9cb2592012-12-05 15:25:54 +01003929};
3930
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003931static void brcmf_sdio_firmware_callback(struct device *dev,
3932 const struct firmware *code,
3933 void *nvram, u32 nvram_len)
3934{
3935 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3936 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3937 struct brcmf_sdio *bus = sdiodev->bus;
3938 int err = 0;
3939 u8 saveclk;
3940
3941 brcmf_dbg(TRACE, "Enter: dev=%s\n", dev_name(dev));
3942
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003943 if (!bus_if->drvr)
3944 return;
3945
Hante Meulemana1cee862015-01-25 20:31:35 +01003946 /* try to download image and nvram to the dongle */
3947 bus->alp_only = true;
3948 err = brcmf_sdio_download_firmware(bus, code, nvram, nvram_len);
3949 if (err)
3950 goto fail;
3951 bus->alp_only = false;
3952
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003953 /* Start the watchdog timer */
3954 bus->sdcnt.tickcnt = 0;
3955 brcmf_sdio_wd_timer(bus, BRCMF_WD_POLL_MS);
3956
3957 sdio_claim_host(sdiodev->func[1]);
3958
3959 /* Make sure backplane clock is on, needed to generate F2 interrupt */
3960 brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
3961 if (bus->clkstate != CLK_AVAIL)
3962 goto release;
3963
3964 /* Force clocks on backplane to be sure F2 interrupt propagates */
3965 saveclk = brcmf_sdiod_regrb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, &err);
3966 if (!err) {
3967 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3968 (saveclk | SBSDIO_FORCE_HT), &err);
3969 }
3970 if (err) {
3971 brcmf_err("Failed to force clock for F2: err %d\n", err);
3972 goto release;
3973 }
3974
3975 /* Enable function 2 (frame transfers) */
3976 w_sdreg32(bus, SDPCM_PROT_VERSION << SMB_DATA_VERSION_SHIFT,
3977 offsetof(struct sdpcmd_regs, tosbmailboxdata));
3978 err = sdio_enable_func(sdiodev->func[SDIO_FUNC_2]);
3979
3980
3981 brcmf_dbg(INFO, "enable F2: err=%d\n", err);
3982
3983 /* If F2 successfully enabled, set core and enable interrupts */
3984 if (!err) {
3985 /* Set up the interrupt mask and enable interrupts */
3986 bus->hostintmask = HOSTINTMASK;
3987 w_sdreg32(bus, bus->hostintmask,
3988 offsetof(struct sdpcmd_regs, hostintmask));
3989
3990 brcmf_sdiod_regwb(sdiodev, SBSDIO_WATERMARK, 8, &err);
3991 } else {
3992 /* Disable F2 again */
3993 sdio_disable_func(sdiodev->func[SDIO_FUNC_2]);
3994 goto release;
3995 }
3996
3997 if (brcmf_chip_sr_capable(bus->ci)) {
3998 brcmf_sdio_sr_init(bus);
3999 } else {
4000 /* Restore previous clock setting */
4001 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
4002 saveclk, &err);
4003 }
4004
4005 if (err == 0) {
4006 err = brcmf_sdiod_intr_register(sdiodev);
4007 if (err != 0)
4008 brcmf_err("intr register failed:%d\n", err);
4009 }
4010
4011 /* If we didn't come up, turn off backplane clock */
4012 if (err != 0)
4013 brcmf_sdio_clkctl(bus, CLK_NONE, false);
4014
4015 sdio_release_host(sdiodev->func[1]);
4016
4017 err = brcmf_bus_start(dev);
4018 if (err != 0) {
4019 brcmf_err("dongle is not responding\n");
4020 goto fail;
4021 }
4022 return;
4023
4024release:
4025 sdio_release_host(sdiodev->func[1]);
4026fail:
4027 brcmf_dbg(TRACE, "failed: dev=%s, err=%d\n", dev_name(dev), err);
4028 device_release_driver(dev);
4029}
4030
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01004031struct brcmf_sdio *brcmf_sdio_probe(struct brcmf_sdio_dev *sdiodev)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004032{
4033 int ret;
Franky Line92eedf2011-11-22 17:21:50 -08004034 struct brcmf_sdio *bus;
Arend van Spriel99824642015-02-06 18:36:45 +01004035 struct workqueue_struct *wq;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004036
Arend van Spriel5b435de2011-10-05 13:19:03 +02004037 brcmf_dbg(TRACE, "Enter\n");
4038
Arend van Spriel5b435de2011-10-05 13:19:03 +02004039 /* Allocate private bus interface state */
Franky Line92eedf2011-11-22 17:21:50 -08004040 bus = kzalloc(sizeof(struct brcmf_sdio), GFP_ATOMIC);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004041 if (!bus)
4042 goto fail;
4043
4044 bus->sdiodev = sdiodev;
4045 sdiodev->bus = bus;
Arend van Sprielb83db862011-10-19 12:51:09 +02004046 skb_queue_head_init(&bus->glom);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004047 bus->txbound = BRCMF_TXBOUND;
4048 bus->rxbound = BRCMF_RXBOUND;
4049 bus->txminmax = BRCMF_TXMINMAX;
Franky Lin6bc52312013-08-10 12:27:28 +02004050 bus->tx_seq = SDPCM_SEQ_WRAP - 1;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004051
Arend van Spriele217d1c2013-11-29 12:25:13 +01004052 /* platform specific configuration:
4053 * alignments must be at least 4 bytes for ADMA
Hante Meuleman888bf76e2014-10-28 14:56:17 +01004054 */
Arend van Spriele217d1c2013-11-29 12:25:13 +01004055 bus->head_align = ALIGNMENT;
4056 bus->sgentry_align = ALIGNMENT;
4057 if (sdiodev->pdata) {
4058 if (sdiodev->pdata->sd_head_align > ALIGNMENT)
4059 bus->head_align = sdiodev->pdata->sd_head_align;
4060 if (sdiodev->pdata->sd_sgentry_align > ALIGNMENT)
4061 bus->sgentry_align = sdiodev->pdata->sd_sgentry_align;
4062 }
4063
Arend van Spriel99824642015-02-06 18:36:45 +01004064 /* single-threaded workqueue */
4065 wq = alloc_ordered_workqueue("brcmf_wq/%s", WQ_MEM_RECLAIM,
4066 dev_name(&sdiodev->func[1]->dev));
4067 if (!wq) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01004068 brcmf_err("insufficient memory to create txworkqueue\n");
Hante Meuleman37ac5782012-11-14 18:46:18 -08004069 goto fail;
4070 }
Arend van Spriel99824642015-02-06 18:36:45 +01004071 brcmf_sdiod_freezer_count(sdiodev);
4072 INIT_WORK(&bus->datawork, brcmf_sdio_dataworker);
4073 bus->brcmf_wq = wq;
Hante Meuleman37ac5782012-11-14 18:46:18 -08004074
Arend van Spriel5b435de2011-10-05 13:19:03 +02004075 /* attempt to attach to the dongle */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01004076 if (!(brcmf_sdio_probe_attach(bus))) {
4077 brcmf_err("brcmf_sdio_probe_attach failed\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02004078 goto fail;
4079 }
4080
Franky Lindd43a012012-11-05 16:22:22 -08004081 spin_lock_init(&bus->rxctl_lock);
Hante Meulemanfed7ec42014-03-15 17:18:18 +01004082 spin_lock_init(&bus->txq_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004083 init_waitqueue_head(&bus->ctrl_wait);
4084 init_waitqueue_head(&bus->dcmd_resp_wait);
4085
4086 /* Set up the watchdog timer */
4087 init_timer(&bus->timer);
4088 bus->timer.data = (unsigned long)bus;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01004089 bus->timer.function = brcmf_sdio_watchdog;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004090
Arend van Spriel5b435de2011-10-05 13:19:03 +02004091 /* Initialize watchdog thread */
4092 init_completion(&bus->watchdog_wait);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01004093 bus->watchdog_tsk = kthread_run(brcmf_sdio_watchdog_thread,
Arend van Spriel99824642015-02-06 18:36:45 +01004094 bus, "brcmf_wdog/%s",
4095 dev_name(&sdiodev->func[1]->dev));
Arend van Spriel5b435de2011-10-05 13:19:03 +02004096 if (IS_ERR(bus->watchdog_tsk)) {
Joe Perches02f77192012-01-15 00:38:44 -08004097 pr_warn("brcmf_watchdog thread failed to start\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02004098 bus->watchdog_tsk = NULL;
4099 }
4100 /* Initialize DPC thread */
Hante Meuleman2c64e162015-03-18 13:25:22 +01004101 bus->dpc_triggered = false;
4102 bus->dpc_running = false;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004103
Franky Lina9ffda82011-12-16 18:37:12 -08004104 /* Assign bus interface call back */
Arend van Sprield9cb2592012-12-05 15:25:54 +01004105 bus->sdiodev->bus_if->dev = bus->sdiodev->dev;
4106 bus->sdiodev->bus_if->ops = &brcmf_sdio_bus_ops;
Arend van Spriel75d907d2013-02-06 18:40:45 +01004107 bus->sdiodev->bus_if->chip = bus->ci->chip;
4108 bus->sdiodev->bus_if->chiprev = bus->ci->chiprev;
Arend van Sprield9cb2592012-12-05 15:25:54 +01004109
Franky Lin706478c2013-08-10 12:27:29 +02004110 /* default sdio bus header length for tx packet */
4111 bus->tx_hdrlen = SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN;
4112
4113 /* Attach to the common layer, reserve hdr space */
Arend van Spriel8dee77b2013-11-29 11:48:14 +01004114 ret = brcmf_attach(bus->sdiodev->dev);
Franky Lin712ac5b2011-12-16 18:37:09 -08004115 if (ret != 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01004116 brcmf_err("brcmf_attach failed\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02004117 goto fail;
4118 }
4119
Daniel Kim7dd3abc2014-05-27 12:56:26 +02004120 /* Query the F2 block size, set roundup accordingly */
4121 bus->blocksize = bus->sdiodev->func[2]->cur_blksize;
4122 bus->roundup = min(max_roundup, bus->blocksize);
4123
Arend van Spriel5b435de2011-10-05 13:19:03 +02004124 /* Allocate buffers */
Arend van Sprielfad13222014-01-06 12:40:40 +01004125 if (bus->sdiodev->bus_if->maxctl) {
Daniel Kim7dd3abc2014-05-27 12:56:26 +02004126 bus->sdiodev->bus_if->maxctl += bus->roundup;
Arend van Sprielfad13222014-01-06 12:40:40 +01004127 bus->rxblen =
4128 roundup((bus->sdiodev->bus_if->maxctl + SDPCM_HDRLEN),
4129 ALIGNMENT) + bus->head_align;
4130 bus->rxbuf = kmalloc(bus->rxblen, GFP_ATOMIC);
4131 if (!(bus->rxbuf)) {
4132 brcmf_err("rxbuf allocation failed\n");
4133 goto fail;
4134 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02004135 }
4136
Arend van Sprielfad13222014-01-06 12:40:40 +01004137 sdio_claim_host(bus->sdiodev->func[1]);
4138
4139 /* Disable F2 to clear any intermediate frame state on the dongle */
4140 sdio_disable_func(bus->sdiodev->func[SDIO_FUNC_2]);
4141
Arend van Sprielfad13222014-01-06 12:40:40 +01004142 bus->rxflow = false;
4143
4144 /* Done with backplane-dependent accesses, can drop clock... */
4145 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, 0, NULL);
4146
4147 sdio_release_host(bus->sdiodev->func[1]);
4148
4149 /* ...and initialize clock/power states */
4150 bus->clkstate = CLK_SDONLY;
4151 bus->idletime = BRCMF_IDLE_INTERVAL;
4152 bus->idleclock = BRCMF_IDLE_ACTIVE;
4153
Arend van Sprielfad13222014-01-06 12:40:40 +01004154 /* SR state */
Arend van Sprielfad13222014-01-06 12:40:40 +01004155 bus->sr_enabled = false;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004156
Arend van Spriel80969832012-06-09 22:51:44 +02004157 brcmf_sdio_debugfs_create(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004158 brcmf_dbg(INFO, "completed!!\n");
4159
Hante Meuleman46d703a2015-11-25 11:32:46 +01004160 ret = brcmf_fw_map_chip_to_name(bus->ci->chip, bus->ci->chiprev,
4161 brcmf_sdio_fwnames,
4162 ARRAY_SIZE(brcmf_sdio_fwnames),
4163 sdiodev->fw_name, sdiodev->nvram_name);
Daniel Kimc1b20532014-07-12 08:49:37 +02004164 if (ret)
4165 goto fail;
4166
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02004167 ret = brcmf_fw_get_firmwares(sdiodev->dev, BRCMF_FW_REQUEST_NVRAM,
Daniel Kimc1b20532014-07-12 08:49:37 +02004168 sdiodev->fw_name, sdiodev->nvram_name,
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02004169 brcmf_sdio_firmware_callback);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004170 if (ret != 0) {
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02004171 brcmf_err("async firmware request failed: %d\n", ret);
Hante Meuleman1799ddf2012-11-14 18:46:17 -08004172 goto fail;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004173 }
Franky Lin15d45b62011-10-21 16:16:32 +02004174
Arend van Spriel5b435de2011-10-05 13:19:03 +02004175 return bus;
4176
4177fail:
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004178 brcmf_sdio_remove(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004179 return NULL;
4180}
4181
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004182/* Detach and free everything */
4183void brcmf_sdio_remove(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004184{
Arend van Spriel5b435de2011-10-05 13:19:03 +02004185 brcmf_dbg(TRACE, "Enter\n");
4186
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004187 if (bus) {
4188 /* De-register interrupt handler */
4189 brcmf_sdiod_intr_unregister(bus->sdiodev);
4190
Arend van Spriel4faf28b2014-05-27 12:56:18 +02004191 brcmf_detach(bus->sdiodev->dev);
Arend van Sprielbfad4a02014-01-06 12:40:39 +01004192
Hante Meulemane0c180ec2014-01-29 15:32:18 +01004193 cancel_work_sync(&bus->datawork);
4194 if (bus->brcmf_wq)
4195 destroy_workqueue(bus->brcmf_wq);
4196
Arend van Sprielbfad4a02014-01-06 12:40:39 +01004197 if (bus->ci) {
Arend van Spriela1ce7a02015-02-06 18:36:42 +01004198 if (bus->sdiodev->state != BRCMF_SDIOD_NOMEDIUM) {
Arend van Sprielbb350712014-01-13 22:20:29 +01004199 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Sprielcf459322015-03-11 16:11:33 +01004200 brcmf_sdio_wd_timer(bus, 0);
Arend van Sprielbb350712014-01-13 22:20:29 +01004201 brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
4202 /* Leave the device in state where it is
Arend van Sprield380ebc2015-03-11 16:11:29 +01004203 * 'passive'. This is done by resetting all
4204 * necessary cores.
Arend van Sprielbb350712014-01-13 22:20:29 +01004205 */
4206 msleep(20);
Arend van Sprield380ebc2015-03-11 16:11:29 +01004207 brcmf_chip_set_passive(bus->ci);
Arend van Sprielbb350712014-01-13 22:20:29 +01004208 brcmf_sdio_clkctl(bus, CLK_NONE, false);
4209 sdio_release_host(bus->sdiodev->func[1]);
4210 }
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01004211 brcmf_chip_detach(bus->ci);
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004212 }
4213
Arend van Sprielbfad4a02014-01-06 12:40:39 +01004214 kfree(bus->rxbuf);
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004215 kfree(bus->hdrbuf);
4216 kfree(bus);
4217 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02004218
4219 brcmf_dbg(TRACE, "Disconnected\n");
4220}
4221
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01004222void brcmf_sdio_wd_timer(struct brcmf_sdio *bus, uint wdtick)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004223{
Arend van Spriel5b435de2011-10-05 13:19:03 +02004224 /* Totally stop the timer */
Joe Perches23677ce2012-02-09 11:17:23 +00004225 if (!wdtick && bus->wd_timer_valid) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02004226 del_timer_sync(&bus->timer);
4227 bus->wd_timer_valid = false;
4228 bus->save_ms = wdtick;
4229 return;
4230 }
4231
Franky Linece960e2011-10-21 16:16:19 +02004232 /* don't start the wd until fw is loaded */
Arend van Spriela1ce7a02015-02-06 18:36:42 +01004233 if (bus->sdiodev->state != BRCMF_SDIOD_DATA)
Franky Linece960e2011-10-21 16:16:19 +02004234 return;
4235
Arend van Spriel5b435de2011-10-05 13:19:03 +02004236 if (wdtick) {
4237 if (bus->save_ms != BRCMF_WD_POLL_MS) {
Joe Perches23677ce2012-02-09 11:17:23 +00004238 if (bus->wd_timer_valid)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004239 /* Stop timer and restart at new value */
4240 del_timer_sync(&bus->timer);
4241
4242 /* Create timer again when watchdog period is
4243 dynamically changed or in the first instance
4244 */
4245 bus->timer.expires =
Nicholas Mc Guire187d3c32015-02-06 05:26:45 -05004246 jiffies + msecs_to_jiffies(BRCMF_WD_POLL_MS);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004247 add_timer(&bus->timer);
4248
4249 } else {
4250 /* Re arm the timer, at last watchdog period */
4251 mod_timer(&bus->timer,
Nicholas Mc Guire187d3c32015-02-06 05:26:45 -05004252 jiffies + msecs_to_jiffies(BRCMF_WD_POLL_MS));
Arend van Spriel5b435de2011-10-05 13:19:03 +02004253 }
4254
4255 bus->wd_timer_valid = true;
4256 bus->save_ms = wdtick;
4257 }
4258}
Arend van Spriel99824642015-02-06 18:36:45 +01004259
4260int brcmf_sdio_sleep(struct brcmf_sdio *bus, bool sleep)
4261{
4262 int ret;
4263
4264 sdio_claim_host(bus->sdiodev->func[1]);
4265 ret = brcmf_sdio_bus_sleep(bus, sleep, false);
4266 sdio_release_host(bus->sdiodev->func[1]);
4267
4268 return ret;
4269}
4270