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,
68 static const struct btmrvl_sdio_card_reg btmrvl_reg_87xx
= {
70 .host_int_mask
= 0x02,
71 .host_intstatus
= 0x03,
73 .sq_read_base_addr_a0
= 0x40,
74 .sq_read_base_addr_a1
= 0x41,
75 .card_revision
= 0x5c,
76 .card_fw_status0
= 0x60,
77 .card_fw_status1
= 0x61,
85 static const struct btmrvl_sdio_card_reg btmrvl_reg_88xx
= {
87 .host_int_mask
= 0x02,
88 .host_intstatus
= 0x03,
90 .sq_read_base_addr_a0
= 0x60,
91 .sq_read_base_addr_a1
= 0x61,
92 .card_revision
= 0xbc,
93 .card_fw_status0
= 0xc0,
94 .card_fw_status1
= 0xc1,
102 static const struct btmrvl_sdio_device btmrvl_sdio_sd8688
= {
103 .helper
= "mrvl/sd8688_helper.bin",
104 .firmware
= "mrvl/sd8688.bin",
105 .reg
= &btmrvl_reg_8688
,
106 .sd_blksz_fw_dl
= 64,
109 static const struct btmrvl_sdio_device btmrvl_sdio_sd8787
= {
111 .firmware
= "mrvl/sd8787_uapsta.bin",
112 .reg
= &btmrvl_reg_87xx
,
113 .sd_blksz_fw_dl
= 256,
116 static const struct btmrvl_sdio_device btmrvl_sdio_sd8797
= {
118 .firmware
= "mrvl/sd8797_uapsta.bin",
119 .reg
= &btmrvl_reg_87xx
,
120 .sd_blksz_fw_dl
= 256,
123 static const struct btmrvl_sdio_device btmrvl_sdio_sd8897
= {
125 .firmware
= "mrvl/sd8897_uapsta.bin",
126 .reg
= &btmrvl_reg_88xx
,
127 .sd_blksz_fw_dl
= 256,
130 static const struct sdio_device_id btmrvl_sdio_ids
[] = {
131 /* Marvell SD8688 Bluetooth device */
132 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL
, 0x9105),
133 .driver_data
= (unsigned long) &btmrvl_sdio_sd8688
},
134 /* Marvell SD8787 Bluetooth device */
135 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL
, 0x911A),
136 .driver_data
= (unsigned long) &btmrvl_sdio_sd8787
},
137 /* Marvell SD8787 Bluetooth AMP device */
138 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL
, 0x911B),
139 .driver_data
= (unsigned long) &btmrvl_sdio_sd8787
},
140 /* Marvell SD8797 Bluetooth device */
141 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL
, 0x912A),
142 .driver_data
= (unsigned long) &btmrvl_sdio_sd8797
},
143 /* Marvell SD8897 Bluetooth device */
144 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL
, 0x912E),
145 .driver_data
= (unsigned long) &btmrvl_sdio_sd8897
},
147 { } /* Terminating entry */
150 MODULE_DEVICE_TABLE(sdio
, btmrvl_sdio_ids
);
152 static int btmrvl_sdio_get_rx_unit(struct btmrvl_sdio_card
*card
)
157 reg
= sdio_readb(card
->func
, card
->reg
->card_rx_unit
, &ret
);
164 static int btmrvl_sdio_read_fw_status(struct btmrvl_sdio_card
*card
, u16
*dat
)
171 fws0
= sdio_readb(card
->func
, card
->reg
->card_fw_status0
, &ret
);
175 fws1
= sdio_readb(card
->func
, card
->reg
->card_fw_status1
, &ret
);
179 *dat
= (((u16
) fws1
) << 8) | fws0
;
184 static int btmrvl_sdio_read_rx_len(struct btmrvl_sdio_card
*card
, u16
*dat
)
189 reg
= sdio_readb(card
->func
, card
->reg
->card_rx_len
, &ret
);
191 *dat
= (u16
) reg
<< card
->rx_unit
;
196 static int btmrvl_sdio_enable_host_int_mask(struct btmrvl_sdio_card
*card
,
201 sdio_writeb(card
->func
, mask
, card
->reg
->host_int_mask
, &ret
);
203 BT_ERR("Unable to enable the host interrupt!");
210 static int btmrvl_sdio_disable_host_int_mask(struct btmrvl_sdio_card
*card
,
216 host_int_mask
= sdio_readb(card
->func
, card
->reg
->host_int_mask
, &ret
);
220 host_int_mask
&= ~mask
;
222 sdio_writeb(card
->func
, host_int_mask
, card
->reg
->host_int_mask
, &ret
);
224 BT_ERR("Unable to disable the host interrupt!");
231 static int btmrvl_sdio_poll_card_status(struct btmrvl_sdio_card
*card
, u8 bits
)
237 for (tries
= 0; tries
< MAX_POLL_TRIES
* 1000; tries
++) {
238 status
= sdio_readb(card
->func
, card
->reg
->card_status
, &ret
);
241 if ((status
& bits
) == bits
)
250 BT_ERR("FAILED! ret=%d", ret
);
255 static int btmrvl_sdio_verify_fw_download(struct btmrvl_sdio_card
*card
,
261 /* Wait for firmware to become ready */
262 for (tries
= 0; tries
< pollnum
; tries
++) {
263 sdio_claim_host(card
->func
);
264 ret
= btmrvl_sdio_read_fw_status(card
, &firmwarestat
);
265 sdio_release_host(card
->func
);
269 if (firmwarestat
== FIRMWARE_READY
)
278 static int btmrvl_sdio_download_helper(struct btmrvl_sdio_card
*card
)
280 const struct firmware
*fw_helper
= NULL
;
281 const u8
*helper
= NULL
;
283 void *tmphlprbuf
= NULL
;
284 int tmphlprbufsz
, hlprblknow
, helperlen
;
288 ret
= request_firmware(&fw_helper
, card
->helper
,
290 if ((ret
< 0) || !fw_helper
) {
291 BT_ERR("request_firmware(helper) failed, error code = %d",
297 helper
= fw_helper
->data
;
298 helperlen
= fw_helper
->size
;
300 BT_DBG("Downloading helper image (%d bytes), block size %d bytes",
301 helperlen
, SDIO_BLOCK_SIZE
);
303 tmphlprbufsz
= ALIGN_SZ(BTM_UPLD_SIZE
, BTSDIO_DMA_ALIGN
);
305 tmphlprbuf
= kzalloc(tmphlprbufsz
, GFP_KERNEL
);
307 BT_ERR("Unable to allocate buffer for helper."
308 " Terminating download");
313 helperbuf
= (u8
*) ALIGN_ADDR(tmphlprbuf
, BTSDIO_DMA_ALIGN
);
315 /* Perform helper data transfer */
316 tx_len
= (FIRMWARE_TRANSFER_NBLOCK
* SDIO_BLOCK_SIZE
)
321 ret
= btmrvl_sdio_poll_card_status(card
,
322 CARD_IO_READY
| DN_LD_CARD_RDY
);
324 BT_ERR("Helper download poll status timeout @ %d",
329 /* Check if there is more data? */
330 if (hlprblknow
>= helperlen
)
333 if (helperlen
- hlprblknow
< tx_len
)
334 tx_len
= helperlen
- hlprblknow
;
337 helperbuf
[0] = ((tx_len
& 0x000000ff) >> 0);
338 helperbuf
[1] = ((tx_len
& 0x0000ff00) >> 8);
339 helperbuf
[2] = ((tx_len
& 0x00ff0000) >> 16);
340 helperbuf
[3] = ((tx_len
& 0xff000000) >> 24);
342 memcpy(&helperbuf
[SDIO_HEADER_LEN
], &helper
[hlprblknow
],
345 /* Now send the data */
346 ret
= sdio_writesb(card
->func
, card
->ioport
, helperbuf
,
347 FIRMWARE_TRANSFER_NBLOCK
* SDIO_BLOCK_SIZE
);
349 BT_ERR("IO error during helper download @ %d",
354 hlprblknow
+= tx_len
;
357 BT_DBG("Transferring helper image EOF block");
359 memset(helperbuf
, 0x0, SDIO_BLOCK_SIZE
);
361 ret
= sdio_writesb(card
->func
, card
->ioport
, helperbuf
,
364 BT_ERR("IO error in writing helper image EOF block");
372 release_firmware(fw_helper
);
376 static int btmrvl_sdio_download_fw_w_helper(struct btmrvl_sdio_card
*card
)
378 const struct firmware
*fw_firmware
= NULL
;
379 const u8
*firmware
= NULL
;
380 int firmwarelen
, tmpfwbufsz
, ret
;
381 unsigned int tries
, offset
;
383 void *tmpfwbuf
= NULL
;
385 u16 len
, blksz_dl
= card
->sd_blksz_fw_dl
;
386 int txlen
= 0, tx_blocks
= 0, count
= 0;
388 ret
= request_firmware(&fw_firmware
, card
->firmware
,
390 if ((ret
< 0) || !fw_firmware
) {
391 BT_ERR("request_firmware(firmware) failed, error code = %d",
397 firmware
= fw_firmware
->data
;
398 firmwarelen
= fw_firmware
->size
;
400 BT_DBG("Downloading FW image (%d bytes)", firmwarelen
);
402 tmpfwbufsz
= ALIGN_SZ(BTM_UPLD_SIZE
, BTSDIO_DMA_ALIGN
);
403 tmpfwbuf
= kzalloc(tmpfwbufsz
, GFP_KERNEL
);
405 BT_ERR("Unable to allocate buffer for firmware."
406 " Terminating download");
411 /* Ensure aligned firmware buffer */
412 fwbuf
= (u8
*) ALIGN_ADDR(tmpfwbuf
, BTSDIO_DMA_ALIGN
);
414 /* Perform firmware data transfer */
417 ret
= btmrvl_sdio_poll_card_status(card
,
418 CARD_IO_READY
| DN_LD_CARD_RDY
);
420 BT_ERR("FW download with helper poll status"
421 " timeout @ %d", offset
);
425 /* Check if there is more data ? */
426 if (offset
>= firmwarelen
)
429 for (tries
= 0; tries
< MAX_POLL_TRIES
; tries
++) {
430 base0
= sdio_readb(card
->func
,
431 card
->reg
->sq_read_base_addr_a0
, &ret
);
433 BT_ERR("BASE0 register read failed:"
434 " base0 = 0x%04X(%d)."
435 " Terminating download",
440 base1
= sdio_readb(card
->func
,
441 card
->reg
->sq_read_base_addr_a1
, &ret
);
443 BT_ERR("BASE1 register read failed:"
444 " base1 = 0x%04X(%d)."
445 " Terminating download",
451 len
= (((u16
) base1
) << 8) | base0
;
460 else if (len
> BTM_UPLD_SIZE
) {
461 BT_ERR("FW download failure @%d, invalid length %d",
471 if (count
> MAX_WRITE_IOMEM_RETRY
) {
472 BT_ERR("FW download failure @%d, "
473 "over max retry count", offset
);
477 BT_ERR("FW CRC error indicated by the helper: "
478 "len = 0x%04X, txlen = %d", len
, txlen
);
480 /* Set txlen to 0 so as to resend from same offset */
486 if (firmwarelen
- offset
< txlen
)
487 txlen
= firmwarelen
- offset
;
489 tx_blocks
= (txlen
+ blksz_dl
- 1) / blksz_dl
;
491 memcpy(fwbuf
, &firmware
[offset
], txlen
);
494 ret
= sdio_writesb(card
->func
, card
->ioport
, fwbuf
,
495 tx_blocks
* blksz_dl
);
498 BT_ERR("FW download, writesb(%d) failed @%d",
500 sdio_writeb(card
->func
, HOST_CMD53_FIN
,
501 card
->reg
->cfg
, &ret
);
503 BT_ERR("writeb failed (CFG)");
509 BT_DBG("FW download over, size %d bytes", offset
);
515 release_firmware(fw_firmware
);
519 static int btmrvl_sdio_card_to_host(struct btmrvl_private
*priv
)
522 int ret
, num_blocks
, blksz
;
523 struct sk_buff
*skb
= NULL
;
526 struct hci_dev
*hdev
= priv
->btmrvl_dev
.hcidev
;
527 struct btmrvl_sdio_card
*card
= priv
->btmrvl_dev
.card
;
529 if (!card
|| !card
->func
) {
530 BT_ERR("card or function is NULL!");
535 /* Read the length of data to be transferred */
536 ret
= btmrvl_sdio_read_rx_len(card
, &buf_len
);
538 BT_ERR("read rx_len failed");
543 blksz
= SDIO_BLOCK_SIZE
;
544 num_blocks
= DIV_ROUND_UP(buf_len
, blksz
);
546 if (buf_len
<= SDIO_HEADER_LEN
547 || (num_blocks
* blksz
) > ALLOC_BUF_SIZE
) {
548 BT_ERR("invalid packet length: %d", buf_len
);
553 /* Allocate buffer */
554 skb
= bt_skb_alloc(num_blocks
* blksz
+ BTSDIO_DMA_ALIGN
, GFP_ATOMIC
);
556 BT_ERR("No free skb");
561 if ((unsigned long) skb
->data
& (BTSDIO_DMA_ALIGN
- 1)) {
562 skb_put(skb
, (unsigned long) skb
->data
&
563 (BTSDIO_DMA_ALIGN
- 1));
564 skb_pull(skb
, (unsigned long) skb
->data
&
565 (BTSDIO_DMA_ALIGN
- 1));
570 ret
= sdio_readsb(card
->func
, payload
, card
->ioport
,
573 BT_ERR("readsb failed: %d", ret
);
578 /* This is SDIO specific header length: byte[2][1][0], type: byte[3]
579 * (HCI_COMMAND = 1, ACL_DATA = 2, SCO_DATA = 3, 0xFE = Vendor)
582 buf_len
= payload
[0];
583 buf_len
|= payload
[1] << 8;
584 buf_len
|= payload
[2] << 16;
586 if (buf_len
> blksz
* num_blocks
) {
587 BT_ERR("Skip incorrect packet: hdrlen %d buffer %d",
588 buf_len
, blksz
* num_blocks
);
596 case HCI_ACLDATA_PKT
:
597 case HCI_SCODATA_PKT
:
599 bt_cb(skb
)->pkt_type
= type
;
600 skb
->dev
= (void *)hdev
;
601 skb_put(skb
, buf_len
);
602 skb_pull(skb
, SDIO_HEADER_LEN
);
604 if (type
== HCI_EVENT_PKT
) {
605 if (btmrvl_check_evtpkt(priv
, skb
))
611 hdev
->stat
.byte_rx
+= buf_len
;
614 case MRVL_VENDOR_PKT
:
615 bt_cb(skb
)->pkt_type
= HCI_VENDOR_PKT
;
616 skb
->dev
= (void *)hdev
;
617 skb_put(skb
, buf_len
);
618 skb_pull(skb
, SDIO_HEADER_LEN
);
620 if (btmrvl_process_event(priv
, skb
))
623 hdev
->stat
.byte_rx
+= buf_len
;
627 BT_ERR("Unknown packet type:%d", type
);
628 BT_ERR("hex: %*ph", blksz
* num_blocks
, payload
);
644 static int btmrvl_sdio_process_int_status(struct btmrvl_private
*priv
)
648 struct btmrvl_sdio_card
*card
= priv
->btmrvl_dev
.card
;
650 spin_lock_irqsave(&priv
->driver_lock
, flags
);
653 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
655 sdio_claim_host(card
->func
);
656 if (ireg
& DN_LD_HOST_INT_STATUS
) {
657 if (priv
->btmrvl_dev
.tx_dnld_rdy
)
658 BT_DBG("tx_done already received: "
659 " int_status=0x%x", ireg
);
661 priv
->btmrvl_dev
.tx_dnld_rdy
= true;
664 if (ireg
& UP_LD_HOST_INT_STATUS
)
665 btmrvl_sdio_card_to_host(priv
);
667 sdio_release_host(card
->func
);
672 static void btmrvl_sdio_interrupt(struct sdio_func
*func
)
674 struct btmrvl_private
*priv
;
675 struct btmrvl_sdio_card
*card
;
680 card
= sdio_get_drvdata(func
);
681 if (!card
|| !card
->priv
) {
682 BT_ERR("sbi_interrupt(%p) card or priv is "
683 "NULL, card=%p\n", func
, card
);
689 ireg
= sdio_readb(card
->func
, card
->reg
->host_intstatus
, &ret
);
691 BT_ERR("sdio_readb: read int status register failed");
697 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
698 * Clear the interrupt status register and re-enable the
701 BT_DBG("ireg = 0x%x", ireg
);
703 sdio_writeb(card
->func
, ~(ireg
) & (DN_LD_HOST_INT_STATUS
|
704 UP_LD_HOST_INT_STATUS
),
705 card
->reg
->host_intstatus
, &ret
);
707 BT_ERR("sdio_writeb: clear int status register failed");
712 spin_lock_irqsave(&priv
->driver_lock
, flags
);
714 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
716 btmrvl_interrupt(priv
);
719 static int btmrvl_sdio_register_dev(struct btmrvl_sdio_card
*card
)
721 struct sdio_func
*func
;
725 if (!card
|| !card
->func
) {
726 BT_ERR("Error: card or function is NULL!");
733 sdio_claim_host(func
);
735 ret
= sdio_enable_func(func
);
737 BT_ERR("sdio_enable_func() failed: ret=%d", ret
);
742 ret
= sdio_claim_irq(func
, btmrvl_sdio_interrupt
);
744 BT_ERR("sdio_claim_irq failed: ret=%d", ret
);
749 ret
= sdio_set_block_size(card
->func
, SDIO_BLOCK_SIZE
);
751 BT_ERR("cannot set SDIO block size");
756 reg
= sdio_readb(func
, card
->reg
->io_port_0
, &ret
);
764 reg
= sdio_readb(func
, card
->reg
->io_port_1
, &ret
);
770 card
->ioport
|= (reg
<< 8);
772 reg
= sdio_readb(func
, card
->reg
->io_port_2
, &ret
);
778 card
->ioport
|= (reg
<< 16);
780 BT_DBG("SDIO FUNC%d IO port: 0x%x", func
->num
, card
->ioport
);
782 sdio_set_drvdata(func
, card
);
784 sdio_release_host(func
);
789 sdio_release_irq(func
);
792 sdio_disable_func(func
);
795 sdio_release_host(func
);
801 static int btmrvl_sdio_unregister_dev(struct btmrvl_sdio_card
*card
)
803 if (card
&& card
->func
) {
804 sdio_claim_host(card
->func
);
805 sdio_release_irq(card
->func
);
806 sdio_disable_func(card
->func
);
807 sdio_release_host(card
->func
);
808 sdio_set_drvdata(card
->func
, NULL
);
814 static int btmrvl_sdio_enable_host_int(struct btmrvl_sdio_card
*card
)
818 if (!card
|| !card
->func
)
821 sdio_claim_host(card
->func
);
823 ret
= btmrvl_sdio_enable_host_int_mask(card
, HIM_ENABLE
);
825 btmrvl_sdio_get_rx_unit(card
);
827 sdio_release_host(card
->func
);
832 static int btmrvl_sdio_disable_host_int(struct btmrvl_sdio_card
*card
)
836 if (!card
|| !card
->func
)
839 sdio_claim_host(card
->func
);
841 ret
= btmrvl_sdio_disable_host_int_mask(card
, HIM_DISABLE
);
843 sdio_release_host(card
->func
);
848 static int btmrvl_sdio_host_to_card(struct btmrvl_private
*priv
,
851 struct btmrvl_sdio_card
*card
= priv
->btmrvl_dev
.card
;
860 if (!card
|| !card
->func
) {
861 BT_ERR("card or function is NULL!");
866 if ((unsigned long) payload
& (BTSDIO_DMA_ALIGN
- 1)) {
867 tmpbufsz
= ALIGN_SZ(nb
, BTSDIO_DMA_ALIGN
);
868 tmpbuf
= kzalloc(tmpbufsz
, GFP_KERNEL
);
871 buf
= (u8
*) ALIGN_ADDR(tmpbuf
, BTSDIO_DMA_ALIGN
);
872 memcpy(buf
, payload
, nb
);
875 blksz
= SDIO_BLOCK_SIZE
;
876 buf_block_len
= (nb
+ blksz
- 1) / blksz
;
878 sdio_claim_host(card
->func
);
881 /* Transfer data to card */
882 ret
= sdio_writesb(card
->func
, card
->ioport
, buf
,
883 buf_block_len
* blksz
);
886 BT_ERR("i=%d writesb failed: %d", i
, ret
);
887 BT_ERR("hex: %*ph", nb
, payload
);
889 if (i
> MAX_WRITE_IOMEM_RETRY
)
894 priv
->btmrvl_dev
.tx_dnld_rdy
= false;
897 sdio_release_host(card
->func
);
903 static int btmrvl_sdio_download_fw(struct btmrvl_sdio_card
*card
)
907 int pollnum
= MAX_POLL_TRIES
;
909 if (!card
|| !card
->func
) {
910 BT_ERR("card or function is NULL!");
914 if (!btmrvl_sdio_verify_fw_download(card
, 1)) {
915 BT_DBG("Firmware already downloaded!");
919 sdio_claim_host(card
->func
);
921 /* Check if other function driver is downloading the firmware */
922 fws0
= sdio_readb(card
->func
, card
->reg
->card_fw_status0
, &ret
);
924 BT_ERR("Failed to read FW downloading status!");
929 BT_DBG("BT not the winner (%#x). Skip FW downloading", fws0
);
931 /* Give other function more time to download the firmware */
935 ret
= btmrvl_sdio_download_helper(card
);
937 BT_ERR("Failed to download helper!");
943 if (btmrvl_sdio_download_fw_w_helper(card
)) {
944 BT_ERR("Failed to download firmware!");
950 sdio_release_host(card
->func
);
953 * winner or not, with this test the FW synchronizes when the
954 * module can continue its initialization
956 if (btmrvl_sdio_verify_fw_download(card
, pollnum
)) {
957 BT_ERR("FW failed to be active in time!");
964 sdio_release_host(card
->func
);
968 static int btmrvl_sdio_wakeup_fw(struct btmrvl_private
*priv
)
970 struct btmrvl_sdio_card
*card
= priv
->btmrvl_dev
.card
;
973 if (!card
|| !card
->func
) {
974 BT_ERR("card or function is NULL!");
978 sdio_claim_host(card
->func
);
980 sdio_writeb(card
->func
, HOST_POWER_UP
, card
->reg
->cfg
, &ret
);
982 sdio_release_host(card
->func
);
984 BT_DBG("wake up firmware");
989 static int btmrvl_sdio_probe(struct sdio_func
*func
,
990 const struct sdio_device_id
*id
)
993 struct btmrvl_private
*priv
= NULL
;
994 struct btmrvl_sdio_card
*card
= NULL
;
996 BT_INFO("vendor=0x%x, device=0x%x, class=%d, fn=%d",
997 id
->vendor
, id
->device
, id
->class, func
->num
);
999 card
= devm_kzalloc(&func
->dev
, sizeof(*card
), GFP_KERNEL
);
1005 if (id
->driver_data
) {
1006 struct btmrvl_sdio_device
*data
= (void *) id
->driver_data
;
1007 card
->helper
= data
->helper
;
1008 card
->firmware
= data
->firmware
;
1009 card
->reg
= data
->reg
;
1010 card
->sd_blksz_fw_dl
= data
->sd_blksz_fw_dl
;
1013 if (btmrvl_sdio_register_dev(card
) < 0) {
1014 BT_ERR("Failed to register BT device!");
1018 /* Disable the interrupts on the card */
1019 btmrvl_sdio_disable_host_int(card
);
1021 if (btmrvl_sdio_download_fw(card
)) {
1022 BT_ERR("Downloading firmware failed!");
1027 btmrvl_sdio_enable_host_int(card
);
1029 priv
= btmrvl_add_card(card
);
1031 BT_ERR("Initializing card failed!");
1033 goto disable_host_int
;
1038 /* Initialize the interface specific function pointers */
1039 priv
->hw_host_to_card
= btmrvl_sdio_host_to_card
;
1040 priv
->hw_wakeup_firmware
= btmrvl_sdio_wakeup_fw
;
1041 priv
->hw_process_int_status
= btmrvl_sdio_process_int_status
;
1043 if (btmrvl_register_hdev(priv
)) {
1044 BT_ERR("Register hdev failed!");
1046 goto disable_host_int
;
1049 priv
->btmrvl_dev
.psmode
= 1;
1050 btmrvl_enable_ps(priv
);
1052 priv
->btmrvl_dev
.gpio_gap
= 0xffff;
1053 btmrvl_send_hscfg_cmd(priv
);
1058 btmrvl_sdio_disable_host_int(card
);
1060 btmrvl_sdio_unregister_dev(card
);
1064 static void btmrvl_sdio_remove(struct sdio_func
*func
)
1066 struct btmrvl_sdio_card
*card
;
1069 card
= sdio_get_drvdata(func
);
1071 /* Send SHUTDOWN command & disable interrupt
1072 * if user removes the module.
1075 btmrvl_send_module_cfg_cmd(card
->priv
,
1076 MODULE_SHUTDOWN_REQ
);
1077 btmrvl_sdio_disable_host_int(card
);
1079 BT_DBG("unregester dev");
1080 btmrvl_sdio_unregister_dev(card
);
1081 btmrvl_remove_card(card
->priv
);
1086 static int btmrvl_sdio_suspend(struct device
*dev
)
1088 struct sdio_func
*func
= dev_to_sdio_func(dev
);
1089 struct btmrvl_sdio_card
*card
;
1090 struct btmrvl_private
*priv
;
1091 mmc_pm_flag_t pm_flags
;
1092 struct hci_dev
*hcidev
;
1095 pm_flags
= sdio_get_host_pm_caps(func
);
1096 BT_DBG("%s: suspend: PM flags = 0x%x", sdio_func_id(func
),
1098 if (!(pm_flags
& MMC_PM_KEEP_POWER
)) {
1099 BT_ERR("%s: cannot remain alive while suspended",
1100 sdio_func_id(func
));
1103 card
= sdio_get_drvdata(func
);
1104 if (!card
|| !card
->priv
) {
1105 BT_ERR("card or priv structure is not valid");
1109 BT_ERR("sdio_func is not specified");
1115 if (priv
->adapter
->hs_state
!= HS_ACTIVATED
) {
1116 if (btmrvl_enable_hs(priv
)) {
1117 BT_ERR("HS not actived, suspend failed!");
1121 hcidev
= priv
->btmrvl_dev
.hcidev
;
1122 BT_DBG("%s: SDIO suspend", hcidev
->name
);
1123 hci_suspend_dev(hcidev
);
1124 skb_queue_purge(&priv
->adapter
->tx_queue
);
1126 priv
->adapter
->is_suspended
= true;
1128 /* We will keep the power when hs enabled successfully */
1129 if (priv
->adapter
->hs_state
== HS_ACTIVATED
) {
1130 BT_DBG("suspend with MMC_PM_KEEP_POWER");
1131 return sdio_set_host_pm_flags(func
, MMC_PM_KEEP_POWER
);
1133 BT_DBG("suspend without MMC_PM_KEEP_POWER");
1138 static int btmrvl_sdio_resume(struct device
*dev
)
1140 struct sdio_func
*func
= dev_to_sdio_func(dev
);
1141 struct btmrvl_sdio_card
*card
;
1142 struct btmrvl_private
*priv
;
1143 mmc_pm_flag_t pm_flags
;
1144 struct hci_dev
*hcidev
;
1147 pm_flags
= sdio_get_host_pm_caps(func
);
1148 BT_DBG("%s: resume: PM flags = 0x%x", sdio_func_id(func
),
1150 card
= sdio_get_drvdata(func
);
1151 if (!card
|| !card
->priv
) {
1152 BT_ERR("card or priv structure is not valid");
1156 BT_ERR("sdio_func is not specified");
1161 if (!priv
->adapter
->is_suspended
) {
1162 BT_DBG("device already resumed");
1166 priv
->adapter
->is_suspended
= false;
1167 hcidev
= priv
->btmrvl_dev
.hcidev
;
1168 BT_DBG("%s: SDIO resume", hcidev
->name
);
1169 hci_resume_dev(hcidev
);
1170 priv
->hw_wakeup_firmware(priv
);
1171 priv
->adapter
->hs_state
= HS_DEACTIVATED
;
1172 BT_DBG("%s: HS DEACTIVATED in resume!", hcidev
->name
);
1177 static const struct dev_pm_ops btmrvl_sdio_pm_ops
= {
1178 .suspend
= btmrvl_sdio_suspend
,
1179 .resume
= btmrvl_sdio_resume
,
1182 static struct sdio_driver bt_mrvl_sdio
= {
1183 .name
= "btmrvl_sdio",
1184 .id_table
= btmrvl_sdio_ids
,
1185 .probe
= btmrvl_sdio_probe
,
1186 .remove
= btmrvl_sdio_remove
,
1188 .owner
= THIS_MODULE
,
1189 .pm
= &btmrvl_sdio_pm_ops
,
1193 static int __init
btmrvl_sdio_init_module(void)
1195 if (sdio_register_driver(&bt_mrvl_sdio
) != 0) {
1196 BT_ERR("SDIO Driver Registration Failed");
1200 /* Clear the flag in case user removes the card. */
1206 static void __exit
btmrvl_sdio_exit_module(void)
1208 /* Set the flag as user is removing this module. */
1211 sdio_unregister_driver(&bt_mrvl_sdio
);
1214 module_init(btmrvl_sdio_init_module
);
1215 module_exit(btmrvl_sdio_exit_module
);
1217 MODULE_AUTHOR("Marvell International Ltd.");
1218 MODULE_DESCRIPTION("Marvell BT-over-SDIO driver ver " VERSION
);
1219 MODULE_VERSION(VERSION
);
1220 MODULE_LICENSE("GPL v2");
1221 MODULE_FIRMWARE("mrvl/sd8688_helper.bin");
1222 MODULE_FIRMWARE("mrvl/sd8688.bin");
1223 MODULE_FIRMWARE("mrvl/sd8787_uapsta.bin");
1224 MODULE_FIRMWARE("mrvl/sd8797_uapsta.bin");
1225 MODULE_FIRMWARE("mrvl/sd8897_uapsta.bin");