crypto: sha512 - make it work, undo percpu message schedule
[linux-stable.git] / drivers / bluetooth / btmrvl_sdio.c
blob9ef48167e2cf8ba64c790d4846a2d297e84c6fd0
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>
26 #include <linux/module.h>
28 #include <net/bluetooth/bluetooth.h>
29 #include <net/bluetooth/hci_core.h>
31 #include "btmrvl_drv.h"
32 #include "btmrvl_sdio.h"
34 #define VERSION "1.0"
36 /* The btmrvl_sdio_remove() callback function is called
37 * when user removes this module from kernel space or ejects
38 * the card from the slot. The driver handles these 2 cases
39 * differently.
40 * If the user is removing the module, a MODULE_SHUTDOWN_REQ
41 * command is sent to firmware and interrupt will be disabled.
42 * If the card is removed, there is no need to send command
43 * or disable interrupt.
45 * The variable 'user_rmmod' is used to distinguish these two
46 * scenarios. This flag is initialized as FALSE in case the card
47 * is removed, and will be set to TRUE for module removal when
48 * module_exit function is called.
50 static u8 user_rmmod;
51 static u8 sdio_ireg;
53 static const struct btmrvl_sdio_card_reg btmrvl_reg_8688 = {
54 .cfg = 0x03,
55 .host_int_mask = 0x04,
56 .host_intstatus = 0x05,
57 .card_status = 0x20,
58 .sq_read_base_addr_a0 = 0x10,
59 .sq_read_base_addr_a1 = 0x11,
60 .card_fw_status0 = 0x40,
61 .card_fw_status1 = 0x41,
62 .card_rx_len = 0x42,
63 .card_rx_unit = 0x43,
64 .io_port_0 = 0x00,
65 .io_port_1 = 0x01,
66 .io_port_2 = 0x02,
68 static const struct btmrvl_sdio_card_reg btmrvl_reg_8787 = {
69 .cfg = 0x00,
70 .host_int_mask = 0x02,
71 .host_intstatus = 0x03,
72 .card_status = 0x30,
73 .sq_read_base_addr_a0 = 0x40,
74 .sq_read_base_addr_a1 = 0x41,
75 .card_revision = 0x5c,
76 .card_fw_status0 = 0x60,
77 .card_fw_status1 = 0x61,
78 .card_rx_len = 0x62,
79 .card_rx_unit = 0x63,
80 .io_port_0 = 0x78,
81 .io_port_1 = 0x79,
82 .io_port_2 = 0x7a,
85 static const struct btmrvl_sdio_device btmrvl_sdio_sd8688 = {
86 .helper = "sd8688_helper.bin",
87 .firmware = "sd8688.bin",
88 .reg = &btmrvl_reg_8688,
89 .sd_blksz_fw_dl = 64,
92 static const struct btmrvl_sdio_device btmrvl_sdio_sd8787 = {
93 .helper = NULL,
94 .firmware = "mrvl/sd8787_uapsta.bin",
95 .reg = &btmrvl_reg_8787,
96 .sd_blksz_fw_dl = 256,
99 static const struct sdio_device_id btmrvl_sdio_ids[] = {
100 /* Marvell SD8688 Bluetooth device */
101 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x9105),
102 .driver_data = (unsigned long) &btmrvl_sdio_sd8688 },
103 /* Marvell SD8787 Bluetooth device */
104 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x911A),
105 .driver_data = (unsigned long) &btmrvl_sdio_sd8787 },
107 { } /* Terminating entry */
110 MODULE_DEVICE_TABLE(sdio, btmrvl_sdio_ids);
112 static int btmrvl_sdio_get_rx_unit(struct btmrvl_sdio_card *card)
114 u8 reg;
115 int ret;
117 reg = sdio_readb(card->func, card->reg->card_rx_unit, &ret);
118 if (!ret)
119 card->rx_unit = reg;
121 return ret;
124 static int btmrvl_sdio_read_fw_status(struct btmrvl_sdio_card *card, u16 *dat)
126 u8 fws0, fws1;
127 int ret;
129 *dat = 0;
131 fws0 = sdio_readb(card->func, card->reg->card_fw_status0, &ret);
132 if (ret)
133 return -EIO;
135 fws1 = sdio_readb(card->func, card->reg->card_fw_status1, &ret);
136 if (ret)
137 return -EIO;
139 *dat = (((u16) fws1) << 8) | fws0;
141 return 0;
144 static int btmrvl_sdio_read_rx_len(struct btmrvl_sdio_card *card, u16 *dat)
146 u8 reg;
147 int ret;
149 reg = sdio_readb(card->func, card->reg->card_rx_len, &ret);
150 if (!ret)
151 *dat = (u16) reg << card->rx_unit;
153 return ret;
156 static int btmrvl_sdio_enable_host_int_mask(struct btmrvl_sdio_card *card,
157 u8 mask)
159 int ret;
161 sdio_writeb(card->func, mask, card->reg->host_int_mask, &ret);
162 if (ret) {
163 BT_ERR("Unable to enable the host interrupt!");
164 ret = -EIO;
167 return ret;
170 static int btmrvl_sdio_disable_host_int_mask(struct btmrvl_sdio_card *card,
171 u8 mask)
173 u8 host_int_mask;
174 int ret;
176 host_int_mask = sdio_readb(card->func, card->reg->host_int_mask, &ret);
177 if (ret)
178 return -EIO;
180 host_int_mask &= ~mask;
182 sdio_writeb(card->func, host_int_mask, card->reg->host_int_mask, &ret);
183 if (ret < 0) {
184 BT_ERR("Unable to disable the host interrupt!");
185 return -EIO;
188 return 0;
191 static int btmrvl_sdio_poll_card_status(struct btmrvl_sdio_card *card, u8 bits)
193 unsigned int tries;
194 u8 status;
195 int ret;
197 for (tries = 0; tries < MAX_POLL_TRIES * 1000; tries++) {
198 status = sdio_readb(card->func, card->reg->card_status, &ret);
199 if (ret)
200 goto failed;
201 if ((status & bits) == bits)
202 return ret;
204 udelay(1);
207 ret = -ETIMEDOUT;
209 failed:
210 BT_ERR("FAILED! ret=%d", ret);
212 return ret;
215 static int btmrvl_sdio_verify_fw_download(struct btmrvl_sdio_card *card,
216 int pollnum)
218 int ret = -ETIMEDOUT;
219 u16 firmwarestat;
220 unsigned int tries;
222 /* Wait for firmware to become ready */
223 for (tries = 0; tries < pollnum; tries++) {
224 if (btmrvl_sdio_read_fw_status(card, &firmwarestat) < 0)
225 continue;
227 if (firmwarestat == FIRMWARE_READY) {
228 ret = 0;
229 break;
230 } else {
231 msleep(10);
235 return ret;
238 static int btmrvl_sdio_download_helper(struct btmrvl_sdio_card *card)
240 const struct firmware *fw_helper = NULL;
241 const u8 *helper = NULL;
242 int ret;
243 void *tmphlprbuf = NULL;
244 int tmphlprbufsz, hlprblknow, helperlen;
245 u8 *helperbuf;
246 u32 tx_len;
248 ret = request_firmware(&fw_helper, card->helper,
249 &card->func->dev);
250 if ((ret < 0) || !fw_helper) {
251 BT_ERR("request_firmware(helper) failed, error code = %d",
252 ret);
253 ret = -ENOENT;
254 goto done;
257 helper = fw_helper->data;
258 helperlen = fw_helper->size;
260 BT_DBG("Downloading helper image (%d bytes), block size %d bytes",
261 helperlen, SDIO_BLOCK_SIZE);
263 tmphlprbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
265 tmphlprbuf = kzalloc(tmphlprbufsz, GFP_KERNEL);
266 if (!tmphlprbuf) {
267 BT_ERR("Unable to allocate buffer for helper."
268 " Terminating download");
269 ret = -ENOMEM;
270 goto done;
273 helperbuf = (u8 *) ALIGN_ADDR(tmphlprbuf, BTSDIO_DMA_ALIGN);
275 /* Perform helper data transfer */
276 tx_len = (FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE)
277 - SDIO_HEADER_LEN;
278 hlprblknow = 0;
280 do {
281 ret = btmrvl_sdio_poll_card_status(card,
282 CARD_IO_READY | DN_LD_CARD_RDY);
283 if (ret < 0) {
284 BT_ERR("Helper download poll status timeout @ %d",
285 hlprblknow);
286 goto done;
289 /* Check if there is more data? */
290 if (hlprblknow >= helperlen)
291 break;
293 if (helperlen - hlprblknow < tx_len)
294 tx_len = helperlen - hlprblknow;
296 /* Little-endian */
297 helperbuf[0] = ((tx_len & 0x000000ff) >> 0);
298 helperbuf[1] = ((tx_len & 0x0000ff00) >> 8);
299 helperbuf[2] = ((tx_len & 0x00ff0000) >> 16);
300 helperbuf[3] = ((tx_len & 0xff000000) >> 24);
302 memcpy(&helperbuf[SDIO_HEADER_LEN], &helper[hlprblknow],
303 tx_len);
305 /* Now send the data */
306 ret = sdio_writesb(card->func, card->ioport, helperbuf,
307 FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE);
308 if (ret < 0) {
309 BT_ERR("IO error during helper download @ %d",
310 hlprblknow);
311 goto done;
314 hlprblknow += tx_len;
315 } while (true);
317 BT_DBG("Transferring helper image EOF block");
319 memset(helperbuf, 0x0, SDIO_BLOCK_SIZE);
321 ret = sdio_writesb(card->func, card->ioport, helperbuf,
322 SDIO_BLOCK_SIZE);
323 if (ret < 0) {
324 BT_ERR("IO error in writing helper image EOF block");
325 goto done;
328 ret = 0;
330 done:
331 kfree(tmphlprbuf);
332 if (fw_helper)
333 release_firmware(fw_helper);
335 return ret;
338 static int btmrvl_sdio_download_fw_w_helper(struct btmrvl_sdio_card *card)
340 const struct firmware *fw_firmware = NULL;
341 const u8 *firmware = NULL;
342 int firmwarelen, tmpfwbufsz, ret;
343 unsigned int tries, offset;
344 u8 base0, base1;
345 void *tmpfwbuf = NULL;
346 u8 *fwbuf;
347 u16 len, blksz_dl = card->sd_blksz_fw_dl;
348 int txlen = 0, tx_blocks = 0, count = 0;
350 ret = request_firmware(&fw_firmware, card->firmware,
351 &card->func->dev);
352 if ((ret < 0) || !fw_firmware) {
353 BT_ERR("request_firmware(firmware) failed, error code = %d",
354 ret);
355 ret = -ENOENT;
356 goto done;
359 firmware = fw_firmware->data;
360 firmwarelen = fw_firmware->size;
362 BT_DBG("Downloading FW image (%d bytes)", firmwarelen);
364 tmpfwbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
365 tmpfwbuf = kzalloc(tmpfwbufsz, GFP_KERNEL);
366 if (!tmpfwbuf) {
367 BT_ERR("Unable to allocate buffer for firmware."
368 " Terminating download");
369 ret = -ENOMEM;
370 goto done;
373 /* Ensure aligned firmware buffer */
374 fwbuf = (u8 *) ALIGN_ADDR(tmpfwbuf, BTSDIO_DMA_ALIGN);
376 /* Perform firmware data transfer */
377 offset = 0;
378 do {
379 ret = btmrvl_sdio_poll_card_status(card,
380 CARD_IO_READY | DN_LD_CARD_RDY);
381 if (ret < 0) {
382 BT_ERR("FW download with helper poll status"
383 " timeout @ %d", offset);
384 goto done;
387 /* Check if there is more data ? */
388 if (offset >= firmwarelen)
389 break;
391 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
392 base0 = sdio_readb(card->func,
393 card->reg->sq_read_base_addr_a0, &ret);
394 if (ret) {
395 BT_ERR("BASE0 register read failed:"
396 " base0 = 0x%04X(%d)."
397 " Terminating download",
398 base0, base0);
399 ret = -EIO;
400 goto done;
402 base1 = sdio_readb(card->func,
403 card->reg->sq_read_base_addr_a1, &ret);
404 if (ret) {
405 BT_ERR("BASE1 register read failed:"
406 " base1 = 0x%04X(%d)."
407 " Terminating download",
408 base1, base1);
409 ret = -EIO;
410 goto done;
413 len = (((u16) base1) << 8) | base0;
414 if (len)
415 break;
417 udelay(10);
420 if (!len)
421 break;
422 else if (len > BTM_UPLD_SIZE) {
423 BT_ERR("FW download failure @%d, invalid length %d",
424 offset, len);
425 ret = -EINVAL;
426 goto done;
429 txlen = len;
431 if (len & BIT(0)) {
432 count++;
433 if (count > MAX_WRITE_IOMEM_RETRY) {
434 BT_ERR("FW download failure @%d, "
435 "over max retry count", offset);
436 ret = -EIO;
437 goto done;
439 BT_ERR("FW CRC error indicated by the helper: "
440 "len = 0x%04X, txlen = %d", len, txlen);
441 len &= ~BIT(0);
442 /* Set txlen to 0 so as to resend from same offset */
443 txlen = 0;
444 } else {
445 count = 0;
447 /* Last block ? */
448 if (firmwarelen - offset < txlen)
449 txlen = firmwarelen - offset;
451 tx_blocks = (txlen + blksz_dl - 1) / blksz_dl;
453 memcpy(fwbuf, &firmware[offset], txlen);
456 ret = sdio_writesb(card->func, card->ioport, fwbuf,
457 tx_blocks * blksz_dl);
459 if (ret < 0) {
460 BT_ERR("FW download, writesb(%d) failed @%d",
461 count, offset);
462 sdio_writeb(card->func, HOST_CMD53_FIN,
463 card->reg->cfg, &ret);
464 if (ret)
465 BT_ERR("writeb failed (CFG)");
468 offset += txlen;
469 } while (true);
471 BT_DBG("FW download over, size %d bytes", offset);
473 ret = 0;
475 done:
476 kfree(tmpfwbuf);
478 if (fw_firmware)
479 release_firmware(fw_firmware);
481 return ret;
484 static int btmrvl_sdio_card_to_host(struct btmrvl_private *priv)
486 u16 buf_len = 0;
487 int ret, buf_block_len, blksz;
488 struct sk_buff *skb = NULL;
489 u32 type;
490 u8 *payload = NULL;
491 struct hci_dev *hdev = priv->btmrvl_dev.hcidev;
492 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
494 if (!card || !card->func) {
495 BT_ERR("card or function is NULL!");
496 ret = -EINVAL;
497 goto exit;
500 /* Read the length of data to be transferred */
501 ret = btmrvl_sdio_read_rx_len(card, &buf_len);
502 if (ret < 0) {
503 BT_ERR("read rx_len failed");
504 ret = -EIO;
505 goto exit;
508 blksz = SDIO_BLOCK_SIZE;
509 buf_block_len = (buf_len + blksz - 1) / blksz;
511 if (buf_len <= SDIO_HEADER_LEN
512 || (buf_block_len * blksz) > ALLOC_BUF_SIZE) {
513 BT_ERR("invalid packet length: %d", buf_len);
514 ret = -EINVAL;
515 goto exit;
518 /* Allocate buffer */
519 skb = bt_skb_alloc(buf_block_len * blksz + BTSDIO_DMA_ALIGN,
520 GFP_ATOMIC);
521 if (skb == NULL) {
522 BT_ERR("No free skb");
523 goto exit;
526 if ((unsigned long) skb->data & (BTSDIO_DMA_ALIGN - 1)) {
527 skb_put(skb, (unsigned long) skb->data &
528 (BTSDIO_DMA_ALIGN - 1));
529 skb_pull(skb, (unsigned long) skb->data &
530 (BTSDIO_DMA_ALIGN - 1));
533 payload = skb->data;
535 ret = sdio_readsb(card->func, payload, card->ioport,
536 buf_block_len * blksz);
537 if (ret < 0) {
538 BT_ERR("readsb failed: %d", ret);
539 ret = -EIO;
540 goto exit;
543 /* This is SDIO specific header length: byte[2][1][0], type: byte[3]
544 * (HCI_COMMAND = 1, ACL_DATA = 2, SCO_DATA = 3, 0xFE = Vendor)
547 buf_len = payload[0];
548 buf_len |= (u16) payload[1] << 8;
549 type = payload[3];
551 switch (type) {
552 case HCI_ACLDATA_PKT:
553 case HCI_SCODATA_PKT:
554 case HCI_EVENT_PKT:
555 bt_cb(skb)->pkt_type = type;
556 skb->dev = (void *)hdev;
557 skb_put(skb, buf_len);
558 skb_pull(skb, SDIO_HEADER_LEN);
560 if (type == HCI_EVENT_PKT)
561 btmrvl_check_evtpkt(priv, skb);
563 hci_recv_frame(skb);
564 hdev->stat.byte_rx += buf_len;
565 break;
567 case MRVL_VENDOR_PKT:
568 bt_cb(skb)->pkt_type = HCI_VENDOR_PKT;
569 skb->dev = (void *)hdev;
570 skb_put(skb, buf_len);
571 skb_pull(skb, SDIO_HEADER_LEN);
573 if (btmrvl_process_event(priv, skb))
574 hci_recv_frame(skb);
576 hdev->stat.byte_rx += buf_len;
577 break;
579 default:
580 BT_ERR("Unknown packet type:%d", type);
581 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, payload,
582 blksz * buf_block_len);
584 kfree_skb(skb);
585 skb = NULL;
586 break;
589 exit:
590 if (ret) {
591 hdev->stat.err_rx++;
592 if (skb)
593 kfree_skb(skb);
596 return ret;
599 static int btmrvl_sdio_process_int_status(struct btmrvl_private *priv)
601 ulong flags;
602 u8 ireg;
603 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
605 spin_lock_irqsave(&priv->driver_lock, flags);
606 ireg = sdio_ireg;
607 sdio_ireg = 0;
608 spin_unlock_irqrestore(&priv->driver_lock, flags);
610 sdio_claim_host(card->func);
611 if (ireg & DN_LD_HOST_INT_STATUS) {
612 if (priv->btmrvl_dev.tx_dnld_rdy)
613 BT_DBG("tx_done already received: "
614 " int_status=0x%x", ireg);
615 else
616 priv->btmrvl_dev.tx_dnld_rdy = true;
619 if (ireg & UP_LD_HOST_INT_STATUS)
620 btmrvl_sdio_card_to_host(priv);
622 sdio_release_host(card->func);
624 return 0;
627 static void btmrvl_sdio_interrupt(struct sdio_func *func)
629 struct btmrvl_private *priv;
630 struct btmrvl_sdio_card *card;
631 ulong flags;
632 u8 ireg = 0;
633 int ret;
635 card = sdio_get_drvdata(func);
636 if (!card || !card->priv) {
637 BT_ERR("sbi_interrupt(%p) card or priv is "
638 "NULL, card=%p\n", func, card);
639 return;
642 priv = card->priv;
644 ireg = sdio_readb(card->func, card->reg->host_intstatus, &ret);
645 if (ret) {
646 BT_ERR("sdio_readb: read int status register failed");
647 return;
650 if (ireg != 0) {
652 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
653 * Clear the interrupt status register and re-enable the
654 * interrupt.
656 BT_DBG("ireg = 0x%x", ireg);
658 sdio_writeb(card->func, ~(ireg) & (DN_LD_HOST_INT_STATUS |
659 UP_LD_HOST_INT_STATUS),
660 card->reg->host_intstatus, &ret);
661 if (ret) {
662 BT_ERR("sdio_writeb: clear int status register failed");
663 return;
667 spin_lock_irqsave(&priv->driver_lock, flags);
668 sdio_ireg |= ireg;
669 spin_unlock_irqrestore(&priv->driver_lock, flags);
671 btmrvl_interrupt(priv);
674 static int btmrvl_sdio_register_dev(struct btmrvl_sdio_card *card)
676 struct sdio_func *func;
677 u8 reg;
678 int ret = 0;
680 if (!card || !card->func) {
681 BT_ERR("Error: card or function is NULL!");
682 ret = -EINVAL;
683 goto failed;
686 func = card->func;
688 sdio_claim_host(func);
690 ret = sdio_enable_func(func);
691 if (ret) {
692 BT_ERR("sdio_enable_func() failed: ret=%d", ret);
693 ret = -EIO;
694 goto release_host;
697 ret = sdio_claim_irq(func, btmrvl_sdio_interrupt);
698 if (ret) {
699 BT_ERR("sdio_claim_irq failed: ret=%d", ret);
700 ret = -EIO;
701 goto disable_func;
704 ret = sdio_set_block_size(card->func, SDIO_BLOCK_SIZE);
705 if (ret) {
706 BT_ERR("cannot set SDIO block size");
707 ret = -EIO;
708 goto release_irq;
711 reg = sdio_readb(func, card->reg->io_port_0, &ret);
712 if (ret < 0) {
713 ret = -EIO;
714 goto release_irq;
717 card->ioport = reg;
719 reg = sdio_readb(func, card->reg->io_port_1, &ret);
720 if (ret < 0) {
721 ret = -EIO;
722 goto release_irq;
725 card->ioport |= (reg << 8);
727 reg = sdio_readb(func, card->reg->io_port_2, &ret);
728 if (ret < 0) {
729 ret = -EIO;
730 goto release_irq;
733 card->ioport |= (reg << 16);
735 BT_DBG("SDIO FUNC%d IO port: 0x%x", func->num, card->ioport);
737 sdio_set_drvdata(func, card);
739 sdio_release_host(func);
741 return 0;
743 release_irq:
744 sdio_release_irq(func);
746 disable_func:
747 sdio_disable_func(func);
749 release_host:
750 sdio_release_host(func);
752 failed:
753 return ret;
756 static int btmrvl_sdio_unregister_dev(struct btmrvl_sdio_card *card)
758 if (card && card->func) {
759 sdio_claim_host(card->func);
760 sdio_release_irq(card->func);
761 sdio_disable_func(card->func);
762 sdio_release_host(card->func);
763 sdio_set_drvdata(card->func, NULL);
766 return 0;
769 static int btmrvl_sdio_enable_host_int(struct btmrvl_sdio_card *card)
771 int ret;
773 if (!card || !card->func)
774 return -EINVAL;
776 sdio_claim_host(card->func);
778 ret = btmrvl_sdio_enable_host_int_mask(card, HIM_ENABLE);
780 btmrvl_sdio_get_rx_unit(card);
782 sdio_release_host(card->func);
784 return ret;
787 static int btmrvl_sdio_disable_host_int(struct btmrvl_sdio_card *card)
789 int ret;
791 if (!card || !card->func)
792 return -EINVAL;
794 sdio_claim_host(card->func);
796 ret = btmrvl_sdio_disable_host_int_mask(card, HIM_DISABLE);
798 sdio_release_host(card->func);
800 return ret;
803 static int btmrvl_sdio_host_to_card(struct btmrvl_private *priv,
804 u8 *payload, u16 nb)
806 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
807 int ret = 0;
808 int buf_block_len;
809 int blksz;
810 int i = 0;
811 u8 *buf = NULL;
812 void *tmpbuf = NULL;
813 int tmpbufsz;
815 if (!card || !card->func) {
816 BT_ERR("card or function is NULL!");
817 return -EINVAL;
820 buf = payload;
821 if ((unsigned long) payload & (BTSDIO_DMA_ALIGN - 1)) {
822 tmpbufsz = ALIGN_SZ(nb, BTSDIO_DMA_ALIGN);
823 tmpbuf = kzalloc(tmpbufsz, GFP_KERNEL);
824 if (!tmpbuf)
825 return -ENOMEM;
826 buf = (u8 *) ALIGN_ADDR(tmpbuf, BTSDIO_DMA_ALIGN);
827 memcpy(buf, payload, nb);
830 blksz = SDIO_BLOCK_SIZE;
831 buf_block_len = (nb + blksz - 1) / blksz;
833 sdio_claim_host(card->func);
835 do {
836 /* Transfer data to card */
837 ret = sdio_writesb(card->func, card->ioport, buf,
838 buf_block_len * blksz);
839 if (ret < 0) {
840 i++;
841 BT_ERR("i=%d writesb failed: %d", i, ret);
842 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
843 payload, nb);
844 ret = -EIO;
845 if (i > MAX_WRITE_IOMEM_RETRY)
846 goto exit;
848 } while (ret);
850 priv->btmrvl_dev.tx_dnld_rdy = false;
852 exit:
853 sdio_release_host(card->func);
854 kfree(tmpbuf);
856 return ret;
859 static int btmrvl_sdio_download_fw(struct btmrvl_sdio_card *card)
861 int ret = 0;
862 u8 fws0;
863 int pollnum = MAX_POLL_TRIES;
865 if (!card || !card->func) {
866 BT_ERR("card or function is NULL!");
867 return -EINVAL;
869 sdio_claim_host(card->func);
871 if (!btmrvl_sdio_verify_fw_download(card, 1)) {
872 BT_DBG("Firmware already downloaded!");
873 goto done;
876 /* Check if other function driver is downloading the firmware */
877 fws0 = sdio_readb(card->func, card->reg->card_fw_status0, &ret);
878 if (ret) {
879 BT_ERR("Failed to read FW downloading status!");
880 ret = -EIO;
881 goto done;
883 if (fws0) {
884 BT_DBG("BT not the winner (%#x). Skip FW downloading", fws0);
886 /* Give other function more time to download the firmware */
887 pollnum *= 10;
888 } else {
889 if (card->helper) {
890 ret = btmrvl_sdio_download_helper(card);
891 if (ret) {
892 BT_ERR("Failed to download helper!");
893 ret = -EIO;
894 goto done;
898 if (btmrvl_sdio_download_fw_w_helper(card)) {
899 BT_ERR("Failed to download firmware!");
900 ret = -EIO;
901 goto done;
905 if (btmrvl_sdio_verify_fw_download(card, pollnum)) {
906 BT_ERR("FW failed to be active in time!");
907 ret = -ETIMEDOUT;
908 goto done;
911 done:
912 sdio_release_host(card->func);
914 return ret;
917 static int btmrvl_sdio_wakeup_fw(struct btmrvl_private *priv)
919 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
920 int ret = 0;
922 if (!card || !card->func) {
923 BT_ERR("card or function is NULL!");
924 return -EINVAL;
927 sdio_claim_host(card->func);
929 sdio_writeb(card->func, HOST_POWER_UP, card->reg->cfg, &ret);
931 sdio_release_host(card->func);
933 BT_DBG("wake up firmware");
935 return ret;
938 static int btmrvl_sdio_probe(struct sdio_func *func,
939 const struct sdio_device_id *id)
941 int ret = 0;
942 struct btmrvl_private *priv = NULL;
943 struct btmrvl_sdio_card *card = NULL;
945 BT_INFO("vendor=0x%x, device=0x%x, class=%d, fn=%d",
946 id->vendor, id->device, id->class, func->num);
948 card = kzalloc(sizeof(*card), GFP_KERNEL);
949 if (!card) {
950 ret = -ENOMEM;
951 goto done;
954 card->func = func;
956 if (id->driver_data) {
957 struct btmrvl_sdio_device *data = (void *) id->driver_data;
958 card->helper = data->helper;
959 card->firmware = data->firmware;
960 card->reg = data->reg;
961 card->sd_blksz_fw_dl = data->sd_blksz_fw_dl;
964 if (btmrvl_sdio_register_dev(card) < 0) {
965 BT_ERR("Failed to register BT device!");
966 ret = -ENODEV;
967 goto free_card;
970 /* Disable the interrupts on the card */
971 btmrvl_sdio_disable_host_int(card);
973 if (btmrvl_sdio_download_fw(card)) {
974 BT_ERR("Downloading firmware failed!");
975 ret = -ENODEV;
976 goto unreg_dev;
979 msleep(100);
981 btmrvl_sdio_enable_host_int(card);
983 priv = btmrvl_add_card(card);
984 if (!priv) {
985 BT_ERR("Initializing card failed!");
986 ret = -ENODEV;
987 goto disable_host_int;
990 card->priv = priv;
992 /* Initialize the interface specific function pointers */
993 priv->hw_host_to_card = btmrvl_sdio_host_to_card;
994 priv->hw_wakeup_firmware = btmrvl_sdio_wakeup_fw;
995 priv->hw_process_int_status = btmrvl_sdio_process_int_status;
997 if (btmrvl_register_hdev(priv)) {
998 BT_ERR("Register hdev failed!");
999 ret = -ENODEV;
1000 goto disable_host_int;
1003 priv->btmrvl_dev.psmode = 1;
1004 btmrvl_enable_ps(priv);
1006 return 0;
1008 disable_host_int:
1009 btmrvl_sdio_disable_host_int(card);
1010 unreg_dev:
1011 btmrvl_sdio_unregister_dev(card);
1012 free_card:
1013 kfree(card);
1014 done:
1015 return ret;
1018 static void btmrvl_sdio_remove(struct sdio_func *func)
1020 struct btmrvl_sdio_card *card;
1022 if (func) {
1023 card = sdio_get_drvdata(func);
1024 if (card) {
1025 /* Send SHUTDOWN command & disable interrupt
1026 * if user removes the module.
1028 if (user_rmmod) {
1029 btmrvl_send_module_cfg_cmd(card->priv,
1030 MODULE_SHUTDOWN_REQ);
1031 btmrvl_sdio_disable_host_int(card);
1033 BT_DBG("unregester dev");
1034 btmrvl_sdio_unregister_dev(card);
1035 btmrvl_remove_card(card->priv);
1036 kfree(card);
1041 static struct sdio_driver bt_mrvl_sdio = {
1042 .name = "btmrvl_sdio",
1043 .id_table = btmrvl_sdio_ids,
1044 .probe = btmrvl_sdio_probe,
1045 .remove = btmrvl_sdio_remove,
1048 static int __init btmrvl_sdio_init_module(void)
1050 if (sdio_register_driver(&bt_mrvl_sdio) != 0) {
1051 BT_ERR("SDIO Driver Registration Failed");
1052 return -ENODEV;
1055 /* Clear the flag in case user removes the card. */
1056 user_rmmod = 0;
1058 return 0;
1061 static void __exit btmrvl_sdio_exit_module(void)
1063 /* Set the flag as user is removing this module. */
1064 user_rmmod = 1;
1066 sdio_unregister_driver(&bt_mrvl_sdio);
1069 module_init(btmrvl_sdio_init_module);
1070 module_exit(btmrvl_sdio_exit_module);
1072 MODULE_AUTHOR("Marvell International Ltd.");
1073 MODULE_DESCRIPTION("Marvell BT-over-SDIO driver ver " VERSION);
1074 MODULE_VERSION(VERSION);
1075 MODULE_LICENSE("GPL v2");
1076 MODULE_FIRMWARE("sd8688_helper.bin");
1077 MODULE_FIRMWARE("sd8688.bin");
1078 MODULE_FIRMWARE("mrvl/sd8787_uapsta.bin");