2 * Marvell BT-over-SDIO driver: SDIO interface related functions.
4 * Copyright (C) 2009, Marvell International Ltd.
6 * This software file (the "File") is distributed by Marvell International
7 * Ltd. under the terms of the GNU General Public License Version 2, June 1991
8 * (the "License"). You may use, redistribute and/or modify this File in
9 * accordance with the terms and conditions of the License, a copy of which
10 * is available by writing to the Free Software Foundation, Inc.,
11 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
12 * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
15 * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
17 * ARE EXPRESSLY DISCLAIMED. The License provides additional details about
18 * this warranty disclaimer.
21 #include <linux/firmware.h>
22 #include <linux/slab.h>
24 #include <linux/mmc/sdio_ids.h>
25 #include <linux/mmc/sdio_func.h>
26 #include <linux/module.h>
28 #include <net/bluetooth/bluetooth.h>
29 #include <net/bluetooth/hci_core.h>
31 #include "btmrvl_drv.h"
32 #include "btmrvl_sdio.h"
36 /* The btmrvl_sdio_remove() callback function is called
37 * when user removes this module from kernel space or ejects
38 * the card from the slot. The driver handles these 2 cases
40 * If the user is removing the module, a MODULE_SHUTDOWN_REQ
41 * command is sent to firmware and interrupt will be disabled.
42 * If the card is removed, there is no need to send command
43 * or disable interrupt.
45 * The variable 'user_rmmod' is used to distinguish these two
46 * scenarios. This flag is initialized as FALSE in case the card
47 * is removed, and will be set to TRUE for module removal when
48 * module_exit function is called.
53 static const struct btmrvl_sdio_card_reg btmrvl_reg_8688
= {
55 .host_int_mask
= 0x04,
56 .host_intstatus
= 0x05,
58 .sq_read_base_addr_a0
= 0x10,
59 .sq_read_base_addr_a1
= 0x11,
60 .card_fw_status0
= 0x40,
61 .card_fw_status1
= 0x41,
67 .int_read_to_clear
= false,
69 static const struct btmrvl_sdio_card_reg btmrvl_reg_87xx
= {
71 .host_int_mask
= 0x02,
72 .host_intstatus
= 0x03,
74 .sq_read_base_addr_a0
= 0x40,
75 .sq_read_base_addr_a1
= 0x41,
76 .card_revision
= 0x5c,
77 .card_fw_status0
= 0x60,
78 .card_fw_status1
= 0x61,
84 .int_read_to_clear
= false,
87 static const struct btmrvl_sdio_card_reg btmrvl_reg_88xx
= {
89 .host_int_mask
= 0x02,
90 .host_intstatus
= 0x03,
92 .sq_read_base_addr_a0
= 0x60,
93 .sq_read_base_addr_a1
= 0x61,
94 .card_revision
= 0xbc,
95 .card_fw_status0
= 0xc0,
96 .card_fw_status1
= 0xc1,
102 .int_read_to_clear
= true,
103 .host_int_rsr
= 0x01,
104 .card_misc_cfg
= 0xcc,
107 static const struct btmrvl_sdio_device btmrvl_sdio_sd8688
= {
108 .helper
= "mrvl/sd8688_helper.bin",
109 .firmware
= "mrvl/sd8688.bin",
110 .reg
= &btmrvl_reg_8688
,
111 .sd_blksz_fw_dl
= 64,
114 static const struct btmrvl_sdio_device btmrvl_sdio_sd8787
= {
116 .firmware
= "mrvl/sd8787_uapsta.bin",
117 .reg
= &btmrvl_reg_87xx
,
118 .sd_blksz_fw_dl
= 256,
121 static const struct btmrvl_sdio_device btmrvl_sdio_sd8797
= {
123 .firmware
= "mrvl/sd8797_uapsta.bin",
124 .reg
= &btmrvl_reg_87xx
,
125 .sd_blksz_fw_dl
= 256,
128 static const struct btmrvl_sdio_device btmrvl_sdio_sd8897
= {
130 .firmware
= "mrvl/sd8897_uapsta.bin",
131 .reg
= &btmrvl_reg_88xx
,
132 .sd_blksz_fw_dl
= 256,
135 static const struct sdio_device_id btmrvl_sdio_ids
[] = {
136 /* Marvell SD8688 Bluetooth device */
137 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL
, 0x9105),
138 .driver_data
= (unsigned long) &btmrvl_sdio_sd8688
},
139 /* Marvell SD8787 Bluetooth device */
140 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL
, 0x911A),
141 .driver_data
= (unsigned long) &btmrvl_sdio_sd8787
},
142 /* Marvell SD8787 Bluetooth AMP device */
143 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL
, 0x911B),
144 .driver_data
= (unsigned long) &btmrvl_sdio_sd8787
},
145 /* Marvell SD8797 Bluetooth device */
146 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL
, 0x912A),
147 .driver_data
= (unsigned long) &btmrvl_sdio_sd8797
},
148 /* Marvell SD8897 Bluetooth device */
149 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL
, 0x912E),
150 .driver_data
= (unsigned long) &btmrvl_sdio_sd8897
},
152 { } /* Terminating entry */
155 MODULE_DEVICE_TABLE(sdio
, btmrvl_sdio_ids
);
157 static int btmrvl_sdio_get_rx_unit(struct btmrvl_sdio_card
*card
)
162 reg
= sdio_readb(card
->func
, card
->reg
->card_rx_unit
, &ret
);
169 static int btmrvl_sdio_read_fw_status(struct btmrvl_sdio_card
*card
, u16
*dat
)
176 fws0
= sdio_readb(card
->func
, card
->reg
->card_fw_status0
, &ret
);
180 fws1
= sdio_readb(card
->func
, card
->reg
->card_fw_status1
, &ret
);
184 *dat
= (((u16
) fws1
) << 8) | fws0
;
189 static int btmrvl_sdio_read_rx_len(struct btmrvl_sdio_card
*card
, u16
*dat
)
194 reg
= sdio_readb(card
->func
, card
->reg
->card_rx_len
, &ret
);
196 *dat
= (u16
) reg
<< card
->rx_unit
;
201 static int btmrvl_sdio_enable_host_int_mask(struct btmrvl_sdio_card
*card
,
206 sdio_writeb(card
->func
, mask
, card
->reg
->host_int_mask
, &ret
);
208 BT_ERR("Unable to enable the host interrupt!");
215 static int btmrvl_sdio_disable_host_int_mask(struct btmrvl_sdio_card
*card
,
221 host_int_mask
= sdio_readb(card
->func
, card
->reg
->host_int_mask
, &ret
);
225 host_int_mask
&= ~mask
;
227 sdio_writeb(card
->func
, host_int_mask
, card
->reg
->host_int_mask
, &ret
);
229 BT_ERR("Unable to disable the host interrupt!");
236 static int btmrvl_sdio_poll_card_status(struct btmrvl_sdio_card
*card
, u8 bits
)
242 for (tries
= 0; tries
< MAX_POLL_TRIES
* 1000; tries
++) {
243 status
= sdio_readb(card
->func
, card
->reg
->card_status
, &ret
);
246 if ((status
& bits
) == bits
)
255 BT_ERR("FAILED! ret=%d", ret
);
260 static int btmrvl_sdio_verify_fw_download(struct btmrvl_sdio_card
*card
,
266 /* Wait for firmware to become ready */
267 for (tries
= 0; tries
< pollnum
; tries
++) {
268 sdio_claim_host(card
->func
);
269 ret
= btmrvl_sdio_read_fw_status(card
, &firmwarestat
);
270 sdio_release_host(card
->func
);
274 if (firmwarestat
== FIRMWARE_READY
)
283 static int btmrvl_sdio_download_helper(struct btmrvl_sdio_card
*card
)
285 const struct firmware
*fw_helper
= NULL
;
286 const u8
*helper
= NULL
;
288 void *tmphlprbuf
= NULL
;
289 int tmphlprbufsz
, hlprblknow
, helperlen
;
293 ret
= request_firmware(&fw_helper
, card
->helper
,
295 if ((ret
< 0) || !fw_helper
) {
296 BT_ERR("request_firmware(helper) failed, error code = %d",
302 helper
= fw_helper
->data
;
303 helperlen
= fw_helper
->size
;
305 BT_DBG("Downloading helper image (%d bytes), block size %d bytes",
306 helperlen
, SDIO_BLOCK_SIZE
);
308 tmphlprbufsz
= ALIGN_SZ(BTM_UPLD_SIZE
, BTSDIO_DMA_ALIGN
);
310 tmphlprbuf
= kzalloc(tmphlprbufsz
, GFP_KERNEL
);
312 BT_ERR("Unable to allocate buffer for helper."
313 " Terminating download");
318 helperbuf
= (u8
*) ALIGN_ADDR(tmphlprbuf
, BTSDIO_DMA_ALIGN
);
320 /* Perform helper data transfer */
321 tx_len
= (FIRMWARE_TRANSFER_NBLOCK
* SDIO_BLOCK_SIZE
)
326 ret
= btmrvl_sdio_poll_card_status(card
,
327 CARD_IO_READY
| DN_LD_CARD_RDY
);
329 BT_ERR("Helper download poll status timeout @ %d",
334 /* Check if there is more data? */
335 if (hlprblknow
>= helperlen
)
338 if (helperlen
- hlprblknow
< tx_len
)
339 tx_len
= helperlen
- hlprblknow
;
342 helperbuf
[0] = ((tx_len
& 0x000000ff) >> 0);
343 helperbuf
[1] = ((tx_len
& 0x0000ff00) >> 8);
344 helperbuf
[2] = ((tx_len
& 0x00ff0000) >> 16);
345 helperbuf
[3] = ((tx_len
& 0xff000000) >> 24);
347 memcpy(&helperbuf
[SDIO_HEADER_LEN
], &helper
[hlprblknow
],
350 /* Now send the data */
351 ret
= sdio_writesb(card
->func
, card
->ioport
, helperbuf
,
352 FIRMWARE_TRANSFER_NBLOCK
* SDIO_BLOCK_SIZE
);
354 BT_ERR("IO error during helper download @ %d",
359 hlprblknow
+= tx_len
;
362 BT_DBG("Transferring helper image EOF block");
364 memset(helperbuf
, 0x0, SDIO_BLOCK_SIZE
);
366 ret
= sdio_writesb(card
->func
, card
->ioport
, helperbuf
,
369 BT_ERR("IO error in writing helper image EOF block");
377 release_firmware(fw_helper
);
381 static int btmrvl_sdio_download_fw_w_helper(struct btmrvl_sdio_card
*card
)
383 const struct firmware
*fw_firmware
= NULL
;
384 const u8
*firmware
= NULL
;
385 int firmwarelen
, tmpfwbufsz
, ret
;
386 unsigned int tries
, offset
;
388 void *tmpfwbuf
= NULL
;
390 u16 len
, blksz_dl
= card
->sd_blksz_fw_dl
;
391 int txlen
= 0, tx_blocks
= 0, count
= 0;
393 ret
= request_firmware(&fw_firmware
, card
->firmware
,
395 if ((ret
< 0) || !fw_firmware
) {
396 BT_ERR("request_firmware(firmware) failed, error code = %d",
402 firmware
= fw_firmware
->data
;
403 firmwarelen
= fw_firmware
->size
;
405 BT_DBG("Downloading FW image (%d bytes)", firmwarelen
);
407 tmpfwbufsz
= ALIGN_SZ(BTM_UPLD_SIZE
, BTSDIO_DMA_ALIGN
);
408 tmpfwbuf
= kzalloc(tmpfwbufsz
, GFP_KERNEL
);
410 BT_ERR("Unable to allocate buffer for firmware."
411 " Terminating download");
416 /* Ensure aligned firmware buffer */
417 fwbuf
= (u8
*) ALIGN_ADDR(tmpfwbuf
, BTSDIO_DMA_ALIGN
);
419 /* Perform firmware data transfer */
422 ret
= btmrvl_sdio_poll_card_status(card
,
423 CARD_IO_READY
| DN_LD_CARD_RDY
);
425 BT_ERR("FW download with helper poll status"
426 " timeout @ %d", offset
);
430 /* Check if there is more data ? */
431 if (offset
>= firmwarelen
)
434 for (tries
= 0; tries
< MAX_POLL_TRIES
; tries
++) {
435 base0
= sdio_readb(card
->func
,
436 card
->reg
->sq_read_base_addr_a0
, &ret
);
438 BT_ERR("BASE0 register read failed:"
439 " base0 = 0x%04X(%d)."
440 " Terminating download",
445 base1
= sdio_readb(card
->func
,
446 card
->reg
->sq_read_base_addr_a1
, &ret
);
448 BT_ERR("BASE1 register read failed:"
449 " base1 = 0x%04X(%d)."
450 " Terminating download",
456 len
= (((u16
) base1
) << 8) | base0
;
465 else if (len
> BTM_UPLD_SIZE
) {
466 BT_ERR("FW download failure @%d, invalid length %d",
476 if (count
> MAX_WRITE_IOMEM_RETRY
) {
477 BT_ERR("FW download failure @%d, "
478 "over max retry count", offset
);
482 BT_ERR("FW CRC error indicated by the helper: "
483 "len = 0x%04X, txlen = %d", len
, txlen
);
485 /* Set txlen to 0 so as to resend from same offset */
491 if (firmwarelen
- offset
< txlen
)
492 txlen
= firmwarelen
- offset
;
494 tx_blocks
= DIV_ROUND_UP(txlen
, blksz_dl
);
496 memcpy(fwbuf
, &firmware
[offset
], txlen
);
499 ret
= sdio_writesb(card
->func
, card
->ioport
, fwbuf
,
500 tx_blocks
* blksz_dl
);
503 BT_ERR("FW download, writesb(%d) failed @%d",
505 sdio_writeb(card
->func
, HOST_CMD53_FIN
,
506 card
->reg
->cfg
, &ret
);
508 BT_ERR("writeb failed (CFG)");
514 BT_DBG("FW download over, size %d bytes", offset
);
520 release_firmware(fw_firmware
);
524 static int btmrvl_sdio_card_to_host(struct btmrvl_private
*priv
)
527 int ret
, num_blocks
, blksz
;
528 struct sk_buff
*skb
= NULL
;
531 struct hci_dev
*hdev
= priv
->btmrvl_dev
.hcidev
;
532 struct btmrvl_sdio_card
*card
= priv
->btmrvl_dev
.card
;
534 if (!card
|| !card
->func
) {
535 BT_ERR("card or function is NULL!");
540 /* Read the length of data to be transferred */
541 ret
= btmrvl_sdio_read_rx_len(card
, &buf_len
);
543 BT_ERR("read rx_len failed");
548 blksz
= SDIO_BLOCK_SIZE
;
549 num_blocks
= DIV_ROUND_UP(buf_len
, blksz
);
551 if (buf_len
<= SDIO_HEADER_LEN
552 || (num_blocks
* blksz
) > ALLOC_BUF_SIZE
) {
553 BT_ERR("invalid packet length: %d", buf_len
);
558 /* Allocate buffer */
559 skb
= bt_skb_alloc(num_blocks
* blksz
+ BTSDIO_DMA_ALIGN
, GFP_ATOMIC
);
561 BT_ERR("No free skb");
566 if ((unsigned long) skb
->data
& (BTSDIO_DMA_ALIGN
- 1)) {
567 skb_put(skb
, (unsigned long) skb
->data
&
568 (BTSDIO_DMA_ALIGN
- 1));
569 skb_pull(skb
, (unsigned long) skb
->data
&
570 (BTSDIO_DMA_ALIGN
- 1));
575 ret
= sdio_readsb(card
->func
, payload
, card
->ioport
,
578 BT_ERR("readsb failed: %d", ret
);
583 /* This is SDIO specific header length: byte[2][1][0], type: byte[3]
584 * (HCI_COMMAND = 1, ACL_DATA = 2, SCO_DATA = 3, 0xFE = Vendor)
587 buf_len
= payload
[0];
588 buf_len
|= payload
[1] << 8;
589 buf_len
|= payload
[2] << 16;
591 if (buf_len
> blksz
* num_blocks
) {
592 BT_ERR("Skip incorrect packet: hdrlen %d buffer %d",
593 buf_len
, blksz
* num_blocks
);
601 case HCI_ACLDATA_PKT
:
602 case HCI_SCODATA_PKT
:
604 bt_cb(skb
)->pkt_type
= type
;
605 skb_put(skb
, buf_len
);
606 skb_pull(skb
, SDIO_HEADER_LEN
);
608 if (type
== HCI_EVENT_PKT
) {
609 if (btmrvl_check_evtpkt(priv
, skb
))
610 hci_recv_frame(hdev
, skb
);
612 hci_recv_frame(hdev
, skb
);
615 hdev
->stat
.byte_rx
+= buf_len
;
618 case MRVL_VENDOR_PKT
:
619 bt_cb(skb
)->pkt_type
= HCI_VENDOR_PKT
;
620 skb_put(skb
, buf_len
);
621 skb_pull(skb
, SDIO_HEADER_LEN
);
623 if (btmrvl_process_event(priv
, skb
))
624 hci_recv_frame(hdev
, skb
);
626 hdev
->stat
.byte_rx
+= buf_len
;
630 BT_ERR("Unknown packet type:%d", type
);
631 BT_ERR("hex: %*ph", blksz
* num_blocks
, payload
);
647 static int btmrvl_sdio_process_int_status(struct btmrvl_private
*priv
)
651 struct btmrvl_sdio_card
*card
= priv
->btmrvl_dev
.card
;
653 spin_lock_irqsave(&priv
->driver_lock
, flags
);
656 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
658 sdio_claim_host(card
->func
);
659 if (ireg
& DN_LD_HOST_INT_STATUS
) {
660 if (priv
->btmrvl_dev
.tx_dnld_rdy
)
661 BT_DBG("tx_done already received: "
662 " int_status=0x%x", ireg
);
664 priv
->btmrvl_dev
.tx_dnld_rdy
= true;
667 if (ireg
& UP_LD_HOST_INT_STATUS
)
668 btmrvl_sdio_card_to_host(priv
);
670 sdio_release_host(card
->func
);
675 static int btmrvl_sdio_read_to_clear(struct btmrvl_sdio_card
*card
, u8
*ireg
)
677 struct btmrvl_adapter
*adapter
= card
->priv
->adapter
;
680 ret
= sdio_readsb(card
->func
, adapter
->hw_regs
, 0, SDIO_BLOCK_SIZE
);
682 BT_ERR("sdio_readsb: read int hw_regs failed: %d", ret
);
686 *ireg
= adapter
->hw_regs
[card
->reg
->host_intstatus
];
687 BT_DBG("hw_regs[%#x]=%#x", card
->reg
->host_intstatus
, *ireg
);
692 static int btmrvl_sdio_write_to_clear(struct btmrvl_sdio_card
*card
, u8
*ireg
)
696 *ireg
= sdio_readb(card
->func
, card
->reg
->host_intstatus
, &ret
);
698 BT_ERR("sdio_readb: read int status failed: %d", ret
);
704 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
705 * Clear the interrupt status register and re-enable the
708 BT_DBG("int_status = 0x%x", *ireg
);
710 sdio_writeb(card
->func
, ~(*ireg
) & (DN_LD_HOST_INT_STATUS
|
711 UP_LD_HOST_INT_STATUS
),
712 card
->reg
->host_intstatus
, &ret
);
714 BT_ERR("sdio_writeb: clear int status failed: %d", ret
);
722 static void btmrvl_sdio_interrupt(struct sdio_func
*func
)
724 struct btmrvl_private
*priv
;
725 struct btmrvl_sdio_card
*card
;
730 card
= sdio_get_drvdata(func
);
731 if (!card
|| !card
->priv
) {
732 BT_ERR("sbi_interrupt(%p) card or priv is "
733 "NULL, card=%p\n", func
, card
);
739 if (card
->reg
->int_read_to_clear
)
740 ret
= btmrvl_sdio_read_to_clear(card
, &ireg
);
742 ret
= btmrvl_sdio_write_to_clear(card
, &ireg
);
747 spin_lock_irqsave(&priv
->driver_lock
, flags
);
749 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
751 btmrvl_interrupt(priv
);
754 static int btmrvl_sdio_register_dev(struct btmrvl_sdio_card
*card
)
756 struct sdio_func
*func
;
760 if (!card
|| !card
->func
) {
761 BT_ERR("Error: card or function is NULL!");
768 sdio_claim_host(func
);
770 ret
= sdio_enable_func(func
);
772 BT_ERR("sdio_enable_func() failed: ret=%d", ret
);
777 ret
= sdio_claim_irq(func
, btmrvl_sdio_interrupt
);
779 BT_ERR("sdio_claim_irq failed: ret=%d", ret
);
784 ret
= sdio_set_block_size(card
->func
, SDIO_BLOCK_SIZE
);
786 BT_ERR("cannot set SDIO block size");
791 reg
= sdio_readb(func
, card
->reg
->io_port_0
, &ret
);
799 reg
= sdio_readb(func
, card
->reg
->io_port_1
, &ret
);
805 card
->ioport
|= (reg
<< 8);
807 reg
= sdio_readb(func
, card
->reg
->io_port_2
, &ret
);
813 card
->ioport
|= (reg
<< 16);
815 BT_DBG("SDIO FUNC%d IO port: 0x%x", func
->num
, card
->ioport
);
817 if (card
->reg
->int_read_to_clear
) {
818 reg
= sdio_readb(func
, card
->reg
->host_int_rsr
, &ret
);
823 sdio_writeb(func
, reg
| 0x3f, card
->reg
->host_int_rsr
, &ret
);
829 reg
= sdio_readb(func
, card
->reg
->card_misc_cfg
, &ret
);
834 sdio_writeb(func
, reg
| 0x10, card
->reg
->card_misc_cfg
, &ret
);
841 sdio_set_drvdata(func
, card
);
843 sdio_release_host(func
);
848 sdio_release_irq(func
);
851 sdio_disable_func(func
);
854 sdio_release_host(func
);
860 static int btmrvl_sdio_unregister_dev(struct btmrvl_sdio_card
*card
)
862 if (card
&& card
->func
) {
863 sdio_claim_host(card
->func
);
864 sdio_release_irq(card
->func
);
865 sdio_disable_func(card
->func
);
866 sdio_release_host(card
->func
);
867 sdio_set_drvdata(card
->func
, NULL
);
873 static int btmrvl_sdio_enable_host_int(struct btmrvl_sdio_card
*card
)
877 if (!card
|| !card
->func
)
880 sdio_claim_host(card
->func
);
882 ret
= btmrvl_sdio_enable_host_int_mask(card
, HIM_ENABLE
);
884 btmrvl_sdio_get_rx_unit(card
);
886 sdio_release_host(card
->func
);
891 static int btmrvl_sdio_disable_host_int(struct btmrvl_sdio_card
*card
)
895 if (!card
|| !card
->func
)
898 sdio_claim_host(card
->func
);
900 ret
= btmrvl_sdio_disable_host_int_mask(card
, HIM_DISABLE
);
902 sdio_release_host(card
->func
);
907 static int btmrvl_sdio_host_to_card(struct btmrvl_private
*priv
,
910 struct btmrvl_sdio_card
*card
= priv
->btmrvl_dev
.card
;
919 if (!card
|| !card
->func
) {
920 BT_ERR("card or function is NULL!");
925 if ((unsigned long) payload
& (BTSDIO_DMA_ALIGN
- 1)) {
926 tmpbufsz
= ALIGN_SZ(nb
, BTSDIO_DMA_ALIGN
);
927 tmpbuf
= kzalloc(tmpbufsz
, GFP_KERNEL
);
930 buf
= (u8
*) ALIGN_ADDR(tmpbuf
, BTSDIO_DMA_ALIGN
);
931 memcpy(buf
, payload
, nb
);
934 blksz
= SDIO_BLOCK_SIZE
;
935 buf_block_len
= DIV_ROUND_UP(nb
, blksz
);
937 sdio_claim_host(card
->func
);
940 /* Transfer data to card */
941 ret
= sdio_writesb(card
->func
, card
->ioport
, buf
,
942 buf_block_len
* blksz
);
945 BT_ERR("i=%d writesb failed: %d", i
, ret
);
946 BT_ERR("hex: %*ph", nb
, payload
);
948 if (i
> MAX_WRITE_IOMEM_RETRY
)
953 priv
->btmrvl_dev
.tx_dnld_rdy
= false;
956 sdio_release_host(card
->func
);
962 static int btmrvl_sdio_download_fw(struct btmrvl_sdio_card
*card
)
966 int pollnum
= MAX_POLL_TRIES
;
968 if (!card
|| !card
->func
) {
969 BT_ERR("card or function is NULL!");
973 if (!btmrvl_sdio_verify_fw_download(card
, 1)) {
974 BT_DBG("Firmware already downloaded!");
978 sdio_claim_host(card
->func
);
980 /* Check if other function driver is downloading the firmware */
981 fws0
= sdio_readb(card
->func
, card
->reg
->card_fw_status0
, &ret
);
983 BT_ERR("Failed to read FW downloading status!");
988 BT_DBG("BT not the winner (%#x). Skip FW downloading", fws0
);
990 /* Give other function more time to download the firmware */
994 ret
= btmrvl_sdio_download_helper(card
);
996 BT_ERR("Failed to download helper!");
1002 if (btmrvl_sdio_download_fw_w_helper(card
)) {
1003 BT_ERR("Failed to download firmware!");
1009 sdio_release_host(card
->func
);
1012 * winner or not, with this test the FW synchronizes when the
1013 * module can continue its initialization
1015 if (btmrvl_sdio_verify_fw_download(card
, pollnum
)) {
1016 BT_ERR("FW failed to be active in time!");
1023 sdio_release_host(card
->func
);
1027 static int btmrvl_sdio_wakeup_fw(struct btmrvl_private
*priv
)
1029 struct btmrvl_sdio_card
*card
= priv
->btmrvl_dev
.card
;
1032 if (!card
|| !card
->func
) {
1033 BT_ERR("card or function is NULL!");
1037 sdio_claim_host(card
->func
);
1039 sdio_writeb(card
->func
, HOST_POWER_UP
, card
->reg
->cfg
, &ret
);
1041 sdio_release_host(card
->func
);
1043 BT_DBG("wake up firmware");
1048 static int btmrvl_sdio_probe(struct sdio_func
*func
,
1049 const struct sdio_device_id
*id
)
1052 struct btmrvl_private
*priv
= NULL
;
1053 struct btmrvl_sdio_card
*card
= NULL
;
1055 BT_INFO("vendor=0x%x, device=0x%x, class=%d, fn=%d",
1056 id
->vendor
, id
->device
, id
->class, func
->num
);
1058 card
= devm_kzalloc(&func
->dev
, sizeof(*card
), GFP_KERNEL
);
1064 if (id
->driver_data
) {
1065 struct btmrvl_sdio_device
*data
= (void *) id
->driver_data
;
1066 card
->helper
= data
->helper
;
1067 card
->firmware
= data
->firmware
;
1068 card
->reg
= data
->reg
;
1069 card
->sd_blksz_fw_dl
= data
->sd_blksz_fw_dl
;
1072 if (btmrvl_sdio_register_dev(card
) < 0) {
1073 BT_ERR("Failed to register BT device!");
1077 /* Disable the interrupts on the card */
1078 btmrvl_sdio_disable_host_int(card
);
1080 if (btmrvl_sdio_download_fw(card
)) {
1081 BT_ERR("Downloading firmware failed!");
1086 btmrvl_sdio_enable_host_int(card
);
1088 priv
= btmrvl_add_card(card
);
1090 BT_ERR("Initializing card failed!");
1092 goto disable_host_int
;
1097 /* Initialize the interface specific function pointers */
1098 priv
->hw_host_to_card
= btmrvl_sdio_host_to_card
;
1099 priv
->hw_wakeup_firmware
= btmrvl_sdio_wakeup_fw
;
1100 priv
->hw_process_int_status
= btmrvl_sdio_process_int_status
;
1102 if (btmrvl_register_hdev(priv
)) {
1103 BT_ERR("Register hdev failed!");
1105 goto disable_host_int
;
1111 btmrvl_sdio_disable_host_int(card
);
1113 btmrvl_sdio_unregister_dev(card
);
1117 static void btmrvl_sdio_remove(struct sdio_func
*func
)
1119 struct btmrvl_sdio_card
*card
;
1122 card
= sdio_get_drvdata(func
);
1124 /* Send SHUTDOWN command & disable interrupt
1125 * if user removes the module.
1128 btmrvl_send_module_cfg_cmd(card
->priv
,
1129 MODULE_SHUTDOWN_REQ
);
1130 btmrvl_sdio_disable_host_int(card
);
1132 BT_DBG("unregester dev");
1133 btmrvl_sdio_unregister_dev(card
);
1134 btmrvl_remove_card(card
->priv
);
1139 static int btmrvl_sdio_suspend(struct device
*dev
)
1141 struct sdio_func
*func
= dev_to_sdio_func(dev
);
1142 struct btmrvl_sdio_card
*card
;
1143 struct btmrvl_private
*priv
;
1144 mmc_pm_flag_t pm_flags
;
1145 struct hci_dev
*hcidev
;
1148 pm_flags
= sdio_get_host_pm_caps(func
);
1149 BT_DBG("%s: suspend: PM flags = 0x%x", sdio_func_id(func
),
1151 if (!(pm_flags
& MMC_PM_KEEP_POWER
)) {
1152 BT_ERR("%s: cannot remain alive while suspended",
1153 sdio_func_id(func
));
1156 card
= sdio_get_drvdata(func
);
1157 if (!card
|| !card
->priv
) {
1158 BT_ERR("card or priv structure is not valid");
1162 BT_ERR("sdio_func is not specified");
1168 if (priv
->adapter
->hs_state
!= HS_ACTIVATED
) {
1169 if (btmrvl_enable_hs(priv
)) {
1170 BT_ERR("HS not actived, suspend failed!");
1174 hcidev
= priv
->btmrvl_dev
.hcidev
;
1175 BT_DBG("%s: SDIO suspend", hcidev
->name
);
1176 hci_suspend_dev(hcidev
);
1177 skb_queue_purge(&priv
->adapter
->tx_queue
);
1179 priv
->adapter
->is_suspended
= true;
1181 /* We will keep the power when hs enabled successfully */
1182 if (priv
->adapter
->hs_state
== HS_ACTIVATED
) {
1183 BT_DBG("suspend with MMC_PM_KEEP_POWER");
1184 return sdio_set_host_pm_flags(func
, MMC_PM_KEEP_POWER
);
1186 BT_DBG("suspend without MMC_PM_KEEP_POWER");
1191 static int btmrvl_sdio_resume(struct device
*dev
)
1193 struct sdio_func
*func
= dev_to_sdio_func(dev
);
1194 struct btmrvl_sdio_card
*card
;
1195 struct btmrvl_private
*priv
;
1196 mmc_pm_flag_t pm_flags
;
1197 struct hci_dev
*hcidev
;
1200 pm_flags
= sdio_get_host_pm_caps(func
);
1201 BT_DBG("%s: resume: PM flags = 0x%x", sdio_func_id(func
),
1203 card
= sdio_get_drvdata(func
);
1204 if (!card
|| !card
->priv
) {
1205 BT_ERR("card or priv structure is not valid");
1209 BT_ERR("sdio_func is not specified");
1214 if (!priv
->adapter
->is_suspended
) {
1215 BT_DBG("device already resumed");
1219 priv
->adapter
->is_suspended
= false;
1220 hcidev
= priv
->btmrvl_dev
.hcidev
;
1221 BT_DBG("%s: SDIO resume", hcidev
->name
);
1222 hci_resume_dev(hcidev
);
1223 priv
->hw_wakeup_firmware(priv
);
1224 priv
->adapter
->hs_state
= HS_DEACTIVATED
;
1225 BT_DBG("%s: HS DEACTIVATED in resume!", hcidev
->name
);
1230 static const struct dev_pm_ops btmrvl_sdio_pm_ops
= {
1231 .suspend
= btmrvl_sdio_suspend
,
1232 .resume
= btmrvl_sdio_resume
,
1235 static struct sdio_driver bt_mrvl_sdio
= {
1236 .name
= "btmrvl_sdio",
1237 .id_table
= btmrvl_sdio_ids
,
1238 .probe
= btmrvl_sdio_probe
,
1239 .remove
= btmrvl_sdio_remove
,
1241 .owner
= THIS_MODULE
,
1242 .pm
= &btmrvl_sdio_pm_ops
,
1246 static int __init
btmrvl_sdio_init_module(void)
1248 if (sdio_register_driver(&bt_mrvl_sdio
) != 0) {
1249 BT_ERR("SDIO Driver Registration Failed");
1253 /* Clear the flag in case user removes the card. */
1259 static void __exit
btmrvl_sdio_exit_module(void)
1261 /* Set the flag as user is removing this module. */
1264 sdio_unregister_driver(&bt_mrvl_sdio
);
1267 module_init(btmrvl_sdio_init_module
);
1268 module_exit(btmrvl_sdio_exit_module
);
1270 MODULE_AUTHOR("Marvell International Ltd.");
1271 MODULE_DESCRIPTION("Marvell BT-over-SDIO driver ver " VERSION
);
1272 MODULE_VERSION(VERSION
);
1273 MODULE_LICENSE("GPL v2");
1274 MODULE_FIRMWARE("mrvl/sd8688_helper.bin");
1275 MODULE_FIRMWARE("mrvl/sd8688.bin");
1276 MODULE_FIRMWARE("mrvl/sd8787_uapsta.bin");
1277 MODULE_FIRMWARE("mrvl/sd8797_uapsta.bin");
1278 MODULE_FIRMWARE("mrvl/sd8897_uapsta.bin");