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.
51 static const struct btmrvl_sdio_device btmrvl_sdio_sd6888 = {
52 .helper = "sd8688_helper.bin",
53 .firmware = "sd8688.bin",
56 static const struct sdio_device_id btmrvl_sdio_ids[] = {
57 /* Marvell SD8688 Bluetooth device */
58 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x9105),
59 .driver_data = (unsigned long) &btmrvl_sdio_sd6888 },
61 { } /* Terminating entry */
64 MODULE_DEVICE_TABLE(sdio, btmrvl_sdio_ids);
66 static int btmrvl_sdio_get_rx_unit(struct btmrvl_sdio_card *card)
71 reg = sdio_readb(card->func, CARD_RX_UNIT_REG, &ret);
78 static int btmrvl_sdio_read_fw_status(struct btmrvl_sdio_card *card, u16 *dat)
85 fws0 = sdio_readb(card->func, CARD_FW_STATUS0_REG, &ret);
88 fws1 = sdio_readb(card->func, CARD_FW_STATUS1_REG, &ret);
93 *dat = (((u16) fws1) << 8) | fws0;
98 static int btmrvl_sdio_read_rx_len(struct btmrvl_sdio_card *card, u16 *dat)
103 reg = sdio_readb(card->func, CARD_RX_LEN_REG, &ret);
105 *dat = (u16) reg << card->rx_unit;
110 static int btmrvl_sdio_enable_host_int_mask(struct btmrvl_sdio_card *card,
115 sdio_writeb(card->func, mask, HOST_INT_MASK_REG, &ret);
117 BT_ERR("Unable to enable the host interrupt!");
124 static int btmrvl_sdio_disable_host_int_mask(struct btmrvl_sdio_card *card,
130 host_int_mask = sdio_readb(card->func, HOST_INT_MASK_REG, &ret);
134 host_int_mask &= ~mask;
136 sdio_writeb(card->func, host_int_mask, HOST_INT_MASK_REG, &ret);
138 BT_ERR("Unable to disable the host interrupt!");
145 static int btmrvl_sdio_poll_card_status(struct btmrvl_sdio_card *card, u8 bits)
151 for (tries = 0; tries < MAX_POLL_TRIES * 1000; tries++) {
152 status = sdio_readb(card->func, CARD_STATUS_REG, &ret);
155 if ((status & bits) == bits)
164 BT_ERR("FAILED! ret=%d", ret);
169 static int btmrvl_sdio_verify_fw_download(struct btmrvl_sdio_card *card,
172 int ret = -ETIMEDOUT;
176 /* Wait for firmware to become ready */
177 for (tries = 0; tries < pollnum; tries++) {
178 if (btmrvl_sdio_read_fw_status(card, &firmwarestat) < 0)
181 if (firmwarestat == FIRMWARE_READY) {
192 static int btmrvl_sdio_download_helper(struct btmrvl_sdio_card *card)
194 const struct firmware *fw_helper = NULL;
195 const u8 *helper = NULL;
197 void *tmphlprbuf = NULL;
198 int tmphlprbufsz, hlprblknow, helperlen;
202 ret = request_firmware(&fw_helper, card->helper,
204 if ((ret < 0) || !fw_helper) {
205 BT_ERR("request_firmware(helper) failed, error code = %d",
211 helper = fw_helper->data;
212 helperlen = fw_helper->size;
214 BT_DBG("Downloading helper image (%d bytes), block size %d bytes",
215 helperlen, SDIO_BLOCK_SIZE);
217 tmphlprbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
219 tmphlprbuf = kmalloc(tmphlprbufsz, GFP_KERNEL);
221 BT_ERR("Unable to allocate buffer for helper."
222 " Terminating download");
227 memset(tmphlprbuf, 0, tmphlprbufsz);
229 helperbuf = (u8 *) ALIGN_ADDR(tmphlprbuf, BTSDIO_DMA_ALIGN);
231 /* Perform helper data transfer */
232 tx_len = (FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE)
237 ret = btmrvl_sdio_poll_card_status(card,
238 CARD_IO_READY | DN_LD_CARD_RDY);
240 BT_ERR("Helper download poll status timeout @ %d",
245 /* Check if there is more data? */
246 if (hlprblknow >= helperlen)
249 if (helperlen - hlprblknow < tx_len)
250 tx_len = helperlen - hlprblknow;
253 helperbuf[0] = ((tx_len & 0x000000ff) >> 0);
254 helperbuf[1] = ((tx_len & 0x0000ff00) >> 8);
255 helperbuf[2] = ((tx_len & 0x00ff0000) >> 16);
256 helperbuf[3] = ((tx_len & 0xff000000) >> 24);
258 memcpy(&helperbuf[SDIO_HEADER_LEN], &helper[hlprblknow],
261 /* Now send the data */
262 ret = sdio_writesb(card->func, card->ioport, helperbuf,
263 FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE);
265 BT_ERR("IO error during helper download @ %d",
270 hlprblknow += tx_len;
273 BT_DBG("Transferring helper image EOF block");
275 memset(helperbuf, 0x0, SDIO_BLOCK_SIZE);
277 ret = sdio_writesb(card->func, card->ioport, helperbuf,
280 BT_ERR("IO error in writing helper image EOF block");
289 release_firmware(fw_helper);
294 static int btmrvl_sdio_download_fw_w_helper(struct btmrvl_sdio_card *card)
296 const struct firmware *fw_firmware = NULL;
297 const u8 *firmware = NULL;
298 int firmwarelen, tmpfwbufsz, ret;
299 unsigned int tries, offset;
301 void *tmpfwbuf = NULL;
304 int txlen = 0, tx_blocks = 0, count = 0;
306 ret = request_firmware(&fw_firmware, card->firmware,
308 if ((ret < 0) || !fw_firmware) {
309 BT_ERR("request_firmware(firmware) failed, error code = %d",
315 firmware = fw_firmware->data;
316 firmwarelen = fw_firmware->size;
318 BT_DBG("Downloading FW image (%d bytes)", firmwarelen);
320 tmpfwbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
321 tmpfwbuf = kmalloc(tmpfwbufsz, GFP_KERNEL);
323 BT_ERR("Unable to allocate buffer for firmware."
324 " Terminating download");
329 memset(tmpfwbuf, 0, tmpfwbufsz);
331 /* Ensure aligned firmware buffer */
332 fwbuf = (u8 *) ALIGN_ADDR(tmpfwbuf, BTSDIO_DMA_ALIGN);
334 /* Perform firmware data transfer */
337 ret = btmrvl_sdio_poll_card_status(card,
338 CARD_IO_READY | DN_LD_CARD_RDY);
340 BT_ERR("FW download with helper poll status"
341 " timeout @ %d", offset);
345 /* Check if there is more data ? */
346 if (offset >= firmwarelen)
349 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
350 base0 = sdio_readb(card->func,
351 SQ_READ_BASE_ADDRESS_A0_REG, &ret);
353 BT_ERR("BASE0 register read failed:"
354 " base0 = 0x%04X(%d)."
355 " Terminating download",
360 base1 = sdio_readb(card->func,
361 SQ_READ_BASE_ADDRESS_A1_REG, &ret);
363 BT_ERR("BASE1 register read failed:"
364 " base1 = 0x%04X(%d)."
365 " Terminating download",
371 len = (((u16) base1) << 8) | base0;
380 else if (len > BTM_UPLD_SIZE) {
381 BT_ERR("FW download failure @%d, invalid length %d",
391 if (count > MAX_WRITE_IOMEM_RETRY) {
392 BT_ERR("FW download failure @%d, "
393 "over max retry count", offset);
397 BT_ERR("FW CRC error indicated by the helper: "
398 "len = 0x%04X, txlen = %d", len, txlen);
400 /* Set txlen to 0 so as to resend from same offset */
406 if (firmwarelen - offset < txlen)
407 txlen = firmwarelen - offset;
410 (txlen + SDIO_BLOCK_SIZE - 1) / SDIO_BLOCK_SIZE;
412 memcpy(fwbuf, &firmware[offset], txlen);
415 ret = sdio_writesb(card->func, card->ioport, fwbuf,
416 tx_blocks * SDIO_BLOCK_SIZE);
419 BT_ERR("FW download, writesb(%d) failed @%d",
421 sdio_writeb(card->func, HOST_CMD53_FIN, CONFIG_REG,
424 BT_ERR("writeb failed (CFG)");
430 BT_DBG("FW download over, size %d bytes", offset);
438 release_firmware(fw_firmware);
443 static int btmrvl_sdio_card_to_host(struct btmrvl_private *priv)
446 int ret, buf_block_len, blksz;
447 struct sk_buff *skb = NULL;
450 struct hci_dev *hdev = priv->btmrvl_dev.hcidev;
451 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
453 if (!card || !card->func) {
454 BT_ERR("card or function is NULL!");
459 /* Read the length of data to be transferred */
460 ret = btmrvl_sdio_read_rx_len(card, &buf_len);
462 BT_ERR("read rx_len failed");
467 blksz = SDIO_BLOCK_SIZE;
468 buf_block_len = (buf_len + blksz - 1) / blksz;
470 if (buf_len <= SDIO_HEADER_LEN
471 || (buf_block_len * blksz) > ALLOC_BUF_SIZE) {
472 BT_ERR("invalid packet length: %d", buf_len);
477 /* Allocate buffer */
478 skb = bt_skb_alloc(buf_block_len * blksz + BTSDIO_DMA_ALIGN,
481 BT_ERR("No free skb");
485 if ((unsigned long) skb->data & (BTSDIO_DMA_ALIGN - 1)) {
486 skb_put(skb, (unsigned long) skb->data &
487 (BTSDIO_DMA_ALIGN - 1));
488 skb_pull(skb, (unsigned long) skb->data &
489 (BTSDIO_DMA_ALIGN - 1));
494 ret = sdio_readsb(card->func, payload, card->ioport,
495 buf_block_len * blksz);
497 BT_ERR("readsb failed: %d", ret);
502 /* This is SDIO specific header length: byte[2][1][0], type: byte[3]
503 * (HCI_COMMAND = 1, ACL_DATA = 2, SCO_DATA = 3, 0xFE = Vendor)
506 buf_len = payload[0];
507 buf_len |= (u16) payload[1] << 8;
511 case HCI_ACLDATA_PKT:
512 case HCI_SCODATA_PKT:
514 bt_cb(skb)->pkt_type = type;
515 skb->dev = (void *)hdev;
516 skb_put(skb, buf_len);
517 skb_pull(skb, SDIO_HEADER_LEN);
519 if (type == HCI_EVENT_PKT)
520 btmrvl_check_evtpkt(priv, skb);
523 hdev->stat.byte_rx += buf_len;
526 case MRVL_VENDOR_PKT:
527 bt_cb(skb)->pkt_type = HCI_VENDOR_PKT;
528 skb->dev = (void *)hdev;
529 skb_put(skb, buf_len);
530 skb_pull(skb, SDIO_HEADER_LEN);
532 if (btmrvl_process_event(priv, skb))
535 hdev->stat.byte_rx += buf_len;
539 BT_ERR("Unknown packet type:%d", type);
540 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, payload,
541 blksz * buf_block_len);
558 static int btmrvl_sdio_get_int_status(struct btmrvl_private *priv, u8 * ireg)
562 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
566 sdio_ireg = sdio_readb(card->func, HOST_INTSTATUS_REG, &ret);
568 BT_ERR("sdio_readb: read int status register failed");
573 if (sdio_ireg != 0) {
575 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
576 * Clear the interrupt status register and re-enable the
579 BT_DBG("sdio_ireg = 0x%x", sdio_ireg);
581 sdio_writeb(card->func, ~(sdio_ireg) & (DN_LD_HOST_INT_STATUS |
582 UP_LD_HOST_INT_STATUS),
583 HOST_INTSTATUS_REG, &ret);
585 BT_ERR("sdio_writeb: clear int status register "
592 if (sdio_ireg & DN_LD_HOST_INT_STATUS) {
593 if (priv->btmrvl_dev.tx_dnld_rdy)
594 BT_DBG("tx_done already received: "
595 " int_status=0x%x", sdio_ireg);
597 priv->btmrvl_dev.tx_dnld_rdy = true;
600 if (sdio_ireg & UP_LD_HOST_INT_STATUS)
601 btmrvl_sdio_card_to_host(priv);
611 static void btmrvl_sdio_interrupt(struct sdio_func *func)
613 struct btmrvl_private *priv;
614 struct hci_dev *hcidev;
615 struct btmrvl_sdio_card *card;
618 card = sdio_get_drvdata(func);
619 if (card && card->priv) {
621 hcidev = priv->btmrvl_dev.hcidev;
623 if (btmrvl_sdio_get_int_status(priv, &ireg))
624 BT_ERR("reading HOST_INT_STATUS_REG failed");
626 BT_DBG("HOST_INT_STATUS_REG %#x", ireg);
628 btmrvl_interrupt(priv);
632 static int btmrvl_sdio_register_dev(struct btmrvl_sdio_card *card)
634 struct sdio_func *func;
638 if (!card || !card->func) {
639 BT_ERR("Error: card or function is NULL!");
646 sdio_claim_host(func);
648 ret = sdio_enable_func(func);
650 BT_ERR("sdio_enable_func() failed: ret=%d", ret);
655 ret = sdio_claim_irq(func, btmrvl_sdio_interrupt);
657 BT_ERR("sdio_claim_irq failed: ret=%d", ret);
662 ret = sdio_set_block_size(card->func, SDIO_BLOCK_SIZE);
664 BT_ERR("cannot set SDIO block size");
669 reg = sdio_readb(func, IO_PORT_0_REG, &ret);
677 reg = sdio_readb(func, IO_PORT_1_REG, &ret);
683 card->ioport |= (reg << 8);
685 reg = sdio_readb(func, IO_PORT_2_REG, &ret);
691 card->ioport |= (reg << 16);
693 BT_DBG("SDIO FUNC%d IO port: 0x%x", func->num, card->ioport);
695 sdio_set_drvdata(func, card);
697 sdio_release_host(func);
702 sdio_release_irq(func);
705 sdio_disable_func(func);
708 sdio_release_host(func);
714 static int btmrvl_sdio_unregister_dev(struct btmrvl_sdio_card *card)
716 if (card && card->func) {
717 sdio_claim_host(card->func);
718 sdio_release_irq(card->func);
719 sdio_disable_func(card->func);
720 sdio_release_host(card->func);
721 sdio_set_drvdata(card->func, NULL);
727 static int btmrvl_sdio_enable_host_int(struct btmrvl_sdio_card *card)
731 if (!card || !card->func)
734 sdio_claim_host(card->func);
736 ret = btmrvl_sdio_enable_host_int_mask(card, HIM_ENABLE);
738 btmrvl_sdio_get_rx_unit(card);
740 sdio_release_host(card->func);
745 static int btmrvl_sdio_disable_host_int(struct btmrvl_sdio_card *card)
749 if (!card || !card->func)
752 sdio_claim_host(card->func);
754 ret = btmrvl_sdio_disable_host_int_mask(card, HIM_DISABLE);
756 sdio_release_host(card->func);
761 static int btmrvl_sdio_host_to_card(struct btmrvl_private *priv,
764 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
773 if (!card || !card->func) {
774 BT_ERR("card or function is NULL!");
779 if ((unsigned long) payload & (BTSDIO_DMA_ALIGN - 1)) {
780 tmpbufsz = ALIGN_SZ(nb, BTSDIO_DMA_ALIGN);
781 tmpbuf = kzalloc(tmpbufsz, GFP_KERNEL);
784 buf = (u8 *) ALIGN_ADDR(tmpbuf, BTSDIO_DMA_ALIGN);
785 memcpy(buf, payload, nb);
788 blksz = SDIO_BLOCK_SIZE;
789 buf_block_len = (nb + blksz - 1) / blksz;
791 sdio_claim_host(card->func);
794 /* Transfer data to card */
795 ret = sdio_writesb(card->func, card->ioport, buf,
796 buf_block_len * blksz);
799 BT_ERR("i=%d writesb failed: %d", i, ret);
800 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
803 if (i > MAX_WRITE_IOMEM_RETRY)
808 priv->btmrvl_dev.tx_dnld_rdy = false;
811 sdio_release_host(card->func);
817 static int btmrvl_sdio_download_fw(struct btmrvl_sdio_card *card)
821 if (!card || !card->func) {
822 BT_ERR("card or function is NULL!");
825 sdio_claim_host(card->func);
827 if (!btmrvl_sdio_verify_fw_download(card, 1)) {
828 BT_DBG("Firmware already downloaded!");
832 ret = btmrvl_sdio_download_helper(card);
834 BT_ERR("Failed to download helper!");
839 if (btmrvl_sdio_download_fw_w_helper(card)) {
840 BT_ERR("Failed to download firmware!");
845 if (btmrvl_sdio_verify_fw_download(card, MAX_POLL_TRIES)) {
846 BT_ERR("FW failed to be active in time!");
852 sdio_release_host(card->func);
857 static int btmrvl_sdio_wakeup_fw(struct btmrvl_private *priv)
859 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
862 if (!card || !card->func) {
863 BT_ERR("card or function is NULL!");
867 sdio_claim_host(card->func);
869 sdio_writeb(card->func, HOST_POWER_UP, CONFIG_REG, &ret);
871 sdio_release_host(card->func);
873 BT_DBG("wake up firmware");
878 static int btmrvl_sdio_probe(struct sdio_func *func,
879 const struct sdio_device_id *id)
882 struct btmrvl_private *priv = NULL;
883 struct btmrvl_sdio_card *card = NULL;
885 BT_INFO("vendor=0x%x, device=0x%x, class=%d, fn=%d",
886 id->vendor, id->device, id->class, func->num);
888 card = kzalloc(sizeof(*card), GFP_KERNEL);
896 if (id->driver_data) {
897 struct btmrvl_sdio_device *data = (void *) id->driver_data;
898 card->helper = data->helper;
899 card->firmware = data->firmware;
902 if (btmrvl_sdio_register_dev(card) < 0) {
903 BT_ERR("Failed to register BT device!");
908 /* Disable the interrupts on the card */
909 btmrvl_sdio_disable_host_int(card);
911 if (btmrvl_sdio_download_fw(card)) {
912 BT_ERR("Downloading firmware failed!");
919 btmrvl_sdio_enable_host_int(card);
921 priv = btmrvl_add_card(card);
923 BT_ERR("Initializing card failed!");
925 goto disable_host_int;
930 /* Initialize the interface specific function pointers */
931 priv->hw_host_to_card = btmrvl_sdio_host_to_card;
932 priv->hw_wakeup_firmware = btmrvl_sdio_wakeup_fw;
934 btmrvl_send_module_cfg_cmd(priv, MODULE_BRINGUP_REQ);
935 priv->btmrvl_dev.psmode = 1;
936 btmrvl_enable_ps(priv);
941 btmrvl_sdio_disable_host_int(card);
943 btmrvl_sdio_unregister_dev(card);
950 static void btmrvl_sdio_remove(struct sdio_func *func)
952 struct btmrvl_sdio_card *card;
955 card = sdio_get_drvdata(func);
957 /* Send SHUTDOWN command & disable interrupt
958 * if user removes the module.
961 btmrvl_send_module_cfg_cmd(card->priv,
962 MODULE_SHUTDOWN_REQ);
963 btmrvl_sdio_disable_host_int(card);
965 BT_DBG("unregester dev");
966 btmrvl_sdio_unregister_dev(card);
967 btmrvl_remove_card(card->priv);
973 static struct sdio_driver bt_mrvl_sdio = {
974 .name = "btmrvl_sdio",
975 .id_table = btmrvl_sdio_ids,
976 .probe = btmrvl_sdio_probe,
977 .remove = btmrvl_sdio_remove,
980 static int __init btmrvl_sdio_init_module(void)
982 if (sdio_register_driver(&bt_mrvl_sdio) != 0) {
983 BT_ERR("SDIO Driver Registration Failed");
987 /* Clear the flag in case user removes the card. */
993 static void __exit btmrvl_sdio_exit_module(void)
995 /* Set the flag as user is removing this module. */
998 sdio_unregister_driver(&bt_mrvl_sdio);
1001 module_init(btmrvl_sdio_init_module);
1002 module_exit(btmrvl_sdio_exit_module);
1004 MODULE_AUTHOR("Marvell International Ltd.");
1005 MODULE_DESCRIPTION("Marvell BT-over-SDIO driver ver " VERSION);
1006 MODULE_VERSION(VERSION);
1007 MODULE_LICENSE("GPL v2");
1008 MODULE_FIRMWARE("sd8688_helper.bin");
1009 MODULE_FIRMWARE("sd8688.bin");