1 // SPDX-License-Identifier: GPL-2.0-only
3 * Marvell BT-over-SDIO driver: SDIO interface related functions.
5 * Copyright (C) 2009, Marvell International Ltd.
8 #include <linux/firmware.h>
9 #include <linux/slab.h>
10 #include <linux/suspend.h>
12 #include <linux/mmc/sdio_ids.h>
13 #include <linux/mmc/sdio_func.h>
14 #include <linux/module.h>
15 #include <linux/devcoredump.h>
17 #include <net/bluetooth/bluetooth.h>
18 #include <net/bluetooth/hci_core.h>
20 #include "btmrvl_drv.h"
21 #include "btmrvl_sdio.h"
25 static struct memory_type_mapping mem_type_mapping_tbl[] = {
26 {"ITCM", NULL, 0, 0xF0},
27 {"DTCM", NULL, 0, 0xF1},
28 {"SQRAM", NULL, 0, 0xF2},
29 {"APU", NULL, 0, 0xF3},
30 {"CIU", NULL, 0, 0xF4},
31 {"ICU", NULL, 0, 0xF5},
32 {"MAC", NULL, 0, 0xF6},
33 {"EXT7", NULL, 0, 0xF7},
34 {"EXT8", NULL, 0, 0xF8},
35 {"EXT9", NULL, 0, 0xF9},
36 {"EXT10", NULL, 0, 0xFA},
37 {"EXT11", NULL, 0, 0xFB},
38 {"EXT12", NULL, 0, 0xFC},
39 {"EXT13", NULL, 0, 0xFD},
40 {"EXTLAST", NULL, 0, 0xFE},
43 static const struct of_device_id btmrvl_sdio_of_match_table[] __maybe_unused = {
44 { .compatible = "marvell,sd8897-bt" },
45 { .compatible = "marvell,sd8997-bt" },
49 static irqreturn_t btmrvl_wake_irq_bt(int irq, void *priv)
51 struct btmrvl_sdio_card *card = priv;
52 struct device *dev = &card->func->dev;
53 struct btmrvl_plt_wake_cfg *cfg = card->plt_wake_cfg;
55 dev_info(dev, "wake by bt\n");
56 cfg->wake_by_bt = true;
57 disable_irq_nosync(irq);
59 pm_wakeup_event(dev, 0);
65 /* This function parses device tree node using mmc subnode devicetree API.
66 * The device node is saved in card->plt_of_node.
67 * If the device tree node exists and includes interrupts attributes, this
68 * function will request platform specific wakeup interrupt.
70 static int btmrvl_sdio_probe_of(struct device *dev,
71 struct btmrvl_sdio_card *card)
73 struct btmrvl_plt_wake_cfg *cfg;
77 !of_match_node(btmrvl_sdio_of_match_table, dev->of_node)) {
78 dev_info(dev, "sdio device tree data not available\n");
82 card->plt_of_node = dev->of_node;
84 card->plt_wake_cfg = devm_kzalloc(dev, sizeof(*card->plt_wake_cfg),
86 cfg = card->plt_wake_cfg;
87 if (cfg && card->plt_of_node) {
88 cfg->irq_bt = irq_of_parse_and_map(card->plt_of_node, 0);
90 dev_err(dev, "fail to parse irq_bt from device tree\n");
93 ret = devm_request_irq(dev, cfg->irq_bt,
98 "Failed to request irq_bt %d (%d)\n",
102 /* Configure wakeup (enabled by default) */
103 device_init_wakeup(dev, true);
104 disable_irq(cfg->irq_bt);
111 /* The btmrvl_sdio_remove() callback function is called
112 * when user removes this module from kernel space or ejects
113 * the card from the slot. The driver handles these 2 cases
115 * If the user is removing the module, a MODULE_SHUTDOWN_REQ
116 * command is sent to firmware and interrupt will be disabled.
117 * If the card is removed, there is no need to send command
118 * or disable interrupt.
120 * The variable 'user_rmmod' is used to distinguish these two
121 * scenarios. This flag is initialized as FALSE in case the card
122 * is removed, and will be set to TRUE for module removal when
123 * module_exit function is called.
125 static u8 user_rmmod;
128 static const struct btmrvl_sdio_card_reg btmrvl_reg_8688 = {
130 .host_int_mask = 0x04,
131 .host_intstatus = 0x05,
133 .sq_read_base_addr_a0 = 0x10,
134 .sq_read_base_addr_a1 = 0x11,
135 .card_fw_status0 = 0x40,
136 .card_fw_status1 = 0x41,
138 .card_rx_unit = 0x43,
142 .int_read_to_clear = false,
144 static const struct btmrvl_sdio_card_reg btmrvl_reg_87xx = {
146 .host_int_mask = 0x02,
147 .host_intstatus = 0x03,
149 .sq_read_base_addr_a0 = 0x40,
150 .sq_read_base_addr_a1 = 0x41,
151 .card_revision = 0x5c,
152 .card_fw_status0 = 0x60,
153 .card_fw_status1 = 0x61,
155 .card_rx_unit = 0x63,
159 .int_read_to_clear = false,
162 static const struct btmrvl_sdio_card_reg btmrvl_reg_8887 = {
164 .host_int_mask = 0x08,
165 .host_intstatus = 0x0C,
167 .sq_read_base_addr_a0 = 0x6C,
168 .sq_read_base_addr_a1 = 0x6D,
169 .card_revision = 0xC8,
170 .card_fw_status0 = 0x88,
171 .card_fw_status1 = 0x89,
173 .card_rx_unit = 0x8B,
177 .int_read_to_clear = true,
178 .host_int_rsr = 0x04,
179 .card_misc_cfg = 0xD8,
182 static const struct btmrvl_sdio_card_reg btmrvl_reg_8897 = {
184 .host_int_mask = 0x02,
185 .host_intstatus = 0x03,
187 .sq_read_base_addr_a0 = 0x60,
188 .sq_read_base_addr_a1 = 0x61,
189 .card_revision = 0xbc,
190 .card_fw_status0 = 0xc0,
191 .card_fw_status1 = 0xc1,
193 .card_rx_unit = 0xc3,
197 .int_read_to_clear = true,
198 .host_int_rsr = 0x01,
199 .card_misc_cfg = 0xcc,
200 .fw_dump_ctrl = 0xe2,
201 .fw_dump_start = 0xe3,
205 static const struct btmrvl_sdio_card_reg btmrvl_reg_89xx = {
207 .host_int_mask = 0x08,
208 .host_intstatus = 0x0c,
210 .sq_read_base_addr_a0 = 0xf8,
211 .sq_read_base_addr_a1 = 0xf9,
212 .card_revision = 0xc8,
213 .card_fw_status0 = 0xe8,
214 .card_fw_status1 = 0xe9,
216 .card_rx_unit = 0xeb,
220 .int_read_to_clear = true,
221 .host_int_rsr = 0x04,
222 .card_misc_cfg = 0xd8,
223 .fw_dump_ctrl = 0xf0,
224 .fw_dump_start = 0xf1,
228 static const struct btmrvl_sdio_device btmrvl_sdio_sd8688 = {
229 .helper = "mrvl/sd8688_helper.bin",
230 .firmware = "mrvl/sd8688.bin",
231 .reg = &btmrvl_reg_8688,
232 .support_pscan_win_report = false,
233 .sd_blksz_fw_dl = 64,
234 .supports_fw_dump = false,
237 static const struct btmrvl_sdio_device btmrvl_sdio_sd8787 = {
239 .firmware = "mrvl/sd8787_uapsta.bin",
240 .reg = &btmrvl_reg_87xx,
241 .support_pscan_win_report = false,
242 .sd_blksz_fw_dl = 256,
243 .supports_fw_dump = false,
246 static const struct btmrvl_sdio_device btmrvl_sdio_sd8797 = {
248 .firmware = "mrvl/sd8797_uapsta.bin",
249 .reg = &btmrvl_reg_87xx,
250 .support_pscan_win_report = false,
251 .sd_blksz_fw_dl = 256,
252 .supports_fw_dump = false,
255 static const struct btmrvl_sdio_device btmrvl_sdio_sd8887 = {
257 .firmware = "mrvl/sd8887_uapsta.bin",
258 .reg = &btmrvl_reg_8887,
259 .support_pscan_win_report = true,
260 .sd_blksz_fw_dl = 256,
261 .supports_fw_dump = false,
264 static const struct btmrvl_sdio_device btmrvl_sdio_sd8897 = {
266 .firmware = "mrvl/sd8897_uapsta.bin",
267 .reg = &btmrvl_reg_8897,
268 .support_pscan_win_report = true,
269 .sd_blksz_fw_dl = 256,
270 .supports_fw_dump = true,
273 static const struct btmrvl_sdio_device btmrvl_sdio_sd8977 = {
275 .firmware = "mrvl/sdsd8977_combo_v2.bin",
276 .reg = &btmrvl_reg_89xx,
277 .support_pscan_win_report = true,
278 .sd_blksz_fw_dl = 256,
279 .supports_fw_dump = true,
282 static const struct btmrvl_sdio_device btmrvl_sdio_sd8987 = {
284 .firmware = "mrvl/sd8987_uapsta.bin",
285 .reg = &btmrvl_reg_89xx,
286 .support_pscan_win_report = true,
287 .sd_blksz_fw_dl = 256,
288 .supports_fw_dump = true,
291 static const struct btmrvl_sdio_device btmrvl_sdio_sd8997 = {
293 .firmware = "mrvl/sdsd8997_combo_v4.bin",
294 .reg = &btmrvl_reg_89xx,
295 .support_pscan_win_report = true,
296 .sd_blksz_fw_dl = 256,
297 .supports_fw_dump = true,
300 static const struct sdio_device_id btmrvl_sdio_ids[] = {
301 /* Marvell SD8688 Bluetooth device */
302 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8688_BT),
303 .driver_data = (unsigned long)&btmrvl_sdio_sd8688 },
304 /* Marvell SD8787 Bluetooth device */
305 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8787_BT),
306 .driver_data = (unsigned long)&btmrvl_sdio_sd8787 },
307 /* Marvell SD8787 Bluetooth AMP device */
308 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8787_BT_AMP),
309 .driver_data = (unsigned long)&btmrvl_sdio_sd8787 },
310 /* Marvell SD8797 Bluetooth device */
311 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8797_BT),
312 .driver_data = (unsigned long)&btmrvl_sdio_sd8797 },
313 /* Marvell SD8887 Bluetooth device */
314 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8887_BT),
315 .driver_data = (unsigned long)&btmrvl_sdio_sd8887 },
316 /* Marvell SD8897 Bluetooth device */
317 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8897_BT),
318 .driver_data = (unsigned long)&btmrvl_sdio_sd8897 },
319 /* Marvell SD8977 Bluetooth device */
320 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8977_BT),
321 .driver_data = (unsigned long)&btmrvl_sdio_sd8977 },
322 /* Marvell SD8987 Bluetooth device */
323 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8987_BT),
324 .driver_data = (unsigned long)&btmrvl_sdio_sd8987 },
325 /* Marvell SD8997 Bluetooth device */
326 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8997_BT),
327 .driver_data = (unsigned long)&btmrvl_sdio_sd8997 },
329 { } /* Terminating entry */
332 MODULE_DEVICE_TABLE(sdio, btmrvl_sdio_ids);
334 static int btmrvl_sdio_get_rx_unit(struct btmrvl_sdio_card *card)
339 reg = sdio_readb(card->func, card->reg->card_rx_unit, &ret);
346 static int btmrvl_sdio_read_fw_status(struct btmrvl_sdio_card *card, u16 *dat)
353 fws0 = sdio_readb(card->func, card->reg->card_fw_status0, &ret);
357 fws1 = sdio_readb(card->func, card->reg->card_fw_status1, &ret);
361 *dat = (((u16) fws1) << 8) | fws0;
366 static int btmrvl_sdio_read_rx_len(struct btmrvl_sdio_card *card, u16 *dat)
371 reg = sdio_readb(card->func, card->reg->card_rx_len, &ret);
373 *dat = (u16) reg << card->rx_unit;
378 static int btmrvl_sdio_enable_host_int_mask(struct btmrvl_sdio_card *card,
383 sdio_writeb(card->func, mask, card->reg->host_int_mask, &ret);
385 BT_ERR("Unable to enable the host interrupt!");
392 static int btmrvl_sdio_disable_host_int_mask(struct btmrvl_sdio_card *card,
398 host_int_mask = sdio_readb(card->func, card->reg->host_int_mask, &ret);
402 host_int_mask &= ~mask;
404 sdio_writeb(card->func, host_int_mask, card->reg->host_int_mask, &ret);
406 BT_ERR("Unable to disable the host interrupt!");
413 static int btmrvl_sdio_poll_card_status(struct btmrvl_sdio_card *card, u8 bits)
419 for (tries = 0; tries < MAX_POLL_TRIES * 1000; tries++) {
420 status = sdio_readb(card->func, card->reg->card_status, &ret);
423 if ((status & bits) == bits)
432 BT_ERR("FAILED! ret=%d", ret);
437 static int btmrvl_sdio_verify_fw_download(struct btmrvl_sdio_card *card,
443 /* Wait for firmware to become ready */
444 for (tries = 0; tries < pollnum; tries++) {
445 sdio_claim_host(card->func);
446 ret = btmrvl_sdio_read_fw_status(card, &firmwarestat);
447 sdio_release_host(card->func);
451 if (firmwarestat == FIRMWARE_READY)
460 static int btmrvl_sdio_download_helper(struct btmrvl_sdio_card *card)
462 const struct firmware *fw_helper = NULL;
463 const u8 *helper = NULL;
465 void *tmphlprbuf = NULL;
466 int tmphlprbufsz, hlprblknow, helperlen;
470 ret = request_firmware(&fw_helper, card->helper,
472 if ((ret < 0) || !fw_helper) {
473 BT_ERR("request_firmware(helper) failed, error code = %d",
479 helper = fw_helper->data;
480 helperlen = fw_helper->size;
482 BT_DBG("Downloading helper image (%d bytes), block size %d bytes",
483 helperlen, SDIO_BLOCK_SIZE);
485 tmphlprbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
487 tmphlprbuf = kzalloc(tmphlprbufsz, GFP_KERNEL);
489 BT_ERR("Unable to allocate buffer for helper."
490 " Terminating download");
495 helperbuf = (u8 *) ALIGN_ADDR(tmphlprbuf, BTSDIO_DMA_ALIGN);
497 /* Perform helper data transfer */
498 tx_len = (FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE)
503 ret = btmrvl_sdio_poll_card_status(card,
504 CARD_IO_READY | DN_LD_CARD_RDY);
506 BT_ERR("Helper download poll status timeout @ %d",
511 /* Check if there is more data? */
512 if (hlprblknow >= helperlen)
515 if (helperlen - hlprblknow < tx_len)
516 tx_len = helperlen - hlprblknow;
519 helperbuf[0] = ((tx_len & 0x000000ff) >> 0);
520 helperbuf[1] = ((tx_len & 0x0000ff00) >> 8);
521 helperbuf[2] = ((tx_len & 0x00ff0000) >> 16);
522 helperbuf[3] = ((tx_len & 0xff000000) >> 24);
524 memcpy(&helperbuf[SDIO_HEADER_LEN], &helper[hlprblknow],
527 /* Now send the data */
528 ret = sdio_writesb(card->func, card->ioport, helperbuf,
529 FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE);
531 BT_ERR("IO error during helper download @ %d",
536 hlprblknow += tx_len;
539 BT_DBG("Transferring helper image EOF block");
541 memset(helperbuf, 0x0, SDIO_BLOCK_SIZE);
543 ret = sdio_writesb(card->func, card->ioport, helperbuf,
546 BT_ERR("IO error in writing helper image EOF block");
554 release_firmware(fw_helper);
558 static int btmrvl_sdio_download_fw_w_helper(struct btmrvl_sdio_card *card)
560 const struct firmware *fw_firmware = NULL;
561 const u8 *firmware = NULL;
562 int firmwarelen, tmpfwbufsz, ret;
563 unsigned int tries, offset;
565 void *tmpfwbuf = NULL;
567 u16 len, blksz_dl = card->sd_blksz_fw_dl;
568 int txlen = 0, tx_blocks = 0, count = 0;
570 ret = request_firmware(&fw_firmware, card->firmware,
572 if ((ret < 0) || !fw_firmware) {
573 BT_ERR("request_firmware(firmware) failed, error code = %d",
579 firmware = fw_firmware->data;
580 firmwarelen = fw_firmware->size;
582 BT_DBG("Downloading FW image (%d bytes)", firmwarelen);
584 tmpfwbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
585 tmpfwbuf = kzalloc(tmpfwbufsz, GFP_KERNEL);
587 BT_ERR("Unable to allocate buffer for firmware."
588 " Terminating download");
593 /* Ensure aligned firmware buffer */
594 fwbuf = (u8 *) ALIGN_ADDR(tmpfwbuf, BTSDIO_DMA_ALIGN);
596 /* Perform firmware data transfer */
599 ret = btmrvl_sdio_poll_card_status(card,
600 CARD_IO_READY | DN_LD_CARD_RDY);
602 BT_ERR("FW download with helper poll status"
603 " timeout @ %d", offset);
607 /* Check if there is more data ? */
608 if (offset >= firmwarelen)
611 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
612 base0 = sdio_readb(card->func,
613 card->reg->sq_read_base_addr_a0, &ret);
615 BT_ERR("BASE0 register read failed:"
616 " base0 = 0x%04X(%d)."
617 " Terminating download",
622 base1 = sdio_readb(card->func,
623 card->reg->sq_read_base_addr_a1, &ret);
625 BT_ERR("BASE1 register read failed:"
626 " base1 = 0x%04X(%d)."
627 " Terminating download",
633 len = (((u16) base1) << 8) | base0;
642 else if (len > BTM_UPLD_SIZE) {
643 BT_ERR("FW download failure @%d, invalid length %d",
653 if (count > MAX_WRITE_IOMEM_RETRY) {
654 BT_ERR("FW download failure @%d, "
655 "over max retry count", offset);
659 BT_ERR("FW CRC error indicated by the helper: "
660 "len = 0x%04X, txlen = %d", len, txlen);
662 /* Set txlen to 0 so as to resend from same offset */
668 if (firmwarelen - offset < txlen)
669 txlen = firmwarelen - offset;
671 tx_blocks = DIV_ROUND_UP(txlen, blksz_dl);
673 memcpy(fwbuf, &firmware[offset], txlen);
676 ret = sdio_writesb(card->func, card->ioport, fwbuf,
677 tx_blocks * blksz_dl);
680 BT_ERR("FW download, writesb(%d) failed @%d",
682 sdio_writeb(card->func, HOST_CMD53_FIN,
683 card->reg->cfg, &ret);
685 BT_ERR("writeb failed (CFG)");
691 BT_INFO("FW download over, size %d bytes", offset);
697 release_firmware(fw_firmware);
701 static int btmrvl_sdio_card_to_host(struct btmrvl_private *priv)
704 int ret, num_blocks, blksz;
705 struct sk_buff *skb = NULL;
708 struct hci_dev *hdev = priv->btmrvl_dev.hcidev;
709 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
711 if (!card || !card->func) {
712 BT_ERR("card or function is NULL!");
717 /* Read the length of data to be transferred */
718 ret = btmrvl_sdio_read_rx_len(card, &buf_len);
720 BT_ERR("read rx_len failed");
725 blksz = SDIO_BLOCK_SIZE;
726 num_blocks = DIV_ROUND_UP(buf_len, blksz);
728 if (buf_len <= SDIO_HEADER_LEN
729 || (num_blocks * blksz) > ALLOC_BUF_SIZE) {
730 BT_ERR("invalid packet length: %d", buf_len);
735 /* Allocate buffer */
736 skb = bt_skb_alloc(num_blocks * blksz + BTSDIO_DMA_ALIGN, GFP_KERNEL);
738 BT_ERR("No free skb");
743 if ((unsigned long) skb->data & (BTSDIO_DMA_ALIGN - 1)) {
744 skb_put(skb, (unsigned long) skb->data &
745 (BTSDIO_DMA_ALIGN - 1));
746 skb_pull(skb, (unsigned long) skb->data &
747 (BTSDIO_DMA_ALIGN - 1));
752 ret = sdio_readsb(card->func, payload, card->ioport,
755 BT_ERR("readsb failed: %d", ret);
760 /* This is SDIO specific header length: byte[2][1][0], type: byte[3]
761 * (HCI_COMMAND = 1, ACL_DATA = 2, SCO_DATA = 3, 0xFE = Vendor)
764 buf_len = payload[0];
765 buf_len |= payload[1] << 8;
766 buf_len |= payload[2] << 16;
768 if (buf_len > blksz * num_blocks) {
769 BT_ERR("Skip incorrect packet: hdrlen %d buffer %d",
770 buf_len, blksz * num_blocks);
778 case HCI_ACLDATA_PKT:
779 case HCI_SCODATA_PKT:
781 hci_skb_pkt_type(skb) = type;
782 skb_put(skb, buf_len);
783 skb_pull(skb, SDIO_HEADER_LEN);
785 if (type == HCI_EVENT_PKT) {
786 if (btmrvl_check_evtpkt(priv, skb))
787 hci_recv_frame(hdev, skb);
789 hci_recv_frame(hdev, skb);
792 hdev->stat.byte_rx += buf_len;
795 case MRVL_VENDOR_PKT:
796 hci_skb_pkt_type(skb) = HCI_VENDOR_PKT;
797 skb_put(skb, buf_len);
798 skb_pull(skb, SDIO_HEADER_LEN);
800 if (btmrvl_process_event(priv, skb))
801 hci_recv_frame(hdev, skb);
803 hdev->stat.byte_rx += buf_len;
807 BT_ERR("Unknown packet type:%d", type);
808 BT_ERR("hex: %*ph", blksz * num_blocks, payload);
824 static int btmrvl_sdio_process_int_status(struct btmrvl_private *priv)
828 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
830 spin_lock_irqsave(&priv->driver_lock, flags);
833 spin_unlock_irqrestore(&priv->driver_lock, flags);
835 sdio_claim_host(card->func);
836 if (ireg & DN_LD_HOST_INT_STATUS) {
837 if (priv->btmrvl_dev.tx_dnld_rdy)
838 BT_DBG("tx_done already received: "
839 " int_status=0x%x", ireg);
841 priv->btmrvl_dev.tx_dnld_rdy = true;
844 if (ireg & UP_LD_HOST_INT_STATUS)
845 btmrvl_sdio_card_to_host(priv);
847 sdio_release_host(card->func);
852 static int btmrvl_sdio_read_to_clear(struct btmrvl_sdio_card *card, u8 *ireg)
854 struct btmrvl_adapter *adapter = card->priv->adapter;
857 ret = sdio_readsb(card->func, adapter->hw_regs, 0, SDIO_BLOCK_SIZE);
859 BT_ERR("sdio_readsb: read int hw_regs failed: %d", ret);
863 *ireg = adapter->hw_regs[card->reg->host_intstatus];
864 BT_DBG("hw_regs[%#x]=%#x", card->reg->host_intstatus, *ireg);
869 static int btmrvl_sdio_write_to_clear(struct btmrvl_sdio_card *card, u8 *ireg)
873 *ireg = sdio_readb(card->func, card->reg->host_intstatus, &ret);
875 BT_ERR("sdio_readb: read int status failed: %d", ret);
881 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
882 * Clear the interrupt status register and re-enable the
885 BT_DBG("int_status = 0x%x", *ireg);
887 sdio_writeb(card->func, ~(*ireg) & (DN_LD_HOST_INT_STATUS |
888 UP_LD_HOST_INT_STATUS),
889 card->reg->host_intstatus, &ret);
891 BT_ERR("sdio_writeb: clear int status failed: %d", ret);
899 static void btmrvl_sdio_interrupt(struct sdio_func *func)
901 struct btmrvl_private *priv;
902 struct btmrvl_sdio_card *card;
907 card = sdio_get_drvdata(func);
908 if (!card || !card->priv) {
909 BT_ERR("sbi_interrupt(%p) card or priv is NULL, card=%p",
916 if (priv->surprise_removed)
919 if (card->reg->int_read_to_clear)
920 ret = btmrvl_sdio_read_to_clear(card, &ireg);
922 ret = btmrvl_sdio_write_to_clear(card, &ireg);
927 spin_lock_irqsave(&priv->driver_lock, flags);
929 spin_unlock_irqrestore(&priv->driver_lock, flags);
931 btmrvl_interrupt(priv);
934 static int btmrvl_sdio_register_dev(struct btmrvl_sdio_card *card)
936 struct sdio_func *func;
940 if (!card || !card->func) {
941 BT_ERR("Error: card or function is NULL!");
948 sdio_claim_host(func);
950 ret = sdio_enable_func(func);
952 BT_ERR("sdio_enable_func() failed: ret=%d", ret);
957 ret = sdio_claim_irq(func, btmrvl_sdio_interrupt);
959 BT_ERR("sdio_claim_irq failed: ret=%d", ret);
964 ret = sdio_set_block_size(card->func, SDIO_BLOCK_SIZE);
966 BT_ERR("cannot set SDIO block size");
971 reg = sdio_readb(func, card->reg->io_port_0, &ret);
979 reg = sdio_readb(func, card->reg->io_port_1, &ret);
985 card->ioport |= (reg << 8);
987 reg = sdio_readb(func, card->reg->io_port_2, &ret);
993 card->ioport |= (reg << 16);
995 BT_DBG("SDIO FUNC%d IO port: 0x%x", func->num, card->ioport);
997 if (card->reg->int_read_to_clear) {
998 reg = sdio_readb(func, card->reg->host_int_rsr, &ret);
1003 sdio_writeb(func, reg | 0x3f, card->reg->host_int_rsr, &ret);
1009 reg = sdio_readb(func, card->reg->card_misc_cfg, &ret);
1014 sdio_writeb(func, reg | 0x10, card->reg->card_misc_cfg, &ret);
1021 sdio_set_drvdata(func, card);
1023 sdio_release_host(func);
1028 sdio_release_irq(func);
1031 sdio_disable_func(func);
1034 sdio_release_host(func);
1040 static int btmrvl_sdio_unregister_dev(struct btmrvl_sdio_card *card)
1042 if (card && card->func) {
1043 sdio_claim_host(card->func);
1044 sdio_release_irq(card->func);
1045 sdio_disable_func(card->func);
1046 sdio_release_host(card->func);
1047 sdio_set_drvdata(card->func, NULL);
1053 static int btmrvl_sdio_enable_host_int(struct btmrvl_sdio_card *card)
1057 if (!card || !card->func)
1060 sdio_claim_host(card->func);
1062 ret = btmrvl_sdio_enable_host_int_mask(card, HIM_ENABLE);
1064 btmrvl_sdio_get_rx_unit(card);
1066 sdio_release_host(card->func);
1071 static int btmrvl_sdio_disable_host_int(struct btmrvl_sdio_card *card)
1075 if (!card || !card->func)
1078 sdio_claim_host(card->func);
1080 ret = btmrvl_sdio_disable_host_int_mask(card, HIM_DISABLE);
1082 sdio_release_host(card->func);
1087 static int btmrvl_sdio_host_to_card(struct btmrvl_private *priv,
1088 u8 *payload, u16 nb)
1090 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
1095 void *tmpbuf = NULL;
1098 if (!card || !card->func) {
1099 BT_ERR("card or function is NULL!");
1103 blksz = DIV_ROUND_UP(nb, SDIO_BLOCK_SIZE) * SDIO_BLOCK_SIZE;
1106 if ((unsigned long) payload & (BTSDIO_DMA_ALIGN - 1) ||
1108 tmpbufsz = ALIGN_SZ(blksz, BTSDIO_DMA_ALIGN) +
1110 tmpbuf = kzalloc(tmpbufsz, GFP_KERNEL);
1113 buf = (u8 *) ALIGN_ADDR(tmpbuf, BTSDIO_DMA_ALIGN);
1114 memcpy(buf, payload, nb);
1117 sdio_claim_host(card->func);
1120 /* Transfer data to card */
1121 ret = sdio_writesb(card->func, card->ioport, buf,
1125 BT_ERR("i=%d writesb failed: %d", i, ret);
1126 BT_ERR("hex: %*ph", nb, payload);
1128 if (i > MAX_WRITE_IOMEM_RETRY)
1133 priv->btmrvl_dev.tx_dnld_rdy = false;
1136 sdio_release_host(card->func);
1142 static int btmrvl_sdio_download_fw(struct btmrvl_sdio_card *card)
1146 int pollnum = MAX_POLL_TRIES;
1148 if (!card || !card->func) {
1149 BT_ERR("card or function is NULL!");
1153 if (!btmrvl_sdio_verify_fw_download(card, 1)) {
1154 BT_DBG("Firmware already downloaded!");
1158 sdio_claim_host(card->func);
1160 /* Check if other function driver is downloading the firmware */
1161 fws0 = sdio_readb(card->func, card->reg->card_fw_status0, &ret);
1163 BT_ERR("Failed to read FW downloading status!");
1168 BT_DBG("BT not the winner (%#x). Skip FW downloading", fws0);
1170 /* Give other function more time to download the firmware */
1174 ret = btmrvl_sdio_download_helper(card);
1176 BT_ERR("Failed to download helper!");
1182 if (btmrvl_sdio_download_fw_w_helper(card)) {
1183 BT_ERR("Failed to download firmware!");
1190 * winner or not, with this test the FW synchronizes when the
1191 * module can continue its initialization
1193 if (btmrvl_sdio_verify_fw_download(card, pollnum)) {
1194 BT_ERR("FW failed to be active in time!");
1199 sdio_release_host(card->func);
1204 sdio_release_host(card->func);
1208 static int btmrvl_sdio_wakeup_fw(struct btmrvl_private *priv)
1210 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
1213 if (!card || !card->func) {
1214 BT_ERR("card or function is NULL!");
1218 sdio_claim_host(card->func);
1220 sdio_writeb(card->func, HOST_POWER_UP, card->reg->cfg, &ret);
1222 sdio_release_host(card->func);
1224 BT_DBG("wake up firmware");
1229 static void btmrvl_sdio_dump_regs(struct btmrvl_private *priv)
1231 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
1233 unsigned int reg, reg_start, reg_end;
1234 char buf[256], *ptr;
1235 u8 loop, func, data;
1238 btmrvl_sdio_wakeup_fw(priv);
1239 sdio_claim_host(card->func);
1241 for (loop = 0; loop < MAX_LOOP; loop++) {
1242 memset(buf, 0, sizeof(buf));
1246 /* Read the registers of SDIO function0 */
1256 ptr += sprintf(ptr, "SDIO Func%d (%#x-%#x): ",
1257 func, reg_start, reg_end);
1258 for (reg = reg_start; reg <= reg_end; reg++) {
1260 data = sdio_f0_readb(card->func, reg, &ret);
1262 data = sdio_readb(card->func, reg, &ret);
1265 ptr += sprintf(ptr, "%02x ", data);
1267 ptr += sprintf(ptr, "ERR");
1275 sdio_release_host(card->func);
1278 /* This function read/write firmware */
1280 rdwr_status btmrvl_sdio_rdwr_firmware(struct btmrvl_private *priv,
1283 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
1287 sdio_writeb(card->func, FW_DUMP_HOST_READY, card->reg->fw_dump_ctrl,
1291 BT_ERR("SDIO write err");
1292 return RDWR_STATUS_FAILURE;
1295 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
1296 ctrl_data = sdio_readb(card->func, card->reg->fw_dump_ctrl,
1300 BT_ERR("SDIO read err");
1301 return RDWR_STATUS_FAILURE;
1304 if (ctrl_data == FW_DUMP_DONE)
1306 if (doneflag && ctrl_data == doneflag)
1307 return RDWR_STATUS_DONE;
1308 if (ctrl_data != FW_DUMP_HOST_READY) {
1309 BT_INFO("The ctrl reg was changed, re-try again!");
1310 sdio_writeb(card->func, FW_DUMP_HOST_READY,
1311 card->reg->fw_dump_ctrl, &ret);
1313 BT_ERR("SDIO write err");
1314 return RDWR_STATUS_FAILURE;
1317 usleep_range(100, 200);
1320 if (ctrl_data == FW_DUMP_HOST_READY) {
1321 BT_ERR("Fail to pull ctrl_data");
1322 return RDWR_STATUS_FAILURE;
1325 return RDWR_STATUS_SUCCESS;
1328 /* This function dump sdio register and memory data */
1329 static void btmrvl_sdio_coredump(struct device *dev)
1331 struct sdio_func *func = dev_to_sdio_func(dev);
1332 struct btmrvl_sdio_card *card;
1333 struct btmrvl_private *priv;
1335 unsigned int reg, reg_start, reg_end;
1336 enum rdwr_status stat;
1337 u8 *dbg_ptr, *end_ptr, *fw_dump_data, *fw_dump_ptr;
1338 u8 dump_num = 0, idx, i, read_reg, doneflag = 0;
1339 u32 memory_size, fw_dump_len = 0;
1342 card = sdio_get_drvdata(func);
1345 /* dump sdio register first */
1346 btmrvl_sdio_dump_regs(priv);
1348 if (!card->supports_fw_dump) {
1349 BT_ERR("Firmware dump not supported for this card!");
1353 for (idx = 0; idx < ARRAY_SIZE(mem_type_mapping_tbl); idx++) {
1354 struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
1356 if (entry->mem_ptr) {
1357 vfree(entry->mem_ptr);
1358 entry->mem_ptr = NULL;
1360 entry->mem_size = 0;
1363 btmrvl_sdio_wakeup_fw(priv);
1364 sdio_claim_host(card->func);
1366 BT_INFO("== btmrvl firmware dump start ==");
1368 stat = btmrvl_sdio_rdwr_firmware(priv, doneflag);
1369 if (stat == RDWR_STATUS_FAILURE)
1372 reg = card->reg->fw_dump_start;
1373 /* Read the number of the memories which will dump */
1374 dump_num = sdio_readb(card->func, reg, &ret);
1377 BT_ERR("SDIO read memory length err");
1381 /* Read the length of every memory which will dump */
1382 for (idx = 0; idx < dump_num; idx++) {
1383 struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
1385 stat = btmrvl_sdio_rdwr_firmware(priv, doneflag);
1386 if (stat == RDWR_STATUS_FAILURE)
1390 reg = card->reg->fw_dump_start;
1391 for (i = 0; i < 4; i++) {
1392 read_reg = sdio_readb(card->func, reg, &ret);
1394 BT_ERR("SDIO read err");
1397 memory_size |= (read_reg << i*8);
1401 if (memory_size == 0) {
1402 BT_INFO("Firmware dump finished!");
1403 sdio_writeb(card->func, FW_DUMP_READ_DONE,
1404 card->reg->fw_dump_ctrl, &ret);
1406 BT_ERR("SDIO Write MEMDUMP_FINISH ERR");
1412 BT_INFO("%s_SIZE=0x%x", entry->mem_name, memory_size);
1413 entry->mem_ptr = vzalloc(memory_size + 1);
1414 entry->mem_size = memory_size;
1415 if (!entry->mem_ptr) {
1416 BT_ERR("Vzalloc %s failed", entry->mem_name);
1420 fw_dump_len += (strlen("========Start dump ") +
1421 strlen(entry->mem_name) +
1422 strlen("========\n") +
1424 strlen("\n========End dump========\n"));
1426 dbg_ptr = entry->mem_ptr;
1427 end_ptr = dbg_ptr + memory_size;
1429 doneflag = entry->done_flag;
1430 BT_INFO("Start %s output, please wait...",
1434 stat = btmrvl_sdio_rdwr_firmware(priv, doneflag);
1435 if (stat == RDWR_STATUS_FAILURE)
1438 reg_start = card->reg->fw_dump_start;
1439 reg_end = card->reg->fw_dump_end;
1440 for (reg = reg_start; reg <= reg_end; reg++) {
1441 *dbg_ptr = sdio_readb(card->func, reg, &ret);
1443 BT_ERR("SDIO read err");
1446 if (dbg_ptr < end_ptr)
1449 BT_ERR("Allocated buffer not enough");
1452 if (stat == RDWR_STATUS_DONE) {
1453 BT_INFO("%s done: size=0x%tx",
1455 dbg_ptr - entry->mem_ptr);
1461 BT_INFO("== btmrvl firmware dump end ==");
1464 sdio_release_host(card->func);
1466 if (fw_dump_len == 0)
1469 fw_dump_data = vzalloc(fw_dump_len + 1);
1470 if (!fw_dump_data) {
1471 BT_ERR("Vzalloc fw_dump_data fail!");
1474 fw_dump_ptr = fw_dump_data;
1476 /* Dump all the memory data into single file, a userspace script will
1477 * be used to split all the memory data to multiple files
1479 BT_INFO("== btmrvl firmware dump to /sys/class/devcoredump start");
1480 for (idx = 0; idx < dump_num; idx++) {
1481 struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
1483 if (entry->mem_ptr) {
1484 size += scnprintf(fw_dump_ptr + size,
1485 fw_dump_len + 1 - size,
1486 "========Start dump %s========\n",
1489 memcpy(fw_dump_ptr + size, entry->mem_ptr,
1491 size += entry->mem_size;
1493 size += scnprintf(fw_dump_ptr + size,
1494 fw_dump_len + 1 - size,
1495 "\n========End dump========\n");
1497 vfree(mem_type_mapping_tbl[idx].mem_ptr);
1498 mem_type_mapping_tbl[idx].mem_ptr = NULL;
1502 /* fw_dump_data will be free in device coredump release function
1505 dev_coredumpv(&card->func->dev, fw_dump_data, fw_dump_len, GFP_KERNEL);
1506 BT_INFO("== btmrvl firmware dump to /sys/class/devcoredump end");
1509 static int btmrvl_sdio_probe(struct sdio_func *func,
1510 const struct sdio_device_id *id)
1513 struct btmrvl_private *priv = NULL;
1514 struct btmrvl_sdio_card *card = NULL;
1516 BT_INFO("vendor=0x%x, device=0x%x, class=%d, fn=%d",
1517 id->vendor, id->device, id->class, func->num);
1519 card = devm_kzalloc(&func->dev, sizeof(*card), GFP_KERNEL);
1525 if (id->driver_data) {
1526 struct btmrvl_sdio_device *data = (void *) id->driver_data;
1527 card->helper = data->helper;
1528 card->firmware = data->firmware;
1529 card->reg = data->reg;
1530 card->sd_blksz_fw_dl = data->sd_blksz_fw_dl;
1531 card->support_pscan_win_report = data->support_pscan_win_report;
1532 card->supports_fw_dump = data->supports_fw_dump;
1535 if (btmrvl_sdio_register_dev(card) < 0) {
1536 BT_ERR("Failed to register BT device!");
1540 /* Disable the interrupts on the card */
1541 btmrvl_sdio_disable_host_int(card);
1543 if (btmrvl_sdio_download_fw(card)) {
1544 BT_ERR("Downloading firmware failed!");
1549 btmrvl_sdio_enable_host_int(card);
1551 /* Device tree node parsing and platform specific configuration*/
1552 btmrvl_sdio_probe_of(&func->dev, card);
1554 priv = btmrvl_add_card(card);
1556 BT_ERR("Initializing card failed!");
1558 goto disable_host_int;
1563 /* Initialize the interface specific function pointers */
1564 priv->hw_host_to_card = btmrvl_sdio_host_to_card;
1565 priv->hw_wakeup_firmware = btmrvl_sdio_wakeup_fw;
1566 priv->hw_process_int_status = btmrvl_sdio_process_int_status;
1568 if (btmrvl_register_hdev(priv)) {
1569 BT_ERR("Register hdev failed!");
1571 goto disable_host_int;
1577 btmrvl_sdio_disable_host_int(card);
1579 btmrvl_sdio_unregister_dev(card);
1583 static void btmrvl_sdio_remove(struct sdio_func *func)
1585 struct btmrvl_sdio_card *card;
1588 card = sdio_get_drvdata(func);
1590 /* Send SHUTDOWN command & disable interrupt
1591 * if user removes the module.
1594 btmrvl_send_module_cfg_cmd(card->priv,
1595 MODULE_SHUTDOWN_REQ);
1596 btmrvl_sdio_disable_host_int(card);
1599 BT_DBG("unregister dev");
1600 card->priv->surprise_removed = true;
1601 btmrvl_sdio_unregister_dev(card);
1602 btmrvl_remove_card(card->priv);
1607 static int btmrvl_sdio_suspend(struct device *dev)
1609 struct sdio_func *func = dev_to_sdio_func(dev);
1610 struct btmrvl_sdio_card *card;
1611 struct btmrvl_private *priv;
1612 mmc_pm_flag_t pm_flags;
1613 struct hci_dev *hcidev;
1616 pm_flags = sdio_get_host_pm_caps(func);
1617 BT_DBG("%s: suspend: PM flags = 0x%x", sdio_func_id(func),
1619 if (!(pm_flags & MMC_PM_KEEP_POWER)) {
1620 BT_ERR("%s: cannot remain alive while suspended",
1621 sdio_func_id(func));
1624 card = sdio_get_drvdata(func);
1625 if (!card || !card->priv) {
1626 BT_ERR("card or priv structure is not valid");
1630 BT_ERR("sdio_func is not specified");
1634 /* Enable platform specific wakeup interrupt */
1635 if (card->plt_wake_cfg && card->plt_wake_cfg->irq_bt >= 0 &&
1636 device_may_wakeup(dev)) {
1637 card->plt_wake_cfg->wake_by_bt = false;
1638 enable_irq(card->plt_wake_cfg->irq_bt);
1639 enable_irq_wake(card->plt_wake_cfg->irq_bt);
1643 priv->adapter->is_suspending = true;
1644 hcidev = priv->btmrvl_dev.hcidev;
1645 BT_DBG("%s: SDIO suspend", hcidev->name);
1646 hci_suspend_dev(hcidev);
1648 if (priv->adapter->hs_state != HS_ACTIVATED) {
1649 if (btmrvl_enable_hs(priv)) {
1650 BT_ERR("HS not activated, suspend failed!");
1651 /* Disable platform specific wakeup interrupt */
1652 if (card->plt_wake_cfg &&
1653 card->plt_wake_cfg->irq_bt >= 0 &&
1654 device_may_wakeup(dev)) {
1655 disable_irq_wake(card->plt_wake_cfg->irq_bt);
1656 disable_irq(card->plt_wake_cfg->irq_bt);
1659 priv->adapter->is_suspending = false;
1664 priv->adapter->is_suspending = false;
1665 priv->adapter->is_suspended = true;
1667 /* We will keep the power when hs enabled successfully */
1668 if (priv->adapter->hs_state == HS_ACTIVATED) {
1669 BT_DBG("suspend with MMC_PM_KEEP_POWER");
1670 return sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
1673 BT_DBG("suspend without MMC_PM_KEEP_POWER");
1677 static int btmrvl_sdio_resume(struct device *dev)
1679 struct sdio_func *func = dev_to_sdio_func(dev);
1680 struct btmrvl_sdio_card *card;
1681 struct btmrvl_private *priv;
1682 mmc_pm_flag_t pm_flags;
1683 struct hci_dev *hcidev;
1686 pm_flags = sdio_get_host_pm_caps(func);
1687 BT_DBG("%s: resume: PM flags = 0x%x", sdio_func_id(func),
1689 card = sdio_get_drvdata(func);
1690 if (!card || !card->priv) {
1691 BT_ERR("card or priv structure is not valid");
1695 BT_ERR("sdio_func is not specified");
1700 if (!priv->adapter->is_suspended) {
1701 BT_DBG("device already resumed");
1705 priv->hw_wakeup_firmware(priv);
1706 priv->adapter->hs_state = HS_DEACTIVATED;
1707 hcidev = priv->btmrvl_dev.hcidev;
1708 BT_DBG("%s: HS DEACTIVATED in resume!", hcidev->name);
1709 priv->adapter->is_suspended = false;
1710 BT_DBG("%s: SDIO resume", hcidev->name);
1711 hci_resume_dev(hcidev);
1713 /* Disable platform specific wakeup interrupt */
1714 if (card->plt_wake_cfg && card->plt_wake_cfg->irq_bt >= 0 &&
1715 device_may_wakeup(dev)) {
1716 disable_irq_wake(card->plt_wake_cfg->irq_bt);
1717 disable_irq(card->plt_wake_cfg->irq_bt);
1718 if (card->plt_wake_cfg->wake_by_bt)
1719 /* Undo our disable, since interrupt handler already
1722 enable_irq(card->plt_wake_cfg->irq_bt);
1728 static const struct dev_pm_ops btmrvl_sdio_pm_ops = {
1729 .suspend = btmrvl_sdio_suspend,
1730 .resume = btmrvl_sdio_resume,
1733 static struct sdio_driver bt_mrvl_sdio = {
1734 .name = "btmrvl_sdio",
1735 .id_table = btmrvl_sdio_ids,
1736 .probe = btmrvl_sdio_probe,
1737 .remove = btmrvl_sdio_remove,
1739 .owner = THIS_MODULE,
1740 .coredump = btmrvl_sdio_coredump,
1741 .pm = &btmrvl_sdio_pm_ops,
1745 static int __init btmrvl_sdio_init_module(void)
1747 if (sdio_register_driver(&bt_mrvl_sdio) != 0) {
1748 BT_ERR("SDIO Driver Registration Failed");
1752 /* Clear the flag in case user removes the card. */
1758 static void __exit btmrvl_sdio_exit_module(void)
1760 /* Set the flag as user is removing this module. */
1763 sdio_unregister_driver(&bt_mrvl_sdio);
1766 module_init(btmrvl_sdio_init_module);
1767 module_exit(btmrvl_sdio_exit_module);
1769 MODULE_AUTHOR("Marvell International Ltd.");
1770 MODULE_DESCRIPTION("Marvell BT-over-SDIO driver ver " VERSION);
1771 MODULE_VERSION(VERSION);
1772 MODULE_LICENSE("GPL v2");
1773 MODULE_FIRMWARE("mrvl/sd8688_helper.bin");
1774 MODULE_FIRMWARE("mrvl/sd8688.bin");
1775 MODULE_FIRMWARE("mrvl/sd8787_uapsta.bin");
1776 MODULE_FIRMWARE("mrvl/sd8797_uapsta.bin");
1777 MODULE_FIRMWARE("mrvl/sd8887_uapsta.bin");
1778 MODULE_FIRMWARE("mrvl/sd8897_uapsta.bin");
1779 MODULE_FIRMWARE("mrvl/sdsd8977_combo_v2.bin");
1780 MODULE_FIRMWARE("mrvl/sd8987_uapsta.bin");
1781 MODULE_FIRMWARE("mrvl/sdsd8997_combo_v4.bin");