Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / bluetooth / btmrvl_sdio.c
blob33d58b30c5acfc23ab4cf7c0d63e6980e8f017fe
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>
23 #include <linux/suspend.h>
25 #include <linux/mmc/sdio_ids.h>
26 #include <linux/mmc/sdio_func.h>
27 #include <linux/module.h>
28 #include <linux/devcoredump.h>
30 #include <net/bluetooth/bluetooth.h>
31 #include <net/bluetooth/hci_core.h>
33 #include "btmrvl_drv.h"
34 #include "btmrvl_sdio.h"
36 #define VERSION "1.0"
38 static struct memory_type_mapping mem_type_mapping_tbl[] = {
39 {"ITCM", NULL, 0, 0xF0},
40 {"DTCM", NULL, 0, 0xF1},
41 {"SQRAM", NULL, 0, 0xF2},
42 {"APU", NULL, 0, 0xF3},
43 {"CIU", NULL, 0, 0xF4},
44 {"ICU", NULL, 0, 0xF5},
45 {"MAC", NULL, 0, 0xF6},
46 {"EXT7", NULL, 0, 0xF7},
47 {"EXT8", NULL, 0, 0xF8},
48 {"EXT9", NULL, 0, 0xF9},
49 {"EXT10", NULL, 0, 0xFA},
50 {"EXT11", NULL, 0, 0xFB},
51 {"EXT12", NULL, 0, 0xFC},
52 {"EXT13", NULL, 0, 0xFD},
53 {"EXTLAST", NULL, 0, 0xFE},
56 static const struct of_device_id btmrvl_sdio_of_match_table[] = {
57 { .compatible = "marvell,sd8897-bt" },
58 { .compatible = "marvell,sd8997-bt" },
59 { }
62 static irqreturn_t btmrvl_wake_irq_bt(int irq, void *priv)
64 struct btmrvl_sdio_card *card = priv;
65 struct device *dev = &card->func->dev;
66 struct btmrvl_plt_wake_cfg *cfg = card->plt_wake_cfg;
68 dev_info(dev, "wake by bt\n");
69 cfg->wake_by_bt = true;
70 disable_irq_nosync(irq);
72 pm_wakeup_event(dev, 0);
73 pm_system_wakeup();
75 return IRQ_HANDLED;
78 /* This function parses device tree node using mmc subnode devicetree API.
79 * The device node is saved in card->plt_of_node.
80 * If the device tree node exists and includes interrupts attributes, this
81 * function will request platform specific wakeup interrupt.
83 static int btmrvl_sdio_probe_of(struct device *dev,
84 struct btmrvl_sdio_card *card)
86 struct btmrvl_plt_wake_cfg *cfg;
87 int ret;
89 if (!dev->of_node ||
90 !of_match_node(btmrvl_sdio_of_match_table, dev->of_node)) {
91 dev_info(dev, "sdio device tree data not available\n");
92 return -1;
95 card->plt_of_node = dev->of_node;
97 card->plt_wake_cfg = devm_kzalloc(dev, sizeof(*card->plt_wake_cfg),
98 GFP_KERNEL);
99 cfg = card->plt_wake_cfg;
100 if (cfg && card->plt_of_node) {
101 cfg->irq_bt = irq_of_parse_and_map(card->plt_of_node, 0);
102 if (!cfg->irq_bt) {
103 dev_err(dev, "fail to parse irq_bt from device tree\n");
104 cfg->irq_bt = -1;
105 } else {
106 ret = devm_request_irq(dev, cfg->irq_bt,
107 btmrvl_wake_irq_bt,
108 0, "bt_wake", card);
109 if (ret) {
110 dev_err(dev,
111 "Failed to request irq_bt %d (%d)\n",
112 cfg->irq_bt, ret);
115 /* Configure wakeup (enabled by default) */
116 device_init_wakeup(dev, true);
117 disable_irq(cfg->irq_bt);
121 return 0;
124 /* The btmrvl_sdio_remove() callback function is called
125 * when user removes this module from kernel space or ejects
126 * the card from the slot. The driver handles these 2 cases
127 * differently.
128 * If the user is removing the module, a MODULE_SHUTDOWN_REQ
129 * command is sent to firmware and interrupt will be disabled.
130 * If the card is removed, there is no need to send command
131 * or disable interrupt.
133 * The variable 'user_rmmod' is used to distinguish these two
134 * scenarios. This flag is initialized as FALSE in case the card
135 * is removed, and will be set to TRUE for module removal when
136 * module_exit function is called.
138 static u8 user_rmmod;
139 static u8 sdio_ireg;
141 static const struct btmrvl_sdio_card_reg btmrvl_reg_8688 = {
142 .cfg = 0x03,
143 .host_int_mask = 0x04,
144 .host_intstatus = 0x05,
145 .card_status = 0x20,
146 .sq_read_base_addr_a0 = 0x10,
147 .sq_read_base_addr_a1 = 0x11,
148 .card_fw_status0 = 0x40,
149 .card_fw_status1 = 0x41,
150 .card_rx_len = 0x42,
151 .card_rx_unit = 0x43,
152 .io_port_0 = 0x00,
153 .io_port_1 = 0x01,
154 .io_port_2 = 0x02,
155 .int_read_to_clear = false,
157 static const struct btmrvl_sdio_card_reg btmrvl_reg_87xx = {
158 .cfg = 0x00,
159 .host_int_mask = 0x02,
160 .host_intstatus = 0x03,
161 .card_status = 0x30,
162 .sq_read_base_addr_a0 = 0x40,
163 .sq_read_base_addr_a1 = 0x41,
164 .card_revision = 0x5c,
165 .card_fw_status0 = 0x60,
166 .card_fw_status1 = 0x61,
167 .card_rx_len = 0x62,
168 .card_rx_unit = 0x63,
169 .io_port_0 = 0x78,
170 .io_port_1 = 0x79,
171 .io_port_2 = 0x7a,
172 .int_read_to_clear = false,
175 static const struct btmrvl_sdio_card_reg btmrvl_reg_8887 = {
176 .cfg = 0x00,
177 .host_int_mask = 0x08,
178 .host_intstatus = 0x0C,
179 .card_status = 0x5C,
180 .sq_read_base_addr_a0 = 0x6C,
181 .sq_read_base_addr_a1 = 0x6D,
182 .card_revision = 0xC8,
183 .card_fw_status0 = 0x88,
184 .card_fw_status1 = 0x89,
185 .card_rx_len = 0x8A,
186 .card_rx_unit = 0x8B,
187 .io_port_0 = 0xE4,
188 .io_port_1 = 0xE5,
189 .io_port_2 = 0xE6,
190 .int_read_to_clear = true,
191 .host_int_rsr = 0x04,
192 .card_misc_cfg = 0xD8,
195 static const struct btmrvl_sdio_card_reg btmrvl_reg_8897 = {
196 .cfg = 0x00,
197 .host_int_mask = 0x02,
198 .host_intstatus = 0x03,
199 .card_status = 0x50,
200 .sq_read_base_addr_a0 = 0x60,
201 .sq_read_base_addr_a1 = 0x61,
202 .card_revision = 0xbc,
203 .card_fw_status0 = 0xc0,
204 .card_fw_status1 = 0xc1,
205 .card_rx_len = 0xc2,
206 .card_rx_unit = 0xc3,
207 .io_port_0 = 0xd8,
208 .io_port_1 = 0xd9,
209 .io_port_2 = 0xda,
210 .int_read_to_clear = true,
211 .host_int_rsr = 0x01,
212 .card_misc_cfg = 0xcc,
213 .fw_dump_ctrl = 0xe2,
214 .fw_dump_start = 0xe3,
215 .fw_dump_end = 0xea,
218 static const struct btmrvl_sdio_card_reg btmrvl_reg_89xx = {
219 .cfg = 0x00,
220 .host_int_mask = 0x08,
221 .host_intstatus = 0x0c,
222 .card_status = 0x5c,
223 .sq_read_base_addr_a0 = 0xf8,
224 .sq_read_base_addr_a1 = 0xf9,
225 .card_revision = 0xc8,
226 .card_fw_status0 = 0xe8,
227 .card_fw_status1 = 0xe9,
228 .card_rx_len = 0xea,
229 .card_rx_unit = 0xeb,
230 .io_port_0 = 0xe4,
231 .io_port_1 = 0xe5,
232 .io_port_2 = 0xe6,
233 .int_read_to_clear = true,
234 .host_int_rsr = 0x04,
235 .card_misc_cfg = 0xd8,
236 .fw_dump_ctrl = 0xf0,
237 .fw_dump_start = 0xf1,
238 .fw_dump_end = 0xf8,
241 static const struct btmrvl_sdio_device btmrvl_sdio_sd8688 = {
242 .helper = "mrvl/sd8688_helper.bin",
243 .firmware = "mrvl/sd8688.bin",
244 .reg = &btmrvl_reg_8688,
245 .support_pscan_win_report = false,
246 .sd_blksz_fw_dl = 64,
247 .supports_fw_dump = false,
250 static const struct btmrvl_sdio_device btmrvl_sdio_sd8787 = {
251 .helper = NULL,
252 .firmware = "mrvl/sd8787_uapsta.bin",
253 .reg = &btmrvl_reg_87xx,
254 .support_pscan_win_report = false,
255 .sd_blksz_fw_dl = 256,
256 .supports_fw_dump = false,
259 static const struct btmrvl_sdio_device btmrvl_sdio_sd8797 = {
260 .helper = NULL,
261 .firmware = "mrvl/sd8797_uapsta.bin",
262 .reg = &btmrvl_reg_87xx,
263 .support_pscan_win_report = false,
264 .sd_blksz_fw_dl = 256,
265 .supports_fw_dump = false,
268 static const struct btmrvl_sdio_device btmrvl_sdio_sd8887 = {
269 .helper = NULL,
270 .firmware = "mrvl/sd8887_uapsta.bin",
271 .reg = &btmrvl_reg_8887,
272 .support_pscan_win_report = true,
273 .sd_blksz_fw_dl = 256,
274 .supports_fw_dump = false,
277 static const struct btmrvl_sdio_device btmrvl_sdio_sd8897 = {
278 .helper = NULL,
279 .firmware = "mrvl/sd8897_uapsta.bin",
280 .reg = &btmrvl_reg_8897,
281 .support_pscan_win_report = true,
282 .sd_blksz_fw_dl = 256,
283 .supports_fw_dump = true,
286 static const struct btmrvl_sdio_device btmrvl_sdio_sd8977 = {
287 .helper = NULL,
288 .firmware = "mrvl/sdsd8977_combo_v2.bin",
289 .reg = &btmrvl_reg_89xx,
290 .support_pscan_win_report = true,
291 .sd_blksz_fw_dl = 256,
292 .supports_fw_dump = true,
295 static const struct btmrvl_sdio_device btmrvl_sdio_sd8987 = {
296 .helper = NULL,
297 .firmware = "mrvl/sd8987_uapsta.bin",
298 .reg = &btmrvl_reg_89xx,
299 .support_pscan_win_report = true,
300 .sd_blksz_fw_dl = 256,
301 .supports_fw_dump = true,
304 static const struct btmrvl_sdio_device btmrvl_sdio_sd8997 = {
305 .helper = NULL,
306 .firmware = "mrvl/sdsd8997_combo_v4.bin",
307 .reg = &btmrvl_reg_89xx,
308 .support_pscan_win_report = true,
309 .sd_blksz_fw_dl = 256,
310 .supports_fw_dump = true,
313 static const struct sdio_device_id btmrvl_sdio_ids[] = {
314 /* Marvell SD8688 Bluetooth device */
315 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8688_BT),
316 .driver_data = (unsigned long)&btmrvl_sdio_sd8688 },
317 /* Marvell SD8787 Bluetooth device */
318 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8787_BT),
319 .driver_data = (unsigned long)&btmrvl_sdio_sd8787 },
320 /* Marvell SD8787 Bluetooth AMP device */
321 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8787_BT_AMP),
322 .driver_data = (unsigned long)&btmrvl_sdio_sd8787 },
323 /* Marvell SD8797 Bluetooth device */
324 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8797_BT),
325 .driver_data = (unsigned long)&btmrvl_sdio_sd8797 },
326 /* Marvell SD8887 Bluetooth device */
327 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8887_BT),
328 .driver_data = (unsigned long)&btmrvl_sdio_sd8887 },
329 /* Marvell SD8897 Bluetooth device */
330 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8897_BT),
331 .driver_data = (unsigned long)&btmrvl_sdio_sd8897 },
332 /* Marvell SD8977 Bluetooth device */
333 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8977_BT),
334 .driver_data = (unsigned long)&btmrvl_sdio_sd8977 },
335 /* Marvell SD8987 Bluetooth device */
336 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8987_BT),
337 .driver_data = (unsigned long)&btmrvl_sdio_sd8987 },
338 /* Marvell SD8997 Bluetooth device */
339 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8997_BT),
340 .driver_data = (unsigned long)&btmrvl_sdio_sd8997 },
342 { } /* Terminating entry */
345 MODULE_DEVICE_TABLE(sdio, btmrvl_sdio_ids);
347 static int btmrvl_sdio_get_rx_unit(struct btmrvl_sdio_card *card)
349 u8 reg;
350 int ret;
352 reg = sdio_readb(card->func, card->reg->card_rx_unit, &ret);
353 if (!ret)
354 card->rx_unit = reg;
356 return ret;
359 static int btmrvl_sdio_read_fw_status(struct btmrvl_sdio_card *card, u16 *dat)
361 u8 fws0, fws1;
362 int ret;
364 *dat = 0;
366 fws0 = sdio_readb(card->func, card->reg->card_fw_status0, &ret);
367 if (ret)
368 return -EIO;
370 fws1 = sdio_readb(card->func, card->reg->card_fw_status1, &ret);
371 if (ret)
372 return -EIO;
374 *dat = (((u16) fws1) << 8) | fws0;
376 return 0;
379 static int btmrvl_sdio_read_rx_len(struct btmrvl_sdio_card *card, u16 *dat)
381 u8 reg;
382 int ret;
384 reg = sdio_readb(card->func, card->reg->card_rx_len, &ret);
385 if (!ret)
386 *dat = (u16) reg << card->rx_unit;
388 return ret;
391 static int btmrvl_sdio_enable_host_int_mask(struct btmrvl_sdio_card *card,
392 u8 mask)
394 int ret;
396 sdio_writeb(card->func, mask, card->reg->host_int_mask, &ret);
397 if (ret) {
398 BT_ERR("Unable to enable the host interrupt!");
399 ret = -EIO;
402 return ret;
405 static int btmrvl_sdio_disable_host_int_mask(struct btmrvl_sdio_card *card,
406 u8 mask)
408 u8 host_int_mask;
409 int ret;
411 host_int_mask = sdio_readb(card->func, card->reg->host_int_mask, &ret);
412 if (ret)
413 return -EIO;
415 host_int_mask &= ~mask;
417 sdio_writeb(card->func, host_int_mask, card->reg->host_int_mask, &ret);
418 if (ret < 0) {
419 BT_ERR("Unable to disable the host interrupt!");
420 return -EIO;
423 return 0;
426 static int btmrvl_sdio_poll_card_status(struct btmrvl_sdio_card *card, u8 bits)
428 unsigned int tries;
429 u8 status;
430 int ret;
432 for (tries = 0; tries < MAX_POLL_TRIES * 1000; tries++) {
433 status = sdio_readb(card->func, card->reg->card_status, &ret);
434 if (ret)
435 goto failed;
436 if ((status & bits) == bits)
437 return ret;
439 udelay(1);
442 ret = -ETIMEDOUT;
444 failed:
445 BT_ERR("FAILED! ret=%d", ret);
447 return ret;
450 static int btmrvl_sdio_verify_fw_download(struct btmrvl_sdio_card *card,
451 int pollnum)
453 u16 firmwarestat;
454 int tries, ret;
456 /* Wait for firmware to become ready */
457 for (tries = 0; tries < pollnum; tries++) {
458 sdio_claim_host(card->func);
459 ret = btmrvl_sdio_read_fw_status(card, &firmwarestat);
460 sdio_release_host(card->func);
461 if (ret < 0)
462 continue;
464 if (firmwarestat == FIRMWARE_READY)
465 return 0;
467 msleep(100);
470 return -ETIMEDOUT;
473 static int btmrvl_sdio_download_helper(struct btmrvl_sdio_card *card)
475 const struct firmware *fw_helper = NULL;
476 const u8 *helper = NULL;
477 int ret;
478 void *tmphlprbuf = NULL;
479 int tmphlprbufsz, hlprblknow, helperlen;
480 u8 *helperbuf;
481 u32 tx_len;
483 ret = request_firmware(&fw_helper, card->helper,
484 &card->func->dev);
485 if ((ret < 0) || !fw_helper) {
486 BT_ERR("request_firmware(helper) failed, error code = %d",
487 ret);
488 ret = -ENOENT;
489 goto done;
492 helper = fw_helper->data;
493 helperlen = fw_helper->size;
495 BT_DBG("Downloading helper image (%d bytes), block size %d bytes",
496 helperlen, SDIO_BLOCK_SIZE);
498 tmphlprbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
500 tmphlprbuf = kzalloc(tmphlprbufsz, GFP_KERNEL);
501 if (!tmphlprbuf) {
502 BT_ERR("Unable to allocate buffer for helper."
503 " Terminating download");
504 ret = -ENOMEM;
505 goto done;
508 helperbuf = (u8 *) ALIGN_ADDR(tmphlprbuf, BTSDIO_DMA_ALIGN);
510 /* Perform helper data transfer */
511 tx_len = (FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE)
512 - SDIO_HEADER_LEN;
513 hlprblknow = 0;
515 do {
516 ret = btmrvl_sdio_poll_card_status(card,
517 CARD_IO_READY | DN_LD_CARD_RDY);
518 if (ret < 0) {
519 BT_ERR("Helper download poll status timeout @ %d",
520 hlprblknow);
521 goto done;
524 /* Check if there is more data? */
525 if (hlprblknow >= helperlen)
526 break;
528 if (helperlen - hlprblknow < tx_len)
529 tx_len = helperlen - hlprblknow;
531 /* Little-endian */
532 helperbuf[0] = ((tx_len & 0x000000ff) >> 0);
533 helperbuf[1] = ((tx_len & 0x0000ff00) >> 8);
534 helperbuf[2] = ((tx_len & 0x00ff0000) >> 16);
535 helperbuf[3] = ((tx_len & 0xff000000) >> 24);
537 memcpy(&helperbuf[SDIO_HEADER_LEN], &helper[hlprblknow],
538 tx_len);
540 /* Now send the data */
541 ret = sdio_writesb(card->func, card->ioport, helperbuf,
542 FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE);
543 if (ret < 0) {
544 BT_ERR("IO error during helper download @ %d",
545 hlprblknow);
546 goto done;
549 hlprblknow += tx_len;
550 } while (true);
552 BT_DBG("Transferring helper image EOF block");
554 memset(helperbuf, 0x0, SDIO_BLOCK_SIZE);
556 ret = sdio_writesb(card->func, card->ioport, helperbuf,
557 SDIO_BLOCK_SIZE);
558 if (ret < 0) {
559 BT_ERR("IO error in writing helper image EOF block");
560 goto done;
563 ret = 0;
565 done:
566 kfree(tmphlprbuf);
567 release_firmware(fw_helper);
568 return ret;
571 static int btmrvl_sdio_download_fw_w_helper(struct btmrvl_sdio_card *card)
573 const struct firmware *fw_firmware = NULL;
574 const u8 *firmware = NULL;
575 int firmwarelen, tmpfwbufsz, ret;
576 unsigned int tries, offset;
577 u8 base0, base1;
578 void *tmpfwbuf = NULL;
579 u8 *fwbuf;
580 u16 len, blksz_dl = card->sd_blksz_fw_dl;
581 int txlen = 0, tx_blocks = 0, count = 0;
583 ret = request_firmware(&fw_firmware, card->firmware,
584 &card->func->dev);
585 if ((ret < 0) || !fw_firmware) {
586 BT_ERR("request_firmware(firmware) failed, error code = %d",
587 ret);
588 ret = -ENOENT;
589 goto done;
592 firmware = fw_firmware->data;
593 firmwarelen = fw_firmware->size;
595 BT_DBG("Downloading FW image (%d bytes)", firmwarelen);
597 tmpfwbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
598 tmpfwbuf = kzalloc(tmpfwbufsz, GFP_KERNEL);
599 if (!tmpfwbuf) {
600 BT_ERR("Unable to allocate buffer for firmware."
601 " Terminating download");
602 ret = -ENOMEM;
603 goto done;
606 /* Ensure aligned firmware buffer */
607 fwbuf = (u8 *) ALIGN_ADDR(tmpfwbuf, BTSDIO_DMA_ALIGN);
609 /* Perform firmware data transfer */
610 offset = 0;
611 do {
612 ret = btmrvl_sdio_poll_card_status(card,
613 CARD_IO_READY | DN_LD_CARD_RDY);
614 if (ret < 0) {
615 BT_ERR("FW download with helper poll status"
616 " timeout @ %d", offset);
617 goto done;
620 /* Check if there is more data ? */
621 if (offset >= firmwarelen)
622 break;
624 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
625 base0 = sdio_readb(card->func,
626 card->reg->sq_read_base_addr_a0, &ret);
627 if (ret) {
628 BT_ERR("BASE0 register read failed:"
629 " base0 = 0x%04X(%d)."
630 " Terminating download",
631 base0, base0);
632 ret = -EIO;
633 goto done;
635 base1 = sdio_readb(card->func,
636 card->reg->sq_read_base_addr_a1, &ret);
637 if (ret) {
638 BT_ERR("BASE1 register read failed:"
639 " base1 = 0x%04X(%d)."
640 " Terminating download",
641 base1, base1);
642 ret = -EIO;
643 goto done;
646 len = (((u16) base1) << 8) | base0;
647 if (len)
648 break;
650 udelay(10);
653 if (!len)
654 break;
655 else if (len > BTM_UPLD_SIZE) {
656 BT_ERR("FW download failure @%d, invalid length %d",
657 offset, len);
658 ret = -EINVAL;
659 goto done;
662 txlen = len;
664 if (len & BIT(0)) {
665 count++;
666 if (count > MAX_WRITE_IOMEM_RETRY) {
667 BT_ERR("FW download failure @%d, "
668 "over max retry count", offset);
669 ret = -EIO;
670 goto done;
672 BT_ERR("FW CRC error indicated by the helper: "
673 "len = 0x%04X, txlen = %d", len, txlen);
674 len &= ~BIT(0);
675 /* Set txlen to 0 so as to resend from same offset */
676 txlen = 0;
677 } else {
678 count = 0;
680 /* Last block ? */
681 if (firmwarelen - offset < txlen)
682 txlen = firmwarelen - offset;
684 tx_blocks = DIV_ROUND_UP(txlen, blksz_dl);
686 memcpy(fwbuf, &firmware[offset], txlen);
689 ret = sdio_writesb(card->func, card->ioport, fwbuf,
690 tx_blocks * blksz_dl);
692 if (ret < 0) {
693 BT_ERR("FW download, writesb(%d) failed @%d",
694 count, offset);
695 sdio_writeb(card->func, HOST_CMD53_FIN,
696 card->reg->cfg, &ret);
697 if (ret)
698 BT_ERR("writeb failed (CFG)");
701 offset += txlen;
702 } while (true);
704 BT_INFO("FW download over, size %d bytes", offset);
706 ret = 0;
708 done:
709 kfree(tmpfwbuf);
710 release_firmware(fw_firmware);
711 return ret;
714 static int btmrvl_sdio_card_to_host(struct btmrvl_private *priv)
716 u16 buf_len = 0;
717 int ret, num_blocks, blksz;
718 struct sk_buff *skb = NULL;
719 u32 type;
720 u8 *payload;
721 struct hci_dev *hdev = priv->btmrvl_dev.hcidev;
722 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
724 if (!card || !card->func) {
725 BT_ERR("card or function is NULL!");
726 ret = -EINVAL;
727 goto exit;
730 /* Read the length of data to be transferred */
731 ret = btmrvl_sdio_read_rx_len(card, &buf_len);
732 if (ret < 0) {
733 BT_ERR("read rx_len failed");
734 ret = -EIO;
735 goto exit;
738 blksz = SDIO_BLOCK_SIZE;
739 num_blocks = DIV_ROUND_UP(buf_len, blksz);
741 if (buf_len <= SDIO_HEADER_LEN
742 || (num_blocks * blksz) > ALLOC_BUF_SIZE) {
743 BT_ERR("invalid packet length: %d", buf_len);
744 ret = -EINVAL;
745 goto exit;
748 /* Allocate buffer */
749 skb = bt_skb_alloc(num_blocks * blksz + BTSDIO_DMA_ALIGN, GFP_KERNEL);
750 if (!skb) {
751 BT_ERR("No free skb");
752 ret = -ENOMEM;
753 goto exit;
756 if ((unsigned long) skb->data & (BTSDIO_DMA_ALIGN - 1)) {
757 skb_put(skb, (unsigned long) skb->data &
758 (BTSDIO_DMA_ALIGN - 1));
759 skb_pull(skb, (unsigned long) skb->data &
760 (BTSDIO_DMA_ALIGN - 1));
763 payload = skb->data;
765 ret = sdio_readsb(card->func, payload, card->ioport,
766 num_blocks * blksz);
767 if (ret < 0) {
768 BT_ERR("readsb failed: %d", ret);
769 ret = -EIO;
770 goto exit;
773 /* This is SDIO specific header length: byte[2][1][0], type: byte[3]
774 * (HCI_COMMAND = 1, ACL_DATA = 2, SCO_DATA = 3, 0xFE = Vendor)
777 buf_len = payload[0];
778 buf_len |= payload[1] << 8;
779 buf_len |= payload[2] << 16;
781 if (buf_len > blksz * num_blocks) {
782 BT_ERR("Skip incorrect packet: hdrlen %d buffer %d",
783 buf_len, blksz * num_blocks);
784 ret = -EIO;
785 goto exit;
788 type = payload[3];
790 switch (type) {
791 case HCI_ACLDATA_PKT:
792 case HCI_SCODATA_PKT:
793 case HCI_EVENT_PKT:
794 hci_skb_pkt_type(skb) = type;
795 skb_put(skb, buf_len);
796 skb_pull(skb, SDIO_HEADER_LEN);
798 if (type == HCI_EVENT_PKT) {
799 if (btmrvl_check_evtpkt(priv, skb))
800 hci_recv_frame(hdev, skb);
801 } else {
802 hci_recv_frame(hdev, skb);
805 hdev->stat.byte_rx += buf_len;
806 break;
808 case MRVL_VENDOR_PKT:
809 hci_skb_pkt_type(skb) = HCI_VENDOR_PKT;
810 skb_put(skb, buf_len);
811 skb_pull(skb, SDIO_HEADER_LEN);
813 if (btmrvl_process_event(priv, skb))
814 hci_recv_frame(hdev, skb);
816 hdev->stat.byte_rx += buf_len;
817 break;
819 default:
820 BT_ERR("Unknown packet type:%d", type);
821 BT_ERR("hex: %*ph", blksz * num_blocks, payload);
823 kfree_skb(skb);
824 skb = NULL;
825 break;
828 exit:
829 if (ret) {
830 hdev->stat.err_rx++;
831 kfree_skb(skb);
834 return ret;
837 static int btmrvl_sdio_process_int_status(struct btmrvl_private *priv)
839 ulong flags;
840 u8 ireg;
841 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
843 spin_lock_irqsave(&priv->driver_lock, flags);
844 ireg = sdio_ireg;
845 sdio_ireg = 0;
846 spin_unlock_irqrestore(&priv->driver_lock, flags);
848 sdio_claim_host(card->func);
849 if (ireg & DN_LD_HOST_INT_STATUS) {
850 if (priv->btmrvl_dev.tx_dnld_rdy)
851 BT_DBG("tx_done already received: "
852 " int_status=0x%x", ireg);
853 else
854 priv->btmrvl_dev.tx_dnld_rdy = true;
857 if (ireg & UP_LD_HOST_INT_STATUS)
858 btmrvl_sdio_card_to_host(priv);
860 sdio_release_host(card->func);
862 return 0;
865 static int btmrvl_sdio_read_to_clear(struct btmrvl_sdio_card *card, u8 *ireg)
867 struct btmrvl_adapter *adapter = card->priv->adapter;
868 int ret;
870 ret = sdio_readsb(card->func, adapter->hw_regs, 0, SDIO_BLOCK_SIZE);
871 if (ret) {
872 BT_ERR("sdio_readsb: read int hw_regs failed: %d", ret);
873 return ret;
876 *ireg = adapter->hw_regs[card->reg->host_intstatus];
877 BT_DBG("hw_regs[%#x]=%#x", card->reg->host_intstatus, *ireg);
879 return 0;
882 static int btmrvl_sdio_write_to_clear(struct btmrvl_sdio_card *card, u8 *ireg)
884 int ret;
886 *ireg = sdio_readb(card->func, card->reg->host_intstatus, &ret);
887 if (ret) {
888 BT_ERR("sdio_readb: read int status failed: %d", ret);
889 return ret;
892 if (*ireg) {
894 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
895 * Clear the interrupt status register and re-enable the
896 * interrupt.
898 BT_DBG("int_status = 0x%x", *ireg);
900 sdio_writeb(card->func, ~(*ireg) & (DN_LD_HOST_INT_STATUS |
901 UP_LD_HOST_INT_STATUS),
902 card->reg->host_intstatus, &ret);
903 if (ret) {
904 BT_ERR("sdio_writeb: clear int status failed: %d", ret);
905 return ret;
909 return 0;
912 static void btmrvl_sdio_interrupt(struct sdio_func *func)
914 struct btmrvl_private *priv;
915 struct btmrvl_sdio_card *card;
916 ulong flags;
917 u8 ireg = 0;
918 int ret;
920 card = sdio_get_drvdata(func);
921 if (!card || !card->priv) {
922 BT_ERR("sbi_interrupt(%p) card or priv is NULL, card=%p",
923 func, card);
924 return;
927 priv = card->priv;
929 if (priv->surprise_removed)
930 return;
932 if (card->reg->int_read_to_clear)
933 ret = btmrvl_sdio_read_to_clear(card, &ireg);
934 else
935 ret = btmrvl_sdio_write_to_clear(card, &ireg);
937 if (ret)
938 return;
940 spin_lock_irqsave(&priv->driver_lock, flags);
941 sdio_ireg |= ireg;
942 spin_unlock_irqrestore(&priv->driver_lock, flags);
944 btmrvl_interrupt(priv);
947 static int btmrvl_sdio_register_dev(struct btmrvl_sdio_card *card)
949 struct sdio_func *func;
950 u8 reg;
951 int ret;
953 if (!card || !card->func) {
954 BT_ERR("Error: card or function is NULL!");
955 ret = -EINVAL;
956 goto failed;
959 func = card->func;
961 sdio_claim_host(func);
963 ret = sdio_enable_func(func);
964 if (ret) {
965 BT_ERR("sdio_enable_func() failed: ret=%d", ret);
966 ret = -EIO;
967 goto release_host;
970 ret = sdio_claim_irq(func, btmrvl_sdio_interrupt);
971 if (ret) {
972 BT_ERR("sdio_claim_irq failed: ret=%d", ret);
973 ret = -EIO;
974 goto disable_func;
977 ret = sdio_set_block_size(card->func, SDIO_BLOCK_SIZE);
978 if (ret) {
979 BT_ERR("cannot set SDIO block size");
980 ret = -EIO;
981 goto release_irq;
984 reg = sdio_readb(func, card->reg->io_port_0, &ret);
985 if (ret < 0) {
986 ret = -EIO;
987 goto release_irq;
990 card->ioport = reg;
992 reg = sdio_readb(func, card->reg->io_port_1, &ret);
993 if (ret < 0) {
994 ret = -EIO;
995 goto release_irq;
998 card->ioport |= (reg << 8);
1000 reg = sdio_readb(func, card->reg->io_port_2, &ret);
1001 if (ret < 0) {
1002 ret = -EIO;
1003 goto release_irq;
1006 card->ioport |= (reg << 16);
1008 BT_DBG("SDIO FUNC%d IO port: 0x%x", func->num, card->ioport);
1010 if (card->reg->int_read_to_clear) {
1011 reg = sdio_readb(func, card->reg->host_int_rsr, &ret);
1012 if (ret < 0) {
1013 ret = -EIO;
1014 goto release_irq;
1016 sdio_writeb(func, reg | 0x3f, card->reg->host_int_rsr, &ret);
1017 if (ret < 0) {
1018 ret = -EIO;
1019 goto release_irq;
1022 reg = sdio_readb(func, card->reg->card_misc_cfg, &ret);
1023 if (ret < 0) {
1024 ret = -EIO;
1025 goto release_irq;
1027 sdio_writeb(func, reg | 0x10, card->reg->card_misc_cfg, &ret);
1028 if (ret < 0) {
1029 ret = -EIO;
1030 goto release_irq;
1034 sdio_set_drvdata(func, card);
1036 sdio_release_host(func);
1038 return 0;
1040 release_irq:
1041 sdio_release_irq(func);
1043 disable_func:
1044 sdio_disable_func(func);
1046 release_host:
1047 sdio_release_host(func);
1049 failed:
1050 return ret;
1053 static int btmrvl_sdio_unregister_dev(struct btmrvl_sdio_card *card)
1055 if (card && card->func) {
1056 sdio_claim_host(card->func);
1057 sdio_release_irq(card->func);
1058 sdio_disable_func(card->func);
1059 sdio_release_host(card->func);
1060 sdio_set_drvdata(card->func, NULL);
1063 return 0;
1066 static int btmrvl_sdio_enable_host_int(struct btmrvl_sdio_card *card)
1068 int ret;
1070 if (!card || !card->func)
1071 return -EINVAL;
1073 sdio_claim_host(card->func);
1075 ret = btmrvl_sdio_enable_host_int_mask(card, HIM_ENABLE);
1077 btmrvl_sdio_get_rx_unit(card);
1079 sdio_release_host(card->func);
1081 return ret;
1084 static int btmrvl_sdio_disable_host_int(struct btmrvl_sdio_card *card)
1086 int ret;
1088 if (!card || !card->func)
1089 return -EINVAL;
1091 sdio_claim_host(card->func);
1093 ret = btmrvl_sdio_disable_host_int_mask(card, HIM_DISABLE);
1095 sdio_release_host(card->func);
1097 return ret;
1100 static int btmrvl_sdio_host_to_card(struct btmrvl_private *priv,
1101 u8 *payload, u16 nb)
1103 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
1104 int ret = 0;
1105 int blksz;
1106 int i = 0;
1107 u8 *buf = NULL;
1108 void *tmpbuf = NULL;
1109 int tmpbufsz;
1111 if (!card || !card->func) {
1112 BT_ERR("card or function is NULL!");
1113 return -EINVAL;
1116 blksz = DIV_ROUND_UP(nb, SDIO_BLOCK_SIZE) * SDIO_BLOCK_SIZE;
1118 buf = payload;
1119 if ((unsigned long) payload & (BTSDIO_DMA_ALIGN - 1) ||
1120 nb < blksz) {
1121 tmpbufsz = ALIGN_SZ(blksz, BTSDIO_DMA_ALIGN) +
1122 BTSDIO_DMA_ALIGN;
1123 tmpbuf = kzalloc(tmpbufsz, GFP_KERNEL);
1124 if (!tmpbuf)
1125 return -ENOMEM;
1126 buf = (u8 *) ALIGN_ADDR(tmpbuf, BTSDIO_DMA_ALIGN);
1127 memcpy(buf, payload, nb);
1130 sdio_claim_host(card->func);
1132 do {
1133 /* Transfer data to card */
1134 ret = sdio_writesb(card->func, card->ioport, buf,
1135 blksz);
1136 if (ret < 0) {
1137 i++;
1138 BT_ERR("i=%d writesb failed: %d", i, ret);
1139 BT_ERR("hex: %*ph", nb, payload);
1140 ret = -EIO;
1141 if (i > MAX_WRITE_IOMEM_RETRY)
1142 goto exit;
1144 } while (ret);
1146 priv->btmrvl_dev.tx_dnld_rdy = false;
1148 exit:
1149 sdio_release_host(card->func);
1150 kfree(tmpbuf);
1152 return ret;
1155 static int btmrvl_sdio_download_fw(struct btmrvl_sdio_card *card)
1157 int ret;
1158 u8 fws0;
1159 int pollnum = MAX_POLL_TRIES;
1161 if (!card || !card->func) {
1162 BT_ERR("card or function is NULL!");
1163 return -EINVAL;
1166 if (!btmrvl_sdio_verify_fw_download(card, 1)) {
1167 BT_DBG("Firmware already downloaded!");
1168 return 0;
1171 sdio_claim_host(card->func);
1173 /* Check if other function driver is downloading the firmware */
1174 fws0 = sdio_readb(card->func, card->reg->card_fw_status0, &ret);
1175 if (ret) {
1176 BT_ERR("Failed to read FW downloading status!");
1177 ret = -EIO;
1178 goto done;
1180 if (fws0) {
1181 BT_DBG("BT not the winner (%#x). Skip FW downloading", fws0);
1183 /* Give other function more time to download the firmware */
1184 pollnum *= 10;
1185 } else {
1186 if (card->helper) {
1187 ret = btmrvl_sdio_download_helper(card);
1188 if (ret) {
1189 BT_ERR("Failed to download helper!");
1190 ret = -EIO;
1191 goto done;
1195 if (btmrvl_sdio_download_fw_w_helper(card)) {
1196 BT_ERR("Failed to download firmware!");
1197 ret = -EIO;
1198 goto done;
1203 * winner or not, with this test the FW synchronizes when the
1204 * module can continue its initialization
1206 if (btmrvl_sdio_verify_fw_download(card, pollnum)) {
1207 BT_ERR("FW failed to be active in time!");
1208 ret = -ETIMEDOUT;
1209 goto done;
1212 sdio_release_host(card->func);
1214 return 0;
1216 done:
1217 sdio_release_host(card->func);
1218 return ret;
1221 static int btmrvl_sdio_wakeup_fw(struct btmrvl_private *priv)
1223 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
1224 int ret = 0;
1226 if (!card || !card->func) {
1227 BT_ERR("card or function is NULL!");
1228 return -EINVAL;
1231 sdio_claim_host(card->func);
1233 sdio_writeb(card->func, HOST_POWER_UP, card->reg->cfg, &ret);
1235 sdio_release_host(card->func);
1237 BT_DBG("wake up firmware");
1239 return ret;
1242 static void btmrvl_sdio_dump_regs(struct btmrvl_private *priv)
1244 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
1245 int ret = 0;
1246 unsigned int reg, reg_start, reg_end;
1247 char buf[256], *ptr;
1248 u8 loop, func, data;
1249 int MAX_LOOP = 2;
1251 btmrvl_sdio_wakeup_fw(priv);
1252 sdio_claim_host(card->func);
1254 for (loop = 0; loop < MAX_LOOP; loop++) {
1255 memset(buf, 0, sizeof(buf));
1256 ptr = buf;
1258 if (loop == 0) {
1259 /* Read the registers of SDIO function0 */
1260 func = loop;
1261 reg_start = 0;
1262 reg_end = 9;
1263 } else {
1264 func = 2;
1265 reg_start = 0;
1266 reg_end = 0x09;
1269 ptr += sprintf(ptr, "SDIO Func%d (%#x-%#x): ",
1270 func, reg_start, reg_end);
1271 for (reg = reg_start; reg <= reg_end; reg++) {
1272 if (func == 0)
1273 data = sdio_f0_readb(card->func, reg, &ret);
1274 else
1275 data = sdio_readb(card->func, reg, &ret);
1277 if (!ret) {
1278 ptr += sprintf(ptr, "%02x ", data);
1279 } else {
1280 ptr += sprintf(ptr, "ERR");
1281 break;
1285 BT_INFO("%s", buf);
1288 sdio_release_host(card->func);
1291 /* This function read/write firmware */
1292 static enum
1293 rdwr_status btmrvl_sdio_rdwr_firmware(struct btmrvl_private *priv,
1294 u8 doneflag)
1296 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
1297 int ret, tries;
1298 u8 ctrl_data = 0;
1300 sdio_writeb(card->func, FW_DUMP_HOST_READY, card->reg->fw_dump_ctrl,
1301 &ret);
1303 if (ret) {
1304 BT_ERR("SDIO write err");
1305 return RDWR_STATUS_FAILURE;
1308 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
1309 ctrl_data = sdio_readb(card->func, card->reg->fw_dump_ctrl,
1310 &ret);
1312 if (ret) {
1313 BT_ERR("SDIO read err");
1314 return RDWR_STATUS_FAILURE;
1317 if (ctrl_data == FW_DUMP_DONE)
1318 break;
1319 if (doneflag && ctrl_data == doneflag)
1320 return RDWR_STATUS_DONE;
1321 if (ctrl_data != FW_DUMP_HOST_READY) {
1322 BT_INFO("The ctrl reg was changed, re-try again!");
1323 sdio_writeb(card->func, FW_DUMP_HOST_READY,
1324 card->reg->fw_dump_ctrl, &ret);
1325 if (ret) {
1326 BT_ERR("SDIO write err");
1327 return RDWR_STATUS_FAILURE;
1330 usleep_range(100, 200);
1333 if (ctrl_data == FW_DUMP_HOST_READY) {
1334 BT_ERR("Fail to pull ctrl_data");
1335 return RDWR_STATUS_FAILURE;
1338 return RDWR_STATUS_SUCCESS;
1341 /* This function dump sdio register and memory data */
1342 static void btmrvl_sdio_coredump(struct device *dev)
1344 struct sdio_func *func = dev_to_sdio_func(dev);
1345 struct btmrvl_sdio_card *card;
1346 struct btmrvl_private *priv;
1347 int ret = 0;
1348 unsigned int reg, reg_start, reg_end;
1349 enum rdwr_status stat;
1350 u8 *dbg_ptr, *end_ptr, *fw_dump_data, *fw_dump_ptr;
1351 u8 dump_num = 0, idx, i, read_reg, doneflag = 0;
1352 u32 memory_size, fw_dump_len = 0;
1354 card = sdio_get_drvdata(func);
1355 priv = card->priv;
1357 /* dump sdio register first */
1358 btmrvl_sdio_dump_regs(priv);
1360 if (!card->supports_fw_dump) {
1361 BT_ERR("Firmware dump not supported for this card!");
1362 return;
1365 for (idx = 0; idx < ARRAY_SIZE(mem_type_mapping_tbl); idx++) {
1366 struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
1368 if (entry->mem_ptr) {
1369 vfree(entry->mem_ptr);
1370 entry->mem_ptr = NULL;
1372 entry->mem_size = 0;
1375 btmrvl_sdio_wakeup_fw(priv);
1376 sdio_claim_host(card->func);
1378 BT_INFO("== btmrvl firmware dump start ==");
1380 stat = btmrvl_sdio_rdwr_firmware(priv, doneflag);
1381 if (stat == RDWR_STATUS_FAILURE)
1382 goto done;
1384 reg = card->reg->fw_dump_start;
1385 /* Read the number of the memories which will dump */
1386 dump_num = sdio_readb(card->func, reg, &ret);
1388 if (ret) {
1389 BT_ERR("SDIO read memory length err");
1390 goto done;
1393 /* Read the length of every memory which will dump */
1394 for (idx = 0; idx < dump_num; idx++) {
1395 struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
1397 stat = btmrvl_sdio_rdwr_firmware(priv, doneflag);
1398 if (stat == RDWR_STATUS_FAILURE)
1399 goto done;
1401 memory_size = 0;
1402 reg = card->reg->fw_dump_start;
1403 for (i = 0; i < 4; i++) {
1404 read_reg = sdio_readb(card->func, reg, &ret);
1405 if (ret) {
1406 BT_ERR("SDIO read err");
1407 goto done;
1409 memory_size |= (read_reg << i*8);
1410 reg++;
1413 if (memory_size == 0) {
1414 BT_INFO("Firmware dump finished!");
1415 sdio_writeb(card->func, FW_DUMP_READ_DONE,
1416 card->reg->fw_dump_ctrl, &ret);
1417 if (ret) {
1418 BT_ERR("SDIO Write MEMDUMP_FINISH ERR");
1419 goto done;
1421 break;
1424 BT_INFO("%s_SIZE=0x%x", entry->mem_name, memory_size);
1425 entry->mem_ptr = vzalloc(memory_size + 1);
1426 entry->mem_size = memory_size;
1427 if (!entry->mem_ptr) {
1428 BT_ERR("Vzalloc %s failed", entry->mem_name);
1429 goto done;
1432 fw_dump_len += (strlen("========Start dump ") +
1433 strlen(entry->mem_name) +
1434 strlen("========\n") +
1435 (memory_size + 1) +
1436 strlen("\n========End dump========\n"));
1438 dbg_ptr = entry->mem_ptr;
1439 end_ptr = dbg_ptr + memory_size;
1441 doneflag = entry->done_flag;
1442 BT_INFO("Start %s output, please wait...",
1443 entry->mem_name);
1445 do {
1446 stat = btmrvl_sdio_rdwr_firmware(priv, doneflag);
1447 if (stat == RDWR_STATUS_FAILURE)
1448 goto done;
1450 reg_start = card->reg->fw_dump_start;
1451 reg_end = card->reg->fw_dump_end;
1452 for (reg = reg_start; reg <= reg_end; reg++) {
1453 *dbg_ptr = sdio_readb(card->func, reg, &ret);
1454 if (ret) {
1455 BT_ERR("SDIO read err");
1456 goto done;
1458 if (dbg_ptr < end_ptr)
1459 dbg_ptr++;
1460 else
1461 BT_ERR("Allocated buffer not enough");
1464 if (stat != RDWR_STATUS_DONE) {
1465 continue;
1466 } else {
1467 BT_INFO("%s done: size=0x%tx",
1468 entry->mem_name,
1469 dbg_ptr - entry->mem_ptr);
1470 break;
1472 } while (1);
1475 BT_INFO("== btmrvl firmware dump end ==");
1477 done:
1478 sdio_release_host(card->func);
1480 if (fw_dump_len == 0)
1481 return;
1483 fw_dump_data = vzalloc(fw_dump_len+1);
1484 if (!fw_dump_data) {
1485 BT_ERR("Vzalloc fw_dump_data fail!");
1486 return;
1488 fw_dump_ptr = fw_dump_data;
1490 /* Dump all the memory data into single file, a userspace script will
1491 * be used to split all the memory data to multiple files
1493 BT_INFO("== btmrvl firmware dump to /sys/class/devcoredump start");
1494 for (idx = 0; idx < dump_num; idx++) {
1495 struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
1497 if (entry->mem_ptr) {
1498 strcpy(fw_dump_ptr, "========Start dump ");
1499 fw_dump_ptr += strlen("========Start dump ");
1501 strcpy(fw_dump_ptr, entry->mem_name);
1502 fw_dump_ptr += strlen(entry->mem_name);
1504 strcpy(fw_dump_ptr, "========\n");
1505 fw_dump_ptr += strlen("========\n");
1507 memcpy(fw_dump_ptr, entry->mem_ptr, entry->mem_size);
1508 fw_dump_ptr += entry->mem_size;
1510 strcpy(fw_dump_ptr, "\n========End dump========\n");
1511 fw_dump_ptr += strlen("\n========End dump========\n");
1513 vfree(mem_type_mapping_tbl[idx].mem_ptr);
1514 mem_type_mapping_tbl[idx].mem_ptr = NULL;
1518 /* fw_dump_data will be free in device coredump release function
1519 * after 5 min
1521 dev_coredumpv(&card->func->dev, fw_dump_data, fw_dump_len, GFP_KERNEL);
1522 BT_INFO("== btmrvl firmware dump to /sys/class/devcoredump end");
1525 static int btmrvl_sdio_probe(struct sdio_func *func,
1526 const struct sdio_device_id *id)
1528 int ret = 0;
1529 struct btmrvl_private *priv = NULL;
1530 struct btmrvl_sdio_card *card = NULL;
1532 BT_INFO("vendor=0x%x, device=0x%x, class=%d, fn=%d",
1533 id->vendor, id->device, id->class, func->num);
1535 card = devm_kzalloc(&func->dev, sizeof(*card), GFP_KERNEL);
1536 if (!card)
1537 return -ENOMEM;
1539 card->func = func;
1541 if (id->driver_data) {
1542 struct btmrvl_sdio_device *data = (void *) id->driver_data;
1543 card->helper = data->helper;
1544 card->firmware = data->firmware;
1545 card->reg = data->reg;
1546 card->sd_blksz_fw_dl = data->sd_blksz_fw_dl;
1547 card->support_pscan_win_report = data->support_pscan_win_report;
1548 card->supports_fw_dump = data->supports_fw_dump;
1551 if (btmrvl_sdio_register_dev(card) < 0) {
1552 BT_ERR("Failed to register BT device!");
1553 return -ENODEV;
1556 /* Disable the interrupts on the card */
1557 btmrvl_sdio_disable_host_int(card);
1559 if (btmrvl_sdio_download_fw(card)) {
1560 BT_ERR("Downloading firmware failed!");
1561 ret = -ENODEV;
1562 goto unreg_dev;
1565 btmrvl_sdio_enable_host_int(card);
1567 /* Device tree node parsing and platform specific configuration*/
1568 btmrvl_sdio_probe_of(&func->dev, card);
1570 priv = btmrvl_add_card(card);
1571 if (!priv) {
1572 BT_ERR("Initializing card failed!");
1573 ret = -ENODEV;
1574 goto disable_host_int;
1577 card->priv = priv;
1579 /* Initialize the interface specific function pointers */
1580 priv->hw_host_to_card = btmrvl_sdio_host_to_card;
1581 priv->hw_wakeup_firmware = btmrvl_sdio_wakeup_fw;
1582 priv->hw_process_int_status = btmrvl_sdio_process_int_status;
1584 if (btmrvl_register_hdev(priv)) {
1585 BT_ERR("Register hdev failed!");
1586 ret = -ENODEV;
1587 goto disable_host_int;
1590 return 0;
1592 disable_host_int:
1593 btmrvl_sdio_disable_host_int(card);
1594 unreg_dev:
1595 btmrvl_sdio_unregister_dev(card);
1596 return ret;
1599 static void btmrvl_sdio_remove(struct sdio_func *func)
1601 struct btmrvl_sdio_card *card;
1603 if (func) {
1604 card = sdio_get_drvdata(func);
1605 if (card) {
1606 /* Send SHUTDOWN command & disable interrupt
1607 * if user removes the module.
1609 if (user_rmmod) {
1610 btmrvl_send_module_cfg_cmd(card->priv,
1611 MODULE_SHUTDOWN_REQ);
1612 btmrvl_sdio_disable_host_int(card);
1615 BT_DBG("unregister dev");
1616 card->priv->surprise_removed = true;
1617 btmrvl_sdio_unregister_dev(card);
1618 btmrvl_remove_card(card->priv);
1623 static int btmrvl_sdio_suspend(struct device *dev)
1625 struct sdio_func *func = dev_to_sdio_func(dev);
1626 struct btmrvl_sdio_card *card;
1627 struct btmrvl_private *priv;
1628 mmc_pm_flag_t pm_flags;
1629 struct hci_dev *hcidev;
1631 if (func) {
1632 pm_flags = sdio_get_host_pm_caps(func);
1633 BT_DBG("%s: suspend: PM flags = 0x%x", sdio_func_id(func),
1634 pm_flags);
1635 if (!(pm_flags & MMC_PM_KEEP_POWER)) {
1636 BT_ERR("%s: cannot remain alive while suspended",
1637 sdio_func_id(func));
1638 return -ENOSYS;
1640 card = sdio_get_drvdata(func);
1641 if (!card || !card->priv) {
1642 BT_ERR("card or priv structure is not valid");
1643 return 0;
1645 } else {
1646 BT_ERR("sdio_func is not specified");
1647 return 0;
1650 /* Enable platform specific wakeup interrupt */
1651 if (card->plt_wake_cfg && card->plt_wake_cfg->irq_bt >= 0 &&
1652 device_may_wakeup(dev)) {
1653 card->plt_wake_cfg->wake_by_bt = false;
1654 enable_irq(card->plt_wake_cfg->irq_bt);
1655 enable_irq_wake(card->plt_wake_cfg->irq_bt);
1658 priv = card->priv;
1659 priv->adapter->is_suspending = true;
1660 hcidev = priv->btmrvl_dev.hcidev;
1661 BT_DBG("%s: SDIO suspend", hcidev->name);
1662 hci_suspend_dev(hcidev);
1664 if (priv->adapter->hs_state != HS_ACTIVATED) {
1665 if (btmrvl_enable_hs(priv)) {
1666 BT_ERR("HS not activated, suspend failed!");
1667 /* Disable platform specific wakeup interrupt */
1668 if (card->plt_wake_cfg &&
1669 card->plt_wake_cfg->irq_bt >= 0 &&
1670 device_may_wakeup(dev)) {
1671 disable_irq_wake(card->plt_wake_cfg->irq_bt);
1672 disable_irq(card->plt_wake_cfg->irq_bt);
1675 priv->adapter->is_suspending = false;
1676 return -EBUSY;
1680 priv->adapter->is_suspending = false;
1681 priv->adapter->is_suspended = true;
1683 /* We will keep the power when hs enabled successfully */
1684 if (priv->adapter->hs_state == HS_ACTIVATED) {
1685 BT_DBG("suspend with MMC_PM_KEEP_POWER");
1686 return sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
1689 BT_DBG("suspend without MMC_PM_KEEP_POWER");
1690 return 0;
1693 static int btmrvl_sdio_resume(struct device *dev)
1695 struct sdio_func *func = dev_to_sdio_func(dev);
1696 struct btmrvl_sdio_card *card;
1697 struct btmrvl_private *priv;
1698 mmc_pm_flag_t pm_flags;
1699 struct hci_dev *hcidev;
1701 if (func) {
1702 pm_flags = sdio_get_host_pm_caps(func);
1703 BT_DBG("%s: resume: PM flags = 0x%x", sdio_func_id(func),
1704 pm_flags);
1705 card = sdio_get_drvdata(func);
1706 if (!card || !card->priv) {
1707 BT_ERR("card or priv structure is not valid");
1708 return 0;
1710 } else {
1711 BT_ERR("sdio_func is not specified");
1712 return 0;
1714 priv = card->priv;
1716 if (!priv->adapter->is_suspended) {
1717 BT_DBG("device already resumed");
1718 return 0;
1721 priv->hw_wakeup_firmware(priv);
1722 priv->adapter->hs_state = HS_DEACTIVATED;
1723 hcidev = priv->btmrvl_dev.hcidev;
1724 BT_DBG("%s: HS DEACTIVATED in resume!", hcidev->name);
1725 priv->adapter->is_suspended = false;
1726 BT_DBG("%s: SDIO resume", hcidev->name);
1727 hci_resume_dev(hcidev);
1729 /* Disable platform specific wakeup interrupt */
1730 if (card->plt_wake_cfg && card->plt_wake_cfg->irq_bt >= 0 &&
1731 device_may_wakeup(dev)) {
1732 disable_irq_wake(card->plt_wake_cfg->irq_bt);
1733 disable_irq(card->plt_wake_cfg->irq_bt);
1734 if (card->plt_wake_cfg->wake_by_bt)
1735 /* Undo our disable, since interrupt handler already
1736 * did this.
1738 enable_irq(card->plt_wake_cfg->irq_bt);
1741 return 0;
1744 static const struct dev_pm_ops btmrvl_sdio_pm_ops = {
1745 .suspend = btmrvl_sdio_suspend,
1746 .resume = btmrvl_sdio_resume,
1749 static struct sdio_driver bt_mrvl_sdio = {
1750 .name = "btmrvl_sdio",
1751 .id_table = btmrvl_sdio_ids,
1752 .probe = btmrvl_sdio_probe,
1753 .remove = btmrvl_sdio_remove,
1754 .drv = {
1755 .owner = THIS_MODULE,
1756 .coredump = btmrvl_sdio_coredump,
1757 .pm = &btmrvl_sdio_pm_ops,
1761 static int __init btmrvl_sdio_init_module(void)
1763 if (sdio_register_driver(&bt_mrvl_sdio) != 0) {
1764 BT_ERR("SDIO Driver Registration Failed");
1765 return -ENODEV;
1768 /* Clear the flag in case user removes the card. */
1769 user_rmmod = 0;
1771 return 0;
1774 static void __exit btmrvl_sdio_exit_module(void)
1776 /* Set the flag as user is removing this module. */
1777 user_rmmod = 1;
1779 sdio_unregister_driver(&bt_mrvl_sdio);
1782 module_init(btmrvl_sdio_init_module);
1783 module_exit(btmrvl_sdio_exit_module);
1785 MODULE_AUTHOR("Marvell International Ltd.");
1786 MODULE_DESCRIPTION("Marvell BT-over-SDIO driver ver " VERSION);
1787 MODULE_VERSION(VERSION);
1788 MODULE_LICENSE("GPL v2");
1789 MODULE_FIRMWARE("mrvl/sd8688_helper.bin");
1790 MODULE_FIRMWARE("mrvl/sd8688.bin");
1791 MODULE_FIRMWARE("mrvl/sd8787_uapsta.bin");
1792 MODULE_FIRMWARE("mrvl/sd8797_uapsta.bin");
1793 MODULE_FIRMWARE("mrvl/sd8887_uapsta.bin");
1794 MODULE_FIRMWARE("mrvl/sd8897_uapsta.bin");
1795 MODULE_FIRMWARE("mrvl/sdsd8977_combo_v2.bin");
1796 MODULE_FIRMWARE("mrvl/sd8987_uapsta.bin");
1797 MODULE_FIRMWARE("mrvl/sdsd8997_combo_v4.bin");