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 /* ****************** SDIO CARD Interface Functions **************************/
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20 #include <linux/types.h>
21 #include <linux/netdevice.h>
22 #include <linux/export.h>
23 #include <linux/pci.h>
24 #include <linux/pci_ids.h>
25 #include <linux/sched.h>
26 #include <linux/completion.h>
27 #include <linux/mmc/sdio.h>
28 #include <linux/mmc/sdio_func.h>
29 #include <linux/mmc/card.h>
32 #include <brcm_hw_ids.h>
33 #include <brcmu_utils.h>
34 #include <brcmu_wifi.h>
38 #include "sdio_host.h"
40 #define SDIOH_API_ACCESS_RETRY_LIMIT 2
42 #ifdef CONFIG_BRCMFMAC_SDIO_OOB
43 static irqreturn_t brcmf_sdio_irqhandler(int irq, void *dev_id)
45 struct brcmf_sdio_dev *sdiodev = dev_get_drvdata(dev_id);
47 brcmf_dbg(INTR, "oob intr triggered\n");
50 * out-of-band interrupt is level-triggered which won't
51 * be cleared until dpc
53 if (sdiodev->irq_en) {
54 disable_irq_nosync(irq);
55 sdiodev->irq_en = false;
58 brcmf_sdbrcm_isr(sdiodev->bus);
63 int brcmf_sdio_intr_register(struct brcmf_sdio_dev *sdiodev)
69 brcmf_dbg(TRACE, "Entering\n");
71 brcmf_dbg(ERROR, "requesting irq %d\n", sdiodev->irq);
72 ret = request_irq(sdiodev->irq, brcmf_sdio_irqhandler,
73 sdiodev->irq_flags, "brcmf_oob_intr",
74 &sdiodev->func[1]->card->dev);
77 spin_lock_init(&sdiodev->irq_en_lock);
78 spin_lock_irqsave(&sdiodev->irq_en_lock, flags);
79 sdiodev->irq_en = true;
80 spin_unlock_irqrestore(&sdiodev->irq_en_lock, flags);
82 ret = enable_irq_wake(sdiodev->irq);
85 sdiodev->irq_wake = true;
87 /* must configure SDIO_CCCR_IENx to enable irq */
88 data = brcmf_sdio_regrb(sdiodev, SDIO_CCCR_IENx, &ret);
89 data |= 1 << SDIO_FUNC_1 | 1 << SDIO_FUNC_2 | 1;
90 brcmf_sdcard_cfg_write(sdiodev, SDIO_FUNC_0, SDIO_CCCR_IENx,
93 /* redirect, configure ane enable io for interrupt signal */
94 data = SDIO_SEPINT_MASK | SDIO_SEPINT_OE;
95 if (sdiodev->irq_flags | IRQF_TRIGGER_HIGH)
96 data |= SDIO_SEPINT_ACT_HI;
97 brcmf_sdcard_cfg_write(sdiodev, SDIO_FUNC_0, SDIO_CCCR_BRCM_SEPINT,
103 int brcmf_sdio_intr_unregister(struct brcmf_sdio_dev *sdiodev)
105 brcmf_dbg(TRACE, "Entering\n");
107 brcmf_sdcard_cfg_write(sdiodev, SDIO_FUNC_0, SDIO_CCCR_BRCM_SEPINT,
109 brcmf_sdcard_cfg_write(sdiodev, SDIO_FUNC_0, SDIO_CCCR_IENx, 0, NULL);
111 if (sdiodev->irq_wake) {
112 disable_irq_wake(sdiodev->irq);
113 sdiodev->irq_wake = false;
115 free_irq(sdiodev->irq, &sdiodev->func[1]->card->dev);
116 sdiodev->irq_en = false;
120 #else /* CONFIG_BRCMFMAC_SDIO_OOB */
121 static void brcmf_sdio_irqhandler(struct sdio_func *func)
123 struct brcmf_sdio_dev *sdiodev = dev_get_drvdata(&func->card->dev);
125 brcmf_dbg(INTR, "ib intr triggered\n");
127 brcmf_sdbrcm_isr(sdiodev->bus);
130 /* dummy handler for SDIO function 2 interrupt */
131 static void brcmf_sdio_dummy_irqhandler(struct sdio_func *func)
135 int brcmf_sdio_intr_register(struct brcmf_sdio_dev *sdiodev)
137 brcmf_dbg(TRACE, "Entering\n");
139 sdio_claim_host(sdiodev->func[1]);
140 sdio_claim_irq(sdiodev->func[1], brcmf_sdio_irqhandler);
141 sdio_claim_irq(sdiodev->func[2], brcmf_sdio_dummy_irqhandler);
142 sdio_release_host(sdiodev->func[1]);
147 int brcmf_sdio_intr_unregister(struct brcmf_sdio_dev *sdiodev)
149 brcmf_dbg(TRACE, "Entering\n");
151 sdio_claim_host(sdiodev->func[1]);
152 sdio_release_irq(sdiodev->func[2]);
153 sdio_release_irq(sdiodev->func[1]);
154 sdio_release_host(sdiodev->func[1]);
158 #endif /* CONFIG_BRCMFMAC_SDIO_OOB */
161 brcmf_sdcard_cfg_write(struct brcmf_sdio_dev *sdiodev, uint fnc_num, u32 addr,
168 if (retry) /* wait for 1 ms till bus get settled down */
170 status = brcmf_sdioh_request_byte(sdiodev, SDIOH_WRITE, fnc_num,
173 && (retry++ < SDIOH_API_ACCESS_RETRY_LIMIT));
177 brcmf_dbg(INFO, "fun = %d, addr = 0x%x, u8data = 0x%x\n",
178 fnc_num, addr, data);
182 brcmf_sdcard_set_sbaddr_window(struct brcmf_sdio_dev *sdiodev, u32 address)
188 addr[0] = (address >> 8) & SBSDIO_SBADDRLOW_MASK;
189 addr[1] = (address >> 16) & SBSDIO_SBADDRMID_MASK;
190 addr[2] = (address >> 24) & SBSDIO_SBADDRHIGH_MASK;
192 for (i = 0; i < 3; i++) {
196 usleep_range(1000, 2000);
197 err = brcmf_sdioh_request_byte(sdiodev, SDIOH_WRITE,
198 SDIO_FUNC_1, SBSDIO_FUNC1_SBADDRLOW + i,
200 } while (err != 0 && retry++ < SDIOH_API_ACCESS_RETRY_LIMIT);
203 brcmf_dbg(ERROR, "failed at addr:0x%0x\n",
204 SBSDIO_FUNC1_SBADDRLOW + i);
213 brcmf_sdio_regrw_helper(struct brcmf_sdio_dev *sdiodev, u32 addr,
214 void *data, bool write)
216 u8 func_num, reg_size;
222 * figure out how to read the register based on address range
223 * 0x00 ~ 0xFF: function 0 CCCR
224 * 0x10000 ~ 0x1FFFF: function 1 miscellaneous registers
225 * The rest: function 1 silicon backplane core registers
227 if ((addr & ~REG_F0_CCCR_MASK) == 0) {
228 func_num = SDIO_FUNC_0;
230 } else if ((addr & ~REG_F1_MISC_MASK) == 0) {
231 func_num = SDIO_FUNC_1;
234 func_num = SDIO_FUNC_1;
237 /* Set the window for SB core register */
238 bar = addr & ~SBSDIO_SB_OFT_ADDR_MASK;
239 if (bar != sdiodev->sbwad) {
240 ret = brcmf_sdcard_set_sbaddr_window(sdiodev, bar);
242 memset(data, 0xFF, reg_size);
245 sdiodev->sbwad = bar;
247 addr &= SBSDIO_SB_OFT_ADDR_MASK;
248 addr |= SBSDIO_SB_ACCESS_2_4B_FLAG;
253 memset(data, 0, reg_size);
254 if (retry) /* wait for 1 ms till bus get settled down */
255 usleep_range(1000, 2000);
257 ret = brcmf_sdioh_request_byte(sdiodev, write,
258 func_num, addr, data);
260 ret = brcmf_sdioh_request_word(sdiodev, write,
261 func_num, addr, data, 4);
262 } while (ret != 0 && retry++ < SDIOH_API_ACCESS_RETRY_LIMIT);
264 sdiodev->regfail = (ret != 0);
265 if (sdiodev->regfail)
266 brcmf_dbg(ERROR, "failed with %d\n", ret);
271 u8 brcmf_sdio_regrb(struct brcmf_sdio_dev *sdiodev, u32 addr, int *ret)
276 brcmf_dbg(INFO, "addr:0x%08x\n", addr);
277 retval = brcmf_sdio_regrw_helper(sdiodev, addr, &data, false);
278 brcmf_dbg(INFO, "data:0x%02x\n", data);
286 u32 brcmf_sdio_regrl(struct brcmf_sdio_dev *sdiodev, u32 addr, int *ret)
291 brcmf_dbg(INFO, "addr:0x%08x\n", addr);
292 retval = brcmf_sdio_regrw_helper(sdiodev, addr, &data, false);
293 brcmf_dbg(INFO, "data:0x%08x\n", data);
301 void brcmf_sdio_regwb(struct brcmf_sdio_dev *sdiodev, u32 addr,
306 brcmf_dbg(INFO, "addr:0x%08x, data:0x%02x\n", addr, data);
307 retval = brcmf_sdio_regrw_helper(sdiodev, addr, &data, true);
313 void brcmf_sdio_regwl(struct brcmf_sdio_dev *sdiodev, u32 addr,
318 brcmf_dbg(INFO, "addr:0x%08x, data:0x%08x\n", addr, data);
319 retval = brcmf_sdio_regrw_helper(sdiodev, addr, &data, true);
325 u32 brcmf_sdcard_reg_read(struct brcmf_sdio_dev *sdiodev, u32 addr)
329 uint bar0 = addr & ~SBSDIO_SB_OFT_ADDR_MASK;
331 brcmf_dbg(INFO, "fun = 1, addr = 0x%x\n", addr);
333 if (bar0 != sdiodev->sbwad) {
334 if (brcmf_sdcard_set_sbaddr_window(sdiodev, bar0))
337 sdiodev->sbwad = bar0;
340 addr &= SBSDIO_SB_OFT_ADDR_MASK;
341 addr |= SBSDIO_SB_ACCESS_2_4B_FLAG;
343 status = brcmf_sdioh_request_word(sdiodev, SDIOH_READ, SDIO_FUNC_1,
346 sdiodev->regfail = (status != 0);
349 brcmf_dbg(INFO, "data = 0x%x\n", word);
352 brcmf_dbg(ERROR, "failed %d at addr 0x%04x\n", status, addr);
357 u32 brcmf_sdcard_reg_write(struct brcmf_sdio_dev *sdiodev, u32 addr, u32 data)
360 uint bar0 = addr & ~SBSDIO_SB_OFT_ADDR_MASK;
363 brcmf_dbg(INFO, "fun = 1, addr = 0x%x, uint32data = 0x%x\n",
366 if (bar0 != sdiodev->sbwad) {
367 err = brcmf_sdcard_set_sbaddr_window(sdiodev, bar0);
371 sdiodev->sbwad = bar0;
374 addr &= SBSDIO_SB_OFT_ADDR_MASK;
375 addr |= SBSDIO_SB_ACCESS_2_4B_FLAG;
377 brcmf_sdioh_request_word(sdiodev, SDIOH_WRITE, SDIO_FUNC_1,
379 sdiodev->regfail = (status != 0);
384 brcmf_dbg(ERROR, "error writing 0x%08x to addr 0x%04x\n",
389 bool brcmf_sdcard_regfail(struct brcmf_sdio_dev *sdiodev)
391 return sdiodev->regfail;
394 static int brcmf_sdcard_recv_prepare(struct brcmf_sdio_dev *sdiodev, uint fn,
395 uint flags, uint width, u32 *addr)
397 uint bar0 = *addr & ~SBSDIO_SB_OFT_ADDR_MASK;
400 /* Async not implemented yet */
401 if (flags & SDIO_REQ_ASYNC)
404 if (bar0 != sdiodev->sbwad) {
405 err = brcmf_sdcard_set_sbaddr_window(sdiodev, bar0);
409 sdiodev->sbwad = bar0;
412 *addr &= SBSDIO_SB_OFT_ADDR_MASK;
415 *addr |= SBSDIO_SB_ACCESS_2_4B_FLAG;
421 brcmf_sdcard_recv_buf(struct brcmf_sdio_dev *sdiodev, u32 addr, uint fn,
422 uint flags, u8 *buf, uint nbytes)
424 struct sk_buff *mypkt;
427 mypkt = brcmu_pkt_buf_get_skb(nbytes);
429 brcmf_dbg(ERROR, "brcmu_pkt_buf_get_skb failed: len %d\n",
434 err = brcmf_sdcard_recv_pkt(sdiodev, addr, fn, flags, mypkt);
436 memcpy(buf, mypkt->data, nbytes);
438 brcmu_pkt_buf_free_skb(mypkt);
443 brcmf_sdcard_recv_pkt(struct brcmf_sdio_dev *sdiodev, u32 addr, uint fn,
444 uint flags, struct sk_buff *pkt)
450 brcmf_dbg(INFO, "fun = %d, addr = 0x%x, size = %d\n",
453 width = (flags & SDIO_REQ_4BYTE) ? 4 : 2;
454 err = brcmf_sdcard_recv_prepare(sdiodev, fn, flags, width, &addr);
458 incr_fix = (flags & SDIO_REQ_FIXED) ? SDIOH_DATA_FIX : SDIOH_DATA_INC;
459 err = brcmf_sdioh_request_buffer(sdiodev, incr_fix, SDIOH_READ,
465 int brcmf_sdcard_recv_chain(struct brcmf_sdio_dev *sdiodev, u32 addr, uint fn,
466 uint flags, struct sk_buff_head *pktq)
472 brcmf_dbg(INFO, "fun = %d, addr = 0x%x, size = %d\n",
473 fn, addr, pktq->qlen);
475 width = (flags & SDIO_REQ_4BYTE) ? 4 : 2;
476 err = brcmf_sdcard_recv_prepare(sdiodev, fn, flags, width, &addr);
480 incr_fix = (flags & SDIO_REQ_FIXED) ? SDIOH_DATA_FIX : SDIOH_DATA_INC;
481 err = brcmf_sdioh_request_chain(sdiodev, incr_fix, SDIOH_READ, fn, addr,
488 brcmf_sdcard_send_buf(struct brcmf_sdio_dev *sdiodev, u32 addr, uint fn,
489 uint flags, u8 *buf, uint nbytes)
491 struct sk_buff *mypkt;
494 mypkt = brcmu_pkt_buf_get_skb(nbytes);
496 brcmf_dbg(ERROR, "brcmu_pkt_buf_get_skb failed: len %d\n",
501 memcpy(mypkt->data, buf, nbytes);
502 err = brcmf_sdcard_send_pkt(sdiodev, addr, fn, flags, mypkt);
504 brcmu_pkt_buf_free_skb(mypkt);
510 brcmf_sdcard_send_pkt(struct brcmf_sdio_dev *sdiodev, u32 addr, uint fn,
511 uint flags, struct sk_buff *pkt)
515 uint bar0 = addr & ~SBSDIO_SB_OFT_ADDR_MASK;
518 brcmf_dbg(INFO, "fun = %d, addr = 0x%x, size = %d\n",
521 /* Async not implemented yet */
522 if (flags & SDIO_REQ_ASYNC)
525 if (bar0 != sdiodev->sbwad) {
526 err = brcmf_sdcard_set_sbaddr_window(sdiodev, bar0);
530 sdiodev->sbwad = bar0;
533 addr &= SBSDIO_SB_OFT_ADDR_MASK;
535 incr_fix = (flags & SDIO_REQ_FIXED) ? SDIOH_DATA_FIX : SDIOH_DATA_INC;
536 width = (flags & SDIO_REQ_4BYTE) ? 4 : 2;
538 addr |= SBSDIO_SB_ACCESS_2_4B_FLAG;
540 return brcmf_sdioh_request_buffer(sdiodev, incr_fix, SDIOH_WRITE, fn,
544 int brcmf_sdcard_rwdata(struct brcmf_sdio_dev *sdiodev, uint rw, u32 addr,
545 u8 *buf, uint nbytes)
547 struct sk_buff *mypkt;
548 bool write = rw ? SDIOH_WRITE : SDIOH_READ;
551 addr &= SBSDIO_SB_OFT_ADDR_MASK;
552 addr |= SBSDIO_SB_ACCESS_2_4B_FLAG;
554 mypkt = brcmu_pkt_buf_get_skb(nbytes);
556 brcmf_dbg(ERROR, "brcmu_pkt_buf_get_skb failed: len %d\n",
561 /* For a write, copy the buffer data into the packet. */
563 memcpy(mypkt->data, buf, nbytes);
565 err = brcmf_sdioh_request_buffer(sdiodev, SDIOH_DATA_INC, write,
566 SDIO_FUNC_1, addr, mypkt);
568 /* For a read, copy the packet data back to the buffer. */
570 memcpy(buf, mypkt->data, nbytes);
572 brcmu_pkt_buf_free_skb(mypkt);
576 int brcmf_sdcard_abort(struct brcmf_sdio_dev *sdiodev, uint fn)
578 char t_func = (char)fn;
579 brcmf_dbg(TRACE, "Enter\n");
581 /* issue abort cmd52 command through F0 */
582 brcmf_sdioh_request_byte(sdiodev, SDIOH_WRITE, SDIO_FUNC_0,
583 SDIO_CCCR_ABORT, &t_func);
585 brcmf_dbg(TRACE, "Exit\n");
589 int brcmf_sdio_probe(struct brcmf_sdio_dev *sdiodev)
594 ret = brcmf_sdioh_attach(sdiodev);
600 /* Report the BAR, to fix if needed */
601 sdiodev->sbwad = SI_ENUM_BASE;
603 /* try to attach to the target device */
604 sdiodev->bus = brcmf_sdbrcm_probe(regs, sdiodev);
606 brcmf_dbg(ERROR, "device attach failed\n");
613 brcmf_sdio_remove(sdiodev);
617 EXPORT_SYMBOL(brcmf_sdio_probe);
619 int brcmf_sdio_remove(struct brcmf_sdio_dev *sdiodev)
622 brcmf_sdbrcm_disconnect(sdiodev->bus);
626 brcmf_sdioh_detach(sdiodev);
632 EXPORT_SYMBOL(brcmf_sdio_remove);
634 void brcmf_sdio_wdtmr_enable(struct brcmf_sdio_dev *sdiodev, bool enable)
637 brcmf_sdbrcm_wd_timer(sdiodev->bus, BRCMF_WD_POLL_MS);
639 brcmf_sdbrcm_wd_timer(sdiodev->bus, 0);