blob: fb4ff910c8a442d3ef976e180f2f76dbf4371868 [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>
Arend van Spriel5b435de2011-10-05 13:19:03 +020034#include <asm/unaligned.h>
35#include <defs.h>
36#include <brcmu_wifi.h>
37#include <brcmu_utils.h>
38#include <brcm_hw_ids.h>
39#include <soc.h>
40#include "sdio_host.h"
Franky Lina83369b2011-11-04 22:23:28 +010041#include "sdio_chip.h"
Arend van Spriel5b435de2011-10-05 13:19:03 +020042
43#define DCMD_RESP_TIMEOUT 2000 /* In milli second */
44
Joe Perches8ae74652012-01-15 00:38:38 -080045#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +020046
47#define BRCMF_TRAP_INFO_SIZE 80
48
49#define CBUF_LEN (128)
50
Arend van Spriel4fc0d012012-06-14 12:16:57 +020051/* Device console log buffer state */
52#define CONSOLE_BUFFER_MAX 2024
53
Arend van Spriel5b435de2011-10-05 13:19:03 +020054struct rte_log_le {
55 __le32 buf; /* Can't be pointer on (64-bit) hosts */
56 __le32 buf_size;
57 __le32 idx;
58 char *_buf_compat; /* Redundant pointer for backward compat. */
59};
60
61struct rte_console {
62 /* Virtual UART
63 * When there is no UART (e.g. Quickturn),
64 * the host should write a complete
65 * input line directly into cbuf and then write
66 * the length into vcons_in.
67 * This may also be used when there is a real UART
68 * (at risk of conflicting with
69 * the real UART). vcons_out is currently unused.
70 */
71 uint vcons_in;
72 uint vcons_out;
73
74 /* Output (logging) buffer
75 * Console output is written to a ring buffer log_buf at index log_idx.
76 * The host may read the output when it sees log_idx advance.
77 * Output will be lost if the output wraps around faster than the host
78 * polls.
79 */
80 struct rte_log_le log_le;
81
82 /* Console input line buffer
83 * Characters are read one at a time into cbuf
84 * until <CR> is received, then
85 * the buffer is processed as a command line.
86 * Also used for virtual UART.
87 */
88 uint cbuf_idx;
89 char cbuf[CBUF_LEN];
90};
91
Joe Perches8ae74652012-01-15 00:38:38 -080092#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +020093#include <chipcommon.h>
94
Arend van Spriel5b435de2011-10-05 13:19:03 +020095#include "dhd_bus.h"
Arend van Spriel5b435de2011-10-05 13:19:03 +020096#include "dhd_dbg.h"
Arend van Spriel40c1c242013-04-05 10:57:44 +020097#include "tracepoint.h"
Arend van Spriel5b435de2011-10-05 13:19:03 +020098
99#define TXQLEN 2048 /* bulk tx queue length */
100#define TXHI (TXQLEN - 256) /* turn on flow control above TXHI */
101#define TXLOW (TXHI - 256) /* turn off flow control below TXLOW */
102#define PRIOMASK 7
103
104#define TXRETRIES 2 /* # of retries for tx frames */
105
106#define BRCMF_RXBOUND 50 /* Default for max rx frames in
107 one scheduling */
108
109#define BRCMF_TXBOUND 20 /* Default for max tx frames in
110 one scheduling */
111
112#define BRCMF_TXMINMAX 1 /* Max tx frames if rx still pending */
113
114#define MEMBLOCK 2048 /* Block size used for downloading
115 of dongle image */
116#define MAX_DATA_BUF (32 * 1024) /* Must be large enough to hold
117 biggest possible glom */
118
119#define BRCMF_FIRSTREAD (1 << 6)
120
121
122/* SBSDIO_DEVICE_CTL */
123
124/* 1: device will assert busy signal when receiving CMD53 */
125#define SBSDIO_DEVCTL_SETBUSY 0x01
126/* 1: assertion of sdio interrupt is synchronous to the sdio clock */
127#define SBSDIO_DEVCTL_SPI_INTR_SYNC 0x02
128/* 1: mask all interrupts to host except the chipActive (rev 8) */
129#define SBSDIO_DEVCTL_CA_INT_ONLY 0x04
130/* 1: isolate internal sdio signals, put external pads in tri-state; requires
131 * sdio bus power cycle to clear (rev 9) */
132#define SBSDIO_DEVCTL_PADS_ISO 0x08
133/* Force SD->SB reset mapping (rev 11) */
134#define SBSDIO_DEVCTL_SB_RST_CTL 0x30
135/* Determined by CoreControl bit */
136#define SBSDIO_DEVCTL_RST_CORECTL 0x00
137/* Force backplane reset */
138#define SBSDIO_DEVCTL_RST_BPRESET 0x10
139/* Force no backplane reset */
140#define SBSDIO_DEVCTL_RST_NOBPRESET 0x20
141
Arend van Spriel5b435de2011-10-05 13:19:03 +0200142/* direct(mapped) cis space */
143
144/* MAPPED common CIS address */
145#define SBSDIO_CIS_BASE_COMMON 0x1000
146/* maximum bytes in one CIS */
147#define SBSDIO_CIS_SIZE_LIMIT 0x200
148/* cis offset addr is < 17 bits */
149#define SBSDIO_CIS_OFT_ADDR_MASK 0x1FFFF
150
151/* manfid tuple length, include tuple, link bytes */
152#define SBSDIO_CIS_MANFID_TUPLE_LEN 6
153
154/* intstatus */
155#define I_SMB_SW0 (1 << 0) /* To SB Mail S/W interrupt 0 */
156#define I_SMB_SW1 (1 << 1) /* To SB Mail S/W interrupt 1 */
157#define I_SMB_SW2 (1 << 2) /* To SB Mail S/W interrupt 2 */
158#define I_SMB_SW3 (1 << 3) /* To SB Mail S/W interrupt 3 */
159#define I_SMB_SW_MASK 0x0000000f /* To SB Mail S/W interrupts mask */
160#define I_SMB_SW_SHIFT 0 /* To SB Mail S/W interrupts shift */
161#define I_HMB_SW0 (1 << 4) /* To Host Mail S/W interrupt 0 */
162#define I_HMB_SW1 (1 << 5) /* To Host Mail S/W interrupt 1 */
163#define I_HMB_SW2 (1 << 6) /* To Host Mail S/W interrupt 2 */
164#define I_HMB_SW3 (1 << 7) /* To Host Mail S/W interrupt 3 */
165#define I_HMB_SW_MASK 0x000000f0 /* To Host Mail S/W interrupts mask */
166#define I_HMB_SW_SHIFT 4 /* To Host Mail S/W interrupts shift */
167#define I_WR_OOSYNC (1 << 8) /* Write Frame Out Of Sync */
168#define I_RD_OOSYNC (1 << 9) /* Read Frame Out Of Sync */
169#define I_PC (1 << 10) /* descriptor error */
170#define I_PD (1 << 11) /* data error */
171#define I_DE (1 << 12) /* Descriptor protocol Error */
172#define I_RU (1 << 13) /* Receive descriptor Underflow */
173#define I_RO (1 << 14) /* Receive fifo Overflow */
174#define I_XU (1 << 15) /* Transmit fifo Underflow */
175#define I_RI (1 << 16) /* Receive Interrupt */
176#define I_BUSPWR (1 << 17) /* SDIO Bus Power Change (rev 9) */
177#define I_XMTDATA_AVAIL (1 << 23) /* bits in fifo */
178#define I_XI (1 << 24) /* Transmit Interrupt */
179#define I_RF_TERM (1 << 25) /* Read Frame Terminate */
180#define I_WF_TERM (1 << 26) /* Write Frame Terminate */
181#define I_PCMCIA_XU (1 << 27) /* PCMCIA Transmit FIFO Underflow */
182#define I_SBINT (1 << 28) /* sbintstatus Interrupt */
183#define I_CHIPACTIVE (1 << 29) /* chip from doze to active state */
184#define I_SRESET (1 << 30) /* CCCR RES interrupt */
185#define I_IOE2 (1U << 31) /* CCCR IOE2 Bit Changed */
186#define I_ERRORS (I_PC | I_PD | I_DE | I_RU | I_RO | I_XU)
187#define I_DMA (I_RI | I_XI | I_ERRORS)
188
189/* corecontrol */
190#define CC_CISRDY (1 << 0) /* CIS Ready */
191#define CC_BPRESEN (1 << 1) /* CCCR RES signal */
192#define CC_F2RDY (1 << 2) /* set CCCR IOR2 bit */
193#define CC_CLRPADSISO (1 << 3) /* clear SDIO pads isolation */
194#define CC_XMTDATAAVAIL_MODE (1 << 4)
195#define CC_XMTDATAAVAIL_CTRL (1 << 5)
196
197/* SDA_FRAMECTRL */
198#define SFC_RF_TERM (1 << 0) /* Read Frame Terminate */
199#define SFC_WF_TERM (1 << 1) /* Write Frame Terminate */
200#define SFC_CRC4WOOS (1 << 2) /* CRC error for write out of sync */
201#define SFC_ABORTALL (1 << 3) /* Abort all in-progress frames */
202
203/* HW frame tag */
204#define SDPCM_FRAMETAG_LEN 4 /* 2 bytes len, 2 bytes check val */
205
206/* Total length of frame header for dongle protocol */
207#define SDPCM_HDRLEN (SDPCM_FRAMETAG_LEN + SDPCM_SWHEADER_LEN)
208#define SDPCM_RESERVE (SDPCM_HDRLEN + BRCMF_SDALIGN)
209
210/*
211 * Software allocation of To SB Mailbox resources
212 */
213
214/* tosbmailbox bits corresponding to intstatus bits */
215#define SMB_NAK (1 << 0) /* Frame NAK */
216#define SMB_INT_ACK (1 << 1) /* Host Interrupt ACK */
217#define SMB_USE_OOB (1 << 2) /* Use OOB Wakeup */
218#define SMB_DEV_INT (1 << 3) /* Miscellaneous Interrupt */
219
220/* tosbmailboxdata */
221#define SMB_DATA_VERSION_SHIFT 16 /* host protocol version */
222
223/*
224 * Software allocation of To Host Mailbox resources
225 */
226
227/* intstatus bits */
228#define I_HMB_FC_STATE I_HMB_SW0 /* Flow Control State */
229#define I_HMB_FC_CHANGE I_HMB_SW1 /* Flow Control State Changed */
230#define I_HMB_FRAME_IND I_HMB_SW2 /* Frame Indication */
231#define I_HMB_HOST_INT I_HMB_SW3 /* Miscellaneous Interrupt */
232
233/* tohostmailboxdata */
234#define HMB_DATA_NAKHANDLED 1 /* retransmit NAK'd frame */
235#define HMB_DATA_DEVREADY 2 /* talk to host after enable */
236#define HMB_DATA_FC 4 /* per prio flowcontrol update flag */
237#define HMB_DATA_FWREADY 8 /* fw ready for protocol activity */
238
239#define HMB_DATA_FCDATA_MASK 0xff000000
240#define HMB_DATA_FCDATA_SHIFT 24
241
242#define HMB_DATA_VERSION_MASK 0x00ff0000
243#define HMB_DATA_VERSION_SHIFT 16
244
245/*
246 * Software-defined protocol header
247 */
248
249/* Current protocol version */
250#define SDPCM_PROT_VERSION 4
251
252/* SW frame header */
253#define SDPCM_PACKET_SEQUENCE(p) (((u8 *)p)[0] & 0xff)
254
255#define SDPCM_CHANNEL_MASK 0x00000f00
256#define SDPCM_CHANNEL_SHIFT 8
257#define SDPCM_PACKET_CHANNEL(p) (((u8 *)p)[1] & 0x0f)
258
259#define SDPCM_NEXTLEN_OFFSET 2
260
261/* Data Offset from SOF (HW Tag, SW Tag, Pad) */
262#define SDPCM_DOFFSET_OFFSET 3 /* Data Offset */
263#define SDPCM_DOFFSET_VALUE(p) (((u8 *)p)[SDPCM_DOFFSET_OFFSET] & 0xff)
264#define SDPCM_DOFFSET_MASK 0xff000000
265#define SDPCM_DOFFSET_SHIFT 24
266#define SDPCM_FCMASK_OFFSET 4 /* Flow control */
267#define SDPCM_FCMASK_VALUE(p) (((u8 *)p)[SDPCM_FCMASK_OFFSET] & 0xff)
268#define SDPCM_WINDOW_OFFSET 5 /* Credit based fc */
269#define SDPCM_WINDOW_VALUE(p) (((u8 *)p)[SDPCM_WINDOW_OFFSET] & 0xff)
270
271#define SDPCM_SWHEADER_LEN 8 /* SW header is 64 bits */
272
273/* logical channel numbers */
274#define SDPCM_CONTROL_CHANNEL 0 /* Control channel Id */
275#define SDPCM_EVENT_CHANNEL 1 /* Asyc Event Indication Channel Id */
276#define SDPCM_DATA_CHANNEL 2 /* Data Xmit/Recv Channel Id */
277#define SDPCM_GLOM_CHANNEL 3 /* For coalesced packets */
278#define SDPCM_TEST_CHANNEL 15 /* Reserved for test/debug packets */
279
280#define SDPCM_SEQUENCE_WRAP 256 /* wrap-around val for 8bit frame seq */
281
282#define SDPCM_GLOMDESC(p) (((u8 *)p)[1] & 0x80)
283
284/*
285 * Shared structure between dongle and the host.
286 * The structure contains pointers to trap or assert information.
287 */
Arend van Spriel4fc0d012012-06-14 12:16:57 +0200288#define SDPCM_SHARED_VERSION 0x0003
Arend van Spriel5b435de2011-10-05 13:19:03 +0200289#define SDPCM_SHARED_VERSION_MASK 0x00FF
290#define SDPCM_SHARED_ASSERT_BUILT 0x0100
291#define SDPCM_SHARED_ASSERT 0x0200
292#define SDPCM_SHARED_TRAP 0x0400
293
294/* Space for header read, limit for data packets */
295#define MAX_HDR_READ (1 << 6)
296#define MAX_RX_DATASZ 2048
297
298/* Maximum milliseconds to wait for F2 to come up */
299#define BRCMF_WAIT_F2RDY 3000
300
301/* Bump up limit on waiting for HT to account for first startup;
302 * if the image is doing a CRC calculation before programming the PMU
303 * for HT availability, it could take a couple hundred ms more, so
304 * max out at a 1 second (1000000us).
305 */
306#undef PMU_MAX_TRANSITION_DLY
307#define PMU_MAX_TRANSITION_DLY 1000000
308
309/* Value for ChipClockCSR during initial setup */
310#define BRCMF_INIT_CLKCTL1 (SBSDIO_FORCE_HW_CLKREQ_OFF | \
311 SBSDIO_ALP_AVAIL_REQ)
312
313/* Flags for SDH calls */
314#define F2SYNC (SDIO_REQ_4BYTE | SDIO_REQ_FIXED)
315
Arend van Spriel52e14092012-02-09 21:09:09 +0100316#define BRCMF_SDIO_FW_NAME "brcm/brcmfmac-sdio.bin"
317#define BRCMF_SDIO_NV_NAME "brcm/brcmfmac-sdio.txt"
318MODULE_FIRMWARE(BRCMF_SDIO_FW_NAME);
319MODULE_FIRMWARE(BRCMF_SDIO_NV_NAME);
Franky Lin8dd939c2011-11-22 17:21:41 -0800320
Franky Lin382a9e02011-12-16 18:37:18 -0800321#define BRCMF_IDLE_IMMEDIATE (-1) /* Enter idle immediately */
322#define BRCMF_IDLE_ACTIVE 0 /* Do not request any SD clock change
323 * when idle
324 */
325#define BRCMF_IDLE_INTERVAL 1
326
Piotr Haber4a3da992013-04-11 13:28:46 +0200327#define KSO_WAIT_US 50
328#define MAX_KSO_ATTEMPTS (PMU_MAX_TRANSITION_DLY/KSO_WAIT_US)
329
Arend van Spriel5b435de2011-10-05 13:19:03 +0200330/*
331 * Conversion of 802.1D priority to precedence level
332 */
333static uint prio2prec(u32 prio)
334{
335 return (prio == PRIO_8021D_NONE || prio == PRIO_8021D_BE) ?
336 (prio^2) : prio;
337}
338
Arend van Spriel5b435de2011-10-05 13:19:03 +0200339/* core registers */
340struct sdpcmd_regs {
341 u32 corecontrol; /* 0x00, rev8 */
342 u32 corestatus; /* rev8 */
343 u32 PAD[1];
344 u32 biststatus; /* rev8 */
345
346 /* PCMCIA access */
347 u16 pcmciamesportaladdr; /* 0x010, rev8 */
348 u16 PAD[1];
349 u16 pcmciamesportalmask; /* rev8 */
350 u16 PAD[1];
351 u16 pcmciawrframebc; /* rev8 */
352 u16 PAD[1];
353 u16 pcmciaunderflowtimer; /* rev8 */
354 u16 PAD[1];
355
356 /* interrupt */
357 u32 intstatus; /* 0x020, rev8 */
358 u32 hostintmask; /* rev8 */
359 u32 intmask; /* rev8 */
360 u32 sbintstatus; /* rev8 */
361 u32 sbintmask; /* rev8 */
362 u32 funcintmask; /* rev4 */
363 u32 PAD[2];
364 u32 tosbmailbox; /* 0x040, rev8 */
365 u32 tohostmailbox; /* rev8 */
366 u32 tosbmailboxdata; /* rev8 */
367 u32 tohostmailboxdata; /* rev8 */
368
369 /* synchronized access to registers in SDIO clock domain */
370 u32 sdioaccess; /* 0x050, rev8 */
371 u32 PAD[3];
372
373 /* PCMCIA frame control */
374 u8 pcmciaframectrl; /* 0x060, rev8 */
375 u8 PAD[3];
376 u8 pcmciawatermark; /* rev8 */
377 u8 PAD[155];
378
379 /* interrupt batching control */
380 u32 intrcvlazy; /* 0x100, rev8 */
381 u32 PAD[3];
382
383 /* counters */
384 u32 cmd52rd; /* 0x110, rev8 */
385 u32 cmd52wr; /* rev8 */
386 u32 cmd53rd; /* rev8 */
387 u32 cmd53wr; /* rev8 */
388 u32 abort; /* rev8 */
389 u32 datacrcerror; /* rev8 */
390 u32 rdoutofsync; /* rev8 */
391 u32 wroutofsync; /* rev8 */
392 u32 writebusy; /* rev8 */
393 u32 readwait; /* rev8 */
394 u32 readterm; /* rev8 */
395 u32 writeterm; /* rev8 */
396 u32 PAD[40];
397 u32 clockctlstatus; /* rev8 */
398 u32 PAD[7];
399
400 u32 PAD[128]; /* DMA engines */
401
402 /* SDIO/PCMCIA CIS region */
403 char cis[512]; /* 0x400-0x5ff, rev6 */
404
405 /* PCMCIA function control registers */
406 char pcmciafcr[256]; /* 0x600-6ff, rev6 */
407 u16 PAD[55];
408
409 /* PCMCIA backplane access */
410 u16 backplanecsr; /* 0x76E, rev6 */
411 u16 backplaneaddr0; /* rev6 */
412 u16 backplaneaddr1; /* rev6 */
413 u16 backplaneaddr2; /* rev6 */
414 u16 backplaneaddr3; /* rev6 */
415 u16 backplanedata0; /* rev6 */
416 u16 backplanedata1; /* rev6 */
417 u16 backplanedata2; /* rev6 */
418 u16 backplanedata3; /* rev6 */
419 u16 PAD[31];
420
421 /* sprom "size" & "blank" info */
422 u16 spromstatus; /* 0x7BE, rev2 */
423 u32 PAD[464];
424
425 u16 PAD[0x80];
426};
427
Joe Perches8ae74652012-01-15 00:38:38 -0800428#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200429/* Device console log buffer state */
430struct brcmf_console {
431 uint count; /* Poll interval msec counter */
432 uint log_addr; /* Log struct address (fixed) */
433 struct rte_log_le log_le; /* Log struct (host copy) */
434 uint bufsize; /* Size of log buffer */
435 u8 *buf; /* Log buffer (host copy) */
436 uint last; /* Last buffer read index */
437};
Arend van Spriel4fc0d012012-06-14 12:16:57 +0200438
439struct brcmf_trap_info {
440 __le32 type;
441 __le32 epc;
442 __le32 cpsr;
443 __le32 spsr;
444 __le32 r0; /* a1 */
445 __le32 r1; /* a2 */
446 __le32 r2; /* a3 */
447 __le32 r3; /* a4 */
448 __le32 r4; /* v1 */
449 __le32 r5; /* v2 */
450 __le32 r6; /* v3 */
451 __le32 r7; /* v4 */
452 __le32 r8; /* v5 */
453 __le32 r9; /* sb/v6 */
454 __le32 r10; /* sl/v7 */
455 __le32 r11; /* fp/v8 */
456 __le32 r12; /* ip */
457 __le32 r13; /* sp */
458 __le32 r14; /* lr */
459 __le32 pc; /* r15 */
460};
Joe Perches8ae74652012-01-15 00:38:38 -0800461#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200462
463struct sdpcm_shared {
464 u32 flags;
465 u32 trap_addr;
466 u32 assert_exp_addr;
467 u32 assert_file_addr;
468 u32 assert_line;
469 u32 console_addr; /* Address of struct rte_console */
470 u32 msgtrace_addr;
471 u8 tag[32];
Arend van Spriel4fc0d012012-06-14 12:16:57 +0200472 u32 brpt_addr;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200473};
474
475struct sdpcm_shared_le {
476 __le32 flags;
477 __le32 trap_addr;
478 __le32 assert_exp_addr;
479 __le32 assert_file_addr;
480 __le32 assert_line;
481 __le32 console_addr; /* Address of struct rte_console */
482 __le32 msgtrace_addr;
483 u8 tag[32];
Arend van Spriel4fc0d012012-06-14 12:16:57 +0200484 __le32 brpt_addr;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200485};
486
Franky Lin4754fce2012-09-19 22:21:11 +0200487/* SDIO read frame info */
488struct brcmf_sdio_read {
489 u8 seq_num;
490 u8 channel;
491 u16 len;
492 u16 len_left;
493 u16 len_nxtfrm;
494 u8 dat_offset;
495};
Arend van Spriel5b435de2011-10-05 13:19:03 +0200496
497/* misc chip info needed by some of the routines */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200498/* Private data for SDIO bus interaction */
Franky Line92eedf2011-11-22 17:21:50 -0800499struct brcmf_sdio {
Arend van Spriel5b435de2011-10-05 13:19:03 +0200500 struct brcmf_sdio_dev *sdiodev; /* sdio device handler */
501 struct chip_info *ci; /* Chip info struct */
502 char *vars; /* Variables (from CIS and/or other) */
503 uint varsz; /* Size of variables buffer */
504
505 u32 ramsize; /* Size of RAM in SOCRAM (bytes) */
506
507 u32 hostintmask; /* Copy of Host Interrupt Mask */
Franky Lin45316032012-09-13 21:12:03 +0200508 atomic_t intstatus; /* Intstatus bits (events) pending */
509 atomic_t fcstate; /* State of dongle flow-control */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200510
511 uint blocksize; /* Block size of SDIO transfers */
512 uint roundup; /* Max roundup limit */
513
514 struct pktq txq; /* Queue length used for flow-control */
515 u8 flowcontrol; /* per prio flow control bitmask */
516 u8 tx_seq; /* Transmit sequence number (next) */
517 u8 tx_max; /* Maximum transmit sequence allowed */
518
519 u8 hdrbuf[MAX_HDR_READ + BRCMF_SDALIGN];
520 u8 *rxhdr; /* Header of current rx frame (in hdrbuf) */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200521 u8 rx_seq; /* Receive sequence number (expected) */
Franky Lin4754fce2012-09-19 22:21:11 +0200522 struct brcmf_sdio_read cur_read;
523 /* info of current read frame */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200524 bool rxskip; /* Skip receive (awaiting NAK ACK) */
Franky Lin4754fce2012-09-19 22:21:11 +0200525 bool rxpending; /* Data frame pending in dongle */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200526
527 uint rxbound; /* Rx frames to read before resched */
528 uint txbound; /* Tx frames to send before resched */
529 uint txminmax;
530
531 struct sk_buff *glomd; /* Packet containing glomming descriptor */
Arend van Sprielb83db862011-10-19 12:51:09 +0200532 struct sk_buff_head glom; /* Packet list for glommed superframe */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200533 uint glomerr; /* Glom packet read errors */
534
535 u8 *rxbuf; /* Buffer for receiving control packets */
536 uint rxblen; /* Allocated length of rxbuf */
537 u8 *rxctl; /* Aligned pointer into rxbuf */
Franky Lindd43a012012-11-05 16:22:22 -0800538 u8 *rxctl_orig; /* pointer for freeing rxctl */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200539 u8 *databuf; /* Buffer for receiving big glom packet */
540 u8 *dataptr; /* Aligned pointer into databuf */
541 uint rxlen; /* Length of valid data in buffer */
Franky Lindd43a012012-11-05 16:22:22 -0800542 spinlock_t rxctl_lock; /* protection lock for ctrl frame resources */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200543
544 u8 sdpcm_ver; /* Bus protocol reported by dongle */
545
546 bool intr; /* Use interrupts */
547 bool poll; /* Use polling */
Franky Lin1d382272012-09-13 21:11:59 +0200548 atomic_t ipend; /* Device interrupt is pending */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200549 uint spurious; /* Count of spurious interrupts */
550 uint pollrate; /* Ticks between device polls */
551 uint polltick; /* Tick counter */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200552
Joe Perches8ae74652012-01-15 00:38:38 -0800553#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200554 uint console_interval;
555 struct brcmf_console console; /* Console output polling support */
556 uint console_addr; /* Console address from shared struct */
Joe Perches8ae74652012-01-15 00:38:38 -0800557#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200558
Arend van Spriel5b435de2011-10-05 13:19:03 +0200559 uint clkstate; /* State of sd and backplane clock(s) */
560 bool activity; /* Activity flag for clock down */
561 s32 idletime; /* Control for activity timeout */
562 s32 idlecount; /* Activity timeout counter */
563 s32 idleclock; /* How to set bus driver when idle */
564 s32 sd_rxchain;
565 bool use_rxchain; /* If brcmf should use PKT chains */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200566 bool rxflow_mode; /* Rx flow control mode */
567 bool rxflow; /* Is rx flow control on */
568 bool alp_only; /* Don't use HT clock (ALP only) */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200569
Arend van Spriel5b435de2011-10-05 13:19:03 +0200570 u8 *ctrl_frame_buf;
571 u32 ctrl_frame_len;
572 bool ctrl_frame_stat;
573
574 spinlock_t txqlock;
575 wait_queue_head_t ctrl_wait;
576 wait_queue_head_t dcmd_resp_wait;
577
578 struct timer_list timer;
579 struct completion watchdog_wait;
580 struct task_struct *watchdog_tsk;
581 bool wd_timer_valid;
582 uint save_ms;
583
Franky Linf1e68c22012-09-13 21:12:00 +0200584 struct workqueue_struct *brcmf_wq;
585 struct work_struct datawork;
Franky Linb948a852012-04-23 14:24:53 -0700586 struct list_head dpc_tsklst;
587 spinlock_t dpc_tl_lock;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200588
Arend van Spriel5b435de2011-10-05 13:19:03 +0200589 const struct firmware *firmware;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200590 u32 fw_ptr;
Franky Linc8bf3482011-12-16 18:37:08 -0800591
592 bool txoff; /* Transmit flow-controlled */
Arend van Spriel80969832012-06-09 22:51:44 +0200593 struct brcmf_sdio_count sdcnt;
Piotr Haber4a3da992013-04-11 13:28:46 +0200594 bool sr_enabled; /* SaveRestore enabled */
595 bool sleeping; /* SDIO bus sleeping */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200596};
597
Arend van Spriel5b435de2011-10-05 13:19:03 +0200598/* clkstate */
599#define CLK_NONE 0
600#define CLK_SDONLY 1
Piotr Haber4a3da992013-04-11 13:28:46 +0200601#define CLK_PENDING 2
Arend van Spriel5b435de2011-10-05 13:19:03 +0200602#define CLK_AVAIL 3
603
Joe Perches8ae74652012-01-15 00:38:38 -0800604#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200605static int qcount[NUMPRIO];
606static int tx_packets[NUMPRIO];
Joe Perches8ae74652012-01-15 00:38:38 -0800607#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200608
609#define SDIO_DRIVE_STRENGTH 6 /* in milliamps */
610
611#define RETRYCHAN(chan) ((chan) == SDPCM_EVENT_CHANNEL)
612
613/* Retry count for register access failures */
614static const uint retry_limit = 2;
615
616/* Limit on rounding up frames */
617static const uint max_roundup = 512;
618
619#define ALIGNMENT 4
620
Franky Lin9d7d6f92012-10-22 10:36:24 -0700621enum brcmf_sdio_frmtype {
622 BRCMF_SDIO_FT_NORMAL,
623 BRCMF_SDIO_FT_SUPER,
624 BRCMF_SDIO_FT_SUB,
625};
626
Arend van Spriel5b435de2011-10-05 13:19:03 +0200627static void pkt_align(struct sk_buff *p, int len, int align)
628{
629 uint datalign;
630 datalign = (unsigned long)(p->data);
631 datalign = roundup(datalign, (align)) - datalign;
632 if (datalign)
633 skb_pull(p, datalign);
634 __skb_trim(p, len);
635}
636
637/* To check if there's window offered */
Franky Line92eedf2011-11-22 17:21:50 -0800638static bool data_ok(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200639{
640 return (u8)(bus->tx_max - bus->tx_seq) != 0 &&
641 ((u8)(bus->tx_max - bus->tx_seq) & 0x80) == 0;
642}
643
644/*
645 * Reads a register in the SDIO hardware block. This block occupies a series of
646 * adresses on the 32 bit backplane bus.
647 */
Franky Lin58692752012-05-04 18:27:36 -0700648static int
649r_sdreg32(struct brcmf_sdio *bus, u32 *regvar, u32 offset)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200650{
Franky Lin99ba15c2011-11-04 22:23:42 +0100651 u8 idx = brcmf_sdio_chip_getinfidx(bus->ci, BCMA_CORE_SDIO_DEV);
Franky Lin79ae3952012-05-04 18:27:34 -0700652 int ret;
Franky Lin58692752012-05-04 18:27:36 -0700653
654 *regvar = brcmf_sdio_regrl(bus->sdiodev,
655 bus->ci->c_inf[idx].base + offset, &ret);
656
657 return ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200658}
659
Franky Lin58692752012-05-04 18:27:36 -0700660static int
661w_sdreg32(struct brcmf_sdio *bus, u32 regval, u32 reg_offset)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200662{
Franky Lin99ba15c2011-11-04 22:23:42 +0100663 u8 idx = brcmf_sdio_chip_getinfidx(bus->ci, BCMA_CORE_SDIO_DEV);
Franky Line13ce262012-05-04 18:27:35 -0700664 int ret;
Franky Lin58692752012-05-04 18:27:36 -0700665
666 brcmf_sdio_regwl(bus->sdiodev,
667 bus->ci->c_inf[idx].base + reg_offset,
668 regval, &ret);
669
670 return ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200671}
672
Piotr Haber4a3da992013-04-11 13:28:46 +0200673static int
674brcmf_sdbrcm_kso_control(struct brcmf_sdio *bus, bool on)
675{
676 u8 wr_val = 0, rd_val, cmp_val, bmask;
677 int err = 0;
678 int try_cnt = 0;
679
680 brcmf_dbg(TRACE, "Enter\n");
681
682 wr_val = (on << SBSDIO_FUNC1_SLEEPCSR_KSO_SHIFT);
683 /* 1st KSO write goes to AOS wake up core if device is asleep */
684 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
685 wr_val, &err);
686 if (err) {
687 brcmf_err("SDIO_AOS KSO write error: %d\n", err);
688 return err;
689 }
690
691 if (on) {
692 /* device WAKEUP through KSO:
693 * write bit 0 & read back until
694 * both bits 0 (kso bit) & 1 (dev on status) are set
695 */
696 cmp_val = SBSDIO_FUNC1_SLEEPCSR_KSO_MASK |
697 SBSDIO_FUNC1_SLEEPCSR_DEVON_MASK;
698 bmask = cmp_val;
699 usleep_range(2000, 3000);
700 } else {
701 /* Put device to sleep, turn off KSO */
702 cmp_val = 0;
703 /* only check for bit0, bit1(dev on status) may not
704 * get cleared right away
705 */
706 bmask = SBSDIO_FUNC1_SLEEPCSR_KSO_MASK;
707 }
708
709 do {
710 /* reliable KSO bit set/clr:
711 * the sdiod sleep write access is synced to PMU 32khz clk
712 * just one write attempt may fail,
713 * read it back until it matches written value
714 */
715 rd_val = brcmf_sdio_regrb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
716 &err);
717 if (((rd_val & bmask) == cmp_val) && !err)
718 break;
719 brcmf_dbg(SDIO, "KSO wr/rd retry:%d (max: %d) ERR:%x\n",
720 try_cnt, MAX_KSO_ATTEMPTS, err);
721 udelay(KSO_WAIT_US);
722 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
723 wr_val, &err);
724 } while (try_cnt++ < MAX_KSO_ATTEMPTS);
725
726 return err;
727}
728
Arend van Spriel5b435de2011-10-05 13:19:03 +0200729#define PKT_AVAILABLE() (intstatus & I_HMB_FRAME_IND)
730
731#define HOSTINTMASK (I_HMB_SW_MASK | I_CHIPACTIVE)
732
Arend van Spriel5b435de2011-10-05 13:19:03 +0200733/* Turn backplane clock on or off */
Franky Line92eedf2011-11-22 17:21:50 -0800734static int brcmf_sdbrcm_htclk(struct brcmf_sdio *bus, bool on, bool pendok)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200735{
736 int err;
737 u8 clkctl, clkreq, devctl;
738 unsigned long timeout;
739
Arend van Sprielc3203372013-04-03 12:40:44 +0200740 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200741
742 clkctl = 0;
743
Piotr Haber4a3da992013-04-11 13:28:46 +0200744 if (bus->sr_enabled) {
745 bus->clkstate = (on ? CLK_AVAIL : CLK_SDONLY);
746 return 0;
747 }
748
Arend van Spriel5b435de2011-10-05 13:19:03 +0200749 if (on) {
750 /* Request HT Avail */
751 clkreq =
752 bus->alp_only ? SBSDIO_ALP_AVAIL_REQ : SBSDIO_HT_AVAIL_REQ;
753
Franky Lin3bba8292012-05-04 18:27:33 -0700754 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
755 clkreq, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200756 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100757 brcmf_err("HT Avail request error: %d\n", err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200758 return -EBADE;
759 }
760
Arend van Spriel5b435de2011-10-05 13:19:03 +0200761 /* Check current status */
Franky Lin45db3392012-05-04 18:27:32 -0700762 clkctl = brcmf_sdio_regrb(bus->sdiodev,
763 SBSDIO_FUNC1_CHIPCLKCSR, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200764 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100765 brcmf_err("HT Avail read error: %d\n", err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200766 return -EBADE;
767 }
768
769 /* Go to pending and await interrupt if appropriate */
770 if (!SBSDIO_CLKAV(clkctl, bus->alp_only) && pendok) {
771 /* Allow only clock-available interrupt */
Franky Lin45db3392012-05-04 18:27:32 -0700772 devctl = brcmf_sdio_regrb(bus->sdiodev,
773 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200774 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100775 brcmf_err("Devctl error setting CA: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200776 err);
777 return -EBADE;
778 }
779
780 devctl |= SBSDIO_DEVCTL_CA_INT_ONLY;
Franky Lin3bba8292012-05-04 18:27:33 -0700781 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
782 devctl, &err);
Arend van Sprielc3203372013-04-03 12:40:44 +0200783 brcmf_dbg(SDIO, "CLKCTL: set PENDING\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200784 bus->clkstate = CLK_PENDING;
785
786 return 0;
787 } else if (bus->clkstate == CLK_PENDING) {
788 /* Cancel CA-only interrupt filter */
Franky Lin45db3392012-05-04 18:27:32 -0700789 devctl = brcmf_sdio_regrb(bus->sdiodev,
Arend van Spriel5b435de2011-10-05 13:19:03 +0200790 SBSDIO_DEVICE_CTL, &err);
791 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
Franky Lin3bba8292012-05-04 18:27:33 -0700792 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
793 devctl, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200794 }
795
796 /* Otherwise, wait here (polling) for HT Avail */
797 timeout = jiffies +
798 msecs_to_jiffies(PMU_MAX_TRANSITION_DLY/1000);
799 while (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
Franky Lin45db3392012-05-04 18:27:32 -0700800 clkctl = brcmf_sdio_regrb(bus->sdiodev,
801 SBSDIO_FUNC1_CHIPCLKCSR,
802 &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200803 if (time_after(jiffies, timeout))
804 break;
805 else
806 usleep_range(5000, 10000);
807 }
808 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100809 brcmf_err("HT Avail request error: %d\n", err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200810 return -EBADE;
811 }
812 if (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100813 brcmf_err("HT Avail timeout (%d): clkctl 0x%02x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200814 PMU_MAX_TRANSITION_DLY, clkctl);
815 return -EBADE;
816 }
817
818 /* Mark clock available */
819 bus->clkstate = CLK_AVAIL;
Arend van Sprielc3203372013-04-03 12:40:44 +0200820 brcmf_dbg(SDIO, "CLKCTL: turned ON\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200821
Joe Perches8ae74652012-01-15 00:38:38 -0800822#if defined(DEBUG)
Joe Perches23677ce2012-02-09 11:17:23 +0000823 if (!bus->alp_only) {
Arend van Spriel5b435de2011-10-05 13:19:03 +0200824 if (SBSDIO_ALPONLY(clkctl))
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100825 brcmf_err("HT Clock should be on\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200826 }
Joe Perches8ae74652012-01-15 00:38:38 -0800827#endif /* defined (DEBUG) */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200828
829 bus->activity = true;
830 } else {
831 clkreq = 0;
832
833 if (bus->clkstate == CLK_PENDING) {
834 /* Cancel CA-only interrupt filter */
Franky Lin45db3392012-05-04 18:27:32 -0700835 devctl = brcmf_sdio_regrb(bus->sdiodev,
836 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200837 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
Franky Lin3bba8292012-05-04 18:27:33 -0700838 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
839 devctl, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200840 }
841
842 bus->clkstate = CLK_SDONLY;
Franky Lin3bba8292012-05-04 18:27:33 -0700843 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
844 clkreq, &err);
Arend van Sprielc3203372013-04-03 12:40:44 +0200845 brcmf_dbg(SDIO, "CLKCTL: turned OFF\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200846 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100847 brcmf_err("Failed access turning clock off: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200848 err);
849 return -EBADE;
850 }
851 }
852 return 0;
853}
854
855/* Change idle/active SD state */
Franky Line92eedf2011-11-22 17:21:50 -0800856static int brcmf_sdbrcm_sdclk(struct brcmf_sdio *bus, bool on)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200857{
Arend van Sprielc3203372013-04-03 12:40:44 +0200858 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200859
860 if (on)
861 bus->clkstate = CLK_SDONLY;
862 else
863 bus->clkstate = CLK_NONE;
864
865 return 0;
866}
867
868/* Transition SD and backplane clock readiness */
Franky Line92eedf2011-11-22 17:21:50 -0800869static int brcmf_sdbrcm_clkctl(struct brcmf_sdio *bus, uint target, bool pendok)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200870{
Joe Perches8ae74652012-01-15 00:38:38 -0800871#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200872 uint oldstate = bus->clkstate;
Joe Perches8ae74652012-01-15 00:38:38 -0800873#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200874
Arend van Sprielc3203372013-04-03 12:40:44 +0200875 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200876
877 /* Early exit if we're already there */
878 if (bus->clkstate == target) {
879 if (target == CLK_AVAIL) {
880 brcmf_sdbrcm_wd_timer(bus, BRCMF_WD_POLL_MS);
881 bus->activity = true;
882 }
883 return 0;
884 }
885
886 switch (target) {
887 case CLK_AVAIL:
888 /* Make sure SD clock is available */
889 if (bus->clkstate == CLK_NONE)
890 brcmf_sdbrcm_sdclk(bus, true);
891 /* Now request HT Avail on the backplane */
892 brcmf_sdbrcm_htclk(bus, true, pendok);
893 brcmf_sdbrcm_wd_timer(bus, BRCMF_WD_POLL_MS);
894 bus->activity = true;
895 break;
896
897 case CLK_SDONLY:
898 /* Remove HT request, or bring up SD clock */
899 if (bus->clkstate == CLK_NONE)
900 brcmf_sdbrcm_sdclk(bus, true);
901 else if (bus->clkstate == CLK_AVAIL)
902 brcmf_sdbrcm_htclk(bus, false, false);
903 else
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100904 brcmf_err("request for %d -> %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200905 bus->clkstate, target);
906 brcmf_sdbrcm_wd_timer(bus, BRCMF_WD_POLL_MS);
907 break;
908
909 case CLK_NONE:
910 /* Make sure to remove HT request */
911 if (bus->clkstate == CLK_AVAIL)
912 brcmf_sdbrcm_htclk(bus, false, false);
913 /* Now remove the SD clock */
914 brcmf_sdbrcm_sdclk(bus, false);
915 brcmf_sdbrcm_wd_timer(bus, 0);
916 break;
917 }
Joe Perches8ae74652012-01-15 00:38:38 -0800918#ifdef DEBUG
Arend van Sprielc3203372013-04-03 12:40:44 +0200919 brcmf_dbg(SDIO, "%d -> %d\n", oldstate, bus->clkstate);
Joe Perches8ae74652012-01-15 00:38:38 -0800920#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200921
922 return 0;
923}
924
Piotr Haber4a3da992013-04-11 13:28:46 +0200925static int
926brcmf_sdbrcm_bus_sleep(struct brcmf_sdio *bus, bool sleep, bool pendok)
927{
928 int err = 0;
929 brcmf_dbg(TRACE, "Enter\n");
930 brcmf_dbg(SDIO, "request %s currently %s\n",
931 (sleep ? "SLEEP" : "WAKE"),
932 (bus->sleeping ? "SLEEP" : "WAKE"));
933
934 /* If SR is enabled control bus state with KSO */
935 if (bus->sr_enabled) {
936 /* Done if we're already in the requested state */
937 if (sleep == bus->sleeping)
938 goto end;
939
940 /* Going to sleep */
941 if (sleep) {
942 /* Don't sleep if something is pending */
943 if (atomic_read(&bus->intstatus) ||
944 atomic_read(&bus->ipend) > 0 ||
945 (!atomic_read(&bus->fcstate) &&
946 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) &&
947 data_ok(bus)))
948 return -EBUSY;
949 err = brcmf_sdbrcm_kso_control(bus, false);
950 /* disable watchdog */
951 if (!err)
952 brcmf_sdbrcm_wd_timer(bus, 0);
953 } else {
954 bus->idlecount = 0;
955 err = brcmf_sdbrcm_kso_control(bus, true);
956 }
957 if (!err) {
958 /* Change state */
959 bus->sleeping = sleep;
960 brcmf_dbg(SDIO, "new state %s\n",
961 (sleep ? "SLEEP" : "WAKE"));
962 } else {
963 brcmf_err("error while changing bus sleep state %d\n",
964 err);
965 return err;
966 }
967 }
968
969end:
970 /* control clocks */
971 if (sleep) {
972 if (!bus->sr_enabled)
973 brcmf_sdbrcm_clkctl(bus, CLK_NONE, pendok);
974 } else {
975 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, pendok);
976 }
977
978 return err;
979
980}
981
Franky Line92eedf2011-11-22 17:21:50 -0800982static u32 brcmf_sdbrcm_hostmail(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200983{
984 u32 intstatus = 0;
985 u32 hmb_data;
986 u8 fcbits;
Franky Lin58692752012-05-04 18:27:36 -0700987 int ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200988
Arend van Sprielc3203372013-04-03 12:40:44 +0200989 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200990
991 /* Read mailbox data and ack that we did so */
Franky Lin58692752012-05-04 18:27:36 -0700992 ret = r_sdreg32(bus, &hmb_data,
993 offsetof(struct sdpcmd_regs, tohostmailboxdata));
Arend van Spriel5b435de2011-10-05 13:19:03 +0200994
Franky Lin58692752012-05-04 18:27:36 -0700995 if (ret == 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200996 w_sdreg32(bus, SMB_INT_ACK,
Franky Lin58692752012-05-04 18:27:36 -0700997 offsetof(struct sdpcmd_regs, tosbmailbox));
Arend van Spriel80969832012-06-09 22:51:44 +0200998 bus->sdcnt.f1regdata += 2;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200999
1000 /* Dongle recomposed rx frames, accept them again */
1001 if (hmb_data & HMB_DATA_NAKHANDLED) {
Arend van Sprielc3203372013-04-03 12:40:44 +02001002 brcmf_dbg(SDIO, "Dongle reports NAK handled, expect rtx of %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001003 bus->rx_seq);
1004 if (!bus->rxskip)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001005 brcmf_err("unexpected NAKHANDLED!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001006
1007 bus->rxskip = false;
1008 intstatus |= I_HMB_FRAME_IND;
1009 }
1010
1011 /*
1012 * DEVREADY does not occur with gSPI.
1013 */
1014 if (hmb_data & (HMB_DATA_DEVREADY | HMB_DATA_FWREADY)) {
1015 bus->sdpcm_ver =
1016 (hmb_data & HMB_DATA_VERSION_MASK) >>
1017 HMB_DATA_VERSION_SHIFT;
1018 if (bus->sdpcm_ver != SDPCM_PROT_VERSION)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001019 brcmf_err("Version mismatch, dongle reports %d, "
Arend van Spriel5b435de2011-10-05 13:19:03 +02001020 "expecting %d\n",
1021 bus->sdpcm_ver, SDPCM_PROT_VERSION);
1022 else
Arend van Sprielc3203372013-04-03 12:40:44 +02001023 brcmf_dbg(SDIO, "Dongle ready, protocol version %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001024 bus->sdpcm_ver);
1025 }
1026
1027 /*
1028 * Flow Control has been moved into the RX headers and this out of band
1029 * method isn't used any more.
1030 * remaining backward compatible with older dongles.
1031 */
1032 if (hmb_data & HMB_DATA_FC) {
1033 fcbits = (hmb_data & HMB_DATA_FCDATA_MASK) >>
1034 HMB_DATA_FCDATA_SHIFT;
1035
1036 if (fcbits & ~bus->flowcontrol)
Arend van Spriel80969832012-06-09 22:51:44 +02001037 bus->sdcnt.fc_xoff++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001038
1039 if (bus->flowcontrol & ~fcbits)
Arend van Spriel80969832012-06-09 22:51:44 +02001040 bus->sdcnt.fc_xon++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001041
Arend van Spriel80969832012-06-09 22:51:44 +02001042 bus->sdcnt.fc_rcvd++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001043 bus->flowcontrol = fcbits;
1044 }
1045
1046 /* Shouldn't be any others */
1047 if (hmb_data & ~(HMB_DATA_DEVREADY |
1048 HMB_DATA_NAKHANDLED |
1049 HMB_DATA_FC |
1050 HMB_DATA_FWREADY |
1051 HMB_DATA_FCDATA_MASK | HMB_DATA_VERSION_MASK))
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001052 brcmf_err("Unknown mailbox data content: 0x%02x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001053 hmb_data);
1054
1055 return intstatus;
1056}
1057
Franky Line92eedf2011-11-22 17:21:50 -08001058static void brcmf_sdbrcm_rxfail(struct brcmf_sdio *bus, bool abort, bool rtx)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001059{
1060 uint retries = 0;
1061 u16 lastrbc;
1062 u8 hi, lo;
1063 int err;
1064
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001065 brcmf_err("%sterminate frame%s\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001066 abort ? "abort command, " : "",
1067 rtx ? ", send NAK" : "");
1068
1069 if (abort)
1070 brcmf_sdcard_abort(bus->sdiodev, SDIO_FUNC_2);
1071
Franky Lin3bba8292012-05-04 18:27:33 -07001072 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL,
1073 SFC_RF_TERM, &err);
Arend van Spriel80969832012-06-09 22:51:44 +02001074 bus->sdcnt.f1regdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001075
1076 /* Wait until the packet has been flushed (device/FIFO stable) */
1077 for (lastrbc = retries = 0xffff; retries > 0; retries--) {
Franky Lin45db3392012-05-04 18:27:32 -07001078 hi = brcmf_sdio_regrb(bus->sdiodev,
Franky Lin5c15c232012-05-04 18:27:37 -07001079 SBSDIO_FUNC1_RFRAMEBCHI, &err);
Franky Lin45db3392012-05-04 18:27:32 -07001080 lo = brcmf_sdio_regrb(bus->sdiodev,
Franky Lin5c15c232012-05-04 18:27:37 -07001081 SBSDIO_FUNC1_RFRAMEBCLO, &err);
Arend van Spriel80969832012-06-09 22:51:44 +02001082 bus->sdcnt.f1regdata += 2;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001083
1084 if ((hi == 0) && (lo == 0))
1085 break;
1086
1087 if ((hi > (lastrbc >> 8)) && (lo > (lastrbc & 0x00ff))) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001088 brcmf_err("count growing: last 0x%04x now 0x%04x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001089 lastrbc, (hi << 8) + lo);
1090 }
1091 lastrbc = (hi << 8) + lo;
1092 }
1093
1094 if (!retries)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001095 brcmf_err("count never zeroed: last 0x%04x\n", lastrbc);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001096 else
Arend van Sprielc3203372013-04-03 12:40:44 +02001097 brcmf_dbg(SDIO, "flush took %d iterations\n", 0xffff - retries);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001098
1099 if (rtx) {
Arend van Spriel80969832012-06-09 22:51:44 +02001100 bus->sdcnt.rxrtx++;
Franky Lin58692752012-05-04 18:27:36 -07001101 err = w_sdreg32(bus, SMB_NAK,
1102 offsetof(struct sdpcmd_regs, tosbmailbox));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001103
Arend van Spriel80969832012-06-09 22:51:44 +02001104 bus->sdcnt.f1regdata++;
Franky Lin58692752012-05-04 18:27:36 -07001105 if (err == 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001106 bus->rxskip = true;
1107 }
1108
1109 /* Clear partial in any case */
Franky Lin4754fce2012-09-19 22:21:11 +02001110 bus->cur_read.len = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001111
1112 /* If we can't reach the device, signal failure */
Franky Lin5c15c232012-05-04 18:27:37 -07001113 if (err)
Franky Lin712ac5b2011-12-16 18:37:09 -08001114 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001115}
1116
Arend van Spriel20e5ca12011-10-18 14:03:09 +02001117/* copy a buffer into a pkt buffer chain */
Franky Line92eedf2011-11-22 17:21:50 -08001118static uint brcmf_sdbrcm_glom_from_buf(struct brcmf_sdio *bus, uint len)
Arend van Spriel20e5ca12011-10-18 14:03:09 +02001119{
1120 uint n, ret = 0;
1121 struct sk_buff *p;
1122 u8 *buf;
1123
Arend van Spriel20e5ca12011-10-18 14:03:09 +02001124 buf = bus->dataptr;
1125
1126 /* copy the data */
Arend van Sprielb83db862011-10-19 12:51:09 +02001127 skb_queue_walk(&bus->glom, p) {
Arend van Spriel20e5ca12011-10-18 14:03:09 +02001128 n = min_t(uint, p->len, len);
1129 memcpy(p->data, buf, n);
1130 buf += n;
1131 len -= n;
1132 ret += n;
Arend van Sprielb83db862011-10-19 12:51:09 +02001133 if (!len)
1134 break;
Arend van Spriel20e5ca12011-10-18 14:03:09 +02001135 }
1136
1137 return ret;
1138}
1139
Arend van Spriel9a95e602011-11-10 20:30:29 +01001140/* return total length of buffer chain */
Franky Line92eedf2011-11-22 17:21:50 -08001141static uint brcmf_sdbrcm_glom_len(struct brcmf_sdio *bus)
Arend van Spriel9a95e602011-11-10 20:30:29 +01001142{
1143 struct sk_buff *p;
1144 uint total;
1145
1146 total = 0;
1147 skb_queue_walk(&bus->glom, p)
1148 total += p->len;
1149 return total;
1150}
1151
Franky Line92eedf2011-11-22 17:21:50 -08001152static void brcmf_sdbrcm_free_glom(struct brcmf_sdio *bus)
Arend van Spriel046808d2011-11-10 20:30:31 +01001153{
1154 struct sk_buff *cur, *next;
1155
1156 skb_queue_walk_safe(&bus->glom, cur, next) {
1157 skb_unlink(cur, &bus->glom);
1158 brcmu_pkt_buf_free_skb(cur);
1159 }
1160}
1161
Franky Lin10510582012-11-05 16:22:26 -08001162static int brcmf_sdio_hdparser(struct brcmf_sdio *bus, u8 *header,
1163 struct brcmf_sdio_read *rd,
1164 enum brcmf_sdio_frmtype type)
Franky Lin4754fce2012-09-19 22:21:11 +02001165{
1166 u16 len, checksum;
1167 u8 rx_seq, fc, tx_seq_max;
1168
1169 /*
1170 * 4 bytes hardware header (frame tag)
1171 * Byte 0~1: Frame length
1172 * Byte 2~3: Checksum, bit-wise inverse of frame length
1173 */
1174 len = get_unaligned_le16(header);
1175 checksum = get_unaligned_le16(header + sizeof(u16));
1176 /* All zero means no more to read */
1177 if (!(len | checksum)) {
1178 bus->rxpending = false;
Franky Lin10510582012-11-05 16:22:26 -08001179 return -ENODATA;
Franky Lin4754fce2012-09-19 22:21:11 +02001180 }
1181 if ((u16)(~(len ^ checksum))) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001182 brcmf_err("HW header checksum error\n");
Franky Lin4754fce2012-09-19 22:21:11 +02001183 bus->sdcnt.rx_badhdr++;
1184 brcmf_sdbrcm_rxfail(bus, false, false);
Franky Lin10510582012-11-05 16:22:26 -08001185 return -EIO;
Franky Lin4754fce2012-09-19 22:21:11 +02001186 }
1187 if (len < SDPCM_HDRLEN) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001188 brcmf_err("HW header length error\n");
Franky Lin10510582012-11-05 16:22:26 -08001189 return -EPROTO;
Franky Lin4754fce2012-09-19 22:21:11 +02001190 }
Franky Lin9d7d6f92012-10-22 10:36:24 -07001191 if (type == BRCMF_SDIO_FT_SUPER &&
1192 (roundup(len, bus->blocksize) != rd->len)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001193 brcmf_err("HW superframe header length error\n");
Franky Lin10510582012-11-05 16:22:26 -08001194 return -EPROTO;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001195 }
1196 if (type == BRCMF_SDIO_FT_SUB && len > rd->len) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001197 brcmf_err("HW subframe header length error\n");
Franky Lin10510582012-11-05 16:22:26 -08001198 return -EPROTO;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001199 }
Franky Lin4754fce2012-09-19 22:21:11 +02001200 rd->len = len;
1201
1202 /*
1203 * 8 bytes hardware header
1204 * Byte 0: Rx sequence number
1205 * Byte 1: 4 MSB Channel number, 4 LSB arbitrary flag
1206 * Byte 2: Length of next data frame
1207 * Byte 3: Data offset
1208 * Byte 4: Flow control bits
1209 * Byte 5: Maximum Sequence number allow for Tx
1210 * Byte 6~7: Reserved
1211 */
Franky Lin9d7d6f92012-10-22 10:36:24 -07001212 if (type == BRCMF_SDIO_FT_SUPER &&
1213 SDPCM_GLOMDESC(&header[SDPCM_FRAMETAG_LEN])) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001214 brcmf_err("Glom descriptor found in superframe head\n");
Franky Lin9d7d6f92012-10-22 10:36:24 -07001215 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001216 return -EINVAL;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001217 }
Franky Lin4754fce2012-09-19 22:21:11 +02001218 rx_seq = SDPCM_PACKET_SEQUENCE(&header[SDPCM_FRAMETAG_LEN]);
1219 rd->channel = SDPCM_PACKET_CHANNEL(&header[SDPCM_FRAMETAG_LEN]);
Franky Lin9d7d6f92012-10-22 10:36:24 -07001220 if (len > MAX_RX_DATASZ && rd->channel != SDPCM_CONTROL_CHANNEL &&
1221 type != BRCMF_SDIO_FT_SUPER) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001222 brcmf_err("HW header length too long\n");
Franky Lin4754fce2012-09-19 22:21:11 +02001223 bus->sdcnt.rx_toolong++;
1224 brcmf_sdbrcm_rxfail(bus, false, false);
1225 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001226 return -EPROTO;
Franky Lin4754fce2012-09-19 22:21:11 +02001227 }
Franky Lin9d7d6f92012-10-22 10:36:24 -07001228 if (type == BRCMF_SDIO_FT_SUPER && rd->channel != SDPCM_GLOM_CHANNEL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001229 brcmf_err("Wrong channel for superframe\n");
Franky Lin9d7d6f92012-10-22 10:36:24 -07001230 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001231 return -EINVAL;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001232 }
1233 if (type == BRCMF_SDIO_FT_SUB && rd->channel != SDPCM_DATA_CHANNEL &&
1234 rd->channel != SDPCM_EVENT_CHANNEL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001235 brcmf_err("Wrong channel for subframe\n");
Franky Lin9d7d6f92012-10-22 10:36:24 -07001236 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001237 return -EINVAL;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001238 }
Franky Lin4754fce2012-09-19 22:21:11 +02001239 rd->dat_offset = SDPCM_DOFFSET_VALUE(&header[SDPCM_FRAMETAG_LEN]);
1240 if (rd->dat_offset < SDPCM_HDRLEN || rd->dat_offset > rd->len) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001241 brcmf_err("seq %d: bad data offset\n", rx_seq);
Franky Lin4754fce2012-09-19 22:21:11 +02001242 bus->sdcnt.rx_badhdr++;
1243 brcmf_sdbrcm_rxfail(bus, false, false);
1244 rd->len = 0;
Franky Lin10510582012-11-05 16:22:26 -08001245 return -ENXIO;
Franky Lin4754fce2012-09-19 22:21:11 +02001246 }
1247 if (rd->seq_num != rx_seq) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001248 brcmf_err("seq %d: sequence number error, expect %d\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001249 rx_seq, rd->seq_num);
1250 bus->sdcnt.rx_badseq++;
1251 rd->seq_num = rx_seq;
1252 }
Franky Lin9d7d6f92012-10-22 10:36:24 -07001253 /* no need to check the reset for subframe */
1254 if (type == BRCMF_SDIO_FT_SUB)
Franky Lin10510582012-11-05 16:22:26 -08001255 return 0;
Franky Lin4754fce2012-09-19 22:21:11 +02001256 rd->len_nxtfrm = header[SDPCM_FRAMETAG_LEN + SDPCM_NEXTLEN_OFFSET];
1257 if (rd->len_nxtfrm << 4 > MAX_RX_DATASZ) {
1258 /* only warm for NON glom packet */
1259 if (rd->channel != SDPCM_GLOM_CHANNEL)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001260 brcmf_err("seq %d: next length error\n", rx_seq);
Franky Lin4754fce2012-09-19 22:21:11 +02001261 rd->len_nxtfrm = 0;
1262 }
1263 fc = SDPCM_FCMASK_VALUE(&header[SDPCM_FRAMETAG_LEN]);
1264 if (bus->flowcontrol != fc) {
1265 if (~bus->flowcontrol & fc)
1266 bus->sdcnt.fc_xoff++;
1267 if (bus->flowcontrol & ~fc)
1268 bus->sdcnt.fc_xon++;
1269 bus->sdcnt.fc_rcvd++;
1270 bus->flowcontrol = fc;
1271 }
1272 tx_seq_max = SDPCM_WINDOW_VALUE(&header[SDPCM_FRAMETAG_LEN]);
1273 if ((u8)(tx_seq_max - bus->tx_seq) > 0x40) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001274 brcmf_err("seq %d: max tx seq number error\n", rx_seq);
Franky Lin4754fce2012-09-19 22:21:11 +02001275 tx_seq_max = bus->tx_seq + 2;
1276 }
1277 bus->tx_max = tx_seq_max;
1278
Franky Lin10510582012-11-05 16:22:26 -08001279 return 0;
Franky Lin4754fce2012-09-19 22:21:11 +02001280}
1281
Franky Line92eedf2011-11-22 17:21:50 -08001282static u8 brcmf_sdbrcm_rxglom(struct brcmf_sdio *bus, u8 rxseq)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001283{
1284 u16 dlen, totlen;
1285 u8 *dptr, num = 0;
1286
Franky Lin9d7d6f92012-10-22 10:36:24 -07001287 u16 sublen;
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001288 struct sk_buff *pfirst, *pnext;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001289
1290 int errcode;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001291 u8 doff, sfdoff;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001292
Arend van Spriel5b435de2011-10-05 13:19:03 +02001293 bool usechain = bus->use_rxchain;
Franky Lin9d7d6f92012-10-22 10:36:24 -07001294
1295 struct brcmf_sdio_read rd_new;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001296
1297 /* If packets, issue read(s) and send up packet chain */
1298 /* Return sequence numbers consumed? */
1299
Arend van Sprielc3203372013-04-03 12:40:44 +02001300 brcmf_dbg(SDIO, "start: glomd %p glom %p\n",
Arend van Sprielb83db862011-10-19 12:51:09 +02001301 bus->glomd, skb_peek(&bus->glom));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001302
1303 /* If there's a descriptor, generate the packet chain */
1304 if (bus->glomd) {
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001305 pfirst = pnext = NULL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001306 dlen = (u16) (bus->glomd->len);
1307 dptr = bus->glomd->data;
1308 if (!dlen || (dlen & 1)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001309 brcmf_err("bad glomd len(%d), ignore descriptor\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001310 dlen);
1311 dlen = 0;
1312 }
1313
1314 for (totlen = num = 0; dlen; num++) {
1315 /* Get (and move past) next length */
1316 sublen = get_unaligned_le16(dptr);
1317 dlen -= sizeof(u16);
1318 dptr += sizeof(u16);
1319 if ((sublen < SDPCM_HDRLEN) ||
1320 ((num == 0) && (sublen < (2 * SDPCM_HDRLEN)))) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001321 brcmf_err("descriptor len %d bad: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001322 num, sublen);
1323 pnext = NULL;
1324 break;
1325 }
1326 if (sublen % BRCMF_SDALIGN) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001327 brcmf_err("sublen %d not multiple of %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001328 sublen, BRCMF_SDALIGN);
1329 usechain = false;
1330 }
1331 totlen += sublen;
1332
1333 /* For last frame, adjust read len so total
1334 is a block multiple */
1335 if (!dlen) {
1336 sublen +=
1337 (roundup(totlen, bus->blocksize) - totlen);
1338 totlen = roundup(totlen, bus->blocksize);
1339 }
1340
1341 /* Allocate/chain packet for next subframe */
1342 pnext = brcmu_pkt_buf_get_skb(sublen + BRCMF_SDALIGN);
1343 if (pnext == NULL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001344 brcmf_err("bcm_pkt_buf_get_skb failed, num %d len %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001345 num, sublen);
1346 break;
1347 }
Arend van Sprielb83db862011-10-19 12:51:09 +02001348 skb_queue_tail(&bus->glom, pnext);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001349
1350 /* Adhere to start alignment requirements */
1351 pkt_align(pnext, sublen, BRCMF_SDALIGN);
1352 }
1353
1354 /* If all allocations succeeded, save packet chain
1355 in bus structure */
1356 if (pnext) {
1357 brcmf_dbg(GLOM, "allocated %d-byte packet chain for %d subframes\n",
1358 totlen, num);
Franky Lin4754fce2012-09-19 22:21:11 +02001359 if (BRCMF_GLOM_ON() && bus->cur_read.len &&
1360 totlen != bus->cur_read.len) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001361 brcmf_dbg(GLOM, "glomdesc mismatch: nextlen %d glomdesc %d rxseq %d\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001362 bus->cur_read.len, totlen, rxseq);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001363 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02001364 pfirst = pnext = NULL;
1365 } else {
Arend van Spriel046808d2011-11-10 20:30:31 +01001366 brcmf_sdbrcm_free_glom(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001367 num = 0;
1368 }
1369
1370 /* Done with descriptor packet */
1371 brcmu_pkt_buf_free_skb(bus->glomd);
1372 bus->glomd = NULL;
Franky Lin4754fce2012-09-19 22:21:11 +02001373 bus->cur_read.len = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001374 }
1375
1376 /* Ok -- either we just generated a packet chain,
1377 or had one from before */
Arend van Sprielb83db862011-10-19 12:51:09 +02001378 if (!skb_queue_empty(&bus->glom)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001379 if (BRCMF_GLOM_ON()) {
1380 brcmf_dbg(GLOM, "try superframe read, packet chain:\n");
Arend van Sprielb83db862011-10-19 12:51:09 +02001381 skb_queue_walk(&bus->glom, pnext) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001382 brcmf_dbg(GLOM, " %p: %p len 0x%04x (%d)\n",
1383 pnext, (u8 *) (pnext->data),
1384 pnext->len, pnext->len);
1385 }
1386 }
1387
Arend van Sprielb83db862011-10-19 12:51:09 +02001388 pfirst = skb_peek(&bus->glom);
Arend van Spriel9a95e602011-11-10 20:30:29 +01001389 dlen = (u16) brcmf_sdbrcm_glom_len(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001390
1391 /* Do an SDIO read for the superframe. Configurable iovar to
1392 * read directly into the chained packet, or allocate a large
1393 * packet and and copy into the chain.
1394 */
Franky Lin38b0b0d2012-11-05 16:22:24 -08001395 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001396 if (usechain) {
Arend van Spriel5adfeb62011-11-22 17:21:38 -08001397 errcode = brcmf_sdcard_recv_chain(bus->sdiodev,
Arend van Spriel5b435de2011-10-05 13:19:03 +02001398 bus->sdiodev->sbwad,
Arend van Spriel5adfeb62011-11-22 17:21:38 -08001399 SDIO_FUNC_2, F2SYNC, &bus->glom);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001400 } else if (bus->dataptr) {
1401 errcode = brcmf_sdcard_recv_buf(bus->sdiodev,
1402 bus->sdiodev->sbwad,
Arend van Spriel5adfeb62011-11-22 17:21:38 -08001403 SDIO_FUNC_2, F2SYNC,
1404 bus->dataptr, dlen);
Arend van Spriel20e5ca12011-10-18 14:03:09 +02001405 sublen = (u16) brcmf_sdbrcm_glom_from_buf(bus, dlen);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001406 if (sublen != dlen) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001407 brcmf_err("FAILED TO COPY, dlen %d sublen %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001408 dlen, sublen);
1409 errcode = -1;
1410 }
1411 pnext = NULL;
1412 } else {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001413 brcmf_err("COULDN'T ALLOC %d-BYTE GLOM, FORCE FAILURE\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001414 dlen);
1415 errcode = -1;
1416 }
Franky Lin38b0b0d2012-11-05 16:22:24 -08001417 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel80969832012-06-09 22:51:44 +02001418 bus->sdcnt.f2rxdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001419
1420 /* On failure, kill the superframe, allow a couple retries */
1421 if (errcode < 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001422 brcmf_err("glom read of %d bytes failed: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001423 dlen, errcode);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001424
Franky Lin38b0b0d2012-11-05 16:22:24 -08001425 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001426 if (bus->glomerr++ < 3) {
1427 brcmf_sdbrcm_rxfail(bus, true, true);
1428 } else {
1429 bus->glomerr = 0;
1430 brcmf_sdbrcm_rxfail(bus, true, false);
Arend van Spriel80969832012-06-09 22:51:44 +02001431 bus->sdcnt.rxglomfail++;
Arend van Spriel046808d2011-11-10 20:30:31 +01001432 brcmf_sdbrcm_free_glom(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001433 }
Franky Lin38b0b0d2012-11-05 16:22:24 -08001434 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001435 return 0;
1436 }
Joe Perches1e023822012-01-15 00:38:40 -08001437
1438 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
1439 pfirst->data, min_t(int, pfirst->len, 48),
1440 "SUPERFRAME:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001441
Franky Lin9d7d6f92012-10-22 10:36:24 -07001442 rd_new.seq_num = rxseq;
1443 rd_new.len = dlen;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001444 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin10510582012-11-05 16:22:26 -08001445 errcode = brcmf_sdio_hdparser(bus, pfirst->data, &rd_new,
1446 BRCMF_SDIO_FT_SUPER);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001447 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin9d7d6f92012-10-22 10:36:24 -07001448 bus->cur_read.len = rd_new.len_nxtfrm << 4;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001449
1450 /* Remove superframe header, remember offset */
Franky Lin9d7d6f92012-10-22 10:36:24 -07001451 skb_pull(pfirst, rd_new.dat_offset);
1452 sfdoff = rd_new.dat_offset;
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001453 num = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001454
1455 /* Validate all the subframe headers */
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001456 skb_queue_walk(&bus->glom, pnext) {
1457 /* leave when invalid subframe is found */
1458 if (errcode)
1459 break;
1460
Franky Lin9d7d6f92012-10-22 10:36:24 -07001461 rd_new.len = pnext->len;
1462 rd_new.seq_num = rxseq++;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001463 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin10510582012-11-05 16:22:26 -08001464 errcode = brcmf_sdio_hdparser(bus, pnext->data, &rd_new,
1465 BRCMF_SDIO_FT_SUB);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001466 sdio_release_host(bus->sdiodev->func[1]);
Joe Perches1e023822012-01-15 00:38:40 -08001467 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
Franky Lin9d7d6f92012-10-22 10:36:24 -07001468 pnext->data, 32, "subframe:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001469
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001470 num++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001471 }
1472
1473 if (errcode) {
1474 /* Terminate frame on error, request
1475 a couple retries */
Franky Lin38b0b0d2012-11-05 16:22:24 -08001476 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001477 if (bus->glomerr++ < 3) {
1478 /* Restore superframe header space */
1479 skb_push(pfirst, sfdoff);
1480 brcmf_sdbrcm_rxfail(bus, true, true);
1481 } else {
1482 bus->glomerr = 0;
1483 brcmf_sdbrcm_rxfail(bus, true, false);
Arend van Spriel80969832012-06-09 22:51:44 +02001484 bus->sdcnt.rxglomfail++;
Arend van Spriel046808d2011-11-10 20:30:31 +01001485 brcmf_sdbrcm_free_glom(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001486 }
Franky Lin38b0b0d2012-11-05 16:22:24 -08001487 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001488 bus->cur_read.len = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001489 return 0;
1490 }
1491
1492 /* Basic SD framing looks ok - process each packet (header) */
Arend van Spriel5b435de2011-10-05 13:19:03 +02001493
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001494 skb_queue_walk_safe(&bus->glom, pfirst, pnext) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001495 dptr = (u8 *) (pfirst->data);
1496 sublen = get_unaligned_le16(dptr);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001497 doff = SDPCM_DOFFSET_VALUE(&dptr[SDPCM_FRAMETAG_LEN]);
1498
Joe Perches1e023822012-01-15 00:38:40 -08001499 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_DATA_ON(),
Franky Lin9d7d6f92012-10-22 10:36:24 -07001500 dptr, pfirst->len,
1501 "Rx Subframe Data:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001502
1503 __skb_trim(pfirst, sublen);
1504 skb_pull(pfirst, doff);
1505
1506 if (pfirst->len == 0) {
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001507 skb_unlink(pfirst, &bus->glom);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001508 brcmu_pkt_buf_free_skb(pfirst);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001509 continue;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001510 }
1511
Joe Perches1e023822012-01-15 00:38:40 -08001512 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
1513 pfirst->data,
1514 min_t(int, pfirst->len, 32),
1515 "subframe %d to stack, %p (%p/%d) nxt/lnk %p/%p\n",
1516 bus->glom.qlen, pfirst, pfirst->data,
1517 pfirst->len, pfirst->next,
1518 pfirst->prev);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001519 }
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001520 /* sent any remaining packets up */
Franky Lin7cdf57d2012-11-05 16:22:23 -08001521 if (bus->glom.qlen)
Arend van Spriela43af512013-01-02 15:22:43 +01001522 brcmf_rx_frames(bus->sdiodev->dev, &bus->glom);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001523
Arend van Spriel80969832012-06-09 22:51:44 +02001524 bus->sdcnt.rxglomframes++;
1525 bus->sdcnt.rxglompkts += bus->glom.qlen;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001526 }
1527 return num;
1528}
1529
Franky Line92eedf2011-11-22 17:21:50 -08001530static int brcmf_sdbrcm_dcmd_resp_wait(struct brcmf_sdio *bus, uint *condition,
Arend van Spriel5b435de2011-10-05 13:19:03 +02001531 bool *pending)
1532{
1533 DECLARE_WAITQUEUE(wait, current);
1534 int timeout = msecs_to_jiffies(DCMD_RESP_TIMEOUT);
1535
1536 /* Wait until control frame is available */
1537 add_wait_queue(&bus->dcmd_resp_wait, &wait);
1538 set_current_state(TASK_INTERRUPTIBLE);
1539
1540 while (!(*condition) && (!signal_pending(current) && timeout))
1541 timeout = schedule_timeout(timeout);
1542
1543 if (signal_pending(current))
1544 *pending = true;
1545
1546 set_current_state(TASK_RUNNING);
1547 remove_wait_queue(&bus->dcmd_resp_wait, &wait);
1548
1549 return timeout;
1550}
1551
Franky Line92eedf2011-11-22 17:21:50 -08001552static int brcmf_sdbrcm_dcmd_resp_wake(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001553{
1554 if (waitqueue_active(&bus->dcmd_resp_wait))
1555 wake_up_interruptible(&bus->dcmd_resp_wait);
1556
1557 return 0;
1558}
1559static void
Franky Line92eedf2011-11-22 17:21:50 -08001560brcmf_sdbrcm_read_control(struct brcmf_sdio *bus, u8 *hdr, uint len, uint doff)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001561{
1562 uint rdlen, pad;
Franky Lindd43a012012-11-05 16:22:22 -08001563 u8 *buf = NULL, *rbuf;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001564 int sdret;
1565
1566 brcmf_dbg(TRACE, "Enter\n");
1567
Franky Lindd43a012012-11-05 16:22:22 -08001568 if (bus->rxblen)
1569 buf = vzalloc(bus->rxblen);
Joe Perches14f8dc42013-02-07 11:46:27 +00001570 if (!buf)
Franky Lindd43a012012-11-05 16:22:22 -08001571 goto done;
Joe Perches14f8dc42013-02-07 11:46:27 +00001572
Franky Lindd43a012012-11-05 16:22:22 -08001573 rbuf = bus->rxbuf;
1574 pad = ((unsigned long)rbuf % BRCMF_SDALIGN);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001575 if (pad)
Franky Lindd43a012012-11-05 16:22:22 -08001576 rbuf += (BRCMF_SDALIGN - pad);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001577
1578 /* Copy the already-read portion over */
Franky Lindd43a012012-11-05 16:22:22 -08001579 memcpy(buf, hdr, BRCMF_FIRSTREAD);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001580 if (len <= BRCMF_FIRSTREAD)
1581 goto gotpkt;
1582
1583 /* Raise rdlen to next SDIO block to avoid tail command */
1584 rdlen = len - BRCMF_FIRSTREAD;
1585 if (bus->roundup && bus->blocksize && (rdlen > bus->blocksize)) {
1586 pad = bus->blocksize - (rdlen % bus->blocksize);
1587 if ((pad <= bus->roundup) && (pad < bus->blocksize) &&
Franky Linb01a6b32011-12-16 18:37:03 -08001588 ((len + pad) < bus->sdiodev->bus_if->maxctl))
Arend van Spriel5b435de2011-10-05 13:19:03 +02001589 rdlen += pad;
1590 } else if (rdlen % BRCMF_SDALIGN) {
1591 rdlen += BRCMF_SDALIGN - (rdlen % BRCMF_SDALIGN);
1592 }
1593
1594 /* Satisfy length-alignment requirements */
1595 if (rdlen & (ALIGNMENT - 1))
1596 rdlen = roundup(rdlen, ALIGNMENT);
1597
1598 /* Drop if the read is too big or it exceeds our maximum */
Franky Linb01a6b32011-12-16 18:37:03 -08001599 if ((rdlen + BRCMF_FIRSTREAD) > bus->sdiodev->bus_if->maxctl) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001600 brcmf_err("%d-byte control read exceeds %d-byte buffer\n",
Franky Linb01a6b32011-12-16 18:37:03 -08001601 rdlen, bus->sdiodev->bus_if->maxctl);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001602 brcmf_sdbrcm_rxfail(bus, false, false);
1603 goto done;
1604 }
1605
Franky Linb01a6b32011-12-16 18:37:03 -08001606 if ((len - doff) > bus->sdiodev->bus_if->maxctl) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001607 brcmf_err("%d-byte ctl frame (%d-byte ctl data) exceeds %d-byte limit\n",
Franky Linb01a6b32011-12-16 18:37:03 -08001608 len, len - doff, bus->sdiodev->bus_if->maxctl);
Arend van Spriel80969832012-06-09 22:51:44 +02001609 bus->sdcnt.rx_toolong++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001610 brcmf_sdbrcm_rxfail(bus, false, false);
1611 goto done;
1612 }
1613
Franky Lindd43a012012-11-05 16:22:22 -08001614 /* Read remain of frame body */
Arend van Spriel5b435de2011-10-05 13:19:03 +02001615 sdret = brcmf_sdcard_recv_buf(bus->sdiodev,
1616 bus->sdiodev->sbwad,
1617 SDIO_FUNC_2,
Franky Lindd43a012012-11-05 16:22:22 -08001618 F2SYNC, rbuf, rdlen);
Arend van Spriel80969832012-06-09 22:51:44 +02001619 bus->sdcnt.f2rxdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001620
1621 /* Control frame failures need retransmission */
1622 if (sdret < 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001623 brcmf_err("read %d control bytes failed: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02001624 rdlen, sdret);
Arend van Spriel80969832012-06-09 22:51:44 +02001625 bus->sdcnt.rxc_errors++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001626 brcmf_sdbrcm_rxfail(bus, true, true);
1627 goto done;
Franky Lindd43a012012-11-05 16:22:22 -08001628 } else
1629 memcpy(buf + BRCMF_FIRSTREAD, rbuf, rdlen);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001630
1631gotpkt:
1632
Joe Perches1e023822012-01-15 00:38:40 -08001633 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_CTL_ON(),
Franky Lindd43a012012-11-05 16:22:22 -08001634 buf, len, "RxCtrl:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001635
1636 /* Point to valid data and indicate its length */
Franky Lindd43a012012-11-05 16:22:22 -08001637 spin_lock_bh(&bus->rxctl_lock);
1638 if (bus->rxctl) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001639 brcmf_err("last control frame is being processed.\n");
Franky Lindd43a012012-11-05 16:22:22 -08001640 spin_unlock_bh(&bus->rxctl_lock);
1641 vfree(buf);
1642 goto done;
1643 }
1644 bus->rxctl = buf + doff;
1645 bus->rxctl_orig = buf;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001646 bus->rxlen = len - doff;
Franky Lindd43a012012-11-05 16:22:22 -08001647 spin_unlock_bh(&bus->rxctl_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001648
1649done:
1650 /* Awake any waiters */
1651 brcmf_sdbrcm_dcmd_resp_wake(bus);
1652}
1653
1654/* Pad read to blocksize for efficiency */
Franky Line92eedf2011-11-22 17:21:50 -08001655static void brcmf_pad(struct brcmf_sdio *bus, u16 *pad, u16 *rdlen)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001656{
1657 if (bus->roundup && bus->blocksize && *rdlen > bus->blocksize) {
1658 *pad = bus->blocksize - (*rdlen % bus->blocksize);
1659 if (*pad <= bus->roundup && *pad < bus->blocksize &&
1660 *rdlen + *pad + BRCMF_FIRSTREAD < MAX_RX_DATASZ)
1661 *rdlen += *pad;
1662 } else if (*rdlen % BRCMF_SDALIGN) {
1663 *rdlen += BRCMF_SDALIGN - (*rdlen % BRCMF_SDALIGN);
1664 }
1665}
1666
Franky Lin4754fce2012-09-19 22:21:11 +02001667static uint brcmf_sdio_readframes(struct brcmf_sdio *bus, uint maxframes)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001668{
Arend van Spriel5b435de2011-10-05 13:19:03 +02001669 struct sk_buff *pkt; /* Packet for event or data frames */
Arend van Spriel3aa7aad2013-01-02 15:22:42 +01001670 struct sk_buff_head pktlist; /* needed for bus interface */
Arend van Spriel5b435de2011-10-05 13:19:03 +02001671 u16 pad; /* Number of pad bytes to read */
Arend van Spriel5b435de2011-10-05 13:19:03 +02001672 uint rxleft = 0; /* Remaining number of frames allowed */
Arend van Spriel349e7102013-03-03 12:45:28 +01001673 int ret; /* Return code from calls */
Arend van Spriel5b435de2011-10-05 13:19:03 +02001674 uint rxcount = 0; /* Total frames read */
Franky Lin4754fce2012-09-19 22:21:11 +02001675 struct brcmf_sdio_read *rd = &bus->cur_read, rd_new;
1676 u8 head_read = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001677
1678 brcmf_dbg(TRACE, "Enter\n");
1679
1680 /* Not finished unless we encounter no more frames indication */
Franky Lin4754fce2012-09-19 22:21:11 +02001681 bus->rxpending = true;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001682
Franky Lin4754fce2012-09-19 22:21:11 +02001683 for (rd->seq_num = bus->rx_seq, rxleft = maxframes;
Franky Lin8d169aa2011-11-22 17:21:52 -08001684 !bus->rxskip && rxleft &&
Franky Lin712ac5b2011-12-16 18:37:09 -08001685 bus->sdiodev->bus_if->state != BRCMF_BUS_DOWN;
Franky Lin4754fce2012-09-19 22:21:11 +02001686 rd->seq_num++, rxleft--) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001687
1688 /* Handle glomming separately */
Arend van Sprielb83db862011-10-19 12:51:09 +02001689 if (bus->glomd || !skb_queue_empty(&bus->glom)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001690 u8 cnt;
1691 brcmf_dbg(GLOM, "calling rxglom: glomd %p, glom %p\n",
Arend van Sprielb83db862011-10-19 12:51:09 +02001692 bus->glomd, skb_peek(&bus->glom));
Franky Lin4754fce2012-09-19 22:21:11 +02001693 cnt = brcmf_sdbrcm_rxglom(bus, rd->seq_num);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001694 brcmf_dbg(GLOM, "rxglom returned %d\n", cnt);
Franky Lin4754fce2012-09-19 22:21:11 +02001695 rd->seq_num += cnt - 1;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001696 rxleft = (rxleft > cnt) ? (rxleft - cnt) : 1;
1697 continue;
1698 }
1699
Franky Lin4754fce2012-09-19 22:21:11 +02001700 rd->len_left = rd->len;
1701 /* read header first for unknow frame length */
Franky Lin38b0b0d2012-11-05 16:22:24 -08001702 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001703 if (!rd->len) {
Arend van Spriel349e7102013-03-03 12:45:28 +01001704 ret = brcmf_sdcard_recv_buf(bus->sdiodev,
Franky Lin4754fce2012-09-19 22:21:11 +02001705 bus->sdiodev->sbwad,
1706 SDIO_FUNC_2, F2SYNC,
1707 bus->rxhdr,
1708 BRCMF_FIRSTREAD);
1709 bus->sdcnt.f2rxhdrs++;
Arend van Spriel349e7102013-03-03 12:45:28 +01001710 if (ret < 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001711 brcmf_err("RXHEADER FAILED: %d\n",
Arend van Spriel349e7102013-03-03 12:45:28 +01001712 ret);
Franky Lin4754fce2012-09-19 22:21:11 +02001713 bus->sdcnt.rx_hdrfail++;
1714 brcmf_sdbrcm_rxfail(bus, true, true);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001715 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001716 continue;
1717 }
1718
Franky Lin4754fce2012-09-19 22:21:11 +02001719 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() || BRCMF_HDRS_ON(),
1720 bus->rxhdr, SDPCM_HDRLEN,
1721 "RxHdr:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001722
Franky Lin10510582012-11-05 16:22:26 -08001723 if (brcmf_sdio_hdparser(bus, bus->rxhdr, rd,
1724 BRCMF_SDIO_FT_NORMAL)) {
Franky Lin38b0b0d2012-11-05 16:22:24 -08001725 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001726 if (!bus->rxpending)
1727 break;
1728 else
1729 continue;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001730 }
1731
Franky Lin4754fce2012-09-19 22:21:11 +02001732 if (rd->channel == SDPCM_CONTROL_CHANNEL) {
1733 brcmf_sdbrcm_read_control(bus, bus->rxhdr,
1734 rd->len,
1735 rd->dat_offset);
1736 /* prepare the descriptor for the next read */
1737 rd->len = rd->len_nxtfrm << 4;
1738 rd->len_nxtfrm = 0;
1739 /* treat all packet as event if we don't know */
1740 rd->channel = SDPCM_EVENT_CHANNEL;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001741 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001742 continue;
1743 }
1744 rd->len_left = rd->len > BRCMF_FIRSTREAD ?
1745 rd->len - BRCMF_FIRSTREAD : 0;
1746 head_read = BRCMF_FIRSTREAD;
1747 }
1748
1749 brcmf_pad(bus, &pad, &rd->len_left);
1750
1751 pkt = brcmu_pkt_buf_get_skb(rd->len_left + head_read +
1752 BRCMF_SDALIGN);
1753 if (!pkt) {
1754 /* Give up on data, request rtx of events */
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001755 brcmf_err("brcmu_pkt_buf_get_skb failed\n");
Franky Lin4754fce2012-09-19 22:21:11 +02001756 brcmf_sdbrcm_rxfail(bus, false,
1757 RETRYCHAN(rd->channel));
Franky Lin38b0b0d2012-11-05 16:22:24 -08001758 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001759 continue;
1760 }
1761 skb_pull(pkt, head_read);
1762 pkt_align(pkt, rd->len_left, BRCMF_SDALIGN);
1763
Arend van Spriel349e7102013-03-03 12:45:28 +01001764 ret = brcmf_sdcard_recv_pkt(bus->sdiodev, bus->sdiodev->sbwad,
Franky Lin4754fce2012-09-19 22:21:11 +02001765 SDIO_FUNC_2, F2SYNC, pkt);
1766 bus->sdcnt.f2rxdata++;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001767 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001768
Arend van Spriel349e7102013-03-03 12:45:28 +01001769 if (ret < 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001770 brcmf_err("read %d bytes from channel %d failed: %d\n",
Arend van Spriel349e7102013-03-03 12:45:28 +01001771 rd->len, rd->channel, ret);
Franky Lin4754fce2012-09-19 22:21:11 +02001772 brcmu_pkt_buf_free_skb(pkt);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001773 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001774 brcmf_sdbrcm_rxfail(bus, true,
1775 RETRYCHAN(rd->channel));
Franky Lin38b0b0d2012-11-05 16:22:24 -08001776 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001777 continue;
1778 }
1779
1780 if (head_read) {
1781 skb_push(pkt, head_read);
1782 memcpy(pkt->data, bus->rxhdr, head_read);
1783 head_read = 0;
1784 } else {
1785 memcpy(bus->rxhdr, pkt->data, SDPCM_HDRLEN);
1786 rd_new.seq_num = rd->seq_num;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001787 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin10510582012-11-05 16:22:26 -08001788 if (brcmf_sdio_hdparser(bus, bus->rxhdr, &rd_new,
1789 BRCMF_SDIO_FT_NORMAL)) {
Franky Lin4754fce2012-09-19 22:21:11 +02001790 rd->len = 0;
1791 brcmu_pkt_buf_free_skb(pkt);
1792 }
Arend van Spriel80969832012-06-09 22:51:44 +02001793 bus->sdcnt.rx_readahead_cnt++;
Franky Lin4754fce2012-09-19 22:21:11 +02001794 if (rd->len != roundup(rd_new.len, 16)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001795 brcmf_err("frame length mismatch:read %d, should be %d\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001796 rd->len,
1797 roundup(rd_new.len, 16) >> 4);
1798 rd->len = 0;
1799 brcmf_sdbrcm_rxfail(bus, true, true);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001800 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001801 brcmu_pkt_buf_free_skb(pkt);
1802 continue;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001803 }
Franky Lin38b0b0d2012-11-05 16:22:24 -08001804 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001805 rd->len_nxtfrm = rd_new.len_nxtfrm;
1806 rd->channel = rd_new.channel;
1807 rd->dat_offset = rd_new.dat_offset;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001808
Joe Perches1e023822012-01-15 00:38:40 -08001809 brcmf_dbg_hex_dump(!(BRCMF_BYTES_ON() &&
1810 BRCMF_DATA_ON()) &&
1811 BRCMF_HDRS_ON(),
1812 bus->rxhdr, SDPCM_HDRLEN,
1813 "RxHdr:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001814
Franky Lin4754fce2012-09-19 22:21:11 +02001815 if (rd_new.channel == SDPCM_CONTROL_CHANNEL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001816 brcmf_err("readahead on control packet %d?\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001817 rd_new.seq_num);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001818 /* Force retry w/normal header read */
Franky Lin4754fce2012-09-19 22:21:11 +02001819 rd->len = 0;
Franky Lin38b0b0d2012-11-05 16:22:24 -08001820 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001821 brcmf_sdbrcm_rxfail(bus, false, true);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001822 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin4754fce2012-09-19 22:21:11 +02001823 brcmu_pkt_buf_free_skb(pkt);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001824 continue;
1825 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02001826 }
1827
Joe Perches1e023822012-01-15 00:38:40 -08001828 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_DATA_ON(),
Franky Lin4754fce2012-09-19 22:21:11 +02001829 pkt->data, rd->len, "Rx Data:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001830
Arend van Spriel5b435de2011-10-05 13:19:03 +02001831 /* Save superframe descriptor and allocate packet frame */
Franky Lin4754fce2012-09-19 22:21:11 +02001832 if (rd->channel == SDPCM_GLOM_CHANNEL) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001833 if (SDPCM_GLOMDESC(&bus->rxhdr[SDPCM_FRAMETAG_LEN])) {
1834 brcmf_dbg(GLOM, "glom descriptor, %d bytes:\n",
Franky Lin4754fce2012-09-19 22:21:11 +02001835 rd->len);
Joe Perches1e023822012-01-15 00:38:40 -08001836 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
Franky Lin4754fce2012-09-19 22:21:11 +02001837 pkt->data, rd->len,
Joe Perches1e023822012-01-15 00:38:40 -08001838 "Glom Data:\n");
Franky Lin4754fce2012-09-19 22:21:11 +02001839 __skb_trim(pkt, rd->len);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001840 skb_pull(pkt, SDPCM_HDRLEN);
1841 bus->glomd = pkt;
1842 } else {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01001843 brcmf_err("%s: glom superframe w/o "
Arend van Spriel5b435de2011-10-05 13:19:03 +02001844 "descriptor!\n", __func__);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001845 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001846 brcmf_sdbrcm_rxfail(bus, false, false);
Franky Lin38b0b0d2012-11-05 16:22:24 -08001847 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001848 }
Franky Lin4754fce2012-09-19 22:21:11 +02001849 /* prepare the descriptor for the next read */
1850 rd->len = rd->len_nxtfrm << 4;
1851 rd->len_nxtfrm = 0;
1852 /* treat all packet as event if we don't know */
1853 rd->channel = SDPCM_EVENT_CHANNEL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001854 continue;
1855 }
1856
1857 /* Fill in packet len and prio, deliver upward */
Franky Lin4754fce2012-09-19 22:21:11 +02001858 __skb_trim(pkt, rd->len);
1859 skb_pull(pkt, rd->dat_offset);
1860
1861 /* prepare the descriptor for the next read */
1862 rd->len = rd->len_nxtfrm << 4;
1863 rd->len_nxtfrm = 0;
1864 /* treat all packet as event if we don't know */
1865 rd->channel = SDPCM_EVENT_CHANNEL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001866
1867 if (pkt->len == 0) {
1868 brcmu_pkt_buf_free_skb(pkt);
1869 continue;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001870 }
1871
Arend van Spriel3aa7aad2013-01-02 15:22:42 +01001872 skb_queue_head_init(&pktlist);
1873 skb_queue_tail(&pktlist, pkt);
Arend van Spriela43af512013-01-02 15:22:43 +01001874 brcmf_rx_frames(bus->sdiodev->dev, &pktlist);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001875 }
Franky Lin4754fce2012-09-19 22:21:11 +02001876
Arend van Spriel5b435de2011-10-05 13:19:03 +02001877 rxcount = maxframes - rxleft;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001878 /* Message if we hit the limit */
1879 if (!rxleft)
Franky Lin4754fce2012-09-19 22:21:11 +02001880 brcmf_dbg(DATA, "hit rx limit of %d frames\n", maxframes);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001881 else
Arend van Spriel5b435de2011-10-05 13:19:03 +02001882 brcmf_dbg(DATA, "processed %d frames\n", rxcount);
1883 /* Back off rxseq if awaiting rtx, update rx_seq */
1884 if (bus->rxskip)
Franky Lin4754fce2012-09-19 22:21:11 +02001885 rd->seq_num--;
1886 bus->rx_seq = rd->seq_num;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001887
1888 return rxcount;
1889}
1890
Arend van Spriel5b435de2011-10-05 13:19:03 +02001891static void
Franky Line92eedf2011-11-22 17:21:50 -08001892brcmf_sdbrcm_wait_event_wakeup(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001893{
1894 if (waitqueue_active(&bus->ctrl_wait))
1895 wake_up_interruptible(&bus->ctrl_wait);
1896 return;
1897}
1898
1899/* Writes a HW/SW header into the packet and sends it. */
1900/* Assumes: (a) header space already there, (b) caller holds lock */
Franky Line92eedf2011-11-22 17:21:50 -08001901static int brcmf_sdbrcm_txpkt(struct brcmf_sdio *bus, struct sk_buff *pkt,
Arend van Spriel7f4bcee2013-03-03 12:45:29 +01001902 uint chan)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001903{
1904 int ret;
1905 u8 *frame;
1906 u16 len, pad = 0;
1907 u32 swheader;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001908 int i;
1909
1910 brcmf_dbg(TRACE, "Enter\n");
1911
1912 frame = (u8 *) (pkt->data);
1913
1914 /* Add alignment padding, allocate new packet if needed */
1915 pad = ((unsigned long)frame % BRCMF_SDALIGN);
1916 if (pad) {
1917 if (skb_headroom(pkt) < pad) {
1918 brcmf_dbg(INFO, "insufficient headroom %d for %d pad\n",
1919 skb_headroom(pkt), pad);
Franky Lin9c1a0432011-12-16 18:37:07 -08001920 bus->sdiodev->bus_if->tx_realloc++;
Arend van Sprielaeecc5742013-04-03 12:40:28 +02001921 ret = skb_cow(pkt, BRCMF_SDALIGN);
1922 if (ret)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001923 goto done;
Arend van Sprielaeecc5742013-04-03 12:40:28 +02001924 pad = ((unsigned long)frame % BRCMF_SDALIGN);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001925 }
Arend van Sprielaeecc5742013-04-03 12:40:28 +02001926 skb_push(pkt, pad);
1927 frame = (u8 *) (pkt->data);
1928 memset(frame, 0, pad + SDPCM_HDRLEN);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001929 }
1930 /* precondition: pad < BRCMF_SDALIGN */
1931
1932 /* Hardware tag: 2 byte len followed by 2 byte ~len check (all LE) */
1933 len = (u16) (pkt->len);
1934 *(__le16 *) frame = cpu_to_le16(len);
1935 *(((__le16 *) frame) + 1) = cpu_to_le16(~len);
1936
1937 /* Software tag: channel, sequence number, data offset */
1938 swheader =
1939 ((chan << SDPCM_CHANNEL_SHIFT) & SDPCM_CHANNEL_MASK) | bus->tx_seq |
1940 (((pad +
1941 SDPCM_HDRLEN) << SDPCM_DOFFSET_SHIFT) & SDPCM_DOFFSET_MASK);
1942
Arend van Spriela0427802013-04-03 12:40:27 +02001943 *(((__le32 *) frame) + 1) = cpu_to_le32(swheader);
1944 *(((__le32 *) frame) + 2) = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001945
Joe Perches8ae74652012-01-15 00:38:38 -08001946#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02001947 tx_packets[pkt->priority]++;
Joe Perches18aad4f2012-01-15 00:38:42 -08001948#endif
Joe Perches1e023822012-01-15 00:38:40 -08001949
1950 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() &&
1951 ((BRCMF_CTL_ON() && chan == SDPCM_CONTROL_CHANNEL) ||
1952 (BRCMF_DATA_ON() && chan != SDPCM_CONTROL_CHANNEL)),
1953 frame, len, "Tx Frame:\n");
1954 brcmf_dbg_hex_dump(!(BRCMF_BYTES_ON() &&
1955 ((BRCMF_CTL_ON() &&
1956 chan == SDPCM_CONTROL_CHANNEL) ||
1957 (BRCMF_DATA_ON() &&
1958 chan != SDPCM_CONTROL_CHANNEL))) &&
1959 BRCMF_HDRS_ON(),
1960 frame, min_t(u16, len, 16), "TxHdr:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001961
1962 /* Raise len to next SDIO block to eliminate tail command */
1963 if (bus->roundup && bus->blocksize && (len > bus->blocksize)) {
1964 u16 pad = bus->blocksize - (len % bus->blocksize);
1965 if ((pad <= bus->roundup) && (pad < bus->blocksize))
1966 len += pad;
1967 } else if (len % BRCMF_SDALIGN) {
1968 len += BRCMF_SDALIGN - (len % BRCMF_SDALIGN);
1969 }
1970
1971 /* Some controllers have trouble with odd bytes -- round to even */
1972 if (len & (ALIGNMENT - 1))
1973 len = roundup(len, ALIGNMENT);
1974
Franky Lin38b0b0d2012-11-05 16:22:24 -08001975 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5adfeb62011-11-22 17:21:38 -08001976 ret = brcmf_sdcard_send_pkt(bus->sdiodev, bus->sdiodev->sbwad,
1977 SDIO_FUNC_2, F2SYNC, pkt);
Arend van Spriel80969832012-06-09 22:51:44 +02001978 bus->sdcnt.f2txdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001979
1980 if (ret < 0) {
1981 /* On failure, abort the command and terminate the frame */
1982 brcmf_dbg(INFO, "sdio error %d, abort command and terminate frame\n",
1983 ret);
Arend van Spriel80969832012-06-09 22:51:44 +02001984 bus->sdcnt.tx_sderrs++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001985
1986 brcmf_sdcard_abort(bus->sdiodev, SDIO_FUNC_2);
Franky Lin3bba8292012-05-04 18:27:33 -07001987 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL,
1988 SFC_WF_TERM, NULL);
Arend van Spriel80969832012-06-09 22:51:44 +02001989 bus->sdcnt.f1regdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001990
1991 for (i = 0; i < 3; i++) {
1992 u8 hi, lo;
Franky Lin45db3392012-05-04 18:27:32 -07001993 hi = brcmf_sdio_regrb(bus->sdiodev,
1994 SBSDIO_FUNC1_WFRAMEBCHI, NULL);
1995 lo = brcmf_sdio_regrb(bus->sdiodev,
1996 SBSDIO_FUNC1_WFRAMEBCLO, NULL);
Arend van Spriel80969832012-06-09 22:51:44 +02001997 bus->sdcnt.f1regdata += 2;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001998 if ((hi == 0) && (lo == 0))
1999 break;
2000 }
2001
2002 }
Franky Lin38b0b0d2012-11-05 16:22:24 -08002003 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002004 if (ret == 0)
2005 bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQUENCE_WRAP;
2006
2007done:
2008 /* restore pkt buffer pointer before calling tx complete routine */
2009 skb_pull(pkt, SDPCM_HDRLEN + pad);
Arend van Spriela886f7f2013-04-03 12:40:26 +02002010 brcmf_txcomplete(bus->sdiodev->dev, pkt, ret == 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002011 return ret;
2012}
2013
Franky Line92eedf2011-11-22 17:21:50 -08002014static uint brcmf_sdbrcm_sendfromq(struct brcmf_sdio *bus, uint maxframes)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002015{
2016 struct sk_buff *pkt;
2017 u32 intstatus = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002018 int ret = 0, prec_out;
2019 uint cnt = 0;
2020 uint datalen;
2021 u8 tx_prec_map;
2022
Arend van Spriel5b435de2011-10-05 13:19:03 +02002023 brcmf_dbg(TRACE, "Enter\n");
2024
2025 tx_prec_map = ~bus->flowcontrol;
2026
2027 /* Send frames until the limit or some other event */
2028 for (cnt = 0; (cnt < maxframes) && data_ok(bus); cnt++) {
2029 spin_lock_bh(&bus->txqlock);
2030 pkt = brcmu_pktq_mdeq(&bus->txq, tx_prec_map, &prec_out);
2031 if (pkt == NULL) {
2032 spin_unlock_bh(&bus->txqlock);
2033 break;
2034 }
2035 spin_unlock_bh(&bus->txqlock);
2036 datalen = pkt->len - SDPCM_HDRLEN;
2037
Arend van Spriel7f4bcee2013-03-03 12:45:29 +01002038 ret = brcmf_sdbrcm_txpkt(bus, pkt, SDPCM_DATA_CHANNEL);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002039
2040 /* In poll mode, need to check for other events */
2041 if (!bus->intr && cnt) {
2042 /* Check device status, signal pending interrupt */
Franky Lin38b0b0d2012-11-05 16:22:24 -08002043 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin5c15c232012-05-04 18:27:37 -07002044 ret = r_sdreg32(bus, &intstatus,
2045 offsetof(struct sdpcmd_regs,
2046 intstatus));
Franky Lin38b0b0d2012-11-05 16:22:24 -08002047 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel80969832012-06-09 22:51:44 +02002048 bus->sdcnt.f2txdata++;
Franky Lin5c15c232012-05-04 18:27:37 -07002049 if (ret != 0)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002050 break;
2051 if (intstatus & bus->hostintmask)
Franky Lin1d382272012-09-13 21:11:59 +02002052 atomic_set(&bus->ipend, 1);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002053 }
2054 }
2055
2056 /* Deflow-control stack if needed */
Hante Meuleman05dde972013-02-06 18:40:36 +01002057 if ((bus->sdiodev->bus_if->state == BRCMF_BUS_DATA) &&
Franky Linc8bf3482011-12-16 18:37:08 -08002058 bus->txoff && (pktq_len(&bus->txq) < TXLOW)) {
Hante Meuleman90d03ff2012-09-11 21:18:46 +02002059 bus->txoff = false;
2060 brcmf_txflowblock(bus->sdiodev->dev, false);
Franky Linc8bf3482011-12-16 18:37:08 -08002061 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002062
2063 return cnt;
2064}
2065
Franky Lina9ffda82011-12-16 18:37:12 -08002066static void brcmf_sdbrcm_bus_stop(struct device *dev)
2067{
2068 u32 local_hostintmask;
2069 u8 saveclk;
Franky Lina9ffda82011-12-16 18:37:12 -08002070 int err;
2071 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01002072 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Lina9ffda82011-12-16 18:37:12 -08002073 struct brcmf_sdio *bus = sdiodev->bus;
2074
2075 brcmf_dbg(TRACE, "Enter\n");
2076
2077 if (bus->watchdog_tsk) {
2078 send_sig(SIGTERM, bus->watchdog_tsk, 1);
2079 kthread_stop(bus->watchdog_tsk);
2080 bus->watchdog_tsk = NULL;
2081 }
2082
Franky Lin38b0b0d2012-11-05 16:22:24 -08002083 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lina9ffda82011-12-16 18:37:12 -08002084
Franky Lina9ffda82011-12-16 18:37:12 -08002085 /* Enable clock for device interrupts */
Piotr Haber4a3da992013-04-11 13:28:46 +02002086 brcmf_sdbrcm_bus_sleep(bus, false, false);
Franky Lina9ffda82011-12-16 18:37:12 -08002087
2088 /* Disable and clear interrupts at the chip level also */
Franky Lin58692752012-05-04 18:27:36 -07002089 w_sdreg32(bus, 0, offsetof(struct sdpcmd_regs, hostintmask));
Franky Lina9ffda82011-12-16 18:37:12 -08002090 local_hostintmask = bus->hostintmask;
2091 bus->hostintmask = 0;
2092
2093 /* Change our idea of bus state */
2094 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
2095
2096 /* Force clocks on backplane to be sure F2 interrupt propagates */
Franky Lin45db3392012-05-04 18:27:32 -07002097 saveclk = brcmf_sdio_regrb(bus->sdiodev,
2098 SBSDIO_FUNC1_CHIPCLKCSR, &err);
Franky Lina9ffda82011-12-16 18:37:12 -08002099 if (!err) {
Franky Lin3bba8292012-05-04 18:27:33 -07002100 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
2101 (saveclk | SBSDIO_FORCE_HT), &err);
Franky Lina9ffda82011-12-16 18:37:12 -08002102 }
2103 if (err)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002104 brcmf_err("Failed to force clock for F2: err %d\n", err);
Franky Lina9ffda82011-12-16 18:37:12 -08002105
2106 /* Turn off the bus (F2), free any pending packets */
2107 brcmf_dbg(INTR, "disable SDIO interrupts\n");
Franky Lin3bba8292012-05-04 18:27:33 -07002108 brcmf_sdio_regwb(bus->sdiodev, SDIO_CCCR_IOEx, SDIO_FUNC_ENABLE_1,
2109 NULL);
Franky Lina9ffda82011-12-16 18:37:12 -08002110
2111 /* Clear any pending interrupts now that F2 is disabled */
2112 w_sdreg32(bus, local_hostintmask,
Franky Lin58692752012-05-04 18:27:36 -07002113 offsetof(struct sdpcmd_regs, intstatus));
Franky Lina9ffda82011-12-16 18:37:12 -08002114
2115 /* Turn off the backplane clock (only) */
2116 brcmf_sdbrcm_clkctl(bus, CLK_SDONLY, false);
Franky Lin38b0b0d2012-11-05 16:22:24 -08002117 sdio_release_host(bus->sdiodev->func[1]);
Franky Lina9ffda82011-12-16 18:37:12 -08002118
2119 /* Clear the data packet queues */
2120 brcmu_pktq_flush(&bus->txq, true, NULL, NULL);
2121
2122 /* Clear any held glomming stuff */
2123 if (bus->glomd)
2124 brcmu_pkt_buf_free_skb(bus->glomd);
2125 brcmf_sdbrcm_free_glom(bus);
2126
2127 /* Clear rx control and wake any waiters */
Franky Lindd43a012012-11-05 16:22:22 -08002128 spin_lock_bh(&bus->rxctl_lock);
Franky Lina9ffda82011-12-16 18:37:12 -08002129 bus->rxlen = 0;
Franky Lindd43a012012-11-05 16:22:22 -08002130 spin_unlock_bh(&bus->rxctl_lock);
Franky Lina9ffda82011-12-16 18:37:12 -08002131 brcmf_sdbrcm_dcmd_resp_wake(bus);
2132
2133 /* Reset some F2 state stuff */
2134 bus->rxskip = false;
2135 bus->tx_seq = bus->rx_seq = 0;
Franky Lina9ffda82011-12-16 18:37:12 -08002136}
2137
Franky Linba89bf12012-04-27 18:56:59 -07002138#ifdef CONFIG_BRCMFMAC_SDIO_OOB
2139static inline void brcmf_sdbrcm_clrintr(struct brcmf_sdio *bus)
2140{
2141 unsigned long flags;
2142
2143 spin_lock_irqsave(&bus->sdiodev->irq_en_lock, flags);
Franky Lin1d382272012-09-13 21:11:59 +02002144 if (!bus->sdiodev->irq_en && !atomic_read(&bus->ipend)) {
Franky Linba89bf12012-04-27 18:56:59 -07002145 enable_irq(bus->sdiodev->irq);
2146 bus->sdiodev->irq_en = true;
2147 }
2148 spin_unlock_irqrestore(&bus->sdiodev->irq_en_lock, flags);
2149}
2150#else
2151static inline void brcmf_sdbrcm_clrintr(struct brcmf_sdio *bus)
2152{
2153}
2154#endif /* CONFIG_BRCMFMAC_SDIO_OOB */
2155
Franky Linf1e68c22012-09-13 21:12:00 +02002156static inline void brcmf_sdbrcm_adddpctsk(struct brcmf_sdio *bus)
2157{
2158 struct list_head *new_hd;
2159 unsigned long flags;
2160
2161 if (in_interrupt())
2162 new_hd = kzalloc(sizeof(struct list_head), GFP_ATOMIC);
2163 else
2164 new_hd = kzalloc(sizeof(struct list_head), GFP_KERNEL);
2165 if (new_hd == NULL)
2166 return;
2167
2168 spin_lock_irqsave(&bus->dpc_tl_lock, flags);
2169 list_add_tail(new_hd, &bus->dpc_tsklst);
2170 spin_unlock_irqrestore(&bus->dpc_tl_lock, flags);
2171}
2172
Franky Lin45316032012-09-13 21:12:03 +02002173static int brcmf_sdio_intr_rstatus(struct brcmf_sdio *bus)
2174{
2175 u8 idx;
2176 u32 addr;
2177 unsigned long val;
2178 int n, ret;
2179
2180 idx = brcmf_sdio_chip_getinfidx(bus->ci, BCMA_CORE_SDIO_DEV);
2181 addr = bus->ci->c_inf[idx].base +
2182 offsetof(struct sdpcmd_regs, intstatus);
2183
2184 ret = brcmf_sdio_regrw_helper(bus->sdiodev, addr, &val, false);
2185 bus->sdcnt.f1regdata++;
2186 if (ret != 0)
2187 val = 0;
2188
2189 val &= bus->hostintmask;
2190 atomic_set(&bus->fcstate, !!(val & I_HMB_FC_STATE));
2191
2192 /* Clear interrupts */
2193 if (val) {
2194 ret = brcmf_sdio_regrw_helper(bus->sdiodev, addr, &val, true);
2195 bus->sdcnt.f1regdata++;
2196 }
2197
2198 if (ret) {
2199 atomic_set(&bus->intstatus, 0);
2200 } else if (val) {
2201 for_each_set_bit(n, &val, 32)
2202 set_bit(n, (unsigned long *)&bus->intstatus.counter);
2203 }
2204
2205 return ret;
2206}
2207
Franky Linf1e68c22012-09-13 21:12:00 +02002208static void brcmf_sdbrcm_dpc(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002209{
Franky Lin45316032012-09-13 21:12:03 +02002210 u32 newstatus = 0;
2211 unsigned long intstatus;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002212 uint rxlimit = bus->rxbound; /* Rx frames to read before resched */
2213 uint txlimit = bus->txbound; /* Tx frames to send before resched */
2214 uint framecnt = 0; /* Temporary counter of tx/rx frames */
Franky Lin45316032012-09-13 21:12:03 +02002215 int err = 0, n;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002216
2217 brcmf_dbg(TRACE, "Enter\n");
2218
Franky Lin38b0b0d2012-11-05 16:22:24 -08002219 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002220
2221 /* If waiting for HTAVAIL, check status */
Piotr Haber4a3da992013-04-11 13:28:46 +02002222 if (!bus->sr_enabled && bus->clkstate == CLK_PENDING) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02002223 u8 clkctl, devctl = 0;
2224
Joe Perches8ae74652012-01-15 00:38:38 -08002225#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02002226 /* Check for inconsistent device control */
Franky Lin45db3392012-05-04 18:27:32 -07002227 devctl = brcmf_sdio_regrb(bus->sdiodev,
2228 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002229 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002230 brcmf_err("error reading DEVCTL: %d\n", err);
Franky Lin712ac5b2011-12-16 18:37:09 -08002231 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002232 }
Joe Perches8ae74652012-01-15 00:38:38 -08002233#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02002234
2235 /* Read CSR, if clock on switch to AVAIL, else ignore */
Franky Lin45db3392012-05-04 18:27:32 -07002236 clkctl = brcmf_sdio_regrb(bus->sdiodev,
2237 SBSDIO_FUNC1_CHIPCLKCSR, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002238 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002239 brcmf_err("error reading CSR: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02002240 err);
Franky Lin712ac5b2011-12-16 18:37:09 -08002241 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002242 }
2243
Arend van Sprielc3203372013-04-03 12:40:44 +02002244 brcmf_dbg(SDIO, "DPC: PENDING, devctl 0x%02x clkctl 0x%02x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02002245 devctl, clkctl);
2246
2247 if (SBSDIO_HTAV(clkctl)) {
Franky Lin45db3392012-05-04 18:27:32 -07002248 devctl = brcmf_sdio_regrb(bus->sdiodev,
2249 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002250 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002251 brcmf_err("error reading DEVCTL: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02002252 err);
Franky Lin712ac5b2011-12-16 18:37:09 -08002253 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002254 }
2255 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
Franky Lin3bba8292012-05-04 18:27:33 -07002256 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
2257 devctl, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002258 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002259 brcmf_err("error writing DEVCTL: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02002260 err);
Franky Lin712ac5b2011-12-16 18:37:09 -08002261 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002262 }
2263 bus->clkstate = CLK_AVAIL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002264 }
2265 }
2266
Arend van Spriel5b435de2011-10-05 13:19:03 +02002267 /* Make sure backplane clock is on */
Piotr Haber4a3da992013-04-11 13:28:46 +02002268 brcmf_sdbrcm_bus_sleep(bus, false, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002269
2270 /* Pending interrupt indicates new device status */
Franky Lin1d382272012-09-13 21:11:59 +02002271 if (atomic_read(&bus->ipend) > 0) {
2272 atomic_set(&bus->ipend, 0);
Franky Lin45316032012-09-13 21:12:03 +02002273 err = brcmf_sdio_intr_rstatus(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002274 }
2275
Franky Lin45316032012-09-13 21:12:03 +02002276 /* Start with leftover status bits */
2277 intstatus = atomic_xchg(&bus->intstatus, 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002278
2279 /* Handle flow-control change: read new state in case our ack
2280 * crossed another change interrupt. If change still set, assume
2281 * FC ON for safety, let next loop through do the debounce.
2282 */
2283 if (intstatus & I_HMB_FC_CHANGE) {
2284 intstatus &= ~I_HMB_FC_CHANGE;
Franky Lin5c15c232012-05-04 18:27:37 -07002285 err = w_sdreg32(bus, I_HMB_FC_CHANGE,
2286 offsetof(struct sdpcmd_regs, intstatus));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002287
Franky Lin5c15c232012-05-04 18:27:37 -07002288 err = r_sdreg32(bus, &newstatus,
2289 offsetof(struct sdpcmd_regs, intstatus));
Arend van Spriel80969832012-06-09 22:51:44 +02002290 bus->sdcnt.f1regdata += 2;
Franky Lin45316032012-09-13 21:12:03 +02002291 atomic_set(&bus->fcstate,
2292 !!(newstatus & (I_HMB_FC_STATE | I_HMB_FC_CHANGE)));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002293 intstatus |= (newstatus & bus->hostintmask);
2294 }
2295
2296 /* Handle host mailbox indication */
2297 if (intstatus & I_HMB_HOST_INT) {
2298 intstatus &= ~I_HMB_HOST_INT;
2299 intstatus |= brcmf_sdbrcm_hostmail(bus);
2300 }
2301
Franky Lin38b0b0d2012-11-05 16:22:24 -08002302 sdio_release_host(bus->sdiodev->func[1]);
Franky Lin7cdf57d2012-11-05 16:22:23 -08002303
Arend van Spriel5b435de2011-10-05 13:19:03 +02002304 /* Generally don't ask for these, can get CRC errors... */
2305 if (intstatus & I_WR_OOSYNC) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002306 brcmf_err("Dongle reports WR_OOSYNC\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002307 intstatus &= ~I_WR_OOSYNC;
2308 }
2309
2310 if (intstatus & I_RD_OOSYNC) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002311 brcmf_err("Dongle reports RD_OOSYNC\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002312 intstatus &= ~I_RD_OOSYNC;
2313 }
2314
2315 if (intstatus & I_SBINT) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002316 brcmf_err("Dongle reports SBINT\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002317 intstatus &= ~I_SBINT;
2318 }
2319
2320 /* Would be active due to wake-wlan in gSPI */
2321 if (intstatus & I_CHIPACTIVE) {
2322 brcmf_dbg(INFO, "Dongle reports CHIPACTIVE\n");
2323 intstatus &= ~I_CHIPACTIVE;
2324 }
2325
2326 /* Ignore frame indications if rxskip is set */
2327 if (bus->rxskip)
2328 intstatus &= ~I_HMB_FRAME_IND;
2329
2330 /* On frame indication, read available frames */
Franky Lin03d5c362012-09-13 21:12:01 +02002331 if (PKT_AVAILABLE() && bus->clkstate == CLK_AVAIL) {
Franky Lin4754fce2012-09-19 22:21:11 +02002332 framecnt = brcmf_sdio_readframes(bus, rxlimit);
2333 if (!bus->rxpending)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002334 intstatus &= ~I_HMB_FRAME_IND;
2335 rxlimit -= min(framecnt, rxlimit);
2336 }
2337
2338 /* Keep still-pending events for next scheduling */
Franky Lin45316032012-09-13 21:12:03 +02002339 if (intstatus) {
2340 for_each_set_bit(n, &intstatus, 32)
2341 set_bit(n, (unsigned long *)&bus->intstatus.counter);
2342 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002343
Franky Linba89bf12012-04-27 18:56:59 -07002344 brcmf_sdbrcm_clrintr(bus);
2345
Arend van Spriel5b435de2011-10-05 13:19:03 +02002346 if (data_ok(bus) && bus->ctrl_frame_stat &&
2347 (bus->clkstate == CLK_AVAIL)) {
Franky Lin03d5c362012-09-13 21:12:01 +02002348 int i;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002349
Franky Lin38b0b0d2012-11-05 16:22:24 -08002350 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin03d5c362012-09-13 21:12:01 +02002351 err = brcmf_sdcard_send_buf(bus->sdiodev, bus->sdiodev->sbwad,
Joe Perches2c208892012-06-04 12:44:17 +00002352 SDIO_FUNC_2, F2SYNC, bus->ctrl_frame_buf,
Arend van Spriel5adfeb62011-11-22 17:21:38 -08002353 (u32) bus->ctrl_frame_len);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002354
Franky Lin03d5c362012-09-13 21:12:01 +02002355 if (err < 0) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02002356 /* On failure, abort the command and
2357 terminate the frame */
2358 brcmf_dbg(INFO, "sdio error %d, abort command and terminate frame\n",
Franky Lin03d5c362012-09-13 21:12:01 +02002359 err);
Arend van Spriel80969832012-06-09 22:51:44 +02002360 bus->sdcnt.tx_sderrs++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002361
2362 brcmf_sdcard_abort(bus->sdiodev, SDIO_FUNC_2);
2363
Franky Lin3bba8292012-05-04 18:27:33 -07002364 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL,
Franky Lin5c15c232012-05-04 18:27:37 -07002365 SFC_WF_TERM, &err);
Arend van Spriel80969832012-06-09 22:51:44 +02002366 bus->sdcnt.f1regdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002367
2368 for (i = 0; i < 3; i++) {
2369 u8 hi, lo;
Franky Lin45db3392012-05-04 18:27:32 -07002370 hi = brcmf_sdio_regrb(bus->sdiodev,
2371 SBSDIO_FUNC1_WFRAMEBCHI,
Franky Lin5c15c232012-05-04 18:27:37 -07002372 &err);
Franky Lin45db3392012-05-04 18:27:32 -07002373 lo = brcmf_sdio_regrb(bus->sdiodev,
2374 SBSDIO_FUNC1_WFRAMEBCLO,
Franky Lin5c15c232012-05-04 18:27:37 -07002375 &err);
Arend van Spriel80969832012-06-09 22:51:44 +02002376 bus->sdcnt.f1regdata += 2;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002377 if ((hi == 0) && (lo == 0))
2378 break;
2379 }
2380
Franky Lin03d5c362012-09-13 21:12:01 +02002381 } else {
Arend van Spriel5b435de2011-10-05 13:19:03 +02002382 bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQUENCE_WRAP;
Franky Lin03d5c362012-09-13 21:12:01 +02002383 }
Franky Lin38b0b0d2012-11-05 16:22:24 -08002384 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002385 bus->ctrl_frame_stat = false;
2386 brcmf_sdbrcm_wait_event_wakeup(bus);
2387 }
2388 /* Send queued frames (limit 1 if rx may still be pending) */
Franky Lin45316032012-09-13 21:12:03 +02002389 else if ((bus->clkstate == CLK_AVAIL) && !atomic_read(&bus->fcstate) &&
Arend van Spriel5b435de2011-10-05 13:19:03 +02002390 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) && txlimit
2391 && data_ok(bus)) {
Franky Lin4754fce2012-09-19 22:21:11 +02002392 framecnt = bus->rxpending ? min(txlimit, bus->txminmax) :
2393 txlimit;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002394 framecnt = brcmf_sdbrcm_sendfromq(bus, framecnt);
2395 txlimit -= framecnt;
2396 }
2397
Franky Lin5c15c232012-05-04 18:27:37 -07002398 if ((bus->sdiodev->bus_if->state == BRCMF_BUS_DOWN) || (err != 0)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002399 brcmf_err("failed backplane access over SDIO, halting operation\n");
Franky Lin712ac5b2011-12-16 18:37:09 -08002400 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
Franky Lin45316032012-09-13 21:12:03 +02002401 atomic_set(&bus->intstatus, 0);
2402 } else if (atomic_read(&bus->intstatus) ||
2403 atomic_read(&bus->ipend) > 0 ||
2404 (!atomic_read(&bus->fcstate) &&
2405 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) &&
2406 data_ok(bus)) || PKT_AVAILABLE()) {
Franky Linf1e68c22012-09-13 21:12:00 +02002407 brcmf_sdbrcm_adddpctsk(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002408 }
2409
Arend van Spriel5b435de2011-10-05 13:19:03 +02002410 /* If we're done for now, turn off clock request. */
2411 if ((bus->clkstate != CLK_PENDING)
2412 && bus->idletime == BRCMF_IDLE_IMMEDIATE) {
2413 bus->activity = false;
Piotr Haber4a3da992013-04-11 13:28:46 +02002414 brcmf_dbg(SDIO, "idle state\n");
Franky Lin38b0b0d2012-11-05 16:22:24 -08002415 sdio_claim_host(bus->sdiodev->func[1]);
Piotr Haber4a3da992013-04-11 13:28:46 +02002416 brcmf_sdbrcm_bus_sleep(bus, true, false);
Franky Lin38b0b0d2012-11-05 16:22:24 -08002417 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002418 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002419}
2420
Arend van Spriele2432b62013-04-03 12:40:38 +02002421static struct pktq *brcmf_sdbrcm_bus_gettxq(struct device *dev)
2422{
2423 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
2424 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
2425 struct brcmf_sdio *bus = sdiodev->bus;
2426
2427 return &bus->txq;
2428}
2429
Franky Linb9692d12011-12-16 18:37:15 -08002430static int brcmf_sdbrcm_bus_txdata(struct device *dev, struct sk_buff *pkt)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002431{
2432 int ret = -EBADE;
2433 uint datalen, prec;
Franky Linbf347bb2011-11-22 17:21:56 -08002434 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01002435 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Linbf347bb2011-11-22 17:21:56 -08002436 struct brcmf_sdio *bus = sdiodev->bus;
Franky Linf1e68c22012-09-13 21:12:00 +02002437 unsigned long flags;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002438
2439 brcmf_dbg(TRACE, "Enter\n");
2440
2441 datalen = pkt->len;
2442
2443 /* Add space for the header */
2444 skb_push(pkt, SDPCM_HDRLEN);
2445 /* precondition: IS_ALIGNED((unsigned long)(pkt->data), 2) */
2446
2447 prec = prio2prec((pkt->priority & PRIOMASK));
2448
2449 /* Check for existing queue, current flow-control,
2450 pending event, or pending clock */
2451 brcmf_dbg(TRACE, "deferring pktq len %d\n", pktq_len(&bus->txq));
Arend van Spriel80969832012-06-09 22:51:44 +02002452 bus->sdcnt.fcqueued++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002453
2454 /* Priority based enq */
2455 spin_lock_bh(&bus->txqlock);
Joe Perches23677ce2012-02-09 11:17:23 +00002456 if (!brcmf_c_prec_enq(bus->sdiodev->dev, &bus->txq, pkt, prec)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02002457 skb_pull(pkt, SDPCM_HDRLEN);
Franky Linc9957882011-12-16 18:36:58 -08002458 brcmf_txcomplete(bus->sdiodev->dev, pkt, false);
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002459 brcmf_err("out of bus->txq !!!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002460 ret = -ENOSR;
2461 } else {
2462 ret = 0;
2463 }
2464 spin_unlock_bh(&bus->txqlock);
2465
Franky Linc8bf3482011-12-16 18:37:08 -08002466 if (pktq_len(&bus->txq) >= TXHI) {
Hante Meuleman90d03ff2012-09-11 21:18:46 +02002467 bus->txoff = true;
2468 brcmf_txflowblock(bus->sdiodev->dev, true);
Franky Linc8bf3482011-12-16 18:37:08 -08002469 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002470
Joe Perches8ae74652012-01-15 00:38:38 -08002471#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02002472 if (pktq_plen(&bus->txq, prec) > qcount[prec])
2473 qcount[prec] = pktq_plen(&bus->txq, prec);
2474#endif
Franky Linf1e68c22012-09-13 21:12:00 +02002475
2476 spin_lock_irqsave(&bus->dpc_tl_lock, flags);
2477 if (list_empty(&bus->dpc_tsklst)) {
2478 spin_unlock_irqrestore(&bus->dpc_tl_lock, flags);
2479
2480 brcmf_sdbrcm_adddpctsk(bus);
2481 queue_work(bus->brcmf_wq, &bus->datawork);
2482 } else {
2483 spin_unlock_irqrestore(&bus->dpc_tl_lock, flags);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002484 }
2485
2486 return ret;
2487}
2488
2489static int
Franky Line92eedf2011-11-22 17:21:50 -08002490brcmf_sdbrcm_membytes(struct brcmf_sdio *bus, bool write, u32 address, u8 *data,
Arend van Spriel5b435de2011-10-05 13:19:03 +02002491 uint size)
2492{
2493 int bcmerror = 0;
2494 u32 sdaddr;
2495 uint dsize;
2496
2497 /* Determine initial transfer parameters */
2498 sdaddr = address & SBSDIO_SB_OFT_ADDR_MASK;
2499 if ((sdaddr + size) & SBSDIO_SBWINDOW_MASK)
2500 dsize = (SBSDIO_SB_OFT_ADDR_LIMIT - sdaddr);
2501 else
2502 dsize = size;
2503
Franky Lin7057fd02012-09-13 21:12:02 +02002504 sdio_claim_host(bus->sdiodev->func[1]);
2505
Arend van Spriel5b435de2011-10-05 13:19:03 +02002506 /* Set the backplane window to include the start address */
2507 bcmerror = brcmf_sdcard_set_sbaddr_window(bus->sdiodev, address);
2508 if (bcmerror) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002509 brcmf_err("window change failed\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002510 goto xfer_done;
2511 }
2512
2513 /* Do the transfer(s) */
2514 while (size) {
Arend van Sprielc3203372013-04-03 12:40:44 +02002515 brcmf_dbg(SDIO, "%s %d bytes at offset 0x%08x in window 0x%08x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02002516 write ? "write" : "read", dsize,
2517 sdaddr, address & SBSDIO_SBWINDOW_MASK);
2518 bcmerror = brcmf_sdcard_rwdata(bus->sdiodev, write,
2519 sdaddr, data, dsize);
2520 if (bcmerror) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002521 brcmf_err("membytes transfer failed\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002522 break;
2523 }
2524
2525 /* Adjust for next transfer (if any) */
2526 size -= dsize;
2527 if (size) {
2528 data += dsize;
2529 address += dsize;
2530 bcmerror = brcmf_sdcard_set_sbaddr_window(bus->sdiodev,
2531 address);
2532 if (bcmerror) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002533 brcmf_err("window change failed\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002534 break;
2535 }
2536 sdaddr = 0;
2537 dsize = min_t(uint, SBSDIO_SB_OFT_ADDR_LIMIT, size);
2538 }
2539 }
2540
2541xfer_done:
2542 /* Return the window to backplane enumeration space for core access */
2543 if (brcmf_sdcard_set_sbaddr_window(bus->sdiodev, bus->sdiodev->sbwad))
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002544 brcmf_err("FAILED to set window back to 0x%x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02002545 bus->sdiodev->sbwad);
2546
Franky Lin7057fd02012-09-13 21:12:02 +02002547 sdio_release_host(bus->sdiodev->func[1]);
2548
Arend van Spriel5b435de2011-10-05 13:19:03 +02002549 return bcmerror;
2550}
2551
Joe Perches8ae74652012-01-15 00:38:38 -08002552#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02002553#define CONSOLE_LINE_MAX 192
2554
Franky Line92eedf2011-11-22 17:21:50 -08002555static int brcmf_sdbrcm_readconsole(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002556{
2557 struct brcmf_console *c = &bus->console;
2558 u8 line[CONSOLE_LINE_MAX], ch;
2559 u32 n, idx, addr;
2560 int rv;
2561
2562 /* Don't do anything until FWREADY updates console address */
2563 if (bus->console_addr == 0)
2564 return 0;
2565
2566 /* Read console log struct */
2567 addr = bus->console_addr + offsetof(struct rte_console, log_le);
2568 rv = brcmf_sdbrcm_membytes(bus, false, addr, (u8 *)&c->log_le,
2569 sizeof(c->log_le));
2570 if (rv < 0)
2571 return rv;
2572
2573 /* Allocate console buffer (one time only) */
2574 if (c->buf == NULL) {
2575 c->bufsize = le32_to_cpu(c->log_le.buf_size);
2576 c->buf = kmalloc(c->bufsize, GFP_ATOMIC);
2577 if (c->buf == NULL)
2578 return -ENOMEM;
2579 }
2580
2581 idx = le32_to_cpu(c->log_le.idx);
2582
2583 /* Protect against corrupt value */
2584 if (idx > c->bufsize)
2585 return -EBADE;
2586
2587 /* Skip reading the console buffer if the index pointer
2588 has not moved */
2589 if (idx == c->last)
2590 return 0;
2591
2592 /* Read the console buffer */
2593 addr = le32_to_cpu(c->log_le.buf);
2594 rv = brcmf_sdbrcm_membytes(bus, false, addr, c->buf, c->bufsize);
2595 if (rv < 0)
2596 return rv;
2597
2598 while (c->last != idx) {
2599 for (n = 0; n < CONSOLE_LINE_MAX - 2; n++) {
2600 if (c->last == idx) {
2601 /* This would output a partial line.
2602 * Instead, back up
2603 * the buffer pointer and output this
2604 * line next time around.
2605 */
2606 if (c->last >= n)
2607 c->last -= n;
2608 else
2609 c->last = c->bufsize - n;
2610 goto break2;
2611 }
2612 ch = c->buf[c->last];
2613 c->last = (c->last + 1) % c->bufsize;
2614 if (ch == '\n')
2615 break;
2616 line[n] = ch;
2617 }
2618
2619 if (n > 0) {
2620 if (line[n - 1] == '\r')
2621 n--;
2622 line[n] = 0;
Joe Perches18aad4f2012-01-15 00:38:42 -08002623 pr_debug("CONSOLE: %s\n", line);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002624 }
2625 }
2626break2:
2627
2628 return 0;
2629}
Joe Perches8ae74652012-01-15 00:38:38 -08002630#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02002631
Franky Line92eedf2011-11-22 17:21:50 -08002632static int brcmf_tx_frame(struct brcmf_sdio *bus, u8 *frame, u16 len)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002633{
2634 int i;
2635 int ret;
2636
2637 bus->ctrl_frame_stat = false;
Arend van Spriel5adfeb62011-11-22 17:21:38 -08002638 ret = brcmf_sdcard_send_buf(bus->sdiodev, bus->sdiodev->sbwad,
2639 SDIO_FUNC_2, F2SYNC, frame, len);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002640
2641 if (ret < 0) {
2642 /* On failure, abort the command and terminate the frame */
2643 brcmf_dbg(INFO, "sdio error %d, abort command and terminate frame\n",
2644 ret);
Arend van Spriel80969832012-06-09 22:51:44 +02002645 bus->sdcnt.tx_sderrs++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002646
2647 brcmf_sdcard_abort(bus->sdiodev, SDIO_FUNC_2);
2648
Franky Lin3bba8292012-05-04 18:27:33 -07002649 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL,
2650 SFC_WF_TERM, NULL);
Arend van Spriel80969832012-06-09 22:51:44 +02002651 bus->sdcnt.f1regdata++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002652
2653 for (i = 0; i < 3; i++) {
2654 u8 hi, lo;
Franky Lin45db3392012-05-04 18:27:32 -07002655 hi = brcmf_sdio_regrb(bus->sdiodev,
2656 SBSDIO_FUNC1_WFRAMEBCHI, NULL);
2657 lo = brcmf_sdio_regrb(bus->sdiodev,
2658 SBSDIO_FUNC1_WFRAMEBCLO, NULL);
Arend van Spriel80969832012-06-09 22:51:44 +02002659 bus->sdcnt.f1regdata += 2;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002660 if (hi == 0 && lo == 0)
2661 break;
2662 }
2663 return ret;
2664 }
2665
2666 bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQUENCE_WRAP;
2667
2668 return ret;
2669}
2670
Franky Linfcf094f2011-12-16 18:37:16 -08002671static int
Franky Lin47a1ce72011-11-22 17:21:55 -08002672brcmf_sdbrcm_bus_txctl(struct device *dev, unsigned char *msg, uint msglen)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002673{
2674 u8 *frame;
2675 u16 len;
2676 u32 swheader;
2677 uint retries = 0;
2678 u8 doff = 0;
2679 int ret = -1;
Franky Lin47a1ce72011-11-22 17:21:55 -08002680 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01002681 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Lin47a1ce72011-11-22 17:21:55 -08002682 struct brcmf_sdio *bus = sdiodev->bus;
Franky Linf1e68c22012-09-13 21:12:00 +02002683 unsigned long flags;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002684
2685 brcmf_dbg(TRACE, "Enter\n");
2686
2687 /* Back the pointer to make a room for bus header */
2688 frame = msg - SDPCM_HDRLEN;
2689 len = (msglen += SDPCM_HDRLEN);
2690
2691 /* Add alignment padding (optional for ctl frames) */
2692 doff = ((unsigned long)frame % BRCMF_SDALIGN);
2693 if (doff) {
2694 frame -= doff;
2695 len += doff;
2696 msglen += doff;
2697 memset(frame, 0, doff + SDPCM_HDRLEN);
2698 }
2699 /* precondition: doff < BRCMF_SDALIGN */
2700 doff += SDPCM_HDRLEN;
2701
2702 /* Round send length to next SDIO block */
2703 if (bus->roundup && bus->blocksize && (len > bus->blocksize)) {
2704 u16 pad = bus->blocksize - (len % bus->blocksize);
2705 if ((pad <= bus->roundup) && (pad < bus->blocksize))
2706 len += pad;
2707 } else if (len % BRCMF_SDALIGN) {
2708 len += BRCMF_SDALIGN - (len % BRCMF_SDALIGN);
2709 }
2710
2711 /* Satisfy length-alignment requirements */
2712 if (len & (ALIGNMENT - 1))
2713 len = roundup(len, ALIGNMENT);
2714
2715 /* precondition: IS_ALIGNED((unsigned long)frame, 2) */
2716
Arend van Spriel5b435de2011-10-05 13:19:03 +02002717 /* Make sure backplane clock is on */
Franky Lin38b0b0d2012-11-05 16:22:24 -08002718 sdio_claim_host(bus->sdiodev->func[1]);
Piotr Haber4a3da992013-04-11 13:28:46 +02002719 brcmf_sdbrcm_bus_sleep(bus, false, false);
Franky Lin38b0b0d2012-11-05 16:22:24 -08002720 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002721
2722 /* Hardware tag: 2 byte len followed by 2 byte ~len check (all LE) */
2723 *(__le16 *) frame = cpu_to_le16((u16) msglen);
2724 *(((__le16 *) frame) + 1) = cpu_to_le16(~msglen);
2725
2726 /* Software tag: channel, sequence number, data offset */
2727 swheader =
2728 ((SDPCM_CONTROL_CHANNEL << SDPCM_CHANNEL_SHIFT) &
2729 SDPCM_CHANNEL_MASK)
2730 | bus->tx_seq | ((doff << SDPCM_DOFFSET_SHIFT) &
2731 SDPCM_DOFFSET_MASK);
2732 put_unaligned_le32(swheader, frame + SDPCM_FRAMETAG_LEN);
2733 put_unaligned_le32(0, frame + SDPCM_FRAMETAG_LEN + sizeof(swheader));
2734
2735 if (!data_ok(bus)) {
2736 brcmf_dbg(INFO, "No bus credit bus->tx_max %d, bus->tx_seq %d\n",
2737 bus->tx_max, bus->tx_seq);
2738 bus->ctrl_frame_stat = true;
2739 /* Send from dpc */
2740 bus->ctrl_frame_buf = frame;
2741 bus->ctrl_frame_len = len;
2742
Franky Linfd67dc82012-11-05 16:22:25 -08002743 wait_event_interruptible_timeout(bus->ctrl_wait,
2744 !bus->ctrl_frame_stat,
2745 msecs_to_jiffies(2000));
Arend van Spriel5b435de2011-10-05 13:19:03 +02002746
Joe Perches23677ce2012-02-09 11:17:23 +00002747 if (!bus->ctrl_frame_stat) {
Arend van Sprielc3203372013-04-03 12:40:44 +02002748 brcmf_dbg(SDIO, "ctrl_frame_stat == false\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002749 ret = 0;
2750 } else {
Arend van Sprielc3203372013-04-03 12:40:44 +02002751 brcmf_dbg(SDIO, "ctrl_frame_stat == true\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002752 ret = -1;
2753 }
2754 }
2755
2756 if (ret == -1) {
Joe Perches1e023822012-01-15 00:38:40 -08002757 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_CTL_ON(),
2758 frame, len, "Tx Frame:\n");
2759 brcmf_dbg_hex_dump(!(BRCMF_BYTES_ON() && BRCMF_CTL_ON()) &&
2760 BRCMF_HDRS_ON(),
2761 frame, min_t(u16, len, 16), "TxHdr:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002762
2763 do {
Franky Lin38b0b0d2012-11-05 16:22:24 -08002764 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002765 ret = brcmf_tx_frame(bus, frame, len);
Franky Lin38b0b0d2012-11-05 16:22:24 -08002766 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002767 } while (ret < 0 && retries++ < TXRETRIES);
2768 }
2769
Franky Linf1e68c22012-09-13 21:12:00 +02002770 spin_lock_irqsave(&bus->dpc_tl_lock, flags);
2771 if ((bus->idletime == BRCMF_IDLE_IMMEDIATE) &&
2772 list_empty(&bus->dpc_tsklst)) {
2773 spin_unlock_irqrestore(&bus->dpc_tl_lock, flags);
2774
Arend van Spriel5b435de2011-10-05 13:19:03 +02002775 bus->activity = false;
Franky Lin38b0b0d2012-11-05 16:22:24 -08002776 sdio_claim_host(bus->sdiodev->func[1]);
Piotr Haber4a3da992013-04-11 13:28:46 +02002777 brcmf_dbg(INFO, "idle\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002778 brcmf_sdbrcm_clkctl(bus, CLK_NONE, true);
Franky Lin38b0b0d2012-11-05 16:22:24 -08002779 sdio_release_host(bus->sdiodev->func[1]);
Franky Linf1e68c22012-09-13 21:12:00 +02002780 } else {
2781 spin_unlock_irqrestore(&bus->dpc_tl_lock, flags);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002782 }
2783
Arend van Spriel5b435de2011-10-05 13:19:03 +02002784 if (ret)
Arend van Spriel80969832012-06-09 22:51:44 +02002785 bus->sdcnt.tx_ctlerrs++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002786 else
Arend van Spriel80969832012-06-09 22:51:44 +02002787 bus->sdcnt.tx_ctlpkts++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002788
2789 return ret ? -EIO : 0;
2790}
2791
Arend van Spriel80969832012-06-09 22:51:44 +02002792#ifdef DEBUG
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002793static inline bool brcmf_sdio_valid_shared_address(u32 addr)
2794{
2795 return !(addr == 0 || ((~addr >> 16) & 0xffff) == (addr & 0xffff));
2796}
2797
2798static int brcmf_sdio_readshared(struct brcmf_sdio *bus,
2799 struct sdpcm_shared *sh)
2800{
2801 u32 addr;
2802 int rv;
2803 u32 shaddr = 0;
2804 struct sdpcm_shared_le sh_le;
2805 __le32 addr_le;
2806
2807 shaddr = bus->ramsize - 4;
2808
2809 /*
2810 * Read last word in socram to determine
2811 * address of sdpcm_shared structure
2812 */
Franky Lin38b0b0d2012-11-05 16:22:24 -08002813 sdio_claim_host(bus->sdiodev->func[1]);
Piotr Haber4a3da992013-04-11 13:28:46 +02002814 brcmf_sdbrcm_bus_sleep(bus, false, false);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002815 rv = brcmf_sdbrcm_membytes(bus, false, shaddr,
2816 (u8 *)&addr_le, 4);
Piotr Haberb55de972013-02-08 12:06:33 +01002817 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002818 if (rv < 0)
2819 return rv;
2820
2821 addr = le32_to_cpu(addr_le);
2822
Arend van Sprielc3203372013-04-03 12:40:44 +02002823 brcmf_dbg(SDIO, "sdpcm_shared address 0x%08X\n", addr);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002824
2825 /*
2826 * Check if addr is valid.
2827 * NVRAM length at the end of memory should have been overwritten.
2828 */
2829 if (!brcmf_sdio_valid_shared_address(addr)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01002830 brcmf_err("invalid sdpcm_shared address 0x%08X\n",
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002831 addr);
2832 return -EINVAL;
2833 }
2834
2835 /* Read hndrte_shared structure */
2836 rv = brcmf_sdbrcm_membytes(bus, false, addr, (u8 *)&sh_le,
2837 sizeof(struct sdpcm_shared_le));
2838 if (rv < 0)
2839 return rv;
2840
2841 /* Endianness */
2842 sh->flags = le32_to_cpu(sh_le.flags);
2843 sh->trap_addr = le32_to_cpu(sh_le.trap_addr);
2844 sh->assert_exp_addr = le32_to_cpu(sh_le.assert_exp_addr);
2845 sh->assert_file_addr = le32_to_cpu(sh_le.assert_file_addr);
2846 sh->assert_line = le32_to_cpu(sh_le.assert_line);
2847 sh->console_addr = le32_to_cpu(sh_le.console_addr);
2848 sh->msgtrace_addr = le32_to_cpu(sh_le.msgtrace_addr);
2849
Piotr Haber86dcd932013-04-05 10:57:43 +02002850 if ((sh->flags & SDPCM_SHARED_VERSION_MASK) > SDPCM_SHARED_VERSION) {
2851 brcmf_err("sdpcm shared version unsupported: dhd %d dongle %d\n",
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002852 SDPCM_SHARED_VERSION,
2853 sh->flags & SDPCM_SHARED_VERSION_MASK);
2854 return -EPROTO;
2855 }
2856
2857 return 0;
2858}
2859
2860static int brcmf_sdio_dump_console(struct brcmf_sdio *bus,
2861 struct sdpcm_shared *sh, char __user *data,
2862 size_t count)
2863{
2864 u32 addr, console_ptr, console_size, console_index;
2865 char *conbuf = NULL;
2866 __le32 sh_val;
2867 int rv;
2868 loff_t pos = 0;
2869 int nbytes = 0;
2870
2871 /* obtain console information from device memory */
2872 addr = sh->console_addr + offsetof(struct rte_console, log_le);
2873 rv = brcmf_sdbrcm_membytes(bus, false, addr,
2874 (u8 *)&sh_val, sizeof(u32));
2875 if (rv < 0)
2876 return rv;
2877 console_ptr = le32_to_cpu(sh_val);
2878
2879 addr = sh->console_addr + offsetof(struct rte_console, log_le.buf_size);
2880 rv = brcmf_sdbrcm_membytes(bus, false, addr,
2881 (u8 *)&sh_val, sizeof(u32));
2882 if (rv < 0)
2883 return rv;
2884 console_size = le32_to_cpu(sh_val);
2885
2886 addr = sh->console_addr + offsetof(struct rte_console, log_le.idx);
2887 rv = brcmf_sdbrcm_membytes(bus, false, addr,
2888 (u8 *)&sh_val, sizeof(u32));
2889 if (rv < 0)
2890 return rv;
2891 console_index = le32_to_cpu(sh_val);
2892
2893 /* allocate buffer for console data */
2894 if (console_size <= CONSOLE_BUFFER_MAX)
2895 conbuf = vzalloc(console_size+1);
2896
2897 if (!conbuf)
2898 return -ENOMEM;
2899
2900 /* obtain the console data from device */
2901 conbuf[console_size] = '\0';
2902 rv = brcmf_sdbrcm_membytes(bus, false, console_ptr, (u8 *)conbuf,
2903 console_size);
2904 if (rv < 0)
2905 goto done;
2906
2907 rv = simple_read_from_buffer(data, count, &pos,
2908 conbuf + console_index,
2909 console_size - console_index);
2910 if (rv < 0)
2911 goto done;
2912
2913 nbytes = rv;
2914 if (console_index > 0) {
2915 pos = 0;
2916 rv = simple_read_from_buffer(data+nbytes, count, &pos,
2917 conbuf, console_index - 1);
2918 if (rv < 0)
2919 goto done;
2920 rv += nbytes;
2921 }
2922done:
2923 vfree(conbuf);
2924 return rv;
2925}
2926
2927static int brcmf_sdio_trap_info(struct brcmf_sdio *bus, struct sdpcm_shared *sh,
2928 char __user *data, size_t count)
2929{
2930 int error, res;
2931 char buf[350];
2932 struct brcmf_trap_info tr;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002933 loff_t pos = 0;
2934
Piotr Haberbaa9e602013-04-05 10:57:42 +02002935 if ((sh->flags & SDPCM_SHARED_TRAP) == 0) {
2936 brcmf_dbg(INFO, "no trap in firmware\n");
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002937 return 0;
Piotr Haberbaa9e602013-04-05 10:57:42 +02002938 }
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002939
2940 error = brcmf_sdbrcm_membytes(bus, false, sh->trap_addr, (u8 *)&tr,
2941 sizeof(struct brcmf_trap_info));
2942 if (error < 0)
2943 return error;
2944
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002945 res = scnprintf(buf, sizeof(buf),
2946 "dongle trap info: type 0x%x @ epc 0x%08x\n"
2947 " cpsr 0x%08x spsr 0x%08x sp 0x%08x\n"
2948 " lr 0x%08x pc 0x%08x offset 0x%x\n"
2949 " r0 0x%08x r1 0x%08x r2 0x%08x r3 0x%08x\n"
2950 " r4 0x%08x r5 0x%08x r6 0x%08x r7 0x%08x\n",
2951 le32_to_cpu(tr.type), le32_to_cpu(tr.epc),
2952 le32_to_cpu(tr.cpsr), le32_to_cpu(tr.spsr),
2953 le32_to_cpu(tr.r13), le32_to_cpu(tr.r14),
Arend van Spriel9bd02c62012-06-26 21:26:40 +02002954 le32_to_cpu(tr.pc), sh->trap_addr,
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002955 le32_to_cpu(tr.r0), le32_to_cpu(tr.r1),
2956 le32_to_cpu(tr.r2), le32_to_cpu(tr.r3),
2957 le32_to_cpu(tr.r4), le32_to_cpu(tr.r5),
2958 le32_to_cpu(tr.r6), le32_to_cpu(tr.r7));
2959
Piotr Haberbaa9e602013-04-05 10:57:42 +02002960 return simple_read_from_buffer(data, count, &pos, buf, res);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002961}
2962
2963static int brcmf_sdio_assert_info(struct brcmf_sdio *bus,
2964 struct sdpcm_shared *sh, char __user *data,
2965 size_t count)
2966{
2967 int error = 0;
2968 char buf[200];
2969 char file[80] = "?";
2970 char expr[80] = "<???>";
2971 int res;
2972 loff_t pos = 0;
2973
2974 if ((sh->flags & SDPCM_SHARED_ASSERT_BUILT) == 0) {
2975 brcmf_dbg(INFO, "firmware not built with -assert\n");
2976 return 0;
2977 } else if ((sh->flags & SDPCM_SHARED_ASSERT) == 0) {
2978 brcmf_dbg(INFO, "no assert in dongle\n");
2979 return 0;
2980 }
2981
Franky Lin38b0b0d2012-11-05 16:22:24 -08002982 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002983 if (sh->assert_file_addr != 0) {
2984 error = brcmf_sdbrcm_membytes(bus, false, sh->assert_file_addr,
2985 (u8 *)file, 80);
2986 if (error < 0)
2987 return error;
2988 }
2989 if (sh->assert_exp_addr != 0) {
2990 error = brcmf_sdbrcm_membytes(bus, false, sh->assert_exp_addr,
2991 (u8 *)expr, 80);
2992 if (error < 0)
2993 return error;
2994 }
Franky Lin38b0b0d2012-11-05 16:22:24 -08002995 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02002996
2997 res = scnprintf(buf, sizeof(buf),
2998 "dongle assert: %s:%d: assert(%s)\n",
2999 file, sh->assert_line, expr);
3000 return simple_read_from_buffer(data, count, &pos, buf, res);
3001}
3002
3003static int brcmf_sdbrcm_checkdied(struct brcmf_sdio *bus)
3004{
3005 int error;
3006 struct sdpcm_shared sh;
3007
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003008 error = brcmf_sdio_readshared(bus, &sh);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003009
3010 if (error < 0)
3011 return error;
3012
3013 if ((sh.flags & SDPCM_SHARED_ASSERT_BUILT) == 0)
3014 brcmf_dbg(INFO, "firmware not built with -assert\n");
3015 else if (sh.flags & SDPCM_SHARED_ASSERT)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003016 brcmf_err("assertion in dongle\n");
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003017
3018 if (sh.flags & SDPCM_SHARED_TRAP)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003019 brcmf_err("firmware trap in dongle\n");
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003020
3021 return 0;
3022}
3023
3024static int brcmf_sdbrcm_died_dump(struct brcmf_sdio *bus, char __user *data,
3025 size_t count, loff_t *ppos)
3026{
3027 int error = 0;
3028 struct sdpcm_shared sh;
3029 int nbytes = 0;
3030 loff_t pos = *ppos;
3031
3032 if (pos != 0)
3033 return 0;
3034
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003035 error = brcmf_sdio_readshared(bus, &sh);
3036 if (error < 0)
3037 goto done;
3038
3039 error = brcmf_sdio_assert_info(bus, &sh, data, count);
3040 if (error < 0)
3041 goto done;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003042 nbytes = error;
Piotr Haberbaa9e602013-04-05 10:57:42 +02003043
3044 error = brcmf_sdio_trap_info(bus, &sh, data+nbytes, count);
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003045 if (error < 0)
3046 goto done;
Piotr Haberbaa9e602013-04-05 10:57:42 +02003047 nbytes += error;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003048
Piotr Haberbaa9e602013-04-05 10:57:42 +02003049 error = brcmf_sdio_dump_console(bus, &sh, data+nbytes, count);
3050 if (error < 0)
3051 goto done;
3052 nbytes += error;
3053
3054 error = nbytes;
3055 *ppos += nbytes;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003056done:
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003057 return error;
3058}
3059
3060static ssize_t brcmf_sdio_forensic_read(struct file *f, char __user *data,
3061 size_t count, loff_t *ppos)
3062{
3063 struct brcmf_sdio *bus = f->private_data;
3064 int res;
3065
3066 res = brcmf_sdbrcm_died_dump(bus, data, count, ppos);
3067 if (res > 0)
3068 *ppos += res;
3069 return (ssize_t)res;
3070}
3071
3072static const struct file_operations brcmf_sdio_forensic_ops = {
3073 .owner = THIS_MODULE,
3074 .open = simple_open,
3075 .read = brcmf_sdio_forensic_read
3076};
3077
Arend van Spriel80969832012-06-09 22:51:44 +02003078static void brcmf_sdio_debugfs_create(struct brcmf_sdio *bus)
3079{
3080 struct brcmf_pub *drvr = bus->sdiodev->bus_if->drvr;
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003081 struct dentry *dentry = brcmf_debugfs_get_devdir(drvr);
Arend van Spriel80969832012-06-09 22:51:44 +02003082
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003083 if (IS_ERR_OR_NULL(dentry))
3084 return;
3085
3086 debugfs_create_file("forensics", S_IRUGO, dentry, bus,
3087 &brcmf_sdio_forensic_ops);
Arend van Spriel80969832012-06-09 22:51:44 +02003088 brcmf_debugfs_create_sdio_count(drvr, &bus->sdcnt);
3089}
3090#else
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003091static int brcmf_sdbrcm_checkdied(struct brcmf_sdio *bus)
3092{
3093 return 0;
3094}
3095
Arend van Spriel80969832012-06-09 22:51:44 +02003096static void brcmf_sdio_debugfs_create(struct brcmf_sdio *bus)
3097{
3098}
3099#endif /* DEBUG */
3100
Franky Linfcf094f2011-12-16 18:37:16 -08003101static int
Franky Lin532cdd32011-11-22 17:21:54 -08003102brcmf_sdbrcm_bus_rxctl(struct device *dev, unsigned char *msg, uint msglen)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003103{
3104 int timeleft;
3105 uint rxlen = 0;
3106 bool pending;
Franky Lindd43a012012-11-05 16:22:22 -08003107 u8 *buf;
Franky Lin532cdd32011-11-22 17:21:54 -08003108 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01003109 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Lin532cdd32011-11-22 17:21:54 -08003110 struct brcmf_sdio *bus = sdiodev->bus;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003111
3112 brcmf_dbg(TRACE, "Enter\n");
3113
3114 /* Wait until control frame is available */
3115 timeleft = brcmf_sdbrcm_dcmd_resp_wait(bus, &bus->rxlen, &pending);
3116
Franky Lindd43a012012-11-05 16:22:22 -08003117 spin_lock_bh(&bus->rxctl_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003118 rxlen = bus->rxlen;
3119 memcpy(msg, bus->rxctl, min(msglen, rxlen));
Franky Lindd43a012012-11-05 16:22:22 -08003120 bus->rxctl = NULL;
3121 buf = bus->rxctl_orig;
3122 bus->rxctl_orig = NULL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003123 bus->rxlen = 0;
Franky Lindd43a012012-11-05 16:22:22 -08003124 spin_unlock_bh(&bus->rxctl_lock);
3125 vfree(buf);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003126
3127 if (rxlen) {
3128 brcmf_dbg(CTL, "resumed on rxctl frame, got %d expected %d\n",
3129 rxlen, msglen);
3130 } else if (timeleft == 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003131 brcmf_err("resumed on timeout\n");
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003132 brcmf_sdbrcm_checkdied(bus);
Joe Perches23677ce2012-02-09 11:17:23 +00003133 } else if (pending) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003134 brcmf_dbg(CTL, "cancelled\n");
3135 return -ERESTARTSYS;
3136 } else {
3137 brcmf_dbg(CTL, "resumed for unknown reason?\n");
Arend van Spriel4fc0d012012-06-14 12:16:57 +02003138 brcmf_sdbrcm_checkdied(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003139 }
3140
3141 if (rxlen)
Arend van Spriel80969832012-06-09 22:51:44 +02003142 bus->sdcnt.rx_ctlpkts++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003143 else
Arend van Spriel80969832012-06-09 22:51:44 +02003144 bus->sdcnt.rx_ctlerrs++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003145
3146 return rxlen ? (int)rxlen : -ETIMEDOUT;
3147}
3148
Franky Line92eedf2011-11-22 17:21:50 -08003149static int brcmf_sdbrcm_write_vars(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003150{
3151 int bcmerror = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003152 u32 varaddr;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003153 u32 varsizew;
3154 __le32 varsizew_le;
Joe Perches8ae74652012-01-15 00:38:38 -08003155#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02003156 char *nvram_ularray;
Joe Perches8ae74652012-01-15 00:38:38 -08003157#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02003158
3159 /* Even if there are no vars are to be written, we still
3160 need to set the ramsize. */
Arend van Spriel6d4ef682012-06-26 21:26:38 +02003161 varaddr = (bus->ramsize - 4) - bus->varsz;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003162
3163 if (bus->vars) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003164 /* Write the vars list */
Arend van Spriel6d4ef682012-06-26 21:26:38 +02003165 bcmerror = brcmf_sdbrcm_membytes(bus, true, varaddr,
3166 bus->vars, bus->varsz);
Joe Perches8ae74652012-01-15 00:38:38 -08003167#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02003168 /* Verify NVRAM bytes */
Arend van Spriel6d4ef682012-06-26 21:26:38 +02003169 brcmf_dbg(INFO, "Compare NVRAM dl & ul; varsize=%d\n",
3170 bus->varsz);
3171 nvram_ularray = kmalloc(bus->varsz, GFP_ATOMIC);
3172 if (!nvram_ularray)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003173 return -ENOMEM;
3174
3175 /* Upload image to verify downloaded contents. */
Arend van Spriel6d4ef682012-06-26 21:26:38 +02003176 memset(nvram_ularray, 0xaa, bus->varsz);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003177
3178 /* Read the vars list to temp buffer for comparison */
Arend van Spriel6d4ef682012-06-26 21:26:38 +02003179 bcmerror = brcmf_sdbrcm_membytes(bus, false, varaddr,
3180 nvram_ularray, bus->varsz);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003181 if (bcmerror) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003182 brcmf_err("error %d on reading %d nvram bytes at 0x%08x\n",
Arend van Spriel6d4ef682012-06-26 21:26:38 +02003183 bcmerror, bus->varsz, varaddr);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003184 }
3185 /* Compare the org NVRAM with the one read from RAM */
Arend van Spriel6d4ef682012-06-26 21:26:38 +02003186 if (memcmp(bus->vars, nvram_ularray, bus->varsz))
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003187 brcmf_err("Downloaded NVRAM image is corrupted\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003188 else
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003189 brcmf_err("Download/Upload/Compare of NVRAM ok\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003190
3191 kfree(nvram_ularray);
Joe Perches8ae74652012-01-15 00:38:38 -08003192#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02003193 }
3194
3195 /* adjust to the user specified RAM */
3196 brcmf_dbg(INFO, "Physical memory size: %d\n", bus->ramsize);
3197 brcmf_dbg(INFO, "Vars are at %d, orig varsize is %d\n",
Arend van Spriel6d4ef682012-06-26 21:26:38 +02003198 varaddr, bus->varsz);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003199
3200 /*
3201 * Determine the length token:
3202 * Varsize, converted to words, in lower 16-bits, checksum
3203 * in upper 16-bits.
3204 */
3205 if (bcmerror) {
3206 varsizew = 0;
3207 varsizew_le = cpu_to_le32(0);
3208 } else {
Arend van Spriel6d4ef682012-06-26 21:26:38 +02003209 varsizew = bus->varsz / 4;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003210 varsizew = (~varsizew << 16) | (varsizew & 0x0000FFFF);
3211 varsizew_le = cpu_to_le32(varsizew);
3212 }
3213
3214 brcmf_dbg(INFO, "New varsize is %d, length token=0x%08x\n",
Arend van Spriel6d4ef682012-06-26 21:26:38 +02003215 bus->varsz, varsizew);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003216
3217 /* Write the length token to the last word */
3218 bcmerror = brcmf_sdbrcm_membytes(bus, true, (bus->ramsize - 4),
3219 (u8 *)&varsizew_le, 4);
3220
3221 return bcmerror;
3222}
3223
Franky Line92eedf2011-11-22 17:21:50 -08003224static int brcmf_sdbrcm_download_state(struct brcmf_sdio *bus, bool enter)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003225{
Arend van Spriel5b435de2011-10-05 13:19:03 +02003226 int bcmerror = 0;
Franky Lin99ba15c2011-11-04 22:23:42 +01003227 struct chip_info *ci = bus->ci;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003228
3229 /* To enter download state, disable ARM and reset SOCRAM.
3230 * To exit download state, simply reset ARM (default is RAM boot).
3231 */
3232 if (enter) {
3233 bus->alp_only = true;
3234
Franky Lin086a2e02011-11-10 20:30:23 +01003235 ci->coredisable(bus->sdiodev, ci, BCMA_CORE_ARM_CM3);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003236
Franky Lind77e70f2011-11-10 20:30:24 +01003237 ci->resetcore(bus->sdiodev, ci, BCMA_CORE_INTERNAL_MEM);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003238
3239 /* Clear the top bit of memory */
3240 if (bus->ramsize) {
3241 u32 zeros = 0;
3242 brcmf_sdbrcm_membytes(bus, true, bus->ramsize - 4,
3243 (u8 *)&zeros, 4);
3244 }
3245 } else {
Franky Lin6ca687d2011-11-10 20:30:21 +01003246 if (!ci->iscoreup(bus->sdiodev, ci, BCMA_CORE_INTERNAL_MEM)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003247 brcmf_err("SOCRAM core is down after reset?\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003248 bcmerror = -EBADE;
3249 goto fail;
3250 }
3251
3252 bcmerror = brcmf_sdbrcm_write_vars(bus);
3253 if (bcmerror) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003254 brcmf_err("no vars written to RAM\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003255 bcmerror = 0;
3256 }
3257
3258 w_sdreg32(bus, 0xFFFFFFFF,
Franky Lin58692752012-05-04 18:27:36 -07003259 offsetof(struct sdpcmd_regs, intstatus));
Arend van Spriel5b435de2011-10-05 13:19:03 +02003260
Franky Lind77e70f2011-11-10 20:30:24 +01003261 ci->resetcore(bus->sdiodev, ci, BCMA_CORE_ARM_CM3);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003262
3263 /* Allow HT Clock now that the ARM is running. */
3264 bus->alp_only = false;
3265
Franky Lin712ac5b2011-12-16 18:37:09 -08003266 bus->sdiodev->bus_if->state = BRCMF_BUS_LOAD;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003267 }
3268fail:
3269 return bcmerror;
3270}
3271
Franky Line92eedf2011-11-22 17:21:50 -08003272static int brcmf_sdbrcm_get_image(char *buf, int len, struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003273{
3274 if (bus->firmware->size < bus->fw_ptr + len)
3275 len = bus->firmware->size - bus->fw_ptr;
3276
3277 memcpy(buf, &bus->firmware->data[bus->fw_ptr], len);
3278 bus->fw_ptr += len;
3279 return len;
3280}
3281
Franky Line92eedf2011-11-22 17:21:50 -08003282static int brcmf_sdbrcm_download_code_file(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003283{
3284 int offset = 0;
3285 uint len;
3286 u8 *memblock = NULL, *memptr;
3287 int ret;
3288
3289 brcmf_dbg(INFO, "Enter\n");
3290
Arend van Spriel52e14092012-02-09 21:09:09 +01003291 ret = request_firmware(&bus->firmware, BRCMF_SDIO_FW_NAME,
Arend van Spriel5b435de2011-10-05 13:19:03 +02003292 &bus->sdiodev->func[2]->dev);
3293 if (ret) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003294 brcmf_err("Fail to request firmware %d\n", ret);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003295 return ret;
3296 }
3297 bus->fw_ptr = 0;
3298
3299 memptr = memblock = kmalloc(MEMBLOCK + BRCMF_SDALIGN, GFP_ATOMIC);
3300 if (memblock == NULL) {
3301 ret = -ENOMEM;
3302 goto err;
3303 }
3304 if ((u32)(unsigned long)memblock % BRCMF_SDALIGN)
3305 memptr += (BRCMF_SDALIGN -
3306 ((u32)(unsigned long)memblock % BRCMF_SDALIGN));
3307
3308 /* Download image */
3309 while ((len =
3310 brcmf_sdbrcm_get_image((char *)memptr, MEMBLOCK, bus))) {
3311 ret = brcmf_sdbrcm_membytes(bus, true, offset, memptr, len);
3312 if (ret) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003313 brcmf_err("error %d on writing %d membytes at 0x%08x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02003314 ret, MEMBLOCK, offset);
3315 goto err;
3316 }
3317
3318 offset += MEMBLOCK;
3319 }
3320
3321err:
3322 kfree(memblock);
3323
3324 release_firmware(bus->firmware);
3325 bus->fw_ptr = 0;
3326
3327 return ret;
3328}
3329
3330/*
3331 * ProcessVars:Takes a buffer of "<var>=<value>\n" lines read from a file
3332 * and ending in a NUL.
3333 * Removes carriage returns, empty lines, comment lines, and converts
3334 * newlines to NULs.
3335 * Shortens buffer as needed and pads with NULs. End of buffer is marked
3336 * by two NULs.
3337*/
3338
Franky Lind610cde2012-06-26 21:26:37 +02003339static int brcmf_process_nvram_vars(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003340{
Franky Lind610cde2012-06-26 21:26:37 +02003341 char *varbuf;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003342 char *dp;
3343 bool findNewline;
3344 int column;
Franky Lind610cde2012-06-26 21:26:37 +02003345 int ret = 0;
3346 uint buf_len, n, len;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003347
Franky Lind610cde2012-06-26 21:26:37 +02003348 len = bus->firmware->size;
3349 varbuf = vmalloc(len);
3350 if (!varbuf)
3351 return -ENOMEM;
3352
3353 memcpy(varbuf, bus->firmware->data, len);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003354 dp = varbuf;
3355
3356 findNewline = false;
3357 column = 0;
3358
3359 for (n = 0; n < len; n++) {
3360 if (varbuf[n] == 0)
3361 break;
3362 if (varbuf[n] == '\r')
3363 continue;
3364 if (findNewline && varbuf[n] != '\n')
3365 continue;
3366 findNewline = false;
3367 if (varbuf[n] == '#') {
3368 findNewline = true;
3369 continue;
3370 }
3371 if (varbuf[n] == '\n') {
3372 if (column == 0)
3373 continue;
3374 *dp++ = 0;
3375 column = 0;
3376 continue;
3377 }
3378 *dp++ = varbuf[n];
3379 column++;
3380 }
3381 buf_len = dp - varbuf;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003382 while (dp < varbuf + n)
3383 *dp++ = 0;
3384
Franky Lind610cde2012-06-26 21:26:37 +02003385 kfree(bus->vars);
Arend van Spriel6d4ef682012-06-26 21:26:38 +02003386 /* roundup needed for download to device */
3387 bus->varsz = roundup(buf_len + 1, 4);
Franky Lind610cde2012-06-26 21:26:37 +02003388 bus->vars = kmalloc(bus->varsz, GFP_KERNEL);
3389 if (bus->vars == NULL) {
3390 bus->varsz = 0;
3391 ret = -ENOMEM;
3392 goto err;
3393 }
3394
3395 /* copy the processed variables and add null termination */
3396 memcpy(bus->vars, varbuf, buf_len);
3397 bus->vars[buf_len] = 0;
3398err:
3399 vfree(varbuf);
3400 return ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003401}
3402
Franky Line92eedf2011-11-22 17:21:50 -08003403static int brcmf_sdbrcm_download_nvram(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003404{
Arend van Spriel5b435de2011-10-05 13:19:03 +02003405 int ret;
3406
Arend van Spriel52e14092012-02-09 21:09:09 +01003407 ret = request_firmware(&bus->firmware, BRCMF_SDIO_NV_NAME,
Arend van Spriel5b435de2011-10-05 13:19:03 +02003408 &bus->sdiodev->func[2]->dev);
3409 if (ret) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003410 brcmf_err("Fail to request nvram %d\n", ret);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003411 return ret;
3412 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02003413
Franky Lind610cde2012-06-26 21:26:37 +02003414 ret = brcmf_process_nvram_vars(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003415
3416 release_firmware(bus->firmware);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003417
3418 return ret;
3419}
3420
Franky Line92eedf2011-11-22 17:21:50 -08003421static int _brcmf_sdbrcm_download_firmware(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003422{
3423 int bcmerror = -1;
3424
3425 /* Keep arm in reset */
3426 if (brcmf_sdbrcm_download_state(bus, true)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003427 brcmf_err("error placing ARM core in reset\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003428 goto err;
3429 }
3430
Arend van Spriel5b435de2011-10-05 13:19:03 +02003431 if (brcmf_sdbrcm_download_code_file(bus)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003432 brcmf_err("dongle image file download failed\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003433 goto err;
3434 }
3435
Franky Lin3eaa9562013-04-02 21:06:20 +02003436 if (brcmf_sdbrcm_download_nvram(bus)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003437 brcmf_err("dongle nvram file download failed\n");
Franky Lin3eaa9562013-04-02 21:06:20 +02003438 goto err;
3439 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02003440
3441 /* Take arm out of reset */
3442 if (brcmf_sdbrcm_download_state(bus, false)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003443 brcmf_err("error getting out of ARM core reset\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003444 goto err;
3445 }
3446
3447 bcmerror = 0;
3448
3449err:
3450 return bcmerror;
3451}
3452
Piotr Haber4a3da992013-04-11 13:28:46 +02003453static bool brcmf_sdbrcm_sr_capable(struct brcmf_sdio *bus)
3454{
3455 u32 addr, reg;
3456
3457 brcmf_dbg(TRACE, "Enter\n");
3458
3459 /* old chips with PMU version less than 17 don't support save restore */
3460 if (bus->ci->pmurev < 17)
3461 return false;
3462
3463 /* read PMU chipcontrol register 3*/
3464 addr = CORE_CC_REG(bus->ci->c_inf[0].base, chipcontrol_addr);
3465 brcmf_sdio_regwl(bus->sdiodev, addr, 3, NULL);
3466 addr = CORE_CC_REG(bus->ci->c_inf[0].base, chipcontrol_data);
3467 reg = brcmf_sdio_regrl(bus->sdiodev, addr, NULL);
3468
3469 return (bool)reg;
3470}
3471
3472static void brcmf_sdbrcm_sr_init(struct brcmf_sdio *bus)
3473{
3474 int err = 0;
3475 u8 val;
3476
3477 brcmf_dbg(TRACE, "Enter\n");
3478
3479 val = brcmf_sdio_regrb(bus->sdiodev, SBSDIO_FUNC1_WAKEUPCTRL,
3480 &err);
3481 if (err) {
3482 brcmf_err("error reading SBSDIO_FUNC1_WAKEUPCTRL\n");
3483 return;
3484 }
3485
3486 val |= 1 << SBSDIO_FUNC1_WCTRL_HTWAIT_SHIFT;
3487 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_WAKEUPCTRL,
3488 val, &err);
3489 if (err) {
3490 brcmf_err("error writing SBSDIO_FUNC1_WAKEUPCTRL\n");
3491 return;
3492 }
3493
3494 /* Add CMD14 Support */
3495 brcmf_sdio_regwb(bus->sdiodev, SDIO_CCCR_BRCM_CARDCAP,
3496 (SDIO_CCCR_BRCM_CARDCAP_CMD14_SUPPORT |
3497 SDIO_CCCR_BRCM_CARDCAP_CMD14_EXT),
3498 &err);
3499 if (err) {
3500 brcmf_err("error writing SDIO_CCCR_BRCM_CARDCAP\n");
3501 return;
3502 }
3503
3504 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3505 SBSDIO_FORCE_HT, &err);
3506 if (err) {
3507 brcmf_err("error writing SBSDIO_FUNC1_CHIPCLKCSR\n");
3508 return;
3509 }
3510
3511 /* set flag */
3512 bus->sr_enabled = true;
3513 brcmf_dbg(INFO, "SR enabled\n");
3514}
3515
3516/* enable KSO bit */
3517static int brcmf_sdbrcm_kso_init(struct brcmf_sdio *bus)
3518{
3519 u8 val;
3520 int err = 0;
3521
3522 brcmf_dbg(TRACE, "Enter\n");
3523
3524 /* KSO bit added in SDIO core rev 12 */
3525 if (bus->ci->c_inf[1].rev < 12)
3526 return 0;
3527
3528 val = brcmf_sdio_regrb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
3529 &err);
3530 if (err) {
3531 brcmf_err("error reading SBSDIO_FUNC1_SLEEPCSR\n");
3532 return err;
3533 }
3534
3535 if (!(val & SBSDIO_FUNC1_SLEEPCSR_KSO_MASK)) {
3536 val |= (SBSDIO_FUNC1_SLEEPCSR_KSO_EN <<
3537 SBSDIO_FUNC1_SLEEPCSR_KSO_SHIFT);
3538 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
3539 val, &err);
3540 if (err) {
3541 brcmf_err("error writing SBSDIO_FUNC1_SLEEPCSR\n");
3542 return err;
3543 }
3544 }
3545
3546 return 0;
3547}
3548
3549
Arend van Spriel5b435de2011-10-05 13:19:03 +02003550static bool
Franky Line92eedf2011-11-22 17:21:50 -08003551brcmf_sdbrcm_download_firmware(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003552{
3553 bool ret;
3554
Franky Lin38b0b0d2012-11-05 16:22:24 -08003555 sdio_claim_host(bus->sdiodev->func[1]);
3556
Arend van Spriel5b435de2011-10-05 13:19:03 +02003557 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
3558
3559 ret = _brcmf_sdbrcm_download_firmware(bus) == 0;
3560
3561 brcmf_sdbrcm_clkctl(bus, CLK_SDONLY, false);
3562
Franky Lin38b0b0d2012-11-05 16:22:24 -08003563 sdio_release_host(bus->sdiodev->func[1]);
3564
Arend van Spriel5b435de2011-10-05 13:19:03 +02003565 return ret;
3566}
3567
Franky Lin99a0b8f2011-12-16 18:37:14 -08003568static int brcmf_sdbrcm_bus_init(struct device *dev)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003569{
Franky Linfa20b912011-11-22 17:21:57 -08003570 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01003571 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Linfa20b912011-11-22 17:21:57 -08003572 struct brcmf_sdio *bus = sdiodev->bus;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003573 unsigned long timeout;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003574 u8 ready, enable;
3575 int err, ret = 0;
3576 u8 saveclk;
3577
3578 brcmf_dbg(TRACE, "Enter\n");
3579
3580 /* try to download image and nvram to the dongle */
Franky Linfa20b912011-11-22 17:21:57 -08003581 if (bus_if->state == BRCMF_BUS_DOWN) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003582 if (!(brcmf_sdbrcm_download_firmware(bus)))
3583 return -1;
3584 }
3585
Franky Lin712ac5b2011-12-16 18:37:09 -08003586 if (!bus->sdiodev->bus_if->drvr)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003587 return 0;
3588
3589 /* Start the watchdog timer */
Arend van Spriel80969832012-06-09 22:51:44 +02003590 bus->sdcnt.tickcnt = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003591 brcmf_sdbrcm_wd_timer(bus, BRCMF_WD_POLL_MS);
3592
Franky Lin38b0b0d2012-11-05 16:22:24 -08003593 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003594
3595 /* Make sure backplane clock is on, needed to generate F2 interrupt */
3596 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
3597 if (bus->clkstate != CLK_AVAIL)
3598 goto exit;
3599
3600 /* Force clocks on backplane to be sure F2 interrupt propagates */
Franky Lin45db3392012-05-04 18:27:32 -07003601 saveclk = brcmf_sdio_regrb(bus->sdiodev,
3602 SBSDIO_FUNC1_CHIPCLKCSR, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003603 if (!err) {
Franky Lin3bba8292012-05-04 18:27:33 -07003604 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3605 (saveclk | SBSDIO_FORCE_HT), &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003606 }
3607 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003608 brcmf_err("Failed to force clock for F2: err %d\n", err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003609 goto exit;
3610 }
3611
3612 /* Enable function 2 (frame transfers) */
3613 w_sdreg32(bus, SDPCM_PROT_VERSION << SMB_DATA_VERSION_SHIFT,
Franky Lin58692752012-05-04 18:27:36 -07003614 offsetof(struct sdpcmd_regs, tosbmailboxdata));
Arend van Spriel5b435de2011-10-05 13:19:03 +02003615 enable = (SDIO_FUNC_ENABLE_1 | SDIO_FUNC_ENABLE_2);
3616
Franky Lin3bba8292012-05-04 18:27:33 -07003617 brcmf_sdio_regwb(bus->sdiodev, SDIO_CCCR_IOEx, enable, NULL);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003618
3619 timeout = jiffies + msecs_to_jiffies(BRCMF_WAIT_F2RDY);
3620 ready = 0;
3621 while (enable != ready) {
Franky Lin45db3392012-05-04 18:27:32 -07003622 ready = brcmf_sdio_regrb(bus->sdiodev,
3623 SDIO_CCCR_IORx, NULL);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003624 if (time_after(jiffies, timeout))
3625 break;
3626 else if (time_after(jiffies, timeout - BRCMF_WAIT_F2RDY + 50))
3627 /* prevent busy waiting if it takes too long */
3628 msleep_interruptible(20);
3629 }
3630
3631 brcmf_dbg(INFO, "enable 0x%02x, ready 0x%02x\n", enable, ready);
3632
3633 /* If F2 successfully enabled, set core and enable interrupts */
3634 if (ready == enable) {
3635 /* Set up the interrupt mask and enable interrupts */
3636 bus->hostintmask = HOSTINTMASK;
3637 w_sdreg32(bus, bus->hostintmask,
Franky Lin58692752012-05-04 18:27:36 -07003638 offsetof(struct sdpcmd_regs, hostintmask));
Arend van Spriel5b435de2011-10-05 13:19:03 +02003639
Franky Lin3bba8292012-05-04 18:27:33 -07003640 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_WATERMARK, 8, &err);
Arend van Sprielc0e89f02012-02-09 21:09:04 +01003641 } else {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003642 /* Disable F2 again */
3643 enable = SDIO_FUNC_ENABLE_1;
Franky Lin3bba8292012-05-04 18:27:33 -07003644 brcmf_sdio_regwb(bus->sdiodev, SDIO_CCCR_IOEx, enable, NULL);
Arend van Sprielc0e89f02012-02-09 21:09:04 +01003645 ret = -ENODEV;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003646 }
3647
Piotr Haber4a3da992013-04-11 13:28:46 +02003648 if (brcmf_sdbrcm_sr_capable(bus)) {
3649 brcmf_sdbrcm_sr_init(bus);
3650 } else {
3651 /* Restore previous clock setting */
3652 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3653 saveclk, &err);
3654 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02003655
Franky Line2f93cc2012-04-27 18:56:58 -07003656 if (ret == 0) {
Franky Linba89bf12012-04-27 18:56:59 -07003657 ret = brcmf_sdio_intr_register(bus->sdiodev);
Franky Line2f93cc2012-04-27 18:56:58 -07003658 if (ret != 0)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003659 brcmf_err("intr register failed:%d\n", ret);
Franky Line2f93cc2012-04-27 18:56:58 -07003660 }
3661
Arend van Spriel5b435de2011-10-05 13:19:03 +02003662 /* If we didn't come up, turn off backplane clock */
Franky Lind9126e02012-04-27 18:56:57 -07003663 if (bus_if->state != BRCMF_BUS_DATA)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003664 brcmf_sdbrcm_clkctl(bus, CLK_NONE, false);
3665
3666exit:
Franky Lin38b0b0d2012-11-05 16:22:24 -08003667 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003668
3669 return ret;
3670}
3671
3672void brcmf_sdbrcm_isr(void *arg)
3673{
Franky Line92eedf2011-11-22 17:21:50 -08003674 struct brcmf_sdio *bus = (struct brcmf_sdio *) arg;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003675
3676 brcmf_dbg(TRACE, "Enter\n");
3677
3678 if (!bus) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003679 brcmf_err("bus is null pointer, exiting\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003680 return;
3681 }
3682
Franky Lin712ac5b2011-12-16 18:37:09 -08003683 if (bus->sdiodev->bus_if->state == BRCMF_BUS_DOWN) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003684 brcmf_err("bus is down. we have nothing to do\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003685 return;
3686 }
3687 /* Count the interrupt call */
Arend van Spriel80969832012-06-09 22:51:44 +02003688 bus->sdcnt.intrcount++;
Franky Lin45316032012-09-13 21:12:03 +02003689 if (in_interrupt())
3690 atomic_set(&bus->ipend, 1);
3691 else
3692 if (brcmf_sdio_intr_rstatus(bus)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003693 brcmf_err("failed backplane access\n");
Franky Lin45316032012-09-13 21:12:03 +02003694 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
3695 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02003696
Arend van Spriel5b435de2011-10-05 13:19:03 +02003697 /* Disable additional interrupts (is this needed now)? */
3698 if (!bus->intr)
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003699 brcmf_err("isr w/o interrupt configured!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003700
Franky Linf1e68c22012-09-13 21:12:00 +02003701 brcmf_sdbrcm_adddpctsk(bus);
3702 queue_work(bus->brcmf_wq, &bus->datawork);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003703}
3704
Franky Lincad2b262011-11-22 17:21:53 -08003705static bool brcmf_sdbrcm_bus_watchdog(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003706{
Joe Perches8ae74652012-01-15 00:38:38 -08003707#ifdef DEBUG
Franky Lincad2b262011-11-22 17:21:53 -08003708 struct brcmf_bus *bus_if = dev_get_drvdata(bus->sdiodev->dev);
Joe Perches8ae74652012-01-15 00:38:38 -08003709#endif /* DEBUG */
Franky Linf1e68c22012-09-13 21:12:00 +02003710 unsigned long flags;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003711
3712 brcmf_dbg(TIMER, "Enter\n");
3713
Arend van Spriel5b435de2011-10-05 13:19:03 +02003714 /* Poll period: check device if appropriate. */
Piotr Haber4a3da992013-04-11 13:28:46 +02003715 if (!bus->sr_enabled &&
3716 bus->poll && (++bus->polltick >= bus->pollrate)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003717 u32 intstatus = 0;
3718
3719 /* Reset poll tick */
3720 bus->polltick = 0;
3721
3722 /* Check device if no interrupts */
Arend van Spriel80969832012-06-09 22:51:44 +02003723 if (!bus->intr ||
3724 (bus->sdcnt.intrcount == bus->sdcnt.lastintrs)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003725
Franky Linf1e68c22012-09-13 21:12:00 +02003726 spin_lock_irqsave(&bus->dpc_tl_lock, flags);
3727 if (list_empty(&bus->dpc_tsklst)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003728 u8 devpend;
Franky Linf1e68c22012-09-13 21:12:00 +02003729 spin_unlock_irqrestore(&bus->dpc_tl_lock,
3730 flags);
Franky Lin38b0b0d2012-11-05 16:22:24 -08003731 sdio_claim_host(bus->sdiodev->func[1]);
Franky Lin45db3392012-05-04 18:27:32 -07003732 devpend = brcmf_sdio_regrb(bus->sdiodev,
3733 SDIO_CCCR_INTx,
3734 NULL);
Franky Lin38b0b0d2012-11-05 16:22:24 -08003735 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003736 intstatus =
3737 devpend & (INTR_STATUS_FUNC1 |
3738 INTR_STATUS_FUNC2);
Franky Linf1e68c22012-09-13 21:12:00 +02003739 } else {
3740 spin_unlock_irqrestore(&bus->dpc_tl_lock,
3741 flags);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003742 }
3743
3744 /* If there is something, make like the ISR and
3745 schedule the DPC */
3746 if (intstatus) {
Arend van Spriel80969832012-06-09 22:51:44 +02003747 bus->sdcnt.pollcnt++;
Franky Lin1d382272012-09-13 21:11:59 +02003748 atomic_set(&bus->ipend, 1);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003749
Franky Linf1e68c22012-09-13 21:12:00 +02003750 brcmf_sdbrcm_adddpctsk(bus);
3751 queue_work(bus->brcmf_wq, &bus->datawork);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003752 }
3753 }
3754
3755 /* Update interrupt tracking */
Arend van Spriel80969832012-06-09 22:51:44 +02003756 bus->sdcnt.lastintrs = bus->sdcnt.intrcount;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003757 }
Joe Perches8ae74652012-01-15 00:38:38 -08003758#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02003759 /* Poll for console output periodically */
Hante Meuleman2def5c12012-11-14 18:46:19 -08003760 if (bus_if && bus_if->state == BRCMF_BUS_DATA &&
Franky Lin8d169aa2011-11-22 17:21:52 -08003761 bus->console_interval != 0) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003762 bus->console.count += BRCMF_WD_POLL_MS;
3763 if (bus->console.count >= bus->console_interval) {
3764 bus->console.count -= bus->console_interval;
Franky Lin38b0b0d2012-11-05 16:22:24 -08003765 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003766 /* Make sure backplane clock is on */
Piotr Haber4a3da992013-04-11 13:28:46 +02003767 brcmf_sdbrcm_bus_sleep(bus, false, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003768 if (brcmf_sdbrcm_readconsole(bus) < 0)
3769 /* stop on error */
3770 bus->console_interval = 0;
Franky Lin38b0b0d2012-11-05 16:22:24 -08003771 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003772 }
3773 }
Joe Perches8ae74652012-01-15 00:38:38 -08003774#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02003775
3776 /* On idle timeout clear activity flag and/or turn off clock */
3777 if ((bus->idletime > 0) && (bus->clkstate == CLK_AVAIL)) {
3778 if (++bus->idlecount >= bus->idletime) {
3779 bus->idlecount = 0;
3780 if (bus->activity) {
3781 bus->activity = false;
3782 brcmf_sdbrcm_wd_timer(bus, BRCMF_WD_POLL_MS);
3783 } else {
Piotr Haber4a3da992013-04-11 13:28:46 +02003784 brcmf_dbg(SDIO, "idle\n");
Franky Lin38b0b0d2012-11-05 16:22:24 -08003785 sdio_claim_host(bus->sdiodev->func[1]);
Piotr Haber4a3da992013-04-11 13:28:46 +02003786 brcmf_sdbrcm_bus_sleep(bus, true, false);
Franky Lin38b0b0d2012-11-05 16:22:24 -08003787 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003788 }
3789 }
3790 }
3791
Franky Lin1d382272012-09-13 21:11:59 +02003792 return (atomic_read(&bus->ipend) > 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003793}
3794
3795static bool brcmf_sdbrcm_chipmatch(u16 chipid)
3796{
Franky Lin4a1c02c2012-08-30 19:42:59 +02003797 if (chipid == BCM43241_CHIP_ID)
3798 return true;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003799 if (chipid == BCM4329_CHIP_ID)
3800 return true;
Franky Lince2d7d72011-12-08 15:06:39 -08003801 if (chipid == BCM4330_CHIP_ID)
3802 return true;
Franky Lin85a4a1c2012-06-26 21:26:39 +02003803 if (chipid == BCM4334_CHIP_ID)
3804 return true;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003805 return false;
3806}
3807
Franky Linf1e68c22012-09-13 21:12:00 +02003808static void brcmf_sdio_dataworker(struct work_struct *work)
3809{
3810 struct brcmf_sdio *bus = container_of(work, struct brcmf_sdio,
3811 datawork);
3812 struct list_head *cur_hd, *tmp_hd;
3813 unsigned long flags;
3814
3815 spin_lock_irqsave(&bus->dpc_tl_lock, flags);
3816 list_for_each_safe(cur_hd, tmp_hd, &bus->dpc_tsklst) {
3817 spin_unlock_irqrestore(&bus->dpc_tl_lock, flags);
3818
3819 brcmf_sdbrcm_dpc(bus);
3820
3821 spin_lock_irqsave(&bus->dpc_tl_lock, flags);
3822 list_del(cur_hd);
3823 kfree(cur_hd);
3824 }
3825 spin_unlock_irqrestore(&bus->dpc_tl_lock, flags);
3826}
3827
Franky Line92eedf2011-11-22 17:21:50 -08003828static void brcmf_sdbrcm_release_malloc(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003829{
3830 brcmf_dbg(TRACE, "Enter\n");
3831
3832 kfree(bus->rxbuf);
3833 bus->rxctl = bus->rxbuf = NULL;
3834 bus->rxlen = 0;
3835
3836 kfree(bus->databuf);
3837 bus->databuf = NULL;
3838}
3839
Franky Line92eedf2011-11-22 17:21:50 -08003840static bool brcmf_sdbrcm_probe_malloc(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003841{
3842 brcmf_dbg(TRACE, "Enter\n");
3843
Franky Linb01a6b32011-12-16 18:37:03 -08003844 if (bus->sdiodev->bus_if->maxctl) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003845 bus->rxblen =
Franky Linb01a6b32011-12-16 18:37:03 -08003846 roundup((bus->sdiodev->bus_if->maxctl + SDPCM_HDRLEN),
Arend van Spriel5b435de2011-10-05 13:19:03 +02003847 ALIGNMENT) + BRCMF_SDALIGN;
3848 bus->rxbuf = kmalloc(bus->rxblen, GFP_ATOMIC);
3849 if (!(bus->rxbuf))
3850 goto fail;
3851 }
3852
3853 /* Allocate buffer to receive glomed packet */
3854 bus->databuf = kmalloc(MAX_DATA_BUF, GFP_ATOMIC);
3855 if (!(bus->databuf)) {
3856 /* release rxbuf which was already located as above */
3857 if (!bus->rxblen)
3858 kfree(bus->rxbuf);
3859 goto fail;
3860 }
3861
3862 /* Align the buffer */
3863 if ((unsigned long)bus->databuf % BRCMF_SDALIGN)
3864 bus->dataptr = bus->databuf + (BRCMF_SDALIGN -
3865 ((unsigned long)bus->databuf % BRCMF_SDALIGN));
3866 else
3867 bus->dataptr = bus->databuf;
3868
3869 return true;
3870
3871fail:
3872 return false;
3873}
3874
Arend van Spriel5b435de2011-10-05 13:19:03 +02003875static bool
Franky Line92eedf2011-11-22 17:21:50 -08003876brcmf_sdbrcm_probe_attach(struct brcmf_sdio *bus, u32 regsva)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003877{
3878 u8 clkctl = 0;
3879 int err = 0;
3880 int reg_addr;
3881 u32 reg_val;
Franky Lin99ba15c2011-11-04 22:23:42 +01003882 u8 idx;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003883
3884 bus->alp_only = true;
3885
Franky Lin38b0b0d2012-11-05 16:22:24 -08003886 sdio_claim_host(bus->sdiodev->func[1]);
3887
Joe Perches18aad4f2012-01-15 00:38:42 -08003888 pr_debug("F1 signature read @0x18000000=0x%4x\n",
Franky Lin79ae3952012-05-04 18:27:34 -07003889 brcmf_sdio_regrl(bus->sdiodev, SI_ENUM_BASE, NULL));
Arend van Spriel5b435de2011-10-05 13:19:03 +02003890
3891 /*
Franky Lina97e4fc2011-11-04 22:23:35 +01003892 * Force PLL off until brcmf_sdio_chip_attach()
Arend van Spriel5b435de2011-10-05 13:19:03 +02003893 * programs PLL control regs
3894 */
3895
Franky Lin3bba8292012-05-04 18:27:33 -07003896 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3897 BRCMF_INIT_CLKCTL1, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003898 if (!err)
Franky Lin45db3392012-05-04 18:27:32 -07003899 clkctl = brcmf_sdio_regrb(bus->sdiodev,
Arend van Spriel5b435de2011-10-05 13:19:03 +02003900 SBSDIO_FUNC1_CHIPCLKCSR, &err);
3901
3902 if (err || ((clkctl & ~SBSDIO_AVBITS) != BRCMF_INIT_CLKCTL1)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003903 brcmf_err("ChipClkCSR access: err %d wrote 0x%02x read 0x%02x\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +02003904 err, BRCMF_INIT_CLKCTL1, clkctl);
3905 goto fail;
3906 }
3907
Franky Lina97e4fc2011-11-04 22:23:35 +01003908 if (brcmf_sdio_chip_attach(bus->sdiodev, &bus->ci, regsva)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003909 brcmf_err("brcmf_sdio_chip_attach failed!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003910 goto fail;
3911 }
3912
3913 if (!brcmf_sdbrcm_chipmatch((u16) bus->ci->chip)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003914 brcmf_err("unsupported chip: 0x%04x\n", bus->ci->chip);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003915 goto fail;
3916 }
3917
Piotr Haber4a3da992013-04-11 13:28:46 +02003918 if (brcmf_sdbrcm_kso_init(bus)) {
3919 brcmf_err("error enabling KSO\n");
3920 goto fail;
3921 }
3922
Franky Line12afb62011-11-04 22:23:40 +01003923 brcmf_sdio_chip_drivestrengthinit(bus->sdiodev, bus->ci,
3924 SDIO_DRIVE_STRENGTH);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003925
Franky Lin454d2a82011-11-04 22:23:37 +01003926 /* Get info on the SOCRAM cores... */
Arend van Spriel5b435de2011-10-05 13:19:03 +02003927 bus->ramsize = bus->ci->ramsize;
3928 if (!(bus->ramsize)) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01003929 brcmf_err("failed to find SOCRAM memory!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003930 goto fail;
3931 }
3932
3933 /* Set core control so an SDIO reset does a backplane reset */
Franky Lin99ba15c2011-11-04 22:23:42 +01003934 idx = brcmf_sdio_chip_getinfidx(bus->ci, BCMA_CORE_SDIO_DEV);
3935 reg_addr = bus->ci->c_inf[idx].base +
Arend van Spriel5b435de2011-10-05 13:19:03 +02003936 offsetof(struct sdpcmd_regs, corecontrol);
Franky Lin79ae3952012-05-04 18:27:34 -07003937 reg_val = brcmf_sdio_regrl(bus->sdiodev, reg_addr, NULL);
Franky Line13ce262012-05-04 18:27:35 -07003938 brcmf_sdio_regwl(bus->sdiodev, reg_addr, reg_val | CC_BPRESEN, NULL);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003939
Franky Lin38b0b0d2012-11-05 16:22:24 -08003940 sdio_release_host(bus->sdiodev->func[1]);
3941
Arend van Spriel5b435de2011-10-05 13:19:03 +02003942 brcmu_pktq_init(&bus->txq, (PRIOMASK + 1), TXQLEN);
3943
3944 /* Locate an appropriately-aligned portion of hdrbuf */
3945 bus->rxhdr = (u8 *) roundup((unsigned long)&bus->hdrbuf[0],
3946 BRCMF_SDALIGN);
3947
3948 /* Set the poll and/or interrupt flags */
3949 bus->intr = true;
3950 bus->poll = false;
3951 if (bus->poll)
3952 bus->pollrate = 1;
3953
3954 return true;
3955
3956fail:
Franky Lin38b0b0d2012-11-05 16:22:24 -08003957 sdio_release_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003958 return false;
3959}
3960
Franky Line92eedf2011-11-22 17:21:50 -08003961static bool brcmf_sdbrcm_probe_init(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003962{
3963 brcmf_dbg(TRACE, "Enter\n");
3964
Franky Lin38b0b0d2012-11-05 16:22:24 -08003965 sdio_claim_host(bus->sdiodev->func[1]);
3966
Arend van Spriel5b435de2011-10-05 13:19:03 +02003967 /* Disable F2 to clear any intermediate frame state on the dongle */
Franky Lin3bba8292012-05-04 18:27:33 -07003968 brcmf_sdio_regwb(bus->sdiodev, SDIO_CCCR_IOEx,
3969 SDIO_FUNC_ENABLE_1, NULL);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003970
Franky Lin712ac5b2011-12-16 18:37:09 -08003971 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003972 bus->rxflow = false;
3973
3974 /* Done with backplane-dependent accesses, can drop clock... */
Franky Lin3bba8292012-05-04 18:27:33 -07003975 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, 0, NULL);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003976
Franky Lin38b0b0d2012-11-05 16:22:24 -08003977 sdio_release_host(bus->sdiodev->func[1]);
3978
Arend van Spriel5b435de2011-10-05 13:19:03 +02003979 /* ...and initialize clock/power states */
3980 bus->clkstate = CLK_SDONLY;
3981 bus->idletime = BRCMF_IDLE_INTERVAL;
3982 bus->idleclock = BRCMF_IDLE_ACTIVE;
3983
3984 /* Query the F2 block size, set roundup accordingly */
3985 bus->blocksize = bus->sdiodev->func[2]->cur_blksize;
3986 bus->roundup = min(max_roundup, bus->blocksize);
3987
3988 /* bus module does not support packet chaining */
3989 bus->use_rxchain = false;
3990 bus->sd_rxchain = false;
3991
Piotr Haber4a3da992013-04-11 13:28:46 +02003992 /* SR state */
3993 bus->sleeping = false;
3994 bus->sr_enabled = false;
3995
Arend van Spriel5b435de2011-10-05 13:19:03 +02003996 return true;
3997}
3998
3999static int
4000brcmf_sdbrcm_watchdog_thread(void *data)
4001{
Franky Line92eedf2011-11-22 17:21:50 -08004002 struct brcmf_sdio *bus = (struct brcmf_sdio *)data;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004003
4004 allow_signal(SIGTERM);
4005 /* Run until signal received */
4006 while (1) {
4007 if (kthread_should_stop())
4008 break;
4009 if (!wait_for_completion_interruptible(&bus->watchdog_wait)) {
Franky Lincad2b262011-11-22 17:21:53 -08004010 brcmf_sdbrcm_bus_watchdog(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004011 /* Count the tick for reference */
Arend van Spriel80969832012-06-09 22:51:44 +02004012 bus->sdcnt.tickcnt++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004013 } else
4014 break;
4015 }
4016 return 0;
4017}
4018
4019static void
4020brcmf_sdbrcm_watchdog(unsigned long data)
4021{
Franky Line92eedf2011-11-22 17:21:50 -08004022 struct brcmf_sdio *bus = (struct brcmf_sdio *)data;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004023
4024 if (bus->watchdog_tsk) {
4025 complete(&bus->watchdog_wait);
4026 /* Reschedule the watchdog */
4027 if (bus->wd_timer_valid)
4028 mod_timer(&bus->timer,
4029 jiffies + BRCMF_WD_POLL_MS * HZ / 1000);
4030 }
4031}
4032
Franky Line92eedf2011-11-22 17:21:50 -08004033static void brcmf_sdbrcm_release_dongle(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004034{
4035 brcmf_dbg(TRACE, "Enter\n");
4036
4037 if (bus->ci) {
Franky Lin38b0b0d2012-11-05 16:22:24 -08004038 sdio_claim_host(bus->sdiodev->func[1]);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004039 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
4040 brcmf_sdbrcm_clkctl(bus, CLK_NONE, false);
Franky Lin38b0b0d2012-11-05 16:22:24 -08004041 sdio_release_host(bus->sdiodev->func[1]);
Franky Lina8a6c042011-11-04 22:23:39 +01004042 brcmf_sdio_chip_detach(&bus->ci);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004043 if (bus->vars && bus->varsz)
4044 kfree(bus->vars);
4045 bus->vars = NULL;
4046 }
4047
4048 brcmf_dbg(TRACE, "Disconnected\n");
4049}
4050
4051/* Detach and free everything */
Franky Line92eedf2011-11-22 17:21:50 -08004052static void brcmf_sdbrcm_release(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004053{
4054 brcmf_dbg(TRACE, "Enter\n");
Arend van Spriel4fc0d012012-06-14 12:16:57 +02004055
Arend van Spriel5b435de2011-10-05 13:19:03 +02004056 if (bus) {
4057 /* De-register interrupt handler */
Franky Linba89bf12012-04-27 18:56:59 -07004058 brcmf_sdio_intr_unregister(bus->sdiodev);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004059
Franky Linf1e68c22012-09-13 21:12:00 +02004060 cancel_work_sync(&bus->datawork);
Hante Meuleman37ac5782012-11-14 18:46:18 -08004061 if (bus->brcmf_wq)
4062 destroy_workqueue(bus->brcmf_wq);
Franky Linf1e68c22012-09-13 21:12:00 +02004063
Franky Lin5f947ad2011-12-16 18:36:56 -08004064 if (bus->sdiodev->bus_if->drvr) {
4065 brcmf_detach(bus->sdiodev->dev);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004066 brcmf_sdbrcm_release_dongle(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004067 }
4068
4069 brcmf_sdbrcm_release_malloc(bus);
4070
4071 kfree(bus);
4072 }
4073
4074 brcmf_dbg(TRACE, "Disconnected\n");
4075}
4076
Arend van Sprield9cb2592012-12-05 15:25:54 +01004077static struct brcmf_bus_ops brcmf_sdio_bus_ops = {
4078 .stop = brcmf_sdbrcm_bus_stop,
4079 .init = brcmf_sdbrcm_bus_init,
4080 .txdata = brcmf_sdbrcm_bus_txdata,
4081 .txctl = brcmf_sdbrcm_bus_txctl,
4082 .rxctl = brcmf_sdbrcm_bus_rxctl,
Arend van Spriele2432b62013-04-03 12:40:38 +02004083 .gettxq = brcmf_sdbrcm_bus_gettxq,
Arend van Sprield9cb2592012-12-05 15:25:54 +01004084};
4085
Franky Lin4175b882011-11-22 17:21:49 -08004086void *brcmf_sdbrcm_probe(u32 regsva, struct brcmf_sdio_dev *sdiodev)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004087{
4088 int ret;
Franky Line92eedf2011-11-22 17:21:50 -08004089 struct brcmf_sdio *bus;
Franky Linbbfd6a62012-06-26 21:26:35 +02004090 struct brcmf_bus_dcmd *dlst;
4091 u32 dngl_txglom;
Franky Linc3d2bc32012-06-26 21:26:36 +02004092 u32 dngl_txglomalign;
Franky Linbbfd6a62012-06-26 21:26:35 +02004093 u8 idx;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004094
Arend van Spriel5b435de2011-10-05 13:19:03 +02004095 brcmf_dbg(TRACE, "Enter\n");
4096
4097 /* We make an assumption about address window mappings:
4098 * regsva == SI_ENUM_BASE*/
4099
4100 /* Allocate private bus interface state */
Franky Line92eedf2011-11-22 17:21:50 -08004101 bus = kzalloc(sizeof(struct brcmf_sdio), GFP_ATOMIC);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004102 if (!bus)
4103 goto fail;
4104
4105 bus->sdiodev = sdiodev;
4106 sdiodev->bus = bus;
Arend van Sprielb83db862011-10-19 12:51:09 +02004107 skb_queue_head_init(&bus->glom);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004108 bus->txbound = BRCMF_TXBOUND;
4109 bus->rxbound = BRCMF_RXBOUND;
4110 bus->txminmax = BRCMF_TXMINMAX;
4111 bus->tx_seq = SDPCM_SEQUENCE_WRAP - 1;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004112
Hante Meuleman37ac5782012-11-14 18:46:18 -08004113 INIT_WORK(&bus->datawork, brcmf_sdio_dataworker);
4114 bus->brcmf_wq = create_singlethread_workqueue("brcmf_wq");
4115 if (bus->brcmf_wq == NULL) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01004116 brcmf_err("insufficient memory to create txworkqueue\n");
Hante Meuleman37ac5782012-11-14 18:46:18 -08004117 goto fail;
4118 }
4119
Arend van Spriel5b435de2011-10-05 13:19:03 +02004120 /* attempt to attach to the dongle */
4121 if (!(brcmf_sdbrcm_probe_attach(bus, regsva))) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01004122 brcmf_err("brcmf_sdbrcm_probe_attach failed\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02004123 goto fail;
4124 }
4125
Franky Lindd43a012012-11-05 16:22:22 -08004126 spin_lock_init(&bus->rxctl_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004127 spin_lock_init(&bus->txqlock);
4128 init_waitqueue_head(&bus->ctrl_wait);
4129 init_waitqueue_head(&bus->dcmd_resp_wait);
4130
4131 /* Set up the watchdog timer */
4132 init_timer(&bus->timer);
4133 bus->timer.data = (unsigned long)bus;
4134 bus->timer.function = brcmf_sdbrcm_watchdog;
4135
Arend van Spriel5b435de2011-10-05 13:19:03 +02004136 /* Initialize watchdog thread */
4137 init_completion(&bus->watchdog_wait);
4138 bus->watchdog_tsk = kthread_run(brcmf_sdbrcm_watchdog_thread,
4139 bus, "brcmf_watchdog");
4140 if (IS_ERR(bus->watchdog_tsk)) {
Joe Perches02f77192012-01-15 00:38:44 -08004141 pr_warn("brcmf_watchdog thread failed to start\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02004142 bus->watchdog_tsk = NULL;
4143 }
4144 /* Initialize DPC thread */
Franky Linb948a852012-04-23 14:24:53 -07004145 INIT_LIST_HEAD(&bus->dpc_tsklst);
4146 spin_lock_init(&bus->dpc_tl_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004147
Franky Lina9ffda82011-12-16 18:37:12 -08004148 /* Assign bus interface call back */
Arend van Sprield9cb2592012-12-05 15:25:54 +01004149 bus->sdiodev->bus_if->dev = bus->sdiodev->dev;
4150 bus->sdiodev->bus_if->ops = &brcmf_sdio_bus_ops;
Arend van Spriel75d907d2013-02-06 18:40:45 +01004151 bus->sdiodev->bus_if->chip = bus->ci->chip;
4152 bus->sdiodev->bus_if->chiprev = bus->ci->chiprev;
Arend van Sprield9cb2592012-12-05 15:25:54 +01004153
Arend van Spriel5b435de2011-10-05 13:19:03 +02004154 /* Attach to the brcmf/OS/network interface */
Franky Lin2447ffb2011-12-16 18:37:10 -08004155 ret = brcmf_attach(SDPCM_RESERVE, bus->sdiodev->dev);
Franky Lin712ac5b2011-12-16 18:37:09 -08004156 if (ret != 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01004157 brcmf_err("brcmf_attach failed\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02004158 goto fail;
4159 }
4160
4161 /* Allocate buffers */
4162 if (!(brcmf_sdbrcm_probe_malloc(bus))) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01004163 brcmf_err("brcmf_sdbrcm_probe_malloc failed\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02004164 goto fail;
4165 }
4166
4167 if (!(brcmf_sdbrcm_probe_init(bus))) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01004168 brcmf_err("brcmf_sdbrcm_probe_init failed\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02004169 goto fail;
4170 }
4171
Arend van Spriel80969832012-06-09 22:51:44 +02004172 brcmf_sdio_debugfs_create(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004173 brcmf_dbg(INFO, "completed!!\n");
4174
Franky Linbbfd6a62012-06-26 21:26:35 +02004175 /* sdio bus core specific dcmd */
4176 idx = brcmf_sdio_chip_getinfidx(bus->ci, BCMA_CORE_SDIO_DEV);
4177 dlst = kzalloc(sizeof(struct brcmf_bus_dcmd), GFP_KERNEL);
Franky Linc3d2bc32012-06-26 21:26:36 +02004178 if (dlst) {
4179 if (bus->ci->c_inf[idx].rev < 12) {
4180 /* for sdio core rev < 12, disable txgloming */
4181 dngl_txglom = 0;
4182 dlst->name = "bus:txglom";
4183 dlst->param = (char *)&dngl_txglom;
4184 dlst->param_len = sizeof(u32);
4185 } else {
4186 /* otherwise, set txglomalign */
4187 dngl_txglomalign = bus->sdiodev->bus_if->align;
4188 dlst->name = "bus:txglomalign";
4189 dlst->param = (char *)&dngl_txglomalign;
4190 dlst->param_len = sizeof(u32);
4191 }
Franky Linbbfd6a62012-06-26 21:26:35 +02004192 list_add(&dlst->list, &bus->sdiodev->bus_if->dcmd_list);
4193 }
4194
Arend van Spriel5b435de2011-10-05 13:19:03 +02004195 /* if firmware path present try to download and bring up bus */
Franky Lined683c92011-12-16 18:36:55 -08004196 ret = brcmf_bus_start(bus->sdiodev->dev);
Arend van Spriel5b435de2011-10-05 13:19:03 +02004197 if (ret != 0) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +01004198 brcmf_err("dongle is not responding\n");
Hante Meuleman1799ddf2012-11-14 18:46:17 -08004199 goto fail;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004200 }
Franky Lin15d45b62011-10-21 16:16:32 +02004201
Arend van Spriel5b435de2011-10-05 13:19:03 +02004202 return bus;
4203
4204fail:
4205 brcmf_sdbrcm_release(bus);
4206 return NULL;
4207}
4208
4209void brcmf_sdbrcm_disconnect(void *ptr)
4210{
Franky Line92eedf2011-11-22 17:21:50 -08004211 struct brcmf_sdio *bus = (struct brcmf_sdio *)ptr;
Arend van Spriel5b435de2011-10-05 13:19:03 +02004212
4213 brcmf_dbg(TRACE, "Enter\n");
4214
4215 if (bus)
4216 brcmf_sdbrcm_release(bus);
4217
4218 brcmf_dbg(TRACE, "Disconnected\n");
4219}
4220
Arend van Spriel5b435de2011-10-05 13:19:03 +02004221void
Franky Line92eedf2011-11-22 17:21:50 -08004222brcmf_sdbrcm_wd_timer(struct brcmf_sdio *bus, uint wdtick)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004223{
Arend van Spriel5b435de2011-10-05 13:19:03 +02004224 /* Totally stop the timer */
Joe Perches23677ce2012-02-09 11:17:23 +00004225 if (!wdtick && bus->wd_timer_valid) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02004226 del_timer_sync(&bus->timer);
4227 bus->wd_timer_valid = false;
4228 bus->save_ms = wdtick;
4229 return;
4230 }
4231
Franky Linece960e2011-10-21 16:16:19 +02004232 /* don't start the wd until fw is loaded */
Franky Lin712ac5b2011-12-16 18:37:09 -08004233 if (bus->sdiodev->bus_if->state == BRCMF_BUS_DOWN)
Franky Linece960e2011-10-21 16:16:19 +02004234 return;
4235
Arend van Spriel5b435de2011-10-05 13:19:03 +02004236 if (wdtick) {
4237 if (bus->save_ms != BRCMF_WD_POLL_MS) {
Joe Perches23677ce2012-02-09 11:17:23 +00004238 if (bus->wd_timer_valid)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004239 /* Stop timer and restart at new value */
4240 del_timer_sync(&bus->timer);
4241
4242 /* Create timer again when watchdog period is
4243 dynamically changed or in the first instance
4244 */
4245 bus->timer.expires =
4246 jiffies + BRCMF_WD_POLL_MS * HZ / 1000;
4247 add_timer(&bus->timer);
4248
4249 } else {
4250 /* Re arm the timer, at last watchdog period */
4251 mod_timer(&bus->timer,
4252 jiffies + BRCMF_WD_POLL_MS * HZ / 1000);
4253 }
4254
4255 bus->wd_timer_valid = true;
4256 bus->save_ms = wdtick;
4257 }
4258}