]> git.proxmox.com Git - mirror_ubuntu-disco-kernel.git/blame - drivers/net/wireless/brcm80211/brcmfmac/dhd_sdio.c
brcmfmac: Remove immediate sleep support from SDIO.
[mirror_ubuntu-disco-kernel.git] / drivers / net / wireless / brcm80211 / brcmfmac / dhd_sdio.c
CommitLineData
5b435de0
AS
1/*
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>
cb7cf7be 26#include <linux/mmc/sdio_ids.h>
5b435de0
AS
27#include <linux/mmc/sdio_func.h>
28#include <linux/mmc/card.h>
29#include <linux/semaphore.h>
30#include <linux/firmware.h>
b7a57e76 31#include <linux/module.h>
99ba15cd 32#include <linux/bcma/bcma.h>
4fc0d016 33#include <linux/debugfs.h>
8dc01811 34#include <linux/vmalloc.h>
668761ac 35#include <linux/platform_data/brcmfmac-sdio.h>
8da9d2c8 36#include <linux/moduleparam.h>
5b435de0
AS
37#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>
43#include "sdio_host.h"
20c9c9bc 44#include "chip.h"
a74d036f 45#include "nvram.h"
5b435de0
AS
46
47#define DCMD_RESP_TIMEOUT 2000 /* In milli second */
48
8ae74654 49#ifdef DEBUG
5b435de0
AS
50
51#define BRCMF_TRAP_INFO_SIZE 80
52
53#define CBUF_LEN (128)
54
4fc0d016
AS
55/* Device console log buffer state */
56#define CONSOLE_BUFFER_MAX 2024
57
5b435de0
AS
58struct 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
8ae74654 96#endif /* DEBUG */
5b435de0
AS
97#include <chipcommon.h>
98
5b435de0 99#include "dhd_bus.h"
5b435de0 100#include "dhd_dbg.h"
40c1c249 101#include "tracepoint.h"
5b435de0
AS
102
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
8da9d2c8
FL
116#define BRCMF_DEFAULT_TXGLOM_SIZE 32 /* max tx frames in glom chain */
117
5b435de0
AS
118#define BRCMF_TXMINMAX 1 /* Max tx frames if rx still pending */
119
120#define MEMBLOCK 2048 /* Block size used for downloading
121 of dongle image */
122#define MAX_DATA_BUF (32 * 1024) /* Must be large enough to hold
123 biggest possible glom */
124
125#define BRCMF_FIRSTREAD (1 << 6)
126
127
128/* SBSDIO_DEVICE_CTL */
129
130/* 1: device will assert busy signal when receiving CMD53 */
131#define SBSDIO_DEVCTL_SETBUSY 0x01
132/* 1: assertion of sdio interrupt is synchronous to the sdio clock */
133#define SBSDIO_DEVCTL_SPI_INTR_SYNC 0x02
134/* 1: mask all interrupts to host except the chipActive (rev 8) */
135#define SBSDIO_DEVCTL_CA_INT_ONLY 0x04
136/* 1: isolate internal sdio signals, put external pads in tri-state; requires
137 * sdio bus power cycle to clear (rev 9) */
138#define SBSDIO_DEVCTL_PADS_ISO 0x08
139/* Force SD->SB reset mapping (rev 11) */
140#define SBSDIO_DEVCTL_SB_RST_CTL 0x30
141/* Determined by CoreControl bit */
142#define SBSDIO_DEVCTL_RST_CORECTL 0x00
143/* Force backplane reset */
144#define SBSDIO_DEVCTL_RST_BPRESET 0x10
145/* Force no backplane reset */
146#define SBSDIO_DEVCTL_RST_NOBPRESET 0x20
147
5b435de0
AS
148/* direct(mapped) cis space */
149
150/* MAPPED common CIS address */
151#define SBSDIO_CIS_BASE_COMMON 0x1000
152/* maximum bytes in one CIS */
153#define SBSDIO_CIS_SIZE_LIMIT 0x200
154/* cis offset addr is < 17 bits */
155#define SBSDIO_CIS_OFT_ADDR_MASK 0x1FFFF
156
157/* manfid tuple length, include tuple, link bytes */
158#define SBSDIO_CIS_MANFID_TUPLE_LEN 6
159
cb7cf7be
AS
160#define CORE_BUS_REG(base, field) \
161 (base + offsetof(struct sdpcmd_regs, field))
162
163/* SDIO function 1 register CHIPCLKCSR */
164/* Force ALP request to backplane */
165#define SBSDIO_FORCE_ALP 0x01
166/* Force HT request to backplane */
167#define SBSDIO_FORCE_HT 0x02
168/* Force ILP request to backplane */
169#define SBSDIO_FORCE_ILP 0x04
170/* Make ALP ready (power up xtal) */
171#define SBSDIO_ALP_AVAIL_REQ 0x08
172/* Make HT ready (power up PLL) */
173#define SBSDIO_HT_AVAIL_REQ 0x10
174/* Squelch clock requests from HW */
175#define SBSDIO_FORCE_HW_CLKREQ_OFF 0x20
176/* Status: ALP is ready */
177#define SBSDIO_ALP_AVAIL 0x40
178/* Status: HT is ready */
179#define SBSDIO_HT_AVAIL 0x80
180#define SBSDIO_AVBITS (SBSDIO_HT_AVAIL | SBSDIO_ALP_AVAIL)
181#define SBSDIO_ALPAV(regval) ((regval) & SBSDIO_AVBITS)
182#define SBSDIO_HTAV(regval) (((regval) & SBSDIO_AVBITS) == SBSDIO_AVBITS)
183#define SBSDIO_ALPONLY(regval) (SBSDIO_ALPAV(regval) && !SBSDIO_HTAV(regval))
184#define SBSDIO_CLKAV(regval, alponly) \
185 (SBSDIO_ALPAV(regval) && (alponly ? 1 : SBSDIO_HTAV(regval)))
186
5b435de0
AS
187/* intstatus */
188#define I_SMB_SW0 (1 << 0) /* To SB Mail S/W interrupt 0 */
189#define I_SMB_SW1 (1 << 1) /* To SB Mail S/W interrupt 1 */
190#define I_SMB_SW2 (1 << 2) /* To SB Mail S/W interrupt 2 */
191#define I_SMB_SW3 (1 << 3) /* To SB Mail S/W interrupt 3 */
192#define I_SMB_SW_MASK 0x0000000f /* To SB Mail S/W interrupts mask */
193#define I_SMB_SW_SHIFT 0 /* To SB Mail S/W interrupts shift */
194#define I_HMB_SW0 (1 << 4) /* To Host Mail S/W interrupt 0 */
195#define I_HMB_SW1 (1 << 5) /* To Host Mail S/W interrupt 1 */
196#define I_HMB_SW2 (1 << 6) /* To Host Mail S/W interrupt 2 */
197#define I_HMB_SW3 (1 << 7) /* To Host Mail S/W interrupt 3 */
198#define I_HMB_SW_MASK 0x000000f0 /* To Host Mail S/W interrupts mask */
199#define I_HMB_SW_SHIFT 4 /* To Host Mail S/W interrupts shift */
200#define I_WR_OOSYNC (1 << 8) /* Write Frame Out Of Sync */
201#define I_RD_OOSYNC (1 << 9) /* Read Frame Out Of Sync */
202#define I_PC (1 << 10) /* descriptor error */
203#define I_PD (1 << 11) /* data error */
204#define I_DE (1 << 12) /* Descriptor protocol Error */
205#define I_RU (1 << 13) /* Receive descriptor Underflow */
206#define I_RO (1 << 14) /* Receive fifo Overflow */
207#define I_XU (1 << 15) /* Transmit fifo Underflow */
208#define I_RI (1 << 16) /* Receive Interrupt */
209#define I_BUSPWR (1 << 17) /* SDIO Bus Power Change (rev 9) */
210#define I_XMTDATA_AVAIL (1 << 23) /* bits in fifo */
211#define I_XI (1 << 24) /* Transmit Interrupt */
212#define I_RF_TERM (1 << 25) /* Read Frame Terminate */
213#define I_WF_TERM (1 << 26) /* Write Frame Terminate */
214#define I_PCMCIA_XU (1 << 27) /* PCMCIA Transmit FIFO Underflow */
215#define I_SBINT (1 << 28) /* sbintstatus Interrupt */
216#define I_CHIPACTIVE (1 << 29) /* chip from doze to active state */
217#define I_SRESET (1 << 30) /* CCCR RES interrupt */
218#define I_IOE2 (1U << 31) /* CCCR IOE2 Bit Changed */
219#define I_ERRORS (I_PC | I_PD | I_DE | I_RU | I_RO | I_XU)
220#define I_DMA (I_RI | I_XI | I_ERRORS)
221
222/* corecontrol */
223#define CC_CISRDY (1 << 0) /* CIS Ready */
224#define CC_BPRESEN (1 << 1) /* CCCR RES signal */
225#define CC_F2RDY (1 << 2) /* set CCCR IOR2 bit */
226#define CC_CLRPADSISO (1 << 3) /* clear SDIO pads isolation */
227#define CC_XMTDATAAVAIL_MODE (1 << 4)
228#define CC_XMTDATAAVAIL_CTRL (1 << 5)
229
230/* SDA_FRAMECTRL */
231#define SFC_RF_TERM (1 << 0) /* Read Frame Terminate */
232#define SFC_WF_TERM (1 << 1) /* Write Frame Terminate */
233#define SFC_CRC4WOOS (1 << 2) /* CRC error for write out of sync */
234#define SFC_ABORTALL (1 << 3) /* Abort all in-progress frames */
235
5b435de0
AS
236/*
237 * Software allocation of To SB Mailbox resources
238 */
239
240/* tosbmailbox bits corresponding to intstatus bits */
241#define SMB_NAK (1 << 0) /* Frame NAK */
242#define SMB_INT_ACK (1 << 1) /* Host Interrupt ACK */
243#define SMB_USE_OOB (1 << 2) /* Use OOB Wakeup */
244#define SMB_DEV_INT (1 << 3) /* Miscellaneous Interrupt */
245
246/* tosbmailboxdata */
247#define SMB_DATA_VERSION_SHIFT 16 /* host protocol version */
248
249/*
250 * Software allocation of To Host Mailbox resources
251 */
252
253/* intstatus bits */
254#define I_HMB_FC_STATE I_HMB_SW0 /* Flow Control State */
255#define I_HMB_FC_CHANGE I_HMB_SW1 /* Flow Control State Changed */
256#define I_HMB_FRAME_IND I_HMB_SW2 /* Frame Indication */
257#define I_HMB_HOST_INT I_HMB_SW3 /* Miscellaneous Interrupt */
258
259/* tohostmailboxdata */
260#define HMB_DATA_NAKHANDLED 1 /* retransmit NAK'd frame */
261#define HMB_DATA_DEVREADY 2 /* talk to host after enable */
262#define HMB_DATA_FC 4 /* per prio flowcontrol update flag */
263#define HMB_DATA_FWREADY 8 /* fw ready for protocol activity */
264
265#define HMB_DATA_FCDATA_MASK 0xff000000
266#define HMB_DATA_FCDATA_SHIFT 24
267
268#define HMB_DATA_VERSION_MASK 0x00ff0000
269#define HMB_DATA_VERSION_SHIFT 16
270
271/*
272 * Software-defined protocol header
273 */
274
275/* Current protocol version */
276#define SDPCM_PROT_VERSION 4
277
5b435de0
AS
278/*
279 * Shared structure between dongle and the host.
280 * The structure contains pointers to trap or assert information.
281 */
4fc0d016 282#define SDPCM_SHARED_VERSION 0x0003
5b435de0
AS
283#define SDPCM_SHARED_VERSION_MASK 0x00FF
284#define SDPCM_SHARED_ASSERT_BUILT 0x0100
285#define SDPCM_SHARED_ASSERT 0x0200
286#define SDPCM_SHARED_TRAP 0x0400
287
288/* Space for header read, limit for data packets */
289#define MAX_HDR_READ (1 << 6)
290#define MAX_RX_DATASZ 2048
291
5b435de0
AS
292/* Bump up limit on waiting for HT to account for first startup;
293 * if the image is doing a CRC calculation before programming the PMU
294 * for HT availability, it could take a couple hundred ms more, so
295 * max out at a 1 second (1000000us).
296 */
297#undef PMU_MAX_TRANSITION_DLY
298#define PMU_MAX_TRANSITION_DLY 1000000
299
300/* Value for ChipClockCSR during initial setup */
301#define BRCMF_INIT_CLKCTL1 (SBSDIO_FORCE_HW_CLKREQ_OFF | \
302 SBSDIO_ALP_AVAIL_REQ)
303
304/* Flags for SDH calls */
305#define F2SYNC (SDIO_REQ_4BYTE | SDIO_REQ_FIXED)
306
382a9e0f
FL
307#define BRCMF_IDLE_ACTIVE 0 /* Do not request any SD clock change
308 * when idle
309 */
310#define BRCMF_IDLE_INTERVAL 1
311
4a3da990
PH
312#define KSO_WAIT_US 50
313#define MAX_KSO_ATTEMPTS (PMU_MAX_TRANSITION_DLY/KSO_WAIT_US)
314
5b435de0
AS
315/*
316 * Conversion of 802.1D priority to precedence level
317 */
318static uint prio2prec(u32 prio)
319{
320 return (prio == PRIO_8021D_NONE || prio == PRIO_8021D_BE) ?
321 (prio^2) : prio;
322}
323
8ae74654 324#ifdef DEBUG
5b435de0
AS
325/* Device console log buffer state */
326struct brcmf_console {
327 uint count; /* Poll interval msec counter */
328 uint log_addr; /* Log struct address (fixed) */
329 struct rte_log_le log_le; /* Log struct (host copy) */
330 uint bufsize; /* Size of log buffer */
331 u8 *buf; /* Log buffer (host copy) */
332 uint last; /* Last buffer read index */
333};
4fc0d016
AS
334
335struct brcmf_trap_info {
336 __le32 type;
337 __le32 epc;
338 __le32 cpsr;
339 __le32 spsr;
340 __le32 r0; /* a1 */
341 __le32 r1; /* a2 */
342 __le32 r2; /* a3 */
343 __le32 r3; /* a4 */
344 __le32 r4; /* v1 */
345 __le32 r5; /* v2 */
346 __le32 r6; /* v3 */
347 __le32 r7; /* v4 */
348 __le32 r8; /* v5 */
349 __le32 r9; /* sb/v6 */
350 __le32 r10; /* sl/v7 */
351 __le32 r11; /* fp/v8 */
352 __le32 r12; /* ip */
353 __le32 r13; /* sp */
354 __le32 r14; /* lr */
355 __le32 pc; /* r15 */
356};
8ae74654 357#endif /* DEBUG */
5b435de0
AS
358
359struct sdpcm_shared {
360 u32 flags;
361 u32 trap_addr;
362 u32 assert_exp_addr;
363 u32 assert_file_addr;
364 u32 assert_line;
365 u32 console_addr; /* Address of struct rte_console */
366 u32 msgtrace_addr;
367 u8 tag[32];
4fc0d016 368 u32 brpt_addr;
5b435de0
AS
369};
370
371struct sdpcm_shared_le {
372 __le32 flags;
373 __le32 trap_addr;
374 __le32 assert_exp_addr;
375 __le32 assert_file_addr;
376 __le32 assert_line;
377 __le32 console_addr; /* Address of struct rte_console */
378 __le32 msgtrace_addr;
379 u8 tag[32];
4fc0d016 380 __le32 brpt_addr;
5b435de0
AS
381};
382
6bc52319
FL
383/* dongle SDIO bus specific header info */
384struct brcmf_sdio_hdrinfo {
4754fcee
FL
385 u8 seq_num;
386 u8 channel;
387 u16 len;
388 u16 len_left;
389 u16 len_nxtfrm;
390 u8 dat_offset;
8da9d2c8
FL
391 bool lastfrm;
392 u16 tail_pad;
4754fcee 393};
5b435de0
AS
394
395/* misc chip info needed by some of the routines */
5b435de0 396/* Private data for SDIO bus interaction */
e92eedf4 397struct brcmf_sdio {
5b435de0 398 struct brcmf_sdio_dev *sdiodev; /* sdio device handler */
9cf218fc 399 struct brcmf_chip *ci; /* Chip info struct */
5b435de0
AS
400
401 u32 ramsize; /* Size of RAM in SOCRAM (bytes) */
402
403 u32 hostintmask; /* Copy of Host Interrupt Mask */
4531603a
FL
404 atomic_t intstatus; /* Intstatus bits (events) pending */
405 atomic_t fcstate; /* State of dongle flow-control */
5b435de0
AS
406
407 uint blocksize; /* Block size of SDIO transfers */
408 uint roundup; /* Max roundup limit */
409
410 struct pktq txq; /* Queue length used for flow-control */
411 u8 flowcontrol; /* per prio flow control bitmask */
412 u8 tx_seq; /* Transmit sequence number (next) */
413 u8 tx_max; /* Maximum transmit sequence allowed */
414
9b2d2f2a 415 u8 *hdrbuf; /* buffer for handling rx frame */
5b435de0 416 u8 *rxhdr; /* Header of current rx frame (in hdrbuf) */
5b435de0 417 u8 rx_seq; /* Receive sequence number (expected) */
6bc52319 418 struct brcmf_sdio_hdrinfo cur_read;
4754fcee 419 /* info of current read frame */
5b435de0 420 bool rxskip; /* Skip receive (awaiting NAK ACK) */
4754fcee 421 bool rxpending; /* Data frame pending in dongle */
5b435de0
AS
422
423 uint rxbound; /* Rx frames to read before resched */
424 uint txbound; /* Tx frames to send before resched */
425 uint txminmax;
426
427 struct sk_buff *glomd; /* Packet containing glomming descriptor */
b83db862 428 struct sk_buff_head glom; /* Packet list for glommed superframe */
5b435de0
AS
429 uint glomerr; /* Glom packet read errors */
430
431 u8 *rxbuf; /* Buffer for receiving control packets */
432 uint rxblen; /* Allocated length of rxbuf */
433 u8 *rxctl; /* Aligned pointer into rxbuf */
dd43a01c 434 u8 *rxctl_orig; /* pointer for freeing rxctl */
5b435de0 435 uint rxlen; /* Length of valid data in buffer */
dd43a01c 436 spinlock_t rxctl_lock; /* protection lock for ctrl frame resources */
5b435de0
AS
437
438 u8 sdpcm_ver; /* Bus protocol reported by dongle */
439
440 bool intr; /* Use interrupts */
441 bool poll; /* Use polling */
1d382273 442 atomic_t ipend; /* Device interrupt is pending */
5b435de0
AS
443 uint spurious; /* Count of spurious interrupts */
444 uint pollrate; /* Ticks between device polls */
445 uint polltick; /* Tick counter */
5b435de0 446
8ae74654 447#ifdef DEBUG
5b435de0
AS
448 uint console_interval;
449 struct brcmf_console console; /* Console output polling support */
450 uint console_addr; /* Console address from shared struct */
8ae74654 451#endif /* DEBUG */
5b435de0 452
5b435de0
AS
453 uint clkstate; /* State of sd and backplane clock(s) */
454 bool activity; /* Activity flag for clock down */
455 s32 idletime; /* Control for activity timeout */
456 s32 idlecount; /* Activity timeout counter */
457 s32 idleclock; /* How to set bus driver when idle */
5b435de0
AS
458 bool rxflow_mode; /* Rx flow control mode */
459 bool rxflow; /* Is rx flow control on */
460 bool alp_only; /* Don't use HT clock (ALP only) */
5b435de0 461
5b435de0
AS
462 u8 *ctrl_frame_buf;
463 u32 ctrl_frame_len;
464 bool ctrl_frame_stat;
465
466 spinlock_t txqlock;
467 wait_queue_head_t ctrl_wait;
468 wait_queue_head_t dcmd_resp_wait;
469
470 struct timer_list timer;
471 struct completion watchdog_wait;
472 struct task_struct *watchdog_tsk;
473 bool wd_timer_valid;
474 uint save_ms;
475
f1e68c2e
FL
476 struct workqueue_struct *brcmf_wq;
477 struct work_struct datawork;
fccfe930 478 atomic_t dpc_tskcnt;
5b435de0 479
c8bf3484 480 bool txoff; /* Transmit flow-controlled */
80969836 481 struct brcmf_sdio_count sdcnt;
4a3da990
PH
482 bool sr_enabled; /* SaveRestore enabled */
483 bool sleeping; /* SDIO bus sleeping */
706478cb
FL
484
485 u8 tx_hdrlen; /* sdio bus header length for tx packet */
8da9d2c8
FL
486 bool txglom; /* host tx glomming enable flag */
487 struct sk_buff *txglom_sgpad; /* scatter-gather padding buffer */
e217d1c8
AS
488 u16 head_align; /* buffer pointer alignment */
489 u16 sgentry_align; /* scatter-gather buffer alignment */
5b435de0
AS
490};
491
5b435de0
AS
492/* clkstate */
493#define CLK_NONE 0
494#define CLK_SDONLY 1
4a3da990 495#define CLK_PENDING 2
5b435de0
AS
496#define CLK_AVAIL 3
497
8ae74654 498#ifdef DEBUG
5b435de0 499static int qcount[NUMPRIO];
8ae74654 500#endif /* DEBUG */
5b435de0 501
668761ac 502#define DEFAULT_SDIO_DRIVE_STRENGTH 6 /* in milliamps */
5b435de0
AS
503
504#define RETRYCHAN(chan) ((chan) == SDPCM_EVENT_CHANNEL)
505
506/* Retry count for register access failures */
507static const uint retry_limit = 2;
508
509/* Limit on rounding up frames */
510static const uint max_roundup = 512;
511
512#define ALIGNMENT 4
513
8da9d2c8
FL
514static int brcmf_sdio_txglomsz = BRCMF_DEFAULT_TXGLOM_SIZE;
515module_param_named(txglomsz, brcmf_sdio_txglomsz, int, 0);
516MODULE_PARM_DESC(txglomsz, "maximum tx packet chain size [SDIO]");
517
9d7d6f95
FL
518enum brcmf_sdio_frmtype {
519 BRCMF_SDIO_FT_NORMAL,
520 BRCMF_SDIO_FT_SUPER,
521 BRCMF_SDIO_FT_SUB,
522};
523
65d80d0b
AS
524#define SDIOD_DRVSTR_KEY(chip, pmu) (((chip) << 16) | (pmu))
525
526/* SDIO Pad drive strength to select value mappings */
527struct sdiod_drive_str {
528 u8 strength; /* Pad Drive Strength in mA */
529 u8 sel; /* Chip-specific select value */
530};
531
532/* SDIO Drive Strength to sel value table for PMU Rev 11 (1.8V) */
533static const struct sdiod_drive_str sdiod_drvstr_tab1_1v8[] = {
534 {32, 0x6},
535 {26, 0x7},
536 {22, 0x4},
537 {16, 0x5},
538 {12, 0x2},
539 {8, 0x3},
540 {4, 0x0},
541 {0, 0x1}
542};
543
544/* SDIO Drive Strength to sel value table for PMU Rev 13 (1.8v) */
545static const struct sdiod_drive_str sdiod_drive_strength_tab5_1v8[] = {
546 {6, 0x7},
547 {5, 0x6},
548 {4, 0x5},
549 {3, 0x4},
550 {2, 0x2},
551 {1, 0x1},
552 {0, 0x0}
553};
554
555/* SDIO Drive Strength to sel value table for PMU Rev 17 (1.8v) */
556static const struct sdiod_drive_str sdiod_drvstr_tab6_1v8[] = {
557 {3, 0x3},
558 {2, 0x2},
559 {1, 0x1},
560 {0, 0x0} };
561
562/* SDIO Drive Strength to sel value table for 43143 PMU Rev 17 (3.3V) */
563static const struct sdiod_drive_str sdiod_drvstr_tab2_3v3[] = {
564 {16, 0x7},
565 {12, 0x5},
566 {8, 0x3},
567 {4, 0x1}
568};
569
f2c44fe7
HM
570#define BCM43143_FIRMWARE_NAME "brcm/brcmfmac43143-sdio.bin"
571#define BCM43143_NVRAM_NAME "brcm/brcmfmac43143-sdio.txt"
572#define BCM43241B0_FIRMWARE_NAME "brcm/brcmfmac43241b0-sdio.bin"
573#define BCM43241B0_NVRAM_NAME "brcm/brcmfmac43241b0-sdio.txt"
574#define BCM43241B4_FIRMWARE_NAME "brcm/brcmfmac43241b4-sdio.bin"
575#define BCM43241B4_NVRAM_NAME "brcm/brcmfmac43241b4-sdio.txt"
576#define BCM4329_FIRMWARE_NAME "brcm/brcmfmac4329-sdio.bin"
577#define BCM4329_NVRAM_NAME "brcm/brcmfmac4329-sdio.txt"
578#define BCM4330_FIRMWARE_NAME "brcm/brcmfmac4330-sdio.bin"
579#define BCM4330_NVRAM_NAME "brcm/brcmfmac4330-sdio.txt"
580#define BCM4334_FIRMWARE_NAME "brcm/brcmfmac4334-sdio.bin"
581#define BCM4334_NVRAM_NAME "brcm/brcmfmac4334-sdio.txt"
582#define BCM4335_FIRMWARE_NAME "brcm/brcmfmac4335-sdio.bin"
583#define BCM4335_NVRAM_NAME "brcm/brcmfmac4335-sdio.txt"
11e69c36
AS
584#define BCM43362_FIRMWARE_NAME "brcm/brcmfmac43362-sdio.bin"
585#define BCM43362_NVRAM_NAME "brcm/brcmfmac43362-sdio.txt"
bed89b64
FL
586#define BCM4339_FIRMWARE_NAME "brcm/brcmfmac4339-sdio.bin"
587#define BCM4339_NVRAM_NAME "brcm/brcmfmac4339-sdio.txt"
f2c44fe7
HM
588
589MODULE_FIRMWARE(BCM43143_FIRMWARE_NAME);
590MODULE_FIRMWARE(BCM43143_NVRAM_NAME);
591MODULE_FIRMWARE(BCM43241B0_FIRMWARE_NAME);
592MODULE_FIRMWARE(BCM43241B0_NVRAM_NAME);
593MODULE_FIRMWARE(BCM43241B4_FIRMWARE_NAME);
594MODULE_FIRMWARE(BCM43241B4_NVRAM_NAME);
595MODULE_FIRMWARE(BCM4329_FIRMWARE_NAME);
596MODULE_FIRMWARE(BCM4329_NVRAM_NAME);
597MODULE_FIRMWARE(BCM4330_FIRMWARE_NAME);
598MODULE_FIRMWARE(BCM4330_NVRAM_NAME);
599MODULE_FIRMWARE(BCM4334_FIRMWARE_NAME);
600MODULE_FIRMWARE(BCM4334_NVRAM_NAME);
601MODULE_FIRMWARE(BCM4335_FIRMWARE_NAME);
602MODULE_FIRMWARE(BCM4335_NVRAM_NAME);
11e69c36
AS
603MODULE_FIRMWARE(BCM43362_FIRMWARE_NAME);
604MODULE_FIRMWARE(BCM43362_NVRAM_NAME);
bed89b64
FL
605MODULE_FIRMWARE(BCM4339_FIRMWARE_NAME);
606MODULE_FIRMWARE(BCM4339_NVRAM_NAME);
f2c44fe7
HM
607
608struct brcmf_firmware_names {
609 u32 chipid;
610 u32 revmsk;
611 const char *bin;
612 const char *nv;
613};
614
615enum brcmf_firmware_type {
616 BRCMF_FIRMWARE_BIN,
617 BRCMF_FIRMWARE_NVRAM
618};
619
620#define BRCMF_FIRMWARE_NVRAM(name) \
621 name ## _FIRMWARE_NAME, name ## _NVRAM_NAME
622
623static const struct brcmf_firmware_names brcmf_fwname_data[] = {
624 { BCM43143_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM43143) },
625 { BCM43241_CHIP_ID, 0x0000001F, BRCMF_FIRMWARE_NVRAM(BCM43241B0) },
626 { BCM43241_CHIP_ID, 0xFFFFFFE0, BRCMF_FIRMWARE_NVRAM(BCM43241B4) },
627 { BCM4329_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM4329) },
628 { BCM4330_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM4330) },
629 { BCM4334_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM4334) },
bed89b64 630 { BCM4335_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM4335) },
11e69c36 631 { BCM43362_CHIP_ID, 0xFFFFFFFE, BRCMF_FIRMWARE_NVRAM(BCM43362) },
bed89b64 632 { BCM4339_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM4339) }
f2c44fe7
HM
633};
634
635
82d7f3c1 636static const struct firmware *brcmf_sdio_get_fw(struct brcmf_sdio *bus,
f2c44fe7
HM
637 enum brcmf_firmware_type type)
638{
639 const struct firmware *fw;
640 const char *name;
641 int err, i;
642
643 for (i = 0; i < ARRAY_SIZE(brcmf_fwname_data); i++) {
644 if (brcmf_fwname_data[i].chipid == bus->ci->chip &&
645 brcmf_fwname_data[i].revmsk & BIT(bus->ci->chiprev)) {
646 switch (type) {
647 case BRCMF_FIRMWARE_BIN:
648 name = brcmf_fwname_data[i].bin;
649 break;
650 case BRCMF_FIRMWARE_NVRAM:
651 name = brcmf_fwname_data[i].nv;
652 break;
653 default:
654 brcmf_err("invalid firmware type (%d)\n", type);
655 return NULL;
656 }
657 goto found;
658 }
659 }
660 brcmf_err("Unknown chipid %d [%d]\n",
661 bus->ci->chip, bus->ci->chiprev);
662 return NULL;
663
664found:
665 err = request_firmware(&fw, name, &bus->sdiodev->func[2]->dev);
666 if ((err) || (!fw)) {
667 brcmf_err("fail to request firmware %s (%d)\n", name, err);
668 return NULL;
669 }
670
671 return fw;
672}
673
5b435de0
AS
674static void pkt_align(struct sk_buff *p, int len, int align)
675{
676 uint datalign;
677 datalign = (unsigned long)(p->data);
678 datalign = roundup(datalign, (align)) - datalign;
679 if (datalign)
680 skb_pull(p, datalign);
681 __skb_trim(p, len);
682}
683
684/* To check if there's window offered */
e92eedf4 685static bool data_ok(struct brcmf_sdio *bus)
5b435de0
AS
686{
687 return (u8)(bus->tx_max - bus->tx_seq) != 0 &&
688 ((u8)(bus->tx_max - bus->tx_seq) & 0x80) == 0;
689}
690
691/*
692 * Reads a register in the SDIO hardware block. This block occupies a series of
693 * adresses on the 32 bit backplane bus.
694 */
cb7cf7be 695static int r_sdreg32(struct brcmf_sdio *bus, u32 *regvar, u32 offset)
5b435de0 696{
cb7cf7be 697 struct brcmf_core *core;
79ae3957 698 int ret;
58692750 699
cb7cf7be
AS
700 core = brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV);
701 *regvar = brcmf_sdiod_regrl(bus->sdiodev, core->base + offset, &ret);
58692750
FL
702
703 return ret;
5b435de0
AS
704}
705
cb7cf7be 706static int w_sdreg32(struct brcmf_sdio *bus, u32 regval, u32 reg_offset)
5b435de0 707{
cb7cf7be 708 struct brcmf_core *core;
e13ce26b 709 int ret;
58692750 710
cb7cf7be
AS
711 core = brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV);
712 brcmf_sdiod_regwl(bus->sdiodev, core->base + reg_offset, regval, &ret);
58692750
FL
713
714 return ret;
5b435de0
AS
715}
716
4a3da990 717static int
82d7f3c1 718brcmf_sdio_kso_control(struct brcmf_sdio *bus, bool on)
4a3da990
PH
719{
720 u8 wr_val = 0, rd_val, cmp_val, bmask;
721 int err = 0;
722 int try_cnt = 0;
723
724 brcmf_dbg(TRACE, "Enter\n");
725
726 wr_val = (on << SBSDIO_FUNC1_SLEEPCSR_KSO_SHIFT);
727 /* 1st KSO write goes to AOS wake up core if device is asleep */
a39be27b
AS
728 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
729 wr_val, &err);
4a3da990
PH
730 if (err) {
731 brcmf_err("SDIO_AOS KSO write error: %d\n", err);
732 return err;
733 }
734
735 if (on) {
736 /* device WAKEUP through KSO:
737 * write bit 0 & read back until
738 * both bits 0 (kso bit) & 1 (dev on status) are set
739 */
740 cmp_val = SBSDIO_FUNC1_SLEEPCSR_KSO_MASK |
741 SBSDIO_FUNC1_SLEEPCSR_DEVON_MASK;
742 bmask = cmp_val;
743 usleep_range(2000, 3000);
744 } else {
745 /* Put device to sleep, turn off KSO */
746 cmp_val = 0;
747 /* only check for bit0, bit1(dev on status) may not
748 * get cleared right away
749 */
750 bmask = SBSDIO_FUNC1_SLEEPCSR_KSO_MASK;
751 }
752
753 do {
754 /* reliable KSO bit set/clr:
755 * the sdiod sleep write access is synced to PMU 32khz clk
756 * just one write attempt may fail,
757 * read it back until it matches written value
758 */
a39be27b
AS
759 rd_val = brcmf_sdiod_regrb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
760 &err);
4a3da990
PH
761 if (((rd_val & bmask) == cmp_val) && !err)
762 break;
763 brcmf_dbg(SDIO, "KSO wr/rd retry:%d (max: %d) ERR:%x\n",
764 try_cnt, MAX_KSO_ATTEMPTS, err);
765 udelay(KSO_WAIT_US);
a39be27b
AS
766 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
767 wr_val, &err);
4a3da990
PH
768 } while (try_cnt++ < MAX_KSO_ATTEMPTS);
769
770 return err;
771}
772
5b435de0
AS
773#define PKT_AVAILABLE() (intstatus & I_HMB_FRAME_IND)
774
775#define HOSTINTMASK (I_HMB_SW_MASK | I_CHIPACTIVE)
776
5b435de0 777/* Turn backplane clock on or off */
82d7f3c1 778static int brcmf_sdio_htclk(struct brcmf_sdio *bus, bool on, bool pendok)
5b435de0
AS
779{
780 int err;
781 u8 clkctl, clkreq, devctl;
782 unsigned long timeout;
783
c3203374 784 brcmf_dbg(SDIO, "Enter\n");
5b435de0
AS
785
786 clkctl = 0;
787
4a3da990
PH
788 if (bus->sr_enabled) {
789 bus->clkstate = (on ? CLK_AVAIL : CLK_SDONLY);
790 return 0;
791 }
792
5b435de0
AS
793 if (on) {
794 /* Request HT Avail */
795 clkreq =
796 bus->alp_only ? SBSDIO_ALP_AVAIL_REQ : SBSDIO_HT_AVAIL_REQ;
797
a39be27b
AS
798 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
799 clkreq, &err);
5b435de0 800 if (err) {
5e8149f5 801 brcmf_err("HT Avail request error: %d\n", err);
5b435de0
AS
802 return -EBADE;
803 }
804
5b435de0 805 /* Check current status */
a39be27b
AS
806 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
807 SBSDIO_FUNC1_CHIPCLKCSR, &err);
5b435de0 808 if (err) {
5e8149f5 809 brcmf_err("HT Avail read error: %d\n", err);
5b435de0
AS
810 return -EBADE;
811 }
812
813 /* Go to pending and await interrupt if appropriate */
814 if (!SBSDIO_CLKAV(clkctl, bus->alp_only) && pendok) {
815 /* Allow only clock-available interrupt */
a39be27b
AS
816 devctl = brcmf_sdiod_regrb(bus->sdiodev,
817 SBSDIO_DEVICE_CTL, &err);
5b435de0 818 if (err) {
5e8149f5 819 brcmf_err("Devctl error setting CA: %d\n",
5b435de0
AS
820 err);
821 return -EBADE;
822 }
823
824 devctl |= SBSDIO_DEVCTL_CA_INT_ONLY;
a39be27b
AS
825 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
826 devctl, &err);
c3203374 827 brcmf_dbg(SDIO, "CLKCTL: set PENDING\n");
5b435de0
AS
828 bus->clkstate = CLK_PENDING;
829
830 return 0;
831 } else if (bus->clkstate == CLK_PENDING) {
832 /* Cancel CA-only interrupt filter */
a39be27b
AS
833 devctl = brcmf_sdiod_regrb(bus->sdiodev,
834 SBSDIO_DEVICE_CTL, &err);
5b435de0 835 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
a39be27b
AS
836 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
837 devctl, &err);
5b435de0
AS
838 }
839
840 /* Otherwise, wait here (polling) for HT Avail */
841 timeout = jiffies +
842 msecs_to_jiffies(PMU_MAX_TRANSITION_DLY/1000);
843 while (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
a39be27b
AS
844 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
845 SBSDIO_FUNC1_CHIPCLKCSR,
846 &err);
5b435de0
AS
847 if (time_after(jiffies, timeout))
848 break;
849 else
850 usleep_range(5000, 10000);
851 }
852 if (err) {
5e8149f5 853 brcmf_err("HT Avail request error: %d\n", err);
5b435de0
AS
854 return -EBADE;
855 }
856 if (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
5e8149f5 857 brcmf_err("HT Avail timeout (%d): clkctl 0x%02x\n",
5b435de0
AS
858 PMU_MAX_TRANSITION_DLY, clkctl);
859 return -EBADE;
860 }
861
862 /* Mark clock available */
863 bus->clkstate = CLK_AVAIL;
c3203374 864 brcmf_dbg(SDIO, "CLKCTL: turned ON\n");
5b435de0 865
8ae74654 866#if defined(DEBUG)
23677ce3 867 if (!bus->alp_only) {
5b435de0 868 if (SBSDIO_ALPONLY(clkctl))
5e8149f5 869 brcmf_err("HT Clock should be on\n");
5b435de0 870 }
8ae74654 871#endif /* defined (DEBUG) */
5b435de0
AS
872
873 bus->activity = true;
874 } else {
875 clkreq = 0;
876
877 if (bus->clkstate == CLK_PENDING) {
878 /* Cancel CA-only interrupt filter */
a39be27b
AS
879 devctl = brcmf_sdiod_regrb(bus->sdiodev,
880 SBSDIO_DEVICE_CTL, &err);
5b435de0 881 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
a39be27b
AS
882 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
883 devctl, &err);
5b435de0
AS
884 }
885
886 bus->clkstate = CLK_SDONLY;
a39be27b
AS
887 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
888 clkreq, &err);
c3203374 889 brcmf_dbg(SDIO, "CLKCTL: turned OFF\n");
5b435de0 890 if (err) {
5e8149f5 891 brcmf_err("Failed access turning clock off: %d\n",
5b435de0
AS
892 err);
893 return -EBADE;
894 }
895 }
896 return 0;
897}
898
899/* Change idle/active SD state */
82d7f3c1 900static int brcmf_sdio_sdclk(struct brcmf_sdio *bus, bool on)
5b435de0 901{
c3203374 902 brcmf_dbg(SDIO, "Enter\n");
5b435de0
AS
903
904 if (on)
905 bus->clkstate = CLK_SDONLY;
906 else
907 bus->clkstate = CLK_NONE;
908
909 return 0;
910}
911
912/* Transition SD and backplane clock readiness */
82d7f3c1 913static int brcmf_sdio_clkctl(struct brcmf_sdio *bus, uint target, bool pendok)
5b435de0 914{
8ae74654 915#ifdef DEBUG
5b435de0 916 uint oldstate = bus->clkstate;
8ae74654 917#endif /* DEBUG */
5b435de0 918
c3203374 919 brcmf_dbg(SDIO, "Enter\n");
5b435de0
AS
920
921 /* Early exit if we're already there */
922 if (bus->clkstate == target) {
923 if (target == CLK_AVAIL) {
82d7f3c1 924 brcmf_sdio_wd_timer(bus, BRCMF_WD_POLL_MS);
5b435de0
AS
925 bus->activity = true;
926 }
927 return 0;
928 }
929
930 switch (target) {
931 case CLK_AVAIL:
932 /* Make sure SD clock is available */
933 if (bus->clkstate == CLK_NONE)
82d7f3c1 934 brcmf_sdio_sdclk(bus, true);
5b435de0 935 /* Now request HT Avail on the backplane */
82d7f3c1
AS
936 brcmf_sdio_htclk(bus, true, pendok);
937 brcmf_sdio_wd_timer(bus, BRCMF_WD_POLL_MS);
5b435de0
AS
938 bus->activity = true;
939 break;
940
941 case CLK_SDONLY:
942 /* Remove HT request, or bring up SD clock */
943 if (bus->clkstate == CLK_NONE)
82d7f3c1 944 brcmf_sdio_sdclk(bus, true);
5b435de0 945 else if (bus->clkstate == CLK_AVAIL)
82d7f3c1 946 brcmf_sdio_htclk(bus, false, false);
5b435de0 947 else
5e8149f5 948 brcmf_err("request for %d -> %d\n",
5b435de0 949 bus->clkstate, target);
82d7f3c1 950 brcmf_sdio_wd_timer(bus, BRCMF_WD_POLL_MS);
5b435de0
AS
951 break;
952
953 case CLK_NONE:
954 /* Make sure to remove HT request */
955 if (bus->clkstate == CLK_AVAIL)
82d7f3c1 956 brcmf_sdio_htclk(bus, false, false);
5b435de0 957 /* Now remove the SD clock */
82d7f3c1
AS
958 brcmf_sdio_sdclk(bus, false);
959 brcmf_sdio_wd_timer(bus, 0);
5b435de0
AS
960 break;
961 }
8ae74654 962#ifdef DEBUG
c3203374 963 brcmf_dbg(SDIO, "%d -> %d\n", oldstate, bus->clkstate);
8ae74654 964#endif /* DEBUG */
5b435de0
AS
965
966 return 0;
967}
968
4a3da990 969static int
82d7f3c1 970brcmf_sdio_bus_sleep(struct brcmf_sdio *bus, bool sleep, bool pendok)
4a3da990
PH
971{
972 int err = 0;
82030d6d
AS
973
974 brcmf_dbg(SDIO, "Enter: request %s currently %s\n",
4a3da990
PH
975 (sleep ? "SLEEP" : "WAKE"),
976 (bus->sleeping ? "SLEEP" : "WAKE"));
977
978 /* If SR is enabled control bus state with KSO */
979 if (bus->sr_enabled) {
980 /* Done if we're already in the requested state */
981 if (sleep == bus->sleeping)
982 goto end;
983
984 /* Going to sleep */
985 if (sleep) {
986 /* Don't sleep if something is pending */
987 if (atomic_read(&bus->intstatus) ||
988 atomic_read(&bus->ipend) > 0 ||
989 (!atomic_read(&bus->fcstate) &&
990 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) &&
991 data_ok(bus)))
992 return -EBUSY;
82d7f3c1 993 err = brcmf_sdio_kso_control(bus, false);
4a3da990
PH
994 /* disable watchdog */
995 if (!err)
82d7f3c1 996 brcmf_sdio_wd_timer(bus, 0);
4a3da990
PH
997 } else {
998 bus->idlecount = 0;
82d7f3c1 999 err = brcmf_sdio_kso_control(bus, true);
4a3da990
PH
1000 }
1001 if (!err) {
1002 /* Change state */
1003 bus->sleeping = sleep;
1004 brcmf_dbg(SDIO, "new state %s\n",
1005 (sleep ? "SLEEP" : "WAKE"));
1006 } else {
1007 brcmf_err("error while changing bus sleep state %d\n",
1008 err);
1009 return err;
1010 }
1011 }
1012
1013end:
1014 /* control clocks */
1015 if (sleep) {
1016 if (!bus->sr_enabled)
82d7f3c1 1017 brcmf_sdio_clkctl(bus, CLK_NONE, pendok);
4a3da990 1018 } else {
82d7f3c1 1019 brcmf_sdio_clkctl(bus, CLK_AVAIL, pendok);
4a3da990
PH
1020 }
1021
1022 return err;
1023
1024}
1025
0801e6c5
DK
1026#ifdef DEBUG
1027static inline bool brcmf_sdio_valid_shared_address(u32 addr)
1028{
1029 return !(addr == 0 || ((~addr >> 16) & 0xffff) == (addr & 0xffff));
1030}
1031
1032static int brcmf_sdio_readshared(struct brcmf_sdio *bus,
1033 struct sdpcm_shared *sh)
1034{
1035 u32 addr;
1036 int rv;
1037 u32 shaddr = 0;
1038 struct sdpcm_shared_le sh_le;
1039 __le32 addr_le;
1040
1041 shaddr = bus->ci->rambase + bus->ramsize - 4;
1042
1043 /*
1044 * Read last word in socram to determine
1045 * address of sdpcm_shared structure
1046 */
1047 sdio_claim_host(bus->sdiodev->func[1]);
1048 brcmf_sdio_bus_sleep(bus, false, false);
1049 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, shaddr, (u8 *)&addr_le, 4);
1050 sdio_release_host(bus->sdiodev->func[1]);
1051 if (rv < 0)
1052 return rv;
1053
1054 addr = le32_to_cpu(addr_le);
1055
1056 brcmf_dbg(SDIO, "sdpcm_shared address 0x%08X\n", addr);
1057
1058 /*
1059 * Check if addr is valid.
1060 * NVRAM length at the end of memory should have been overwritten.
1061 */
1062 if (!brcmf_sdio_valid_shared_address(addr)) {
1063 brcmf_err("invalid sdpcm_shared address 0x%08X\n",
1064 addr);
1065 return -EINVAL;
1066 }
1067
1068 /* Read hndrte_shared structure */
1069 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, (u8 *)&sh_le,
1070 sizeof(struct sdpcm_shared_le));
1071 if (rv < 0)
1072 return rv;
1073
1074 /* Endianness */
1075 sh->flags = le32_to_cpu(sh_le.flags);
1076 sh->trap_addr = le32_to_cpu(sh_le.trap_addr);
1077 sh->assert_exp_addr = le32_to_cpu(sh_le.assert_exp_addr);
1078 sh->assert_file_addr = le32_to_cpu(sh_le.assert_file_addr);
1079 sh->assert_line = le32_to_cpu(sh_le.assert_line);
1080 sh->console_addr = le32_to_cpu(sh_le.console_addr);
1081 sh->msgtrace_addr = le32_to_cpu(sh_le.msgtrace_addr);
1082
1083 if ((sh->flags & SDPCM_SHARED_VERSION_MASK) > SDPCM_SHARED_VERSION) {
1084 brcmf_err("sdpcm shared version unsupported: dhd %d dongle %d\n",
1085 SDPCM_SHARED_VERSION,
1086 sh->flags & SDPCM_SHARED_VERSION_MASK);
1087 return -EPROTO;
1088 }
1089
1090 return 0;
1091}
1092
1093static void brcmf_sdio_get_console_addr(struct brcmf_sdio *bus)
1094{
1095 struct sdpcm_shared sh;
1096
1097 if (brcmf_sdio_readshared(bus, &sh) == 0)
1098 bus->console_addr = sh.console_addr;
1099}
1100#else
1101static void brcmf_sdio_get_console_addr(struct brcmf_sdio *bus)
1102{
1103}
1104#endif /* DEBUG */
1105
82d7f3c1 1106static u32 brcmf_sdio_hostmail(struct brcmf_sdio *bus)
5b435de0
AS
1107{
1108 u32 intstatus = 0;
1109 u32 hmb_data;
1110 u8 fcbits;
58692750 1111 int ret;
5b435de0 1112
c3203374 1113 brcmf_dbg(SDIO, "Enter\n");
5b435de0
AS
1114
1115 /* Read mailbox data and ack that we did so */
58692750
FL
1116 ret = r_sdreg32(bus, &hmb_data,
1117 offsetof(struct sdpcmd_regs, tohostmailboxdata));
5b435de0 1118
58692750 1119 if (ret == 0)
5b435de0 1120 w_sdreg32(bus, SMB_INT_ACK,
58692750 1121 offsetof(struct sdpcmd_regs, tosbmailbox));
80969836 1122 bus->sdcnt.f1regdata += 2;
5b435de0
AS
1123
1124 /* Dongle recomposed rx frames, accept them again */
1125 if (hmb_data & HMB_DATA_NAKHANDLED) {
c3203374 1126 brcmf_dbg(SDIO, "Dongle reports NAK handled, expect rtx of %d\n",
5b435de0
AS
1127 bus->rx_seq);
1128 if (!bus->rxskip)
5e8149f5 1129 brcmf_err("unexpected NAKHANDLED!\n");
5b435de0
AS
1130
1131 bus->rxskip = false;
1132 intstatus |= I_HMB_FRAME_IND;
1133 }
1134
1135 /*
1136 * DEVREADY does not occur with gSPI.
1137 */
1138 if (hmb_data & (HMB_DATA_DEVREADY | HMB_DATA_FWREADY)) {
1139 bus->sdpcm_ver =
1140 (hmb_data & HMB_DATA_VERSION_MASK) >>
1141 HMB_DATA_VERSION_SHIFT;
1142 if (bus->sdpcm_ver != SDPCM_PROT_VERSION)
5e8149f5 1143 brcmf_err("Version mismatch, dongle reports %d, "
5b435de0
AS
1144 "expecting %d\n",
1145 bus->sdpcm_ver, SDPCM_PROT_VERSION);
1146 else
c3203374 1147 brcmf_dbg(SDIO, "Dongle ready, protocol version %d\n",
5b435de0 1148 bus->sdpcm_ver);
0801e6c5
DK
1149
1150 /*
1151 * Retrieve console state address now that firmware should have
1152 * updated it.
1153 */
1154 brcmf_sdio_get_console_addr(bus);
5b435de0
AS
1155 }
1156
1157 /*
1158 * Flow Control has been moved into the RX headers and this out of band
1159 * method isn't used any more.
1160 * remaining backward compatible with older dongles.
1161 */
1162 if (hmb_data & HMB_DATA_FC) {
1163 fcbits = (hmb_data & HMB_DATA_FCDATA_MASK) >>
1164 HMB_DATA_FCDATA_SHIFT;
1165
1166 if (fcbits & ~bus->flowcontrol)
80969836 1167 bus->sdcnt.fc_xoff++;
5b435de0
AS
1168
1169 if (bus->flowcontrol & ~fcbits)
80969836 1170 bus->sdcnt.fc_xon++;
5b435de0 1171
80969836 1172 bus->sdcnt.fc_rcvd++;
5b435de0
AS
1173 bus->flowcontrol = fcbits;
1174 }
1175
1176 /* Shouldn't be any others */
1177 if (hmb_data & ~(HMB_DATA_DEVREADY |
1178 HMB_DATA_NAKHANDLED |
1179 HMB_DATA_FC |
1180 HMB_DATA_FWREADY |
1181 HMB_DATA_FCDATA_MASK | HMB_DATA_VERSION_MASK))
5e8149f5 1182 brcmf_err("Unknown mailbox data content: 0x%02x\n",
5b435de0
AS
1183 hmb_data);
1184
1185 return intstatus;
1186}
1187
82d7f3c1 1188static void brcmf_sdio_rxfail(struct brcmf_sdio *bus, bool abort, bool rtx)
5b435de0
AS
1189{
1190 uint retries = 0;
1191 u16 lastrbc;
1192 u8 hi, lo;
1193 int err;
1194
5e8149f5 1195 brcmf_err("%sterminate frame%s\n",
5b435de0
AS
1196 abort ? "abort command, " : "",
1197 rtx ? ", send NAK" : "");
1198
1199 if (abort)
a39be27b 1200 brcmf_sdiod_abort(bus->sdiodev, SDIO_FUNC_2);
5b435de0 1201
a39be27b
AS
1202 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL,
1203 SFC_RF_TERM, &err);
80969836 1204 bus->sdcnt.f1regdata++;
5b435de0
AS
1205
1206 /* Wait until the packet has been flushed (device/FIFO stable) */
1207 for (lastrbc = retries = 0xffff; retries > 0; retries--) {
a39be27b
AS
1208 hi = brcmf_sdiod_regrb(bus->sdiodev,
1209 SBSDIO_FUNC1_RFRAMEBCHI, &err);
1210 lo = brcmf_sdiod_regrb(bus->sdiodev,
1211 SBSDIO_FUNC1_RFRAMEBCLO, &err);
80969836 1212 bus->sdcnt.f1regdata += 2;
5b435de0
AS
1213
1214 if ((hi == 0) && (lo == 0))
1215 break;
1216
1217 if ((hi > (lastrbc >> 8)) && (lo > (lastrbc & 0x00ff))) {
5e8149f5 1218 brcmf_err("count growing: last 0x%04x now 0x%04x\n",
5b435de0
AS
1219 lastrbc, (hi << 8) + lo);
1220 }
1221 lastrbc = (hi << 8) + lo;
1222 }
1223
1224 if (!retries)
5e8149f5 1225 brcmf_err("count never zeroed: last 0x%04x\n", lastrbc);
5b435de0 1226 else
c3203374 1227 brcmf_dbg(SDIO, "flush took %d iterations\n", 0xffff - retries);
5b435de0
AS
1228
1229 if (rtx) {
80969836 1230 bus->sdcnt.rxrtx++;
58692750
FL
1231 err = w_sdreg32(bus, SMB_NAK,
1232 offsetof(struct sdpcmd_regs, tosbmailbox));
5b435de0 1233
80969836 1234 bus->sdcnt.f1regdata++;
58692750 1235 if (err == 0)
5b435de0
AS
1236 bus->rxskip = true;
1237 }
1238
1239 /* Clear partial in any case */
4754fcee 1240 bus->cur_read.len = 0;
5b435de0
AS
1241}
1242
9a95e60e 1243/* return total length of buffer chain */
82d7f3c1 1244static uint brcmf_sdio_glom_len(struct brcmf_sdio *bus)
9a95e60e
AS
1245{
1246 struct sk_buff *p;
1247 uint total;
1248
1249 total = 0;
1250 skb_queue_walk(&bus->glom, p)
1251 total += p->len;
1252 return total;
1253}
1254
82d7f3c1 1255static void brcmf_sdio_free_glom(struct brcmf_sdio *bus)
046808da
AS
1256{
1257 struct sk_buff *cur, *next;
1258
1259 skb_queue_walk_safe(&bus->glom, cur, next) {
1260 skb_unlink(cur, &bus->glom);
1261 brcmu_pkt_buf_free_skb(cur);
1262 }
1263}
1264
6bc52319
FL
1265/**
1266 * brcmfmac sdio bus specific header
1267 * This is the lowest layer header wrapped on the packets transmitted between
1268 * host and WiFi dongle which contains information needed for SDIO core and
1269 * firmware
1270 *
8da9d2c8
FL
1271 * It consists of 3 parts: hardware header, hardware extension header and
1272 * software header
6bc52319
FL
1273 * hardware header (frame tag) - 4 bytes
1274 * Byte 0~1: Frame length
1275 * Byte 2~3: Checksum, bit-wise inverse of frame length
8da9d2c8
FL
1276 * hardware extension header - 8 bytes
1277 * Tx glom mode only, N/A for Rx or normal Tx
1278 * Byte 0~1: Packet length excluding hw frame tag
1279 * Byte 2: Reserved
1280 * Byte 3: Frame flags, bit 0: last frame indication
1281 * Byte 4~5: Reserved
1282 * Byte 6~7: Tail padding length
6bc52319
FL
1283 * software header - 8 bytes
1284 * Byte 0: Rx/Tx sequence number
1285 * Byte 1: 4 MSB Channel number, 4 LSB arbitrary flag
1286 * Byte 2: Length of next data frame, reserved for Tx
1287 * Byte 3: Data offset
1288 * Byte 4: Flow control bits, reserved for Tx
1289 * Byte 5: Maximum Sequence number allowed by firmware for Tx, N/A for Tx packet
1290 * Byte 6~7: Reserved
1291 */
1292#define SDPCM_HWHDR_LEN 4
8da9d2c8 1293#define SDPCM_HWEXT_LEN 8
6bc52319
FL
1294#define SDPCM_SWHDR_LEN 8
1295#define SDPCM_HDRLEN (SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN)
6bc52319
FL
1296/* software header */
1297#define SDPCM_SEQ_MASK 0x000000ff
1298#define SDPCM_SEQ_WRAP 256
1299#define SDPCM_CHANNEL_MASK 0x00000f00
1300#define SDPCM_CHANNEL_SHIFT 8
1301#define SDPCM_CONTROL_CHANNEL 0 /* Control */
1302#define SDPCM_EVENT_CHANNEL 1 /* Asyc Event Indication */
1303#define SDPCM_DATA_CHANNEL 2 /* Data Xmit/Recv */
1304#define SDPCM_GLOM_CHANNEL 3 /* Coalesced packets */
1305#define SDPCM_TEST_CHANNEL 15 /* Test/debug packets */
1306#define SDPCM_GLOMDESC(p) (((u8 *)p)[1] & 0x80)
1307#define SDPCM_NEXTLEN_MASK 0x00ff0000
1308#define SDPCM_NEXTLEN_SHIFT 16
1309#define SDPCM_DOFFSET_MASK 0xff000000
1310#define SDPCM_DOFFSET_SHIFT 24
1311#define SDPCM_FCMASK_MASK 0x000000ff
1312#define SDPCM_WINDOW_MASK 0x0000ff00
1313#define SDPCM_WINDOW_SHIFT 8
1314
1315static inline u8 brcmf_sdio_getdatoffset(u8 *swheader)
1316{
1317 u32 hdrvalue;
1318 hdrvalue = *(u32 *)swheader;
1319 return (u8)((hdrvalue & SDPCM_DOFFSET_MASK) >> SDPCM_DOFFSET_SHIFT);
1320}
1321
1322static int brcmf_sdio_hdparse(struct brcmf_sdio *bus, u8 *header,
1323 struct brcmf_sdio_hdrinfo *rd,
1324 enum brcmf_sdio_frmtype type)
4754fcee
FL
1325{
1326 u16 len, checksum;
1327 u8 rx_seq, fc, tx_seq_max;
6bc52319 1328 u32 swheader;
4754fcee 1329
4b776961 1330 trace_brcmf_sdpcm_hdr(SDPCM_RX, header);
76584ece 1331
6bc52319 1332 /* hw header */
4754fcee
FL
1333 len = get_unaligned_le16(header);
1334 checksum = get_unaligned_le16(header + sizeof(u16));
1335 /* All zero means no more to read */
1336 if (!(len | checksum)) {
1337 bus->rxpending = false;
10510589 1338 return -ENODATA;
4754fcee
FL
1339 }
1340 if ((u16)(~(len ^ checksum))) {
5e8149f5 1341 brcmf_err("HW header checksum error\n");
4754fcee 1342 bus->sdcnt.rx_badhdr++;
82d7f3c1 1343 brcmf_sdio_rxfail(bus, false, false);
10510589 1344 return -EIO;
4754fcee
FL
1345 }
1346 if (len < SDPCM_HDRLEN) {
5e8149f5 1347 brcmf_err("HW header length error\n");
10510589 1348 return -EPROTO;
4754fcee 1349 }
9d7d6f95
FL
1350 if (type == BRCMF_SDIO_FT_SUPER &&
1351 (roundup(len, bus->blocksize) != rd->len)) {
5e8149f5 1352 brcmf_err("HW superframe header length error\n");
10510589 1353 return -EPROTO;
9d7d6f95
FL
1354 }
1355 if (type == BRCMF_SDIO_FT_SUB && len > rd->len) {
5e8149f5 1356 brcmf_err("HW subframe header length error\n");
10510589 1357 return -EPROTO;
9d7d6f95 1358 }
4754fcee
FL
1359 rd->len = len;
1360
6bc52319
FL
1361 /* software header */
1362 header += SDPCM_HWHDR_LEN;
1363 swheader = le32_to_cpu(*(__le32 *)header);
1364 if (type == BRCMF_SDIO_FT_SUPER && SDPCM_GLOMDESC(header)) {
5e8149f5 1365 brcmf_err("Glom descriptor found in superframe head\n");
9d7d6f95 1366 rd->len = 0;
10510589 1367 return -EINVAL;
9d7d6f95 1368 }
6bc52319
FL
1369 rx_seq = (u8)(swheader & SDPCM_SEQ_MASK);
1370 rd->channel = (swheader & SDPCM_CHANNEL_MASK) >> SDPCM_CHANNEL_SHIFT;
9d7d6f95
FL
1371 if (len > MAX_RX_DATASZ && rd->channel != SDPCM_CONTROL_CHANNEL &&
1372 type != BRCMF_SDIO_FT_SUPER) {
5e8149f5 1373 brcmf_err("HW header length too long\n");
4754fcee 1374 bus->sdcnt.rx_toolong++;
82d7f3c1 1375 brcmf_sdio_rxfail(bus, false, false);
4754fcee 1376 rd->len = 0;
10510589 1377 return -EPROTO;
4754fcee 1378 }
9d7d6f95 1379 if (type == BRCMF_SDIO_FT_SUPER && rd->channel != SDPCM_GLOM_CHANNEL) {
5e8149f5 1380 brcmf_err("Wrong channel for superframe\n");
9d7d6f95 1381 rd->len = 0;
10510589 1382 return -EINVAL;
9d7d6f95
FL
1383 }
1384 if (type == BRCMF_SDIO_FT_SUB && rd->channel != SDPCM_DATA_CHANNEL &&
1385 rd->channel != SDPCM_EVENT_CHANNEL) {
5e8149f5 1386 brcmf_err("Wrong channel for subframe\n");
9d7d6f95 1387 rd->len = 0;
10510589 1388 return -EINVAL;
9d7d6f95 1389 }
6bc52319 1390 rd->dat_offset = brcmf_sdio_getdatoffset(header);
4754fcee 1391 if (rd->dat_offset < SDPCM_HDRLEN || rd->dat_offset > rd->len) {
5e8149f5 1392 brcmf_err("seq %d: bad data offset\n", rx_seq);
4754fcee 1393 bus->sdcnt.rx_badhdr++;
82d7f3c1 1394 brcmf_sdio_rxfail(bus, false, false);
4754fcee 1395 rd->len = 0;
10510589 1396 return -ENXIO;
4754fcee
FL
1397 }
1398 if (rd->seq_num != rx_seq) {
5e8149f5 1399 brcmf_err("seq %d: sequence number error, expect %d\n",
4754fcee
FL
1400 rx_seq, rd->seq_num);
1401 bus->sdcnt.rx_badseq++;
1402 rd->seq_num = rx_seq;
1403 }
9d7d6f95
FL
1404 /* no need to check the reset for subframe */
1405 if (type == BRCMF_SDIO_FT_SUB)
10510589 1406 return 0;
6bc52319 1407 rd->len_nxtfrm = (swheader & SDPCM_NEXTLEN_MASK) >> SDPCM_NEXTLEN_SHIFT;
4754fcee
FL
1408 if (rd->len_nxtfrm << 4 > MAX_RX_DATASZ) {
1409 /* only warm for NON glom packet */
1410 if (rd->channel != SDPCM_GLOM_CHANNEL)
5e8149f5 1411 brcmf_err("seq %d: next length error\n", rx_seq);
4754fcee
FL
1412 rd->len_nxtfrm = 0;
1413 }
6bc52319
FL
1414 swheader = le32_to_cpu(*(__le32 *)(header + 4));
1415 fc = swheader & SDPCM_FCMASK_MASK;
4754fcee
FL
1416 if (bus->flowcontrol != fc) {
1417 if (~bus->flowcontrol & fc)
1418 bus->sdcnt.fc_xoff++;
1419 if (bus->flowcontrol & ~fc)
1420 bus->sdcnt.fc_xon++;
1421 bus->sdcnt.fc_rcvd++;
1422 bus->flowcontrol = fc;
1423 }
6bc52319 1424 tx_seq_max = (swheader & SDPCM_WINDOW_MASK) >> SDPCM_WINDOW_SHIFT;
4754fcee 1425 if ((u8)(tx_seq_max - bus->tx_seq) > 0x40) {
5e8149f5 1426 brcmf_err("seq %d: max tx seq number error\n", rx_seq);
4754fcee
FL
1427 tx_seq_max = bus->tx_seq + 2;
1428 }
1429 bus->tx_max = tx_seq_max;
1430
10510589 1431 return 0;
4754fcee
FL
1432}
1433
6bc52319
FL
1434static inline void brcmf_sdio_update_hwhdr(u8 *header, u16 frm_length)
1435{
1436 *(__le16 *)header = cpu_to_le16(frm_length);
1437 *(((__le16 *)header) + 1) = cpu_to_le16(~frm_length);
1438}
1439
1440static void brcmf_sdio_hdpack(struct brcmf_sdio *bus, u8 *header,
1441 struct brcmf_sdio_hdrinfo *hd_info)
1442{
8da9d2c8
FL
1443 u32 hdrval;
1444 u8 hdr_offset;
6bc52319
FL
1445
1446 brcmf_sdio_update_hwhdr(header, hd_info->len);
8da9d2c8
FL
1447 hdr_offset = SDPCM_HWHDR_LEN;
1448
1449 if (bus->txglom) {
1450 hdrval = (hd_info->len - hdr_offset) | (hd_info->lastfrm << 24);
1451 *((__le32 *)(header + hdr_offset)) = cpu_to_le32(hdrval);
1452 hdrval = (u16)hd_info->tail_pad << 16;
1453 *(((__le32 *)(header + hdr_offset)) + 1) = cpu_to_le32(hdrval);
1454 hdr_offset += SDPCM_HWEXT_LEN;
1455 }
6bc52319 1456
8da9d2c8
FL
1457 hdrval = hd_info->seq_num;
1458 hdrval |= (hd_info->channel << SDPCM_CHANNEL_SHIFT) &
1459 SDPCM_CHANNEL_MASK;
1460 hdrval |= (hd_info->dat_offset << SDPCM_DOFFSET_SHIFT) &
1461 SDPCM_DOFFSET_MASK;
1462 *((__le32 *)(header + hdr_offset)) = cpu_to_le32(hdrval);
1463 *(((__le32 *)(header + hdr_offset)) + 1) = 0;
1464 trace_brcmf_sdpcm_hdr(SDPCM_TX + !!(bus->txglom), header);
6bc52319
FL
1465}
1466
82d7f3c1 1467static u8 brcmf_sdio_rxglom(struct brcmf_sdio *bus, u8 rxseq)
5b435de0
AS
1468{
1469 u16 dlen, totlen;
1470 u8 *dptr, num = 0;
9d7d6f95 1471 u16 sublen;
0b45bf74 1472 struct sk_buff *pfirst, *pnext;
5b435de0
AS
1473
1474 int errcode;
9d7d6f95 1475 u8 doff, sfdoff;
5b435de0 1476
6bc52319 1477 struct brcmf_sdio_hdrinfo rd_new;
5b435de0
AS
1478
1479 /* If packets, issue read(s) and send up packet chain */
1480 /* Return sequence numbers consumed? */
1481
c3203374 1482 brcmf_dbg(SDIO, "start: glomd %p glom %p\n",
b83db862 1483 bus->glomd, skb_peek(&bus->glom));
5b435de0
AS
1484
1485 /* If there's a descriptor, generate the packet chain */
1486 if (bus->glomd) {
0b45bf74 1487 pfirst = pnext = NULL;
5b435de0
AS
1488 dlen = (u16) (bus->glomd->len);
1489 dptr = bus->glomd->data;
1490 if (!dlen || (dlen & 1)) {
5e8149f5 1491 brcmf_err("bad glomd len(%d), ignore descriptor\n",
5b435de0
AS
1492 dlen);
1493 dlen = 0;
1494 }
1495
1496 for (totlen = num = 0; dlen; num++) {
1497 /* Get (and move past) next length */
1498 sublen = get_unaligned_le16(dptr);
1499 dlen -= sizeof(u16);
1500 dptr += sizeof(u16);
1501 if ((sublen < SDPCM_HDRLEN) ||
1502 ((num == 0) && (sublen < (2 * SDPCM_HDRLEN)))) {
5e8149f5 1503 brcmf_err("descriptor len %d bad: %d\n",
5b435de0
AS
1504 num, sublen);
1505 pnext = NULL;
1506 break;
1507 }
e217d1c8 1508 if (sublen % bus->sgentry_align) {
5e8149f5 1509 brcmf_err("sublen %d not multiple of %d\n",
e217d1c8 1510 sublen, bus->sgentry_align);
5b435de0
AS
1511 }
1512 totlen += sublen;
1513
1514 /* For last frame, adjust read len so total
1515 is a block multiple */
1516 if (!dlen) {
1517 sublen +=
1518 (roundup(totlen, bus->blocksize) - totlen);
1519 totlen = roundup(totlen, bus->blocksize);
1520 }
1521
1522 /* Allocate/chain packet for next subframe */
e217d1c8 1523 pnext = brcmu_pkt_buf_get_skb(sublen + bus->sgentry_align);
5b435de0 1524 if (pnext == NULL) {
5e8149f5 1525 brcmf_err("bcm_pkt_buf_get_skb failed, num %d len %d\n",
5b435de0
AS
1526 num, sublen);
1527 break;
1528 }
b83db862 1529 skb_queue_tail(&bus->glom, pnext);
5b435de0
AS
1530
1531 /* Adhere to start alignment requirements */
e217d1c8 1532 pkt_align(pnext, sublen, bus->sgentry_align);
5b435de0
AS
1533 }
1534
1535 /* If all allocations succeeded, save packet chain
1536 in bus structure */
1537 if (pnext) {
1538 brcmf_dbg(GLOM, "allocated %d-byte packet chain for %d subframes\n",
1539 totlen, num);
4754fcee
FL
1540 if (BRCMF_GLOM_ON() && bus->cur_read.len &&
1541 totlen != bus->cur_read.len) {
5b435de0 1542 brcmf_dbg(GLOM, "glomdesc mismatch: nextlen %d glomdesc %d rxseq %d\n",
4754fcee 1543 bus->cur_read.len, totlen, rxseq);
5b435de0 1544 }
5b435de0
AS
1545 pfirst = pnext = NULL;
1546 } else {
82d7f3c1 1547 brcmf_sdio_free_glom(bus);
5b435de0
AS
1548 num = 0;
1549 }
1550
1551 /* Done with descriptor packet */
1552 brcmu_pkt_buf_free_skb(bus->glomd);
1553 bus->glomd = NULL;
4754fcee 1554 bus->cur_read.len = 0;
5b435de0
AS
1555 }
1556
1557 /* Ok -- either we just generated a packet chain,
1558 or had one from before */
b83db862 1559 if (!skb_queue_empty(&bus->glom)) {
5b435de0
AS
1560 if (BRCMF_GLOM_ON()) {
1561 brcmf_dbg(GLOM, "try superframe read, packet chain:\n");
b83db862 1562 skb_queue_walk(&bus->glom, pnext) {
5b435de0
AS
1563 brcmf_dbg(GLOM, " %p: %p len 0x%04x (%d)\n",
1564 pnext, (u8 *) (pnext->data),
1565 pnext->len, pnext->len);
1566 }
1567 }
1568
b83db862 1569 pfirst = skb_peek(&bus->glom);
82d7f3c1 1570 dlen = (u16) brcmf_sdio_glom_len(bus);
5b435de0
AS
1571
1572 /* Do an SDIO read for the superframe. Configurable iovar to
1573 * read directly into the chained packet, or allocate a large
1574 * packet and and copy into the chain.
1575 */
38b0b0dd 1576 sdio_claim_host(bus->sdiodev->func[1]);
a39be27b 1577 errcode = brcmf_sdiod_recv_chain(bus->sdiodev,
a39be27b 1578 &bus->glom, dlen);
38b0b0dd 1579 sdio_release_host(bus->sdiodev->func[1]);
80969836 1580 bus->sdcnt.f2rxdata++;
5b435de0
AS
1581
1582 /* On failure, kill the superframe, allow a couple retries */
1583 if (errcode < 0) {
5e8149f5 1584 brcmf_err("glom read of %d bytes failed: %d\n",
5b435de0 1585 dlen, errcode);
5b435de0 1586
38b0b0dd 1587 sdio_claim_host(bus->sdiodev->func[1]);
5b435de0 1588 if (bus->glomerr++ < 3) {
82d7f3c1 1589 brcmf_sdio_rxfail(bus, true, true);
5b435de0
AS
1590 } else {
1591 bus->glomerr = 0;
82d7f3c1 1592 brcmf_sdio_rxfail(bus, true, false);
80969836 1593 bus->sdcnt.rxglomfail++;
82d7f3c1 1594 brcmf_sdio_free_glom(bus);
5b435de0 1595 }
38b0b0dd 1596 sdio_release_host(bus->sdiodev->func[1]);
5b435de0
AS
1597 return 0;
1598 }
1e023829
JP
1599
1600 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
1601 pfirst->data, min_t(int, pfirst->len, 48),
1602 "SUPERFRAME:\n");
5b435de0 1603
9d7d6f95
FL
1604 rd_new.seq_num = rxseq;
1605 rd_new.len = dlen;
38b0b0dd 1606 sdio_claim_host(bus->sdiodev->func[1]);
6bc52319
FL
1607 errcode = brcmf_sdio_hdparse(bus, pfirst->data, &rd_new,
1608 BRCMF_SDIO_FT_SUPER);
38b0b0dd 1609 sdio_release_host(bus->sdiodev->func[1]);
9d7d6f95 1610 bus->cur_read.len = rd_new.len_nxtfrm << 4;
5b435de0
AS
1611
1612 /* Remove superframe header, remember offset */
9d7d6f95
FL
1613 skb_pull(pfirst, rd_new.dat_offset);
1614 sfdoff = rd_new.dat_offset;
0b45bf74 1615 num = 0;
5b435de0
AS
1616
1617 /* Validate all the subframe headers */
0b45bf74
AS
1618 skb_queue_walk(&bus->glom, pnext) {
1619 /* leave when invalid subframe is found */
1620 if (errcode)
1621 break;
1622
9d7d6f95
FL
1623 rd_new.len = pnext->len;
1624 rd_new.seq_num = rxseq++;
38b0b0dd 1625 sdio_claim_host(bus->sdiodev->func[1]);
6bc52319
FL
1626 errcode = brcmf_sdio_hdparse(bus, pnext->data, &rd_new,
1627 BRCMF_SDIO_FT_SUB);
38b0b0dd 1628 sdio_release_host(bus->sdiodev->func[1]);
1e023829 1629 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
9d7d6f95 1630 pnext->data, 32, "subframe:\n");
5b435de0 1631
0b45bf74 1632 num++;
5b435de0
AS
1633 }
1634
1635 if (errcode) {
1636 /* Terminate frame on error, request
1637 a couple retries */
38b0b0dd 1638 sdio_claim_host(bus->sdiodev->func[1]);
5b435de0
AS
1639 if (bus->glomerr++ < 3) {
1640 /* Restore superframe header space */
1641 skb_push(pfirst, sfdoff);
82d7f3c1 1642 brcmf_sdio_rxfail(bus, true, true);
5b435de0
AS
1643 } else {
1644 bus->glomerr = 0;
82d7f3c1 1645 brcmf_sdio_rxfail(bus, true, false);
80969836 1646 bus->sdcnt.rxglomfail++;
82d7f3c1 1647 brcmf_sdio_free_glom(bus);
5b435de0 1648 }
38b0b0dd 1649 sdio_release_host(bus->sdiodev->func[1]);
4754fcee 1650 bus->cur_read.len = 0;
5b435de0
AS
1651 return 0;
1652 }
1653
1654 /* Basic SD framing looks ok - process each packet (header) */
5b435de0 1655
0b45bf74 1656 skb_queue_walk_safe(&bus->glom, pfirst, pnext) {
5b435de0
AS
1657 dptr = (u8 *) (pfirst->data);
1658 sublen = get_unaligned_le16(dptr);
6bc52319 1659 doff = brcmf_sdio_getdatoffset(&dptr[SDPCM_HWHDR_LEN]);
5b435de0 1660
1e023829 1661 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_DATA_ON(),
9d7d6f95
FL
1662 dptr, pfirst->len,
1663 "Rx Subframe Data:\n");
5b435de0
AS
1664
1665 __skb_trim(pfirst, sublen);
1666 skb_pull(pfirst, doff);
1667
1668 if (pfirst->len == 0) {
0b45bf74 1669 skb_unlink(pfirst, &bus->glom);
5b435de0 1670 brcmu_pkt_buf_free_skb(pfirst);
5b435de0 1671 continue;
5b435de0
AS
1672 }
1673
1e023829
JP
1674 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
1675 pfirst->data,
1676 min_t(int, pfirst->len, 32),
1677 "subframe %d to stack, %p (%p/%d) nxt/lnk %p/%p\n",
1678 bus->glom.qlen, pfirst, pfirst->data,
1679 pfirst->len, pfirst->next,
1680 pfirst->prev);
05f3820b
AS
1681 skb_unlink(pfirst, &bus->glom);
1682 brcmf_rx_frame(bus->sdiodev->dev, pfirst);
1683 bus->sdcnt.rxglompkts++;
5b435de0 1684 }
5b435de0 1685
80969836 1686 bus->sdcnt.rxglomframes++;
5b435de0
AS
1687 }
1688 return num;
1689}
1690
82d7f3c1
AS
1691static int brcmf_sdio_dcmd_resp_wait(struct brcmf_sdio *bus, uint *condition,
1692 bool *pending)
5b435de0
AS
1693{
1694 DECLARE_WAITQUEUE(wait, current);
1695 int timeout = msecs_to_jiffies(DCMD_RESP_TIMEOUT);
1696
1697 /* Wait until control frame is available */
1698 add_wait_queue(&bus->dcmd_resp_wait, &wait);
1699 set_current_state(TASK_INTERRUPTIBLE);
1700
1701 while (!(*condition) && (!signal_pending(current) && timeout))
1702 timeout = schedule_timeout(timeout);
1703
1704 if (signal_pending(current))
1705 *pending = true;
1706
1707 set_current_state(TASK_RUNNING);
1708 remove_wait_queue(&bus->dcmd_resp_wait, &wait);
1709
1710 return timeout;
1711}
1712
82d7f3c1 1713static int brcmf_sdio_dcmd_resp_wake(struct brcmf_sdio *bus)
5b435de0
AS
1714{
1715 if (waitqueue_active(&bus->dcmd_resp_wait))
1716 wake_up_interruptible(&bus->dcmd_resp_wait);
1717
1718 return 0;
1719}
1720static void
82d7f3c1 1721brcmf_sdio_read_control(struct brcmf_sdio *bus, u8 *hdr, uint len, uint doff)
5b435de0
AS
1722{
1723 uint rdlen, pad;
dd43a01c 1724 u8 *buf = NULL, *rbuf;
5b435de0
AS
1725 int sdret;
1726
1727 brcmf_dbg(TRACE, "Enter\n");
1728
dd43a01c
FL
1729 if (bus->rxblen)
1730 buf = vzalloc(bus->rxblen);
14f8dc49 1731 if (!buf)
dd43a01c 1732 goto done;
14f8dc49 1733
dd43a01c 1734 rbuf = bus->rxbuf;
9b2d2f2a 1735 pad = ((unsigned long)rbuf % bus->head_align);
5b435de0 1736 if (pad)
9b2d2f2a 1737 rbuf += (bus->head_align - pad);
5b435de0
AS
1738
1739 /* Copy the already-read portion over */
dd43a01c 1740 memcpy(buf, hdr, BRCMF_FIRSTREAD);
5b435de0
AS
1741 if (len <= BRCMF_FIRSTREAD)
1742 goto gotpkt;
1743
1744 /* Raise rdlen to next SDIO block to avoid tail command */
1745 rdlen = len - BRCMF_FIRSTREAD;
1746 if (bus->roundup && bus->blocksize && (rdlen > bus->blocksize)) {
1747 pad = bus->blocksize - (rdlen % bus->blocksize);
1748 if ((pad <= bus->roundup) && (pad < bus->blocksize) &&
b01a6b3c 1749 ((len + pad) < bus->sdiodev->bus_if->maxctl))
5b435de0 1750 rdlen += pad;
9b2d2f2a
AS
1751 } else if (rdlen % bus->head_align) {
1752 rdlen += bus->head_align - (rdlen % bus->head_align);
5b435de0
AS
1753 }
1754
5b435de0 1755 /* Drop if the read is too big or it exceeds our maximum */
b01a6b3c 1756 if ((rdlen + BRCMF_FIRSTREAD) > bus->sdiodev->bus_if->maxctl) {
5e8149f5 1757 brcmf_err("%d-byte control read exceeds %d-byte buffer\n",
b01a6b3c 1758 rdlen, bus->sdiodev->bus_if->maxctl);
82d7f3c1 1759 brcmf_sdio_rxfail(bus, false, false);
5b435de0
AS
1760 goto done;
1761 }
1762
b01a6b3c 1763 if ((len - doff) > bus->sdiodev->bus_if->maxctl) {
5e8149f5 1764 brcmf_err("%d-byte ctl frame (%d-byte ctl data) exceeds %d-byte limit\n",
b01a6b3c 1765 len, len - doff, bus->sdiodev->bus_if->maxctl);
80969836 1766 bus->sdcnt.rx_toolong++;
82d7f3c1 1767 brcmf_sdio_rxfail(bus, false, false);
5b435de0
AS
1768 goto done;
1769 }
1770
dd43a01c 1771 /* Read remain of frame body */
a7cdd821 1772 sdret = brcmf_sdiod_recv_buf(bus->sdiodev, rbuf, rdlen);
80969836 1773 bus->sdcnt.f2rxdata++;
5b435de0
AS
1774
1775 /* Control frame failures need retransmission */
1776 if (sdret < 0) {
5e8149f5 1777 brcmf_err("read %d control bytes failed: %d\n",
5b435de0 1778 rdlen, sdret);
80969836 1779 bus->sdcnt.rxc_errors++;
82d7f3c1 1780 brcmf_sdio_rxfail(bus, true, true);
5b435de0 1781 goto done;
dd43a01c
FL
1782 } else
1783 memcpy(buf + BRCMF_FIRSTREAD, rbuf, rdlen);
5b435de0
AS
1784
1785gotpkt:
1786
1e023829 1787 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_CTL_ON(),
dd43a01c 1788 buf, len, "RxCtrl:\n");
5b435de0
AS
1789
1790 /* Point to valid data and indicate its length */
dd43a01c
FL
1791 spin_lock_bh(&bus->rxctl_lock);
1792 if (bus->rxctl) {
5e8149f5 1793 brcmf_err("last control frame is being processed.\n");
dd43a01c
FL
1794 spin_unlock_bh(&bus->rxctl_lock);
1795 vfree(buf);
1796 goto done;
1797 }
1798 bus->rxctl = buf + doff;
1799 bus->rxctl_orig = buf;
5b435de0 1800 bus->rxlen = len - doff;
dd43a01c 1801 spin_unlock_bh(&bus->rxctl_lock);
5b435de0
AS
1802
1803done:
1804 /* Awake any waiters */
82d7f3c1 1805 brcmf_sdio_dcmd_resp_wake(bus);
5b435de0
AS
1806}
1807
1808/* Pad read to blocksize for efficiency */
82d7f3c1 1809static void brcmf_sdio_pad(struct brcmf_sdio *bus, u16 *pad, u16 *rdlen)
5b435de0
AS
1810{
1811 if (bus->roundup && bus->blocksize && *rdlen > bus->blocksize) {
1812 *pad = bus->blocksize - (*rdlen % bus->blocksize);
1813 if (*pad <= bus->roundup && *pad < bus->blocksize &&
1814 *rdlen + *pad + BRCMF_FIRSTREAD < MAX_RX_DATASZ)
1815 *rdlen += *pad;
9b2d2f2a
AS
1816 } else if (*rdlen % bus->head_align) {
1817 *rdlen += bus->head_align - (*rdlen % bus->head_align);
5b435de0
AS
1818 }
1819}
1820
4754fcee 1821static uint brcmf_sdio_readframes(struct brcmf_sdio *bus, uint maxframes)
5b435de0 1822{
5b435de0
AS
1823 struct sk_buff *pkt; /* Packet for event or data frames */
1824 u16 pad; /* Number of pad bytes to read */
5b435de0 1825 uint rxleft = 0; /* Remaining number of frames allowed */
349e7104 1826 int ret; /* Return code from calls */
5b435de0 1827 uint rxcount = 0; /* Total frames read */
6bc52319 1828 struct brcmf_sdio_hdrinfo *rd = &bus->cur_read, rd_new;
4754fcee 1829 u8 head_read = 0;
5b435de0
AS
1830
1831 brcmf_dbg(TRACE, "Enter\n");
1832
1833 /* Not finished unless we encounter no more frames indication */
4754fcee 1834 bus->rxpending = true;
5b435de0 1835
4754fcee 1836 for (rd->seq_num = bus->rx_seq, rxleft = maxframes;
bb350711 1837 !bus->rxskip && rxleft && brcmf_bus_ready(bus->sdiodev->bus_if);
4754fcee 1838 rd->seq_num++, rxleft--) {
5b435de0
AS
1839
1840 /* Handle glomming separately */
b83db862 1841 if (bus->glomd || !skb_queue_empty(&bus->glom)) {
5b435de0
AS
1842 u8 cnt;
1843 brcmf_dbg(GLOM, "calling rxglom: glomd %p, glom %p\n",
b83db862 1844 bus->glomd, skb_peek(&bus->glom));
82d7f3c1 1845 cnt = brcmf_sdio_rxglom(bus, rd->seq_num);
5b435de0 1846 brcmf_dbg(GLOM, "rxglom returned %d\n", cnt);
4754fcee 1847 rd->seq_num += cnt - 1;
5b435de0
AS
1848 rxleft = (rxleft > cnt) ? (rxleft - cnt) : 1;
1849 continue;
1850 }
1851
4754fcee
FL
1852 rd->len_left = rd->len;
1853 /* read header first for unknow frame length */
38b0b0dd 1854 sdio_claim_host(bus->sdiodev->func[1]);
4754fcee 1855 if (!rd->len) {
a39be27b 1856 ret = brcmf_sdiod_recv_buf(bus->sdiodev,
a39be27b 1857 bus->rxhdr, BRCMF_FIRSTREAD);
4754fcee 1858 bus->sdcnt.f2rxhdrs++;
349e7104 1859 if (ret < 0) {
5e8149f5 1860 brcmf_err("RXHEADER FAILED: %d\n",
349e7104 1861 ret);
4754fcee 1862 bus->sdcnt.rx_hdrfail++;
82d7f3c1 1863 brcmf_sdio_rxfail(bus, true, true);
38b0b0dd 1864 sdio_release_host(bus->sdiodev->func[1]);
5b435de0 1865 continue;
5b435de0 1866 }
5b435de0 1867
4754fcee 1868 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() || BRCMF_HDRS_ON(),
1e023829
JP
1869 bus->rxhdr, SDPCM_HDRLEN,
1870 "RxHdr:\n");
5b435de0 1871
6bc52319
FL
1872 if (brcmf_sdio_hdparse(bus, bus->rxhdr, rd,
1873 BRCMF_SDIO_FT_NORMAL)) {
38b0b0dd 1874 sdio_release_host(bus->sdiodev->func[1]);
4754fcee
FL
1875 if (!bus->rxpending)
1876 break;
1877 else
1878 continue;
5b435de0
AS
1879 }
1880
4754fcee 1881 if (rd->channel == SDPCM_CONTROL_CHANNEL) {
82d7f3c1
AS
1882 brcmf_sdio_read_control(bus, bus->rxhdr,
1883 rd->len,
1884 rd->dat_offset);
4754fcee
FL
1885 /* prepare the descriptor for the next read */
1886 rd->len = rd->len_nxtfrm << 4;
1887 rd->len_nxtfrm = 0;
1888 /* treat all packet as event if we don't know */
1889 rd->channel = SDPCM_EVENT_CHANNEL;
38b0b0dd 1890 sdio_release_host(bus->sdiodev->func[1]);
5b435de0
AS
1891 continue;
1892 }
4754fcee
FL
1893 rd->len_left = rd->len > BRCMF_FIRSTREAD ?
1894 rd->len - BRCMF_FIRSTREAD : 0;
1895 head_read = BRCMF_FIRSTREAD;
5b435de0
AS
1896 }
1897
82d7f3c1 1898 brcmf_sdio_pad(bus, &pad, &rd->len_left);
5b435de0 1899
4754fcee 1900 pkt = brcmu_pkt_buf_get_skb(rd->len_left + head_read +
9b2d2f2a 1901 bus->head_align);
5b435de0
AS
1902 if (!pkt) {
1903 /* Give up on data, request rtx of events */
5e8149f5 1904 brcmf_err("brcmu_pkt_buf_get_skb failed\n");
82d7f3c1 1905 brcmf_sdio_rxfail(bus, false,
4754fcee 1906 RETRYCHAN(rd->channel));
38b0b0dd 1907 sdio_release_host(bus->sdiodev->func[1]);
5b435de0
AS
1908 continue;
1909 }
4754fcee 1910 skb_pull(pkt, head_read);
9b2d2f2a 1911 pkt_align(pkt, rd->len_left, bus->head_align);
5b435de0 1912
a7cdd821 1913 ret = brcmf_sdiod_recv_pkt(bus->sdiodev, pkt);
80969836 1914 bus->sdcnt.f2rxdata++;
38b0b0dd 1915 sdio_release_host(bus->sdiodev->func[1]);
5b435de0 1916
349e7104 1917 if (ret < 0) {
5e8149f5 1918 brcmf_err("read %d bytes from channel %d failed: %d\n",
349e7104 1919 rd->len, rd->channel, ret);
5b435de0 1920 brcmu_pkt_buf_free_skb(pkt);
38b0b0dd 1921 sdio_claim_host(bus->sdiodev->func[1]);
82d7f3c1 1922 brcmf_sdio_rxfail(bus, true,
4754fcee 1923 RETRYCHAN(rd->channel));
38b0b0dd 1924 sdio_release_host(bus->sdiodev->func[1]);
5b435de0
AS
1925 continue;
1926 }
1927
4754fcee
FL
1928 if (head_read) {
1929 skb_push(pkt, head_read);
1930 memcpy(pkt->data, bus->rxhdr, head_read);
1931 head_read = 0;
1932 } else {
1933 memcpy(bus->rxhdr, pkt->data, SDPCM_HDRLEN);
1934 rd_new.seq_num = rd->seq_num;
38b0b0dd 1935 sdio_claim_host(bus->sdiodev->func[1]);
6bc52319
FL
1936 if (brcmf_sdio_hdparse(bus, bus->rxhdr, &rd_new,
1937 BRCMF_SDIO_FT_NORMAL)) {
4754fcee
FL
1938 rd->len = 0;
1939 brcmu_pkt_buf_free_skb(pkt);
1940 }
1941 bus->sdcnt.rx_readahead_cnt++;
1942 if (rd->len != roundup(rd_new.len, 16)) {
5e8149f5 1943 brcmf_err("frame length mismatch:read %d, should be %d\n",
4754fcee
FL
1944 rd->len,
1945 roundup(rd_new.len, 16) >> 4);
1946 rd->len = 0;
82d7f3c1 1947 brcmf_sdio_rxfail(bus, true, true);
38b0b0dd 1948 sdio_release_host(bus->sdiodev->func[1]);
4754fcee
FL
1949 brcmu_pkt_buf_free_skb(pkt);
1950 continue;
1951 }
38b0b0dd 1952 sdio_release_host(bus->sdiodev->func[1]);
4754fcee
FL
1953 rd->len_nxtfrm = rd_new.len_nxtfrm;
1954 rd->channel = rd_new.channel;
1955 rd->dat_offset = rd_new.dat_offset;
1956
1957 brcmf_dbg_hex_dump(!(BRCMF_BYTES_ON() &&
1958 BRCMF_DATA_ON()) &&
1959 BRCMF_HDRS_ON(),
1960 bus->rxhdr, SDPCM_HDRLEN,
1961 "RxHdr:\n");
1962
1963 if (rd_new.channel == SDPCM_CONTROL_CHANNEL) {
5e8149f5 1964 brcmf_err("readahead on control packet %d?\n",
4754fcee
FL
1965 rd_new.seq_num);
1966 /* Force retry w/normal header read */
1967 rd->len = 0;
38b0b0dd 1968 sdio_claim_host(bus->sdiodev->func[1]);
82d7f3c1 1969 brcmf_sdio_rxfail(bus, false, true);
38b0b0dd 1970 sdio_release_host(bus->sdiodev->func[1]);
4754fcee
FL
1971 brcmu_pkt_buf_free_skb(pkt);
1972 continue;
1973 }
1974 }
5b435de0 1975
1e023829 1976 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_DATA_ON(),
4754fcee 1977 pkt->data, rd->len, "Rx Data:\n");
5b435de0 1978
5b435de0 1979 /* Save superframe descriptor and allocate packet frame */
4754fcee 1980 if (rd->channel == SDPCM_GLOM_CHANNEL) {
6bc52319 1981 if (SDPCM_GLOMDESC(&bus->rxhdr[SDPCM_HWHDR_LEN])) {
5b435de0 1982 brcmf_dbg(GLOM, "glom descriptor, %d bytes:\n",
4754fcee 1983 rd->len);
1e023829 1984 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
4754fcee 1985 pkt->data, rd->len,
1e023829 1986 "Glom Data:\n");
4754fcee 1987 __skb_trim(pkt, rd->len);
5b435de0
AS
1988 skb_pull(pkt, SDPCM_HDRLEN);
1989 bus->glomd = pkt;
1990 } else {
5e8149f5 1991 brcmf_err("%s: glom superframe w/o "
5b435de0 1992 "descriptor!\n", __func__);
38b0b0dd 1993 sdio_claim_host(bus->sdiodev->func[1]);
82d7f3c1 1994 brcmf_sdio_rxfail(bus, false, false);
38b0b0dd 1995 sdio_release_host(bus->sdiodev->func[1]);
5b435de0 1996 }
4754fcee
FL
1997 /* prepare the descriptor for the next read */
1998 rd->len = rd->len_nxtfrm << 4;
1999 rd->len_nxtfrm = 0;
2000 /* treat all packet as event if we don't know */
2001 rd->channel = SDPCM_EVENT_CHANNEL;
5b435de0
AS
2002 continue;
2003 }
2004
2005 /* Fill in packet len and prio, deliver upward */
4754fcee
FL
2006 __skb_trim(pkt, rd->len);
2007 skb_pull(pkt, rd->dat_offset);
2008
2009 /* prepare the descriptor for the next read */
2010 rd->len = rd->len_nxtfrm << 4;
2011 rd->len_nxtfrm = 0;
2012 /* treat all packet as event if we don't know */
2013 rd->channel = SDPCM_EVENT_CHANNEL;
5b435de0
AS
2014
2015 if (pkt->len == 0) {
2016 brcmu_pkt_buf_free_skb(pkt);
2017 continue;
5b435de0
AS
2018 }
2019
05f3820b 2020 brcmf_rx_frame(bus->sdiodev->dev, pkt);
5b435de0 2021 }
4754fcee 2022
5b435de0 2023 rxcount = maxframes - rxleft;
5b435de0
AS
2024 /* Message if we hit the limit */
2025 if (!rxleft)
4754fcee 2026 brcmf_dbg(DATA, "hit rx limit of %d frames\n", maxframes);
5b435de0 2027 else
5b435de0
AS
2028 brcmf_dbg(DATA, "processed %d frames\n", rxcount);
2029 /* Back off rxseq if awaiting rtx, update rx_seq */
2030 if (bus->rxskip)
4754fcee
FL
2031 rd->seq_num--;
2032 bus->rx_seq = rd->seq_num;
5b435de0
AS
2033
2034 return rxcount;
2035}
2036
5b435de0 2037static void
82d7f3c1 2038brcmf_sdio_wait_event_wakeup(struct brcmf_sdio *bus)
5b435de0
AS
2039{
2040 if (waitqueue_active(&bus->ctrl_wait))
2041 wake_up_interruptible(&bus->ctrl_wait);
2042 return;
2043}
2044
8da9d2c8
FL
2045static int brcmf_sdio_txpkt_hdalign(struct brcmf_sdio *bus, struct sk_buff *pkt)
2046{
e217d1c8 2047 u16 head_pad;
8da9d2c8
FL
2048 u8 *dat_buf;
2049
8da9d2c8
FL
2050 dat_buf = (u8 *)(pkt->data);
2051
2052 /* Check head padding */
e217d1c8 2053 head_pad = ((unsigned long)dat_buf % bus->head_align);
8da9d2c8
FL
2054 if (head_pad) {
2055 if (skb_headroom(pkt) < head_pad) {
2056 bus->sdiodev->bus_if->tx_realloc++;
2057 head_pad = 0;
2058 if (skb_cow(pkt, head_pad))
2059 return -ENOMEM;
2060 }
2061 skb_push(pkt, head_pad);
2062 dat_buf = (u8 *)(pkt->data);
2063 memset(dat_buf, 0, head_pad + bus->tx_hdrlen);
2064 }
2065 return head_pad;
2066}
2067
5491c11c
FL
2068/**
2069 * struct brcmf_skbuff_cb reserves first two bytes in sk_buff::cb for
2070 * bus layer usage.
2071 */
b05e9254 2072/* flag marking a dummy skb added for DMA alignment requirement */
5491c11c 2073#define ALIGN_SKB_FLAG 0x8000
b05e9254 2074/* bit mask of data length chopped from the previous packet */
5491c11c
FL
2075#define ALIGN_SKB_CHOP_LEN_MASK 0x7fff
2076
8da9d2c8 2077static int brcmf_sdio_txpkt_prep_sg(struct brcmf_sdio *bus,
a64304f0 2078 struct sk_buff_head *pktq,
8da9d2c8 2079 struct sk_buff *pkt, u16 total_len)
a64304f0 2080{
8da9d2c8 2081 struct brcmf_sdio_dev *sdiodev;
a64304f0 2082 struct sk_buff *pkt_pad;
e217d1c8 2083 u16 tail_pad, tail_chop, chain_pad;
a64304f0 2084 unsigned int blksize;
8da9d2c8
FL
2085 bool lastfrm;
2086 int ntail, ret;
a64304f0 2087
8da9d2c8 2088 sdiodev = bus->sdiodev;
a64304f0 2089 blksize = sdiodev->func[SDIO_FUNC_2]->cur_blksize;
a64304f0 2090 /* sg entry alignment should be a divisor of block size */
e217d1c8 2091 WARN_ON(blksize % bus->sgentry_align);
a64304f0
AS
2092
2093 /* Check tail padding */
8da9d2c8
FL
2094 lastfrm = skb_queue_is_last(pktq, pkt);
2095 tail_pad = 0;
e217d1c8 2096 tail_chop = pkt->len % bus->sgentry_align;
8da9d2c8 2097 if (tail_chop)
e217d1c8 2098 tail_pad = bus->sgentry_align - tail_chop;
8da9d2c8
FL
2099 chain_pad = (total_len + tail_pad) % blksize;
2100 if (lastfrm && chain_pad)
2101 tail_pad += blksize - chain_pad;
a64304f0 2102 if (skb_tailroom(pkt) < tail_pad && pkt->len > blksize) {
8da9d2c8
FL
2103 pkt_pad = bus->txglom_sgpad;
2104 if (pkt_pad == NULL)
2105 brcmu_pkt_buf_get_skb(tail_pad + tail_chop);
a64304f0
AS
2106 if (pkt_pad == NULL)
2107 return -ENOMEM;
8da9d2c8
FL
2108 ret = brcmf_sdio_txpkt_hdalign(bus, pkt_pad);
2109 if (unlikely(ret < 0))
2110 return ret;
a64304f0
AS
2111 memcpy(pkt_pad->data,
2112 pkt->data + pkt->len - tail_chop,
2113 tail_chop);
5aa9f0ea 2114 *(u16 *)(pkt_pad->cb) = ALIGN_SKB_FLAG + tail_chop;
a64304f0
AS
2115 skb_trim(pkt, pkt->len - tail_chop);
2116 __skb_queue_after(pktq, pkt, pkt_pad);
2117 } else {
2118 ntail = pkt->data_len + tail_pad -
2119 (pkt->end - pkt->tail);
2120 if (skb_cloned(pkt) || ntail > 0)
2121 if (pskb_expand_head(pkt, 0, ntail, GFP_ATOMIC))
2122 return -ENOMEM;
2123 if (skb_linearize(pkt))
2124 return -ENOMEM;
a64304f0
AS
2125 __skb_put(pkt, tail_pad);
2126 }
2127
8da9d2c8 2128 return tail_pad;
a64304f0
AS
2129}
2130
b05e9254
FL
2131/**
2132 * brcmf_sdio_txpkt_prep - packet preparation for transmit
2133 * @bus: brcmf_sdio structure pointer
2134 * @pktq: packet list pointer
2135 * @chan: virtual channel to transmit the packet
2136 *
2137 * Processes to be applied to the packet
2138 * - Align data buffer pointer
2139 * - Align data buffer length
2140 * - Prepare header
2141 * Return: negative value if there is error
2142 */
2143static int
2144brcmf_sdio_txpkt_prep(struct brcmf_sdio *bus, struct sk_buff_head *pktq,
2145 uint chan)
5b435de0 2146{
8da9d2c8 2147 u16 head_pad, total_len;
a64304f0 2148 struct sk_buff *pkt_next;
8da9d2c8
FL
2149 u8 txseq;
2150 int ret;
6bc52319 2151 struct brcmf_sdio_hdrinfo hd_info = {0};
b05e9254 2152
8da9d2c8
FL
2153 txseq = bus->tx_seq;
2154 total_len = 0;
2155 skb_queue_walk(pktq, pkt_next) {
2156 /* alignment packet inserted in previous
2157 * loop cycle can be skipped as it is
2158 * already properly aligned and does not
2159 * need an sdpcm header.
2160 */
5aa9f0ea 2161 if (*(u16 *)(pkt_next->cb) & ALIGN_SKB_FLAG)
8da9d2c8 2162 continue;
5b435de0 2163
8da9d2c8
FL
2164 /* align packet data pointer */
2165 ret = brcmf_sdio_txpkt_hdalign(bus, pkt_next);
2166 if (ret < 0)
2167 return ret;
2168 head_pad = (u16)ret;
2169 if (head_pad)
2170 memset(pkt_next->data, 0, head_pad + bus->tx_hdrlen);
5b435de0 2171
8da9d2c8 2172 total_len += pkt_next->len;
5b435de0 2173
a64304f0 2174 hd_info.len = pkt_next->len;
8da9d2c8
FL
2175 hd_info.lastfrm = skb_queue_is_last(pktq, pkt_next);
2176 if (bus->txglom && pktq->qlen > 1) {
2177 ret = brcmf_sdio_txpkt_prep_sg(bus, pktq,
2178 pkt_next, total_len);
2179 if (ret < 0)
2180 return ret;
2181 hd_info.tail_pad = (u16)ret;
2182 total_len += (u16)ret;
2183 }
5b435de0 2184
8da9d2c8
FL
2185 hd_info.channel = chan;
2186 hd_info.dat_offset = head_pad + bus->tx_hdrlen;
2187 hd_info.seq_num = txseq++;
2188
2189 /* Now fill the header */
2190 brcmf_sdio_hdpack(bus, pkt_next->data, &hd_info);
2191
2192 if (BRCMF_BYTES_ON() &&
2193 ((BRCMF_CTL_ON() && chan == SDPCM_CONTROL_CHANNEL) ||
2194 (BRCMF_DATA_ON() && chan != SDPCM_CONTROL_CHANNEL)))
47ab4cd8 2195 brcmf_dbg_hex_dump(true, pkt_next->data, hd_info.len,
8da9d2c8
FL
2196 "Tx Frame:\n");
2197 else if (BRCMF_HDRS_ON())
47ab4cd8 2198 brcmf_dbg_hex_dump(true, pkt_next->data,
8da9d2c8
FL
2199 head_pad + bus->tx_hdrlen,
2200 "Tx Header:\n");
2201 }
2202 /* Hardware length tag of the first packet should be total
2203 * length of the chain (including padding)
2204 */
2205 if (bus->txglom)
2206 brcmf_sdio_update_hwhdr(pktq->next->data, total_len);
b05e9254
FL
2207 return 0;
2208}
5b435de0 2209
b05e9254
FL
2210/**
2211 * brcmf_sdio_txpkt_postp - packet post processing for transmit
2212 * @bus: brcmf_sdio structure pointer
2213 * @pktq: packet list pointer
2214 *
2215 * Processes to be applied to the packet
2216 * - Remove head padding
2217 * - Remove tail padding
2218 */
2219static void
2220brcmf_sdio_txpkt_postp(struct brcmf_sdio *bus, struct sk_buff_head *pktq)
2221{
2222 u8 *hdr;
2223 u32 dat_offset;
8da9d2c8 2224 u16 tail_pad;
5aa9f0ea 2225 u16 dummy_flags, chop_len;
b05e9254
FL
2226 struct sk_buff *pkt_next, *tmp, *pkt_prev;
2227
2228 skb_queue_walk_safe(pktq, pkt_next, tmp) {
5aa9f0ea 2229 dummy_flags = *(u16 *)(pkt_next->cb);
5491c11c
FL
2230 if (dummy_flags & ALIGN_SKB_FLAG) {
2231 chop_len = dummy_flags & ALIGN_SKB_CHOP_LEN_MASK;
b05e9254
FL
2232 if (chop_len) {
2233 pkt_prev = pkt_next->prev;
b05e9254
FL
2234 skb_put(pkt_prev, chop_len);
2235 }
2236 __skb_unlink(pkt_next, pktq);
2237 brcmu_pkt_buf_free_skb(pkt_next);
2238 } else {
8da9d2c8 2239 hdr = pkt_next->data + bus->tx_hdrlen - SDPCM_SWHDR_LEN;
b05e9254
FL
2240 dat_offset = le32_to_cpu(*(__le32 *)hdr);
2241 dat_offset = (dat_offset & SDPCM_DOFFSET_MASK) >>
2242 SDPCM_DOFFSET_SHIFT;
2243 skb_pull(pkt_next, dat_offset);
8da9d2c8
FL
2244 if (bus->txglom) {
2245 tail_pad = le16_to_cpu(*(__le16 *)(hdr - 2));
2246 skb_trim(pkt_next, pkt_next->len - tail_pad);
2247 }
b05e9254 2248 }
5b435de0 2249 }
b05e9254 2250}
5b435de0 2251
b05e9254
FL
2252/* Writes a HW/SW header into the packet and sends it. */
2253/* Assumes: (a) header space already there, (b) caller holds lock */
82d7f3c1
AS
2254static int brcmf_sdio_txpkt(struct brcmf_sdio *bus, struct sk_buff_head *pktq,
2255 uint chan)
b05e9254
FL
2256{
2257 int ret;
2258 int i;
8da9d2c8 2259 struct sk_buff *pkt_next, *tmp;
b05e9254
FL
2260
2261 brcmf_dbg(TRACE, "Enter\n");
2262
8da9d2c8 2263 ret = brcmf_sdio_txpkt_prep(bus, pktq, chan);
b05e9254
FL
2264 if (ret)
2265 goto done;
5b435de0 2266
38b0b0dd 2267 sdio_claim_host(bus->sdiodev->func[1]);
a7cdd821 2268 ret = brcmf_sdiod_send_pkt(bus->sdiodev, pktq);
80969836 2269 bus->sdcnt.f2txdata++;
5b435de0
AS
2270
2271 if (ret < 0) {
2272 /* On failure, abort the command and terminate the frame */
2273 brcmf_dbg(INFO, "sdio error %d, abort command and terminate frame\n",
2274 ret);
80969836 2275 bus->sdcnt.tx_sderrs++;
5b435de0 2276
a39be27b
AS
2277 brcmf_sdiod_abort(bus->sdiodev, SDIO_FUNC_2);
2278 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL,
2279 SFC_WF_TERM, NULL);
80969836 2280 bus->sdcnt.f1regdata++;
5b435de0
AS
2281
2282 for (i = 0; i < 3; i++) {
2283 u8 hi, lo;
a39be27b
AS
2284 hi = brcmf_sdiod_regrb(bus->sdiodev,
2285 SBSDIO_FUNC1_WFRAMEBCHI, NULL);
2286 lo = brcmf_sdiod_regrb(bus->sdiodev,
2287 SBSDIO_FUNC1_WFRAMEBCLO, NULL);
80969836 2288 bus->sdcnt.f1regdata += 2;
5b435de0
AS
2289 if ((hi == 0) && (lo == 0))
2290 break;
2291 }
5b435de0 2292 }
38b0b0dd 2293 sdio_release_host(bus->sdiodev->func[1]);
5b435de0
AS
2294
2295done:
8da9d2c8
FL
2296 brcmf_sdio_txpkt_postp(bus, pktq);
2297 if (ret == 0)
2298 bus->tx_seq = (bus->tx_seq + pktq->qlen) % SDPCM_SEQ_WRAP;
2299 skb_queue_walk_safe(pktq, pkt_next, tmp) {
2300 __skb_unlink(pkt_next, pktq);
2301 brcmf_txcomplete(bus->sdiodev->dev, pkt_next, ret == 0);
2302 }
5b435de0
AS
2303 return ret;
2304}
2305
82d7f3c1 2306static uint brcmf_sdio_sendfromq(struct brcmf_sdio *bus, uint maxframes)
5b435de0
AS
2307{
2308 struct sk_buff *pkt;
8da9d2c8 2309 struct sk_buff_head pktq;
5b435de0 2310 u32 intstatus = 0;
8da9d2c8 2311 int ret = 0, prec_out, i;
5b435de0 2312 uint cnt = 0;
8da9d2c8 2313 u8 tx_prec_map, pkt_num;
5b435de0 2314
5b435de0
AS
2315 brcmf_dbg(TRACE, "Enter\n");
2316
2317 tx_prec_map = ~bus->flowcontrol;
2318
2319 /* Send frames until the limit or some other event */
8da9d2c8
FL
2320 for (cnt = 0; (cnt < maxframes) && data_ok(bus);) {
2321 pkt_num = 1;
2322 __skb_queue_head_init(&pktq);
2323 if (bus->txglom)
2324 pkt_num = min_t(u8, bus->tx_max - bus->tx_seq,
2325 brcmf_sdio_txglomsz);
2326 pkt_num = min_t(u32, pkt_num,
2327 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol));
5b435de0 2328 spin_lock_bh(&bus->txqlock);
8da9d2c8
FL
2329 for (i = 0; i < pkt_num; i++) {
2330 pkt = brcmu_pktq_mdeq(&bus->txq, tx_prec_map,
2331 &prec_out);
2332 if (pkt == NULL)
2333 break;
2334 __skb_queue_tail(&pktq, pkt);
5b435de0
AS
2335 }
2336 spin_unlock_bh(&bus->txqlock);
8da9d2c8
FL
2337 if (i == 0)
2338 break;
5b435de0 2339
82d7f3c1 2340 ret = brcmf_sdio_txpkt(bus, &pktq, SDPCM_DATA_CHANNEL);
8da9d2c8 2341 cnt += i;
5b435de0
AS
2342
2343 /* In poll mode, need to check for other events */
2344 if (!bus->intr && cnt) {
2345 /* Check device status, signal pending interrupt */
38b0b0dd 2346 sdio_claim_host(bus->sdiodev->func[1]);
5c15c23a
FL
2347 ret = r_sdreg32(bus, &intstatus,
2348 offsetof(struct sdpcmd_regs,
2349 intstatus));
38b0b0dd 2350 sdio_release_host(bus->sdiodev->func[1]);
80969836 2351 bus->sdcnt.f2txdata++;
5c15c23a 2352 if (ret != 0)
5b435de0
AS
2353 break;
2354 if (intstatus & bus->hostintmask)
1d382273 2355 atomic_set(&bus->ipend, 1);
5b435de0
AS
2356 }
2357 }
2358
2359 /* Deflow-control stack if needed */
05dde977 2360 if ((bus->sdiodev->bus_if->state == BRCMF_BUS_DATA) &&
c8bf3484 2361 bus->txoff && (pktq_len(&bus->txq) < TXLOW)) {
90d03ff7
HM
2362 bus->txoff = false;
2363 brcmf_txflowblock(bus->sdiodev->dev, false);
c8bf3484 2364 }
5b435de0
AS
2365
2366 return cnt;
2367}
2368
82d7f3c1 2369static void brcmf_sdio_bus_stop(struct device *dev)
a9ffda88
FL
2370{
2371 u32 local_hostintmask;
2372 u8 saveclk;
a9ffda88
FL
2373 int err;
2374 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
0a332e46 2375 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
a9ffda88
FL
2376 struct brcmf_sdio *bus = sdiodev->bus;
2377
2378 brcmf_dbg(TRACE, "Enter\n");
2379
2380 if (bus->watchdog_tsk) {
2381 send_sig(SIGTERM, bus->watchdog_tsk, 1);
2382 kthread_stop(bus->watchdog_tsk);
2383 bus->watchdog_tsk = NULL;
2384 }
2385
bb350711
AS
2386 if (bus_if->state == BRCMF_BUS_DOWN) {
2387 sdio_claim_host(sdiodev->func[1]);
2388
2389 /* Enable clock for device interrupts */
2390 brcmf_sdio_bus_sleep(bus, false, false);
2391
2392 /* Disable and clear interrupts at the chip level also */
2393 w_sdreg32(bus, 0, offsetof(struct sdpcmd_regs, hostintmask));
2394 local_hostintmask = bus->hostintmask;
2395 bus->hostintmask = 0;
2396
2397 /* Force backplane clocks to assure F2 interrupt propagates */
2398 saveclk = brcmf_sdiod_regrb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
2399 &err);
2400 if (!err)
2401 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
2402 (saveclk | SBSDIO_FORCE_HT), &err);
2403 if (err)
2404 brcmf_err("Failed to force clock for F2: err %d\n",
2405 err);
a9ffda88 2406
bb350711
AS
2407 /* Turn off the bus (F2), free any pending packets */
2408 brcmf_dbg(INTR, "disable SDIO interrupts\n");
2409 sdio_disable_func(sdiodev->func[SDIO_FUNC_2]);
a9ffda88 2410
bb350711
AS
2411 /* Clear any pending interrupts now that F2 is disabled */
2412 w_sdreg32(bus, local_hostintmask,
2413 offsetof(struct sdpcmd_regs, intstatus));
a9ffda88 2414
bb350711 2415 sdio_release_host(sdiodev->func[1]);
a9ffda88 2416 }
a9ffda88
FL
2417 /* Clear the data packet queues */
2418 brcmu_pktq_flush(&bus->txq, true, NULL, NULL);
2419
2420 /* Clear any held glomming stuff */
2421 if (bus->glomd)
2422 brcmu_pkt_buf_free_skb(bus->glomd);
82d7f3c1 2423 brcmf_sdio_free_glom(bus);
a9ffda88
FL
2424
2425 /* Clear rx control and wake any waiters */
dd43a01c 2426 spin_lock_bh(&bus->rxctl_lock);
a9ffda88 2427 bus->rxlen = 0;
dd43a01c 2428 spin_unlock_bh(&bus->rxctl_lock);
82d7f3c1 2429 brcmf_sdio_dcmd_resp_wake(bus);
a9ffda88
FL
2430
2431 /* Reset some F2 state stuff */
2432 bus->rxskip = false;
2433 bus->tx_seq = bus->rx_seq = 0;
a9ffda88
FL
2434}
2435
82d7f3c1 2436static inline void brcmf_sdio_clrintr(struct brcmf_sdio *bus)
ba89bf19
FL
2437{
2438 unsigned long flags;
2439
668761ac
HM
2440 if (bus->sdiodev->oob_irq_requested) {
2441 spin_lock_irqsave(&bus->sdiodev->irq_en_lock, flags);
2442 if (!bus->sdiodev->irq_en && !atomic_read(&bus->ipend)) {
2443 enable_irq(bus->sdiodev->pdata->oob_irq_nr);
2444 bus->sdiodev->irq_en = true;
2445 }
2446 spin_unlock_irqrestore(&bus->sdiodev->irq_en_lock, flags);
ba89bf19 2447 }
ba89bf19 2448}
ba89bf19 2449
4531603a
FL
2450static int brcmf_sdio_intr_rstatus(struct brcmf_sdio *bus)
2451{
cb7cf7be 2452 struct brcmf_core *buscore;
4531603a
FL
2453 u32 addr;
2454 unsigned long val;
2455 int n, ret;
2456
cb7cf7be
AS
2457 buscore = brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV);
2458 addr = buscore->base + offsetof(struct sdpcmd_regs, intstatus);
4531603a 2459
a39be27b 2460 val = brcmf_sdiod_regrl(bus->sdiodev, addr, &ret);
4531603a
FL
2461 bus->sdcnt.f1regdata++;
2462 if (ret != 0)
2463 val = 0;
2464
2465 val &= bus->hostintmask;
2466 atomic_set(&bus->fcstate, !!(val & I_HMB_FC_STATE));
2467
2468 /* Clear interrupts */
2469 if (val) {
a39be27b 2470 brcmf_sdiod_regwl(bus->sdiodev, addr, val, &ret);
4531603a
FL
2471 bus->sdcnt.f1regdata++;
2472 }
2473
2474 if (ret) {
2475 atomic_set(&bus->intstatus, 0);
2476 } else if (val) {
2477 for_each_set_bit(n, &val, 32)
2478 set_bit(n, (unsigned long *)&bus->intstatus.counter);
2479 }
2480
2481 return ret;
2482}
2483
82d7f3c1 2484static void brcmf_sdio_dpc(struct brcmf_sdio *bus)
5b435de0 2485{
4531603a
FL
2486 u32 newstatus = 0;
2487 unsigned long intstatus;
5b435de0
AS
2488 uint rxlimit = bus->rxbound; /* Rx frames to read before resched */
2489 uint txlimit = bus->txbound; /* Tx frames to send before resched */
2490 uint framecnt = 0; /* Temporary counter of tx/rx frames */
4531603a 2491 int err = 0, n;
5b435de0
AS
2492
2493 brcmf_dbg(TRACE, "Enter\n");
2494
38b0b0dd 2495 sdio_claim_host(bus->sdiodev->func[1]);
5b435de0
AS
2496
2497 /* If waiting for HTAVAIL, check status */
4a3da990 2498 if (!bus->sr_enabled && bus->clkstate == CLK_PENDING) {
5b435de0
AS
2499 u8 clkctl, devctl = 0;
2500
8ae74654 2501#ifdef DEBUG
5b435de0 2502 /* Check for inconsistent device control */
a39be27b
AS
2503 devctl = brcmf_sdiod_regrb(bus->sdiodev,
2504 SBSDIO_DEVICE_CTL, &err);
8ae74654 2505#endif /* DEBUG */
5b435de0
AS
2506
2507 /* Read CSR, if clock on switch to AVAIL, else ignore */
a39be27b
AS
2508 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
2509 SBSDIO_FUNC1_CHIPCLKCSR, &err);
5b435de0 2510
c3203374 2511 brcmf_dbg(SDIO, "DPC: PENDING, devctl 0x%02x clkctl 0x%02x\n",
5b435de0
AS
2512 devctl, clkctl);
2513
2514 if (SBSDIO_HTAV(clkctl)) {
a39be27b
AS
2515 devctl = brcmf_sdiod_regrb(bus->sdiodev,
2516 SBSDIO_DEVICE_CTL, &err);
5b435de0 2517 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
a39be27b
AS
2518 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
2519 devctl, &err);
5b435de0 2520 bus->clkstate = CLK_AVAIL;
5b435de0
AS
2521 }
2522 }
2523
5b435de0 2524 /* Make sure backplane clock is on */
82d7f3c1 2525 brcmf_sdio_bus_sleep(bus, false, true);
5b435de0
AS
2526
2527 /* Pending interrupt indicates new device status */
1d382273
FL
2528 if (atomic_read(&bus->ipend) > 0) {
2529 atomic_set(&bus->ipend, 0);
4531603a 2530 err = brcmf_sdio_intr_rstatus(bus);
5b435de0
AS
2531 }
2532
4531603a
FL
2533 /* Start with leftover status bits */
2534 intstatus = atomic_xchg(&bus->intstatus, 0);
5b435de0
AS
2535
2536 /* Handle flow-control change: read new state in case our ack
2537 * crossed another change interrupt. If change still set, assume
2538 * FC ON for safety, let next loop through do the debounce.
2539 */
2540 if (intstatus & I_HMB_FC_CHANGE) {
2541 intstatus &= ~I_HMB_FC_CHANGE;
5c15c23a
FL
2542 err = w_sdreg32(bus, I_HMB_FC_CHANGE,
2543 offsetof(struct sdpcmd_regs, intstatus));
5b435de0 2544
5c15c23a
FL
2545 err = r_sdreg32(bus, &newstatus,
2546 offsetof(struct sdpcmd_regs, intstatus));
80969836 2547 bus->sdcnt.f1regdata += 2;
4531603a
FL
2548 atomic_set(&bus->fcstate,
2549 !!(newstatus & (I_HMB_FC_STATE | I_HMB_FC_CHANGE)));
5b435de0
AS
2550 intstatus |= (newstatus & bus->hostintmask);
2551 }
2552
2553 /* Handle host mailbox indication */
2554 if (intstatus & I_HMB_HOST_INT) {
2555 intstatus &= ~I_HMB_HOST_INT;
82d7f3c1 2556 intstatus |= brcmf_sdio_hostmail(bus);
5b435de0
AS
2557 }
2558
38b0b0dd 2559 sdio_release_host(bus->sdiodev->func[1]);
7cdf57d3 2560
5b435de0
AS
2561 /* Generally don't ask for these, can get CRC errors... */
2562 if (intstatus & I_WR_OOSYNC) {
5e8149f5 2563 brcmf_err("Dongle reports WR_OOSYNC\n");
5b435de0
AS
2564 intstatus &= ~I_WR_OOSYNC;
2565 }
2566
2567 if (intstatus & I_RD_OOSYNC) {
5e8149f5 2568 brcmf_err("Dongle reports RD_OOSYNC\n");
5b435de0
AS
2569 intstatus &= ~I_RD_OOSYNC;
2570 }
2571
2572 if (intstatus & I_SBINT) {
5e8149f5 2573 brcmf_err("Dongle reports SBINT\n");
5b435de0
AS
2574 intstatus &= ~I_SBINT;
2575 }
2576
2577 /* Would be active due to wake-wlan in gSPI */
2578 if (intstatus & I_CHIPACTIVE) {
2579 brcmf_dbg(INFO, "Dongle reports CHIPACTIVE\n");
2580 intstatus &= ~I_CHIPACTIVE;
2581 }
2582
2583 /* Ignore frame indications if rxskip is set */
2584 if (bus->rxskip)
2585 intstatus &= ~I_HMB_FRAME_IND;
2586
2587 /* On frame indication, read available frames */
03d5c360 2588 if (PKT_AVAILABLE() && bus->clkstate == CLK_AVAIL) {
4754fcee
FL
2589 framecnt = brcmf_sdio_readframes(bus, rxlimit);
2590 if (!bus->rxpending)
5b435de0
AS
2591 intstatus &= ~I_HMB_FRAME_IND;
2592 rxlimit -= min(framecnt, rxlimit);
2593 }
2594
2595 /* Keep still-pending events for next scheduling */
4531603a
FL
2596 if (intstatus) {
2597 for_each_set_bit(n, &intstatus, 32)
2598 set_bit(n, (unsigned long *)&bus->intstatus.counter);
2599 }
5b435de0 2600
82d7f3c1 2601 brcmf_sdio_clrintr(bus);
ba89bf19 2602
5b435de0
AS
2603 if (data_ok(bus) && bus->ctrl_frame_stat &&
2604 (bus->clkstate == CLK_AVAIL)) {
03d5c360 2605 int i;
5b435de0 2606
38b0b0dd 2607 sdio_claim_host(bus->sdiodev->func[1]);
a7cdd821 2608 err = brcmf_sdiod_send_buf(bus->sdiodev, bus->ctrl_frame_buf,
a39be27b 2609 (u32)bus->ctrl_frame_len);
5b435de0 2610
03d5c360 2611 if (err < 0) {
5b435de0
AS
2612 /* On failure, abort the command and
2613 terminate the frame */
2614 brcmf_dbg(INFO, "sdio error %d, abort command and terminate frame\n",
03d5c360 2615 err);
80969836 2616 bus->sdcnt.tx_sderrs++;
5b435de0 2617
a39be27b 2618 brcmf_sdiod_abort(bus->sdiodev, SDIO_FUNC_2);
5b435de0 2619
a39be27b
AS
2620 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL,
2621 SFC_WF_TERM, &err);
80969836 2622 bus->sdcnt.f1regdata++;
5b435de0
AS
2623
2624 for (i = 0; i < 3; i++) {
2625 u8 hi, lo;
a39be27b
AS
2626 hi = brcmf_sdiod_regrb(bus->sdiodev,
2627 SBSDIO_FUNC1_WFRAMEBCHI,
2628 &err);
2629 lo = brcmf_sdiod_regrb(bus->sdiodev,
2630 SBSDIO_FUNC1_WFRAMEBCLO,
2631 &err);
80969836 2632 bus->sdcnt.f1regdata += 2;
5b435de0
AS
2633 if ((hi == 0) && (lo == 0))
2634 break;
2635 }
2636
03d5c360 2637 } else {
6bc52319 2638 bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQ_WRAP;
03d5c360 2639 }
38b0b0dd 2640 sdio_release_host(bus->sdiodev->func[1]);
5b435de0 2641 bus->ctrl_frame_stat = false;
82d7f3c1 2642 brcmf_sdio_wait_event_wakeup(bus);
5b435de0
AS
2643 }
2644 /* Send queued frames (limit 1 if rx may still be pending) */
4531603a 2645 else if ((bus->clkstate == CLK_AVAIL) && !atomic_read(&bus->fcstate) &&
5b435de0
AS
2646 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) && txlimit
2647 && data_ok(bus)) {
4754fcee
FL
2648 framecnt = bus->rxpending ? min(txlimit, bus->txminmax) :
2649 txlimit;
82d7f3c1 2650 framecnt = brcmf_sdio_sendfromq(bus, framecnt);
5b435de0
AS
2651 txlimit -= framecnt;
2652 }
2653
bb350711 2654 if (!brcmf_bus_ready(bus->sdiodev->bus_if) || (err != 0)) {
5e8149f5 2655 brcmf_err("failed backplane access over SDIO, halting operation\n");
4531603a
FL
2656 atomic_set(&bus->intstatus, 0);
2657 } else if (atomic_read(&bus->intstatus) ||
2658 atomic_read(&bus->ipend) > 0 ||
2659 (!atomic_read(&bus->fcstate) &&
2660 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) &&
2661 data_ok(bus)) || PKT_AVAILABLE()) {
fccfe930 2662 atomic_inc(&bus->dpc_tskcnt);
5b435de0 2663 }
5b435de0
AS
2664}
2665
82d7f3c1 2666static struct pktq *brcmf_sdio_bus_gettxq(struct device *dev)
e2432b67
AS
2667{
2668 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
2669 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
2670 struct brcmf_sdio *bus = sdiodev->bus;
2671
2672 return &bus->txq;
2673}
2674
82d7f3c1 2675static int brcmf_sdio_bus_txdata(struct device *dev, struct sk_buff *pkt)
5b435de0
AS
2676{
2677 int ret = -EBADE;
44ff5660 2678 uint prec;
bf347bb9 2679 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
0a332e46 2680 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
bf347bb9 2681 struct brcmf_sdio *bus = sdiodev->bus;
4061f895 2682 ulong flags;
5b435de0 2683
44ff5660 2684 brcmf_dbg(TRACE, "Enter: pkt: data %p len %d\n", pkt->data, pkt->len);
5b435de0
AS
2685
2686 /* Add space for the header */
706478cb 2687 skb_push(pkt, bus->tx_hdrlen);
5b435de0
AS
2688 /* precondition: IS_ALIGNED((unsigned long)(pkt->data), 2) */
2689
2690 prec = prio2prec((pkt->priority & PRIOMASK));
2691
2692 /* Check for existing queue, current flow-control,
2693 pending event, or pending clock */
2694 brcmf_dbg(TRACE, "deferring pktq len %d\n", pktq_len(&bus->txq));
80969836 2695 bus->sdcnt.fcqueued++;
5b435de0
AS
2696
2697 /* Priority based enq */
4061f895 2698 spin_lock_irqsave(&bus->txqlock, flags);
5aa9f0ea
AS
2699 /* reset bus_flags in packet cb */
2700 *(u16 *)(pkt->cb) = 0;
23677ce3 2701 if (!brcmf_c_prec_enq(bus->sdiodev->dev, &bus->txq, pkt, prec)) {
706478cb 2702 skb_pull(pkt, bus->tx_hdrlen);
5e8149f5 2703 brcmf_err("out of bus->txq !!!\n");
5b435de0
AS
2704 ret = -ENOSR;
2705 } else {
2706 ret = 0;
2707 }
5b435de0 2708
c8bf3484 2709 if (pktq_len(&bus->txq) >= TXHI) {
90d03ff7
HM
2710 bus->txoff = true;
2711 brcmf_txflowblock(bus->sdiodev->dev, true);
c8bf3484 2712 }
4061f895 2713 spin_unlock_irqrestore(&bus->txqlock, flags);
5b435de0 2714
8ae74654 2715#ifdef DEBUG
5b435de0
AS
2716 if (pktq_plen(&bus->txq, prec) > qcount[prec])
2717 qcount[prec] = pktq_plen(&bus->txq, prec);
2718#endif
f1e68c2e 2719
fccfe930
AS
2720 if (atomic_read(&bus->dpc_tskcnt) == 0) {
2721 atomic_inc(&bus->dpc_tskcnt);
f1e68c2e 2722 queue_work(bus->brcmf_wq, &bus->datawork);
5b435de0
AS
2723 }
2724
2725 return ret;
2726}
2727
8ae74654 2728#ifdef DEBUG
5b435de0
AS
2729#define CONSOLE_LINE_MAX 192
2730
82d7f3c1 2731static int brcmf_sdio_readconsole(struct brcmf_sdio *bus)
5b435de0
AS
2732{
2733 struct brcmf_console *c = &bus->console;
2734 u8 line[CONSOLE_LINE_MAX], ch;
2735 u32 n, idx, addr;
2736 int rv;
2737
2738 /* Don't do anything until FWREADY updates console address */
2739 if (bus->console_addr == 0)
2740 return 0;
2741
2742 /* Read console log struct */
2743 addr = bus->console_addr + offsetof(struct rte_console, log_le);
a39be27b
AS
2744 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, (u8 *)&c->log_le,
2745 sizeof(c->log_le));
5b435de0
AS
2746 if (rv < 0)
2747 return rv;
2748
2749 /* Allocate console buffer (one time only) */
2750 if (c->buf == NULL) {
2751 c->bufsize = le32_to_cpu(c->log_le.buf_size);
2752 c->buf = kmalloc(c->bufsize, GFP_ATOMIC);
2753 if (c->buf == NULL)
2754 return -ENOMEM;
2755 }
2756
2757 idx = le32_to_cpu(c->log_le.idx);
2758
2759 /* Protect against corrupt value */
2760 if (idx > c->bufsize)
2761 return -EBADE;
2762
2763 /* Skip reading the console buffer if the index pointer
2764 has not moved */
2765 if (idx == c->last)
2766 return 0;
2767
2768 /* Read the console buffer */
2769 addr = le32_to_cpu(c->log_le.buf);
a39be27b 2770 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, c->buf, c->bufsize);
5b435de0
AS
2771 if (rv < 0)
2772 return rv;
2773
2774 while (c->last != idx) {
2775 for (n = 0; n < CONSOLE_LINE_MAX - 2; n++) {
2776 if (c->last == idx) {
2777 /* This would output a partial line.
2778 * Instead, back up
2779 * the buffer pointer and output this
2780 * line next time around.
2781 */
2782 if (c->last >= n)
2783 c->last -= n;
2784 else
2785 c->last = c->bufsize - n;
2786 goto break2;
2787 }
2788 ch = c->buf[c->last];
2789 c->last = (c->last + 1) % c->bufsize;
2790 if (ch == '\n')
2791 break;
2792 line[n] = ch;
2793 }
2794
2795 if (n > 0) {
2796 if (line[n - 1] == '\r')
2797 n--;
2798 line[n] = 0;
18aad4f8 2799 pr_debug("CONSOLE: %s\n", line);
5b435de0
AS
2800 }
2801 }
2802break2:
2803
2804 return 0;
2805}
8ae74654 2806#endif /* DEBUG */
5b435de0 2807
82d7f3c1 2808static int brcmf_sdio_tx_frame(struct brcmf_sdio *bus, u8 *frame, u16 len)
5b435de0
AS
2809{
2810 int i;
2811 int ret;
2812
2813 bus->ctrl_frame_stat = false;
a7cdd821 2814 ret = brcmf_sdiod_send_buf(bus->sdiodev, frame, len);
5b435de0
AS
2815
2816 if (ret < 0) {
2817 /* On failure, abort the command and terminate the frame */
2818 brcmf_dbg(INFO, "sdio error %d, abort command and terminate frame\n",
2819 ret);
80969836 2820 bus->sdcnt.tx_sderrs++;
5b435de0 2821
a39be27b 2822 brcmf_sdiod_abort(bus->sdiodev, SDIO_FUNC_2);
5b435de0 2823
a39be27b
AS
2824 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL,
2825 SFC_WF_TERM, NULL);
80969836 2826 bus->sdcnt.f1regdata++;
5b435de0
AS
2827
2828 for (i = 0; i < 3; i++) {
2829 u8 hi, lo;
a39be27b
AS
2830 hi = brcmf_sdiod_regrb(bus->sdiodev,
2831 SBSDIO_FUNC1_WFRAMEBCHI, NULL);
2832 lo = brcmf_sdiod_regrb(bus->sdiodev,
2833 SBSDIO_FUNC1_WFRAMEBCLO, NULL);
80969836 2834 bus->sdcnt.f1regdata += 2;
5b435de0
AS
2835 if (hi == 0 && lo == 0)
2836 break;
2837 }
2838 return ret;
2839 }
2840
6bc52319 2841 bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQ_WRAP;
5b435de0
AS
2842
2843 return ret;
2844}
2845
fcf094f4 2846static int
82d7f3c1 2847brcmf_sdio_bus_txctl(struct device *dev, unsigned char *msg, uint msglen)
5b435de0
AS
2848{
2849 u8 *frame;
8da9d2c8 2850 u16 len, pad;
5b435de0
AS
2851 uint retries = 0;
2852 u8 doff = 0;
2853 int ret = -1;
47a1ce78 2854 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
0a332e46 2855 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
47a1ce78 2856 struct brcmf_sdio *bus = sdiodev->bus;
6bc52319 2857 struct brcmf_sdio_hdrinfo hd_info = {0};
5b435de0
AS
2858
2859 brcmf_dbg(TRACE, "Enter\n");
2860
2861 /* Back the pointer to make a room for bus header */
706478cb
FL
2862 frame = msg - bus->tx_hdrlen;
2863 len = (msglen += bus->tx_hdrlen);
5b435de0
AS
2864
2865 /* Add alignment padding (optional for ctl frames) */
9b2d2f2a 2866 doff = ((unsigned long)frame % bus->head_align);
5b435de0
AS
2867 if (doff) {
2868 frame -= doff;
2869 len += doff;
2870 msglen += doff;
706478cb 2871 memset(frame, 0, doff + bus->tx_hdrlen);
5b435de0 2872 }
9b2d2f2a 2873 /* precondition: doff < bus->head_align */
706478cb 2874 doff += bus->tx_hdrlen;
5b435de0
AS
2875
2876 /* Round send length to next SDIO block */
8da9d2c8 2877 pad = 0;
5b435de0 2878 if (bus->roundup && bus->blocksize && (len > bus->blocksize)) {
8da9d2c8
FL
2879 pad = bus->blocksize - (len % bus->blocksize);
2880 if ((pad > bus->roundup) || (pad >= bus->blocksize))
2881 pad = 0;
9b2d2f2a
AS
2882 } else if (len % bus->head_align) {
2883 pad = bus->head_align - (len % bus->head_align);
5b435de0 2884 }
8da9d2c8 2885 len += pad;
5b435de0 2886
5b435de0
AS
2887 /* precondition: IS_ALIGNED((unsigned long)frame, 2) */
2888
5b435de0 2889 /* Make sure backplane clock is on */
38b0b0dd 2890 sdio_claim_host(bus->sdiodev->func[1]);
82d7f3c1 2891 brcmf_sdio_bus_sleep(bus, false, false);
38b0b0dd 2892 sdio_release_host(bus->sdiodev->func[1]);
5b435de0 2893
6bc52319
FL
2894 hd_info.len = (u16)msglen;
2895 hd_info.channel = SDPCM_CONTROL_CHANNEL;
2896 hd_info.dat_offset = doff;
8da9d2c8 2897 hd_info.seq_num = bus->tx_seq;
9b2d2f2a
AS
2898 hd_info.lastfrm = true;
2899 hd_info.tail_pad = pad;
6bc52319 2900 brcmf_sdio_hdpack(bus, frame, &hd_info);
5b435de0 2901
8da9d2c8
FL
2902 if (bus->txglom)
2903 brcmf_sdio_update_hwhdr(frame, len);
2904
5b435de0
AS
2905 if (!data_ok(bus)) {
2906 brcmf_dbg(INFO, "No bus credit bus->tx_max %d, bus->tx_seq %d\n",
2907 bus->tx_max, bus->tx_seq);
2908 bus->ctrl_frame_stat = true;
2909 /* Send from dpc */
2910 bus->ctrl_frame_buf = frame;
2911 bus->ctrl_frame_len = len;
2912
fd67dc83
FL
2913 wait_event_interruptible_timeout(bus->ctrl_wait,
2914 !bus->ctrl_frame_stat,
2915 msecs_to_jiffies(2000));
5b435de0 2916
23677ce3 2917 if (!bus->ctrl_frame_stat) {
c3203374 2918 brcmf_dbg(SDIO, "ctrl_frame_stat == false\n");
5b435de0
AS
2919 ret = 0;
2920 } else {
c3203374 2921 brcmf_dbg(SDIO, "ctrl_frame_stat == true\n");
5b435de0
AS
2922 ret = -1;
2923 }
2924 }
2925
2926 if (ret == -1) {
1e023829
JP
2927 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_CTL_ON(),
2928 frame, len, "Tx Frame:\n");
2929 brcmf_dbg_hex_dump(!(BRCMF_BYTES_ON() && BRCMF_CTL_ON()) &&
2930 BRCMF_HDRS_ON(),
2931 frame, min_t(u16, len, 16), "TxHdr:\n");
5b435de0
AS
2932
2933 do {
38b0b0dd 2934 sdio_claim_host(bus->sdiodev->func[1]);
82d7f3c1 2935 ret = brcmf_sdio_tx_frame(bus, frame, len);
38b0b0dd 2936 sdio_release_host(bus->sdiodev->func[1]);
5b435de0
AS
2937 } while (ret < 0 && retries++ < TXRETRIES);
2938 }
2939
5b435de0 2940 if (ret)
80969836 2941 bus->sdcnt.tx_ctlerrs++;
5b435de0 2942 else
80969836 2943 bus->sdcnt.tx_ctlpkts++;
5b435de0
AS
2944
2945 return ret ? -EIO : 0;
2946}
2947
80969836 2948#ifdef DEBUG
4fc0d016
AS
2949static int brcmf_sdio_dump_console(struct brcmf_sdio *bus,
2950 struct sdpcm_shared *sh, char __user *data,
2951 size_t count)
2952{
2953 u32 addr, console_ptr, console_size, console_index;
2954 char *conbuf = NULL;
2955 __le32 sh_val;
2956 int rv;
2957 loff_t pos = 0;
2958 int nbytes = 0;
2959
2960 /* obtain console information from device memory */
2961 addr = sh->console_addr + offsetof(struct rte_console, log_le);
a39be27b
AS
2962 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
2963 (u8 *)&sh_val, sizeof(u32));
4fc0d016
AS
2964 if (rv < 0)
2965 return rv;
2966 console_ptr = le32_to_cpu(sh_val);
2967
2968 addr = sh->console_addr + offsetof(struct rte_console, log_le.buf_size);
a39be27b
AS
2969 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
2970 (u8 *)&sh_val, sizeof(u32));
4fc0d016
AS
2971 if (rv < 0)
2972 return rv;
2973 console_size = le32_to_cpu(sh_val);
2974
2975 addr = sh->console_addr + offsetof(struct rte_console, log_le.idx);
a39be27b
AS
2976 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
2977 (u8 *)&sh_val, sizeof(u32));
4fc0d016
AS
2978 if (rv < 0)
2979 return rv;
2980 console_index = le32_to_cpu(sh_val);
2981
2982 /* allocate buffer for console data */
2983 if (console_size <= CONSOLE_BUFFER_MAX)
2984 conbuf = vzalloc(console_size+1);
2985
2986 if (!conbuf)
2987 return -ENOMEM;
2988
2989 /* obtain the console data from device */
2990 conbuf[console_size] = '\0';
a39be27b
AS
2991 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, console_ptr, (u8 *)conbuf,
2992 console_size);
4fc0d016
AS
2993 if (rv < 0)
2994 goto done;
2995
2996 rv = simple_read_from_buffer(data, count, &pos,
2997 conbuf + console_index,
2998 console_size - console_index);
2999 if (rv < 0)
3000 goto done;
3001
3002 nbytes = rv;
3003 if (console_index > 0) {
3004 pos = 0;
3005 rv = simple_read_from_buffer(data+nbytes, count, &pos,
3006 conbuf, console_index - 1);
3007 if (rv < 0)
3008 goto done;
3009 rv += nbytes;
3010 }
3011done:
3012 vfree(conbuf);
3013 return rv;
3014}
3015
3016static int brcmf_sdio_trap_info(struct brcmf_sdio *bus, struct sdpcm_shared *sh,
3017 char __user *data, size_t count)
3018{
3019 int error, res;
3020 char buf[350];
3021 struct brcmf_trap_info tr;
4fc0d016
AS
3022 loff_t pos = 0;
3023
baa9e609
PH
3024 if ((sh->flags & SDPCM_SHARED_TRAP) == 0) {
3025 brcmf_dbg(INFO, "no trap in firmware\n");
4fc0d016 3026 return 0;
baa9e609 3027 }
4fc0d016 3028
a39be27b
AS
3029 error = brcmf_sdiod_ramrw(bus->sdiodev, false, sh->trap_addr, (u8 *)&tr,
3030 sizeof(struct brcmf_trap_info));
4fc0d016
AS
3031 if (error < 0)
3032 return error;
3033
4fc0d016
AS
3034 res = scnprintf(buf, sizeof(buf),
3035 "dongle trap info: type 0x%x @ epc 0x%08x\n"
3036 " cpsr 0x%08x spsr 0x%08x sp 0x%08x\n"
3037 " lr 0x%08x pc 0x%08x offset 0x%x\n"
3038 " r0 0x%08x r1 0x%08x r2 0x%08x r3 0x%08x\n"
3039 " r4 0x%08x r5 0x%08x r6 0x%08x r7 0x%08x\n",
3040 le32_to_cpu(tr.type), le32_to_cpu(tr.epc),
3041 le32_to_cpu(tr.cpsr), le32_to_cpu(tr.spsr),
3042 le32_to_cpu(tr.r13), le32_to_cpu(tr.r14),
9bd02c6b 3043 le32_to_cpu(tr.pc), sh->trap_addr,
4fc0d016
AS
3044 le32_to_cpu(tr.r0), le32_to_cpu(tr.r1),
3045 le32_to_cpu(tr.r2), le32_to_cpu(tr.r3),
3046 le32_to_cpu(tr.r4), le32_to_cpu(tr.r5),
3047 le32_to_cpu(tr.r6), le32_to_cpu(tr.r7));
3048
baa9e609 3049 return simple_read_from_buffer(data, count, &pos, buf, res);
4fc0d016
AS
3050}
3051
3052static int brcmf_sdio_assert_info(struct brcmf_sdio *bus,
3053 struct sdpcm_shared *sh, char __user *data,
3054 size_t count)
3055{
3056 int error = 0;
3057 char buf[200];
3058 char file[80] = "?";
3059 char expr[80] = "<???>";
3060 int res;
3061 loff_t pos = 0;
3062
3063 if ((sh->flags & SDPCM_SHARED_ASSERT_BUILT) == 0) {
3064 brcmf_dbg(INFO, "firmware not built with -assert\n");
3065 return 0;
3066 } else if ((sh->flags & SDPCM_SHARED_ASSERT) == 0) {
3067 brcmf_dbg(INFO, "no assert in dongle\n");
3068 return 0;
3069 }
3070
38b0b0dd 3071 sdio_claim_host(bus->sdiodev->func[1]);
4fc0d016 3072 if (sh->assert_file_addr != 0) {
a39be27b
AS
3073 error = brcmf_sdiod_ramrw(bus->sdiodev, false,
3074 sh->assert_file_addr, (u8 *)file, 80);
4fc0d016
AS
3075 if (error < 0)
3076 return error;
3077 }
3078 if (sh->assert_exp_addr != 0) {
a39be27b
AS
3079 error = brcmf_sdiod_ramrw(bus->sdiodev, false,
3080 sh->assert_exp_addr, (u8 *)expr, 80);
4fc0d016
AS
3081 if (error < 0)
3082 return error;
3083 }
38b0b0dd 3084 sdio_release_host(bus->sdiodev->func[1]);
4fc0d016
AS
3085
3086 res = scnprintf(buf, sizeof(buf),
3087 "dongle assert: %s:%d: assert(%s)\n",
3088 file, sh->assert_line, expr);
3089 return simple_read_from_buffer(data, count, &pos, buf, res);
3090}
3091
82d7f3c1 3092static int brcmf_sdio_checkdied(struct brcmf_sdio *bus)
4fc0d016
AS
3093{
3094 int error;
3095 struct sdpcm_shared sh;
3096
4fc0d016 3097 error = brcmf_sdio_readshared(bus, &sh);
4fc0d016
AS
3098
3099 if (error < 0)
3100 return error;
3101
3102 if ((sh.flags & SDPCM_SHARED_ASSERT_BUILT) == 0)
3103 brcmf_dbg(INFO, "firmware not built with -assert\n");
3104 else if (sh.flags & SDPCM_SHARED_ASSERT)
5e8149f5 3105 brcmf_err("assertion in dongle\n");
4fc0d016
AS
3106
3107 if (sh.flags & SDPCM_SHARED_TRAP)
5e8149f5 3108 brcmf_err("firmware trap in dongle\n");
4fc0d016
AS
3109
3110 return 0;
3111}
3112
82d7f3c1
AS
3113static int brcmf_sdio_died_dump(struct brcmf_sdio *bus, char __user *data,
3114 size_t count, loff_t *ppos)
4fc0d016
AS
3115{
3116 int error = 0;
3117 struct sdpcm_shared sh;
3118 int nbytes = 0;
3119 loff_t pos = *ppos;
3120
3121 if (pos != 0)
3122 return 0;
3123
4fc0d016
AS
3124 error = brcmf_sdio_readshared(bus, &sh);
3125 if (error < 0)
3126 goto done;
3127
3128 error = brcmf_sdio_assert_info(bus, &sh, data, count);
3129 if (error < 0)
3130 goto done;
4fc0d016 3131 nbytes = error;
baa9e609
PH
3132
3133 error = brcmf_sdio_trap_info(bus, &sh, data+nbytes, count);
4fc0d016
AS
3134 if (error < 0)
3135 goto done;
baa9e609
PH
3136 nbytes += error;
3137
3138 error = brcmf_sdio_dump_console(bus, &sh, data+nbytes, count);
3139 if (error < 0)
3140 goto done;
3141 nbytes += error;
4fc0d016 3142
baa9e609
PH
3143 error = nbytes;
3144 *ppos += nbytes;
4fc0d016 3145done:
4fc0d016
AS
3146 return error;
3147}
3148
3149static ssize_t brcmf_sdio_forensic_read(struct file *f, char __user *data,
3150 size_t count, loff_t *ppos)
3151{
3152 struct brcmf_sdio *bus = f->private_data;
3153 int res;
3154
82d7f3c1 3155 res = brcmf_sdio_died_dump(bus, data, count, ppos);
4fc0d016
AS
3156 if (res > 0)
3157 *ppos += res;
3158 return (ssize_t)res;
3159}
3160
3161static const struct file_operations brcmf_sdio_forensic_ops = {
3162 .owner = THIS_MODULE,
3163 .open = simple_open,
3164 .read = brcmf_sdio_forensic_read
3165};
3166
80969836
AS
3167static void brcmf_sdio_debugfs_create(struct brcmf_sdio *bus)
3168{
3169 struct brcmf_pub *drvr = bus->sdiodev->bus_if->drvr;
4fc0d016 3170 struct dentry *dentry = brcmf_debugfs_get_devdir(drvr);
80969836 3171
4fc0d016
AS
3172 if (IS_ERR_OR_NULL(dentry))
3173 return;
3174
3175 debugfs_create_file("forensics", S_IRUGO, dentry, bus,
3176 &brcmf_sdio_forensic_ops);
80969836 3177 brcmf_debugfs_create_sdio_count(drvr, &bus->sdcnt);
0801e6c5
DK
3178 debugfs_create_u32("console_interval", 0644, dentry,
3179 &bus->console_interval);
80969836
AS
3180}
3181#else
82d7f3c1 3182static int brcmf_sdio_checkdied(struct brcmf_sdio *bus)
4fc0d016
AS
3183{
3184 return 0;
3185}
3186
80969836
AS
3187static void brcmf_sdio_debugfs_create(struct brcmf_sdio *bus)
3188{
3189}
3190#endif /* DEBUG */
3191
fcf094f4 3192static int
82d7f3c1 3193brcmf_sdio_bus_rxctl(struct device *dev, unsigned char *msg, uint msglen)
5b435de0
AS
3194{
3195 int timeleft;
3196 uint rxlen = 0;
3197 bool pending;
dd43a01c 3198 u8 *buf;
532cdd3b 3199 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
0a332e46 3200 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
532cdd3b 3201 struct brcmf_sdio *bus = sdiodev->bus;
5b435de0
AS
3202
3203 brcmf_dbg(TRACE, "Enter\n");
3204
3205 /* Wait until control frame is available */
82d7f3c1 3206 timeleft = brcmf_sdio_dcmd_resp_wait(bus, &bus->rxlen, &pending);
5b435de0 3207
dd43a01c 3208 spin_lock_bh(&bus->rxctl_lock);
5b435de0
AS
3209 rxlen = bus->rxlen;
3210 memcpy(msg, bus->rxctl, min(msglen, rxlen));
dd43a01c
FL
3211 bus->rxctl = NULL;
3212 buf = bus->rxctl_orig;
3213 bus->rxctl_orig = NULL;
5b435de0 3214 bus->rxlen = 0;
dd43a01c
FL
3215 spin_unlock_bh(&bus->rxctl_lock);
3216 vfree(buf);
5b435de0
AS
3217
3218 if (rxlen) {
3219 brcmf_dbg(CTL, "resumed on rxctl frame, got %d expected %d\n",
3220 rxlen, msglen);
3221 } else if (timeleft == 0) {
5e8149f5 3222 brcmf_err("resumed on timeout\n");
82d7f3c1 3223 brcmf_sdio_checkdied(bus);
23677ce3 3224 } else if (pending) {
5b435de0
AS
3225 brcmf_dbg(CTL, "cancelled\n");
3226 return -ERESTARTSYS;
3227 } else {
3228 brcmf_dbg(CTL, "resumed for unknown reason?\n");
82d7f3c1 3229 brcmf_sdio_checkdied(bus);
5b435de0
AS
3230 }
3231
3232 if (rxlen)
80969836 3233 bus->sdcnt.rx_ctlpkts++;
5b435de0 3234 else
80969836 3235 bus->sdcnt.rx_ctlerrs++;
5b435de0
AS
3236
3237 return rxlen ? (int)rxlen : -ETIMEDOUT;
3238}
3239
a74d036f
HM
3240#ifdef DEBUG
3241static bool
3242brcmf_sdio_verifymemory(struct brcmf_sdio_dev *sdiodev, u32 ram_addr,
3243 u8 *ram_data, uint ram_sz)
3244{
3245 char *ram_cmp;
3246 int err;
3247 bool ret = true;
3248 int address;
3249 int offset;
3250 int len;
3251
3252 /* read back and verify */
3253 brcmf_dbg(INFO, "Compare RAM dl & ul at 0x%08x; size=%d\n", ram_addr,
3254 ram_sz);
3255 ram_cmp = kmalloc(MEMBLOCK, GFP_KERNEL);
3256 /* do not proceed while no memory but */
3257 if (!ram_cmp)
3258 return true;
3259
3260 address = ram_addr;
3261 offset = 0;
3262 while (offset < ram_sz) {
3263 len = ((offset + MEMBLOCK) < ram_sz) ? MEMBLOCK :
3264 ram_sz - offset;
3265 err = brcmf_sdiod_ramrw(sdiodev, false, address, ram_cmp, len);
3266 if (err) {
3267 brcmf_err("error %d on reading %d membytes at 0x%08x\n",
3268 err, len, address);
3269 ret = false;
3270 break;
3271 } else if (memcmp(ram_cmp, &ram_data[offset], len)) {
3272 brcmf_err("Downloaded RAM image is corrupted, block offset is %d, len is %d\n",
3273 offset, len);
3274 ret = false;
3275 break;
3276 }
3277 offset += len;
3278 address += len;
3279 }
3280
3281 kfree(ram_cmp);
3282
3283 return ret;
3284}
3285#else /* DEBUG */
3286static bool
3287brcmf_sdio_verifymemory(struct brcmf_sdio_dev *sdiodev, u32 ram_addr,
3288 u8 *ram_data, uint ram_sz)
3289{
3290 return true;
3291}
3292#endif /* DEBUG */
3293
3355650c
AS
3294static int brcmf_sdio_download_code_file(struct brcmf_sdio *bus,
3295 const struct firmware *fw)
5b435de0 3296{
f2c44fe7 3297 int err;
f2c44fe7 3298
a74d036f
HM
3299 brcmf_dbg(TRACE, "Enter\n");
3300
f9951c13
HM
3301 err = brcmf_sdiod_ramrw(bus->sdiodev, true, bus->ci->rambase,
3302 (u8 *)fw->data, fw->size);
3303 if (err)
3304 brcmf_err("error %d on writing %d membytes at 0x%08x\n",
3305 err, (int)fw->size, bus->ci->rambase);
3306 else if (!brcmf_sdio_verifymemory(bus->sdiodev, bus->ci->rambase,
3307 (u8 *)fw->data, fw->size))
3308 err = -EIO;
5b435de0 3309
f2c44fe7 3310 return err;
5b435de0
AS
3311}
3312
3355650c
AS
3313static int brcmf_sdio_download_nvram(struct brcmf_sdio *bus,
3314 const struct firmware *nv)
5b435de0 3315{
a74d036f
HM
3316 void *vars;
3317 u32 varsz;
3318 int address;
3319 int err;
3320
3321 brcmf_dbg(TRACE, "Enter\n");
5b435de0 3322
a74d036f 3323 vars = brcmf_nvram_strip(nv, &varsz);
5b435de0 3324
a74d036f
HM
3325 if (vars == NULL)
3326 return -EINVAL;
3327
3328 address = bus->ci->ramsize - varsz + bus->ci->rambase;
3329 err = brcmf_sdiod_ramrw(bus->sdiodev, true, address, vars, varsz);
3330 if (err)
3331 brcmf_err("error %d on writing %d nvram bytes at 0x%08x\n",
3332 err, varsz, address);
3333 else if (!brcmf_sdio_verifymemory(bus->sdiodev, address, vars, varsz))
3334 err = -EIO;
3335
3336 brcmf_nvram_free(vars);
3337
3338 return err;
5b435de0
AS
3339}
3340
82d7f3c1 3341static int brcmf_sdio_download_firmware(struct brcmf_sdio *bus)
5b435de0 3342{
82d7f3c1 3343 int bcmerror = -EFAULT;
3355650c
AS
3344 const struct firmware *fw;
3345 u32 rstvec;
82d7f3c1
AS
3346
3347 sdio_claim_host(bus->sdiodev->func[1]);
3348 brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
5b435de0
AS
3349
3350 /* Keep arm in reset */
cb7cf7be 3351 brcmf_chip_enter_download(bus->ci);
3355650c
AS
3352
3353 fw = brcmf_sdio_get_fw(bus, BRCMF_FIRMWARE_BIN);
3354 if (fw == NULL) {
3355 bcmerror = -ENOENT;
5b435de0
AS
3356 goto err;
3357 }
3358
3355650c
AS
3359 rstvec = get_unaligned_le32(fw->data);
3360 brcmf_dbg(SDIO, "firmware rstvec: %x\n", rstvec);
3361
3362 bcmerror = brcmf_sdio_download_code_file(bus, fw);
3363 release_firmware(fw);
3364 if (bcmerror) {
5e8149f5 3365 brcmf_err("dongle image file download failed\n");
5b435de0
AS
3366 goto err;
3367 }
3368
3355650c
AS
3369 fw = brcmf_sdio_get_fw(bus, BRCMF_FIRMWARE_NVRAM);
3370 if (fw == NULL) {
3371 bcmerror = -ENOENT;
3372 goto err;
3373 }
3374
3375 bcmerror = brcmf_sdio_download_nvram(bus, fw);
3376 release_firmware(fw);
3377 if (bcmerror) {
5e8149f5 3378 brcmf_err("dongle nvram file download failed\n");
3eaa956c
FL
3379 goto err;
3380 }
5b435de0
AS
3381
3382 /* Take arm out of reset */
cb7cf7be 3383 if (!brcmf_chip_exit_download(bus->ci, rstvec)) {
5e8149f5 3384 brcmf_err("error getting out of ARM core reset\n");
5b435de0
AS
3385 goto err;
3386 }
3387
3355650c 3388 /* Allow HT Clock now that the ARM is running. */
bb350711 3389 brcmf_bus_change_state(bus->sdiodev->bus_if, BRCMF_BUS_LOAD);
5b435de0
AS
3390 bcmerror = 0;
3391
3392err:
82d7f3c1
AS
3393 brcmf_sdio_clkctl(bus, CLK_SDONLY, false);
3394 sdio_release_host(bus->sdiodev->func[1]);
5b435de0
AS
3395 return bcmerror;
3396}
3397
82d7f3c1 3398static void brcmf_sdio_sr_init(struct brcmf_sdio *bus)
4a3da990
PH
3399{
3400 int err = 0;
3401 u8 val;
3402
3403 brcmf_dbg(TRACE, "Enter\n");
3404
a39be27b 3405 val = brcmf_sdiod_regrb(bus->sdiodev, SBSDIO_FUNC1_WAKEUPCTRL, &err);
4a3da990
PH
3406 if (err) {
3407 brcmf_err("error reading SBSDIO_FUNC1_WAKEUPCTRL\n");
3408 return;
3409 }
3410
3411 val |= 1 << SBSDIO_FUNC1_WCTRL_HTWAIT_SHIFT;
a39be27b 3412 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_WAKEUPCTRL, val, &err);
4a3da990
PH
3413 if (err) {
3414 brcmf_err("error writing SBSDIO_FUNC1_WAKEUPCTRL\n");
3415 return;
3416 }
3417
3418 /* Add CMD14 Support */
a39be27b
AS
3419 brcmf_sdiod_regwb(bus->sdiodev, SDIO_CCCR_BRCM_CARDCAP,
3420 (SDIO_CCCR_BRCM_CARDCAP_CMD14_SUPPORT |
3421 SDIO_CCCR_BRCM_CARDCAP_CMD14_EXT),
3422 &err);
4a3da990
PH
3423 if (err) {
3424 brcmf_err("error writing SDIO_CCCR_BRCM_CARDCAP\n");
3425 return;
3426 }
3427
a39be27b
AS
3428 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3429 SBSDIO_FORCE_HT, &err);
4a3da990
PH
3430 if (err) {
3431 brcmf_err("error writing SBSDIO_FUNC1_CHIPCLKCSR\n");
3432 return;
3433 }
3434
3435 /* set flag */
3436 bus->sr_enabled = true;
3437 brcmf_dbg(INFO, "SR enabled\n");
3438}
3439
3440/* enable KSO bit */
82d7f3c1 3441static int brcmf_sdio_kso_init(struct brcmf_sdio *bus)
4a3da990
PH
3442{
3443 u8 val;
3444 int err = 0;
3445
3446 brcmf_dbg(TRACE, "Enter\n");
3447
3448 /* KSO bit added in SDIO core rev 12 */
cb7cf7be 3449 if (brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV)->rev < 12)
4a3da990
PH
3450 return 0;
3451
a39be27b 3452 val = brcmf_sdiod_regrb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR, &err);
4a3da990
PH
3453 if (err) {
3454 brcmf_err("error reading SBSDIO_FUNC1_SLEEPCSR\n");
3455 return err;
3456 }
3457
3458 if (!(val & SBSDIO_FUNC1_SLEEPCSR_KSO_MASK)) {
3459 val |= (SBSDIO_FUNC1_SLEEPCSR_KSO_EN <<
3460 SBSDIO_FUNC1_SLEEPCSR_KSO_SHIFT);
a39be27b
AS
3461 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
3462 val, &err);
4a3da990
PH
3463 if (err) {
3464 brcmf_err("error writing SBSDIO_FUNC1_SLEEPCSR\n");
3465 return err;
3466 }
3467 }
3468
3469 return 0;
3470}
3471
3472
82d7f3c1 3473static int brcmf_sdio_bus_preinit(struct device *dev)
cf458287
AS
3474{
3475 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3476 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3477 struct brcmf_sdio *bus = sdiodev->bus;
8da9d2c8 3478 uint pad_size;
cf458287 3479 u32 value;
cf458287
AS
3480 int err;
3481
8da9d2c8
FL
3482 /* the commands below use the terms tx and rx from
3483 * a device perspective, ie. bus:txglom affects the
3484 * bus transfers from device to host.
3485 */
cb7cf7be 3486 if (brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV)->rev < 12) {
cf458287
AS
3487 /* for sdio core rev < 12, disable txgloming */
3488 value = 0;
3489 err = brcmf_iovar_data_set(dev, "bus:txglom", &value,
3490 sizeof(u32));
3491 } else {
3492 /* otherwise, set txglomalign */
3493 value = 4;
3494 if (sdiodev->pdata)
3495 value = sdiodev->pdata->sd_sgentry_align;
3496 /* SDIO ADMA requires at least 32 bit alignment */
3497 value = max_t(u32, value, 4);
3498 err = brcmf_iovar_data_set(dev, "bus:txglomalign", &value,
3499 sizeof(u32));
3500 }
8da9d2c8
FL
3501
3502 if (err < 0)
3503 goto done;
3504
3505 bus->tx_hdrlen = SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN;
3506 if (sdiodev->sg_support) {
3507 bus->txglom = false;
3508 value = 1;
3509 pad_size = bus->sdiodev->func[2]->cur_blksize << 1;
3510 bus->txglom_sgpad = brcmu_pkt_buf_get_skb(pad_size);
3511 if (!bus->txglom_sgpad)
3512 brcmf_err("allocating txglom padding skb failed, reduced performance\n");
3513
3514 err = brcmf_iovar_data_set(bus->sdiodev->dev, "bus:rxglom",
3515 &value, sizeof(u32));
3516 if (err < 0) {
3517 /* bus:rxglom is allowed to fail */
3518 err = 0;
3519 } else {
3520 bus->txglom = true;
3521 bus->tx_hdrlen += SDPCM_HWEXT_LEN;
3522 }
3523 }
3524 brcmf_bus_add_txhdrlen(bus->sdiodev->dev, bus->tx_hdrlen);
3525
3526done:
cf458287
AS
3527 return err;
3528}
3529
82d7f3c1 3530static int brcmf_sdio_bus_init(struct device *dev)
5b435de0 3531{
fa20b911 3532 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
0a332e46 3533 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
fa20b911 3534 struct brcmf_sdio *bus = sdiodev->bus;
5b435de0
AS
3535 int err, ret = 0;
3536 u8 saveclk;
3537
3538 brcmf_dbg(TRACE, "Enter\n");
3539
3540 /* try to download image and nvram to the dongle */
fa20b911 3541 if (bus_if->state == BRCMF_BUS_DOWN) {
3355650c 3542 bus->alp_only = true;
82d7f3c1
AS
3543 err = brcmf_sdio_download_firmware(bus);
3544 if (err)
3545 return err;
3355650c 3546 bus->alp_only = false;
5b435de0
AS
3547 }
3548
712ac5b3 3549 if (!bus->sdiodev->bus_if->drvr)
5b435de0
AS
3550 return 0;
3551
3552 /* Start the watchdog timer */
80969836 3553 bus->sdcnt.tickcnt = 0;
82d7f3c1 3554 brcmf_sdio_wd_timer(bus, BRCMF_WD_POLL_MS);
5b435de0 3555
38b0b0dd 3556 sdio_claim_host(bus->sdiodev->func[1]);
5b435de0
AS
3557
3558 /* Make sure backplane clock is on, needed to generate F2 interrupt */
82d7f3c1 3559 brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
5b435de0
AS
3560 if (bus->clkstate != CLK_AVAIL)
3561 goto exit;
3562
3563 /* Force clocks on backplane to be sure F2 interrupt propagates */
a39be27b
AS
3564 saveclk = brcmf_sdiod_regrb(bus->sdiodev,
3565 SBSDIO_FUNC1_CHIPCLKCSR, &err);
5b435de0 3566 if (!err) {
a39be27b
AS
3567 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3568 (saveclk | SBSDIO_FORCE_HT), &err);
5b435de0
AS
3569 }
3570 if (err) {
5e8149f5 3571 brcmf_err("Failed to force clock for F2: err %d\n", err);
5b435de0
AS
3572 goto exit;
3573 }
3574
3575 /* Enable function 2 (frame transfers) */
3576 w_sdreg32(bus, SDPCM_PROT_VERSION << SMB_DATA_VERSION_SHIFT,
58692750 3577 offsetof(struct sdpcmd_regs, tosbmailboxdata));
71370eb8 3578 err = sdio_enable_func(bus->sdiodev->func[SDIO_FUNC_2]);
5b435de0 3579
5b435de0 3580
71370eb8 3581 brcmf_dbg(INFO, "enable F2: err=%d\n", err);
5b435de0
AS
3582
3583 /* If F2 successfully enabled, set core and enable interrupts */
71370eb8 3584 if (!err) {
5b435de0
AS
3585 /* Set up the interrupt mask and enable interrupts */
3586 bus->hostintmask = HOSTINTMASK;
3587 w_sdreg32(bus, bus->hostintmask,
58692750 3588 offsetof(struct sdpcmd_regs, hostintmask));
5b435de0 3589
a39be27b 3590 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_WATERMARK, 8, &err);
c0e89f08 3591 } else {
5b435de0 3592 /* Disable F2 again */
71370eb8 3593 sdio_disable_func(bus->sdiodev->func[SDIO_FUNC_2]);
c0e89f08 3594 ret = -ENODEV;
5b435de0
AS
3595 }
3596
cb7cf7be 3597 if (brcmf_chip_sr_capable(bus->ci)) {
82d7f3c1 3598 brcmf_sdio_sr_init(bus);
4a3da990
PH
3599 } else {
3600 /* Restore previous clock setting */
a39be27b
AS
3601 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3602 saveclk, &err);
4a3da990 3603 }
5b435de0 3604
e2f93cc3 3605 if (ret == 0) {
a39be27b 3606 ret = brcmf_sdiod_intr_register(bus->sdiodev);
e2f93cc3 3607 if (ret != 0)
5e8149f5 3608 brcmf_err("intr register failed:%d\n", ret);
e2f93cc3
FL
3609 }
3610
5b435de0 3611 /* If we didn't come up, turn off backplane clock */
76a4c681 3612 if (ret != 0)
82d7f3c1 3613 brcmf_sdio_clkctl(bus, CLK_NONE, false);
5b435de0
AS
3614
3615exit:
38b0b0dd 3616 sdio_release_host(bus->sdiodev->func[1]);
5b435de0
AS
3617
3618 return ret;
3619}
3620
82d7f3c1 3621void brcmf_sdio_isr(struct brcmf_sdio *bus)
5b435de0 3622{
5b435de0
AS
3623 brcmf_dbg(TRACE, "Enter\n");
3624
3625 if (!bus) {
5e8149f5 3626 brcmf_err("bus is null pointer, exiting\n");
5b435de0
AS
3627 return;
3628 }
3629
bb350711 3630 if (!brcmf_bus_ready(bus->sdiodev->bus_if)) {
5e8149f5 3631 brcmf_err("bus is down. we have nothing to do\n");
5b435de0
AS
3632 return;
3633 }
3634 /* Count the interrupt call */
80969836 3635 bus->sdcnt.intrcount++;
4531603a
FL
3636 if (in_interrupt())
3637 atomic_set(&bus->ipend, 1);
3638 else
3639 if (brcmf_sdio_intr_rstatus(bus)) {
5e8149f5 3640 brcmf_err("failed backplane access\n");
4531603a 3641 }
5b435de0 3642
5b435de0
AS
3643 /* Disable additional interrupts (is this needed now)? */
3644 if (!bus->intr)
5e8149f5 3645 brcmf_err("isr w/o interrupt configured!\n");
5b435de0 3646
fccfe930 3647 atomic_inc(&bus->dpc_tskcnt);
f1e68c2e 3648 queue_work(bus->brcmf_wq, &bus->datawork);
5b435de0
AS
3649}
3650
82d7f3c1 3651static bool brcmf_sdio_bus_watchdog(struct brcmf_sdio *bus)
5b435de0 3652{
8ae74654 3653#ifdef DEBUG
cad2b26b 3654 struct brcmf_bus *bus_if = dev_get_drvdata(bus->sdiodev->dev);
8ae74654 3655#endif /* DEBUG */
5b435de0
AS
3656
3657 brcmf_dbg(TIMER, "Enter\n");
3658
5b435de0 3659 /* Poll period: check device if appropriate. */
4a3da990
PH
3660 if (!bus->sr_enabled &&
3661 bus->poll && (++bus->polltick >= bus->pollrate)) {
5b435de0
AS
3662 u32 intstatus = 0;
3663
3664 /* Reset poll tick */
3665 bus->polltick = 0;
3666
3667 /* Check device if no interrupts */
80969836
AS
3668 if (!bus->intr ||
3669 (bus->sdcnt.intrcount == bus->sdcnt.lastintrs)) {
5b435de0 3670
fccfe930 3671 if (atomic_read(&bus->dpc_tskcnt) == 0) {
5b435de0 3672 u8 devpend;
fccfe930 3673
38b0b0dd 3674 sdio_claim_host(bus->sdiodev->func[1]);
a39be27b
AS
3675 devpend = brcmf_sdiod_regrb(bus->sdiodev,
3676 SDIO_CCCR_INTx,
3677 NULL);
38b0b0dd 3678 sdio_release_host(bus->sdiodev->func[1]);
5b435de0
AS
3679 intstatus =
3680 devpend & (INTR_STATUS_FUNC1 |
3681 INTR_STATUS_FUNC2);
3682 }
3683
3684 /* If there is something, make like the ISR and
3685 schedule the DPC */
3686 if (intstatus) {
80969836 3687 bus->sdcnt.pollcnt++;
1d382273 3688 atomic_set(&bus->ipend, 1);
5b435de0 3689
fccfe930 3690 atomic_inc(&bus->dpc_tskcnt);
f1e68c2e 3691 queue_work(bus->brcmf_wq, &bus->datawork);
5b435de0
AS
3692 }
3693 }
3694
3695 /* Update interrupt tracking */
80969836 3696 bus->sdcnt.lastintrs = bus->sdcnt.intrcount;
5b435de0 3697 }
8ae74654 3698#ifdef DEBUG
5b435de0 3699 /* Poll for console output periodically */
2def5c10 3700 if (bus_if && bus_if->state == BRCMF_BUS_DATA &&
8d169aa0 3701 bus->console_interval != 0) {
5b435de0
AS
3702 bus->console.count += BRCMF_WD_POLL_MS;
3703 if (bus->console.count >= bus->console_interval) {
3704 bus->console.count -= bus->console_interval;
38b0b0dd 3705 sdio_claim_host(bus->sdiodev->func[1]);
5b435de0 3706 /* Make sure backplane clock is on */
82d7f3c1
AS
3707 brcmf_sdio_bus_sleep(bus, false, false);
3708 if (brcmf_sdio_readconsole(bus) < 0)
5b435de0
AS
3709 /* stop on error */
3710 bus->console_interval = 0;
38b0b0dd 3711 sdio_release_host(bus->sdiodev->func[1]);
5b435de0
AS
3712 }
3713 }
8ae74654 3714#endif /* DEBUG */
5b435de0
AS
3715
3716 /* On idle timeout clear activity flag and/or turn off clock */
3717 if ((bus->idletime > 0) && (bus->clkstate == CLK_AVAIL)) {
3718 if (++bus->idlecount >= bus->idletime) {
3719 bus->idlecount = 0;
3720 if (bus->activity) {
3721 bus->activity = false;
82d7f3c1 3722 brcmf_sdio_wd_timer(bus, BRCMF_WD_POLL_MS);
5b435de0 3723 } else {
4a3da990 3724 brcmf_dbg(SDIO, "idle\n");
38b0b0dd 3725 sdio_claim_host(bus->sdiodev->func[1]);
82d7f3c1 3726 brcmf_sdio_bus_sleep(bus, true, false);
38b0b0dd 3727 sdio_release_host(bus->sdiodev->func[1]);
5b435de0
AS
3728 }
3729 }
3730 }
3731
1d382273 3732 return (atomic_read(&bus->ipend) > 0);
5b435de0
AS
3733}
3734
f1e68c2e
FL
3735static void brcmf_sdio_dataworker(struct work_struct *work)
3736{
3737 struct brcmf_sdio *bus = container_of(work, struct brcmf_sdio,
3738 datawork);
f1e68c2e 3739
fccfe930 3740 while (atomic_read(&bus->dpc_tskcnt)) {
71abdc00 3741 atomic_set(&bus->dpc_tskcnt, 0);
82d7f3c1 3742 brcmf_sdio_dpc(bus);
f1e68c2e 3743 }
f1e68c2e
FL
3744}
3745
65d80d0b
AS
3746static void
3747brcmf_sdio_drivestrengthinit(struct brcmf_sdio_dev *sdiodev,
cb7cf7be 3748 struct brcmf_chip *ci, u32 drivestrength)
65d80d0b
AS
3749{
3750 const struct sdiod_drive_str *str_tab = NULL;
3751 u32 str_mask;
3752 u32 str_shift;
cb7cf7be 3753 u32 base;
65d80d0b
AS
3754 u32 i;
3755 u32 drivestrength_sel = 0;
3756 u32 cc_data_temp;
3757 u32 addr;
3758
cb7cf7be 3759 if (!(ci->cc_caps & CC_CAP_PMU))
65d80d0b
AS
3760 return;
3761
3762 switch (SDIOD_DRVSTR_KEY(ci->chip, ci->pmurev)) {
3763 case SDIOD_DRVSTR_KEY(BCM4330_CHIP_ID, 12):
3764 str_tab = sdiod_drvstr_tab1_1v8;
3765 str_mask = 0x00003800;
3766 str_shift = 11;
3767 break;
3768 case SDIOD_DRVSTR_KEY(BCM4334_CHIP_ID, 17):
3769 str_tab = sdiod_drvstr_tab6_1v8;
3770 str_mask = 0x00001800;
3771 str_shift = 11;
3772 break;
3773 case SDIOD_DRVSTR_KEY(BCM43143_CHIP_ID, 17):
3774 /* note: 43143 does not support tristate */
3775 i = ARRAY_SIZE(sdiod_drvstr_tab2_3v3) - 1;
3776 if (drivestrength >= sdiod_drvstr_tab2_3v3[i].strength) {
3777 str_tab = sdiod_drvstr_tab2_3v3;
3778 str_mask = 0x00000007;
3779 str_shift = 0;
3780 } else
3781 brcmf_err("Invalid SDIO Drive strength for chip %s, strength=%d\n",
cb7cf7be 3782 ci->name, drivestrength);
65d80d0b
AS
3783 break;
3784 case SDIOD_DRVSTR_KEY(BCM43362_CHIP_ID, 13):
3785 str_tab = sdiod_drive_strength_tab5_1v8;
3786 str_mask = 0x00003800;
3787 str_shift = 11;
3788 break;
3789 default:
3790 brcmf_err("No SDIO Drive strength init done for chip %s rev %d pmurev %d\n",
cb7cf7be 3791 ci->name, ci->chiprev, ci->pmurev);
65d80d0b
AS
3792 break;
3793 }
3794
3795 if (str_tab != NULL) {
3796 for (i = 0; str_tab[i].strength != 0; i++) {
3797 if (drivestrength >= str_tab[i].strength) {
3798 drivestrength_sel = str_tab[i].sel;
3799 break;
3800 }
3801 }
cb7cf7be 3802 base = brcmf_chip_get_chipcommon(ci)->base;
65d80d0b
AS
3803 addr = CORE_CC_REG(base, chipcontrol_addr);
3804 brcmf_sdiod_regwl(sdiodev, addr, 1, NULL);
3805 cc_data_temp = brcmf_sdiod_regrl(sdiodev, addr, NULL);
3806 cc_data_temp &= ~str_mask;
3807 drivestrength_sel <<= str_shift;
3808 cc_data_temp |= drivestrength_sel;
3809 brcmf_sdiod_regwl(sdiodev, addr, cc_data_temp, NULL);
3810
3811 brcmf_dbg(INFO, "SDIO: %d mA (req=%d mA) drive strength selected, set to 0x%08x\n",
3812 str_tab[i].strength, drivestrength, cc_data_temp);
3813 }
3814}
3815
cb7cf7be 3816static int brcmf_sdio_buscoreprep(void *ctx)
65d80d0b 3817{
cb7cf7be 3818 struct brcmf_sdio_dev *sdiodev = ctx;
65d80d0b
AS
3819 int err = 0;
3820 u8 clkval, clkset;
3821
3822 /* Try forcing SDIO core to do ALPAvail request only */
3823 clkset = SBSDIO_FORCE_HW_CLKREQ_OFF | SBSDIO_ALP_AVAIL_REQ;
3824 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, clkset, &err);
3825 if (err) {
3826 brcmf_err("error writing for HT off\n");
3827 return err;
3828 }
3829
3830 /* If register supported, wait for ALPAvail and then force ALP */
3831 /* This may take up to 15 milliseconds */
3832 clkval = brcmf_sdiod_regrb(sdiodev,
3833 SBSDIO_FUNC1_CHIPCLKCSR, NULL);
3834
3835 if ((clkval & ~SBSDIO_AVBITS) != clkset) {
3836 brcmf_err("ChipClkCSR access: wrote 0x%02x read 0x%02x\n",
3837 clkset, clkval);
3838 return -EACCES;
3839 }
3840
3841 SPINWAIT(((clkval = brcmf_sdiod_regrb(sdiodev,
3842 SBSDIO_FUNC1_CHIPCLKCSR, NULL)),
3843 !SBSDIO_ALPAV(clkval)),
3844 PMU_MAX_TRANSITION_DLY);
3845 if (!SBSDIO_ALPAV(clkval)) {
3846 brcmf_err("timeout on ALPAV wait, clkval 0x%02x\n",
3847 clkval);
3848 return -EBUSY;
3849 }
3850
3851 clkset = SBSDIO_FORCE_HW_CLKREQ_OFF | SBSDIO_FORCE_ALP;
3852 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, clkset, &err);
3853 udelay(65);
3854
3855 /* Also, disable the extra SDIO pull-ups */
3856 brcmf_sdiod_regwb(sdiodev, SBSDIO_FUNC1_SDIOPULLUP, 0, NULL);
3857
3858 return 0;
3859}
3860
cb7cf7be
AS
3861static void brcmf_sdio_buscore_exitdl(void *ctx, struct brcmf_chip *chip,
3862 u32 rstvec)
3863{
3864 struct brcmf_sdio_dev *sdiodev = ctx;
3865 struct brcmf_core *core;
3866 u32 reg_addr;
3867
3868 /* clear all interrupts */
3869 core = brcmf_chip_get_core(chip, BCMA_CORE_SDIO_DEV);
3870 reg_addr = core->base + offsetof(struct sdpcmd_regs, intstatus);
3871 brcmf_sdiod_regwl(sdiodev, reg_addr, 0xFFFFFFFF, NULL);
3872
3873 if (rstvec)
3874 /* Write reset vector to address 0 */
3875 brcmf_sdiod_ramrw(sdiodev, true, 0, (void *)&rstvec,
3876 sizeof(rstvec));
3877}
3878
3879static u32 brcmf_sdio_buscore_read32(void *ctx, u32 addr)
3880{
3881 struct brcmf_sdio_dev *sdiodev = ctx;
3882 u32 val, rev;
3883
3884 val = brcmf_sdiod_regrl(sdiodev, addr, NULL);
3885 if (sdiodev->func[0]->device == SDIO_DEVICE_ID_BROADCOM_4335_4339 &&
3886 addr == CORE_CC_REG(SI_ENUM_BASE, chipid)) {
3887 rev = (val & CID_REV_MASK) >> CID_REV_SHIFT;
3888 if (rev >= 2) {
3889 val &= ~CID_ID_MASK;
3890 val |= BCM4339_CHIP_ID;
3891 }
3892 }
3893 return val;
3894}
3895
3896static void brcmf_sdio_buscore_write32(void *ctx, u32 addr, u32 val)
3897{
3898 struct brcmf_sdio_dev *sdiodev = ctx;
3899
3900 brcmf_sdiod_regwl(sdiodev, addr, val, NULL);
3901}
3902
3903static const struct brcmf_buscore_ops brcmf_sdio_buscore_ops = {
3904 .prepare = brcmf_sdio_buscoreprep,
3905 .exit_dl = brcmf_sdio_buscore_exitdl,
3906 .read32 = brcmf_sdio_buscore_read32,
3907 .write32 = brcmf_sdio_buscore_write32,
3908};
3909
5b435de0 3910static bool
82d7f3c1 3911brcmf_sdio_probe_attach(struct brcmf_sdio *bus)
5b435de0
AS
3912{
3913 u8 clkctl = 0;
3914 int err = 0;
3915 int reg_addr;
3916 u32 reg_val;
668761ac 3917 u32 drivestrength;
5b435de0 3918
38b0b0dd
FL
3919 sdio_claim_host(bus->sdiodev->func[1]);
3920
18aad4f8 3921 pr_debug("F1 signature read @0x18000000=0x%4x\n",
a39be27b 3922 brcmf_sdiod_regrl(bus->sdiodev, SI_ENUM_BASE, NULL));
5b435de0
AS
3923
3924 /*
cb7cf7be 3925 * Force PLL off until brcmf_chip_attach()
5b435de0
AS
3926 * programs PLL control regs
3927 */
3928
a39be27b
AS
3929 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3930 BRCMF_INIT_CLKCTL1, &err);
5b435de0 3931 if (!err)
a39be27b
AS
3932 clkctl = brcmf_sdiod_regrb(bus->sdiodev,
3933 SBSDIO_FUNC1_CHIPCLKCSR, &err);
5b435de0
AS
3934
3935 if (err || ((clkctl & ~SBSDIO_AVBITS) != BRCMF_INIT_CLKCTL1)) {
5e8149f5 3936 brcmf_err("ChipClkCSR access: err %d wrote 0x%02x read 0x%02x\n",
5b435de0
AS
3937 err, BRCMF_INIT_CLKCTL1, clkctl);
3938 goto fail;
3939 }
3940
bb350711
AS
3941 /* SDIO register access works so moving
3942 * state from UNKNOWN to DOWN.
3943 */
3944 brcmf_bus_change_state(bus->sdiodev->bus_if, BRCMF_BUS_DOWN);
3945
cb7cf7be
AS
3946 bus->ci = brcmf_chip_attach(bus->sdiodev, &brcmf_sdio_buscore_ops);
3947 if (IS_ERR(bus->ci)) {
3948 brcmf_err("brcmf_chip_attach failed!\n");
3949 bus->ci = NULL;
5b435de0
AS
3950 goto fail;
3951 }
3952
82d7f3c1 3953 if (brcmf_sdio_kso_init(bus)) {
4a3da990
PH
3954 brcmf_err("error enabling KSO\n");
3955 goto fail;
3956 }
3957
668761ac
HM
3958 if ((bus->sdiodev->pdata) && (bus->sdiodev->pdata->drive_strength))
3959 drivestrength = bus->sdiodev->pdata->drive_strength;
3960 else
3961 drivestrength = DEFAULT_SDIO_DRIVE_STRENGTH;
65d80d0b 3962 brcmf_sdio_drivestrengthinit(bus->sdiodev, bus->ci, drivestrength);
5b435de0 3963
454d2a88 3964 /* Get info on the SOCRAM cores... */
5b435de0
AS
3965 bus->ramsize = bus->ci->ramsize;
3966 if (!(bus->ramsize)) {
5e8149f5 3967 brcmf_err("failed to find SOCRAM memory!\n");
5b435de0
AS
3968 goto fail;
3969 }
3970
1e9ab4dd 3971 /* Set card control so an SDIO card reset does a WLAN backplane reset */
a39be27b
AS
3972 reg_val = brcmf_sdiod_regrb(bus->sdiodev,
3973 SDIO_CCCR_BRCM_CARDCTRL, &err);
1e9ab4dd
PH
3974 if (err)
3975 goto fail;
3976
3977 reg_val |= SDIO_CCCR_BRCM_CARDCTRL_WLANRESET;
3978
a39be27b
AS
3979 brcmf_sdiod_regwb(bus->sdiodev,
3980 SDIO_CCCR_BRCM_CARDCTRL, reg_val, &err);
1e9ab4dd
PH
3981 if (err)
3982 goto fail;
3983
3984 /* set PMUControl so a backplane reset does PMU state reload */
cb7cf7be 3985 reg_addr = CORE_CC_REG(brcmf_chip_get_chipcommon(bus->ci)->base,
1e9ab4dd 3986 pmucontrol);
cb7cf7be 3987 reg_val = brcmf_sdiod_regrl(bus->sdiodev, reg_addr, &err);
1e9ab4dd
PH
3988 if (err)
3989 goto fail;
3990
3991 reg_val |= (BCMA_CC_PMU_CTL_RES_RELOAD << BCMA_CC_PMU_CTL_RES_SHIFT);
3992
cb7cf7be 3993 brcmf_sdiod_regwl(bus->sdiodev, reg_addr, reg_val, &err);
1e9ab4dd
PH
3994 if (err)
3995 goto fail;
3996
38b0b0dd
FL
3997 sdio_release_host(bus->sdiodev->func[1]);
3998
5b435de0
AS
3999 brcmu_pktq_init(&bus->txq, (PRIOMASK + 1), TXQLEN);
4000
9b2d2f2a
AS
4001 /* allocate header buffer */
4002 bus->hdrbuf = kzalloc(MAX_HDR_READ + bus->head_align, GFP_KERNEL);
4003 if (!bus->hdrbuf)
4004 return false;
5b435de0
AS
4005 /* Locate an appropriately-aligned portion of hdrbuf */
4006 bus->rxhdr = (u8 *) roundup((unsigned long)&bus->hdrbuf[0],
9b2d2f2a 4007 bus->head_align);
5b435de0
AS
4008
4009 /* Set the poll and/or interrupt flags */
4010 bus->intr = true;
4011 bus->poll = false;
4012 if (bus->poll)
4013 bus->pollrate = 1;
4014
4015 return true;
4016
4017fail:
38b0b0dd 4018 sdio_release_host(bus->sdiodev->func[1]);
5b435de0
AS
4019 return false;
4020}
4021
5b435de0 4022static int
82d7f3c1 4023brcmf_sdio_watchdog_thread(void *data)
5b435de0 4024{
e92eedf4 4025 struct brcmf_sdio *bus = (struct brcmf_sdio *)data;
5b435de0
AS
4026
4027 allow_signal(SIGTERM);
4028 /* Run until signal received */
4029 while (1) {
4030 if (kthread_should_stop())
4031 break;
4032 if (!wait_for_completion_interruptible(&bus->watchdog_wait)) {
82d7f3c1 4033 brcmf_sdio_bus_watchdog(bus);
5b435de0 4034 /* Count the tick for reference */
80969836 4035 bus->sdcnt.tickcnt++;
5b435de0
AS
4036 } else
4037 break;
4038 }
4039 return 0;
4040}
4041
4042static void
82d7f3c1 4043brcmf_sdio_watchdog(unsigned long data)
5b435de0 4044{
e92eedf4 4045 struct brcmf_sdio *bus = (struct brcmf_sdio *)data;
5b435de0
AS
4046
4047 if (bus->watchdog_tsk) {
4048 complete(&bus->watchdog_wait);
4049 /* Reschedule the watchdog */
4050 if (bus->wd_timer_valid)
4051 mod_timer(&bus->timer,
4052 jiffies + BRCMF_WD_POLL_MS * HZ / 1000);
4053 }
4054}
4055
d9cb2596 4056static struct brcmf_bus_ops brcmf_sdio_bus_ops = {
82d7f3c1
AS
4057 .stop = brcmf_sdio_bus_stop,
4058 .preinit = brcmf_sdio_bus_preinit,
4059 .init = brcmf_sdio_bus_init,
4060 .txdata = brcmf_sdio_bus_txdata,
4061 .txctl = brcmf_sdio_bus_txctl,
4062 .rxctl = brcmf_sdio_bus_rxctl,
4063 .gettxq = brcmf_sdio_bus_gettxq,
d9cb2596
AS
4064};
4065
82d7f3c1 4066struct brcmf_sdio *brcmf_sdio_probe(struct brcmf_sdio_dev *sdiodev)
5b435de0
AS
4067{
4068 int ret;
e92eedf4 4069 struct brcmf_sdio *bus;
5b435de0 4070
5b435de0
AS
4071 brcmf_dbg(TRACE, "Enter\n");
4072
5b435de0 4073 /* Allocate private bus interface state */
e92eedf4 4074 bus = kzalloc(sizeof(struct brcmf_sdio), GFP_ATOMIC);
5b435de0
AS
4075 if (!bus)
4076 goto fail;
4077
4078 bus->sdiodev = sdiodev;
4079 sdiodev->bus = bus;
b83db862 4080 skb_queue_head_init(&bus->glom);
5b435de0
AS
4081 bus->txbound = BRCMF_TXBOUND;
4082 bus->rxbound = BRCMF_RXBOUND;
4083 bus->txminmax = BRCMF_TXMINMAX;
6bc52319 4084 bus->tx_seq = SDPCM_SEQ_WRAP - 1;
5b435de0 4085
e217d1c8
AS
4086 /* platform specific configuration:
4087 * alignments must be at least 4 bytes for ADMA
4088 */
4089 bus->head_align = ALIGNMENT;
4090 bus->sgentry_align = ALIGNMENT;
4091 if (sdiodev->pdata) {
4092 if (sdiodev->pdata->sd_head_align > ALIGNMENT)
4093 bus->head_align = sdiodev->pdata->sd_head_align;
4094 if (sdiodev->pdata->sd_sgentry_align > ALIGNMENT)
4095 bus->sgentry_align = sdiodev->pdata->sd_sgentry_align;
4096 }
4097
37ac5780
HM
4098 INIT_WORK(&bus->datawork, brcmf_sdio_dataworker);
4099 bus->brcmf_wq = create_singlethread_workqueue("brcmf_wq");
4100 if (bus->brcmf_wq == NULL) {
5e8149f5 4101 brcmf_err("insufficient memory to create txworkqueue\n");
37ac5780
HM
4102 goto fail;
4103 }
4104
5b435de0 4105 /* attempt to attach to the dongle */
82d7f3c1
AS
4106 if (!(brcmf_sdio_probe_attach(bus))) {
4107 brcmf_err("brcmf_sdio_probe_attach failed\n");
5b435de0
AS
4108 goto fail;
4109 }
4110
dd43a01c 4111 spin_lock_init(&bus->rxctl_lock);
5b435de0
AS
4112 spin_lock_init(&bus->txqlock);
4113 init_waitqueue_head(&bus->ctrl_wait);
4114 init_waitqueue_head(&bus->dcmd_resp_wait);
4115
4116 /* Set up the watchdog timer */
4117 init_timer(&bus->timer);
4118 bus->timer.data = (unsigned long)bus;
82d7f3c1 4119 bus->timer.function = brcmf_sdio_watchdog;
5b435de0 4120
5b435de0
AS
4121 /* Initialize watchdog thread */
4122 init_completion(&bus->watchdog_wait);
82d7f3c1 4123 bus->watchdog_tsk = kthread_run(brcmf_sdio_watchdog_thread,
5b435de0
AS
4124 bus, "brcmf_watchdog");
4125 if (IS_ERR(bus->watchdog_tsk)) {
02f77195 4126 pr_warn("brcmf_watchdog thread failed to start\n");
5b435de0
AS
4127 bus->watchdog_tsk = NULL;
4128 }
4129 /* Initialize DPC thread */
fccfe930 4130 atomic_set(&bus->dpc_tskcnt, 0);
5b435de0 4131
a9ffda88 4132 /* Assign bus interface call back */
d9cb2596
AS
4133 bus->sdiodev->bus_if->dev = bus->sdiodev->dev;
4134 bus->sdiodev->bus_if->ops = &brcmf_sdio_bus_ops;
75d907d3
AS
4135 bus->sdiodev->bus_if->chip = bus->ci->chip;
4136 bus->sdiodev->bus_if->chiprev = bus->ci->chiprev;
d9cb2596 4137
706478cb
FL
4138 /* default sdio bus header length for tx packet */
4139 bus->tx_hdrlen = SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN;
4140
4141 /* Attach to the common layer, reserve hdr space */
8dee77ba 4142 ret = brcmf_attach(bus->sdiodev->dev);
712ac5b3 4143 if (ret != 0) {
5e8149f5 4144 brcmf_err("brcmf_attach failed\n");
5b435de0
AS
4145 goto fail;
4146 }
4147
4148 /* Allocate buffers */
fad13228
AS
4149 if (bus->sdiodev->bus_if->maxctl) {
4150 bus->rxblen =
4151 roundup((bus->sdiodev->bus_if->maxctl + SDPCM_HDRLEN),
4152 ALIGNMENT) + bus->head_align;
4153 bus->rxbuf = kmalloc(bus->rxblen, GFP_ATOMIC);
4154 if (!(bus->rxbuf)) {
4155 brcmf_err("rxbuf allocation failed\n");
4156 goto fail;
4157 }
5b435de0
AS
4158 }
4159
fad13228
AS
4160 sdio_claim_host(bus->sdiodev->func[1]);
4161
4162 /* Disable F2 to clear any intermediate frame state on the dongle */
4163 sdio_disable_func(bus->sdiodev->func[SDIO_FUNC_2]);
4164
fad13228
AS
4165 bus->rxflow = false;
4166
4167 /* Done with backplane-dependent accesses, can drop clock... */
4168 brcmf_sdiod_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, 0, NULL);
4169
4170 sdio_release_host(bus->sdiodev->func[1]);
4171
4172 /* ...and initialize clock/power states */
4173 bus->clkstate = CLK_SDONLY;
4174 bus->idletime = BRCMF_IDLE_INTERVAL;
4175 bus->idleclock = BRCMF_IDLE_ACTIVE;
4176
4177 /* Query the F2 block size, set roundup accordingly */
4178 bus->blocksize = bus->sdiodev->func[2]->cur_blksize;
4179 bus->roundup = min(max_roundup, bus->blocksize);
4180
4181 /* SR state */
4182 bus->sleeping = false;
4183 bus->sr_enabled = false;
5b435de0 4184
80969836 4185 brcmf_sdio_debugfs_create(bus);
5b435de0
AS
4186 brcmf_dbg(INFO, "completed!!\n");
4187
4188 /* if firmware path present try to download and bring up bus */
ed683c98 4189 ret = brcmf_bus_start(bus->sdiodev->dev);
5b435de0 4190 if (ret != 0) {
5e8149f5 4191 brcmf_err("dongle is not responding\n");
1799ddf1 4192 goto fail;
5b435de0 4193 }
15d45b6f 4194
5b435de0
AS
4195 return bus;
4196
4197fail:
9fbe2a6d 4198 brcmf_sdio_remove(bus);
5b435de0
AS
4199 return NULL;
4200}
4201
9fbe2a6d
AS
4202/* Detach and free everything */
4203void brcmf_sdio_remove(struct brcmf_sdio *bus)
5b435de0 4204{
5b435de0
AS
4205 brcmf_dbg(TRACE, "Enter\n");
4206
9fbe2a6d
AS
4207 if (bus) {
4208 /* De-register interrupt handler */
4209 brcmf_sdiod_intr_unregister(bus->sdiodev);
4210
9fbe2a6d
AS
4211 if (bus->sdiodev->bus_if->drvr) {
4212 brcmf_detach(bus->sdiodev->dev);
bfad4a04
AS
4213 }
4214
e0c180ec
HM
4215 cancel_work_sync(&bus->datawork);
4216 if (bus->brcmf_wq)
4217 destroy_workqueue(bus->brcmf_wq);
4218
bfad4a04 4219 if (bus->ci) {
bb350711
AS
4220 if (bus->sdiodev->bus_if->state == BRCMF_BUS_DOWN) {
4221 sdio_claim_host(bus->sdiodev->func[1]);
4222 brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
4223 /* Leave the device in state where it is
4224 * 'quiet'. This is done by putting it in
4225 * download_state which essentially resets
4226 * all necessary cores.
4227 */
4228 msleep(20);
cb7cf7be 4229 brcmf_chip_enter_download(bus->ci);
bb350711
AS
4230 brcmf_sdio_clkctl(bus, CLK_NONE, false);
4231 sdio_release_host(bus->sdiodev->func[1]);
4232 }
cb7cf7be 4233 brcmf_chip_detach(bus->ci);
9fbe2a6d
AS
4234 }
4235
4236 brcmu_pkt_buf_free_skb(bus->txglom_sgpad);
bfad4a04 4237 kfree(bus->rxbuf);
9fbe2a6d
AS
4238 kfree(bus->hdrbuf);
4239 kfree(bus);
4240 }
5b435de0
AS
4241
4242 brcmf_dbg(TRACE, "Disconnected\n");
4243}
4244
82d7f3c1 4245void brcmf_sdio_wd_timer(struct brcmf_sdio *bus, uint wdtick)
5b435de0 4246{
5b435de0 4247 /* Totally stop the timer */
23677ce3 4248 if (!wdtick && bus->wd_timer_valid) {
5b435de0
AS
4249 del_timer_sync(&bus->timer);
4250 bus->wd_timer_valid = false;
4251 bus->save_ms = wdtick;
4252 return;
4253 }
4254
ece960ea 4255 /* don't start the wd until fw is loaded */
d6ae2c51 4256 if (bus->sdiodev->bus_if->state != BRCMF_BUS_DATA)
ece960ea
FL
4257 return;
4258
5b435de0
AS
4259 if (wdtick) {
4260 if (bus->save_ms != BRCMF_WD_POLL_MS) {
23677ce3 4261 if (bus->wd_timer_valid)
5b435de0
AS
4262 /* Stop timer and restart at new value */
4263 del_timer_sync(&bus->timer);
4264
4265 /* Create timer again when watchdog period is
4266 dynamically changed or in the first instance
4267 */
4268 bus->timer.expires =
4269 jiffies + BRCMF_WD_POLL_MS * HZ / 1000;
4270 add_timer(&bus->timer);
4271
4272 } else {
4273 /* Re arm the timer, at last watchdog period */
4274 mod_timer(&bus->timer,
4275 jiffies + BRCMF_WD_POLL_MS * HZ / 1000);
4276 }
4277
4278 bus->wd_timer_valid = true;
4279 bus->save_ms = wdtick;
4280 }
4281}