Linux 4.1.16
[linux/fpc-iii.git] / drivers / bluetooth / btmrvl_sdio.c
blob01d6da577eeb0713127f57c264e6345b5055781d
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>
27 #include <linux/devcoredump.h>
29 #include <net/bluetooth/bluetooth.h>
30 #include <net/bluetooth/hci_core.h>
32 #include "btmrvl_drv.h"
33 #include "btmrvl_sdio.h"
35 #define VERSION "1.0"
37 static struct memory_type_mapping mem_type_mapping_tbl[] = {
38 {"ITCM", NULL, 0, 0xF0},
39 {"DTCM", NULL, 0, 0xF1},
40 {"SQRAM", NULL, 0, 0xF2},
41 {"APU", NULL, 0, 0xF3},
42 {"CIU", NULL, 0, 0xF4},
43 {"ICU", NULL, 0, 0xF5},
44 {"MAC", NULL, 0, 0xF6},
45 {"EXT7", NULL, 0, 0xF7},
46 {"EXT8", NULL, 0, 0xF8},
47 {"EXT9", NULL, 0, 0xF9},
48 {"EXT10", NULL, 0, 0xFA},
49 {"EXT11", NULL, 0, 0xFB},
50 {"EXT12", NULL, 0, 0xFC},
51 {"EXT13", NULL, 0, 0xFD},
52 {"EXTLAST", NULL, 0, 0xFE},
55 /* The btmrvl_sdio_remove() callback function is called
56 * when user removes this module from kernel space or ejects
57 * the card from the slot. The driver handles these 2 cases
58 * differently.
59 * If the user is removing the module, a MODULE_SHUTDOWN_REQ
60 * command is sent to firmware and interrupt will be disabled.
61 * If the card is removed, there is no need to send command
62 * or disable interrupt.
64 * The variable 'user_rmmod' is used to distinguish these two
65 * scenarios. This flag is initialized as FALSE in case the card
66 * is removed, and will be set to TRUE for module removal when
67 * module_exit function is called.
69 static u8 user_rmmod;
70 static u8 sdio_ireg;
72 static const struct btmrvl_sdio_card_reg btmrvl_reg_8688 = {
73 .cfg = 0x03,
74 .host_int_mask = 0x04,
75 .host_intstatus = 0x05,
76 .card_status = 0x20,
77 .sq_read_base_addr_a0 = 0x10,
78 .sq_read_base_addr_a1 = 0x11,
79 .card_fw_status0 = 0x40,
80 .card_fw_status1 = 0x41,
81 .card_rx_len = 0x42,
82 .card_rx_unit = 0x43,
83 .io_port_0 = 0x00,
84 .io_port_1 = 0x01,
85 .io_port_2 = 0x02,
86 .int_read_to_clear = false,
88 static const struct btmrvl_sdio_card_reg btmrvl_reg_87xx = {
89 .cfg = 0x00,
90 .host_int_mask = 0x02,
91 .host_intstatus = 0x03,
92 .card_status = 0x30,
93 .sq_read_base_addr_a0 = 0x40,
94 .sq_read_base_addr_a1 = 0x41,
95 .card_revision = 0x5c,
96 .card_fw_status0 = 0x60,
97 .card_fw_status1 = 0x61,
98 .card_rx_len = 0x62,
99 .card_rx_unit = 0x63,
100 .io_port_0 = 0x78,
101 .io_port_1 = 0x79,
102 .io_port_2 = 0x7a,
103 .int_read_to_clear = false,
106 static const struct btmrvl_sdio_card_reg btmrvl_reg_8887 = {
107 .cfg = 0x00,
108 .host_int_mask = 0x08,
109 .host_intstatus = 0x0C,
110 .card_status = 0x5C,
111 .sq_read_base_addr_a0 = 0x6C,
112 .sq_read_base_addr_a1 = 0x6D,
113 .card_revision = 0xC8,
114 .card_fw_status0 = 0x88,
115 .card_fw_status1 = 0x89,
116 .card_rx_len = 0x8A,
117 .card_rx_unit = 0x8B,
118 .io_port_0 = 0xE4,
119 .io_port_1 = 0xE5,
120 .io_port_2 = 0xE6,
121 .int_read_to_clear = true,
122 .host_int_rsr = 0x04,
123 .card_misc_cfg = 0xD8,
126 static const struct btmrvl_sdio_card_reg btmrvl_reg_8897 = {
127 .cfg = 0x00,
128 .host_int_mask = 0x02,
129 .host_intstatus = 0x03,
130 .card_status = 0x50,
131 .sq_read_base_addr_a0 = 0x60,
132 .sq_read_base_addr_a1 = 0x61,
133 .card_revision = 0xbc,
134 .card_fw_status0 = 0xc0,
135 .card_fw_status1 = 0xc1,
136 .card_rx_len = 0xc2,
137 .card_rx_unit = 0xc3,
138 .io_port_0 = 0xd8,
139 .io_port_1 = 0xd9,
140 .io_port_2 = 0xda,
141 .int_read_to_clear = true,
142 .host_int_rsr = 0x01,
143 .card_misc_cfg = 0xcc,
144 .fw_dump_ctrl = 0xe2,
145 .fw_dump_start = 0xe3,
146 .fw_dump_end = 0xea,
149 static const struct btmrvl_sdio_device btmrvl_sdio_sd8688 = {
150 .helper = "mrvl/sd8688_helper.bin",
151 .firmware = "mrvl/sd8688.bin",
152 .reg = &btmrvl_reg_8688,
153 .support_pscan_win_report = false,
154 .sd_blksz_fw_dl = 64,
155 .supports_fw_dump = false,
158 static const struct btmrvl_sdio_device btmrvl_sdio_sd8787 = {
159 .helper = NULL,
160 .firmware = "mrvl/sd8787_uapsta.bin",
161 .reg = &btmrvl_reg_87xx,
162 .support_pscan_win_report = false,
163 .sd_blksz_fw_dl = 256,
164 .supports_fw_dump = false,
167 static const struct btmrvl_sdio_device btmrvl_sdio_sd8797 = {
168 .helper = NULL,
169 .firmware = "mrvl/sd8797_uapsta.bin",
170 .reg = &btmrvl_reg_87xx,
171 .support_pscan_win_report = false,
172 .sd_blksz_fw_dl = 256,
173 .supports_fw_dump = false,
176 static const struct btmrvl_sdio_device btmrvl_sdio_sd8887 = {
177 .helper = NULL,
178 .firmware = "mrvl/sd8887_uapsta.bin",
179 .reg = &btmrvl_reg_8887,
180 .support_pscan_win_report = true,
181 .sd_blksz_fw_dl = 256,
182 .supports_fw_dump = false,
185 static const struct btmrvl_sdio_device btmrvl_sdio_sd8897 = {
186 .helper = NULL,
187 .firmware = "mrvl/sd8897_uapsta.bin",
188 .reg = &btmrvl_reg_8897,
189 .support_pscan_win_report = true,
190 .sd_blksz_fw_dl = 256,
191 .supports_fw_dump = true,
194 static const struct sdio_device_id btmrvl_sdio_ids[] = {
195 /* Marvell SD8688 Bluetooth device */
196 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x9105),
197 .driver_data = (unsigned long) &btmrvl_sdio_sd8688 },
198 /* Marvell SD8787 Bluetooth device */
199 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x911A),
200 .driver_data = (unsigned long) &btmrvl_sdio_sd8787 },
201 /* Marvell SD8787 Bluetooth AMP device */
202 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x911B),
203 .driver_data = (unsigned long) &btmrvl_sdio_sd8787 },
204 /* Marvell SD8797 Bluetooth device */
205 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x912A),
206 .driver_data = (unsigned long) &btmrvl_sdio_sd8797 },
207 /* Marvell SD8887 Bluetooth device */
208 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x9136),
209 .driver_data = (unsigned long)&btmrvl_sdio_sd8887 },
210 /* Marvell SD8897 Bluetooth device */
211 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x912E),
212 .driver_data = (unsigned long) &btmrvl_sdio_sd8897 },
214 { } /* Terminating entry */
217 MODULE_DEVICE_TABLE(sdio, btmrvl_sdio_ids);
219 static int btmrvl_sdio_get_rx_unit(struct btmrvl_sdio_card *card)
221 u8 reg;
222 int ret;
224 reg = sdio_readb(card->func, card->reg->card_rx_unit, &ret);
225 if (!ret)
226 card->rx_unit = reg;
228 return ret;
231 static int btmrvl_sdio_read_fw_status(struct btmrvl_sdio_card *card, u16 *dat)
233 u8 fws0, fws1;
234 int ret;
236 *dat = 0;
238 fws0 = sdio_readb(card->func, card->reg->card_fw_status0, &ret);
239 if (ret)
240 return -EIO;
242 fws1 = sdio_readb(card->func, card->reg->card_fw_status1, &ret);
243 if (ret)
244 return -EIO;
246 *dat = (((u16) fws1) << 8) | fws0;
248 return 0;
251 static int btmrvl_sdio_read_rx_len(struct btmrvl_sdio_card *card, u16 *dat)
253 u8 reg;
254 int ret;
256 reg = sdio_readb(card->func, card->reg->card_rx_len, &ret);
257 if (!ret)
258 *dat = (u16) reg << card->rx_unit;
260 return ret;
263 static int btmrvl_sdio_enable_host_int_mask(struct btmrvl_sdio_card *card,
264 u8 mask)
266 int ret;
268 sdio_writeb(card->func, mask, card->reg->host_int_mask, &ret);
269 if (ret) {
270 BT_ERR("Unable to enable the host interrupt!");
271 ret = -EIO;
274 return ret;
277 static int btmrvl_sdio_disable_host_int_mask(struct btmrvl_sdio_card *card,
278 u8 mask)
280 u8 host_int_mask;
281 int ret;
283 host_int_mask = sdio_readb(card->func, card->reg->host_int_mask, &ret);
284 if (ret)
285 return -EIO;
287 host_int_mask &= ~mask;
289 sdio_writeb(card->func, host_int_mask, card->reg->host_int_mask, &ret);
290 if (ret < 0) {
291 BT_ERR("Unable to disable the host interrupt!");
292 return -EIO;
295 return 0;
298 static int btmrvl_sdio_poll_card_status(struct btmrvl_sdio_card *card, u8 bits)
300 unsigned int tries;
301 u8 status;
302 int ret;
304 for (tries = 0; tries < MAX_POLL_TRIES * 1000; tries++) {
305 status = sdio_readb(card->func, card->reg->card_status, &ret);
306 if (ret)
307 goto failed;
308 if ((status & bits) == bits)
309 return ret;
311 udelay(1);
314 ret = -ETIMEDOUT;
316 failed:
317 BT_ERR("FAILED! ret=%d", ret);
319 return ret;
322 static int btmrvl_sdio_verify_fw_download(struct btmrvl_sdio_card *card,
323 int pollnum)
325 u16 firmwarestat;
326 int tries, ret;
328 /* Wait for firmware to become ready */
329 for (tries = 0; tries < pollnum; tries++) {
330 sdio_claim_host(card->func);
331 ret = btmrvl_sdio_read_fw_status(card, &firmwarestat);
332 sdio_release_host(card->func);
333 if (ret < 0)
334 continue;
336 if (firmwarestat == FIRMWARE_READY)
337 return 0;
339 msleep(10);
342 return -ETIMEDOUT;
345 static int btmrvl_sdio_download_helper(struct btmrvl_sdio_card *card)
347 const struct firmware *fw_helper = NULL;
348 const u8 *helper = NULL;
349 int ret;
350 void *tmphlprbuf = NULL;
351 int tmphlprbufsz, hlprblknow, helperlen;
352 u8 *helperbuf;
353 u32 tx_len;
355 ret = request_firmware(&fw_helper, card->helper,
356 &card->func->dev);
357 if ((ret < 0) || !fw_helper) {
358 BT_ERR("request_firmware(helper) failed, error code = %d",
359 ret);
360 ret = -ENOENT;
361 goto done;
364 helper = fw_helper->data;
365 helperlen = fw_helper->size;
367 BT_DBG("Downloading helper image (%d bytes), block size %d bytes",
368 helperlen, SDIO_BLOCK_SIZE);
370 tmphlprbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
372 tmphlprbuf = kzalloc(tmphlprbufsz, GFP_KERNEL);
373 if (!tmphlprbuf) {
374 BT_ERR("Unable to allocate buffer for helper."
375 " Terminating download");
376 ret = -ENOMEM;
377 goto done;
380 helperbuf = (u8 *) ALIGN_ADDR(tmphlprbuf, BTSDIO_DMA_ALIGN);
382 /* Perform helper data transfer */
383 tx_len = (FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE)
384 - SDIO_HEADER_LEN;
385 hlprblknow = 0;
387 do {
388 ret = btmrvl_sdio_poll_card_status(card,
389 CARD_IO_READY | DN_LD_CARD_RDY);
390 if (ret < 0) {
391 BT_ERR("Helper download poll status timeout @ %d",
392 hlprblknow);
393 goto done;
396 /* Check if there is more data? */
397 if (hlprblknow >= helperlen)
398 break;
400 if (helperlen - hlprblknow < tx_len)
401 tx_len = helperlen - hlprblknow;
403 /* Little-endian */
404 helperbuf[0] = ((tx_len & 0x000000ff) >> 0);
405 helperbuf[1] = ((tx_len & 0x0000ff00) >> 8);
406 helperbuf[2] = ((tx_len & 0x00ff0000) >> 16);
407 helperbuf[3] = ((tx_len & 0xff000000) >> 24);
409 memcpy(&helperbuf[SDIO_HEADER_LEN], &helper[hlprblknow],
410 tx_len);
412 /* Now send the data */
413 ret = sdio_writesb(card->func, card->ioport, helperbuf,
414 FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE);
415 if (ret < 0) {
416 BT_ERR("IO error during helper download @ %d",
417 hlprblknow);
418 goto done;
421 hlprblknow += tx_len;
422 } while (true);
424 BT_DBG("Transferring helper image EOF block");
426 memset(helperbuf, 0x0, SDIO_BLOCK_SIZE);
428 ret = sdio_writesb(card->func, card->ioport, helperbuf,
429 SDIO_BLOCK_SIZE);
430 if (ret < 0) {
431 BT_ERR("IO error in writing helper image EOF block");
432 goto done;
435 ret = 0;
437 done:
438 kfree(tmphlprbuf);
439 release_firmware(fw_helper);
440 return ret;
443 static int btmrvl_sdio_download_fw_w_helper(struct btmrvl_sdio_card *card)
445 const struct firmware *fw_firmware = NULL;
446 const u8 *firmware = NULL;
447 int firmwarelen, tmpfwbufsz, ret;
448 unsigned int tries, offset;
449 u8 base0, base1;
450 void *tmpfwbuf = NULL;
451 u8 *fwbuf;
452 u16 len, blksz_dl = card->sd_blksz_fw_dl;
453 int txlen = 0, tx_blocks = 0, count = 0;
455 ret = request_firmware(&fw_firmware, card->firmware,
456 &card->func->dev);
457 if ((ret < 0) || !fw_firmware) {
458 BT_ERR("request_firmware(firmware) failed, error code = %d",
459 ret);
460 ret = -ENOENT;
461 goto done;
464 firmware = fw_firmware->data;
465 firmwarelen = fw_firmware->size;
467 BT_DBG("Downloading FW image (%d bytes)", firmwarelen);
469 tmpfwbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
470 tmpfwbuf = kzalloc(tmpfwbufsz, GFP_KERNEL);
471 if (!tmpfwbuf) {
472 BT_ERR("Unable to allocate buffer for firmware."
473 " Terminating download");
474 ret = -ENOMEM;
475 goto done;
478 /* Ensure aligned firmware buffer */
479 fwbuf = (u8 *) ALIGN_ADDR(tmpfwbuf, BTSDIO_DMA_ALIGN);
481 /* Perform firmware data transfer */
482 offset = 0;
483 do {
484 ret = btmrvl_sdio_poll_card_status(card,
485 CARD_IO_READY | DN_LD_CARD_RDY);
486 if (ret < 0) {
487 BT_ERR("FW download with helper poll status"
488 " timeout @ %d", offset);
489 goto done;
492 /* Check if there is more data ? */
493 if (offset >= firmwarelen)
494 break;
496 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
497 base0 = sdio_readb(card->func,
498 card->reg->sq_read_base_addr_a0, &ret);
499 if (ret) {
500 BT_ERR("BASE0 register read failed:"
501 " base0 = 0x%04X(%d)."
502 " Terminating download",
503 base0, base0);
504 ret = -EIO;
505 goto done;
507 base1 = sdio_readb(card->func,
508 card->reg->sq_read_base_addr_a1, &ret);
509 if (ret) {
510 BT_ERR("BASE1 register read failed:"
511 " base1 = 0x%04X(%d)."
512 " Terminating download",
513 base1, base1);
514 ret = -EIO;
515 goto done;
518 len = (((u16) base1) << 8) | base0;
519 if (len)
520 break;
522 udelay(10);
525 if (!len)
526 break;
527 else if (len > BTM_UPLD_SIZE) {
528 BT_ERR("FW download failure @%d, invalid length %d",
529 offset, len);
530 ret = -EINVAL;
531 goto done;
534 txlen = len;
536 if (len & BIT(0)) {
537 count++;
538 if (count > MAX_WRITE_IOMEM_RETRY) {
539 BT_ERR("FW download failure @%d, "
540 "over max retry count", offset);
541 ret = -EIO;
542 goto done;
544 BT_ERR("FW CRC error indicated by the helper: "
545 "len = 0x%04X, txlen = %d", len, txlen);
546 len &= ~BIT(0);
547 /* Set txlen to 0 so as to resend from same offset */
548 txlen = 0;
549 } else {
550 count = 0;
552 /* Last block ? */
553 if (firmwarelen - offset < txlen)
554 txlen = firmwarelen - offset;
556 tx_blocks = DIV_ROUND_UP(txlen, blksz_dl);
558 memcpy(fwbuf, &firmware[offset], txlen);
561 ret = sdio_writesb(card->func, card->ioport, fwbuf,
562 tx_blocks * blksz_dl);
564 if (ret < 0) {
565 BT_ERR("FW download, writesb(%d) failed @%d",
566 count, offset);
567 sdio_writeb(card->func, HOST_CMD53_FIN,
568 card->reg->cfg, &ret);
569 if (ret)
570 BT_ERR("writeb failed (CFG)");
573 offset += txlen;
574 } while (true);
576 BT_INFO("FW download over, size %d bytes", offset);
578 ret = 0;
580 done:
581 kfree(tmpfwbuf);
582 release_firmware(fw_firmware);
583 return ret;
586 static int btmrvl_sdio_card_to_host(struct btmrvl_private *priv)
588 u16 buf_len = 0;
589 int ret, num_blocks, blksz;
590 struct sk_buff *skb = NULL;
591 u32 type;
592 u8 *payload = NULL;
593 struct hci_dev *hdev = priv->btmrvl_dev.hcidev;
594 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
596 if (!card || !card->func) {
597 BT_ERR("card or function is NULL!");
598 ret = -EINVAL;
599 goto exit;
602 /* Read the length of data to be transferred */
603 ret = btmrvl_sdio_read_rx_len(card, &buf_len);
604 if (ret < 0) {
605 BT_ERR("read rx_len failed");
606 ret = -EIO;
607 goto exit;
610 blksz = SDIO_BLOCK_SIZE;
611 num_blocks = DIV_ROUND_UP(buf_len, blksz);
613 if (buf_len <= SDIO_HEADER_LEN
614 || (num_blocks * blksz) > ALLOC_BUF_SIZE) {
615 BT_ERR("invalid packet length: %d", buf_len);
616 ret = -EINVAL;
617 goto exit;
620 /* Allocate buffer */
621 skb = bt_skb_alloc(num_blocks * blksz + BTSDIO_DMA_ALIGN, GFP_ATOMIC);
622 if (skb == NULL) {
623 BT_ERR("No free skb");
624 ret = -ENOMEM;
625 goto exit;
628 if ((unsigned long) skb->data & (BTSDIO_DMA_ALIGN - 1)) {
629 skb_put(skb, (unsigned long) skb->data &
630 (BTSDIO_DMA_ALIGN - 1));
631 skb_pull(skb, (unsigned long) skb->data &
632 (BTSDIO_DMA_ALIGN - 1));
635 payload = skb->data;
637 ret = sdio_readsb(card->func, payload, card->ioport,
638 num_blocks * blksz);
639 if (ret < 0) {
640 BT_ERR("readsb failed: %d", ret);
641 ret = -EIO;
642 goto exit;
645 /* This is SDIO specific header length: byte[2][1][0], type: byte[3]
646 * (HCI_COMMAND = 1, ACL_DATA = 2, SCO_DATA = 3, 0xFE = Vendor)
649 buf_len = payload[0];
650 buf_len |= payload[1] << 8;
651 buf_len |= payload[2] << 16;
653 if (buf_len > blksz * num_blocks) {
654 BT_ERR("Skip incorrect packet: hdrlen %d buffer %d",
655 buf_len, blksz * num_blocks);
656 ret = -EIO;
657 goto exit;
660 type = payload[3];
662 switch (type) {
663 case HCI_ACLDATA_PKT:
664 case HCI_SCODATA_PKT:
665 case HCI_EVENT_PKT:
666 bt_cb(skb)->pkt_type = type;
667 skb_put(skb, buf_len);
668 skb_pull(skb, SDIO_HEADER_LEN);
670 if (type == HCI_EVENT_PKT) {
671 if (btmrvl_check_evtpkt(priv, skb))
672 hci_recv_frame(hdev, skb);
673 } else {
674 hci_recv_frame(hdev, skb);
677 hdev->stat.byte_rx += buf_len;
678 break;
680 case MRVL_VENDOR_PKT:
681 bt_cb(skb)->pkt_type = HCI_VENDOR_PKT;
682 skb_put(skb, buf_len);
683 skb_pull(skb, SDIO_HEADER_LEN);
685 if (btmrvl_process_event(priv, skb))
686 hci_recv_frame(hdev, skb);
688 hdev->stat.byte_rx += buf_len;
689 break;
691 default:
692 BT_ERR("Unknown packet type:%d", type);
693 BT_ERR("hex: %*ph", blksz * num_blocks, payload);
695 kfree_skb(skb);
696 skb = NULL;
697 break;
700 exit:
701 if (ret) {
702 hdev->stat.err_rx++;
703 kfree_skb(skb);
706 return ret;
709 static int btmrvl_sdio_process_int_status(struct btmrvl_private *priv)
711 ulong flags;
712 u8 ireg;
713 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
715 spin_lock_irqsave(&priv->driver_lock, flags);
716 ireg = sdio_ireg;
717 sdio_ireg = 0;
718 spin_unlock_irqrestore(&priv->driver_lock, flags);
720 sdio_claim_host(card->func);
721 if (ireg & DN_LD_HOST_INT_STATUS) {
722 if (priv->btmrvl_dev.tx_dnld_rdy)
723 BT_DBG("tx_done already received: "
724 " int_status=0x%x", ireg);
725 else
726 priv->btmrvl_dev.tx_dnld_rdy = true;
729 if (ireg & UP_LD_HOST_INT_STATUS)
730 btmrvl_sdio_card_to_host(priv);
732 sdio_release_host(card->func);
734 return 0;
737 static int btmrvl_sdio_read_to_clear(struct btmrvl_sdio_card *card, u8 *ireg)
739 struct btmrvl_adapter *adapter = card->priv->adapter;
740 int ret;
742 ret = sdio_readsb(card->func, adapter->hw_regs, 0, SDIO_BLOCK_SIZE);
743 if (ret) {
744 BT_ERR("sdio_readsb: read int hw_regs failed: %d", ret);
745 return ret;
748 *ireg = adapter->hw_regs[card->reg->host_intstatus];
749 BT_DBG("hw_regs[%#x]=%#x", card->reg->host_intstatus, *ireg);
751 return 0;
754 static int btmrvl_sdio_write_to_clear(struct btmrvl_sdio_card *card, u8 *ireg)
756 int ret;
758 *ireg = sdio_readb(card->func, card->reg->host_intstatus, &ret);
759 if (ret) {
760 BT_ERR("sdio_readb: read int status failed: %d", ret);
761 return ret;
764 if (*ireg) {
766 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
767 * Clear the interrupt status register and re-enable the
768 * interrupt.
770 BT_DBG("int_status = 0x%x", *ireg);
772 sdio_writeb(card->func, ~(*ireg) & (DN_LD_HOST_INT_STATUS |
773 UP_LD_HOST_INT_STATUS),
774 card->reg->host_intstatus, &ret);
775 if (ret) {
776 BT_ERR("sdio_writeb: clear int status failed: %d", ret);
777 return ret;
781 return 0;
784 static void btmrvl_sdio_interrupt(struct sdio_func *func)
786 struct btmrvl_private *priv;
787 struct btmrvl_sdio_card *card;
788 ulong flags;
789 u8 ireg = 0;
790 int ret;
792 card = sdio_get_drvdata(func);
793 if (!card || !card->priv) {
794 BT_ERR("sbi_interrupt(%p) card or priv is NULL, card=%p",
795 func, card);
796 return;
799 priv = card->priv;
801 if (priv->surprise_removed)
802 return;
804 if (card->reg->int_read_to_clear)
805 ret = btmrvl_sdio_read_to_clear(card, &ireg);
806 else
807 ret = btmrvl_sdio_write_to_clear(card, &ireg);
809 if (ret)
810 return;
812 spin_lock_irqsave(&priv->driver_lock, flags);
813 sdio_ireg |= ireg;
814 spin_unlock_irqrestore(&priv->driver_lock, flags);
816 btmrvl_interrupt(priv);
819 static int btmrvl_sdio_register_dev(struct btmrvl_sdio_card *card)
821 struct sdio_func *func;
822 u8 reg;
823 int ret = 0;
825 if (!card || !card->func) {
826 BT_ERR("Error: card or function is NULL!");
827 ret = -EINVAL;
828 goto failed;
831 func = card->func;
833 sdio_claim_host(func);
835 ret = sdio_enable_func(func);
836 if (ret) {
837 BT_ERR("sdio_enable_func() failed: ret=%d", ret);
838 ret = -EIO;
839 goto release_host;
842 ret = sdio_claim_irq(func, btmrvl_sdio_interrupt);
843 if (ret) {
844 BT_ERR("sdio_claim_irq failed: ret=%d", ret);
845 ret = -EIO;
846 goto disable_func;
849 ret = sdio_set_block_size(card->func, SDIO_BLOCK_SIZE);
850 if (ret) {
851 BT_ERR("cannot set SDIO block size");
852 ret = -EIO;
853 goto release_irq;
856 reg = sdio_readb(func, card->reg->io_port_0, &ret);
857 if (ret < 0) {
858 ret = -EIO;
859 goto release_irq;
862 card->ioport = reg;
864 reg = sdio_readb(func, card->reg->io_port_1, &ret);
865 if (ret < 0) {
866 ret = -EIO;
867 goto release_irq;
870 card->ioport |= (reg << 8);
872 reg = sdio_readb(func, card->reg->io_port_2, &ret);
873 if (ret < 0) {
874 ret = -EIO;
875 goto release_irq;
878 card->ioport |= (reg << 16);
880 BT_DBG("SDIO FUNC%d IO port: 0x%x", func->num, card->ioport);
882 if (card->reg->int_read_to_clear) {
883 reg = sdio_readb(func, card->reg->host_int_rsr, &ret);
884 if (ret < 0) {
885 ret = -EIO;
886 goto release_irq;
888 sdio_writeb(func, reg | 0x3f, card->reg->host_int_rsr, &ret);
889 if (ret < 0) {
890 ret = -EIO;
891 goto release_irq;
894 reg = sdio_readb(func, card->reg->card_misc_cfg, &ret);
895 if (ret < 0) {
896 ret = -EIO;
897 goto release_irq;
899 sdio_writeb(func, reg | 0x10, card->reg->card_misc_cfg, &ret);
900 if (ret < 0) {
901 ret = -EIO;
902 goto release_irq;
906 sdio_set_drvdata(func, card);
908 sdio_release_host(func);
910 return 0;
912 release_irq:
913 sdio_release_irq(func);
915 disable_func:
916 sdio_disable_func(func);
918 release_host:
919 sdio_release_host(func);
921 failed:
922 return ret;
925 static int btmrvl_sdio_unregister_dev(struct btmrvl_sdio_card *card)
927 if (card && card->func) {
928 sdio_claim_host(card->func);
929 sdio_release_irq(card->func);
930 sdio_disable_func(card->func);
931 sdio_release_host(card->func);
932 sdio_set_drvdata(card->func, NULL);
935 return 0;
938 static int btmrvl_sdio_enable_host_int(struct btmrvl_sdio_card *card)
940 int ret;
942 if (!card || !card->func)
943 return -EINVAL;
945 sdio_claim_host(card->func);
947 ret = btmrvl_sdio_enable_host_int_mask(card, HIM_ENABLE);
949 btmrvl_sdio_get_rx_unit(card);
951 sdio_release_host(card->func);
953 return ret;
956 static int btmrvl_sdio_disable_host_int(struct btmrvl_sdio_card *card)
958 int ret;
960 if (!card || !card->func)
961 return -EINVAL;
963 sdio_claim_host(card->func);
965 ret = btmrvl_sdio_disable_host_int_mask(card, HIM_DISABLE);
967 sdio_release_host(card->func);
969 return ret;
972 static int btmrvl_sdio_host_to_card(struct btmrvl_private *priv,
973 u8 *payload, u16 nb)
975 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
976 int ret = 0;
977 int buf_block_len;
978 int blksz;
979 int i = 0;
980 u8 *buf = NULL;
981 void *tmpbuf = NULL;
982 int tmpbufsz;
984 if (!card || !card->func) {
985 BT_ERR("card or function is NULL!");
986 return -EINVAL;
989 buf = payload;
990 if ((unsigned long) payload & (BTSDIO_DMA_ALIGN - 1)) {
991 tmpbufsz = ALIGN_SZ(nb, BTSDIO_DMA_ALIGN);
992 tmpbuf = kzalloc(tmpbufsz, GFP_KERNEL);
993 if (!tmpbuf)
994 return -ENOMEM;
995 buf = (u8 *) ALIGN_ADDR(tmpbuf, BTSDIO_DMA_ALIGN);
996 memcpy(buf, payload, nb);
999 blksz = SDIO_BLOCK_SIZE;
1000 buf_block_len = DIV_ROUND_UP(nb, blksz);
1002 sdio_claim_host(card->func);
1004 do {
1005 /* Transfer data to card */
1006 ret = sdio_writesb(card->func, card->ioport, buf,
1007 buf_block_len * blksz);
1008 if (ret < 0) {
1009 i++;
1010 BT_ERR("i=%d writesb failed: %d", i, ret);
1011 BT_ERR("hex: %*ph", nb, payload);
1012 ret = -EIO;
1013 if (i > MAX_WRITE_IOMEM_RETRY)
1014 goto exit;
1016 } while (ret);
1018 priv->btmrvl_dev.tx_dnld_rdy = false;
1020 exit:
1021 sdio_release_host(card->func);
1022 kfree(tmpbuf);
1024 return ret;
1027 static int btmrvl_sdio_download_fw(struct btmrvl_sdio_card *card)
1029 int ret;
1030 u8 fws0;
1031 int pollnum = MAX_POLL_TRIES;
1033 if (!card || !card->func) {
1034 BT_ERR("card or function is NULL!");
1035 return -EINVAL;
1038 if (!btmrvl_sdio_verify_fw_download(card, 1)) {
1039 BT_DBG("Firmware already downloaded!");
1040 return 0;
1043 sdio_claim_host(card->func);
1045 /* Check if other function driver is downloading the firmware */
1046 fws0 = sdio_readb(card->func, card->reg->card_fw_status0, &ret);
1047 if (ret) {
1048 BT_ERR("Failed to read FW downloading status!");
1049 ret = -EIO;
1050 goto done;
1052 if (fws0) {
1053 BT_DBG("BT not the winner (%#x). Skip FW downloading", fws0);
1055 /* Give other function more time to download the firmware */
1056 pollnum *= 10;
1057 } else {
1058 if (card->helper) {
1059 ret = btmrvl_sdio_download_helper(card);
1060 if (ret) {
1061 BT_ERR("Failed to download helper!");
1062 ret = -EIO;
1063 goto done;
1067 if (btmrvl_sdio_download_fw_w_helper(card)) {
1068 BT_ERR("Failed to download firmware!");
1069 ret = -EIO;
1070 goto done;
1074 sdio_release_host(card->func);
1077 * winner or not, with this test the FW synchronizes when the
1078 * module can continue its initialization
1080 if (btmrvl_sdio_verify_fw_download(card, pollnum)) {
1081 BT_ERR("FW failed to be active in time!");
1082 return -ETIMEDOUT;
1085 return 0;
1087 done:
1088 sdio_release_host(card->func);
1089 return ret;
1092 static int btmrvl_sdio_wakeup_fw(struct btmrvl_private *priv)
1094 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
1095 int ret = 0;
1097 if (!card || !card->func) {
1098 BT_ERR("card or function is NULL!");
1099 return -EINVAL;
1102 sdio_claim_host(card->func);
1104 sdio_writeb(card->func, HOST_POWER_UP, card->reg->cfg, &ret);
1106 sdio_release_host(card->func);
1108 BT_DBG("wake up firmware");
1110 return ret;
1113 static void btmrvl_sdio_dump_regs(struct btmrvl_private *priv)
1115 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
1116 int ret = 0;
1117 unsigned int reg, reg_start, reg_end;
1118 char buf[256], *ptr;
1119 u8 loop, func, data;
1120 int MAX_LOOP = 2;
1122 btmrvl_sdio_wakeup_fw(priv);
1123 sdio_claim_host(card->func);
1125 for (loop = 0; loop < MAX_LOOP; loop++) {
1126 memset(buf, 0, sizeof(buf));
1127 ptr = buf;
1129 if (loop == 0) {
1130 /* Read the registers of SDIO function0 */
1131 func = loop;
1132 reg_start = 0;
1133 reg_end = 9;
1134 } else {
1135 func = 2;
1136 reg_start = 0;
1137 reg_end = 0x09;
1140 ptr += sprintf(ptr, "SDIO Func%d (%#x-%#x): ",
1141 func, reg_start, reg_end);
1142 for (reg = reg_start; reg <= reg_end; reg++) {
1143 if (func == 0)
1144 data = sdio_f0_readb(card->func, reg, &ret);
1145 else
1146 data = sdio_readb(card->func, reg, &ret);
1148 if (!ret) {
1149 ptr += sprintf(ptr, "%02x ", data);
1150 } else {
1151 ptr += sprintf(ptr, "ERR");
1152 break;
1156 BT_INFO("%s", buf);
1159 sdio_release_host(card->func);
1162 /* This function read/write firmware */
1163 static enum
1164 rdwr_status btmrvl_sdio_rdwr_firmware(struct btmrvl_private *priv,
1165 u8 doneflag)
1167 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
1168 int ret, tries;
1169 u8 ctrl_data = 0;
1171 sdio_writeb(card->func, FW_DUMP_HOST_READY, card->reg->fw_dump_ctrl,
1172 &ret);
1174 if (ret) {
1175 BT_ERR("SDIO write err");
1176 return RDWR_STATUS_FAILURE;
1179 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
1180 ctrl_data = sdio_readb(card->func, card->reg->fw_dump_ctrl,
1181 &ret);
1183 if (ret) {
1184 BT_ERR("SDIO read err");
1185 return RDWR_STATUS_FAILURE;
1188 if (ctrl_data == FW_DUMP_DONE)
1189 break;
1190 if (doneflag && ctrl_data == doneflag)
1191 return RDWR_STATUS_DONE;
1192 if (ctrl_data != FW_DUMP_HOST_READY) {
1193 BT_INFO("The ctrl reg was changed, re-try again!");
1194 sdio_writeb(card->func, FW_DUMP_HOST_READY,
1195 card->reg->fw_dump_ctrl, &ret);
1196 if (ret) {
1197 BT_ERR("SDIO write err");
1198 return RDWR_STATUS_FAILURE;
1201 usleep_range(100, 200);
1204 if (ctrl_data == FW_DUMP_HOST_READY) {
1205 BT_ERR("Fail to pull ctrl_data");
1206 return RDWR_STATUS_FAILURE;
1209 return RDWR_STATUS_SUCCESS;
1212 /* This function dump sdio register and memory data */
1213 static void btmrvl_sdio_dump_firmware(struct btmrvl_private *priv)
1215 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
1216 int ret = 0;
1217 unsigned int reg, reg_start, reg_end;
1218 enum rdwr_status stat;
1219 u8 *dbg_ptr, *end_ptr, *fw_dump_data, *fw_dump_ptr;
1220 u8 dump_num, idx, i, read_reg, doneflag = 0;
1221 u32 memory_size, fw_dump_len = 0;
1223 /* dump sdio register first */
1224 btmrvl_sdio_dump_regs(priv);
1226 if (!card->supports_fw_dump) {
1227 BT_ERR("Firmware dump not supported for this card!");
1228 return;
1231 for (idx = 0; idx < ARRAY_SIZE(mem_type_mapping_tbl); idx++) {
1232 struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
1234 if (entry->mem_ptr) {
1235 vfree(entry->mem_ptr);
1236 entry->mem_ptr = NULL;
1238 entry->mem_size = 0;
1241 btmrvl_sdio_wakeup_fw(priv);
1242 sdio_claim_host(card->func);
1244 BT_INFO("== btmrvl firmware dump start ==");
1246 stat = btmrvl_sdio_rdwr_firmware(priv, doneflag);
1247 if (stat == RDWR_STATUS_FAILURE)
1248 goto done;
1250 reg = card->reg->fw_dump_start;
1251 /* Read the number of the memories which will dump */
1252 dump_num = sdio_readb(card->func, reg, &ret);
1254 if (ret) {
1255 BT_ERR("SDIO read memory length err");
1256 goto done;
1259 /* Read the length of every memory which will dump */
1260 for (idx = 0; idx < dump_num; idx++) {
1261 struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
1263 stat = btmrvl_sdio_rdwr_firmware(priv, doneflag);
1264 if (stat == RDWR_STATUS_FAILURE)
1265 goto done;
1267 memory_size = 0;
1268 reg = card->reg->fw_dump_start;
1269 for (i = 0; i < 4; i++) {
1270 read_reg = sdio_readb(card->func, reg, &ret);
1271 if (ret) {
1272 BT_ERR("SDIO read err");
1273 goto done;
1275 memory_size |= (read_reg << i*8);
1276 reg++;
1279 if (memory_size == 0) {
1280 BT_INFO("Firmware dump finished!");
1281 break;
1284 BT_INFO("%s_SIZE=0x%x", entry->mem_name, memory_size);
1285 entry->mem_ptr = vzalloc(memory_size + 1);
1286 entry->mem_size = memory_size;
1287 if (!entry->mem_ptr) {
1288 BT_ERR("Vzalloc %s failed", entry->mem_name);
1289 goto done;
1292 fw_dump_len += (strlen("========Start dump ") +
1293 strlen(entry->mem_name) +
1294 strlen("========\n") +
1295 (memory_size + 1) +
1296 strlen("\n========End dump========\n"));
1298 dbg_ptr = entry->mem_ptr;
1299 end_ptr = dbg_ptr + memory_size;
1301 doneflag = entry->done_flag;
1302 BT_INFO("Start %s output, please wait...",
1303 entry->mem_name);
1305 do {
1306 stat = btmrvl_sdio_rdwr_firmware(priv, doneflag);
1307 if (stat == RDWR_STATUS_FAILURE)
1308 goto done;
1310 reg_start = card->reg->fw_dump_start;
1311 reg_end = card->reg->fw_dump_end;
1312 for (reg = reg_start; reg <= reg_end; reg++) {
1313 *dbg_ptr = sdio_readb(card->func, reg, &ret);
1314 if (ret) {
1315 BT_ERR("SDIO read err");
1316 goto done;
1318 if (dbg_ptr < end_ptr)
1319 dbg_ptr++;
1320 else
1321 BT_ERR("Allocated buffer not enough");
1324 if (stat != RDWR_STATUS_DONE) {
1325 continue;
1326 } else {
1327 BT_INFO("%s done: size=0x%tx",
1328 entry->mem_name,
1329 dbg_ptr - entry->mem_ptr);
1330 break;
1332 } while (1);
1335 BT_INFO("== btmrvl firmware dump end ==");
1337 done:
1338 sdio_release_host(card->func);
1340 if (fw_dump_len == 0)
1341 return;
1343 fw_dump_data = vzalloc(fw_dump_len+1);
1344 if (!fw_dump_data) {
1345 BT_ERR("Vzalloc fw_dump_data fail!");
1346 return;
1348 fw_dump_ptr = fw_dump_data;
1350 /* Dump all the memory data into single file, a userspace script will
1351 be used to split all the memory data to multiple files*/
1352 BT_INFO("== btmrvl firmware dump to /sys/class/devcoredump start");
1353 for (idx = 0; idx < dump_num; idx++) {
1354 struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
1356 if (entry->mem_ptr) {
1357 strcpy(fw_dump_ptr, "========Start dump ");
1358 fw_dump_ptr += strlen("========Start dump ");
1360 strcpy(fw_dump_ptr, entry->mem_name);
1361 fw_dump_ptr += strlen(entry->mem_name);
1363 strcpy(fw_dump_ptr, "========\n");
1364 fw_dump_ptr += strlen("========\n");
1366 memcpy(fw_dump_ptr, entry->mem_ptr, entry->mem_size);
1367 fw_dump_ptr += entry->mem_size;
1369 strcpy(fw_dump_ptr, "\n========End dump========\n");
1370 fw_dump_ptr += strlen("\n========End dump========\n");
1372 vfree(mem_type_mapping_tbl[idx].mem_ptr);
1373 mem_type_mapping_tbl[idx].mem_ptr = NULL;
1377 /* fw_dump_data will be free in device coredump release function
1378 after 5 min*/
1379 dev_coredumpv(&priv->btmrvl_dev.hcidev->dev, fw_dump_data,
1380 fw_dump_len, GFP_KERNEL);
1381 BT_INFO("== btmrvl firmware dump to /sys/class/devcoredump end");
1384 static int btmrvl_sdio_probe(struct sdio_func *func,
1385 const struct sdio_device_id *id)
1387 int ret = 0;
1388 struct btmrvl_private *priv = NULL;
1389 struct btmrvl_sdio_card *card = NULL;
1391 BT_INFO("vendor=0x%x, device=0x%x, class=%d, fn=%d",
1392 id->vendor, id->device, id->class, func->num);
1394 card = devm_kzalloc(&func->dev, sizeof(*card), GFP_KERNEL);
1395 if (!card)
1396 return -ENOMEM;
1398 card->func = func;
1400 if (id->driver_data) {
1401 struct btmrvl_sdio_device *data = (void *) id->driver_data;
1402 card->helper = data->helper;
1403 card->firmware = data->firmware;
1404 card->reg = data->reg;
1405 card->sd_blksz_fw_dl = data->sd_blksz_fw_dl;
1406 card->support_pscan_win_report = data->support_pscan_win_report;
1407 card->supports_fw_dump = data->supports_fw_dump;
1410 if (btmrvl_sdio_register_dev(card) < 0) {
1411 BT_ERR("Failed to register BT device!");
1412 return -ENODEV;
1415 /* Disable the interrupts on the card */
1416 btmrvl_sdio_disable_host_int(card);
1418 if (btmrvl_sdio_download_fw(card)) {
1419 BT_ERR("Downloading firmware failed!");
1420 ret = -ENODEV;
1421 goto unreg_dev;
1424 btmrvl_sdio_enable_host_int(card);
1426 priv = btmrvl_add_card(card);
1427 if (!priv) {
1428 BT_ERR("Initializing card failed!");
1429 ret = -ENODEV;
1430 goto disable_host_int;
1433 card->priv = priv;
1435 /* Initialize the interface specific function pointers */
1436 priv->hw_host_to_card = btmrvl_sdio_host_to_card;
1437 priv->hw_wakeup_firmware = btmrvl_sdio_wakeup_fw;
1438 priv->hw_process_int_status = btmrvl_sdio_process_int_status;
1439 priv->firmware_dump = btmrvl_sdio_dump_firmware;
1441 if (btmrvl_register_hdev(priv)) {
1442 BT_ERR("Register hdev failed!");
1443 ret = -ENODEV;
1444 goto disable_host_int;
1447 return 0;
1449 disable_host_int:
1450 btmrvl_sdio_disable_host_int(card);
1451 unreg_dev:
1452 btmrvl_sdio_unregister_dev(card);
1453 return ret;
1456 static void btmrvl_sdio_remove(struct sdio_func *func)
1458 struct btmrvl_sdio_card *card;
1460 if (func) {
1461 card = sdio_get_drvdata(func);
1462 if (card) {
1463 /* Send SHUTDOWN command & disable interrupt
1464 * if user removes the module.
1466 if (user_rmmod) {
1467 btmrvl_send_module_cfg_cmd(card->priv,
1468 MODULE_SHUTDOWN_REQ);
1469 btmrvl_sdio_disable_host_int(card);
1471 BT_DBG("unregester dev");
1472 card->priv->surprise_removed = true;
1473 btmrvl_sdio_unregister_dev(card);
1474 btmrvl_remove_card(card->priv);
1479 static int btmrvl_sdio_suspend(struct device *dev)
1481 struct sdio_func *func = dev_to_sdio_func(dev);
1482 struct btmrvl_sdio_card *card;
1483 struct btmrvl_private *priv;
1484 mmc_pm_flag_t pm_flags;
1485 struct hci_dev *hcidev;
1487 if (func) {
1488 pm_flags = sdio_get_host_pm_caps(func);
1489 BT_DBG("%s: suspend: PM flags = 0x%x", sdio_func_id(func),
1490 pm_flags);
1491 if (!(pm_flags & MMC_PM_KEEP_POWER)) {
1492 BT_ERR("%s: cannot remain alive while suspended",
1493 sdio_func_id(func));
1494 return -ENOSYS;
1496 card = sdio_get_drvdata(func);
1497 if (!card || !card->priv) {
1498 BT_ERR("card or priv structure is not valid");
1499 return 0;
1501 } else {
1502 BT_ERR("sdio_func is not specified");
1503 return 0;
1506 priv = card->priv;
1507 hcidev = priv->btmrvl_dev.hcidev;
1508 BT_DBG("%s: SDIO suspend", hcidev->name);
1509 hci_suspend_dev(hcidev);
1510 skb_queue_purge(&priv->adapter->tx_queue);
1512 if (priv->adapter->hs_state != HS_ACTIVATED) {
1513 if (btmrvl_enable_hs(priv)) {
1514 BT_ERR("HS not actived, suspend failed!");
1515 return -EBUSY;
1519 priv->adapter->is_suspended = true;
1521 /* We will keep the power when hs enabled successfully */
1522 if (priv->adapter->hs_state == HS_ACTIVATED) {
1523 BT_DBG("suspend with MMC_PM_KEEP_POWER");
1524 return sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
1525 } else {
1526 BT_DBG("suspend without MMC_PM_KEEP_POWER");
1527 return 0;
1531 static int btmrvl_sdio_resume(struct device *dev)
1533 struct sdio_func *func = dev_to_sdio_func(dev);
1534 struct btmrvl_sdio_card *card;
1535 struct btmrvl_private *priv;
1536 mmc_pm_flag_t pm_flags;
1537 struct hci_dev *hcidev;
1539 if (func) {
1540 pm_flags = sdio_get_host_pm_caps(func);
1541 BT_DBG("%s: resume: PM flags = 0x%x", sdio_func_id(func),
1542 pm_flags);
1543 card = sdio_get_drvdata(func);
1544 if (!card || !card->priv) {
1545 BT_ERR("card or priv structure is not valid");
1546 return 0;
1548 } else {
1549 BT_ERR("sdio_func is not specified");
1550 return 0;
1552 priv = card->priv;
1554 if (!priv->adapter->is_suspended) {
1555 BT_DBG("device already resumed");
1556 return 0;
1559 priv->hw_wakeup_firmware(priv);
1560 priv->adapter->hs_state = HS_DEACTIVATED;
1561 hcidev = priv->btmrvl_dev.hcidev;
1562 BT_DBG("%s: HS DEACTIVATED in resume!", hcidev->name);
1563 priv->adapter->is_suspended = false;
1564 BT_DBG("%s: SDIO resume", hcidev->name);
1565 hci_resume_dev(hcidev);
1567 return 0;
1570 static const struct dev_pm_ops btmrvl_sdio_pm_ops = {
1571 .suspend = btmrvl_sdio_suspend,
1572 .resume = btmrvl_sdio_resume,
1575 static struct sdio_driver bt_mrvl_sdio = {
1576 .name = "btmrvl_sdio",
1577 .id_table = btmrvl_sdio_ids,
1578 .probe = btmrvl_sdio_probe,
1579 .remove = btmrvl_sdio_remove,
1580 .drv = {
1581 .owner = THIS_MODULE,
1582 .pm = &btmrvl_sdio_pm_ops,
1586 static int __init btmrvl_sdio_init_module(void)
1588 if (sdio_register_driver(&bt_mrvl_sdio) != 0) {
1589 BT_ERR("SDIO Driver Registration Failed");
1590 return -ENODEV;
1593 /* Clear the flag in case user removes the card. */
1594 user_rmmod = 0;
1596 return 0;
1599 static void __exit btmrvl_sdio_exit_module(void)
1601 /* Set the flag as user is removing this module. */
1602 user_rmmod = 1;
1604 sdio_unregister_driver(&bt_mrvl_sdio);
1607 module_init(btmrvl_sdio_init_module);
1608 module_exit(btmrvl_sdio_exit_module);
1610 MODULE_AUTHOR("Marvell International Ltd.");
1611 MODULE_DESCRIPTION("Marvell BT-over-SDIO driver ver " VERSION);
1612 MODULE_VERSION(VERSION);
1613 MODULE_LICENSE("GPL v2");
1614 MODULE_FIRMWARE("mrvl/sd8688_helper.bin");
1615 MODULE_FIRMWARE("mrvl/sd8688.bin");
1616 MODULE_FIRMWARE("mrvl/sd8787_uapsta.bin");
1617 MODULE_FIRMWARE("mrvl/sd8797_uapsta.bin");
1618 MODULE_FIRMWARE("mrvl/sd8887_uapsta.bin");
1619 MODULE_FIRMWARE("mrvl/sd8897_uapsta.bin");