blob: 5fb8b91b9326ab9ac2fc7848e7b2c297a2086649 [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>
Arend van Spriel5b435de2011-10-05 13:19:03 +020036#include <asm/unaligned.h>
37#include <defs.h>
38#include <brcmu_wifi.h>
39#include <brcmu_utils.h>
40#include <brcm_hw_ids.h>
41#include <soc.h>
Hante Meuleman888bf76e2014-10-28 14:56:17 +010042#include "sdio.h"
Arend van Spriel20c9c9b2014-01-29 15:32:15 +010043#include "chip.h"
Arend van Sprieldabedab2014-05-27 12:56:17 +020044#include "firmware.h"
Hante Meuleman4d792892016-02-17 11:27:07 +010045#include "core.h"
46#include "common.h"
Arend van Spriel5b435de2011-10-05 13:19:03 +020047
Sjoerd Simons97f1a172016-01-25 11:47:29 +010048#define DCMD_RESP_TIMEOUT msecs_to_jiffies(2500)
49#define CTL_DONE_TIMEOUT msecs_to_jiffies(2500)
Arend van Spriel5b435de2011-10-05 13:19:03 +020050
Joe Perches8ae74652012-01-15 00:38:38 -080051#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +020052
53#define BRCMF_TRAP_INFO_SIZE 80
54
55#define CBUF_LEN (128)
56
Arend van Spriel4fc0d012012-06-14 12:16:57 +020057/* Device console log buffer state */
58#define CONSOLE_BUFFER_MAX 2024
59
Arend van Spriel5b435de2011-10-05 13:19:03 +020060struct rte_log_le {
61 __le32 buf; /* Can't be pointer on (64-bit) hosts */
62 __le32 buf_size;
63 __le32 idx;
64 char *_buf_compat; /* Redundant pointer for backward compat. */
65};
66
67struct rte_console {
68 /* Virtual UART
69 * When there is no UART (e.g. Quickturn),
70 * the host should write a complete
71 * input line directly into cbuf and then write
72 * the length into vcons_in.
73 * This may also be used when there is a real UART
74 * (at risk of conflicting with
75 * the real UART). vcons_out is currently unused.
76 */
77 uint vcons_in;
78 uint vcons_out;
79
80 /* Output (logging) buffer
81 * Console output is written to a ring buffer log_buf at index log_idx.
82 * The host may read the output when it sees log_idx advance.
83 * Output will be lost if the output wraps around faster than the host
84 * polls.
85 */
86 struct rte_log_le log_le;
87
88 /* Console input line buffer
89 * Characters are read one at a time into cbuf
90 * until <CR> is received, then
91 * the buffer is processed as a command line.
92 * Also used for virtual UART.
93 */
94 uint cbuf_idx;
95 char cbuf[CBUF_LEN];
96};
97
Joe Perches8ae74652012-01-15 00:38:38 -080098#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +020099#include <chipcommon.h>
100
Hante Meulemand14f78b2014-10-28 14:56:14 +0100101#include "bus.h"
Hante Meulemana8e8ed32014-10-28 14:56:13 +0100102#include "debug.h"
Arend van Spriel40c1c242013-04-05 10:57:44 +0200103#include "tracepoint.h"
Arend van Spriel5b435de2011-10-05 13:19:03 +0200104
105#define TXQLEN 2048 /* bulk tx queue length */
106#define TXHI (TXQLEN - 256) /* turn on flow control above TXHI */
107#define TXLOW (TXHI - 256) /* turn off flow control below TXLOW */
108#define PRIOMASK 7
109
110#define TXRETRIES 2 /* # of retries for tx frames */
111
112#define BRCMF_RXBOUND 50 /* Default for max rx frames in
113 one scheduling */
114
115#define BRCMF_TXBOUND 20 /* Default for max tx frames in
116 one scheduling */
117
118#define BRCMF_TXMINMAX 1 /* Max tx frames if rx still pending */
119
120#define MEMBLOCK 2048 /* Block size used for downloading
121 of dongle image */
122#define MAX_DATA_BUF (32 * 1024) /* Must be large enough to hold
123 biggest possible glom */
124
125#define BRCMF_FIRSTREAD (1 << 6)
126
Arend van Spriel9d6c1dc2015-08-26 22:15:01 +0200127#define BRCMF_CONSOLE 10 /* watchdog interval to poll console */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200128
129/* SBSDIO_DEVICE_CTL */
130
131/* 1: device will assert busy signal when receiving CMD53 */
132#define SBSDIO_DEVCTL_SETBUSY 0x01
133/* 1: assertion of sdio interrupt is synchronous to the sdio clock */
134#define SBSDIO_DEVCTL_SPI_INTR_SYNC 0x02
135/* 1: mask all interrupts to host except the chipActive (rev 8) */
136#define SBSDIO_DEVCTL_CA_INT_ONLY 0x04
137/* 1: isolate internal sdio signals, put external pads in tri-state; requires
138 * sdio bus power cycle to clear (rev 9) */
139#define SBSDIO_DEVCTL_PADS_ISO 0x08
140/* Force SD->SB reset mapping (rev 11) */
141#define SBSDIO_DEVCTL_SB_RST_CTL 0x30
142/* Determined by CoreControl bit */
143#define SBSDIO_DEVCTL_RST_CORECTL 0x00
144/* Force backplane reset */
145#define SBSDIO_DEVCTL_RST_BPRESET 0x10
146/* Force no backplane reset */
147#define SBSDIO_DEVCTL_RST_NOBPRESET 0x20
148
Arend van Spriel5b435de2011-10-05 13:19:03 +0200149/* direct(mapped) cis space */
150
151/* MAPPED common CIS address */
152#define SBSDIO_CIS_BASE_COMMON 0x1000
153/* maximum bytes in one CIS */
154#define SBSDIO_CIS_SIZE_LIMIT 0x200
155/* cis offset addr is < 17 bits */
156#define SBSDIO_CIS_OFT_ADDR_MASK 0x1FFFF
157
158/* manfid tuple length, include tuple, link bytes */
159#define SBSDIO_CIS_MANFID_TUPLE_LEN 6
160
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100161#define CORE_BUS_REG(base, field) \
162 (base + offsetof(struct sdpcmd_regs, field))
163
164/* SDIO function 1 register CHIPCLKCSR */
165/* Force ALP request to backplane */
166#define SBSDIO_FORCE_ALP 0x01
167/* Force HT request to backplane */
168#define SBSDIO_FORCE_HT 0x02
169/* Force ILP request to backplane */
170#define SBSDIO_FORCE_ILP 0x04
171/* Make ALP ready (power up xtal) */
172#define SBSDIO_ALP_AVAIL_REQ 0x08
173/* Make HT ready (power up PLL) */
174#define SBSDIO_HT_AVAIL_REQ 0x10
175/* Squelch clock requests from HW */
176#define SBSDIO_FORCE_HW_CLKREQ_OFF 0x20
177/* Status: ALP is ready */
178#define SBSDIO_ALP_AVAIL 0x40
179/* Status: HT is ready */
180#define SBSDIO_HT_AVAIL 0x80
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100181#define SBSDIO_CSR_MASK 0x1F
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100182#define SBSDIO_AVBITS (SBSDIO_HT_AVAIL | SBSDIO_ALP_AVAIL)
183#define SBSDIO_ALPAV(regval) ((regval) & SBSDIO_AVBITS)
184#define SBSDIO_HTAV(regval) (((regval) & SBSDIO_AVBITS) == SBSDIO_AVBITS)
185#define SBSDIO_ALPONLY(regval) (SBSDIO_ALPAV(regval) && !SBSDIO_HTAV(regval))
186#define SBSDIO_CLKAV(regval, alponly) \
187 (SBSDIO_ALPAV(regval) && (alponly ? 1 : SBSDIO_HTAV(regval)))
188
Arend van Spriel5b435de2011-10-05 13:19:03 +0200189/* intstatus */
190#define I_SMB_SW0 (1 << 0) /* To SB Mail S/W interrupt 0 */
191#define I_SMB_SW1 (1 << 1) /* To SB Mail S/W interrupt 1 */
192#define I_SMB_SW2 (1 << 2) /* To SB Mail S/W interrupt 2 */
193#define I_SMB_SW3 (1 << 3) /* To SB Mail S/W interrupt 3 */
194#define I_SMB_SW_MASK 0x0000000f /* To SB Mail S/W interrupts mask */
195#define I_SMB_SW_SHIFT 0 /* To SB Mail S/W interrupts shift */
196#define I_HMB_SW0 (1 << 4) /* To Host Mail S/W interrupt 0 */
197#define I_HMB_SW1 (1 << 5) /* To Host Mail S/W interrupt 1 */
198#define I_HMB_SW2 (1 << 6) /* To Host Mail S/W interrupt 2 */
199#define I_HMB_SW3 (1 << 7) /* To Host Mail S/W interrupt 3 */
200#define I_HMB_SW_MASK 0x000000f0 /* To Host Mail S/W interrupts mask */
201#define I_HMB_SW_SHIFT 4 /* To Host Mail S/W interrupts shift */
202#define I_WR_OOSYNC (1 << 8) /* Write Frame Out Of Sync */
203#define I_RD_OOSYNC (1 << 9) /* Read Frame Out Of Sync */
204#define I_PC (1 << 10) /* descriptor error */
205#define I_PD (1 << 11) /* data error */
206#define I_DE (1 << 12) /* Descriptor protocol Error */
207#define I_RU (1 << 13) /* Receive descriptor Underflow */
208#define I_RO (1 << 14) /* Receive fifo Overflow */
209#define I_XU (1 << 15) /* Transmit fifo Underflow */
210#define I_RI (1 << 16) /* Receive Interrupt */
211#define I_BUSPWR (1 << 17) /* SDIO Bus Power Change (rev 9) */
212#define I_XMTDATA_AVAIL (1 << 23) /* bits in fifo */
213#define I_XI (1 << 24) /* Transmit Interrupt */
214#define I_RF_TERM (1 << 25) /* Read Frame Terminate */
215#define I_WF_TERM (1 << 26) /* Write Frame Terminate */
216#define I_PCMCIA_XU (1 << 27) /* PCMCIA Transmit FIFO Underflow */
217#define I_SBINT (1 << 28) /* sbintstatus Interrupt */
218#define I_CHIPACTIVE (1 << 29) /* chip from doze to active state */
219#define I_SRESET (1 << 30) /* CCCR RES interrupt */
220#define I_IOE2 (1U << 31) /* CCCR IOE2 Bit Changed */
221#define I_ERRORS (I_PC | I_PD | I_DE | I_RU | I_RO | I_XU)
222#define I_DMA (I_RI | I_XI | I_ERRORS)
223
224/* corecontrol */
225#define CC_CISRDY (1 << 0) /* CIS Ready */
226#define CC_BPRESEN (1 << 1) /* CCCR RES signal */
227#define CC_F2RDY (1 << 2) /* set CCCR IOR2 bit */
228#define CC_CLRPADSISO (1 << 3) /* clear SDIO pads isolation */
229#define CC_XMTDATAAVAIL_MODE (1 << 4)
230#define CC_XMTDATAAVAIL_CTRL (1 << 5)
231
232/* SDA_FRAMECTRL */
233#define SFC_RF_TERM (1 << 0) /* Read Frame Terminate */
234#define SFC_WF_TERM (1 << 1) /* Write Frame Terminate */
235#define SFC_CRC4WOOS (1 << 2) /* CRC error for write out of sync */
236#define SFC_ABORTALL (1 << 3) /* Abort all in-progress frames */
237
Arend van Spriel5b435de2011-10-05 13:19:03 +0200238/*
239 * Software allocation of To SB Mailbox resources
240 */
241
242/* tosbmailbox bits corresponding to intstatus bits */
243#define SMB_NAK (1 << 0) /* Frame NAK */
244#define SMB_INT_ACK (1 << 1) /* Host Interrupt ACK */
245#define SMB_USE_OOB (1 << 2) /* Use OOB Wakeup */
246#define SMB_DEV_INT (1 << 3) /* Miscellaneous Interrupt */
247
248/* tosbmailboxdata */
249#define SMB_DATA_VERSION_SHIFT 16 /* host protocol version */
250
251/*
252 * Software allocation of To Host Mailbox resources
253 */
254
255/* intstatus bits */
256#define I_HMB_FC_STATE I_HMB_SW0 /* Flow Control State */
257#define I_HMB_FC_CHANGE I_HMB_SW1 /* Flow Control State Changed */
258#define I_HMB_FRAME_IND I_HMB_SW2 /* Frame Indication */
259#define I_HMB_HOST_INT I_HMB_SW3 /* Miscellaneous Interrupt */
260
261/* tohostmailboxdata */
262#define HMB_DATA_NAKHANDLED 1 /* retransmit NAK'd frame */
263#define HMB_DATA_DEVREADY 2 /* talk to host after enable */
264#define HMB_DATA_FC 4 /* per prio flowcontrol update flag */
265#define HMB_DATA_FWREADY 8 /* fw ready for protocol activity */
266
267#define HMB_DATA_FCDATA_MASK 0xff000000
268#define HMB_DATA_FCDATA_SHIFT 24
269
270#define HMB_DATA_VERSION_MASK 0x00ff0000
271#define HMB_DATA_VERSION_SHIFT 16
272
273/*
274 * Software-defined protocol header
275 */
276
277/* Current protocol version */
278#define SDPCM_PROT_VERSION 4
279
Arend van Spriel5b435de2011-10-05 13:19:03 +0200280/*
281 * Shared structure between dongle and the host.
282 * The structure contains pointers to trap or assert information.
283 */
Arend van Spriel4fc0d012012-06-14 12:16:57 +0200284#define SDPCM_SHARED_VERSION 0x0003
Arend van Spriel5b435de2011-10-05 13:19:03 +0200285#define SDPCM_SHARED_VERSION_MASK 0x00FF
286#define SDPCM_SHARED_ASSERT_BUILT 0x0100
287#define SDPCM_SHARED_ASSERT 0x0200
288#define SDPCM_SHARED_TRAP 0x0400
289
290/* Space for header read, limit for data packets */
291#define MAX_HDR_READ (1 << 6)
292#define MAX_RX_DATASZ 2048
293
Arend van Spriel5b435de2011-10-05 13:19:03 +0200294/* Bump up limit on waiting for HT to account for first startup;
295 * if the image is doing a CRC calculation before programming the PMU
296 * for HT availability, it could take a couple hundred ms more, so
297 * max out at a 1 second (1000000us).
298 */
299#undef PMU_MAX_TRANSITION_DLY
300#define PMU_MAX_TRANSITION_DLY 1000000
301
302/* Value for ChipClockCSR during initial setup */
303#define BRCMF_INIT_CLKCTL1 (SBSDIO_FORCE_HW_CLKREQ_OFF | \
304 SBSDIO_ALP_AVAIL_REQ)
305
306/* Flags for SDH calls */
307#define F2SYNC (SDIO_REQ_4BYTE | SDIO_REQ_FIXED)
308
Franky Lin382a9e02011-12-16 18:37:18 -0800309#define BRCMF_IDLE_ACTIVE 0 /* Do not request any SD clock change
310 * when idle
311 */
312#define BRCMF_IDLE_INTERVAL 1
313
Piotr Haber4a3da992013-04-11 13:28:46 +0200314#define KSO_WAIT_US 50
315#define MAX_KSO_ATTEMPTS (PMU_MAX_TRANSITION_DLY/KSO_WAIT_US)
316
Arend van Spriel5b435de2011-10-05 13:19:03 +0200317/*
318 * Conversion of 802.1D priority to precedence level
319 */
320static uint prio2prec(u32 prio)
321{
322 return (prio == PRIO_8021D_NONE || prio == PRIO_8021D_BE) ?
323 (prio^2) : prio;
324}
325
Joe Perches8ae74652012-01-15 00:38:38 -0800326#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200327/* Device console log buffer state */
328struct brcmf_console {
329 uint count; /* Poll interval msec counter */
330 uint log_addr; /* Log struct address (fixed) */
331 struct rte_log_le log_le; /* Log struct (host copy) */
332 uint bufsize; /* Size of log buffer */
333 u8 *buf; /* Log buffer (host copy) */
334 uint last; /* Last buffer read index */
335};
Arend van Spriel4fc0d012012-06-14 12:16:57 +0200336
337struct brcmf_trap_info {
338 __le32 type;
339 __le32 epc;
340 __le32 cpsr;
341 __le32 spsr;
342 __le32 r0; /* a1 */
343 __le32 r1; /* a2 */
344 __le32 r2; /* a3 */
345 __le32 r3; /* a4 */
346 __le32 r4; /* v1 */
347 __le32 r5; /* v2 */
348 __le32 r6; /* v3 */
349 __le32 r7; /* v4 */
350 __le32 r8; /* v5 */
351 __le32 r9; /* sb/v6 */
352 __le32 r10; /* sl/v7 */
353 __le32 r11; /* fp/v8 */
354 __le32 r12; /* ip */
355 __le32 r13; /* sp */
356 __le32 r14; /* lr */
357 __le32 pc; /* r15 */
358};
Joe Perches8ae74652012-01-15 00:38:38 -0800359#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200360
361struct sdpcm_shared {
362 u32 flags;
363 u32 trap_addr;
364 u32 assert_exp_addr;
365 u32 assert_file_addr;
366 u32 assert_line;
367 u32 console_addr; /* Address of struct rte_console */
368 u32 msgtrace_addr;
369 u8 tag[32];
Arend van Spriel4fc0d012012-06-14 12:16:57 +0200370 u32 brpt_addr;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200371};
372
373struct sdpcm_shared_le {
374 __le32 flags;
375 __le32 trap_addr;
376 __le32 assert_exp_addr;
377 __le32 assert_file_addr;
378 __le32 assert_line;
379 __le32 console_addr; /* Address of struct rte_console */
380 __le32 msgtrace_addr;
381 u8 tag[32];
Arend van Spriel4fc0d012012-06-14 12:16:57 +0200382 __le32 brpt_addr;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200383};
384
Franky Lin6bc52312013-08-10 12:27:28 +0200385/* dongle SDIO bus specific header info */
386struct brcmf_sdio_hdrinfo {
Franky Lin4754fce2012-09-19 22:21:11 +0200387 u8 seq_num;
388 u8 channel;
389 u16 len;
390 u16 len_left;
391 u16 len_nxtfrm;
392 u8 dat_offset;
Franky Lin8da9d2c2013-11-29 12:25:10 +0100393 bool lastfrm;
394 u16 tail_pad;
Franky Lin4754fce2012-09-19 22:21:11 +0200395};
Arend van Spriel5b435de2011-10-05 13:19:03 +0200396
Arend van Spriel82d957e2014-07-12 08:49:36 +0200397/*
398 * hold counter variables
399 */
400struct brcmf_sdio_count {
401 uint intrcount; /* Count of device interrupt callbacks */
402 uint lastintrs; /* Count as of last watchdog timer */
403 uint pollcnt; /* Count of active polls */
404 uint regfails; /* Count of R_REG failures */
405 uint tx_sderrs; /* Count of tx attempts with sd errors */
406 uint fcqueued; /* Tx packets that got queued */
407 uint rxrtx; /* Count of rtx requests (NAK to dongle) */
408 uint rx_toolong; /* Receive frames too long to receive */
409 uint rxc_errors; /* SDIO errors when reading control frames */
410 uint rx_hdrfail; /* SDIO errors on header reads */
411 uint rx_badhdr; /* Bad received headers (roosync?) */
412 uint rx_badseq; /* Mismatched rx sequence number */
413 uint fc_rcvd; /* Number of flow-control events received */
414 uint fc_xoff; /* Number which turned on flow-control */
415 uint fc_xon; /* Number which turned off flow-control */
416 uint rxglomfail; /* Failed deglom attempts */
417 uint rxglomframes; /* Number of glom frames (superframes) */
418 uint rxglompkts; /* Number of packets from glom frames */
419 uint f2rxhdrs; /* Number of header reads */
420 uint f2rxdata; /* Number of frame data reads */
421 uint f2txdata; /* Number of f2 frame writes */
422 uint f1regdata; /* Number of f1 register accesses */
423 uint tickcnt; /* Number of watchdog been schedule */
424 ulong tx_ctlerrs; /* Err of sending ctrl frames */
425 ulong tx_ctlpkts; /* Ctrl frames sent to dongle */
426 ulong rx_ctlerrs; /* Err of processing rx ctrl frames */
427 ulong rx_ctlpkts; /* Ctrl frames processed from dongle */
428 ulong rx_readahead_cnt; /* packets where header read-ahead was used */
429};
430
Arend van Spriel5b435de2011-10-05 13:19:03 +0200431/* misc chip info needed by some of the routines */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200432/* Private data for SDIO bus interaction */
Franky Line92eedf2011-11-22 17:21:50 -0800433struct brcmf_sdio {
Arend van Spriel5b435de2011-10-05 13:19:03 +0200434 struct brcmf_sdio_dev *sdiodev; /* sdio device handler */
Arend van Spriel9cf218f2014-01-13 22:20:27 +0100435 struct brcmf_chip *ci; /* Chip info struct */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200436
Arend van Spriel5b435de2011-10-05 13:19:03 +0200437 u32 hostintmask; /* Copy of Host Interrupt Mask */
Franky Lin45316032012-09-13 21:12:03 +0200438 atomic_t intstatus; /* Intstatus bits (events) pending */
439 atomic_t fcstate; /* State of dongle flow-control */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200440
441 uint blocksize; /* Block size of SDIO transfers */
442 uint roundup; /* Max roundup limit */
443
444 struct pktq txq; /* Queue length used for flow-control */
445 u8 flowcontrol; /* per prio flow control bitmask */
446 u8 tx_seq; /* Transmit sequence number (next) */
447 u8 tx_max; /* Maximum transmit sequence allowed */
448
Arend van Spriel9b2d2f22013-11-29 12:25:14 +0100449 u8 *hdrbuf; /* buffer for handling rx frame */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200450 u8 *rxhdr; /* Header of current rx frame (in hdrbuf) */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200451 u8 rx_seq; /* Receive sequence number (expected) */
Franky Lin6bc52312013-08-10 12:27:28 +0200452 struct brcmf_sdio_hdrinfo cur_read;
Franky Lin4754fce2012-09-19 22:21:11 +0200453 /* info of current read frame */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200454 bool rxskip; /* Skip receive (awaiting NAK ACK) */
Franky Lin4754fce2012-09-19 22:21:11 +0200455 bool rxpending; /* Data frame pending in dongle */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200456
457 uint rxbound; /* Rx frames to read before resched */
458 uint txbound; /* Tx frames to send before resched */
459 uint txminmax;
460
461 struct sk_buff *glomd; /* Packet containing glomming descriptor */
Arend van Sprielb83db862011-10-19 12:51:09 +0200462 struct sk_buff_head glom; /* Packet list for glommed superframe */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200463
464 u8 *rxbuf; /* Buffer for receiving control packets */
465 uint rxblen; /* Allocated length of rxbuf */
466 u8 *rxctl; /* Aligned pointer into rxbuf */
Franky Lindd43a012012-11-05 16:22:22 -0800467 u8 *rxctl_orig; /* pointer for freeing rxctl */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200468 uint rxlen; /* Length of valid data in buffer */
Franky Lindd43a012012-11-05 16:22:22 -0800469 spinlock_t rxctl_lock; /* protection lock for ctrl frame resources */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200470
471 u8 sdpcm_ver; /* Bus protocol reported by dongle */
472
473 bool intr; /* Use interrupts */
474 bool poll; /* Use polling */
Franky Lin1d382272012-09-13 21:11:59 +0200475 atomic_t ipend; /* Device interrupt is pending */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200476 uint spurious; /* Count of spurious interrupts */
477 uint pollrate; /* Ticks between device polls */
478 uint polltick; /* Tick counter */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200479
Joe Perches8ae74652012-01-15 00:38:38 -0800480#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200481 uint console_interval;
482 struct brcmf_console console; /* Console output polling support */
483 uint console_addr; /* Console address from shared struct */
Joe Perches8ae74652012-01-15 00:38:38 -0800484#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200485
Arend van Spriel5b435de2011-10-05 13:19:03 +0200486 uint clkstate; /* State of sd and backplane clock(s) */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200487 s32 idletime; /* Control for activity timeout */
Hante Meulemanb441ba82015-03-06 18:40:39 +0100488 s32 idlecount; /* Activity timeout counter */
489 s32 idleclock; /* How to set bus driver when idle */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200490 bool rxflow_mode; /* Rx flow control mode */
491 bool rxflow; /* Is rx flow control on */
492 bool alp_only; /* Don't use HT clock (ALP only) */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200493
Arend van Spriel5b435de2011-10-05 13:19:03 +0200494 u8 *ctrl_frame_buf;
Hante Meulemanfed7ec42014-03-15 17:18:18 +0100495 u16 ctrl_frame_len;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200496 bool ctrl_frame_stat;
Hante Meuleman4dd8b262015-01-25 20:31:32 +0100497 int ctrl_frame_err;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200498
Hante Meulemanfed7ec42014-03-15 17:18:18 +0100499 spinlock_t txq_lock; /* protect bus->txq */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200500 wait_queue_head_t ctrl_wait;
501 wait_queue_head_t dcmd_resp_wait;
502
503 struct timer_list timer;
504 struct completion watchdog_wait;
505 struct task_struct *watchdog_tsk;
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
Arend van Spriel5b435de2011-10-05 13:19:03 +0200538/* Limit on rounding up frames */
539static const uint max_roundup = 512;
540
541#define ALIGNMENT 4
542
Franky Lin9d7d6f92012-10-22 10:36:24 -0700543enum brcmf_sdio_frmtype {
544 BRCMF_SDIO_FT_NORMAL,
545 BRCMF_SDIO_FT_SUPER,
546 BRCMF_SDIO_FT_SUB,
547};
548
Arend van Spriel65d80d02014-01-29 15:32:14 +0100549#define SDIOD_DRVSTR_KEY(chip, pmu) (((chip) << 16) | (pmu))
550
551/* SDIO Pad drive strength to select value mappings */
552struct sdiod_drive_str {
553 u8 strength; /* Pad Drive Strength in mA */
554 u8 sel; /* Chip-specific select value */
555};
556
557/* SDIO Drive Strength to sel value table for PMU Rev 11 (1.8V) */
558static const struct sdiod_drive_str sdiod_drvstr_tab1_1v8[] = {
559 {32, 0x6},
560 {26, 0x7},
561 {22, 0x4},
562 {16, 0x5},
563 {12, 0x2},
564 {8, 0x3},
565 {4, 0x0},
566 {0, 0x1}
567};
568
569/* SDIO Drive Strength to sel value table for PMU Rev 13 (1.8v) */
570static const struct sdiod_drive_str sdiod_drive_strength_tab5_1v8[] = {
571 {6, 0x7},
572 {5, 0x6},
573 {4, 0x5},
574 {3, 0x4},
575 {2, 0x2},
576 {1, 0x1},
577 {0, 0x0}
578};
579
580/* SDIO Drive Strength to sel value table for PMU Rev 17 (1.8v) */
581static const struct sdiod_drive_str sdiod_drvstr_tab6_1v8[] = {
582 {3, 0x3},
583 {2, 0x2},
584 {1, 0x1},
585 {0, 0x0} };
586
587/* SDIO Drive Strength to sel value table for 43143 PMU Rev 17 (3.3V) */
588static const struct sdiod_drive_str sdiod_drvstr_tab2_3v3[] = {
589 {16, 0x7},
590 {12, 0x5},
591 {8, 0x3},
592 {4, 0x1}
593};
594
Hante Meuleman46d703a2015-11-25 11:32:46 +0100595BRCMF_FW_NVRAM_DEF(43143, "brcmfmac43143-sdio.bin", "brcmfmac43143-sdio.txt");
596BRCMF_FW_NVRAM_DEF(43241B0, "brcmfmac43241b0-sdio.bin",
597 "brcmfmac43241b0-sdio.txt");
598BRCMF_FW_NVRAM_DEF(43241B4, "brcmfmac43241b4-sdio.bin",
599 "brcmfmac43241b4-sdio.txt");
600BRCMF_FW_NVRAM_DEF(43241B5, "brcmfmac43241b5-sdio.bin",
601 "brcmfmac43241b5-sdio.txt");
602BRCMF_FW_NVRAM_DEF(4329, "brcmfmac4329-sdio.bin", "brcmfmac4329-sdio.txt");
603BRCMF_FW_NVRAM_DEF(4330, "brcmfmac4330-sdio.bin", "brcmfmac4330-sdio.txt");
604BRCMF_FW_NVRAM_DEF(4334, "brcmfmac4334-sdio.bin", "brcmfmac4334-sdio.txt");
605BRCMF_FW_NVRAM_DEF(43340, "brcmfmac43340-sdio.bin", "brcmfmac43340-sdio.txt");
606BRCMF_FW_NVRAM_DEF(4335, "brcmfmac4335-sdio.bin", "brcmfmac4335-sdio.txt");
607BRCMF_FW_NVRAM_DEF(43362, "brcmfmac43362-sdio.bin", "brcmfmac43362-sdio.txt");
608BRCMF_FW_NVRAM_DEF(4339, "brcmfmac4339-sdio.bin", "brcmfmac4339-sdio.txt");
609BRCMF_FW_NVRAM_DEF(43430, "brcmfmac43430-sdio.bin", "brcmfmac43430-sdio.txt");
610BRCMF_FW_NVRAM_DEF(43455, "brcmfmac43455-sdio.bin", "brcmfmac43455-sdio.txt");
611BRCMF_FW_NVRAM_DEF(4354, "brcmfmac4354-sdio.bin", "brcmfmac4354-sdio.txt");
Christian Daudt496aec52016-05-04 17:55:20 -0700612BRCMF_FW_NVRAM_DEF(4356, "brcmfmac4356-sdio.bin", "brcmfmac4356-sdio.txt");
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200613
Hante Meuleman46d703a2015-11-25 11:32:46 +0100614static struct brcmf_firmware_mapping brcmf_sdio_fwnames[] = {
615 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_43143_CHIP_ID, 0xFFFFFFFF, 43143),
616 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_43241_CHIP_ID, 0x0000001F, 43241B0),
617 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_43241_CHIP_ID, 0x00000020, 43241B4),
618 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_43241_CHIP_ID, 0xFFFFFFC0, 43241B5),
619 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_4329_CHIP_ID, 0xFFFFFFFF, 4329),
620 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_4330_CHIP_ID, 0xFFFFFFFF, 4330),
621 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_4334_CHIP_ID, 0xFFFFFFFF, 4334),
622 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_43340_CHIP_ID, 0xFFFFFFFF, 43340),
623 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_4335_CHIP_ID, 0xFFFFFFFF, 4335),
624 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_43362_CHIP_ID, 0xFFFFFFFE, 43362),
625 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_4339_CHIP_ID, 0xFFFFFFFF, 4339),
626 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_43430_CHIP_ID, 0xFFFFFFFF, 43430),
627 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_4345_CHIP_ID, 0xFFFFFFC0, 43455),
Christian Daudt496aec52016-05-04 17:55:20 -0700628 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_4354_CHIP_ID, 0xFFFFFFFF, 4354),
629 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_4356_CHIP_ID, 0xFFFFFFFF, 4356)
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200630};
631
Arend van Spriel5b435de2011-10-05 13:19:03 +0200632static void pkt_align(struct sk_buff *p, int len, int align)
633{
634 uint datalign;
635 datalign = (unsigned long)(p->data);
636 datalign = roundup(datalign, (align)) - datalign;
637 if (datalign)
638 skb_pull(p, datalign);
639 __skb_trim(p, len);
640}
641
642/* To check if there's window offered */
Franky Line92eedf2011-11-22 17:21:50 -0800643static bool data_ok(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200644{
645 return (u8)(bus->tx_max - bus->tx_seq) != 0 &&
646 ((u8)(bus->tx_max - bus->tx_seq) & 0x80) == 0;
647}
648
649/*
650 * Reads a register in the SDIO hardware block. This block occupies a series of
651 * adresses on the 32 bit backplane bus.
652 */
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100653static int r_sdreg32(struct brcmf_sdio *bus, u32 *regvar, u32 offset)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200654{
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100655 struct brcmf_core *core;
Franky Lin79ae3952012-05-04 18:27:34 -0700656 int ret;
Franky Lin58692752012-05-04 18:27:36 -0700657
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100658 core = brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV);
659 *regvar = brcmf_sdiod_regrl(bus->sdiodev, core->base + offset, &ret);
Franky Lin58692752012-05-04 18:27:36 -0700660
661 return ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200662}
663
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100664static int w_sdreg32(struct brcmf_sdio *bus, u32 regval, u32 reg_offset)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200665{
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100666 struct brcmf_core *core;
Franky Line13ce262012-05-04 18:27:35 -0700667 int ret;
Franky Lin58692752012-05-04 18:27:36 -0700668
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100669 core = brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV);
670 brcmf_sdiod_regwl(bus->sdiodev, core->base + reg_offset, regval, &ret);
Franky Lin58692752012-05-04 18:27:36 -0700671
672 return ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200673}
674
Piotr Haber4a3da992013-04-11 13:28:46 +0200675static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100676brcmf_sdio_kso_control(struct brcmf_sdio *bus, bool on)
Piotr Haber4a3da992013-04-11 13:28:46 +0200677{
678 u8 wr_val = 0, rd_val, cmp_val, bmask;
679 int err = 0;
680 int try_cnt = 0;
681
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100682 brcmf_dbg(TRACE, "Enter: on=%d\n", on);
Piotr Haber4a3da992013-04-11 13:28:46 +0200683
684 wr_val = (on << SBSDIO_FUNC1_SLEEPCSR_KSO_SHIFT);
685 /* 1st KSO write goes to AOS wake up core if device is asleep */
Arend van Spriela39be272013-12-12 11:58:58 +0100686 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
687 wr_val, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +0200688
689 if (on) {
690 /* device WAKEUP through KSO:
691 * write bit 0 & read back until
692 * both bits 0 (kso bit) & 1 (dev on status) are set
693 */
694 cmp_val = SBSDIO_FUNC1_SLEEPCSR_KSO_MASK |
695 SBSDIO_FUNC1_SLEEPCSR_DEVON_MASK;
696 bmask = cmp_val;
697 usleep_range(2000, 3000);
698 } else {
699 /* Put device to sleep, turn off KSO */
700 cmp_val = 0;
701 /* only check for bit0, bit1(dev on status) may not
702 * get cleared right away
703 */
704 bmask = SBSDIO_FUNC1_SLEEPCSR_KSO_MASK;
705 }
706
707 do {
708 /* reliable KSO bit set/clr:
709 * the sdiod sleep write access is synced to PMU 32khz clk
710 * just one write attempt may fail,
711 * read it back until it matches written value
712 */
Arend van Spriela39be272013-12-12 11:58:58 +0100713 rd_val = brcmf_sdiod_regrb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
714 &err);
Piotr Haber4a3da992013-04-11 13:28:46 +0200715 if (((rd_val & bmask) == cmp_val) && !err)
716 break;
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100717
Piotr Haber4a3da992013-04-11 13:28:46 +0200718 udelay(KSO_WAIT_US);
Arend van Spriela39be272013-12-12 11:58:58 +0100719 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
720 wr_val, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +0200721 } while (try_cnt++ < MAX_KSO_ATTEMPTS);
722
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100723 if (try_cnt > 2)
724 brcmf_dbg(SDIO, "try_cnt=%d rd_val=0x%x err=%d\n", try_cnt,
725 rd_val, err);
726
727 if (try_cnt > MAX_KSO_ATTEMPTS)
728 brcmf_err("max tries: rd_val=0x%x err=%d\n", rd_val, err);
729
Piotr Haber4a3da992013-04-11 13:28:46 +0200730 return err;
731}
732
Arend van Spriel5b435de2011-10-05 13:19:03 +0200733#define HOSTINTMASK (I_HMB_SW_MASK | I_CHIPACTIVE)
734
Arend van Spriel5b435de2011-10-05 13:19:03 +0200735/* Turn backplane clock on or off */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100736static int brcmf_sdio_htclk(struct brcmf_sdio *bus, bool on, bool pendok)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200737{
738 int err;
739 u8 clkctl, clkreq, devctl;
740 unsigned long timeout;
741
Arend van Sprielc3203372013-04-03 12:40:44 +0200742 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200743
744 clkctl = 0;
745
Piotr Haber4a3da992013-04-11 13:28:46 +0200746 if (bus->sr_enabled) {
747 bus->clkstate = (on ? CLK_AVAIL : CLK_SDONLY);
748 return 0;
749 }
750
Arend van Spriel5b435de2011-10-05 13:19:03 +0200751 if (on) {
752 /* Request HT Avail */
753 clkreq =
754 bus->alp_only ? SBSDIO_ALP_AVAIL_REQ : SBSDIO_HT_AVAIL_REQ;
755
Arend van Spriela39be272013-12-12 11:58:58 +0100756 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
757 clkreq, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200758 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100759 brcmf_err("HT Avail request error: %d\n", err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200760 return -EBADE;
761 }
762
Arend van Spriel5b435de2011-10-05 13:19:03 +0200763 /* Check current status */
Arend van Spriela39be272013-12-12 11:58:58 +0100764 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
765 SBSDIO_FUNC1_CHIPCLKCSR, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200766 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100767 brcmf_err("HT Avail read error: %d\n", err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200768 return -EBADE;
769 }
770
771 /* Go to pending and await interrupt if appropriate */
772 if (!SBSDIO_CLKAV(clkctl, bus->alp_only) && pendok) {
773 /* Allow only clock-available interrupt */
Arend van Spriela39be272013-12-12 11:58:58 +0100774 devctl = brcmf_sdiod_regrb(bus->sdiodev,
775 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200776 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100777 brcmf_err("Devctl error setting CA: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200778 err);
779 return -EBADE;
780 }
781
782 devctl |= SBSDIO_DEVCTL_CA_INT_ONLY;
Arend van Spriela39be272013-12-12 11:58:58 +0100783 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
784 devctl, &err);
Arend van Sprielc3203372013-04-03 12:40:44 +0200785 brcmf_dbg(SDIO, "CLKCTL: set PENDING\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200786 bus->clkstate = CLK_PENDING;
787
788 return 0;
789 } else if (bus->clkstate == CLK_PENDING) {
790 /* Cancel CA-only interrupt filter */
Arend van Spriela39be272013-12-12 11:58:58 +0100791 devctl = brcmf_sdiod_regrb(bus->sdiodev,
792 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200793 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
Arend van Spriela39be272013-12-12 11:58:58 +0100794 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
795 devctl, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200796 }
797
798 /* Otherwise, wait here (polling) for HT Avail */
799 timeout = jiffies +
800 msecs_to_jiffies(PMU_MAX_TRANSITION_DLY/1000);
801 while (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
Arend van Spriela39be272013-12-12 11:58:58 +0100802 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
803 SBSDIO_FUNC1_CHIPCLKCSR,
804 &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200805 if (time_after(jiffies, timeout))
806 break;
807 else
808 usleep_range(5000, 10000);
809 }
810 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100811 brcmf_err("HT Avail request error: %d\n", err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200812 return -EBADE;
813 }
814 if (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100815 brcmf_err("HT Avail timeout (%d): clkctl 0x%02x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200816 PMU_MAX_TRANSITION_DLY, clkctl);
817 return -EBADE;
818 }
819
820 /* Mark clock available */
821 bus->clkstate = CLK_AVAIL;
Arend van Sprielc3203372013-04-03 12:40:44 +0200822 brcmf_dbg(SDIO, "CLKCTL: turned ON\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200823
Joe Perches8ae74652012-01-15 00:38:38 -0800824#if defined(DEBUG)
Joe Perches23677ce2012-02-09 11:17:23 +0000825 if (!bus->alp_only) {
Arend van Spriel5b435de2011-10-05 13:19:03 +0200826 if (SBSDIO_ALPONLY(clkctl))
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100827 brcmf_err("HT Clock should be on\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200828 }
Joe Perches8ae74652012-01-15 00:38:38 -0800829#endif /* defined (DEBUG) */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200830
Arend van Spriel5b435de2011-10-05 13:19:03 +0200831 } else {
832 clkreq = 0;
833
834 if (bus->clkstate == CLK_PENDING) {
835 /* Cancel CA-only interrupt filter */
Arend van Spriela39be272013-12-12 11:58:58 +0100836 devctl = brcmf_sdiod_regrb(bus->sdiodev,
837 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200838 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
Arend van Spriela39be272013-12-12 11:58:58 +0100839 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
840 devctl, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200841 }
842
843 bus->clkstate = CLK_SDONLY;
Arend van Spriela39be272013-12-12 11:58:58 +0100844 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
845 clkreq, &err);
Arend van Sprielc3203372013-04-03 12:40:44 +0200846 brcmf_dbg(SDIO, "CLKCTL: turned OFF\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200847 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100848 brcmf_err("Failed access turning clock off: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200849 err);
850 return -EBADE;
851 }
852 }
853 return 0;
854}
855
856/* Change idle/active SD state */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100857static int brcmf_sdio_sdclk(struct brcmf_sdio *bus, bool on)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200858{
Arend van Sprielc3203372013-04-03 12:40:44 +0200859 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200860
861 if (on)
862 bus->clkstate = CLK_SDONLY;
863 else
864 bus->clkstate = CLK_NONE;
865
866 return 0;
867}
868
869/* Transition SD and backplane clock readiness */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100870static int brcmf_sdio_clkctl(struct brcmf_sdio *bus, uint target, bool pendok)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200871{
Joe Perches8ae74652012-01-15 00:38:38 -0800872#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200873 uint oldstate = bus->clkstate;
Joe Perches8ae74652012-01-15 00:38:38 -0800874#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200875
Arend van Sprielc3203372013-04-03 12:40:44 +0200876 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200877
878 /* Early exit if we're already there */
Hante Meulemanb441ba82015-03-06 18:40:39 +0100879 if (bus->clkstate == target)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200880 return 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200881
882 switch (target) {
883 case CLK_AVAIL:
884 /* Make sure SD clock is available */
885 if (bus->clkstate == CLK_NONE)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100886 brcmf_sdio_sdclk(bus, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200887 /* Now request HT Avail on the backplane */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100888 brcmf_sdio_htclk(bus, true, pendok);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200889 break;
890
891 case CLK_SDONLY:
892 /* Remove HT request, or bring up SD clock */
893 if (bus->clkstate == CLK_NONE)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100894 brcmf_sdio_sdclk(bus, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200895 else if (bus->clkstate == CLK_AVAIL)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100896 brcmf_sdio_htclk(bus, false, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200897 else
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100898 brcmf_err("request for %d -> %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200899 bus->clkstate, target);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200900 break;
901
902 case CLK_NONE:
903 /* Make sure to remove HT request */
904 if (bus->clkstate == CLK_AVAIL)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100905 brcmf_sdio_htclk(bus, false, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200906 /* Now remove the SD clock */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100907 brcmf_sdio_sdclk(bus, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200908 break;
909 }
Joe Perches8ae74652012-01-15 00:38:38 -0800910#ifdef DEBUG
Arend van Sprielc3203372013-04-03 12:40:44 +0200911 brcmf_dbg(SDIO, "%d -> %d\n", oldstate, bus->clkstate);
Joe Perches8ae74652012-01-15 00:38:38 -0800912#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200913
914 return 0;
915}
916
Piotr Haber4a3da992013-04-11 13:28:46 +0200917static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100918brcmf_sdio_bus_sleep(struct brcmf_sdio *bus, bool sleep, bool pendok)
Piotr Haber4a3da992013-04-11 13:28:46 +0200919{
920 int err = 0;
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100921 u8 clkcsr;
Arend van Spriel82030d62014-01-29 15:32:20 +0100922
923 brcmf_dbg(SDIO, "Enter: request %s currently %s\n",
Piotr Haber4a3da992013-04-11 13:28:46 +0200924 (sleep ? "SLEEP" : "WAKE"),
Arend van Spriel99824642015-02-06 18:36:45 +0100925 (bus->sleeping ? "SLEEP" : "WAKE"));
Piotr Haber4a3da992013-04-11 13:28:46 +0200926
927 /* If SR is enabled control bus state with KSO */
928 if (bus->sr_enabled) {
929 /* Done if we're already in the requested state */
Arend van Spriel99824642015-02-06 18:36:45 +0100930 if (sleep == bus->sleeping)
Piotr Haber4a3da992013-04-11 13:28:46 +0200931 goto end;
932
933 /* Going to sleep */
934 if (sleep) {
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100935 clkcsr = brcmf_sdiod_regrb(bus->sdiodev,
936 SBSDIO_FUNC1_CHIPCLKCSR,
937 &err);
938 if ((clkcsr & SBSDIO_CSR_MASK) == 0) {
939 brcmf_dbg(SDIO, "no clock, set ALP\n");
940 brcmf_sdiod_regwb(bus->sdiodev,
941 SBSDIO_FUNC1_CHIPCLKCSR,
942 SBSDIO_ALP_AVAIL_REQ, &err);
943 }
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100944 err = brcmf_sdio_kso_control(bus, false);
Piotr Haber4a3da992013-04-11 13:28:46 +0200945 } else {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100946 err = brcmf_sdio_kso_control(bus, true);
Piotr Haber4a3da992013-04-11 13:28:46 +0200947 }
Arend van Spriel8982cd42015-01-25 20:31:34 +0100948 if (err) {
Piotr Haber4a3da992013-04-11 13:28:46 +0200949 brcmf_err("error while changing bus sleep state %d\n",
950 err);
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100951 goto done;
Piotr Haber4a3da992013-04-11 13:28:46 +0200952 }
953 }
954
955end:
956 /* control clocks */
957 if (sleep) {
958 if (!bus->sr_enabled)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100959 brcmf_sdio_clkctl(bus, CLK_NONE, pendok);
Piotr Haber4a3da992013-04-11 13:28:46 +0200960 } else {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100961 brcmf_sdio_clkctl(bus, CLK_AVAIL, pendok);
Arend van Spriel4011fc42016-01-05 11:05:47 +0100962 brcmf_sdio_wd_timer(bus, true);
Piotr Haber4a3da992013-04-11 13:28:46 +0200963 }
Arend van Spriel99824642015-02-06 18:36:45 +0100964 bus->sleeping = sleep;
Arend van Spriel8982cd42015-01-25 20:31:34 +0100965 brcmf_dbg(SDIO, "new state %s\n",
966 (sleep ? "SLEEP" : "WAKE"));
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100967done:
968 brcmf_dbg(SDIO, "Exit: err=%d\n", err);
Piotr Haber4a3da992013-04-11 13:28:46 +0200969 return err;
970
971}
972
Daniel Kim0801e6c2014-01-29 15:32:13 +0100973#ifdef DEBUG
974static inline bool brcmf_sdio_valid_shared_address(u32 addr)
975{
976 return !(addr == 0 || ((~addr >> 16) & 0xffff) == (addr & 0xffff));
977}
978
979static int brcmf_sdio_readshared(struct brcmf_sdio *bus,
980 struct sdpcm_shared *sh)
981{
Arend van Spriel9819a902015-03-11 16:11:32 +0100982 u32 addr = 0;
Daniel Kim0801e6c2014-01-29 15:32:13 +0100983 int rv;
984 u32 shaddr = 0;
985 struct sdpcm_shared_le sh_le;
986 __le32 addr_le;
987
Arend van Spriel9819a902015-03-11 16:11:32 +0100988 sdio_claim_host(bus->sdiodev->func[1]);
989 brcmf_sdio_bus_sleep(bus, false, false);
Daniel Kim0801e6c2014-01-29 15:32:13 +0100990
991 /*
992 * Read last word in socram to determine
993 * address of sdpcm_shared structure
994 */
Arend van Spriel9819a902015-03-11 16:11:32 +0100995 shaddr = bus->ci->rambase + bus->ci->ramsize - 4;
996 if (!bus->ci->rambase && brcmf_chip_sr_capable(bus->ci))
997 shaddr -= bus->ci->srsize;
998 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, shaddr,
999 (u8 *)&addr_le, 4);
Daniel Kim0801e6c2014-01-29 15:32:13 +01001000 if (rv < 0)
Arend van Spriel9819a902015-03-11 16:11:32 +01001001 goto fail;
Daniel Kim0801e6c2014-01-29 15:32:13 +01001002
1003 /*
1004 * Check if addr is valid.
1005 * NVRAM length at the end of memory should have been overwritten.
1006 */
Arend van Spriel9819a902015-03-11 16:11:32 +01001007 addr = le32_to_cpu(addr_le);
Daniel Kim0801e6c2014-01-29 15:32:13 +01001008 if (!brcmf_sdio_valid_shared_address(addr)) {
Arend van Spriel9819a902015-03-11 16:11:32 +01001009 brcmf_err("invalid sdpcm_shared address 0x%08X\n", addr);
1010 rv = -EINVAL;
1011 goto fail;
Daniel Kim0801e6c2014-01-29 15:32:13 +01001012 }
1013
Arend van Spriel9819a902015-03-11 16:11:32 +01001014 brcmf_dbg(INFO, "sdpcm_shared address 0x%08X\n", addr);
1015
Daniel Kim0801e6c2014-01-29 15:32:13 +01001016 /* Read hndrte_shared structure */
1017 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, (u8 *)&sh_le,
1018 sizeof(struct sdpcm_shared_le));
1019 if (rv < 0)
Arend van Spriel9819a902015-03-11 16:11:32 +01001020 goto fail;
1021
1022 sdio_release_host(bus->sdiodev->func[1]);
Daniel Kim0801e6c2014-01-29 15:32:13 +01001023
1024 /* Endianness */
1025 sh->flags = le32_to_cpu(sh_le.flags);
1026 sh->trap_addr = le32_to_cpu(sh_le.trap_addr);
1027 sh->assert_exp_addr = le32_to_cpu(sh_le.assert_exp_addr);
1028 sh->assert_file_addr = le32_to_cpu(sh_le.assert_file_addr);
1029 sh->assert_line = le32_to_cpu(sh_le.assert_line);
1030 sh->console_addr = le32_to_cpu(sh_le.console_addr);
1031 sh->msgtrace_addr = le32_to_cpu(sh_le.msgtrace_addr);
1032
1033 if ((sh->flags & SDPCM_SHARED_VERSION_MASK) > SDPCM_SHARED_VERSION) {
1034 brcmf_err("sdpcm shared version unsupported: dhd %d dongle %d\n",
1035 SDPCM_SHARED_VERSION,
1036 sh->flags & SDPCM_SHARED_VERSION_MASK);
1037 return -EPROTO;
1038 }
Daniel Kim0801e6c2014-01-29 15:32:13 +01001039 return 0;
Arend van Spriel9819a902015-03-11 16:11:32 +01001040
1041fail:
1042 brcmf_err("unable to obtain sdpcm_shared info: rv=%d (addr=0x%x)\n",
1043 rv, addr);
1044 sdio_release_host(bus->sdiodev->func[1]);
1045 return rv;
Daniel Kim0801e6c2014-01-29 15:32:13 +01001046}
1047
1048static void brcmf_sdio_get_console_addr(struct brcmf_sdio *bus)
1049{
1050 struct sdpcm_shared sh;
1051
1052 if (brcmf_sdio_readshared(bus, &sh) == 0)
1053 bus->console_addr = sh.console_addr;
1054}
1055#else
1056static void brcmf_sdio_get_console_addr(struct brcmf_sdio *bus)
1057{
1058}
1059#endif /* DEBUG */
1060
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001061static u32 brcmf_sdio_hostmail(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001062{
1063 u32 intstatus = 0;
1064 u32 hmb_data;
1065 u8 fcbits;
Franky Lin58692752012-05-04 18:27:36 -07001066 int ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001067
Arend van Sprielc3203372013-04-03 12:40:44 +02001068 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001069
1070 /* Read mailbox data and ack that we did so */
Franky Lin58692752012-05-04 18:27:36 -07001071 ret = r_sdreg32(bus, &hmb_data,
1072 offsetof(struct sdpcmd_regs, tohostmailboxdata));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001073
Franky Lin58692752012-05-04 18:27:36 -07001074 if (ret == 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001075 w_sdreg32(bus, SMB_INT_ACK,
Franky Lin58692752012-05-04 18:27:36 -07001076 offsetof(struct sdpcmd_regs, tosbmailbox));
Arend van Spriel80969832012-06-09 22:51:44 +02001077 bus->sdcnt.f1regdata += 2;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001078
1079 /* Dongle recomposed rx frames, accept them again */
1080 if (hmb_data & HMB_DATA_NAKHANDLED) {
Arend van Sprielc3203372013-04-03 12:40:44 +02001081 brcmf_dbg(SDIO, "Dongle reports NAK handled, expect rtx of %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001082 bus->rx_seq);
1083 if (!bus->rxskip)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001084 brcmf_err("unexpected NAKHANDLED!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001085
1086 bus->rxskip = false;
1087 intstatus |= I_HMB_FRAME_IND;
1088 }
1089
1090 /*
1091 * DEVREADY does not occur with gSPI.
1092 */
1093 if (hmb_data & (HMB_DATA_DEVREADY | HMB_DATA_FWREADY)) {
1094 bus->sdpcm_ver =
1095 (hmb_data & HMB_DATA_VERSION_MASK) >>
1096 HMB_DATA_VERSION_SHIFT;
1097 if (bus->sdpcm_ver != SDPCM_PROT_VERSION)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001098 brcmf_err("Version mismatch, dongle reports %d, "
Arend van Spriel5b435de2011-10-05 13:19:03 +02001099 "expecting %d\n",
1100 bus->sdpcm_ver, SDPCM_PROT_VERSION);
1101 else
Arend van Sprielc3203372013-04-03 12:40:44 +02001102 brcmf_dbg(SDIO, "Dongle ready, protocol version %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001103 bus->sdpcm_ver);
Daniel Kim0801e6c2014-01-29 15:32:13 +01001104
1105 /*
1106 * Retrieve console state address now that firmware should have
1107 * updated it.
1108 */
1109 brcmf_sdio_get_console_addr(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001110 }
1111
1112 /*
1113 * Flow Control has been moved into the RX headers and this out of band
1114 * method isn't used any more.
1115 * remaining backward compatible with older dongles.
1116 */
1117 if (hmb_data & HMB_DATA_FC) {
1118 fcbits = (hmb_data & HMB_DATA_FCDATA_MASK) >>
1119 HMB_DATA_FCDATA_SHIFT;
1120
1121 if (fcbits & ~bus->flowcontrol)
Arend van Spriel80969832012-06-09 22:51:44 +02001122 bus->sdcnt.fc_xoff++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001123
1124 if (bus->flowcontrol & ~fcbits)
Arend van Spriel80969832012-06-09 22:51:44 +02001125 bus->sdcnt.fc_xon++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001126
Arend van Spriel80969832012-06-09 22:51:44 +02001127 bus->sdcnt.fc_rcvd++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001128 bus->flowcontrol = fcbits;
1129 }
1130
1131 /* Shouldn't be any others */
1132 if (hmb_data & ~(HMB_DATA_DEVREADY |
1133 HMB_DATA_NAKHANDLED |
1134 HMB_DATA_FC |
1135 HMB_DATA_FWREADY |
1136 HMB_DATA_FCDATA_MASK | HMB_DATA_VERSION_MASK))
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001137 brcmf_err("Unknown mailbox data content: 0x%02x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001138 hmb_data);
1139
1140 return intstatus;
1141}
1142
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001143static void brcmf_sdio_rxfail(struct brcmf_sdio *bus, bool abort, bool rtx)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001144{
1145 uint retries = 0;
1146 u16 lastrbc;
1147 u8 hi, lo;
1148 int err;
1149
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001150 brcmf_err("%sterminate frame%s\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001151 abort ? "abort command, " : "",
1152 rtx ? ", send NAK" : "");
1153
1154 if (abort)
Arend van Spriela39be272013-12-12 11:58:58 +01001155 brcmf_sdiod_abort(bus->sdiodev, SDIO_FUNC_2);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001156
Arend van Spriela39be272013-12-12 11:58:58 +01001157 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL,
1158 SFC_RF_TERM, &err);
Arend van Spriel80969832012-06-09 22:51:44 +02001159 bus->sdcnt.f1regdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001160
1161 /* Wait until the packet has been flushed (device/FIFO stable) */
1162 for (lastrbc = retries = 0xffff; retries > 0; retries--) {
Arend van Spriela39be272013-12-12 11:58:58 +01001163 hi = brcmf_sdiod_regrb(bus->sdiodev,
1164 SBSDIO_FUNC1_RFRAMEBCHI, &err);
1165 lo = brcmf_sdiod_regrb(bus->sdiodev,
1166 SBSDIO_FUNC1_RFRAMEBCLO, &err);
Arend van Spriel80969832012-06-09 22:51:44 +02001167 bus->sdcnt.f1regdata += 2;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001168
1169 if ((hi == 0) && (lo == 0))
1170 break;
1171
1172 if ((hi > (lastrbc >> 8)) && (lo > (lastrbc & 0x00ff))) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001173 brcmf_err("count growing: last 0x%04x now 0x%04x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001174 lastrbc, (hi << 8) + lo);
1175 }
1176 lastrbc = (hi << 8) + lo;
1177 }
1178
1179 if (!retries)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001180 brcmf_err("count never zeroed: last 0x%04x\n", lastrbc);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001181 else
Arend van Sprielc3203372013-04-03 12:40:44 +02001182 brcmf_dbg(SDIO, "flush took %d iterations\n", 0xffff - retries);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001183
1184 if (rtx) {
Arend van Spriel80969832012-06-09 22:51:44 +02001185 bus->sdcnt.rxrtx++;
Franky Lin58692752012-05-04 18:27:36 -07001186 err = w_sdreg32(bus, SMB_NAK,
1187 offsetof(struct sdpcmd_regs, tosbmailbox));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001188
Arend van Spriel80969832012-06-09 22:51:44 +02001189 bus->sdcnt.f1regdata++;
Franky Lin58692752012-05-04 18:27:36 -07001190 if (err == 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001191 bus->rxskip = true;
1192 }
1193
1194 /* Clear partial in any case */
Franky Lin4754fce2012-09-19 22:21:11 +02001195 bus->cur_read.len = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001196}
1197
Hante Meuleman81c78832014-02-25 20:30:35 +01001198static void brcmf_sdio_txfail(struct brcmf_sdio *bus)
1199{
1200 struct brcmf_sdio_dev *sdiodev = bus->sdiodev;
1201 u8 i, hi, lo;
1202
1203 /* On failure, abort the command and terminate the frame */
1204 brcmf_err("sdio error, abort command and terminate frame\n");
1205 bus->sdcnt.tx_sderrs++;
1206
1207 brcmf_sdiod_abort(sdiodev, SDIO_FUNC_2);
1208 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_FRAMECTRL, SFC_WF_TERM, NULL);
1209 bus->sdcnt.f1regdata++;
1210
1211 for (i = 0; i < 3; i++) {
1212 hi = brcmf_sdiod_regrb(sdiodev, SBSDIO_FUNC1_WFRAMEBCHI, NULL);
1213 lo = brcmf_sdiod_regrb(sdiodev, SBSDIO_FUNC1_WFRAMEBCLO, NULL);
1214 bus->sdcnt.f1regdata += 2;
1215 if ((hi == 0) && (lo == 0))
1216 break;
1217 }
1218}
1219
Arend van Spriel9a95e602011-11-10 20:30:29 +01001220/* return total length of buffer chain */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001221static uint brcmf_sdio_glom_len(struct brcmf_sdio *bus)
Arend van Spriel9a95e602011-11-10 20:30:29 +01001222{
1223 struct sk_buff *p;
1224 uint total;
1225
1226 total = 0;
1227 skb_queue_walk(&bus->glom, p)
1228 total += p->len;
1229 return total;
1230}
1231
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001232static void brcmf_sdio_free_glom(struct brcmf_sdio *bus)
Arend van Spriel046808d2011-11-10 20:30:31 +01001233{
1234 struct sk_buff *cur, *next;
1235
1236 skb_queue_walk_safe(&bus->glom, cur, next) {
1237 skb_unlink(cur, &bus->glom);
1238 brcmu_pkt_buf_free_skb(cur);
1239 }
1240}
1241
Franky Lin6bc52312013-08-10 12:27:28 +02001242/**
1243 * brcmfmac sdio bus specific header
1244 * This is the lowest layer header wrapped on the packets transmitted between
1245 * host and WiFi dongle which contains information needed for SDIO core and
1246 * firmware
1247 *
Franky Lin8da9d2c2013-11-29 12:25:10 +01001248 * It consists of 3 parts: hardware header, hardware extension header and
1249 * software header
Franky Lin6bc52312013-08-10 12:27:28 +02001250 * hardware header (frame tag) - 4 bytes
1251 * Byte 0~1: Frame length
1252 * Byte 2~3: Checksum, bit-wise inverse of frame length
Franky Lin8da9d2c2013-11-29 12:25:10 +01001253 * hardware extension header - 8 bytes
1254 * Tx glom mode only, N/A for Rx or normal Tx
1255 * Byte 0~1: Packet length excluding hw frame tag
1256 * Byte 2: Reserved
1257 * Byte 3: Frame flags, bit 0: last frame indication
1258 * Byte 4~5: Reserved
1259 * Byte 6~7: Tail padding length
Franky Lin6bc52312013-08-10 12:27:28 +02001260 * software header - 8 bytes
1261 * Byte 0: Rx/Tx sequence number
1262 * Byte 1: 4 MSB Channel number, 4 LSB arbitrary flag
1263 * Byte 2: Length of next data frame, reserved for Tx
1264 * Byte 3: Data offset
1265 * Byte 4: Flow control bits, reserved for Tx
1266 * Byte 5: Maximum Sequence number allowed by firmware for Tx, N/A for Tx packet
1267 * Byte 6~7: Reserved
1268 */
1269#define SDPCM_HWHDR_LEN 4
Franky Lin8da9d2c2013-11-29 12:25:10 +01001270#define SDPCM_HWEXT_LEN 8
Franky Lin6bc52312013-08-10 12:27:28 +02001271#define SDPCM_SWHDR_LEN 8
1272#define SDPCM_HDRLEN (SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN)
Franky Lin6bc52312013-08-10 12:27:28 +02001273/* software header */
1274#define SDPCM_SEQ_MASK 0x000000ff
1275#define SDPCM_SEQ_WRAP 256
1276#define SDPCM_CHANNEL_MASK 0x00000f00
1277#define SDPCM_CHANNEL_SHIFT 8
1278#define SDPCM_CONTROL_CHANNEL 0 /* Control */
1279#define SDPCM_EVENT_CHANNEL 1 /* Asyc Event Indication */
1280#define SDPCM_DATA_CHANNEL 2 /* Data Xmit/Recv */
1281#define SDPCM_GLOM_CHANNEL 3 /* Coalesced packets */
1282#define SDPCM_TEST_CHANNEL 15 /* Test/debug packets */
1283#define SDPCM_GLOMDESC(p) (((u8 *)p)[1] & 0x80)
1284#define SDPCM_NEXTLEN_MASK 0x00ff0000
1285#define SDPCM_NEXTLEN_SHIFT 16
1286#define SDPCM_DOFFSET_MASK 0xff000000
1287#define SDPCM_DOFFSET_SHIFT 24
1288#define SDPCM_FCMASK_MASK 0x000000ff
1289#define SDPCM_WINDOW_MASK 0x0000ff00
1290#define SDPCM_WINDOW_SHIFT 8
1291
1292static inline u8 brcmf_sdio_getdatoffset(u8 *swheader)
1293{
1294 u32 hdrvalue;
1295 hdrvalue = *(u32 *)swheader;
1296 return (u8)((hdrvalue & SDPCM_DOFFSET_MASK) >> SDPCM_DOFFSET_SHIFT);
1297}
1298
Franky Linc56caa9d2016-04-11 11:35:25 +02001299static inline bool brcmf_sdio_fromevntchan(u8 *swheader)
1300{
1301 u32 hdrvalue;
1302 u8 ret;
1303
1304 hdrvalue = *(u32 *)swheader;
1305 ret = (u8)((hdrvalue & SDPCM_CHANNEL_MASK) >> SDPCM_CHANNEL_SHIFT);
1306
1307 return (ret == SDPCM_EVENT_CHANNEL);
1308}
1309
Franky Lin6bc52312013-08-10 12:27:28 +02001310static int brcmf_sdio_hdparse(struct brcmf_sdio *bus, u8 *header,
1311 struct brcmf_sdio_hdrinfo *rd,
1312 enum brcmf_sdio_frmtype type)
Franky Lin4754fce2012-09-19 22:21:11 +02001313{
1314 u16 len, checksum;
1315 u8 rx_seq, fc, tx_seq_max;
Franky Lin6bc52312013-08-10 12:27:28 +02001316 u32 swheader;
Franky Lin4754fce2012-09-19 22:21:11 +02001317
Arend van Spriel4b776962013-11-29 12:25:06 +01001318 trace_brcmf_sdpcm_hdr(SDPCM_RX, header);
Arend van Spriel76584ec2013-10-15 15:44:47 +02001319
Franky Lin6bc52312013-08-10 12:27:28 +02001320 /* hw header */
Franky Lin4754fce2012-09-19 22:21:11 +02001321 len = get_unaligned_le16(header);
1322 checksum = get_unaligned_le16(header + sizeof(u16));
1323 /* All zero means no more to read */
1324 if (!(len | checksum)) {
1325 bus->rxpending = false;
Franky Lin10510582012-11-05 16:22:26 -08001326 return -ENODATA;
Franky Lin4754fce2012-09-19 22:21:11 +02001327 }
1328 if ((u16)(~(len ^ checksum))) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001329 brcmf_err("HW header checksum error\n");
Franky Lin4754fce2012-09-19 22:21:11 +02001330 bus->sdcnt.rx_badhdr++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001331 brcmf_sdio_rxfail(bus, false, false);
Franky Lin10510582012-11-05 16:22:26 -08001332 return -EIO;
Franky Lin4754fce2012-09-19 22:21:11 +02001333 }
1334 if (len < SDPCM_HDRLEN) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001335 brcmf_err("HW header length error\n");
Franky Lin10510582012-11-05 16:22:26 -08001336 return -EPROTO;
Franky Lin4754fce2012-09-19 22:21:11 +02001337 }
Franky Lin9d7d6f92012-10-22 10:36:24 -07001338 if (type == BRCMF_SDIO_FT_SUPER &&
1339 (roundup(len, bus->blocksize) != rd->len)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001340 brcmf_err("HW superframe header length error\n");
Franky Lin10510582012-11-05 16:22:26 -08001341 return -EPROTO;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001342 }
1343 if (type == BRCMF_SDIO_FT_SUB && len > rd->len) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001344 brcmf_err("HW subframe header length error\n");
Franky Lin10510582012-11-05 16:22:26 -08001345 return -EPROTO;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001346 }
Franky Lin4754fce2012-09-19 22:21:11 +02001347 rd->len = len;
1348
Franky Lin6bc52312013-08-10 12:27:28 +02001349 /* software header */
1350 header += SDPCM_HWHDR_LEN;
1351 swheader = le32_to_cpu(*(__le32 *)header);
1352 if (type == BRCMF_SDIO_FT_SUPER && SDPCM_GLOMDESC(header)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001353 brcmf_err("Glom descriptor found in superframe head\n");
Franky Lin9d7d6f92012-10-22 10:36:24 -07001354 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001355 return -EINVAL;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001356 }
Franky Lin6bc52312013-08-10 12:27:28 +02001357 rx_seq = (u8)(swheader & SDPCM_SEQ_MASK);
1358 rd->channel = (swheader & SDPCM_CHANNEL_MASK) >> SDPCM_CHANNEL_SHIFT;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001359 if (len > MAX_RX_DATASZ && rd->channel != SDPCM_CONTROL_CHANNEL &&
1360 type != BRCMF_SDIO_FT_SUPER) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001361 brcmf_err("HW header length too long\n");
Franky Lin4754fce2012-09-19 22:21:11 +02001362 bus->sdcnt.rx_toolong++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001363 brcmf_sdio_rxfail(bus, false, false);
Franky Lin4754fce2012-09-19 22:21:11 +02001364 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001365 return -EPROTO;
Franky Lin4754fce2012-09-19 22:21:11 +02001366 }
Franky Lin9d7d6f92012-10-22 10:36:24 -07001367 if (type == BRCMF_SDIO_FT_SUPER && rd->channel != SDPCM_GLOM_CHANNEL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001368 brcmf_err("Wrong channel for superframe\n");
Franky Lin9d7d6f92012-10-22 10:36:24 -07001369 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001370 return -EINVAL;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001371 }
1372 if (type == BRCMF_SDIO_FT_SUB && rd->channel != SDPCM_DATA_CHANNEL &&
1373 rd->channel != SDPCM_EVENT_CHANNEL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001374 brcmf_err("Wrong channel for subframe\n");
Franky Lin9d7d6f92012-10-22 10:36:24 -07001375 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001376 return -EINVAL;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001377 }
Franky Lin6bc52312013-08-10 12:27:28 +02001378 rd->dat_offset = brcmf_sdio_getdatoffset(header);
Franky Lin4754fce2012-09-19 22:21:11 +02001379 if (rd->dat_offset < SDPCM_HDRLEN || rd->dat_offset > rd->len) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001380 brcmf_err("seq %d: bad data offset\n", rx_seq);
Franky Lin4754fce2012-09-19 22:21:11 +02001381 bus->sdcnt.rx_badhdr++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001382 brcmf_sdio_rxfail(bus, false, false);
Franky Lin4754fce2012-09-19 22:21:11 +02001383 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001384 return -ENXIO;
Franky Lin4754fce2012-09-19 22:21:11 +02001385 }
1386 if (rd->seq_num != rx_seq) {
Wright Feng98aff6c2016-06-03 23:31:11 +02001387 brcmf_dbg(SDIO, "seq %d, expected %d\n", rx_seq, rd->seq_num);
Franky Lin4754fce2012-09-19 22:21:11 +02001388 bus->sdcnt.rx_badseq++;
1389 rd->seq_num = rx_seq;
1390 }
Franky Lin9d7d6f92012-10-22 10:36:24 -07001391 /* no need to check the reset for subframe */
1392 if (type == BRCMF_SDIO_FT_SUB)
Franky Lin10510582012-11-05 16:22:26 -08001393 return 0;
Franky Lin6bc52312013-08-10 12:27:28 +02001394 rd->len_nxtfrm = (swheader & SDPCM_NEXTLEN_MASK) >> SDPCM_NEXTLEN_SHIFT;
Franky Lin4754fce2012-09-19 22:21:11 +02001395 if (rd->len_nxtfrm << 4 > MAX_RX_DATASZ) {
1396 /* only warm for NON glom packet */
1397 if (rd->channel != SDPCM_GLOM_CHANNEL)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001398 brcmf_err("seq %d: next length error\n", rx_seq);
Franky Lin4754fce2012-09-19 22:21:11 +02001399 rd->len_nxtfrm = 0;
1400 }
Franky Lin6bc52312013-08-10 12:27:28 +02001401 swheader = le32_to_cpu(*(__le32 *)(header + 4));
1402 fc = swheader & SDPCM_FCMASK_MASK;
Franky Lin4754fce2012-09-19 22:21:11 +02001403 if (bus->flowcontrol != fc) {
1404 if (~bus->flowcontrol & fc)
1405 bus->sdcnt.fc_xoff++;
1406 if (bus->flowcontrol & ~fc)
1407 bus->sdcnt.fc_xon++;
1408 bus->sdcnt.fc_rcvd++;
1409 bus->flowcontrol = fc;
1410 }
Franky Lin6bc52312013-08-10 12:27:28 +02001411 tx_seq_max = (swheader & SDPCM_WINDOW_MASK) >> SDPCM_WINDOW_SHIFT;
Franky Lin4754fce2012-09-19 22:21:11 +02001412 if ((u8)(tx_seq_max - bus->tx_seq) > 0x40) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001413 brcmf_err("seq %d: max tx seq number error\n", rx_seq);
Franky Lin4754fce2012-09-19 22:21:11 +02001414 tx_seq_max = bus->tx_seq + 2;
1415 }
1416 bus->tx_max = tx_seq_max;
1417
Franky Lin10510582012-11-05 16:22:26 -08001418 return 0;
Franky Lin4754fce2012-09-19 22:21:11 +02001419}
1420
Franky Lin6bc52312013-08-10 12:27:28 +02001421static inline void brcmf_sdio_update_hwhdr(u8 *header, u16 frm_length)
1422{
1423 *(__le16 *)header = cpu_to_le16(frm_length);
1424 *(((__le16 *)header) + 1) = cpu_to_le16(~frm_length);
1425}
1426
1427static void brcmf_sdio_hdpack(struct brcmf_sdio *bus, u8 *header,
1428 struct brcmf_sdio_hdrinfo *hd_info)
1429{
Franky Lin8da9d2c2013-11-29 12:25:10 +01001430 u32 hdrval;
1431 u8 hdr_offset;
Franky Lin6bc52312013-08-10 12:27:28 +02001432
1433 brcmf_sdio_update_hwhdr(header, hd_info->len);
Franky Lin8da9d2c2013-11-29 12:25:10 +01001434 hdr_offset = SDPCM_HWHDR_LEN;
Franky Lin6bc52312013-08-10 12:27:28 +02001435
Franky Lin8da9d2c2013-11-29 12:25:10 +01001436 if (bus->txglom) {
1437 hdrval = (hd_info->len - hdr_offset) | (hd_info->lastfrm << 24);
1438 *((__le32 *)(header + hdr_offset)) = cpu_to_le32(hdrval);
1439 hdrval = (u16)hd_info->tail_pad << 16;
1440 *(((__le32 *)(header + hdr_offset)) + 1) = cpu_to_le32(hdrval);
1441 hdr_offset += SDPCM_HWEXT_LEN;
1442 }
1443
1444 hdrval = hd_info->seq_num;
1445 hdrval |= (hd_info->channel << SDPCM_CHANNEL_SHIFT) &
1446 SDPCM_CHANNEL_MASK;
1447 hdrval |= (hd_info->dat_offset << SDPCM_DOFFSET_SHIFT) &
1448 SDPCM_DOFFSET_MASK;
1449 *((__le32 *)(header + hdr_offset)) = cpu_to_le32(hdrval);
1450 *(((__le32 *)(header + hdr_offset)) + 1) = 0;
1451 trace_brcmf_sdpcm_hdr(SDPCM_TX + !!(bus->txglom), header);
Franky Lin6bc52312013-08-10 12:27:28 +02001452}
1453
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001454static u8 brcmf_sdio_rxglom(struct brcmf_sdio *bus, u8 rxseq)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001455{
1456 u16 dlen, totlen;
1457 u8 *dptr, num = 0;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001458 u16 sublen;
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001459 struct sk_buff *pfirst, *pnext;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001460
1461 int errcode;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001462 u8 doff, sfdoff;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001463
Franky Lin6bc52312013-08-10 12:27:28 +02001464 struct brcmf_sdio_hdrinfo rd_new;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001465
1466 /* If packets, issue read(s) and send up packet chain */
1467 /* Return sequence numbers consumed? */
1468
Arend van Sprielc3203372013-04-03 12:40:44 +02001469 brcmf_dbg(SDIO, "start: glomd %p glom %p\n",
Arend van Sprielb83db862011-10-19 12:51:09 +02001470 bus->glomd, skb_peek(&bus->glom));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001471
1472 /* If there's a descriptor, generate the packet chain */
1473 if (bus->glomd) {
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001474 pfirst = pnext = NULL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001475 dlen = (u16) (bus->glomd->len);
1476 dptr = bus->glomd->data;
1477 if (!dlen || (dlen & 1)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001478 brcmf_err("bad glomd len(%d), ignore descriptor\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001479 dlen);
1480 dlen = 0;
1481 }
1482
1483 for (totlen = num = 0; dlen; num++) {
1484 /* Get (and move past) next length */
1485 sublen = get_unaligned_le16(dptr);
1486 dlen -= sizeof(u16);
1487 dptr += sizeof(u16);
1488 if ((sublen < SDPCM_HDRLEN) ||
1489 ((num == 0) && (sublen < (2 * SDPCM_HDRLEN)))) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001490 brcmf_err("descriptor len %d bad: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001491 num, sublen);
1492 pnext = NULL;
1493 break;
1494 }
Arend van Spriele217d1c2013-11-29 12:25:13 +01001495 if (sublen % bus->sgentry_align) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001496 brcmf_err("sublen %d not multiple of %d\n",
Arend van Spriele217d1c2013-11-29 12:25:13 +01001497 sublen, bus->sgentry_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001498 }
1499 totlen += sublen;
1500
1501 /* For last frame, adjust read len so total
1502 is a block multiple */
1503 if (!dlen) {
1504 sublen +=
1505 (roundup(totlen, bus->blocksize) - totlen);
1506 totlen = roundup(totlen, bus->blocksize);
1507 }
1508
1509 /* Allocate/chain packet for next subframe */
Arend van Spriele217d1c2013-11-29 12:25:13 +01001510 pnext = brcmu_pkt_buf_get_skb(sublen + bus->sgentry_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001511 if (pnext == NULL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001512 brcmf_err("bcm_pkt_buf_get_skb failed, num %d len %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001513 num, sublen);
1514 break;
1515 }
Arend van Sprielb83db862011-10-19 12:51:09 +02001516 skb_queue_tail(&bus->glom, pnext);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001517
1518 /* Adhere to start alignment requirements */
Arend van Spriele217d1c2013-11-29 12:25:13 +01001519 pkt_align(pnext, sublen, bus->sgentry_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001520 }
1521
1522 /* If all allocations succeeded, save packet chain
1523 in bus structure */
1524 if (pnext) {
1525 brcmf_dbg(GLOM, "allocated %d-byte packet chain for %d subframes\n",
1526 totlen, num);
Franky Lin4754fce2012-09-19 22:21:11 +02001527 if (BRCMF_GLOM_ON() && bus->cur_read.len &&
1528 totlen != bus->cur_read.len) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001529 brcmf_dbg(GLOM, "glomdesc mismatch: nextlen %d glomdesc %d rxseq %d\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001530 bus->cur_read.len, totlen, rxseq);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001531 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02001532 pfirst = pnext = NULL;
1533 } else {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001534 brcmf_sdio_free_glom(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001535 num = 0;
1536 }
1537
1538 /* Done with descriptor packet */
1539 brcmu_pkt_buf_free_skb(bus->glomd);
1540 bus->glomd = NULL;
Franky Lin4754fce2012-09-19 22:21:11 +02001541 bus->cur_read.len = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001542 }
1543
1544 /* Ok -- either we just generated a packet chain,
1545 or had one from before */
Arend van Sprielb83db862011-10-19 12:51:09 +02001546 if (!skb_queue_empty(&bus->glom)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001547 if (BRCMF_GLOM_ON()) {
1548 brcmf_dbg(GLOM, "try superframe read, packet chain:\n");
Arend van Sprielb83db862011-10-19 12:51:09 +02001549 skb_queue_walk(&bus->glom, pnext) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001550 brcmf_dbg(GLOM, " %p: %p len 0x%04x (%d)\n",
1551 pnext, (u8 *) (pnext->data),
1552 pnext->len, pnext->len);
1553 }
1554 }
1555
Arend van Sprielb83db862011-10-19 12:51:09 +02001556 pfirst = skb_peek(&bus->glom);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001557 dlen = (u16) brcmf_sdio_glom_len(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001558
1559 /* Do an SDIO read for the superframe. Configurable iovar to
1560 * read directly into the chained packet, or allocate a large
1561 * packet and and copy into the chain.
1562 */
Franky Lin38b0b0d2012-11-05 16:22:24 -08001563 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriela39be272013-12-12 11:58:58 +01001564 errcode = brcmf_sdiod_recv_chain(bus->sdiodev,
Arend van Spriela39be272013-12-12 11:58:58 +01001565 &bus->glom, dlen);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001566 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel80969832012-06-09 22:51:44 +02001567 bus->sdcnt.f2rxdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001568
Franky Lin64d66c32015-11-25 11:32:40 +01001569 /* On failure, kill the superframe */
Arend van Spriel5b435de2011-10-05 13:19:03 +02001570 if (errcode < 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001571 brcmf_err("glom read of %d bytes failed: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001572 dlen, errcode);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001573
Franky Lin38b0b0d2012-11-05 16:22:24 -08001574 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin64d66c32015-11-25 11:32:40 +01001575 brcmf_sdio_rxfail(bus, true, false);
1576 bus->sdcnt.rxglomfail++;
1577 brcmf_sdio_free_glom(bus);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001578 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001579 return 0;
1580 }
Joe Perches1e023822012-01-15 00:38:40 -08001581
1582 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
1583 pfirst->data, min_t(int, pfirst->len, 48),
1584 "SUPERFRAME:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001585
Franky Lin9d7d6f92012-10-22 10:36:24 -07001586 rd_new.seq_num = rxseq;
1587 rd_new.len = dlen;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001588 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin6bc52312013-08-10 12:27:28 +02001589 errcode = brcmf_sdio_hdparse(bus, pfirst->data, &rd_new,
1590 BRCMF_SDIO_FT_SUPER);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001591 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin9d7d6f92012-10-22 10:36:24 -07001592 bus->cur_read.len = rd_new.len_nxtfrm << 4;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001593
1594 /* Remove superframe header, remember offset */
Franky Lin9d7d6f92012-10-22 10:36:24 -07001595 skb_pull(pfirst, rd_new.dat_offset);
1596 sfdoff = rd_new.dat_offset;
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001597 num = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001598
1599 /* Validate all the subframe headers */
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001600 skb_queue_walk(&bus->glom, pnext) {
1601 /* leave when invalid subframe is found */
1602 if (errcode)
1603 break;
1604
Franky Lin9d7d6f92012-10-22 10:36:24 -07001605 rd_new.len = pnext->len;
1606 rd_new.seq_num = rxseq++;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001607 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin6bc52312013-08-10 12:27:28 +02001608 errcode = brcmf_sdio_hdparse(bus, pnext->data, &rd_new,
1609 BRCMF_SDIO_FT_SUB);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001610 sdio_release_host(bus->sdiodev->func[1]);
Joe Perches1e023822012-01-15 00:38:40 -08001611 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
Franky Lin9d7d6f92012-10-22 10:36:24 -07001612 pnext->data, 32, "subframe:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001613
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001614 num++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001615 }
1616
1617 if (errcode) {
Franky Lin64d66c32015-11-25 11:32:40 +01001618 /* Terminate frame on error */
Franky Lin38b0b0d2012-11-05 16:22:24 -08001619 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin64d66c32015-11-25 11:32:40 +01001620 brcmf_sdio_rxfail(bus, true, false);
1621 bus->sdcnt.rxglomfail++;
1622 brcmf_sdio_free_glom(bus);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001623 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001624 bus->cur_read.len = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001625 return 0;
1626 }
1627
1628 /* Basic SD framing looks ok - process each packet (header) */
Arend van Spriel5b435de2011-10-05 13:19:03 +02001629
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001630 skb_queue_walk_safe(&bus->glom, pfirst, pnext) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001631 dptr = (u8 *) (pfirst->data);
1632 sublen = get_unaligned_le16(dptr);
Franky Lin6bc52312013-08-10 12:27:28 +02001633 doff = brcmf_sdio_getdatoffset(&dptr[SDPCM_HWHDR_LEN]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001634
Joe Perches1e023822012-01-15 00:38:40 -08001635 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_DATA_ON(),
Franky Lin9d7d6f92012-10-22 10:36:24 -07001636 dptr, pfirst->len,
1637 "Rx Subframe Data:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001638
1639 __skb_trim(pfirst, sublen);
1640 skb_pull(pfirst, doff);
1641
1642 if (pfirst->len == 0) {
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001643 skb_unlink(pfirst, &bus->glom);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001644 brcmu_pkt_buf_free_skb(pfirst);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001645 continue;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001646 }
1647
Joe Perches1e023822012-01-15 00:38:40 -08001648 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
1649 pfirst->data,
1650 min_t(int, pfirst->len, 32),
1651 "subframe %d to stack, %p (%p/%d) nxt/lnk %p/%p\n",
1652 bus->glom.qlen, pfirst, pfirst->data,
1653 pfirst->len, pfirst->next,
1654 pfirst->prev);
Arend van Spriel05f38202013-09-25 13:05:46 +02001655 skb_unlink(pfirst, &bus->glom);
Franky Linc56caa9d2016-04-11 11:35:25 +02001656 if (brcmf_sdio_fromevntchan(pfirst->data))
1657 brcmf_rx_event(bus->sdiodev->dev, pfirst);
1658 else
1659 brcmf_rx_frame(bus->sdiodev->dev, pfirst,
1660 false);
Arend van Spriel05f38202013-09-25 13:05:46 +02001661 bus->sdcnt.rxglompkts++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001662 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02001663
Arend van Spriel80969832012-06-09 22:51:44 +02001664 bus->sdcnt.rxglomframes++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001665 }
1666 return num;
1667}
1668
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001669static int brcmf_sdio_dcmd_resp_wait(struct brcmf_sdio *bus, uint *condition,
1670 bool *pending)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001671{
1672 DECLARE_WAITQUEUE(wait, current);
Arend van Spriel63ce3d52016-01-05 11:05:48 +01001673 int timeout = DCMD_RESP_TIMEOUT;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001674
1675 /* Wait until control frame is available */
1676 add_wait_queue(&bus->dcmd_resp_wait, &wait);
1677 set_current_state(TASK_INTERRUPTIBLE);
1678
1679 while (!(*condition) && (!signal_pending(current) && timeout))
1680 timeout = schedule_timeout(timeout);
1681
1682 if (signal_pending(current))
1683 *pending = true;
1684
1685 set_current_state(TASK_RUNNING);
1686 remove_wait_queue(&bus->dcmd_resp_wait, &wait);
1687
1688 return timeout;
1689}
1690
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001691static int brcmf_sdio_dcmd_resp_wake(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001692{
Kosuke Tatsukawaa7decc42015-12-10 13:43:08 +01001693 wake_up_interruptible(&bus->dcmd_resp_wait);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001694
1695 return 0;
1696}
1697static void
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001698brcmf_sdio_read_control(struct brcmf_sdio *bus, u8 *hdr, uint len, uint doff)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001699{
1700 uint rdlen, pad;
Franky Lindd43a012012-11-05 16:22:22 -08001701 u8 *buf = NULL, *rbuf;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001702 int sdret;
1703
1704 brcmf_dbg(TRACE, "Enter\n");
1705
Franky Lindd43a012012-11-05 16:22:22 -08001706 if (bus->rxblen)
1707 buf = vzalloc(bus->rxblen);
Joe Perches14f8dc42013-02-07 11:46:27 +00001708 if (!buf)
Franky Lindd43a012012-11-05 16:22:22 -08001709 goto done;
Joe Perches14f8dc42013-02-07 11:46:27 +00001710
Franky Lindd43a012012-11-05 16:22:22 -08001711 rbuf = bus->rxbuf;
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01001712 pad = ((unsigned long)rbuf % bus->head_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001713 if (pad)
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01001714 rbuf += (bus->head_align - pad);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001715
1716 /* Copy the already-read portion over */
Franky Lindd43a012012-11-05 16:22:22 -08001717 memcpy(buf, hdr, BRCMF_FIRSTREAD);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001718 if (len <= BRCMF_FIRSTREAD)
1719 goto gotpkt;
1720
1721 /* Raise rdlen to next SDIO block to avoid tail command */
1722 rdlen = len - BRCMF_FIRSTREAD;
1723 if (bus->roundup && bus->blocksize && (rdlen > bus->blocksize)) {
1724 pad = bus->blocksize - (rdlen % bus->blocksize);
1725 if ((pad <= bus->roundup) && (pad < bus->blocksize) &&
Franky Linb01a6b32011-12-16 18:37:03 -08001726 ((len + pad) < bus->sdiodev->bus_if->maxctl))
Arend van Spriel5b435de2011-10-05 13:19:03 +02001727 rdlen += pad;
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01001728 } else if (rdlen % bus->head_align) {
1729 rdlen += bus->head_align - (rdlen % bus->head_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001730 }
1731
Arend van Spriel5b435de2011-10-05 13:19:03 +02001732 /* Drop if the read is too big or it exceeds our maximum */
Franky Linb01a6b32011-12-16 18:37:03 -08001733 if ((rdlen + BRCMF_FIRSTREAD) > bus->sdiodev->bus_if->maxctl) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001734 brcmf_err("%d-byte control read exceeds %d-byte buffer\n",
Franky Linb01a6b32011-12-16 18:37:03 -08001735 rdlen, bus->sdiodev->bus_if->maxctl);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001736 brcmf_sdio_rxfail(bus, false, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001737 goto done;
1738 }
1739
Franky Linb01a6b32011-12-16 18:37:03 -08001740 if ((len - doff) > bus->sdiodev->bus_if->maxctl) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001741 brcmf_err("%d-byte ctl frame (%d-byte ctl data) exceeds %d-byte limit\n",
Franky Linb01a6b32011-12-16 18:37:03 -08001742 len, len - doff, bus->sdiodev->bus_if->maxctl);
Arend van Spriel80969832012-06-09 22:51:44 +02001743 bus->sdcnt.rx_toolong++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001744 brcmf_sdio_rxfail(bus, false, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001745 goto done;
1746 }
1747
Franky Lindd43a012012-11-05 16:22:22 -08001748 /* Read remain of frame body */
Arend van Spriela7cdd822013-12-12 11:58:59 +01001749 sdret = brcmf_sdiod_recv_buf(bus->sdiodev, rbuf, rdlen);
Arend van Spriel80969832012-06-09 22:51:44 +02001750 bus->sdcnt.f2rxdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001751
1752 /* Control frame failures need retransmission */
1753 if (sdret < 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001754 brcmf_err("read %d control bytes failed: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001755 rdlen, sdret);
Arend van Spriel80969832012-06-09 22:51:44 +02001756 bus->sdcnt.rxc_errors++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001757 brcmf_sdio_rxfail(bus, true, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001758 goto done;
Franky Lindd43a012012-11-05 16:22:22 -08001759 } else
1760 memcpy(buf + BRCMF_FIRSTREAD, rbuf, rdlen);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001761
1762gotpkt:
1763
Joe Perches1e023822012-01-15 00:38:40 -08001764 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_CTL_ON(),
Franky Lindd43a012012-11-05 16:22:22 -08001765 buf, len, "RxCtrl:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001766
1767 /* Point to valid data and indicate its length */
Franky Lindd43a012012-11-05 16:22:22 -08001768 spin_lock_bh(&bus->rxctl_lock);
1769 if (bus->rxctl) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001770 brcmf_err("last control frame is being processed.\n");
Franky Lindd43a012012-11-05 16:22:22 -08001771 spin_unlock_bh(&bus->rxctl_lock);
1772 vfree(buf);
1773 goto done;
1774 }
1775 bus->rxctl = buf + doff;
1776 bus->rxctl_orig = buf;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001777 bus->rxlen = len - doff;
Franky Lindd43a012012-11-05 16:22:22 -08001778 spin_unlock_bh(&bus->rxctl_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001779
1780done:
1781 /* Awake any waiters */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001782 brcmf_sdio_dcmd_resp_wake(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001783}
1784
1785/* Pad read to blocksize for efficiency */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001786static void brcmf_sdio_pad(struct brcmf_sdio *bus, u16 *pad, u16 *rdlen)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001787{
1788 if (bus->roundup && bus->blocksize && *rdlen > bus->blocksize) {
1789 *pad = bus->blocksize - (*rdlen % bus->blocksize);
1790 if (*pad <= bus->roundup && *pad < bus->blocksize &&
1791 *rdlen + *pad + BRCMF_FIRSTREAD < MAX_RX_DATASZ)
1792 *rdlen += *pad;
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01001793 } else if (*rdlen % bus->head_align) {
1794 *rdlen += bus->head_align - (*rdlen % bus->head_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001795 }
1796}
1797
Franky Lin4754fce2012-09-19 22:21:11 +02001798static uint brcmf_sdio_readframes(struct brcmf_sdio *bus, uint maxframes)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001799{
Arend van Spriel5b435de2011-10-05 13:19:03 +02001800 struct sk_buff *pkt; /* Packet for event or data frames */
1801 u16 pad; /* Number of pad bytes to read */
Arend van Spriel5b435de2011-10-05 13:19:03 +02001802 uint rxleft = 0; /* Remaining number of frames allowed */
Arend van Spriel349e7102013-03-03 12:45:28 +01001803 int ret; /* Return code from calls */
Arend van Spriel5b435de2011-10-05 13:19:03 +02001804 uint rxcount = 0; /* Total frames read */
Franky Lin6bc52312013-08-10 12:27:28 +02001805 struct brcmf_sdio_hdrinfo *rd = &bus->cur_read, rd_new;
Franky Lin4754fce2012-09-19 22:21:11 +02001806 u8 head_read = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001807
1808 brcmf_dbg(TRACE, "Enter\n");
1809
1810 /* Not finished unless we encounter no more frames indication */
Franky Lin4754fce2012-09-19 22:21:11 +02001811 bus->rxpending = true;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001812
Franky Lin4754fce2012-09-19 22:21:11 +02001813 for (rd->seq_num = bus->rx_seq, rxleft = maxframes;
Arend van Spriela1ce7a02015-02-06 18:36:42 +01001814 !bus->rxskip && rxleft && bus->sdiodev->state == BRCMF_SDIOD_DATA;
Franky Lin4754fce2012-09-19 22:21:11 +02001815 rd->seq_num++, rxleft--) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001816
1817 /* Handle glomming separately */
Arend van Sprielb83db862011-10-19 12:51:09 +02001818 if (bus->glomd || !skb_queue_empty(&bus->glom)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001819 u8 cnt;
1820 brcmf_dbg(GLOM, "calling rxglom: glomd %p, glom %p\n",
Arend van Sprielb83db862011-10-19 12:51:09 +02001821 bus->glomd, skb_peek(&bus->glom));
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001822 cnt = brcmf_sdio_rxglom(bus, rd->seq_num);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001823 brcmf_dbg(GLOM, "rxglom returned %d\n", cnt);
Franky Lin4754fce2012-09-19 22:21:11 +02001824 rd->seq_num += cnt - 1;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001825 rxleft = (rxleft > cnt) ? (rxleft - cnt) : 1;
1826 continue;
1827 }
1828
Franky Lin4754fce2012-09-19 22:21:11 +02001829 rd->len_left = rd->len;
1830 /* read header first for unknow frame length */
Franky Lin38b0b0d2012-11-05 16:22:24 -08001831 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001832 if (!rd->len) {
Arend van Spriela39be272013-12-12 11:58:58 +01001833 ret = brcmf_sdiod_recv_buf(bus->sdiodev,
Arend van Spriela39be272013-12-12 11:58:58 +01001834 bus->rxhdr, BRCMF_FIRSTREAD);
Franky Lin4754fce2012-09-19 22:21:11 +02001835 bus->sdcnt.f2rxhdrs++;
Arend van Spriel349e7102013-03-03 12:45:28 +01001836 if (ret < 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001837 brcmf_err("RXHEADER FAILED: %d\n",
Arend van Spriel349e7102013-03-03 12:45:28 +01001838 ret);
Franky Lin4754fce2012-09-19 22:21:11 +02001839 bus->sdcnt.rx_hdrfail++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001840 brcmf_sdio_rxfail(bus, true, true);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001841 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001842 continue;
1843 }
1844
Franky Lin4754fce2012-09-19 22:21:11 +02001845 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() || BRCMF_HDRS_ON(),
1846 bus->rxhdr, SDPCM_HDRLEN,
1847 "RxHdr:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001848
Franky Lin6bc52312013-08-10 12:27:28 +02001849 if (brcmf_sdio_hdparse(bus, bus->rxhdr, rd,
1850 BRCMF_SDIO_FT_NORMAL)) {
Franky Lin38b0b0d2012-11-05 16:22:24 -08001851 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001852 if (!bus->rxpending)
1853 break;
1854 else
1855 continue;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001856 }
1857
Franky Lin4754fce2012-09-19 22:21:11 +02001858 if (rd->channel == SDPCM_CONTROL_CHANNEL) {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001859 brcmf_sdio_read_control(bus, bus->rxhdr,
1860 rd->len,
1861 rd->dat_offset);
Franky Lin4754fce2012-09-19 22:21:11 +02001862 /* prepare the descriptor for the next read */
1863 rd->len = rd->len_nxtfrm << 4;
1864 rd->len_nxtfrm = 0;
1865 /* treat all packet as event if we don't know */
1866 rd->channel = SDPCM_EVENT_CHANNEL;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001867 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001868 continue;
1869 }
1870 rd->len_left = rd->len > BRCMF_FIRSTREAD ?
1871 rd->len - BRCMF_FIRSTREAD : 0;
1872 head_read = BRCMF_FIRSTREAD;
1873 }
1874
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001875 brcmf_sdio_pad(bus, &pad, &rd->len_left);
Franky Lin4754fce2012-09-19 22:21:11 +02001876
1877 pkt = brcmu_pkt_buf_get_skb(rd->len_left + head_read +
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01001878 bus->head_align);
Franky Lin4754fce2012-09-19 22:21:11 +02001879 if (!pkt) {
1880 /* Give up on data, request rtx of events */
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001881 brcmf_err("brcmu_pkt_buf_get_skb failed\n");
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001882 brcmf_sdio_rxfail(bus, false,
Franky Lin4754fce2012-09-19 22:21:11 +02001883 RETRYCHAN(rd->channel));
Franky Lin38b0b0d2012-11-05 16:22:24 -08001884 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001885 continue;
1886 }
1887 skb_pull(pkt, head_read);
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01001888 pkt_align(pkt, rd->len_left, bus->head_align);
Franky Lin4754fce2012-09-19 22:21:11 +02001889
Arend van Spriela7cdd822013-12-12 11:58:59 +01001890 ret = brcmf_sdiod_recv_pkt(bus->sdiodev, pkt);
Franky Lin4754fce2012-09-19 22:21:11 +02001891 bus->sdcnt.f2rxdata++;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001892 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001893
Arend van Spriel349e7102013-03-03 12:45:28 +01001894 if (ret < 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001895 brcmf_err("read %d bytes from channel %d failed: %d\n",
Arend van Spriel349e7102013-03-03 12:45:28 +01001896 rd->len, rd->channel, ret);
Franky Lin4754fce2012-09-19 22:21:11 +02001897 brcmu_pkt_buf_free_skb(pkt);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001898 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001899 brcmf_sdio_rxfail(bus, true,
Franky Lin4754fce2012-09-19 22:21:11 +02001900 RETRYCHAN(rd->channel));
Franky Lin38b0b0d2012-11-05 16:22:24 -08001901 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001902 continue;
1903 }
1904
1905 if (head_read) {
1906 skb_push(pkt, head_read);
1907 memcpy(pkt->data, bus->rxhdr, head_read);
1908 head_read = 0;
1909 } else {
1910 memcpy(bus->rxhdr, pkt->data, SDPCM_HDRLEN);
1911 rd_new.seq_num = rd->seq_num;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001912 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin6bc52312013-08-10 12:27:28 +02001913 if (brcmf_sdio_hdparse(bus, bus->rxhdr, &rd_new,
1914 BRCMF_SDIO_FT_NORMAL)) {
Franky Lin4754fce2012-09-19 22:21:11 +02001915 rd->len = 0;
1916 brcmu_pkt_buf_free_skb(pkt);
1917 }
Arend van Spriel80969832012-06-09 22:51:44 +02001918 bus->sdcnt.rx_readahead_cnt++;
Franky Lin4754fce2012-09-19 22:21:11 +02001919 if (rd->len != roundup(rd_new.len, 16)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001920 brcmf_err("frame length mismatch:read %d, should be %d\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001921 rd->len,
1922 roundup(rd_new.len, 16) >> 4);
1923 rd->len = 0;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001924 brcmf_sdio_rxfail(bus, true, true);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001925 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001926 brcmu_pkt_buf_free_skb(pkt);
1927 continue;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001928 }
Franky Lin38b0b0d2012-11-05 16:22:24 -08001929 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001930 rd->len_nxtfrm = rd_new.len_nxtfrm;
1931 rd->channel = rd_new.channel;
1932 rd->dat_offset = rd_new.dat_offset;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001933
Joe Perches1e023822012-01-15 00:38:40 -08001934 brcmf_dbg_hex_dump(!(BRCMF_BYTES_ON() &&
1935 BRCMF_DATA_ON()) &&
1936 BRCMF_HDRS_ON(),
1937 bus->rxhdr, SDPCM_HDRLEN,
1938 "RxHdr:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001939
Franky Lin4754fce2012-09-19 22:21:11 +02001940 if (rd_new.channel == SDPCM_CONTROL_CHANNEL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001941 brcmf_err("readahead on control packet %d?\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001942 rd_new.seq_num);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001943 /* Force retry w/normal header read */
Franky Lin4754fce2012-09-19 22:21:11 +02001944 rd->len = 0;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001945 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001946 brcmf_sdio_rxfail(bus, false, true);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001947 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001948 brcmu_pkt_buf_free_skb(pkt);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001949 continue;
1950 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02001951 }
1952
Joe Perches1e023822012-01-15 00:38:40 -08001953 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_DATA_ON(),
Franky Lin4754fce2012-09-19 22:21:11 +02001954 pkt->data, rd->len, "Rx Data:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001955
Arend van Spriel5b435de2011-10-05 13:19:03 +02001956 /* Save superframe descriptor and allocate packet frame */
Franky Lin4754fce2012-09-19 22:21:11 +02001957 if (rd->channel == SDPCM_GLOM_CHANNEL) {
Franky Lin6bc52312013-08-10 12:27:28 +02001958 if (SDPCM_GLOMDESC(&bus->rxhdr[SDPCM_HWHDR_LEN])) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001959 brcmf_dbg(GLOM, "glom descriptor, %d bytes:\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001960 rd->len);
Joe Perches1e023822012-01-15 00:38:40 -08001961 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
Franky Lin4754fce2012-09-19 22:21:11 +02001962 pkt->data, rd->len,
Joe Perches1e023822012-01-15 00:38:40 -08001963 "Glom Data:\n");
Franky Lin4754fce2012-09-19 22:21:11 +02001964 __skb_trim(pkt, rd->len);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001965 skb_pull(pkt, SDPCM_HDRLEN);
1966 bus->glomd = pkt;
1967 } else {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001968 brcmf_err("%s: glom superframe w/o "
Arend van Spriel5b435de2011-10-05 13:19:03 +02001969 "descriptor!\n", __func__);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001970 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001971 brcmf_sdio_rxfail(bus, false, false);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001972 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001973 }
Franky Lin4754fce2012-09-19 22:21:11 +02001974 /* prepare the descriptor for the next read */
1975 rd->len = rd->len_nxtfrm << 4;
1976 rd->len_nxtfrm = 0;
1977 /* treat all packet as event if we don't know */
1978 rd->channel = SDPCM_EVENT_CHANNEL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001979 continue;
1980 }
1981
1982 /* Fill in packet len and prio, deliver upward */
Franky Lin4754fce2012-09-19 22:21:11 +02001983 __skb_trim(pkt, rd->len);
1984 skb_pull(pkt, rd->dat_offset);
1985
Franky Linc56caa9d2016-04-11 11:35:25 +02001986 if (pkt->len == 0)
1987 brcmu_pkt_buf_free_skb(pkt);
1988 else if (rd->channel == SDPCM_EVENT_CHANNEL)
1989 brcmf_rx_event(bus->sdiodev->dev, pkt);
1990 else
1991 brcmf_rx_frame(bus->sdiodev->dev, pkt,
1992 false);
1993
Franky Lin4754fce2012-09-19 22:21:11 +02001994 /* prepare the descriptor for the next read */
1995 rd->len = rd->len_nxtfrm << 4;
1996 rd->len_nxtfrm = 0;
1997 /* treat all packet as event if we don't know */
1998 rd->channel = SDPCM_EVENT_CHANNEL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001999 }
Franky Lin4754fce2012-09-19 22:21:11 +02002000
Arend van Spriel5b435de2011-10-05 13:19:03 +02002001 rxcount = maxframes - rxleft;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002002 /* Message if we hit the limit */
2003 if (!rxleft)
Franky Lin4754fce2012-09-19 22:21:11 +02002004 brcmf_dbg(DATA, "hit rx limit of %d frames\n", maxframes);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002005 else
Arend van Spriel5b435de2011-10-05 13:19:03 +02002006 brcmf_dbg(DATA, "processed %d frames\n", rxcount);
2007 /* Back off rxseq if awaiting rtx, update rx_seq */
2008 if (bus->rxskip)
Franky Lin4754fce2012-09-19 22:21:11 +02002009 rd->seq_num--;
2010 bus->rx_seq = rd->seq_num;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002011
2012 return rxcount;
2013}
2014
Arend van Spriel5b435de2011-10-05 13:19:03 +02002015static void
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002016brcmf_sdio_wait_event_wakeup(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002017{
Kosuke Tatsukawaa7decc42015-12-10 13:43:08 +01002018 wake_up_interruptible(&bus->ctrl_wait);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002019 return;
2020}
2021
Franky Lin8da9d2c2013-11-29 12:25:10 +01002022static int brcmf_sdio_txpkt_hdalign(struct brcmf_sdio *bus, struct sk_buff *pkt)
2023{
Arend van Spriele217d1c2013-11-29 12:25:13 +01002024 u16 head_pad;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002025 u8 *dat_buf;
2026
Franky Lin8da9d2c2013-11-29 12:25:10 +01002027 dat_buf = (u8 *)(pkt->data);
2028
2029 /* Check head padding */
Arend van Spriele217d1c2013-11-29 12:25:13 +01002030 head_pad = ((unsigned long)dat_buf % bus->head_align);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002031 if (head_pad) {
2032 if (skb_headroom(pkt) < head_pad) {
2033 bus->sdiodev->bus_if->tx_realloc++;
2034 head_pad = 0;
2035 if (skb_cow(pkt, head_pad))
2036 return -ENOMEM;
2037 }
2038 skb_push(pkt, head_pad);
2039 dat_buf = (u8 *)(pkt->data);
2040 memset(dat_buf, 0, head_pad + bus->tx_hdrlen);
2041 }
2042 return head_pad;
2043}
2044
Franky Lin5491c112013-09-25 13:05:43 +02002045/**
2046 * struct brcmf_skbuff_cb reserves first two bytes in sk_buff::cb for
2047 * bus layer usage.
2048 */
Franky Linb05e9252013-08-10 12:27:26 +02002049/* flag marking a dummy skb added for DMA alignment requirement */
Franky Lin5491c112013-09-25 13:05:43 +02002050#define ALIGN_SKB_FLAG 0x8000
Franky Linb05e9252013-08-10 12:27:26 +02002051/* bit mask of data length chopped from the previous packet */
Franky Lin5491c112013-09-25 13:05:43 +02002052#define ALIGN_SKB_CHOP_LEN_MASK 0x7fff
2053
Franky Lin8da9d2c2013-11-29 12:25:10 +01002054static int brcmf_sdio_txpkt_prep_sg(struct brcmf_sdio *bus,
Arend van Spriela64304f2013-10-15 15:44:56 +02002055 struct sk_buff_head *pktq,
Franky Lin8da9d2c2013-11-29 12:25:10 +01002056 struct sk_buff *pkt, u16 total_len)
Arend van Spriela64304f2013-10-15 15:44:56 +02002057{
Franky Lin8da9d2c2013-11-29 12:25:10 +01002058 struct brcmf_sdio_dev *sdiodev;
Arend van Spriela64304f2013-10-15 15:44:56 +02002059 struct sk_buff *pkt_pad;
Arend van Spriele217d1c2013-11-29 12:25:13 +01002060 u16 tail_pad, tail_chop, chain_pad;
Arend van Spriela64304f2013-10-15 15:44:56 +02002061 unsigned int blksize;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002062 bool lastfrm;
2063 int ntail, ret;
Arend van Spriela64304f2013-10-15 15:44:56 +02002064
Franky Lin8da9d2c2013-11-29 12:25:10 +01002065 sdiodev = bus->sdiodev;
Arend van Spriela64304f2013-10-15 15:44:56 +02002066 blksize = sdiodev->func[SDIO_FUNC_2]->cur_blksize;
Arend van Spriela64304f2013-10-15 15:44:56 +02002067 /* sg entry alignment should be a divisor of block size */
Arend van Spriele217d1c2013-11-29 12:25:13 +01002068 WARN_ON(blksize % bus->sgentry_align);
Arend van Spriela64304f2013-10-15 15:44:56 +02002069
2070 /* Check tail padding */
Franky Lin8da9d2c2013-11-29 12:25:10 +01002071 lastfrm = skb_queue_is_last(pktq, pkt);
2072 tail_pad = 0;
Arend van Spriele217d1c2013-11-29 12:25:13 +01002073 tail_chop = pkt->len % bus->sgentry_align;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002074 if (tail_chop)
Arend van Spriele217d1c2013-11-29 12:25:13 +01002075 tail_pad = bus->sgentry_align - tail_chop;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002076 chain_pad = (total_len + tail_pad) % blksize;
2077 if (lastfrm && chain_pad)
2078 tail_pad += blksize - chain_pad;
Arend van Spriela64304f2013-10-15 15:44:56 +02002079 if (skb_tailroom(pkt) < tail_pad && pkt->len > blksize) {
Arend van Spriel1eb43012014-02-20 18:55:55 +01002080 pkt_pad = brcmu_pkt_buf_get_skb(tail_pad + tail_chop +
2081 bus->head_align);
Arend van Spriela64304f2013-10-15 15:44:56 +02002082 if (pkt_pad == NULL)
2083 return -ENOMEM;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002084 ret = brcmf_sdio_txpkt_hdalign(bus, pkt_pad);
Dave Jones2dc3a8e2014-03-06 15:51:08 -05002085 if (unlikely(ret < 0)) {
2086 kfree_skb(pkt_pad);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002087 return ret;
Dave Jones2dc3a8e2014-03-06 15:51:08 -05002088 }
Arend van Spriela64304f2013-10-15 15:44:56 +02002089 memcpy(pkt_pad->data,
2090 pkt->data + pkt->len - tail_chop,
2091 tail_chop);
Arend van Spriel5aa9f0e2014-02-25 20:30:27 +01002092 *(u16 *)(pkt_pad->cb) = ALIGN_SKB_FLAG + tail_chop;
Arend van Spriela64304f2013-10-15 15:44:56 +02002093 skb_trim(pkt, pkt->len - tail_chop);
Arend van Spriel1eb43012014-02-20 18:55:55 +01002094 skb_trim(pkt_pad, tail_pad + tail_chop);
Arend van Spriela64304f2013-10-15 15:44:56 +02002095 __skb_queue_after(pktq, pkt, pkt_pad);
2096 } else {
2097 ntail = pkt->data_len + tail_pad -
2098 (pkt->end - pkt->tail);
2099 if (skb_cloned(pkt) || ntail > 0)
2100 if (pskb_expand_head(pkt, 0, ntail, GFP_ATOMIC))
2101 return -ENOMEM;
2102 if (skb_linearize(pkt))
2103 return -ENOMEM;
Arend van Spriela64304f2013-10-15 15:44:56 +02002104 __skb_put(pkt, tail_pad);
2105 }
2106
Franky Lin8da9d2c2013-11-29 12:25:10 +01002107 return tail_pad;
Arend van Spriela64304f2013-10-15 15:44:56 +02002108}
2109
Franky Linb05e9252013-08-10 12:27:26 +02002110/**
2111 * brcmf_sdio_txpkt_prep - packet preparation for transmit
2112 * @bus: brcmf_sdio structure pointer
2113 * @pktq: packet list pointer
2114 * @chan: virtual channel to transmit the packet
2115 *
2116 * Processes to be applied to the packet
2117 * - Align data buffer pointer
2118 * - Align data buffer length
2119 * - Prepare header
2120 * Return: negative value if there is error
2121 */
2122static int
2123brcmf_sdio_txpkt_prep(struct brcmf_sdio *bus, struct sk_buff_head *pktq,
2124 uint chan)
2125{
Franky Lin8da9d2c2013-11-29 12:25:10 +01002126 u16 head_pad, total_len;
Arend van Spriela64304f2013-10-15 15:44:56 +02002127 struct sk_buff *pkt_next;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002128 u8 txseq;
2129 int ret;
Franky Lin6bc52312013-08-10 12:27:28 +02002130 struct brcmf_sdio_hdrinfo hd_info = {0};
Franky Linb05e9252013-08-10 12:27:26 +02002131
Franky Lin8da9d2c2013-11-29 12:25:10 +01002132 txseq = bus->tx_seq;
2133 total_len = 0;
2134 skb_queue_walk(pktq, pkt_next) {
2135 /* alignment packet inserted in previous
2136 * loop cycle can be skipped as it is
2137 * already properly aligned and does not
2138 * need an sdpcm header.
2139 */
Arend van Spriel5aa9f0e2014-02-25 20:30:27 +01002140 if (*(u16 *)(pkt_next->cb) & ALIGN_SKB_FLAG)
Franky Lin8da9d2c2013-11-29 12:25:10 +01002141 continue;
Franky Linb05e9252013-08-10 12:27:26 +02002142
Franky Lin8da9d2c2013-11-29 12:25:10 +01002143 /* align packet data pointer */
2144 ret = brcmf_sdio_txpkt_hdalign(bus, pkt_next);
2145 if (ret < 0)
2146 return ret;
2147 head_pad = (u16)ret;
2148 if (head_pad)
Arend van Spriel1eb43012014-02-20 18:55:55 +01002149 memset(pkt_next->data + bus->tx_hdrlen, 0, head_pad);
Franky Linb05e9252013-08-10 12:27:26 +02002150
Franky Lin8da9d2c2013-11-29 12:25:10 +01002151 total_len += pkt_next->len;
Franky Linb05e9252013-08-10 12:27:26 +02002152
Arend van Spriela64304f2013-10-15 15:44:56 +02002153 hd_info.len = pkt_next->len;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002154 hd_info.lastfrm = skb_queue_is_last(pktq, pkt_next);
2155 if (bus->txglom && pktq->qlen > 1) {
2156 ret = brcmf_sdio_txpkt_prep_sg(bus, pktq,
2157 pkt_next, total_len);
2158 if (ret < 0)
2159 return ret;
2160 hd_info.tail_pad = (u16)ret;
2161 total_len += (u16)ret;
2162 }
2163
2164 hd_info.channel = chan;
2165 hd_info.dat_offset = head_pad + bus->tx_hdrlen;
2166 hd_info.seq_num = txseq++;
2167
2168 /* Now fill the header */
2169 brcmf_sdio_hdpack(bus, pkt_next->data, &hd_info);
2170
2171 if (BRCMF_BYTES_ON() &&
2172 ((BRCMF_CTL_ON() && chan == SDPCM_CONTROL_CHANNEL) ||
2173 (BRCMF_DATA_ON() && chan != SDPCM_CONTROL_CHANNEL)))
Hante Meuleman47ab4cd2014-02-25 20:30:29 +01002174 brcmf_dbg_hex_dump(true, pkt_next->data, hd_info.len,
Franky Lin8da9d2c2013-11-29 12:25:10 +01002175 "Tx Frame:\n");
2176 else if (BRCMF_HDRS_ON())
Hante Meuleman47ab4cd2014-02-25 20:30:29 +01002177 brcmf_dbg_hex_dump(true, pkt_next->data,
Franky Lin8da9d2c2013-11-29 12:25:10 +01002178 head_pad + bus->tx_hdrlen,
2179 "Tx Header:\n");
Franky Linb05e9252013-08-10 12:27:26 +02002180 }
Franky Lin8da9d2c2013-11-29 12:25:10 +01002181 /* Hardware length tag of the first packet should be total
2182 * length of the chain (including padding)
2183 */
2184 if (bus->txglom)
2185 brcmf_sdio_update_hwhdr(pktq->next->data, total_len);
Franky Linb05e9252013-08-10 12:27:26 +02002186 return 0;
2187}
2188
2189/**
2190 * brcmf_sdio_txpkt_postp - packet post processing for transmit
2191 * @bus: brcmf_sdio structure pointer
2192 * @pktq: packet list pointer
2193 *
2194 * Processes to be applied to the packet
2195 * - Remove head padding
2196 * - Remove tail padding
2197 */
2198static void
2199brcmf_sdio_txpkt_postp(struct brcmf_sdio *bus, struct sk_buff_head *pktq)
2200{
2201 u8 *hdr;
2202 u32 dat_offset;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002203 u16 tail_pad;
Arend van Spriel5aa9f0e2014-02-25 20:30:27 +01002204 u16 dummy_flags, chop_len;
Franky Linb05e9252013-08-10 12:27:26 +02002205 struct sk_buff *pkt_next, *tmp, *pkt_prev;
2206
2207 skb_queue_walk_safe(pktq, pkt_next, tmp) {
Arend van Spriel5aa9f0e2014-02-25 20:30:27 +01002208 dummy_flags = *(u16 *)(pkt_next->cb);
Franky Lin5491c112013-09-25 13:05:43 +02002209 if (dummy_flags & ALIGN_SKB_FLAG) {
2210 chop_len = dummy_flags & ALIGN_SKB_CHOP_LEN_MASK;
Franky Linb05e9252013-08-10 12:27:26 +02002211 if (chop_len) {
2212 pkt_prev = pkt_next->prev;
Franky Linb05e9252013-08-10 12:27:26 +02002213 skb_put(pkt_prev, chop_len);
2214 }
2215 __skb_unlink(pkt_next, pktq);
2216 brcmu_pkt_buf_free_skb(pkt_next);
2217 } else {
Franky Lin8da9d2c2013-11-29 12:25:10 +01002218 hdr = pkt_next->data + bus->tx_hdrlen - SDPCM_SWHDR_LEN;
Franky Linb05e9252013-08-10 12:27:26 +02002219 dat_offset = le32_to_cpu(*(__le32 *)hdr);
2220 dat_offset = (dat_offset & SDPCM_DOFFSET_MASK) >>
2221 SDPCM_DOFFSET_SHIFT;
2222 skb_pull(pkt_next, dat_offset);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002223 if (bus->txglom) {
2224 tail_pad = le16_to_cpu(*(__le16 *)(hdr - 2));
2225 skb_trim(pkt_next, pkt_next->len - tail_pad);
2226 }
Franky Linb05e9252013-08-10 12:27:26 +02002227 }
2228 }
2229}
2230
Arend van Spriel5b435de2011-10-05 13:19:03 +02002231/* Writes a HW/SW header into the packet and sends it. */
2232/* Assumes: (a) header space already there, (b) caller holds lock */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002233static int brcmf_sdio_txpkt(struct brcmf_sdio *bus, struct sk_buff_head *pktq,
2234 uint chan)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002235{
2236 int ret;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002237 struct sk_buff *pkt_next, *tmp;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002238
2239 brcmf_dbg(TRACE, "Enter\n");
2240
Franky Lin8da9d2c2013-11-29 12:25:10 +01002241 ret = brcmf_sdio_txpkt_prep(bus, pktq, chan);
Franky Linb05e9252013-08-10 12:27:26 +02002242 if (ret)
2243 goto done;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002244
Franky Lin38b0b0d2012-11-05 16:22:24 -08002245 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriela7cdd822013-12-12 11:58:59 +01002246 ret = brcmf_sdiod_send_pkt(bus->sdiodev, pktq);
Arend van Spriel80969832012-06-09 22:51:44 +02002247 bus->sdcnt.f2txdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002248
Hante Meuleman81c78832014-02-25 20:30:35 +01002249 if (ret < 0)
2250 brcmf_sdio_txfail(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002251
Franky Lin38b0b0d2012-11-05 16:22:24 -08002252 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002253
2254done:
Franky Lin8da9d2c2013-11-29 12:25:10 +01002255 brcmf_sdio_txpkt_postp(bus, pktq);
2256 if (ret == 0)
2257 bus->tx_seq = (bus->tx_seq + pktq->qlen) % SDPCM_SEQ_WRAP;
2258 skb_queue_walk_safe(pktq, pkt_next, tmp) {
2259 __skb_unlink(pkt_next, pktq);
2260 brcmf_txcomplete(bus->sdiodev->dev, pkt_next, ret == 0);
2261 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002262 return ret;
2263}
2264
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002265static uint brcmf_sdio_sendfromq(struct brcmf_sdio *bus, uint maxframes)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002266{
2267 struct sk_buff *pkt;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002268 struct sk_buff_head pktq;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002269 u32 intstatus = 0;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002270 int ret = 0, prec_out, i;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002271 uint cnt = 0;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002272 u8 tx_prec_map, pkt_num;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002273
Arend van Spriel5b435de2011-10-05 13:19:03 +02002274 brcmf_dbg(TRACE, "Enter\n");
2275
2276 tx_prec_map = ~bus->flowcontrol;
2277
2278 /* Send frames until the limit or some other event */
Franky Lin8da9d2c2013-11-29 12:25:10 +01002279 for (cnt = 0; (cnt < maxframes) && data_ok(bus);) {
2280 pkt_num = 1;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002281 if (bus->txglom)
2282 pkt_num = min_t(u8, bus->tx_max - bus->tx_seq,
Arend van Sprielaf1fa212014-02-27 19:25:00 +01002283 bus->sdiodev->txglomsz);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002284 pkt_num = min_t(u32, pkt_num,
2285 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol));
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002286 __skb_queue_head_init(&pktq);
2287 spin_lock_bh(&bus->txq_lock);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002288 for (i = 0; i < pkt_num; i++) {
2289 pkt = brcmu_pktq_mdeq(&bus->txq, tx_prec_map,
2290 &prec_out);
2291 if (pkt == NULL)
2292 break;
2293 __skb_queue_tail(&pktq, pkt);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002294 }
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002295 spin_unlock_bh(&bus->txq_lock);
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002296 if (i == 0)
Franky Lin8da9d2c2013-11-29 12:25:10 +01002297 break;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002298
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002299 ret = brcmf_sdio_txpkt(bus, &pktq, SDPCM_DATA_CHANNEL);
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002300
Franky Lin8da9d2c2013-11-29 12:25:10 +01002301 cnt += i;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002302
2303 /* In poll mode, need to check for other events */
Hante Meulemanb6a8cf22014-02-25 20:30:33 +01002304 if (!bus->intr) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02002305 /* Check device status, signal pending interrupt */
Franky Lin38b0b0d2012-11-05 16:22:24 -08002306 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin5c15c232012-05-04 18:27:37 -07002307 ret = r_sdreg32(bus, &intstatus,
2308 offsetof(struct sdpcmd_regs,
2309 intstatus));
Franky Lin38b0b0d2012-11-05 16:22:24 -08002310 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel80969832012-06-09 22:51:44 +02002311 bus->sdcnt.f2txdata++;
Franky Lin5c15c232012-05-04 18:27:37 -07002312 if (ret != 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002313 break;
2314 if (intstatus & bus->hostintmask)
Franky Lin1d382272012-09-13 21:11:59 +02002315 atomic_set(&bus->ipend, 1);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002316 }
2317 }
2318
2319 /* Deflow-control stack if needed */
Arend van Spriela1ce7a02015-02-06 18:36:42 +01002320 if ((bus->sdiodev->state == BRCMF_SDIOD_DATA) &&
Franky Linc8bf3482011-12-16 18:37:08 -08002321 bus->txoff && (pktq_len(&bus->txq) < TXLOW)) {
Hante Meuleman90d03ff2012-09-11 21:18:46 +02002322 bus->txoff = false;
2323 brcmf_txflowblock(bus->sdiodev->dev, false);
Franky Linc8bf3482011-12-16 18:37:08 -08002324 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002325
2326 return cnt;
2327}
2328
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002329static int brcmf_sdio_tx_ctrlframe(struct brcmf_sdio *bus, u8 *frame, u16 len)
2330{
2331 u8 doff;
2332 u16 pad;
2333 uint retries = 0;
2334 struct brcmf_sdio_hdrinfo hd_info = {0};
2335 int ret;
2336
2337 brcmf_dbg(TRACE, "Enter\n");
2338
2339 /* Back the pointer to make room for bus header */
2340 frame -= bus->tx_hdrlen;
2341 len += bus->tx_hdrlen;
2342
2343 /* Add alignment padding (optional for ctl frames) */
2344 doff = ((unsigned long)frame % bus->head_align);
2345 if (doff) {
2346 frame -= doff;
2347 len += doff;
2348 memset(frame + bus->tx_hdrlen, 0, doff);
2349 }
2350
2351 /* Round send length to next SDIO block */
2352 pad = 0;
2353 if (bus->roundup && bus->blocksize && (len > bus->blocksize)) {
2354 pad = bus->blocksize - (len % bus->blocksize);
2355 if ((pad > bus->roundup) || (pad >= bus->blocksize))
2356 pad = 0;
2357 } else if (len % bus->head_align) {
2358 pad = bus->head_align - (len % bus->head_align);
2359 }
2360 len += pad;
2361
2362 hd_info.len = len - pad;
2363 hd_info.channel = SDPCM_CONTROL_CHANNEL;
2364 hd_info.dat_offset = doff + bus->tx_hdrlen;
2365 hd_info.seq_num = bus->tx_seq;
2366 hd_info.lastfrm = true;
2367 hd_info.tail_pad = pad;
2368 brcmf_sdio_hdpack(bus, frame, &hd_info);
2369
2370 if (bus->txglom)
2371 brcmf_sdio_update_hwhdr(frame, len);
2372
2373 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_CTL_ON(),
2374 frame, len, "Tx Frame:\n");
2375 brcmf_dbg_hex_dump(!(BRCMF_BYTES_ON() && BRCMF_CTL_ON()) &&
2376 BRCMF_HDRS_ON(),
2377 frame, min_t(u16, len, 16), "TxHdr:\n");
2378
2379 do {
2380 ret = brcmf_sdiod_send_buf(bus->sdiodev, frame, len);
2381
2382 if (ret < 0)
2383 brcmf_sdio_txfail(bus);
2384 else
2385 bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQ_WRAP;
2386 } while (ret < 0 && retries++ < TXRETRIES);
2387
2388 return ret;
2389}
2390
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002391static void brcmf_sdio_bus_stop(struct device *dev)
Franky Lina9ffda82011-12-16 18:37:12 -08002392{
2393 u32 local_hostintmask;
2394 u8 saveclk;
Franky Lina9ffda82011-12-16 18:37:12 -08002395 int err;
2396 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01002397 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Lina9ffda82011-12-16 18:37:12 -08002398 struct brcmf_sdio *bus = sdiodev->bus;
2399
2400 brcmf_dbg(TRACE, "Enter\n");
2401
2402 if (bus->watchdog_tsk) {
2403 send_sig(SIGTERM, bus->watchdog_tsk, 1);
2404 kthread_stop(bus->watchdog_tsk);
2405 bus->watchdog_tsk = NULL;
2406 }
2407
Arend van Spriela1ce7a02015-02-06 18:36:42 +01002408 if (sdiodev->state != BRCMF_SDIOD_NOMEDIUM) {
Arend van Sprielbb350712014-01-13 22:20:29 +01002409 sdio_claim_host(sdiodev->func[1]);
Franky Lina9ffda82011-12-16 18:37:12 -08002410
Arend van Sprielbb350712014-01-13 22:20:29 +01002411 /* Enable clock for device interrupts */
2412 brcmf_sdio_bus_sleep(bus, false, false);
Franky Lina9ffda82011-12-16 18:37:12 -08002413
Arend van Sprielbb350712014-01-13 22:20:29 +01002414 /* Disable and clear interrupts at the chip level also */
2415 w_sdreg32(bus, 0, offsetof(struct sdpcmd_regs, hostintmask));
2416 local_hostintmask = bus->hostintmask;
2417 bus->hostintmask = 0;
Franky Lina9ffda82011-12-16 18:37:12 -08002418
Arend van Sprielbb350712014-01-13 22:20:29 +01002419 /* Force backplane clocks to assure F2 interrupt propagates */
2420 saveclk = brcmf_sdiod_regrb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
2421 &err);
2422 if (!err)
2423 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
2424 (saveclk | SBSDIO_FORCE_HT), &err);
2425 if (err)
2426 brcmf_err("Failed to force clock for F2: err %d\n",
2427 err);
Franky Lina9ffda82011-12-16 18:37:12 -08002428
Arend van Sprielbb350712014-01-13 22:20:29 +01002429 /* Turn off the bus (F2), free any pending packets */
2430 brcmf_dbg(INTR, "disable SDIO interrupts\n");
2431 sdio_disable_func(sdiodev->func[SDIO_FUNC_2]);
2432
2433 /* Clear any pending interrupts now that F2 is disabled */
2434 w_sdreg32(bus, local_hostintmask,
2435 offsetof(struct sdpcmd_regs, intstatus));
2436
2437 sdio_release_host(sdiodev->func[1]);
Franky Lina9ffda82011-12-16 18:37:12 -08002438 }
Franky Lina9ffda82011-12-16 18:37:12 -08002439 /* Clear the data packet queues */
2440 brcmu_pktq_flush(&bus->txq, true, NULL, NULL);
2441
2442 /* Clear any held glomming stuff */
Markus Elfring297540f2015-02-04 20:28:49 +01002443 brcmu_pkt_buf_free_skb(bus->glomd);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002444 brcmf_sdio_free_glom(bus);
Franky Lina9ffda82011-12-16 18:37:12 -08002445
2446 /* Clear rx control and wake any waiters */
Franky Lindd43a012012-11-05 16:22:22 -08002447 spin_lock_bh(&bus->rxctl_lock);
Franky Lina9ffda82011-12-16 18:37:12 -08002448 bus->rxlen = 0;
Franky Lindd43a012012-11-05 16:22:22 -08002449 spin_unlock_bh(&bus->rxctl_lock);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002450 brcmf_sdio_dcmd_resp_wake(bus);
Franky Lina9ffda82011-12-16 18:37:12 -08002451
2452 /* Reset some F2 state stuff */
2453 bus->rxskip = false;
2454 bus->tx_seq = bus->rx_seq = 0;
Franky Lina9ffda82011-12-16 18:37:12 -08002455}
2456
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002457static inline void brcmf_sdio_clrintr(struct brcmf_sdio *bus)
Franky Linba89bf12012-04-27 18:56:59 -07002458{
Hante Meulemanaf5b5e62016-02-17 11:27:08 +01002459 struct brcmf_sdio_dev *sdiodev;
Franky Linba89bf12012-04-27 18:56:59 -07002460 unsigned long flags;
2461
Hante Meulemanaf5b5e62016-02-17 11:27:08 +01002462 sdiodev = bus->sdiodev;
2463 if (sdiodev->oob_irq_requested) {
2464 spin_lock_irqsave(&sdiodev->irq_en_lock, flags);
2465 if (!sdiodev->irq_en && !atomic_read(&bus->ipend)) {
2466 enable_irq(sdiodev->settings->bus.sdio.oob_irq_nr);
2467 sdiodev->irq_en = true;
Hante Meuleman668761a2013-04-12 10:55:55 +02002468 }
Hante Meulemanaf5b5e62016-02-17 11:27:08 +01002469 spin_unlock_irqrestore(&sdiodev->irq_en_lock, flags);
Franky Linba89bf12012-04-27 18:56:59 -07002470 }
Franky Linba89bf12012-04-27 18:56:59 -07002471}
Franky Linba89bf12012-04-27 18:56:59 -07002472
Franky Lin45316032012-09-13 21:12:03 +02002473static int brcmf_sdio_intr_rstatus(struct brcmf_sdio *bus)
2474{
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01002475 struct brcmf_core *buscore;
Franky Lin45316032012-09-13 21:12:03 +02002476 u32 addr;
2477 unsigned long val;
Hante Meuleman5cbb9c22014-03-06 10:16:12 +01002478 int ret;
Franky Lin45316032012-09-13 21:12:03 +02002479
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01002480 buscore = brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV);
2481 addr = buscore->base + offsetof(struct sdpcmd_regs, intstatus);
Franky Lin45316032012-09-13 21:12:03 +02002482
Arend van Spriela39be272013-12-12 11:58:58 +01002483 val = brcmf_sdiod_regrl(bus->sdiodev, addr, &ret);
Franky Lin45316032012-09-13 21:12:03 +02002484 bus->sdcnt.f1regdata++;
2485 if (ret != 0)
Hante Meuleman5cbb9c22014-03-06 10:16:12 +01002486 return ret;
Franky Lin45316032012-09-13 21:12:03 +02002487
2488 val &= bus->hostintmask;
2489 atomic_set(&bus->fcstate, !!(val & I_HMB_FC_STATE));
2490
2491 /* Clear interrupts */
2492 if (val) {
Arend van Spriela39be272013-12-12 11:58:58 +01002493 brcmf_sdiod_regwl(bus->sdiodev, addr, val, &ret);
Franky Lin45316032012-09-13 21:12:03 +02002494 bus->sdcnt.f1regdata++;
Vineet Guptad3928d02015-07-09 13:43:18 +05302495 atomic_or(val, &bus->intstatus);
Franky Lin45316032012-09-13 21:12:03 +02002496 }
2497
2498 return ret;
2499}
2500
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002501static void brcmf_sdio_dpc(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002502{
Franky Lin45316032012-09-13 21:12:03 +02002503 u32 newstatus = 0;
2504 unsigned long intstatus;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002505 uint txlimit = bus->txbound; /* Tx frames to send before resched */
Hante Meulemanb6a8cf22014-02-25 20:30:33 +01002506 uint framecnt; /* Temporary counter of tx/rx frames */
Hante Meuleman5cbb9c22014-03-06 10:16:12 +01002507 int err = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002508
2509 brcmf_dbg(TRACE, "Enter\n");
2510
Franky Lin38b0b0d2012-11-05 16:22:24 -08002511 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002512
2513 /* If waiting for HTAVAIL, check status */
Piotr Haber4a3da992013-04-11 13:28:46 +02002514 if (!bus->sr_enabled && bus->clkstate == CLK_PENDING) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02002515 u8 clkctl, devctl = 0;
2516
Joe Perches8ae74652012-01-15 00:38:38 -08002517#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02002518 /* Check for inconsistent device control */
Arend van Spriela39be272013-12-12 11:58:58 +01002519 devctl = brcmf_sdiod_regrb(bus->sdiodev,
2520 SBSDIO_DEVICE_CTL, &err);
Joe Perches8ae74652012-01-15 00:38:38 -08002521#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02002522
2523 /* Read CSR, if clock on switch to AVAIL, else ignore */
Arend van Spriela39be272013-12-12 11:58:58 +01002524 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
2525 SBSDIO_FUNC1_CHIPCLKCSR, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002526
Arend van Sprielc3203372013-04-03 12:40:44 +02002527 brcmf_dbg(SDIO, "DPC: PENDING, devctl 0x%02x clkctl 0x%02x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02002528 devctl, clkctl);
2529
2530 if (SBSDIO_HTAV(clkctl)) {
Arend van Spriela39be272013-12-12 11:58:58 +01002531 devctl = brcmf_sdiod_regrb(bus->sdiodev,
2532 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002533 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
Arend van Spriela39be272013-12-12 11:58:58 +01002534 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
2535 devctl, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002536 bus->clkstate = CLK_AVAIL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002537 }
2538 }
2539
Arend van Spriel5b435de2011-10-05 13:19:03 +02002540 /* Make sure backplane clock is on */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002541 brcmf_sdio_bus_sleep(bus, false, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002542
2543 /* Pending interrupt indicates new device status */
Franky Lin1d382272012-09-13 21:11:59 +02002544 if (atomic_read(&bus->ipend) > 0) {
2545 atomic_set(&bus->ipend, 0);
Franky Lin45316032012-09-13 21:12:03 +02002546 err = brcmf_sdio_intr_rstatus(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002547 }
2548
Franky Lin45316032012-09-13 21:12:03 +02002549 /* Start with leftover status bits */
2550 intstatus = atomic_xchg(&bus->intstatus, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002551
2552 /* Handle flow-control change: read new state in case our ack
2553 * crossed another change interrupt. If change still set, assume
2554 * FC ON for safety, let next loop through do the debounce.
2555 */
2556 if (intstatus & I_HMB_FC_CHANGE) {
2557 intstatus &= ~I_HMB_FC_CHANGE;
Franky Lin5c15c232012-05-04 18:27:37 -07002558 err = w_sdreg32(bus, I_HMB_FC_CHANGE,
2559 offsetof(struct sdpcmd_regs, intstatus));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002560
Franky Lin5c15c232012-05-04 18:27:37 -07002561 err = r_sdreg32(bus, &newstatus,
2562 offsetof(struct sdpcmd_regs, intstatus));
Arend van Spriel80969832012-06-09 22:51:44 +02002563 bus->sdcnt.f1regdata += 2;
Franky Lin45316032012-09-13 21:12:03 +02002564 atomic_set(&bus->fcstate,
2565 !!(newstatus & (I_HMB_FC_STATE | I_HMB_FC_CHANGE)));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002566 intstatus |= (newstatus & bus->hostintmask);
2567 }
2568
2569 /* Handle host mailbox indication */
2570 if (intstatus & I_HMB_HOST_INT) {
2571 intstatus &= ~I_HMB_HOST_INT;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002572 intstatus |= brcmf_sdio_hostmail(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002573 }
2574
Franky Lin38b0b0d2012-11-05 16:22:24 -08002575 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin7cdf57d2012-11-05 16:22:23 -08002576
Arend van Spriel5b435de2011-10-05 13:19:03 +02002577 /* Generally don't ask for these, can get CRC errors... */
2578 if (intstatus & I_WR_OOSYNC) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002579 brcmf_err("Dongle reports WR_OOSYNC\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002580 intstatus &= ~I_WR_OOSYNC;
2581 }
2582
2583 if (intstatus & I_RD_OOSYNC) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002584 brcmf_err("Dongle reports RD_OOSYNC\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002585 intstatus &= ~I_RD_OOSYNC;
2586 }
2587
2588 if (intstatus & I_SBINT) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002589 brcmf_err("Dongle reports SBINT\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002590 intstatus &= ~I_SBINT;
2591 }
2592
2593 /* Would be active due to wake-wlan in gSPI */
2594 if (intstatus & I_CHIPACTIVE) {
2595 brcmf_dbg(INFO, "Dongle reports CHIPACTIVE\n");
2596 intstatus &= ~I_CHIPACTIVE;
2597 }
2598
2599 /* Ignore frame indications if rxskip is set */
2600 if (bus->rxskip)
2601 intstatus &= ~I_HMB_FRAME_IND;
2602
2603 /* On frame indication, read available frames */
Hante Meulemanb6a8cf22014-02-25 20:30:33 +01002604 if ((intstatus & I_HMB_FRAME_IND) && (bus->clkstate == CLK_AVAIL)) {
2605 brcmf_sdio_readframes(bus, bus->rxbound);
Franky Lin4754fce2012-09-19 22:21:11 +02002606 if (!bus->rxpending)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002607 intstatus &= ~I_HMB_FRAME_IND;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002608 }
2609
2610 /* Keep still-pending events for next scheduling */
Hante Meuleman5cbb9c22014-03-06 10:16:12 +01002611 if (intstatus)
Vineet Guptad3928d02015-07-09 13:43:18 +05302612 atomic_or(intstatus, &bus->intstatus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002613
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002614 brcmf_sdio_clrintr(bus);
Franky Linba89bf12012-04-27 18:56:59 -07002615
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002616 if (bus->ctrl_frame_stat && (bus->clkstate == CLK_AVAIL) &&
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002617 data_ok(bus)) {
2618 sdio_claim_host(bus->sdiodev->func[1]);
Hante Meuleman449e58b2015-03-06 18:40:40 +01002619 if (bus->ctrl_frame_stat) {
2620 err = brcmf_sdio_tx_ctrlframe(bus, bus->ctrl_frame_buf,
2621 bus->ctrl_frame_len);
2622 bus->ctrl_frame_err = err;
Hante Meuleman2c64e162015-03-18 13:25:22 +01002623 wmb();
Hante Meuleman449e58b2015-03-06 18:40:40 +01002624 bus->ctrl_frame_stat = false;
2625 }
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002626 sdio_release_host(bus->sdiodev->func[1]);
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002627 brcmf_sdio_wait_event_wakeup(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002628 }
2629 /* Send queued frames (limit 1 if rx may still be pending) */
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002630 if ((bus->clkstate == CLK_AVAIL) && !atomic_read(&bus->fcstate) &&
2631 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) && txlimit &&
2632 data_ok(bus)) {
Franky Lin4754fce2012-09-19 22:21:11 +02002633 framecnt = bus->rxpending ? min(txlimit, bus->txminmax) :
2634 txlimit;
Hante Meulemanb6a8cf22014-02-25 20:30:33 +01002635 brcmf_sdio_sendfromq(bus, framecnt);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002636 }
2637
Arend van Spriela1ce7a02015-02-06 18:36:42 +01002638 if ((bus->sdiodev->state != BRCMF_SDIOD_DATA) || (err != 0)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002639 brcmf_err("failed backplane access over SDIO, halting operation\n");
Franky Lin45316032012-09-13 21:12:03 +02002640 atomic_set(&bus->intstatus, 0);
Hante Meulemande6878c2015-03-06 18:40:38 +01002641 if (bus->ctrl_frame_stat) {
Hante Meuleman449e58b2015-03-06 18:40:40 +01002642 sdio_claim_host(bus->sdiodev->func[1]);
2643 if (bus->ctrl_frame_stat) {
2644 bus->ctrl_frame_err = -ENODEV;
Hante Meuleman2c64e162015-03-18 13:25:22 +01002645 wmb();
Hante Meuleman449e58b2015-03-06 18:40:40 +01002646 bus->ctrl_frame_stat = false;
2647 brcmf_sdio_wait_event_wakeup(bus);
2648 }
2649 sdio_release_host(bus->sdiodev->func[1]);
Hante Meulemande6878c2015-03-06 18:40:38 +01002650 }
Franky Lin45316032012-09-13 21:12:03 +02002651 } else if (atomic_read(&bus->intstatus) ||
2652 atomic_read(&bus->ipend) > 0 ||
2653 (!atomic_read(&bus->fcstate) &&
2654 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) &&
Hante Meulemanb6a8cf22014-02-25 20:30:33 +01002655 data_ok(bus))) {
Hante Meuleman2c64e162015-03-18 13:25:22 +01002656 bus->dpc_triggered = true;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002657 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002658}
2659
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002660static struct pktq *brcmf_sdio_bus_gettxq(struct device *dev)
Arend van Spriele2432b62013-04-03 12:40:38 +02002661{
2662 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
2663 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
2664 struct brcmf_sdio *bus = sdiodev->bus;
2665
2666 return &bus->txq;
2667}
2668
Hante Meuleman84936622014-10-28 14:56:19 +01002669static bool brcmf_sdio_prec_enq(struct pktq *q, struct sk_buff *pkt, int prec)
2670{
2671 struct sk_buff *p;
2672 int eprec = -1; /* precedence to evict from */
2673
2674 /* Fast case, precedence queue is not full and we are also not
2675 * exceeding total queue length
2676 */
2677 if (!pktq_pfull(q, prec) && !pktq_full(q)) {
2678 brcmu_pktq_penq(q, prec, pkt);
2679 return true;
2680 }
2681
2682 /* Determine precedence from which to evict packet, if any */
2683 if (pktq_pfull(q, prec)) {
2684 eprec = prec;
2685 } else if (pktq_full(q)) {
2686 p = brcmu_pktq_peek_tail(q, &eprec);
2687 if (eprec > prec)
2688 return false;
2689 }
2690
2691 /* Evict if needed */
2692 if (eprec >= 0) {
2693 /* Detect queueing to unconfigured precedence */
2694 if (eprec == prec)
2695 return false; /* refuse newer (incoming) packet */
2696 /* Evict packet according to discard policy */
2697 p = brcmu_pktq_pdeq_tail(q, eprec);
2698 if (p == NULL)
2699 brcmf_err("brcmu_pktq_pdeq_tail() failed\n");
2700 brcmu_pkt_buf_free_skb(p);
2701 }
2702
2703 /* Enqueue */
2704 p = brcmu_pktq_penq(q, prec, pkt);
2705 if (p == NULL)
2706 brcmf_err("brcmu_pktq_penq() failed\n");
2707
2708 return p != NULL;
2709}
2710
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002711static int brcmf_sdio_bus_txdata(struct device *dev, struct sk_buff *pkt)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002712{
2713 int ret = -EBADE;
Arend van Spriel44ff5662014-02-25 20:30:28 +01002714 uint prec;
Franky Linbf347bb2011-11-22 17:21:56 -08002715 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01002716 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Linbf347bb2011-11-22 17:21:56 -08002717 struct brcmf_sdio *bus = sdiodev->bus;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002718
Arend van Spriel44ff5662014-02-25 20:30:28 +01002719 brcmf_dbg(TRACE, "Enter: pkt: data %p len %d\n", pkt->data, pkt->len);
Arend van Spriel5768f312015-06-11 00:12:20 +02002720 if (sdiodev->state != BRCMF_SDIOD_DATA)
2721 return -EIO;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002722
2723 /* Add space for the header */
Franky Lin706478c2013-08-10 12:27:29 +02002724 skb_push(pkt, bus->tx_hdrlen);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002725 /* precondition: IS_ALIGNED((unsigned long)(pkt->data), 2) */
2726
2727 prec = prio2prec((pkt->priority & PRIOMASK));
2728
2729 /* Check for existing queue, current flow-control,
2730 pending event, or pending clock */
2731 brcmf_dbg(TRACE, "deferring pktq len %d\n", pktq_len(&bus->txq));
Arend van Spriel80969832012-06-09 22:51:44 +02002732 bus->sdcnt.fcqueued++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002733
2734 /* Priority based enq */
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002735 spin_lock_bh(&bus->txq_lock);
Arend van Spriel5aa9f0e2014-02-25 20:30:27 +01002736 /* reset bus_flags in packet cb */
2737 *(u16 *)(pkt->cb) = 0;
Hante Meuleman84936622014-10-28 14:56:19 +01002738 if (!brcmf_sdio_prec_enq(&bus->txq, pkt, prec)) {
Franky Lin706478c2013-08-10 12:27:29 +02002739 skb_pull(pkt, bus->tx_hdrlen);
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002740 brcmf_err("out of bus->txq !!!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002741 ret = -ENOSR;
2742 } else {
2743 ret = 0;
2744 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002745
Franky Linc8bf3482011-12-16 18:37:08 -08002746 if (pktq_len(&bus->txq) >= TXHI) {
Hante Meuleman90d03ff2012-09-11 21:18:46 +02002747 bus->txoff = true;
Hante Meuleman84936622014-10-28 14:56:19 +01002748 brcmf_txflowblock(dev, true);
Franky Linc8bf3482011-12-16 18:37:08 -08002749 }
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002750 spin_unlock_bh(&bus->txq_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002751
Joe Perches8ae74652012-01-15 00:38:38 -08002752#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02002753 if (pktq_plen(&bus->txq, prec) > qcount[prec])
2754 qcount[prec] = pktq_plen(&bus->txq, prec);
2755#endif
Franky Linf1e68c22012-09-13 21:12:00 +02002756
Arend van Spriel99824642015-02-06 18:36:45 +01002757 brcmf_sdio_trigger_dpc(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002758 return ret;
2759}
2760
Joe Perches8ae74652012-01-15 00:38:38 -08002761#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02002762#define CONSOLE_LINE_MAX 192
2763
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002764static int brcmf_sdio_readconsole(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002765{
2766 struct brcmf_console *c = &bus->console;
2767 u8 line[CONSOLE_LINE_MAX], ch;
2768 u32 n, idx, addr;
2769 int rv;
2770
2771 /* Don't do anything until FWREADY updates console address */
2772 if (bus->console_addr == 0)
2773 return 0;
2774
2775 /* Read console log struct */
2776 addr = bus->console_addr + offsetof(struct rte_console, log_le);
Arend van Spriela39be272013-12-12 11:58:58 +01002777 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, (u8 *)&c->log_le,
2778 sizeof(c->log_le));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002779 if (rv < 0)
2780 return rv;
2781
2782 /* Allocate console buffer (one time only) */
2783 if (c->buf == NULL) {
2784 c->bufsize = le32_to_cpu(c->log_le.buf_size);
2785 c->buf = kmalloc(c->bufsize, GFP_ATOMIC);
2786 if (c->buf == NULL)
2787 return -ENOMEM;
2788 }
2789
2790 idx = le32_to_cpu(c->log_le.idx);
2791
2792 /* Protect against corrupt value */
2793 if (idx > c->bufsize)
2794 return -EBADE;
2795
2796 /* Skip reading the console buffer if the index pointer
2797 has not moved */
2798 if (idx == c->last)
2799 return 0;
2800
2801 /* Read the console buffer */
2802 addr = le32_to_cpu(c->log_le.buf);
Arend van Spriela39be272013-12-12 11:58:58 +01002803 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, c->buf, c->bufsize);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002804 if (rv < 0)
2805 return rv;
2806
2807 while (c->last != idx) {
2808 for (n = 0; n < CONSOLE_LINE_MAX - 2; n++) {
2809 if (c->last == idx) {
2810 /* This would output a partial line.
2811 * Instead, back up
2812 * the buffer pointer and output this
2813 * line next time around.
2814 */
2815 if (c->last >= n)
2816 c->last -= n;
2817 else
2818 c->last = c->bufsize - n;
2819 goto break2;
2820 }
2821 ch = c->buf[c->last];
2822 c->last = (c->last + 1) % c->bufsize;
2823 if (ch == '\n')
2824 break;
2825 line[n] = ch;
2826 }
2827
2828 if (n > 0) {
2829 if (line[n - 1] == '\r')
2830 n--;
2831 line[n] = 0;
Joe Perches18aad4f2012-01-15 00:38:42 -08002832 pr_debug("CONSOLE: %s\n", line);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002833 }
2834 }
2835break2:
2836
2837 return 0;
2838}
Joe Perches8ae74652012-01-15 00:38:38 -08002839#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02002840
Franky Linfcf094f2011-12-16 18:37:16 -08002841static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002842brcmf_sdio_bus_txctl(struct device *dev, unsigned char *msg, uint msglen)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002843{
Franky Lin47a1ce72011-11-22 17:21:55 -08002844 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01002845 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Lin47a1ce72011-11-22 17:21:55 -08002846 struct brcmf_sdio *bus = sdiodev->bus;
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002847 int ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002848
2849 brcmf_dbg(TRACE, "Enter\n");
Arend van Spriel5768f312015-06-11 00:12:20 +02002850 if (sdiodev->state != BRCMF_SDIOD_DATA)
2851 return -EIO;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002852
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002853 /* Send from dpc */
2854 bus->ctrl_frame_buf = msg;
2855 bus->ctrl_frame_len = msglen;
Hante Meuleman2c64e162015-03-18 13:25:22 +01002856 wmb();
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002857 bus->ctrl_frame_stat = true;
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002858
Arend van Spriel99824642015-02-06 18:36:45 +01002859 brcmf_sdio_trigger_dpc(bus);
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002860 wait_event_interruptible_timeout(bus->ctrl_wait, !bus->ctrl_frame_stat,
Arend van Spriel63ce3d52016-01-05 11:05:48 +01002861 CTL_DONE_TIMEOUT);
Hante Meuleman449e58b2015-03-06 18:40:40 +01002862 ret = 0;
2863 if (bus->ctrl_frame_stat) {
2864 sdio_claim_host(bus->sdiodev->func[1]);
2865 if (bus->ctrl_frame_stat) {
2866 brcmf_dbg(SDIO, "ctrl_frame timeout\n");
2867 bus->ctrl_frame_stat = false;
2868 ret = -ETIMEDOUT;
2869 }
2870 sdio_release_host(bus->sdiodev->func[1]);
2871 }
2872 if (!ret) {
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002873 brcmf_dbg(SDIO, "ctrl_frame complete, err=%d\n",
2874 bus->ctrl_frame_err);
Hante Meuleman2c64e162015-03-18 13:25:22 +01002875 rmb();
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002876 ret = bus->ctrl_frame_err;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002877 }
2878
Arend van Spriel5b435de2011-10-05 13:19:03 +02002879 if (ret)
Arend van Spriel80969832012-06-09 22:51:44 +02002880 bus->sdcnt.tx_ctlerrs++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002881 else
Arend van Spriel80969832012-06-09 22:51:44 +02002882 bus->sdcnt.tx_ctlpkts++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002883
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002884 return ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002885}
2886
Arend van Spriel80969832012-06-09 22:51:44 +02002887#ifdef DEBUG
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002888static int brcmf_sdio_dump_console(struct seq_file *seq, struct brcmf_sdio *bus,
2889 struct sdpcm_shared *sh)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002890{
2891 u32 addr, console_ptr, console_size, console_index;
2892 char *conbuf = NULL;
2893 __le32 sh_val;
2894 int rv;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002895
2896 /* obtain console information from device memory */
2897 addr = sh->console_addr + offsetof(struct rte_console, log_le);
Arend van Spriela39be272013-12-12 11:58:58 +01002898 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
2899 (u8 *)&sh_val, sizeof(u32));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002900 if (rv < 0)
2901 return rv;
2902 console_ptr = le32_to_cpu(sh_val);
2903
2904 addr = sh->console_addr + offsetof(struct rte_console, log_le.buf_size);
Arend van Spriela39be272013-12-12 11:58:58 +01002905 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
2906 (u8 *)&sh_val, sizeof(u32));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002907 if (rv < 0)
2908 return rv;
2909 console_size = le32_to_cpu(sh_val);
2910
2911 addr = sh->console_addr + offsetof(struct rte_console, log_le.idx);
Arend van Spriela39be272013-12-12 11:58:58 +01002912 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
2913 (u8 *)&sh_val, sizeof(u32));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002914 if (rv < 0)
2915 return rv;
2916 console_index = le32_to_cpu(sh_val);
2917
2918 /* allocate buffer for console data */
2919 if (console_size <= CONSOLE_BUFFER_MAX)
2920 conbuf = vzalloc(console_size+1);
2921
2922 if (!conbuf)
2923 return -ENOMEM;
2924
2925 /* obtain the console data from device */
2926 conbuf[console_size] = '\0';
Arend van Spriela39be272013-12-12 11:58:58 +01002927 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, console_ptr, (u8 *)conbuf,
2928 console_size);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002929 if (rv < 0)
2930 goto done;
2931
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002932 rv = seq_write(seq, conbuf + console_index,
2933 console_size - console_index);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002934 if (rv < 0)
2935 goto done;
2936
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002937 if (console_index > 0)
2938 rv = seq_write(seq, conbuf, console_index - 1);
2939
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002940done:
2941 vfree(conbuf);
2942 return rv;
2943}
2944
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002945static int brcmf_sdio_trap_info(struct seq_file *seq, struct brcmf_sdio *bus,
2946 struct sdpcm_shared *sh)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002947{
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002948 int error;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002949 struct brcmf_trap_info tr;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002950
Piotr Haberbaa9e602013-04-05 10:57:42 +02002951 if ((sh->flags & SDPCM_SHARED_TRAP) == 0) {
2952 brcmf_dbg(INFO, "no trap in firmware\n");
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002953 return 0;
Piotr Haberbaa9e602013-04-05 10:57:42 +02002954 }
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002955
Arend van Spriela39be272013-12-12 11:58:58 +01002956 error = brcmf_sdiod_ramrw(bus->sdiodev, false, sh->trap_addr, (u8 *)&tr,
2957 sizeof(struct brcmf_trap_info));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002958 if (error < 0)
2959 return error;
2960
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002961 seq_printf(seq,
2962 "dongle trap info: type 0x%x @ epc 0x%08x\n"
2963 " cpsr 0x%08x spsr 0x%08x sp 0x%08x\n"
2964 " lr 0x%08x pc 0x%08x offset 0x%x\n"
2965 " r0 0x%08x r1 0x%08x r2 0x%08x r3 0x%08x\n"
2966 " r4 0x%08x r5 0x%08x r6 0x%08x r7 0x%08x\n",
2967 le32_to_cpu(tr.type), le32_to_cpu(tr.epc),
2968 le32_to_cpu(tr.cpsr), le32_to_cpu(tr.spsr),
2969 le32_to_cpu(tr.r13), le32_to_cpu(tr.r14),
2970 le32_to_cpu(tr.pc), sh->trap_addr,
2971 le32_to_cpu(tr.r0), le32_to_cpu(tr.r1),
2972 le32_to_cpu(tr.r2), le32_to_cpu(tr.r3),
2973 le32_to_cpu(tr.r4), le32_to_cpu(tr.r5),
2974 le32_to_cpu(tr.r6), le32_to_cpu(tr.r7));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002975
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002976 return 0;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002977}
2978
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002979static int brcmf_sdio_assert_info(struct seq_file *seq, struct brcmf_sdio *bus,
2980 struct sdpcm_shared *sh)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002981{
2982 int error = 0;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002983 char file[80] = "?";
2984 char expr[80] = "<???>";
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002985
2986 if ((sh->flags & SDPCM_SHARED_ASSERT_BUILT) == 0) {
2987 brcmf_dbg(INFO, "firmware not built with -assert\n");
2988 return 0;
2989 } else if ((sh->flags & SDPCM_SHARED_ASSERT) == 0) {
2990 brcmf_dbg(INFO, "no assert in dongle\n");
2991 return 0;
2992 }
2993
Franky Lin38b0b0d2012-11-05 16:22:24 -08002994 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002995 if (sh->assert_file_addr != 0) {
Arend van Spriela39be272013-12-12 11:58:58 +01002996 error = brcmf_sdiod_ramrw(bus->sdiodev, false,
2997 sh->assert_file_addr, (u8 *)file, 80);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002998 if (error < 0)
2999 return error;
3000 }
3001 if (sh->assert_exp_addr != 0) {
Arend van Spriela39be272013-12-12 11:58:58 +01003002 error = brcmf_sdiod_ramrw(bus->sdiodev, false,
3003 sh->assert_exp_addr, (u8 *)expr, 80);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003004 if (error < 0)
3005 return error;
3006 }
Franky Lin38b0b0d2012-11-05 16:22:24 -08003007 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003008
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003009 seq_printf(seq, "dongle assert: %s:%d: assert(%s)\n",
3010 file, sh->assert_line, expr);
3011 return 0;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003012}
3013
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003014static int brcmf_sdio_checkdied(struct brcmf_sdio *bus)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003015{
3016 int error;
3017 struct sdpcm_shared sh;
3018
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003019 error = brcmf_sdio_readshared(bus, &sh);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003020
3021 if (error < 0)
3022 return error;
3023
3024 if ((sh.flags & SDPCM_SHARED_ASSERT_BUILT) == 0)
3025 brcmf_dbg(INFO, "firmware not built with -assert\n");
3026 else if (sh.flags & SDPCM_SHARED_ASSERT)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003027 brcmf_err("assertion in dongle\n");
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003028
3029 if (sh.flags & SDPCM_SHARED_TRAP)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003030 brcmf_err("firmware trap in dongle\n");
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003031
3032 return 0;
3033}
3034
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003035static int brcmf_sdio_died_dump(struct seq_file *seq, struct brcmf_sdio *bus)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003036{
3037 int error = 0;
3038 struct sdpcm_shared sh;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003039
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003040 error = brcmf_sdio_readshared(bus, &sh);
3041 if (error < 0)
3042 goto done;
3043
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003044 error = brcmf_sdio_assert_info(seq, bus, &sh);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003045 if (error < 0)
3046 goto done;
Piotr Haberbaa9e602013-04-05 10:57:42 +02003047
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003048 error = brcmf_sdio_trap_info(seq, bus, &sh);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003049 if (error < 0)
3050 goto done;
3051
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003052 error = brcmf_sdio_dump_console(seq, bus, &sh);
Piotr Haberbaa9e602013-04-05 10:57:42 +02003053
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003054done:
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003055 return error;
3056}
3057
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003058static int brcmf_sdio_forensic_read(struct seq_file *seq, void *data)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003059{
Arend van Spriel82d957e2014-07-12 08:49:36 +02003060 struct brcmf_bus *bus_if = dev_get_drvdata(seq->private);
3061 struct brcmf_sdio *bus = bus_if->bus_priv.sdio->bus;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003062
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003063 return brcmf_sdio_died_dump(seq, bus);
3064}
3065
Arend van Spriel82d957e2014-07-12 08:49:36 +02003066static int brcmf_debugfs_sdio_count_read(struct seq_file *seq, void *data)
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003067{
Arend van Spriel82d957e2014-07-12 08:49:36 +02003068 struct brcmf_bus *bus_if = dev_get_drvdata(seq->private);
3069 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3070 struct brcmf_sdio_count *sdcnt = &sdiodev->bus->sdcnt;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003071
Arend van Spriel82d957e2014-07-12 08:49:36 +02003072 seq_printf(seq,
3073 "intrcount: %u\nlastintrs: %u\n"
3074 "pollcnt: %u\nregfails: %u\n"
3075 "tx_sderrs: %u\nfcqueued: %u\n"
3076 "rxrtx: %u\nrx_toolong: %u\n"
3077 "rxc_errors: %u\nrx_hdrfail: %u\n"
3078 "rx_badhdr: %u\nrx_badseq: %u\n"
3079 "fc_rcvd: %u\nfc_xoff: %u\n"
3080 "fc_xon: %u\nrxglomfail: %u\n"
3081 "rxglomframes: %u\nrxglompkts: %u\n"
3082 "f2rxhdrs: %u\nf2rxdata: %u\n"
3083 "f2txdata: %u\nf1regdata: %u\n"
3084 "tickcnt: %u\ntx_ctlerrs: %lu\n"
3085 "tx_ctlpkts: %lu\nrx_ctlerrs: %lu\n"
3086 "rx_ctlpkts: %lu\nrx_readahead: %lu\n",
3087 sdcnt->intrcount, sdcnt->lastintrs,
3088 sdcnt->pollcnt, sdcnt->regfails,
3089 sdcnt->tx_sderrs, sdcnt->fcqueued,
3090 sdcnt->rxrtx, sdcnt->rx_toolong,
3091 sdcnt->rxc_errors, sdcnt->rx_hdrfail,
3092 sdcnt->rx_badhdr, sdcnt->rx_badseq,
3093 sdcnt->fc_rcvd, sdcnt->fc_xoff,
3094 sdcnt->fc_xon, sdcnt->rxglomfail,
3095 sdcnt->rxglomframes, sdcnt->rxglompkts,
3096 sdcnt->f2rxhdrs, sdcnt->f2rxdata,
3097 sdcnt->f2txdata, sdcnt->f1regdata,
3098 sdcnt->tickcnt, sdcnt->tx_ctlerrs,
3099 sdcnt->tx_ctlpkts, sdcnt->rx_ctlerrs,
3100 sdcnt->rx_ctlpkts, sdcnt->rx_readahead_cnt);
3101
3102 return 0;
3103}
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003104
Arend van Spriel80969832012-06-09 22:51:44 +02003105static void brcmf_sdio_debugfs_create(struct brcmf_sdio *bus)
3106{
3107 struct brcmf_pub *drvr = bus->sdiodev->bus_if->drvr;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003108 struct dentry *dentry = brcmf_debugfs_get_devdir(drvr);
Arend van Spriel80969832012-06-09 22:51:44 +02003109
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003110 if (IS_ERR_OR_NULL(dentry))
3111 return;
3112
Arend van Spriel9d6c1dc2015-08-26 22:15:01 +02003113 bus->console_interval = BRCMF_CONSOLE;
3114
Arend van Spriel82d957e2014-07-12 08:49:36 +02003115 brcmf_debugfs_add_entry(drvr, "forensics", brcmf_sdio_forensic_read);
3116 brcmf_debugfs_add_entry(drvr, "counters",
3117 brcmf_debugfs_sdio_count_read);
Daniel Kim0801e6c2014-01-29 15:32:13 +01003118 debugfs_create_u32("console_interval", 0644, dentry,
3119 &bus->console_interval);
Arend van Spriel80969832012-06-09 22:51:44 +02003120}
3121#else
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003122static int brcmf_sdio_checkdied(struct brcmf_sdio *bus)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003123{
3124 return 0;
3125}
3126
Arend van Spriel80969832012-06-09 22:51:44 +02003127static void brcmf_sdio_debugfs_create(struct brcmf_sdio *bus)
3128{
3129}
3130#endif /* DEBUG */
3131
Franky Linfcf094f2011-12-16 18:37:16 -08003132static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003133brcmf_sdio_bus_rxctl(struct device *dev, unsigned char *msg, uint msglen)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003134{
3135 int timeleft;
3136 uint rxlen = 0;
3137 bool pending;
Franky Lindd43a012012-11-05 16:22:22 -08003138 u8 *buf;
Franky Lin532cdd32011-11-22 17:21:54 -08003139 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01003140 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Lin532cdd32011-11-22 17:21:54 -08003141 struct brcmf_sdio *bus = sdiodev->bus;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003142
3143 brcmf_dbg(TRACE, "Enter\n");
Arend van Spriel5768f312015-06-11 00:12:20 +02003144 if (sdiodev->state != BRCMF_SDIOD_DATA)
3145 return -EIO;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003146
3147 /* Wait until control frame is available */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003148 timeleft = brcmf_sdio_dcmd_resp_wait(bus, &bus->rxlen, &pending);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003149
Franky Lindd43a012012-11-05 16:22:22 -08003150 spin_lock_bh(&bus->rxctl_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003151 rxlen = bus->rxlen;
3152 memcpy(msg, bus->rxctl, min(msglen, rxlen));
Franky Lindd43a012012-11-05 16:22:22 -08003153 bus->rxctl = NULL;
3154 buf = bus->rxctl_orig;
3155 bus->rxctl_orig = NULL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003156 bus->rxlen = 0;
Franky Lindd43a012012-11-05 16:22:22 -08003157 spin_unlock_bh(&bus->rxctl_lock);
3158 vfree(buf);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003159
3160 if (rxlen) {
3161 brcmf_dbg(CTL, "resumed on rxctl frame, got %d expected %d\n",
3162 rxlen, msglen);
3163 } else if (timeleft == 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003164 brcmf_err("resumed on timeout\n");
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003165 brcmf_sdio_checkdied(bus);
Joe Perches23677ce2012-02-09 11:17:23 +00003166 } else if (pending) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003167 brcmf_dbg(CTL, "cancelled\n");
3168 return -ERESTARTSYS;
3169 } else {
3170 brcmf_dbg(CTL, "resumed for unknown reason?\n");
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003171 brcmf_sdio_checkdied(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003172 }
3173
3174 if (rxlen)
Arend van Spriel80969832012-06-09 22:51:44 +02003175 bus->sdcnt.rx_ctlpkts++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003176 else
Arend van Spriel80969832012-06-09 22:51:44 +02003177 bus->sdcnt.rx_ctlerrs++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003178
3179 return rxlen ? (int)rxlen : -ETIMEDOUT;
3180}
3181
Hante Meulemana74d0362014-01-13 22:20:22 +01003182#ifdef DEBUG
3183static bool
3184brcmf_sdio_verifymemory(struct brcmf_sdio_dev *sdiodev, u32 ram_addr,
3185 u8 *ram_data, uint ram_sz)
3186{
3187 char *ram_cmp;
3188 int err;
3189 bool ret = true;
3190 int address;
3191 int offset;
3192 int len;
3193
3194 /* read back and verify */
3195 brcmf_dbg(INFO, "Compare RAM dl & ul at 0x%08x; size=%d\n", ram_addr,
3196 ram_sz);
3197 ram_cmp = kmalloc(MEMBLOCK, GFP_KERNEL);
3198 /* do not proceed while no memory but */
3199 if (!ram_cmp)
3200 return true;
3201
3202 address = ram_addr;
3203 offset = 0;
3204 while (offset < ram_sz) {
3205 len = ((offset + MEMBLOCK) < ram_sz) ? MEMBLOCK :
3206 ram_sz - offset;
3207 err = brcmf_sdiod_ramrw(sdiodev, false, address, ram_cmp, len);
3208 if (err) {
3209 brcmf_err("error %d on reading %d membytes at 0x%08x\n",
3210 err, len, address);
3211 ret = false;
3212 break;
3213 } else if (memcmp(ram_cmp, &ram_data[offset], len)) {
3214 brcmf_err("Downloaded RAM image is corrupted, block offset is %d, len is %d\n",
3215 offset, len);
3216 ret = false;
3217 break;
3218 }
3219 offset += len;
3220 address += len;
3221 }
3222
3223 kfree(ram_cmp);
3224
3225 return ret;
3226}
3227#else /* DEBUG */
3228static bool
3229brcmf_sdio_verifymemory(struct brcmf_sdio_dev *sdiodev, u32 ram_addr,
3230 u8 *ram_data, uint ram_sz)
3231{
3232 return true;
3233}
3234#endif /* DEBUG */
3235
Arend van Spriel33556502014-01-13 22:20:25 +01003236static int brcmf_sdio_download_code_file(struct brcmf_sdio *bus,
3237 const struct firmware *fw)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003238{
Hante Meulemanf2c44fe2013-09-25 13:05:45 +02003239 int err;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003240
Hante Meulemana74d0362014-01-13 22:20:22 +01003241 brcmf_dbg(TRACE, "Enter\n");
3242
Hante Meulemanf9951c12014-01-29 15:32:17 +01003243 err = brcmf_sdiod_ramrw(bus->sdiodev, true, bus->ci->rambase,
3244 (u8 *)fw->data, fw->size);
3245 if (err)
3246 brcmf_err("error %d on writing %d membytes at 0x%08x\n",
3247 err, (int)fw->size, bus->ci->rambase);
3248 else if (!brcmf_sdio_verifymemory(bus->sdiodev, bus->ci->rambase,
3249 (u8 *)fw->data, fw->size))
3250 err = -EIO;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003251
Hante Meulemanf2c44fe2013-09-25 13:05:45 +02003252 return err;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003253}
3254
Arend van Spriel33556502014-01-13 22:20:25 +01003255static int brcmf_sdio_download_nvram(struct brcmf_sdio *bus,
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003256 void *vars, u32 varsz)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003257{
Hante Meulemana74d0362014-01-13 22:20:22 +01003258 int address;
3259 int err;
3260
3261 brcmf_dbg(TRACE, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003262
Hante Meulemana74d0362014-01-13 22:20:22 +01003263 address = bus->ci->ramsize - varsz + bus->ci->rambase;
3264 err = brcmf_sdiod_ramrw(bus->sdiodev, true, address, vars, varsz);
3265 if (err)
3266 brcmf_err("error %d on writing %d nvram bytes at 0x%08x\n",
3267 err, varsz, address);
3268 else if (!brcmf_sdio_verifymemory(bus->sdiodev, address, vars, varsz))
3269 err = -EIO;
3270
Hante Meulemana74d0362014-01-13 22:20:22 +01003271 return err;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003272}
3273
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003274static int brcmf_sdio_download_firmware(struct brcmf_sdio *bus,
3275 const struct firmware *fw,
3276 void *nvram, u32 nvlen)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003277{
Markus Elfring9e129042016-03-18 13:23:24 +11003278 int bcmerror;
Arend van Spriel33556502014-01-13 22:20:25 +01003279 u32 rstvec;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003280
3281 sdio_claim_host(bus->sdiodev->func[1]);
3282 brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003283
Arend van Spriel33556502014-01-13 22:20:25 +01003284 rstvec = get_unaligned_le32(fw->data);
3285 brcmf_dbg(SDIO, "firmware rstvec: %x\n", rstvec);
3286
3287 bcmerror = brcmf_sdio_download_code_file(bus, fw);
3288 release_firmware(fw);
3289 if (bcmerror) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003290 brcmf_err("dongle image file download failed\n");
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003291 brcmf_fw_nvram_free(nvram);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003292 goto err;
3293 }
3294
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003295 bcmerror = brcmf_sdio_download_nvram(bus, nvram, nvlen);
3296 brcmf_fw_nvram_free(nvram);
Arend van Spriel33556502014-01-13 22:20:25 +01003297 if (bcmerror) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003298 brcmf_err("dongle nvram file download failed\n");
Franky Lin3eaa9562013-04-02 21:06:20 +02003299 goto err;
3300 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02003301
3302 /* Take arm out of reset */
Arend van Sprield380ebc2015-03-11 16:11:29 +01003303 if (!brcmf_chip_set_active(bus->ci, rstvec)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003304 brcmf_err("error getting out of ARM core reset\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003305 goto err;
3306 }
3307
Hante Meulemana1cee862015-01-25 20:31:35 +01003308 /* Allow full data communication using DPC from now on. */
Arend van Spriela1ce7a02015-02-06 18:36:42 +01003309 brcmf_sdiod_change_state(bus->sdiodev, BRCMF_SDIOD_DATA);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003310 bcmerror = 0;
3311
3312err:
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003313 brcmf_sdio_clkctl(bus, CLK_SDONLY, false);
3314 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003315 return bcmerror;
3316}
3317
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003318static void brcmf_sdio_sr_init(struct brcmf_sdio *bus)
Piotr Haber4a3da992013-04-11 13:28:46 +02003319{
3320 int err = 0;
3321 u8 val;
3322
3323 brcmf_dbg(TRACE, "Enter\n");
3324
Arend van Spriela39be272013-12-12 11:58:58 +01003325 val = brcmf_sdiod_regrb(bus->sdiodev, SBSDIO_FUNC1_WAKEUPCTRL, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003326 if (err) {
3327 brcmf_err("error reading SBSDIO_FUNC1_WAKEUPCTRL\n");
3328 return;
3329 }
3330
3331 val |= 1 << SBSDIO_FUNC1_WCTRL_HTWAIT_SHIFT;
Arend van Spriela39be272013-12-12 11:58:58 +01003332 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_WAKEUPCTRL, val, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003333 if (err) {
3334 brcmf_err("error writing SBSDIO_FUNC1_WAKEUPCTRL\n");
3335 return;
3336 }
3337
3338 /* Add CMD14 Support */
Arend van Spriela39be272013-12-12 11:58:58 +01003339 brcmf_sdiod_regwb(bus->sdiodev, SDIO_CCCR_BRCM_CARDCAP,
3340 (SDIO_CCCR_BRCM_CARDCAP_CMD14_SUPPORT |
3341 SDIO_CCCR_BRCM_CARDCAP_CMD14_EXT),
3342 &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003343 if (err) {
3344 brcmf_err("error writing SDIO_CCCR_BRCM_CARDCAP\n");
3345 return;
3346 }
3347
Arend van Spriela39be272013-12-12 11:58:58 +01003348 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3349 SBSDIO_FORCE_HT, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003350 if (err) {
3351 brcmf_err("error writing SBSDIO_FUNC1_CHIPCLKCSR\n");
3352 return;
3353 }
3354
3355 /* set flag */
3356 bus->sr_enabled = true;
3357 brcmf_dbg(INFO, "SR enabled\n");
3358}
3359
3360/* enable KSO bit */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003361static int brcmf_sdio_kso_init(struct brcmf_sdio *bus)
Piotr Haber4a3da992013-04-11 13:28:46 +02003362{
3363 u8 val;
3364 int err = 0;
3365
3366 brcmf_dbg(TRACE, "Enter\n");
3367
3368 /* KSO bit added in SDIO core rev 12 */
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003369 if (brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV)->rev < 12)
Piotr Haber4a3da992013-04-11 13:28:46 +02003370 return 0;
3371
Arend van Spriela39be272013-12-12 11:58:58 +01003372 val = brcmf_sdiod_regrb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003373 if (err) {
3374 brcmf_err("error reading SBSDIO_FUNC1_SLEEPCSR\n");
3375 return err;
3376 }
3377
3378 if (!(val & SBSDIO_FUNC1_SLEEPCSR_KSO_MASK)) {
3379 val |= (SBSDIO_FUNC1_SLEEPCSR_KSO_EN <<
3380 SBSDIO_FUNC1_SLEEPCSR_KSO_SHIFT);
Arend van Spriela39be272013-12-12 11:58:58 +01003381 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
3382 val, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003383 if (err) {
3384 brcmf_err("error writing SBSDIO_FUNC1_SLEEPCSR\n");
3385 return err;
3386 }
3387 }
3388
3389 return 0;
3390}
3391
3392
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003393static int brcmf_sdio_bus_preinit(struct device *dev)
Arend van Sprielcf458282013-11-29 11:48:15 +01003394{
3395 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3396 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3397 struct brcmf_sdio *bus = sdiodev->bus;
Franky Lin8da9d2c2013-11-29 12:25:10 +01003398 uint pad_size;
Arend van Sprielcf458282013-11-29 11:48:15 +01003399 u32 value;
Arend van Sprielcf458282013-11-29 11:48:15 +01003400 int err;
3401
Franky Lin8da9d2c2013-11-29 12:25:10 +01003402 /* the commands below use the terms tx and rx from
3403 * a device perspective, ie. bus:txglom affects the
3404 * bus transfers from device to host.
3405 */
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003406 if (brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV)->rev < 12) {
Arend van Sprielcf458282013-11-29 11:48:15 +01003407 /* for sdio core rev < 12, disable txgloming */
3408 value = 0;
3409 err = brcmf_iovar_data_set(dev, "bus:txglom", &value,
3410 sizeof(u32));
3411 } else {
3412 /* otherwise, set txglomalign */
Hante Meulemanaf5b5e62016-02-17 11:27:08 +01003413 value = sdiodev->settings->bus.sdio.sd_sgentry_align;
Arend van Sprielcf458282013-11-29 11:48:15 +01003414 /* SDIO ADMA requires at least 32 bit alignment */
3415 value = max_t(u32, value, 4);
3416 err = brcmf_iovar_data_set(dev, "bus:txglomalign", &value,
3417 sizeof(u32));
3418 }
Franky Lin8da9d2c2013-11-29 12:25:10 +01003419
3420 if (err < 0)
3421 goto done;
3422
3423 bus->tx_hdrlen = SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN;
3424 if (sdiodev->sg_support) {
3425 bus->txglom = false;
3426 value = 1;
3427 pad_size = bus->sdiodev->func[2]->cur_blksize << 1;
Franky Lin8da9d2c2013-11-29 12:25:10 +01003428 err = brcmf_iovar_data_set(bus->sdiodev->dev, "bus:rxglom",
3429 &value, sizeof(u32));
3430 if (err < 0) {
3431 /* bus:rxglom is allowed to fail */
3432 err = 0;
3433 } else {
3434 bus->txglom = true;
3435 bus->tx_hdrlen += SDPCM_HWEXT_LEN;
3436 }
3437 }
3438 brcmf_bus_add_txhdrlen(bus->sdiodev->dev, bus->tx_hdrlen);
3439
3440done:
Arend van Sprielcf458282013-11-29 11:48:15 +01003441 return err;
3442}
3443
Arend van Sprielff4445a2015-10-08 20:33:11 +02003444static size_t brcmf_sdio_bus_get_ramsize(struct device *dev)
3445{
3446 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3447 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3448 struct brcmf_sdio *bus = sdiodev->bus;
3449
3450 return bus->ci->ramsize - bus->ci->srsize;
3451}
3452
3453static int brcmf_sdio_bus_get_memdump(struct device *dev, void *data,
3454 size_t mem_size)
3455{
3456 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3457 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3458 struct brcmf_sdio *bus = sdiodev->bus;
3459 int err;
3460 int address;
3461 int offset;
3462 int len;
3463
3464 brcmf_dbg(INFO, "dump at 0x%08x: size=%zu\n", bus->ci->rambase,
3465 mem_size);
3466
3467 address = bus->ci->rambase;
3468 offset = err = 0;
3469 sdio_claim_host(sdiodev->func[1]);
3470 while (offset < mem_size) {
3471 len = ((offset + MEMBLOCK) < mem_size) ? MEMBLOCK :
3472 mem_size - offset;
3473 err = brcmf_sdiod_ramrw(sdiodev, false, address, data, len);
3474 if (err) {
3475 brcmf_err("error %d on reading %d membytes at 0x%08x\n",
3476 err, len, address);
3477 goto done;
3478 }
3479 data += len;
3480 offset += len;
3481 address += len;
3482 }
3483
3484done:
3485 sdio_release_host(sdiodev->func[1]);
3486 return err;
3487}
3488
Arend van Spriel99824642015-02-06 18:36:45 +01003489void brcmf_sdio_trigger_dpc(struct brcmf_sdio *bus)
3490{
Hante Meuleman2c64e162015-03-18 13:25:22 +01003491 if (!bus->dpc_triggered) {
3492 bus->dpc_triggered = true;
Arend van Spriel99824642015-02-06 18:36:45 +01003493 queue_work(bus->brcmf_wq, &bus->datawork);
3494 }
3495}
3496
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003497void brcmf_sdio_isr(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003498{
Arend van Spriel5b435de2011-10-05 13:19:03 +02003499 brcmf_dbg(TRACE, "Enter\n");
3500
3501 if (!bus) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003502 brcmf_err("bus is null pointer, exiting\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003503 return;
3504 }
3505
Arend van Spriel5b435de2011-10-05 13:19:03 +02003506 /* Count the interrupt call */
Arend van Spriel80969832012-06-09 22:51:44 +02003507 bus->sdcnt.intrcount++;
Franky Lin45316032012-09-13 21:12:03 +02003508 if (in_interrupt())
3509 atomic_set(&bus->ipend, 1);
3510 else
3511 if (brcmf_sdio_intr_rstatus(bus)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003512 brcmf_err("failed backplane access\n");
Franky Lin45316032012-09-13 21:12:03 +02003513 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02003514
Arend van Spriel5b435de2011-10-05 13:19:03 +02003515 /* Disable additional interrupts (is this needed now)? */
3516 if (!bus->intr)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003517 brcmf_err("isr w/o interrupt configured!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003518
Hante Meuleman2c64e162015-03-18 13:25:22 +01003519 bus->dpc_triggered = true;
Franky Linf1e68c22012-09-13 21:12:00 +02003520 queue_work(bus->brcmf_wq, &bus->datawork);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003521}
3522
Hante Meulemanb441ba82015-03-06 18:40:39 +01003523static void brcmf_sdio_bus_watchdog(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003524{
Arend van Spriel5b435de2011-10-05 13:19:03 +02003525 brcmf_dbg(TIMER, "Enter\n");
3526
Arend van Spriel5b435de2011-10-05 13:19:03 +02003527 /* Poll period: check device if appropriate. */
Piotr Haber4a3da992013-04-11 13:28:46 +02003528 if (!bus->sr_enabled &&
3529 bus->poll && (++bus->polltick >= bus->pollrate)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003530 u32 intstatus = 0;
3531
3532 /* Reset poll tick */
3533 bus->polltick = 0;
3534
3535 /* Check device if no interrupts */
Arend van Spriel80969832012-06-09 22:51:44 +02003536 if (!bus->intr ||
3537 (bus->sdcnt.intrcount == bus->sdcnt.lastintrs)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003538
Hante Meuleman2c64e162015-03-18 13:25:22 +01003539 if (!bus->dpc_triggered) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003540 u8 devpend;
Arend van Sprielfccfe932013-06-18 13:29:26 +02003541
Franky Lin38b0b0d2012-11-05 16:22:24 -08003542 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriela39be272013-12-12 11:58:58 +01003543 devpend = brcmf_sdiod_regrb(bus->sdiodev,
3544 SDIO_CCCR_INTx,
3545 NULL);
Franky Lin38b0b0d2012-11-05 16:22:24 -08003546 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel99824642015-02-06 18:36:45 +01003547 intstatus = devpend & (INTR_STATUS_FUNC1 |
3548 INTR_STATUS_FUNC2);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003549 }
3550
3551 /* If there is something, make like the ISR and
3552 schedule the DPC */
3553 if (intstatus) {
Arend van Spriel80969832012-06-09 22:51:44 +02003554 bus->sdcnt.pollcnt++;
Franky Lin1d382272012-09-13 21:11:59 +02003555 atomic_set(&bus->ipend, 1);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003556
Hante Meuleman2c64e162015-03-18 13:25:22 +01003557 bus->dpc_triggered = true;
Franky Linf1e68c22012-09-13 21:12:00 +02003558 queue_work(bus->brcmf_wq, &bus->datawork);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003559 }
3560 }
3561
3562 /* Update interrupt tracking */
Arend van Spriel80969832012-06-09 22:51:44 +02003563 bus->sdcnt.lastintrs = bus->sdcnt.intrcount;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003564 }
Joe Perches8ae74652012-01-15 00:38:38 -08003565#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02003566 /* Poll for console output periodically */
Arend van Spriel9d6c1dc2015-08-26 22:15:01 +02003567 if (bus->sdiodev->state == BRCMF_SDIOD_DATA && BRCMF_FWCON_ON() &&
Franky Lin8d169aa2011-11-22 17:21:52 -08003568 bus->console_interval != 0) {
Arend van Spriel63ce3d52016-01-05 11:05:48 +01003569 bus->console.count += jiffies_to_msecs(BRCMF_WD_POLL);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003570 if (bus->console.count >= bus->console_interval) {
3571 bus->console.count -= bus->console_interval;
Franky Lin38b0b0d2012-11-05 16:22:24 -08003572 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003573 /* Make sure backplane clock is on */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003574 brcmf_sdio_bus_sleep(bus, false, false);
3575 if (brcmf_sdio_readconsole(bus) < 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003576 /* stop on error */
3577 bus->console_interval = 0;
Franky Lin38b0b0d2012-11-05 16:22:24 -08003578 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003579 }
3580 }
Joe Perches8ae74652012-01-15 00:38:38 -08003581#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02003582
3583 /* On idle timeout clear activity flag and/or turn off clock */
Hante Meuleman2c64e162015-03-18 13:25:22 +01003584 if (!bus->dpc_triggered) {
3585 rmb();
3586 if ((!bus->dpc_running) && (bus->idletime > 0) &&
3587 (bus->clkstate == CLK_AVAIL)) {
3588 bus->idlecount++;
3589 if (bus->idlecount > bus->idletime) {
3590 brcmf_dbg(SDIO, "idle\n");
3591 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel4011fc42016-01-05 11:05:47 +01003592 brcmf_sdio_wd_timer(bus, false);
Hante Meuleman2c64e162015-03-18 13:25:22 +01003593 bus->idlecount = 0;
3594 brcmf_sdio_bus_sleep(bus, true, false);
3595 sdio_release_host(bus->sdiodev->func[1]);
3596 }
3597 } else {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003598 bus->idlecount = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003599 }
Hante Meulemanb441ba82015-03-06 18:40:39 +01003600 } else {
3601 bus->idlecount = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003602 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02003603}
3604
Franky Linf1e68c22012-09-13 21:12:00 +02003605static void brcmf_sdio_dataworker(struct work_struct *work)
3606{
3607 struct brcmf_sdio *bus = container_of(work, struct brcmf_sdio,
3608 datawork);
Franky Linf1e68c22012-09-13 21:12:00 +02003609
Hante Meuleman2c64e162015-03-18 13:25:22 +01003610 bus->dpc_running = true;
3611 wmb();
3612 while (ACCESS_ONCE(bus->dpc_triggered)) {
3613 bus->dpc_triggered = false;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003614 brcmf_sdio_dpc(bus);
Hante Meulemanb441ba82015-03-06 18:40:39 +01003615 bus->idlecount = 0;
Franky Linf1e68c22012-09-13 21:12:00 +02003616 }
Hante Meuleman2c64e162015-03-18 13:25:22 +01003617 bus->dpc_running = false;
Arend van Spriel99824642015-02-06 18:36:45 +01003618 if (brcmf_sdiod_freezing(bus->sdiodev)) {
3619 brcmf_sdiod_change_state(bus->sdiodev, BRCMF_SDIOD_DOWN);
3620 brcmf_sdiod_try_freeze(bus->sdiodev);
3621 brcmf_sdiod_change_state(bus->sdiodev, BRCMF_SDIOD_DATA);
3622 }
Franky Linf1e68c22012-09-13 21:12:00 +02003623}
3624
Arend van Spriel65d80d02014-01-29 15:32:14 +01003625static void
3626brcmf_sdio_drivestrengthinit(struct brcmf_sdio_dev *sdiodev,
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003627 struct brcmf_chip *ci, u32 drivestrength)
Arend van Spriel65d80d02014-01-29 15:32:14 +01003628{
3629 const struct sdiod_drive_str *str_tab = NULL;
3630 u32 str_mask;
3631 u32 str_shift;
Arend van Spriel65d80d02014-01-29 15:32:14 +01003632 u32 i;
3633 u32 drivestrength_sel = 0;
3634 u32 cc_data_temp;
3635 u32 addr;
3636
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003637 if (!(ci->cc_caps & CC_CAP_PMU))
Arend van Spriel65d80d02014-01-29 15:32:14 +01003638 return;
3639
3640 switch (SDIOD_DRVSTR_KEY(ci->chip, ci->pmurev)) {
Hante Meuleman5779ae62014-07-12 08:49:34 +02003641 case SDIOD_DRVSTR_KEY(BRCM_CC_4330_CHIP_ID, 12):
Arend van Spriel65d80d02014-01-29 15:32:14 +01003642 str_tab = sdiod_drvstr_tab1_1v8;
3643 str_mask = 0x00003800;
3644 str_shift = 11;
3645 break;
Hante Meuleman5779ae62014-07-12 08:49:34 +02003646 case SDIOD_DRVSTR_KEY(BRCM_CC_4334_CHIP_ID, 17):
Arend van Spriel65d80d02014-01-29 15:32:14 +01003647 str_tab = sdiod_drvstr_tab6_1v8;
3648 str_mask = 0x00001800;
3649 str_shift = 11;
3650 break;
Hante Meuleman5779ae62014-07-12 08:49:34 +02003651 case SDIOD_DRVSTR_KEY(BRCM_CC_43143_CHIP_ID, 17):
Arend van Spriel65d80d02014-01-29 15:32:14 +01003652 /* note: 43143 does not support tristate */
3653 i = ARRAY_SIZE(sdiod_drvstr_tab2_3v3) - 1;
3654 if (drivestrength >= sdiod_drvstr_tab2_3v3[i].strength) {
3655 str_tab = sdiod_drvstr_tab2_3v3;
3656 str_mask = 0x00000007;
3657 str_shift = 0;
3658 } else
3659 brcmf_err("Invalid SDIO Drive strength for chip %s, strength=%d\n",
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003660 ci->name, drivestrength);
Arend van Spriel65d80d02014-01-29 15:32:14 +01003661 break;
Hante Meuleman5779ae62014-07-12 08:49:34 +02003662 case SDIOD_DRVSTR_KEY(BRCM_CC_43362_CHIP_ID, 13):
Arend van Spriel65d80d02014-01-29 15:32:14 +01003663 str_tab = sdiod_drive_strength_tab5_1v8;
3664 str_mask = 0x00003800;
3665 str_shift = 11;
3666 break;
3667 default:
Wright Fengd922dfa2016-06-03 23:31:08 +02003668 brcmf_dbg(INFO, "No SDIO driver strength init needed for chip %s rev %d pmurev %d\n",
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003669 ci->name, ci->chiprev, ci->pmurev);
Arend van Spriel65d80d02014-01-29 15:32:14 +01003670 break;
3671 }
3672
3673 if (str_tab != NULL) {
Rafał Miłeckie2b397f2016-01-26 17:57:04 +01003674 struct brcmf_core *pmu = brcmf_chip_get_pmu(ci);
3675
Arend van Spriel65d80d02014-01-29 15:32:14 +01003676 for (i = 0; str_tab[i].strength != 0; i++) {
3677 if (drivestrength >= str_tab[i].strength) {
3678 drivestrength_sel = str_tab[i].sel;
3679 break;
3680 }
3681 }
Rafał Miłeckie2b397f2016-01-26 17:57:04 +01003682 addr = CORE_CC_REG(pmu->base, chipcontrol_addr);
Arend van Spriel65d80d02014-01-29 15:32:14 +01003683 brcmf_sdiod_regwl(sdiodev, addr, 1, NULL);
3684 cc_data_temp = brcmf_sdiod_regrl(sdiodev, addr, NULL);
3685 cc_data_temp &= ~str_mask;
3686 drivestrength_sel <<= str_shift;
3687 cc_data_temp |= drivestrength_sel;
3688 brcmf_sdiod_regwl(sdiodev, addr, cc_data_temp, NULL);
3689
3690 brcmf_dbg(INFO, "SDIO: %d mA (req=%d mA) drive strength selected, set to 0x%08x\n",
3691 str_tab[i].strength, drivestrength, cc_data_temp);
3692 }
3693}
3694
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003695static int brcmf_sdio_buscoreprep(void *ctx)
Arend van Spriel65d80d02014-01-29 15:32:14 +01003696{
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003697 struct brcmf_sdio_dev *sdiodev = ctx;
Arend van Spriel65d80d02014-01-29 15:32:14 +01003698 int err = 0;
3699 u8 clkval, clkset;
3700
3701 /* Try forcing SDIO core to do ALPAvail request only */
3702 clkset = SBSDIO_FORCE_HW_CLKREQ_OFF | SBSDIO_ALP_AVAIL_REQ;
3703 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, clkset, &err);
3704 if (err) {
3705 brcmf_err("error writing for HT off\n");
3706 return err;
3707 }
3708
3709 /* If register supported, wait for ALPAvail and then force ALP */
3710 /* This may take up to 15 milliseconds */
3711 clkval = brcmf_sdiod_regrb(sdiodev,
3712 SBSDIO_FUNC1_CHIPCLKCSR, NULL);
3713
3714 if ((clkval & ~SBSDIO_AVBITS) != clkset) {
3715 brcmf_err("ChipClkCSR access: wrote 0x%02x read 0x%02x\n",
3716 clkset, clkval);
3717 return -EACCES;
3718 }
3719
3720 SPINWAIT(((clkval = brcmf_sdiod_regrb(sdiodev,
3721 SBSDIO_FUNC1_CHIPCLKCSR, NULL)),
3722 !SBSDIO_ALPAV(clkval)),
3723 PMU_MAX_TRANSITION_DLY);
3724 if (!SBSDIO_ALPAV(clkval)) {
3725 brcmf_err("timeout on ALPAV wait, clkval 0x%02x\n",
3726 clkval);
3727 return -EBUSY;
3728 }
3729
3730 clkset = SBSDIO_FORCE_HW_CLKREQ_OFF | SBSDIO_FORCE_ALP;
3731 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, clkset, &err);
3732 udelay(65);
3733
3734 /* Also, disable the extra SDIO pull-ups */
3735 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_SDIOPULLUP, 0, NULL);
3736
3737 return 0;
3738}
3739
Arend van Sprield380ebc2015-03-11 16:11:29 +01003740static void brcmf_sdio_buscore_activate(void *ctx, struct brcmf_chip *chip,
3741 u32 rstvec)
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003742{
3743 struct brcmf_sdio_dev *sdiodev = ctx;
3744 struct brcmf_core *core;
3745 u32 reg_addr;
3746
3747 /* clear all interrupts */
3748 core = brcmf_chip_get_core(chip, BCMA_CORE_SDIO_DEV);
3749 reg_addr = core->base + offsetof(struct sdpcmd_regs, intstatus);
3750 brcmf_sdiod_regwl(sdiodev, reg_addr, 0xFFFFFFFF, NULL);
3751
3752 if (rstvec)
3753 /* Write reset vector to address 0 */
3754 brcmf_sdiod_ramrw(sdiodev, true, 0, (void *)&rstvec,
3755 sizeof(rstvec));
3756}
3757
3758static u32 brcmf_sdio_buscore_read32(void *ctx, u32 addr)
3759{
3760 struct brcmf_sdio_dev *sdiodev = ctx;
3761 u32 val, rev;
3762
3763 val = brcmf_sdiod_regrl(sdiodev, addr, NULL);
Arend van Spriel8bd61f82015-01-06 23:02:54 +01003764 if (sdiodev->func[0]->device == SDIO_DEVICE_ID_BROADCOM_4335_4339 &&
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003765 addr == CORE_CC_REG(SI_ENUM_BASE, chipid)) {
3766 rev = (val & CID_REV_MASK) >> CID_REV_SHIFT;
3767 if (rev >= 2) {
3768 val &= ~CID_ID_MASK;
Hante Meuleman5779ae62014-07-12 08:49:34 +02003769 val |= BRCM_CC_4339_CHIP_ID;
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003770 }
3771 }
3772 return val;
3773}
3774
3775static void brcmf_sdio_buscore_write32(void *ctx, u32 addr, u32 val)
3776{
3777 struct brcmf_sdio_dev *sdiodev = ctx;
3778
3779 brcmf_sdiod_regwl(sdiodev, addr, val, NULL);
3780}
3781
3782static const struct brcmf_buscore_ops brcmf_sdio_buscore_ops = {
3783 .prepare = brcmf_sdio_buscoreprep,
Arend van Sprield380ebc2015-03-11 16:11:29 +01003784 .activate = brcmf_sdio_buscore_activate,
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003785 .read32 = brcmf_sdio_buscore_read32,
3786 .write32 = brcmf_sdio_buscore_write32,
3787};
3788
Arend van Spriel5b435de2011-10-05 13:19:03 +02003789static bool
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003790brcmf_sdio_probe_attach(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003791{
Hante Meuleman4d792892016-02-17 11:27:07 +01003792 struct brcmf_sdio_dev *sdiodev;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003793 u8 clkctl = 0;
3794 int err = 0;
3795 int reg_addr;
3796 u32 reg_val;
Hante Meuleman668761a2013-04-12 10:55:55 +02003797 u32 drivestrength;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003798
Hante Meuleman4d792892016-02-17 11:27:07 +01003799 sdiodev = bus->sdiodev;
3800 sdio_claim_host(sdiodev->func[1]);
Franky Lin38b0b0d2012-11-05 16:22:24 -08003801
Joe Perches18aad4f2012-01-15 00:38:42 -08003802 pr_debug("F1 signature read @0x18000000=0x%4x\n",
Hante Meuleman4d792892016-02-17 11:27:07 +01003803 brcmf_sdiod_regrl(sdiodev, SI_ENUM_BASE, NULL));
Arend van Spriel5b435de2011-10-05 13:19:03 +02003804
3805 /*
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003806 * Force PLL off until brcmf_chip_attach()
Arend van Spriel5b435de2011-10-05 13:19:03 +02003807 * programs PLL control regs
3808 */
3809
Hante Meuleman4d792892016-02-17 11:27:07 +01003810 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
Arend van Spriela39be272013-12-12 11:58:58 +01003811 BRCMF_INIT_CLKCTL1, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003812 if (!err)
Hante Meuleman4d792892016-02-17 11:27:07 +01003813 clkctl = brcmf_sdiod_regrb(sdiodev,
Arend van Spriela39be272013-12-12 11:58:58 +01003814 SBSDIO_FUNC1_CHIPCLKCSR, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003815
3816 if (err || ((clkctl & ~SBSDIO_AVBITS) != BRCMF_INIT_CLKCTL1)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003817 brcmf_err("ChipClkCSR access: err %d wrote 0x%02x read 0x%02x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02003818 err, BRCMF_INIT_CLKCTL1, clkctl);
3819 goto fail;
3820 }
3821
Hante Meuleman4d792892016-02-17 11:27:07 +01003822 bus->ci = brcmf_chip_attach(sdiodev, &brcmf_sdio_buscore_ops);
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003823 if (IS_ERR(bus->ci)) {
3824 brcmf_err("brcmf_chip_attach failed!\n");
3825 bus->ci = NULL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003826 goto fail;
3827 }
Hante Meulemanaf5b5e62016-02-17 11:27:08 +01003828 sdiodev->settings = brcmf_get_module_param(sdiodev->dev,
Hante Meuleman4d792892016-02-17 11:27:07 +01003829 BRCMF_BUSTYPE_SDIO,
3830 bus->ci->chip,
3831 bus->ci->chiprev);
Hante Meulemanaf5b5e62016-02-17 11:27:08 +01003832 if (!sdiodev->settings) {
3833 brcmf_err("Failed to get device parameters\n");
3834 goto fail;
3835 }
Hante Meuleman4d792892016-02-17 11:27:07 +01003836 /* platform specific configuration:
3837 * alignments must be at least 4 bytes for ADMA
3838 */
3839 bus->head_align = ALIGNMENT;
3840 bus->sgentry_align = ALIGNMENT;
Hante Meulemanaf5b5e62016-02-17 11:27:08 +01003841 if (sdiodev->settings->bus.sdio.sd_head_align > ALIGNMENT)
3842 bus->head_align = sdiodev->settings->bus.sdio.sd_head_align;
3843 if (sdiodev->settings->bus.sdio.sd_sgentry_align > ALIGNMENT)
3844 bus->sgentry_align =
3845 sdiodev->settings->bus.sdio.sd_sgentry_align;
3846
Hante Meuleman4d792892016-02-17 11:27:07 +01003847 /* allocate scatter-gather table. sg support
3848 * will be disabled upon allocation failure.
3849 */
3850 brcmf_sdiod_sgtable_alloc(sdiodev);
3851
3852#ifdef CONFIG_PM_SLEEP
3853 /* wowl can be supported when KEEP_POWER is true and (WAKE_SDIO_IRQ
3854 * is true or when platform data OOB irq is true).
3855 */
3856 if ((sdio_get_host_pm_caps(sdiodev->func[1]) & MMC_PM_KEEP_POWER) &&
3857 ((sdio_get_host_pm_caps(sdiodev->func[1]) & MMC_PM_WAKE_SDIO_IRQ) ||
Hante Meulemanaf5b5e62016-02-17 11:27:08 +01003858 (sdiodev->settings->bus.sdio.oob_irq_supported)))
Hante Meuleman4d792892016-02-17 11:27:07 +01003859 sdiodev->bus_if->wowl_supported = true;
3860#endif
Arend van Spriel5b435de2011-10-05 13:19:03 +02003861
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003862 if (brcmf_sdio_kso_init(bus)) {
Piotr Haber4a3da992013-04-11 13:28:46 +02003863 brcmf_err("error enabling KSO\n");
3864 goto fail;
3865 }
3866
Hante Meulemanaf5b5e62016-02-17 11:27:08 +01003867 if (sdiodev->settings->bus.sdio.drive_strength)
3868 drivestrength = sdiodev->settings->bus.sdio.drive_strength;
Hante Meuleman668761a2013-04-12 10:55:55 +02003869 else
3870 drivestrength = DEFAULT_SDIO_DRIVE_STRENGTH;
Hante Meuleman4d792892016-02-17 11:27:07 +01003871 brcmf_sdio_drivestrengthinit(sdiodev, bus->ci, drivestrength);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003872
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003873 /* Set card control so an SDIO card reset does a WLAN backplane reset */
Hante Meuleman4d792892016-02-17 11:27:07 +01003874 reg_val = brcmf_sdiod_regrb(sdiodev, SDIO_CCCR_BRCM_CARDCTRL, &err);
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003875 if (err)
3876 goto fail;
3877
3878 reg_val |= SDIO_CCCR_BRCM_CARDCTRL_WLANRESET;
3879
Hante Meuleman4d792892016-02-17 11:27:07 +01003880 brcmf_sdiod_regwb(sdiodev, SDIO_CCCR_BRCM_CARDCTRL, reg_val, &err);
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003881 if (err)
3882 goto fail;
3883
3884 /* set PMUControl so a backplane reset does PMU state reload */
Rafał Miłeckie2b397f2016-01-26 17:57:04 +01003885 reg_addr = CORE_CC_REG(brcmf_chip_get_pmu(bus->ci)->base, pmucontrol);
Hante Meuleman4d792892016-02-17 11:27:07 +01003886 reg_val = brcmf_sdiod_regrl(sdiodev, reg_addr, &err);
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003887 if (err)
3888 goto fail;
3889
3890 reg_val |= (BCMA_CC_PMU_CTL_RES_RELOAD << BCMA_CC_PMU_CTL_RES_SHIFT);
3891
Hante Meuleman4d792892016-02-17 11:27:07 +01003892 brcmf_sdiod_regwl(sdiodev, reg_addr, reg_val, &err);
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003893 if (err)
3894 goto fail;
3895
Hante Meuleman4d792892016-02-17 11:27:07 +01003896 sdio_release_host(sdiodev->func[1]);
Franky Lin38b0b0d2012-11-05 16:22:24 -08003897
Arend van Spriel5b435de2011-10-05 13:19:03 +02003898 brcmu_pktq_init(&bus->txq, (PRIOMASK + 1), TXQLEN);
3899
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01003900 /* allocate header buffer */
3901 bus->hdrbuf = kzalloc(MAX_HDR_READ + bus->head_align, GFP_KERNEL);
3902 if (!bus->hdrbuf)
3903 return false;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003904 /* Locate an appropriately-aligned portion of hdrbuf */
3905 bus->rxhdr = (u8 *) roundup((unsigned long)&bus->hdrbuf[0],
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01003906 bus->head_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003907
3908 /* Set the poll and/or interrupt flags */
3909 bus->intr = true;
3910 bus->poll = false;
3911 if (bus->poll)
3912 bus->pollrate = 1;
3913
3914 return true;
3915
3916fail:
Hante Meuleman4d792892016-02-17 11:27:07 +01003917 sdio_release_host(sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003918 return false;
3919}
3920
Arend van Spriel5b435de2011-10-05 13:19:03 +02003921static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003922brcmf_sdio_watchdog_thread(void *data)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003923{
Franky Line92eedf2011-11-22 17:21:50 -08003924 struct brcmf_sdio *bus = (struct brcmf_sdio *)data;
Arend van Spriel99824642015-02-06 18:36:45 +01003925 int wait;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003926
3927 allow_signal(SIGTERM);
3928 /* Run until signal received */
Arend van Spriel99824642015-02-06 18:36:45 +01003929 brcmf_sdiod_freezer_count(bus->sdiodev);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003930 while (1) {
3931 if (kthread_should_stop())
3932 break;
Arend van Spriel99824642015-02-06 18:36:45 +01003933 brcmf_sdiod_freezer_uncount(bus->sdiodev);
3934 wait = wait_for_completion_interruptible(&bus->watchdog_wait);
3935 brcmf_sdiod_freezer_count(bus->sdiodev);
3936 brcmf_sdiod_try_freeze(bus->sdiodev);
3937 if (!wait) {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003938 brcmf_sdio_bus_watchdog(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003939 /* Count the tick for reference */
Arend van Spriel80969832012-06-09 22:51:44 +02003940 bus->sdcnt.tickcnt++;
Arend van Spriel58e9df42014-03-20 10:18:01 +01003941 reinit_completion(&bus->watchdog_wait);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003942 } else
3943 break;
3944 }
3945 return 0;
3946}
3947
3948static void
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003949brcmf_sdio_watchdog(unsigned long data)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003950{
Franky Line92eedf2011-11-22 17:21:50 -08003951 struct brcmf_sdio *bus = (struct brcmf_sdio *)data;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003952
3953 if (bus->watchdog_tsk) {
3954 complete(&bus->watchdog_wait);
3955 /* Reschedule the watchdog */
Arend van Spriel4011fc42016-01-05 11:05:47 +01003956 if (bus->wd_active)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003957 mod_timer(&bus->timer,
Arend van Spriel63ce3d52016-01-05 11:05:48 +01003958 jiffies + BRCMF_WD_POLL);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003959 }
3960}
3961
Julia Lawall6866a642015-11-14 17:22:07 +01003962static const struct brcmf_bus_ops brcmf_sdio_bus_ops = {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003963 .stop = brcmf_sdio_bus_stop,
3964 .preinit = brcmf_sdio_bus_preinit,
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003965 .txdata = brcmf_sdio_bus_txdata,
3966 .txctl = brcmf_sdio_bus_txctl,
3967 .rxctl = brcmf_sdio_bus_rxctl,
3968 .gettxq = brcmf_sdio_bus_gettxq,
Arend van Sprielff4445a2015-10-08 20:33:11 +02003969 .wowl_config = brcmf_sdio_wowl_config,
3970 .get_ramsize = brcmf_sdio_bus_get_ramsize,
3971 .get_memdump = brcmf_sdio_bus_get_memdump,
Arend van Sprield9cb2592012-12-05 15:25:54 +01003972};
3973
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003974static void brcmf_sdio_firmware_callback(struct device *dev,
3975 const struct firmware *code,
3976 void *nvram, u32 nvram_len)
3977{
3978 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3979 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3980 struct brcmf_sdio *bus = sdiodev->bus;
3981 int err = 0;
3982 u8 saveclk;
3983
3984 brcmf_dbg(TRACE, "Enter: dev=%s\n", dev_name(dev));
3985
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003986 if (!bus_if->drvr)
3987 return;
3988
Hante Meulemana1cee862015-01-25 20:31:35 +01003989 /* try to download image and nvram to the dongle */
3990 bus->alp_only = true;
3991 err = brcmf_sdio_download_firmware(bus, code, nvram, nvram_len);
3992 if (err)
3993 goto fail;
3994 bus->alp_only = false;
3995
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003996 /* Start the watchdog timer */
3997 bus->sdcnt.tickcnt = 0;
Arend van Spriel4011fc42016-01-05 11:05:47 +01003998 brcmf_sdio_wd_timer(bus, true);
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003999
4000 sdio_claim_host(sdiodev->func[1]);
4001
4002 /* Make sure backplane clock is on, needed to generate F2 interrupt */
4003 brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
4004 if (bus->clkstate != CLK_AVAIL)
4005 goto release;
4006
4007 /* Force clocks on backplane to be sure F2 interrupt propagates */
4008 saveclk = brcmf_sdiod_regrb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, &err);
4009 if (!err) {
4010 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
4011 (saveclk | SBSDIO_FORCE_HT), &err);
4012 }
4013 if (err) {
4014 brcmf_err("Failed to force clock for F2: err %d\n", err);
4015 goto release;
4016 }
4017
4018 /* Enable function 2 (frame transfers) */
4019 w_sdreg32(bus, SDPCM_PROT_VERSION << SMB_DATA_VERSION_SHIFT,
4020 offsetof(struct sdpcmd_regs, tosbmailboxdata));
4021 err = sdio_enable_func(sdiodev->func[SDIO_FUNC_2]);
4022
4023
4024 brcmf_dbg(INFO, "enable F2: err=%d\n", err);
4025
4026 /* If F2 successfully enabled, set core and enable interrupts */
4027 if (!err) {
4028 /* Set up the interrupt mask and enable interrupts */
4029 bus->hostintmask = HOSTINTMASK;
4030 w_sdreg32(bus, bus->hostintmask,
4031 offsetof(struct sdpcmd_regs, hostintmask));
4032
4033 brcmf_sdiod_regwb(sdiodev, SBSDIO_WATERMARK, 8, &err);
4034 } else {
4035 /* Disable F2 again */
4036 sdio_disable_func(sdiodev->func[SDIO_FUNC_2]);
4037 goto release;
4038 }
4039
4040 if (brcmf_chip_sr_capable(bus->ci)) {
4041 brcmf_sdio_sr_init(bus);
4042 } else {
4043 /* Restore previous clock setting */
4044 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
4045 saveclk, &err);
4046 }
4047
4048 if (err == 0) {
4049 err = brcmf_sdiod_intr_register(sdiodev);
4050 if (err != 0)
4051 brcmf_err("intr register failed:%d\n", err);
4052 }
4053
4054 /* If we didn't come up, turn off backplane clock */
4055 if (err != 0)
4056 brcmf_sdio_clkctl(bus, CLK_NONE, false);
4057
4058 sdio_release_host(sdiodev->func[1]);
4059
4060 err = brcmf_bus_start(dev);
4061 if (err != 0) {
4062 brcmf_err("dongle is not responding\n");
4063 goto fail;
4064 }
4065 return;
4066
4067release:
4068 sdio_release_host(sdiodev->func[1]);
4069fail:
4070 brcmf_dbg(TRACE, "failed: dev=%s, err=%d\n", dev_name(dev), err);
4071 device_release_driver(dev);
4072}
4073
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01004074struct brcmf_sdio *brcmf_sdio_probe(struct brcmf_sdio_dev *sdiodev)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004075{
4076 int ret;
Franky Line92eedf2011-11-22 17:21:50 -08004077 struct brcmf_sdio *bus;
Arend van Spriel99824642015-02-06 18:36:45 +01004078 struct workqueue_struct *wq;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004079
Arend van Spriel5b435de2011-10-05 13:19:03 +02004080 brcmf_dbg(TRACE, "Enter\n");
4081
Arend van Spriel5b435de2011-10-05 13:19:03 +02004082 /* Allocate private bus interface state */
Franky Line92eedf2011-11-22 17:21:50 -08004083 bus = kzalloc(sizeof(struct brcmf_sdio), GFP_ATOMIC);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004084 if (!bus)
4085 goto fail;
4086
4087 bus->sdiodev = sdiodev;
4088 sdiodev->bus = bus;
Arend van Sprielb83db862011-10-19 12:51:09 +02004089 skb_queue_head_init(&bus->glom);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004090 bus->txbound = BRCMF_TXBOUND;
4091 bus->rxbound = BRCMF_RXBOUND;
4092 bus->txminmax = BRCMF_TXMINMAX;
Franky Lin6bc52312013-08-10 12:27:28 +02004093 bus->tx_seq = SDPCM_SEQ_WRAP - 1;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004094
Arend van Spriel99824642015-02-06 18:36:45 +01004095 /* single-threaded workqueue */
4096 wq = alloc_ordered_workqueue("brcmf_wq/%s", WQ_MEM_RECLAIM,
4097 dev_name(&sdiodev->func[1]->dev));
4098 if (!wq) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01004099 brcmf_err("insufficient memory to create txworkqueue\n");
Hante Meuleman37ac5782012-11-14 18:46:18 -08004100 goto fail;
4101 }
Arend van Spriel99824642015-02-06 18:36:45 +01004102 brcmf_sdiod_freezer_count(sdiodev);
4103 INIT_WORK(&bus->datawork, brcmf_sdio_dataworker);
4104 bus->brcmf_wq = wq;
Hante Meuleman37ac5782012-11-14 18:46:18 -08004105
Arend van Spriel5b435de2011-10-05 13:19:03 +02004106 /* attempt to attach to the dongle */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01004107 if (!(brcmf_sdio_probe_attach(bus))) {
4108 brcmf_err("brcmf_sdio_probe_attach failed\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02004109 goto fail;
4110 }
4111
Franky Lindd43a012012-11-05 16:22:22 -08004112 spin_lock_init(&bus->rxctl_lock);
Hante Meulemanfed7ec42014-03-15 17:18:18 +01004113 spin_lock_init(&bus->txq_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004114 init_waitqueue_head(&bus->ctrl_wait);
4115 init_waitqueue_head(&bus->dcmd_resp_wait);
4116
4117 /* Set up the watchdog timer */
4118 init_timer(&bus->timer);
4119 bus->timer.data = (unsigned long)bus;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01004120 bus->timer.function = brcmf_sdio_watchdog;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004121
Arend van Spriel5b435de2011-10-05 13:19:03 +02004122 /* Initialize watchdog thread */
4123 init_completion(&bus->watchdog_wait);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01004124 bus->watchdog_tsk = kthread_run(brcmf_sdio_watchdog_thread,
Arend van Spriel99824642015-02-06 18:36:45 +01004125 bus, "brcmf_wdog/%s",
4126 dev_name(&sdiodev->func[1]->dev));
Arend van Spriel5b435de2011-10-05 13:19:03 +02004127 if (IS_ERR(bus->watchdog_tsk)) {
Joe Perches02f77192012-01-15 00:38:44 -08004128 pr_warn("brcmf_watchdog thread failed to start\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02004129 bus->watchdog_tsk = NULL;
4130 }
4131 /* Initialize DPC thread */
Hante Meuleman2c64e162015-03-18 13:25:22 +01004132 bus->dpc_triggered = false;
4133 bus->dpc_running = false;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004134
Franky Lina9ffda82011-12-16 18:37:12 -08004135 /* Assign bus interface call back */
Arend van Sprield9cb2592012-12-05 15:25:54 +01004136 bus->sdiodev->bus_if->dev = bus->sdiodev->dev;
4137 bus->sdiodev->bus_if->ops = &brcmf_sdio_bus_ops;
Arend van Spriel75d907d2013-02-06 18:40:45 +01004138 bus->sdiodev->bus_if->chip = bus->ci->chip;
4139 bus->sdiodev->bus_if->chiprev = bus->ci->chiprev;
Arend van Sprield9cb2592012-12-05 15:25:54 +01004140
Franky Lin706478c2013-08-10 12:27:29 +02004141 /* default sdio bus header length for tx packet */
4142 bus->tx_hdrlen = SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN;
4143
4144 /* Attach to the common layer, reserve hdr space */
Hante Meulemanaf5b5e62016-02-17 11:27:08 +01004145 ret = brcmf_attach(bus->sdiodev->dev, bus->sdiodev->settings);
Franky Lin712ac5b2011-12-16 18:37:09 -08004146 if (ret != 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01004147 brcmf_err("brcmf_attach failed\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02004148 goto fail;
4149 }
4150
Hante Meulemane0045bf2016-01-19 12:39:24 +01004151 /* allocate scatter-gather table. sg support
4152 * will be disabled upon allocation failure.
4153 */
4154 brcmf_sdiod_sgtable_alloc(bus->sdiodev);
4155
Daniel Kim7dd3abc2014-05-27 12:56:26 +02004156 /* Query the F2 block size, set roundup accordingly */
4157 bus->blocksize = bus->sdiodev->func[2]->cur_blksize;
4158 bus->roundup = min(max_roundup, bus->blocksize);
4159
Arend van Spriel5b435de2011-10-05 13:19:03 +02004160 /* Allocate buffers */
Arend van Sprielfad13222014-01-06 12:40:40 +01004161 if (bus->sdiodev->bus_if->maxctl) {
Daniel Kim7dd3abc2014-05-27 12:56:26 +02004162 bus->sdiodev->bus_if->maxctl += bus->roundup;
Arend van Sprielfad13222014-01-06 12:40:40 +01004163 bus->rxblen =
4164 roundup((bus->sdiodev->bus_if->maxctl + SDPCM_HDRLEN),
4165 ALIGNMENT) + bus->head_align;
4166 bus->rxbuf = kmalloc(bus->rxblen, GFP_ATOMIC);
4167 if (!(bus->rxbuf)) {
4168 brcmf_err("rxbuf allocation failed\n");
4169 goto fail;
4170 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02004171 }
4172
Arend van Sprielfad13222014-01-06 12:40:40 +01004173 sdio_claim_host(bus->sdiodev->func[1]);
4174
4175 /* Disable F2 to clear any intermediate frame state on the dongle */
4176 sdio_disable_func(bus->sdiodev->func[SDIO_FUNC_2]);
4177
Arend van Sprielfad13222014-01-06 12:40:40 +01004178 bus->rxflow = false;
4179
4180 /* Done with backplane-dependent accesses, can drop clock... */
4181 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, 0, NULL);
4182
4183 sdio_release_host(bus->sdiodev->func[1]);
4184
4185 /* ...and initialize clock/power states */
4186 bus->clkstate = CLK_SDONLY;
4187 bus->idletime = BRCMF_IDLE_INTERVAL;
4188 bus->idleclock = BRCMF_IDLE_ACTIVE;
4189
Arend van Sprielfad13222014-01-06 12:40:40 +01004190 /* SR state */
Arend van Sprielfad13222014-01-06 12:40:40 +01004191 bus->sr_enabled = false;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004192
Arend van Spriel80969832012-06-09 22:51:44 +02004193 brcmf_sdio_debugfs_create(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004194 brcmf_dbg(INFO, "completed!!\n");
4195
Hante Meuleman46d703a2015-11-25 11:32:46 +01004196 ret = brcmf_fw_map_chip_to_name(bus->ci->chip, bus->ci->chiprev,
4197 brcmf_sdio_fwnames,
4198 ARRAY_SIZE(brcmf_sdio_fwnames),
4199 sdiodev->fw_name, sdiodev->nvram_name);
Daniel Kimc1b20532014-07-12 08:49:37 +02004200 if (ret)
4201 goto fail;
4202
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02004203 ret = brcmf_fw_get_firmwares(sdiodev->dev, BRCMF_FW_REQUEST_NVRAM,
Daniel Kimc1b20532014-07-12 08:49:37 +02004204 sdiodev->fw_name, sdiodev->nvram_name,
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02004205 brcmf_sdio_firmware_callback);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004206 if (ret != 0) {
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02004207 brcmf_err("async firmware request failed: %d\n", ret);
Hante Meuleman1799ddf2012-11-14 18:46:17 -08004208 goto fail;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004209 }
Franky Lin15d45b62011-10-21 16:16:32 +02004210
Arend van Spriel5b435de2011-10-05 13:19:03 +02004211 return bus;
4212
4213fail:
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004214 brcmf_sdio_remove(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004215 return NULL;
4216}
4217
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004218/* Detach and free everything */
4219void brcmf_sdio_remove(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004220{
Arend van Spriel5b435de2011-10-05 13:19:03 +02004221 brcmf_dbg(TRACE, "Enter\n");
4222
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004223 if (bus) {
4224 /* De-register interrupt handler */
4225 brcmf_sdiod_intr_unregister(bus->sdiodev);
4226
Arend van Spriel4faf28b2014-05-27 12:56:18 +02004227 brcmf_detach(bus->sdiodev->dev);
Arend van Sprielbfad4a02014-01-06 12:40:39 +01004228
Hante Meulemane0c180ec2014-01-29 15:32:18 +01004229 cancel_work_sync(&bus->datawork);
4230 if (bus->brcmf_wq)
4231 destroy_workqueue(bus->brcmf_wq);
4232
Arend van Sprielbfad4a02014-01-06 12:40:39 +01004233 if (bus->ci) {
Arend van Spriela1ce7a02015-02-06 18:36:42 +01004234 if (bus->sdiodev->state != BRCMF_SDIOD_NOMEDIUM) {
Arend van Sprielbb350712014-01-13 22:20:29 +01004235 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel4011fc42016-01-05 11:05:47 +01004236 brcmf_sdio_wd_timer(bus, false);
Arend van Sprielbb350712014-01-13 22:20:29 +01004237 brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
4238 /* Leave the device in state where it is
Arend van Sprield380ebc2015-03-11 16:11:29 +01004239 * 'passive'. This is done by resetting all
4240 * necessary cores.
Arend van Sprielbb350712014-01-13 22:20:29 +01004241 */
4242 msleep(20);
Arend van Sprield380ebc2015-03-11 16:11:29 +01004243 brcmf_chip_set_passive(bus->ci);
Arend van Sprielbb350712014-01-13 22:20:29 +01004244 brcmf_sdio_clkctl(bus, CLK_NONE, false);
4245 sdio_release_host(bus->sdiodev->func[1]);
4246 }
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01004247 brcmf_chip_detach(bus->ci);
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004248 }
Hante Meulemanaf5b5e62016-02-17 11:27:08 +01004249 if (bus->sdiodev->settings)
4250 brcmf_release_module_param(bus->sdiodev->settings);
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004251
Arend van Sprielbfad4a02014-01-06 12:40:39 +01004252 kfree(bus->rxbuf);
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004253 kfree(bus->hdrbuf);
4254 kfree(bus);
4255 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02004256
4257 brcmf_dbg(TRACE, "Disconnected\n");
4258}
4259
Arend van Spriel4011fc42016-01-05 11:05:47 +01004260void brcmf_sdio_wd_timer(struct brcmf_sdio *bus, bool active)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004261{
Arend van Spriel5b435de2011-10-05 13:19:03 +02004262 /* Totally stop the timer */
Arend van Spriel4011fc42016-01-05 11:05:47 +01004263 if (!active && bus->wd_active) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02004264 del_timer_sync(&bus->timer);
Arend van Spriel4011fc42016-01-05 11:05:47 +01004265 bus->wd_active = false;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004266 return;
4267 }
4268
Franky Linece960e2011-10-21 16:16:19 +02004269 /* don't start the wd until fw is loaded */
Arend van Spriela1ce7a02015-02-06 18:36:42 +01004270 if (bus->sdiodev->state != BRCMF_SDIOD_DATA)
Franky Linece960e2011-10-21 16:16:19 +02004271 return;
4272
Arend van Spriel4011fc42016-01-05 11:05:47 +01004273 if (active) {
4274 if (!bus->wd_active) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02004275 /* Create timer again when watchdog period is
4276 dynamically changed or in the first instance
4277 */
Arend van Spriel63ce3d52016-01-05 11:05:48 +01004278 bus->timer.expires = jiffies + BRCMF_WD_POLL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004279 add_timer(&bus->timer);
Arend van Spriel4011fc42016-01-05 11:05:47 +01004280 bus->wd_active = true;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004281 } else {
4282 /* Re arm the timer, at last watchdog period */
Arend van Spriel63ce3d52016-01-05 11:05:48 +01004283 mod_timer(&bus->timer, jiffies + BRCMF_WD_POLL);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004284 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02004285 }
4286}
Arend van Spriel99824642015-02-06 18:36:45 +01004287
4288int brcmf_sdio_sleep(struct brcmf_sdio *bus, bool sleep)
4289{
4290 int ret;
4291
4292 sdio_claim_host(bus->sdiodev->func[1]);
4293 ret = brcmf_sdio_bus_sleep(bus, sleep, false);
4294 sdio_release_host(bus->sdiodev->func[1]);
4295
4296 return ret;
4297}
4298