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_sdcard_cfg_read(sdiodev, SDIO_FUNC_0,
89 SDIO_CCCR_IENx, &ret);
90 data |= 1 << SDIO_FUNC_1 | 1 << SDIO_FUNC_2 | 1;
91 brcmf_sdcard_cfg_write(sdiodev, SDIO_FUNC_0, SDIO_CCCR_IENx,
94 /* redirect, configure ane enable io for interrupt signal */
95 data = SDIO_SEPINT_MASK | SDIO_SEPINT_OE;
96 if (sdiodev->irq_flags | IRQF_TRIGGER_HIGH)
97 data |= SDIO_SEPINT_ACT_HI;
98 brcmf_sdcard_cfg_write(sdiodev, SDIO_FUNC_0, SDIO_CCCR_BRCM_SEPINT,
104 int brcmf_sdio_intr_unregister(struct brcmf_sdio_dev *sdiodev)
106 brcmf_dbg(TRACE, "Entering\n");
108 brcmf_sdcard_cfg_write(sdiodev, SDIO_FUNC_0, SDIO_CCCR_BRCM_SEPINT,
110 brcmf_sdcard_cfg_write(sdiodev, SDIO_FUNC_0, SDIO_CCCR_IENx, 0, NULL);
112 if (sdiodev->irq_wake) {
113 disable_irq_wake(sdiodev->irq);
114 sdiodev->irq_wake = false;
116 free_irq(sdiodev->irq, &sdiodev->func[1]->card->dev);
117 sdiodev->irq_en = false;
121 #else /* CONFIG_BRCMFMAC_SDIO_OOB */
122 static void brcmf_sdio_irqhandler(struct sdio_func *func)
124 struct brcmf_sdio_dev *sdiodev = dev_get_drvdata(&func->card->dev);
126 brcmf_dbg(INTR, "ib intr triggered\n");
128 brcmf_sdbrcm_isr(sdiodev->bus);
131 /* dummy handler for SDIO function 2 interrupt */
132 static void brcmf_sdio_dummy_irqhandler(struct sdio_func *func)
136 int brcmf_sdio_intr_register(struct brcmf_sdio_dev *sdiodev)
138 brcmf_dbg(TRACE, "Entering\n");
140 sdio_claim_host(sdiodev->func[1]);
141 sdio_claim_irq(sdiodev->func[1], brcmf_sdio_irqhandler);
142 sdio_claim_irq(sdiodev->func[2], brcmf_sdio_dummy_irqhandler);
143 sdio_release_host(sdiodev->func[1]);
148 int brcmf_sdio_intr_unregister(struct brcmf_sdio_dev *sdiodev)
150 brcmf_dbg(TRACE, "Entering\n");
152 sdio_claim_host(sdiodev->func[1]);
153 sdio_release_irq(sdiodev->func[2]);
154 sdio_release_irq(sdiodev->func[1]);
155 sdio_release_host(sdiodev->func[1]);
159 #endif /* CONFIG_BRCMFMAC_SDIO_OOB */
161 u8 brcmf_sdcard_cfg_read(struct brcmf_sdio_dev *sdiodev, uint fnc_num, u32 addr,
169 if (retry) /* wait for 1 ms till bus get settled down */
171 status = brcmf_sdioh_request_byte(sdiodev, SDIOH_READ, fnc_num,
174 && (retry++ < SDIOH_API_ACCESS_RETRY_LIMIT));
178 brcmf_dbg(INFO, "fun = %d, addr = 0x%x, u8data = 0x%x\n",
179 fnc_num, addr, data);
185 brcmf_sdcard_cfg_write(struct brcmf_sdio_dev *sdiodev, uint fnc_num, u32 addr,
192 if (retry) /* wait for 1 ms till bus get settled down */
194 status = brcmf_sdioh_request_byte(sdiodev, SDIOH_WRITE, fnc_num,
197 && (retry++ < SDIOH_API_ACCESS_RETRY_LIMIT));
201 brcmf_dbg(INFO, "fun = %d, addr = 0x%x, u8data = 0x%x\n",
202 fnc_num, addr, data);
206 brcmf_sdcard_set_sbaddr_window(struct brcmf_sdio_dev *sdiodev, u32 address)
209 brcmf_sdcard_cfg_write(sdiodev, SDIO_FUNC_1, SBSDIO_FUNC1_SBADDRLOW,
210 (address >> 8) & SBSDIO_SBADDRLOW_MASK, &err);
212 brcmf_sdcard_cfg_write(sdiodev, SDIO_FUNC_1,
213 SBSDIO_FUNC1_SBADDRMID,
214 (address >> 16) & SBSDIO_SBADDRMID_MASK,
217 brcmf_sdcard_cfg_write(sdiodev, SDIO_FUNC_1,
218 SBSDIO_FUNC1_SBADDRHIGH,
219 (address >> 24) & SBSDIO_SBADDRHIGH_MASK,
225 u32 brcmf_sdcard_reg_read(struct brcmf_sdio_dev *sdiodev, u32 addr)
229 uint bar0 = addr & ~SBSDIO_SB_OFT_ADDR_MASK;
231 brcmf_dbg(INFO, "fun = 1, addr = 0x%x\n", addr);
233 if (bar0 != sdiodev->sbwad) {
234 if (brcmf_sdcard_set_sbaddr_window(sdiodev, bar0))
237 sdiodev->sbwad = bar0;
240 addr &= SBSDIO_SB_OFT_ADDR_MASK;
241 addr |= SBSDIO_SB_ACCESS_2_4B_FLAG;
243 status = brcmf_sdioh_request_word(sdiodev, SDIOH_READ, SDIO_FUNC_1,
246 sdiodev->regfail = (status != 0);
249 brcmf_dbg(INFO, "data = 0x%x\n", word);
252 brcmf_dbg(ERROR, "failed %d at addr 0x%04x\n", status, addr);
257 u32 brcmf_sdcard_reg_write(struct brcmf_sdio_dev *sdiodev, u32 addr, u32 data)
260 uint bar0 = addr & ~SBSDIO_SB_OFT_ADDR_MASK;
263 brcmf_dbg(INFO, "fun = 1, addr = 0x%x, uint32data = 0x%x\n",
266 if (bar0 != sdiodev->sbwad) {
267 err = brcmf_sdcard_set_sbaddr_window(sdiodev, bar0);
271 sdiodev->sbwad = bar0;
274 addr &= SBSDIO_SB_OFT_ADDR_MASK;
275 addr |= SBSDIO_SB_ACCESS_2_4B_FLAG;
277 brcmf_sdioh_request_word(sdiodev, SDIOH_WRITE, SDIO_FUNC_1,
279 sdiodev->regfail = (status != 0);
284 brcmf_dbg(ERROR, "error writing 0x%08x to addr 0x%04x\n",
289 bool brcmf_sdcard_regfail(struct brcmf_sdio_dev *sdiodev)
291 return sdiodev->regfail;
294 static int brcmf_sdcard_recv_prepare(struct brcmf_sdio_dev *sdiodev, uint fn,
295 uint flags, uint width, u32 *addr)
297 uint bar0 = *addr & ~SBSDIO_SB_OFT_ADDR_MASK;
300 /* Async not implemented yet */
301 if (flags & SDIO_REQ_ASYNC)
304 if (bar0 != sdiodev->sbwad) {
305 err = brcmf_sdcard_set_sbaddr_window(sdiodev, bar0);
309 sdiodev->sbwad = bar0;
312 *addr &= SBSDIO_SB_OFT_ADDR_MASK;
315 *addr |= SBSDIO_SB_ACCESS_2_4B_FLAG;
321 brcmf_sdcard_recv_buf(struct brcmf_sdio_dev *sdiodev, u32 addr, uint fn,
322 uint flags, u8 *buf, uint nbytes)
324 struct sk_buff *mypkt;
327 mypkt = brcmu_pkt_buf_get_skb(nbytes);
329 brcmf_dbg(ERROR, "brcmu_pkt_buf_get_skb failed: len %d\n",
334 err = brcmf_sdcard_recv_pkt(sdiodev, addr, fn, flags, mypkt);
336 memcpy(buf, mypkt->data, nbytes);
338 brcmu_pkt_buf_free_skb(mypkt);
343 brcmf_sdcard_recv_pkt(struct brcmf_sdio_dev *sdiodev, u32 addr, uint fn,
344 uint flags, struct sk_buff *pkt)
350 brcmf_dbg(INFO, "fun = %d, addr = 0x%x, size = %d\n",
353 width = (flags & SDIO_REQ_4BYTE) ? 4 : 2;
354 err = brcmf_sdcard_recv_prepare(sdiodev, fn, flags, width, &addr);
358 incr_fix = (flags & SDIO_REQ_FIXED) ? SDIOH_DATA_FIX : SDIOH_DATA_INC;
359 err = brcmf_sdioh_request_buffer(sdiodev, incr_fix, SDIOH_READ,
365 int brcmf_sdcard_recv_chain(struct brcmf_sdio_dev *sdiodev, u32 addr, uint fn,
366 uint flags, struct sk_buff_head *pktq)
372 brcmf_dbg(INFO, "fun = %d, addr = 0x%x, size = %d\n",
373 fn, addr, pktq->qlen);
375 width = (flags & SDIO_REQ_4BYTE) ? 4 : 2;
376 err = brcmf_sdcard_recv_prepare(sdiodev, fn, flags, width, &addr);
380 incr_fix = (flags & SDIO_REQ_FIXED) ? SDIOH_DATA_FIX : SDIOH_DATA_INC;
381 err = brcmf_sdioh_request_chain(sdiodev, incr_fix, SDIOH_READ, fn, addr,
388 brcmf_sdcard_send_buf(struct brcmf_sdio_dev *sdiodev, u32 addr, uint fn,
389 uint flags, u8 *buf, uint nbytes)
391 struct sk_buff *mypkt;
394 mypkt = brcmu_pkt_buf_get_skb(nbytes);
396 brcmf_dbg(ERROR, "brcmu_pkt_buf_get_skb failed: len %d\n",
401 memcpy(mypkt->data, buf, nbytes);
402 err = brcmf_sdcard_send_pkt(sdiodev, addr, fn, flags, mypkt);
404 brcmu_pkt_buf_free_skb(mypkt);
410 brcmf_sdcard_send_pkt(struct brcmf_sdio_dev *sdiodev, u32 addr, uint fn,
411 uint flags, struct sk_buff *pkt)
415 uint bar0 = addr & ~SBSDIO_SB_OFT_ADDR_MASK;
418 brcmf_dbg(INFO, "fun = %d, addr = 0x%x, size = %d\n",
421 /* Async not implemented yet */
422 if (flags & SDIO_REQ_ASYNC)
425 if (bar0 != sdiodev->sbwad) {
426 err = brcmf_sdcard_set_sbaddr_window(sdiodev, bar0);
430 sdiodev->sbwad = bar0;
433 addr &= SBSDIO_SB_OFT_ADDR_MASK;
435 incr_fix = (flags & SDIO_REQ_FIXED) ? SDIOH_DATA_FIX : SDIOH_DATA_INC;
436 width = (flags & SDIO_REQ_4BYTE) ? 4 : 2;
438 addr |= SBSDIO_SB_ACCESS_2_4B_FLAG;
440 return brcmf_sdioh_request_buffer(sdiodev, incr_fix, SDIOH_WRITE, fn,
444 int brcmf_sdcard_rwdata(struct brcmf_sdio_dev *sdiodev, uint rw, u32 addr,
445 u8 *buf, uint nbytes)
447 struct sk_buff *mypkt;
448 bool write = rw ? SDIOH_WRITE : SDIOH_READ;
451 addr &= SBSDIO_SB_OFT_ADDR_MASK;
452 addr |= SBSDIO_SB_ACCESS_2_4B_FLAG;
454 mypkt = brcmu_pkt_buf_get_skb(nbytes);
456 brcmf_dbg(ERROR, "brcmu_pkt_buf_get_skb failed: len %d\n",
461 /* For a write, copy the buffer data into the packet. */
463 memcpy(mypkt->data, buf, nbytes);
465 err = brcmf_sdioh_request_buffer(sdiodev, SDIOH_DATA_INC, write,
466 SDIO_FUNC_1, addr, mypkt);
468 /* For a read, copy the packet data back to the buffer. */
470 memcpy(buf, mypkt->data, nbytes);
472 brcmu_pkt_buf_free_skb(mypkt);
476 int brcmf_sdcard_abort(struct brcmf_sdio_dev *sdiodev, uint fn)
478 char t_func = (char)fn;
479 brcmf_dbg(TRACE, "Enter\n");
481 /* issue abort cmd52 command through F0 */
482 brcmf_sdioh_request_byte(sdiodev, SDIOH_WRITE, SDIO_FUNC_0,
483 SDIO_CCCR_ABORT, &t_func);
485 brcmf_dbg(TRACE, "Exit\n");
489 int brcmf_sdio_probe(struct brcmf_sdio_dev *sdiodev)
494 ret = brcmf_sdioh_attach(sdiodev);
500 /* Report the BAR, to fix if needed */
501 sdiodev->sbwad = SI_ENUM_BASE;
503 /* try to attach to the target device */
504 sdiodev->bus = brcmf_sdbrcm_probe(regs, sdiodev);
506 brcmf_dbg(ERROR, "device attach failed\n");
513 brcmf_sdio_remove(sdiodev);
517 EXPORT_SYMBOL(brcmf_sdio_probe);
519 int brcmf_sdio_remove(struct brcmf_sdio_dev *sdiodev)
522 brcmf_sdbrcm_disconnect(sdiodev->bus);
526 brcmf_sdioh_detach(sdiodev);
532 EXPORT_SYMBOL(brcmf_sdio_remove);
534 void brcmf_sdio_wdtmr_enable(struct brcmf_sdio_dev *sdiodev, bool enable)
537 brcmf_sdbrcm_wd_timer(sdiodev->bus, BRCMF_WD_POLL_MS);
539 brcmf_sdbrcm_wd_timer(sdiodev->bus, 0);