blob: a999f95062c7d023a0c8f7e35b2d8b70cadb1d59 [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
543#define ALIGNMENT 4
544
Franky Lin9d7d6f92012-10-22 10:36:24 -0700545enum brcmf_sdio_frmtype {
546 BRCMF_SDIO_FT_NORMAL,
547 BRCMF_SDIO_FT_SUPER,
548 BRCMF_SDIO_FT_SUB,
549};
550
Arend van Spriel65d80d02014-01-29 15:32:14 +0100551#define SDIOD_DRVSTR_KEY(chip, pmu) (((chip) << 16) | (pmu))
552
553/* SDIO Pad drive strength to select value mappings */
554struct sdiod_drive_str {
555 u8 strength; /* Pad Drive Strength in mA */
556 u8 sel; /* Chip-specific select value */
557};
558
559/* SDIO Drive Strength to sel value table for PMU Rev 11 (1.8V) */
560static const struct sdiod_drive_str sdiod_drvstr_tab1_1v8[] = {
561 {32, 0x6},
562 {26, 0x7},
563 {22, 0x4},
564 {16, 0x5},
565 {12, 0x2},
566 {8, 0x3},
567 {4, 0x0},
568 {0, 0x1}
569};
570
571/* SDIO Drive Strength to sel value table for PMU Rev 13 (1.8v) */
572static const struct sdiod_drive_str sdiod_drive_strength_tab5_1v8[] = {
573 {6, 0x7},
574 {5, 0x6},
575 {4, 0x5},
576 {3, 0x4},
577 {2, 0x2},
578 {1, 0x1},
579 {0, 0x0}
580};
581
582/* SDIO Drive Strength to sel value table for PMU Rev 17 (1.8v) */
583static const struct sdiod_drive_str sdiod_drvstr_tab6_1v8[] = {
584 {3, 0x3},
585 {2, 0x2},
586 {1, 0x1},
587 {0, 0x0} };
588
589/* SDIO Drive Strength to sel value table for 43143 PMU Rev 17 (3.3V) */
590static const struct sdiod_drive_str sdiod_drvstr_tab2_3v3[] = {
591 {16, 0x7},
592 {12, 0x5},
593 {8, 0x3},
594 {4, 0x1}
595};
596
Hante Meuleman46d703a2015-11-25 11:32:46 +0100597BRCMF_FW_NVRAM_DEF(43143, "brcmfmac43143-sdio.bin", "brcmfmac43143-sdio.txt");
598BRCMF_FW_NVRAM_DEF(43241B0, "brcmfmac43241b0-sdio.bin",
599 "brcmfmac43241b0-sdio.txt");
600BRCMF_FW_NVRAM_DEF(43241B4, "brcmfmac43241b4-sdio.bin",
601 "brcmfmac43241b4-sdio.txt");
602BRCMF_FW_NVRAM_DEF(43241B5, "brcmfmac43241b5-sdio.bin",
603 "brcmfmac43241b5-sdio.txt");
604BRCMF_FW_NVRAM_DEF(4329, "brcmfmac4329-sdio.bin", "brcmfmac4329-sdio.txt");
605BRCMF_FW_NVRAM_DEF(4330, "brcmfmac4330-sdio.bin", "brcmfmac4330-sdio.txt");
606BRCMF_FW_NVRAM_DEF(4334, "brcmfmac4334-sdio.bin", "brcmfmac4334-sdio.txt");
607BRCMF_FW_NVRAM_DEF(43340, "brcmfmac43340-sdio.bin", "brcmfmac43340-sdio.txt");
608BRCMF_FW_NVRAM_DEF(4335, "brcmfmac4335-sdio.bin", "brcmfmac4335-sdio.txt");
609BRCMF_FW_NVRAM_DEF(43362, "brcmfmac43362-sdio.bin", "brcmfmac43362-sdio.txt");
610BRCMF_FW_NVRAM_DEF(4339, "brcmfmac4339-sdio.bin", "brcmfmac4339-sdio.txt");
611BRCMF_FW_NVRAM_DEF(43430, "brcmfmac43430-sdio.bin", "brcmfmac43430-sdio.txt");
612BRCMF_FW_NVRAM_DEF(43455, "brcmfmac43455-sdio.bin", "brcmfmac43455-sdio.txt");
613BRCMF_FW_NVRAM_DEF(4354, "brcmfmac4354-sdio.bin", "brcmfmac4354-sdio.txt");
Christian Daudt496aec52016-05-04 17:55:20 -0700614BRCMF_FW_NVRAM_DEF(4356, "brcmfmac4356-sdio.bin", "brcmfmac4356-sdio.txt");
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200615
Hante Meuleman46d703a2015-11-25 11:32:46 +0100616static struct brcmf_firmware_mapping brcmf_sdio_fwnames[] = {
617 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_43143_CHIP_ID, 0xFFFFFFFF, 43143),
618 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_43241_CHIP_ID, 0x0000001F, 43241B0),
619 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_43241_CHIP_ID, 0x00000020, 43241B4),
620 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_43241_CHIP_ID, 0xFFFFFFC0, 43241B5),
621 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_4329_CHIP_ID, 0xFFFFFFFF, 4329),
622 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_4330_CHIP_ID, 0xFFFFFFFF, 4330),
623 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_4334_CHIP_ID, 0xFFFFFFFF, 4334),
624 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_43340_CHIP_ID, 0xFFFFFFFF, 43340),
Arend Van Sprieldc630dc2016-11-23 10:25:21 +0000625 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_43341_CHIP_ID, 0xFFFFFFFF, 43340),
Hante Meuleman46d703a2015-11-25 11:32:46 +0100626 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_4335_CHIP_ID, 0xFFFFFFFF, 4335),
627 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_43362_CHIP_ID, 0xFFFFFFFE, 43362),
628 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_4339_CHIP_ID, 0xFFFFFFFF, 4339),
629 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_43430_CHIP_ID, 0xFFFFFFFF, 43430),
630 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_4345_CHIP_ID, 0xFFFFFFC0, 43455),
Christian Daudt496aec52016-05-04 17:55:20 -0700631 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_4354_CHIP_ID, 0xFFFFFFFF, 4354),
632 BRCMF_FW_NVRAM_ENTRY(BRCM_CC_4356_CHIP_ID, 0xFFFFFFFF, 4356)
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200633};
634
Arend van Spriel5b435de2011-10-05 13:19:03 +0200635static void pkt_align(struct sk_buff *p, int len, int align)
636{
637 uint datalign;
638 datalign = (unsigned long)(p->data);
639 datalign = roundup(datalign, (align)) - datalign;
640 if (datalign)
641 skb_pull(p, datalign);
642 __skb_trim(p, len);
643}
644
645/* To check if there's window offered */
Franky Line92eedf2011-11-22 17:21:50 -0800646static bool data_ok(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200647{
648 return (u8)(bus->tx_max - bus->tx_seq) != 0 &&
649 ((u8)(bus->tx_max - bus->tx_seq) & 0x80) == 0;
650}
651
652/*
653 * Reads a register in the SDIO hardware block. This block occupies a series of
654 * adresses on the 32 bit backplane bus.
655 */
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100656static int r_sdreg32(struct brcmf_sdio *bus, u32 *regvar, u32 offset)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200657{
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100658 struct brcmf_core *core;
Franky Lin79ae3952012-05-04 18:27:34 -0700659 int ret;
Franky Lin58692752012-05-04 18:27:36 -0700660
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100661 core = brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV);
662 *regvar = brcmf_sdiod_regrl(bus->sdiodev, core->base + offset, &ret);
Franky Lin58692752012-05-04 18:27:36 -0700663
664 return ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200665}
666
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100667static int w_sdreg32(struct brcmf_sdio *bus, u32 regval, u32 reg_offset)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200668{
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100669 struct brcmf_core *core;
Franky Line13ce262012-05-04 18:27:35 -0700670 int ret;
Franky Lin58692752012-05-04 18:27:36 -0700671
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100672 core = brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV);
673 brcmf_sdiod_regwl(bus->sdiodev, core->base + reg_offset, regval, &ret);
Franky Lin58692752012-05-04 18:27:36 -0700674
675 return ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200676}
677
Piotr Haber4a3da992013-04-11 13:28:46 +0200678static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100679brcmf_sdio_kso_control(struct brcmf_sdio *bus, bool on)
Piotr Haber4a3da992013-04-11 13:28:46 +0200680{
681 u8 wr_val = 0, rd_val, cmp_val, bmask;
682 int err = 0;
Arend Van Spriel5251b6b2016-09-05 11:42:13 +0100683 int err_cnt = 0;
Piotr Haber4a3da992013-04-11 13:28:46 +0200684 int try_cnt = 0;
685
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100686 brcmf_dbg(TRACE, "Enter: on=%d\n", on);
Piotr Haber4a3da992013-04-11 13:28:46 +0200687
688 wr_val = (on << SBSDIO_FUNC1_SLEEPCSR_KSO_SHIFT);
689 /* 1st KSO write goes to AOS wake up core if device is asleep */
Arend van Spriela39be272013-12-12 11:58:58 +0100690 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
691 wr_val, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +0200692
693 if (on) {
694 /* device WAKEUP through KSO:
695 * write bit 0 & read back until
696 * both bits 0 (kso bit) & 1 (dev on status) are set
697 */
698 cmp_val = SBSDIO_FUNC1_SLEEPCSR_KSO_MASK |
699 SBSDIO_FUNC1_SLEEPCSR_DEVON_MASK;
700 bmask = cmp_val;
701 usleep_range(2000, 3000);
702 } else {
703 /* Put device to sleep, turn off KSO */
704 cmp_val = 0;
705 /* only check for bit0, bit1(dev on status) may not
706 * get cleared right away
707 */
708 bmask = SBSDIO_FUNC1_SLEEPCSR_KSO_MASK;
709 }
710
711 do {
712 /* reliable KSO bit set/clr:
713 * the sdiod sleep write access is synced to PMU 32khz clk
714 * just one write attempt may fail,
715 * read it back until it matches written value
716 */
Arend van Spriela39be272013-12-12 11:58:58 +0100717 rd_val = brcmf_sdiod_regrb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
718 &err);
Arend Van Spriel5251b6b2016-09-05 11:42:13 +0100719 if (!err) {
720 if ((rd_val & bmask) == cmp_val)
721 break;
722 err_cnt = 0;
723 }
724 /* bail out upon subsequent access errors */
725 if (err && (err_cnt++ > BRCMF_SDIO_MAX_ACCESS_ERRORS))
Piotr Haber4a3da992013-04-11 13:28:46 +0200726 break;
Piotr Haber4a3da992013-04-11 13:28:46 +0200727 udelay(KSO_WAIT_US);
Arend van Spriela39be272013-12-12 11:58:58 +0100728 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
729 wr_val, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +0200730 } while (try_cnt++ < MAX_KSO_ATTEMPTS);
731
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100732 if (try_cnt > 2)
733 brcmf_dbg(SDIO, "try_cnt=%d rd_val=0x%x err=%d\n", try_cnt,
734 rd_val, err);
735
736 if (try_cnt > MAX_KSO_ATTEMPTS)
737 brcmf_err("max tries: rd_val=0x%x err=%d\n", rd_val, err);
738
Piotr Haber4a3da992013-04-11 13:28:46 +0200739 return err;
740}
741
Arend van Spriel5b435de2011-10-05 13:19:03 +0200742#define HOSTINTMASK (I_HMB_SW_MASK | I_CHIPACTIVE)
743
Arend van Spriel5b435de2011-10-05 13:19:03 +0200744/* Turn backplane clock on or off */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100745static int brcmf_sdio_htclk(struct brcmf_sdio *bus, bool on, bool pendok)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200746{
747 int err;
748 u8 clkctl, clkreq, devctl;
749 unsigned long timeout;
750
Arend van Sprielc3203372013-04-03 12:40:44 +0200751 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200752
753 clkctl = 0;
754
Piotr Haber4a3da992013-04-11 13:28:46 +0200755 if (bus->sr_enabled) {
756 bus->clkstate = (on ? CLK_AVAIL : CLK_SDONLY);
757 return 0;
758 }
759
Arend van Spriel5b435de2011-10-05 13:19:03 +0200760 if (on) {
761 /* Request HT Avail */
762 clkreq =
763 bus->alp_only ? SBSDIO_ALP_AVAIL_REQ : SBSDIO_HT_AVAIL_REQ;
764
Arend van Spriela39be272013-12-12 11:58:58 +0100765 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
766 clkreq, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200767 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100768 brcmf_err("HT Avail request error: %d\n", err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200769 return -EBADE;
770 }
771
Arend van Spriel5b435de2011-10-05 13:19:03 +0200772 /* Check current status */
Arend van Spriela39be272013-12-12 11:58:58 +0100773 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
774 SBSDIO_FUNC1_CHIPCLKCSR, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200775 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100776 brcmf_err("HT Avail read error: %d\n", err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200777 return -EBADE;
778 }
779
780 /* Go to pending and await interrupt if appropriate */
781 if (!SBSDIO_CLKAV(clkctl, bus->alp_only) && pendok) {
782 /* Allow only clock-available interrupt */
Arend van Spriela39be272013-12-12 11:58:58 +0100783 devctl = brcmf_sdiod_regrb(bus->sdiodev,
784 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200785 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100786 brcmf_err("Devctl error setting CA: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200787 err);
788 return -EBADE;
789 }
790
791 devctl |= SBSDIO_DEVCTL_CA_INT_ONLY;
Arend van Spriela39be272013-12-12 11:58:58 +0100792 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
793 devctl, &err);
Arend van Sprielc3203372013-04-03 12:40:44 +0200794 brcmf_dbg(SDIO, "CLKCTL: set PENDING\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200795 bus->clkstate = CLK_PENDING;
796
797 return 0;
798 } else if (bus->clkstate == CLK_PENDING) {
799 /* Cancel CA-only interrupt filter */
Arend van Spriela39be272013-12-12 11:58:58 +0100800 devctl = brcmf_sdiod_regrb(bus->sdiodev,
801 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200802 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
Arend van Spriela39be272013-12-12 11:58:58 +0100803 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
804 devctl, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200805 }
806
807 /* Otherwise, wait here (polling) for HT Avail */
808 timeout = jiffies +
809 msecs_to_jiffies(PMU_MAX_TRANSITION_DLY/1000);
810 while (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
Arend van Spriela39be272013-12-12 11:58:58 +0100811 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
812 SBSDIO_FUNC1_CHIPCLKCSR,
813 &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200814 if (time_after(jiffies, timeout))
815 break;
816 else
817 usleep_range(5000, 10000);
818 }
819 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100820 brcmf_err("HT Avail request error: %d\n", err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200821 return -EBADE;
822 }
823 if (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100824 brcmf_err("HT Avail timeout (%d): clkctl 0x%02x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200825 PMU_MAX_TRANSITION_DLY, clkctl);
826 return -EBADE;
827 }
828
829 /* Mark clock available */
830 bus->clkstate = CLK_AVAIL;
Arend van Sprielc3203372013-04-03 12:40:44 +0200831 brcmf_dbg(SDIO, "CLKCTL: turned ON\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200832
Joe Perches8ae74652012-01-15 00:38:38 -0800833#if defined(DEBUG)
Joe Perches23677ce2012-02-09 11:17:23 +0000834 if (!bus->alp_only) {
Arend van Spriel5b435de2011-10-05 13:19:03 +0200835 if (SBSDIO_ALPONLY(clkctl))
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100836 brcmf_err("HT Clock should be on\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200837 }
Joe Perches8ae74652012-01-15 00:38:38 -0800838#endif /* defined (DEBUG) */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200839
Arend van Spriel5b435de2011-10-05 13:19:03 +0200840 } else {
841 clkreq = 0;
842
843 if (bus->clkstate == CLK_PENDING) {
844 /* Cancel CA-only interrupt filter */
Arend van Spriela39be272013-12-12 11:58:58 +0100845 devctl = brcmf_sdiod_regrb(bus->sdiodev,
846 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200847 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
Arend van Spriela39be272013-12-12 11:58:58 +0100848 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
849 devctl, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200850 }
851
852 bus->clkstate = CLK_SDONLY;
Arend van Spriela39be272013-12-12 11:58:58 +0100853 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
854 clkreq, &err);
Arend van Sprielc3203372013-04-03 12:40:44 +0200855 brcmf_dbg(SDIO, "CLKCTL: turned OFF\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200856 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100857 brcmf_err("Failed access turning clock off: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200858 err);
859 return -EBADE;
860 }
861 }
862 return 0;
863}
864
865/* Change idle/active SD state */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100866static int brcmf_sdio_sdclk(struct brcmf_sdio *bus, bool on)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200867{
Arend van Sprielc3203372013-04-03 12:40:44 +0200868 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200869
870 if (on)
871 bus->clkstate = CLK_SDONLY;
872 else
873 bus->clkstate = CLK_NONE;
874
875 return 0;
876}
877
878/* Transition SD and backplane clock readiness */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100879static int brcmf_sdio_clkctl(struct brcmf_sdio *bus, uint target, bool pendok)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200880{
Joe Perches8ae74652012-01-15 00:38:38 -0800881#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200882 uint oldstate = bus->clkstate;
Joe Perches8ae74652012-01-15 00:38:38 -0800883#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200884
Arend van Sprielc3203372013-04-03 12:40:44 +0200885 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200886
887 /* Early exit if we're already there */
Hante Meulemanb441ba82015-03-06 18:40:39 +0100888 if (bus->clkstate == target)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200889 return 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200890
891 switch (target) {
892 case CLK_AVAIL:
893 /* Make sure SD clock is available */
894 if (bus->clkstate == CLK_NONE)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100895 brcmf_sdio_sdclk(bus, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200896 /* Now request HT Avail on the backplane */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100897 brcmf_sdio_htclk(bus, true, pendok);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200898 break;
899
900 case CLK_SDONLY:
901 /* Remove HT request, or bring up SD clock */
902 if (bus->clkstate == CLK_NONE)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100903 brcmf_sdio_sdclk(bus, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200904 else if (bus->clkstate == CLK_AVAIL)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100905 brcmf_sdio_htclk(bus, false, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200906 else
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100907 brcmf_err("request for %d -> %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200908 bus->clkstate, target);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200909 break;
910
911 case CLK_NONE:
912 /* Make sure to remove HT request */
913 if (bus->clkstate == CLK_AVAIL)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100914 brcmf_sdio_htclk(bus, false, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200915 /* Now remove the SD clock */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100916 brcmf_sdio_sdclk(bus, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200917 break;
918 }
Joe Perches8ae74652012-01-15 00:38:38 -0800919#ifdef DEBUG
Arend van Sprielc3203372013-04-03 12:40:44 +0200920 brcmf_dbg(SDIO, "%d -> %d\n", oldstate, bus->clkstate);
Joe Perches8ae74652012-01-15 00:38:38 -0800921#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200922
923 return 0;
924}
925
Piotr Haber4a3da992013-04-11 13:28:46 +0200926static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100927brcmf_sdio_bus_sleep(struct brcmf_sdio *bus, bool sleep, bool pendok)
Piotr Haber4a3da992013-04-11 13:28:46 +0200928{
929 int err = 0;
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100930 u8 clkcsr;
Arend van Spriel82030d62014-01-29 15:32:20 +0100931
932 brcmf_dbg(SDIO, "Enter: request %s currently %s\n",
Piotr Haber4a3da992013-04-11 13:28:46 +0200933 (sleep ? "SLEEP" : "WAKE"),
Arend van Spriel99824642015-02-06 18:36:45 +0100934 (bus->sleeping ? "SLEEP" : "WAKE"));
Piotr Haber4a3da992013-04-11 13:28:46 +0200935
936 /* If SR is enabled control bus state with KSO */
937 if (bus->sr_enabled) {
938 /* Done if we're already in the requested state */
Arend van Spriel99824642015-02-06 18:36:45 +0100939 if (sleep == bus->sleeping)
Piotr Haber4a3da992013-04-11 13:28:46 +0200940 goto end;
941
942 /* Going to sleep */
943 if (sleep) {
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100944 clkcsr = brcmf_sdiod_regrb(bus->sdiodev,
945 SBSDIO_FUNC1_CHIPCLKCSR,
946 &err);
947 if ((clkcsr & SBSDIO_CSR_MASK) == 0) {
948 brcmf_dbg(SDIO, "no clock, set ALP\n");
949 brcmf_sdiod_regwb(bus->sdiodev,
950 SBSDIO_FUNC1_CHIPCLKCSR,
951 SBSDIO_ALP_AVAIL_REQ, &err);
952 }
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100953 err = brcmf_sdio_kso_control(bus, false);
Piotr Haber4a3da992013-04-11 13:28:46 +0200954 } else {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100955 err = brcmf_sdio_kso_control(bus, true);
Piotr Haber4a3da992013-04-11 13:28:46 +0200956 }
Arend van Spriel8982cd42015-01-25 20:31:34 +0100957 if (err) {
Piotr Haber4a3da992013-04-11 13:28:46 +0200958 brcmf_err("error while changing bus sleep state %d\n",
959 err);
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100960 goto done;
Piotr Haber4a3da992013-04-11 13:28:46 +0200961 }
962 }
963
964end:
965 /* control clocks */
966 if (sleep) {
967 if (!bus->sr_enabled)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100968 brcmf_sdio_clkctl(bus, CLK_NONE, pendok);
Piotr Haber4a3da992013-04-11 13:28:46 +0200969 } else {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100970 brcmf_sdio_clkctl(bus, CLK_AVAIL, pendok);
Arend van Spriel4011fc42016-01-05 11:05:47 +0100971 brcmf_sdio_wd_timer(bus, true);
Piotr Haber4a3da992013-04-11 13:28:46 +0200972 }
Arend van Spriel99824642015-02-06 18:36:45 +0100973 bus->sleeping = sleep;
Arend van Spriel8982cd42015-01-25 20:31:34 +0100974 brcmf_dbg(SDIO, "new state %s\n",
975 (sleep ? "SLEEP" : "WAKE"));
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100976done:
977 brcmf_dbg(SDIO, "Exit: err=%d\n", err);
Piotr Haber4a3da992013-04-11 13:28:46 +0200978 return err;
979
980}
981
Daniel Kim0801e6c2014-01-29 15:32:13 +0100982#ifdef DEBUG
983static inline bool brcmf_sdio_valid_shared_address(u32 addr)
984{
985 return !(addr == 0 || ((~addr >> 16) & 0xffff) == (addr & 0xffff));
986}
987
988static int brcmf_sdio_readshared(struct brcmf_sdio *bus,
989 struct sdpcm_shared *sh)
990{
Arend van Spriel9819a902015-03-11 16:11:32 +0100991 u32 addr = 0;
Daniel Kim0801e6c2014-01-29 15:32:13 +0100992 int rv;
993 u32 shaddr = 0;
994 struct sdpcm_shared_le sh_le;
995 __le32 addr_le;
996
Arend van Spriel9819a902015-03-11 16:11:32 +0100997 sdio_claim_host(bus->sdiodev->func[1]);
998 brcmf_sdio_bus_sleep(bus, false, false);
Daniel Kim0801e6c2014-01-29 15:32:13 +0100999
1000 /*
1001 * Read last word in socram to determine
1002 * address of sdpcm_shared structure
1003 */
Arend van Spriel9819a902015-03-11 16:11:32 +01001004 shaddr = bus->ci->rambase + bus->ci->ramsize - 4;
1005 if (!bus->ci->rambase && brcmf_chip_sr_capable(bus->ci))
1006 shaddr -= bus->ci->srsize;
1007 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, shaddr,
1008 (u8 *)&addr_le, 4);
Daniel Kim0801e6c2014-01-29 15:32:13 +01001009 if (rv < 0)
Arend van Spriel9819a902015-03-11 16:11:32 +01001010 goto fail;
Daniel Kim0801e6c2014-01-29 15:32:13 +01001011
1012 /*
1013 * Check if addr is valid.
1014 * NVRAM length at the end of memory should have been overwritten.
1015 */
Arend van Spriel9819a902015-03-11 16:11:32 +01001016 addr = le32_to_cpu(addr_le);
Daniel Kim0801e6c2014-01-29 15:32:13 +01001017 if (!brcmf_sdio_valid_shared_address(addr)) {
Arend van Spriel9819a902015-03-11 16:11:32 +01001018 brcmf_err("invalid sdpcm_shared address 0x%08X\n", addr);
1019 rv = -EINVAL;
1020 goto fail;
Daniel Kim0801e6c2014-01-29 15:32:13 +01001021 }
1022
Arend van Spriel9819a902015-03-11 16:11:32 +01001023 brcmf_dbg(INFO, "sdpcm_shared address 0x%08X\n", addr);
1024
Daniel Kim0801e6c2014-01-29 15:32:13 +01001025 /* Read hndrte_shared structure */
1026 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, (u8 *)&sh_le,
1027 sizeof(struct sdpcm_shared_le));
1028 if (rv < 0)
Arend van Spriel9819a902015-03-11 16:11:32 +01001029 goto fail;
1030
1031 sdio_release_host(bus->sdiodev->func[1]);
Daniel Kim0801e6c2014-01-29 15:32:13 +01001032
1033 /* Endianness */
1034 sh->flags = le32_to_cpu(sh_le.flags);
1035 sh->trap_addr = le32_to_cpu(sh_le.trap_addr);
1036 sh->assert_exp_addr = le32_to_cpu(sh_le.assert_exp_addr);
1037 sh->assert_file_addr = le32_to_cpu(sh_le.assert_file_addr);
1038 sh->assert_line = le32_to_cpu(sh_le.assert_line);
1039 sh->console_addr = le32_to_cpu(sh_le.console_addr);
1040 sh->msgtrace_addr = le32_to_cpu(sh_le.msgtrace_addr);
1041
1042 if ((sh->flags & SDPCM_SHARED_VERSION_MASK) > SDPCM_SHARED_VERSION) {
1043 brcmf_err("sdpcm shared version unsupported: dhd %d dongle %d\n",
1044 SDPCM_SHARED_VERSION,
1045 sh->flags & SDPCM_SHARED_VERSION_MASK);
1046 return -EPROTO;
1047 }
Daniel Kim0801e6c2014-01-29 15:32:13 +01001048 return 0;
Arend van Spriel9819a902015-03-11 16:11:32 +01001049
1050fail:
1051 brcmf_err("unable to obtain sdpcm_shared info: rv=%d (addr=0x%x)\n",
1052 rv, addr);
1053 sdio_release_host(bus->sdiodev->func[1]);
1054 return rv;
Daniel Kim0801e6c2014-01-29 15:32:13 +01001055}
1056
1057static void brcmf_sdio_get_console_addr(struct brcmf_sdio *bus)
1058{
1059 struct sdpcm_shared sh;
1060
1061 if (brcmf_sdio_readshared(bus, &sh) == 0)
1062 bus->console_addr = sh.console_addr;
1063}
1064#else
1065static void brcmf_sdio_get_console_addr(struct brcmf_sdio *bus)
1066{
1067}
1068#endif /* DEBUG */
1069
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001070static u32 brcmf_sdio_hostmail(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001071{
1072 u32 intstatus = 0;
1073 u32 hmb_data;
1074 u8 fcbits;
Franky Lin58692752012-05-04 18:27:36 -07001075 int ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001076
Arend van Sprielc3203372013-04-03 12:40:44 +02001077 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001078
1079 /* Read mailbox data and ack that we did so */
Franky Lin58692752012-05-04 18:27:36 -07001080 ret = r_sdreg32(bus, &hmb_data,
1081 offsetof(struct sdpcmd_regs, tohostmailboxdata));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001082
Franky Lin58692752012-05-04 18:27:36 -07001083 if (ret == 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001084 w_sdreg32(bus, SMB_INT_ACK,
Franky Lin58692752012-05-04 18:27:36 -07001085 offsetof(struct sdpcmd_regs, tosbmailbox));
Arend van Spriel80969832012-06-09 22:51:44 +02001086 bus->sdcnt.f1regdata += 2;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001087
1088 /* Dongle recomposed rx frames, accept them again */
1089 if (hmb_data & HMB_DATA_NAKHANDLED) {
Arend van Sprielc3203372013-04-03 12:40:44 +02001090 brcmf_dbg(SDIO, "Dongle reports NAK handled, expect rtx of %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001091 bus->rx_seq);
1092 if (!bus->rxskip)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001093 brcmf_err("unexpected NAKHANDLED!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001094
1095 bus->rxskip = false;
1096 intstatus |= I_HMB_FRAME_IND;
1097 }
1098
1099 /*
1100 * DEVREADY does not occur with gSPI.
1101 */
1102 if (hmb_data & (HMB_DATA_DEVREADY | HMB_DATA_FWREADY)) {
1103 bus->sdpcm_ver =
1104 (hmb_data & HMB_DATA_VERSION_MASK) >>
1105 HMB_DATA_VERSION_SHIFT;
1106 if (bus->sdpcm_ver != SDPCM_PROT_VERSION)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001107 brcmf_err("Version mismatch, dongle reports %d, "
Arend van Spriel5b435de2011-10-05 13:19:03 +02001108 "expecting %d\n",
1109 bus->sdpcm_ver, SDPCM_PROT_VERSION);
1110 else
Arend van Sprielc3203372013-04-03 12:40:44 +02001111 brcmf_dbg(SDIO, "Dongle ready, protocol version %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001112 bus->sdpcm_ver);
Daniel Kim0801e6c2014-01-29 15:32:13 +01001113
1114 /*
1115 * Retrieve console state address now that firmware should have
1116 * updated it.
1117 */
1118 brcmf_sdio_get_console_addr(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001119 }
1120
1121 /*
1122 * Flow Control has been moved into the RX headers and this out of band
1123 * method isn't used any more.
1124 * remaining backward compatible with older dongles.
1125 */
1126 if (hmb_data & HMB_DATA_FC) {
1127 fcbits = (hmb_data & HMB_DATA_FCDATA_MASK) >>
1128 HMB_DATA_FCDATA_SHIFT;
1129
1130 if (fcbits & ~bus->flowcontrol)
Arend van Spriel80969832012-06-09 22:51:44 +02001131 bus->sdcnt.fc_xoff++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001132
1133 if (bus->flowcontrol & ~fcbits)
Arend van Spriel80969832012-06-09 22:51:44 +02001134 bus->sdcnt.fc_xon++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001135
Arend van Spriel80969832012-06-09 22:51:44 +02001136 bus->sdcnt.fc_rcvd++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001137 bus->flowcontrol = fcbits;
1138 }
1139
1140 /* Shouldn't be any others */
1141 if (hmb_data & ~(HMB_DATA_DEVREADY |
1142 HMB_DATA_NAKHANDLED |
1143 HMB_DATA_FC |
1144 HMB_DATA_FWREADY |
1145 HMB_DATA_FCDATA_MASK | HMB_DATA_VERSION_MASK))
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001146 brcmf_err("Unknown mailbox data content: 0x%02x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001147 hmb_data);
1148
1149 return intstatus;
1150}
1151
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001152static void brcmf_sdio_rxfail(struct brcmf_sdio *bus, bool abort, bool rtx)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001153{
1154 uint retries = 0;
1155 u16 lastrbc;
1156 u8 hi, lo;
1157 int err;
1158
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001159 brcmf_err("%sterminate frame%s\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001160 abort ? "abort command, " : "",
1161 rtx ? ", send NAK" : "");
1162
1163 if (abort)
Arend van Spriela39be272013-12-12 11:58:58 +01001164 brcmf_sdiod_abort(bus->sdiodev, SDIO_FUNC_2);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001165
Arend van Spriela39be272013-12-12 11:58:58 +01001166 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL,
1167 SFC_RF_TERM, &err);
Arend van Spriel80969832012-06-09 22:51:44 +02001168 bus->sdcnt.f1regdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001169
1170 /* Wait until the packet has been flushed (device/FIFO stable) */
1171 for (lastrbc = retries = 0xffff; retries > 0; retries--) {
Arend van Spriela39be272013-12-12 11:58:58 +01001172 hi = brcmf_sdiod_regrb(bus->sdiodev,
1173 SBSDIO_FUNC1_RFRAMEBCHI, &err);
1174 lo = brcmf_sdiod_regrb(bus->sdiodev,
1175 SBSDIO_FUNC1_RFRAMEBCLO, &err);
Arend van Spriel80969832012-06-09 22:51:44 +02001176 bus->sdcnt.f1regdata += 2;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001177
1178 if ((hi == 0) && (lo == 0))
1179 break;
1180
1181 if ((hi > (lastrbc >> 8)) && (lo > (lastrbc & 0x00ff))) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001182 brcmf_err("count growing: last 0x%04x now 0x%04x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001183 lastrbc, (hi << 8) + lo);
1184 }
1185 lastrbc = (hi << 8) + lo;
1186 }
1187
1188 if (!retries)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001189 brcmf_err("count never zeroed: last 0x%04x\n", lastrbc);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001190 else
Arend van Sprielc3203372013-04-03 12:40:44 +02001191 brcmf_dbg(SDIO, "flush took %d iterations\n", 0xffff - retries);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001192
1193 if (rtx) {
Arend van Spriel80969832012-06-09 22:51:44 +02001194 bus->sdcnt.rxrtx++;
Franky Lin58692752012-05-04 18:27:36 -07001195 err = w_sdreg32(bus, SMB_NAK,
1196 offsetof(struct sdpcmd_regs, tosbmailbox));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001197
Arend van Spriel80969832012-06-09 22:51:44 +02001198 bus->sdcnt.f1regdata++;
Franky Lin58692752012-05-04 18:27:36 -07001199 if (err == 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001200 bus->rxskip = true;
1201 }
1202
1203 /* Clear partial in any case */
Franky Lin4754fce2012-09-19 22:21:11 +02001204 bus->cur_read.len = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001205}
1206
Hante Meuleman81c78832014-02-25 20:30:35 +01001207static void brcmf_sdio_txfail(struct brcmf_sdio *bus)
1208{
1209 struct brcmf_sdio_dev *sdiodev = bus->sdiodev;
1210 u8 i, hi, lo;
1211
1212 /* On failure, abort the command and terminate the frame */
1213 brcmf_err("sdio error, abort command and terminate frame\n");
1214 bus->sdcnt.tx_sderrs++;
1215
1216 brcmf_sdiod_abort(sdiodev, SDIO_FUNC_2);
1217 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_FRAMECTRL, SFC_WF_TERM, NULL);
1218 bus->sdcnt.f1regdata++;
1219
1220 for (i = 0; i < 3; i++) {
1221 hi = brcmf_sdiod_regrb(sdiodev, SBSDIO_FUNC1_WFRAMEBCHI, NULL);
1222 lo = brcmf_sdiod_regrb(sdiodev, SBSDIO_FUNC1_WFRAMEBCLO, NULL);
1223 bus->sdcnt.f1regdata += 2;
1224 if ((hi == 0) && (lo == 0))
1225 break;
1226 }
1227}
1228
Arend van Spriel9a95e602011-11-10 20:30:29 +01001229/* return total length of buffer chain */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001230static uint brcmf_sdio_glom_len(struct brcmf_sdio *bus)
Arend van Spriel9a95e602011-11-10 20:30:29 +01001231{
1232 struct sk_buff *p;
1233 uint total;
1234
1235 total = 0;
1236 skb_queue_walk(&bus->glom, p)
1237 total += p->len;
1238 return total;
1239}
1240
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001241static void brcmf_sdio_free_glom(struct brcmf_sdio *bus)
Arend van Spriel046808d2011-11-10 20:30:31 +01001242{
1243 struct sk_buff *cur, *next;
1244
1245 skb_queue_walk_safe(&bus->glom, cur, next) {
1246 skb_unlink(cur, &bus->glom);
1247 brcmu_pkt_buf_free_skb(cur);
1248 }
1249}
1250
Franky Lin6bc52312013-08-10 12:27:28 +02001251/**
1252 * brcmfmac sdio bus specific header
1253 * This is the lowest layer header wrapped on the packets transmitted between
1254 * host and WiFi dongle which contains information needed for SDIO core and
1255 * firmware
1256 *
Franky Lin8da9d2c2013-11-29 12:25:10 +01001257 * It consists of 3 parts: hardware header, hardware extension header and
1258 * software header
Franky Lin6bc52312013-08-10 12:27:28 +02001259 * hardware header (frame tag) - 4 bytes
1260 * Byte 0~1: Frame length
1261 * Byte 2~3: Checksum, bit-wise inverse of frame length
Franky Lin8da9d2c2013-11-29 12:25:10 +01001262 * hardware extension header - 8 bytes
1263 * Tx glom mode only, N/A for Rx or normal Tx
1264 * Byte 0~1: Packet length excluding hw frame tag
1265 * Byte 2: Reserved
1266 * Byte 3: Frame flags, bit 0: last frame indication
1267 * Byte 4~5: Reserved
1268 * Byte 6~7: Tail padding length
Franky Lin6bc52312013-08-10 12:27:28 +02001269 * software header - 8 bytes
1270 * Byte 0: Rx/Tx sequence number
1271 * Byte 1: 4 MSB Channel number, 4 LSB arbitrary flag
1272 * Byte 2: Length of next data frame, reserved for Tx
1273 * Byte 3: Data offset
1274 * Byte 4: Flow control bits, reserved for Tx
1275 * Byte 5: Maximum Sequence number allowed by firmware for Tx, N/A for Tx packet
1276 * Byte 6~7: Reserved
1277 */
1278#define SDPCM_HWHDR_LEN 4
Franky Lin8da9d2c2013-11-29 12:25:10 +01001279#define SDPCM_HWEXT_LEN 8
Franky Lin6bc52312013-08-10 12:27:28 +02001280#define SDPCM_SWHDR_LEN 8
1281#define SDPCM_HDRLEN (SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN)
Franky Lin6bc52312013-08-10 12:27:28 +02001282/* software header */
1283#define SDPCM_SEQ_MASK 0x000000ff
1284#define SDPCM_SEQ_WRAP 256
1285#define SDPCM_CHANNEL_MASK 0x00000f00
1286#define SDPCM_CHANNEL_SHIFT 8
1287#define SDPCM_CONTROL_CHANNEL 0 /* Control */
1288#define SDPCM_EVENT_CHANNEL 1 /* Asyc Event Indication */
1289#define SDPCM_DATA_CHANNEL 2 /* Data Xmit/Recv */
1290#define SDPCM_GLOM_CHANNEL 3 /* Coalesced packets */
1291#define SDPCM_TEST_CHANNEL 15 /* Test/debug packets */
1292#define SDPCM_GLOMDESC(p) (((u8 *)p)[1] & 0x80)
1293#define SDPCM_NEXTLEN_MASK 0x00ff0000
1294#define SDPCM_NEXTLEN_SHIFT 16
1295#define SDPCM_DOFFSET_MASK 0xff000000
1296#define SDPCM_DOFFSET_SHIFT 24
1297#define SDPCM_FCMASK_MASK 0x000000ff
1298#define SDPCM_WINDOW_MASK 0x0000ff00
1299#define SDPCM_WINDOW_SHIFT 8
1300
1301static inline u8 brcmf_sdio_getdatoffset(u8 *swheader)
1302{
1303 u32 hdrvalue;
1304 hdrvalue = *(u32 *)swheader;
1305 return (u8)((hdrvalue & SDPCM_DOFFSET_MASK) >> SDPCM_DOFFSET_SHIFT);
1306}
1307
Franky Linc56caa9d2016-04-11 11:35:25 +02001308static inline bool brcmf_sdio_fromevntchan(u8 *swheader)
1309{
1310 u32 hdrvalue;
1311 u8 ret;
1312
1313 hdrvalue = *(u32 *)swheader;
1314 ret = (u8)((hdrvalue & SDPCM_CHANNEL_MASK) >> SDPCM_CHANNEL_SHIFT);
1315
1316 return (ret == SDPCM_EVENT_CHANNEL);
1317}
1318
Franky Lin6bc52312013-08-10 12:27:28 +02001319static int brcmf_sdio_hdparse(struct brcmf_sdio *bus, u8 *header,
1320 struct brcmf_sdio_hdrinfo *rd,
1321 enum brcmf_sdio_frmtype type)
Franky Lin4754fce2012-09-19 22:21:11 +02001322{
1323 u16 len, checksum;
1324 u8 rx_seq, fc, tx_seq_max;
Franky Lin6bc52312013-08-10 12:27:28 +02001325 u32 swheader;
Franky Lin4754fce2012-09-19 22:21:11 +02001326
Arend van Spriel4b776962013-11-29 12:25:06 +01001327 trace_brcmf_sdpcm_hdr(SDPCM_RX, header);
Arend van Spriel76584ec2013-10-15 15:44:47 +02001328
Franky Lin6bc52312013-08-10 12:27:28 +02001329 /* hw header */
Franky Lin4754fce2012-09-19 22:21:11 +02001330 len = get_unaligned_le16(header);
1331 checksum = get_unaligned_le16(header + sizeof(u16));
1332 /* All zero means no more to read */
1333 if (!(len | checksum)) {
1334 bus->rxpending = false;
Franky Lin10510582012-11-05 16:22:26 -08001335 return -ENODATA;
Franky Lin4754fce2012-09-19 22:21:11 +02001336 }
1337 if ((u16)(~(len ^ checksum))) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001338 brcmf_err("HW header checksum error\n");
Franky Lin4754fce2012-09-19 22:21:11 +02001339 bus->sdcnt.rx_badhdr++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001340 brcmf_sdio_rxfail(bus, false, false);
Franky Lin10510582012-11-05 16:22:26 -08001341 return -EIO;
Franky Lin4754fce2012-09-19 22:21:11 +02001342 }
1343 if (len < SDPCM_HDRLEN) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001344 brcmf_err("HW header length error\n");
Franky Lin10510582012-11-05 16:22:26 -08001345 return -EPROTO;
Franky Lin4754fce2012-09-19 22:21:11 +02001346 }
Franky Lin9d7d6f92012-10-22 10:36:24 -07001347 if (type == BRCMF_SDIO_FT_SUPER &&
1348 (roundup(len, bus->blocksize) != rd->len)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001349 brcmf_err("HW superframe header length error\n");
Franky Lin10510582012-11-05 16:22:26 -08001350 return -EPROTO;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001351 }
1352 if (type == BRCMF_SDIO_FT_SUB && len > rd->len) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001353 brcmf_err("HW subframe header length error\n");
Franky Lin10510582012-11-05 16:22:26 -08001354 return -EPROTO;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001355 }
Franky Lin4754fce2012-09-19 22:21:11 +02001356 rd->len = len;
1357
Franky Lin6bc52312013-08-10 12:27:28 +02001358 /* software header */
1359 header += SDPCM_HWHDR_LEN;
1360 swheader = le32_to_cpu(*(__le32 *)header);
1361 if (type == BRCMF_SDIO_FT_SUPER && SDPCM_GLOMDESC(header)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001362 brcmf_err("Glom descriptor found in superframe head\n");
Franky Lin9d7d6f92012-10-22 10:36:24 -07001363 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001364 return -EINVAL;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001365 }
Franky Lin6bc52312013-08-10 12:27:28 +02001366 rx_seq = (u8)(swheader & SDPCM_SEQ_MASK);
1367 rd->channel = (swheader & SDPCM_CHANNEL_MASK) >> SDPCM_CHANNEL_SHIFT;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001368 if (len > MAX_RX_DATASZ && rd->channel != SDPCM_CONTROL_CHANNEL &&
1369 type != BRCMF_SDIO_FT_SUPER) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001370 brcmf_err("HW header length too long\n");
Franky Lin4754fce2012-09-19 22:21:11 +02001371 bus->sdcnt.rx_toolong++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001372 brcmf_sdio_rxfail(bus, false, false);
Franky Lin4754fce2012-09-19 22:21:11 +02001373 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001374 return -EPROTO;
Franky Lin4754fce2012-09-19 22:21:11 +02001375 }
Franky Lin9d7d6f92012-10-22 10:36:24 -07001376 if (type == BRCMF_SDIO_FT_SUPER && rd->channel != SDPCM_GLOM_CHANNEL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001377 brcmf_err("Wrong channel for superframe\n");
Franky Lin9d7d6f92012-10-22 10:36:24 -07001378 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001379 return -EINVAL;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001380 }
1381 if (type == BRCMF_SDIO_FT_SUB && rd->channel != SDPCM_DATA_CHANNEL &&
1382 rd->channel != SDPCM_EVENT_CHANNEL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001383 brcmf_err("Wrong channel for subframe\n");
Franky Lin9d7d6f92012-10-22 10:36:24 -07001384 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001385 return -EINVAL;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001386 }
Franky Lin6bc52312013-08-10 12:27:28 +02001387 rd->dat_offset = brcmf_sdio_getdatoffset(header);
Franky Lin4754fce2012-09-19 22:21:11 +02001388 if (rd->dat_offset < SDPCM_HDRLEN || rd->dat_offset > rd->len) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001389 brcmf_err("seq %d: bad data offset\n", rx_seq);
Franky Lin4754fce2012-09-19 22:21:11 +02001390 bus->sdcnt.rx_badhdr++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001391 brcmf_sdio_rxfail(bus, false, false);
Franky Lin4754fce2012-09-19 22:21:11 +02001392 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001393 return -ENXIO;
Franky Lin4754fce2012-09-19 22:21:11 +02001394 }
1395 if (rd->seq_num != rx_seq) {
Wright Feng98aff6c2016-06-03 23:31:11 +02001396 brcmf_dbg(SDIO, "seq %d, expected %d\n", rx_seq, rd->seq_num);
Franky Lin4754fce2012-09-19 22:21:11 +02001397 bus->sdcnt.rx_badseq++;
1398 rd->seq_num = rx_seq;
1399 }
Franky Lin9d7d6f92012-10-22 10:36:24 -07001400 /* no need to check the reset for subframe */
1401 if (type == BRCMF_SDIO_FT_SUB)
Franky Lin10510582012-11-05 16:22:26 -08001402 return 0;
Franky Lin6bc52312013-08-10 12:27:28 +02001403 rd->len_nxtfrm = (swheader & SDPCM_NEXTLEN_MASK) >> SDPCM_NEXTLEN_SHIFT;
Franky Lin4754fce2012-09-19 22:21:11 +02001404 if (rd->len_nxtfrm << 4 > MAX_RX_DATASZ) {
1405 /* only warm for NON glom packet */
1406 if (rd->channel != SDPCM_GLOM_CHANNEL)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001407 brcmf_err("seq %d: next length error\n", rx_seq);
Franky Lin4754fce2012-09-19 22:21:11 +02001408 rd->len_nxtfrm = 0;
1409 }
Franky Lin6bc52312013-08-10 12:27:28 +02001410 swheader = le32_to_cpu(*(__le32 *)(header + 4));
1411 fc = swheader & SDPCM_FCMASK_MASK;
Franky Lin4754fce2012-09-19 22:21:11 +02001412 if (bus->flowcontrol != fc) {
1413 if (~bus->flowcontrol & fc)
1414 bus->sdcnt.fc_xoff++;
1415 if (bus->flowcontrol & ~fc)
1416 bus->sdcnt.fc_xon++;
1417 bus->sdcnt.fc_rcvd++;
1418 bus->flowcontrol = fc;
1419 }
Franky Lin6bc52312013-08-10 12:27:28 +02001420 tx_seq_max = (swheader & SDPCM_WINDOW_MASK) >> SDPCM_WINDOW_SHIFT;
Franky Lin4754fce2012-09-19 22:21:11 +02001421 if ((u8)(tx_seq_max - bus->tx_seq) > 0x40) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001422 brcmf_err("seq %d: max tx seq number error\n", rx_seq);
Franky Lin4754fce2012-09-19 22:21:11 +02001423 tx_seq_max = bus->tx_seq + 2;
1424 }
1425 bus->tx_max = tx_seq_max;
1426
Franky Lin10510582012-11-05 16:22:26 -08001427 return 0;
Franky Lin4754fce2012-09-19 22:21:11 +02001428}
1429
Franky Lin6bc52312013-08-10 12:27:28 +02001430static inline void brcmf_sdio_update_hwhdr(u8 *header, u16 frm_length)
1431{
1432 *(__le16 *)header = cpu_to_le16(frm_length);
1433 *(((__le16 *)header) + 1) = cpu_to_le16(~frm_length);
1434}
1435
1436static void brcmf_sdio_hdpack(struct brcmf_sdio *bus, u8 *header,
1437 struct brcmf_sdio_hdrinfo *hd_info)
1438{
Franky Lin8da9d2c2013-11-29 12:25:10 +01001439 u32 hdrval;
1440 u8 hdr_offset;
Franky Lin6bc52312013-08-10 12:27:28 +02001441
1442 brcmf_sdio_update_hwhdr(header, hd_info->len);
Franky Lin8da9d2c2013-11-29 12:25:10 +01001443 hdr_offset = SDPCM_HWHDR_LEN;
Franky Lin6bc52312013-08-10 12:27:28 +02001444
Franky Lin8da9d2c2013-11-29 12:25:10 +01001445 if (bus->txglom) {
1446 hdrval = (hd_info->len - hdr_offset) | (hd_info->lastfrm << 24);
1447 *((__le32 *)(header + hdr_offset)) = cpu_to_le32(hdrval);
1448 hdrval = (u16)hd_info->tail_pad << 16;
1449 *(((__le32 *)(header + hdr_offset)) + 1) = cpu_to_le32(hdrval);
1450 hdr_offset += SDPCM_HWEXT_LEN;
1451 }
1452
1453 hdrval = hd_info->seq_num;
1454 hdrval |= (hd_info->channel << SDPCM_CHANNEL_SHIFT) &
1455 SDPCM_CHANNEL_MASK;
1456 hdrval |= (hd_info->dat_offset << SDPCM_DOFFSET_SHIFT) &
1457 SDPCM_DOFFSET_MASK;
1458 *((__le32 *)(header + hdr_offset)) = cpu_to_le32(hdrval);
1459 *(((__le32 *)(header + hdr_offset)) + 1) = 0;
1460 trace_brcmf_sdpcm_hdr(SDPCM_TX + !!(bus->txglom), header);
Franky Lin6bc52312013-08-10 12:27:28 +02001461}
1462
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001463static u8 brcmf_sdio_rxglom(struct brcmf_sdio *bus, u8 rxseq)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001464{
1465 u16 dlen, totlen;
1466 u8 *dptr, num = 0;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001467 u16 sublen;
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001468 struct sk_buff *pfirst, *pnext;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001469
1470 int errcode;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001471 u8 doff, sfdoff;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001472
Franky Lin6bc52312013-08-10 12:27:28 +02001473 struct brcmf_sdio_hdrinfo rd_new;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001474
1475 /* If packets, issue read(s) and send up packet chain */
1476 /* Return sequence numbers consumed? */
1477
Arend van Sprielc3203372013-04-03 12:40:44 +02001478 brcmf_dbg(SDIO, "start: glomd %p glom %p\n",
Arend van Sprielb83db862011-10-19 12:51:09 +02001479 bus->glomd, skb_peek(&bus->glom));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001480
1481 /* If there's a descriptor, generate the packet chain */
1482 if (bus->glomd) {
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001483 pfirst = pnext = NULL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001484 dlen = (u16) (bus->glomd->len);
1485 dptr = bus->glomd->data;
1486 if (!dlen || (dlen & 1)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001487 brcmf_err("bad glomd len(%d), ignore descriptor\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001488 dlen);
1489 dlen = 0;
1490 }
1491
1492 for (totlen = num = 0; dlen; num++) {
1493 /* Get (and move past) next length */
1494 sublen = get_unaligned_le16(dptr);
1495 dlen -= sizeof(u16);
1496 dptr += sizeof(u16);
1497 if ((sublen < SDPCM_HDRLEN) ||
1498 ((num == 0) && (sublen < (2 * SDPCM_HDRLEN)))) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001499 brcmf_err("descriptor len %d bad: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001500 num, sublen);
1501 pnext = NULL;
1502 break;
1503 }
Arend van Spriele217d1c2013-11-29 12:25:13 +01001504 if (sublen % bus->sgentry_align) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001505 brcmf_err("sublen %d not multiple of %d\n",
Arend van Spriele217d1c2013-11-29 12:25:13 +01001506 sublen, bus->sgentry_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001507 }
1508 totlen += sublen;
1509
1510 /* For last frame, adjust read len so total
1511 is a block multiple */
1512 if (!dlen) {
1513 sublen +=
1514 (roundup(totlen, bus->blocksize) - totlen);
1515 totlen = roundup(totlen, bus->blocksize);
1516 }
1517
1518 /* Allocate/chain packet for next subframe */
Arend van Spriele217d1c2013-11-29 12:25:13 +01001519 pnext = brcmu_pkt_buf_get_skb(sublen + bus->sgentry_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001520 if (pnext == NULL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001521 brcmf_err("bcm_pkt_buf_get_skb failed, num %d len %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001522 num, sublen);
1523 break;
1524 }
Arend van Sprielb83db862011-10-19 12:51:09 +02001525 skb_queue_tail(&bus->glom, pnext);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001526
1527 /* Adhere to start alignment requirements */
Arend van Spriele217d1c2013-11-29 12:25:13 +01001528 pkt_align(pnext, sublen, bus->sgentry_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001529 }
1530
1531 /* If all allocations succeeded, save packet chain
1532 in bus structure */
1533 if (pnext) {
1534 brcmf_dbg(GLOM, "allocated %d-byte packet chain for %d subframes\n",
1535 totlen, num);
Franky Lin4754fce2012-09-19 22:21:11 +02001536 if (BRCMF_GLOM_ON() && bus->cur_read.len &&
1537 totlen != bus->cur_read.len) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001538 brcmf_dbg(GLOM, "glomdesc mismatch: nextlen %d glomdesc %d rxseq %d\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001539 bus->cur_read.len, totlen, rxseq);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001540 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02001541 pfirst = pnext = NULL;
1542 } else {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001543 brcmf_sdio_free_glom(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001544 num = 0;
1545 }
1546
1547 /* Done with descriptor packet */
1548 brcmu_pkt_buf_free_skb(bus->glomd);
1549 bus->glomd = NULL;
Franky Lin4754fce2012-09-19 22:21:11 +02001550 bus->cur_read.len = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001551 }
1552
1553 /* Ok -- either we just generated a packet chain,
1554 or had one from before */
Arend van Sprielb83db862011-10-19 12:51:09 +02001555 if (!skb_queue_empty(&bus->glom)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001556 if (BRCMF_GLOM_ON()) {
1557 brcmf_dbg(GLOM, "try superframe read, packet chain:\n");
Arend van Sprielb83db862011-10-19 12:51:09 +02001558 skb_queue_walk(&bus->glom, pnext) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001559 brcmf_dbg(GLOM, " %p: %p len 0x%04x (%d)\n",
1560 pnext, (u8 *) (pnext->data),
1561 pnext->len, pnext->len);
1562 }
1563 }
1564
Arend van Sprielb83db862011-10-19 12:51:09 +02001565 pfirst = skb_peek(&bus->glom);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001566 dlen = (u16) brcmf_sdio_glom_len(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001567
1568 /* Do an SDIO read for the superframe. Configurable iovar to
1569 * read directly into the chained packet, or allocate a large
1570 * packet and and copy into the chain.
1571 */
Franky Lin38b0b0d2012-11-05 16:22:24 -08001572 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriela39be272013-12-12 11:58:58 +01001573 errcode = brcmf_sdiod_recv_chain(bus->sdiodev,
Arend van Spriela39be272013-12-12 11:58:58 +01001574 &bus->glom, dlen);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001575 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel80969832012-06-09 22:51:44 +02001576 bus->sdcnt.f2rxdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001577
Franky Lin64d66c32015-11-25 11:32:40 +01001578 /* On failure, kill the superframe */
Arend van Spriel5b435de2011-10-05 13:19:03 +02001579 if (errcode < 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001580 brcmf_err("glom read of %d bytes failed: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001581 dlen, errcode);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001582
Franky Lin38b0b0d2012-11-05 16:22:24 -08001583 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin64d66c32015-11-25 11:32:40 +01001584 brcmf_sdio_rxfail(bus, true, false);
1585 bus->sdcnt.rxglomfail++;
1586 brcmf_sdio_free_glom(bus);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001587 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001588 return 0;
1589 }
Joe Perches1e023822012-01-15 00:38:40 -08001590
1591 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
1592 pfirst->data, min_t(int, pfirst->len, 48),
1593 "SUPERFRAME:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001594
Franky Lin9d7d6f92012-10-22 10:36:24 -07001595 rd_new.seq_num = rxseq;
1596 rd_new.len = dlen;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001597 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin6bc52312013-08-10 12:27:28 +02001598 errcode = brcmf_sdio_hdparse(bus, pfirst->data, &rd_new,
1599 BRCMF_SDIO_FT_SUPER);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001600 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin9d7d6f92012-10-22 10:36:24 -07001601 bus->cur_read.len = rd_new.len_nxtfrm << 4;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001602
1603 /* Remove superframe header, remember offset */
Franky Lin9d7d6f92012-10-22 10:36:24 -07001604 skb_pull(pfirst, rd_new.dat_offset);
1605 sfdoff = rd_new.dat_offset;
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001606 num = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001607
1608 /* Validate all the subframe headers */
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001609 skb_queue_walk(&bus->glom, pnext) {
1610 /* leave when invalid subframe is found */
1611 if (errcode)
1612 break;
1613
Franky Lin9d7d6f92012-10-22 10:36:24 -07001614 rd_new.len = pnext->len;
1615 rd_new.seq_num = rxseq++;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001616 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin6bc52312013-08-10 12:27:28 +02001617 errcode = brcmf_sdio_hdparse(bus, pnext->data, &rd_new,
1618 BRCMF_SDIO_FT_SUB);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001619 sdio_release_host(bus->sdiodev->func[1]);
Joe Perches1e023822012-01-15 00:38:40 -08001620 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
Franky Lin9d7d6f92012-10-22 10:36:24 -07001621 pnext->data, 32, "subframe:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001622
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001623 num++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001624 }
1625
1626 if (errcode) {
Franky Lin64d66c32015-11-25 11:32:40 +01001627 /* Terminate frame on error */
Franky Lin38b0b0d2012-11-05 16:22:24 -08001628 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin64d66c32015-11-25 11:32:40 +01001629 brcmf_sdio_rxfail(bus, true, false);
1630 bus->sdcnt.rxglomfail++;
1631 brcmf_sdio_free_glom(bus);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001632 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001633 bus->cur_read.len = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001634 return 0;
1635 }
1636
1637 /* Basic SD framing looks ok - process each packet (header) */
Arend van Spriel5b435de2011-10-05 13:19:03 +02001638
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001639 skb_queue_walk_safe(&bus->glom, pfirst, pnext) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001640 dptr = (u8 *) (pfirst->data);
1641 sublen = get_unaligned_le16(dptr);
Franky Lin6bc52312013-08-10 12:27:28 +02001642 doff = brcmf_sdio_getdatoffset(&dptr[SDPCM_HWHDR_LEN]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001643
Joe Perches1e023822012-01-15 00:38:40 -08001644 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_DATA_ON(),
Franky Lin9d7d6f92012-10-22 10:36:24 -07001645 dptr, pfirst->len,
1646 "Rx Subframe Data:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001647
1648 __skb_trim(pfirst, sublen);
1649 skb_pull(pfirst, doff);
1650
1651 if (pfirst->len == 0) {
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001652 skb_unlink(pfirst, &bus->glom);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001653 brcmu_pkt_buf_free_skb(pfirst);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001654 continue;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001655 }
1656
Joe Perches1e023822012-01-15 00:38:40 -08001657 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
1658 pfirst->data,
1659 min_t(int, pfirst->len, 32),
1660 "subframe %d to stack, %p (%p/%d) nxt/lnk %p/%p\n",
1661 bus->glom.qlen, pfirst, pfirst->data,
1662 pfirst->len, pfirst->next,
1663 pfirst->prev);
Arend van Spriel05f38202013-09-25 13:05:46 +02001664 skb_unlink(pfirst, &bus->glom);
Gavin Li8e290ce2017-01-17 15:24:05 -08001665 if (brcmf_sdio_fromevntchan(&dptr[SDPCM_HWHDR_LEN]))
Franky Linc56caa9d2016-04-11 11:35:25 +02001666 brcmf_rx_event(bus->sdiodev->dev, pfirst);
1667 else
1668 brcmf_rx_frame(bus->sdiodev->dev, pfirst,
1669 false);
Arend van Spriel05f38202013-09-25 13:05:46 +02001670 bus->sdcnt.rxglompkts++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001671 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02001672
Arend van Spriel80969832012-06-09 22:51:44 +02001673 bus->sdcnt.rxglomframes++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001674 }
1675 return num;
1676}
1677
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001678static int brcmf_sdio_dcmd_resp_wait(struct brcmf_sdio *bus, uint *condition,
1679 bool *pending)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001680{
1681 DECLARE_WAITQUEUE(wait, current);
Arend van Spriel63ce3d52016-01-05 11:05:48 +01001682 int timeout = DCMD_RESP_TIMEOUT;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001683
1684 /* Wait until control frame is available */
1685 add_wait_queue(&bus->dcmd_resp_wait, &wait);
1686 set_current_state(TASK_INTERRUPTIBLE);
1687
1688 while (!(*condition) && (!signal_pending(current) && timeout))
1689 timeout = schedule_timeout(timeout);
1690
1691 if (signal_pending(current))
1692 *pending = true;
1693
1694 set_current_state(TASK_RUNNING);
1695 remove_wait_queue(&bus->dcmd_resp_wait, &wait);
1696
1697 return timeout;
1698}
1699
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001700static int brcmf_sdio_dcmd_resp_wake(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001701{
Kosuke Tatsukawaa7decc42015-12-10 13:43:08 +01001702 wake_up_interruptible(&bus->dcmd_resp_wait);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001703
1704 return 0;
1705}
1706static void
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001707brcmf_sdio_read_control(struct brcmf_sdio *bus, u8 *hdr, uint len, uint doff)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001708{
1709 uint rdlen, pad;
Franky Lindd43a012012-11-05 16:22:22 -08001710 u8 *buf = NULL, *rbuf;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001711 int sdret;
1712
1713 brcmf_dbg(TRACE, "Enter\n");
1714
Franky Lindd43a012012-11-05 16:22:22 -08001715 if (bus->rxblen)
1716 buf = vzalloc(bus->rxblen);
Joe Perches14f8dc42013-02-07 11:46:27 +00001717 if (!buf)
Franky Lindd43a012012-11-05 16:22:22 -08001718 goto done;
Joe Perches14f8dc42013-02-07 11:46:27 +00001719
Franky Lindd43a012012-11-05 16:22:22 -08001720 rbuf = bus->rxbuf;
Arend van Spriel9b2d2f2a82013-11-29 12:25:14 +01001721 pad = ((unsigned long)rbuf % bus->head_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001722 if (pad)
Arend van Spriel9b2d2f2a82013-11-29 12:25:14 +01001723 rbuf += (bus->head_align - pad);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001724
1725 /* Copy the already-read portion over */
Franky Lindd43a012012-11-05 16:22:22 -08001726 memcpy(buf, hdr, BRCMF_FIRSTREAD);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001727 if (len <= BRCMF_FIRSTREAD)
1728 goto gotpkt;
1729
1730 /* Raise rdlen to next SDIO block to avoid tail command */
1731 rdlen = len - BRCMF_FIRSTREAD;
1732 if (bus->roundup && bus->blocksize && (rdlen > bus->blocksize)) {
1733 pad = bus->blocksize - (rdlen % bus->blocksize);
1734 if ((pad <= bus->roundup) && (pad < bus->blocksize) &&
Franky Linb01a6b32011-12-16 18:37:03 -08001735 ((len + pad) < bus->sdiodev->bus_if->maxctl))
Arend van Spriel5b435de2011-10-05 13:19:03 +02001736 rdlen += pad;
Arend van Spriel9b2d2f2a82013-11-29 12:25:14 +01001737 } else if (rdlen % bus->head_align) {
1738 rdlen += bus->head_align - (rdlen % bus->head_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001739 }
1740
Arend van Spriel5b435de2011-10-05 13:19:03 +02001741 /* Drop if the read is too big or it exceeds our maximum */
Franky Linb01a6b32011-12-16 18:37:03 -08001742 if ((rdlen + BRCMF_FIRSTREAD) > bus->sdiodev->bus_if->maxctl) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001743 brcmf_err("%d-byte control read exceeds %d-byte buffer\n",
Franky Linb01a6b32011-12-16 18:37:03 -08001744 rdlen, bus->sdiodev->bus_if->maxctl);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001745 brcmf_sdio_rxfail(bus, false, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001746 goto done;
1747 }
1748
Franky Linb01a6b32011-12-16 18:37:03 -08001749 if ((len - doff) > bus->sdiodev->bus_if->maxctl) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001750 brcmf_err("%d-byte ctl frame (%d-byte ctl data) exceeds %d-byte limit\n",
Franky Linb01a6b32011-12-16 18:37:03 -08001751 len, len - doff, bus->sdiodev->bus_if->maxctl);
Arend van Spriel80969832012-06-09 22:51:44 +02001752 bus->sdcnt.rx_toolong++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001753 brcmf_sdio_rxfail(bus, false, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001754 goto done;
1755 }
1756
Franky Lindd43a012012-11-05 16:22:22 -08001757 /* Read remain of frame body */
Arend van Spriela7cdd822013-12-12 11:58:59 +01001758 sdret = brcmf_sdiod_recv_buf(bus->sdiodev, rbuf, rdlen);
Arend van Spriel80969832012-06-09 22:51:44 +02001759 bus->sdcnt.f2rxdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001760
1761 /* Control frame failures need retransmission */
1762 if (sdret < 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001763 brcmf_err("read %d control bytes failed: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001764 rdlen, sdret);
Arend van Spriel80969832012-06-09 22:51:44 +02001765 bus->sdcnt.rxc_errors++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001766 brcmf_sdio_rxfail(bus, true, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001767 goto done;
Franky Lindd43a012012-11-05 16:22:22 -08001768 } else
1769 memcpy(buf + BRCMF_FIRSTREAD, rbuf, rdlen);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001770
1771gotpkt:
1772
Joe Perches1e023822012-01-15 00:38:40 -08001773 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_CTL_ON(),
Franky Lindd43a012012-11-05 16:22:22 -08001774 buf, len, "RxCtrl:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001775
1776 /* Point to valid data and indicate its length */
Franky Lindd43a012012-11-05 16:22:22 -08001777 spin_lock_bh(&bus->rxctl_lock);
1778 if (bus->rxctl) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001779 brcmf_err("last control frame is being processed.\n");
Franky Lindd43a012012-11-05 16:22:22 -08001780 spin_unlock_bh(&bus->rxctl_lock);
1781 vfree(buf);
1782 goto done;
1783 }
1784 bus->rxctl = buf + doff;
1785 bus->rxctl_orig = buf;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001786 bus->rxlen = len - doff;
Franky Lindd43a012012-11-05 16:22:22 -08001787 spin_unlock_bh(&bus->rxctl_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001788
1789done:
1790 /* Awake any waiters */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001791 brcmf_sdio_dcmd_resp_wake(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001792}
1793
1794/* Pad read to blocksize for efficiency */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001795static void brcmf_sdio_pad(struct brcmf_sdio *bus, u16 *pad, u16 *rdlen)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001796{
1797 if (bus->roundup && bus->blocksize && *rdlen > bus->blocksize) {
1798 *pad = bus->blocksize - (*rdlen % bus->blocksize);
1799 if (*pad <= bus->roundup && *pad < bus->blocksize &&
1800 *rdlen + *pad + BRCMF_FIRSTREAD < MAX_RX_DATASZ)
1801 *rdlen += *pad;
Arend van Spriel9b2d2f2a82013-11-29 12:25:14 +01001802 } else if (*rdlen % bus->head_align) {
1803 *rdlen += bus->head_align - (*rdlen % bus->head_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001804 }
1805}
1806
Franky Lin4754fce2012-09-19 22:21:11 +02001807static uint brcmf_sdio_readframes(struct brcmf_sdio *bus, uint maxframes)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001808{
Arend van Spriel5b435de2011-10-05 13:19:03 +02001809 struct sk_buff *pkt; /* Packet for event or data frames */
1810 u16 pad; /* Number of pad bytes to read */
Arend van Spriel5b435de2011-10-05 13:19:03 +02001811 uint rxleft = 0; /* Remaining number of frames allowed */
Arend van Spriel349e7102013-03-03 12:45:28 +01001812 int ret; /* Return code from calls */
Arend van Spriel5b435de2011-10-05 13:19:03 +02001813 uint rxcount = 0; /* Total frames read */
Franky Lin6bc52312013-08-10 12:27:28 +02001814 struct brcmf_sdio_hdrinfo *rd = &bus->cur_read, rd_new;
Franky Lin4754fce2012-09-19 22:21:11 +02001815 u8 head_read = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001816
1817 brcmf_dbg(TRACE, "Enter\n");
1818
1819 /* Not finished unless we encounter no more frames indication */
Franky Lin4754fce2012-09-19 22:21:11 +02001820 bus->rxpending = true;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001821
Franky Lin4754fce2012-09-19 22:21:11 +02001822 for (rd->seq_num = bus->rx_seq, rxleft = maxframes;
Arend van Spriela1ce7a02015-02-06 18:36:42 +01001823 !bus->rxskip && rxleft && bus->sdiodev->state == BRCMF_SDIOD_DATA;
Franky Lin4754fce2012-09-19 22:21:11 +02001824 rd->seq_num++, rxleft--) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001825
1826 /* Handle glomming separately */
Arend van Sprielb83db862011-10-19 12:51:09 +02001827 if (bus->glomd || !skb_queue_empty(&bus->glom)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001828 u8 cnt;
1829 brcmf_dbg(GLOM, "calling rxglom: glomd %p, glom %p\n",
Arend van Sprielb83db862011-10-19 12:51:09 +02001830 bus->glomd, skb_peek(&bus->glom));
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001831 cnt = brcmf_sdio_rxglom(bus, rd->seq_num);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001832 brcmf_dbg(GLOM, "rxglom returned %d\n", cnt);
Franky Lin4754fce2012-09-19 22:21:11 +02001833 rd->seq_num += cnt - 1;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001834 rxleft = (rxleft > cnt) ? (rxleft - cnt) : 1;
1835 continue;
1836 }
1837
Franky Lin4754fce2012-09-19 22:21:11 +02001838 rd->len_left = rd->len;
1839 /* read header first for unknow frame length */
Franky Lin38b0b0d2012-11-05 16:22:24 -08001840 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001841 if (!rd->len) {
Arend van Spriela39be272013-12-12 11:58:58 +01001842 ret = brcmf_sdiod_recv_buf(bus->sdiodev,
Arend van Spriela39be272013-12-12 11:58:58 +01001843 bus->rxhdr, BRCMF_FIRSTREAD);
Franky Lin4754fce2012-09-19 22:21:11 +02001844 bus->sdcnt.f2rxhdrs++;
Arend van Spriel349e7102013-03-03 12:45:28 +01001845 if (ret < 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001846 brcmf_err("RXHEADER FAILED: %d\n",
Arend van Spriel349e7102013-03-03 12:45:28 +01001847 ret);
Franky Lin4754fce2012-09-19 22:21:11 +02001848 bus->sdcnt.rx_hdrfail++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001849 brcmf_sdio_rxfail(bus, true, true);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001850 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001851 continue;
1852 }
1853
Franky Lin4754fce2012-09-19 22:21:11 +02001854 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() || BRCMF_HDRS_ON(),
1855 bus->rxhdr, SDPCM_HDRLEN,
1856 "RxHdr:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001857
Franky Lin6bc52312013-08-10 12:27:28 +02001858 if (brcmf_sdio_hdparse(bus, bus->rxhdr, rd,
1859 BRCMF_SDIO_FT_NORMAL)) {
Franky Lin38b0b0d2012-11-05 16:22:24 -08001860 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001861 if (!bus->rxpending)
1862 break;
1863 else
1864 continue;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001865 }
1866
Franky Lin4754fce2012-09-19 22:21:11 +02001867 if (rd->channel == SDPCM_CONTROL_CHANNEL) {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001868 brcmf_sdio_read_control(bus, bus->rxhdr,
1869 rd->len,
1870 rd->dat_offset);
Franky Lin4754fce2012-09-19 22:21:11 +02001871 /* prepare the descriptor for the next read */
1872 rd->len = rd->len_nxtfrm << 4;
1873 rd->len_nxtfrm = 0;
1874 /* treat all packet as event if we don't know */
1875 rd->channel = SDPCM_EVENT_CHANNEL;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001876 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001877 continue;
1878 }
1879 rd->len_left = rd->len > BRCMF_FIRSTREAD ?
1880 rd->len - BRCMF_FIRSTREAD : 0;
1881 head_read = BRCMF_FIRSTREAD;
1882 }
1883
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001884 brcmf_sdio_pad(bus, &pad, &rd->len_left);
Franky Lin4754fce2012-09-19 22:21:11 +02001885
1886 pkt = brcmu_pkt_buf_get_skb(rd->len_left + head_read +
Arend van Spriel9b2d2f2a82013-11-29 12:25:14 +01001887 bus->head_align);
Franky Lin4754fce2012-09-19 22:21:11 +02001888 if (!pkt) {
1889 /* Give up on data, request rtx of events */
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001890 brcmf_err("brcmu_pkt_buf_get_skb failed\n");
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001891 brcmf_sdio_rxfail(bus, false,
Franky Lin4754fce2012-09-19 22:21:11 +02001892 RETRYCHAN(rd->channel));
Franky Lin38b0b0d2012-11-05 16:22:24 -08001893 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001894 continue;
1895 }
1896 skb_pull(pkt, head_read);
Arend van Spriel9b2d2f2a82013-11-29 12:25:14 +01001897 pkt_align(pkt, rd->len_left, bus->head_align);
Franky Lin4754fce2012-09-19 22:21:11 +02001898
Arend van Spriela7cdd822013-12-12 11:58:59 +01001899 ret = brcmf_sdiod_recv_pkt(bus->sdiodev, pkt);
Franky Lin4754fce2012-09-19 22:21:11 +02001900 bus->sdcnt.f2rxdata++;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001901 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001902
Arend van Spriel349e7102013-03-03 12:45:28 +01001903 if (ret < 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001904 brcmf_err("read %d bytes from channel %d failed: %d\n",
Arend van Spriel349e7102013-03-03 12:45:28 +01001905 rd->len, rd->channel, ret);
Franky Lin4754fce2012-09-19 22:21:11 +02001906 brcmu_pkt_buf_free_skb(pkt);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001907 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001908 brcmf_sdio_rxfail(bus, true,
Franky Lin4754fce2012-09-19 22:21:11 +02001909 RETRYCHAN(rd->channel));
Franky Lin38b0b0d2012-11-05 16:22:24 -08001910 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001911 continue;
1912 }
1913
1914 if (head_read) {
1915 skb_push(pkt, head_read);
1916 memcpy(pkt->data, bus->rxhdr, head_read);
1917 head_read = 0;
1918 } else {
1919 memcpy(bus->rxhdr, pkt->data, SDPCM_HDRLEN);
1920 rd_new.seq_num = rd->seq_num;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001921 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin6bc52312013-08-10 12:27:28 +02001922 if (brcmf_sdio_hdparse(bus, bus->rxhdr, &rd_new,
1923 BRCMF_SDIO_FT_NORMAL)) {
Franky Lin4754fce2012-09-19 22:21:11 +02001924 rd->len = 0;
1925 brcmu_pkt_buf_free_skb(pkt);
1926 }
Arend van Spriel80969832012-06-09 22:51:44 +02001927 bus->sdcnt.rx_readahead_cnt++;
Franky Lin4754fce2012-09-19 22:21:11 +02001928 if (rd->len != roundup(rd_new.len, 16)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001929 brcmf_err("frame length mismatch:read %d, should be %d\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001930 rd->len,
1931 roundup(rd_new.len, 16) >> 4);
1932 rd->len = 0;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001933 brcmf_sdio_rxfail(bus, true, true);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001934 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001935 brcmu_pkt_buf_free_skb(pkt);
1936 continue;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001937 }
Franky Lin38b0b0d2012-11-05 16:22:24 -08001938 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001939 rd->len_nxtfrm = rd_new.len_nxtfrm;
1940 rd->channel = rd_new.channel;
1941 rd->dat_offset = rd_new.dat_offset;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001942
Joe Perches1e023822012-01-15 00:38:40 -08001943 brcmf_dbg_hex_dump(!(BRCMF_BYTES_ON() &&
1944 BRCMF_DATA_ON()) &&
1945 BRCMF_HDRS_ON(),
1946 bus->rxhdr, SDPCM_HDRLEN,
1947 "RxHdr:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001948
Franky Lin4754fce2012-09-19 22:21:11 +02001949 if (rd_new.channel == SDPCM_CONTROL_CHANNEL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001950 brcmf_err("readahead on control packet %d?\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001951 rd_new.seq_num);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001952 /* Force retry w/normal header read */
Franky Lin4754fce2012-09-19 22:21:11 +02001953 rd->len = 0;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001954 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001955 brcmf_sdio_rxfail(bus, false, true);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001956 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001957 brcmu_pkt_buf_free_skb(pkt);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001958 continue;
1959 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02001960 }
1961
Joe Perches1e023822012-01-15 00:38:40 -08001962 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_DATA_ON(),
Franky Lin4754fce2012-09-19 22:21:11 +02001963 pkt->data, rd->len, "Rx Data:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001964
Arend van Spriel5b435de2011-10-05 13:19:03 +02001965 /* Save superframe descriptor and allocate packet frame */
Franky Lin4754fce2012-09-19 22:21:11 +02001966 if (rd->channel == SDPCM_GLOM_CHANNEL) {
Franky Lin6bc52312013-08-10 12:27:28 +02001967 if (SDPCM_GLOMDESC(&bus->rxhdr[SDPCM_HWHDR_LEN])) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001968 brcmf_dbg(GLOM, "glom descriptor, %d bytes:\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001969 rd->len);
Joe Perches1e023822012-01-15 00:38:40 -08001970 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
Franky Lin4754fce2012-09-19 22:21:11 +02001971 pkt->data, rd->len,
Joe Perches1e023822012-01-15 00:38:40 -08001972 "Glom Data:\n");
Franky Lin4754fce2012-09-19 22:21:11 +02001973 __skb_trim(pkt, rd->len);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001974 skb_pull(pkt, SDPCM_HDRLEN);
1975 bus->glomd = pkt;
1976 } else {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001977 brcmf_err("%s: glom superframe w/o "
Arend van Spriel5b435de2011-10-05 13:19:03 +02001978 "descriptor!\n", __func__);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001979 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001980 brcmf_sdio_rxfail(bus, false, false);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001981 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001982 }
Franky Lin4754fce2012-09-19 22:21:11 +02001983 /* prepare the descriptor for the next read */
1984 rd->len = rd->len_nxtfrm << 4;
1985 rd->len_nxtfrm = 0;
1986 /* treat all packet as event if we don't know */
1987 rd->channel = SDPCM_EVENT_CHANNEL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001988 continue;
1989 }
1990
1991 /* Fill in packet len and prio, deliver upward */
Franky Lin4754fce2012-09-19 22:21:11 +02001992 __skb_trim(pkt, rd->len);
1993 skb_pull(pkt, rd->dat_offset);
1994
Franky Linc56caa9d2016-04-11 11:35:25 +02001995 if (pkt->len == 0)
1996 brcmu_pkt_buf_free_skb(pkt);
1997 else if (rd->channel == SDPCM_EVENT_CHANNEL)
1998 brcmf_rx_event(bus->sdiodev->dev, pkt);
1999 else
2000 brcmf_rx_frame(bus->sdiodev->dev, pkt,
2001 false);
2002
Franky Lin4754fce2012-09-19 22:21:11 +02002003 /* prepare the descriptor for the next read */
2004 rd->len = rd->len_nxtfrm << 4;
2005 rd->len_nxtfrm = 0;
2006 /* treat all packet as event if we don't know */
2007 rd->channel = SDPCM_EVENT_CHANNEL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002008 }
Franky Lin4754fce2012-09-19 22:21:11 +02002009
Arend van Spriel5b435de2011-10-05 13:19:03 +02002010 rxcount = maxframes - rxleft;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002011 /* Message if we hit the limit */
2012 if (!rxleft)
Franky Lin4754fce2012-09-19 22:21:11 +02002013 brcmf_dbg(DATA, "hit rx limit of %d frames\n", maxframes);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002014 else
Arend van Spriel5b435de2011-10-05 13:19:03 +02002015 brcmf_dbg(DATA, "processed %d frames\n", rxcount);
2016 /* Back off rxseq if awaiting rtx, update rx_seq */
2017 if (bus->rxskip)
Franky Lin4754fce2012-09-19 22:21:11 +02002018 rd->seq_num--;
2019 bus->rx_seq = rd->seq_num;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002020
2021 return rxcount;
2022}
2023
Arend van Spriel5b435de2011-10-05 13:19:03 +02002024static void
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002025brcmf_sdio_wait_event_wakeup(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002026{
Kosuke Tatsukawaa7decc42015-12-10 13:43:08 +01002027 wake_up_interruptible(&bus->ctrl_wait);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002028 return;
2029}
2030
Franky Lin8da9d2c2013-11-29 12:25:10 +01002031static int brcmf_sdio_txpkt_hdalign(struct brcmf_sdio *bus, struct sk_buff *pkt)
2032{
Arend van Spriele217d1c2013-11-29 12:25:13 +01002033 u16 head_pad;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002034 u8 *dat_buf;
2035
Franky Lin8da9d2c2013-11-29 12:25:10 +01002036 dat_buf = (u8 *)(pkt->data);
2037
2038 /* Check head padding */
Arend van Spriele217d1c2013-11-29 12:25:13 +01002039 head_pad = ((unsigned long)dat_buf % bus->head_align);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002040 if (head_pad) {
2041 if (skb_headroom(pkt) < head_pad) {
2042 bus->sdiodev->bus_if->tx_realloc++;
2043 head_pad = 0;
2044 if (skb_cow(pkt, head_pad))
2045 return -ENOMEM;
2046 }
2047 skb_push(pkt, head_pad);
2048 dat_buf = (u8 *)(pkt->data);
2049 memset(dat_buf, 0, head_pad + bus->tx_hdrlen);
2050 }
2051 return head_pad;
2052}
2053
Franky Lin5491c112013-09-25 13:05:43 +02002054/**
2055 * struct brcmf_skbuff_cb reserves first two bytes in sk_buff::cb for
2056 * bus layer usage.
2057 */
Franky Linb05e9252013-08-10 12:27:26 +02002058/* flag marking a dummy skb added for DMA alignment requirement */
Franky Lin5491c112013-09-25 13:05:43 +02002059#define ALIGN_SKB_FLAG 0x8000
Franky Linb05e9252013-08-10 12:27:26 +02002060/* bit mask of data length chopped from the previous packet */
Franky Lin5491c112013-09-25 13:05:43 +02002061#define ALIGN_SKB_CHOP_LEN_MASK 0x7fff
2062
Franky Lin8da9d2c2013-11-29 12:25:10 +01002063static int brcmf_sdio_txpkt_prep_sg(struct brcmf_sdio *bus,
Arend van Spriela64304f2013-10-15 15:44:56 +02002064 struct sk_buff_head *pktq,
Franky Lin8da9d2c2013-11-29 12:25:10 +01002065 struct sk_buff *pkt, u16 total_len)
Arend van Spriela64304f2013-10-15 15:44:56 +02002066{
Franky Lin8da9d2c2013-11-29 12:25:10 +01002067 struct brcmf_sdio_dev *sdiodev;
Arend van Spriela64304f2013-10-15 15:44:56 +02002068 struct sk_buff *pkt_pad;
Arend van Spriele217d1c2013-11-29 12:25:13 +01002069 u16 tail_pad, tail_chop, chain_pad;
Arend van Spriela64304f2013-10-15 15:44:56 +02002070 unsigned int blksize;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002071 bool lastfrm;
2072 int ntail, ret;
Arend van Spriela64304f2013-10-15 15:44:56 +02002073
Franky Lin8da9d2c2013-11-29 12:25:10 +01002074 sdiodev = bus->sdiodev;
Arend van Spriela64304f2013-10-15 15:44:56 +02002075 blksize = sdiodev->func[SDIO_FUNC_2]->cur_blksize;
Arend van Spriela64304f2013-10-15 15:44:56 +02002076 /* sg entry alignment should be a divisor of block size */
Arend van Spriele217d1c2013-11-29 12:25:13 +01002077 WARN_ON(blksize % bus->sgentry_align);
Arend van Spriela64304f2013-10-15 15:44:56 +02002078
2079 /* Check tail padding */
Franky Lin8da9d2c2013-11-29 12:25:10 +01002080 lastfrm = skb_queue_is_last(pktq, pkt);
2081 tail_pad = 0;
Arend van Spriele217d1c2013-11-29 12:25:13 +01002082 tail_chop = pkt->len % bus->sgentry_align;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002083 if (tail_chop)
Arend van Spriele217d1c2013-11-29 12:25:13 +01002084 tail_pad = bus->sgentry_align - tail_chop;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002085 chain_pad = (total_len + tail_pad) % blksize;
2086 if (lastfrm && chain_pad)
2087 tail_pad += blksize - chain_pad;
Arend van Spriela64304f2013-10-15 15:44:56 +02002088 if (skb_tailroom(pkt) < tail_pad && pkt->len > blksize) {
Arend van Spriel1eb43012014-02-20 18:55:55 +01002089 pkt_pad = brcmu_pkt_buf_get_skb(tail_pad + tail_chop +
2090 bus->head_align);
Arend van Spriela64304f2013-10-15 15:44:56 +02002091 if (pkt_pad == NULL)
2092 return -ENOMEM;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002093 ret = brcmf_sdio_txpkt_hdalign(bus, pkt_pad);
Dave Jones2dc3a8e2014-03-06 15:51:08 -05002094 if (unlikely(ret < 0)) {
2095 kfree_skb(pkt_pad);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002096 return ret;
Dave Jones2dc3a8e2014-03-06 15:51:08 -05002097 }
Arend van Spriela64304f2013-10-15 15:44:56 +02002098 memcpy(pkt_pad->data,
2099 pkt->data + pkt->len - tail_chop,
2100 tail_chop);
Arend van Spriel5aa9f0e2014-02-25 20:30:27 +01002101 *(u16 *)(pkt_pad->cb) = ALIGN_SKB_FLAG + tail_chop;
Arend van Spriela64304f2013-10-15 15:44:56 +02002102 skb_trim(pkt, pkt->len - tail_chop);
Arend van Spriel1eb43012014-02-20 18:55:55 +01002103 skb_trim(pkt_pad, tail_pad + tail_chop);
Arend van Spriela64304f2013-10-15 15:44:56 +02002104 __skb_queue_after(pktq, pkt, pkt_pad);
2105 } else {
2106 ntail = pkt->data_len + tail_pad -
2107 (pkt->end - pkt->tail);
2108 if (skb_cloned(pkt) || ntail > 0)
2109 if (pskb_expand_head(pkt, 0, ntail, GFP_ATOMIC))
2110 return -ENOMEM;
2111 if (skb_linearize(pkt))
2112 return -ENOMEM;
Arend van Spriela64304f2013-10-15 15:44:56 +02002113 __skb_put(pkt, tail_pad);
2114 }
2115
Franky Lin8da9d2c2013-11-29 12:25:10 +01002116 return tail_pad;
Arend van Spriela64304f2013-10-15 15:44:56 +02002117}
2118
Franky Linb05e9252013-08-10 12:27:26 +02002119/**
2120 * brcmf_sdio_txpkt_prep - packet preparation for transmit
2121 * @bus: brcmf_sdio structure pointer
2122 * @pktq: packet list pointer
2123 * @chan: virtual channel to transmit the packet
2124 *
2125 * Processes to be applied to the packet
2126 * - Align data buffer pointer
2127 * - Align data buffer length
2128 * - Prepare header
2129 * Return: negative value if there is error
2130 */
2131static int
2132brcmf_sdio_txpkt_prep(struct brcmf_sdio *bus, struct sk_buff_head *pktq,
2133 uint chan)
2134{
Franky Lin8da9d2c2013-11-29 12:25:10 +01002135 u16 head_pad, total_len;
Arend van Spriela64304f2013-10-15 15:44:56 +02002136 struct sk_buff *pkt_next;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002137 u8 txseq;
2138 int ret;
Franky Lin6bc52312013-08-10 12:27:28 +02002139 struct brcmf_sdio_hdrinfo hd_info = {0};
Franky Linb05e9252013-08-10 12:27:26 +02002140
Franky Lin8da9d2c2013-11-29 12:25:10 +01002141 txseq = bus->tx_seq;
2142 total_len = 0;
2143 skb_queue_walk(pktq, pkt_next) {
2144 /* alignment packet inserted in previous
2145 * loop cycle can be skipped as it is
2146 * already properly aligned and does not
2147 * need an sdpcm header.
2148 */
Arend van Spriel5aa9f0e2014-02-25 20:30:27 +01002149 if (*(u16 *)(pkt_next->cb) & ALIGN_SKB_FLAG)
Franky Lin8da9d2c2013-11-29 12:25:10 +01002150 continue;
Franky Linb05e9252013-08-10 12:27:26 +02002151
Franky Lin8da9d2c2013-11-29 12:25:10 +01002152 /* align packet data pointer */
2153 ret = brcmf_sdio_txpkt_hdalign(bus, pkt_next);
2154 if (ret < 0)
2155 return ret;
2156 head_pad = (u16)ret;
2157 if (head_pad)
Arend van Spriel1eb43012014-02-20 18:55:55 +01002158 memset(pkt_next->data + bus->tx_hdrlen, 0, head_pad);
Franky Linb05e9252013-08-10 12:27:26 +02002159
Franky Lin8da9d2c2013-11-29 12:25:10 +01002160 total_len += pkt_next->len;
Franky Linb05e9252013-08-10 12:27:26 +02002161
Arend van Spriela64304f2013-10-15 15:44:56 +02002162 hd_info.len = pkt_next->len;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002163 hd_info.lastfrm = skb_queue_is_last(pktq, pkt_next);
2164 if (bus->txglom && pktq->qlen > 1) {
2165 ret = brcmf_sdio_txpkt_prep_sg(bus, pktq,
2166 pkt_next, total_len);
2167 if (ret < 0)
2168 return ret;
2169 hd_info.tail_pad = (u16)ret;
2170 total_len += (u16)ret;
2171 }
2172
2173 hd_info.channel = chan;
2174 hd_info.dat_offset = head_pad + bus->tx_hdrlen;
2175 hd_info.seq_num = txseq++;
2176
2177 /* Now fill the header */
2178 brcmf_sdio_hdpack(bus, pkt_next->data, &hd_info);
2179
2180 if (BRCMF_BYTES_ON() &&
2181 ((BRCMF_CTL_ON() && chan == SDPCM_CONTROL_CHANNEL) ||
2182 (BRCMF_DATA_ON() && chan != SDPCM_CONTROL_CHANNEL)))
Hante Meuleman47ab4cd2014-02-25 20:30:29 +01002183 brcmf_dbg_hex_dump(true, pkt_next->data, hd_info.len,
Franky Lin8da9d2c2013-11-29 12:25:10 +01002184 "Tx Frame:\n");
2185 else if (BRCMF_HDRS_ON())
Hante Meuleman47ab4cd2014-02-25 20:30:29 +01002186 brcmf_dbg_hex_dump(true, pkt_next->data,
Franky Lin8da9d2c2013-11-29 12:25:10 +01002187 head_pad + bus->tx_hdrlen,
2188 "Tx Header:\n");
Franky Linb05e9252013-08-10 12:27:26 +02002189 }
Franky Lin8da9d2c2013-11-29 12:25:10 +01002190 /* Hardware length tag of the first packet should be total
2191 * length of the chain (including padding)
2192 */
2193 if (bus->txglom)
2194 brcmf_sdio_update_hwhdr(pktq->next->data, total_len);
Franky Linb05e9252013-08-10 12:27:26 +02002195 return 0;
2196}
2197
2198/**
2199 * brcmf_sdio_txpkt_postp - packet post processing for transmit
2200 * @bus: brcmf_sdio structure pointer
2201 * @pktq: packet list pointer
2202 *
2203 * Processes to be applied to the packet
2204 * - Remove head padding
2205 * - Remove tail padding
2206 */
2207static void
2208brcmf_sdio_txpkt_postp(struct brcmf_sdio *bus, struct sk_buff_head *pktq)
2209{
2210 u8 *hdr;
2211 u32 dat_offset;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002212 u16 tail_pad;
Arend van Spriel5aa9f0e2014-02-25 20:30:27 +01002213 u16 dummy_flags, chop_len;
Franky Linb05e9252013-08-10 12:27:26 +02002214 struct sk_buff *pkt_next, *tmp, *pkt_prev;
2215
2216 skb_queue_walk_safe(pktq, pkt_next, tmp) {
Arend van Spriel5aa9f0e2014-02-25 20:30:27 +01002217 dummy_flags = *(u16 *)(pkt_next->cb);
Franky Lin5491c112013-09-25 13:05:43 +02002218 if (dummy_flags & ALIGN_SKB_FLAG) {
2219 chop_len = dummy_flags & ALIGN_SKB_CHOP_LEN_MASK;
Franky Linb05e9252013-08-10 12:27:26 +02002220 if (chop_len) {
2221 pkt_prev = pkt_next->prev;
Franky Linb05e9252013-08-10 12:27:26 +02002222 skb_put(pkt_prev, chop_len);
2223 }
2224 __skb_unlink(pkt_next, pktq);
2225 brcmu_pkt_buf_free_skb(pkt_next);
2226 } else {
Franky Lin8da9d2c2013-11-29 12:25:10 +01002227 hdr = pkt_next->data + bus->tx_hdrlen - SDPCM_SWHDR_LEN;
Franky Linb05e9252013-08-10 12:27:26 +02002228 dat_offset = le32_to_cpu(*(__le32 *)hdr);
2229 dat_offset = (dat_offset & SDPCM_DOFFSET_MASK) >>
2230 SDPCM_DOFFSET_SHIFT;
2231 skb_pull(pkt_next, dat_offset);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002232 if (bus->txglom) {
2233 tail_pad = le16_to_cpu(*(__le16 *)(hdr - 2));
2234 skb_trim(pkt_next, pkt_next->len - tail_pad);
2235 }
Franky Linb05e9252013-08-10 12:27:26 +02002236 }
2237 }
2238}
2239
Arend van Spriel5b435de2011-10-05 13:19:03 +02002240/* Writes a HW/SW header into the packet and sends it. */
2241/* Assumes: (a) header space already there, (b) caller holds lock */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002242static int brcmf_sdio_txpkt(struct brcmf_sdio *bus, struct sk_buff_head *pktq,
2243 uint chan)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002244{
2245 int ret;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002246 struct sk_buff *pkt_next, *tmp;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002247
2248 brcmf_dbg(TRACE, "Enter\n");
2249
Franky Lin8da9d2c2013-11-29 12:25:10 +01002250 ret = brcmf_sdio_txpkt_prep(bus, pktq, chan);
Franky Linb05e9252013-08-10 12:27:26 +02002251 if (ret)
2252 goto done;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002253
Franky Lin38b0b0d2012-11-05 16:22:24 -08002254 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriela7cdd822013-12-12 11:58:59 +01002255 ret = brcmf_sdiod_send_pkt(bus->sdiodev, pktq);
Arend van Spriel80969832012-06-09 22:51:44 +02002256 bus->sdcnt.f2txdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002257
Hante Meuleman81c78832014-02-25 20:30:35 +01002258 if (ret < 0)
2259 brcmf_sdio_txfail(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002260
Franky Lin38b0b0d2012-11-05 16:22:24 -08002261 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002262
2263done:
Franky Lin8da9d2c2013-11-29 12:25:10 +01002264 brcmf_sdio_txpkt_postp(bus, pktq);
2265 if (ret == 0)
2266 bus->tx_seq = (bus->tx_seq + pktq->qlen) % SDPCM_SEQ_WRAP;
2267 skb_queue_walk_safe(pktq, pkt_next, tmp) {
2268 __skb_unlink(pkt_next, pktq);
Franky Lin7b584392017-03-10 21:17:03 +00002269 brcmf_proto_bcdc_txcomplete(bus->sdiodev->dev, pkt_next,
2270 ret == 0);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002271 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002272 return ret;
2273}
2274
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002275static uint brcmf_sdio_sendfromq(struct brcmf_sdio *bus, uint maxframes)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002276{
2277 struct sk_buff *pkt;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002278 struct sk_buff_head pktq;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002279 u32 intstatus = 0;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002280 int ret = 0, prec_out, i;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002281 uint cnt = 0;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002282 u8 tx_prec_map, pkt_num;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002283
Arend van Spriel5b435de2011-10-05 13:19:03 +02002284 brcmf_dbg(TRACE, "Enter\n");
2285
2286 tx_prec_map = ~bus->flowcontrol;
2287
2288 /* Send frames until the limit or some other event */
Franky Lin8da9d2c2013-11-29 12:25:10 +01002289 for (cnt = 0; (cnt < maxframes) && data_ok(bus);) {
2290 pkt_num = 1;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002291 if (bus->txglom)
2292 pkt_num = min_t(u8, bus->tx_max - bus->tx_seq,
Arend van Sprielaf1fa212014-02-27 19:25:00 +01002293 bus->sdiodev->txglomsz);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002294 pkt_num = min_t(u32, pkt_num,
2295 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol));
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002296 __skb_queue_head_init(&pktq);
2297 spin_lock_bh(&bus->txq_lock);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002298 for (i = 0; i < pkt_num; i++) {
2299 pkt = brcmu_pktq_mdeq(&bus->txq, tx_prec_map,
2300 &prec_out);
2301 if (pkt == NULL)
2302 break;
2303 __skb_queue_tail(&pktq, pkt);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002304 }
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002305 spin_unlock_bh(&bus->txq_lock);
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002306 if (i == 0)
Franky Lin8da9d2c2013-11-29 12:25:10 +01002307 break;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002308
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002309 ret = brcmf_sdio_txpkt(bus, &pktq, SDPCM_DATA_CHANNEL);
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002310
Franky Lin8da9d2c2013-11-29 12:25:10 +01002311 cnt += i;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002312
2313 /* In poll mode, need to check for other events */
Hante Meulemanb6a8cf22014-02-25 20:30:33 +01002314 if (!bus->intr) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02002315 /* Check device status, signal pending interrupt */
Franky Lin38b0b0d2012-11-05 16:22:24 -08002316 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin5c15c232012-05-04 18:27:37 -07002317 ret = r_sdreg32(bus, &intstatus,
2318 offsetof(struct sdpcmd_regs,
2319 intstatus));
Franky Lin38b0b0d2012-11-05 16:22:24 -08002320 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel80969832012-06-09 22:51:44 +02002321 bus->sdcnt.f2txdata++;
Franky Lin5c15c232012-05-04 18:27:37 -07002322 if (ret != 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002323 break;
2324 if (intstatus & bus->hostintmask)
Franky Lin1d382272012-09-13 21:11:59 +02002325 atomic_set(&bus->ipend, 1);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002326 }
2327 }
2328
2329 /* Deflow-control stack if needed */
Arend van Spriela1ce7a02015-02-06 18:36:42 +01002330 if ((bus->sdiodev->state == BRCMF_SDIOD_DATA) &&
Franky Linc8bf3482011-12-16 18:37:08 -08002331 bus->txoff && (pktq_len(&bus->txq) < TXLOW)) {
Hante Meuleman90d03ff2012-09-11 21:18:46 +02002332 bus->txoff = false;
Franky Lin20ec4f52017-03-10 21:17:02 +00002333 brcmf_proto_bcdc_txflowblock(bus->sdiodev->dev, false);
Franky Linc8bf3482011-12-16 18:37:08 -08002334 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002335
2336 return cnt;
2337}
2338
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002339static int brcmf_sdio_tx_ctrlframe(struct brcmf_sdio *bus, u8 *frame, u16 len)
2340{
2341 u8 doff;
2342 u16 pad;
2343 uint retries = 0;
2344 struct brcmf_sdio_hdrinfo hd_info = {0};
2345 int ret;
2346
2347 brcmf_dbg(TRACE, "Enter\n");
2348
2349 /* Back the pointer to make room for bus header */
2350 frame -= bus->tx_hdrlen;
2351 len += bus->tx_hdrlen;
2352
2353 /* Add alignment padding (optional for ctl frames) */
2354 doff = ((unsigned long)frame % bus->head_align);
2355 if (doff) {
2356 frame -= doff;
2357 len += doff;
2358 memset(frame + bus->tx_hdrlen, 0, doff);
2359 }
2360
2361 /* Round send length to next SDIO block */
2362 pad = 0;
2363 if (bus->roundup && bus->blocksize && (len > bus->blocksize)) {
2364 pad = bus->blocksize - (len % bus->blocksize);
2365 if ((pad > bus->roundup) || (pad >= bus->blocksize))
2366 pad = 0;
2367 } else if (len % bus->head_align) {
2368 pad = bus->head_align - (len % bus->head_align);
2369 }
2370 len += pad;
2371
2372 hd_info.len = len - pad;
2373 hd_info.channel = SDPCM_CONTROL_CHANNEL;
2374 hd_info.dat_offset = doff + bus->tx_hdrlen;
2375 hd_info.seq_num = bus->tx_seq;
2376 hd_info.lastfrm = true;
2377 hd_info.tail_pad = pad;
2378 brcmf_sdio_hdpack(bus, frame, &hd_info);
2379
2380 if (bus->txglom)
2381 brcmf_sdio_update_hwhdr(frame, len);
2382
2383 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_CTL_ON(),
2384 frame, len, "Tx Frame:\n");
2385 brcmf_dbg_hex_dump(!(BRCMF_BYTES_ON() && BRCMF_CTL_ON()) &&
2386 BRCMF_HDRS_ON(),
2387 frame, min_t(u16, len, 16), "TxHdr:\n");
2388
2389 do {
2390 ret = brcmf_sdiod_send_buf(bus->sdiodev, frame, len);
2391
2392 if (ret < 0)
2393 brcmf_sdio_txfail(bus);
2394 else
2395 bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQ_WRAP;
2396 } while (ret < 0 && retries++ < TXRETRIES);
2397
2398 return ret;
2399}
2400
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002401static void brcmf_sdio_bus_stop(struct device *dev)
Franky Lina9ffda82011-12-16 18:37:12 -08002402{
2403 u32 local_hostintmask;
2404 u8 saveclk;
Franky Lina9ffda82011-12-16 18:37:12 -08002405 int err;
2406 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01002407 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Lina9ffda82011-12-16 18:37:12 -08002408 struct brcmf_sdio *bus = sdiodev->bus;
2409
2410 brcmf_dbg(TRACE, "Enter\n");
2411
2412 if (bus->watchdog_tsk) {
2413 send_sig(SIGTERM, bus->watchdog_tsk, 1);
2414 kthread_stop(bus->watchdog_tsk);
2415 bus->watchdog_tsk = NULL;
2416 }
2417
Arend van Spriela1ce7a02015-02-06 18:36:42 +01002418 if (sdiodev->state != BRCMF_SDIOD_NOMEDIUM) {
Arend van Sprielbb350712014-01-13 22:20:29 +01002419 sdio_claim_host(sdiodev->func[1]);
Franky Lina9ffda82011-12-16 18:37:12 -08002420
Arend van Sprielbb350712014-01-13 22:20:29 +01002421 /* Enable clock for device interrupts */
2422 brcmf_sdio_bus_sleep(bus, false, false);
Franky Lina9ffda82011-12-16 18:37:12 -08002423
Arend van Sprielbb350712014-01-13 22:20:29 +01002424 /* Disable and clear interrupts at the chip level also */
2425 w_sdreg32(bus, 0, offsetof(struct sdpcmd_regs, hostintmask));
2426 local_hostintmask = bus->hostintmask;
2427 bus->hostintmask = 0;
Franky Lina9ffda82011-12-16 18:37:12 -08002428
Arend van Sprielbb350712014-01-13 22:20:29 +01002429 /* Force backplane clocks to assure F2 interrupt propagates */
2430 saveclk = brcmf_sdiod_regrb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
2431 &err);
2432 if (!err)
2433 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
2434 (saveclk | SBSDIO_FORCE_HT), &err);
2435 if (err)
2436 brcmf_err("Failed to force clock for F2: err %d\n",
2437 err);
Franky Lina9ffda82011-12-16 18:37:12 -08002438
Arend van Sprielbb350712014-01-13 22:20:29 +01002439 /* Turn off the bus (F2), free any pending packets */
2440 brcmf_dbg(INTR, "disable SDIO interrupts\n");
2441 sdio_disable_func(sdiodev->func[SDIO_FUNC_2]);
2442
2443 /* Clear any pending interrupts now that F2 is disabled */
2444 w_sdreg32(bus, local_hostintmask,
2445 offsetof(struct sdpcmd_regs, intstatus));
2446
2447 sdio_release_host(sdiodev->func[1]);
Franky Lina9ffda82011-12-16 18:37:12 -08002448 }
Franky Lina9ffda82011-12-16 18:37:12 -08002449 /* Clear the data packet queues */
2450 brcmu_pktq_flush(&bus->txq, true, NULL, NULL);
2451
2452 /* Clear any held glomming stuff */
Markus Elfring297540f2015-02-04 20:28:49 +01002453 brcmu_pkt_buf_free_skb(bus->glomd);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002454 brcmf_sdio_free_glom(bus);
Franky Lina9ffda82011-12-16 18:37:12 -08002455
2456 /* Clear rx control and wake any waiters */
Franky Lindd43a012012-11-05 16:22:22 -08002457 spin_lock_bh(&bus->rxctl_lock);
Franky Lina9ffda82011-12-16 18:37:12 -08002458 bus->rxlen = 0;
Franky Lindd43a012012-11-05 16:22:22 -08002459 spin_unlock_bh(&bus->rxctl_lock);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002460 brcmf_sdio_dcmd_resp_wake(bus);
Franky Lina9ffda82011-12-16 18:37:12 -08002461
2462 /* Reset some F2 state stuff */
2463 bus->rxskip = false;
2464 bus->tx_seq = bus->rx_seq = 0;
Franky Lina9ffda82011-12-16 18:37:12 -08002465}
2466
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002467static inline void brcmf_sdio_clrintr(struct brcmf_sdio *bus)
Franky Linba89bf12012-04-27 18:56:59 -07002468{
Hante Meulemanaf5b5e62016-02-17 11:27:08 +01002469 struct brcmf_sdio_dev *sdiodev;
Franky Linba89bf12012-04-27 18:56:59 -07002470 unsigned long flags;
2471
Hante Meulemanaf5b5e62016-02-17 11:27:08 +01002472 sdiodev = bus->sdiodev;
2473 if (sdiodev->oob_irq_requested) {
2474 spin_lock_irqsave(&sdiodev->irq_en_lock, flags);
2475 if (!sdiodev->irq_en && !atomic_read(&bus->ipend)) {
2476 enable_irq(sdiodev->settings->bus.sdio.oob_irq_nr);
2477 sdiodev->irq_en = true;
Hante Meuleman668761a2013-04-12 10:55:55 +02002478 }
Hante Meulemanaf5b5e62016-02-17 11:27:08 +01002479 spin_unlock_irqrestore(&sdiodev->irq_en_lock, flags);
Franky Linba89bf12012-04-27 18:56:59 -07002480 }
Franky Linba89bf12012-04-27 18:56:59 -07002481}
Franky Linba89bf12012-04-27 18:56:59 -07002482
Franky Lin45316032012-09-13 21:12:03 +02002483static int brcmf_sdio_intr_rstatus(struct brcmf_sdio *bus)
2484{
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01002485 struct brcmf_core *buscore;
Franky Lin45316032012-09-13 21:12:03 +02002486 u32 addr;
2487 unsigned long val;
Hante Meuleman5cbb9c22014-03-06 10:16:12 +01002488 int ret;
Franky Lin45316032012-09-13 21:12:03 +02002489
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01002490 buscore = brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV);
2491 addr = buscore->base + offsetof(struct sdpcmd_regs, intstatus);
Franky Lin45316032012-09-13 21:12:03 +02002492
Arend van Spriela39be272013-12-12 11:58:58 +01002493 val = brcmf_sdiod_regrl(bus->sdiodev, addr, &ret);
Franky Lin45316032012-09-13 21:12:03 +02002494 bus->sdcnt.f1regdata++;
2495 if (ret != 0)
Hante Meuleman5cbb9c22014-03-06 10:16:12 +01002496 return ret;
Franky Lin45316032012-09-13 21:12:03 +02002497
2498 val &= bus->hostintmask;
2499 atomic_set(&bus->fcstate, !!(val & I_HMB_FC_STATE));
2500
2501 /* Clear interrupts */
2502 if (val) {
Arend van Spriela39be272013-12-12 11:58:58 +01002503 brcmf_sdiod_regwl(bus->sdiodev, addr, val, &ret);
Franky Lin45316032012-09-13 21:12:03 +02002504 bus->sdcnt.f1regdata++;
Vineet Guptad3928d02015-07-09 13:43:18 +05302505 atomic_or(val, &bus->intstatus);
Franky Lin45316032012-09-13 21:12:03 +02002506 }
2507
2508 return ret;
2509}
2510
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002511static void brcmf_sdio_dpc(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002512{
Franky Lin45316032012-09-13 21:12:03 +02002513 u32 newstatus = 0;
2514 unsigned long intstatus;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002515 uint txlimit = bus->txbound; /* Tx frames to send before resched */
Hante Meulemanb6a8cf22014-02-25 20:30:33 +01002516 uint framecnt; /* Temporary counter of tx/rx frames */
Hante Meuleman5cbb9c22014-03-06 10:16:12 +01002517 int err = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002518
2519 brcmf_dbg(TRACE, "Enter\n");
2520
Franky Lin38b0b0d2012-11-05 16:22:24 -08002521 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002522
2523 /* If waiting for HTAVAIL, check status */
Piotr Haber4a3da992013-04-11 13:28:46 +02002524 if (!bus->sr_enabled && bus->clkstate == CLK_PENDING) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02002525 u8 clkctl, devctl = 0;
2526
Joe Perches8ae74652012-01-15 00:38:38 -08002527#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02002528 /* Check for inconsistent device control */
Arend van Spriela39be272013-12-12 11:58:58 +01002529 devctl = brcmf_sdiod_regrb(bus->sdiodev,
2530 SBSDIO_DEVICE_CTL, &err);
Joe Perches8ae74652012-01-15 00:38:38 -08002531#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02002532
2533 /* Read CSR, if clock on switch to AVAIL, else ignore */
Arend van Spriela39be272013-12-12 11:58:58 +01002534 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
2535 SBSDIO_FUNC1_CHIPCLKCSR, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002536
Arend van Sprielc3203372013-04-03 12:40:44 +02002537 brcmf_dbg(SDIO, "DPC: PENDING, devctl 0x%02x clkctl 0x%02x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02002538 devctl, clkctl);
2539
2540 if (SBSDIO_HTAV(clkctl)) {
Arend van Spriela39be272013-12-12 11:58:58 +01002541 devctl = brcmf_sdiod_regrb(bus->sdiodev,
2542 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002543 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
Arend van Spriela39be272013-12-12 11:58:58 +01002544 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
2545 devctl, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002546 bus->clkstate = CLK_AVAIL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002547 }
2548 }
2549
Arend van Spriel5b435de2011-10-05 13:19:03 +02002550 /* Make sure backplane clock is on */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002551 brcmf_sdio_bus_sleep(bus, false, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002552
2553 /* Pending interrupt indicates new device status */
Franky Lin1d382272012-09-13 21:11:59 +02002554 if (atomic_read(&bus->ipend) > 0) {
2555 atomic_set(&bus->ipend, 0);
Franky Lin45316032012-09-13 21:12:03 +02002556 err = brcmf_sdio_intr_rstatus(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002557 }
2558
Franky Lin45316032012-09-13 21:12:03 +02002559 /* Start with leftover status bits */
2560 intstatus = atomic_xchg(&bus->intstatus, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002561
2562 /* Handle flow-control change: read new state in case our ack
2563 * crossed another change interrupt. If change still set, assume
2564 * FC ON for safety, let next loop through do the debounce.
2565 */
2566 if (intstatus & I_HMB_FC_CHANGE) {
2567 intstatus &= ~I_HMB_FC_CHANGE;
Franky Lin5c15c232012-05-04 18:27:37 -07002568 err = w_sdreg32(bus, I_HMB_FC_CHANGE,
2569 offsetof(struct sdpcmd_regs, intstatus));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002570
Franky Lin5c15c232012-05-04 18:27:37 -07002571 err = r_sdreg32(bus, &newstatus,
2572 offsetof(struct sdpcmd_regs, intstatus));
Arend van Spriel80969832012-06-09 22:51:44 +02002573 bus->sdcnt.f1regdata += 2;
Franky Lin45316032012-09-13 21:12:03 +02002574 atomic_set(&bus->fcstate,
2575 !!(newstatus & (I_HMB_FC_STATE | I_HMB_FC_CHANGE)));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002576 intstatus |= (newstatus & bus->hostintmask);
2577 }
2578
2579 /* Handle host mailbox indication */
2580 if (intstatus & I_HMB_HOST_INT) {
2581 intstatus &= ~I_HMB_HOST_INT;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002582 intstatus |= brcmf_sdio_hostmail(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002583 }
2584
Franky Lin38b0b0d2012-11-05 16:22:24 -08002585 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin7cdf57d2012-11-05 16:22:23 -08002586
Arend van Spriel5b435de2011-10-05 13:19:03 +02002587 /* Generally don't ask for these, can get CRC errors... */
2588 if (intstatus & I_WR_OOSYNC) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002589 brcmf_err("Dongle reports WR_OOSYNC\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002590 intstatus &= ~I_WR_OOSYNC;
2591 }
2592
2593 if (intstatus & I_RD_OOSYNC) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002594 brcmf_err("Dongle reports RD_OOSYNC\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002595 intstatus &= ~I_RD_OOSYNC;
2596 }
2597
2598 if (intstatus & I_SBINT) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002599 brcmf_err("Dongle reports SBINT\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002600 intstatus &= ~I_SBINT;
2601 }
2602
2603 /* Would be active due to wake-wlan in gSPI */
2604 if (intstatus & I_CHIPACTIVE) {
2605 brcmf_dbg(INFO, "Dongle reports CHIPACTIVE\n");
2606 intstatus &= ~I_CHIPACTIVE;
2607 }
2608
2609 /* Ignore frame indications if rxskip is set */
2610 if (bus->rxskip)
2611 intstatus &= ~I_HMB_FRAME_IND;
2612
2613 /* On frame indication, read available frames */
Hante Meulemanb6a8cf22014-02-25 20:30:33 +01002614 if ((intstatus & I_HMB_FRAME_IND) && (bus->clkstate == CLK_AVAIL)) {
2615 brcmf_sdio_readframes(bus, bus->rxbound);
Franky Lin4754fce2012-09-19 22:21:11 +02002616 if (!bus->rxpending)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002617 intstatus &= ~I_HMB_FRAME_IND;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002618 }
2619
2620 /* Keep still-pending events for next scheduling */
Hante Meuleman5cbb9c22014-03-06 10:16:12 +01002621 if (intstatus)
Vineet Guptad3928d02015-07-09 13:43:18 +05302622 atomic_or(intstatus, &bus->intstatus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002623
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002624 brcmf_sdio_clrintr(bus);
Franky Linba89bf12012-04-27 18:56:59 -07002625
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002626 if (bus->ctrl_frame_stat && (bus->clkstate == CLK_AVAIL) &&
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002627 data_ok(bus)) {
2628 sdio_claim_host(bus->sdiodev->func[1]);
Hante Meuleman449e58b2015-03-06 18:40:40 +01002629 if (bus->ctrl_frame_stat) {
2630 err = brcmf_sdio_tx_ctrlframe(bus, bus->ctrl_frame_buf,
2631 bus->ctrl_frame_len);
2632 bus->ctrl_frame_err = err;
Hante Meuleman2c64e162015-03-18 13:25:22 +01002633 wmb();
Hante Meuleman449e58b2015-03-06 18:40:40 +01002634 bus->ctrl_frame_stat = false;
2635 }
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002636 sdio_release_host(bus->sdiodev->func[1]);
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002637 brcmf_sdio_wait_event_wakeup(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002638 }
2639 /* Send queued frames (limit 1 if rx may still be pending) */
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002640 if ((bus->clkstate == CLK_AVAIL) && !atomic_read(&bus->fcstate) &&
2641 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) && txlimit &&
2642 data_ok(bus)) {
Franky Lin4754fce2012-09-19 22:21:11 +02002643 framecnt = bus->rxpending ? min(txlimit, bus->txminmax) :
2644 txlimit;
Hante Meulemanb6a8cf22014-02-25 20:30:33 +01002645 brcmf_sdio_sendfromq(bus, framecnt);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002646 }
2647
Arend van Spriela1ce7a02015-02-06 18:36:42 +01002648 if ((bus->sdiodev->state != BRCMF_SDIOD_DATA) || (err != 0)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002649 brcmf_err("failed backplane access over SDIO, halting operation\n");
Franky Lin45316032012-09-13 21:12:03 +02002650 atomic_set(&bus->intstatus, 0);
Hante Meulemande6878c2015-03-06 18:40:38 +01002651 if (bus->ctrl_frame_stat) {
Hante Meuleman449e58b2015-03-06 18:40:40 +01002652 sdio_claim_host(bus->sdiodev->func[1]);
2653 if (bus->ctrl_frame_stat) {
2654 bus->ctrl_frame_err = -ENODEV;
Hante Meuleman2c64e162015-03-18 13:25:22 +01002655 wmb();
Hante Meuleman449e58b2015-03-06 18:40:40 +01002656 bus->ctrl_frame_stat = false;
2657 brcmf_sdio_wait_event_wakeup(bus);
2658 }
2659 sdio_release_host(bus->sdiodev->func[1]);
Hante Meulemande6878c2015-03-06 18:40:38 +01002660 }
Franky Lin45316032012-09-13 21:12:03 +02002661 } else if (atomic_read(&bus->intstatus) ||
2662 atomic_read(&bus->ipend) > 0 ||
2663 (!atomic_read(&bus->fcstate) &&
2664 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) &&
Hante Meulemanb6a8cf22014-02-25 20:30:33 +01002665 data_ok(bus))) {
Hante Meuleman2c64e162015-03-18 13:25:22 +01002666 bus->dpc_triggered = true;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002667 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002668}
2669
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002670static struct pktq *brcmf_sdio_bus_gettxq(struct device *dev)
Arend van Spriele2432b62013-04-03 12:40:38 +02002671{
2672 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
2673 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
2674 struct brcmf_sdio *bus = sdiodev->bus;
2675
2676 return &bus->txq;
2677}
2678
Hante Meuleman84936622014-10-28 14:56:19 +01002679static bool brcmf_sdio_prec_enq(struct pktq *q, struct sk_buff *pkt, int prec)
2680{
2681 struct sk_buff *p;
2682 int eprec = -1; /* precedence to evict from */
2683
2684 /* Fast case, precedence queue is not full and we are also not
2685 * exceeding total queue length
2686 */
2687 if (!pktq_pfull(q, prec) && !pktq_full(q)) {
2688 brcmu_pktq_penq(q, prec, pkt);
2689 return true;
2690 }
2691
2692 /* Determine precedence from which to evict packet, if any */
2693 if (pktq_pfull(q, prec)) {
2694 eprec = prec;
2695 } else if (pktq_full(q)) {
2696 p = brcmu_pktq_peek_tail(q, &eprec);
2697 if (eprec > prec)
2698 return false;
2699 }
2700
2701 /* Evict if needed */
2702 if (eprec >= 0) {
2703 /* Detect queueing to unconfigured precedence */
2704 if (eprec == prec)
2705 return false; /* refuse newer (incoming) packet */
2706 /* Evict packet according to discard policy */
2707 p = brcmu_pktq_pdeq_tail(q, eprec);
2708 if (p == NULL)
2709 brcmf_err("brcmu_pktq_pdeq_tail() failed\n");
2710 brcmu_pkt_buf_free_skb(p);
2711 }
2712
2713 /* Enqueue */
2714 p = brcmu_pktq_penq(q, prec, pkt);
2715 if (p == NULL)
2716 brcmf_err("brcmu_pktq_penq() failed\n");
2717
2718 return p != NULL;
2719}
2720
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002721static int brcmf_sdio_bus_txdata(struct device *dev, struct sk_buff *pkt)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002722{
2723 int ret = -EBADE;
Arend van Spriel44ff5662014-02-25 20:30:28 +01002724 uint prec;
Franky Linbf347bb2011-11-22 17:21:56 -08002725 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01002726 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Linbf347bb2011-11-22 17:21:56 -08002727 struct brcmf_sdio *bus = sdiodev->bus;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002728
Arend van Spriel44ff5662014-02-25 20:30:28 +01002729 brcmf_dbg(TRACE, "Enter: pkt: data %p len %d\n", pkt->data, pkt->len);
Arend van Spriel5768f312015-06-11 00:12:20 +02002730 if (sdiodev->state != BRCMF_SDIOD_DATA)
2731 return -EIO;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002732
2733 /* Add space for the header */
Franky Lin706478c2013-08-10 12:27:29 +02002734 skb_push(pkt, bus->tx_hdrlen);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002735 /* precondition: IS_ALIGNED((unsigned long)(pkt->data), 2) */
2736
2737 prec = prio2prec((pkt->priority & PRIOMASK));
2738
2739 /* Check for existing queue, current flow-control,
2740 pending event, or pending clock */
2741 brcmf_dbg(TRACE, "deferring pktq len %d\n", pktq_len(&bus->txq));
Arend van Spriel80969832012-06-09 22:51:44 +02002742 bus->sdcnt.fcqueued++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002743
2744 /* Priority based enq */
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002745 spin_lock_bh(&bus->txq_lock);
Arend van Spriel5aa9f0e2014-02-25 20:30:27 +01002746 /* reset bus_flags in packet cb */
2747 *(u16 *)(pkt->cb) = 0;
Hante Meuleman84936622014-10-28 14:56:19 +01002748 if (!brcmf_sdio_prec_enq(&bus->txq, pkt, prec)) {
Franky Lin706478c2013-08-10 12:27:29 +02002749 skb_pull(pkt, bus->tx_hdrlen);
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002750 brcmf_err("out of bus->txq !!!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002751 ret = -ENOSR;
2752 } else {
2753 ret = 0;
2754 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002755
Franky Linc8bf3482011-12-16 18:37:08 -08002756 if (pktq_len(&bus->txq) >= TXHI) {
Hante Meuleman90d03ff2012-09-11 21:18:46 +02002757 bus->txoff = true;
Franky Lin20ec4f52017-03-10 21:17:02 +00002758 brcmf_proto_bcdc_txflowblock(dev, true);
Franky Linc8bf3482011-12-16 18:37:08 -08002759 }
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002760 spin_unlock_bh(&bus->txq_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002761
Joe Perches8ae74652012-01-15 00:38:38 -08002762#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02002763 if (pktq_plen(&bus->txq, prec) > qcount[prec])
2764 qcount[prec] = pktq_plen(&bus->txq, prec);
2765#endif
Franky Linf1e68c22012-09-13 21:12:00 +02002766
Arend van Spriel99824642015-02-06 18:36:45 +01002767 brcmf_sdio_trigger_dpc(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002768 return ret;
2769}
2770
Joe Perches8ae74652012-01-15 00:38:38 -08002771#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02002772#define CONSOLE_LINE_MAX 192
2773
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002774static int brcmf_sdio_readconsole(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002775{
2776 struct brcmf_console *c = &bus->console;
2777 u8 line[CONSOLE_LINE_MAX], ch;
2778 u32 n, idx, addr;
2779 int rv;
2780
2781 /* Don't do anything until FWREADY updates console address */
2782 if (bus->console_addr == 0)
2783 return 0;
2784
2785 /* Read console log struct */
2786 addr = bus->console_addr + offsetof(struct rte_console, log_le);
Arend van Spriela39be272013-12-12 11:58:58 +01002787 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, (u8 *)&c->log_le,
2788 sizeof(c->log_le));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002789 if (rv < 0)
2790 return rv;
2791
2792 /* Allocate console buffer (one time only) */
2793 if (c->buf == NULL) {
2794 c->bufsize = le32_to_cpu(c->log_le.buf_size);
2795 c->buf = kmalloc(c->bufsize, GFP_ATOMIC);
2796 if (c->buf == NULL)
2797 return -ENOMEM;
2798 }
2799
2800 idx = le32_to_cpu(c->log_le.idx);
2801
2802 /* Protect against corrupt value */
2803 if (idx > c->bufsize)
2804 return -EBADE;
2805
2806 /* Skip reading the console buffer if the index pointer
2807 has not moved */
2808 if (idx == c->last)
2809 return 0;
2810
2811 /* Read the console buffer */
2812 addr = le32_to_cpu(c->log_le.buf);
Arend van Spriela39be272013-12-12 11:58:58 +01002813 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, c->buf, c->bufsize);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002814 if (rv < 0)
2815 return rv;
2816
2817 while (c->last != idx) {
2818 for (n = 0; n < CONSOLE_LINE_MAX - 2; n++) {
2819 if (c->last == idx) {
2820 /* This would output a partial line.
2821 * Instead, back up
2822 * the buffer pointer and output this
2823 * line next time around.
2824 */
2825 if (c->last >= n)
2826 c->last -= n;
2827 else
2828 c->last = c->bufsize - n;
2829 goto break2;
2830 }
2831 ch = c->buf[c->last];
2832 c->last = (c->last + 1) % c->bufsize;
2833 if (ch == '\n')
2834 break;
2835 line[n] = ch;
2836 }
2837
2838 if (n > 0) {
2839 if (line[n - 1] == '\r')
2840 n--;
2841 line[n] = 0;
Joe Perches18aad4f2012-01-15 00:38:42 -08002842 pr_debug("CONSOLE: %s\n", line);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002843 }
2844 }
2845break2:
2846
2847 return 0;
2848}
Joe Perches8ae74652012-01-15 00:38:38 -08002849#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02002850
Franky Linfcf094f2011-12-16 18:37:16 -08002851static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002852brcmf_sdio_bus_txctl(struct device *dev, unsigned char *msg, uint msglen)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002853{
Franky Lin47a1ce72011-11-22 17:21:55 -08002854 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01002855 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Lin47a1ce72011-11-22 17:21:55 -08002856 struct brcmf_sdio *bus = sdiodev->bus;
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002857 int ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002858
2859 brcmf_dbg(TRACE, "Enter\n");
Arend van Spriel5768f312015-06-11 00:12:20 +02002860 if (sdiodev->state != BRCMF_SDIOD_DATA)
2861 return -EIO;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002862
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002863 /* Send from dpc */
2864 bus->ctrl_frame_buf = msg;
2865 bus->ctrl_frame_len = msglen;
Hante Meuleman2c64e162015-03-18 13:25:22 +01002866 wmb();
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002867 bus->ctrl_frame_stat = true;
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002868
Arend van Spriel99824642015-02-06 18:36:45 +01002869 brcmf_sdio_trigger_dpc(bus);
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002870 wait_event_interruptible_timeout(bus->ctrl_wait, !bus->ctrl_frame_stat,
Arend van Spriel63ce3d52016-01-05 11:05:48 +01002871 CTL_DONE_TIMEOUT);
Hante Meuleman449e58b2015-03-06 18:40:40 +01002872 ret = 0;
2873 if (bus->ctrl_frame_stat) {
2874 sdio_claim_host(bus->sdiodev->func[1]);
2875 if (bus->ctrl_frame_stat) {
2876 brcmf_dbg(SDIO, "ctrl_frame timeout\n");
2877 bus->ctrl_frame_stat = false;
2878 ret = -ETIMEDOUT;
2879 }
2880 sdio_release_host(bus->sdiodev->func[1]);
2881 }
2882 if (!ret) {
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002883 brcmf_dbg(SDIO, "ctrl_frame complete, err=%d\n",
2884 bus->ctrl_frame_err);
Hante Meuleman2c64e162015-03-18 13:25:22 +01002885 rmb();
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002886 ret = bus->ctrl_frame_err;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002887 }
2888
Arend van Spriel5b435de2011-10-05 13:19:03 +02002889 if (ret)
Arend van Spriel80969832012-06-09 22:51:44 +02002890 bus->sdcnt.tx_ctlerrs++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002891 else
Arend van Spriel80969832012-06-09 22:51:44 +02002892 bus->sdcnt.tx_ctlpkts++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002893
Hante Meuleman4dd8b262015-01-25 20:31:32 +01002894 return ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002895}
2896
Arend van Spriel80969832012-06-09 22:51:44 +02002897#ifdef DEBUG
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002898static int brcmf_sdio_dump_console(struct seq_file *seq, struct brcmf_sdio *bus,
2899 struct sdpcm_shared *sh)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002900{
2901 u32 addr, console_ptr, console_size, console_index;
2902 char *conbuf = NULL;
2903 __le32 sh_val;
2904 int rv;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002905
2906 /* obtain console information from device memory */
2907 addr = sh->console_addr + offsetof(struct rte_console, log_le);
Arend van Spriela39be272013-12-12 11:58:58 +01002908 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
2909 (u8 *)&sh_val, sizeof(u32));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002910 if (rv < 0)
2911 return rv;
2912 console_ptr = le32_to_cpu(sh_val);
2913
2914 addr = sh->console_addr + offsetof(struct rte_console, log_le.buf_size);
Arend van Spriela39be272013-12-12 11:58:58 +01002915 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
2916 (u8 *)&sh_val, sizeof(u32));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002917 if (rv < 0)
2918 return rv;
2919 console_size = le32_to_cpu(sh_val);
2920
2921 addr = sh->console_addr + offsetof(struct rte_console, log_le.idx);
Arend van Spriela39be272013-12-12 11:58:58 +01002922 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
2923 (u8 *)&sh_val, sizeof(u32));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002924 if (rv < 0)
2925 return rv;
2926 console_index = le32_to_cpu(sh_val);
2927
2928 /* allocate buffer for console data */
2929 if (console_size <= CONSOLE_BUFFER_MAX)
2930 conbuf = vzalloc(console_size+1);
2931
2932 if (!conbuf)
2933 return -ENOMEM;
2934
2935 /* obtain the console data from device */
2936 conbuf[console_size] = '\0';
Arend van Spriela39be272013-12-12 11:58:58 +01002937 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, console_ptr, (u8 *)conbuf,
2938 console_size);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002939 if (rv < 0)
2940 goto done;
2941
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002942 rv = seq_write(seq, conbuf + console_index,
2943 console_size - console_index);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002944 if (rv < 0)
2945 goto done;
2946
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002947 if (console_index > 0)
2948 rv = seq_write(seq, conbuf, console_index - 1);
2949
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002950done:
2951 vfree(conbuf);
2952 return rv;
2953}
2954
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002955static int brcmf_sdio_trap_info(struct seq_file *seq, struct brcmf_sdio *bus,
2956 struct sdpcm_shared *sh)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002957{
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002958 int error;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002959 struct brcmf_trap_info tr;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002960
Piotr Haberbaa9e602013-04-05 10:57:42 +02002961 if ((sh->flags & SDPCM_SHARED_TRAP) == 0) {
2962 brcmf_dbg(INFO, "no trap in firmware\n");
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002963 return 0;
Piotr Haberbaa9e602013-04-05 10:57:42 +02002964 }
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002965
Arend van Spriela39be272013-12-12 11:58:58 +01002966 error = brcmf_sdiod_ramrw(bus->sdiodev, false, sh->trap_addr, (u8 *)&tr,
2967 sizeof(struct brcmf_trap_info));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002968 if (error < 0)
2969 return error;
2970
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002971 seq_printf(seq,
2972 "dongle trap info: type 0x%x @ epc 0x%08x\n"
2973 " cpsr 0x%08x spsr 0x%08x sp 0x%08x\n"
2974 " lr 0x%08x pc 0x%08x offset 0x%x\n"
2975 " r0 0x%08x r1 0x%08x r2 0x%08x r3 0x%08x\n"
2976 " r4 0x%08x r5 0x%08x r6 0x%08x r7 0x%08x\n",
2977 le32_to_cpu(tr.type), le32_to_cpu(tr.epc),
2978 le32_to_cpu(tr.cpsr), le32_to_cpu(tr.spsr),
2979 le32_to_cpu(tr.r13), le32_to_cpu(tr.r14),
2980 le32_to_cpu(tr.pc), sh->trap_addr,
2981 le32_to_cpu(tr.r0), le32_to_cpu(tr.r1),
2982 le32_to_cpu(tr.r2), le32_to_cpu(tr.r3),
2983 le32_to_cpu(tr.r4), le32_to_cpu(tr.r5),
2984 le32_to_cpu(tr.r6), le32_to_cpu(tr.r7));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002985
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002986 return 0;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002987}
2988
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002989static int brcmf_sdio_assert_info(struct seq_file *seq, struct brcmf_sdio *bus,
2990 struct sdpcm_shared *sh)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002991{
2992 int error = 0;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002993 char file[80] = "?";
2994 char expr[80] = "<???>";
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002995
2996 if ((sh->flags & SDPCM_SHARED_ASSERT_BUILT) == 0) {
2997 brcmf_dbg(INFO, "firmware not built with -assert\n");
2998 return 0;
2999 } else if ((sh->flags & SDPCM_SHARED_ASSERT) == 0) {
3000 brcmf_dbg(INFO, "no assert in dongle\n");
3001 return 0;
3002 }
3003
Franky Lin38b0b0d2012-11-05 16:22:24 -08003004 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003005 if (sh->assert_file_addr != 0) {
Arend van Spriela39be272013-12-12 11:58:58 +01003006 error = brcmf_sdiod_ramrw(bus->sdiodev, false,
3007 sh->assert_file_addr, (u8 *)file, 80);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003008 if (error < 0)
3009 return error;
3010 }
3011 if (sh->assert_exp_addr != 0) {
Arend van Spriela39be272013-12-12 11:58:58 +01003012 error = brcmf_sdiod_ramrw(bus->sdiodev, false,
3013 sh->assert_exp_addr, (u8 *)expr, 80);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003014 if (error < 0)
3015 return error;
3016 }
Franky Lin38b0b0d2012-11-05 16:22:24 -08003017 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003018
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003019 seq_printf(seq, "dongle assert: %s:%d: assert(%s)\n",
3020 file, sh->assert_line, expr);
3021 return 0;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003022}
3023
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003024static int brcmf_sdio_checkdied(struct brcmf_sdio *bus)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003025{
3026 int error;
3027 struct sdpcm_shared sh;
3028
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003029 error = brcmf_sdio_readshared(bus, &sh);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003030
3031 if (error < 0)
3032 return error;
3033
3034 if ((sh.flags & SDPCM_SHARED_ASSERT_BUILT) == 0)
3035 brcmf_dbg(INFO, "firmware not built with -assert\n");
3036 else if (sh.flags & SDPCM_SHARED_ASSERT)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003037 brcmf_err("assertion in dongle\n");
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003038
3039 if (sh.flags & SDPCM_SHARED_TRAP)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003040 brcmf_err("firmware trap in dongle\n");
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003041
3042 return 0;
3043}
3044
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003045static int brcmf_sdio_died_dump(struct seq_file *seq, struct brcmf_sdio *bus)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003046{
3047 int error = 0;
3048 struct sdpcm_shared sh;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003049
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003050 error = brcmf_sdio_readshared(bus, &sh);
3051 if (error < 0)
3052 goto done;
3053
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003054 error = brcmf_sdio_assert_info(seq, bus, &sh);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003055 if (error < 0)
3056 goto done;
Piotr Haberbaa9e602013-04-05 10:57:42 +02003057
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003058 error = brcmf_sdio_trap_info(seq, bus, &sh);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003059 if (error < 0)
3060 goto done;
3061
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003062 error = brcmf_sdio_dump_console(seq, bus, &sh);
Piotr Haberbaa9e602013-04-05 10:57:42 +02003063
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003064done:
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003065 return error;
3066}
3067
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003068static int brcmf_sdio_forensic_read(struct seq_file *seq, void *data)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003069{
Arend van Spriel82d957e2014-07-12 08:49:36 +02003070 struct brcmf_bus *bus_if = dev_get_drvdata(seq->private);
3071 struct brcmf_sdio *bus = bus_if->bus_priv.sdio->bus;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003072
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003073 return brcmf_sdio_died_dump(seq, bus);
3074}
3075
Arend van Spriel82d957e2014-07-12 08:49:36 +02003076static int brcmf_debugfs_sdio_count_read(struct seq_file *seq, void *data)
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003077{
Arend van Spriel82d957e2014-07-12 08:49:36 +02003078 struct brcmf_bus *bus_if = dev_get_drvdata(seq->private);
3079 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3080 struct brcmf_sdio_count *sdcnt = &sdiodev->bus->sdcnt;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003081
Arend van Spriel82d957e2014-07-12 08:49:36 +02003082 seq_printf(seq,
3083 "intrcount: %u\nlastintrs: %u\n"
3084 "pollcnt: %u\nregfails: %u\n"
3085 "tx_sderrs: %u\nfcqueued: %u\n"
3086 "rxrtx: %u\nrx_toolong: %u\n"
3087 "rxc_errors: %u\nrx_hdrfail: %u\n"
3088 "rx_badhdr: %u\nrx_badseq: %u\n"
3089 "fc_rcvd: %u\nfc_xoff: %u\n"
3090 "fc_xon: %u\nrxglomfail: %u\n"
3091 "rxglomframes: %u\nrxglompkts: %u\n"
3092 "f2rxhdrs: %u\nf2rxdata: %u\n"
3093 "f2txdata: %u\nf1regdata: %u\n"
3094 "tickcnt: %u\ntx_ctlerrs: %lu\n"
3095 "tx_ctlpkts: %lu\nrx_ctlerrs: %lu\n"
3096 "rx_ctlpkts: %lu\nrx_readahead: %lu\n",
3097 sdcnt->intrcount, sdcnt->lastintrs,
3098 sdcnt->pollcnt, sdcnt->regfails,
3099 sdcnt->tx_sderrs, sdcnt->fcqueued,
3100 sdcnt->rxrtx, sdcnt->rx_toolong,
3101 sdcnt->rxc_errors, sdcnt->rx_hdrfail,
3102 sdcnt->rx_badhdr, sdcnt->rx_badseq,
3103 sdcnt->fc_rcvd, sdcnt->fc_xoff,
3104 sdcnt->fc_xon, sdcnt->rxglomfail,
3105 sdcnt->rxglomframes, sdcnt->rxglompkts,
3106 sdcnt->f2rxhdrs, sdcnt->f2rxdata,
3107 sdcnt->f2txdata, sdcnt->f1regdata,
3108 sdcnt->tickcnt, sdcnt->tx_ctlerrs,
3109 sdcnt->tx_ctlpkts, sdcnt->rx_ctlerrs,
3110 sdcnt->rx_ctlpkts, sdcnt->rx_readahead_cnt);
3111
3112 return 0;
3113}
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003114
Arend van Spriel80969832012-06-09 22:51:44 +02003115static void brcmf_sdio_debugfs_create(struct brcmf_sdio *bus)
3116{
3117 struct brcmf_pub *drvr = bus->sdiodev->bus_if->drvr;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003118 struct dentry *dentry = brcmf_debugfs_get_devdir(drvr);
Arend van Spriel80969832012-06-09 22:51:44 +02003119
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003120 if (IS_ERR_OR_NULL(dentry))
3121 return;
3122
Arend van Spriel9d6c1dc2015-08-26 22:15:01 +02003123 bus->console_interval = BRCMF_CONSOLE;
3124
Arend van Spriel82d957e2014-07-12 08:49:36 +02003125 brcmf_debugfs_add_entry(drvr, "forensics", brcmf_sdio_forensic_read);
3126 brcmf_debugfs_add_entry(drvr, "counters",
3127 brcmf_debugfs_sdio_count_read);
Daniel Kim0801e6c2014-01-29 15:32:13 +01003128 debugfs_create_u32("console_interval", 0644, dentry,
3129 &bus->console_interval);
Arend van Spriel80969832012-06-09 22:51:44 +02003130}
3131#else
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003132static int brcmf_sdio_checkdied(struct brcmf_sdio *bus)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003133{
3134 return 0;
3135}
3136
Arend van Spriel80969832012-06-09 22:51:44 +02003137static void brcmf_sdio_debugfs_create(struct brcmf_sdio *bus)
3138{
3139}
3140#endif /* DEBUG */
3141
Franky Linfcf094f2011-12-16 18:37:16 -08003142static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003143brcmf_sdio_bus_rxctl(struct device *dev, unsigned char *msg, uint msglen)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003144{
3145 int timeleft;
3146 uint rxlen = 0;
3147 bool pending;
Franky Lindd43a012012-11-05 16:22:22 -08003148 u8 *buf;
Franky Lin532cdd32011-11-22 17:21:54 -08003149 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01003150 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Lin532cdd32011-11-22 17:21:54 -08003151 struct brcmf_sdio *bus = sdiodev->bus;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003152
3153 brcmf_dbg(TRACE, "Enter\n");
Arend van Spriel5768f312015-06-11 00:12:20 +02003154 if (sdiodev->state != BRCMF_SDIOD_DATA)
3155 return -EIO;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003156
3157 /* Wait until control frame is available */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003158 timeleft = brcmf_sdio_dcmd_resp_wait(bus, &bus->rxlen, &pending);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003159
Franky Lindd43a012012-11-05 16:22:22 -08003160 spin_lock_bh(&bus->rxctl_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003161 rxlen = bus->rxlen;
3162 memcpy(msg, bus->rxctl, min(msglen, rxlen));
Franky Lindd43a012012-11-05 16:22:22 -08003163 bus->rxctl = NULL;
3164 buf = bus->rxctl_orig;
3165 bus->rxctl_orig = NULL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003166 bus->rxlen = 0;
Franky Lindd43a012012-11-05 16:22:22 -08003167 spin_unlock_bh(&bus->rxctl_lock);
3168 vfree(buf);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003169
3170 if (rxlen) {
3171 brcmf_dbg(CTL, "resumed on rxctl frame, got %d expected %d\n",
3172 rxlen, msglen);
3173 } else if (timeleft == 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003174 brcmf_err("resumed on timeout\n");
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003175 brcmf_sdio_checkdied(bus);
Joe Perches23677ce2012-02-09 11:17:23 +00003176 } else if (pending) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003177 brcmf_dbg(CTL, "cancelled\n");
3178 return -ERESTARTSYS;
3179 } else {
3180 brcmf_dbg(CTL, "resumed for unknown reason?\n");
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003181 brcmf_sdio_checkdied(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003182 }
3183
3184 if (rxlen)
Arend van Spriel80969832012-06-09 22:51:44 +02003185 bus->sdcnt.rx_ctlpkts++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003186 else
Arend van Spriel80969832012-06-09 22:51:44 +02003187 bus->sdcnt.rx_ctlerrs++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003188
3189 return rxlen ? (int)rxlen : -ETIMEDOUT;
3190}
3191
Hante Meulemana74d0362014-01-13 22:20:22 +01003192#ifdef DEBUG
3193static bool
3194brcmf_sdio_verifymemory(struct brcmf_sdio_dev *sdiodev, u32 ram_addr,
3195 u8 *ram_data, uint ram_sz)
3196{
3197 char *ram_cmp;
3198 int err;
3199 bool ret = true;
3200 int address;
3201 int offset;
3202 int len;
3203
3204 /* read back and verify */
3205 brcmf_dbg(INFO, "Compare RAM dl & ul at 0x%08x; size=%d\n", ram_addr,
3206 ram_sz);
3207 ram_cmp = kmalloc(MEMBLOCK, GFP_KERNEL);
3208 /* do not proceed while no memory but */
3209 if (!ram_cmp)
3210 return true;
3211
3212 address = ram_addr;
3213 offset = 0;
3214 while (offset < ram_sz) {
3215 len = ((offset + MEMBLOCK) < ram_sz) ? MEMBLOCK :
3216 ram_sz - offset;
3217 err = brcmf_sdiod_ramrw(sdiodev, false, address, ram_cmp, len);
3218 if (err) {
3219 brcmf_err("error %d on reading %d membytes at 0x%08x\n",
3220 err, len, address);
3221 ret = false;
3222 break;
3223 } else if (memcmp(ram_cmp, &ram_data[offset], len)) {
3224 brcmf_err("Downloaded RAM image is corrupted, block offset is %d, len is %d\n",
3225 offset, len);
3226 ret = false;
3227 break;
3228 }
3229 offset += len;
3230 address += len;
3231 }
3232
3233 kfree(ram_cmp);
3234
3235 return ret;
3236}
3237#else /* DEBUG */
3238static bool
3239brcmf_sdio_verifymemory(struct brcmf_sdio_dev *sdiodev, u32 ram_addr,
3240 u8 *ram_data, uint ram_sz)
3241{
3242 return true;
3243}
3244#endif /* DEBUG */
3245
Arend van Spriel33556502014-01-13 22:20:25 +01003246static int brcmf_sdio_download_code_file(struct brcmf_sdio *bus,
3247 const struct firmware *fw)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003248{
Hante Meulemanf2c44fe2013-09-25 13:05:45 +02003249 int err;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003250
Hante Meulemana74d0362014-01-13 22:20:22 +01003251 brcmf_dbg(TRACE, "Enter\n");
3252
Hante Meulemanf9951c12014-01-29 15:32:17 +01003253 err = brcmf_sdiod_ramrw(bus->sdiodev, true, bus->ci->rambase,
3254 (u8 *)fw->data, fw->size);
3255 if (err)
3256 brcmf_err("error %d on writing %d membytes at 0x%08x\n",
3257 err, (int)fw->size, bus->ci->rambase);
3258 else if (!brcmf_sdio_verifymemory(bus->sdiodev, bus->ci->rambase,
3259 (u8 *)fw->data, fw->size))
3260 err = -EIO;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003261
Hante Meulemanf2c44fe2013-09-25 13:05:45 +02003262 return err;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003263}
3264
Arend van Spriel33556502014-01-13 22:20:25 +01003265static int brcmf_sdio_download_nvram(struct brcmf_sdio *bus,
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003266 void *vars, u32 varsz)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003267{
Hante Meulemana74d0362014-01-13 22:20:22 +01003268 int address;
3269 int err;
3270
3271 brcmf_dbg(TRACE, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003272
Hante Meulemana74d0362014-01-13 22:20:22 +01003273 address = bus->ci->ramsize - varsz + bus->ci->rambase;
3274 err = brcmf_sdiod_ramrw(bus->sdiodev, true, address, vars, varsz);
3275 if (err)
3276 brcmf_err("error %d on writing %d nvram bytes at 0x%08x\n",
3277 err, varsz, address);
3278 else if (!brcmf_sdio_verifymemory(bus->sdiodev, address, vars, varsz))
3279 err = -EIO;
3280
Hante Meulemana74d0362014-01-13 22:20:22 +01003281 return err;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003282}
3283
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003284static int brcmf_sdio_download_firmware(struct brcmf_sdio *bus,
3285 const struct firmware *fw,
3286 void *nvram, u32 nvlen)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003287{
Markus Elfring9e129042016-03-18 13:23:24 +11003288 int bcmerror;
Arend van Spriel33556502014-01-13 22:20:25 +01003289 u32 rstvec;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003290
3291 sdio_claim_host(bus->sdiodev->func[1]);
3292 brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003293
Arend van Spriel33556502014-01-13 22:20:25 +01003294 rstvec = get_unaligned_le32(fw->data);
3295 brcmf_dbg(SDIO, "firmware rstvec: %x\n", rstvec);
3296
3297 bcmerror = brcmf_sdio_download_code_file(bus, fw);
3298 release_firmware(fw);
3299 if (bcmerror) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003300 brcmf_err("dongle image file download failed\n");
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003301 brcmf_fw_nvram_free(nvram);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003302 goto err;
3303 }
3304
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003305 bcmerror = brcmf_sdio_download_nvram(bus, nvram, nvlen);
3306 brcmf_fw_nvram_free(nvram);
Arend van Spriel33556502014-01-13 22:20:25 +01003307 if (bcmerror) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003308 brcmf_err("dongle nvram file download failed\n");
Franky Lin3eaa9562013-04-02 21:06:20 +02003309 goto err;
3310 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02003311
3312 /* Take arm out of reset */
Arend van Sprield380ebc2015-03-11 16:11:29 +01003313 if (!brcmf_chip_set_active(bus->ci, rstvec)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003314 brcmf_err("error getting out of ARM core reset\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003315 goto err;
3316 }
3317
Arend van Spriel5b435de2011-10-05 13:19:03 +02003318err:
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003319 brcmf_sdio_clkctl(bus, CLK_SDONLY, false);
3320 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003321 return bcmerror;
3322}
3323
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003324static void brcmf_sdio_sr_init(struct brcmf_sdio *bus)
Piotr Haber4a3da992013-04-11 13:28:46 +02003325{
3326 int err = 0;
3327 u8 val;
3328
3329 brcmf_dbg(TRACE, "Enter\n");
3330
Arend van Spriela39be272013-12-12 11:58:58 +01003331 val = brcmf_sdiod_regrb(bus->sdiodev, SBSDIO_FUNC1_WAKEUPCTRL, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003332 if (err) {
3333 brcmf_err("error reading SBSDIO_FUNC1_WAKEUPCTRL\n");
3334 return;
3335 }
3336
3337 val |= 1 << SBSDIO_FUNC1_WCTRL_HTWAIT_SHIFT;
Arend van Spriela39be272013-12-12 11:58:58 +01003338 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_WAKEUPCTRL, val, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003339 if (err) {
3340 brcmf_err("error writing SBSDIO_FUNC1_WAKEUPCTRL\n");
3341 return;
3342 }
3343
3344 /* Add CMD14 Support */
Arend van Spriela39be272013-12-12 11:58:58 +01003345 brcmf_sdiod_regwb(bus->sdiodev, SDIO_CCCR_BRCM_CARDCAP,
3346 (SDIO_CCCR_BRCM_CARDCAP_CMD14_SUPPORT |
3347 SDIO_CCCR_BRCM_CARDCAP_CMD14_EXT),
3348 &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003349 if (err) {
3350 brcmf_err("error writing SDIO_CCCR_BRCM_CARDCAP\n");
3351 return;
3352 }
3353
Arend van Spriela39be272013-12-12 11:58:58 +01003354 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3355 SBSDIO_FORCE_HT, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003356 if (err) {
3357 brcmf_err("error writing SBSDIO_FUNC1_CHIPCLKCSR\n");
3358 return;
3359 }
3360
3361 /* set flag */
3362 bus->sr_enabled = true;
3363 brcmf_dbg(INFO, "SR enabled\n");
3364}
3365
3366/* enable KSO bit */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003367static int brcmf_sdio_kso_init(struct brcmf_sdio *bus)
Piotr Haber4a3da992013-04-11 13:28:46 +02003368{
3369 u8 val;
3370 int err = 0;
3371
3372 brcmf_dbg(TRACE, "Enter\n");
3373
3374 /* KSO bit added in SDIO core rev 12 */
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003375 if (brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV)->rev < 12)
Piotr Haber4a3da992013-04-11 13:28:46 +02003376 return 0;
3377
Arend van Spriela39be272013-12-12 11:58:58 +01003378 val = brcmf_sdiod_regrb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003379 if (err) {
3380 brcmf_err("error reading SBSDIO_FUNC1_SLEEPCSR\n");
3381 return err;
3382 }
3383
3384 if (!(val & SBSDIO_FUNC1_SLEEPCSR_KSO_MASK)) {
3385 val |= (SBSDIO_FUNC1_SLEEPCSR_KSO_EN <<
3386 SBSDIO_FUNC1_SLEEPCSR_KSO_SHIFT);
Arend van Spriela39be272013-12-12 11:58:58 +01003387 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
3388 val, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003389 if (err) {
3390 brcmf_err("error writing SBSDIO_FUNC1_SLEEPCSR\n");
3391 return err;
3392 }
3393 }
3394
3395 return 0;
3396}
3397
3398
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003399static int brcmf_sdio_bus_preinit(struct device *dev)
Arend van Sprielcf458282013-11-29 11:48:15 +01003400{
3401 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3402 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3403 struct brcmf_sdio *bus = sdiodev->bus;
Franky Lin8da9d2c2013-11-29 12:25:10 +01003404 uint pad_size;
Arend van Sprielcf458282013-11-29 11:48:15 +01003405 u32 value;
Arend van Sprielcf458282013-11-29 11:48:15 +01003406 int err;
3407
Franky Lin8da9d2c2013-11-29 12:25:10 +01003408 /* the commands below use the terms tx and rx from
3409 * a device perspective, ie. bus:txglom affects the
3410 * bus transfers from device to host.
3411 */
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003412 if (brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV)->rev < 12) {
Arend van Sprielcf458282013-11-29 11:48:15 +01003413 /* for sdio core rev < 12, disable txgloming */
3414 value = 0;
3415 err = brcmf_iovar_data_set(dev, "bus:txglom", &value,
3416 sizeof(u32));
3417 } else {
3418 /* otherwise, set txglomalign */
Hante Meulemanaf5b5e62016-02-17 11:27:08 +01003419 value = sdiodev->settings->bus.sdio.sd_sgentry_align;
Arend van Sprielcf458282013-11-29 11:48:15 +01003420 /* SDIO ADMA requires at least 32 bit alignment */
3421 value = max_t(u32, value, 4);
3422 err = brcmf_iovar_data_set(dev, "bus:txglomalign", &value,
3423 sizeof(u32));
3424 }
Franky Lin8da9d2c2013-11-29 12:25:10 +01003425
3426 if (err < 0)
3427 goto done;
3428
3429 bus->tx_hdrlen = SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN;
3430 if (sdiodev->sg_support) {
3431 bus->txglom = false;
3432 value = 1;
3433 pad_size = bus->sdiodev->func[2]->cur_blksize << 1;
Franky Lin8da9d2c2013-11-29 12:25:10 +01003434 err = brcmf_iovar_data_set(bus->sdiodev->dev, "bus:rxglom",
3435 &value, sizeof(u32));
3436 if (err < 0) {
3437 /* bus:rxglom is allowed to fail */
3438 err = 0;
3439 } else {
3440 bus->txglom = true;
3441 bus->tx_hdrlen += SDPCM_HWEXT_LEN;
3442 }
3443 }
3444 brcmf_bus_add_txhdrlen(bus->sdiodev->dev, bus->tx_hdrlen);
3445
3446done:
Arend van Sprielcf458282013-11-29 11:48:15 +01003447 return err;
3448}
3449
Arend van Sprielff4445a2015-10-08 20:33:11 +02003450static size_t brcmf_sdio_bus_get_ramsize(struct device *dev)
3451{
3452 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3453 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3454 struct brcmf_sdio *bus = sdiodev->bus;
3455
3456 return bus->ci->ramsize - bus->ci->srsize;
3457}
3458
3459static int brcmf_sdio_bus_get_memdump(struct device *dev, void *data,
3460 size_t mem_size)
3461{
3462 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3463 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3464 struct brcmf_sdio *bus = sdiodev->bus;
3465 int err;
3466 int address;
3467 int offset;
3468 int len;
3469
3470 brcmf_dbg(INFO, "dump at 0x%08x: size=%zu\n", bus->ci->rambase,
3471 mem_size);
3472
3473 address = bus->ci->rambase;
3474 offset = err = 0;
3475 sdio_claim_host(sdiodev->func[1]);
3476 while (offset < mem_size) {
3477 len = ((offset + MEMBLOCK) < mem_size) ? MEMBLOCK :
3478 mem_size - offset;
3479 err = brcmf_sdiod_ramrw(sdiodev, false, address, data, len);
3480 if (err) {
3481 brcmf_err("error %d on reading %d membytes at 0x%08x\n",
3482 err, len, address);
3483 goto done;
3484 }
3485 data += len;
3486 offset += len;
3487 address += len;
3488 }
3489
3490done:
3491 sdio_release_host(sdiodev->func[1]);
3492 return err;
3493}
3494
Arend van Spriel99824642015-02-06 18:36:45 +01003495void brcmf_sdio_trigger_dpc(struct brcmf_sdio *bus)
3496{
Hante Meuleman2c64e162015-03-18 13:25:22 +01003497 if (!bus->dpc_triggered) {
3498 bus->dpc_triggered = true;
Arend van Spriel99824642015-02-06 18:36:45 +01003499 queue_work(bus->brcmf_wq, &bus->datawork);
3500 }
3501}
3502
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003503void brcmf_sdio_isr(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003504{
Arend van Spriel5b435de2011-10-05 13:19:03 +02003505 brcmf_dbg(TRACE, "Enter\n");
3506
3507 if (!bus) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003508 brcmf_err("bus is null pointer, exiting\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003509 return;
3510 }
3511
Arend van Spriel5b435de2011-10-05 13:19:03 +02003512 /* Count the interrupt call */
Arend van Spriel80969832012-06-09 22:51:44 +02003513 bus->sdcnt.intrcount++;
Franky Lin45316032012-09-13 21:12:03 +02003514 if (in_interrupt())
3515 atomic_set(&bus->ipend, 1);
3516 else
3517 if (brcmf_sdio_intr_rstatus(bus)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003518 brcmf_err("failed backplane access\n");
Franky Lin45316032012-09-13 21:12:03 +02003519 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02003520
Arend van Spriel5b435de2011-10-05 13:19:03 +02003521 /* Disable additional interrupts (is this needed now)? */
3522 if (!bus->intr)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003523 brcmf_err("isr w/o interrupt configured!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003524
Hante Meuleman2c64e162015-03-18 13:25:22 +01003525 bus->dpc_triggered = true;
Franky Linf1e68c22012-09-13 21:12:00 +02003526 queue_work(bus->brcmf_wq, &bus->datawork);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003527}
3528
Hante Meulemanb441ba82015-03-06 18:40:39 +01003529static void brcmf_sdio_bus_watchdog(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003530{
Arend van Spriel5b435de2011-10-05 13:19:03 +02003531 brcmf_dbg(TIMER, "Enter\n");
3532
Arend van Spriel5b435de2011-10-05 13:19:03 +02003533 /* Poll period: check device if appropriate. */
Piotr Haber4a3da992013-04-11 13:28:46 +02003534 if (!bus->sr_enabled &&
3535 bus->poll && (++bus->polltick >= bus->pollrate)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003536 u32 intstatus = 0;
3537
3538 /* Reset poll tick */
3539 bus->polltick = 0;
3540
3541 /* Check device if no interrupts */
Arend van Spriel80969832012-06-09 22:51:44 +02003542 if (!bus->intr ||
3543 (bus->sdcnt.intrcount == bus->sdcnt.lastintrs)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003544
Hante Meuleman2c64e162015-03-18 13:25:22 +01003545 if (!bus->dpc_triggered) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003546 u8 devpend;
Arend van Sprielfccfe932013-06-18 13:29:26 +02003547
Franky Lin38b0b0d2012-11-05 16:22:24 -08003548 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriela39be272013-12-12 11:58:58 +01003549 devpend = brcmf_sdiod_regrb(bus->sdiodev,
3550 SDIO_CCCR_INTx,
3551 NULL);
Franky Lin38b0b0d2012-11-05 16:22:24 -08003552 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel99824642015-02-06 18:36:45 +01003553 intstatus = devpend & (INTR_STATUS_FUNC1 |
3554 INTR_STATUS_FUNC2);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003555 }
3556
3557 /* If there is something, make like the ISR and
3558 schedule the DPC */
3559 if (intstatus) {
Arend van Spriel80969832012-06-09 22:51:44 +02003560 bus->sdcnt.pollcnt++;
Franky Lin1d382272012-09-13 21:11:59 +02003561 atomic_set(&bus->ipend, 1);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003562
Hante Meuleman2c64e162015-03-18 13:25:22 +01003563 bus->dpc_triggered = true;
Franky Linf1e68c22012-09-13 21:12:00 +02003564 queue_work(bus->brcmf_wq, &bus->datawork);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003565 }
3566 }
3567
3568 /* Update interrupt tracking */
Arend van Spriel80969832012-06-09 22:51:44 +02003569 bus->sdcnt.lastintrs = bus->sdcnt.intrcount;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003570 }
Joe Perches8ae74652012-01-15 00:38:38 -08003571#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02003572 /* Poll for console output periodically */
Arend van Spriel9d6c1dc2015-08-26 22:15:01 +02003573 if (bus->sdiodev->state == BRCMF_SDIOD_DATA && BRCMF_FWCON_ON() &&
Franky Lin8d169aa2011-11-22 17:21:52 -08003574 bus->console_interval != 0) {
Arend van Spriel63ce3d52016-01-05 11:05:48 +01003575 bus->console.count += jiffies_to_msecs(BRCMF_WD_POLL);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003576 if (bus->console.count >= bus->console_interval) {
3577 bus->console.count -= bus->console_interval;
Franky Lin38b0b0d2012-11-05 16:22:24 -08003578 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003579 /* Make sure backplane clock is on */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003580 brcmf_sdio_bus_sleep(bus, false, false);
3581 if (brcmf_sdio_readconsole(bus) < 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003582 /* stop on error */
3583 bus->console_interval = 0;
Franky Lin38b0b0d2012-11-05 16:22:24 -08003584 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003585 }
3586 }
Joe Perches8ae74652012-01-15 00:38:38 -08003587#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02003588
3589 /* On idle timeout clear activity flag and/or turn off clock */
Hante Meuleman2c64e162015-03-18 13:25:22 +01003590 if (!bus->dpc_triggered) {
3591 rmb();
3592 if ((!bus->dpc_running) && (bus->idletime > 0) &&
3593 (bus->clkstate == CLK_AVAIL)) {
3594 bus->idlecount++;
3595 if (bus->idlecount > bus->idletime) {
3596 brcmf_dbg(SDIO, "idle\n");
3597 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel4011fc42016-01-05 11:05:47 +01003598 brcmf_sdio_wd_timer(bus, false);
Hante Meuleman2c64e162015-03-18 13:25:22 +01003599 bus->idlecount = 0;
3600 brcmf_sdio_bus_sleep(bus, true, false);
3601 sdio_release_host(bus->sdiodev->func[1]);
3602 }
3603 } else {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003604 bus->idlecount = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003605 }
Hante Meulemanb441ba82015-03-06 18:40:39 +01003606 } else {
3607 bus->idlecount = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003608 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02003609}
3610
Franky Linf1e68c22012-09-13 21:12:00 +02003611static void brcmf_sdio_dataworker(struct work_struct *work)
3612{
3613 struct brcmf_sdio *bus = container_of(work, struct brcmf_sdio,
3614 datawork);
Franky Linf1e68c22012-09-13 21:12:00 +02003615
Hante Meuleman2c64e162015-03-18 13:25:22 +01003616 bus->dpc_running = true;
3617 wmb();
3618 while (ACCESS_ONCE(bus->dpc_triggered)) {
3619 bus->dpc_triggered = false;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003620 brcmf_sdio_dpc(bus);
Hante Meulemanb441ba82015-03-06 18:40:39 +01003621 bus->idlecount = 0;
Franky Linf1e68c22012-09-13 21:12:00 +02003622 }
Hante Meuleman2c64e162015-03-18 13:25:22 +01003623 bus->dpc_running = false;
Arend van Spriel99824642015-02-06 18:36:45 +01003624 if (brcmf_sdiod_freezing(bus->sdiodev)) {
3625 brcmf_sdiod_change_state(bus->sdiodev, BRCMF_SDIOD_DOWN);
3626 brcmf_sdiod_try_freeze(bus->sdiodev);
3627 brcmf_sdiod_change_state(bus->sdiodev, BRCMF_SDIOD_DATA);
3628 }
Franky Linf1e68c22012-09-13 21:12:00 +02003629}
3630
Arend van Spriel65d80d02014-01-29 15:32:14 +01003631static void
3632brcmf_sdio_drivestrengthinit(struct brcmf_sdio_dev *sdiodev,
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003633 struct brcmf_chip *ci, u32 drivestrength)
Arend van Spriel65d80d02014-01-29 15:32:14 +01003634{
3635 const struct sdiod_drive_str *str_tab = NULL;
3636 u32 str_mask;
3637 u32 str_shift;
Arend van Spriel65d80d02014-01-29 15:32:14 +01003638 u32 i;
3639 u32 drivestrength_sel = 0;
3640 u32 cc_data_temp;
3641 u32 addr;
3642
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003643 if (!(ci->cc_caps & CC_CAP_PMU))
Arend van Spriel65d80d02014-01-29 15:32:14 +01003644 return;
3645
3646 switch (SDIOD_DRVSTR_KEY(ci->chip, ci->pmurev)) {
Hante Meuleman5779ae62014-07-12 08:49:34 +02003647 case SDIOD_DRVSTR_KEY(BRCM_CC_4330_CHIP_ID, 12):
Arend van Spriel65d80d02014-01-29 15:32:14 +01003648 str_tab = sdiod_drvstr_tab1_1v8;
3649 str_mask = 0x00003800;
3650 str_shift = 11;
3651 break;
Hante Meuleman5779ae62014-07-12 08:49:34 +02003652 case SDIOD_DRVSTR_KEY(BRCM_CC_4334_CHIP_ID, 17):
Arend van Spriel65d80d02014-01-29 15:32:14 +01003653 str_tab = sdiod_drvstr_tab6_1v8;
3654 str_mask = 0x00001800;
3655 str_shift = 11;
3656 break;
Hante Meuleman5779ae62014-07-12 08:49:34 +02003657 case SDIOD_DRVSTR_KEY(BRCM_CC_43143_CHIP_ID, 17):
Arend van Spriel65d80d02014-01-29 15:32:14 +01003658 /* note: 43143 does not support tristate */
3659 i = ARRAY_SIZE(sdiod_drvstr_tab2_3v3) - 1;
3660 if (drivestrength >= sdiod_drvstr_tab2_3v3[i].strength) {
3661 str_tab = sdiod_drvstr_tab2_3v3;
3662 str_mask = 0x00000007;
3663 str_shift = 0;
3664 } else
3665 brcmf_err("Invalid SDIO Drive strength for chip %s, strength=%d\n",
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003666 ci->name, drivestrength);
Arend van Spriel65d80d02014-01-29 15:32:14 +01003667 break;
Hante Meuleman5779ae62014-07-12 08:49:34 +02003668 case SDIOD_DRVSTR_KEY(BRCM_CC_43362_CHIP_ID, 13):
Arend van Spriel65d80d02014-01-29 15:32:14 +01003669 str_tab = sdiod_drive_strength_tab5_1v8;
3670 str_mask = 0x00003800;
3671 str_shift = 11;
3672 break;
3673 default:
Wright Fengd922dfa2016-06-03 23:31:08 +02003674 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 +01003675 ci->name, ci->chiprev, ci->pmurev);
Arend van Spriel65d80d02014-01-29 15:32:14 +01003676 break;
3677 }
3678
3679 if (str_tab != NULL) {
Rafał Miłeckie2b397f2016-01-26 17:57:04 +01003680 struct brcmf_core *pmu = brcmf_chip_get_pmu(ci);
3681
Arend van Spriel65d80d02014-01-29 15:32:14 +01003682 for (i = 0; str_tab[i].strength != 0; i++) {
3683 if (drivestrength >= str_tab[i].strength) {
3684 drivestrength_sel = str_tab[i].sel;
3685 break;
3686 }
3687 }
Rafał Miłeckie2b397f2016-01-26 17:57:04 +01003688 addr = CORE_CC_REG(pmu->base, chipcontrol_addr);
Arend van Spriel65d80d02014-01-29 15:32:14 +01003689 brcmf_sdiod_regwl(sdiodev, addr, 1, NULL);
3690 cc_data_temp = brcmf_sdiod_regrl(sdiodev, addr, NULL);
3691 cc_data_temp &= ~str_mask;
3692 drivestrength_sel <<= str_shift;
3693 cc_data_temp |= drivestrength_sel;
3694 brcmf_sdiod_regwl(sdiodev, addr, cc_data_temp, NULL);
3695
3696 brcmf_dbg(INFO, "SDIO: %d mA (req=%d mA) drive strength selected, set to 0x%08x\n",
3697 str_tab[i].strength, drivestrength, cc_data_temp);
3698 }
3699}
3700
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003701static int brcmf_sdio_buscoreprep(void *ctx)
Arend van Spriel65d80d02014-01-29 15:32:14 +01003702{
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003703 struct brcmf_sdio_dev *sdiodev = ctx;
Arend van Spriel65d80d02014-01-29 15:32:14 +01003704 int err = 0;
3705 u8 clkval, clkset;
3706
3707 /* Try forcing SDIO core to do ALPAvail request only */
3708 clkset = SBSDIO_FORCE_HW_CLKREQ_OFF | SBSDIO_ALP_AVAIL_REQ;
3709 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, clkset, &err);
3710 if (err) {
3711 brcmf_err("error writing for HT off\n");
3712 return err;
3713 }
3714
3715 /* If register supported, wait for ALPAvail and then force ALP */
3716 /* This may take up to 15 milliseconds */
3717 clkval = brcmf_sdiod_regrb(sdiodev,
3718 SBSDIO_FUNC1_CHIPCLKCSR, NULL);
3719
3720 if ((clkval & ~SBSDIO_AVBITS) != clkset) {
3721 brcmf_err("ChipClkCSR access: wrote 0x%02x read 0x%02x\n",
3722 clkset, clkval);
3723 return -EACCES;
3724 }
3725
3726 SPINWAIT(((clkval = brcmf_sdiod_regrb(sdiodev,
3727 SBSDIO_FUNC1_CHIPCLKCSR, NULL)),
3728 !SBSDIO_ALPAV(clkval)),
3729 PMU_MAX_TRANSITION_DLY);
3730 if (!SBSDIO_ALPAV(clkval)) {
3731 brcmf_err("timeout on ALPAV wait, clkval 0x%02x\n",
3732 clkval);
3733 return -EBUSY;
3734 }
3735
3736 clkset = SBSDIO_FORCE_HW_CLKREQ_OFF | SBSDIO_FORCE_ALP;
3737 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, clkset, &err);
3738 udelay(65);
3739
3740 /* Also, disable the extra SDIO pull-ups */
3741 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_SDIOPULLUP, 0, NULL);
3742
3743 return 0;
3744}
3745
Arend van Sprield380ebc2015-03-11 16:11:29 +01003746static void brcmf_sdio_buscore_activate(void *ctx, struct brcmf_chip *chip,
3747 u32 rstvec)
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003748{
3749 struct brcmf_sdio_dev *sdiodev = ctx;
3750 struct brcmf_core *core;
3751 u32 reg_addr;
3752
3753 /* clear all interrupts */
3754 core = brcmf_chip_get_core(chip, BCMA_CORE_SDIO_DEV);
3755 reg_addr = core->base + offsetof(struct sdpcmd_regs, intstatus);
3756 brcmf_sdiod_regwl(sdiodev, reg_addr, 0xFFFFFFFF, NULL);
3757
3758 if (rstvec)
3759 /* Write reset vector to address 0 */
3760 brcmf_sdiod_ramrw(sdiodev, true, 0, (void *)&rstvec,
3761 sizeof(rstvec));
3762}
3763
3764static u32 brcmf_sdio_buscore_read32(void *ctx, u32 addr)
3765{
3766 struct brcmf_sdio_dev *sdiodev = ctx;
3767 u32 val, rev;
3768
3769 val = brcmf_sdiod_regrl(sdiodev, addr, NULL);
Arend Van Spriel634faf32016-09-05 11:42:12 +01003770 if ((sdiodev->func[0]->device == SDIO_DEVICE_ID_BROADCOM_4335_4339 ||
3771 sdiodev->func[0]->device == SDIO_DEVICE_ID_BROADCOM_4339) &&
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003772 addr == CORE_CC_REG(SI_ENUM_BASE, chipid)) {
3773 rev = (val & CID_REV_MASK) >> CID_REV_SHIFT;
3774 if (rev >= 2) {
3775 val &= ~CID_ID_MASK;
Hante Meuleman5779ae62014-07-12 08:49:34 +02003776 val |= BRCM_CC_4339_CHIP_ID;
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003777 }
3778 }
3779 return val;
3780}
3781
3782static void brcmf_sdio_buscore_write32(void *ctx, u32 addr, u32 val)
3783{
3784 struct brcmf_sdio_dev *sdiodev = ctx;
3785
3786 brcmf_sdiod_regwl(sdiodev, addr, val, NULL);
3787}
3788
3789static const struct brcmf_buscore_ops brcmf_sdio_buscore_ops = {
3790 .prepare = brcmf_sdio_buscoreprep,
Arend van Sprield380ebc2015-03-11 16:11:29 +01003791 .activate = brcmf_sdio_buscore_activate,
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003792 .read32 = brcmf_sdio_buscore_read32,
3793 .write32 = brcmf_sdio_buscore_write32,
3794};
3795
Arend van Spriel5b435de2011-10-05 13:19:03 +02003796static bool
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003797brcmf_sdio_probe_attach(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003798{
Hante Meuleman4d792892016-02-17 11:27:07 +01003799 struct brcmf_sdio_dev *sdiodev;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003800 u8 clkctl = 0;
3801 int err = 0;
3802 int reg_addr;
3803 u32 reg_val;
Hante Meuleman668761a2013-04-12 10:55:55 +02003804 u32 drivestrength;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003805
Hante Meuleman4d792892016-02-17 11:27:07 +01003806 sdiodev = bus->sdiodev;
3807 sdio_claim_host(sdiodev->func[1]);
Franky Lin38b0b0d2012-11-05 16:22:24 -08003808
Joe Perches18aad4f2012-01-15 00:38:42 -08003809 pr_debug("F1 signature read @0x18000000=0x%4x\n",
Hante Meuleman4d792892016-02-17 11:27:07 +01003810 brcmf_sdiod_regrl(sdiodev, SI_ENUM_BASE, NULL));
Arend van Spriel5b435de2011-10-05 13:19:03 +02003811
3812 /*
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003813 * Force PLL off until brcmf_chip_attach()
Arend van Spriel5b435de2011-10-05 13:19:03 +02003814 * programs PLL control regs
3815 */
3816
Hante Meuleman4d792892016-02-17 11:27:07 +01003817 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
Arend van Spriela39be272013-12-12 11:58:58 +01003818 BRCMF_INIT_CLKCTL1, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003819 if (!err)
Hante Meuleman4d792892016-02-17 11:27:07 +01003820 clkctl = brcmf_sdiod_regrb(sdiodev,
Arend van Spriela39be272013-12-12 11:58:58 +01003821 SBSDIO_FUNC1_CHIPCLKCSR, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003822
3823 if (err || ((clkctl & ~SBSDIO_AVBITS) != BRCMF_INIT_CLKCTL1)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003824 brcmf_err("ChipClkCSR access: err %d wrote 0x%02x read 0x%02x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02003825 err, BRCMF_INIT_CLKCTL1, clkctl);
3826 goto fail;
3827 }
3828
Hante Meuleman4d792892016-02-17 11:27:07 +01003829 bus->ci = brcmf_chip_attach(sdiodev, &brcmf_sdio_buscore_ops);
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003830 if (IS_ERR(bus->ci)) {
3831 brcmf_err("brcmf_chip_attach failed!\n");
3832 bus->ci = NULL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003833 goto fail;
3834 }
Hante Meulemanaf5b5e62016-02-17 11:27:08 +01003835 sdiodev->settings = brcmf_get_module_param(sdiodev->dev,
Hante Meuleman4d792892016-02-17 11:27:07 +01003836 BRCMF_BUSTYPE_SDIO,
3837 bus->ci->chip,
3838 bus->ci->chiprev);
Hante Meulemanaf5b5e62016-02-17 11:27:08 +01003839 if (!sdiodev->settings) {
3840 brcmf_err("Failed to get device parameters\n");
3841 goto fail;
3842 }
Hante Meuleman4d792892016-02-17 11:27:07 +01003843 /* platform specific configuration:
3844 * alignments must be at least 4 bytes for ADMA
3845 */
3846 bus->head_align = ALIGNMENT;
3847 bus->sgentry_align = ALIGNMENT;
Hante Meulemanaf5b5e62016-02-17 11:27:08 +01003848 if (sdiodev->settings->bus.sdio.sd_head_align > ALIGNMENT)
3849 bus->head_align = sdiodev->settings->bus.sdio.sd_head_align;
3850 if (sdiodev->settings->bus.sdio.sd_sgentry_align > ALIGNMENT)
3851 bus->sgentry_align =
3852 sdiodev->settings->bus.sdio.sd_sgentry_align;
3853
Hante Meuleman4d792892016-02-17 11:27:07 +01003854 /* allocate scatter-gather table. sg support
3855 * will be disabled upon allocation failure.
3856 */
3857 brcmf_sdiod_sgtable_alloc(sdiodev);
3858
3859#ifdef CONFIG_PM_SLEEP
3860 /* wowl can be supported when KEEP_POWER is true and (WAKE_SDIO_IRQ
3861 * is true or when platform data OOB irq is true).
3862 */
3863 if ((sdio_get_host_pm_caps(sdiodev->func[1]) & MMC_PM_KEEP_POWER) &&
3864 ((sdio_get_host_pm_caps(sdiodev->func[1]) & MMC_PM_WAKE_SDIO_IRQ) ||
Hante Meulemanaf5b5e62016-02-17 11:27:08 +01003865 (sdiodev->settings->bus.sdio.oob_irq_supported)))
Hante Meuleman4d792892016-02-17 11:27:07 +01003866 sdiodev->bus_if->wowl_supported = true;
3867#endif
Arend van Spriel5b435de2011-10-05 13:19:03 +02003868
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003869 if (brcmf_sdio_kso_init(bus)) {
Piotr Haber4a3da992013-04-11 13:28:46 +02003870 brcmf_err("error enabling KSO\n");
3871 goto fail;
3872 }
3873
Hante Meulemanaf5b5e62016-02-17 11:27:08 +01003874 if (sdiodev->settings->bus.sdio.drive_strength)
3875 drivestrength = sdiodev->settings->bus.sdio.drive_strength;
Hante Meuleman668761a2013-04-12 10:55:55 +02003876 else
3877 drivestrength = DEFAULT_SDIO_DRIVE_STRENGTH;
Hante Meuleman4d792892016-02-17 11:27:07 +01003878 brcmf_sdio_drivestrengthinit(sdiodev, bus->ci, drivestrength);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003879
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003880 /* Set card control so an SDIO card reset does a WLAN backplane reset */
Hante Meuleman4d792892016-02-17 11:27:07 +01003881 reg_val = brcmf_sdiod_regrb(sdiodev, SDIO_CCCR_BRCM_CARDCTRL, &err);
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003882 if (err)
3883 goto fail;
3884
3885 reg_val |= SDIO_CCCR_BRCM_CARDCTRL_WLANRESET;
3886
Hante Meuleman4d792892016-02-17 11:27:07 +01003887 brcmf_sdiod_regwb(sdiodev, SDIO_CCCR_BRCM_CARDCTRL, reg_val, &err);
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003888 if (err)
3889 goto fail;
3890
3891 /* set PMUControl so a backplane reset does PMU state reload */
Rafał Miłeckie2b397f2016-01-26 17:57:04 +01003892 reg_addr = CORE_CC_REG(brcmf_chip_get_pmu(bus->ci)->base, pmucontrol);
Hante Meuleman4d792892016-02-17 11:27:07 +01003893 reg_val = brcmf_sdiod_regrl(sdiodev, reg_addr, &err);
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003894 if (err)
3895 goto fail;
3896
3897 reg_val |= (BCMA_CC_PMU_CTL_RES_RELOAD << BCMA_CC_PMU_CTL_RES_SHIFT);
3898
Hante Meuleman4d792892016-02-17 11:27:07 +01003899 brcmf_sdiod_regwl(sdiodev, reg_addr, reg_val, &err);
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003900 if (err)
3901 goto fail;
3902
Hante Meuleman4d792892016-02-17 11:27:07 +01003903 sdio_release_host(sdiodev->func[1]);
Franky Lin38b0b0d2012-11-05 16:22:24 -08003904
Arend van Spriel5b435de2011-10-05 13:19:03 +02003905 brcmu_pktq_init(&bus->txq, (PRIOMASK + 1), TXQLEN);
3906
Arend van Spriel9b2d2f2a82013-11-29 12:25:14 +01003907 /* allocate header buffer */
3908 bus->hdrbuf = kzalloc(MAX_HDR_READ + bus->head_align, GFP_KERNEL);
3909 if (!bus->hdrbuf)
3910 return false;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003911 /* Locate an appropriately-aligned portion of hdrbuf */
3912 bus->rxhdr = (u8 *) roundup((unsigned long)&bus->hdrbuf[0],
Arend van Spriel9b2d2f2a82013-11-29 12:25:14 +01003913 bus->head_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003914
3915 /* Set the poll and/or interrupt flags */
3916 bus->intr = true;
3917 bus->poll = false;
3918 if (bus->poll)
3919 bus->pollrate = 1;
3920
3921 return true;
3922
3923fail:
Hante Meuleman4d792892016-02-17 11:27:07 +01003924 sdio_release_host(sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003925 return false;
3926}
3927
Arend van Spriel5b435de2011-10-05 13:19:03 +02003928static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003929brcmf_sdio_watchdog_thread(void *data)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003930{
Franky Line92eedf2011-11-22 17:21:50 -08003931 struct brcmf_sdio *bus = (struct brcmf_sdio *)data;
Arend van Spriel99824642015-02-06 18:36:45 +01003932 int wait;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003933
3934 allow_signal(SIGTERM);
3935 /* Run until signal received */
Arend van Spriel99824642015-02-06 18:36:45 +01003936 brcmf_sdiod_freezer_count(bus->sdiodev);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003937 while (1) {
3938 if (kthread_should_stop())
3939 break;
Arend van Spriel99824642015-02-06 18:36:45 +01003940 brcmf_sdiod_freezer_uncount(bus->sdiodev);
3941 wait = wait_for_completion_interruptible(&bus->watchdog_wait);
3942 brcmf_sdiod_freezer_count(bus->sdiodev);
3943 brcmf_sdiod_try_freeze(bus->sdiodev);
3944 if (!wait) {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003945 brcmf_sdio_bus_watchdog(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003946 /* Count the tick for reference */
Arend van Spriel80969832012-06-09 22:51:44 +02003947 bus->sdcnt.tickcnt++;
Arend van Spriel58e9df42014-03-20 10:18:01 +01003948 reinit_completion(&bus->watchdog_wait);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003949 } else
3950 break;
3951 }
3952 return 0;
3953}
3954
3955static void
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003956brcmf_sdio_watchdog(unsigned long data)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003957{
Franky Line92eedf2011-11-22 17:21:50 -08003958 struct brcmf_sdio *bus = (struct brcmf_sdio *)data;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003959
3960 if (bus->watchdog_tsk) {
3961 complete(&bus->watchdog_wait);
3962 /* Reschedule the watchdog */
Arend van Spriel4011fc42016-01-05 11:05:47 +01003963 if (bus->wd_active)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003964 mod_timer(&bus->timer,
Arend van Spriel63ce3d52016-01-05 11:05:48 +01003965 jiffies + BRCMF_WD_POLL);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003966 }
3967}
3968
Julia Lawall6866a642015-11-14 17:22:07 +01003969static const struct brcmf_bus_ops brcmf_sdio_bus_ops = {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003970 .stop = brcmf_sdio_bus_stop,
3971 .preinit = brcmf_sdio_bus_preinit,
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003972 .txdata = brcmf_sdio_bus_txdata,
3973 .txctl = brcmf_sdio_bus_txctl,
3974 .rxctl = brcmf_sdio_bus_rxctl,
3975 .gettxq = brcmf_sdio_bus_gettxq,
Arend van Sprielff4445a2015-10-08 20:33:11 +02003976 .wowl_config = brcmf_sdio_wowl_config,
3977 .get_ramsize = brcmf_sdio_bus_get_ramsize,
3978 .get_memdump = brcmf_sdio_bus_get_memdump,
Arend van Sprield9cb2592012-12-05 15:25:54 +01003979};
3980
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003981static void brcmf_sdio_firmware_callback(struct device *dev,
3982 const struct firmware *code,
3983 void *nvram, u32 nvram_len)
3984{
3985 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3986 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3987 struct brcmf_sdio *bus = sdiodev->bus;
3988 int err = 0;
3989 u8 saveclk;
3990
3991 brcmf_dbg(TRACE, "Enter: dev=%s\n", dev_name(dev));
3992
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003993 if (!bus_if->drvr)
3994 return;
3995
Hante Meulemana1cee862015-01-25 20:31:35 +01003996 /* try to download image and nvram to the dongle */
3997 bus->alp_only = true;
3998 err = brcmf_sdio_download_firmware(bus, code, nvram, nvram_len);
3999 if (err)
4000 goto fail;
4001 bus->alp_only = false;
4002
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02004003 /* Start the watchdog timer */
4004 bus->sdcnt.tickcnt = 0;
Arend van Spriel4011fc42016-01-05 11:05:47 +01004005 brcmf_sdio_wd_timer(bus, true);
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02004006
4007 sdio_claim_host(sdiodev->func[1]);
4008
4009 /* Make sure backplane clock is on, needed to generate F2 interrupt */
4010 brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
4011 if (bus->clkstate != CLK_AVAIL)
4012 goto release;
4013
4014 /* Force clocks on backplane to be sure F2 interrupt propagates */
4015 saveclk = brcmf_sdiod_regrb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, &err);
4016 if (!err) {
4017 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
4018 (saveclk | SBSDIO_FORCE_HT), &err);
4019 }
4020 if (err) {
4021 brcmf_err("Failed to force clock for F2: err %d\n", err);
4022 goto release;
4023 }
4024
4025 /* Enable function 2 (frame transfers) */
4026 w_sdreg32(bus, SDPCM_PROT_VERSION << SMB_DATA_VERSION_SHIFT,
4027 offsetof(struct sdpcmd_regs, tosbmailboxdata));
4028 err = sdio_enable_func(sdiodev->func[SDIO_FUNC_2]);
4029
4030
4031 brcmf_dbg(INFO, "enable F2: err=%d\n", err);
4032
4033 /* If F2 successfully enabled, set core and enable interrupts */
4034 if (!err) {
4035 /* Set up the interrupt mask and enable interrupts */
4036 bus->hostintmask = HOSTINTMASK;
4037 w_sdreg32(bus, bus->hostintmask,
4038 offsetof(struct sdpcmd_regs, hostintmask));
4039
4040 brcmf_sdiod_regwb(sdiodev, SBSDIO_WATERMARK, 8, &err);
4041 } else {
4042 /* Disable F2 again */
4043 sdio_disable_func(sdiodev->func[SDIO_FUNC_2]);
4044 goto release;
4045 }
4046
4047 if (brcmf_chip_sr_capable(bus->ci)) {
4048 brcmf_sdio_sr_init(bus);
4049 } else {
4050 /* Restore previous clock setting */
4051 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
4052 saveclk, &err);
4053 }
4054
4055 if (err == 0) {
Arend Van Sprielfd3ed332016-07-15 12:39:13 +02004056 /* Allow full data communication using DPC from now on. */
4057 brcmf_sdiod_change_state(bus->sdiodev, BRCMF_SDIOD_DATA);
4058
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02004059 err = brcmf_sdiod_intr_register(sdiodev);
4060 if (err != 0)
4061 brcmf_err("intr register failed:%d\n", err);
4062 }
4063
4064 /* If we didn't come up, turn off backplane clock */
4065 if (err != 0)
4066 brcmf_sdio_clkctl(bus, CLK_NONE, false);
4067
4068 sdio_release_host(sdiodev->func[1]);
4069
Rafał Miłeckie8cd4752017-01-18 11:48:54 +01004070 err = brcmf_bus_started(dev);
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02004071 if (err != 0) {
4072 brcmf_err("dongle is not responding\n");
4073 goto fail;
4074 }
4075 return;
4076
4077release:
4078 sdio_release_host(sdiodev->func[1]);
4079fail:
4080 brcmf_dbg(TRACE, "failed: dev=%s, err=%d\n", dev_name(dev), err);
4081 device_release_driver(dev);
4082}
4083
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01004084struct brcmf_sdio *brcmf_sdio_probe(struct brcmf_sdio_dev *sdiodev)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004085{
4086 int ret;
Franky Line92eedf2011-11-22 17:21:50 -08004087 struct brcmf_sdio *bus;
Arend van Spriel99824642015-02-06 18:36:45 +01004088 struct workqueue_struct *wq;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004089
Arend van Spriel5b435de2011-10-05 13:19:03 +02004090 brcmf_dbg(TRACE, "Enter\n");
4091
Arend van Spriel5b435de2011-10-05 13:19:03 +02004092 /* Allocate private bus interface state */
Franky Line92eedf2011-11-22 17:21:50 -08004093 bus = kzalloc(sizeof(struct brcmf_sdio), GFP_ATOMIC);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004094 if (!bus)
4095 goto fail;
4096
4097 bus->sdiodev = sdiodev;
4098 sdiodev->bus = bus;
Arend van Sprielb83db862011-10-19 12:51:09 +02004099 skb_queue_head_init(&bus->glom);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004100 bus->txbound = BRCMF_TXBOUND;
4101 bus->rxbound = BRCMF_RXBOUND;
4102 bus->txminmax = BRCMF_TXMINMAX;
Franky Lin6bc52312013-08-10 12:27:28 +02004103 bus->tx_seq = SDPCM_SEQ_WRAP - 1;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004104
Arend van Spriel99824642015-02-06 18:36:45 +01004105 /* single-threaded workqueue */
4106 wq = alloc_ordered_workqueue("brcmf_wq/%s", WQ_MEM_RECLAIM,
4107 dev_name(&sdiodev->func[1]->dev));
4108 if (!wq) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01004109 brcmf_err("insufficient memory to create txworkqueue\n");
Hante Meuleman37ac5782012-11-14 18:46:18 -08004110 goto fail;
4111 }
Arend van Spriel99824642015-02-06 18:36:45 +01004112 brcmf_sdiod_freezer_count(sdiodev);
4113 INIT_WORK(&bus->datawork, brcmf_sdio_dataworker);
4114 bus->brcmf_wq = wq;
Hante Meuleman37ac5782012-11-14 18:46:18 -08004115
Arend van Spriel5b435de2011-10-05 13:19:03 +02004116 /* attempt to attach to the dongle */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01004117 if (!(brcmf_sdio_probe_attach(bus))) {
4118 brcmf_err("brcmf_sdio_probe_attach failed\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02004119 goto fail;
4120 }
4121
Franky Lindd43a012012-11-05 16:22:22 -08004122 spin_lock_init(&bus->rxctl_lock);
Hante Meulemanfed7ec42014-03-15 17:18:18 +01004123 spin_lock_init(&bus->txq_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004124 init_waitqueue_head(&bus->ctrl_wait);
4125 init_waitqueue_head(&bus->dcmd_resp_wait);
4126
4127 /* Set up the watchdog timer */
4128 init_timer(&bus->timer);
4129 bus->timer.data = (unsigned long)bus;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01004130 bus->timer.function = brcmf_sdio_watchdog;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004131
Arend van Spriel5b435de2011-10-05 13:19:03 +02004132 /* Initialize watchdog thread */
4133 init_completion(&bus->watchdog_wait);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01004134 bus->watchdog_tsk = kthread_run(brcmf_sdio_watchdog_thread,
Arend van Spriel99824642015-02-06 18:36:45 +01004135 bus, "brcmf_wdog/%s",
4136 dev_name(&sdiodev->func[1]->dev));
Arend van Spriel5b435de2011-10-05 13:19:03 +02004137 if (IS_ERR(bus->watchdog_tsk)) {
Joe Perches02f77192012-01-15 00:38:44 -08004138 pr_warn("brcmf_watchdog thread failed to start\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02004139 bus->watchdog_tsk = NULL;
4140 }
4141 /* Initialize DPC thread */
Hante Meuleman2c64e162015-03-18 13:25:22 +01004142 bus->dpc_triggered = false;
4143 bus->dpc_running = false;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004144
Franky Lina9ffda82011-12-16 18:37:12 -08004145 /* Assign bus interface call back */
Arend van Sprield9cb2592012-12-05 15:25:54 +01004146 bus->sdiodev->bus_if->dev = bus->sdiodev->dev;
4147 bus->sdiodev->bus_if->ops = &brcmf_sdio_bus_ops;
Arend van Spriel75d907d2013-02-06 18:40:45 +01004148 bus->sdiodev->bus_if->chip = bus->ci->chip;
4149 bus->sdiodev->bus_if->chiprev = bus->ci->chiprev;
Arend van Sprield9cb2592012-12-05 15:25:54 +01004150
Franky Lin706478c2013-08-10 12:27:29 +02004151 /* default sdio bus header length for tx packet */
4152 bus->tx_hdrlen = SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN;
4153
4154 /* Attach to the common layer, reserve hdr space */
Hante Meulemanaf5b5e62016-02-17 11:27:08 +01004155 ret = brcmf_attach(bus->sdiodev->dev, bus->sdiodev->settings);
Franky Lin712ac5b2011-12-16 18:37:09 -08004156 if (ret != 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01004157 brcmf_err("brcmf_attach failed\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02004158 goto fail;
4159 }
4160
Hante Meulemane0045bf2016-01-19 12:39:24 +01004161 /* allocate scatter-gather table. sg support
4162 * will be disabled upon allocation failure.
4163 */
4164 brcmf_sdiod_sgtable_alloc(bus->sdiodev);
4165
Daniel Kim7dd3abc2014-05-27 12:56:26 +02004166 /* Query the F2 block size, set roundup accordingly */
4167 bus->blocksize = bus->sdiodev->func[2]->cur_blksize;
4168 bus->roundup = min(max_roundup, bus->blocksize);
4169
Arend van Spriel5b435de2011-10-05 13:19:03 +02004170 /* Allocate buffers */
Arend van Sprielfad13222014-01-06 12:40:40 +01004171 if (bus->sdiodev->bus_if->maxctl) {
Daniel Kim7dd3abc2014-05-27 12:56:26 +02004172 bus->sdiodev->bus_if->maxctl += bus->roundup;
Arend van Sprielfad13222014-01-06 12:40:40 +01004173 bus->rxblen =
4174 roundup((bus->sdiodev->bus_if->maxctl + SDPCM_HDRLEN),
4175 ALIGNMENT) + bus->head_align;
4176 bus->rxbuf = kmalloc(bus->rxblen, GFP_ATOMIC);
4177 if (!(bus->rxbuf)) {
4178 brcmf_err("rxbuf allocation failed\n");
4179 goto fail;
4180 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02004181 }
4182
Arend van Sprielfad13222014-01-06 12:40:40 +01004183 sdio_claim_host(bus->sdiodev->func[1]);
4184
4185 /* Disable F2 to clear any intermediate frame state on the dongle */
4186 sdio_disable_func(bus->sdiodev->func[SDIO_FUNC_2]);
4187
Arend van Sprielfad13222014-01-06 12:40:40 +01004188 bus->rxflow = false;
4189
4190 /* Done with backplane-dependent accesses, can drop clock... */
4191 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, 0, NULL);
4192
4193 sdio_release_host(bus->sdiodev->func[1]);
4194
4195 /* ...and initialize clock/power states */
4196 bus->clkstate = CLK_SDONLY;
4197 bus->idletime = BRCMF_IDLE_INTERVAL;
4198 bus->idleclock = BRCMF_IDLE_ACTIVE;
4199
Arend van Sprielfad13222014-01-06 12:40:40 +01004200 /* SR state */
Arend van Sprielfad13222014-01-06 12:40:40 +01004201 bus->sr_enabled = false;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004202
Arend van Spriel80969832012-06-09 22:51:44 +02004203 brcmf_sdio_debugfs_create(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004204 brcmf_dbg(INFO, "completed!!\n");
4205
Hante Meuleman46d703a2015-11-25 11:32:46 +01004206 ret = brcmf_fw_map_chip_to_name(bus->ci->chip, bus->ci->chiprev,
4207 brcmf_sdio_fwnames,
4208 ARRAY_SIZE(brcmf_sdio_fwnames),
4209 sdiodev->fw_name, sdiodev->nvram_name);
Daniel Kimc1b20532014-07-12 08:49:37 +02004210 if (ret)
4211 goto fail;
4212
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02004213 ret = brcmf_fw_get_firmwares(sdiodev->dev, BRCMF_FW_REQUEST_NVRAM,
Daniel Kimc1b20532014-07-12 08:49:37 +02004214 sdiodev->fw_name, sdiodev->nvram_name,
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02004215 brcmf_sdio_firmware_callback);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004216 if (ret != 0) {
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02004217 brcmf_err("async firmware request failed: %d\n", ret);
Hante Meuleman1799ddf2012-11-14 18:46:17 -08004218 goto fail;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004219 }
Franky Lin15d45b62011-10-21 16:16:32 +02004220
Arend van Spriel5b435de2011-10-05 13:19:03 +02004221 return bus;
4222
4223fail:
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004224 brcmf_sdio_remove(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004225 return NULL;
4226}
4227
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004228/* Detach and free everything */
4229void brcmf_sdio_remove(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004230{
Arend van Spriel5b435de2011-10-05 13:19:03 +02004231 brcmf_dbg(TRACE, "Enter\n");
4232
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004233 if (bus) {
4234 /* De-register interrupt handler */
4235 brcmf_sdiod_intr_unregister(bus->sdiodev);
4236
Arend van Spriel4faf28b2014-05-27 12:56:18 +02004237 brcmf_detach(bus->sdiodev->dev);
Arend van Sprielbfad4a02014-01-06 12:40:39 +01004238
Hante Meulemane0c180ec2014-01-29 15:32:18 +01004239 cancel_work_sync(&bus->datawork);
4240 if (bus->brcmf_wq)
4241 destroy_workqueue(bus->brcmf_wq);
4242
Arend van Sprielbfad4a02014-01-06 12:40:39 +01004243 if (bus->ci) {
Arend van Spriela1ce7a02015-02-06 18:36:42 +01004244 if (bus->sdiodev->state != BRCMF_SDIOD_NOMEDIUM) {
Arend van Sprielbb350712014-01-13 22:20:29 +01004245 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel4011fc42016-01-05 11:05:47 +01004246 brcmf_sdio_wd_timer(bus, false);
Arend van Sprielbb350712014-01-13 22:20:29 +01004247 brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
4248 /* Leave the device in state where it is
Arend van Sprield380ebc2015-03-11 16:11:29 +01004249 * 'passive'. This is done by resetting all
4250 * necessary cores.
Arend van Sprielbb350712014-01-13 22:20:29 +01004251 */
4252 msleep(20);
Arend van Sprield380ebc2015-03-11 16:11:29 +01004253 brcmf_chip_set_passive(bus->ci);
Arend van Sprielbb350712014-01-13 22:20:29 +01004254 brcmf_sdio_clkctl(bus, CLK_NONE, false);
4255 sdio_release_host(bus->sdiodev->func[1]);
4256 }
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01004257 brcmf_chip_detach(bus->ci);
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004258 }
Hante Meulemanaf5b5e62016-02-17 11:27:08 +01004259 if (bus->sdiodev->settings)
4260 brcmf_release_module_param(bus->sdiodev->settings);
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004261
Arend van Sprielbfad4a02014-01-06 12:40:39 +01004262 kfree(bus->rxbuf);
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004263 kfree(bus->hdrbuf);
4264 kfree(bus);
4265 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02004266
4267 brcmf_dbg(TRACE, "Disconnected\n");
4268}
4269
Arend van Spriel4011fc42016-01-05 11:05:47 +01004270void brcmf_sdio_wd_timer(struct brcmf_sdio *bus, bool active)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004271{
Arend van Spriel5b435de2011-10-05 13:19:03 +02004272 /* Totally stop the timer */
Arend van Spriel4011fc42016-01-05 11:05:47 +01004273 if (!active && bus->wd_active) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02004274 del_timer_sync(&bus->timer);
Arend van Spriel4011fc42016-01-05 11:05:47 +01004275 bus->wd_active = false;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004276 return;
4277 }
4278
Franky Linece960e2011-10-21 16:16:19 +02004279 /* don't start the wd until fw is loaded */
Arend van Spriela1ce7a02015-02-06 18:36:42 +01004280 if (bus->sdiodev->state != BRCMF_SDIOD_DATA)
Franky Linece960e2011-10-21 16:16:19 +02004281 return;
4282
Arend van Spriel4011fc42016-01-05 11:05:47 +01004283 if (active) {
4284 if (!bus->wd_active) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02004285 /* Create timer again when watchdog period is
4286 dynamically changed or in the first instance
4287 */
Arend van Spriel63ce3d52016-01-05 11:05:48 +01004288 bus->timer.expires = jiffies + BRCMF_WD_POLL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004289 add_timer(&bus->timer);
Arend van Spriel4011fc42016-01-05 11:05:47 +01004290 bus->wd_active = true;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004291 } else {
4292 /* Re arm the timer, at last watchdog period */
Arend van Spriel63ce3d52016-01-05 11:05:48 +01004293 mod_timer(&bus->timer, jiffies + BRCMF_WD_POLL);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004294 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02004295 }
4296}
Arend van Spriel99824642015-02-06 18:36:45 +01004297
4298int brcmf_sdio_sleep(struct brcmf_sdio *bus, bool sleep)
4299{
4300 int ret;
4301
4302 sdio_claim_host(bus->sdiodev->func[1]);
4303 ret = brcmf_sdio_bus_sleep(bus, sleep, false);
4304 sdio_release_host(bus->sdiodev->func[1]);
4305
4306 return ret;
4307}
4308