sched: Remove double_rq_lock() from __migrate_task()
[linux/fpc-iii.git] / drivers / bluetooth / btmrvl_sdio.c
blob9dedca516ff50567a278fb9a511dbcdfd7a1980c
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,
67 .int_read_to_clear = false,
69 static const struct btmrvl_sdio_card_reg btmrvl_reg_87xx = {
70 .cfg = 0x00,
71 .host_int_mask = 0x02,
72 .host_intstatus = 0x03,
73 .card_status = 0x30,
74 .sq_read_base_addr_a0 = 0x40,
75 .sq_read_base_addr_a1 = 0x41,
76 .card_revision = 0x5c,
77 .card_fw_status0 = 0x60,
78 .card_fw_status1 = 0x61,
79 .card_rx_len = 0x62,
80 .card_rx_unit = 0x63,
81 .io_port_0 = 0x78,
82 .io_port_1 = 0x79,
83 .io_port_2 = 0x7a,
84 .int_read_to_clear = false,
87 static const struct btmrvl_sdio_card_reg btmrvl_reg_88xx = {
88 .cfg = 0x00,
89 .host_int_mask = 0x02,
90 .host_intstatus = 0x03,
91 .card_status = 0x50,
92 .sq_read_base_addr_a0 = 0x60,
93 .sq_read_base_addr_a1 = 0x61,
94 .card_revision = 0xbc,
95 .card_fw_status0 = 0xc0,
96 .card_fw_status1 = 0xc1,
97 .card_rx_len = 0xc2,
98 .card_rx_unit = 0xc3,
99 .io_port_0 = 0xd8,
100 .io_port_1 = 0xd9,
101 .io_port_2 = 0xda,
102 .int_read_to_clear = true,
103 .host_int_rsr = 0x01,
104 .card_misc_cfg = 0xcc,
107 static const struct btmrvl_sdio_device btmrvl_sdio_sd8688 = {
108 .helper = "mrvl/sd8688_helper.bin",
109 .firmware = "mrvl/sd8688.bin",
110 .reg = &btmrvl_reg_8688,
111 .sd_blksz_fw_dl = 64,
114 static const struct btmrvl_sdio_device btmrvl_sdio_sd8787 = {
115 .helper = NULL,
116 .firmware = "mrvl/sd8787_uapsta.bin",
117 .reg = &btmrvl_reg_87xx,
118 .sd_blksz_fw_dl = 256,
121 static const struct btmrvl_sdio_device btmrvl_sdio_sd8797 = {
122 .helper = NULL,
123 .firmware = "mrvl/sd8797_uapsta.bin",
124 .reg = &btmrvl_reg_87xx,
125 .sd_blksz_fw_dl = 256,
128 static const struct btmrvl_sdio_device btmrvl_sdio_sd8897 = {
129 .helper = NULL,
130 .firmware = "mrvl/sd8897_uapsta.bin",
131 .reg = &btmrvl_reg_88xx,
132 .sd_blksz_fw_dl = 256,
135 static const struct sdio_device_id btmrvl_sdio_ids[] = {
136 /* Marvell SD8688 Bluetooth device */
137 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x9105),
138 .driver_data = (unsigned long) &btmrvl_sdio_sd8688 },
139 /* Marvell SD8787 Bluetooth device */
140 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x911A),
141 .driver_data = (unsigned long) &btmrvl_sdio_sd8787 },
142 /* Marvell SD8787 Bluetooth AMP device */
143 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x911B),
144 .driver_data = (unsigned long) &btmrvl_sdio_sd8787 },
145 /* Marvell SD8797 Bluetooth device */
146 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x912A),
147 .driver_data = (unsigned long) &btmrvl_sdio_sd8797 },
148 /* Marvell SD8897 Bluetooth device */
149 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x912E),
150 .driver_data = (unsigned long) &btmrvl_sdio_sd8897 },
152 { } /* Terminating entry */
155 MODULE_DEVICE_TABLE(sdio, btmrvl_sdio_ids);
157 static int btmrvl_sdio_get_rx_unit(struct btmrvl_sdio_card *card)
159 u8 reg;
160 int ret;
162 reg = sdio_readb(card->func, card->reg->card_rx_unit, &ret);
163 if (!ret)
164 card->rx_unit = reg;
166 return ret;
169 static int btmrvl_sdio_read_fw_status(struct btmrvl_sdio_card *card, u16 *dat)
171 u8 fws0, fws1;
172 int ret;
174 *dat = 0;
176 fws0 = sdio_readb(card->func, card->reg->card_fw_status0, &ret);
177 if (ret)
178 return -EIO;
180 fws1 = sdio_readb(card->func, card->reg->card_fw_status1, &ret);
181 if (ret)
182 return -EIO;
184 *dat = (((u16) fws1) << 8) | fws0;
186 return 0;
189 static int btmrvl_sdio_read_rx_len(struct btmrvl_sdio_card *card, u16 *dat)
191 u8 reg;
192 int ret;
194 reg = sdio_readb(card->func, card->reg->card_rx_len, &ret);
195 if (!ret)
196 *dat = (u16) reg << card->rx_unit;
198 return ret;
201 static int btmrvl_sdio_enable_host_int_mask(struct btmrvl_sdio_card *card,
202 u8 mask)
204 int ret;
206 sdio_writeb(card->func, mask, card->reg->host_int_mask, &ret);
207 if (ret) {
208 BT_ERR("Unable to enable the host interrupt!");
209 ret = -EIO;
212 return ret;
215 static int btmrvl_sdio_disable_host_int_mask(struct btmrvl_sdio_card *card,
216 u8 mask)
218 u8 host_int_mask;
219 int ret;
221 host_int_mask = sdio_readb(card->func, card->reg->host_int_mask, &ret);
222 if (ret)
223 return -EIO;
225 host_int_mask &= ~mask;
227 sdio_writeb(card->func, host_int_mask, card->reg->host_int_mask, &ret);
228 if (ret < 0) {
229 BT_ERR("Unable to disable the host interrupt!");
230 return -EIO;
233 return 0;
236 static int btmrvl_sdio_poll_card_status(struct btmrvl_sdio_card *card, u8 bits)
238 unsigned int tries;
239 u8 status;
240 int ret;
242 for (tries = 0; tries < MAX_POLL_TRIES * 1000; tries++) {
243 status = sdio_readb(card->func, card->reg->card_status, &ret);
244 if (ret)
245 goto failed;
246 if ((status & bits) == bits)
247 return ret;
249 udelay(1);
252 ret = -ETIMEDOUT;
254 failed:
255 BT_ERR("FAILED! ret=%d", ret);
257 return ret;
260 static int btmrvl_sdio_verify_fw_download(struct btmrvl_sdio_card *card,
261 int pollnum)
263 u16 firmwarestat;
264 int tries, ret;
266 /* Wait for firmware to become ready */
267 for (tries = 0; tries < pollnum; tries++) {
268 sdio_claim_host(card->func);
269 ret = btmrvl_sdio_read_fw_status(card, &firmwarestat);
270 sdio_release_host(card->func);
271 if (ret < 0)
272 continue;
274 if (firmwarestat == FIRMWARE_READY)
275 return 0;
277 msleep(10);
280 return -ETIMEDOUT;
283 static int btmrvl_sdio_download_helper(struct btmrvl_sdio_card *card)
285 const struct firmware *fw_helper = NULL;
286 const u8 *helper = NULL;
287 int ret;
288 void *tmphlprbuf = NULL;
289 int tmphlprbufsz, hlprblknow, helperlen;
290 u8 *helperbuf;
291 u32 tx_len;
293 ret = request_firmware(&fw_helper, card->helper,
294 &card->func->dev);
295 if ((ret < 0) || !fw_helper) {
296 BT_ERR("request_firmware(helper) failed, error code = %d",
297 ret);
298 ret = -ENOENT;
299 goto done;
302 helper = fw_helper->data;
303 helperlen = fw_helper->size;
305 BT_DBG("Downloading helper image (%d bytes), block size %d bytes",
306 helperlen, SDIO_BLOCK_SIZE);
308 tmphlprbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
310 tmphlprbuf = kzalloc(tmphlprbufsz, GFP_KERNEL);
311 if (!tmphlprbuf) {
312 BT_ERR("Unable to allocate buffer for helper."
313 " Terminating download");
314 ret = -ENOMEM;
315 goto done;
318 helperbuf = (u8 *) ALIGN_ADDR(tmphlprbuf, BTSDIO_DMA_ALIGN);
320 /* Perform helper data transfer */
321 tx_len = (FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE)
322 - SDIO_HEADER_LEN;
323 hlprblknow = 0;
325 do {
326 ret = btmrvl_sdio_poll_card_status(card,
327 CARD_IO_READY | DN_LD_CARD_RDY);
328 if (ret < 0) {
329 BT_ERR("Helper download poll status timeout @ %d",
330 hlprblknow);
331 goto done;
334 /* Check if there is more data? */
335 if (hlprblknow >= helperlen)
336 break;
338 if (helperlen - hlprblknow < tx_len)
339 tx_len = helperlen - hlprblknow;
341 /* Little-endian */
342 helperbuf[0] = ((tx_len & 0x000000ff) >> 0);
343 helperbuf[1] = ((tx_len & 0x0000ff00) >> 8);
344 helperbuf[2] = ((tx_len & 0x00ff0000) >> 16);
345 helperbuf[3] = ((tx_len & 0xff000000) >> 24);
347 memcpy(&helperbuf[SDIO_HEADER_LEN], &helper[hlprblknow],
348 tx_len);
350 /* Now send the data */
351 ret = sdio_writesb(card->func, card->ioport, helperbuf,
352 FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE);
353 if (ret < 0) {
354 BT_ERR("IO error during helper download @ %d",
355 hlprblknow);
356 goto done;
359 hlprblknow += tx_len;
360 } while (true);
362 BT_DBG("Transferring helper image EOF block");
364 memset(helperbuf, 0x0, SDIO_BLOCK_SIZE);
366 ret = sdio_writesb(card->func, card->ioport, helperbuf,
367 SDIO_BLOCK_SIZE);
368 if (ret < 0) {
369 BT_ERR("IO error in writing helper image EOF block");
370 goto done;
373 ret = 0;
375 done:
376 kfree(tmphlprbuf);
377 release_firmware(fw_helper);
378 return ret;
381 static int btmrvl_sdio_download_fw_w_helper(struct btmrvl_sdio_card *card)
383 const struct firmware *fw_firmware = NULL;
384 const u8 *firmware = NULL;
385 int firmwarelen, tmpfwbufsz, ret;
386 unsigned int tries, offset;
387 u8 base0, base1;
388 void *tmpfwbuf = NULL;
389 u8 *fwbuf;
390 u16 len, blksz_dl = card->sd_blksz_fw_dl;
391 int txlen = 0, tx_blocks = 0, count = 0;
393 ret = request_firmware(&fw_firmware, card->firmware,
394 &card->func->dev);
395 if ((ret < 0) || !fw_firmware) {
396 BT_ERR("request_firmware(firmware) failed, error code = %d",
397 ret);
398 ret = -ENOENT;
399 goto done;
402 firmware = fw_firmware->data;
403 firmwarelen = fw_firmware->size;
405 BT_DBG("Downloading FW image (%d bytes)", firmwarelen);
407 tmpfwbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
408 tmpfwbuf = kzalloc(tmpfwbufsz, GFP_KERNEL);
409 if (!tmpfwbuf) {
410 BT_ERR("Unable to allocate buffer for firmware."
411 " Terminating download");
412 ret = -ENOMEM;
413 goto done;
416 /* Ensure aligned firmware buffer */
417 fwbuf = (u8 *) ALIGN_ADDR(tmpfwbuf, BTSDIO_DMA_ALIGN);
419 /* Perform firmware data transfer */
420 offset = 0;
421 do {
422 ret = btmrvl_sdio_poll_card_status(card,
423 CARD_IO_READY | DN_LD_CARD_RDY);
424 if (ret < 0) {
425 BT_ERR("FW download with helper poll status"
426 " timeout @ %d", offset);
427 goto done;
430 /* Check if there is more data ? */
431 if (offset >= firmwarelen)
432 break;
434 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
435 base0 = sdio_readb(card->func,
436 card->reg->sq_read_base_addr_a0, &ret);
437 if (ret) {
438 BT_ERR("BASE0 register read failed:"
439 " base0 = 0x%04X(%d)."
440 " Terminating download",
441 base0, base0);
442 ret = -EIO;
443 goto done;
445 base1 = sdio_readb(card->func,
446 card->reg->sq_read_base_addr_a1, &ret);
447 if (ret) {
448 BT_ERR("BASE1 register read failed:"
449 " base1 = 0x%04X(%d)."
450 " Terminating download",
451 base1, base1);
452 ret = -EIO;
453 goto done;
456 len = (((u16) base1) << 8) | base0;
457 if (len)
458 break;
460 udelay(10);
463 if (!len)
464 break;
465 else if (len > BTM_UPLD_SIZE) {
466 BT_ERR("FW download failure @%d, invalid length %d",
467 offset, len);
468 ret = -EINVAL;
469 goto done;
472 txlen = len;
474 if (len & BIT(0)) {
475 count++;
476 if (count > MAX_WRITE_IOMEM_RETRY) {
477 BT_ERR("FW download failure @%d, "
478 "over max retry count", offset);
479 ret = -EIO;
480 goto done;
482 BT_ERR("FW CRC error indicated by the helper: "
483 "len = 0x%04X, txlen = %d", len, txlen);
484 len &= ~BIT(0);
485 /* Set txlen to 0 so as to resend from same offset */
486 txlen = 0;
487 } else {
488 count = 0;
490 /* Last block ? */
491 if (firmwarelen - offset < txlen)
492 txlen = firmwarelen - offset;
494 tx_blocks = DIV_ROUND_UP(txlen, blksz_dl);
496 memcpy(fwbuf, &firmware[offset], txlen);
499 ret = sdio_writesb(card->func, card->ioport, fwbuf,
500 tx_blocks * blksz_dl);
502 if (ret < 0) {
503 BT_ERR("FW download, writesb(%d) failed @%d",
504 count, offset);
505 sdio_writeb(card->func, HOST_CMD53_FIN,
506 card->reg->cfg, &ret);
507 if (ret)
508 BT_ERR("writeb failed (CFG)");
511 offset += txlen;
512 } while (true);
514 BT_DBG("FW download over, size %d bytes", offset);
516 ret = 0;
518 done:
519 kfree(tmpfwbuf);
520 release_firmware(fw_firmware);
521 return ret;
524 static int btmrvl_sdio_card_to_host(struct btmrvl_private *priv)
526 u16 buf_len = 0;
527 int ret, num_blocks, blksz;
528 struct sk_buff *skb = NULL;
529 u32 type;
530 u8 *payload = NULL;
531 struct hci_dev *hdev = priv->btmrvl_dev.hcidev;
532 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
534 if (!card || !card->func) {
535 BT_ERR("card or function is NULL!");
536 ret = -EINVAL;
537 goto exit;
540 /* Read the length of data to be transferred */
541 ret = btmrvl_sdio_read_rx_len(card, &buf_len);
542 if (ret < 0) {
543 BT_ERR("read rx_len failed");
544 ret = -EIO;
545 goto exit;
548 blksz = SDIO_BLOCK_SIZE;
549 num_blocks = DIV_ROUND_UP(buf_len, blksz);
551 if (buf_len <= SDIO_HEADER_LEN
552 || (num_blocks * blksz) > ALLOC_BUF_SIZE) {
553 BT_ERR("invalid packet length: %d", buf_len);
554 ret = -EINVAL;
555 goto exit;
558 /* Allocate buffer */
559 skb = bt_skb_alloc(num_blocks * blksz + BTSDIO_DMA_ALIGN, GFP_ATOMIC);
560 if (skb == NULL) {
561 BT_ERR("No free skb");
562 ret = -ENOMEM;
563 goto exit;
566 if ((unsigned long) skb->data & (BTSDIO_DMA_ALIGN - 1)) {
567 skb_put(skb, (unsigned long) skb->data &
568 (BTSDIO_DMA_ALIGN - 1));
569 skb_pull(skb, (unsigned long) skb->data &
570 (BTSDIO_DMA_ALIGN - 1));
573 payload = skb->data;
575 ret = sdio_readsb(card->func, payload, card->ioport,
576 num_blocks * blksz);
577 if (ret < 0) {
578 BT_ERR("readsb failed: %d", ret);
579 ret = -EIO;
580 goto exit;
583 /* This is SDIO specific header length: byte[2][1][0], type: byte[3]
584 * (HCI_COMMAND = 1, ACL_DATA = 2, SCO_DATA = 3, 0xFE = Vendor)
587 buf_len = payload[0];
588 buf_len |= payload[1] << 8;
589 buf_len |= payload[2] << 16;
591 if (buf_len > blksz * num_blocks) {
592 BT_ERR("Skip incorrect packet: hdrlen %d buffer %d",
593 buf_len, blksz * num_blocks);
594 ret = -EIO;
595 goto exit;
598 type = payload[3];
600 switch (type) {
601 case HCI_ACLDATA_PKT:
602 case HCI_SCODATA_PKT:
603 case HCI_EVENT_PKT:
604 bt_cb(skb)->pkt_type = type;
605 skb_put(skb, buf_len);
606 skb_pull(skb, SDIO_HEADER_LEN);
608 if (type == HCI_EVENT_PKT) {
609 if (btmrvl_check_evtpkt(priv, skb))
610 hci_recv_frame(hdev, skb);
611 } else {
612 hci_recv_frame(hdev, skb);
615 hdev->stat.byte_rx += buf_len;
616 break;
618 case MRVL_VENDOR_PKT:
619 bt_cb(skb)->pkt_type = HCI_VENDOR_PKT;
620 skb_put(skb, buf_len);
621 skb_pull(skb, SDIO_HEADER_LEN);
623 if (btmrvl_process_event(priv, skb))
624 hci_recv_frame(hdev, skb);
626 hdev->stat.byte_rx += buf_len;
627 break;
629 default:
630 BT_ERR("Unknown packet type:%d", type);
631 BT_ERR("hex: %*ph", blksz * num_blocks, payload);
633 kfree_skb(skb);
634 skb = NULL;
635 break;
638 exit:
639 if (ret) {
640 hdev->stat.err_rx++;
641 kfree_skb(skb);
644 return ret;
647 static int btmrvl_sdio_process_int_status(struct btmrvl_private *priv)
649 ulong flags;
650 u8 ireg;
651 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
653 spin_lock_irqsave(&priv->driver_lock, flags);
654 ireg = sdio_ireg;
655 sdio_ireg = 0;
656 spin_unlock_irqrestore(&priv->driver_lock, flags);
658 sdio_claim_host(card->func);
659 if (ireg & DN_LD_HOST_INT_STATUS) {
660 if (priv->btmrvl_dev.tx_dnld_rdy)
661 BT_DBG("tx_done already received: "
662 " int_status=0x%x", ireg);
663 else
664 priv->btmrvl_dev.tx_dnld_rdy = true;
667 if (ireg & UP_LD_HOST_INT_STATUS)
668 btmrvl_sdio_card_to_host(priv);
670 sdio_release_host(card->func);
672 return 0;
675 static int btmrvl_sdio_read_to_clear(struct btmrvl_sdio_card *card, u8 *ireg)
677 struct btmrvl_adapter *adapter = card->priv->adapter;
678 int ret;
680 ret = sdio_readsb(card->func, adapter->hw_regs, 0, SDIO_BLOCK_SIZE);
681 if (ret) {
682 BT_ERR("sdio_readsb: read int hw_regs failed: %d", ret);
683 return ret;
686 *ireg = adapter->hw_regs[card->reg->host_intstatus];
687 BT_DBG("hw_regs[%#x]=%#x", card->reg->host_intstatus, *ireg);
689 return 0;
692 static int btmrvl_sdio_write_to_clear(struct btmrvl_sdio_card *card, u8 *ireg)
694 int ret;
696 *ireg = sdio_readb(card->func, card->reg->host_intstatus, &ret);
697 if (ret) {
698 BT_ERR("sdio_readb: read int status failed: %d", ret);
699 return ret;
702 if (*ireg) {
704 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
705 * Clear the interrupt status register and re-enable the
706 * interrupt.
708 BT_DBG("int_status = 0x%x", *ireg);
710 sdio_writeb(card->func, ~(*ireg) & (DN_LD_HOST_INT_STATUS |
711 UP_LD_HOST_INT_STATUS),
712 card->reg->host_intstatus, &ret);
713 if (ret) {
714 BT_ERR("sdio_writeb: clear int status failed: %d", ret);
715 return ret;
719 return 0;
722 static void btmrvl_sdio_interrupt(struct sdio_func *func)
724 struct btmrvl_private *priv;
725 struct btmrvl_sdio_card *card;
726 ulong flags;
727 u8 ireg = 0;
728 int ret;
730 card = sdio_get_drvdata(func);
731 if (!card || !card->priv) {
732 BT_ERR("sbi_interrupt(%p) card or priv is "
733 "NULL, card=%p\n", func, card);
734 return;
737 priv = card->priv;
739 if (card->reg->int_read_to_clear)
740 ret = btmrvl_sdio_read_to_clear(card, &ireg);
741 else
742 ret = btmrvl_sdio_write_to_clear(card, &ireg);
744 if (ret)
745 return;
747 spin_lock_irqsave(&priv->driver_lock, flags);
748 sdio_ireg |= ireg;
749 spin_unlock_irqrestore(&priv->driver_lock, flags);
751 btmrvl_interrupt(priv);
754 static int btmrvl_sdio_register_dev(struct btmrvl_sdio_card *card)
756 struct sdio_func *func;
757 u8 reg;
758 int ret = 0;
760 if (!card || !card->func) {
761 BT_ERR("Error: card or function is NULL!");
762 ret = -EINVAL;
763 goto failed;
766 func = card->func;
768 sdio_claim_host(func);
770 ret = sdio_enable_func(func);
771 if (ret) {
772 BT_ERR("sdio_enable_func() failed: ret=%d", ret);
773 ret = -EIO;
774 goto release_host;
777 ret = sdio_claim_irq(func, btmrvl_sdio_interrupt);
778 if (ret) {
779 BT_ERR("sdio_claim_irq failed: ret=%d", ret);
780 ret = -EIO;
781 goto disable_func;
784 ret = sdio_set_block_size(card->func, SDIO_BLOCK_SIZE);
785 if (ret) {
786 BT_ERR("cannot set SDIO block size");
787 ret = -EIO;
788 goto release_irq;
791 reg = sdio_readb(func, card->reg->io_port_0, &ret);
792 if (ret < 0) {
793 ret = -EIO;
794 goto release_irq;
797 card->ioport = reg;
799 reg = sdio_readb(func, card->reg->io_port_1, &ret);
800 if (ret < 0) {
801 ret = -EIO;
802 goto release_irq;
805 card->ioport |= (reg << 8);
807 reg = sdio_readb(func, card->reg->io_port_2, &ret);
808 if (ret < 0) {
809 ret = -EIO;
810 goto release_irq;
813 card->ioport |= (reg << 16);
815 BT_DBG("SDIO FUNC%d IO port: 0x%x", func->num, card->ioport);
817 if (card->reg->int_read_to_clear) {
818 reg = sdio_readb(func, card->reg->host_int_rsr, &ret);
819 if (ret < 0) {
820 ret = -EIO;
821 goto release_irq;
823 sdio_writeb(func, reg | 0x3f, card->reg->host_int_rsr, &ret);
824 if (ret < 0) {
825 ret = -EIO;
826 goto release_irq;
829 reg = sdio_readb(func, card->reg->card_misc_cfg, &ret);
830 if (ret < 0) {
831 ret = -EIO;
832 goto release_irq;
834 sdio_writeb(func, reg | 0x10, card->reg->card_misc_cfg, &ret);
835 if (ret < 0) {
836 ret = -EIO;
837 goto release_irq;
841 sdio_set_drvdata(func, card);
843 sdio_release_host(func);
845 return 0;
847 release_irq:
848 sdio_release_irq(func);
850 disable_func:
851 sdio_disable_func(func);
853 release_host:
854 sdio_release_host(func);
856 failed:
857 return ret;
860 static int btmrvl_sdio_unregister_dev(struct btmrvl_sdio_card *card)
862 if (card && card->func) {
863 sdio_claim_host(card->func);
864 sdio_release_irq(card->func);
865 sdio_disable_func(card->func);
866 sdio_release_host(card->func);
867 sdio_set_drvdata(card->func, NULL);
870 return 0;
873 static int btmrvl_sdio_enable_host_int(struct btmrvl_sdio_card *card)
875 int ret;
877 if (!card || !card->func)
878 return -EINVAL;
880 sdio_claim_host(card->func);
882 ret = btmrvl_sdio_enable_host_int_mask(card, HIM_ENABLE);
884 btmrvl_sdio_get_rx_unit(card);
886 sdio_release_host(card->func);
888 return ret;
891 static int btmrvl_sdio_disable_host_int(struct btmrvl_sdio_card *card)
893 int ret;
895 if (!card || !card->func)
896 return -EINVAL;
898 sdio_claim_host(card->func);
900 ret = btmrvl_sdio_disable_host_int_mask(card, HIM_DISABLE);
902 sdio_release_host(card->func);
904 return ret;
907 static int btmrvl_sdio_host_to_card(struct btmrvl_private *priv,
908 u8 *payload, u16 nb)
910 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
911 int ret = 0;
912 int buf_block_len;
913 int blksz;
914 int i = 0;
915 u8 *buf = NULL;
916 void *tmpbuf = NULL;
917 int tmpbufsz;
919 if (!card || !card->func) {
920 BT_ERR("card or function is NULL!");
921 return -EINVAL;
924 buf = payload;
925 if ((unsigned long) payload & (BTSDIO_DMA_ALIGN - 1)) {
926 tmpbufsz = ALIGN_SZ(nb, BTSDIO_DMA_ALIGN);
927 tmpbuf = kzalloc(tmpbufsz, GFP_KERNEL);
928 if (!tmpbuf)
929 return -ENOMEM;
930 buf = (u8 *) ALIGN_ADDR(tmpbuf, BTSDIO_DMA_ALIGN);
931 memcpy(buf, payload, nb);
934 blksz = SDIO_BLOCK_SIZE;
935 buf_block_len = DIV_ROUND_UP(nb, blksz);
937 sdio_claim_host(card->func);
939 do {
940 /* Transfer data to card */
941 ret = sdio_writesb(card->func, card->ioport, buf,
942 buf_block_len * blksz);
943 if (ret < 0) {
944 i++;
945 BT_ERR("i=%d writesb failed: %d", i, ret);
946 BT_ERR("hex: %*ph", nb, payload);
947 ret = -EIO;
948 if (i > MAX_WRITE_IOMEM_RETRY)
949 goto exit;
951 } while (ret);
953 priv->btmrvl_dev.tx_dnld_rdy = false;
955 exit:
956 sdio_release_host(card->func);
957 kfree(tmpbuf);
959 return ret;
962 static int btmrvl_sdio_download_fw(struct btmrvl_sdio_card *card)
964 int ret;
965 u8 fws0;
966 int pollnum = MAX_POLL_TRIES;
968 if (!card || !card->func) {
969 BT_ERR("card or function is NULL!");
970 return -EINVAL;
973 if (!btmrvl_sdio_verify_fw_download(card, 1)) {
974 BT_DBG("Firmware already downloaded!");
975 return 0;
978 sdio_claim_host(card->func);
980 /* Check if other function driver is downloading the firmware */
981 fws0 = sdio_readb(card->func, card->reg->card_fw_status0, &ret);
982 if (ret) {
983 BT_ERR("Failed to read FW downloading status!");
984 ret = -EIO;
985 goto done;
987 if (fws0) {
988 BT_DBG("BT not the winner (%#x). Skip FW downloading", fws0);
990 /* Give other function more time to download the firmware */
991 pollnum *= 10;
992 } else {
993 if (card->helper) {
994 ret = btmrvl_sdio_download_helper(card);
995 if (ret) {
996 BT_ERR("Failed to download helper!");
997 ret = -EIO;
998 goto done;
1002 if (btmrvl_sdio_download_fw_w_helper(card)) {
1003 BT_ERR("Failed to download firmware!");
1004 ret = -EIO;
1005 goto done;
1009 sdio_release_host(card->func);
1012 * winner or not, with this test the FW synchronizes when the
1013 * module can continue its initialization
1015 if (btmrvl_sdio_verify_fw_download(card, pollnum)) {
1016 BT_ERR("FW failed to be active in time!");
1017 return -ETIMEDOUT;
1020 return 0;
1022 done:
1023 sdio_release_host(card->func);
1024 return ret;
1027 static int btmrvl_sdio_wakeup_fw(struct btmrvl_private *priv)
1029 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
1030 int ret = 0;
1032 if (!card || !card->func) {
1033 BT_ERR("card or function is NULL!");
1034 return -EINVAL;
1037 sdio_claim_host(card->func);
1039 sdio_writeb(card->func, HOST_POWER_UP, card->reg->cfg, &ret);
1041 sdio_release_host(card->func);
1043 BT_DBG("wake up firmware");
1045 return ret;
1048 static int btmrvl_sdio_probe(struct sdio_func *func,
1049 const struct sdio_device_id *id)
1051 int ret = 0;
1052 struct btmrvl_private *priv = NULL;
1053 struct btmrvl_sdio_card *card = NULL;
1055 BT_INFO("vendor=0x%x, device=0x%x, class=%d, fn=%d",
1056 id->vendor, id->device, id->class, func->num);
1058 card = devm_kzalloc(&func->dev, sizeof(*card), GFP_KERNEL);
1059 if (!card)
1060 return -ENOMEM;
1062 card->func = func;
1064 if (id->driver_data) {
1065 struct btmrvl_sdio_device *data = (void *) id->driver_data;
1066 card->helper = data->helper;
1067 card->firmware = data->firmware;
1068 card->reg = data->reg;
1069 card->sd_blksz_fw_dl = data->sd_blksz_fw_dl;
1072 if (btmrvl_sdio_register_dev(card) < 0) {
1073 BT_ERR("Failed to register BT device!");
1074 return -ENODEV;
1077 /* Disable the interrupts on the card */
1078 btmrvl_sdio_disable_host_int(card);
1080 if (btmrvl_sdio_download_fw(card)) {
1081 BT_ERR("Downloading firmware failed!");
1082 ret = -ENODEV;
1083 goto unreg_dev;
1086 btmrvl_sdio_enable_host_int(card);
1088 priv = btmrvl_add_card(card);
1089 if (!priv) {
1090 BT_ERR("Initializing card failed!");
1091 ret = -ENODEV;
1092 goto disable_host_int;
1095 card->priv = priv;
1097 /* Initialize the interface specific function pointers */
1098 priv->hw_host_to_card = btmrvl_sdio_host_to_card;
1099 priv->hw_wakeup_firmware = btmrvl_sdio_wakeup_fw;
1100 priv->hw_process_int_status = btmrvl_sdio_process_int_status;
1102 if (btmrvl_register_hdev(priv)) {
1103 BT_ERR("Register hdev failed!");
1104 ret = -ENODEV;
1105 goto disable_host_int;
1108 return 0;
1110 disable_host_int:
1111 btmrvl_sdio_disable_host_int(card);
1112 unreg_dev:
1113 btmrvl_sdio_unregister_dev(card);
1114 return ret;
1117 static void btmrvl_sdio_remove(struct sdio_func *func)
1119 struct btmrvl_sdio_card *card;
1121 if (func) {
1122 card = sdio_get_drvdata(func);
1123 if (card) {
1124 /* Send SHUTDOWN command & disable interrupt
1125 * if user removes the module.
1127 if (user_rmmod) {
1128 btmrvl_send_module_cfg_cmd(card->priv,
1129 MODULE_SHUTDOWN_REQ);
1130 btmrvl_sdio_disable_host_int(card);
1132 BT_DBG("unregester dev");
1133 btmrvl_sdio_unregister_dev(card);
1134 btmrvl_remove_card(card->priv);
1139 static int btmrvl_sdio_suspend(struct device *dev)
1141 struct sdio_func *func = dev_to_sdio_func(dev);
1142 struct btmrvl_sdio_card *card;
1143 struct btmrvl_private *priv;
1144 mmc_pm_flag_t pm_flags;
1145 struct hci_dev *hcidev;
1147 if (func) {
1148 pm_flags = sdio_get_host_pm_caps(func);
1149 BT_DBG("%s: suspend: PM flags = 0x%x", sdio_func_id(func),
1150 pm_flags);
1151 if (!(pm_flags & MMC_PM_KEEP_POWER)) {
1152 BT_ERR("%s: cannot remain alive while suspended",
1153 sdio_func_id(func));
1154 return -ENOSYS;
1156 card = sdio_get_drvdata(func);
1157 if (!card || !card->priv) {
1158 BT_ERR("card or priv structure is not valid");
1159 return 0;
1161 } else {
1162 BT_ERR("sdio_func is not specified");
1163 return 0;
1166 priv = card->priv;
1168 if (priv->adapter->hs_state != HS_ACTIVATED) {
1169 if (btmrvl_enable_hs(priv)) {
1170 BT_ERR("HS not actived, suspend failed!");
1171 return -EBUSY;
1174 hcidev = priv->btmrvl_dev.hcidev;
1175 BT_DBG("%s: SDIO suspend", hcidev->name);
1176 hci_suspend_dev(hcidev);
1177 skb_queue_purge(&priv->adapter->tx_queue);
1179 priv->adapter->is_suspended = true;
1181 /* We will keep the power when hs enabled successfully */
1182 if (priv->adapter->hs_state == HS_ACTIVATED) {
1183 BT_DBG("suspend with MMC_PM_KEEP_POWER");
1184 return sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
1185 } else {
1186 BT_DBG("suspend without MMC_PM_KEEP_POWER");
1187 return 0;
1191 static int btmrvl_sdio_resume(struct device *dev)
1193 struct sdio_func *func = dev_to_sdio_func(dev);
1194 struct btmrvl_sdio_card *card;
1195 struct btmrvl_private *priv;
1196 mmc_pm_flag_t pm_flags;
1197 struct hci_dev *hcidev;
1199 if (func) {
1200 pm_flags = sdio_get_host_pm_caps(func);
1201 BT_DBG("%s: resume: PM flags = 0x%x", sdio_func_id(func),
1202 pm_flags);
1203 card = sdio_get_drvdata(func);
1204 if (!card || !card->priv) {
1205 BT_ERR("card or priv structure is not valid");
1206 return 0;
1208 } else {
1209 BT_ERR("sdio_func is not specified");
1210 return 0;
1212 priv = card->priv;
1214 if (!priv->adapter->is_suspended) {
1215 BT_DBG("device already resumed");
1216 return 0;
1219 priv->adapter->is_suspended = false;
1220 hcidev = priv->btmrvl_dev.hcidev;
1221 BT_DBG("%s: SDIO resume", hcidev->name);
1222 hci_resume_dev(hcidev);
1223 priv->hw_wakeup_firmware(priv);
1224 priv->adapter->hs_state = HS_DEACTIVATED;
1225 BT_DBG("%s: HS DEACTIVATED in resume!", hcidev->name);
1227 return 0;
1230 static const struct dev_pm_ops btmrvl_sdio_pm_ops = {
1231 .suspend = btmrvl_sdio_suspend,
1232 .resume = btmrvl_sdio_resume,
1235 static struct sdio_driver bt_mrvl_sdio = {
1236 .name = "btmrvl_sdio",
1237 .id_table = btmrvl_sdio_ids,
1238 .probe = btmrvl_sdio_probe,
1239 .remove = btmrvl_sdio_remove,
1240 .drv = {
1241 .owner = THIS_MODULE,
1242 .pm = &btmrvl_sdio_pm_ops,
1246 static int __init btmrvl_sdio_init_module(void)
1248 if (sdio_register_driver(&bt_mrvl_sdio) != 0) {
1249 BT_ERR("SDIO Driver Registration Failed");
1250 return -ENODEV;
1253 /* Clear the flag in case user removes the card. */
1254 user_rmmod = 0;
1256 return 0;
1259 static void __exit btmrvl_sdio_exit_module(void)
1261 /* Set the flag as user is removing this module. */
1262 user_rmmod = 1;
1264 sdio_unregister_driver(&bt_mrvl_sdio);
1267 module_init(btmrvl_sdio_init_module);
1268 module_exit(btmrvl_sdio_exit_module);
1270 MODULE_AUTHOR("Marvell International Ltd.");
1271 MODULE_DESCRIPTION("Marvell BT-over-SDIO driver ver " VERSION);
1272 MODULE_VERSION(VERSION);
1273 MODULE_LICENSE("GPL v2");
1274 MODULE_FIRMWARE("mrvl/sd8688_helper.bin");
1275 MODULE_FIRMWARE("mrvl/sd8688.bin");
1276 MODULE_FIRMWARE("mrvl/sd8787_uapsta.bin");
1277 MODULE_FIRMWARE("mrvl/sd8797_uapsta.bin");
1278 MODULE_FIRMWARE("mrvl/sd8897_uapsta.bin");