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.
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 #include <linux/types.h>
20 #include <linux/netdevice.h>
21 #include <linux/mmc/sdio.h>
22 #include <linux/mmc/core.h>
23 #include <linux/mmc/sdio_func.h>
24 #include <linux/mmc/sdio_ids.h>
25 #include <linux/mmc/card.h>
26 #include <linux/suspend.h>
27 #include <linux/errno.h>
28 #include <linux/sched.h> /* request_irq() */
29 #include <linux/module.h>
30 #include <linux/platform_device.h>
31 #include <net/cfg80211.h>
34 #include <brcm_hw_ids.h>
35 #include <brcmu_utils.h>
36 #include <brcmu_wifi.h>
37 #include "sdio_host.h"
41 #define SDIO_VENDOR_ID_BROADCOM 0x02d0
43 #define DMA_ALIGN_MASK 0x03
45 #define SDIO_DEVICE_ID_BROADCOM_43241 0x4324
46 #define SDIO_DEVICE_ID_BROADCOM_4329 0x4329
47 #define SDIO_DEVICE_ID_BROADCOM_4330 0x4330
48 #define SDIO_DEVICE_ID_BROADCOM_4334 0x4334
50 #define SDIO_FUNC1_BLOCKSIZE 64
51 #define SDIO_FUNC2_BLOCKSIZE 512
53 /* devices we support, null terminated */
54 static const struct sdio_device_id brcmf_sdmmc_ids[] = {
55 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_43241)},
56 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4329)},
57 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4330)},
58 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4334)},
59 { /* end: all zeroes */ },
61 MODULE_DEVICE_TABLE(sdio, brcmf_sdmmc_ids);
63 #ifdef CONFIG_BRCMFMAC_SDIO_OOB
64 static struct list_head oobirq_lh;
65 struct brcmf_sdio_oobirq {
68 struct list_head list;
70 #endif /* CONFIG_BRCMFMAC_SDIO_OOB */
73 brcmf_pm_resume_error(struct brcmf_sdio_dev *sdiodev)
76 #ifdef CONFIG_PM_SLEEP
77 is_err = atomic_read(&sdiodev->suspend);
83 brcmf_pm_resume_wait(struct brcmf_sdio_dev *sdiodev, wait_queue_head_t *wq)
85 #ifdef CONFIG_PM_SLEEP
87 while (atomic_read(&sdiodev->suspend) && retry++ != 30)
88 wait_event_timeout(*wq, false, HZ/100);
92 static inline int brcmf_sdioh_f0_write_byte(struct brcmf_sdio_dev *sdiodev,
93 uint regaddr, u8 *byte)
95 struct sdio_func *sdfunc = sdiodev->func[0];
99 * Can only directly write to some F0 registers.
100 * Handle F2 enable/disable and Abort command
103 if (regaddr == SDIO_CCCR_IOEx) {
104 sdfunc = sdiodev->func[2];
106 sdio_claim_host(sdfunc);
107 if (*byte & SDIO_FUNC_ENABLE_2) {
108 /* Enable Function 2 */
109 err_ret = sdio_enable_func(sdfunc);
112 "enable F2 failed:%d\n",
115 /* Disable Function 2 */
116 err_ret = sdio_disable_func(sdfunc);
119 "Disable F2 failed:%d\n",
122 sdio_release_host(sdfunc);
124 } else if ((regaddr == SDIO_CCCR_ABORT) ||
125 (regaddr == SDIO_CCCR_IENx)) {
126 sdfunc = kmemdup(sdiodev->func[0], sizeof(struct sdio_func),
131 sdio_claim_host(sdfunc);
132 sdio_writeb(sdfunc, *byte, regaddr, &err_ret);
133 sdio_release_host(sdfunc);
135 } else if (regaddr < 0xF0) {
136 brcmf_dbg(ERROR, "F0 Wr:0x%02x: write disallowed\n", regaddr);
139 sdio_claim_host(sdfunc);
140 sdio_f0_writeb(sdfunc, *byte, regaddr, &err_ret);
141 sdio_release_host(sdfunc);
147 int brcmf_sdioh_request_byte(struct brcmf_sdio_dev *sdiodev, uint rw, uint func,
148 uint regaddr, u8 *byte)
152 brcmf_dbg(INFO, "rw=%d, func=%d, addr=0x%05x\n", rw, func, regaddr);
154 brcmf_pm_resume_wait(sdiodev, &sdiodev->request_byte_wait);
155 if (brcmf_pm_resume_error(sdiodev))
158 if (rw && func == 0) {
159 /* handle F0 separately */
160 err_ret = brcmf_sdioh_f0_write_byte(sdiodev, regaddr, byte);
162 sdio_claim_host(sdiodev->func[func]);
163 if (rw) /* CMD52 Write */
164 sdio_writeb(sdiodev->func[func], *byte, regaddr,
166 else if (func == 0) {
167 *byte = sdio_f0_readb(sdiodev->func[func], regaddr,
170 *byte = sdio_readb(sdiodev->func[func], regaddr,
173 sdio_release_host(sdiodev->func[func]);
177 brcmf_dbg(ERROR, "Failed to %s byte F%d:@0x%05x=%02x, Err: %d\n",
178 rw ? "write" : "read", func, regaddr, *byte, err_ret);
183 int brcmf_sdioh_request_word(struct brcmf_sdio_dev *sdiodev,
184 uint rw, uint func, uint addr, u32 *word,
190 brcmf_dbg(ERROR, "Only CMD52 allowed to F0\n");
194 brcmf_dbg(INFO, "rw=%d, func=%d, addr=0x%05x, nbytes=%d\n",
195 rw, func, addr, nbytes);
197 brcmf_pm_resume_wait(sdiodev, &sdiodev->request_word_wait);
198 if (brcmf_pm_resume_error(sdiodev))
200 /* Claim host controller */
201 sdio_claim_host(sdiodev->func[func]);
203 if (rw) { /* CMD52 Write */
205 sdio_writel(sdiodev->func[func], *word, addr,
207 else if (nbytes == 2)
208 sdio_writew(sdiodev->func[func], (*word & 0xFFFF),
211 brcmf_dbg(ERROR, "Invalid nbytes: %d\n", nbytes);
212 } else { /* CMD52 Read */
214 *word = sdio_readl(sdiodev->func[func], addr, &err_ret);
215 else if (nbytes == 2)
216 *word = sdio_readw(sdiodev->func[func], addr,
219 brcmf_dbg(ERROR, "Invalid nbytes: %d\n", nbytes);
222 /* Release host controller */
223 sdio_release_host(sdiodev->func[func]);
226 brcmf_dbg(ERROR, "Failed to %s word, Err: 0x%08x\n",
227 rw ? "write" : "read", err_ret);
232 /* precondition: host controller is claimed */
234 brcmf_sdioh_request_data(struct brcmf_sdio_dev *sdiodev, uint write, bool fifo,
235 uint func, uint addr, struct sk_buff *pkt, uint pktlen)
239 if ((write) && (!fifo)) {
240 err_ret = sdio_memcpy_toio(sdiodev->func[func], addr,
241 ((u8 *) (pkt->data)), pktlen);
243 err_ret = sdio_memcpy_toio(sdiodev->func[func], addr,
244 ((u8 *) (pkt->data)), pktlen);
246 err_ret = sdio_readsb(sdiodev->func[func],
247 ((u8 *) (pkt->data)), addr, pktlen);
249 err_ret = sdio_memcpy_fromio(sdiodev->func[func],
250 ((u8 *) (pkt->data)),
258 * This function takes a queue of packets. The packets on the queue
259 * are assumed to be properly aligned by the caller.
262 brcmf_sdioh_request_chain(struct brcmf_sdio_dev *sdiodev, uint fix_inc,
263 uint write, uint func, uint addr,
264 struct sk_buff_head *pktq)
266 bool fifo = (fix_inc == SDIOH_DATA_FIX);
272 brcmf_dbg(TRACE, "Enter\n");
274 brcmf_pm_resume_wait(sdiodev, &sdiodev->request_chain_wait);
275 if (brcmf_pm_resume_error(sdiodev))
278 /* Claim host controller */
279 sdio_claim_host(sdiodev->func[func]);
281 skb_queue_walk(pktq, pkt) {
282 uint pkt_len = pkt->len;
284 pkt_len &= 0xFFFFFFFC;
286 err_ret = brcmf_sdioh_request_data(sdiodev, write, fifo, func,
289 brcmf_dbg(ERROR, "%s FAILED %p[%d], addr=0x%05x, pkt_len=%d, ERR=0x%08x\n",
290 write ? "TX" : "RX", pkt, SGCount, addr,
293 brcmf_dbg(TRACE, "%s xfr'd %p[%d], addr=0x%05x, len=%d\n",
294 write ? "TX" : "RX", pkt, SGCount, addr,
303 /* Release host controller */
304 sdio_release_host(sdiodev->func[func]);
306 brcmf_dbg(TRACE, "Exit\n");
311 * This function takes a single DMA-able packet.
313 int brcmf_sdioh_request_buffer(struct brcmf_sdio_dev *sdiodev,
314 uint fix_inc, uint write, uint func, uint addr,
319 bool fifo = (fix_inc == SDIOH_DATA_FIX);
321 brcmf_dbg(TRACE, "Enter\n");
327 brcmf_pm_resume_wait(sdiodev, &sdiodev->request_buffer_wait);
328 if (brcmf_pm_resume_error(sdiodev))
331 /* Claim host controller */
332 sdio_claim_host(sdiodev->func[func]);
337 status = brcmf_sdioh_request_data(sdiodev, write, fifo, func,
340 brcmf_dbg(ERROR, "%s FAILED %p, addr=0x%05x, pkt_len=%d, ERR=0x%08x\n",
341 write ? "TX" : "RX", pkt, addr, pkt_len, status);
343 brcmf_dbg(TRACE, "%s xfr'd %p, addr=0x%05x, len=%d\n",
344 write ? "TX" : "RX", pkt, addr, pkt_len);
347 /* Release host controller */
348 sdio_release_host(sdiodev->func[func]);
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 regdata = brcmf_sdio_regrl(sdiodev, regaddr, &ret);
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 #ifdef CONFIG_BRCMFMAC_SDIO_OOB
460 static int brcmf_sdio_getintrcfg(struct brcmf_sdio_dev *sdiodev)
462 struct brcmf_sdio_oobirq *oobirq_entry;
464 if (list_empty(&oobirq_lh)) {
465 brcmf_dbg(ERROR, "no valid oob irq resource\n");
469 oobirq_entry = list_first_entry(&oobirq_lh, struct brcmf_sdio_oobirq,
472 sdiodev->irq = oobirq_entry->irq;
473 sdiodev->irq_flags = oobirq_entry->flags;
474 list_del(&oobirq_entry->list);
480 static inline int brcmf_sdio_getintrcfg(struct brcmf_sdio_dev *sdiodev)
484 #endif /* CONFIG_BRCMFMAC_SDIO_OOB */
486 static int brcmf_ops_sdio_probe(struct sdio_func *func,
487 const struct sdio_device_id *id)
490 struct brcmf_sdio_dev *sdiodev;
491 struct brcmf_bus *bus_if;
493 brcmf_dbg(TRACE, "Enter\n");
494 brcmf_dbg(TRACE, "func->class=%x\n", func->class);
495 brcmf_dbg(TRACE, "sdio_vendor: 0x%04x\n", func->vendor);
496 brcmf_dbg(TRACE, "sdio_device: 0x%04x\n", func->device);
497 brcmf_dbg(TRACE, "Function#: 0x%04x\n", func->num);
499 if (func->num == 1) {
500 if (dev_get_drvdata(&func->card->dev)) {
501 brcmf_dbg(ERROR, "card private drvdata occupied\n");
504 bus_if = kzalloc(sizeof(struct brcmf_bus), GFP_KERNEL);
507 sdiodev = kzalloc(sizeof(struct brcmf_sdio_dev), GFP_KERNEL);
512 sdiodev->func[0] = func;
513 sdiodev->func[1] = func;
514 sdiodev->bus_if = bus_if;
515 bus_if->bus_priv.sdio = sdiodev;
516 bus_if->type = SDIO_BUS;
517 bus_if->align = BRCMF_SDALIGN;
518 dev_set_drvdata(&func->card->dev, sdiodev);
520 atomic_set(&sdiodev->suspend, false);
521 init_waitqueue_head(&sdiodev->request_byte_wait);
522 init_waitqueue_head(&sdiodev->request_word_wait);
523 init_waitqueue_head(&sdiodev->request_chain_wait);
524 init_waitqueue_head(&sdiodev->request_buffer_wait);
527 if (func->num == 2) {
528 sdiodev = dev_get_drvdata(&func->card->dev);
529 if ((!sdiodev) || (sdiodev->func[1]->card != func->card))
532 ret = brcmf_sdio_getintrcfg(sdiodev);
535 sdiodev->func[2] = func;
537 bus_if = sdiodev->bus_if;
538 sdiodev->dev = &func->dev;
539 dev_set_drvdata(&func->dev, bus_if);
541 brcmf_dbg(TRACE, "F2 found, calling brcmf_sdio_probe...\n");
542 ret = brcmf_sdio_probe(sdiodev);
548 static void brcmf_ops_sdio_remove(struct sdio_func *func)
550 struct brcmf_bus *bus_if;
551 struct brcmf_sdio_dev *sdiodev;
552 brcmf_dbg(TRACE, "Enter\n");
553 brcmf_dbg(INFO, "func->class=%x\n", func->class);
554 brcmf_dbg(INFO, "sdio_vendor: 0x%04x\n", func->vendor);
555 brcmf_dbg(INFO, "sdio_device: 0x%04x\n", func->device);
556 brcmf_dbg(INFO, "Function#: 0x%04x\n", func->num);
558 if (func->num == 2) {
559 bus_if = dev_get_drvdata(&func->dev);
560 sdiodev = bus_if->bus_priv.sdio;
561 brcmf_dbg(TRACE, "F2 found, calling brcmf_sdio_remove...\n");
562 brcmf_sdio_remove(sdiodev);
563 dev_set_drvdata(&func->card->dev, NULL);
564 dev_set_drvdata(&func->dev, NULL);
570 #ifdef CONFIG_PM_SLEEP
571 static int brcmf_sdio_suspend(struct device *dev)
573 mmc_pm_flag_t sdio_flags;
574 struct sdio_func *func = dev_to_sdio_func(dev);
575 struct brcmf_sdio_dev *sdiodev = dev_get_drvdata(&func->card->dev);
578 brcmf_dbg(TRACE, "\n");
580 atomic_set(&sdiodev->suspend, true);
582 sdio_flags = sdio_get_host_pm_caps(sdiodev->func[1]);
583 if (!(sdio_flags & MMC_PM_KEEP_POWER)) {
584 brcmf_dbg(ERROR, "Host can't keep power while suspended\n");
588 ret = sdio_set_host_pm_flags(sdiodev->func[1], MMC_PM_KEEP_POWER);
590 brcmf_dbg(ERROR, "Failed to set pm_flags\n");
594 brcmf_sdio_wdtmr_enable(sdiodev, false);
599 static int brcmf_sdio_resume(struct device *dev)
601 struct sdio_func *func = dev_to_sdio_func(dev);
602 struct brcmf_sdio_dev *sdiodev = dev_get_drvdata(&func->card->dev);
604 brcmf_sdio_wdtmr_enable(sdiodev, true);
605 atomic_set(&sdiodev->suspend, false);
609 static const struct dev_pm_ops brcmf_sdio_pm_ops = {
610 .suspend = brcmf_sdio_suspend,
611 .resume = brcmf_sdio_resume,
613 #endif /* CONFIG_PM_SLEEP */
615 static struct sdio_driver brcmf_sdmmc_driver = {
616 .probe = brcmf_ops_sdio_probe,
617 .remove = brcmf_ops_sdio_remove,
619 .id_table = brcmf_sdmmc_ids,
620 #ifdef CONFIG_PM_SLEEP
622 .pm = &brcmf_sdio_pm_ops,
624 #endif /* CONFIG_PM_SLEEP */
627 #ifdef CONFIG_BRCMFMAC_SDIO_OOB
628 static int brcmf_sdio_pd_probe(struct platform_device *pdev)
630 struct resource *res;
631 struct brcmf_sdio_oobirq *oobirq_entry;
634 INIT_LIST_HEAD(&oobirq_lh);
637 res = platform_get_resource(pdev, IORESOURCE_IRQ, i);
641 oobirq_entry = kzalloc(sizeof(struct brcmf_sdio_oobirq),
645 oobirq_entry->irq = res->start;
646 oobirq_entry->flags = res->flags & IRQF_TRIGGER_MASK;
647 list_add_tail(&oobirq_entry->list, &oobirq_lh);
652 ret = sdio_register_driver(&brcmf_sdmmc_driver);
655 brcmf_dbg(ERROR, "sdio_register_driver failed: %d\n", ret);
660 static struct platform_driver brcmf_sdio_pd = {
661 .probe = brcmf_sdio_pd_probe,
663 .name = "brcmf_sdio_pd"
667 void brcmf_sdio_exit(void)
669 brcmf_dbg(TRACE, "Enter\n");
671 sdio_unregister_driver(&brcmf_sdmmc_driver);
673 platform_driver_unregister(&brcmf_sdio_pd);
676 void brcmf_sdio_init(void)
680 brcmf_dbg(TRACE, "Enter\n");
682 ret = platform_driver_register(&brcmf_sdio_pd);
685 brcmf_dbg(ERROR, "platform_driver_register failed: %d\n", ret);
688 void brcmf_sdio_exit(void)
690 brcmf_dbg(TRACE, "Enter\n");
692 sdio_unregister_driver(&brcmf_sdmmc_driver);
695 void brcmf_sdio_init(void)
699 brcmf_dbg(TRACE, "Enter\n");
701 ret = sdio_register_driver(&brcmf_sdmmc_driver);
704 brcmf_dbg(ERROR, "sdio_register_driver failed: %d\n", ret);
706 #endif /* CONFIG_BRCMFMAC_SDIO_OOB */