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"
46 #include "sdio_chip.h"
48 #define SDIOH_API_ACCESS_RETRY_LIMIT 2
50 #define DMA_ALIGN_MASK 0x03
52 #define SDIO_FUNC1_BLOCKSIZE 64
53 #define SDIO_FUNC2_BLOCKSIZE 512
54 /* Maximum milliseconds to wait for F2 to come up */
55 #define SDIO_WAIT_F2RDY 3000
58 static irqreturn_t
brcmf_sdiod_oob_irqhandler(int irq
, void *dev_id
)
60 struct brcmf_bus
*bus_if
= dev_get_drvdata(dev_id
);
61 struct brcmf_sdio_dev
*sdiodev
= bus_if
->bus_priv
.sdio
;
63 brcmf_dbg(INTR
, "OOB intr triggered\n");
65 /* out-of-band interrupt is level-triggered which won't
66 * be cleared until dpc
68 if (sdiodev
->irq_en
) {
69 disable_irq_nosync(irq
);
70 sdiodev
->irq_en
= false;
73 brcmf_sdio_isr(sdiodev
->bus
);
78 static void brcmf_sdiod_ib_irqhandler(struct sdio_func
*func
)
80 struct brcmf_bus
*bus_if
= dev_get_drvdata(&func
->dev
);
81 struct brcmf_sdio_dev
*sdiodev
= bus_if
->bus_priv
.sdio
;
83 brcmf_dbg(INTR
, "IB intr triggered\n");
85 brcmf_sdio_isr(sdiodev
->bus
);
88 /* dummy handler for SDIO function 2 interrupt */
89 static void brcmf_sdiod_dummy_irqhandler(struct sdio_func
*func
)
93 static bool brcmf_sdiod_pm_resume_error(struct brcmf_sdio_dev
*sdiodev
)
96 #ifdef CONFIG_PM_SLEEP
97 is_err
= atomic_read(&sdiodev
->suspend
);
102 static void brcmf_sdiod_pm_resume_wait(struct brcmf_sdio_dev
*sdiodev
,
103 wait_queue_head_t
*wq
)
105 #ifdef CONFIG_PM_SLEEP
107 while (atomic_read(&sdiodev
->suspend
) && retry
++ != 30)
108 wait_event_timeout(*wq
, false, HZ
/100);
112 int brcmf_sdiod_intr_register(struct brcmf_sdio_dev
*sdiodev
)
118 if ((sdiodev
->pdata
) && (sdiodev
->pdata
->oob_irq_supported
)) {
119 brcmf_dbg(SDIO
, "Enter, register OOB IRQ %d\n",
120 sdiodev
->pdata
->oob_irq_nr
);
121 ret
= request_irq(sdiodev
->pdata
->oob_irq_nr
,
122 brcmf_sdiod_oob_irqhandler
,
123 sdiodev
->pdata
->oob_irq_flags
,
125 &sdiodev
->func
[1]->dev
);
127 brcmf_err("request_irq failed %d\n", ret
);
130 sdiodev
->oob_irq_requested
= true;
131 spin_lock_init(&sdiodev
->irq_en_lock
);
132 spin_lock_irqsave(&sdiodev
->irq_en_lock
, flags
);
133 sdiodev
->irq_en
= true;
134 spin_unlock_irqrestore(&sdiodev
->irq_en_lock
, flags
);
136 ret
= enable_irq_wake(sdiodev
->pdata
->oob_irq_nr
);
138 brcmf_err("enable_irq_wake failed %d\n", ret
);
141 sdiodev
->irq_wake
= true;
143 sdio_claim_host(sdiodev
->func
[1]);
145 /* must configure SDIO_CCCR_IENx to enable irq */
146 data
= brcmf_sdiod_regrb(sdiodev
, SDIO_CCCR_IENx
, &ret
);
147 data
|= 1 << SDIO_FUNC_1
| 1 << SDIO_FUNC_2
| 1;
148 brcmf_sdiod_regwb(sdiodev
, SDIO_CCCR_IENx
, data
, &ret
);
150 /* redirect, configure and enable io for interrupt signal */
151 data
= SDIO_SEPINT_MASK
| SDIO_SEPINT_OE
;
152 if (sdiodev
->pdata
->oob_irq_flags
& IRQF_TRIGGER_HIGH
)
153 data
|= SDIO_SEPINT_ACT_HI
;
154 brcmf_sdiod_regwb(sdiodev
, SDIO_CCCR_BRCM_SEPINT
, data
, &ret
);
156 sdio_release_host(sdiodev
->func
[1]);
158 brcmf_dbg(SDIO
, "Entering\n");
159 sdio_claim_host(sdiodev
->func
[1]);
160 sdio_claim_irq(sdiodev
->func
[1], brcmf_sdiod_ib_irqhandler
);
161 sdio_claim_irq(sdiodev
->func
[2], brcmf_sdiod_dummy_irqhandler
);
162 sdio_release_host(sdiodev
->func
[1]);
168 int brcmf_sdiod_intr_unregister(struct brcmf_sdio_dev
*sdiodev
)
170 brcmf_dbg(SDIO
, "Entering\n");
172 if ((sdiodev
->pdata
) && (sdiodev
->pdata
->oob_irq_supported
)) {
173 sdio_claim_host(sdiodev
->func
[1]);
174 brcmf_sdiod_regwb(sdiodev
, SDIO_CCCR_BRCM_SEPINT
, 0, NULL
);
175 brcmf_sdiod_regwb(sdiodev
, SDIO_CCCR_IENx
, 0, NULL
);
176 sdio_release_host(sdiodev
->func
[1]);
178 if (sdiodev
->oob_irq_requested
) {
179 sdiodev
->oob_irq_requested
= false;
180 if (sdiodev
->irq_wake
) {
181 disable_irq_wake(sdiodev
->pdata
->oob_irq_nr
);
182 sdiodev
->irq_wake
= false;
184 free_irq(sdiodev
->pdata
->oob_irq_nr
,
185 &sdiodev
->func
[1]->dev
);
186 sdiodev
->irq_en
= false;
189 sdio_claim_host(sdiodev
->func
[1]);
190 sdio_release_irq(sdiodev
->func
[2]);
191 sdio_release_irq(sdiodev
->func
[1]);
192 sdio_release_host(sdiodev
->func
[1]);
198 static inline int brcmf_sdiod_f0_writeb(struct sdio_func
*func
,
199 uint regaddr
, u8 byte
)
204 * Can only directly write to some F0 registers.
205 * Handle CCCR_IENx and CCCR_ABORT command
208 if ((regaddr
== SDIO_CCCR_ABORT
) ||
209 (regaddr
== SDIO_CCCR_IENx
))
210 sdio_writeb(func
, byte
, regaddr
, &err_ret
);
212 sdio_f0_writeb(func
, byte
, regaddr
, &err_ret
);
217 static int brcmf_sdiod_request_data(struct brcmf_sdio_dev
*sdiodev
, u8 fn
,
218 u32 addr
, u8 regsz
, void *data
, bool write
)
220 struct sdio_func
*func
;
223 brcmf_dbg(SDIO
, "rw=%d, func=%d, addr=0x%05x, nbytes=%d\n",
224 write
, fn
, addr
, regsz
);
226 brcmf_sdiod_pm_resume_wait(sdiodev
, &sdiodev
->request_word_wait
);
227 if (brcmf_sdiod_pm_resume_error(sdiodev
))
230 /* only allow byte access on F0 */
231 if (WARN_ON(regsz
> 1 && !fn
))
233 func
= sdiodev
->func
[fn
];
239 sdio_writeb(func
, *(u8
*)data
, addr
, &ret
);
241 ret
= brcmf_sdiod_f0_writeb(func
, addr
,
245 *(u8
*)data
= sdio_readb(func
, addr
, &ret
);
247 *(u8
*)data
= sdio_f0_readb(func
, addr
, &ret
);
252 sdio_writew(func
, *(u16
*)data
, addr
, &ret
);
254 *(u16
*)data
= sdio_readw(func
, addr
, &ret
);
258 sdio_writel(func
, *(u32
*)data
, addr
, &ret
);
260 *(u32
*)data
= sdio_readl(func
, addr
, &ret
);
263 brcmf_err("invalid size: %d\n", regsz
);
269 * SleepCSR register access can fail when
270 * waking up the device so reduce this noise
273 if (addr
!= SBSDIO_FUNC1_SLEEPCSR
)
274 brcmf_err("failed to %s data F%d@0x%05x, err: %d\n",
275 write
? "write" : "read", fn
, addr
, ret
);
277 brcmf_dbg(SDIO
, "failed to %s data F%d@0x%05x, err: %d\n",
278 write
? "write" : "read", fn
, addr
, ret
);
283 static int brcmf_sdiod_regrw_helper(struct brcmf_sdio_dev
*sdiodev
, u32 addr
,
284 u8 regsz
, void *data
, bool write
)
290 if (sdiodev
->bus_if
->state
== BRCMF_BUS_NOMEDIUM
)
294 * figure out how to read the register based on address range
295 * 0x00 ~ 0x7FF: function 0 CCCR and FBR
296 * 0x10000 ~ 0x1FFFF: function 1 miscellaneous registers
297 * The rest: function 1 silicon backplane core registers
299 if ((addr
& ~REG_F0_REG_MASK
) == 0)
300 func_num
= SDIO_FUNC_0
;
302 func_num
= SDIO_FUNC_1
;
306 memset(data
, 0, regsz
);
307 /* for retry wait for 1 ms till bus get settled down */
309 usleep_range(1000, 2000);
310 ret
= brcmf_sdiod_request_data(sdiodev
, func_num
, addr
, regsz
,
312 } while (ret
!= 0 && ret
!= -ENOMEDIUM
&&
313 retry
++ < SDIOH_API_ACCESS_RETRY_LIMIT
);
315 if (ret
== -ENOMEDIUM
)
316 brcmf_bus_change_state(sdiodev
->bus_if
, BRCMF_BUS_NOMEDIUM
);
318 brcmf_err("failed with %d\n", ret
);
324 brcmf_sdiod_set_sbaddr_window(struct brcmf_sdio_dev
*sdiodev
, u32 address
)
329 if (sdiodev
->bus_if
->state
== BRCMF_BUS_NOMEDIUM
)
332 addr
[0] = (address
>> 8) & SBSDIO_SBADDRLOW_MASK
;
333 addr
[1] = (address
>> 16) & SBSDIO_SBADDRMID_MASK
;
334 addr
[2] = (address
>> 24) & SBSDIO_SBADDRHIGH_MASK
;
336 for (i
= 0; i
< 3; i
++) {
337 err
= brcmf_sdiod_regrw_helper(sdiodev
,
338 SBSDIO_FUNC1_SBADDRLOW
+ i
,
339 sizeof(u8
), &addr
[i
], true);
341 brcmf_err("failed at addr: 0x%0x\n",
342 SBSDIO_FUNC1_SBADDRLOW
+ i
);
351 brcmf_sdiod_addrprep(struct brcmf_sdio_dev
*sdiodev
, uint width
, u32
*addr
)
353 uint bar0
= *addr
& ~SBSDIO_SB_OFT_ADDR_MASK
;
356 if (bar0
!= sdiodev
->sbwad
) {
357 err
= brcmf_sdiod_set_sbaddr_window(sdiodev
, bar0
);
361 sdiodev
->sbwad
= bar0
;
364 *addr
&= SBSDIO_SB_OFT_ADDR_MASK
;
367 *addr
|= SBSDIO_SB_ACCESS_2_4B_FLAG
;
372 u8
brcmf_sdiod_regrb(struct brcmf_sdio_dev
*sdiodev
, u32 addr
, int *ret
)
377 brcmf_dbg(SDIO
, "addr:0x%08x\n", addr
);
378 retval
= brcmf_sdiod_regrw_helper(sdiodev
, addr
, sizeof(data
), &data
,
380 brcmf_dbg(SDIO
, "data:0x%02x\n", data
);
388 u32
brcmf_sdiod_regrl(struct brcmf_sdio_dev
*sdiodev
, u32 addr
, int *ret
)
393 brcmf_dbg(SDIO
, "addr:0x%08x\n", addr
);
394 retval
= brcmf_sdiod_addrprep(sdiodev
, sizeof(data
), &addr
);
397 retval
= brcmf_sdiod_regrw_helper(sdiodev
, addr
, sizeof(data
), &data
,
399 brcmf_dbg(SDIO
, "data:0x%08x\n", data
);
408 void brcmf_sdiod_regwb(struct brcmf_sdio_dev
*sdiodev
, u32 addr
,
413 brcmf_dbg(SDIO
, "addr:0x%08x, data:0x%02x\n", addr
, data
);
414 retval
= brcmf_sdiod_regrw_helper(sdiodev
, addr
, sizeof(data
), &data
,
420 void brcmf_sdiod_regwl(struct brcmf_sdio_dev
*sdiodev
, u32 addr
,
425 brcmf_dbg(SDIO
, "addr:0x%08x, data:0x%08x\n", addr
, data
);
426 retval
= brcmf_sdiod_addrprep(sdiodev
, sizeof(data
), &addr
);
429 retval
= brcmf_sdiod_regrw_helper(sdiodev
, addr
, sizeof(data
), &data
,
437 static int brcmf_sdiod_buffrw(struct brcmf_sdio_dev
*sdiodev
, uint fn
,
438 bool write
, u32 addr
, struct sk_buff
*pkt
)
443 brcmf_sdiod_pm_resume_wait(sdiodev
, &sdiodev
->request_buffer_wait
);
444 if (brcmf_sdiod_pm_resume_error(sdiodev
))
447 /* Single skb use the standard mmc interface */
448 req_sz
= pkt
->len
+ 3;
452 err
= sdio_memcpy_toio(sdiodev
->func
[fn
], addr
,
453 ((u8
*)(pkt
->data
)), req_sz
);
455 err
= sdio_memcpy_fromio(sdiodev
->func
[fn
], ((u8
*)(pkt
->data
)),
458 /* function 2 read is FIFO operation */
459 err
= sdio_readsb(sdiodev
->func
[fn
], ((u8
*)(pkt
->data
)), addr
,
461 if (err
== -ENOMEDIUM
)
462 brcmf_bus_change_state(sdiodev
->bus_if
, BRCMF_BUS_NOMEDIUM
);
467 * brcmf_sdiod_sglist_rw - SDIO interface function for block data access
468 * @sdiodev: brcmfmac sdio device
469 * @fn: SDIO function number
470 * @write: direction flag
471 * @addr: dongle memory address as source/destination
474 * This function takes the respbonsibility as the interface function to MMC
475 * stack for block data access. It assumes that the skb passed down by the
476 * caller has already been padded and aligned.
478 static int brcmf_sdiod_sglist_rw(struct brcmf_sdio_dev
*sdiodev
, uint fn
,
479 bool write
, u32 addr
,
480 struct sk_buff_head
*pktlist
)
482 unsigned int req_sz
, func_blk_sz
, sg_cnt
, sg_data_sz
, pkt_offset
;
483 unsigned int max_req_sz
, orig_offset
, dst_offset
;
484 unsigned short max_seg_cnt
, seg_sz
;
485 unsigned char *pkt_data
, *orig_data
, *dst_data
;
486 struct sk_buff
*pkt_next
= NULL
, *local_pkt_next
;
487 struct sk_buff_head local_list
, *target_list
;
488 struct mmc_request mmc_req
;
489 struct mmc_command mmc_cmd
;
490 struct mmc_data mmc_dat
;
492 struct scatterlist
*sgl
;
498 brcmf_sdiod_pm_resume_wait(sdiodev
, &sdiodev
->request_buffer_wait
);
499 if (brcmf_sdiod_pm_resume_error(sdiodev
))
502 target_list
= pktlist
;
503 /* for host with broken sg support, prepare a page aligned list */
504 __skb_queue_head_init(&local_list
);
505 if (sdiodev
->pdata
&& sdiodev
->pdata
->broken_sg_support
&& !write
) {
507 skb_queue_walk(pktlist
, pkt_next
)
508 req_sz
+= pkt_next
->len
;
509 req_sz
= ALIGN(req_sz
, sdiodev
->func
[fn
]->cur_blksize
);
510 while (req_sz
> PAGE_SIZE
) {
511 pkt_next
= brcmu_pkt_buf_get_skb(PAGE_SIZE
);
512 if (pkt_next
== NULL
) {
516 __skb_queue_tail(&local_list
, pkt_next
);
519 pkt_next
= brcmu_pkt_buf_get_skb(req_sz
);
520 if (pkt_next
== NULL
) {
524 __skb_queue_tail(&local_list
, pkt_next
);
525 target_list
= &local_list
;
528 func_blk_sz
= sdiodev
->func
[fn
]->cur_blksize
;
529 max_req_sz
= sdiodev
->max_request_size
;
530 max_seg_cnt
= min_t(unsigned short, sdiodev
->max_segment_count
,
532 seg_sz
= target_list
->qlen
;
534 pkt_next
= target_list
->next
;
536 if (sg_alloc_table(&st
, max_seg_cnt
, GFP_KERNEL
)) {
541 memset(&mmc_req
, 0, sizeof(struct mmc_request
));
542 memset(&mmc_cmd
, 0, sizeof(struct mmc_command
));
543 memset(&mmc_dat
, 0, sizeof(struct mmc_data
));
546 mmc_dat
.blksz
= func_blk_sz
;
547 mmc_dat
.flags
= write
? MMC_DATA_WRITE
: MMC_DATA_READ
;
548 mmc_cmd
.opcode
= SD_IO_RW_EXTENDED
;
549 mmc_cmd
.arg
= write
? 1<<31 : 0; /* write flag */
550 mmc_cmd
.arg
|= (fn
& 0x7) << 28; /* SDIO func num */
551 mmc_cmd
.arg
|= 1<<27; /* block mode */
552 /* for function 1 the addr will be incremented */
553 mmc_cmd
.arg
|= (fn
== 1) ? 1<<26 : 0;
554 mmc_cmd
.flags
= MMC_RSP_SPI_R5
| MMC_RSP_R5
| MMC_CMD_ADTC
;
555 mmc_req
.cmd
= &mmc_cmd
;
556 mmc_req
.data
= &mmc_dat
;
563 while (pkt_next
!= (struct sk_buff
*)target_list
) {
564 pkt_data
= pkt_next
->data
+ pkt_offset
;
565 sg_data_sz
= pkt_next
->len
- pkt_offset
;
566 if (sg_data_sz
> sdiodev
->max_segment_size
)
567 sg_data_sz
= sdiodev
->max_segment_size
;
568 if (sg_data_sz
> max_req_sz
- req_sz
)
569 sg_data_sz
= max_req_sz
- req_sz
;
571 sg_set_buf(sgl
, pkt_data
, sg_data_sz
);
575 req_sz
+= sg_data_sz
;
576 pkt_offset
+= sg_data_sz
;
577 if (pkt_offset
== pkt_next
->len
) {
579 pkt_next
= pkt_next
->next
;
582 if (req_sz
>= max_req_sz
|| sg_cnt
>= max_seg_cnt
)
587 if (req_sz
% func_blk_sz
!= 0) {
588 brcmf_err("sg request length %u is not %u aligned\n",
589 req_sz
, func_blk_sz
);
594 mmc_dat
.sg_len
= sg_cnt
;
595 mmc_dat
.blocks
= req_sz
/ func_blk_sz
;
596 mmc_cmd
.arg
|= (addr
& 0x1FFFF) << 9; /* address */
597 mmc_cmd
.arg
|= mmc_dat
.blocks
& 0x1FF; /* block count */
598 /* incrementing addr for function 1 */
602 mmc_set_data_timeout(&mmc_dat
, sdiodev
->func
[fn
]->card
);
603 mmc_wait_for_req(sdiodev
->func
[fn
]->card
->host
, &mmc_req
);
605 ret
= mmc_cmd
.error
? mmc_cmd
.error
: mmc_dat
.error
;
606 if (ret
== -ENOMEDIUM
) {
607 brcmf_bus_change_state(sdiodev
->bus_if
,
610 } else if (ret
!= 0) {
611 brcmf_err("CMD53 sg block %s failed %d\n",
612 write
? "write" : "read", ret
);
618 if (sdiodev
->pdata
&& sdiodev
->pdata
->broken_sg_support
&& !write
) {
619 local_pkt_next
= local_list
.next
;
621 skb_queue_walk(pktlist
, pkt_next
) {
624 req_sz
= local_pkt_next
->len
- orig_offset
;
625 req_sz
= min_t(uint
, pkt_next
->len
- dst_offset
,
627 orig_data
= local_pkt_next
->data
+ orig_offset
;
628 dst_data
= pkt_next
->data
+ dst_offset
;
629 memcpy(dst_data
, orig_data
, req_sz
);
630 orig_offset
+= req_sz
;
631 dst_offset
+= req_sz
;
632 if (orig_offset
== local_pkt_next
->len
) {
634 local_pkt_next
= local_pkt_next
->next
;
636 if (dst_offset
== pkt_next
->len
)
638 } while (!skb_queue_empty(&local_list
));
644 while ((pkt_next
= __skb_dequeue(&local_list
)) != NULL
)
645 brcmu_pkt_buf_free_skb(pkt_next
);
650 int brcmf_sdiod_recv_buf(struct brcmf_sdio_dev
*sdiodev
, u8
*buf
, uint nbytes
)
652 struct sk_buff
*mypkt
;
655 mypkt
= brcmu_pkt_buf_get_skb(nbytes
);
657 brcmf_err("brcmu_pkt_buf_get_skb failed: len %d\n",
662 err
= brcmf_sdiod_recv_pkt(sdiodev
, mypkt
);
664 memcpy(buf
, mypkt
->data
, nbytes
);
666 brcmu_pkt_buf_free_skb(mypkt
);
670 int brcmf_sdiod_recv_pkt(struct brcmf_sdio_dev
*sdiodev
, struct sk_buff
*pkt
)
672 u32 addr
= sdiodev
->sbwad
;
675 brcmf_dbg(SDIO
, "addr = 0x%x, size = %d\n", addr
, pkt
->len
);
677 err
= brcmf_sdiod_addrprep(sdiodev
, 4, &addr
);
681 err
= brcmf_sdiod_buffrw(sdiodev
, SDIO_FUNC_2
, false, addr
, pkt
);
687 int brcmf_sdiod_recv_chain(struct brcmf_sdio_dev
*sdiodev
,
688 struct sk_buff_head
*pktq
, uint totlen
)
690 struct sk_buff
*glom_skb
;
692 u32 addr
= sdiodev
->sbwad
;
695 brcmf_dbg(SDIO
, "addr = 0x%x, size = %d\n",
698 err
= brcmf_sdiod_addrprep(sdiodev
, 4, &addr
);
703 err
= brcmf_sdiod_buffrw(sdiodev
, SDIO_FUNC_2
, false, addr
,
705 else if (!sdiodev
->sg_support
) {
706 glom_skb
= brcmu_pkt_buf_get_skb(totlen
);
709 err
= brcmf_sdiod_buffrw(sdiodev
, SDIO_FUNC_2
, false, addr
,
714 skb_queue_walk(pktq
, skb
) {
715 memcpy(skb
->data
, glom_skb
->data
, skb
->len
);
716 skb_pull(glom_skb
, skb
->len
);
719 err
= brcmf_sdiod_sglist_rw(sdiodev
, SDIO_FUNC_2
, false, addr
,
726 int brcmf_sdiod_send_buf(struct brcmf_sdio_dev
*sdiodev
, u8
*buf
, uint nbytes
)
728 struct sk_buff
*mypkt
;
729 u32 addr
= sdiodev
->sbwad
;
732 mypkt
= brcmu_pkt_buf_get_skb(nbytes
);
734 brcmf_err("brcmu_pkt_buf_get_skb failed: len %d\n",
739 memcpy(mypkt
->data
, buf
, nbytes
);
741 err
= brcmf_sdiod_addrprep(sdiodev
, 4, &addr
);
744 err
= brcmf_sdiod_buffrw(sdiodev
, SDIO_FUNC_2
, true, addr
,
747 brcmu_pkt_buf_free_skb(mypkt
);
752 int brcmf_sdiod_send_pkt(struct brcmf_sdio_dev
*sdiodev
,
753 struct sk_buff_head
*pktq
)
756 u32 addr
= sdiodev
->sbwad
;
759 brcmf_dbg(SDIO
, "addr = 0x%x, size = %d\n", addr
, pktq
->qlen
);
761 err
= brcmf_sdiod_addrprep(sdiodev
, 4, &addr
);
765 if (pktq
->qlen
== 1 || !sdiodev
->sg_support
)
766 skb_queue_walk(pktq
, skb
) {
767 err
= brcmf_sdiod_buffrw(sdiodev
, SDIO_FUNC_2
, true,
773 err
= brcmf_sdiod_sglist_rw(sdiodev
, SDIO_FUNC_2
, true, addr
,
780 brcmf_sdiod_ramrw(struct brcmf_sdio_dev
*sdiodev
, bool write
, u32 address
,
788 dsize
= min_t(uint
, SBSDIO_SB_OFT_ADDR_LIMIT
, size
);
789 pkt
= dev_alloc_skb(dsize
);
791 brcmf_err("dev_alloc_skb failed: len %d\n", dsize
);
796 /* Determine initial transfer parameters */
797 sdaddr
= address
& SBSDIO_SB_OFT_ADDR_MASK
;
798 if ((sdaddr
+ size
) & SBSDIO_SBWINDOW_MASK
)
799 dsize
= (SBSDIO_SB_OFT_ADDR_LIMIT
- sdaddr
);
803 sdio_claim_host(sdiodev
->func
[1]);
805 /* Do the transfer(s) */
807 /* Set the backplane window to include the start address */
808 bcmerror
= brcmf_sdiod_set_sbaddr_window(sdiodev
, address
);
812 brcmf_dbg(SDIO
, "%s %d bytes at offset 0x%08x in window 0x%08x\n",
813 write
? "write" : "read", dsize
,
814 sdaddr
, address
& SBSDIO_SBWINDOW_MASK
);
816 sdaddr
&= SBSDIO_SB_OFT_ADDR_MASK
;
817 sdaddr
|= SBSDIO_SB_ACCESS_2_4B_FLAG
;
821 memcpy(pkt
->data
, data
, dsize
);
822 bcmerror
= brcmf_sdiod_buffrw(sdiodev
, SDIO_FUNC_1
, write
,
825 brcmf_err("membytes transfer failed\n");
829 memcpy(data
, pkt
->data
, dsize
);
830 skb_trim(pkt
, dsize
);
832 /* Adjust for next transfer (if any) */
838 dsize
= min_t(uint
, SBSDIO_SB_OFT_ADDR_LIMIT
, size
);
844 /* Return the window to backplane enumeration space for core access */
845 if (brcmf_sdiod_set_sbaddr_window(sdiodev
, sdiodev
->sbwad
))
846 brcmf_err("FAILED to set window back to 0x%x\n",
849 sdio_release_host(sdiodev
->func
[1]);
854 int brcmf_sdiod_abort(struct brcmf_sdio_dev
*sdiodev
, uint fn
)
856 char t_func
= (char)fn
;
857 brcmf_dbg(SDIO
, "Enter\n");
859 /* issue abort cmd52 command through F0 */
860 brcmf_sdiod_request_data(sdiodev
, SDIO_FUNC_0
, SDIO_CCCR_ABORT
,
861 sizeof(t_func
), &t_func
, true);
863 brcmf_dbg(SDIO
, "Exit\n");
867 static int brcmf_sdiod_remove(struct brcmf_sdio_dev
*sdiodev
)
870 brcmf_sdio_remove(sdiodev
->bus
);
874 /* Disable Function 2 */
875 sdio_claim_host(sdiodev
->func
[2]);
876 sdio_disable_func(sdiodev
->func
[2]);
877 sdio_release_host(sdiodev
->func
[2]);
879 /* Disable Function 1 */
880 sdio_claim_host(sdiodev
->func
[1]);
881 sdio_disable_func(sdiodev
->func
[1]);
882 sdio_release_host(sdiodev
->func
[1]);
889 static int brcmf_sdiod_probe(struct brcmf_sdio_dev
*sdiodev
)
891 struct sdio_func
*func
;
892 struct mmc_host
*host
;
896 sdiodev
->num_funcs
= 2;
898 sdio_claim_host(sdiodev
->func
[1]);
900 ret
= sdio_set_block_size(sdiodev
->func
[1], SDIO_FUNC1_BLOCKSIZE
);
902 brcmf_err("Failed to set F1 blocksize\n");
903 sdio_release_host(sdiodev
->func
[1]);
906 ret
= sdio_set_block_size(sdiodev
->func
[2], SDIO_FUNC2_BLOCKSIZE
);
908 brcmf_err("Failed to set F2 blocksize\n");
909 sdio_release_host(sdiodev
->func
[1]);
913 /* increase F2 timeout */
914 sdiodev
->func
[2]->enable_timeout
= SDIO_WAIT_F2RDY
;
916 /* Enable Function 1 */
917 ret
= sdio_enable_func(sdiodev
->func
[1]);
918 sdio_release_host(sdiodev
->func
[1]);
920 brcmf_err("Failed to enable F1: err=%d\n", ret
);
925 * determine host related variables after brcmf_sdiod_probe()
926 * as func->cur_blksize is properly set and F2 init has been
927 * completed successfully.
929 func
= sdiodev
->func
[2];
930 host
= func
->card
->host
;
931 sdiodev
->sg_support
= host
->max_segs
> 1;
932 max_blocks
= min_t(uint
, host
->max_blk_count
, 511u);
933 sdiodev
->max_request_size
= min_t(uint
, host
->max_req_size
,
934 max_blocks
* func
->cur_blksize
);
935 sdiodev
->max_segment_count
= min_t(uint
, host
->max_segs
,
936 SG_MAX_SINGLE_ALLOC
);
937 sdiodev
->max_segment_size
= host
->max_seg_size
;
939 /* try to attach to the target device */
940 sdiodev
->bus
= brcmf_sdio_probe(sdiodev
);
948 brcmf_sdiod_remove(sdiodev
);
953 /* devices we support, null terminated */
954 static const struct sdio_device_id brcmf_sdmmc_ids
[] = {
955 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM
, SDIO_DEVICE_ID_BROADCOM_43143
)},
956 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM
, SDIO_DEVICE_ID_BROADCOM_43241
)},
957 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM
, SDIO_DEVICE_ID_BROADCOM_4329
)},
958 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM
, SDIO_DEVICE_ID_BROADCOM_4330
)},
959 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM
, SDIO_DEVICE_ID_BROADCOM_4334
)},
960 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM
, SDIO_DEVICE_ID_BROADCOM_43362
)},
961 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM
,
962 SDIO_DEVICE_ID_BROADCOM_4335_4339
)},
963 { /* end: all zeroes */ },
965 MODULE_DEVICE_TABLE(sdio
, brcmf_sdmmc_ids
);
967 static struct brcmfmac_sdio_platform_data
*brcmfmac_sdio_pdata
;
970 static int brcmf_ops_sdio_probe(struct sdio_func
*func
,
971 const struct sdio_device_id
*id
)
974 struct brcmf_sdio_dev
*sdiodev
;
975 struct brcmf_bus
*bus_if
;
977 brcmf_dbg(SDIO
, "Enter\n");
978 brcmf_dbg(SDIO
, "Class=%x\n", func
->class);
979 brcmf_dbg(SDIO
, "sdio vendor ID: 0x%04x\n", func
->vendor
);
980 brcmf_dbg(SDIO
, "sdio device ID: 0x%04x\n", func
->device
);
981 brcmf_dbg(SDIO
, "Function#: %d\n", func
->num
);
983 /* Consume func num 1 but dont do anything with it. */
987 /* Ignore anything but func 2 */
991 bus_if
= kzalloc(sizeof(struct brcmf_bus
), GFP_KERNEL
);
994 sdiodev
= kzalloc(sizeof(struct brcmf_sdio_dev
), GFP_KERNEL
);
1000 /* store refs to functions used. mmc_card does
1001 * not hold the F0 function pointer.
1003 sdiodev
->func
[0] = kmemdup(func
, sizeof(*func
), GFP_KERNEL
);
1004 sdiodev
->func
[0]->num
= 0;
1005 sdiodev
->func
[1] = func
->card
->sdio_func
[0];
1006 sdiodev
->func
[2] = func
;
1008 sdiodev
->bus_if
= bus_if
;
1009 bus_if
->bus_priv
.sdio
= sdiodev
;
1010 bus_if
->proto_type
= BRCMF_PROTO_BCDC
;
1011 dev_set_drvdata(&func
->dev
, bus_if
);
1012 dev_set_drvdata(&sdiodev
->func
[1]->dev
, bus_if
);
1013 sdiodev
->dev
= &sdiodev
->func
[1]->dev
;
1014 sdiodev
->pdata
= brcmfmac_sdio_pdata
;
1016 atomic_set(&sdiodev
->suspend
, false);
1017 init_waitqueue_head(&sdiodev
->request_word_wait
);
1018 init_waitqueue_head(&sdiodev
->request_buffer_wait
);
1020 brcmf_dbg(SDIO
, "F2 found, calling brcmf_sdiod_probe...\n");
1021 err
= brcmf_sdiod_probe(sdiodev
);
1023 brcmf_err("F2 error, probe failed %d...\n", err
);
1027 brcmf_dbg(SDIO
, "F2 init completed...\n");
1031 dev_set_drvdata(&func
->dev
, NULL
);
1032 dev_set_drvdata(&sdiodev
->func
[1]->dev
, NULL
);
1033 kfree(sdiodev
->func
[0]);
1039 static void brcmf_ops_sdio_remove(struct sdio_func
*func
)
1041 struct brcmf_bus
*bus_if
;
1042 struct brcmf_sdio_dev
*sdiodev
;
1044 brcmf_dbg(SDIO
, "Enter\n");
1045 brcmf_dbg(SDIO
, "sdio vendor ID: 0x%04x\n", func
->vendor
);
1046 brcmf_dbg(SDIO
, "sdio device ID: 0x%04x\n", func
->device
);
1047 brcmf_dbg(SDIO
, "Function: %d\n", func
->num
);
1049 if (func
->num
!= 1 && func
->num
!= 2)
1052 bus_if
= dev_get_drvdata(&func
->dev
);
1054 sdiodev
= bus_if
->bus_priv
.sdio
;
1055 brcmf_sdiod_remove(sdiodev
);
1057 dev_set_drvdata(&sdiodev
->func
[1]->dev
, NULL
);
1058 dev_set_drvdata(&sdiodev
->func
[2]->dev
, NULL
);
1061 kfree(sdiodev
->func
[0]);
1065 brcmf_dbg(SDIO
, "Exit\n");
1068 #ifdef CONFIG_PM_SLEEP
1069 static int brcmf_ops_sdio_suspend(struct device
*dev
)
1071 mmc_pm_flag_t sdio_flags
;
1072 struct brcmf_bus
*bus_if
= dev_get_drvdata(dev
);
1073 struct brcmf_sdio_dev
*sdiodev
= bus_if
->bus_priv
.sdio
;
1076 brcmf_dbg(SDIO
, "\n");
1078 atomic_set(&sdiodev
->suspend
, true);
1080 sdio_flags
= sdio_get_host_pm_caps(sdiodev
->func
[1]);
1081 if (!(sdio_flags
& MMC_PM_KEEP_POWER
)) {
1082 brcmf_err("Host can't keep power while suspended\n");
1086 ret
= sdio_set_host_pm_flags(sdiodev
->func
[1], MMC_PM_KEEP_POWER
);
1088 brcmf_err("Failed to set pm_flags\n");
1092 brcmf_sdio_wd_timer(sdiodev
->bus
, 0);
1097 static int brcmf_ops_sdio_resume(struct device
*dev
)
1099 struct brcmf_bus
*bus_if
= dev_get_drvdata(dev
);
1100 struct brcmf_sdio_dev
*sdiodev
= bus_if
->bus_priv
.sdio
;
1102 brcmf_sdio_wd_timer(sdiodev
->bus
, BRCMF_WD_POLL_MS
);
1103 atomic_set(&sdiodev
->suspend
, false);
1107 static const struct dev_pm_ops brcmf_sdio_pm_ops
= {
1108 .suspend
= brcmf_ops_sdio_suspend
,
1109 .resume
= brcmf_ops_sdio_resume
,
1111 #endif /* CONFIG_PM_SLEEP */
1113 static struct sdio_driver brcmf_sdmmc_driver
= {
1114 .probe
= brcmf_ops_sdio_probe
,
1115 .remove
= brcmf_ops_sdio_remove
,
1116 .name
= BRCMFMAC_SDIO_PDATA_NAME
,
1117 .id_table
= brcmf_sdmmc_ids
,
1118 #ifdef CONFIG_PM_SLEEP
1120 .pm
= &brcmf_sdio_pm_ops
,
1122 #endif /* CONFIG_PM_SLEEP */
1125 static int brcmf_sdio_pd_probe(struct platform_device
*pdev
)
1127 brcmf_dbg(SDIO
, "Enter\n");
1129 brcmfmac_sdio_pdata
= dev_get_platdata(&pdev
->dev
);
1131 if (brcmfmac_sdio_pdata
->power_on
)
1132 brcmfmac_sdio_pdata
->power_on();
1137 static int brcmf_sdio_pd_remove(struct platform_device
*pdev
)
1139 brcmf_dbg(SDIO
, "Enter\n");
1141 if (brcmfmac_sdio_pdata
->power_off
)
1142 brcmfmac_sdio_pdata
->power_off();
1144 sdio_unregister_driver(&brcmf_sdmmc_driver
);
1149 static struct platform_driver brcmf_sdio_pd
= {
1150 .remove
= brcmf_sdio_pd_remove
,
1152 .name
= BRCMFMAC_SDIO_PDATA_NAME
,
1153 .owner
= THIS_MODULE
,
1157 void brcmf_sdio_register(void)
1161 ret
= sdio_register_driver(&brcmf_sdmmc_driver
);
1163 brcmf_err("sdio_register_driver failed: %d\n", ret
);
1166 void brcmf_sdio_exit(void)
1168 brcmf_dbg(SDIO
, "Enter\n");
1170 if (brcmfmac_sdio_pdata
)
1171 platform_driver_unregister(&brcmf_sdio_pd
);
1173 sdio_unregister_driver(&brcmf_sdmmc_driver
);
1176 void __init
brcmf_sdio_init(void)
1180 brcmf_dbg(SDIO
, "Enter\n");
1182 ret
= platform_driver_probe(&brcmf_sdio_pd
, brcmf_sdio_pd_probe
);
1184 brcmf_dbg(SDIO
, "No platform data available.\n");