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