blob: 67f05db4b9b8e11918771b087925c12d184821fd [file] [log] [blame]
Arend van Spriel5b435de2011-10-05 13:19:03 +02001/*
2 * Copyright (c) 2010 Broadcom Corporation
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17#include <linux/types.h>
18#include <linux/kernel.h>
19#include <linux/kthread.h>
20#include <linux/printk.h>
21#include <linux/pci_ids.h>
22#include <linux/netdevice.h>
23#include <linux/interrupt.h>
24#include <linux/sched.h>
25#include <linux/mmc/sdio.h>
26#include <linux/mmc/sdio_func.h>
27#include <linux/mmc/card.h>
28#include <linux/semaphore.h>
29#include <linux/firmware.h>
Stephen Rothwellb7a57e72011-10-12 21:35:07 +020030#include <linux/module.h>
Franky Lin99ba15c2011-11-04 22:23:42 +010031#include <linux/bcma/bcma.h>
Arend van Spriel4fc0d012012-06-14 12:16:57 +020032#include <linux/debugfs.h>
Arend van Spriel8dc01812012-06-21 11:33:32 +020033#include <linux/vmalloc.h>
Hante Meuleman668761a2013-04-12 10:55:55 +020034#include <linux/platform_data/brcmfmac-sdio.h>
Arend van Spriel5b435de2011-10-05 13:19:03 +020035#include <asm/unaligned.h>
36#include <defs.h>
37#include <brcmu_wifi.h>
38#include <brcmu_utils.h>
39#include <brcm_hw_ids.h>
40#include <soc.h>
41#include "sdio_host.h"
Franky Lina83369b2011-11-04 22:23:28 +010042#include "sdio_chip.h"
Arend van Spriel5b435de2011-10-05 13:19:03 +020043
44#define DCMD_RESP_TIMEOUT 2000 /* In milli second */
45
Joe Perches8ae74652012-01-15 00:38:38 -080046#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +020047
48#define BRCMF_TRAP_INFO_SIZE 80
49
50#define CBUF_LEN (128)
51
Arend van Spriel4fc0d012012-06-14 12:16:57 +020052/* Device console log buffer state */
53#define CONSOLE_BUFFER_MAX 2024
54
Arend van Spriel5b435de2011-10-05 13:19:03 +020055struct rte_log_le {
56 __le32 buf; /* Can't be pointer on (64-bit) hosts */
57 __le32 buf_size;
58 __le32 idx;
59 char *_buf_compat; /* Redundant pointer for backward compat. */
60};
61
62struct rte_console {
63 /* Virtual UART
64 * When there is no UART (e.g. Quickturn),
65 * the host should write a complete
66 * input line directly into cbuf and then write
67 * the length into vcons_in.
68 * This may also be used when there is a real UART
69 * (at risk of conflicting with
70 * the real UART). vcons_out is currently unused.
71 */
72 uint vcons_in;
73 uint vcons_out;
74
75 /* Output (logging) buffer
76 * Console output is written to a ring buffer log_buf at index log_idx.
77 * The host may read the output when it sees log_idx advance.
78 * Output will be lost if the output wraps around faster than the host
79 * polls.
80 */
81 struct rte_log_le log_le;
82
83 /* Console input line buffer
84 * Characters are read one at a time into cbuf
85 * until <CR> is received, then
86 * the buffer is processed as a command line.
87 * Also used for virtual UART.
88 */
89 uint cbuf_idx;
90 char cbuf[CBUF_LEN];
91};
92
Joe Perches8ae74652012-01-15 00:38:38 -080093#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +020094#include <chipcommon.h>
95
Arend van Spriel5b435de2011-10-05 13:19:03 +020096#include "dhd_bus.h"
Arend van Spriel5b435de2011-10-05 13:19:03 +020097#include "dhd_dbg.h"
Arend van Spriel40c1c242013-04-05 10:57:44 +020098#include "tracepoint.h"
Arend van Spriel5b435de2011-10-05 13:19:03 +020099
100#define TXQLEN 2048 /* bulk tx queue length */
101#define TXHI (TXQLEN - 256) /* turn on flow control above TXHI */
102#define TXLOW (TXHI - 256) /* turn off flow control below TXLOW */
103#define PRIOMASK 7
104
105#define TXRETRIES 2 /* # of retries for tx frames */
106
107#define BRCMF_RXBOUND 50 /* Default for max rx frames in
108 one scheduling */
109
110#define BRCMF_TXBOUND 20 /* Default for max tx frames in
111 one scheduling */
112
113#define BRCMF_TXMINMAX 1 /* Max tx frames if rx still pending */
114
115#define MEMBLOCK 2048 /* Block size used for downloading
116 of dongle image */
117#define MAX_DATA_BUF (32 * 1024) /* Must be large enough to hold
118 biggest possible glom */
119
120#define BRCMF_FIRSTREAD (1 << 6)
121
122
123/* SBSDIO_DEVICE_CTL */
124
125/* 1: device will assert busy signal when receiving CMD53 */
126#define SBSDIO_DEVCTL_SETBUSY 0x01
127/* 1: assertion of sdio interrupt is synchronous to the sdio clock */
128#define SBSDIO_DEVCTL_SPI_INTR_SYNC 0x02
129/* 1: mask all interrupts to host except the chipActive (rev 8) */
130#define SBSDIO_DEVCTL_CA_INT_ONLY 0x04
131/* 1: isolate internal sdio signals, put external pads in tri-state; requires
132 * sdio bus power cycle to clear (rev 9) */
133#define SBSDIO_DEVCTL_PADS_ISO 0x08
134/* Force SD->SB reset mapping (rev 11) */
135#define SBSDIO_DEVCTL_SB_RST_CTL 0x30
136/* Determined by CoreControl bit */
137#define SBSDIO_DEVCTL_RST_CORECTL 0x00
138/* Force backplane reset */
139#define SBSDIO_DEVCTL_RST_BPRESET 0x10
140/* Force no backplane reset */
141#define SBSDIO_DEVCTL_RST_NOBPRESET 0x20
142
Arend van Spriel5b435de2011-10-05 13:19:03 +0200143/* direct(mapped) cis space */
144
145/* MAPPED common CIS address */
146#define SBSDIO_CIS_BASE_COMMON 0x1000
147/* maximum bytes in one CIS */
148#define SBSDIO_CIS_SIZE_LIMIT 0x200
149/* cis offset addr is < 17 bits */
150#define SBSDIO_CIS_OFT_ADDR_MASK 0x1FFFF
151
152/* manfid tuple length, include tuple, link bytes */
153#define SBSDIO_CIS_MANFID_TUPLE_LEN 6
154
155/* intstatus */
156#define I_SMB_SW0 (1 << 0) /* To SB Mail S/W interrupt 0 */
157#define I_SMB_SW1 (1 << 1) /* To SB Mail S/W interrupt 1 */
158#define I_SMB_SW2 (1 << 2) /* To SB Mail S/W interrupt 2 */
159#define I_SMB_SW3 (1 << 3) /* To SB Mail S/W interrupt 3 */
160#define I_SMB_SW_MASK 0x0000000f /* To SB Mail S/W interrupts mask */
161#define I_SMB_SW_SHIFT 0 /* To SB Mail S/W interrupts shift */
162#define I_HMB_SW0 (1 << 4) /* To Host Mail S/W interrupt 0 */
163#define I_HMB_SW1 (1 << 5) /* To Host Mail S/W interrupt 1 */
164#define I_HMB_SW2 (1 << 6) /* To Host Mail S/W interrupt 2 */
165#define I_HMB_SW3 (1 << 7) /* To Host Mail S/W interrupt 3 */
166#define I_HMB_SW_MASK 0x000000f0 /* To Host Mail S/W interrupts mask */
167#define I_HMB_SW_SHIFT 4 /* To Host Mail S/W interrupts shift */
168#define I_WR_OOSYNC (1 << 8) /* Write Frame Out Of Sync */
169#define I_RD_OOSYNC (1 << 9) /* Read Frame Out Of Sync */
170#define I_PC (1 << 10) /* descriptor error */
171#define I_PD (1 << 11) /* data error */
172#define I_DE (1 << 12) /* Descriptor protocol Error */
173#define I_RU (1 << 13) /* Receive descriptor Underflow */
174#define I_RO (1 << 14) /* Receive fifo Overflow */
175#define I_XU (1 << 15) /* Transmit fifo Underflow */
176#define I_RI (1 << 16) /* Receive Interrupt */
177#define I_BUSPWR (1 << 17) /* SDIO Bus Power Change (rev 9) */
178#define I_XMTDATA_AVAIL (1 << 23) /* bits in fifo */
179#define I_XI (1 << 24) /* Transmit Interrupt */
180#define I_RF_TERM (1 << 25) /* Read Frame Terminate */
181#define I_WF_TERM (1 << 26) /* Write Frame Terminate */
182#define I_PCMCIA_XU (1 << 27) /* PCMCIA Transmit FIFO Underflow */
183#define I_SBINT (1 << 28) /* sbintstatus Interrupt */
184#define I_CHIPACTIVE (1 << 29) /* chip from doze to active state */
185#define I_SRESET (1 << 30) /* CCCR RES interrupt */
186#define I_IOE2 (1U << 31) /* CCCR IOE2 Bit Changed */
187#define I_ERRORS (I_PC | I_PD | I_DE | I_RU | I_RO | I_XU)
188#define I_DMA (I_RI | I_XI | I_ERRORS)
189
190/* corecontrol */
191#define CC_CISRDY (1 << 0) /* CIS Ready */
192#define CC_BPRESEN (1 << 1) /* CCCR RES signal */
193#define CC_F2RDY (1 << 2) /* set CCCR IOR2 bit */
194#define CC_CLRPADSISO (1 << 3) /* clear SDIO pads isolation */
195#define CC_XMTDATAAVAIL_MODE (1 << 4)
196#define CC_XMTDATAAVAIL_CTRL (1 << 5)
197
198/* SDA_FRAMECTRL */
199#define SFC_RF_TERM (1 << 0) /* Read Frame Terminate */
200#define SFC_WF_TERM (1 << 1) /* Write Frame Terminate */
201#define SFC_CRC4WOOS (1 << 2) /* CRC error for write out of sync */
202#define SFC_ABORTALL (1 << 3) /* Abort all in-progress frames */
203
Arend van Spriel5b435de2011-10-05 13:19:03 +0200204/*
205 * Software allocation of To SB Mailbox resources
206 */
207
208/* tosbmailbox bits corresponding to intstatus bits */
209#define SMB_NAK (1 << 0) /* Frame NAK */
210#define SMB_INT_ACK (1 << 1) /* Host Interrupt ACK */
211#define SMB_USE_OOB (1 << 2) /* Use OOB Wakeup */
212#define SMB_DEV_INT (1 << 3) /* Miscellaneous Interrupt */
213
214/* tosbmailboxdata */
215#define SMB_DATA_VERSION_SHIFT 16 /* host protocol version */
216
217/*
218 * Software allocation of To Host Mailbox resources
219 */
220
221/* intstatus bits */
222#define I_HMB_FC_STATE I_HMB_SW0 /* Flow Control State */
223#define I_HMB_FC_CHANGE I_HMB_SW1 /* Flow Control State Changed */
224#define I_HMB_FRAME_IND I_HMB_SW2 /* Frame Indication */
225#define I_HMB_HOST_INT I_HMB_SW3 /* Miscellaneous Interrupt */
226
227/* tohostmailboxdata */
228#define HMB_DATA_NAKHANDLED 1 /* retransmit NAK'd frame */
229#define HMB_DATA_DEVREADY 2 /* talk to host after enable */
230#define HMB_DATA_FC 4 /* per prio flowcontrol update flag */
231#define HMB_DATA_FWREADY 8 /* fw ready for protocol activity */
232
233#define HMB_DATA_FCDATA_MASK 0xff000000
234#define HMB_DATA_FCDATA_SHIFT 24
235
236#define HMB_DATA_VERSION_MASK 0x00ff0000
237#define HMB_DATA_VERSION_SHIFT 16
238
239/*
240 * Software-defined protocol header
241 */
242
243/* Current protocol version */
244#define SDPCM_PROT_VERSION 4
245
Arend van Spriel5b435de2011-10-05 13:19:03 +0200246/*
247 * Shared structure between dongle and the host.
248 * The structure contains pointers to trap or assert information.
249 */
Arend van Spriel4fc0d012012-06-14 12:16:57 +0200250#define SDPCM_SHARED_VERSION 0x0003
Arend van Spriel5b435de2011-10-05 13:19:03 +0200251#define SDPCM_SHARED_VERSION_MASK 0x00FF
252#define SDPCM_SHARED_ASSERT_BUILT 0x0100
253#define SDPCM_SHARED_ASSERT 0x0200
254#define SDPCM_SHARED_TRAP 0x0400
255
256/* Space for header read, limit for data packets */
257#define MAX_HDR_READ (1 << 6)
258#define MAX_RX_DATASZ 2048
259
260/* Maximum milliseconds to wait for F2 to come up */
261#define BRCMF_WAIT_F2RDY 3000
262
263/* Bump up limit on waiting for HT to account for first startup;
264 * if the image is doing a CRC calculation before programming the PMU
265 * for HT availability, it could take a couple hundred ms more, so
266 * max out at a 1 second (1000000us).
267 */
268#undef PMU_MAX_TRANSITION_DLY
269#define PMU_MAX_TRANSITION_DLY 1000000
270
271/* Value for ChipClockCSR during initial setup */
272#define BRCMF_INIT_CLKCTL1 (SBSDIO_FORCE_HW_CLKREQ_OFF | \
273 SBSDIO_ALP_AVAIL_REQ)
274
275/* Flags for SDH calls */
276#define F2SYNC (SDIO_REQ_4BYTE | SDIO_REQ_FIXED)
277
Franky Lin382a9e02011-12-16 18:37:18 -0800278#define BRCMF_IDLE_IMMEDIATE (-1) /* Enter idle immediately */
279#define BRCMF_IDLE_ACTIVE 0 /* Do not request any SD clock change
280 * when idle
281 */
282#define BRCMF_IDLE_INTERVAL 1
283
Piotr Haber4a3da992013-04-11 13:28:46 +0200284#define KSO_WAIT_US 50
285#define MAX_KSO_ATTEMPTS (PMU_MAX_TRANSITION_DLY/KSO_WAIT_US)
286
Arend van Spriel5b435de2011-10-05 13:19:03 +0200287/*
288 * Conversion of 802.1D priority to precedence level
289 */
290static uint prio2prec(u32 prio)
291{
292 return (prio == PRIO_8021D_NONE || prio == PRIO_8021D_BE) ?
293 (prio^2) : prio;
294}
295
Joe Perches8ae74652012-01-15 00:38:38 -0800296#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200297/* Device console log buffer state */
298struct brcmf_console {
299 uint count; /* Poll interval msec counter */
300 uint log_addr; /* Log struct address (fixed) */
301 struct rte_log_le log_le; /* Log struct (host copy) */
302 uint bufsize; /* Size of log buffer */
303 u8 *buf; /* Log buffer (host copy) */
304 uint last; /* Last buffer read index */
305};
Arend van Spriel4fc0d012012-06-14 12:16:57 +0200306
307struct brcmf_trap_info {
308 __le32 type;
309 __le32 epc;
310 __le32 cpsr;
311 __le32 spsr;
312 __le32 r0; /* a1 */
313 __le32 r1; /* a2 */
314 __le32 r2; /* a3 */
315 __le32 r3; /* a4 */
316 __le32 r4; /* v1 */
317 __le32 r5; /* v2 */
318 __le32 r6; /* v3 */
319 __le32 r7; /* v4 */
320 __le32 r8; /* v5 */
321 __le32 r9; /* sb/v6 */
322 __le32 r10; /* sl/v7 */
323 __le32 r11; /* fp/v8 */
324 __le32 r12; /* ip */
325 __le32 r13; /* sp */
326 __le32 r14; /* lr */
327 __le32 pc; /* r15 */
328};
Joe Perches8ae74652012-01-15 00:38:38 -0800329#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200330
331struct sdpcm_shared {
332 u32 flags;
333 u32 trap_addr;
334 u32 assert_exp_addr;
335 u32 assert_file_addr;
336 u32 assert_line;
337 u32 console_addr; /* Address of struct rte_console */
338 u32 msgtrace_addr;
339 u8 tag[32];
Arend van Spriel4fc0d012012-06-14 12:16:57 +0200340 u32 brpt_addr;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200341};
342
343struct sdpcm_shared_le {
344 __le32 flags;
345 __le32 trap_addr;
346 __le32 assert_exp_addr;
347 __le32 assert_file_addr;
348 __le32 assert_line;
349 __le32 console_addr; /* Address of struct rte_console */
350 __le32 msgtrace_addr;
351 u8 tag[32];
Arend van Spriel4fc0d012012-06-14 12:16:57 +0200352 __le32 brpt_addr;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200353};
354
Franky Lin6bc52312013-08-10 12:27:28 +0200355/* dongle SDIO bus specific header info */
356struct brcmf_sdio_hdrinfo {
Franky Lin4754fce2012-09-19 22:21:11 +0200357 u8 seq_num;
358 u8 channel;
359 u16 len;
360 u16 len_left;
361 u16 len_nxtfrm;
362 u8 dat_offset;
363};
Arend van Spriel5b435de2011-10-05 13:19:03 +0200364
365/* misc chip info needed by some of the routines */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200366/* Private data for SDIO bus interaction */
Franky Line92eedf2011-11-22 17:21:50 -0800367struct brcmf_sdio {
Arend van Spriel5b435de2011-10-05 13:19:03 +0200368 struct brcmf_sdio_dev *sdiodev; /* sdio device handler */
369 struct chip_info *ci; /* Chip info struct */
370 char *vars; /* Variables (from CIS and/or other) */
371 uint varsz; /* Size of variables buffer */
372
373 u32 ramsize; /* Size of RAM in SOCRAM (bytes) */
374
375 u32 hostintmask; /* Copy of Host Interrupt Mask */
Franky Lin45316032012-09-13 21:12:03 +0200376 atomic_t intstatus; /* Intstatus bits (events) pending */
377 atomic_t fcstate; /* State of dongle flow-control */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200378
379 uint blocksize; /* Block size of SDIO transfers */
380 uint roundup; /* Max roundup limit */
381
382 struct pktq txq; /* Queue length used for flow-control */
383 u8 flowcontrol; /* per prio flow control bitmask */
384 u8 tx_seq; /* Transmit sequence number (next) */
385 u8 tx_max; /* Maximum transmit sequence allowed */
386
387 u8 hdrbuf[MAX_HDR_READ + BRCMF_SDALIGN];
388 u8 *rxhdr; /* Header of current rx frame (in hdrbuf) */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200389 u8 rx_seq; /* Receive sequence number (expected) */
Franky Lin6bc52312013-08-10 12:27:28 +0200390 struct brcmf_sdio_hdrinfo cur_read;
Franky Lin4754fce2012-09-19 22:21:11 +0200391 /* info of current read frame */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200392 bool rxskip; /* Skip receive (awaiting NAK ACK) */
Franky Lin4754fce2012-09-19 22:21:11 +0200393 bool rxpending; /* Data frame pending in dongle */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200394
395 uint rxbound; /* Rx frames to read before resched */
396 uint txbound; /* Tx frames to send before resched */
397 uint txminmax;
398
399 struct sk_buff *glomd; /* Packet containing glomming descriptor */
Arend van Sprielb83db862011-10-19 12:51:09 +0200400 struct sk_buff_head glom; /* Packet list for glommed superframe */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200401 uint glomerr; /* Glom packet read errors */
402
403 u8 *rxbuf; /* Buffer for receiving control packets */
404 uint rxblen; /* Allocated length of rxbuf */
405 u8 *rxctl; /* Aligned pointer into rxbuf */
Franky Lindd43a012012-11-05 16:22:22 -0800406 u8 *rxctl_orig; /* pointer for freeing rxctl */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200407 uint rxlen; /* Length of valid data in buffer */
Franky Lindd43a012012-11-05 16:22:22 -0800408 spinlock_t rxctl_lock; /* protection lock for ctrl frame resources */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200409
410 u8 sdpcm_ver; /* Bus protocol reported by dongle */
411
412 bool intr; /* Use interrupts */
413 bool poll; /* Use polling */
Franky Lin1d382272012-09-13 21:11:59 +0200414 atomic_t ipend; /* Device interrupt is pending */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200415 uint spurious; /* Count of spurious interrupts */
416 uint pollrate; /* Ticks between device polls */
417 uint polltick; /* Tick counter */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200418
Joe Perches8ae74652012-01-15 00:38:38 -0800419#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200420 uint console_interval;
421 struct brcmf_console console; /* Console output polling support */
422 uint console_addr; /* Console address from shared struct */
Joe Perches8ae74652012-01-15 00:38:38 -0800423#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200424
Arend van Spriel5b435de2011-10-05 13:19:03 +0200425 uint clkstate; /* State of sd and backplane clock(s) */
426 bool activity; /* Activity flag for clock down */
427 s32 idletime; /* Control for activity timeout */
428 s32 idlecount; /* Activity timeout counter */
429 s32 idleclock; /* How to set bus driver when idle */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200430 bool rxflow_mode; /* Rx flow control mode */
431 bool rxflow; /* Is rx flow control on */
432 bool alp_only; /* Don't use HT clock (ALP only) */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200433
Arend van Spriel5b435de2011-10-05 13:19:03 +0200434 u8 *ctrl_frame_buf;
435 u32 ctrl_frame_len;
436 bool ctrl_frame_stat;
437
438 spinlock_t txqlock;
439 wait_queue_head_t ctrl_wait;
440 wait_queue_head_t dcmd_resp_wait;
441
442 struct timer_list timer;
443 struct completion watchdog_wait;
444 struct task_struct *watchdog_tsk;
445 bool wd_timer_valid;
446 uint save_ms;
447
Franky Linf1e68c22012-09-13 21:12:00 +0200448 struct workqueue_struct *brcmf_wq;
449 struct work_struct datawork;
Arend van Sprielfccfe932013-06-18 13:29:26 +0200450 atomic_t dpc_tskcnt;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200451
Franky Linc8bf3482011-12-16 18:37:08 -0800452 bool txoff; /* Transmit flow-controlled */
Arend van Spriel80969832012-06-09 22:51:44 +0200453 struct brcmf_sdio_count sdcnt;
Piotr Haber4a3da992013-04-11 13:28:46 +0200454 bool sr_enabled; /* SaveRestore enabled */
455 bool sleeping; /* SDIO bus sleeping */
Franky Lin706478c2013-08-10 12:27:29 +0200456
457 u8 tx_hdrlen; /* sdio bus header length for tx packet */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200458};
459
Arend van Spriel5b435de2011-10-05 13:19:03 +0200460/* clkstate */
461#define CLK_NONE 0
462#define CLK_SDONLY 1
Piotr Haber4a3da992013-04-11 13:28:46 +0200463#define CLK_PENDING 2
Arend van Spriel5b435de2011-10-05 13:19:03 +0200464#define CLK_AVAIL 3
465
Joe Perches8ae74652012-01-15 00:38:38 -0800466#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200467static int qcount[NUMPRIO];
Joe Perches8ae74652012-01-15 00:38:38 -0800468#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200469
Hante Meuleman668761a2013-04-12 10:55:55 +0200470#define DEFAULT_SDIO_DRIVE_STRENGTH 6 /* in milliamps */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200471
472#define RETRYCHAN(chan) ((chan) == SDPCM_EVENT_CHANNEL)
473
474/* Retry count for register access failures */
475static const uint retry_limit = 2;
476
477/* Limit on rounding up frames */
478static const uint max_roundup = 512;
479
480#define ALIGNMENT 4
481
Franky Lin9d7d6f92012-10-22 10:36:24 -0700482enum brcmf_sdio_frmtype {
483 BRCMF_SDIO_FT_NORMAL,
484 BRCMF_SDIO_FT_SUPER,
485 BRCMF_SDIO_FT_SUB,
486};
487
Hante Meulemanf2c44fe2013-09-25 13:05:45 +0200488#define BCM43143_FIRMWARE_NAME "brcm/brcmfmac43143-sdio.bin"
489#define BCM43143_NVRAM_NAME "brcm/brcmfmac43143-sdio.txt"
490#define BCM43241B0_FIRMWARE_NAME "brcm/brcmfmac43241b0-sdio.bin"
491#define BCM43241B0_NVRAM_NAME "brcm/brcmfmac43241b0-sdio.txt"
492#define BCM43241B4_FIRMWARE_NAME "brcm/brcmfmac43241b4-sdio.bin"
493#define BCM43241B4_NVRAM_NAME "brcm/brcmfmac43241b4-sdio.txt"
494#define BCM4329_FIRMWARE_NAME "brcm/brcmfmac4329-sdio.bin"
495#define BCM4329_NVRAM_NAME "brcm/brcmfmac4329-sdio.txt"
496#define BCM4330_FIRMWARE_NAME "brcm/brcmfmac4330-sdio.bin"
497#define BCM4330_NVRAM_NAME "brcm/brcmfmac4330-sdio.txt"
498#define BCM4334_FIRMWARE_NAME "brcm/brcmfmac4334-sdio.bin"
499#define BCM4334_NVRAM_NAME "brcm/brcmfmac4334-sdio.txt"
500#define BCM4335_FIRMWARE_NAME "brcm/brcmfmac4335-sdio.bin"
501#define BCM4335_NVRAM_NAME "brcm/brcmfmac4335-sdio.txt"
502
503MODULE_FIRMWARE(BCM43143_FIRMWARE_NAME);
504MODULE_FIRMWARE(BCM43143_NVRAM_NAME);
505MODULE_FIRMWARE(BCM43241B0_FIRMWARE_NAME);
506MODULE_FIRMWARE(BCM43241B0_NVRAM_NAME);
507MODULE_FIRMWARE(BCM43241B4_FIRMWARE_NAME);
508MODULE_FIRMWARE(BCM43241B4_NVRAM_NAME);
509MODULE_FIRMWARE(BCM4329_FIRMWARE_NAME);
510MODULE_FIRMWARE(BCM4329_NVRAM_NAME);
511MODULE_FIRMWARE(BCM4330_FIRMWARE_NAME);
512MODULE_FIRMWARE(BCM4330_NVRAM_NAME);
513MODULE_FIRMWARE(BCM4334_FIRMWARE_NAME);
514MODULE_FIRMWARE(BCM4334_NVRAM_NAME);
515MODULE_FIRMWARE(BCM4335_FIRMWARE_NAME);
516MODULE_FIRMWARE(BCM4335_NVRAM_NAME);
517
518struct brcmf_firmware_names {
519 u32 chipid;
520 u32 revmsk;
521 const char *bin;
522 const char *nv;
523};
524
525enum brcmf_firmware_type {
526 BRCMF_FIRMWARE_BIN,
527 BRCMF_FIRMWARE_NVRAM
528};
529
530#define BRCMF_FIRMWARE_NVRAM(name) \
531 name ## _FIRMWARE_NAME, name ## _NVRAM_NAME
532
533static const struct brcmf_firmware_names brcmf_fwname_data[] = {
534 { BCM43143_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM43143) },
535 { BCM43241_CHIP_ID, 0x0000001F, BRCMF_FIRMWARE_NVRAM(BCM43241B0) },
536 { BCM43241_CHIP_ID, 0xFFFFFFE0, BRCMF_FIRMWARE_NVRAM(BCM43241B4) },
537 { BCM4329_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM4329) },
538 { BCM4330_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM4330) },
539 { BCM4334_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM4334) },
540 { BCM4335_CHIP_ID, 0xFFFFFFFF, BRCMF_FIRMWARE_NVRAM(BCM4335) }
541};
542
543
544static const struct firmware *brcmf_sdbrcm_get_fw(struct brcmf_sdio *bus,
545 enum brcmf_firmware_type type)
546{
547 const struct firmware *fw;
548 const char *name;
549 int err, i;
550
551 for (i = 0; i < ARRAY_SIZE(brcmf_fwname_data); i++) {
552 if (brcmf_fwname_data[i].chipid == bus->ci->chip &&
553 brcmf_fwname_data[i].revmsk & BIT(bus->ci->chiprev)) {
554 switch (type) {
555 case BRCMF_FIRMWARE_BIN:
556 name = brcmf_fwname_data[i].bin;
557 break;
558 case BRCMF_FIRMWARE_NVRAM:
559 name = brcmf_fwname_data[i].nv;
560 break;
561 default:
562 brcmf_err("invalid firmware type (%d)\n", type);
563 return NULL;
564 }
565 goto found;
566 }
567 }
568 brcmf_err("Unknown chipid %d [%d]\n",
569 bus->ci->chip, bus->ci->chiprev);
570 return NULL;
571
572found:
573 err = request_firmware(&fw, name, &bus->sdiodev->func[2]->dev);
574 if ((err) || (!fw)) {
575 brcmf_err("fail to request firmware %s (%d)\n", name, err);
576 return NULL;
577 }
578
579 return fw;
580}
581
Arend van Spriel5b435de2011-10-05 13:19:03 +0200582static void pkt_align(struct sk_buff *p, int len, int align)
583{
584 uint datalign;
585 datalign = (unsigned long)(p->data);
586 datalign = roundup(datalign, (align)) - datalign;
587 if (datalign)
588 skb_pull(p, datalign);
589 __skb_trim(p, len);
590}
591
592/* To check if there's window offered */
Franky Line92eedf2011-11-22 17:21:50 -0800593static bool data_ok(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200594{
595 return (u8)(bus->tx_max - bus->tx_seq) != 0 &&
596 ((u8)(bus->tx_max - bus->tx_seq) & 0x80) == 0;
597}
598
599/*
600 * Reads a register in the SDIO hardware block. This block occupies a series of
601 * adresses on the 32 bit backplane bus.
602 */
Franky Lin58692752012-05-04 18:27:36 -0700603static int
604r_sdreg32(struct brcmf_sdio *bus, u32 *regvar, u32 offset)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200605{
Franky Lin99ba15c2011-11-04 22:23:42 +0100606 u8 idx = brcmf_sdio_chip_getinfidx(bus->ci, BCMA_CORE_SDIO_DEV);
Franky Lin79ae3952012-05-04 18:27:34 -0700607 int ret;
Franky Lin58692752012-05-04 18:27:36 -0700608
609 *regvar = brcmf_sdio_regrl(bus->sdiodev,
610 bus->ci->c_inf[idx].base + offset, &ret);
611
612 return ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200613}
614
Franky Lin58692752012-05-04 18:27:36 -0700615static int
616w_sdreg32(struct brcmf_sdio *bus, u32 regval, u32 reg_offset)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200617{
Franky Lin99ba15c2011-11-04 22:23:42 +0100618 u8 idx = brcmf_sdio_chip_getinfidx(bus->ci, BCMA_CORE_SDIO_DEV);
Franky Line13ce262012-05-04 18:27:35 -0700619 int ret;
Franky Lin58692752012-05-04 18:27:36 -0700620
621 brcmf_sdio_regwl(bus->sdiodev,
622 bus->ci->c_inf[idx].base + reg_offset,
623 regval, &ret);
624
625 return ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200626}
627
Piotr Haber4a3da992013-04-11 13:28:46 +0200628static int
629brcmf_sdbrcm_kso_control(struct brcmf_sdio *bus, bool on)
630{
631 u8 wr_val = 0, rd_val, cmp_val, bmask;
632 int err = 0;
633 int try_cnt = 0;
634
635 brcmf_dbg(TRACE, "Enter\n");
636
637 wr_val = (on << SBSDIO_FUNC1_SLEEPCSR_KSO_SHIFT);
638 /* 1st KSO write goes to AOS wake up core if device is asleep */
639 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
640 wr_val, &err);
641 if (err) {
642 brcmf_err("SDIO_AOS KSO write error: %d\n", err);
643 return err;
644 }
645
646 if (on) {
647 /* device WAKEUP through KSO:
648 * write bit 0 & read back until
649 * both bits 0 (kso bit) & 1 (dev on status) are set
650 */
651 cmp_val = SBSDIO_FUNC1_SLEEPCSR_KSO_MASK |
652 SBSDIO_FUNC1_SLEEPCSR_DEVON_MASK;
653 bmask = cmp_val;
654 usleep_range(2000, 3000);
655 } else {
656 /* Put device to sleep, turn off KSO */
657 cmp_val = 0;
658 /* only check for bit0, bit1(dev on status) may not
659 * get cleared right away
660 */
661 bmask = SBSDIO_FUNC1_SLEEPCSR_KSO_MASK;
662 }
663
664 do {
665 /* reliable KSO bit set/clr:
666 * the sdiod sleep write access is synced to PMU 32khz clk
667 * just one write attempt may fail,
668 * read it back until it matches written value
669 */
670 rd_val = brcmf_sdio_regrb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
671 &err);
672 if (((rd_val & bmask) == cmp_val) && !err)
673 break;
674 brcmf_dbg(SDIO, "KSO wr/rd retry:%d (max: %d) ERR:%x\n",
675 try_cnt, MAX_KSO_ATTEMPTS, err);
676 udelay(KSO_WAIT_US);
677 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
678 wr_val, &err);
679 } while (try_cnt++ < MAX_KSO_ATTEMPTS);
680
681 return err;
682}
683
Arend van Spriel5b435de2011-10-05 13:19:03 +0200684#define PKT_AVAILABLE() (intstatus & I_HMB_FRAME_IND)
685
686#define HOSTINTMASK (I_HMB_SW_MASK | I_CHIPACTIVE)
687
Arend van Spriel5b435de2011-10-05 13:19:03 +0200688/* Turn backplane clock on or off */
Franky Line92eedf2011-11-22 17:21:50 -0800689static int brcmf_sdbrcm_htclk(struct brcmf_sdio *bus, bool on, bool pendok)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200690{
691 int err;
692 u8 clkctl, clkreq, devctl;
693 unsigned long timeout;
694
Arend van Sprielc3203372013-04-03 12:40:44 +0200695 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200696
697 clkctl = 0;
698
Piotr Haber4a3da992013-04-11 13:28:46 +0200699 if (bus->sr_enabled) {
700 bus->clkstate = (on ? CLK_AVAIL : CLK_SDONLY);
701 return 0;
702 }
703
Arend van Spriel5b435de2011-10-05 13:19:03 +0200704 if (on) {
705 /* Request HT Avail */
706 clkreq =
707 bus->alp_only ? SBSDIO_ALP_AVAIL_REQ : SBSDIO_HT_AVAIL_REQ;
708
Franky Lin3bba8292012-05-04 18:27:33 -0700709 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
710 clkreq, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200711 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100712 brcmf_err("HT Avail request error: %d\n", err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200713 return -EBADE;
714 }
715
Arend van Spriel5b435de2011-10-05 13:19:03 +0200716 /* Check current status */
Franky Lin45db3392012-05-04 18:27:32 -0700717 clkctl = brcmf_sdio_regrb(bus->sdiodev,
718 SBSDIO_FUNC1_CHIPCLKCSR, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200719 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100720 brcmf_err("HT Avail read error: %d\n", err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200721 return -EBADE;
722 }
723
724 /* Go to pending and await interrupt if appropriate */
725 if (!SBSDIO_CLKAV(clkctl, bus->alp_only) && pendok) {
726 /* Allow only clock-available interrupt */
Franky Lin45db3392012-05-04 18:27:32 -0700727 devctl = brcmf_sdio_regrb(bus->sdiodev,
728 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200729 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100730 brcmf_err("Devctl error setting CA: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200731 err);
732 return -EBADE;
733 }
734
735 devctl |= SBSDIO_DEVCTL_CA_INT_ONLY;
Franky Lin3bba8292012-05-04 18:27:33 -0700736 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
737 devctl, &err);
Arend van Sprielc3203372013-04-03 12:40:44 +0200738 brcmf_dbg(SDIO, "CLKCTL: set PENDING\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200739 bus->clkstate = CLK_PENDING;
740
741 return 0;
742 } else if (bus->clkstate == CLK_PENDING) {
743 /* Cancel CA-only interrupt filter */
Franky Lin45db3392012-05-04 18:27:32 -0700744 devctl = brcmf_sdio_regrb(bus->sdiodev,
Arend van Spriel5b435de2011-10-05 13:19:03 +0200745 SBSDIO_DEVICE_CTL, &err);
746 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
Franky Lin3bba8292012-05-04 18:27:33 -0700747 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
748 devctl, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200749 }
750
751 /* Otherwise, wait here (polling) for HT Avail */
752 timeout = jiffies +
753 msecs_to_jiffies(PMU_MAX_TRANSITION_DLY/1000);
754 while (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
Franky Lin45db3392012-05-04 18:27:32 -0700755 clkctl = brcmf_sdio_regrb(bus->sdiodev,
756 SBSDIO_FUNC1_CHIPCLKCSR,
757 &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200758 if (time_after(jiffies, timeout))
759 break;
760 else
761 usleep_range(5000, 10000);
762 }
763 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100764 brcmf_err("HT Avail request error: %d\n", err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200765 return -EBADE;
766 }
767 if (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100768 brcmf_err("HT Avail timeout (%d): clkctl 0x%02x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200769 PMU_MAX_TRANSITION_DLY, clkctl);
770 return -EBADE;
771 }
772
773 /* Mark clock available */
774 bus->clkstate = CLK_AVAIL;
Arend van Sprielc3203372013-04-03 12:40:44 +0200775 brcmf_dbg(SDIO, "CLKCTL: turned ON\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200776
Joe Perches8ae74652012-01-15 00:38:38 -0800777#if defined(DEBUG)
Joe Perches23677ce2012-02-09 11:17:23 +0000778 if (!bus->alp_only) {
Arend van Spriel5b435de2011-10-05 13:19:03 +0200779 if (SBSDIO_ALPONLY(clkctl))
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100780 brcmf_err("HT Clock should be on\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200781 }
Joe Perches8ae74652012-01-15 00:38:38 -0800782#endif /* defined (DEBUG) */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200783
784 bus->activity = true;
785 } else {
786 clkreq = 0;
787
788 if (bus->clkstate == CLK_PENDING) {
789 /* Cancel CA-only interrupt filter */
Franky Lin45db3392012-05-04 18:27:32 -0700790 devctl = brcmf_sdio_regrb(bus->sdiodev,
791 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200792 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
Franky Lin3bba8292012-05-04 18:27:33 -0700793 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
794 devctl, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200795 }
796
797 bus->clkstate = CLK_SDONLY;
Franky Lin3bba8292012-05-04 18:27:33 -0700798 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
799 clkreq, &err);
Arend van Sprielc3203372013-04-03 12:40:44 +0200800 brcmf_dbg(SDIO, "CLKCTL: turned OFF\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200801 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100802 brcmf_err("Failed access turning clock off: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200803 err);
804 return -EBADE;
805 }
806 }
807 return 0;
808}
809
810/* Change idle/active SD state */
Franky Line92eedf2011-11-22 17:21:50 -0800811static int brcmf_sdbrcm_sdclk(struct brcmf_sdio *bus, bool on)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200812{
Arend van Sprielc3203372013-04-03 12:40:44 +0200813 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200814
815 if (on)
816 bus->clkstate = CLK_SDONLY;
817 else
818 bus->clkstate = CLK_NONE;
819
820 return 0;
821}
822
823/* Transition SD and backplane clock readiness */
Franky Line92eedf2011-11-22 17:21:50 -0800824static int brcmf_sdbrcm_clkctl(struct brcmf_sdio *bus, uint target, bool pendok)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200825{
Joe Perches8ae74652012-01-15 00:38:38 -0800826#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200827 uint oldstate = bus->clkstate;
Joe Perches8ae74652012-01-15 00:38:38 -0800828#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200829
Arend van Sprielc3203372013-04-03 12:40:44 +0200830 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200831
832 /* Early exit if we're already there */
833 if (bus->clkstate == target) {
834 if (target == CLK_AVAIL) {
835 brcmf_sdbrcm_wd_timer(bus, BRCMF_WD_POLL_MS);
836 bus->activity = true;
837 }
838 return 0;
839 }
840
841 switch (target) {
842 case CLK_AVAIL:
843 /* Make sure SD clock is available */
844 if (bus->clkstate == CLK_NONE)
845 brcmf_sdbrcm_sdclk(bus, true);
846 /* Now request HT Avail on the backplane */
847 brcmf_sdbrcm_htclk(bus, true, pendok);
848 brcmf_sdbrcm_wd_timer(bus, BRCMF_WD_POLL_MS);
849 bus->activity = true;
850 break;
851
852 case CLK_SDONLY:
853 /* Remove HT request, or bring up SD clock */
854 if (bus->clkstate == CLK_NONE)
855 brcmf_sdbrcm_sdclk(bus, true);
856 else if (bus->clkstate == CLK_AVAIL)
857 brcmf_sdbrcm_htclk(bus, false, false);
858 else
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100859 brcmf_err("request for %d -> %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200860 bus->clkstate, target);
861 brcmf_sdbrcm_wd_timer(bus, BRCMF_WD_POLL_MS);
862 break;
863
864 case CLK_NONE:
865 /* Make sure to remove HT request */
866 if (bus->clkstate == CLK_AVAIL)
867 brcmf_sdbrcm_htclk(bus, false, false);
868 /* Now remove the SD clock */
869 brcmf_sdbrcm_sdclk(bus, false);
870 brcmf_sdbrcm_wd_timer(bus, 0);
871 break;
872 }
Joe Perches8ae74652012-01-15 00:38:38 -0800873#ifdef DEBUG
Arend van Sprielc3203372013-04-03 12:40:44 +0200874 brcmf_dbg(SDIO, "%d -> %d\n", oldstate, bus->clkstate);
Joe Perches8ae74652012-01-15 00:38:38 -0800875#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200876
877 return 0;
878}
879
Piotr Haber4a3da992013-04-11 13:28:46 +0200880static int
881brcmf_sdbrcm_bus_sleep(struct brcmf_sdio *bus, bool sleep, bool pendok)
882{
883 int err = 0;
884 brcmf_dbg(TRACE, "Enter\n");
885 brcmf_dbg(SDIO, "request %s currently %s\n",
886 (sleep ? "SLEEP" : "WAKE"),
887 (bus->sleeping ? "SLEEP" : "WAKE"));
888
889 /* If SR is enabled control bus state with KSO */
890 if (bus->sr_enabled) {
891 /* Done if we're already in the requested state */
892 if (sleep == bus->sleeping)
893 goto end;
894
895 /* Going to sleep */
896 if (sleep) {
897 /* Don't sleep if something is pending */
898 if (atomic_read(&bus->intstatus) ||
899 atomic_read(&bus->ipend) > 0 ||
900 (!atomic_read(&bus->fcstate) &&
901 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) &&
902 data_ok(bus)))
903 return -EBUSY;
904 err = brcmf_sdbrcm_kso_control(bus, false);
905 /* disable watchdog */
906 if (!err)
907 brcmf_sdbrcm_wd_timer(bus, 0);
908 } else {
909 bus->idlecount = 0;
910 err = brcmf_sdbrcm_kso_control(bus, true);
911 }
912 if (!err) {
913 /* Change state */
914 bus->sleeping = sleep;
915 brcmf_dbg(SDIO, "new state %s\n",
916 (sleep ? "SLEEP" : "WAKE"));
917 } else {
918 brcmf_err("error while changing bus sleep state %d\n",
919 err);
920 return err;
921 }
922 }
923
924end:
925 /* control clocks */
926 if (sleep) {
927 if (!bus->sr_enabled)
928 brcmf_sdbrcm_clkctl(bus, CLK_NONE, pendok);
929 } else {
930 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, pendok);
931 }
932
933 return err;
934
935}
936
Franky Line92eedf2011-11-22 17:21:50 -0800937static u32 brcmf_sdbrcm_hostmail(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200938{
939 u32 intstatus = 0;
940 u32 hmb_data;
941 u8 fcbits;
Franky Lin58692752012-05-04 18:27:36 -0700942 int ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200943
Arend van Sprielc3203372013-04-03 12:40:44 +0200944 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200945
946 /* Read mailbox data and ack that we did so */
Franky Lin58692752012-05-04 18:27:36 -0700947 ret = r_sdreg32(bus, &hmb_data,
948 offsetof(struct sdpcmd_regs, tohostmailboxdata));
Arend van Spriel5b435de2011-10-05 13:19:03 +0200949
Franky Lin58692752012-05-04 18:27:36 -0700950 if (ret == 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200951 w_sdreg32(bus, SMB_INT_ACK,
Franky Lin58692752012-05-04 18:27:36 -0700952 offsetof(struct sdpcmd_regs, tosbmailbox));
Arend van Spriel80969832012-06-09 22:51:44 +0200953 bus->sdcnt.f1regdata += 2;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200954
955 /* Dongle recomposed rx frames, accept them again */
956 if (hmb_data & HMB_DATA_NAKHANDLED) {
Arend van Sprielc3203372013-04-03 12:40:44 +0200957 brcmf_dbg(SDIO, "Dongle reports NAK handled, expect rtx of %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200958 bus->rx_seq);
959 if (!bus->rxskip)
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100960 brcmf_err("unexpected NAKHANDLED!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200961
962 bus->rxskip = false;
963 intstatus |= I_HMB_FRAME_IND;
964 }
965
966 /*
967 * DEVREADY does not occur with gSPI.
968 */
969 if (hmb_data & (HMB_DATA_DEVREADY | HMB_DATA_FWREADY)) {
970 bus->sdpcm_ver =
971 (hmb_data & HMB_DATA_VERSION_MASK) >>
972 HMB_DATA_VERSION_SHIFT;
973 if (bus->sdpcm_ver != SDPCM_PROT_VERSION)
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100974 brcmf_err("Version mismatch, dongle reports %d, "
Arend van Spriel5b435de2011-10-05 13:19:03 +0200975 "expecting %d\n",
976 bus->sdpcm_ver, SDPCM_PROT_VERSION);
977 else
Arend van Sprielc3203372013-04-03 12:40:44 +0200978 brcmf_dbg(SDIO, "Dongle ready, protocol version %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200979 bus->sdpcm_ver);
980 }
981
982 /*
983 * Flow Control has been moved into the RX headers and this out of band
984 * method isn't used any more.
985 * remaining backward compatible with older dongles.
986 */
987 if (hmb_data & HMB_DATA_FC) {
988 fcbits = (hmb_data & HMB_DATA_FCDATA_MASK) >>
989 HMB_DATA_FCDATA_SHIFT;
990
991 if (fcbits & ~bus->flowcontrol)
Arend van Spriel80969832012-06-09 22:51:44 +0200992 bus->sdcnt.fc_xoff++;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200993
994 if (bus->flowcontrol & ~fcbits)
Arend van Spriel80969832012-06-09 22:51:44 +0200995 bus->sdcnt.fc_xon++;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200996
Arend van Spriel80969832012-06-09 22:51:44 +0200997 bus->sdcnt.fc_rcvd++;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200998 bus->flowcontrol = fcbits;
999 }
1000
1001 /* Shouldn't be any others */
1002 if (hmb_data & ~(HMB_DATA_DEVREADY |
1003 HMB_DATA_NAKHANDLED |
1004 HMB_DATA_FC |
1005 HMB_DATA_FWREADY |
1006 HMB_DATA_FCDATA_MASK | HMB_DATA_VERSION_MASK))
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001007 brcmf_err("Unknown mailbox data content: 0x%02x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001008 hmb_data);
1009
1010 return intstatus;
1011}
1012
Franky Line92eedf2011-11-22 17:21:50 -08001013static void brcmf_sdbrcm_rxfail(struct brcmf_sdio *bus, bool abort, bool rtx)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001014{
1015 uint retries = 0;
1016 u16 lastrbc;
1017 u8 hi, lo;
1018 int err;
1019
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001020 brcmf_err("%sterminate frame%s\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001021 abort ? "abort command, " : "",
1022 rtx ? ", send NAK" : "");
1023
1024 if (abort)
1025 brcmf_sdcard_abort(bus->sdiodev, SDIO_FUNC_2);
1026
Franky Lin3bba8292012-05-04 18:27:33 -07001027 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL,
1028 SFC_RF_TERM, &err);
Arend van Spriel80969832012-06-09 22:51:44 +02001029 bus->sdcnt.f1regdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001030
1031 /* Wait until the packet has been flushed (device/FIFO stable) */
1032 for (lastrbc = retries = 0xffff; retries > 0; retries--) {
Franky Lin45db3392012-05-04 18:27:32 -07001033 hi = brcmf_sdio_regrb(bus->sdiodev,
Franky Lin5c15c232012-05-04 18:27:37 -07001034 SBSDIO_FUNC1_RFRAMEBCHI, &err);
Franky Lin45db3392012-05-04 18:27:32 -07001035 lo = brcmf_sdio_regrb(bus->sdiodev,
Franky Lin5c15c232012-05-04 18:27:37 -07001036 SBSDIO_FUNC1_RFRAMEBCLO, &err);
Arend van Spriel80969832012-06-09 22:51:44 +02001037 bus->sdcnt.f1regdata += 2;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001038
1039 if ((hi == 0) && (lo == 0))
1040 break;
1041
1042 if ((hi > (lastrbc >> 8)) && (lo > (lastrbc & 0x00ff))) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001043 brcmf_err("count growing: last 0x%04x now 0x%04x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001044 lastrbc, (hi << 8) + lo);
1045 }
1046 lastrbc = (hi << 8) + lo;
1047 }
1048
1049 if (!retries)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001050 brcmf_err("count never zeroed: last 0x%04x\n", lastrbc);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001051 else
Arend van Sprielc3203372013-04-03 12:40:44 +02001052 brcmf_dbg(SDIO, "flush took %d iterations\n", 0xffff - retries);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001053
1054 if (rtx) {
Arend van Spriel80969832012-06-09 22:51:44 +02001055 bus->sdcnt.rxrtx++;
Franky Lin58692752012-05-04 18:27:36 -07001056 err = w_sdreg32(bus, SMB_NAK,
1057 offsetof(struct sdpcmd_regs, tosbmailbox));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001058
Arend van Spriel80969832012-06-09 22:51:44 +02001059 bus->sdcnt.f1regdata++;
Franky Lin58692752012-05-04 18:27:36 -07001060 if (err == 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001061 bus->rxskip = true;
1062 }
1063
1064 /* Clear partial in any case */
Franky Lin4754fce2012-09-19 22:21:11 +02001065 bus->cur_read.len = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001066
1067 /* If we can't reach the device, signal failure */
Franky Lin5c15c232012-05-04 18:27:37 -07001068 if (err)
Franky Lin712ac5b2011-12-16 18:37:09 -08001069 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001070}
1071
Arend van Spriel9a95e602011-11-10 20:30:29 +01001072/* return total length of buffer chain */
Franky Line92eedf2011-11-22 17:21:50 -08001073static uint brcmf_sdbrcm_glom_len(struct brcmf_sdio *bus)
Arend van Spriel9a95e602011-11-10 20:30:29 +01001074{
1075 struct sk_buff *p;
1076 uint total;
1077
1078 total = 0;
1079 skb_queue_walk(&bus->glom, p)
1080 total += p->len;
1081 return total;
1082}
1083
Franky Line92eedf2011-11-22 17:21:50 -08001084static void brcmf_sdbrcm_free_glom(struct brcmf_sdio *bus)
Arend van Spriel046808d2011-11-10 20:30:31 +01001085{
1086 struct sk_buff *cur, *next;
1087
1088 skb_queue_walk_safe(&bus->glom, cur, next) {
1089 skb_unlink(cur, &bus->glom);
1090 brcmu_pkt_buf_free_skb(cur);
1091 }
1092}
1093
Franky Lin6bc52312013-08-10 12:27:28 +02001094/**
1095 * brcmfmac sdio bus specific header
1096 * This is the lowest layer header wrapped on the packets transmitted between
1097 * host and WiFi dongle which contains information needed for SDIO core and
1098 * firmware
1099 *
1100 * It consists of 2 parts: hw header and software header
1101 * hardware header (frame tag) - 4 bytes
1102 * Byte 0~1: Frame length
1103 * Byte 2~3: Checksum, bit-wise inverse of frame length
1104 * software header - 8 bytes
1105 * Byte 0: Rx/Tx sequence number
1106 * Byte 1: 4 MSB Channel number, 4 LSB arbitrary flag
1107 * Byte 2: Length of next data frame, reserved for Tx
1108 * Byte 3: Data offset
1109 * Byte 4: Flow control bits, reserved for Tx
1110 * Byte 5: Maximum Sequence number allowed by firmware for Tx, N/A for Tx packet
1111 * Byte 6~7: Reserved
1112 */
1113#define SDPCM_HWHDR_LEN 4
1114#define SDPCM_SWHDR_LEN 8
1115#define SDPCM_HDRLEN (SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN)
Franky Lin6bc52312013-08-10 12:27:28 +02001116/* software header */
1117#define SDPCM_SEQ_MASK 0x000000ff
1118#define SDPCM_SEQ_WRAP 256
1119#define SDPCM_CHANNEL_MASK 0x00000f00
1120#define SDPCM_CHANNEL_SHIFT 8
1121#define SDPCM_CONTROL_CHANNEL 0 /* Control */
1122#define SDPCM_EVENT_CHANNEL 1 /* Asyc Event Indication */
1123#define SDPCM_DATA_CHANNEL 2 /* Data Xmit/Recv */
1124#define SDPCM_GLOM_CHANNEL 3 /* Coalesced packets */
1125#define SDPCM_TEST_CHANNEL 15 /* Test/debug packets */
1126#define SDPCM_GLOMDESC(p) (((u8 *)p)[1] & 0x80)
1127#define SDPCM_NEXTLEN_MASK 0x00ff0000
1128#define SDPCM_NEXTLEN_SHIFT 16
1129#define SDPCM_DOFFSET_MASK 0xff000000
1130#define SDPCM_DOFFSET_SHIFT 24
1131#define SDPCM_FCMASK_MASK 0x000000ff
1132#define SDPCM_WINDOW_MASK 0x0000ff00
1133#define SDPCM_WINDOW_SHIFT 8
1134
1135static inline u8 brcmf_sdio_getdatoffset(u8 *swheader)
1136{
1137 u32 hdrvalue;
1138 hdrvalue = *(u32 *)swheader;
1139 return (u8)((hdrvalue & SDPCM_DOFFSET_MASK) >> SDPCM_DOFFSET_SHIFT);
1140}
1141
1142static int brcmf_sdio_hdparse(struct brcmf_sdio *bus, u8 *header,
1143 struct brcmf_sdio_hdrinfo *rd,
1144 enum brcmf_sdio_frmtype type)
Franky Lin4754fce2012-09-19 22:21:11 +02001145{
1146 u16 len, checksum;
1147 u8 rx_seq, fc, tx_seq_max;
Franky Lin6bc52312013-08-10 12:27:28 +02001148 u32 swheader;
Franky Lin4754fce2012-09-19 22:21:11 +02001149
Franky Lin6bc52312013-08-10 12:27:28 +02001150 /* hw header */
Franky Lin4754fce2012-09-19 22:21:11 +02001151 len = get_unaligned_le16(header);
1152 checksum = get_unaligned_le16(header + sizeof(u16));
1153 /* All zero means no more to read */
1154 if (!(len | checksum)) {
1155 bus->rxpending = false;
Franky Lin10510582012-11-05 16:22:26 -08001156 return -ENODATA;
Franky Lin4754fce2012-09-19 22:21:11 +02001157 }
1158 if ((u16)(~(len ^ checksum))) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001159 brcmf_err("HW header checksum error\n");
Franky Lin4754fce2012-09-19 22:21:11 +02001160 bus->sdcnt.rx_badhdr++;
1161 brcmf_sdbrcm_rxfail(bus, false, false);
Franky Lin10510582012-11-05 16:22:26 -08001162 return -EIO;
Franky Lin4754fce2012-09-19 22:21:11 +02001163 }
1164 if (len < SDPCM_HDRLEN) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001165 brcmf_err("HW header length error\n");
Franky Lin10510582012-11-05 16:22:26 -08001166 return -EPROTO;
Franky Lin4754fce2012-09-19 22:21:11 +02001167 }
Franky Lin9d7d6f92012-10-22 10:36:24 -07001168 if (type == BRCMF_SDIO_FT_SUPER &&
1169 (roundup(len, bus->blocksize) != rd->len)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001170 brcmf_err("HW superframe header length error\n");
Franky Lin10510582012-11-05 16:22:26 -08001171 return -EPROTO;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001172 }
1173 if (type == BRCMF_SDIO_FT_SUB && len > rd->len) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001174 brcmf_err("HW subframe header length error\n");
Franky Lin10510582012-11-05 16:22:26 -08001175 return -EPROTO;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001176 }
Franky Lin4754fce2012-09-19 22:21:11 +02001177 rd->len = len;
1178
Franky Lin6bc52312013-08-10 12:27:28 +02001179 /* software header */
1180 header += SDPCM_HWHDR_LEN;
1181 swheader = le32_to_cpu(*(__le32 *)header);
1182 if (type == BRCMF_SDIO_FT_SUPER && SDPCM_GLOMDESC(header)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001183 brcmf_err("Glom descriptor found in superframe head\n");
Franky Lin9d7d6f92012-10-22 10:36:24 -07001184 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001185 return -EINVAL;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001186 }
Franky Lin6bc52312013-08-10 12:27:28 +02001187 rx_seq = (u8)(swheader & SDPCM_SEQ_MASK);
1188 rd->channel = (swheader & SDPCM_CHANNEL_MASK) >> SDPCM_CHANNEL_SHIFT;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001189 if (len > MAX_RX_DATASZ && rd->channel != SDPCM_CONTROL_CHANNEL &&
1190 type != BRCMF_SDIO_FT_SUPER) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001191 brcmf_err("HW header length too long\n");
Franky Lin4754fce2012-09-19 22:21:11 +02001192 bus->sdcnt.rx_toolong++;
1193 brcmf_sdbrcm_rxfail(bus, false, false);
1194 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001195 return -EPROTO;
Franky Lin4754fce2012-09-19 22:21:11 +02001196 }
Franky Lin9d7d6f92012-10-22 10:36:24 -07001197 if (type == BRCMF_SDIO_FT_SUPER && rd->channel != SDPCM_GLOM_CHANNEL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001198 brcmf_err("Wrong channel for superframe\n");
Franky Lin9d7d6f92012-10-22 10:36:24 -07001199 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001200 return -EINVAL;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001201 }
1202 if (type == BRCMF_SDIO_FT_SUB && rd->channel != SDPCM_DATA_CHANNEL &&
1203 rd->channel != SDPCM_EVENT_CHANNEL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001204 brcmf_err("Wrong channel for subframe\n");
Franky Lin9d7d6f92012-10-22 10:36:24 -07001205 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001206 return -EINVAL;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001207 }
Franky Lin6bc52312013-08-10 12:27:28 +02001208 rd->dat_offset = brcmf_sdio_getdatoffset(header);
Franky Lin4754fce2012-09-19 22:21:11 +02001209 if (rd->dat_offset < SDPCM_HDRLEN || rd->dat_offset > rd->len) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001210 brcmf_err("seq %d: bad data offset\n", rx_seq);
Franky Lin4754fce2012-09-19 22:21:11 +02001211 bus->sdcnt.rx_badhdr++;
1212 brcmf_sdbrcm_rxfail(bus, false, false);
1213 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001214 return -ENXIO;
Franky Lin4754fce2012-09-19 22:21:11 +02001215 }
1216 if (rd->seq_num != rx_seq) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001217 brcmf_err("seq %d: sequence number error, expect %d\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001218 rx_seq, rd->seq_num);
1219 bus->sdcnt.rx_badseq++;
1220 rd->seq_num = rx_seq;
1221 }
Franky Lin9d7d6f92012-10-22 10:36:24 -07001222 /* no need to check the reset for subframe */
1223 if (type == BRCMF_SDIO_FT_SUB)
Franky Lin10510582012-11-05 16:22:26 -08001224 return 0;
Franky Lin6bc52312013-08-10 12:27:28 +02001225 rd->len_nxtfrm = (swheader & SDPCM_NEXTLEN_MASK) >> SDPCM_NEXTLEN_SHIFT;
Franky Lin4754fce2012-09-19 22:21:11 +02001226 if (rd->len_nxtfrm << 4 > MAX_RX_DATASZ) {
1227 /* only warm for NON glom packet */
1228 if (rd->channel != SDPCM_GLOM_CHANNEL)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001229 brcmf_err("seq %d: next length error\n", rx_seq);
Franky Lin4754fce2012-09-19 22:21:11 +02001230 rd->len_nxtfrm = 0;
1231 }
Franky Lin6bc52312013-08-10 12:27:28 +02001232 swheader = le32_to_cpu(*(__le32 *)(header + 4));
1233 fc = swheader & SDPCM_FCMASK_MASK;
Franky Lin4754fce2012-09-19 22:21:11 +02001234 if (bus->flowcontrol != fc) {
1235 if (~bus->flowcontrol & fc)
1236 bus->sdcnt.fc_xoff++;
1237 if (bus->flowcontrol & ~fc)
1238 bus->sdcnt.fc_xon++;
1239 bus->sdcnt.fc_rcvd++;
1240 bus->flowcontrol = fc;
1241 }
Franky Lin6bc52312013-08-10 12:27:28 +02001242 tx_seq_max = (swheader & SDPCM_WINDOW_MASK) >> SDPCM_WINDOW_SHIFT;
Franky Lin4754fce2012-09-19 22:21:11 +02001243 if ((u8)(tx_seq_max - bus->tx_seq) > 0x40) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001244 brcmf_err("seq %d: max tx seq number error\n", rx_seq);
Franky Lin4754fce2012-09-19 22:21:11 +02001245 tx_seq_max = bus->tx_seq + 2;
1246 }
1247 bus->tx_max = tx_seq_max;
1248
Franky Lin10510582012-11-05 16:22:26 -08001249 return 0;
Franky Lin4754fce2012-09-19 22:21:11 +02001250}
1251
Franky Lin6bc52312013-08-10 12:27:28 +02001252static inline void brcmf_sdio_update_hwhdr(u8 *header, u16 frm_length)
1253{
1254 *(__le16 *)header = cpu_to_le16(frm_length);
1255 *(((__le16 *)header) + 1) = cpu_to_le16(~frm_length);
1256}
1257
1258static void brcmf_sdio_hdpack(struct brcmf_sdio *bus, u8 *header,
1259 struct brcmf_sdio_hdrinfo *hd_info)
1260{
1261 u32 sw_header;
1262
1263 brcmf_sdio_update_hwhdr(header, hd_info->len);
1264
1265 sw_header = bus->tx_seq;
1266 sw_header |= (hd_info->channel << SDPCM_CHANNEL_SHIFT) &
1267 SDPCM_CHANNEL_MASK;
1268 sw_header |= (hd_info->dat_offset << SDPCM_DOFFSET_SHIFT) &
1269 SDPCM_DOFFSET_MASK;
1270 *(((__le32 *)header) + 1) = cpu_to_le32(sw_header);
1271 *(((__le32 *)header) + 2) = 0;
1272}
1273
Franky Line92eedf2011-11-22 17:21:50 -08001274static u8 brcmf_sdbrcm_rxglom(struct brcmf_sdio *bus, u8 rxseq)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001275{
1276 u16 dlen, totlen;
1277 u8 *dptr, num = 0;
Franky Lincb7f7962013-08-10 12:27:27 +02001278 u32 align = 0;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001279 u16 sublen;
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001280 struct sk_buff *pfirst, *pnext;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001281
1282 int errcode;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001283 u8 doff, sfdoff;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001284
Franky Lin6bc52312013-08-10 12:27:28 +02001285 struct brcmf_sdio_hdrinfo rd_new;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001286
1287 /* If packets, issue read(s) and send up packet chain */
1288 /* Return sequence numbers consumed? */
1289
Arend van Sprielc3203372013-04-03 12:40:44 +02001290 brcmf_dbg(SDIO, "start: glomd %p glom %p\n",
Arend van Sprielb83db862011-10-19 12:51:09 +02001291 bus->glomd, skb_peek(&bus->glom));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001292
Franky Lincb7f7962013-08-10 12:27:27 +02001293 if (bus->sdiodev->pdata)
1294 align = bus->sdiodev->pdata->sd_sgentry_align;
1295 if (align < 4)
1296 align = 4;
1297
Arend van Spriel5b435de2011-10-05 13:19:03 +02001298 /* If there's a descriptor, generate the packet chain */
1299 if (bus->glomd) {
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001300 pfirst = pnext = NULL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001301 dlen = (u16) (bus->glomd->len);
1302 dptr = bus->glomd->data;
1303 if (!dlen || (dlen & 1)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001304 brcmf_err("bad glomd len(%d), ignore descriptor\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001305 dlen);
1306 dlen = 0;
1307 }
1308
1309 for (totlen = num = 0; dlen; num++) {
1310 /* Get (and move past) next length */
1311 sublen = get_unaligned_le16(dptr);
1312 dlen -= sizeof(u16);
1313 dptr += sizeof(u16);
1314 if ((sublen < SDPCM_HDRLEN) ||
1315 ((num == 0) && (sublen < (2 * SDPCM_HDRLEN)))) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001316 brcmf_err("descriptor len %d bad: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001317 num, sublen);
1318 pnext = NULL;
1319 break;
1320 }
Franky Lincb7f7962013-08-10 12:27:27 +02001321 if (sublen % align) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001322 brcmf_err("sublen %d not multiple of %d\n",
Franky Lincb7f7962013-08-10 12:27:27 +02001323 sublen, align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001324 }
1325 totlen += sublen;
1326
1327 /* For last frame, adjust read len so total
1328 is a block multiple */
1329 if (!dlen) {
1330 sublen +=
1331 (roundup(totlen, bus->blocksize) - totlen);
1332 totlen = roundup(totlen, bus->blocksize);
1333 }
1334
1335 /* Allocate/chain packet for next subframe */
Franky Lincb7f7962013-08-10 12:27:27 +02001336 pnext = brcmu_pkt_buf_get_skb(sublen + align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001337 if (pnext == NULL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001338 brcmf_err("bcm_pkt_buf_get_skb failed, num %d len %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001339 num, sublen);
1340 break;
1341 }
Arend van Sprielb83db862011-10-19 12:51:09 +02001342 skb_queue_tail(&bus->glom, pnext);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001343
1344 /* Adhere to start alignment requirements */
Franky Lincb7f7962013-08-10 12:27:27 +02001345 pkt_align(pnext, sublen, align);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001346 }
1347
1348 /* If all allocations succeeded, save packet chain
1349 in bus structure */
1350 if (pnext) {
1351 brcmf_dbg(GLOM, "allocated %d-byte packet chain for %d subframes\n",
1352 totlen, num);
Franky Lin4754fce2012-09-19 22:21:11 +02001353 if (BRCMF_GLOM_ON() && bus->cur_read.len &&
1354 totlen != bus->cur_read.len) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001355 brcmf_dbg(GLOM, "glomdesc mismatch: nextlen %d glomdesc %d rxseq %d\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001356 bus->cur_read.len, totlen, rxseq);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001357 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02001358 pfirst = pnext = NULL;
1359 } else {
Arend van Spriel046808d2011-11-10 20:30:31 +01001360 brcmf_sdbrcm_free_glom(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001361 num = 0;
1362 }
1363
1364 /* Done with descriptor packet */
1365 brcmu_pkt_buf_free_skb(bus->glomd);
1366 bus->glomd = NULL;
Franky Lin4754fce2012-09-19 22:21:11 +02001367 bus->cur_read.len = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001368 }
1369
1370 /* Ok -- either we just generated a packet chain,
1371 or had one from before */
Arend van Sprielb83db862011-10-19 12:51:09 +02001372 if (!skb_queue_empty(&bus->glom)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001373 if (BRCMF_GLOM_ON()) {
1374 brcmf_dbg(GLOM, "try superframe read, packet chain:\n");
Arend van Sprielb83db862011-10-19 12:51:09 +02001375 skb_queue_walk(&bus->glom, pnext) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001376 brcmf_dbg(GLOM, " %p: %p len 0x%04x (%d)\n",
1377 pnext, (u8 *) (pnext->data),
1378 pnext->len, pnext->len);
1379 }
1380 }
1381
Arend van Sprielb83db862011-10-19 12:51:09 +02001382 pfirst = skb_peek(&bus->glom);
Arend van Spriel9a95e602011-11-10 20:30:29 +01001383 dlen = (u16) brcmf_sdbrcm_glom_len(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001384
1385 /* Do an SDIO read for the superframe. Configurable iovar to
1386 * read directly into the chained packet, or allocate a large
1387 * packet and and copy into the chain.
1388 */
Franky Lin38b0b0d2012-11-05 16:22:24 -08001389 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin354b75b2013-06-18 13:29:29 +02001390 errcode = brcmf_sdcard_recv_chain(bus->sdiodev,
1391 bus->sdiodev->sbwad,
1392 SDIO_FUNC_2, F2SYNC, &bus->glom);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001393 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel80969832012-06-09 22:51:44 +02001394 bus->sdcnt.f2rxdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001395
1396 /* On failure, kill the superframe, allow a couple retries */
1397 if (errcode < 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001398 brcmf_err("glom read of %d bytes failed: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001399 dlen, errcode);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001400
Franky Lin38b0b0d2012-11-05 16:22:24 -08001401 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001402 if (bus->glomerr++ < 3) {
1403 brcmf_sdbrcm_rxfail(bus, true, true);
1404 } else {
1405 bus->glomerr = 0;
1406 brcmf_sdbrcm_rxfail(bus, true, false);
Arend van Spriel80969832012-06-09 22:51:44 +02001407 bus->sdcnt.rxglomfail++;
Arend van Spriel046808d2011-11-10 20:30:31 +01001408 brcmf_sdbrcm_free_glom(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001409 }
Franky Lin38b0b0d2012-11-05 16:22:24 -08001410 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001411 return 0;
1412 }
Joe Perches1e023822012-01-15 00:38:40 -08001413
1414 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
1415 pfirst->data, min_t(int, pfirst->len, 48),
1416 "SUPERFRAME:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001417
Franky Lin9d7d6f92012-10-22 10:36:24 -07001418 rd_new.seq_num = rxseq;
1419 rd_new.len = dlen;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001420 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin6bc52312013-08-10 12:27:28 +02001421 errcode = brcmf_sdio_hdparse(bus, pfirst->data, &rd_new,
1422 BRCMF_SDIO_FT_SUPER);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001423 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin9d7d6f92012-10-22 10:36:24 -07001424 bus->cur_read.len = rd_new.len_nxtfrm << 4;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001425
1426 /* Remove superframe header, remember offset */
Franky Lin9d7d6f92012-10-22 10:36:24 -07001427 skb_pull(pfirst, rd_new.dat_offset);
1428 sfdoff = rd_new.dat_offset;
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001429 num = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001430
1431 /* Validate all the subframe headers */
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001432 skb_queue_walk(&bus->glom, pnext) {
1433 /* leave when invalid subframe is found */
1434 if (errcode)
1435 break;
1436
Franky Lin9d7d6f92012-10-22 10:36:24 -07001437 rd_new.len = pnext->len;
1438 rd_new.seq_num = rxseq++;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001439 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin6bc52312013-08-10 12:27:28 +02001440 errcode = brcmf_sdio_hdparse(bus, pnext->data, &rd_new,
1441 BRCMF_SDIO_FT_SUB);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001442 sdio_release_host(bus->sdiodev->func[1]);
Joe Perches1e023822012-01-15 00:38:40 -08001443 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
Franky Lin9d7d6f92012-10-22 10:36:24 -07001444 pnext->data, 32, "subframe:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001445
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001446 num++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001447 }
1448
1449 if (errcode) {
1450 /* Terminate frame on error, request
1451 a couple retries */
Franky Lin38b0b0d2012-11-05 16:22:24 -08001452 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001453 if (bus->glomerr++ < 3) {
1454 /* Restore superframe header space */
1455 skb_push(pfirst, sfdoff);
1456 brcmf_sdbrcm_rxfail(bus, true, true);
1457 } else {
1458 bus->glomerr = 0;
1459 brcmf_sdbrcm_rxfail(bus, true, false);
Arend van Spriel80969832012-06-09 22:51:44 +02001460 bus->sdcnt.rxglomfail++;
Arend van Spriel046808d2011-11-10 20:30:31 +01001461 brcmf_sdbrcm_free_glom(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001462 }
Franky Lin38b0b0d2012-11-05 16:22:24 -08001463 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001464 bus->cur_read.len = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001465 return 0;
1466 }
1467
1468 /* Basic SD framing looks ok - process each packet (header) */
Arend van Spriel5b435de2011-10-05 13:19:03 +02001469
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001470 skb_queue_walk_safe(&bus->glom, pfirst, pnext) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001471 dptr = (u8 *) (pfirst->data);
1472 sublen = get_unaligned_le16(dptr);
Franky Lin6bc52312013-08-10 12:27:28 +02001473 doff = brcmf_sdio_getdatoffset(&dptr[SDPCM_HWHDR_LEN]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001474
Joe Perches1e023822012-01-15 00:38:40 -08001475 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_DATA_ON(),
Franky Lin9d7d6f92012-10-22 10:36:24 -07001476 dptr, pfirst->len,
1477 "Rx Subframe Data:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001478
1479 __skb_trim(pfirst, sublen);
1480 skb_pull(pfirst, doff);
1481
1482 if (pfirst->len == 0) {
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001483 skb_unlink(pfirst, &bus->glom);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001484 brcmu_pkt_buf_free_skb(pfirst);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001485 continue;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001486 }
1487
Joe Perches1e023822012-01-15 00:38:40 -08001488 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
1489 pfirst->data,
1490 min_t(int, pfirst->len, 32),
1491 "subframe %d to stack, %p (%p/%d) nxt/lnk %p/%p\n",
1492 bus->glom.qlen, pfirst, pfirst->data,
1493 pfirst->len, pfirst->next,
1494 pfirst->prev);
Arend van Spriel05f38202013-09-25 13:05:46 +02001495 skb_unlink(pfirst, &bus->glom);
1496 brcmf_rx_frame(bus->sdiodev->dev, pfirst);
1497 bus->sdcnt.rxglompkts++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001498 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02001499
Arend van Spriel80969832012-06-09 22:51:44 +02001500 bus->sdcnt.rxglomframes++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001501 }
1502 return num;
1503}
1504
Franky Line92eedf2011-11-22 17:21:50 -08001505static int brcmf_sdbrcm_dcmd_resp_wait(struct brcmf_sdio *bus, uint *condition,
Arend van Spriel5b435de2011-10-05 13:19:03 +02001506 bool *pending)
1507{
1508 DECLARE_WAITQUEUE(wait, current);
1509 int timeout = msecs_to_jiffies(DCMD_RESP_TIMEOUT);
1510
1511 /* Wait until control frame is available */
1512 add_wait_queue(&bus->dcmd_resp_wait, &wait);
1513 set_current_state(TASK_INTERRUPTIBLE);
1514
1515 while (!(*condition) && (!signal_pending(current) && timeout))
1516 timeout = schedule_timeout(timeout);
1517
1518 if (signal_pending(current))
1519 *pending = true;
1520
1521 set_current_state(TASK_RUNNING);
1522 remove_wait_queue(&bus->dcmd_resp_wait, &wait);
1523
1524 return timeout;
1525}
1526
Franky Line92eedf2011-11-22 17:21:50 -08001527static int brcmf_sdbrcm_dcmd_resp_wake(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001528{
1529 if (waitqueue_active(&bus->dcmd_resp_wait))
1530 wake_up_interruptible(&bus->dcmd_resp_wait);
1531
1532 return 0;
1533}
1534static void
Franky Line92eedf2011-11-22 17:21:50 -08001535brcmf_sdbrcm_read_control(struct brcmf_sdio *bus, u8 *hdr, uint len, uint doff)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001536{
1537 uint rdlen, pad;
Franky Lindd43a012012-11-05 16:22:22 -08001538 u8 *buf = NULL, *rbuf;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001539 int sdret;
1540
1541 brcmf_dbg(TRACE, "Enter\n");
1542
Franky Lindd43a012012-11-05 16:22:22 -08001543 if (bus->rxblen)
1544 buf = vzalloc(bus->rxblen);
Joe Perches14f8dc42013-02-07 11:46:27 +00001545 if (!buf)
Franky Lindd43a012012-11-05 16:22:22 -08001546 goto done;
Joe Perches14f8dc42013-02-07 11:46:27 +00001547
Franky Lindd43a012012-11-05 16:22:22 -08001548 rbuf = bus->rxbuf;
1549 pad = ((unsigned long)rbuf % BRCMF_SDALIGN);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001550 if (pad)
Franky Lindd43a012012-11-05 16:22:22 -08001551 rbuf += (BRCMF_SDALIGN - pad);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001552
1553 /* Copy the already-read portion over */
Franky Lindd43a012012-11-05 16:22:22 -08001554 memcpy(buf, hdr, BRCMF_FIRSTREAD);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001555 if (len <= BRCMF_FIRSTREAD)
1556 goto gotpkt;
1557
1558 /* Raise rdlen to next SDIO block to avoid tail command */
1559 rdlen = len - BRCMF_FIRSTREAD;
1560 if (bus->roundup && bus->blocksize && (rdlen > bus->blocksize)) {
1561 pad = bus->blocksize - (rdlen % bus->blocksize);
1562 if ((pad <= bus->roundup) && (pad < bus->blocksize) &&
Franky Linb01a6b32011-12-16 18:37:03 -08001563 ((len + pad) < bus->sdiodev->bus_if->maxctl))
Arend van Spriel5b435de2011-10-05 13:19:03 +02001564 rdlen += pad;
1565 } else if (rdlen % BRCMF_SDALIGN) {
1566 rdlen += BRCMF_SDALIGN - (rdlen % BRCMF_SDALIGN);
1567 }
1568
1569 /* Satisfy length-alignment requirements */
1570 if (rdlen & (ALIGNMENT - 1))
1571 rdlen = roundup(rdlen, ALIGNMENT);
1572
1573 /* Drop if the read is too big or it exceeds our maximum */
Franky Linb01a6b32011-12-16 18:37:03 -08001574 if ((rdlen + BRCMF_FIRSTREAD) > bus->sdiodev->bus_if->maxctl) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001575 brcmf_err("%d-byte control read exceeds %d-byte buffer\n",
Franky Linb01a6b32011-12-16 18:37:03 -08001576 rdlen, bus->sdiodev->bus_if->maxctl);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001577 brcmf_sdbrcm_rxfail(bus, false, false);
1578 goto done;
1579 }
1580
Franky Linb01a6b32011-12-16 18:37:03 -08001581 if ((len - doff) > bus->sdiodev->bus_if->maxctl) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001582 brcmf_err("%d-byte ctl frame (%d-byte ctl data) exceeds %d-byte limit\n",
Franky Linb01a6b32011-12-16 18:37:03 -08001583 len, len - doff, bus->sdiodev->bus_if->maxctl);
Arend van Spriel80969832012-06-09 22:51:44 +02001584 bus->sdcnt.rx_toolong++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001585 brcmf_sdbrcm_rxfail(bus, false, false);
1586 goto done;
1587 }
1588
Franky Lindd43a012012-11-05 16:22:22 -08001589 /* Read remain of frame body */
Arend van Spriel5b435de2011-10-05 13:19:03 +02001590 sdret = brcmf_sdcard_recv_buf(bus->sdiodev,
1591 bus->sdiodev->sbwad,
1592 SDIO_FUNC_2,
Franky Lindd43a012012-11-05 16:22:22 -08001593 F2SYNC, rbuf, rdlen);
Arend van Spriel80969832012-06-09 22:51:44 +02001594 bus->sdcnt.f2rxdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001595
1596 /* Control frame failures need retransmission */
1597 if (sdret < 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001598 brcmf_err("read %d control bytes failed: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001599 rdlen, sdret);
Arend van Spriel80969832012-06-09 22:51:44 +02001600 bus->sdcnt.rxc_errors++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001601 brcmf_sdbrcm_rxfail(bus, true, true);
1602 goto done;
Franky Lindd43a012012-11-05 16:22:22 -08001603 } else
1604 memcpy(buf + BRCMF_FIRSTREAD, rbuf, rdlen);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001605
1606gotpkt:
1607
Joe Perches1e023822012-01-15 00:38:40 -08001608 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_CTL_ON(),
Franky Lindd43a012012-11-05 16:22:22 -08001609 buf, len, "RxCtrl:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001610
1611 /* Point to valid data and indicate its length */
Franky Lindd43a012012-11-05 16:22:22 -08001612 spin_lock_bh(&bus->rxctl_lock);
1613 if (bus->rxctl) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001614 brcmf_err("last control frame is being processed.\n");
Franky Lindd43a012012-11-05 16:22:22 -08001615 spin_unlock_bh(&bus->rxctl_lock);
1616 vfree(buf);
1617 goto done;
1618 }
1619 bus->rxctl = buf + doff;
1620 bus->rxctl_orig = buf;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001621 bus->rxlen = len - doff;
Franky Lindd43a012012-11-05 16:22:22 -08001622 spin_unlock_bh(&bus->rxctl_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001623
1624done:
1625 /* Awake any waiters */
1626 brcmf_sdbrcm_dcmd_resp_wake(bus);
1627}
1628
1629/* Pad read to blocksize for efficiency */
Franky Line92eedf2011-11-22 17:21:50 -08001630static void brcmf_pad(struct brcmf_sdio *bus, u16 *pad, u16 *rdlen)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001631{
1632 if (bus->roundup && bus->blocksize && *rdlen > bus->blocksize) {
1633 *pad = bus->blocksize - (*rdlen % bus->blocksize);
1634 if (*pad <= bus->roundup && *pad < bus->blocksize &&
1635 *rdlen + *pad + BRCMF_FIRSTREAD < MAX_RX_DATASZ)
1636 *rdlen += *pad;
1637 } else if (*rdlen % BRCMF_SDALIGN) {
1638 *rdlen += BRCMF_SDALIGN - (*rdlen % BRCMF_SDALIGN);
1639 }
1640}
1641
Franky Lin4754fce2012-09-19 22:21:11 +02001642static uint brcmf_sdio_readframes(struct brcmf_sdio *bus, uint maxframes)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001643{
Arend van Spriel5b435de2011-10-05 13:19:03 +02001644 struct sk_buff *pkt; /* Packet for event or data frames */
1645 u16 pad; /* Number of pad bytes to read */
Arend van Spriel5b435de2011-10-05 13:19:03 +02001646 uint rxleft = 0; /* Remaining number of frames allowed */
Arend van Spriel349e7102013-03-03 12:45:28 +01001647 int ret; /* Return code from calls */
Arend van Spriel5b435de2011-10-05 13:19:03 +02001648 uint rxcount = 0; /* Total frames read */
Franky Lin6bc52312013-08-10 12:27:28 +02001649 struct brcmf_sdio_hdrinfo *rd = &bus->cur_read, rd_new;
Franky Lin4754fce2012-09-19 22:21:11 +02001650 u8 head_read = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001651
1652 brcmf_dbg(TRACE, "Enter\n");
1653
1654 /* Not finished unless we encounter no more frames indication */
Franky Lin4754fce2012-09-19 22:21:11 +02001655 bus->rxpending = true;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001656
Franky Lin4754fce2012-09-19 22:21:11 +02001657 for (rd->seq_num = bus->rx_seq, rxleft = maxframes;
Franky Lin8d169aa2011-11-22 17:21:52 -08001658 !bus->rxskip && rxleft &&
Franky Lin712ac5b2011-12-16 18:37:09 -08001659 bus->sdiodev->bus_if->state != BRCMF_BUS_DOWN;
Franky Lin4754fce2012-09-19 22:21:11 +02001660 rd->seq_num++, rxleft--) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001661
1662 /* Handle glomming separately */
Arend van Sprielb83db862011-10-19 12:51:09 +02001663 if (bus->glomd || !skb_queue_empty(&bus->glom)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001664 u8 cnt;
1665 brcmf_dbg(GLOM, "calling rxglom: glomd %p, glom %p\n",
Arend van Sprielb83db862011-10-19 12:51:09 +02001666 bus->glomd, skb_peek(&bus->glom));
Franky Lin4754fce2012-09-19 22:21:11 +02001667 cnt = brcmf_sdbrcm_rxglom(bus, rd->seq_num);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001668 brcmf_dbg(GLOM, "rxglom returned %d\n", cnt);
Franky Lin4754fce2012-09-19 22:21:11 +02001669 rd->seq_num += cnt - 1;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001670 rxleft = (rxleft > cnt) ? (rxleft - cnt) : 1;
1671 continue;
1672 }
1673
Franky Lin4754fce2012-09-19 22:21:11 +02001674 rd->len_left = rd->len;
1675 /* read header first for unknow frame length */
Franky Lin38b0b0d2012-11-05 16:22:24 -08001676 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001677 if (!rd->len) {
Arend van Spriel349e7102013-03-03 12:45:28 +01001678 ret = brcmf_sdcard_recv_buf(bus->sdiodev,
Franky Lin4754fce2012-09-19 22:21:11 +02001679 bus->sdiodev->sbwad,
1680 SDIO_FUNC_2, F2SYNC,
1681 bus->rxhdr,
1682 BRCMF_FIRSTREAD);
1683 bus->sdcnt.f2rxhdrs++;
Arend van Spriel349e7102013-03-03 12:45:28 +01001684 if (ret < 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001685 brcmf_err("RXHEADER FAILED: %d\n",
Arend van Spriel349e7102013-03-03 12:45:28 +01001686 ret);
Franky Lin4754fce2012-09-19 22:21:11 +02001687 bus->sdcnt.rx_hdrfail++;
1688 brcmf_sdbrcm_rxfail(bus, true, true);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001689 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001690 continue;
1691 }
1692
Franky Lin4754fce2012-09-19 22:21:11 +02001693 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() || BRCMF_HDRS_ON(),
1694 bus->rxhdr, SDPCM_HDRLEN,
1695 "RxHdr:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001696
Franky Lin6bc52312013-08-10 12:27:28 +02001697 if (brcmf_sdio_hdparse(bus, bus->rxhdr, rd,
1698 BRCMF_SDIO_FT_NORMAL)) {
Franky Lin38b0b0d2012-11-05 16:22:24 -08001699 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001700 if (!bus->rxpending)
1701 break;
1702 else
1703 continue;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001704 }
1705
Franky Lin4754fce2012-09-19 22:21:11 +02001706 if (rd->channel == SDPCM_CONTROL_CHANNEL) {
1707 brcmf_sdbrcm_read_control(bus, bus->rxhdr,
1708 rd->len,
1709 rd->dat_offset);
1710 /* prepare the descriptor for the next read */
1711 rd->len = rd->len_nxtfrm << 4;
1712 rd->len_nxtfrm = 0;
1713 /* treat all packet as event if we don't know */
1714 rd->channel = SDPCM_EVENT_CHANNEL;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001715 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001716 continue;
1717 }
1718 rd->len_left = rd->len > BRCMF_FIRSTREAD ?
1719 rd->len - BRCMF_FIRSTREAD : 0;
1720 head_read = BRCMF_FIRSTREAD;
1721 }
1722
1723 brcmf_pad(bus, &pad, &rd->len_left);
1724
1725 pkt = brcmu_pkt_buf_get_skb(rd->len_left + head_read +
1726 BRCMF_SDALIGN);
1727 if (!pkt) {
1728 /* Give up on data, request rtx of events */
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001729 brcmf_err("brcmu_pkt_buf_get_skb failed\n");
Franky Lin4754fce2012-09-19 22:21:11 +02001730 brcmf_sdbrcm_rxfail(bus, false,
1731 RETRYCHAN(rd->channel));
Franky Lin38b0b0d2012-11-05 16:22:24 -08001732 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001733 continue;
1734 }
1735 skb_pull(pkt, head_read);
1736 pkt_align(pkt, rd->len_left, BRCMF_SDALIGN);
1737
Arend van Spriel349e7102013-03-03 12:45:28 +01001738 ret = brcmf_sdcard_recv_pkt(bus->sdiodev, bus->sdiodev->sbwad,
Franky Lin4754fce2012-09-19 22:21:11 +02001739 SDIO_FUNC_2, F2SYNC, pkt);
1740 bus->sdcnt.f2rxdata++;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001741 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001742
Arend van Spriel349e7102013-03-03 12:45:28 +01001743 if (ret < 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001744 brcmf_err("read %d bytes from channel %d failed: %d\n",
Arend van Spriel349e7102013-03-03 12:45:28 +01001745 rd->len, rd->channel, ret);
Franky Lin4754fce2012-09-19 22:21:11 +02001746 brcmu_pkt_buf_free_skb(pkt);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001747 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001748 brcmf_sdbrcm_rxfail(bus, true,
1749 RETRYCHAN(rd->channel));
Franky Lin38b0b0d2012-11-05 16:22:24 -08001750 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001751 continue;
1752 }
1753
1754 if (head_read) {
1755 skb_push(pkt, head_read);
1756 memcpy(pkt->data, bus->rxhdr, head_read);
1757 head_read = 0;
1758 } else {
1759 memcpy(bus->rxhdr, pkt->data, SDPCM_HDRLEN);
1760 rd_new.seq_num = rd->seq_num;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001761 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin6bc52312013-08-10 12:27:28 +02001762 if (brcmf_sdio_hdparse(bus, bus->rxhdr, &rd_new,
1763 BRCMF_SDIO_FT_NORMAL)) {
Franky Lin4754fce2012-09-19 22:21:11 +02001764 rd->len = 0;
1765 brcmu_pkt_buf_free_skb(pkt);
1766 }
Arend van Spriel80969832012-06-09 22:51:44 +02001767 bus->sdcnt.rx_readahead_cnt++;
Franky Lin4754fce2012-09-19 22:21:11 +02001768 if (rd->len != roundup(rd_new.len, 16)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001769 brcmf_err("frame length mismatch:read %d, should be %d\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001770 rd->len,
1771 roundup(rd_new.len, 16) >> 4);
1772 rd->len = 0;
1773 brcmf_sdbrcm_rxfail(bus, true, true);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001774 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001775 brcmu_pkt_buf_free_skb(pkt);
1776 continue;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001777 }
Franky Lin38b0b0d2012-11-05 16:22:24 -08001778 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001779 rd->len_nxtfrm = rd_new.len_nxtfrm;
1780 rd->channel = rd_new.channel;
1781 rd->dat_offset = rd_new.dat_offset;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001782
Joe Perches1e023822012-01-15 00:38:40 -08001783 brcmf_dbg_hex_dump(!(BRCMF_BYTES_ON() &&
1784 BRCMF_DATA_ON()) &&
1785 BRCMF_HDRS_ON(),
1786 bus->rxhdr, SDPCM_HDRLEN,
1787 "RxHdr:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001788
Franky Lin4754fce2012-09-19 22:21:11 +02001789 if (rd_new.channel == SDPCM_CONTROL_CHANNEL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001790 brcmf_err("readahead on control packet %d?\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001791 rd_new.seq_num);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001792 /* Force retry w/normal header read */
Franky Lin4754fce2012-09-19 22:21:11 +02001793 rd->len = 0;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001794 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001795 brcmf_sdbrcm_rxfail(bus, false, true);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001796 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001797 brcmu_pkt_buf_free_skb(pkt);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001798 continue;
1799 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02001800 }
1801
Joe Perches1e023822012-01-15 00:38:40 -08001802 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_DATA_ON(),
Franky Lin4754fce2012-09-19 22:21:11 +02001803 pkt->data, rd->len, "Rx Data:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001804
Arend van Spriel5b435de2011-10-05 13:19:03 +02001805 /* Save superframe descriptor and allocate packet frame */
Franky Lin4754fce2012-09-19 22:21:11 +02001806 if (rd->channel == SDPCM_GLOM_CHANNEL) {
Franky Lin6bc52312013-08-10 12:27:28 +02001807 if (SDPCM_GLOMDESC(&bus->rxhdr[SDPCM_HWHDR_LEN])) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001808 brcmf_dbg(GLOM, "glom descriptor, %d bytes:\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001809 rd->len);
Joe Perches1e023822012-01-15 00:38:40 -08001810 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
Franky Lin4754fce2012-09-19 22:21:11 +02001811 pkt->data, rd->len,
Joe Perches1e023822012-01-15 00:38:40 -08001812 "Glom Data:\n");
Franky Lin4754fce2012-09-19 22:21:11 +02001813 __skb_trim(pkt, rd->len);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001814 skb_pull(pkt, SDPCM_HDRLEN);
1815 bus->glomd = pkt;
1816 } else {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001817 brcmf_err("%s: glom superframe w/o "
Arend van Spriel5b435de2011-10-05 13:19:03 +02001818 "descriptor!\n", __func__);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001819 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001820 brcmf_sdbrcm_rxfail(bus, false, false);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001821 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001822 }
Franky Lin4754fce2012-09-19 22:21:11 +02001823 /* prepare the descriptor for the next read */
1824 rd->len = rd->len_nxtfrm << 4;
1825 rd->len_nxtfrm = 0;
1826 /* treat all packet as event if we don't know */
1827 rd->channel = SDPCM_EVENT_CHANNEL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001828 continue;
1829 }
1830
1831 /* Fill in packet len and prio, deliver upward */
Franky Lin4754fce2012-09-19 22:21:11 +02001832 __skb_trim(pkt, rd->len);
1833 skb_pull(pkt, rd->dat_offset);
1834
1835 /* prepare the descriptor for the next read */
1836 rd->len = rd->len_nxtfrm << 4;
1837 rd->len_nxtfrm = 0;
1838 /* treat all packet as event if we don't know */
1839 rd->channel = SDPCM_EVENT_CHANNEL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001840
1841 if (pkt->len == 0) {
1842 brcmu_pkt_buf_free_skb(pkt);
1843 continue;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001844 }
1845
Arend van Spriel05f38202013-09-25 13:05:46 +02001846 brcmf_rx_frame(bus->sdiodev->dev, pkt);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001847 }
Franky Lin4754fce2012-09-19 22:21:11 +02001848
Arend van Spriel5b435de2011-10-05 13:19:03 +02001849 rxcount = maxframes - rxleft;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001850 /* Message if we hit the limit */
1851 if (!rxleft)
Franky Lin4754fce2012-09-19 22:21:11 +02001852 brcmf_dbg(DATA, "hit rx limit of %d frames\n", maxframes);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001853 else
Arend van Spriel5b435de2011-10-05 13:19:03 +02001854 brcmf_dbg(DATA, "processed %d frames\n", rxcount);
1855 /* Back off rxseq if awaiting rtx, update rx_seq */
1856 if (bus->rxskip)
Franky Lin4754fce2012-09-19 22:21:11 +02001857 rd->seq_num--;
1858 bus->rx_seq = rd->seq_num;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001859
1860 return rxcount;
1861}
1862
Arend van Spriel5b435de2011-10-05 13:19:03 +02001863static void
Franky Line92eedf2011-11-22 17:21:50 -08001864brcmf_sdbrcm_wait_event_wakeup(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001865{
1866 if (waitqueue_active(&bus->ctrl_wait))
1867 wake_up_interruptible(&bus->ctrl_wait);
1868 return;
1869}
1870
Franky Lin5491c112013-09-25 13:05:43 +02001871/**
1872 * struct brcmf_skbuff_cb reserves first two bytes in sk_buff::cb for
1873 * bus layer usage.
1874 */
Franky Linb05e9252013-08-10 12:27:26 +02001875/* flag marking a dummy skb added for DMA alignment requirement */
Franky Lin5491c112013-09-25 13:05:43 +02001876#define ALIGN_SKB_FLAG 0x8000
Franky Linb05e9252013-08-10 12:27:26 +02001877/* bit mask of data length chopped from the previous packet */
Franky Lin5491c112013-09-25 13:05:43 +02001878#define ALIGN_SKB_CHOP_LEN_MASK 0x7fff
1879
Franky Linb05e9252013-08-10 12:27:26 +02001880/**
1881 * brcmf_sdio_txpkt_prep - packet preparation for transmit
1882 * @bus: brcmf_sdio structure pointer
1883 * @pktq: packet list pointer
1884 * @chan: virtual channel to transmit the packet
1885 *
1886 * Processes to be applied to the packet
1887 * - Align data buffer pointer
1888 * - Align data buffer length
1889 * - Prepare header
1890 * Return: negative value if there is error
1891 */
1892static int
1893brcmf_sdio_txpkt_prep(struct brcmf_sdio *bus, struct sk_buff_head *pktq,
1894 uint chan)
1895{
Franky Lin6bc52312013-08-10 12:27:28 +02001896 u16 head_pad, tail_pad, tail_chop, head_align, sg_align;
Franky Linb05e9252013-08-10 12:27:26 +02001897 int ntail;
1898 struct sk_buff *pkt_next, *pkt_new;
1899 u8 *dat_buf;
1900 unsigned blksize = bus->sdiodev->func[SDIO_FUNC_2]->cur_blksize;
Franky Lin6bc52312013-08-10 12:27:28 +02001901 struct brcmf_sdio_hdrinfo hd_info = {0};
Franky Linb05e9252013-08-10 12:27:26 +02001902
1903 /* SDIO ADMA requires at least 32 bit alignment */
1904 head_align = 4;
1905 sg_align = 4;
1906 if (bus->sdiodev->pdata) {
1907 head_align = bus->sdiodev->pdata->sd_head_align > 4 ?
1908 bus->sdiodev->pdata->sd_head_align : 4;
1909 sg_align = bus->sdiodev->pdata->sd_sgentry_align > 4 ?
1910 bus->sdiodev->pdata->sd_sgentry_align : 4;
1911 }
1912 /* sg entry alignment should be a divisor of block size */
1913 WARN_ON(blksize % sg_align);
1914
1915 pkt_next = pktq->next;
1916 dat_buf = (u8 *)(pkt_next->data);
1917
1918 /* Check head padding */
1919 head_pad = ((unsigned long)dat_buf % head_align);
1920 if (head_pad) {
1921 if (skb_headroom(pkt_next) < head_pad) {
1922 bus->sdiodev->bus_if->tx_realloc++;
1923 head_pad = 0;
1924 if (skb_cow(pkt_next, head_pad))
1925 return -ENOMEM;
1926 }
1927 skb_push(pkt_next, head_pad);
1928 dat_buf = (u8 *)(pkt_next->data);
Franky Lin706478c2013-08-10 12:27:29 +02001929 memset(dat_buf, 0, head_pad + bus->tx_hdrlen);
Franky Linb05e9252013-08-10 12:27:26 +02001930 }
1931
1932 /* Check tail padding */
1933 pkt_new = NULL;
1934 tail_chop = pkt_next->len % sg_align;
1935 tail_pad = sg_align - tail_chop;
1936 tail_pad += blksize - (pkt_next->len + tail_pad) % blksize;
1937 if (skb_tailroom(pkt_next) < tail_pad && pkt_next->len > blksize) {
1938 pkt_new = brcmu_pkt_buf_get_skb(tail_pad + tail_chop);
1939 if (pkt_new == NULL)
1940 return -ENOMEM;
1941 memcpy(pkt_new->data,
1942 pkt_next->data + pkt_next->len - tail_chop,
1943 tail_chop);
Franky Lin5491c112013-09-25 13:05:43 +02001944 *(u32 *)(pkt_new->cb) = ALIGN_SKB_FLAG + tail_chop;
Franky Linb05e9252013-08-10 12:27:26 +02001945 skb_trim(pkt_next, pkt_next->len - tail_chop);
1946 __skb_queue_after(pktq, pkt_next, pkt_new);
1947 } else {
1948 ntail = pkt_next->data_len + tail_pad -
1949 (pkt_next->end - pkt_next->tail);
1950 if (skb_cloned(pkt_next) || ntail > 0)
1951 if (pskb_expand_head(pkt_next, 0, ntail, GFP_ATOMIC))
1952 return -ENOMEM;
1953 if (skb_linearize(pkt_next))
1954 return -ENOMEM;
1955 dat_buf = (u8 *)(pkt_next->data);
1956 __skb_put(pkt_next, tail_pad);
1957 }
1958
1959 /* Now prep the header */
Franky Linb05e9252013-08-10 12:27:26 +02001960 if (pkt_new)
Franky Lin6bc52312013-08-10 12:27:28 +02001961 hd_info.len = pkt_next->len + tail_chop;
Franky Linb05e9252013-08-10 12:27:26 +02001962 else
Franky Lin6bc52312013-08-10 12:27:28 +02001963 hd_info.len = pkt_next->len - tail_pad;
1964 hd_info.channel = chan;
Franky Lin706478c2013-08-10 12:27:29 +02001965 hd_info.dat_offset = head_pad + bus->tx_hdrlen;
Franky Lin6bc52312013-08-10 12:27:28 +02001966 brcmf_sdio_hdpack(bus, dat_buf, &hd_info);
Franky Linb05e9252013-08-10 12:27:26 +02001967
1968 if (BRCMF_BYTES_ON() &&
1969 ((BRCMF_CTL_ON() && chan == SDPCM_CONTROL_CHANNEL) ||
1970 (BRCMF_DATA_ON() && chan != SDPCM_CONTROL_CHANNEL)))
Franky Lin6bc52312013-08-10 12:27:28 +02001971 brcmf_dbg_hex_dump(true, pkt_next, hd_info.len, "Tx Frame:\n");
Franky Linb05e9252013-08-10 12:27:26 +02001972 else if (BRCMF_HDRS_ON())
Franky Lin706478c2013-08-10 12:27:29 +02001973 brcmf_dbg_hex_dump(true, pkt_next, head_pad + bus->tx_hdrlen,
Franky Linb05e9252013-08-10 12:27:26 +02001974 "Tx Header:\n");
1975
1976 return 0;
1977}
1978
1979/**
1980 * brcmf_sdio_txpkt_postp - packet post processing for transmit
1981 * @bus: brcmf_sdio structure pointer
1982 * @pktq: packet list pointer
1983 *
1984 * Processes to be applied to the packet
1985 * - Remove head padding
1986 * - Remove tail padding
1987 */
1988static void
1989brcmf_sdio_txpkt_postp(struct brcmf_sdio *bus, struct sk_buff_head *pktq)
1990{
1991 u8 *hdr;
1992 u32 dat_offset;
1993 u32 dummy_flags, chop_len;
1994 struct sk_buff *pkt_next, *tmp, *pkt_prev;
1995
1996 skb_queue_walk_safe(pktq, pkt_next, tmp) {
1997 dummy_flags = *(u32 *)(pkt_next->cb);
Franky Lin5491c112013-09-25 13:05:43 +02001998 if (dummy_flags & ALIGN_SKB_FLAG) {
1999 chop_len = dummy_flags & ALIGN_SKB_CHOP_LEN_MASK;
Franky Linb05e9252013-08-10 12:27:26 +02002000 if (chop_len) {
2001 pkt_prev = pkt_next->prev;
2002 memcpy(pkt_prev->data + pkt_prev->len,
2003 pkt_next->data, chop_len);
2004 skb_put(pkt_prev, chop_len);
2005 }
2006 __skb_unlink(pkt_next, pktq);
2007 brcmu_pkt_buf_free_skb(pkt_next);
2008 } else {
Franky Lin6bc52312013-08-10 12:27:28 +02002009 hdr = pkt_next->data + SDPCM_HWHDR_LEN;
Franky Linb05e9252013-08-10 12:27:26 +02002010 dat_offset = le32_to_cpu(*(__le32 *)hdr);
2011 dat_offset = (dat_offset & SDPCM_DOFFSET_MASK) >>
2012 SDPCM_DOFFSET_SHIFT;
2013 skb_pull(pkt_next, dat_offset);
2014 }
2015 }
2016}
2017
Arend van Spriel5b435de2011-10-05 13:19:03 +02002018/* Writes a HW/SW header into the packet and sends it. */
2019/* Assumes: (a) header space already there, (b) caller holds lock */
Franky Line92eedf2011-11-22 17:21:50 -08002020static int brcmf_sdbrcm_txpkt(struct brcmf_sdio *bus, struct sk_buff *pkt,
Arend van Spriel7f4bcee2013-03-03 12:45:29 +01002021 uint chan)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002022{
2023 int ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002024 int i;
Franky Linb05e9252013-08-10 12:27:26 +02002025 struct sk_buff_head localq;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002026
2027 brcmf_dbg(TRACE, "Enter\n");
2028
Franky Linb05e9252013-08-10 12:27:26 +02002029 __skb_queue_head_init(&localq);
2030 __skb_queue_tail(&localq, pkt);
2031 ret = brcmf_sdio_txpkt_prep(bus, &localq, chan);
2032 if (ret)
2033 goto done;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002034
Franky Lin38b0b0d2012-11-05 16:22:24 -08002035 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5adfeb62011-11-22 17:21:38 -08002036 ret = brcmf_sdcard_send_pkt(bus->sdiodev, bus->sdiodev->sbwad,
Franky Linb05e9252013-08-10 12:27:26 +02002037 SDIO_FUNC_2, F2SYNC, &localq);
Arend van Spriel80969832012-06-09 22:51:44 +02002038 bus->sdcnt.f2txdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002039
2040 if (ret < 0) {
2041 /* On failure, abort the command and terminate the frame */
2042 brcmf_dbg(INFO, "sdio error %d, abort command and terminate frame\n",
2043 ret);
Arend van Spriel80969832012-06-09 22:51:44 +02002044 bus->sdcnt.tx_sderrs++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002045
2046 brcmf_sdcard_abort(bus->sdiodev, SDIO_FUNC_2);
Franky Lin3bba8292012-05-04 18:27:33 -07002047 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL,
2048 SFC_WF_TERM, NULL);
Arend van Spriel80969832012-06-09 22:51:44 +02002049 bus->sdcnt.f1regdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002050
2051 for (i = 0; i < 3; i++) {
2052 u8 hi, lo;
Franky Lin45db3392012-05-04 18:27:32 -07002053 hi = brcmf_sdio_regrb(bus->sdiodev,
2054 SBSDIO_FUNC1_WFRAMEBCHI, NULL);
2055 lo = brcmf_sdio_regrb(bus->sdiodev,
2056 SBSDIO_FUNC1_WFRAMEBCLO, NULL);
Arend van Spriel80969832012-06-09 22:51:44 +02002057 bus->sdcnt.f1regdata += 2;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002058 if ((hi == 0) && (lo == 0))
2059 break;
2060 }
2061
2062 }
Franky Lin38b0b0d2012-11-05 16:22:24 -08002063 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002064 if (ret == 0)
Franky Lin6bc52312013-08-10 12:27:28 +02002065 bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQ_WRAP;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002066
2067done:
Franky Linb05e9252013-08-10 12:27:26 +02002068 brcmf_sdio_txpkt_postp(bus, &localq);
2069 __skb_dequeue_tail(&localq);
Arend van Spriela886f7f2013-04-03 12:40:26 +02002070 brcmf_txcomplete(bus->sdiodev->dev, pkt, ret == 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002071 return ret;
2072}
2073
Franky Line92eedf2011-11-22 17:21:50 -08002074static uint brcmf_sdbrcm_sendfromq(struct brcmf_sdio *bus, uint maxframes)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002075{
2076 struct sk_buff *pkt;
2077 u32 intstatus = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002078 int ret = 0, prec_out;
2079 uint cnt = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002080 u8 tx_prec_map;
2081
Arend van Spriel5b435de2011-10-05 13:19:03 +02002082 brcmf_dbg(TRACE, "Enter\n");
2083
2084 tx_prec_map = ~bus->flowcontrol;
2085
2086 /* Send frames until the limit or some other event */
2087 for (cnt = 0; (cnt < maxframes) && data_ok(bus); cnt++) {
2088 spin_lock_bh(&bus->txqlock);
2089 pkt = brcmu_pktq_mdeq(&bus->txq, tx_prec_map, &prec_out);
2090 if (pkt == NULL) {
2091 spin_unlock_bh(&bus->txqlock);
2092 break;
2093 }
2094 spin_unlock_bh(&bus->txqlock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002095
Arend van Spriel7f4bcee2013-03-03 12:45:29 +01002096 ret = brcmf_sdbrcm_txpkt(bus, pkt, SDPCM_DATA_CHANNEL);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002097
2098 /* In poll mode, need to check for other events */
2099 if (!bus->intr && cnt) {
2100 /* Check device status, signal pending interrupt */
Franky Lin38b0b0d2012-11-05 16:22:24 -08002101 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin5c15c232012-05-04 18:27:37 -07002102 ret = r_sdreg32(bus, &intstatus,
2103 offsetof(struct sdpcmd_regs,
2104 intstatus));
Franky Lin38b0b0d2012-11-05 16:22:24 -08002105 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel80969832012-06-09 22:51:44 +02002106 bus->sdcnt.f2txdata++;
Franky Lin5c15c232012-05-04 18:27:37 -07002107 if (ret != 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002108 break;
2109 if (intstatus & bus->hostintmask)
Franky Lin1d382272012-09-13 21:11:59 +02002110 atomic_set(&bus->ipend, 1);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002111 }
2112 }
2113
2114 /* Deflow-control stack if needed */
Hante Meuleman05dde972013-02-06 18:40:36 +01002115 if ((bus->sdiodev->bus_if->state == BRCMF_BUS_DATA) &&
Franky Linc8bf3482011-12-16 18:37:08 -08002116 bus->txoff && (pktq_len(&bus->txq) < TXLOW)) {
Hante Meuleman90d03ff2012-09-11 21:18:46 +02002117 bus->txoff = false;
2118 brcmf_txflowblock(bus->sdiodev->dev, false);
Franky Linc8bf3482011-12-16 18:37:08 -08002119 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002120
2121 return cnt;
2122}
2123
Franky Lina9ffda82011-12-16 18:37:12 -08002124static void brcmf_sdbrcm_bus_stop(struct device *dev)
2125{
2126 u32 local_hostintmask;
2127 u8 saveclk;
Franky Lina9ffda82011-12-16 18:37:12 -08002128 int err;
2129 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01002130 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Lina9ffda82011-12-16 18:37:12 -08002131 struct brcmf_sdio *bus = sdiodev->bus;
2132
2133 brcmf_dbg(TRACE, "Enter\n");
2134
2135 if (bus->watchdog_tsk) {
2136 send_sig(SIGTERM, bus->watchdog_tsk, 1);
2137 kthread_stop(bus->watchdog_tsk);
2138 bus->watchdog_tsk = NULL;
2139 }
2140
Franky Lin38b0b0d2012-11-05 16:22:24 -08002141 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lina9ffda82011-12-16 18:37:12 -08002142
Franky Lina9ffda82011-12-16 18:37:12 -08002143 /* Enable clock for device interrupts */
Piotr Haber4a3da992013-04-11 13:28:46 +02002144 brcmf_sdbrcm_bus_sleep(bus, false, false);
Franky Lina9ffda82011-12-16 18:37:12 -08002145
2146 /* Disable and clear interrupts at the chip level also */
Franky Lin58692752012-05-04 18:27:36 -07002147 w_sdreg32(bus, 0, offsetof(struct sdpcmd_regs, hostintmask));
Franky Lina9ffda82011-12-16 18:37:12 -08002148 local_hostintmask = bus->hostintmask;
2149 bus->hostintmask = 0;
2150
2151 /* Change our idea of bus state */
2152 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
2153
2154 /* Force clocks on backplane to be sure F2 interrupt propagates */
Franky Lin45db3392012-05-04 18:27:32 -07002155 saveclk = brcmf_sdio_regrb(bus->sdiodev,
2156 SBSDIO_FUNC1_CHIPCLKCSR, &err);
Franky Lina9ffda82011-12-16 18:37:12 -08002157 if (!err) {
Franky Lin3bba8292012-05-04 18:27:33 -07002158 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
2159 (saveclk | SBSDIO_FORCE_HT), &err);
Franky Lina9ffda82011-12-16 18:37:12 -08002160 }
2161 if (err)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002162 brcmf_err("Failed to force clock for F2: err %d\n", err);
Franky Lina9ffda82011-12-16 18:37:12 -08002163
2164 /* Turn off the bus (F2), free any pending packets */
2165 brcmf_dbg(INTR, "disable SDIO interrupts\n");
Franky Lin3bba8292012-05-04 18:27:33 -07002166 brcmf_sdio_regwb(bus->sdiodev, SDIO_CCCR_IOEx, SDIO_FUNC_ENABLE_1,
2167 NULL);
Franky Lina9ffda82011-12-16 18:37:12 -08002168
2169 /* Clear any pending interrupts now that F2 is disabled */
2170 w_sdreg32(bus, local_hostintmask,
Franky Lin58692752012-05-04 18:27:36 -07002171 offsetof(struct sdpcmd_regs, intstatus));
Franky Lina9ffda82011-12-16 18:37:12 -08002172
2173 /* Turn off the backplane clock (only) */
2174 brcmf_sdbrcm_clkctl(bus, CLK_SDONLY, false);
Franky Lin38b0b0d2012-11-05 16:22:24 -08002175 sdio_release_host(bus->sdiodev->func[1]);
Franky Lina9ffda82011-12-16 18:37:12 -08002176
2177 /* Clear the data packet queues */
2178 brcmu_pktq_flush(&bus->txq, true, NULL, NULL);
2179
2180 /* Clear any held glomming stuff */
2181 if (bus->glomd)
2182 brcmu_pkt_buf_free_skb(bus->glomd);
2183 brcmf_sdbrcm_free_glom(bus);
2184
2185 /* Clear rx control and wake any waiters */
Franky Lindd43a012012-11-05 16:22:22 -08002186 spin_lock_bh(&bus->rxctl_lock);
Franky Lina9ffda82011-12-16 18:37:12 -08002187 bus->rxlen = 0;
Franky Lindd43a012012-11-05 16:22:22 -08002188 spin_unlock_bh(&bus->rxctl_lock);
Franky Lina9ffda82011-12-16 18:37:12 -08002189 brcmf_sdbrcm_dcmd_resp_wake(bus);
2190
2191 /* Reset some F2 state stuff */
2192 bus->rxskip = false;
2193 bus->tx_seq = bus->rx_seq = 0;
Franky Lina9ffda82011-12-16 18:37:12 -08002194}
2195
Franky Linba89bf12012-04-27 18:56:59 -07002196static inline void brcmf_sdbrcm_clrintr(struct brcmf_sdio *bus)
2197{
2198 unsigned long flags;
2199
Hante Meuleman668761a2013-04-12 10:55:55 +02002200 if (bus->sdiodev->oob_irq_requested) {
2201 spin_lock_irqsave(&bus->sdiodev->irq_en_lock, flags);
2202 if (!bus->sdiodev->irq_en && !atomic_read(&bus->ipend)) {
2203 enable_irq(bus->sdiodev->pdata->oob_irq_nr);
2204 bus->sdiodev->irq_en = true;
2205 }
2206 spin_unlock_irqrestore(&bus->sdiodev->irq_en_lock, flags);
Franky Linba89bf12012-04-27 18:56:59 -07002207 }
Franky Linba89bf12012-04-27 18:56:59 -07002208}
Franky Linba89bf12012-04-27 18:56:59 -07002209
Franky Lin45316032012-09-13 21:12:03 +02002210static int brcmf_sdio_intr_rstatus(struct brcmf_sdio *bus)
2211{
2212 u8 idx;
2213 u32 addr;
2214 unsigned long val;
2215 int n, ret;
2216
2217 idx = brcmf_sdio_chip_getinfidx(bus->ci, BCMA_CORE_SDIO_DEV);
2218 addr = bus->ci->c_inf[idx].base +
2219 offsetof(struct sdpcmd_regs, intstatus);
2220
2221 ret = brcmf_sdio_regrw_helper(bus->sdiodev, addr, &val, false);
2222 bus->sdcnt.f1regdata++;
2223 if (ret != 0)
2224 val = 0;
2225
2226 val &= bus->hostintmask;
2227 atomic_set(&bus->fcstate, !!(val & I_HMB_FC_STATE));
2228
2229 /* Clear interrupts */
2230 if (val) {
2231 ret = brcmf_sdio_regrw_helper(bus->sdiodev, addr, &val, true);
2232 bus->sdcnt.f1regdata++;
2233 }
2234
2235 if (ret) {
2236 atomic_set(&bus->intstatus, 0);
2237 } else if (val) {
2238 for_each_set_bit(n, &val, 32)
2239 set_bit(n, (unsigned long *)&bus->intstatus.counter);
2240 }
2241
2242 return ret;
2243}
2244
Franky Linf1e68c22012-09-13 21:12:00 +02002245static void brcmf_sdbrcm_dpc(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002246{
Franky Lin45316032012-09-13 21:12:03 +02002247 u32 newstatus = 0;
2248 unsigned long intstatus;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002249 uint rxlimit = bus->rxbound; /* Rx frames to read before resched */
2250 uint txlimit = bus->txbound; /* Tx frames to send before resched */
2251 uint framecnt = 0; /* Temporary counter of tx/rx frames */
Franky Lin45316032012-09-13 21:12:03 +02002252 int err = 0, n;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002253
2254 brcmf_dbg(TRACE, "Enter\n");
2255
Franky Lin38b0b0d2012-11-05 16:22:24 -08002256 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002257
2258 /* If waiting for HTAVAIL, check status */
Piotr Haber4a3da992013-04-11 13:28:46 +02002259 if (!bus->sr_enabled && bus->clkstate == CLK_PENDING) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02002260 u8 clkctl, devctl = 0;
2261
Joe Perches8ae74652012-01-15 00:38:38 -08002262#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02002263 /* Check for inconsistent device control */
Franky Lin45db3392012-05-04 18:27:32 -07002264 devctl = brcmf_sdio_regrb(bus->sdiodev,
2265 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002266 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002267 brcmf_err("error reading DEVCTL: %d\n", err);
Franky Lin712ac5b2011-12-16 18:37:09 -08002268 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002269 }
Joe Perches8ae74652012-01-15 00:38:38 -08002270#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02002271
2272 /* Read CSR, if clock on switch to AVAIL, else ignore */
Franky Lin45db3392012-05-04 18:27:32 -07002273 clkctl = brcmf_sdio_regrb(bus->sdiodev,
2274 SBSDIO_FUNC1_CHIPCLKCSR, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002275 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002276 brcmf_err("error reading CSR: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02002277 err);
Franky Lin712ac5b2011-12-16 18:37:09 -08002278 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002279 }
2280
Arend van Sprielc3203372013-04-03 12:40:44 +02002281 brcmf_dbg(SDIO, "DPC: PENDING, devctl 0x%02x clkctl 0x%02x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02002282 devctl, clkctl);
2283
2284 if (SBSDIO_HTAV(clkctl)) {
Franky Lin45db3392012-05-04 18:27:32 -07002285 devctl = brcmf_sdio_regrb(bus->sdiodev,
2286 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002287 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002288 brcmf_err("error reading DEVCTL: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02002289 err);
Franky Lin712ac5b2011-12-16 18:37:09 -08002290 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002291 }
2292 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
Franky Lin3bba8292012-05-04 18:27:33 -07002293 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
2294 devctl, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002295 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002296 brcmf_err("error writing DEVCTL: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02002297 err);
Franky Lin712ac5b2011-12-16 18:37:09 -08002298 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002299 }
2300 bus->clkstate = CLK_AVAIL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002301 }
2302 }
2303
Arend van Spriel5b435de2011-10-05 13:19:03 +02002304 /* Make sure backplane clock is on */
Piotr Haber4a3da992013-04-11 13:28:46 +02002305 brcmf_sdbrcm_bus_sleep(bus, false, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002306
2307 /* Pending interrupt indicates new device status */
Franky Lin1d382272012-09-13 21:11:59 +02002308 if (atomic_read(&bus->ipend) > 0) {
2309 atomic_set(&bus->ipend, 0);
Franky Lin45316032012-09-13 21:12:03 +02002310 err = brcmf_sdio_intr_rstatus(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002311 }
2312
Franky Lin45316032012-09-13 21:12:03 +02002313 /* Start with leftover status bits */
2314 intstatus = atomic_xchg(&bus->intstatus, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002315
2316 /* Handle flow-control change: read new state in case our ack
2317 * crossed another change interrupt. If change still set, assume
2318 * FC ON for safety, let next loop through do the debounce.
2319 */
2320 if (intstatus & I_HMB_FC_CHANGE) {
2321 intstatus &= ~I_HMB_FC_CHANGE;
Franky Lin5c15c232012-05-04 18:27:37 -07002322 err = w_sdreg32(bus, I_HMB_FC_CHANGE,
2323 offsetof(struct sdpcmd_regs, intstatus));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002324
Franky Lin5c15c232012-05-04 18:27:37 -07002325 err = r_sdreg32(bus, &newstatus,
2326 offsetof(struct sdpcmd_regs, intstatus));
Arend van Spriel80969832012-06-09 22:51:44 +02002327 bus->sdcnt.f1regdata += 2;
Franky Lin45316032012-09-13 21:12:03 +02002328 atomic_set(&bus->fcstate,
2329 !!(newstatus & (I_HMB_FC_STATE | I_HMB_FC_CHANGE)));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002330 intstatus |= (newstatus & bus->hostintmask);
2331 }
2332
2333 /* Handle host mailbox indication */
2334 if (intstatus & I_HMB_HOST_INT) {
2335 intstatus &= ~I_HMB_HOST_INT;
2336 intstatus |= brcmf_sdbrcm_hostmail(bus);
2337 }
2338
Franky Lin38b0b0d2012-11-05 16:22:24 -08002339 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin7cdf57d2012-11-05 16:22:23 -08002340
Arend van Spriel5b435de2011-10-05 13:19:03 +02002341 /* Generally don't ask for these, can get CRC errors... */
2342 if (intstatus & I_WR_OOSYNC) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002343 brcmf_err("Dongle reports WR_OOSYNC\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002344 intstatus &= ~I_WR_OOSYNC;
2345 }
2346
2347 if (intstatus & I_RD_OOSYNC) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002348 brcmf_err("Dongle reports RD_OOSYNC\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002349 intstatus &= ~I_RD_OOSYNC;
2350 }
2351
2352 if (intstatus & I_SBINT) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002353 brcmf_err("Dongle reports SBINT\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002354 intstatus &= ~I_SBINT;
2355 }
2356
2357 /* Would be active due to wake-wlan in gSPI */
2358 if (intstatus & I_CHIPACTIVE) {
2359 brcmf_dbg(INFO, "Dongle reports CHIPACTIVE\n");
2360 intstatus &= ~I_CHIPACTIVE;
2361 }
2362
2363 /* Ignore frame indications if rxskip is set */
2364 if (bus->rxskip)
2365 intstatus &= ~I_HMB_FRAME_IND;
2366
2367 /* On frame indication, read available frames */
Franky Lin03d5c362012-09-13 21:12:01 +02002368 if (PKT_AVAILABLE() && bus->clkstate == CLK_AVAIL) {
Franky Lin4754fce2012-09-19 22:21:11 +02002369 framecnt = brcmf_sdio_readframes(bus, rxlimit);
2370 if (!bus->rxpending)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002371 intstatus &= ~I_HMB_FRAME_IND;
2372 rxlimit -= min(framecnt, rxlimit);
2373 }
2374
2375 /* Keep still-pending events for next scheduling */
Franky Lin45316032012-09-13 21:12:03 +02002376 if (intstatus) {
2377 for_each_set_bit(n, &intstatus, 32)
2378 set_bit(n, (unsigned long *)&bus->intstatus.counter);
2379 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002380
Franky Linba89bf12012-04-27 18:56:59 -07002381 brcmf_sdbrcm_clrintr(bus);
2382
Arend van Spriel5b435de2011-10-05 13:19:03 +02002383 if (data_ok(bus) && bus->ctrl_frame_stat &&
2384 (bus->clkstate == CLK_AVAIL)) {
Franky Lin03d5c362012-09-13 21:12:01 +02002385 int i;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002386
Franky Lin38b0b0d2012-11-05 16:22:24 -08002387 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin03d5c362012-09-13 21:12:01 +02002388 err = brcmf_sdcard_send_buf(bus->sdiodev, bus->sdiodev->sbwad,
Joe Perches2c208892012-06-04 12:44:17 +00002389 SDIO_FUNC_2, F2SYNC, bus->ctrl_frame_buf,
Arend van Spriel5adfeb62011-11-22 17:21:38 -08002390 (u32) bus->ctrl_frame_len);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002391
Franky Lin03d5c362012-09-13 21:12:01 +02002392 if (err < 0) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02002393 /* On failure, abort the command and
2394 terminate the frame */
2395 brcmf_dbg(INFO, "sdio error %d, abort command and terminate frame\n",
Franky Lin03d5c362012-09-13 21:12:01 +02002396 err);
Arend van Spriel80969832012-06-09 22:51:44 +02002397 bus->sdcnt.tx_sderrs++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002398
2399 brcmf_sdcard_abort(bus->sdiodev, SDIO_FUNC_2);
2400
Franky Lin3bba8292012-05-04 18:27:33 -07002401 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL,
Franky Lin5c15c232012-05-04 18:27:37 -07002402 SFC_WF_TERM, &err);
Arend van Spriel80969832012-06-09 22:51:44 +02002403 bus->sdcnt.f1regdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002404
2405 for (i = 0; i < 3; i++) {
2406 u8 hi, lo;
Franky Lin45db3392012-05-04 18:27:32 -07002407 hi = brcmf_sdio_regrb(bus->sdiodev,
2408 SBSDIO_FUNC1_WFRAMEBCHI,
Franky Lin5c15c232012-05-04 18:27:37 -07002409 &err);
Franky Lin45db3392012-05-04 18:27:32 -07002410 lo = brcmf_sdio_regrb(bus->sdiodev,
2411 SBSDIO_FUNC1_WFRAMEBCLO,
Franky Lin5c15c232012-05-04 18:27:37 -07002412 &err);
Arend van Spriel80969832012-06-09 22:51:44 +02002413 bus->sdcnt.f1regdata += 2;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002414 if ((hi == 0) && (lo == 0))
2415 break;
2416 }
2417
Franky Lin03d5c362012-09-13 21:12:01 +02002418 } else {
Franky Lin6bc52312013-08-10 12:27:28 +02002419 bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQ_WRAP;
Franky Lin03d5c362012-09-13 21:12:01 +02002420 }
Franky Lin38b0b0d2012-11-05 16:22:24 -08002421 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002422 bus->ctrl_frame_stat = false;
2423 brcmf_sdbrcm_wait_event_wakeup(bus);
2424 }
2425 /* Send queued frames (limit 1 if rx may still be pending) */
Franky Lin45316032012-09-13 21:12:03 +02002426 else if ((bus->clkstate == CLK_AVAIL) && !atomic_read(&bus->fcstate) &&
Arend van Spriel5b435de2011-10-05 13:19:03 +02002427 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) && txlimit
2428 && data_ok(bus)) {
Franky Lin4754fce2012-09-19 22:21:11 +02002429 framecnt = bus->rxpending ? min(txlimit, bus->txminmax) :
2430 txlimit;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002431 framecnt = brcmf_sdbrcm_sendfromq(bus, framecnt);
2432 txlimit -= framecnt;
2433 }
2434
Franky Lin5c15c232012-05-04 18:27:37 -07002435 if ((bus->sdiodev->bus_if->state == BRCMF_BUS_DOWN) || (err != 0)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002436 brcmf_err("failed backplane access over SDIO, halting operation\n");
Franky Lin712ac5b2011-12-16 18:37:09 -08002437 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
Franky Lin45316032012-09-13 21:12:03 +02002438 atomic_set(&bus->intstatus, 0);
2439 } else if (atomic_read(&bus->intstatus) ||
2440 atomic_read(&bus->ipend) > 0 ||
2441 (!atomic_read(&bus->fcstate) &&
2442 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) &&
2443 data_ok(bus)) || PKT_AVAILABLE()) {
Arend van Sprielfccfe932013-06-18 13:29:26 +02002444 atomic_inc(&bus->dpc_tskcnt);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002445 }
2446
Arend van Spriel5b435de2011-10-05 13:19:03 +02002447 /* If we're done for now, turn off clock request. */
2448 if ((bus->clkstate != CLK_PENDING)
2449 && bus->idletime == BRCMF_IDLE_IMMEDIATE) {
2450 bus->activity = false;
Piotr Haber4a3da992013-04-11 13:28:46 +02002451 brcmf_dbg(SDIO, "idle state\n");
Franky Lin38b0b0d2012-11-05 16:22:24 -08002452 sdio_claim_host(bus->sdiodev->func[1]);
Piotr Haber4a3da992013-04-11 13:28:46 +02002453 brcmf_sdbrcm_bus_sleep(bus, true, false);
Franky Lin38b0b0d2012-11-05 16:22:24 -08002454 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002455 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002456}
2457
Arend van Spriele2432b62013-04-03 12:40:38 +02002458static struct pktq *brcmf_sdbrcm_bus_gettxq(struct device *dev)
2459{
2460 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
2461 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
2462 struct brcmf_sdio *bus = sdiodev->bus;
2463
2464 return &bus->txq;
2465}
2466
Franky Linb9692d12011-12-16 18:37:15 -08002467static int brcmf_sdbrcm_bus_txdata(struct device *dev, struct sk_buff *pkt)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002468{
2469 int ret = -EBADE;
2470 uint datalen, prec;
Franky Linbf347bb2011-11-22 17:21:56 -08002471 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01002472 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Linbf347bb2011-11-22 17:21:56 -08002473 struct brcmf_sdio *bus = sdiodev->bus;
Arend van Spriel4061f892013-08-10 12:27:19 +02002474 ulong flags;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002475
2476 brcmf_dbg(TRACE, "Enter\n");
2477
2478 datalen = pkt->len;
2479
2480 /* Add space for the header */
Franky Lin706478c2013-08-10 12:27:29 +02002481 skb_push(pkt, bus->tx_hdrlen);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002482 /* precondition: IS_ALIGNED((unsigned long)(pkt->data), 2) */
2483
2484 prec = prio2prec((pkt->priority & PRIOMASK));
2485
2486 /* Check for existing queue, current flow-control,
2487 pending event, or pending clock */
2488 brcmf_dbg(TRACE, "deferring pktq len %d\n", pktq_len(&bus->txq));
Arend van Spriel80969832012-06-09 22:51:44 +02002489 bus->sdcnt.fcqueued++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002490
2491 /* Priority based enq */
Arend van Spriel4061f892013-08-10 12:27:19 +02002492 spin_lock_irqsave(&bus->txqlock, flags);
Joe Perches23677ce2012-02-09 11:17:23 +00002493 if (!brcmf_c_prec_enq(bus->sdiodev->dev, &bus->txq, pkt, prec)) {
Franky Lin706478c2013-08-10 12:27:29 +02002494 skb_pull(pkt, bus->tx_hdrlen);
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002495 brcmf_err("out of bus->txq !!!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002496 ret = -ENOSR;
2497 } else {
2498 ret = 0;
2499 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002500
Franky Linc8bf3482011-12-16 18:37:08 -08002501 if (pktq_len(&bus->txq) >= TXHI) {
Hante Meuleman90d03ff2012-09-11 21:18:46 +02002502 bus->txoff = true;
2503 brcmf_txflowblock(bus->sdiodev->dev, true);
Franky Linc8bf3482011-12-16 18:37:08 -08002504 }
Arend van Spriel4061f892013-08-10 12:27:19 +02002505 spin_unlock_irqrestore(&bus->txqlock, flags);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002506
Joe Perches8ae74652012-01-15 00:38:38 -08002507#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02002508 if (pktq_plen(&bus->txq, prec) > qcount[prec])
2509 qcount[prec] = pktq_plen(&bus->txq, prec);
2510#endif
Franky Linf1e68c22012-09-13 21:12:00 +02002511
Arend van Sprielfccfe932013-06-18 13:29:26 +02002512 if (atomic_read(&bus->dpc_tskcnt) == 0) {
2513 atomic_inc(&bus->dpc_tskcnt);
Franky Linf1e68c22012-09-13 21:12:00 +02002514 queue_work(bus->brcmf_wq, &bus->datawork);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002515 }
2516
2517 return ret;
2518}
2519
Joe Perches8ae74652012-01-15 00:38:38 -08002520#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02002521#define CONSOLE_LINE_MAX 192
2522
Franky Line92eedf2011-11-22 17:21:50 -08002523static int brcmf_sdbrcm_readconsole(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002524{
2525 struct brcmf_console *c = &bus->console;
2526 u8 line[CONSOLE_LINE_MAX], ch;
2527 u32 n, idx, addr;
2528 int rv;
2529
2530 /* Don't do anything until FWREADY updates console address */
2531 if (bus->console_addr == 0)
2532 return 0;
2533
2534 /* Read console log struct */
2535 addr = bus->console_addr + offsetof(struct rte_console, log_le);
Franky Linba540b02013-04-11 13:28:47 +02002536 rv = brcmf_sdio_ramrw(bus->sdiodev, false, addr, (u8 *)&c->log_le,
2537 sizeof(c->log_le));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002538 if (rv < 0)
2539 return rv;
2540
2541 /* Allocate console buffer (one time only) */
2542 if (c->buf == NULL) {
2543 c->bufsize = le32_to_cpu(c->log_le.buf_size);
2544 c->buf = kmalloc(c->bufsize, GFP_ATOMIC);
2545 if (c->buf == NULL)
2546 return -ENOMEM;
2547 }
2548
2549 idx = le32_to_cpu(c->log_le.idx);
2550
2551 /* Protect against corrupt value */
2552 if (idx > c->bufsize)
2553 return -EBADE;
2554
2555 /* Skip reading the console buffer if the index pointer
2556 has not moved */
2557 if (idx == c->last)
2558 return 0;
2559
2560 /* Read the console buffer */
2561 addr = le32_to_cpu(c->log_le.buf);
Franky Linba540b02013-04-11 13:28:47 +02002562 rv = brcmf_sdio_ramrw(bus->sdiodev, false, addr, c->buf, c->bufsize);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002563 if (rv < 0)
2564 return rv;
2565
2566 while (c->last != idx) {
2567 for (n = 0; n < CONSOLE_LINE_MAX - 2; n++) {
2568 if (c->last == idx) {
2569 /* This would output a partial line.
2570 * Instead, back up
2571 * the buffer pointer and output this
2572 * line next time around.
2573 */
2574 if (c->last >= n)
2575 c->last -= n;
2576 else
2577 c->last = c->bufsize - n;
2578 goto break2;
2579 }
2580 ch = c->buf[c->last];
2581 c->last = (c->last + 1) % c->bufsize;
2582 if (ch == '\n')
2583 break;
2584 line[n] = ch;
2585 }
2586
2587 if (n > 0) {
2588 if (line[n - 1] == '\r')
2589 n--;
2590 line[n] = 0;
Joe Perches18aad4f2012-01-15 00:38:42 -08002591 pr_debug("CONSOLE: %s\n", line);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002592 }
2593 }
2594break2:
2595
2596 return 0;
2597}
Joe Perches8ae74652012-01-15 00:38:38 -08002598#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02002599
Franky Line92eedf2011-11-22 17:21:50 -08002600static int brcmf_tx_frame(struct brcmf_sdio *bus, u8 *frame, u16 len)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002601{
2602 int i;
2603 int ret;
2604
2605 bus->ctrl_frame_stat = false;
Arend van Spriel5adfeb62011-11-22 17:21:38 -08002606 ret = brcmf_sdcard_send_buf(bus->sdiodev, bus->sdiodev->sbwad,
2607 SDIO_FUNC_2, F2SYNC, frame, len);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002608
2609 if (ret < 0) {
2610 /* On failure, abort the command and terminate the frame */
2611 brcmf_dbg(INFO, "sdio error %d, abort command and terminate frame\n",
2612 ret);
Arend van Spriel80969832012-06-09 22:51:44 +02002613 bus->sdcnt.tx_sderrs++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002614
2615 brcmf_sdcard_abort(bus->sdiodev, SDIO_FUNC_2);
2616
Franky Lin3bba8292012-05-04 18:27:33 -07002617 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL,
2618 SFC_WF_TERM, NULL);
Arend van Spriel80969832012-06-09 22:51:44 +02002619 bus->sdcnt.f1regdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002620
2621 for (i = 0; i < 3; i++) {
2622 u8 hi, lo;
Franky Lin45db3392012-05-04 18:27:32 -07002623 hi = brcmf_sdio_regrb(bus->sdiodev,
2624 SBSDIO_FUNC1_WFRAMEBCHI, NULL);
2625 lo = brcmf_sdio_regrb(bus->sdiodev,
2626 SBSDIO_FUNC1_WFRAMEBCLO, NULL);
Arend van Spriel80969832012-06-09 22:51:44 +02002627 bus->sdcnt.f1regdata += 2;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002628 if (hi == 0 && lo == 0)
2629 break;
2630 }
2631 return ret;
2632 }
2633
Franky Lin6bc52312013-08-10 12:27:28 +02002634 bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQ_WRAP;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002635
2636 return ret;
2637}
2638
Franky Linfcf094f2011-12-16 18:37:16 -08002639static int
Franky Lin47a1ce72011-11-22 17:21:55 -08002640brcmf_sdbrcm_bus_txctl(struct device *dev, unsigned char *msg, uint msglen)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002641{
2642 u8 *frame;
2643 u16 len;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002644 uint retries = 0;
2645 u8 doff = 0;
2646 int ret = -1;
Franky Lin47a1ce72011-11-22 17:21:55 -08002647 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01002648 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Lin47a1ce72011-11-22 17:21:55 -08002649 struct brcmf_sdio *bus = sdiodev->bus;
Franky Lin6bc52312013-08-10 12:27:28 +02002650 struct brcmf_sdio_hdrinfo hd_info = {0};
Arend van Spriel5b435de2011-10-05 13:19:03 +02002651
2652 brcmf_dbg(TRACE, "Enter\n");
2653
2654 /* Back the pointer to make a room for bus header */
Franky Lin706478c2013-08-10 12:27:29 +02002655 frame = msg - bus->tx_hdrlen;
2656 len = (msglen += bus->tx_hdrlen);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002657
2658 /* Add alignment padding (optional for ctl frames) */
2659 doff = ((unsigned long)frame % BRCMF_SDALIGN);
2660 if (doff) {
2661 frame -= doff;
2662 len += doff;
2663 msglen += doff;
Franky Lin706478c2013-08-10 12:27:29 +02002664 memset(frame, 0, doff + bus->tx_hdrlen);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002665 }
2666 /* precondition: doff < BRCMF_SDALIGN */
Franky Lin706478c2013-08-10 12:27:29 +02002667 doff += bus->tx_hdrlen;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002668
2669 /* Round send length to next SDIO block */
2670 if (bus->roundup && bus->blocksize && (len > bus->blocksize)) {
2671 u16 pad = bus->blocksize - (len % bus->blocksize);
2672 if ((pad <= bus->roundup) && (pad < bus->blocksize))
2673 len += pad;
2674 } else if (len % BRCMF_SDALIGN) {
2675 len += BRCMF_SDALIGN - (len % BRCMF_SDALIGN);
2676 }
2677
2678 /* Satisfy length-alignment requirements */
2679 if (len & (ALIGNMENT - 1))
2680 len = roundup(len, ALIGNMENT);
2681
2682 /* precondition: IS_ALIGNED((unsigned long)frame, 2) */
2683
Arend van Spriel5b435de2011-10-05 13:19:03 +02002684 /* Make sure backplane clock is on */
Franky Lin38b0b0d2012-11-05 16:22:24 -08002685 sdio_claim_host(bus->sdiodev->func[1]);
Piotr Haber4a3da992013-04-11 13:28:46 +02002686 brcmf_sdbrcm_bus_sleep(bus, false, false);
Franky Lin38b0b0d2012-11-05 16:22:24 -08002687 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002688
Franky Lin6bc52312013-08-10 12:27:28 +02002689 hd_info.len = (u16)msglen;
2690 hd_info.channel = SDPCM_CONTROL_CHANNEL;
2691 hd_info.dat_offset = doff;
2692 brcmf_sdio_hdpack(bus, frame, &hd_info);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002693
2694 if (!data_ok(bus)) {
2695 brcmf_dbg(INFO, "No bus credit bus->tx_max %d, bus->tx_seq %d\n",
2696 bus->tx_max, bus->tx_seq);
2697 bus->ctrl_frame_stat = true;
2698 /* Send from dpc */
2699 bus->ctrl_frame_buf = frame;
2700 bus->ctrl_frame_len = len;
2701
Franky Linfd67dc82012-11-05 16:22:25 -08002702 wait_event_interruptible_timeout(bus->ctrl_wait,
2703 !bus->ctrl_frame_stat,
2704 msecs_to_jiffies(2000));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002705
Joe Perches23677ce2012-02-09 11:17:23 +00002706 if (!bus->ctrl_frame_stat) {
Arend van Sprielc3203372013-04-03 12:40:44 +02002707 brcmf_dbg(SDIO, "ctrl_frame_stat == false\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002708 ret = 0;
2709 } else {
Arend van Sprielc3203372013-04-03 12:40:44 +02002710 brcmf_dbg(SDIO, "ctrl_frame_stat == true\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002711 ret = -1;
2712 }
2713 }
2714
2715 if (ret == -1) {
Joe Perches1e023822012-01-15 00:38:40 -08002716 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_CTL_ON(),
2717 frame, len, "Tx Frame:\n");
2718 brcmf_dbg_hex_dump(!(BRCMF_BYTES_ON() && BRCMF_CTL_ON()) &&
2719 BRCMF_HDRS_ON(),
2720 frame, min_t(u16, len, 16), "TxHdr:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002721
2722 do {
Franky Lin38b0b0d2012-11-05 16:22:24 -08002723 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002724 ret = brcmf_tx_frame(bus, frame, len);
Franky Lin38b0b0d2012-11-05 16:22:24 -08002725 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002726 } while (ret < 0 && retries++ < TXRETRIES);
2727 }
2728
Franky Linf1e68c22012-09-13 21:12:00 +02002729 if ((bus->idletime == BRCMF_IDLE_IMMEDIATE) &&
Arend van Sprielfccfe932013-06-18 13:29:26 +02002730 atomic_read(&bus->dpc_tskcnt) == 0) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02002731 bus->activity = false;
Franky Lin38b0b0d2012-11-05 16:22:24 -08002732 sdio_claim_host(bus->sdiodev->func[1]);
Piotr Haber4a3da992013-04-11 13:28:46 +02002733 brcmf_dbg(INFO, "idle\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002734 brcmf_sdbrcm_clkctl(bus, CLK_NONE, true);
Franky Lin38b0b0d2012-11-05 16:22:24 -08002735 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002736 }
2737
Arend van Spriel5b435de2011-10-05 13:19:03 +02002738 if (ret)
Arend van Spriel80969832012-06-09 22:51:44 +02002739 bus->sdcnt.tx_ctlerrs++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002740 else
Arend van Spriel80969832012-06-09 22:51:44 +02002741 bus->sdcnt.tx_ctlpkts++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002742
2743 return ret ? -EIO : 0;
2744}
2745
Arend van Spriel80969832012-06-09 22:51:44 +02002746#ifdef DEBUG
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002747static inline bool brcmf_sdio_valid_shared_address(u32 addr)
2748{
2749 return !(addr == 0 || ((~addr >> 16) & 0xffff) == (addr & 0xffff));
2750}
2751
2752static int brcmf_sdio_readshared(struct brcmf_sdio *bus,
2753 struct sdpcm_shared *sh)
2754{
2755 u32 addr;
2756 int rv;
2757 u32 shaddr = 0;
2758 struct sdpcm_shared_le sh_le;
2759 __le32 addr_le;
2760
Franky Lin1640f282013-04-11 13:28:51 +02002761 shaddr = bus->ci->rambase + bus->ramsize - 4;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002762
2763 /*
2764 * Read last word in socram to determine
2765 * address of sdpcm_shared structure
2766 */
Franky Lin38b0b0d2012-11-05 16:22:24 -08002767 sdio_claim_host(bus->sdiodev->func[1]);
Piotr Haber4a3da992013-04-11 13:28:46 +02002768 brcmf_sdbrcm_bus_sleep(bus, false, false);
Franky Linba540b02013-04-11 13:28:47 +02002769 rv = brcmf_sdio_ramrw(bus->sdiodev, false, shaddr, (u8 *)&addr_le, 4);
Piotr Haberb55de972013-02-08 12:06:33 +01002770 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002771 if (rv < 0)
2772 return rv;
2773
2774 addr = le32_to_cpu(addr_le);
2775
Arend van Sprielc3203372013-04-03 12:40:44 +02002776 brcmf_dbg(SDIO, "sdpcm_shared address 0x%08X\n", addr);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002777
2778 /*
2779 * Check if addr is valid.
2780 * NVRAM length at the end of memory should have been overwritten.
2781 */
2782 if (!brcmf_sdio_valid_shared_address(addr)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002783 brcmf_err("invalid sdpcm_shared address 0x%08X\n",
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002784 addr);
2785 return -EINVAL;
2786 }
2787
2788 /* Read hndrte_shared structure */
Franky Linba540b02013-04-11 13:28:47 +02002789 rv = brcmf_sdio_ramrw(bus->sdiodev, false, addr, (u8 *)&sh_le,
2790 sizeof(struct sdpcm_shared_le));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002791 if (rv < 0)
2792 return rv;
2793
2794 /* Endianness */
2795 sh->flags = le32_to_cpu(sh_le.flags);
2796 sh->trap_addr = le32_to_cpu(sh_le.trap_addr);
2797 sh->assert_exp_addr = le32_to_cpu(sh_le.assert_exp_addr);
2798 sh->assert_file_addr = le32_to_cpu(sh_le.assert_file_addr);
2799 sh->assert_line = le32_to_cpu(sh_le.assert_line);
2800 sh->console_addr = le32_to_cpu(sh_le.console_addr);
2801 sh->msgtrace_addr = le32_to_cpu(sh_le.msgtrace_addr);
2802
Piotr Haber86dcd932013-04-05 10:57:43 +02002803 if ((sh->flags & SDPCM_SHARED_VERSION_MASK) > SDPCM_SHARED_VERSION) {
2804 brcmf_err("sdpcm shared version unsupported: dhd %d dongle %d\n",
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002805 SDPCM_SHARED_VERSION,
2806 sh->flags & SDPCM_SHARED_VERSION_MASK);
2807 return -EPROTO;
2808 }
2809
2810 return 0;
2811}
2812
2813static int brcmf_sdio_dump_console(struct brcmf_sdio *bus,
2814 struct sdpcm_shared *sh, char __user *data,
2815 size_t count)
2816{
2817 u32 addr, console_ptr, console_size, console_index;
2818 char *conbuf = NULL;
2819 __le32 sh_val;
2820 int rv;
2821 loff_t pos = 0;
2822 int nbytes = 0;
2823
2824 /* obtain console information from device memory */
2825 addr = sh->console_addr + offsetof(struct rte_console, log_le);
Franky Linba540b02013-04-11 13:28:47 +02002826 rv = brcmf_sdio_ramrw(bus->sdiodev, false, addr,
2827 (u8 *)&sh_val, sizeof(u32));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002828 if (rv < 0)
2829 return rv;
2830 console_ptr = le32_to_cpu(sh_val);
2831
2832 addr = sh->console_addr + offsetof(struct rte_console, log_le.buf_size);
Franky Linba540b02013-04-11 13:28:47 +02002833 rv = brcmf_sdio_ramrw(bus->sdiodev, false, addr,
2834 (u8 *)&sh_val, sizeof(u32));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002835 if (rv < 0)
2836 return rv;
2837 console_size = le32_to_cpu(sh_val);
2838
2839 addr = sh->console_addr + offsetof(struct rte_console, log_le.idx);
Franky Linba540b02013-04-11 13:28:47 +02002840 rv = brcmf_sdio_ramrw(bus->sdiodev, false, addr,
2841 (u8 *)&sh_val, sizeof(u32));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002842 if (rv < 0)
2843 return rv;
2844 console_index = le32_to_cpu(sh_val);
2845
2846 /* allocate buffer for console data */
2847 if (console_size <= CONSOLE_BUFFER_MAX)
2848 conbuf = vzalloc(console_size+1);
2849
2850 if (!conbuf)
2851 return -ENOMEM;
2852
2853 /* obtain the console data from device */
2854 conbuf[console_size] = '\0';
Franky Linba540b02013-04-11 13:28:47 +02002855 rv = brcmf_sdio_ramrw(bus->sdiodev, false, console_ptr, (u8 *)conbuf,
2856 console_size);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002857 if (rv < 0)
2858 goto done;
2859
2860 rv = simple_read_from_buffer(data, count, &pos,
2861 conbuf + console_index,
2862 console_size - console_index);
2863 if (rv < 0)
2864 goto done;
2865
2866 nbytes = rv;
2867 if (console_index > 0) {
2868 pos = 0;
2869 rv = simple_read_from_buffer(data+nbytes, count, &pos,
2870 conbuf, console_index - 1);
2871 if (rv < 0)
2872 goto done;
2873 rv += nbytes;
2874 }
2875done:
2876 vfree(conbuf);
2877 return rv;
2878}
2879
2880static int brcmf_sdio_trap_info(struct brcmf_sdio *bus, struct sdpcm_shared *sh,
2881 char __user *data, size_t count)
2882{
2883 int error, res;
2884 char buf[350];
2885 struct brcmf_trap_info tr;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002886 loff_t pos = 0;
2887
Piotr Haberbaa9e602013-04-05 10:57:42 +02002888 if ((sh->flags & SDPCM_SHARED_TRAP) == 0) {
2889 brcmf_dbg(INFO, "no trap in firmware\n");
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002890 return 0;
Piotr Haberbaa9e602013-04-05 10:57:42 +02002891 }
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002892
Franky Linba540b02013-04-11 13:28:47 +02002893 error = brcmf_sdio_ramrw(bus->sdiodev, false, sh->trap_addr, (u8 *)&tr,
2894 sizeof(struct brcmf_trap_info));
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002895 if (error < 0)
2896 return error;
2897
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002898 res = scnprintf(buf, sizeof(buf),
2899 "dongle trap info: type 0x%x @ epc 0x%08x\n"
2900 " cpsr 0x%08x spsr 0x%08x sp 0x%08x\n"
2901 " lr 0x%08x pc 0x%08x offset 0x%x\n"
2902 " r0 0x%08x r1 0x%08x r2 0x%08x r3 0x%08x\n"
2903 " r4 0x%08x r5 0x%08x r6 0x%08x r7 0x%08x\n",
2904 le32_to_cpu(tr.type), le32_to_cpu(tr.epc),
2905 le32_to_cpu(tr.cpsr), le32_to_cpu(tr.spsr),
2906 le32_to_cpu(tr.r13), le32_to_cpu(tr.r14),
Arend van Spriel9bd02c62012-06-26 21:26:40 +02002907 le32_to_cpu(tr.pc), sh->trap_addr,
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002908 le32_to_cpu(tr.r0), le32_to_cpu(tr.r1),
2909 le32_to_cpu(tr.r2), le32_to_cpu(tr.r3),
2910 le32_to_cpu(tr.r4), le32_to_cpu(tr.r5),
2911 le32_to_cpu(tr.r6), le32_to_cpu(tr.r7));
2912
Piotr Haberbaa9e602013-04-05 10:57:42 +02002913 return simple_read_from_buffer(data, count, &pos, buf, res);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002914}
2915
2916static int brcmf_sdio_assert_info(struct brcmf_sdio *bus,
2917 struct sdpcm_shared *sh, char __user *data,
2918 size_t count)
2919{
2920 int error = 0;
2921 char buf[200];
2922 char file[80] = "?";
2923 char expr[80] = "<???>";
2924 int res;
2925 loff_t pos = 0;
2926
2927 if ((sh->flags & SDPCM_SHARED_ASSERT_BUILT) == 0) {
2928 brcmf_dbg(INFO, "firmware not built with -assert\n");
2929 return 0;
2930 } else if ((sh->flags & SDPCM_SHARED_ASSERT) == 0) {
2931 brcmf_dbg(INFO, "no assert in dongle\n");
2932 return 0;
2933 }
2934
Franky Lin38b0b0d2012-11-05 16:22:24 -08002935 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002936 if (sh->assert_file_addr != 0) {
Franky Linba540b02013-04-11 13:28:47 +02002937 error = brcmf_sdio_ramrw(bus->sdiodev, false,
2938 sh->assert_file_addr, (u8 *)file, 80);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002939 if (error < 0)
2940 return error;
2941 }
2942 if (sh->assert_exp_addr != 0) {
Franky Linba540b02013-04-11 13:28:47 +02002943 error = brcmf_sdio_ramrw(bus->sdiodev, false,
2944 sh->assert_exp_addr, (u8 *)expr, 80);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002945 if (error < 0)
2946 return error;
2947 }
Franky Lin38b0b0d2012-11-05 16:22:24 -08002948 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002949
2950 res = scnprintf(buf, sizeof(buf),
2951 "dongle assert: %s:%d: assert(%s)\n",
2952 file, sh->assert_line, expr);
2953 return simple_read_from_buffer(data, count, &pos, buf, res);
2954}
2955
2956static int brcmf_sdbrcm_checkdied(struct brcmf_sdio *bus)
2957{
2958 int error;
2959 struct sdpcm_shared sh;
2960
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002961 error = brcmf_sdio_readshared(bus, &sh);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002962
2963 if (error < 0)
2964 return error;
2965
2966 if ((sh.flags & SDPCM_SHARED_ASSERT_BUILT) == 0)
2967 brcmf_dbg(INFO, "firmware not built with -assert\n");
2968 else if (sh.flags & SDPCM_SHARED_ASSERT)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002969 brcmf_err("assertion in dongle\n");
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002970
2971 if (sh.flags & SDPCM_SHARED_TRAP)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002972 brcmf_err("firmware trap in dongle\n");
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002973
2974 return 0;
2975}
2976
2977static int brcmf_sdbrcm_died_dump(struct brcmf_sdio *bus, char __user *data,
2978 size_t count, loff_t *ppos)
2979{
2980 int error = 0;
2981 struct sdpcm_shared sh;
2982 int nbytes = 0;
2983 loff_t pos = *ppos;
2984
2985 if (pos != 0)
2986 return 0;
2987
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002988 error = brcmf_sdio_readshared(bus, &sh);
2989 if (error < 0)
2990 goto done;
2991
2992 error = brcmf_sdio_assert_info(bus, &sh, data, count);
2993 if (error < 0)
2994 goto done;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002995 nbytes = error;
Piotr Haberbaa9e602013-04-05 10:57:42 +02002996
2997 error = brcmf_sdio_trap_info(bus, &sh, data+nbytes, count);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002998 if (error < 0)
2999 goto done;
Piotr Haberbaa9e602013-04-05 10:57:42 +02003000 nbytes += error;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003001
Piotr Haberbaa9e602013-04-05 10:57:42 +02003002 error = brcmf_sdio_dump_console(bus, &sh, data+nbytes, count);
3003 if (error < 0)
3004 goto done;
3005 nbytes += error;
3006
3007 error = nbytes;
3008 *ppos += nbytes;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003009done:
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003010 return error;
3011}
3012
3013static ssize_t brcmf_sdio_forensic_read(struct file *f, char __user *data,
3014 size_t count, loff_t *ppos)
3015{
3016 struct brcmf_sdio *bus = f->private_data;
3017 int res;
3018
3019 res = brcmf_sdbrcm_died_dump(bus, data, count, ppos);
3020 if (res > 0)
3021 *ppos += res;
3022 return (ssize_t)res;
3023}
3024
3025static const struct file_operations brcmf_sdio_forensic_ops = {
3026 .owner = THIS_MODULE,
3027 .open = simple_open,
3028 .read = brcmf_sdio_forensic_read
3029};
3030
Arend van Spriel80969832012-06-09 22:51:44 +02003031static void brcmf_sdio_debugfs_create(struct brcmf_sdio *bus)
3032{
3033 struct brcmf_pub *drvr = bus->sdiodev->bus_if->drvr;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003034 struct dentry *dentry = brcmf_debugfs_get_devdir(drvr);
Arend van Spriel80969832012-06-09 22:51:44 +02003035
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003036 if (IS_ERR_OR_NULL(dentry))
3037 return;
3038
3039 debugfs_create_file("forensics", S_IRUGO, dentry, bus,
3040 &brcmf_sdio_forensic_ops);
Arend van Spriel80969832012-06-09 22:51:44 +02003041 brcmf_debugfs_create_sdio_count(drvr, &bus->sdcnt);
3042}
3043#else
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003044static int brcmf_sdbrcm_checkdied(struct brcmf_sdio *bus)
3045{
3046 return 0;
3047}
3048
Arend van Spriel80969832012-06-09 22:51:44 +02003049static void brcmf_sdio_debugfs_create(struct brcmf_sdio *bus)
3050{
3051}
3052#endif /* DEBUG */
3053
Franky Linfcf094f2011-12-16 18:37:16 -08003054static int
Franky Lin532cdd32011-11-22 17:21:54 -08003055brcmf_sdbrcm_bus_rxctl(struct device *dev, unsigned char *msg, uint msglen)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003056{
3057 int timeleft;
3058 uint rxlen = 0;
3059 bool pending;
Franky Lindd43a012012-11-05 16:22:22 -08003060 u8 *buf;
Franky Lin532cdd32011-11-22 17:21:54 -08003061 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01003062 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Lin532cdd32011-11-22 17:21:54 -08003063 struct brcmf_sdio *bus = sdiodev->bus;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003064
3065 brcmf_dbg(TRACE, "Enter\n");
3066
3067 /* Wait until control frame is available */
3068 timeleft = brcmf_sdbrcm_dcmd_resp_wait(bus, &bus->rxlen, &pending);
3069
Franky Lindd43a012012-11-05 16:22:22 -08003070 spin_lock_bh(&bus->rxctl_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003071 rxlen = bus->rxlen;
3072 memcpy(msg, bus->rxctl, min(msglen, rxlen));
Franky Lindd43a012012-11-05 16:22:22 -08003073 bus->rxctl = NULL;
3074 buf = bus->rxctl_orig;
3075 bus->rxctl_orig = NULL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003076 bus->rxlen = 0;
Franky Lindd43a012012-11-05 16:22:22 -08003077 spin_unlock_bh(&bus->rxctl_lock);
3078 vfree(buf);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003079
3080 if (rxlen) {
3081 brcmf_dbg(CTL, "resumed on rxctl frame, got %d expected %d\n",
3082 rxlen, msglen);
3083 } else if (timeleft == 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003084 brcmf_err("resumed on timeout\n");
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003085 brcmf_sdbrcm_checkdied(bus);
Joe Perches23677ce2012-02-09 11:17:23 +00003086 } else if (pending) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003087 brcmf_dbg(CTL, "cancelled\n");
3088 return -ERESTARTSYS;
3089 } else {
3090 brcmf_dbg(CTL, "resumed for unknown reason?\n");
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003091 brcmf_sdbrcm_checkdied(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003092 }
3093
3094 if (rxlen)
Arend van Spriel80969832012-06-09 22:51:44 +02003095 bus->sdcnt.rx_ctlpkts++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003096 else
Arend van Spriel80969832012-06-09 22:51:44 +02003097 bus->sdcnt.rx_ctlerrs++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003098
3099 return rxlen ? (int)rxlen : -ETIMEDOUT;
3100}
3101
Franky Lin069eddd2013-04-11 13:28:48 +02003102static bool brcmf_sdbrcm_download_state(struct brcmf_sdio *bus, bool enter)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003103{
Franky Lin99ba15c2011-11-04 22:23:42 +01003104 struct chip_info *ci = bus->ci;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003105
3106 /* To enter download state, disable ARM and reset SOCRAM.
3107 * To exit download state, simply reset ARM (default is RAM boot).
3108 */
3109 if (enter) {
3110 bus->alp_only = true;
3111
Franky Lin069eddd2013-04-11 13:28:48 +02003112 brcmf_sdio_chip_enter_download(bus->sdiodev, ci);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003113 } else {
Franky Lin069eddd2013-04-11 13:28:48 +02003114 if (!brcmf_sdio_chip_exit_download(bus->sdiodev, ci, bus->vars,
3115 bus->varsz))
3116 return false;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003117
3118 /* Allow HT Clock now that the ARM is running. */
3119 bus->alp_only = false;
3120
Franky Lin712ac5b2011-12-16 18:37:09 -08003121 bus->sdiodev->bus_if->state = BRCMF_BUS_LOAD;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003122 }
Franky Lin069eddd2013-04-11 13:28:48 +02003123
3124 return true;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003125}
3126
Franky Line92eedf2011-11-22 17:21:50 -08003127static int brcmf_sdbrcm_download_code_file(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003128{
Hante Meulemanf2c44fe2013-09-25 13:05:45 +02003129 const struct firmware *fw;
3130 int err;
Franky Lin1640f282013-04-11 13:28:51 +02003131 int offset;
Hante Meulemanf2c44fe2013-09-25 13:05:45 +02003132 int address;
3133 int len;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003134
Hante Meulemanf2c44fe2013-09-25 13:05:45 +02003135 fw = brcmf_sdbrcm_get_fw(bus, BRCMF_FIRMWARE_BIN);
3136 if (fw == NULL)
3137 return -ENOENT;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003138
Hante Meulemanf2c44fe2013-09-25 13:05:45 +02003139 if (brcmf_sdio_chip_getinfidx(bus->ci, BCMA_CORE_ARM_CR4) !=
3140 BRCMF_MAX_CORENUM)
3141 memcpy(&bus->ci->rst_vec, fw->data, sizeof(bus->ci->rst_vec));
Arend van Spriel5b435de2011-10-05 13:19:03 +02003142
Hante Meulemanf2c44fe2013-09-25 13:05:45 +02003143 err = 0;
3144 offset = 0;
3145 address = bus->ci->rambase;
3146 while (offset < fw->size) {
3147 len = ((offset + MEMBLOCK) < fw->size) ? MEMBLOCK :
3148 fw->size - offset;
3149 err = brcmf_sdio_ramrw(bus->sdiodev, true, address,
3150 (u8 *)&fw->data[offset], len);
3151 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003152 brcmf_err("error %d on writing %d membytes at 0x%08x\n",
Hante Meulemanf2c44fe2013-09-25 13:05:45 +02003153 err, len, address);
3154 goto failure;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003155 }
Hante Meulemanf2c44fe2013-09-25 13:05:45 +02003156 offset += len;
3157 address += len;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003158 }
3159
Hante Meulemanf2c44fe2013-09-25 13:05:45 +02003160failure:
3161 release_firmware(fw);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003162
Hante Meulemanf2c44fe2013-09-25 13:05:45 +02003163 return err;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003164}
3165
3166/*
3167 * ProcessVars:Takes a buffer of "<var>=<value>\n" lines read from a file
3168 * and ending in a NUL.
3169 * Removes carriage returns, empty lines, comment lines, and converts
3170 * newlines to NULs.
3171 * Shortens buffer as needed and pads with NULs. End of buffer is marked
3172 * by two NULs.
3173*/
3174
Hante Meulemanf2c44fe2013-09-25 13:05:45 +02003175static int brcmf_process_nvram_vars(struct brcmf_sdio *bus,
3176 const struct firmware *nv)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003177{
Franky Lind610cde2012-06-26 21:26:37 +02003178 char *varbuf;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003179 char *dp;
3180 bool findNewline;
3181 int column;
Franky Lind610cde2012-06-26 21:26:37 +02003182 int ret = 0;
3183 uint buf_len, n, len;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003184
Hante Meulemanf2c44fe2013-09-25 13:05:45 +02003185 len = nv->size;
Franky Lind610cde2012-06-26 21:26:37 +02003186 varbuf = vmalloc(len);
3187 if (!varbuf)
3188 return -ENOMEM;
3189
Hante Meulemanf2c44fe2013-09-25 13:05:45 +02003190 memcpy(varbuf, nv->data, len);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003191 dp = varbuf;
3192
3193 findNewline = false;
3194 column = 0;
3195
3196 for (n = 0; n < len; n++) {
3197 if (varbuf[n] == 0)
3198 break;
3199 if (varbuf[n] == '\r')
3200 continue;
3201 if (findNewline && varbuf[n] != '\n')
3202 continue;
3203 findNewline = false;
3204 if (varbuf[n] == '#') {
3205 findNewline = true;
3206 continue;
3207 }
3208 if (varbuf[n] == '\n') {
3209 if (column == 0)
3210 continue;
3211 *dp++ = 0;
3212 column = 0;
3213 continue;
3214 }
3215 *dp++ = varbuf[n];
3216 column++;
3217 }
3218 buf_len = dp - varbuf;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003219 while (dp < varbuf + n)
3220 *dp++ = 0;
3221
Franky Lind610cde2012-06-26 21:26:37 +02003222 kfree(bus->vars);
Arend van Spriel6d4ef682012-06-26 21:26:38 +02003223 /* roundup needed for download to device */
3224 bus->varsz = roundup(buf_len + 1, 4);
Franky Lind610cde2012-06-26 21:26:37 +02003225 bus->vars = kmalloc(bus->varsz, GFP_KERNEL);
3226 if (bus->vars == NULL) {
3227 bus->varsz = 0;
3228 ret = -ENOMEM;
3229 goto err;
3230 }
3231
3232 /* copy the processed variables and add null termination */
3233 memcpy(bus->vars, varbuf, buf_len);
3234 bus->vars[buf_len] = 0;
3235err:
3236 vfree(varbuf);
3237 return ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003238}
3239
Franky Line92eedf2011-11-22 17:21:50 -08003240static int brcmf_sdbrcm_download_nvram(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003241{
Hante Meulemanf2c44fe2013-09-25 13:05:45 +02003242 const struct firmware *nv;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003243 int ret;
3244
Hante Meulemanf2c44fe2013-09-25 13:05:45 +02003245 nv = brcmf_sdbrcm_get_fw(bus, BRCMF_FIRMWARE_NVRAM);
3246 if (nv == NULL)
3247 return -ENOENT;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003248
Hante Meulemanf2c44fe2013-09-25 13:05:45 +02003249 ret = brcmf_process_nvram_vars(bus, nv);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003250
Hante Meulemanf2c44fe2013-09-25 13:05:45 +02003251 release_firmware(nv);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003252
3253 return ret;
3254}
3255
Franky Line92eedf2011-11-22 17:21:50 -08003256static int _brcmf_sdbrcm_download_firmware(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003257{
3258 int bcmerror = -1;
3259
3260 /* Keep arm in reset */
Franky Lin069eddd2013-04-11 13:28:48 +02003261 if (!brcmf_sdbrcm_download_state(bus, true)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003262 brcmf_err("error placing ARM core in reset\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003263 goto err;
3264 }
3265
Arend van Spriel5b435de2011-10-05 13:19:03 +02003266 if (brcmf_sdbrcm_download_code_file(bus)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003267 brcmf_err("dongle image file download failed\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003268 goto err;
3269 }
3270
Franky Lin3eaa9562013-04-02 21:06:20 +02003271 if (brcmf_sdbrcm_download_nvram(bus)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003272 brcmf_err("dongle nvram file download failed\n");
Franky Lin3eaa9562013-04-02 21:06:20 +02003273 goto err;
3274 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02003275
3276 /* Take arm out of reset */
Franky Lin069eddd2013-04-11 13:28:48 +02003277 if (!brcmf_sdbrcm_download_state(bus, false)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003278 brcmf_err("error getting out of ARM core reset\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003279 goto err;
3280 }
3281
3282 bcmerror = 0;
3283
3284err:
3285 return bcmerror;
3286}
3287
Piotr Haber4a3da992013-04-11 13:28:46 +02003288static bool brcmf_sdbrcm_sr_capable(struct brcmf_sdio *bus)
3289{
3290 u32 addr, reg;
3291
3292 brcmf_dbg(TRACE, "Enter\n");
3293
3294 /* old chips with PMU version less than 17 don't support save restore */
3295 if (bus->ci->pmurev < 17)
3296 return false;
3297
3298 /* read PMU chipcontrol register 3*/
3299 addr = CORE_CC_REG(bus->ci->c_inf[0].base, chipcontrol_addr);
3300 brcmf_sdio_regwl(bus->sdiodev, addr, 3, NULL);
3301 addr = CORE_CC_REG(bus->ci->c_inf[0].base, chipcontrol_data);
3302 reg = brcmf_sdio_regrl(bus->sdiodev, addr, NULL);
3303
3304 return (bool)reg;
3305}
3306
3307static void brcmf_sdbrcm_sr_init(struct brcmf_sdio *bus)
3308{
3309 int err = 0;
3310 u8 val;
3311
3312 brcmf_dbg(TRACE, "Enter\n");
3313
3314 val = brcmf_sdio_regrb(bus->sdiodev, SBSDIO_FUNC1_WAKEUPCTRL,
3315 &err);
3316 if (err) {
3317 brcmf_err("error reading SBSDIO_FUNC1_WAKEUPCTRL\n");
3318 return;
3319 }
3320
3321 val |= 1 << SBSDIO_FUNC1_WCTRL_HTWAIT_SHIFT;
3322 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_WAKEUPCTRL,
3323 val, &err);
3324 if (err) {
3325 brcmf_err("error writing SBSDIO_FUNC1_WAKEUPCTRL\n");
3326 return;
3327 }
3328
3329 /* Add CMD14 Support */
3330 brcmf_sdio_regwb(bus->sdiodev, SDIO_CCCR_BRCM_CARDCAP,
3331 (SDIO_CCCR_BRCM_CARDCAP_CMD14_SUPPORT |
3332 SDIO_CCCR_BRCM_CARDCAP_CMD14_EXT),
3333 &err);
3334 if (err) {
3335 brcmf_err("error writing SDIO_CCCR_BRCM_CARDCAP\n");
3336 return;
3337 }
3338
3339 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3340 SBSDIO_FORCE_HT, &err);
3341 if (err) {
3342 brcmf_err("error writing SBSDIO_FUNC1_CHIPCLKCSR\n");
3343 return;
3344 }
3345
3346 /* set flag */
3347 bus->sr_enabled = true;
3348 brcmf_dbg(INFO, "SR enabled\n");
3349}
3350
3351/* enable KSO bit */
3352static int brcmf_sdbrcm_kso_init(struct brcmf_sdio *bus)
3353{
3354 u8 val;
3355 int err = 0;
3356
3357 brcmf_dbg(TRACE, "Enter\n");
3358
3359 /* KSO bit added in SDIO core rev 12 */
3360 if (bus->ci->c_inf[1].rev < 12)
3361 return 0;
3362
3363 val = brcmf_sdio_regrb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
3364 &err);
3365 if (err) {
3366 brcmf_err("error reading SBSDIO_FUNC1_SLEEPCSR\n");
3367 return err;
3368 }
3369
3370 if (!(val & SBSDIO_FUNC1_SLEEPCSR_KSO_MASK)) {
3371 val |= (SBSDIO_FUNC1_SLEEPCSR_KSO_EN <<
3372 SBSDIO_FUNC1_SLEEPCSR_KSO_SHIFT);
3373 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
3374 val, &err);
3375 if (err) {
3376 brcmf_err("error writing SBSDIO_FUNC1_SLEEPCSR\n");
3377 return err;
3378 }
3379 }
3380
3381 return 0;
3382}
3383
3384
Arend van Spriel5b435de2011-10-05 13:19:03 +02003385static bool
Franky Line92eedf2011-11-22 17:21:50 -08003386brcmf_sdbrcm_download_firmware(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003387{
3388 bool ret;
3389
Franky Lin38b0b0d2012-11-05 16:22:24 -08003390 sdio_claim_host(bus->sdiodev->func[1]);
3391
Arend van Spriel5b435de2011-10-05 13:19:03 +02003392 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
3393
3394 ret = _brcmf_sdbrcm_download_firmware(bus) == 0;
3395
3396 brcmf_sdbrcm_clkctl(bus, CLK_SDONLY, false);
3397
Franky Lin38b0b0d2012-11-05 16:22:24 -08003398 sdio_release_host(bus->sdiodev->func[1]);
3399
Arend van Spriel5b435de2011-10-05 13:19:03 +02003400 return ret;
3401}
3402
Franky Lin99a0b8f2011-12-16 18:37:14 -08003403static int brcmf_sdbrcm_bus_init(struct device *dev)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003404{
Franky Linfa20b912011-11-22 17:21:57 -08003405 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01003406 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Linfa20b912011-11-22 17:21:57 -08003407 struct brcmf_sdio *bus = sdiodev->bus;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003408 unsigned long timeout;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003409 u8 ready, enable;
3410 int err, ret = 0;
3411 u8 saveclk;
3412
3413 brcmf_dbg(TRACE, "Enter\n");
3414
3415 /* try to download image and nvram to the dongle */
Franky Linfa20b912011-11-22 17:21:57 -08003416 if (bus_if->state == BRCMF_BUS_DOWN) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003417 if (!(brcmf_sdbrcm_download_firmware(bus)))
3418 return -1;
3419 }
3420
Franky Lin712ac5b2011-12-16 18:37:09 -08003421 if (!bus->sdiodev->bus_if->drvr)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003422 return 0;
3423
3424 /* Start the watchdog timer */
Arend van Spriel80969832012-06-09 22:51:44 +02003425 bus->sdcnt.tickcnt = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003426 brcmf_sdbrcm_wd_timer(bus, BRCMF_WD_POLL_MS);
3427
Franky Lin38b0b0d2012-11-05 16:22:24 -08003428 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003429
3430 /* Make sure backplane clock is on, needed to generate F2 interrupt */
3431 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
3432 if (bus->clkstate != CLK_AVAIL)
3433 goto exit;
3434
3435 /* Force clocks on backplane to be sure F2 interrupt propagates */
Franky Lin45db3392012-05-04 18:27:32 -07003436 saveclk = brcmf_sdio_regrb(bus->sdiodev,
3437 SBSDIO_FUNC1_CHIPCLKCSR, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003438 if (!err) {
Franky Lin3bba8292012-05-04 18:27:33 -07003439 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3440 (saveclk | SBSDIO_FORCE_HT), &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003441 }
3442 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003443 brcmf_err("Failed to force clock for F2: err %d\n", err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003444 goto exit;
3445 }
3446
3447 /* Enable function 2 (frame transfers) */
3448 w_sdreg32(bus, SDPCM_PROT_VERSION << SMB_DATA_VERSION_SHIFT,
Franky Lin58692752012-05-04 18:27:36 -07003449 offsetof(struct sdpcmd_regs, tosbmailboxdata));
Arend van Spriel5b435de2011-10-05 13:19:03 +02003450 enable = (SDIO_FUNC_ENABLE_1 | SDIO_FUNC_ENABLE_2);
3451
Franky Lin3bba8292012-05-04 18:27:33 -07003452 brcmf_sdio_regwb(bus->sdiodev, SDIO_CCCR_IOEx, enable, NULL);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003453
3454 timeout = jiffies + msecs_to_jiffies(BRCMF_WAIT_F2RDY);
3455 ready = 0;
3456 while (enable != ready) {
Franky Lin45db3392012-05-04 18:27:32 -07003457 ready = brcmf_sdio_regrb(bus->sdiodev,
3458 SDIO_CCCR_IORx, NULL);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003459 if (time_after(jiffies, timeout))
3460 break;
3461 else if (time_after(jiffies, timeout - BRCMF_WAIT_F2RDY + 50))
3462 /* prevent busy waiting if it takes too long */
3463 msleep_interruptible(20);
3464 }
3465
3466 brcmf_dbg(INFO, "enable 0x%02x, ready 0x%02x\n", enable, ready);
3467
3468 /* If F2 successfully enabled, set core and enable interrupts */
3469 if (ready == enable) {
3470 /* Set up the interrupt mask and enable interrupts */
3471 bus->hostintmask = HOSTINTMASK;
3472 w_sdreg32(bus, bus->hostintmask,
Franky Lin58692752012-05-04 18:27:36 -07003473 offsetof(struct sdpcmd_regs, hostintmask));
Arend van Spriel5b435de2011-10-05 13:19:03 +02003474
Franky Lin3bba8292012-05-04 18:27:33 -07003475 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_WATERMARK, 8, &err);
Arend van Sprielc0e89f02012-02-09 21:09:04 +01003476 } else {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003477 /* Disable F2 again */
3478 enable = SDIO_FUNC_ENABLE_1;
Franky Lin3bba8292012-05-04 18:27:33 -07003479 brcmf_sdio_regwb(bus->sdiodev, SDIO_CCCR_IOEx, enable, NULL);
Arend van Sprielc0e89f02012-02-09 21:09:04 +01003480 ret = -ENODEV;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003481 }
3482
Piotr Haber4a3da992013-04-11 13:28:46 +02003483 if (brcmf_sdbrcm_sr_capable(bus)) {
3484 brcmf_sdbrcm_sr_init(bus);
3485 } else {
3486 /* Restore previous clock setting */
3487 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3488 saveclk, &err);
3489 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02003490
Franky Line2f93cc2012-04-27 18:56:58 -07003491 if (ret == 0) {
Franky Linba89bf12012-04-27 18:56:59 -07003492 ret = brcmf_sdio_intr_register(bus->sdiodev);
Franky Line2f93cc2012-04-27 18:56:58 -07003493 if (ret != 0)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003494 brcmf_err("intr register failed:%d\n", ret);
Franky Line2f93cc2012-04-27 18:56:58 -07003495 }
3496
Arend van Spriel5b435de2011-10-05 13:19:03 +02003497 /* If we didn't come up, turn off backplane clock */
Franky Lind9126e02012-04-27 18:56:57 -07003498 if (bus_if->state != BRCMF_BUS_DATA)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003499 brcmf_sdbrcm_clkctl(bus, CLK_NONE, false);
3500
3501exit:
Franky Lin38b0b0d2012-11-05 16:22:24 -08003502 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003503
3504 return ret;
3505}
3506
3507void brcmf_sdbrcm_isr(void *arg)
3508{
Franky Line92eedf2011-11-22 17:21:50 -08003509 struct brcmf_sdio *bus = (struct brcmf_sdio *) arg;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003510
3511 brcmf_dbg(TRACE, "Enter\n");
3512
3513 if (!bus) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003514 brcmf_err("bus is null pointer, exiting\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003515 return;
3516 }
3517
Franky Lin712ac5b2011-12-16 18:37:09 -08003518 if (bus->sdiodev->bus_if->state == BRCMF_BUS_DOWN) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003519 brcmf_err("bus is down. we have nothing to do\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003520 return;
3521 }
3522 /* Count the interrupt call */
Arend van Spriel80969832012-06-09 22:51:44 +02003523 bus->sdcnt.intrcount++;
Franky Lin45316032012-09-13 21:12:03 +02003524 if (in_interrupt())
3525 atomic_set(&bus->ipend, 1);
3526 else
3527 if (brcmf_sdio_intr_rstatus(bus)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003528 brcmf_err("failed backplane access\n");
Franky Lin45316032012-09-13 21:12:03 +02003529 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
3530 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02003531
Arend van Spriel5b435de2011-10-05 13:19:03 +02003532 /* Disable additional interrupts (is this needed now)? */
3533 if (!bus->intr)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003534 brcmf_err("isr w/o interrupt configured!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003535
Arend van Sprielfccfe932013-06-18 13:29:26 +02003536 atomic_inc(&bus->dpc_tskcnt);
Franky Linf1e68c22012-09-13 21:12:00 +02003537 queue_work(bus->brcmf_wq, &bus->datawork);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003538}
3539
Franky Lincad2b262011-11-22 17:21:53 -08003540static bool brcmf_sdbrcm_bus_watchdog(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003541{
Joe Perches8ae74652012-01-15 00:38:38 -08003542#ifdef DEBUG
Franky Lincad2b262011-11-22 17:21:53 -08003543 struct brcmf_bus *bus_if = dev_get_drvdata(bus->sdiodev->dev);
Joe Perches8ae74652012-01-15 00:38:38 -08003544#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02003545
3546 brcmf_dbg(TIMER, "Enter\n");
3547
Arend van Spriel5b435de2011-10-05 13:19:03 +02003548 /* Poll period: check device if appropriate. */
Piotr Haber4a3da992013-04-11 13:28:46 +02003549 if (!bus->sr_enabled &&
3550 bus->poll && (++bus->polltick >= bus->pollrate)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003551 u32 intstatus = 0;
3552
3553 /* Reset poll tick */
3554 bus->polltick = 0;
3555
3556 /* Check device if no interrupts */
Arend van Spriel80969832012-06-09 22:51:44 +02003557 if (!bus->intr ||
3558 (bus->sdcnt.intrcount == bus->sdcnt.lastintrs)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003559
Arend van Sprielfccfe932013-06-18 13:29:26 +02003560 if (atomic_read(&bus->dpc_tskcnt) == 0) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003561 u8 devpend;
Arend van Sprielfccfe932013-06-18 13:29:26 +02003562
Franky Lin38b0b0d2012-11-05 16:22:24 -08003563 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin45db3392012-05-04 18:27:32 -07003564 devpend = brcmf_sdio_regrb(bus->sdiodev,
3565 SDIO_CCCR_INTx,
3566 NULL);
Franky Lin38b0b0d2012-11-05 16:22:24 -08003567 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003568 intstatus =
3569 devpend & (INTR_STATUS_FUNC1 |
3570 INTR_STATUS_FUNC2);
3571 }
3572
3573 /* If there is something, make like the ISR and
3574 schedule the DPC */
3575 if (intstatus) {
Arend van Spriel80969832012-06-09 22:51:44 +02003576 bus->sdcnt.pollcnt++;
Franky Lin1d382272012-09-13 21:11:59 +02003577 atomic_set(&bus->ipend, 1);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003578
Arend van Sprielfccfe932013-06-18 13:29:26 +02003579 atomic_inc(&bus->dpc_tskcnt);
Franky Linf1e68c22012-09-13 21:12:00 +02003580 queue_work(bus->brcmf_wq, &bus->datawork);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003581 }
3582 }
3583
3584 /* Update interrupt tracking */
Arend van Spriel80969832012-06-09 22:51:44 +02003585 bus->sdcnt.lastintrs = bus->sdcnt.intrcount;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003586 }
Joe Perches8ae74652012-01-15 00:38:38 -08003587#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02003588 /* Poll for console output periodically */
Hante Meuleman2def5c12012-11-14 18:46:19 -08003589 if (bus_if && bus_if->state == BRCMF_BUS_DATA &&
Franky Lin8d169aa2011-11-22 17:21:52 -08003590 bus->console_interval != 0) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003591 bus->console.count += BRCMF_WD_POLL_MS;
3592 if (bus->console.count >= bus->console_interval) {
3593 bus->console.count -= bus->console_interval;
Franky Lin38b0b0d2012-11-05 16:22:24 -08003594 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003595 /* Make sure backplane clock is on */
Piotr Haber4a3da992013-04-11 13:28:46 +02003596 brcmf_sdbrcm_bus_sleep(bus, false, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003597 if (brcmf_sdbrcm_readconsole(bus) < 0)
3598 /* stop on error */
3599 bus->console_interval = 0;
Franky Lin38b0b0d2012-11-05 16:22:24 -08003600 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003601 }
3602 }
Joe Perches8ae74652012-01-15 00:38:38 -08003603#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02003604
3605 /* On idle timeout clear activity flag and/or turn off clock */
3606 if ((bus->idletime > 0) && (bus->clkstate == CLK_AVAIL)) {
3607 if (++bus->idlecount >= bus->idletime) {
3608 bus->idlecount = 0;
3609 if (bus->activity) {
3610 bus->activity = false;
3611 brcmf_sdbrcm_wd_timer(bus, BRCMF_WD_POLL_MS);
3612 } else {
Piotr Haber4a3da992013-04-11 13:28:46 +02003613 brcmf_dbg(SDIO, "idle\n");
Franky Lin38b0b0d2012-11-05 16:22:24 -08003614 sdio_claim_host(bus->sdiodev->func[1]);
Piotr Haber4a3da992013-04-11 13:28:46 +02003615 brcmf_sdbrcm_bus_sleep(bus, true, false);
Franky Lin38b0b0d2012-11-05 16:22:24 -08003616 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003617 }
3618 }
3619 }
3620
Franky Lin1d382272012-09-13 21:11:59 +02003621 return (atomic_read(&bus->ipend) > 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003622}
3623
Franky Linf1e68c22012-09-13 21:12:00 +02003624static void brcmf_sdio_dataworker(struct work_struct *work)
3625{
3626 struct brcmf_sdio *bus = container_of(work, struct brcmf_sdio,
3627 datawork);
Franky Linf1e68c22012-09-13 21:12:00 +02003628
Arend van Sprielfccfe932013-06-18 13:29:26 +02003629 while (atomic_read(&bus->dpc_tskcnt)) {
Franky Linf1e68c22012-09-13 21:12:00 +02003630 brcmf_sdbrcm_dpc(bus);
Arend van Sprielfccfe932013-06-18 13:29:26 +02003631 atomic_dec(&bus->dpc_tskcnt);
Franky Linf1e68c22012-09-13 21:12:00 +02003632 }
Franky Linf1e68c22012-09-13 21:12:00 +02003633}
3634
Franky Line92eedf2011-11-22 17:21:50 -08003635static void brcmf_sdbrcm_release_malloc(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003636{
3637 brcmf_dbg(TRACE, "Enter\n");
3638
3639 kfree(bus->rxbuf);
3640 bus->rxctl = bus->rxbuf = NULL;
3641 bus->rxlen = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003642}
3643
Franky Line92eedf2011-11-22 17:21:50 -08003644static bool brcmf_sdbrcm_probe_malloc(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003645{
3646 brcmf_dbg(TRACE, "Enter\n");
3647
Franky Linb01a6b32011-12-16 18:37:03 -08003648 if (bus->sdiodev->bus_if->maxctl) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003649 bus->rxblen =
Franky Linb01a6b32011-12-16 18:37:03 -08003650 roundup((bus->sdiodev->bus_if->maxctl + SDPCM_HDRLEN),
Arend van Spriel5b435de2011-10-05 13:19:03 +02003651 ALIGNMENT) + BRCMF_SDALIGN;
3652 bus->rxbuf = kmalloc(bus->rxblen, GFP_ATOMIC);
3653 if (!(bus->rxbuf))
Franky Lin354b75b2013-06-18 13:29:29 +02003654 return false;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003655 }
3656
Arend van Spriel5b435de2011-10-05 13:19:03 +02003657 return true;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003658}
3659
Arend van Spriel5b435de2011-10-05 13:19:03 +02003660static bool
Franky Line92eedf2011-11-22 17:21:50 -08003661brcmf_sdbrcm_probe_attach(struct brcmf_sdio *bus, u32 regsva)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003662{
3663 u8 clkctl = 0;
3664 int err = 0;
3665 int reg_addr;
3666 u32 reg_val;
Hante Meuleman668761a2013-04-12 10:55:55 +02003667 u32 drivestrength;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003668
3669 bus->alp_only = true;
3670
Franky Lin38b0b0d2012-11-05 16:22:24 -08003671 sdio_claim_host(bus->sdiodev->func[1]);
3672
Joe Perches18aad4f2012-01-15 00:38:42 -08003673 pr_debug("F1 signature read @0x18000000=0x%4x\n",
Franky Lin79ae3952012-05-04 18:27:34 -07003674 brcmf_sdio_regrl(bus->sdiodev, SI_ENUM_BASE, NULL));
Arend van Spriel5b435de2011-10-05 13:19:03 +02003675
3676 /*
Franky Lina97e4fc2011-11-04 22:23:35 +01003677 * Force PLL off until brcmf_sdio_chip_attach()
Arend van Spriel5b435de2011-10-05 13:19:03 +02003678 * programs PLL control regs
3679 */
3680
Franky Lin3bba8292012-05-04 18:27:33 -07003681 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3682 BRCMF_INIT_CLKCTL1, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003683 if (!err)
Franky Lin45db3392012-05-04 18:27:32 -07003684 clkctl = brcmf_sdio_regrb(bus->sdiodev,
Arend van Spriel5b435de2011-10-05 13:19:03 +02003685 SBSDIO_FUNC1_CHIPCLKCSR, &err);
3686
3687 if (err || ((clkctl & ~SBSDIO_AVBITS) != BRCMF_INIT_CLKCTL1)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003688 brcmf_err("ChipClkCSR access: err %d wrote 0x%02x read 0x%02x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02003689 err, BRCMF_INIT_CLKCTL1, clkctl);
3690 goto fail;
3691 }
3692
Franky Lina97e4fc2011-11-04 22:23:35 +01003693 if (brcmf_sdio_chip_attach(bus->sdiodev, &bus->ci, regsva)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003694 brcmf_err("brcmf_sdio_chip_attach failed!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003695 goto fail;
3696 }
3697
Piotr Haber4a3da992013-04-11 13:28:46 +02003698 if (brcmf_sdbrcm_kso_init(bus)) {
3699 brcmf_err("error enabling KSO\n");
3700 goto fail;
3701 }
3702
Hante Meuleman668761a2013-04-12 10:55:55 +02003703 if ((bus->sdiodev->pdata) && (bus->sdiodev->pdata->drive_strength))
3704 drivestrength = bus->sdiodev->pdata->drive_strength;
3705 else
3706 drivestrength = DEFAULT_SDIO_DRIVE_STRENGTH;
3707 brcmf_sdio_chip_drivestrengthinit(bus->sdiodev, bus->ci, drivestrength);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003708
Franky Lin454d2a82011-11-04 22:23:37 +01003709 /* Get info on the SOCRAM cores... */
Arend van Spriel5b435de2011-10-05 13:19:03 +02003710 bus->ramsize = bus->ci->ramsize;
3711 if (!(bus->ramsize)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003712 brcmf_err("failed to find SOCRAM memory!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003713 goto fail;
3714 }
3715
Piotr Haber1e9ab4d2013-04-11 13:28:52 +02003716 /* Set card control so an SDIO card reset does a WLAN backplane reset */
3717 reg_val = brcmf_sdio_regrb(bus->sdiodev,
3718 SDIO_CCCR_BRCM_CARDCTRL, &err);
3719 if (err)
3720 goto fail;
3721
3722 reg_val |= SDIO_CCCR_BRCM_CARDCTRL_WLANRESET;
3723
3724 brcmf_sdio_regwb(bus->sdiodev,
3725 SDIO_CCCR_BRCM_CARDCTRL, reg_val, &err);
3726 if (err)
3727 goto fail;
3728
3729 /* set PMUControl so a backplane reset does PMU state reload */
3730 reg_addr = CORE_CC_REG(bus->ci->c_inf[0].base,
3731 pmucontrol);
3732 reg_val = brcmf_sdio_regrl(bus->sdiodev,
3733 reg_addr,
3734 &err);
3735 if (err)
3736 goto fail;
3737
3738 reg_val |= (BCMA_CC_PMU_CTL_RES_RELOAD << BCMA_CC_PMU_CTL_RES_SHIFT);
3739
3740 brcmf_sdio_regwl(bus->sdiodev,
3741 reg_addr,
3742 reg_val,
3743 &err);
3744 if (err)
3745 goto fail;
3746
Arend van Spriel5b435de2011-10-05 13:19:03 +02003747
Franky Lin38b0b0d2012-11-05 16:22:24 -08003748 sdio_release_host(bus->sdiodev->func[1]);
3749
Arend van Spriel5b435de2011-10-05 13:19:03 +02003750 brcmu_pktq_init(&bus->txq, (PRIOMASK + 1), TXQLEN);
3751
3752 /* Locate an appropriately-aligned portion of hdrbuf */
3753 bus->rxhdr = (u8 *) roundup((unsigned long)&bus->hdrbuf[0],
3754 BRCMF_SDALIGN);
3755
3756 /* Set the poll and/or interrupt flags */
3757 bus->intr = true;
3758 bus->poll = false;
3759 if (bus->poll)
3760 bus->pollrate = 1;
3761
3762 return true;
3763
3764fail:
Franky Lin38b0b0d2012-11-05 16:22:24 -08003765 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003766 return false;
3767}
3768
Franky Line92eedf2011-11-22 17:21:50 -08003769static bool brcmf_sdbrcm_probe_init(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003770{
3771 brcmf_dbg(TRACE, "Enter\n");
3772
Franky Lin38b0b0d2012-11-05 16:22:24 -08003773 sdio_claim_host(bus->sdiodev->func[1]);
3774
Arend van Spriel5b435de2011-10-05 13:19:03 +02003775 /* Disable F2 to clear any intermediate frame state on the dongle */
Franky Lin3bba8292012-05-04 18:27:33 -07003776 brcmf_sdio_regwb(bus->sdiodev, SDIO_CCCR_IOEx,
3777 SDIO_FUNC_ENABLE_1, NULL);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003778
Franky Lin712ac5b2011-12-16 18:37:09 -08003779 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003780 bus->rxflow = false;
3781
3782 /* Done with backplane-dependent accesses, can drop clock... */
Franky Lin3bba8292012-05-04 18:27:33 -07003783 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, 0, NULL);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003784
Franky Lin38b0b0d2012-11-05 16:22:24 -08003785 sdio_release_host(bus->sdiodev->func[1]);
3786
Arend van Spriel5b435de2011-10-05 13:19:03 +02003787 /* ...and initialize clock/power states */
3788 bus->clkstate = CLK_SDONLY;
3789 bus->idletime = BRCMF_IDLE_INTERVAL;
3790 bus->idleclock = BRCMF_IDLE_ACTIVE;
3791
3792 /* Query the F2 block size, set roundup accordingly */
3793 bus->blocksize = bus->sdiodev->func[2]->cur_blksize;
3794 bus->roundup = min(max_roundup, bus->blocksize);
3795
Piotr Haber4a3da992013-04-11 13:28:46 +02003796 /* SR state */
3797 bus->sleeping = false;
3798 bus->sr_enabled = false;
3799
Arend van Spriel5b435de2011-10-05 13:19:03 +02003800 return true;
3801}
3802
3803static int
3804brcmf_sdbrcm_watchdog_thread(void *data)
3805{
Franky Line92eedf2011-11-22 17:21:50 -08003806 struct brcmf_sdio *bus = (struct brcmf_sdio *)data;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003807
3808 allow_signal(SIGTERM);
3809 /* Run until signal received */
3810 while (1) {
3811 if (kthread_should_stop())
3812 break;
3813 if (!wait_for_completion_interruptible(&bus->watchdog_wait)) {
Franky Lincad2b262011-11-22 17:21:53 -08003814 brcmf_sdbrcm_bus_watchdog(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003815 /* Count the tick for reference */
Arend van Spriel80969832012-06-09 22:51:44 +02003816 bus->sdcnt.tickcnt++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003817 } else
3818 break;
3819 }
3820 return 0;
3821}
3822
3823static void
3824brcmf_sdbrcm_watchdog(unsigned long data)
3825{
Franky Line92eedf2011-11-22 17:21:50 -08003826 struct brcmf_sdio *bus = (struct brcmf_sdio *)data;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003827
3828 if (bus->watchdog_tsk) {
3829 complete(&bus->watchdog_wait);
3830 /* Reschedule the watchdog */
3831 if (bus->wd_timer_valid)
3832 mod_timer(&bus->timer,
3833 jiffies + BRCMF_WD_POLL_MS * HZ / 1000);
3834 }
3835}
3836
Franky Line92eedf2011-11-22 17:21:50 -08003837static void brcmf_sdbrcm_release_dongle(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003838{
3839 brcmf_dbg(TRACE, "Enter\n");
3840
3841 if (bus->ci) {
Franky Lin38b0b0d2012-11-05 16:22:24 -08003842 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003843 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
3844 brcmf_sdbrcm_clkctl(bus, CLK_NONE, false);
Franky Lin38b0b0d2012-11-05 16:22:24 -08003845 sdio_release_host(bus->sdiodev->func[1]);
Franky Lina8a6c042011-11-04 22:23:39 +01003846 brcmf_sdio_chip_detach(&bus->ci);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003847 if (bus->vars && bus->varsz)
3848 kfree(bus->vars);
3849 bus->vars = NULL;
3850 }
3851
3852 brcmf_dbg(TRACE, "Disconnected\n");
3853}
3854
3855/* Detach and free everything */
Franky Line92eedf2011-11-22 17:21:50 -08003856static void brcmf_sdbrcm_release(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003857{
3858 brcmf_dbg(TRACE, "Enter\n");
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003859
Arend van Spriel5b435de2011-10-05 13:19:03 +02003860 if (bus) {
3861 /* De-register interrupt handler */
Franky Linba89bf12012-04-27 18:56:59 -07003862 brcmf_sdio_intr_unregister(bus->sdiodev);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003863
Franky Linf1e68c22012-09-13 21:12:00 +02003864 cancel_work_sync(&bus->datawork);
Hante Meuleman37ac5782012-11-14 18:46:18 -08003865 if (bus->brcmf_wq)
3866 destroy_workqueue(bus->brcmf_wq);
Franky Linf1e68c22012-09-13 21:12:00 +02003867
Franky Lin5f947ad2011-12-16 18:36:56 -08003868 if (bus->sdiodev->bus_if->drvr) {
3869 brcmf_detach(bus->sdiodev->dev);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003870 brcmf_sdbrcm_release_dongle(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003871 }
3872
3873 brcmf_sdbrcm_release_malloc(bus);
3874
3875 kfree(bus);
3876 }
3877
3878 brcmf_dbg(TRACE, "Disconnected\n");
3879}
3880
Arend van Sprield9cb2592012-12-05 15:25:54 +01003881static struct brcmf_bus_ops brcmf_sdio_bus_ops = {
3882 .stop = brcmf_sdbrcm_bus_stop,
3883 .init = brcmf_sdbrcm_bus_init,
3884 .txdata = brcmf_sdbrcm_bus_txdata,
3885 .txctl = brcmf_sdbrcm_bus_txctl,
3886 .rxctl = brcmf_sdbrcm_bus_rxctl,
Arend van Spriele2432b62013-04-03 12:40:38 +02003887 .gettxq = brcmf_sdbrcm_bus_gettxq,
Arend van Sprield9cb2592012-12-05 15:25:54 +01003888};
3889
Franky Lin4175b882011-11-22 17:21:49 -08003890void *brcmf_sdbrcm_probe(u32 regsva, struct brcmf_sdio_dev *sdiodev)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003891{
3892 int ret;
Franky Line92eedf2011-11-22 17:21:50 -08003893 struct brcmf_sdio *bus;
Franky Linbbfd6a62012-06-26 21:26:35 +02003894 struct brcmf_bus_dcmd *dlst;
3895 u32 dngl_txglom;
Franky Lincb7f7962013-08-10 12:27:27 +02003896 u32 txglomalign = 0;
Franky Linbbfd6a62012-06-26 21:26:35 +02003897 u8 idx;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003898
Arend van Spriel5b435de2011-10-05 13:19:03 +02003899 brcmf_dbg(TRACE, "Enter\n");
3900
3901 /* We make an assumption about address window mappings:
3902 * regsva == SI_ENUM_BASE*/
3903
3904 /* Allocate private bus interface state */
Franky Line92eedf2011-11-22 17:21:50 -08003905 bus = kzalloc(sizeof(struct brcmf_sdio), GFP_ATOMIC);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003906 if (!bus)
3907 goto fail;
3908
3909 bus->sdiodev = sdiodev;
3910 sdiodev->bus = bus;
Arend van Sprielb83db862011-10-19 12:51:09 +02003911 skb_queue_head_init(&bus->glom);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003912 bus->txbound = BRCMF_TXBOUND;
3913 bus->rxbound = BRCMF_RXBOUND;
3914 bus->txminmax = BRCMF_TXMINMAX;
Franky Lin6bc52312013-08-10 12:27:28 +02003915 bus->tx_seq = SDPCM_SEQ_WRAP - 1;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003916
Hante Meuleman37ac5782012-11-14 18:46:18 -08003917 INIT_WORK(&bus->datawork, brcmf_sdio_dataworker);
3918 bus->brcmf_wq = create_singlethread_workqueue("brcmf_wq");
3919 if (bus->brcmf_wq == NULL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003920 brcmf_err("insufficient memory to create txworkqueue\n");
Hante Meuleman37ac5782012-11-14 18:46:18 -08003921 goto fail;
3922 }
3923
Arend van Spriel5b435de2011-10-05 13:19:03 +02003924 /* attempt to attach to the dongle */
3925 if (!(brcmf_sdbrcm_probe_attach(bus, regsva))) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003926 brcmf_err("brcmf_sdbrcm_probe_attach failed\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003927 goto fail;
3928 }
3929
Franky Lindd43a012012-11-05 16:22:22 -08003930 spin_lock_init(&bus->rxctl_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003931 spin_lock_init(&bus->txqlock);
3932 init_waitqueue_head(&bus->ctrl_wait);
3933 init_waitqueue_head(&bus->dcmd_resp_wait);
3934
3935 /* Set up the watchdog timer */
3936 init_timer(&bus->timer);
3937 bus->timer.data = (unsigned long)bus;
3938 bus->timer.function = brcmf_sdbrcm_watchdog;
3939
Arend van Spriel5b435de2011-10-05 13:19:03 +02003940 /* Initialize watchdog thread */
3941 init_completion(&bus->watchdog_wait);
3942 bus->watchdog_tsk = kthread_run(brcmf_sdbrcm_watchdog_thread,
3943 bus, "brcmf_watchdog");
3944 if (IS_ERR(bus->watchdog_tsk)) {
Joe Perches02f77192012-01-15 00:38:44 -08003945 pr_warn("brcmf_watchdog thread failed to start\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003946 bus->watchdog_tsk = NULL;
3947 }
3948 /* Initialize DPC thread */
Arend van Sprielfccfe932013-06-18 13:29:26 +02003949 atomic_set(&bus->dpc_tskcnt, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003950
Franky Lina9ffda82011-12-16 18:37:12 -08003951 /* Assign bus interface call back */
Arend van Sprield9cb2592012-12-05 15:25:54 +01003952 bus->sdiodev->bus_if->dev = bus->sdiodev->dev;
3953 bus->sdiodev->bus_if->ops = &brcmf_sdio_bus_ops;
Arend van Spriel75d907d2013-02-06 18:40:45 +01003954 bus->sdiodev->bus_if->chip = bus->ci->chip;
3955 bus->sdiodev->bus_if->chiprev = bus->ci->chiprev;
Arend van Sprield9cb2592012-12-05 15:25:54 +01003956
Franky Lin706478c2013-08-10 12:27:29 +02003957 /* default sdio bus header length for tx packet */
3958 bus->tx_hdrlen = SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN;
3959
3960 /* Attach to the common layer, reserve hdr space */
3961 ret = brcmf_attach(bus->tx_hdrlen, bus->sdiodev->dev);
Franky Lin712ac5b2011-12-16 18:37:09 -08003962 if (ret != 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003963 brcmf_err("brcmf_attach failed\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003964 goto fail;
3965 }
3966
3967 /* Allocate buffers */
3968 if (!(brcmf_sdbrcm_probe_malloc(bus))) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003969 brcmf_err("brcmf_sdbrcm_probe_malloc failed\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003970 goto fail;
3971 }
3972
3973 if (!(brcmf_sdbrcm_probe_init(bus))) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003974 brcmf_err("brcmf_sdbrcm_probe_init failed\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003975 goto fail;
3976 }
3977
Arend van Spriel80969832012-06-09 22:51:44 +02003978 brcmf_sdio_debugfs_create(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003979 brcmf_dbg(INFO, "completed!!\n");
3980
Franky Linbbfd6a62012-06-26 21:26:35 +02003981 /* sdio bus core specific dcmd */
3982 idx = brcmf_sdio_chip_getinfidx(bus->ci, BCMA_CORE_SDIO_DEV);
3983 dlst = kzalloc(sizeof(struct brcmf_bus_dcmd), GFP_KERNEL);
Franky Linc3d2bc32012-06-26 21:26:36 +02003984 if (dlst) {
3985 if (bus->ci->c_inf[idx].rev < 12) {
3986 /* for sdio core rev < 12, disable txgloming */
3987 dngl_txglom = 0;
3988 dlst->name = "bus:txglom";
3989 dlst->param = (char *)&dngl_txglom;
3990 dlst->param_len = sizeof(u32);
3991 } else {
3992 /* otherwise, set txglomalign */
Franky Lincb7f7962013-08-10 12:27:27 +02003993 if (sdiodev->pdata)
3994 txglomalign = sdiodev->pdata->sd_sgentry_align;
3995 /* SDIO ADMA requires at least 32 bit alignment */
3996 if (txglomalign < 4)
3997 txglomalign = 4;
Franky Linc3d2bc32012-06-26 21:26:36 +02003998 dlst->name = "bus:txglomalign";
Franky Lincb7f7962013-08-10 12:27:27 +02003999 dlst->param = (char *)&txglomalign;
Franky Linc3d2bc32012-06-26 21:26:36 +02004000 dlst->param_len = sizeof(u32);
4001 }
Franky Linbbfd6a62012-06-26 21:26:35 +02004002 list_add(&dlst->list, &bus->sdiodev->bus_if->dcmd_list);
4003 }
4004
Arend van Spriel5b435de2011-10-05 13:19:03 +02004005 /* if firmware path present try to download and bring up bus */
Franky Lined683c92011-12-16 18:36:55 -08004006 ret = brcmf_bus_start(bus->sdiodev->dev);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004007 if (ret != 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01004008 brcmf_err("dongle is not responding\n");
Hante Meuleman1799ddf2012-11-14 18:46:17 -08004009 goto fail;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004010 }
Franky Lin15d45b62011-10-21 16:16:32 +02004011
Arend van Spriel5b435de2011-10-05 13:19:03 +02004012 return bus;
4013
4014fail:
4015 brcmf_sdbrcm_release(bus);
4016 return NULL;
4017}
4018
4019void brcmf_sdbrcm_disconnect(void *ptr)
4020{
Franky Line92eedf2011-11-22 17:21:50 -08004021 struct brcmf_sdio *bus = (struct brcmf_sdio *)ptr;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004022
4023 brcmf_dbg(TRACE, "Enter\n");
4024
4025 if (bus)
4026 brcmf_sdbrcm_release(bus);
4027
4028 brcmf_dbg(TRACE, "Disconnected\n");
4029}
4030
Arend van Spriel5b435de2011-10-05 13:19:03 +02004031void
Franky Line92eedf2011-11-22 17:21:50 -08004032brcmf_sdbrcm_wd_timer(struct brcmf_sdio *bus, uint wdtick)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004033{
Arend van Spriel5b435de2011-10-05 13:19:03 +02004034 /* Totally stop the timer */
Joe Perches23677ce2012-02-09 11:17:23 +00004035 if (!wdtick && bus->wd_timer_valid) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02004036 del_timer_sync(&bus->timer);
4037 bus->wd_timer_valid = false;
4038 bus->save_ms = wdtick;
4039 return;
4040 }
4041
Franky Linece960e2011-10-21 16:16:19 +02004042 /* don't start the wd until fw is loaded */
Franky Lin712ac5b2011-12-16 18:37:09 -08004043 if (bus->sdiodev->bus_if->state == BRCMF_BUS_DOWN)
Franky Linece960e2011-10-21 16:16:19 +02004044 return;
4045
Arend van Spriel5b435de2011-10-05 13:19:03 +02004046 if (wdtick) {
4047 if (bus->save_ms != BRCMF_WD_POLL_MS) {
Joe Perches23677ce2012-02-09 11:17:23 +00004048 if (bus->wd_timer_valid)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004049 /* Stop timer and restart at new value */
4050 del_timer_sync(&bus->timer);
4051
4052 /* Create timer again when watchdog period is
4053 dynamically changed or in the first instance
4054 */
4055 bus->timer.expires =
4056 jiffies + BRCMF_WD_POLL_MS * HZ / 1000;
4057 add_timer(&bus->timer);
4058
4059 } else {
4060 /* Re arm the timer, at last watchdog period */
4061 mod_timer(&bus->timer,
4062 jiffies + BRCMF_WD_POLL_MS * HZ / 1000);
4063 }
4064
4065 bus->wd_timer_valid = true;
4066 bus->save_ms = wdtick;
4067 }
4068}