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>
27 #include <net/bluetooth/bluetooth.h>
28 #include <net/bluetooth/hci_core.h>
30 #include "btmrvl_drv.h"
31 #include "btmrvl_sdio.h"
35 /* The btmrvl_sdio_remove() callback function is called
36 * when user removes this module from kernel space or ejects
37 * the card from the slot. The driver handles these 2 cases
39 * If the user is removing the module, a MODULE_SHUTDOWN_REQ
40 * command is sent to firmware and interrupt will be disabled.
41 * If the card is removed, there is no need to send command
42 * or disable interrupt.
44 * The variable 'user_rmmod' is used to distinguish these two
45 * scenarios. This flag is initialized as FALSE in case the card
46 * is removed, and will be set to TRUE for module removal when
47 * module_exit function is called.
52 static const struct btmrvl_sdio_device btmrvl_sdio_sd6888 = {
53 .helper = "sd8688_helper.bin",
54 .firmware = "sd8688.bin",
57 static const struct sdio_device_id btmrvl_sdio_ids[] = {
58 /* Marvell SD8688 Bluetooth device */
59 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x9105),
60 .driver_data = (unsigned long) &btmrvl_sdio_sd6888 },
62 { } /* Terminating entry */
65 MODULE_DEVICE_TABLE(sdio, btmrvl_sdio_ids);
67 static int btmrvl_sdio_get_rx_unit(struct btmrvl_sdio_card *card)
72 reg = sdio_readb(card->func, CARD_RX_UNIT_REG, &ret);
79 static int btmrvl_sdio_read_fw_status(struct btmrvl_sdio_card *card, u16 *dat)
86 fws0 = sdio_readb(card->func, CARD_FW_STATUS0_REG, &ret);
90 fws1 = sdio_readb(card->func, CARD_FW_STATUS1_REG, &ret);
94 *dat = (((u16) fws1) << 8) | fws0;
99 static int btmrvl_sdio_read_rx_len(struct btmrvl_sdio_card *card, u16 *dat)
104 reg = sdio_readb(card->func, CARD_RX_LEN_REG, &ret);
106 *dat = (u16) reg << card->rx_unit;
111 static int btmrvl_sdio_enable_host_int_mask(struct btmrvl_sdio_card *card,
116 sdio_writeb(card->func, mask, HOST_INT_MASK_REG, &ret);
118 BT_ERR("Unable to enable the host interrupt!");
125 static int btmrvl_sdio_disable_host_int_mask(struct btmrvl_sdio_card *card,
131 host_int_mask = sdio_readb(card->func, HOST_INT_MASK_REG, &ret);
135 host_int_mask &= ~mask;
137 sdio_writeb(card->func, host_int_mask, HOST_INT_MASK_REG, &ret);
139 BT_ERR("Unable to disable the host interrupt!");
146 static int btmrvl_sdio_poll_card_status(struct btmrvl_sdio_card *card, u8 bits)
152 for (tries = 0; tries < MAX_POLL_TRIES * 1000; tries++) {
153 status = sdio_readb(card->func, CARD_STATUS_REG, &ret);
156 if ((status & bits) == bits)
165 BT_ERR("FAILED! ret=%d", ret);
170 static int btmrvl_sdio_verify_fw_download(struct btmrvl_sdio_card *card,
173 int ret = -ETIMEDOUT;
177 /* Wait for firmware to become ready */
178 for (tries = 0; tries < pollnum; tries++) {
179 if (btmrvl_sdio_read_fw_status(card, &firmwarestat) < 0)
182 if (firmwarestat == FIRMWARE_READY) {
193 static int btmrvl_sdio_download_helper(struct btmrvl_sdio_card *card)
195 const struct firmware *fw_helper = NULL;
196 const u8 *helper = NULL;
198 void *tmphlprbuf = NULL;
199 int tmphlprbufsz, hlprblknow, helperlen;
203 ret = request_firmware(&fw_helper, card->helper,
205 if ((ret < 0) || !fw_helper) {
206 BT_ERR("request_firmware(helper) failed, error code = %d",
212 helper = fw_helper->data;
213 helperlen = fw_helper->size;
215 BT_DBG("Downloading helper image (%d bytes), block size %d bytes",
216 helperlen, SDIO_BLOCK_SIZE);
218 tmphlprbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
220 tmphlprbuf = kzalloc(tmphlprbufsz, GFP_KERNEL);
222 BT_ERR("Unable to allocate buffer for helper."
223 " Terminating download");
228 helperbuf = (u8 *) ALIGN_ADDR(tmphlprbuf, BTSDIO_DMA_ALIGN);
230 /* Perform helper data transfer */
231 tx_len = (FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE)
236 ret = btmrvl_sdio_poll_card_status(card,
237 CARD_IO_READY | DN_LD_CARD_RDY);
239 BT_ERR("Helper download poll status timeout @ %d",
244 /* Check if there is more data? */
245 if (hlprblknow >= helperlen)
248 if (helperlen - hlprblknow < tx_len)
249 tx_len = helperlen - hlprblknow;
252 helperbuf[0] = ((tx_len & 0x000000ff) >> 0);
253 helperbuf[1] = ((tx_len & 0x0000ff00) >> 8);
254 helperbuf[2] = ((tx_len & 0x00ff0000) >> 16);
255 helperbuf[3] = ((tx_len & 0xff000000) >> 24);
257 memcpy(&helperbuf[SDIO_HEADER_LEN], &helper[hlprblknow],
260 /* Now send the data */
261 ret = sdio_writesb(card->func, card->ioport, helperbuf,
262 FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE);
264 BT_ERR("IO error during helper download @ %d",
269 hlprblknow += tx_len;
272 BT_DBG("Transferring helper image EOF block");
274 memset(helperbuf, 0x0, SDIO_BLOCK_SIZE);
276 ret = sdio_writesb(card->func, card->ioport, helperbuf,
279 BT_ERR("IO error in writing helper image EOF block");
288 release_firmware(fw_helper);
293 static int btmrvl_sdio_download_fw_w_helper(struct btmrvl_sdio_card *card)
295 const struct firmware *fw_firmware = NULL;
296 const u8 *firmware = NULL;
297 int firmwarelen, tmpfwbufsz, ret;
298 unsigned int tries, offset;
300 void *tmpfwbuf = NULL;
303 int txlen = 0, tx_blocks = 0, count = 0;
305 ret = request_firmware(&fw_firmware, card->firmware,
307 if ((ret < 0) || !fw_firmware) {
308 BT_ERR("request_firmware(firmware) failed, error code = %d",
314 firmware = fw_firmware->data;
315 firmwarelen = fw_firmware->size;
317 BT_DBG("Downloading FW image (%d bytes)", firmwarelen);
319 tmpfwbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
320 tmpfwbuf = kzalloc(tmpfwbufsz, GFP_KERNEL);
322 BT_ERR("Unable to allocate buffer for firmware."
323 " Terminating download");
328 /* Ensure aligned firmware buffer */
329 fwbuf = (u8 *) ALIGN_ADDR(tmpfwbuf, BTSDIO_DMA_ALIGN);
331 /* Perform firmware data transfer */
334 ret = btmrvl_sdio_poll_card_status(card,
335 CARD_IO_READY | DN_LD_CARD_RDY);
337 BT_ERR("FW download with helper poll status"
338 " timeout @ %d", offset);
342 /* Check if there is more data ? */
343 if (offset >= firmwarelen)
346 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
347 base0 = sdio_readb(card->func,
348 SQ_READ_BASE_ADDRESS_A0_REG, &ret);
350 BT_ERR("BASE0 register read failed:"
351 " base0 = 0x%04X(%d)."
352 " Terminating download",
357 base1 = sdio_readb(card->func,
358 SQ_READ_BASE_ADDRESS_A1_REG, &ret);
360 BT_ERR("BASE1 register read failed:"
361 " base1 = 0x%04X(%d)."
362 " Terminating download",
368 len = (((u16) base1) << 8) | base0;
377 else if (len > BTM_UPLD_SIZE) {
378 BT_ERR("FW download failure @%d, invalid length %d",
388 if (count > MAX_WRITE_IOMEM_RETRY) {
389 BT_ERR("FW download failure @%d, "
390 "over max retry count", offset);
394 BT_ERR("FW CRC error indicated by the helper: "
395 "len = 0x%04X, txlen = %d", len, txlen);
397 /* Set txlen to 0 so as to resend from same offset */
403 if (firmwarelen - offset < txlen)
404 txlen = firmwarelen - offset;
407 (txlen + SDIO_BLOCK_SIZE - 1) / SDIO_BLOCK_SIZE;
409 memcpy(fwbuf, &firmware[offset], txlen);
412 ret = sdio_writesb(card->func, card->ioport, fwbuf,
413 tx_blocks * SDIO_BLOCK_SIZE);
416 BT_ERR("FW download, writesb(%d) failed @%d",
418 sdio_writeb(card->func, HOST_CMD53_FIN, CONFIG_REG,
421 BT_ERR("writeb failed (CFG)");
427 BT_DBG("FW download over, size %d bytes", offset);
435 release_firmware(fw_firmware);
440 static int btmrvl_sdio_card_to_host(struct btmrvl_private *priv)
443 int ret, buf_block_len, blksz;
444 struct sk_buff *skb = NULL;
447 struct hci_dev *hdev = priv->btmrvl_dev.hcidev;
448 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
450 if (!card || !card->func) {
451 BT_ERR("card or function is NULL!");
456 /* Read the length of data to be transferred */
457 ret = btmrvl_sdio_read_rx_len(card, &buf_len);
459 BT_ERR("read rx_len failed");
464 blksz = SDIO_BLOCK_SIZE;
465 buf_block_len = (buf_len + blksz - 1) / blksz;
467 if (buf_len <= SDIO_HEADER_LEN
468 || (buf_block_len * blksz) > ALLOC_BUF_SIZE) {
469 BT_ERR("invalid packet length: %d", buf_len);
474 /* Allocate buffer */
475 skb = bt_skb_alloc(buf_block_len * blksz + BTSDIO_DMA_ALIGN,
478 BT_ERR("No free skb");
482 if ((unsigned long) skb->data & (BTSDIO_DMA_ALIGN - 1)) {
483 skb_put(skb, (unsigned long) skb->data &
484 (BTSDIO_DMA_ALIGN - 1));
485 skb_pull(skb, (unsigned long) skb->data &
486 (BTSDIO_DMA_ALIGN - 1));
491 ret = sdio_readsb(card->func, payload, card->ioport,
492 buf_block_len * blksz);
494 BT_ERR("readsb failed: %d", ret);
499 /* This is SDIO specific header length: byte[2][1][0], type: byte[3]
500 * (HCI_COMMAND = 1, ACL_DATA = 2, SCO_DATA = 3, 0xFE = Vendor)
503 buf_len = payload[0];
504 buf_len |= (u16) payload[1] << 8;
508 case HCI_ACLDATA_PKT:
509 case HCI_SCODATA_PKT:
511 bt_cb(skb)->pkt_type = type;
512 skb->dev = (void *)hdev;
513 skb_put(skb, buf_len);
514 skb_pull(skb, SDIO_HEADER_LEN);
516 if (type == HCI_EVENT_PKT)
517 btmrvl_check_evtpkt(priv, skb);
520 hdev->stat.byte_rx += buf_len;
523 case MRVL_VENDOR_PKT:
524 bt_cb(skb)->pkt_type = HCI_VENDOR_PKT;
525 skb->dev = (void *)hdev;
526 skb_put(skb, buf_len);
527 skb_pull(skb, SDIO_HEADER_LEN);
529 if (btmrvl_process_event(priv, skb))
532 hdev->stat.byte_rx += buf_len;
536 BT_ERR("Unknown packet type:%d", type);
537 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, payload,
538 blksz * buf_block_len);
555 static int btmrvl_sdio_process_int_status(struct btmrvl_private *priv)
559 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
561 spin_lock_irqsave(&priv->driver_lock, flags);
564 spin_unlock_irqrestore(&priv->driver_lock, flags);
566 sdio_claim_host(card->func);
567 if (ireg & DN_LD_HOST_INT_STATUS) {
568 if (priv->btmrvl_dev.tx_dnld_rdy)
569 BT_DBG("tx_done already received: "
570 " int_status=0x%x", ireg);
572 priv->btmrvl_dev.tx_dnld_rdy = true;
575 if (ireg & UP_LD_HOST_INT_STATUS)
576 btmrvl_sdio_card_to_host(priv);
578 sdio_release_host(card->func);
583 static void btmrvl_sdio_interrupt(struct sdio_func *func)
585 struct btmrvl_private *priv;
586 struct btmrvl_sdio_card *card;
591 card = sdio_get_drvdata(func);
592 if (!card || !card->priv) {
593 BT_ERR("sbi_interrupt(%p) card or priv is "
594 "NULL, card=%p\n", func, card);
600 ireg = sdio_readb(card->func, HOST_INTSTATUS_REG, &ret);
602 BT_ERR("sdio_readb: read int status register failed");
608 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
609 * Clear the interrupt status register and re-enable the
612 BT_DBG("ireg = 0x%x", ireg);
614 sdio_writeb(card->func, ~(ireg) & (DN_LD_HOST_INT_STATUS |
615 UP_LD_HOST_INT_STATUS),
616 HOST_INTSTATUS_REG, &ret);
618 BT_ERR("sdio_writeb: clear int status register failed");
623 spin_lock_irqsave(&priv->driver_lock, flags);
625 spin_unlock_irqrestore(&priv->driver_lock, flags);
627 btmrvl_interrupt(priv);
630 static int btmrvl_sdio_register_dev(struct btmrvl_sdio_card *card)
632 struct sdio_func *func;
636 if (!card || !card->func) {
637 BT_ERR("Error: card or function is NULL!");
644 sdio_claim_host(func);
646 ret = sdio_enable_func(func);
648 BT_ERR("sdio_enable_func() failed: ret=%d", ret);
653 ret = sdio_claim_irq(func, btmrvl_sdio_interrupt);
655 BT_ERR("sdio_claim_irq failed: ret=%d", ret);
660 ret = sdio_set_block_size(card->func, SDIO_BLOCK_SIZE);
662 BT_ERR("cannot set SDIO block size");
667 reg = sdio_readb(func, IO_PORT_0_REG, &ret);
675 reg = sdio_readb(func, IO_PORT_1_REG, &ret);
681 card->ioport |= (reg << 8);
683 reg = sdio_readb(func, IO_PORT_2_REG, &ret);
689 card->ioport |= (reg << 16);
691 BT_DBG("SDIO FUNC%d IO port: 0x%x", func->num, card->ioport);
693 sdio_set_drvdata(func, card);
695 sdio_release_host(func);
700 sdio_release_irq(func);
703 sdio_disable_func(func);
706 sdio_release_host(func);
712 static int btmrvl_sdio_unregister_dev(struct btmrvl_sdio_card *card)
714 if (card && card->func) {
715 sdio_claim_host(card->func);
716 sdio_release_irq(card->func);
717 sdio_disable_func(card->func);
718 sdio_release_host(card->func);
719 sdio_set_drvdata(card->func, NULL);
725 static int btmrvl_sdio_enable_host_int(struct btmrvl_sdio_card *card)
729 if (!card || !card->func)
732 sdio_claim_host(card->func);
734 ret = btmrvl_sdio_enable_host_int_mask(card, HIM_ENABLE);
736 btmrvl_sdio_get_rx_unit(card);
738 sdio_release_host(card->func);
743 static int btmrvl_sdio_disable_host_int(struct btmrvl_sdio_card *card)
747 if (!card || !card->func)
750 sdio_claim_host(card->func);
752 ret = btmrvl_sdio_disable_host_int_mask(card, HIM_DISABLE);
754 sdio_release_host(card->func);
759 static int btmrvl_sdio_host_to_card(struct btmrvl_private *priv,
762 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
771 if (!card || !card->func) {
772 BT_ERR("card or function is NULL!");
777 if ((unsigned long) payload & (BTSDIO_DMA_ALIGN - 1)) {
778 tmpbufsz = ALIGN_SZ(nb, BTSDIO_DMA_ALIGN);
779 tmpbuf = kzalloc(tmpbufsz, GFP_KERNEL);
782 buf = (u8 *) ALIGN_ADDR(tmpbuf, BTSDIO_DMA_ALIGN);
783 memcpy(buf, payload, nb);
786 blksz = SDIO_BLOCK_SIZE;
787 buf_block_len = (nb + blksz - 1) / blksz;
789 sdio_claim_host(card->func);
792 /* Transfer data to card */
793 ret = sdio_writesb(card->func, card->ioport, buf,
794 buf_block_len * blksz);
797 BT_ERR("i=%d writesb failed: %d", i, ret);
798 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
801 if (i > MAX_WRITE_IOMEM_RETRY)
806 priv->btmrvl_dev.tx_dnld_rdy = false;
809 sdio_release_host(card->func);
815 static int btmrvl_sdio_download_fw(struct btmrvl_sdio_card *card)
819 if (!card || !card->func) {
820 BT_ERR("card or function is NULL!");
823 sdio_claim_host(card->func);
825 if (!btmrvl_sdio_verify_fw_download(card, 1)) {
826 BT_DBG("Firmware already downloaded!");
830 ret = btmrvl_sdio_download_helper(card);
832 BT_ERR("Failed to download helper!");
837 if (btmrvl_sdio_download_fw_w_helper(card)) {
838 BT_ERR("Failed to download firmware!");
843 if (btmrvl_sdio_verify_fw_download(card, MAX_POLL_TRIES)) {
844 BT_ERR("FW failed to be active in time!");
850 sdio_release_host(card->func);
855 static int btmrvl_sdio_wakeup_fw(struct btmrvl_private *priv)
857 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
860 if (!card || !card->func) {
861 BT_ERR("card or function is NULL!");
865 sdio_claim_host(card->func);
867 sdio_writeb(card->func, HOST_POWER_UP, CONFIG_REG, &ret);
869 sdio_release_host(card->func);
871 BT_DBG("wake up firmware");
876 static int btmrvl_sdio_probe(struct sdio_func *func,
877 const struct sdio_device_id *id)
880 struct btmrvl_private *priv = NULL;
881 struct btmrvl_sdio_card *card = NULL;
883 BT_INFO("vendor=0x%x, device=0x%x, class=%d, fn=%d",
884 id->vendor, id->device, id->class, func->num);
886 card = kzalloc(sizeof(*card), GFP_KERNEL);
894 if (id->driver_data) {
895 struct btmrvl_sdio_device *data = (void *) id->driver_data;
896 card->helper = data->helper;
897 card->firmware = data->firmware;
900 if (btmrvl_sdio_register_dev(card) < 0) {
901 BT_ERR("Failed to register BT device!");
906 /* Disable the interrupts on the card */
907 btmrvl_sdio_disable_host_int(card);
909 if (btmrvl_sdio_download_fw(card)) {
910 BT_ERR("Downloading firmware failed!");
917 btmrvl_sdio_enable_host_int(card);
919 priv = btmrvl_add_card(card);
921 BT_ERR("Initializing card failed!");
923 goto disable_host_int;
928 /* Initialize the interface specific function pointers */
929 priv->hw_host_to_card = btmrvl_sdio_host_to_card;
930 priv->hw_wakeup_firmware = btmrvl_sdio_wakeup_fw;
931 priv->hw_process_int_status = btmrvl_sdio_process_int_status;
933 if (btmrvl_register_hdev(priv)) {
934 BT_ERR("Register hdev failed!");
936 goto disable_host_int;
939 priv->btmrvl_dev.psmode = 1;
940 btmrvl_enable_ps(priv);
945 btmrvl_sdio_disable_host_int(card);
947 btmrvl_sdio_unregister_dev(card);
954 static void btmrvl_sdio_remove(struct sdio_func *func)
956 struct btmrvl_sdio_card *card;
959 card = sdio_get_drvdata(func);
961 /* Send SHUTDOWN command & disable interrupt
962 * if user removes the module.
965 btmrvl_send_module_cfg_cmd(card->priv,
966 MODULE_SHUTDOWN_REQ);
967 btmrvl_sdio_disable_host_int(card);
969 BT_DBG("unregester dev");
970 btmrvl_sdio_unregister_dev(card);
971 btmrvl_remove_card(card->priv);
977 static struct sdio_driver bt_mrvl_sdio = {
978 .name = "btmrvl_sdio",
979 .id_table = btmrvl_sdio_ids,
980 .probe = btmrvl_sdio_probe,
981 .remove = btmrvl_sdio_remove,
984 static int __init btmrvl_sdio_init_module(void)
986 if (sdio_register_driver(&bt_mrvl_sdio) != 0) {
987 BT_ERR("SDIO Driver Registration Failed");
991 /* Clear the flag in case user removes the card. */
997 static void __exit btmrvl_sdio_exit_module(void)
999 /* Set the flag as user is removing this module. */
1002 sdio_unregister_driver(&bt_mrvl_sdio);
1005 module_init(btmrvl_sdio_init_module);
1006 module_exit(btmrvl_sdio_exit_module);
1008 MODULE_AUTHOR("Marvell International Ltd.");
1009 MODULE_DESCRIPTION("Marvell BT-over-SDIO driver ver " VERSION);
1010 MODULE_VERSION(VERSION);
1011 MODULE_LICENSE("GPL v2");
1012 MODULE_FIRMWARE("sd8688_helper.bin");
1013 MODULE_FIRMWARE("sd8688.bin");