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_8887
= {
89 .host_int_mask
= 0x08,
90 .host_intstatus
= 0x0C,
92 .sq_read_base_addr_a0
= 0x6C,
93 .sq_read_base_addr_a1
= 0x6D,
94 .card_revision
= 0xC8,
95 .card_fw_status0
= 0x88,
96 .card_fw_status1
= 0x89,
102 .int_read_to_clear
= true,
103 .host_int_rsr
= 0x04,
104 .card_misc_cfg
= 0xD8,
107 static const struct btmrvl_sdio_card_reg btmrvl_reg_8897
= {
109 .host_int_mask
= 0x02,
110 .host_intstatus
= 0x03,
112 .sq_read_base_addr_a0
= 0x60,
113 .sq_read_base_addr_a1
= 0x61,
114 .card_revision
= 0xbc,
115 .card_fw_status0
= 0xc0,
116 .card_fw_status1
= 0xc1,
118 .card_rx_unit
= 0xc3,
122 .int_read_to_clear
= true,
123 .host_int_rsr
= 0x01,
124 .card_misc_cfg
= 0xcc,
127 static const struct btmrvl_sdio_device btmrvl_sdio_sd8688
= {
128 .helper
= "mrvl/sd8688_helper.bin",
129 .firmware
= "mrvl/sd8688.bin",
130 .reg
= &btmrvl_reg_8688
,
131 .support_pscan_win_report
= false,
132 .sd_blksz_fw_dl
= 64,
135 static const struct btmrvl_sdio_device btmrvl_sdio_sd8787
= {
137 .firmware
= "mrvl/sd8787_uapsta.bin",
138 .reg
= &btmrvl_reg_87xx
,
139 .support_pscan_win_report
= false,
140 .sd_blksz_fw_dl
= 256,
143 static const struct btmrvl_sdio_device btmrvl_sdio_sd8797
= {
145 .firmware
= "mrvl/sd8797_uapsta.bin",
146 .reg
= &btmrvl_reg_87xx
,
147 .support_pscan_win_report
= false,
148 .sd_blksz_fw_dl
= 256,
151 static const struct btmrvl_sdio_device btmrvl_sdio_sd8887
= {
153 .firmware
= "mrvl/sd8887_uapsta.bin",
154 .reg
= &btmrvl_reg_8887
,
155 .support_pscan_win_report
= true,
156 .sd_blksz_fw_dl
= 256,
159 static const struct btmrvl_sdio_device btmrvl_sdio_sd8897
= {
161 .firmware
= "mrvl/sd8897_uapsta.bin",
162 .reg
= &btmrvl_reg_8897
,
163 .support_pscan_win_report
= true,
164 .sd_blksz_fw_dl
= 256,
167 static const struct sdio_device_id btmrvl_sdio_ids
[] = {
168 /* Marvell SD8688 Bluetooth device */
169 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL
, 0x9105),
170 .driver_data
= (unsigned long) &btmrvl_sdio_sd8688
},
171 /* Marvell SD8787 Bluetooth device */
172 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL
, 0x911A),
173 .driver_data
= (unsigned long) &btmrvl_sdio_sd8787
},
174 /* Marvell SD8787 Bluetooth AMP device */
175 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL
, 0x911B),
176 .driver_data
= (unsigned long) &btmrvl_sdio_sd8787
},
177 /* Marvell SD8797 Bluetooth device */
178 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL
, 0x912A),
179 .driver_data
= (unsigned long) &btmrvl_sdio_sd8797
},
180 /* Marvell SD8887 Bluetooth device */
181 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL
, 0x9136),
182 .driver_data
= (unsigned long)&btmrvl_sdio_sd8887
},
183 /* Marvell SD8897 Bluetooth device */
184 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL
, 0x912E),
185 .driver_data
= (unsigned long) &btmrvl_sdio_sd8897
},
187 { } /* Terminating entry */
190 MODULE_DEVICE_TABLE(sdio
, btmrvl_sdio_ids
);
192 static int btmrvl_sdio_get_rx_unit(struct btmrvl_sdio_card
*card
)
197 reg
= sdio_readb(card
->func
, card
->reg
->card_rx_unit
, &ret
);
204 static int btmrvl_sdio_read_fw_status(struct btmrvl_sdio_card
*card
, u16
*dat
)
211 fws0
= sdio_readb(card
->func
, card
->reg
->card_fw_status0
, &ret
);
215 fws1
= sdio_readb(card
->func
, card
->reg
->card_fw_status1
, &ret
);
219 *dat
= (((u16
) fws1
) << 8) | fws0
;
224 static int btmrvl_sdio_read_rx_len(struct btmrvl_sdio_card
*card
, u16
*dat
)
229 reg
= sdio_readb(card
->func
, card
->reg
->card_rx_len
, &ret
);
231 *dat
= (u16
) reg
<< card
->rx_unit
;
236 static int btmrvl_sdio_enable_host_int_mask(struct btmrvl_sdio_card
*card
,
241 sdio_writeb(card
->func
, mask
, card
->reg
->host_int_mask
, &ret
);
243 BT_ERR("Unable to enable the host interrupt!");
250 static int btmrvl_sdio_disable_host_int_mask(struct btmrvl_sdio_card
*card
,
256 host_int_mask
= sdio_readb(card
->func
, card
->reg
->host_int_mask
, &ret
);
260 host_int_mask
&= ~mask
;
262 sdio_writeb(card
->func
, host_int_mask
, card
->reg
->host_int_mask
, &ret
);
264 BT_ERR("Unable to disable the host interrupt!");
271 static int btmrvl_sdio_poll_card_status(struct btmrvl_sdio_card
*card
, u8 bits
)
277 for (tries
= 0; tries
< MAX_POLL_TRIES
* 1000; tries
++) {
278 status
= sdio_readb(card
->func
, card
->reg
->card_status
, &ret
);
281 if ((status
& bits
) == bits
)
290 BT_ERR("FAILED! ret=%d", ret
);
295 static int btmrvl_sdio_verify_fw_download(struct btmrvl_sdio_card
*card
,
301 /* Wait for firmware to become ready */
302 for (tries
= 0; tries
< pollnum
; tries
++) {
303 sdio_claim_host(card
->func
);
304 ret
= btmrvl_sdio_read_fw_status(card
, &firmwarestat
);
305 sdio_release_host(card
->func
);
309 if (firmwarestat
== FIRMWARE_READY
)
318 static int btmrvl_sdio_download_helper(struct btmrvl_sdio_card
*card
)
320 const struct firmware
*fw_helper
= NULL
;
321 const u8
*helper
= NULL
;
323 void *tmphlprbuf
= NULL
;
324 int tmphlprbufsz
, hlprblknow
, helperlen
;
328 ret
= request_firmware(&fw_helper
, card
->helper
,
330 if ((ret
< 0) || !fw_helper
) {
331 BT_ERR("request_firmware(helper) failed, error code = %d",
337 helper
= fw_helper
->data
;
338 helperlen
= fw_helper
->size
;
340 BT_DBG("Downloading helper image (%d bytes), block size %d bytes",
341 helperlen
, SDIO_BLOCK_SIZE
);
343 tmphlprbufsz
= ALIGN_SZ(BTM_UPLD_SIZE
, BTSDIO_DMA_ALIGN
);
345 tmphlprbuf
= kzalloc(tmphlprbufsz
, GFP_KERNEL
);
347 BT_ERR("Unable to allocate buffer for helper."
348 " Terminating download");
353 helperbuf
= (u8
*) ALIGN_ADDR(tmphlprbuf
, BTSDIO_DMA_ALIGN
);
355 /* Perform helper data transfer */
356 tx_len
= (FIRMWARE_TRANSFER_NBLOCK
* SDIO_BLOCK_SIZE
)
361 ret
= btmrvl_sdio_poll_card_status(card
,
362 CARD_IO_READY
| DN_LD_CARD_RDY
);
364 BT_ERR("Helper download poll status timeout @ %d",
369 /* Check if there is more data? */
370 if (hlprblknow
>= helperlen
)
373 if (helperlen
- hlprblknow
< tx_len
)
374 tx_len
= helperlen
- hlprblknow
;
377 helperbuf
[0] = ((tx_len
& 0x000000ff) >> 0);
378 helperbuf
[1] = ((tx_len
& 0x0000ff00) >> 8);
379 helperbuf
[2] = ((tx_len
& 0x00ff0000) >> 16);
380 helperbuf
[3] = ((tx_len
& 0xff000000) >> 24);
382 memcpy(&helperbuf
[SDIO_HEADER_LEN
], &helper
[hlprblknow
],
385 /* Now send the data */
386 ret
= sdio_writesb(card
->func
, card
->ioport
, helperbuf
,
387 FIRMWARE_TRANSFER_NBLOCK
* SDIO_BLOCK_SIZE
);
389 BT_ERR("IO error during helper download @ %d",
394 hlprblknow
+= tx_len
;
397 BT_DBG("Transferring helper image EOF block");
399 memset(helperbuf
, 0x0, SDIO_BLOCK_SIZE
);
401 ret
= sdio_writesb(card
->func
, card
->ioport
, helperbuf
,
404 BT_ERR("IO error in writing helper image EOF block");
412 release_firmware(fw_helper
);
416 static int btmrvl_sdio_download_fw_w_helper(struct btmrvl_sdio_card
*card
)
418 const struct firmware
*fw_firmware
= NULL
;
419 const u8
*firmware
= NULL
;
420 int firmwarelen
, tmpfwbufsz
, ret
;
421 unsigned int tries
, offset
;
423 void *tmpfwbuf
= NULL
;
425 u16 len
, blksz_dl
= card
->sd_blksz_fw_dl
;
426 int txlen
= 0, tx_blocks
= 0, count
= 0;
428 ret
= request_firmware(&fw_firmware
, card
->firmware
,
430 if ((ret
< 0) || !fw_firmware
) {
431 BT_ERR("request_firmware(firmware) failed, error code = %d",
437 firmware
= fw_firmware
->data
;
438 firmwarelen
= fw_firmware
->size
;
440 BT_DBG("Downloading FW image (%d bytes)", firmwarelen
);
442 tmpfwbufsz
= ALIGN_SZ(BTM_UPLD_SIZE
, BTSDIO_DMA_ALIGN
);
443 tmpfwbuf
= kzalloc(tmpfwbufsz
, GFP_KERNEL
);
445 BT_ERR("Unable to allocate buffer for firmware."
446 " Terminating download");
451 /* Ensure aligned firmware buffer */
452 fwbuf
= (u8
*) ALIGN_ADDR(tmpfwbuf
, BTSDIO_DMA_ALIGN
);
454 /* Perform firmware data transfer */
457 ret
= btmrvl_sdio_poll_card_status(card
,
458 CARD_IO_READY
| DN_LD_CARD_RDY
);
460 BT_ERR("FW download with helper poll status"
461 " timeout @ %d", offset
);
465 /* Check if there is more data ? */
466 if (offset
>= firmwarelen
)
469 for (tries
= 0; tries
< MAX_POLL_TRIES
; tries
++) {
470 base0
= sdio_readb(card
->func
,
471 card
->reg
->sq_read_base_addr_a0
, &ret
);
473 BT_ERR("BASE0 register read failed:"
474 " base0 = 0x%04X(%d)."
475 " Terminating download",
480 base1
= sdio_readb(card
->func
,
481 card
->reg
->sq_read_base_addr_a1
, &ret
);
483 BT_ERR("BASE1 register read failed:"
484 " base1 = 0x%04X(%d)."
485 " Terminating download",
491 len
= (((u16
) base1
) << 8) | base0
;
500 else if (len
> BTM_UPLD_SIZE
) {
501 BT_ERR("FW download failure @%d, invalid length %d",
511 if (count
> MAX_WRITE_IOMEM_RETRY
) {
512 BT_ERR("FW download failure @%d, "
513 "over max retry count", offset
);
517 BT_ERR("FW CRC error indicated by the helper: "
518 "len = 0x%04X, txlen = %d", len
, txlen
);
520 /* Set txlen to 0 so as to resend from same offset */
526 if (firmwarelen
- offset
< txlen
)
527 txlen
= firmwarelen
- offset
;
529 tx_blocks
= DIV_ROUND_UP(txlen
, blksz_dl
);
531 memcpy(fwbuf
, &firmware
[offset
], txlen
);
534 ret
= sdio_writesb(card
->func
, card
->ioport
, fwbuf
,
535 tx_blocks
* blksz_dl
);
538 BT_ERR("FW download, writesb(%d) failed @%d",
540 sdio_writeb(card
->func
, HOST_CMD53_FIN
,
541 card
->reg
->cfg
, &ret
);
543 BT_ERR("writeb failed (CFG)");
549 BT_DBG("FW download over, size %d bytes", offset
);
555 release_firmware(fw_firmware
);
559 static int btmrvl_sdio_card_to_host(struct btmrvl_private
*priv
)
562 int ret
, num_blocks
, blksz
;
563 struct sk_buff
*skb
= NULL
;
566 struct hci_dev
*hdev
= priv
->btmrvl_dev
.hcidev
;
567 struct btmrvl_sdio_card
*card
= priv
->btmrvl_dev
.card
;
569 if (!card
|| !card
->func
) {
570 BT_ERR("card or function is NULL!");
575 /* Read the length of data to be transferred */
576 ret
= btmrvl_sdio_read_rx_len(card
, &buf_len
);
578 BT_ERR("read rx_len failed");
583 blksz
= SDIO_BLOCK_SIZE
;
584 num_blocks
= DIV_ROUND_UP(buf_len
, blksz
);
586 if (buf_len
<= SDIO_HEADER_LEN
587 || (num_blocks
* blksz
) > ALLOC_BUF_SIZE
) {
588 BT_ERR("invalid packet length: %d", buf_len
);
593 /* Allocate buffer */
594 skb
= bt_skb_alloc(num_blocks
* blksz
+ BTSDIO_DMA_ALIGN
, GFP_ATOMIC
);
596 BT_ERR("No free skb");
601 if ((unsigned long) skb
->data
& (BTSDIO_DMA_ALIGN
- 1)) {
602 skb_put(skb
, (unsigned long) skb
->data
&
603 (BTSDIO_DMA_ALIGN
- 1));
604 skb_pull(skb
, (unsigned long) skb
->data
&
605 (BTSDIO_DMA_ALIGN
- 1));
610 ret
= sdio_readsb(card
->func
, payload
, card
->ioport
,
613 BT_ERR("readsb failed: %d", ret
);
618 /* This is SDIO specific header length: byte[2][1][0], type: byte[3]
619 * (HCI_COMMAND = 1, ACL_DATA = 2, SCO_DATA = 3, 0xFE = Vendor)
622 buf_len
= payload
[0];
623 buf_len
|= payload
[1] << 8;
624 buf_len
|= payload
[2] << 16;
626 if (buf_len
> blksz
* num_blocks
) {
627 BT_ERR("Skip incorrect packet: hdrlen %d buffer %d",
628 buf_len
, blksz
* num_blocks
);
636 case HCI_ACLDATA_PKT
:
637 case HCI_SCODATA_PKT
:
639 bt_cb(skb
)->pkt_type
= type
;
640 skb_put(skb
, buf_len
);
641 skb_pull(skb
, SDIO_HEADER_LEN
);
643 if (type
== HCI_EVENT_PKT
) {
644 if (btmrvl_check_evtpkt(priv
, skb
))
645 hci_recv_frame(hdev
, skb
);
647 hci_recv_frame(hdev
, skb
);
650 hdev
->stat
.byte_rx
+= buf_len
;
653 case MRVL_VENDOR_PKT
:
654 bt_cb(skb
)->pkt_type
= HCI_VENDOR_PKT
;
655 skb_put(skb
, buf_len
);
656 skb_pull(skb
, SDIO_HEADER_LEN
);
658 if (btmrvl_process_event(priv
, skb
))
659 hci_recv_frame(hdev
, skb
);
661 hdev
->stat
.byte_rx
+= buf_len
;
665 BT_ERR("Unknown packet type:%d", type
);
666 BT_ERR("hex: %*ph", blksz
* num_blocks
, payload
);
682 static int btmrvl_sdio_process_int_status(struct btmrvl_private
*priv
)
686 struct btmrvl_sdio_card
*card
= priv
->btmrvl_dev
.card
;
688 spin_lock_irqsave(&priv
->driver_lock
, flags
);
691 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
693 sdio_claim_host(card
->func
);
694 if (ireg
& DN_LD_HOST_INT_STATUS
) {
695 if (priv
->btmrvl_dev
.tx_dnld_rdy
)
696 BT_DBG("tx_done already received: "
697 " int_status=0x%x", ireg
);
699 priv
->btmrvl_dev
.tx_dnld_rdy
= true;
702 if (ireg
& UP_LD_HOST_INT_STATUS
)
703 btmrvl_sdio_card_to_host(priv
);
705 sdio_release_host(card
->func
);
710 static int btmrvl_sdio_read_to_clear(struct btmrvl_sdio_card
*card
, u8
*ireg
)
712 struct btmrvl_adapter
*adapter
= card
->priv
->adapter
;
715 ret
= sdio_readsb(card
->func
, adapter
->hw_regs
, 0, SDIO_BLOCK_SIZE
);
717 BT_ERR("sdio_readsb: read int hw_regs failed: %d", ret
);
721 *ireg
= adapter
->hw_regs
[card
->reg
->host_intstatus
];
722 BT_DBG("hw_regs[%#x]=%#x", card
->reg
->host_intstatus
, *ireg
);
727 static int btmrvl_sdio_write_to_clear(struct btmrvl_sdio_card
*card
, u8
*ireg
)
731 *ireg
= sdio_readb(card
->func
, card
->reg
->host_intstatus
, &ret
);
733 BT_ERR("sdio_readb: read int status failed: %d", ret
);
739 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
740 * Clear the interrupt status register and re-enable the
743 BT_DBG("int_status = 0x%x", *ireg
);
745 sdio_writeb(card
->func
, ~(*ireg
) & (DN_LD_HOST_INT_STATUS
|
746 UP_LD_HOST_INT_STATUS
),
747 card
->reg
->host_intstatus
, &ret
);
749 BT_ERR("sdio_writeb: clear int status failed: %d", ret
);
757 static void btmrvl_sdio_interrupt(struct sdio_func
*func
)
759 struct btmrvl_private
*priv
;
760 struct btmrvl_sdio_card
*card
;
765 card
= sdio_get_drvdata(func
);
766 if (!card
|| !card
->priv
) {
767 BT_ERR("sbi_interrupt(%p) card or priv is "
768 "NULL, card=%p\n", func
, card
);
774 if (card
->reg
->int_read_to_clear
)
775 ret
= btmrvl_sdio_read_to_clear(card
, &ireg
);
777 ret
= btmrvl_sdio_write_to_clear(card
, &ireg
);
782 spin_lock_irqsave(&priv
->driver_lock
, flags
);
784 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
786 btmrvl_interrupt(priv
);
789 static int btmrvl_sdio_register_dev(struct btmrvl_sdio_card
*card
)
791 struct sdio_func
*func
;
795 if (!card
|| !card
->func
) {
796 BT_ERR("Error: card or function is NULL!");
803 sdio_claim_host(func
);
805 ret
= sdio_enable_func(func
);
807 BT_ERR("sdio_enable_func() failed: ret=%d", ret
);
812 ret
= sdio_claim_irq(func
, btmrvl_sdio_interrupt
);
814 BT_ERR("sdio_claim_irq failed: ret=%d", ret
);
819 ret
= sdio_set_block_size(card
->func
, SDIO_BLOCK_SIZE
);
821 BT_ERR("cannot set SDIO block size");
826 reg
= sdio_readb(func
, card
->reg
->io_port_0
, &ret
);
834 reg
= sdio_readb(func
, card
->reg
->io_port_1
, &ret
);
840 card
->ioport
|= (reg
<< 8);
842 reg
= sdio_readb(func
, card
->reg
->io_port_2
, &ret
);
848 card
->ioport
|= (reg
<< 16);
850 BT_DBG("SDIO FUNC%d IO port: 0x%x", func
->num
, card
->ioport
);
852 if (card
->reg
->int_read_to_clear
) {
853 reg
= sdio_readb(func
, card
->reg
->host_int_rsr
, &ret
);
858 sdio_writeb(func
, reg
| 0x3f, card
->reg
->host_int_rsr
, &ret
);
864 reg
= sdio_readb(func
, card
->reg
->card_misc_cfg
, &ret
);
869 sdio_writeb(func
, reg
| 0x10, card
->reg
->card_misc_cfg
, &ret
);
876 sdio_set_drvdata(func
, card
);
878 sdio_release_host(func
);
883 sdio_release_irq(func
);
886 sdio_disable_func(func
);
889 sdio_release_host(func
);
895 static int btmrvl_sdio_unregister_dev(struct btmrvl_sdio_card
*card
)
897 if (card
&& card
->func
) {
898 sdio_claim_host(card
->func
);
899 sdio_release_irq(card
->func
);
900 sdio_disable_func(card
->func
);
901 sdio_release_host(card
->func
);
902 sdio_set_drvdata(card
->func
, NULL
);
908 static int btmrvl_sdio_enable_host_int(struct btmrvl_sdio_card
*card
)
912 if (!card
|| !card
->func
)
915 sdio_claim_host(card
->func
);
917 ret
= btmrvl_sdio_enable_host_int_mask(card
, HIM_ENABLE
);
919 btmrvl_sdio_get_rx_unit(card
);
921 sdio_release_host(card
->func
);
926 static int btmrvl_sdio_disable_host_int(struct btmrvl_sdio_card
*card
)
930 if (!card
|| !card
->func
)
933 sdio_claim_host(card
->func
);
935 ret
= btmrvl_sdio_disable_host_int_mask(card
, HIM_DISABLE
);
937 sdio_release_host(card
->func
);
942 static int btmrvl_sdio_host_to_card(struct btmrvl_private
*priv
,
945 struct btmrvl_sdio_card
*card
= priv
->btmrvl_dev
.card
;
954 if (!card
|| !card
->func
) {
955 BT_ERR("card or function is NULL!");
960 if ((unsigned long) payload
& (BTSDIO_DMA_ALIGN
- 1)) {
961 tmpbufsz
= ALIGN_SZ(nb
, BTSDIO_DMA_ALIGN
);
962 tmpbuf
= kzalloc(tmpbufsz
, GFP_KERNEL
);
965 buf
= (u8
*) ALIGN_ADDR(tmpbuf
, BTSDIO_DMA_ALIGN
);
966 memcpy(buf
, payload
, nb
);
969 blksz
= SDIO_BLOCK_SIZE
;
970 buf_block_len
= DIV_ROUND_UP(nb
, blksz
);
972 sdio_claim_host(card
->func
);
975 /* Transfer data to card */
976 ret
= sdio_writesb(card
->func
, card
->ioport
, buf
,
977 buf_block_len
* blksz
);
980 BT_ERR("i=%d writesb failed: %d", i
, ret
);
981 BT_ERR("hex: %*ph", nb
, payload
);
983 if (i
> MAX_WRITE_IOMEM_RETRY
)
988 priv
->btmrvl_dev
.tx_dnld_rdy
= false;
991 sdio_release_host(card
->func
);
997 static int btmrvl_sdio_download_fw(struct btmrvl_sdio_card
*card
)
1001 int pollnum
= MAX_POLL_TRIES
;
1003 if (!card
|| !card
->func
) {
1004 BT_ERR("card or function is NULL!");
1008 if (!btmrvl_sdio_verify_fw_download(card
, 1)) {
1009 BT_DBG("Firmware already downloaded!");
1013 sdio_claim_host(card
->func
);
1015 /* Check if other function driver is downloading the firmware */
1016 fws0
= sdio_readb(card
->func
, card
->reg
->card_fw_status0
, &ret
);
1018 BT_ERR("Failed to read FW downloading status!");
1023 BT_DBG("BT not the winner (%#x). Skip FW downloading", fws0
);
1025 /* Give other function more time to download the firmware */
1029 ret
= btmrvl_sdio_download_helper(card
);
1031 BT_ERR("Failed to download helper!");
1037 if (btmrvl_sdio_download_fw_w_helper(card
)) {
1038 BT_ERR("Failed to download firmware!");
1044 sdio_release_host(card
->func
);
1047 * winner or not, with this test the FW synchronizes when the
1048 * module can continue its initialization
1050 if (btmrvl_sdio_verify_fw_download(card
, pollnum
)) {
1051 BT_ERR("FW failed to be active in time!");
1058 sdio_release_host(card
->func
);
1062 static int btmrvl_sdio_wakeup_fw(struct btmrvl_private
*priv
)
1064 struct btmrvl_sdio_card
*card
= priv
->btmrvl_dev
.card
;
1067 if (!card
|| !card
->func
) {
1068 BT_ERR("card or function is NULL!");
1072 sdio_claim_host(card
->func
);
1074 sdio_writeb(card
->func
, HOST_POWER_UP
, card
->reg
->cfg
, &ret
);
1076 sdio_release_host(card
->func
);
1078 BT_DBG("wake up firmware");
1083 static int btmrvl_sdio_probe(struct sdio_func
*func
,
1084 const struct sdio_device_id
*id
)
1087 struct btmrvl_private
*priv
= NULL
;
1088 struct btmrvl_sdio_card
*card
= NULL
;
1090 BT_INFO("vendor=0x%x, device=0x%x, class=%d, fn=%d",
1091 id
->vendor
, id
->device
, id
->class, func
->num
);
1093 card
= devm_kzalloc(&func
->dev
, sizeof(*card
), GFP_KERNEL
);
1099 if (id
->driver_data
) {
1100 struct btmrvl_sdio_device
*data
= (void *) id
->driver_data
;
1101 card
->helper
= data
->helper
;
1102 card
->firmware
= data
->firmware
;
1103 card
->reg
= data
->reg
;
1104 card
->sd_blksz_fw_dl
= data
->sd_blksz_fw_dl
;
1105 card
->support_pscan_win_report
= data
->support_pscan_win_report
;
1108 if (btmrvl_sdio_register_dev(card
) < 0) {
1109 BT_ERR("Failed to register BT device!");
1113 /* Disable the interrupts on the card */
1114 btmrvl_sdio_disable_host_int(card
);
1116 if (btmrvl_sdio_download_fw(card
)) {
1117 BT_ERR("Downloading firmware failed!");
1122 btmrvl_sdio_enable_host_int(card
);
1124 priv
= btmrvl_add_card(card
);
1126 BT_ERR("Initializing card failed!");
1128 goto disable_host_int
;
1133 /* Initialize the interface specific function pointers */
1134 priv
->hw_host_to_card
= btmrvl_sdio_host_to_card
;
1135 priv
->hw_wakeup_firmware
= btmrvl_sdio_wakeup_fw
;
1136 priv
->hw_process_int_status
= btmrvl_sdio_process_int_status
;
1138 if (btmrvl_register_hdev(priv
)) {
1139 BT_ERR("Register hdev failed!");
1141 goto disable_host_int
;
1147 btmrvl_sdio_disable_host_int(card
);
1149 btmrvl_sdio_unregister_dev(card
);
1153 static void btmrvl_sdio_remove(struct sdio_func
*func
)
1155 struct btmrvl_sdio_card
*card
;
1158 card
= sdio_get_drvdata(func
);
1160 /* Send SHUTDOWN command & disable interrupt
1161 * if user removes the module.
1164 btmrvl_send_module_cfg_cmd(card
->priv
,
1165 MODULE_SHUTDOWN_REQ
);
1166 btmrvl_sdio_disable_host_int(card
);
1168 BT_DBG("unregester dev");
1169 btmrvl_sdio_unregister_dev(card
);
1170 btmrvl_remove_card(card
->priv
);
1175 static int btmrvl_sdio_suspend(struct device
*dev
)
1177 struct sdio_func
*func
= dev_to_sdio_func(dev
);
1178 struct btmrvl_sdio_card
*card
;
1179 struct btmrvl_private
*priv
;
1180 mmc_pm_flag_t pm_flags
;
1181 struct hci_dev
*hcidev
;
1184 pm_flags
= sdio_get_host_pm_caps(func
);
1185 BT_DBG("%s: suspend: PM flags = 0x%x", sdio_func_id(func
),
1187 if (!(pm_flags
& MMC_PM_KEEP_POWER
)) {
1188 BT_ERR("%s: cannot remain alive while suspended",
1189 sdio_func_id(func
));
1192 card
= sdio_get_drvdata(func
);
1193 if (!card
|| !card
->priv
) {
1194 BT_ERR("card or priv structure is not valid");
1198 BT_ERR("sdio_func is not specified");
1203 hcidev
= priv
->btmrvl_dev
.hcidev
;
1204 BT_DBG("%s: SDIO suspend", hcidev
->name
);
1205 hci_suspend_dev(hcidev
);
1206 skb_queue_purge(&priv
->adapter
->tx_queue
);
1208 if (priv
->adapter
->hs_state
!= HS_ACTIVATED
) {
1209 if (btmrvl_enable_hs(priv
)) {
1210 BT_ERR("HS not actived, suspend failed!");
1215 priv
->adapter
->is_suspended
= true;
1217 /* We will keep the power when hs enabled successfully */
1218 if (priv
->adapter
->hs_state
== HS_ACTIVATED
) {
1219 BT_DBG("suspend with MMC_PM_KEEP_POWER");
1220 return sdio_set_host_pm_flags(func
, MMC_PM_KEEP_POWER
);
1222 BT_DBG("suspend without MMC_PM_KEEP_POWER");
1227 static int btmrvl_sdio_resume(struct device
*dev
)
1229 struct sdio_func
*func
= dev_to_sdio_func(dev
);
1230 struct btmrvl_sdio_card
*card
;
1231 struct btmrvl_private
*priv
;
1232 mmc_pm_flag_t pm_flags
;
1233 struct hci_dev
*hcidev
;
1236 pm_flags
= sdio_get_host_pm_caps(func
);
1237 BT_DBG("%s: resume: PM flags = 0x%x", sdio_func_id(func
),
1239 card
= sdio_get_drvdata(func
);
1240 if (!card
|| !card
->priv
) {
1241 BT_ERR("card or priv structure is not valid");
1245 BT_ERR("sdio_func is not specified");
1250 if (!priv
->adapter
->is_suspended
) {
1251 BT_DBG("device already resumed");
1255 priv
->hw_wakeup_firmware(priv
);
1256 priv
->adapter
->hs_state
= HS_DEACTIVATED
;
1257 hcidev
= priv
->btmrvl_dev
.hcidev
;
1258 BT_DBG("%s: HS DEACTIVATED in resume!", hcidev
->name
);
1259 priv
->adapter
->is_suspended
= false;
1260 BT_DBG("%s: SDIO resume", hcidev
->name
);
1261 hci_resume_dev(hcidev
);
1266 static const struct dev_pm_ops btmrvl_sdio_pm_ops
= {
1267 .suspend
= btmrvl_sdio_suspend
,
1268 .resume
= btmrvl_sdio_resume
,
1271 static struct sdio_driver bt_mrvl_sdio
= {
1272 .name
= "btmrvl_sdio",
1273 .id_table
= btmrvl_sdio_ids
,
1274 .probe
= btmrvl_sdio_probe
,
1275 .remove
= btmrvl_sdio_remove
,
1277 .owner
= THIS_MODULE
,
1278 .pm
= &btmrvl_sdio_pm_ops
,
1282 static int __init
btmrvl_sdio_init_module(void)
1284 if (sdio_register_driver(&bt_mrvl_sdio
) != 0) {
1285 BT_ERR("SDIO Driver Registration Failed");
1289 /* Clear the flag in case user removes the card. */
1295 static void __exit
btmrvl_sdio_exit_module(void)
1297 /* Set the flag as user is removing this module. */
1300 sdio_unregister_driver(&bt_mrvl_sdio
);
1303 module_init(btmrvl_sdio_init_module
);
1304 module_exit(btmrvl_sdio_exit_module
);
1306 MODULE_AUTHOR("Marvell International Ltd.");
1307 MODULE_DESCRIPTION("Marvell BT-over-SDIO driver ver " VERSION
);
1308 MODULE_VERSION(VERSION
);
1309 MODULE_LICENSE("GPL v2");
1310 MODULE_FIRMWARE("mrvl/sd8688_helper.bin");
1311 MODULE_FIRMWARE("mrvl/sd8688.bin");
1312 MODULE_FIRMWARE("mrvl/sd8787_uapsta.bin");
1313 MODULE_FIRMWARE("mrvl/sd8797_uapsta.bin");
1314 MODULE_FIRMWARE("mrvl/sd8887_uapsta.bin");
1315 MODULE_FIRMWARE("mrvl/sd8897_uapsta.bin");