This source file includes following definitions.
- prio2prec
- pkt_align
- data_ok
- brcmf_sdio_kso_control
- brcmf_sdio_htclk
- brcmf_sdio_sdclk
- brcmf_sdio_clkctl
- brcmf_sdio_bus_sleep
- brcmf_sdio_valid_shared_address
- brcmf_sdio_readshared
- brcmf_sdio_get_console_addr
- brcmf_sdio_get_console_addr
- brcmf_sdio_hostmail
- brcmf_sdio_rxfail
- brcmf_sdio_txfail
- brcmf_sdio_glom_len
- brcmf_sdio_free_glom
- brcmf_sdio_getdatoffset
- brcmf_sdio_fromevntchan
- brcmf_sdio_hdparse
- brcmf_sdio_update_hwhdr
- brcmf_sdio_hdpack
- brcmf_sdio_rxglom
- brcmf_sdio_dcmd_resp_wait
- brcmf_sdio_dcmd_resp_wake
- brcmf_sdio_read_control
- brcmf_sdio_pad
- brcmf_sdio_readframes
- brcmf_sdio_wait_event_wakeup
- brcmf_sdio_txpkt_hdalign
- brcmf_sdio_txpkt_prep_sg
- brcmf_sdio_txpkt_prep
- brcmf_sdio_txpkt_postp
- brcmf_sdio_txpkt
- brcmf_sdio_sendfromq
- brcmf_sdio_tx_ctrlframe
- brcmf_chip_is_ulp
- brcmf_sdio_bus_stop
- brcmf_sdio_clrintr
- brcmf_sdio_intr_rstatus
- brcmf_sdio_dpc
- brcmf_sdio_bus_gettxq
- brcmf_sdio_prec_enq
- brcmf_sdio_bus_txdata
- brcmf_sdio_readconsole
- brcmf_sdio_bus_txctl
- brcmf_sdio_dump_console
- brcmf_sdio_trap_info
- brcmf_sdio_assert_info
- brcmf_sdio_checkdied
- brcmf_sdio_died_dump
- brcmf_sdio_forensic_read
- brcmf_debugfs_sdio_count_read
- brcmf_sdio_debugfs_create
- brcmf_sdio_checkdied
- brcmf_sdio_debugfs_create
- brcmf_sdio_bus_rxctl
- brcmf_sdio_verifymemory
- brcmf_sdio_verifymemory
- brcmf_sdio_download_code_file
- brcmf_sdio_download_nvram
- brcmf_sdio_download_firmware
- brcmf_sdio_aos_no_decode
- brcmf_sdio_sr_init
- brcmf_sdio_kso_init
- brcmf_sdio_bus_preinit
- brcmf_sdio_bus_get_ramsize
- brcmf_sdio_bus_get_memdump
- brcmf_sdio_trigger_dpc
- brcmf_sdio_isr
- brcmf_sdio_bus_watchdog
- brcmf_sdio_dataworker
- brcmf_sdio_drivestrengthinit
- brcmf_sdio_buscoreprep
- brcmf_sdio_buscore_activate
- brcmf_sdio_buscore_read32
- brcmf_sdio_buscore_write32
- brcmf_sdio_probe_attach
- brcmf_sdio_watchdog_thread
- brcmf_sdio_watchdog
- brcmf_sdio_get_fwname
- brcmf_sdio_firmware_callback
- brcmf_sdio_prepare_fw_request
- brcmf_sdio_probe
- brcmf_sdio_remove
- brcmf_sdio_wd_timer
- brcmf_sdio_sleep
1
2
3
4
5
6 #include <linux/types.h>
7 #include <linux/atomic.h>
8 #include <linux/kernel.h>
9 #include <linux/kthread.h>
10 #include <linux/printk.h>
11 #include <linux/pci_ids.h>
12 #include <linux/netdevice.h>
13 #include <linux/interrupt.h>
14 #include <linux/sched/signal.h>
15 #include <linux/mmc/sdio.h>
16 #include <linux/mmc/sdio_ids.h>
17 #include <linux/mmc/sdio_func.h>
18 #include <linux/mmc/card.h>
19 #include <linux/semaphore.h>
20 #include <linux/firmware.h>
21 #include <linux/module.h>
22 #include <linux/bcma/bcma.h>
23 #include <linux/debugfs.h>
24 #include <linux/vmalloc.h>
25 #include <asm/unaligned.h>
26 #include <defs.h>
27 #include <brcmu_wifi.h>
28 #include <brcmu_utils.h>
29 #include <brcm_hw_ids.h>
30 #include <soc.h>
31 #include "sdio.h"
32 #include "chip.h"
33 #include "firmware.h"
34 #include "core.h"
35 #include "common.h"
36 #include "bcdc.h"
37
38 #define DCMD_RESP_TIMEOUT msecs_to_jiffies(2500)
39 #define CTL_DONE_TIMEOUT msecs_to_jiffies(2500)
40
41
42 #define DEFAULT_F2_WATERMARK 0x8
43 #define CY_4373_F2_WATERMARK 0x40
44 #define CY_43012_F2_WATERMARK 0x60
45
46 #ifdef DEBUG
47
48 #define BRCMF_TRAP_INFO_SIZE 80
49
50 #define CBUF_LEN (128)
51
52
53 #define CONSOLE_BUFFER_MAX 2024
54
55 struct rte_log_le {
56 __le32 buf;
57 __le32 buf_size;
58 __le32 idx;
59 char *_buf_compat;
60 };
61
62 struct rte_console {
63
64
65
66
67
68
69
70
71
72 uint vcons_in;
73 uint vcons_out;
74
75
76
77
78
79
80
81 struct rte_log_le log_le;
82
83
84
85
86
87
88
89 uint cbuf_idx;
90 char cbuf[CBUF_LEN];
91 };
92
93 #endif
94 #include <chipcommon.h>
95
96 #include "bus.h"
97 #include "debug.h"
98 #include "tracepoint.h"
99
100 #define TXQLEN 2048
101 #define TXHI (TXQLEN - 256)
102 #define TXLOW (TXHI - 256)
103 #define PRIOMASK 7
104
105 #define TXRETRIES 2
106
107 #define BRCMF_RXBOUND 50
108
109
110 #define BRCMF_TXBOUND 20
111
112
113 #define BRCMF_TXMINMAX 1
114
115 #define MEMBLOCK 2048
116
117 #define MAX_DATA_BUF (32 * 1024)
118
119
120 #define BRCMF_FIRSTREAD (1 << 6)
121
122 #define BRCMF_CONSOLE 10
123
124
125
126
127 #define SBSDIO_DEVCTL_SETBUSY 0x01
128
129 #define SBSDIO_DEVCTL_SPI_INTR_SYNC 0x02
130
131 #define SBSDIO_DEVCTL_CA_INT_ONLY 0x04
132
133
134 #define SBSDIO_DEVCTL_PADS_ISO 0x08
135
136 #define SBSDIO_DEVCTL_F2WM_ENAB 0x10
137
138 #define SBSDIO_DEVCTL_SB_RST_CTL 0x30
139
140 #define SBSDIO_DEVCTL_RST_CORECTL 0x00
141
142 #define SBSDIO_DEVCTL_RST_BPRESET 0x10
143
144 #define SBSDIO_DEVCTL_RST_NOBPRESET 0x20
145
146
147
148
149 #define SBSDIO_CIS_BASE_COMMON 0x1000
150
151 #define SBSDIO_CIS_SIZE_LIMIT 0x200
152
153 #define SBSDIO_CIS_OFT_ADDR_MASK 0x1FFFF
154
155
156 #define SBSDIO_CIS_MANFID_TUPLE_LEN 6
157
158 #define SD_REG(field) \
159 (offsetof(struct sdpcmd_regs, field))
160
161
162
163 #define SBSDIO_FORCE_ALP 0x01
164
165 #define SBSDIO_FORCE_HT 0x02
166
167 #define SBSDIO_FORCE_ILP 0x04
168
169 #define SBSDIO_ALP_AVAIL_REQ 0x08
170
171 #define SBSDIO_HT_AVAIL_REQ 0x10
172
173 #define SBSDIO_FORCE_HW_CLKREQ_OFF 0x20
174
175 #define SBSDIO_ALP_AVAIL 0x40
176
177 #define SBSDIO_HT_AVAIL 0x80
178 #define SBSDIO_CSR_MASK 0x1F
179 #define SBSDIO_AVBITS (SBSDIO_HT_AVAIL | SBSDIO_ALP_AVAIL)
180 #define SBSDIO_ALPAV(regval) ((regval) & SBSDIO_AVBITS)
181 #define SBSDIO_HTAV(regval) (((regval) & SBSDIO_AVBITS) == SBSDIO_AVBITS)
182 #define SBSDIO_ALPONLY(regval) (SBSDIO_ALPAV(regval) && !SBSDIO_HTAV(regval))
183 #define SBSDIO_CLKAV(regval, alponly) \
184 (SBSDIO_ALPAV(regval) && (alponly ? 1 : SBSDIO_HTAV(regval)))
185
186
187 #define I_SMB_SW0 (1 << 0)
188 #define I_SMB_SW1 (1 << 1)
189 #define I_SMB_SW2 (1 << 2)
190 #define I_SMB_SW3 (1 << 3)
191 #define I_SMB_SW_MASK 0x0000000f
192 #define I_SMB_SW_SHIFT 0
193 #define I_HMB_SW0 (1 << 4)
194 #define I_HMB_SW1 (1 << 5)
195 #define I_HMB_SW2 (1 << 6)
196 #define I_HMB_SW3 (1 << 7)
197 #define I_HMB_SW_MASK 0x000000f0
198 #define I_HMB_SW_SHIFT 4
199 #define I_WR_OOSYNC (1 << 8)
200 #define I_RD_OOSYNC (1 << 9)
201 #define I_PC (1 << 10)
202 #define I_PD (1 << 11)
203 #define I_DE (1 << 12)
204 #define I_RU (1 << 13)
205 #define I_RO (1 << 14)
206 #define I_XU (1 << 15)
207 #define I_RI (1 << 16)
208 #define I_BUSPWR (1 << 17)
209 #define I_XMTDATA_AVAIL (1 << 23)
210 #define I_XI (1 << 24)
211 #define I_RF_TERM (1 << 25)
212 #define I_WF_TERM (1 << 26)
213 #define I_PCMCIA_XU (1 << 27)
214 #define I_SBINT (1 << 28)
215 #define I_CHIPACTIVE (1 << 29)
216 #define I_SRESET (1 << 30)
217 #define I_IOE2 (1U << 31)
218 #define I_ERRORS (I_PC | I_PD | I_DE | I_RU | I_RO | I_XU)
219 #define I_DMA (I_RI | I_XI | I_ERRORS)
220
221
222 #define CC_CISRDY (1 << 0)
223 #define CC_BPRESEN (1 << 1)
224 #define CC_F2RDY (1 << 2)
225 #define CC_CLRPADSISO (1 << 3)
226 #define CC_XMTDATAAVAIL_MODE (1 << 4)
227 #define CC_XMTDATAAVAIL_CTRL (1 << 5)
228
229
230 #define SFC_RF_TERM (1 << 0)
231 #define SFC_WF_TERM (1 << 1)
232 #define SFC_CRC4WOOS (1 << 2)
233 #define SFC_ABORTALL (1 << 3)
234
235
236
237
238
239
240 #define SMB_NAK (1 << 0)
241 #define SMB_INT_ACK (1 << 1)
242 #define SMB_USE_OOB (1 << 2)
243 #define SMB_DEV_INT (1 << 3)
244
245
246 #define SMB_DATA_VERSION_SHIFT 16
247
248
249
250
251
252
253 #define I_HMB_FC_STATE I_HMB_SW0
254 #define I_HMB_FC_CHANGE I_HMB_SW1
255 #define I_HMB_FRAME_IND I_HMB_SW2
256 #define I_HMB_HOST_INT I_HMB_SW3
257
258
259 #define HMB_DATA_NAKHANDLED 0x0001
260 #define HMB_DATA_DEVREADY 0x0002
261 #define HMB_DATA_FC 0x0004
262 #define HMB_DATA_FWREADY 0x0008
263 #define HMB_DATA_FWHALT 0x0010
264
265 #define HMB_DATA_FCDATA_MASK 0xff000000
266 #define HMB_DATA_FCDATA_SHIFT 24
267
268 #define HMB_DATA_VERSION_MASK 0x00ff0000
269 #define HMB_DATA_VERSION_SHIFT 16
270
271
272
273
274
275
276 #define SDPCM_PROT_VERSION 4
277
278
279
280
281
282 #define SDPCM_SHARED_VERSION 0x0003
283 #define SDPCM_SHARED_VERSION_MASK 0x00FF
284 #define SDPCM_SHARED_ASSERT_BUILT 0x0100
285 #define SDPCM_SHARED_ASSERT 0x0200
286 #define SDPCM_SHARED_TRAP 0x0400
287
288
289 #define MAX_HDR_READ (1 << 6)
290 #define MAX_RX_DATASZ 2048
291
292
293
294
295
296
297 #undef PMU_MAX_TRANSITION_DLY
298 #define PMU_MAX_TRANSITION_DLY 1000000
299
300
301 #define BRCMF_INIT_CLKCTL1 (SBSDIO_FORCE_HW_CLKREQ_OFF | \
302 SBSDIO_ALP_AVAIL_REQ)
303
304
305 #define F2SYNC (SDIO_REQ_4BYTE | SDIO_REQ_FIXED)
306
307 #define BRCMF_IDLE_ACTIVE 0
308
309
310 #define BRCMF_IDLE_INTERVAL 1
311
312 #define KSO_WAIT_US 50
313 #define MAX_KSO_ATTEMPTS (PMU_MAX_TRANSITION_DLY/KSO_WAIT_US)
314 #define BRCMF_SDIO_MAX_ACCESS_ERRORS 5
315
316
317
318
319 static uint prio2prec(u32 prio)
320 {
321 return (prio == PRIO_8021D_NONE || prio == PRIO_8021D_BE) ?
322 (prio^2) : prio;
323 }
324
325 #ifdef DEBUG
326
327 struct brcmf_console {
328 uint count;
329 uint log_addr;
330 struct rte_log_le log_le;
331 uint bufsize;
332 u8 *buf;
333 uint last;
334 };
335
336 struct brcmf_trap_info {
337 __le32 type;
338 __le32 epc;
339 __le32 cpsr;
340 __le32 spsr;
341 __le32 r0;
342 __le32 r1;
343 __le32 r2;
344 __le32 r3;
345 __le32 r4;
346 __le32 r5;
347 __le32 r6;
348 __le32 r7;
349 __le32 r8;
350 __le32 r9;
351 __le32 r10;
352 __le32 r11;
353 __le32 r12;
354 __le32 r13;
355 __le32 r14;
356 __le32 pc;
357 };
358 #endif
359
360 struct sdpcm_shared {
361 u32 flags;
362 u32 trap_addr;
363 u32 assert_exp_addr;
364 u32 assert_file_addr;
365 u32 assert_line;
366 u32 console_addr;
367 u32 msgtrace_addr;
368 u8 tag[32];
369 u32 brpt_addr;
370 };
371
372 struct sdpcm_shared_le {
373 __le32 flags;
374 __le32 trap_addr;
375 __le32 assert_exp_addr;
376 __le32 assert_file_addr;
377 __le32 assert_line;
378 __le32 console_addr;
379 __le32 msgtrace_addr;
380 u8 tag[32];
381 __le32 brpt_addr;
382 };
383
384
385 struct brcmf_sdio_hdrinfo {
386 u8 seq_num;
387 u8 channel;
388 u16 len;
389 u16 len_left;
390 u16 len_nxtfrm;
391 u8 dat_offset;
392 bool lastfrm;
393 u16 tail_pad;
394 };
395
396
397
398
399 struct brcmf_sdio_count {
400 uint intrcount;
401 uint lastintrs;
402 uint pollcnt;
403 uint regfails;
404 uint tx_sderrs;
405 uint fcqueued;
406 uint rxrtx;
407 uint rx_toolong;
408 uint rxc_errors;
409 uint rx_hdrfail;
410 uint rx_badhdr;
411 uint rx_badseq;
412 uint fc_rcvd;
413 uint fc_xoff;
414 uint fc_xon;
415 uint rxglomfail;
416 uint rxglomframes;
417 uint rxglompkts;
418 uint f2rxhdrs;
419 uint f2rxdata;
420 uint f2txdata;
421 uint f1regdata;
422 uint tickcnt;
423 ulong tx_ctlerrs;
424 ulong tx_ctlpkts;
425 ulong rx_ctlerrs;
426 ulong rx_ctlpkts;
427 ulong rx_readahead_cnt;
428 };
429
430
431
432 struct brcmf_sdio {
433 struct brcmf_sdio_dev *sdiodev;
434 struct brcmf_chip *ci;
435 struct brcmf_core *sdio_core;
436
437 u32 hostintmask;
438 atomic_t intstatus;
439 atomic_t fcstate;
440
441 uint blocksize;
442 uint roundup;
443
444 struct pktq txq;
445 u8 flowcontrol;
446 u8 tx_seq;
447 u8 tx_max;
448
449 u8 *hdrbuf;
450 u8 *rxhdr;
451 u8 rx_seq;
452 struct brcmf_sdio_hdrinfo cur_read;
453
454 bool rxskip;
455 bool rxpending;
456
457 uint rxbound;
458 uint txbound;
459 uint txminmax;
460
461 struct sk_buff *glomd;
462 struct sk_buff_head glom;
463
464 u8 *rxbuf;
465 uint rxblen;
466 u8 *rxctl;
467 u8 *rxctl_orig;
468 uint rxlen;
469 spinlock_t rxctl_lock;
470
471 u8 sdpcm_ver;
472
473 bool intr;
474 bool poll;
475 atomic_t ipend;
476 uint spurious;
477 uint pollrate;
478 uint polltick;
479
480 #ifdef DEBUG
481 uint console_interval;
482 struct brcmf_console console;
483 uint console_addr;
484 #endif
485
486 uint clkstate;
487 s32 idletime;
488 s32 idlecount;
489 s32 idleclock;
490 bool rxflow_mode;
491 bool rxflow;
492 bool alp_only;
493
494 u8 *ctrl_frame_buf;
495 u16 ctrl_frame_len;
496 bool ctrl_frame_stat;
497 int ctrl_frame_err;
498
499 spinlock_t txq_lock;
500 wait_queue_head_t ctrl_wait;
501 wait_queue_head_t dcmd_resp_wait;
502
503 struct timer_list timer;
504 struct completion watchdog_wait;
505 struct task_struct *watchdog_tsk;
506 bool wd_active;
507
508 struct workqueue_struct *brcmf_wq;
509 struct work_struct datawork;
510 bool dpc_triggered;
511 bool dpc_running;
512
513 bool txoff;
514 struct brcmf_sdio_count sdcnt;
515 bool sr_enabled;
516 bool sleeping;
517
518 u8 tx_hdrlen;
519 bool txglom;
520 u16 head_align;
521 u16 sgentry_align;
522 };
523
524
525 #define CLK_NONE 0
526 #define CLK_SDONLY 1
527 #define CLK_PENDING 2
528 #define CLK_AVAIL 3
529
530 #ifdef DEBUG
531 static int qcount[NUMPRIO];
532 #endif
533
534 #define DEFAULT_SDIO_DRIVE_STRENGTH 6
535
536 #define RETRYCHAN(chan) ((chan) == SDPCM_EVENT_CHANNEL)
537
538
539 static const uint max_roundup = 512;
540
541 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
542 #define ALIGNMENT 8
543 #else
544 #define ALIGNMENT 4
545 #endif
546
547 enum brcmf_sdio_frmtype {
548 BRCMF_SDIO_FT_NORMAL,
549 BRCMF_SDIO_FT_SUPER,
550 BRCMF_SDIO_FT_SUB,
551 };
552
553 #define SDIOD_DRVSTR_KEY(chip, pmu) (((chip) << 16) | (pmu))
554
555
556 struct sdiod_drive_str {
557 u8 strength;
558 u8 sel;
559 };
560
561
562 static const struct sdiod_drive_str sdiod_drvstr_tab1_1v8[] = {
563 {32, 0x6},
564 {26, 0x7},
565 {22, 0x4},
566 {16, 0x5},
567 {12, 0x2},
568 {8, 0x3},
569 {4, 0x0},
570 {0, 0x1}
571 };
572
573
574 static const struct sdiod_drive_str sdiod_drive_strength_tab5_1v8[] = {
575 {6, 0x7},
576 {5, 0x6},
577 {4, 0x5},
578 {3, 0x4},
579 {2, 0x2},
580 {1, 0x1},
581 {0, 0x0}
582 };
583
584
585 static const struct sdiod_drive_str sdiod_drvstr_tab6_1v8[] = {
586 {3, 0x3},
587 {2, 0x2},
588 {1, 0x1},
589 {0, 0x0} };
590
591
592 static const struct sdiod_drive_str sdiod_drvstr_tab2_3v3[] = {
593 {16, 0x7},
594 {12, 0x5},
595 {8, 0x3},
596 {4, 0x1}
597 };
598
599 BRCMF_FW_DEF(43143, "brcmfmac43143-sdio");
600 BRCMF_FW_DEF(43241B0, "brcmfmac43241b0-sdio");
601 BRCMF_FW_DEF(43241B4, "brcmfmac43241b4-sdio");
602 BRCMF_FW_DEF(43241B5, "brcmfmac43241b5-sdio");
603 BRCMF_FW_DEF(4329, "brcmfmac4329-sdio");
604 BRCMF_FW_DEF(4330, "brcmfmac4330-sdio");
605 BRCMF_FW_DEF(4334, "brcmfmac4334-sdio");
606 BRCMF_FW_DEF(43340, "brcmfmac43340-sdio");
607 BRCMF_FW_DEF(4335, "brcmfmac4335-sdio");
608 BRCMF_FW_DEF(43362, "brcmfmac43362-sdio");
609 BRCMF_FW_DEF(4339, "brcmfmac4339-sdio");
610 BRCMF_FW_DEF(43430A0, "brcmfmac43430a0-sdio");
611
612 BRCMF_FW_DEF(43430A1, "brcmfmac43430-sdio");
613 BRCMF_FW_DEF(43455, "brcmfmac43455-sdio");
614 BRCMF_FW_DEF(43456, "brcmfmac43456-sdio");
615 BRCMF_FW_DEF(4354, "brcmfmac4354-sdio");
616 BRCMF_FW_DEF(4356, "brcmfmac4356-sdio");
617 BRCMF_FW_DEF(4373, "brcmfmac4373-sdio");
618 BRCMF_FW_DEF(43012, "brcmfmac43012-sdio");
619
620 static const struct brcmf_firmware_mapping brcmf_sdio_fwnames[] = {
621 BRCMF_FW_ENTRY(BRCM_CC_43143_CHIP_ID, 0xFFFFFFFF, 43143),
622 BRCMF_FW_ENTRY(BRCM_CC_43241_CHIP_ID, 0x0000001F, 43241B0),
623 BRCMF_FW_ENTRY(BRCM_CC_43241_CHIP_ID, 0x00000020, 43241B4),
624 BRCMF_FW_ENTRY(BRCM_CC_43241_CHIP_ID, 0xFFFFFFC0, 43241B5),
625 BRCMF_FW_ENTRY(BRCM_CC_4329_CHIP_ID, 0xFFFFFFFF, 4329),
626 BRCMF_FW_ENTRY(BRCM_CC_4330_CHIP_ID, 0xFFFFFFFF, 4330),
627 BRCMF_FW_ENTRY(BRCM_CC_4334_CHIP_ID, 0xFFFFFFFF, 4334),
628 BRCMF_FW_ENTRY(BRCM_CC_43340_CHIP_ID, 0xFFFFFFFF, 43340),
629 BRCMF_FW_ENTRY(BRCM_CC_43341_CHIP_ID, 0xFFFFFFFF, 43340),
630 BRCMF_FW_ENTRY(BRCM_CC_4335_CHIP_ID, 0xFFFFFFFF, 4335),
631 BRCMF_FW_ENTRY(BRCM_CC_43362_CHIP_ID, 0xFFFFFFFE, 43362),
632 BRCMF_FW_ENTRY(BRCM_CC_4339_CHIP_ID, 0xFFFFFFFF, 4339),
633 BRCMF_FW_ENTRY(BRCM_CC_43430_CHIP_ID, 0x00000001, 43430A0),
634 BRCMF_FW_ENTRY(BRCM_CC_43430_CHIP_ID, 0xFFFFFFFE, 43430A1),
635 BRCMF_FW_ENTRY(BRCM_CC_4345_CHIP_ID, 0x00000200, 43456),
636 BRCMF_FW_ENTRY(BRCM_CC_4345_CHIP_ID, 0xFFFFFDC0, 43455),
637 BRCMF_FW_ENTRY(BRCM_CC_4354_CHIP_ID, 0xFFFFFFFF, 4354),
638 BRCMF_FW_ENTRY(BRCM_CC_4356_CHIP_ID, 0xFFFFFFFF, 4356),
639 BRCMF_FW_ENTRY(CY_CC_4373_CHIP_ID, 0xFFFFFFFF, 4373),
640 BRCMF_FW_ENTRY(CY_CC_43012_CHIP_ID, 0xFFFFFFFF, 43012)
641 };
642
643 static void pkt_align(struct sk_buff *p, int len, int align)
644 {
645 uint datalign;
646 datalign = (unsigned long)(p->data);
647 datalign = roundup(datalign, (align)) - datalign;
648 if (datalign)
649 skb_pull(p, datalign);
650 __skb_trim(p, len);
651 }
652
653
654 static bool data_ok(struct brcmf_sdio *bus)
655 {
656 return (u8)(bus->tx_max - bus->tx_seq) != 0 &&
657 ((u8)(bus->tx_max - bus->tx_seq) & 0x80) == 0;
658 }
659
660 static int
661 brcmf_sdio_kso_control(struct brcmf_sdio *bus, bool on)
662 {
663 u8 wr_val = 0, rd_val, cmp_val, bmask;
664 int err = 0;
665 int err_cnt = 0;
666 int try_cnt = 0;
667
668 brcmf_dbg(TRACE, "Enter: on=%d\n", on);
669
670 sdio_retune_crc_disable(bus->sdiodev->func1);
671
672
673 if (on)
674 sdio_retune_hold_now(bus->sdiodev->func1);
675
676 wr_val = (on << SBSDIO_FUNC1_SLEEPCSR_KSO_SHIFT);
677
678 brcmf_sdiod_writeb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR, wr_val, &err);
679
680
681
682
683
684
685 if (!on && bus->ci->chip == CY_CC_43012_CHIP_ID)
686 return err;
687
688 if (on) {
689
690
691
692
693 cmp_val = SBSDIO_FUNC1_SLEEPCSR_KSO_MASK |
694 SBSDIO_FUNC1_SLEEPCSR_DEVON_MASK;
695 bmask = cmp_val;
696 usleep_range(2000, 3000);
697 } else {
698
699 cmp_val = 0;
700
701
702
703 bmask = SBSDIO_FUNC1_SLEEPCSR_KSO_MASK;
704 }
705
706 do {
707
708
709
710
711
712 rd_val = brcmf_sdiod_readb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
713 &err);
714 if (!err) {
715 if ((rd_val & bmask) == cmp_val)
716 break;
717 err_cnt = 0;
718 }
719
720 if (err && (err_cnt++ > BRCMF_SDIO_MAX_ACCESS_ERRORS))
721 break;
722
723 udelay(KSO_WAIT_US);
724 brcmf_sdiod_writeb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR, wr_val,
725 &err);
726
727 } while (try_cnt++ < MAX_KSO_ATTEMPTS);
728
729 if (try_cnt > 2)
730 brcmf_dbg(SDIO, "try_cnt=%d rd_val=0x%x err=%d\n", try_cnt,
731 rd_val, err);
732
733 if (try_cnt > MAX_KSO_ATTEMPTS)
734 brcmf_err("max tries: rd_val=0x%x err=%d\n", rd_val, err);
735
736 if (on)
737 sdio_retune_release(bus->sdiodev->func1);
738
739 sdio_retune_crc_enable(bus->sdiodev->func1);
740
741 return err;
742 }
743
744 #define HOSTINTMASK (I_HMB_SW_MASK | I_CHIPACTIVE)
745
746
747 static int brcmf_sdio_htclk(struct brcmf_sdio *bus, bool on, bool pendok)
748 {
749 int err;
750 u8 clkctl, clkreq, devctl;
751 unsigned long timeout;
752
753 brcmf_dbg(SDIO, "Enter\n");
754
755 clkctl = 0;
756
757 if (bus->sr_enabled) {
758 bus->clkstate = (on ? CLK_AVAIL : CLK_SDONLY);
759 return 0;
760 }
761
762 if (on) {
763
764 clkreq =
765 bus->alp_only ? SBSDIO_ALP_AVAIL_REQ : SBSDIO_HT_AVAIL_REQ;
766
767 brcmf_sdiod_writeb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
768 clkreq, &err);
769 if (err) {
770 brcmf_err("HT Avail request error: %d\n", err);
771 return -EBADE;
772 }
773
774
775 clkctl = brcmf_sdiod_readb(bus->sdiodev,
776 SBSDIO_FUNC1_CHIPCLKCSR, &err);
777 if (err) {
778 brcmf_err("HT Avail read error: %d\n", err);
779 return -EBADE;
780 }
781
782
783 if (!SBSDIO_CLKAV(clkctl, bus->alp_only) && pendok) {
784
785 devctl = brcmf_sdiod_readb(bus->sdiodev,
786 SBSDIO_DEVICE_CTL, &err);
787 if (err) {
788 brcmf_err("Devctl error setting CA: %d\n", err);
789 return -EBADE;
790 }
791
792 devctl |= SBSDIO_DEVCTL_CA_INT_ONLY;
793 brcmf_sdiod_writeb(bus->sdiodev, SBSDIO_DEVICE_CTL,
794 devctl, &err);
795 brcmf_dbg(SDIO, "CLKCTL: set PENDING\n");
796 bus->clkstate = CLK_PENDING;
797
798 return 0;
799 } else if (bus->clkstate == CLK_PENDING) {
800
801 devctl = brcmf_sdiod_readb(bus->sdiodev,
802 SBSDIO_DEVICE_CTL, &err);
803 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
804 brcmf_sdiod_writeb(bus->sdiodev, SBSDIO_DEVICE_CTL,
805 devctl, &err);
806 }
807
808
809 timeout = jiffies +
810 msecs_to_jiffies(PMU_MAX_TRANSITION_DLY/1000);
811 while (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
812 clkctl = brcmf_sdiod_readb(bus->sdiodev,
813 SBSDIO_FUNC1_CHIPCLKCSR,
814 &err);
815 if (time_after(jiffies, timeout))
816 break;
817 else
818 usleep_range(5000, 10000);
819 }
820 if (err) {
821 brcmf_err("HT Avail request error: %d\n", err);
822 return -EBADE;
823 }
824 if (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
825 brcmf_err("HT Avail timeout (%d): clkctl 0x%02x\n",
826 PMU_MAX_TRANSITION_DLY, clkctl);
827 return -EBADE;
828 }
829
830
831 bus->clkstate = CLK_AVAIL;
832 brcmf_dbg(SDIO, "CLKCTL: turned ON\n");
833
834 #if defined(DEBUG)
835 if (!bus->alp_only) {
836 if (SBSDIO_ALPONLY(clkctl))
837 brcmf_err("HT Clock should be on\n");
838 }
839 #endif
840
841 } else {
842 clkreq = 0;
843
844 if (bus->clkstate == CLK_PENDING) {
845
846 devctl = brcmf_sdiod_readb(bus->sdiodev,
847 SBSDIO_DEVICE_CTL, &err);
848 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
849 brcmf_sdiod_writeb(bus->sdiodev, SBSDIO_DEVICE_CTL,
850 devctl, &err);
851 }
852
853 bus->clkstate = CLK_SDONLY;
854 brcmf_sdiod_writeb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
855 clkreq, &err);
856 brcmf_dbg(SDIO, "CLKCTL: turned OFF\n");
857 if (err) {
858 brcmf_err("Failed access turning clock off: %d\n",
859 err);
860 return -EBADE;
861 }
862 }
863 return 0;
864 }
865
866
867 static int brcmf_sdio_sdclk(struct brcmf_sdio *bus, bool on)
868 {
869 brcmf_dbg(SDIO, "Enter\n");
870
871 if (on)
872 bus->clkstate = CLK_SDONLY;
873 else
874 bus->clkstate = CLK_NONE;
875
876 return 0;
877 }
878
879
880 static int brcmf_sdio_clkctl(struct brcmf_sdio *bus, uint target, bool pendok)
881 {
882 #ifdef DEBUG
883 uint oldstate = bus->clkstate;
884 #endif
885
886 brcmf_dbg(SDIO, "Enter\n");
887
888
889 if (bus->clkstate == target)
890 return 0;
891
892 switch (target) {
893 case CLK_AVAIL:
894
895 if (bus->clkstate == CLK_NONE)
896 brcmf_sdio_sdclk(bus, true);
897
898 brcmf_sdio_htclk(bus, true, pendok);
899 break;
900
901 case CLK_SDONLY:
902
903 if (bus->clkstate == CLK_NONE)
904 brcmf_sdio_sdclk(bus, true);
905 else if (bus->clkstate == CLK_AVAIL)
906 brcmf_sdio_htclk(bus, false, false);
907 else
908 brcmf_err("request for %d -> %d\n",
909 bus->clkstate, target);
910 break;
911
912 case CLK_NONE:
913
914 if (bus->clkstate == CLK_AVAIL)
915 brcmf_sdio_htclk(bus, false, false);
916
917 brcmf_sdio_sdclk(bus, false);
918 break;
919 }
920 #ifdef DEBUG
921 brcmf_dbg(SDIO, "%d -> %d\n", oldstate, bus->clkstate);
922 #endif
923
924 return 0;
925 }
926
927 static int
928 brcmf_sdio_bus_sleep(struct brcmf_sdio *bus, bool sleep, bool pendok)
929 {
930 int err = 0;
931 u8 clkcsr;
932
933 brcmf_dbg(SDIO, "Enter: request %s currently %s\n",
934 (sleep ? "SLEEP" : "WAKE"),
935 (bus->sleeping ? "SLEEP" : "WAKE"));
936
937
938 if (bus->sr_enabled) {
939
940 if (sleep == bus->sleeping)
941 goto end;
942
943
944 if (sleep) {
945 clkcsr = brcmf_sdiod_readb(bus->sdiodev,
946 SBSDIO_FUNC1_CHIPCLKCSR,
947 &err);
948 if ((clkcsr & SBSDIO_CSR_MASK) == 0) {
949 brcmf_dbg(SDIO, "no clock, set ALP\n");
950 brcmf_sdiod_writeb(bus->sdiodev,
951 SBSDIO_FUNC1_CHIPCLKCSR,
952 SBSDIO_ALP_AVAIL_REQ, &err);
953 }
954 err = brcmf_sdio_kso_control(bus, false);
955 } else {
956 err = brcmf_sdio_kso_control(bus, true);
957 }
958 if (err) {
959 brcmf_err("error while changing bus sleep state %d\n",
960 err);
961 goto done;
962 }
963 }
964
965 end:
966
967 if (sleep) {
968 if (!bus->sr_enabled)
969 brcmf_sdio_clkctl(bus, CLK_NONE, pendok);
970 } else {
971 brcmf_sdio_clkctl(bus, CLK_AVAIL, pendok);
972 brcmf_sdio_wd_timer(bus, true);
973 }
974 bus->sleeping = sleep;
975 brcmf_dbg(SDIO, "new state %s\n",
976 (sleep ? "SLEEP" : "WAKE"));
977 done:
978 brcmf_dbg(SDIO, "Exit: err=%d\n", err);
979 return err;
980
981 }
982
983 #ifdef DEBUG
984 static inline bool brcmf_sdio_valid_shared_address(u32 addr)
985 {
986 return !(addr == 0 || ((~addr >> 16) & 0xffff) == (addr & 0xffff));
987 }
988
989 static int brcmf_sdio_readshared(struct brcmf_sdio *bus,
990 struct sdpcm_shared *sh)
991 {
992 u32 addr = 0;
993 int rv;
994 u32 shaddr = 0;
995 struct sdpcm_shared_le sh_le;
996 __le32 addr_le;
997
998 sdio_claim_host(bus->sdiodev->func1);
999 brcmf_sdio_bus_sleep(bus, false, false);
1000
1001
1002
1003
1004
1005 shaddr = bus->ci->rambase + bus->ci->ramsize - 4;
1006 if (!bus->ci->rambase && brcmf_chip_sr_capable(bus->ci))
1007 shaddr -= bus->ci->srsize;
1008 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, shaddr,
1009 (u8 *)&addr_le, 4);
1010 if (rv < 0)
1011 goto fail;
1012
1013
1014
1015
1016
1017 addr = le32_to_cpu(addr_le);
1018 if (!brcmf_sdio_valid_shared_address(addr)) {
1019 brcmf_err("invalid sdpcm_shared address 0x%08X\n", addr);
1020 rv = -EINVAL;
1021 goto fail;
1022 }
1023
1024 brcmf_dbg(INFO, "sdpcm_shared address 0x%08X\n", addr);
1025
1026
1027 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, (u8 *)&sh_le,
1028 sizeof(struct sdpcm_shared_le));
1029 if (rv < 0)
1030 goto fail;
1031
1032 sdio_release_host(bus->sdiodev->func1);
1033
1034
1035 sh->flags = le32_to_cpu(sh_le.flags);
1036 sh->trap_addr = le32_to_cpu(sh_le.trap_addr);
1037 sh->assert_exp_addr = le32_to_cpu(sh_le.assert_exp_addr);
1038 sh->assert_file_addr = le32_to_cpu(sh_le.assert_file_addr);
1039 sh->assert_line = le32_to_cpu(sh_le.assert_line);
1040 sh->console_addr = le32_to_cpu(sh_le.console_addr);
1041 sh->msgtrace_addr = le32_to_cpu(sh_le.msgtrace_addr);
1042
1043 if ((sh->flags & SDPCM_SHARED_VERSION_MASK) > SDPCM_SHARED_VERSION) {
1044 brcmf_err("sdpcm shared version unsupported: dhd %d dongle %d\n",
1045 SDPCM_SHARED_VERSION,
1046 sh->flags & SDPCM_SHARED_VERSION_MASK);
1047 return -EPROTO;
1048 }
1049 return 0;
1050
1051 fail:
1052 brcmf_err("unable to obtain sdpcm_shared info: rv=%d (addr=0x%x)\n",
1053 rv, addr);
1054 sdio_release_host(bus->sdiodev->func1);
1055 return rv;
1056 }
1057
1058 static void brcmf_sdio_get_console_addr(struct brcmf_sdio *bus)
1059 {
1060 struct sdpcm_shared sh;
1061
1062 if (brcmf_sdio_readshared(bus, &sh) == 0)
1063 bus->console_addr = sh.console_addr;
1064 }
1065 #else
1066 static void brcmf_sdio_get_console_addr(struct brcmf_sdio *bus)
1067 {
1068 }
1069 #endif
1070
1071 static u32 brcmf_sdio_hostmail(struct brcmf_sdio *bus)
1072 {
1073 struct brcmf_sdio_dev *sdiod = bus->sdiodev;
1074 struct brcmf_core *core = bus->sdio_core;
1075 u32 intstatus = 0;
1076 u32 hmb_data;
1077 u8 fcbits;
1078 int ret;
1079
1080 brcmf_dbg(SDIO, "Enter\n");
1081
1082
1083 hmb_data = brcmf_sdiod_readl(sdiod,
1084 core->base + SD_REG(tohostmailboxdata),
1085 &ret);
1086
1087 if (!ret)
1088 brcmf_sdiod_writel(sdiod, core->base + SD_REG(tosbmailbox),
1089 SMB_INT_ACK, &ret);
1090
1091 bus->sdcnt.f1regdata += 2;
1092
1093
1094 if (hmb_data & HMB_DATA_FWHALT) {
1095 brcmf_dbg(SDIO, "mailbox indicates firmware halted\n");
1096 brcmf_fw_crashed(&sdiod->func1->dev);
1097 }
1098
1099
1100 if (hmb_data & HMB_DATA_NAKHANDLED) {
1101 brcmf_dbg(SDIO, "Dongle reports NAK handled, expect rtx of %d\n",
1102 bus->rx_seq);
1103 if (!bus->rxskip)
1104 brcmf_err("unexpected NAKHANDLED!\n");
1105
1106 bus->rxskip = false;
1107 intstatus |= I_HMB_FRAME_IND;
1108 }
1109
1110
1111
1112
1113 if (hmb_data & (HMB_DATA_DEVREADY | HMB_DATA_FWREADY)) {
1114 bus->sdpcm_ver =
1115 (hmb_data & HMB_DATA_VERSION_MASK) >>
1116 HMB_DATA_VERSION_SHIFT;
1117 if (bus->sdpcm_ver != SDPCM_PROT_VERSION)
1118 brcmf_err("Version mismatch, dongle reports %d, "
1119 "expecting %d\n",
1120 bus->sdpcm_ver, SDPCM_PROT_VERSION);
1121 else
1122 brcmf_dbg(SDIO, "Dongle ready, protocol version %d\n",
1123 bus->sdpcm_ver);
1124
1125
1126
1127
1128
1129 brcmf_sdio_get_console_addr(bus);
1130 }
1131
1132
1133
1134
1135
1136
1137 if (hmb_data & HMB_DATA_FC) {
1138 fcbits = (hmb_data & HMB_DATA_FCDATA_MASK) >>
1139 HMB_DATA_FCDATA_SHIFT;
1140
1141 if (fcbits & ~bus->flowcontrol)
1142 bus->sdcnt.fc_xoff++;
1143
1144 if (bus->flowcontrol & ~fcbits)
1145 bus->sdcnt.fc_xon++;
1146
1147 bus->sdcnt.fc_rcvd++;
1148 bus->flowcontrol = fcbits;
1149 }
1150
1151
1152 if (hmb_data & ~(HMB_DATA_DEVREADY |
1153 HMB_DATA_NAKHANDLED |
1154 HMB_DATA_FC |
1155 HMB_DATA_FWREADY |
1156 HMB_DATA_FWHALT |
1157 HMB_DATA_FCDATA_MASK | HMB_DATA_VERSION_MASK))
1158 brcmf_err("Unknown mailbox data content: 0x%02x\n",
1159 hmb_data);
1160
1161 return intstatus;
1162 }
1163
1164 static void brcmf_sdio_rxfail(struct brcmf_sdio *bus, bool abort, bool rtx)
1165 {
1166 struct brcmf_sdio_dev *sdiod = bus->sdiodev;
1167 struct brcmf_core *core = bus->sdio_core;
1168 uint retries = 0;
1169 u16 lastrbc;
1170 u8 hi, lo;
1171 int err;
1172
1173 brcmf_err("%sterminate frame%s\n",
1174 abort ? "abort command, " : "",
1175 rtx ? ", send NAK" : "");
1176
1177 if (abort)
1178 brcmf_sdiod_abort(bus->sdiodev, bus->sdiodev->func2);
1179
1180 brcmf_sdiod_writeb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL, SFC_RF_TERM,
1181 &err);
1182 bus->sdcnt.f1regdata++;
1183
1184
1185 for (lastrbc = retries = 0xffff; retries > 0; retries--) {
1186 hi = brcmf_sdiod_readb(bus->sdiodev, SBSDIO_FUNC1_RFRAMEBCHI,
1187 &err);
1188 lo = brcmf_sdiod_readb(bus->sdiodev, SBSDIO_FUNC1_RFRAMEBCLO,
1189 &err);
1190 bus->sdcnt.f1regdata += 2;
1191
1192 if ((hi == 0) && (lo == 0))
1193 break;
1194
1195 if ((hi > (lastrbc >> 8)) && (lo > (lastrbc & 0x00ff))) {
1196 brcmf_err("count growing: last 0x%04x now 0x%04x\n",
1197 lastrbc, (hi << 8) + lo);
1198 }
1199 lastrbc = (hi << 8) + lo;
1200 }
1201
1202 if (!retries)
1203 brcmf_err("count never zeroed: last 0x%04x\n", lastrbc);
1204 else
1205 brcmf_dbg(SDIO, "flush took %d iterations\n", 0xffff - retries);
1206
1207 if (rtx) {
1208 bus->sdcnt.rxrtx++;
1209 brcmf_sdiod_writel(sdiod, core->base + SD_REG(tosbmailbox),
1210 SMB_NAK, &err);
1211
1212 bus->sdcnt.f1regdata++;
1213 if (err == 0)
1214 bus->rxskip = true;
1215 }
1216
1217
1218 bus->cur_read.len = 0;
1219 }
1220
1221 static void brcmf_sdio_txfail(struct brcmf_sdio *bus)
1222 {
1223 struct brcmf_sdio_dev *sdiodev = bus->sdiodev;
1224 u8 i, hi, lo;
1225
1226
1227 brcmf_err("sdio error, abort command and terminate frame\n");
1228 bus->sdcnt.tx_sderrs++;
1229
1230 brcmf_sdiod_abort(sdiodev, sdiodev->func2);
1231 brcmf_sdiod_writeb(sdiodev, SBSDIO_FUNC1_FRAMECTRL, SFC_WF_TERM, NULL);
1232 bus->sdcnt.f1regdata++;
1233
1234 for (i = 0; i < 3; i++) {
1235 hi = brcmf_sdiod_readb(sdiodev, SBSDIO_FUNC1_WFRAMEBCHI, NULL);
1236 lo = brcmf_sdiod_readb(sdiodev, SBSDIO_FUNC1_WFRAMEBCLO, NULL);
1237 bus->sdcnt.f1regdata += 2;
1238 if ((hi == 0) && (lo == 0))
1239 break;
1240 }
1241 }
1242
1243
1244 static uint brcmf_sdio_glom_len(struct brcmf_sdio *bus)
1245 {
1246 struct sk_buff *p;
1247 uint total;
1248
1249 total = 0;
1250 skb_queue_walk(&bus->glom, p)
1251 total += p->len;
1252 return total;
1253 }
1254
1255 static void brcmf_sdio_free_glom(struct brcmf_sdio *bus)
1256 {
1257 struct sk_buff *cur, *next;
1258
1259 skb_queue_walk_safe(&bus->glom, cur, next) {
1260 skb_unlink(cur, &bus->glom);
1261 brcmu_pkt_buf_free_skb(cur);
1262 }
1263 }
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292 #define SDPCM_HWHDR_LEN 4
1293 #define SDPCM_HWEXT_LEN 8
1294 #define SDPCM_SWHDR_LEN 8
1295 #define SDPCM_HDRLEN (SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN)
1296
1297 #define SDPCM_SEQ_MASK 0x000000ff
1298 #define SDPCM_SEQ_WRAP 256
1299 #define SDPCM_CHANNEL_MASK 0x00000f00
1300 #define SDPCM_CHANNEL_SHIFT 8
1301 #define SDPCM_CONTROL_CHANNEL 0
1302 #define SDPCM_EVENT_CHANNEL 1
1303 #define SDPCM_DATA_CHANNEL 2
1304 #define SDPCM_GLOM_CHANNEL 3
1305 #define SDPCM_TEST_CHANNEL 15
1306 #define SDPCM_GLOMDESC(p) (((u8 *)p)[1] & 0x80)
1307 #define SDPCM_NEXTLEN_MASK 0x00ff0000
1308 #define SDPCM_NEXTLEN_SHIFT 16
1309 #define SDPCM_DOFFSET_MASK 0xff000000
1310 #define SDPCM_DOFFSET_SHIFT 24
1311 #define SDPCM_FCMASK_MASK 0x000000ff
1312 #define SDPCM_WINDOW_MASK 0x0000ff00
1313 #define SDPCM_WINDOW_SHIFT 8
1314
1315 static inline u8 brcmf_sdio_getdatoffset(u8 *swheader)
1316 {
1317 u32 hdrvalue;
1318 hdrvalue = *(u32 *)swheader;
1319 return (u8)((hdrvalue & SDPCM_DOFFSET_MASK) >> SDPCM_DOFFSET_SHIFT);
1320 }
1321
1322 static inline bool brcmf_sdio_fromevntchan(u8 *swheader)
1323 {
1324 u32 hdrvalue;
1325 u8 ret;
1326
1327 hdrvalue = *(u32 *)swheader;
1328 ret = (u8)((hdrvalue & SDPCM_CHANNEL_MASK) >> SDPCM_CHANNEL_SHIFT);
1329
1330 return (ret == SDPCM_EVENT_CHANNEL);
1331 }
1332
1333 static int brcmf_sdio_hdparse(struct brcmf_sdio *bus, u8 *header,
1334 struct brcmf_sdio_hdrinfo *rd,
1335 enum brcmf_sdio_frmtype type)
1336 {
1337 u16 len, checksum;
1338 u8 rx_seq, fc, tx_seq_max;
1339 u32 swheader;
1340
1341 trace_brcmf_sdpcm_hdr(SDPCM_RX, header);
1342
1343
1344 len = get_unaligned_le16(header);
1345 checksum = get_unaligned_le16(header + sizeof(u16));
1346
1347 if (!(len | checksum)) {
1348 bus->rxpending = false;
1349 return -ENODATA;
1350 }
1351 if ((u16)(~(len ^ checksum))) {
1352 brcmf_err("HW header checksum error\n");
1353 bus->sdcnt.rx_badhdr++;
1354 brcmf_sdio_rxfail(bus, false, false);
1355 return -EIO;
1356 }
1357 if (len < SDPCM_HDRLEN) {
1358 brcmf_err("HW header length error\n");
1359 return -EPROTO;
1360 }
1361 if (type == BRCMF_SDIO_FT_SUPER &&
1362 (roundup(len, bus->blocksize) != rd->len)) {
1363 brcmf_err("HW superframe header length error\n");
1364 return -EPROTO;
1365 }
1366 if (type == BRCMF_SDIO_FT_SUB && len > rd->len) {
1367 brcmf_err("HW subframe header length error\n");
1368 return -EPROTO;
1369 }
1370 rd->len = len;
1371
1372
1373 header += SDPCM_HWHDR_LEN;
1374 swheader = le32_to_cpu(*(__le32 *)header);
1375 if (type == BRCMF_SDIO_FT_SUPER && SDPCM_GLOMDESC(header)) {
1376 brcmf_err("Glom descriptor found in superframe head\n");
1377 rd->len = 0;
1378 return -EINVAL;
1379 }
1380 rx_seq = (u8)(swheader & SDPCM_SEQ_MASK);
1381 rd->channel = (swheader & SDPCM_CHANNEL_MASK) >> SDPCM_CHANNEL_SHIFT;
1382 if (len > MAX_RX_DATASZ && rd->channel != SDPCM_CONTROL_CHANNEL &&
1383 type != BRCMF_SDIO_FT_SUPER) {
1384 brcmf_err("HW header length too long\n");
1385 bus->sdcnt.rx_toolong++;
1386 brcmf_sdio_rxfail(bus, false, false);
1387 rd->len = 0;
1388 return -EPROTO;
1389 }
1390 if (type == BRCMF_SDIO_FT_SUPER && rd->channel != SDPCM_GLOM_CHANNEL) {
1391 brcmf_err("Wrong channel for superframe\n");
1392 rd->len = 0;
1393 return -EINVAL;
1394 }
1395 if (type == BRCMF_SDIO_FT_SUB && rd->channel != SDPCM_DATA_CHANNEL &&
1396 rd->channel != SDPCM_EVENT_CHANNEL) {
1397 brcmf_err("Wrong channel for subframe\n");
1398 rd->len = 0;
1399 return -EINVAL;
1400 }
1401 rd->dat_offset = brcmf_sdio_getdatoffset(header);
1402 if (rd->dat_offset < SDPCM_HDRLEN || rd->dat_offset > rd->len) {
1403 brcmf_err("seq %d: bad data offset\n", rx_seq);
1404 bus->sdcnt.rx_badhdr++;
1405 brcmf_sdio_rxfail(bus, false, false);
1406 rd->len = 0;
1407 return -ENXIO;
1408 }
1409 if (rd->seq_num != rx_seq) {
1410 brcmf_dbg(SDIO, "seq %d, expected %d\n", rx_seq, rd->seq_num);
1411 bus->sdcnt.rx_badseq++;
1412 rd->seq_num = rx_seq;
1413 }
1414
1415 if (type == BRCMF_SDIO_FT_SUB)
1416 return 0;
1417 rd->len_nxtfrm = (swheader & SDPCM_NEXTLEN_MASK) >> SDPCM_NEXTLEN_SHIFT;
1418 if (rd->len_nxtfrm << 4 > MAX_RX_DATASZ) {
1419
1420 if (rd->channel != SDPCM_GLOM_CHANNEL)
1421 brcmf_err("seq %d: next length error\n", rx_seq);
1422 rd->len_nxtfrm = 0;
1423 }
1424 swheader = le32_to_cpu(*(__le32 *)(header + 4));
1425 fc = swheader & SDPCM_FCMASK_MASK;
1426 if (bus->flowcontrol != fc) {
1427 if (~bus->flowcontrol & fc)
1428 bus->sdcnt.fc_xoff++;
1429 if (bus->flowcontrol & ~fc)
1430 bus->sdcnt.fc_xon++;
1431 bus->sdcnt.fc_rcvd++;
1432 bus->flowcontrol = fc;
1433 }
1434 tx_seq_max = (swheader & SDPCM_WINDOW_MASK) >> SDPCM_WINDOW_SHIFT;
1435 if ((u8)(tx_seq_max - bus->tx_seq) > 0x40) {
1436 brcmf_err("seq %d: max tx seq number error\n", rx_seq);
1437 tx_seq_max = bus->tx_seq + 2;
1438 }
1439 bus->tx_max = tx_seq_max;
1440
1441 return 0;
1442 }
1443
1444 static inline void brcmf_sdio_update_hwhdr(u8 *header, u16 frm_length)
1445 {
1446 *(__le16 *)header = cpu_to_le16(frm_length);
1447 *(((__le16 *)header) + 1) = cpu_to_le16(~frm_length);
1448 }
1449
1450 static void brcmf_sdio_hdpack(struct brcmf_sdio *bus, u8 *header,
1451 struct brcmf_sdio_hdrinfo *hd_info)
1452 {
1453 u32 hdrval;
1454 u8 hdr_offset;
1455
1456 brcmf_sdio_update_hwhdr(header, hd_info->len);
1457 hdr_offset = SDPCM_HWHDR_LEN;
1458
1459 if (bus->txglom) {
1460 hdrval = (hd_info->len - hdr_offset) | (hd_info->lastfrm << 24);
1461 *((__le32 *)(header + hdr_offset)) = cpu_to_le32(hdrval);
1462 hdrval = (u16)hd_info->tail_pad << 16;
1463 *(((__le32 *)(header + hdr_offset)) + 1) = cpu_to_le32(hdrval);
1464 hdr_offset += SDPCM_HWEXT_LEN;
1465 }
1466
1467 hdrval = hd_info->seq_num;
1468 hdrval |= (hd_info->channel << SDPCM_CHANNEL_SHIFT) &
1469 SDPCM_CHANNEL_MASK;
1470 hdrval |= (hd_info->dat_offset << SDPCM_DOFFSET_SHIFT) &
1471 SDPCM_DOFFSET_MASK;
1472 *((__le32 *)(header + hdr_offset)) = cpu_to_le32(hdrval);
1473 *(((__le32 *)(header + hdr_offset)) + 1) = 0;
1474 trace_brcmf_sdpcm_hdr(SDPCM_TX + !!(bus->txglom), header);
1475 }
1476
1477 static u8 brcmf_sdio_rxglom(struct brcmf_sdio *bus, u8 rxseq)
1478 {
1479 u16 dlen, totlen;
1480 u8 *dptr, num = 0;
1481 u16 sublen;
1482 struct sk_buff *pfirst, *pnext;
1483
1484 int errcode;
1485 u8 doff;
1486
1487 struct brcmf_sdio_hdrinfo rd_new;
1488
1489
1490
1491
1492 brcmf_dbg(SDIO, "start: glomd %p glom %p\n",
1493 bus->glomd, skb_peek(&bus->glom));
1494
1495
1496 if (bus->glomd) {
1497 pfirst = pnext = NULL;
1498 dlen = (u16) (bus->glomd->len);
1499 dptr = bus->glomd->data;
1500 if (!dlen || (dlen & 1)) {
1501 brcmf_err("bad glomd len(%d), ignore descriptor\n",
1502 dlen);
1503 dlen = 0;
1504 }
1505
1506 for (totlen = num = 0; dlen; num++) {
1507
1508 sublen = get_unaligned_le16(dptr);
1509 dlen -= sizeof(u16);
1510 dptr += sizeof(u16);
1511 if ((sublen < SDPCM_HDRLEN) ||
1512 ((num == 0) && (sublen < (2 * SDPCM_HDRLEN)))) {
1513 brcmf_err("descriptor len %d bad: %d\n",
1514 num, sublen);
1515 pnext = NULL;
1516 break;
1517 }
1518 if (sublen % bus->sgentry_align) {
1519 brcmf_err("sublen %d not multiple of %d\n",
1520 sublen, bus->sgentry_align);
1521 }
1522 totlen += sublen;
1523
1524
1525
1526 if (!dlen) {
1527 sublen +=
1528 (roundup(totlen, bus->blocksize) - totlen);
1529 totlen = roundup(totlen, bus->blocksize);
1530 }
1531
1532
1533 pnext = brcmu_pkt_buf_get_skb(sublen + bus->sgentry_align);
1534 if (pnext == NULL) {
1535 brcmf_err("bcm_pkt_buf_get_skb failed, num %d len %d\n",
1536 num, sublen);
1537 break;
1538 }
1539 skb_queue_tail(&bus->glom, pnext);
1540
1541
1542 pkt_align(pnext, sublen, bus->sgentry_align);
1543 }
1544
1545
1546
1547 if (pnext) {
1548 brcmf_dbg(GLOM, "allocated %d-byte packet chain for %d subframes\n",
1549 totlen, num);
1550 if (BRCMF_GLOM_ON() && bus->cur_read.len &&
1551 totlen != bus->cur_read.len) {
1552 brcmf_dbg(GLOM, "glomdesc mismatch: nextlen %d glomdesc %d rxseq %d\n",
1553 bus->cur_read.len, totlen, rxseq);
1554 }
1555 pfirst = pnext = NULL;
1556 } else {
1557 brcmf_sdio_free_glom(bus);
1558 num = 0;
1559 }
1560
1561
1562 brcmu_pkt_buf_free_skb(bus->glomd);
1563 bus->glomd = NULL;
1564 bus->cur_read.len = 0;
1565 }
1566
1567
1568
1569 if (!skb_queue_empty(&bus->glom)) {
1570 if (BRCMF_GLOM_ON()) {
1571 brcmf_dbg(GLOM, "try superframe read, packet chain:\n");
1572 skb_queue_walk(&bus->glom, pnext) {
1573 brcmf_dbg(GLOM, " %p: %p len 0x%04x (%d)\n",
1574 pnext, (u8 *) (pnext->data),
1575 pnext->len, pnext->len);
1576 }
1577 }
1578
1579 pfirst = skb_peek(&bus->glom);
1580 dlen = (u16) brcmf_sdio_glom_len(bus);
1581
1582
1583
1584
1585
1586 sdio_claim_host(bus->sdiodev->func1);
1587 errcode = brcmf_sdiod_recv_chain(bus->sdiodev,
1588 &bus->glom, dlen);
1589 sdio_release_host(bus->sdiodev->func1);
1590 bus->sdcnt.f2rxdata++;
1591
1592
1593 if (errcode < 0) {
1594 brcmf_err("glom read of %d bytes failed: %d\n",
1595 dlen, errcode);
1596
1597 sdio_claim_host(bus->sdiodev->func1);
1598 brcmf_sdio_rxfail(bus, true, false);
1599 bus->sdcnt.rxglomfail++;
1600 brcmf_sdio_free_glom(bus);
1601 sdio_release_host(bus->sdiodev->func1);
1602 return 0;
1603 }
1604
1605 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
1606 pfirst->data, min_t(int, pfirst->len, 48),
1607 "SUPERFRAME:\n");
1608
1609 rd_new.seq_num = rxseq;
1610 rd_new.len = dlen;
1611 sdio_claim_host(bus->sdiodev->func1);
1612 errcode = brcmf_sdio_hdparse(bus, pfirst->data, &rd_new,
1613 BRCMF_SDIO_FT_SUPER);
1614 sdio_release_host(bus->sdiodev->func1);
1615 bus->cur_read.len = rd_new.len_nxtfrm << 4;
1616
1617
1618 skb_pull(pfirst, rd_new.dat_offset);
1619 num = 0;
1620
1621
1622 skb_queue_walk(&bus->glom, pnext) {
1623
1624 if (errcode)
1625 break;
1626
1627 rd_new.len = pnext->len;
1628 rd_new.seq_num = rxseq++;
1629 sdio_claim_host(bus->sdiodev->func1);
1630 errcode = brcmf_sdio_hdparse(bus, pnext->data, &rd_new,
1631 BRCMF_SDIO_FT_SUB);
1632 sdio_release_host(bus->sdiodev->func1);
1633 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
1634 pnext->data, 32, "subframe:\n");
1635
1636 num++;
1637 }
1638
1639 if (errcode) {
1640
1641 sdio_claim_host(bus->sdiodev->func1);
1642 brcmf_sdio_rxfail(bus, true, false);
1643 bus->sdcnt.rxglomfail++;
1644 brcmf_sdio_free_glom(bus);
1645 sdio_release_host(bus->sdiodev->func1);
1646 bus->cur_read.len = 0;
1647 return 0;
1648 }
1649
1650
1651
1652 skb_queue_walk_safe(&bus->glom, pfirst, pnext) {
1653 dptr = (u8 *) (pfirst->data);
1654 sublen = get_unaligned_le16(dptr);
1655 doff = brcmf_sdio_getdatoffset(&dptr[SDPCM_HWHDR_LEN]);
1656
1657 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_DATA_ON(),
1658 dptr, pfirst->len,
1659 "Rx Subframe Data:\n");
1660
1661 __skb_trim(pfirst, sublen);
1662 skb_pull(pfirst, doff);
1663
1664 if (pfirst->len == 0) {
1665 skb_unlink(pfirst, &bus->glom);
1666 brcmu_pkt_buf_free_skb(pfirst);
1667 continue;
1668 }
1669
1670 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
1671 pfirst->data,
1672 min_t(int, pfirst->len, 32),
1673 "subframe %d to stack, %p (%p/%d) nxt/lnk %p/%p\n",
1674 bus->glom.qlen, pfirst, pfirst->data,
1675 pfirst->len, pfirst->next,
1676 pfirst->prev);
1677 skb_unlink(pfirst, &bus->glom);
1678 if (brcmf_sdio_fromevntchan(&dptr[SDPCM_HWHDR_LEN]))
1679 brcmf_rx_event(bus->sdiodev->dev, pfirst);
1680 else
1681 brcmf_rx_frame(bus->sdiodev->dev, pfirst,
1682 false);
1683 bus->sdcnt.rxglompkts++;
1684 }
1685
1686 bus->sdcnt.rxglomframes++;
1687 }
1688 return num;
1689 }
1690
1691 static int brcmf_sdio_dcmd_resp_wait(struct brcmf_sdio *bus, uint *condition,
1692 bool *pending)
1693 {
1694 DECLARE_WAITQUEUE(wait, current);
1695 int timeout = DCMD_RESP_TIMEOUT;
1696
1697
1698 add_wait_queue(&bus->dcmd_resp_wait, &wait);
1699 set_current_state(TASK_INTERRUPTIBLE);
1700
1701 while (!(*condition) && (!signal_pending(current) && timeout))
1702 timeout = schedule_timeout(timeout);
1703
1704 if (signal_pending(current))
1705 *pending = true;
1706
1707 set_current_state(TASK_RUNNING);
1708 remove_wait_queue(&bus->dcmd_resp_wait, &wait);
1709
1710 return timeout;
1711 }
1712
1713 static int brcmf_sdio_dcmd_resp_wake(struct brcmf_sdio *bus)
1714 {
1715 wake_up_interruptible(&bus->dcmd_resp_wait);
1716
1717 return 0;
1718 }
1719 static void
1720 brcmf_sdio_read_control(struct brcmf_sdio *bus, u8 *hdr, uint len, uint doff)
1721 {
1722 uint rdlen, pad;
1723 u8 *buf = NULL, *rbuf;
1724 int sdret;
1725
1726 brcmf_dbg(SDIO, "Enter\n");
1727 if (bus->rxblen)
1728 buf = vzalloc(bus->rxblen);
1729 if (!buf)
1730 goto done;
1731
1732 rbuf = bus->rxbuf;
1733 pad = ((unsigned long)rbuf % bus->head_align);
1734 if (pad)
1735 rbuf += (bus->head_align - pad);
1736
1737
1738 memcpy(buf, hdr, BRCMF_FIRSTREAD);
1739 if (len <= BRCMF_FIRSTREAD)
1740 goto gotpkt;
1741
1742
1743 rdlen = len - BRCMF_FIRSTREAD;
1744 if (bus->roundup && bus->blocksize && (rdlen > bus->blocksize)) {
1745 pad = bus->blocksize - (rdlen % bus->blocksize);
1746 if ((pad <= bus->roundup) && (pad < bus->blocksize) &&
1747 ((len + pad) < bus->sdiodev->bus_if->maxctl))
1748 rdlen += pad;
1749 } else if (rdlen % bus->head_align) {
1750 rdlen += bus->head_align - (rdlen % bus->head_align);
1751 }
1752
1753
1754 if ((rdlen + BRCMF_FIRSTREAD) > bus->sdiodev->bus_if->maxctl) {
1755 brcmf_err("%d-byte control read exceeds %d-byte buffer\n",
1756 rdlen, bus->sdiodev->bus_if->maxctl);
1757 brcmf_sdio_rxfail(bus, false, false);
1758 goto done;
1759 }
1760
1761 if ((len - doff) > bus->sdiodev->bus_if->maxctl) {
1762 brcmf_err("%d-byte ctl frame (%d-byte ctl data) exceeds %d-byte limit\n",
1763 len, len - doff, bus->sdiodev->bus_if->maxctl);
1764 bus->sdcnt.rx_toolong++;
1765 brcmf_sdio_rxfail(bus, false, false);
1766 goto done;
1767 }
1768
1769
1770 sdret = brcmf_sdiod_recv_buf(bus->sdiodev, rbuf, rdlen);
1771 bus->sdcnt.f2rxdata++;
1772
1773
1774 if (sdret < 0) {
1775 brcmf_err("read %d control bytes failed: %d\n",
1776 rdlen, sdret);
1777 bus->sdcnt.rxc_errors++;
1778 brcmf_sdio_rxfail(bus, true, true);
1779 goto done;
1780 } else
1781 memcpy(buf + BRCMF_FIRSTREAD, rbuf, rdlen);
1782
1783 gotpkt:
1784
1785 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_CTL_ON(),
1786 buf, len, "RxCtrl:\n");
1787
1788
1789 spin_lock_bh(&bus->rxctl_lock);
1790 if (bus->rxctl) {
1791 brcmf_err("last control frame is being processed.\n");
1792 spin_unlock_bh(&bus->rxctl_lock);
1793 vfree(buf);
1794 goto done;
1795 }
1796 bus->rxctl = buf + doff;
1797 bus->rxctl_orig = buf;
1798 bus->rxlen = len - doff;
1799 spin_unlock_bh(&bus->rxctl_lock);
1800
1801 done:
1802
1803 brcmf_sdio_dcmd_resp_wake(bus);
1804 }
1805
1806
1807 static void brcmf_sdio_pad(struct brcmf_sdio *bus, u16 *pad, u16 *rdlen)
1808 {
1809 if (bus->roundup && bus->blocksize && *rdlen > bus->blocksize) {
1810 *pad = bus->blocksize - (*rdlen % bus->blocksize);
1811 if (*pad <= bus->roundup && *pad < bus->blocksize &&
1812 *rdlen + *pad + BRCMF_FIRSTREAD < MAX_RX_DATASZ)
1813 *rdlen += *pad;
1814 } else if (*rdlen % bus->head_align) {
1815 *rdlen += bus->head_align - (*rdlen % bus->head_align);
1816 }
1817 }
1818
1819 static uint brcmf_sdio_readframes(struct brcmf_sdio *bus, uint maxframes)
1820 {
1821 struct sk_buff *pkt;
1822 u16 pad;
1823 uint rxleft = 0;
1824 int ret;
1825 uint rxcount = 0;
1826 struct brcmf_sdio_hdrinfo *rd = &bus->cur_read, rd_new;
1827 u8 head_read = 0;
1828
1829 brcmf_dbg(SDIO, "Enter\n");
1830
1831
1832 bus->rxpending = true;
1833
1834 for (rd->seq_num = bus->rx_seq, rxleft = maxframes;
1835 !bus->rxskip && rxleft && bus->sdiodev->state == BRCMF_SDIOD_DATA;
1836 rd->seq_num++, rxleft--) {
1837
1838
1839 if (bus->glomd || !skb_queue_empty(&bus->glom)) {
1840 u8 cnt;
1841 brcmf_dbg(GLOM, "calling rxglom: glomd %p, glom %p\n",
1842 bus->glomd, skb_peek(&bus->glom));
1843 cnt = brcmf_sdio_rxglom(bus, rd->seq_num);
1844 brcmf_dbg(GLOM, "rxglom returned %d\n", cnt);
1845 rd->seq_num += cnt - 1;
1846 rxleft = (rxleft > cnt) ? (rxleft - cnt) : 1;
1847 continue;
1848 }
1849
1850 rd->len_left = rd->len;
1851
1852 sdio_claim_host(bus->sdiodev->func1);
1853 if (!rd->len) {
1854 ret = brcmf_sdiod_recv_buf(bus->sdiodev,
1855 bus->rxhdr, BRCMF_FIRSTREAD);
1856 bus->sdcnt.f2rxhdrs++;
1857 if (ret < 0) {
1858 brcmf_err("RXHEADER FAILED: %d\n",
1859 ret);
1860 bus->sdcnt.rx_hdrfail++;
1861 brcmf_sdio_rxfail(bus, true, true);
1862 sdio_release_host(bus->sdiodev->func1);
1863 continue;
1864 }
1865
1866 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() || BRCMF_HDRS_ON(),
1867 bus->rxhdr, SDPCM_HDRLEN,
1868 "RxHdr:\n");
1869
1870 if (brcmf_sdio_hdparse(bus, bus->rxhdr, rd,
1871 BRCMF_SDIO_FT_NORMAL)) {
1872 sdio_release_host(bus->sdiodev->func1);
1873 if (!bus->rxpending)
1874 break;
1875 else
1876 continue;
1877 }
1878
1879 if (rd->channel == SDPCM_CONTROL_CHANNEL) {
1880 brcmf_sdio_read_control(bus, bus->rxhdr,
1881 rd->len,
1882 rd->dat_offset);
1883
1884 rd->len = rd->len_nxtfrm << 4;
1885 rd->len_nxtfrm = 0;
1886
1887 rd->channel = SDPCM_EVENT_CHANNEL;
1888 sdio_release_host(bus->sdiodev->func1);
1889 continue;
1890 }
1891 rd->len_left = rd->len > BRCMF_FIRSTREAD ?
1892 rd->len - BRCMF_FIRSTREAD : 0;
1893 head_read = BRCMF_FIRSTREAD;
1894 }
1895
1896 brcmf_sdio_pad(bus, &pad, &rd->len_left);
1897
1898 pkt = brcmu_pkt_buf_get_skb(rd->len_left + head_read +
1899 bus->head_align);
1900 if (!pkt) {
1901
1902 brcmf_err("brcmu_pkt_buf_get_skb failed\n");
1903 brcmf_sdio_rxfail(bus, false,
1904 RETRYCHAN(rd->channel));
1905 sdio_release_host(bus->sdiodev->func1);
1906 continue;
1907 }
1908 skb_pull(pkt, head_read);
1909 pkt_align(pkt, rd->len_left, bus->head_align);
1910
1911 ret = brcmf_sdiod_recv_pkt(bus->sdiodev, pkt);
1912 bus->sdcnt.f2rxdata++;
1913 sdio_release_host(bus->sdiodev->func1);
1914
1915 if (ret < 0) {
1916 brcmf_err("read %d bytes from channel %d failed: %d\n",
1917 rd->len, rd->channel, ret);
1918 brcmu_pkt_buf_free_skb(pkt);
1919 sdio_claim_host(bus->sdiodev->func1);
1920 brcmf_sdio_rxfail(bus, true,
1921 RETRYCHAN(rd->channel));
1922 sdio_release_host(bus->sdiodev->func1);
1923 continue;
1924 }
1925
1926 if (head_read) {
1927 skb_push(pkt, head_read);
1928 memcpy(pkt->data, bus->rxhdr, head_read);
1929 head_read = 0;
1930 } else {
1931 memcpy(bus->rxhdr, pkt->data, SDPCM_HDRLEN);
1932 rd_new.seq_num = rd->seq_num;
1933 sdio_claim_host(bus->sdiodev->func1);
1934 if (brcmf_sdio_hdparse(bus, bus->rxhdr, &rd_new,
1935 BRCMF_SDIO_FT_NORMAL)) {
1936 rd->len = 0;
1937 brcmf_sdio_rxfail(bus, true, true);
1938 sdio_release_host(bus->sdiodev->func1);
1939 brcmu_pkt_buf_free_skb(pkt);
1940 continue;
1941 }
1942 bus->sdcnt.rx_readahead_cnt++;
1943 if (rd->len != roundup(rd_new.len, 16)) {
1944 brcmf_err("frame length mismatch:read %d, should be %d\n",
1945 rd->len,
1946 roundup(rd_new.len, 16) >> 4);
1947 rd->len = 0;
1948 brcmf_sdio_rxfail(bus, true, true);
1949 sdio_release_host(bus->sdiodev->func1);
1950 brcmu_pkt_buf_free_skb(pkt);
1951 continue;
1952 }
1953 sdio_release_host(bus->sdiodev->func1);
1954 rd->len_nxtfrm = rd_new.len_nxtfrm;
1955 rd->channel = rd_new.channel;
1956 rd->dat_offset = rd_new.dat_offset;
1957
1958 brcmf_dbg_hex_dump(!(BRCMF_BYTES_ON() &&
1959 BRCMF_DATA_ON()) &&
1960 BRCMF_HDRS_ON(),
1961 bus->rxhdr, SDPCM_HDRLEN,
1962 "RxHdr:\n");
1963
1964 if (rd_new.channel == SDPCM_CONTROL_CHANNEL) {
1965 brcmf_err("readahead on control packet %d?\n",
1966 rd_new.seq_num);
1967
1968 rd->len = 0;
1969 sdio_claim_host(bus->sdiodev->func1);
1970 brcmf_sdio_rxfail(bus, false, true);
1971 sdio_release_host(bus->sdiodev->func1);
1972 brcmu_pkt_buf_free_skb(pkt);
1973 continue;
1974 }
1975 }
1976
1977 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_DATA_ON(),
1978 pkt->data, rd->len, "Rx Data:\n");
1979
1980
1981 if (rd->channel == SDPCM_GLOM_CHANNEL) {
1982 if (SDPCM_GLOMDESC(&bus->rxhdr[SDPCM_HWHDR_LEN])) {
1983 brcmf_dbg(GLOM, "glom descriptor, %d bytes:\n",
1984 rd->len);
1985 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
1986 pkt->data, rd->len,
1987 "Glom Data:\n");
1988 __skb_trim(pkt, rd->len);
1989 skb_pull(pkt, SDPCM_HDRLEN);
1990 bus->glomd = pkt;
1991 } else {
1992 brcmf_err("%s: glom superframe w/o "
1993 "descriptor!\n", __func__);
1994 sdio_claim_host(bus->sdiodev->func1);
1995 brcmf_sdio_rxfail(bus, false, false);
1996 sdio_release_host(bus->sdiodev->func1);
1997 }
1998
1999 rd->len = rd->len_nxtfrm << 4;
2000 rd->len_nxtfrm = 0;
2001
2002 rd->channel = SDPCM_EVENT_CHANNEL;
2003 continue;
2004 }
2005
2006
2007 __skb_trim(pkt, rd->len);
2008 skb_pull(pkt, rd->dat_offset);
2009
2010 if (pkt->len == 0)
2011 brcmu_pkt_buf_free_skb(pkt);
2012 else if (rd->channel == SDPCM_EVENT_CHANNEL)
2013 brcmf_rx_event(bus->sdiodev->dev, pkt);
2014 else
2015 brcmf_rx_frame(bus->sdiodev->dev, pkt,
2016 false);
2017
2018
2019 rd->len = rd->len_nxtfrm << 4;
2020 rd->len_nxtfrm = 0;
2021
2022 rd->channel = SDPCM_EVENT_CHANNEL;
2023 }
2024
2025 rxcount = maxframes - rxleft;
2026
2027 if (!rxleft)
2028 brcmf_dbg(DATA, "hit rx limit of %d frames\n", maxframes);
2029 else
2030 brcmf_dbg(DATA, "processed %d frames\n", rxcount);
2031
2032 if (bus->rxskip)
2033 rd->seq_num--;
2034 bus->rx_seq = rd->seq_num;
2035
2036 return rxcount;
2037 }
2038
2039 static void
2040 brcmf_sdio_wait_event_wakeup(struct brcmf_sdio *bus)
2041 {
2042 wake_up_interruptible(&bus->ctrl_wait);
2043 return;
2044 }
2045
2046 static int brcmf_sdio_txpkt_hdalign(struct brcmf_sdio *bus, struct sk_buff *pkt)
2047 {
2048 struct brcmf_bus_stats *stats;
2049 u16 head_pad;
2050 u8 *dat_buf;
2051
2052 dat_buf = (u8 *)(pkt->data);
2053
2054
2055 head_pad = ((unsigned long)dat_buf % bus->head_align);
2056 if (head_pad) {
2057 if (skb_headroom(pkt) < head_pad) {
2058 stats = &bus->sdiodev->bus_if->stats;
2059 atomic_inc(&stats->pktcowed);
2060 if (skb_cow_head(pkt, head_pad)) {
2061 atomic_inc(&stats->pktcow_failed);
2062 return -ENOMEM;
2063 }
2064 head_pad = 0;
2065 }
2066 skb_push(pkt, head_pad);
2067 dat_buf = (u8 *)(pkt->data);
2068 }
2069 memset(dat_buf, 0, head_pad + bus->tx_hdrlen);
2070 return head_pad;
2071 }
2072
2073
2074
2075
2076
2077
2078 #define ALIGN_SKB_FLAG 0x8000
2079
2080 #define ALIGN_SKB_CHOP_LEN_MASK 0x7fff
2081
2082 static int brcmf_sdio_txpkt_prep_sg(struct brcmf_sdio *bus,
2083 struct sk_buff_head *pktq,
2084 struct sk_buff *pkt, u16 total_len)
2085 {
2086 struct brcmf_sdio_dev *sdiodev;
2087 struct sk_buff *pkt_pad;
2088 u16 tail_pad, tail_chop, chain_pad;
2089 unsigned int blksize;
2090 bool lastfrm;
2091 int ntail, ret;
2092
2093 sdiodev = bus->sdiodev;
2094 blksize = sdiodev->func2->cur_blksize;
2095
2096 WARN_ON(blksize % bus->sgentry_align);
2097
2098
2099 lastfrm = skb_queue_is_last(pktq, pkt);
2100 tail_pad = 0;
2101 tail_chop = pkt->len % bus->sgentry_align;
2102 if (tail_chop)
2103 tail_pad = bus->sgentry_align - tail_chop;
2104 chain_pad = (total_len + tail_pad) % blksize;
2105 if (lastfrm && chain_pad)
2106 tail_pad += blksize - chain_pad;
2107 if (skb_tailroom(pkt) < tail_pad && pkt->len > blksize) {
2108 pkt_pad = brcmu_pkt_buf_get_skb(tail_pad + tail_chop +
2109 bus->head_align);
2110 if (pkt_pad == NULL)
2111 return -ENOMEM;
2112 ret = brcmf_sdio_txpkt_hdalign(bus, pkt_pad);
2113 if (unlikely(ret < 0)) {
2114 kfree_skb(pkt_pad);
2115 return ret;
2116 }
2117 memcpy(pkt_pad->data,
2118 pkt->data + pkt->len - tail_chop,
2119 tail_chop);
2120 *(u16 *)(pkt_pad->cb) = ALIGN_SKB_FLAG + tail_chop;
2121 skb_trim(pkt, pkt->len - tail_chop);
2122 skb_trim(pkt_pad, tail_pad + tail_chop);
2123 __skb_queue_after(pktq, pkt, pkt_pad);
2124 } else {
2125 ntail = pkt->data_len + tail_pad -
2126 (pkt->end - pkt->tail);
2127 if (skb_cloned(pkt) || ntail > 0)
2128 if (pskb_expand_head(pkt, 0, ntail, GFP_ATOMIC))
2129 return -ENOMEM;
2130 if (skb_linearize(pkt))
2131 return -ENOMEM;
2132 __skb_put(pkt, tail_pad);
2133 }
2134
2135 return tail_pad;
2136 }
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150 static int
2151 brcmf_sdio_txpkt_prep(struct brcmf_sdio *bus, struct sk_buff_head *pktq,
2152 uint chan)
2153 {
2154 u16 head_pad, total_len;
2155 struct sk_buff *pkt_next;
2156 u8 txseq;
2157 int ret;
2158 struct brcmf_sdio_hdrinfo hd_info = {0};
2159
2160 txseq = bus->tx_seq;
2161 total_len = 0;
2162 skb_queue_walk(pktq, pkt_next) {
2163
2164
2165
2166
2167
2168 if (*(u16 *)(pkt_next->cb) & ALIGN_SKB_FLAG)
2169 continue;
2170
2171
2172 ret = brcmf_sdio_txpkt_hdalign(bus, pkt_next);
2173 if (ret < 0)
2174 return ret;
2175 head_pad = (u16)ret;
2176 if (head_pad)
2177 memset(pkt_next->data + bus->tx_hdrlen, 0, head_pad);
2178
2179 total_len += pkt_next->len;
2180
2181 hd_info.len = pkt_next->len;
2182 hd_info.lastfrm = skb_queue_is_last(pktq, pkt_next);
2183 if (bus->txglom && pktq->qlen > 1) {
2184 ret = brcmf_sdio_txpkt_prep_sg(bus, pktq,
2185 pkt_next, total_len);
2186 if (ret < 0)
2187 return ret;
2188 hd_info.tail_pad = (u16)ret;
2189 total_len += (u16)ret;
2190 }
2191
2192 hd_info.channel = chan;
2193 hd_info.dat_offset = head_pad + bus->tx_hdrlen;
2194 hd_info.seq_num = txseq++;
2195
2196
2197 brcmf_sdio_hdpack(bus, pkt_next->data, &hd_info);
2198
2199 if (BRCMF_BYTES_ON() &&
2200 ((BRCMF_CTL_ON() && chan == SDPCM_CONTROL_CHANNEL) ||
2201 (BRCMF_DATA_ON() && chan != SDPCM_CONTROL_CHANNEL)))
2202 brcmf_dbg_hex_dump(true, pkt_next->data, hd_info.len,
2203 "Tx Frame:\n");
2204 else if (BRCMF_HDRS_ON())
2205 brcmf_dbg_hex_dump(true, pkt_next->data,
2206 head_pad + bus->tx_hdrlen,
2207 "Tx Header:\n");
2208 }
2209
2210
2211
2212 if (bus->txglom)
2213 brcmf_sdio_update_hwhdr(__skb_peek(pktq)->data, total_len);
2214 return 0;
2215 }
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226 static void
2227 brcmf_sdio_txpkt_postp(struct brcmf_sdio *bus, struct sk_buff_head *pktq)
2228 {
2229 u8 *hdr;
2230 u32 dat_offset;
2231 u16 tail_pad;
2232 u16 dummy_flags, chop_len;
2233 struct sk_buff *pkt_next, *tmp, *pkt_prev;
2234
2235 skb_queue_walk_safe(pktq, pkt_next, tmp) {
2236 dummy_flags = *(u16 *)(pkt_next->cb);
2237 if (dummy_flags & ALIGN_SKB_FLAG) {
2238 chop_len = dummy_flags & ALIGN_SKB_CHOP_LEN_MASK;
2239 if (chop_len) {
2240 pkt_prev = pkt_next->prev;
2241 skb_put(pkt_prev, chop_len);
2242 }
2243 __skb_unlink(pkt_next, pktq);
2244 brcmu_pkt_buf_free_skb(pkt_next);
2245 } else {
2246 hdr = pkt_next->data + bus->tx_hdrlen - SDPCM_SWHDR_LEN;
2247 dat_offset = le32_to_cpu(*(__le32 *)hdr);
2248 dat_offset = (dat_offset & SDPCM_DOFFSET_MASK) >>
2249 SDPCM_DOFFSET_SHIFT;
2250 skb_pull(pkt_next, dat_offset);
2251 if (bus->txglom) {
2252 tail_pad = le16_to_cpu(*(__le16 *)(hdr - 2));
2253 skb_trim(pkt_next, pkt_next->len - tail_pad);
2254 }
2255 }
2256 }
2257 }
2258
2259
2260
2261 static int brcmf_sdio_txpkt(struct brcmf_sdio *bus, struct sk_buff_head *pktq,
2262 uint chan)
2263 {
2264 int ret;
2265 struct sk_buff *pkt_next, *tmp;
2266
2267 brcmf_dbg(TRACE, "Enter\n");
2268
2269 ret = brcmf_sdio_txpkt_prep(bus, pktq, chan);
2270 if (ret)
2271 goto done;
2272
2273 sdio_claim_host(bus->sdiodev->func1);
2274 ret = brcmf_sdiod_send_pkt(bus->sdiodev, pktq);
2275 bus->sdcnt.f2txdata++;
2276
2277 if (ret < 0)
2278 brcmf_sdio_txfail(bus);
2279
2280 sdio_release_host(bus->sdiodev->func1);
2281
2282 done:
2283 brcmf_sdio_txpkt_postp(bus, pktq);
2284 if (ret == 0)
2285 bus->tx_seq = (bus->tx_seq + pktq->qlen) % SDPCM_SEQ_WRAP;
2286 skb_queue_walk_safe(pktq, pkt_next, tmp) {
2287 __skb_unlink(pkt_next, pktq);
2288 brcmf_proto_bcdc_txcomplete(bus->sdiodev->dev, pkt_next,
2289 ret == 0);
2290 }
2291 return ret;
2292 }
2293
2294 static uint brcmf_sdio_sendfromq(struct brcmf_sdio *bus, uint maxframes)
2295 {
2296 struct sk_buff *pkt;
2297 struct sk_buff_head pktq;
2298 u32 intstat_addr = bus->sdio_core->base + SD_REG(intstatus);
2299 u32 intstatus = 0;
2300 int ret = 0, prec_out, i;
2301 uint cnt = 0;
2302 u8 tx_prec_map, pkt_num;
2303
2304 brcmf_dbg(TRACE, "Enter\n");
2305
2306 tx_prec_map = ~bus->flowcontrol;
2307
2308
2309 for (cnt = 0; (cnt < maxframes) && data_ok(bus);) {
2310 pkt_num = 1;
2311 if (bus->txglom)
2312 pkt_num = min_t(u8, bus->tx_max - bus->tx_seq,
2313 bus->sdiodev->txglomsz);
2314 pkt_num = min_t(u32, pkt_num,
2315 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol));
2316 __skb_queue_head_init(&pktq);
2317 spin_lock_bh(&bus->txq_lock);
2318 for (i = 0; i < pkt_num; i++) {
2319 pkt = brcmu_pktq_mdeq(&bus->txq, tx_prec_map,
2320 &prec_out);
2321 if (pkt == NULL)
2322 break;
2323 __skb_queue_tail(&pktq, pkt);
2324 }
2325 spin_unlock_bh(&bus->txq_lock);
2326 if (i == 0)
2327 break;
2328
2329 ret = brcmf_sdio_txpkt(bus, &pktq, SDPCM_DATA_CHANNEL);
2330
2331 cnt += i;
2332
2333
2334 if (!bus->intr) {
2335
2336 sdio_claim_host(bus->sdiodev->func1);
2337 intstatus = brcmf_sdiod_readl(bus->sdiodev,
2338 intstat_addr, &ret);
2339 sdio_release_host(bus->sdiodev->func1);
2340
2341 bus->sdcnt.f2txdata++;
2342 if (ret != 0)
2343 break;
2344 if (intstatus & bus->hostintmask)
2345 atomic_set(&bus->ipend, 1);
2346 }
2347 }
2348
2349
2350 if ((bus->sdiodev->state == BRCMF_SDIOD_DATA) &&
2351 bus->txoff && (pktq_len(&bus->txq) < TXLOW)) {
2352 bus->txoff = false;
2353 brcmf_proto_bcdc_txflowblock(bus->sdiodev->dev, false);
2354 }
2355
2356 return cnt;
2357 }
2358
2359 static int brcmf_sdio_tx_ctrlframe(struct brcmf_sdio *bus, u8 *frame, u16 len)
2360 {
2361 u8 doff;
2362 u16 pad;
2363 uint retries = 0;
2364 struct brcmf_sdio_hdrinfo hd_info = {0};
2365 int ret;
2366
2367 brcmf_dbg(SDIO, "Enter\n");
2368
2369
2370 frame -= bus->tx_hdrlen;
2371 len += bus->tx_hdrlen;
2372
2373
2374 doff = ((unsigned long)frame % bus->head_align);
2375 if (doff) {
2376 frame -= doff;
2377 len += doff;
2378 memset(frame + bus->tx_hdrlen, 0, doff);
2379 }
2380
2381
2382 pad = 0;
2383 if (bus->roundup && bus->blocksize && (len > bus->blocksize)) {
2384 pad = bus->blocksize - (len % bus->blocksize);
2385 if ((pad > bus->roundup) || (pad >= bus->blocksize))
2386 pad = 0;
2387 } else if (len % bus->head_align) {
2388 pad = bus->head_align - (len % bus->head_align);
2389 }
2390 len += pad;
2391
2392 hd_info.len = len - pad;
2393 hd_info.channel = SDPCM_CONTROL_CHANNEL;
2394 hd_info.dat_offset = doff + bus->tx_hdrlen;
2395 hd_info.seq_num = bus->tx_seq;
2396 hd_info.lastfrm = true;
2397 hd_info.tail_pad = pad;
2398 brcmf_sdio_hdpack(bus, frame, &hd_info);
2399
2400 if (bus->txglom)
2401 brcmf_sdio_update_hwhdr(frame, len);
2402
2403 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_CTL_ON(),
2404 frame, len, "Tx Frame:\n");
2405 brcmf_dbg_hex_dump(!(BRCMF_BYTES_ON() && BRCMF_CTL_ON()) &&
2406 BRCMF_HDRS_ON(),
2407 frame, min_t(u16, len, 16), "TxHdr:\n");
2408
2409 do {
2410 ret = brcmf_sdiod_send_buf(bus->sdiodev, frame, len);
2411
2412 if (ret < 0)
2413 brcmf_sdio_txfail(bus);
2414 else
2415 bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQ_WRAP;
2416 } while (ret < 0 && retries++ < TXRETRIES);
2417
2418 return ret;
2419 }
2420
2421 static bool brcmf_chip_is_ulp(struct brcmf_chip *ci)
2422 {
2423 if (ci->chip == CY_CC_43012_CHIP_ID)
2424 return true;
2425 else
2426 return false;
2427 }
2428
2429 static void brcmf_sdio_bus_stop(struct device *dev)
2430 {
2431 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
2432 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
2433 struct brcmf_sdio *bus = sdiodev->bus;
2434 struct brcmf_core *core = bus->sdio_core;
2435 u32 local_hostintmask;
2436 u8 saveclk, bpreq;
2437 int err;
2438
2439 brcmf_dbg(TRACE, "Enter\n");
2440
2441 if (bus->watchdog_tsk) {
2442 send_sig(SIGTERM, bus->watchdog_tsk, 1);
2443 kthread_stop(bus->watchdog_tsk);
2444 bus->watchdog_tsk = NULL;
2445 }
2446
2447 if (sdiodev->state != BRCMF_SDIOD_NOMEDIUM) {
2448 sdio_claim_host(sdiodev->func1);
2449
2450
2451 brcmf_sdio_bus_sleep(bus, false, false);
2452
2453
2454 brcmf_sdiod_writel(sdiodev, core->base + SD_REG(hostintmask),
2455 0, NULL);
2456
2457 local_hostintmask = bus->hostintmask;
2458 bus->hostintmask = 0;
2459
2460
2461 saveclk = brcmf_sdiod_readb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
2462 &err);
2463 if (!err) {
2464 bpreq = saveclk;
2465 bpreq |= brcmf_chip_is_ulp(bus->ci) ?
2466 SBSDIO_HT_AVAIL_REQ : SBSDIO_FORCE_HT;
2467 brcmf_sdiod_writeb(sdiodev,
2468 SBSDIO_FUNC1_CHIPCLKCSR,
2469 bpreq, &err);
2470 }
2471 if (err)
2472 brcmf_err("Failed to force clock for F2: err %d\n",
2473 err);
2474
2475
2476 brcmf_dbg(INTR, "disable SDIO interrupts\n");
2477 sdio_disable_func(sdiodev->func2);
2478
2479
2480 brcmf_sdiod_writel(sdiodev, core->base + SD_REG(intstatus),
2481 local_hostintmask, NULL);
2482
2483 sdio_release_host(sdiodev->func1);
2484 }
2485
2486 brcmu_pktq_flush(&bus->txq, true, NULL, NULL);
2487
2488
2489 brcmu_pkt_buf_free_skb(bus->glomd);
2490 brcmf_sdio_free_glom(bus);
2491
2492
2493 spin_lock_bh(&bus->rxctl_lock);
2494 bus->rxlen = 0;
2495 spin_unlock_bh(&bus->rxctl_lock);
2496 brcmf_sdio_dcmd_resp_wake(bus);
2497
2498
2499 bus->rxskip = false;
2500 bus->tx_seq = bus->rx_seq = 0;
2501 }
2502
2503 static inline void brcmf_sdio_clrintr(struct brcmf_sdio *bus)
2504 {
2505 struct brcmf_sdio_dev *sdiodev;
2506 unsigned long flags;
2507
2508 sdiodev = bus->sdiodev;
2509 if (sdiodev->oob_irq_requested) {
2510 spin_lock_irqsave(&sdiodev->irq_en_lock, flags);
2511 if (!sdiodev->irq_en && !atomic_read(&bus->ipend)) {
2512 enable_irq(sdiodev->settings->bus.sdio.oob_irq_nr);
2513 sdiodev->irq_en = true;
2514 }
2515 spin_unlock_irqrestore(&sdiodev->irq_en_lock, flags);
2516 }
2517 }
2518
2519 static int brcmf_sdio_intr_rstatus(struct brcmf_sdio *bus)
2520 {
2521 struct brcmf_core *core = bus->sdio_core;
2522 u32 addr;
2523 unsigned long val;
2524 int ret;
2525
2526 addr = core->base + SD_REG(intstatus);
2527
2528 val = brcmf_sdiod_readl(bus->sdiodev, addr, &ret);
2529 bus->sdcnt.f1regdata++;
2530 if (ret != 0)
2531 return ret;
2532
2533 val &= bus->hostintmask;
2534 atomic_set(&bus->fcstate, !!(val & I_HMB_FC_STATE));
2535
2536
2537 if (val) {
2538 brcmf_sdiod_writel(bus->sdiodev, addr, val, &ret);
2539 bus->sdcnt.f1regdata++;
2540 atomic_or(val, &bus->intstatus);
2541 }
2542
2543 return ret;
2544 }
2545
2546 static void brcmf_sdio_dpc(struct brcmf_sdio *bus)
2547 {
2548 struct brcmf_sdio_dev *sdiod = bus->sdiodev;
2549 u32 newstatus = 0;
2550 u32 intstat_addr = bus->sdio_core->base + SD_REG(intstatus);
2551 unsigned long intstatus;
2552 uint txlimit = bus->txbound;
2553 uint framecnt;
2554 int err = 0;
2555
2556 brcmf_dbg(SDIO, "Enter\n");
2557
2558 sdio_claim_host(bus->sdiodev->func1);
2559
2560
2561 if (!bus->sr_enabled && bus->clkstate == CLK_PENDING) {
2562 u8 clkctl, devctl = 0;
2563
2564 #ifdef DEBUG
2565
2566 devctl = brcmf_sdiod_readb(bus->sdiodev, SBSDIO_DEVICE_CTL,
2567 &err);
2568 #endif
2569
2570
2571 clkctl = brcmf_sdiod_readb(bus->sdiodev,
2572 SBSDIO_FUNC1_CHIPCLKCSR, &err);
2573
2574 brcmf_dbg(SDIO, "DPC: PENDING, devctl 0x%02x clkctl 0x%02x\n",
2575 devctl, clkctl);
2576
2577 if (SBSDIO_HTAV(clkctl)) {
2578 devctl = brcmf_sdiod_readb(bus->sdiodev,
2579 SBSDIO_DEVICE_CTL, &err);
2580 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
2581 brcmf_sdiod_writeb(bus->sdiodev,
2582 SBSDIO_DEVICE_CTL, devctl, &err);
2583 bus->clkstate = CLK_AVAIL;
2584 }
2585 }
2586
2587
2588 brcmf_sdio_bus_sleep(bus, false, true);
2589
2590
2591 if (atomic_read(&bus->ipend) > 0) {
2592 atomic_set(&bus->ipend, 0);
2593 err = brcmf_sdio_intr_rstatus(bus);
2594 }
2595
2596
2597 intstatus = atomic_xchg(&bus->intstatus, 0);
2598
2599
2600
2601
2602
2603 if (intstatus & I_HMB_FC_CHANGE) {
2604 intstatus &= ~I_HMB_FC_CHANGE;
2605 brcmf_sdiod_writel(sdiod, intstat_addr, I_HMB_FC_CHANGE, &err);
2606
2607 newstatus = brcmf_sdiod_readl(sdiod, intstat_addr, &err);
2608
2609 bus->sdcnt.f1regdata += 2;
2610 atomic_set(&bus->fcstate,
2611 !!(newstatus & (I_HMB_FC_STATE | I_HMB_FC_CHANGE)));
2612 intstatus |= (newstatus & bus->hostintmask);
2613 }
2614
2615
2616 if (intstatus & I_HMB_HOST_INT) {
2617 intstatus &= ~I_HMB_HOST_INT;
2618 intstatus |= brcmf_sdio_hostmail(bus);
2619 }
2620
2621 sdio_release_host(bus->sdiodev->func1);
2622
2623
2624 if (intstatus & I_WR_OOSYNC) {
2625 brcmf_err("Dongle reports WR_OOSYNC\n");
2626 intstatus &= ~I_WR_OOSYNC;
2627 }
2628
2629 if (intstatus & I_RD_OOSYNC) {
2630 brcmf_err("Dongle reports RD_OOSYNC\n");
2631 intstatus &= ~I_RD_OOSYNC;
2632 }
2633
2634 if (intstatus & I_SBINT) {
2635 brcmf_err("Dongle reports SBINT\n");
2636 intstatus &= ~I_SBINT;
2637 }
2638
2639
2640 if (intstatus & I_CHIPACTIVE) {
2641 brcmf_dbg(SDIO, "Dongle reports CHIPACTIVE\n");
2642 intstatus &= ~I_CHIPACTIVE;
2643 }
2644
2645
2646 if (bus->rxskip)
2647 intstatus &= ~I_HMB_FRAME_IND;
2648
2649
2650 if ((intstatus & I_HMB_FRAME_IND) && (bus->clkstate == CLK_AVAIL)) {
2651 brcmf_sdio_readframes(bus, bus->rxbound);
2652 if (!bus->rxpending)
2653 intstatus &= ~I_HMB_FRAME_IND;
2654 }
2655
2656
2657 if (intstatus)
2658 atomic_or(intstatus, &bus->intstatus);
2659
2660 brcmf_sdio_clrintr(bus);
2661
2662 if (bus->ctrl_frame_stat && (bus->clkstate == CLK_AVAIL) &&
2663 data_ok(bus)) {
2664 sdio_claim_host(bus->sdiodev->func1);
2665 if (bus->ctrl_frame_stat) {
2666 err = brcmf_sdio_tx_ctrlframe(bus, bus->ctrl_frame_buf,
2667 bus->ctrl_frame_len);
2668 bus->ctrl_frame_err = err;
2669 wmb();
2670 bus->ctrl_frame_stat = false;
2671 }
2672 sdio_release_host(bus->sdiodev->func1);
2673 brcmf_sdio_wait_event_wakeup(bus);
2674 }
2675
2676 if ((bus->clkstate == CLK_AVAIL) && !atomic_read(&bus->fcstate) &&
2677 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) && txlimit &&
2678 data_ok(bus)) {
2679 framecnt = bus->rxpending ? min(txlimit, bus->txminmax) :
2680 txlimit;
2681 brcmf_sdio_sendfromq(bus, framecnt);
2682 }
2683
2684 if ((bus->sdiodev->state != BRCMF_SDIOD_DATA) || (err != 0)) {
2685 brcmf_err("failed backplane access over SDIO, halting operation\n");
2686 atomic_set(&bus->intstatus, 0);
2687 if (bus->ctrl_frame_stat) {
2688 sdio_claim_host(bus->sdiodev->func1);
2689 if (bus->ctrl_frame_stat) {
2690 bus->ctrl_frame_err = -ENODEV;
2691 wmb();
2692 bus->ctrl_frame_stat = false;
2693 brcmf_sdio_wait_event_wakeup(bus);
2694 }
2695 sdio_release_host(bus->sdiodev->func1);
2696 }
2697 } else if (atomic_read(&bus->intstatus) ||
2698 atomic_read(&bus->ipend) > 0 ||
2699 (!atomic_read(&bus->fcstate) &&
2700 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) &&
2701 data_ok(bus))) {
2702 bus->dpc_triggered = true;
2703 }
2704 }
2705
2706 static struct pktq *brcmf_sdio_bus_gettxq(struct device *dev)
2707 {
2708 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
2709 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
2710 struct brcmf_sdio *bus = sdiodev->bus;
2711
2712 return &bus->txq;
2713 }
2714
2715 static bool brcmf_sdio_prec_enq(struct pktq *q, struct sk_buff *pkt, int prec)
2716 {
2717 struct sk_buff *p;
2718 int eprec = -1;
2719
2720
2721
2722
2723 if (!pktq_pfull(q, prec) && !pktq_full(q)) {
2724 brcmu_pktq_penq(q, prec, pkt);
2725 return true;
2726 }
2727
2728
2729 if (pktq_pfull(q, prec)) {
2730 eprec = prec;
2731 } else if (pktq_full(q)) {
2732 p = brcmu_pktq_peek_tail(q, &eprec);
2733 if (eprec > prec)
2734 return false;
2735 }
2736
2737
2738 if (eprec >= 0) {
2739
2740 if (eprec == prec)
2741 return false;
2742
2743 p = brcmu_pktq_pdeq_tail(q, eprec);
2744 if (p == NULL)
2745 brcmf_err("brcmu_pktq_pdeq_tail() failed\n");
2746 brcmu_pkt_buf_free_skb(p);
2747 }
2748
2749
2750 p = brcmu_pktq_penq(q, prec, pkt);
2751 if (p == NULL)
2752 brcmf_err("brcmu_pktq_penq() failed\n");
2753
2754 return p != NULL;
2755 }
2756
2757 static int brcmf_sdio_bus_txdata(struct device *dev, struct sk_buff *pkt)
2758 {
2759 int ret = -EBADE;
2760 uint prec;
2761 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
2762 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
2763 struct brcmf_sdio *bus = sdiodev->bus;
2764
2765 brcmf_dbg(TRACE, "Enter: pkt: data %p len %d\n", pkt->data, pkt->len);
2766 if (sdiodev->state != BRCMF_SDIOD_DATA)
2767 return -EIO;
2768
2769
2770 skb_push(pkt, bus->tx_hdrlen);
2771
2772
2773 prec = prio2prec((pkt->priority & PRIOMASK));
2774
2775
2776
2777 brcmf_dbg(TRACE, "deferring pktq len %d\n", pktq_len(&bus->txq));
2778 bus->sdcnt.fcqueued++;
2779
2780
2781 spin_lock_bh(&bus->txq_lock);
2782
2783 *(u16 *)(pkt->cb) = 0;
2784 if (!brcmf_sdio_prec_enq(&bus->txq, pkt, prec)) {
2785 skb_pull(pkt, bus->tx_hdrlen);
2786 brcmf_err("out of bus->txq !!!\n");
2787 ret = -ENOSR;
2788 } else {
2789 ret = 0;
2790 }
2791
2792 if (pktq_len(&bus->txq) >= TXHI) {
2793 bus->txoff = true;
2794 brcmf_proto_bcdc_txflowblock(dev, true);
2795 }
2796 spin_unlock_bh(&bus->txq_lock);
2797
2798 #ifdef DEBUG
2799 if (pktq_plen(&bus->txq, prec) > qcount[prec])
2800 qcount[prec] = pktq_plen(&bus->txq, prec);
2801 #endif
2802
2803 brcmf_sdio_trigger_dpc(bus);
2804 return ret;
2805 }
2806
2807 #ifdef DEBUG
2808 #define CONSOLE_LINE_MAX 192
2809
2810 static int brcmf_sdio_readconsole(struct brcmf_sdio *bus)
2811 {
2812 struct brcmf_console *c = &bus->console;
2813 u8 line[CONSOLE_LINE_MAX], ch;
2814 u32 n, idx, addr;
2815 int rv;
2816
2817
2818 if (bus->console_addr == 0)
2819 return 0;
2820
2821
2822 addr = bus->console_addr + offsetof(struct rte_console, log_le);
2823 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, (u8 *)&c->log_le,
2824 sizeof(c->log_le));
2825 if (rv < 0)
2826 return rv;
2827
2828
2829 if (c->buf == NULL) {
2830 c->bufsize = le32_to_cpu(c->log_le.buf_size);
2831 c->buf = kmalloc(c->bufsize, GFP_ATOMIC);
2832 if (c->buf == NULL)
2833 return -ENOMEM;
2834 }
2835
2836 idx = le32_to_cpu(c->log_le.idx);
2837
2838
2839 if (idx > c->bufsize)
2840 return -EBADE;
2841
2842
2843
2844 if (idx == c->last)
2845 return 0;
2846
2847
2848 addr = le32_to_cpu(c->log_le.buf);
2849 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, c->buf, c->bufsize);
2850 if (rv < 0)
2851 return rv;
2852
2853 while (c->last != idx) {
2854 for (n = 0; n < CONSOLE_LINE_MAX - 2; n++) {
2855 if (c->last == idx) {
2856
2857
2858
2859
2860
2861 if (c->last >= n)
2862 c->last -= n;
2863 else
2864 c->last = c->bufsize - n;
2865 goto break2;
2866 }
2867 ch = c->buf[c->last];
2868 c->last = (c->last + 1) % c->bufsize;
2869 if (ch == '\n')
2870 break;
2871 line[n] = ch;
2872 }
2873
2874 if (n > 0) {
2875 if (line[n - 1] == '\r')
2876 n--;
2877 line[n] = 0;
2878 pr_debug("CONSOLE: %s\n", line);
2879 }
2880 }
2881 break2:
2882
2883 return 0;
2884 }
2885 #endif
2886
2887 static int
2888 brcmf_sdio_bus_txctl(struct device *dev, unsigned char *msg, uint msglen)
2889 {
2890 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
2891 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
2892 struct brcmf_sdio *bus = sdiodev->bus;
2893 int ret;
2894
2895 brcmf_dbg(TRACE, "Enter\n");
2896 if (sdiodev->state != BRCMF_SDIOD_DATA)
2897 return -EIO;
2898
2899
2900 bus->ctrl_frame_buf = msg;
2901 bus->ctrl_frame_len = msglen;
2902 wmb();
2903 bus->ctrl_frame_stat = true;
2904
2905 brcmf_sdio_trigger_dpc(bus);
2906 wait_event_interruptible_timeout(bus->ctrl_wait, !bus->ctrl_frame_stat,
2907 CTL_DONE_TIMEOUT);
2908 ret = 0;
2909 if (bus->ctrl_frame_stat) {
2910 sdio_claim_host(bus->sdiodev->func1);
2911 if (bus->ctrl_frame_stat) {
2912 brcmf_dbg(SDIO, "ctrl_frame timeout\n");
2913 bus->ctrl_frame_stat = false;
2914 ret = -ETIMEDOUT;
2915 }
2916 sdio_release_host(bus->sdiodev->func1);
2917 }
2918 if (!ret) {
2919 brcmf_dbg(SDIO, "ctrl_frame complete, err=%d\n",
2920 bus->ctrl_frame_err);
2921 rmb();
2922 ret = bus->ctrl_frame_err;
2923 }
2924
2925 if (ret)
2926 bus->sdcnt.tx_ctlerrs++;
2927 else
2928 bus->sdcnt.tx_ctlpkts++;
2929
2930 return ret;
2931 }
2932
2933 #ifdef DEBUG
2934 static int brcmf_sdio_dump_console(struct seq_file *seq, struct brcmf_sdio *bus,
2935 struct sdpcm_shared *sh)
2936 {
2937 u32 addr, console_ptr, console_size, console_index;
2938 char *conbuf = NULL;
2939 __le32 sh_val;
2940 int rv;
2941
2942
2943 addr = sh->console_addr + offsetof(struct rte_console, log_le);
2944 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
2945 (u8 *)&sh_val, sizeof(u32));
2946 if (rv < 0)
2947 return rv;
2948 console_ptr = le32_to_cpu(sh_val);
2949
2950 addr = sh->console_addr + offsetof(struct rte_console, log_le.buf_size);
2951 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
2952 (u8 *)&sh_val, sizeof(u32));
2953 if (rv < 0)
2954 return rv;
2955 console_size = le32_to_cpu(sh_val);
2956
2957 addr = sh->console_addr + offsetof(struct rte_console, log_le.idx);
2958 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
2959 (u8 *)&sh_val, sizeof(u32));
2960 if (rv < 0)
2961 return rv;
2962 console_index = le32_to_cpu(sh_val);
2963
2964
2965 if (console_size <= CONSOLE_BUFFER_MAX)
2966 conbuf = vzalloc(console_size+1);
2967
2968 if (!conbuf)
2969 return -ENOMEM;
2970
2971
2972 conbuf[console_size] = '\0';
2973 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, console_ptr, (u8 *)conbuf,
2974 console_size);
2975 if (rv < 0)
2976 goto done;
2977
2978 rv = seq_write(seq, conbuf + console_index,
2979 console_size - console_index);
2980 if (rv < 0)
2981 goto done;
2982
2983 if (console_index > 0)
2984 rv = seq_write(seq, conbuf, console_index - 1);
2985
2986 done:
2987 vfree(conbuf);
2988 return rv;
2989 }
2990
2991 static int brcmf_sdio_trap_info(struct seq_file *seq, struct brcmf_sdio *bus,
2992 struct sdpcm_shared *sh)
2993 {
2994 int error;
2995 struct brcmf_trap_info tr;
2996
2997 if ((sh->flags & SDPCM_SHARED_TRAP) == 0) {
2998 brcmf_dbg(INFO, "no trap in firmware\n");
2999 return 0;
3000 }
3001
3002 error = brcmf_sdiod_ramrw(bus->sdiodev, false, sh->trap_addr, (u8 *)&tr,
3003 sizeof(struct brcmf_trap_info));
3004 if (error < 0)
3005 return error;
3006
3007 if (seq)
3008 seq_printf(seq,
3009 "dongle trap info: type 0x%x @ epc 0x%08x\n"
3010 " cpsr 0x%08x spsr 0x%08x sp 0x%08x\n"
3011 " lr 0x%08x pc 0x%08x offset 0x%x\n"
3012 " r0 0x%08x r1 0x%08x r2 0x%08x r3 0x%08x\n"
3013 " r4 0x%08x r5 0x%08x r6 0x%08x r7 0x%08x\n",
3014 le32_to_cpu(tr.type), le32_to_cpu(tr.epc),
3015 le32_to_cpu(tr.cpsr), le32_to_cpu(tr.spsr),
3016 le32_to_cpu(tr.r13), le32_to_cpu(tr.r14),
3017 le32_to_cpu(tr.pc), sh->trap_addr,
3018 le32_to_cpu(tr.r0), le32_to_cpu(tr.r1),
3019 le32_to_cpu(tr.r2), le32_to_cpu(tr.r3),
3020 le32_to_cpu(tr.r4), le32_to_cpu(tr.r5),
3021 le32_to_cpu(tr.r6), le32_to_cpu(tr.r7));
3022 else
3023 pr_debug("dongle trap info: type 0x%x @ epc 0x%08x\n"
3024 " cpsr 0x%08x spsr 0x%08x sp 0x%08x\n"
3025 " lr 0x%08x pc 0x%08x offset 0x%x\n"
3026 " r0 0x%08x r1 0x%08x r2 0x%08x r3 0x%08x\n"
3027 " r4 0x%08x r5 0x%08x r6 0x%08x r7 0x%08x\n",
3028 le32_to_cpu(tr.type), le32_to_cpu(tr.epc),
3029 le32_to_cpu(tr.cpsr), le32_to_cpu(tr.spsr),
3030 le32_to_cpu(tr.r13), le32_to_cpu(tr.r14),
3031 le32_to_cpu(tr.pc), sh->trap_addr,
3032 le32_to_cpu(tr.r0), le32_to_cpu(tr.r1),
3033 le32_to_cpu(tr.r2), le32_to_cpu(tr.r3),
3034 le32_to_cpu(tr.r4), le32_to_cpu(tr.r5),
3035 le32_to_cpu(tr.r6), le32_to_cpu(tr.r7));
3036 return 0;
3037 }
3038
3039 static int brcmf_sdio_assert_info(struct seq_file *seq, struct brcmf_sdio *bus,
3040 struct sdpcm_shared *sh)
3041 {
3042 int error = 0;
3043 char file[80] = "?";
3044 char expr[80] = "<???>";
3045
3046 if ((sh->flags & SDPCM_SHARED_ASSERT_BUILT) == 0) {
3047 brcmf_dbg(INFO, "firmware not built with -assert\n");
3048 return 0;
3049 } else if ((sh->flags & SDPCM_SHARED_ASSERT) == 0) {
3050 brcmf_dbg(INFO, "no assert in dongle\n");
3051 return 0;
3052 }
3053
3054 sdio_claim_host(bus->sdiodev->func1);
3055 if (sh->assert_file_addr != 0) {
3056 error = brcmf_sdiod_ramrw(bus->sdiodev, false,
3057 sh->assert_file_addr, (u8 *)file, 80);
3058 if (error < 0)
3059 return error;
3060 }
3061 if (sh->assert_exp_addr != 0) {
3062 error = brcmf_sdiod_ramrw(bus->sdiodev, false,
3063 sh->assert_exp_addr, (u8 *)expr, 80);
3064 if (error < 0)
3065 return error;
3066 }
3067 sdio_release_host(bus->sdiodev->func1);
3068
3069 seq_printf(seq, "dongle assert: %s:%d: assert(%s)\n",
3070 file, sh->assert_line, expr);
3071 return 0;
3072 }
3073
3074 static int brcmf_sdio_checkdied(struct brcmf_sdio *bus)
3075 {
3076 int error;
3077 struct sdpcm_shared sh;
3078
3079 error = brcmf_sdio_readshared(bus, &sh);
3080
3081 if (error < 0)
3082 return error;
3083
3084 if ((sh.flags & SDPCM_SHARED_ASSERT_BUILT) == 0)
3085 brcmf_dbg(INFO, "firmware not built with -assert\n");
3086 else if (sh.flags & SDPCM_SHARED_ASSERT)
3087 brcmf_err("assertion in dongle\n");
3088
3089 if (sh.flags & SDPCM_SHARED_TRAP) {
3090 brcmf_err("firmware trap in dongle\n");
3091 brcmf_sdio_trap_info(NULL, bus, &sh);
3092 }
3093
3094 return 0;
3095 }
3096
3097 static int brcmf_sdio_died_dump(struct seq_file *seq, struct brcmf_sdio *bus)
3098 {
3099 int error = 0;
3100 struct sdpcm_shared sh;
3101
3102 error = brcmf_sdio_readshared(bus, &sh);
3103 if (error < 0)
3104 goto done;
3105
3106 error = brcmf_sdio_assert_info(seq, bus, &sh);
3107 if (error < 0)
3108 goto done;
3109
3110 error = brcmf_sdio_trap_info(seq, bus, &sh);
3111 if (error < 0)
3112 goto done;
3113
3114 error = brcmf_sdio_dump_console(seq, bus, &sh);
3115
3116 done:
3117 return error;
3118 }
3119
3120 static int brcmf_sdio_forensic_read(struct seq_file *seq, void *data)
3121 {
3122 struct brcmf_bus *bus_if = dev_get_drvdata(seq->private);
3123 struct brcmf_sdio *bus = bus_if->bus_priv.sdio->bus;
3124
3125 return brcmf_sdio_died_dump(seq, bus);
3126 }
3127
3128 static int brcmf_debugfs_sdio_count_read(struct seq_file *seq, void *data)
3129 {
3130 struct brcmf_bus *bus_if = dev_get_drvdata(seq->private);
3131 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3132 struct brcmf_sdio_count *sdcnt = &sdiodev->bus->sdcnt;
3133
3134 seq_printf(seq,
3135 "intrcount: %u\nlastintrs: %u\n"
3136 "pollcnt: %u\nregfails: %u\n"
3137 "tx_sderrs: %u\nfcqueued: %u\n"
3138 "rxrtx: %u\nrx_toolong: %u\n"
3139 "rxc_errors: %u\nrx_hdrfail: %u\n"
3140 "rx_badhdr: %u\nrx_badseq: %u\n"
3141 "fc_rcvd: %u\nfc_xoff: %u\n"
3142 "fc_xon: %u\nrxglomfail: %u\n"
3143 "rxglomframes: %u\nrxglompkts: %u\n"
3144 "f2rxhdrs: %u\nf2rxdata: %u\n"
3145 "f2txdata: %u\nf1regdata: %u\n"
3146 "tickcnt: %u\ntx_ctlerrs: %lu\n"
3147 "tx_ctlpkts: %lu\nrx_ctlerrs: %lu\n"
3148 "rx_ctlpkts: %lu\nrx_readahead: %lu\n",
3149 sdcnt->intrcount, sdcnt->lastintrs,
3150 sdcnt->pollcnt, sdcnt->regfails,
3151 sdcnt->tx_sderrs, sdcnt->fcqueued,
3152 sdcnt->rxrtx, sdcnt->rx_toolong,
3153 sdcnt->rxc_errors, sdcnt->rx_hdrfail,
3154 sdcnt->rx_badhdr, sdcnt->rx_badseq,
3155 sdcnt->fc_rcvd, sdcnt->fc_xoff,
3156 sdcnt->fc_xon, sdcnt->rxglomfail,
3157 sdcnt->rxglomframes, sdcnt->rxglompkts,
3158 sdcnt->f2rxhdrs, sdcnt->f2rxdata,
3159 sdcnt->f2txdata, sdcnt->f1regdata,
3160 sdcnt->tickcnt, sdcnt->tx_ctlerrs,
3161 sdcnt->tx_ctlpkts, sdcnt->rx_ctlerrs,
3162 sdcnt->rx_ctlpkts, sdcnt->rx_readahead_cnt);
3163
3164 return 0;
3165 }
3166
3167 static void brcmf_sdio_debugfs_create(struct device *dev)
3168 {
3169 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3170 struct brcmf_pub *drvr = bus_if->drvr;
3171 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3172 struct brcmf_sdio *bus = sdiodev->bus;
3173 struct dentry *dentry = brcmf_debugfs_get_devdir(drvr);
3174
3175 if (IS_ERR_OR_NULL(dentry))
3176 return;
3177
3178 bus->console_interval = BRCMF_CONSOLE;
3179
3180 brcmf_debugfs_add_entry(drvr, "forensics", brcmf_sdio_forensic_read);
3181 brcmf_debugfs_add_entry(drvr, "counters",
3182 brcmf_debugfs_sdio_count_read);
3183 debugfs_create_u32("console_interval", 0644, dentry,
3184 &bus->console_interval);
3185 }
3186 #else
3187 static int brcmf_sdio_checkdied(struct brcmf_sdio *bus)
3188 {
3189 return 0;
3190 }
3191
3192 static void brcmf_sdio_debugfs_create(struct device *dev)
3193 {
3194 }
3195 #endif
3196
3197 static int
3198 brcmf_sdio_bus_rxctl(struct device *dev, unsigned char *msg, uint msglen)
3199 {
3200 int timeleft;
3201 uint rxlen = 0;
3202 bool pending;
3203 u8 *buf;
3204 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3205 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3206 struct brcmf_sdio *bus = sdiodev->bus;
3207
3208 brcmf_dbg(TRACE, "Enter\n");
3209 if (sdiodev->state != BRCMF_SDIOD_DATA)
3210 return -EIO;
3211
3212
3213 timeleft = brcmf_sdio_dcmd_resp_wait(bus, &bus->rxlen, &pending);
3214
3215 spin_lock_bh(&bus->rxctl_lock);
3216 rxlen = bus->rxlen;
3217 memcpy(msg, bus->rxctl, min(msglen, rxlen));
3218 bus->rxctl = NULL;
3219 buf = bus->rxctl_orig;
3220 bus->rxctl_orig = NULL;
3221 bus->rxlen = 0;
3222 spin_unlock_bh(&bus->rxctl_lock);
3223 vfree(buf);
3224
3225 if (rxlen) {
3226 brcmf_dbg(CTL, "resumed on rxctl frame, got %d expected %d\n",
3227 rxlen, msglen);
3228 } else if (timeleft == 0) {
3229 brcmf_err("resumed on timeout\n");
3230 brcmf_sdio_checkdied(bus);
3231 } else if (pending) {
3232 brcmf_dbg(CTL, "cancelled\n");
3233 return -ERESTARTSYS;
3234 } else {
3235 brcmf_dbg(CTL, "resumed for unknown reason?\n");
3236 brcmf_sdio_checkdied(bus);
3237 }
3238
3239 if (rxlen)
3240 bus->sdcnt.rx_ctlpkts++;
3241 else
3242 bus->sdcnt.rx_ctlerrs++;
3243
3244 return rxlen ? (int)rxlen : -ETIMEDOUT;
3245 }
3246
3247 #ifdef DEBUG
3248 static bool
3249 brcmf_sdio_verifymemory(struct brcmf_sdio_dev *sdiodev, u32 ram_addr,
3250 u8 *ram_data, uint ram_sz)
3251 {
3252 char *ram_cmp;
3253 int err;
3254 bool ret = true;
3255 int address;
3256 int offset;
3257 int len;
3258
3259
3260 brcmf_dbg(INFO, "Compare RAM dl & ul at 0x%08x; size=%d\n", ram_addr,
3261 ram_sz);
3262 ram_cmp = kmalloc(MEMBLOCK, GFP_KERNEL);
3263
3264 if (!ram_cmp)
3265 return true;
3266
3267 address = ram_addr;
3268 offset = 0;
3269 while (offset < ram_sz) {
3270 len = ((offset + MEMBLOCK) < ram_sz) ? MEMBLOCK :
3271 ram_sz - offset;
3272 err = brcmf_sdiod_ramrw(sdiodev, false, address, ram_cmp, len);
3273 if (err) {
3274 brcmf_err("error %d on reading %d membytes at 0x%08x\n",
3275 err, len, address);
3276 ret = false;
3277 break;
3278 } else if (memcmp(ram_cmp, &ram_data[offset], len)) {
3279 brcmf_err("Downloaded RAM image is corrupted, block offset is %d, len is %d\n",
3280 offset, len);
3281 ret = false;
3282 break;
3283 }
3284 offset += len;
3285 address += len;
3286 }
3287
3288 kfree(ram_cmp);
3289
3290 return ret;
3291 }
3292 #else
3293 static bool
3294 brcmf_sdio_verifymemory(struct brcmf_sdio_dev *sdiodev, u32 ram_addr,
3295 u8 *ram_data, uint ram_sz)
3296 {
3297 return true;
3298 }
3299 #endif
3300
3301 static int brcmf_sdio_download_code_file(struct brcmf_sdio *bus,
3302 const struct firmware *fw)
3303 {
3304 int err;
3305
3306 brcmf_dbg(TRACE, "Enter\n");
3307
3308 err = brcmf_sdiod_ramrw(bus->sdiodev, true, bus->ci->rambase,
3309 (u8 *)fw->data, fw->size);
3310 if (err)
3311 brcmf_err("error %d on writing %d membytes at 0x%08x\n",
3312 err, (int)fw->size, bus->ci->rambase);
3313 else if (!brcmf_sdio_verifymemory(bus->sdiodev, bus->ci->rambase,
3314 (u8 *)fw->data, fw->size))
3315 err = -EIO;
3316
3317 return err;
3318 }
3319
3320 static int brcmf_sdio_download_nvram(struct brcmf_sdio *bus,
3321 void *vars, u32 varsz)
3322 {
3323 int address;
3324 int err;
3325
3326 brcmf_dbg(TRACE, "Enter\n");
3327
3328 address = bus->ci->ramsize - varsz + bus->ci->rambase;
3329 err = brcmf_sdiod_ramrw(bus->sdiodev, true, address, vars, varsz);
3330 if (err)
3331 brcmf_err("error %d on writing %d nvram bytes at 0x%08x\n",
3332 err, varsz, address);
3333 else if (!brcmf_sdio_verifymemory(bus->sdiodev, address, vars, varsz))
3334 err = -EIO;
3335
3336 return err;
3337 }
3338
3339 static int brcmf_sdio_download_firmware(struct brcmf_sdio *bus,
3340 const struct firmware *fw,
3341 void *nvram, u32 nvlen)
3342 {
3343 int bcmerror;
3344 u32 rstvec;
3345
3346 sdio_claim_host(bus->sdiodev->func1);
3347 brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
3348
3349 rstvec = get_unaligned_le32(fw->data);
3350 brcmf_dbg(SDIO, "firmware rstvec: %x\n", rstvec);
3351
3352 bcmerror = brcmf_sdio_download_code_file(bus, fw);
3353 release_firmware(fw);
3354 if (bcmerror) {
3355 brcmf_err("dongle image file download failed\n");
3356 brcmf_fw_nvram_free(nvram);
3357 goto err;
3358 }
3359
3360 bcmerror = brcmf_sdio_download_nvram(bus, nvram, nvlen);
3361 brcmf_fw_nvram_free(nvram);
3362 if (bcmerror) {
3363 brcmf_err("dongle nvram file download failed\n");
3364 goto err;
3365 }
3366
3367
3368 if (!brcmf_chip_set_active(bus->ci, rstvec)) {
3369 brcmf_err("error getting out of ARM core reset\n");
3370 goto err;
3371 }
3372
3373 err:
3374 brcmf_sdio_clkctl(bus, CLK_SDONLY, false);
3375 sdio_release_host(bus->sdiodev->func1);
3376 return bcmerror;
3377 }
3378
3379 static bool brcmf_sdio_aos_no_decode(struct brcmf_sdio *bus)
3380 {
3381 if (bus->ci->chip == CY_CC_43012_CHIP_ID)
3382 return true;
3383 else
3384 return false;
3385 }
3386
3387 static void brcmf_sdio_sr_init(struct brcmf_sdio *bus)
3388 {
3389 int err = 0;
3390 u8 val;
3391 u8 wakeupctrl;
3392 u8 cardcap;
3393 u8 chipclkcsr;
3394
3395 brcmf_dbg(TRACE, "Enter\n");
3396
3397 if (brcmf_chip_is_ulp(bus->ci)) {
3398 wakeupctrl = SBSDIO_FUNC1_WCTRL_ALPWAIT_SHIFT;
3399 chipclkcsr = SBSDIO_HT_AVAIL_REQ;
3400 } else {
3401 wakeupctrl = SBSDIO_FUNC1_WCTRL_HTWAIT_SHIFT;
3402 chipclkcsr = SBSDIO_FORCE_HT;
3403 }
3404
3405 if (brcmf_sdio_aos_no_decode(bus)) {
3406 cardcap = SDIO_CCCR_BRCM_CARDCAP_CMD_NODEC;
3407 } else {
3408 cardcap = (SDIO_CCCR_BRCM_CARDCAP_CMD14_SUPPORT |
3409 SDIO_CCCR_BRCM_CARDCAP_CMD14_EXT);
3410 }
3411
3412 val = brcmf_sdiod_readb(bus->sdiodev, SBSDIO_FUNC1_WAKEUPCTRL, &err);
3413 if (err) {
3414 brcmf_err("error reading SBSDIO_FUNC1_WAKEUPCTRL\n");
3415 return;
3416 }
3417 val |= 1 << wakeupctrl;
3418 brcmf_sdiod_writeb(bus->sdiodev, SBSDIO_FUNC1_WAKEUPCTRL, val, &err);
3419 if (err) {
3420 brcmf_err("error writing SBSDIO_FUNC1_WAKEUPCTRL\n");
3421 return;
3422 }
3423
3424
3425 brcmf_sdiod_func0_wb(bus->sdiodev, SDIO_CCCR_BRCM_CARDCAP,
3426 cardcap,
3427 &err);
3428 if (err) {
3429 brcmf_err("error writing SDIO_CCCR_BRCM_CARDCAP\n");
3430 return;
3431 }
3432
3433 brcmf_sdiod_writeb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3434 chipclkcsr, &err);
3435 if (err) {
3436 brcmf_err("error writing SBSDIO_FUNC1_CHIPCLKCSR\n");
3437 return;
3438 }
3439
3440
3441 bus->sr_enabled = true;
3442 brcmf_dbg(INFO, "SR enabled\n");
3443 }
3444
3445
3446 static int brcmf_sdio_kso_init(struct brcmf_sdio *bus)
3447 {
3448 struct brcmf_core *core = bus->sdio_core;
3449 u8 val;
3450 int err = 0;
3451
3452 brcmf_dbg(TRACE, "Enter\n");
3453
3454
3455 if (core->rev < 12)
3456 return 0;
3457
3458 val = brcmf_sdiod_readb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR, &err);
3459 if (err) {
3460 brcmf_err("error reading SBSDIO_FUNC1_SLEEPCSR\n");
3461 return err;
3462 }
3463
3464 if (!(val & SBSDIO_FUNC1_SLEEPCSR_KSO_MASK)) {
3465 val |= (SBSDIO_FUNC1_SLEEPCSR_KSO_EN <<
3466 SBSDIO_FUNC1_SLEEPCSR_KSO_SHIFT);
3467 brcmf_sdiod_writeb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
3468 val, &err);
3469 if (err) {
3470 brcmf_err("error writing SBSDIO_FUNC1_SLEEPCSR\n");
3471 return err;
3472 }
3473 }
3474
3475 return 0;
3476 }
3477
3478
3479 static int brcmf_sdio_bus_preinit(struct device *dev)
3480 {
3481 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3482 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3483 struct brcmf_sdio *bus = sdiodev->bus;
3484 struct brcmf_core *core = bus->sdio_core;
3485 u32 value;
3486 int err;
3487
3488
3489 if (WARN_ON(!bus_if->maxctl))
3490 return -EINVAL;
3491
3492
3493 bus_if->maxctl += bus->roundup;
3494 value = roundup((bus_if->maxctl + SDPCM_HDRLEN), ALIGNMENT);
3495 value += bus->head_align;
3496 bus->rxbuf = kmalloc(value, GFP_ATOMIC);
3497 if (bus->rxbuf)
3498 bus->rxblen = value;
3499
3500
3501
3502
3503
3504 if (core->rev < 12) {
3505
3506 value = 0;
3507 err = brcmf_iovar_data_set(dev, "bus:txglom", &value,
3508 sizeof(u32));
3509 } else {
3510
3511 value = sdiodev->settings->bus.sdio.sd_sgentry_align;
3512
3513 value = max_t(u32, value, ALIGNMENT);
3514 err = brcmf_iovar_data_set(dev, "bus:txglomalign", &value,
3515 sizeof(u32));
3516 }
3517
3518 if (err < 0)
3519 goto done;
3520
3521 bus->tx_hdrlen = SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN;
3522 if (sdiodev->sg_support) {
3523 bus->txglom = false;
3524 value = 1;
3525 err = brcmf_iovar_data_set(bus->sdiodev->dev, "bus:rxglom",
3526 &value, sizeof(u32));
3527 if (err < 0) {
3528
3529 err = 0;
3530 } else {
3531 bus->txglom = true;
3532 bus->tx_hdrlen += SDPCM_HWEXT_LEN;
3533 }
3534 }
3535 brcmf_bus_add_txhdrlen(bus->sdiodev->dev, bus->tx_hdrlen);
3536
3537 done:
3538 return err;
3539 }
3540
3541 static size_t brcmf_sdio_bus_get_ramsize(struct device *dev)
3542 {
3543 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3544 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3545 struct brcmf_sdio *bus = sdiodev->bus;
3546
3547 return bus->ci->ramsize - bus->ci->srsize;
3548 }
3549
3550 static int brcmf_sdio_bus_get_memdump(struct device *dev, void *data,
3551 size_t mem_size)
3552 {
3553 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3554 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3555 struct brcmf_sdio *bus = sdiodev->bus;
3556 int err;
3557 int address;
3558 int offset;
3559 int len;
3560
3561 brcmf_dbg(INFO, "dump at 0x%08x: size=%zu\n", bus->ci->rambase,
3562 mem_size);
3563
3564 address = bus->ci->rambase;
3565 offset = err = 0;
3566 sdio_claim_host(sdiodev->func1);
3567 while (offset < mem_size) {
3568 len = ((offset + MEMBLOCK) < mem_size) ? MEMBLOCK :
3569 mem_size - offset;
3570 err = brcmf_sdiod_ramrw(sdiodev, false, address, data, len);
3571 if (err) {
3572 brcmf_err("error %d on reading %d membytes at 0x%08x\n",
3573 err, len, address);
3574 goto done;
3575 }
3576 data += len;
3577 offset += len;
3578 address += len;
3579 }
3580
3581 done:
3582 sdio_release_host(sdiodev->func1);
3583 return err;
3584 }
3585
3586 void brcmf_sdio_trigger_dpc(struct brcmf_sdio *bus)
3587 {
3588 if (!bus->dpc_triggered) {
3589 bus->dpc_triggered = true;
3590 queue_work(bus->brcmf_wq, &bus->datawork);
3591 }
3592 }
3593
3594 void brcmf_sdio_isr(struct brcmf_sdio *bus)
3595 {
3596 brcmf_dbg(TRACE, "Enter\n");
3597
3598 if (!bus) {
3599 brcmf_err("bus is null pointer, exiting\n");
3600 return;
3601 }
3602
3603
3604 bus->sdcnt.intrcount++;
3605 if (in_interrupt())
3606 atomic_set(&bus->ipend, 1);
3607 else
3608 if (brcmf_sdio_intr_rstatus(bus)) {
3609 brcmf_err("failed backplane access\n");
3610 }
3611
3612
3613 if (!bus->intr)
3614 brcmf_err("isr w/o interrupt configured!\n");
3615
3616 bus->dpc_triggered = true;
3617 queue_work(bus->brcmf_wq, &bus->datawork);
3618 }
3619
3620 static void brcmf_sdio_bus_watchdog(struct brcmf_sdio *bus)
3621 {
3622 brcmf_dbg(TIMER, "Enter\n");
3623
3624
3625 if (!bus->sr_enabled &&
3626 bus->poll && (++bus->polltick >= bus->pollrate)) {
3627 u32 intstatus = 0;
3628
3629
3630 bus->polltick = 0;
3631
3632
3633 if (!bus->intr ||
3634 (bus->sdcnt.intrcount == bus->sdcnt.lastintrs)) {
3635
3636 if (!bus->dpc_triggered) {
3637 u8 devpend;
3638
3639 sdio_claim_host(bus->sdiodev->func1);
3640 devpend = brcmf_sdiod_func0_rb(bus->sdiodev,
3641 SDIO_CCCR_INTx, NULL);
3642 sdio_release_host(bus->sdiodev->func1);
3643 intstatus = devpend & (INTR_STATUS_FUNC1 |
3644 INTR_STATUS_FUNC2);
3645 }
3646
3647
3648
3649 if (intstatus) {
3650 bus->sdcnt.pollcnt++;
3651 atomic_set(&bus->ipend, 1);
3652
3653 bus->dpc_triggered = true;
3654 queue_work(bus->brcmf_wq, &bus->datawork);
3655 }
3656 }
3657
3658
3659 bus->sdcnt.lastintrs = bus->sdcnt.intrcount;
3660 }
3661 #ifdef DEBUG
3662
3663 if (bus->sdiodev->state == BRCMF_SDIOD_DATA && BRCMF_FWCON_ON() &&
3664 bus->console_interval != 0) {
3665 bus->console.count += jiffies_to_msecs(BRCMF_WD_POLL);
3666 if (bus->console.count >= bus->console_interval) {
3667 bus->console.count -= bus->console_interval;
3668 sdio_claim_host(bus->sdiodev->func1);
3669
3670 brcmf_sdio_bus_sleep(bus, false, false);
3671 if (brcmf_sdio_readconsole(bus) < 0)
3672
3673 bus->console_interval = 0;
3674 sdio_release_host(bus->sdiodev->func1);
3675 }
3676 }
3677 #endif
3678
3679
3680 if (!bus->dpc_triggered) {
3681 rmb();
3682 if ((!bus->dpc_running) && (bus->idletime > 0) &&
3683 (bus->clkstate == CLK_AVAIL)) {
3684 bus->idlecount++;
3685 if (bus->idlecount > bus->idletime) {
3686 brcmf_dbg(SDIO, "idle\n");
3687 sdio_claim_host(bus->sdiodev->func1);
3688 brcmf_sdio_wd_timer(bus, false);
3689 bus->idlecount = 0;
3690 brcmf_sdio_bus_sleep(bus, true, false);
3691 sdio_release_host(bus->sdiodev->func1);
3692 }
3693 } else {
3694 bus->idlecount = 0;
3695 }
3696 } else {
3697 bus->idlecount = 0;
3698 }
3699 }
3700
3701 static void brcmf_sdio_dataworker(struct work_struct *work)
3702 {
3703 struct brcmf_sdio *bus = container_of(work, struct brcmf_sdio,
3704 datawork);
3705
3706 bus->dpc_running = true;
3707 wmb();
3708 while (READ_ONCE(bus->dpc_triggered)) {
3709 bus->dpc_triggered = false;
3710 brcmf_sdio_dpc(bus);
3711 bus->idlecount = 0;
3712 }
3713 bus->dpc_running = false;
3714 if (brcmf_sdiod_freezing(bus->sdiodev)) {
3715 brcmf_sdiod_change_state(bus->sdiodev, BRCMF_SDIOD_DOWN);
3716 brcmf_sdiod_try_freeze(bus->sdiodev);
3717 brcmf_sdiod_change_state(bus->sdiodev, BRCMF_SDIOD_DATA);
3718 }
3719 }
3720
3721 static void
3722 brcmf_sdio_drivestrengthinit(struct brcmf_sdio_dev *sdiodev,
3723 struct brcmf_chip *ci, u32 drivestrength)
3724 {
3725 const struct sdiod_drive_str *str_tab = NULL;
3726 u32 str_mask;
3727 u32 str_shift;
3728 u32 i;
3729 u32 drivestrength_sel = 0;
3730 u32 cc_data_temp;
3731 u32 addr;
3732
3733 if (!(ci->cc_caps & CC_CAP_PMU))
3734 return;
3735
3736 switch (SDIOD_DRVSTR_KEY(ci->chip, ci->pmurev)) {
3737 case SDIOD_DRVSTR_KEY(BRCM_CC_4330_CHIP_ID, 12):
3738 str_tab = sdiod_drvstr_tab1_1v8;
3739 str_mask = 0x00003800;
3740 str_shift = 11;
3741 break;
3742 case SDIOD_DRVSTR_KEY(BRCM_CC_4334_CHIP_ID, 17):
3743 str_tab = sdiod_drvstr_tab6_1v8;
3744 str_mask = 0x00001800;
3745 str_shift = 11;
3746 break;
3747 case SDIOD_DRVSTR_KEY(BRCM_CC_43143_CHIP_ID, 17):
3748
3749 i = ARRAY_SIZE(sdiod_drvstr_tab2_3v3) - 1;
3750 if (drivestrength >= sdiod_drvstr_tab2_3v3[i].strength) {
3751 str_tab = sdiod_drvstr_tab2_3v3;
3752 str_mask = 0x00000007;
3753 str_shift = 0;
3754 } else
3755 brcmf_err("Invalid SDIO Drive strength for chip %s, strength=%d\n",
3756 ci->name, drivestrength);
3757 break;
3758 case SDIOD_DRVSTR_KEY(BRCM_CC_43362_CHIP_ID, 13):
3759 str_tab = sdiod_drive_strength_tab5_1v8;
3760 str_mask = 0x00003800;
3761 str_shift = 11;
3762 break;
3763 default:
3764 brcmf_dbg(INFO, "No SDIO driver strength init needed for chip %s rev %d pmurev %d\n",
3765 ci->name, ci->chiprev, ci->pmurev);
3766 break;
3767 }
3768
3769 if (str_tab != NULL) {
3770 struct brcmf_core *pmu = brcmf_chip_get_pmu(ci);
3771
3772 for (i = 0; str_tab[i].strength != 0; i++) {
3773 if (drivestrength >= str_tab[i].strength) {
3774 drivestrength_sel = str_tab[i].sel;
3775 break;
3776 }
3777 }
3778 addr = CORE_CC_REG(pmu->base, chipcontrol_addr);
3779 brcmf_sdiod_writel(sdiodev, addr, 1, NULL);
3780 cc_data_temp = brcmf_sdiod_readl(sdiodev, addr, NULL);
3781 cc_data_temp &= ~str_mask;
3782 drivestrength_sel <<= str_shift;
3783 cc_data_temp |= drivestrength_sel;
3784 brcmf_sdiod_writel(sdiodev, addr, cc_data_temp, NULL);
3785
3786 brcmf_dbg(INFO, "SDIO: %d mA (req=%d mA) drive strength selected, set to 0x%08x\n",
3787 str_tab[i].strength, drivestrength, cc_data_temp);
3788 }
3789 }
3790
3791 static int brcmf_sdio_buscoreprep(void *ctx)
3792 {
3793 struct brcmf_sdio_dev *sdiodev = ctx;
3794 int err = 0;
3795 u8 clkval, clkset;
3796
3797
3798 clkset = SBSDIO_FORCE_HW_CLKREQ_OFF | SBSDIO_ALP_AVAIL_REQ;
3799 brcmf_sdiod_writeb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, clkset, &err);
3800 if (err) {
3801 brcmf_err("error writing for HT off\n");
3802 return err;
3803 }
3804
3805
3806
3807 clkval = brcmf_sdiod_readb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, NULL);
3808
3809 if ((clkval & ~SBSDIO_AVBITS) != clkset) {
3810 brcmf_err("ChipClkCSR access: wrote 0x%02x read 0x%02x\n",
3811 clkset, clkval);
3812 return -EACCES;
3813 }
3814
3815 SPINWAIT(((clkval = brcmf_sdiod_readb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3816 NULL)),
3817 !SBSDIO_ALPAV(clkval)),
3818 PMU_MAX_TRANSITION_DLY);
3819
3820 if (!SBSDIO_ALPAV(clkval)) {
3821 brcmf_err("timeout on ALPAV wait, clkval 0x%02x\n",
3822 clkval);
3823 return -EBUSY;
3824 }
3825
3826 clkset = SBSDIO_FORCE_HW_CLKREQ_OFF | SBSDIO_FORCE_ALP;
3827 brcmf_sdiod_writeb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, clkset, &err);
3828 udelay(65);
3829
3830
3831 brcmf_sdiod_writeb(sdiodev, SBSDIO_FUNC1_SDIOPULLUP, 0, NULL);
3832
3833 return 0;
3834 }
3835
3836 static void brcmf_sdio_buscore_activate(void *ctx, struct brcmf_chip *chip,
3837 u32 rstvec)
3838 {
3839 struct brcmf_sdio_dev *sdiodev = ctx;
3840 struct brcmf_core *core = sdiodev->bus->sdio_core;
3841 u32 reg_addr;
3842
3843
3844 reg_addr = core->base + SD_REG(intstatus);
3845 brcmf_sdiod_writel(sdiodev, reg_addr, 0xFFFFFFFF, NULL);
3846
3847 if (rstvec)
3848
3849 brcmf_sdiod_ramrw(sdiodev, true, 0, (void *)&rstvec,
3850 sizeof(rstvec));
3851 }
3852
3853 static u32 brcmf_sdio_buscore_read32(void *ctx, u32 addr)
3854 {
3855 struct brcmf_sdio_dev *sdiodev = ctx;
3856 u32 val, rev;
3857
3858 val = brcmf_sdiod_readl(sdiodev, addr, NULL);
3859
3860
3861
3862
3863
3864
3865
3866
3867 if (addr == CORE_CC_REG(SI_ENUM_BASE, chipid) &&
3868 (sdiodev->func1->device == SDIO_DEVICE_ID_BROADCOM_4339 ||
3869 sdiodev->func1->device == SDIO_DEVICE_ID_BROADCOM_4335_4339)) {
3870 rev = (val & CID_REV_MASK) >> CID_REV_SHIFT;
3871 if (rev >= 2) {
3872 val &= ~CID_ID_MASK;
3873 val |= BRCM_CC_4339_CHIP_ID;
3874 }
3875 }
3876
3877 return val;
3878 }
3879
3880 static void brcmf_sdio_buscore_write32(void *ctx, u32 addr, u32 val)
3881 {
3882 struct brcmf_sdio_dev *sdiodev = ctx;
3883
3884 brcmf_sdiod_writel(sdiodev, addr, val, NULL);
3885 }
3886
3887 static const struct brcmf_buscore_ops brcmf_sdio_buscore_ops = {
3888 .prepare = brcmf_sdio_buscoreprep,
3889 .activate = brcmf_sdio_buscore_activate,
3890 .read32 = brcmf_sdio_buscore_read32,
3891 .write32 = brcmf_sdio_buscore_write32,
3892 };
3893
3894 static bool
3895 brcmf_sdio_probe_attach(struct brcmf_sdio *bus)
3896 {
3897 struct brcmf_sdio_dev *sdiodev;
3898 u8 clkctl = 0;
3899 int err = 0;
3900 int reg_addr;
3901 u32 reg_val;
3902 u32 drivestrength;
3903
3904 sdiodev = bus->sdiodev;
3905 sdio_claim_host(sdiodev->func1);
3906
3907 pr_debug("F1 signature read @0x18000000=0x%4x\n",
3908 brcmf_sdiod_readl(sdiodev, SI_ENUM_BASE, NULL));
3909
3910
3911
3912
3913
3914
3915 brcmf_sdiod_writeb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, BRCMF_INIT_CLKCTL1,
3916 &err);
3917 if (!err)
3918 clkctl = brcmf_sdiod_readb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3919 &err);
3920
3921 if (err || ((clkctl & ~SBSDIO_AVBITS) != BRCMF_INIT_CLKCTL1)) {
3922 brcmf_err("ChipClkCSR access: err %d wrote 0x%02x read 0x%02x\n",
3923 err, BRCMF_INIT_CLKCTL1, clkctl);
3924 goto fail;
3925 }
3926
3927 bus->ci = brcmf_chip_attach(sdiodev, &brcmf_sdio_buscore_ops);
3928 if (IS_ERR(bus->ci)) {
3929 brcmf_err("brcmf_chip_attach failed!\n");
3930 bus->ci = NULL;
3931 goto fail;
3932 }
3933
3934
3935 bus->sdio_core = brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV);
3936 if (!bus->sdio_core)
3937 goto fail;
3938
3939
3940 sdiodev->cc_core = brcmf_chip_get_core(bus->ci, BCMA_CORE_CHIPCOMMON);
3941 if (!sdiodev->cc_core)
3942 goto fail;
3943
3944 sdiodev->settings = brcmf_get_module_param(sdiodev->dev,
3945 BRCMF_BUSTYPE_SDIO,
3946 bus->ci->chip,
3947 bus->ci->chiprev);
3948 if (!sdiodev->settings) {
3949 brcmf_err("Failed to get device parameters\n");
3950 goto fail;
3951 }
3952
3953
3954
3955 bus->head_align = ALIGNMENT;
3956 bus->sgentry_align = ALIGNMENT;
3957 if (sdiodev->settings->bus.sdio.sd_head_align > ALIGNMENT)
3958 bus->head_align = sdiodev->settings->bus.sdio.sd_head_align;
3959 if (sdiodev->settings->bus.sdio.sd_sgentry_align > ALIGNMENT)
3960 bus->sgentry_align =
3961 sdiodev->settings->bus.sdio.sd_sgentry_align;
3962
3963
3964
3965
3966 brcmf_sdiod_sgtable_alloc(sdiodev);
3967
3968 #ifdef CONFIG_PM_SLEEP
3969
3970
3971
3972 if ((sdio_get_host_pm_caps(sdiodev->func1) & MMC_PM_KEEP_POWER) &&
3973 ((sdio_get_host_pm_caps(sdiodev->func1) & MMC_PM_WAKE_SDIO_IRQ) ||
3974 (sdiodev->settings->bus.sdio.oob_irq_supported)))
3975 sdiodev->bus_if->wowl_supported = true;
3976 #endif
3977
3978 if (brcmf_sdio_kso_init(bus)) {
3979 brcmf_err("error enabling KSO\n");
3980 goto fail;
3981 }
3982
3983 if (sdiodev->settings->bus.sdio.drive_strength)
3984 drivestrength = sdiodev->settings->bus.sdio.drive_strength;
3985 else
3986 drivestrength = DEFAULT_SDIO_DRIVE_STRENGTH;
3987 brcmf_sdio_drivestrengthinit(sdiodev, bus->ci, drivestrength);
3988
3989
3990 reg_val = brcmf_sdiod_func0_rb(sdiodev, SDIO_CCCR_BRCM_CARDCTRL, &err);
3991 if (err)
3992 goto fail;
3993
3994 reg_val |= SDIO_CCCR_BRCM_CARDCTRL_WLANRESET;
3995
3996 brcmf_sdiod_func0_wb(sdiodev, SDIO_CCCR_BRCM_CARDCTRL, reg_val, &err);
3997 if (err)
3998 goto fail;
3999
4000
4001 reg_addr = CORE_CC_REG(brcmf_chip_get_pmu(bus->ci)->base, pmucontrol);
4002 reg_val = brcmf_sdiod_readl(sdiodev, reg_addr, &err);
4003 if (err)
4004 goto fail;
4005
4006 reg_val |= (BCMA_CC_PMU_CTL_RES_RELOAD << BCMA_CC_PMU_CTL_RES_SHIFT);
4007
4008 brcmf_sdiod_writel(sdiodev, reg_addr, reg_val, &err);
4009 if (err)
4010 goto fail;
4011
4012 sdio_release_host(sdiodev->func1);
4013
4014 brcmu_pktq_init(&bus->txq, (PRIOMASK + 1), TXQLEN);
4015
4016
4017 bus->hdrbuf = kzalloc(MAX_HDR_READ + bus->head_align, GFP_KERNEL);
4018 if (!bus->hdrbuf)
4019 return false;
4020
4021 bus->rxhdr = (u8 *) roundup((unsigned long)&bus->hdrbuf[0],
4022 bus->head_align);
4023
4024
4025 bus->intr = true;
4026 bus->poll = false;
4027 if (bus->poll)
4028 bus->pollrate = 1;
4029
4030 return true;
4031
4032 fail:
4033 sdio_release_host(sdiodev->func1);
4034 return false;
4035 }
4036
4037 static int
4038 brcmf_sdio_watchdog_thread(void *data)
4039 {
4040 struct brcmf_sdio *bus = (struct brcmf_sdio *)data;
4041 int wait;
4042
4043 allow_signal(SIGTERM);
4044
4045 brcmf_sdiod_freezer_count(bus->sdiodev);
4046 while (1) {
4047 if (kthread_should_stop())
4048 break;
4049 brcmf_sdiod_freezer_uncount(bus->sdiodev);
4050 wait = wait_for_completion_interruptible(&bus->watchdog_wait);
4051 brcmf_sdiod_freezer_count(bus->sdiodev);
4052 brcmf_sdiod_try_freeze(bus->sdiodev);
4053 if (!wait) {
4054 brcmf_sdio_bus_watchdog(bus);
4055
4056 bus->sdcnt.tickcnt++;
4057 reinit_completion(&bus->watchdog_wait);
4058 } else
4059 break;
4060 }
4061 return 0;
4062 }
4063
4064 static void
4065 brcmf_sdio_watchdog(struct timer_list *t)
4066 {
4067 struct brcmf_sdio *bus = from_timer(bus, t, timer);
4068
4069 if (bus->watchdog_tsk) {
4070 complete(&bus->watchdog_wait);
4071
4072 if (bus->wd_active)
4073 mod_timer(&bus->timer,
4074 jiffies + BRCMF_WD_POLL);
4075 }
4076 }
4077
4078 static
4079 int brcmf_sdio_get_fwname(struct device *dev, const char *ext, u8 *fw_name)
4080 {
4081 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
4082 struct brcmf_fw_request *fwreq;
4083 struct brcmf_fw_name fwnames[] = {
4084 { ext, fw_name },
4085 };
4086
4087 fwreq = brcmf_fw_alloc_request(bus_if->chip, bus_if->chiprev,
4088 brcmf_sdio_fwnames,
4089 ARRAY_SIZE(brcmf_sdio_fwnames),
4090 fwnames, ARRAY_SIZE(fwnames));
4091 if (!fwreq)
4092 return -ENOMEM;
4093
4094 kfree(fwreq);
4095 return 0;
4096 }
4097
4098 static const struct brcmf_bus_ops brcmf_sdio_bus_ops = {
4099 .stop = brcmf_sdio_bus_stop,
4100 .preinit = brcmf_sdio_bus_preinit,
4101 .txdata = brcmf_sdio_bus_txdata,
4102 .txctl = brcmf_sdio_bus_txctl,
4103 .rxctl = brcmf_sdio_bus_rxctl,
4104 .gettxq = brcmf_sdio_bus_gettxq,
4105 .wowl_config = brcmf_sdio_wowl_config,
4106 .get_ramsize = brcmf_sdio_bus_get_ramsize,
4107 .get_memdump = brcmf_sdio_bus_get_memdump,
4108 .get_fwname = brcmf_sdio_get_fwname,
4109 .debugfs_create = brcmf_sdio_debugfs_create
4110 };
4111
4112 #define BRCMF_SDIO_FW_CODE 0
4113 #define BRCMF_SDIO_FW_NVRAM 1
4114
4115 static void brcmf_sdio_firmware_callback(struct device *dev, int err,
4116 struct brcmf_fw_request *fwreq)
4117 {
4118 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
4119 struct brcmf_sdio_dev *sdiod = bus_if->bus_priv.sdio;
4120 struct brcmf_sdio *bus = sdiod->bus;
4121 struct brcmf_core *core = bus->sdio_core;
4122 const struct firmware *code;
4123 void *nvram;
4124 u32 nvram_len;
4125 u8 saveclk, bpreq;
4126 u8 devctl;
4127
4128 brcmf_dbg(TRACE, "Enter: dev=%s, err=%d\n", dev_name(dev), err);
4129
4130 if (err)
4131 goto fail;
4132
4133 code = fwreq->items[BRCMF_SDIO_FW_CODE].binary;
4134 nvram = fwreq->items[BRCMF_SDIO_FW_NVRAM].nv_data.data;
4135 nvram_len = fwreq->items[BRCMF_SDIO_FW_NVRAM].nv_data.len;
4136 kfree(fwreq);
4137
4138
4139 bus->alp_only = true;
4140 err = brcmf_sdio_download_firmware(bus, code, nvram, nvram_len);
4141 if (err)
4142 goto fail;
4143 bus->alp_only = false;
4144
4145
4146 bus->sdcnt.tickcnt = 0;
4147 brcmf_sdio_wd_timer(bus, true);
4148
4149 sdio_claim_host(sdiod->func1);
4150
4151
4152 brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
4153 if (bus->clkstate != CLK_AVAIL)
4154 goto release;
4155
4156
4157 saveclk = brcmf_sdiod_readb(sdiod, SBSDIO_FUNC1_CHIPCLKCSR, &err);
4158 if (!err) {
4159 bpreq = saveclk;
4160 bpreq |= brcmf_chip_is_ulp(bus->ci) ?
4161 SBSDIO_HT_AVAIL_REQ : SBSDIO_FORCE_HT;
4162 brcmf_sdiod_writeb(sdiod, SBSDIO_FUNC1_CHIPCLKCSR,
4163 bpreq, &err);
4164 }
4165 if (err) {
4166 brcmf_err("Failed to force clock for F2: err %d\n", err);
4167 goto release;
4168 }
4169
4170
4171 brcmf_sdiod_writel(sdiod, core->base + SD_REG(tosbmailboxdata),
4172 SDPCM_PROT_VERSION << SMB_DATA_VERSION_SHIFT, NULL);
4173
4174 err = sdio_enable_func(sdiod->func2);
4175
4176 brcmf_dbg(INFO, "enable F2: err=%d\n", err);
4177
4178
4179 if (!err) {
4180
4181 bus->hostintmask = HOSTINTMASK;
4182 brcmf_sdiod_writel(sdiod, core->base + SD_REG(hostintmask),
4183 bus->hostintmask, NULL);
4184
4185 switch (sdiod->func1->device) {
4186 case SDIO_DEVICE_ID_CYPRESS_4373:
4187 brcmf_dbg(INFO, "set F2 watermark to 0x%x*4 bytes\n",
4188 CY_4373_F2_WATERMARK);
4189 brcmf_sdiod_writeb(sdiod, SBSDIO_WATERMARK,
4190 CY_4373_F2_WATERMARK, &err);
4191 devctl = brcmf_sdiod_readb(sdiod, SBSDIO_DEVICE_CTL,
4192 &err);
4193 devctl |= SBSDIO_DEVCTL_F2WM_ENAB;
4194 brcmf_sdiod_writeb(sdiod, SBSDIO_DEVICE_CTL, devctl,
4195 &err);
4196 brcmf_sdiod_writeb(sdiod, SBSDIO_FUNC1_MESBUSYCTRL,
4197 CY_4373_F2_WATERMARK |
4198 SBSDIO_MESBUSYCTRL_ENAB, &err);
4199 break;
4200 case SDIO_DEVICE_ID_CYPRESS_43012:
4201 brcmf_dbg(INFO, "set F2 watermark to 0x%x*4 bytes\n",
4202 CY_43012_F2_WATERMARK);
4203 brcmf_sdiod_writeb(sdiod, SBSDIO_WATERMARK,
4204 CY_43012_F2_WATERMARK, &err);
4205 devctl = brcmf_sdiod_readb(sdiod, SBSDIO_DEVICE_CTL,
4206 &err);
4207 devctl |= SBSDIO_DEVCTL_F2WM_ENAB;
4208 brcmf_sdiod_writeb(sdiod, SBSDIO_DEVICE_CTL, devctl,
4209 &err);
4210 break;
4211 default:
4212 brcmf_sdiod_writeb(sdiod, SBSDIO_WATERMARK,
4213 DEFAULT_F2_WATERMARK, &err);
4214 break;
4215 }
4216 } else {
4217
4218 sdio_disable_func(sdiod->func2);
4219 goto checkdied;
4220 }
4221
4222 if (brcmf_chip_sr_capable(bus->ci)) {
4223 brcmf_sdio_sr_init(bus);
4224 } else {
4225
4226 brcmf_sdiod_writeb(sdiod, SBSDIO_FUNC1_CHIPCLKCSR,
4227 saveclk, &err);
4228 }
4229
4230 if (err == 0) {
4231
4232 sdiod->bus_if->dev = sdiod->dev;
4233 sdiod->bus_if->ops = &brcmf_sdio_bus_ops;
4234 sdiod->bus_if->chip = bus->ci->chip;
4235 sdiod->bus_if->chiprev = bus->ci->chiprev;
4236
4237
4238 brcmf_sdiod_change_state(bus->sdiodev, BRCMF_SDIOD_DATA);
4239
4240 err = brcmf_sdiod_intr_register(sdiod);
4241 if (err != 0)
4242 brcmf_err("intr register failed:%d\n", err);
4243 }
4244
4245
4246 if (err != 0) {
4247 brcmf_sdio_clkctl(bus, CLK_NONE, false);
4248 goto checkdied;
4249 }
4250
4251 sdio_release_host(sdiod->func1);
4252
4253 err = brcmf_alloc(sdiod->dev, sdiod->settings);
4254 if (err) {
4255 brcmf_err("brcmf_alloc failed\n");
4256 goto claim;
4257 }
4258
4259
4260 err = brcmf_attach(sdiod->dev);
4261 if (err != 0) {
4262 brcmf_err("brcmf_attach failed\n");
4263 goto free;
4264 }
4265
4266
4267 return;
4268
4269 free:
4270 brcmf_free(sdiod->dev);
4271 claim:
4272 sdio_claim_host(sdiod->func1);
4273 checkdied:
4274 brcmf_sdio_checkdied(bus);
4275 release:
4276 sdio_release_host(sdiod->func1);
4277 fail:
4278 brcmf_dbg(TRACE, "failed: dev=%s, err=%d\n", dev_name(dev), err);
4279 device_release_driver(&sdiod->func2->dev);
4280 device_release_driver(dev);
4281 }
4282
4283 static struct brcmf_fw_request *
4284 brcmf_sdio_prepare_fw_request(struct brcmf_sdio *bus)
4285 {
4286 struct brcmf_fw_request *fwreq;
4287 struct brcmf_fw_name fwnames[] = {
4288 { ".bin", bus->sdiodev->fw_name },
4289 { ".txt", bus->sdiodev->nvram_name },
4290 };
4291
4292 fwreq = brcmf_fw_alloc_request(bus->ci->chip, bus->ci->chiprev,
4293 brcmf_sdio_fwnames,
4294 ARRAY_SIZE(brcmf_sdio_fwnames),
4295 fwnames, ARRAY_SIZE(fwnames));
4296 if (!fwreq)
4297 return NULL;
4298
4299 fwreq->items[BRCMF_SDIO_FW_CODE].type = BRCMF_FW_TYPE_BINARY;
4300 fwreq->items[BRCMF_SDIO_FW_NVRAM].type = BRCMF_FW_TYPE_NVRAM;
4301 fwreq->board_type = bus->sdiodev->settings->board_type;
4302
4303 return fwreq;
4304 }
4305
4306 struct brcmf_sdio *brcmf_sdio_probe(struct brcmf_sdio_dev *sdiodev)
4307 {
4308 int ret;
4309 struct brcmf_sdio *bus;
4310 struct workqueue_struct *wq;
4311 struct brcmf_fw_request *fwreq;
4312
4313 brcmf_dbg(TRACE, "Enter\n");
4314
4315
4316 bus = kzalloc(sizeof(struct brcmf_sdio), GFP_ATOMIC);
4317 if (!bus)
4318 goto fail;
4319
4320 bus->sdiodev = sdiodev;
4321 sdiodev->bus = bus;
4322 skb_queue_head_init(&bus->glom);
4323 bus->txbound = BRCMF_TXBOUND;
4324 bus->rxbound = BRCMF_RXBOUND;
4325 bus->txminmax = BRCMF_TXMINMAX;
4326 bus->tx_seq = SDPCM_SEQ_WRAP - 1;
4327
4328
4329 wq = alloc_ordered_workqueue("brcmf_wq/%s", WQ_MEM_RECLAIM,
4330 dev_name(&sdiodev->func1->dev));
4331 if (!wq) {
4332 brcmf_err("insufficient memory to create txworkqueue\n");
4333 goto fail;
4334 }
4335 brcmf_sdiod_freezer_count(sdiodev);
4336 INIT_WORK(&bus->datawork, brcmf_sdio_dataworker);
4337 bus->brcmf_wq = wq;
4338
4339
4340 if (!(brcmf_sdio_probe_attach(bus))) {
4341 brcmf_err("brcmf_sdio_probe_attach failed\n");
4342 goto fail;
4343 }
4344
4345 spin_lock_init(&bus->rxctl_lock);
4346 spin_lock_init(&bus->txq_lock);
4347 init_waitqueue_head(&bus->ctrl_wait);
4348 init_waitqueue_head(&bus->dcmd_resp_wait);
4349
4350
4351 timer_setup(&bus->timer, brcmf_sdio_watchdog, 0);
4352
4353 init_completion(&bus->watchdog_wait);
4354 bus->watchdog_tsk = kthread_run(brcmf_sdio_watchdog_thread,
4355 bus, "brcmf_wdog/%s",
4356 dev_name(&sdiodev->func1->dev));
4357 if (IS_ERR(bus->watchdog_tsk)) {
4358 pr_warn("brcmf_watchdog thread failed to start\n");
4359 bus->watchdog_tsk = NULL;
4360 }
4361
4362 bus->dpc_triggered = false;
4363 bus->dpc_running = false;
4364
4365
4366 bus->tx_hdrlen = SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN;
4367
4368
4369 bus->blocksize = bus->sdiodev->func2->cur_blksize;
4370 bus->roundup = min(max_roundup, bus->blocksize);
4371
4372 sdio_claim_host(bus->sdiodev->func1);
4373
4374
4375 sdio_disable_func(bus->sdiodev->func2);
4376
4377 bus->rxflow = false;
4378
4379
4380 brcmf_sdiod_writeb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, 0, NULL);
4381
4382 sdio_release_host(bus->sdiodev->func1);
4383
4384
4385 bus->clkstate = CLK_SDONLY;
4386 bus->idletime = BRCMF_IDLE_INTERVAL;
4387 bus->idleclock = BRCMF_IDLE_ACTIVE;
4388
4389
4390 bus->sr_enabled = false;
4391
4392 brcmf_dbg(INFO, "completed!!\n");
4393
4394 fwreq = brcmf_sdio_prepare_fw_request(bus);
4395 if (!fwreq) {
4396 ret = -ENOMEM;
4397 goto fail;
4398 }
4399
4400 ret = brcmf_fw_get_firmwares(sdiodev->dev, fwreq,
4401 brcmf_sdio_firmware_callback);
4402 if (ret != 0) {
4403 brcmf_err("async firmware request failed: %d\n", ret);
4404 kfree(fwreq);
4405 goto fail;
4406 }
4407
4408 return bus;
4409
4410 fail:
4411 brcmf_sdio_remove(bus);
4412 return NULL;
4413 }
4414
4415
4416 void brcmf_sdio_remove(struct brcmf_sdio *bus)
4417 {
4418 brcmf_dbg(TRACE, "Enter\n");
4419
4420 if (bus) {
4421
4422 if (bus->watchdog_tsk) {
4423 send_sig(SIGTERM, bus->watchdog_tsk, 1);
4424 kthread_stop(bus->watchdog_tsk);
4425 bus->watchdog_tsk = NULL;
4426 }
4427
4428
4429 brcmf_sdiod_intr_unregister(bus->sdiodev);
4430
4431 brcmf_detach(bus->sdiodev->dev);
4432
4433 cancel_work_sync(&bus->datawork);
4434 if (bus->brcmf_wq)
4435 destroy_workqueue(bus->brcmf_wq);
4436
4437 if (bus->ci) {
4438 if (bus->sdiodev->state != BRCMF_SDIOD_NOMEDIUM) {
4439 sdio_claim_host(bus->sdiodev->func1);
4440 brcmf_sdio_wd_timer(bus, false);
4441 brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
4442
4443
4444
4445
4446 msleep(20);
4447 brcmf_chip_set_passive(bus->ci);
4448 brcmf_sdio_clkctl(bus, CLK_NONE, false);
4449 sdio_release_host(bus->sdiodev->func1);
4450 }
4451 brcmf_chip_detach(bus->ci);
4452 }
4453 if (bus->sdiodev->settings)
4454 brcmf_release_module_param(bus->sdiodev->settings);
4455
4456 kfree(bus->rxbuf);
4457 kfree(bus->hdrbuf);
4458 kfree(bus);
4459 }
4460
4461 brcmf_dbg(TRACE, "Disconnected\n");
4462 }
4463
4464 void brcmf_sdio_wd_timer(struct brcmf_sdio *bus, bool active)
4465 {
4466
4467 if (!active && bus->wd_active) {
4468 del_timer_sync(&bus->timer);
4469 bus->wd_active = false;
4470 return;
4471 }
4472
4473
4474 if (bus->sdiodev->state != BRCMF_SDIOD_DATA)
4475 return;
4476
4477 if (active) {
4478 if (!bus->wd_active) {
4479
4480
4481
4482 bus->timer.expires = jiffies + BRCMF_WD_POLL;
4483 add_timer(&bus->timer);
4484 bus->wd_active = true;
4485 } else {
4486
4487 mod_timer(&bus->timer, jiffies + BRCMF_WD_POLL);
4488 }
4489 }
4490 }
4491
4492 int brcmf_sdio_sleep(struct brcmf_sdio *bus, bool sleep)
4493 {
4494 int ret;
4495
4496 sdio_claim_host(bus->sdiodev->func1);
4497 ret = brcmf_sdio_bus_sleep(bus, sleep, false);
4498 sdio_release_host(bus->sdiodev->func1);
4499
4500 return ret;
4501 }
4502