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"
37 #define SDIO_VENDOR_ID_BROADCOM 0x02d0
39 #define DMA_ALIGN_MASK 0x03
41 #define SDIO_DEVICE_ID_BROADCOM_4329 0x4329
42 #define SDIO_DEVICE_ID_BROADCOM_4330 0x4330
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 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM
, SDIO_DEVICE_ID_BROADCOM_4330
)},
51 { /* end: all zeroes */ },
53 MODULE_DEVICE_TABLE(sdio
, brcmf_sdmmc_ids
);
56 brcmf_pm_resume_error(struct brcmf_sdio_dev
*sdiodev
)
59 #ifdef CONFIG_PM_SLEEP
60 is_err
= atomic_read(&sdiodev
->suspend
);
66 brcmf_pm_resume_wait(struct brcmf_sdio_dev
*sdiodev
, wait_queue_head_t
*wq
)
68 #ifdef CONFIG_PM_SLEEP
70 while (atomic_read(&sdiodev
->suspend
) && retry
++ != 30)
71 wait_event_timeout(*wq
, false, HZ
/100);
75 static inline int brcmf_sdioh_f0_write_byte(struct brcmf_sdio_dev
*sdiodev
,
76 uint regaddr
, u8
*byte
)
78 struct sdio_func
*sdfunc
= sdiodev
->func
[0];
82 * Can only directly write to some F0 registers.
83 * Handle F2 enable/disable and Abort command
86 if (regaddr
== SDIO_CCCR_IOEx
) {
87 sdfunc
= sdiodev
->func
[2];
89 sdio_claim_host(sdfunc
);
90 if (*byte
& SDIO_FUNC_ENABLE_2
) {
91 /* Enable Function 2 */
92 err_ret
= sdio_enable_func(sdfunc
);
95 "enable F2 failed:%d\n",
98 /* Disable Function 2 */
99 err_ret
= sdio_disable_func(sdfunc
);
102 "Disable F2 failed:%d\n",
105 sdio_release_host(sdfunc
);
107 } else if (regaddr
== SDIO_CCCR_ABORT
) {
108 sdio_claim_host(sdfunc
);
109 sdio_writeb(sdfunc
, *byte
, regaddr
, &err_ret
);
110 sdio_release_host(sdfunc
);
111 } else if (regaddr
< 0xF0) {
112 brcmf_dbg(ERROR
, "F0 Wr:0x%02x: write disallowed\n", regaddr
);
115 sdio_claim_host(sdfunc
);
116 sdio_f0_writeb(sdfunc
, *byte
, regaddr
, &err_ret
);
117 sdio_release_host(sdfunc
);
123 int brcmf_sdioh_request_byte(struct brcmf_sdio_dev
*sdiodev
, uint rw
, uint func
,
124 uint regaddr
, u8
*byte
)
128 brcmf_dbg(INFO
, "rw=%d, func=%d, addr=0x%05x\n", rw
, func
, regaddr
);
130 brcmf_pm_resume_wait(sdiodev
, &sdiodev
->request_byte_wait
);
131 if (brcmf_pm_resume_error(sdiodev
))
134 if (rw
&& func
== 0) {
135 /* handle F0 separately */
136 err_ret
= brcmf_sdioh_f0_write_byte(sdiodev
, regaddr
, byte
);
138 sdio_claim_host(sdiodev
->func
[func
]);
139 if (rw
) /* CMD52 Write */
140 sdio_writeb(sdiodev
->func
[func
], *byte
, regaddr
,
142 else if (func
== 0) {
143 *byte
= sdio_f0_readb(sdiodev
->func
[func
], regaddr
,
146 *byte
= sdio_readb(sdiodev
->func
[func
], regaddr
,
149 sdio_release_host(sdiodev
->func
[func
]);
153 brcmf_dbg(ERROR
, "Failed to %s byte F%d:@0x%05x=%02x, Err: %d\n",
154 rw
? "write" : "read", func
, regaddr
, *byte
, err_ret
);
159 int brcmf_sdioh_request_word(struct brcmf_sdio_dev
*sdiodev
,
160 uint rw
, uint func
, uint addr
, u32
*word
,
166 brcmf_dbg(ERROR
, "Only CMD52 allowed to F0\n");
170 brcmf_dbg(INFO
, "rw=%d, func=%d, addr=0x%05x, nbytes=%d\n",
171 rw
, func
, addr
, nbytes
);
173 brcmf_pm_resume_wait(sdiodev
, &sdiodev
->request_word_wait
);
174 if (brcmf_pm_resume_error(sdiodev
))
176 /* Claim host controller */
177 sdio_claim_host(sdiodev
->func
[func
]);
179 if (rw
) { /* CMD52 Write */
181 sdio_writel(sdiodev
->func
[func
], *word
, addr
,
183 else if (nbytes
== 2)
184 sdio_writew(sdiodev
->func
[func
], (*word
& 0xFFFF),
187 brcmf_dbg(ERROR
, "Invalid nbytes: %d\n", nbytes
);
188 } else { /* CMD52 Read */
190 *word
= sdio_readl(sdiodev
->func
[func
], addr
, &err_ret
);
191 else if (nbytes
== 2)
192 *word
= sdio_readw(sdiodev
->func
[func
], addr
,
195 brcmf_dbg(ERROR
, "Invalid nbytes: %d\n", nbytes
);
198 /* Release host controller */
199 sdio_release_host(sdiodev
->func
[func
]);
202 brcmf_dbg(ERROR
, "Failed to %s word, Err: 0x%08x\n",
203 rw
? "write" : "read", err_ret
);
208 /* precondition: host controller is claimed */
210 brcmf_sdioh_request_data(struct brcmf_sdio_dev
*sdiodev
, uint write
, bool fifo
,
211 uint func
, uint addr
, struct sk_buff
*pkt
, uint pktlen
)
215 if ((write
) && (!fifo
)) {
216 err_ret
= sdio_memcpy_toio(sdiodev
->func
[func
], addr
,
217 ((u8
*) (pkt
->data
)), pktlen
);
219 err_ret
= sdio_memcpy_toio(sdiodev
->func
[func
], addr
,
220 ((u8
*) (pkt
->data
)), pktlen
);
222 err_ret
= sdio_readsb(sdiodev
->func
[func
],
223 ((u8
*) (pkt
->data
)), addr
, pktlen
);
225 err_ret
= sdio_memcpy_fromio(sdiodev
->func
[func
],
226 ((u8
*) (pkt
->data
)),
234 * This function takes a queue of packets. The packets on the queue
235 * are assumed to be properly aligned by the caller.
238 brcmf_sdioh_request_chain(struct brcmf_sdio_dev
*sdiodev
, uint fix_inc
,
239 uint write
, uint func
, uint addr
,
240 struct sk_buff_head
*pktq
)
242 bool fifo
= (fix_inc
== SDIOH_DATA_FIX
);
248 brcmf_dbg(TRACE
, "Enter\n");
250 brcmf_pm_resume_wait(sdiodev
, &sdiodev
->request_chain_wait
);
251 if (brcmf_pm_resume_error(sdiodev
))
254 /* Claim host controller */
255 sdio_claim_host(sdiodev
->func
[func
]);
257 skb_queue_walk(pktq
, pkt
) {
258 uint pkt_len
= pkt
->len
;
260 pkt_len
&= 0xFFFFFFFC;
262 err_ret
= brcmf_sdioh_request_data(sdiodev
, write
, fifo
, func
,
265 brcmf_dbg(ERROR
, "%s FAILED %p[%d], addr=0x%05x, pkt_len=%d, ERR=0x%08x\n",
266 write
? "TX" : "RX", pkt
, SGCount
, addr
,
269 brcmf_dbg(TRACE
, "%s xfr'd %p[%d], addr=0x%05x, len=%d\n",
270 write
? "TX" : "RX", pkt
, SGCount
, addr
,
279 /* Release host controller */
280 sdio_release_host(sdiodev
->func
[func
]);
282 brcmf_dbg(TRACE
, "Exit\n");
287 * This function takes a single DMA-able packet.
289 int brcmf_sdioh_request_buffer(struct brcmf_sdio_dev
*sdiodev
,
290 uint fix_inc
, uint write
, uint func
, uint addr
,
294 uint pkt_len
= pkt
->len
;
295 bool fifo
= (fix_inc
== SDIOH_DATA_FIX
);
297 brcmf_dbg(TRACE
, "Enter\n");
302 brcmf_pm_resume_wait(sdiodev
, &sdiodev
->request_buffer_wait
);
303 if (brcmf_pm_resume_error(sdiodev
))
306 /* Claim host controller */
307 sdio_claim_host(sdiodev
->func
[func
]);
312 status
= brcmf_sdioh_request_data(sdiodev
, write
, fifo
, func
,
315 brcmf_dbg(ERROR
, "%s FAILED %p, addr=0x%05x, pkt_len=%d, ERR=0x%08x\n",
316 write
? "TX" : "RX", pkt
, addr
, pkt_len
, status
);
318 brcmf_dbg(TRACE
, "%s xfr'd %p, addr=0x%05x, len=%d\n",
319 write
? "TX" : "RX", pkt
, addr
, pkt_len
);
322 /* Release host controller */
323 sdio_release_host(sdiodev
->func
[func
]);
328 /* Read client card reg */
330 brcmf_sdioh_card_regread(struct brcmf_sdio_dev
*sdiodev
, int func
, u32 regaddr
,
331 int regsize
, u32
*data
)
334 if ((func
== 0) || (regsize
== 1)) {
337 brcmf_sdioh_request_byte(sdiodev
, SDIOH_READ
, func
, regaddr
,
341 brcmf_dbg(DATA
, "byte read data=0x%02x\n", *data
);
343 brcmf_sdioh_request_word(sdiodev
, SDIOH_READ
, func
, regaddr
,
348 brcmf_dbg(DATA
, "word read data=0x%08x\n", *data
);
354 static int brcmf_sdioh_get_cisaddr(struct brcmf_sdio_dev
*sdiodev
, u32 regaddr
)
356 /* read 24 bits and return valid 17 bit addr */
358 u32 scratch
, regdata
;
360 u8
*ptr
= (u8
*)&scratch_le
;
362 for (i
= 0; i
< 3; i
++) {
363 if ((brcmf_sdioh_card_regread(sdiodev
, 0, regaddr
, 1,
364 ®data
)) != SUCCESS
)
365 brcmf_dbg(ERROR
, "Can't read!\n");
367 *ptr
++ = (u8
) regdata
;
371 /* Only the lower 17-bits are valid */
372 scratch
= le32_to_cpu(scratch_le
);
373 scratch
&= 0x0001FFFF;
377 static int brcmf_sdioh_enablefuncs(struct brcmf_sdio_dev
*sdiodev
)
383 brcmf_dbg(TRACE
, "\n");
385 /* Get the Card's common CIS address */
386 sdiodev
->func_cis_ptr
[0] = brcmf_sdioh_get_cisaddr(sdiodev
,
388 brcmf_dbg(INFO
, "Card's Common CIS Ptr = 0x%x\n",
389 sdiodev
->func_cis_ptr
[0]);
391 /* Get the Card's function CIS (for each function) */
392 for (fbraddr
= SDIO_FBR_BASE(1), func
= 1;
393 func
<= sdiodev
->num_funcs
; func
++, fbraddr
+= SDIOD_FBR_SIZE
) {
394 sdiodev
->func_cis_ptr
[func
] =
395 brcmf_sdioh_get_cisaddr(sdiodev
, SDIO_FBR_CIS
+ fbraddr
);
396 brcmf_dbg(INFO
, "Function %d CIS Ptr = 0x%x\n",
397 func
, sdiodev
->func_cis_ptr
[func
]);
400 /* Enable Function 1 */
401 sdio_claim_host(sdiodev
->func
[1]);
402 err_ret
= sdio_enable_func(sdiodev
->func
[1]);
403 sdio_release_host(sdiodev
->func
[1]);
405 brcmf_dbg(ERROR
, "Failed to enable F1 Err: 0x%08x\n", err_ret
);
411 * Public entry points & extern's
413 int brcmf_sdioh_attach(struct brcmf_sdio_dev
*sdiodev
)
417 brcmf_dbg(TRACE
, "\n");
419 sdiodev
->num_funcs
= 2;
421 sdio_claim_host(sdiodev
->func
[1]);
422 err_ret
= sdio_set_block_size(sdiodev
->func
[1], SDIO_FUNC1_BLOCKSIZE
);
423 sdio_release_host(sdiodev
->func
[1]);
425 brcmf_dbg(ERROR
, "Failed to set F1 blocksize\n");
429 sdio_claim_host(sdiodev
->func
[2]);
430 err_ret
= sdio_set_block_size(sdiodev
->func
[2], SDIO_FUNC2_BLOCKSIZE
);
431 sdio_release_host(sdiodev
->func
[2]);
433 brcmf_dbg(ERROR
, "Failed to set F2 blocksize\n");
437 brcmf_sdioh_enablefuncs(sdiodev
);
440 brcmf_dbg(TRACE
, "Done\n");
444 void brcmf_sdioh_detach(struct brcmf_sdio_dev
*sdiodev
)
446 brcmf_dbg(TRACE
, "\n");
448 /* Disable Function 2 */
449 sdio_claim_host(sdiodev
->func
[2]);
450 sdio_disable_func(sdiodev
->func
[2]);
451 sdio_release_host(sdiodev
->func
[2]);
453 /* Disable Function 1 */
454 sdio_claim_host(sdiodev
->func
[1]);
455 sdio_disable_func(sdiodev
->func
[1]);
456 sdio_release_host(sdiodev
->func
[1]);
460 static int brcmf_ops_sdio_probe(struct sdio_func
*func
,
461 const struct sdio_device_id
*id
)
464 struct brcmf_sdio_dev
*sdiodev
;
465 struct brcmf_bus
*bus_if
;
466 brcmf_dbg(TRACE
, "Enter\n");
467 brcmf_dbg(TRACE
, "func->class=%x\n", func
->class);
468 brcmf_dbg(TRACE
, "sdio_vendor: 0x%04x\n", func
->vendor
);
469 brcmf_dbg(TRACE
, "sdio_device: 0x%04x\n", func
->device
);
470 brcmf_dbg(TRACE
, "Function#: 0x%04x\n", func
->num
);
472 if (func
->num
== 1) {
473 if (dev_get_drvdata(&func
->card
->dev
)) {
474 brcmf_dbg(ERROR
, "card private drvdata occupied\n");
477 bus_if
= kzalloc(sizeof(struct brcmf_bus
), GFP_KERNEL
);
480 sdiodev
= kzalloc(sizeof(struct brcmf_sdio_dev
), GFP_KERNEL
);
485 sdiodev
->func
[0] = func
->card
->sdio_func
[0];
486 sdiodev
->func
[1] = func
;
487 sdiodev
->bus_if
= bus_if
;
488 bus_if
->bus_priv
= sdiodev
;
489 bus_if
->type
= SDIO_BUS
;
490 bus_if
->align
= BRCMF_SDALIGN
;
491 dev_set_drvdata(&func
->card
->dev
, sdiodev
);
493 atomic_set(&sdiodev
->suspend
, false);
494 init_waitqueue_head(&sdiodev
->request_byte_wait
);
495 init_waitqueue_head(&sdiodev
->request_word_wait
);
496 init_waitqueue_head(&sdiodev
->request_chain_wait
);
497 init_waitqueue_head(&sdiodev
->request_buffer_wait
);
500 if (func
->num
== 2) {
501 sdiodev
= dev_get_drvdata(&func
->card
->dev
);
502 if ((!sdiodev
) || (sdiodev
->func
[1]->card
!= func
->card
))
504 sdiodev
->func
[2] = func
;
506 bus_if
= sdiodev
->bus_if
;
507 sdiodev
->dev
= &func
->dev
;
508 dev_set_drvdata(&func
->dev
, bus_if
);
510 brcmf_dbg(TRACE
, "F2 found, calling brcmf_sdio_probe...\n");
511 ret
= brcmf_sdio_probe(sdiodev
);
517 static void brcmf_ops_sdio_remove(struct sdio_func
*func
)
519 struct brcmf_bus
*bus_if
;
520 struct brcmf_sdio_dev
*sdiodev
;
521 brcmf_dbg(TRACE
, "Enter\n");
522 brcmf_dbg(INFO
, "func->class=%x\n", func
->class);
523 brcmf_dbg(INFO
, "sdio_vendor: 0x%04x\n", func
->vendor
);
524 brcmf_dbg(INFO
, "sdio_device: 0x%04x\n", func
->device
);
525 brcmf_dbg(INFO
, "Function#: 0x%04x\n", func
->num
);
527 if (func
->num
== 2) {
528 bus_if
= dev_get_drvdata(&func
->dev
);
529 sdiodev
= bus_if
->bus_priv
;
530 brcmf_dbg(TRACE
, "F2 found, calling brcmf_sdio_remove...\n");
531 brcmf_sdio_remove(sdiodev
);
532 dev_set_drvdata(&func
->card
->dev
, NULL
);
533 dev_set_drvdata(&func
->dev
, NULL
);
539 #ifdef CONFIG_PM_SLEEP
540 static int brcmf_sdio_suspend(struct device
*dev
)
542 mmc_pm_flag_t sdio_flags
;
543 struct sdio_func
*func
= dev_to_sdio_func(dev
);
544 struct brcmf_sdio_dev
*sdiodev
= dev_get_drvdata(&func
->card
->dev
);
547 brcmf_dbg(TRACE
, "\n");
549 atomic_set(&sdiodev
->suspend
, true);
551 sdio_flags
= sdio_get_host_pm_caps(sdiodev
->func
[1]);
552 if (!(sdio_flags
& MMC_PM_KEEP_POWER
)) {
553 brcmf_dbg(ERROR
, "Host can't keep power while suspended\n");
557 ret
= sdio_set_host_pm_flags(sdiodev
->func
[1], MMC_PM_KEEP_POWER
);
559 brcmf_dbg(ERROR
, "Failed to set pm_flags\n");
563 brcmf_sdio_wdtmr_enable(sdiodev
, false);
568 static int brcmf_sdio_resume(struct device
*dev
)
570 struct sdio_func
*func
= dev_to_sdio_func(dev
);
571 struct brcmf_sdio_dev
*sdiodev
= dev_get_drvdata(&func
->card
->dev
);
573 brcmf_sdio_wdtmr_enable(sdiodev
, true);
574 atomic_set(&sdiodev
->suspend
, false);
578 static const struct dev_pm_ops brcmf_sdio_pm_ops
= {
579 .suspend
= brcmf_sdio_suspend
,
580 .resume
= brcmf_sdio_resume
,
582 #endif /* CONFIG_PM_SLEEP */
584 static struct sdio_driver brcmf_sdmmc_driver
= {
585 .probe
= brcmf_ops_sdio_probe
,
586 .remove
= brcmf_ops_sdio_remove
,
588 .id_table
= brcmf_sdmmc_ids
,
589 #ifdef CONFIG_PM_SLEEP
591 .pm
= &brcmf_sdio_pm_ops
,
593 #endif /* CONFIG_PM_SLEEP */
596 static void __exit
brcmf_sdio_exit(void)
598 brcmf_dbg(TRACE
, "Enter\n");
600 sdio_unregister_driver(&brcmf_sdmmc_driver
);
603 static int __init
brcmf_sdio_init(void)
607 brcmf_dbg(TRACE
, "Enter\n");
609 ret
= sdio_register_driver(&brcmf_sdmmc_driver
);
612 brcmf_dbg(ERROR
, "sdio_register_driver failed: %d\n", ret
);
617 module_init(brcmf_sdio_init
);
618 module_exit(brcmf_sdio_exit
);