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 #include <linux/types.h>
19 #include <linux/netdevice.h>
20 #include <linux/pci.h>
21 #include <linux/pci_ids.h>
22 #include <linux/sched.h>
23 #include <linux/completion.h>
24 #include <linux/scatterlist.h>
25 #include <linux/mmc/sdio.h>
26 #include <linux/mmc/core.h>
27 #include <linux/mmc/sdio_func.h>
28 #include <linux/mmc/sdio_ids.h>
29 #include <linux/mmc/card.h>
30 #include <linux/mmc/host.h>
31 #include <linux/platform_device.h>
32 #include <linux/platform_data/brcmfmac-sdio.h>
33 #include <linux/suspend.h>
34 #include <linux/errno.h>
35 #include <linux/module.h>
36 #include <net/cfg80211.h>
39 #include <brcm_hw_ids.h>
40 #include <brcmu_utils.h>
41 #include <brcmu_wifi.h>
45 #include "sdio_host.h"
47 #define SDIOH_API_ACCESS_RETRY_LIMIT 2
49 #define DMA_ALIGN_MASK 0x03
51 #define SDIO_FUNC1_BLOCKSIZE 64
52 #define SDIO_FUNC2_BLOCKSIZE 512
53 /* Maximum milliseconds to wait for F2 to come up */
54 #define SDIO_WAIT_F2RDY 3000
56 #define BRCMF_DEFAULT_TXGLOM_SIZE 32 /* max tx frames in glom chain */
57 #define BRCMF_DEFAULT_RXGLOM_SIZE 32 /* max rx frames in glom chain */
59 static int brcmf_sdiod_txglomsz = BRCMF_DEFAULT_TXGLOM_SIZE;
60 module_param_named(txglomsz, brcmf_sdiod_txglomsz, int, 0);
61 MODULE_PARM_DESC(txglomsz, "maximum tx packet chain size [SDIO]");
63 static irqreturn_t brcmf_sdiod_oob_irqhandler(int irq, void *dev_id)
65 struct brcmf_bus *bus_if = dev_get_drvdata(dev_id);
66 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
68 brcmf_dbg(INTR, "OOB intr triggered\n");
70 /* out-of-band interrupt is level-triggered which won't
71 * be cleared until dpc
73 if (sdiodev->irq_en) {
74 disable_irq_nosync(irq);
75 sdiodev->irq_en = false;
78 brcmf_sdio_isr(sdiodev->bus);
83 static void brcmf_sdiod_ib_irqhandler(struct sdio_func *func)
85 struct brcmf_bus *bus_if = dev_get_drvdata(&func->dev);
86 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
88 brcmf_dbg(INTR, "IB intr triggered\n");
90 brcmf_sdio_isr(sdiodev->bus);
93 /* dummy handler for SDIO function 2 interrupt */
94 static void brcmf_sdiod_dummy_irqhandler(struct sdio_func *func)
98 static bool brcmf_sdiod_pm_resume_error(struct brcmf_sdio_dev *sdiodev)
101 #ifdef CONFIG_PM_SLEEP
102 is_err = atomic_read(&sdiodev->suspend);
107 static void brcmf_sdiod_pm_resume_wait(struct brcmf_sdio_dev *sdiodev,
108 wait_queue_head_t *wq)
110 #ifdef CONFIG_PM_SLEEP
112 while (atomic_read(&sdiodev->suspend) && retry++ != 30)
113 wait_event_timeout(*wq, false, HZ/100);
117 int brcmf_sdiod_intr_register(struct brcmf_sdio_dev *sdiodev)
123 if ((sdiodev->pdata) && (sdiodev->pdata->oob_irq_supported)) {
124 brcmf_dbg(SDIO, "Enter, register OOB IRQ %d\n",
125 sdiodev->pdata->oob_irq_nr);
126 ret = request_irq(sdiodev->pdata->oob_irq_nr,
127 brcmf_sdiod_oob_irqhandler,
128 sdiodev->pdata->oob_irq_flags,
130 &sdiodev->func[1]->dev);
132 brcmf_err("request_irq failed %d\n", ret);
135 sdiodev->oob_irq_requested = true;
136 spin_lock_init(&sdiodev->irq_en_lock);
137 spin_lock_irqsave(&sdiodev->irq_en_lock, flags);
138 sdiodev->irq_en = true;
139 spin_unlock_irqrestore(&sdiodev->irq_en_lock, flags);
141 ret = enable_irq_wake(sdiodev->pdata->oob_irq_nr);
143 brcmf_err("enable_irq_wake failed %d\n", ret);
146 sdiodev->irq_wake = true;
148 sdio_claim_host(sdiodev->func[1]);
150 /* must configure SDIO_CCCR_IENx to enable irq */
151 data = brcmf_sdiod_regrb(sdiodev, SDIO_CCCR_IENx, &ret);
152 data |= 1 << SDIO_FUNC_1 | 1 << SDIO_FUNC_2 | 1;
153 brcmf_sdiod_regwb(sdiodev, SDIO_CCCR_IENx, data, &ret);
155 /* redirect, configure and enable io for interrupt signal */
156 data = SDIO_SEPINT_MASK | SDIO_SEPINT_OE;
157 if (sdiodev->pdata->oob_irq_flags & IRQF_TRIGGER_HIGH)
158 data |= SDIO_SEPINT_ACT_HI;
159 brcmf_sdiod_regwb(sdiodev, SDIO_CCCR_BRCM_SEPINT, data, &ret);
161 sdio_release_host(sdiodev->func[1]);
163 brcmf_dbg(SDIO, "Entering\n");
164 sdio_claim_host(sdiodev->func[1]);
165 sdio_claim_irq(sdiodev->func[1], brcmf_sdiod_ib_irqhandler);
166 sdio_claim_irq(sdiodev->func[2], brcmf_sdiod_dummy_irqhandler);
167 sdio_release_host(sdiodev->func[1]);
173 int brcmf_sdiod_intr_unregister(struct brcmf_sdio_dev *sdiodev)
175 brcmf_dbg(SDIO, "Entering\n");
177 if ((sdiodev->pdata) && (sdiodev->pdata->oob_irq_supported)) {
178 sdio_claim_host(sdiodev->func[1]);
179 brcmf_sdiod_regwb(sdiodev, SDIO_CCCR_BRCM_SEPINT, 0, NULL);
180 brcmf_sdiod_regwb(sdiodev, SDIO_CCCR_IENx, 0, NULL);
181 sdio_release_host(sdiodev->func[1]);
183 if (sdiodev->oob_irq_requested) {
184 sdiodev->oob_irq_requested = false;
185 if (sdiodev->irq_wake) {
186 disable_irq_wake(sdiodev->pdata->oob_irq_nr);
187 sdiodev->irq_wake = false;
189 free_irq(sdiodev->pdata->oob_irq_nr,
190 &sdiodev->func[1]->dev);
191 sdiodev->irq_en = false;
194 sdio_claim_host(sdiodev->func[1]);
195 sdio_release_irq(sdiodev->func[2]);
196 sdio_release_irq(sdiodev->func[1]);
197 sdio_release_host(sdiodev->func[1]);
203 static inline int brcmf_sdiod_f0_writeb(struct sdio_func *func,
204 uint regaddr, u8 byte)
209 * Can only directly write to some F0 registers.
210 * Handle CCCR_IENx and CCCR_ABORT command
213 if ((regaddr == SDIO_CCCR_ABORT) ||
214 (regaddr == SDIO_CCCR_IENx))
215 sdio_writeb(func, byte, regaddr, &err_ret);
217 sdio_f0_writeb(func, byte, regaddr, &err_ret);
222 static int brcmf_sdiod_request_data(struct brcmf_sdio_dev *sdiodev, u8 fn,
223 u32 addr, u8 regsz, void *data, bool write)
225 struct sdio_func *func;
228 brcmf_dbg(SDIO, "rw=%d, func=%d, addr=0x%05x, nbytes=%d\n",
229 write, fn, addr, regsz);
231 brcmf_sdiod_pm_resume_wait(sdiodev, &sdiodev->request_word_wait);
232 if (brcmf_sdiod_pm_resume_error(sdiodev))
235 /* only allow byte access on F0 */
236 if (WARN_ON(regsz > 1 && !fn))
238 func = sdiodev->func[fn];
244 sdio_writeb(func, *(u8 *)data, addr, &ret);
246 ret = brcmf_sdiod_f0_writeb(func, addr,
250 *(u8 *)data = sdio_readb(func, addr, &ret);
252 *(u8 *)data = sdio_f0_readb(func, addr, &ret);
257 sdio_writew(func, *(u16 *)data, addr, &ret);
259 *(u16 *)data = sdio_readw(func, addr, &ret);
263 sdio_writel(func, *(u32 *)data, addr, &ret);
265 *(u32 *)data = sdio_readl(func, addr, &ret);
268 brcmf_err("invalid size: %d\n", regsz);
274 * SleepCSR register access can fail when
275 * waking up the device so reduce this noise
278 if (addr != SBSDIO_FUNC1_SLEEPCSR)
279 brcmf_err("failed to %s data F%d@0x%05x, err: %d\n",
280 write ? "write" : "read", fn, addr, ret);
282 brcmf_dbg(SDIO, "failed to %s data F%d@0x%05x, err: %d\n",
283 write ? "write" : "read", fn, addr, ret);
288 static int brcmf_sdiod_regrw_helper(struct brcmf_sdio_dev *sdiodev, u32 addr,
289 u8 regsz, void *data, bool write)
295 if (sdiodev->bus_if->state == BRCMF_BUS_NOMEDIUM)
299 * figure out how to read the register based on address range
300 * 0x00 ~ 0x7FF: function 0 CCCR and FBR
301 * 0x10000 ~ 0x1FFFF: function 1 miscellaneous registers
302 * The rest: function 1 silicon backplane core registers
304 if ((addr & ~REG_F0_REG_MASK) == 0)
305 func_num = SDIO_FUNC_0;
307 func_num = SDIO_FUNC_1;
311 memset(data, 0, regsz);
312 /* for retry wait for 1 ms till bus get settled down */
314 usleep_range(1000, 2000);
315 ret = brcmf_sdiod_request_data(sdiodev, func_num, addr, regsz,
317 } while (ret != 0 && ret != -ENOMEDIUM &&
318 retry++ < SDIOH_API_ACCESS_RETRY_LIMIT);
320 if (ret == -ENOMEDIUM)
321 brcmf_bus_change_state(sdiodev->bus_if, BRCMF_BUS_NOMEDIUM);
323 brcmf_err("failed with %d\n", ret);
329 brcmf_sdiod_set_sbaddr_window(struct brcmf_sdio_dev *sdiodev, u32 address)
334 if (sdiodev->bus_if->state == BRCMF_BUS_NOMEDIUM)
337 addr[0] = (address >> 8) & SBSDIO_SBADDRLOW_MASK;
338 addr[1] = (address >> 16) & SBSDIO_SBADDRMID_MASK;
339 addr[2] = (address >> 24) & SBSDIO_SBADDRHIGH_MASK;
341 for (i = 0; i < 3; i++) {
342 err = brcmf_sdiod_regrw_helper(sdiodev,
343 SBSDIO_FUNC1_SBADDRLOW + i,
344 sizeof(u8), &addr[i], true);
346 brcmf_err("failed at addr: 0x%0x\n",
347 SBSDIO_FUNC1_SBADDRLOW + i);
356 brcmf_sdiod_addrprep(struct brcmf_sdio_dev *sdiodev, uint width, u32 *addr)
358 uint bar0 = *addr & ~SBSDIO_SB_OFT_ADDR_MASK;
361 if (bar0 != sdiodev->sbwad) {
362 err = brcmf_sdiod_set_sbaddr_window(sdiodev, bar0);
366 sdiodev->sbwad = bar0;
369 *addr &= SBSDIO_SB_OFT_ADDR_MASK;
372 *addr |= SBSDIO_SB_ACCESS_2_4B_FLAG;
377 u8 brcmf_sdiod_regrb(struct brcmf_sdio_dev *sdiodev, u32 addr, int *ret)
382 brcmf_dbg(SDIO, "addr:0x%08x\n", addr);
383 retval = brcmf_sdiod_regrw_helper(sdiodev, addr, sizeof(data), &data,
385 brcmf_dbg(SDIO, "data:0x%02x\n", data);
393 u32 brcmf_sdiod_regrl(struct brcmf_sdio_dev *sdiodev, u32 addr, int *ret)
398 brcmf_dbg(SDIO, "addr:0x%08x\n", addr);
399 retval = brcmf_sdiod_addrprep(sdiodev, sizeof(data), &addr);
402 retval = brcmf_sdiod_regrw_helper(sdiodev, addr, sizeof(data), &data,
404 brcmf_dbg(SDIO, "data:0x%08x\n", data);
413 void brcmf_sdiod_regwb(struct brcmf_sdio_dev *sdiodev, u32 addr,
418 brcmf_dbg(SDIO, "addr:0x%08x, data:0x%02x\n", addr, data);
419 retval = brcmf_sdiod_regrw_helper(sdiodev, addr, sizeof(data), &data,
425 void brcmf_sdiod_regwl(struct brcmf_sdio_dev *sdiodev, u32 addr,
430 brcmf_dbg(SDIO, "addr:0x%08x, data:0x%08x\n", addr, data);
431 retval = brcmf_sdiod_addrprep(sdiodev, sizeof(data), &addr);
434 retval = brcmf_sdiod_regrw_helper(sdiodev, addr, sizeof(data), &data,
442 static int brcmf_sdiod_buffrw(struct brcmf_sdio_dev *sdiodev, uint fn,
443 bool write, u32 addr, struct sk_buff *pkt)
448 brcmf_sdiod_pm_resume_wait(sdiodev, &sdiodev->request_buffer_wait);
449 if (brcmf_sdiod_pm_resume_error(sdiodev))
452 /* Single skb use the standard mmc interface */
453 req_sz = pkt->len + 3;
457 err = sdio_memcpy_toio(sdiodev->func[fn], addr,
458 ((u8 *)(pkt->data)), req_sz);
460 err = sdio_memcpy_fromio(sdiodev->func[fn], ((u8 *)(pkt->data)),
463 /* function 2 read is FIFO operation */
464 err = sdio_readsb(sdiodev->func[fn], ((u8 *)(pkt->data)), addr,
466 if (err == -ENOMEDIUM)
467 brcmf_bus_change_state(sdiodev->bus_if, BRCMF_BUS_NOMEDIUM);
472 * brcmf_sdiod_sglist_rw - SDIO interface function for block data access
473 * @sdiodev: brcmfmac sdio device
474 * @fn: SDIO function number
475 * @write: direction flag
476 * @addr: dongle memory address as source/destination
479 * This function takes the respbonsibility as the interface function to MMC
480 * stack for block data access. It assumes that the skb passed down by the
481 * caller has already been padded and aligned.
483 static int brcmf_sdiod_sglist_rw(struct brcmf_sdio_dev *sdiodev, uint fn,
484 bool write, u32 addr,
485 struct sk_buff_head *pktlist)
487 unsigned int req_sz, func_blk_sz, sg_cnt, sg_data_sz, pkt_offset;
488 unsigned int max_req_sz, orig_offset, dst_offset;
489 unsigned short max_seg_cnt, seg_sz;
490 unsigned char *pkt_data, *orig_data, *dst_data;
491 struct sk_buff *pkt_next = NULL, *local_pkt_next;
492 struct sk_buff_head local_list, *target_list;
493 struct mmc_request mmc_req;
494 struct mmc_command mmc_cmd;
495 struct mmc_data mmc_dat;
496 struct scatterlist *sgl;
502 brcmf_sdiod_pm_resume_wait(sdiodev, &sdiodev->request_buffer_wait);
503 if (brcmf_sdiod_pm_resume_error(sdiodev))
506 target_list = pktlist;
507 /* for host with broken sg support, prepare a page aligned list */
508 __skb_queue_head_init(&local_list);
509 if (sdiodev->pdata && sdiodev->pdata->broken_sg_support && !write) {
511 skb_queue_walk(pktlist, pkt_next)
512 req_sz += pkt_next->len;
513 req_sz = ALIGN(req_sz, sdiodev->func[fn]->cur_blksize);
514 while (req_sz > PAGE_SIZE) {
515 pkt_next = brcmu_pkt_buf_get_skb(PAGE_SIZE);
516 if (pkt_next == NULL) {
520 __skb_queue_tail(&local_list, pkt_next);
523 pkt_next = brcmu_pkt_buf_get_skb(req_sz);
524 if (pkt_next == NULL) {
528 __skb_queue_tail(&local_list, pkt_next);
529 target_list = &local_list;
532 func_blk_sz = sdiodev->func[fn]->cur_blksize;
533 max_req_sz = sdiodev->max_request_size;
534 max_seg_cnt = min_t(unsigned short, sdiodev->max_segment_count,
536 seg_sz = target_list->qlen;
538 pkt_next = target_list->next;
540 memset(&mmc_req, 0, sizeof(struct mmc_request));
541 memset(&mmc_cmd, 0, sizeof(struct mmc_command));
542 memset(&mmc_dat, 0, sizeof(struct mmc_data));
544 mmc_dat.sg = sdiodev->sgtable.sgl;
545 mmc_dat.blksz = func_blk_sz;
546 mmc_dat.flags = write ? MMC_DATA_WRITE : MMC_DATA_READ;
547 mmc_cmd.opcode = SD_IO_RW_EXTENDED;
548 mmc_cmd.arg = write ? 1<<31 : 0; /* write flag */
549 mmc_cmd.arg |= (fn & 0x7) << 28; /* SDIO func num */
550 mmc_cmd.arg |= 1<<27; /* block mode */
551 /* for function 1 the addr will be incremented */
552 mmc_cmd.arg |= (fn == 1) ? 1<<26 : 0;
553 mmc_cmd.flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_ADTC;
554 mmc_req.cmd = &mmc_cmd;
555 mmc_req.data = &mmc_dat;
560 sgl = sdiodev->sgtable.sgl;
562 while (pkt_next != (struct sk_buff *)target_list) {
563 pkt_data = pkt_next->data + pkt_offset;
564 sg_data_sz = pkt_next->len - pkt_offset;
565 if (sg_data_sz > sdiodev->max_segment_size)
566 sg_data_sz = sdiodev->max_segment_size;
567 if (sg_data_sz > max_req_sz - req_sz)
568 sg_data_sz = max_req_sz - req_sz;
570 sg_set_buf(sgl, pkt_data, sg_data_sz);
574 req_sz += sg_data_sz;
575 pkt_offset += sg_data_sz;
576 if (pkt_offset == pkt_next->len) {
578 pkt_next = pkt_next->next;
581 if (req_sz >= max_req_sz || sg_cnt >= max_seg_cnt)
586 if (req_sz % func_blk_sz != 0) {
587 brcmf_err("sg request length %u is not %u aligned\n",
588 req_sz, func_blk_sz);
593 mmc_dat.sg_len = sg_cnt;
594 mmc_dat.blocks = req_sz / func_blk_sz;
595 mmc_cmd.arg |= (addr & 0x1FFFF) << 9; /* address */
596 mmc_cmd.arg |= mmc_dat.blocks & 0x1FF; /* block count */
597 /* incrementing addr for function 1 */
601 mmc_set_data_timeout(&mmc_dat, sdiodev->func[fn]->card);
602 mmc_wait_for_req(sdiodev->func[fn]->card->host, &mmc_req);
604 ret = mmc_cmd.error ? mmc_cmd.error : mmc_dat.error;
605 if (ret == -ENOMEDIUM) {
606 brcmf_bus_change_state(sdiodev->bus_if,
609 } else if (ret != 0) {
610 brcmf_err("CMD53 sg block %s failed %d\n",
611 write ? "write" : "read", ret);
617 if (sdiodev->pdata && sdiodev->pdata->broken_sg_support && !write) {
618 local_pkt_next = local_list.next;
620 skb_queue_walk(pktlist, pkt_next) {
623 req_sz = local_pkt_next->len - orig_offset;
624 req_sz = min_t(uint, pkt_next->len - dst_offset,
626 orig_data = local_pkt_next->data + orig_offset;
627 dst_data = pkt_next->data + dst_offset;
628 memcpy(dst_data, orig_data, req_sz);
629 orig_offset += req_sz;
630 dst_offset += req_sz;
631 if (orig_offset == local_pkt_next->len) {
633 local_pkt_next = local_pkt_next->next;
635 if (dst_offset == pkt_next->len)
637 } while (!skb_queue_empty(&local_list));
642 sg_init_table(sdiodev->sgtable.sgl, sdiodev->sgtable.orig_nents);
643 while ((pkt_next = __skb_dequeue(&local_list)) != NULL)
644 brcmu_pkt_buf_free_skb(pkt_next);
649 int brcmf_sdiod_recv_buf(struct brcmf_sdio_dev *sdiodev, u8 *buf, uint nbytes)
651 struct sk_buff *mypkt;
654 mypkt = brcmu_pkt_buf_get_skb(nbytes);
656 brcmf_err("brcmu_pkt_buf_get_skb failed: len %d\n",
661 err = brcmf_sdiod_recv_pkt(sdiodev, mypkt);
663 memcpy(buf, mypkt->data, nbytes);
665 brcmu_pkt_buf_free_skb(mypkt);
669 int brcmf_sdiod_recv_pkt(struct brcmf_sdio_dev *sdiodev, struct sk_buff *pkt)
671 u32 addr = sdiodev->sbwad;
674 brcmf_dbg(SDIO, "addr = 0x%x, size = %d\n", addr, pkt->len);
676 err = brcmf_sdiod_addrprep(sdiodev, 4, &addr);
680 err = brcmf_sdiod_buffrw(sdiodev, SDIO_FUNC_2, false, addr, pkt);
686 int brcmf_sdiod_recv_chain(struct brcmf_sdio_dev *sdiodev,
687 struct sk_buff_head *pktq, uint totlen)
689 struct sk_buff *glom_skb;
691 u32 addr = sdiodev->sbwad;
694 brcmf_dbg(SDIO, "addr = 0x%x, size = %d\n",
697 err = brcmf_sdiod_addrprep(sdiodev, 4, &addr);
702 err = brcmf_sdiod_buffrw(sdiodev, SDIO_FUNC_2, false, addr,
704 else if (!sdiodev->sg_support) {
705 glom_skb = brcmu_pkt_buf_get_skb(totlen);
708 err = brcmf_sdiod_buffrw(sdiodev, SDIO_FUNC_2, false, addr,
713 skb_queue_walk(pktq, skb) {
714 memcpy(skb->data, glom_skb->data, skb->len);
715 skb_pull(glom_skb, skb->len);
718 err = brcmf_sdiod_sglist_rw(sdiodev, SDIO_FUNC_2, false, addr,
725 int brcmf_sdiod_send_buf(struct brcmf_sdio_dev *sdiodev, u8 *buf, uint nbytes)
727 struct sk_buff *mypkt;
728 u32 addr = sdiodev->sbwad;
731 mypkt = brcmu_pkt_buf_get_skb(nbytes);
733 brcmf_err("brcmu_pkt_buf_get_skb failed: len %d\n",
738 memcpy(mypkt->data, buf, nbytes);
740 err = brcmf_sdiod_addrprep(sdiodev, 4, &addr);
743 err = brcmf_sdiod_buffrw(sdiodev, SDIO_FUNC_2, true, addr,
746 brcmu_pkt_buf_free_skb(mypkt);
751 int brcmf_sdiod_send_pkt(struct brcmf_sdio_dev *sdiodev,
752 struct sk_buff_head *pktq)
755 u32 addr = sdiodev->sbwad;
758 brcmf_dbg(SDIO, "addr = 0x%x, size = %d\n", addr, pktq->qlen);
760 err = brcmf_sdiod_addrprep(sdiodev, 4, &addr);
764 if (pktq->qlen == 1 || !sdiodev->sg_support)
765 skb_queue_walk(pktq, skb) {
766 err = brcmf_sdiod_buffrw(sdiodev, SDIO_FUNC_2, true,
772 err = brcmf_sdiod_sglist_rw(sdiodev, SDIO_FUNC_2, true, addr,
779 brcmf_sdiod_ramrw(struct brcmf_sdio_dev *sdiodev, bool write, u32 address,
787 dsize = min_t(uint, SBSDIO_SB_OFT_ADDR_LIMIT, size);
788 pkt = dev_alloc_skb(dsize);
790 brcmf_err("dev_alloc_skb failed: len %d\n", dsize);
795 /* Determine initial transfer parameters */
796 sdaddr = address & SBSDIO_SB_OFT_ADDR_MASK;
797 if ((sdaddr + size) & SBSDIO_SBWINDOW_MASK)
798 dsize = (SBSDIO_SB_OFT_ADDR_LIMIT - sdaddr);
802 sdio_claim_host(sdiodev->func[1]);
804 /* Do the transfer(s) */
806 /* Set the backplane window to include the start address */
807 bcmerror = brcmf_sdiod_set_sbaddr_window(sdiodev, address);
811 brcmf_dbg(SDIO, "%s %d bytes at offset 0x%08x in window 0x%08x\n",
812 write ? "write" : "read", dsize,
813 sdaddr, address & SBSDIO_SBWINDOW_MASK);
815 sdaddr &= SBSDIO_SB_OFT_ADDR_MASK;
816 sdaddr |= SBSDIO_SB_ACCESS_2_4B_FLAG;
820 memcpy(pkt->data, data, dsize);
821 bcmerror = brcmf_sdiod_buffrw(sdiodev, SDIO_FUNC_1, write,
824 brcmf_err("membytes transfer failed\n");
828 memcpy(data, pkt->data, dsize);
831 /* Adjust for next transfer (if any) */
837 dsize = min_t(uint, SBSDIO_SB_OFT_ADDR_LIMIT, size);
843 /* Return the window to backplane enumeration space for core access */
844 if (brcmf_sdiod_set_sbaddr_window(sdiodev, sdiodev->sbwad))
845 brcmf_err("FAILED to set window back to 0x%x\n",
848 sdio_release_host(sdiodev->func[1]);
853 int brcmf_sdiod_abort(struct brcmf_sdio_dev *sdiodev, uint fn)
855 char t_func = (char)fn;
856 brcmf_dbg(SDIO, "Enter\n");
858 /* issue abort cmd52 command through F0 */
859 brcmf_sdiod_request_data(sdiodev, SDIO_FUNC_0, SDIO_CCCR_ABORT,
860 sizeof(t_func), &t_func, true);
862 brcmf_dbg(SDIO, "Exit\n");
866 static void brcmf_sdiod_sgtable_alloc(struct brcmf_sdio_dev *sdiodev)
871 if (!sdiodev->sg_support)
874 nents = max_t(uint, BRCMF_DEFAULT_RXGLOM_SIZE, brcmf_sdiod_txglomsz);
875 nents += (nents >> 4) + 1;
877 WARN_ON(nents > sdiodev->max_segment_count);
879 brcmf_dbg(TRACE, "nents=%d\n", nents);
880 err = sg_alloc_table(&sdiodev->sgtable, nents, GFP_KERNEL);
882 brcmf_err("allocation failed: disable scatter-gather");
883 sdiodev->sg_support = false;
886 sdiodev->txglomsz = brcmf_sdiod_txglomsz;
889 static int brcmf_sdiod_remove(struct brcmf_sdio_dev *sdiodev)
892 brcmf_sdio_remove(sdiodev->bus);
896 /* Disable Function 2 */
897 sdio_claim_host(sdiodev->func[2]);
898 sdio_disable_func(sdiodev->func[2]);
899 sdio_release_host(sdiodev->func[2]);
901 /* Disable Function 1 */
902 sdio_claim_host(sdiodev->func[1]);
903 sdio_disable_func(sdiodev->func[1]);
904 sdio_release_host(sdiodev->func[1]);
906 sg_free_table(&sdiodev->sgtable);
912 static int brcmf_sdiod_probe(struct brcmf_sdio_dev *sdiodev)
914 struct sdio_func *func;
915 struct mmc_host *host;
919 sdiodev->num_funcs = 2;
921 sdio_claim_host(sdiodev->func[1]);
923 ret = sdio_set_block_size(sdiodev->func[1], SDIO_FUNC1_BLOCKSIZE);
925 brcmf_err("Failed to set F1 blocksize\n");
926 sdio_release_host(sdiodev->func[1]);
929 ret = sdio_set_block_size(sdiodev->func[2], SDIO_FUNC2_BLOCKSIZE);
931 brcmf_err("Failed to set F2 blocksize\n");
932 sdio_release_host(sdiodev->func[1]);
936 /* increase F2 timeout */
937 sdiodev->func[2]->enable_timeout = SDIO_WAIT_F2RDY;
939 /* Enable Function 1 */
940 ret = sdio_enable_func(sdiodev->func[1]);
941 sdio_release_host(sdiodev->func[1]);
943 brcmf_err("Failed to enable F1: err=%d\n", ret);
948 * determine host related variables after brcmf_sdiod_probe()
949 * as func->cur_blksize is properly set and F2 init has been
950 * completed successfully.
952 func = sdiodev->func[2];
953 host = func->card->host;
954 sdiodev->sg_support = host->max_segs > 1;
955 max_blocks = min_t(uint, host->max_blk_count, 511u);
956 sdiodev->max_request_size = min_t(uint, host->max_req_size,
957 max_blocks * func->cur_blksize);
958 sdiodev->max_segment_count = min_t(uint, host->max_segs,
959 SG_MAX_SINGLE_ALLOC);
960 sdiodev->max_segment_size = host->max_seg_size;
962 /* allocate scatter-gather table. sg support
963 * will be disabled upon allocation failure.
965 brcmf_sdiod_sgtable_alloc(sdiodev);
967 /* try to attach to the target device */
968 sdiodev->bus = brcmf_sdio_probe(sdiodev);
976 brcmf_sdiod_remove(sdiodev);
981 /* devices we support, null terminated */
982 static const struct sdio_device_id brcmf_sdmmc_ids[] = {
983 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_43143)},
984 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_43241)},
985 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4329)},
986 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4330)},
987 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4334)},
988 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_43362)},
989 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM,
990 SDIO_DEVICE_ID_BROADCOM_4335_4339)},
991 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4354)},
992 { /* end: all zeroes */ },
994 MODULE_DEVICE_TABLE(sdio, brcmf_sdmmc_ids);
996 static struct brcmfmac_sdio_platform_data *brcmfmac_sdio_pdata;
999 static int brcmf_ops_sdio_probe(struct sdio_func *func,
1000 const struct sdio_device_id *id)
1003 struct brcmf_sdio_dev *sdiodev;
1004 struct brcmf_bus *bus_if;
1006 brcmf_dbg(SDIO, "Enter\n");
1007 brcmf_dbg(SDIO, "Class=%x\n", func->class);
1008 brcmf_dbg(SDIO, "sdio vendor ID: 0x%04x\n", func->vendor);
1009 brcmf_dbg(SDIO, "sdio device ID: 0x%04x\n", func->device);
1010 brcmf_dbg(SDIO, "Function#: %d\n", func->num);
1012 /* Consume func num 1 but dont do anything with it. */
1016 /* Ignore anything but func 2 */
1020 bus_if = kzalloc(sizeof(struct brcmf_bus), GFP_KERNEL);
1023 sdiodev = kzalloc(sizeof(struct brcmf_sdio_dev), GFP_KERNEL);
1029 /* store refs to functions used. mmc_card does
1030 * not hold the F0 function pointer.
1032 sdiodev->func[0] = kmemdup(func, sizeof(*func), GFP_KERNEL);
1033 sdiodev->func[0]->num = 0;
1034 sdiodev->func[1] = func->card->sdio_func[0];
1035 sdiodev->func[2] = func;
1037 sdiodev->bus_if = bus_if;
1038 bus_if->bus_priv.sdio = sdiodev;
1039 bus_if->proto_type = BRCMF_PROTO_BCDC;
1040 dev_set_drvdata(&func->dev, bus_if);
1041 dev_set_drvdata(&sdiodev->func[1]->dev, bus_if);
1042 sdiodev->dev = &sdiodev->func[1]->dev;
1043 sdiodev->pdata = brcmfmac_sdio_pdata;
1045 atomic_set(&sdiodev->suspend, false);
1046 init_waitqueue_head(&sdiodev->request_word_wait);
1047 init_waitqueue_head(&sdiodev->request_buffer_wait);
1049 brcmf_dbg(SDIO, "F2 found, calling brcmf_sdiod_probe...\n");
1050 err = brcmf_sdiod_probe(sdiodev);
1052 brcmf_err("F2 error, probe failed %d...\n", err);
1056 brcmf_dbg(SDIO, "F2 init completed...\n");
1060 dev_set_drvdata(&func->dev, NULL);
1061 dev_set_drvdata(&sdiodev->func[1]->dev, NULL);
1062 kfree(sdiodev->func[0]);
1068 static void brcmf_ops_sdio_remove(struct sdio_func *func)
1070 struct brcmf_bus *bus_if;
1071 struct brcmf_sdio_dev *sdiodev;
1073 brcmf_dbg(SDIO, "Enter\n");
1074 brcmf_dbg(SDIO, "sdio vendor ID: 0x%04x\n", func->vendor);
1075 brcmf_dbg(SDIO, "sdio device ID: 0x%04x\n", func->device);
1076 brcmf_dbg(SDIO, "Function: %d\n", func->num);
1078 if (func->num != 1 && func->num != 2)
1081 bus_if = dev_get_drvdata(&func->dev);
1083 sdiodev = bus_if->bus_priv.sdio;
1084 brcmf_sdiod_remove(sdiodev);
1086 dev_set_drvdata(&sdiodev->func[1]->dev, NULL);
1087 dev_set_drvdata(&sdiodev->func[2]->dev, NULL);
1090 kfree(sdiodev->func[0]);
1094 brcmf_dbg(SDIO, "Exit\n");
1097 #ifdef CONFIG_PM_SLEEP
1098 static int brcmf_ops_sdio_suspend(struct device *dev)
1100 mmc_pm_flag_t sdio_flags;
1101 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1102 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
1105 brcmf_dbg(SDIO, "Enter\n");
1107 sdio_flags = sdio_get_host_pm_caps(sdiodev->func[1]);
1108 if (!(sdio_flags & MMC_PM_KEEP_POWER)) {
1109 brcmf_err("Host can't keep power while suspended\n");
1113 atomic_set(&sdiodev->suspend, true);
1115 ret = sdio_set_host_pm_flags(sdiodev->func[1], MMC_PM_KEEP_POWER);
1117 brcmf_err("Failed to set pm_flags\n");
1118 atomic_set(&sdiodev->suspend, false);
1122 brcmf_sdio_wd_timer(sdiodev->bus, 0);
1127 static int brcmf_ops_sdio_resume(struct device *dev)
1129 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1130 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
1132 brcmf_dbg(SDIO, "Enter\n");
1133 brcmf_sdio_wd_timer(sdiodev->bus, BRCMF_WD_POLL_MS);
1134 atomic_set(&sdiodev->suspend, false);
1138 static const struct dev_pm_ops brcmf_sdio_pm_ops = {
1139 .suspend = brcmf_ops_sdio_suspend,
1140 .resume = brcmf_ops_sdio_resume,
1142 #endif /* CONFIG_PM_SLEEP */
1144 static struct sdio_driver brcmf_sdmmc_driver = {
1145 .probe = brcmf_ops_sdio_probe,
1146 .remove = brcmf_ops_sdio_remove,
1147 .name = BRCMFMAC_SDIO_PDATA_NAME,
1148 .id_table = brcmf_sdmmc_ids,
1150 .owner = THIS_MODULE,
1151 #ifdef CONFIG_PM_SLEEP
1152 .pm = &brcmf_sdio_pm_ops,
1153 #endif /* CONFIG_PM_SLEEP */
1157 static int __init brcmf_sdio_pd_probe(struct platform_device *pdev)
1159 brcmf_dbg(SDIO, "Enter\n");
1161 brcmfmac_sdio_pdata = dev_get_platdata(&pdev->dev);
1163 if (brcmfmac_sdio_pdata->power_on)
1164 brcmfmac_sdio_pdata->power_on();
1169 static int brcmf_sdio_pd_remove(struct platform_device *pdev)
1171 brcmf_dbg(SDIO, "Enter\n");
1173 if (brcmfmac_sdio_pdata->power_off)
1174 brcmfmac_sdio_pdata->power_off();
1176 sdio_unregister_driver(&brcmf_sdmmc_driver);
1181 static struct platform_driver brcmf_sdio_pd = {
1182 .remove = brcmf_sdio_pd_remove,
1184 .name = BRCMFMAC_SDIO_PDATA_NAME,
1185 .owner = THIS_MODULE,
1189 void brcmf_sdio_register(void)
1193 ret = sdio_register_driver(&brcmf_sdmmc_driver);
1195 brcmf_err("sdio_register_driver failed: %d\n", ret);
1198 void brcmf_sdio_exit(void)
1200 brcmf_dbg(SDIO, "Enter\n");
1202 if (brcmfmac_sdio_pdata)
1203 platform_driver_unregister(&brcmf_sdio_pd);
1205 sdio_unregister_driver(&brcmf_sdmmc_driver);
1208 void __init brcmf_sdio_init(void)
1212 brcmf_dbg(SDIO, "Enter\n");
1214 ret = platform_driver_probe(&brcmf_sdio_pd, brcmf_sdio_pd_probe);
1216 brcmf_dbg(SDIO, "No platform data available.\n");