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>
27 #include <linux/devcoredump.h>
29 #include <net/bluetooth/bluetooth.h>
30 #include <net/bluetooth/hci_core.h>
32 #include "btmrvl_drv.h"
33 #include "btmrvl_sdio.h"
37 static struct memory_type_mapping mem_type_mapping_tbl
[] = {
38 {"ITCM", NULL
, 0, 0xF0},
39 {"DTCM", NULL
, 0, 0xF1},
40 {"SQRAM", NULL
, 0, 0xF2},
41 {"APU", NULL
, 0, 0xF3},
42 {"CIU", NULL
, 0, 0xF4},
43 {"ICU", NULL
, 0, 0xF5},
44 {"MAC", NULL
, 0, 0xF6},
45 {"EXT7", NULL
, 0, 0xF7},
46 {"EXT8", NULL
, 0, 0xF8},
47 {"EXT9", NULL
, 0, 0xF9},
48 {"EXT10", NULL
, 0, 0xFA},
49 {"EXT11", NULL
, 0, 0xFB},
50 {"EXT12", NULL
, 0, 0xFC},
51 {"EXT13", NULL
, 0, 0xFD},
52 {"EXTLAST", NULL
, 0, 0xFE},
55 /* The btmrvl_sdio_remove() callback function is called
56 * when user removes this module from kernel space or ejects
57 * the card from the slot. The driver handles these 2 cases
59 * If the user is removing the module, a MODULE_SHUTDOWN_REQ
60 * command is sent to firmware and interrupt will be disabled.
61 * If the card is removed, there is no need to send command
62 * or disable interrupt.
64 * The variable 'user_rmmod' is used to distinguish these two
65 * scenarios. This flag is initialized as FALSE in case the card
66 * is removed, and will be set to TRUE for module removal when
67 * module_exit function is called.
72 static const struct btmrvl_sdio_card_reg btmrvl_reg_8688
= {
74 .host_int_mask
= 0x04,
75 .host_intstatus
= 0x05,
77 .sq_read_base_addr_a0
= 0x10,
78 .sq_read_base_addr_a1
= 0x11,
79 .card_fw_status0
= 0x40,
80 .card_fw_status1
= 0x41,
86 .int_read_to_clear
= false,
88 static const struct btmrvl_sdio_card_reg btmrvl_reg_87xx
= {
90 .host_int_mask
= 0x02,
91 .host_intstatus
= 0x03,
93 .sq_read_base_addr_a0
= 0x40,
94 .sq_read_base_addr_a1
= 0x41,
95 .card_revision
= 0x5c,
96 .card_fw_status0
= 0x60,
97 .card_fw_status1
= 0x61,
103 .int_read_to_clear
= false,
106 static const struct btmrvl_sdio_card_reg btmrvl_reg_8887
= {
108 .host_int_mask
= 0x08,
109 .host_intstatus
= 0x0C,
111 .sq_read_base_addr_a0
= 0x6C,
112 .sq_read_base_addr_a1
= 0x6D,
113 .card_revision
= 0xC8,
114 .card_fw_status0
= 0x88,
115 .card_fw_status1
= 0x89,
117 .card_rx_unit
= 0x8B,
121 .int_read_to_clear
= true,
122 .host_int_rsr
= 0x04,
123 .card_misc_cfg
= 0xD8,
126 static const struct btmrvl_sdio_card_reg btmrvl_reg_8897
= {
128 .host_int_mask
= 0x02,
129 .host_intstatus
= 0x03,
131 .sq_read_base_addr_a0
= 0x60,
132 .sq_read_base_addr_a1
= 0x61,
133 .card_revision
= 0xbc,
134 .card_fw_status0
= 0xc0,
135 .card_fw_status1
= 0xc1,
137 .card_rx_unit
= 0xc3,
141 .int_read_to_clear
= true,
142 .host_int_rsr
= 0x01,
143 .card_misc_cfg
= 0xcc,
144 .fw_dump_ctrl
= 0xe2,
145 .fw_dump_start
= 0xe3,
149 static const struct btmrvl_sdio_card_reg btmrvl_reg_8997
= {
151 .host_int_mask
= 0x08,
152 .host_intstatus
= 0x0c,
154 .sq_read_base_addr_a0
= 0xf8,
155 .sq_read_base_addr_a1
= 0xf9,
156 .card_revision
= 0xc8,
157 .card_fw_status0
= 0xe8,
158 .card_fw_status1
= 0xe9,
160 .card_rx_unit
= 0xeb,
164 .int_read_to_clear
= true,
165 .host_int_rsr
= 0x04,
166 .card_misc_cfg
= 0xD8,
167 .fw_dump_ctrl
= 0xf0,
168 .fw_dump_start
= 0xf1,
172 static const struct btmrvl_sdio_device btmrvl_sdio_sd8688
= {
173 .helper
= "mrvl/sd8688_helper.bin",
174 .firmware
= "mrvl/sd8688.bin",
175 .reg
= &btmrvl_reg_8688
,
176 .support_pscan_win_report
= false,
177 .sd_blksz_fw_dl
= 64,
178 .supports_fw_dump
= false,
181 static const struct btmrvl_sdio_device btmrvl_sdio_sd8787
= {
183 .firmware
= "mrvl/sd8787_uapsta.bin",
184 .reg
= &btmrvl_reg_87xx
,
185 .support_pscan_win_report
= false,
186 .sd_blksz_fw_dl
= 256,
187 .supports_fw_dump
= false,
190 static const struct btmrvl_sdio_device btmrvl_sdio_sd8797
= {
192 .firmware
= "mrvl/sd8797_uapsta.bin",
193 .reg
= &btmrvl_reg_87xx
,
194 .support_pscan_win_report
= false,
195 .sd_blksz_fw_dl
= 256,
196 .supports_fw_dump
= false,
199 static const struct btmrvl_sdio_device btmrvl_sdio_sd8887
= {
201 .firmware
= "mrvl/sd8887_uapsta.bin",
202 .reg
= &btmrvl_reg_8887
,
203 .support_pscan_win_report
= true,
204 .sd_blksz_fw_dl
= 256,
205 .supports_fw_dump
= false,
208 static const struct btmrvl_sdio_device btmrvl_sdio_sd8897
= {
210 .firmware
= "mrvl/sd8897_uapsta.bin",
211 .reg
= &btmrvl_reg_8897
,
212 .support_pscan_win_report
= true,
213 .sd_blksz_fw_dl
= 256,
214 .supports_fw_dump
= true,
217 static const struct btmrvl_sdio_device btmrvl_sdio_sd8997
= {
219 .firmware
= "mrvl/sd8997_uapsta.bin",
220 .reg
= &btmrvl_reg_8997
,
221 .support_pscan_win_report
= true,
222 .sd_blksz_fw_dl
= 256,
223 .supports_fw_dump
= true,
226 static const struct sdio_device_id btmrvl_sdio_ids
[] = {
227 /* Marvell SD8688 Bluetooth device */
228 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL
, 0x9105),
229 .driver_data
= (unsigned long)&btmrvl_sdio_sd8688
},
230 /* Marvell SD8787 Bluetooth device */
231 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL
, 0x911A),
232 .driver_data
= (unsigned long)&btmrvl_sdio_sd8787
},
233 /* Marvell SD8787 Bluetooth AMP device */
234 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL
, 0x911B),
235 .driver_data
= (unsigned long)&btmrvl_sdio_sd8787
},
236 /* Marvell SD8797 Bluetooth device */
237 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL
, 0x912A),
238 .driver_data
= (unsigned long)&btmrvl_sdio_sd8797
},
239 /* Marvell SD8887 Bluetooth device */
240 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL
, 0x9136),
241 .driver_data
= (unsigned long)&btmrvl_sdio_sd8887
},
242 /* Marvell SD8897 Bluetooth device */
243 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL
, 0x912E),
244 .driver_data
= (unsigned long)&btmrvl_sdio_sd8897
},
245 /* Marvell SD8997 Bluetooth device */
246 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL
, 0x9142),
247 .driver_data
= (unsigned long)&btmrvl_sdio_sd8997
},
249 { } /* Terminating entry */
252 MODULE_DEVICE_TABLE(sdio
, btmrvl_sdio_ids
);
254 static int btmrvl_sdio_get_rx_unit(struct btmrvl_sdio_card
*card
)
259 reg
= sdio_readb(card
->func
, card
->reg
->card_rx_unit
, &ret
);
266 static int btmrvl_sdio_read_fw_status(struct btmrvl_sdio_card
*card
, u16
*dat
)
273 fws0
= sdio_readb(card
->func
, card
->reg
->card_fw_status0
, &ret
);
277 fws1
= sdio_readb(card
->func
, card
->reg
->card_fw_status1
, &ret
);
281 *dat
= (((u16
) fws1
) << 8) | fws0
;
286 static int btmrvl_sdio_read_rx_len(struct btmrvl_sdio_card
*card
, u16
*dat
)
291 reg
= sdio_readb(card
->func
, card
->reg
->card_rx_len
, &ret
);
293 *dat
= (u16
) reg
<< card
->rx_unit
;
298 static int btmrvl_sdio_enable_host_int_mask(struct btmrvl_sdio_card
*card
,
303 sdio_writeb(card
->func
, mask
, card
->reg
->host_int_mask
, &ret
);
305 BT_ERR("Unable to enable the host interrupt!");
312 static int btmrvl_sdio_disable_host_int_mask(struct btmrvl_sdio_card
*card
,
318 host_int_mask
= sdio_readb(card
->func
, card
->reg
->host_int_mask
, &ret
);
322 host_int_mask
&= ~mask
;
324 sdio_writeb(card
->func
, host_int_mask
, card
->reg
->host_int_mask
, &ret
);
326 BT_ERR("Unable to disable the host interrupt!");
333 static int btmrvl_sdio_poll_card_status(struct btmrvl_sdio_card
*card
, u8 bits
)
339 for (tries
= 0; tries
< MAX_POLL_TRIES
* 1000; tries
++) {
340 status
= sdio_readb(card
->func
, card
->reg
->card_status
, &ret
);
343 if ((status
& bits
) == bits
)
352 BT_ERR("FAILED! ret=%d", ret
);
357 static int btmrvl_sdio_verify_fw_download(struct btmrvl_sdio_card
*card
,
363 /* Wait for firmware to become ready */
364 for (tries
= 0; tries
< pollnum
; tries
++) {
365 sdio_claim_host(card
->func
);
366 ret
= btmrvl_sdio_read_fw_status(card
, &firmwarestat
);
367 sdio_release_host(card
->func
);
371 if (firmwarestat
== FIRMWARE_READY
)
380 static int btmrvl_sdio_download_helper(struct btmrvl_sdio_card
*card
)
382 const struct firmware
*fw_helper
= NULL
;
383 const u8
*helper
= NULL
;
385 void *tmphlprbuf
= NULL
;
386 int tmphlprbufsz
, hlprblknow
, helperlen
;
390 ret
= request_firmware(&fw_helper
, card
->helper
,
392 if ((ret
< 0) || !fw_helper
) {
393 BT_ERR("request_firmware(helper) failed, error code = %d",
399 helper
= fw_helper
->data
;
400 helperlen
= fw_helper
->size
;
402 BT_DBG("Downloading helper image (%d bytes), block size %d bytes",
403 helperlen
, SDIO_BLOCK_SIZE
);
405 tmphlprbufsz
= ALIGN_SZ(BTM_UPLD_SIZE
, BTSDIO_DMA_ALIGN
);
407 tmphlprbuf
= kzalloc(tmphlprbufsz
, GFP_KERNEL
);
409 BT_ERR("Unable to allocate buffer for helper."
410 " Terminating download");
415 helperbuf
= (u8
*) ALIGN_ADDR(tmphlprbuf
, BTSDIO_DMA_ALIGN
);
417 /* Perform helper data transfer */
418 tx_len
= (FIRMWARE_TRANSFER_NBLOCK
* SDIO_BLOCK_SIZE
)
423 ret
= btmrvl_sdio_poll_card_status(card
,
424 CARD_IO_READY
| DN_LD_CARD_RDY
);
426 BT_ERR("Helper download poll status timeout @ %d",
431 /* Check if there is more data? */
432 if (hlprblknow
>= helperlen
)
435 if (helperlen
- hlprblknow
< tx_len
)
436 tx_len
= helperlen
- hlprblknow
;
439 helperbuf
[0] = ((tx_len
& 0x000000ff) >> 0);
440 helperbuf
[1] = ((tx_len
& 0x0000ff00) >> 8);
441 helperbuf
[2] = ((tx_len
& 0x00ff0000) >> 16);
442 helperbuf
[3] = ((tx_len
& 0xff000000) >> 24);
444 memcpy(&helperbuf
[SDIO_HEADER_LEN
], &helper
[hlprblknow
],
447 /* Now send the data */
448 ret
= sdio_writesb(card
->func
, card
->ioport
, helperbuf
,
449 FIRMWARE_TRANSFER_NBLOCK
* SDIO_BLOCK_SIZE
);
451 BT_ERR("IO error during helper download @ %d",
456 hlprblknow
+= tx_len
;
459 BT_DBG("Transferring helper image EOF block");
461 memset(helperbuf
, 0x0, SDIO_BLOCK_SIZE
);
463 ret
= sdio_writesb(card
->func
, card
->ioport
, helperbuf
,
466 BT_ERR("IO error in writing helper image EOF block");
474 release_firmware(fw_helper
);
478 static int btmrvl_sdio_download_fw_w_helper(struct btmrvl_sdio_card
*card
)
480 const struct firmware
*fw_firmware
= NULL
;
481 const u8
*firmware
= NULL
;
482 int firmwarelen
, tmpfwbufsz
, ret
;
483 unsigned int tries
, offset
;
485 void *tmpfwbuf
= NULL
;
487 u16 len
, blksz_dl
= card
->sd_blksz_fw_dl
;
488 int txlen
= 0, tx_blocks
= 0, count
= 0;
490 ret
= request_firmware(&fw_firmware
, card
->firmware
,
492 if ((ret
< 0) || !fw_firmware
) {
493 BT_ERR("request_firmware(firmware) failed, error code = %d",
499 firmware
= fw_firmware
->data
;
500 firmwarelen
= fw_firmware
->size
;
502 BT_DBG("Downloading FW image (%d bytes)", firmwarelen
);
504 tmpfwbufsz
= ALIGN_SZ(BTM_UPLD_SIZE
, BTSDIO_DMA_ALIGN
);
505 tmpfwbuf
= kzalloc(tmpfwbufsz
, GFP_KERNEL
);
507 BT_ERR("Unable to allocate buffer for firmware."
508 " Terminating download");
513 /* Ensure aligned firmware buffer */
514 fwbuf
= (u8
*) ALIGN_ADDR(tmpfwbuf
, BTSDIO_DMA_ALIGN
);
516 /* Perform firmware data transfer */
519 ret
= btmrvl_sdio_poll_card_status(card
,
520 CARD_IO_READY
| DN_LD_CARD_RDY
);
522 BT_ERR("FW download with helper poll status"
523 " timeout @ %d", offset
);
527 /* Check if there is more data ? */
528 if (offset
>= firmwarelen
)
531 for (tries
= 0; tries
< MAX_POLL_TRIES
; tries
++) {
532 base0
= sdio_readb(card
->func
,
533 card
->reg
->sq_read_base_addr_a0
, &ret
);
535 BT_ERR("BASE0 register read failed:"
536 " base0 = 0x%04X(%d)."
537 " Terminating download",
542 base1
= sdio_readb(card
->func
,
543 card
->reg
->sq_read_base_addr_a1
, &ret
);
545 BT_ERR("BASE1 register read failed:"
546 " base1 = 0x%04X(%d)."
547 " Terminating download",
553 len
= (((u16
) base1
) << 8) | base0
;
562 else if (len
> BTM_UPLD_SIZE
) {
563 BT_ERR("FW download failure @%d, invalid length %d",
573 if (count
> MAX_WRITE_IOMEM_RETRY
) {
574 BT_ERR("FW download failure @%d, "
575 "over max retry count", offset
);
579 BT_ERR("FW CRC error indicated by the helper: "
580 "len = 0x%04X, txlen = %d", len
, txlen
);
582 /* Set txlen to 0 so as to resend from same offset */
588 if (firmwarelen
- offset
< txlen
)
589 txlen
= firmwarelen
- offset
;
591 tx_blocks
= DIV_ROUND_UP(txlen
, blksz_dl
);
593 memcpy(fwbuf
, &firmware
[offset
], txlen
);
596 ret
= sdio_writesb(card
->func
, card
->ioport
, fwbuf
,
597 tx_blocks
* blksz_dl
);
600 BT_ERR("FW download, writesb(%d) failed @%d",
602 sdio_writeb(card
->func
, HOST_CMD53_FIN
,
603 card
->reg
->cfg
, &ret
);
605 BT_ERR("writeb failed (CFG)");
611 BT_INFO("FW download over, size %d bytes", offset
);
617 release_firmware(fw_firmware
);
621 static int btmrvl_sdio_card_to_host(struct btmrvl_private
*priv
)
624 int ret
, num_blocks
, blksz
;
625 struct sk_buff
*skb
= NULL
;
628 struct hci_dev
*hdev
= priv
->btmrvl_dev
.hcidev
;
629 struct btmrvl_sdio_card
*card
= priv
->btmrvl_dev
.card
;
631 if (!card
|| !card
->func
) {
632 BT_ERR("card or function is NULL!");
637 /* Read the length of data to be transferred */
638 ret
= btmrvl_sdio_read_rx_len(card
, &buf_len
);
640 BT_ERR("read rx_len failed");
645 blksz
= SDIO_BLOCK_SIZE
;
646 num_blocks
= DIV_ROUND_UP(buf_len
, blksz
);
648 if (buf_len
<= SDIO_HEADER_LEN
649 || (num_blocks
* blksz
) > ALLOC_BUF_SIZE
) {
650 BT_ERR("invalid packet length: %d", buf_len
);
655 /* Allocate buffer */
656 skb
= bt_skb_alloc(num_blocks
* blksz
+ BTSDIO_DMA_ALIGN
, GFP_ATOMIC
);
658 BT_ERR("No free skb");
663 if ((unsigned long) skb
->data
& (BTSDIO_DMA_ALIGN
- 1)) {
664 skb_put(skb
, (unsigned long) skb
->data
&
665 (BTSDIO_DMA_ALIGN
- 1));
666 skb_pull(skb
, (unsigned long) skb
->data
&
667 (BTSDIO_DMA_ALIGN
- 1));
672 ret
= sdio_readsb(card
->func
, payload
, card
->ioport
,
675 BT_ERR("readsb failed: %d", ret
);
680 /* This is SDIO specific header length: byte[2][1][0], type: byte[3]
681 * (HCI_COMMAND = 1, ACL_DATA = 2, SCO_DATA = 3, 0xFE = Vendor)
684 buf_len
= payload
[0];
685 buf_len
|= payload
[1] << 8;
686 buf_len
|= payload
[2] << 16;
688 if (buf_len
> blksz
* num_blocks
) {
689 BT_ERR("Skip incorrect packet: hdrlen %d buffer %d",
690 buf_len
, blksz
* num_blocks
);
698 case HCI_ACLDATA_PKT
:
699 case HCI_SCODATA_PKT
:
701 bt_cb(skb
)->pkt_type
= type
;
702 skb_put(skb
, buf_len
);
703 skb_pull(skb
, SDIO_HEADER_LEN
);
705 if (type
== HCI_EVENT_PKT
) {
706 if (btmrvl_check_evtpkt(priv
, skb
))
707 hci_recv_frame(hdev
, skb
);
709 hci_recv_frame(hdev
, skb
);
712 hdev
->stat
.byte_rx
+= buf_len
;
715 case MRVL_VENDOR_PKT
:
716 bt_cb(skb
)->pkt_type
= HCI_VENDOR_PKT
;
717 skb_put(skb
, buf_len
);
718 skb_pull(skb
, SDIO_HEADER_LEN
);
720 if (btmrvl_process_event(priv
, skb
))
721 hci_recv_frame(hdev
, skb
);
723 hdev
->stat
.byte_rx
+= buf_len
;
727 BT_ERR("Unknown packet type:%d", type
);
728 BT_ERR("hex: %*ph", blksz
* num_blocks
, payload
);
744 static int btmrvl_sdio_process_int_status(struct btmrvl_private
*priv
)
748 struct btmrvl_sdio_card
*card
= priv
->btmrvl_dev
.card
;
750 spin_lock_irqsave(&priv
->driver_lock
, flags
);
753 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
755 sdio_claim_host(card
->func
);
756 if (ireg
& DN_LD_HOST_INT_STATUS
) {
757 if (priv
->btmrvl_dev
.tx_dnld_rdy
)
758 BT_DBG("tx_done already received: "
759 " int_status=0x%x", ireg
);
761 priv
->btmrvl_dev
.tx_dnld_rdy
= true;
764 if (ireg
& UP_LD_HOST_INT_STATUS
)
765 btmrvl_sdio_card_to_host(priv
);
767 sdio_release_host(card
->func
);
772 static int btmrvl_sdio_read_to_clear(struct btmrvl_sdio_card
*card
, u8
*ireg
)
774 struct btmrvl_adapter
*adapter
= card
->priv
->adapter
;
777 ret
= sdio_readsb(card
->func
, adapter
->hw_regs
, 0, SDIO_BLOCK_SIZE
);
779 BT_ERR("sdio_readsb: read int hw_regs failed: %d", ret
);
783 *ireg
= adapter
->hw_regs
[card
->reg
->host_intstatus
];
784 BT_DBG("hw_regs[%#x]=%#x", card
->reg
->host_intstatus
, *ireg
);
789 static int btmrvl_sdio_write_to_clear(struct btmrvl_sdio_card
*card
, u8
*ireg
)
793 *ireg
= sdio_readb(card
->func
, card
->reg
->host_intstatus
, &ret
);
795 BT_ERR("sdio_readb: read int status failed: %d", ret
);
801 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
802 * Clear the interrupt status register and re-enable the
805 BT_DBG("int_status = 0x%x", *ireg
);
807 sdio_writeb(card
->func
, ~(*ireg
) & (DN_LD_HOST_INT_STATUS
|
808 UP_LD_HOST_INT_STATUS
),
809 card
->reg
->host_intstatus
, &ret
);
811 BT_ERR("sdio_writeb: clear int status failed: %d", ret
);
819 static void btmrvl_sdio_interrupt(struct sdio_func
*func
)
821 struct btmrvl_private
*priv
;
822 struct btmrvl_sdio_card
*card
;
827 card
= sdio_get_drvdata(func
);
828 if (!card
|| !card
->priv
) {
829 BT_ERR("sbi_interrupt(%p) card or priv is NULL, card=%p",
836 if (priv
->surprise_removed
)
839 if (card
->reg
->int_read_to_clear
)
840 ret
= btmrvl_sdio_read_to_clear(card
, &ireg
);
842 ret
= btmrvl_sdio_write_to_clear(card
, &ireg
);
847 spin_lock_irqsave(&priv
->driver_lock
, flags
);
849 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
851 btmrvl_interrupt(priv
);
854 static int btmrvl_sdio_register_dev(struct btmrvl_sdio_card
*card
)
856 struct sdio_func
*func
;
860 if (!card
|| !card
->func
) {
861 BT_ERR("Error: card or function is NULL!");
868 sdio_claim_host(func
);
870 ret
= sdio_enable_func(func
);
872 BT_ERR("sdio_enable_func() failed: ret=%d", ret
);
877 ret
= sdio_claim_irq(func
, btmrvl_sdio_interrupt
);
879 BT_ERR("sdio_claim_irq failed: ret=%d", ret
);
884 ret
= sdio_set_block_size(card
->func
, SDIO_BLOCK_SIZE
);
886 BT_ERR("cannot set SDIO block size");
891 reg
= sdio_readb(func
, card
->reg
->io_port_0
, &ret
);
899 reg
= sdio_readb(func
, card
->reg
->io_port_1
, &ret
);
905 card
->ioport
|= (reg
<< 8);
907 reg
= sdio_readb(func
, card
->reg
->io_port_2
, &ret
);
913 card
->ioport
|= (reg
<< 16);
915 BT_DBG("SDIO FUNC%d IO port: 0x%x", func
->num
, card
->ioport
);
917 if (card
->reg
->int_read_to_clear
) {
918 reg
= sdio_readb(func
, card
->reg
->host_int_rsr
, &ret
);
923 sdio_writeb(func
, reg
| 0x3f, card
->reg
->host_int_rsr
, &ret
);
929 reg
= sdio_readb(func
, card
->reg
->card_misc_cfg
, &ret
);
934 sdio_writeb(func
, reg
| 0x10, card
->reg
->card_misc_cfg
, &ret
);
941 sdio_set_drvdata(func
, card
);
943 sdio_release_host(func
);
948 sdio_release_irq(func
);
951 sdio_disable_func(func
);
954 sdio_release_host(func
);
960 static int btmrvl_sdio_unregister_dev(struct btmrvl_sdio_card
*card
)
962 if (card
&& card
->func
) {
963 sdio_claim_host(card
->func
);
964 sdio_release_irq(card
->func
);
965 sdio_disable_func(card
->func
);
966 sdio_release_host(card
->func
);
967 sdio_set_drvdata(card
->func
, NULL
);
973 static int btmrvl_sdio_enable_host_int(struct btmrvl_sdio_card
*card
)
977 if (!card
|| !card
->func
)
980 sdio_claim_host(card
->func
);
982 ret
= btmrvl_sdio_enable_host_int_mask(card
, HIM_ENABLE
);
984 btmrvl_sdio_get_rx_unit(card
);
986 sdio_release_host(card
->func
);
991 static int btmrvl_sdio_disable_host_int(struct btmrvl_sdio_card
*card
)
995 if (!card
|| !card
->func
)
998 sdio_claim_host(card
->func
);
1000 ret
= btmrvl_sdio_disable_host_int_mask(card
, HIM_DISABLE
);
1002 sdio_release_host(card
->func
);
1007 static int btmrvl_sdio_host_to_card(struct btmrvl_private
*priv
,
1008 u8
*payload
, u16 nb
)
1010 struct btmrvl_sdio_card
*card
= priv
->btmrvl_dev
.card
;
1016 void *tmpbuf
= NULL
;
1019 if (!card
|| !card
->func
) {
1020 BT_ERR("card or function is NULL!");
1025 if ((unsigned long) payload
& (BTSDIO_DMA_ALIGN
- 1)) {
1026 tmpbufsz
= ALIGN_SZ(nb
, BTSDIO_DMA_ALIGN
);
1027 tmpbuf
= kzalloc(tmpbufsz
, GFP_KERNEL
);
1030 buf
= (u8
*) ALIGN_ADDR(tmpbuf
, BTSDIO_DMA_ALIGN
);
1031 memcpy(buf
, payload
, nb
);
1034 blksz
= SDIO_BLOCK_SIZE
;
1035 buf_block_len
= DIV_ROUND_UP(nb
, blksz
);
1037 sdio_claim_host(card
->func
);
1040 /* Transfer data to card */
1041 ret
= sdio_writesb(card
->func
, card
->ioport
, buf
,
1042 buf_block_len
* blksz
);
1045 BT_ERR("i=%d writesb failed: %d", i
, ret
);
1046 BT_ERR("hex: %*ph", nb
, payload
);
1048 if (i
> MAX_WRITE_IOMEM_RETRY
)
1053 priv
->btmrvl_dev
.tx_dnld_rdy
= false;
1056 sdio_release_host(card
->func
);
1062 static int btmrvl_sdio_download_fw(struct btmrvl_sdio_card
*card
)
1066 int pollnum
= MAX_POLL_TRIES
;
1068 if (!card
|| !card
->func
) {
1069 BT_ERR("card or function is NULL!");
1073 if (!btmrvl_sdio_verify_fw_download(card
, 1)) {
1074 BT_DBG("Firmware already downloaded!");
1078 sdio_claim_host(card
->func
);
1080 /* Check if other function driver is downloading the firmware */
1081 fws0
= sdio_readb(card
->func
, card
->reg
->card_fw_status0
, &ret
);
1083 BT_ERR("Failed to read FW downloading status!");
1088 BT_DBG("BT not the winner (%#x). Skip FW downloading", fws0
);
1090 /* Give other function more time to download the firmware */
1094 ret
= btmrvl_sdio_download_helper(card
);
1096 BT_ERR("Failed to download helper!");
1102 if (btmrvl_sdio_download_fw_w_helper(card
)) {
1103 BT_ERR("Failed to download firmware!");
1110 * winner or not, with this test the FW synchronizes when the
1111 * module can continue its initialization
1113 if (btmrvl_sdio_verify_fw_download(card
, pollnum
)) {
1114 BT_ERR("FW failed to be active in time!");
1118 sdio_release_host(card
->func
);
1123 sdio_release_host(card
->func
);
1127 static int btmrvl_sdio_wakeup_fw(struct btmrvl_private
*priv
)
1129 struct btmrvl_sdio_card
*card
= priv
->btmrvl_dev
.card
;
1132 if (!card
|| !card
->func
) {
1133 BT_ERR("card or function is NULL!");
1137 sdio_claim_host(card
->func
);
1139 sdio_writeb(card
->func
, HOST_POWER_UP
, card
->reg
->cfg
, &ret
);
1141 sdio_release_host(card
->func
);
1143 BT_DBG("wake up firmware");
1148 static void btmrvl_sdio_dump_regs(struct btmrvl_private
*priv
)
1150 struct btmrvl_sdio_card
*card
= priv
->btmrvl_dev
.card
;
1152 unsigned int reg
, reg_start
, reg_end
;
1153 char buf
[256], *ptr
;
1154 u8 loop
, func
, data
;
1157 btmrvl_sdio_wakeup_fw(priv
);
1158 sdio_claim_host(card
->func
);
1160 for (loop
= 0; loop
< MAX_LOOP
; loop
++) {
1161 memset(buf
, 0, sizeof(buf
));
1165 /* Read the registers of SDIO function0 */
1175 ptr
+= sprintf(ptr
, "SDIO Func%d (%#x-%#x): ",
1176 func
, reg_start
, reg_end
);
1177 for (reg
= reg_start
; reg
<= reg_end
; reg
++) {
1179 data
= sdio_f0_readb(card
->func
, reg
, &ret
);
1181 data
= sdio_readb(card
->func
, reg
, &ret
);
1184 ptr
+= sprintf(ptr
, "%02x ", data
);
1186 ptr
+= sprintf(ptr
, "ERR");
1194 sdio_release_host(card
->func
);
1197 /* This function read/write firmware */
1199 rdwr_status
btmrvl_sdio_rdwr_firmware(struct btmrvl_private
*priv
,
1202 struct btmrvl_sdio_card
*card
= priv
->btmrvl_dev
.card
;
1206 sdio_writeb(card
->func
, FW_DUMP_HOST_READY
, card
->reg
->fw_dump_ctrl
,
1210 BT_ERR("SDIO write err");
1211 return RDWR_STATUS_FAILURE
;
1214 for (tries
= 0; tries
< MAX_POLL_TRIES
; tries
++) {
1215 ctrl_data
= sdio_readb(card
->func
, card
->reg
->fw_dump_ctrl
,
1219 BT_ERR("SDIO read err");
1220 return RDWR_STATUS_FAILURE
;
1223 if (ctrl_data
== FW_DUMP_DONE
)
1225 if (doneflag
&& ctrl_data
== doneflag
)
1226 return RDWR_STATUS_DONE
;
1227 if (ctrl_data
!= FW_DUMP_HOST_READY
) {
1228 BT_INFO("The ctrl reg was changed, re-try again!");
1229 sdio_writeb(card
->func
, FW_DUMP_HOST_READY
,
1230 card
->reg
->fw_dump_ctrl
, &ret
);
1232 BT_ERR("SDIO write err");
1233 return RDWR_STATUS_FAILURE
;
1236 usleep_range(100, 200);
1239 if (ctrl_data
== FW_DUMP_HOST_READY
) {
1240 BT_ERR("Fail to pull ctrl_data");
1241 return RDWR_STATUS_FAILURE
;
1244 return RDWR_STATUS_SUCCESS
;
1247 /* This function dump sdio register and memory data */
1248 static void btmrvl_sdio_dump_firmware(struct btmrvl_private
*priv
)
1250 struct btmrvl_sdio_card
*card
= priv
->btmrvl_dev
.card
;
1252 unsigned int reg
, reg_start
, reg_end
;
1253 enum rdwr_status stat
;
1254 u8
*dbg_ptr
, *end_ptr
, *fw_dump_data
, *fw_dump_ptr
;
1255 u8 dump_num
= 0, idx
, i
, read_reg
, doneflag
= 0;
1256 u32 memory_size
, fw_dump_len
= 0;
1258 /* dump sdio register first */
1259 btmrvl_sdio_dump_regs(priv
);
1261 if (!card
->supports_fw_dump
) {
1262 BT_ERR("Firmware dump not supported for this card!");
1266 for (idx
= 0; idx
< ARRAY_SIZE(mem_type_mapping_tbl
); idx
++) {
1267 struct memory_type_mapping
*entry
= &mem_type_mapping_tbl
[idx
];
1269 if (entry
->mem_ptr
) {
1270 vfree(entry
->mem_ptr
);
1271 entry
->mem_ptr
= NULL
;
1273 entry
->mem_size
= 0;
1276 btmrvl_sdio_wakeup_fw(priv
);
1277 sdio_claim_host(card
->func
);
1279 BT_INFO("== btmrvl firmware dump start ==");
1281 stat
= btmrvl_sdio_rdwr_firmware(priv
, doneflag
);
1282 if (stat
== RDWR_STATUS_FAILURE
)
1285 reg
= card
->reg
->fw_dump_start
;
1286 /* Read the number of the memories which will dump */
1287 dump_num
= sdio_readb(card
->func
, reg
, &ret
);
1290 BT_ERR("SDIO read memory length err");
1294 /* Read the length of every memory which will dump */
1295 for (idx
= 0; idx
< dump_num
; idx
++) {
1296 struct memory_type_mapping
*entry
= &mem_type_mapping_tbl
[idx
];
1298 stat
= btmrvl_sdio_rdwr_firmware(priv
, doneflag
);
1299 if (stat
== RDWR_STATUS_FAILURE
)
1303 reg
= card
->reg
->fw_dump_start
;
1304 for (i
= 0; i
< 4; i
++) {
1305 read_reg
= sdio_readb(card
->func
, reg
, &ret
);
1307 BT_ERR("SDIO read err");
1310 memory_size
|= (read_reg
<< i
*8);
1314 if (memory_size
== 0) {
1315 BT_INFO("Firmware dump finished!");
1316 sdio_writeb(card
->func
, FW_DUMP_READ_DONE
,
1317 card
->reg
->fw_dump_ctrl
, &ret
);
1319 BT_ERR("SDIO Write MEMDUMP_FINISH ERR");
1325 BT_INFO("%s_SIZE=0x%x", entry
->mem_name
, memory_size
);
1326 entry
->mem_ptr
= vzalloc(memory_size
+ 1);
1327 entry
->mem_size
= memory_size
;
1328 if (!entry
->mem_ptr
) {
1329 BT_ERR("Vzalloc %s failed", entry
->mem_name
);
1333 fw_dump_len
+= (strlen("========Start dump ") +
1334 strlen(entry
->mem_name
) +
1335 strlen("========\n") +
1337 strlen("\n========End dump========\n"));
1339 dbg_ptr
= entry
->mem_ptr
;
1340 end_ptr
= dbg_ptr
+ memory_size
;
1342 doneflag
= entry
->done_flag
;
1343 BT_INFO("Start %s output, please wait...",
1347 stat
= btmrvl_sdio_rdwr_firmware(priv
, doneflag
);
1348 if (stat
== RDWR_STATUS_FAILURE
)
1351 reg_start
= card
->reg
->fw_dump_start
;
1352 reg_end
= card
->reg
->fw_dump_end
;
1353 for (reg
= reg_start
; reg
<= reg_end
; reg
++) {
1354 *dbg_ptr
= sdio_readb(card
->func
, reg
, &ret
);
1356 BT_ERR("SDIO read err");
1359 if (dbg_ptr
< end_ptr
)
1362 BT_ERR("Allocated buffer not enough");
1365 if (stat
!= RDWR_STATUS_DONE
) {
1368 BT_INFO("%s done: size=0x%tx",
1370 dbg_ptr
- entry
->mem_ptr
);
1376 BT_INFO("== btmrvl firmware dump end ==");
1379 sdio_release_host(card
->func
);
1381 if (fw_dump_len
== 0)
1384 fw_dump_data
= vzalloc(fw_dump_len
+1);
1385 if (!fw_dump_data
) {
1386 BT_ERR("Vzalloc fw_dump_data fail!");
1389 fw_dump_ptr
= fw_dump_data
;
1391 /* Dump all the memory data into single file, a userspace script will
1392 be used to split all the memory data to multiple files*/
1393 BT_INFO("== btmrvl firmware dump to /sys/class/devcoredump start");
1394 for (idx
= 0; idx
< dump_num
; idx
++) {
1395 struct memory_type_mapping
*entry
= &mem_type_mapping_tbl
[idx
];
1397 if (entry
->mem_ptr
) {
1398 strcpy(fw_dump_ptr
, "========Start dump ");
1399 fw_dump_ptr
+= strlen("========Start dump ");
1401 strcpy(fw_dump_ptr
, entry
->mem_name
);
1402 fw_dump_ptr
+= strlen(entry
->mem_name
);
1404 strcpy(fw_dump_ptr
, "========\n");
1405 fw_dump_ptr
+= strlen("========\n");
1407 memcpy(fw_dump_ptr
, entry
->mem_ptr
, entry
->mem_size
);
1408 fw_dump_ptr
+= entry
->mem_size
;
1410 strcpy(fw_dump_ptr
, "\n========End dump========\n");
1411 fw_dump_ptr
+= strlen("\n========End dump========\n");
1413 vfree(mem_type_mapping_tbl
[idx
].mem_ptr
);
1414 mem_type_mapping_tbl
[idx
].mem_ptr
= NULL
;
1418 /* fw_dump_data will be free in device coredump release function
1420 dev_coredumpv(&card
->func
->dev
, fw_dump_data
, fw_dump_len
, GFP_KERNEL
);
1421 BT_INFO("== btmrvl firmware dump to /sys/class/devcoredump end");
1424 static int btmrvl_sdio_probe(struct sdio_func
*func
,
1425 const struct sdio_device_id
*id
)
1428 struct btmrvl_private
*priv
= NULL
;
1429 struct btmrvl_sdio_card
*card
= NULL
;
1431 BT_INFO("vendor=0x%x, device=0x%x, class=%d, fn=%d",
1432 id
->vendor
, id
->device
, id
->class, func
->num
);
1434 card
= devm_kzalloc(&func
->dev
, sizeof(*card
), GFP_KERNEL
);
1440 if (id
->driver_data
) {
1441 struct btmrvl_sdio_device
*data
= (void *) id
->driver_data
;
1442 card
->helper
= data
->helper
;
1443 card
->firmware
= data
->firmware
;
1444 card
->reg
= data
->reg
;
1445 card
->sd_blksz_fw_dl
= data
->sd_blksz_fw_dl
;
1446 card
->support_pscan_win_report
= data
->support_pscan_win_report
;
1447 card
->supports_fw_dump
= data
->supports_fw_dump
;
1450 if (btmrvl_sdio_register_dev(card
) < 0) {
1451 BT_ERR("Failed to register BT device!");
1455 /* Disable the interrupts on the card */
1456 btmrvl_sdio_disable_host_int(card
);
1458 if (btmrvl_sdio_download_fw(card
)) {
1459 BT_ERR("Downloading firmware failed!");
1464 btmrvl_sdio_enable_host_int(card
);
1466 priv
= btmrvl_add_card(card
);
1468 BT_ERR("Initializing card failed!");
1470 goto disable_host_int
;
1475 /* Initialize the interface specific function pointers */
1476 priv
->hw_host_to_card
= btmrvl_sdio_host_to_card
;
1477 priv
->hw_wakeup_firmware
= btmrvl_sdio_wakeup_fw
;
1478 priv
->hw_process_int_status
= btmrvl_sdio_process_int_status
;
1479 priv
->firmware_dump
= btmrvl_sdio_dump_firmware
;
1481 if (btmrvl_register_hdev(priv
)) {
1482 BT_ERR("Register hdev failed!");
1484 goto disable_host_int
;
1490 btmrvl_sdio_disable_host_int(card
);
1492 btmrvl_sdio_unregister_dev(card
);
1496 static void btmrvl_sdio_remove(struct sdio_func
*func
)
1498 struct btmrvl_sdio_card
*card
;
1501 card
= sdio_get_drvdata(func
);
1503 /* Send SHUTDOWN command & disable interrupt
1504 * if user removes the module.
1507 btmrvl_send_module_cfg_cmd(card
->priv
,
1508 MODULE_SHUTDOWN_REQ
);
1509 btmrvl_sdio_disable_host_int(card
);
1511 BT_DBG("unregester dev");
1512 card
->priv
->surprise_removed
= true;
1513 btmrvl_sdio_unregister_dev(card
);
1514 btmrvl_remove_card(card
->priv
);
1519 static int btmrvl_sdio_suspend(struct device
*dev
)
1521 struct sdio_func
*func
= dev_to_sdio_func(dev
);
1522 struct btmrvl_sdio_card
*card
;
1523 struct btmrvl_private
*priv
;
1524 mmc_pm_flag_t pm_flags
;
1525 struct hci_dev
*hcidev
;
1528 pm_flags
= sdio_get_host_pm_caps(func
);
1529 BT_DBG("%s: suspend: PM flags = 0x%x", sdio_func_id(func
),
1531 if (!(pm_flags
& MMC_PM_KEEP_POWER
)) {
1532 BT_ERR("%s: cannot remain alive while suspended",
1533 sdio_func_id(func
));
1536 card
= sdio_get_drvdata(func
);
1537 if (!card
|| !card
->priv
) {
1538 BT_ERR("card or priv structure is not valid");
1542 BT_ERR("sdio_func is not specified");
1547 hcidev
= priv
->btmrvl_dev
.hcidev
;
1548 BT_DBG("%s: SDIO suspend", hcidev
->name
);
1549 hci_suspend_dev(hcidev
);
1550 skb_queue_purge(&priv
->adapter
->tx_queue
);
1552 if (priv
->adapter
->hs_state
!= HS_ACTIVATED
) {
1553 if (btmrvl_enable_hs(priv
)) {
1554 BT_ERR("HS not actived, suspend failed!");
1559 priv
->adapter
->is_suspended
= true;
1561 /* We will keep the power when hs enabled successfully */
1562 if (priv
->adapter
->hs_state
== HS_ACTIVATED
) {
1563 BT_DBG("suspend with MMC_PM_KEEP_POWER");
1564 return sdio_set_host_pm_flags(func
, MMC_PM_KEEP_POWER
);
1566 BT_DBG("suspend without MMC_PM_KEEP_POWER");
1571 static int btmrvl_sdio_resume(struct device
*dev
)
1573 struct sdio_func
*func
= dev_to_sdio_func(dev
);
1574 struct btmrvl_sdio_card
*card
;
1575 struct btmrvl_private
*priv
;
1576 mmc_pm_flag_t pm_flags
;
1577 struct hci_dev
*hcidev
;
1580 pm_flags
= sdio_get_host_pm_caps(func
);
1581 BT_DBG("%s: resume: PM flags = 0x%x", sdio_func_id(func
),
1583 card
= sdio_get_drvdata(func
);
1584 if (!card
|| !card
->priv
) {
1585 BT_ERR("card or priv structure is not valid");
1589 BT_ERR("sdio_func is not specified");
1594 if (!priv
->adapter
->is_suspended
) {
1595 BT_DBG("device already resumed");
1599 priv
->hw_wakeup_firmware(priv
);
1600 priv
->adapter
->hs_state
= HS_DEACTIVATED
;
1601 hcidev
= priv
->btmrvl_dev
.hcidev
;
1602 BT_DBG("%s: HS DEACTIVATED in resume!", hcidev
->name
);
1603 priv
->adapter
->is_suspended
= false;
1604 BT_DBG("%s: SDIO resume", hcidev
->name
);
1605 hci_resume_dev(hcidev
);
1610 static const struct dev_pm_ops btmrvl_sdio_pm_ops
= {
1611 .suspend
= btmrvl_sdio_suspend
,
1612 .resume
= btmrvl_sdio_resume
,
1615 static struct sdio_driver bt_mrvl_sdio
= {
1616 .name
= "btmrvl_sdio",
1617 .id_table
= btmrvl_sdio_ids
,
1618 .probe
= btmrvl_sdio_probe
,
1619 .remove
= btmrvl_sdio_remove
,
1621 .owner
= THIS_MODULE
,
1622 .pm
= &btmrvl_sdio_pm_ops
,
1626 static int __init
btmrvl_sdio_init_module(void)
1628 if (sdio_register_driver(&bt_mrvl_sdio
) != 0) {
1629 BT_ERR("SDIO Driver Registration Failed");
1633 /* Clear the flag in case user removes the card. */
1639 static void __exit
btmrvl_sdio_exit_module(void)
1641 /* Set the flag as user is removing this module. */
1644 sdio_unregister_driver(&bt_mrvl_sdio
);
1647 module_init(btmrvl_sdio_init_module
);
1648 module_exit(btmrvl_sdio_exit_module
);
1650 MODULE_AUTHOR("Marvell International Ltd.");
1651 MODULE_DESCRIPTION("Marvell BT-over-SDIO driver ver " VERSION
);
1652 MODULE_VERSION(VERSION
);
1653 MODULE_LICENSE("GPL v2");
1654 MODULE_FIRMWARE("mrvl/sd8688_helper.bin");
1655 MODULE_FIRMWARE("mrvl/sd8688.bin");
1656 MODULE_FIRMWARE("mrvl/sd8787_uapsta.bin");
1657 MODULE_FIRMWARE("mrvl/sd8797_uapsta.bin");
1658 MODULE_FIRMWARE("mrvl/sd8887_uapsta.bin");
1659 MODULE_FIRMWARE("mrvl/sd8897_uapsta.bin");
1660 MODULE_FIRMWARE("mrvl/sd8997_uapsta.bin");