blob: d2219885071f6d718f0f26320d580c66adc26e36 [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)
Arend Van Spriel5251b6b2016-09-05 11:42:13 +0100316#define BRCMF_SDIO_MAX_ACCESS_ERRORS 5
Piotr Haber4a3da992013-04-11 13:28:46 +0200317
Arend van Spriel5b435de2011-10-05 13:19:03 +0200318/*
319 * Conversion of 802.1D priority to precedence level
320 */
321static uint prio2prec(u32 prio)
322{
323 return (prio == PRIO_8021D_NONE || prio == PRIO_8021D_BE) ?
324 (prio^2) : prio;
325}
326
Joe Perches8ae74652012-01-15 00:38:38 -0800327#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200328/* Device console log buffer state */
329struct brcmf_console {
330 uint count; /* Poll interval msec counter */
331 uint log_addr; /* Log struct address (fixed) */
332 struct rte_log_le log_le; /* Log struct (host copy) */
333 uint bufsize; /* Size of log buffer */
334 u8 *buf; /* Log buffer (host copy) */
335 uint last; /* Last buffer read index */
336};
Arend van Spriel4fc0d012012-06-14 12:16:57 +0200337
338struct brcmf_trap_info {
339 __le32 type;
340 __le32 epc;
341 __le32 cpsr;
342 __le32 spsr;
343 __le32 r0; /* a1 */
344 __le32 r1; /* a2 */
345 __le32 r2; /* a3 */
346 __le32 r3; /* a4 */
347 __le32 r4; /* v1 */
348 __le32 r5; /* v2 */
349 __le32 r6; /* v3 */
350 __le32 r7; /* v4 */
351 __le32 r8; /* v5 */
352 __le32 r9; /* sb/v6 */
353 __le32 r10; /* sl/v7 */
354 __le32 r11; /* fp/v8 */
355 __le32 r12; /* ip */
356 __le32 r13; /* sp */
357 __le32 r14; /* lr */
358 __le32 pc; /* r15 */
359};
Joe Perches8ae74652012-01-15 00:38:38 -0800360#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200361
362struct sdpcm_shared {
363 u32 flags;
364 u32 trap_addr;
365 u32 assert_exp_addr;
366 u32 assert_file_addr;
367 u32 assert_line;
368 u32 console_addr; /* Address of struct rte_console */
369 u32 msgtrace_addr;
370 u8 tag[32];
Arend van Spriel4fc0d012012-06-14 12:16:57 +0200371 u32 brpt_addr;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200372};
373
374struct sdpcm_shared_le {
375 __le32 flags;
376 __le32 trap_addr;
377 __le32 assert_exp_addr;
378 __le32 assert_file_addr;
379 __le32 assert_line;
380 __le32 console_addr; /* Address of struct rte_console */
381 __le32 msgtrace_addr;
382 u8 tag[32];
Arend van Spriel4fc0d012012-06-14 12:16:57 +0200383 __le32 brpt_addr;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200384};
385
Franky Lin6bc52312013-08-10 12:27:28 +0200386/* dongle SDIO bus specific header info */
387struct brcmf_sdio_hdrinfo {
Franky Lin4754fce2012-09-19 22:21:11 +0200388 u8 seq_num;
389 u8 channel;
390 u16 len;
391 u16 len_left;
392 u16 len_nxtfrm;
393 u8 dat_offset;
Franky Lin8da9d2c2013-11-29 12:25:10 +0100394 bool lastfrm;
395 u16 tail_pad;
Franky Lin4754fce2012-09-19 22:21:11 +0200396};
Arend van Spriel5b435de2011-10-05 13:19:03 +0200397
Arend van Spriel82d957e2014-07-12 08:49:36 +0200398/*
399 * hold counter variables
400 */
401struct brcmf_sdio_count {
402 uint intrcount; /* Count of device interrupt callbacks */
403 uint lastintrs; /* Count as of last watchdog timer */
404 uint pollcnt; /* Count of active polls */
405 uint regfails; /* Count of R_REG failures */
406 uint tx_sderrs; /* Count of tx attempts with sd errors */
407 uint fcqueued; /* Tx packets that got queued */
408 uint rxrtx; /* Count of rtx requests (NAK to dongle) */
409 uint rx_toolong; /* Receive frames too long to receive */
410 uint rxc_errors; /* SDIO errors when reading control frames */
411 uint rx_hdrfail; /* SDIO errors on header reads */
412 uint rx_badhdr; /* Bad received headers (roosync?) */
413 uint rx_badseq; /* Mismatched rx sequence number */
414 uint fc_rcvd; /* Number of flow-control events received */
415 uint fc_xoff; /* Number which turned on flow-control */
416 uint fc_xon; /* Number which turned off flow-control */
417 uint rxglomfail; /* Failed deglom attempts */
418 uint rxglomframes; /* Number of glom frames (superframes) */
419 uint rxglompkts; /* Number of packets from glom frames */
420 uint f2rxhdrs; /* Number of header reads */
421 uint f2rxdata; /* Number of frame data reads */
422 uint f2txdata; /* Number of f2 frame writes */
423 uint f1regdata; /* Number of f1 register accesses */
424 uint tickcnt; /* Number of watchdog been schedule */
425 ulong tx_ctlerrs; /* Err of sending ctrl frames */
426 ulong tx_ctlpkts; /* Ctrl frames sent to dongle */
427 ulong rx_ctlerrs; /* Err of processing rx ctrl frames */
428 ulong rx_ctlpkts; /* Ctrl frames processed from dongle */
429 ulong rx_readahead_cnt; /* packets where header read-ahead was used */
430};
431
Arend van Spriel5b435de2011-10-05 13:19:03 +0200432/* misc chip info needed by some of the routines */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200433/* Private data for SDIO bus interaction */
Franky Line92eedf2011-11-22 17:21:50 -0800434struct brcmf_sdio {
Arend van Spriel5b435de2011-10-05 13:19:03 +0200435 struct brcmf_sdio_dev *sdiodev; /* sdio device handler */
Arend van Spriel9cf218f2014-01-13 22:20:27 +0100436 struct brcmf_chip *ci; /* Chip info struct */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200437
Arend van Spriel5b435de2011-10-05 13:19:03 +0200438 u32 hostintmask; /* Copy of Host Interrupt Mask */
Franky Lin45316032012-09-13 21:12:03 +0200439 atomic_t intstatus; /* Intstatus bits (events) pending */
440 atomic_t fcstate; /* State of dongle flow-control */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200441
442 uint blocksize; /* Block size of SDIO transfers */
443 uint roundup; /* Max roundup limit */
444
445 struct pktq txq; /* Queue length used for flow-control */
446 u8 flowcontrol; /* per prio flow control bitmask */
447 u8 tx_seq; /* Transmit sequence number (next) */
448 u8 tx_max; /* Maximum transmit sequence allowed */
449
Arend van Spriel9b2d2f22013-11-29 12:25:14 +0100450 u8 *hdrbuf; /* buffer for handling rx frame */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200451 u8 *rxhdr; /* Header of current rx frame (in hdrbuf) */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200452 u8 rx_seq; /* Receive sequence number (expected) */
Franky Lin6bc52312013-08-10 12:27:28 +0200453 struct brcmf_sdio_hdrinfo cur_read;
Franky Lin4754fce2012-09-19 22:21:11 +0200454 /* info of current read frame */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200455 bool rxskip; /* Skip receive (awaiting NAK ACK) */
Franky Lin4754fce2012-09-19 22:21:11 +0200456 bool rxpending; /* Data frame pending in dongle */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200457
458 uint rxbound; /* Rx frames to read before resched */
459 uint txbound; /* Tx frames to send before resched */
460 uint txminmax;
461
462 struct sk_buff *glomd; /* Packet containing glomming descriptor */
Arend van Sprielb83db862011-10-19 12:51:09 +0200463 struct sk_buff_head glom; /* Packet list for glommed superframe */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200464
465 u8 *rxbuf; /* Buffer for receiving control packets */
466 uint rxblen; /* Allocated length of rxbuf */
467 u8 *rxctl; /* Aligned pointer into rxbuf */
Franky Lindd43a012012-11-05 16:22:22 -0800468 u8 *rxctl_orig; /* pointer for freeing rxctl */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200469 uint rxlen; /* Length of valid data in buffer */
Franky Lindd43a012012-11-05 16:22:22 -0800470 spinlock_t rxctl_lock; /* protection lock for ctrl frame resources */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200471
472 u8 sdpcm_ver; /* Bus protocol reported by dongle */
473
474 bool intr; /* Use interrupts */
475 bool poll; /* Use polling */
Franky Lin1d382272012-09-13 21:11:59 +0200476 atomic_t ipend; /* Device interrupt is pending */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200477 uint spurious; /* Count of spurious interrupts */
478 uint pollrate; /* Ticks between device polls */
479 uint polltick; /* Tick counter */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200480
Joe Perches8ae74652012-01-15 00:38:38 -0800481#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200482 uint console_interval;
483 struct brcmf_console console; /* Console output polling support */
484 uint console_addr; /* Console address from shared struct */
Joe Perches8ae74652012-01-15 00:38:38 -0800485#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200486
Arend van Spriel5b435de2011-10-05 13:19:03 +0200487 uint clkstate; /* State of sd and backplane clock(s) */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200488 s32 idletime; /* Control for activity timeout */
Hante Meulemanb441ba82015-03-06 18:40:39 +0100489 s32 idlecount; /* Activity timeout counter */
490 s32 idleclock; /* How to set bus driver when idle */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200491 bool rxflow_mode; /* Rx flow control mode */
492 bool rxflow; /* Is rx flow control on */
493 bool alp_only; /* Don't use HT clock (ALP only) */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200494
Arend van Spriel5b435de2011-10-05 13:19:03 +0200495 u8 *ctrl_frame_buf;
Hante Meulemanfed7ec42014-03-15 17:18:18 +0100496 u16 ctrl_frame_len;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200497 bool ctrl_frame_stat;
Hante Meuleman4dd8b262015-01-25 20:31:32 +0100498 int ctrl_frame_err;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200499
Hante Meulemanfed7ec42014-03-15 17:18:18 +0100500 spinlock_t txq_lock; /* protect bus->txq */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200501 wait_queue_head_t ctrl_wait;
502 wait_queue_head_t dcmd_resp_wait;
503
504 struct timer_list timer;
505 struct completion watchdog_wait;
506 struct task_struct *watchdog_tsk;
Arend van Spriel4011fc42016-01-05 11:05:47 +0100507 bool wd_active;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200508
Franky Linf1e68c22012-09-13 21:12:00 +0200509 struct workqueue_struct *brcmf_wq;
510 struct work_struct datawork;
Hante Meuleman2c64e162015-03-18 13:25:22 +0100511 bool dpc_triggered;
512 bool dpc_running;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200513
Franky Linc8bf3482011-12-16 18:37:08 -0800514 bool txoff; /* Transmit flow-controlled */
Arend van Spriel80969832012-06-09 22:51:44 +0200515 struct brcmf_sdio_count sdcnt;
Piotr Haber4a3da992013-04-11 13:28:46 +0200516 bool sr_enabled; /* SaveRestore enabled */
Arend van Spriel99824642015-02-06 18:36:45 +0100517 bool sleeping;
Franky Lin706478c2013-08-10 12:27:29 +0200518
519 u8 tx_hdrlen; /* sdio bus header length for tx packet */
Franky Lin8da9d2c2013-11-29 12:25:10 +0100520 bool txglom; /* host tx glomming enable flag */
Arend van Spriele217d1c2013-11-29 12:25:13 +0100521 u16 head_align; /* buffer pointer alignment */
522 u16 sgentry_align; /* scatter-gather buffer alignment */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200523};
524
Arend van Spriel5b435de2011-10-05 13:19:03 +0200525/* clkstate */
526#define CLK_NONE 0
527#define CLK_SDONLY 1
Piotr Haber4a3da992013-04-11 13:28:46 +0200528#define CLK_PENDING 2
Arend van Spriel5b435de2011-10-05 13:19:03 +0200529#define CLK_AVAIL 3
530
Joe Perches8ae74652012-01-15 00:38:38 -0800531#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200532static int qcount[NUMPRIO];
Joe Perches8ae74652012-01-15 00:38:38 -0800533#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200534
Hante Meuleman668761a2013-04-12 10:55:55 +0200535#define DEFAULT_SDIO_DRIVE_STRENGTH 6 /* in milliamps */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200536
537#define RETRYCHAN(chan) ((chan) == SDPCM_EVENT_CHANNEL)
538
Arend van Spriel5b435de2011-10-05 13:19:03 +0200539/* Limit on rounding up frames */
540static const uint max_roundup = 512;
541
542#define ALIGNMENT 4
543
Franky Lin9d7d6f92012-10-22 10:36:24 -0700544enum brcmf_sdio_frmtype {
545 BRCMF_SDIO_FT_NORMAL,
546 BRCMF_SDIO_FT_SUPER,
547 BRCMF_SDIO_FT_SUB,
548};
549
Arend van Spriel65d80d02014-01-29 15:32:14 +0100550#define SDIOD_DRVSTR_KEY(chip, pmu) (((chip) << 16) | (pmu))
551
552/* SDIO Pad drive strength to select value mappings */
553struct sdiod_drive_str {
554 u8 strength; /* Pad Drive Strength in mA */
555 u8 sel; /* Chip-specific select value */
556};
557
558/* SDIO Drive Strength to sel value table for PMU Rev 11 (1.8V) */
559static const struct sdiod_drive_str sdiod_drvstr_tab1_1v8[] = {
560 {32, 0x6},
561 {26, 0x7},
562 {22, 0x4},
563 {16, 0x5},
564 {12, 0x2},
565 {8, 0x3},
566 {4, 0x0},
567 {0, 0x1}
568};
569
570/* SDIO Drive Strength to sel value table for PMU Rev 13 (1.8v) */
571static const struct sdiod_drive_str sdiod_drive_strength_tab5_1v8[] = {
572 {6, 0x7},
573 {5, 0x6},
574 {4, 0x5},
575 {3, 0x4},
576 {2, 0x2},
577 {1, 0x1},
578 {0, 0x0}
579};
580
581/* SDIO Drive Strength to sel value table for PMU Rev 17 (1.8v) */
582static const struct sdiod_drive_str sdiod_drvstr_tab6_1v8[] = {
583 {3, 0x3},
584 {2, 0x2},
585 {1, 0x1},
586 {0, 0x0} };
587
588/* SDIO Drive Strength to sel value table for 43143 PMU Rev 17 (3.3V) */
589static const struct sdiod_drive_str sdiod_drvstr_tab2_3v3[] = {
590 {16, 0x7},
591 {12, 0x5},
592 {8, 0x3},
593 {4, 0x1}
594};
595
Hante Meuleman46d703a2015-11-25 11:32:46 +0100596BRCMF_FW_NVRAM_DEF(43143, "brcmfmac43143-sdio.bin", "brcmfmac43143-sdio.txt");
597BRCMF_FW_NVRAM_DEF(43241B0, "brcmfmac43241b0-sdio.bin",
598 "brcmfmac43241b0-sdio.txt");
599BRCMF_FW_NVRAM_DEF(43241B4, "brcmfmac43241b4-sdio.bin",
600 "brcmfmac43241b4-sdio.txt");
601BRCMF_FW_NVRAM_DEF(43241B5, "brcmfmac43241b5-sdio.bin",
602 "brcmfmac43241b5-sdio.txt");
603BRCMF_FW_NVRAM_DEF(4329, "brcmfmac4329-sdio.bin", "brcmfmac4329-sdio.txt");
604BRCMF_FW_NVRAM_DEF(4330, "brcmfmac4330-sdio.bin", "brcmfmac4330-sdio.txt");
605BRCMF_FW_NVRAM_DEF(4334, "brcmfmac4334-sdio.bin", "brcmfmac4334-sdio.txt");
606BRCMF_FW_NVRAM_DEF(43340, "brcmfmac43340-sdio.bin", "brcmfmac43340-sdio.txt");
607BRCMF_FW_NVRAM_DEF(4335, "brcmfmac4335-sdio.bin", "brcmfmac4335-sdio.txt");
608BRCMF_FW_NVRAM_DEF(43362, "brcmfmac43362-sdio.bin", "brcmfmac43362-sdio.txt");
609BRCMF_FW_NVRAM_DEF(4339, "brcmfmac4339-sdio.bin", "brcmfmac4339-sdio.txt");
610BRCMF_FW_NVRAM_DEF(43430, "brcmfmac43430-sdio.bin", "brcmfmac43430-sdio.txt");
611BRCMF_FW_NVRAM_DEF(43455, "brcmfmac43455-sdio.bin", "brcmfmac43455-sdio.txt");
612BRCMF_FW_NVRAM_DEF(4354, "brcmfmac4354-sdio.bin", "brcmfmac4354-sdio.txt");
Christian Daudt496aec52016-05-04 17:55:20 -0700613BRCMF_FW_NVRAM_DEF(4356, "brcmfmac4356-sdio.bin", "brcmfmac4356-sdio.txt");
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200614
Hante Meuleman46d703a2015-11-25 11:32:46 +0100615static struct brcmf_firmware_mapping brcmf_sdio_fwnames[] = {
616 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_43143_CHIP_ID, 0xFFFFFFFF, 43143),
617 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_43241_CHIP_ID, 0x0000001F, 43241B0),
618 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_43241_CHIP_ID, 0x00000020, 43241B4),
619 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_43241_CHIP_ID, 0xFFFFFFC0, 43241B5),
620 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_4329_CHIP_ID, 0xFFFFFFFF, 4329),
621 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_4330_CHIP_ID, 0xFFFFFFFF, 4330),
622 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_4334_CHIP_ID, 0xFFFFFFFF, 4334),
623 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_43340_CHIP_ID, 0xFFFFFFFF, 43340),
Arend Van Sprieldc630dc2016-11-23 10:25:21 +0000624 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_43341_CHIP_ID, 0xFFFFFFFF, 43340),
Hante Meuleman46d703a2015-11-25 11:32:46 +0100625 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_4335_CHIP_ID, 0xFFFFFFFF, 4335),
626 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_43362_CHIP_ID, 0xFFFFFFFE, 43362),
627 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_4339_CHIP_ID, 0xFFFFFFFF, 4339),
628 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_43430_CHIP_ID, 0xFFFFFFFF, 43430),
629 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_4345_CHIP_ID, 0xFFFFFFC0, 43455),
Christian Daudt496aec52016-05-04 17:55:20 -0700630 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_4354_CHIP_ID, 0xFFFFFFFF, 4354),
631 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_4356_CHIP_ID, 0xFFFFFFFF, 4356)
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200632};
633
Arend van Spriel5b435de2011-10-05 13:19:03 +0200634static void pkt_align(struct sk_buff *p, int len, int align)
635{
636 uint datalign;
637 datalign = (unsigned long)(p->data);
638 datalign = roundup(datalign, (align)) - datalign;
639 if (datalign)
640 skb_pull(p, datalign);
641 __skb_trim(p, len);
642}
643
644/* To check if there's window offered */
Franky Line92eedf2011-11-22 17:21:50 -0800645static bool data_ok(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200646{
647 return (u8)(bus->tx_max - bus->tx_seq) != 0 &&
648 ((u8)(bus->tx_max - bus->tx_seq) & 0x80) == 0;
649}
650
651/*
652 * Reads a register in the SDIO hardware block. This block occupies a series of
653 * adresses on the 32 bit backplane bus.
654 */
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100655static int r_sdreg32(struct brcmf_sdio *bus, u32 *regvar, u32 offset)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200656{
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100657 struct brcmf_core *core;
Franky Lin79ae3952012-05-04 18:27:34 -0700658 int ret;
Franky Lin58692752012-05-04 18:27:36 -0700659
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100660 core = brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV);
661 *regvar = brcmf_sdiod_regrl(bus->sdiodev, core->base + offset, &ret);
Franky Lin58692752012-05-04 18:27:36 -0700662
663 return ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200664}
665
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100666static int w_sdreg32(struct brcmf_sdio *bus, u32 regval, u32 reg_offset)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200667{
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100668 struct brcmf_core *core;
Franky Line13ce262012-05-04 18:27:35 -0700669 int ret;
Franky Lin58692752012-05-04 18:27:36 -0700670
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100671 core = brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV);
672 brcmf_sdiod_regwl(bus->sdiodev, core->base + reg_offset, regval, &ret);
Franky Lin58692752012-05-04 18:27:36 -0700673
674 return ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200675}
676
Piotr Haber4a3da992013-04-11 13:28:46 +0200677static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100678brcmf_sdio_kso_control(struct brcmf_sdio *bus, bool on)
Piotr Haber4a3da992013-04-11 13:28:46 +0200679{
680 u8 wr_val = 0, rd_val, cmp_val, bmask;
681 int err = 0;
Arend Van Spriel5251b6b2016-09-05 11:42:13 +0100682 int err_cnt = 0;
Piotr Haber4a3da992013-04-11 13:28:46 +0200683 int try_cnt = 0;
684
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100685 brcmf_dbg(TRACE, "Enter: on=%d\n", on);
Piotr Haber4a3da992013-04-11 13:28:46 +0200686
687 wr_val = (on << SBSDIO_FUNC1_SLEEPCSR_KSO_SHIFT);
688 /* 1st KSO write goes to AOS wake up core if device is asleep */
Arend van Spriela39be272013-12-12 11:58:58 +0100689 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
690 wr_val, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +0200691
692 if (on) {
693 /* device WAKEUP through KSO:
694 * write bit 0 & read back until
695 * both bits 0 (kso bit) & 1 (dev on status) are set
696 */
697 cmp_val = SBSDIO_FUNC1_SLEEPCSR_KSO_MASK |
698 SBSDIO_FUNC1_SLEEPCSR_DEVON_MASK;
699 bmask = cmp_val;
700 usleep_range(2000, 3000);
701 } else {
702 /* Put device to sleep, turn off KSO */
703 cmp_val = 0;
704 /* only check for bit0, bit1(dev on status) may not
705 * get cleared right away
706 */
707 bmask = SBSDIO_FUNC1_SLEEPCSR_KSO_MASK;
708 }
709
710 do {
711 /* reliable KSO bit set/clr:
712 * the sdiod sleep write access is synced to PMU 32khz clk
713 * just one write attempt may fail,
714 * read it back until it matches written value
715 */
Arend van Spriela39be272013-12-12 11:58:58 +0100716 rd_val = brcmf_sdiod_regrb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
717 &err);
Arend Van Spriel5251b6b2016-09-05 11:42:13 +0100718 if (!err) {
719 if ((rd_val & bmask) == cmp_val)
720 break;
721 err_cnt = 0;
722 }
723 /* bail out upon subsequent access errors */
724 if (err && (err_cnt++ > BRCMF_SDIO_MAX_ACCESS_ERRORS))
Piotr Haber4a3da992013-04-11 13:28:46 +0200725 break;
Piotr Haber4a3da992013-04-11 13:28:46 +0200726 udelay(KSO_WAIT_US);
Arend van Spriela39be272013-12-12 11:58:58 +0100727 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
728 wr_val, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +0200729 } while (try_cnt++ < MAX_KSO_ATTEMPTS);
730
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100731 if (try_cnt > 2)
732 brcmf_dbg(SDIO, "try_cnt=%d rd_val=0x%x err=%d\n", try_cnt,
733 rd_val, err);
734
735 if (try_cnt > MAX_KSO_ATTEMPTS)
736 brcmf_err("max tries: rd_val=0x%x err=%d\n", rd_val, err);
737
Piotr Haber4a3da992013-04-11 13:28:46 +0200738 return err;
739}
740
Arend van Spriel5b435de2011-10-05 13:19:03 +0200741#define HOSTINTMASK (I_HMB_SW_MASK | I_CHIPACTIVE)
742
Arend van Spriel5b435de2011-10-05 13:19:03 +0200743/* Turn backplane clock on or off */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100744static int brcmf_sdio_htclk(struct brcmf_sdio *bus, bool on, bool pendok)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200745{
746 int err;
747 u8 clkctl, clkreq, devctl;
748 unsigned long timeout;
749
Arend van Sprielc3203372013-04-03 12:40:44 +0200750 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200751
752 clkctl = 0;
753
Piotr Haber4a3da992013-04-11 13:28:46 +0200754 if (bus->sr_enabled) {
755 bus->clkstate = (on ? CLK_AVAIL : CLK_SDONLY);
756 return 0;
757 }
758
Arend van Spriel5b435de2011-10-05 13:19:03 +0200759 if (on) {
760 /* Request HT Avail */
761 clkreq =
762 bus->alp_only ? SBSDIO_ALP_AVAIL_REQ : SBSDIO_HT_AVAIL_REQ;
763
Arend van Spriela39be272013-12-12 11:58:58 +0100764 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
765 clkreq, &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 request error: %d\n", err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200768 return -EBADE;
769 }
770
Arend van Spriel5b435de2011-10-05 13:19:03 +0200771 /* Check current status */
Arend van Spriela39be272013-12-12 11:58:58 +0100772 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
773 SBSDIO_FUNC1_CHIPCLKCSR, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200774 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100775 brcmf_err("HT Avail read error: %d\n", err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200776 return -EBADE;
777 }
778
779 /* Go to pending and await interrupt if appropriate */
780 if (!SBSDIO_CLKAV(clkctl, bus->alp_only) && pendok) {
781 /* Allow only clock-available interrupt */
Arend van Spriela39be272013-12-12 11:58:58 +0100782 devctl = brcmf_sdiod_regrb(bus->sdiodev,
783 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200784 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100785 brcmf_err("Devctl error setting CA: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200786 err);
787 return -EBADE;
788 }
789
790 devctl |= SBSDIO_DEVCTL_CA_INT_ONLY;
Arend van Spriela39be272013-12-12 11:58:58 +0100791 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
792 devctl, &err);
Arend van Sprielc3203372013-04-03 12:40:44 +0200793 brcmf_dbg(SDIO, "CLKCTL: set PENDING\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200794 bus->clkstate = CLK_PENDING;
795
796 return 0;
797 } else if (bus->clkstate == CLK_PENDING) {
798 /* Cancel CA-only interrupt filter */
Arend van Spriela39be272013-12-12 11:58:58 +0100799 devctl = brcmf_sdiod_regrb(bus->sdiodev,
800 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200801 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
Arend van Spriela39be272013-12-12 11:58:58 +0100802 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
803 devctl, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200804 }
805
806 /* Otherwise, wait here (polling) for HT Avail */
807 timeout = jiffies +
808 msecs_to_jiffies(PMU_MAX_TRANSITION_DLY/1000);
809 while (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
Arend van Spriela39be272013-12-12 11:58:58 +0100810 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
811 SBSDIO_FUNC1_CHIPCLKCSR,
812 &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200813 if (time_after(jiffies, timeout))
814 break;
815 else
816 usleep_range(5000, 10000);
817 }
818 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100819 brcmf_err("HT Avail request error: %d\n", err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200820 return -EBADE;
821 }
822 if (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100823 brcmf_err("HT Avail timeout (%d): clkctl 0x%02x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200824 PMU_MAX_TRANSITION_DLY, clkctl);
825 return -EBADE;
826 }
827
828 /* Mark clock available */
829 bus->clkstate = CLK_AVAIL;
Arend van Sprielc3203372013-04-03 12:40:44 +0200830 brcmf_dbg(SDIO, "CLKCTL: turned ON\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200831
Joe Perches8ae74652012-01-15 00:38:38 -0800832#if defined(DEBUG)
Joe Perches23677ce2012-02-09 11:17:23 +0000833 if (!bus->alp_only) {
Arend van Spriel5b435de2011-10-05 13:19:03 +0200834 if (SBSDIO_ALPONLY(clkctl))
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100835 brcmf_err("HT Clock should be on\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200836 }
Joe Perches8ae74652012-01-15 00:38:38 -0800837#endif /* defined (DEBUG) */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200838
Arend van Spriel5b435de2011-10-05 13:19:03 +0200839 } else {
840 clkreq = 0;
841
842 if (bus->clkstate == CLK_PENDING) {
843 /* Cancel CA-only interrupt filter */
Arend van Spriela39be272013-12-12 11:58:58 +0100844 devctl = brcmf_sdiod_regrb(bus->sdiodev,
845 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200846 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
Arend van Spriela39be272013-12-12 11:58:58 +0100847 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
848 devctl, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200849 }
850
851 bus->clkstate = CLK_SDONLY;
Arend van Spriela39be272013-12-12 11:58:58 +0100852 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
853 clkreq, &err);
Arend van Sprielc3203372013-04-03 12:40:44 +0200854 brcmf_dbg(SDIO, "CLKCTL: turned OFF\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200855 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100856 brcmf_err("Failed access turning clock off: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200857 err);
858 return -EBADE;
859 }
860 }
861 return 0;
862}
863
864/* Change idle/active SD state */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100865static int brcmf_sdio_sdclk(struct brcmf_sdio *bus, bool on)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200866{
Arend van Sprielc3203372013-04-03 12:40:44 +0200867 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200868
869 if (on)
870 bus->clkstate = CLK_SDONLY;
871 else
872 bus->clkstate = CLK_NONE;
873
874 return 0;
875}
876
877/* Transition SD and backplane clock readiness */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100878static int brcmf_sdio_clkctl(struct brcmf_sdio *bus, uint target, bool pendok)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200879{
Joe Perches8ae74652012-01-15 00:38:38 -0800880#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200881 uint oldstate = bus->clkstate;
Joe Perches8ae74652012-01-15 00:38:38 -0800882#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200883
Arend van Sprielc3203372013-04-03 12:40:44 +0200884 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200885
886 /* Early exit if we're already there */
Hante Meulemanb441ba82015-03-06 18:40:39 +0100887 if (bus->clkstate == target)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200888 return 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200889
890 switch (target) {
891 case CLK_AVAIL:
892 /* Make sure SD clock is available */
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 /* Now request HT Avail on the backplane */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100896 brcmf_sdio_htclk(bus, true, pendok);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200897 break;
898
899 case CLK_SDONLY:
900 /* Remove HT request, or bring up SD clock */
901 if (bus->clkstate == CLK_NONE)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100902 brcmf_sdio_sdclk(bus, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200903 else if (bus->clkstate == CLK_AVAIL)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100904 brcmf_sdio_htclk(bus, false, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200905 else
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100906 brcmf_err("request for %d -> %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200907 bus->clkstate, target);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200908 break;
909
910 case CLK_NONE:
911 /* Make sure to remove HT request */
912 if (bus->clkstate == CLK_AVAIL)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100913 brcmf_sdio_htclk(bus, false, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200914 /* Now remove the SD clock */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100915 brcmf_sdio_sdclk(bus, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200916 break;
917 }
Joe Perches8ae74652012-01-15 00:38:38 -0800918#ifdef DEBUG
Arend van Sprielc3203372013-04-03 12:40:44 +0200919 brcmf_dbg(SDIO, "%d -> %d\n", oldstate, bus->clkstate);
Joe Perches8ae74652012-01-15 00:38:38 -0800920#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200921
922 return 0;
923}
924
Piotr Haber4a3da992013-04-11 13:28:46 +0200925static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100926brcmf_sdio_bus_sleep(struct brcmf_sdio *bus, bool sleep, bool pendok)
Piotr Haber4a3da992013-04-11 13:28:46 +0200927{
928 int err = 0;
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100929 u8 clkcsr;
Arend van Spriel82030d62014-01-29 15:32:20 +0100930
931 brcmf_dbg(SDIO, "Enter: request %s currently %s\n",
Piotr Haber4a3da992013-04-11 13:28:46 +0200932 (sleep ? "SLEEP" : "WAKE"),
Arend van Spriel99824642015-02-06 18:36:45 +0100933 (bus->sleeping ? "SLEEP" : "WAKE"));
Piotr Haber4a3da992013-04-11 13:28:46 +0200934
935 /* If SR is enabled control bus state with KSO */
936 if (bus->sr_enabled) {
937 /* Done if we're already in the requested state */
Arend van Spriel99824642015-02-06 18:36:45 +0100938 if (sleep == bus->sleeping)
Piotr Haber4a3da992013-04-11 13:28:46 +0200939 goto end;
940
941 /* Going to sleep */
942 if (sleep) {
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100943 clkcsr = brcmf_sdiod_regrb(bus->sdiodev,
944 SBSDIO_FUNC1_CHIPCLKCSR,
945 &err);
946 if ((clkcsr & SBSDIO_CSR_MASK) == 0) {
947 brcmf_dbg(SDIO, "no clock, set ALP\n");
948 brcmf_sdiod_regwb(bus->sdiodev,
949 SBSDIO_FUNC1_CHIPCLKCSR,
950 SBSDIO_ALP_AVAIL_REQ, &err);
951 }
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100952 err = brcmf_sdio_kso_control(bus, false);
Piotr Haber4a3da992013-04-11 13:28:46 +0200953 } else {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100954 err = brcmf_sdio_kso_control(bus, true);
Piotr Haber4a3da992013-04-11 13:28:46 +0200955 }
Arend van Spriel8982cd42015-01-25 20:31:34 +0100956 if (err) {
Piotr Haber4a3da992013-04-11 13:28:46 +0200957 brcmf_err("error while changing bus sleep state %d\n",
958 err);
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100959 goto done;
Piotr Haber4a3da992013-04-11 13:28:46 +0200960 }
961 }
962
963end:
964 /* control clocks */
965 if (sleep) {
966 if (!bus->sr_enabled)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100967 brcmf_sdio_clkctl(bus, CLK_NONE, pendok);
Piotr Haber4a3da992013-04-11 13:28:46 +0200968 } else {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100969 brcmf_sdio_clkctl(bus, CLK_AVAIL, pendok);
Arend van Spriel4011fc42016-01-05 11:05:47 +0100970 brcmf_sdio_wd_timer(bus, true);
Piotr Haber4a3da992013-04-11 13:28:46 +0200971 }
Arend van Spriel99824642015-02-06 18:36:45 +0100972 bus->sleeping = sleep;
Arend van Spriel8982cd42015-01-25 20:31:34 +0100973 brcmf_dbg(SDIO, "new state %s\n",
974 (sleep ? "SLEEP" : "WAKE"));
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100975done:
976 brcmf_dbg(SDIO, "Exit: err=%d\n", err);
Piotr Haber4a3da992013-04-11 13:28:46 +0200977 return err;
978
979}
980
Daniel Kim0801e6c2014-01-29 15:32:13 +0100981#ifdef DEBUG
982static inline bool brcmf_sdio_valid_shared_address(u32 addr)
983{
984 return !(addr == 0 || ((~addr >> 16) & 0xffff) == (addr & 0xffff));
985}
986
987static int brcmf_sdio_readshared(struct brcmf_sdio *bus,
988 struct sdpcm_shared *sh)
989{
Arend van Spriel9819a902015-03-11 16:11:32 +0100990 u32 addr = 0;
Daniel Kim0801e6c2014-01-29 15:32:13 +0100991 int rv;
992 u32 shaddr = 0;
993 struct sdpcm_shared_le sh_le;
994 __le32 addr_le;
995
Arend van Spriel9819a902015-03-11 16:11:32 +0100996 sdio_claim_host(bus->sdiodev->func[1]);
997 brcmf_sdio_bus_sleep(bus, false, false);
Daniel Kim0801e6c2014-01-29 15:32:13 +0100998
999 /*
1000 * Read last word in socram to determine
1001 * address of sdpcm_shared structure
1002 */
Arend van Spriel9819a902015-03-11 16:11:32 +01001003 shaddr = bus->ci->rambase + bus->ci->ramsize - 4;
1004 if (!bus->ci->rambase && brcmf_chip_sr_capable(bus->ci))
1005 shaddr -= bus->ci->srsize;
1006 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, shaddr,
1007 (u8 *)&addr_le, 4);
Daniel Kim0801e6c2014-01-29 15:32:13 +01001008 if (rv < 0)
Arend van Spriel9819a902015-03-11 16:11:32 +01001009 goto fail;
Daniel Kim0801e6c2014-01-29 15:32:13 +01001010
1011 /*
1012 * Check if addr is valid.
1013 * NVRAM length at the end of memory should have been overwritten.
1014 */
Arend van Spriel9819a902015-03-11 16:11:32 +01001015 addr = le32_to_cpu(addr_le);
Daniel Kim0801e6c2014-01-29 15:32:13 +01001016 if (!brcmf_sdio_valid_shared_address(addr)) {
Arend van Spriel9819a902015-03-11 16:11:32 +01001017 brcmf_err("invalid sdpcm_shared address 0x%08X\n", addr);
1018 rv = -EINVAL;
1019 goto fail;
Daniel Kim0801e6c2014-01-29 15:32:13 +01001020 }
1021
Arend van Spriel9819a902015-03-11 16:11:32 +01001022 brcmf_dbg(INFO, "sdpcm_shared address 0x%08X\n", addr);
1023
Daniel Kim0801e6c2014-01-29 15:32:13 +01001024 /* Read hndrte_shared structure */
1025 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, (u8 *)&sh_le,
1026 sizeof(struct sdpcm_shared_le));
1027 if (rv < 0)
Arend van Spriel9819a902015-03-11 16:11:32 +01001028 goto fail;
1029
1030 sdio_release_host(bus->sdiodev->func[1]);
Daniel Kim0801e6c2014-01-29 15:32:13 +01001031
1032 /* Endianness */
1033 sh->flags = le32_to_cpu(sh_le.flags);
1034 sh->trap_addr = le32_to_cpu(sh_le.trap_addr);
1035 sh->assert_exp_addr = le32_to_cpu(sh_le.assert_exp_addr);
1036 sh->assert_file_addr = le32_to_cpu(sh_le.assert_file_addr);
1037 sh->assert_line = le32_to_cpu(sh_le.assert_line);
1038 sh->console_addr = le32_to_cpu(sh_le.console_addr);
1039 sh->msgtrace_addr = le32_to_cpu(sh_le.msgtrace_addr);
1040
1041 if ((sh->flags & SDPCM_SHARED_VERSION_MASK) > SDPCM_SHARED_VERSION) {
1042 brcmf_err("sdpcm shared version unsupported: dhd %d dongle %d\n",
1043 SDPCM_SHARED_VERSION,
1044 sh->flags & SDPCM_SHARED_VERSION_MASK);
1045 return -EPROTO;
1046 }
Daniel Kim0801e6c2014-01-29 15:32:13 +01001047 return 0;
Arend van Spriel9819a902015-03-11 16:11:32 +01001048
1049fail:
1050 brcmf_err("unable to obtain sdpcm_shared info: rv=%d (addr=0x%x)\n",
1051 rv, addr);
1052 sdio_release_host(bus->sdiodev->func[1]);
1053 return rv;
Daniel Kim0801e6c2014-01-29 15:32:13 +01001054}
1055
1056static void brcmf_sdio_get_console_addr(struct brcmf_sdio *bus)
1057{
1058 struct sdpcm_shared sh;
1059
1060 if (brcmf_sdio_readshared(bus, &sh) == 0)
1061 bus->console_addr = sh.console_addr;
1062}
1063#else
1064static void brcmf_sdio_get_console_addr(struct brcmf_sdio *bus)
1065{
1066}
1067#endif /* DEBUG */
1068
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001069static u32 brcmf_sdio_hostmail(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001070{
1071 u32 intstatus = 0;
1072 u32 hmb_data;
1073 u8 fcbits;
Franky Lin58692752012-05-04 18:27:36 -07001074 int ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001075
Arend van Sprielc3203372013-04-03 12:40:44 +02001076 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001077
1078 /* Read mailbox data and ack that we did so */
Franky Lin58692752012-05-04 18:27:36 -07001079 ret = r_sdreg32(bus, &hmb_data,
1080 offsetof(struct sdpcmd_regs, tohostmailboxdata));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001081
Franky Lin58692752012-05-04 18:27:36 -07001082 if (ret == 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001083 w_sdreg32(bus, SMB_INT_ACK,
Franky Lin58692752012-05-04 18:27:36 -07001084 offsetof(struct sdpcmd_regs, tosbmailbox));
Arend van Spriel80969832012-06-09 22:51:44 +02001085 bus->sdcnt.f1regdata += 2;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001086
1087 /* Dongle recomposed rx frames, accept them again */
1088 if (hmb_data & HMB_DATA_NAKHANDLED) {
Arend van Sprielc3203372013-04-03 12:40:44 +02001089 brcmf_dbg(SDIO, "Dongle reports NAK handled, expect rtx of %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001090 bus->rx_seq);
1091 if (!bus->rxskip)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001092 brcmf_err("unexpected NAKHANDLED!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001093
1094 bus->rxskip = false;
1095 intstatus |= I_HMB_FRAME_IND;
1096 }
1097
1098 /*
1099 * DEVREADY does not occur with gSPI.
1100 */
1101 if (hmb_data & (HMB_DATA_DEVREADY | HMB_DATA_FWREADY)) {
1102 bus->sdpcm_ver =
1103 (hmb_data & HMB_DATA_VERSION_MASK) >>
1104 HMB_DATA_VERSION_SHIFT;
1105 if (bus->sdpcm_ver != SDPCM_PROT_VERSION)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001106 brcmf_err("Version mismatch, dongle reports %d, "
Arend van Spriel5b435de2011-10-05 13:19:03 +02001107 "expecting %d\n",
1108 bus->sdpcm_ver, SDPCM_PROT_VERSION);
1109 else
Arend van Sprielc3203372013-04-03 12:40:44 +02001110 brcmf_dbg(SDIO, "Dongle ready, protocol version %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001111 bus->sdpcm_ver);
Daniel Kim0801e6c2014-01-29 15:32:13 +01001112
1113 /*
1114 * Retrieve console state address now that firmware should have
1115 * updated it.
1116 */
1117 brcmf_sdio_get_console_addr(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001118 }
1119
1120 /*
1121 * Flow Control has been moved into the RX headers and this out of band
1122 * method isn't used any more.
1123 * remaining backward compatible with older dongles.
1124 */
1125 if (hmb_data & HMB_DATA_FC) {
1126 fcbits = (hmb_data & HMB_DATA_FCDATA_MASK) >>
1127 HMB_DATA_FCDATA_SHIFT;
1128
1129 if (fcbits & ~bus->flowcontrol)
Arend van Spriel80969832012-06-09 22:51:44 +02001130 bus->sdcnt.fc_xoff++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001131
1132 if (bus->flowcontrol & ~fcbits)
Arend van Spriel80969832012-06-09 22:51:44 +02001133 bus->sdcnt.fc_xon++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001134
Arend van Spriel80969832012-06-09 22:51:44 +02001135 bus->sdcnt.fc_rcvd++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001136 bus->flowcontrol = fcbits;
1137 }
1138
1139 /* Shouldn't be any others */
1140 if (hmb_data & ~(HMB_DATA_DEVREADY |
1141 HMB_DATA_NAKHANDLED |
1142 HMB_DATA_FC |
1143 HMB_DATA_FWREADY |
1144 HMB_DATA_FCDATA_MASK | HMB_DATA_VERSION_MASK))
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001145 brcmf_err("Unknown mailbox data content: 0x%02x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001146 hmb_data);
1147
1148 return intstatus;
1149}
1150
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001151static void brcmf_sdio_rxfail(struct brcmf_sdio *bus, bool abort, bool rtx)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001152{
1153 uint retries = 0;
1154 u16 lastrbc;
1155 u8 hi, lo;
1156 int err;
1157
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001158 brcmf_err("%sterminate frame%s\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001159 abort ? "abort command, " : "",
1160 rtx ? ", send NAK" : "");
1161
1162 if (abort)
Arend van Spriela39be272013-12-12 11:58:58 +01001163 brcmf_sdiod_abort(bus->sdiodev, SDIO_FUNC_2);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001164
Arend van Spriela39be272013-12-12 11:58:58 +01001165 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL,
1166 SFC_RF_TERM, &err);
Arend van Spriel80969832012-06-09 22:51:44 +02001167 bus->sdcnt.f1regdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001168
1169 /* Wait until the packet has been flushed (device/FIFO stable) */
1170 for (lastrbc = retries = 0xffff; retries > 0; retries--) {
Arend van Spriela39be272013-12-12 11:58:58 +01001171 hi = brcmf_sdiod_regrb(bus->sdiodev,
1172 SBSDIO_FUNC1_RFRAMEBCHI, &err);
1173 lo = brcmf_sdiod_regrb(bus->sdiodev,
1174 SBSDIO_FUNC1_RFRAMEBCLO, &err);
Arend van Spriel80969832012-06-09 22:51:44 +02001175 bus->sdcnt.f1regdata += 2;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001176
1177 if ((hi == 0) && (lo == 0))
1178 break;
1179
1180 if ((hi > (lastrbc >> 8)) && (lo > (lastrbc & 0x00ff))) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001181 brcmf_err("count growing: last 0x%04x now 0x%04x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001182 lastrbc, (hi << 8) + lo);
1183 }
1184 lastrbc = (hi << 8) + lo;
1185 }
1186
1187 if (!retries)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001188 brcmf_err("count never zeroed: last 0x%04x\n", lastrbc);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001189 else
Arend van Sprielc3203372013-04-03 12:40:44 +02001190 brcmf_dbg(SDIO, "flush took %d iterations\n", 0xffff - retries);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001191
1192 if (rtx) {
Arend van Spriel80969832012-06-09 22:51:44 +02001193 bus->sdcnt.rxrtx++;
Franky Lin58692752012-05-04 18:27:36 -07001194 err = w_sdreg32(bus, SMB_NAK,
1195 offsetof(struct sdpcmd_regs, tosbmailbox));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001196
Arend van Spriel80969832012-06-09 22:51:44 +02001197 bus->sdcnt.f1regdata++;
Franky Lin58692752012-05-04 18:27:36 -07001198 if (err == 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001199 bus->rxskip = true;
1200 }
1201
1202 /* Clear partial in any case */
Franky Lin4754fce2012-09-19 22:21:11 +02001203 bus->cur_read.len = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001204}
1205
Hante Meuleman81c78832014-02-25 20:30:35 +01001206static void brcmf_sdio_txfail(struct brcmf_sdio *bus)
1207{
1208 struct brcmf_sdio_dev *sdiodev = bus->sdiodev;
1209 u8 i, hi, lo;
1210
1211 /* On failure, abort the command and terminate the frame */
1212 brcmf_err("sdio error, abort command and terminate frame\n");
1213 bus->sdcnt.tx_sderrs++;
1214
1215 brcmf_sdiod_abort(sdiodev, SDIO_FUNC_2);
1216 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_FRAMECTRL, SFC_WF_TERM, NULL);
1217 bus->sdcnt.f1regdata++;
1218
1219 for (i = 0; i < 3; i++) {
1220 hi = brcmf_sdiod_regrb(sdiodev, SBSDIO_FUNC1_WFRAMEBCHI, NULL);
1221 lo = brcmf_sdiod_regrb(sdiodev, SBSDIO_FUNC1_WFRAMEBCLO, NULL);
1222 bus->sdcnt.f1regdata += 2;
1223 if ((hi == 0) && (lo == 0))
1224 break;
1225 }
1226}
1227
Arend van Spriel9a95e602011-11-10 20:30:29 +01001228/* return total length of buffer chain */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001229static uint brcmf_sdio_glom_len(struct brcmf_sdio *bus)
Arend van Spriel9a95e602011-11-10 20:30:29 +01001230{
1231 struct sk_buff *p;
1232 uint total;
1233
1234 total = 0;
1235 skb_queue_walk(&bus->glom, p)
1236 total += p->len;
1237 return total;
1238}
1239
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001240static void brcmf_sdio_free_glom(struct brcmf_sdio *bus)
Arend van Spriel046808d2011-11-10 20:30:31 +01001241{
1242 struct sk_buff *cur, *next;
1243
1244 skb_queue_walk_safe(&bus->glom, cur, next) {
1245 skb_unlink(cur, &bus->glom);
1246 brcmu_pkt_buf_free_skb(cur);
1247 }
1248}
1249
Franky Lin6bc52312013-08-10 12:27:28 +02001250/**
1251 * brcmfmac sdio bus specific header
1252 * This is the lowest layer header wrapped on the packets transmitted between
1253 * host and WiFi dongle which contains information needed for SDIO core and
1254 * firmware
1255 *
Franky Lin8da9d2c2013-11-29 12:25:10 +01001256 * It consists of 3 parts: hardware header, hardware extension header and
1257 * software header
Franky Lin6bc52312013-08-10 12:27:28 +02001258 * hardware header (frame tag) - 4 bytes
1259 * Byte 0~1: Frame length
1260 * Byte 2~3: Checksum, bit-wise inverse of frame length
Franky Lin8da9d2c2013-11-29 12:25:10 +01001261 * hardware extension header - 8 bytes
1262 * Tx glom mode only, N/A for Rx or normal Tx
1263 * Byte 0~1: Packet length excluding hw frame tag
1264 * Byte 2: Reserved
1265 * Byte 3: Frame flags, bit 0: last frame indication
1266 * Byte 4~5: Reserved
1267 * Byte 6~7: Tail padding length
Franky Lin6bc52312013-08-10 12:27:28 +02001268 * software header - 8 bytes
1269 * Byte 0: Rx/Tx sequence number
1270 * Byte 1: 4 MSB Channel number, 4 LSB arbitrary flag
1271 * Byte 2: Length of next data frame, reserved for Tx
1272 * Byte 3: Data offset
1273 * Byte 4: Flow control bits, reserved for Tx
1274 * Byte 5: Maximum Sequence number allowed by firmware for Tx, N/A for Tx packet
1275 * Byte 6~7: Reserved
1276 */
1277#define SDPCM_HWHDR_LEN 4
Franky Lin8da9d2c2013-11-29 12:25:10 +01001278#define SDPCM_HWEXT_LEN 8
Franky Lin6bc52312013-08-10 12:27:28 +02001279#define SDPCM_SWHDR_LEN 8
1280#define SDPCM_HDRLEN (SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN)
Franky Lin6bc52312013-08-10 12:27:28 +02001281/* software header */
1282#define SDPCM_SEQ_MASK 0x000000ff
1283#define SDPCM_SEQ_WRAP 256
1284#define SDPCM_CHANNEL_MASK 0x00000f00
1285#define SDPCM_CHANNEL_SHIFT 8
1286#define SDPCM_CONTROL_CHANNEL 0 /* Control */
1287#define SDPCM_EVENT_CHANNEL 1 /* Asyc Event Indication */
1288#define SDPCM_DATA_CHANNEL 2 /* Data Xmit/Recv */
1289#define SDPCM_GLOM_CHANNEL 3 /* Coalesced packets */
1290#define SDPCM_TEST_CHANNEL 15 /* Test/debug packets */
1291#define SDPCM_GLOMDESC(p) (((u8 *)p)[1] & 0x80)
1292#define SDPCM_NEXTLEN_MASK 0x00ff0000
1293#define SDPCM_NEXTLEN_SHIFT 16
1294#define SDPCM_DOFFSET_MASK 0xff000000
1295#define SDPCM_DOFFSET_SHIFT 24
1296#define SDPCM_FCMASK_MASK 0x000000ff
1297#define SDPCM_WINDOW_MASK 0x0000ff00
1298#define SDPCM_WINDOW_SHIFT 8
1299
1300static inline u8 brcmf_sdio_getdatoffset(u8 *swheader)
1301{
1302 u32 hdrvalue;
1303 hdrvalue = *(u32 *)swheader;
1304 return (u8)((hdrvalue & SDPCM_DOFFSET_MASK) >> SDPCM_DOFFSET_SHIFT);
1305}
1306
Franky Linc56caa9d2016-04-11 11:35:25 +02001307static inline bool brcmf_sdio_fromevntchan(u8 *swheader)
1308{
1309 u32 hdrvalue;
1310 u8 ret;
1311
1312 hdrvalue = *(u32 *)swheader;
1313 ret = (u8)((hdrvalue & SDPCM_CHANNEL_MASK) >> SDPCM_CHANNEL_SHIFT);
1314
1315 return (ret == SDPCM_EVENT_CHANNEL);
1316}
1317
Franky Lin6bc52312013-08-10 12:27:28 +02001318static int brcmf_sdio_hdparse(struct brcmf_sdio *bus, u8 *header,
1319 struct brcmf_sdio_hdrinfo *rd,
1320 enum brcmf_sdio_frmtype type)
Franky Lin4754fce2012-09-19 22:21:11 +02001321{
1322 u16 len, checksum;
1323 u8 rx_seq, fc, tx_seq_max;
Franky Lin6bc52312013-08-10 12:27:28 +02001324 u32 swheader;
Franky Lin4754fce2012-09-19 22:21:11 +02001325
Arend van Spriel4b776962013-11-29 12:25:06 +01001326 trace_brcmf_sdpcm_hdr(SDPCM_RX, header);
Arend van Spriel76584ec2013-10-15 15:44:47 +02001327
Franky Lin6bc52312013-08-10 12:27:28 +02001328 /* hw header */
Franky Lin4754fce2012-09-19 22:21:11 +02001329 len = get_unaligned_le16(header);
1330 checksum = get_unaligned_le16(header + sizeof(u16));
1331 /* All zero means no more to read */
1332 if (!(len | checksum)) {
1333 bus->rxpending = false;
Franky Lin10510582012-11-05 16:22:26 -08001334 return -ENODATA;
Franky Lin4754fce2012-09-19 22:21:11 +02001335 }
1336 if ((u16)(~(len ^ checksum))) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001337 brcmf_err("HW header checksum error\n");
Franky Lin4754fce2012-09-19 22:21:11 +02001338 bus->sdcnt.rx_badhdr++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001339 brcmf_sdio_rxfail(bus, false, false);
Franky Lin10510582012-11-05 16:22:26 -08001340 return -EIO;
Franky Lin4754fce2012-09-19 22:21:11 +02001341 }
1342 if (len < SDPCM_HDRLEN) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001343 brcmf_err("HW header length error\n");
Franky Lin10510582012-11-05 16:22:26 -08001344 return -EPROTO;
Franky Lin4754fce2012-09-19 22:21:11 +02001345 }
Franky Lin9d7d6f92012-10-22 10:36:24 -07001346 if (type == BRCMF_SDIO_FT_SUPER &&
1347 (roundup(len, bus->blocksize) != rd->len)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001348 brcmf_err("HW superframe header length error\n");
Franky Lin10510582012-11-05 16:22:26 -08001349 return -EPROTO;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001350 }
1351 if (type == BRCMF_SDIO_FT_SUB && len > rd->len) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001352 brcmf_err("HW subframe header length error\n");
Franky Lin10510582012-11-05 16:22:26 -08001353 return -EPROTO;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001354 }
Franky Lin4754fce2012-09-19 22:21:11 +02001355 rd->len = len;
1356
Franky Lin6bc52312013-08-10 12:27:28 +02001357 /* software header */
1358 header += SDPCM_HWHDR_LEN;
1359 swheader = le32_to_cpu(*(__le32 *)header);
1360 if (type == BRCMF_SDIO_FT_SUPER && SDPCM_GLOMDESC(header)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001361 brcmf_err("Glom descriptor found in superframe head\n");
Franky Lin9d7d6f92012-10-22 10:36:24 -07001362 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001363 return -EINVAL;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001364 }
Franky Lin6bc52312013-08-10 12:27:28 +02001365 rx_seq = (u8)(swheader & SDPCM_SEQ_MASK);
1366 rd->channel = (swheader & SDPCM_CHANNEL_MASK) >> SDPCM_CHANNEL_SHIFT;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001367 if (len > MAX_RX_DATASZ && rd->channel != SDPCM_CONTROL_CHANNEL &&
1368 type != BRCMF_SDIO_FT_SUPER) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001369 brcmf_err("HW header length too long\n");
Franky Lin4754fce2012-09-19 22:21:11 +02001370 bus->sdcnt.rx_toolong++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001371 brcmf_sdio_rxfail(bus, false, false);
Franky Lin4754fce2012-09-19 22:21:11 +02001372 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001373 return -EPROTO;
Franky Lin4754fce2012-09-19 22:21:11 +02001374 }
Franky Lin9d7d6f92012-10-22 10:36:24 -07001375 if (type == BRCMF_SDIO_FT_SUPER && rd->channel != SDPCM_GLOM_CHANNEL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001376 brcmf_err("Wrong channel for superframe\n");
Franky Lin9d7d6f92012-10-22 10:36:24 -07001377 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001378 return -EINVAL;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001379 }
1380 if (type == BRCMF_SDIO_FT_SUB && rd->channel != SDPCM_DATA_CHANNEL &&
1381 rd->channel != SDPCM_EVENT_CHANNEL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001382 brcmf_err("Wrong channel for subframe\n");
Franky Lin9d7d6f92012-10-22 10:36:24 -07001383 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001384 return -EINVAL;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001385 }
Franky Lin6bc52312013-08-10 12:27:28 +02001386 rd->dat_offset = brcmf_sdio_getdatoffset(header);
Franky Lin4754fce2012-09-19 22:21:11 +02001387 if (rd->dat_offset < SDPCM_HDRLEN || rd->dat_offset > rd->len) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001388 brcmf_err("seq %d: bad data offset\n", rx_seq);
Franky Lin4754fce2012-09-19 22:21:11 +02001389 bus->sdcnt.rx_badhdr++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001390 brcmf_sdio_rxfail(bus, false, false);
Franky Lin4754fce2012-09-19 22:21:11 +02001391 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001392 return -ENXIO;
Franky Lin4754fce2012-09-19 22:21:11 +02001393 }
1394 if (rd->seq_num != rx_seq) {
Wright Feng98aff6c2016-06-03 23:31:11 +02001395 brcmf_dbg(SDIO, "seq %d, expected %d\n", rx_seq, rd->seq_num);
Franky Lin4754fce2012-09-19 22:21:11 +02001396 bus->sdcnt.rx_badseq++;
1397 rd->seq_num = rx_seq;
1398 }
Franky Lin9d7d6f92012-10-22 10:36:24 -07001399 /* no need to check the reset for subframe */
1400 if (type == BRCMF_SDIO_FT_SUB)
Franky Lin10510582012-11-05 16:22:26 -08001401 return 0;
Franky Lin6bc52312013-08-10 12:27:28 +02001402 rd->len_nxtfrm = (swheader & SDPCM_NEXTLEN_MASK) >> SDPCM_NEXTLEN_SHIFT;
Franky Lin4754fce2012-09-19 22:21:11 +02001403 if (rd->len_nxtfrm << 4 > MAX_RX_DATASZ) {
1404 /* only warm for NON glom packet */
1405 if (rd->channel != SDPCM_GLOM_CHANNEL)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001406 brcmf_err("seq %d: next length error\n", rx_seq);
Franky Lin4754fce2012-09-19 22:21:11 +02001407 rd->len_nxtfrm = 0;
1408 }
Franky Lin6bc52312013-08-10 12:27:28 +02001409 swheader = le32_to_cpu(*(__le32 *)(header + 4));
1410 fc = swheader & SDPCM_FCMASK_MASK;
Franky Lin4754fce2012-09-19 22:21:11 +02001411 if (bus->flowcontrol != fc) {
1412 if (~bus->flowcontrol & fc)
1413 bus->sdcnt.fc_xoff++;
1414 if (bus->flowcontrol & ~fc)
1415 bus->sdcnt.fc_xon++;
1416 bus->sdcnt.fc_rcvd++;
1417 bus->flowcontrol = fc;
1418 }
Franky Lin6bc52312013-08-10 12:27:28 +02001419 tx_seq_max = (swheader & SDPCM_WINDOW_MASK) >> SDPCM_WINDOW_SHIFT;
Franky Lin4754fce2012-09-19 22:21:11 +02001420 if ((u8)(tx_seq_max - bus->tx_seq) > 0x40) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001421 brcmf_err("seq %d: max tx seq number error\n", rx_seq);
Franky Lin4754fce2012-09-19 22:21:11 +02001422 tx_seq_max = bus->tx_seq + 2;
1423 }
1424 bus->tx_max = tx_seq_max;
1425
Franky Lin10510582012-11-05 16:22:26 -08001426 return 0;
Franky Lin4754fce2012-09-19 22:21:11 +02001427}
1428
Franky Lin6bc52312013-08-10 12:27:28 +02001429static inline void brcmf_sdio_update_hwhdr(u8 *header, u16 frm_length)
1430{
1431 *(__le16 *)header = cpu_to_le16(frm_length);
1432 *(((__le16 *)header) + 1) = cpu_to_le16(~frm_length);
1433}
1434
1435static void brcmf_sdio_hdpack(struct brcmf_sdio *bus, u8 *header,
1436 struct brcmf_sdio_hdrinfo *hd_info)
1437{
Franky Lin8da9d2c2013-11-29 12:25:10 +01001438 u32 hdrval;
1439 u8 hdr_offset;
Franky Lin6bc52312013-08-10 12:27:28 +02001440
1441 brcmf_sdio_update_hwhdr(header, hd_info->len);
Franky Lin8da9d2c2013-11-29 12:25:10 +01001442 hdr_offset = SDPCM_HWHDR_LEN;
Franky Lin6bc52312013-08-10 12:27:28 +02001443
Franky Lin8da9d2c2013-11-29 12:25:10 +01001444 if (bus->txglom) {
1445 hdrval = (hd_info->len - hdr_offset) | (hd_info->lastfrm << 24);
1446 *((__le32 *)(header + hdr_offset)) = cpu_to_le32(hdrval);
1447 hdrval = (u16)hd_info->tail_pad << 16;
1448 *(((__le32 *)(header + hdr_offset)) + 1) = cpu_to_le32(hdrval);
1449 hdr_offset += SDPCM_HWEXT_LEN;
1450 }
1451
1452 hdrval = hd_info->seq_num;
1453 hdrval |= (hd_info->channel << SDPCM_CHANNEL_SHIFT) &
1454 SDPCM_CHANNEL_MASK;
1455 hdrval |= (hd_info->dat_offset << SDPCM_DOFFSET_SHIFT) &
1456 SDPCM_DOFFSET_MASK;
1457 *((__le32 *)(header + hdr_offset)) = cpu_to_le32(hdrval);
1458 *(((__le32 *)(header + hdr_offset)) + 1) = 0;
1459 trace_brcmf_sdpcm_hdr(SDPCM_TX + !!(bus->txglom), header);
Franky Lin6bc52312013-08-10 12:27:28 +02001460}
1461
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001462static u8 brcmf_sdio_rxglom(struct brcmf_sdio *bus, u8 rxseq)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001463{
1464 u16 dlen, totlen;
1465 u8 *dptr, num = 0;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001466 u16 sublen;
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001467 struct sk_buff *pfirst, *pnext;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001468
1469 int errcode;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001470 u8 doff, sfdoff;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001471
Franky Lin6bc52312013-08-10 12:27:28 +02001472 struct brcmf_sdio_hdrinfo rd_new;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001473
1474 /* If packets, issue read(s) and send up packet chain */
1475 /* Return sequence numbers consumed? */
1476
Arend van Sprielc3203372013-04-03 12:40:44 +02001477 brcmf_dbg(SDIO, "start: glomd %p glom %p\n",
Arend van Sprielb83db862011-10-19 12:51:09 +02001478 bus->glomd, skb_peek(&bus->glom));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001479
1480 /* If there's a descriptor, generate the packet chain */
1481 if (bus->glomd) {
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001482 pfirst = pnext = NULL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001483 dlen = (u16) (bus->glomd->len);
1484 dptr = bus->glomd->data;
1485 if (!dlen || (dlen & 1)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001486 brcmf_err("bad glomd len(%d), ignore descriptor\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001487 dlen);
1488 dlen = 0;
1489 }
1490
1491 for (totlen = num = 0; dlen; num++) {
1492 /* Get (and move past) next length */
1493 sublen = get_unaligned_le16(dptr);
1494 dlen -= sizeof(u16);
1495 dptr += sizeof(u16);
1496 if ((sublen < SDPCM_HDRLEN) ||
1497 ((num == 0) && (sublen < (2 * SDPCM_HDRLEN)))) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001498 brcmf_err("descriptor len %d bad: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001499 num, sublen);
1500 pnext = NULL;
1501 break;
1502 }
Arend van Spriele217d1c2013-11-29 12:25:13 +01001503 if (sublen % bus->sgentry_align) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001504 brcmf_err("sublen %d not multiple of %d\n",
Arend van Spriele217d1c2013-11-29 12:25:13 +01001505 sublen, bus->sgentry_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001506 }
1507 totlen += sublen;
1508
1509 /* For last frame, adjust read len so total
1510 is a block multiple */
1511 if (!dlen) {
1512 sublen +=
1513 (roundup(totlen, bus->blocksize) - totlen);
1514 totlen = roundup(totlen, bus->blocksize);
1515 }
1516
1517 /* Allocate/chain packet for next subframe */
Arend van Spriele217d1c2013-11-29 12:25:13 +01001518 pnext = brcmu_pkt_buf_get_skb(sublen + bus->sgentry_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001519 if (pnext == NULL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001520 brcmf_err("bcm_pkt_buf_get_skb failed, num %d len %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001521 num, sublen);
1522 break;
1523 }
Arend van Sprielb83db862011-10-19 12:51:09 +02001524 skb_queue_tail(&bus->glom, pnext);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001525
1526 /* Adhere to start alignment requirements */
Arend van Spriele217d1c2013-11-29 12:25:13 +01001527 pkt_align(pnext, sublen, bus->sgentry_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001528 }
1529
1530 /* If all allocations succeeded, save packet chain
1531 in bus structure */
1532 if (pnext) {
1533 brcmf_dbg(GLOM, "allocated %d-byte packet chain for %d subframes\n",
1534 totlen, num);
Franky Lin4754fce2012-09-19 22:21:11 +02001535 if (BRCMF_GLOM_ON() && bus->cur_read.len &&
1536 totlen != bus->cur_read.len) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001537 brcmf_dbg(GLOM, "glomdesc mismatch: nextlen %d glomdesc %d rxseq %d\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001538 bus->cur_read.len, totlen, rxseq);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001539 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02001540 pfirst = pnext = NULL;
1541 } else {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001542 brcmf_sdio_free_glom(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001543 num = 0;
1544 }
1545
1546 /* Done with descriptor packet */
1547 brcmu_pkt_buf_free_skb(bus->glomd);
1548 bus->glomd = NULL;
Franky Lin4754fce2012-09-19 22:21:11 +02001549 bus->cur_read.len = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001550 }
1551
1552 /* Ok -- either we just generated a packet chain,
1553 or had one from before */
Arend van Sprielb83db862011-10-19 12:51:09 +02001554 if (!skb_queue_empty(&bus->glom)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001555 if (BRCMF_GLOM_ON()) {
1556 brcmf_dbg(GLOM, "try superframe read, packet chain:\n");
Arend van Sprielb83db862011-10-19 12:51:09 +02001557 skb_queue_walk(&bus->glom, pnext) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001558 brcmf_dbg(GLOM, " %p: %p len 0x%04x (%d)\n",
1559 pnext, (u8 *) (pnext->data),
1560 pnext->len, pnext->len);
1561 }
1562 }
1563
Arend van Sprielb83db862011-10-19 12:51:09 +02001564 pfirst = skb_peek(&bus->glom);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001565 dlen = (u16) brcmf_sdio_glom_len(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001566
1567 /* Do an SDIO read for the superframe. Configurable iovar to
1568 * read directly into the chained packet, or allocate a large
1569 * packet and and copy into the chain.
1570 */
Franky Lin38b0b0d2012-11-05 16:22:24 -08001571 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriela39be272013-12-12 11:58:58 +01001572 errcode = brcmf_sdiod_recv_chain(bus->sdiodev,
Arend van Spriela39be272013-12-12 11:58:58 +01001573 &bus->glom, dlen);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001574 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel80969832012-06-09 22:51:44 +02001575 bus->sdcnt.f2rxdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001576
Franky Lin64d66c32015-11-25 11:32:40 +01001577 /* On failure, kill the superframe */
Arend van Spriel5b435de2011-10-05 13:19:03 +02001578 if (errcode < 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001579 brcmf_err("glom read of %d bytes failed: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001580 dlen, errcode);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001581
Franky Lin38b0b0d2012-11-05 16:22:24 -08001582 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin64d66c32015-11-25 11:32:40 +01001583 brcmf_sdio_rxfail(bus, true, false);
1584 bus->sdcnt.rxglomfail++;
1585 brcmf_sdio_free_glom(bus);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001586 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001587 return 0;
1588 }
Joe Perches1e023822012-01-15 00:38:40 -08001589
1590 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
1591 pfirst->data, min_t(int, pfirst->len, 48),
1592 "SUPERFRAME:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001593
Franky Lin9d7d6f92012-10-22 10:36:24 -07001594 rd_new.seq_num = rxseq;
1595 rd_new.len = dlen;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001596 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin6bc52312013-08-10 12:27:28 +02001597 errcode = brcmf_sdio_hdparse(bus, pfirst->data, &rd_new,
1598 BRCMF_SDIO_FT_SUPER);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001599 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin9d7d6f92012-10-22 10:36:24 -07001600 bus->cur_read.len = rd_new.len_nxtfrm << 4;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001601
1602 /* Remove superframe header, remember offset */
Franky Lin9d7d6f92012-10-22 10:36:24 -07001603 skb_pull(pfirst, rd_new.dat_offset);
1604 sfdoff = rd_new.dat_offset;
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001605 num = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001606
1607 /* Validate all the subframe headers */
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001608 skb_queue_walk(&bus->glom, pnext) {
1609 /* leave when invalid subframe is found */
1610 if (errcode)
1611 break;
1612
Franky Lin9d7d6f92012-10-22 10:36:24 -07001613 rd_new.len = pnext->len;
1614 rd_new.seq_num = rxseq++;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001615 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin6bc52312013-08-10 12:27:28 +02001616 errcode = brcmf_sdio_hdparse(bus, pnext->data, &rd_new,
1617 BRCMF_SDIO_FT_SUB);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001618 sdio_release_host(bus->sdiodev->func[1]);
Joe Perches1e023822012-01-15 00:38:40 -08001619 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
Franky Lin9d7d6f92012-10-22 10:36:24 -07001620 pnext->data, 32, "subframe:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001621
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001622 num++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001623 }
1624
1625 if (errcode) {
Franky Lin64d66c32015-11-25 11:32:40 +01001626 /* Terminate frame on error */
Franky Lin38b0b0d2012-11-05 16:22:24 -08001627 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin64d66c32015-11-25 11:32:40 +01001628 brcmf_sdio_rxfail(bus, true, false);
1629 bus->sdcnt.rxglomfail++;
1630 brcmf_sdio_free_glom(bus);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001631 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001632 bus->cur_read.len = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001633 return 0;
1634 }
1635
1636 /* Basic SD framing looks ok - process each packet (header) */
Arend van Spriel5b435de2011-10-05 13:19:03 +02001637
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001638 skb_queue_walk_safe(&bus->glom, pfirst, pnext) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001639 dptr = (u8 *) (pfirst->data);
1640 sublen = get_unaligned_le16(dptr);
Franky Lin6bc52312013-08-10 12:27:28 +02001641 doff = brcmf_sdio_getdatoffset(&dptr[SDPCM_HWHDR_LEN]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001642
Joe Perches1e023822012-01-15 00:38:40 -08001643 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_DATA_ON(),
Franky Lin9d7d6f92012-10-22 10:36:24 -07001644 dptr, pfirst->len,
1645 "Rx Subframe Data:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001646
1647 __skb_trim(pfirst, sublen);
1648 skb_pull(pfirst, doff);
1649
1650 if (pfirst->len == 0) {
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001651 skb_unlink(pfirst, &bus->glom);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001652 brcmu_pkt_buf_free_skb(pfirst);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001653 continue;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001654 }
1655
Joe Perches1e023822012-01-15 00:38:40 -08001656 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
1657 pfirst->data,
1658 min_t(int, pfirst->len, 32),
1659 "subframe %d to stack, %p (%p/%d) nxt/lnk %p/%p\n",
1660 bus->glom.qlen, pfirst, pfirst->data,
1661 pfirst->len, pfirst->next,
1662 pfirst->prev);
Arend van Spriel05f38202013-09-25 13:05:46 +02001663 skb_unlink(pfirst, &bus->glom);
Gavin Li8e290ce2017-01-17 15:24:05 -08001664 if (brcmf_sdio_fromevntchan(&dptr[SDPCM_HWHDR_LEN]))
Franky Linc56caa9d2016-04-11 11:35:25 +02001665 brcmf_rx_event(bus->sdiodev->dev, pfirst);
1666 else
1667 brcmf_rx_frame(bus->sdiodev->dev, pfirst,
1668 false);
Arend van Spriel05f38202013-09-25 13:05:46 +02001669 bus->sdcnt.rxglompkts++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001670 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02001671
Arend van Spriel80969832012-06-09 22:51:44 +02001672 bus->sdcnt.rxglomframes++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001673 }
1674 return num;
1675}
1676
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001677static int brcmf_sdio_dcmd_resp_wait(struct brcmf_sdio *bus, uint *condition,
1678 bool *pending)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001679{
1680 DECLARE_WAITQUEUE(wait, current);
Arend van Spriel63ce3d52016-01-05 11:05:48 +01001681 int timeout = DCMD_RESP_TIMEOUT;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001682
1683 /* Wait until control frame is available */
1684 add_wait_queue(&bus->dcmd_resp_wait, &wait);
1685 set_current_state(TASK_INTERRUPTIBLE);
1686
1687 while (!(*condition) && (!signal_pending(current) && timeout))
1688 timeout = schedule_timeout(timeout);
1689
1690 if (signal_pending(current))
1691 *pending = true;
1692
1693 set_current_state(TASK_RUNNING);
1694 remove_wait_queue(&bus->dcmd_resp_wait, &wait);
1695
1696 return timeout;
1697}
1698
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001699static int brcmf_sdio_dcmd_resp_wake(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001700{
Kosuke Tatsukawaa7decc42015-12-10 13:43:08 +01001701 wake_up_interruptible(&bus->dcmd_resp_wait);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001702
1703 return 0;
1704}
1705static void
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001706brcmf_sdio_read_control(struct brcmf_sdio *bus, u8 *hdr, uint len, uint doff)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001707{
1708 uint rdlen, pad;
Franky Lindd43a012012-11-05 16:22:22 -08001709 u8 *buf = NULL, *rbuf;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001710 int sdret;
1711
1712 brcmf_dbg(TRACE, "Enter\n");
1713
Franky Lindd43a012012-11-05 16:22:22 -08001714 if (bus->rxblen)
1715 buf = vzalloc(bus->rxblen);
Joe Perches14f8dc42013-02-07 11:46:27 +00001716 if (!buf)
Franky Lindd43a012012-11-05 16:22:22 -08001717 goto done;
Joe Perches14f8dc42013-02-07 11:46:27 +00001718
Franky Lindd43a012012-11-05 16:22:22 -08001719 rbuf = bus->rxbuf;
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01001720 pad = ((unsigned long)rbuf % bus->head_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001721 if (pad)
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01001722 rbuf += (bus->head_align - pad);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001723
1724 /* Copy the already-read portion over */
Franky Lindd43a012012-11-05 16:22:22 -08001725 memcpy(buf, hdr, BRCMF_FIRSTREAD);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001726 if (len <= BRCMF_FIRSTREAD)
1727 goto gotpkt;
1728
1729 /* Raise rdlen to next SDIO block to avoid tail command */
1730 rdlen = len - BRCMF_FIRSTREAD;
1731 if (bus->roundup && bus->blocksize && (rdlen > bus->blocksize)) {
1732 pad = bus->blocksize - (rdlen % bus->blocksize);
1733 if ((pad <= bus->roundup) && (pad < bus->blocksize) &&
Franky Linb01a6b32011-12-16 18:37:03 -08001734 ((len + pad) < bus->sdiodev->bus_if->maxctl))
Arend van Spriel5b435de2011-10-05 13:19:03 +02001735 rdlen += pad;
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01001736 } else if (rdlen % bus->head_align) {
1737 rdlen += bus->head_align - (rdlen % bus->head_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001738 }
1739
Arend van Spriel5b435de2011-10-05 13:19:03 +02001740 /* Drop if the read is too big or it exceeds our maximum */
Franky Linb01a6b32011-12-16 18:37:03 -08001741 if ((rdlen + BRCMF_FIRSTREAD) > bus->sdiodev->bus_if->maxctl) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001742 brcmf_err("%d-byte control read exceeds %d-byte buffer\n",
Franky Linb01a6b32011-12-16 18:37:03 -08001743 rdlen, bus->sdiodev->bus_if->maxctl);
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 Linb01a6b32011-12-16 18:37:03 -08001748 if ((len - doff) > bus->sdiodev->bus_if->maxctl) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001749 brcmf_err("%d-byte ctl frame (%d-byte ctl data) exceeds %d-byte limit\n",
Franky Linb01a6b32011-12-16 18:37:03 -08001750 len, len - doff, bus->sdiodev->bus_if->maxctl);
Arend van Spriel80969832012-06-09 22:51:44 +02001751 bus->sdcnt.rx_toolong++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001752 brcmf_sdio_rxfail(bus, false, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001753 goto done;
1754 }
1755
Franky Lindd43a012012-11-05 16:22:22 -08001756 /* Read remain of frame body */
Arend van Spriela7cdd822013-12-12 11:58:59 +01001757 sdret = brcmf_sdiod_recv_buf(bus->sdiodev, rbuf, rdlen);
Arend van Spriel80969832012-06-09 22:51:44 +02001758 bus->sdcnt.f2rxdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001759
1760 /* Control frame failures need retransmission */
1761 if (sdret < 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001762 brcmf_err("read %d control bytes failed: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001763 rdlen, sdret);
Arend van Spriel80969832012-06-09 22:51:44 +02001764 bus->sdcnt.rxc_errors++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001765 brcmf_sdio_rxfail(bus, true, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001766 goto done;
Franky Lindd43a012012-11-05 16:22:22 -08001767 } else
1768 memcpy(buf + BRCMF_FIRSTREAD, rbuf, rdlen);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001769
1770gotpkt:
1771
Joe Perches1e023822012-01-15 00:38:40 -08001772 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_CTL_ON(),
Franky Lindd43a012012-11-05 16:22:22 -08001773 buf, len, "RxCtrl:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001774
1775 /* Point to valid data and indicate its length */
Franky Lindd43a012012-11-05 16:22:22 -08001776 spin_lock_bh(&bus->rxctl_lock);
1777 if (bus->rxctl) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001778 brcmf_err("last control frame is being processed.\n");
Franky Lindd43a012012-11-05 16:22:22 -08001779 spin_unlock_bh(&bus->rxctl_lock);
1780 vfree(buf);
1781 goto done;
1782 }
1783 bus->rxctl = buf + doff;
1784 bus->rxctl_orig = buf;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001785 bus->rxlen = len - doff;
Franky Lindd43a012012-11-05 16:22:22 -08001786 spin_unlock_bh(&bus->rxctl_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001787
1788done:
1789 /* Awake any waiters */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001790 brcmf_sdio_dcmd_resp_wake(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001791}
1792
1793/* Pad read to blocksize for efficiency */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001794static void brcmf_sdio_pad(struct brcmf_sdio *bus, u16 *pad, u16 *rdlen)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001795{
1796 if (bus->roundup && bus->blocksize && *rdlen > bus->blocksize) {
1797 *pad = bus->blocksize - (*rdlen % bus->blocksize);
1798 if (*pad <= bus->roundup && *pad < bus->blocksize &&
1799 *rdlen + *pad + BRCMF_FIRSTREAD < MAX_RX_DATASZ)
1800 *rdlen += *pad;
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01001801 } else if (*rdlen % bus->head_align) {
1802 *rdlen += bus->head_align - (*rdlen % bus->head_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001803 }
1804}
1805
Franky Lin4754fce2012-09-19 22:21:11 +02001806static uint brcmf_sdio_readframes(struct brcmf_sdio *bus, uint maxframes)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001807{
Arend van Spriel5b435de2011-10-05 13:19:03 +02001808 struct sk_buff *pkt; /* Packet for event or data frames */
1809 u16 pad; /* Number of pad bytes to read */
Arend van Spriel5b435de2011-10-05 13:19:03 +02001810 uint rxleft = 0; /* Remaining number of frames allowed */
Arend van Spriel349e7102013-03-03 12:45:28 +01001811 int ret; /* Return code from calls */
Arend van Spriel5b435de2011-10-05 13:19:03 +02001812 uint rxcount = 0; /* Total frames read */
Franky Lin6bc52312013-08-10 12:27:28 +02001813 struct brcmf_sdio_hdrinfo *rd = &bus->cur_read, rd_new;
Franky Lin4754fce2012-09-19 22:21:11 +02001814 u8 head_read = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001815
1816 brcmf_dbg(TRACE, "Enter\n");
1817
1818 /* Not finished unless we encounter no more frames indication */
Franky Lin4754fce2012-09-19 22:21:11 +02001819 bus->rxpending = true;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001820
Franky Lin4754fce2012-09-19 22:21:11 +02001821 for (rd->seq_num = bus->rx_seq, rxleft = maxframes;
Arend van Spriela1ce7a02015-02-06 18:36:42 +01001822 !bus->rxskip && rxleft && bus->sdiodev->state == BRCMF_SDIOD_DATA;
Franky Lin4754fce2012-09-19 22:21:11 +02001823 rd->seq_num++, rxleft--) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001824
1825 /* Handle glomming separately */
Arend van Sprielb83db862011-10-19 12:51:09 +02001826 if (bus->glomd || !skb_queue_empty(&bus->glom)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001827 u8 cnt;
1828 brcmf_dbg(GLOM, "calling rxglom: glomd %p, glom %p\n",
Arend van Sprielb83db862011-10-19 12:51:09 +02001829 bus->glomd, skb_peek(&bus->glom));
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001830 cnt = brcmf_sdio_rxglom(bus, rd->seq_num);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001831 brcmf_dbg(GLOM, "rxglom returned %d\n", cnt);
Franky Lin4754fce2012-09-19 22:21:11 +02001832 rd->seq_num += cnt - 1;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001833 rxleft = (rxleft > cnt) ? (rxleft - cnt) : 1;
1834 continue;
1835 }
1836
Franky Lin4754fce2012-09-19 22:21:11 +02001837 rd->len_left = rd->len;
1838 /* read header first for unknow frame length */
Franky Lin38b0b0d2012-11-05 16:22:24 -08001839 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001840 if (!rd->len) {
Arend van Spriela39be272013-12-12 11:58:58 +01001841 ret = brcmf_sdiod_recv_buf(bus->sdiodev,
Arend van Spriela39be272013-12-12 11:58:58 +01001842 bus->rxhdr, BRCMF_FIRSTREAD);
Franky Lin4754fce2012-09-19 22:21:11 +02001843 bus->sdcnt.f2rxhdrs++;
Arend van Spriel349e7102013-03-03 12:45:28 +01001844 if (ret < 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001845 brcmf_err("RXHEADER FAILED: %d\n",
Arend van Spriel349e7102013-03-03 12:45:28 +01001846 ret);
Franky Lin4754fce2012-09-19 22:21:11 +02001847 bus->sdcnt.rx_hdrfail++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001848 brcmf_sdio_rxfail(bus, true, true);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001849 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001850 continue;
1851 }
1852
Franky Lin4754fce2012-09-19 22:21:11 +02001853 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() || BRCMF_HDRS_ON(),
1854 bus->rxhdr, SDPCM_HDRLEN,
1855 "RxHdr:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001856
Franky Lin6bc52312013-08-10 12:27:28 +02001857 if (brcmf_sdio_hdparse(bus, bus->rxhdr, rd,
1858 BRCMF_SDIO_FT_NORMAL)) {
Franky Lin38b0b0d2012-11-05 16:22:24 -08001859 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001860 if (!bus->rxpending)
1861 break;
1862 else
1863 continue;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001864 }
1865
Franky Lin4754fce2012-09-19 22:21:11 +02001866 if (rd->channel == SDPCM_CONTROL_CHANNEL) {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001867 brcmf_sdio_read_control(bus, bus->rxhdr,
1868 rd->len,
1869 rd->dat_offset);
Franky Lin4754fce2012-09-19 22:21:11 +02001870 /* prepare the descriptor for the next read */
1871 rd->len = rd->len_nxtfrm << 4;
1872 rd->len_nxtfrm = 0;
1873 /* treat all packet as event if we don't know */
1874 rd->channel = SDPCM_EVENT_CHANNEL;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001875 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001876 continue;
1877 }
1878 rd->len_left = rd->len > BRCMF_FIRSTREAD ?
1879 rd->len - BRCMF_FIRSTREAD : 0;
1880 head_read = BRCMF_FIRSTREAD;
1881 }
1882
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001883 brcmf_sdio_pad(bus, &pad, &rd->len_left);
Franky Lin4754fce2012-09-19 22:21:11 +02001884
1885 pkt = brcmu_pkt_buf_get_skb(rd->len_left + head_read +
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01001886 bus->head_align);
Franky Lin4754fce2012-09-19 22:21:11 +02001887 if (!pkt) {
1888 /* Give up on data, request rtx of events */
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001889 brcmf_err("brcmu_pkt_buf_get_skb failed\n");
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001890 brcmf_sdio_rxfail(bus, false,
Franky Lin4754fce2012-09-19 22:21:11 +02001891 RETRYCHAN(rd->channel));
Franky Lin38b0b0d2012-11-05 16:22:24 -08001892 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001893 continue;
1894 }
1895 skb_pull(pkt, head_read);
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01001896 pkt_align(pkt, rd->len_left, bus->head_align);
Franky Lin4754fce2012-09-19 22:21:11 +02001897
Arend van Spriela7cdd822013-12-12 11:58:59 +01001898 ret = brcmf_sdiod_recv_pkt(bus->sdiodev, pkt);
Franky Lin4754fce2012-09-19 22:21:11 +02001899 bus->sdcnt.f2rxdata++;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001900 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001901
Arend van Spriel349e7102013-03-03 12:45:28 +01001902 if (ret < 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001903 brcmf_err("read %d bytes from channel %d failed: %d\n",
Arend van Spriel349e7102013-03-03 12:45:28 +01001904 rd->len, rd->channel, ret);
Franky Lin4754fce2012-09-19 22:21:11 +02001905 brcmu_pkt_buf_free_skb(pkt);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001906 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001907 brcmf_sdio_rxfail(bus, true,
Franky Lin4754fce2012-09-19 22:21:11 +02001908 RETRYCHAN(rd->channel));
Franky Lin38b0b0d2012-11-05 16:22:24 -08001909 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001910 continue;
1911 }
1912
1913 if (head_read) {
1914 skb_push(pkt, head_read);
1915 memcpy(pkt->data, bus->rxhdr, head_read);
1916 head_read = 0;
1917 } else {
1918 memcpy(bus->rxhdr, pkt->data, SDPCM_HDRLEN);
1919 rd_new.seq_num = rd->seq_num;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001920 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin6bc52312013-08-10 12:27:28 +02001921 if (brcmf_sdio_hdparse(bus, bus->rxhdr, &rd_new,
1922 BRCMF_SDIO_FT_NORMAL)) {
Franky Lin4754fce2012-09-19 22:21:11 +02001923 rd->len = 0;
1924 brcmu_pkt_buf_free_skb(pkt);
1925 }
Arend van Spriel80969832012-06-09 22:51:44 +02001926 bus->sdcnt.rx_readahead_cnt++;
Franky Lin4754fce2012-09-19 22:21:11 +02001927 if (rd->len != roundup(rd_new.len, 16)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001928 brcmf_err("frame length mismatch:read %d, should be %d\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001929 rd->len,
1930 roundup(rd_new.len, 16) >> 4);
1931 rd->len = 0;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001932 brcmf_sdio_rxfail(bus, true, true);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001933 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001934 brcmu_pkt_buf_free_skb(pkt);
1935 continue;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001936 }
Franky Lin38b0b0d2012-11-05 16:22:24 -08001937 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001938 rd->len_nxtfrm = rd_new.len_nxtfrm;
1939 rd->channel = rd_new.channel;
1940 rd->dat_offset = rd_new.dat_offset;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001941
Joe Perches1e023822012-01-15 00:38:40 -08001942 brcmf_dbg_hex_dump(!(BRCMF_BYTES_ON() &&
1943 BRCMF_DATA_ON()) &&
1944 BRCMF_HDRS_ON(),
1945 bus->rxhdr, SDPCM_HDRLEN,
1946 "RxHdr:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001947
Franky Lin4754fce2012-09-19 22:21:11 +02001948 if (rd_new.channel == SDPCM_CONTROL_CHANNEL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001949 brcmf_err("readahead on control packet %d?\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001950 rd_new.seq_num);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001951 /* Force retry w/normal header read */
Franky Lin4754fce2012-09-19 22:21:11 +02001952 rd->len = 0;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001953 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001954 brcmf_sdio_rxfail(bus, false, true);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001955 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001956 brcmu_pkt_buf_free_skb(pkt);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001957 continue;
1958 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02001959 }
1960
Joe Perches1e023822012-01-15 00:38:40 -08001961 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_DATA_ON(),
Franky Lin4754fce2012-09-19 22:21:11 +02001962 pkt->data, rd->len, "Rx Data:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001963
Arend van Spriel5b435de2011-10-05 13:19:03 +02001964 /* Save superframe descriptor and allocate packet frame */
Franky Lin4754fce2012-09-19 22:21:11 +02001965 if (rd->channel == SDPCM_GLOM_CHANNEL) {
Franky Lin6bc52312013-08-10 12:27:28 +02001966 if (SDPCM_GLOMDESC(&bus->rxhdr[SDPCM_HWHDR_LEN])) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001967 brcmf_dbg(GLOM, "glom descriptor, %d bytes:\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001968 rd->len);
Joe Perches1e023822012-01-15 00:38:40 -08001969 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
Franky Lin4754fce2012-09-19 22:21:11 +02001970 pkt->data, rd->len,
Joe Perches1e023822012-01-15 00:38:40 -08001971 "Glom Data:\n");
Franky Lin4754fce2012-09-19 22:21:11 +02001972 __skb_trim(pkt, rd->len);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001973 skb_pull(pkt, SDPCM_HDRLEN);
1974 bus->glomd = pkt;
1975 } else {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001976 brcmf_err("%s: glom superframe w/o "
Arend van Spriel5b435de2011-10-05 13:19:03 +02001977 "descriptor!\n", __func__);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001978 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001979 brcmf_sdio_rxfail(bus, false, false);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001980 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001981 }
Franky Lin4754fce2012-09-19 22:21:11 +02001982 /* prepare the descriptor for the next read */
1983 rd->len = rd->len_nxtfrm << 4;
1984 rd->len_nxtfrm = 0;
1985 /* treat all packet as event if we don't know */
1986 rd->channel = SDPCM_EVENT_CHANNEL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001987 continue;
1988 }
1989
1990 /* Fill in packet len and prio, deliver upward */
Franky Lin4754fce2012-09-19 22:21:11 +02001991 __skb_trim(pkt, rd->len);
1992 skb_pull(pkt, rd->dat_offset);
1993
Franky Linc56caa9d2016-04-11 11:35:25 +02001994 if (pkt->len == 0)
1995 brcmu_pkt_buf_free_skb(pkt);
1996 else if (rd->channel == SDPCM_EVENT_CHANNEL)
1997 brcmf_rx_event(bus->sdiodev->dev, pkt);
1998 else
1999 brcmf_rx_frame(bus->sdiodev->dev, pkt,
2000 false);
2001
Franky Lin4754fce2012-09-19 22:21:11 +02002002 /* prepare the descriptor for the next read */
2003 rd->len = rd->len_nxtfrm << 4;
2004 rd->len_nxtfrm = 0;
2005 /* treat all packet as event if we don't know */
2006 rd->channel = SDPCM_EVENT_CHANNEL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002007 }
Franky Lin4754fce2012-09-19 22:21:11 +02002008
Arend van Spriel5b435de2011-10-05 13:19:03 +02002009 rxcount = maxframes - rxleft;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002010 /* Message if we hit the limit */
2011 if (!rxleft)
Franky Lin4754fce2012-09-19 22:21:11 +02002012 brcmf_dbg(DATA, "hit rx limit of %d frames\n", maxframes);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002013 else
Arend van Spriel5b435de2011-10-05 13:19:03 +02002014 brcmf_dbg(DATA, "processed %d frames\n", rxcount);
2015 /* Back off rxseq if awaiting rtx, update rx_seq */
2016 if (bus->rxskip)
Franky Lin4754fce2012-09-19 22:21:11 +02002017 rd->seq_num--;
2018 bus->rx_seq = rd->seq_num;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002019
2020 return rxcount;
2021}
2022
Arend van Spriel5b435de2011-10-05 13:19:03 +02002023static void
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002024brcmf_sdio_wait_event_wakeup(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002025{
Kosuke Tatsukawaa7decc42015-12-10 13:43:08 +01002026 wake_up_interruptible(&bus->ctrl_wait);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002027 return;
2028}
2029
Franky Lin8da9d2c2013-11-29 12:25:10 +01002030static int brcmf_sdio_txpkt_hdalign(struct brcmf_sdio *bus, struct sk_buff *pkt)
2031{
Arend van Spriele217d1c2013-11-29 12:25:13 +01002032 u16 head_pad;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002033 u8 *dat_buf;
2034
Franky Lin8da9d2c2013-11-29 12:25:10 +01002035 dat_buf = (u8 *)(pkt->data);
2036
2037 /* Check head padding */
Arend van Spriele217d1c2013-11-29 12:25:13 +01002038 head_pad = ((unsigned long)dat_buf % bus->head_align);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002039 if (head_pad) {
2040 if (skb_headroom(pkt) < head_pad) {
2041 bus->sdiodev->bus_if->tx_realloc++;
2042 head_pad = 0;
2043 if (skb_cow(pkt, head_pad))
2044 return -ENOMEM;
2045 }
2046 skb_push(pkt, head_pad);
2047 dat_buf = (u8 *)(pkt->data);
2048 memset(dat_buf, 0, head_pad + bus->tx_hdrlen);
2049 }
2050 return head_pad;
2051}
2052
Franky Lin5491c112013-09-25 13:05:43 +02002053/**
2054 * struct brcmf_skbuff_cb reserves first two bytes in sk_buff::cb for
2055 * bus layer usage.
2056 */
Franky Linb05e9252013-08-10 12:27:26 +02002057/* flag marking a dummy skb added for DMA alignment requirement */
Franky Lin5491c112013-09-25 13:05:43 +02002058#define ALIGN_SKB_FLAG 0x8000
Franky Linb05e9252013-08-10 12:27:26 +02002059/* bit mask of data length chopped from the previous packet */
Franky Lin5491c112013-09-25 13:05:43 +02002060#define ALIGN_SKB_CHOP_LEN_MASK 0x7fff
2061
Franky Lin8da9d2c2013-11-29 12:25:10 +01002062static int brcmf_sdio_txpkt_prep_sg(struct brcmf_sdio *bus,
Arend van Spriela64304f2013-10-15 15:44:56 +02002063 struct sk_buff_head *pktq,
Franky Lin8da9d2c2013-11-29 12:25:10 +01002064 struct sk_buff *pkt, u16 total_len)
Arend van Spriela64304f2013-10-15 15:44:56 +02002065{
Franky Lin8da9d2c2013-11-29 12:25:10 +01002066 struct brcmf_sdio_dev *sdiodev;
Arend van Spriela64304f2013-10-15 15:44:56 +02002067 struct sk_buff *pkt_pad;
Arend van Spriele217d1c2013-11-29 12:25:13 +01002068 u16 tail_pad, tail_chop, chain_pad;
Arend van Spriela64304f2013-10-15 15:44:56 +02002069 unsigned int blksize;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002070 bool lastfrm;
2071 int ntail, ret;
Arend van Spriela64304f2013-10-15 15:44:56 +02002072
Franky Lin8da9d2c2013-11-29 12:25:10 +01002073 sdiodev = bus->sdiodev;
Arend van Spriela64304f2013-10-15 15:44:56 +02002074 blksize = sdiodev->func[SDIO_FUNC_2]->cur_blksize;
Arend van Spriela64304f2013-10-15 15:44:56 +02002075 /* sg entry alignment should be a divisor of block size */
Arend van Spriele217d1c2013-11-29 12:25:13 +01002076 WARN_ON(blksize % bus->sgentry_align);
Arend van Spriela64304f2013-10-15 15:44:56 +02002077
2078 /* Check tail padding */
Franky Lin8da9d2c2013-11-29 12:25:10 +01002079 lastfrm = skb_queue_is_last(pktq, pkt);
2080 tail_pad = 0;
Arend van Spriele217d1c2013-11-29 12:25:13 +01002081 tail_chop = pkt->len % bus->sgentry_align;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002082 if (tail_chop)
Arend van Spriele217d1c2013-11-29 12:25:13 +01002083 tail_pad = bus->sgentry_align - tail_chop;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002084 chain_pad = (total_len + tail_pad) % blksize;
2085 if (lastfrm && chain_pad)
2086 tail_pad += blksize - chain_pad;
Arend van Spriela64304f2013-10-15 15:44:56 +02002087 if (skb_tailroom(pkt) < tail_pad && pkt->len > blksize) {
Arend van Spriel1eb43012014-02-20 18:55:55 +01002088 pkt_pad = brcmu_pkt_buf_get_skb(tail_pad + tail_chop +
2089 bus->head_align);
Arend van Spriela64304f2013-10-15 15:44:56 +02002090 if (pkt_pad == NULL)
2091 return -ENOMEM;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002092 ret = brcmf_sdio_txpkt_hdalign(bus, pkt_pad);
Dave Jones2dc3a8e2014-03-06 15:51:08 -05002093 if (unlikely(ret < 0)) {
2094 kfree_skb(pkt_pad);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002095 return ret;
Dave Jones2dc3a8e2014-03-06 15:51:08 -05002096 }
Arend van Spriela64304f2013-10-15 15:44:56 +02002097 memcpy(pkt_pad->data,
2098 pkt->data + pkt->len - tail_chop,
2099 tail_chop);
Arend van Spriel5aa9f0e2014-02-25 20:30:27 +01002100 *(u16 *)(pkt_pad->cb) = ALIGN_SKB_FLAG + tail_chop;
Arend van Spriela64304f2013-10-15 15:44:56 +02002101 skb_trim(pkt, pkt->len - tail_chop);
Arend van Spriel1eb43012014-02-20 18:55:55 +01002102 skb_trim(pkt_pad, tail_pad + tail_chop);
Arend van Spriela64304f2013-10-15 15:44:56 +02002103 __skb_queue_after(pktq, pkt, pkt_pad);
2104 } else {
2105 ntail = pkt->data_len + tail_pad -
2106 (pkt->end - pkt->tail);
2107 if (skb_cloned(pkt) || ntail > 0)
2108 if (pskb_expand_head(pkt, 0, ntail, GFP_ATOMIC))
2109 return -ENOMEM;
2110 if (skb_linearize(pkt))
2111 return -ENOMEM;
Arend van Spriela64304f2013-10-15 15:44:56 +02002112 __skb_put(pkt, tail_pad);
2113 }
2114
Franky Lin8da9d2c2013-11-29 12:25:10 +01002115 return tail_pad;
Arend van Spriela64304f2013-10-15 15:44:56 +02002116}
2117
Franky Linb05e9252013-08-10 12:27:26 +02002118/**
2119 * brcmf_sdio_txpkt_prep - packet preparation for transmit
2120 * @bus: brcmf_sdio structure pointer
2121 * @pktq: packet list pointer
2122 * @chan: virtual channel to transmit the packet
2123 *
2124 * Processes to be applied to the packet
2125 * - Align data buffer pointer
2126 * - Align data buffer length
2127 * - Prepare header
2128 * Return: negative value if there is error
2129 */
2130static int
2131brcmf_sdio_txpkt_prep(struct brcmf_sdio *bus, struct sk_buff_head *pktq,
2132 uint chan)
2133{
Franky Lin8da9d2c2013-11-29 12:25:10 +01002134 u16 head_pad, total_len;
Arend van Spriela64304f2013-10-15 15:44:56 +02002135 struct sk_buff *pkt_next;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002136 u8 txseq;
2137 int ret;
Franky Lin6bc52312013-08-10 12:27:28 +02002138 struct brcmf_sdio_hdrinfo hd_info = {0};
Franky Linb05e9252013-08-10 12:27:26 +02002139
Franky Lin8da9d2c2013-11-29 12:25:10 +01002140 txseq = bus->tx_seq;
2141 total_len = 0;
2142 skb_queue_walk(pktq, pkt_next) {
2143 /* alignment packet inserted in previous
2144 * loop cycle can be skipped as it is
2145 * already properly aligned and does not
2146 * need an sdpcm header.
2147 */
Arend van Spriel5aa9f0e2014-02-25 20:30:27 +01002148 if (*(u16 *)(pkt_next->cb) & ALIGN_SKB_FLAG)
Franky Lin8da9d2c2013-11-29 12:25:10 +01002149 continue;
Franky Linb05e9252013-08-10 12:27:26 +02002150
Franky Lin8da9d2c2013-11-29 12:25:10 +01002151 /* align packet data pointer */
2152 ret = brcmf_sdio_txpkt_hdalign(bus, pkt_next);
2153 if (ret < 0)
2154 return ret;
2155 head_pad = (u16)ret;
2156 if (head_pad)
Arend van Spriel1eb43012014-02-20 18:55:55 +01002157 memset(pkt_next->data + bus->tx_hdrlen, 0, head_pad);
Franky Linb05e9252013-08-10 12:27:26 +02002158
Franky Lin8da9d2c2013-11-29 12:25:10 +01002159 total_len += pkt_next->len;
Franky Linb05e9252013-08-10 12:27:26 +02002160
Arend van Spriela64304f2013-10-15 15:44:56 +02002161 hd_info.len = pkt_next->len;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002162 hd_info.lastfrm = skb_queue_is_last(pktq, pkt_next);
2163 if (bus->txglom && pktq->qlen > 1) {
2164 ret = brcmf_sdio_txpkt_prep_sg(bus, pktq,
2165 pkt_next, total_len);
2166 if (ret < 0)
2167 return ret;
2168 hd_info.tail_pad = (u16)ret;
2169 total_len += (u16)ret;
2170 }
2171
2172 hd_info.channel = chan;
2173 hd_info.dat_offset = head_pad + bus->tx_hdrlen;
2174 hd_info.seq_num = txseq++;
2175
2176 /* Now fill the header */
2177 brcmf_sdio_hdpack(bus, pkt_next->data, &hd_info);
2178
2179 if (BRCMF_BYTES_ON() &&
2180 ((BRCMF_CTL_ON() && chan == SDPCM_CONTROL_CHANNEL) ||
2181 (BRCMF_DATA_ON() && chan != SDPCM_CONTROL_CHANNEL)))
Hante Meuleman47ab4cd2014-02-25 20:30:29 +01002182 brcmf_dbg_hex_dump(true, pkt_next->data, hd_info.len,
Franky Lin8da9d2c2013-11-29 12:25:10 +01002183 "Tx Frame:\n");
2184 else if (BRCMF_HDRS_ON())
Hante Meuleman47ab4cd2014-02-25 20:30:29 +01002185 brcmf_dbg_hex_dump(true, pkt_next->data,
Franky Lin8da9d2c2013-11-29 12:25:10 +01002186 head_pad + bus->tx_hdrlen,
2187 "Tx Header:\n");
Franky Linb05e9252013-08-10 12:27:26 +02002188 }
Franky Lin8da9d2c2013-11-29 12:25:10 +01002189 /* Hardware length tag of the first packet should be total
2190 * length of the chain (including padding)
2191 */
2192 if (bus->txglom)
2193 brcmf_sdio_update_hwhdr(pktq->next->data, total_len);
Franky Linb05e9252013-08-10 12:27:26 +02002194 return 0;
2195}
2196
2197/**
2198 * brcmf_sdio_txpkt_postp - packet post processing for transmit
2199 * @bus: brcmf_sdio structure pointer
2200 * @pktq: packet list pointer
2201 *
2202 * Processes to be applied to the packet
2203 * - Remove head padding
2204 * - Remove tail padding
2205 */
2206static void
2207brcmf_sdio_txpkt_postp(struct brcmf_sdio *bus, struct sk_buff_head *pktq)
2208{
2209 u8 *hdr;
2210 u32 dat_offset;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002211 u16 tail_pad;
Arend van Spriel5aa9f0e2014-02-25 20:30:27 +01002212 u16 dummy_flags, chop_len;
Franky Linb05e9252013-08-10 12:27:26 +02002213 struct sk_buff *pkt_next, *tmp, *pkt_prev;
2214
2215 skb_queue_walk_safe(pktq, pkt_next, tmp) {
Arend van Spriel5aa9f0e2014-02-25 20:30:27 +01002216 dummy_flags = *(u16 *)(pkt_next->cb);
Franky Lin5491c112013-09-25 13:05:43 +02002217 if (dummy_flags & ALIGN_SKB_FLAG) {
2218 chop_len = dummy_flags & ALIGN_SKB_CHOP_LEN_MASK;
Franky Linb05e9252013-08-10 12:27:26 +02002219 if (chop_len) {
2220 pkt_prev = pkt_next->prev;
Franky Linb05e9252013-08-10 12:27:26 +02002221 skb_put(pkt_prev, chop_len);
2222 }
2223 __skb_unlink(pkt_next, pktq);
2224 brcmu_pkt_buf_free_skb(pkt_next);
2225 } else {
Franky Lin8da9d2c2013-11-29 12:25:10 +01002226 hdr = pkt_next->data + bus->tx_hdrlen - SDPCM_SWHDR_LEN;
Franky Linb05e9252013-08-10 12:27:26 +02002227 dat_offset = le32_to_cpu(*(__le32 *)hdr);
2228 dat_offset = (dat_offset & SDPCM_DOFFSET_MASK) >>
2229 SDPCM_DOFFSET_SHIFT;
2230 skb_pull(pkt_next, dat_offset);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002231 if (bus->txglom) {
2232 tail_pad = le16_to_cpu(*(__le16 *)(hdr - 2));
2233 skb_trim(pkt_next, pkt_next->len - tail_pad);
2234 }
Franky Linb05e9252013-08-10 12:27:26 +02002235 }
2236 }
2237}
2238
Arend van Spriel5b435de2011-10-05 13:19:03 +02002239/* Writes a HW/SW header into the packet and sends it. */
2240/* Assumes: (a) header space already there, (b) caller holds lock */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002241static int brcmf_sdio_txpkt(struct brcmf_sdio *bus, struct sk_buff_head *pktq,
2242 uint chan)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002243{
2244 int ret;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002245 struct sk_buff *pkt_next, *tmp;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002246
2247 brcmf_dbg(TRACE, "Enter\n");
2248
Franky Lin8da9d2c2013-11-29 12:25:10 +01002249 ret = brcmf_sdio_txpkt_prep(bus, pktq, chan);
Franky Linb05e9252013-08-10 12:27:26 +02002250 if (ret)
2251 goto done;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002252
Franky Lin38b0b0d2012-11-05 16:22:24 -08002253 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriela7cdd822013-12-12 11:58:59 +01002254 ret = brcmf_sdiod_send_pkt(bus->sdiodev, pktq);
Arend van Spriel80969832012-06-09 22:51:44 +02002255 bus->sdcnt.f2txdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002256
Hante Meuleman81c78832014-02-25 20:30:35 +01002257 if (ret < 0)
2258 brcmf_sdio_txfail(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002259
Franky Lin38b0b0d2012-11-05 16:22:24 -08002260 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002261
2262done:
Franky Lin8da9d2c2013-11-29 12:25:10 +01002263 brcmf_sdio_txpkt_postp(bus, pktq);
2264 if (ret == 0)
2265 bus->tx_seq = (bus->tx_seq + pktq->qlen) % SDPCM_SEQ_WRAP;
2266 skb_queue_walk_safe(pktq, pkt_next, tmp) {
2267 __skb_unlink(pkt_next, pktq);
2268 brcmf_txcomplete(bus->sdiodev->dev, pkt_next, ret == 0);
2269 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002270 return ret;
2271}
2272
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002273static uint brcmf_sdio_sendfromq(struct brcmf_sdio *bus, uint maxframes)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002274{
2275 struct sk_buff *pkt;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002276 struct sk_buff_head pktq;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002277 u32 intstatus = 0;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002278 int ret = 0, prec_out, i;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002279 uint cnt = 0;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002280 u8 tx_prec_map, pkt_num;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002281
Arend van Spriel5b435de2011-10-05 13:19:03 +02002282 brcmf_dbg(TRACE, "Enter\n");
2283
2284 tx_prec_map = ~bus->flowcontrol;
2285
2286 /* Send frames until the limit or some other event */
Franky Lin8da9d2c2013-11-29 12:25:10 +01002287 for (cnt = 0; (cnt < maxframes) && data_ok(bus);) {
2288 pkt_num = 1;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002289 if (bus->txglom)
2290 pkt_num = min_t(u8, bus->tx_max - bus->tx_seq,
Arend van Sprielaf1fa212014-02-27 19:25:00 +01002291 bus->sdiodev->txglomsz);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002292 pkt_num = min_t(u32, pkt_num,
2293 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol));
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002294 __skb_queue_head_init(&pktq);
2295 spin_lock_bh(&bus->txq_lock);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002296 for (i = 0; i < pkt_num; i++) {
2297 pkt = brcmu_pktq_mdeq(&bus->txq, tx_prec_map,
2298 &prec_out);
2299 if (pkt == NULL)
2300 break;
2301 __skb_queue_tail(&pktq, pkt);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002302 }
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002303 spin_unlock_bh(&bus->txq_lock);
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002304 if (i == 0)
Franky Lin8da9d2c2013-11-29 12:25:10 +01002305 break;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002306
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002307 ret = brcmf_sdio_txpkt(bus, &pktq, SDPCM_DATA_CHANNEL);
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002308
Franky Lin8da9d2c2013-11-29 12:25:10 +01002309 cnt += i;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002310
2311 /* In poll mode, need to check for other events */
Hante Meulemanb6a8cf22014-02-25 20:30:33 +01002312 if (!bus->intr) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02002313 /* Check device status, signal pending interrupt */
Franky Lin38b0b0d2012-11-05 16:22:24 -08002314 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin5c15c232012-05-04 18:27:37 -07002315 ret = r_sdreg32(bus, &intstatus,
2316 offsetof(struct sdpcmd_regs,
2317 intstatus));
Franky Lin38b0b0d2012-11-05 16:22:24 -08002318 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel80969832012-06-09 22:51:44 +02002319 bus->sdcnt.f2txdata++;
Franky Lin5c15c232012-05-04 18:27:37 -07002320 if (ret != 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002321 break;
2322 if (intstatus & bus->hostintmask)
Franky Lin1d382272012-09-13 21:11:59 +02002323 atomic_set(&bus->ipend, 1);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002324 }
2325 }
2326
2327 /* Deflow-control stack if needed */
Arend van Spriela1ce7a02015-02-06 18:36:42 +01002328 if ((bus->sdiodev->state == BRCMF_SDIOD_DATA) &&
Franky Linc8bf3482011-12-16 18:37:08 -08002329 bus->txoff && (pktq_len(&bus->txq) < TXLOW)) {
Hante Meuleman90d03ff2012-09-11 21:18:46 +02002330 bus->txoff = false;
2331 brcmf_txflowblock(bus->sdiodev->dev, false);
Franky Linc8bf3482011-12-16 18:37:08 -08002332 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002333
2334 return cnt;
2335}
2336
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002337static int brcmf_sdio_tx_ctrlframe(struct brcmf_sdio *bus, u8 *frame, u16 len)
2338{
2339 u8 doff;
2340 u16 pad;
2341 uint retries = 0;
2342 struct brcmf_sdio_hdrinfo hd_info = {0};
2343 int ret;
2344
2345 brcmf_dbg(TRACE, "Enter\n");
2346
2347 /* Back the pointer to make room for bus header */
2348 frame -= bus->tx_hdrlen;
2349 len += bus->tx_hdrlen;
2350
2351 /* Add alignment padding (optional for ctl frames) */
2352 doff = ((unsigned long)frame % bus->head_align);
2353 if (doff) {
2354 frame -= doff;
2355 len += doff;
2356 memset(frame + bus->tx_hdrlen, 0, doff);
2357 }
2358
2359 /* Round send length to next SDIO block */
2360 pad = 0;
2361 if (bus->roundup && bus->blocksize && (len > bus->blocksize)) {
2362 pad = bus->blocksize - (len % bus->blocksize);
2363 if ((pad > bus->roundup) || (pad >= bus->blocksize))
2364 pad = 0;
2365 } else if (len % bus->head_align) {
2366 pad = bus->head_align - (len % bus->head_align);
2367 }
2368 len += pad;
2369
2370 hd_info.len = len - pad;
2371 hd_info.channel = SDPCM_CONTROL_CHANNEL;
2372 hd_info.dat_offset = doff + bus->tx_hdrlen;
2373 hd_info.seq_num = bus->tx_seq;
2374 hd_info.lastfrm = true;
2375 hd_info.tail_pad = pad;
2376 brcmf_sdio_hdpack(bus, frame, &hd_info);
2377
2378 if (bus->txglom)
2379 brcmf_sdio_update_hwhdr(frame, len);
2380
2381 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_CTL_ON(),
2382 frame, len, "Tx Frame:\n");
2383 brcmf_dbg_hex_dump(!(BRCMF_BYTES_ON() && BRCMF_CTL_ON()) &&
2384 BRCMF_HDRS_ON(),
2385 frame, min_t(u16, len, 16), "TxHdr:\n");
2386
2387 do {
2388 ret = brcmf_sdiod_send_buf(bus->sdiodev, frame, len);
2389
2390 if (ret < 0)
2391 brcmf_sdio_txfail(bus);
2392 else
2393 bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQ_WRAP;
2394 } while (ret < 0 && retries++ < TXRETRIES);
2395
2396 return ret;
2397}
2398
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002399static void brcmf_sdio_bus_stop(struct device *dev)
Franky Lina9ffda82011-12-16 18:37:12 -08002400{
2401 u32 local_hostintmask;
2402 u8 saveclk;
Franky Lina9ffda82011-12-16 18:37:12 -08002403 int err;
2404 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01002405 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Lina9ffda82011-12-16 18:37:12 -08002406 struct brcmf_sdio *bus = sdiodev->bus;
2407
2408 brcmf_dbg(TRACE, "Enter\n");
2409
2410 if (bus->watchdog_tsk) {
2411 send_sig(SIGTERM, bus->watchdog_tsk, 1);
2412 kthread_stop(bus->watchdog_tsk);
2413 bus->watchdog_tsk = NULL;
2414 }
2415
Arend van Spriela1ce7a02015-02-06 18:36:42 +01002416 if (sdiodev->state != BRCMF_SDIOD_NOMEDIUM) {
Arend van Sprielbb350712014-01-13 22:20:29 +01002417 sdio_claim_host(sdiodev->func[1]);
Franky Lina9ffda82011-12-16 18:37:12 -08002418
Arend van Sprielbb350712014-01-13 22:20:29 +01002419 /* Enable clock for device interrupts */
2420 brcmf_sdio_bus_sleep(bus, false, false);
Franky Lina9ffda82011-12-16 18:37:12 -08002421
Arend van Sprielbb350712014-01-13 22:20:29 +01002422 /* Disable and clear interrupts at the chip level also */
2423 w_sdreg32(bus, 0, offsetof(struct sdpcmd_regs, hostintmask));
2424 local_hostintmask = bus->hostintmask;
2425 bus->hostintmask = 0;
Franky Lina9ffda82011-12-16 18:37:12 -08002426
Arend van Sprielbb350712014-01-13 22:20:29 +01002427 /* Force backplane clocks to assure F2 interrupt propagates */
2428 saveclk = brcmf_sdiod_regrb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
2429 &err);
2430 if (!err)
2431 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
2432 (saveclk | SBSDIO_FORCE_HT), &err);
2433 if (err)
2434 brcmf_err("Failed to force clock for F2: err %d\n",
2435 err);
Franky Lina9ffda82011-12-16 18:37:12 -08002436
Arend van Sprielbb350712014-01-13 22:20:29 +01002437 /* Turn off the bus (F2), free any pending packets */
2438 brcmf_dbg(INTR, "disable SDIO interrupts\n");
2439 sdio_disable_func(sdiodev->func[SDIO_FUNC_2]);
2440
2441 /* Clear any pending interrupts now that F2 is disabled */
2442 w_sdreg32(bus, local_hostintmask,
2443 offsetof(struct sdpcmd_regs, intstatus));
2444
2445 sdio_release_host(sdiodev->func[1]);
Franky Lina9ffda82011-12-16 18:37:12 -08002446 }
Franky Lina9ffda82011-12-16 18:37:12 -08002447 /* Clear the data packet queues */
2448 brcmu_pktq_flush(&bus->txq, true, NULL, NULL);
2449
2450 /* Clear any held glomming stuff */
Markus Elfring297540f2015-02-04 20:28:49 +01002451 brcmu_pkt_buf_free_skb(bus->glomd);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002452 brcmf_sdio_free_glom(bus);
Franky Lina9ffda82011-12-16 18:37:12 -08002453
2454 /* Clear rx control and wake any waiters */
Franky Lindd43a012012-11-05 16:22:22 -08002455 spin_lock_bh(&bus->rxctl_lock);
Franky Lina9ffda82011-12-16 18:37:12 -08002456 bus->rxlen = 0;
Franky Lindd43a012012-11-05 16:22:22 -08002457 spin_unlock_bh(&bus->rxctl_lock);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002458 brcmf_sdio_dcmd_resp_wake(bus);
Franky Lina9ffda82011-12-16 18:37:12 -08002459
2460 /* Reset some F2 state stuff */
2461 bus->rxskip = false;
2462 bus->tx_seq = bus->rx_seq = 0;
Franky Lina9ffda82011-12-16 18:37:12 -08002463}
2464
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002465static inline void brcmf_sdio_clrintr(struct brcmf_sdio *bus)
Franky Linba89bf12012-04-27 18:56:59 -07002466{
Hante Meulemanaf5b5e62016-02-17 11:27:08 +01002467 struct brcmf_sdio_dev *sdiodev;
Franky Linba89bf12012-04-27 18:56:59 -07002468 unsigned long flags;
2469
Hante Meulemanaf5b5e62016-02-17 11:27:08 +01002470 sdiodev = bus->sdiodev;
2471 if (sdiodev->oob_irq_requested) {
2472 spin_lock_irqsave(&sdiodev->irq_en_lock, flags);
2473 if (!sdiodev->irq_en && !atomic_read(&bus->ipend)) {
2474 enable_irq(sdiodev->settings->bus.sdio.oob_irq_nr);
2475 sdiodev->irq_en = true;
Hante Meuleman668761a2013-04-12 10:55:55 +02002476 }
Hante Meulemanaf5b5e62016-02-17 11:27:08 +01002477 spin_unlock_irqrestore(&sdiodev->irq_en_lock, flags);
Franky Linba89bf12012-04-27 18:56:59 -07002478 }
Franky Linba89bf12012-04-27 18:56:59 -07002479}
Franky Linba89bf12012-04-27 18:56:59 -07002480
Franky Lin45316032012-09-13 21:12:03 +02002481static int brcmf_sdio_intr_rstatus(struct brcmf_sdio *bus)
2482{
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01002483 struct brcmf_core *buscore;
Franky Lin45316032012-09-13 21:12:03 +02002484 u32 addr;
2485 unsigned long val;
Hante Meuleman5cbb9c22014-03-06 10:16:12 +01002486 int ret;
Franky Lin45316032012-09-13 21:12:03 +02002487
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01002488 buscore = brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV);
2489 addr = buscore->base + offsetof(struct sdpcmd_regs, intstatus);
Franky Lin45316032012-09-13 21:12:03 +02002490
Arend van Spriela39be272013-12-12 11:58:58 +01002491 val = brcmf_sdiod_regrl(bus->sdiodev, addr, &ret);
Franky Lin45316032012-09-13 21:12:03 +02002492 bus->sdcnt.f1regdata++;
2493 if (ret != 0)
Hante Meuleman5cbb9c22014-03-06 10:16:12 +01002494 return ret;
Franky Lin45316032012-09-13 21:12:03 +02002495
2496 val &= bus->hostintmask;
2497 atomic_set(&bus->fcstate, !!(val & I_HMB_FC_STATE));
2498
2499 /* Clear interrupts */
2500 if (val) {
Arend van Spriela39be272013-12-12 11:58:58 +01002501 brcmf_sdiod_regwl(bus->sdiodev, addr, val, &ret);
Franky Lin45316032012-09-13 21:12:03 +02002502 bus->sdcnt.f1regdata++;
Vineet Guptad3928d02015-07-09 13:43:18 +05302503 atomic_or(val, &bus->intstatus);
Franky Lin45316032012-09-13 21:12:03 +02002504 }
2505
2506 return ret;
2507}
2508
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002509static void brcmf_sdio_dpc(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002510{
Franky Lin45316032012-09-13 21:12:03 +02002511 u32 newstatus = 0;
2512 unsigned long intstatus;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002513 uint txlimit = bus->txbound; /* Tx frames to send before resched */
Hante Meulemanb6a8cf22014-02-25 20:30:33 +01002514 uint framecnt; /* Temporary counter of tx/rx frames */
Hante Meuleman5cbb9c22014-03-06 10:16:12 +01002515 int err = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002516
2517 brcmf_dbg(TRACE, "Enter\n");
2518
Franky Lin38b0b0d2012-11-05 16:22:24 -08002519 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002520
2521 /* If waiting for HTAVAIL, check status */
Piotr Haber4a3da992013-04-11 13:28:46 +02002522 if (!bus->sr_enabled && bus->clkstate == CLK_PENDING) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02002523 u8 clkctl, devctl = 0;
2524
Joe Perches8ae74652012-01-15 00:38:38 -08002525#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02002526 /* Check for inconsistent device control */
Arend van Spriela39be272013-12-12 11:58:58 +01002527 devctl = brcmf_sdiod_regrb(bus->sdiodev,
2528 SBSDIO_DEVICE_CTL, &err);
Joe Perches8ae74652012-01-15 00:38:38 -08002529#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02002530
2531 /* Read CSR, if clock on switch to AVAIL, else ignore */
Arend van Spriela39be272013-12-12 11:58:58 +01002532 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
2533 SBSDIO_FUNC1_CHIPCLKCSR, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002534
Arend van Sprielc3203372013-04-03 12:40:44 +02002535 brcmf_dbg(SDIO, "DPC: PENDING, devctl 0x%02x clkctl 0x%02x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02002536 devctl, clkctl);
2537
2538 if (SBSDIO_HTAV(clkctl)) {
Arend van Spriela39be272013-12-12 11:58:58 +01002539 devctl = brcmf_sdiod_regrb(bus->sdiodev,
2540 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002541 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
Arend van Spriela39be272013-12-12 11:58:58 +01002542 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
2543 devctl, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002544 bus->clkstate = CLK_AVAIL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002545 }
2546 }
2547
Arend van Spriel5b435de2011-10-05 13:19:03 +02002548 /* Make sure backplane clock is on */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002549 brcmf_sdio_bus_sleep(bus, false, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002550
2551 /* Pending interrupt indicates new device status */
Franky Lin1d382272012-09-13 21:11:59 +02002552 if (atomic_read(&bus->ipend) > 0) {
2553 atomic_set(&bus->ipend, 0);
Franky Lin45316032012-09-13 21:12:03 +02002554 err = brcmf_sdio_intr_rstatus(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002555 }
2556
Franky Lin45316032012-09-13 21:12:03 +02002557 /* Start with leftover status bits */
2558 intstatus = atomic_xchg(&bus->intstatus, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002559
2560 /* Handle flow-control change: read new state in case our ack
2561 * crossed another change interrupt. If change still set, assume
2562 * FC ON for safety, let next loop through do the debounce.
2563 */
2564 if (intstatus & I_HMB_FC_CHANGE) {
2565 intstatus &= ~I_HMB_FC_CHANGE;
Franky Lin5c15c232012-05-04 18:27:37 -07002566 err = w_sdreg32(bus, I_HMB_FC_CHANGE,
2567 offsetof(struct sdpcmd_regs, intstatus));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002568
Franky Lin5c15c232012-05-04 18:27:37 -07002569 err = r_sdreg32(bus, &newstatus,
2570 offsetof(struct sdpcmd_regs, intstatus));
Arend van Spriel80969832012-06-09 22:51:44 +02002571 bus->sdcnt.f1regdata += 2;
Franky Lin45316032012-09-13 21:12:03 +02002572 atomic_set(&bus->fcstate,
2573 !!(newstatus & (I_HMB_FC_STATE | I_HMB_FC_CHANGE)));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002574 intstatus |= (newstatus & bus->hostintmask);
2575 }
2576
2577 /* Handle host mailbox indication */
2578 if (intstatus & I_HMB_HOST_INT) {
2579 intstatus &= ~I_HMB_HOST_INT;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002580 intstatus |= brcmf_sdio_hostmail(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002581 }
2582
Franky Lin38b0b0d2012-11-05 16:22:24 -08002583 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin7cdf57d2012-11-05 16:22:23 -08002584
Arend van Spriel5b435de2011-10-05 13:19:03 +02002585 /* Generally don't ask for these, can get CRC errors... */
2586 if (intstatus & I_WR_OOSYNC) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002587 brcmf_err("Dongle reports WR_OOSYNC\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002588 intstatus &= ~I_WR_OOSYNC;
2589 }
2590
2591 if (intstatus & I_RD_OOSYNC) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002592 brcmf_err("Dongle reports RD_OOSYNC\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002593 intstatus &= ~I_RD_OOSYNC;
2594 }
2595
2596 if (intstatus & I_SBINT) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002597 brcmf_err("Dongle reports SBINT\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002598 intstatus &= ~I_SBINT;
2599 }
2600
2601 /* Would be active due to wake-wlan in gSPI */
2602 if (intstatus & I_CHIPACTIVE) {
2603 brcmf_dbg(INFO, "Dongle reports CHIPACTIVE\n");
2604 intstatus &= ~I_CHIPACTIVE;
2605 }
2606
2607 /* Ignore frame indications if rxskip is set */
2608 if (bus->rxskip)
2609 intstatus &= ~I_HMB_FRAME_IND;
2610
2611 /* On frame indication, read available frames */
Hante Meulemanb6a8cf22014-02-25 20:30:33 +01002612 if ((intstatus & I_HMB_FRAME_IND) && (bus->clkstate == CLK_AVAIL)) {
2613 brcmf_sdio_readframes(bus, bus->rxbound);
Franky Lin4754fce2012-09-19 22:21:11 +02002614 if (!bus->rxpending)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002615 intstatus &= ~I_HMB_FRAME_IND;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002616 }
2617
2618 /* Keep still-pending events for next scheduling */
Hante Meuleman5cbb9c22014-03-06 10:16:12 +01002619 if (intstatus)
Vineet Guptad3928d02015-07-09 13:43:18 +05302620 atomic_or(intstatus, &bus->intstatus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002621
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002622 brcmf_sdio_clrintr(bus);
Franky Linba89bf12012-04-27 18:56:59 -07002623
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002624 if (bus->ctrl_frame_stat && (bus->clkstate == CLK_AVAIL) &&
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002625 data_ok(bus)) {
2626 sdio_claim_host(bus->sdiodev->func[1]);
Hante Meuleman449e58b2015-03-06 18:40:40 +01002627 if (bus->ctrl_frame_stat) {
2628 err = brcmf_sdio_tx_ctrlframe(bus, bus->ctrl_frame_buf,
2629 bus->ctrl_frame_len);
2630 bus->ctrl_frame_err = err;
Hante Meuleman2c64e162015-03-18 13:25:22 +01002631 wmb();
Hante Meuleman449e58b2015-03-06 18:40:40 +01002632 bus->ctrl_frame_stat = false;
2633 }
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002634 sdio_release_host(bus->sdiodev->func[1]);
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002635 brcmf_sdio_wait_event_wakeup(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002636 }
2637 /* Send queued frames (limit 1 if rx may still be pending) */
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002638 if ((bus->clkstate == CLK_AVAIL) && !atomic_read(&bus->fcstate) &&
2639 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) && txlimit &&
2640 data_ok(bus)) {
Franky Lin4754fce2012-09-19 22:21:11 +02002641 framecnt = bus->rxpending ? min(txlimit, bus->txminmax) :
2642 txlimit;
Hante Meulemanb6a8cf22014-02-25 20:30:33 +01002643 brcmf_sdio_sendfromq(bus, framecnt);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002644 }
2645
Arend van Spriela1ce7a02015-02-06 18:36:42 +01002646 if ((bus->sdiodev->state != BRCMF_SDIOD_DATA) || (err != 0)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002647 brcmf_err("failed backplane access over SDIO, halting operation\n");
Franky Lin45316032012-09-13 21:12:03 +02002648 atomic_set(&bus->intstatus, 0);
Hante Meulemande6878c2015-03-06 18:40:38 +01002649 if (bus->ctrl_frame_stat) {
Hante Meuleman449e58b2015-03-06 18:40:40 +01002650 sdio_claim_host(bus->sdiodev->func[1]);
2651 if (bus->ctrl_frame_stat) {
2652 bus->ctrl_frame_err = -ENODEV;
Hante Meuleman2c64e162015-03-18 13:25:22 +01002653 wmb();
Hante Meuleman449e58b2015-03-06 18:40:40 +01002654 bus->ctrl_frame_stat = false;
2655 brcmf_sdio_wait_event_wakeup(bus);
2656 }
2657 sdio_release_host(bus->sdiodev->func[1]);
Hante Meulemande6878c2015-03-06 18:40:38 +01002658 }
Franky Lin45316032012-09-13 21:12:03 +02002659 } else if (atomic_read(&bus->intstatus) ||
2660 atomic_read(&bus->ipend) > 0 ||
2661 (!atomic_read(&bus->fcstate) &&
2662 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) &&
Hante Meulemanb6a8cf22014-02-25 20:30:33 +01002663 data_ok(bus))) {
Hante Meuleman2c64e162015-03-18 13:25:22 +01002664 bus->dpc_triggered = true;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002665 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002666}
2667
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002668static struct pktq *brcmf_sdio_bus_gettxq(struct device *dev)
Arend van Spriele2432b62013-04-03 12:40:38 +02002669{
2670 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
2671 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
2672 struct brcmf_sdio *bus = sdiodev->bus;
2673
2674 return &bus->txq;
2675}
2676
Hante Meuleman84936622014-10-28 14:56:19 +01002677static bool brcmf_sdio_prec_enq(struct pktq *q, struct sk_buff *pkt, int prec)
2678{
2679 struct sk_buff *p;
2680 int eprec = -1; /* precedence to evict from */
2681
2682 /* Fast case, precedence queue is not full and we are also not
2683 * exceeding total queue length
2684 */
2685 if (!pktq_pfull(q, prec) && !pktq_full(q)) {
2686 brcmu_pktq_penq(q, prec, pkt);
2687 return true;
2688 }
2689
2690 /* Determine precedence from which to evict packet, if any */
2691 if (pktq_pfull(q, prec)) {
2692 eprec = prec;
2693 } else if (pktq_full(q)) {
2694 p = brcmu_pktq_peek_tail(q, &eprec);
2695 if (eprec > prec)
2696 return false;
2697 }
2698
2699 /* Evict if needed */
2700 if (eprec >= 0) {
2701 /* Detect queueing to unconfigured precedence */
2702 if (eprec == prec)
2703 return false; /* refuse newer (incoming) packet */
2704 /* Evict packet according to discard policy */
2705 p = brcmu_pktq_pdeq_tail(q, eprec);
2706 if (p == NULL)
2707 brcmf_err("brcmu_pktq_pdeq_tail() failed\n");
2708 brcmu_pkt_buf_free_skb(p);
2709 }
2710
2711 /* Enqueue */
2712 p = brcmu_pktq_penq(q, prec, pkt);
2713 if (p == NULL)
2714 brcmf_err("brcmu_pktq_penq() failed\n");
2715
2716 return p != NULL;
2717}
2718
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002719static int brcmf_sdio_bus_txdata(struct device *dev, struct sk_buff *pkt)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002720{
2721 int ret = -EBADE;
Arend van Spriel44ff5662014-02-25 20:30:28 +01002722 uint prec;
Franky Linbf347bb2011-11-22 17:21:56 -08002723 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01002724 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Linbf347bb2011-11-22 17:21:56 -08002725 struct brcmf_sdio *bus = sdiodev->bus;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002726
Arend van Spriel44ff5662014-02-25 20:30:28 +01002727 brcmf_dbg(TRACE, "Enter: pkt: data %p len %d\n", pkt->data, pkt->len);
Arend van Spriel5768f312015-06-11 00:12:20 +02002728 if (sdiodev->state != BRCMF_SDIOD_DATA)
2729 return -EIO;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002730
2731 /* Add space for the header */
Franky Lin706478c2013-08-10 12:27:29 +02002732 skb_push(pkt, bus->tx_hdrlen);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002733 /* precondition: IS_ALIGNED((unsigned long)(pkt->data), 2) */
2734
2735 prec = prio2prec((pkt->priority & PRIOMASK));
2736
2737 /* Check for existing queue, current flow-control,
2738 pending event, or pending clock */
2739 brcmf_dbg(TRACE, "deferring pktq len %d\n", pktq_len(&bus->txq));
Arend van Spriel80969832012-06-09 22:51:44 +02002740 bus->sdcnt.fcqueued++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002741
2742 /* Priority based enq */
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002743 spin_lock_bh(&bus->txq_lock);
Arend van Spriel5aa9f0e2014-02-25 20:30:27 +01002744 /* reset bus_flags in packet cb */
2745 *(u16 *)(pkt->cb) = 0;
Hante Meuleman84936622014-10-28 14:56:19 +01002746 if (!brcmf_sdio_prec_enq(&bus->txq, pkt, prec)) {
Franky Lin706478c2013-08-10 12:27:29 +02002747 skb_pull(pkt, bus->tx_hdrlen);
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002748 brcmf_err("out of bus->txq !!!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002749 ret = -ENOSR;
2750 } else {
2751 ret = 0;
2752 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002753
Franky Linc8bf3482011-12-16 18:37:08 -08002754 if (pktq_len(&bus->txq) >= TXHI) {
Hante Meuleman90d03ff2012-09-11 21:18:46 +02002755 bus->txoff = true;
Hante Meuleman84936622014-10-28 14:56:19 +01002756 brcmf_txflowblock(dev, true);
Franky Linc8bf3482011-12-16 18:37:08 -08002757 }
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002758 spin_unlock_bh(&bus->txq_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002759
Joe Perches8ae74652012-01-15 00:38:38 -08002760#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02002761 if (pktq_plen(&bus->txq, prec) > qcount[prec])
2762 qcount[prec] = pktq_plen(&bus->txq, prec);
2763#endif
Franky Linf1e68c22012-09-13 21:12:00 +02002764
Arend van Spriel99824642015-02-06 18:36:45 +01002765 brcmf_sdio_trigger_dpc(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002766 return ret;
2767}
2768
Joe Perches8ae74652012-01-15 00:38:38 -08002769#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02002770#define CONSOLE_LINE_MAX 192
2771
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002772static int brcmf_sdio_readconsole(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002773{
2774 struct brcmf_console *c = &bus->console;
2775 u8 line[CONSOLE_LINE_MAX], ch;
2776 u32 n, idx, addr;
2777 int rv;
2778
2779 /* Don't do anything until FWREADY updates console address */
2780 if (bus->console_addr == 0)
2781 return 0;
2782
2783 /* Read console log struct */
2784 addr = bus->console_addr + offsetof(struct rte_console, log_le);
Arend van Spriela39be272013-12-12 11:58:58 +01002785 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, (u8 *)&c->log_le,
2786 sizeof(c->log_le));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002787 if (rv < 0)
2788 return rv;
2789
2790 /* Allocate console buffer (one time only) */
2791 if (c->buf == NULL) {
2792 c->bufsize = le32_to_cpu(c->log_le.buf_size);
2793 c->buf = kmalloc(c->bufsize, GFP_ATOMIC);
2794 if (c->buf == NULL)
2795 return -ENOMEM;
2796 }
2797
2798 idx = le32_to_cpu(c->log_le.idx);
2799
2800 /* Protect against corrupt value */
2801 if (idx > c->bufsize)
2802 return -EBADE;
2803
2804 /* Skip reading the console buffer if the index pointer
2805 has not moved */
2806 if (idx == c->last)
2807 return 0;
2808
2809 /* Read the console buffer */
2810 addr = le32_to_cpu(c->log_le.buf);
Arend van Spriela39be272013-12-12 11:58:58 +01002811 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, c->buf, c->bufsize);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002812 if (rv < 0)
2813 return rv;
2814
2815 while (c->last != idx) {
2816 for (n = 0; n < CONSOLE_LINE_MAX - 2; n++) {
2817 if (c->last == idx) {
2818 /* This would output a partial line.
2819 * Instead, back up
2820 * the buffer pointer and output this
2821 * line next time around.
2822 */
2823 if (c->last >= n)
2824 c->last -= n;
2825 else
2826 c->last = c->bufsize - n;
2827 goto break2;
2828 }
2829 ch = c->buf[c->last];
2830 c->last = (c->last + 1) % c->bufsize;
2831 if (ch == '\n')
2832 break;
2833 line[n] = ch;
2834 }
2835
2836 if (n > 0) {
2837 if (line[n - 1] == '\r')
2838 n--;
2839 line[n] = 0;
Joe Perches18aad4f2012-01-15 00:38:42 -08002840 pr_debug("CONSOLE: %s\n", line);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002841 }
2842 }
2843break2:
2844
2845 return 0;
2846}
Joe Perches8ae74652012-01-15 00:38:38 -08002847#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02002848
Franky Linfcf094f2011-12-16 18:37:16 -08002849static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002850brcmf_sdio_bus_txctl(struct device *dev, unsigned char *msg, uint msglen)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002851{
Franky Lin47a1ce72011-11-22 17:21:55 -08002852 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01002853 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Lin47a1ce72011-11-22 17:21:55 -08002854 struct brcmf_sdio *bus = sdiodev->bus;
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002855 int ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002856
2857 brcmf_dbg(TRACE, "Enter\n");
Arend van Spriel5768f312015-06-11 00:12:20 +02002858 if (sdiodev->state != BRCMF_SDIOD_DATA)
2859 return -EIO;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002860
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002861 /* Send from dpc */
2862 bus->ctrl_frame_buf = msg;
2863 bus->ctrl_frame_len = msglen;
Hante Meuleman2c64e162015-03-18 13:25:22 +01002864 wmb();
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002865 bus->ctrl_frame_stat = true;
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002866
Arend van Spriel99824642015-02-06 18:36:45 +01002867 brcmf_sdio_trigger_dpc(bus);
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002868 wait_event_interruptible_timeout(bus->ctrl_wait, !bus->ctrl_frame_stat,
Arend van Spriel63ce3d52016-01-05 11:05:48 +01002869 CTL_DONE_TIMEOUT);
Hante Meuleman449e58b2015-03-06 18:40:40 +01002870 ret = 0;
2871 if (bus->ctrl_frame_stat) {
2872 sdio_claim_host(bus->sdiodev->func[1]);
2873 if (bus->ctrl_frame_stat) {
2874 brcmf_dbg(SDIO, "ctrl_frame timeout\n");
2875 bus->ctrl_frame_stat = false;
2876 ret = -ETIMEDOUT;
2877 }
2878 sdio_release_host(bus->sdiodev->func[1]);
2879 }
2880 if (!ret) {
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002881 brcmf_dbg(SDIO, "ctrl_frame complete, err=%d\n",
2882 bus->ctrl_frame_err);
Hante Meuleman2c64e162015-03-18 13:25:22 +01002883 rmb();
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002884 ret = bus->ctrl_frame_err;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002885 }
2886
Arend van Spriel5b435de2011-10-05 13:19:03 +02002887 if (ret)
Arend van Spriel80969832012-06-09 22:51:44 +02002888 bus->sdcnt.tx_ctlerrs++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002889 else
Arend van Spriel80969832012-06-09 22:51:44 +02002890 bus->sdcnt.tx_ctlpkts++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002891
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002892 return ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002893}
2894
Arend van Spriel80969832012-06-09 22:51:44 +02002895#ifdef DEBUG
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002896static int brcmf_sdio_dump_console(struct seq_file *seq, struct brcmf_sdio *bus,
2897 struct sdpcm_shared *sh)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002898{
2899 u32 addr, console_ptr, console_size, console_index;
2900 char *conbuf = NULL;
2901 __le32 sh_val;
2902 int rv;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002903
2904 /* obtain console information from device memory */
2905 addr = sh->console_addr + offsetof(struct rte_console, log_le);
Arend van Spriela39be272013-12-12 11:58:58 +01002906 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
2907 (u8 *)&sh_val, sizeof(u32));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002908 if (rv < 0)
2909 return rv;
2910 console_ptr = le32_to_cpu(sh_val);
2911
2912 addr = sh->console_addr + offsetof(struct rte_console, log_le.buf_size);
Arend van Spriela39be272013-12-12 11:58:58 +01002913 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
2914 (u8 *)&sh_val, sizeof(u32));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002915 if (rv < 0)
2916 return rv;
2917 console_size = le32_to_cpu(sh_val);
2918
2919 addr = sh->console_addr + offsetof(struct rte_console, log_le.idx);
Arend van Spriela39be272013-12-12 11:58:58 +01002920 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
2921 (u8 *)&sh_val, sizeof(u32));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002922 if (rv < 0)
2923 return rv;
2924 console_index = le32_to_cpu(sh_val);
2925
2926 /* allocate buffer for console data */
2927 if (console_size <= CONSOLE_BUFFER_MAX)
2928 conbuf = vzalloc(console_size+1);
2929
2930 if (!conbuf)
2931 return -ENOMEM;
2932
2933 /* obtain the console data from device */
2934 conbuf[console_size] = '\0';
Arend van Spriela39be272013-12-12 11:58:58 +01002935 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, console_ptr, (u8 *)conbuf,
2936 console_size);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002937 if (rv < 0)
2938 goto done;
2939
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002940 rv = seq_write(seq, conbuf + console_index,
2941 console_size - console_index);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002942 if (rv < 0)
2943 goto done;
2944
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002945 if (console_index > 0)
2946 rv = seq_write(seq, conbuf, console_index - 1);
2947
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002948done:
2949 vfree(conbuf);
2950 return rv;
2951}
2952
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002953static int brcmf_sdio_trap_info(struct seq_file *seq, struct brcmf_sdio *bus,
2954 struct sdpcm_shared *sh)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002955{
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002956 int error;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002957 struct brcmf_trap_info tr;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002958
Piotr Haberbaa9e602013-04-05 10:57:42 +02002959 if ((sh->flags & SDPCM_SHARED_TRAP) == 0) {
2960 brcmf_dbg(INFO, "no trap in firmware\n");
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002961 return 0;
Piotr Haberbaa9e602013-04-05 10:57:42 +02002962 }
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002963
Arend van Spriela39be272013-12-12 11:58:58 +01002964 error = brcmf_sdiod_ramrw(bus->sdiodev, false, sh->trap_addr, (u8 *)&tr,
2965 sizeof(struct brcmf_trap_info));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002966 if (error < 0)
2967 return error;
2968
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002969 seq_printf(seq,
2970 "dongle trap info: type 0x%x @ epc 0x%08x\n"
2971 " cpsr 0x%08x spsr 0x%08x sp 0x%08x\n"
2972 " lr 0x%08x pc 0x%08x offset 0x%x\n"
2973 " r0 0x%08x r1 0x%08x r2 0x%08x r3 0x%08x\n"
2974 " r4 0x%08x r5 0x%08x r6 0x%08x r7 0x%08x\n",
2975 le32_to_cpu(tr.type), le32_to_cpu(tr.epc),
2976 le32_to_cpu(tr.cpsr), le32_to_cpu(tr.spsr),
2977 le32_to_cpu(tr.r13), le32_to_cpu(tr.r14),
2978 le32_to_cpu(tr.pc), sh->trap_addr,
2979 le32_to_cpu(tr.r0), le32_to_cpu(tr.r1),
2980 le32_to_cpu(tr.r2), le32_to_cpu(tr.r3),
2981 le32_to_cpu(tr.r4), le32_to_cpu(tr.r5),
2982 le32_to_cpu(tr.r6), le32_to_cpu(tr.r7));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002983
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002984 return 0;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002985}
2986
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002987static int brcmf_sdio_assert_info(struct seq_file *seq, struct brcmf_sdio *bus,
2988 struct sdpcm_shared *sh)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002989{
2990 int error = 0;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002991 char file[80] = "?";
2992 char expr[80] = "<???>";
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002993
2994 if ((sh->flags & SDPCM_SHARED_ASSERT_BUILT) == 0) {
2995 brcmf_dbg(INFO, "firmware not built with -assert\n");
2996 return 0;
2997 } else if ((sh->flags & SDPCM_SHARED_ASSERT) == 0) {
2998 brcmf_dbg(INFO, "no assert in dongle\n");
2999 return 0;
3000 }
3001
Franky Lin38b0b0d2012-11-05 16:22:24 -08003002 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003003 if (sh->assert_file_addr != 0) {
Arend van Spriela39be272013-12-12 11:58:58 +01003004 error = brcmf_sdiod_ramrw(bus->sdiodev, false,
3005 sh->assert_file_addr, (u8 *)file, 80);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003006 if (error < 0)
3007 return error;
3008 }
3009 if (sh->assert_exp_addr != 0) {
Arend van Spriela39be272013-12-12 11:58:58 +01003010 error = brcmf_sdiod_ramrw(bus->sdiodev, false,
3011 sh->assert_exp_addr, (u8 *)expr, 80);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003012 if (error < 0)
3013 return error;
3014 }
Franky Lin38b0b0d2012-11-05 16:22:24 -08003015 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003016
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003017 seq_printf(seq, "dongle assert: %s:%d: assert(%s)\n",
3018 file, sh->assert_line, expr);
3019 return 0;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003020}
3021
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003022static int brcmf_sdio_checkdied(struct brcmf_sdio *bus)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003023{
3024 int error;
3025 struct sdpcm_shared sh;
3026
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003027 error = brcmf_sdio_readshared(bus, &sh);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003028
3029 if (error < 0)
3030 return error;
3031
3032 if ((sh.flags & SDPCM_SHARED_ASSERT_BUILT) == 0)
3033 brcmf_dbg(INFO, "firmware not built with -assert\n");
3034 else if (sh.flags & SDPCM_SHARED_ASSERT)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003035 brcmf_err("assertion in dongle\n");
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003036
3037 if (sh.flags & SDPCM_SHARED_TRAP)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003038 brcmf_err("firmware trap in dongle\n");
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003039
3040 return 0;
3041}
3042
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003043static int brcmf_sdio_died_dump(struct seq_file *seq, struct brcmf_sdio *bus)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003044{
3045 int error = 0;
3046 struct sdpcm_shared sh;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003047
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003048 error = brcmf_sdio_readshared(bus, &sh);
3049 if (error < 0)
3050 goto done;
3051
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003052 error = brcmf_sdio_assert_info(seq, bus, &sh);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003053 if (error < 0)
3054 goto done;
Piotr Haberbaa9e602013-04-05 10:57:42 +02003055
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003056 error = brcmf_sdio_trap_info(seq, bus, &sh);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003057 if (error < 0)
3058 goto done;
3059
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003060 error = brcmf_sdio_dump_console(seq, bus, &sh);
Piotr Haberbaa9e602013-04-05 10:57:42 +02003061
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003062done:
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003063 return error;
3064}
3065
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003066static int brcmf_sdio_forensic_read(struct seq_file *seq, void *data)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003067{
Arend van Spriel82d957e2014-07-12 08:49:36 +02003068 struct brcmf_bus *bus_if = dev_get_drvdata(seq->private);
3069 struct brcmf_sdio *bus = bus_if->bus_priv.sdio->bus;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003070
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003071 return brcmf_sdio_died_dump(seq, bus);
3072}
3073
Arend van Spriel82d957e2014-07-12 08:49:36 +02003074static int brcmf_debugfs_sdio_count_read(struct seq_file *seq, void *data)
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003075{
Arend van Spriel82d957e2014-07-12 08:49:36 +02003076 struct brcmf_bus *bus_if = dev_get_drvdata(seq->private);
3077 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3078 struct brcmf_sdio_count *sdcnt = &sdiodev->bus->sdcnt;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003079
Arend van Spriel82d957e2014-07-12 08:49:36 +02003080 seq_printf(seq,
3081 "intrcount: %u\nlastintrs: %u\n"
3082 "pollcnt: %u\nregfails: %u\n"
3083 "tx_sderrs: %u\nfcqueued: %u\n"
3084 "rxrtx: %u\nrx_toolong: %u\n"
3085 "rxc_errors: %u\nrx_hdrfail: %u\n"
3086 "rx_badhdr: %u\nrx_badseq: %u\n"
3087 "fc_rcvd: %u\nfc_xoff: %u\n"
3088 "fc_xon: %u\nrxglomfail: %u\n"
3089 "rxglomframes: %u\nrxglompkts: %u\n"
3090 "f2rxhdrs: %u\nf2rxdata: %u\n"
3091 "f2txdata: %u\nf1regdata: %u\n"
3092 "tickcnt: %u\ntx_ctlerrs: %lu\n"
3093 "tx_ctlpkts: %lu\nrx_ctlerrs: %lu\n"
3094 "rx_ctlpkts: %lu\nrx_readahead: %lu\n",
3095 sdcnt->intrcount, sdcnt->lastintrs,
3096 sdcnt->pollcnt, sdcnt->regfails,
3097 sdcnt->tx_sderrs, sdcnt->fcqueued,
3098 sdcnt->rxrtx, sdcnt->rx_toolong,
3099 sdcnt->rxc_errors, sdcnt->rx_hdrfail,
3100 sdcnt->rx_badhdr, sdcnt->rx_badseq,
3101 sdcnt->fc_rcvd, sdcnt->fc_xoff,
3102 sdcnt->fc_xon, sdcnt->rxglomfail,
3103 sdcnt->rxglomframes, sdcnt->rxglompkts,
3104 sdcnt->f2rxhdrs, sdcnt->f2rxdata,
3105 sdcnt->f2txdata, sdcnt->f1regdata,
3106 sdcnt->tickcnt, sdcnt->tx_ctlerrs,
3107 sdcnt->tx_ctlpkts, sdcnt->rx_ctlerrs,
3108 sdcnt->rx_ctlpkts, sdcnt->rx_readahead_cnt);
3109
3110 return 0;
3111}
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003112
Arend van Spriel80969832012-06-09 22:51:44 +02003113static void brcmf_sdio_debugfs_create(struct brcmf_sdio *bus)
3114{
3115 struct brcmf_pub *drvr = bus->sdiodev->bus_if->drvr;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003116 struct dentry *dentry = brcmf_debugfs_get_devdir(drvr);
Arend van Spriel80969832012-06-09 22:51:44 +02003117
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003118 if (IS_ERR_OR_NULL(dentry))
3119 return;
3120
Arend van Spriel9d6c1dc2015-08-26 22:15:01 +02003121 bus->console_interval = BRCMF_CONSOLE;
3122
Arend van Spriel82d957e2014-07-12 08:49:36 +02003123 brcmf_debugfs_add_entry(drvr, "forensics", brcmf_sdio_forensic_read);
3124 brcmf_debugfs_add_entry(drvr, "counters",
3125 brcmf_debugfs_sdio_count_read);
Daniel Kim0801e6c2014-01-29 15:32:13 +01003126 debugfs_create_u32("console_interval", 0644, dentry,
3127 &bus->console_interval);
Arend van Spriel80969832012-06-09 22:51:44 +02003128}
3129#else
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003130static int brcmf_sdio_checkdied(struct brcmf_sdio *bus)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003131{
3132 return 0;
3133}
3134
Arend van Spriel80969832012-06-09 22:51:44 +02003135static void brcmf_sdio_debugfs_create(struct brcmf_sdio *bus)
3136{
3137}
3138#endif /* DEBUG */
3139
Franky Linfcf094f2011-12-16 18:37:16 -08003140static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003141brcmf_sdio_bus_rxctl(struct device *dev, unsigned char *msg, uint msglen)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003142{
3143 int timeleft;
3144 uint rxlen = 0;
3145 bool pending;
Franky Lindd43a012012-11-05 16:22:22 -08003146 u8 *buf;
Franky Lin532cdd32011-11-22 17:21:54 -08003147 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01003148 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Lin532cdd32011-11-22 17:21:54 -08003149 struct brcmf_sdio *bus = sdiodev->bus;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003150
3151 brcmf_dbg(TRACE, "Enter\n");
Arend van Spriel5768f312015-06-11 00:12:20 +02003152 if (sdiodev->state != BRCMF_SDIOD_DATA)
3153 return -EIO;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003154
3155 /* Wait until control frame is available */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003156 timeleft = brcmf_sdio_dcmd_resp_wait(bus, &bus->rxlen, &pending);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003157
Franky Lindd43a012012-11-05 16:22:22 -08003158 spin_lock_bh(&bus->rxctl_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003159 rxlen = bus->rxlen;
3160 memcpy(msg, bus->rxctl, min(msglen, rxlen));
Franky Lindd43a012012-11-05 16:22:22 -08003161 bus->rxctl = NULL;
3162 buf = bus->rxctl_orig;
3163 bus->rxctl_orig = NULL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003164 bus->rxlen = 0;
Franky Lindd43a012012-11-05 16:22:22 -08003165 spin_unlock_bh(&bus->rxctl_lock);
3166 vfree(buf);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003167
3168 if (rxlen) {
3169 brcmf_dbg(CTL, "resumed on rxctl frame, got %d expected %d\n",
3170 rxlen, msglen);
3171 } else if (timeleft == 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003172 brcmf_err("resumed on timeout\n");
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003173 brcmf_sdio_checkdied(bus);
Joe Perches23677ce2012-02-09 11:17:23 +00003174 } else if (pending) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003175 brcmf_dbg(CTL, "cancelled\n");
3176 return -ERESTARTSYS;
3177 } else {
3178 brcmf_dbg(CTL, "resumed for unknown reason?\n");
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003179 brcmf_sdio_checkdied(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003180 }
3181
3182 if (rxlen)
Arend van Spriel80969832012-06-09 22:51:44 +02003183 bus->sdcnt.rx_ctlpkts++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003184 else
Arend van Spriel80969832012-06-09 22:51:44 +02003185 bus->sdcnt.rx_ctlerrs++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003186
3187 return rxlen ? (int)rxlen : -ETIMEDOUT;
3188}
3189
Hante Meulemana74d0362014-01-13 22:20:22 +01003190#ifdef DEBUG
3191static bool
3192brcmf_sdio_verifymemory(struct brcmf_sdio_dev *sdiodev, u32 ram_addr,
3193 u8 *ram_data, uint ram_sz)
3194{
3195 char *ram_cmp;
3196 int err;
3197 bool ret = true;
3198 int address;
3199 int offset;
3200 int len;
3201
3202 /* read back and verify */
3203 brcmf_dbg(INFO, "Compare RAM dl & ul at 0x%08x; size=%d\n", ram_addr,
3204 ram_sz);
3205 ram_cmp = kmalloc(MEMBLOCK, GFP_KERNEL);
3206 /* do not proceed while no memory but */
3207 if (!ram_cmp)
3208 return true;
3209
3210 address = ram_addr;
3211 offset = 0;
3212 while (offset < ram_sz) {
3213 len = ((offset + MEMBLOCK) < ram_sz) ? MEMBLOCK :
3214 ram_sz - offset;
3215 err = brcmf_sdiod_ramrw(sdiodev, false, address, ram_cmp, len);
3216 if (err) {
3217 brcmf_err("error %d on reading %d membytes at 0x%08x\n",
3218 err, len, address);
3219 ret = false;
3220 break;
3221 } else if (memcmp(ram_cmp, &ram_data[offset], len)) {
3222 brcmf_err("Downloaded RAM image is corrupted, block offset is %d, len is %d\n",
3223 offset, len);
3224 ret = false;
3225 break;
3226 }
3227 offset += len;
3228 address += len;
3229 }
3230
3231 kfree(ram_cmp);
3232
3233 return ret;
3234}
3235#else /* DEBUG */
3236static bool
3237brcmf_sdio_verifymemory(struct brcmf_sdio_dev *sdiodev, u32 ram_addr,
3238 u8 *ram_data, uint ram_sz)
3239{
3240 return true;
3241}
3242#endif /* DEBUG */
3243
Arend van Spriel33556502014-01-13 22:20:25 +01003244static int brcmf_sdio_download_code_file(struct brcmf_sdio *bus,
3245 const struct firmware *fw)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003246{
Hante Meulemanf2c44fe2013-09-25 13:05:45 +02003247 int err;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003248
Hante Meulemana74d0362014-01-13 22:20:22 +01003249 brcmf_dbg(TRACE, "Enter\n");
3250
Hante Meulemanf9951c12014-01-29 15:32:17 +01003251 err = brcmf_sdiod_ramrw(bus->sdiodev, true, bus->ci->rambase,
3252 (u8 *)fw->data, fw->size);
3253 if (err)
3254 brcmf_err("error %d on writing %d membytes at 0x%08x\n",
3255 err, (int)fw->size, bus->ci->rambase);
3256 else if (!brcmf_sdio_verifymemory(bus->sdiodev, bus->ci->rambase,
3257 (u8 *)fw->data, fw->size))
3258 err = -EIO;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003259
Hante Meulemanf2c44fe2013-09-25 13:05:45 +02003260 return err;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003261}
3262
Arend van Spriel33556502014-01-13 22:20:25 +01003263static int brcmf_sdio_download_nvram(struct brcmf_sdio *bus,
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003264 void *vars, u32 varsz)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003265{
Hante Meulemana74d0362014-01-13 22:20:22 +01003266 int address;
3267 int err;
3268
3269 brcmf_dbg(TRACE, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003270
Hante Meulemana74d0362014-01-13 22:20:22 +01003271 address = bus->ci->ramsize - varsz + bus->ci->rambase;
3272 err = brcmf_sdiod_ramrw(bus->sdiodev, true, address, vars, varsz);
3273 if (err)
3274 brcmf_err("error %d on writing %d nvram bytes at 0x%08x\n",
3275 err, varsz, address);
3276 else if (!brcmf_sdio_verifymemory(bus->sdiodev, address, vars, varsz))
3277 err = -EIO;
3278
Hante Meulemana74d0362014-01-13 22:20:22 +01003279 return err;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003280}
3281
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003282static int brcmf_sdio_download_firmware(struct brcmf_sdio *bus,
3283 const struct firmware *fw,
3284 void *nvram, u32 nvlen)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003285{
Markus Elfring9e129042016-03-18 13:23:24 +11003286 int bcmerror;
Arend van Spriel33556502014-01-13 22:20:25 +01003287 u32 rstvec;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003288
3289 sdio_claim_host(bus->sdiodev->func[1]);
3290 brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003291
Arend van Spriel33556502014-01-13 22:20:25 +01003292 rstvec = get_unaligned_le32(fw->data);
3293 brcmf_dbg(SDIO, "firmware rstvec: %x\n", rstvec);
3294
3295 bcmerror = brcmf_sdio_download_code_file(bus, fw);
3296 release_firmware(fw);
3297 if (bcmerror) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003298 brcmf_err("dongle image file download failed\n");
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003299 brcmf_fw_nvram_free(nvram);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003300 goto err;
3301 }
3302
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003303 bcmerror = brcmf_sdio_download_nvram(bus, nvram, nvlen);
3304 brcmf_fw_nvram_free(nvram);
Arend van Spriel33556502014-01-13 22:20:25 +01003305 if (bcmerror) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003306 brcmf_err("dongle nvram file download failed\n");
Franky Lin3eaa9562013-04-02 21:06:20 +02003307 goto err;
3308 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02003309
3310 /* Take arm out of reset */
Arend van Sprield380ebc2015-03-11 16:11:29 +01003311 if (!brcmf_chip_set_active(bus->ci, rstvec)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003312 brcmf_err("error getting out of ARM core reset\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003313 goto err;
3314 }
3315
Arend van Spriel5b435de2011-10-05 13:19:03 +02003316err:
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003317 brcmf_sdio_clkctl(bus, CLK_SDONLY, false);
3318 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003319 return bcmerror;
3320}
3321
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003322static void brcmf_sdio_sr_init(struct brcmf_sdio *bus)
Piotr Haber4a3da992013-04-11 13:28:46 +02003323{
3324 int err = 0;
3325 u8 val;
3326
3327 brcmf_dbg(TRACE, "Enter\n");
3328
Arend van Spriela39be272013-12-12 11:58:58 +01003329 val = brcmf_sdiod_regrb(bus->sdiodev, SBSDIO_FUNC1_WAKEUPCTRL, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003330 if (err) {
3331 brcmf_err("error reading SBSDIO_FUNC1_WAKEUPCTRL\n");
3332 return;
3333 }
3334
3335 val |= 1 << SBSDIO_FUNC1_WCTRL_HTWAIT_SHIFT;
Arend van Spriela39be272013-12-12 11:58:58 +01003336 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_WAKEUPCTRL, val, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003337 if (err) {
3338 brcmf_err("error writing SBSDIO_FUNC1_WAKEUPCTRL\n");
3339 return;
3340 }
3341
3342 /* Add CMD14 Support */
Arend van Spriela39be272013-12-12 11:58:58 +01003343 brcmf_sdiod_regwb(bus->sdiodev, SDIO_CCCR_BRCM_CARDCAP,
3344 (SDIO_CCCR_BRCM_CARDCAP_CMD14_SUPPORT |
3345 SDIO_CCCR_BRCM_CARDCAP_CMD14_EXT),
3346 &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003347 if (err) {
3348 brcmf_err("error writing SDIO_CCCR_BRCM_CARDCAP\n");
3349 return;
3350 }
3351
Arend van Spriela39be272013-12-12 11:58:58 +01003352 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3353 SBSDIO_FORCE_HT, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003354 if (err) {
3355 brcmf_err("error writing SBSDIO_FUNC1_CHIPCLKCSR\n");
3356 return;
3357 }
3358
3359 /* set flag */
3360 bus->sr_enabled = true;
3361 brcmf_dbg(INFO, "SR enabled\n");
3362}
3363
3364/* enable KSO bit */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003365static int brcmf_sdio_kso_init(struct brcmf_sdio *bus)
Piotr Haber4a3da992013-04-11 13:28:46 +02003366{
3367 u8 val;
3368 int err = 0;
3369
3370 brcmf_dbg(TRACE, "Enter\n");
3371
3372 /* KSO bit added in SDIO core rev 12 */
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003373 if (brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV)->rev < 12)
Piotr Haber4a3da992013-04-11 13:28:46 +02003374 return 0;
3375
Arend van Spriela39be272013-12-12 11:58:58 +01003376 val = brcmf_sdiod_regrb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003377 if (err) {
3378 brcmf_err("error reading SBSDIO_FUNC1_SLEEPCSR\n");
3379 return err;
3380 }
3381
3382 if (!(val & SBSDIO_FUNC1_SLEEPCSR_KSO_MASK)) {
3383 val |= (SBSDIO_FUNC1_SLEEPCSR_KSO_EN <<
3384 SBSDIO_FUNC1_SLEEPCSR_KSO_SHIFT);
Arend van Spriela39be272013-12-12 11:58:58 +01003385 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
3386 val, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003387 if (err) {
3388 brcmf_err("error writing SBSDIO_FUNC1_SLEEPCSR\n");
3389 return err;
3390 }
3391 }
3392
3393 return 0;
3394}
3395
3396
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003397static int brcmf_sdio_bus_preinit(struct device *dev)
Arend van Sprielcf458282013-11-29 11:48:15 +01003398{
3399 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3400 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3401 struct brcmf_sdio *bus = sdiodev->bus;
Franky Lin8da9d2c2013-11-29 12:25:10 +01003402 uint pad_size;
Arend van Sprielcf458282013-11-29 11:48:15 +01003403 u32 value;
Arend van Sprielcf458282013-11-29 11:48:15 +01003404 int err;
3405
Franky Lin8da9d2c2013-11-29 12:25:10 +01003406 /* the commands below use the terms tx and rx from
3407 * a device perspective, ie. bus:txglom affects the
3408 * bus transfers from device to host.
3409 */
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003410 if (brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV)->rev < 12) {
Arend van Sprielcf458282013-11-29 11:48:15 +01003411 /* for sdio core rev < 12, disable txgloming */
3412 value = 0;
3413 err = brcmf_iovar_data_set(dev, "bus:txglom", &value,
3414 sizeof(u32));
3415 } else {
3416 /* otherwise, set txglomalign */
Hante Meulemanaf5b5e62016-02-17 11:27:08 +01003417 value = sdiodev->settings->bus.sdio.sd_sgentry_align;
Arend van Sprielcf458282013-11-29 11:48:15 +01003418 /* SDIO ADMA requires at least 32 bit alignment */
3419 value = max_t(u32, value, 4);
3420 err = brcmf_iovar_data_set(dev, "bus:txglomalign", &value,
3421 sizeof(u32));
3422 }
Franky Lin8da9d2c2013-11-29 12:25:10 +01003423
3424 if (err < 0)
3425 goto done;
3426
3427 bus->tx_hdrlen = SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN;
3428 if (sdiodev->sg_support) {
3429 bus->txglom = false;
3430 value = 1;
3431 pad_size = bus->sdiodev->func[2]->cur_blksize << 1;
Franky Lin8da9d2c2013-11-29 12:25:10 +01003432 err = brcmf_iovar_data_set(bus->sdiodev->dev, "bus:rxglom",
3433 &value, sizeof(u32));
3434 if (err < 0) {
3435 /* bus:rxglom is allowed to fail */
3436 err = 0;
3437 } else {
3438 bus->txglom = true;
3439 bus->tx_hdrlen += SDPCM_HWEXT_LEN;
3440 }
3441 }
3442 brcmf_bus_add_txhdrlen(bus->sdiodev->dev, bus->tx_hdrlen);
3443
3444done:
Arend van Sprielcf458282013-11-29 11:48:15 +01003445 return err;
3446}
3447
Arend van Sprielff4445a2015-10-08 20:33:11 +02003448static size_t brcmf_sdio_bus_get_ramsize(struct device *dev)
3449{
3450 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3451 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3452 struct brcmf_sdio *bus = sdiodev->bus;
3453
3454 return bus->ci->ramsize - bus->ci->srsize;
3455}
3456
3457static int brcmf_sdio_bus_get_memdump(struct device *dev, void *data,
3458 size_t mem_size)
3459{
3460 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3461 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3462 struct brcmf_sdio *bus = sdiodev->bus;
3463 int err;
3464 int address;
3465 int offset;
3466 int len;
3467
3468 brcmf_dbg(INFO, "dump at 0x%08x: size=%zu\n", bus->ci->rambase,
3469 mem_size);
3470
3471 address = bus->ci->rambase;
3472 offset = err = 0;
3473 sdio_claim_host(sdiodev->func[1]);
3474 while (offset < mem_size) {
3475 len = ((offset + MEMBLOCK) < mem_size) ? MEMBLOCK :
3476 mem_size - offset;
3477 err = brcmf_sdiod_ramrw(sdiodev, false, address, data, len);
3478 if (err) {
3479 brcmf_err("error %d on reading %d membytes at 0x%08x\n",
3480 err, len, address);
3481 goto done;
3482 }
3483 data += len;
3484 offset += len;
3485 address += len;
3486 }
3487
3488done:
3489 sdio_release_host(sdiodev->func[1]);
3490 return err;
3491}
3492
Arend van Spriel99824642015-02-06 18:36:45 +01003493void brcmf_sdio_trigger_dpc(struct brcmf_sdio *bus)
3494{
Hante Meuleman2c64e162015-03-18 13:25:22 +01003495 if (!bus->dpc_triggered) {
3496 bus->dpc_triggered = true;
Arend van Spriel99824642015-02-06 18:36:45 +01003497 queue_work(bus->brcmf_wq, &bus->datawork);
3498 }
3499}
3500
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003501void brcmf_sdio_isr(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003502{
Arend van Spriel5b435de2011-10-05 13:19:03 +02003503 brcmf_dbg(TRACE, "Enter\n");
3504
3505 if (!bus) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003506 brcmf_err("bus is null pointer, exiting\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003507 return;
3508 }
3509
Arend van Spriel5b435de2011-10-05 13:19:03 +02003510 /* Count the interrupt call */
Arend van Spriel80969832012-06-09 22:51:44 +02003511 bus->sdcnt.intrcount++;
Franky Lin45316032012-09-13 21:12:03 +02003512 if (in_interrupt())
3513 atomic_set(&bus->ipend, 1);
3514 else
3515 if (brcmf_sdio_intr_rstatus(bus)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003516 brcmf_err("failed backplane access\n");
Franky Lin45316032012-09-13 21:12:03 +02003517 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02003518
Arend van Spriel5b435de2011-10-05 13:19:03 +02003519 /* Disable additional interrupts (is this needed now)? */
3520 if (!bus->intr)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003521 brcmf_err("isr w/o interrupt configured!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003522
Hante Meuleman2c64e162015-03-18 13:25:22 +01003523 bus->dpc_triggered = true;
Franky Linf1e68c22012-09-13 21:12:00 +02003524 queue_work(bus->brcmf_wq, &bus->datawork);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003525}
3526
Hante Meulemanb441ba82015-03-06 18:40:39 +01003527static void brcmf_sdio_bus_watchdog(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003528{
Arend van Spriel5b435de2011-10-05 13:19:03 +02003529 brcmf_dbg(TIMER, "Enter\n");
3530
Arend van Spriel5b435de2011-10-05 13:19:03 +02003531 /* Poll period: check device if appropriate. */
Piotr Haber4a3da992013-04-11 13:28:46 +02003532 if (!bus->sr_enabled &&
3533 bus->poll && (++bus->polltick >= bus->pollrate)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003534 u32 intstatus = 0;
3535
3536 /* Reset poll tick */
3537 bus->polltick = 0;
3538
3539 /* Check device if no interrupts */
Arend van Spriel80969832012-06-09 22:51:44 +02003540 if (!bus->intr ||
3541 (bus->sdcnt.intrcount == bus->sdcnt.lastintrs)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003542
Hante Meuleman2c64e162015-03-18 13:25:22 +01003543 if (!bus->dpc_triggered) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003544 u8 devpend;
Arend van Sprielfccfe932013-06-18 13:29:26 +02003545
Franky Lin38b0b0d2012-11-05 16:22:24 -08003546 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriela39be272013-12-12 11:58:58 +01003547 devpend = brcmf_sdiod_regrb(bus->sdiodev,
3548 SDIO_CCCR_INTx,
3549 NULL);
Franky Lin38b0b0d2012-11-05 16:22:24 -08003550 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel99824642015-02-06 18:36:45 +01003551 intstatus = devpend & (INTR_STATUS_FUNC1 |
3552 INTR_STATUS_FUNC2);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003553 }
3554
3555 /* If there is something, make like the ISR and
3556 schedule the DPC */
3557 if (intstatus) {
Arend van Spriel80969832012-06-09 22:51:44 +02003558 bus->sdcnt.pollcnt++;
Franky Lin1d382272012-09-13 21:11:59 +02003559 atomic_set(&bus->ipend, 1);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003560
Hante Meuleman2c64e162015-03-18 13:25:22 +01003561 bus->dpc_triggered = true;
Franky Linf1e68c22012-09-13 21:12:00 +02003562 queue_work(bus->brcmf_wq, &bus->datawork);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003563 }
3564 }
3565
3566 /* Update interrupt tracking */
Arend van Spriel80969832012-06-09 22:51:44 +02003567 bus->sdcnt.lastintrs = bus->sdcnt.intrcount;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003568 }
Joe Perches8ae74652012-01-15 00:38:38 -08003569#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02003570 /* Poll for console output periodically */
Arend van Spriel9d6c1dc2015-08-26 22:15:01 +02003571 if (bus->sdiodev->state == BRCMF_SDIOD_DATA && BRCMF_FWCON_ON() &&
Franky Lin8d169aa2011-11-22 17:21:52 -08003572 bus->console_interval != 0) {
Arend van Spriel63ce3d52016-01-05 11:05:48 +01003573 bus->console.count += jiffies_to_msecs(BRCMF_WD_POLL);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003574 if (bus->console.count >= bus->console_interval) {
3575 bus->console.count -= bus->console_interval;
Franky Lin38b0b0d2012-11-05 16:22:24 -08003576 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003577 /* Make sure backplane clock is on */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003578 brcmf_sdio_bus_sleep(bus, false, false);
3579 if (brcmf_sdio_readconsole(bus) < 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003580 /* stop on error */
3581 bus->console_interval = 0;
Franky Lin38b0b0d2012-11-05 16:22:24 -08003582 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003583 }
3584 }
Joe Perches8ae74652012-01-15 00:38:38 -08003585#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02003586
3587 /* On idle timeout clear activity flag and/or turn off clock */
Hante Meuleman2c64e162015-03-18 13:25:22 +01003588 if (!bus->dpc_triggered) {
3589 rmb();
3590 if ((!bus->dpc_running) && (bus->idletime > 0) &&
3591 (bus->clkstate == CLK_AVAIL)) {
3592 bus->idlecount++;
3593 if (bus->idlecount > bus->idletime) {
3594 brcmf_dbg(SDIO, "idle\n");
3595 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel4011fc42016-01-05 11:05:47 +01003596 brcmf_sdio_wd_timer(bus, false);
Hante Meuleman2c64e162015-03-18 13:25:22 +01003597 bus->idlecount = 0;
3598 brcmf_sdio_bus_sleep(bus, true, false);
3599 sdio_release_host(bus->sdiodev->func[1]);
3600 }
3601 } else {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003602 bus->idlecount = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003603 }
Hante Meulemanb441ba82015-03-06 18:40:39 +01003604 } else {
3605 bus->idlecount = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003606 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02003607}
3608
Franky Linf1e68c22012-09-13 21:12:00 +02003609static void brcmf_sdio_dataworker(struct work_struct *work)
3610{
3611 struct brcmf_sdio *bus = container_of(work, struct brcmf_sdio,
3612 datawork);
Franky Linf1e68c22012-09-13 21:12:00 +02003613
Hante Meuleman2c64e162015-03-18 13:25:22 +01003614 bus->dpc_running = true;
3615 wmb();
3616 while (ACCESS_ONCE(bus->dpc_triggered)) {
3617 bus->dpc_triggered = false;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003618 brcmf_sdio_dpc(bus);
Hante Meulemanb441ba82015-03-06 18:40:39 +01003619 bus->idlecount = 0;
Franky Linf1e68c22012-09-13 21:12:00 +02003620 }
Hante Meuleman2c64e162015-03-18 13:25:22 +01003621 bus->dpc_running = false;
Arend van Spriel99824642015-02-06 18:36:45 +01003622 if (brcmf_sdiod_freezing(bus->sdiodev)) {
3623 brcmf_sdiod_change_state(bus->sdiodev, BRCMF_SDIOD_DOWN);
3624 brcmf_sdiod_try_freeze(bus->sdiodev);
3625 brcmf_sdiod_change_state(bus->sdiodev, BRCMF_SDIOD_DATA);
3626 }
Franky Linf1e68c22012-09-13 21:12:00 +02003627}
3628
Arend van Spriel65d80d02014-01-29 15:32:14 +01003629static void
3630brcmf_sdio_drivestrengthinit(struct brcmf_sdio_dev *sdiodev,
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003631 struct brcmf_chip *ci, u32 drivestrength)
Arend van Spriel65d80d02014-01-29 15:32:14 +01003632{
3633 const struct sdiod_drive_str *str_tab = NULL;
3634 u32 str_mask;
3635 u32 str_shift;
Arend van Spriel65d80d02014-01-29 15:32:14 +01003636 u32 i;
3637 u32 drivestrength_sel = 0;
3638 u32 cc_data_temp;
3639 u32 addr;
3640
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003641 if (!(ci->cc_caps & CC_CAP_PMU))
Arend van Spriel65d80d02014-01-29 15:32:14 +01003642 return;
3643
3644 switch (SDIOD_DRVSTR_KEY(ci->chip, ci->pmurev)) {
Hante Meuleman5779ae62014-07-12 08:49:34 +02003645 case SDIOD_DRVSTR_KEY(BRCM_CC_4330_CHIP_ID, 12):
Arend van Spriel65d80d02014-01-29 15:32:14 +01003646 str_tab = sdiod_drvstr_tab1_1v8;
3647 str_mask = 0x00003800;
3648 str_shift = 11;
3649 break;
Hante Meuleman5779ae62014-07-12 08:49:34 +02003650 case SDIOD_DRVSTR_KEY(BRCM_CC_4334_CHIP_ID, 17):
Arend van Spriel65d80d02014-01-29 15:32:14 +01003651 str_tab = sdiod_drvstr_tab6_1v8;
3652 str_mask = 0x00001800;
3653 str_shift = 11;
3654 break;
Hante Meuleman5779ae62014-07-12 08:49:34 +02003655 case SDIOD_DRVSTR_KEY(BRCM_CC_43143_CHIP_ID, 17):
Arend van Spriel65d80d02014-01-29 15:32:14 +01003656 /* note: 43143 does not support tristate */
3657 i = ARRAY_SIZE(sdiod_drvstr_tab2_3v3) - 1;
3658 if (drivestrength >= sdiod_drvstr_tab2_3v3[i].strength) {
3659 str_tab = sdiod_drvstr_tab2_3v3;
3660 str_mask = 0x00000007;
3661 str_shift = 0;
3662 } else
3663 brcmf_err("Invalid SDIO Drive strength for chip %s, strength=%d\n",
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003664 ci->name, drivestrength);
Arend van Spriel65d80d02014-01-29 15:32:14 +01003665 break;
Hante Meuleman5779ae62014-07-12 08:49:34 +02003666 case SDIOD_DRVSTR_KEY(BRCM_CC_43362_CHIP_ID, 13):
Arend van Spriel65d80d02014-01-29 15:32:14 +01003667 str_tab = sdiod_drive_strength_tab5_1v8;
3668 str_mask = 0x00003800;
3669 str_shift = 11;
3670 break;
3671 default:
Wright Fengd922dfa2016-06-03 23:31:08 +02003672 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 +01003673 ci->name, ci->chiprev, ci->pmurev);
Arend van Spriel65d80d02014-01-29 15:32:14 +01003674 break;
3675 }
3676
3677 if (str_tab != NULL) {
Rafał Miłeckie2b397f2016-01-26 17:57:04 +01003678 struct brcmf_core *pmu = brcmf_chip_get_pmu(ci);
3679
Arend van Spriel65d80d02014-01-29 15:32:14 +01003680 for (i = 0; str_tab[i].strength != 0; i++) {
3681 if (drivestrength >= str_tab[i].strength) {
3682 drivestrength_sel = str_tab[i].sel;
3683 break;
3684 }
3685 }
Rafał Miłeckie2b397f2016-01-26 17:57:04 +01003686 addr = CORE_CC_REG(pmu->base, chipcontrol_addr);
Arend van Spriel65d80d02014-01-29 15:32:14 +01003687 brcmf_sdiod_regwl(sdiodev, addr, 1, NULL);
3688 cc_data_temp = brcmf_sdiod_regrl(sdiodev, addr, NULL);
3689 cc_data_temp &= ~str_mask;
3690 drivestrength_sel <<= str_shift;
3691 cc_data_temp |= drivestrength_sel;
3692 brcmf_sdiod_regwl(sdiodev, addr, cc_data_temp, NULL);
3693
3694 brcmf_dbg(INFO, "SDIO: %d mA (req=%d mA) drive strength selected, set to 0x%08x\n",
3695 str_tab[i].strength, drivestrength, cc_data_temp);
3696 }
3697}
3698
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003699static int brcmf_sdio_buscoreprep(void *ctx)
Arend van Spriel65d80d02014-01-29 15:32:14 +01003700{
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003701 struct brcmf_sdio_dev *sdiodev = ctx;
Arend van Spriel65d80d02014-01-29 15:32:14 +01003702 int err = 0;
3703 u8 clkval, clkset;
3704
3705 /* Try forcing SDIO core to do ALPAvail request only */
3706 clkset = SBSDIO_FORCE_HW_CLKREQ_OFF | SBSDIO_ALP_AVAIL_REQ;
3707 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, clkset, &err);
3708 if (err) {
3709 brcmf_err("error writing for HT off\n");
3710 return err;
3711 }
3712
3713 /* If register supported, wait for ALPAvail and then force ALP */
3714 /* This may take up to 15 milliseconds */
3715 clkval = brcmf_sdiod_regrb(sdiodev,
3716 SBSDIO_FUNC1_CHIPCLKCSR, NULL);
3717
3718 if ((clkval & ~SBSDIO_AVBITS) != clkset) {
3719 brcmf_err("ChipClkCSR access: wrote 0x%02x read 0x%02x\n",
3720 clkset, clkval);
3721 return -EACCES;
3722 }
3723
3724 SPINWAIT(((clkval = brcmf_sdiod_regrb(sdiodev,
3725 SBSDIO_FUNC1_CHIPCLKCSR, NULL)),
3726 !SBSDIO_ALPAV(clkval)),
3727 PMU_MAX_TRANSITION_DLY);
3728 if (!SBSDIO_ALPAV(clkval)) {
3729 brcmf_err("timeout on ALPAV wait, clkval 0x%02x\n",
3730 clkval);
3731 return -EBUSY;
3732 }
3733
3734 clkset = SBSDIO_FORCE_HW_CLKREQ_OFF | SBSDIO_FORCE_ALP;
3735 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, clkset, &err);
3736 udelay(65);
3737
3738 /* Also, disable the extra SDIO pull-ups */
3739 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_SDIOPULLUP, 0, NULL);
3740
3741 return 0;
3742}
3743
Arend van Sprield380ebc2015-03-11 16:11:29 +01003744static void brcmf_sdio_buscore_activate(void *ctx, struct brcmf_chip *chip,
3745 u32 rstvec)
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003746{
3747 struct brcmf_sdio_dev *sdiodev = ctx;
3748 struct brcmf_core *core;
3749 u32 reg_addr;
3750
3751 /* clear all interrupts */
3752 core = brcmf_chip_get_core(chip, BCMA_CORE_SDIO_DEV);
3753 reg_addr = core->base + offsetof(struct sdpcmd_regs, intstatus);
3754 brcmf_sdiod_regwl(sdiodev, reg_addr, 0xFFFFFFFF, NULL);
3755
3756 if (rstvec)
3757 /* Write reset vector to address 0 */
3758 brcmf_sdiod_ramrw(sdiodev, true, 0, (void *)&rstvec,
3759 sizeof(rstvec));
3760}
3761
3762static u32 brcmf_sdio_buscore_read32(void *ctx, u32 addr)
3763{
3764 struct brcmf_sdio_dev *sdiodev = ctx;
3765 u32 val, rev;
3766
3767 val = brcmf_sdiod_regrl(sdiodev, addr, NULL);
Arend Van Spriel634faf32016-09-05 11:42:12 +01003768 if ((sdiodev->func[0]->device == SDIO_DEVICE_ID_BROADCOM_4335_4339 ||
3769 sdiodev->func[0]->device == SDIO_DEVICE_ID_BROADCOM_4339) &&
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003770 addr == CORE_CC_REG(SI_ENUM_BASE, chipid)) {
3771 rev = (val & CID_REV_MASK) >> CID_REV_SHIFT;
3772 if (rev >= 2) {
3773 val &= ~CID_ID_MASK;
Hante Meuleman5779ae62014-07-12 08:49:34 +02003774 val |= BRCM_CC_4339_CHIP_ID;
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003775 }
3776 }
3777 return val;
3778}
3779
3780static void brcmf_sdio_buscore_write32(void *ctx, u32 addr, u32 val)
3781{
3782 struct brcmf_sdio_dev *sdiodev = ctx;
3783
3784 brcmf_sdiod_regwl(sdiodev, addr, val, NULL);
3785}
3786
3787static const struct brcmf_buscore_ops brcmf_sdio_buscore_ops = {
3788 .prepare = brcmf_sdio_buscoreprep,
Arend van Sprield380ebc2015-03-11 16:11:29 +01003789 .activate = brcmf_sdio_buscore_activate,
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003790 .read32 = brcmf_sdio_buscore_read32,
3791 .write32 = brcmf_sdio_buscore_write32,
3792};
3793
Arend van Spriel5b435de2011-10-05 13:19:03 +02003794static bool
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003795brcmf_sdio_probe_attach(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003796{
Hante Meuleman4d792892016-02-17 11:27:07 +01003797 struct brcmf_sdio_dev *sdiodev;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003798 u8 clkctl = 0;
3799 int err = 0;
3800 int reg_addr;
3801 u32 reg_val;
Hante Meuleman668761a2013-04-12 10:55:55 +02003802 u32 drivestrength;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003803
Hante Meuleman4d792892016-02-17 11:27:07 +01003804 sdiodev = bus->sdiodev;
3805 sdio_claim_host(sdiodev->func[1]);
Franky Lin38b0b0d2012-11-05 16:22:24 -08003806
Joe Perches18aad4f2012-01-15 00:38:42 -08003807 pr_debug("F1 signature read @0x18000000=0x%4x\n",
Hante Meuleman4d792892016-02-17 11:27:07 +01003808 brcmf_sdiod_regrl(sdiodev, SI_ENUM_BASE, NULL));
Arend van Spriel5b435de2011-10-05 13:19:03 +02003809
3810 /*
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003811 * Force PLL off until brcmf_chip_attach()
Arend van Spriel5b435de2011-10-05 13:19:03 +02003812 * programs PLL control regs
3813 */
3814
Hante Meuleman4d792892016-02-17 11:27:07 +01003815 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
Arend van Spriela39be272013-12-12 11:58:58 +01003816 BRCMF_INIT_CLKCTL1, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003817 if (!err)
Hante Meuleman4d792892016-02-17 11:27:07 +01003818 clkctl = brcmf_sdiod_regrb(sdiodev,
Arend van Spriela39be272013-12-12 11:58:58 +01003819 SBSDIO_FUNC1_CHIPCLKCSR, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003820
3821 if (err || ((clkctl & ~SBSDIO_AVBITS) != BRCMF_INIT_CLKCTL1)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003822 brcmf_err("ChipClkCSR access: err %d wrote 0x%02x read 0x%02x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02003823 err, BRCMF_INIT_CLKCTL1, clkctl);
3824 goto fail;
3825 }
3826
Hante Meuleman4d792892016-02-17 11:27:07 +01003827 bus->ci = brcmf_chip_attach(sdiodev, &brcmf_sdio_buscore_ops);
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003828 if (IS_ERR(bus->ci)) {
3829 brcmf_err("brcmf_chip_attach failed!\n");
3830 bus->ci = NULL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003831 goto fail;
3832 }
Hante Meulemanaf5b5e62016-02-17 11:27:08 +01003833 sdiodev->settings = brcmf_get_module_param(sdiodev->dev,
Hante Meuleman4d792892016-02-17 11:27:07 +01003834 BRCMF_BUSTYPE_SDIO,
3835 bus->ci->chip,
3836 bus->ci->chiprev);
Hante Meulemanaf5b5e62016-02-17 11:27:08 +01003837 if (!sdiodev->settings) {
3838 brcmf_err("Failed to get device parameters\n");
3839 goto fail;
3840 }
Hante Meuleman4d792892016-02-17 11:27:07 +01003841 /* platform specific configuration:
3842 * alignments must be at least 4 bytes for ADMA
3843 */
3844 bus->head_align = ALIGNMENT;
3845 bus->sgentry_align = ALIGNMENT;
Hante Meulemanaf5b5e62016-02-17 11:27:08 +01003846 if (sdiodev->settings->bus.sdio.sd_head_align > ALIGNMENT)
3847 bus->head_align = sdiodev->settings->bus.sdio.sd_head_align;
3848 if (sdiodev->settings->bus.sdio.sd_sgentry_align > ALIGNMENT)
3849 bus->sgentry_align =
3850 sdiodev->settings->bus.sdio.sd_sgentry_align;
3851
Hante Meuleman4d792892016-02-17 11:27:07 +01003852 /* allocate scatter-gather table. sg support
3853 * will be disabled upon allocation failure.
3854 */
3855 brcmf_sdiod_sgtable_alloc(sdiodev);
3856
3857#ifdef CONFIG_PM_SLEEP
3858 /* wowl can be supported when KEEP_POWER is true and (WAKE_SDIO_IRQ
3859 * is true or when platform data OOB irq is true).
3860 */
3861 if ((sdio_get_host_pm_caps(sdiodev->func[1]) & MMC_PM_KEEP_POWER) &&
3862 ((sdio_get_host_pm_caps(sdiodev->func[1]) & MMC_PM_WAKE_SDIO_IRQ) ||
Hante Meulemanaf5b5e62016-02-17 11:27:08 +01003863 (sdiodev->settings->bus.sdio.oob_irq_supported)))
Hante Meuleman4d792892016-02-17 11:27:07 +01003864 sdiodev->bus_if->wowl_supported = true;
3865#endif
Arend van Spriel5b435de2011-10-05 13:19:03 +02003866
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003867 if (brcmf_sdio_kso_init(bus)) {
Piotr Haber4a3da992013-04-11 13:28:46 +02003868 brcmf_err("error enabling KSO\n");
3869 goto fail;
3870 }
3871
Hante Meulemanaf5b5e62016-02-17 11:27:08 +01003872 if (sdiodev->settings->bus.sdio.drive_strength)
3873 drivestrength = sdiodev->settings->bus.sdio.drive_strength;
Hante Meuleman668761a2013-04-12 10:55:55 +02003874 else
3875 drivestrength = DEFAULT_SDIO_DRIVE_STRENGTH;
Hante Meuleman4d792892016-02-17 11:27:07 +01003876 brcmf_sdio_drivestrengthinit(sdiodev, bus->ci, drivestrength);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003877
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003878 /* Set card control so an SDIO card reset does a WLAN backplane reset */
Hante Meuleman4d792892016-02-17 11:27:07 +01003879 reg_val = brcmf_sdiod_regrb(sdiodev, SDIO_CCCR_BRCM_CARDCTRL, &err);
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003880 if (err)
3881 goto fail;
3882
3883 reg_val |= SDIO_CCCR_BRCM_CARDCTRL_WLANRESET;
3884
Hante Meuleman4d792892016-02-17 11:27:07 +01003885 brcmf_sdiod_regwb(sdiodev, SDIO_CCCR_BRCM_CARDCTRL, reg_val, &err);
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003886 if (err)
3887 goto fail;
3888
3889 /* set PMUControl so a backplane reset does PMU state reload */
Rafał Miłeckie2b397f2016-01-26 17:57:04 +01003890 reg_addr = CORE_CC_REG(brcmf_chip_get_pmu(bus->ci)->base, pmucontrol);
Hante Meuleman4d792892016-02-17 11:27:07 +01003891 reg_val = brcmf_sdiod_regrl(sdiodev, reg_addr, &err);
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003892 if (err)
3893 goto fail;
3894
3895 reg_val |= (BCMA_CC_PMU_CTL_RES_RELOAD << BCMA_CC_PMU_CTL_RES_SHIFT);
3896
Hante Meuleman4d792892016-02-17 11:27:07 +01003897 brcmf_sdiod_regwl(sdiodev, reg_addr, reg_val, &err);
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003898 if (err)
3899 goto fail;
3900
Hante Meuleman4d792892016-02-17 11:27:07 +01003901 sdio_release_host(sdiodev->func[1]);
Franky Lin38b0b0d2012-11-05 16:22:24 -08003902
Arend van Spriel5b435de2011-10-05 13:19:03 +02003903 brcmu_pktq_init(&bus->txq, (PRIOMASK + 1), TXQLEN);
3904
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01003905 /* allocate header buffer */
3906 bus->hdrbuf = kzalloc(MAX_HDR_READ + bus->head_align, GFP_KERNEL);
3907 if (!bus->hdrbuf)
3908 return false;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003909 /* Locate an appropriately-aligned portion of hdrbuf */
3910 bus->rxhdr = (u8 *) roundup((unsigned long)&bus->hdrbuf[0],
Arend van Spriel9b2d2f22013-11-29 12:25:14 +01003911 bus->head_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003912
3913 /* Set the poll and/or interrupt flags */
3914 bus->intr = true;
3915 bus->poll = false;
3916 if (bus->poll)
3917 bus->pollrate = 1;
3918
3919 return true;
3920
3921fail:
Hante Meuleman4d792892016-02-17 11:27:07 +01003922 sdio_release_host(sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003923 return false;
3924}
3925
Arend van Spriel5b435de2011-10-05 13:19:03 +02003926static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003927brcmf_sdio_watchdog_thread(void *data)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003928{
Franky Line92eedf2011-11-22 17:21:50 -08003929 struct brcmf_sdio *bus = (struct brcmf_sdio *)data;
Arend van Spriel99824642015-02-06 18:36:45 +01003930 int wait;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003931
3932 allow_signal(SIGTERM);
3933 /* Run until signal received */
Arend van Spriel99824642015-02-06 18:36:45 +01003934 brcmf_sdiod_freezer_count(bus->sdiodev);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003935 while (1) {
3936 if (kthread_should_stop())
3937 break;
Arend van Spriel99824642015-02-06 18:36:45 +01003938 brcmf_sdiod_freezer_uncount(bus->sdiodev);
3939 wait = wait_for_completion_interruptible(&bus->watchdog_wait);
3940 brcmf_sdiod_freezer_count(bus->sdiodev);
3941 brcmf_sdiod_try_freeze(bus->sdiodev);
3942 if (!wait) {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003943 brcmf_sdio_bus_watchdog(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003944 /* Count the tick for reference */
Arend van Spriel80969832012-06-09 22:51:44 +02003945 bus->sdcnt.tickcnt++;
Arend van Spriel58e9df42014-03-20 10:18:01 +01003946 reinit_completion(&bus->watchdog_wait);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003947 } else
3948 break;
3949 }
3950 return 0;
3951}
3952
3953static void
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003954brcmf_sdio_watchdog(unsigned long data)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003955{
Franky Line92eedf2011-11-22 17:21:50 -08003956 struct brcmf_sdio *bus = (struct brcmf_sdio *)data;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003957
3958 if (bus->watchdog_tsk) {
3959 complete(&bus->watchdog_wait);
3960 /* Reschedule the watchdog */
Arend van Spriel4011fc42016-01-05 11:05:47 +01003961 if (bus->wd_active)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003962 mod_timer(&bus->timer,
Arend van Spriel63ce3d52016-01-05 11:05:48 +01003963 jiffies + BRCMF_WD_POLL);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003964 }
3965}
3966
Julia Lawall6866a642015-11-14 17:22:07 +01003967static const struct brcmf_bus_ops brcmf_sdio_bus_ops = {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003968 .stop = brcmf_sdio_bus_stop,
3969 .preinit = brcmf_sdio_bus_preinit,
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003970 .txdata = brcmf_sdio_bus_txdata,
3971 .txctl = brcmf_sdio_bus_txctl,
3972 .rxctl = brcmf_sdio_bus_rxctl,
3973 .gettxq = brcmf_sdio_bus_gettxq,
Arend van Sprielff4445a2015-10-08 20:33:11 +02003974 .wowl_config = brcmf_sdio_wowl_config,
3975 .get_ramsize = brcmf_sdio_bus_get_ramsize,
3976 .get_memdump = brcmf_sdio_bus_get_memdump,
Arend van Sprield9cb2592012-12-05 15:25:54 +01003977};
3978
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003979static void brcmf_sdio_firmware_callback(struct device *dev,
3980 const struct firmware *code,
3981 void *nvram, u32 nvram_len)
3982{
3983 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3984 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3985 struct brcmf_sdio *bus = sdiodev->bus;
3986 int err = 0;
3987 u8 saveclk;
3988
3989 brcmf_dbg(TRACE, "Enter: dev=%s\n", dev_name(dev));
3990
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003991 if (!bus_if->drvr)
3992 return;
3993
Hante Meulemana1cee862015-01-25 20:31:35 +01003994 /* try to download image and nvram to the dongle */
3995 bus->alp_only = true;
3996 err = brcmf_sdio_download_firmware(bus, code, nvram, nvram_len);
3997 if (err)
3998 goto fail;
3999 bus->alp_only = false;
4000
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02004001 /* Start the watchdog timer */
4002 bus->sdcnt.tickcnt = 0;
Arend van Spriel4011fc42016-01-05 11:05:47 +01004003 brcmf_sdio_wd_timer(bus, true);
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02004004
4005 sdio_claim_host(sdiodev->func[1]);
4006
4007 /* Make sure backplane clock is on, needed to generate F2 interrupt */
4008 brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
4009 if (bus->clkstate != CLK_AVAIL)
4010 goto release;
4011
4012 /* Force clocks on backplane to be sure F2 interrupt propagates */
4013 saveclk = brcmf_sdiod_regrb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, &err);
4014 if (!err) {
4015 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
4016 (saveclk | SBSDIO_FORCE_HT), &err);
4017 }
4018 if (err) {
4019 brcmf_err("Failed to force clock for F2: err %d\n", err);
4020 goto release;
4021 }
4022
4023 /* Enable function 2 (frame transfers) */
4024 w_sdreg32(bus, SDPCM_PROT_VERSION << SMB_DATA_VERSION_SHIFT,
4025 offsetof(struct sdpcmd_regs, tosbmailboxdata));
4026 err = sdio_enable_func(sdiodev->func[SDIO_FUNC_2]);
4027
4028
4029 brcmf_dbg(INFO, "enable F2: err=%d\n", err);
4030
4031 /* If F2 successfully enabled, set core and enable interrupts */
4032 if (!err) {
4033 /* Set up the interrupt mask and enable interrupts */
4034 bus->hostintmask = HOSTINTMASK;
4035 w_sdreg32(bus, bus->hostintmask,
4036 offsetof(struct sdpcmd_regs, hostintmask));
4037
4038 brcmf_sdiod_regwb(sdiodev, SBSDIO_WATERMARK, 8, &err);
4039 } else {
4040 /* Disable F2 again */
4041 sdio_disable_func(sdiodev->func[SDIO_FUNC_2]);
4042 goto release;
4043 }
4044
4045 if (brcmf_chip_sr_capable(bus->ci)) {
4046 brcmf_sdio_sr_init(bus);
4047 } else {
4048 /* Restore previous clock setting */
4049 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
4050 saveclk, &err);
4051 }
4052
4053 if (err == 0) {
Arend Van Sprielfd3ed332016-07-15 12:39:13 +02004054 /* Allow full data communication using DPC from now on. */
4055 brcmf_sdiod_change_state(bus->sdiodev, BRCMF_SDIOD_DATA);
4056
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02004057 err = brcmf_sdiod_intr_register(sdiodev);
4058 if (err != 0)
4059 brcmf_err("intr register failed:%d\n", err);
4060 }
4061
4062 /* If we didn't come up, turn off backplane clock */
4063 if (err != 0)
4064 brcmf_sdio_clkctl(bus, CLK_NONE, false);
4065
4066 sdio_release_host(sdiodev->func[1]);
4067
4068 err = brcmf_bus_start(dev);
4069 if (err != 0) {
4070 brcmf_err("dongle is not responding\n");
4071 goto fail;
4072 }
4073 return;
4074
4075release:
4076 sdio_release_host(sdiodev->func[1]);
4077fail:
4078 brcmf_dbg(TRACE, "failed: dev=%s, err=%d\n", dev_name(dev), err);
4079 device_release_driver(dev);
4080}
4081
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01004082struct brcmf_sdio *brcmf_sdio_probe(struct brcmf_sdio_dev *sdiodev)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004083{
4084 int ret;
Franky Line92eedf2011-11-22 17:21:50 -08004085 struct brcmf_sdio *bus;
Arend van Spriel99824642015-02-06 18:36:45 +01004086 struct workqueue_struct *wq;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004087
Arend van Spriel5b435de2011-10-05 13:19:03 +02004088 brcmf_dbg(TRACE, "Enter\n");
4089
Arend van Spriel5b435de2011-10-05 13:19:03 +02004090 /* Allocate private bus interface state */
Franky Line92eedf2011-11-22 17:21:50 -08004091 bus = kzalloc(sizeof(struct brcmf_sdio), GFP_ATOMIC);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004092 if (!bus)
4093 goto fail;
4094
4095 bus->sdiodev = sdiodev;
4096 sdiodev->bus = bus;
Arend van Sprielb83db862011-10-19 12:51:09 +02004097 skb_queue_head_init(&bus->glom);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004098 bus->txbound = BRCMF_TXBOUND;
4099 bus->rxbound = BRCMF_RXBOUND;
4100 bus->txminmax = BRCMF_TXMINMAX;
Franky Lin6bc52312013-08-10 12:27:28 +02004101 bus->tx_seq = SDPCM_SEQ_WRAP - 1;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004102
Arend van Spriel99824642015-02-06 18:36:45 +01004103 /* single-threaded workqueue */
4104 wq = alloc_ordered_workqueue("brcmf_wq/%s", WQ_MEM_RECLAIM,
4105 dev_name(&sdiodev->func[1]->dev));
4106 if (!wq) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01004107 brcmf_err("insufficient memory to create txworkqueue\n");
Hante Meuleman37ac5782012-11-14 18:46:18 -08004108 goto fail;
4109 }
Arend van Spriel99824642015-02-06 18:36:45 +01004110 brcmf_sdiod_freezer_count(sdiodev);
4111 INIT_WORK(&bus->datawork, brcmf_sdio_dataworker);
4112 bus->brcmf_wq = wq;
Hante Meuleman37ac5782012-11-14 18:46:18 -08004113
Arend van Spriel5b435de2011-10-05 13:19:03 +02004114 /* attempt to attach to the dongle */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01004115 if (!(brcmf_sdio_probe_attach(bus))) {
4116 brcmf_err("brcmf_sdio_probe_attach failed\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02004117 goto fail;
4118 }
4119
Franky Lindd43a012012-11-05 16:22:22 -08004120 spin_lock_init(&bus->rxctl_lock);
Hante Meulemanfed7ec42014-03-15 17:18:18 +01004121 spin_lock_init(&bus->txq_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004122 init_waitqueue_head(&bus->ctrl_wait);
4123 init_waitqueue_head(&bus->dcmd_resp_wait);
4124
4125 /* Set up the watchdog timer */
4126 init_timer(&bus->timer);
4127 bus->timer.data = (unsigned long)bus;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01004128 bus->timer.function = brcmf_sdio_watchdog;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004129
Arend van Spriel5b435de2011-10-05 13:19:03 +02004130 /* Initialize watchdog thread */
4131 init_completion(&bus->watchdog_wait);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01004132 bus->watchdog_tsk = kthread_run(brcmf_sdio_watchdog_thread,
Arend van Spriel99824642015-02-06 18:36:45 +01004133 bus, "brcmf_wdog/%s",
4134 dev_name(&sdiodev->func[1]->dev));
Arend van Spriel5b435de2011-10-05 13:19:03 +02004135 if (IS_ERR(bus->watchdog_tsk)) {
Joe Perches02f77192012-01-15 00:38:44 -08004136 pr_warn("brcmf_watchdog thread failed to start\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02004137 bus->watchdog_tsk = NULL;
4138 }
4139 /* Initialize DPC thread */
Hante Meuleman2c64e162015-03-18 13:25:22 +01004140 bus->dpc_triggered = false;
4141 bus->dpc_running = false;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004142
Franky Lina9ffda82011-12-16 18:37:12 -08004143 /* Assign bus interface call back */
Arend van Sprield9cb2592012-12-05 15:25:54 +01004144 bus->sdiodev->bus_if->dev = bus->sdiodev->dev;
4145 bus->sdiodev->bus_if->ops = &brcmf_sdio_bus_ops;
Arend van Spriel75d907d2013-02-06 18:40:45 +01004146 bus->sdiodev->bus_if->chip = bus->ci->chip;
4147 bus->sdiodev->bus_if->chiprev = bus->ci->chiprev;
Arend van Sprield9cb2592012-12-05 15:25:54 +01004148
Franky Lin706478c2013-08-10 12:27:29 +02004149 /* default sdio bus header length for tx packet */
4150 bus->tx_hdrlen = SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN;
4151
4152 /* Attach to the common layer, reserve hdr space */
Hante Meulemanaf5b5e62016-02-17 11:27:08 +01004153 ret = brcmf_attach(bus->sdiodev->dev, bus->sdiodev->settings);
Franky Lin712ac5b2011-12-16 18:37:09 -08004154 if (ret != 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01004155 brcmf_err("brcmf_attach failed\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02004156 goto fail;
4157 }
4158
Hante Meulemane0045bf2016-01-19 12:39:24 +01004159 /* allocate scatter-gather table. sg support
4160 * will be disabled upon allocation failure.
4161 */
4162 brcmf_sdiod_sgtable_alloc(bus->sdiodev);
4163
Daniel Kim7dd3abc2014-05-27 12:56:26 +02004164 /* Query the F2 block size, set roundup accordingly */
4165 bus->blocksize = bus->sdiodev->func[2]->cur_blksize;
4166 bus->roundup = min(max_roundup, bus->blocksize);
4167
Arend van Spriel5b435de2011-10-05 13:19:03 +02004168 /* Allocate buffers */
Arend van Sprielfad13222014-01-06 12:40:40 +01004169 if (bus->sdiodev->bus_if->maxctl) {
Daniel Kim7dd3abc2014-05-27 12:56:26 +02004170 bus->sdiodev->bus_if->maxctl += bus->roundup;
Arend van Sprielfad13222014-01-06 12:40:40 +01004171 bus->rxblen =
4172 roundup((bus->sdiodev->bus_if->maxctl + SDPCM_HDRLEN),
4173 ALIGNMENT) + bus->head_align;
4174 bus->rxbuf = kmalloc(bus->rxblen, GFP_ATOMIC);
4175 if (!(bus->rxbuf)) {
4176 brcmf_err("rxbuf allocation failed\n");
4177 goto fail;
4178 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02004179 }
4180
Arend van Sprielfad13222014-01-06 12:40:40 +01004181 sdio_claim_host(bus->sdiodev->func[1]);
4182
4183 /* Disable F2 to clear any intermediate frame state on the dongle */
4184 sdio_disable_func(bus->sdiodev->func[SDIO_FUNC_2]);
4185
Arend van Sprielfad13222014-01-06 12:40:40 +01004186 bus->rxflow = false;
4187
4188 /* Done with backplane-dependent accesses, can drop clock... */
4189 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, 0, NULL);
4190
4191 sdio_release_host(bus->sdiodev->func[1]);
4192
4193 /* ...and initialize clock/power states */
4194 bus->clkstate = CLK_SDONLY;
4195 bus->idletime = BRCMF_IDLE_INTERVAL;
4196 bus->idleclock = BRCMF_IDLE_ACTIVE;
4197
Arend van Sprielfad13222014-01-06 12:40:40 +01004198 /* SR state */
Arend van Sprielfad13222014-01-06 12:40:40 +01004199 bus->sr_enabled = false;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004200
Arend van Spriel80969832012-06-09 22:51:44 +02004201 brcmf_sdio_debugfs_create(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004202 brcmf_dbg(INFO, "completed!!\n");
4203
Hante Meuleman46d703a2015-11-25 11:32:46 +01004204 ret = brcmf_fw_map_chip_to_name(bus->ci->chip, bus->ci->chiprev,
4205 brcmf_sdio_fwnames,
4206 ARRAY_SIZE(brcmf_sdio_fwnames),
4207 sdiodev->fw_name, sdiodev->nvram_name);
Daniel Kimc1b20532014-07-12 08:49:37 +02004208 if (ret)
4209 goto fail;
4210
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02004211 ret = brcmf_fw_get_firmwares(sdiodev->dev, BRCMF_FW_REQUEST_NVRAM,
Daniel Kimc1b20532014-07-12 08:49:37 +02004212 sdiodev->fw_name, sdiodev->nvram_name,
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02004213 brcmf_sdio_firmware_callback);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004214 if (ret != 0) {
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02004215 brcmf_err("async firmware request failed: %d\n", ret);
Hante Meuleman1799ddf2012-11-14 18:46:17 -08004216 goto fail;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004217 }
Franky Lin15d45b62011-10-21 16:16:32 +02004218
Arend van Spriel5b435de2011-10-05 13:19:03 +02004219 return bus;
4220
4221fail:
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004222 brcmf_sdio_remove(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004223 return NULL;
4224}
4225
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004226/* Detach and free everything */
4227void brcmf_sdio_remove(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004228{
Arend van Spriel5b435de2011-10-05 13:19:03 +02004229 brcmf_dbg(TRACE, "Enter\n");
4230
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004231 if (bus) {
4232 /* De-register interrupt handler */
4233 brcmf_sdiod_intr_unregister(bus->sdiodev);
4234
Arend van Spriel4faf28b2014-05-27 12:56:18 +02004235 brcmf_detach(bus->sdiodev->dev);
Arend van Sprielbfad4a02014-01-06 12:40:39 +01004236
Hante Meulemane0c180ec2014-01-29 15:32:18 +01004237 cancel_work_sync(&bus->datawork);
4238 if (bus->brcmf_wq)
4239 destroy_workqueue(bus->brcmf_wq);
4240
Arend van Sprielbfad4a02014-01-06 12:40:39 +01004241 if (bus->ci) {
Arend van Spriela1ce7a02015-02-06 18:36:42 +01004242 if (bus->sdiodev->state != BRCMF_SDIOD_NOMEDIUM) {
Arend van Sprielbb350712014-01-13 22:20:29 +01004243 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel4011fc42016-01-05 11:05:47 +01004244 brcmf_sdio_wd_timer(bus, false);
Arend van Sprielbb350712014-01-13 22:20:29 +01004245 brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
4246 /* Leave the device in state where it is
Arend van Sprield380ebc2015-03-11 16:11:29 +01004247 * 'passive'. This is done by resetting all
4248 * necessary cores.
Arend van Sprielbb350712014-01-13 22:20:29 +01004249 */
4250 msleep(20);
Arend van Sprield380ebc2015-03-11 16:11:29 +01004251 brcmf_chip_set_passive(bus->ci);
Arend van Sprielbb350712014-01-13 22:20:29 +01004252 brcmf_sdio_clkctl(bus, CLK_NONE, false);
4253 sdio_release_host(bus->sdiodev->func[1]);
4254 }
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01004255 brcmf_chip_detach(bus->ci);
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004256 }
Hante Meulemanaf5b5e62016-02-17 11:27:08 +01004257 if (bus->sdiodev->settings)
4258 brcmf_release_module_param(bus->sdiodev->settings);
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004259
Arend van Sprielbfad4a02014-01-06 12:40:39 +01004260 kfree(bus->rxbuf);
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004261 kfree(bus->hdrbuf);
4262 kfree(bus);
4263 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02004264
4265 brcmf_dbg(TRACE, "Disconnected\n");
4266}
4267
Arend van Spriel4011fc42016-01-05 11:05:47 +01004268void brcmf_sdio_wd_timer(struct brcmf_sdio *bus, bool active)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004269{
Arend van Spriel5b435de2011-10-05 13:19:03 +02004270 /* Totally stop the timer */
Arend van Spriel4011fc42016-01-05 11:05:47 +01004271 if (!active && bus->wd_active) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02004272 del_timer_sync(&bus->timer);
Arend van Spriel4011fc42016-01-05 11:05:47 +01004273 bus->wd_active = false;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004274 return;
4275 }
4276
Franky Linece960e2011-10-21 16:16:19 +02004277 /* don't start the wd until fw is loaded */
Arend van Spriela1ce7a02015-02-06 18:36:42 +01004278 if (bus->sdiodev->state != BRCMF_SDIOD_DATA)
Franky Linece960e2011-10-21 16:16:19 +02004279 return;
4280
Arend van Spriel4011fc42016-01-05 11:05:47 +01004281 if (active) {
4282 if (!bus->wd_active) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02004283 /* Create timer again when watchdog period is
4284 dynamically changed or in the first instance
4285 */
Arend van Spriel63ce3d52016-01-05 11:05:48 +01004286 bus->timer.expires = jiffies + BRCMF_WD_POLL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004287 add_timer(&bus->timer);
Arend van Spriel4011fc42016-01-05 11:05:47 +01004288 bus->wd_active = true;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004289 } else {
4290 /* Re arm the timer, at last watchdog period */
Arend van Spriel63ce3d52016-01-05 11:05:48 +01004291 mod_timer(&bus->timer, jiffies + BRCMF_WD_POLL);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004292 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02004293 }
4294}
Arend van Spriel99824642015-02-06 18:36:45 +01004295
4296int brcmf_sdio_sleep(struct brcmf_sdio *bus, bool sleep)
4297{
4298 int ret;
4299
4300 sdio_claim_host(bus->sdiodev->func[1]);
4301 ret = brcmf_sdio_bus_sleep(bus, sleep, false);
4302 sdio_release_host(bus->sdiodev->func[1]);
4303
4304 return ret;
4305}
4306