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/card.h>
29 #include <linux/mmc/host.h>
30 #include <linux/platform_device.h>
31 #include <linux/platform_data/brcmfmac-sdio.h>
32 #include <linux/suspend.h>
33 #include <linux/errno.h>
34 #include <linux/module.h>
35 #include <net/cfg80211.h>
38 #include <brcm_hw_ids.h>
39 #include <brcmu_utils.h>
40 #include <brcmu_wifi.h>
41 #include <chipcommon.h>
46 #include "sdio_host.h"
49 #define SDIOH_API_ACCESS_RETRY_LIMIT 2
51 #define DMA_ALIGN_MASK 0x03
53 #define SDIO_FUNC1_BLOCKSIZE 64
54 #define SDIO_FUNC2_BLOCKSIZE 512
55 /* Maximum milliseconds to wait for F2 to come up */
56 #define SDIO_WAIT_F2RDY 3000
58 #define BRCMF_DEFAULT_TXGLOM_SIZE 32 /* max tx frames in glom chain */
59 #define BRCMF_DEFAULT_RXGLOM_SIZE 32 /* max rx frames in glom chain */
61 static int brcmf_sdiod_txglomsz = BRCMF_DEFAULT_TXGLOM_SIZE;
62 module_param_named(txglomsz, brcmf_sdiod_txglomsz, int, 0);
63 MODULE_PARM_DESC(txglomsz, "maximum tx packet chain size [SDIO]");
65 static irqreturn_t brcmf_sdiod_oob_irqhandler(int irq, void *dev_id)
67 struct brcmf_bus *bus_if = dev_get_drvdata(dev_id);
68 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
70 brcmf_dbg(INTR, "OOB intr triggered\n");
72 /* out-of-band interrupt is level-triggered which won't
73 * be cleared until dpc
75 if (sdiodev->irq_en) {
76 disable_irq_nosync(irq);
77 sdiodev->irq_en = false;
80 brcmf_sdio_isr(sdiodev->bus);
85 static void brcmf_sdiod_ib_irqhandler(struct sdio_func *func)
87 struct brcmf_bus *bus_if = dev_get_drvdata(&func->dev);
88 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
90 brcmf_dbg(INTR, "IB intr triggered\n");
92 brcmf_sdio_isr(sdiodev->bus);
95 /* dummy handler for SDIO function 2 interrupt */
96 static void brcmf_sdiod_dummy_irqhandler(struct sdio_func *func)
100 static bool brcmf_sdiod_pm_resume_error(struct brcmf_sdio_dev *sdiodev)
103 #ifdef CONFIG_PM_SLEEP
104 is_err = atomic_read(&sdiodev->suspend);
109 static void brcmf_sdiod_pm_resume_wait(struct brcmf_sdio_dev *sdiodev,
110 wait_queue_head_t *wq)
112 #ifdef CONFIG_PM_SLEEP
114 while (atomic_read(&sdiodev->suspend) && retry++ != 30)
115 wait_event_timeout(*wq, false, HZ/100);
119 int brcmf_sdiod_intr_register(struct brcmf_sdio_dev *sdiodev)
123 u32 addr, gpiocontrol;
126 if ((sdiodev->pdata) && (sdiodev->pdata->oob_irq_supported)) {
127 brcmf_dbg(SDIO, "Enter, register OOB IRQ %d\n",
128 sdiodev->pdata->oob_irq_nr);
129 ret = request_irq(sdiodev->pdata->oob_irq_nr,
130 brcmf_sdiod_oob_irqhandler,
131 sdiodev->pdata->oob_irq_flags,
133 &sdiodev->func[1]->dev);
135 brcmf_err("request_irq failed %d\n", ret);
138 sdiodev->oob_irq_requested = true;
139 spin_lock_init(&sdiodev->irq_en_lock);
140 spin_lock_irqsave(&sdiodev->irq_en_lock, flags);
141 sdiodev->irq_en = true;
142 spin_unlock_irqrestore(&sdiodev->irq_en_lock, flags);
144 ret = enable_irq_wake(sdiodev->pdata->oob_irq_nr);
146 brcmf_err("enable_irq_wake failed %d\n", ret);
149 sdiodev->irq_wake = true;
151 sdio_claim_host(sdiodev->func[1]);
153 if (sdiodev->bus_if->chip == BRCM_CC_43362_CHIP_ID) {
154 /* assign GPIO to SDIO core */
155 addr = CORE_CC_REG(SI_ENUM_BASE, gpiocontrol);
156 gpiocontrol = brcmf_sdiod_regrl(sdiodev, addr, &ret);
158 brcmf_sdiod_regwl(sdiodev, addr, gpiocontrol, &ret);
160 brcmf_sdiod_regwb(sdiodev, SBSDIO_GPIO_SELECT, 0xf,
162 brcmf_sdiod_regwb(sdiodev, SBSDIO_GPIO_OUT, 0, &ret);
163 brcmf_sdiod_regwb(sdiodev, SBSDIO_GPIO_EN, 0x2, &ret);
166 /* must configure SDIO_CCCR_IENx to enable irq */
167 data = brcmf_sdiod_regrb(sdiodev, SDIO_CCCR_IENx, &ret);
168 data |= 1 << SDIO_FUNC_1 | 1 << SDIO_FUNC_2 | 1;
169 brcmf_sdiod_regwb(sdiodev, SDIO_CCCR_IENx, data, &ret);
171 /* redirect, configure and enable io for interrupt signal */
172 data = SDIO_SEPINT_MASK | SDIO_SEPINT_OE;
173 if (sdiodev->pdata->oob_irq_flags & IRQF_TRIGGER_HIGH)
174 data |= SDIO_SEPINT_ACT_HI;
175 brcmf_sdiod_regwb(sdiodev, SDIO_CCCR_BRCM_SEPINT, data, &ret);
177 sdio_release_host(sdiodev->func[1]);
179 brcmf_dbg(SDIO, "Entering\n");
180 sdio_claim_host(sdiodev->func[1]);
181 sdio_claim_irq(sdiodev->func[1], brcmf_sdiod_ib_irqhandler);
182 sdio_claim_irq(sdiodev->func[2], brcmf_sdiod_dummy_irqhandler);
183 sdio_release_host(sdiodev->func[1]);
189 int brcmf_sdiod_intr_unregister(struct brcmf_sdio_dev *sdiodev)
191 brcmf_dbg(SDIO, "Entering\n");
193 if ((sdiodev->pdata) && (sdiodev->pdata->oob_irq_supported)) {
194 sdio_claim_host(sdiodev->func[1]);
195 brcmf_sdiod_regwb(sdiodev, SDIO_CCCR_BRCM_SEPINT, 0, NULL);
196 brcmf_sdiod_regwb(sdiodev, SDIO_CCCR_IENx, 0, NULL);
197 sdio_release_host(sdiodev->func[1]);
199 if (sdiodev->oob_irq_requested) {
200 sdiodev->oob_irq_requested = false;
201 if (sdiodev->irq_wake) {
202 disable_irq_wake(sdiodev->pdata->oob_irq_nr);
203 sdiodev->irq_wake = false;
205 free_irq(sdiodev->pdata->oob_irq_nr,
206 &sdiodev->func[1]->dev);
207 sdiodev->irq_en = false;
210 sdio_claim_host(sdiodev->func[1]);
211 sdio_release_irq(sdiodev->func[2]);
212 sdio_release_irq(sdiodev->func[1]);
213 sdio_release_host(sdiodev->func[1]);
219 static inline int brcmf_sdiod_f0_writeb(struct sdio_func *func,
220 uint regaddr, u8 byte)
225 * Can only directly write to some F0 registers.
226 * Handle CCCR_IENx and CCCR_ABORT command
229 if ((regaddr == SDIO_CCCR_ABORT) ||
230 (regaddr == SDIO_CCCR_IENx))
231 sdio_writeb(func, byte, regaddr, &err_ret);
233 sdio_f0_writeb(func, byte, regaddr, &err_ret);
238 static int brcmf_sdiod_request_data(struct brcmf_sdio_dev *sdiodev, u8 fn,
239 u32 addr, u8 regsz, void *data, bool write)
241 struct sdio_func *func;
244 brcmf_dbg(SDIO, "rw=%d, func=%d, addr=0x%05x, nbytes=%d\n",
245 write, fn, addr, regsz);
247 brcmf_sdiod_pm_resume_wait(sdiodev, &sdiodev->request_word_wait);
248 if (brcmf_sdiod_pm_resume_error(sdiodev))
251 /* only allow byte access on F0 */
252 if (WARN_ON(regsz > 1 && !fn))
254 func = sdiodev->func[fn];
260 sdio_writeb(func, *(u8 *)data, addr, &ret);
262 ret = brcmf_sdiod_f0_writeb(func, addr,
266 *(u8 *)data = sdio_readb(func, addr, &ret);
268 *(u8 *)data = sdio_f0_readb(func, addr, &ret);
273 sdio_writew(func, *(u16 *)data, addr, &ret);
275 *(u16 *)data = sdio_readw(func, addr, &ret);
279 sdio_writel(func, *(u32 *)data, addr, &ret);
281 *(u32 *)data = sdio_readl(func, addr, &ret);
284 brcmf_err("invalid size: %d\n", regsz);
289 brcmf_dbg(SDIO, "failed to %s data F%d@0x%05x, err: %d\n",
290 write ? "write" : "read", fn, addr, ret);
295 static int brcmf_sdiod_regrw_helper(struct brcmf_sdio_dev *sdiodev, u32 addr,
296 u8 regsz, void *data, bool write)
302 if (sdiodev->bus_if->state == BRCMF_BUS_NOMEDIUM)
306 * figure out how to read the register based on address range
307 * 0x00 ~ 0x7FF: function 0 CCCR and FBR
308 * 0x10000 ~ 0x1FFFF: function 1 miscellaneous registers
309 * The rest: function 1 silicon backplane core registers
311 if ((addr & ~REG_F0_REG_MASK) == 0)
318 memset(data, 0, regsz);
319 /* for retry wait for 1 ms till bus get settled down */
321 usleep_range(1000, 2000);
322 ret = brcmf_sdiod_request_data(sdiodev, func, addr, regsz,
324 } while (ret != 0 && ret != -ENOMEDIUM &&
325 retry++ < SDIOH_API_ACCESS_RETRY_LIMIT);
327 if (ret == -ENOMEDIUM)
328 brcmf_bus_change_state(sdiodev->bus_if, BRCMF_BUS_NOMEDIUM);
331 * SleepCSR register access can fail when
332 * waking up the device so reduce this noise
335 if (addr != SBSDIO_FUNC1_SLEEPCSR)
336 brcmf_err("failed to %s data F%d@0x%05x, err: %d\n",
337 write ? "write" : "read", func, addr, ret);
339 brcmf_dbg(SDIO, "failed to %s data F%d@0x%05x, err: %d\n",
340 write ? "write" : "read", func, addr, ret);
346 brcmf_sdiod_set_sbaddr_window(struct brcmf_sdio_dev *sdiodev, u32 address)
351 if (sdiodev->bus_if->state == BRCMF_BUS_NOMEDIUM)
354 addr[0] = (address >> 8) & SBSDIO_SBADDRLOW_MASK;
355 addr[1] = (address >> 16) & SBSDIO_SBADDRMID_MASK;
356 addr[2] = (address >> 24) & SBSDIO_SBADDRHIGH_MASK;
358 for (i = 0; i < 3; i++) {
359 err = brcmf_sdiod_regrw_helper(sdiodev,
360 SBSDIO_FUNC1_SBADDRLOW + i,
361 sizeof(u8), &addr[i], true);
363 brcmf_err("failed at addr: 0x%0x\n",
364 SBSDIO_FUNC1_SBADDRLOW + i);
373 brcmf_sdiod_addrprep(struct brcmf_sdio_dev *sdiodev, uint width, u32 *addr)
375 uint bar0 = *addr & ~SBSDIO_SB_OFT_ADDR_MASK;
378 if (bar0 != sdiodev->sbwad) {
379 err = brcmf_sdiod_set_sbaddr_window(sdiodev, bar0);
383 sdiodev->sbwad = bar0;
386 *addr &= SBSDIO_SB_OFT_ADDR_MASK;
389 *addr |= SBSDIO_SB_ACCESS_2_4B_FLAG;
394 u8 brcmf_sdiod_regrb(struct brcmf_sdio_dev *sdiodev, u32 addr, int *ret)
399 brcmf_dbg(SDIO, "addr:0x%08x\n", addr);
400 retval = brcmf_sdiod_regrw_helper(sdiodev, addr, sizeof(data), &data,
402 brcmf_dbg(SDIO, "data:0x%02x\n", data);
410 u32 brcmf_sdiod_regrl(struct brcmf_sdio_dev *sdiodev, u32 addr, int *ret)
415 brcmf_dbg(SDIO, "addr:0x%08x\n", addr);
416 retval = brcmf_sdiod_addrprep(sdiodev, sizeof(data), &addr);
419 retval = brcmf_sdiod_regrw_helper(sdiodev, addr, sizeof(data), &data,
421 brcmf_dbg(SDIO, "data:0x%08x\n", data);
430 void brcmf_sdiod_regwb(struct brcmf_sdio_dev *sdiodev, u32 addr,
435 brcmf_dbg(SDIO, "addr:0x%08x, data:0x%02x\n", addr, data);
436 retval = brcmf_sdiod_regrw_helper(sdiodev, addr, sizeof(data), &data,
442 void brcmf_sdiod_regwl(struct brcmf_sdio_dev *sdiodev, u32 addr,
447 brcmf_dbg(SDIO, "addr:0x%08x, data:0x%08x\n", addr, data);
448 retval = brcmf_sdiod_addrprep(sdiodev, sizeof(data), &addr);
451 retval = brcmf_sdiod_regrw_helper(sdiodev, addr, sizeof(data), &data,
459 static int brcmf_sdiod_buffrw(struct brcmf_sdio_dev *sdiodev, uint fn,
460 bool write, u32 addr, struct sk_buff *pkt)
465 brcmf_sdiod_pm_resume_wait(sdiodev, &sdiodev->request_buffer_wait);
466 if (brcmf_sdiod_pm_resume_error(sdiodev))
469 /* Single skb use the standard mmc interface */
470 req_sz = pkt->len + 3;
474 err = sdio_memcpy_toio(sdiodev->func[fn], addr,
475 ((u8 *)(pkt->data)), req_sz);
477 err = sdio_memcpy_fromio(sdiodev->func[fn], ((u8 *)(pkt->data)),
480 /* function 2 read is FIFO operation */
481 err = sdio_readsb(sdiodev->func[fn], ((u8 *)(pkt->data)), addr,
483 if (err == -ENOMEDIUM)
484 brcmf_bus_change_state(sdiodev->bus_if, BRCMF_BUS_NOMEDIUM);
489 * brcmf_sdiod_sglist_rw - SDIO interface function for block data access
490 * @sdiodev: brcmfmac sdio device
491 * @fn: SDIO function number
492 * @write: direction flag
493 * @addr: dongle memory address as source/destination
496 * This function takes the respbonsibility as the interface function to MMC
497 * stack for block data access. It assumes that the skb passed down by the
498 * caller has already been padded and aligned.
500 static int brcmf_sdiod_sglist_rw(struct brcmf_sdio_dev *sdiodev, uint fn,
501 bool write, u32 addr,
502 struct sk_buff_head *pktlist)
504 unsigned int req_sz, func_blk_sz, sg_cnt, sg_data_sz, pkt_offset;
505 unsigned int max_req_sz, orig_offset, dst_offset;
506 unsigned short max_seg_cnt, seg_sz;
507 unsigned char *pkt_data, *orig_data, *dst_data;
508 struct sk_buff *pkt_next = NULL, *local_pkt_next;
509 struct sk_buff_head local_list, *target_list;
510 struct mmc_request mmc_req;
511 struct mmc_command mmc_cmd;
512 struct mmc_data mmc_dat;
513 struct scatterlist *sgl;
519 brcmf_sdiod_pm_resume_wait(sdiodev, &sdiodev->request_buffer_wait);
520 if (brcmf_sdiod_pm_resume_error(sdiodev))
523 target_list = pktlist;
524 /* for host with broken sg support, prepare a page aligned list */
525 __skb_queue_head_init(&local_list);
526 if (sdiodev->pdata && sdiodev->pdata->broken_sg_support && !write) {
528 skb_queue_walk(pktlist, pkt_next)
529 req_sz += pkt_next->len;
530 req_sz = ALIGN(req_sz, sdiodev->func[fn]->cur_blksize);
531 while (req_sz > PAGE_SIZE) {
532 pkt_next = brcmu_pkt_buf_get_skb(PAGE_SIZE);
533 if (pkt_next == NULL) {
537 __skb_queue_tail(&local_list, pkt_next);
540 pkt_next = brcmu_pkt_buf_get_skb(req_sz);
541 if (pkt_next == NULL) {
545 __skb_queue_tail(&local_list, pkt_next);
546 target_list = &local_list;
549 func_blk_sz = sdiodev->func[fn]->cur_blksize;
550 max_req_sz = sdiodev->max_request_size;
551 max_seg_cnt = min_t(unsigned short, sdiodev->max_segment_count,
553 seg_sz = target_list->qlen;
555 pkt_next = target_list->next;
557 memset(&mmc_req, 0, sizeof(struct mmc_request));
558 memset(&mmc_cmd, 0, sizeof(struct mmc_command));
559 memset(&mmc_dat, 0, sizeof(struct mmc_data));
561 mmc_dat.sg = sdiodev->sgtable.sgl;
562 mmc_dat.blksz = func_blk_sz;
563 mmc_dat.flags = write ? MMC_DATA_WRITE : MMC_DATA_READ;
564 mmc_cmd.opcode = SD_IO_RW_EXTENDED;
565 mmc_cmd.arg = write ? 1<<31 : 0; /* write flag */
566 mmc_cmd.arg |= (fn & 0x7) << 28; /* SDIO func num */
567 mmc_cmd.arg |= 1<<27; /* block mode */
568 /* for function 1 the addr will be incremented */
569 mmc_cmd.arg |= (fn == 1) ? 1<<26 : 0;
570 mmc_cmd.flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_ADTC;
571 mmc_req.cmd = &mmc_cmd;
572 mmc_req.data = &mmc_dat;
577 sgl = sdiodev->sgtable.sgl;
579 while (pkt_next != (struct sk_buff *)target_list) {
580 pkt_data = pkt_next->data + pkt_offset;
581 sg_data_sz = pkt_next->len - pkt_offset;
582 if (sg_data_sz > sdiodev->max_segment_size)
583 sg_data_sz = sdiodev->max_segment_size;
584 if (sg_data_sz > max_req_sz - req_sz)
585 sg_data_sz = max_req_sz - req_sz;
587 sg_set_buf(sgl, pkt_data, sg_data_sz);
591 req_sz += sg_data_sz;
592 pkt_offset += sg_data_sz;
593 if (pkt_offset == pkt_next->len) {
595 pkt_next = pkt_next->next;
598 if (req_sz >= max_req_sz || sg_cnt >= max_seg_cnt)
603 if (req_sz % func_blk_sz != 0) {
604 brcmf_err("sg request length %u is not %u aligned\n",
605 req_sz, func_blk_sz);
610 mmc_dat.sg_len = sg_cnt;
611 mmc_dat.blocks = req_sz / func_blk_sz;
612 mmc_cmd.arg |= (addr & 0x1FFFF) << 9; /* address */
613 mmc_cmd.arg |= mmc_dat.blocks & 0x1FF; /* block count */
614 /* incrementing addr for function 1 */
618 mmc_set_data_timeout(&mmc_dat, sdiodev->func[fn]->card);
619 mmc_wait_for_req(sdiodev->func[fn]->card->host, &mmc_req);
621 ret = mmc_cmd.error ? mmc_cmd.error : mmc_dat.error;
622 if (ret == -ENOMEDIUM) {
623 brcmf_bus_change_state(sdiodev->bus_if,
626 } else if (ret != 0) {
627 brcmf_err("CMD53 sg block %s failed %d\n",
628 write ? "write" : "read", ret);
634 if (sdiodev->pdata && sdiodev->pdata->broken_sg_support && !write) {
635 local_pkt_next = local_list.next;
637 skb_queue_walk(pktlist, pkt_next) {
640 req_sz = local_pkt_next->len - orig_offset;
641 req_sz = min_t(uint, pkt_next->len - dst_offset,
643 orig_data = local_pkt_next->data + orig_offset;
644 dst_data = pkt_next->data + dst_offset;
645 memcpy(dst_data, orig_data, req_sz);
646 orig_offset += req_sz;
647 dst_offset += req_sz;
648 if (orig_offset == local_pkt_next->len) {
650 local_pkt_next = local_pkt_next->next;
652 if (dst_offset == pkt_next->len)
654 } while (!skb_queue_empty(&local_list));
659 sg_init_table(sdiodev->sgtable.sgl, sdiodev->sgtable.orig_nents);
660 while ((pkt_next = __skb_dequeue(&local_list)) != NULL)
661 brcmu_pkt_buf_free_skb(pkt_next);
666 int brcmf_sdiod_recv_buf(struct brcmf_sdio_dev *sdiodev, u8 *buf, uint nbytes)
668 struct sk_buff *mypkt;
671 mypkt = brcmu_pkt_buf_get_skb(nbytes);
673 brcmf_err("brcmu_pkt_buf_get_skb failed: len %d\n",
678 err = brcmf_sdiod_recv_pkt(sdiodev, mypkt);
680 memcpy(buf, mypkt->data, nbytes);
682 brcmu_pkt_buf_free_skb(mypkt);
686 int brcmf_sdiod_recv_pkt(struct brcmf_sdio_dev *sdiodev, struct sk_buff *pkt)
688 u32 addr = sdiodev->sbwad;
691 brcmf_dbg(SDIO, "addr = 0x%x, size = %d\n", addr, pkt->len);
693 err = brcmf_sdiod_addrprep(sdiodev, 4, &addr);
697 err = brcmf_sdiod_buffrw(sdiodev, SDIO_FUNC_2, false, addr, pkt);
703 int brcmf_sdiod_recv_chain(struct brcmf_sdio_dev *sdiodev,
704 struct sk_buff_head *pktq, uint totlen)
706 struct sk_buff *glom_skb;
708 u32 addr = sdiodev->sbwad;
711 brcmf_dbg(SDIO, "addr = 0x%x, size = %d\n",
714 err = brcmf_sdiod_addrprep(sdiodev, 4, &addr);
719 err = brcmf_sdiod_buffrw(sdiodev, SDIO_FUNC_2, false, addr,
721 else if (!sdiodev->sg_support) {
722 glom_skb = brcmu_pkt_buf_get_skb(totlen);
725 err = brcmf_sdiod_buffrw(sdiodev, SDIO_FUNC_2, false, addr,
730 skb_queue_walk(pktq, skb) {
731 memcpy(skb->data, glom_skb->data, skb->len);
732 skb_pull(glom_skb, skb->len);
735 err = brcmf_sdiod_sglist_rw(sdiodev, SDIO_FUNC_2, false, addr,
742 int brcmf_sdiod_send_buf(struct brcmf_sdio_dev *sdiodev, u8 *buf, uint nbytes)
744 struct sk_buff *mypkt;
745 u32 addr = sdiodev->sbwad;
748 mypkt = brcmu_pkt_buf_get_skb(nbytes);
750 brcmf_err("brcmu_pkt_buf_get_skb failed: len %d\n",
755 memcpy(mypkt->data, buf, nbytes);
757 err = brcmf_sdiod_addrprep(sdiodev, 4, &addr);
760 err = brcmf_sdiod_buffrw(sdiodev, SDIO_FUNC_2, true, addr,
763 brcmu_pkt_buf_free_skb(mypkt);
768 int brcmf_sdiod_send_pkt(struct brcmf_sdio_dev *sdiodev,
769 struct sk_buff_head *pktq)
772 u32 addr = sdiodev->sbwad;
775 brcmf_dbg(SDIO, "addr = 0x%x, size = %d\n", addr, pktq->qlen);
777 err = brcmf_sdiod_addrprep(sdiodev, 4, &addr);
781 if (pktq->qlen == 1 || !sdiodev->sg_support)
782 skb_queue_walk(pktq, skb) {
783 err = brcmf_sdiod_buffrw(sdiodev, SDIO_FUNC_2, true,
789 err = brcmf_sdiod_sglist_rw(sdiodev, SDIO_FUNC_2, true, addr,
796 brcmf_sdiod_ramrw(struct brcmf_sdio_dev *sdiodev, bool write, u32 address,
804 dsize = min_t(uint, SBSDIO_SB_OFT_ADDR_LIMIT, size);
805 pkt = dev_alloc_skb(dsize);
807 brcmf_err("dev_alloc_skb failed: len %d\n", dsize);
812 /* Determine initial transfer parameters */
813 sdaddr = address & SBSDIO_SB_OFT_ADDR_MASK;
814 if ((sdaddr + size) & SBSDIO_SBWINDOW_MASK)
815 dsize = (SBSDIO_SB_OFT_ADDR_LIMIT - sdaddr);
819 sdio_claim_host(sdiodev->func[1]);
821 /* Do the transfer(s) */
823 /* Set the backplane window to include the start address */
824 bcmerror = brcmf_sdiod_set_sbaddr_window(sdiodev, address);
828 brcmf_dbg(SDIO, "%s %d bytes at offset 0x%08x in window 0x%08x\n",
829 write ? "write" : "read", dsize,
830 sdaddr, address & SBSDIO_SBWINDOW_MASK);
832 sdaddr &= SBSDIO_SB_OFT_ADDR_MASK;
833 sdaddr |= SBSDIO_SB_ACCESS_2_4B_FLAG;
837 memcpy(pkt->data, data, dsize);
838 bcmerror = brcmf_sdiod_buffrw(sdiodev, SDIO_FUNC_1, write,
841 brcmf_err("membytes transfer failed\n");
845 memcpy(data, pkt->data, dsize);
848 /* Adjust for next transfer (if any) */
854 dsize = min_t(uint, SBSDIO_SB_OFT_ADDR_LIMIT, size);
860 /* Return the window to backplane enumeration space for core access */
861 if (brcmf_sdiod_set_sbaddr_window(sdiodev, sdiodev->sbwad))
862 brcmf_err("FAILED to set window back to 0x%x\n",
865 sdio_release_host(sdiodev->func[1]);
870 int brcmf_sdiod_abort(struct brcmf_sdio_dev *sdiodev, uint fn)
872 char t_func = (char)fn;
873 brcmf_dbg(SDIO, "Enter\n");
875 /* issue abort cmd52 command through F0 */
876 brcmf_sdiod_request_data(sdiodev, SDIO_FUNC_0, SDIO_CCCR_ABORT,
877 sizeof(t_func), &t_func, true);
879 brcmf_dbg(SDIO, "Exit\n");
883 static void brcmf_sdiod_sgtable_alloc(struct brcmf_sdio_dev *sdiodev)
888 if (!sdiodev->sg_support)
891 nents = max_t(uint, BRCMF_DEFAULT_RXGLOM_SIZE, brcmf_sdiod_txglomsz);
892 nents += (nents >> 4) + 1;
894 WARN_ON(nents > sdiodev->max_segment_count);
896 brcmf_dbg(TRACE, "nents=%d\n", nents);
897 err = sg_alloc_table(&sdiodev->sgtable, nents, GFP_KERNEL);
899 brcmf_err("allocation failed: disable scatter-gather");
900 sdiodev->sg_support = false;
903 sdiodev->txglomsz = brcmf_sdiod_txglomsz;
906 static int brcmf_sdiod_remove(struct brcmf_sdio_dev *sdiodev)
909 brcmf_sdio_remove(sdiodev->bus);
913 /* Disable Function 2 */
914 sdio_claim_host(sdiodev->func[2]);
915 sdio_disable_func(sdiodev->func[2]);
916 sdio_release_host(sdiodev->func[2]);
918 /* Disable Function 1 */
919 sdio_claim_host(sdiodev->func[1]);
920 sdio_disable_func(sdiodev->func[1]);
921 sdio_release_host(sdiodev->func[1]);
923 sg_free_table(&sdiodev->sgtable);
929 static int brcmf_sdiod_probe(struct brcmf_sdio_dev *sdiodev)
931 struct sdio_func *func;
932 struct mmc_host *host;
936 sdiodev->num_funcs = 2;
938 sdio_claim_host(sdiodev->func[1]);
940 ret = sdio_set_block_size(sdiodev->func[1], SDIO_FUNC1_BLOCKSIZE);
942 brcmf_err("Failed to set F1 blocksize\n");
943 sdio_release_host(sdiodev->func[1]);
946 ret = sdio_set_block_size(sdiodev->func[2], SDIO_FUNC2_BLOCKSIZE);
948 brcmf_err("Failed to set F2 blocksize\n");
949 sdio_release_host(sdiodev->func[1]);
953 /* increase F2 timeout */
954 sdiodev->func[2]->enable_timeout = SDIO_WAIT_F2RDY;
956 /* Enable Function 1 */
957 ret = sdio_enable_func(sdiodev->func[1]);
958 sdio_release_host(sdiodev->func[1]);
960 brcmf_err("Failed to enable F1: err=%d\n", ret);
965 * determine host related variables after brcmf_sdiod_probe()
966 * as func->cur_blksize is properly set and F2 init has been
967 * completed successfully.
969 func = sdiodev->func[2];
970 host = func->card->host;
971 sdiodev->sg_support = host->max_segs > 1;
972 max_blocks = min_t(uint, host->max_blk_count, 511u);
973 sdiodev->max_request_size = min_t(uint, host->max_req_size,
974 max_blocks * func->cur_blksize);
975 sdiodev->max_segment_count = min_t(uint, host->max_segs,
976 SG_MAX_SINGLE_ALLOC);
977 sdiodev->max_segment_size = host->max_seg_size;
979 /* allocate scatter-gather table. sg support
980 * will be disabled upon allocation failure.
982 brcmf_sdiod_sgtable_alloc(sdiodev);
984 /* try to attach to the target device */
985 sdiodev->bus = brcmf_sdio_probe(sdiodev);
993 brcmf_sdiod_remove(sdiodev);
998 #define BRCMF_SDIO_DEVICE(dev_id) \
999 {SDIO_DEVICE(BRCM_SDIO_VENDOR_ID_BROADCOM, dev_id)}
1001 /* devices we support, null terminated */
1002 static const struct sdio_device_id brcmf_sdmmc_ids[] = {
1003 BRCMF_SDIO_DEVICE(BRCM_SDIO_43143_DEVICE_ID),
1004 BRCMF_SDIO_DEVICE(BRCM_SDIO_43241_DEVICE_ID),
1005 BRCMF_SDIO_DEVICE(BRCM_SDIO_4329_DEVICE_ID),
1006 BRCMF_SDIO_DEVICE(BRCM_SDIO_4330_DEVICE_ID),
1007 BRCMF_SDIO_DEVICE(BRCM_SDIO_4334_DEVICE_ID),
1008 BRCMF_SDIO_DEVICE(BRCM_SDIO_43362_DEVICE_ID),
1009 BRCMF_SDIO_DEVICE(BRCM_SDIO_4335_4339_DEVICE_ID),
1010 BRCMF_SDIO_DEVICE(BRCM_SDIO_4354_DEVICE_ID),
1011 { /* end: all zeroes */ }
1013 MODULE_DEVICE_TABLE(sdio, brcmf_sdmmc_ids);
1015 static struct brcmfmac_sdio_platform_data *brcmfmac_sdio_pdata;
1018 static int brcmf_ops_sdio_probe(struct sdio_func *func,
1019 const struct sdio_device_id *id)
1022 struct brcmf_sdio_dev *sdiodev;
1023 struct brcmf_bus *bus_if;
1025 brcmf_dbg(SDIO, "Enter\n");
1026 brcmf_dbg(SDIO, "Class=%x\n", func->class);
1027 brcmf_dbg(SDIO, "sdio vendor ID: 0x%04x\n", func->vendor);
1028 brcmf_dbg(SDIO, "sdio device ID: 0x%04x\n", func->device);
1029 brcmf_dbg(SDIO, "Function#: %d\n", func->num);
1031 /* Consume func num 1 but dont do anything with it. */
1035 /* Ignore anything but func 2 */
1039 bus_if = kzalloc(sizeof(struct brcmf_bus), GFP_KERNEL);
1042 sdiodev = kzalloc(sizeof(struct brcmf_sdio_dev), GFP_KERNEL);
1048 /* store refs to functions used. mmc_card does
1049 * not hold the F0 function pointer.
1051 sdiodev->func[0] = kmemdup(func, sizeof(*func), GFP_KERNEL);
1052 sdiodev->func[0]->num = 0;
1053 sdiodev->func[1] = func->card->sdio_func[0];
1054 sdiodev->func[2] = func;
1056 sdiodev->bus_if = bus_if;
1057 bus_if->bus_priv.sdio = sdiodev;
1058 bus_if->proto_type = BRCMF_PROTO_BCDC;
1059 dev_set_drvdata(&func->dev, bus_if);
1060 dev_set_drvdata(&sdiodev->func[1]->dev, bus_if);
1061 sdiodev->dev = &sdiodev->func[1]->dev;
1062 sdiodev->pdata = brcmfmac_sdio_pdata;
1064 if (!sdiodev->pdata)
1065 brcmf_of_probe(sdiodev);
1067 atomic_set(&sdiodev->suspend, false);
1068 init_waitqueue_head(&sdiodev->request_word_wait);
1069 init_waitqueue_head(&sdiodev->request_buffer_wait);
1071 brcmf_dbg(SDIO, "F2 found, calling brcmf_sdiod_probe...\n");
1072 err = brcmf_sdiod_probe(sdiodev);
1074 brcmf_err("F2 error, probe failed %d...\n", err);
1078 brcmf_dbg(SDIO, "F2 init completed...\n");
1082 dev_set_drvdata(&func->dev, NULL);
1083 dev_set_drvdata(&sdiodev->func[1]->dev, NULL);
1084 kfree(sdiodev->func[0]);
1090 static void brcmf_ops_sdio_remove(struct sdio_func *func)
1092 struct brcmf_bus *bus_if;
1093 struct brcmf_sdio_dev *sdiodev;
1095 brcmf_dbg(SDIO, "Enter\n");
1096 brcmf_dbg(SDIO, "sdio vendor ID: 0x%04x\n", func->vendor);
1097 brcmf_dbg(SDIO, "sdio device ID: 0x%04x\n", func->device);
1098 brcmf_dbg(SDIO, "Function: %d\n", func->num);
1100 if (func->num != 1 && func->num != 2)
1103 bus_if = dev_get_drvdata(&func->dev);
1105 sdiodev = bus_if->bus_priv.sdio;
1106 brcmf_sdiod_remove(sdiodev);
1108 dev_set_drvdata(&sdiodev->func[1]->dev, NULL);
1109 dev_set_drvdata(&sdiodev->func[2]->dev, NULL);
1112 kfree(sdiodev->func[0]);
1116 brcmf_dbg(SDIO, "Exit\n");
1119 #ifdef CONFIG_PM_SLEEP
1120 static int brcmf_ops_sdio_suspend(struct device *dev)
1122 mmc_pm_flag_t sdio_flags;
1123 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1124 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
1127 brcmf_dbg(SDIO, "Enter\n");
1129 sdio_flags = sdio_get_host_pm_caps(sdiodev->func[1]);
1130 if (!(sdio_flags & MMC_PM_KEEP_POWER)) {
1131 brcmf_err("Host can't keep power while suspended\n");
1135 atomic_set(&sdiodev->suspend, true);
1137 ret = sdio_set_host_pm_flags(sdiodev->func[1], MMC_PM_KEEP_POWER);
1139 brcmf_err("Failed to set pm_flags\n");
1140 atomic_set(&sdiodev->suspend, false);
1144 brcmf_sdio_wd_timer(sdiodev->bus, 0);
1149 static int brcmf_ops_sdio_resume(struct device *dev)
1151 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1152 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
1154 brcmf_dbg(SDIO, "Enter\n");
1155 brcmf_sdio_wd_timer(sdiodev->bus, BRCMF_WD_POLL_MS);
1156 atomic_set(&sdiodev->suspend, false);
1160 static const struct dev_pm_ops brcmf_sdio_pm_ops = {
1161 .suspend = brcmf_ops_sdio_suspend,
1162 .resume = brcmf_ops_sdio_resume,
1164 #endif /* CONFIG_PM_SLEEP */
1166 static struct sdio_driver brcmf_sdmmc_driver = {
1167 .probe = brcmf_ops_sdio_probe,
1168 .remove = brcmf_ops_sdio_remove,
1169 .name = BRCMFMAC_SDIO_PDATA_NAME,
1170 .id_table = brcmf_sdmmc_ids,
1172 .owner = THIS_MODULE,
1173 #ifdef CONFIG_PM_SLEEP
1174 .pm = &brcmf_sdio_pm_ops,
1175 #endif /* CONFIG_PM_SLEEP */
1179 static int __init brcmf_sdio_pd_probe(struct platform_device *pdev)
1181 brcmf_dbg(SDIO, "Enter\n");
1183 brcmfmac_sdio_pdata = dev_get_platdata(&pdev->dev);
1185 if (brcmfmac_sdio_pdata->power_on)
1186 brcmfmac_sdio_pdata->power_on();
1191 static int brcmf_sdio_pd_remove(struct platform_device *pdev)
1193 brcmf_dbg(SDIO, "Enter\n");
1195 if (brcmfmac_sdio_pdata->power_off)
1196 brcmfmac_sdio_pdata->power_off();
1198 sdio_unregister_driver(&brcmf_sdmmc_driver);
1203 static struct platform_driver brcmf_sdio_pd = {
1204 .remove = brcmf_sdio_pd_remove,
1206 .name = BRCMFMAC_SDIO_PDATA_NAME,
1207 .owner = THIS_MODULE,
1211 void brcmf_sdio_register(void)
1215 ret = sdio_register_driver(&brcmf_sdmmc_driver);
1217 brcmf_err("sdio_register_driver failed: %d\n", ret);
1220 void brcmf_sdio_exit(void)
1222 brcmf_dbg(SDIO, "Enter\n");
1224 if (brcmfmac_sdio_pdata)
1225 platform_driver_unregister(&brcmf_sdio_pd);
1227 sdio_unregister_driver(&brcmf_sdmmc_driver);
1230 void __init brcmf_sdio_init(void)
1234 brcmf_dbg(SDIO, "Enter\n");
1236 ret = platform_driver_probe(&brcmf_sdio_pd, brcmf_sdio_pd_probe);
1238 brcmf_dbg(SDIO, "No platform data available.\n");