Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / bluetooth / btmrvl_sdio.c
blob07cd308f7abf6debe114452ed4e3e6de82efc1f8
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Marvell BT-over-SDIO driver: SDIO interface related functions.
5 * Copyright (C) 2009, Marvell International Ltd.
6 **/
8 #include <linux/firmware.h>
9 #include <linux/slab.h>
10 #include <linux/suspend.h>
12 #include <linux/mmc/sdio_ids.h>
13 #include <linux/mmc/sdio_func.h>
14 #include <linux/module.h>
15 #include <linux/devcoredump.h>
17 #include <net/bluetooth/bluetooth.h>
18 #include <net/bluetooth/hci_core.h>
20 #include "btmrvl_drv.h"
21 #include "btmrvl_sdio.h"
23 #define VERSION "1.0"
25 static struct memory_type_mapping mem_type_mapping_tbl[] = {
26 {"ITCM", NULL, 0, 0xF0},
27 {"DTCM", NULL, 0, 0xF1},
28 {"SQRAM", NULL, 0, 0xF2},
29 {"APU", NULL, 0, 0xF3},
30 {"CIU", NULL, 0, 0xF4},
31 {"ICU", NULL, 0, 0xF5},
32 {"MAC", NULL, 0, 0xF6},
33 {"EXT7", NULL, 0, 0xF7},
34 {"EXT8", NULL, 0, 0xF8},
35 {"EXT9", NULL, 0, 0xF9},
36 {"EXT10", NULL, 0, 0xFA},
37 {"EXT11", NULL, 0, 0xFB},
38 {"EXT12", NULL, 0, 0xFC},
39 {"EXT13", NULL, 0, 0xFD},
40 {"EXTLAST", NULL, 0, 0xFE},
43 static const struct of_device_id btmrvl_sdio_of_match_table[] __maybe_unused = {
44 { .compatible = "marvell,sd8897-bt" },
45 { .compatible = "marvell,sd8997-bt" },
46 { }
49 static irqreturn_t btmrvl_wake_irq_bt(int irq, void *priv)
51 struct btmrvl_sdio_card *card = priv;
52 struct device *dev = &card->func->dev;
53 struct btmrvl_plt_wake_cfg *cfg = card->plt_wake_cfg;
55 dev_info(dev, "wake by bt\n");
56 cfg->wake_by_bt = true;
57 disable_irq_nosync(irq);
59 pm_wakeup_event(dev, 0);
60 pm_system_wakeup();
62 return IRQ_HANDLED;
65 /* This function parses device tree node using mmc subnode devicetree API.
66 * The device node is saved in card->plt_of_node.
67 * If the device tree node exists and includes interrupts attributes, this
68 * function will request platform specific wakeup interrupt.
70 static int btmrvl_sdio_probe_of(struct device *dev,
71 struct btmrvl_sdio_card *card)
73 struct btmrvl_plt_wake_cfg *cfg;
74 int ret;
76 if (!dev->of_node ||
77 !of_match_node(btmrvl_sdio_of_match_table, dev->of_node)) {
78 dev_info(dev, "sdio device tree data not available\n");
79 return -1;
82 card->plt_of_node = dev->of_node;
84 card->plt_wake_cfg = devm_kzalloc(dev, sizeof(*card->plt_wake_cfg),
85 GFP_KERNEL);
86 cfg = card->plt_wake_cfg;
87 if (cfg && card->plt_of_node) {
88 cfg->irq_bt = irq_of_parse_and_map(card->plt_of_node, 0);
89 if (!cfg->irq_bt) {
90 dev_err(dev, "fail to parse irq_bt from device tree\n");
91 cfg->irq_bt = -1;
92 } else {
93 ret = devm_request_irq(dev, cfg->irq_bt,
94 btmrvl_wake_irq_bt,
95 IRQF_NO_AUTOEN, "bt_wake", card);
96 if (ret) {
97 dev_err(dev,
98 "Failed to request irq_bt %d (%d)\n",
99 cfg->irq_bt, ret);
102 /* Configure wakeup (enabled by default) */
103 device_init_wakeup(dev, true);
107 return 0;
110 /* The btmrvl_sdio_remove() callback function is called
111 * when user removes this module from kernel space or ejects
112 * the card from the slot. The driver handles these 2 cases
113 * differently.
114 * If the user is removing the module, a MODULE_SHUTDOWN_REQ
115 * command is sent to firmware and interrupt will be disabled.
116 * If the card is removed, there is no need to send command
117 * or disable interrupt.
119 * The variable 'user_rmmod' is used to distinguish these two
120 * scenarios. This flag is initialized as FALSE in case the card
121 * is removed, and will be set to TRUE for module removal when
122 * module_exit function is called.
124 static u8 user_rmmod;
125 static u8 sdio_ireg;
127 static const struct btmrvl_sdio_card_reg btmrvl_reg_8688 = {
128 .cfg = 0x03,
129 .host_int_mask = 0x04,
130 .host_intstatus = 0x05,
131 .card_status = 0x20,
132 .sq_read_base_addr_a0 = 0x10,
133 .sq_read_base_addr_a1 = 0x11,
134 .card_fw_status0 = 0x40,
135 .card_fw_status1 = 0x41,
136 .card_rx_len = 0x42,
137 .card_rx_unit = 0x43,
138 .io_port_0 = 0x00,
139 .io_port_1 = 0x01,
140 .io_port_2 = 0x02,
141 .int_read_to_clear = false,
143 static const struct btmrvl_sdio_card_reg btmrvl_reg_87xx = {
144 .cfg = 0x00,
145 .host_int_mask = 0x02,
146 .host_intstatus = 0x03,
147 .card_status = 0x30,
148 .sq_read_base_addr_a0 = 0x40,
149 .sq_read_base_addr_a1 = 0x41,
150 .card_revision = 0x5c,
151 .card_fw_status0 = 0x60,
152 .card_fw_status1 = 0x61,
153 .card_rx_len = 0x62,
154 .card_rx_unit = 0x63,
155 .io_port_0 = 0x78,
156 .io_port_1 = 0x79,
157 .io_port_2 = 0x7a,
158 .int_read_to_clear = false,
161 static const struct btmrvl_sdio_card_reg btmrvl_reg_8887 = {
162 .cfg = 0x00,
163 .host_int_mask = 0x08,
164 .host_intstatus = 0x0C,
165 .card_status = 0x5C,
166 .sq_read_base_addr_a0 = 0x6C,
167 .sq_read_base_addr_a1 = 0x6D,
168 .card_revision = 0xC8,
169 .card_fw_status0 = 0x88,
170 .card_fw_status1 = 0x89,
171 .card_rx_len = 0x8A,
172 .card_rx_unit = 0x8B,
173 .io_port_0 = 0xE4,
174 .io_port_1 = 0xE5,
175 .io_port_2 = 0xE6,
176 .int_read_to_clear = true,
177 .host_int_rsr = 0x04,
178 .card_misc_cfg = 0xD8,
181 static const struct btmrvl_sdio_card_reg btmrvl_reg_8897 = {
182 .cfg = 0x00,
183 .host_int_mask = 0x02,
184 .host_intstatus = 0x03,
185 .card_status = 0x50,
186 .sq_read_base_addr_a0 = 0x60,
187 .sq_read_base_addr_a1 = 0x61,
188 .card_revision = 0xbc,
189 .card_fw_status0 = 0xc0,
190 .card_fw_status1 = 0xc1,
191 .card_rx_len = 0xc2,
192 .card_rx_unit = 0xc3,
193 .io_port_0 = 0xd8,
194 .io_port_1 = 0xd9,
195 .io_port_2 = 0xda,
196 .int_read_to_clear = true,
197 .host_int_rsr = 0x01,
198 .card_misc_cfg = 0xcc,
199 .fw_dump_ctrl = 0xe2,
200 .fw_dump_start = 0xe3,
201 .fw_dump_end = 0xea,
204 static const struct btmrvl_sdio_card_reg btmrvl_reg_89xx = {
205 .cfg = 0x00,
206 .host_int_mask = 0x08,
207 .host_intstatus = 0x0c,
208 .card_status = 0x5c,
209 .sq_read_base_addr_a0 = 0xf8,
210 .sq_read_base_addr_a1 = 0xf9,
211 .card_revision = 0xc8,
212 .card_fw_status0 = 0xe8,
213 .card_fw_status1 = 0xe9,
214 .card_rx_len = 0xea,
215 .card_rx_unit = 0xeb,
216 .io_port_0 = 0xe4,
217 .io_port_1 = 0xe5,
218 .io_port_2 = 0xe6,
219 .int_read_to_clear = true,
220 .host_int_rsr = 0x04,
221 .card_misc_cfg = 0xd8,
222 .fw_dump_ctrl = 0xf0,
223 .fw_dump_start = 0xf1,
224 .fw_dump_end = 0xf8,
227 static const struct btmrvl_sdio_device btmrvl_sdio_sd8688 = {
228 .helper = "mrvl/sd8688_helper.bin",
229 .firmware = "mrvl/sd8688.bin",
230 .reg = &btmrvl_reg_8688,
231 .support_pscan_win_report = false,
232 .sd_blksz_fw_dl = 64,
233 .supports_fw_dump = false,
236 static const struct btmrvl_sdio_device btmrvl_sdio_sd8787 = {
237 .helper = NULL,
238 .firmware = "mrvl/sd8787_uapsta.bin",
239 .reg = &btmrvl_reg_87xx,
240 .support_pscan_win_report = false,
241 .sd_blksz_fw_dl = 256,
242 .supports_fw_dump = false,
245 static const struct btmrvl_sdio_device btmrvl_sdio_sd8797 = {
246 .helper = NULL,
247 .firmware = "mrvl/sd8797_uapsta.bin",
248 .reg = &btmrvl_reg_87xx,
249 .support_pscan_win_report = false,
250 .sd_blksz_fw_dl = 256,
251 .supports_fw_dump = false,
254 static const struct btmrvl_sdio_device btmrvl_sdio_sd8887 = {
255 .helper = NULL,
256 .firmware = "mrvl/sd8887_uapsta.bin",
257 .reg = &btmrvl_reg_8887,
258 .support_pscan_win_report = true,
259 .sd_blksz_fw_dl = 256,
260 .supports_fw_dump = false,
263 static const struct btmrvl_sdio_device btmrvl_sdio_sd8897 = {
264 .helper = NULL,
265 .firmware = "mrvl/sd8897_uapsta.bin",
266 .reg = &btmrvl_reg_8897,
267 .support_pscan_win_report = true,
268 .sd_blksz_fw_dl = 256,
269 .supports_fw_dump = true,
272 static const struct btmrvl_sdio_device btmrvl_sdio_sd8977 = {
273 .helper = NULL,
274 .firmware = "mrvl/sdsd8977_combo_v2.bin",
275 .reg = &btmrvl_reg_89xx,
276 .support_pscan_win_report = true,
277 .sd_blksz_fw_dl = 256,
278 .supports_fw_dump = true,
281 static const struct btmrvl_sdio_device btmrvl_sdio_sd8987 = {
282 .helper = NULL,
283 .firmware = "mrvl/sd8987_uapsta.bin",
284 .reg = &btmrvl_reg_89xx,
285 .support_pscan_win_report = true,
286 .sd_blksz_fw_dl = 256,
287 .supports_fw_dump = true,
290 static const struct btmrvl_sdio_device btmrvl_sdio_sd8997 = {
291 .helper = NULL,
292 .firmware = "mrvl/sdsd8997_combo_v4.bin",
293 .reg = &btmrvl_reg_89xx,
294 .support_pscan_win_report = true,
295 .sd_blksz_fw_dl = 256,
296 .supports_fw_dump = true,
299 static const struct sdio_device_id btmrvl_sdio_ids[] = {
300 /* Marvell SD8688 Bluetooth device */
301 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8688_BT),
302 .driver_data = (unsigned long)&btmrvl_sdio_sd8688 },
303 /* Marvell SD8787 Bluetooth device */
304 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8787_BT),
305 .driver_data = (unsigned long)&btmrvl_sdio_sd8787 },
306 /* Marvell SD8787 Bluetooth AMP device */
307 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8787_BT_AMP),
308 .driver_data = (unsigned long)&btmrvl_sdio_sd8787 },
309 /* Marvell SD8797 Bluetooth device */
310 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8797_BT),
311 .driver_data = (unsigned long)&btmrvl_sdio_sd8797 },
312 /* Marvell SD8887 Bluetooth device */
313 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8887_BT),
314 .driver_data = (unsigned long)&btmrvl_sdio_sd8887 },
315 /* Marvell SD8897 Bluetooth device */
316 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8897_BT),
317 .driver_data = (unsigned long)&btmrvl_sdio_sd8897 },
318 /* Marvell SD8977 Bluetooth device */
319 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8977_BT),
320 .driver_data = (unsigned long)&btmrvl_sdio_sd8977 },
321 /* Marvell SD8987 Bluetooth device */
322 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8987_BT),
323 .driver_data = (unsigned long)&btmrvl_sdio_sd8987 },
324 /* Marvell SD8997 Bluetooth device */
325 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8997_BT),
326 .driver_data = (unsigned long)&btmrvl_sdio_sd8997 },
328 { } /* Terminating entry */
331 MODULE_DEVICE_TABLE(sdio, btmrvl_sdio_ids);
333 static int btmrvl_sdio_get_rx_unit(struct btmrvl_sdio_card *card)
335 u8 reg;
336 int ret;
338 reg = sdio_readb(card->func, card->reg->card_rx_unit, &ret);
339 if (!ret)
340 card->rx_unit = reg;
342 return ret;
345 static int btmrvl_sdio_read_fw_status(struct btmrvl_sdio_card *card, u16 *dat)
347 u8 fws0, fws1;
348 int ret;
350 *dat = 0;
352 fws0 = sdio_readb(card->func, card->reg->card_fw_status0, &ret);
353 if (ret)
354 return -EIO;
356 fws1 = sdio_readb(card->func, card->reg->card_fw_status1, &ret);
357 if (ret)
358 return -EIO;
360 *dat = (((u16) fws1) << 8) | fws0;
362 return 0;
365 static int btmrvl_sdio_read_rx_len(struct btmrvl_sdio_card *card, u16 *dat)
367 u8 reg;
368 int ret;
370 reg = sdio_readb(card->func, card->reg->card_rx_len, &ret);
371 if (!ret)
372 *dat = (u16) reg << card->rx_unit;
374 return ret;
377 static int btmrvl_sdio_enable_host_int_mask(struct btmrvl_sdio_card *card,
378 u8 mask)
380 int ret;
382 sdio_writeb(card->func, mask, card->reg->host_int_mask, &ret);
383 if (ret) {
384 BT_ERR("Unable to enable the host interrupt!");
385 ret = -EIO;
388 return ret;
391 static int btmrvl_sdio_disable_host_int_mask(struct btmrvl_sdio_card *card,
392 u8 mask)
394 u8 host_int_mask;
395 int ret;
397 host_int_mask = sdio_readb(card->func, card->reg->host_int_mask, &ret);
398 if (ret)
399 return -EIO;
401 host_int_mask &= ~mask;
403 sdio_writeb(card->func, host_int_mask, card->reg->host_int_mask, &ret);
404 if (ret < 0) {
405 BT_ERR("Unable to disable the host interrupt!");
406 return -EIO;
409 return 0;
412 static int btmrvl_sdio_poll_card_status(struct btmrvl_sdio_card *card, u8 bits)
414 unsigned int tries;
415 u8 status;
416 int ret;
418 for (tries = 0; tries < MAX_POLL_TRIES * 1000; tries++) {
419 status = sdio_readb(card->func, card->reg->card_status, &ret);
420 if (ret)
421 goto failed;
422 if ((status & bits) == bits)
423 return ret;
425 udelay(1);
428 ret = -ETIMEDOUT;
430 failed:
431 BT_ERR("FAILED! ret=%d", ret);
433 return ret;
436 static int btmrvl_sdio_verify_fw_download(struct btmrvl_sdio_card *card,
437 int pollnum)
439 u16 firmwarestat;
440 int tries, ret;
442 /* Wait for firmware to become ready */
443 for (tries = 0; tries < pollnum; tries++) {
444 sdio_claim_host(card->func);
445 ret = btmrvl_sdio_read_fw_status(card, &firmwarestat);
446 sdio_release_host(card->func);
447 if (ret < 0)
448 continue;
450 if (firmwarestat == FIRMWARE_READY)
451 return 0;
453 msleep(100);
456 return -ETIMEDOUT;
459 static int btmrvl_sdio_download_helper(struct btmrvl_sdio_card *card)
461 const struct firmware *fw_helper = NULL;
462 const u8 *helper = NULL;
463 int ret;
464 void *tmphlprbuf = NULL;
465 int tmphlprbufsz, hlprblknow, helperlen;
466 u8 *helperbuf;
467 u32 tx_len;
469 ret = request_firmware(&fw_helper, card->helper,
470 &card->func->dev);
471 if ((ret < 0) || !fw_helper) {
472 BT_ERR("request_firmware(helper) failed, error code = %d",
473 ret);
474 ret = -ENOENT;
475 goto done;
478 helper = fw_helper->data;
479 helperlen = fw_helper->size;
481 BT_DBG("Downloading helper image (%d bytes), block size %d bytes",
482 helperlen, SDIO_BLOCK_SIZE);
484 tmphlprbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
486 tmphlprbuf = kzalloc(tmphlprbufsz, GFP_KERNEL);
487 if (!tmphlprbuf) {
488 BT_ERR("Unable to allocate buffer for helper."
489 " Terminating download");
490 ret = -ENOMEM;
491 goto done;
494 helperbuf = (u8 *) ALIGN_ADDR(tmphlprbuf, BTSDIO_DMA_ALIGN);
496 /* Perform helper data transfer */
497 tx_len = (FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE)
498 - SDIO_HEADER_LEN;
499 hlprblknow = 0;
501 do {
502 ret = btmrvl_sdio_poll_card_status(card,
503 CARD_IO_READY | DN_LD_CARD_RDY);
504 if (ret < 0) {
505 BT_ERR("Helper download poll status timeout @ %d",
506 hlprblknow);
507 goto done;
510 /* Check if there is more data? */
511 if (hlprblknow >= helperlen)
512 break;
514 if (helperlen - hlprblknow < tx_len)
515 tx_len = helperlen - hlprblknow;
517 /* Little-endian */
518 helperbuf[0] = ((tx_len & 0x000000ff) >> 0);
519 helperbuf[1] = ((tx_len & 0x0000ff00) >> 8);
520 helperbuf[2] = ((tx_len & 0x00ff0000) >> 16);
521 helperbuf[3] = ((tx_len & 0xff000000) >> 24);
523 memcpy(&helperbuf[SDIO_HEADER_LEN], &helper[hlprblknow],
524 tx_len);
526 /* Now send the data */
527 ret = sdio_writesb(card->func, card->ioport, helperbuf,
528 FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE);
529 if (ret < 0) {
530 BT_ERR("IO error during helper download @ %d",
531 hlprblknow);
532 goto done;
535 hlprblknow += tx_len;
536 } while (true);
538 BT_DBG("Transferring helper image EOF block");
540 memset(helperbuf, 0x0, SDIO_BLOCK_SIZE);
542 ret = sdio_writesb(card->func, card->ioport, helperbuf,
543 SDIO_BLOCK_SIZE);
544 if (ret < 0) {
545 BT_ERR("IO error in writing helper image EOF block");
546 goto done;
549 ret = 0;
551 done:
552 kfree(tmphlprbuf);
553 release_firmware(fw_helper);
554 return ret;
557 static int btmrvl_sdio_download_fw_w_helper(struct btmrvl_sdio_card *card)
559 const struct firmware *fw_firmware = NULL;
560 const u8 *firmware = NULL;
561 int firmwarelen, tmpfwbufsz, ret;
562 unsigned int tries, offset;
563 u8 base0, base1;
564 void *tmpfwbuf = NULL;
565 u8 *fwbuf;
566 u16 len, blksz_dl = card->sd_blksz_fw_dl;
567 int txlen = 0, tx_blocks = 0, count = 0;
569 ret = request_firmware(&fw_firmware, card->firmware,
570 &card->func->dev);
571 if ((ret < 0) || !fw_firmware) {
572 BT_ERR("request_firmware(firmware) failed, error code = %d",
573 ret);
574 ret = -ENOENT;
575 goto done;
578 firmware = fw_firmware->data;
579 firmwarelen = fw_firmware->size;
581 BT_DBG("Downloading FW image (%d bytes)", firmwarelen);
583 tmpfwbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
584 tmpfwbuf = kzalloc(tmpfwbufsz, GFP_KERNEL);
585 if (!tmpfwbuf) {
586 BT_ERR("Unable to allocate buffer for firmware."
587 " Terminating download");
588 ret = -ENOMEM;
589 goto done;
592 /* Ensure aligned firmware buffer */
593 fwbuf = (u8 *) ALIGN_ADDR(tmpfwbuf, BTSDIO_DMA_ALIGN);
595 /* Perform firmware data transfer */
596 offset = 0;
597 do {
598 ret = btmrvl_sdio_poll_card_status(card,
599 CARD_IO_READY | DN_LD_CARD_RDY);
600 if (ret < 0) {
601 BT_ERR("FW download with helper poll status"
602 " timeout @ %d", offset);
603 goto done;
606 /* Check if there is more data ? */
607 if (offset >= firmwarelen)
608 break;
610 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
611 base0 = sdio_readb(card->func,
612 card->reg->sq_read_base_addr_a0, &ret);
613 if (ret) {
614 BT_ERR("BASE0 register read failed:"
615 " base0 = 0x%04X(%d)."
616 " Terminating download",
617 base0, base0);
618 ret = -EIO;
619 goto done;
621 base1 = sdio_readb(card->func,
622 card->reg->sq_read_base_addr_a1, &ret);
623 if (ret) {
624 BT_ERR("BASE1 register read failed:"
625 " base1 = 0x%04X(%d)."
626 " Terminating download",
627 base1, base1);
628 ret = -EIO;
629 goto done;
632 len = (((u16) base1) << 8) | base0;
633 if (len)
634 break;
636 udelay(10);
639 if (!len)
640 break;
641 else if (len > BTM_UPLD_SIZE) {
642 BT_ERR("FW download failure @%d, invalid length %d",
643 offset, len);
644 ret = -EINVAL;
645 goto done;
648 txlen = len;
650 if (len & BIT(0)) {
651 count++;
652 if (count > MAX_WRITE_IOMEM_RETRY) {
653 BT_ERR("FW download failure @%d, "
654 "over max retry count", offset);
655 ret = -EIO;
656 goto done;
658 BT_ERR("FW CRC error indicated by the helper: "
659 "len = 0x%04X, txlen = %d", len, txlen);
660 len &= ~BIT(0);
661 /* Set txlen to 0 so as to resend from same offset */
662 txlen = 0;
663 } else {
664 count = 0;
666 /* Last block ? */
667 if (firmwarelen - offset < txlen)
668 txlen = firmwarelen - offset;
670 tx_blocks = DIV_ROUND_UP(txlen, blksz_dl);
672 memcpy(fwbuf, &firmware[offset], txlen);
675 ret = sdio_writesb(card->func, card->ioport, fwbuf,
676 tx_blocks * blksz_dl);
678 if (ret < 0) {
679 BT_ERR("FW download, writesb(%d) failed @%d",
680 count, offset);
681 sdio_writeb(card->func, HOST_CMD53_FIN,
682 card->reg->cfg, &ret);
683 if (ret)
684 BT_ERR("writeb failed (CFG)");
687 offset += txlen;
688 } while (true);
690 BT_INFO("FW download over, size %d bytes", offset);
692 ret = 0;
694 done:
695 kfree(tmpfwbuf);
696 release_firmware(fw_firmware);
697 return ret;
700 static int btmrvl_sdio_card_to_host(struct btmrvl_private *priv)
702 u16 buf_len = 0;
703 int ret, num_blocks, blksz;
704 struct sk_buff *skb = NULL;
705 u32 type;
706 u8 *payload;
707 struct hci_dev *hdev = priv->btmrvl_dev.hcidev;
708 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
710 if (!card || !card->func) {
711 BT_ERR("card or function is NULL!");
712 ret = -EINVAL;
713 goto exit;
716 /* Read the length of data to be transferred */
717 ret = btmrvl_sdio_read_rx_len(card, &buf_len);
718 if (ret < 0) {
719 BT_ERR("read rx_len failed");
720 ret = -EIO;
721 goto exit;
724 blksz = SDIO_BLOCK_SIZE;
725 num_blocks = DIV_ROUND_UP(buf_len, blksz);
727 if (buf_len <= SDIO_HEADER_LEN
728 || (num_blocks * blksz) > ALLOC_BUF_SIZE) {
729 BT_ERR("invalid packet length: %d", buf_len);
730 ret = -EINVAL;
731 goto exit;
734 /* Allocate buffer */
735 skb = bt_skb_alloc(num_blocks * blksz + BTSDIO_DMA_ALIGN, GFP_KERNEL);
736 if (!skb) {
737 BT_ERR("No free skb");
738 ret = -ENOMEM;
739 goto exit;
742 if ((unsigned long) skb->data & (BTSDIO_DMA_ALIGN - 1)) {
743 skb_put(skb, (unsigned long) skb->data &
744 (BTSDIO_DMA_ALIGN - 1));
745 skb_pull(skb, (unsigned long) skb->data &
746 (BTSDIO_DMA_ALIGN - 1));
749 payload = skb->data;
751 ret = sdio_readsb(card->func, payload, card->ioport,
752 num_blocks * blksz);
753 if (ret < 0) {
754 BT_ERR("readsb failed: %d", ret);
755 ret = -EIO;
756 goto exit;
759 /* This is SDIO specific header length: byte[2][1][0], type: byte[3]
760 * (HCI_COMMAND = 1, ACL_DATA = 2, SCO_DATA = 3, 0xFE = Vendor)
763 buf_len = payload[0];
764 buf_len |= payload[1] << 8;
765 buf_len |= payload[2] << 16;
767 if (buf_len > blksz * num_blocks) {
768 BT_ERR("Skip incorrect packet: hdrlen %d buffer %d",
769 buf_len, blksz * num_blocks);
770 ret = -EIO;
771 goto exit;
774 type = payload[3];
776 switch (type) {
777 case HCI_ACLDATA_PKT:
778 case HCI_SCODATA_PKT:
779 case HCI_EVENT_PKT:
780 hci_skb_pkt_type(skb) = type;
781 skb_put(skb, buf_len);
782 skb_pull(skb, SDIO_HEADER_LEN);
784 if (type == HCI_EVENT_PKT) {
785 if (btmrvl_check_evtpkt(priv, skb))
786 hci_recv_frame(hdev, skb);
787 } else {
788 hci_recv_frame(hdev, skb);
791 hdev->stat.byte_rx += buf_len;
792 break;
794 case MRVL_VENDOR_PKT:
795 hci_skb_pkt_type(skb) = HCI_VENDOR_PKT;
796 skb_put(skb, buf_len);
797 skb_pull(skb, SDIO_HEADER_LEN);
799 if (btmrvl_process_event(priv, skb))
800 hci_recv_frame(hdev, skb);
802 hdev->stat.byte_rx += buf_len;
803 break;
805 default:
806 BT_ERR("Unknown packet type:%d", type);
807 BT_ERR("hex: %*ph", blksz * num_blocks, payload);
809 kfree_skb(skb);
810 skb = NULL;
811 break;
814 exit:
815 if (ret) {
816 hdev->stat.err_rx++;
817 kfree_skb(skb);
820 return ret;
823 static int btmrvl_sdio_process_int_status(struct btmrvl_private *priv)
825 ulong flags;
826 u8 ireg;
827 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
829 spin_lock_irqsave(&priv->driver_lock, flags);
830 ireg = sdio_ireg;
831 sdio_ireg = 0;
832 spin_unlock_irqrestore(&priv->driver_lock, flags);
834 sdio_claim_host(card->func);
835 if (ireg & DN_LD_HOST_INT_STATUS) {
836 if (priv->btmrvl_dev.tx_dnld_rdy)
837 BT_DBG("tx_done already received: "
838 " int_status=0x%x", ireg);
839 else
840 priv->btmrvl_dev.tx_dnld_rdy = true;
843 if (ireg & UP_LD_HOST_INT_STATUS)
844 btmrvl_sdio_card_to_host(priv);
846 sdio_release_host(card->func);
848 return 0;
851 static int btmrvl_sdio_read_to_clear(struct btmrvl_sdio_card *card, u8 *ireg)
853 struct btmrvl_adapter *adapter = card->priv->adapter;
854 int ret;
856 ret = sdio_readsb(card->func, adapter->hw_regs, 0, SDIO_BLOCK_SIZE);
857 if (ret) {
858 BT_ERR("sdio_readsb: read int hw_regs failed: %d", ret);
859 return ret;
862 *ireg = adapter->hw_regs[card->reg->host_intstatus];
863 BT_DBG("hw_regs[%#x]=%#x", card->reg->host_intstatus, *ireg);
865 return 0;
868 static int btmrvl_sdio_write_to_clear(struct btmrvl_sdio_card *card, u8 *ireg)
870 int ret;
872 *ireg = sdio_readb(card->func, card->reg->host_intstatus, &ret);
873 if (ret) {
874 BT_ERR("sdio_readb: read int status failed: %d", ret);
875 return ret;
878 if (*ireg) {
880 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
881 * Clear the interrupt status register and re-enable the
882 * interrupt.
884 BT_DBG("int_status = 0x%x", *ireg);
886 sdio_writeb(card->func, ~(*ireg) & (DN_LD_HOST_INT_STATUS |
887 UP_LD_HOST_INT_STATUS),
888 card->reg->host_intstatus, &ret);
889 if (ret) {
890 BT_ERR("sdio_writeb: clear int status failed: %d", ret);
891 return ret;
895 return 0;
898 static void btmrvl_sdio_interrupt(struct sdio_func *func)
900 struct btmrvl_private *priv;
901 struct btmrvl_sdio_card *card;
902 ulong flags;
903 u8 ireg = 0;
904 int ret;
906 card = sdio_get_drvdata(func);
907 if (!card || !card->priv) {
908 BT_ERR("sbi_interrupt(%p) card or priv is NULL, card=%p",
909 func, card);
910 return;
913 priv = card->priv;
915 if (priv->surprise_removed)
916 return;
918 if (card->reg->int_read_to_clear)
919 ret = btmrvl_sdio_read_to_clear(card, &ireg);
920 else
921 ret = btmrvl_sdio_write_to_clear(card, &ireg);
923 if (ret)
924 return;
926 spin_lock_irqsave(&priv->driver_lock, flags);
927 sdio_ireg |= ireg;
928 spin_unlock_irqrestore(&priv->driver_lock, flags);
930 btmrvl_interrupt(priv);
933 static int btmrvl_sdio_register_dev(struct btmrvl_sdio_card *card)
935 struct sdio_func *func;
936 u8 reg;
937 int ret;
939 if (!card || !card->func) {
940 BT_ERR("Error: card or function is NULL!");
941 ret = -EINVAL;
942 goto failed;
945 func = card->func;
947 sdio_claim_host(func);
949 ret = sdio_enable_func(func);
950 if (ret) {
951 BT_ERR("sdio_enable_func() failed: ret=%d", ret);
952 ret = -EIO;
953 goto release_host;
956 ret = sdio_claim_irq(func, btmrvl_sdio_interrupt);
957 if (ret) {
958 BT_ERR("sdio_claim_irq failed: ret=%d", ret);
959 ret = -EIO;
960 goto disable_func;
963 ret = sdio_set_block_size(card->func, SDIO_BLOCK_SIZE);
964 if (ret) {
965 BT_ERR("cannot set SDIO block size");
966 ret = -EIO;
967 goto release_irq;
970 reg = sdio_readb(func, card->reg->io_port_0, &ret);
971 if (ret < 0) {
972 ret = -EIO;
973 goto release_irq;
976 card->ioport = reg;
978 reg = sdio_readb(func, card->reg->io_port_1, &ret);
979 if (ret < 0) {
980 ret = -EIO;
981 goto release_irq;
984 card->ioport |= (reg << 8);
986 reg = sdio_readb(func, card->reg->io_port_2, &ret);
987 if (ret < 0) {
988 ret = -EIO;
989 goto release_irq;
992 card->ioport |= (reg << 16);
994 BT_DBG("SDIO FUNC%d IO port: 0x%x", func->num, card->ioport);
996 if (card->reg->int_read_to_clear) {
997 reg = sdio_readb(func, card->reg->host_int_rsr, &ret);
998 if (ret < 0) {
999 ret = -EIO;
1000 goto release_irq;
1002 sdio_writeb(func, reg | 0x3f, card->reg->host_int_rsr, &ret);
1003 if (ret < 0) {
1004 ret = -EIO;
1005 goto release_irq;
1008 reg = sdio_readb(func, card->reg->card_misc_cfg, &ret);
1009 if (ret < 0) {
1010 ret = -EIO;
1011 goto release_irq;
1013 sdio_writeb(func, reg | 0x10, card->reg->card_misc_cfg, &ret);
1014 if (ret < 0) {
1015 ret = -EIO;
1016 goto release_irq;
1020 sdio_set_drvdata(func, card);
1022 sdio_release_host(func);
1024 return 0;
1026 release_irq:
1027 sdio_release_irq(func);
1029 disable_func:
1030 sdio_disable_func(func);
1032 release_host:
1033 sdio_release_host(func);
1035 failed:
1036 return ret;
1039 static int btmrvl_sdio_unregister_dev(struct btmrvl_sdio_card *card)
1041 if (card && card->func) {
1042 sdio_claim_host(card->func);
1043 sdio_release_irq(card->func);
1044 sdio_disable_func(card->func);
1045 sdio_release_host(card->func);
1046 sdio_set_drvdata(card->func, NULL);
1049 return 0;
1052 static int btmrvl_sdio_enable_host_int(struct btmrvl_sdio_card *card)
1054 int ret;
1056 if (!card || !card->func)
1057 return -EINVAL;
1059 sdio_claim_host(card->func);
1061 ret = btmrvl_sdio_enable_host_int_mask(card, HIM_ENABLE);
1063 btmrvl_sdio_get_rx_unit(card);
1065 sdio_release_host(card->func);
1067 return ret;
1070 static int btmrvl_sdio_disable_host_int(struct btmrvl_sdio_card *card)
1072 int ret;
1074 if (!card || !card->func)
1075 return -EINVAL;
1077 sdio_claim_host(card->func);
1079 ret = btmrvl_sdio_disable_host_int_mask(card, HIM_DISABLE);
1081 sdio_release_host(card->func);
1083 return ret;
1086 static int btmrvl_sdio_host_to_card(struct btmrvl_private *priv,
1087 u8 *payload, u16 nb)
1089 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
1090 int ret = 0;
1091 int blksz;
1092 int i = 0;
1093 u8 *buf = NULL;
1094 void *tmpbuf = NULL;
1095 int tmpbufsz;
1097 if (!card || !card->func) {
1098 BT_ERR("card or function is NULL!");
1099 return -EINVAL;
1102 blksz = DIV_ROUND_UP(nb, SDIO_BLOCK_SIZE) * SDIO_BLOCK_SIZE;
1104 buf = payload;
1105 if ((unsigned long) payload & (BTSDIO_DMA_ALIGN - 1) ||
1106 nb < blksz) {
1107 tmpbufsz = ALIGN_SZ(blksz, BTSDIO_DMA_ALIGN) +
1108 BTSDIO_DMA_ALIGN;
1109 tmpbuf = kzalloc(tmpbufsz, GFP_KERNEL);
1110 if (!tmpbuf)
1111 return -ENOMEM;
1112 buf = (u8 *) ALIGN_ADDR(tmpbuf, BTSDIO_DMA_ALIGN);
1113 memcpy(buf, payload, nb);
1116 sdio_claim_host(card->func);
1118 do {
1119 /* Transfer data to card */
1120 ret = sdio_writesb(card->func, card->ioport, buf,
1121 blksz);
1122 if (ret < 0) {
1123 i++;
1124 BT_ERR("i=%d writesb failed: %d", i, ret);
1125 BT_ERR("hex: %*ph", nb, payload);
1126 ret = -EIO;
1127 if (i > MAX_WRITE_IOMEM_RETRY)
1128 goto exit;
1130 } while (ret);
1132 priv->btmrvl_dev.tx_dnld_rdy = false;
1134 exit:
1135 sdio_release_host(card->func);
1136 kfree(tmpbuf);
1138 return ret;
1141 static int btmrvl_sdio_download_fw(struct btmrvl_sdio_card *card)
1143 int ret;
1144 u8 fws0;
1145 int pollnum = MAX_POLL_TRIES;
1147 if (!card || !card->func) {
1148 BT_ERR("card or function is NULL!");
1149 return -EINVAL;
1152 if (!btmrvl_sdio_verify_fw_download(card, 1)) {
1153 BT_DBG("Firmware already downloaded!");
1154 return 0;
1157 sdio_claim_host(card->func);
1159 /* Check if other function driver is downloading the firmware */
1160 fws0 = sdio_readb(card->func, card->reg->card_fw_status0, &ret);
1161 if (ret) {
1162 BT_ERR("Failed to read FW downloading status!");
1163 ret = -EIO;
1164 goto done;
1166 if (fws0) {
1167 BT_DBG("BT not the winner (%#x). Skip FW downloading", fws0);
1169 /* Give other function more time to download the firmware */
1170 pollnum *= 10;
1171 } else {
1172 if (card->helper) {
1173 ret = btmrvl_sdio_download_helper(card);
1174 if (ret) {
1175 BT_ERR("Failed to download helper!");
1176 ret = -EIO;
1177 goto done;
1181 if (btmrvl_sdio_download_fw_w_helper(card)) {
1182 BT_ERR("Failed to download firmware!");
1183 ret = -EIO;
1184 goto done;
1189 * winner or not, with this test the FW synchronizes when the
1190 * module can continue its initialization
1192 if (btmrvl_sdio_verify_fw_download(card, pollnum)) {
1193 BT_ERR("FW failed to be active in time!");
1194 ret = -ETIMEDOUT;
1195 goto done;
1198 sdio_release_host(card->func);
1200 return 0;
1202 done:
1203 sdio_release_host(card->func);
1204 return ret;
1207 static int btmrvl_sdio_wakeup_fw(struct btmrvl_private *priv)
1209 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
1210 int ret = 0;
1212 if (!card || !card->func) {
1213 BT_ERR("card or function is NULL!");
1214 return -EINVAL;
1217 sdio_claim_host(card->func);
1219 sdio_writeb(card->func, HOST_POWER_UP, card->reg->cfg, &ret);
1221 sdio_release_host(card->func);
1223 BT_DBG("wake up firmware");
1225 return ret;
1228 static void btmrvl_sdio_dump_regs(struct btmrvl_private *priv)
1230 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
1231 int ret = 0;
1232 unsigned int reg, reg_start, reg_end;
1233 char buf[256], *ptr;
1234 u8 loop, func, data;
1235 int MAX_LOOP = 2;
1237 btmrvl_sdio_wakeup_fw(priv);
1238 sdio_claim_host(card->func);
1240 for (loop = 0; loop < MAX_LOOP; loop++) {
1241 memset(buf, 0, sizeof(buf));
1242 ptr = buf;
1244 if (loop == 0) {
1245 /* Read the registers of SDIO function0 */
1246 func = loop;
1247 reg_start = 0;
1248 reg_end = 9;
1249 } else {
1250 func = 2;
1251 reg_start = 0;
1252 reg_end = 0x09;
1255 ptr += sprintf(ptr, "SDIO Func%d (%#x-%#x): ",
1256 func, reg_start, reg_end);
1257 for (reg = reg_start; reg <= reg_end; reg++) {
1258 if (func == 0)
1259 data = sdio_f0_readb(card->func, reg, &ret);
1260 else
1261 data = sdio_readb(card->func, reg, &ret);
1263 if (!ret) {
1264 ptr += sprintf(ptr, "%02x ", data);
1265 } else {
1266 ptr += sprintf(ptr, "ERR");
1267 break;
1271 BT_INFO("%s", buf);
1274 sdio_release_host(card->func);
1277 /* This function read/write firmware */
1278 static enum
1279 rdwr_status btmrvl_sdio_rdwr_firmware(struct btmrvl_private *priv,
1280 u8 doneflag)
1282 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
1283 int ret, tries;
1284 u8 ctrl_data = 0;
1286 sdio_writeb(card->func, FW_DUMP_HOST_READY, card->reg->fw_dump_ctrl,
1287 &ret);
1289 if (ret) {
1290 BT_ERR("SDIO write err");
1291 return RDWR_STATUS_FAILURE;
1294 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
1295 ctrl_data = sdio_readb(card->func, card->reg->fw_dump_ctrl,
1296 &ret);
1298 if (ret) {
1299 BT_ERR("SDIO read err");
1300 return RDWR_STATUS_FAILURE;
1303 if (ctrl_data == FW_DUMP_DONE)
1304 break;
1305 if (doneflag && ctrl_data == doneflag)
1306 return RDWR_STATUS_DONE;
1307 if (ctrl_data != FW_DUMP_HOST_READY) {
1308 BT_INFO("The ctrl reg was changed, re-try again!");
1309 sdio_writeb(card->func, FW_DUMP_HOST_READY,
1310 card->reg->fw_dump_ctrl, &ret);
1311 if (ret) {
1312 BT_ERR("SDIO write err");
1313 return RDWR_STATUS_FAILURE;
1316 usleep_range(100, 200);
1319 if (ctrl_data == FW_DUMP_HOST_READY) {
1320 BT_ERR("Fail to pull ctrl_data");
1321 return RDWR_STATUS_FAILURE;
1324 return RDWR_STATUS_SUCCESS;
1327 /* This function dump sdio register and memory data */
1328 static void btmrvl_sdio_coredump(struct device *dev)
1330 struct sdio_func *func = dev_to_sdio_func(dev);
1331 struct btmrvl_sdio_card *card;
1332 struct btmrvl_private *priv;
1333 int ret = 0;
1334 unsigned int reg, reg_start, reg_end;
1335 enum rdwr_status stat;
1336 u8 *dbg_ptr, *end_ptr, *fw_dump_data, *fw_dump_ptr;
1337 u8 dump_num = 0, idx, i, read_reg, doneflag = 0;
1338 u32 memory_size, fw_dump_len = 0;
1339 int size = 0;
1341 card = sdio_get_drvdata(func);
1342 priv = card->priv;
1344 /* dump sdio register first */
1345 btmrvl_sdio_dump_regs(priv);
1347 if (!card->supports_fw_dump) {
1348 BT_ERR("Firmware dump not supported for this card!");
1349 return;
1352 for (idx = 0; idx < ARRAY_SIZE(mem_type_mapping_tbl); idx++) {
1353 struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
1355 if (entry->mem_ptr) {
1356 vfree(entry->mem_ptr);
1357 entry->mem_ptr = NULL;
1359 entry->mem_size = 0;
1362 btmrvl_sdio_wakeup_fw(priv);
1363 sdio_claim_host(card->func);
1365 BT_INFO("== btmrvl firmware dump start ==");
1367 stat = btmrvl_sdio_rdwr_firmware(priv, doneflag);
1368 if (stat == RDWR_STATUS_FAILURE)
1369 goto done;
1371 reg = card->reg->fw_dump_start;
1372 /* Read the number of the memories which will dump */
1373 dump_num = sdio_readb(card->func, reg, &ret);
1375 if (ret) {
1376 BT_ERR("SDIO read memory length err");
1377 goto done;
1380 /* Read the length of every memory which will dump */
1381 for (idx = 0; idx < dump_num; idx++) {
1382 struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
1384 stat = btmrvl_sdio_rdwr_firmware(priv, doneflag);
1385 if (stat == RDWR_STATUS_FAILURE)
1386 goto done;
1388 memory_size = 0;
1389 reg = card->reg->fw_dump_start;
1390 for (i = 0; i < 4; i++) {
1391 read_reg = sdio_readb(card->func, reg, &ret);
1392 if (ret) {
1393 BT_ERR("SDIO read err");
1394 goto done;
1396 memory_size |= (read_reg << i*8);
1397 reg++;
1400 if (memory_size == 0) {
1401 BT_INFO("Firmware dump finished!");
1402 sdio_writeb(card->func, FW_DUMP_READ_DONE,
1403 card->reg->fw_dump_ctrl, &ret);
1404 if (ret) {
1405 BT_ERR("SDIO Write MEMDUMP_FINISH ERR");
1406 goto done;
1408 break;
1411 BT_INFO("%s_SIZE=0x%x", entry->mem_name, memory_size);
1412 entry->mem_ptr = vzalloc(memory_size + 1);
1413 entry->mem_size = memory_size;
1414 if (!entry->mem_ptr) {
1415 BT_ERR("Vzalloc %s failed", entry->mem_name);
1416 goto done;
1419 fw_dump_len += (strlen("========Start dump ") +
1420 strlen(entry->mem_name) +
1421 strlen("========\n") +
1422 (memory_size + 1) +
1423 strlen("\n========End dump========\n"));
1425 dbg_ptr = entry->mem_ptr;
1426 end_ptr = dbg_ptr + memory_size;
1428 doneflag = entry->done_flag;
1429 BT_INFO("Start %s output, please wait...",
1430 entry->mem_name);
1432 do {
1433 stat = btmrvl_sdio_rdwr_firmware(priv, doneflag);
1434 if (stat == RDWR_STATUS_FAILURE)
1435 goto done;
1437 reg_start = card->reg->fw_dump_start;
1438 reg_end = card->reg->fw_dump_end;
1439 for (reg = reg_start; reg <= reg_end; reg++) {
1440 *dbg_ptr = sdio_readb(card->func, reg, &ret);
1441 if (ret) {
1442 BT_ERR("SDIO read err");
1443 goto done;
1445 if (dbg_ptr < end_ptr)
1446 dbg_ptr++;
1447 else
1448 BT_ERR("Allocated buffer not enough");
1451 if (stat == RDWR_STATUS_DONE) {
1452 BT_INFO("%s done: size=0x%tx",
1453 entry->mem_name,
1454 dbg_ptr - entry->mem_ptr);
1455 break;
1457 } while (1);
1460 BT_INFO("== btmrvl firmware dump end ==");
1462 done:
1463 sdio_release_host(card->func);
1465 if (fw_dump_len == 0)
1466 return;
1468 fw_dump_data = vzalloc(fw_dump_len + 1);
1469 if (!fw_dump_data) {
1470 BT_ERR("Vzalloc fw_dump_data fail!");
1471 return;
1473 fw_dump_ptr = fw_dump_data;
1475 /* Dump all the memory data into single file, a userspace script will
1476 * be used to split all the memory data to multiple files
1478 BT_INFO("== btmrvl firmware dump to /sys/class/devcoredump start");
1479 for (idx = 0; idx < dump_num; idx++) {
1480 struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
1482 if (entry->mem_ptr) {
1483 size += scnprintf(fw_dump_ptr + size,
1484 fw_dump_len + 1 - size,
1485 "========Start dump %s========\n",
1486 entry->mem_name);
1488 memcpy(fw_dump_ptr + size, entry->mem_ptr,
1489 entry->mem_size);
1490 size += entry->mem_size;
1492 size += scnprintf(fw_dump_ptr + size,
1493 fw_dump_len + 1 - size,
1494 "\n========End dump========\n");
1496 vfree(mem_type_mapping_tbl[idx].mem_ptr);
1497 mem_type_mapping_tbl[idx].mem_ptr = NULL;
1501 /* fw_dump_data will be free in device coredump release function
1502 * after 5 min
1504 dev_coredumpv(&card->func->dev, fw_dump_data, fw_dump_len, GFP_KERNEL);
1505 BT_INFO("== btmrvl firmware dump to /sys/class/devcoredump end");
1508 static int btmrvl_sdio_probe(struct sdio_func *func,
1509 const struct sdio_device_id *id)
1511 int ret = 0;
1512 struct btmrvl_private *priv = NULL;
1513 struct btmrvl_sdio_card *card = NULL;
1515 BT_INFO("vendor=0x%x, device=0x%x, class=%d, fn=%d",
1516 id->vendor, id->device, id->class, func->num);
1518 card = devm_kzalloc(&func->dev, sizeof(*card), GFP_KERNEL);
1519 if (!card)
1520 return -ENOMEM;
1522 card->func = func;
1524 if (id->driver_data) {
1525 struct btmrvl_sdio_device *data = (void *) id->driver_data;
1526 card->helper = data->helper;
1527 card->firmware = data->firmware;
1528 card->reg = data->reg;
1529 card->sd_blksz_fw_dl = data->sd_blksz_fw_dl;
1530 card->support_pscan_win_report = data->support_pscan_win_report;
1531 card->supports_fw_dump = data->supports_fw_dump;
1534 if (btmrvl_sdio_register_dev(card) < 0) {
1535 BT_ERR("Failed to register BT device!");
1536 return -ENODEV;
1539 /* Disable the interrupts on the card */
1540 btmrvl_sdio_disable_host_int(card);
1542 if (btmrvl_sdio_download_fw(card)) {
1543 BT_ERR("Downloading firmware failed!");
1544 ret = -ENODEV;
1545 goto unreg_dev;
1548 btmrvl_sdio_enable_host_int(card);
1550 /* Device tree node parsing and platform specific configuration*/
1551 btmrvl_sdio_probe_of(&func->dev, card);
1553 priv = btmrvl_add_card(card);
1554 if (!priv) {
1555 BT_ERR("Initializing card failed!");
1556 ret = -ENODEV;
1557 goto disable_host_int;
1560 card->priv = priv;
1562 /* Initialize the interface specific function pointers */
1563 priv->hw_host_to_card = btmrvl_sdio_host_to_card;
1564 priv->hw_wakeup_firmware = btmrvl_sdio_wakeup_fw;
1565 priv->hw_process_int_status = btmrvl_sdio_process_int_status;
1567 if (btmrvl_register_hdev(priv)) {
1568 BT_ERR("Register hdev failed!");
1569 ret = -ENODEV;
1570 goto disable_host_int;
1573 return 0;
1575 disable_host_int:
1576 btmrvl_sdio_disable_host_int(card);
1577 unreg_dev:
1578 btmrvl_sdio_unregister_dev(card);
1579 return ret;
1582 static void btmrvl_sdio_remove(struct sdio_func *func)
1584 struct btmrvl_sdio_card *card;
1586 if (func) {
1587 card = sdio_get_drvdata(func);
1588 if (card) {
1589 /* Send SHUTDOWN command & disable interrupt
1590 * if user removes the module.
1592 if (user_rmmod) {
1593 btmrvl_send_module_cfg_cmd(card->priv,
1594 MODULE_SHUTDOWN_REQ);
1595 btmrvl_sdio_disable_host_int(card);
1598 BT_DBG("unregister dev");
1599 card->priv->surprise_removed = true;
1600 btmrvl_sdio_unregister_dev(card);
1601 btmrvl_remove_card(card->priv);
1606 static int btmrvl_sdio_suspend(struct device *dev)
1608 struct sdio_func *func = dev_to_sdio_func(dev);
1609 struct btmrvl_sdio_card *card;
1610 struct btmrvl_private *priv;
1611 mmc_pm_flag_t pm_flags;
1612 struct hci_dev *hcidev;
1614 if (func) {
1615 pm_flags = sdio_get_host_pm_caps(func);
1616 BT_DBG("%s: suspend: PM flags = 0x%x", sdio_func_id(func),
1617 pm_flags);
1618 if (!(pm_flags & MMC_PM_KEEP_POWER)) {
1619 BT_ERR("%s: cannot remain alive while suspended",
1620 sdio_func_id(func));
1621 return -ENOSYS;
1623 card = sdio_get_drvdata(func);
1624 if (!card || !card->priv) {
1625 BT_ERR("card or priv structure is not valid");
1626 return 0;
1628 } else {
1629 BT_ERR("sdio_func is not specified");
1630 return 0;
1633 /* Enable platform specific wakeup interrupt */
1634 if (card->plt_wake_cfg && card->plt_wake_cfg->irq_bt >= 0 &&
1635 device_may_wakeup(dev)) {
1636 card->plt_wake_cfg->wake_by_bt = false;
1637 enable_irq(card->plt_wake_cfg->irq_bt);
1638 enable_irq_wake(card->plt_wake_cfg->irq_bt);
1641 priv = card->priv;
1642 priv->adapter->is_suspending = true;
1643 hcidev = priv->btmrvl_dev.hcidev;
1644 BT_DBG("%s: SDIO suspend", hcidev->name);
1645 hci_suspend_dev(hcidev);
1647 if (priv->adapter->hs_state != HS_ACTIVATED) {
1648 if (btmrvl_enable_hs(priv)) {
1649 BT_ERR("HS not activated, suspend failed!");
1650 /* Disable platform specific wakeup interrupt */
1651 if (card->plt_wake_cfg &&
1652 card->plt_wake_cfg->irq_bt >= 0 &&
1653 device_may_wakeup(dev)) {
1654 disable_irq_wake(card->plt_wake_cfg->irq_bt);
1655 disable_irq(card->plt_wake_cfg->irq_bt);
1658 priv->adapter->is_suspending = false;
1659 return -EBUSY;
1663 priv->adapter->is_suspending = false;
1664 priv->adapter->is_suspended = true;
1666 /* We will keep the power when hs enabled successfully */
1667 if (priv->adapter->hs_state == HS_ACTIVATED) {
1668 BT_DBG("suspend with MMC_PM_KEEP_POWER");
1669 return sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
1672 BT_DBG("suspend without MMC_PM_KEEP_POWER");
1673 return 0;
1676 static int btmrvl_sdio_resume(struct device *dev)
1678 struct sdio_func *func = dev_to_sdio_func(dev);
1679 struct btmrvl_sdio_card *card;
1680 struct btmrvl_private *priv;
1681 mmc_pm_flag_t pm_flags;
1682 struct hci_dev *hcidev;
1684 if (func) {
1685 pm_flags = sdio_get_host_pm_caps(func);
1686 BT_DBG("%s: resume: PM flags = 0x%x", sdio_func_id(func),
1687 pm_flags);
1688 card = sdio_get_drvdata(func);
1689 if (!card || !card->priv) {
1690 BT_ERR("card or priv structure is not valid");
1691 return 0;
1693 } else {
1694 BT_ERR("sdio_func is not specified");
1695 return 0;
1697 priv = card->priv;
1699 if (!priv->adapter->is_suspended) {
1700 BT_DBG("device already resumed");
1701 return 0;
1704 priv->hw_wakeup_firmware(priv);
1705 priv->adapter->hs_state = HS_DEACTIVATED;
1706 hcidev = priv->btmrvl_dev.hcidev;
1707 BT_DBG("%s: HS DEACTIVATED in resume!", hcidev->name);
1708 priv->adapter->is_suspended = false;
1709 BT_DBG("%s: SDIO resume", hcidev->name);
1710 hci_resume_dev(hcidev);
1712 /* Disable platform specific wakeup interrupt */
1713 if (card->plt_wake_cfg && card->plt_wake_cfg->irq_bt >= 0 &&
1714 device_may_wakeup(dev)) {
1715 disable_irq_wake(card->plt_wake_cfg->irq_bt);
1716 disable_irq(card->plt_wake_cfg->irq_bt);
1717 if (card->plt_wake_cfg->wake_by_bt)
1718 /* Undo our disable, since interrupt handler already
1719 * did this.
1721 enable_irq(card->plt_wake_cfg->irq_bt);
1724 return 0;
1727 static const struct dev_pm_ops btmrvl_sdio_pm_ops = {
1728 .suspend = btmrvl_sdio_suspend,
1729 .resume = btmrvl_sdio_resume,
1732 static struct sdio_driver bt_mrvl_sdio = {
1733 .name = "btmrvl_sdio",
1734 .id_table = btmrvl_sdio_ids,
1735 .probe = btmrvl_sdio_probe,
1736 .remove = btmrvl_sdio_remove,
1737 .drv = {
1738 .coredump = btmrvl_sdio_coredump,
1739 .pm = &btmrvl_sdio_pm_ops,
1743 static int __init btmrvl_sdio_init_module(void)
1745 if (sdio_register_driver(&bt_mrvl_sdio) != 0) {
1746 BT_ERR("SDIO Driver Registration Failed");
1747 return -ENODEV;
1750 /* Clear the flag in case user removes the card. */
1751 user_rmmod = 0;
1753 return 0;
1756 static void __exit btmrvl_sdio_exit_module(void)
1758 /* Set the flag as user is removing this module. */
1759 user_rmmod = 1;
1761 sdio_unregister_driver(&bt_mrvl_sdio);
1764 module_init(btmrvl_sdio_init_module);
1765 module_exit(btmrvl_sdio_exit_module);
1767 MODULE_AUTHOR("Marvell International Ltd.");
1768 MODULE_DESCRIPTION("Marvell BT-over-SDIO driver ver " VERSION);
1769 MODULE_VERSION(VERSION);
1770 MODULE_LICENSE("GPL v2");
1771 MODULE_FIRMWARE("mrvl/sd8688_helper.bin");
1772 MODULE_FIRMWARE("mrvl/sd8688.bin");
1773 MODULE_FIRMWARE("mrvl/sd8787_uapsta.bin");
1774 MODULE_FIRMWARE("mrvl/sd8797_uapsta.bin");
1775 MODULE_FIRMWARE("mrvl/sd8887_uapsta.bin");
1776 MODULE_FIRMWARE("mrvl/sd8897_uapsta.bin");
1777 MODULE_FIRMWARE("mrvl/sdsd8977_combo_v2.bin");
1778 MODULE_FIRMWARE("mrvl/sd8987_uapsta.bin");
1779 MODULE_FIRMWARE("mrvl/sdsd8997_combo_v4.bin");