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