Merge branch 'for-next' of git://git.kernel.org/pub/scm/linux/kernel/git/nab/target...
[linux-btrfs-devel.git] / drivers / bluetooth / btmrvl_sdio.c
blob7f521d4ac657e13764e9917fd45f393b0933eee3
1 /**
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.
19 **/
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"
33 #define VERSION "1.0"
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
38 * differently.
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.
49 static u8 user_rmmod;
50 static u8 sdio_ireg;
52 static const struct btmrvl_sdio_card_reg btmrvl_reg_8688 = {
53 .cfg = 0x03,
54 .host_int_mask = 0x04,
55 .host_intstatus = 0x05,
56 .card_status = 0x20,
57 .sq_read_base_addr_a0 = 0x10,
58 .sq_read_base_addr_a1 = 0x11,
59 .card_fw_status0 = 0x40,
60 .card_fw_status1 = 0x41,
61 .card_rx_len = 0x42,
62 .card_rx_unit = 0x43,
63 .io_port_0 = 0x00,
64 .io_port_1 = 0x01,
65 .io_port_2 = 0x02,
67 static const struct btmrvl_sdio_card_reg btmrvl_reg_8787 = {
68 .cfg = 0x00,
69 .host_int_mask = 0x02,
70 .host_intstatus = 0x03,
71 .card_status = 0x30,
72 .sq_read_base_addr_a0 = 0x40,
73 .sq_read_base_addr_a1 = 0x41,
74 .card_revision = 0x5c,
75 .card_fw_status0 = 0x60,
76 .card_fw_status1 = 0x61,
77 .card_rx_len = 0x62,
78 .card_rx_unit = 0x63,
79 .io_port_0 = 0x78,
80 .io_port_1 = 0x79,
81 .io_port_2 = 0x7a,
84 static const struct btmrvl_sdio_device btmrvl_sdio_sd6888 = {
85 .helper = "sd8688_helper.bin",
86 .firmware = "sd8688.bin",
87 .reg = &btmrvl_reg_8688,
88 .sd_blksz_fw_dl = 64,
91 static const struct btmrvl_sdio_device btmrvl_sdio_sd8787 = {
92 .helper = NULL,
93 .firmware = "mrvl/sd8787_uapsta.bin",
94 .reg = &btmrvl_reg_8787,
95 .sd_blksz_fw_dl = 256,
98 static const struct sdio_device_id btmrvl_sdio_ids[] = {
99 /* Marvell SD8688 Bluetooth device */
100 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x9105),
101 .driver_data = (unsigned long) &btmrvl_sdio_sd6888 },
102 /* Marvell SD8787 Bluetooth device */
103 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x911A),
104 .driver_data = (unsigned long) &btmrvl_sdio_sd8787 },
106 { } /* Terminating entry */
109 MODULE_DEVICE_TABLE(sdio, btmrvl_sdio_ids);
111 static int btmrvl_sdio_get_rx_unit(struct btmrvl_sdio_card *card)
113 u8 reg;
114 int ret;
116 reg = sdio_readb(card->func, card->reg->card_rx_unit, &ret);
117 if (!ret)
118 card->rx_unit = reg;
120 return ret;
123 static int btmrvl_sdio_read_fw_status(struct btmrvl_sdio_card *card, u16 *dat)
125 u8 fws0, fws1;
126 int ret;
128 *dat = 0;
130 fws0 = sdio_readb(card->func, card->reg->card_fw_status0, &ret);
131 if (ret)
132 return -EIO;
134 fws1 = sdio_readb(card->func, card->reg->card_fw_status1, &ret);
135 if (ret)
136 return -EIO;
138 *dat = (((u16) fws1) << 8) | fws0;
140 return 0;
143 static int btmrvl_sdio_read_rx_len(struct btmrvl_sdio_card *card, u16 *dat)
145 u8 reg;
146 int ret;
148 reg = sdio_readb(card->func, card->reg->card_rx_len, &ret);
149 if (!ret)
150 *dat = (u16) reg << card->rx_unit;
152 return ret;
155 static int btmrvl_sdio_enable_host_int_mask(struct btmrvl_sdio_card *card,
156 u8 mask)
158 int ret;
160 sdio_writeb(card->func, mask, card->reg->host_int_mask, &ret);
161 if (ret) {
162 BT_ERR("Unable to enable the host interrupt!");
163 ret = -EIO;
166 return ret;
169 static int btmrvl_sdio_disable_host_int_mask(struct btmrvl_sdio_card *card,
170 u8 mask)
172 u8 host_int_mask;
173 int ret;
175 host_int_mask = sdio_readb(card->func, card->reg->host_int_mask, &ret);
176 if (ret)
177 return -EIO;
179 host_int_mask &= ~mask;
181 sdio_writeb(card->func, host_int_mask, card->reg->host_int_mask, &ret);
182 if (ret < 0) {
183 BT_ERR("Unable to disable the host interrupt!");
184 return -EIO;
187 return 0;
190 static int btmrvl_sdio_poll_card_status(struct btmrvl_sdio_card *card, u8 bits)
192 unsigned int tries;
193 u8 status;
194 int ret;
196 for (tries = 0; tries < MAX_POLL_TRIES * 1000; tries++) {
197 status = sdio_readb(card->func, card->reg->card_status, &ret);
198 if (ret)
199 goto failed;
200 if ((status & bits) == bits)
201 return ret;
203 udelay(1);
206 ret = -ETIMEDOUT;
208 failed:
209 BT_ERR("FAILED! ret=%d", ret);
211 return ret;
214 static int btmrvl_sdio_verify_fw_download(struct btmrvl_sdio_card *card,
215 int pollnum)
217 int ret = -ETIMEDOUT;
218 u16 firmwarestat;
219 unsigned int tries;
221 /* Wait for firmware to become ready */
222 for (tries = 0; tries < pollnum; tries++) {
223 if (btmrvl_sdio_read_fw_status(card, &firmwarestat) < 0)
224 continue;
226 if (firmwarestat == FIRMWARE_READY) {
227 ret = 0;
228 break;
229 } else {
230 msleep(10);
234 return ret;
237 static int btmrvl_sdio_download_helper(struct btmrvl_sdio_card *card)
239 const struct firmware *fw_helper = NULL;
240 const u8 *helper = NULL;
241 int ret;
242 void *tmphlprbuf = NULL;
243 int tmphlprbufsz, hlprblknow, helperlen;
244 u8 *helperbuf;
245 u32 tx_len;
247 ret = request_firmware(&fw_helper, card->helper,
248 &card->func->dev);
249 if ((ret < 0) || !fw_helper) {
250 BT_ERR("request_firmware(helper) failed, error code = %d",
251 ret);
252 ret = -ENOENT;
253 goto done;
256 helper = fw_helper->data;
257 helperlen = fw_helper->size;
259 BT_DBG("Downloading helper image (%d bytes), block size %d bytes",
260 helperlen, SDIO_BLOCK_SIZE);
262 tmphlprbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
264 tmphlprbuf = kzalloc(tmphlprbufsz, GFP_KERNEL);
265 if (!tmphlprbuf) {
266 BT_ERR("Unable to allocate buffer for helper."
267 " Terminating download");
268 ret = -ENOMEM;
269 goto done;
272 helperbuf = (u8 *) ALIGN_ADDR(tmphlprbuf, BTSDIO_DMA_ALIGN);
274 /* Perform helper data transfer */
275 tx_len = (FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE)
276 - SDIO_HEADER_LEN;
277 hlprblknow = 0;
279 do {
280 ret = btmrvl_sdio_poll_card_status(card,
281 CARD_IO_READY | DN_LD_CARD_RDY);
282 if (ret < 0) {
283 BT_ERR("Helper download poll status timeout @ %d",
284 hlprblknow);
285 goto done;
288 /* Check if there is more data? */
289 if (hlprblknow >= helperlen)
290 break;
292 if (helperlen - hlprblknow < tx_len)
293 tx_len = helperlen - hlprblknow;
295 /* Little-endian */
296 helperbuf[0] = ((tx_len & 0x000000ff) >> 0);
297 helperbuf[1] = ((tx_len & 0x0000ff00) >> 8);
298 helperbuf[2] = ((tx_len & 0x00ff0000) >> 16);
299 helperbuf[3] = ((tx_len & 0xff000000) >> 24);
301 memcpy(&helperbuf[SDIO_HEADER_LEN], &helper[hlprblknow],
302 tx_len);
304 /* Now send the data */
305 ret = sdio_writesb(card->func, card->ioport, helperbuf,
306 FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE);
307 if (ret < 0) {
308 BT_ERR("IO error during helper download @ %d",
309 hlprblknow);
310 goto done;
313 hlprblknow += tx_len;
314 } while (true);
316 BT_DBG("Transferring helper image EOF block");
318 memset(helperbuf, 0x0, SDIO_BLOCK_SIZE);
320 ret = sdio_writesb(card->func, card->ioport, helperbuf,
321 SDIO_BLOCK_SIZE);
322 if (ret < 0) {
323 BT_ERR("IO error in writing helper image EOF block");
324 goto done;
327 ret = 0;
329 done:
330 kfree(tmphlprbuf);
331 if (fw_helper)
332 release_firmware(fw_helper);
334 return ret;
337 static int btmrvl_sdio_download_fw_w_helper(struct btmrvl_sdio_card *card)
339 const struct firmware *fw_firmware = NULL;
340 const u8 *firmware = NULL;
341 int firmwarelen, tmpfwbufsz, ret;
342 unsigned int tries, offset;
343 u8 base0, base1;
344 void *tmpfwbuf = NULL;
345 u8 *fwbuf;
346 u16 len, blksz_dl = card->sd_blksz_fw_dl;
347 int txlen = 0, tx_blocks = 0, count = 0;
349 ret = request_firmware(&fw_firmware, card->firmware,
350 &card->func->dev);
351 if ((ret < 0) || !fw_firmware) {
352 BT_ERR("request_firmware(firmware) failed, error code = %d",
353 ret);
354 ret = -ENOENT;
355 goto done;
358 firmware = fw_firmware->data;
359 firmwarelen = fw_firmware->size;
361 BT_DBG("Downloading FW image (%d bytes)", firmwarelen);
363 tmpfwbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
364 tmpfwbuf = kzalloc(tmpfwbufsz, GFP_KERNEL);
365 if (!tmpfwbuf) {
366 BT_ERR("Unable to allocate buffer for firmware."
367 " Terminating download");
368 ret = -ENOMEM;
369 goto done;
372 /* Ensure aligned firmware buffer */
373 fwbuf = (u8 *) ALIGN_ADDR(tmpfwbuf, BTSDIO_DMA_ALIGN);
375 /* Perform firmware data transfer */
376 offset = 0;
377 do {
378 ret = btmrvl_sdio_poll_card_status(card,
379 CARD_IO_READY | DN_LD_CARD_RDY);
380 if (ret < 0) {
381 BT_ERR("FW download with helper poll status"
382 " timeout @ %d", offset);
383 goto done;
386 /* Check if there is more data ? */
387 if (offset >= firmwarelen)
388 break;
390 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
391 base0 = sdio_readb(card->func,
392 card->reg->sq_read_base_addr_a0, &ret);
393 if (ret) {
394 BT_ERR("BASE0 register read failed:"
395 " base0 = 0x%04X(%d)."
396 " Terminating download",
397 base0, base0);
398 ret = -EIO;
399 goto done;
401 base1 = sdio_readb(card->func,
402 card->reg->sq_read_base_addr_a1, &ret);
403 if (ret) {
404 BT_ERR("BASE1 register read failed:"
405 " base1 = 0x%04X(%d)."
406 " Terminating download",
407 base1, base1);
408 ret = -EIO;
409 goto done;
412 len = (((u16) base1) << 8) | base0;
413 if (len)
414 break;
416 udelay(10);
419 if (!len)
420 break;
421 else if (len > BTM_UPLD_SIZE) {
422 BT_ERR("FW download failure @%d, invalid length %d",
423 offset, len);
424 ret = -EINVAL;
425 goto done;
428 txlen = len;
430 if (len & BIT(0)) {
431 count++;
432 if (count > MAX_WRITE_IOMEM_RETRY) {
433 BT_ERR("FW download failure @%d, "
434 "over max retry count", offset);
435 ret = -EIO;
436 goto done;
438 BT_ERR("FW CRC error indicated by the helper: "
439 "len = 0x%04X, txlen = %d", len, txlen);
440 len &= ~BIT(0);
441 /* Set txlen to 0 so as to resend from same offset */
442 txlen = 0;
443 } else {
444 count = 0;
446 /* Last block ? */
447 if (firmwarelen - offset < txlen)
448 txlen = firmwarelen - offset;
450 tx_blocks = (txlen + blksz_dl - 1) / blksz_dl;
452 memcpy(fwbuf, &firmware[offset], txlen);
455 ret = sdio_writesb(card->func, card->ioport, fwbuf,
456 tx_blocks * blksz_dl);
458 if (ret < 0) {
459 BT_ERR("FW download, writesb(%d) failed @%d",
460 count, offset);
461 sdio_writeb(card->func, HOST_CMD53_FIN,
462 card->reg->cfg, &ret);
463 if (ret)
464 BT_ERR("writeb failed (CFG)");
467 offset += txlen;
468 } while (true);
470 BT_DBG("FW download over, size %d bytes", offset);
472 ret = 0;
474 done:
475 kfree(tmpfwbuf);
477 if (fw_firmware)
478 release_firmware(fw_firmware);
480 return ret;
483 static int btmrvl_sdio_card_to_host(struct btmrvl_private *priv)
485 u16 buf_len = 0;
486 int ret, buf_block_len, blksz;
487 struct sk_buff *skb = NULL;
488 u32 type;
489 u8 *payload = NULL;
490 struct hci_dev *hdev = priv->btmrvl_dev.hcidev;
491 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
493 if (!card || !card->func) {
494 BT_ERR("card or function is NULL!");
495 ret = -EINVAL;
496 goto exit;
499 /* Read the length of data to be transferred */
500 ret = btmrvl_sdio_read_rx_len(card, &buf_len);
501 if (ret < 0) {
502 BT_ERR("read rx_len failed");
503 ret = -EIO;
504 goto exit;
507 blksz = SDIO_BLOCK_SIZE;
508 buf_block_len = (buf_len + blksz - 1) / blksz;
510 if (buf_len <= SDIO_HEADER_LEN
511 || (buf_block_len * blksz) > ALLOC_BUF_SIZE) {
512 BT_ERR("invalid packet length: %d", buf_len);
513 ret = -EINVAL;
514 goto exit;
517 /* Allocate buffer */
518 skb = bt_skb_alloc(buf_block_len * blksz + BTSDIO_DMA_ALIGN,
519 GFP_ATOMIC);
520 if (skb == NULL) {
521 BT_ERR("No free skb");
522 goto exit;
525 if ((unsigned long) skb->data & (BTSDIO_DMA_ALIGN - 1)) {
526 skb_put(skb, (unsigned long) skb->data &
527 (BTSDIO_DMA_ALIGN - 1));
528 skb_pull(skb, (unsigned long) skb->data &
529 (BTSDIO_DMA_ALIGN - 1));
532 payload = skb->data;
534 ret = sdio_readsb(card->func, payload, card->ioport,
535 buf_block_len * blksz);
536 if (ret < 0) {
537 BT_ERR("readsb failed: %d", ret);
538 ret = -EIO;
539 goto exit;
542 /* This is SDIO specific header length: byte[2][1][0], type: byte[3]
543 * (HCI_COMMAND = 1, ACL_DATA = 2, SCO_DATA = 3, 0xFE = Vendor)
546 buf_len = payload[0];
547 buf_len |= (u16) payload[1] << 8;
548 type = payload[3];
550 switch (type) {
551 case HCI_ACLDATA_PKT:
552 case HCI_SCODATA_PKT:
553 case HCI_EVENT_PKT:
554 bt_cb(skb)->pkt_type = type;
555 skb->dev = (void *)hdev;
556 skb_put(skb, buf_len);
557 skb_pull(skb, SDIO_HEADER_LEN);
559 if (type == HCI_EVENT_PKT)
560 btmrvl_check_evtpkt(priv, skb);
562 hci_recv_frame(skb);
563 hdev->stat.byte_rx += buf_len;
564 break;
566 case MRVL_VENDOR_PKT:
567 bt_cb(skb)->pkt_type = HCI_VENDOR_PKT;
568 skb->dev = (void *)hdev;
569 skb_put(skb, buf_len);
570 skb_pull(skb, SDIO_HEADER_LEN);
572 if (btmrvl_process_event(priv, skb))
573 hci_recv_frame(skb);
575 hdev->stat.byte_rx += buf_len;
576 break;
578 default:
579 BT_ERR("Unknown packet type:%d", type);
580 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, payload,
581 blksz * buf_block_len);
583 kfree_skb(skb);
584 skb = NULL;
585 break;
588 exit:
589 if (ret) {
590 hdev->stat.err_rx++;
591 if (skb)
592 kfree_skb(skb);
595 return ret;
598 static int btmrvl_sdio_process_int_status(struct btmrvl_private *priv)
600 ulong flags;
601 u8 ireg;
602 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
604 spin_lock_irqsave(&priv->driver_lock, flags);
605 ireg = sdio_ireg;
606 sdio_ireg = 0;
607 spin_unlock_irqrestore(&priv->driver_lock, flags);
609 sdio_claim_host(card->func);
610 if (ireg & DN_LD_HOST_INT_STATUS) {
611 if (priv->btmrvl_dev.tx_dnld_rdy)
612 BT_DBG("tx_done already received: "
613 " int_status=0x%x", ireg);
614 else
615 priv->btmrvl_dev.tx_dnld_rdy = true;
618 if (ireg & UP_LD_HOST_INT_STATUS)
619 btmrvl_sdio_card_to_host(priv);
621 sdio_release_host(card->func);
623 return 0;
626 static void btmrvl_sdio_interrupt(struct sdio_func *func)
628 struct btmrvl_private *priv;
629 struct btmrvl_sdio_card *card;
630 ulong flags;
631 u8 ireg = 0;
632 int ret;
634 card = sdio_get_drvdata(func);
635 if (!card || !card->priv) {
636 BT_ERR("sbi_interrupt(%p) card or priv is "
637 "NULL, card=%p\n", func, card);
638 return;
641 priv = card->priv;
643 ireg = sdio_readb(card->func, card->reg->host_intstatus, &ret);
644 if (ret) {
645 BT_ERR("sdio_readb: read int status register failed");
646 return;
649 if (ireg != 0) {
651 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
652 * Clear the interrupt status register and re-enable the
653 * interrupt.
655 BT_DBG("ireg = 0x%x", ireg);
657 sdio_writeb(card->func, ~(ireg) & (DN_LD_HOST_INT_STATUS |
658 UP_LD_HOST_INT_STATUS),
659 card->reg->host_intstatus, &ret);
660 if (ret) {
661 BT_ERR("sdio_writeb: clear int status register failed");
662 return;
666 spin_lock_irqsave(&priv->driver_lock, flags);
667 sdio_ireg |= ireg;
668 spin_unlock_irqrestore(&priv->driver_lock, flags);
670 btmrvl_interrupt(priv);
673 static int btmrvl_sdio_register_dev(struct btmrvl_sdio_card *card)
675 struct sdio_func *func;
676 u8 reg;
677 int ret = 0;
679 if (!card || !card->func) {
680 BT_ERR("Error: card or function is NULL!");
681 ret = -EINVAL;
682 goto failed;
685 func = card->func;
687 sdio_claim_host(func);
689 ret = sdio_enable_func(func);
690 if (ret) {
691 BT_ERR("sdio_enable_func() failed: ret=%d", ret);
692 ret = -EIO;
693 goto release_host;
696 ret = sdio_claim_irq(func, btmrvl_sdio_interrupt);
697 if (ret) {
698 BT_ERR("sdio_claim_irq failed: ret=%d", ret);
699 ret = -EIO;
700 goto disable_func;
703 ret = sdio_set_block_size(card->func, SDIO_BLOCK_SIZE);
704 if (ret) {
705 BT_ERR("cannot set SDIO block size");
706 ret = -EIO;
707 goto release_irq;
710 reg = sdio_readb(func, card->reg->io_port_0, &ret);
711 if (ret < 0) {
712 ret = -EIO;
713 goto release_irq;
716 card->ioport = reg;
718 reg = sdio_readb(func, card->reg->io_port_1, &ret);
719 if (ret < 0) {
720 ret = -EIO;
721 goto release_irq;
724 card->ioport |= (reg << 8);
726 reg = sdio_readb(func, card->reg->io_port_2, &ret);
727 if (ret < 0) {
728 ret = -EIO;
729 goto release_irq;
732 card->ioport |= (reg << 16);
734 BT_DBG("SDIO FUNC%d IO port: 0x%x", func->num, card->ioport);
736 sdio_set_drvdata(func, card);
738 sdio_release_host(func);
740 return 0;
742 release_irq:
743 sdio_release_irq(func);
745 disable_func:
746 sdio_disable_func(func);
748 release_host:
749 sdio_release_host(func);
751 failed:
752 return ret;
755 static int btmrvl_sdio_unregister_dev(struct btmrvl_sdio_card *card)
757 if (card && card->func) {
758 sdio_claim_host(card->func);
759 sdio_release_irq(card->func);
760 sdio_disable_func(card->func);
761 sdio_release_host(card->func);
762 sdio_set_drvdata(card->func, NULL);
765 return 0;
768 static int btmrvl_sdio_enable_host_int(struct btmrvl_sdio_card *card)
770 int ret;
772 if (!card || !card->func)
773 return -EINVAL;
775 sdio_claim_host(card->func);
777 ret = btmrvl_sdio_enable_host_int_mask(card, HIM_ENABLE);
779 btmrvl_sdio_get_rx_unit(card);
781 sdio_release_host(card->func);
783 return ret;
786 static int btmrvl_sdio_disable_host_int(struct btmrvl_sdio_card *card)
788 int ret;
790 if (!card || !card->func)
791 return -EINVAL;
793 sdio_claim_host(card->func);
795 ret = btmrvl_sdio_disable_host_int_mask(card, HIM_DISABLE);
797 sdio_release_host(card->func);
799 return ret;
802 static int btmrvl_sdio_host_to_card(struct btmrvl_private *priv,
803 u8 *payload, u16 nb)
805 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
806 int ret = 0;
807 int buf_block_len;
808 int blksz;
809 int i = 0;
810 u8 *buf = NULL;
811 void *tmpbuf = NULL;
812 int tmpbufsz;
814 if (!card || !card->func) {
815 BT_ERR("card or function is NULL!");
816 return -EINVAL;
819 buf = payload;
820 if ((unsigned long) payload & (BTSDIO_DMA_ALIGN - 1)) {
821 tmpbufsz = ALIGN_SZ(nb, BTSDIO_DMA_ALIGN);
822 tmpbuf = kzalloc(tmpbufsz, GFP_KERNEL);
823 if (!tmpbuf)
824 return -ENOMEM;
825 buf = (u8 *) ALIGN_ADDR(tmpbuf, BTSDIO_DMA_ALIGN);
826 memcpy(buf, payload, nb);
829 blksz = SDIO_BLOCK_SIZE;
830 buf_block_len = (nb + blksz - 1) / blksz;
832 sdio_claim_host(card->func);
834 do {
835 /* Transfer data to card */
836 ret = sdio_writesb(card->func, card->ioport, buf,
837 buf_block_len * blksz);
838 if (ret < 0) {
839 i++;
840 BT_ERR("i=%d writesb failed: %d", i, ret);
841 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
842 payload, nb);
843 ret = -EIO;
844 if (i > MAX_WRITE_IOMEM_RETRY)
845 goto exit;
847 } while (ret);
849 priv->btmrvl_dev.tx_dnld_rdy = false;
851 exit:
852 sdio_release_host(card->func);
853 kfree(tmpbuf);
855 return ret;
858 static int btmrvl_sdio_download_fw(struct btmrvl_sdio_card *card)
860 int ret = 0;
861 u8 fws0;
862 int pollnum = MAX_POLL_TRIES;
864 if (!card || !card->func) {
865 BT_ERR("card or function is NULL!");
866 return -EINVAL;
868 sdio_claim_host(card->func);
870 if (!btmrvl_sdio_verify_fw_download(card, 1)) {
871 BT_DBG("Firmware already downloaded!");
872 goto done;
875 /* Check if other function driver is downloading the firmware */
876 fws0 = sdio_readb(card->func, card->reg->card_fw_status0, &ret);
877 if (ret) {
878 BT_ERR("Failed to read FW downloading status!");
879 ret = -EIO;
880 goto done;
882 if (fws0) {
883 BT_DBG("BT not the winner (%#x). Skip FW downloading", fws0);
885 /* Give other function more time to download the firmware */
886 pollnum *= 10;
887 } else {
888 if (card->helper) {
889 ret = btmrvl_sdio_download_helper(card);
890 if (ret) {
891 BT_ERR("Failed to download helper!");
892 ret = -EIO;
893 goto done;
897 if (btmrvl_sdio_download_fw_w_helper(card)) {
898 BT_ERR("Failed to download firmware!");
899 ret = -EIO;
900 goto done;
904 if (btmrvl_sdio_verify_fw_download(card, pollnum)) {
905 BT_ERR("FW failed to be active in time!");
906 ret = -ETIMEDOUT;
907 goto done;
910 done:
911 sdio_release_host(card->func);
913 return ret;
916 static int btmrvl_sdio_wakeup_fw(struct btmrvl_private *priv)
918 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
919 int ret = 0;
921 if (!card || !card->func) {
922 BT_ERR("card or function is NULL!");
923 return -EINVAL;
926 sdio_claim_host(card->func);
928 sdio_writeb(card->func, HOST_POWER_UP, card->reg->cfg, &ret);
930 sdio_release_host(card->func);
932 BT_DBG("wake up firmware");
934 return ret;
937 static int btmrvl_sdio_probe(struct sdio_func *func,
938 const struct sdio_device_id *id)
940 int ret = 0;
941 struct btmrvl_private *priv = NULL;
942 struct btmrvl_sdio_card *card = NULL;
944 BT_INFO("vendor=0x%x, device=0x%x, class=%d, fn=%d",
945 id->vendor, id->device, id->class, func->num);
947 card = kzalloc(sizeof(*card), GFP_KERNEL);
948 if (!card) {
949 ret = -ENOMEM;
950 goto done;
953 card->func = func;
955 if (id->driver_data) {
956 struct btmrvl_sdio_device *data = (void *) id->driver_data;
957 card->helper = data->helper;
958 card->firmware = data->firmware;
959 card->reg = data->reg;
960 card->sd_blksz_fw_dl = data->sd_blksz_fw_dl;
963 if (btmrvl_sdio_register_dev(card) < 0) {
964 BT_ERR("Failed to register BT device!");
965 ret = -ENODEV;
966 goto free_card;
969 /* Disable the interrupts on the card */
970 btmrvl_sdio_disable_host_int(card);
972 if (btmrvl_sdio_download_fw(card)) {
973 BT_ERR("Downloading firmware failed!");
974 ret = -ENODEV;
975 goto unreg_dev;
978 msleep(100);
980 btmrvl_sdio_enable_host_int(card);
982 priv = btmrvl_add_card(card);
983 if (!priv) {
984 BT_ERR("Initializing card failed!");
985 ret = -ENODEV;
986 goto disable_host_int;
989 card->priv = priv;
991 /* Initialize the interface specific function pointers */
992 priv->hw_host_to_card = btmrvl_sdio_host_to_card;
993 priv->hw_wakeup_firmware = btmrvl_sdio_wakeup_fw;
994 priv->hw_process_int_status = btmrvl_sdio_process_int_status;
996 if (btmrvl_register_hdev(priv)) {
997 BT_ERR("Register hdev failed!");
998 ret = -ENODEV;
999 goto disable_host_int;
1002 priv->btmrvl_dev.psmode = 1;
1003 btmrvl_enable_ps(priv);
1005 return 0;
1007 disable_host_int:
1008 btmrvl_sdio_disable_host_int(card);
1009 unreg_dev:
1010 btmrvl_sdio_unregister_dev(card);
1011 free_card:
1012 kfree(card);
1013 done:
1014 return ret;
1017 static void btmrvl_sdio_remove(struct sdio_func *func)
1019 struct btmrvl_sdio_card *card;
1021 if (func) {
1022 card = sdio_get_drvdata(func);
1023 if (card) {
1024 /* Send SHUTDOWN command & disable interrupt
1025 * if user removes the module.
1027 if (user_rmmod) {
1028 btmrvl_send_module_cfg_cmd(card->priv,
1029 MODULE_SHUTDOWN_REQ);
1030 btmrvl_sdio_disable_host_int(card);
1032 BT_DBG("unregester dev");
1033 btmrvl_sdio_unregister_dev(card);
1034 btmrvl_remove_card(card->priv);
1035 kfree(card);
1040 static struct sdio_driver bt_mrvl_sdio = {
1041 .name = "btmrvl_sdio",
1042 .id_table = btmrvl_sdio_ids,
1043 .probe = btmrvl_sdio_probe,
1044 .remove = btmrvl_sdio_remove,
1047 static int __init btmrvl_sdio_init_module(void)
1049 if (sdio_register_driver(&bt_mrvl_sdio) != 0) {
1050 BT_ERR("SDIO Driver Registration Failed");
1051 return -ENODEV;
1054 /* Clear the flag in case user removes the card. */
1055 user_rmmod = 0;
1057 return 0;
1060 static void __exit btmrvl_sdio_exit_module(void)
1062 /* Set the flag as user is removing this module. */
1063 user_rmmod = 1;
1065 sdio_unregister_driver(&bt_mrvl_sdio);
1068 module_init(btmrvl_sdio_init_module);
1069 module_exit(btmrvl_sdio_exit_module);
1071 MODULE_AUTHOR("Marvell International Ltd.");
1072 MODULE_DESCRIPTION("Marvell BT-over-SDIO driver ver " VERSION);
1073 MODULE_VERSION(VERSION);
1074 MODULE_LICENSE("GPL v2");
1075 MODULE_FIRMWARE("sd8688_helper.bin");
1076 MODULE_FIRMWARE("sd8688.bin");
1077 MODULE_FIRMWARE("mrvl/sd8787_uapsta.bin");