2 * Copyright (c) 2010 Broadcom Corporation
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 #include <linux/types.h>
17 #include <linux/netdevice.h>
18 #include <linux/mmc/sdio.h>
19 #include <linux/mmc/core.h>
20 #include <linux/mmc/sdio_func.h>
21 #include <linux/mmc/sdio_ids.h>
22 #include <linux/mmc/card.h>
23 #include <linux/suspend.h>
24 #include <linux/errno.h>
25 #include <linux/sched.h> /* request_irq() */
26 #include <linux/module.h>
27 #include <net/cfg80211.h>
30 #include <brcm_hw_ids.h>
31 #include <brcmu_utils.h>
32 #include <brcmu_wifi.h>
33 #include "sdio_host.h"
36 #include "wl_cfg80211.h"
38 #define SDIO_VENDOR_ID_BROADCOM 0x02d0
40 #define DMA_ALIGN_MASK 0x03
42 #define SDIO_DEVICE_ID_BROADCOM_4329 0x4329
44 #define SDIO_FUNC1_BLOCKSIZE 64
45 #define SDIO_FUNC2_BLOCKSIZE 512
47 /* devices we support, null terminated */
48 static const struct sdio_device_id brcmf_sdmmc_ids
[] = {
49 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM
, SDIO_DEVICE_ID_BROADCOM_4329
)},
50 { /* end: all zeroes */ },
52 MODULE_DEVICE_TABLE(sdio
, brcmf_sdmmc_ids
);
55 brcmf_pm_resume_error(struct brcmf_sdio_dev
*sdiodev
)
58 #ifdef CONFIG_PM_SLEEP
59 is_err
= atomic_read(&sdiodev
->suspend
);
65 brcmf_pm_resume_wait(struct brcmf_sdio_dev
*sdiodev
, wait_queue_head_t
*wq
)
67 #ifdef CONFIG_PM_SLEEP
69 while (atomic_read(&sdiodev
->suspend
) && retry
++ != 30)
70 wait_event_timeout(*wq
, false, HZ
/100);
74 static inline int brcmf_sdioh_f0_write_byte(struct brcmf_sdio_dev
*sdiodev
,
75 uint regaddr
, u8
*byte
)
77 struct sdio_func
*sdfunc
= sdiodev
->func
[0];
81 * Can only directly write to some F0 registers.
82 * Handle F2 enable/disable and Abort command
85 if (regaddr
== SDIO_CCCR_IOEx
) {
86 sdfunc
= sdiodev
->func
[2];
88 sdio_claim_host(sdfunc
);
89 if (*byte
& SDIO_FUNC_ENABLE_2
) {
90 /* Enable Function 2 */
91 err_ret
= sdio_enable_func(sdfunc
);
94 "enable F2 failed:%d\n",
97 /* Disable Function 2 */
98 err_ret
= sdio_disable_func(sdfunc
);
101 "Disable F2 failed:%d\n",
104 sdio_release_host(sdfunc
);
106 } else if (regaddr
== SDIO_CCCR_ABORT
) {
107 sdio_claim_host(sdfunc
);
108 sdio_writeb(sdfunc
, *byte
, regaddr
, &err_ret
);
109 sdio_release_host(sdfunc
);
110 } else if (regaddr
< 0xF0) {
111 brcmf_dbg(ERROR
, "F0 Wr:0x%02x: write disallowed\n", regaddr
);
114 sdio_claim_host(sdfunc
);
115 sdio_f0_writeb(sdfunc
, *byte
, regaddr
, &err_ret
);
116 sdio_release_host(sdfunc
);
122 int brcmf_sdioh_request_byte(struct brcmf_sdio_dev
*sdiodev
, uint rw
, uint func
,
123 uint regaddr
, u8
*byte
)
127 brcmf_dbg(INFO
, "rw=%d, func=%d, addr=0x%05x\n", rw
, func
, regaddr
);
129 brcmf_pm_resume_wait(sdiodev
, &sdiodev
->request_byte_wait
);
130 if (brcmf_pm_resume_error(sdiodev
))
133 if (rw
&& func
== 0) {
134 /* handle F0 separately */
135 err_ret
= brcmf_sdioh_f0_write_byte(sdiodev
, regaddr
, byte
);
137 sdio_claim_host(sdiodev
->func
[func
]);
138 if (rw
) /* CMD52 Write */
139 sdio_writeb(sdiodev
->func
[func
], *byte
, regaddr
,
141 else if (func
== 0) {
142 *byte
= sdio_f0_readb(sdiodev
->func
[func
], regaddr
,
145 *byte
= sdio_readb(sdiodev
->func
[func
], regaddr
,
148 sdio_release_host(sdiodev
->func
[func
]);
152 brcmf_dbg(ERROR
, "Failed to %s byte F%d:@0x%05x=%02x, Err: %d\n",
153 rw
? "write" : "read", func
, regaddr
, *byte
, err_ret
);
158 int brcmf_sdioh_request_word(struct brcmf_sdio_dev
*sdiodev
,
159 uint rw
, uint func
, uint addr
, u32
*word
,
165 brcmf_dbg(ERROR
, "Only CMD52 allowed to F0\n");
169 brcmf_dbg(INFO
, "rw=%d, func=%d, addr=0x%05x, nbytes=%d\n",
170 rw
, func
, addr
, nbytes
);
172 brcmf_pm_resume_wait(sdiodev
, &sdiodev
->request_word_wait
);
173 if (brcmf_pm_resume_error(sdiodev
))
175 /* Claim host controller */
176 sdio_claim_host(sdiodev
->func
[func
]);
178 if (rw
) { /* CMD52 Write */
180 sdio_writel(sdiodev
->func
[func
], *word
, addr
,
182 else if (nbytes
== 2)
183 sdio_writew(sdiodev
->func
[func
], (*word
& 0xFFFF),
186 brcmf_dbg(ERROR
, "Invalid nbytes: %d\n", nbytes
);
187 } else { /* CMD52 Read */
189 *word
= sdio_readl(sdiodev
->func
[func
], addr
, &err_ret
);
190 else if (nbytes
== 2)
191 *word
= sdio_readw(sdiodev
->func
[func
], addr
,
194 brcmf_dbg(ERROR
, "Invalid nbytes: %d\n", nbytes
);
197 /* Release host controller */
198 sdio_release_host(sdiodev
->func
[func
]);
201 brcmf_dbg(ERROR
, "Failed to %s word, Err: 0x%08x\n",
202 rw
? "write" : "read", err_ret
);
207 /* precondition: host controller is claimed */
209 brcmf_sdioh_request_data(struct brcmf_sdio_dev
*sdiodev
, uint write
, bool fifo
,
210 uint func
, uint addr
, struct sk_buff
*pkt
, uint pktlen
)
214 if ((write
) && (!fifo
)) {
215 err_ret
= sdio_memcpy_toio(sdiodev
->func
[func
], addr
,
216 ((u8
*) (pkt
->data
)), pktlen
);
218 err_ret
= sdio_memcpy_toio(sdiodev
->func
[func
], addr
,
219 ((u8
*) (pkt
->data
)), pktlen
);
221 err_ret
= sdio_readsb(sdiodev
->func
[func
],
222 ((u8
*) (pkt
->data
)), addr
, pktlen
);
224 err_ret
= sdio_memcpy_fromio(sdiodev
->func
[func
],
225 ((u8
*) (pkt
->data
)),
233 * This function takes a queue of packets. The packets on the queue
234 * are assumed to be properly aligned by the caller.
237 brcmf_sdioh_request_chain(struct brcmf_sdio_dev
*sdiodev
, uint fix_inc
,
238 uint write
, uint func
, uint addr
,
239 struct sk_buff_head
*pktq
)
241 bool fifo
= (fix_inc
== SDIOH_DATA_FIX
);
247 brcmf_dbg(TRACE
, "Enter\n");
249 brcmf_pm_resume_wait(sdiodev
, &sdiodev
->request_chain_wait
);
250 if (brcmf_pm_resume_error(sdiodev
))
253 /* Claim host controller */
254 sdio_claim_host(sdiodev
->func
[func
]);
256 skb_queue_walk(pktq
, pkt
) {
257 uint pkt_len
= pkt
->len
;
259 pkt_len
&= 0xFFFFFFFC;
261 err_ret
= brcmf_sdioh_request_data(sdiodev
, write
, fifo
, func
,
264 brcmf_dbg(ERROR
, "%s FAILED %p[%d], addr=0x%05x, pkt_len=%d, ERR=0x%08x\n",
265 write
? "TX" : "RX", pkt
, SGCount
, addr
,
268 brcmf_dbg(TRACE
, "%s xfr'd %p[%d], addr=0x%05x, len=%d\n",
269 write
? "TX" : "RX", pkt
, SGCount
, addr
,
278 /* Release host controller */
279 sdio_release_host(sdiodev
->func
[func
]);
281 brcmf_dbg(TRACE
, "Exit\n");
286 * This function takes a single DMA-able packet.
288 int brcmf_sdioh_request_buffer(struct brcmf_sdio_dev
*sdiodev
,
289 uint fix_inc
, uint write
, uint func
, uint addr
,
293 uint pkt_len
= pkt
->len
;
294 bool fifo
= (fix_inc
== SDIOH_DATA_FIX
);
296 brcmf_dbg(TRACE
, "Enter\n");
301 brcmf_pm_resume_wait(sdiodev
, &sdiodev
->request_buffer_wait
);
302 if (brcmf_pm_resume_error(sdiodev
))
305 /* Claim host controller */
306 sdio_claim_host(sdiodev
->func
[func
]);
311 status
= brcmf_sdioh_request_data(sdiodev
, write
, fifo
, func
,
314 brcmf_dbg(ERROR
, "%s FAILED %p, addr=0x%05x, pkt_len=%d, ERR=0x%08x\n",
315 write
? "TX" : "RX", pkt
, addr
, pkt_len
, status
);
317 brcmf_dbg(TRACE
, "%s xfr'd %p, addr=0x%05x, len=%d\n",
318 write
? "TX" : "RX", pkt
, addr
, pkt_len
);
321 /* Release host controller */
322 sdio_release_host(sdiodev
->func
[func
]);
327 /* Read client card reg */
329 brcmf_sdioh_card_regread(struct brcmf_sdio_dev
*sdiodev
, int func
, u32 regaddr
,
330 int regsize
, u32
*data
)
333 if ((func
== 0) || (regsize
== 1)) {
336 brcmf_sdioh_request_byte(sdiodev
, SDIOH_READ
, func
, regaddr
,
340 brcmf_dbg(DATA
, "byte read data=0x%02x\n", *data
);
342 brcmf_sdioh_request_word(sdiodev
, SDIOH_READ
, func
, regaddr
,
347 brcmf_dbg(DATA
, "word read data=0x%08x\n", *data
);
353 static int brcmf_sdioh_get_cisaddr(struct brcmf_sdio_dev
*sdiodev
, u32 regaddr
)
355 /* read 24 bits and return valid 17 bit addr */
357 u32 scratch
, regdata
;
359 u8
*ptr
= (u8
*)&scratch_le
;
361 for (i
= 0; i
< 3; i
++) {
362 if ((brcmf_sdioh_card_regread(sdiodev
, 0, regaddr
, 1,
363 ®data
)) != SUCCESS
)
364 brcmf_dbg(ERROR
, "Can't read!\n");
366 *ptr
++ = (u8
) regdata
;
370 /* Only the lower 17-bits are valid */
371 scratch
= le32_to_cpu(scratch_le
);
372 scratch
&= 0x0001FFFF;
376 static int brcmf_sdioh_enablefuncs(struct brcmf_sdio_dev
*sdiodev
)
382 brcmf_dbg(TRACE
, "\n");
384 /* Get the Card's common CIS address */
385 sdiodev
->func_cis_ptr
[0] = brcmf_sdioh_get_cisaddr(sdiodev
,
387 brcmf_dbg(INFO
, "Card's Common CIS Ptr = 0x%x\n",
388 sdiodev
->func_cis_ptr
[0]);
390 /* Get the Card's function CIS (for each function) */
391 for (fbraddr
= SDIO_FBR_BASE(1), func
= 1;
392 func
<= sdiodev
->num_funcs
; func
++, fbraddr
+= SDIOD_FBR_SIZE
) {
393 sdiodev
->func_cis_ptr
[func
] =
394 brcmf_sdioh_get_cisaddr(sdiodev
, SDIO_FBR_CIS
+ fbraddr
);
395 brcmf_dbg(INFO
, "Function %d CIS Ptr = 0x%x\n",
396 func
, sdiodev
->func_cis_ptr
[func
]);
399 /* Enable Function 1 */
400 sdio_claim_host(sdiodev
->func
[1]);
401 err_ret
= sdio_enable_func(sdiodev
->func
[1]);
402 sdio_release_host(sdiodev
->func
[1]);
404 brcmf_dbg(ERROR
, "Failed to enable F1 Err: 0x%08x\n", err_ret
);
410 * Public entry points & extern's
412 int brcmf_sdioh_attach(struct brcmf_sdio_dev
*sdiodev
)
416 brcmf_dbg(TRACE
, "\n");
418 sdiodev
->num_funcs
= 2;
420 sdio_claim_host(sdiodev
->func
[1]);
421 err_ret
= sdio_set_block_size(sdiodev
->func
[1], SDIO_FUNC1_BLOCKSIZE
);
422 sdio_release_host(sdiodev
->func
[1]);
424 brcmf_dbg(ERROR
, "Failed to set F1 blocksize\n");
428 sdio_claim_host(sdiodev
->func
[2]);
429 err_ret
= sdio_set_block_size(sdiodev
->func
[2], SDIO_FUNC2_BLOCKSIZE
);
430 sdio_release_host(sdiodev
->func
[2]);
432 brcmf_dbg(ERROR
, "Failed to set F2 blocksize\n");
436 brcmf_sdioh_enablefuncs(sdiodev
);
439 brcmf_dbg(TRACE
, "Done\n");
443 void brcmf_sdioh_detach(struct brcmf_sdio_dev
*sdiodev
)
445 brcmf_dbg(TRACE
, "\n");
447 /* Disable Function 2 */
448 sdio_claim_host(sdiodev
->func
[2]);
449 sdio_disable_func(sdiodev
->func
[2]);
450 sdio_release_host(sdiodev
->func
[2]);
452 /* Disable Function 1 */
453 sdio_claim_host(sdiodev
->func
[1]);
454 sdio_disable_func(sdiodev
->func
[1]);
455 sdio_release_host(sdiodev
->func
[1]);
459 static int brcmf_ops_sdio_probe(struct sdio_func
*func
,
460 const struct sdio_device_id
*id
)
463 struct brcmf_sdio_dev
*sdiodev
;
464 struct brcmf_bus
*bus_if
;
465 brcmf_dbg(TRACE
, "Enter\n");
466 brcmf_dbg(TRACE
, "func->class=%x\n", func
->class);
467 brcmf_dbg(TRACE
, "sdio_vendor: 0x%04x\n", func
->vendor
);
468 brcmf_dbg(TRACE
, "sdio_device: 0x%04x\n", func
->device
);
469 brcmf_dbg(TRACE
, "Function#: 0x%04x\n", func
->num
);
471 if (func
->num
== 1) {
472 if (dev_get_drvdata(&func
->card
->dev
)) {
473 brcmf_dbg(ERROR
, "card private drvdata occupied\n");
476 bus_if
= kzalloc(sizeof(struct brcmf_bus
), GFP_KERNEL
);
479 sdiodev
= kzalloc(sizeof(struct brcmf_sdio_dev
), GFP_KERNEL
);
484 sdiodev
->func
[0] = func
->card
->sdio_func
[0];
485 sdiodev
->func
[1] = func
;
486 sdiodev
->bus_if
= bus_if
;
487 bus_if
->bus_priv
= sdiodev
;
488 bus_if
->type
= SDIO_BUS
;
489 dev_set_drvdata(&func
->card
->dev
, sdiodev
);
491 atomic_set(&sdiodev
->suspend
, false);
492 init_waitqueue_head(&sdiodev
->request_byte_wait
);
493 init_waitqueue_head(&sdiodev
->request_word_wait
);
494 init_waitqueue_head(&sdiodev
->request_chain_wait
);
495 init_waitqueue_head(&sdiodev
->request_buffer_wait
);
498 if (func
->num
== 2) {
499 sdiodev
= dev_get_drvdata(&func
->card
->dev
);
500 if ((!sdiodev
) || (sdiodev
->func
[1]->card
!= func
->card
))
502 sdiodev
->func
[2] = func
;
504 bus_if
= sdiodev
->bus_if
;
505 sdiodev
->dev
= &func
->dev
;
506 dev_set_drvdata(&func
->dev
, bus_if
);
508 brcmf_dbg(TRACE
, "F2 found, calling brcmf_sdio_probe...\n");
509 ret
= brcmf_sdio_probe(sdiodev
);
515 static void brcmf_ops_sdio_remove(struct sdio_func
*func
)
517 struct brcmf_bus
*bus_if
;
518 struct brcmf_sdio_dev
*sdiodev
;
519 brcmf_dbg(TRACE
, "Enter\n");
520 brcmf_dbg(INFO
, "func->class=%x\n", func
->class);
521 brcmf_dbg(INFO
, "sdio_vendor: 0x%04x\n", func
->vendor
);
522 brcmf_dbg(INFO
, "sdio_device: 0x%04x\n", func
->device
);
523 brcmf_dbg(INFO
, "Function#: 0x%04x\n", func
->num
);
525 if (func
->num
== 2) {
526 bus_if
= dev_get_drvdata(&func
->dev
);
527 sdiodev
= bus_if
->bus_priv
;
528 brcmf_dbg(TRACE
, "F2 found, calling brcmf_sdio_remove...\n");
529 brcmf_sdio_remove(sdiodev
);
530 dev_set_drvdata(&func
->card
->dev
, NULL
);
531 dev_set_drvdata(&func
->dev
, NULL
);
537 #ifdef CONFIG_PM_SLEEP
538 static int brcmf_sdio_suspend(struct device
*dev
)
540 mmc_pm_flag_t sdio_flags
;
541 struct sdio_func
*func
= dev_to_sdio_func(dev
);
542 struct brcmf_sdio_dev
*sdiodev
= dev_get_drvdata(&func
->card
->dev
);
545 brcmf_dbg(TRACE
, "\n");
547 atomic_set(&sdiodev
->suspend
, true);
549 sdio_flags
= sdio_get_host_pm_caps(sdiodev
->func
[1]);
550 if (!(sdio_flags
& MMC_PM_KEEP_POWER
)) {
551 brcmf_dbg(ERROR
, "Host can't keep power while suspended\n");
555 ret
= sdio_set_host_pm_flags(sdiodev
->func
[1], MMC_PM_KEEP_POWER
);
557 brcmf_dbg(ERROR
, "Failed to set pm_flags\n");
561 brcmf_sdio_wdtmr_enable(sdiodev
, false);
566 static int brcmf_sdio_resume(struct device
*dev
)
568 struct sdio_func
*func
= dev_to_sdio_func(dev
);
569 struct brcmf_sdio_dev
*sdiodev
= dev_get_drvdata(&func
->card
->dev
);
571 brcmf_sdio_wdtmr_enable(sdiodev
, true);
572 atomic_set(&sdiodev
->suspend
, false);
576 static const struct dev_pm_ops brcmf_sdio_pm_ops
= {
577 .suspend
= brcmf_sdio_suspend
,
578 .resume
= brcmf_sdio_resume
,
580 #endif /* CONFIG_PM_SLEEP */
582 static struct sdio_driver brcmf_sdmmc_driver
= {
583 .probe
= brcmf_ops_sdio_probe
,
584 .remove
= brcmf_ops_sdio_remove
,
586 .id_table
= brcmf_sdmmc_ids
,
587 #ifdef CONFIG_PM_SLEEP
589 .pm
= &brcmf_sdio_pm_ops
,
591 #endif /* CONFIG_PM_SLEEP */
594 static void __exit
brcmf_sdio_exit(void)
596 brcmf_dbg(TRACE
, "Enter\n");
598 sdio_unregister_driver(&brcmf_sdmmc_driver
);
601 static int __init
brcmf_sdio_init(void)
605 brcmf_dbg(TRACE
, "Enter\n");
607 ret
= sdio_register_driver(&brcmf_sdmmc_driver
);
610 brcmf_dbg(ERROR
, "sdio_register_driver failed: %d\n", ret
);
615 module_init(brcmf_sdio_init
);
616 module_exit(brcmf_sdio_exit
);