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_device btmrvl_sdio_sd8688
= {
150 .helper
= "mrvl/sd8688_helper.bin",
151 .firmware
= "mrvl/sd8688.bin",
152 .reg
= &btmrvl_reg_8688
,
153 .support_pscan_win_report
= false,
154 .sd_blksz_fw_dl
= 64,
155 .supports_fw_dump
= false,
158 static const struct btmrvl_sdio_device btmrvl_sdio_sd8787
= {
160 .firmware
= "mrvl/sd8787_uapsta.bin",
161 .reg
= &btmrvl_reg_87xx
,
162 .support_pscan_win_report
= false,
163 .sd_blksz_fw_dl
= 256,
164 .supports_fw_dump
= false,
167 static const struct btmrvl_sdio_device btmrvl_sdio_sd8797
= {
169 .firmware
= "mrvl/sd8797_uapsta.bin",
170 .reg
= &btmrvl_reg_87xx
,
171 .support_pscan_win_report
= false,
172 .sd_blksz_fw_dl
= 256,
173 .supports_fw_dump
= false,
176 static const struct btmrvl_sdio_device btmrvl_sdio_sd8887
= {
178 .firmware
= "mrvl/sd8887_uapsta.bin",
179 .reg
= &btmrvl_reg_8887
,
180 .support_pscan_win_report
= true,
181 .sd_blksz_fw_dl
= 256,
182 .supports_fw_dump
= false,
185 static const struct btmrvl_sdio_device btmrvl_sdio_sd8897
= {
187 .firmware
= "mrvl/sd8897_uapsta.bin",
188 .reg
= &btmrvl_reg_8897
,
189 .support_pscan_win_report
= true,
190 .sd_blksz_fw_dl
= 256,
191 .supports_fw_dump
= true,
194 static const struct sdio_device_id btmrvl_sdio_ids
[] = {
195 /* Marvell SD8688 Bluetooth device */
196 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL
, 0x9105),
197 .driver_data
= (unsigned long) &btmrvl_sdio_sd8688
},
198 /* Marvell SD8787 Bluetooth device */
199 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL
, 0x911A),
200 .driver_data
= (unsigned long) &btmrvl_sdio_sd8787
},
201 /* Marvell SD8787 Bluetooth AMP device */
202 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL
, 0x911B),
203 .driver_data
= (unsigned long) &btmrvl_sdio_sd8787
},
204 /* Marvell SD8797 Bluetooth device */
205 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL
, 0x912A),
206 .driver_data
= (unsigned long) &btmrvl_sdio_sd8797
},
207 /* Marvell SD8887 Bluetooth device */
208 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL
, 0x9136),
209 .driver_data
= (unsigned long)&btmrvl_sdio_sd8887
},
210 /* Marvell SD8897 Bluetooth device */
211 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL
, 0x912E),
212 .driver_data
= (unsigned long) &btmrvl_sdio_sd8897
},
214 { } /* Terminating entry */
217 MODULE_DEVICE_TABLE(sdio
, btmrvl_sdio_ids
);
219 static int btmrvl_sdio_get_rx_unit(struct btmrvl_sdio_card
*card
)
224 reg
= sdio_readb(card
->func
, card
->reg
->card_rx_unit
, &ret
);
231 static int btmrvl_sdio_read_fw_status(struct btmrvl_sdio_card
*card
, u16
*dat
)
238 fws0
= sdio_readb(card
->func
, card
->reg
->card_fw_status0
, &ret
);
242 fws1
= sdio_readb(card
->func
, card
->reg
->card_fw_status1
, &ret
);
246 *dat
= (((u16
) fws1
) << 8) | fws0
;
251 static int btmrvl_sdio_read_rx_len(struct btmrvl_sdio_card
*card
, u16
*dat
)
256 reg
= sdio_readb(card
->func
, card
->reg
->card_rx_len
, &ret
);
258 *dat
= (u16
) reg
<< card
->rx_unit
;
263 static int btmrvl_sdio_enable_host_int_mask(struct btmrvl_sdio_card
*card
,
268 sdio_writeb(card
->func
, mask
, card
->reg
->host_int_mask
, &ret
);
270 BT_ERR("Unable to enable the host interrupt!");
277 static int btmrvl_sdio_disable_host_int_mask(struct btmrvl_sdio_card
*card
,
283 host_int_mask
= sdio_readb(card
->func
, card
->reg
->host_int_mask
, &ret
);
287 host_int_mask
&= ~mask
;
289 sdio_writeb(card
->func
, host_int_mask
, card
->reg
->host_int_mask
, &ret
);
291 BT_ERR("Unable to disable the host interrupt!");
298 static int btmrvl_sdio_poll_card_status(struct btmrvl_sdio_card
*card
, u8 bits
)
304 for (tries
= 0; tries
< MAX_POLL_TRIES
* 1000; tries
++) {
305 status
= sdio_readb(card
->func
, card
->reg
->card_status
, &ret
);
308 if ((status
& bits
) == bits
)
317 BT_ERR("FAILED! ret=%d", ret
);
322 static int btmrvl_sdio_verify_fw_download(struct btmrvl_sdio_card
*card
,
328 /* Wait for firmware to become ready */
329 for (tries
= 0; tries
< pollnum
; tries
++) {
330 sdio_claim_host(card
->func
);
331 ret
= btmrvl_sdio_read_fw_status(card
, &firmwarestat
);
332 sdio_release_host(card
->func
);
336 if (firmwarestat
== FIRMWARE_READY
)
345 static int btmrvl_sdio_download_helper(struct btmrvl_sdio_card
*card
)
347 const struct firmware
*fw_helper
= NULL
;
348 const u8
*helper
= NULL
;
350 void *tmphlprbuf
= NULL
;
351 int tmphlprbufsz
, hlprblknow
, helperlen
;
355 ret
= request_firmware(&fw_helper
, card
->helper
,
357 if ((ret
< 0) || !fw_helper
) {
358 BT_ERR("request_firmware(helper) failed, error code = %d",
364 helper
= fw_helper
->data
;
365 helperlen
= fw_helper
->size
;
367 BT_DBG("Downloading helper image (%d bytes), block size %d bytes",
368 helperlen
, SDIO_BLOCK_SIZE
);
370 tmphlprbufsz
= ALIGN_SZ(BTM_UPLD_SIZE
, BTSDIO_DMA_ALIGN
);
372 tmphlprbuf
= kzalloc(tmphlprbufsz
, GFP_KERNEL
);
374 BT_ERR("Unable to allocate buffer for helper."
375 " Terminating download");
380 helperbuf
= (u8
*) ALIGN_ADDR(tmphlprbuf
, BTSDIO_DMA_ALIGN
);
382 /* Perform helper data transfer */
383 tx_len
= (FIRMWARE_TRANSFER_NBLOCK
* SDIO_BLOCK_SIZE
)
388 ret
= btmrvl_sdio_poll_card_status(card
,
389 CARD_IO_READY
| DN_LD_CARD_RDY
);
391 BT_ERR("Helper download poll status timeout @ %d",
396 /* Check if there is more data? */
397 if (hlprblknow
>= helperlen
)
400 if (helperlen
- hlprblknow
< tx_len
)
401 tx_len
= helperlen
- hlprblknow
;
404 helperbuf
[0] = ((tx_len
& 0x000000ff) >> 0);
405 helperbuf
[1] = ((tx_len
& 0x0000ff00) >> 8);
406 helperbuf
[2] = ((tx_len
& 0x00ff0000) >> 16);
407 helperbuf
[3] = ((tx_len
& 0xff000000) >> 24);
409 memcpy(&helperbuf
[SDIO_HEADER_LEN
], &helper
[hlprblknow
],
412 /* Now send the data */
413 ret
= sdio_writesb(card
->func
, card
->ioport
, helperbuf
,
414 FIRMWARE_TRANSFER_NBLOCK
* SDIO_BLOCK_SIZE
);
416 BT_ERR("IO error during helper download @ %d",
421 hlprblknow
+= tx_len
;
424 BT_DBG("Transferring helper image EOF block");
426 memset(helperbuf
, 0x0, SDIO_BLOCK_SIZE
);
428 ret
= sdio_writesb(card
->func
, card
->ioport
, helperbuf
,
431 BT_ERR("IO error in writing helper image EOF block");
439 release_firmware(fw_helper
);
443 static int btmrvl_sdio_download_fw_w_helper(struct btmrvl_sdio_card
*card
)
445 const struct firmware
*fw_firmware
= NULL
;
446 const u8
*firmware
= NULL
;
447 int firmwarelen
, tmpfwbufsz
, ret
;
448 unsigned int tries
, offset
;
450 void *tmpfwbuf
= NULL
;
452 u16 len
, blksz_dl
= card
->sd_blksz_fw_dl
;
453 int txlen
= 0, tx_blocks
= 0, count
= 0;
455 ret
= request_firmware(&fw_firmware
, card
->firmware
,
457 if ((ret
< 0) || !fw_firmware
) {
458 BT_ERR("request_firmware(firmware) failed, error code = %d",
464 firmware
= fw_firmware
->data
;
465 firmwarelen
= fw_firmware
->size
;
467 BT_DBG("Downloading FW image (%d bytes)", firmwarelen
);
469 tmpfwbufsz
= ALIGN_SZ(BTM_UPLD_SIZE
, BTSDIO_DMA_ALIGN
);
470 tmpfwbuf
= kzalloc(tmpfwbufsz
, GFP_KERNEL
);
472 BT_ERR("Unable to allocate buffer for firmware."
473 " Terminating download");
478 /* Ensure aligned firmware buffer */
479 fwbuf
= (u8
*) ALIGN_ADDR(tmpfwbuf
, BTSDIO_DMA_ALIGN
);
481 /* Perform firmware data transfer */
484 ret
= btmrvl_sdio_poll_card_status(card
,
485 CARD_IO_READY
| DN_LD_CARD_RDY
);
487 BT_ERR("FW download with helper poll status"
488 " timeout @ %d", offset
);
492 /* Check if there is more data ? */
493 if (offset
>= firmwarelen
)
496 for (tries
= 0; tries
< MAX_POLL_TRIES
; tries
++) {
497 base0
= sdio_readb(card
->func
,
498 card
->reg
->sq_read_base_addr_a0
, &ret
);
500 BT_ERR("BASE0 register read failed:"
501 " base0 = 0x%04X(%d)."
502 " Terminating download",
507 base1
= sdio_readb(card
->func
,
508 card
->reg
->sq_read_base_addr_a1
, &ret
);
510 BT_ERR("BASE1 register read failed:"
511 " base1 = 0x%04X(%d)."
512 " Terminating download",
518 len
= (((u16
) base1
) << 8) | base0
;
527 else if (len
> BTM_UPLD_SIZE
) {
528 BT_ERR("FW download failure @%d, invalid length %d",
538 if (count
> MAX_WRITE_IOMEM_RETRY
) {
539 BT_ERR("FW download failure @%d, "
540 "over max retry count", offset
);
544 BT_ERR("FW CRC error indicated by the helper: "
545 "len = 0x%04X, txlen = %d", len
, txlen
);
547 /* Set txlen to 0 so as to resend from same offset */
553 if (firmwarelen
- offset
< txlen
)
554 txlen
= firmwarelen
- offset
;
556 tx_blocks
= DIV_ROUND_UP(txlen
, blksz_dl
);
558 memcpy(fwbuf
, &firmware
[offset
], txlen
);
561 ret
= sdio_writesb(card
->func
, card
->ioport
, fwbuf
,
562 tx_blocks
* blksz_dl
);
565 BT_ERR("FW download, writesb(%d) failed @%d",
567 sdio_writeb(card
->func
, HOST_CMD53_FIN
,
568 card
->reg
->cfg
, &ret
);
570 BT_ERR("writeb failed (CFG)");
576 BT_INFO("FW download over, size %d bytes", offset
);
582 release_firmware(fw_firmware
);
586 static int btmrvl_sdio_card_to_host(struct btmrvl_private
*priv
)
589 int ret
, num_blocks
, blksz
;
590 struct sk_buff
*skb
= NULL
;
593 struct hci_dev
*hdev
= priv
->btmrvl_dev
.hcidev
;
594 struct btmrvl_sdio_card
*card
= priv
->btmrvl_dev
.card
;
596 if (!card
|| !card
->func
) {
597 BT_ERR("card or function is NULL!");
602 /* Read the length of data to be transferred */
603 ret
= btmrvl_sdio_read_rx_len(card
, &buf_len
);
605 BT_ERR("read rx_len failed");
610 blksz
= SDIO_BLOCK_SIZE
;
611 num_blocks
= DIV_ROUND_UP(buf_len
, blksz
);
613 if (buf_len
<= SDIO_HEADER_LEN
614 || (num_blocks
* blksz
) > ALLOC_BUF_SIZE
) {
615 BT_ERR("invalid packet length: %d", buf_len
);
620 /* Allocate buffer */
621 skb
= bt_skb_alloc(num_blocks
* blksz
+ BTSDIO_DMA_ALIGN
, GFP_ATOMIC
);
623 BT_ERR("No free skb");
628 if ((unsigned long) skb
->data
& (BTSDIO_DMA_ALIGN
- 1)) {
629 skb_put(skb
, (unsigned long) skb
->data
&
630 (BTSDIO_DMA_ALIGN
- 1));
631 skb_pull(skb
, (unsigned long) skb
->data
&
632 (BTSDIO_DMA_ALIGN
- 1));
637 ret
= sdio_readsb(card
->func
, payload
, card
->ioport
,
640 BT_ERR("readsb failed: %d", ret
);
645 /* This is SDIO specific header length: byte[2][1][0], type: byte[3]
646 * (HCI_COMMAND = 1, ACL_DATA = 2, SCO_DATA = 3, 0xFE = Vendor)
649 buf_len
= payload
[0];
650 buf_len
|= payload
[1] << 8;
651 buf_len
|= payload
[2] << 16;
653 if (buf_len
> blksz
* num_blocks
) {
654 BT_ERR("Skip incorrect packet: hdrlen %d buffer %d",
655 buf_len
, blksz
* num_blocks
);
663 case HCI_ACLDATA_PKT
:
664 case HCI_SCODATA_PKT
:
666 bt_cb(skb
)->pkt_type
= type
;
667 skb_put(skb
, buf_len
);
668 skb_pull(skb
, SDIO_HEADER_LEN
);
670 if (type
== HCI_EVENT_PKT
) {
671 if (btmrvl_check_evtpkt(priv
, skb
))
672 hci_recv_frame(hdev
, skb
);
674 hci_recv_frame(hdev
, skb
);
677 hdev
->stat
.byte_rx
+= buf_len
;
680 case MRVL_VENDOR_PKT
:
681 bt_cb(skb
)->pkt_type
= HCI_VENDOR_PKT
;
682 skb_put(skb
, buf_len
);
683 skb_pull(skb
, SDIO_HEADER_LEN
);
685 if (btmrvl_process_event(priv
, skb
))
686 hci_recv_frame(hdev
, skb
);
688 hdev
->stat
.byte_rx
+= buf_len
;
692 BT_ERR("Unknown packet type:%d", type
);
693 BT_ERR("hex: %*ph", blksz
* num_blocks
, payload
);
709 static int btmrvl_sdio_process_int_status(struct btmrvl_private
*priv
)
713 struct btmrvl_sdio_card
*card
= priv
->btmrvl_dev
.card
;
715 spin_lock_irqsave(&priv
->driver_lock
, flags
);
718 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
720 sdio_claim_host(card
->func
);
721 if (ireg
& DN_LD_HOST_INT_STATUS
) {
722 if (priv
->btmrvl_dev
.tx_dnld_rdy
)
723 BT_DBG("tx_done already received: "
724 " int_status=0x%x", ireg
);
726 priv
->btmrvl_dev
.tx_dnld_rdy
= true;
729 if (ireg
& UP_LD_HOST_INT_STATUS
)
730 btmrvl_sdio_card_to_host(priv
);
732 sdio_release_host(card
->func
);
737 static int btmrvl_sdio_read_to_clear(struct btmrvl_sdio_card
*card
, u8
*ireg
)
739 struct btmrvl_adapter
*adapter
= card
->priv
->adapter
;
742 ret
= sdio_readsb(card
->func
, adapter
->hw_regs
, 0, SDIO_BLOCK_SIZE
);
744 BT_ERR("sdio_readsb: read int hw_regs failed: %d", ret
);
748 *ireg
= adapter
->hw_regs
[card
->reg
->host_intstatus
];
749 BT_DBG("hw_regs[%#x]=%#x", card
->reg
->host_intstatus
, *ireg
);
754 static int btmrvl_sdio_write_to_clear(struct btmrvl_sdio_card
*card
, u8
*ireg
)
758 *ireg
= sdio_readb(card
->func
, card
->reg
->host_intstatus
, &ret
);
760 BT_ERR("sdio_readb: read int status failed: %d", ret
);
766 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
767 * Clear the interrupt status register and re-enable the
770 BT_DBG("int_status = 0x%x", *ireg
);
772 sdio_writeb(card
->func
, ~(*ireg
) & (DN_LD_HOST_INT_STATUS
|
773 UP_LD_HOST_INT_STATUS
),
774 card
->reg
->host_intstatus
, &ret
);
776 BT_ERR("sdio_writeb: clear int status failed: %d", ret
);
784 static void btmrvl_sdio_interrupt(struct sdio_func
*func
)
786 struct btmrvl_private
*priv
;
787 struct btmrvl_sdio_card
*card
;
792 card
= sdio_get_drvdata(func
);
793 if (!card
|| !card
->priv
) {
794 BT_ERR("sbi_interrupt(%p) card or priv is NULL, card=%p",
801 if (priv
->surprise_removed
)
804 if (card
->reg
->int_read_to_clear
)
805 ret
= btmrvl_sdio_read_to_clear(card
, &ireg
);
807 ret
= btmrvl_sdio_write_to_clear(card
, &ireg
);
812 spin_lock_irqsave(&priv
->driver_lock
, flags
);
814 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
816 btmrvl_interrupt(priv
);
819 static int btmrvl_sdio_register_dev(struct btmrvl_sdio_card
*card
)
821 struct sdio_func
*func
;
825 if (!card
|| !card
->func
) {
826 BT_ERR("Error: card or function is NULL!");
833 sdio_claim_host(func
);
835 ret
= sdio_enable_func(func
);
837 BT_ERR("sdio_enable_func() failed: ret=%d", ret
);
842 ret
= sdio_claim_irq(func
, btmrvl_sdio_interrupt
);
844 BT_ERR("sdio_claim_irq failed: ret=%d", ret
);
849 ret
= sdio_set_block_size(card
->func
, SDIO_BLOCK_SIZE
);
851 BT_ERR("cannot set SDIO block size");
856 reg
= sdio_readb(func
, card
->reg
->io_port_0
, &ret
);
864 reg
= sdio_readb(func
, card
->reg
->io_port_1
, &ret
);
870 card
->ioport
|= (reg
<< 8);
872 reg
= sdio_readb(func
, card
->reg
->io_port_2
, &ret
);
878 card
->ioport
|= (reg
<< 16);
880 BT_DBG("SDIO FUNC%d IO port: 0x%x", func
->num
, card
->ioport
);
882 if (card
->reg
->int_read_to_clear
) {
883 reg
= sdio_readb(func
, card
->reg
->host_int_rsr
, &ret
);
888 sdio_writeb(func
, reg
| 0x3f, card
->reg
->host_int_rsr
, &ret
);
894 reg
= sdio_readb(func
, card
->reg
->card_misc_cfg
, &ret
);
899 sdio_writeb(func
, reg
| 0x10, card
->reg
->card_misc_cfg
, &ret
);
906 sdio_set_drvdata(func
, card
);
908 sdio_release_host(func
);
913 sdio_release_irq(func
);
916 sdio_disable_func(func
);
919 sdio_release_host(func
);
925 static int btmrvl_sdio_unregister_dev(struct btmrvl_sdio_card
*card
)
927 if (card
&& card
->func
) {
928 sdio_claim_host(card
->func
);
929 sdio_release_irq(card
->func
);
930 sdio_disable_func(card
->func
);
931 sdio_release_host(card
->func
);
932 sdio_set_drvdata(card
->func
, NULL
);
938 static int btmrvl_sdio_enable_host_int(struct btmrvl_sdio_card
*card
)
942 if (!card
|| !card
->func
)
945 sdio_claim_host(card
->func
);
947 ret
= btmrvl_sdio_enable_host_int_mask(card
, HIM_ENABLE
);
949 btmrvl_sdio_get_rx_unit(card
);
951 sdio_release_host(card
->func
);
956 static int btmrvl_sdio_disable_host_int(struct btmrvl_sdio_card
*card
)
960 if (!card
|| !card
->func
)
963 sdio_claim_host(card
->func
);
965 ret
= btmrvl_sdio_disable_host_int_mask(card
, HIM_DISABLE
);
967 sdio_release_host(card
->func
);
972 static int btmrvl_sdio_host_to_card(struct btmrvl_private
*priv
,
975 struct btmrvl_sdio_card
*card
= priv
->btmrvl_dev
.card
;
984 if (!card
|| !card
->func
) {
985 BT_ERR("card or function is NULL!");
990 if ((unsigned long) payload
& (BTSDIO_DMA_ALIGN
- 1)) {
991 tmpbufsz
= ALIGN_SZ(nb
, BTSDIO_DMA_ALIGN
);
992 tmpbuf
= kzalloc(tmpbufsz
, GFP_KERNEL
);
995 buf
= (u8
*) ALIGN_ADDR(tmpbuf
, BTSDIO_DMA_ALIGN
);
996 memcpy(buf
, payload
, nb
);
999 blksz
= SDIO_BLOCK_SIZE
;
1000 buf_block_len
= DIV_ROUND_UP(nb
, blksz
);
1002 sdio_claim_host(card
->func
);
1005 /* Transfer data to card */
1006 ret
= sdio_writesb(card
->func
, card
->ioport
, buf
,
1007 buf_block_len
* blksz
);
1010 BT_ERR("i=%d writesb failed: %d", i
, ret
);
1011 BT_ERR("hex: %*ph", nb
, payload
);
1013 if (i
> MAX_WRITE_IOMEM_RETRY
)
1018 priv
->btmrvl_dev
.tx_dnld_rdy
= false;
1021 sdio_release_host(card
->func
);
1027 static int btmrvl_sdio_download_fw(struct btmrvl_sdio_card
*card
)
1031 int pollnum
= MAX_POLL_TRIES
;
1033 if (!card
|| !card
->func
) {
1034 BT_ERR("card or function is NULL!");
1038 if (!btmrvl_sdio_verify_fw_download(card
, 1)) {
1039 BT_DBG("Firmware already downloaded!");
1043 sdio_claim_host(card
->func
);
1045 /* Check if other function driver is downloading the firmware */
1046 fws0
= sdio_readb(card
->func
, card
->reg
->card_fw_status0
, &ret
);
1048 BT_ERR("Failed to read FW downloading status!");
1053 BT_DBG("BT not the winner (%#x). Skip FW downloading", fws0
);
1055 /* Give other function more time to download the firmware */
1059 ret
= btmrvl_sdio_download_helper(card
);
1061 BT_ERR("Failed to download helper!");
1067 if (btmrvl_sdio_download_fw_w_helper(card
)) {
1068 BT_ERR("Failed to download firmware!");
1074 sdio_release_host(card
->func
);
1077 * winner or not, with this test the FW synchronizes when the
1078 * module can continue its initialization
1080 if (btmrvl_sdio_verify_fw_download(card
, pollnum
)) {
1081 BT_ERR("FW failed to be active in time!");
1088 sdio_release_host(card
->func
);
1092 static int btmrvl_sdio_wakeup_fw(struct btmrvl_private
*priv
)
1094 struct btmrvl_sdio_card
*card
= priv
->btmrvl_dev
.card
;
1097 if (!card
|| !card
->func
) {
1098 BT_ERR("card or function is NULL!");
1102 sdio_claim_host(card
->func
);
1104 sdio_writeb(card
->func
, HOST_POWER_UP
, card
->reg
->cfg
, &ret
);
1106 sdio_release_host(card
->func
);
1108 BT_DBG("wake up firmware");
1113 static void btmrvl_sdio_dump_regs(struct btmrvl_private
*priv
)
1115 struct btmrvl_sdio_card
*card
= priv
->btmrvl_dev
.card
;
1117 unsigned int reg
, reg_start
, reg_end
;
1118 char buf
[256], *ptr
;
1119 u8 loop
, func
, data
;
1122 btmrvl_sdio_wakeup_fw(priv
);
1123 sdio_claim_host(card
->func
);
1125 for (loop
= 0; loop
< MAX_LOOP
; loop
++) {
1126 memset(buf
, 0, sizeof(buf
));
1130 /* Read the registers of SDIO function0 */
1140 ptr
+= sprintf(ptr
, "SDIO Func%d (%#x-%#x): ",
1141 func
, reg_start
, reg_end
);
1142 for (reg
= reg_start
; reg
<= reg_end
; reg
++) {
1144 data
= sdio_f0_readb(card
->func
, reg
, &ret
);
1146 data
= sdio_readb(card
->func
, reg
, &ret
);
1149 ptr
+= sprintf(ptr
, "%02x ", data
);
1151 ptr
+= sprintf(ptr
, "ERR");
1159 sdio_release_host(card
->func
);
1162 /* This function read/write firmware */
1164 rdwr_status
btmrvl_sdio_rdwr_firmware(struct btmrvl_private
*priv
,
1167 struct btmrvl_sdio_card
*card
= priv
->btmrvl_dev
.card
;
1171 sdio_writeb(card
->func
, FW_DUMP_HOST_READY
, card
->reg
->fw_dump_ctrl
,
1175 BT_ERR("SDIO write err");
1176 return RDWR_STATUS_FAILURE
;
1179 for (tries
= 0; tries
< MAX_POLL_TRIES
; tries
++) {
1180 ctrl_data
= sdio_readb(card
->func
, card
->reg
->fw_dump_ctrl
,
1184 BT_ERR("SDIO read err");
1185 return RDWR_STATUS_FAILURE
;
1188 if (ctrl_data
== FW_DUMP_DONE
)
1190 if (doneflag
&& ctrl_data
== doneflag
)
1191 return RDWR_STATUS_DONE
;
1192 if (ctrl_data
!= FW_DUMP_HOST_READY
) {
1193 BT_INFO("The ctrl reg was changed, re-try again!");
1194 sdio_writeb(card
->func
, FW_DUMP_HOST_READY
,
1195 card
->reg
->fw_dump_ctrl
, &ret
);
1197 BT_ERR("SDIO write err");
1198 return RDWR_STATUS_FAILURE
;
1201 usleep_range(100, 200);
1204 if (ctrl_data
== FW_DUMP_HOST_READY
) {
1205 BT_ERR("Fail to pull ctrl_data");
1206 return RDWR_STATUS_FAILURE
;
1209 return RDWR_STATUS_SUCCESS
;
1212 /* This function dump sdio register and memory data */
1213 static void btmrvl_sdio_dump_firmware(struct btmrvl_private
*priv
)
1215 struct btmrvl_sdio_card
*card
= priv
->btmrvl_dev
.card
;
1217 unsigned int reg
, reg_start
, reg_end
;
1218 enum rdwr_status stat
;
1219 u8
*dbg_ptr
, *end_ptr
, *fw_dump_data
, *fw_dump_ptr
;
1220 u8 dump_num
, idx
, i
, read_reg
, doneflag
= 0;
1221 u32 memory_size
, fw_dump_len
= 0;
1223 /* dump sdio register first */
1224 btmrvl_sdio_dump_regs(priv
);
1226 if (!card
->supports_fw_dump
) {
1227 BT_ERR("Firmware dump not supported for this card!");
1231 for (idx
= 0; idx
< ARRAY_SIZE(mem_type_mapping_tbl
); idx
++) {
1232 struct memory_type_mapping
*entry
= &mem_type_mapping_tbl
[idx
];
1234 if (entry
->mem_ptr
) {
1235 vfree(entry
->mem_ptr
);
1236 entry
->mem_ptr
= NULL
;
1238 entry
->mem_size
= 0;
1241 btmrvl_sdio_wakeup_fw(priv
);
1242 sdio_claim_host(card
->func
);
1244 BT_INFO("== btmrvl firmware dump start ==");
1246 stat
= btmrvl_sdio_rdwr_firmware(priv
, doneflag
);
1247 if (stat
== RDWR_STATUS_FAILURE
)
1250 reg
= card
->reg
->fw_dump_start
;
1251 /* Read the number of the memories which will dump */
1252 dump_num
= sdio_readb(card
->func
, reg
, &ret
);
1255 BT_ERR("SDIO read memory length err");
1259 /* Read the length of every memory which will dump */
1260 for (idx
= 0; idx
< dump_num
; idx
++) {
1261 struct memory_type_mapping
*entry
= &mem_type_mapping_tbl
[idx
];
1263 stat
= btmrvl_sdio_rdwr_firmware(priv
, doneflag
);
1264 if (stat
== RDWR_STATUS_FAILURE
)
1268 reg
= card
->reg
->fw_dump_start
;
1269 for (i
= 0; i
< 4; i
++) {
1270 read_reg
= sdio_readb(card
->func
, reg
, &ret
);
1272 BT_ERR("SDIO read err");
1275 memory_size
|= (read_reg
<< i
*8);
1279 if (memory_size
== 0) {
1280 BT_INFO("Firmware dump finished!");
1284 BT_INFO("%s_SIZE=0x%x", entry
->mem_name
, memory_size
);
1285 entry
->mem_ptr
= vzalloc(memory_size
+ 1);
1286 entry
->mem_size
= memory_size
;
1287 if (!entry
->mem_ptr
) {
1288 BT_ERR("Vzalloc %s failed", entry
->mem_name
);
1292 fw_dump_len
+= (strlen("========Start dump ") +
1293 strlen(entry
->mem_name
) +
1294 strlen("========\n") +
1296 strlen("\n========End dump========\n"));
1298 dbg_ptr
= entry
->mem_ptr
;
1299 end_ptr
= dbg_ptr
+ memory_size
;
1301 doneflag
= entry
->done_flag
;
1302 BT_INFO("Start %s output, please wait...",
1306 stat
= btmrvl_sdio_rdwr_firmware(priv
, doneflag
);
1307 if (stat
== RDWR_STATUS_FAILURE
)
1310 reg_start
= card
->reg
->fw_dump_start
;
1311 reg_end
= card
->reg
->fw_dump_end
;
1312 for (reg
= reg_start
; reg
<= reg_end
; reg
++) {
1313 *dbg_ptr
= sdio_readb(card
->func
, reg
, &ret
);
1315 BT_ERR("SDIO read err");
1318 if (dbg_ptr
< end_ptr
)
1321 BT_ERR("Allocated buffer not enough");
1324 if (stat
!= RDWR_STATUS_DONE
) {
1327 BT_INFO("%s done: size=0x%tx",
1329 dbg_ptr
- entry
->mem_ptr
);
1335 BT_INFO("== btmrvl firmware dump end ==");
1338 sdio_release_host(card
->func
);
1340 if (fw_dump_len
== 0)
1343 fw_dump_data
= vzalloc(fw_dump_len
+1);
1344 if (!fw_dump_data
) {
1345 BT_ERR("Vzalloc fw_dump_data fail!");
1348 fw_dump_ptr
= fw_dump_data
;
1350 /* Dump all the memory data into single file, a userspace script will
1351 be used to split all the memory data to multiple files*/
1352 BT_INFO("== btmrvl firmware dump to /sys/class/devcoredump start");
1353 for (idx
= 0; idx
< dump_num
; idx
++) {
1354 struct memory_type_mapping
*entry
= &mem_type_mapping_tbl
[idx
];
1356 if (entry
->mem_ptr
) {
1357 strcpy(fw_dump_ptr
, "========Start dump ");
1358 fw_dump_ptr
+= strlen("========Start dump ");
1360 strcpy(fw_dump_ptr
, entry
->mem_name
);
1361 fw_dump_ptr
+= strlen(entry
->mem_name
);
1363 strcpy(fw_dump_ptr
, "========\n");
1364 fw_dump_ptr
+= strlen("========\n");
1366 memcpy(fw_dump_ptr
, entry
->mem_ptr
, entry
->mem_size
);
1367 fw_dump_ptr
+= entry
->mem_size
;
1369 strcpy(fw_dump_ptr
, "\n========End dump========\n");
1370 fw_dump_ptr
+= strlen("\n========End dump========\n");
1372 vfree(mem_type_mapping_tbl
[idx
].mem_ptr
);
1373 mem_type_mapping_tbl
[idx
].mem_ptr
= NULL
;
1377 /* fw_dump_data will be free in device coredump release function
1379 dev_coredumpv(&priv
->btmrvl_dev
.hcidev
->dev
, fw_dump_data
,
1380 fw_dump_len
, GFP_KERNEL
);
1381 BT_INFO("== btmrvl firmware dump to /sys/class/devcoredump end");
1384 static int btmrvl_sdio_probe(struct sdio_func
*func
,
1385 const struct sdio_device_id
*id
)
1388 struct btmrvl_private
*priv
= NULL
;
1389 struct btmrvl_sdio_card
*card
= NULL
;
1391 BT_INFO("vendor=0x%x, device=0x%x, class=%d, fn=%d",
1392 id
->vendor
, id
->device
, id
->class, func
->num
);
1394 card
= devm_kzalloc(&func
->dev
, sizeof(*card
), GFP_KERNEL
);
1400 if (id
->driver_data
) {
1401 struct btmrvl_sdio_device
*data
= (void *) id
->driver_data
;
1402 card
->helper
= data
->helper
;
1403 card
->firmware
= data
->firmware
;
1404 card
->reg
= data
->reg
;
1405 card
->sd_blksz_fw_dl
= data
->sd_blksz_fw_dl
;
1406 card
->support_pscan_win_report
= data
->support_pscan_win_report
;
1407 card
->supports_fw_dump
= data
->supports_fw_dump
;
1410 if (btmrvl_sdio_register_dev(card
) < 0) {
1411 BT_ERR("Failed to register BT device!");
1415 /* Disable the interrupts on the card */
1416 btmrvl_sdio_disable_host_int(card
);
1418 if (btmrvl_sdio_download_fw(card
)) {
1419 BT_ERR("Downloading firmware failed!");
1424 btmrvl_sdio_enable_host_int(card
);
1426 priv
= btmrvl_add_card(card
);
1428 BT_ERR("Initializing card failed!");
1430 goto disable_host_int
;
1435 /* Initialize the interface specific function pointers */
1436 priv
->hw_host_to_card
= btmrvl_sdio_host_to_card
;
1437 priv
->hw_wakeup_firmware
= btmrvl_sdio_wakeup_fw
;
1438 priv
->hw_process_int_status
= btmrvl_sdio_process_int_status
;
1439 priv
->firmware_dump
= btmrvl_sdio_dump_firmware
;
1441 if (btmrvl_register_hdev(priv
)) {
1442 BT_ERR("Register hdev failed!");
1444 goto disable_host_int
;
1450 btmrvl_sdio_disable_host_int(card
);
1452 btmrvl_sdio_unregister_dev(card
);
1456 static void btmrvl_sdio_remove(struct sdio_func
*func
)
1458 struct btmrvl_sdio_card
*card
;
1461 card
= sdio_get_drvdata(func
);
1463 /* Send SHUTDOWN command & disable interrupt
1464 * if user removes the module.
1467 btmrvl_send_module_cfg_cmd(card
->priv
,
1468 MODULE_SHUTDOWN_REQ
);
1469 btmrvl_sdio_disable_host_int(card
);
1471 BT_DBG("unregester dev");
1472 card
->priv
->surprise_removed
= true;
1473 btmrvl_sdio_unregister_dev(card
);
1474 btmrvl_remove_card(card
->priv
);
1479 static int btmrvl_sdio_suspend(struct device
*dev
)
1481 struct sdio_func
*func
= dev_to_sdio_func(dev
);
1482 struct btmrvl_sdio_card
*card
;
1483 struct btmrvl_private
*priv
;
1484 mmc_pm_flag_t pm_flags
;
1485 struct hci_dev
*hcidev
;
1488 pm_flags
= sdio_get_host_pm_caps(func
);
1489 BT_DBG("%s: suspend: PM flags = 0x%x", sdio_func_id(func
),
1491 if (!(pm_flags
& MMC_PM_KEEP_POWER
)) {
1492 BT_ERR("%s: cannot remain alive while suspended",
1493 sdio_func_id(func
));
1496 card
= sdio_get_drvdata(func
);
1497 if (!card
|| !card
->priv
) {
1498 BT_ERR("card or priv structure is not valid");
1502 BT_ERR("sdio_func is not specified");
1507 hcidev
= priv
->btmrvl_dev
.hcidev
;
1508 BT_DBG("%s: SDIO suspend", hcidev
->name
);
1509 hci_suspend_dev(hcidev
);
1510 skb_queue_purge(&priv
->adapter
->tx_queue
);
1512 if (priv
->adapter
->hs_state
!= HS_ACTIVATED
) {
1513 if (btmrvl_enable_hs(priv
)) {
1514 BT_ERR("HS not actived, suspend failed!");
1519 priv
->adapter
->is_suspended
= true;
1521 /* We will keep the power when hs enabled successfully */
1522 if (priv
->adapter
->hs_state
== HS_ACTIVATED
) {
1523 BT_DBG("suspend with MMC_PM_KEEP_POWER");
1524 return sdio_set_host_pm_flags(func
, MMC_PM_KEEP_POWER
);
1526 BT_DBG("suspend without MMC_PM_KEEP_POWER");
1531 static int btmrvl_sdio_resume(struct device
*dev
)
1533 struct sdio_func
*func
= dev_to_sdio_func(dev
);
1534 struct btmrvl_sdio_card
*card
;
1535 struct btmrvl_private
*priv
;
1536 mmc_pm_flag_t pm_flags
;
1537 struct hci_dev
*hcidev
;
1540 pm_flags
= sdio_get_host_pm_caps(func
);
1541 BT_DBG("%s: resume: PM flags = 0x%x", sdio_func_id(func
),
1543 card
= sdio_get_drvdata(func
);
1544 if (!card
|| !card
->priv
) {
1545 BT_ERR("card or priv structure is not valid");
1549 BT_ERR("sdio_func is not specified");
1554 if (!priv
->adapter
->is_suspended
) {
1555 BT_DBG("device already resumed");
1559 priv
->hw_wakeup_firmware(priv
);
1560 priv
->adapter
->hs_state
= HS_DEACTIVATED
;
1561 hcidev
= priv
->btmrvl_dev
.hcidev
;
1562 BT_DBG("%s: HS DEACTIVATED in resume!", hcidev
->name
);
1563 priv
->adapter
->is_suspended
= false;
1564 BT_DBG("%s: SDIO resume", hcidev
->name
);
1565 hci_resume_dev(hcidev
);
1570 static const struct dev_pm_ops btmrvl_sdio_pm_ops
= {
1571 .suspend
= btmrvl_sdio_suspend
,
1572 .resume
= btmrvl_sdio_resume
,
1575 static struct sdio_driver bt_mrvl_sdio
= {
1576 .name
= "btmrvl_sdio",
1577 .id_table
= btmrvl_sdio_ids
,
1578 .probe
= btmrvl_sdio_probe
,
1579 .remove
= btmrvl_sdio_remove
,
1581 .owner
= THIS_MODULE
,
1582 .pm
= &btmrvl_sdio_pm_ops
,
1586 static int __init
btmrvl_sdio_init_module(void)
1588 if (sdio_register_driver(&bt_mrvl_sdio
) != 0) {
1589 BT_ERR("SDIO Driver Registration Failed");
1593 /* Clear the flag in case user removes the card. */
1599 static void __exit
btmrvl_sdio_exit_module(void)
1601 /* Set the flag as user is removing this module. */
1604 sdio_unregister_driver(&bt_mrvl_sdio
);
1607 module_init(btmrvl_sdio_init_module
);
1608 module_exit(btmrvl_sdio_exit_module
);
1610 MODULE_AUTHOR("Marvell International Ltd.");
1611 MODULE_DESCRIPTION("Marvell BT-over-SDIO driver ver " VERSION
);
1612 MODULE_VERSION(VERSION
);
1613 MODULE_LICENSE("GPL v2");
1614 MODULE_FIRMWARE("mrvl/sd8688_helper.bin");
1615 MODULE_FIRMWARE("mrvl/sd8688.bin");
1616 MODULE_FIRMWARE("mrvl/sd8787_uapsta.bin");
1617 MODULE_FIRMWARE("mrvl/sd8797_uapsta.bin");
1618 MODULE_FIRMWARE("mrvl/sd8887_uapsta.bin");
1619 MODULE_FIRMWARE("mrvl/sd8897_uapsta.bin");