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/pm_runtime.h>
31 #include <linux/suspend.h>
32 #include <linux/errno.h>
33 #include <linux/module.h>
34 #include <linux/acpi.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>
50 #define SDIOH_API_ACCESS_RETRY_LIMIT 2
52 #define DMA_ALIGN_MASK 0x03
54 #define SDIO_FUNC1_BLOCKSIZE 64
55 #define SDIO_FUNC2_BLOCKSIZE 512
56 /* Maximum milliseconds to wait for F2 to come up */
57 #define SDIO_WAIT_F2RDY 3000
59 #define BRCMF_DEFAULT_RXGLOM_SIZE 32 /* max rx frames in glom chain */
61 struct brcmf_sdiod_freezer {
63 atomic_t thread_count;
65 wait_queue_head_t thread_freeze;
66 struct completion resumed;
69 static irqreturn_t brcmf_sdiod_oob_irqhandler(int irq, void *dev_id)
71 struct brcmf_bus *bus_if = dev_get_drvdata(dev_id);
72 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
74 brcmf_dbg(INTR, "OOB intr triggered\n");
76 /* out-of-band interrupt is level-triggered which won't
77 * be cleared until dpc
79 if (sdiodev->irq_en) {
80 disable_irq_nosync(irq);
81 sdiodev->irq_en = false;
84 brcmf_sdio_isr(sdiodev->bus);
89 static void brcmf_sdiod_ib_irqhandler(struct sdio_func *func)
91 struct brcmf_bus *bus_if = dev_get_drvdata(&func->dev);
92 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
94 brcmf_dbg(INTR, "IB intr triggered\n");
96 brcmf_sdio_isr(sdiodev->bus);
99 /* dummy handler for SDIO function 2 interrupt */
100 static void brcmf_sdiod_dummy_irqhandler(struct sdio_func *func)
104 int brcmf_sdiod_intr_register(struct brcmf_sdio_dev *sdiodev)
106 struct brcmfmac_sdio_pd *pdata;
109 u32 addr, gpiocontrol;
112 pdata = &sdiodev->settings->bus.sdio;
113 if (pdata->oob_irq_supported) {
114 brcmf_dbg(SDIO, "Enter, register OOB IRQ %d\n",
116 ret = request_irq(pdata->oob_irq_nr, brcmf_sdiod_oob_irqhandler,
117 pdata->oob_irq_flags, "brcmf_oob_intr",
118 &sdiodev->func[1]->dev);
120 brcmf_err("request_irq failed %d\n", ret);
123 sdiodev->oob_irq_requested = true;
124 spin_lock_init(&sdiodev->irq_en_lock);
125 spin_lock_irqsave(&sdiodev->irq_en_lock, flags);
126 sdiodev->irq_en = true;
127 spin_unlock_irqrestore(&sdiodev->irq_en_lock, flags);
129 ret = enable_irq_wake(pdata->oob_irq_nr);
131 brcmf_err("enable_irq_wake failed %d\n", ret);
134 sdiodev->irq_wake = true;
136 sdio_claim_host(sdiodev->func[1]);
138 if (sdiodev->bus_if->chip == BRCM_CC_43362_CHIP_ID) {
139 /* assign GPIO to SDIO core */
140 addr = CORE_CC_REG(SI_ENUM_BASE, gpiocontrol);
141 gpiocontrol = brcmf_sdiod_regrl(sdiodev, addr, &ret);
143 brcmf_sdiod_regwl(sdiodev, addr, gpiocontrol, &ret);
145 brcmf_sdiod_regwb(sdiodev, SBSDIO_GPIO_SELECT, 0xf,
147 brcmf_sdiod_regwb(sdiodev, SBSDIO_GPIO_OUT, 0, &ret);
148 brcmf_sdiod_regwb(sdiodev, SBSDIO_GPIO_EN, 0x2, &ret);
151 /* must configure SDIO_CCCR_IENx to enable irq */
152 data = brcmf_sdiod_regrb(sdiodev, SDIO_CCCR_IENx, &ret);
153 data |= 1 << SDIO_FUNC_1 | 1 << SDIO_FUNC_2 | 1;
154 brcmf_sdiod_regwb(sdiodev, SDIO_CCCR_IENx, data, &ret);
156 /* redirect, configure and enable io for interrupt signal */
157 data = SDIO_SEPINT_MASK | SDIO_SEPINT_OE;
158 if (pdata->oob_irq_flags & IRQF_TRIGGER_HIGH)
159 data |= SDIO_SEPINT_ACT_HI;
160 brcmf_sdiod_regwb(sdiodev, SDIO_CCCR_BRCM_SEPINT, data, &ret);
162 sdio_release_host(sdiodev->func[1]);
164 brcmf_dbg(SDIO, "Entering\n");
165 sdio_claim_host(sdiodev->func[1]);
166 sdio_claim_irq(sdiodev->func[1], brcmf_sdiod_ib_irqhandler);
167 sdio_claim_irq(sdiodev->func[2], brcmf_sdiod_dummy_irqhandler);
168 sdio_release_host(sdiodev->func[1]);
169 sdiodev->sd_irq_requested = true;
175 void brcmf_sdiod_intr_unregister(struct brcmf_sdio_dev *sdiodev)
178 brcmf_dbg(SDIO, "Entering oob=%d sd=%d\n",
179 sdiodev->oob_irq_requested,
180 sdiodev->sd_irq_requested);
182 if (sdiodev->oob_irq_requested) {
183 struct brcmfmac_sdio_pd *pdata;
185 pdata = &sdiodev->settings->bus.sdio;
186 sdio_claim_host(sdiodev->func[1]);
187 brcmf_sdiod_regwb(sdiodev, SDIO_CCCR_BRCM_SEPINT, 0, NULL);
188 brcmf_sdiod_regwb(sdiodev, SDIO_CCCR_IENx, 0, NULL);
189 sdio_release_host(sdiodev->func[1]);
191 sdiodev->oob_irq_requested = false;
192 if (sdiodev->irq_wake) {
193 disable_irq_wake(pdata->oob_irq_nr);
194 sdiodev->irq_wake = false;
196 free_irq(pdata->oob_irq_nr, &sdiodev->func[1]->dev);
197 sdiodev->irq_en = false;
198 sdiodev->oob_irq_requested = false;
201 if (sdiodev->sd_irq_requested) {
202 sdio_claim_host(sdiodev->func[1]);
203 sdio_release_irq(sdiodev->func[2]);
204 sdio_release_irq(sdiodev->func[1]);
205 sdio_release_host(sdiodev->func[1]);
206 sdiodev->sd_irq_requested = false;
210 void brcmf_sdiod_change_state(struct brcmf_sdio_dev *sdiodev,
211 enum brcmf_sdiod_state state)
213 if (sdiodev->state == BRCMF_SDIOD_NOMEDIUM ||
214 state == sdiodev->state)
217 brcmf_dbg(TRACE, "%d -> %d\n", sdiodev->state, state);
218 switch (sdiodev->state) {
219 case BRCMF_SDIOD_DATA:
220 /* any other state means bus interface is down */
221 brcmf_bus_change_state(sdiodev->bus_if, BRCMF_BUS_DOWN);
223 case BRCMF_SDIOD_DOWN:
224 /* transition from DOWN to DATA means bus interface is up */
225 if (state == BRCMF_SDIOD_DATA)
226 brcmf_bus_change_state(sdiodev->bus_if, BRCMF_BUS_UP);
231 sdiodev->state = state;
234 static inline int brcmf_sdiod_f0_writeb(struct sdio_func *func,
235 uint regaddr, u8 byte)
240 * Can only directly write to some F0 registers.
241 * Handle CCCR_IENx and CCCR_ABORT command
244 if ((regaddr == SDIO_CCCR_ABORT) ||
245 (regaddr == SDIO_CCCR_IENx))
246 sdio_writeb(func, byte, regaddr, &err_ret);
248 sdio_f0_writeb(func, byte, regaddr, &err_ret);
253 static int brcmf_sdiod_request_data(struct brcmf_sdio_dev *sdiodev, u8 fn,
254 u32 addr, u8 regsz, void *data, bool write)
256 struct sdio_func *func;
259 brcmf_dbg(SDIO, "rw=%d, func=%d, addr=0x%05x, nbytes=%d\n",
260 write, fn, addr, regsz);
262 /* only allow byte access on F0 */
263 if (WARN_ON(regsz > 1 && !fn))
265 func = sdiodev->func[fn];
271 sdio_writeb(func, *(u8 *)data, addr, &ret);
273 ret = brcmf_sdiod_f0_writeb(func, addr,
277 *(u8 *)data = sdio_readb(func, addr, &ret);
279 *(u8 *)data = sdio_f0_readb(func, addr, &ret);
284 sdio_writew(func, *(u16 *)data, addr, &ret);
286 *(u16 *)data = sdio_readw(func, addr, &ret);
290 sdio_writel(func, *(u32 *)data, addr, &ret);
292 *(u32 *)data = sdio_readl(func, addr, &ret);
295 brcmf_err("invalid size: %d\n", regsz);
300 brcmf_dbg(SDIO, "failed to %s data F%d@0x%05x, err: %d\n",
301 write ? "write" : "read", fn, addr, ret);
306 static int brcmf_sdiod_regrw_helper(struct brcmf_sdio_dev *sdiodev, u32 addr,
307 u8 regsz, void *data, bool write)
313 if (sdiodev->state == BRCMF_SDIOD_NOMEDIUM)
317 * figure out how to read the register based on address range
318 * 0x00 ~ 0x7FF: function 0 CCCR and FBR
319 * 0x10000 ~ 0x1FFFF: function 1 miscellaneous registers
320 * The rest: function 1 silicon backplane core registers
322 if ((addr & ~REG_F0_REG_MASK) == 0)
329 memset(data, 0, regsz);
330 /* for retry wait for 1 ms till bus get settled down */
332 usleep_range(1000, 2000);
333 ret = brcmf_sdiod_request_data(sdiodev, func, addr, regsz,
335 } while (ret != 0 && ret != -ENOMEDIUM &&
336 retry++ < SDIOH_API_ACCESS_RETRY_LIMIT);
338 if (ret == -ENOMEDIUM)
339 brcmf_sdiod_change_state(sdiodev, BRCMF_SDIOD_NOMEDIUM);
342 * SleepCSR register access can fail when
343 * waking up the device so reduce this noise
346 if (addr != SBSDIO_FUNC1_SLEEPCSR)
347 brcmf_err("failed to %s data F%d@0x%05x, err: %d\n",
348 write ? "write" : "read", func, addr, ret);
350 brcmf_dbg(SDIO, "failed to %s data F%d@0x%05x, err: %d\n",
351 write ? "write" : "read", func, addr, ret);
357 brcmf_sdiod_set_sbaddr_window(struct brcmf_sdio_dev *sdiodev, u32 address)
362 if (sdiodev->state == BRCMF_SDIOD_NOMEDIUM)
365 addr[0] = (address >> 8) & SBSDIO_SBADDRLOW_MASK;
366 addr[1] = (address >> 16) & SBSDIO_SBADDRMID_MASK;
367 addr[2] = (address >> 24) & SBSDIO_SBADDRHIGH_MASK;
369 for (i = 0; i < 3; i++) {
370 err = brcmf_sdiod_regrw_helper(sdiodev,
371 SBSDIO_FUNC1_SBADDRLOW + i,
372 sizeof(u8), &addr[i], true);
374 brcmf_err("failed at addr: 0x%0x\n",
375 SBSDIO_FUNC1_SBADDRLOW + i);
384 brcmf_sdiod_addrprep(struct brcmf_sdio_dev *sdiodev, uint width, u32 *addr)
386 uint bar0 = *addr & ~SBSDIO_SB_OFT_ADDR_MASK;
389 if (bar0 != sdiodev->sbwad) {
390 err = brcmf_sdiod_set_sbaddr_window(sdiodev, bar0);
394 sdiodev->sbwad = bar0;
397 *addr &= SBSDIO_SB_OFT_ADDR_MASK;
400 *addr |= SBSDIO_SB_ACCESS_2_4B_FLAG;
405 u8 brcmf_sdiod_regrb(struct brcmf_sdio_dev *sdiodev, u32 addr, int *ret)
410 brcmf_dbg(SDIO, "addr:0x%08x\n", addr);
411 retval = brcmf_sdiod_regrw_helper(sdiodev, addr, sizeof(data), &data,
413 brcmf_dbg(SDIO, "data:0x%02x\n", data);
421 u32 brcmf_sdiod_regrl(struct brcmf_sdio_dev *sdiodev, u32 addr, int *ret)
426 brcmf_dbg(SDIO, "addr:0x%08x\n", addr);
427 retval = brcmf_sdiod_addrprep(sdiodev, sizeof(data), &addr);
430 retval = brcmf_sdiod_regrw_helper(sdiodev, addr, sizeof(data), &data,
432 brcmf_dbg(SDIO, "data:0x%08x\n", data);
441 void brcmf_sdiod_regwb(struct brcmf_sdio_dev *sdiodev, u32 addr,
446 brcmf_dbg(SDIO, "addr:0x%08x, data:0x%02x\n", addr, data);
447 retval = brcmf_sdiod_regrw_helper(sdiodev, addr, sizeof(data), &data,
453 void brcmf_sdiod_regwl(struct brcmf_sdio_dev *sdiodev, u32 addr,
458 brcmf_dbg(SDIO, "addr:0x%08x, data:0x%08x\n", addr, data);
459 retval = brcmf_sdiod_addrprep(sdiodev, sizeof(data), &addr);
462 retval = brcmf_sdiod_regrw_helper(sdiodev, addr, sizeof(data), &data,
470 static int brcmf_sdiod_buffrw(struct brcmf_sdio_dev *sdiodev, uint fn,
471 bool write, u32 addr, struct sk_buff *pkt)
476 /* Single skb use the standard mmc interface */
477 req_sz = pkt->len + 3;
481 err = sdio_memcpy_toio(sdiodev->func[fn], addr,
482 ((u8 *)(pkt->data)), req_sz);
484 err = sdio_memcpy_fromio(sdiodev->func[fn], ((u8 *)(pkt->data)),
487 /* function 2 read is FIFO operation */
488 err = sdio_readsb(sdiodev->func[fn], ((u8 *)(pkt->data)), addr,
490 if (err == -ENOMEDIUM)
491 brcmf_sdiod_change_state(sdiodev, BRCMF_SDIOD_NOMEDIUM);
496 * brcmf_sdiod_sglist_rw - SDIO interface function for block data access
497 * @sdiodev: brcmfmac sdio device
498 * @fn: SDIO function number
499 * @write: direction flag
500 * @addr: dongle memory address as source/destination
503 * This function takes the respbonsibility as the interface function to MMC
504 * stack for block data access. It assumes that the skb passed down by the
505 * caller has already been padded and aligned.
507 static int brcmf_sdiod_sglist_rw(struct brcmf_sdio_dev *sdiodev, uint fn,
508 bool write, u32 addr,
509 struct sk_buff_head *pktlist)
511 unsigned int req_sz, func_blk_sz, sg_cnt, sg_data_sz, pkt_offset;
512 unsigned int max_req_sz, orig_offset, dst_offset;
513 unsigned short max_seg_cnt, seg_sz;
514 unsigned char *pkt_data, *orig_data, *dst_data;
515 struct sk_buff *pkt_next = NULL, *local_pkt_next;
516 struct sk_buff_head local_list, *target_list;
517 struct mmc_request mmc_req;
518 struct mmc_command mmc_cmd;
519 struct mmc_data mmc_dat;
520 struct scatterlist *sgl;
526 target_list = pktlist;
527 /* for host with broken sg support, prepare a page aligned list */
528 __skb_queue_head_init(&local_list);
529 if (!write && sdiodev->settings->bus.sdio.broken_sg_support) {
531 skb_queue_walk(pktlist, pkt_next)
532 req_sz += pkt_next->len;
533 req_sz = ALIGN(req_sz, sdiodev->func[fn]->cur_blksize);
534 while (req_sz > PAGE_SIZE) {
535 pkt_next = brcmu_pkt_buf_get_skb(PAGE_SIZE);
536 if (pkt_next == NULL) {
540 __skb_queue_tail(&local_list, pkt_next);
543 pkt_next = brcmu_pkt_buf_get_skb(req_sz);
544 if (pkt_next == NULL) {
548 __skb_queue_tail(&local_list, pkt_next);
549 target_list = &local_list;
552 func_blk_sz = sdiodev->func[fn]->cur_blksize;
553 max_req_sz = sdiodev->max_request_size;
554 max_seg_cnt = min_t(unsigned short, sdiodev->max_segment_count,
556 seg_sz = target_list->qlen;
558 pkt_next = target_list->next;
560 memset(&mmc_req, 0, sizeof(struct mmc_request));
561 memset(&mmc_cmd, 0, sizeof(struct mmc_command));
562 memset(&mmc_dat, 0, sizeof(struct mmc_data));
564 mmc_dat.sg = sdiodev->sgtable.sgl;
565 mmc_dat.blksz = func_blk_sz;
566 mmc_dat.flags = write ? MMC_DATA_WRITE : MMC_DATA_READ;
567 mmc_cmd.opcode = SD_IO_RW_EXTENDED;
568 mmc_cmd.arg = write ? 1<<31 : 0; /* write flag */
569 mmc_cmd.arg |= (fn & 0x7) << 28; /* SDIO func num */
570 mmc_cmd.arg |= 1<<27; /* block mode */
571 /* for function 1 the addr will be incremented */
572 mmc_cmd.arg |= (fn == 1) ? 1<<26 : 0;
573 mmc_cmd.flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_ADTC;
574 mmc_req.cmd = &mmc_cmd;
575 mmc_req.data = &mmc_dat;
580 sgl = sdiodev->sgtable.sgl;
582 while (pkt_next != (struct sk_buff *)target_list) {
583 pkt_data = pkt_next->data + pkt_offset;
584 sg_data_sz = pkt_next->len - pkt_offset;
585 if (sg_data_sz > sdiodev->max_segment_size)
586 sg_data_sz = sdiodev->max_segment_size;
587 if (sg_data_sz > max_req_sz - req_sz)
588 sg_data_sz = max_req_sz - req_sz;
590 sg_set_buf(sgl, pkt_data, sg_data_sz);
594 req_sz += sg_data_sz;
595 pkt_offset += sg_data_sz;
596 if (pkt_offset == pkt_next->len) {
598 pkt_next = pkt_next->next;
601 if (req_sz >= max_req_sz || sg_cnt >= max_seg_cnt)
606 if (req_sz % func_blk_sz != 0) {
607 brcmf_err("sg request length %u is not %u aligned\n",
608 req_sz, func_blk_sz);
613 mmc_dat.sg_len = sg_cnt;
614 mmc_dat.blocks = req_sz / func_blk_sz;
615 mmc_cmd.arg |= (addr & 0x1FFFF) << 9; /* address */
616 mmc_cmd.arg |= mmc_dat.blocks & 0x1FF; /* block count */
617 /* incrementing addr for function 1 */
621 mmc_set_data_timeout(&mmc_dat, sdiodev->func[fn]->card);
622 mmc_wait_for_req(sdiodev->func[fn]->card->host, &mmc_req);
624 ret = mmc_cmd.error ? mmc_cmd.error : mmc_dat.error;
625 if (ret == -ENOMEDIUM) {
626 brcmf_sdiod_change_state(sdiodev, BRCMF_SDIOD_NOMEDIUM);
628 } else if (ret != 0) {
629 brcmf_err("CMD53 sg block %s failed %d\n",
630 write ? "write" : "read", ret);
636 if (!write && sdiodev->settings->bus.sdio.broken_sg_support) {
637 local_pkt_next = local_list.next;
639 skb_queue_walk(pktlist, pkt_next) {
642 req_sz = local_pkt_next->len - orig_offset;
643 req_sz = min_t(uint, pkt_next->len - dst_offset,
645 orig_data = local_pkt_next->data + orig_offset;
646 dst_data = pkt_next->data + dst_offset;
647 memcpy(dst_data, orig_data, req_sz);
648 orig_offset += req_sz;
649 dst_offset += req_sz;
650 if (orig_offset == local_pkt_next->len) {
652 local_pkt_next = local_pkt_next->next;
654 if (dst_offset == pkt_next->len)
656 } while (!skb_queue_empty(&local_list));
661 sg_init_table(sdiodev->sgtable.sgl, sdiodev->sgtable.orig_nents);
662 while ((pkt_next = __skb_dequeue(&local_list)) != NULL)
663 brcmu_pkt_buf_free_skb(pkt_next);
668 int brcmf_sdiod_recv_buf(struct brcmf_sdio_dev *sdiodev, u8 *buf, uint nbytes)
670 struct sk_buff *mypkt;
673 mypkt = brcmu_pkt_buf_get_skb(nbytes);
675 brcmf_err("brcmu_pkt_buf_get_skb failed: len %d\n",
680 err = brcmf_sdiod_recv_pkt(sdiodev, mypkt);
682 memcpy(buf, mypkt->data, nbytes);
684 brcmu_pkt_buf_free_skb(mypkt);
688 int brcmf_sdiod_recv_pkt(struct brcmf_sdio_dev *sdiodev, struct sk_buff *pkt)
690 u32 addr = sdiodev->sbwad;
693 brcmf_dbg(SDIO, "addr = 0x%x, size = %d\n", addr, pkt->len);
695 err = brcmf_sdiod_addrprep(sdiodev, 4, &addr);
699 err = brcmf_sdiod_buffrw(sdiodev, SDIO_FUNC_2, false, addr, pkt);
705 int brcmf_sdiod_recv_chain(struct brcmf_sdio_dev *sdiodev,
706 struct sk_buff_head *pktq, uint totlen)
708 struct sk_buff *glom_skb = NULL;
710 u32 addr = sdiodev->sbwad;
713 brcmf_dbg(SDIO, "addr = 0x%x, size = %d\n",
716 err = brcmf_sdiod_addrprep(sdiodev, 4, &addr);
721 err = brcmf_sdiod_buffrw(sdiodev, SDIO_FUNC_2, false, addr,
723 else if (!sdiodev->sg_support) {
724 glom_skb = brcmu_pkt_buf_get_skb(totlen);
727 err = brcmf_sdiod_buffrw(sdiodev, SDIO_FUNC_2, false, addr,
732 skb_queue_walk(pktq, skb) {
733 memcpy(skb->data, glom_skb->data, skb->len);
734 skb_pull(glom_skb, skb->len);
737 err = brcmf_sdiod_sglist_rw(sdiodev, SDIO_FUNC_2, false, addr,
741 brcmu_pkt_buf_free_skb(glom_skb);
745 int brcmf_sdiod_send_buf(struct brcmf_sdio_dev *sdiodev, u8 *buf, uint nbytes)
747 struct sk_buff *mypkt;
748 u32 addr = sdiodev->sbwad;
751 mypkt = brcmu_pkt_buf_get_skb(nbytes);
753 brcmf_err("brcmu_pkt_buf_get_skb failed: len %d\n",
758 memcpy(mypkt->data, buf, nbytes);
760 err = brcmf_sdiod_addrprep(sdiodev, 4, &addr);
763 err = brcmf_sdiod_buffrw(sdiodev, SDIO_FUNC_2, true, addr,
766 brcmu_pkt_buf_free_skb(mypkt);
771 int brcmf_sdiod_send_pkt(struct brcmf_sdio_dev *sdiodev,
772 struct sk_buff_head *pktq)
775 u32 addr = sdiodev->sbwad;
778 brcmf_dbg(SDIO, "addr = 0x%x, size = %d\n", addr, pktq->qlen);
780 err = brcmf_sdiod_addrprep(sdiodev, 4, &addr);
784 if (pktq->qlen == 1 || !sdiodev->sg_support)
785 skb_queue_walk(pktq, skb) {
786 err = brcmf_sdiod_buffrw(sdiodev, SDIO_FUNC_2, true,
792 err = brcmf_sdiod_sglist_rw(sdiodev, SDIO_FUNC_2, true, addr,
799 brcmf_sdiod_ramrw(struct brcmf_sdio_dev *sdiodev, bool write, u32 address,
807 dsize = min_t(uint, SBSDIO_SB_OFT_ADDR_LIMIT, size);
808 pkt = dev_alloc_skb(dsize);
810 brcmf_err("dev_alloc_skb failed: len %d\n", dsize);
815 /* Determine initial transfer parameters */
816 sdaddr = address & SBSDIO_SB_OFT_ADDR_MASK;
817 if ((sdaddr + size) & SBSDIO_SBWINDOW_MASK)
818 dsize = (SBSDIO_SB_OFT_ADDR_LIMIT - sdaddr);
822 sdio_claim_host(sdiodev->func[1]);
824 /* Do the transfer(s) */
826 /* Set the backplane window to include the start address */
827 bcmerror = brcmf_sdiod_set_sbaddr_window(sdiodev, address);
831 brcmf_dbg(SDIO, "%s %d bytes at offset 0x%08x in window 0x%08x\n",
832 write ? "write" : "read", dsize,
833 sdaddr, address & SBSDIO_SBWINDOW_MASK);
835 sdaddr &= SBSDIO_SB_OFT_ADDR_MASK;
836 sdaddr |= SBSDIO_SB_ACCESS_2_4B_FLAG;
840 memcpy(pkt->data, data, dsize);
841 bcmerror = brcmf_sdiod_buffrw(sdiodev, SDIO_FUNC_1, write,
844 brcmf_err("membytes transfer failed\n");
848 memcpy(data, pkt->data, dsize);
851 /* Adjust for next transfer (if any) */
857 dsize = min_t(uint, SBSDIO_SB_OFT_ADDR_LIMIT, size);
863 /* Return the window to backplane enumeration space for core access */
864 if (brcmf_sdiod_set_sbaddr_window(sdiodev, sdiodev->sbwad))
865 brcmf_err("FAILED to set window back to 0x%x\n",
868 sdio_release_host(sdiodev->func[1]);
873 int brcmf_sdiod_abort(struct brcmf_sdio_dev *sdiodev, uint fn)
875 char t_func = (char)fn;
876 brcmf_dbg(SDIO, "Enter\n");
878 /* issue abort cmd52 command through F0 */
879 brcmf_sdiod_request_data(sdiodev, SDIO_FUNC_0, SDIO_CCCR_ABORT,
880 sizeof(t_func), &t_func, true);
882 brcmf_dbg(SDIO, "Exit\n");
886 void brcmf_sdiod_sgtable_alloc(struct brcmf_sdio_dev *sdiodev)
888 struct sdio_func *func;
889 struct mmc_host *host;
894 func = sdiodev->func[2];
895 host = func->card->host;
896 sdiodev->sg_support = host->max_segs > 1;
897 max_blocks = min_t(uint, host->max_blk_count, 511u);
898 sdiodev->max_request_size = min_t(uint, host->max_req_size,
899 max_blocks * func->cur_blksize);
900 sdiodev->max_segment_count = min_t(uint, host->max_segs,
901 SG_MAX_SINGLE_ALLOC);
902 sdiodev->max_segment_size = host->max_seg_size;
904 if (!sdiodev->sg_support)
907 nents = max_t(uint, BRCMF_DEFAULT_RXGLOM_SIZE,
908 sdiodev->settings->bus.sdio.txglomsz);
909 nents += (nents >> 4) + 1;
911 WARN_ON(nents > sdiodev->max_segment_count);
913 brcmf_dbg(TRACE, "nents=%d\n", nents);
914 err = sg_alloc_table(&sdiodev->sgtable, nents, GFP_KERNEL);
916 brcmf_err("allocation failed: disable scatter-gather");
917 sdiodev->sg_support = false;
920 sdiodev->txglomsz = sdiodev->settings->bus.sdio.txglomsz;
923 #ifdef CONFIG_PM_SLEEP
924 static int brcmf_sdiod_freezer_attach(struct brcmf_sdio_dev *sdiodev)
926 sdiodev->freezer = kzalloc(sizeof(*sdiodev->freezer), GFP_KERNEL);
927 if (!sdiodev->freezer)
929 atomic_set(&sdiodev->freezer->thread_count, 0);
930 atomic_set(&sdiodev->freezer->freezing, 0);
931 init_waitqueue_head(&sdiodev->freezer->thread_freeze);
932 init_completion(&sdiodev->freezer->resumed);
936 static void brcmf_sdiod_freezer_detach(struct brcmf_sdio_dev *sdiodev)
938 if (sdiodev->freezer) {
939 WARN_ON(atomic_read(&sdiodev->freezer->freezing));
940 kfree(sdiodev->freezer);
944 static int brcmf_sdiod_freezer_on(struct brcmf_sdio_dev *sdiodev)
946 atomic_t *expect = &sdiodev->freezer->thread_count;
949 sdiodev->freezer->frozen_count = 0;
950 reinit_completion(&sdiodev->freezer->resumed);
951 atomic_set(&sdiodev->freezer->freezing, 1);
952 brcmf_sdio_trigger_dpc(sdiodev->bus);
953 wait_event(sdiodev->freezer->thread_freeze,
954 atomic_read(expect) == sdiodev->freezer->frozen_count);
955 sdio_claim_host(sdiodev->func[1]);
956 res = brcmf_sdio_sleep(sdiodev->bus, true);
957 sdio_release_host(sdiodev->func[1]);
961 static void brcmf_sdiod_freezer_off(struct brcmf_sdio_dev *sdiodev)
963 sdio_claim_host(sdiodev->func[1]);
964 brcmf_sdio_sleep(sdiodev->bus, false);
965 sdio_release_host(sdiodev->func[1]);
966 atomic_set(&sdiodev->freezer->freezing, 0);
967 complete_all(&sdiodev->freezer->resumed);
970 bool brcmf_sdiod_freezing(struct brcmf_sdio_dev *sdiodev)
972 return atomic_read(&sdiodev->freezer->freezing);
975 void brcmf_sdiod_try_freeze(struct brcmf_sdio_dev *sdiodev)
977 if (!brcmf_sdiod_freezing(sdiodev))
979 sdiodev->freezer->frozen_count++;
980 wake_up(&sdiodev->freezer->thread_freeze);
981 wait_for_completion(&sdiodev->freezer->resumed);
984 void brcmf_sdiod_freezer_count(struct brcmf_sdio_dev *sdiodev)
986 atomic_inc(&sdiodev->freezer->thread_count);
989 void brcmf_sdiod_freezer_uncount(struct brcmf_sdio_dev *sdiodev)
991 atomic_dec(&sdiodev->freezer->thread_count);
994 static int brcmf_sdiod_freezer_attach(struct brcmf_sdio_dev *sdiodev)
999 static void brcmf_sdiod_freezer_detach(struct brcmf_sdio_dev *sdiodev)
1002 #endif /* CONFIG_PM_SLEEP */
1004 static int brcmf_sdiod_remove(struct brcmf_sdio_dev *sdiodev)
1006 sdiodev->state = BRCMF_SDIOD_DOWN;
1008 brcmf_sdio_remove(sdiodev->bus);
1009 sdiodev->bus = NULL;
1012 brcmf_sdiod_freezer_detach(sdiodev);
1014 /* Disable Function 2 */
1015 sdio_claim_host(sdiodev->func[2]);
1016 sdio_disable_func(sdiodev->func[2]);
1017 sdio_release_host(sdiodev->func[2]);
1019 /* Disable Function 1 */
1020 sdio_claim_host(sdiodev->func[1]);
1021 sdio_disable_func(sdiodev->func[1]);
1022 sdio_release_host(sdiodev->func[1]);
1024 sg_free_table(&sdiodev->sgtable);
1027 pm_runtime_allow(sdiodev->func[1]->card->host->parent);
1031 static void brcmf_sdiod_host_fixup(struct mmc_host *host)
1033 /* runtime-pm powers off the device */
1034 pm_runtime_forbid(host->parent);
1035 /* avoid removal detection upon resume */
1036 host->caps |= MMC_CAP_NONREMOVABLE;
1039 static int brcmf_sdiod_probe(struct brcmf_sdio_dev *sdiodev)
1043 sdiodev->num_funcs = 2;
1045 sdio_claim_host(sdiodev->func[1]);
1047 ret = sdio_set_block_size(sdiodev->func[1], SDIO_FUNC1_BLOCKSIZE);
1049 brcmf_err("Failed to set F1 blocksize\n");
1050 sdio_release_host(sdiodev->func[1]);
1053 ret = sdio_set_block_size(sdiodev->func[2], SDIO_FUNC2_BLOCKSIZE);
1055 brcmf_err("Failed to set F2 blocksize\n");
1056 sdio_release_host(sdiodev->func[1]);
1060 /* increase F2 timeout */
1061 sdiodev->func[2]->enable_timeout = SDIO_WAIT_F2RDY;
1063 /* Enable Function 1 */
1064 ret = sdio_enable_func(sdiodev->func[1]);
1065 sdio_release_host(sdiodev->func[1]);
1067 brcmf_err("Failed to enable F1: err=%d\n", ret);
1071 ret = brcmf_sdiod_freezer_attach(sdiodev);
1075 /* try to attach to the target device */
1076 sdiodev->bus = brcmf_sdio_probe(sdiodev);
1077 if (!sdiodev->bus) {
1081 brcmf_sdiod_host_fixup(sdiodev->func[2]->card->host);
1084 brcmf_sdiod_remove(sdiodev);
1089 #define BRCMF_SDIO_DEVICE(dev_id) \
1090 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, dev_id)}
1092 /* devices we support, null terminated */
1093 static const struct sdio_device_id brcmf_sdmmc_ids[] = {
1094 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_43143),
1095 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_43241),
1096 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_4329),
1097 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_4330),
1098 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_4334),
1099 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_43340),
1100 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_43341),
1101 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_43362),
1102 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_43364),
1103 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_4335_4339),
1104 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_4339),
1105 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_43430),
1106 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_4345),
1107 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_4354),
1108 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_4356),
1109 { /* end: all zeroes */ }
1111 MODULE_DEVICE_TABLE(sdio, brcmf_sdmmc_ids);
1114 static void brcmf_sdiod_acpi_set_power_manageable(struct device *dev,
1117 #if IS_ENABLED(CONFIG_ACPI)
1118 struct acpi_device *adev;
1120 adev = ACPI_COMPANION(dev);
1122 adev->flags.power_manageable = 0;
1126 static int brcmf_ops_sdio_probe(struct sdio_func *func,
1127 const struct sdio_device_id *id)
1130 struct brcmf_sdio_dev *sdiodev;
1131 struct brcmf_bus *bus_if;
1134 brcmf_dbg(SDIO, "Enter\n");
1135 brcmf_dbg(SDIO, "Class=%x\n", func->class);
1136 brcmf_dbg(SDIO, "sdio vendor ID: 0x%04x\n", func->vendor);
1137 brcmf_dbg(SDIO, "sdio device ID: 0x%04x\n", func->device);
1138 brcmf_dbg(SDIO, "Function#: %d\n", func->num);
1141 /* prohibit ACPI power management for this device */
1142 brcmf_sdiod_acpi_set_power_manageable(dev, 0);
1144 /* Consume func num 1 but dont do anything with it. */
1148 /* Ignore anything but func 2 */
1152 bus_if = kzalloc(sizeof(struct brcmf_bus), GFP_KERNEL);
1155 sdiodev = kzalloc(sizeof(struct brcmf_sdio_dev), GFP_KERNEL);
1161 /* store refs to functions used. mmc_card does
1162 * not hold the F0 function pointer.
1164 sdiodev->func[0] = kmemdup(func, sizeof(*func), GFP_KERNEL);
1165 sdiodev->func[0]->num = 0;
1166 sdiodev->func[1] = func->card->sdio_func[0];
1167 sdiodev->func[2] = func;
1169 sdiodev->bus_if = bus_if;
1170 bus_if->bus_priv.sdio = sdiodev;
1171 bus_if->proto_type = BRCMF_PROTO_BCDC;
1172 dev_set_drvdata(&func->dev, bus_if);
1173 dev_set_drvdata(&sdiodev->func[1]->dev, bus_if);
1174 sdiodev->dev = &sdiodev->func[1]->dev;
1176 brcmf_sdiod_change_state(sdiodev, BRCMF_SDIOD_DOWN);
1178 brcmf_dbg(SDIO, "F2 found, calling brcmf_sdiod_probe...\n");
1179 err = brcmf_sdiod_probe(sdiodev);
1181 brcmf_err("F2 error, probe failed %d...\n", err);
1185 brcmf_dbg(SDIO, "F2 init completed...\n");
1189 dev_set_drvdata(&func->dev, NULL);
1190 dev_set_drvdata(&sdiodev->func[1]->dev, NULL);
1191 kfree(sdiodev->func[0]);
1197 static void brcmf_ops_sdio_remove(struct sdio_func *func)
1199 struct brcmf_bus *bus_if;
1200 struct brcmf_sdio_dev *sdiodev;
1202 brcmf_dbg(SDIO, "Enter\n");
1203 brcmf_dbg(SDIO, "sdio vendor ID: 0x%04x\n", func->vendor);
1204 brcmf_dbg(SDIO, "sdio device ID: 0x%04x\n", func->device);
1205 brcmf_dbg(SDIO, "Function: %d\n", func->num);
1207 bus_if = dev_get_drvdata(&func->dev);
1209 sdiodev = bus_if->bus_priv.sdio;
1211 /* start by unregistering irqs */
1212 brcmf_sdiod_intr_unregister(sdiodev);
1217 /* only proceed with rest of cleanup if func 1 */
1218 brcmf_sdiod_remove(sdiodev);
1220 dev_set_drvdata(&sdiodev->func[1]->dev, NULL);
1221 dev_set_drvdata(&sdiodev->func[2]->dev, NULL);
1224 kfree(sdiodev->func[0]);
1228 brcmf_dbg(SDIO, "Exit\n");
1231 void brcmf_sdio_wowl_config(struct device *dev, bool enabled)
1233 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1234 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
1236 brcmf_dbg(SDIO, "Configuring WOWL, enabled=%d\n", enabled);
1237 sdiodev->wowl_enabled = enabled;
1240 #ifdef CONFIG_PM_SLEEP
1241 static int brcmf_ops_sdio_suspend(struct device *dev)
1243 struct sdio_func *func;
1244 struct brcmf_bus *bus_if;
1245 struct brcmf_sdio_dev *sdiodev;
1246 mmc_pm_flag_t sdio_flags;
1248 func = container_of(dev, struct sdio_func, dev);
1249 brcmf_dbg(SDIO, "Enter: F%d\n", func->num);
1250 if (func->num != SDIO_FUNC_1)
1254 bus_if = dev_get_drvdata(dev);
1255 sdiodev = bus_if->bus_priv.sdio;
1257 brcmf_sdiod_freezer_on(sdiodev);
1258 brcmf_sdio_wd_timer(sdiodev->bus, 0);
1260 sdio_flags = MMC_PM_KEEP_POWER;
1261 if (sdiodev->wowl_enabled) {
1262 if (sdiodev->settings->bus.sdio.oob_irq_supported)
1263 enable_irq_wake(sdiodev->settings->bus.sdio.oob_irq_nr);
1265 sdio_flags |= MMC_PM_WAKE_SDIO_IRQ;
1267 if (sdio_set_host_pm_flags(sdiodev->func[1], sdio_flags))
1268 brcmf_err("Failed to set pm_flags %x\n", sdio_flags);
1272 static int brcmf_ops_sdio_resume(struct device *dev)
1274 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1275 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
1276 struct sdio_func *func = container_of(dev, struct sdio_func, dev);
1278 brcmf_dbg(SDIO, "Enter: F%d\n", func->num);
1279 if (func->num != SDIO_FUNC_2)
1282 brcmf_sdiod_freezer_off(sdiodev);
1286 static const struct dev_pm_ops brcmf_sdio_pm_ops = {
1287 .suspend = brcmf_ops_sdio_suspend,
1288 .resume = brcmf_ops_sdio_resume,
1290 #endif /* CONFIG_PM_SLEEP */
1292 static struct sdio_driver brcmf_sdmmc_driver = {
1293 .probe = brcmf_ops_sdio_probe,
1294 .remove = brcmf_ops_sdio_remove,
1295 .name = KBUILD_MODNAME,
1296 .id_table = brcmf_sdmmc_ids,
1298 .owner = THIS_MODULE,
1299 #ifdef CONFIG_PM_SLEEP
1300 .pm = &brcmf_sdio_pm_ops,
1301 #endif /* CONFIG_PM_SLEEP */
1305 void brcmf_sdio_register(void)
1309 ret = sdio_register_driver(&brcmf_sdmmc_driver);
1311 brcmf_err("sdio_register_driver failed: %d\n", ret);
1314 void brcmf_sdio_exit(void)
1316 brcmf_dbg(SDIO, "Enter\n");
1318 sdio_unregister_driver(&brcmf_sdmmc_driver);