blob: fc64b8913aa6a11c0111fec3b9d900174dc250c3 [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>
Ingo Molnar3f07c012017-02-08 18:51:30 +010025#include <linux/sched/signal.h>
Arend van Spriel5b435de2011-10-05 13:19:03 +020026#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"
Franky Lin20ec4f52017-03-10 21:17:02 +000047#include "bcdc.h"
Arend van Spriel5b435de2011-10-05 13:19:03 +020048
Sjoerd Simons97f1a172016-01-25 11:47:29 +010049#define DCMD_RESP_TIMEOUT msecs_to_jiffies(2500)
50#define CTL_DONE_TIMEOUT msecs_to_jiffies(2500)
Arend van Spriel5b435de2011-10-05 13:19:03 +020051
Joe Perches8ae74652012-01-15 00:38:38 -080052#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +020053
54#define BRCMF_TRAP_INFO_SIZE 80
55
56#define CBUF_LEN (128)
57
Arend van Spriel4fc0d012012-06-14 12:16:57 +020058/* Device console log buffer state */
59#define CONSOLE_BUFFER_MAX 2024
60
Arend van Spriel5b435de2011-10-05 13:19:03 +020061struct rte_log_le {
62 __le32 buf; /* Can't be pointer on (64-bit) hosts */
63 __le32 buf_size;
64 __le32 idx;
65 char *_buf_compat; /* Redundant pointer for backward compat. */
66};
67
68struct rte_console {
69 /* Virtual UART
70 * When there is no UART (e.g. Quickturn),
71 * the host should write a complete
72 * input line directly into cbuf and then write
73 * the length into vcons_in.
74 * This may also be used when there is a real UART
75 * (at risk of conflicting with
76 * the real UART). vcons_out is currently unused.
77 */
78 uint vcons_in;
79 uint vcons_out;
80
81 /* Output (logging) buffer
82 * Console output is written to a ring buffer log_buf at index log_idx.
83 * The host may read the output when it sees log_idx advance.
84 * Output will be lost if the output wraps around faster than the host
85 * polls.
86 */
87 struct rte_log_le log_le;
88
89 /* Console input line buffer
90 * Characters are read one at a time into cbuf
91 * until <CR> is received, then
92 * the buffer is processed as a command line.
93 * Also used for virtual UART.
94 */
95 uint cbuf_idx;
96 char cbuf[CBUF_LEN];
97};
98
Joe Perches8ae74652012-01-15 00:38:38 -080099#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200100#include <chipcommon.h>
101
Hante Meulemand14f78b2014-10-28 14:56:14 +0100102#include "bus.h"
Hante Meulemana8e8ed32014-10-28 14:56:13 +0100103#include "debug.h"
Arend van Spriel40c1c242013-04-05 10:57:44 +0200104#include "tracepoint.h"
Arend van Spriel5b435de2011-10-05 13:19:03 +0200105
106#define TXQLEN 2048 /* bulk tx queue length */
107#define TXHI (TXQLEN - 256) /* turn on flow control above TXHI */
108#define TXLOW (TXHI - 256) /* turn off flow control below TXLOW */
109#define PRIOMASK 7
110
111#define TXRETRIES 2 /* # of retries for tx frames */
112
113#define BRCMF_RXBOUND 50 /* Default for max rx frames in
114 one scheduling */
115
116#define BRCMF_TXBOUND 20 /* Default for max tx frames in
117 one scheduling */
118
119#define BRCMF_TXMINMAX 1 /* Max tx frames if rx still pending */
120
121#define MEMBLOCK 2048 /* Block size used for downloading
122 of dongle image */
123#define MAX_DATA_BUF (32 * 1024) /* Must be large enough to hold
124 biggest possible glom */
125
126#define BRCMF_FIRSTREAD (1 << 6)
127
Arend van Spriel9d6c1dc2015-08-26 22:15:01 +0200128#define BRCMF_CONSOLE 10 /* watchdog interval to poll console */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200129
130/* SBSDIO_DEVICE_CTL */
131
132/* 1: device will assert busy signal when receiving CMD53 */
133#define SBSDIO_DEVCTL_SETBUSY 0x01
134/* 1: assertion of sdio interrupt is synchronous to the sdio clock */
135#define SBSDIO_DEVCTL_SPI_INTR_SYNC 0x02
136/* 1: mask all interrupts to host except the chipActive (rev 8) */
137#define SBSDIO_DEVCTL_CA_INT_ONLY 0x04
138/* 1: isolate internal sdio signals, put external pads in tri-state; requires
139 * sdio bus power cycle to clear (rev 9) */
140#define SBSDIO_DEVCTL_PADS_ISO 0x08
141/* Force SD->SB reset mapping (rev 11) */
142#define SBSDIO_DEVCTL_SB_RST_CTL 0x30
143/* Determined by CoreControl bit */
144#define SBSDIO_DEVCTL_RST_CORECTL 0x00
145/* Force backplane reset */
146#define SBSDIO_DEVCTL_RST_BPRESET 0x10
147/* Force no backplane reset */
148#define SBSDIO_DEVCTL_RST_NOBPRESET 0x20
149
Arend van Spriel5b435de2011-10-05 13:19:03 +0200150/* direct(mapped) cis space */
151
152/* MAPPED common CIS address */
153#define SBSDIO_CIS_BASE_COMMON 0x1000
154/* maximum bytes in one CIS */
155#define SBSDIO_CIS_SIZE_LIMIT 0x200
156/* cis offset addr is < 17 bits */
157#define SBSDIO_CIS_OFT_ADDR_MASK 0x1FFFF
158
159/* manfid tuple length, include tuple, link bytes */
160#define SBSDIO_CIS_MANFID_TUPLE_LEN 6
161
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100162#define CORE_BUS_REG(base, field) \
163 (base + offsetof(struct sdpcmd_regs, field))
164
165/* SDIO function 1 register CHIPCLKCSR */
166/* Force ALP request to backplane */
167#define SBSDIO_FORCE_ALP 0x01
168/* Force HT request to backplane */
169#define SBSDIO_FORCE_HT 0x02
170/* Force ILP request to backplane */
171#define SBSDIO_FORCE_ILP 0x04
172/* Make ALP ready (power up xtal) */
173#define SBSDIO_ALP_AVAIL_REQ 0x08
174/* Make HT ready (power up PLL) */
175#define SBSDIO_HT_AVAIL_REQ 0x10
176/* Squelch clock requests from HW */
177#define SBSDIO_FORCE_HW_CLKREQ_OFF 0x20
178/* Status: ALP is ready */
179#define SBSDIO_ALP_AVAIL 0x40
180/* Status: HT is ready */
181#define SBSDIO_HT_AVAIL 0x80
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100182#define SBSDIO_CSR_MASK 0x1F
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100183#define SBSDIO_AVBITS (SBSDIO_HT_AVAIL | SBSDIO_ALP_AVAIL)
184#define SBSDIO_ALPAV(regval) ((regval) & SBSDIO_AVBITS)
185#define SBSDIO_HTAV(regval) (((regval) & SBSDIO_AVBITS) == SBSDIO_AVBITS)
186#define SBSDIO_ALPONLY(regval) (SBSDIO_ALPAV(regval) && !SBSDIO_HTAV(regval))
187#define SBSDIO_CLKAV(regval, alponly) \
188 (SBSDIO_ALPAV(regval) && (alponly ? 1 : SBSDIO_HTAV(regval)))
189
Arend van Spriel5b435de2011-10-05 13:19:03 +0200190/* intstatus */
191#define I_SMB_SW0 (1 << 0) /* To SB Mail S/W interrupt 0 */
192#define I_SMB_SW1 (1 << 1) /* To SB Mail S/W interrupt 1 */
193#define I_SMB_SW2 (1 << 2) /* To SB Mail S/W interrupt 2 */
194#define I_SMB_SW3 (1 << 3) /* To SB Mail S/W interrupt 3 */
195#define I_SMB_SW_MASK 0x0000000f /* To SB Mail S/W interrupts mask */
196#define I_SMB_SW_SHIFT 0 /* To SB Mail S/W interrupts shift */
197#define I_HMB_SW0 (1 << 4) /* To Host Mail S/W interrupt 0 */
198#define I_HMB_SW1 (1 << 5) /* To Host Mail S/W interrupt 1 */
199#define I_HMB_SW2 (1 << 6) /* To Host Mail S/W interrupt 2 */
200#define I_HMB_SW3 (1 << 7) /* To Host Mail S/W interrupt 3 */
201#define I_HMB_SW_MASK 0x000000f0 /* To Host Mail S/W interrupts mask */
202#define I_HMB_SW_SHIFT 4 /* To Host Mail S/W interrupts shift */
203#define I_WR_OOSYNC (1 << 8) /* Write Frame Out Of Sync */
204#define I_RD_OOSYNC (1 << 9) /* Read Frame Out Of Sync */
205#define I_PC (1 << 10) /* descriptor error */
206#define I_PD (1 << 11) /* data error */
207#define I_DE (1 << 12) /* Descriptor protocol Error */
208#define I_RU (1 << 13) /* Receive descriptor Underflow */
209#define I_RO (1 << 14) /* Receive fifo Overflow */
210#define I_XU (1 << 15) /* Transmit fifo Underflow */
211#define I_RI (1 << 16) /* Receive Interrupt */
212#define I_BUSPWR (1 << 17) /* SDIO Bus Power Change (rev 9) */
213#define I_XMTDATA_AVAIL (1 << 23) /* bits in fifo */
214#define I_XI (1 << 24) /* Transmit Interrupt */
215#define I_RF_TERM (1 << 25) /* Read Frame Terminate */
216#define I_WF_TERM (1 << 26) /* Write Frame Terminate */
217#define I_PCMCIA_XU (1 << 27) /* PCMCIA Transmit FIFO Underflow */
218#define I_SBINT (1 << 28) /* sbintstatus Interrupt */
219#define I_CHIPACTIVE (1 << 29) /* chip from doze to active state */
220#define I_SRESET (1 << 30) /* CCCR RES interrupt */
221#define I_IOE2 (1U << 31) /* CCCR IOE2 Bit Changed */
222#define I_ERRORS (I_PC | I_PD | I_DE | I_RU | I_RO | I_XU)
223#define I_DMA (I_RI | I_XI | I_ERRORS)
224
225/* corecontrol */
226#define CC_CISRDY (1 << 0) /* CIS Ready */
227#define CC_BPRESEN (1 << 1) /* CCCR RES signal */
228#define CC_F2RDY (1 << 2) /* set CCCR IOR2 bit */
229#define CC_CLRPADSISO (1 << 3) /* clear SDIO pads isolation */
230#define CC_XMTDATAAVAIL_MODE (1 << 4)
231#define CC_XMTDATAAVAIL_CTRL (1 << 5)
232
233/* SDA_FRAMECTRL */
234#define SFC_RF_TERM (1 << 0) /* Read Frame Terminate */
235#define SFC_WF_TERM (1 << 1) /* Write Frame Terminate */
236#define SFC_CRC4WOOS (1 << 2) /* CRC error for write out of sync */
237#define SFC_ABORTALL (1 << 3) /* Abort all in-progress frames */
238
Arend van Spriel5b435de2011-10-05 13:19:03 +0200239/*
240 * Software allocation of To SB Mailbox resources
241 */
242
243/* tosbmailbox bits corresponding to intstatus bits */
244#define SMB_NAK (1 << 0) /* Frame NAK */
245#define SMB_INT_ACK (1 << 1) /* Host Interrupt ACK */
246#define SMB_USE_OOB (1 << 2) /* Use OOB Wakeup */
247#define SMB_DEV_INT (1 << 3) /* Miscellaneous Interrupt */
248
249/* tosbmailboxdata */
250#define SMB_DATA_VERSION_SHIFT 16 /* host protocol version */
251
252/*
253 * Software allocation of To Host Mailbox resources
254 */
255
256/* intstatus bits */
257#define I_HMB_FC_STATE I_HMB_SW0 /* Flow Control State */
258#define I_HMB_FC_CHANGE I_HMB_SW1 /* Flow Control State Changed */
259#define I_HMB_FRAME_IND I_HMB_SW2 /* Frame Indication */
260#define I_HMB_HOST_INT I_HMB_SW3 /* Miscellaneous Interrupt */
261
262/* tohostmailboxdata */
263#define HMB_DATA_NAKHANDLED 1 /* retransmit NAK'd frame */
264#define HMB_DATA_DEVREADY 2 /* talk to host after enable */
265#define HMB_DATA_FC 4 /* per prio flowcontrol update flag */
266#define HMB_DATA_FWREADY 8 /* fw ready for protocol activity */
267
268#define HMB_DATA_FCDATA_MASK 0xff000000
269#define HMB_DATA_FCDATA_SHIFT 24
270
271#define HMB_DATA_VERSION_MASK 0x00ff0000
272#define HMB_DATA_VERSION_SHIFT 16
273
274/*
275 * Software-defined protocol header
276 */
277
278/* Current protocol version */
279#define SDPCM_PROT_VERSION 4
280
Arend van Spriel5b435de2011-10-05 13:19:03 +0200281/*
282 * Shared structure between dongle and the host.
283 * The structure contains pointers to trap or assert information.
284 */
Arend van Spriel4fc0d012012-06-14 12:16:57 +0200285#define SDPCM_SHARED_VERSION 0x0003
Arend van Spriel5b435de2011-10-05 13:19:03 +0200286#define SDPCM_SHARED_VERSION_MASK 0x00FF
287#define SDPCM_SHARED_ASSERT_BUILT 0x0100
288#define SDPCM_SHARED_ASSERT 0x0200
289#define SDPCM_SHARED_TRAP 0x0400
290
291/* Space for header read, limit for data packets */
292#define MAX_HDR_READ (1 << 6)
293#define MAX_RX_DATASZ 2048
294
Arend van Spriel5b435de2011-10-05 13:19:03 +0200295/* Bump up limit on waiting for HT to account for first startup;
296 * if the image is doing a CRC calculation before programming the PMU
297 * for HT availability, it could take a couple hundred ms more, so
298 * max out at a 1 second (1000000us).
299 */
300#undef PMU_MAX_TRANSITION_DLY
301#define PMU_MAX_TRANSITION_DLY 1000000
302
303/* Value for ChipClockCSR during initial setup */
304#define BRCMF_INIT_CLKCTL1 (SBSDIO_FORCE_HW_CLKREQ_OFF | \
305 SBSDIO_ALP_AVAIL_REQ)
306
307/* Flags for SDH calls */
308#define F2SYNC (SDIO_REQ_4BYTE | SDIO_REQ_FIXED)
309
Franky Lin382a9e02011-12-16 18:37:18 -0800310#define BRCMF_IDLE_ACTIVE 0 /* Do not request any SD clock change
311 * when idle
312 */
313#define BRCMF_IDLE_INTERVAL 1
314
Piotr Haber4a3da992013-04-11 13:28:46 +0200315#define KSO_WAIT_US 50
316#define MAX_KSO_ATTEMPTS (PMU_MAX_TRANSITION_DLY/KSO_WAIT_US)
Arend Van Spriel5251b6b2016-09-05 11:42:13 +0100317#define BRCMF_SDIO_MAX_ACCESS_ERRORS 5
Piotr Haber4a3da992013-04-11 13:28:46 +0200318
Arend van Spriel5b435de2011-10-05 13:19:03 +0200319/*
320 * Conversion of 802.1D priority to precedence level
321 */
322static uint prio2prec(u32 prio)
323{
324 return (prio == PRIO_8021D_NONE || prio == PRIO_8021D_BE) ?
325 (prio^2) : prio;
326}
327
Joe Perches8ae74652012-01-15 00:38:38 -0800328#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200329/* Device console log buffer state */
330struct brcmf_console {
331 uint count; /* Poll interval msec counter */
332 uint log_addr; /* Log struct address (fixed) */
333 struct rte_log_le log_le; /* Log struct (host copy) */
334 uint bufsize; /* Size of log buffer */
335 u8 *buf; /* Log buffer (host copy) */
336 uint last; /* Last buffer read index */
337};
Arend van Spriel4fc0d012012-06-14 12:16:57 +0200338
339struct brcmf_trap_info {
340 __le32 type;
341 __le32 epc;
342 __le32 cpsr;
343 __le32 spsr;
344 __le32 r0; /* a1 */
345 __le32 r1; /* a2 */
346 __le32 r2; /* a3 */
347 __le32 r3; /* a4 */
348 __le32 r4; /* v1 */
349 __le32 r5; /* v2 */
350 __le32 r6; /* v3 */
351 __le32 r7; /* v4 */
352 __le32 r8; /* v5 */
353 __le32 r9; /* sb/v6 */
354 __le32 r10; /* sl/v7 */
355 __le32 r11; /* fp/v8 */
356 __le32 r12; /* ip */
357 __le32 r13; /* sp */
358 __le32 r14; /* lr */
359 __le32 pc; /* r15 */
360};
Joe Perches8ae74652012-01-15 00:38:38 -0800361#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200362
363struct sdpcm_shared {
364 u32 flags;
365 u32 trap_addr;
366 u32 assert_exp_addr;
367 u32 assert_file_addr;
368 u32 assert_line;
369 u32 console_addr; /* Address of struct rte_console */
370 u32 msgtrace_addr;
371 u8 tag[32];
Arend van Spriel4fc0d012012-06-14 12:16:57 +0200372 u32 brpt_addr;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200373};
374
375struct sdpcm_shared_le {
376 __le32 flags;
377 __le32 trap_addr;
378 __le32 assert_exp_addr;
379 __le32 assert_file_addr;
380 __le32 assert_line;
381 __le32 console_addr; /* Address of struct rte_console */
382 __le32 msgtrace_addr;
383 u8 tag[32];
Arend van Spriel4fc0d012012-06-14 12:16:57 +0200384 __le32 brpt_addr;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200385};
386
Franky Lin6bc52312013-08-10 12:27:28 +0200387/* dongle SDIO bus specific header info */
388struct brcmf_sdio_hdrinfo {
Franky Lin4754fce2012-09-19 22:21:11 +0200389 u8 seq_num;
390 u8 channel;
391 u16 len;
392 u16 len_left;
393 u16 len_nxtfrm;
394 u8 dat_offset;
Franky Lin8da9d2c2013-11-29 12:25:10 +0100395 bool lastfrm;
396 u16 tail_pad;
Franky Lin4754fce2012-09-19 22:21:11 +0200397};
Arend van Spriel5b435de2011-10-05 13:19:03 +0200398
Arend van Spriel82d957e2014-07-12 08:49:36 +0200399/*
400 * hold counter variables
401 */
402struct brcmf_sdio_count {
403 uint intrcount; /* Count of device interrupt callbacks */
404 uint lastintrs; /* Count as of last watchdog timer */
405 uint pollcnt; /* Count of active polls */
406 uint regfails; /* Count of R_REG failures */
407 uint tx_sderrs; /* Count of tx attempts with sd errors */
408 uint fcqueued; /* Tx packets that got queued */
409 uint rxrtx; /* Count of rtx requests (NAK to dongle) */
410 uint rx_toolong; /* Receive frames too long to receive */
411 uint rxc_errors; /* SDIO errors when reading control frames */
412 uint rx_hdrfail; /* SDIO errors on header reads */
413 uint rx_badhdr; /* Bad received headers (roosync?) */
414 uint rx_badseq; /* Mismatched rx sequence number */
415 uint fc_rcvd; /* Number of flow-control events received */
416 uint fc_xoff; /* Number which turned on flow-control */
417 uint fc_xon; /* Number which turned off flow-control */
418 uint rxglomfail; /* Failed deglom attempts */
419 uint rxglomframes; /* Number of glom frames (superframes) */
420 uint rxglompkts; /* Number of packets from glom frames */
421 uint f2rxhdrs; /* Number of header reads */
422 uint f2rxdata; /* Number of frame data reads */
423 uint f2txdata; /* Number of f2 frame writes */
424 uint f1regdata; /* Number of f1 register accesses */
425 uint tickcnt; /* Number of watchdog been schedule */
426 ulong tx_ctlerrs; /* Err of sending ctrl frames */
427 ulong tx_ctlpkts; /* Ctrl frames sent to dongle */
428 ulong rx_ctlerrs; /* Err of processing rx ctrl frames */
429 ulong rx_ctlpkts; /* Ctrl frames processed from dongle */
430 ulong rx_readahead_cnt; /* packets where header read-ahead was used */
431};
432
Arend van Spriel5b435de2011-10-05 13:19:03 +0200433/* misc chip info needed by some of the routines */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200434/* Private data for SDIO bus interaction */
Franky Line92eedf2011-11-22 17:21:50 -0800435struct brcmf_sdio {
Arend van Spriel5b435de2011-10-05 13:19:03 +0200436 struct brcmf_sdio_dev *sdiodev; /* sdio device handler */
Arend van Spriel9cf218f2014-01-13 22:20:27 +0100437 struct brcmf_chip *ci; /* Chip info struct */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200438
Arend van Spriel5b435de2011-10-05 13:19:03 +0200439 u32 hostintmask; /* Copy of Host Interrupt Mask */
Franky Lin45316032012-09-13 21:12:03 +0200440 atomic_t intstatus; /* Intstatus bits (events) pending */
441 atomic_t fcstate; /* State of dongle flow-control */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200442
443 uint blocksize; /* Block size of SDIO transfers */
444 uint roundup; /* Max roundup limit */
445
446 struct pktq txq; /* Queue length used for flow-control */
447 u8 flowcontrol; /* per prio flow control bitmask */
448 u8 tx_seq; /* Transmit sequence number (next) */
449 u8 tx_max; /* Maximum transmit sequence allowed */
450
Arend van Spriel9b2d2f2a82013-11-29 12:25:14 +0100451 u8 *hdrbuf; /* buffer for handling rx frame */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200452 u8 *rxhdr; /* Header of current rx frame (in hdrbuf) */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200453 u8 rx_seq; /* Receive sequence number (expected) */
Franky Lin6bc52312013-08-10 12:27:28 +0200454 struct brcmf_sdio_hdrinfo cur_read;
Franky Lin4754fce2012-09-19 22:21:11 +0200455 /* info of current read frame */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200456 bool rxskip; /* Skip receive (awaiting NAK ACK) */
Franky Lin4754fce2012-09-19 22:21:11 +0200457 bool rxpending; /* Data frame pending in dongle */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200458
459 uint rxbound; /* Rx frames to read before resched */
460 uint txbound; /* Tx frames to send before resched */
461 uint txminmax;
462
463 struct sk_buff *glomd; /* Packet containing glomming descriptor */
Arend van Sprielb83db862011-10-19 12:51:09 +0200464 struct sk_buff_head glom; /* Packet list for glommed superframe */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200465
466 u8 *rxbuf; /* Buffer for receiving control packets */
467 uint rxblen; /* Allocated length of rxbuf */
468 u8 *rxctl; /* Aligned pointer into rxbuf */
Franky Lindd43a012012-11-05 16:22:22 -0800469 u8 *rxctl_orig; /* pointer for freeing rxctl */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200470 uint rxlen; /* Length of valid data in buffer */
Franky Lindd43a012012-11-05 16:22:22 -0800471 spinlock_t rxctl_lock; /* protection lock for ctrl frame resources */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200472
473 u8 sdpcm_ver; /* Bus protocol reported by dongle */
474
475 bool intr; /* Use interrupts */
476 bool poll; /* Use polling */
Franky Lin1d382272012-09-13 21:11:59 +0200477 atomic_t ipend; /* Device interrupt is pending */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200478 uint spurious; /* Count of spurious interrupts */
479 uint pollrate; /* Ticks between device polls */
480 uint polltick; /* Tick counter */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200481
Joe Perches8ae74652012-01-15 00:38:38 -0800482#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200483 uint console_interval;
484 struct brcmf_console console; /* Console output polling support */
485 uint console_addr; /* Console address from shared struct */
Joe Perches8ae74652012-01-15 00:38:38 -0800486#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200487
Arend van Spriel5b435de2011-10-05 13:19:03 +0200488 uint clkstate; /* State of sd and backplane clock(s) */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200489 s32 idletime; /* Control for activity timeout */
Hante Meulemanb441ba82015-03-06 18:40:39 +0100490 s32 idlecount; /* Activity timeout counter */
491 s32 idleclock; /* How to set bus driver when idle */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200492 bool rxflow_mode; /* Rx flow control mode */
493 bool rxflow; /* Is rx flow control on */
494 bool alp_only; /* Don't use HT clock (ALP only) */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200495
Arend van Spriel5b435de2011-10-05 13:19:03 +0200496 u8 *ctrl_frame_buf;
Hante Meulemanfed7ec42014-03-15 17:18:18 +0100497 u16 ctrl_frame_len;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200498 bool ctrl_frame_stat;
Hante Meuleman4dd8b262015-01-25 20:31:32 +0100499 int ctrl_frame_err;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200500
Hante Meulemanfed7ec42014-03-15 17:18:18 +0100501 spinlock_t txq_lock; /* protect bus->txq */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200502 wait_queue_head_t ctrl_wait;
503 wait_queue_head_t dcmd_resp_wait;
504
505 struct timer_list timer;
506 struct completion watchdog_wait;
507 struct task_struct *watchdog_tsk;
Arend van Spriel4011fc42016-01-05 11:05:47 +0100508 bool wd_active;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200509
Franky Linf1e68c22012-09-13 21:12:00 +0200510 struct workqueue_struct *brcmf_wq;
511 struct work_struct datawork;
Hante Meuleman2c64e162015-03-18 13:25:22 +0100512 bool dpc_triggered;
513 bool dpc_running;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200514
Franky Linc8bf3482011-12-16 18:37:08 -0800515 bool txoff; /* Transmit flow-controlled */
Arend van Spriel80969832012-06-09 22:51:44 +0200516 struct brcmf_sdio_count sdcnt;
Piotr Haber4a3da992013-04-11 13:28:46 +0200517 bool sr_enabled; /* SaveRestore enabled */
Arend van Spriel99824642015-02-06 18:36:45 +0100518 bool sleeping;
Franky Lin706478c2013-08-10 12:27:29 +0200519
520 u8 tx_hdrlen; /* sdio bus header length for tx packet */
Franky Lin8da9d2c2013-11-29 12:25:10 +0100521 bool txglom; /* host tx glomming enable flag */
Arend van Spriele217d1c2013-11-29 12:25:13 +0100522 u16 head_align; /* buffer pointer alignment */
523 u16 sgentry_align; /* scatter-gather buffer alignment */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200524};
525
Arend van Spriel5b435de2011-10-05 13:19:03 +0200526/* clkstate */
527#define CLK_NONE 0
528#define CLK_SDONLY 1
Piotr Haber4a3da992013-04-11 13:28:46 +0200529#define CLK_PENDING 2
Arend van Spriel5b435de2011-10-05 13:19:03 +0200530#define CLK_AVAIL 3
531
Joe Perches8ae74652012-01-15 00:38:38 -0800532#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200533static int qcount[NUMPRIO];
Joe Perches8ae74652012-01-15 00:38:38 -0800534#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200535
Hante Meuleman668761a2013-04-12 10:55:55 +0200536#define DEFAULT_SDIO_DRIVE_STRENGTH 6 /* in milliamps */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200537
538#define RETRYCHAN(chan) ((chan) == SDPCM_EVENT_CHANNEL)
539
Arend van Spriel5b435de2011-10-05 13:19:03 +0200540/* Limit on rounding up frames */
541static const uint max_roundup = 512;
542
Heiner Kallweit6e84ab62017-04-05 20:33:26 +0200543#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
544#define ALIGNMENT 8
545#else
Arend van Spriel5b435de2011-10-05 13:19:03 +0200546#define ALIGNMENT 4
Heiner Kallweit6e84ab62017-04-05 20:33:26 +0200547#endif
Arend van Spriel5b435de2011-10-05 13:19:03 +0200548
Franky Lin9d7d6f92012-10-22 10:36:24 -0700549enum brcmf_sdio_frmtype {
550 BRCMF_SDIO_FT_NORMAL,
551 BRCMF_SDIO_FT_SUPER,
552 BRCMF_SDIO_FT_SUB,
553};
554
Arend van Spriel65d80d02014-01-29 15:32:14 +0100555#define SDIOD_DRVSTR_KEY(chip, pmu) (((chip) << 16) | (pmu))
556
557/* SDIO Pad drive strength to select value mappings */
558struct sdiod_drive_str {
559 u8 strength; /* Pad Drive Strength in mA */
560 u8 sel; /* Chip-specific select value */
561};
562
563/* SDIO Drive Strength to sel value table for PMU Rev 11 (1.8V) */
564static const struct sdiod_drive_str sdiod_drvstr_tab1_1v8[] = {
565 {32, 0x6},
566 {26, 0x7},
567 {22, 0x4},
568 {16, 0x5},
569 {12, 0x2},
570 {8, 0x3},
571 {4, 0x0},
572 {0, 0x1}
573};
574
575/* SDIO Drive Strength to sel value table for PMU Rev 13 (1.8v) */
576static const struct sdiod_drive_str sdiod_drive_strength_tab5_1v8[] = {
577 {6, 0x7},
578 {5, 0x6},
579 {4, 0x5},
580 {3, 0x4},
581 {2, 0x2},
582 {1, 0x1},
583 {0, 0x0}
584};
585
586/* SDIO Drive Strength to sel value table for PMU Rev 17 (1.8v) */
587static const struct sdiod_drive_str sdiod_drvstr_tab6_1v8[] = {
588 {3, 0x3},
589 {2, 0x2},
590 {1, 0x1},
591 {0, 0x0} };
592
593/* SDIO Drive Strength to sel value table for 43143 PMU Rev 17 (3.3V) */
594static const struct sdiod_drive_str sdiod_drvstr_tab2_3v3[] = {
595 {16, 0x7},
596 {12, 0x5},
597 {8, 0x3},
598 {4, 0x1}
599};
600
Hante Meuleman46d703a2015-11-25 11:32:46 +0100601BRCMF_FW_NVRAM_DEF(43143, "brcmfmac43143-sdio.bin", "brcmfmac43143-sdio.txt");
602BRCMF_FW_NVRAM_DEF(43241B0, "brcmfmac43241b0-sdio.bin",
603 "brcmfmac43241b0-sdio.txt");
604BRCMF_FW_NVRAM_DEF(43241B4, "brcmfmac43241b4-sdio.bin",
605 "brcmfmac43241b4-sdio.txt");
606BRCMF_FW_NVRAM_DEF(43241B5, "brcmfmac43241b5-sdio.bin",
607 "brcmfmac43241b5-sdio.txt");
608BRCMF_FW_NVRAM_DEF(4329, "brcmfmac4329-sdio.bin", "brcmfmac4329-sdio.txt");
609BRCMF_FW_NVRAM_DEF(4330, "brcmfmac4330-sdio.bin", "brcmfmac4330-sdio.txt");
610BRCMF_FW_NVRAM_DEF(4334, "brcmfmac4334-sdio.bin", "brcmfmac4334-sdio.txt");
611BRCMF_FW_NVRAM_DEF(43340, "brcmfmac43340-sdio.bin", "brcmfmac43340-sdio.txt");
612BRCMF_FW_NVRAM_DEF(4335, "brcmfmac4335-sdio.bin", "brcmfmac4335-sdio.txt");
613BRCMF_FW_NVRAM_DEF(43362, "brcmfmac43362-sdio.bin", "brcmfmac43362-sdio.txt");
614BRCMF_FW_NVRAM_DEF(4339, "brcmfmac4339-sdio.bin", "brcmfmac4339-sdio.txt");
615BRCMF_FW_NVRAM_DEF(43430, "brcmfmac43430-sdio.bin", "brcmfmac43430-sdio.txt");
616BRCMF_FW_NVRAM_DEF(43455, "brcmfmac43455-sdio.bin", "brcmfmac43455-sdio.txt");
617BRCMF_FW_NVRAM_DEF(4354, "brcmfmac4354-sdio.bin", "brcmfmac4354-sdio.txt");
Christian Daudt496aec52016-05-04 17:55:20 -0700618BRCMF_FW_NVRAM_DEF(4356, "brcmfmac4356-sdio.bin", "brcmfmac4356-sdio.txt");
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200619
Hante Meuleman46d703a2015-11-25 11:32:46 +0100620static struct brcmf_firmware_mapping brcmf_sdio_fwnames[] = {
621 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_43143_CHIP_ID, 0xFFFFFFFF, 43143),
622 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_43241_CHIP_ID, 0x0000001F, 43241B0),
623 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_43241_CHIP_ID, 0x00000020, 43241B4),
624 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_43241_CHIP_ID, 0xFFFFFFC0, 43241B5),
625 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_4329_CHIP_ID, 0xFFFFFFFF, 4329),
626 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_4330_CHIP_ID, 0xFFFFFFFF, 4330),
627 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_4334_CHIP_ID, 0xFFFFFFFF, 4334),
628 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_43340_CHIP_ID, 0xFFFFFFFF, 43340),
Arend Van Sprieldc630dc2016-11-23 10:25:21 +0000629 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_43341_CHIP_ID, 0xFFFFFFFF, 43340),
Hante Meuleman46d703a2015-11-25 11:32:46 +0100630 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_4335_CHIP_ID, 0xFFFFFFFF, 4335),
631 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_43362_CHIP_ID, 0xFFFFFFFE, 43362),
632 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_4339_CHIP_ID, 0xFFFFFFFF, 4339),
633 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_43430_CHIP_ID, 0xFFFFFFFF, 43430),
634 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_4345_CHIP_ID, 0xFFFFFFC0, 43455),
Christian Daudt496aec52016-05-04 17:55:20 -0700635 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_4354_CHIP_ID, 0xFFFFFFFF, 4354),
636 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_4356_CHIP_ID, 0xFFFFFFFF, 4356)
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200637};
638
Arend van Spriel5b435de2011-10-05 13:19:03 +0200639static void pkt_align(struct sk_buff *p, int len, int align)
640{
641 uint datalign;
642 datalign = (unsigned long)(p->data);
643 datalign = roundup(datalign, (align)) - datalign;
644 if (datalign)
645 skb_pull(p, datalign);
646 __skb_trim(p, len);
647}
648
649/* To check if there's window offered */
Franky Line92eedf2011-11-22 17:21:50 -0800650static bool data_ok(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200651{
652 return (u8)(bus->tx_max - bus->tx_seq) != 0 &&
653 ((u8)(bus->tx_max - bus->tx_seq) & 0x80) == 0;
654}
655
656/*
657 * Reads a register in the SDIO hardware block. This block occupies a series of
658 * adresses on the 32 bit backplane bus.
659 */
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100660static int r_sdreg32(struct brcmf_sdio *bus, u32 *regvar, u32 offset)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200661{
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100662 struct brcmf_core *core;
Franky Lin79ae3952012-05-04 18:27:34 -0700663 int ret;
Franky Lin58692752012-05-04 18:27:36 -0700664
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100665 core = brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV);
666 *regvar = brcmf_sdiod_regrl(bus->sdiodev, core->base + offset, &ret);
Franky Lin58692752012-05-04 18:27:36 -0700667
668 return ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200669}
670
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100671static int w_sdreg32(struct brcmf_sdio *bus, u32 regval, u32 reg_offset)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200672{
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100673 struct brcmf_core *core;
Franky Line13ce262012-05-04 18:27:35 -0700674 int ret;
Franky Lin58692752012-05-04 18:27:36 -0700675
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100676 core = brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV);
677 brcmf_sdiod_regwl(bus->sdiodev, core->base + reg_offset, regval, &ret);
Franky Lin58692752012-05-04 18:27:36 -0700678
679 return ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200680}
681
Piotr Haber4a3da992013-04-11 13:28:46 +0200682static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100683brcmf_sdio_kso_control(struct brcmf_sdio *bus, bool on)
Piotr Haber4a3da992013-04-11 13:28:46 +0200684{
685 u8 wr_val = 0, rd_val, cmp_val, bmask;
686 int err = 0;
Arend Van Spriel5251b6b2016-09-05 11:42:13 +0100687 int err_cnt = 0;
Piotr Haber4a3da992013-04-11 13:28:46 +0200688 int try_cnt = 0;
689
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100690 brcmf_dbg(TRACE, "Enter: on=%d\n", on);
Piotr Haber4a3da992013-04-11 13:28:46 +0200691
692 wr_val = (on << SBSDIO_FUNC1_SLEEPCSR_KSO_SHIFT);
693 /* 1st KSO write goes to AOS wake up core if device is asleep */
Arend van Spriela39be272013-12-12 11:58:58 +0100694 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
695 wr_val, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +0200696
697 if (on) {
698 /* device WAKEUP through KSO:
699 * write bit 0 & read back until
700 * both bits 0 (kso bit) & 1 (dev on status) are set
701 */
702 cmp_val = SBSDIO_FUNC1_SLEEPCSR_KSO_MASK |
703 SBSDIO_FUNC1_SLEEPCSR_DEVON_MASK;
704 bmask = cmp_val;
705 usleep_range(2000, 3000);
706 } else {
707 /* Put device to sleep, turn off KSO */
708 cmp_val = 0;
709 /* only check for bit0, bit1(dev on status) may not
710 * get cleared right away
711 */
712 bmask = SBSDIO_FUNC1_SLEEPCSR_KSO_MASK;
713 }
714
715 do {
716 /* reliable KSO bit set/clr:
717 * the sdiod sleep write access is synced to PMU 32khz clk
718 * just one write attempt may fail,
719 * read it back until it matches written value
720 */
Arend van Spriela39be272013-12-12 11:58:58 +0100721 rd_val = brcmf_sdiod_regrb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
722 &err);
Arend Van Spriel5251b6b2016-09-05 11:42:13 +0100723 if (!err) {
724 if ((rd_val & bmask) == cmp_val)
725 break;
726 err_cnt = 0;
727 }
728 /* bail out upon subsequent access errors */
729 if (err && (err_cnt++ > BRCMF_SDIO_MAX_ACCESS_ERRORS))
Piotr Haber4a3da992013-04-11 13:28:46 +0200730 break;
Piotr Haber4a3da992013-04-11 13:28:46 +0200731 udelay(KSO_WAIT_US);
Arend van Spriela39be272013-12-12 11:58:58 +0100732 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
733 wr_val, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +0200734 } while (try_cnt++ < MAX_KSO_ATTEMPTS);
735
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100736 if (try_cnt > 2)
737 brcmf_dbg(SDIO, "try_cnt=%d rd_val=0x%x err=%d\n", try_cnt,
738 rd_val, err);
739
740 if (try_cnt > MAX_KSO_ATTEMPTS)
741 brcmf_err("max tries: rd_val=0x%x err=%d\n", rd_val, err);
742
Piotr Haber4a3da992013-04-11 13:28:46 +0200743 return err;
744}
745
Arend van Spriel5b435de2011-10-05 13:19:03 +0200746#define HOSTINTMASK (I_HMB_SW_MASK | I_CHIPACTIVE)
747
Arend van Spriel5b435de2011-10-05 13:19:03 +0200748/* Turn backplane clock on or off */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100749static int brcmf_sdio_htclk(struct brcmf_sdio *bus, bool on, bool pendok)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200750{
751 int err;
752 u8 clkctl, clkreq, devctl;
753 unsigned long timeout;
754
Arend van Sprielc3203372013-04-03 12:40:44 +0200755 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200756
757 clkctl = 0;
758
Piotr Haber4a3da992013-04-11 13:28:46 +0200759 if (bus->sr_enabled) {
760 bus->clkstate = (on ? CLK_AVAIL : CLK_SDONLY);
761 return 0;
762 }
763
Arend van Spriel5b435de2011-10-05 13:19:03 +0200764 if (on) {
765 /* Request HT Avail */
766 clkreq =
767 bus->alp_only ? SBSDIO_ALP_AVAIL_REQ : SBSDIO_HT_AVAIL_REQ;
768
Arend van Spriela39be272013-12-12 11:58:58 +0100769 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
770 clkreq, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200771 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100772 brcmf_err("HT Avail request error: %d\n", err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200773 return -EBADE;
774 }
775
Arend van Spriel5b435de2011-10-05 13:19:03 +0200776 /* Check current status */
Arend van Spriela39be272013-12-12 11:58:58 +0100777 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
778 SBSDIO_FUNC1_CHIPCLKCSR, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200779 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100780 brcmf_err("HT Avail read error: %d\n", err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200781 return -EBADE;
782 }
783
784 /* Go to pending and await interrupt if appropriate */
785 if (!SBSDIO_CLKAV(clkctl, bus->alp_only) && pendok) {
786 /* Allow only clock-available interrupt */
Arend van Spriela39be272013-12-12 11:58:58 +0100787 devctl = brcmf_sdiod_regrb(bus->sdiodev,
788 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200789 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100790 brcmf_err("Devctl error setting CA: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200791 err);
792 return -EBADE;
793 }
794
795 devctl |= SBSDIO_DEVCTL_CA_INT_ONLY;
Arend van Spriela39be272013-12-12 11:58:58 +0100796 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
797 devctl, &err);
Arend van Sprielc3203372013-04-03 12:40:44 +0200798 brcmf_dbg(SDIO, "CLKCTL: set PENDING\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200799 bus->clkstate = CLK_PENDING;
800
801 return 0;
802 } else if (bus->clkstate == CLK_PENDING) {
803 /* Cancel CA-only interrupt filter */
Arend van Spriela39be272013-12-12 11:58:58 +0100804 devctl = brcmf_sdiod_regrb(bus->sdiodev,
805 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200806 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
Arend van Spriela39be272013-12-12 11:58:58 +0100807 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
808 devctl, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200809 }
810
811 /* Otherwise, wait here (polling) for HT Avail */
812 timeout = jiffies +
813 msecs_to_jiffies(PMU_MAX_TRANSITION_DLY/1000);
814 while (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
Arend van Spriela39be272013-12-12 11:58:58 +0100815 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
816 SBSDIO_FUNC1_CHIPCLKCSR,
817 &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200818 if (time_after(jiffies, timeout))
819 break;
820 else
821 usleep_range(5000, 10000);
822 }
823 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100824 brcmf_err("HT Avail request error: %d\n", err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200825 return -EBADE;
826 }
827 if (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100828 brcmf_err("HT Avail timeout (%d): clkctl 0x%02x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200829 PMU_MAX_TRANSITION_DLY, clkctl);
830 return -EBADE;
831 }
832
833 /* Mark clock available */
834 bus->clkstate = CLK_AVAIL;
Arend van Sprielc3203372013-04-03 12:40:44 +0200835 brcmf_dbg(SDIO, "CLKCTL: turned ON\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200836
Joe Perches8ae74652012-01-15 00:38:38 -0800837#if defined(DEBUG)
Joe Perches23677ce2012-02-09 11:17:23 +0000838 if (!bus->alp_only) {
Arend van Spriel5b435de2011-10-05 13:19:03 +0200839 if (SBSDIO_ALPONLY(clkctl))
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100840 brcmf_err("HT Clock should be on\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200841 }
Joe Perches8ae74652012-01-15 00:38:38 -0800842#endif /* defined (DEBUG) */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200843
Arend van Spriel5b435de2011-10-05 13:19:03 +0200844 } else {
845 clkreq = 0;
846
847 if (bus->clkstate == CLK_PENDING) {
848 /* Cancel CA-only interrupt filter */
Arend van Spriela39be272013-12-12 11:58:58 +0100849 devctl = brcmf_sdiod_regrb(bus->sdiodev,
850 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200851 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
Arend van Spriela39be272013-12-12 11:58:58 +0100852 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
853 devctl, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200854 }
855
856 bus->clkstate = CLK_SDONLY;
Arend van Spriela39be272013-12-12 11:58:58 +0100857 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
858 clkreq, &err);
Arend van Sprielc3203372013-04-03 12:40:44 +0200859 brcmf_dbg(SDIO, "CLKCTL: turned OFF\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200860 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100861 brcmf_err("Failed access turning clock off: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200862 err);
863 return -EBADE;
864 }
865 }
866 return 0;
867}
868
869/* Change idle/active SD state */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100870static int brcmf_sdio_sdclk(struct brcmf_sdio *bus, bool on)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200871{
Arend van Sprielc3203372013-04-03 12:40:44 +0200872 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200873
874 if (on)
875 bus->clkstate = CLK_SDONLY;
876 else
877 bus->clkstate = CLK_NONE;
878
879 return 0;
880}
881
882/* Transition SD and backplane clock readiness */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100883static int brcmf_sdio_clkctl(struct brcmf_sdio *bus, uint target, bool pendok)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200884{
Joe Perches8ae74652012-01-15 00:38:38 -0800885#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200886 uint oldstate = bus->clkstate;
Joe Perches8ae74652012-01-15 00:38:38 -0800887#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200888
Arend van Sprielc3203372013-04-03 12:40:44 +0200889 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200890
891 /* Early exit if we're already there */
Hante Meulemanb441ba82015-03-06 18:40:39 +0100892 if (bus->clkstate == target)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200893 return 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200894
895 switch (target) {
896 case CLK_AVAIL:
897 /* Make sure SD clock is available */
898 if (bus->clkstate == CLK_NONE)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100899 brcmf_sdio_sdclk(bus, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200900 /* Now request HT Avail on the backplane */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100901 brcmf_sdio_htclk(bus, true, pendok);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200902 break;
903
904 case CLK_SDONLY:
905 /* Remove HT request, or bring up SD clock */
906 if (bus->clkstate == CLK_NONE)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100907 brcmf_sdio_sdclk(bus, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200908 else if (bus->clkstate == CLK_AVAIL)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100909 brcmf_sdio_htclk(bus, false, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200910 else
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100911 brcmf_err("request for %d -> %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200912 bus->clkstate, target);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200913 break;
914
915 case CLK_NONE:
916 /* Make sure to remove HT request */
917 if (bus->clkstate == CLK_AVAIL)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100918 brcmf_sdio_htclk(bus, false, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200919 /* Now remove the SD clock */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100920 brcmf_sdio_sdclk(bus, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200921 break;
922 }
Joe Perches8ae74652012-01-15 00:38:38 -0800923#ifdef DEBUG
Arend van Sprielc3203372013-04-03 12:40:44 +0200924 brcmf_dbg(SDIO, "%d -> %d\n", oldstate, bus->clkstate);
Joe Perches8ae74652012-01-15 00:38:38 -0800925#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200926
927 return 0;
928}
929
Piotr Haber4a3da992013-04-11 13:28:46 +0200930static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100931brcmf_sdio_bus_sleep(struct brcmf_sdio *bus, bool sleep, bool pendok)
Piotr Haber4a3da992013-04-11 13:28:46 +0200932{
933 int err = 0;
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100934 u8 clkcsr;
Arend van Spriel82030d62014-01-29 15:32:20 +0100935
936 brcmf_dbg(SDIO, "Enter: request %s currently %s\n",
Piotr Haber4a3da992013-04-11 13:28:46 +0200937 (sleep ? "SLEEP" : "WAKE"),
Arend van Spriel99824642015-02-06 18:36:45 +0100938 (bus->sleeping ? "SLEEP" : "WAKE"));
Piotr Haber4a3da992013-04-11 13:28:46 +0200939
940 /* If SR is enabled control bus state with KSO */
941 if (bus->sr_enabled) {
942 /* Done if we're already in the requested state */
Arend van Spriel99824642015-02-06 18:36:45 +0100943 if (sleep == bus->sleeping)
Piotr Haber4a3da992013-04-11 13:28:46 +0200944 goto end;
945
946 /* Going to sleep */
947 if (sleep) {
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100948 clkcsr = brcmf_sdiod_regrb(bus->sdiodev,
949 SBSDIO_FUNC1_CHIPCLKCSR,
950 &err);
951 if ((clkcsr & SBSDIO_CSR_MASK) == 0) {
952 brcmf_dbg(SDIO, "no clock, set ALP\n");
953 brcmf_sdiod_regwb(bus->sdiodev,
954 SBSDIO_FUNC1_CHIPCLKCSR,
955 SBSDIO_ALP_AVAIL_REQ, &err);
956 }
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100957 err = brcmf_sdio_kso_control(bus, false);
Piotr Haber4a3da992013-04-11 13:28:46 +0200958 } else {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100959 err = brcmf_sdio_kso_control(bus, true);
Piotr Haber4a3da992013-04-11 13:28:46 +0200960 }
Arend van Spriel8982cd42015-01-25 20:31:34 +0100961 if (err) {
Piotr Haber4a3da992013-04-11 13:28:46 +0200962 brcmf_err("error while changing bus sleep state %d\n",
963 err);
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100964 goto done;
Piotr Haber4a3da992013-04-11 13:28:46 +0200965 }
966 }
967
968end:
969 /* control clocks */
970 if (sleep) {
971 if (!bus->sr_enabled)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100972 brcmf_sdio_clkctl(bus, CLK_NONE, pendok);
Piotr Haber4a3da992013-04-11 13:28:46 +0200973 } else {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100974 brcmf_sdio_clkctl(bus, CLK_AVAIL, pendok);
Arend van Spriel4011fc42016-01-05 11:05:47 +0100975 brcmf_sdio_wd_timer(bus, true);
Piotr Haber4a3da992013-04-11 13:28:46 +0200976 }
Arend van Spriel99824642015-02-06 18:36:45 +0100977 bus->sleeping = sleep;
Arend van Spriel8982cd42015-01-25 20:31:34 +0100978 brcmf_dbg(SDIO, "new state %s\n",
979 (sleep ? "SLEEP" : "WAKE"));
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100980done:
981 brcmf_dbg(SDIO, "Exit: err=%d\n", err);
Piotr Haber4a3da992013-04-11 13:28:46 +0200982 return err;
983
984}
985
Daniel Kim0801e6c2014-01-29 15:32:13 +0100986#ifdef DEBUG
987static inline bool brcmf_sdio_valid_shared_address(u32 addr)
988{
989 return !(addr == 0 || ((~addr >> 16) & 0xffff) == (addr & 0xffff));
990}
991
992static int brcmf_sdio_readshared(struct brcmf_sdio *bus,
993 struct sdpcm_shared *sh)
994{
Arend van Spriel9819a902015-03-11 16:11:32 +0100995 u32 addr = 0;
Daniel Kim0801e6c2014-01-29 15:32:13 +0100996 int rv;
997 u32 shaddr = 0;
998 struct sdpcm_shared_le sh_le;
999 __le32 addr_le;
1000
Arend van Spriel9819a902015-03-11 16:11:32 +01001001 sdio_claim_host(bus->sdiodev->func[1]);
1002 brcmf_sdio_bus_sleep(bus, false, false);
Daniel Kim0801e6c2014-01-29 15:32:13 +01001003
1004 /*
1005 * Read last word in socram to determine
1006 * address of sdpcm_shared structure
1007 */
Arend van Spriel9819a902015-03-11 16:11:32 +01001008 shaddr = bus->ci->rambase + bus->ci->ramsize - 4;
1009 if (!bus->ci->rambase && brcmf_chip_sr_capable(bus->ci))
1010 shaddr -= bus->ci->srsize;
1011 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, shaddr,
1012 (u8 *)&addr_le, 4);
Daniel Kim0801e6c2014-01-29 15:32:13 +01001013 if (rv < 0)
Arend van Spriel9819a902015-03-11 16:11:32 +01001014 goto fail;
Daniel Kim0801e6c2014-01-29 15:32:13 +01001015
1016 /*
1017 * Check if addr is valid.
1018 * NVRAM length at the end of memory should have been overwritten.
1019 */
Arend van Spriel9819a902015-03-11 16:11:32 +01001020 addr = le32_to_cpu(addr_le);
Daniel Kim0801e6c2014-01-29 15:32:13 +01001021 if (!brcmf_sdio_valid_shared_address(addr)) {
Arend van Spriel9819a902015-03-11 16:11:32 +01001022 brcmf_err("invalid sdpcm_shared address 0x%08X\n", addr);
1023 rv = -EINVAL;
1024 goto fail;
Daniel Kim0801e6c2014-01-29 15:32:13 +01001025 }
1026
Arend van Spriel9819a902015-03-11 16:11:32 +01001027 brcmf_dbg(INFO, "sdpcm_shared address 0x%08X\n", addr);
1028
Daniel Kim0801e6c2014-01-29 15:32:13 +01001029 /* Read hndrte_shared structure */
1030 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, (u8 *)&sh_le,
1031 sizeof(struct sdpcm_shared_le));
1032 if (rv < 0)
Arend van Spriel9819a902015-03-11 16:11:32 +01001033 goto fail;
1034
1035 sdio_release_host(bus->sdiodev->func[1]);
Daniel Kim0801e6c2014-01-29 15:32:13 +01001036
1037 /* Endianness */
1038 sh->flags = le32_to_cpu(sh_le.flags);
1039 sh->trap_addr = le32_to_cpu(sh_le.trap_addr);
1040 sh->assert_exp_addr = le32_to_cpu(sh_le.assert_exp_addr);
1041 sh->assert_file_addr = le32_to_cpu(sh_le.assert_file_addr);
1042 sh->assert_line = le32_to_cpu(sh_le.assert_line);
1043 sh->console_addr = le32_to_cpu(sh_le.console_addr);
1044 sh->msgtrace_addr = le32_to_cpu(sh_le.msgtrace_addr);
1045
1046 if ((sh->flags & SDPCM_SHARED_VERSION_MASK) > SDPCM_SHARED_VERSION) {
1047 brcmf_err("sdpcm shared version unsupported: dhd %d dongle %d\n",
1048 SDPCM_SHARED_VERSION,
1049 sh->flags & SDPCM_SHARED_VERSION_MASK);
1050 return -EPROTO;
1051 }
Daniel Kim0801e6c2014-01-29 15:32:13 +01001052 return 0;
Arend van Spriel9819a902015-03-11 16:11:32 +01001053
1054fail:
1055 brcmf_err("unable to obtain sdpcm_shared info: rv=%d (addr=0x%x)\n",
1056 rv, addr);
1057 sdio_release_host(bus->sdiodev->func[1]);
1058 return rv;
Daniel Kim0801e6c2014-01-29 15:32:13 +01001059}
1060
1061static void brcmf_sdio_get_console_addr(struct brcmf_sdio *bus)
1062{
1063 struct sdpcm_shared sh;
1064
1065 if (brcmf_sdio_readshared(bus, &sh) == 0)
1066 bus->console_addr = sh.console_addr;
1067}
1068#else
1069static void brcmf_sdio_get_console_addr(struct brcmf_sdio *bus)
1070{
1071}
1072#endif /* DEBUG */
1073
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001074static u32 brcmf_sdio_hostmail(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001075{
1076 u32 intstatus = 0;
1077 u32 hmb_data;
1078 u8 fcbits;
Franky Lin58692752012-05-04 18:27:36 -07001079 int ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001080
Arend van Sprielc3203372013-04-03 12:40:44 +02001081 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001082
1083 /* Read mailbox data and ack that we did so */
Franky Lin58692752012-05-04 18:27:36 -07001084 ret = r_sdreg32(bus, &hmb_data,
1085 offsetof(struct sdpcmd_regs, tohostmailboxdata));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001086
Franky Lin58692752012-05-04 18:27:36 -07001087 if (ret == 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001088 w_sdreg32(bus, SMB_INT_ACK,
Franky Lin58692752012-05-04 18:27:36 -07001089 offsetof(struct sdpcmd_regs, tosbmailbox));
Arend van Spriel80969832012-06-09 22:51:44 +02001090 bus->sdcnt.f1regdata += 2;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001091
1092 /* Dongle recomposed rx frames, accept them again */
1093 if (hmb_data & HMB_DATA_NAKHANDLED) {
Arend van Sprielc3203372013-04-03 12:40:44 +02001094 brcmf_dbg(SDIO, "Dongle reports NAK handled, expect rtx of %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001095 bus->rx_seq);
1096 if (!bus->rxskip)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001097 brcmf_err("unexpected NAKHANDLED!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001098
1099 bus->rxskip = false;
1100 intstatus |= I_HMB_FRAME_IND;
1101 }
1102
1103 /*
1104 * DEVREADY does not occur with gSPI.
1105 */
1106 if (hmb_data & (HMB_DATA_DEVREADY | HMB_DATA_FWREADY)) {
1107 bus->sdpcm_ver =
1108 (hmb_data & HMB_DATA_VERSION_MASK) >>
1109 HMB_DATA_VERSION_SHIFT;
1110 if (bus->sdpcm_ver != SDPCM_PROT_VERSION)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001111 brcmf_err("Version mismatch, dongle reports %d, "
Arend van Spriel5b435de2011-10-05 13:19:03 +02001112 "expecting %d\n",
1113 bus->sdpcm_ver, SDPCM_PROT_VERSION);
1114 else
Arend van Sprielc3203372013-04-03 12:40:44 +02001115 brcmf_dbg(SDIO, "Dongle ready, protocol version %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001116 bus->sdpcm_ver);
Daniel Kim0801e6c2014-01-29 15:32:13 +01001117
1118 /*
1119 * Retrieve console state address now that firmware should have
1120 * updated it.
1121 */
1122 brcmf_sdio_get_console_addr(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001123 }
1124
1125 /*
1126 * Flow Control has been moved into the RX headers and this out of band
1127 * method isn't used any more.
1128 * remaining backward compatible with older dongles.
1129 */
1130 if (hmb_data & HMB_DATA_FC) {
1131 fcbits = (hmb_data & HMB_DATA_FCDATA_MASK) >>
1132 HMB_DATA_FCDATA_SHIFT;
1133
1134 if (fcbits & ~bus->flowcontrol)
Arend van Spriel80969832012-06-09 22:51:44 +02001135 bus->sdcnt.fc_xoff++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001136
1137 if (bus->flowcontrol & ~fcbits)
Arend van Spriel80969832012-06-09 22:51:44 +02001138 bus->sdcnt.fc_xon++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001139
Arend van Spriel80969832012-06-09 22:51:44 +02001140 bus->sdcnt.fc_rcvd++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001141 bus->flowcontrol = fcbits;
1142 }
1143
1144 /* Shouldn't be any others */
1145 if (hmb_data & ~(HMB_DATA_DEVREADY |
1146 HMB_DATA_NAKHANDLED |
1147 HMB_DATA_FC |
1148 HMB_DATA_FWREADY |
1149 HMB_DATA_FCDATA_MASK | HMB_DATA_VERSION_MASK))
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001150 brcmf_err("Unknown mailbox data content: 0x%02x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001151 hmb_data);
1152
1153 return intstatus;
1154}
1155
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001156static void brcmf_sdio_rxfail(struct brcmf_sdio *bus, bool abort, bool rtx)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001157{
1158 uint retries = 0;
1159 u16 lastrbc;
1160 u8 hi, lo;
1161 int err;
1162
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001163 brcmf_err("%sterminate frame%s\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001164 abort ? "abort command, " : "",
1165 rtx ? ", send NAK" : "");
1166
1167 if (abort)
Arend van Spriela39be272013-12-12 11:58:58 +01001168 brcmf_sdiod_abort(bus->sdiodev, SDIO_FUNC_2);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001169
Arend van Spriela39be272013-12-12 11:58:58 +01001170 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL,
1171 SFC_RF_TERM, &err);
Arend van Spriel80969832012-06-09 22:51:44 +02001172 bus->sdcnt.f1regdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001173
1174 /* Wait until the packet has been flushed (device/FIFO stable) */
1175 for (lastrbc = retries = 0xffff; retries > 0; retries--) {
Arend van Spriela39be272013-12-12 11:58:58 +01001176 hi = brcmf_sdiod_regrb(bus->sdiodev,
1177 SBSDIO_FUNC1_RFRAMEBCHI, &err);
1178 lo = brcmf_sdiod_regrb(bus->sdiodev,
1179 SBSDIO_FUNC1_RFRAMEBCLO, &err);
Arend van Spriel80969832012-06-09 22:51:44 +02001180 bus->sdcnt.f1regdata += 2;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001181
1182 if ((hi == 0) && (lo == 0))
1183 break;
1184
1185 if ((hi > (lastrbc >> 8)) && (lo > (lastrbc & 0x00ff))) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001186 brcmf_err("count growing: last 0x%04x now 0x%04x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001187 lastrbc, (hi << 8) + lo);
1188 }
1189 lastrbc = (hi << 8) + lo;
1190 }
1191
1192 if (!retries)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001193 brcmf_err("count never zeroed: last 0x%04x\n", lastrbc);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001194 else
Arend van Sprielc3203372013-04-03 12:40:44 +02001195 brcmf_dbg(SDIO, "flush took %d iterations\n", 0xffff - retries);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001196
1197 if (rtx) {
Arend van Spriel80969832012-06-09 22:51:44 +02001198 bus->sdcnt.rxrtx++;
Franky Lin58692752012-05-04 18:27:36 -07001199 err = w_sdreg32(bus, SMB_NAK,
1200 offsetof(struct sdpcmd_regs, tosbmailbox));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001201
Arend van Spriel80969832012-06-09 22:51:44 +02001202 bus->sdcnt.f1regdata++;
Franky Lin58692752012-05-04 18:27:36 -07001203 if (err == 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001204 bus->rxskip = true;
1205 }
1206
1207 /* Clear partial in any case */
Franky Lin4754fce2012-09-19 22:21:11 +02001208 bus->cur_read.len = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001209}
1210
Hante Meuleman81c78832014-02-25 20:30:35 +01001211static void brcmf_sdio_txfail(struct brcmf_sdio *bus)
1212{
1213 struct brcmf_sdio_dev *sdiodev = bus->sdiodev;
1214 u8 i, hi, lo;
1215
1216 /* On failure, abort the command and terminate the frame */
1217 brcmf_err("sdio error, abort command and terminate frame\n");
1218 bus->sdcnt.tx_sderrs++;
1219
1220 brcmf_sdiod_abort(sdiodev, SDIO_FUNC_2);
1221 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_FRAMECTRL, SFC_WF_TERM, NULL);
1222 bus->sdcnt.f1regdata++;
1223
1224 for (i = 0; i < 3; i++) {
1225 hi = brcmf_sdiod_regrb(sdiodev, SBSDIO_FUNC1_WFRAMEBCHI, NULL);
1226 lo = brcmf_sdiod_regrb(sdiodev, SBSDIO_FUNC1_WFRAMEBCLO, NULL);
1227 bus->sdcnt.f1regdata += 2;
1228 if ((hi == 0) && (lo == 0))
1229 break;
1230 }
1231}
1232
Arend van Spriel9a95e602011-11-10 20:30:29 +01001233/* return total length of buffer chain */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001234static uint brcmf_sdio_glom_len(struct brcmf_sdio *bus)
Arend van Spriel9a95e602011-11-10 20:30:29 +01001235{
1236 struct sk_buff *p;
1237 uint total;
1238
1239 total = 0;
1240 skb_queue_walk(&bus->glom, p)
1241 total += p->len;
1242 return total;
1243}
1244
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001245static void brcmf_sdio_free_glom(struct brcmf_sdio *bus)
Arend van Spriel046808d2011-11-10 20:30:31 +01001246{
1247 struct sk_buff *cur, *next;
1248
1249 skb_queue_walk_safe(&bus->glom, cur, next) {
1250 skb_unlink(cur, &bus->glom);
1251 brcmu_pkt_buf_free_skb(cur);
1252 }
1253}
1254
Franky Lin6bc52312013-08-10 12:27:28 +02001255/**
1256 * brcmfmac sdio bus specific header
1257 * This is the lowest layer header wrapped on the packets transmitted between
1258 * host and WiFi dongle which contains information needed for SDIO core and
1259 * firmware
1260 *
Franky Lin8da9d2c2013-11-29 12:25:10 +01001261 * It consists of 3 parts: hardware header, hardware extension header and
1262 * software header
Franky Lin6bc52312013-08-10 12:27:28 +02001263 * hardware header (frame tag) - 4 bytes
1264 * Byte 0~1: Frame length
1265 * Byte 2~3: Checksum, bit-wise inverse of frame length
Franky Lin8da9d2c2013-11-29 12:25:10 +01001266 * hardware extension header - 8 bytes
1267 * Tx glom mode only, N/A for Rx or normal Tx
1268 * Byte 0~1: Packet length excluding hw frame tag
1269 * Byte 2: Reserved
1270 * Byte 3: Frame flags, bit 0: last frame indication
1271 * Byte 4~5: Reserved
1272 * Byte 6~7: Tail padding length
Franky Lin6bc52312013-08-10 12:27:28 +02001273 * software header - 8 bytes
1274 * Byte 0: Rx/Tx sequence number
1275 * Byte 1: 4 MSB Channel number, 4 LSB arbitrary flag
1276 * Byte 2: Length of next data frame, reserved for Tx
1277 * Byte 3: Data offset
1278 * Byte 4: Flow control bits, reserved for Tx
1279 * Byte 5: Maximum Sequence number allowed by firmware for Tx, N/A for Tx packet
1280 * Byte 6~7: Reserved
1281 */
1282#define SDPCM_HWHDR_LEN 4
Franky Lin8da9d2c2013-11-29 12:25:10 +01001283#define SDPCM_HWEXT_LEN 8
Franky Lin6bc52312013-08-10 12:27:28 +02001284#define SDPCM_SWHDR_LEN 8
1285#define SDPCM_HDRLEN (SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN)
Franky Lin6bc52312013-08-10 12:27:28 +02001286/* software header */
1287#define SDPCM_SEQ_MASK 0x000000ff
1288#define SDPCM_SEQ_WRAP 256
1289#define SDPCM_CHANNEL_MASK 0x00000f00
1290#define SDPCM_CHANNEL_SHIFT 8
1291#define SDPCM_CONTROL_CHANNEL 0 /* Control */
1292#define SDPCM_EVENT_CHANNEL 1 /* Asyc Event Indication */
1293#define SDPCM_DATA_CHANNEL 2 /* Data Xmit/Recv */
1294#define SDPCM_GLOM_CHANNEL 3 /* Coalesced packets */
1295#define SDPCM_TEST_CHANNEL 15 /* Test/debug packets */
1296#define SDPCM_GLOMDESC(p) (((u8 *)p)[1] & 0x80)
1297#define SDPCM_NEXTLEN_MASK 0x00ff0000
1298#define SDPCM_NEXTLEN_SHIFT 16
1299#define SDPCM_DOFFSET_MASK 0xff000000
1300#define SDPCM_DOFFSET_SHIFT 24
1301#define SDPCM_FCMASK_MASK 0x000000ff
1302#define SDPCM_WINDOW_MASK 0x0000ff00
1303#define SDPCM_WINDOW_SHIFT 8
1304
1305static inline u8 brcmf_sdio_getdatoffset(u8 *swheader)
1306{
1307 u32 hdrvalue;
1308 hdrvalue = *(u32 *)swheader;
1309 return (u8)((hdrvalue & SDPCM_DOFFSET_MASK) >> SDPCM_DOFFSET_SHIFT);
1310}
1311
Franky Linc56caa9d2016-04-11 11:35:25 +02001312static inline bool brcmf_sdio_fromevntchan(u8 *swheader)
1313{
1314 u32 hdrvalue;
1315 u8 ret;
1316
1317 hdrvalue = *(u32 *)swheader;
1318 ret = (u8)((hdrvalue & SDPCM_CHANNEL_MASK) >> SDPCM_CHANNEL_SHIFT);
1319
1320 return (ret == SDPCM_EVENT_CHANNEL);
1321}
1322
Franky Lin6bc52312013-08-10 12:27:28 +02001323static int brcmf_sdio_hdparse(struct brcmf_sdio *bus, u8 *header,
1324 struct brcmf_sdio_hdrinfo *rd,
1325 enum brcmf_sdio_frmtype type)
Franky Lin4754fce2012-09-19 22:21:11 +02001326{
1327 u16 len, checksum;
1328 u8 rx_seq, fc, tx_seq_max;
Franky Lin6bc52312013-08-10 12:27:28 +02001329 u32 swheader;
Franky Lin4754fce2012-09-19 22:21:11 +02001330
Arend van Spriel4b776962013-11-29 12:25:06 +01001331 trace_brcmf_sdpcm_hdr(SDPCM_RX, header);
Arend van Spriel76584ec2013-10-15 15:44:47 +02001332
Franky Lin6bc52312013-08-10 12:27:28 +02001333 /* hw header */
Franky Lin4754fce2012-09-19 22:21:11 +02001334 len = get_unaligned_le16(header);
1335 checksum = get_unaligned_le16(header + sizeof(u16));
1336 /* All zero means no more to read */
1337 if (!(len | checksum)) {
1338 bus->rxpending = false;
Franky Lin10510582012-11-05 16:22:26 -08001339 return -ENODATA;
Franky Lin4754fce2012-09-19 22:21:11 +02001340 }
1341 if ((u16)(~(len ^ checksum))) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001342 brcmf_err("HW header checksum error\n");
Franky Lin4754fce2012-09-19 22:21:11 +02001343 bus->sdcnt.rx_badhdr++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001344 brcmf_sdio_rxfail(bus, false, false);
Franky Lin10510582012-11-05 16:22:26 -08001345 return -EIO;
Franky Lin4754fce2012-09-19 22:21:11 +02001346 }
1347 if (len < SDPCM_HDRLEN) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001348 brcmf_err("HW header length error\n");
Franky Lin10510582012-11-05 16:22:26 -08001349 return -EPROTO;
Franky Lin4754fce2012-09-19 22:21:11 +02001350 }
Franky Lin9d7d6f92012-10-22 10:36:24 -07001351 if (type == BRCMF_SDIO_FT_SUPER &&
1352 (roundup(len, bus->blocksize) != rd->len)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001353 brcmf_err("HW superframe header length error\n");
Franky Lin10510582012-11-05 16:22:26 -08001354 return -EPROTO;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001355 }
1356 if (type == BRCMF_SDIO_FT_SUB && len > rd->len) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001357 brcmf_err("HW subframe header length error\n");
Franky Lin10510582012-11-05 16:22:26 -08001358 return -EPROTO;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001359 }
Franky Lin4754fce2012-09-19 22:21:11 +02001360 rd->len = len;
1361
Franky Lin6bc52312013-08-10 12:27:28 +02001362 /* software header */
1363 header += SDPCM_HWHDR_LEN;
1364 swheader = le32_to_cpu(*(__le32 *)header);
1365 if (type == BRCMF_SDIO_FT_SUPER && SDPCM_GLOMDESC(header)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001366 brcmf_err("Glom descriptor found in superframe head\n");
Franky Lin9d7d6f92012-10-22 10:36:24 -07001367 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001368 return -EINVAL;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001369 }
Franky Lin6bc52312013-08-10 12:27:28 +02001370 rx_seq = (u8)(swheader & SDPCM_SEQ_MASK);
1371 rd->channel = (swheader & SDPCM_CHANNEL_MASK) >> SDPCM_CHANNEL_SHIFT;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001372 if (len > MAX_RX_DATASZ && rd->channel != SDPCM_CONTROL_CHANNEL &&
1373 type != BRCMF_SDIO_FT_SUPER) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001374 brcmf_err("HW header length too long\n");
Franky Lin4754fce2012-09-19 22:21:11 +02001375 bus->sdcnt.rx_toolong++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001376 brcmf_sdio_rxfail(bus, false, false);
Franky Lin4754fce2012-09-19 22:21:11 +02001377 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001378 return -EPROTO;
Franky Lin4754fce2012-09-19 22:21:11 +02001379 }
Franky Lin9d7d6f92012-10-22 10:36:24 -07001380 if (type == BRCMF_SDIO_FT_SUPER && rd->channel != SDPCM_GLOM_CHANNEL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001381 brcmf_err("Wrong channel for superframe\n");
Franky Lin9d7d6f92012-10-22 10:36:24 -07001382 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001383 return -EINVAL;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001384 }
1385 if (type == BRCMF_SDIO_FT_SUB && rd->channel != SDPCM_DATA_CHANNEL &&
1386 rd->channel != SDPCM_EVENT_CHANNEL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001387 brcmf_err("Wrong channel for subframe\n");
Franky Lin9d7d6f92012-10-22 10:36:24 -07001388 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001389 return -EINVAL;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001390 }
Franky Lin6bc52312013-08-10 12:27:28 +02001391 rd->dat_offset = brcmf_sdio_getdatoffset(header);
Franky Lin4754fce2012-09-19 22:21:11 +02001392 if (rd->dat_offset < SDPCM_HDRLEN || rd->dat_offset > rd->len) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001393 brcmf_err("seq %d: bad data offset\n", rx_seq);
Franky Lin4754fce2012-09-19 22:21:11 +02001394 bus->sdcnt.rx_badhdr++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001395 brcmf_sdio_rxfail(bus, false, false);
Franky Lin4754fce2012-09-19 22:21:11 +02001396 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001397 return -ENXIO;
Franky Lin4754fce2012-09-19 22:21:11 +02001398 }
1399 if (rd->seq_num != rx_seq) {
Wright Feng98aff6c2016-06-03 23:31:11 +02001400 brcmf_dbg(SDIO, "seq %d, expected %d\n", rx_seq, rd->seq_num);
Franky Lin4754fce2012-09-19 22:21:11 +02001401 bus->sdcnt.rx_badseq++;
1402 rd->seq_num = rx_seq;
1403 }
Franky Lin9d7d6f92012-10-22 10:36:24 -07001404 /* no need to check the reset for subframe */
1405 if (type == BRCMF_SDIO_FT_SUB)
Franky Lin10510582012-11-05 16:22:26 -08001406 return 0;
Franky Lin6bc52312013-08-10 12:27:28 +02001407 rd->len_nxtfrm = (swheader & SDPCM_NEXTLEN_MASK) >> SDPCM_NEXTLEN_SHIFT;
Franky Lin4754fce2012-09-19 22:21:11 +02001408 if (rd->len_nxtfrm << 4 > MAX_RX_DATASZ) {
1409 /* only warm for NON glom packet */
1410 if (rd->channel != SDPCM_GLOM_CHANNEL)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001411 brcmf_err("seq %d: next length error\n", rx_seq);
Franky Lin4754fce2012-09-19 22:21:11 +02001412 rd->len_nxtfrm = 0;
1413 }
Franky Lin6bc52312013-08-10 12:27:28 +02001414 swheader = le32_to_cpu(*(__le32 *)(header + 4));
1415 fc = swheader & SDPCM_FCMASK_MASK;
Franky Lin4754fce2012-09-19 22:21:11 +02001416 if (bus->flowcontrol != fc) {
1417 if (~bus->flowcontrol & fc)
1418 bus->sdcnt.fc_xoff++;
1419 if (bus->flowcontrol & ~fc)
1420 bus->sdcnt.fc_xon++;
1421 bus->sdcnt.fc_rcvd++;
1422 bus->flowcontrol = fc;
1423 }
Franky Lin6bc52312013-08-10 12:27:28 +02001424 tx_seq_max = (swheader & SDPCM_WINDOW_MASK) >> SDPCM_WINDOW_SHIFT;
Franky Lin4754fce2012-09-19 22:21:11 +02001425 if ((u8)(tx_seq_max - bus->tx_seq) > 0x40) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001426 brcmf_err("seq %d: max tx seq number error\n", rx_seq);
Franky Lin4754fce2012-09-19 22:21:11 +02001427 tx_seq_max = bus->tx_seq + 2;
1428 }
1429 bus->tx_max = tx_seq_max;
1430
Franky Lin10510582012-11-05 16:22:26 -08001431 return 0;
Franky Lin4754fce2012-09-19 22:21:11 +02001432}
1433
Franky Lin6bc52312013-08-10 12:27:28 +02001434static inline void brcmf_sdio_update_hwhdr(u8 *header, u16 frm_length)
1435{
1436 *(__le16 *)header = cpu_to_le16(frm_length);
1437 *(((__le16 *)header) + 1) = cpu_to_le16(~frm_length);
1438}
1439
1440static void brcmf_sdio_hdpack(struct brcmf_sdio *bus, u8 *header,
1441 struct brcmf_sdio_hdrinfo *hd_info)
1442{
Franky Lin8da9d2c2013-11-29 12:25:10 +01001443 u32 hdrval;
1444 u8 hdr_offset;
Franky Lin6bc52312013-08-10 12:27:28 +02001445
1446 brcmf_sdio_update_hwhdr(header, hd_info->len);
Franky Lin8da9d2c2013-11-29 12:25:10 +01001447 hdr_offset = SDPCM_HWHDR_LEN;
Franky Lin6bc52312013-08-10 12:27:28 +02001448
Franky Lin8da9d2c2013-11-29 12:25:10 +01001449 if (bus->txglom) {
1450 hdrval = (hd_info->len - hdr_offset) | (hd_info->lastfrm << 24);
1451 *((__le32 *)(header + hdr_offset)) = cpu_to_le32(hdrval);
1452 hdrval = (u16)hd_info->tail_pad << 16;
1453 *(((__le32 *)(header + hdr_offset)) + 1) = cpu_to_le32(hdrval);
1454 hdr_offset += SDPCM_HWEXT_LEN;
1455 }
1456
1457 hdrval = hd_info->seq_num;
1458 hdrval |= (hd_info->channel << SDPCM_CHANNEL_SHIFT) &
1459 SDPCM_CHANNEL_MASK;
1460 hdrval |= (hd_info->dat_offset << SDPCM_DOFFSET_SHIFT) &
1461 SDPCM_DOFFSET_MASK;
1462 *((__le32 *)(header + hdr_offset)) = cpu_to_le32(hdrval);
1463 *(((__le32 *)(header + hdr_offset)) + 1) = 0;
1464 trace_brcmf_sdpcm_hdr(SDPCM_TX + !!(bus->txglom), header);
Franky Lin6bc52312013-08-10 12:27:28 +02001465}
1466
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001467static u8 brcmf_sdio_rxglom(struct brcmf_sdio *bus, u8 rxseq)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001468{
1469 u16 dlen, totlen;
1470 u8 *dptr, num = 0;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001471 u16 sublen;
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001472 struct sk_buff *pfirst, *pnext;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001473
1474 int errcode;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001475 u8 doff, sfdoff;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001476
Franky Lin6bc52312013-08-10 12:27:28 +02001477 struct brcmf_sdio_hdrinfo rd_new;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001478
1479 /* If packets, issue read(s) and send up packet chain */
1480 /* Return sequence numbers consumed? */
1481
Arend van Sprielc3203372013-04-03 12:40:44 +02001482 brcmf_dbg(SDIO, "start: glomd %p glom %p\n",
Arend van Sprielb83db862011-10-19 12:51:09 +02001483 bus->glomd, skb_peek(&bus->glom));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001484
1485 /* If there's a descriptor, generate the packet chain */
1486 if (bus->glomd) {
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001487 pfirst = pnext = NULL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001488 dlen = (u16) (bus->glomd->len);
1489 dptr = bus->glomd->data;
1490 if (!dlen || (dlen & 1)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001491 brcmf_err("bad glomd len(%d), ignore descriptor\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001492 dlen);
1493 dlen = 0;
1494 }
1495
1496 for (totlen = num = 0; dlen; num++) {
1497 /* Get (and move past) next length */
1498 sublen = get_unaligned_le16(dptr);
1499 dlen -= sizeof(u16);
1500 dptr += sizeof(u16);
1501 if ((sublen < SDPCM_HDRLEN) ||
1502 ((num == 0) && (sublen < (2 * SDPCM_HDRLEN)))) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001503 brcmf_err("descriptor len %d bad: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001504 num, sublen);
1505 pnext = NULL;
1506 break;
1507 }
Arend van Spriele217d1c2013-11-29 12:25:13 +01001508 if (sublen % bus->sgentry_align) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001509 brcmf_err("sublen %d not multiple of %d\n",
Arend van Spriele217d1c2013-11-29 12:25:13 +01001510 sublen, bus->sgentry_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001511 }
1512 totlen += sublen;
1513
1514 /* For last frame, adjust read len so total
1515 is a block multiple */
1516 if (!dlen) {
1517 sublen +=
1518 (roundup(totlen, bus->blocksize) - totlen);
1519 totlen = roundup(totlen, bus->blocksize);
1520 }
1521
1522 /* Allocate/chain packet for next subframe */
Arend van Spriele217d1c2013-11-29 12:25:13 +01001523 pnext = brcmu_pkt_buf_get_skb(sublen + bus->sgentry_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001524 if (pnext == NULL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001525 brcmf_err("bcm_pkt_buf_get_skb failed, num %d len %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001526 num, sublen);
1527 break;
1528 }
Arend van Sprielb83db862011-10-19 12:51:09 +02001529 skb_queue_tail(&bus->glom, pnext);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001530
1531 /* Adhere to start alignment requirements */
Arend van Spriele217d1c2013-11-29 12:25:13 +01001532 pkt_align(pnext, sublen, bus->sgentry_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001533 }
1534
1535 /* If all allocations succeeded, save packet chain
1536 in bus structure */
1537 if (pnext) {
1538 brcmf_dbg(GLOM, "allocated %d-byte packet chain for %d subframes\n",
1539 totlen, num);
Franky Lin4754fce2012-09-19 22:21:11 +02001540 if (BRCMF_GLOM_ON() && bus->cur_read.len &&
1541 totlen != bus->cur_read.len) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001542 brcmf_dbg(GLOM, "glomdesc mismatch: nextlen %d glomdesc %d rxseq %d\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001543 bus->cur_read.len, totlen, rxseq);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001544 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02001545 pfirst = pnext = NULL;
1546 } else {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001547 brcmf_sdio_free_glom(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001548 num = 0;
1549 }
1550
1551 /* Done with descriptor packet */
1552 brcmu_pkt_buf_free_skb(bus->glomd);
1553 bus->glomd = NULL;
Franky Lin4754fce2012-09-19 22:21:11 +02001554 bus->cur_read.len = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001555 }
1556
1557 /* Ok -- either we just generated a packet chain,
1558 or had one from before */
Arend van Sprielb83db862011-10-19 12:51:09 +02001559 if (!skb_queue_empty(&bus->glom)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001560 if (BRCMF_GLOM_ON()) {
1561 brcmf_dbg(GLOM, "try superframe read, packet chain:\n");
Arend van Sprielb83db862011-10-19 12:51:09 +02001562 skb_queue_walk(&bus->glom, pnext) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001563 brcmf_dbg(GLOM, " %p: %p len 0x%04x (%d)\n",
1564 pnext, (u8 *) (pnext->data),
1565 pnext->len, pnext->len);
1566 }
1567 }
1568
Arend van Sprielb83db862011-10-19 12:51:09 +02001569 pfirst = skb_peek(&bus->glom);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001570 dlen = (u16) brcmf_sdio_glom_len(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001571
1572 /* Do an SDIO read for the superframe. Configurable iovar to
1573 * read directly into the chained packet, or allocate a large
1574 * packet and and copy into the chain.
1575 */
Franky Lin38b0b0d2012-11-05 16:22:24 -08001576 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriela39be272013-12-12 11:58:58 +01001577 errcode = brcmf_sdiod_recv_chain(bus->sdiodev,
Arend van Spriela39be272013-12-12 11:58:58 +01001578 &bus->glom, dlen);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001579 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel80969832012-06-09 22:51:44 +02001580 bus->sdcnt.f2rxdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001581
Franky Lin64d66c32015-11-25 11:32:40 +01001582 /* On failure, kill the superframe */
Arend van Spriel5b435de2011-10-05 13:19:03 +02001583 if (errcode < 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001584 brcmf_err("glom read of %d bytes failed: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001585 dlen, errcode);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001586
Franky Lin38b0b0d2012-11-05 16:22:24 -08001587 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin64d66c32015-11-25 11:32:40 +01001588 brcmf_sdio_rxfail(bus, true, false);
1589 bus->sdcnt.rxglomfail++;
1590 brcmf_sdio_free_glom(bus);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001591 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001592 return 0;
1593 }
Joe Perches1e023822012-01-15 00:38:40 -08001594
1595 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
1596 pfirst->data, min_t(int, pfirst->len, 48),
1597 "SUPERFRAME:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001598
Franky Lin9d7d6f92012-10-22 10:36:24 -07001599 rd_new.seq_num = rxseq;
1600 rd_new.len = dlen;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001601 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin6bc52312013-08-10 12:27:28 +02001602 errcode = brcmf_sdio_hdparse(bus, pfirst->data, &rd_new,
1603 BRCMF_SDIO_FT_SUPER);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001604 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin9d7d6f92012-10-22 10:36:24 -07001605 bus->cur_read.len = rd_new.len_nxtfrm << 4;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001606
1607 /* Remove superframe header, remember offset */
Franky Lin9d7d6f92012-10-22 10:36:24 -07001608 skb_pull(pfirst, rd_new.dat_offset);
1609 sfdoff = rd_new.dat_offset;
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001610 num = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001611
1612 /* Validate all the subframe headers */
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001613 skb_queue_walk(&bus->glom, pnext) {
1614 /* leave when invalid subframe is found */
1615 if (errcode)
1616 break;
1617
Franky Lin9d7d6f92012-10-22 10:36:24 -07001618 rd_new.len = pnext->len;
1619 rd_new.seq_num = rxseq++;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001620 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin6bc52312013-08-10 12:27:28 +02001621 errcode = brcmf_sdio_hdparse(bus, pnext->data, &rd_new,
1622 BRCMF_SDIO_FT_SUB);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001623 sdio_release_host(bus->sdiodev->func[1]);
Joe Perches1e023822012-01-15 00:38:40 -08001624 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
Franky Lin9d7d6f92012-10-22 10:36:24 -07001625 pnext->data, 32, "subframe:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001626
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001627 num++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001628 }
1629
1630 if (errcode) {
Franky Lin64d66c32015-11-25 11:32:40 +01001631 /* Terminate frame on error */
Franky Lin38b0b0d2012-11-05 16:22:24 -08001632 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin64d66c32015-11-25 11:32:40 +01001633 brcmf_sdio_rxfail(bus, true, false);
1634 bus->sdcnt.rxglomfail++;
1635 brcmf_sdio_free_glom(bus);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001636 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001637 bus->cur_read.len = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001638 return 0;
1639 }
1640
1641 /* Basic SD framing looks ok - process each packet (header) */
Arend van Spriel5b435de2011-10-05 13:19:03 +02001642
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001643 skb_queue_walk_safe(&bus->glom, pfirst, pnext) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001644 dptr = (u8 *) (pfirst->data);
1645 sublen = get_unaligned_le16(dptr);
Franky Lin6bc52312013-08-10 12:27:28 +02001646 doff = brcmf_sdio_getdatoffset(&dptr[SDPCM_HWHDR_LEN]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001647
Joe Perches1e023822012-01-15 00:38:40 -08001648 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_DATA_ON(),
Franky Lin9d7d6f92012-10-22 10:36:24 -07001649 dptr, pfirst->len,
1650 "Rx Subframe Data:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001651
1652 __skb_trim(pfirst, sublen);
1653 skb_pull(pfirst, doff);
1654
1655 if (pfirst->len == 0) {
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001656 skb_unlink(pfirst, &bus->glom);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001657 brcmu_pkt_buf_free_skb(pfirst);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001658 continue;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001659 }
1660
Joe Perches1e023822012-01-15 00:38:40 -08001661 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
1662 pfirst->data,
1663 min_t(int, pfirst->len, 32),
1664 "subframe %d to stack, %p (%p/%d) nxt/lnk %p/%p\n",
1665 bus->glom.qlen, pfirst, pfirst->data,
1666 pfirst->len, pfirst->next,
1667 pfirst->prev);
Arend van Spriel05f38202013-09-25 13:05:46 +02001668 skb_unlink(pfirst, &bus->glom);
Gavin Li8e290ce2017-01-17 15:24:05 -08001669 if (brcmf_sdio_fromevntchan(&dptr[SDPCM_HWHDR_LEN]))
Franky Linc56caa9d2016-04-11 11:35:25 +02001670 brcmf_rx_event(bus->sdiodev->dev, pfirst);
1671 else
1672 brcmf_rx_frame(bus->sdiodev->dev, pfirst,
1673 false);
Arend van Spriel05f38202013-09-25 13:05:46 +02001674 bus->sdcnt.rxglompkts++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001675 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02001676
Arend van Spriel80969832012-06-09 22:51:44 +02001677 bus->sdcnt.rxglomframes++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001678 }
1679 return num;
1680}
1681
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001682static int brcmf_sdio_dcmd_resp_wait(struct brcmf_sdio *bus, uint *condition,
1683 bool *pending)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001684{
1685 DECLARE_WAITQUEUE(wait, current);
Arend van Spriel63ce3d52016-01-05 11:05:48 +01001686 int timeout = DCMD_RESP_TIMEOUT;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001687
1688 /* Wait until control frame is available */
1689 add_wait_queue(&bus->dcmd_resp_wait, &wait);
1690 set_current_state(TASK_INTERRUPTIBLE);
1691
1692 while (!(*condition) && (!signal_pending(current) && timeout))
1693 timeout = schedule_timeout(timeout);
1694
1695 if (signal_pending(current))
1696 *pending = true;
1697
1698 set_current_state(TASK_RUNNING);
1699 remove_wait_queue(&bus->dcmd_resp_wait, &wait);
1700
1701 return timeout;
1702}
1703
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001704static int brcmf_sdio_dcmd_resp_wake(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001705{
Kosuke Tatsukawaa7decc42015-12-10 13:43:08 +01001706 wake_up_interruptible(&bus->dcmd_resp_wait);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001707
1708 return 0;
1709}
1710static void
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001711brcmf_sdio_read_control(struct brcmf_sdio *bus, u8 *hdr, uint len, uint doff)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001712{
1713 uint rdlen, pad;
Franky Lindd43a012012-11-05 16:22:22 -08001714 u8 *buf = NULL, *rbuf;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001715 int sdret;
1716
1717 brcmf_dbg(TRACE, "Enter\n");
1718
Franky Lindd43a012012-11-05 16:22:22 -08001719 if (bus->rxblen)
1720 buf = vzalloc(bus->rxblen);
Joe Perches14f8dc42013-02-07 11:46:27 +00001721 if (!buf)
Franky Lindd43a012012-11-05 16:22:22 -08001722 goto done;
Joe Perches14f8dc42013-02-07 11:46:27 +00001723
Franky Lindd43a012012-11-05 16:22:22 -08001724 rbuf = bus->rxbuf;
Arend van Spriel9b2d2f2a82013-11-29 12:25:14 +01001725 pad = ((unsigned long)rbuf % bus->head_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001726 if (pad)
Arend van Spriel9b2d2f2a82013-11-29 12:25:14 +01001727 rbuf += (bus->head_align - pad);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001728
1729 /* Copy the already-read portion over */
Franky Lindd43a012012-11-05 16:22:22 -08001730 memcpy(buf, hdr, BRCMF_FIRSTREAD);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001731 if (len <= BRCMF_FIRSTREAD)
1732 goto gotpkt;
1733
1734 /* Raise rdlen to next SDIO block to avoid tail command */
1735 rdlen = len - BRCMF_FIRSTREAD;
1736 if (bus->roundup && bus->blocksize && (rdlen > bus->blocksize)) {
1737 pad = bus->blocksize - (rdlen % bus->blocksize);
1738 if ((pad <= bus->roundup) && (pad < bus->blocksize) &&
Franky Linb01a6b32011-12-16 18:37:03 -08001739 ((len + pad) < bus->sdiodev->bus_if->maxctl))
Arend van Spriel5b435de2011-10-05 13:19:03 +02001740 rdlen += pad;
Arend van Spriel9b2d2f2a82013-11-29 12:25:14 +01001741 } else if (rdlen % bus->head_align) {
1742 rdlen += bus->head_align - (rdlen % bus->head_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001743 }
1744
Arend van Spriel5b435de2011-10-05 13:19:03 +02001745 /* Drop if the read is too big or it exceeds our maximum */
Franky Linb01a6b32011-12-16 18:37:03 -08001746 if ((rdlen + BRCMF_FIRSTREAD) > bus->sdiodev->bus_if->maxctl) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001747 brcmf_err("%d-byte control read exceeds %d-byte buffer\n",
Franky Linb01a6b32011-12-16 18:37:03 -08001748 rdlen, bus->sdiodev->bus_if->maxctl);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001749 brcmf_sdio_rxfail(bus, false, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001750 goto done;
1751 }
1752
Franky Linb01a6b32011-12-16 18:37:03 -08001753 if ((len - doff) > bus->sdiodev->bus_if->maxctl) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001754 brcmf_err("%d-byte ctl frame (%d-byte ctl data) exceeds %d-byte limit\n",
Franky Linb01a6b32011-12-16 18:37:03 -08001755 len, len - doff, bus->sdiodev->bus_if->maxctl);
Arend van Spriel80969832012-06-09 22:51:44 +02001756 bus->sdcnt.rx_toolong++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001757 brcmf_sdio_rxfail(bus, false, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001758 goto done;
1759 }
1760
Franky Lindd43a012012-11-05 16:22:22 -08001761 /* Read remain of frame body */
Arend van Spriela7cdd822013-12-12 11:58:59 +01001762 sdret = brcmf_sdiod_recv_buf(bus->sdiodev, rbuf, rdlen);
Arend van Spriel80969832012-06-09 22:51:44 +02001763 bus->sdcnt.f2rxdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001764
1765 /* Control frame failures need retransmission */
1766 if (sdret < 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001767 brcmf_err("read %d control bytes failed: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001768 rdlen, sdret);
Arend van Spriel80969832012-06-09 22:51:44 +02001769 bus->sdcnt.rxc_errors++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001770 brcmf_sdio_rxfail(bus, true, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001771 goto done;
Franky Lindd43a012012-11-05 16:22:22 -08001772 } else
1773 memcpy(buf + BRCMF_FIRSTREAD, rbuf, rdlen);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001774
1775gotpkt:
1776
Joe Perches1e023822012-01-15 00:38:40 -08001777 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_CTL_ON(),
Franky Lindd43a012012-11-05 16:22:22 -08001778 buf, len, "RxCtrl:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001779
1780 /* Point to valid data and indicate its length */
Franky Lindd43a012012-11-05 16:22:22 -08001781 spin_lock_bh(&bus->rxctl_lock);
1782 if (bus->rxctl) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001783 brcmf_err("last control frame is being processed.\n");
Franky Lindd43a012012-11-05 16:22:22 -08001784 spin_unlock_bh(&bus->rxctl_lock);
1785 vfree(buf);
1786 goto done;
1787 }
1788 bus->rxctl = buf + doff;
1789 bus->rxctl_orig = buf;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001790 bus->rxlen = len - doff;
Franky Lindd43a012012-11-05 16:22:22 -08001791 spin_unlock_bh(&bus->rxctl_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001792
1793done:
1794 /* Awake any waiters */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001795 brcmf_sdio_dcmd_resp_wake(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001796}
1797
1798/* Pad read to blocksize for efficiency */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001799static void brcmf_sdio_pad(struct brcmf_sdio *bus, u16 *pad, u16 *rdlen)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001800{
1801 if (bus->roundup && bus->blocksize && *rdlen > bus->blocksize) {
1802 *pad = bus->blocksize - (*rdlen % bus->blocksize);
1803 if (*pad <= bus->roundup && *pad < bus->blocksize &&
1804 *rdlen + *pad + BRCMF_FIRSTREAD < MAX_RX_DATASZ)
1805 *rdlen += *pad;
Arend van Spriel9b2d2f2a82013-11-29 12:25:14 +01001806 } else if (*rdlen % bus->head_align) {
1807 *rdlen += bus->head_align - (*rdlen % bus->head_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001808 }
1809}
1810
Franky Lin4754fce2012-09-19 22:21:11 +02001811static uint brcmf_sdio_readframes(struct brcmf_sdio *bus, uint maxframes)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001812{
Arend van Spriel5b435de2011-10-05 13:19:03 +02001813 struct sk_buff *pkt; /* Packet for event or data frames */
1814 u16 pad; /* Number of pad bytes to read */
Arend van Spriel5b435de2011-10-05 13:19:03 +02001815 uint rxleft = 0; /* Remaining number of frames allowed */
Arend van Spriel349e7102013-03-03 12:45:28 +01001816 int ret; /* Return code from calls */
Arend van Spriel5b435de2011-10-05 13:19:03 +02001817 uint rxcount = 0; /* Total frames read */
Franky Lin6bc52312013-08-10 12:27:28 +02001818 struct brcmf_sdio_hdrinfo *rd = &bus->cur_read, rd_new;
Franky Lin4754fce2012-09-19 22:21:11 +02001819 u8 head_read = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001820
1821 brcmf_dbg(TRACE, "Enter\n");
1822
1823 /* Not finished unless we encounter no more frames indication */
Franky Lin4754fce2012-09-19 22:21:11 +02001824 bus->rxpending = true;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001825
Franky Lin4754fce2012-09-19 22:21:11 +02001826 for (rd->seq_num = bus->rx_seq, rxleft = maxframes;
Arend van Spriela1ce7a02015-02-06 18:36:42 +01001827 !bus->rxskip && rxleft && bus->sdiodev->state == BRCMF_SDIOD_DATA;
Franky Lin4754fce2012-09-19 22:21:11 +02001828 rd->seq_num++, rxleft--) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001829
1830 /* Handle glomming separately */
Arend van Sprielb83db862011-10-19 12:51:09 +02001831 if (bus->glomd || !skb_queue_empty(&bus->glom)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001832 u8 cnt;
1833 brcmf_dbg(GLOM, "calling rxglom: glomd %p, glom %p\n",
Arend van Sprielb83db862011-10-19 12:51:09 +02001834 bus->glomd, skb_peek(&bus->glom));
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001835 cnt = brcmf_sdio_rxglom(bus, rd->seq_num);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001836 brcmf_dbg(GLOM, "rxglom returned %d\n", cnt);
Franky Lin4754fce2012-09-19 22:21:11 +02001837 rd->seq_num += cnt - 1;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001838 rxleft = (rxleft > cnt) ? (rxleft - cnt) : 1;
1839 continue;
1840 }
1841
Franky Lin4754fce2012-09-19 22:21:11 +02001842 rd->len_left = rd->len;
1843 /* read header first for unknow frame length */
Franky Lin38b0b0d2012-11-05 16:22:24 -08001844 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001845 if (!rd->len) {
Arend van Spriela39be272013-12-12 11:58:58 +01001846 ret = brcmf_sdiod_recv_buf(bus->sdiodev,
Arend van Spriela39be272013-12-12 11:58:58 +01001847 bus->rxhdr, BRCMF_FIRSTREAD);
Franky Lin4754fce2012-09-19 22:21:11 +02001848 bus->sdcnt.f2rxhdrs++;
Arend van Spriel349e7102013-03-03 12:45:28 +01001849 if (ret < 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001850 brcmf_err("RXHEADER FAILED: %d\n",
Arend van Spriel349e7102013-03-03 12:45:28 +01001851 ret);
Franky Lin4754fce2012-09-19 22:21:11 +02001852 bus->sdcnt.rx_hdrfail++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001853 brcmf_sdio_rxfail(bus, true, true);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001854 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001855 continue;
1856 }
1857
Franky Lin4754fce2012-09-19 22:21:11 +02001858 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() || BRCMF_HDRS_ON(),
1859 bus->rxhdr, SDPCM_HDRLEN,
1860 "RxHdr:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001861
Franky Lin6bc52312013-08-10 12:27:28 +02001862 if (brcmf_sdio_hdparse(bus, bus->rxhdr, rd,
1863 BRCMF_SDIO_FT_NORMAL)) {
Franky Lin38b0b0d2012-11-05 16:22:24 -08001864 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001865 if (!bus->rxpending)
1866 break;
1867 else
1868 continue;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001869 }
1870
Franky Lin4754fce2012-09-19 22:21:11 +02001871 if (rd->channel == SDPCM_CONTROL_CHANNEL) {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001872 brcmf_sdio_read_control(bus, bus->rxhdr,
1873 rd->len,
1874 rd->dat_offset);
Franky Lin4754fce2012-09-19 22:21:11 +02001875 /* prepare the descriptor for the next read */
1876 rd->len = rd->len_nxtfrm << 4;
1877 rd->len_nxtfrm = 0;
1878 /* treat all packet as event if we don't know */
1879 rd->channel = SDPCM_EVENT_CHANNEL;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001880 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001881 continue;
1882 }
1883 rd->len_left = rd->len > BRCMF_FIRSTREAD ?
1884 rd->len - BRCMF_FIRSTREAD : 0;
1885 head_read = BRCMF_FIRSTREAD;
1886 }
1887
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001888 brcmf_sdio_pad(bus, &pad, &rd->len_left);
Franky Lin4754fce2012-09-19 22:21:11 +02001889
1890 pkt = brcmu_pkt_buf_get_skb(rd->len_left + head_read +
Arend van Spriel9b2d2f2a82013-11-29 12:25:14 +01001891 bus->head_align);
Franky Lin4754fce2012-09-19 22:21:11 +02001892 if (!pkt) {
1893 /* Give up on data, request rtx of events */
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001894 brcmf_err("brcmu_pkt_buf_get_skb failed\n");
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001895 brcmf_sdio_rxfail(bus, false,
Franky Lin4754fce2012-09-19 22:21:11 +02001896 RETRYCHAN(rd->channel));
Franky Lin38b0b0d2012-11-05 16:22:24 -08001897 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001898 continue;
1899 }
1900 skb_pull(pkt, head_read);
Arend van Spriel9b2d2f2a82013-11-29 12:25:14 +01001901 pkt_align(pkt, rd->len_left, bus->head_align);
Franky Lin4754fce2012-09-19 22:21:11 +02001902
Arend van Spriela7cdd822013-12-12 11:58:59 +01001903 ret = brcmf_sdiod_recv_pkt(bus->sdiodev, pkt);
Franky Lin4754fce2012-09-19 22:21:11 +02001904 bus->sdcnt.f2rxdata++;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001905 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001906
Arend van Spriel349e7102013-03-03 12:45:28 +01001907 if (ret < 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001908 brcmf_err("read %d bytes from channel %d failed: %d\n",
Arend van Spriel349e7102013-03-03 12:45:28 +01001909 rd->len, rd->channel, ret);
Franky Lin4754fce2012-09-19 22:21:11 +02001910 brcmu_pkt_buf_free_skb(pkt);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001911 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001912 brcmf_sdio_rxfail(bus, true,
Franky Lin4754fce2012-09-19 22:21:11 +02001913 RETRYCHAN(rd->channel));
Franky Lin38b0b0d2012-11-05 16:22:24 -08001914 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001915 continue;
1916 }
1917
1918 if (head_read) {
1919 skb_push(pkt, head_read);
1920 memcpy(pkt->data, bus->rxhdr, head_read);
1921 head_read = 0;
1922 } else {
1923 memcpy(bus->rxhdr, pkt->data, SDPCM_HDRLEN);
1924 rd_new.seq_num = rd->seq_num;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001925 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin6bc52312013-08-10 12:27:28 +02001926 if (brcmf_sdio_hdparse(bus, bus->rxhdr, &rd_new,
1927 BRCMF_SDIO_FT_NORMAL)) {
Franky Lin4754fce2012-09-19 22:21:11 +02001928 rd->len = 0;
1929 brcmu_pkt_buf_free_skb(pkt);
1930 }
Arend van Spriel80969832012-06-09 22:51:44 +02001931 bus->sdcnt.rx_readahead_cnt++;
Franky Lin4754fce2012-09-19 22:21:11 +02001932 if (rd->len != roundup(rd_new.len, 16)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001933 brcmf_err("frame length mismatch:read %d, should be %d\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001934 rd->len,
1935 roundup(rd_new.len, 16) >> 4);
1936 rd->len = 0;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001937 brcmf_sdio_rxfail(bus, true, true);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001938 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001939 brcmu_pkt_buf_free_skb(pkt);
1940 continue;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001941 }
Franky Lin38b0b0d2012-11-05 16:22:24 -08001942 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001943 rd->len_nxtfrm = rd_new.len_nxtfrm;
1944 rd->channel = rd_new.channel;
1945 rd->dat_offset = rd_new.dat_offset;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001946
Joe Perches1e023822012-01-15 00:38:40 -08001947 brcmf_dbg_hex_dump(!(BRCMF_BYTES_ON() &&
1948 BRCMF_DATA_ON()) &&
1949 BRCMF_HDRS_ON(),
1950 bus->rxhdr, SDPCM_HDRLEN,
1951 "RxHdr:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001952
Franky Lin4754fce2012-09-19 22:21:11 +02001953 if (rd_new.channel == SDPCM_CONTROL_CHANNEL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001954 brcmf_err("readahead on control packet %d?\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001955 rd_new.seq_num);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001956 /* Force retry w/normal header read */
Franky Lin4754fce2012-09-19 22:21:11 +02001957 rd->len = 0;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001958 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001959 brcmf_sdio_rxfail(bus, false, true);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001960 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001961 brcmu_pkt_buf_free_skb(pkt);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001962 continue;
1963 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02001964 }
1965
Joe Perches1e023822012-01-15 00:38:40 -08001966 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_DATA_ON(),
Franky Lin4754fce2012-09-19 22:21:11 +02001967 pkt->data, rd->len, "Rx Data:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001968
Arend van Spriel5b435de2011-10-05 13:19:03 +02001969 /* Save superframe descriptor and allocate packet frame */
Franky Lin4754fce2012-09-19 22:21:11 +02001970 if (rd->channel == SDPCM_GLOM_CHANNEL) {
Franky Lin6bc52312013-08-10 12:27:28 +02001971 if (SDPCM_GLOMDESC(&bus->rxhdr[SDPCM_HWHDR_LEN])) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001972 brcmf_dbg(GLOM, "glom descriptor, %d bytes:\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001973 rd->len);
Joe Perches1e023822012-01-15 00:38:40 -08001974 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
Franky Lin4754fce2012-09-19 22:21:11 +02001975 pkt->data, rd->len,
Joe Perches1e023822012-01-15 00:38:40 -08001976 "Glom Data:\n");
Franky Lin4754fce2012-09-19 22:21:11 +02001977 __skb_trim(pkt, rd->len);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001978 skb_pull(pkt, SDPCM_HDRLEN);
1979 bus->glomd = pkt;
1980 } else {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001981 brcmf_err("%s: glom superframe w/o "
Arend van Spriel5b435de2011-10-05 13:19:03 +02001982 "descriptor!\n", __func__);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001983 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001984 brcmf_sdio_rxfail(bus, false, false);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001985 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001986 }
Franky Lin4754fce2012-09-19 22:21:11 +02001987 /* prepare the descriptor for the next read */
1988 rd->len = rd->len_nxtfrm << 4;
1989 rd->len_nxtfrm = 0;
1990 /* treat all packet as event if we don't know */
1991 rd->channel = SDPCM_EVENT_CHANNEL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001992 continue;
1993 }
1994
1995 /* Fill in packet len and prio, deliver upward */
Franky Lin4754fce2012-09-19 22:21:11 +02001996 __skb_trim(pkt, rd->len);
1997 skb_pull(pkt, rd->dat_offset);
1998
Franky Linc56caa9d2016-04-11 11:35:25 +02001999 if (pkt->len == 0)
2000 brcmu_pkt_buf_free_skb(pkt);
2001 else if (rd->channel == SDPCM_EVENT_CHANNEL)
2002 brcmf_rx_event(bus->sdiodev->dev, pkt);
2003 else
2004 brcmf_rx_frame(bus->sdiodev->dev, pkt,
2005 false);
2006
Franky Lin4754fce2012-09-19 22:21:11 +02002007 /* prepare the descriptor for the next read */
2008 rd->len = rd->len_nxtfrm << 4;
2009 rd->len_nxtfrm = 0;
2010 /* treat all packet as event if we don't know */
2011 rd->channel = SDPCM_EVENT_CHANNEL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002012 }
Franky Lin4754fce2012-09-19 22:21:11 +02002013
Arend van Spriel5b435de2011-10-05 13:19:03 +02002014 rxcount = maxframes - rxleft;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002015 /* Message if we hit the limit */
2016 if (!rxleft)
Franky Lin4754fce2012-09-19 22:21:11 +02002017 brcmf_dbg(DATA, "hit rx limit of %d frames\n", maxframes);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002018 else
Arend van Spriel5b435de2011-10-05 13:19:03 +02002019 brcmf_dbg(DATA, "processed %d frames\n", rxcount);
2020 /* Back off rxseq if awaiting rtx, update rx_seq */
2021 if (bus->rxskip)
Franky Lin4754fce2012-09-19 22:21:11 +02002022 rd->seq_num--;
2023 bus->rx_seq = rd->seq_num;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002024
2025 return rxcount;
2026}
2027
Arend van Spriel5b435de2011-10-05 13:19:03 +02002028static void
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002029brcmf_sdio_wait_event_wakeup(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002030{
Kosuke Tatsukawaa7decc42015-12-10 13:43:08 +01002031 wake_up_interruptible(&bus->ctrl_wait);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002032 return;
2033}
2034
Franky Lin8da9d2c2013-11-29 12:25:10 +01002035static int brcmf_sdio_txpkt_hdalign(struct brcmf_sdio *bus, struct sk_buff *pkt)
2036{
Arend van Spriele217d1c2013-11-29 12:25:13 +01002037 u16 head_pad;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002038 u8 *dat_buf;
2039
Franky Lin8da9d2c2013-11-29 12:25:10 +01002040 dat_buf = (u8 *)(pkt->data);
2041
2042 /* Check head padding */
Arend van Spriele217d1c2013-11-29 12:25:13 +01002043 head_pad = ((unsigned long)dat_buf % bus->head_align);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002044 if (head_pad) {
2045 if (skb_headroom(pkt) < head_pad) {
2046 bus->sdiodev->bus_if->tx_realloc++;
2047 head_pad = 0;
2048 if (skb_cow(pkt, head_pad))
2049 return -ENOMEM;
2050 }
2051 skb_push(pkt, head_pad);
2052 dat_buf = (u8 *)(pkt->data);
2053 memset(dat_buf, 0, head_pad + bus->tx_hdrlen);
2054 }
2055 return head_pad;
2056}
2057
Franky Lin5491c112013-09-25 13:05:43 +02002058/**
2059 * struct brcmf_skbuff_cb reserves first two bytes in sk_buff::cb for
2060 * bus layer usage.
2061 */
Franky Linb05e9252013-08-10 12:27:26 +02002062/* flag marking a dummy skb added for DMA alignment requirement */
Franky Lin5491c112013-09-25 13:05:43 +02002063#define ALIGN_SKB_FLAG 0x8000
Franky Linb05e9252013-08-10 12:27:26 +02002064/* bit mask of data length chopped from the previous packet */
Franky Lin5491c112013-09-25 13:05:43 +02002065#define ALIGN_SKB_CHOP_LEN_MASK 0x7fff
2066
Franky Lin8da9d2c2013-11-29 12:25:10 +01002067static int brcmf_sdio_txpkt_prep_sg(struct brcmf_sdio *bus,
Arend van Spriela64304f2013-10-15 15:44:56 +02002068 struct sk_buff_head *pktq,
Franky Lin8da9d2c2013-11-29 12:25:10 +01002069 struct sk_buff *pkt, u16 total_len)
Arend van Spriela64304f2013-10-15 15:44:56 +02002070{
Franky Lin8da9d2c2013-11-29 12:25:10 +01002071 struct brcmf_sdio_dev *sdiodev;
Arend van Spriela64304f2013-10-15 15:44:56 +02002072 struct sk_buff *pkt_pad;
Arend van Spriele217d1c2013-11-29 12:25:13 +01002073 u16 tail_pad, tail_chop, chain_pad;
Arend van Spriela64304f2013-10-15 15:44:56 +02002074 unsigned int blksize;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002075 bool lastfrm;
2076 int ntail, ret;
Arend van Spriela64304f2013-10-15 15:44:56 +02002077
Franky Lin8da9d2c2013-11-29 12:25:10 +01002078 sdiodev = bus->sdiodev;
Arend van Spriela64304f2013-10-15 15:44:56 +02002079 blksize = sdiodev->func[SDIO_FUNC_2]->cur_blksize;
Arend van Spriela64304f2013-10-15 15:44:56 +02002080 /* sg entry alignment should be a divisor of block size */
Arend van Spriele217d1c2013-11-29 12:25:13 +01002081 WARN_ON(blksize % bus->sgentry_align);
Arend van Spriela64304f2013-10-15 15:44:56 +02002082
2083 /* Check tail padding */
Franky Lin8da9d2c2013-11-29 12:25:10 +01002084 lastfrm = skb_queue_is_last(pktq, pkt);
2085 tail_pad = 0;
Arend van Spriele217d1c2013-11-29 12:25:13 +01002086 tail_chop = pkt->len % bus->sgentry_align;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002087 if (tail_chop)
Arend van Spriele217d1c2013-11-29 12:25:13 +01002088 tail_pad = bus->sgentry_align - tail_chop;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002089 chain_pad = (total_len + tail_pad) % blksize;
2090 if (lastfrm && chain_pad)
2091 tail_pad += blksize - chain_pad;
Arend van Spriela64304f2013-10-15 15:44:56 +02002092 if (skb_tailroom(pkt) < tail_pad && pkt->len > blksize) {
Arend van Spriel1eb43012014-02-20 18:55:55 +01002093 pkt_pad = brcmu_pkt_buf_get_skb(tail_pad + tail_chop +
2094 bus->head_align);
Arend van Spriela64304f2013-10-15 15:44:56 +02002095 if (pkt_pad == NULL)
2096 return -ENOMEM;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002097 ret = brcmf_sdio_txpkt_hdalign(bus, pkt_pad);
Dave Jones2dc3a8e2014-03-06 15:51:08 -05002098 if (unlikely(ret < 0)) {
2099 kfree_skb(pkt_pad);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002100 return ret;
Dave Jones2dc3a8e2014-03-06 15:51:08 -05002101 }
Arend van Spriela64304f2013-10-15 15:44:56 +02002102 memcpy(pkt_pad->data,
2103 pkt->data + pkt->len - tail_chop,
2104 tail_chop);
Arend van Spriel5aa9f0e2014-02-25 20:30:27 +01002105 *(u16 *)(pkt_pad->cb) = ALIGN_SKB_FLAG + tail_chop;
Arend van Spriela64304f2013-10-15 15:44:56 +02002106 skb_trim(pkt, pkt->len - tail_chop);
Arend van Spriel1eb43012014-02-20 18:55:55 +01002107 skb_trim(pkt_pad, tail_pad + tail_chop);
Arend van Spriela64304f2013-10-15 15:44:56 +02002108 __skb_queue_after(pktq, pkt, pkt_pad);
2109 } else {
2110 ntail = pkt->data_len + tail_pad -
2111 (pkt->end - pkt->tail);
2112 if (skb_cloned(pkt) || ntail > 0)
2113 if (pskb_expand_head(pkt, 0, ntail, GFP_ATOMIC))
2114 return -ENOMEM;
2115 if (skb_linearize(pkt))
2116 return -ENOMEM;
Arend van Spriela64304f2013-10-15 15:44:56 +02002117 __skb_put(pkt, tail_pad);
2118 }
2119
Franky Lin8da9d2c2013-11-29 12:25:10 +01002120 return tail_pad;
Arend van Spriela64304f2013-10-15 15:44:56 +02002121}
2122
Franky Linb05e9252013-08-10 12:27:26 +02002123/**
2124 * brcmf_sdio_txpkt_prep - packet preparation for transmit
2125 * @bus: brcmf_sdio structure pointer
2126 * @pktq: packet list pointer
2127 * @chan: virtual channel to transmit the packet
2128 *
2129 * Processes to be applied to the packet
2130 * - Align data buffer pointer
2131 * - Align data buffer length
2132 * - Prepare header
2133 * Return: negative value if there is error
2134 */
2135static int
2136brcmf_sdio_txpkt_prep(struct brcmf_sdio *bus, struct sk_buff_head *pktq,
2137 uint chan)
2138{
Franky Lin8da9d2c2013-11-29 12:25:10 +01002139 u16 head_pad, total_len;
Arend van Spriela64304f2013-10-15 15:44:56 +02002140 struct sk_buff *pkt_next;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002141 u8 txseq;
2142 int ret;
Franky Lin6bc52312013-08-10 12:27:28 +02002143 struct brcmf_sdio_hdrinfo hd_info = {0};
Franky Linb05e9252013-08-10 12:27:26 +02002144
Franky Lin8da9d2c2013-11-29 12:25:10 +01002145 txseq = bus->tx_seq;
2146 total_len = 0;
2147 skb_queue_walk(pktq, pkt_next) {
2148 /* alignment packet inserted in previous
2149 * loop cycle can be skipped as it is
2150 * already properly aligned and does not
2151 * need an sdpcm header.
2152 */
Arend van Spriel5aa9f0e2014-02-25 20:30:27 +01002153 if (*(u16 *)(pkt_next->cb) & ALIGN_SKB_FLAG)
Franky Lin8da9d2c2013-11-29 12:25:10 +01002154 continue;
Franky Linb05e9252013-08-10 12:27:26 +02002155
Franky Lin8da9d2c2013-11-29 12:25:10 +01002156 /* align packet data pointer */
2157 ret = brcmf_sdio_txpkt_hdalign(bus, pkt_next);
2158 if (ret < 0)
2159 return ret;
2160 head_pad = (u16)ret;
2161 if (head_pad)
Arend van Spriel1eb43012014-02-20 18:55:55 +01002162 memset(pkt_next->data + bus->tx_hdrlen, 0, head_pad);
Franky Linb05e9252013-08-10 12:27:26 +02002163
Franky Lin8da9d2c2013-11-29 12:25:10 +01002164 total_len += pkt_next->len;
Franky Linb05e9252013-08-10 12:27:26 +02002165
Arend van Spriela64304f2013-10-15 15:44:56 +02002166 hd_info.len = pkt_next->len;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002167 hd_info.lastfrm = skb_queue_is_last(pktq, pkt_next);
2168 if (bus->txglom && pktq->qlen > 1) {
2169 ret = brcmf_sdio_txpkt_prep_sg(bus, pktq,
2170 pkt_next, total_len);
2171 if (ret < 0)
2172 return ret;
2173 hd_info.tail_pad = (u16)ret;
2174 total_len += (u16)ret;
2175 }
2176
2177 hd_info.channel = chan;
2178 hd_info.dat_offset = head_pad + bus->tx_hdrlen;
2179 hd_info.seq_num = txseq++;
2180
2181 /* Now fill the header */
2182 brcmf_sdio_hdpack(bus, pkt_next->data, &hd_info);
2183
2184 if (BRCMF_BYTES_ON() &&
2185 ((BRCMF_CTL_ON() && chan == SDPCM_CONTROL_CHANNEL) ||
2186 (BRCMF_DATA_ON() && chan != SDPCM_CONTROL_CHANNEL)))
Hante Meuleman47ab4cd2014-02-25 20:30:29 +01002187 brcmf_dbg_hex_dump(true, pkt_next->data, hd_info.len,
Franky Lin8da9d2c2013-11-29 12:25:10 +01002188 "Tx Frame:\n");
2189 else if (BRCMF_HDRS_ON())
Hante Meuleman47ab4cd2014-02-25 20:30:29 +01002190 brcmf_dbg_hex_dump(true, pkt_next->data,
Franky Lin8da9d2c2013-11-29 12:25:10 +01002191 head_pad + bus->tx_hdrlen,
2192 "Tx Header:\n");
Franky Linb05e9252013-08-10 12:27:26 +02002193 }
Franky Lin8da9d2c2013-11-29 12:25:10 +01002194 /* Hardware length tag of the first packet should be total
2195 * length of the chain (including padding)
2196 */
2197 if (bus->txglom)
2198 brcmf_sdio_update_hwhdr(pktq->next->data, total_len);
Franky Linb05e9252013-08-10 12:27:26 +02002199 return 0;
2200}
2201
2202/**
2203 * brcmf_sdio_txpkt_postp - packet post processing for transmit
2204 * @bus: brcmf_sdio structure pointer
2205 * @pktq: packet list pointer
2206 *
2207 * Processes to be applied to the packet
2208 * - Remove head padding
2209 * - Remove tail padding
2210 */
2211static void
2212brcmf_sdio_txpkt_postp(struct brcmf_sdio *bus, struct sk_buff_head *pktq)
2213{
2214 u8 *hdr;
2215 u32 dat_offset;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002216 u16 tail_pad;
Arend van Spriel5aa9f0e2014-02-25 20:30:27 +01002217 u16 dummy_flags, chop_len;
Franky Linb05e9252013-08-10 12:27:26 +02002218 struct sk_buff *pkt_next, *tmp, *pkt_prev;
2219
2220 skb_queue_walk_safe(pktq, pkt_next, tmp) {
Arend van Spriel5aa9f0e2014-02-25 20:30:27 +01002221 dummy_flags = *(u16 *)(pkt_next->cb);
Franky Lin5491c112013-09-25 13:05:43 +02002222 if (dummy_flags & ALIGN_SKB_FLAG) {
2223 chop_len = dummy_flags & ALIGN_SKB_CHOP_LEN_MASK;
Franky Linb05e9252013-08-10 12:27:26 +02002224 if (chop_len) {
2225 pkt_prev = pkt_next->prev;
Franky Linb05e9252013-08-10 12:27:26 +02002226 skb_put(pkt_prev, chop_len);
2227 }
2228 __skb_unlink(pkt_next, pktq);
2229 brcmu_pkt_buf_free_skb(pkt_next);
2230 } else {
Franky Lin8da9d2c2013-11-29 12:25:10 +01002231 hdr = pkt_next->data + bus->tx_hdrlen - SDPCM_SWHDR_LEN;
Franky Linb05e9252013-08-10 12:27:26 +02002232 dat_offset = le32_to_cpu(*(__le32 *)hdr);
2233 dat_offset = (dat_offset & SDPCM_DOFFSET_MASK) >>
2234 SDPCM_DOFFSET_SHIFT;
2235 skb_pull(pkt_next, dat_offset);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002236 if (bus->txglom) {
2237 tail_pad = le16_to_cpu(*(__le16 *)(hdr - 2));
2238 skb_trim(pkt_next, pkt_next->len - tail_pad);
2239 }
Franky Linb05e9252013-08-10 12:27:26 +02002240 }
2241 }
2242}
2243
Arend van Spriel5b435de2011-10-05 13:19:03 +02002244/* Writes a HW/SW header into the packet and sends it. */
2245/* Assumes: (a) header space already there, (b) caller holds lock */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002246static int brcmf_sdio_txpkt(struct brcmf_sdio *bus, struct sk_buff_head *pktq,
2247 uint chan)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002248{
2249 int ret;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002250 struct sk_buff *pkt_next, *tmp;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002251
2252 brcmf_dbg(TRACE, "Enter\n");
2253
Franky Lin8da9d2c2013-11-29 12:25:10 +01002254 ret = brcmf_sdio_txpkt_prep(bus, pktq, chan);
Franky Linb05e9252013-08-10 12:27:26 +02002255 if (ret)
2256 goto done;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002257
Franky Lin38b0b0d2012-11-05 16:22:24 -08002258 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriela7cdd822013-12-12 11:58:59 +01002259 ret = brcmf_sdiod_send_pkt(bus->sdiodev, pktq);
Arend van Spriel80969832012-06-09 22:51:44 +02002260 bus->sdcnt.f2txdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002261
Hante Meuleman81c78832014-02-25 20:30:35 +01002262 if (ret < 0)
2263 brcmf_sdio_txfail(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002264
Franky Lin38b0b0d2012-11-05 16:22:24 -08002265 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002266
2267done:
Franky Lin8da9d2c2013-11-29 12:25:10 +01002268 brcmf_sdio_txpkt_postp(bus, pktq);
2269 if (ret == 0)
2270 bus->tx_seq = (bus->tx_seq + pktq->qlen) % SDPCM_SEQ_WRAP;
2271 skb_queue_walk_safe(pktq, pkt_next, tmp) {
2272 __skb_unlink(pkt_next, pktq);
Franky Lin7b584392017-03-10 21:17:03 +00002273 brcmf_proto_bcdc_txcomplete(bus->sdiodev->dev, pkt_next,
2274 ret == 0);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002275 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002276 return ret;
2277}
2278
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002279static uint brcmf_sdio_sendfromq(struct brcmf_sdio *bus, uint maxframes)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002280{
2281 struct sk_buff *pkt;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002282 struct sk_buff_head pktq;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002283 u32 intstatus = 0;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002284 int ret = 0, prec_out, i;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002285 uint cnt = 0;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002286 u8 tx_prec_map, pkt_num;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002287
Arend van Spriel5b435de2011-10-05 13:19:03 +02002288 brcmf_dbg(TRACE, "Enter\n");
2289
2290 tx_prec_map = ~bus->flowcontrol;
2291
2292 /* Send frames until the limit or some other event */
Franky Lin8da9d2c2013-11-29 12:25:10 +01002293 for (cnt = 0; (cnt < maxframes) && data_ok(bus);) {
2294 pkt_num = 1;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002295 if (bus->txglom)
2296 pkt_num = min_t(u8, bus->tx_max - bus->tx_seq,
Arend van Sprielaf1fa212014-02-27 19:25:00 +01002297 bus->sdiodev->txglomsz);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002298 pkt_num = min_t(u32, pkt_num,
2299 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol));
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002300 __skb_queue_head_init(&pktq);
2301 spin_lock_bh(&bus->txq_lock);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002302 for (i = 0; i < pkt_num; i++) {
2303 pkt = brcmu_pktq_mdeq(&bus->txq, tx_prec_map,
2304 &prec_out);
2305 if (pkt == NULL)
2306 break;
2307 __skb_queue_tail(&pktq, pkt);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002308 }
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002309 spin_unlock_bh(&bus->txq_lock);
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002310 if (i == 0)
Franky Lin8da9d2c2013-11-29 12:25:10 +01002311 break;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002312
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002313 ret = brcmf_sdio_txpkt(bus, &pktq, SDPCM_DATA_CHANNEL);
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002314
Franky Lin8da9d2c2013-11-29 12:25:10 +01002315 cnt += i;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002316
2317 /* In poll mode, need to check for other events */
Hante Meulemanb6a8cf22014-02-25 20:30:33 +01002318 if (!bus->intr) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02002319 /* Check device status, signal pending interrupt */
Franky Lin38b0b0d2012-11-05 16:22:24 -08002320 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin5c15c232012-05-04 18:27:37 -07002321 ret = r_sdreg32(bus, &intstatus,
2322 offsetof(struct sdpcmd_regs,
2323 intstatus));
Franky Lin38b0b0d2012-11-05 16:22:24 -08002324 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel80969832012-06-09 22:51:44 +02002325 bus->sdcnt.f2txdata++;
Franky Lin5c15c232012-05-04 18:27:37 -07002326 if (ret != 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002327 break;
2328 if (intstatus & bus->hostintmask)
Franky Lin1d382272012-09-13 21:11:59 +02002329 atomic_set(&bus->ipend, 1);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002330 }
2331 }
2332
2333 /* Deflow-control stack if needed */
Arend van Spriela1ce7a02015-02-06 18:36:42 +01002334 if ((bus->sdiodev->state == BRCMF_SDIOD_DATA) &&
Franky Linc8bf3482011-12-16 18:37:08 -08002335 bus->txoff && (pktq_len(&bus->txq) < TXLOW)) {
Hante Meuleman90d03ff2012-09-11 21:18:46 +02002336 bus->txoff = false;
Franky Lin20ec4f52017-03-10 21:17:02 +00002337 brcmf_proto_bcdc_txflowblock(bus->sdiodev->dev, false);
Franky Linc8bf3482011-12-16 18:37:08 -08002338 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002339
2340 return cnt;
2341}
2342
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002343static int brcmf_sdio_tx_ctrlframe(struct brcmf_sdio *bus, u8 *frame, u16 len)
2344{
2345 u8 doff;
2346 u16 pad;
2347 uint retries = 0;
2348 struct brcmf_sdio_hdrinfo hd_info = {0};
2349 int ret;
2350
2351 brcmf_dbg(TRACE, "Enter\n");
2352
2353 /* Back the pointer to make room for bus header */
2354 frame -= bus->tx_hdrlen;
2355 len += bus->tx_hdrlen;
2356
2357 /* Add alignment padding (optional for ctl frames) */
2358 doff = ((unsigned long)frame % bus->head_align);
2359 if (doff) {
2360 frame -= doff;
2361 len += doff;
2362 memset(frame + bus->tx_hdrlen, 0, doff);
2363 }
2364
2365 /* Round send length to next SDIO block */
2366 pad = 0;
2367 if (bus->roundup && bus->blocksize && (len > bus->blocksize)) {
2368 pad = bus->blocksize - (len % bus->blocksize);
2369 if ((pad > bus->roundup) || (pad >= bus->blocksize))
2370 pad = 0;
2371 } else if (len % bus->head_align) {
2372 pad = bus->head_align - (len % bus->head_align);
2373 }
2374 len += pad;
2375
2376 hd_info.len = len - pad;
2377 hd_info.channel = SDPCM_CONTROL_CHANNEL;
2378 hd_info.dat_offset = doff + bus->tx_hdrlen;
2379 hd_info.seq_num = bus->tx_seq;
2380 hd_info.lastfrm = true;
2381 hd_info.tail_pad = pad;
2382 brcmf_sdio_hdpack(bus, frame, &hd_info);
2383
2384 if (bus->txglom)
2385 brcmf_sdio_update_hwhdr(frame, len);
2386
2387 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_CTL_ON(),
2388 frame, len, "Tx Frame:\n");
2389 brcmf_dbg_hex_dump(!(BRCMF_BYTES_ON() && BRCMF_CTL_ON()) &&
2390 BRCMF_HDRS_ON(),
2391 frame, min_t(u16, len, 16), "TxHdr:\n");
2392
2393 do {
2394 ret = brcmf_sdiod_send_buf(bus->sdiodev, frame, len);
2395
2396 if (ret < 0)
2397 brcmf_sdio_txfail(bus);
2398 else
2399 bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQ_WRAP;
2400 } while (ret < 0 && retries++ < TXRETRIES);
2401
2402 return ret;
2403}
2404
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002405static void brcmf_sdio_bus_stop(struct device *dev)
Franky Lina9ffda82011-12-16 18:37:12 -08002406{
2407 u32 local_hostintmask;
2408 u8 saveclk;
Franky Lina9ffda82011-12-16 18:37:12 -08002409 int err;
2410 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01002411 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Lina9ffda82011-12-16 18:37:12 -08002412 struct brcmf_sdio *bus = sdiodev->bus;
2413
2414 brcmf_dbg(TRACE, "Enter\n");
2415
2416 if (bus->watchdog_tsk) {
2417 send_sig(SIGTERM, bus->watchdog_tsk, 1);
2418 kthread_stop(bus->watchdog_tsk);
2419 bus->watchdog_tsk = NULL;
2420 }
2421
Arend van Spriela1ce7a02015-02-06 18:36:42 +01002422 if (sdiodev->state != BRCMF_SDIOD_NOMEDIUM) {
Arend van Sprielbb350712014-01-13 22:20:29 +01002423 sdio_claim_host(sdiodev->func[1]);
Franky Lina9ffda82011-12-16 18:37:12 -08002424
Arend van Sprielbb350712014-01-13 22:20:29 +01002425 /* Enable clock for device interrupts */
2426 brcmf_sdio_bus_sleep(bus, false, false);
Franky Lina9ffda82011-12-16 18:37:12 -08002427
Arend van Sprielbb350712014-01-13 22:20:29 +01002428 /* Disable and clear interrupts at the chip level also */
2429 w_sdreg32(bus, 0, offsetof(struct sdpcmd_regs, hostintmask));
2430 local_hostintmask = bus->hostintmask;
2431 bus->hostintmask = 0;
Franky Lina9ffda82011-12-16 18:37:12 -08002432
Arend van Sprielbb350712014-01-13 22:20:29 +01002433 /* Force backplane clocks to assure F2 interrupt propagates */
2434 saveclk = brcmf_sdiod_regrb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
2435 &err);
2436 if (!err)
2437 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
2438 (saveclk | SBSDIO_FORCE_HT), &err);
2439 if (err)
2440 brcmf_err("Failed to force clock for F2: err %d\n",
2441 err);
Franky Lina9ffda82011-12-16 18:37:12 -08002442
Arend van Sprielbb350712014-01-13 22:20:29 +01002443 /* Turn off the bus (F2), free any pending packets */
2444 brcmf_dbg(INTR, "disable SDIO interrupts\n");
2445 sdio_disable_func(sdiodev->func[SDIO_FUNC_2]);
2446
2447 /* Clear any pending interrupts now that F2 is disabled */
2448 w_sdreg32(bus, local_hostintmask,
2449 offsetof(struct sdpcmd_regs, intstatus));
2450
2451 sdio_release_host(sdiodev->func[1]);
Franky Lina9ffda82011-12-16 18:37:12 -08002452 }
Franky Lina9ffda82011-12-16 18:37:12 -08002453 /* Clear the data packet queues */
2454 brcmu_pktq_flush(&bus->txq, true, NULL, NULL);
2455
2456 /* Clear any held glomming stuff */
Markus Elfring297540f2015-02-04 20:28:49 +01002457 brcmu_pkt_buf_free_skb(bus->glomd);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002458 brcmf_sdio_free_glom(bus);
Franky Lina9ffda82011-12-16 18:37:12 -08002459
2460 /* Clear rx control and wake any waiters */
Franky Lindd43a012012-11-05 16:22:22 -08002461 spin_lock_bh(&bus->rxctl_lock);
Franky Lina9ffda82011-12-16 18:37:12 -08002462 bus->rxlen = 0;
Franky Lindd43a012012-11-05 16:22:22 -08002463 spin_unlock_bh(&bus->rxctl_lock);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002464 brcmf_sdio_dcmd_resp_wake(bus);
Franky Lina9ffda82011-12-16 18:37:12 -08002465
2466 /* Reset some F2 state stuff */
2467 bus->rxskip = false;
2468 bus->tx_seq = bus->rx_seq = 0;
Franky Lina9ffda82011-12-16 18:37:12 -08002469}
2470
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002471static inline void brcmf_sdio_clrintr(struct brcmf_sdio *bus)
Franky Linba89bf12012-04-27 18:56:59 -07002472{
Hante Meulemanaf5b5e62016-02-17 11:27:08 +01002473 struct brcmf_sdio_dev *sdiodev;
Franky Linba89bf12012-04-27 18:56:59 -07002474 unsigned long flags;
2475
Hante Meulemanaf5b5e62016-02-17 11:27:08 +01002476 sdiodev = bus->sdiodev;
2477 if (sdiodev->oob_irq_requested) {
2478 spin_lock_irqsave(&sdiodev->irq_en_lock, flags);
2479 if (!sdiodev->irq_en && !atomic_read(&bus->ipend)) {
2480 enable_irq(sdiodev->settings->bus.sdio.oob_irq_nr);
2481 sdiodev->irq_en = true;
Hante Meuleman668761a2013-04-12 10:55:55 +02002482 }
Hante Meulemanaf5b5e62016-02-17 11:27:08 +01002483 spin_unlock_irqrestore(&sdiodev->irq_en_lock, flags);
Franky Linba89bf12012-04-27 18:56:59 -07002484 }
Franky Linba89bf12012-04-27 18:56:59 -07002485}
Franky Linba89bf12012-04-27 18:56:59 -07002486
Franky Lin45316032012-09-13 21:12:03 +02002487static int brcmf_sdio_intr_rstatus(struct brcmf_sdio *bus)
2488{
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01002489 struct brcmf_core *buscore;
Franky Lin45316032012-09-13 21:12:03 +02002490 u32 addr;
2491 unsigned long val;
Hante Meuleman5cbb9c22014-03-06 10:16:12 +01002492 int ret;
Franky Lin45316032012-09-13 21:12:03 +02002493
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01002494 buscore = brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV);
2495 addr = buscore->base + offsetof(struct sdpcmd_regs, intstatus);
Franky Lin45316032012-09-13 21:12:03 +02002496
Arend van Spriela39be272013-12-12 11:58:58 +01002497 val = brcmf_sdiod_regrl(bus->sdiodev, addr, &ret);
Franky Lin45316032012-09-13 21:12:03 +02002498 bus->sdcnt.f1regdata++;
2499 if (ret != 0)
Hante Meuleman5cbb9c22014-03-06 10:16:12 +01002500 return ret;
Franky Lin45316032012-09-13 21:12:03 +02002501
2502 val &= bus->hostintmask;
2503 atomic_set(&bus->fcstate, !!(val & I_HMB_FC_STATE));
2504
2505 /* Clear interrupts */
2506 if (val) {
Arend van Spriela39be272013-12-12 11:58:58 +01002507 brcmf_sdiod_regwl(bus->sdiodev, addr, val, &ret);
Franky Lin45316032012-09-13 21:12:03 +02002508 bus->sdcnt.f1regdata++;
Vineet Guptad3928d02015-07-09 13:43:18 +05302509 atomic_or(val, &bus->intstatus);
Franky Lin45316032012-09-13 21:12:03 +02002510 }
2511
2512 return ret;
2513}
2514
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002515static void brcmf_sdio_dpc(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002516{
Franky Lin45316032012-09-13 21:12:03 +02002517 u32 newstatus = 0;
2518 unsigned long intstatus;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002519 uint txlimit = bus->txbound; /* Tx frames to send before resched */
Hante Meulemanb6a8cf22014-02-25 20:30:33 +01002520 uint framecnt; /* Temporary counter of tx/rx frames */
Hante Meuleman5cbb9c22014-03-06 10:16:12 +01002521 int err = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002522
2523 brcmf_dbg(TRACE, "Enter\n");
2524
Franky Lin38b0b0d2012-11-05 16:22:24 -08002525 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002526
2527 /* If waiting for HTAVAIL, check status */
Piotr Haber4a3da992013-04-11 13:28:46 +02002528 if (!bus->sr_enabled && bus->clkstate == CLK_PENDING) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02002529 u8 clkctl, devctl = 0;
2530
Joe Perches8ae74652012-01-15 00:38:38 -08002531#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02002532 /* Check for inconsistent device control */
Arend van Spriela39be272013-12-12 11:58:58 +01002533 devctl = brcmf_sdiod_regrb(bus->sdiodev,
2534 SBSDIO_DEVICE_CTL, &err);
Joe Perches8ae74652012-01-15 00:38:38 -08002535#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02002536
2537 /* Read CSR, if clock on switch to AVAIL, else ignore */
Arend van Spriela39be272013-12-12 11:58:58 +01002538 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
2539 SBSDIO_FUNC1_CHIPCLKCSR, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002540
Arend van Sprielc3203372013-04-03 12:40:44 +02002541 brcmf_dbg(SDIO, "DPC: PENDING, devctl 0x%02x clkctl 0x%02x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02002542 devctl, clkctl);
2543
2544 if (SBSDIO_HTAV(clkctl)) {
Arend van Spriela39be272013-12-12 11:58:58 +01002545 devctl = brcmf_sdiod_regrb(bus->sdiodev,
2546 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002547 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
Arend van Spriela39be272013-12-12 11:58:58 +01002548 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
2549 devctl, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002550 bus->clkstate = CLK_AVAIL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002551 }
2552 }
2553
Arend van Spriel5b435de2011-10-05 13:19:03 +02002554 /* Make sure backplane clock is on */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002555 brcmf_sdio_bus_sleep(bus, false, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002556
2557 /* Pending interrupt indicates new device status */
Franky Lin1d382272012-09-13 21:11:59 +02002558 if (atomic_read(&bus->ipend) > 0) {
2559 atomic_set(&bus->ipend, 0);
Franky Lin45316032012-09-13 21:12:03 +02002560 err = brcmf_sdio_intr_rstatus(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002561 }
2562
Franky Lin45316032012-09-13 21:12:03 +02002563 /* Start with leftover status bits */
2564 intstatus = atomic_xchg(&bus->intstatus, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002565
2566 /* Handle flow-control change: read new state in case our ack
2567 * crossed another change interrupt. If change still set, assume
2568 * FC ON for safety, let next loop through do the debounce.
2569 */
2570 if (intstatus & I_HMB_FC_CHANGE) {
2571 intstatus &= ~I_HMB_FC_CHANGE;
Franky Lin5c15c232012-05-04 18:27:37 -07002572 err = w_sdreg32(bus, I_HMB_FC_CHANGE,
2573 offsetof(struct sdpcmd_regs, intstatus));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002574
Franky Lin5c15c232012-05-04 18:27:37 -07002575 err = r_sdreg32(bus, &newstatus,
2576 offsetof(struct sdpcmd_regs, intstatus));
Arend van Spriel80969832012-06-09 22:51:44 +02002577 bus->sdcnt.f1regdata += 2;
Franky Lin45316032012-09-13 21:12:03 +02002578 atomic_set(&bus->fcstate,
2579 !!(newstatus & (I_HMB_FC_STATE | I_HMB_FC_CHANGE)));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002580 intstatus |= (newstatus & bus->hostintmask);
2581 }
2582
2583 /* Handle host mailbox indication */
2584 if (intstatus & I_HMB_HOST_INT) {
2585 intstatus &= ~I_HMB_HOST_INT;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002586 intstatus |= brcmf_sdio_hostmail(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002587 }
2588
Franky Lin38b0b0d2012-11-05 16:22:24 -08002589 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin7cdf57d2012-11-05 16:22:23 -08002590
Arend van Spriel5b435de2011-10-05 13:19:03 +02002591 /* Generally don't ask for these, can get CRC errors... */
2592 if (intstatus & I_WR_OOSYNC) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002593 brcmf_err("Dongle reports WR_OOSYNC\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002594 intstatus &= ~I_WR_OOSYNC;
2595 }
2596
2597 if (intstatus & I_RD_OOSYNC) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002598 brcmf_err("Dongle reports RD_OOSYNC\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002599 intstatus &= ~I_RD_OOSYNC;
2600 }
2601
2602 if (intstatus & I_SBINT) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002603 brcmf_err("Dongle reports SBINT\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002604 intstatus &= ~I_SBINT;
2605 }
2606
2607 /* Would be active due to wake-wlan in gSPI */
2608 if (intstatus & I_CHIPACTIVE) {
2609 brcmf_dbg(INFO, "Dongle reports CHIPACTIVE\n");
2610 intstatus &= ~I_CHIPACTIVE;
2611 }
2612
2613 /* Ignore frame indications if rxskip is set */
2614 if (bus->rxskip)
2615 intstatus &= ~I_HMB_FRAME_IND;
2616
2617 /* On frame indication, read available frames */
Hante Meulemanb6a8cf22014-02-25 20:30:33 +01002618 if ((intstatus & I_HMB_FRAME_IND) && (bus->clkstate == CLK_AVAIL)) {
2619 brcmf_sdio_readframes(bus, bus->rxbound);
Franky Lin4754fce2012-09-19 22:21:11 +02002620 if (!bus->rxpending)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002621 intstatus &= ~I_HMB_FRAME_IND;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002622 }
2623
2624 /* Keep still-pending events for next scheduling */
Hante Meuleman5cbb9c22014-03-06 10:16:12 +01002625 if (intstatus)
Vineet Guptad3928d02015-07-09 13:43:18 +05302626 atomic_or(intstatus, &bus->intstatus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002627
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002628 brcmf_sdio_clrintr(bus);
Franky Linba89bf12012-04-27 18:56:59 -07002629
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002630 if (bus->ctrl_frame_stat && (bus->clkstate == CLK_AVAIL) &&
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002631 data_ok(bus)) {
2632 sdio_claim_host(bus->sdiodev->func[1]);
Hante Meuleman449e58b2015-03-06 18:40:40 +01002633 if (bus->ctrl_frame_stat) {
2634 err = brcmf_sdio_tx_ctrlframe(bus, bus->ctrl_frame_buf,
2635 bus->ctrl_frame_len);
2636 bus->ctrl_frame_err = err;
Hante Meuleman2c64e162015-03-18 13:25:22 +01002637 wmb();
Hante Meuleman449e58b2015-03-06 18:40:40 +01002638 bus->ctrl_frame_stat = false;
2639 }
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002640 sdio_release_host(bus->sdiodev->func[1]);
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002641 brcmf_sdio_wait_event_wakeup(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002642 }
2643 /* Send queued frames (limit 1 if rx may still be pending) */
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002644 if ((bus->clkstate == CLK_AVAIL) && !atomic_read(&bus->fcstate) &&
2645 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) && txlimit &&
2646 data_ok(bus)) {
Franky Lin4754fce2012-09-19 22:21:11 +02002647 framecnt = bus->rxpending ? min(txlimit, bus->txminmax) :
2648 txlimit;
Hante Meulemanb6a8cf22014-02-25 20:30:33 +01002649 brcmf_sdio_sendfromq(bus, framecnt);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002650 }
2651
Arend van Spriela1ce7a02015-02-06 18:36:42 +01002652 if ((bus->sdiodev->state != BRCMF_SDIOD_DATA) || (err != 0)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002653 brcmf_err("failed backplane access over SDIO, halting operation\n");
Franky Lin45316032012-09-13 21:12:03 +02002654 atomic_set(&bus->intstatus, 0);
Hante Meulemande6878c2015-03-06 18:40:38 +01002655 if (bus->ctrl_frame_stat) {
Hante Meuleman449e58b2015-03-06 18:40:40 +01002656 sdio_claim_host(bus->sdiodev->func[1]);
2657 if (bus->ctrl_frame_stat) {
2658 bus->ctrl_frame_err = -ENODEV;
Hante Meuleman2c64e162015-03-18 13:25:22 +01002659 wmb();
Hante Meuleman449e58b2015-03-06 18:40:40 +01002660 bus->ctrl_frame_stat = false;
2661 brcmf_sdio_wait_event_wakeup(bus);
2662 }
2663 sdio_release_host(bus->sdiodev->func[1]);
Hante Meulemande6878c2015-03-06 18:40:38 +01002664 }
Franky Lin45316032012-09-13 21:12:03 +02002665 } else if (atomic_read(&bus->intstatus) ||
2666 atomic_read(&bus->ipend) > 0 ||
2667 (!atomic_read(&bus->fcstate) &&
2668 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) &&
Hante Meulemanb6a8cf22014-02-25 20:30:33 +01002669 data_ok(bus))) {
Hante Meuleman2c64e162015-03-18 13:25:22 +01002670 bus->dpc_triggered = true;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002671 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002672}
2673
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002674static struct pktq *brcmf_sdio_bus_gettxq(struct device *dev)
Arend van Spriele2432b62013-04-03 12:40:38 +02002675{
2676 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
2677 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
2678 struct brcmf_sdio *bus = sdiodev->bus;
2679
2680 return &bus->txq;
2681}
2682
Hante Meuleman84936622014-10-28 14:56:19 +01002683static bool brcmf_sdio_prec_enq(struct pktq *q, struct sk_buff *pkt, int prec)
2684{
2685 struct sk_buff *p;
2686 int eprec = -1; /* precedence to evict from */
2687
2688 /* Fast case, precedence queue is not full and we are also not
2689 * exceeding total queue length
2690 */
2691 if (!pktq_pfull(q, prec) && !pktq_full(q)) {
2692 brcmu_pktq_penq(q, prec, pkt);
2693 return true;
2694 }
2695
2696 /* Determine precedence from which to evict packet, if any */
2697 if (pktq_pfull(q, prec)) {
2698 eprec = prec;
2699 } else if (pktq_full(q)) {
2700 p = brcmu_pktq_peek_tail(q, &eprec);
2701 if (eprec > prec)
2702 return false;
2703 }
2704
2705 /* Evict if needed */
2706 if (eprec >= 0) {
2707 /* Detect queueing to unconfigured precedence */
2708 if (eprec == prec)
2709 return false; /* refuse newer (incoming) packet */
2710 /* Evict packet according to discard policy */
2711 p = brcmu_pktq_pdeq_tail(q, eprec);
2712 if (p == NULL)
2713 brcmf_err("brcmu_pktq_pdeq_tail() failed\n");
2714 brcmu_pkt_buf_free_skb(p);
2715 }
2716
2717 /* Enqueue */
2718 p = brcmu_pktq_penq(q, prec, pkt);
2719 if (p == NULL)
2720 brcmf_err("brcmu_pktq_penq() failed\n");
2721
2722 return p != NULL;
2723}
2724
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002725static int brcmf_sdio_bus_txdata(struct device *dev, struct sk_buff *pkt)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002726{
2727 int ret = -EBADE;
Arend van Spriel44ff5662014-02-25 20:30:28 +01002728 uint prec;
Franky Linbf347bb2011-11-22 17:21:56 -08002729 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01002730 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Linbf347bb2011-11-22 17:21:56 -08002731 struct brcmf_sdio *bus = sdiodev->bus;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002732
Arend van Spriel44ff5662014-02-25 20:30:28 +01002733 brcmf_dbg(TRACE, "Enter: pkt: data %p len %d\n", pkt->data, pkt->len);
Arend van Spriel5768f312015-06-11 00:12:20 +02002734 if (sdiodev->state != BRCMF_SDIOD_DATA)
2735 return -EIO;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002736
2737 /* Add space for the header */
Franky Lin706478c2013-08-10 12:27:29 +02002738 skb_push(pkt, bus->tx_hdrlen);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002739 /* precondition: IS_ALIGNED((unsigned long)(pkt->data), 2) */
2740
2741 prec = prio2prec((pkt->priority & PRIOMASK));
2742
2743 /* Check for existing queue, current flow-control,
2744 pending event, or pending clock */
2745 brcmf_dbg(TRACE, "deferring pktq len %d\n", pktq_len(&bus->txq));
Arend van Spriel80969832012-06-09 22:51:44 +02002746 bus->sdcnt.fcqueued++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002747
2748 /* Priority based enq */
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002749 spin_lock_bh(&bus->txq_lock);
Arend van Spriel5aa9f0e2014-02-25 20:30:27 +01002750 /* reset bus_flags in packet cb */
2751 *(u16 *)(pkt->cb) = 0;
Hante Meuleman84936622014-10-28 14:56:19 +01002752 if (!brcmf_sdio_prec_enq(&bus->txq, pkt, prec)) {
Franky Lin706478c2013-08-10 12:27:29 +02002753 skb_pull(pkt, bus->tx_hdrlen);
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002754 brcmf_err("out of bus->txq !!!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002755 ret = -ENOSR;
2756 } else {
2757 ret = 0;
2758 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002759
Franky Linc8bf3482011-12-16 18:37:08 -08002760 if (pktq_len(&bus->txq) >= TXHI) {
Hante Meuleman90d03ff2012-09-11 21:18:46 +02002761 bus->txoff = true;
Franky Lin20ec4f52017-03-10 21:17:02 +00002762 brcmf_proto_bcdc_txflowblock(dev, true);
Franky Linc8bf3482011-12-16 18:37:08 -08002763 }
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002764 spin_unlock_bh(&bus->txq_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002765
Joe Perches8ae74652012-01-15 00:38:38 -08002766#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02002767 if (pktq_plen(&bus->txq, prec) > qcount[prec])
2768 qcount[prec] = pktq_plen(&bus->txq, prec);
2769#endif
Franky Linf1e68c22012-09-13 21:12:00 +02002770
Arend van Spriel99824642015-02-06 18:36:45 +01002771 brcmf_sdio_trigger_dpc(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002772 return ret;
2773}
2774
Joe Perches8ae74652012-01-15 00:38:38 -08002775#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02002776#define CONSOLE_LINE_MAX 192
2777
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002778static int brcmf_sdio_readconsole(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002779{
2780 struct brcmf_console *c = &bus->console;
2781 u8 line[CONSOLE_LINE_MAX], ch;
2782 u32 n, idx, addr;
2783 int rv;
2784
2785 /* Don't do anything until FWREADY updates console address */
2786 if (bus->console_addr == 0)
2787 return 0;
2788
2789 /* Read console log struct */
2790 addr = bus->console_addr + offsetof(struct rte_console, log_le);
Arend van Spriela39be272013-12-12 11:58:58 +01002791 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, (u8 *)&c->log_le,
2792 sizeof(c->log_le));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002793 if (rv < 0)
2794 return rv;
2795
2796 /* Allocate console buffer (one time only) */
2797 if (c->buf == NULL) {
2798 c->bufsize = le32_to_cpu(c->log_le.buf_size);
2799 c->buf = kmalloc(c->bufsize, GFP_ATOMIC);
2800 if (c->buf == NULL)
2801 return -ENOMEM;
2802 }
2803
2804 idx = le32_to_cpu(c->log_le.idx);
2805
2806 /* Protect against corrupt value */
2807 if (idx > c->bufsize)
2808 return -EBADE;
2809
2810 /* Skip reading the console buffer if the index pointer
2811 has not moved */
2812 if (idx == c->last)
2813 return 0;
2814
2815 /* Read the console buffer */
2816 addr = le32_to_cpu(c->log_le.buf);
Arend van Spriela39be272013-12-12 11:58:58 +01002817 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, c->buf, c->bufsize);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002818 if (rv < 0)
2819 return rv;
2820
2821 while (c->last != idx) {
2822 for (n = 0; n < CONSOLE_LINE_MAX - 2; n++) {
2823 if (c->last == idx) {
2824 /* This would output a partial line.
2825 * Instead, back up
2826 * the buffer pointer and output this
2827 * line next time around.
2828 */
2829 if (c->last >= n)
2830 c->last -= n;
2831 else
2832 c->last = c->bufsize - n;
2833 goto break2;
2834 }
2835 ch = c->buf[c->last];
2836 c->last = (c->last + 1) % c->bufsize;
2837 if (ch == '\n')
2838 break;
2839 line[n] = ch;
2840 }
2841
2842 if (n > 0) {
2843 if (line[n - 1] == '\r')
2844 n--;
2845 line[n] = 0;
Joe Perches18aad4f2012-01-15 00:38:42 -08002846 pr_debug("CONSOLE: %s\n", line);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002847 }
2848 }
2849break2:
2850
2851 return 0;
2852}
Joe Perches8ae74652012-01-15 00:38:38 -08002853#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02002854
Franky Linfcf094f2011-12-16 18:37:16 -08002855static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002856brcmf_sdio_bus_txctl(struct device *dev, unsigned char *msg, uint msglen)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002857{
Franky Lin47a1ce72011-11-22 17:21:55 -08002858 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01002859 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Lin47a1ce72011-11-22 17:21:55 -08002860 struct brcmf_sdio *bus = sdiodev->bus;
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002861 int ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002862
2863 brcmf_dbg(TRACE, "Enter\n");
Arend van Spriel5768f312015-06-11 00:12:20 +02002864 if (sdiodev->state != BRCMF_SDIOD_DATA)
2865 return -EIO;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002866
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002867 /* Send from dpc */
2868 bus->ctrl_frame_buf = msg;
2869 bus->ctrl_frame_len = msglen;
Hante Meuleman2c64e162015-03-18 13:25:22 +01002870 wmb();
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002871 bus->ctrl_frame_stat = true;
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002872
Arend van Spriel99824642015-02-06 18:36:45 +01002873 brcmf_sdio_trigger_dpc(bus);
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002874 wait_event_interruptible_timeout(bus->ctrl_wait, !bus->ctrl_frame_stat,
Arend van Spriel63ce3d52016-01-05 11:05:48 +01002875 CTL_DONE_TIMEOUT);
Hante Meuleman449e58b2015-03-06 18:40:40 +01002876 ret = 0;
2877 if (bus->ctrl_frame_stat) {
2878 sdio_claim_host(bus->sdiodev->func[1]);
2879 if (bus->ctrl_frame_stat) {
2880 brcmf_dbg(SDIO, "ctrl_frame timeout\n");
2881 bus->ctrl_frame_stat = false;
2882 ret = -ETIMEDOUT;
2883 }
2884 sdio_release_host(bus->sdiodev->func[1]);
2885 }
2886 if (!ret) {
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002887 brcmf_dbg(SDIO, "ctrl_frame complete, err=%d\n",
2888 bus->ctrl_frame_err);
Hante Meuleman2c64e162015-03-18 13:25:22 +01002889 rmb();
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002890 ret = bus->ctrl_frame_err;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002891 }
2892
Arend van Spriel5b435de2011-10-05 13:19:03 +02002893 if (ret)
Arend van Spriel80969832012-06-09 22:51:44 +02002894 bus->sdcnt.tx_ctlerrs++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002895 else
Arend van Spriel80969832012-06-09 22:51:44 +02002896 bus->sdcnt.tx_ctlpkts++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002897
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002898 return ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002899}
2900
Arend van Spriel80969832012-06-09 22:51:44 +02002901#ifdef DEBUG
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002902static int brcmf_sdio_dump_console(struct seq_file *seq, struct brcmf_sdio *bus,
2903 struct sdpcm_shared *sh)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002904{
2905 u32 addr, console_ptr, console_size, console_index;
2906 char *conbuf = NULL;
2907 __le32 sh_val;
2908 int rv;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002909
2910 /* obtain console information from device memory */
2911 addr = sh->console_addr + offsetof(struct rte_console, log_le);
Arend van Spriela39be272013-12-12 11:58:58 +01002912 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
2913 (u8 *)&sh_val, sizeof(u32));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002914 if (rv < 0)
2915 return rv;
2916 console_ptr = le32_to_cpu(sh_val);
2917
2918 addr = sh->console_addr + offsetof(struct rte_console, log_le.buf_size);
Arend van Spriela39be272013-12-12 11:58:58 +01002919 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
2920 (u8 *)&sh_val, sizeof(u32));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002921 if (rv < 0)
2922 return rv;
2923 console_size = le32_to_cpu(sh_val);
2924
2925 addr = sh->console_addr + offsetof(struct rte_console, log_le.idx);
Arend van Spriela39be272013-12-12 11:58:58 +01002926 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
2927 (u8 *)&sh_val, sizeof(u32));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002928 if (rv < 0)
2929 return rv;
2930 console_index = le32_to_cpu(sh_val);
2931
2932 /* allocate buffer for console data */
2933 if (console_size <= CONSOLE_BUFFER_MAX)
2934 conbuf = vzalloc(console_size+1);
2935
2936 if (!conbuf)
2937 return -ENOMEM;
2938
2939 /* obtain the console data from device */
2940 conbuf[console_size] = '\0';
Arend van Spriela39be272013-12-12 11:58:58 +01002941 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, console_ptr, (u8 *)conbuf,
2942 console_size);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002943 if (rv < 0)
2944 goto done;
2945
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002946 rv = seq_write(seq, conbuf + console_index,
2947 console_size - console_index);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002948 if (rv < 0)
2949 goto done;
2950
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002951 if (console_index > 0)
2952 rv = seq_write(seq, conbuf, console_index - 1);
2953
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002954done:
2955 vfree(conbuf);
2956 return rv;
2957}
2958
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002959static int brcmf_sdio_trap_info(struct seq_file *seq, struct brcmf_sdio *bus,
2960 struct sdpcm_shared *sh)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002961{
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002962 int error;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002963 struct brcmf_trap_info tr;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002964
Piotr Haberbaa9e602013-04-05 10:57:42 +02002965 if ((sh->flags & SDPCM_SHARED_TRAP) == 0) {
2966 brcmf_dbg(INFO, "no trap in firmware\n");
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002967 return 0;
Piotr Haberbaa9e602013-04-05 10:57:42 +02002968 }
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002969
Arend van Spriela39be272013-12-12 11:58:58 +01002970 error = brcmf_sdiod_ramrw(bus->sdiodev, false, sh->trap_addr, (u8 *)&tr,
2971 sizeof(struct brcmf_trap_info));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002972 if (error < 0)
2973 return error;
2974
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002975 seq_printf(seq,
2976 "dongle trap info: type 0x%x @ epc 0x%08x\n"
2977 " cpsr 0x%08x spsr 0x%08x sp 0x%08x\n"
2978 " lr 0x%08x pc 0x%08x offset 0x%x\n"
2979 " r0 0x%08x r1 0x%08x r2 0x%08x r3 0x%08x\n"
2980 " r4 0x%08x r5 0x%08x r6 0x%08x r7 0x%08x\n",
2981 le32_to_cpu(tr.type), le32_to_cpu(tr.epc),
2982 le32_to_cpu(tr.cpsr), le32_to_cpu(tr.spsr),
2983 le32_to_cpu(tr.r13), le32_to_cpu(tr.r14),
2984 le32_to_cpu(tr.pc), sh->trap_addr,
2985 le32_to_cpu(tr.r0), le32_to_cpu(tr.r1),
2986 le32_to_cpu(tr.r2), le32_to_cpu(tr.r3),
2987 le32_to_cpu(tr.r4), le32_to_cpu(tr.r5),
2988 le32_to_cpu(tr.r6), le32_to_cpu(tr.r7));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002989
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002990 return 0;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002991}
2992
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002993static int brcmf_sdio_assert_info(struct seq_file *seq, struct brcmf_sdio *bus,
2994 struct sdpcm_shared *sh)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002995{
2996 int error = 0;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002997 char file[80] = "?";
2998 char expr[80] = "<???>";
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002999
3000 if ((sh->flags & SDPCM_SHARED_ASSERT_BUILT) == 0) {
3001 brcmf_dbg(INFO, "firmware not built with -assert\n");
3002 return 0;
3003 } else if ((sh->flags & SDPCM_SHARED_ASSERT) == 0) {
3004 brcmf_dbg(INFO, "no assert in dongle\n");
3005 return 0;
3006 }
3007
Franky Lin38b0b0d2012-11-05 16:22:24 -08003008 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003009 if (sh->assert_file_addr != 0) {
Arend van Spriela39be272013-12-12 11:58:58 +01003010 error = brcmf_sdiod_ramrw(bus->sdiodev, false,
3011 sh->assert_file_addr, (u8 *)file, 80);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003012 if (error < 0)
3013 return error;
3014 }
3015 if (sh->assert_exp_addr != 0) {
Arend van Spriela39be272013-12-12 11:58:58 +01003016 error = brcmf_sdiod_ramrw(bus->sdiodev, false,
3017 sh->assert_exp_addr, (u8 *)expr, 80);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003018 if (error < 0)
3019 return error;
3020 }
Franky Lin38b0b0d2012-11-05 16:22:24 -08003021 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003022
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003023 seq_printf(seq, "dongle assert: %s:%d: assert(%s)\n",
3024 file, sh->assert_line, expr);
3025 return 0;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003026}
3027
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003028static int brcmf_sdio_checkdied(struct brcmf_sdio *bus)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003029{
3030 int error;
3031 struct sdpcm_shared sh;
3032
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003033 error = brcmf_sdio_readshared(bus, &sh);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003034
3035 if (error < 0)
3036 return error;
3037
3038 if ((sh.flags & SDPCM_SHARED_ASSERT_BUILT) == 0)
3039 brcmf_dbg(INFO, "firmware not built with -assert\n");
3040 else if (sh.flags & SDPCM_SHARED_ASSERT)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003041 brcmf_err("assertion in dongle\n");
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003042
3043 if (sh.flags & SDPCM_SHARED_TRAP)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003044 brcmf_err("firmware trap in dongle\n");
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003045
3046 return 0;
3047}
3048
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003049static int brcmf_sdio_died_dump(struct seq_file *seq, struct brcmf_sdio *bus)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003050{
3051 int error = 0;
3052 struct sdpcm_shared sh;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003053
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003054 error = brcmf_sdio_readshared(bus, &sh);
3055 if (error < 0)
3056 goto done;
3057
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003058 error = brcmf_sdio_assert_info(seq, bus, &sh);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003059 if (error < 0)
3060 goto done;
Piotr Haberbaa9e602013-04-05 10:57:42 +02003061
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003062 error = brcmf_sdio_trap_info(seq, bus, &sh);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003063 if (error < 0)
3064 goto done;
3065
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003066 error = brcmf_sdio_dump_console(seq, bus, &sh);
Piotr Haberbaa9e602013-04-05 10:57:42 +02003067
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003068done:
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003069 return error;
3070}
3071
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003072static int brcmf_sdio_forensic_read(struct seq_file *seq, void *data)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003073{
Arend van Spriel82d957e2014-07-12 08:49:36 +02003074 struct brcmf_bus *bus_if = dev_get_drvdata(seq->private);
3075 struct brcmf_sdio *bus = bus_if->bus_priv.sdio->bus;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003076
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003077 return brcmf_sdio_died_dump(seq, bus);
3078}
3079
Arend van Spriel82d957e2014-07-12 08:49:36 +02003080static int brcmf_debugfs_sdio_count_read(struct seq_file *seq, void *data)
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003081{
Arend van Spriel82d957e2014-07-12 08:49:36 +02003082 struct brcmf_bus *bus_if = dev_get_drvdata(seq->private);
3083 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3084 struct brcmf_sdio_count *sdcnt = &sdiodev->bus->sdcnt;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003085
Arend van Spriel82d957e2014-07-12 08:49:36 +02003086 seq_printf(seq,
3087 "intrcount: %u\nlastintrs: %u\n"
3088 "pollcnt: %u\nregfails: %u\n"
3089 "tx_sderrs: %u\nfcqueued: %u\n"
3090 "rxrtx: %u\nrx_toolong: %u\n"
3091 "rxc_errors: %u\nrx_hdrfail: %u\n"
3092 "rx_badhdr: %u\nrx_badseq: %u\n"
3093 "fc_rcvd: %u\nfc_xoff: %u\n"
3094 "fc_xon: %u\nrxglomfail: %u\n"
3095 "rxglomframes: %u\nrxglompkts: %u\n"
3096 "f2rxhdrs: %u\nf2rxdata: %u\n"
3097 "f2txdata: %u\nf1regdata: %u\n"
3098 "tickcnt: %u\ntx_ctlerrs: %lu\n"
3099 "tx_ctlpkts: %lu\nrx_ctlerrs: %lu\n"
3100 "rx_ctlpkts: %lu\nrx_readahead: %lu\n",
3101 sdcnt->intrcount, sdcnt->lastintrs,
3102 sdcnt->pollcnt, sdcnt->regfails,
3103 sdcnt->tx_sderrs, sdcnt->fcqueued,
3104 sdcnt->rxrtx, sdcnt->rx_toolong,
3105 sdcnt->rxc_errors, sdcnt->rx_hdrfail,
3106 sdcnt->rx_badhdr, sdcnt->rx_badseq,
3107 sdcnt->fc_rcvd, sdcnt->fc_xoff,
3108 sdcnt->fc_xon, sdcnt->rxglomfail,
3109 sdcnt->rxglomframes, sdcnt->rxglompkts,
3110 sdcnt->f2rxhdrs, sdcnt->f2rxdata,
3111 sdcnt->f2txdata, sdcnt->f1regdata,
3112 sdcnt->tickcnt, sdcnt->tx_ctlerrs,
3113 sdcnt->tx_ctlpkts, sdcnt->rx_ctlerrs,
3114 sdcnt->rx_ctlpkts, sdcnt->rx_readahead_cnt);
3115
3116 return 0;
3117}
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003118
Arend van Spriel80969832012-06-09 22:51:44 +02003119static void brcmf_sdio_debugfs_create(struct brcmf_sdio *bus)
3120{
3121 struct brcmf_pub *drvr = bus->sdiodev->bus_if->drvr;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003122 struct dentry *dentry = brcmf_debugfs_get_devdir(drvr);
Arend van Spriel80969832012-06-09 22:51:44 +02003123
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003124 if (IS_ERR_OR_NULL(dentry))
3125 return;
3126
Arend van Spriel9d6c1dc2015-08-26 22:15:01 +02003127 bus->console_interval = BRCMF_CONSOLE;
3128
Arend van Spriel82d957e2014-07-12 08:49:36 +02003129 brcmf_debugfs_add_entry(drvr, "forensics", brcmf_sdio_forensic_read);
3130 brcmf_debugfs_add_entry(drvr, "counters",
3131 brcmf_debugfs_sdio_count_read);
Daniel Kim0801e6c2014-01-29 15:32:13 +01003132 debugfs_create_u32("console_interval", 0644, dentry,
3133 &bus->console_interval);
Arend van Spriel80969832012-06-09 22:51:44 +02003134}
3135#else
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003136static int brcmf_sdio_checkdied(struct brcmf_sdio *bus)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003137{
3138 return 0;
3139}
3140
Arend van Spriel80969832012-06-09 22:51:44 +02003141static void brcmf_sdio_debugfs_create(struct brcmf_sdio *bus)
3142{
3143}
3144#endif /* DEBUG */
3145
Franky Linfcf094f2011-12-16 18:37:16 -08003146static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003147brcmf_sdio_bus_rxctl(struct device *dev, unsigned char *msg, uint msglen)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003148{
3149 int timeleft;
3150 uint rxlen = 0;
3151 bool pending;
Franky Lindd43a012012-11-05 16:22:22 -08003152 u8 *buf;
Franky Lin532cdd32011-11-22 17:21:54 -08003153 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01003154 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Lin532cdd32011-11-22 17:21:54 -08003155 struct brcmf_sdio *bus = sdiodev->bus;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003156
3157 brcmf_dbg(TRACE, "Enter\n");
Arend van Spriel5768f312015-06-11 00:12:20 +02003158 if (sdiodev->state != BRCMF_SDIOD_DATA)
3159 return -EIO;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003160
3161 /* Wait until control frame is available */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003162 timeleft = brcmf_sdio_dcmd_resp_wait(bus, &bus->rxlen, &pending);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003163
Franky Lindd43a012012-11-05 16:22:22 -08003164 spin_lock_bh(&bus->rxctl_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003165 rxlen = bus->rxlen;
3166 memcpy(msg, bus->rxctl, min(msglen, rxlen));
Franky Lindd43a012012-11-05 16:22:22 -08003167 bus->rxctl = NULL;
3168 buf = bus->rxctl_orig;
3169 bus->rxctl_orig = NULL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003170 bus->rxlen = 0;
Franky Lindd43a012012-11-05 16:22:22 -08003171 spin_unlock_bh(&bus->rxctl_lock);
3172 vfree(buf);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003173
3174 if (rxlen) {
3175 brcmf_dbg(CTL, "resumed on rxctl frame, got %d expected %d\n",
3176 rxlen, msglen);
3177 } else if (timeleft == 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003178 brcmf_err("resumed on timeout\n");
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003179 brcmf_sdio_checkdied(bus);
Joe Perches23677ce2012-02-09 11:17:23 +00003180 } else if (pending) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003181 brcmf_dbg(CTL, "cancelled\n");
3182 return -ERESTARTSYS;
3183 } else {
3184 brcmf_dbg(CTL, "resumed for unknown reason?\n");
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003185 brcmf_sdio_checkdied(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003186 }
3187
3188 if (rxlen)
Arend van Spriel80969832012-06-09 22:51:44 +02003189 bus->sdcnt.rx_ctlpkts++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003190 else
Arend van Spriel80969832012-06-09 22:51:44 +02003191 bus->sdcnt.rx_ctlerrs++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003192
3193 return rxlen ? (int)rxlen : -ETIMEDOUT;
3194}
3195
Hante Meulemana74d0362014-01-13 22:20:22 +01003196#ifdef DEBUG
3197static bool
3198brcmf_sdio_verifymemory(struct brcmf_sdio_dev *sdiodev, u32 ram_addr,
3199 u8 *ram_data, uint ram_sz)
3200{
3201 char *ram_cmp;
3202 int err;
3203 bool ret = true;
3204 int address;
3205 int offset;
3206 int len;
3207
3208 /* read back and verify */
3209 brcmf_dbg(INFO, "Compare RAM dl & ul at 0x%08x; size=%d\n", ram_addr,
3210 ram_sz);
3211 ram_cmp = kmalloc(MEMBLOCK, GFP_KERNEL);
3212 /* do not proceed while no memory but */
3213 if (!ram_cmp)
3214 return true;
3215
3216 address = ram_addr;
3217 offset = 0;
3218 while (offset < ram_sz) {
3219 len = ((offset + MEMBLOCK) < ram_sz) ? MEMBLOCK :
3220 ram_sz - offset;
3221 err = brcmf_sdiod_ramrw(sdiodev, false, address, ram_cmp, len);
3222 if (err) {
3223 brcmf_err("error %d on reading %d membytes at 0x%08x\n",
3224 err, len, address);
3225 ret = false;
3226 break;
3227 } else if (memcmp(ram_cmp, &ram_data[offset], len)) {
3228 brcmf_err("Downloaded RAM image is corrupted, block offset is %d, len is %d\n",
3229 offset, len);
3230 ret = false;
3231 break;
3232 }
3233 offset += len;
3234 address += len;
3235 }
3236
3237 kfree(ram_cmp);
3238
3239 return ret;
3240}
3241#else /* DEBUG */
3242static bool
3243brcmf_sdio_verifymemory(struct brcmf_sdio_dev *sdiodev, u32 ram_addr,
3244 u8 *ram_data, uint ram_sz)
3245{
3246 return true;
3247}
3248#endif /* DEBUG */
3249
Arend van Spriel33556502014-01-13 22:20:25 +01003250static int brcmf_sdio_download_code_file(struct brcmf_sdio *bus,
3251 const struct firmware *fw)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003252{
Hante Meulemanf2c44fe2013-09-25 13:05:45 +02003253 int err;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003254
Hante Meulemana74d0362014-01-13 22:20:22 +01003255 brcmf_dbg(TRACE, "Enter\n");
3256
Hante Meulemanf9951c12014-01-29 15:32:17 +01003257 err = brcmf_sdiod_ramrw(bus->sdiodev, true, bus->ci->rambase,
3258 (u8 *)fw->data, fw->size);
3259 if (err)
3260 brcmf_err("error %d on writing %d membytes at 0x%08x\n",
3261 err, (int)fw->size, bus->ci->rambase);
3262 else if (!brcmf_sdio_verifymemory(bus->sdiodev, bus->ci->rambase,
3263 (u8 *)fw->data, fw->size))
3264 err = -EIO;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003265
Hante Meulemanf2c44fe2013-09-25 13:05:45 +02003266 return err;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003267}
3268
Arend van Spriel33556502014-01-13 22:20:25 +01003269static int brcmf_sdio_download_nvram(struct brcmf_sdio *bus,
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003270 void *vars, u32 varsz)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003271{
Hante Meulemana74d0362014-01-13 22:20:22 +01003272 int address;
3273 int err;
3274
3275 brcmf_dbg(TRACE, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003276
Hante Meulemana74d0362014-01-13 22:20:22 +01003277 address = bus->ci->ramsize - varsz + bus->ci->rambase;
3278 err = brcmf_sdiod_ramrw(bus->sdiodev, true, address, vars, varsz);
3279 if (err)
3280 brcmf_err("error %d on writing %d nvram bytes at 0x%08x\n",
3281 err, varsz, address);
3282 else if (!brcmf_sdio_verifymemory(bus->sdiodev, address, vars, varsz))
3283 err = -EIO;
3284
Hante Meulemana74d0362014-01-13 22:20:22 +01003285 return err;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003286}
3287
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003288static int brcmf_sdio_download_firmware(struct brcmf_sdio *bus,
3289 const struct firmware *fw,
3290 void *nvram, u32 nvlen)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003291{
Markus Elfring9e129042016-03-18 13:23:24 +11003292 int bcmerror;
Arend van Spriel33556502014-01-13 22:20:25 +01003293 u32 rstvec;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003294
3295 sdio_claim_host(bus->sdiodev->func[1]);
3296 brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003297
Arend van Spriel33556502014-01-13 22:20:25 +01003298 rstvec = get_unaligned_le32(fw->data);
3299 brcmf_dbg(SDIO, "firmware rstvec: %x\n", rstvec);
3300
3301 bcmerror = brcmf_sdio_download_code_file(bus, fw);
3302 release_firmware(fw);
3303 if (bcmerror) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003304 brcmf_err("dongle image file download failed\n");
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003305 brcmf_fw_nvram_free(nvram);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003306 goto err;
3307 }
3308
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003309 bcmerror = brcmf_sdio_download_nvram(bus, nvram, nvlen);
3310 brcmf_fw_nvram_free(nvram);
Arend van Spriel33556502014-01-13 22:20:25 +01003311 if (bcmerror) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003312 brcmf_err("dongle nvram file download failed\n");
Franky Lin3eaa9562013-04-02 21:06:20 +02003313 goto err;
3314 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02003315
3316 /* Take arm out of reset */
Arend van Sprield380ebc2015-03-11 16:11:29 +01003317 if (!brcmf_chip_set_active(bus->ci, rstvec)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003318 brcmf_err("error getting out of ARM core reset\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003319 goto err;
3320 }
3321
Arend van Spriel5b435de2011-10-05 13:19:03 +02003322err:
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003323 brcmf_sdio_clkctl(bus, CLK_SDONLY, false);
3324 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003325 return bcmerror;
3326}
3327
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003328static void brcmf_sdio_sr_init(struct brcmf_sdio *bus)
Piotr Haber4a3da992013-04-11 13:28:46 +02003329{
3330 int err = 0;
3331 u8 val;
3332
3333 brcmf_dbg(TRACE, "Enter\n");
3334
Arend van Spriela39be272013-12-12 11:58:58 +01003335 val = brcmf_sdiod_regrb(bus->sdiodev, SBSDIO_FUNC1_WAKEUPCTRL, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003336 if (err) {
3337 brcmf_err("error reading SBSDIO_FUNC1_WAKEUPCTRL\n");
3338 return;
3339 }
3340
3341 val |= 1 << SBSDIO_FUNC1_WCTRL_HTWAIT_SHIFT;
Arend van Spriela39be272013-12-12 11:58:58 +01003342 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_WAKEUPCTRL, val, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003343 if (err) {
3344 brcmf_err("error writing SBSDIO_FUNC1_WAKEUPCTRL\n");
3345 return;
3346 }
3347
3348 /* Add CMD14 Support */
Arend van Spriela39be272013-12-12 11:58:58 +01003349 brcmf_sdiod_regwb(bus->sdiodev, SDIO_CCCR_BRCM_CARDCAP,
3350 (SDIO_CCCR_BRCM_CARDCAP_CMD14_SUPPORT |
3351 SDIO_CCCR_BRCM_CARDCAP_CMD14_EXT),
3352 &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003353 if (err) {
3354 brcmf_err("error writing SDIO_CCCR_BRCM_CARDCAP\n");
3355 return;
3356 }
3357
Arend van Spriela39be272013-12-12 11:58:58 +01003358 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3359 SBSDIO_FORCE_HT, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003360 if (err) {
3361 brcmf_err("error writing SBSDIO_FUNC1_CHIPCLKCSR\n");
3362 return;
3363 }
3364
3365 /* set flag */
3366 bus->sr_enabled = true;
3367 brcmf_dbg(INFO, "SR enabled\n");
3368}
3369
3370/* enable KSO bit */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003371static int brcmf_sdio_kso_init(struct brcmf_sdio *bus)
Piotr Haber4a3da992013-04-11 13:28:46 +02003372{
3373 u8 val;
3374 int err = 0;
3375
3376 brcmf_dbg(TRACE, "Enter\n");
3377
3378 /* KSO bit added in SDIO core rev 12 */
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003379 if (brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV)->rev < 12)
Piotr Haber4a3da992013-04-11 13:28:46 +02003380 return 0;
3381
Arend van Spriela39be272013-12-12 11:58:58 +01003382 val = brcmf_sdiod_regrb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003383 if (err) {
3384 brcmf_err("error reading SBSDIO_FUNC1_SLEEPCSR\n");
3385 return err;
3386 }
3387
3388 if (!(val & SBSDIO_FUNC1_SLEEPCSR_KSO_MASK)) {
3389 val |= (SBSDIO_FUNC1_SLEEPCSR_KSO_EN <<
3390 SBSDIO_FUNC1_SLEEPCSR_KSO_SHIFT);
Arend van Spriela39be272013-12-12 11:58:58 +01003391 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
3392 val, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003393 if (err) {
3394 brcmf_err("error writing SBSDIO_FUNC1_SLEEPCSR\n");
3395 return err;
3396 }
3397 }
3398
3399 return 0;
3400}
3401
3402
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003403static int brcmf_sdio_bus_preinit(struct device *dev)
Arend van Sprielcf458282013-11-29 11:48:15 +01003404{
3405 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3406 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3407 struct brcmf_sdio *bus = sdiodev->bus;
Franky Lin8da9d2c2013-11-29 12:25:10 +01003408 uint pad_size;
Arend van Sprielcf458282013-11-29 11:48:15 +01003409 u32 value;
Arend van Sprielcf458282013-11-29 11:48:15 +01003410 int err;
3411
Franky Lin8da9d2c2013-11-29 12:25:10 +01003412 /* the commands below use the terms tx and rx from
3413 * a device perspective, ie. bus:txglom affects the
3414 * bus transfers from device to host.
3415 */
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003416 if (brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV)->rev < 12) {
Arend van Sprielcf458282013-11-29 11:48:15 +01003417 /* for sdio core rev < 12, disable txgloming */
3418 value = 0;
3419 err = brcmf_iovar_data_set(dev, "bus:txglom", &value,
3420 sizeof(u32));
3421 } else {
3422 /* otherwise, set txglomalign */
Hante Meulemanaf5b5e62016-02-17 11:27:08 +01003423 value = sdiodev->settings->bus.sdio.sd_sgentry_align;
Arend van Sprielcf458282013-11-29 11:48:15 +01003424 /* SDIO ADMA requires at least 32 bit alignment */
3425 value = max_t(u32, value, 4);
3426 err = brcmf_iovar_data_set(dev, "bus:txglomalign", &value,
3427 sizeof(u32));
3428 }
Franky Lin8da9d2c2013-11-29 12:25:10 +01003429
3430 if (err < 0)
3431 goto done;
3432
3433 bus->tx_hdrlen = SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN;
3434 if (sdiodev->sg_support) {
3435 bus->txglom = false;
3436 value = 1;
3437 pad_size = bus->sdiodev->func[2]->cur_blksize << 1;
Franky Lin8da9d2c2013-11-29 12:25:10 +01003438 err = brcmf_iovar_data_set(bus->sdiodev->dev, "bus:rxglom",
3439 &value, sizeof(u32));
3440 if (err < 0) {
3441 /* bus:rxglom is allowed to fail */
3442 err = 0;
3443 } else {
3444 bus->txglom = true;
3445 bus->tx_hdrlen += SDPCM_HWEXT_LEN;
3446 }
3447 }
3448 brcmf_bus_add_txhdrlen(bus->sdiodev->dev, bus->tx_hdrlen);
3449
3450done:
Arend van Sprielcf458282013-11-29 11:48:15 +01003451 return err;
3452}
3453
Arend van Sprielff4445a2015-10-08 20:33:11 +02003454static size_t brcmf_sdio_bus_get_ramsize(struct device *dev)
3455{
3456 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3457 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3458 struct brcmf_sdio *bus = sdiodev->bus;
3459
3460 return bus->ci->ramsize - bus->ci->srsize;
3461}
3462
3463static int brcmf_sdio_bus_get_memdump(struct device *dev, void *data,
3464 size_t mem_size)
3465{
3466 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3467 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3468 struct brcmf_sdio *bus = sdiodev->bus;
3469 int err;
3470 int address;
3471 int offset;
3472 int len;
3473
3474 brcmf_dbg(INFO, "dump at 0x%08x: size=%zu\n", bus->ci->rambase,
3475 mem_size);
3476
3477 address = bus->ci->rambase;
3478 offset = err = 0;
3479 sdio_claim_host(sdiodev->func[1]);
3480 while (offset < mem_size) {
3481 len = ((offset + MEMBLOCK) < mem_size) ? MEMBLOCK :
3482 mem_size - offset;
3483 err = brcmf_sdiod_ramrw(sdiodev, false, address, data, len);
3484 if (err) {
3485 brcmf_err("error %d on reading %d membytes at 0x%08x\n",
3486 err, len, address);
3487 goto done;
3488 }
3489 data += len;
3490 offset += len;
3491 address += len;
3492 }
3493
3494done:
3495 sdio_release_host(sdiodev->func[1]);
3496 return err;
3497}
3498
Arend van Spriel99824642015-02-06 18:36:45 +01003499void brcmf_sdio_trigger_dpc(struct brcmf_sdio *bus)
3500{
Hante Meuleman2c64e162015-03-18 13:25:22 +01003501 if (!bus->dpc_triggered) {
3502 bus->dpc_triggered = true;
Arend van Spriel99824642015-02-06 18:36:45 +01003503 queue_work(bus->brcmf_wq, &bus->datawork);
3504 }
3505}
3506
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003507void brcmf_sdio_isr(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003508{
Arend van Spriel5b435de2011-10-05 13:19:03 +02003509 brcmf_dbg(TRACE, "Enter\n");
3510
3511 if (!bus) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003512 brcmf_err("bus is null pointer, exiting\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003513 return;
3514 }
3515
Arend van Spriel5b435de2011-10-05 13:19:03 +02003516 /* Count the interrupt call */
Arend van Spriel80969832012-06-09 22:51:44 +02003517 bus->sdcnt.intrcount++;
Franky Lin45316032012-09-13 21:12:03 +02003518 if (in_interrupt())
3519 atomic_set(&bus->ipend, 1);
3520 else
3521 if (brcmf_sdio_intr_rstatus(bus)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003522 brcmf_err("failed backplane access\n");
Franky Lin45316032012-09-13 21:12:03 +02003523 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02003524
Arend van Spriel5b435de2011-10-05 13:19:03 +02003525 /* Disable additional interrupts (is this needed now)? */
3526 if (!bus->intr)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003527 brcmf_err("isr w/o interrupt configured!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003528
Hante Meuleman2c64e162015-03-18 13:25:22 +01003529 bus->dpc_triggered = true;
Franky Linf1e68c22012-09-13 21:12:00 +02003530 queue_work(bus->brcmf_wq, &bus->datawork);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003531}
3532
Hante Meulemanb441ba82015-03-06 18:40:39 +01003533static void brcmf_sdio_bus_watchdog(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003534{
Arend van Spriel5b435de2011-10-05 13:19:03 +02003535 brcmf_dbg(TIMER, "Enter\n");
3536
Arend van Spriel5b435de2011-10-05 13:19:03 +02003537 /* Poll period: check device if appropriate. */
Piotr Haber4a3da992013-04-11 13:28:46 +02003538 if (!bus->sr_enabled &&
3539 bus->poll && (++bus->polltick >= bus->pollrate)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003540 u32 intstatus = 0;
3541
3542 /* Reset poll tick */
3543 bus->polltick = 0;
3544
3545 /* Check device if no interrupts */
Arend van Spriel80969832012-06-09 22:51:44 +02003546 if (!bus->intr ||
3547 (bus->sdcnt.intrcount == bus->sdcnt.lastintrs)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003548
Hante Meuleman2c64e162015-03-18 13:25:22 +01003549 if (!bus->dpc_triggered) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003550 u8 devpend;
Arend van Sprielfccfe932013-06-18 13:29:26 +02003551
Franky Lin38b0b0d2012-11-05 16:22:24 -08003552 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriela39be272013-12-12 11:58:58 +01003553 devpend = brcmf_sdiod_regrb(bus->sdiodev,
3554 SDIO_CCCR_INTx,
3555 NULL);
Franky Lin38b0b0d2012-11-05 16:22:24 -08003556 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel99824642015-02-06 18:36:45 +01003557 intstatus = devpend & (INTR_STATUS_FUNC1 |
3558 INTR_STATUS_FUNC2);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003559 }
3560
3561 /* If there is something, make like the ISR and
3562 schedule the DPC */
3563 if (intstatus) {
Arend van Spriel80969832012-06-09 22:51:44 +02003564 bus->sdcnt.pollcnt++;
Franky Lin1d382272012-09-13 21:11:59 +02003565 atomic_set(&bus->ipend, 1);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003566
Hante Meuleman2c64e162015-03-18 13:25:22 +01003567 bus->dpc_triggered = true;
Franky Linf1e68c22012-09-13 21:12:00 +02003568 queue_work(bus->brcmf_wq, &bus->datawork);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003569 }
3570 }
3571
3572 /* Update interrupt tracking */
Arend van Spriel80969832012-06-09 22:51:44 +02003573 bus->sdcnt.lastintrs = bus->sdcnt.intrcount;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003574 }
Joe Perches8ae74652012-01-15 00:38:38 -08003575#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02003576 /* Poll for console output periodically */
Arend van Spriel9d6c1dc2015-08-26 22:15:01 +02003577 if (bus->sdiodev->state == BRCMF_SDIOD_DATA && BRCMF_FWCON_ON() &&
Franky Lin8d169aa2011-11-22 17:21:52 -08003578 bus->console_interval != 0) {
Arend van Spriel63ce3d52016-01-05 11:05:48 +01003579 bus->console.count += jiffies_to_msecs(BRCMF_WD_POLL);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003580 if (bus->console.count >= bus->console_interval) {
3581 bus->console.count -= bus->console_interval;
Franky Lin38b0b0d2012-11-05 16:22:24 -08003582 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003583 /* Make sure backplane clock is on */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003584 brcmf_sdio_bus_sleep(bus, false, false);
3585 if (brcmf_sdio_readconsole(bus) < 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003586 /* stop on error */
3587 bus->console_interval = 0;
Franky Lin38b0b0d2012-11-05 16:22:24 -08003588 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003589 }
3590 }
Joe Perches8ae74652012-01-15 00:38:38 -08003591#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02003592
3593 /* On idle timeout clear activity flag and/or turn off clock */
Hante Meuleman2c64e162015-03-18 13:25:22 +01003594 if (!bus->dpc_triggered) {
3595 rmb();
3596 if ((!bus->dpc_running) && (bus->idletime > 0) &&
3597 (bus->clkstate == CLK_AVAIL)) {
3598 bus->idlecount++;
3599 if (bus->idlecount > bus->idletime) {
3600 brcmf_dbg(SDIO, "idle\n");
3601 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel4011fc42016-01-05 11:05:47 +01003602 brcmf_sdio_wd_timer(bus, false);
Hante Meuleman2c64e162015-03-18 13:25:22 +01003603 bus->idlecount = 0;
3604 brcmf_sdio_bus_sleep(bus, true, false);
3605 sdio_release_host(bus->sdiodev->func[1]);
3606 }
3607 } else {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003608 bus->idlecount = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003609 }
Hante Meulemanb441ba82015-03-06 18:40:39 +01003610 } else {
3611 bus->idlecount = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003612 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02003613}
3614
Franky Linf1e68c22012-09-13 21:12:00 +02003615static void brcmf_sdio_dataworker(struct work_struct *work)
3616{
3617 struct brcmf_sdio *bus = container_of(work, struct brcmf_sdio,
3618 datawork);
Franky Linf1e68c22012-09-13 21:12:00 +02003619
Hante Meuleman2c64e162015-03-18 13:25:22 +01003620 bus->dpc_running = true;
3621 wmb();
3622 while (ACCESS_ONCE(bus->dpc_triggered)) {
3623 bus->dpc_triggered = false;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003624 brcmf_sdio_dpc(bus);
Hante Meulemanb441ba82015-03-06 18:40:39 +01003625 bus->idlecount = 0;
Franky Linf1e68c22012-09-13 21:12:00 +02003626 }
Hante Meuleman2c64e162015-03-18 13:25:22 +01003627 bus->dpc_running = false;
Arend van Spriel99824642015-02-06 18:36:45 +01003628 if (brcmf_sdiod_freezing(bus->sdiodev)) {
3629 brcmf_sdiod_change_state(bus->sdiodev, BRCMF_SDIOD_DOWN);
3630 brcmf_sdiod_try_freeze(bus->sdiodev);
3631 brcmf_sdiod_change_state(bus->sdiodev, BRCMF_SDIOD_DATA);
3632 }
Franky Linf1e68c22012-09-13 21:12:00 +02003633}
3634
Arend van Spriel65d80d02014-01-29 15:32:14 +01003635static void
3636brcmf_sdio_drivestrengthinit(struct brcmf_sdio_dev *sdiodev,
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003637 struct brcmf_chip *ci, u32 drivestrength)
Arend van Spriel65d80d02014-01-29 15:32:14 +01003638{
3639 const struct sdiod_drive_str *str_tab = NULL;
3640 u32 str_mask;
3641 u32 str_shift;
Arend van Spriel65d80d02014-01-29 15:32:14 +01003642 u32 i;
3643 u32 drivestrength_sel = 0;
3644 u32 cc_data_temp;
3645 u32 addr;
3646
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003647 if (!(ci->cc_caps & CC_CAP_PMU))
Arend van Spriel65d80d02014-01-29 15:32:14 +01003648 return;
3649
3650 switch (SDIOD_DRVSTR_KEY(ci->chip, ci->pmurev)) {
Hante Meuleman5779ae62014-07-12 08:49:34 +02003651 case SDIOD_DRVSTR_KEY(BRCM_CC_4330_CHIP_ID, 12):
Arend van Spriel65d80d02014-01-29 15:32:14 +01003652 str_tab = sdiod_drvstr_tab1_1v8;
3653 str_mask = 0x00003800;
3654 str_shift = 11;
3655 break;
Hante Meuleman5779ae62014-07-12 08:49:34 +02003656 case SDIOD_DRVSTR_KEY(BRCM_CC_4334_CHIP_ID, 17):
Arend van Spriel65d80d02014-01-29 15:32:14 +01003657 str_tab = sdiod_drvstr_tab6_1v8;
3658 str_mask = 0x00001800;
3659 str_shift = 11;
3660 break;
Hante Meuleman5779ae62014-07-12 08:49:34 +02003661 case SDIOD_DRVSTR_KEY(BRCM_CC_43143_CHIP_ID, 17):
Arend van Spriel65d80d02014-01-29 15:32:14 +01003662 /* note: 43143 does not support tristate */
3663 i = ARRAY_SIZE(sdiod_drvstr_tab2_3v3) - 1;
3664 if (drivestrength >= sdiod_drvstr_tab2_3v3[i].strength) {
3665 str_tab = sdiod_drvstr_tab2_3v3;
3666 str_mask = 0x00000007;
3667 str_shift = 0;
3668 } else
3669 brcmf_err("Invalid SDIO Drive strength for chip %s, strength=%d\n",
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003670 ci->name, drivestrength);
Arend van Spriel65d80d02014-01-29 15:32:14 +01003671 break;
Hante Meuleman5779ae62014-07-12 08:49:34 +02003672 case SDIOD_DRVSTR_KEY(BRCM_CC_43362_CHIP_ID, 13):
Arend van Spriel65d80d02014-01-29 15:32:14 +01003673 str_tab = sdiod_drive_strength_tab5_1v8;
3674 str_mask = 0x00003800;
3675 str_shift = 11;
3676 break;
3677 default:
Wright Fengd922dfa2016-06-03 23:31:08 +02003678 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 +01003679 ci->name, ci->chiprev, ci->pmurev);
Arend van Spriel65d80d02014-01-29 15:32:14 +01003680 break;
3681 }
3682
3683 if (str_tab != NULL) {
Rafał Miłeckie2b397f2016-01-26 17:57:04 +01003684 struct brcmf_core *pmu = brcmf_chip_get_pmu(ci);
3685
Arend van Spriel65d80d02014-01-29 15:32:14 +01003686 for (i = 0; str_tab[i].strength != 0; i++) {
3687 if (drivestrength >= str_tab[i].strength) {
3688 drivestrength_sel = str_tab[i].sel;
3689 break;
3690 }
3691 }
Rafał Miłeckie2b397f2016-01-26 17:57:04 +01003692 addr = CORE_CC_REG(pmu->base, chipcontrol_addr);
Arend van Spriel65d80d02014-01-29 15:32:14 +01003693 brcmf_sdiod_regwl(sdiodev, addr, 1, NULL);
3694 cc_data_temp = brcmf_sdiod_regrl(sdiodev, addr, NULL);
3695 cc_data_temp &= ~str_mask;
3696 drivestrength_sel <<= str_shift;
3697 cc_data_temp |= drivestrength_sel;
3698 brcmf_sdiod_regwl(sdiodev, addr, cc_data_temp, NULL);
3699
3700 brcmf_dbg(INFO, "SDIO: %d mA (req=%d mA) drive strength selected, set to 0x%08x\n",
3701 str_tab[i].strength, drivestrength, cc_data_temp);
3702 }
3703}
3704
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003705static int brcmf_sdio_buscoreprep(void *ctx)
Arend van Spriel65d80d02014-01-29 15:32:14 +01003706{
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003707 struct brcmf_sdio_dev *sdiodev = ctx;
Arend van Spriel65d80d02014-01-29 15:32:14 +01003708 int err = 0;
3709 u8 clkval, clkset;
3710
3711 /* Try forcing SDIO core to do ALPAvail request only */
3712 clkset = SBSDIO_FORCE_HW_CLKREQ_OFF | SBSDIO_ALP_AVAIL_REQ;
3713 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, clkset, &err);
3714 if (err) {
3715 brcmf_err("error writing for HT off\n");
3716 return err;
3717 }
3718
3719 /* If register supported, wait for ALPAvail and then force ALP */
3720 /* This may take up to 15 milliseconds */
3721 clkval = brcmf_sdiod_regrb(sdiodev,
3722 SBSDIO_FUNC1_CHIPCLKCSR, NULL);
3723
3724 if ((clkval & ~SBSDIO_AVBITS) != clkset) {
3725 brcmf_err("ChipClkCSR access: wrote 0x%02x read 0x%02x\n",
3726 clkset, clkval);
3727 return -EACCES;
3728 }
3729
3730 SPINWAIT(((clkval = brcmf_sdiod_regrb(sdiodev,
3731 SBSDIO_FUNC1_CHIPCLKCSR, NULL)),
3732 !SBSDIO_ALPAV(clkval)),
3733 PMU_MAX_TRANSITION_DLY);
3734 if (!SBSDIO_ALPAV(clkval)) {
3735 brcmf_err("timeout on ALPAV wait, clkval 0x%02x\n",
3736 clkval);
3737 return -EBUSY;
3738 }
3739
3740 clkset = SBSDIO_FORCE_HW_CLKREQ_OFF | SBSDIO_FORCE_ALP;
3741 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, clkset, &err);
3742 udelay(65);
3743
3744 /* Also, disable the extra SDIO pull-ups */
3745 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_SDIOPULLUP, 0, NULL);
3746
3747 return 0;
3748}
3749
Arend van Sprield380ebc2015-03-11 16:11:29 +01003750static void brcmf_sdio_buscore_activate(void *ctx, struct brcmf_chip *chip,
3751 u32 rstvec)
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003752{
3753 struct brcmf_sdio_dev *sdiodev = ctx;
3754 struct brcmf_core *core;
3755 u32 reg_addr;
3756
3757 /* clear all interrupts */
3758 core = brcmf_chip_get_core(chip, BCMA_CORE_SDIO_DEV);
3759 reg_addr = core->base + offsetof(struct sdpcmd_regs, intstatus);
3760 brcmf_sdiod_regwl(sdiodev, reg_addr, 0xFFFFFFFF, NULL);
3761
3762 if (rstvec)
3763 /* Write reset vector to address 0 */
3764 brcmf_sdiod_ramrw(sdiodev, true, 0, (void *)&rstvec,
3765 sizeof(rstvec));
3766}
3767
3768static u32 brcmf_sdio_buscore_read32(void *ctx, u32 addr)
3769{
3770 struct brcmf_sdio_dev *sdiodev = ctx;
3771 u32 val, rev;
3772
3773 val = brcmf_sdiod_regrl(sdiodev, addr, NULL);
Arend Van Spriel634faf32016-09-05 11:42:12 +01003774 if ((sdiodev->func[0]->device == SDIO_DEVICE_ID_BROADCOM_4335_4339 ||
3775 sdiodev->func[0]->device == SDIO_DEVICE_ID_BROADCOM_4339) &&
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003776 addr == CORE_CC_REG(SI_ENUM_BASE, chipid)) {
3777 rev = (val & CID_REV_MASK) >> CID_REV_SHIFT;
3778 if (rev >= 2) {
3779 val &= ~CID_ID_MASK;
Hante Meuleman5779ae62014-07-12 08:49:34 +02003780 val |= BRCM_CC_4339_CHIP_ID;
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003781 }
3782 }
3783 return val;
3784}
3785
3786static void brcmf_sdio_buscore_write32(void *ctx, u32 addr, u32 val)
3787{
3788 struct brcmf_sdio_dev *sdiodev = ctx;
3789
3790 brcmf_sdiod_regwl(sdiodev, addr, val, NULL);
3791}
3792
3793static const struct brcmf_buscore_ops brcmf_sdio_buscore_ops = {
3794 .prepare = brcmf_sdio_buscoreprep,
Arend van Sprield380ebc2015-03-11 16:11:29 +01003795 .activate = brcmf_sdio_buscore_activate,
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003796 .read32 = brcmf_sdio_buscore_read32,
3797 .write32 = brcmf_sdio_buscore_write32,
3798};
3799
Arend van Spriel5b435de2011-10-05 13:19:03 +02003800static bool
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003801brcmf_sdio_probe_attach(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003802{
Hante Meuleman4d792892016-02-17 11:27:07 +01003803 struct brcmf_sdio_dev *sdiodev;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003804 u8 clkctl = 0;
3805 int err = 0;
3806 int reg_addr;
3807 u32 reg_val;
Hante Meuleman668761a2013-04-12 10:55:55 +02003808 u32 drivestrength;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003809
Hante Meuleman4d792892016-02-17 11:27:07 +01003810 sdiodev = bus->sdiodev;
3811 sdio_claim_host(sdiodev->func[1]);
Franky Lin38b0b0d2012-11-05 16:22:24 -08003812
Joe Perches18aad4f2012-01-15 00:38:42 -08003813 pr_debug("F1 signature read @0x18000000=0x%4x\n",
Hante Meuleman4d792892016-02-17 11:27:07 +01003814 brcmf_sdiod_regrl(sdiodev, SI_ENUM_BASE, NULL));
Arend van Spriel5b435de2011-10-05 13:19:03 +02003815
3816 /*
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003817 * Force PLL off until brcmf_chip_attach()
Arend van Spriel5b435de2011-10-05 13:19:03 +02003818 * programs PLL control regs
3819 */
3820
Hante Meuleman4d792892016-02-17 11:27:07 +01003821 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
Arend van Spriela39be272013-12-12 11:58:58 +01003822 BRCMF_INIT_CLKCTL1, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003823 if (!err)
Hante Meuleman4d792892016-02-17 11:27:07 +01003824 clkctl = brcmf_sdiod_regrb(sdiodev,
Arend van Spriela39be272013-12-12 11:58:58 +01003825 SBSDIO_FUNC1_CHIPCLKCSR, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003826
3827 if (err || ((clkctl & ~SBSDIO_AVBITS) != BRCMF_INIT_CLKCTL1)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003828 brcmf_err("ChipClkCSR access: err %d wrote 0x%02x read 0x%02x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02003829 err, BRCMF_INIT_CLKCTL1, clkctl);
3830 goto fail;
3831 }
3832
Hante Meuleman4d792892016-02-17 11:27:07 +01003833 bus->ci = brcmf_chip_attach(sdiodev, &brcmf_sdio_buscore_ops);
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003834 if (IS_ERR(bus->ci)) {
3835 brcmf_err("brcmf_chip_attach failed!\n");
3836 bus->ci = NULL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003837 goto fail;
3838 }
Hante Meulemanaf5b5e62016-02-17 11:27:08 +01003839 sdiodev->settings = brcmf_get_module_param(sdiodev->dev,
Hante Meuleman4d792892016-02-17 11:27:07 +01003840 BRCMF_BUSTYPE_SDIO,
3841 bus->ci->chip,
3842 bus->ci->chiprev);
Hante Meulemanaf5b5e62016-02-17 11:27:08 +01003843 if (!sdiodev->settings) {
3844 brcmf_err("Failed to get device parameters\n");
3845 goto fail;
3846 }
Hante Meuleman4d792892016-02-17 11:27:07 +01003847 /* platform specific configuration:
3848 * alignments must be at least 4 bytes for ADMA
3849 */
3850 bus->head_align = ALIGNMENT;
3851 bus->sgentry_align = ALIGNMENT;
Hante Meulemanaf5b5e62016-02-17 11:27:08 +01003852 if (sdiodev->settings->bus.sdio.sd_head_align > ALIGNMENT)
3853 bus->head_align = sdiodev->settings->bus.sdio.sd_head_align;
3854 if (sdiodev->settings->bus.sdio.sd_sgentry_align > ALIGNMENT)
3855 bus->sgentry_align =
3856 sdiodev->settings->bus.sdio.sd_sgentry_align;
3857
Hante Meuleman4d792892016-02-17 11:27:07 +01003858 /* allocate scatter-gather table. sg support
3859 * will be disabled upon allocation failure.
3860 */
3861 brcmf_sdiod_sgtable_alloc(sdiodev);
3862
3863#ifdef CONFIG_PM_SLEEP
3864 /* wowl can be supported when KEEP_POWER is true and (WAKE_SDIO_IRQ
3865 * is true or when platform data OOB irq is true).
3866 */
3867 if ((sdio_get_host_pm_caps(sdiodev->func[1]) & MMC_PM_KEEP_POWER) &&
3868 ((sdio_get_host_pm_caps(sdiodev->func[1]) & MMC_PM_WAKE_SDIO_IRQ) ||
Hante Meulemanaf5b5e62016-02-17 11:27:08 +01003869 (sdiodev->settings->bus.sdio.oob_irq_supported)))
Hante Meuleman4d792892016-02-17 11:27:07 +01003870 sdiodev->bus_if->wowl_supported = true;
3871#endif
Arend van Spriel5b435de2011-10-05 13:19:03 +02003872
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003873 if (brcmf_sdio_kso_init(bus)) {
Piotr Haber4a3da992013-04-11 13:28:46 +02003874 brcmf_err("error enabling KSO\n");
3875 goto fail;
3876 }
3877
Hante Meulemanaf5b5e62016-02-17 11:27:08 +01003878 if (sdiodev->settings->bus.sdio.drive_strength)
3879 drivestrength = sdiodev->settings->bus.sdio.drive_strength;
Hante Meuleman668761a2013-04-12 10:55:55 +02003880 else
3881 drivestrength = DEFAULT_SDIO_DRIVE_STRENGTH;
Hante Meuleman4d792892016-02-17 11:27:07 +01003882 brcmf_sdio_drivestrengthinit(sdiodev, bus->ci, drivestrength);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003883
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003884 /* Set card control so an SDIO card reset does a WLAN backplane reset */
Hante Meuleman4d792892016-02-17 11:27:07 +01003885 reg_val = brcmf_sdiod_regrb(sdiodev, SDIO_CCCR_BRCM_CARDCTRL, &err);
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003886 if (err)
3887 goto fail;
3888
3889 reg_val |= SDIO_CCCR_BRCM_CARDCTRL_WLANRESET;
3890
Hante Meuleman4d792892016-02-17 11:27:07 +01003891 brcmf_sdiod_regwb(sdiodev, SDIO_CCCR_BRCM_CARDCTRL, reg_val, &err);
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003892 if (err)
3893 goto fail;
3894
3895 /* set PMUControl so a backplane reset does PMU state reload */
Rafał Miłeckie2b397f2016-01-26 17:57:04 +01003896 reg_addr = CORE_CC_REG(brcmf_chip_get_pmu(bus->ci)->base, pmucontrol);
Hante Meuleman4d792892016-02-17 11:27:07 +01003897 reg_val = brcmf_sdiod_regrl(sdiodev, reg_addr, &err);
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003898 if (err)
3899 goto fail;
3900
3901 reg_val |= (BCMA_CC_PMU_CTL_RES_RELOAD << BCMA_CC_PMU_CTL_RES_SHIFT);
3902
Hante Meuleman4d792892016-02-17 11:27:07 +01003903 brcmf_sdiod_regwl(sdiodev, reg_addr, reg_val, &err);
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003904 if (err)
3905 goto fail;
3906
Hante Meuleman4d792892016-02-17 11:27:07 +01003907 sdio_release_host(sdiodev->func[1]);
Franky Lin38b0b0d2012-11-05 16:22:24 -08003908
Arend van Spriel5b435de2011-10-05 13:19:03 +02003909 brcmu_pktq_init(&bus->txq, (PRIOMASK + 1), TXQLEN);
3910
Arend van Spriel9b2d2f2a82013-11-29 12:25:14 +01003911 /* allocate header buffer */
3912 bus->hdrbuf = kzalloc(MAX_HDR_READ + bus->head_align, GFP_KERNEL);
3913 if (!bus->hdrbuf)
3914 return false;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003915 /* Locate an appropriately-aligned portion of hdrbuf */
3916 bus->rxhdr = (u8 *) roundup((unsigned long)&bus->hdrbuf[0],
Arend van Spriel9b2d2f2a82013-11-29 12:25:14 +01003917 bus->head_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003918
3919 /* Set the poll and/or interrupt flags */
3920 bus->intr = true;
3921 bus->poll = false;
3922 if (bus->poll)
3923 bus->pollrate = 1;
3924
3925 return true;
3926
3927fail:
Hante Meuleman4d792892016-02-17 11:27:07 +01003928 sdio_release_host(sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003929 return false;
3930}
3931
Arend van Spriel5b435de2011-10-05 13:19:03 +02003932static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003933brcmf_sdio_watchdog_thread(void *data)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003934{
Franky Line92eedf2011-11-22 17:21:50 -08003935 struct brcmf_sdio *bus = (struct brcmf_sdio *)data;
Arend van Spriel99824642015-02-06 18:36:45 +01003936 int wait;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003937
3938 allow_signal(SIGTERM);
3939 /* Run until signal received */
Arend van Spriel99824642015-02-06 18:36:45 +01003940 brcmf_sdiod_freezer_count(bus->sdiodev);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003941 while (1) {
3942 if (kthread_should_stop())
3943 break;
Arend van Spriel99824642015-02-06 18:36:45 +01003944 brcmf_sdiod_freezer_uncount(bus->sdiodev);
3945 wait = wait_for_completion_interruptible(&bus->watchdog_wait);
3946 brcmf_sdiod_freezer_count(bus->sdiodev);
3947 brcmf_sdiod_try_freeze(bus->sdiodev);
3948 if (!wait) {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003949 brcmf_sdio_bus_watchdog(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003950 /* Count the tick for reference */
Arend van Spriel80969832012-06-09 22:51:44 +02003951 bus->sdcnt.tickcnt++;
Arend van Spriel58e9df42014-03-20 10:18:01 +01003952 reinit_completion(&bus->watchdog_wait);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003953 } else
3954 break;
3955 }
3956 return 0;
3957}
3958
3959static void
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003960brcmf_sdio_watchdog(unsigned long data)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003961{
Franky Line92eedf2011-11-22 17:21:50 -08003962 struct brcmf_sdio *bus = (struct brcmf_sdio *)data;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003963
3964 if (bus->watchdog_tsk) {
3965 complete(&bus->watchdog_wait);
3966 /* Reschedule the watchdog */
Arend van Spriel4011fc42016-01-05 11:05:47 +01003967 if (bus->wd_active)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003968 mod_timer(&bus->timer,
Arend van Spriel63ce3d52016-01-05 11:05:48 +01003969 jiffies + BRCMF_WD_POLL);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003970 }
3971}
3972
Julia Lawall6866a642015-11-14 17:22:07 +01003973static const struct brcmf_bus_ops brcmf_sdio_bus_ops = {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003974 .stop = brcmf_sdio_bus_stop,
3975 .preinit = brcmf_sdio_bus_preinit,
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003976 .txdata = brcmf_sdio_bus_txdata,
3977 .txctl = brcmf_sdio_bus_txctl,
3978 .rxctl = brcmf_sdio_bus_rxctl,
3979 .gettxq = brcmf_sdio_bus_gettxq,
Arend van Sprielff4445a2015-10-08 20:33:11 +02003980 .wowl_config = brcmf_sdio_wowl_config,
3981 .get_ramsize = brcmf_sdio_bus_get_ramsize,
3982 .get_memdump = brcmf_sdio_bus_get_memdump,
Arend van Sprield9cb2592012-12-05 15:25:54 +01003983};
3984
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003985static void brcmf_sdio_firmware_callback(struct device *dev,
3986 const struct firmware *code,
3987 void *nvram, u32 nvram_len)
3988{
3989 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3990 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3991 struct brcmf_sdio *bus = sdiodev->bus;
3992 int err = 0;
3993 u8 saveclk;
3994
3995 brcmf_dbg(TRACE, "Enter: dev=%s\n", dev_name(dev));
3996
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003997 if (!bus_if->drvr)
3998 return;
3999
Hante Meulemana1cee862015-01-25 20:31:35 +01004000 /* try to download image and nvram to the dongle */
4001 bus->alp_only = true;
4002 err = brcmf_sdio_download_firmware(bus, code, nvram, nvram_len);
4003 if (err)
4004 goto fail;
4005 bus->alp_only = false;
4006
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02004007 /* Start the watchdog timer */
4008 bus->sdcnt.tickcnt = 0;
Arend van Spriel4011fc42016-01-05 11:05:47 +01004009 brcmf_sdio_wd_timer(bus, true);
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02004010
4011 sdio_claim_host(sdiodev->func[1]);
4012
4013 /* Make sure backplane clock is on, needed to generate F2 interrupt */
4014 brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
4015 if (bus->clkstate != CLK_AVAIL)
4016 goto release;
4017
4018 /* Force clocks on backplane to be sure F2 interrupt propagates */
4019 saveclk = brcmf_sdiod_regrb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, &err);
4020 if (!err) {
4021 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
4022 (saveclk | SBSDIO_FORCE_HT), &err);
4023 }
4024 if (err) {
4025 brcmf_err("Failed to force clock for F2: err %d\n", err);
4026 goto release;
4027 }
4028
4029 /* Enable function 2 (frame transfers) */
4030 w_sdreg32(bus, SDPCM_PROT_VERSION << SMB_DATA_VERSION_SHIFT,
4031 offsetof(struct sdpcmd_regs, tosbmailboxdata));
4032 err = sdio_enable_func(sdiodev->func[SDIO_FUNC_2]);
4033
4034
4035 brcmf_dbg(INFO, "enable F2: err=%d\n", err);
4036
4037 /* If F2 successfully enabled, set core and enable interrupts */
4038 if (!err) {
4039 /* Set up the interrupt mask and enable interrupts */
4040 bus->hostintmask = HOSTINTMASK;
4041 w_sdreg32(bus, bus->hostintmask,
4042 offsetof(struct sdpcmd_regs, hostintmask));
4043
4044 brcmf_sdiod_regwb(sdiodev, SBSDIO_WATERMARK, 8, &err);
4045 } else {
4046 /* Disable F2 again */
4047 sdio_disable_func(sdiodev->func[SDIO_FUNC_2]);
4048 goto release;
4049 }
4050
4051 if (brcmf_chip_sr_capable(bus->ci)) {
4052 brcmf_sdio_sr_init(bus);
4053 } else {
4054 /* Restore previous clock setting */
4055 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
4056 saveclk, &err);
4057 }
4058
4059 if (err == 0) {
Arend Van Sprielfd3ed332016-07-15 12:39:13 +02004060 /* Allow full data communication using DPC from now on. */
4061 brcmf_sdiod_change_state(bus->sdiodev, BRCMF_SDIOD_DATA);
4062
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02004063 err = brcmf_sdiod_intr_register(sdiodev);
4064 if (err != 0)
4065 brcmf_err("intr register failed:%d\n", err);
4066 }
4067
4068 /* If we didn't come up, turn off backplane clock */
4069 if (err != 0)
4070 brcmf_sdio_clkctl(bus, CLK_NONE, false);
4071
4072 sdio_release_host(sdiodev->func[1]);
4073
Rafał Miłeckie8cd4752017-01-18 11:48:54 +01004074 err = brcmf_bus_started(dev);
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02004075 if (err != 0) {
4076 brcmf_err("dongle is not responding\n");
4077 goto fail;
4078 }
4079 return;
4080
4081release:
4082 sdio_release_host(sdiodev->func[1]);
4083fail:
4084 brcmf_dbg(TRACE, "failed: dev=%s, err=%d\n", dev_name(dev), err);
4085 device_release_driver(dev);
4086}
4087
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01004088struct brcmf_sdio *brcmf_sdio_probe(struct brcmf_sdio_dev *sdiodev)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004089{
4090 int ret;
Franky Line92eedf2011-11-22 17:21:50 -08004091 struct brcmf_sdio *bus;
Arend van Spriel99824642015-02-06 18:36:45 +01004092 struct workqueue_struct *wq;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004093
Arend van Spriel5b435de2011-10-05 13:19:03 +02004094 brcmf_dbg(TRACE, "Enter\n");
4095
Arend van Spriel5b435de2011-10-05 13:19:03 +02004096 /* Allocate private bus interface state */
Franky Line92eedf2011-11-22 17:21:50 -08004097 bus = kzalloc(sizeof(struct brcmf_sdio), GFP_ATOMIC);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004098 if (!bus)
4099 goto fail;
4100
4101 bus->sdiodev = sdiodev;
4102 sdiodev->bus = bus;
Arend van Sprielb83db862011-10-19 12:51:09 +02004103 skb_queue_head_init(&bus->glom);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004104 bus->txbound = BRCMF_TXBOUND;
4105 bus->rxbound = BRCMF_RXBOUND;
4106 bus->txminmax = BRCMF_TXMINMAX;
Franky Lin6bc52312013-08-10 12:27:28 +02004107 bus->tx_seq = SDPCM_SEQ_WRAP - 1;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004108
Arend van Spriel99824642015-02-06 18:36:45 +01004109 /* single-threaded workqueue */
4110 wq = alloc_ordered_workqueue("brcmf_wq/%s", WQ_MEM_RECLAIM,
4111 dev_name(&sdiodev->func[1]->dev));
4112 if (!wq) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01004113 brcmf_err("insufficient memory to create txworkqueue\n");
Hante Meuleman37ac5782012-11-14 18:46:18 -08004114 goto fail;
4115 }
Arend van Spriel99824642015-02-06 18:36:45 +01004116 brcmf_sdiod_freezer_count(sdiodev);
4117 INIT_WORK(&bus->datawork, brcmf_sdio_dataworker);
4118 bus->brcmf_wq = wq;
Hante Meuleman37ac5782012-11-14 18:46:18 -08004119
Arend van Spriel5b435de2011-10-05 13:19:03 +02004120 /* attempt to attach to the dongle */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01004121 if (!(brcmf_sdio_probe_attach(bus))) {
4122 brcmf_err("brcmf_sdio_probe_attach failed\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02004123 goto fail;
4124 }
4125
Franky Lindd43a012012-11-05 16:22:22 -08004126 spin_lock_init(&bus->rxctl_lock);
Hante Meulemanfed7ec42014-03-15 17:18:18 +01004127 spin_lock_init(&bus->txq_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004128 init_waitqueue_head(&bus->ctrl_wait);
4129 init_waitqueue_head(&bus->dcmd_resp_wait);
4130
4131 /* Set up the watchdog timer */
4132 init_timer(&bus->timer);
4133 bus->timer.data = (unsigned long)bus;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01004134 bus->timer.function = brcmf_sdio_watchdog;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004135
Arend van Spriel5b435de2011-10-05 13:19:03 +02004136 /* Initialize watchdog thread */
4137 init_completion(&bus->watchdog_wait);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01004138 bus->watchdog_tsk = kthread_run(brcmf_sdio_watchdog_thread,
Arend van Spriel99824642015-02-06 18:36:45 +01004139 bus, "brcmf_wdog/%s",
4140 dev_name(&sdiodev->func[1]->dev));
Arend van Spriel5b435de2011-10-05 13:19:03 +02004141 if (IS_ERR(bus->watchdog_tsk)) {
Joe Perches02f77192012-01-15 00:38:44 -08004142 pr_warn("brcmf_watchdog thread failed to start\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02004143 bus->watchdog_tsk = NULL;
4144 }
4145 /* Initialize DPC thread */
Hante Meuleman2c64e162015-03-18 13:25:22 +01004146 bus->dpc_triggered = false;
4147 bus->dpc_running = false;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004148
Franky Lina9ffda82011-12-16 18:37:12 -08004149 /* Assign bus interface call back */
Arend van Sprield9cb2592012-12-05 15:25:54 +01004150 bus->sdiodev->bus_if->dev = bus->sdiodev->dev;
4151 bus->sdiodev->bus_if->ops = &brcmf_sdio_bus_ops;
Arend van Spriel75d907d2013-02-06 18:40:45 +01004152 bus->sdiodev->bus_if->chip = bus->ci->chip;
4153 bus->sdiodev->bus_if->chiprev = bus->ci->chiprev;
Arend van Sprield9cb2592012-12-05 15:25:54 +01004154
Franky Lin706478c2013-08-10 12:27:29 +02004155 /* default sdio bus header length for tx packet */
4156 bus->tx_hdrlen = SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN;
4157
4158 /* Attach to the common layer, reserve hdr space */
Hante Meulemanaf5b5e62016-02-17 11:27:08 +01004159 ret = brcmf_attach(bus->sdiodev->dev, bus->sdiodev->settings);
Franky Lin712ac5b2011-12-16 18:37:09 -08004160 if (ret != 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01004161 brcmf_err("brcmf_attach failed\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02004162 goto fail;
4163 }
4164
Hante Meulemane0045bf2016-01-19 12:39:24 +01004165 /* allocate scatter-gather table. sg support
4166 * will be disabled upon allocation failure.
4167 */
4168 brcmf_sdiod_sgtable_alloc(bus->sdiodev);
4169
Daniel Kim7dd3abc2014-05-27 12:56:26 +02004170 /* Query the F2 block size, set roundup accordingly */
4171 bus->blocksize = bus->sdiodev->func[2]->cur_blksize;
4172 bus->roundup = min(max_roundup, bus->blocksize);
4173
Arend van Spriel5b435de2011-10-05 13:19:03 +02004174 /* Allocate buffers */
Arend van Sprielfad13222014-01-06 12:40:40 +01004175 if (bus->sdiodev->bus_if->maxctl) {
Daniel Kim7dd3abc2014-05-27 12:56:26 +02004176 bus->sdiodev->bus_if->maxctl += bus->roundup;
Arend van Sprielfad13222014-01-06 12:40:40 +01004177 bus->rxblen =
4178 roundup((bus->sdiodev->bus_if->maxctl + SDPCM_HDRLEN),
4179 ALIGNMENT) + bus->head_align;
4180 bus->rxbuf = kmalloc(bus->rxblen, GFP_ATOMIC);
4181 if (!(bus->rxbuf)) {
4182 brcmf_err("rxbuf allocation failed\n");
4183 goto fail;
4184 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02004185 }
4186
Arend van Sprielfad13222014-01-06 12:40:40 +01004187 sdio_claim_host(bus->sdiodev->func[1]);
4188
4189 /* Disable F2 to clear any intermediate frame state on the dongle */
4190 sdio_disable_func(bus->sdiodev->func[SDIO_FUNC_2]);
4191
Arend van Sprielfad13222014-01-06 12:40:40 +01004192 bus->rxflow = false;
4193
4194 /* Done with backplane-dependent accesses, can drop clock... */
4195 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, 0, NULL);
4196
4197 sdio_release_host(bus->sdiodev->func[1]);
4198
4199 /* ...and initialize clock/power states */
4200 bus->clkstate = CLK_SDONLY;
4201 bus->idletime = BRCMF_IDLE_INTERVAL;
4202 bus->idleclock = BRCMF_IDLE_ACTIVE;
4203
Arend van Sprielfad13222014-01-06 12:40:40 +01004204 /* SR state */
Arend van Sprielfad13222014-01-06 12:40:40 +01004205 bus->sr_enabled = false;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004206
Arend van Spriel80969832012-06-09 22:51:44 +02004207 brcmf_sdio_debugfs_create(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004208 brcmf_dbg(INFO, "completed!!\n");
4209
Hante Meuleman46d703a2015-11-25 11:32:46 +01004210 ret = brcmf_fw_map_chip_to_name(bus->ci->chip, bus->ci->chiprev,
4211 brcmf_sdio_fwnames,
4212 ARRAY_SIZE(brcmf_sdio_fwnames),
4213 sdiodev->fw_name, sdiodev->nvram_name);
Daniel Kimc1b20532014-07-12 08:49:37 +02004214 if (ret)
4215 goto fail;
4216
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02004217 ret = brcmf_fw_get_firmwares(sdiodev->dev, BRCMF_FW_REQUEST_NVRAM,
Daniel Kimc1b20532014-07-12 08:49:37 +02004218 sdiodev->fw_name, sdiodev->nvram_name,
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02004219 brcmf_sdio_firmware_callback);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004220 if (ret != 0) {
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02004221 brcmf_err("async firmware request failed: %d\n", ret);
Hante Meuleman1799ddf2012-11-14 18:46:17 -08004222 goto fail;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004223 }
Franky Lin15d45b62011-10-21 16:16:32 +02004224
Arend van Spriel5b435de2011-10-05 13:19:03 +02004225 return bus;
4226
4227fail:
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004228 brcmf_sdio_remove(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004229 return NULL;
4230}
4231
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004232/* Detach and free everything */
4233void brcmf_sdio_remove(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004234{
Arend van Spriel5b435de2011-10-05 13:19:03 +02004235 brcmf_dbg(TRACE, "Enter\n");
4236
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004237 if (bus) {
4238 /* De-register interrupt handler */
4239 brcmf_sdiod_intr_unregister(bus->sdiodev);
4240
Arend van Spriel4faf28b2014-05-27 12:56:18 +02004241 brcmf_detach(bus->sdiodev->dev);
Arend van Sprielbfad4a02014-01-06 12:40:39 +01004242
Hante Meulemane0c180ec2014-01-29 15:32:18 +01004243 cancel_work_sync(&bus->datawork);
4244 if (bus->brcmf_wq)
4245 destroy_workqueue(bus->brcmf_wq);
4246
Arend van Sprielbfad4a02014-01-06 12:40:39 +01004247 if (bus->ci) {
Arend van Spriela1ce7a02015-02-06 18:36:42 +01004248 if (bus->sdiodev->state != BRCMF_SDIOD_NOMEDIUM) {
Arend van Sprielbb350712014-01-13 22:20:29 +01004249 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel4011fc42016-01-05 11:05:47 +01004250 brcmf_sdio_wd_timer(bus, false);
Arend van Sprielbb350712014-01-13 22:20:29 +01004251 brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
4252 /* Leave the device in state where it is
Arend van Sprield380ebc2015-03-11 16:11:29 +01004253 * 'passive'. This is done by resetting all
4254 * necessary cores.
Arend van Sprielbb350712014-01-13 22:20:29 +01004255 */
4256 msleep(20);
Arend van Sprield380ebc2015-03-11 16:11:29 +01004257 brcmf_chip_set_passive(bus->ci);
Arend van Sprielbb350712014-01-13 22:20:29 +01004258 brcmf_sdio_clkctl(bus, CLK_NONE, false);
4259 sdio_release_host(bus->sdiodev->func[1]);
4260 }
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01004261 brcmf_chip_detach(bus->ci);
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004262 }
Hante Meulemanaf5b5e62016-02-17 11:27:08 +01004263 if (bus->sdiodev->settings)
4264 brcmf_release_module_param(bus->sdiodev->settings);
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004265
Arend van Sprielbfad4a02014-01-06 12:40:39 +01004266 kfree(bus->rxbuf);
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004267 kfree(bus->hdrbuf);
4268 kfree(bus);
4269 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02004270
4271 brcmf_dbg(TRACE, "Disconnected\n");
4272}
4273
Arend van Spriel4011fc42016-01-05 11:05:47 +01004274void brcmf_sdio_wd_timer(struct brcmf_sdio *bus, bool active)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004275{
Arend van Spriel5b435de2011-10-05 13:19:03 +02004276 /* Totally stop the timer */
Arend van Spriel4011fc42016-01-05 11:05:47 +01004277 if (!active && bus->wd_active) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02004278 del_timer_sync(&bus->timer);
Arend van Spriel4011fc42016-01-05 11:05:47 +01004279 bus->wd_active = false;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004280 return;
4281 }
4282
Franky Linece960e2011-10-21 16:16:19 +02004283 /* don't start the wd until fw is loaded */
Arend van Spriela1ce7a02015-02-06 18:36:42 +01004284 if (bus->sdiodev->state != BRCMF_SDIOD_DATA)
Franky Linece960e2011-10-21 16:16:19 +02004285 return;
4286
Arend van Spriel4011fc42016-01-05 11:05:47 +01004287 if (active) {
4288 if (!bus->wd_active) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02004289 /* Create timer again when watchdog period is
4290 dynamically changed or in the first instance
4291 */
Arend van Spriel63ce3d52016-01-05 11:05:48 +01004292 bus->timer.expires = jiffies + BRCMF_WD_POLL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004293 add_timer(&bus->timer);
Arend van Spriel4011fc42016-01-05 11:05:47 +01004294 bus->wd_active = true;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004295 } else {
4296 /* Re arm the timer, at last watchdog period */
Arend van Spriel63ce3d52016-01-05 11:05:48 +01004297 mod_timer(&bus->timer, jiffies + BRCMF_WD_POLL);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004298 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02004299 }
4300}
Arend van Spriel99824642015-02-06 18:36:45 +01004301
4302int brcmf_sdio_sleep(struct brcmf_sdio *bus, bool sleep)
4303{
4304 int ret;
4305
4306 sdio_claim_host(bus->sdiodev->func[1]);
4307 ret = brcmf_sdio_bus_sleep(bus, sleep, false);
4308 sdio_release_host(bus->sdiodev->func[1]);
4309
4310 return ret;
4311}
4312