blob: 99a37765888d2086d477db1dd10493d95615084b [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>
18#include <linux/kernel.h>
19#include <linux/kthread.h>
20#include <linux/printk.h>
21#include <linux/pci_ids.h>
22#include <linux/netdevice.h>
23#include <linux/interrupt.h>
24#include <linux/sched.h>
25#include <linux/mmc/sdio.h>
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +010026#include <linux/mmc/sdio_ids.h>
Arend van Spriel5b435de2011-10-05 13:19:03 +020027#include <linux/mmc/sdio_func.h>
28#include <linux/mmc/card.h>
29#include <linux/semaphore.h>
30#include <linux/firmware.h>
Stephen Rothwellb7a57e72011-10-12 21:35:07 +020031#include <linux/module.h>
Franky Lin99ba15c2011-11-04 22:23:42 +010032#include <linux/bcma/bcma.h>
Arend van Spriel4fc0d012012-06-14 12:16:57 +020033#include <linux/debugfs.h>
Arend van Spriel8dc01812012-06-21 11:33:32 +020034#include <linux/vmalloc.h>
Hante Meuleman668761a2013-04-12 10:55:55 +020035#include <linux/platform_data/brcmfmac-sdio.h>
Franky Lin8da9d2c2013-11-29 12:25:10 +010036#include <linux/moduleparam.h>
Arend van Spriel5b435de2011-10-05 13:19:03 +020037#include <asm/unaligned.h>
38#include <defs.h>
39#include <brcmu_wifi.h>
40#include <brcmu_utils.h>
41#include <brcm_hw_ids.h>
42#include <soc.h>
Hante Meuleman888bf76e2014-10-28 14:56:17 +010043#include "sdio.h"
Arend van Spriel20c9c9b2014-01-29 15:32:15 +010044#include "chip.h"
Arend van Sprieldabedab2014-05-27 12:56:17 +020045#include "firmware.h"
Arend van Spriel5b435de2011-10-05 13:19:03 +020046
47#define DCMD_RESP_TIMEOUT 2000 /* In milli second */
48
Joe Perches8ae74652012-01-15 00:38:38 -080049#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +020050
51#define BRCMF_TRAP_INFO_SIZE 80
52
53#define CBUF_LEN (128)
54
Arend van Spriel4fc0d012012-06-14 12:16:57 +020055/* Device console log buffer state */
56#define CONSOLE_BUFFER_MAX 2024
57
Arend van Spriel5b435de2011-10-05 13:19:03 +020058struct rte_log_le {
59 __le32 buf; /* Can't be pointer on (64-bit) hosts */
60 __le32 buf_size;
61 __le32 idx;
62 char *_buf_compat; /* Redundant pointer for backward compat. */
63};
64
65struct rte_console {
66 /* Virtual UART
67 * When there is no UART (e.g. Quickturn),
68 * the host should write a complete
69 * input line directly into cbuf and then write
70 * the length into vcons_in.
71 * This may also be used when there is a real UART
72 * (at risk of conflicting with
73 * the real UART). vcons_out is currently unused.
74 */
75 uint vcons_in;
76 uint vcons_out;
77
78 /* Output (logging) buffer
79 * Console output is written to a ring buffer log_buf at index log_idx.
80 * The host may read the output when it sees log_idx advance.
81 * Output will be lost if the output wraps around faster than the host
82 * polls.
83 */
84 struct rte_log_le log_le;
85
86 /* Console input line buffer
87 * Characters are read one at a time into cbuf
88 * until <CR> is received, then
89 * the buffer is processed as a command line.
90 * Also used for virtual UART.
91 */
92 uint cbuf_idx;
93 char cbuf[CBUF_LEN];
94};
95
Joe Perches8ae74652012-01-15 00:38:38 -080096#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +020097#include <chipcommon.h>
98
Hante Meulemand14f78b2014-10-28 14:56:14 +010099#include "bus.h"
Hante Meulemana8e8ed32014-10-28 14:56:13 +0100100#include "debug.h"
Arend van Spriel40c1c242013-04-05 10:57:44 +0200101#include "tracepoint.h"
Arend van Spriel5b435de2011-10-05 13:19:03 +0200102
103#define TXQLEN 2048 /* bulk tx queue length */
104#define TXHI (TXQLEN - 256) /* turn on flow control above TXHI */
105#define TXLOW (TXHI - 256) /* turn off flow control below TXLOW */
106#define PRIOMASK 7
107
108#define TXRETRIES 2 /* # of retries for tx frames */
109
110#define BRCMF_RXBOUND 50 /* Default for max rx frames in
111 one scheduling */
112
113#define BRCMF_TXBOUND 20 /* Default for max tx frames in
114 one scheduling */
115
116#define BRCMF_TXMINMAX 1 /* Max tx frames if rx still pending */
117
118#define MEMBLOCK 2048 /* Block size used for downloading
119 of dongle image */
120#define MAX_DATA_BUF (32 * 1024) /* Must be large enough to hold
121 biggest possible glom */
122
123#define BRCMF_FIRSTREAD (1 << 6)
124
125
126/* SBSDIO_DEVICE_CTL */
127
128/* 1: device will assert busy signal when receiving CMD53 */
129#define SBSDIO_DEVCTL_SETBUSY 0x01
130/* 1: assertion of sdio interrupt is synchronous to the sdio clock */
131#define SBSDIO_DEVCTL_SPI_INTR_SYNC 0x02
132/* 1: mask all interrupts to host except the chipActive (rev 8) */
133#define SBSDIO_DEVCTL_CA_INT_ONLY 0x04
134/* 1: isolate internal sdio signals, put external pads in tri-state; requires
135 * sdio bus power cycle to clear (rev 9) */
136#define SBSDIO_DEVCTL_PADS_ISO 0x08
137/* Force SD->SB reset mapping (rev 11) */
138#define SBSDIO_DEVCTL_SB_RST_CTL 0x30
139/* Determined by CoreControl bit */
140#define SBSDIO_DEVCTL_RST_CORECTL 0x00
141/* Force backplane reset */
142#define SBSDIO_DEVCTL_RST_BPRESET 0x10
143/* Force no backplane reset */
144#define SBSDIO_DEVCTL_RST_NOBPRESET 0x20
145
Arend van Spriel5b435de2011-10-05 13:19:03 +0200146/* direct(mapped) cis space */
147
148/* MAPPED common CIS address */
149#define SBSDIO_CIS_BASE_COMMON 0x1000
150/* maximum bytes in one CIS */
151#define SBSDIO_CIS_SIZE_LIMIT 0x200
152/* cis offset addr is < 17 bits */
153#define SBSDIO_CIS_OFT_ADDR_MASK 0x1FFFF
154
155/* manfid tuple length, include tuple, link bytes */
156#define SBSDIO_CIS_MANFID_TUPLE_LEN 6
157
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100158#define CORE_BUS_REG(base, field) \
159 (base + offsetof(struct sdpcmd_regs, field))
160
161/* SDIO function 1 register CHIPCLKCSR */
162/* Force ALP request to backplane */
163#define SBSDIO_FORCE_ALP 0x01
164/* Force HT request to backplane */
165#define SBSDIO_FORCE_HT 0x02
166/* Force ILP request to backplane */
167#define SBSDIO_FORCE_ILP 0x04
168/* Make ALP ready (power up xtal) */
169#define SBSDIO_ALP_AVAIL_REQ 0x08
170/* Make HT ready (power up PLL) */
171#define SBSDIO_HT_AVAIL_REQ 0x10
172/* Squelch clock requests from HW */
173#define SBSDIO_FORCE_HW_CLKREQ_OFF 0x20
174/* Status: ALP is ready */
175#define SBSDIO_ALP_AVAIL 0x40
176/* Status: HT is ready */
177#define SBSDIO_HT_AVAIL 0x80
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100178#define SBSDIO_CSR_MASK 0x1F
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100179#define SBSDIO_AVBITS (SBSDIO_HT_AVAIL | SBSDIO_ALP_AVAIL)
180#define SBSDIO_ALPAV(regval) ((regval) & SBSDIO_AVBITS)
181#define SBSDIO_HTAV(regval) (((regval) & SBSDIO_AVBITS) == SBSDIO_AVBITS)
182#define SBSDIO_ALPONLY(regval) (SBSDIO_ALPAV(regval) && !SBSDIO_HTAV(regval))
183#define SBSDIO_CLKAV(regval, alponly) \
184 (SBSDIO_ALPAV(regval) && (alponly ? 1 : SBSDIO_HTAV(regval)))
185
Arend van Spriel5b435de2011-10-05 13:19:03 +0200186/* intstatus */
187#define I_SMB_SW0 (1 << 0) /* To SB Mail S/W interrupt 0 */
188#define I_SMB_SW1 (1 << 1) /* To SB Mail S/W interrupt 1 */
189#define I_SMB_SW2 (1 << 2) /* To SB Mail S/W interrupt 2 */
190#define I_SMB_SW3 (1 << 3) /* To SB Mail S/W interrupt 3 */
191#define I_SMB_SW_MASK 0x0000000f /* To SB Mail S/W interrupts mask */
192#define I_SMB_SW_SHIFT 0 /* To SB Mail S/W interrupts shift */
193#define I_HMB_SW0 (1 << 4) /* To Host Mail S/W interrupt 0 */
194#define I_HMB_SW1 (1 << 5) /* To Host Mail S/W interrupt 1 */
195#define I_HMB_SW2 (1 << 6) /* To Host Mail S/W interrupt 2 */
196#define I_HMB_SW3 (1 << 7) /* To Host Mail S/W interrupt 3 */
197#define I_HMB_SW_MASK 0x000000f0 /* To Host Mail S/W interrupts mask */
198#define I_HMB_SW_SHIFT 4 /* To Host Mail S/W interrupts shift */
199#define I_WR_OOSYNC (1 << 8) /* Write Frame Out Of Sync */
200#define I_RD_OOSYNC (1 << 9) /* Read Frame Out Of Sync */
201#define I_PC (1 << 10) /* descriptor error */
202#define I_PD (1 << 11) /* data error */
203#define I_DE (1 << 12) /* Descriptor protocol Error */
204#define I_RU (1 << 13) /* Receive descriptor Underflow */
205#define I_RO (1 << 14) /* Receive fifo Overflow */
206#define I_XU (1 << 15) /* Transmit fifo Underflow */
207#define I_RI (1 << 16) /* Receive Interrupt */
208#define I_BUSPWR (1 << 17) /* SDIO Bus Power Change (rev 9) */
209#define I_XMTDATA_AVAIL (1 << 23) /* bits in fifo */
210#define I_XI (1 << 24) /* Transmit Interrupt */
211#define I_RF_TERM (1 << 25) /* Read Frame Terminate */
212#define I_WF_TERM (1 << 26) /* Write Frame Terminate */
213#define I_PCMCIA_XU (1 << 27) /* PCMCIA Transmit FIFO Underflow */
214#define I_SBINT (1 << 28) /* sbintstatus Interrupt */
215#define I_CHIPACTIVE (1 << 29) /* chip from doze to active state */
216#define I_SRESET (1 << 30) /* CCCR RES interrupt */
217#define I_IOE2 (1U << 31) /* CCCR IOE2 Bit Changed */
218#define I_ERRORS (I_PC | I_PD | I_DE | I_RU | I_RO | I_XU)
219#define I_DMA (I_RI | I_XI | I_ERRORS)
220
221/* corecontrol */
222#define CC_CISRDY (1 << 0) /* CIS Ready */
223#define CC_BPRESEN (1 << 1) /* CCCR RES signal */
224#define CC_F2RDY (1 << 2) /* set CCCR IOR2 bit */
225#define CC_CLRPADSISO (1 << 3) /* clear SDIO pads isolation */
226#define CC_XMTDATAAVAIL_MODE (1 << 4)
227#define CC_XMTDATAAVAIL_CTRL (1 << 5)
228
229/* SDA_FRAMECTRL */
230#define SFC_RF_TERM (1 << 0) /* Read Frame Terminate */
231#define SFC_WF_TERM (1 << 1) /* Write Frame Terminate */
232#define SFC_CRC4WOOS (1 << 2) /* CRC error for write out of sync */
233#define SFC_ABORTALL (1 << 3) /* Abort all in-progress frames */
234
Arend van Spriel5b435de2011-10-05 13:19:03 +0200235/*
236 * Software allocation of To SB Mailbox resources
237 */
238
239/* tosbmailbox bits corresponding to intstatus bits */
240#define SMB_NAK (1 << 0) /* Frame NAK */
241#define SMB_INT_ACK (1 << 1) /* Host Interrupt ACK */
242#define SMB_USE_OOB (1 << 2) /* Use OOB Wakeup */
243#define SMB_DEV_INT (1 << 3) /* Miscellaneous Interrupt */
244
245/* tosbmailboxdata */
246#define SMB_DATA_VERSION_SHIFT 16 /* host protocol version */
247
248/*
249 * Software allocation of To Host Mailbox resources
250 */
251
252/* intstatus bits */
253#define I_HMB_FC_STATE I_HMB_SW0 /* Flow Control State */
254#define I_HMB_FC_CHANGE I_HMB_SW1 /* Flow Control State Changed */
255#define I_HMB_FRAME_IND I_HMB_SW2 /* Frame Indication */
256#define I_HMB_HOST_INT I_HMB_SW3 /* Miscellaneous Interrupt */
257
258/* tohostmailboxdata */
259#define HMB_DATA_NAKHANDLED 1 /* retransmit NAK'd frame */
260#define HMB_DATA_DEVREADY 2 /* talk to host after enable */
261#define HMB_DATA_FC 4 /* per prio flowcontrol update flag */
262#define HMB_DATA_FWREADY 8 /* fw ready for protocol activity */
263
264#define HMB_DATA_FCDATA_MASK 0xff000000
265#define HMB_DATA_FCDATA_SHIFT 24
266
267#define HMB_DATA_VERSION_MASK 0x00ff0000
268#define HMB_DATA_VERSION_SHIFT 16
269
270/*
271 * Software-defined protocol header
272 */
273
274/* Current protocol version */
275#define SDPCM_PROT_VERSION 4
276
Arend van Spriel5b435de2011-10-05 13:19:03 +0200277/*
278 * Shared structure between dongle and the host.
279 * The structure contains pointers to trap or assert information.
280 */
Arend van Spriel4fc0d012012-06-14 12:16:57 +0200281#define SDPCM_SHARED_VERSION 0x0003
Arend van Spriel5b435de2011-10-05 13:19:03 +0200282#define SDPCM_SHARED_VERSION_MASK 0x00FF
283#define SDPCM_SHARED_ASSERT_BUILT 0x0100
284#define SDPCM_SHARED_ASSERT 0x0200
285#define SDPCM_SHARED_TRAP 0x0400
286
287/* Space for header read, limit for data packets */
288#define MAX_HDR_READ (1 << 6)
289#define MAX_RX_DATASZ 2048
290
Arend van Spriel5b435de2011-10-05 13:19:03 +0200291/* Bump up limit on waiting for HT to account for first startup;
292 * if the image is doing a CRC calculation before programming the PMU
293 * for HT availability, it could take a couple hundred ms more, so
294 * max out at a 1 second (1000000us).
295 */
296#undef PMU_MAX_TRANSITION_DLY
297#define PMU_MAX_TRANSITION_DLY 1000000
298
299/* Value for ChipClockCSR during initial setup */
300#define BRCMF_INIT_CLKCTL1 (SBSDIO_FORCE_HW_CLKREQ_OFF | \
301 SBSDIO_ALP_AVAIL_REQ)
302
303/* Flags for SDH calls */
304#define F2SYNC (SDIO_REQ_4BYTE | SDIO_REQ_FIXED)
305
Franky Lin382a9e02011-12-16 18:37:18 -0800306#define BRCMF_IDLE_ACTIVE 0 /* Do not request any SD clock change
307 * when idle
308 */
309#define BRCMF_IDLE_INTERVAL 1
310
Piotr Haber4a3da992013-04-11 13:28:46 +0200311#define KSO_WAIT_US 50
312#define MAX_KSO_ATTEMPTS (PMU_MAX_TRANSITION_DLY/KSO_WAIT_US)
313
Arend van Spriel5b435de2011-10-05 13:19:03 +0200314/*
315 * Conversion of 802.1D priority to precedence level
316 */
317static uint prio2prec(u32 prio)
318{
319 return (prio == PRIO_8021D_NONE || prio == PRIO_8021D_BE) ?
320 (prio^2) : prio;
321}
322
Joe Perches8ae74652012-01-15 00:38:38 -0800323#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200324/* Device console log buffer state */
325struct brcmf_console {
326 uint count; /* Poll interval msec counter */
327 uint log_addr; /* Log struct address (fixed) */
328 struct rte_log_le log_le; /* Log struct (host copy) */
329 uint bufsize; /* Size of log buffer */
330 u8 *buf; /* Log buffer (host copy) */
331 uint last; /* Last buffer read index */
332};
Arend van Spriel4fc0d012012-06-14 12:16:57 +0200333
334struct brcmf_trap_info {
335 __le32 type;
336 __le32 epc;
337 __le32 cpsr;
338 __le32 spsr;
339 __le32 r0; /* a1 */
340 __le32 r1; /* a2 */
341 __le32 r2; /* a3 */
342 __le32 r3; /* a4 */
343 __le32 r4; /* v1 */
344 __le32 r5; /* v2 */
345 __le32 r6; /* v3 */
346 __le32 r7; /* v4 */
347 __le32 r8; /* v5 */
348 __le32 r9; /* sb/v6 */
349 __le32 r10; /* sl/v7 */
350 __le32 r11; /* fp/v8 */
351 __le32 r12; /* ip */
352 __le32 r13; /* sp */
353 __le32 r14; /* lr */
354 __le32 pc; /* r15 */
355};
Joe Perches8ae74652012-01-15 00:38:38 -0800356#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200357
358struct sdpcm_shared {
359 u32 flags;
360 u32 trap_addr;
361 u32 assert_exp_addr;
362 u32 assert_file_addr;
363 u32 assert_line;
364 u32 console_addr; /* Address of struct rte_console */
365 u32 msgtrace_addr;
366 u8 tag[32];
Arend van Spriel4fc0d012012-06-14 12:16:57 +0200367 u32 brpt_addr;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200368};
369
370struct sdpcm_shared_le {
371 __le32 flags;
372 __le32 trap_addr;
373 __le32 assert_exp_addr;
374 __le32 assert_file_addr;
375 __le32 assert_line;
376 __le32 console_addr; /* Address of struct rte_console */
377 __le32 msgtrace_addr;
378 u8 tag[32];
Arend van Spriel4fc0d012012-06-14 12:16:57 +0200379 __le32 brpt_addr;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200380};
381
Franky Lin6bc52312013-08-10 12:27:28 +0200382/* dongle SDIO bus specific header info */
383struct brcmf_sdio_hdrinfo {
Franky Lin4754fce2012-09-19 22:21:11 +0200384 u8 seq_num;
385 u8 channel;
386 u16 len;
387 u16 len_left;
388 u16 len_nxtfrm;
389 u8 dat_offset;
Franky Lin8da9d2c2013-11-29 12:25:10 +0100390 bool lastfrm;
391 u16 tail_pad;
Franky Lin4754fce2012-09-19 22:21:11 +0200392};
Arend van Spriel5b435de2011-10-05 13:19:03 +0200393
Arend van Spriel82d957e2014-07-12 08:49:36 +0200394/*
395 * hold counter variables
396 */
397struct brcmf_sdio_count {
398 uint intrcount; /* Count of device interrupt callbacks */
399 uint lastintrs; /* Count as of last watchdog timer */
400 uint pollcnt; /* Count of active polls */
401 uint regfails; /* Count of R_REG failures */
402 uint tx_sderrs; /* Count of tx attempts with sd errors */
403 uint fcqueued; /* Tx packets that got queued */
404 uint rxrtx; /* Count of rtx requests (NAK to dongle) */
405 uint rx_toolong; /* Receive frames too long to receive */
406 uint rxc_errors; /* SDIO errors when reading control frames */
407 uint rx_hdrfail; /* SDIO errors on header reads */
408 uint rx_badhdr; /* Bad received headers (roosync?) */
409 uint rx_badseq; /* Mismatched rx sequence number */
410 uint fc_rcvd; /* Number of flow-control events received */
411 uint fc_xoff; /* Number which turned on flow-control */
412 uint fc_xon; /* Number which turned off flow-control */
413 uint rxglomfail; /* Failed deglom attempts */
414 uint rxglomframes; /* Number of glom frames (superframes) */
415 uint rxglompkts; /* Number of packets from glom frames */
416 uint f2rxhdrs; /* Number of header reads */
417 uint f2rxdata; /* Number of frame data reads */
418 uint f2txdata; /* Number of f2 frame writes */
419 uint f1regdata; /* Number of f1 register accesses */
420 uint tickcnt; /* Number of watchdog been schedule */
421 ulong tx_ctlerrs; /* Err of sending ctrl frames */
422 ulong tx_ctlpkts; /* Ctrl frames sent to dongle */
423 ulong rx_ctlerrs; /* Err of processing rx ctrl frames */
424 ulong rx_ctlpkts; /* Ctrl frames processed from dongle */
425 ulong rx_readahead_cnt; /* packets where header read-ahead was used */
426};
427
Arend van Spriel5b435de2011-10-05 13:19:03 +0200428/* misc chip info needed by some of the routines */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200429/* Private data for SDIO bus interaction */
Franky Line92eedf2011-11-22 17:21:50 -0800430struct brcmf_sdio {
Arend van Spriel5b435de2011-10-05 13:19:03 +0200431 struct brcmf_sdio_dev *sdiodev; /* sdio device handler */
Arend van Spriel9cf218f2014-01-13 22:20:27 +0100432 struct brcmf_chip *ci; /* Chip info struct */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200433
434 u32 ramsize; /* Size of RAM in SOCRAM (bytes) */
435
436 u32 hostintmask; /* Copy of Host Interrupt Mask */
Franky Lin45316032012-09-13 21:12:03 +0200437 atomic_t intstatus; /* Intstatus bits (events) pending */
438 atomic_t fcstate; /* State of dongle flow-control */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200439
440 uint blocksize; /* Block size of SDIO transfers */
441 uint roundup; /* Max roundup limit */
442
443 struct pktq txq; /* Queue length used for flow-control */
444 u8 flowcontrol; /* per prio flow control bitmask */
445 u8 tx_seq; /* Transmit sequence number (next) */
446 u8 tx_max; /* Maximum transmit sequence allowed */
447
Arend van Spriel9b2d2f2a82013-11-29 12:25:14 +0100448 u8 *hdrbuf; /* buffer for handling rx frame */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200449 u8 *rxhdr; /* Header of current rx frame (in hdrbuf) */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200450 u8 rx_seq; /* Receive sequence number (expected) */
Franky Lin6bc52312013-08-10 12:27:28 +0200451 struct brcmf_sdio_hdrinfo cur_read;
Franky Lin4754fce2012-09-19 22:21:11 +0200452 /* info of current read frame */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200453 bool rxskip; /* Skip receive (awaiting NAK ACK) */
Franky Lin4754fce2012-09-19 22:21:11 +0200454 bool rxpending; /* Data frame pending in dongle */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200455
456 uint rxbound; /* Rx frames to read before resched */
457 uint txbound; /* Tx frames to send before resched */
458 uint txminmax;
459
460 struct sk_buff *glomd; /* Packet containing glomming descriptor */
Arend van Sprielb83db862011-10-19 12:51:09 +0200461 struct sk_buff_head glom; /* Packet list for glommed superframe */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200462 uint glomerr; /* Glom packet read errors */
463
464 u8 *rxbuf; /* Buffer for receiving control packets */
465 uint rxblen; /* Allocated length of rxbuf */
466 u8 *rxctl; /* Aligned pointer into rxbuf */
Franky Lindd43a012012-11-05 16:22:22 -0800467 u8 *rxctl_orig; /* pointer for freeing rxctl */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200468 uint rxlen; /* Length of valid data in buffer */
Franky Lindd43a012012-11-05 16:22:22 -0800469 spinlock_t rxctl_lock; /* protection lock for ctrl frame resources */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200470
471 u8 sdpcm_ver; /* Bus protocol reported by dongle */
472
473 bool intr; /* Use interrupts */
474 bool poll; /* Use polling */
Franky Lin1d382272012-09-13 21:11:59 +0200475 atomic_t ipend; /* Device interrupt is pending */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200476 uint spurious; /* Count of spurious interrupts */
477 uint pollrate; /* Ticks between device polls */
478 uint polltick; /* Tick counter */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200479
Joe Perches8ae74652012-01-15 00:38:38 -0800480#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200481 uint console_interval;
482 struct brcmf_console console; /* Console output polling support */
483 uint console_addr; /* Console address from shared struct */
Joe Perches8ae74652012-01-15 00:38:38 -0800484#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200485
Arend van Spriel5b435de2011-10-05 13:19:03 +0200486 uint clkstate; /* State of sd and backplane clock(s) */
487 bool activity; /* Activity flag for clock down */
488 s32 idletime; /* Control for activity timeout */
489 s32 idlecount; /* Activity timeout counter */
490 s32 idleclock; /* How to set bus driver when idle */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200491 bool rxflow_mode; /* Rx flow control mode */
492 bool rxflow; /* Is rx flow control on */
493 bool alp_only; /* Don't use HT clock (ALP only) */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200494
Arend van Spriel5b435de2011-10-05 13:19:03 +0200495 u8 *ctrl_frame_buf;
Hante Meulemanfed7ec42014-03-15 17:18:18 +0100496 u16 ctrl_frame_len;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200497 bool ctrl_frame_stat;
498
Hante Meulemanfed7ec42014-03-15 17:18:18 +0100499 spinlock_t txq_lock; /* protect bus->txq */
500 struct semaphore tx_seq_lock; /* protect bus->tx_seq */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200501 wait_queue_head_t ctrl_wait;
502 wait_queue_head_t dcmd_resp_wait;
503
504 struct timer_list timer;
505 struct completion watchdog_wait;
506 struct task_struct *watchdog_tsk;
507 bool wd_timer_valid;
508 uint save_ms;
509
Franky Linf1e68c22012-09-13 21:12:00 +0200510 struct workqueue_struct *brcmf_wq;
511 struct work_struct datawork;
Arend van Sprielfccfe932013-06-18 13:29:26 +0200512 atomic_t dpc_tskcnt;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200513
Franky Linc8bf3482011-12-16 18:37:08 -0800514 bool txoff; /* Transmit flow-controlled */
Arend van Spriel80969832012-06-09 22:51:44 +0200515 struct brcmf_sdio_count sdcnt;
Piotr Haber4a3da992013-04-11 13:28:46 +0200516 bool sr_enabled; /* SaveRestore enabled */
517 bool sleeping; /* SDIO bus sleeping */
Franky Lin706478c2013-08-10 12:27:29 +0200518
519 u8 tx_hdrlen; /* sdio bus header length for tx packet */
Franky Lin8da9d2c2013-11-29 12:25:10 +0100520 bool txglom; /* host tx glomming enable flag */
Arend van Spriele217d1c2013-11-29 12:25:13 +0100521 u16 head_align; /* buffer pointer alignment */
522 u16 sgentry_align; /* scatter-gather buffer alignment */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200523};
524
Arend van Spriel5b435de2011-10-05 13:19:03 +0200525/* clkstate */
526#define CLK_NONE 0
527#define CLK_SDONLY 1
Piotr Haber4a3da992013-04-11 13:28:46 +0200528#define CLK_PENDING 2
Arend van Spriel5b435de2011-10-05 13:19:03 +0200529#define CLK_AVAIL 3
530
Joe Perches8ae74652012-01-15 00:38:38 -0800531#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200532static int qcount[NUMPRIO];
Joe Perches8ae74652012-01-15 00:38:38 -0800533#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200534
Hante Meuleman668761a2013-04-12 10:55:55 +0200535#define DEFAULT_SDIO_DRIVE_STRENGTH 6 /* in milliamps */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200536
537#define RETRYCHAN(chan) ((chan) == SDPCM_EVENT_CHANNEL)
538
539/* Retry count for register access failures */
540static const uint retry_limit = 2;
541
542/* Limit on rounding up frames */
543static const uint max_roundup = 512;
544
545#define ALIGNMENT 4
546
Franky Lin9d7d6f92012-10-22 10:36:24 -0700547enum brcmf_sdio_frmtype {
548 BRCMF_SDIO_FT_NORMAL,
549 BRCMF_SDIO_FT_SUPER,
550 BRCMF_SDIO_FT_SUB,
551};
552
Arend van Spriel65d80d02014-01-29 15:32:14 +0100553#define SDIOD_DRVSTR_KEY(chip, pmu) (((chip) << 16) | (pmu))
554
555/* SDIO Pad drive strength to select value mappings */
556struct sdiod_drive_str {
557 u8 strength; /* Pad Drive Strength in mA */
558 u8 sel; /* Chip-specific select value */
559};
560
561/* SDIO Drive Strength to sel value table for PMU Rev 11 (1.8V) */
562static const struct sdiod_drive_str sdiod_drvstr_tab1_1v8[] = {
563 {32, 0x6},
564 {26, 0x7},
565 {22, 0x4},
566 {16, 0x5},
567 {12, 0x2},
568 {8, 0x3},
569 {4, 0x0},
570 {0, 0x1}
571};
572
573/* SDIO Drive Strength to sel value table for PMU Rev 13 (1.8v) */
574static const struct sdiod_drive_str sdiod_drive_strength_tab5_1v8[] = {
575 {6, 0x7},
576 {5, 0x6},
577 {4, 0x5},
578 {3, 0x4},
579 {2, 0x2},
580 {1, 0x1},
581 {0, 0x0}
582};
583
584/* SDIO Drive Strength to sel value table for PMU Rev 17 (1.8v) */
585static const struct sdiod_drive_str sdiod_drvstr_tab6_1v8[] = {
586 {3, 0x3},
587 {2, 0x2},
588 {1, 0x1},
589 {0, 0x0} };
590
591/* SDIO Drive Strength to sel value table for 43143 PMU Rev 17 (3.3V) */
592static const struct sdiod_drive_str sdiod_drvstr_tab2_3v3[] = {
593 {16, 0x7},
594 {12, 0x5},
595 {8, 0x3},
596 {4, 0x1}
597};
598
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200599#define BCM43143_FIRMWARE_NAME "brcm/brcmfmac43143-sdio.bin"
600#define BCM43143_NVRAM_NAME "brcm/brcmfmac43143-sdio.txt"
601#define BCM43241B0_FIRMWARE_NAME "brcm/brcmfmac43241b0-sdio.bin"
602#define BCM43241B0_NVRAM_NAME "brcm/brcmfmac43241b0-sdio.txt"
603#define BCM43241B4_FIRMWARE_NAME "brcm/brcmfmac43241b4-sdio.bin"
604#define BCM43241B4_NVRAM_NAME "brcm/brcmfmac43241b4-sdio.txt"
605#define BCM4329_FIRMWARE_NAME "brcm/brcmfmac4329-sdio.bin"
606#define BCM4329_NVRAM_NAME "brcm/brcmfmac4329-sdio.txt"
607#define BCM4330_FIRMWARE_NAME "brcm/brcmfmac4330-sdio.bin"
608#define BCM4330_NVRAM_NAME "brcm/brcmfmac4330-sdio.txt"
609#define BCM4334_FIRMWARE_NAME "brcm/brcmfmac4334-sdio.bin"
610#define BCM4334_NVRAM_NAME "brcm/brcmfmac4334-sdio.txt"
Arend van Spriel8b3a38d2014-12-23 19:04:23 +0100611#define BCM43340_FIRMWARE_NAME "brcm/brcmfmac43340-sdio.bin"
612#define BCM43340_NVRAM_NAME "brcm/brcmfmac43340-sdio.txt"
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200613#define BCM4335_FIRMWARE_NAME "brcm/brcmfmac4335-sdio.bin"
614#define BCM4335_NVRAM_NAME "brcm/brcmfmac4335-sdio.txt"
Arend van Spriel11e69c32014-01-08 10:49:33 +0100615#define BCM43362_FIRMWARE_NAME "brcm/brcmfmac43362-sdio.bin"
616#define BCM43362_NVRAM_NAME "brcm/brcmfmac43362-sdio.txt"
Franky Linbed89b62013-11-29 12:25:09 +0100617#define BCM4339_FIRMWARE_NAME "brcm/brcmfmac4339-sdio.bin"
618#define BCM4339_NVRAM_NAME "brcm/brcmfmac4339-sdio.txt"
Franky Lina797ca12014-03-15 17:18:17 +0100619#define BCM4354_FIRMWARE_NAME "brcm/brcmfmac4354-sdio.bin"
620#define BCM4354_NVRAM_NAME "brcm/brcmfmac4354-sdio.txt"
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200621
622MODULE_FIRMWARE(BCM43143_FIRMWARE_NAME);
623MODULE_FIRMWARE(BCM43143_NVRAM_NAME);
624MODULE_FIRMWARE(BCM43241B0_FIRMWARE_NAME);
625MODULE_FIRMWARE(BCM43241B0_NVRAM_NAME);
626MODULE_FIRMWARE(BCM43241B4_FIRMWARE_NAME);
627MODULE_FIRMWARE(BCM43241B4_NVRAM_NAME);
628MODULE_FIRMWARE(BCM4329_FIRMWARE_NAME);
629MODULE_FIRMWARE(BCM4329_NVRAM_NAME);
630MODULE_FIRMWARE(BCM4330_FIRMWARE_NAME);
631MODULE_FIRMWARE(BCM4330_NVRAM_NAME);
632MODULE_FIRMWARE(BCM4334_FIRMWARE_NAME);
633MODULE_FIRMWARE(BCM4334_NVRAM_NAME);
Arend van Spriel8b3a38d2014-12-23 19:04:23 +0100634MODULE_FIRMWARE(BCM43340_FIRMWARE_NAME);
635MODULE_FIRMWARE(BCM43340_NVRAM_NAME);
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200636MODULE_FIRMWARE(BCM4335_FIRMWARE_NAME);
637MODULE_FIRMWARE(BCM4335_NVRAM_NAME);
Arend van Spriel11e69c32014-01-08 10:49:33 +0100638MODULE_FIRMWARE(BCM43362_FIRMWARE_NAME);
639MODULE_FIRMWARE(BCM43362_NVRAM_NAME);
Franky Linbed89b62013-11-29 12:25:09 +0100640MODULE_FIRMWARE(BCM4339_FIRMWARE_NAME);
641MODULE_FIRMWARE(BCM4339_NVRAM_NAME);
Franky Lina797ca12014-03-15 17:18:17 +0100642MODULE_FIRMWARE(BCM4354_FIRMWARE_NAME);
643MODULE_FIRMWARE(BCM4354_NVRAM_NAME);
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200644
645struct brcmf_firmware_names {
646 u32 chipid;
647 u32 revmsk;
648 const char *bin;
649 const char *nv;
650};
651
652enum brcmf_firmware_type {
653 BRCMF_FIRMWARE_BIN,
654 BRCMF_FIRMWARE_NVRAM
655};
656
657#define BRCMF_FIRMWARE_NVRAM(name) \
658 name ## _FIRMWARE_NAME, name ## _NVRAM_NAME
659
660static const struct brcmf_firmware_names brcmf_fwname_data[] = {
Hante Meuleman5779ae62014-07-12 08:49:34 +0200661 { BRCM_CC_43143_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM43143) },
662 { BRCM_CC_43241_CHIP_ID, 0x0000001F, BRCMF_FIRMWARE_NVRAM(BCM43241B0) },
663 { BRCM_CC_43241_CHIP_ID, 0xFFFFFFE0, BRCMF_FIRMWARE_NVRAM(BCM43241B4) },
664 { BRCM_CC_4329_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM4329) },
665 { BRCM_CC_4330_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM4330) },
666 { BRCM_CC_4334_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM4334) },
Arend van Spriel8b3a38d2014-12-23 19:04:23 +0100667 { BRCM_CC_43340_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM43340) },
Hante Meuleman5779ae62014-07-12 08:49:34 +0200668 { BRCM_CC_4335_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM4335) },
669 { BRCM_CC_43362_CHIP_ID, 0xFFFFFFFE, BRCMF_FIRMWARE_NVRAM(BCM43362) },
670 { BRCM_CC_4339_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM4339) },
671 { BRCM_CC_4354_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM4354) }
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200672};
673
Daniel Kimc1b20532014-07-12 08:49:37 +0200674static int brcmf_sdio_get_fwnames(struct brcmf_chip *ci,
675 struct brcmf_sdio_dev *sdiodev)
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200676{
Arend van Sprielbd0e1b12014-05-27 12:56:22 +0200677 int i;
Daniel Kim46de0682014-07-30 13:20:00 +0200678 char end;
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200679
680 for (i = 0; i < ARRAY_SIZE(brcmf_fwname_data); i++) {
Arend van Sprielbd0e1b12014-05-27 12:56:22 +0200681 if (brcmf_fwname_data[i].chipid == ci->chip &&
Daniel Kimc1b20532014-07-12 08:49:37 +0200682 brcmf_fwname_data[i].revmsk & BIT(ci->chiprev))
683 break;
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200684 }
Daniel Kimc1b20532014-07-12 08:49:37 +0200685
686 if (i == ARRAY_SIZE(brcmf_fwname_data)) {
687 brcmf_err("Unknown chipid %d [%d]\n", ci->chip, ci->chiprev);
688 return -ENODEV;
689 }
690
691 /* check if firmware path is provided by module parameter */
692 if (brcmf_firmware_path[0] != '\0') {
Rickard Strandqvist59dfdd92014-10-12 13:42:14 +0200693 strlcpy(sdiodev->fw_name, brcmf_firmware_path,
694 sizeof(sdiodev->fw_name));
695 strlcpy(sdiodev->nvram_name, brcmf_firmware_path,
696 sizeof(sdiodev->nvram_name));
Daniel Kimc1b20532014-07-12 08:49:37 +0200697
Daniel Kim46de0682014-07-30 13:20:00 +0200698 end = brcmf_firmware_path[strlen(brcmf_firmware_path) - 1];
699 if (end != '/') {
Rickard Strandqvist59dfdd92014-10-12 13:42:14 +0200700 strlcat(sdiodev->fw_name, "/",
701 sizeof(sdiodev->fw_name));
702 strlcat(sdiodev->nvram_name, "/",
703 sizeof(sdiodev->nvram_name));
Daniel Kim46de0682014-07-30 13:20:00 +0200704 }
Daniel Kimc1b20532014-07-12 08:49:37 +0200705 }
Rickard Strandqvist59dfdd92014-10-12 13:42:14 +0200706 strlcat(sdiodev->fw_name, brcmf_fwname_data[i].bin,
707 sizeof(sdiodev->fw_name));
708 strlcat(sdiodev->nvram_name, brcmf_fwname_data[i].nv,
709 sizeof(sdiodev->nvram_name));
Daniel Kimc1b20532014-07-12 08:49:37 +0200710
711 return 0;
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200712}
713
Arend van Spriel5b435de2011-10-05 13:19:03 +0200714static void pkt_align(struct sk_buff *p, int len, int align)
715{
716 uint datalign;
717 datalign = (unsigned long)(p->data);
718 datalign = roundup(datalign, (align)) - datalign;
719 if (datalign)
720 skb_pull(p, datalign);
721 __skb_trim(p, len);
722}
723
724/* To check if there's window offered */
Franky Line92eedf2011-11-22 17:21:50 -0800725static bool data_ok(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200726{
727 return (u8)(bus->tx_max - bus->tx_seq) != 0 &&
728 ((u8)(bus->tx_max - bus->tx_seq) & 0x80) == 0;
729}
730
731/*
732 * Reads a register in the SDIO hardware block. This block occupies a series of
733 * adresses on the 32 bit backplane bus.
734 */
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100735static int r_sdreg32(struct brcmf_sdio *bus, u32 *regvar, u32 offset)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200736{
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100737 struct brcmf_core *core;
Franky Lin79ae3952012-05-04 18:27:34 -0700738 int ret;
Franky Lin58692752012-05-04 18:27:36 -0700739
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100740 core = brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV);
741 *regvar = brcmf_sdiod_regrl(bus->sdiodev, core->base + offset, &ret);
Franky Lin58692752012-05-04 18:27:36 -0700742
743 return ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200744}
745
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100746static int w_sdreg32(struct brcmf_sdio *bus, u32 regval, u32 reg_offset)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200747{
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100748 struct brcmf_core *core;
Franky Line13ce262012-05-04 18:27:35 -0700749 int ret;
Franky Lin58692752012-05-04 18:27:36 -0700750
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +0100751 core = brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV);
752 brcmf_sdiod_regwl(bus->sdiodev, core->base + reg_offset, regval, &ret);
Franky Lin58692752012-05-04 18:27:36 -0700753
754 return ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200755}
756
Piotr Haber4a3da992013-04-11 13:28:46 +0200757static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100758brcmf_sdio_kso_control(struct brcmf_sdio *bus, bool on)
Piotr Haber4a3da992013-04-11 13:28:46 +0200759{
760 u8 wr_val = 0, rd_val, cmp_val, bmask;
761 int err = 0;
762 int try_cnt = 0;
763
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100764 brcmf_dbg(TRACE, "Enter: on=%d\n", on);
Piotr Haber4a3da992013-04-11 13:28:46 +0200765
766 wr_val = (on << SBSDIO_FUNC1_SLEEPCSR_KSO_SHIFT);
767 /* 1st KSO write goes to AOS wake up core if device is asleep */
Arend van Spriela39be272013-12-12 11:58:58 +0100768 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
769 wr_val, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +0200770
771 if (on) {
772 /* device WAKEUP through KSO:
773 * write bit 0 & read back until
774 * both bits 0 (kso bit) & 1 (dev on status) are set
775 */
776 cmp_val = SBSDIO_FUNC1_SLEEPCSR_KSO_MASK |
777 SBSDIO_FUNC1_SLEEPCSR_DEVON_MASK;
778 bmask = cmp_val;
779 usleep_range(2000, 3000);
780 } else {
781 /* Put device to sleep, turn off KSO */
782 cmp_val = 0;
783 /* only check for bit0, bit1(dev on status) may not
784 * get cleared right away
785 */
786 bmask = SBSDIO_FUNC1_SLEEPCSR_KSO_MASK;
787 }
788
789 do {
790 /* reliable KSO bit set/clr:
791 * the sdiod sleep write access is synced to PMU 32khz clk
792 * just one write attempt may fail,
793 * read it back until it matches written value
794 */
Arend van Spriela39be272013-12-12 11:58:58 +0100795 rd_val = brcmf_sdiod_regrb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
796 &err);
Piotr Haber4a3da992013-04-11 13:28:46 +0200797 if (((rd_val & bmask) == cmp_val) && !err)
798 break;
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100799
Piotr Haber4a3da992013-04-11 13:28:46 +0200800 udelay(KSO_WAIT_US);
Arend van Spriela39be272013-12-12 11:58:58 +0100801 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
802 wr_val, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +0200803 } while (try_cnt++ < MAX_KSO_ATTEMPTS);
804
Arend van Spriel8a385ba2014-03-15 17:18:20 +0100805 if (try_cnt > 2)
806 brcmf_dbg(SDIO, "try_cnt=%d rd_val=0x%x err=%d\n", try_cnt,
807 rd_val, err);
808
809 if (try_cnt > MAX_KSO_ATTEMPTS)
810 brcmf_err("max tries: rd_val=0x%x err=%d\n", rd_val, err);
811
Piotr Haber4a3da992013-04-11 13:28:46 +0200812 return err;
813}
814
Arend van Spriel5b435de2011-10-05 13:19:03 +0200815#define HOSTINTMASK (I_HMB_SW_MASK | I_CHIPACTIVE)
816
Arend van Spriel5b435de2011-10-05 13:19:03 +0200817/* Turn backplane clock on or off */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100818static int brcmf_sdio_htclk(struct brcmf_sdio *bus, bool on, bool pendok)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200819{
820 int err;
821 u8 clkctl, clkreq, devctl;
822 unsigned long timeout;
823
Arend van Sprielc3203372013-04-03 12:40:44 +0200824 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200825
826 clkctl = 0;
827
Piotr Haber4a3da992013-04-11 13:28:46 +0200828 if (bus->sr_enabled) {
829 bus->clkstate = (on ? CLK_AVAIL : CLK_SDONLY);
830 return 0;
831 }
832
Arend van Spriel5b435de2011-10-05 13:19:03 +0200833 if (on) {
834 /* Request HT Avail */
835 clkreq =
836 bus->alp_only ? SBSDIO_ALP_AVAIL_REQ : SBSDIO_HT_AVAIL_REQ;
837
Arend van Spriela39be272013-12-12 11:58:58 +0100838 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
839 clkreq, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200840 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100841 brcmf_err("HT Avail request error: %d\n", err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200842 return -EBADE;
843 }
844
Arend van Spriel5b435de2011-10-05 13:19:03 +0200845 /* Check current status */
Arend van Spriela39be272013-12-12 11:58:58 +0100846 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
847 SBSDIO_FUNC1_CHIPCLKCSR, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200848 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100849 brcmf_err("HT Avail read error: %d\n", err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200850 return -EBADE;
851 }
852
853 /* Go to pending and await interrupt if appropriate */
854 if (!SBSDIO_CLKAV(clkctl, bus->alp_only) && pendok) {
855 /* Allow only clock-available interrupt */
Arend van Spriela39be272013-12-12 11:58:58 +0100856 devctl = brcmf_sdiod_regrb(bus->sdiodev,
857 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200858 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100859 brcmf_err("Devctl error setting CA: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200860 err);
861 return -EBADE;
862 }
863
864 devctl |= SBSDIO_DEVCTL_CA_INT_ONLY;
Arend van Spriela39be272013-12-12 11:58:58 +0100865 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
866 devctl, &err);
Arend van Sprielc3203372013-04-03 12:40:44 +0200867 brcmf_dbg(SDIO, "CLKCTL: set PENDING\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200868 bus->clkstate = CLK_PENDING;
869
870 return 0;
871 } else if (bus->clkstate == CLK_PENDING) {
872 /* Cancel CA-only interrupt filter */
Arend van Spriela39be272013-12-12 11:58:58 +0100873 devctl = brcmf_sdiod_regrb(bus->sdiodev,
874 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200875 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
Arend van Spriela39be272013-12-12 11:58:58 +0100876 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
877 devctl, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200878 }
879
880 /* Otherwise, wait here (polling) for HT Avail */
881 timeout = jiffies +
882 msecs_to_jiffies(PMU_MAX_TRANSITION_DLY/1000);
883 while (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
Arend van Spriela39be272013-12-12 11:58:58 +0100884 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
885 SBSDIO_FUNC1_CHIPCLKCSR,
886 &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200887 if (time_after(jiffies, timeout))
888 break;
889 else
890 usleep_range(5000, 10000);
891 }
892 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100893 brcmf_err("HT Avail request error: %d\n", err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200894 return -EBADE;
895 }
896 if (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100897 brcmf_err("HT Avail timeout (%d): clkctl 0x%02x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200898 PMU_MAX_TRANSITION_DLY, clkctl);
899 return -EBADE;
900 }
901
902 /* Mark clock available */
903 bus->clkstate = CLK_AVAIL;
Arend van Sprielc3203372013-04-03 12:40:44 +0200904 brcmf_dbg(SDIO, "CLKCTL: turned ON\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200905
Joe Perches8ae74652012-01-15 00:38:38 -0800906#if defined(DEBUG)
Joe Perches23677ce2012-02-09 11:17:23 +0000907 if (!bus->alp_only) {
Arend van Spriel5b435de2011-10-05 13:19:03 +0200908 if (SBSDIO_ALPONLY(clkctl))
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100909 brcmf_err("HT Clock should be on\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200910 }
Joe Perches8ae74652012-01-15 00:38:38 -0800911#endif /* defined (DEBUG) */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200912
Arend van Spriel5b435de2011-10-05 13:19:03 +0200913 } else {
914 clkreq = 0;
915
916 if (bus->clkstate == CLK_PENDING) {
917 /* Cancel CA-only interrupt filter */
Arend van Spriela39be272013-12-12 11:58:58 +0100918 devctl = brcmf_sdiod_regrb(bus->sdiodev,
919 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200920 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
Arend van Spriela39be272013-12-12 11:58:58 +0100921 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
922 devctl, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200923 }
924
925 bus->clkstate = CLK_SDONLY;
Arend van Spriela39be272013-12-12 11:58:58 +0100926 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
927 clkreq, &err);
Arend van Sprielc3203372013-04-03 12:40:44 +0200928 brcmf_dbg(SDIO, "CLKCTL: turned OFF\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200929 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100930 brcmf_err("Failed access turning clock off: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200931 err);
932 return -EBADE;
933 }
934 }
935 return 0;
936}
937
938/* Change idle/active SD state */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100939static int brcmf_sdio_sdclk(struct brcmf_sdio *bus, bool on)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200940{
Arend van Sprielc3203372013-04-03 12:40:44 +0200941 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200942
943 if (on)
944 bus->clkstate = CLK_SDONLY;
945 else
946 bus->clkstate = CLK_NONE;
947
948 return 0;
949}
950
951/* Transition SD and backplane clock readiness */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100952static int brcmf_sdio_clkctl(struct brcmf_sdio *bus, uint target, bool pendok)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200953{
Joe Perches8ae74652012-01-15 00:38:38 -0800954#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200955 uint oldstate = bus->clkstate;
Joe Perches8ae74652012-01-15 00:38:38 -0800956#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200957
Arend van Sprielc3203372013-04-03 12:40:44 +0200958 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200959
960 /* Early exit if we're already there */
961 if (bus->clkstate == target) {
962 if (target == CLK_AVAIL) {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100963 brcmf_sdio_wd_timer(bus, BRCMF_WD_POLL_MS);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200964 bus->activity = true;
965 }
966 return 0;
967 }
968
969 switch (target) {
970 case CLK_AVAIL:
971 /* Make sure SD clock is available */
972 if (bus->clkstate == CLK_NONE)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100973 brcmf_sdio_sdclk(bus, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200974 /* Now request HT Avail on the backplane */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100975 brcmf_sdio_htclk(bus, true, pendok);
976 brcmf_sdio_wd_timer(bus, BRCMF_WD_POLL_MS);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200977 bus->activity = true;
978 break;
979
980 case CLK_SDONLY:
981 /* Remove HT request, or bring up SD clock */
982 if (bus->clkstate == CLK_NONE)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100983 brcmf_sdio_sdclk(bus, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200984 else if (bus->clkstate == CLK_AVAIL)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100985 brcmf_sdio_htclk(bus, false, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200986 else
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100987 brcmf_err("request for %d -> %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200988 bus->clkstate, target);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100989 brcmf_sdio_wd_timer(bus, BRCMF_WD_POLL_MS);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200990 break;
991
992 case CLK_NONE:
993 /* Make sure to remove HT request */
994 if (bus->clkstate == CLK_AVAIL)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100995 brcmf_sdio_htclk(bus, false, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200996 /* Now remove the SD clock */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100997 brcmf_sdio_sdclk(bus, false);
998 brcmf_sdio_wd_timer(bus, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200999 break;
1000 }
Joe Perches8ae74652012-01-15 00:38:38 -08001001#ifdef DEBUG
Arend van Sprielc3203372013-04-03 12:40:44 +02001002 brcmf_dbg(SDIO, "%d -> %d\n", oldstate, bus->clkstate);
Joe Perches8ae74652012-01-15 00:38:38 -08001003#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02001004
1005 return 0;
1006}
1007
Piotr Haber4a3da992013-04-11 13:28:46 +02001008static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001009brcmf_sdio_bus_sleep(struct brcmf_sdio *bus, bool sleep, bool pendok)
Piotr Haber4a3da992013-04-11 13:28:46 +02001010{
1011 int err = 0;
Arend van Spriel8a385ba2014-03-15 17:18:20 +01001012 u8 clkcsr;
Arend van Spriel82030d62014-01-29 15:32:20 +01001013
1014 brcmf_dbg(SDIO, "Enter: request %s currently %s\n",
Piotr Haber4a3da992013-04-11 13:28:46 +02001015 (sleep ? "SLEEP" : "WAKE"),
1016 (bus->sleeping ? "SLEEP" : "WAKE"));
1017
1018 /* If SR is enabled control bus state with KSO */
1019 if (bus->sr_enabled) {
1020 /* Done if we're already in the requested state */
1021 if (sleep == bus->sleeping)
1022 goto end;
1023
1024 /* Going to sleep */
1025 if (sleep) {
1026 /* Don't sleep if something is pending */
1027 if (atomic_read(&bus->intstatus) ||
1028 atomic_read(&bus->ipend) > 0 ||
1029 (!atomic_read(&bus->fcstate) &&
1030 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) &&
Arend van Spriel8a385ba2014-03-15 17:18:20 +01001031 data_ok(bus))) {
1032 err = -EBUSY;
1033 goto done;
1034 }
1035
1036 clkcsr = brcmf_sdiod_regrb(bus->sdiodev,
1037 SBSDIO_FUNC1_CHIPCLKCSR,
1038 &err);
1039 if ((clkcsr & SBSDIO_CSR_MASK) == 0) {
1040 brcmf_dbg(SDIO, "no clock, set ALP\n");
1041 brcmf_sdiod_regwb(bus->sdiodev,
1042 SBSDIO_FUNC1_CHIPCLKCSR,
1043 SBSDIO_ALP_AVAIL_REQ, &err);
1044 }
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001045 err = brcmf_sdio_kso_control(bus, false);
Piotr Haber4a3da992013-04-11 13:28:46 +02001046 /* disable watchdog */
1047 if (!err)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001048 brcmf_sdio_wd_timer(bus, 0);
Piotr Haber4a3da992013-04-11 13:28:46 +02001049 } else {
1050 bus->idlecount = 0;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001051 err = brcmf_sdio_kso_control(bus, true);
Piotr Haber4a3da992013-04-11 13:28:46 +02001052 }
1053 if (!err) {
1054 /* Change state */
1055 bus->sleeping = sleep;
1056 brcmf_dbg(SDIO, "new state %s\n",
1057 (sleep ? "SLEEP" : "WAKE"));
1058 } else {
1059 brcmf_err("error while changing bus sleep state %d\n",
1060 err);
Arend van Spriel8a385ba2014-03-15 17:18:20 +01001061 goto done;
Piotr Haber4a3da992013-04-11 13:28:46 +02001062 }
1063 }
1064
1065end:
1066 /* control clocks */
1067 if (sleep) {
1068 if (!bus->sr_enabled)
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001069 brcmf_sdio_clkctl(bus, CLK_NONE, pendok);
Piotr Haber4a3da992013-04-11 13:28:46 +02001070 } else {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001071 brcmf_sdio_clkctl(bus, CLK_AVAIL, pendok);
Piotr Haber4a3da992013-04-11 13:28:46 +02001072 }
Arend van Spriel8a385ba2014-03-15 17:18:20 +01001073done:
1074 brcmf_dbg(SDIO, "Exit: err=%d\n", err);
Piotr Haber4a3da992013-04-11 13:28:46 +02001075 return err;
1076
1077}
1078
Daniel Kim0801e6c2014-01-29 15:32:13 +01001079#ifdef DEBUG
1080static inline bool brcmf_sdio_valid_shared_address(u32 addr)
1081{
1082 return !(addr == 0 || ((~addr >> 16) & 0xffff) == (addr & 0xffff));
1083}
1084
1085static int brcmf_sdio_readshared(struct brcmf_sdio *bus,
1086 struct sdpcm_shared *sh)
1087{
1088 u32 addr;
1089 int rv;
1090 u32 shaddr = 0;
1091 struct sdpcm_shared_le sh_le;
1092 __le32 addr_le;
1093
1094 shaddr = bus->ci->rambase + bus->ramsize - 4;
1095
1096 /*
1097 * Read last word in socram to determine
1098 * address of sdpcm_shared structure
1099 */
1100 sdio_claim_host(bus->sdiodev->func[1]);
1101 brcmf_sdio_bus_sleep(bus, false, false);
1102 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, shaddr, (u8 *)&addr_le, 4);
1103 sdio_release_host(bus->sdiodev->func[1]);
1104 if (rv < 0)
1105 return rv;
1106
1107 addr = le32_to_cpu(addr_le);
1108
1109 brcmf_dbg(SDIO, "sdpcm_shared address 0x%08X\n", addr);
1110
1111 /*
1112 * Check if addr is valid.
1113 * NVRAM length at the end of memory should have been overwritten.
1114 */
1115 if (!brcmf_sdio_valid_shared_address(addr)) {
1116 brcmf_err("invalid sdpcm_shared address 0x%08X\n",
1117 addr);
1118 return -EINVAL;
1119 }
1120
1121 /* Read hndrte_shared structure */
1122 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, (u8 *)&sh_le,
1123 sizeof(struct sdpcm_shared_le));
1124 if (rv < 0)
1125 return rv;
1126
1127 /* Endianness */
1128 sh->flags = le32_to_cpu(sh_le.flags);
1129 sh->trap_addr = le32_to_cpu(sh_le.trap_addr);
1130 sh->assert_exp_addr = le32_to_cpu(sh_le.assert_exp_addr);
1131 sh->assert_file_addr = le32_to_cpu(sh_le.assert_file_addr);
1132 sh->assert_line = le32_to_cpu(sh_le.assert_line);
1133 sh->console_addr = le32_to_cpu(sh_le.console_addr);
1134 sh->msgtrace_addr = le32_to_cpu(sh_le.msgtrace_addr);
1135
1136 if ((sh->flags & SDPCM_SHARED_VERSION_MASK) > SDPCM_SHARED_VERSION) {
1137 brcmf_err("sdpcm shared version unsupported: dhd %d dongle %d\n",
1138 SDPCM_SHARED_VERSION,
1139 sh->flags & SDPCM_SHARED_VERSION_MASK);
1140 return -EPROTO;
1141 }
1142
1143 return 0;
1144}
1145
1146static void brcmf_sdio_get_console_addr(struct brcmf_sdio *bus)
1147{
1148 struct sdpcm_shared sh;
1149
1150 if (brcmf_sdio_readshared(bus, &sh) == 0)
1151 bus->console_addr = sh.console_addr;
1152}
1153#else
1154static void brcmf_sdio_get_console_addr(struct brcmf_sdio *bus)
1155{
1156}
1157#endif /* DEBUG */
1158
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001159static u32 brcmf_sdio_hostmail(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001160{
1161 u32 intstatus = 0;
1162 u32 hmb_data;
1163 u8 fcbits;
Franky Lin58692752012-05-04 18:27:36 -07001164 int ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001165
Arend van Sprielc3203372013-04-03 12:40:44 +02001166 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001167
1168 /* Read mailbox data and ack that we did so */
Franky Lin58692752012-05-04 18:27:36 -07001169 ret = r_sdreg32(bus, &hmb_data,
1170 offsetof(struct sdpcmd_regs, tohostmailboxdata));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001171
Franky Lin58692752012-05-04 18:27:36 -07001172 if (ret == 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001173 w_sdreg32(bus, SMB_INT_ACK,
Franky Lin58692752012-05-04 18:27:36 -07001174 offsetof(struct sdpcmd_regs, tosbmailbox));
Arend van Spriel80969832012-06-09 22:51:44 +02001175 bus->sdcnt.f1regdata += 2;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001176
1177 /* Dongle recomposed rx frames, accept them again */
1178 if (hmb_data & HMB_DATA_NAKHANDLED) {
Arend van Sprielc3203372013-04-03 12:40:44 +02001179 brcmf_dbg(SDIO, "Dongle reports NAK handled, expect rtx of %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001180 bus->rx_seq);
1181 if (!bus->rxskip)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001182 brcmf_err("unexpected NAKHANDLED!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001183
1184 bus->rxskip = false;
1185 intstatus |= I_HMB_FRAME_IND;
1186 }
1187
1188 /*
1189 * DEVREADY does not occur with gSPI.
1190 */
1191 if (hmb_data & (HMB_DATA_DEVREADY | HMB_DATA_FWREADY)) {
1192 bus->sdpcm_ver =
1193 (hmb_data & HMB_DATA_VERSION_MASK) >>
1194 HMB_DATA_VERSION_SHIFT;
1195 if (bus->sdpcm_ver != SDPCM_PROT_VERSION)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001196 brcmf_err("Version mismatch, dongle reports %d, "
Arend van Spriel5b435de2011-10-05 13:19:03 +02001197 "expecting %d\n",
1198 bus->sdpcm_ver, SDPCM_PROT_VERSION);
1199 else
Arend van Sprielc3203372013-04-03 12:40:44 +02001200 brcmf_dbg(SDIO, "Dongle ready, protocol version %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001201 bus->sdpcm_ver);
Daniel Kim0801e6c2014-01-29 15:32:13 +01001202
1203 /*
1204 * Retrieve console state address now that firmware should have
1205 * updated it.
1206 */
1207 brcmf_sdio_get_console_addr(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001208 }
1209
1210 /*
1211 * Flow Control has been moved into the RX headers and this out of band
1212 * method isn't used any more.
1213 * remaining backward compatible with older dongles.
1214 */
1215 if (hmb_data & HMB_DATA_FC) {
1216 fcbits = (hmb_data & HMB_DATA_FCDATA_MASK) >>
1217 HMB_DATA_FCDATA_SHIFT;
1218
1219 if (fcbits & ~bus->flowcontrol)
Arend van Spriel80969832012-06-09 22:51:44 +02001220 bus->sdcnt.fc_xoff++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001221
1222 if (bus->flowcontrol & ~fcbits)
Arend van Spriel80969832012-06-09 22:51:44 +02001223 bus->sdcnt.fc_xon++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001224
Arend van Spriel80969832012-06-09 22:51:44 +02001225 bus->sdcnt.fc_rcvd++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001226 bus->flowcontrol = fcbits;
1227 }
1228
1229 /* Shouldn't be any others */
1230 if (hmb_data & ~(HMB_DATA_DEVREADY |
1231 HMB_DATA_NAKHANDLED |
1232 HMB_DATA_FC |
1233 HMB_DATA_FWREADY |
1234 HMB_DATA_FCDATA_MASK | HMB_DATA_VERSION_MASK))
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001235 brcmf_err("Unknown mailbox data content: 0x%02x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001236 hmb_data);
1237
1238 return intstatus;
1239}
1240
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001241static void brcmf_sdio_rxfail(struct brcmf_sdio *bus, bool abort, bool rtx)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001242{
1243 uint retries = 0;
1244 u16 lastrbc;
1245 u8 hi, lo;
1246 int err;
1247
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001248 brcmf_err("%sterminate frame%s\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001249 abort ? "abort command, " : "",
1250 rtx ? ", send NAK" : "");
1251
1252 if (abort)
Arend van Spriela39be272013-12-12 11:58:58 +01001253 brcmf_sdiod_abort(bus->sdiodev, SDIO_FUNC_2);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001254
Arend van Spriela39be272013-12-12 11:58:58 +01001255 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL,
1256 SFC_RF_TERM, &err);
Arend van Spriel80969832012-06-09 22:51:44 +02001257 bus->sdcnt.f1regdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001258
1259 /* Wait until the packet has been flushed (device/FIFO stable) */
1260 for (lastrbc = retries = 0xffff; retries > 0; retries--) {
Arend van Spriela39be272013-12-12 11:58:58 +01001261 hi = brcmf_sdiod_regrb(bus->sdiodev,
1262 SBSDIO_FUNC1_RFRAMEBCHI, &err);
1263 lo = brcmf_sdiod_regrb(bus->sdiodev,
1264 SBSDIO_FUNC1_RFRAMEBCLO, &err);
Arend van Spriel80969832012-06-09 22:51:44 +02001265 bus->sdcnt.f1regdata += 2;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001266
1267 if ((hi == 0) && (lo == 0))
1268 break;
1269
1270 if ((hi > (lastrbc >> 8)) && (lo > (lastrbc & 0x00ff))) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001271 brcmf_err("count growing: last 0x%04x now 0x%04x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001272 lastrbc, (hi << 8) + lo);
1273 }
1274 lastrbc = (hi << 8) + lo;
1275 }
1276
1277 if (!retries)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001278 brcmf_err("count never zeroed: last 0x%04x\n", lastrbc);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001279 else
Arend van Sprielc3203372013-04-03 12:40:44 +02001280 brcmf_dbg(SDIO, "flush took %d iterations\n", 0xffff - retries);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001281
1282 if (rtx) {
Arend van Spriel80969832012-06-09 22:51:44 +02001283 bus->sdcnt.rxrtx++;
Franky Lin58692752012-05-04 18:27:36 -07001284 err = w_sdreg32(bus, SMB_NAK,
1285 offsetof(struct sdpcmd_regs, tosbmailbox));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001286
Arend van Spriel80969832012-06-09 22:51:44 +02001287 bus->sdcnt.f1regdata++;
Franky Lin58692752012-05-04 18:27:36 -07001288 if (err == 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001289 bus->rxskip = true;
1290 }
1291
1292 /* Clear partial in any case */
Franky Lin4754fce2012-09-19 22:21:11 +02001293 bus->cur_read.len = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001294}
1295
Hante Meuleman81c78832014-02-25 20:30:35 +01001296static void brcmf_sdio_txfail(struct brcmf_sdio *bus)
1297{
1298 struct brcmf_sdio_dev *sdiodev = bus->sdiodev;
1299 u8 i, hi, lo;
1300
1301 /* On failure, abort the command and terminate the frame */
1302 brcmf_err("sdio error, abort command and terminate frame\n");
1303 bus->sdcnt.tx_sderrs++;
1304
1305 brcmf_sdiod_abort(sdiodev, SDIO_FUNC_2);
1306 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_FRAMECTRL, SFC_WF_TERM, NULL);
1307 bus->sdcnt.f1regdata++;
1308
1309 for (i = 0; i < 3; i++) {
1310 hi = brcmf_sdiod_regrb(sdiodev, SBSDIO_FUNC1_WFRAMEBCHI, NULL);
1311 lo = brcmf_sdiod_regrb(sdiodev, SBSDIO_FUNC1_WFRAMEBCLO, NULL);
1312 bus->sdcnt.f1regdata += 2;
1313 if ((hi == 0) && (lo == 0))
1314 break;
1315 }
1316}
1317
Arend van Spriel9a95e602011-11-10 20:30:29 +01001318/* return total length of buffer chain */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001319static uint brcmf_sdio_glom_len(struct brcmf_sdio *bus)
Arend van Spriel9a95e602011-11-10 20:30:29 +01001320{
1321 struct sk_buff *p;
1322 uint total;
1323
1324 total = 0;
1325 skb_queue_walk(&bus->glom, p)
1326 total += p->len;
1327 return total;
1328}
1329
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001330static void brcmf_sdio_free_glom(struct brcmf_sdio *bus)
Arend van Spriel046808d2011-11-10 20:30:31 +01001331{
1332 struct sk_buff *cur, *next;
1333
1334 skb_queue_walk_safe(&bus->glom, cur, next) {
1335 skb_unlink(cur, &bus->glom);
1336 brcmu_pkt_buf_free_skb(cur);
1337 }
1338}
1339
Franky Lin6bc52312013-08-10 12:27:28 +02001340/**
1341 * brcmfmac sdio bus specific header
1342 * This is the lowest layer header wrapped on the packets transmitted between
1343 * host and WiFi dongle which contains information needed for SDIO core and
1344 * firmware
1345 *
Franky Lin8da9d2c2013-11-29 12:25:10 +01001346 * It consists of 3 parts: hardware header, hardware extension header and
1347 * software header
Franky Lin6bc52312013-08-10 12:27:28 +02001348 * hardware header (frame tag) - 4 bytes
1349 * Byte 0~1: Frame length
1350 * Byte 2~3: Checksum, bit-wise inverse of frame length
Franky Lin8da9d2c2013-11-29 12:25:10 +01001351 * hardware extension header - 8 bytes
1352 * Tx glom mode only, N/A for Rx or normal Tx
1353 * Byte 0~1: Packet length excluding hw frame tag
1354 * Byte 2: Reserved
1355 * Byte 3: Frame flags, bit 0: last frame indication
1356 * Byte 4~5: Reserved
1357 * Byte 6~7: Tail padding length
Franky Lin6bc52312013-08-10 12:27:28 +02001358 * software header - 8 bytes
1359 * Byte 0: Rx/Tx sequence number
1360 * Byte 1: 4 MSB Channel number, 4 LSB arbitrary flag
1361 * Byte 2: Length of next data frame, reserved for Tx
1362 * Byte 3: Data offset
1363 * Byte 4: Flow control bits, reserved for Tx
1364 * Byte 5: Maximum Sequence number allowed by firmware for Tx, N/A for Tx packet
1365 * Byte 6~7: Reserved
1366 */
1367#define SDPCM_HWHDR_LEN 4
Franky Lin8da9d2c2013-11-29 12:25:10 +01001368#define SDPCM_HWEXT_LEN 8
Franky Lin6bc52312013-08-10 12:27:28 +02001369#define SDPCM_SWHDR_LEN 8
1370#define SDPCM_HDRLEN (SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN)
Franky Lin6bc52312013-08-10 12:27:28 +02001371/* software header */
1372#define SDPCM_SEQ_MASK 0x000000ff
1373#define SDPCM_SEQ_WRAP 256
1374#define SDPCM_CHANNEL_MASK 0x00000f00
1375#define SDPCM_CHANNEL_SHIFT 8
1376#define SDPCM_CONTROL_CHANNEL 0 /* Control */
1377#define SDPCM_EVENT_CHANNEL 1 /* Asyc Event Indication */
1378#define SDPCM_DATA_CHANNEL 2 /* Data Xmit/Recv */
1379#define SDPCM_GLOM_CHANNEL 3 /* Coalesced packets */
1380#define SDPCM_TEST_CHANNEL 15 /* Test/debug packets */
1381#define SDPCM_GLOMDESC(p) (((u8 *)p)[1] & 0x80)
1382#define SDPCM_NEXTLEN_MASK 0x00ff0000
1383#define SDPCM_NEXTLEN_SHIFT 16
1384#define SDPCM_DOFFSET_MASK 0xff000000
1385#define SDPCM_DOFFSET_SHIFT 24
1386#define SDPCM_FCMASK_MASK 0x000000ff
1387#define SDPCM_WINDOW_MASK 0x0000ff00
1388#define SDPCM_WINDOW_SHIFT 8
1389
1390static inline u8 brcmf_sdio_getdatoffset(u8 *swheader)
1391{
1392 u32 hdrvalue;
1393 hdrvalue = *(u32 *)swheader;
1394 return (u8)((hdrvalue & SDPCM_DOFFSET_MASK) >> SDPCM_DOFFSET_SHIFT);
1395}
1396
1397static int brcmf_sdio_hdparse(struct brcmf_sdio *bus, u8 *header,
1398 struct brcmf_sdio_hdrinfo *rd,
1399 enum brcmf_sdio_frmtype type)
Franky Lin4754fce2012-09-19 22:21:11 +02001400{
1401 u16 len, checksum;
1402 u8 rx_seq, fc, tx_seq_max;
Franky Lin6bc52312013-08-10 12:27:28 +02001403 u32 swheader;
Franky Lin4754fce2012-09-19 22:21:11 +02001404
Arend van Spriel4b776962013-11-29 12:25:06 +01001405 trace_brcmf_sdpcm_hdr(SDPCM_RX, header);
Arend van Spriel76584ec2013-10-15 15:44:47 +02001406
Franky Lin6bc52312013-08-10 12:27:28 +02001407 /* hw header */
Franky Lin4754fce2012-09-19 22:21:11 +02001408 len = get_unaligned_le16(header);
1409 checksum = get_unaligned_le16(header + sizeof(u16));
1410 /* All zero means no more to read */
1411 if (!(len | checksum)) {
1412 bus->rxpending = false;
Franky Lin10510582012-11-05 16:22:26 -08001413 return -ENODATA;
Franky Lin4754fce2012-09-19 22:21:11 +02001414 }
1415 if ((u16)(~(len ^ checksum))) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001416 brcmf_err("HW header checksum error\n");
Franky Lin4754fce2012-09-19 22:21:11 +02001417 bus->sdcnt.rx_badhdr++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001418 brcmf_sdio_rxfail(bus, false, false);
Franky Lin10510582012-11-05 16:22:26 -08001419 return -EIO;
Franky Lin4754fce2012-09-19 22:21:11 +02001420 }
1421 if (len < SDPCM_HDRLEN) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001422 brcmf_err("HW header length error\n");
Franky Lin10510582012-11-05 16:22:26 -08001423 return -EPROTO;
Franky Lin4754fce2012-09-19 22:21:11 +02001424 }
Franky Lin9d7d6f92012-10-22 10:36:24 -07001425 if (type == BRCMF_SDIO_FT_SUPER &&
1426 (roundup(len, bus->blocksize) != rd->len)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001427 brcmf_err("HW superframe header length error\n");
Franky Lin10510582012-11-05 16:22:26 -08001428 return -EPROTO;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001429 }
1430 if (type == BRCMF_SDIO_FT_SUB && len > rd->len) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001431 brcmf_err("HW subframe header length error\n");
Franky Lin10510582012-11-05 16:22:26 -08001432 return -EPROTO;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001433 }
Franky Lin4754fce2012-09-19 22:21:11 +02001434 rd->len = len;
1435
Franky Lin6bc52312013-08-10 12:27:28 +02001436 /* software header */
1437 header += SDPCM_HWHDR_LEN;
1438 swheader = le32_to_cpu(*(__le32 *)header);
1439 if (type == BRCMF_SDIO_FT_SUPER && SDPCM_GLOMDESC(header)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001440 brcmf_err("Glom descriptor found in superframe head\n");
Franky Lin9d7d6f92012-10-22 10:36:24 -07001441 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001442 return -EINVAL;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001443 }
Franky Lin6bc52312013-08-10 12:27:28 +02001444 rx_seq = (u8)(swheader & SDPCM_SEQ_MASK);
1445 rd->channel = (swheader & SDPCM_CHANNEL_MASK) >> SDPCM_CHANNEL_SHIFT;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001446 if (len > MAX_RX_DATASZ && rd->channel != SDPCM_CONTROL_CHANNEL &&
1447 type != BRCMF_SDIO_FT_SUPER) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001448 brcmf_err("HW header length too long\n");
Franky Lin4754fce2012-09-19 22:21:11 +02001449 bus->sdcnt.rx_toolong++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001450 brcmf_sdio_rxfail(bus, false, false);
Franky Lin4754fce2012-09-19 22:21:11 +02001451 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001452 return -EPROTO;
Franky Lin4754fce2012-09-19 22:21:11 +02001453 }
Franky Lin9d7d6f92012-10-22 10:36:24 -07001454 if (type == BRCMF_SDIO_FT_SUPER && rd->channel != SDPCM_GLOM_CHANNEL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001455 brcmf_err("Wrong channel for superframe\n");
Franky Lin9d7d6f92012-10-22 10:36:24 -07001456 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001457 return -EINVAL;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001458 }
1459 if (type == BRCMF_SDIO_FT_SUB && rd->channel != SDPCM_DATA_CHANNEL &&
1460 rd->channel != SDPCM_EVENT_CHANNEL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001461 brcmf_err("Wrong channel for subframe\n");
Franky Lin9d7d6f92012-10-22 10:36:24 -07001462 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001463 return -EINVAL;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001464 }
Franky Lin6bc52312013-08-10 12:27:28 +02001465 rd->dat_offset = brcmf_sdio_getdatoffset(header);
Franky Lin4754fce2012-09-19 22:21:11 +02001466 if (rd->dat_offset < SDPCM_HDRLEN || rd->dat_offset > rd->len) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001467 brcmf_err("seq %d: bad data offset\n", rx_seq);
Franky Lin4754fce2012-09-19 22:21:11 +02001468 bus->sdcnt.rx_badhdr++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001469 brcmf_sdio_rxfail(bus, false, false);
Franky Lin4754fce2012-09-19 22:21:11 +02001470 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001471 return -ENXIO;
Franky Lin4754fce2012-09-19 22:21:11 +02001472 }
1473 if (rd->seq_num != rx_seq) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001474 brcmf_err("seq %d: sequence number error, expect %d\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001475 rx_seq, rd->seq_num);
1476 bus->sdcnt.rx_badseq++;
1477 rd->seq_num = rx_seq;
1478 }
Franky Lin9d7d6f92012-10-22 10:36:24 -07001479 /* no need to check the reset for subframe */
1480 if (type == BRCMF_SDIO_FT_SUB)
Franky Lin10510582012-11-05 16:22:26 -08001481 return 0;
Franky Lin6bc52312013-08-10 12:27:28 +02001482 rd->len_nxtfrm = (swheader & SDPCM_NEXTLEN_MASK) >> SDPCM_NEXTLEN_SHIFT;
Franky Lin4754fce2012-09-19 22:21:11 +02001483 if (rd->len_nxtfrm << 4 > MAX_RX_DATASZ) {
1484 /* only warm for NON glom packet */
1485 if (rd->channel != SDPCM_GLOM_CHANNEL)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001486 brcmf_err("seq %d: next length error\n", rx_seq);
Franky Lin4754fce2012-09-19 22:21:11 +02001487 rd->len_nxtfrm = 0;
1488 }
Franky Lin6bc52312013-08-10 12:27:28 +02001489 swheader = le32_to_cpu(*(__le32 *)(header + 4));
1490 fc = swheader & SDPCM_FCMASK_MASK;
Franky Lin4754fce2012-09-19 22:21:11 +02001491 if (bus->flowcontrol != fc) {
1492 if (~bus->flowcontrol & fc)
1493 bus->sdcnt.fc_xoff++;
1494 if (bus->flowcontrol & ~fc)
1495 bus->sdcnt.fc_xon++;
1496 bus->sdcnt.fc_rcvd++;
1497 bus->flowcontrol = fc;
1498 }
Franky Lin6bc52312013-08-10 12:27:28 +02001499 tx_seq_max = (swheader & SDPCM_WINDOW_MASK) >> SDPCM_WINDOW_SHIFT;
Franky Lin4754fce2012-09-19 22:21:11 +02001500 if ((u8)(tx_seq_max - bus->tx_seq) > 0x40) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001501 brcmf_err("seq %d: max tx seq number error\n", rx_seq);
Franky Lin4754fce2012-09-19 22:21:11 +02001502 tx_seq_max = bus->tx_seq + 2;
1503 }
1504 bus->tx_max = tx_seq_max;
1505
Franky Lin10510582012-11-05 16:22:26 -08001506 return 0;
Franky Lin4754fce2012-09-19 22:21:11 +02001507}
1508
Franky Lin6bc52312013-08-10 12:27:28 +02001509static inline void brcmf_sdio_update_hwhdr(u8 *header, u16 frm_length)
1510{
1511 *(__le16 *)header = cpu_to_le16(frm_length);
1512 *(((__le16 *)header) + 1) = cpu_to_le16(~frm_length);
1513}
1514
1515static void brcmf_sdio_hdpack(struct brcmf_sdio *bus, u8 *header,
1516 struct brcmf_sdio_hdrinfo *hd_info)
1517{
Franky Lin8da9d2c2013-11-29 12:25:10 +01001518 u32 hdrval;
1519 u8 hdr_offset;
Franky Lin6bc52312013-08-10 12:27:28 +02001520
1521 brcmf_sdio_update_hwhdr(header, hd_info->len);
Franky Lin8da9d2c2013-11-29 12:25:10 +01001522 hdr_offset = SDPCM_HWHDR_LEN;
Franky Lin6bc52312013-08-10 12:27:28 +02001523
Franky Lin8da9d2c2013-11-29 12:25:10 +01001524 if (bus->txglom) {
1525 hdrval = (hd_info->len - hdr_offset) | (hd_info->lastfrm << 24);
1526 *((__le32 *)(header + hdr_offset)) = cpu_to_le32(hdrval);
1527 hdrval = (u16)hd_info->tail_pad << 16;
1528 *(((__le32 *)(header + hdr_offset)) + 1) = cpu_to_le32(hdrval);
1529 hdr_offset += SDPCM_HWEXT_LEN;
1530 }
1531
1532 hdrval = hd_info->seq_num;
1533 hdrval |= (hd_info->channel << SDPCM_CHANNEL_SHIFT) &
1534 SDPCM_CHANNEL_MASK;
1535 hdrval |= (hd_info->dat_offset << SDPCM_DOFFSET_SHIFT) &
1536 SDPCM_DOFFSET_MASK;
1537 *((__le32 *)(header + hdr_offset)) = cpu_to_le32(hdrval);
1538 *(((__le32 *)(header + hdr_offset)) + 1) = 0;
1539 trace_brcmf_sdpcm_hdr(SDPCM_TX + !!(bus->txglom), header);
Franky Lin6bc52312013-08-10 12:27:28 +02001540}
1541
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001542static u8 brcmf_sdio_rxglom(struct brcmf_sdio *bus, u8 rxseq)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001543{
1544 u16 dlen, totlen;
1545 u8 *dptr, num = 0;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001546 u16 sublen;
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001547 struct sk_buff *pfirst, *pnext;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001548
1549 int errcode;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001550 u8 doff, sfdoff;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001551
Franky Lin6bc52312013-08-10 12:27:28 +02001552 struct brcmf_sdio_hdrinfo rd_new;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001553
1554 /* If packets, issue read(s) and send up packet chain */
1555 /* Return sequence numbers consumed? */
1556
Arend van Sprielc3203372013-04-03 12:40:44 +02001557 brcmf_dbg(SDIO, "start: glomd %p glom %p\n",
Arend van Sprielb83db862011-10-19 12:51:09 +02001558 bus->glomd, skb_peek(&bus->glom));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001559
1560 /* If there's a descriptor, generate the packet chain */
1561 if (bus->glomd) {
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001562 pfirst = pnext = NULL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001563 dlen = (u16) (bus->glomd->len);
1564 dptr = bus->glomd->data;
1565 if (!dlen || (dlen & 1)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001566 brcmf_err("bad glomd len(%d), ignore descriptor\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001567 dlen);
1568 dlen = 0;
1569 }
1570
1571 for (totlen = num = 0; dlen; num++) {
1572 /* Get (and move past) next length */
1573 sublen = get_unaligned_le16(dptr);
1574 dlen -= sizeof(u16);
1575 dptr += sizeof(u16);
1576 if ((sublen < SDPCM_HDRLEN) ||
1577 ((num == 0) && (sublen < (2 * SDPCM_HDRLEN)))) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001578 brcmf_err("descriptor len %d bad: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001579 num, sublen);
1580 pnext = NULL;
1581 break;
1582 }
Arend van Spriele217d1c2013-11-29 12:25:13 +01001583 if (sublen % bus->sgentry_align) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001584 brcmf_err("sublen %d not multiple of %d\n",
Arend van Spriele217d1c2013-11-29 12:25:13 +01001585 sublen, bus->sgentry_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001586 }
1587 totlen += sublen;
1588
1589 /* For last frame, adjust read len so total
1590 is a block multiple */
1591 if (!dlen) {
1592 sublen +=
1593 (roundup(totlen, bus->blocksize) - totlen);
1594 totlen = roundup(totlen, bus->blocksize);
1595 }
1596
1597 /* Allocate/chain packet for next subframe */
Arend van Spriele217d1c2013-11-29 12:25:13 +01001598 pnext = brcmu_pkt_buf_get_skb(sublen + bus->sgentry_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001599 if (pnext == NULL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001600 brcmf_err("bcm_pkt_buf_get_skb failed, num %d len %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001601 num, sublen);
1602 break;
1603 }
Arend van Sprielb83db862011-10-19 12:51:09 +02001604 skb_queue_tail(&bus->glom, pnext);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001605
1606 /* Adhere to start alignment requirements */
Arend van Spriele217d1c2013-11-29 12:25:13 +01001607 pkt_align(pnext, sublen, bus->sgentry_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001608 }
1609
1610 /* If all allocations succeeded, save packet chain
1611 in bus structure */
1612 if (pnext) {
1613 brcmf_dbg(GLOM, "allocated %d-byte packet chain for %d subframes\n",
1614 totlen, num);
Franky Lin4754fce2012-09-19 22:21:11 +02001615 if (BRCMF_GLOM_ON() && bus->cur_read.len &&
1616 totlen != bus->cur_read.len) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001617 brcmf_dbg(GLOM, "glomdesc mismatch: nextlen %d glomdesc %d rxseq %d\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001618 bus->cur_read.len, totlen, rxseq);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001619 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02001620 pfirst = pnext = NULL;
1621 } else {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001622 brcmf_sdio_free_glom(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001623 num = 0;
1624 }
1625
1626 /* Done with descriptor packet */
1627 brcmu_pkt_buf_free_skb(bus->glomd);
1628 bus->glomd = NULL;
Franky Lin4754fce2012-09-19 22:21:11 +02001629 bus->cur_read.len = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001630 }
1631
1632 /* Ok -- either we just generated a packet chain,
1633 or had one from before */
Arend van Sprielb83db862011-10-19 12:51:09 +02001634 if (!skb_queue_empty(&bus->glom)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001635 if (BRCMF_GLOM_ON()) {
1636 brcmf_dbg(GLOM, "try superframe read, packet chain:\n");
Arend van Sprielb83db862011-10-19 12:51:09 +02001637 skb_queue_walk(&bus->glom, pnext) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001638 brcmf_dbg(GLOM, " %p: %p len 0x%04x (%d)\n",
1639 pnext, (u8 *) (pnext->data),
1640 pnext->len, pnext->len);
1641 }
1642 }
1643
Arend van Sprielb83db862011-10-19 12:51:09 +02001644 pfirst = skb_peek(&bus->glom);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001645 dlen = (u16) brcmf_sdio_glom_len(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001646
1647 /* Do an SDIO read for the superframe. Configurable iovar to
1648 * read directly into the chained packet, or allocate a large
1649 * packet and and copy into the chain.
1650 */
Franky Lin38b0b0d2012-11-05 16:22:24 -08001651 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriela39be272013-12-12 11:58:58 +01001652 errcode = brcmf_sdiod_recv_chain(bus->sdiodev,
Arend van Spriela39be272013-12-12 11:58:58 +01001653 &bus->glom, dlen);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001654 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel80969832012-06-09 22:51:44 +02001655 bus->sdcnt.f2rxdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001656
1657 /* On failure, kill the superframe, allow a couple retries */
1658 if (errcode < 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001659 brcmf_err("glom read of %d bytes failed: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001660 dlen, errcode);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001661
Franky Lin38b0b0d2012-11-05 16:22:24 -08001662 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001663 if (bus->glomerr++ < 3) {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001664 brcmf_sdio_rxfail(bus, true, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001665 } else {
1666 bus->glomerr = 0;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001667 brcmf_sdio_rxfail(bus, true, false);
Arend van Spriel80969832012-06-09 22:51:44 +02001668 bus->sdcnt.rxglomfail++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001669 brcmf_sdio_free_glom(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001670 }
Franky Lin38b0b0d2012-11-05 16:22:24 -08001671 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001672 return 0;
1673 }
Joe Perches1e023822012-01-15 00:38:40 -08001674
1675 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
1676 pfirst->data, min_t(int, pfirst->len, 48),
1677 "SUPERFRAME:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001678
Franky Lin9d7d6f92012-10-22 10:36:24 -07001679 rd_new.seq_num = rxseq;
1680 rd_new.len = dlen;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001681 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin6bc52312013-08-10 12:27:28 +02001682 errcode = brcmf_sdio_hdparse(bus, pfirst->data, &rd_new,
1683 BRCMF_SDIO_FT_SUPER);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001684 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin9d7d6f92012-10-22 10:36:24 -07001685 bus->cur_read.len = rd_new.len_nxtfrm << 4;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001686
1687 /* Remove superframe header, remember offset */
Franky Lin9d7d6f92012-10-22 10:36:24 -07001688 skb_pull(pfirst, rd_new.dat_offset);
1689 sfdoff = rd_new.dat_offset;
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001690 num = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001691
1692 /* Validate all the subframe headers */
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001693 skb_queue_walk(&bus->glom, pnext) {
1694 /* leave when invalid subframe is found */
1695 if (errcode)
1696 break;
1697
Franky Lin9d7d6f92012-10-22 10:36:24 -07001698 rd_new.len = pnext->len;
1699 rd_new.seq_num = rxseq++;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001700 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin6bc52312013-08-10 12:27:28 +02001701 errcode = brcmf_sdio_hdparse(bus, pnext->data, &rd_new,
1702 BRCMF_SDIO_FT_SUB);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001703 sdio_release_host(bus->sdiodev->func[1]);
Joe Perches1e023822012-01-15 00:38:40 -08001704 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
Franky Lin9d7d6f92012-10-22 10:36:24 -07001705 pnext->data, 32, "subframe:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001706
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001707 num++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001708 }
1709
1710 if (errcode) {
1711 /* Terminate frame on error, request
1712 a couple retries */
Franky Lin38b0b0d2012-11-05 16:22:24 -08001713 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001714 if (bus->glomerr++ < 3) {
1715 /* Restore superframe header space */
1716 skb_push(pfirst, sfdoff);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001717 brcmf_sdio_rxfail(bus, true, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001718 } else {
1719 bus->glomerr = 0;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001720 brcmf_sdio_rxfail(bus, true, false);
Arend van Spriel80969832012-06-09 22:51:44 +02001721 bus->sdcnt.rxglomfail++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001722 brcmf_sdio_free_glom(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001723 }
Franky Lin38b0b0d2012-11-05 16:22:24 -08001724 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001725 bus->cur_read.len = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001726 return 0;
1727 }
1728
1729 /* Basic SD framing looks ok - process each packet (header) */
Arend van Spriel5b435de2011-10-05 13:19:03 +02001730
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001731 skb_queue_walk_safe(&bus->glom, pfirst, pnext) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001732 dptr = (u8 *) (pfirst->data);
1733 sublen = get_unaligned_le16(dptr);
Franky Lin6bc52312013-08-10 12:27:28 +02001734 doff = brcmf_sdio_getdatoffset(&dptr[SDPCM_HWHDR_LEN]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001735
Joe Perches1e023822012-01-15 00:38:40 -08001736 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_DATA_ON(),
Franky Lin9d7d6f92012-10-22 10:36:24 -07001737 dptr, pfirst->len,
1738 "Rx Subframe Data:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001739
1740 __skb_trim(pfirst, sublen);
1741 skb_pull(pfirst, doff);
1742
1743 if (pfirst->len == 0) {
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001744 skb_unlink(pfirst, &bus->glom);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001745 brcmu_pkt_buf_free_skb(pfirst);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001746 continue;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001747 }
1748
Joe Perches1e023822012-01-15 00:38:40 -08001749 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
1750 pfirst->data,
1751 min_t(int, pfirst->len, 32),
1752 "subframe %d to stack, %p (%p/%d) nxt/lnk %p/%p\n",
1753 bus->glom.qlen, pfirst, pfirst->data,
1754 pfirst->len, pfirst->next,
1755 pfirst->prev);
Arend van Spriel05f38202013-09-25 13:05:46 +02001756 skb_unlink(pfirst, &bus->glom);
1757 brcmf_rx_frame(bus->sdiodev->dev, pfirst);
1758 bus->sdcnt.rxglompkts++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001759 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02001760
Arend van Spriel80969832012-06-09 22:51:44 +02001761 bus->sdcnt.rxglomframes++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001762 }
1763 return num;
1764}
1765
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001766static int brcmf_sdio_dcmd_resp_wait(struct brcmf_sdio *bus, uint *condition,
1767 bool *pending)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001768{
1769 DECLARE_WAITQUEUE(wait, current);
1770 int timeout = msecs_to_jiffies(DCMD_RESP_TIMEOUT);
1771
1772 /* Wait until control frame is available */
1773 add_wait_queue(&bus->dcmd_resp_wait, &wait);
1774 set_current_state(TASK_INTERRUPTIBLE);
1775
1776 while (!(*condition) && (!signal_pending(current) && timeout))
1777 timeout = schedule_timeout(timeout);
1778
1779 if (signal_pending(current))
1780 *pending = true;
1781
1782 set_current_state(TASK_RUNNING);
1783 remove_wait_queue(&bus->dcmd_resp_wait, &wait);
1784
1785 return timeout;
1786}
1787
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001788static int brcmf_sdio_dcmd_resp_wake(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001789{
1790 if (waitqueue_active(&bus->dcmd_resp_wait))
1791 wake_up_interruptible(&bus->dcmd_resp_wait);
1792
1793 return 0;
1794}
1795static void
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001796brcmf_sdio_read_control(struct brcmf_sdio *bus, u8 *hdr, uint len, uint doff)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001797{
1798 uint rdlen, pad;
Franky Lindd43a012012-11-05 16:22:22 -08001799 u8 *buf = NULL, *rbuf;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001800 int sdret;
1801
1802 brcmf_dbg(TRACE, "Enter\n");
1803
Franky Lindd43a012012-11-05 16:22:22 -08001804 if (bus->rxblen)
1805 buf = vzalloc(bus->rxblen);
Joe Perches14f8dc42013-02-07 11:46:27 +00001806 if (!buf)
Franky Lindd43a012012-11-05 16:22:22 -08001807 goto done;
Joe Perches14f8dc42013-02-07 11:46:27 +00001808
Franky Lindd43a012012-11-05 16:22:22 -08001809 rbuf = bus->rxbuf;
Arend van Spriel9b2d2f2a82013-11-29 12:25:14 +01001810 pad = ((unsigned long)rbuf % bus->head_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001811 if (pad)
Arend van Spriel9b2d2f2a82013-11-29 12:25:14 +01001812 rbuf += (bus->head_align - pad);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001813
1814 /* Copy the already-read portion over */
Franky Lindd43a012012-11-05 16:22:22 -08001815 memcpy(buf, hdr, BRCMF_FIRSTREAD);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001816 if (len <= BRCMF_FIRSTREAD)
1817 goto gotpkt;
1818
1819 /* Raise rdlen to next SDIO block to avoid tail command */
1820 rdlen = len - BRCMF_FIRSTREAD;
1821 if (bus->roundup && bus->blocksize && (rdlen > bus->blocksize)) {
1822 pad = bus->blocksize - (rdlen % bus->blocksize);
1823 if ((pad <= bus->roundup) && (pad < bus->blocksize) &&
Franky Linb01a6b32011-12-16 18:37:03 -08001824 ((len + pad) < bus->sdiodev->bus_if->maxctl))
Arend van Spriel5b435de2011-10-05 13:19:03 +02001825 rdlen += pad;
Arend van Spriel9b2d2f2a82013-11-29 12:25:14 +01001826 } else if (rdlen % bus->head_align) {
1827 rdlen += bus->head_align - (rdlen % bus->head_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001828 }
1829
Arend van Spriel5b435de2011-10-05 13:19:03 +02001830 /* Drop if the read is too big or it exceeds our maximum */
Franky Linb01a6b32011-12-16 18:37:03 -08001831 if ((rdlen + BRCMF_FIRSTREAD) > bus->sdiodev->bus_if->maxctl) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001832 brcmf_err("%d-byte control read exceeds %d-byte buffer\n",
Franky Linb01a6b32011-12-16 18:37:03 -08001833 rdlen, bus->sdiodev->bus_if->maxctl);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001834 brcmf_sdio_rxfail(bus, false, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001835 goto done;
1836 }
1837
Franky Linb01a6b32011-12-16 18:37:03 -08001838 if ((len - doff) > bus->sdiodev->bus_if->maxctl) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001839 brcmf_err("%d-byte ctl frame (%d-byte ctl data) exceeds %d-byte limit\n",
Franky Linb01a6b32011-12-16 18:37:03 -08001840 len, len - doff, bus->sdiodev->bus_if->maxctl);
Arend van Spriel80969832012-06-09 22:51:44 +02001841 bus->sdcnt.rx_toolong++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001842 brcmf_sdio_rxfail(bus, false, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001843 goto done;
1844 }
1845
Franky Lindd43a012012-11-05 16:22:22 -08001846 /* Read remain of frame body */
Arend van Spriela7cdd822013-12-12 11:58:59 +01001847 sdret = brcmf_sdiod_recv_buf(bus->sdiodev, rbuf, rdlen);
Arend van Spriel80969832012-06-09 22:51:44 +02001848 bus->sdcnt.f2rxdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001849
1850 /* Control frame failures need retransmission */
1851 if (sdret < 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001852 brcmf_err("read %d control bytes failed: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001853 rdlen, sdret);
Arend van Spriel80969832012-06-09 22:51:44 +02001854 bus->sdcnt.rxc_errors++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001855 brcmf_sdio_rxfail(bus, true, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001856 goto done;
Franky Lindd43a012012-11-05 16:22:22 -08001857 } else
1858 memcpy(buf + BRCMF_FIRSTREAD, rbuf, rdlen);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001859
1860gotpkt:
1861
Joe Perches1e023822012-01-15 00:38:40 -08001862 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_CTL_ON(),
Franky Lindd43a012012-11-05 16:22:22 -08001863 buf, len, "RxCtrl:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001864
1865 /* Point to valid data and indicate its length */
Franky Lindd43a012012-11-05 16:22:22 -08001866 spin_lock_bh(&bus->rxctl_lock);
1867 if (bus->rxctl) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001868 brcmf_err("last control frame is being processed.\n");
Franky Lindd43a012012-11-05 16:22:22 -08001869 spin_unlock_bh(&bus->rxctl_lock);
1870 vfree(buf);
1871 goto done;
1872 }
1873 bus->rxctl = buf + doff;
1874 bus->rxctl_orig = buf;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001875 bus->rxlen = len - doff;
Franky Lindd43a012012-11-05 16:22:22 -08001876 spin_unlock_bh(&bus->rxctl_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001877
1878done:
1879 /* Awake any waiters */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001880 brcmf_sdio_dcmd_resp_wake(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001881}
1882
1883/* Pad read to blocksize for efficiency */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001884static void brcmf_sdio_pad(struct brcmf_sdio *bus, u16 *pad, u16 *rdlen)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001885{
1886 if (bus->roundup && bus->blocksize && *rdlen > bus->blocksize) {
1887 *pad = bus->blocksize - (*rdlen % bus->blocksize);
1888 if (*pad <= bus->roundup && *pad < bus->blocksize &&
1889 *rdlen + *pad + BRCMF_FIRSTREAD < MAX_RX_DATASZ)
1890 *rdlen += *pad;
Arend van Spriel9b2d2f2a82013-11-29 12:25:14 +01001891 } else if (*rdlen % bus->head_align) {
1892 *rdlen += bus->head_align - (*rdlen % bus->head_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001893 }
1894}
1895
Franky Lin4754fce2012-09-19 22:21:11 +02001896static uint brcmf_sdio_readframes(struct brcmf_sdio *bus, uint maxframes)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001897{
Arend van Spriel5b435de2011-10-05 13:19:03 +02001898 struct sk_buff *pkt; /* Packet for event or data frames */
1899 u16 pad; /* Number of pad bytes to read */
Arend van Spriel5b435de2011-10-05 13:19:03 +02001900 uint rxleft = 0; /* Remaining number of frames allowed */
Arend van Spriel349e7102013-03-03 12:45:28 +01001901 int ret; /* Return code from calls */
Arend van Spriel5b435de2011-10-05 13:19:03 +02001902 uint rxcount = 0; /* Total frames read */
Franky Lin6bc52312013-08-10 12:27:28 +02001903 struct brcmf_sdio_hdrinfo *rd = &bus->cur_read, rd_new;
Franky Lin4754fce2012-09-19 22:21:11 +02001904 u8 head_read = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001905
1906 brcmf_dbg(TRACE, "Enter\n");
1907
1908 /* Not finished unless we encounter no more frames indication */
Franky Lin4754fce2012-09-19 22:21:11 +02001909 bus->rxpending = true;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001910
Franky Lin4754fce2012-09-19 22:21:11 +02001911 for (rd->seq_num = bus->rx_seq, rxleft = maxframes;
Arend van Sprielbb350712014-01-13 22:20:29 +01001912 !bus->rxskip && rxleft && brcmf_bus_ready(bus->sdiodev->bus_if);
Franky Lin4754fce2012-09-19 22:21:11 +02001913 rd->seq_num++, rxleft--) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001914
1915 /* Handle glomming separately */
Arend van Sprielb83db862011-10-19 12:51:09 +02001916 if (bus->glomd || !skb_queue_empty(&bus->glom)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001917 u8 cnt;
1918 brcmf_dbg(GLOM, "calling rxglom: glomd %p, glom %p\n",
Arend van Sprielb83db862011-10-19 12:51:09 +02001919 bus->glomd, skb_peek(&bus->glom));
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001920 cnt = brcmf_sdio_rxglom(bus, rd->seq_num);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001921 brcmf_dbg(GLOM, "rxglom returned %d\n", cnt);
Franky Lin4754fce2012-09-19 22:21:11 +02001922 rd->seq_num += cnt - 1;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001923 rxleft = (rxleft > cnt) ? (rxleft - cnt) : 1;
1924 continue;
1925 }
1926
Franky Lin4754fce2012-09-19 22:21:11 +02001927 rd->len_left = rd->len;
1928 /* read header first for unknow frame length */
Franky Lin38b0b0d2012-11-05 16:22:24 -08001929 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001930 if (!rd->len) {
Arend van Spriela39be272013-12-12 11:58:58 +01001931 ret = brcmf_sdiod_recv_buf(bus->sdiodev,
Arend van Spriela39be272013-12-12 11:58:58 +01001932 bus->rxhdr, BRCMF_FIRSTREAD);
Franky Lin4754fce2012-09-19 22:21:11 +02001933 bus->sdcnt.f2rxhdrs++;
Arend van Spriel349e7102013-03-03 12:45:28 +01001934 if (ret < 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001935 brcmf_err("RXHEADER FAILED: %d\n",
Arend van Spriel349e7102013-03-03 12:45:28 +01001936 ret);
Franky Lin4754fce2012-09-19 22:21:11 +02001937 bus->sdcnt.rx_hdrfail++;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001938 brcmf_sdio_rxfail(bus, true, true);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001939 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001940 continue;
1941 }
1942
Franky Lin4754fce2012-09-19 22:21:11 +02001943 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() || BRCMF_HDRS_ON(),
1944 bus->rxhdr, SDPCM_HDRLEN,
1945 "RxHdr:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001946
Franky Lin6bc52312013-08-10 12:27:28 +02001947 if (brcmf_sdio_hdparse(bus, bus->rxhdr, rd,
1948 BRCMF_SDIO_FT_NORMAL)) {
Franky Lin38b0b0d2012-11-05 16:22:24 -08001949 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001950 if (!bus->rxpending)
1951 break;
1952 else
1953 continue;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001954 }
1955
Franky Lin4754fce2012-09-19 22:21:11 +02001956 if (rd->channel == SDPCM_CONTROL_CHANNEL) {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001957 brcmf_sdio_read_control(bus, bus->rxhdr,
1958 rd->len,
1959 rd->dat_offset);
Franky Lin4754fce2012-09-19 22:21:11 +02001960 /* prepare the descriptor for the next read */
1961 rd->len = rd->len_nxtfrm << 4;
1962 rd->len_nxtfrm = 0;
1963 /* treat all packet as event if we don't know */
1964 rd->channel = SDPCM_EVENT_CHANNEL;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001965 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001966 continue;
1967 }
1968 rd->len_left = rd->len > BRCMF_FIRSTREAD ?
1969 rd->len - BRCMF_FIRSTREAD : 0;
1970 head_read = BRCMF_FIRSTREAD;
1971 }
1972
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001973 brcmf_sdio_pad(bus, &pad, &rd->len_left);
Franky Lin4754fce2012-09-19 22:21:11 +02001974
1975 pkt = brcmu_pkt_buf_get_skb(rd->len_left + head_read +
Arend van Spriel9b2d2f2a82013-11-29 12:25:14 +01001976 bus->head_align);
Franky Lin4754fce2012-09-19 22:21:11 +02001977 if (!pkt) {
1978 /* Give up on data, request rtx of events */
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001979 brcmf_err("brcmu_pkt_buf_get_skb failed\n");
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001980 brcmf_sdio_rxfail(bus, false,
Franky Lin4754fce2012-09-19 22:21:11 +02001981 RETRYCHAN(rd->channel));
Franky Lin38b0b0d2012-11-05 16:22:24 -08001982 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001983 continue;
1984 }
1985 skb_pull(pkt, head_read);
Arend van Spriel9b2d2f2a82013-11-29 12:25:14 +01001986 pkt_align(pkt, rd->len_left, bus->head_align);
Franky Lin4754fce2012-09-19 22:21:11 +02001987
Arend van Spriela7cdd822013-12-12 11:58:59 +01001988 ret = brcmf_sdiod_recv_pkt(bus->sdiodev, pkt);
Franky Lin4754fce2012-09-19 22:21:11 +02001989 bus->sdcnt.f2rxdata++;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001990 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001991
Arend van Spriel349e7102013-03-03 12:45:28 +01001992 if (ret < 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001993 brcmf_err("read %d bytes from channel %d failed: %d\n",
Arend van Spriel349e7102013-03-03 12:45:28 +01001994 rd->len, rd->channel, ret);
Franky Lin4754fce2012-09-19 22:21:11 +02001995 brcmu_pkt_buf_free_skb(pkt);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001996 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001997 brcmf_sdio_rxfail(bus, true,
Franky Lin4754fce2012-09-19 22:21:11 +02001998 RETRYCHAN(rd->channel));
Franky Lin38b0b0d2012-11-05 16:22:24 -08001999 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02002000 continue;
2001 }
2002
2003 if (head_read) {
2004 skb_push(pkt, head_read);
2005 memcpy(pkt->data, bus->rxhdr, head_read);
2006 head_read = 0;
2007 } else {
2008 memcpy(bus->rxhdr, pkt->data, SDPCM_HDRLEN);
2009 rd_new.seq_num = rd->seq_num;
Franky Lin38b0b0d2012-11-05 16:22:24 -08002010 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin6bc52312013-08-10 12:27:28 +02002011 if (brcmf_sdio_hdparse(bus, bus->rxhdr, &rd_new,
2012 BRCMF_SDIO_FT_NORMAL)) {
Franky Lin4754fce2012-09-19 22:21:11 +02002013 rd->len = 0;
2014 brcmu_pkt_buf_free_skb(pkt);
2015 }
Arend van Spriel80969832012-06-09 22:51:44 +02002016 bus->sdcnt.rx_readahead_cnt++;
Franky Lin4754fce2012-09-19 22:21:11 +02002017 if (rd->len != roundup(rd_new.len, 16)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002018 brcmf_err("frame length mismatch:read %d, should be %d\n",
Franky Lin4754fce2012-09-19 22:21:11 +02002019 rd->len,
2020 roundup(rd_new.len, 16) >> 4);
2021 rd->len = 0;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002022 brcmf_sdio_rxfail(bus, true, true);
Franky Lin38b0b0d2012-11-05 16:22:24 -08002023 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02002024 brcmu_pkt_buf_free_skb(pkt);
2025 continue;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002026 }
Franky Lin38b0b0d2012-11-05 16:22:24 -08002027 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02002028 rd->len_nxtfrm = rd_new.len_nxtfrm;
2029 rd->channel = rd_new.channel;
2030 rd->dat_offset = rd_new.dat_offset;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002031
Joe Perches1e023822012-01-15 00:38:40 -08002032 brcmf_dbg_hex_dump(!(BRCMF_BYTES_ON() &&
2033 BRCMF_DATA_ON()) &&
2034 BRCMF_HDRS_ON(),
2035 bus->rxhdr, SDPCM_HDRLEN,
2036 "RxHdr:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002037
Franky Lin4754fce2012-09-19 22:21:11 +02002038 if (rd_new.channel == SDPCM_CONTROL_CHANNEL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002039 brcmf_err("readahead on control packet %d?\n",
Franky Lin4754fce2012-09-19 22:21:11 +02002040 rd_new.seq_num);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002041 /* Force retry w/normal header read */
Franky Lin4754fce2012-09-19 22:21:11 +02002042 rd->len = 0;
Franky Lin38b0b0d2012-11-05 16:22:24 -08002043 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002044 brcmf_sdio_rxfail(bus, false, true);
Franky Lin38b0b0d2012-11-05 16:22:24 -08002045 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02002046 brcmu_pkt_buf_free_skb(pkt);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002047 continue;
2048 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002049 }
2050
Joe Perches1e023822012-01-15 00:38:40 -08002051 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_DATA_ON(),
Franky Lin4754fce2012-09-19 22:21:11 +02002052 pkt->data, rd->len, "Rx Data:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002053
Arend van Spriel5b435de2011-10-05 13:19:03 +02002054 /* Save superframe descriptor and allocate packet frame */
Franky Lin4754fce2012-09-19 22:21:11 +02002055 if (rd->channel == SDPCM_GLOM_CHANNEL) {
Franky Lin6bc52312013-08-10 12:27:28 +02002056 if (SDPCM_GLOMDESC(&bus->rxhdr[SDPCM_HWHDR_LEN])) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02002057 brcmf_dbg(GLOM, "glom descriptor, %d bytes:\n",
Franky Lin4754fce2012-09-19 22:21:11 +02002058 rd->len);
Joe Perches1e023822012-01-15 00:38:40 -08002059 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
Franky Lin4754fce2012-09-19 22:21:11 +02002060 pkt->data, rd->len,
Joe Perches1e023822012-01-15 00:38:40 -08002061 "Glom Data:\n");
Franky Lin4754fce2012-09-19 22:21:11 +02002062 __skb_trim(pkt, rd->len);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002063 skb_pull(pkt, SDPCM_HDRLEN);
2064 bus->glomd = pkt;
2065 } else {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002066 brcmf_err("%s: glom superframe w/o "
Arend van Spriel5b435de2011-10-05 13:19:03 +02002067 "descriptor!\n", __func__);
Franky Lin38b0b0d2012-11-05 16:22:24 -08002068 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002069 brcmf_sdio_rxfail(bus, false, false);
Franky Lin38b0b0d2012-11-05 16:22:24 -08002070 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002071 }
Franky Lin4754fce2012-09-19 22:21:11 +02002072 /* prepare the descriptor for the next read */
2073 rd->len = rd->len_nxtfrm << 4;
2074 rd->len_nxtfrm = 0;
2075 /* treat all packet as event if we don't know */
2076 rd->channel = SDPCM_EVENT_CHANNEL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002077 continue;
2078 }
2079
2080 /* Fill in packet len and prio, deliver upward */
Franky Lin4754fce2012-09-19 22:21:11 +02002081 __skb_trim(pkt, rd->len);
2082 skb_pull(pkt, rd->dat_offset);
2083
2084 /* prepare the descriptor for the next read */
2085 rd->len = rd->len_nxtfrm << 4;
2086 rd->len_nxtfrm = 0;
2087 /* treat all packet as event if we don't know */
2088 rd->channel = SDPCM_EVENT_CHANNEL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002089
2090 if (pkt->len == 0) {
2091 brcmu_pkt_buf_free_skb(pkt);
2092 continue;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002093 }
2094
Arend van Spriel05f38202013-09-25 13:05:46 +02002095 brcmf_rx_frame(bus->sdiodev->dev, pkt);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002096 }
Franky Lin4754fce2012-09-19 22:21:11 +02002097
Arend van Spriel5b435de2011-10-05 13:19:03 +02002098 rxcount = maxframes - rxleft;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002099 /* Message if we hit the limit */
2100 if (!rxleft)
Franky Lin4754fce2012-09-19 22:21:11 +02002101 brcmf_dbg(DATA, "hit rx limit of %d frames\n", maxframes);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002102 else
Arend van Spriel5b435de2011-10-05 13:19:03 +02002103 brcmf_dbg(DATA, "processed %d frames\n", rxcount);
2104 /* Back off rxseq if awaiting rtx, update rx_seq */
2105 if (bus->rxskip)
Franky Lin4754fce2012-09-19 22:21:11 +02002106 rd->seq_num--;
2107 bus->rx_seq = rd->seq_num;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002108
2109 return rxcount;
2110}
2111
Arend van Spriel5b435de2011-10-05 13:19:03 +02002112static void
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002113brcmf_sdio_wait_event_wakeup(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002114{
2115 if (waitqueue_active(&bus->ctrl_wait))
2116 wake_up_interruptible(&bus->ctrl_wait);
2117 return;
2118}
2119
Franky Lin8da9d2c2013-11-29 12:25:10 +01002120static int brcmf_sdio_txpkt_hdalign(struct brcmf_sdio *bus, struct sk_buff *pkt)
2121{
Arend van Spriele217d1c2013-11-29 12:25:13 +01002122 u16 head_pad;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002123 u8 *dat_buf;
2124
Franky Lin8da9d2c2013-11-29 12:25:10 +01002125 dat_buf = (u8 *)(pkt->data);
2126
2127 /* Check head padding */
Arend van Spriele217d1c2013-11-29 12:25:13 +01002128 head_pad = ((unsigned long)dat_buf % bus->head_align);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002129 if (head_pad) {
2130 if (skb_headroom(pkt) < head_pad) {
2131 bus->sdiodev->bus_if->tx_realloc++;
2132 head_pad = 0;
2133 if (skb_cow(pkt, head_pad))
2134 return -ENOMEM;
2135 }
2136 skb_push(pkt, head_pad);
2137 dat_buf = (u8 *)(pkt->data);
2138 memset(dat_buf, 0, head_pad + bus->tx_hdrlen);
2139 }
2140 return head_pad;
2141}
2142
Franky Lin5491c112013-09-25 13:05:43 +02002143/**
2144 * struct brcmf_skbuff_cb reserves first two bytes in sk_buff::cb for
2145 * bus layer usage.
2146 */
Franky Linb05e9252013-08-10 12:27:26 +02002147/* flag marking a dummy skb added for DMA alignment requirement */
Franky Lin5491c112013-09-25 13:05:43 +02002148#define ALIGN_SKB_FLAG 0x8000
Franky Linb05e9252013-08-10 12:27:26 +02002149/* bit mask of data length chopped from the previous packet */
Franky Lin5491c112013-09-25 13:05:43 +02002150#define ALIGN_SKB_CHOP_LEN_MASK 0x7fff
2151
Franky Lin8da9d2c2013-11-29 12:25:10 +01002152static int brcmf_sdio_txpkt_prep_sg(struct brcmf_sdio *bus,
Arend van Spriela64304f2013-10-15 15:44:56 +02002153 struct sk_buff_head *pktq,
Franky Lin8da9d2c2013-11-29 12:25:10 +01002154 struct sk_buff *pkt, u16 total_len)
Arend van Spriela64304f2013-10-15 15:44:56 +02002155{
Franky Lin8da9d2c2013-11-29 12:25:10 +01002156 struct brcmf_sdio_dev *sdiodev;
Arend van Spriela64304f2013-10-15 15:44:56 +02002157 struct sk_buff *pkt_pad;
Arend van Spriele217d1c2013-11-29 12:25:13 +01002158 u16 tail_pad, tail_chop, chain_pad;
Arend van Spriela64304f2013-10-15 15:44:56 +02002159 unsigned int blksize;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002160 bool lastfrm;
2161 int ntail, ret;
Arend van Spriela64304f2013-10-15 15:44:56 +02002162
Franky Lin8da9d2c2013-11-29 12:25:10 +01002163 sdiodev = bus->sdiodev;
Arend van Spriela64304f2013-10-15 15:44:56 +02002164 blksize = sdiodev->func[SDIO_FUNC_2]->cur_blksize;
Arend van Spriela64304f2013-10-15 15:44:56 +02002165 /* sg entry alignment should be a divisor of block size */
Arend van Spriele217d1c2013-11-29 12:25:13 +01002166 WARN_ON(blksize % bus->sgentry_align);
Arend van Spriela64304f2013-10-15 15:44:56 +02002167
2168 /* Check tail padding */
Franky Lin8da9d2c2013-11-29 12:25:10 +01002169 lastfrm = skb_queue_is_last(pktq, pkt);
2170 tail_pad = 0;
Arend van Spriele217d1c2013-11-29 12:25:13 +01002171 tail_chop = pkt->len % bus->sgentry_align;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002172 if (tail_chop)
Arend van Spriele217d1c2013-11-29 12:25:13 +01002173 tail_pad = bus->sgentry_align - tail_chop;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002174 chain_pad = (total_len + tail_pad) % blksize;
2175 if (lastfrm && chain_pad)
2176 tail_pad += blksize - chain_pad;
Arend van Spriela64304f2013-10-15 15:44:56 +02002177 if (skb_tailroom(pkt) < tail_pad && pkt->len > blksize) {
Arend van Spriel1eb43012014-02-20 18:55:55 +01002178 pkt_pad = brcmu_pkt_buf_get_skb(tail_pad + tail_chop +
2179 bus->head_align);
Arend van Spriela64304f2013-10-15 15:44:56 +02002180 if (pkt_pad == NULL)
2181 return -ENOMEM;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002182 ret = brcmf_sdio_txpkt_hdalign(bus, pkt_pad);
Dave Jones2dc3a8e2014-03-06 15:51:08 -05002183 if (unlikely(ret < 0)) {
2184 kfree_skb(pkt_pad);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002185 return ret;
Dave Jones2dc3a8e2014-03-06 15:51:08 -05002186 }
Arend van Spriela64304f2013-10-15 15:44:56 +02002187 memcpy(pkt_pad->data,
2188 pkt->data + pkt->len - tail_chop,
2189 tail_chop);
Arend van Spriel5aa9f0e2014-02-25 20:30:27 +01002190 *(u16 *)(pkt_pad->cb) = ALIGN_SKB_FLAG + tail_chop;
Arend van Spriela64304f2013-10-15 15:44:56 +02002191 skb_trim(pkt, pkt->len - tail_chop);
Arend van Spriel1eb43012014-02-20 18:55:55 +01002192 skb_trim(pkt_pad, tail_pad + tail_chop);
Arend van Spriela64304f2013-10-15 15:44:56 +02002193 __skb_queue_after(pktq, pkt, pkt_pad);
2194 } else {
2195 ntail = pkt->data_len + tail_pad -
2196 (pkt->end - pkt->tail);
2197 if (skb_cloned(pkt) || ntail > 0)
2198 if (pskb_expand_head(pkt, 0, ntail, GFP_ATOMIC))
2199 return -ENOMEM;
2200 if (skb_linearize(pkt))
2201 return -ENOMEM;
Arend van Spriela64304f2013-10-15 15:44:56 +02002202 __skb_put(pkt, tail_pad);
2203 }
2204
Franky Lin8da9d2c2013-11-29 12:25:10 +01002205 return tail_pad;
Arend van Spriela64304f2013-10-15 15:44:56 +02002206}
2207
Franky Linb05e9252013-08-10 12:27:26 +02002208/**
2209 * brcmf_sdio_txpkt_prep - packet preparation for transmit
2210 * @bus: brcmf_sdio structure pointer
2211 * @pktq: packet list pointer
2212 * @chan: virtual channel to transmit the packet
2213 *
2214 * Processes to be applied to the packet
2215 * - Align data buffer pointer
2216 * - Align data buffer length
2217 * - Prepare header
2218 * Return: negative value if there is error
2219 */
2220static int
2221brcmf_sdio_txpkt_prep(struct brcmf_sdio *bus, struct sk_buff_head *pktq,
2222 uint chan)
2223{
Franky Lin8da9d2c2013-11-29 12:25:10 +01002224 u16 head_pad, total_len;
Arend van Spriela64304f2013-10-15 15:44:56 +02002225 struct sk_buff *pkt_next;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002226 u8 txseq;
2227 int ret;
Franky Lin6bc52312013-08-10 12:27:28 +02002228 struct brcmf_sdio_hdrinfo hd_info = {0};
Franky Linb05e9252013-08-10 12:27:26 +02002229
Franky Lin8da9d2c2013-11-29 12:25:10 +01002230 txseq = bus->tx_seq;
2231 total_len = 0;
2232 skb_queue_walk(pktq, pkt_next) {
2233 /* alignment packet inserted in previous
2234 * loop cycle can be skipped as it is
2235 * already properly aligned and does not
2236 * need an sdpcm header.
2237 */
Arend van Spriel5aa9f0e2014-02-25 20:30:27 +01002238 if (*(u16 *)(pkt_next->cb) & ALIGN_SKB_FLAG)
Franky Lin8da9d2c2013-11-29 12:25:10 +01002239 continue;
Franky Linb05e9252013-08-10 12:27:26 +02002240
Franky Lin8da9d2c2013-11-29 12:25:10 +01002241 /* align packet data pointer */
2242 ret = brcmf_sdio_txpkt_hdalign(bus, pkt_next);
2243 if (ret < 0)
2244 return ret;
2245 head_pad = (u16)ret;
2246 if (head_pad)
Arend van Spriel1eb43012014-02-20 18:55:55 +01002247 memset(pkt_next->data + bus->tx_hdrlen, 0, head_pad);
Franky Linb05e9252013-08-10 12:27:26 +02002248
Franky Lin8da9d2c2013-11-29 12:25:10 +01002249 total_len += pkt_next->len;
Franky Linb05e9252013-08-10 12:27:26 +02002250
Arend van Spriela64304f2013-10-15 15:44:56 +02002251 hd_info.len = pkt_next->len;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002252 hd_info.lastfrm = skb_queue_is_last(pktq, pkt_next);
2253 if (bus->txglom && pktq->qlen > 1) {
2254 ret = brcmf_sdio_txpkt_prep_sg(bus, pktq,
2255 pkt_next, total_len);
2256 if (ret < 0)
2257 return ret;
2258 hd_info.tail_pad = (u16)ret;
2259 total_len += (u16)ret;
2260 }
2261
2262 hd_info.channel = chan;
2263 hd_info.dat_offset = head_pad + bus->tx_hdrlen;
2264 hd_info.seq_num = txseq++;
2265
2266 /* Now fill the header */
2267 brcmf_sdio_hdpack(bus, pkt_next->data, &hd_info);
2268
2269 if (BRCMF_BYTES_ON() &&
2270 ((BRCMF_CTL_ON() && chan == SDPCM_CONTROL_CHANNEL) ||
2271 (BRCMF_DATA_ON() && chan != SDPCM_CONTROL_CHANNEL)))
Hante Meuleman47ab4cd2014-02-25 20:30:29 +01002272 brcmf_dbg_hex_dump(true, pkt_next->data, hd_info.len,
Franky Lin8da9d2c2013-11-29 12:25:10 +01002273 "Tx Frame:\n");
2274 else if (BRCMF_HDRS_ON())
Hante Meuleman47ab4cd2014-02-25 20:30:29 +01002275 brcmf_dbg_hex_dump(true, pkt_next->data,
Franky Lin8da9d2c2013-11-29 12:25:10 +01002276 head_pad + bus->tx_hdrlen,
2277 "Tx Header:\n");
Franky Linb05e9252013-08-10 12:27:26 +02002278 }
Franky Lin8da9d2c2013-11-29 12:25:10 +01002279 /* Hardware length tag of the first packet should be total
2280 * length of the chain (including padding)
2281 */
2282 if (bus->txglom)
2283 brcmf_sdio_update_hwhdr(pktq->next->data, total_len);
Franky Linb05e9252013-08-10 12:27:26 +02002284 return 0;
2285}
2286
2287/**
2288 * brcmf_sdio_txpkt_postp - packet post processing for transmit
2289 * @bus: brcmf_sdio structure pointer
2290 * @pktq: packet list pointer
2291 *
2292 * Processes to be applied to the packet
2293 * - Remove head padding
2294 * - Remove tail padding
2295 */
2296static void
2297brcmf_sdio_txpkt_postp(struct brcmf_sdio *bus, struct sk_buff_head *pktq)
2298{
2299 u8 *hdr;
2300 u32 dat_offset;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002301 u16 tail_pad;
Arend van Spriel5aa9f0e2014-02-25 20:30:27 +01002302 u16 dummy_flags, chop_len;
Franky Linb05e9252013-08-10 12:27:26 +02002303 struct sk_buff *pkt_next, *tmp, *pkt_prev;
2304
2305 skb_queue_walk_safe(pktq, pkt_next, tmp) {
Arend van Spriel5aa9f0e2014-02-25 20:30:27 +01002306 dummy_flags = *(u16 *)(pkt_next->cb);
Franky Lin5491c112013-09-25 13:05:43 +02002307 if (dummy_flags & ALIGN_SKB_FLAG) {
2308 chop_len = dummy_flags & ALIGN_SKB_CHOP_LEN_MASK;
Franky Linb05e9252013-08-10 12:27:26 +02002309 if (chop_len) {
2310 pkt_prev = pkt_next->prev;
Franky Linb05e9252013-08-10 12:27:26 +02002311 skb_put(pkt_prev, chop_len);
2312 }
2313 __skb_unlink(pkt_next, pktq);
2314 brcmu_pkt_buf_free_skb(pkt_next);
2315 } else {
Franky Lin8da9d2c2013-11-29 12:25:10 +01002316 hdr = pkt_next->data + bus->tx_hdrlen - SDPCM_SWHDR_LEN;
Franky Linb05e9252013-08-10 12:27:26 +02002317 dat_offset = le32_to_cpu(*(__le32 *)hdr);
2318 dat_offset = (dat_offset & SDPCM_DOFFSET_MASK) >>
2319 SDPCM_DOFFSET_SHIFT;
2320 skb_pull(pkt_next, dat_offset);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002321 if (bus->txglom) {
2322 tail_pad = le16_to_cpu(*(__le16 *)(hdr - 2));
2323 skb_trim(pkt_next, pkt_next->len - tail_pad);
2324 }
Franky Linb05e9252013-08-10 12:27:26 +02002325 }
2326 }
2327}
2328
Arend van Spriel5b435de2011-10-05 13:19:03 +02002329/* Writes a HW/SW header into the packet and sends it. */
2330/* Assumes: (a) header space already there, (b) caller holds lock */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002331static int brcmf_sdio_txpkt(struct brcmf_sdio *bus, struct sk_buff_head *pktq,
2332 uint chan)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002333{
2334 int ret;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002335 struct sk_buff *pkt_next, *tmp;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002336
2337 brcmf_dbg(TRACE, "Enter\n");
2338
Franky Lin8da9d2c2013-11-29 12:25:10 +01002339 ret = brcmf_sdio_txpkt_prep(bus, pktq, chan);
Franky Linb05e9252013-08-10 12:27:26 +02002340 if (ret)
2341 goto done;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002342
Franky Lin38b0b0d2012-11-05 16:22:24 -08002343 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriela7cdd822013-12-12 11:58:59 +01002344 ret = brcmf_sdiod_send_pkt(bus->sdiodev, pktq);
Arend van Spriel80969832012-06-09 22:51:44 +02002345 bus->sdcnt.f2txdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002346
Hante Meuleman81c78832014-02-25 20:30:35 +01002347 if (ret < 0)
2348 brcmf_sdio_txfail(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002349
Franky Lin38b0b0d2012-11-05 16:22:24 -08002350 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002351
2352done:
Franky Lin8da9d2c2013-11-29 12:25:10 +01002353 brcmf_sdio_txpkt_postp(bus, pktq);
2354 if (ret == 0)
2355 bus->tx_seq = (bus->tx_seq + pktq->qlen) % SDPCM_SEQ_WRAP;
2356 skb_queue_walk_safe(pktq, pkt_next, tmp) {
2357 __skb_unlink(pkt_next, pktq);
2358 brcmf_txcomplete(bus->sdiodev->dev, pkt_next, ret == 0);
2359 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002360 return ret;
2361}
2362
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002363static uint brcmf_sdio_sendfromq(struct brcmf_sdio *bus, uint maxframes)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002364{
2365 struct sk_buff *pkt;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002366 struct sk_buff_head pktq;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002367 u32 intstatus = 0;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002368 int ret = 0, prec_out, i;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002369 uint cnt = 0;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002370 u8 tx_prec_map, pkt_num;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002371
Arend van Spriel5b435de2011-10-05 13:19:03 +02002372 brcmf_dbg(TRACE, "Enter\n");
2373
2374 tx_prec_map = ~bus->flowcontrol;
2375
2376 /* Send frames until the limit or some other event */
Franky Lin8da9d2c2013-11-29 12:25:10 +01002377 for (cnt = 0; (cnt < maxframes) && data_ok(bus);) {
2378 pkt_num = 1;
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002379 if (down_interruptible(&bus->tx_seq_lock))
2380 return cnt;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002381 if (bus->txglom)
2382 pkt_num = min_t(u8, bus->tx_max - bus->tx_seq,
Arend van Sprielaf1fa212014-02-27 19:25:00 +01002383 bus->sdiodev->txglomsz);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002384 pkt_num = min_t(u32, pkt_num,
2385 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol));
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002386 __skb_queue_head_init(&pktq);
2387 spin_lock_bh(&bus->txq_lock);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002388 for (i = 0; i < pkt_num; i++) {
2389 pkt = brcmu_pktq_mdeq(&bus->txq, tx_prec_map,
2390 &prec_out);
2391 if (pkt == NULL)
2392 break;
2393 __skb_queue_tail(&pktq, pkt);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002394 }
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002395 spin_unlock_bh(&bus->txq_lock);
2396 if (i == 0) {
2397 up(&bus->tx_seq_lock);
Franky Lin8da9d2c2013-11-29 12:25:10 +01002398 break;
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002399 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002400
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002401 ret = brcmf_sdio_txpkt(bus, &pktq, SDPCM_DATA_CHANNEL);
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002402 up(&bus->tx_seq_lock);
2403
Franky Lin8da9d2c2013-11-29 12:25:10 +01002404 cnt += i;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002405
2406 /* In poll mode, need to check for other events */
Hante Meulemanb6a8cf22014-02-25 20:30:33 +01002407 if (!bus->intr) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02002408 /* Check device status, signal pending interrupt */
Franky Lin38b0b0d2012-11-05 16:22:24 -08002409 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin5c15c232012-05-04 18:27:37 -07002410 ret = r_sdreg32(bus, &intstatus,
2411 offsetof(struct sdpcmd_regs,
2412 intstatus));
Franky Lin38b0b0d2012-11-05 16:22:24 -08002413 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel80969832012-06-09 22:51:44 +02002414 bus->sdcnt.f2txdata++;
Franky Lin5c15c232012-05-04 18:27:37 -07002415 if (ret != 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002416 break;
2417 if (intstatus & bus->hostintmask)
Franky Lin1d382272012-09-13 21:11:59 +02002418 atomic_set(&bus->ipend, 1);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002419 }
2420 }
2421
2422 /* Deflow-control stack if needed */
Hante Meuleman05dde972013-02-06 18:40:36 +01002423 if ((bus->sdiodev->bus_if->state == BRCMF_BUS_DATA) &&
Franky Linc8bf3482011-12-16 18:37:08 -08002424 bus->txoff && (pktq_len(&bus->txq) < TXLOW)) {
Hante Meuleman90d03ff2012-09-11 21:18:46 +02002425 bus->txoff = false;
2426 brcmf_txflowblock(bus->sdiodev->dev, false);
Franky Linc8bf3482011-12-16 18:37:08 -08002427 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002428
2429 return cnt;
2430}
2431
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002432static int brcmf_sdio_tx_ctrlframe(struct brcmf_sdio *bus, u8 *frame, u16 len)
2433{
2434 u8 doff;
2435 u16 pad;
2436 uint retries = 0;
2437 struct brcmf_sdio_hdrinfo hd_info = {0};
2438 int ret;
2439
2440 brcmf_dbg(TRACE, "Enter\n");
2441
2442 /* Back the pointer to make room for bus header */
2443 frame -= bus->tx_hdrlen;
2444 len += bus->tx_hdrlen;
2445
2446 /* Add alignment padding (optional for ctl frames) */
2447 doff = ((unsigned long)frame % bus->head_align);
2448 if (doff) {
2449 frame -= doff;
2450 len += doff;
2451 memset(frame + bus->tx_hdrlen, 0, doff);
2452 }
2453
2454 /* Round send length to next SDIO block */
2455 pad = 0;
2456 if (bus->roundup && bus->blocksize && (len > bus->blocksize)) {
2457 pad = bus->blocksize - (len % bus->blocksize);
2458 if ((pad > bus->roundup) || (pad >= bus->blocksize))
2459 pad = 0;
2460 } else if (len % bus->head_align) {
2461 pad = bus->head_align - (len % bus->head_align);
2462 }
2463 len += pad;
2464
2465 hd_info.len = len - pad;
2466 hd_info.channel = SDPCM_CONTROL_CHANNEL;
2467 hd_info.dat_offset = doff + bus->tx_hdrlen;
2468 hd_info.seq_num = bus->tx_seq;
2469 hd_info.lastfrm = true;
2470 hd_info.tail_pad = pad;
2471 brcmf_sdio_hdpack(bus, frame, &hd_info);
2472
2473 if (bus->txglom)
2474 brcmf_sdio_update_hwhdr(frame, len);
2475
2476 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_CTL_ON(),
2477 frame, len, "Tx Frame:\n");
2478 brcmf_dbg_hex_dump(!(BRCMF_BYTES_ON() && BRCMF_CTL_ON()) &&
2479 BRCMF_HDRS_ON(),
2480 frame, min_t(u16, len, 16), "TxHdr:\n");
2481
2482 do {
2483 ret = brcmf_sdiod_send_buf(bus->sdiodev, frame, len);
2484
2485 if (ret < 0)
2486 brcmf_sdio_txfail(bus);
2487 else
2488 bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQ_WRAP;
2489 } while (ret < 0 && retries++ < TXRETRIES);
2490
2491 return ret;
2492}
2493
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002494static void brcmf_sdio_bus_stop(struct device *dev)
Franky Lina9ffda82011-12-16 18:37:12 -08002495{
2496 u32 local_hostintmask;
2497 u8 saveclk;
Franky Lina9ffda82011-12-16 18:37:12 -08002498 int err;
2499 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01002500 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Lina9ffda82011-12-16 18:37:12 -08002501 struct brcmf_sdio *bus = sdiodev->bus;
2502
2503 brcmf_dbg(TRACE, "Enter\n");
2504
2505 if (bus->watchdog_tsk) {
2506 send_sig(SIGTERM, bus->watchdog_tsk, 1);
2507 kthread_stop(bus->watchdog_tsk);
2508 bus->watchdog_tsk = NULL;
2509 }
2510
Arend van Sprielbb350712014-01-13 22:20:29 +01002511 if (bus_if->state == BRCMF_BUS_DOWN) {
2512 sdio_claim_host(sdiodev->func[1]);
Franky Lina9ffda82011-12-16 18:37:12 -08002513
Arend van Sprielbb350712014-01-13 22:20:29 +01002514 /* Enable clock for device interrupts */
2515 brcmf_sdio_bus_sleep(bus, false, false);
Franky Lina9ffda82011-12-16 18:37:12 -08002516
Arend van Sprielbb350712014-01-13 22:20:29 +01002517 /* Disable and clear interrupts at the chip level also */
2518 w_sdreg32(bus, 0, offsetof(struct sdpcmd_regs, hostintmask));
2519 local_hostintmask = bus->hostintmask;
2520 bus->hostintmask = 0;
Franky Lina9ffda82011-12-16 18:37:12 -08002521
Arend van Sprielbb350712014-01-13 22:20:29 +01002522 /* Force backplane clocks to assure F2 interrupt propagates */
2523 saveclk = brcmf_sdiod_regrb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
2524 &err);
2525 if (!err)
2526 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
2527 (saveclk | SBSDIO_FORCE_HT), &err);
2528 if (err)
2529 brcmf_err("Failed to force clock for F2: err %d\n",
2530 err);
Franky Lina9ffda82011-12-16 18:37:12 -08002531
Arend van Sprielbb350712014-01-13 22:20:29 +01002532 /* Turn off the bus (F2), free any pending packets */
2533 brcmf_dbg(INTR, "disable SDIO interrupts\n");
2534 sdio_disable_func(sdiodev->func[SDIO_FUNC_2]);
2535
2536 /* Clear any pending interrupts now that F2 is disabled */
2537 w_sdreg32(bus, local_hostintmask,
2538 offsetof(struct sdpcmd_regs, intstatus));
2539
2540 sdio_release_host(sdiodev->func[1]);
Franky Lina9ffda82011-12-16 18:37:12 -08002541 }
Franky Lina9ffda82011-12-16 18:37:12 -08002542 /* Clear the data packet queues */
2543 brcmu_pktq_flush(&bus->txq, true, NULL, NULL);
2544
2545 /* Clear any held glomming stuff */
2546 if (bus->glomd)
2547 brcmu_pkt_buf_free_skb(bus->glomd);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002548 brcmf_sdio_free_glom(bus);
Franky Lina9ffda82011-12-16 18:37:12 -08002549
2550 /* Clear rx control and wake any waiters */
Franky Lindd43a012012-11-05 16:22:22 -08002551 spin_lock_bh(&bus->rxctl_lock);
Franky Lina9ffda82011-12-16 18:37:12 -08002552 bus->rxlen = 0;
Franky Lindd43a012012-11-05 16:22:22 -08002553 spin_unlock_bh(&bus->rxctl_lock);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002554 brcmf_sdio_dcmd_resp_wake(bus);
Franky Lina9ffda82011-12-16 18:37:12 -08002555
2556 /* Reset some F2 state stuff */
2557 bus->rxskip = false;
2558 bus->tx_seq = bus->rx_seq = 0;
Franky Lina9ffda82011-12-16 18:37:12 -08002559}
2560
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002561static inline void brcmf_sdio_clrintr(struct brcmf_sdio *bus)
Franky Linba89bf12012-04-27 18:56:59 -07002562{
2563 unsigned long flags;
2564
Hante Meuleman668761a2013-04-12 10:55:55 +02002565 if (bus->sdiodev->oob_irq_requested) {
2566 spin_lock_irqsave(&bus->sdiodev->irq_en_lock, flags);
2567 if (!bus->sdiodev->irq_en && !atomic_read(&bus->ipend)) {
2568 enable_irq(bus->sdiodev->pdata->oob_irq_nr);
2569 bus->sdiodev->irq_en = true;
2570 }
2571 spin_unlock_irqrestore(&bus->sdiodev->irq_en_lock, flags);
Franky Linba89bf12012-04-27 18:56:59 -07002572 }
Franky Linba89bf12012-04-27 18:56:59 -07002573}
Franky Linba89bf12012-04-27 18:56:59 -07002574
Hante Meuleman5cbb9c22014-03-06 10:16:12 +01002575static void atomic_orr(int val, atomic_t *v)
2576{
2577 int old_val;
2578
2579 old_val = atomic_read(v);
2580 while (atomic_cmpxchg(v, old_val, val | old_val) != old_val)
2581 old_val = atomic_read(v);
2582}
2583
Franky Lin45316032012-09-13 21:12:03 +02002584static int brcmf_sdio_intr_rstatus(struct brcmf_sdio *bus)
2585{
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01002586 struct brcmf_core *buscore;
Franky Lin45316032012-09-13 21:12:03 +02002587 u32 addr;
2588 unsigned long val;
Hante Meuleman5cbb9c22014-03-06 10:16:12 +01002589 int ret;
Franky Lin45316032012-09-13 21:12:03 +02002590
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01002591 buscore = brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV);
2592 addr = buscore->base + offsetof(struct sdpcmd_regs, intstatus);
Franky Lin45316032012-09-13 21:12:03 +02002593
Arend van Spriela39be272013-12-12 11:58:58 +01002594 val = brcmf_sdiod_regrl(bus->sdiodev, addr, &ret);
Franky Lin45316032012-09-13 21:12:03 +02002595 bus->sdcnt.f1regdata++;
2596 if (ret != 0)
Hante Meuleman5cbb9c22014-03-06 10:16:12 +01002597 return ret;
Franky Lin45316032012-09-13 21:12:03 +02002598
2599 val &= bus->hostintmask;
2600 atomic_set(&bus->fcstate, !!(val & I_HMB_FC_STATE));
2601
2602 /* Clear interrupts */
2603 if (val) {
Arend van Spriela39be272013-12-12 11:58:58 +01002604 brcmf_sdiod_regwl(bus->sdiodev, addr, val, &ret);
Franky Lin45316032012-09-13 21:12:03 +02002605 bus->sdcnt.f1regdata++;
Hante Meuleman5cbb9c22014-03-06 10:16:12 +01002606 atomic_orr(val, &bus->intstatus);
Franky Lin45316032012-09-13 21:12:03 +02002607 }
2608
2609 return ret;
2610}
2611
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002612static void brcmf_sdio_dpc(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002613{
Franky Lin45316032012-09-13 21:12:03 +02002614 u32 newstatus = 0;
2615 unsigned long intstatus;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002616 uint txlimit = bus->txbound; /* Tx frames to send before resched */
Hante Meulemanb6a8cf22014-02-25 20:30:33 +01002617 uint framecnt; /* Temporary counter of tx/rx frames */
Hante Meuleman5cbb9c22014-03-06 10:16:12 +01002618 int err = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002619
2620 brcmf_dbg(TRACE, "Enter\n");
2621
Franky Lin38b0b0d2012-11-05 16:22:24 -08002622 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002623
2624 /* If waiting for HTAVAIL, check status */
Piotr Haber4a3da992013-04-11 13:28:46 +02002625 if (!bus->sr_enabled && bus->clkstate == CLK_PENDING) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02002626 u8 clkctl, devctl = 0;
2627
Joe Perches8ae74652012-01-15 00:38:38 -08002628#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02002629 /* Check for inconsistent device control */
Arend van Spriela39be272013-12-12 11:58:58 +01002630 devctl = brcmf_sdiod_regrb(bus->sdiodev,
2631 SBSDIO_DEVICE_CTL, &err);
Joe Perches8ae74652012-01-15 00:38:38 -08002632#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02002633
2634 /* Read CSR, if clock on switch to AVAIL, else ignore */
Arend van Spriela39be272013-12-12 11:58:58 +01002635 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
2636 SBSDIO_FUNC1_CHIPCLKCSR, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002637
Arend van Sprielc3203372013-04-03 12:40:44 +02002638 brcmf_dbg(SDIO, "DPC: PENDING, devctl 0x%02x clkctl 0x%02x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02002639 devctl, clkctl);
2640
2641 if (SBSDIO_HTAV(clkctl)) {
Arend van Spriela39be272013-12-12 11:58:58 +01002642 devctl = brcmf_sdiod_regrb(bus->sdiodev,
2643 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002644 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
Arend van Spriela39be272013-12-12 11:58:58 +01002645 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
2646 devctl, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002647 bus->clkstate = CLK_AVAIL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002648 }
2649 }
2650
Arend van Spriel5b435de2011-10-05 13:19:03 +02002651 /* Make sure backplane clock is on */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002652 brcmf_sdio_bus_sleep(bus, false, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002653
2654 /* Pending interrupt indicates new device status */
Franky Lin1d382272012-09-13 21:11:59 +02002655 if (atomic_read(&bus->ipend) > 0) {
2656 atomic_set(&bus->ipend, 0);
Franky Lin45316032012-09-13 21:12:03 +02002657 err = brcmf_sdio_intr_rstatus(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002658 }
2659
Franky Lin45316032012-09-13 21:12:03 +02002660 /* Start with leftover status bits */
2661 intstatus = atomic_xchg(&bus->intstatus, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002662
2663 /* Handle flow-control change: read new state in case our ack
2664 * crossed another change interrupt. If change still set, assume
2665 * FC ON for safety, let next loop through do the debounce.
2666 */
2667 if (intstatus & I_HMB_FC_CHANGE) {
2668 intstatus &= ~I_HMB_FC_CHANGE;
Franky Lin5c15c232012-05-04 18:27:37 -07002669 err = w_sdreg32(bus, I_HMB_FC_CHANGE,
2670 offsetof(struct sdpcmd_regs, intstatus));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002671
Franky Lin5c15c232012-05-04 18:27:37 -07002672 err = r_sdreg32(bus, &newstatus,
2673 offsetof(struct sdpcmd_regs, intstatus));
Arend van Spriel80969832012-06-09 22:51:44 +02002674 bus->sdcnt.f1regdata += 2;
Franky Lin45316032012-09-13 21:12:03 +02002675 atomic_set(&bus->fcstate,
2676 !!(newstatus & (I_HMB_FC_STATE | I_HMB_FC_CHANGE)));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002677 intstatus |= (newstatus & bus->hostintmask);
2678 }
2679
2680 /* Handle host mailbox indication */
2681 if (intstatus & I_HMB_HOST_INT) {
2682 intstatus &= ~I_HMB_HOST_INT;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002683 intstatus |= brcmf_sdio_hostmail(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002684 }
2685
Franky Lin38b0b0d2012-11-05 16:22:24 -08002686 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin7cdf57d2012-11-05 16:22:23 -08002687
Arend van Spriel5b435de2011-10-05 13:19:03 +02002688 /* Generally don't ask for these, can get CRC errors... */
2689 if (intstatus & I_WR_OOSYNC) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002690 brcmf_err("Dongle reports WR_OOSYNC\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002691 intstatus &= ~I_WR_OOSYNC;
2692 }
2693
2694 if (intstatus & I_RD_OOSYNC) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002695 brcmf_err("Dongle reports RD_OOSYNC\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002696 intstatus &= ~I_RD_OOSYNC;
2697 }
2698
2699 if (intstatus & I_SBINT) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002700 brcmf_err("Dongle reports SBINT\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002701 intstatus &= ~I_SBINT;
2702 }
2703
2704 /* Would be active due to wake-wlan in gSPI */
2705 if (intstatus & I_CHIPACTIVE) {
2706 brcmf_dbg(INFO, "Dongle reports CHIPACTIVE\n");
2707 intstatus &= ~I_CHIPACTIVE;
2708 }
2709
2710 /* Ignore frame indications if rxskip is set */
2711 if (bus->rxskip)
2712 intstatus &= ~I_HMB_FRAME_IND;
2713
2714 /* On frame indication, read available frames */
Hante Meulemanb6a8cf22014-02-25 20:30:33 +01002715 if ((intstatus & I_HMB_FRAME_IND) && (bus->clkstate == CLK_AVAIL)) {
2716 brcmf_sdio_readframes(bus, bus->rxbound);
Franky Lin4754fce2012-09-19 22:21:11 +02002717 if (!bus->rxpending)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002718 intstatus &= ~I_HMB_FRAME_IND;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002719 }
2720
2721 /* Keep still-pending events for next scheduling */
Hante Meuleman5cbb9c22014-03-06 10:16:12 +01002722 if (intstatus)
2723 atomic_orr(intstatus, &bus->intstatus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002724
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002725 brcmf_sdio_clrintr(bus);
Franky Linba89bf12012-04-27 18:56:59 -07002726
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002727 if (bus->ctrl_frame_stat && (bus->clkstate == CLK_AVAIL) &&
2728 (down_interruptible(&bus->tx_seq_lock) == 0)) {
2729 if (data_ok(bus)) {
2730 sdio_claim_host(bus->sdiodev->func[1]);
2731 err = brcmf_sdio_tx_ctrlframe(bus, bus->ctrl_frame_buf,
2732 bus->ctrl_frame_len);
2733 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002734
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002735 bus->ctrl_frame_stat = false;
2736 brcmf_sdio_wait_event_wakeup(bus);
2737 }
2738 up(&bus->tx_seq_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002739 }
2740 /* Send queued frames (limit 1 if rx may still be pending) */
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002741 if ((bus->clkstate == CLK_AVAIL) && !atomic_read(&bus->fcstate) &&
2742 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) && txlimit &&
2743 data_ok(bus)) {
Franky Lin4754fce2012-09-19 22:21:11 +02002744 framecnt = bus->rxpending ? min(txlimit, bus->txminmax) :
2745 txlimit;
Hante Meulemanb6a8cf22014-02-25 20:30:33 +01002746 brcmf_sdio_sendfromq(bus, framecnt);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002747 }
2748
Arend van Sprielbb350712014-01-13 22:20:29 +01002749 if (!brcmf_bus_ready(bus->sdiodev->bus_if) || (err != 0)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002750 brcmf_err("failed backplane access over SDIO, halting operation\n");
Franky Lin45316032012-09-13 21:12:03 +02002751 atomic_set(&bus->intstatus, 0);
2752 } else if (atomic_read(&bus->intstatus) ||
2753 atomic_read(&bus->ipend) > 0 ||
2754 (!atomic_read(&bus->fcstate) &&
2755 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) &&
Hante Meulemanb6a8cf22014-02-25 20:30:33 +01002756 data_ok(bus))) {
Arend van Sprielfccfe932013-06-18 13:29:26 +02002757 atomic_inc(&bus->dpc_tskcnt);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002758 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002759}
2760
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002761static struct pktq *brcmf_sdio_bus_gettxq(struct device *dev)
Arend van Spriele2432b62013-04-03 12:40:38 +02002762{
2763 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
2764 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
2765 struct brcmf_sdio *bus = sdiodev->bus;
2766
2767 return &bus->txq;
2768}
2769
Hante Meuleman84936622014-10-28 14:56:19 +01002770static bool brcmf_sdio_prec_enq(struct pktq *q, struct sk_buff *pkt, int prec)
2771{
2772 struct sk_buff *p;
2773 int eprec = -1; /* precedence to evict from */
2774
2775 /* Fast case, precedence queue is not full and we are also not
2776 * exceeding total queue length
2777 */
2778 if (!pktq_pfull(q, prec) && !pktq_full(q)) {
2779 brcmu_pktq_penq(q, prec, pkt);
2780 return true;
2781 }
2782
2783 /* Determine precedence from which to evict packet, if any */
2784 if (pktq_pfull(q, prec)) {
2785 eprec = prec;
2786 } else if (pktq_full(q)) {
2787 p = brcmu_pktq_peek_tail(q, &eprec);
2788 if (eprec > prec)
2789 return false;
2790 }
2791
2792 /* Evict if needed */
2793 if (eprec >= 0) {
2794 /* Detect queueing to unconfigured precedence */
2795 if (eprec == prec)
2796 return false; /* refuse newer (incoming) packet */
2797 /* Evict packet according to discard policy */
2798 p = brcmu_pktq_pdeq_tail(q, eprec);
2799 if (p == NULL)
2800 brcmf_err("brcmu_pktq_pdeq_tail() failed\n");
2801 brcmu_pkt_buf_free_skb(p);
2802 }
2803
2804 /* Enqueue */
2805 p = brcmu_pktq_penq(q, prec, pkt);
2806 if (p == NULL)
2807 brcmf_err("brcmu_pktq_penq() failed\n");
2808
2809 return p != NULL;
2810}
2811
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002812static int brcmf_sdio_bus_txdata(struct device *dev, struct sk_buff *pkt)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002813{
2814 int ret = -EBADE;
Arend van Spriel44ff5662014-02-25 20:30:28 +01002815 uint prec;
Franky Linbf347bb2011-11-22 17:21:56 -08002816 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01002817 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Linbf347bb2011-11-22 17:21:56 -08002818 struct brcmf_sdio *bus = sdiodev->bus;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002819
Arend van Spriel44ff5662014-02-25 20:30:28 +01002820 brcmf_dbg(TRACE, "Enter: pkt: data %p len %d\n", pkt->data, pkt->len);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002821
2822 /* Add space for the header */
Franky Lin706478c2013-08-10 12:27:29 +02002823 skb_push(pkt, bus->tx_hdrlen);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002824 /* precondition: IS_ALIGNED((unsigned long)(pkt->data), 2) */
2825
2826 prec = prio2prec((pkt->priority & PRIOMASK));
2827
2828 /* Check for existing queue, current flow-control,
2829 pending event, or pending clock */
2830 brcmf_dbg(TRACE, "deferring pktq len %d\n", pktq_len(&bus->txq));
Arend van Spriel80969832012-06-09 22:51:44 +02002831 bus->sdcnt.fcqueued++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002832
2833 /* Priority based enq */
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002834 spin_lock_bh(&bus->txq_lock);
Arend van Spriel5aa9f0e2014-02-25 20:30:27 +01002835 /* reset bus_flags in packet cb */
2836 *(u16 *)(pkt->cb) = 0;
Hante Meuleman84936622014-10-28 14:56:19 +01002837 if (!brcmf_sdio_prec_enq(&bus->txq, pkt, prec)) {
Franky Lin706478c2013-08-10 12:27:29 +02002838 skb_pull(pkt, bus->tx_hdrlen);
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002839 brcmf_err("out of bus->txq !!!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002840 ret = -ENOSR;
2841 } else {
2842 ret = 0;
2843 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002844
Franky Linc8bf3482011-12-16 18:37:08 -08002845 if (pktq_len(&bus->txq) >= TXHI) {
Hante Meuleman90d03ff2012-09-11 21:18:46 +02002846 bus->txoff = true;
Hante Meuleman84936622014-10-28 14:56:19 +01002847 brcmf_txflowblock(dev, true);
Franky Linc8bf3482011-12-16 18:37:08 -08002848 }
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002849 spin_unlock_bh(&bus->txq_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002850
Joe Perches8ae74652012-01-15 00:38:38 -08002851#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02002852 if (pktq_plen(&bus->txq, prec) > qcount[prec])
2853 qcount[prec] = pktq_plen(&bus->txq, prec);
2854#endif
Franky Linf1e68c22012-09-13 21:12:00 +02002855
Arend van Sprielfccfe932013-06-18 13:29:26 +02002856 if (atomic_read(&bus->dpc_tskcnt) == 0) {
2857 atomic_inc(&bus->dpc_tskcnt);
Franky Linf1e68c22012-09-13 21:12:00 +02002858 queue_work(bus->brcmf_wq, &bus->datawork);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002859 }
2860
2861 return ret;
2862}
2863
Joe Perches8ae74652012-01-15 00:38:38 -08002864#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02002865#define CONSOLE_LINE_MAX 192
2866
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002867static int brcmf_sdio_readconsole(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002868{
2869 struct brcmf_console *c = &bus->console;
2870 u8 line[CONSOLE_LINE_MAX], ch;
2871 u32 n, idx, addr;
2872 int rv;
2873
2874 /* Don't do anything until FWREADY updates console address */
2875 if (bus->console_addr == 0)
2876 return 0;
2877
2878 /* Read console log struct */
2879 addr = bus->console_addr + offsetof(struct rte_console, log_le);
Arend van Spriela39be272013-12-12 11:58:58 +01002880 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, (u8 *)&c->log_le,
2881 sizeof(c->log_le));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002882 if (rv < 0)
2883 return rv;
2884
2885 /* Allocate console buffer (one time only) */
2886 if (c->buf == NULL) {
2887 c->bufsize = le32_to_cpu(c->log_le.buf_size);
2888 c->buf = kmalloc(c->bufsize, GFP_ATOMIC);
2889 if (c->buf == NULL)
2890 return -ENOMEM;
2891 }
2892
2893 idx = le32_to_cpu(c->log_le.idx);
2894
2895 /* Protect against corrupt value */
2896 if (idx > c->bufsize)
2897 return -EBADE;
2898
2899 /* Skip reading the console buffer if the index pointer
2900 has not moved */
2901 if (idx == c->last)
2902 return 0;
2903
2904 /* Read the console buffer */
2905 addr = le32_to_cpu(c->log_le.buf);
Arend van Spriela39be272013-12-12 11:58:58 +01002906 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, c->buf, c->bufsize);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002907 if (rv < 0)
2908 return rv;
2909
2910 while (c->last != idx) {
2911 for (n = 0; n < CONSOLE_LINE_MAX - 2; n++) {
2912 if (c->last == idx) {
2913 /* This would output a partial line.
2914 * Instead, back up
2915 * the buffer pointer and output this
2916 * line next time around.
2917 */
2918 if (c->last >= n)
2919 c->last -= n;
2920 else
2921 c->last = c->bufsize - n;
2922 goto break2;
2923 }
2924 ch = c->buf[c->last];
2925 c->last = (c->last + 1) % c->bufsize;
2926 if (ch == '\n')
2927 break;
2928 line[n] = ch;
2929 }
2930
2931 if (n > 0) {
2932 if (line[n - 1] == '\r')
2933 n--;
2934 line[n] = 0;
Joe Perches18aad4f2012-01-15 00:38:42 -08002935 pr_debug("CONSOLE: %s\n", line);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002936 }
2937 }
2938break2:
2939
2940 return 0;
2941}
Joe Perches8ae74652012-01-15 00:38:38 -08002942#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02002943
Franky Linfcf094f2011-12-16 18:37:16 -08002944static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01002945brcmf_sdio_bus_txctl(struct device *dev, unsigned char *msg, uint msglen)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002946{
Franky Lin47a1ce72011-11-22 17:21:55 -08002947 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01002948 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Lin47a1ce72011-11-22 17:21:55 -08002949 struct brcmf_sdio *bus = sdiodev->bus;
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002950 int ret = -1;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002951
2952 brcmf_dbg(TRACE, "Enter\n");
2953
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002954 if (down_interruptible(&bus->tx_seq_lock))
2955 return -EINTR;
Franky Lin8da9d2c2013-11-29 12:25:10 +01002956
Arend van Spriel5b435de2011-10-05 13:19:03 +02002957 if (!data_ok(bus)) {
2958 brcmf_dbg(INFO, "No bus credit bus->tx_max %d, bus->tx_seq %d\n",
2959 bus->tx_max, bus->tx_seq);
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002960 up(&bus->tx_seq_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002961 /* Send from dpc */
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002962 bus->ctrl_frame_buf = msg;
2963 bus->ctrl_frame_len = msglen;
2964 bus->ctrl_frame_stat = true;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002965
Franky Linfd67dc82012-11-05 16:22:25 -08002966 wait_event_interruptible_timeout(bus->ctrl_wait,
2967 !bus->ctrl_frame_stat,
2968 msecs_to_jiffies(2000));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002969
Joe Perches23677ce2012-02-09 11:17:23 +00002970 if (!bus->ctrl_frame_stat) {
Arend van Sprielc3203372013-04-03 12:40:44 +02002971 brcmf_dbg(SDIO, "ctrl_frame_stat == false\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002972 ret = 0;
2973 } else {
Arend van Sprielc3203372013-04-03 12:40:44 +02002974 brcmf_dbg(SDIO, "ctrl_frame_stat == true\n");
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002975 bus->ctrl_frame_stat = false;
2976 if (down_interruptible(&bus->tx_seq_lock))
2977 return -EINTR;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002978 ret = -1;
2979 }
2980 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002981 if (ret == -1) {
Hante Meulemanfed7ec42014-03-15 17:18:18 +01002982 sdio_claim_host(bus->sdiodev->func[1]);
2983 brcmf_sdio_bus_sleep(bus, false, false);
2984 ret = brcmf_sdio_tx_ctrlframe(bus, msg, msglen);
2985 sdio_release_host(bus->sdiodev->func[1]);
2986 up(&bus->tx_seq_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002987 }
2988
Arend van Spriel5b435de2011-10-05 13:19:03 +02002989 if (ret)
Arend van Spriel80969832012-06-09 22:51:44 +02002990 bus->sdcnt.tx_ctlerrs++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002991 else
Arend van Spriel80969832012-06-09 22:51:44 +02002992 bus->sdcnt.tx_ctlpkts++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002993
2994 return ret ? -EIO : 0;
2995}
2996
Arend van Spriel80969832012-06-09 22:51:44 +02002997#ifdef DEBUG
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02002998static int brcmf_sdio_dump_console(struct seq_file *seq, struct brcmf_sdio *bus,
2999 struct sdpcm_shared *sh)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003000{
3001 u32 addr, console_ptr, console_size, console_index;
3002 char *conbuf = NULL;
3003 __le32 sh_val;
3004 int rv;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003005
3006 /* obtain console information from device memory */
3007 addr = sh->console_addr + offsetof(struct rte_console, log_le);
Arend van Spriela39be272013-12-12 11:58:58 +01003008 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
3009 (u8 *)&sh_val, sizeof(u32));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003010 if (rv < 0)
3011 return rv;
3012 console_ptr = le32_to_cpu(sh_val);
3013
3014 addr = sh->console_addr + offsetof(struct rte_console, log_le.buf_size);
Arend van Spriela39be272013-12-12 11:58:58 +01003015 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
3016 (u8 *)&sh_val, sizeof(u32));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003017 if (rv < 0)
3018 return rv;
3019 console_size = le32_to_cpu(sh_val);
3020
3021 addr = sh->console_addr + offsetof(struct rte_console, log_le.idx);
Arend van Spriela39be272013-12-12 11:58:58 +01003022 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
3023 (u8 *)&sh_val, sizeof(u32));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003024 if (rv < 0)
3025 return rv;
3026 console_index = le32_to_cpu(sh_val);
3027
3028 /* allocate buffer for console data */
3029 if (console_size <= CONSOLE_BUFFER_MAX)
3030 conbuf = vzalloc(console_size+1);
3031
3032 if (!conbuf)
3033 return -ENOMEM;
3034
3035 /* obtain the console data from device */
3036 conbuf[console_size] = '\0';
Arend van Spriela39be272013-12-12 11:58:58 +01003037 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, console_ptr, (u8 *)conbuf,
3038 console_size);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003039 if (rv < 0)
3040 goto done;
3041
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003042 rv = seq_write(seq, conbuf + console_index,
3043 console_size - console_index);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003044 if (rv < 0)
3045 goto done;
3046
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003047 if (console_index > 0)
3048 rv = seq_write(seq, conbuf, console_index - 1);
3049
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003050done:
3051 vfree(conbuf);
3052 return rv;
3053}
3054
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003055static int brcmf_sdio_trap_info(struct seq_file *seq, struct brcmf_sdio *bus,
3056 struct sdpcm_shared *sh)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003057{
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003058 int error;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003059 struct brcmf_trap_info tr;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003060
Piotr Haberbaa9e602013-04-05 10:57:42 +02003061 if ((sh->flags & SDPCM_SHARED_TRAP) == 0) {
3062 brcmf_dbg(INFO, "no trap in firmware\n");
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003063 return 0;
Piotr Haberbaa9e602013-04-05 10:57:42 +02003064 }
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003065
Arend van Spriela39be272013-12-12 11:58:58 +01003066 error = brcmf_sdiod_ramrw(bus->sdiodev, false, sh->trap_addr, (u8 *)&tr,
3067 sizeof(struct brcmf_trap_info));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003068 if (error < 0)
3069 return error;
3070
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003071 seq_printf(seq,
3072 "dongle trap info: type 0x%x @ epc 0x%08x\n"
3073 " cpsr 0x%08x spsr 0x%08x sp 0x%08x\n"
3074 " lr 0x%08x pc 0x%08x offset 0x%x\n"
3075 " r0 0x%08x r1 0x%08x r2 0x%08x r3 0x%08x\n"
3076 " r4 0x%08x r5 0x%08x r6 0x%08x r7 0x%08x\n",
3077 le32_to_cpu(tr.type), le32_to_cpu(tr.epc),
3078 le32_to_cpu(tr.cpsr), le32_to_cpu(tr.spsr),
3079 le32_to_cpu(tr.r13), le32_to_cpu(tr.r14),
3080 le32_to_cpu(tr.pc), sh->trap_addr,
3081 le32_to_cpu(tr.r0), le32_to_cpu(tr.r1),
3082 le32_to_cpu(tr.r2), le32_to_cpu(tr.r3),
3083 le32_to_cpu(tr.r4), le32_to_cpu(tr.r5),
3084 le32_to_cpu(tr.r6), le32_to_cpu(tr.r7));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003085
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003086 return 0;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003087}
3088
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003089static int brcmf_sdio_assert_info(struct seq_file *seq, struct brcmf_sdio *bus,
3090 struct sdpcm_shared *sh)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003091{
3092 int error = 0;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003093 char file[80] = "?";
3094 char expr[80] = "<???>";
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003095
3096 if ((sh->flags & SDPCM_SHARED_ASSERT_BUILT) == 0) {
3097 brcmf_dbg(INFO, "firmware not built with -assert\n");
3098 return 0;
3099 } else if ((sh->flags & SDPCM_SHARED_ASSERT) == 0) {
3100 brcmf_dbg(INFO, "no assert in dongle\n");
3101 return 0;
3102 }
3103
Franky Lin38b0b0d2012-11-05 16:22:24 -08003104 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003105 if (sh->assert_file_addr != 0) {
Arend van Spriela39be272013-12-12 11:58:58 +01003106 error = brcmf_sdiod_ramrw(bus->sdiodev, false,
3107 sh->assert_file_addr, (u8 *)file, 80);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003108 if (error < 0)
3109 return error;
3110 }
3111 if (sh->assert_exp_addr != 0) {
Arend van Spriela39be272013-12-12 11:58:58 +01003112 error = brcmf_sdiod_ramrw(bus->sdiodev, false,
3113 sh->assert_exp_addr, (u8 *)expr, 80);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003114 if (error < 0)
3115 return error;
3116 }
Franky Lin38b0b0d2012-11-05 16:22:24 -08003117 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003118
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003119 seq_printf(seq, "dongle assert: %s:%d: assert(%s)\n",
3120 file, sh->assert_line, expr);
3121 return 0;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003122}
3123
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003124static int brcmf_sdio_checkdied(struct brcmf_sdio *bus)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003125{
3126 int error;
3127 struct sdpcm_shared sh;
3128
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003129 error = brcmf_sdio_readshared(bus, &sh);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003130
3131 if (error < 0)
3132 return error;
3133
3134 if ((sh.flags & SDPCM_SHARED_ASSERT_BUILT) == 0)
3135 brcmf_dbg(INFO, "firmware not built with -assert\n");
3136 else if (sh.flags & SDPCM_SHARED_ASSERT)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003137 brcmf_err("assertion in dongle\n");
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003138
3139 if (sh.flags & SDPCM_SHARED_TRAP)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003140 brcmf_err("firmware trap in dongle\n");
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003141
3142 return 0;
3143}
3144
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003145static int brcmf_sdio_died_dump(struct seq_file *seq, struct brcmf_sdio *bus)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003146{
3147 int error = 0;
3148 struct sdpcm_shared sh;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003149
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003150 error = brcmf_sdio_readshared(bus, &sh);
3151 if (error < 0)
3152 goto done;
3153
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003154 error = brcmf_sdio_assert_info(seq, bus, &sh);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003155 if (error < 0)
3156 goto done;
Piotr Haberbaa9e602013-04-05 10:57:42 +02003157
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003158 error = brcmf_sdio_trap_info(seq, bus, &sh);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003159 if (error < 0)
3160 goto done;
3161
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003162 error = brcmf_sdio_dump_console(seq, bus, &sh);
Piotr Haberbaa9e602013-04-05 10:57:42 +02003163
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003164done:
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003165 return error;
3166}
3167
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003168static int brcmf_sdio_forensic_read(struct seq_file *seq, void *data)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003169{
Arend van Spriel82d957e2014-07-12 08:49:36 +02003170 struct brcmf_bus *bus_if = dev_get_drvdata(seq->private);
3171 struct brcmf_sdio *bus = bus_if->bus_priv.sdio->bus;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003172
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003173 return brcmf_sdio_died_dump(seq, bus);
3174}
3175
Arend van Spriel82d957e2014-07-12 08:49:36 +02003176static int brcmf_debugfs_sdio_count_read(struct seq_file *seq, void *data)
Arend van Spriel1b1e4e92014-07-12 08:49:35 +02003177{
Arend van Spriel82d957e2014-07-12 08:49:36 +02003178 struct brcmf_bus *bus_if = dev_get_drvdata(seq->private);
3179 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3180 struct brcmf_sdio_count *sdcnt = &sdiodev->bus->sdcnt;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003181
Arend van Spriel82d957e2014-07-12 08:49:36 +02003182 seq_printf(seq,
3183 "intrcount: %u\nlastintrs: %u\n"
3184 "pollcnt: %u\nregfails: %u\n"
3185 "tx_sderrs: %u\nfcqueued: %u\n"
3186 "rxrtx: %u\nrx_toolong: %u\n"
3187 "rxc_errors: %u\nrx_hdrfail: %u\n"
3188 "rx_badhdr: %u\nrx_badseq: %u\n"
3189 "fc_rcvd: %u\nfc_xoff: %u\n"
3190 "fc_xon: %u\nrxglomfail: %u\n"
3191 "rxglomframes: %u\nrxglompkts: %u\n"
3192 "f2rxhdrs: %u\nf2rxdata: %u\n"
3193 "f2txdata: %u\nf1regdata: %u\n"
3194 "tickcnt: %u\ntx_ctlerrs: %lu\n"
3195 "tx_ctlpkts: %lu\nrx_ctlerrs: %lu\n"
3196 "rx_ctlpkts: %lu\nrx_readahead: %lu\n",
3197 sdcnt->intrcount, sdcnt->lastintrs,
3198 sdcnt->pollcnt, sdcnt->regfails,
3199 sdcnt->tx_sderrs, sdcnt->fcqueued,
3200 sdcnt->rxrtx, sdcnt->rx_toolong,
3201 sdcnt->rxc_errors, sdcnt->rx_hdrfail,
3202 sdcnt->rx_badhdr, sdcnt->rx_badseq,
3203 sdcnt->fc_rcvd, sdcnt->fc_xoff,
3204 sdcnt->fc_xon, sdcnt->rxglomfail,
3205 sdcnt->rxglomframes, sdcnt->rxglompkts,
3206 sdcnt->f2rxhdrs, sdcnt->f2rxdata,
3207 sdcnt->f2txdata, sdcnt->f1regdata,
3208 sdcnt->tickcnt, sdcnt->tx_ctlerrs,
3209 sdcnt->tx_ctlpkts, sdcnt->rx_ctlerrs,
3210 sdcnt->rx_ctlpkts, sdcnt->rx_readahead_cnt);
3211
3212 return 0;
3213}
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003214
Arend van Spriel80969832012-06-09 22:51:44 +02003215static void brcmf_sdio_debugfs_create(struct brcmf_sdio *bus)
3216{
3217 struct brcmf_pub *drvr = bus->sdiodev->bus_if->drvr;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003218 struct dentry *dentry = brcmf_debugfs_get_devdir(drvr);
Arend van Spriel80969832012-06-09 22:51:44 +02003219
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003220 if (IS_ERR_OR_NULL(dentry))
3221 return;
3222
Arend van Spriel82d957e2014-07-12 08:49:36 +02003223 brcmf_debugfs_add_entry(drvr, "forensics", brcmf_sdio_forensic_read);
3224 brcmf_debugfs_add_entry(drvr, "counters",
3225 brcmf_debugfs_sdio_count_read);
Daniel Kim0801e6c2014-01-29 15:32:13 +01003226 debugfs_create_u32("console_interval", 0644, dentry,
3227 &bus->console_interval);
Arend van Spriel80969832012-06-09 22:51:44 +02003228}
3229#else
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003230static int brcmf_sdio_checkdied(struct brcmf_sdio *bus)
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003231{
3232 return 0;
3233}
3234
Arend van Spriel80969832012-06-09 22:51:44 +02003235static void brcmf_sdio_debugfs_create(struct brcmf_sdio *bus)
3236{
3237}
3238#endif /* DEBUG */
3239
Franky Linfcf094f2011-12-16 18:37:16 -08003240static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003241brcmf_sdio_bus_rxctl(struct device *dev, unsigned char *msg, uint msglen)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003242{
3243 int timeleft;
3244 uint rxlen = 0;
3245 bool pending;
Franky Lindd43a012012-11-05 16:22:22 -08003246 u8 *buf;
Franky Lin532cdd32011-11-22 17:21:54 -08003247 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01003248 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Lin532cdd32011-11-22 17:21:54 -08003249 struct brcmf_sdio *bus = sdiodev->bus;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003250
3251 brcmf_dbg(TRACE, "Enter\n");
3252
3253 /* Wait until control frame is available */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003254 timeleft = brcmf_sdio_dcmd_resp_wait(bus, &bus->rxlen, &pending);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003255
Franky Lindd43a012012-11-05 16:22:22 -08003256 spin_lock_bh(&bus->rxctl_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003257 rxlen = bus->rxlen;
3258 memcpy(msg, bus->rxctl, min(msglen, rxlen));
Franky Lindd43a012012-11-05 16:22:22 -08003259 bus->rxctl = NULL;
3260 buf = bus->rxctl_orig;
3261 bus->rxctl_orig = NULL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003262 bus->rxlen = 0;
Franky Lindd43a012012-11-05 16:22:22 -08003263 spin_unlock_bh(&bus->rxctl_lock);
3264 vfree(buf);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003265
3266 if (rxlen) {
3267 brcmf_dbg(CTL, "resumed on rxctl frame, got %d expected %d\n",
3268 rxlen, msglen);
3269 } else if (timeleft == 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003270 brcmf_err("resumed on timeout\n");
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003271 brcmf_sdio_checkdied(bus);
Joe Perches23677ce2012-02-09 11:17:23 +00003272 } else if (pending) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003273 brcmf_dbg(CTL, "cancelled\n");
3274 return -ERESTARTSYS;
3275 } else {
3276 brcmf_dbg(CTL, "resumed for unknown reason?\n");
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003277 brcmf_sdio_checkdied(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003278 }
3279
3280 if (rxlen)
Arend van Spriel80969832012-06-09 22:51:44 +02003281 bus->sdcnt.rx_ctlpkts++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003282 else
Arend van Spriel80969832012-06-09 22:51:44 +02003283 bus->sdcnt.rx_ctlerrs++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003284
3285 return rxlen ? (int)rxlen : -ETIMEDOUT;
3286}
3287
Hante Meulemana74d0362014-01-13 22:20:22 +01003288#ifdef DEBUG
3289static bool
3290brcmf_sdio_verifymemory(struct brcmf_sdio_dev *sdiodev, u32 ram_addr,
3291 u8 *ram_data, uint ram_sz)
3292{
3293 char *ram_cmp;
3294 int err;
3295 bool ret = true;
3296 int address;
3297 int offset;
3298 int len;
3299
3300 /* read back and verify */
3301 brcmf_dbg(INFO, "Compare RAM dl & ul at 0x%08x; size=%d\n", ram_addr,
3302 ram_sz);
3303 ram_cmp = kmalloc(MEMBLOCK, GFP_KERNEL);
3304 /* do not proceed while no memory but */
3305 if (!ram_cmp)
3306 return true;
3307
3308 address = ram_addr;
3309 offset = 0;
3310 while (offset < ram_sz) {
3311 len = ((offset + MEMBLOCK) < ram_sz) ? MEMBLOCK :
3312 ram_sz - offset;
3313 err = brcmf_sdiod_ramrw(sdiodev, false, address, ram_cmp, len);
3314 if (err) {
3315 brcmf_err("error %d on reading %d membytes at 0x%08x\n",
3316 err, len, address);
3317 ret = false;
3318 break;
3319 } else if (memcmp(ram_cmp, &ram_data[offset], len)) {
3320 brcmf_err("Downloaded RAM image is corrupted, block offset is %d, len is %d\n",
3321 offset, len);
3322 ret = false;
3323 break;
3324 }
3325 offset += len;
3326 address += len;
3327 }
3328
3329 kfree(ram_cmp);
3330
3331 return ret;
3332}
3333#else /* DEBUG */
3334static bool
3335brcmf_sdio_verifymemory(struct brcmf_sdio_dev *sdiodev, u32 ram_addr,
3336 u8 *ram_data, uint ram_sz)
3337{
3338 return true;
3339}
3340#endif /* DEBUG */
3341
Arend van Spriel33556502014-01-13 22:20:25 +01003342static int brcmf_sdio_download_code_file(struct brcmf_sdio *bus,
3343 const struct firmware *fw)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003344{
Hante Meulemanf2c44fe2013-09-25 13:05:45 +02003345 int err;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003346
Hante Meulemana74d0362014-01-13 22:20:22 +01003347 brcmf_dbg(TRACE, "Enter\n");
3348
Hante Meulemanf9951c12014-01-29 15:32:17 +01003349 err = brcmf_sdiod_ramrw(bus->sdiodev, true, bus->ci->rambase,
3350 (u8 *)fw->data, fw->size);
3351 if (err)
3352 brcmf_err("error %d on writing %d membytes at 0x%08x\n",
3353 err, (int)fw->size, bus->ci->rambase);
3354 else if (!brcmf_sdio_verifymemory(bus->sdiodev, bus->ci->rambase,
3355 (u8 *)fw->data, fw->size))
3356 err = -EIO;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003357
Hante Meulemanf2c44fe2013-09-25 13:05:45 +02003358 return err;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003359}
3360
Arend van Spriel33556502014-01-13 22:20:25 +01003361static int brcmf_sdio_download_nvram(struct brcmf_sdio *bus,
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003362 void *vars, u32 varsz)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003363{
Hante Meulemana74d0362014-01-13 22:20:22 +01003364 int address;
3365 int err;
3366
3367 brcmf_dbg(TRACE, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003368
Hante Meulemana74d0362014-01-13 22:20:22 +01003369 address = bus->ci->ramsize - varsz + bus->ci->rambase;
3370 err = brcmf_sdiod_ramrw(bus->sdiodev, true, address, vars, varsz);
3371 if (err)
3372 brcmf_err("error %d on writing %d nvram bytes at 0x%08x\n",
3373 err, varsz, address);
3374 else if (!brcmf_sdio_verifymemory(bus->sdiodev, address, vars, varsz))
3375 err = -EIO;
3376
Hante Meulemana74d0362014-01-13 22:20:22 +01003377 return err;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003378}
3379
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003380static int brcmf_sdio_download_firmware(struct brcmf_sdio *bus,
3381 const struct firmware *fw,
3382 void *nvram, u32 nvlen)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003383{
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003384 int bcmerror = -EFAULT;
Arend van Spriel33556502014-01-13 22:20:25 +01003385 u32 rstvec;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003386
3387 sdio_claim_host(bus->sdiodev->func[1]);
3388 brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003389
3390 /* Keep arm in reset */
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003391 brcmf_chip_enter_download(bus->ci);
Arend van Spriel33556502014-01-13 22:20:25 +01003392
Arend van Spriel33556502014-01-13 22:20:25 +01003393 rstvec = get_unaligned_le32(fw->data);
3394 brcmf_dbg(SDIO, "firmware rstvec: %x\n", rstvec);
3395
3396 bcmerror = brcmf_sdio_download_code_file(bus, fw);
3397 release_firmware(fw);
3398 if (bcmerror) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003399 brcmf_err("dongle image file download failed\n");
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003400 brcmf_fw_nvram_free(nvram);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003401 goto err;
3402 }
3403
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02003404 bcmerror = brcmf_sdio_download_nvram(bus, nvram, nvlen);
3405 brcmf_fw_nvram_free(nvram);
Arend van Spriel33556502014-01-13 22:20:25 +01003406 if (bcmerror) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003407 brcmf_err("dongle nvram file download failed\n");
Franky Lin3eaa9562013-04-02 21:06:20 +02003408 goto err;
3409 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02003410
3411 /* Take arm out of reset */
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003412 if (!brcmf_chip_exit_download(bus->ci, rstvec)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003413 brcmf_err("error getting out of ARM core reset\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003414 goto err;
3415 }
3416
Arend van Spriel33556502014-01-13 22:20:25 +01003417 /* Allow HT Clock now that the ARM is running. */
Arend van Sprielbb350712014-01-13 22:20:29 +01003418 brcmf_bus_change_state(bus->sdiodev->bus_if, BRCMF_BUS_LOAD);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003419 bcmerror = 0;
3420
3421err:
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003422 brcmf_sdio_clkctl(bus, CLK_SDONLY, false);
3423 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003424 return bcmerror;
3425}
3426
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003427static void brcmf_sdio_sr_init(struct brcmf_sdio *bus)
Piotr Haber4a3da992013-04-11 13:28:46 +02003428{
3429 int err = 0;
3430 u8 val;
3431
3432 brcmf_dbg(TRACE, "Enter\n");
3433
Arend van Spriela39be272013-12-12 11:58:58 +01003434 val = brcmf_sdiod_regrb(bus->sdiodev, SBSDIO_FUNC1_WAKEUPCTRL, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003435 if (err) {
3436 brcmf_err("error reading SBSDIO_FUNC1_WAKEUPCTRL\n");
3437 return;
3438 }
3439
3440 val |= 1 << SBSDIO_FUNC1_WCTRL_HTWAIT_SHIFT;
Arend van Spriela39be272013-12-12 11:58:58 +01003441 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_WAKEUPCTRL, val, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003442 if (err) {
3443 brcmf_err("error writing SBSDIO_FUNC1_WAKEUPCTRL\n");
3444 return;
3445 }
3446
3447 /* Add CMD14 Support */
Arend van Spriela39be272013-12-12 11:58:58 +01003448 brcmf_sdiod_regwb(bus->sdiodev, SDIO_CCCR_BRCM_CARDCAP,
3449 (SDIO_CCCR_BRCM_CARDCAP_CMD14_SUPPORT |
3450 SDIO_CCCR_BRCM_CARDCAP_CMD14_EXT),
3451 &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003452 if (err) {
3453 brcmf_err("error writing SDIO_CCCR_BRCM_CARDCAP\n");
3454 return;
3455 }
3456
Arend van Spriela39be272013-12-12 11:58:58 +01003457 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3458 SBSDIO_FORCE_HT, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003459 if (err) {
3460 brcmf_err("error writing SBSDIO_FUNC1_CHIPCLKCSR\n");
3461 return;
3462 }
3463
3464 /* set flag */
3465 bus->sr_enabled = true;
3466 brcmf_dbg(INFO, "SR enabled\n");
3467}
3468
3469/* enable KSO bit */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003470static int brcmf_sdio_kso_init(struct brcmf_sdio *bus)
Piotr Haber4a3da992013-04-11 13:28:46 +02003471{
3472 u8 val;
3473 int err = 0;
3474
3475 brcmf_dbg(TRACE, "Enter\n");
3476
3477 /* KSO bit added in SDIO core rev 12 */
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003478 if (brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV)->rev < 12)
Piotr Haber4a3da992013-04-11 13:28:46 +02003479 return 0;
3480
Arend van Spriela39be272013-12-12 11:58:58 +01003481 val = brcmf_sdiod_regrb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003482 if (err) {
3483 brcmf_err("error reading SBSDIO_FUNC1_SLEEPCSR\n");
3484 return err;
3485 }
3486
3487 if (!(val & SBSDIO_FUNC1_SLEEPCSR_KSO_MASK)) {
3488 val |= (SBSDIO_FUNC1_SLEEPCSR_KSO_EN <<
3489 SBSDIO_FUNC1_SLEEPCSR_KSO_SHIFT);
Arend van Spriela39be272013-12-12 11:58:58 +01003490 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
3491 val, &err);
Piotr Haber4a3da992013-04-11 13:28:46 +02003492 if (err) {
3493 brcmf_err("error writing SBSDIO_FUNC1_SLEEPCSR\n");
3494 return err;
3495 }
3496 }
3497
3498 return 0;
3499}
3500
3501
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003502static int brcmf_sdio_bus_preinit(struct device *dev)
Arend van Sprielcf458282013-11-29 11:48:15 +01003503{
3504 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3505 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3506 struct brcmf_sdio *bus = sdiodev->bus;
Franky Lin8da9d2c2013-11-29 12:25:10 +01003507 uint pad_size;
Arend van Sprielcf458282013-11-29 11:48:15 +01003508 u32 value;
Arend van Sprielcf458282013-11-29 11:48:15 +01003509 int err;
3510
Franky Lin8da9d2c2013-11-29 12:25:10 +01003511 /* the commands below use the terms tx and rx from
3512 * a device perspective, ie. bus:txglom affects the
3513 * bus transfers from device to host.
3514 */
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003515 if (brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV)->rev < 12) {
Arend van Sprielcf458282013-11-29 11:48:15 +01003516 /* for sdio core rev < 12, disable txgloming */
3517 value = 0;
3518 err = brcmf_iovar_data_set(dev, "bus:txglom", &value,
3519 sizeof(u32));
3520 } else {
3521 /* otherwise, set txglomalign */
3522 value = 4;
3523 if (sdiodev->pdata)
3524 value = sdiodev->pdata->sd_sgentry_align;
3525 /* SDIO ADMA requires at least 32 bit alignment */
3526 value = max_t(u32, value, 4);
3527 err = brcmf_iovar_data_set(dev, "bus:txglomalign", &value,
3528 sizeof(u32));
3529 }
Franky Lin8da9d2c2013-11-29 12:25:10 +01003530
3531 if (err < 0)
3532 goto done;
3533
3534 bus->tx_hdrlen = SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN;
3535 if (sdiodev->sg_support) {
3536 bus->txglom = false;
3537 value = 1;
3538 pad_size = bus->sdiodev->func[2]->cur_blksize << 1;
Franky Lin8da9d2c2013-11-29 12:25:10 +01003539 err = brcmf_iovar_data_set(bus->sdiodev->dev, "bus:rxglom",
3540 &value, sizeof(u32));
3541 if (err < 0) {
3542 /* bus:rxglom is allowed to fail */
3543 err = 0;
3544 } else {
3545 bus->txglom = true;
3546 bus->tx_hdrlen += SDPCM_HWEXT_LEN;
3547 }
3548 }
3549 brcmf_bus_add_txhdrlen(bus->sdiodev->dev, bus->tx_hdrlen);
3550
3551done:
Arend van Sprielcf458282013-11-29 11:48:15 +01003552 return err;
3553}
3554
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003555void brcmf_sdio_isr(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003556{
Arend van Spriel5b435de2011-10-05 13:19:03 +02003557 brcmf_dbg(TRACE, "Enter\n");
3558
3559 if (!bus) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003560 brcmf_err("bus is null pointer, exiting\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003561 return;
3562 }
3563
Arend van Sprielbb350712014-01-13 22:20:29 +01003564 if (!brcmf_bus_ready(bus->sdiodev->bus_if)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003565 brcmf_err("bus is down. we have nothing to do\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003566 return;
3567 }
3568 /* Count the interrupt call */
Arend van Spriel80969832012-06-09 22:51:44 +02003569 bus->sdcnt.intrcount++;
Franky Lin45316032012-09-13 21:12:03 +02003570 if (in_interrupt())
3571 atomic_set(&bus->ipend, 1);
3572 else
3573 if (brcmf_sdio_intr_rstatus(bus)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003574 brcmf_err("failed backplane access\n");
Franky Lin45316032012-09-13 21:12:03 +02003575 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02003576
Arend van Spriel5b435de2011-10-05 13:19:03 +02003577 /* Disable additional interrupts (is this needed now)? */
3578 if (!bus->intr)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003579 brcmf_err("isr w/o interrupt configured!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003580
Arend van Sprielfccfe932013-06-18 13:29:26 +02003581 atomic_inc(&bus->dpc_tskcnt);
Franky Linf1e68c22012-09-13 21:12:00 +02003582 queue_work(bus->brcmf_wq, &bus->datawork);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003583}
3584
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003585static bool brcmf_sdio_bus_watchdog(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003586{
Joe Perches8ae74652012-01-15 00:38:38 -08003587#ifdef DEBUG
Franky Lincad2b262011-11-22 17:21:53 -08003588 struct brcmf_bus *bus_if = dev_get_drvdata(bus->sdiodev->dev);
Joe Perches8ae74652012-01-15 00:38:38 -08003589#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02003590
3591 brcmf_dbg(TIMER, "Enter\n");
3592
Arend van Spriel5b435de2011-10-05 13:19:03 +02003593 /* Poll period: check device if appropriate. */
Piotr Haber4a3da992013-04-11 13:28:46 +02003594 if (!bus->sr_enabled &&
3595 bus->poll && (++bus->polltick >= bus->pollrate)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003596 u32 intstatus = 0;
3597
3598 /* Reset poll tick */
3599 bus->polltick = 0;
3600
3601 /* Check device if no interrupts */
Arend van Spriel80969832012-06-09 22:51:44 +02003602 if (!bus->intr ||
3603 (bus->sdcnt.intrcount == bus->sdcnt.lastintrs)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003604
Arend van Sprielfccfe932013-06-18 13:29:26 +02003605 if (atomic_read(&bus->dpc_tskcnt) == 0) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003606 u8 devpend;
Arend van Sprielfccfe932013-06-18 13:29:26 +02003607
Franky Lin38b0b0d2012-11-05 16:22:24 -08003608 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriela39be272013-12-12 11:58:58 +01003609 devpend = brcmf_sdiod_regrb(bus->sdiodev,
3610 SDIO_CCCR_INTx,
3611 NULL);
Franky Lin38b0b0d2012-11-05 16:22:24 -08003612 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003613 intstatus =
3614 devpend & (INTR_STATUS_FUNC1 |
3615 INTR_STATUS_FUNC2);
3616 }
3617
3618 /* If there is something, make like the ISR and
3619 schedule the DPC */
3620 if (intstatus) {
Arend van Spriel80969832012-06-09 22:51:44 +02003621 bus->sdcnt.pollcnt++;
Franky Lin1d382272012-09-13 21:11:59 +02003622 atomic_set(&bus->ipend, 1);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003623
Arend van Sprielfccfe932013-06-18 13:29:26 +02003624 atomic_inc(&bus->dpc_tskcnt);
Franky Linf1e68c22012-09-13 21:12:00 +02003625 queue_work(bus->brcmf_wq, &bus->datawork);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003626 }
3627 }
3628
3629 /* Update interrupt tracking */
Arend van Spriel80969832012-06-09 22:51:44 +02003630 bus->sdcnt.lastintrs = bus->sdcnt.intrcount;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003631 }
Joe Perches8ae74652012-01-15 00:38:38 -08003632#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02003633 /* Poll for console output periodically */
Hante Meuleman2def5c12012-11-14 18:46:19 -08003634 if (bus_if && bus_if->state == BRCMF_BUS_DATA &&
Franky Lin8d169aa2011-11-22 17:21:52 -08003635 bus->console_interval != 0) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003636 bus->console.count += BRCMF_WD_POLL_MS;
3637 if (bus->console.count >= bus->console_interval) {
3638 bus->console.count -= bus->console_interval;
Franky Lin38b0b0d2012-11-05 16:22:24 -08003639 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003640 /* Make sure backplane clock is on */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003641 brcmf_sdio_bus_sleep(bus, false, false);
3642 if (brcmf_sdio_readconsole(bus) < 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003643 /* stop on error */
3644 bus->console_interval = 0;
Franky Lin38b0b0d2012-11-05 16:22:24 -08003645 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003646 }
3647 }
Joe Perches8ae74652012-01-15 00:38:38 -08003648#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02003649
3650 /* On idle timeout clear activity flag and/or turn off clock */
3651 if ((bus->idletime > 0) && (bus->clkstate == CLK_AVAIL)) {
3652 if (++bus->idlecount >= bus->idletime) {
3653 bus->idlecount = 0;
3654 if (bus->activity) {
3655 bus->activity = false;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003656 brcmf_sdio_wd_timer(bus, BRCMF_WD_POLL_MS);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003657 } else {
Piotr Haber4a3da992013-04-11 13:28:46 +02003658 brcmf_dbg(SDIO, "idle\n");
Franky Lin38b0b0d2012-11-05 16:22:24 -08003659 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003660 brcmf_sdio_bus_sleep(bus, true, false);
Franky Lin38b0b0d2012-11-05 16:22:24 -08003661 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003662 }
3663 }
3664 }
3665
Franky Lin1d382272012-09-13 21:11:59 +02003666 return (atomic_read(&bus->ipend) > 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003667}
3668
Franky Linf1e68c22012-09-13 21:12:00 +02003669static void brcmf_sdio_dataworker(struct work_struct *work)
3670{
3671 struct brcmf_sdio *bus = container_of(work, struct brcmf_sdio,
3672 datawork);
Franky Linf1e68c22012-09-13 21:12:00 +02003673
Arend van Sprielfccfe932013-06-18 13:29:26 +02003674 while (atomic_read(&bus->dpc_tskcnt)) {
Hante Meuleman71abdc02014-02-25 20:30:31 +01003675 atomic_set(&bus->dpc_tskcnt, 0);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003676 brcmf_sdio_dpc(bus);
Franky Linf1e68c22012-09-13 21:12:00 +02003677 }
Franky Linf1e68c22012-09-13 21:12:00 +02003678}
3679
Arend van Spriel65d80d02014-01-29 15:32:14 +01003680static void
3681brcmf_sdio_drivestrengthinit(struct brcmf_sdio_dev *sdiodev,
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003682 struct brcmf_chip *ci, u32 drivestrength)
Arend van Spriel65d80d02014-01-29 15:32:14 +01003683{
3684 const struct sdiod_drive_str *str_tab = NULL;
3685 u32 str_mask;
3686 u32 str_shift;
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003687 u32 base;
Arend van Spriel65d80d02014-01-29 15:32:14 +01003688 u32 i;
3689 u32 drivestrength_sel = 0;
3690 u32 cc_data_temp;
3691 u32 addr;
3692
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003693 if (!(ci->cc_caps & CC_CAP_PMU))
Arend van Spriel65d80d02014-01-29 15:32:14 +01003694 return;
3695
3696 switch (SDIOD_DRVSTR_KEY(ci->chip, ci->pmurev)) {
Hante Meuleman5779ae62014-07-12 08:49:34 +02003697 case SDIOD_DRVSTR_KEY(BRCM_CC_4330_CHIP_ID, 12):
Arend van Spriel65d80d02014-01-29 15:32:14 +01003698 str_tab = sdiod_drvstr_tab1_1v8;
3699 str_mask = 0x00003800;
3700 str_shift = 11;
3701 break;
Hante Meuleman5779ae62014-07-12 08:49:34 +02003702 case SDIOD_DRVSTR_KEY(BRCM_CC_4334_CHIP_ID, 17):
Arend van Spriel65d80d02014-01-29 15:32:14 +01003703 str_tab = sdiod_drvstr_tab6_1v8;
3704 str_mask = 0x00001800;
3705 str_shift = 11;
3706 break;
Hante Meuleman5779ae62014-07-12 08:49:34 +02003707 case SDIOD_DRVSTR_KEY(BRCM_CC_43143_CHIP_ID, 17):
Arend van Spriel65d80d02014-01-29 15:32:14 +01003708 /* note: 43143 does not support tristate */
3709 i = ARRAY_SIZE(sdiod_drvstr_tab2_3v3) - 1;
3710 if (drivestrength >= sdiod_drvstr_tab2_3v3[i].strength) {
3711 str_tab = sdiod_drvstr_tab2_3v3;
3712 str_mask = 0x00000007;
3713 str_shift = 0;
3714 } else
3715 brcmf_err("Invalid SDIO Drive strength for chip %s, strength=%d\n",
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003716 ci->name, drivestrength);
Arend van Spriel65d80d02014-01-29 15:32:14 +01003717 break;
Hante Meuleman5779ae62014-07-12 08:49:34 +02003718 case SDIOD_DRVSTR_KEY(BRCM_CC_43362_CHIP_ID, 13):
Arend van Spriel65d80d02014-01-29 15:32:14 +01003719 str_tab = sdiod_drive_strength_tab5_1v8;
3720 str_mask = 0x00003800;
3721 str_shift = 11;
3722 break;
3723 default:
3724 brcmf_err("No SDIO Drive strength init done for chip %s rev %d pmurev %d\n",
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003725 ci->name, ci->chiprev, ci->pmurev);
Arend van Spriel65d80d02014-01-29 15:32:14 +01003726 break;
3727 }
3728
3729 if (str_tab != NULL) {
3730 for (i = 0; str_tab[i].strength != 0; i++) {
3731 if (drivestrength >= str_tab[i].strength) {
3732 drivestrength_sel = str_tab[i].sel;
3733 break;
3734 }
3735 }
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003736 base = brcmf_chip_get_chipcommon(ci)->base;
Arend van Spriel65d80d02014-01-29 15:32:14 +01003737 addr = CORE_CC_REG(base, chipcontrol_addr);
3738 brcmf_sdiod_regwl(sdiodev, addr, 1, NULL);
3739 cc_data_temp = brcmf_sdiod_regrl(sdiodev, addr, NULL);
3740 cc_data_temp &= ~str_mask;
3741 drivestrength_sel <<= str_shift;
3742 cc_data_temp |= drivestrength_sel;
3743 brcmf_sdiod_regwl(sdiodev, addr, cc_data_temp, NULL);
3744
3745 brcmf_dbg(INFO, "SDIO: %d mA (req=%d mA) drive strength selected, set to 0x%08x\n",
3746 str_tab[i].strength, drivestrength, cc_data_temp);
3747 }
3748}
3749
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003750static int brcmf_sdio_buscoreprep(void *ctx)
Arend van Spriel65d80d02014-01-29 15:32:14 +01003751{
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003752 struct brcmf_sdio_dev *sdiodev = ctx;
Arend van Spriel65d80d02014-01-29 15:32:14 +01003753 int err = 0;
3754 u8 clkval, clkset;
3755
3756 /* Try forcing SDIO core to do ALPAvail request only */
3757 clkset = SBSDIO_FORCE_HW_CLKREQ_OFF | SBSDIO_ALP_AVAIL_REQ;
3758 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, clkset, &err);
3759 if (err) {
3760 brcmf_err("error writing for HT off\n");
3761 return err;
3762 }
3763
3764 /* If register supported, wait for ALPAvail and then force ALP */
3765 /* This may take up to 15 milliseconds */
3766 clkval = brcmf_sdiod_regrb(sdiodev,
3767 SBSDIO_FUNC1_CHIPCLKCSR, NULL);
3768
3769 if ((clkval & ~SBSDIO_AVBITS) != clkset) {
3770 brcmf_err("ChipClkCSR access: wrote 0x%02x read 0x%02x\n",
3771 clkset, clkval);
3772 return -EACCES;
3773 }
3774
3775 SPINWAIT(((clkval = brcmf_sdiod_regrb(sdiodev,
3776 SBSDIO_FUNC1_CHIPCLKCSR, NULL)),
3777 !SBSDIO_ALPAV(clkval)),
3778 PMU_MAX_TRANSITION_DLY);
3779 if (!SBSDIO_ALPAV(clkval)) {
3780 brcmf_err("timeout on ALPAV wait, clkval 0x%02x\n",
3781 clkval);
3782 return -EBUSY;
3783 }
3784
3785 clkset = SBSDIO_FORCE_HW_CLKREQ_OFF | SBSDIO_FORCE_ALP;
3786 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, clkset, &err);
3787 udelay(65);
3788
3789 /* Also, disable the extra SDIO pull-ups */
3790 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_SDIOPULLUP, 0, NULL);
3791
3792 return 0;
3793}
3794
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003795static void brcmf_sdio_buscore_exitdl(void *ctx, struct brcmf_chip *chip,
3796 u32 rstvec)
3797{
3798 struct brcmf_sdio_dev *sdiodev = ctx;
3799 struct brcmf_core *core;
3800 u32 reg_addr;
3801
3802 /* clear all interrupts */
3803 core = brcmf_chip_get_core(chip, BCMA_CORE_SDIO_DEV);
3804 reg_addr = core->base + offsetof(struct sdpcmd_regs, intstatus);
3805 brcmf_sdiod_regwl(sdiodev, reg_addr, 0xFFFFFFFF, NULL);
3806
3807 if (rstvec)
3808 /* Write reset vector to address 0 */
3809 brcmf_sdiod_ramrw(sdiodev, true, 0, (void *)&rstvec,
3810 sizeof(rstvec));
3811}
3812
3813static u32 brcmf_sdio_buscore_read32(void *ctx, u32 addr)
3814{
3815 struct brcmf_sdio_dev *sdiodev = ctx;
3816 u32 val, rev;
3817
3818 val = brcmf_sdiod_regrl(sdiodev, addr, NULL);
Arend van Spriel8bd61f82015-01-06 23:02:54 +01003819 if (sdiodev->func[0]->device == SDIO_DEVICE_ID_BROADCOM_4335_4339 &&
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003820 addr == CORE_CC_REG(SI_ENUM_BASE, chipid)) {
3821 rev = (val & CID_REV_MASK) >> CID_REV_SHIFT;
3822 if (rev >= 2) {
3823 val &= ~CID_ID_MASK;
Hante Meuleman5779ae62014-07-12 08:49:34 +02003824 val |= BRCM_CC_4339_CHIP_ID;
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003825 }
3826 }
3827 return val;
3828}
3829
3830static void brcmf_sdio_buscore_write32(void *ctx, u32 addr, u32 val)
3831{
3832 struct brcmf_sdio_dev *sdiodev = ctx;
3833
3834 brcmf_sdiod_regwl(sdiodev, addr, val, NULL);
3835}
3836
3837static const struct brcmf_buscore_ops brcmf_sdio_buscore_ops = {
3838 .prepare = brcmf_sdio_buscoreprep,
3839 .exit_dl = brcmf_sdio_buscore_exitdl,
3840 .read32 = brcmf_sdio_buscore_read32,
3841 .write32 = brcmf_sdio_buscore_write32,
3842};
3843
Arend van Spriel5b435de2011-10-05 13:19:03 +02003844static bool
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003845brcmf_sdio_probe_attach(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003846{
3847 u8 clkctl = 0;
3848 int err = 0;
3849 int reg_addr;
3850 u32 reg_val;
Hante Meuleman668761a2013-04-12 10:55:55 +02003851 u32 drivestrength;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003852
Franky Lin38b0b0d2012-11-05 16:22:24 -08003853 sdio_claim_host(bus->sdiodev->func[1]);
3854
Joe Perches18aad4f2012-01-15 00:38:42 -08003855 pr_debug("F1 signature read @0x18000000=0x%4x\n",
Arend van Spriela39be272013-12-12 11:58:58 +01003856 brcmf_sdiod_regrl(bus->sdiodev, SI_ENUM_BASE, NULL));
Arend van Spriel5b435de2011-10-05 13:19:03 +02003857
3858 /*
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003859 * Force PLL off until brcmf_chip_attach()
Arend van Spriel5b435de2011-10-05 13:19:03 +02003860 * programs PLL control regs
3861 */
3862
Arend van Spriela39be272013-12-12 11:58:58 +01003863 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3864 BRCMF_INIT_CLKCTL1, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003865 if (!err)
Arend van Spriela39be272013-12-12 11:58:58 +01003866 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
3867 SBSDIO_FUNC1_CHIPCLKCSR, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003868
3869 if (err || ((clkctl & ~SBSDIO_AVBITS) != BRCMF_INIT_CLKCTL1)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003870 brcmf_err("ChipClkCSR access: err %d wrote 0x%02x read 0x%02x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02003871 err, BRCMF_INIT_CLKCTL1, clkctl);
3872 goto fail;
3873 }
3874
Arend van Sprielbb350712014-01-13 22:20:29 +01003875 /* SDIO register access works so moving
3876 * state from UNKNOWN to DOWN.
3877 */
3878 brcmf_bus_change_state(bus->sdiodev->bus_if, BRCMF_BUS_DOWN);
3879
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003880 bus->ci = brcmf_chip_attach(bus->sdiodev, &brcmf_sdio_buscore_ops);
3881 if (IS_ERR(bus->ci)) {
3882 brcmf_err("brcmf_chip_attach failed!\n");
3883 bus->ci = NULL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003884 goto fail;
3885 }
3886
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003887 if (brcmf_sdio_kso_init(bus)) {
Piotr Haber4a3da992013-04-11 13:28:46 +02003888 brcmf_err("error enabling KSO\n");
3889 goto fail;
3890 }
3891
Hante Meuleman668761a2013-04-12 10:55:55 +02003892 if ((bus->sdiodev->pdata) && (bus->sdiodev->pdata->drive_strength))
3893 drivestrength = bus->sdiodev->pdata->drive_strength;
3894 else
3895 drivestrength = DEFAULT_SDIO_DRIVE_STRENGTH;
Arend van Spriel65d80d02014-01-29 15:32:14 +01003896 brcmf_sdio_drivestrengthinit(bus->sdiodev, bus->ci, drivestrength);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003897
Franky Lin454d2a82011-11-04 22:23:37 +01003898 /* Get info on the SOCRAM cores... */
Arend van Spriel5b435de2011-10-05 13:19:03 +02003899 bus->ramsize = bus->ci->ramsize;
3900 if (!(bus->ramsize)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003901 brcmf_err("failed to find SOCRAM memory!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003902 goto fail;
3903 }
3904
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003905 /* Set card control so an SDIO card reset does a WLAN backplane reset */
Arend van Spriela39be272013-12-12 11:58:58 +01003906 reg_val = brcmf_sdiod_regrb(bus->sdiodev,
3907 SDIO_CCCR_BRCM_CARDCTRL, &err);
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003908 if (err)
3909 goto fail;
3910
3911 reg_val |= SDIO_CCCR_BRCM_CARDCTRL_WLANRESET;
3912
Arend van Spriela39be272013-12-12 11:58:58 +01003913 brcmf_sdiod_regwb(bus->sdiodev,
3914 SDIO_CCCR_BRCM_CARDCTRL, reg_val, &err);
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003915 if (err)
3916 goto fail;
3917
3918 /* set PMUControl so a backplane reset does PMU state reload */
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003919 reg_addr = CORE_CC_REG(brcmf_chip_get_chipcommon(bus->ci)->base,
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003920 pmucontrol);
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003921 reg_val = brcmf_sdiod_regrl(bus->sdiodev, reg_addr, &err);
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003922 if (err)
3923 goto fail;
3924
3925 reg_val |= (BCMA_CC_PMU_CTL_RES_RELOAD << BCMA_CC_PMU_CTL_RES_SHIFT);
3926
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01003927 brcmf_sdiod_regwl(bus->sdiodev, reg_addr, reg_val, &err);
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003928 if (err)
3929 goto fail;
3930
Franky Lin38b0b0d2012-11-05 16:22:24 -08003931 sdio_release_host(bus->sdiodev->func[1]);
3932
Arend van Spriel5b435de2011-10-05 13:19:03 +02003933 brcmu_pktq_init(&bus->txq, (PRIOMASK + 1), TXQLEN);
3934
Arend van Spriel9b2d2f2a82013-11-29 12:25:14 +01003935 /* allocate header buffer */
3936 bus->hdrbuf = kzalloc(MAX_HDR_READ + bus->head_align, GFP_KERNEL);
3937 if (!bus->hdrbuf)
3938 return false;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003939 /* Locate an appropriately-aligned portion of hdrbuf */
3940 bus->rxhdr = (u8 *) roundup((unsigned long)&bus->hdrbuf[0],
Arend van Spriel9b2d2f2a82013-11-29 12:25:14 +01003941 bus->head_align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003942
3943 /* Set the poll and/or interrupt flags */
3944 bus->intr = true;
3945 bus->poll = false;
3946 if (bus->poll)
3947 bus->pollrate = 1;
3948
3949 return true;
3950
3951fail:
Franky Lin38b0b0d2012-11-05 16:22:24 -08003952 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003953 return false;
3954}
3955
Arend van Spriel5b435de2011-10-05 13:19:03 +02003956static int
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003957brcmf_sdio_watchdog_thread(void *data)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003958{
Franky Line92eedf2011-11-22 17:21:50 -08003959 struct brcmf_sdio *bus = (struct brcmf_sdio *)data;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003960
3961 allow_signal(SIGTERM);
3962 /* Run until signal received */
3963 while (1) {
3964 if (kthread_should_stop())
3965 break;
3966 if (!wait_for_completion_interruptible(&bus->watchdog_wait)) {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003967 brcmf_sdio_bus_watchdog(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003968 /* Count the tick for reference */
Arend van Spriel80969832012-06-09 22:51:44 +02003969 bus->sdcnt.tickcnt++;
Arend van Spriel58e9df42014-03-20 10:18:01 +01003970 reinit_completion(&bus->watchdog_wait);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003971 } else
3972 break;
3973 }
3974 return 0;
3975}
3976
3977static void
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003978brcmf_sdio_watchdog(unsigned long data)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003979{
Franky Line92eedf2011-11-22 17:21:50 -08003980 struct brcmf_sdio *bus = (struct brcmf_sdio *)data;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003981
3982 if (bus->watchdog_tsk) {
3983 complete(&bus->watchdog_wait);
3984 /* Reschedule the watchdog */
3985 if (bus->wd_timer_valid)
3986 mod_timer(&bus->timer,
3987 jiffies + BRCMF_WD_POLL_MS * HZ / 1000);
3988 }
3989}
3990
Arend van Sprield9cb2592012-12-05 15:25:54 +01003991static struct brcmf_bus_ops brcmf_sdio_bus_ops = {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003992 .stop = brcmf_sdio_bus_stop,
3993 .preinit = brcmf_sdio_bus_preinit,
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01003994 .txdata = brcmf_sdio_bus_txdata,
3995 .txctl = brcmf_sdio_bus_txctl,
3996 .rxctl = brcmf_sdio_bus_rxctl,
3997 .gettxq = brcmf_sdio_bus_gettxq,
Hante Meuleman330b4e42014-10-28 14:56:05 +01003998 .wowl_config = brcmf_sdio_wowl_config
Arend van Sprield9cb2592012-12-05 15:25:54 +01003999};
4000
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02004001static void brcmf_sdio_firmware_callback(struct device *dev,
4002 const struct firmware *code,
4003 void *nvram, u32 nvram_len)
4004{
4005 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
4006 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
4007 struct brcmf_sdio *bus = sdiodev->bus;
4008 int err = 0;
4009 u8 saveclk;
4010
4011 brcmf_dbg(TRACE, "Enter: dev=%s\n", dev_name(dev));
4012
4013 /* try to download image and nvram to the dongle */
4014 if (bus_if->state == BRCMF_BUS_DOWN) {
4015 bus->alp_only = true;
4016 err = brcmf_sdio_download_firmware(bus, code, nvram, nvram_len);
4017 if (err)
4018 goto fail;
4019 bus->alp_only = false;
4020 }
4021
4022 if (!bus_if->drvr)
4023 return;
4024
4025 /* Start the watchdog timer */
4026 bus->sdcnt.tickcnt = 0;
4027 brcmf_sdio_wd_timer(bus, BRCMF_WD_POLL_MS);
4028
4029 sdio_claim_host(sdiodev->func[1]);
4030
4031 /* Make sure backplane clock is on, needed to generate F2 interrupt */
4032 brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
4033 if (bus->clkstate != CLK_AVAIL)
4034 goto release;
4035
4036 /* Force clocks on backplane to be sure F2 interrupt propagates */
4037 saveclk = brcmf_sdiod_regrb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, &err);
4038 if (!err) {
4039 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
4040 (saveclk | SBSDIO_FORCE_HT), &err);
4041 }
4042 if (err) {
4043 brcmf_err("Failed to force clock for F2: err %d\n", err);
4044 goto release;
4045 }
4046
4047 /* Enable function 2 (frame transfers) */
4048 w_sdreg32(bus, SDPCM_PROT_VERSION << SMB_DATA_VERSION_SHIFT,
4049 offsetof(struct sdpcmd_regs, tosbmailboxdata));
4050 err = sdio_enable_func(sdiodev->func[SDIO_FUNC_2]);
4051
4052
4053 brcmf_dbg(INFO, "enable F2: err=%d\n", err);
4054
4055 /* If F2 successfully enabled, set core and enable interrupts */
4056 if (!err) {
4057 /* Set up the interrupt mask and enable interrupts */
4058 bus->hostintmask = HOSTINTMASK;
4059 w_sdreg32(bus, bus->hostintmask,
4060 offsetof(struct sdpcmd_regs, hostintmask));
4061
4062 brcmf_sdiod_regwb(sdiodev, SBSDIO_WATERMARK, 8, &err);
4063 } else {
4064 /* Disable F2 again */
4065 sdio_disable_func(sdiodev->func[SDIO_FUNC_2]);
4066 goto release;
4067 }
4068
4069 if (brcmf_chip_sr_capable(bus->ci)) {
4070 brcmf_sdio_sr_init(bus);
4071 } else {
4072 /* Restore previous clock setting */
4073 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
4074 saveclk, &err);
4075 }
4076
4077 if (err == 0) {
4078 err = brcmf_sdiod_intr_register(sdiodev);
4079 if (err != 0)
4080 brcmf_err("intr register failed:%d\n", err);
4081 }
4082
4083 /* If we didn't come up, turn off backplane clock */
4084 if (err != 0)
4085 brcmf_sdio_clkctl(bus, CLK_NONE, false);
4086
4087 sdio_release_host(sdiodev->func[1]);
4088
4089 err = brcmf_bus_start(dev);
4090 if (err != 0) {
4091 brcmf_err("dongle is not responding\n");
4092 goto fail;
4093 }
4094 return;
4095
4096release:
4097 sdio_release_host(sdiodev->func[1]);
4098fail:
4099 brcmf_dbg(TRACE, "failed: dev=%s, err=%d\n", dev_name(dev), err);
4100 device_release_driver(dev);
4101}
4102
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01004103struct brcmf_sdio *brcmf_sdio_probe(struct brcmf_sdio_dev *sdiodev)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004104{
4105 int ret;
Franky Line92eedf2011-11-22 17:21:50 -08004106 struct brcmf_sdio *bus;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004107
Arend van Spriel5b435de2011-10-05 13:19:03 +02004108 brcmf_dbg(TRACE, "Enter\n");
4109
Arend van Spriel5b435de2011-10-05 13:19:03 +02004110 /* Allocate private bus interface state */
Franky Line92eedf2011-11-22 17:21:50 -08004111 bus = kzalloc(sizeof(struct brcmf_sdio), GFP_ATOMIC);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004112 if (!bus)
4113 goto fail;
4114
4115 bus->sdiodev = sdiodev;
4116 sdiodev->bus = bus;
Arend van Sprielb83db862011-10-19 12:51:09 +02004117 skb_queue_head_init(&bus->glom);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004118 bus->txbound = BRCMF_TXBOUND;
4119 bus->rxbound = BRCMF_RXBOUND;
4120 bus->txminmax = BRCMF_TXMINMAX;
Franky Lin6bc52312013-08-10 12:27:28 +02004121 bus->tx_seq = SDPCM_SEQ_WRAP - 1;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004122
Arend van Spriele217d1c2013-11-29 12:25:13 +01004123 /* platform specific configuration:
4124 * alignments must be at least 4 bytes for ADMA
Hante Meuleman888bf76e2014-10-28 14:56:17 +01004125 */
Arend van Spriele217d1c2013-11-29 12:25:13 +01004126 bus->head_align = ALIGNMENT;
4127 bus->sgentry_align = ALIGNMENT;
4128 if (sdiodev->pdata) {
4129 if (sdiodev->pdata->sd_head_align > ALIGNMENT)
4130 bus->head_align = sdiodev->pdata->sd_head_align;
4131 if (sdiodev->pdata->sd_sgentry_align > ALIGNMENT)
4132 bus->sgentry_align = sdiodev->pdata->sd_sgentry_align;
4133 }
4134
Hante Meuleman37ac5782012-11-14 18:46:18 -08004135 INIT_WORK(&bus->datawork, brcmf_sdio_dataworker);
4136 bus->brcmf_wq = create_singlethread_workqueue("brcmf_wq");
4137 if (bus->brcmf_wq == NULL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01004138 brcmf_err("insufficient memory to create txworkqueue\n");
Hante Meuleman37ac5782012-11-14 18:46:18 -08004139 goto fail;
4140 }
4141
Arend van Spriel5b435de2011-10-05 13:19:03 +02004142 /* attempt to attach to the dongle */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01004143 if (!(brcmf_sdio_probe_attach(bus))) {
4144 brcmf_err("brcmf_sdio_probe_attach failed\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02004145 goto fail;
4146 }
4147
Franky Lindd43a012012-11-05 16:22:22 -08004148 spin_lock_init(&bus->rxctl_lock);
Hante Meulemanfed7ec42014-03-15 17:18:18 +01004149 spin_lock_init(&bus->txq_lock);
4150 sema_init(&bus->tx_seq_lock, 1);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004151 init_waitqueue_head(&bus->ctrl_wait);
4152 init_waitqueue_head(&bus->dcmd_resp_wait);
4153
4154 /* Set up the watchdog timer */
4155 init_timer(&bus->timer);
4156 bus->timer.data = (unsigned long)bus;
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01004157 bus->timer.function = brcmf_sdio_watchdog;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004158
Arend van Spriel5b435de2011-10-05 13:19:03 +02004159 /* Initialize watchdog thread */
4160 init_completion(&bus->watchdog_wait);
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01004161 bus->watchdog_tsk = kthread_run(brcmf_sdio_watchdog_thread,
Arend van Spriel5b435de2011-10-05 13:19:03 +02004162 bus, "brcmf_watchdog");
4163 if (IS_ERR(bus->watchdog_tsk)) {
Joe Perches02f77192012-01-15 00:38:44 -08004164 pr_warn("brcmf_watchdog thread failed to start\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02004165 bus->watchdog_tsk = NULL;
4166 }
4167 /* Initialize DPC thread */
Arend van Sprielfccfe932013-06-18 13:29:26 +02004168 atomic_set(&bus->dpc_tskcnt, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004169
Franky Lina9ffda82011-12-16 18:37:12 -08004170 /* Assign bus interface call back */
Arend van Sprield9cb2592012-12-05 15:25:54 +01004171 bus->sdiodev->bus_if->dev = bus->sdiodev->dev;
4172 bus->sdiodev->bus_if->ops = &brcmf_sdio_bus_ops;
Arend van Spriel75d907d2013-02-06 18:40:45 +01004173 bus->sdiodev->bus_if->chip = bus->ci->chip;
4174 bus->sdiodev->bus_if->chiprev = bus->ci->chiprev;
Arend van Sprield9cb2592012-12-05 15:25:54 +01004175
Franky Lin706478c2013-08-10 12:27:29 +02004176 /* default sdio bus header length for tx packet */
4177 bus->tx_hdrlen = SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN;
4178
4179 /* Attach to the common layer, reserve hdr space */
Arend van Spriel8dee77b2013-11-29 11:48:14 +01004180 ret = brcmf_attach(bus->sdiodev->dev);
Franky Lin712ac5b2011-12-16 18:37:09 -08004181 if (ret != 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01004182 brcmf_err("brcmf_attach failed\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02004183 goto fail;
4184 }
4185
Daniel Kim7dd3abc2014-05-27 12:56:26 +02004186 /* Query the F2 block size, set roundup accordingly */
4187 bus->blocksize = bus->sdiodev->func[2]->cur_blksize;
4188 bus->roundup = min(max_roundup, bus->blocksize);
4189
Arend van Spriel5b435de2011-10-05 13:19:03 +02004190 /* Allocate buffers */
Arend van Sprielfad13222014-01-06 12:40:40 +01004191 if (bus->sdiodev->bus_if->maxctl) {
Daniel Kim7dd3abc2014-05-27 12:56:26 +02004192 bus->sdiodev->bus_if->maxctl += bus->roundup;
Arend van Sprielfad13222014-01-06 12:40:40 +01004193 bus->rxblen =
4194 roundup((bus->sdiodev->bus_if->maxctl + SDPCM_HDRLEN),
4195 ALIGNMENT) + bus->head_align;
4196 bus->rxbuf = kmalloc(bus->rxblen, GFP_ATOMIC);
4197 if (!(bus->rxbuf)) {
4198 brcmf_err("rxbuf allocation failed\n");
4199 goto fail;
4200 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02004201 }
4202
Arend van Sprielfad13222014-01-06 12:40:40 +01004203 sdio_claim_host(bus->sdiodev->func[1]);
4204
4205 /* Disable F2 to clear any intermediate frame state on the dongle */
4206 sdio_disable_func(bus->sdiodev->func[SDIO_FUNC_2]);
4207
Arend van Sprielfad13222014-01-06 12:40:40 +01004208 bus->rxflow = false;
4209
4210 /* Done with backplane-dependent accesses, can drop clock... */
4211 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, 0, NULL);
4212
4213 sdio_release_host(bus->sdiodev->func[1]);
4214
4215 /* ...and initialize clock/power states */
4216 bus->clkstate = CLK_SDONLY;
4217 bus->idletime = BRCMF_IDLE_INTERVAL;
4218 bus->idleclock = BRCMF_IDLE_ACTIVE;
4219
Arend van Sprielfad13222014-01-06 12:40:40 +01004220 /* SR state */
4221 bus->sleeping = false;
4222 bus->sr_enabled = false;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004223
Arend van Spriel80969832012-06-09 22:51:44 +02004224 brcmf_sdio_debugfs_create(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004225 brcmf_dbg(INFO, "completed!!\n");
4226
Daniel Kimc1b20532014-07-12 08:49:37 +02004227 ret = brcmf_sdio_get_fwnames(bus->ci, sdiodev);
4228 if (ret)
4229 goto fail;
4230
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02004231 ret = brcmf_fw_get_firmwares(sdiodev->dev, BRCMF_FW_REQUEST_NVRAM,
Daniel Kimc1b20532014-07-12 08:49:37 +02004232 sdiodev->fw_name, sdiodev->nvram_name,
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02004233 brcmf_sdio_firmware_callback);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004234 if (ret != 0) {
Arend van Sprielbd0e1b12014-05-27 12:56:22 +02004235 brcmf_err("async firmware request failed: %d\n", ret);
Hante Meuleman1799ddf2012-11-14 18:46:17 -08004236 goto fail;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004237 }
Franky Lin15d45b62011-10-21 16:16:32 +02004238
Arend van Spriel5b435de2011-10-05 13:19:03 +02004239 return bus;
4240
4241fail:
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004242 brcmf_sdio_remove(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004243 return NULL;
4244}
4245
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004246/* Detach and free everything */
4247void brcmf_sdio_remove(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004248{
Arend van Spriel5b435de2011-10-05 13:19:03 +02004249 brcmf_dbg(TRACE, "Enter\n");
4250
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004251 if (bus) {
4252 /* De-register interrupt handler */
4253 brcmf_sdiod_intr_unregister(bus->sdiodev);
4254
Arend van Spriel4faf28b2014-05-27 12:56:18 +02004255 brcmf_detach(bus->sdiodev->dev);
Arend van Sprielbfad4a02014-01-06 12:40:39 +01004256
Hante Meulemane0c180ec2014-01-29 15:32:18 +01004257 cancel_work_sync(&bus->datawork);
4258 if (bus->brcmf_wq)
4259 destroy_workqueue(bus->brcmf_wq);
4260
Arend van Sprielbfad4a02014-01-06 12:40:39 +01004261 if (bus->ci) {
Arend van Sprielbb350712014-01-13 22:20:29 +01004262 if (bus->sdiodev->bus_if->state == BRCMF_BUS_DOWN) {
4263 sdio_claim_host(bus->sdiodev->func[1]);
4264 brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
4265 /* Leave the device in state where it is
4266 * 'quiet'. This is done by putting it in
4267 * download_state which essentially resets
4268 * all necessary cores.
4269 */
4270 msleep(20);
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01004271 brcmf_chip_enter_download(bus->ci);
Arend van Sprielbb350712014-01-13 22:20:29 +01004272 brcmf_sdio_clkctl(bus, CLK_NONE, false);
4273 sdio_release_host(bus->sdiodev->func[1]);
4274 }
Arend van Sprielcb7cf7b2014-01-29 15:32:19 +01004275 brcmf_chip_detach(bus->ci);
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004276 }
4277
Arend van Sprielbfad4a02014-01-06 12:40:39 +01004278 kfree(bus->rxbuf);
Arend van Spriel9fbe2a62013-12-12 11:59:05 +01004279 kfree(bus->hdrbuf);
4280 kfree(bus);
4281 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02004282
4283 brcmf_dbg(TRACE, "Disconnected\n");
4284}
4285
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01004286void brcmf_sdio_wd_timer(struct brcmf_sdio *bus, uint wdtick)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004287{
Arend van Spriel5b435de2011-10-05 13:19:03 +02004288 /* Totally stop the timer */
Joe Perches23677ce2012-02-09 11:17:23 +00004289 if (!wdtick && bus->wd_timer_valid) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02004290 del_timer_sync(&bus->timer);
4291 bus->wd_timer_valid = false;
4292 bus->save_ms = wdtick;
4293 return;
4294 }
4295
Franky Linece960e2011-10-21 16:16:19 +02004296 /* don't start the wd until fw is loaded */
Arend van Sprield6ae2c52014-01-06 12:40:44 +01004297 if (bus->sdiodev->bus_if->state != BRCMF_BUS_DATA)
Franky Linece960e2011-10-21 16:16:19 +02004298 return;
4299
Arend van Spriel5b435de2011-10-05 13:19:03 +02004300 if (wdtick) {
4301 if (bus->save_ms != BRCMF_WD_POLL_MS) {
Joe Perches23677ce2012-02-09 11:17:23 +00004302 if (bus->wd_timer_valid)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004303 /* Stop timer and restart at new value */
4304 del_timer_sync(&bus->timer);
4305
4306 /* Create timer again when watchdog period is
4307 dynamically changed or in the first instance
4308 */
4309 bus->timer.expires =
4310 jiffies + BRCMF_WD_POLL_MS * HZ / 1000;
4311 add_timer(&bus->timer);
4312
4313 } else {
4314 /* Re arm the timer, at last watchdog period */
4315 mod_timer(&bus->timer,
4316 jiffies + BRCMF_WD_POLL_MS * HZ / 1000);
4317 }
4318
4319 bus->wd_timer_valid = true;
4320 bus->save_ms = wdtick;
4321 }
4322}