blob: 80b5d478464549a8b79ac2a397e095c395bc1a33 [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
Arend van Spriel63ce3d52016-01-05 11:05:48 +010048#define DCMD_RESP_TIMEOUT msecs_to_jiffies(2000)
49#define CTL_DONE_TIMEOUT msecs_to_jiffies(2000)
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 Spriel9b2d2f2a82013-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;
Arend van Spriel4011fc42016-01-05 11:05:47 +0100506 bool wd_active;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200507
Franky Linf1e68c22012-09-13 21:12:00 +0200508 struct workqueue_struct *brcmf_wq;
509 struct work_struct datawork;
Hante Meuleman2c64e162015-03-18 13:25:22 +0100510 bool dpc_triggered;
511 bool dpc_running;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200512
Franky Linc8bf3482011-12-16 18:37:08 -0800513 bool txoff; /* Transmit flow-controlled */
Arend van Spriel80969832012-06-09 22:51:44 +0200514 struct brcmf_sdio_count sdcnt;
Piotr Haber4a3da992013-04-11 13:28:46 +0200515 bool sr_enabled; /* SaveRestore enabled */
Arend van Spriel99824642015-02-06 18:36:45 +0100516 bool sleeping;
Franky Lin706478c2013-08-10 12:27:29 +0200517
518 u8 tx_hdrlen; /* sdio bus header length for tx packet */
Franky Lin8da9d2c2013-11-29 12:25:10 +0100519 bool txglom; /* host tx glomming enable flag */
Arend van Spriele217d1c2013-11-29 12:25:13 +0100520 u16 head_align; /* buffer pointer alignment */
521 u16 sgentry_align; /* scatter-gather buffer alignment */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200522};
523
Arend van Spriel5b435de2011-10-05 13:19:03 +0200524/* clkstate */
525#define CLK_NONE 0
526#define CLK_SDONLY 1
Piotr Haber4a3da992013-04-11 13:28:46 +0200527#define CLK_PENDING 2
Arend van Spriel5b435de2011-10-05 13:19:03 +0200528#define CLK_AVAIL 3
529
Joe Perches8ae74652012-01-15 00:38:38 -0800530#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200531static int qcount[NUMPRIO];
Joe Perches8ae74652012-01-15 00:38:38 -0800532#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200533
Hante Meuleman668761a2013-04-12 10:55:55 +0200534#define DEFAULT_SDIO_DRIVE_STRENGTH 6 /* in milliamps */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200535
536#define RETRYCHAN(chan) ((chan) == SDPCM_EVENT_CHANNEL)
537
538/* Retry count for register access failures */
539static const uint retry_limit = 2;
540
541/* Limit on rounding up frames */
542static const uint max_roundup = 512;
543
544#define ALIGNMENT 4
545
Franky Lin9d7d6f92012-10-22 10:36:24 -0700546enum brcmf_sdio_frmtype {
547 BRCMF_SDIO_FT_NORMAL,
548 BRCMF_SDIO_FT_SUPER,
549 BRCMF_SDIO_FT_SUB,
550};
551
Arend van Spriel65d80d02014-01-29 15:32:14 +0100552#define SDIOD_DRVSTR_KEY(chip, pmu) (((chip) << 16) | (pmu))
553
554/* SDIO Pad drive strength to select value mappings */
555struct sdiod_drive_str {
556 u8 strength; /* Pad Drive Strength in mA */
557 u8 sel; /* Chip-specific select value */
558};
559
560/* SDIO Drive Strength to sel value table for PMU Rev 11 (1.8V) */
561static const struct sdiod_drive_str sdiod_drvstr_tab1_1v8[] = {
562 {32, 0x6},
563 {26, 0x7},
564 {22, 0x4},
565 {16, 0x5},
566 {12, 0x2},
567 {8, 0x3},
568 {4, 0x0},
569 {0, 0x1}
570};
571
572/* SDIO Drive Strength to sel value table for PMU Rev 13 (1.8v) */
573static const struct sdiod_drive_str sdiod_drive_strength_tab5_1v8[] = {
574 {6, 0x7},
575 {5, 0x6},
576 {4, 0x5},
577 {3, 0x4},
578 {2, 0x2},
579 {1, 0x1},
580 {0, 0x0}
581};
582
583/* SDIO Drive Strength to sel value table for PMU Rev 17 (1.8v) */
584static const struct sdiod_drive_str sdiod_drvstr_tab6_1v8[] = {
585 {3, 0x3},
586 {2, 0x2},
587 {1, 0x1},
588 {0, 0x0} };
589
590/* SDIO Drive Strength to sel value table for 43143 PMU Rev 17 (3.3V) */
591static const struct sdiod_drive_str sdiod_drvstr_tab2_3v3[] = {
592 {16, 0x7},
593 {12, 0x5},
594 {8, 0x3},
595 {4, 0x1}
596};
597
Hante Meuleman46d703a2015-11-25 11:32:46 +0100598BRCMF_FW_NVRAM_DEF(43143, "brcmfmac43143-sdio.bin", "brcmfmac43143-sdio.txt");
599BRCMF_FW_NVRAM_DEF(43241B0, "brcmfmac43241b0-sdio.bin",
600 "brcmfmac43241b0-sdio.txt");
601BRCMF_FW_NVRAM_DEF(43241B4, "brcmfmac43241b4-sdio.bin",
602 "brcmfmac43241b4-sdio.txt");
603BRCMF_FW_NVRAM_DEF(43241B5, "brcmfmac43241b5-sdio.bin",
604 "brcmfmac43241b5-sdio.txt");
605BRCMF_FW_NVRAM_DEF(4329, "brcmfmac4329-sdio.bin", "brcmfmac4329-sdio.txt");
606BRCMF_FW_NVRAM_DEF(4330, "brcmfmac4330-sdio.bin", "brcmfmac4330-sdio.txt");
607BRCMF_FW_NVRAM_DEF(4334, "brcmfmac4334-sdio.bin", "brcmfmac4334-sdio.txt");
608BRCMF_FW_NVRAM_DEF(43340, "brcmfmac43340-sdio.bin", "brcmfmac43340-sdio.txt");
609BRCMF_FW_NVRAM_DEF(4335, "brcmfmac4335-sdio.bin", "brcmfmac4335-sdio.txt");
610BRCMF_FW_NVRAM_DEF(43362, "brcmfmac43362-sdio.bin", "brcmfmac43362-sdio.txt");
611BRCMF_FW_NVRAM_DEF(4339, "brcmfmac4339-sdio.bin", "brcmfmac4339-sdio.txt");
612BRCMF_FW_NVRAM_DEF(43430, "brcmfmac43430-sdio.bin", "brcmfmac43430-sdio.txt");
613BRCMF_FW_NVRAM_DEF(43455, "brcmfmac43455-sdio.bin", "brcmfmac43455-sdio.txt");
614BRCMF_FW_NVRAM_DEF(4354, "brcmfmac4354-sdio.bin", "brcmfmac4354-sdio.txt");
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200615
Hante Meuleman46d703a2015-11-25 11:32:46 +0100616static struct brcmf_firmware_mapping brcmf_sdio_fwnames[] = {
617 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_43143_CHIP_ID, 0xFFFFFFFF, 43143),
618 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_43241_CHIP_ID, 0x0000001F, 43241B0),
619 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_43241_CHIP_ID, 0x00000020, 43241B4),
620 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_43241_CHIP_ID, 0xFFFFFFC0, 43241B5),
621 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_4329_CHIP_ID, 0xFFFFFFFF, 4329),
622 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_4330_CHIP_ID, 0xFFFFFFFF, 4330),
623 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_4334_CHIP_ID, 0xFFFFFFFF, 4334),
624 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_43340_CHIP_ID, 0xFFFFFFFF, 43340),
625 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_4335_CHIP_ID, 0xFFFFFFFF, 4335),
626 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_43362_CHIP_ID, 0xFFFFFFFE, 43362),
627 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_4339_CHIP_ID, 0xFFFFFFFF, 4339),
628 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_43430_CHIP_ID, 0xFFFFFFFF, 43430),
629 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_4345_CHIP_ID, 0xFFFFFFC0, 43455),
630 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_4354_CHIP_ID, 0xFFFFFFFF, 4354)
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200631};
632
Arend van Spriel5b435de2011-10-05 13:19:03 +0200633static void pkt_align(struct sk_buff *p, int len, int align)
634{
635 uint datalign;
636 datalign = (unsigned long)(p->data);
637 datalign = roundup(datalign, (align)) - datalign;
638 if (datalign)
639 skb_pull(p, datalign);
640 __skb_trim(p, len);
641}
642
643/* To check if there's window offered */
Franky Line92eedf2011-11-22 17:21:50 -0800644static bool data_ok(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200645{
646 return (u8)(bus->tx_max - bus->tx_seq) != 0 &&
647 ((u8)(bus->tx_max - bus->tx_seq) & 0x80) == 0;
648}
649
650/*
651 * Reads a register in the SDIO hardware block. This block occupies a series of
652 * adresses on the 32 bit backplane bus.
653 */
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100654static int r_sdreg32(struct brcmf_sdio *bus, u32 *regvar, u32 offset)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200655{
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100656 struct brcmf_core *core;
Franky Lin79ae3952012-05-04 18:27:34 -0700657 int ret;
Franky Lin58692752012-05-04 18:27:36 -0700658
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100659 core = brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV);
660 *regvar = brcmf_sdiod_regrl(bus->sdiodev, core->base + offset, &ret);
Franky Lin58692752012-05-04 18:27:36 -0700661
662 return ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200663}
664
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100665static int w_sdreg32(struct brcmf_sdio *bus, u32 regval, u32 reg_offset)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200666{
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100667 struct brcmf_core *core;
Franky Line13ce262012-05-04 18:27:35 -0700668 int ret;
Franky Lin58692752012-05-04 18:27:36 -0700669
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100670 core = brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV);
671 brcmf_sdiod_regwl(bus->sdiodev, core->base + reg_offset, regval, &ret);
Franky Lin58692752012-05-04 18:27:36 -0700672
673 return ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200674}
675
Piotr Haber4a3da992013-04-11 13:28:46 +0200676static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100677brcmf_sdio_kso_control(struct brcmf_sdio *bus, bool on)
Piotr Haber4a3da992013-04-11 13:28:46 +0200678{
679 u8 wr_val = 0, rd_val, cmp_val, bmask;
680 int err = 0;
681 int try_cnt = 0;
682
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100683 brcmf_dbg(TRACE, "Enter: on=%d\n", on);
Piotr Haber4a3da992013-04-11 13:28:46 +0200684
685 wr_val = (on << SBSDIO_FUNC1_SLEEPCSR_KSO_SHIFT);
686 /* 1st KSO write goes to AOS wake up core if device is asleep */
Arend van Spriela39be272013-12-12 11:58:58 +0100687 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
688 wr_val, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +0200689
690 if (on) {
691 /* device WAKEUP through KSO:
692 * write bit 0 & read back until
693 * both bits 0 (kso bit) & 1 (dev on status) are set
694 */
695 cmp_val = SBSDIO_FUNC1_SLEEPCSR_KSO_MASK |
696 SBSDIO_FUNC1_SLEEPCSR_DEVON_MASK;
697 bmask = cmp_val;
698 usleep_range(2000, 3000);
699 } else {
700 /* Put device to sleep, turn off KSO */
701 cmp_val = 0;
702 /* only check for bit0, bit1(dev on status) may not
703 * get cleared right away
704 */
705 bmask = SBSDIO_FUNC1_SLEEPCSR_KSO_MASK;
706 }
707
708 do {
709 /* reliable KSO bit set/clr:
710 * the sdiod sleep write access is synced to PMU 32khz clk
711 * just one write attempt may fail,
712 * read it back until it matches written value
713 */
Arend van Spriela39be272013-12-12 11:58:58 +0100714 rd_val = brcmf_sdiod_regrb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
715 &err);
Piotr Haber4a3da992013-04-11 13:28:46 +0200716 if (((rd_val & bmask) == cmp_val) && !err)
717 break;
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100718
Piotr Haber4a3da992013-04-11 13:28:46 +0200719 udelay(KSO_WAIT_US);
Arend van Spriela39be272013-12-12 11:58:58 +0100720 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
721 wr_val, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +0200722 } while (try_cnt++ < MAX_KSO_ATTEMPTS);
723
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100724 if (try_cnt > 2)
725 brcmf_dbg(SDIO, "try_cnt=%d rd_val=0x%x err=%d\n", try_cnt,
726 rd_val, err);
727
728 if (try_cnt > MAX_KSO_ATTEMPTS)
729 brcmf_err("max tries: rd_val=0x%x err=%d\n", rd_val, err);
730
Piotr Haber4a3da992013-04-11 13:28:46 +0200731 return err;
732}
733
Arend van Spriel5b435de2011-10-05 13:19:03 +0200734#define HOSTINTMASK (I_HMB_SW_MASK | I_CHIPACTIVE)
735
Arend van Spriel5b435de2011-10-05 13:19:03 +0200736/* Turn backplane clock on or off */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100737static int brcmf_sdio_htclk(struct brcmf_sdio *bus, bool on, bool pendok)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200738{
739 int err;
740 u8 clkctl, clkreq, devctl;
741 unsigned long timeout;
742
Arend van Sprielc3203372013-04-03 12:40:44 +0200743 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200744
745 clkctl = 0;
746
Piotr Haber4a3da992013-04-11 13:28:46 +0200747 if (bus->sr_enabled) {
748 bus->clkstate = (on ? CLK_AVAIL : CLK_SDONLY);
749 return 0;
750 }
751
Arend van Spriel5b435de2011-10-05 13:19:03 +0200752 if (on) {
753 /* Request HT Avail */
754 clkreq =
755 bus->alp_only ? SBSDIO_ALP_AVAIL_REQ : SBSDIO_HT_AVAIL_REQ;
756
Arend van Spriela39be272013-12-12 11:58:58 +0100757 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
758 clkreq, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200759 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100760 brcmf_err("HT Avail request error: %d\n", err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200761 return -EBADE;
762 }
763
Arend van Spriel5b435de2011-10-05 13:19:03 +0200764 /* Check current status */
Arend van Spriela39be272013-12-12 11:58:58 +0100765 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
766 SBSDIO_FUNC1_CHIPCLKCSR, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200767 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100768 brcmf_err("HT Avail read error: %d\n", err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200769 return -EBADE;
770 }
771
772 /* Go to pending and await interrupt if appropriate */
773 if (!SBSDIO_CLKAV(clkctl, bus->alp_only) && pendok) {
774 /* Allow only clock-available interrupt */
Arend van Spriela39be272013-12-12 11:58:58 +0100775 devctl = brcmf_sdiod_regrb(bus->sdiodev,
776 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200777 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100778 brcmf_err("Devctl error setting CA: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200779 err);
780 return -EBADE;
781 }
782
783 devctl |= SBSDIO_DEVCTL_CA_INT_ONLY;
Arend van Spriela39be272013-12-12 11:58:58 +0100784 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
785 devctl, &err);
Arend van Sprielc3203372013-04-03 12:40:44 +0200786 brcmf_dbg(SDIO, "CLKCTL: set PENDING\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200787 bus->clkstate = CLK_PENDING;
788
789 return 0;
790 } else if (bus->clkstate == CLK_PENDING) {
791 /* Cancel CA-only interrupt filter */
Arend van Spriela39be272013-12-12 11:58:58 +0100792 devctl = brcmf_sdiod_regrb(bus->sdiodev,
793 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200794 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
Arend van Spriela39be272013-12-12 11:58:58 +0100795 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
796 devctl, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200797 }
798
799 /* Otherwise, wait here (polling) for HT Avail */
800 timeout = jiffies +
801 msecs_to_jiffies(PMU_MAX_TRANSITION_DLY/1000);
802 while (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
Arend van Spriela39be272013-12-12 11:58:58 +0100803 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
804 SBSDIO_FUNC1_CHIPCLKCSR,
805 &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200806 if (time_after(jiffies, timeout))
807 break;
808 else
809 usleep_range(5000, 10000);
810 }
811 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100812 brcmf_err("HT Avail request error: %d\n", err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200813 return -EBADE;
814 }
815 if (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100816 brcmf_err("HT Avail timeout (%d): clkctl 0x%02x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200817 PMU_MAX_TRANSITION_DLY, clkctl);
818 return -EBADE;
819 }
820
821 /* Mark clock available */
822 bus->clkstate = CLK_AVAIL;
Arend van Sprielc3203372013-04-03 12:40:44 +0200823 brcmf_dbg(SDIO, "CLKCTL: turned ON\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200824
Joe Perches8ae74652012-01-15 00:38:38 -0800825#if defined(DEBUG)
Joe Perches23677ce2012-02-09 11:17:23 +0000826 if (!bus->alp_only) {
Arend van Spriel5b435de2011-10-05 13:19:03 +0200827 if (SBSDIO_ALPONLY(clkctl))
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100828 brcmf_err("HT Clock should be on\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200829 }
Joe Perches8ae74652012-01-15 00:38:38 -0800830#endif /* defined (DEBUG) */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200831
Arend van Spriel5b435de2011-10-05 13:19:03 +0200832 } else {
833 clkreq = 0;
834
835 if (bus->clkstate == CLK_PENDING) {
836 /* Cancel CA-only interrupt filter */
Arend van Spriela39be272013-12-12 11:58:58 +0100837 devctl = brcmf_sdiod_regrb(bus->sdiodev,
838 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200839 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
Arend van Spriela39be272013-12-12 11:58:58 +0100840 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
841 devctl, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200842 }
843
844 bus->clkstate = CLK_SDONLY;
Arend van Spriela39be272013-12-12 11:58:58 +0100845 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
846 clkreq, &err);
Arend van Sprielc3203372013-04-03 12:40:44 +0200847 brcmf_dbg(SDIO, "CLKCTL: turned OFF\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200848 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100849 brcmf_err("Failed access turning clock off: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200850 err);
851 return -EBADE;
852 }
853 }
854 return 0;
855}
856
857/* Change idle/active SD state */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100858static int brcmf_sdio_sdclk(struct brcmf_sdio *bus, bool on)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200859{
Arend van Sprielc3203372013-04-03 12:40:44 +0200860 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200861
862 if (on)
863 bus->clkstate = CLK_SDONLY;
864 else
865 bus->clkstate = CLK_NONE;
866
867 return 0;
868}
869
870/* Transition SD and backplane clock readiness */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100871static int brcmf_sdio_clkctl(struct brcmf_sdio *bus, uint target, bool pendok)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200872{
Joe Perches8ae74652012-01-15 00:38:38 -0800873#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200874 uint oldstate = bus->clkstate;
Joe Perches8ae74652012-01-15 00:38:38 -0800875#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200876
Arend van Sprielc3203372013-04-03 12:40:44 +0200877 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200878
879 /* Early exit if we're already there */
Hante Meulemanb441ba82015-03-06 18:40:39 +0100880 if (bus->clkstate == target)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200881 return 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200882
883 switch (target) {
884 case CLK_AVAIL:
885 /* Make sure SD clock is available */
886 if (bus->clkstate == CLK_NONE)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100887 brcmf_sdio_sdclk(bus, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200888 /* Now request HT Avail on the backplane */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100889 brcmf_sdio_htclk(bus, true, pendok);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200890 break;
891
892 case CLK_SDONLY:
893 /* Remove HT request, or bring up SD clock */
894 if (bus->clkstate == CLK_NONE)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100895 brcmf_sdio_sdclk(bus, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200896 else if (bus->clkstate == CLK_AVAIL)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100897 brcmf_sdio_htclk(bus, false, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200898 else
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100899 brcmf_err("request for %d -> %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200900 bus->clkstate, target);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200901 break;
902
903 case CLK_NONE:
904 /* Make sure to remove HT request */
905 if (bus->clkstate == CLK_AVAIL)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100906 brcmf_sdio_htclk(bus, false, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200907 /* Now remove the SD clock */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100908 brcmf_sdio_sdclk(bus, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200909 break;
910 }
Joe Perches8ae74652012-01-15 00:38:38 -0800911#ifdef DEBUG
Arend van Sprielc3203372013-04-03 12:40:44 +0200912 brcmf_dbg(SDIO, "%d -> %d\n", oldstate, bus->clkstate);
Joe Perches8ae74652012-01-15 00:38:38 -0800913#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200914
915 return 0;
916}
917
Piotr Haber4a3da992013-04-11 13:28:46 +0200918static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100919brcmf_sdio_bus_sleep(struct brcmf_sdio *bus, bool sleep, bool pendok)
Piotr Haber4a3da992013-04-11 13:28:46 +0200920{
921 int err = 0;
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100922 u8 clkcsr;
Arend van Spriel82030d62014-01-29 15:32:20 +0100923
924 brcmf_dbg(SDIO, "Enter: request %s currently %s\n",
Piotr Haber4a3da992013-04-11 13:28:46 +0200925 (sleep ? "SLEEP" : "WAKE"),
Arend van Spriel99824642015-02-06 18:36:45 +0100926 (bus->sleeping ? "SLEEP" : "WAKE"));
Piotr Haber4a3da992013-04-11 13:28:46 +0200927
928 /* If SR is enabled control bus state with KSO */
929 if (bus->sr_enabled) {
930 /* Done if we're already in the requested state */
Arend van Spriel99824642015-02-06 18:36:45 +0100931 if (sleep == bus->sleeping)
Piotr Haber4a3da992013-04-11 13:28:46 +0200932 goto end;
933
934 /* Going to sleep */
935 if (sleep) {
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100936 clkcsr = brcmf_sdiod_regrb(bus->sdiodev,
937 SBSDIO_FUNC1_CHIPCLKCSR,
938 &err);
939 if ((clkcsr & SBSDIO_CSR_MASK) == 0) {
940 brcmf_dbg(SDIO, "no clock, set ALP\n");
941 brcmf_sdiod_regwb(bus->sdiodev,
942 SBSDIO_FUNC1_CHIPCLKCSR,
943 SBSDIO_ALP_AVAIL_REQ, &err);
944 }
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100945 err = brcmf_sdio_kso_control(bus, false);
Piotr Haber4a3da992013-04-11 13:28:46 +0200946 } else {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100947 err = brcmf_sdio_kso_control(bus, true);
Piotr Haber4a3da992013-04-11 13:28:46 +0200948 }
Arend van Spriel8982cd42015-01-25 20:31:34 +0100949 if (err) {
Piotr Haber4a3da992013-04-11 13:28:46 +0200950 brcmf_err("error while changing bus sleep state %d\n",
951 err);
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100952 goto done;
Piotr Haber4a3da992013-04-11 13:28:46 +0200953 }
954 }
955
956end:
957 /* control clocks */
958 if (sleep) {
959 if (!bus->sr_enabled)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100960 brcmf_sdio_clkctl(bus, CLK_NONE, pendok);
Piotr Haber4a3da992013-04-11 13:28:46 +0200961 } else {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100962 brcmf_sdio_clkctl(bus, CLK_AVAIL, pendok);
Arend van Spriel4011fc42016-01-05 11:05:47 +0100963 brcmf_sdio_wd_timer(bus, true);
Piotr Haber4a3da992013-04-11 13:28:46 +0200964 }
Arend van Spriel99824642015-02-06 18:36:45 +0100965 bus->sleeping = sleep;
Arend van Spriel8982cd42015-01-25 20:31:34 +0100966 brcmf_dbg(SDIO, "new state %s\n",
967 (sleep ? "SLEEP" : "WAKE"));
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100968done:
969 brcmf_dbg(SDIO, "Exit: err=%d\n", err);
Piotr Haber4a3da992013-04-11 13:28:46 +0200970 return err;
971
972}
973
Daniel Kim0801e6c2014-01-29 15:32:13 +0100974#ifdef DEBUG
975static inline bool brcmf_sdio_valid_shared_address(u32 addr)
976{
977 return !(addr == 0 || ((~addr >> 16) & 0xffff) == (addr & 0xffff));
978}
979
980static int brcmf_sdio_readshared(struct brcmf_sdio *bus,
981 struct sdpcm_shared *sh)
982{
Arend van Spriel9819a902015-03-11 16:11:32 +0100983 u32 addr = 0;
Daniel Kim0801e6c2014-01-29 15:32:13 +0100984 int rv;
985 u32 shaddr = 0;
986 struct sdpcm_shared_le sh_le;
987 __le32 addr_le;
988
Arend van Spriel9819a902015-03-11 16:11:32 +0100989 sdio_claim_host(bus->sdiodev->func[1]);
990 brcmf_sdio_bus_sleep(bus, false, false);
Daniel Kim0801e6c2014-01-29 15:32:13 +0100991
992 /*
993 * Read last word in socram to determine
994 * address of sdpcm_shared structure
995 */
Arend van Spriel9819a902015-03-11 16:11:32 +0100996 shaddr = bus->ci->rambase + bus->ci->ramsize - 4;
997 if (!bus->ci->rambase && brcmf_chip_sr_capable(bus->ci))
998 shaddr -= bus->ci->srsize;
999 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, shaddr,
1000 (u8 *)&addr_le, 4);
Daniel Kim0801e6c2014-01-29 15:32:13 +01001001 if (rv < 0)
Arend van Spriel9819a902015-03-11 16:11:32 +01001002 goto fail;
Daniel Kim0801e6c2014-01-29 15:32:13 +01001003
1004 /*
1005 * Check if addr is valid.
1006 * NVRAM length at the end of memory should have been overwritten.
1007 */
Arend van Spriel9819a902015-03-11 16:11:32 +01001008 addr = le32_to_cpu(addr_le);
Daniel Kim0801e6c2014-01-29 15:32:13 +01001009 if (!brcmf_sdio_valid_shared_address(addr)) {
Arend van Spriel9819a902015-03-11 16:11:32 +01001010 brcmf_err("invalid sdpcm_shared address 0x%08X\n", addr);
1011 rv = -EINVAL;
1012 goto fail;
Daniel Kim0801e6c2014-01-29 15:32:13 +01001013 }
1014
Arend van Spriel9819a902015-03-11 16:11:32 +01001015 brcmf_dbg(INFO, "sdpcm_shared address 0x%08X\n", addr);
1016
Daniel Kim0801e6c2014-01-29 15:32:13 +01001017 /* Read hndrte_shared structure */
1018 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, (u8 *)&sh_le,
1019 sizeof(struct sdpcm_shared_le));
1020 if (rv < 0)
Arend van Spriel9819a902015-03-11 16:11:32 +01001021 goto fail;
1022
1023 sdio_release_host(bus->sdiodev->func[1]);
Daniel Kim0801e6c2014-01-29 15:32:13 +01001024
1025 /* Endianness */
1026 sh->flags = le32_to_cpu(sh_le.flags);
1027 sh->trap_addr = le32_to_cpu(sh_le.trap_addr);
1028 sh->assert_exp_addr = le32_to_cpu(sh_le.assert_exp_addr);
1029 sh->assert_file_addr = le32_to_cpu(sh_le.assert_file_addr);
1030 sh->assert_line = le32_to_cpu(sh_le.assert_line);
1031 sh->console_addr = le32_to_cpu(sh_le.console_addr);
1032 sh->msgtrace_addr = le32_to_cpu(sh_le.msgtrace_addr);
1033
1034 if ((sh->flags & SDPCM_SHARED_VERSION_MASK) > SDPCM_SHARED_VERSION) {
1035 brcmf_err("sdpcm shared version unsupported: dhd %d dongle %d\n",
1036 SDPCM_SHARED_VERSION,
1037 sh->flags & SDPCM_SHARED_VERSION_MASK);
1038 return -EPROTO;
1039 }
Daniel Kim0801e6c2014-01-29 15:32:13 +01001040 return 0;
Arend van Spriel9819a902015-03-11 16:11:32 +01001041
1042fail:
1043 brcmf_err("unable to obtain sdpcm_shared info: rv=%d (addr=0x%x)\n",
1044 rv, addr);
1045 sdio_release_host(bus->sdiodev->func[1]);
1046 return rv;
Daniel Kim0801e6c2014-01-29 15:32:13 +01001047}
1048
1049static void brcmf_sdio_get_console_addr(struct brcmf_sdio *bus)
1050{
1051 struct sdpcm_shared sh;
1052
1053 if (brcmf_sdio_readshared(bus, &sh) == 0)
1054 bus->console_addr = sh.console_addr;
1055}
1056#else
1057static void brcmf_sdio_get_console_addr(struct brcmf_sdio *bus)
1058{
1059}
1060#endif /* DEBUG */
1061
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001062static u32 brcmf_sdio_hostmail(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001063{
1064 u32 intstatus = 0;
1065 u32 hmb_data;
1066 u8 fcbits;
Franky Lin58692752012-05-04 18:27:36 -07001067 int ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001068
Arend van Sprielc3203372013-04-03 12:40:44 +02001069 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001070
1071 /* Read mailbox data and ack that we did so */
Franky Lin58692752012-05-04 18:27:36 -07001072 ret = r_sdreg32(bus, &hmb_data,
1073 offsetof(struct sdpcmd_regs, tohostmailboxdata));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001074
Franky Lin58692752012-05-04 18:27:36 -07001075 if (ret == 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001076 w_sdreg32(bus, SMB_INT_ACK,
Franky Lin58692752012-05-04 18:27:36 -07001077 offsetof(struct sdpcmd_regs, tosbmailbox));
Arend van Spriel80969832012-06-09 22:51:44 +02001078 bus->sdcnt.f1regdata += 2;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001079
1080 /* Dongle recomposed rx frames, accept them again */
1081 if (hmb_data & HMB_DATA_NAKHANDLED) {
Arend van Sprielc3203372013-04-03 12:40:44 +02001082 brcmf_dbg(SDIO, "Dongle reports NAK handled, expect rtx of %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001083 bus->rx_seq);
1084 if (!bus->rxskip)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001085 brcmf_err("unexpected NAKHANDLED!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001086
1087 bus->rxskip = false;
1088 intstatus |= I_HMB_FRAME_IND;
1089 }
1090
1091 /*
1092 * DEVREADY does not occur with gSPI.
1093 */
1094 if (hmb_data & (HMB_DATA_DEVREADY | HMB_DATA_FWREADY)) {
1095 bus->sdpcm_ver =
1096 (hmb_data & HMB_DATA_VERSION_MASK) >>
1097 HMB_DATA_VERSION_SHIFT;
1098 if (bus->sdpcm_ver != SDPCM_PROT_VERSION)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001099 brcmf_err("Version mismatch, dongle reports %d, "
Arend van Spriel5b435de2011-10-05 13:19:03 +02001100 "expecting %d\n",
1101 bus->sdpcm_ver, SDPCM_PROT_VERSION);
1102 else
Arend van Sprielc3203372013-04-03 12:40:44 +02001103 brcmf_dbg(SDIO, "Dongle ready, protocol version %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001104 bus->sdpcm_ver);
Daniel Kim0801e6c2014-01-29 15:32:13 +01001105
1106 /*
1107 * Retrieve console state address now that firmware should have
1108 * updated it.
1109 */
1110 brcmf_sdio_get_console_addr(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001111 }
1112
1113 /*
1114 * Flow Control has been moved into the RX headers and this out of band
1115 * method isn't used any more.
1116 * remaining backward compatible with older dongles.
1117 */
1118 if (hmb_data & HMB_DATA_FC) {
1119 fcbits = (hmb_data & HMB_DATA_FCDATA_MASK) >>
1120 HMB_DATA_FCDATA_SHIFT;
1121
1122 if (fcbits & ~bus->flowcontrol)
Arend van Spriel80969832012-06-09 22:51:44 +02001123 bus->sdcnt.fc_xoff++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001124
1125 if (bus->flowcontrol & ~fcbits)
Arend van Spriel80969832012-06-09 22:51:44 +02001126 bus->sdcnt.fc_xon++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001127
Arend van Spriel80969832012-06-09 22:51:44 +02001128 bus->sdcnt.fc_rcvd++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001129 bus->flowcontrol = fcbits;
1130 }
1131
1132 /* Shouldn't be any others */
1133 if (hmb_data & ~(HMB_DATA_DEVREADY |
1134 HMB_DATA_NAKHANDLED |
1135 HMB_DATA_FC |
1136 HMB_DATA_FWREADY |
1137 HMB_DATA_FCDATA_MASK | HMB_DATA_VERSION_MASK))
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001138 brcmf_err("Unknown mailbox data content: 0x%02x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001139 hmb_data);
1140
1141 return intstatus;
1142}
1143
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001144static void brcmf_sdio_rxfail(struct brcmf_sdio *bus, bool abort, bool rtx)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001145{
1146 uint retries = 0;
1147 u16 lastrbc;
1148 u8 hi, lo;
1149 int err;
1150
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001151 brcmf_err("%sterminate frame%s\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001152 abort ? "abort command, " : "",
1153 rtx ? ", send NAK" : "");
1154
1155 if (abort)
Arend van Spriela39be272013-12-12 11:58:58 +01001156 brcmf_sdiod_abort(bus->sdiodev, SDIO_FUNC_2);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001157
Arend van Spriela39be272013-12-12 11:58:58 +01001158 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL,
1159 SFC_RF_TERM, &err);
Arend van Spriel80969832012-06-09 22:51:44 +02001160 bus->sdcnt.f1regdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001161
1162 /* Wait until the packet has been flushed (device/FIFO stable) */
1163 for (lastrbc = retries = 0xffff; retries > 0; retries--) {
Arend van Spriela39be272013-12-12 11:58:58 +01001164 hi = brcmf_sdiod_regrb(bus->sdiodev,
1165 SBSDIO_FUNC1_RFRAMEBCHI, &err);
1166 lo = brcmf_sdiod_regrb(bus->sdiodev,
1167 SBSDIO_FUNC1_RFRAMEBCLO, &err);
Arend van Spriel80969832012-06-09 22:51:44 +02001168 bus->sdcnt.f1regdata += 2;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001169
1170 if ((hi == 0) && (lo == 0))
1171 break;
1172
1173 if ((hi > (lastrbc >> 8)) && (lo > (lastrbc & 0x00ff))) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001174 brcmf_err("count growing: last 0x%04x now 0x%04x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001175 lastrbc, (hi << 8) + lo);
1176 }
1177 lastrbc = (hi << 8) + lo;
1178 }
1179
1180 if (!retries)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001181 brcmf_err("count never zeroed: last 0x%04x\n", lastrbc);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001182 else
Arend van Sprielc3203372013-04-03 12:40:44 +02001183 brcmf_dbg(SDIO, "flush took %d iterations\n", 0xffff - retries);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001184
1185 if (rtx) {
Arend van Spriel80969832012-06-09 22:51:44 +02001186 bus->sdcnt.rxrtx++;
Franky Lin58692752012-05-04 18:27:36 -07001187 err = w_sdreg32(bus, SMB_NAK,
1188 offsetof(struct sdpcmd_regs, tosbmailbox));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001189
Arend van Spriel80969832012-06-09 22:51:44 +02001190 bus->sdcnt.f1regdata++;
Franky Lin58692752012-05-04 18:27:36 -07001191 if (err == 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001192 bus->rxskip = true;
1193 }
1194
1195 /* Clear partial in any case */
Franky Lin4754fce2012-09-19 22:21:11 +02001196 bus->cur_read.len = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001197}
1198
Hante Meuleman81c78832014-02-25 20:30:35 +01001199static void brcmf_sdio_txfail(struct brcmf_sdio *bus)
1200{
1201 struct brcmf_sdio_dev *sdiodev = bus->sdiodev;
1202 u8 i, hi, lo;
1203
1204 /* On failure, abort the command and terminate the frame */
1205 brcmf_err("sdio error, abort command and terminate frame\n");
1206 bus->sdcnt.tx_sderrs++;
1207
1208 brcmf_sdiod_abort(sdiodev, SDIO_FUNC_2);
1209 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_FRAMECTRL, SFC_WF_TERM, NULL);
1210 bus->sdcnt.f1regdata++;
1211
1212 for (i = 0; i < 3; i++) {
1213 hi = brcmf_sdiod_regrb(sdiodev, SBSDIO_FUNC1_WFRAMEBCHI, NULL);
1214 lo = brcmf_sdiod_regrb(sdiodev, SBSDIO_FUNC1_WFRAMEBCLO, NULL);
1215 bus->sdcnt.f1regdata += 2;
1216 if ((hi == 0) && (lo == 0))
1217 break;
1218 }
1219}
1220
Arend van Spriel9a95e602011-11-10 20:30:29 +01001221/* return total length of buffer chain */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001222static uint brcmf_sdio_glom_len(struct brcmf_sdio *bus)
Arend van Spriel9a95e602011-11-10 20:30:29 +01001223{
1224 struct sk_buff *p;
1225 uint total;
1226
1227 total = 0;
1228 skb_queue_walk(&bus->glom, p)
1229 total += p->len;
1230 return total;
1231}
1232
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001233static void brcmf_sdio_free_glom(struct brcmf_sdio *bus)
Arend van Spriel046808d2011-11-10 20:30:31 +01001234{
1235 struct sk_buff *cur, *next;
1236
1237 skb_queue_walk_safe(&bus->glom, cur, next) {
1238 skb_unlink(cur, &bus->glom);
1239 brcmu_pkt_buf_free_skb(cur);
1240 }
1241}
1242
Franky Lin6bc52312013-08-10 12:27:28 +02001243/**
1244 * brcmfmac sdio bus specific header
1245 * This is the lowest layer header wrapped on the packets transmitted between
1246 * host and WiFi dongle which contains information needed for SDIO core and
1247 * firmware
1248 *
Franky Lin8da9d2c2013-11-29 12:25:10 +01001249 * It consists of 3 parts: hardware header, hardware extension header and
1250 * software header
Franky Lin6bc52312013-08-10 12:27:28 +02001251 * hardware header (frame tag) - 4 bytes
1252 * Byte 0~1: Frame length
1253 * Byte 2~3: Checksum, bit-wise inverse of frame length
Franky Lin8da9d2c2013-11-29 12:25:10 +01001254 * hardware extension header - 8 bytes
1255 * Tx glom mode only, N/A for Rx or normal Tx
1256 * Byte 0~1: Packet length excluding hw frame tag
1257 * Byte 2: Reserved
1258 * Byte 3: Frame flags, bit 0: last frame indication
1259 * Byte 4~5: Reserved
1260 * Byte 6~7: Tail padding length
Franky Lin6bc52312013-08-10 12:27:28 +02001261 * software header - 8 bytes
1262 * Byte 0: Rx/Tx sequence number
1263 * Byte 1: 4 MSB Channel number, 4 LSB arbitrary flag
1264 * Byte 2: Length of next data frame, reserved for Tx
1265 * Byte 3: Data offset
1266 * Byte 4: Flow control bits, reserved for Tx
1267 * Byte 5: Maximum Sequence number allowed by firmware for Tx, N/A for Tx packet
1268 * Byte 6~7: Reserved
1269 */
1270#define SDPCM_HWHDR_LEN 4
Franky Lin8da9d2c2013-11-29 12:25:10 +01001271#define SDPCM_HWEXT_LEN 8
Franky Lin6bc52312013-08-10 12:27:28 +02001272#define SDPCM_SWHDR_LEN 8
1273#define SDPCM_HDRLEN (SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN)
Franky Lin6bc52312013-08-10 12:27:28 +02001274/* software header */
1275#define SDPCM_SEQ_MASK 0x000000ff
1276#define SDPCM_SEQ_WRAP 256
1277#define SDPCM_CHANNEL_MASK 0x00000f00
1278#define SDPCM_CHANNEL_SHIFT 8
1279#define SDPCM_CONTROL_CHANNEL 0 /* Control */
1280#define SDPCM_EVENT_CHANNEL 1 /* Asyc Event Indication */
1281#define SDPCM_DATA_CHANNEL 2 /* Data Xmit/Recv */
1282#define SDPCM_GLOM_CHANNEL 3 /* Coalesced packets */
1283#define SDPCM_TEST_CHANNEL 15 /* Test/debug packets */
1284#define SDPCM_GLOMDESC(p) (((u8 *)p)[1] & 0x80)
1285#define SDPCM_NEXTLEN_MASK 0x00ff0000
1286#define SDPCM_NEXTLEN_SHIFT 16
1287#define SDPCM_DOFFSET_MASK 0xff000000
1288#define SDPCM_DOFFSET_SHIFT 24
1289#define SDPCM_FCMASK_MASK 0x000000ff
1290#define SDPCM_WINDOW_MASK 0x0000ff00
1291#define SDPCM_WINDOW_SHIFT 8
1292
1293static inline u8 brcmf_sdio_getdatoffset(u8 *swheader)
1294{
1295 u32 hdrvalue;
1296 hdrvalue = *(u32 *)swheader;
1297 return (u8)((hdrvalue & SDPCM_DOFFSET_MASK) >> SDPCM_DOFFSET_SHIFT);
1298}
1299
1300static int brcmf_sdio_hdparse(struct brcmf_sdio *bus, u8 *header,
1301 struct brcmf_sdio_hdrinfo *rd,
1302 enum brcmf_sdio_frmtype type)
Franky Lin4754fce2012-09-19 22:21:11 +02001303{
1304 u16 len, checksum;
1305 u8 rx_seq, fc, tx_seq_max;
Franky Lin6bc52312013-08-10 12:27:28 +02001306 u32 swheader;
Franky Lin4754fce2012-09-19 22:21:11 +02001307
Arend van Spriel4b776962013-11-29 12:25:06 +01001308 trace_brcmf_sdpcm_hdr(SDPCM_RX, header);
Arend van Spriel76584ec2013-10-15 15:44:47 +02001309
Franky Lin6bc52312013-08-10 12:27:28 +02001310 /* hw header */
Franky Lin4754fce2012-09-19 22:21:11 +02001311 len = get_unaligned_le16(header);
1312 checksum = get_unaligned_le16(header + sizeof(u16));
1313 /* All zero means no more to read */
1314 if (!(len | checksum)) {
1315 bus->rxpending = false;
Franky Lin10510582012-11-05 16:22:26 -08001316 return -ENODATA;
Franky Lin4754fce2012-09-19 22:21:11 +02001317 }
1318 if ((u16)(~(len ^ checksum))) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001319 brcmf_err("HW header checksum error\n");
Franky Lin4754fce2012-09-19 22:21:11 +02001320 bus->sdcnt.rx_badhdr++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001321 brcmf_sdio_rxfail(bus, false, false);
Franky Lin10510582012-11-05 16:22:26 -08001322 return -EIO;
Franky Lin4754fce2012-09-19 22:21:11 +02001323 }
1324 if (len < SDPCM_HDRLEN) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001325 brcmf_err("HW header length error\n");
Franky Lin10510582012-11-05 16:22:26 -08001326 return -EPROTO;
Franky Lin4754fce2012-09-19 22:21:11 +02001327 }
Franky Lin9d7d6f92012-10-22 10:36:24 -07001328 if (type == BRCMF_SDIO_FT_SUPER &&
1329 (roundup(len, bus->blocksize) != rd->len)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001330 brcmf_err("HW superframe header length error\n");
Franky Lin10510582012-11-05 16:22:26 -08001331 return -EPROTO;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001332 }
1333 if (type == BRCMF_SDIO_FT_SUB && len > rd->len) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001334 brcmf_err("HW subframe header length error\n");
Franky Lin10510582012-11-05 16:22:26 -08001335 return -EPROTO;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001336 }
Franky Lin4754fce2012-09-19 22:21:11 +02001337 rd->len = len;
1338
Franky Lin6bc52312013-08-10 12:27:28 +02001339 /* software header */
1340 header += SDPCM_HWHDR_LEN;
1341 swheader = le32_to_cpu(*(__le32 *)header);
1342 if (type == BRCMF_SDIO_FT_SUPER && SDPCM_GLOMDESC(header)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001343 brcmf_err("Glom descriptor found in superframe head\n");
Franky Lin9d7d6f92012-10-22 10:36:24 -07001344 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001345 return -EINVAL;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001346 }
Franky Lin6bc52312013-08-10 12:27:28 +02001347 rx_seq = (u8)(swheader & SDPCM_SEQ_MASK);
1348 rd->channel = (swheader & SDPCM_CHANNEL_MASK) >> SDPCM_CHANNEL_SHIFT;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001349 if (len > MAX_RX_DATASZ && rd->channel != SDPCM_CONTROL_CHANNEL &&
1350 type != BRCMF_SDIO_FT_SUPER) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001351 brcmf_err("HW header length too long\n");
Franky Lin4754fce2012-09-19 22:21:11 +02001352 bus->sdcnt.rx_toolong++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001353 brcmf_sdio_rxfail(bus, false, false);
Franky Lin4754fce2012-09-19 22:21:11 +02001354 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001355 return -EPROTO;
Franky Lin4754fce2012-09-19 22:21:11 +02001356 }
Franky Lin9d7d6f92012-10-22 10:36:24 -07001357 if (type == BRCMF_SDIO_FT_SUPER && rd->channel != SDPCM_GLOM_CHANNEL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001358 brcmf_err("Wrong channel for superframe\n");
Franky Lin9d7d6f92012-10-22 10:36:24 -07001359 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001360 return -EINVAL;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001361 }
1362 if (type == BRCMF_SDIO_FT_SUB && rd->channel != SDPCM_DATA_CHANNEL &&
1363 rd->channel != SDPCM_EVENT_CHANNEL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001364 brcmf_err("Wrong channel for subframe\n");
Franky Lin9d7d6f92012-10-22 10:36:24 -07001365 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001366 return -EINVAL;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001367 }
Franky Lin6bc52312013-08-10 12:27:28 +02001368 rd->dat_offset = brcmf_sdio_getdatoffset(header);
Franky Lin4754fce2012-09-19 22:21:11 +02001369 if (rd->dat_offset < SDPCM_HDRLEN || rd->dat_offset > rd->len) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001370 brcmf_err("seq %d: bad data offset\n", rx_seq);
Franky Lin4754fce2012-09-19 22:21:11 +02001371 bus->sdcnt.rx_badhdr++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001372 brcmf_sdio_rxfail(bus, false, false);
Franky Lin4754fce2012-09-19 22:21:11 +02001373 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001374 return -ENXIO;
Franky Lin4754fce2012-09-19 22:21:11 +02001375 }
1376 if (rd->seq_num != rx_seq) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001377 brcmf_err("seq %d: sequence number error, expect %d\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001378 rx_seq, rd->seq_num);
1379 bus->sdcnt.rx_badseq++;
1380 rd->seq_num = rx_seq;
1381 }
Franky Lin9d7d6f92012-10-22 10:36:24 -07001382 /* no need to check the reset for subframe */
1383 if (type == BRCMF_SDIO_FT_SUB)
Franky Lin10510582012-11-05 16:22:26 -08001384 return 0;
Franky Lin6bc52312013-08-10 12:27:28 +02001385 rd->len_nxtfrm = (swheader & SDPCM_NEXTLEN_MASK) >> SDPCM_NEXTLEN_SHIFT;
Franky Lin4754fce2012-09-19 22:21:11 +02001386 if (rd->len_nxtfrm << 4 > MAX_RX_DATASZ) {
1387 /* only warm for NON glom packet */
1388 if (rd->channel != SDPCM_GLOM_CHANNEL)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001389 brcmf_err("seq %d: next length error\n", rx_seq);
Franky Lin4754fce2012-09-19 22:21:11 +02001390 rd->len_nxtfrm = 0;
1391 }
Franky Lin6bc52312013-08-10 12:27:28 +02001392 swheader = le32_to_cpu(*(__le32 *)(header + 4));
1393 fc = swheader & SDPCM_FCMASK_MASK;
Franky Lin4754fce2012-09-19 22:21:11 +02001394 if (bus->flowcontrol != fc) {
1395 if (~bus->flowcontrol & fc)
1396 bus->sdcnt.fc_xoff++;
1397 if (bus->flowcontrol & ~fc)
1398 bus->sdcnt.fc_xon++;
1399 bus->sdcnt.fc_rcvd++;
1400 bus->flowcontrol = fc;
1401 }
Franky Lin6bc52312013-08-10 12:27:28 +02001402 tx_seq_max = (swheader & SDPCM_WINDOW_MASK) >> SDPCM_WINDOW_SHIFT;
Franky Lin4754fce2012-09-19 22:21:11 +02001403 if ((u8)(tx_seq_max - bus->tx_seq) > 0x40) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001404 brcmf_err("seq %d: max tx seq number error\n", rx_seq);
Franky Lin4754fce2012-09-19 22:21:11 +02001405 tx_seq_max = bus->tx_seq + 2;
1406 }
1407 bus->tx_max = tx_seq_max;
1408
Franky Lin10510582012-11-05 16:22:26 -08001409 return 0;
Franky Lin4754fce2012-09-19 22:21:11 +02001410}
1411
Franky Lin6bc52312013-08-10 12:27:28 +02001412static inline void brcmf_sdio_update_hwhdr(u8 *header, u16 frm_length)
1413{
1414 *(__le16 *)header = cpu_to_le16(frm_length);
1415 *(((__le16 *)header) + 1) = cpu_to_le16(~frm_length);
1416}
1417
1418static void brcmf_sdio_hdpack(struct brcmf_sdio *bus, u8 *header,
1419 struct brcmf_sdio_hdrinfo *hd_info)
1420{
Franky Lin8da9d2c2013-11-29 12:25:10 +01001421 u32 hdrval;
1422 u8 hdr_offset;
Franky Lin6bc52312013-08-10 12:27:28 +02001423
1424 brcmf_sdio_update_hwhdr(header, hd_info->len);
Franky Lin8da9d2c2013-11-29 12:25:10 +01001425 hdr_offset = SDPCM_HWHDR_LEN;
Franky Lin6bc52312013-08-10 12:27:28 +02001426
Franky Lin8da9d2c2013-11-29 12:25:10 +01001427 if (bus->txglom) {
1428 hdrval = (hd_info->len - hdr_offset) | (hd_info->lastfrm << 24);
1429 *((__le32 *)(header + hdr_offset)) = cpu_to_le32(hdrval);
1430 hdrval = (u16)hd_info->tail_pad << 16;
1431 *(((__le32 *)(header + hdr_offset)) + 1) = cpu_to_le32(hdrval);
1432 hdr_offset += SDPCM_HWEXT_LEN;
1433 }
1434
1435 hdrval = hd_info->seq_num;
1436 hdrval |= (hd_info->channel << SDPCM_CHANNEL_SHIFT) &
1437 SDPCM_CHANNEL_MASK;
1438 hdrval |= (hd_info->dat_offset << SDPCM_DOFFSET_SHIFT) &
1439 SDPCM_DOFFSET_MASK;
1440 *((__le32 *)(header + hdr_offset)) = cpu_to_le32(hdrval);
1441 *(((__le32 *)(header + hdr_offset)) + 1) = 0;
1442 trace_brcmf_sdpcm_hdr(SDPCM_TX + !!(bus->txglom), header);
Franky Lin6bc52312013-08-10 12:27:28 +02001443}
1444
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001445static u8 brcmf_sdio_rxglom(struct brcmf_sdio *bus, u8 rxseq)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001446{
1447 u16 dlen, totlen;
1448 u8 *dptr, num = 0;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001449 u16 sublen;
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001450 struct sk_buff *pfirst, *pnext;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001451
1452 int errcode;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001453 u8 doff, sfdoff;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001454
Franky Lin6bc52312013-08-10 12:27:28 +02001455 struct brcmf_sdio_hdrinfo rd_new;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001456
1457 /* If packets, issue read(s) and send up packet chain */
1458 /* Return sequence numbers consumed? */
1459
Arend van Sprielc3203372013-04-03 12:40:44 +02001460 brcmf_dbg(SDIO, "start: glomd %p glom %p\n",
Arend van Sprielb83db862011-10-19 12:51:09 +02001461 bus->glomd, skb_peek(&bus->glom));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001462
1463 /* If there's a descriptor, generate the packet chain */
1464 if (bus->glomd) {
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001465 pfirst = pnext = NULL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001466 dlen = (u16) (bus->glomd->len);
1467 dptr = bus->glomd->data;
1468 if (!dlen || (dlen & 1)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001469 brcmf_err("bad glomd len(%d), ignore descriptor\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001470 dlen);
1471 dlen = 0;
1472 }
1473
1474 for (totlen = num = 0; dlen; num++) {
1475 /* Get (and move past) next length */
1476 sublen = get_unaligned_le16(dptr);
1477 dlen -= sizeof(u16);
1478 dptr += sizeof(u16);
1479 if ((sublen < SDPCM_HDRLEN) ||
1480 ((num == 0) && (sublen < (2 * SDPCM_HDRLEN)))) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001481 brcmf_err("descriptor len %d bad: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001482 num, sublen);
1483 pnext = NULL;
1484 break;
1485 }
Arend van Spriele217d1c2013-11-29 12:25:13 +01001486 if (sublen % bus->sgentry_align) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001487 brcmf_err("sublen %d not multiple of %d\n",
Arend van Spriele217d1c2013-11-29 12:25:13 +01001488 sublen, bus->sgentry_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001489 }
1490 totlen += sublen;
1491
1492 /* For last frame, adjust read len so total
1493 is a block multiple */
1494 if (!dlen) {
1495 sublen +=
1496 (roundup(totlen, bus->blocksize) - totlen);
1497 totlen = roundup(totlen, bus->blocksize);
1498 }
1499
1500 /* Allocate/chain packet for next subframe */
Arend van Spriele217d1c2013-11-29 12:25:13 +01001501 pnext = brcmu_pkt_buf_get_skb(sublen + bus->sgentry_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001502 if (pnext == NULL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001503 brcmf_err("bcm_pkt_buf_get_skb failed, num %d len %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001504 num, sublen);
1505 break;
1506 }
Arend van Sprielb83db862011-10-19 12:51:09 +02001507 skb_queue_tail(&bus->glom, pnext);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001508
1509 /* Adhere to start alignment requirements */
Arend van Spriele217d1c2013-11-29 12:25:13 +01001510 pkt_align(pnext, sublen, bus->sgentry_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001511 }
1512
1513 /* If all allocations succeeded, save packet chain
1514 in bus structure */
1515 if (pnext) {
1516 brcmf_dbg(GLOM, "allocated %d-byte packet chain for %d subframes\n",
1517 totlen, num);
Franky Lin4754fce2012-09-19 22:21:11 +02001518 if (BRCMF_GLOM_ON() && bus->cur_read.len &&
1519 totlen != bus->cur_read.len) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001520 brcmf_dbg(GLOM, "glomdesc mismatch: nextlen %d glomdesc %d rxseq %d\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001521 bus->cur_read.len, totlen, rxseq);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001522 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02001523 pfirst = pnext = NULL;
1524 } else {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001525 brcmf_sdio_free_glom(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001526 num = 0;
1527 }
1528
1529 /* Done with descriptor packet */
1530 brcmu_pkt_buf_free_skb(bus->glomd);
1531 bus->glomd = NULL;
Franky Lin4754fce2012-09-19 22:21:11 +02001532 bus->cur_read.len = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001533 }
1534
1535 /* Ok -- either we just generated a packet chain,
1536 or had one from before */
Arend van Sprielb83db862011-10-19 12:51:09 +02001537 if (!skb_queue_empty(&bus->glom)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001538 if (BRCMF_GLOM_ON()) {
1539 brcmf_dbg(GLOM, "try superframe read, packet chain:\n");
Arend van Sprielb83db862011-10-19 12:51:09 +02001540 skb_queue_walk(&bus->glom, pnext) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001541 brcmf_dbg(GLOM, " %p: %p len 0x%04x (%d)\n",
1542 pnext, (u8 *) (pnext->data),
1543 pnext->len, pnext->len);
1544 }
1545 }
1546
Arend van Sprielb83db862011-10-19 12:51:09 +02001547 pfirst = skb_peek(&bus->glom);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001548 dlen = (u16) brcmf_sdio_glom_len(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001549
1550 /* Do an SDIO read for the superframe. Configurable iovar to
1551 * read directly into the chained packet, or allocate a large
1552 * packet and and copy into the chain.
1553 */
Franky Lin38b0b0d2012-11-05 16:22:24 -08001554 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriela39be272013-12-12 11:58:58 +01001555 errcode = brcmf_sdiod_recv_chain(bus->sdiodev,
Arend van Spriela39be272013-12-12 11:58:58 +01001556 &bus->glom, dlen);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001557 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel80969832012-06-09 22:51:44 +02001558 bus->sdcnt.f2rxdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001559
Franky Lin64d66c32015-11-25 11:32:40 +01001560 /* On failure, kill the superframe */
Arend van Spriel5b435de2011-10-05 13:19:03 +02001561 if (errcode < 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001562 brcmf_err("glom read of %d bytes failed: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001563 dlen, errcode);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001564
Franky Lin38b0b0d2012-11-05 16:22:24 -08001565 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin64d66c32015-11-25 11:32:40 +01001566 brcmf_sdio_rxfail(bus, true, false);
1567 bus->sdcnt.rxglomfail++;
1568 brcmf_sdio_free_glom(bus);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001569 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001570 return 0;
1571 }
Joe Perches1e023822012-01-15 00:38:40 -08001572
1573 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
1574 pfirst->data, min_t(int, pfirst->len, 48),
1575 "SUPERFRAME:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001576
Franky Lin9d7d6f92012-10-22 10:36:24 -07001577 rd_new.seq_num = rxseq;
1578 rd_new.len = dlen;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001579 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin6bc52312013-08-10 12:27:28 +02001580 errcode = brcmf_sdio_hdparse(bus, pfirst->data, &rd_new,
1581 BRCMF_SDIO_FT_SUPER);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001582 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin9d7d6f92012-10-22 10:36:24 -07001583 bus->cur_read.len = rd_new.len_nxtfrm << 4;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001584
1585 /* Remove superframe header, remember offset */
Franky Lin9d7d6f92012-10-22 10:36:24 -07001586 skb_pull(pfirst, rd_new.dat_offset);
1587 sfdoff = rd_new.dat_offset;
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001588 num = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001589
1590 /* Validate all the subframe headers */
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001591 skb_queue_walk(&bus->glom, pnext) {
1592 /* leave when invalid subframe is found */
1593 if (errcode)
1594 break;
1595
Franky Lin9d7d6f92012-10-22 10:36:24 -07001596 rd_new.len = pnext->len;
1597 rd_new.seq_num = rxseq++;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001598 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin6bc52312013-08-10 12:27:28 +02001599 errcode = brcmf_sdio_hdparse(bus, pnext->data, &rd_new,
1600 BRCMF_SDIO_FT_SUB);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001601 sdio_release_host(bus->sdiodev->func[1]);
Joe Perches1e023822012-01-15 00:38:40 -08001602 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
Franky Lin9d7d6f92012-10-22 10:36:24 -07001603 pnext->data, 32, "subframe:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001604
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001605 num++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001606 }
1607
1608 if (errcode) {
Franky Lin64d66c32015-11-25 11:32:40 +01001609 /* Terminate frame on error */
Franky Lin38b0b0d2012-11-05 16:22:24 -08001610 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin64d66c32015-11-25 11:32:40 +01001611 brcmf_sdio_rxfail(bus, true, false);
1612 bus->sdcnt.rxglomfail++;
1613 brcmf_sdio_free_glom(bus);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001614 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001615 bus->cur_read.len = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001616 return 0;
1617 }
1618
1619 /* Basic SD framing looks ok - process each packet (header) */
Arend van Spriel5b435de2011-10-05 13:19:03 +02001620
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001621 skb_queue_walk_safe(&bus->glom, pfirst, pnext) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001622 dptr = (u8 *) (pfirst->data);
1623 sublen = get_unaligned_le16(dptr);
Franky Lin6bc52312013-08-10 12:27:28 +02001624 doff = brcmf_sdio_getdatoffset(&dptr[SDPCM_HWHDR_LEN]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001625
Joe Perches1e023822012-01-15 00:38:40 -08001626 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_DATA_ON(),
Franky Lin9d7d6f92012-10-22 10:36:24 -07001627 dptr, pfirst->len,
1628 "Rx Subframe Data:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001629
1630 __skb_trim(pfirst, sublen);
1631 skb_pull(pfirst, doff);
1632
1633 if (pfirst->len == 0) {
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001634 skb_unlink(pfirst, &bus->glom);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001635 brcmu_pkt_buf_free_skb(pfirst);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001636 continue;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001637 }
1638
Joe Perches1e023822012-01-15 00:38:40 -08001639 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
1640 pfirst->data,
1641 min_t(int, pfirst->len, 32),
1642 "subframe %d to stack, %p (%p/%d) nxt/lnk %p/%p\n",
1643 bus->glom.qlen, pfirst, pfirst->data,
1644 pfirst->len, pfirst->next,
1645 pfirst->prev);
Arend van Spriel05f38202013-09-25 13:05:46 +02001646 skb_unlink(pfirst, &bus->glom);
1647 brcmf_rx_frame(bus->sdiodev->dev, pfirst);
1648 bus->sdcnt.rxglompkts++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001649 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02001650
Arend van Spriel80969832012-06-09 22:51:44 +02001651 bus->sdcnt.rxglomframes++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001652 }
1653 return num;
1654}
1655
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001656static int brcmf_sdio_dcmd_resp_wait(struct brcmf_sdio *bus, uint *condition,
1657 bool *pending)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001658{
1659 DECLARE_WAITQUEUE(wait, current);
Arend van Spriel63ce3d52016-01-05 11:05:48 +01001660 int timeout = DCMD_RESP_TIMEOUT;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001661
1662 /* Wait until control frame is available */
1663 add_wait_queue(&bus->dcmd_resp_wait, &wait);
1664 set_current_state(TASK_INTERRUPTIBLE);
1665
1666 while (!(*condition) && (!signal_pending(current) && timeout))
1667 timeout = schedule_timeout(timeout);
1668
1669 if (signal_pending(current))
1670 *pending = true;
1671
1672 set_current_state(TASK_RUNNING);
1673 remove_wait_queue(&bus->dcmd_resp_wait, &wait);
1674
1675 return timeout;
1676}
1677
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001678static int brcmf_sdio_dcmd_resp_wake(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001679{
Kosuke Tatsukawaa7decc42015-12-10 13:43:08 +01001680 wake_up_interruptible(&bus->dcmd_resp_wait);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001681
1682 return 0;
1683}
1684static void
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001685brcmf_sdio_read_control(struct brcmf_sdio *bus, u8 *hdr, uint len, uint doff)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001686{
1687 uint rdlen, pad;
Franky Lindd43a012012-11-05 16:22:22 -08001688 u8 *buf = NULL, *rbuf;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001689 int sdret;
1690
1691 brcmf_dbg(TRACE, "Enter\n");
1692
Franky Lindd43a012012-11-05 16:22:22 -08001693 if (bus->rxblen)
1694 buf = vzalloc(bus->rxblen);
Joe Perches14f8dc42013-02-07 11:46:27 +00001695 if (!buf)
Franky Lindd43a012012-11-05 16:22:22 -08001696 goto done;
Joe Perches14f8dc42013-02-07 11:46:27 +00001697
Franky Lindd43a012012-11-05 16:22:22 -08001698 rbuf = bus->rxbuf;
Arend van Spriel9b2d2f2a82013-11-29 12:25:14 +01001699 pad = ((unsigned long)rbuf % bus->head_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001700 if (pad)
Arend van Spriel9b2d2f2a82013-11-29 12:25:14 +01001701 rbuf += (bus->head_align - pad);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001702
1703 /* Copy the already-read portion over */
Franky Lindd43a012012-11-05 16:22:22 -08001704 memcpy(buf, hdr, BRCMF_FIRSTREAD);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001705 if (len <= BRCMF_FIRSTREAD)
1706 goto gotpkt;
1707
1708 /* Raise rdlen to next SDIO block to avoid tail command */
1709 rdlen = len - BRCMF_FIRSTREAD;
1710 if (bus->roundup && bus->blocksize && (rdlen > bus->blocksize)) {
1711 pad = bus->blocksize - (rdlen % bus->blocksize);
1712 if ((pad <= bus->roundup) && (pad < bus->blocksize) &&
Franky Linb01a6b32011-12-16 18:37:03 -08001713 ((len + pad) < bus->sdiodev->bus_if->maxctl))
Arend van Spriel5b435de2011-10-05 13:19:03 +02001714 rdlen += pad;
Arend van Spriel9b2d2f2a82013-11-29 12:25:14 +01001715 } else if (rdlen % bus->head_align) {
1716 rdlen += bus->head_align - (rdlen % bus->head_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001717 }
1718
Arend van Spriel5b435de2011-10-05 13:19:03 +02001719 /* Drop if the read is too big or it exceeds our maximum */
Franky Linb01a6b32011-12-16 18:37:03 -08001720 if ((rdlen + BRCMF_FIRSTREAD) > bus->sdiodev->bus_if->maxctl) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001721 brcmf_err("%d-byte control read exceeds %d-byte buffer\n",
Franky Linb01a6b32011-12-16 18:37:03 -08001722 rdlen, bus->sdiodev->bus_if->maxctl);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001723 brcmf_sdio_rxfail(bus, false, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001724 goto done;
1725 }
1726
Franky Linb01a6b32011-12-16 18:37:03 -08001727 if ((len - doff) > bus->sdiodev->bus_if->maxctl) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001728 brcmf_err("%d-byte ctl frame (%d-byte ctl data) exceeds %d-byte limit\n",
Franky Linb01a6b32011-12-16 18:37:03 -08001729 len, len - doff, bus->sdiodev->bus_if->maxctl);
Arend van Spriel80969832012-06-09 22:51:44 +02001730 bus->sdcnt.rx_toolong++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001731 brcmf_sdio_rxfail(bus, false, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001732 goto done;
1733 }
1734
Franky Lindd43a012012-11-05 16:22:22 -08001735 /* Read remain of frame body */
Arend van Spriela7cdd822013-12-12 11:58:59 +01001736 sdret = brcmf_sdiod_recv_buf(bus->sdiodev, rbuf, rdlen);
Arend van Spriel80969832012-06-09 22:51:44 +02001737 bus->sdcnt.f2rxdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001738
1739 /* Control frame failures need retransmission */
1740 if (sdret < 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001741 brcmf_err("read %d control bytes failed: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001742 rdlen, sdret);
Arend van Spriel80969832012-06-09 22:51:44 +02001743 bus->sdcnt.rxc_errors++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001744 brcmf_sdio_rxfail(bus, true, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001745 goto done;
Franky Lindd43a012012-11-05 16:22:22 -08001746 } else
1747 memcpy(buf + BRCMF_FIRSTREAD, rbuf, rdlen);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001748
1749gotpkt:
1750
Joe Perches1e023822012-01-15 00:38:40 -08001751 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_CTL_ON(),
Franky Lindd43a012012-11-05 16:22:22 -08001752 buf, len, "RxCtrl:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001753
1754 /* Point to valid data and indicate its length */
Franky Lindd43a012012-11-05 16:22:22 -08001755 spin_lock_bh(&bus->rxctl_lock);
1756 if (bus->rxctl) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001757 brcmf_err("last control frame is being processed.\n");
Franky Lindd43a012012-11-05 16:22:22 -08001758 spin_unlock_bh(&bus->rxctl_lock);
1759 vfree(buf);
1760 goto done;
1761 }
1762 bus->rxctl = buf + doff;
1763 bus->rxctl_orig = buf;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001764 bus->rxlen = len - doff;
Franky Lindd43a012012-11-05 16:22:22 -08001765 spin_unlock_bh(&bus->rxctl_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001766
1767done:
1768 /* Awake any waiters */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001769 brcmf_sdio_dcmd_resp_wake(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001770}
1771
1772/* Pad read to blocksize for efficiency */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001773static void brcmf_sdio_pad(struct brcmf_sdio *bus, u16 *pad, u16 *rdlen)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001774{
1775 if (bus->roundup && bus->blocksize && *rdlen > bus->blocksize) {
1776 *pad = bus->blocksize - (*rdlen % bus->blocksize);
1777 if (*pad <= bus->roundup && *pad < bus->blocksize &&
1778 *rdlen + *pad + BRCMF_FIRSTREAD < MAX_RX_DATASZ)
1779 *rdlen += *pad;
Arend van Spriel9b2d2f2a82013-11-29 12:25:14 +01001780 } else if (*rdlen % bus->head_align) {
1781 *rdlen += bus->head_align - (*rdlen % bus->head_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001782 }
1783}
1784
Franky Lin4754fce2012-09-19 22:21:11 +02001785static uint brcmf_sdio_readframes(struct brcmf_sdio *bus, uint maxframes)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001786{
Arend van Spriel5b435de2011-10-05 13:19:03 +02001787 struct sk_buff *pkt; /* Packet for event or data frames */
1788 u16 pad; /* Number of pad bytes to read */
Arend van Spriel5b435de2011-10-05 13:19:03 +02001789 uint rxleft = 0; /* Remaining number of frames allowed */
Arend van Spriel349e7102013-03-03 12:45:28 +01001790 int ret; /* Return code from calls */
Arend van Spriel5b435de2011-10-05 13:19:03 +02001791 uint rxcount = 0; /* Total frames read */
Franky Lin6bc52312013-08-10 12:27:28 +02001792 struct brcmf_sdio_hdrinfo *rd = &bus->cur_read, rd_new;
Franky Lin4754fce2012-09-19 22:21:11 +02001793 u8 head_read = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001794
1795 brcmf_dbg(TRACE, "Enter\n");
1796
1797 /* Not finished unless we encounter no more frames indication */
Franky Lin4754fce2012-09-19 22:21:11 +02001798 bus->rxpending = true;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001799
Franky Lin4754fce2012-09-19 22:21:11 +02001800 for (rd->seq_num = bus->rx_seq, rxleft = maxframes;
Arend van Spriela1ce7a02015-02-06 18:36:42 +01001801 !bus->rxskip && rxleft && bus->sdiodev->state == BRCMF_SDIOD_DATA;
Franky Lin4754fce2012-09-19 22:21:11 +02001802 rd->seq_num++, rxleft--) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001803
1804 /* Handle glomming separately */
Arend van Sprielb83db862011-10-19 12:51:09 +02001805 if (bus->glomd || !skb_queue_empty(&bus->glom)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001806 u8 cnt;
1807 brcmf_dbg(GLOM, "calling rxglom: glomd %p, glom %p\n",
Arend van Sprielb83db862011-10-19 12:51:09 +02001808 bus->glomd, skb_peek(&bus->glom));
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001809 cnt = brcmf_sdio_rxglom(bus, rd->seq_num);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001810 brcmf_dbg(GLOM, "rxglom returned %d\n", cnt);
Franky Lin4754fce2012-09-19 22:21:11 +02001811 rd->seq_num += cnt - 1;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001812 rxleft = (rxleft > cnt) ? (rxleft - cnt) : 1;
1813 continue;
1814 }
1815
Franky Lin4754fce2012-09-19 22:21:11 +02001816 rd->len_left = rd->len;
1817 /* read header first for unknow frame length */
Franky Lin38b0b0d2012-11-05 16:22:24 -08001818 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001819 if (!rd->len) {
Arend van Spriela39be272013-12-12 11:58:58 +01001820 ret = brcmf_sdiod_recv_buf(bus->sdiodev,
Arend van Spriela39be272013-12-12 11:58:58 +01001821 bus->rxhdr, BRCMF_FIRSTREAD);
Franky Lin4754fce2012-09-19 22:21:11 +02001822 bus->sdcnt.f2rxhdrs++;
Arend van Spriel349e7102013-03-03 12:45:28 +01001823 if (ret < 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001824 brcmf_err("RXHEADER FAILED: %d\n",
Arend van Spriel349e7102013-03-03 12:45:28 +01001825 ret);
Franky Lin4754fce2012-09-19 22:21:11 +02001826 bus->sdcnt.rx_hdrfail++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001827 brcmf_sdio_rxfail(bus, true, true);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001828 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001829 continue;
1830 }
1831
Franky Lin4754fce2012-09-19 22:21:11 +02001832 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() || BRCMF_HDRS_ON(),
1833 bus->rxhdr, SDPCM_HDRLEN,
1834 "RxHdr:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001835
Franky Lin6bc52312013-08-10 12:27:28 +02001836 if (brcmf_sdio_hdparse(bus, bus->rxhdr, rd,
1837 BRCMF_SDIO_FT_NORMAL)) {
Franky Lin38b0b0d2012-11-05 16:22:24 -08001838 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001839 if (!bus->rxpending)
1840 break;
1841 else
1842 continue;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001843 }
1844
Franky Lin4754fce2012-09-19 22:21:11 +02001845 if (rd->channel == SDPCM_CONTROL_CHANNEL) {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001846 brcmf_sdio_read_control(bus, bus->rxhdr,
1847 rd->len,
1848 rd->dat_offset);
Franky Lin4754fce2012-09-19 22:21:11 +02001849 /* prepare the descriptor for the next read */
1850 rd->len = rd->len_nxtfrm << 4;
1851 rd->len_nxtfrm = 0;
1852 /* treat all packet as event if we don't know */
1853 rd->channel = SDPCM_EVENT_CHANNEL;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001854 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001855 continue;
1856 }
1857 rd->len_left = rd->len > BRCMF_FIRSTREAD ?
1858 rd->len - BRCMF_FIRSTREAD : 0;
1859 head_read = BRCMF_FIRSTREAD;
1860 }
1861
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001862 brcmf_sdio_pad(bus, &pad, &rd->len_left);
Franky Lin4754fce2012-09-19 22:21:11 +02001863
1864 pkt = brcmu_pkt_buf_get_skb(rd->len_left + head_read +
Arend van Spriel9b2d2f2a82013-11-29 12:25:14 +01001865 bus->head_align);
Franky Lin4754fce2012-09-19 22:21:11 +02001866 if (!pkt) {
1867 /* Give up on data, request rtx of events */
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001868 brcmf_err("brcmu_pkt_buf_get_skb failed\n");
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001869 brcmf_sdio_rxfail(bus, false,
Franky Lin4754fce2012-09-19 22:21:11 +02001870 RETRYCHAN(rd->channel));
Franky Lin38b0b0d2012-11-05 16:22:24 -08001871 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001872 continue;
1873 }
1874 skb_pull(pkt, head_read);
Arend van Spriel9b2d2f2a82013-11-29 12:25:14 +01001875 pkt_align(pkt, rd->len_left, bus->head_align);
Franky Lin4754fce2012-09-19 22:21:11 +02001876
Arend van Spriela7cdd822013-12-12 11:58:59 +01001877 ret = brcmf_sdiod_recv_pkt(bus->sdiodev, pkt);
Franky Lin4754fce2012-09-19 22:21:11 +02001878 bus->sdcnt.f2rxdata++;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001879 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001880
Arend van Spriel349e7102013-03-03 12:45:28 +01001881 if (ret < 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001882 brcmf_err("read %d bytes from channel %d failed: %d\n",
Arend van Spriel349e7102013-03-03 12:45:28 +01001883 rd->len, rd->channel, ret);
Franky Lin4754fce2012-09-19 22:21:11 +02001884 brcmu_pkt_buf_free_skb(pkt);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001885 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001886 brcmf_sdio_rxfail(bus, true,
Franky Lin4754fce2012-09-19 22:21:11 +02001887 RETRYCHAN(rd->channel));
Franky Lin38b0b0d2012-11-05 16:22:24 -08001888 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001889 continue;
1890 }
1891
1892 if (head_read) {
1893 skb_push(pkt, head_read);
1894 memcpy(pkt->data, bus->rxhdr, head_read);
1895 head_read = 0;
1896 } else {
1897 memcpy(bus->rxhdr, pkt->data, SDPCM_HDRLEN);
1898 rd_new.seq_num = rd->seq_num;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001899 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin6bc52312013-08-10 12:27:28 +02001900 if (brcmf_sdio_hdparse(bus, bus->rxhdr, &rd_new,
1901 BRCMF_SDIO_FT_NORMAL)) {
Franky Lin4754fce2012-09-19 22:21:11 +02001902 rd->len = 0;
1903 brcmu_pkt_buf_free_skb(pkt);
1904 }
Arend van Spriel80969832012-06-09 22:51:44 +02001905 bus->sdcnt.rx_readahead_cnt++;
Franky Lin4754fce2012-09-19 22:21:11 +02001906 if (rd->len != roundup(rd_new.len, 16)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001907 brcmf_err("frame length mismatch:read %d, should be %d\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001908 rd->len,
1909 roundup(rd_new.len, 16) >> 4);
1910 rd->len = 0;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001911 brcmf_sdio_rxfail(bus, true, true);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001912 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001913 brcmu_pkt_buf_free_skb(pkt);
1914 continue;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001915 }
Franky Lin38b0b0d2012-11-05 16:22:24 -08001916 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001917 rd->len_nxtfrm = rd_new.len_nxtfrm;
1918 rd->channel = rd_new.channel;
1919 rd->dat_offset = rd_new.dat_offset;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001920
Joe Perches1e023822012-01-15 00:38:40 -08001921 brcmf_dbg_hex_dump(!(BRCMF_BYTES_ON() &&
1922 BRCMF_DATA_ON()) &&
1923 BRCMF_HDRS_ON(),
1924 bus->rxhdr, SDPCM_HDRLEN,
1925 "RxHdr:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001926
Franky Lin4754fce2012-09-19 22:21:11 +02001927 if (rd_new.channel == SDPCM_CONTROL_CHANNEL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001928 brcmf_err("readahead on control packet %d?\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001929 rd_new.seq_num);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001930 /* Force retry w/normal header read */
Franky Lin4754fce2012-09-19 22:21:11 +02001931 rd->len = 0;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001932 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001933 brcmf_sdio_rxfail(bus, false, true);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001934 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001935 brcmu_pkt_buf_free_skb(pkt);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001936 continue;
1937 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02001938 }
1939
Joe Perches1e023822012-01-15 00:38:40 -08001940 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_DATA_ON(),
Franky Lin4754fce2012-09-19 22:21:11 +02001941 pkt->data, rd->len, "Rx Data:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001942
Arend van Spriel5b435de2011-10-05 13:19:03 +02001943 /* Save superframe descriptor and allocate packet frame */
Franky Lin4754fce2012-09-19 22:21:11 +02001944 if (rd->channel == SDPCM_GLOM_CHANNEL) {
Franky Lin6bc52312013-08-10 12:27:28 +02001945 if (SDPCM_GLOMDESC(&bus->rxhdr[SDPCM_HWHDR_LEN])) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001946 brcmf_dbg(GLOM, "glom descriptor, %d bytes:\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001947 rd->len);
Joe Perches1e023822012-01-15 00:38:40 -08001948 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
Franky Lin4754fce2012-09-19 22:21:11 +02001949 pkt->data, rd->len,
Joe Perches1e023822012-01-15 00:38:40 -08001950 "Glom Data:\n");
Franky Lin4754fce2012-09-19 22:21:11 +02001951 __skb_trim(pkt, rd->len);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001952 skb_pull(pkt, SDPCM_HDRLEN);
1953 bus->glomd = pkt;
1954 } else {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001955 brcmf_err("%s: glom superframe w/o "
Arend van Spriel5b435de2011-10-05 13:19:03 +02001956 "descriptor!\n", __func__);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001957 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001958 brcmf_sdio_rxfail(bus, false, false);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001959 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001960 }
Franky Lin4754fce2012-09-19 22:21:11 +02001961 /* prepare the descriptor for the next read */
1962 rd->len = rd->len_nxtfrm << 4;
1963 rd->len_nxtfrm = 0;
1964 /* treat all packet as event if we don't know */
1965 rd->channel = SDPCM_EVENT_CHANNEL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001966 continue;
1967 }
1968
1969 /* Fill in packet len and prio, deliver upward */
Franky Lin4754fce2012-09-19 22:21:11 +02001970 __skb_trim(pkt, rd->len);
1971 skb_pull(pkt, rd->dat_offset);
1972
1973 /* prepare the descriptor for the next read */
1974 rd->len = rd->len_nxtfrm << 4;
1975 rd->len_nxtfrm = 0;
1976 /* treat all packet as event if we don't know */
1977 rd->channel = SDPCM_EVENT_CHANNEL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001978
1979 if (pkt->len == 0) {
1980 brcmu_pkt_buf_free_skb(pkt);
1981 continue;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001982 }
1983
Arend van Spriel05f38202013-09-25 13:05:46 +02001984 brcmf_rx_frame(bus->sdiodev->dev, pkt);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001985 }
Franky Lin4754fce2012-09-19 22:21:11 +02001986
Arend van Spriel5b435de2011-10-05 13:19:03 +02001987 rxcount = maxframes - rxleft;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001988 /* Message if we hit the limit */
1989 if (!rxleft)
Franky Lin4754fce2012-09-19 22:21:11 +02001990 brcmf_dbg(DATA, "hit rx limit of %d frames\n", maxframes);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001991 else
Arend van Spriel5b435de2011-10-05 13:19:03 +02001992 brcmf_dbg(DATA, "processed %d frames\n", rxcount);
1993 /* Back off rxseq if awaiting rtx, update rx_seq */
1994 if (bus->rxskip)
Franky Lin4754fce2012-09-19 22:21:11 +02001995 rd->seq_num--;
1996 bus->rx_seq = rd->seq_num;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001997
1998 return rxcount;
1999}
2000
Arend van Spriel5b435de2011-10-05 13:19:03 +02002001static void
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002002brcmf_sdio_wait_event_wakeup(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002003{
Kosuke Tatsukawaa7decc42015-12-10 13:43:08 +01002004 wake_up_interruptible(&bus->ctrl_wait);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002005 return;
2006}
2007
Franky Lin8da9d2c2013-11-29 12:25:10 +01002008static int brcmf_sdio_txpkt_hdalign(struct brcmf_sdio *bus, struct sk_buff *pkt)
2009{
Arend van Spriele217d1c2013-11-29 12:25:13 +01002010 u16 head_pad;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002011 u8 *dat_buf;
2012
Franky Lin8da9d2c2013-11-29 12:25:10 +01002013 dat_buf = (u8 *)(pkt->data);
2014
2015 /* Check head padding */
Arend van Spriele217d1c2013-11-29 12:25:13 +01002016 head_pad = ((unsigned long)dat_buf % bus->head_align);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002017 if (head_pad) {
2018 if (skb_headroom(pkt) < head_pad) {
2019 bus->sdiodev->bus_if->tx_realloc++;
2020 head_pad = 0;
2021 if (skb_cow(pkt, head_pad))
2022 return -ENOMEM;
2023 }
2024 skb_push(pkt, head_pad);
2025 dat_buf = (u8 *)(pkt->data);
2026 memset(dat_buf, 0, head_pad + bus->tx_hdrlen);
2027 }
2028 return head_pad;
2029}
2030
Franky Lin5491c112013-09-25 13:05:43 +02002031/**
2032 * struct brcmf_skbuff_cb reserves first two bytes in sk_buff::cb for
2033 * bus layer usage.
2034 */
Franky Linb05e9252013-08-10 12:27:26 +02002035/* flag marking a dummy skb added for DMA alignment requirement */
Franky Lin5491c112013-09-25 13:05:43 +02002036#define ALIGN_SKB_FLAG 0x8000
Franky Linb05e9252013-08-10 12:27:26 +02002037/* bit mask of data length chopped from the previous packet */
Franky Lin5491c112013-09-25 13:05:43 +02002038#define ALIGN_SKB_CHOP_LEN_MASK 0x7fff
2039
Franky Lin8da9d2c2013-11-29 12:25:10 +01002040static int brcmf_sdio_txpkt_prep_sg(struct brcmf_sdio *bus,
Arend van Spriela64304f2013-10-15 15:44:56 +02002041 struct sk_buff_head *pktq,
Franky Lin8da9d2c2013-11-29 12:25:10 +01002042 struct sk_buff *pkt, u16 total_len)
Arend van Spriela64304f2013-10-15 15:44:56 +02002043{
Franky Lin8da9d2c2013-11-29 12:25:10 +01002044 struct brcmf_sdio_dev *sdiodev;
Arend van Spriela64304f2013-10-15 15:44:56 +02002045 struct sk_buff *pkt_pad;
Arend van Spriele217d1c2013-11-29 12:25:13 +01002046 u16 tail_pad, tail_chop, chain_pad;
Arend van Spriela64304f2013-10-15 15:44:56 +02002047 unsigned int blksize;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002048 bool lastfrm;
2049 int ntail, ret;
Arend van Spriela64304f2013-10-15 15:44:56 +02002050
Franky Lin8da9d2c2013-11-29 12:25:10 +01002051 sdiodev = bus->sdiodev;
Arend van Spriela64304f2013-10-15 15:44:56 +02002052 blksize = sdiodev->func[SDIO_FUNC_2]->cur_blksize;
Arend van Spriela64304f2013-10-15 15:44:56 +02002053 /* sg entry alignment should be a divisor of block size */
Arend van Spriele217d1c2013-11-29 12:25:13 +01002054 WARN_ON(blksize % bus->sgentry_align);
Arend van Spriela64304f2013-10-15 15:44:56 +02002055
2056 /* Check tail padding */
Franky Lin8da9d2c2013-11-29 12:25:10 +01002057 lastfrm = skb_queue_is_last(pktq, pkt);
2058 tail_pad = 0;
Arend van Spriele217d1c2013-11-29 12:25:13 +01002059 tail_chop = pkt->len % bus->sgentry_align;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002060 if (tail_chop)
Arend van Spriele217d1c2013-11-29 12:25:13 +01002061 tail_pad = bus->sgentry_align - tail_chop;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002062 chain_pad = (total_len + tail_pad) % blksize;
2063 if (lastfrm && chain_pad)
2064 tail_pad += blksize - chain_pad;
Arend van Spriela64304f2013-10-15 15:44:56 +02002065 if (skb_tailroom(pkt) < tail_pad && pkt->len > blksize) {
Arend van Spriel1eb43012014-02-20 18:55:55 +01002066 pkt_pad = brcmu_pkt_buf_get_skb(tail_pad + tail_chop +
2067 bus->head_align);
Arend van Spriela64304f2013-10-15 15:44:56 +02002068 if (pkt_pad == NULL)
2069 return -ENOMEM;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002070 ret = brcmf_sdio_txpkt_hdalign(bus, pkt_pad);
Dave Jones2dc3a8e2014-03-06 15:51:08 -05002071 if (unlikely(ret < 0)) {
2072 kfree_skb(pkt_pad);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002073 return ret;
Dave Jones2dc3a8e2014-03-06 15:51:08 -05002074 }
Arend van Spriela64304f2013-10-15 15:44:56 +02002075 memcpy(pkt_pad->data,
2076 pkt->data + pkt->len - tail_chop,
2077 tail_chop);
Arend van Spriel5aa9f0e2014-02-25 20:30:27 +01002078 *(u16 *)(pkt_pad->cb) = ALIGN_SKB_FLAG + tail_chop;
Arend van Spriela64304f2013-10-15 15:44:56 +02002079 skb_trim(pkt, pkt->len - tail_chop);
Arend van Spriel1eb43012014-02-20 18:55:55 +01002080 skb_trim(pkt_pad, tail_pad + tail_chop);
Arend van Spriela64304f2013-10-15 15:44:56 +02002081 __skb_queue_after(pktq, pkt, pkt_pad);
2082 } else {
2083 ntail = pkt->data_len + tail_pad -
2084 (pkt->end - pkt->tail);
2085 if (skb_cloned(pkt) || ntail > 0)
2086 if (pskb_expand_head(pkt, 0, ntail, GFP_ATOMIC))
2087 return -ENOMEM;
2088 if (skb_linearize(pkt))
2089 return -ENOMEM;
Arend van Spriela64304f2013-10-15 15:44:56 +02002090 __skb_put(pkt, tail_pad);
2091 }
2092
Franky Lin8da9d2c2013-11-29 12:25:10 +01002093 return tail_pad;
Arend van Spriela64304f2013-10-15 15:44:56 +02002094}
2095
Franky Linb05e9252013-08-10 12:27:26 +02002096/**
2097 * brcmf_sdio_txpkt_prep - packet preparation for transmit
2098 * @bus: brcmf_sdio structure pointer
2099 * @pktq: packet list pointer
2100 * @chan: virtual channel to transmit the packet
2101 *
2102 * Processes to be applied to the packet
2103 * - Align data buffer pointer
2104 * - Align data buffer length
2105 * - Prepare header
2106 * Return: negative value if there is error
2107 */
2108static int
2109brcmf_sdio_txpkt_prep(struct brcmf_sdio *bus, struct sk_buff_head *pktq,
2110 uint chan)
2111{
Franky Lin8da9d2c2013-11-29 12:25:10 +01002112 u16 head_pad, total_len;
Arend van Spriela64304f2013-10-15 15:44:56 +02002113 struct sk_buff *pkt_next;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002114 u8 txseq;
2115 int ret;
Franky Lin6bc52312013-08-10 12:27:28 +02002116 struct brcmf_sdio_hdrinfo hd_info = {0};
Franky Linb05e9252013-08-10 12:27:26 +02002117
Franky Lin8da9d2c2013-11-29 12:25:10 +01002118 txseq = bus->tx_seq;
2119 total_len = 0;
2120 skb_queue_walk(pktq, pkt_next) {
2121 /* alignment packet inserted in previous
2122 * loop cycle can be skipped as it is
2123 * already properly aligned and does not
2124 * need an sdpcm header.
2125 */
Arend van Spriel5aa9f0e2014-02-25 20:30:27 +01002126 if (*(u16 *)(pkt_next->cb) & ALIGN_SKB_FLAG)
Franky Lin8da9d2c2013-11-29 12:25:10 +01002127 continue;
Franky Linb05e9252013-08-10 12:27:26 +02002128
Franky Lin8da9d2c2013-11-29 12:25:10 +01002129 /* align packet data pointer */
2130 ret = brcmf_sdio_txpkt_hdalign(bus, pkt_next);
2131 if (ret < 0)
2132 return ret;
2133 head_pad = (u16)ret;
2134 if (head_pad)
Arend van Spriel1eb43012014-02-20 18:55:55 +01002135 memset(pkt_next->data + bus->tx_hdrlen, 0, head_pad);
Franky Linb05e9252013-08-10 12:27:26 +02002136
Franky Lin8da9d2c2013-11-29 12:25:10 +01002137 total_len += pkt_next->len;
Franky Linb05e9252013-08-10 12:27:26 +02002138
Arend van Spriela64304f2013-10-15 15:44:56 +02002139 hd_info.len = pkt_next->len;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002140 hd_info.lastfrm = skb_queue_is_last(pktq, pkt_next);
2141 if (bus->txglom && pktq->qlen > 1) {
2142 ret = brcmf_sdio_txpkt_prep_sg(bus, pktq,
2143 pkt_next, total_len);
2144 if (ret < 0)
2145 return ret;
2146 hd_info.tail_pad = (u16)ret;
2147 total_len += (u16)ret;
2148 }
2149
2150 hd_info.channel = chan;
2151 hd_info.dat_offset = head_pad + bus->tx_hdrlen;
2152 hd_info.seq_num = txseq++;
2153
2154 /* Now fill the header */
2155 brcmf_sdio_hdpack(bus, pkt_next->data, &hd_info);
2156
2157 if (BRCMF_BYTES_ON() &&
2158 ((BRCMF_CTL_ON() && chan == SDPCM_CONTROL_CHANNEL) ||
2159 (BRCMF_DATA_ON() && chan != SDPCM_CONTROL_CHANNEL)))
Hante Meuleman47ab4cd2014-02-25 20:30:29 +01002160 brcmf_dbg_hex_dump(true, pkt_next->data, hd_info.len,
Franky Lin8da9d2c2013-11-29 12:25:10 +01002161 "Tx Frame:\n");
2162 else if (BRCMF_HDRS_ON())
Hante Meuleman47ab4cd2014-02-25 20:30:29 +01002163 brcmf_dbg_hex_dump(true, pkt_next->data,
Franky Lin8da9d2c2013-11-29 12:25:10 +01002164 head_pad + bus->tx_hdrlen,
2165 "Tx Header:\n");
Franky Linb05e9252013-08-10 12:27:26 +02002166 }
Franky Lin8da9d2c2013-11-29 12:25:10 +01002167 /* Hardware length tag of the first packet should be total
2168 * length of the chain (including padding)
2169 */
2170 if (bus->txglom)
2171 brcmf_sdio_update_hwhdr(pktq->next->data, total_len);
Franky Linb05e9252013-08-10 12:27:26 +02002172 return 0;
2173}
2174
2175/**
2176 * brcmf_sdio_txpkt_postp - packet post processing for transmit
2177 * @bus: brcmf_sdio structure pointer
2178 * @pktq: packet list pointer
2179 *
2180 * Processes to be applied to the packet
2181 * - Remove head padding
2182 * - Remove tail padding
2183 */
2184static void
2185brcmf_sdio_txpkt_postp(struct brcmf_sdio *bus, struct sk_buff_head *pktq)
2186{
2187 u8 *hdr;
2188 u32 dat_offset;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002189 u16 tail_pad;
Arend van Spriel5aa9f0e2014-02-25 20:30:27 +01002190 u16 dummy_flags, chop_len;
Franky Linb05e9252013-08-10 12:27:26 +02002191 struct sk_buff *pkt_next, *tmp, *pkt_prev;
2192
2193 skb_queue_walk_safe(pktq, pkt_next, tmp) {
Arend van Spriel5aa9f0e2014-02-25 20:30:27 +01002194 dummy_flags = *(u16 *)(pkt_next->cb);
Franky Lin5491c112013-09-25 13:05:43 +02002195 if (dummy_flags & ALIGN_SKB_FLAG) {
2196 chop_len = dummy_flags & ALIGN_SKB_CHOP_LEN_MASK;
Franky Linb05e9252013-08-10 12:27:26 +02002197 if (chop_len) {
2198 pkt_prev = pkt_next->prev;
Franky Linb05e9252013-08-10 12:27:26 +02002199 skb_put(pkt_prev, chop_len);
2200 }
2201 __skb_unlink(pkt_next, pktq);
2202 brcmu_pkt_buf_free_skb(pkt_next);
2203 } else {
Franky Lin8da9d2c2013-11-29 12:25:10 +01002204 hdr = pkt_next->data + bus->tx_hdrlen - SDPCM_SWHDR_LEN;
Franky Linb05e9252013-08-10 12:27:26 +02002205 dat_offset = le32_to_cpu(*(__le32 *)hdr);
2206 dat_offset = (dat_offset & SDPCM_DOFFSET_MASK) >>
2207 SDPCM_DOFFSET_SHIFT;
2208 skb_pull(pkt_next, dat_offset);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002209 if (bus->txglom) {
2210 tail_pad = le16_to_cpu(*(__le16 *)(hdr - 2));
2211 skb_trim(pkt_next, pkt_next->len - tail_pad);
2212 }
Franky Linb05e9252013-08-10 12:27:26 +02002213 }
2214 }
2215}
2216
Arend van Spriel5b435de2011-10-05 13:19:03 +02002217/* Writes a HW/SW header into the packet and sends it. */
2218/* Assumes: (a) header space already there, (b) caller holds lock */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002219static int brcmf_sdio_txpkt(struct brcmf_sdio *bus, struct sk_buff_head *pktq,
2220 uint chan)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002221{
2222 int ret;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002223 struct sk_buff *pkt_next, *tmp;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002224
2225 brcmf_dbg(TRACE, "Enter\n");
2226
Franky Lin8da9d2c2013-11-29 12:25:10 +01002227 ret = brcmf_sdio_txpkt_prep(bus, pktq, chan);
Franky Linb05e9252013-08-10 12:27:26 +02002228 if (ret)
2229 goto done;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002230
Franky Lin38b0b0d2012-11-05 16:22:24 -08002231 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriela7cdd822013-12-12 11:58:59 +01002232 ret = brcmf_sdiod_send_pkt(bus->sdiodev, pktq);
Arend van Spriel80969832012-06-09 22:51:44 +02002233 bus->sdcnt.f2txdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002234
Hante Meuleman81c78832014-02-25 20:30:35 +01002235 if (ret < 0)
2236 brcmf_sdio_txfail(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002237
Franky Lin38b0b0d2012-11-05 16:22:24 -08002238 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002239
2240done:
Franky Lin8da9d2c2013-11-29 12:25:10 +01002241 brcmf_sdio_txpkt_postp(bus, pktq);
2242 if (ret == 0)
2243 bus->tx_seq = (bus->tx_seq + pktq->qlen) % SDPCM_SEQ_WRAP;
2244 skb_queue_walk_safe(pktq, pkt_next, tmp) {
2245 __skb_unlink(pkt_next, pktq);
2246 brcmf_txcomplete(bus->sdiodev->dev, pkt_next, ret == 0);
2247 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002248 return ret;
2249}
2250
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002251static uint brcmf_sdio_sendfromq(struct brcmf_sdio *bus, uint maxframes)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002252{
2253 struct sk_buff *pkt;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002254 struct sk_buff_head pktq;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002255 u32 intstatus = 0;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002256 int ret = 0, prec_out, i;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002257 uint cnt = 0;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002258 u8 tx_prec_map, pkt_num;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002259
Arend van Spriel5b435de2011-10-05 13:19:03 +02002260 brcmf_dbg(TRACE, "Enter\n");
2261
2262 tx_prec_map = ~bus->flowcontrol;
2263
2264 /* Send frames until the limit or some other event */
Franky Lin8da9d2c2013-11-29 12:25:10 +01002265 for (cnt = 0; (cnt < maxframes) && data_ok(bus);) {
2266 pkt_num = 1;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002267 if (bus->txglom)
2268 pkt_num = min_t(u8, bus->tx_max - bus->tx_seq,
Arend van Sprielaf1fa212014-02-27 19:25:00 +01002269 bus->sdiodev->txglomsz);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002270 pkt_num = min_t(u32, pkt_num,
2271 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol));
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002272 __skb_queue_head_init(&pktq);
2273 spin_lock_bh(&bus->txq_lock);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002274 for (i = 0; i < pkt_num; i++) {
2275 pkt = brcmu_pktq_mdeq(&bus->txq, tx_prec_map,
2276 &prec_out);
2277 if (pkt == NULL)
2278 break;
2279 __skb_queue_tail(&pktq, pkt);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002280 }
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002281 spin_unlock_bh(&bus->txq_lock);
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002282 if (i == 0)
Franky Lin8da9d2c2013-11-29 12:25:10 +01002283 break;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002284
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002285 ret = brcmf_sdio_txpkt(bus, &pktq, SDPCM_DATA_CHANNEL);
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002286
Franky Lin8da9d2c2013-11-29 12:25:10 +01002287 cnt += i;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002288
2289 /* In poll mode, need to check for other events */
Hante Meulemanb6a8cf22014-02-25 20:30:33 +01002290 if (!bus->intr) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02002291 /* Check device status, signal pending interrupt */
Franky Lin38b0b0d2012-11-05 16:22:24 -08002292 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin5c15c232012-05-04 18:27:37 -07002293 ret = r_sdreg32(bus, &intstatus,
2294 offsetof(struct sdpcmd_regs,
2295 intstatus));
Franky Lin38b0b0d2012-11-05 16:22:24 -08002296 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel80969832012-06-09 22:51:44 +02002297 bus->sdcnt.f2txdata++;
Franky Lin5c15c232012-05-04 18:27:37 -07002298 if (ret != 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002299 break;
2300 if (intstatus & bus->hostintmask)
Franky Lin1d382272012-09-13 21:11:59 +02002301 atomic_set(&bus->ipend, 1);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002302 }
2303 }
2304
2305 /* Deflow-control stack if needed */
Arend van Spriela1ce7a02015-02-06 18:36:42 +01002306 if ((bus->sdiodev->state == BRCMF_SDIOD_DATA) &&
Franky Linc8bf3482011-12-16 18:37:08 -08002307 bus->txoff && (pktq_len(&bus->txq) < TXLOW)) {
Hante Meuleman90d03ff2012-09-11 21:18:46 +02002308 bus->txoff = false;
2309 brcmf_txflowblock(bus->sdiodev->dev, false);
Franky Linc8bf3482011-12-16 18:37:08 -08002310 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002311
2312 return cnt;
2313}
2314
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002315static int brcmf_sdio_tx_ctrlframe(struct brcmf_sdio *bus, u8 *frame, u16 len)
2316{
2317 u8 doff;
2318 u16 pad;
2319 uint retries = 0;
2320 struct brcmf_sdio_hdrinfo hd_info = {0};
2321 int ret;
2322
2323 brcmf_dbg(TRACE, "Enter\n");
2324
2325 /* Back the pointer to make room for bus header */
2326 frame -= bus->tx_hdrlen;
2327 len += bus->tx_hdrlen;
2328
2329 /* Add alignment padding (optional for ctl frames) */
2330 doff = ((unsigned long)frame % bus->head_align);
2331 if (doff) {
2332 frame -= doff;
2333 len += doff;
2334 memset(frame + bus->tx_hdrlen, 0, doff);
2335 }
2336
2337 /* Round send length to next SDIO block */
2338 pad = 0;
2339 if (bus->roundup && bus->blocksize && (len > bus->blocksize)) {
2340 pad = bus->blocksize - (len % bus->blocksize);
2341 if ((pad > bus->roundup) || (pad >= bus->blocksize))
2342 pad = 0;
2343 } else if (len % bus->head_align) {
2344 pad = bus->head_align - (len % bus->head_align);
2345 }
2346 len += pad;
2347
2348 hd_info.len = len - pad;
2349 hd_info.channel = SDPCM_CONTROL_CHANNEL;
2350 hd_info.dat_offset = doff + bus->tx_hdrlen;
2351 hd_info.seq_num = bus->tx_seq;
2352 hd_info.lastfrm = true;
2353 hd_info.tail_pad = pad;
2354 brcmf_sdio_hdpack(bus, frame, &hd_info);
2355
2356 if (bus->txglom)
2357 brcmf_sdio_update_hwhdr(frame, len);
2358
2359 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_CTL_ON(),
2360 frame, len, "Tx Frame:\n");
2361 brcmf_dbg_hex_dump(!(BRCMF_BYTES_ON() && BRCMF_CTL_ON()) &&
2362 BRCMF_HDRS_ON(),
2363 frame, min_t(u16, len, 16), "TxHdr:\n");
2364
2365 do {
2366 ret = brcmf_sdiod_send_buf(bus->sdiodev, frame, len);
2367
2368 if (ret < 0)
2369 brcmf_sdio_txfail(bus);
2370 else
2371 bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQ_WRAP;
2372 } while (ret < 0 && retries++ < TXRETRIES);
2373
2374 return ret;
2375}
2376
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002377static void brcmf_sdio_bus_stop(struct device *dev)
Franky Lina9ffda82011-12-16 18:37:12 -08002378{
2379 u32 local_hostintmask;
2380 u8 saveclk;
Franky Lina9ffda82011-12-16 18:37:12 -08002381 int err;
2382 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01002383 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Lina9ffda82011-12-16 18:37:12 -08002384 struct brcmf_sdio *bus = sdiodev->bus;
2385
2386 brcmf_dbg(TRACE, "Enter\n");
2387
2388 if (bus->watchdog_tsk) {
2389 send_sig(SIGTERM, bus->watchdog_tsk, 1);
2390 kthread_stop(bus->watchdog_tsk);
2391 bus->watchdog_tsk = NULL;
2392 }
2393
Arend van Spriela1ce7a02015-02-06 18:36:42 +01002394 if (sdiodev->state != BRCMF_SDIOD_NOMEDIUM) {
Arend van Sprielbb350712014-01-13 22:20:29 +01002395 sdio_claim_host(sdiodev->func[1]);
Franky Lina9ffda82011-12-16 18:37:12 -08002396
Arend van Sprielbb350712014-01-13 22:20:29 +01002397 /* Enable clock for device interrupts */
2398 brcmf_sdio_bus_sleep(bus, false, false);
Franky Lina9ffda82011-12-16 18:37:12 -08002399
Arend van Sprielbb350712014-01-13 22:20:29 +01002400 /* Disable and clear interrupts at the chip level also */
2401 w_sdreg32(bus, 0, offsetof(struct sdpcmd_regs, hostintmask));
2402 local_hostintmask = bus->hostintmask;
2403 bus->hostintmask = 0;
Franky Lina9ffda82011-12-16 18:37:12 -08002404
Arend van Sprielbb350712014-01-13 22:20:29 +01002405 /* Force backplane clocks to assure F2 interrupt propagates */
2406 saveclk = brcmf_sdiod_regrb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
2407 &err);
2408 if (!err)
2409 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
2410 (saveclk | SBSDIO_FORCE_HT), &err);
2411 if (err)
2412 brcmf_err("Failed to force clock for F2: err %d\n",
2413 err);
Franky Lina9ffda82011-12-16 18:37:12 -08002414
Arend van Sprielbb350712014-01-13 22:20:29 +01002415 /* Turn off the bus (F2), free any pending packets */
2416 brcmf_dbg(INTR, "disable SDIO interrupts\n");
2417 sdio_disable_func(sdiodev->func[SDIO_FUNC_2]);
2418
2419 /* Clear any pending interrupts now that F2 is disabled */
2420 w_sdreg32(bus, local_hostintmask,
2421 offsetof(struct sdpcmd_regs, intstatus));
2422
2423 sdio_release_host(sdiodev->func[1]);
Franky Lina9ffda82011-12-16 18:37:12 -08002424 }
Franky Lina9ffda82011-12-16 18:37:12 -08002425 /* Clear the data packet queues */
2426 brcmu_pktq_flush(&bus->txq, true, NULL, NULL);
2427
2428 /* Clear any held glomming stuff */
Markus Elfring297540f2015-02-04 20:28:49 +01002429 brcmu_pkt_buf_free_skb(bus->glomd);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002430 brcmf_sdio_free_glom(bus);
Franky Lina9ffda82011-12-16 18:37:12 -08002431
2432 /* Clear rx control and wake any waiters */
Franky Lindd43a012012-11-05 16:22:22 -08002433 spin_lock_bh(&bus->rxctl_lock);
Franky Lina9ffda82011-12-16 18:37:12 -08002434 bus->rxlen = 0;
Franky Lindd43a012012-11-05 16:22:22 -08002435 spin_unlock_bh(&bus->rxctl_lock);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002436 brcmf_sdio_dcmd_resp_wake(bus);
Franky Lina9ffda82011-12-16 18:37:12 -08002437
2438 /* Reset some F2 state stuff */
2439 bus->rxskip = false;
2440 bus->tx_seq = bus->rx_seq = 0;
Franky Lina9ffda82011-12-16 18:37:12 -08002441}
2442
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002443static inline void brcmf_sdio_clrintr(struct brcmf_sdio *bus)
Franky Linba89bf12012-04-27 18:56:59 -07002444{
2445 unsigned long flags;
2446
Hante Meuleman668761a2013-04-12 10:55:55 +02002447 if (bus->sdiodev->oob_irq_requested) {
2448 spin_lock_irqsave(&bus->sdiodev->irq_en_lock, flags);
2449 if (!bus->sdiodev->irq_en && !atomic_read(&bus->ipend)) {
2450 enable_irq(bus->sdiodev->pdata->oob_irq_nr);
2451 bus->sdiodev->irq_en = true;
2452 }
2453 spin_unlock_irqrestore(&bus->sdiodev->irq_en_lock, flags);
Franky Linba89bf12012-04-27 18:56:59 -07002454 }
Franky Linba89bf12012-04-27 18:56:59 -07002455}
Franky Linba89bf12012-04-27 18:56:59 -07002456
Franky Lin45316032012-09-13 21:12:03 +02002457static int brcmf_sdio_intr_rstatus(struct brcmf_sdio *bus)
2458{
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01002459 struct brcmf_core *buscore;
Franky Lin45316032012-09-13 21:12:03 +02002460 u32 addr;
2461 unsigned long val;
Hante Meuleman5cbb9c22014-03-06 10:16:12 +01002462 int ret;
Franky Lin45316032012-09-13 21:12:03 +02002463
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01002464 buscore = brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV);
2465 addr = buscore->base + offsetof(struct sdpcmd_regs, intstatus);
Franky Lin45316032012-09-13 21:12:03 +02002466
Arend van Spriela39be272013-12-12 11:58:58 +01002467 val = brcmf_sdiod_regrl(bus->sdiodev, addr, &ret);
Franky Lin45316032012-09-13 21:12:03 +02002468 bus->sdcnt.f1regdata++;
2469 if (ret != 0)
Hante Meuleman5cbb9c22014-03-06 10:16:12 +01002470 return ret;
Franky Lin45316032012-09-13 21:12:03 +02002471
2472 val &= bus->hostintmask;
2473 atomic_set(&bus->fcstate, !!(val & I_HMB_FC_STATE));
2474
2475 /* Clear interrupts */
2476 if (val) {
Arend van Spriela39be272013-12-12 11:58:58 +01002477 brcmf_sdiod_regwl(bus->sdiodev, addr, val, &ret);
Franky Lin45316032012-09-13 21:12:03 +02002478 bus->sdcnt.f1regdata++;
Vineet Guptad3928d02015-07-09 13:43:18 +05302479 atomic_or(val, &bus->intstatus);
Franky Lin45316032012-09-13 21:12:03 +02002480 }
2481
2482 return ret;
2483}
2484
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002485static void brcmf_sdio_dpc(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002486{
Franky Lin45316032012-09-13 21:12:03 +02002487 u32 newstatus = 0;
2488 unsigned long intstatus;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002489 uint txlimit = bus->txbound; /* Tx frames to send before resched */
Hante Meulemanb6a8cf22014-02-25 20:30:33 +01002490 uint framecnt; /* Temporary counter of tx/rx frames */
Hante Meuleman5cbb9c22014-03-06 10:16:12 +01002491 int err = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002492
2493 brcmf_dbg(TRACE, "Enter\n");
2494
Franky Lin38b0b0d2012-11-05 16:22:24 -08002495 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002496
2497 /* If waiting for HTAVAIL, check status */
Piotr Haber4a3da992013-04-11 13:28:46 +02002498 if (!bus->sr_enabled && bus->clkstate == CLK_PENDING) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02002499 u8 clkctl, devctl = 0;
2500
Joe Perches8ae74652012-01-15 00:38:38 -08002501#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02002502 /* Check for inconsistent device control */
Arend van Spriela39be272013-12-12 11:58:58 +01002503 devctl = brcmf_sdiod_regrb(bus->sdiodev,
2504 SBSDIO_DEVICE_CTL, &err);
Joe Perches8ae74652012-01-15 00:38:38 -08002505#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02002506
2507 /* Read CSR, if clock on switch to AVAIL, else ignore */
Arend van Spriela39be272013-12-12 11:58:58 +01002508 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
2509 SBSDIO_FUNC1_CHIPCLKCSR, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002510
Arend van Sprielc3203372013-04-03 12:40:44 +02002511 brcmf_dbg(SDIO, "DPC: PENDING, devctl 0x%02x clkctl 0x%02x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02002512 devctl, clkctl);
2513
2514 if (SBSDIO_HTAV(clkctl)) {
Arend van Spriela39be272013-12-12 11:58:58 +01002515 devctl = brcmf_sdiod_regrb(bus->sdiodev,
2516 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002517 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
Arend van Spriela39be272013-12-12 11:58:58 +01002518 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
2519 devctl, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002520 bus->clkstate = CLK_AVAIL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002521 }
2522 }
2523
Arend van Spriel5b435de2011-10-05 13:19:03 +02002524 /* Make sure backplane clock is on */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002525 brcmf_sdio_bus_sleep(bus, false, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002526
2527 /* Pending interrupt indicates new device status */
Franky Lin1d382272012-09-13 21:11:59 +02002528 if (atomic_read(&bus->ipend) > 0) {
2529 atomic_set(&bus->ipend, 0);
Franky Lin45316032012-09-13 21:12:03 +02002530 err = brcmf_sdio_intr_rstatus(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002531 }
2532
Franky Lin45316032012-09-13 21:12:03 +02002533 /* Start with leftover status bits */
2534 intstatus = atomic_xchg(&bus->intstatus, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002535
2536 /* Handle flow-control change: read new state in case our ack
2537 * crossed another change interrupt. If change still set, assume
2538 * FC ON for safety, let next loop through do the debounce.
2539 */
2540 if (intstatus & I_HMB_FC_CHANGE) {
2541 intstatus &= ~I_HMB_FC_CHANGE;
Franky Lin5c15c232012-05-04 18:27:37 -07002542 err = w_sdreg32(bus, I_HMB_FC_CHANGE,
2543 offsetof(struct sdpcmd_regs, intstatus));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002544
Franky Lin5c15c232012-05-04 18:27:37 -07002545 err = r_sdreg32(bus, &newstatus,
2546 offsetof(struct sdpcmd_regs, intstatus));
Arend van Spriel80969832012-06-09 22:51:44 +02002547 bus->sdcnt.f1regdata += 2;
Franky Lin45316032012-09-13 21:12:03 +02002548 atomic_set(&bus->fcstate,
2549 !!(newstatus & (I_HMB_FC_STATE | I_HMB_FC_CHANGE)));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002550 intstatus |= (newstatus & bus->hostintmask);
2551 }
2552
2553 /* Handle host mailbox indication */
2554 if (intstatus & I_HMB_HOST_INT) {
2555 intstatus &= ~I_HMB_HOST_INT;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002556 intstatus |= brcmf_sdio_hostmail(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002557 }
2558
Franky Lin38b0b0d2012-11-05 16:22:24 -08002559 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin7cdf57d2012-11-05 16:22:23 -08002560
Arend van Spriel5b435de2011-10-05 13:19:03 +02002561 /* Generally don't ask for these, can get CRC errors... */
2562 if (intstatus & I_WR_OOSYNC) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002563 brcmf_err("Dongle reports WR_OOSYNC\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002564 intstatus &= ~I_WR_OOSYNC;
2565 }
2566
2567 if (intstatus & I_RD_OOSYNC) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002568 brcmf_err("Dongle reports RD_OOSYNC\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002569 intstatus &= ~I_RD_OOSYNC;
2570 }
2571
2572 if (intstatus & I_SBINT) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002573 brcmf_err("Dongle reports SBINT\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002574 intstatus &= ~I_SBINT;
2575 }
2576
2577 /* Would be active due to wake-wlan in gSPI */
2578 if (intstatus & I_CHIPACTIVE) {
2579 brcmf_dbg(INFO, "Dongle reports CHIPACTIVE\n");
2580 intstatus &= ~I_CHIPACTIVE;
2581 }
2582
2583 /* Ignore frame indications if rxskip is set */
2584 if (bus->rxskip)
2585 intstatus &= ~I_HMB_FRAME_IND;
2586
2587 /* On frame indication, read available frames */
Hante Meulemanb6a8cf22014-02-25 20:30:33 +01002588 if ((intstatus & I_HMB_FRAME_IND) && (bus->clkstate == CLK_AVAIL)) {
2589 brcmf_sdio_readframes(bus, bus->rxbound);
Franky Lin4754fce2012-09-19 22:21:11 +02002590 if (!bus->rxpending)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002591 intstatus &= ~I_HMB_FRAME_IND;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002592 }
2593
2594 /* Keep still-pending events for next scheduling */
Hante Meuleman5cbb9c22014-03-06 10:16:12 +01002595 if (intstatus)
Vineet Guptad3928d02015-07-09 13:43:18 +05302596 atomic_or(intstatus, &bus->intstatus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002597
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002598 brcmf_sdio_clrintr(bus);
Franky Linba89bf12012-04-27 18:56:59 -07002599
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002600 if (bus->ctrl_frame_stat && (bus->clkstate == CLK_AVAIL) &&
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002601 data_ok(bus)) {
2602 sdio_claim_host(bus->sdiodev->func[1]);
Hante Meuleman449e58b2015-03-06 18:40:40 +01002603 if (bus->ctrl_frame_stat) {
2604 err = brcmf_sdio_tx_ctrlframe(bus, bus->ctrl_frame_buf,
2605 bus->ctrl_frame_len);
2606 bus->ctrl_frame_err = err;
Hante Meuleman2c64e162015-03-18 13:25:22 +01002607 wmb();
Hante Meuleman449e58b2015-03-06 18:40:40 +01002608 bus->ctrl_frame_stat = false;
2609 }
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002610 sdio_release_host(bus->sdiodev->func[1]);
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002611 brcmf_sdio_wait_event_wakeup(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002612 }
2613 /* Send queued frames (limit 1 if rx may still be pending) */
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002614 if ((bus->clkstate == CLK_AVAIL) && !atomic_read(&bus->fcstate) &&
2615 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) && txlimit &&
2616 data_ok(bus)) {
Franky Lin4754fce2012-09-19 22:21:11 +02002617 framecnt = bus->rxpending ? min(txlimit, bus->txminmax) :
2618 txlimit;
Hante Meulemanb6a8cf22014-02-25 20:30:33 +01002619 brcmf_sdio_sendfromq(bus, framecnt);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002620 }
2621
Arend van Spriela1ce7a02015-02-06 18:36:42 +01002622 if ((bus->sdiodev->state != BRCMF_SDIOD_DATA) || (err != 0)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002623 brcmf_err("failed backplane access over SDIO, halting operation\n");
Franky Lin45316032012-09-13 21:12:03 +02002624 atomic_set(&bus->intstatus, 0);
Hante Meulemande6878c2015-03-06 18:40:38 +01002625 if (bus->ctrl_frame_stat) {
Hante Meuleman449e58b2015-03-06 18:40:40 +01002626 sdio_claim_host(bus->sdiodev->func[1]);
2627 if (bus->ctrl_frame_stat) {
2628 bus->ctrl_frame_err = -ENODEV;
Hante Meuleman2c64e162015-03-18 13:25:22 +01002629 wmb();
Hante Meuleman449e58b2015-03-06 18:40:40 +01002630 bus->ctrl_frame_stat = false;
2631 brcmf_sdio_wait_event_wakeup(bus);
2632 }
2633 sdio_release_host(bus->sdiodev->func[1]);
Hante Meulemande6878c2015-03-06 18:40:38 +01002634 }
Franky Lin45316032012-09-13 21:12:03 +02002635 } else if (atomic_read(&bus->intstatus) ||
2636 atomic_read(&bus->ipend) > 0 ||
2637 (!atomic_read(&bus->fcstate) &&
2638 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) &&
Hante Meulemanb6a8cf22014-02-25 20:30:33 +01002639 data_ok(bus))) {
Hante Meuleman2c64e162015-03-18 13:25:22 +01002640 bus->dpc_triggered = true;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002641 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002642}
2643
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002644static struct pktq *brcmf_sdio_bus_gettxq(struct device *dev)
Arend van Spriele2432b62013-04-03 12:40:38 +02002645{
2646 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
2647 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
2648 struct brcmf_sdio *bus = sdiodev->bus;
2649
2650 return &bus->txq;
2651}
2652
Hante Meuleman84936622014-10-28 14:56:19 +01002653static bool brcmf_sdio_prec_enq(struct pktq *q, struct sk_buff *pkt, int prec)
2654{
2655 struct sk_buff *p;
2656 int eprec = -1; /* precedence to evict from */
2657
2658 /* Fast case, precedence queue is not full and we are also not
2659 * exceeding total queue length
2660 */
2661 if (!pktq_pfull(q, prec) && !pktq_full(q)) {
2662 brcmu_pktq_penq(q, prec, pkt);
2663 return true;
2664 }
2665
2666 /* Determine precedence from which to evict packet, if any */
2667 if (pktq_pfull(q, prec)) {
2668 eprec = prec;
2669 } else if (pktq_full(q)) {
2670 p = brcmu_pktq_peek_tail(q, &eprec);
2671 if (eprec > prec)
2672 return false;
2673 }
2674
2675 /* Evict if needed */
2676 if (eprec >= 0) {
2677 /* Detect queueing to unconfigured precedence */
2678 if (eprec == prec)
2679 return false; /* refuse newer (incoming) packet */
2680 /* Evict packet according to discard policy */
2681 p = brcmu_pktq_pdeq_tail(q, eprec);
2682 if (p == NULL)
2683 brcmf_err("brcmu_pktq_pdeq_tail() failed\n");
2684 brcmu_pkt_buf_free_skb(p);
2685 }
2686
2687 /* Enqueue */
2688 p = brcmu_pktq_penq(q, prec, pkt);
2689 if (p == NULL)
2690 brcmf_err("brcmu_pktq_penq() failed\n");
2691
2692 return p != NULL;
2693}
2694
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002695static int brcmf_sdio_bus_txdata(struct device *dev, struct sk_buff *pkt)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002696{
2697 int ret = -EBADE;
Arend van Spriel44ff5662014-02-25 20:30:28 +01002698 uint prec;
Franky Linbf347bb2011-11-22 17:21:56 -08002699 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01002700 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Linbf347bb2011-11-22 17:21:56 -08002701 struct brcmf_sdio *bus = sdiodev->bus;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002702
Arend van Spriel44ff5662014-02-25 20:30:28 +01002703 brcmf_dbg(TRACE, "Enter: pkt: data %p len %d\n", pkt->data, pkt->len);
Arend van Spriel5768f312015-06-11 00:12:20 +02002704 if (sdiodev->state != BRCMF_SDIOD_DATA)
2705 return -EIO;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002706
2707 /* Add space for the header */
Franky Lin706478c2013-08-10 12:27:29 +02002708 skb_push(pkt, bus->tx_hdrlen);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002709 /* precondition: IS_ALIGNED((unsigned long)(pkt->data), 2) */
2710
2711 prec = prio2prec((pkt->priority & PRIOMASK));
2712
2713 /* Check for existing queue, current flow-control,
2714 pending event, or pending clock */
2715 brcmf_dbg(TRACE, "deferring pktq len %d\n", pktq_len(&bus->txq));
Arend van Spriel80969832012-06-09 22:51:44 +02002716 bus->sdcnt.fcqueued++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002717
2718 /* Priority based enq */
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002719 spin_lock_bh(&bus->txq_lock);
Arend van Spriel5aa9f0e2014-02-25 20:30:27 +01002720 /* reset bus_flags in packet cb */
2721 *(u16 *)(pkt->cb) = 0;
Hante Meuleman84936622014-10-28 14:56:19 +01002722 if (!brcmf_sdio_prec_enq(&bus->txq, pkt, prec)) {
Franky Lin706478c2013-08-10 12:27:29 +02002723 skb_pull(pkt, bus->tx_hdrlen);
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002724 brcmf_err("out of bus->txq !!!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002725 ret = -ENOSR;
2726 } else {
2727 ret = 0;
2728 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002729
Franky Linc8bf3482011-12-16 18:37:08 -08002730 if (pktq_len(&bus->txq) >= TXHI) {
Hante Meuleman90d03ff2012-09-11 21:18:46 +02002731 bus->txoff = true;
Hante Meuleman84936622014-10-28 14:56:19 +01002732 brcmf_txflowblock(dev, true);
Franky Linc8bf3482011-12-16 18:37:08 -08002733 }
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002734 spin_unlock_bh(&bus->txq_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002735
Joe Perches8ae74652012-01-15 00:38:38 -08002736#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02002737 if (pktq_plen(&bus->txq, prec) > qcount[prec])
2738 qcount[prec] = pktq_plen(&bus->txq, prec);
2739#endif
Franky Linf1e68c22012-09-13 21:12:00 +02002740
Arend van Spriel99824642015-02-06 18:36:45 +01002741 brcmf_sdio_trigger_dpc(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002742 return ret;
2743}
2744
Joe Perches8ae74652012-01-15 00:38:38 -08002745#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02002746#define CONSOLE_LINE_MAX 192
2747
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002748static int brcmf_sdio_readconsole(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002749{
2750 struct brcmf_console *c = &bus->console;
2751 u8 line[CONSOLE_LINE_MAX], ch;
2752 u32 n, idx, addr;
2753 int rv;
2754
2755 /* Don't do anything until FWREADY updates console address */
2756 if (bus->console_addr == 0)
2757 return 0;
2758
2759 /* Read console log struct */
2760 addr = bus->console_addr + offsetof(struct rte_console, log_le);
Arend van Spriela39be272013-12-12 11:58:58 +01002761 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, (u8 *)&c->log_le,
2762 sizeof(c->log_le));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002763 if (rv < 0)
2764 return rv;
2765
2766 /* Allocate console buffer (one time only) */
2767 if (c->buf == NULL) {
2768 c->bufsize = le32_to_cpu(c->log_le.buf_size);
2769 c->buf = kmalloc(c->bufsize, GFP_ATOMIC);
2770 if (c->buf == NULL)
2771 return -ENOMEM;
2772 }
2773
2774 idx = le32_to_cpu(c->log_le.idx);
2775
2776 /* Protect against corrupt value */
2777 if (idx > c->bufsize)
2778 return -EBADE;
2779
2780 /* Skip reading the console buffer if the index pointer
2781 has not moved */
2782 if (idx == c->last)
2783 return 0;
2784
2785 /* Read the console buffer */
2786 addr = le32_to_cpu(c->log_le.buf);
Arend van Spriela39be272013-12-12 11:58:58 +01002787 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, c->buf, c->bufsize);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002788 if (rv < 0)
2789 return rv;
2790
2791 while (c->last != idx) {
2792 for (n = 0; n < CONSOLE_LINE_MAX - 2; n++) {
2793 if (c->last == idx) {
2794 /* This would output a partial line.
2795 * Instead, back up
2796 * the buffer pointer and output this
2797 * line next time around.
2798 */
2799 if (c->last >= n)
2800 c->last -= n;
2801 else
2802 c->last = c->bufsize - n;
2803 goto break2;
2804 }
2805 ch = c->buf[c->last];
2806 c->last = (c->last + 1) % c->bufsize;
2807 if (ch == '\n')
2808 break;
2809 line[n] = ch;
2810 }
2811
2812 if (n > 0) {
2813 if (line[n - 1] == '\r')
2814 n--;
2815 line[n] = 0;
Joe Perches18aad4f2012-01-15 00:38:42 -08002816 pr_debug("CONSOLE: %s\n", line);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002817 }
2818 }
2819break2:
2820
2821 return 0;
2822}
Joe Perches8ae74652012-01-15 00:38:38 -08002823#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02002824
Franky Linfcf094f2011-12-16 18:37:16 -08002825static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002826brcmf_sdio_bus_txctl(struct device *dev, unsigned char *msg, uint msglen)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002827{
Franky Lin47a1ce72011-11-22 17:21:55 -08002828 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01002829 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Lin47a1ce72011-11-22 17:21:55 -08002830 struct brcmf_sdio *bus = sdiodev->bus;
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002831 int ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002832
2833 brcmf_dbg(TRACE, "Enter\n");
Arend van Spriel5768f312015-06-11 00:12:20 +02002834 if (sdiodev->state != BRCMF_SDIOD_DATA)
2835 return -EIO;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002836
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002837 /* Send from dpc */
2838 bus->ctrl_frame_buf = msg;
2839 bus->ctrl_frame_len = msglen;
Hante Meuleman2c64e162015-03-18 13:25:22 +01002840 wmb();
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002841 bus->ctrl_frame_stat = true;
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002842
Arend van Spriel99824642015-02-06 18:36:45 +01002843 brcmf_sdio_trigger_dpc(bus);
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002844 wait_event_interruptible_timeout(bus->ctrl_wait, !bus->ctrl_frame_stat,
Arend van Spriel63ce3d52016-01-05 11:05:48 +01002845 CTL_DONE_TIMEOUT);
Hante Meuleman449e58b2015-03-06 18:40:40 +01002846 ret = 0;
2847 if (bus->ctrl_frame_stat) {
2848 sdio_claim_host(bus->sdiodev->func[1]);
2849 if (bus->ctrl_frame_stat) {
2850 brcmf_dbg(SDIO, "ctrl_frame timeout\n");
2851 bus->ctrl_frame_stat = false;
2852 ret = -ETIMEDOUT;
2853 }
2854 sdio_release_host(bus->sdiodev->func[1]);
2855 }
2856 if (!ret) {
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002857 brcmf_dbg(SDIO, "ctrl_frame complete, err=%d\n",
2858 bus->ctrl_frame_err);
Hante Meuleman2c64e162015-03-18 13:25:22 +01002859 rmb();
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002860 ret = bus->ctrl_frame_err;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002861 }
2862
Arend van Spriel5b435de2011-10-05 13:19:03 +02002863 if (ret)
Arend van Spriel80969832012-06-09 22:51:44 +02002864 bus->sdcnt.tx_ctlerrs++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002865 else
Arend van Spriel80969832012-06-09 22:51:44 +02002866 bus->sdcnt.tx_ctlpkts++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002867
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002868 return ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002869}
2870
Arend van Spriel80969832012-06-09 22:51:44 +02002871#ifdef DEBUG
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002872static int brcmf_sdio_dump_console(struct seq_file *seq, struct brcmf_sdio *bus,
2873 struct sdpcm_shared *sh)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002874{
2875 u32 addr, console_ptr, console_size, console_index;
2876 char *conbuf = NULL;
2877 __le32 sh_val;
2878 int rv;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002879
2880 /* obtain console information from device memory */
2881 addr = sh->console_addr + offsetof(struct rte_console, log_le);
Arend van Spriela39be272013-12-12 11:58:58 +01002882 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
2883 (u8 *)&sh_val, sizeof(u32));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002884 if (rv < 0)
2885 return rv;
2886 console_ptr = le32_to_cpu(sh_val);
2887
2888 addr = sh->console_addr + offsetof(struct rte_console, log_le.buf_size);
Arend van Spriela39be272013-12-12 11:58:58 +01002889 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
2890 (u8 *)&sh_val, sizeof(u32));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002891 if (rv < 0)
2892 return rv;
2893 console_size = le32_to_cpu(sh_val);
2894
2895 addr = sh->console_addr + offsetof(struct rte_console, log_le.idx);
Arend van Spriela39be272013-12-12 11:58:58 +01002896 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
2897 (u8 *)&sh_val, sizeof(u32));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002898 if (rv < 0)
2899 return rv;
2900 console_index = le32_to_cpu(sh_val);
2901
2902 /* allocate buffer for console data */
2903 if (console_size <= CONSOLE_BUFFER_MAX)
2904 conbuf = vzalloc(console_size+1);
2905
2906 if (!conbuf)
2907 return -ENOMEM;
2908
2909 /* obtain the console data from device */
2910 conbuf[console_size] = '\0';
Arend van Spriela39be272013-12-12 11:58:58 +01002911 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, console_ptr, (u8 *)conbuf,
2912 console_size);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002913 if (rv < 0)
2914 goto done;
2915
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002916 rv = seq_write(seq, conbuf + console_index,
2917 console_size - console_index);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002918 if (rv < 0)
2919 goto done;
2920
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002921 if (console_index > 0)
2922 rv = seq_write(seq, conbuf, console_index - 1);
2923
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002924done:
2925 vfree(conbuf);
2926 return rv;
2927}
2928
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002929static int brcmf_sdio_trap_info(struct seq_file *seq, struct brcmf_sdio *bus,
2930 struct sdpcm_shared *sh)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002931{
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002932 int error;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002933 struct brcmf_trap_info tr;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002934
Piotr Haberbaa9e602013-04-05 10:57:42 +02002935 if ((sh->flags & SDPCM_SHARED_TRAP) == 0) {
2936 brcmf_dbg(INFO, "no trap in firmware\n");
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002937 return 0;
Piotr Haberbaa9e602013-04-05 10:57:42 +02002938 }
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002939
Arend van Spriela39be272013-12-12 11:58:58 +01002940 error = brcmf_sdiod_ramrw(bus->sdiodev, false, sh->trap_addr, (u8 *)&tr,
2941 sizeof(struct brcmf_trap_info));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002942 if (error < 0)
2943 return error;
2944
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002945 seq_printf(seq,
2946 "dongle trap info: type 0x%x @ epc 0x%08x\n"
2947 " cpsr 0x%08x spsr 0x%08x sp 0x%08x\n"
2948 " lr 0x%08x pc 0x%08x offset 0x%x\n"
2949 " r0 0x%08x r1 0x%08x r2 0x%08x r3 0x%08x\n"
2950 " r4 0x%08x r5 0x%08x r6 0x%08x r7 0x%08x\n",
2951 le32_to_cpu(tr.type), le32_to_cpu(tr.epc),
2952 le32_to_cpu(tr.cpsr), le32_to_cpu(tr.spsr),
2953 le32_to_cpu(tr.r13), le32_to_cpu(tr.r14),
2954 le32_to_cpu(tr.pc), sh->trap_addr,
2955 le32_to_cpu(tr.r0), le32_to_cpu(tr.r1),
2956 le32_to_cpu(tr.r2), le32_to_cpu(tr.r3),
2957 le32_to_cpu(tr.r4), le32_to_cpu(tr.r5),
2958 le32_to_cpu(tr.r6), le32_to_cpu(tr.r7));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002959
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002960 return 0;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002961}
2962
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002963static int brcmf_sdio_assert_info(struct seq_file *seq, struct brcmf_sdio *bus,
2964 struct sdpcm_shared *sh)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002965{
2966 int error = 0;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002967 char file[80] = "?";
2968 char expr[80] = "<???>";
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002969
2970 if ((sh->flags & SDPCM_SHARED_ASSERT_BUILT) == 0) {
2971 brcmf_dbg(INFO, "firmware not built with -assert\n");
2972 return 0;
2973 } else if ((sh->flags & SDPCM_SHARED_ASSERT) == 0) {
2974 brcmf_dbg(INFO, "no assert in dongle\n");
2975 return 0;
2976 }
2977
Franky Lin38b0b0d2012-11-05 16:22:24 -08002978 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002979 if (sh->assert_file_addr != 0) {
Arend van Spriela39be272013-12-12 11:58:58 +01002980 error = brcmf_sdiod_ramrw(bus->sdiodev, false,
2981 sh->assert_file_addr, (u8 *)file, 80);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002982 if (error < 0)
2983 return error;
2984 }
2985 if (sh->assert_exp_addr != 0) {
Arend van Spriela39be272013-12-12 11:58:58 +01002986 error = brcmf_sdiod_ramrw(bus->sdiodev, false,
2987 sh->assert_exp_addr, (u8 *)expr, 80);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002988 if (error < 0)
2989 return error;
2990 }
Franky Lin38b0b0d2012-11-05 16:22:24 -08002991 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002992
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002993 seq_printf(seq, "dongle assert: %s:%d: assert(%s)\n",
2994 file, sh->assert_line, expr);
2995 return 0;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002996}
2997
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002998static int brcmf_sdio_checkdied(struct brcmf_sdio *bus)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002999{
3000 int error;
3001 struct sdpcm_shared sh;
3002
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003003 error = brcmf_sdio_readshared(bus, &sh);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003004
3005 if (error < 0)
3006 return error;
3007
3008 if ((sh.flags & SDPCM_SHARED_ASSERT_BUILT) == 0)
3009 brcmf_dbg(INFO, "firmware not built with -assert\n");
3010 else if (sh.flags & SDPCM_SHARED_ASSERT)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003011 brcmf_err("assertion in dongle\n");
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003012
3013 if (sh.flags & SDPCM_SHARED_TRAP)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003014 brcmf_err("firmware trap in dongle\n");
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003015
3016 return 0;
3017}
3018
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003019static int brcmf_sdio_died_dump(struct seq_file *seq, struct brcmf_sdio *bus)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003020{
3021 int error = 0;
3022 struct sdpcm_shared sh;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003023
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003024 error = brcmf_sdio_readshared(bus, &sh);
3025 if (error < 0)
3026 goto done;
3027
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003028 error = brcmf_sdio_assert_info(seq, bus, &sh);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003029 if (error < 0)
3030 goto done;
Piotr Haberbaa9e602013-04-05 10:57:42 +02003031
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003032 error = brcmf_sdio_trap_info(seq, bus, &sh);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003033 if (error < 0)
3034 goto done;
3035
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003036 error = brcmf_sdio_dump_console(seq, bus, &sh);
Piotr Haberbaa9e602013-04-05 10:57:42 +02003037
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003038done:
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003039 return error;
3040}
3041
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003042static int brcmf_sdio_forensic_read(struct seq_file *seq, void *data)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003043{
Arend van Spriel82d957e2014-07-12 08:49:36 +02003044 struct brcmf_bus *bus_if = dev_get_drvdata(seq->private);
3045 struct brcmf_sdio *bus = bus_if->bus_priv.sdio->bus;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003046
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003047 return brcmf_sdio_died_dump(seq, bus);
3048}
3049
Arend van Spriel82d957e2014-07-12 08:49:36 +02003050static int brcmf_debugfs_sdio_count_read(struct seq_file *seq, void *data)
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003051{
Arend van Spriel82d957e2014-07-12 08:49:36 +02003052 struct brcmf_bus *bus_if = dev_get_drvdata(seq->private);
3053 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3054 struct brcmf_sdio_count *sdcnt = &sdiodev->bus->sdcnt;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003055
Arend van Spriel82d957e2014-07-12 08:49:36 +02003056 seq_printf(seq,
3057 "intrcount: %u\nlastintrs: %u\n"
3058 "pollcnt: %u\nregfails: %u\n"
3059 "tx_sderrs: %u\nfcqueued: %u\n"
3060 "rxrtx: %u\nrx_toolong: %u\n"
3061 "rxc_errors: %u\nrx_hdrfail: %u\n"
3062 "rx_badhdr: %u\nrx_badseq: %u\n"
3063 "fc_rcvd: %u\nfc_xoff: %u\n"
3064 "fc_xon: %u\nrxglomfail: %u\n"
3065 "rxglomframes: %u\nrxglompkts: %u\n"
3066 "f2rxhdrs: %u\nf2rxdata: %u\n"
3067 "f2txdata: %u\nf1regdata: %u\n"
3068 "tickcnt: %u\ntx_ctlerrs: %lu\n"
3069 "tx_ctlpkts: %lu\nrx_ctlerrs: %lu\n"
3070 "rx_ctlpkts: %lu\nrx_readahead: %lu\n",
3071 sdcnt->intrcount, sdcnt->lastintrs,
3072 sdcnt->pollcnt, sdcnt->regfails,
3073 sdcnt->tx_sderrs, sdcnt->fcqueued,
3074 sdcnt->rxrtx, sdcnt->rx_toolong,
3075 sdcnt->rxc_errors, sdcnt->rx_hdrfail,
3076 sdcnt->rx_badhdr, sdcnt->rx_badseq,
3077 sdcnt->fc_rcvd, sdcnt->fc_xoff,
3078 sdcnt->fc_xon, sdcnt->rxglomfail,
3079 sdcnt->rxglomframes, sdcnt->rxglompkts,
3080 sdcnt->f2rxhdrs, sdcnt->f2rxdata,
3081 sdcnt->f2txdata, sdcnt->f1regdata,
3082 sdcnt->tickcnt, sdcnt->tx_ctlerrs,
3083 sdcnt->tx_ctlpkts, sdcnt->rx_ctlerrs,
3084 sdcnt->rx_ctlpkts, sdcnt->rx_readahead_cnt);
3085
3086 return 0;
3087}
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003088
Arend van Spriel80969832012-06-09 22:51:44 +02003089static void brcmf_sdio_debugfs_create(struct brcmf_sdio *bus)
3090{
3091 struct brcmf_pub *drvr = bus->sdiodev->bus_if->drvr;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003092 struct dentry *dentry = brcmf_debugfs_get_devdir(drvr);
Arend van Spriel80969832012-06-09 22:51:44 +02003093
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003094 if (IS_ERR_OR_NULL(dentry))
3095 return;
3096
Arend van Spriel9d6c1dc2015-08-26 22:15:01 +02003097 bus->console_interval = BRCMF_CONSOLE;
3098
Arend van Spriel82d957e2014-07-12 08:49:36 +02003099 brcmf_debugfs_add_entry(drvr, "forensics", brcmf_sdio_forensic_read);
3100 brcmf_debugfs_add_entry(drvr, "counters",
3101 brcmf_debugfs_sdio_count_read);
Daniel Kim0801e6c2014-01-29 15:32:13 +01003102 debugfs_create_u32("console_interval", 0644, dentry,
3103 &bus->console_interval);
Arend van Spriel80969832012-06-09 22:51:44 +02003104}
3105#else
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003106static int brcmf_sdio_checkdied(struct brcmf_sdio *bus)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003107{
3108 return 0;
3109}
3110
Arend van Spriel80969832012-06-09 22:51:44 +02003111static void brcmf_sdio_debugfs_create(struct brcmf_sdio *bus)
3112{
3113}
3114#endif /* DEBUG */
3115
Franky Linfcf094f2011-12-16 18:37:16 -08003116static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003117brcmf_sdio_bus_rxctl(struct device *dev, unsigned char *msg, uint msglen)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003118{
3119 int timeleft;
3120 uint rxlen = 0;
3121 bool pending;
Franky Lindd43a012012-11-05 16:22:22 -08003122 u8 *buf;
Franky Lin532cdd32011-11-22 17:21:54 -08003123 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01003124 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Lin532cdd32011-11-22 17:21:54 -08003125 struct brcmf_sdio *bus = sdiodev->bus;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003126
3127 brcmf_dbg(TRACE, "Enter\n");
Arend van Spriel5768f312015-06-11 00:12:20 +02003128 if (sdiodev->state != BRCMF_SDIOD_DATA)
3129 return -EIO;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003130
3131 /* Wait until control frame is available */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003132 timeleft = brcmf_sdio_dcmd_resp_wait(bus, &bus->rxlen, &pending);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003133
Franky Lindd43a012012-11-05 16:22:22 -08003134 spin_lock_bh(&bus->rxctl_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003135 rxlen = bus->rxlen;
3136 memcpy(msg, bus->rxctl, min(msglen, rxlen));
Franky Lindd43a012012-11-05 16:22:22 -08003137 bus->rxctl = NULL;
3138 buf = bus->rxctl_orig;
3139 bus->rxctl_orig = NULL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003140 bus->rxlen = 0;
Franky Lindd43a012012-11-05 16:22:22 -08003141 spin_unlock_bh(&bus->rxctl_lock);
3142 vfree(buf);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003143
3144 if (rxlen) {
3145 brcmf_dbg(CTL, "resumed on rxctl frame, got %d expected %d\n",
3146 rxlen, msglen);
3147 } else if (timeleft == 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003148 brcmf_err("resumed on timeout\n");
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003149 brcmf_sdio_checkdied(bus);
Joe Perches23677ce2012-02-09 11:17:23 +00003150 } else if (pending) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003151 brcmf_dbg(CTL, "cancelled\n");
3152 return -ERESTARTSYS;
3153 } else {
3154 brcmf_dbg(CTL, "resumed for unknown reason?\n");
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003155 brcmf_sdio_checkdied(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003156 }
3157
3158 if (rxlen)
Arend van Spriel80969832012-06-09 22:51:44 +02003159 bus->sdcnt.rx_ctlpkts++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003160 else
Arend van Spriel80969832012-06-09 22:51:44 +02003161 bus->sdcnt.rx_ctlerrs++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003162
3163 return rxlen ? (int)rxlen : -ETIMEDOUT;
3164}
3165
Hante Meulemana74d0362014-01-13 22:20:22 +01003166#ifdef DEBUG
3167static bool
3168brcmf_sdio_verifymemory(struct brcmf_sdio_dev *sdiodev, u32 ram_addr,
3169 u8 *ram_data, uint ram_sz)
3170{
3171 char *ram_cmp;
3172 int err;
3173 bool ret = true;
3174 int address;
3175 int offset;
3176 int len;
3177
3178 /* read back and verify */
3179 brcmf_dbg(INFO, "Compare RAM dl & ul at 0x%08x; size=%d\n", ram_addr,
3180 ram_sz);
3181 ram_cmp = kmalloc(MEMBLOCK, GFP_KERNEL);
3182 /* do not proceed while no memory but */
3183 if (!ram_cmp)
3184 return true;
3185
3186 address = ram_addr;
3187 offset = 0;
3188 while (offset < ram_sz) {
3189 len = ((offset + MEMBLOCK) < ram_sz) ? MEMBLOCK :
3190 ram_sz - offset;
3191 err = brcmf_sdiod_ramrw(sdiodev, false, address, ram_cmp, len);
3192 if (err) {
3193 brcmf_err("error %d on reading %d membytes at 0x%08x\n",
3194 err, len, address);
3195 ret = false;
3196 break;
3197 } else if (memcmp(ram_cmp, &ram_data[offset], len)) {
3198 brcmf_err("Downloaded RAM image is corrupted, block offset is %d, len is %d\n",
3199 offset, len);
3200 ret = false;
3201 break;
3202 }
3203 offset += len;
3204 address += len;
3205 }
3206
3207 kfree(ram_cmp);
3208
3209 return ret;
3210}
3211#else /* DEBUG */
3212static bool
3213brcmf_sdio_verifymemory(struct brcmf_sdio_dev *sdiodev, u32 ram_addr,
3214 u8 *ram_data, uint ram_sz)
3215{
3216 return true;
3217}
3218#endif /* DEBUG */
3219
Arend van Spriel33556502014-01-13 22:20:25 +01003220static int brcmf_sdio_download_code_file(struct brcmf_sdio *bus,
3221 const struct firmware *fw)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003222{
Hante Meulemanf2c44fe2013-09-25 13:05:45 +02003223 int err;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003224
Hante Meulemana74d0362014-01-13 22:20:22 +01003225 brcmf_dbg(TRACE, "Enter\n");
3226
Hante Meulemanf9951c12014-01-29 15:32:17 +01003227 err = brcmf_sdiod_ramrw(bus->sdiodev, true, bus->ci->rambase,
3228 (u8 *)fw->data, fw->size);
3229 if (err)
3230 brcmf_err("error %d on writing %d membytes at 0x%08x\n",
3231 err, (int)fw->size, bus->ci->rambase);
3232 else if (!brcmf_sdio_verifymemory(bus->sdiodev, bus->ci->rambase,
3233 (u8 *)fw->data, fw->size))
3234 err = -EIO;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003235
Hante Meulemanf2c44fe2013-09-25 13:05:45 +02003236 return err;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003237}
3238
Arend van Spriel33556502014-01-13 22:20:25 +01003239static int brcmf_sdio_download_nvram(struct brcmf_sdio *bus,
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003240 void *vars, u32 varsz)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003241{
Hante Meulemana74d0362014-01-13 22:20:22 +01003242 int address;
3243 int err;
3244
3245 brcmf_dbg(TRACE, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003246
Hante Meulemana74d0362014-01-13 22:20:22 +01003247 address = bus->ci->ramsize - varsz + bus->ci->rambase;
3248 err = brcmf_sdiod_ramrw(bus->sdiodev, true, address, vars, varsz);
3249 if (err)
3250 brcmf_err("error %d on writing %d nvram bytes at 0x%08x\n",
3251 err, varsz, address);
3252 else if (!brcmf_sdio_verifymemory(bus->sdiodev, address, vars, varsz))
3253 err = -EIO;
3254
Hante Meulemana74d0362014-01-13 22:20:22 +01003255 return err;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003256}
3257
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003258static int brcmf_sdio_download_firmware(struct brcmf_sdio *bus,
3259 const struct firmware *fw,
3260 void *nvram, u32 nvlen)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003261{
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003262 int bcmerror = -EFAULT;
Arend van Spriel33556502014-01-13 22:20:25 +01003263 u32 rstvec;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003264
3265 sdio_claim_host(bus->sdiodev->func[1]);
3266 brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003267
Arend van Spriel33556502014-01-13 22:20:25 +01003268 rstvec = get_unaligned_le32(fw->data);
3269 brcmf_dbg(SDIO, "firmware rstvec: %x\n", rstvec);
3270
3271 bcmerror = brcmf_sdio_download_code_file(bus, fw);
3272 release_firmware(fw);
3273 if (bcmerror) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003274 brcmf_err("dongle image file download failed\n");
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003275 brcmf_fw_nvram_free(nvram);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003276 goto err;
3277 }
3278
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003279 bcmerror = brcmf_sdio_download_nvram(bus, nvram, nvlen);
3280 brcmf_fw_nvram_free(nvram);
Arend van Spriel33556502014-01-13 22:20:25 +01003281 if (bcmerror) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003282 brcmf_err("dongle nvram file download failed\n");
Franky Lin3eaa9562013-04-02 21:06:20 +02003283 goto err;
3284 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02003285
3286 /* Take arm out of reset */
Arend van Sprield380ebc2015-03-11 16:11:29 +01003287 if (!brcmf_chip_set_active(bus->ci, rstvec)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003288 brcmf_err("error getting out of ARM core reset\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003289 goto err;
3290 }
3291
Hante Meulemana1cee862015-01-25 20:31:35 +01003292 /* Allow full data communication using DPC from now on. */
Arend van Spriela1ce7a02015-02-06 18:36:42 +01003293 brcmf_sdiod_change_state(bus->sdiodev, BRCMF_SDIOD_DATA);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003294 bcmerror = 0;
3295
3296err:
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003297 brcmf_sdio_clkctl(bus, CLK_SDONLY, false);
3298 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003299 return bcmerror;
3300}
3301
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003302static void brcmf_sdio_sr_init(struct brcmf_sdio *bus)
Piotr Haber4a3da992013-04-11 13:28:46 +02003303{
3304 int err = 0;
3305 u8 val;
3306
3307 brcmf_dbg(TRACE, "Enter\n");
3308
Arend van Spriela39be272013-12-12 11:58:58 +01003309 val = brcmf_sdiod_regrb(bus->sdiodev, SBSDIO_FUNC1_WAKEUPCTRL, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003310 if (err) {
3311 brcmf_err("error reading SBSDIO_FUNC1_WAKEUPCTRL\n");
3312 return;
3313 }
3314
3315 val |= 1 << SBSDIO_FUNC1_WCTRL_HTWAIT_SHIFT;
Arend van Spriela39be272013-12-12 11:58:58 +01003316 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_WAKEUPCTRL, val, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003317 if (err) {
3318 brcmf_err("error writing SBSDIO_FUNC1_WAKEUPCTRL\n");
3319 return;
3320 }
3321
3322 /* Add CMD14 Support */
Arend van Spriela39be272013-12-12 11:58:58 +01003323 brcmf_sdiod_regwb(bus->sdiodev, SDIO_CCCR_BRCM_CARDCAP,
3324 (SDIO_CCCR_BRCM_CARDCAP_CMD14_SUPPORT |
3325 SDIO_CCCR_BRCM_CARDCAP_CMD14_EXT),
3326 &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003327 if (err) {
3328 brcmf_err("error writing SDIO_CCCR_BRCM_CARDCAP\n");
3329 return;
3330 }
3331
Arend van Spriela39be272013-12-12 11:58:58 +01003332 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3333 SBSDIO_FORCE_HT, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003334 if (err) {
3335 brcmf_err("error writing SBSDIO_FUNC1_CHIPCLKCSR\n");
3336 return;
3337 }
3338
3339 /* set flag */
3340 bus->sr_enabled = true;
3341 brcmf_dbg(INFO, "SR enabled\n");
3342}
3343
3344/* enable KSO bit */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003345static int brcmf_sdio_kso_init(struct brcmf_sdio *bus)
Piotr Haber4a3da992013-04-11 13:28:46 +02003346{
3347 u8 val;
3348 int err = 0;
3349
3350 brcmf_dbg(TRACE, "Enter\n");
3351
3352 /* KSO bit added in SDIO core rev 12 */
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003353 if (brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV)->rev < 12)
Piotr Haber4a3da992013-04-11 13:28:46 +02003354 return 0;
3355
Arend van Spriela39be272013-12-12 11:58:58 +01003356 val = brcmf_sdiod_regrb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003357 if (err) {
3358 brcmf_err("error reading SBSDIO_FUNC1_SLEEPCSR\n");
3359 return err;
3360 }
3361
3362 if (!(val & SBSDIO_FUNC1_SLEEPCSR_KSO_MASK)) {
3363 val |= (SBSDIO_FUNC1_SLEEPCSR_KSO_EN <<
3364 SBSDIO_FUNC1_SLEEPCSR_KSO_SHIFT);
Arend van Spriela39be272013-12-12 11:58:58 +01003365 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
3366 val, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003367 if (err) {
3368 brcmf_err("error writing SBSDIO_FUNC1_SLEEPCSR\n");
3369 return err;
3370 }
3371 }
3372
3373 return 0;
3374}
3375
3376
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003377static int brcmf_sdio_bus_preinit(struct device *dev)
Arend van Sprielcf458282013-11-29 11:48:15 +01003378{
3379 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3380 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3381 struct brcmf_sdio *bus = sdiodev->bus;
Franky Lin8da9d2c2013-11-29 12:25:10 +01003382 uint pad_size;
Arend van Sprielcf458282013-11-29 11:48:15 +01003383 u32 value;
Arend van Sprielcf458282013-11-29 11:48:15 +01003384 int err;
3385
Franky Lin8da9d2c2013-11-29 12:25:10 +01003386 /* the commands below use the terms tx and rx from
3387 * a device perspective, ie. bus:txglom affects the
3388 * bus transfers from device to host.
3389 */
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003390 if (brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV)->rev < 12) {
Arend van Sprielcf458282013-11-29 11:48:15 +01003391 /* for sdio core rev < 12, disable txgloming */
3392 value = 0;
3393 err = brcmf_iovar_data_set(dev, "bus:txglom", &value,
3394 sizeof(u32));
3395 } else {
3396 /* otherwise, set txglomalign */
3397 value = 4;
3398 if (sdiodev->pdata)
3399 value = sdiodev->pdata->sd_sgentry_align;
3400 /* SDIO ADMA requires at least 32 bit alignment */
3401 value = max_t(u32, value, 4);
3402 err = brcmf_iovar_data_set(dev, "bus:txglomalign", &value,
3403 sizeof(u32));
3404 }
Franky Lin8da9d2c2013-11-29 12:25:10 +01003405
3406 if (err < 0)
3407 goto done;
3408
3409 bus->tx_hdrlen = SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN;
3410 if (sdiodev->sg_support) {
3411 bus->txglom = false;
3412 value = 1;
3413 pad_size = bus->sdiodev->func[2]->cur_blksize << 1;
Franky Lin8da9d2c2013-11-29 12:25:10 +01003414 err = brcmf_iovar_data_set(bus->sdiodev->dev, "bus:rxglom",
3415 &value, sizeof(u32));
3416 if (err < 0) {
3417 /* bus:rxglom is allowed to fail */
3418 err = 0;
3419 } else {
3420 bus->txglom = true;
3421 bus->tx_hdrlen += SDPCM_HWEXT_LEN;
3422 }
3423 }
3424 brcmf_bus_add_txhdrlen(bus->sdiodev->dev, bus->tx_hdrlen);
3425
3426done:
Arend van Sprielcf458282013-11-29 11:48:15 +01003427 return err;
3428}
3429
Arend van Sprielff4445a2015-10-08 20:33:11 +02003430static size_t brcmf_sdio_bus_get_ramsize(struct device *dev)
3431{
3432 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3433 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3434 struct brcmf_sdio *bus = sdiodev->bus;
3435
3436 return bus->ci->ramsize - bus->ci->srsize;
3437}
3438
3439static int brcmf_sdio_bus_get_memdump(struct device *dev, void *data,
3440 size_t mem_size)
3441{
3442 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3443 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3444 struct brcmf_sdio *bus = sdiodev->bus;
3445 int err;
3446 int address;
3447 int offset;
3448 int len;
3449
3450 brcmf_dbg(INFO, "dump at 0x%08x: size=%zu\n", bus->ci->rambase,
3451 mem_size);
3452
3453 address = bus->ci->rambase;
3454 offset = err = 0;
3455 sdio_claim_host(sdiodev->func[1]);
3456 while (offset < mem_size) {
3457 len = ((offset + MEMBLOCK) < mem_size) ? MEMBLOCK :
3458 mem_size - offset;
3459 err = brcmf_sdiod_ramrw(sdiodev, false, address, data, len);
3460 if (err) {
3461 brcmf_err("error %d on reading %d membytes at 0x%08x\n",
3462 err, len, address);
3463 goto done;
3464 }
3465 data += len;
3466 offset += len;
3467 address += len;
3468 }
3469
3470done:
3471 sdio_release_host(sdiodev->func[1]);
3472 return err;
3473}
3474
Arend van Spriel99824642015-02-06 18:36:45 +01003475void brcmf_sdio_trigger_dpc(struct brcmf_sdio *bus)
3476{
Hante Meuleman2c64e162015-03-18 13:25:22 +01003477 if (!bus->dpc_triggered) {
3478 bus->dpc_triggered = true;
Arend van Spriel99824642015-02-06 18:36:45 +01003479 queue_work(bus->brcmf_wq, &bus->datawork);
3480 }
3481}
3482
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003483void brcmf_sdio_isr(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003484{
Arend van Spriel5b435de2011-10-05 13:19:03 +02003485 brcmf_dbg(TRACE, "Enter\n");
3486
3487 if (!bus) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003488 brcmf_err("bus is null pointer, exiting\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003489 return;
3490 }
3491
Arend van Spriel5b435de2011-10-05 13:19:03 +02003492 /* Count the interrupt call */
Arend van Spriel80969832012-06-09 22:51:44 +02003493 bus->sdcnt.intrcount++;
Franky Lin45316032012-09-13 21:12:03 +02003494 if (in_interrupt())
3495 atomic_set(&bus->ipend, 1);
3496 else
3497 if (brcmf_sdio_intr_rstatus(bus)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003498 brcmf_err("failed backplane access\n");
Franky Lin45316032012-09-13 21:12:03 +02003499 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02003500
Arend van Spriel5b435de2011-10-05 13:19:03 +02003501 /* Disable additional interrupts (is this needed now)? */
3502 if (!bus->intr)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003503 brcmf_err("isr w/o interrupt configured!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003504
Hante Meuleman2c64e162015-03-18 13:25:22 +01003505 bus->dpc_triggered = true;
Franky Linf1e68c22012-09-13 21:12:00 +02003506 queue_work(bus->brcmf_wq, &bus->datawork);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003507}
3508
Hante Meulemanb441ba82015-03-06 18:40:39 +01003509static void brcmf_sdio_bus_watchdog(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003510{
Arend van Spriel5b435de2011-10-05 13:19:03 +02003511 brcmf_dbg(TIMER, "Enter\n");
3512
Arend van Spriel5b435de2011-10-05 13:19:03 +02003513 /* Poll period: check device if appropriate. */
Piotr Haber4a3da992013-04-11 13:28:46 +02003514 if (!bus->sr_enabled &&
3515 bus->poll && (++bus->polltick >= bus->pollrate)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003516 u32 intstatus = 0;
3517
3518 /* Reset poll tick */
3519 bus->polltick = 0;
3520
3521 /* Check device if no interrupts */
Arend van Spriel80969832012-06-09 22:51:44 +02003522 if (!bus->intr ||
3523 (bus->sdcnt.intrcount == bus->sdcnt.lastintrs)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003524
Hante Meuleman2c64e162015-03-18 13:25:22 +01003525 if (!bus->dpc_triggered) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003526 u8 devpend;
Arend van Sprielfccfe932013-06-18 13:29:26 +02003527
Franky Lin38b0b0d2012-11-05 16:22:24 -08003528 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriela39be272013-12-12 11:58:58 +01003529 devpend = brcmf_sdiod_regrb(bus->sdiodev,
3530 SDIO_CCCR_INTx,
3531 NULL);
Franky Lin38b0b0d2012-11-05 16:22:24 -08003532 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel99824642015-02-06 18:36:45 +01003533 intstatus = devpend & (INTR_STATUS_FUNC1 |
3534 INTR_STATUS_FUNC2);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003535 }
3536
3537 /* If there is something, make like the ISR and
3538 schedule the DPC */
3539 if (intstatus) {
Arend van Spriel80969832012-06-09 22:51:44 +02003540 bus->sdcnt.pollcnt++;
Franky Lin1d382272012-09-13 21:11:59 +02003541 atomic_set(&bus->ipend, 1);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003542
Hante Meuleman2c64e162015-03-18 13:25:22 +01003543 bus->dpc_triggered = true;
Franky Linf1e68c22012-09-13 21:12:00 +02003544 queue_work(bus->brcmf_wq, &bus->datawork);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003545 }
3546 }
3547
3548 /* Update interrupt tracking */
Arend van Spriel80969832012-06-09 22:51:44 +02003549 bus->sdcnt.lastintrs = bus->sdcnt.intrcount;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003550 }
Joe Perches8ae74652012-01-15 00:38:38 -08003551#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02003552 /* Poll for console output periodically */
Arend van Spriel9d6c1dc2015-08-26 22:15:01 +02003553 if (bus->sdiodev->state == BRCMF_SDIOD_DATA && BRCMF_FWCON_ON() &&
Franky Lin8d169aa2011-11-22 17:21:52 -08003554 bus->console_interval != 0) {
Arend van Spriel63ce3d52016-01-05 11:05:48 +01003555 bus->console.count += jiffies_to_msecs(BRCMF_WD_POLL);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003556 if (bus->console.count >= bus->console_interval) {
3557 bus->console.count -= bus->console_interval;
Franky Lin38b0b0d2012-11-05 16:22:24 -08003558 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003559 /* Make sure backplane clock is on */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003560 brcmf_sdio_bus_sleep(bus, false, false);
3561 if (brcmf_sdio_readconsole(bus) < 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003562 /* stop on error */
3563 bus->console_interval = 0;
Franky Lin38b0b0d2012-11-05 16:22:24 -08003564 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003565 }
3566 }
Joe Perches8ae74652012-01-15 00:38:38 -08003567#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02003568
3569 /* On idle timeout clear activity flag and/or turn off clock */
Hante Meuleman2c64e162015-03-18 13:25:22 +01003570 if (!bus->dpc_triggered) {
3571 rmb();
3572 if ((!bus->dpc_running) && (bus->idletime > 0) &&
3573 (bus->clkstate == CLK_AVAIL)) {
3574 bus->idlecount++;
3575 if (bus->idlecount > bus->idletime) {
3576 brcmf_dbg(SDIO, "idle\n");
3577 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel4011fc42016-01-05 11:05:47 +01003578 brcmf_sdio_wd_timer(bus, false);
Hante Meuleman2c64e162015-03-18 13:25:22 +01003579 bus->idlecount = 0;
3580 brcmf_sdio_bus_sleep(bus, true, false);
3581 sdio_release_host(bus->sdiodev->func[1]);
3582 }
3583 } else {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003584 bus->idlecount = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003585 }
Hante Meulemanb441ba82015-03-06 18:40:39 +01003586 } else {
3587 bus->idlecount = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003588 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02003589}
3590
Franky Linf1e68c22012-09-13 21:12:00 +02003591static void brcmf_sdio_dataworker(struct work_struct *work)
3592{
3593 struct brcmf_sdio *bus = container_of(work, struct brcmf_sdio,
3594 datawork);
Franky Linf1e68c22012-09-13 21:12:00 +02003595
Hante Meuleman2c64e162015-03-18 13:25:22 +01003596 bus->dpc_running = true;
3597 wmb();
3598 while (ACCESS_ONCE(bus->dpc_triggered)) {
3599 bus->dpc_triggered = false;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003600 brcmf_sdio_dpc(bus);
Hante Meulemanb441ba82015-03-06 18:40:39 +01003601 bus->idlecount = 0;
Franky Linf1e68c22012-09-13 21:12:00 +02003602 }
Hante Meuleman2c64e162015-03-18 13:25:22 +01003603 bus->dpc_running = false;
Arend van Spriel99824642015-02-06 18:36:45 +01003604 if (brcmf_sdiod_freezing(bus->sdiodev)) {
3605 brcmf_sdiod_change_state(bus->sdiodev, BRCMF_SDIOD_DOWN);
3606 brcmf_sdiod_try_freeze(bus->sdiodev);
3607 brcmf_sdiod_change_state(bus->sdiodev, BRCMF_SDIOD_DATA);
3608 }
Franky Linf1e68c22012-09-13 21:12:00 +02003609}
3610
Arend van Spriel65d80d02014-01-29 15:32:14 +01003611static void
3612brcmf_sdio_drivestrengthinit(struct brcmf_sdio_dev *sdiodev,
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003613 struct brcmf_chip *ci, u32 drivestrength)
Arend van Spriel65d80d02014-01-29 15:32:14 +01003614{
3615 const struct sdiod_drive_str *str_tab = NULL;
3616 u32 str_mask;
3617 u32 str_shift;
Arend van Spriel65d80d02014-01-29 15:32:14 +01003618 u32 i;
3619 u32 drivestrength_sel = 0;
3620 u32 cc_data_temp;
3621 u32 addr;
3622
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003623 if (!(ci->cc_caps & CC_CAP_PMU))
Arend van Spriel65d80d02014-01-29 15:32:14 +01003624 return;
3625
3626 switch (SDIOD_DRVSTR_KEY(ci->chip, ci->pmurev)) {
Hante Meuleman5779ae62014-07-12 08:49:34 +02003627 case SDIOD_DRVSTR_KEY(BRCM_CC_4330_CHIP_ID, 12):
Arend van Spriel65d80d02014-01-29 15:32:14 +01003628 str_tab = sdiod_drvstr_tab1_1v8;
3629 str_mask = 0x00003800;
3630 str_shift = 11;
3631 break;
Hante Meuleman5779ae62014-07-12 08:49:34 +02003632 case SDIOD_DRVSTR_KEY(BRCM_CC_4334_CHIP_ID, 17):
Arend van Spriel65d80d02014-01-29 15:32:14 +01003633 str_tab = sdiod_drvstr_tab6_1v8;
3634 str_mask = 0x00001800;
3635 str_shift = 11;
3636 break;
Hante Meuleman5779ae62014-07-12 08:49:34 +02003637 case SDIOD_DRVSTR_KEY(BRCM_CC_43143_CHIP_ID, 17):
Arend van Spriel65d80d02014-01-29 15:32:14 +01003638 /* note: 43143 does not support tristate */
3639 i = ARRAY_SIZE(sdiod_drvstr_tab2_3v3) - 1;
3640 if (drivestrength >= sdiod_drvstr_tab2_3v3[i].strength) {
3641 str_tab = sdiod_drvstr_tab2_3v3;
3642 str_mask = 0x00000007;
3643 str_shift = 0;
3644 } else
3645 brcmf_err("Invalid SDIO Drive strength for chip %s, strength=%d\n",
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003646 ci->name, drivestrength);
Arend van Spriel65d80d02014-01-29 15:32:14 +01003647 break;
Hante Meuleman5779ae62014-07-12 08:49:34 +02003648 case SDIOD_DRVSTR_KEY(BRCM_CC_43362_CHIP_ID, 13):
Arend van Spriel65d80d02014-01-29 15:32:14 +01003649 str_tab = sdiod_drive_strength_tab5_1v8;
3650 str_mask = 0x00003800;
3651 str_shift = 11;
3652 break;
3653 default:
3654 brcmf_err("No SDIO Drive strength init done for chip %s rev %d pmurev %d\n",
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003655 ci->name, ci->chiprev, ci->pmurev);
Arend van Spriel65d80d02014-01-29 15:32:14 +01003656 break;
3657 }
3658
3659 if (str_tab != NULL) {
Rafał Miłeckie2b397f2016-01-26 17:57:04 +01003660 struct brcmf_core *pmu = brcmf_chip_get_pmu(ci);
3661
Arend van Spriel65d80d02014-01-29 15:32:14 +01003662 for (i = 0; str_tab[i].strength != 0; i++) {
3663 if (drivestrength >= str_tab[i].strength) {
3664 drivestrength_sel = str_tab[i].sel;
3665 break;
3666 }
3667 }
Rafał Miłeckie2b397f2016-01-26 17:57:04 +01003668 addr = CORE_CC_REG(pmu->base, chipcontrol_addr);
Arend van Spriel65d80d02014-01-29 15:32:14 +01003669 brcmf_sdiod_regwl(sdiodev, addr, 1, NULL);
3670 cc_data_temp = brcmf_sdiod_regrl(sdiodev, addr, NULL);
3671 cc_data_temp &= ~str_mask;
3672 drivestrength_sel <<= str_shift;
3673 cc_data_temp |= drivestrength_sel;
3674 brcmf_sdiod_regwl(sdiodev, addr, cc_data_temp, NULL);
3675
3676 brcmf_dbg(INFO, "SDIO: %d mA (req=%d mA) drive strength selected, set to 0x%08x\n",
3677 str_tab[i].strength, drivestrength, cc_data_temp);
3678 }
3679}
3680
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003681static int brcmf_sdio_buscoreprep(void *ctx)
Arend van Spriel65d80d02014-01-29 15:32:14 +01003682{
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003683 struct brcmf_sdio_dev *sdiodev = ctx;
Arend van Spriel65d80d02014-01-29 15:32:14 +01003684 int err = 0;
3685 u8 clkval, clkset;
3686
3687 /* Try forcing SDIO core to do ALPAvail request only */
3688 clkset = SBSDIO_FORCE_HW_CLKREQ_OFF | SBSDIO_ALP_AVAIL_REQ;
3689 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, clkset, &err);
3690 if (err) {
3691 brcmf_err("error writing for HT off\n");
3692 return err;
3693 }
3694
3695 /* If register supported, wait for ALPAvail and then force ALP */
3696 /* This may take up to 15 milliseconds */
3697 clkval = brcmf_sdiod_regrb(sdiodev,
3698 SBSDIO_FUNC1_CHIPCLKCSR, NULL);
3699
3700 if ((clkval & ~SBSDIO_AVBITS) != clkset) {
3701 brcmf_err("ChipClkCSR access: wrote 0x%02x read 0x%02x\n",
3702 clkset, clkval);
3703 return -EACCES;
3704 }
3705
3706 SPINWAIT(((clkval = brcmf_sdiod_regrb(sdiodev,
3707 SBSDIO_FUNC1_CHIPCLKCSR, NULL)),
3708 !SBSDIO_ALPAV(clkval)),
3709 PMU_MAX_TRANSITION_DLY);
3710 if (!SBSDIO_ALPAV(clkval)) {
3711 brcmf_err("timeout on ALPAV wait, clkval 0x%02x\n",
3712 clkval);
3713 return -EBUSY;
3714 }
3715
3716 clkset = SBSDIO_FORCE_HW_CLKREQ_OFF | SBSDIO_FORCE_ALP;
3717 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, clkset, &err);
3718 udelay(65);
3719
3720 /* Also, disable the extra SDIO pull-ups */
3721 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_SDIOPULLUP, 0, NULL);
3722
3723 return 0;
3724}
3725
Arend van Sprield380ebc2015-03-11 16:11:29 +01003726static void brcmf_sdio_buscore_activate(void *ctx, struct brcmf_chip *chip,
3727 u32 rstvec)
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003728{
3729 struct brcmf_sdio_dev *sdiodev = ctx;
3730 struct brcmf_core *core;
3731 u32 reg_addr;
3732
3733 /* clear all interrupts */
3734 core = brcmf_chip_get_core(chip, BCMA_CORE_SDIO_DEV);
3735 reg_addr = core->base + offsetof(struct sdpcmd_regs, intstatus);
3736 brcmf_sdiod_regwl(sdiodev, reg_addr, 0xFFFFFFFF, NULL);
3737
3738 if (rstvec)
3739 /* Write reset vector to address 0 */
3740 brcmf_sdiod_ramrw(sdiodev, true, 0, (void *)&rstvec,
3741 sizeof(rstvec));
3742}
3743
3744static u32 brcmf_sdio_buscore_read32(void *ctx, u32 addr)
3745{
3746 struct brcmf_sdio_dev *sdiodev = ctx;
3747 u32 val, rev;
3748
3749 val = brcmf_sdiod_regrl(sdiodev, addr, NULL);
Arend van Spriel8bd61f82015-01-06 23:02:54 +01003750 if (sdiodev->func[0]->device == SDIO_DEVICE_ID_BROADCOM_4335_4339 &&
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003751 addr == CORE_CC_REG(SI_ENUM_BASE, chipid)) {
3752 rev = (val & CID_REV_MASK) >> CID_REV_SHIFT;
3753 if (rev >= 2) {
3754 val &= ~CID_ID_MASK;
Hante Meuleman5779ae62014-07-12 08:49:34 +02003755 val |= BRCM_CC_4339_CHIP_ID;
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003756 }
3757 }
3758 return val;
3759}
3760
3761static void brcmf_sdio_buscore_write32(void *ctx, u32 addr, u32 val)
3762{
3763 struct brcmf_sdio_dev *sdiodev = ctx;
3764
3765 brcmf_sdiod_regwl(sdiodev, addr, val, NULL);
3766}
3767
3768static const struct brcmf_buscore_ops brcmf_sdio_buscore_ops = {
3769 .prepare = brcmf_sdio_buscoreprep,
Arend van Sprield380ebc2015-03-11 16:11:29 +01003770 .activate = brcmf_sdio_buscore_activate,
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003771 .read32 = brcmf_sdio_buscore_read32,
3772 .write32 = brcmf_sdio_buscore_write32,
3773};
3774
Arend van Spriel5b435de2011-10-05 13:19:03 +02003775static bool
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003776brcmf_sdio_probe_attach(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003777{
3778 u8 clkctl = 0;
3779 int err = 0;
3780 int reg_addr;
3781 u32 reg_val;
Hante Meuleman668761a2013-04-12 10:55:55 +02003782 u32 drivestrength;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003783
Franky Lin38b0b0d2012-11-05 16:22:24 -08003784 sdio_claim_host(bus->sdiodev->func[1]);
3785
Joe Perches18aad4f2012-01-15 00:38:42 -08003786 pr_debug("F1 signature read @0x18000000=0x%4x\n",
Arend van Spriela39be272013-12-12 11:58:58 +01003787 brcmf_sdiod_regrl(bus->sdiodev, SI_ENUM_BASE, NULL));
Arend van Spriel5b435de2011-10-05 13:19:03 +02003788
3789 /*
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003790 * Force PLL off until brcmf_chip_attach()
Arend van Spriel5b435de2011-10-05 13:19:03 +02003791 * programs PLL control regs
3792 */
3793
Arend van Spriela39be272013-12-12 11:58:58 +01003794 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3795 BRCMF_INIT_CLKCTL1, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003796 if (!err)
Arend van Spriela39be272013-12-12 11:58:58 +01003797 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
3798 SBSDIO_FUNC1_CHIPCLKCSR, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003799
3800 if (err || ((clkctl & ~SBSDIO_AVBITS) != BRCMF_INIT_CLKCTL1)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003801 brcmf_err("ChipClkCSR access: err %d wrote 0x%02x read 0x%02x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02003802 err, BRCMF_INIT_CLKCTL1, clkctl);
3803 goto fail;
3804 }
3805
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003806 bus->ci = brcmf_chip_attach(bus->sdiodev, &brcmf_sdio_buscore_ops);
3807 if (IS_ERR(bus->ci)) {
3808 brcmf_err("brcmf_chip_attach failed!\n");
3809 bus->ci = NULL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003810 goto fail;
3811 }
3812
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003813 if (brcmf_sdio_kso_init(bus)) {
Piotr Haber4a3da992013-04-11 13:28:46 +02003814 brcmf_err("error enabling KSO\n");
3815 goto fail;
3816 }
3817
Hante Meuleman668761a2013-04-12 10:55:55 +02003818 if ((bus->sdiodev->pdata) && (bus->sdiodev->pdata->drive_strength))
3819 drivestrength = bus->sdiodev->pdata->drive_strength;
3820 else
3821 drivestrength = DEFAULT_SDIO_DRIVE_STRENGTH;
Arend van Spriel65d80d02014-01-29 15:32:14 +01003822 brcmf_sdio_drivestrengthinit(bus->sdiodev, bus->ci, drivestrength);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003823
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003824 /* Set card control so an SDIO card reset does a WLAN backplane reset */
Arend van Spriela39be272013-12-12 11:58:58 +01003825 reg_val = brcmf_sdiod_regrb(bus->sdiodev,
3826 SDIO_CCCR_BRCM_CARDCTRL, &err);
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003827 if (err)
3828 goto fail;
3829
3830 reg_val |= SDIO_CCCR_BRCM_CARDCTRL_WLANRESET;
3831
Arend van Spriela39be272013-12-12 11:58:58 +01003832 brcmf_sdiod_regwb(bus->sdiodev,
3833 SDIO_CCCR_BRCM_CARDCTRL, reg_val, &err);
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003834 if (err)
3835 goto fail;
3836
3837 /* set PMUControl so a backplane reset does PMU state reload */
Rafał Miłeckie2b397f2016-01-26 17:57:04 +01003838 reg_addr = CORE_CC_REG(brcmf_chip_get_pmu(bus->ci)->base, pmucontrol);
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003839 reg_val = brcmf_sdiod_regrl(bus->sdiodev, reg_addr, &err);
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003840 if (err)
3841 goto fail;
3842
3843 reg_val |= (BCMA_CC_PMU_CTL_RES_RELOAD << BCMA_CC_PMU_CTL_RES_SHIFT);
3844
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003845 brcmf_sdiod_regwl(bus->sdiodev, reg_addr, reg_val, &err);
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003846 if (err)
3847 goto fail;
3848
Franky Lin38b0b0d2012-11-05 16:22:24 -08003849 sdio_release_host(bus->sdiodev->func[1]);
3850
Arend van Spriel5b435de2011-10-05 13:19:03 +02003851 brcmu_pktq_init(&bus->txq, (PRIOMASK + 1), TXQLEN);
3852
Arend van Spriel9b2d2f2a82013-11-29 12:25:14 +01003853 /* allocate header buffer */
3854 bus->hdrbuf = kzalloc(MAX_HDR_READ + bus->head_align, GFP_KERNEL);
3855 if (!bus->hdrbuf)
3856 return false;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003857 /* Locate an appropriately-aligned portion of hdrbuf */
3858 bus->rxhdr = (u8 *) roundup((unsigned long)&bus->hdrbuf[0],
Arend van Spriel9b2d2f2a82013-11-29 12:25:14 +01003859 bus->head_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003860
3861 /* Set the poll and/or interrupt flags */
3862 bus->intr = true;
3863 bus->poll = false;
3864 if (bus->poll)
3865 bus->pollrate = 1;
3866
3867 return true;
3868
3869fail:
Franky Lin38b0b0d2012-11-05 16:22:24 -08003870 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003871 return false;
3872}
3873
Arend van Spriel5b435de2011-10-05 13:19:03 +02003874static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003875brcmf_sdio_watchdog_thread(void *data)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003876{
Franky Line92eedf2011-11-22 17:21:50 -08003877 struct brcmf_sdio *bus = (struct brcmf_sdio *)data;
Arend van Spriel99824642015-02-06 18:36:45 +01003878 int wait;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003879
3880 allow_signal(SIGTERM);
3881 /* Run until signal received */
Arend van Spriel99824642015-02-06 18:36:45 +01003882 brcmf_sdiod_freezer_count(bus->sdiodev);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003883 while (1) {
3884 if (kthread_should_stop())
3885 break;
Arend van Spriel99824642015-02-06 18:36:45 +01003886 brcmf_sdiod_freezer_uncount(bus->sdiodev);
3887 wait = wait_for_completion_interruptible(&bus->watchdog_wait);
3888 brcmf_sdiod_freezer_count(bus->sdiodev);
3889 brcmf_sdiod_try_freeze(bus->sdiodev);
3890 if (!wait) {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003891 brcmf_sdio_bus_watchdog(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003892 /* Count the tick for reference */
Arend van Spriel80969832012-06-09 22:51:44 +02003893 bus->sdcnt.tickcnt++;
Arend van Spriel58e9df42014-03-20 10:18:01 +01003894 reinit_completion(&bus->watchdog_wait);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003895 } else
3896 break;
3897 }
3898 return 0;
3899}
3900
3901static void
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003902brcmf_sdio_watchdog(unsigned long data)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003903{
Franky Line92eedf2011-11-22 17:21:50 -08003904 struct brcmf_sdio *bus = (struct brcmf_sdio *)data;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003905
3906 if (bus->watchdog_tsk) {
3907 complete(&bus->watchdog_wait);
3908 /* Reschedule the watchdog */
Arend van Spriel4011fc42016-01-05 11:05:47 +01003909 if (bus->wd_active)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003910 mod_timer(&bus->timer,
Arend van Spriel63ce3d52016-01-05 11:05:48 +01003911 jiffies + BRCMF_WD_POLL);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003912 }
3913}
3914
Julia Lawall6866a642015-11-14 17:22:07 +01003915static const struct brcmf_bus_ops brcmf_sdio_bus_ops = {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003916 .stop = brcmf_sdio_bus_stop,
3917 .preinit = brcmf_sdio_bus_preinit,
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003918 .txdata = brcmf_sdio_bus_txdata,
3919 .txctl = brcmf_sdio_bus_txctl,
3920 .rxctl = brcmf_sdio_bus_rxctl,
3921 .gettxq = brcmf_sdio_bus_gettxq,
Arend van Sprielff4445a2015-10-08 20:33:11 +02003922 .wowl_config = brcmf_sdio_wowl_config,
3923 .get_ramsize = brcmf_sdio_bus_get_ramsize,
3924 .get_memdump = brcmf_sdio_bus_get_memdump,
Arend van Sprield9cb2592012-12-05 15:25:54 +01003925};
3926
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003927static void brcmf_sdio_firmware_callback(struct device *dev,
3928 const struct firmware *code,
3929 void *nvram, u32 nvram_len)
3930{
3931 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3932 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3933 struct brcmf_sdio *bus = sdiodev->bus;
3934 int err = 0;
3935 u8 saveclk;
3936
3937 brcmf_dbg(TRACE, "Enter: dev=%s\n", dev_name(dev));
3938
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003939 if (!bus_if->drvr)
3940 return;
3941
Hante Meulemana1cee862015-01-25 20:31:35 +01003942 /* try to download image and nvram to the dongle */
3943 bus->alp_only = true;
3944 err = brcmf_sdio_download_firmware(bus, code, nvram, nvram_len);
3945 if (err)
3946 goto fail;
3947 bus->alp_only = false;
3948
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003949 /* Start the watchdog timer */
3950 bus->sdcnt.tickcnt = 0;
Arend van Spriel4011fc42016-01-05 11:05:47 +01003951 brcmf_sdio_wd_timer(bus, true);
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003952
3953 sdio_claim_host(sdiodev->func[1]);
3954
3955 /* Make sure backplane clock is on, needed to generate F2 interrupt */
3956 brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
3957 if (bus->clkstate != CLK_AVAIL)
3958 goto release;
3959
3960 /* Force clocks on backplane to be sure F2 interrupt propagates */
3961 saveclk = brcmf_sdiod_regrb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, &err);
3962 if (!err) {
3963 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3964 (saveclk | SBSDIO_FORCE_HT), &err);
3965 }
3966 if (err) {
3967 brcmf_err("Failed to force clock for F2: err %d\n", err);
3968 goto release;
3969 }
3970
3971 /* Enable function 2 (frame transfers) */
3972 w_sdreg32(bus, SDPCM_PROT_VERSION << SMB_DATA_VERSION_SHIFT,
3973 offsetof(struct sdpcmd_regs, tosbmailboxdata));
3974 err = sdio_enable_func(sdiodev->func[SDIO_FUNC_2]);
3975
3976
3977 brcmf_dbg(INFO, "enable F2: err=%d\n", err);
3978
3979 /* If F2 successfully enabled, set core and enable interrupts */
3980 if (!err) {
3981 /* Set up the interrupt mask and enable interrupts */
3982 bus->hostintmask = HOSTINTMASK;
3983 w_sdreg32(bus, bus->hostintmask,
3984 offsetof(struct sdpcmd_regs, hostintmask));
3985
3986 brcmf_sdiod_regwb(sdiodev, SBSDIO_WATERMARK, 8, &err);
3987 } else {
3988 /* Disable F2 again */
3989 sdio_disable_func(sdiodev->func[SDIO_FUNC_2]);
3990 goto release;
3991 }
3992
3993 if (brcmf_chip_sr_capable(bus->ci)) {
3994 brcmf_sdio_sr_init(bus);
3995 } else {
3996 /* Restore previous clock setting */
3997 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3998 saveclk, &err);
3999 }
4000
4001 if (err == 0) {
4002 err = brcmf_sdiod_intr_register(sdiodev);
4003 if (err != 0)
4004 brcmf_err("intr register failed:%d\n", err);
4005 }
4006
4007 /* If we didn't come up, turn off backplane clock */
4008 if (err != 0)
4009 brcmf_sdio_clkctl(bus, CLK_NONE, false);
4010
4011 sdio_release_host(sdiodev->func[1]);
4012
4013 err = brcmf_bus_start(dev);
4014 if (err != 0) {
4015 brcmf_err("dongle is not responding\n");
4016 goto fail;
4017 }
4018 return;
4019
4020release:
4021 sdio_release_host(sdiodev->func[1]);
4022fail:
4023 brcmf_dbg(TRACE, "failed: dev=%s, err=%d\n", dev_name(dev), err);
4024 device_release_driver(dev);
4025}
4026
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01004027struct brcmf_sdio *brcmf_sdio_probe(struct brcmf_sdio_dev *sdiodev)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004028{
4029 int ret;
Franky Line92eedf2011-11-22 17:21:50 -08004030 struct brcmf_sdio *bus;
Arend van Spriel99824642015-02-06 18:36:45 +01004031 struct workqueue_struct *wq;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004032
Arend van Spriel5b435de2011-10-05 13:19:03 +02004033 brcmf_dbg(TRACE, "Enter\n");
4034
Arend van Spriel5b435de2011-10-05 13:19:03 +02004035 /* Allocate private bus interface state */
Franky Line92eedf2011-11-22 17:21:50 -08004036 bus = kzalloc(sizeof(struct brcmf_sdio), GFP_ATOMIC);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004037 if (!bus)
4038 goto fail;
4039
4040 bus->sdiodev = sdiodev;
4041 sdiodev->bus = bus;
Arend van Sprielb83db862011-10-19 12:51:09 +02004042 skb_queue_head_init(&bus->glom);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004043 bus->txbound = BRCMF_TXBOUND;
4044 bus->rxbound = BRCMF_RXBOUND;
4045 bus->txminmax = BRCMF_TXMINMAX;
Franky Lin6bc52312013-08-10 12:27:28 +02004046 bus->tx_seq = SDPCM_SEQ_WRAP - 1;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004047
Arend van Spriele217d1c2013-11-29 12:25:13 +01004048 /* platform specific configuration:
4049 * alignments must be at least 4 bytes for ADMA
Hante Meuleman888bf76e2014-10-28 14:56:17 +01004050 */
Arend van Spriele217d1c2013-11-29 12:25:13 +01004051 bus->head_align = ALIGNMENT;
4052 bus->sgentry_align = ALIGNMENT;
4053 if (sdiodev->pdata) {
4054 if (sdiodev->pdata->sd_head_align > ALIGNMENT)
4055 bus->head_align = sdiodev->pdata->sd_head_align;
4056 if (sdiodev->pdata->sd_sgentry_align > ALIGNMENT)
4057 bus->sgentry_align = sdiodev->pdata->sd_sgentry_align;
4058 }
4059
Arend van Spriel99824642015-02-06 18:36:45 +01004060 /* single-threaded workqueue */
4061 wq = alloc_ordered_workqueue("brcmf_wq/%s", WQ_MEM_RECLAIM,
4062 dev_name(&sdiodev->func[1]->dev));
4063 if (!wq) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01004064 brcmf_err("insufficient memory to create txworkqueue\n");
Hante Meuleman37ac5782012-11-14 18:46:18 -08004065 goto fail;
4066 }
Arend van Spriel99824642015-02-06 18:36:45 +01004067 brcmf_sdiod_freezer_count(sdiodev);
4068 INIT_WORK(&bus->datawork, brcmf_sdio_dataworker);
4069 bus->brcmf_wq = wq;
Hante Meuleman37ac5782012-11-14 18:46:18 -08004070
Arend van Spriel5b435de2011-10-05 13:19:03 +02004071 /* attempt to attach to the dongle */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01004072 if (!(brcmf_sdio_probe_attach(bus))) {
4073 brcmf_err("brcmf_sdio_probe_attach failed\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02004074 goto fail;
4075 }
4076
Franky Lindd43a012012-11-05 16:22:22 -08004077 spin_lock_init(&bus->rxctl_lock);
Hante Meulemanfed7ec42014-03-15 17:18:18 +01004078 spin_lock_init(&bus->txq_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004079 init_waitqueue_head(&bus->ctrl_wait);
4080 init_waitqueue_head(&bus->dcmd_resp_wait);
4081
4082 /* Set up the watchdog timer */
4083 init_timer(&bus->timer);
4084 bus->timer.data = (unsigned long)bus;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01004085 bus->timer.function = brcmf_sdio_watchdog;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004086
Arend van Spriel5b435de2011-10-05 13:19:03 +02004087 /* Initialize watchdog thread */
4088 init_completion(&bus->watchdog_wait);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01004089 bus->watchdog_tsk = kthread_run(brcmf_sdio_watchdog_thread,
Arend van Spriel99824642015-02-06 18:36:45 +01004090 bus, "brcmf_wdog/%s",
4091 dev_name(&sdiodev->func[1]->dev));
Arend van Spriel5b435de2011-10-05 13:19:03 +02004092 if (IS_ERR(bus->watchdog_tsk)) {
Joe Perches02f77192012-01-15 00:38:44 -08004093 pr_warn("brcmf_watchdog thread failed to start\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02004094 bus->watchdog_tsk = NULL;
4095 }
4096 /* Initialize DPC thread */
Hante Meuleman2c64e162015-03-18 13:25:22 +01004097 bus->dpc_triggered = false;
4098 bus->dpc_running = false;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004099
Franky Lina9ffda82011-12-16 18:37:12 -08004100 /* Assign bus interface call back */
Arend van Sprield9cb2592012-12-05 15:25:54 +01004101 bus->sdiodev->bus_if->dev = bus->sdiodev->dev;
4102 bus->sdiodev->bus_if->ops = &brcmf_sdio_bus_ops;
Arend van Spriel75d907d2013-02-06 18:40:45 +01004103 bus->sdiodev->bus_if->chip = bus->ci->chip;
4104 bus->sdiodev->bus_if->chiprev = bus->ci->chiprev;
Arend van Sprield9cb2592012-12-05 15:25:54 +01004105
Franky Lin706478c2013-08-10 12:27:29 +02004106 /* default sdio bus header length for tx packet */
4107 bus->tx_hdrlen = SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN;
4108
4109 /* Attach to the common layer, reserve hdr space */
Arend van Spriel8dee77b2013-11-29 11:48:14 +01004110 ret = brcmf_attach(bus->sdiodev->dev);
Franky Lin712ac5b2011-12-16 18:37:09 -08004111 if (ret != 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01004112 brcmf_err("brcmf_attach failed\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02004113 goto fail;
4114 }
4115
Daniel Kim7dd3abc2014-05-27 12:56:26 +02004116 /* Query the F2 block size, set roundup accordingly */
4117 bus->blocksize = bus->sdiodev->func[2]->cur_blksize;
4118 bus->roundup = min(max_roundup, bus->blocksize);
4119
Arend van Spriel5b435de2011-10-05 13:19:03 +02004120 /* Allocate buffers */
Arend van Sprielfad13222014-01-06 12:40:40 +01004121 if (bus->sdiodev->bus_if->maxctl) {
Daniel Kim7dd3abc2014-05-27 12:56:26 +02004122 bus->sdiodev->bus_if->maxctl += bus->roundup;
Arend van Sprielfad13222014-01-06 12:40:40 +01004123 bus->rxblen =
4124 roundup((bus->sdiodev->bus_if->maxctl + SDPCM_HDRLEN),
4125 ALIGNMENT) + bus->head_align;
4126 bus->rxbuf = kmalloc(bus->rxblen, GFP_ATOMIC);
4127 if (!(bus->rxbuf)) {
4128 brcmf_err("rxbuf allocation failed\n");
4129 goto fail;
4130 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02004131 }
4132
Arend van Sprielfad13222014-01-06 12:40:40 +01004133 sdio_claim_host(bus->sdiodev->func[1]);
4134
4135 /* Disable F2 to clear any intermediate frame state on the dongle */
4136 sdio_disable_func(bus->sdiodev->func[SDIO_FUNC_2]);
4137
Arend van Sprielfad13222014-01-06 12:40:40 +01004138 bus->rxflow = false;
4139
4140 /* Done with backplane-dependent accesses, can drop clock... */
4141 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, 0, NULL);
4142
4143 sdio_release_host(bus->sdiodev->func[1]);
4144
4145 /* ...and initialize clock/power states */
4146 bus->clkstate = CLK_SDONLY;
4147 bus->idletime = BRCMF_IDLE_INTERVAL;
4148 bus->idleclock = BRCMF_IDLE_ACTIVE;
4149
Arend van Sprielfad13222014-01-06 12:40:40 +01004150 /* SR state */
Arend van Sprielfad13222014-01-06 12:40:40 +01004151 bus->sr_enabled = false;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004152
Arend van Spriel80969832012-06-09 22:51:44 +02004153 brcmf_sdio_debugfs_create(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004154 brcmf_dbg(INFO, "completed!!\n");
4155
Hante Meuleman46d703a2015-11-25 11:32:46 +01004156 ret = brcmf_fw_map_chip_to_name(bus->ci->chip, bus->ci->chiprev,
4157 brcmf_sdio_fwnames,
4158 ARRAY_SIZE(brcmf_sdio_fwnames),
4159 sdiodev->fw_name, sdiodev->nvram_name);
Daniel Kimc1b20532014-07-12 08:49:37 +02004160 if (ret)
4161 goto fail;
4162
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02004163 ret = brcmf_fw_get_firmwares(sdiodev->dev, BRCMF_FW_REQUEST_NVRAM,
Daniel Kimc1b20532014-07-12 08:49:37 +02004164 sdiodev->fw_name, sdiodev->nvram_name,
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02004165 brcmf_sdio_firmware_callback);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004166 if (ret != 0) {
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02004167 brcmf_err("async firmware request failed: %d\n", ret);
Hante Meuleman1799ddf2012-11-14 18:46:17 -08004168 goto fail;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004169 }
Franky Lin15d45b62011-10-21 16:16:32 +02004170
Arend van Spriel5b435de2011-10-05 13:19:03 +02004171 return bus;
4172
4173fail:
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004174 brcmf_sdio_remove(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004175 return NULL;
4176}
4177
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004178/* Detach and free everything */
4179void brcmf_sdio_remove(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004180{
Arend van Spriel5b435de2011-10-05 13:19:03 +02004181 brcmf_dbg(TRACE, "Enter\n");
4182
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004183 if (bus) {
4184 /* De-register interrupt handler */
4185 brcmf_sdiod_intr_unregister(bus->sdiodev);
4186
Arend van Spriel4faf28b2014-05-27 12:56:18 +02004187 brcmf_detach(bus->sdiodev->dev);
Arend van Sprielbfad4a02014-01-06 12:40:39 +01004188
Hante Meulemane0c180ec2014-01-29 15:32:18 +01004189 cancel_work_sync(&bus->datawork);
4190 if (bus->brcmf_wq)
4191 destroy_workqueue(bus->brcmf_wq);
4192
Arend van Sprielbfad4a02014-01-06 12:40:39 +01004193 if (bus->ci) {
Arend van Spriela1ce7a02015-02-06 18:36:42 +01004194 if (bus->sdiodev->state != BRCMF_SDIOD_NOMEDIUM) {
Arend van Sprielbb350712014-01-13 22:20:29 +01004195 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel4011fc42016-01-05 11:05:47 +01004196 brcmf_sdio_wd_timer(bus, false);
Arend van Sprielbb350712014-01-13 22:20:29 +01004197 brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
4198 /* Leave the device in state where it is
Arend van Sprield380ebc2015-03-11 16:11:29 +01004199 * 'passive'. This is done by resetting all
4200 * necessary cores.
Arend van Sprielbb350712014-01-13 22:20:29 +01004201 */
4202 msleep(20);
Arend van Sprield380ebc2015-03-11 16:11:29 +01004203 brcmf_chip_set_passive(bus->ci);
Arend van Sprielbb350712014-01-13 22:20:29 +01004204 brcmf_sdio_clkctl(bus, CLK_NONE, false);
4205 sdio_release_host(bus->sdiodev->func[1]);
4206 }
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01004207 brcmf_chip_detach(bus->ci);
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004208 }
4209
Arend van Sprielbfad4a02014-01-06 12:40:39 +01004210 kfree(bus->rxbuf);
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004211 kfree(bus->hdrbuf);
4212 kfree(bus);
4213 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02004214
4215 brcmf_dbg(TRACE, "Disconnected\n");
4216}
4217
Arend van Spriel4011fc42016-01-05 11:05:47 +01004218void brcmf_sdio_wd_timer(struct brcmf_sdio *bus, bool active)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004219{
Arend van Spriel5b435de2011-10-05 13:19:03 +02004220 /* Totally stop the timer */
Arend van Spriel4011fc42016-01-05 11:05:47 +01004221 if (!active && bus->wd_active) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02004222 del_timer_sync(&bus->timer);
Arend van Spriel4011fc42016-01-05 11:05:47 +01004223 bus->wd_active = false;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004224 return;
4225 }
4226
Franky Linece960e2011-10-21 16:16:19 +02004227 /* don't start the wd until fw is loaded */
Arend van Spriela1ce7a02015-02-06 18:36:42 +01004228 if (bus->sdiodev->state != BRCMF_SDIOD_DATA)
Franky Linece960e2011-10-21 16:16:19 +02004229 return;
4230
Arend van Spriel4011fc42016-01-05 11:05:47 +01004231 if (active) {
4232 if (!bus->wd_active) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02004233 /* Create timer again when watchdog period is
4234 dynamically changed or in the first instance
4235 */
Arend van Spriel63ce3d52016-01-05 11:05:48 +01004236 bus->timer.expires = jiffies + BRCMF_WD_POLL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004237 add_timer(&bus->timer);
Arend van Spriel4011fc42016-01-05 11:05:47 +01004238 bus->wd_active = true;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004239 } else {
4240 /* Re arm the timer, at last watchdog period */
Arend van Spriel63ce3d52016-01-05 11:05:48 +01004241 mod_timer(&bus->timer, jiffies + BRCMF_WD_POLL);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004242 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02004243 }
4244}
Arend van Spriel99824642015-02-06 18:36:45 +01004245
4246int brcmf_sdio_sleep(struct brcmf_sdio *bus, bool sleep)
4247{
4248 int ret;
4249
4250 sdio_claim_host(bus->sdiodev->func[1]);
4251 ret = brcmf_sdio_bus_sleep(bus, sleep, false);
4252 sdio_release_host(bus->sdiodev->func[1]);
4253
4254 return ret;
4255}
4256