1 /** 2 * Marvell BT-over-SDIO driver: SDIO interface related functions. 3 * 4 * Copyright (C) 2009, Marvell International Ltd. 5 * 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. 13 * 14 * 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 **/ 20 21 #include <linux/firmware.h> 22 #include <linux/slab.h> 23 #include <linux/suspend.h> 24 25 #include <linux/mmc/sdio_ids.h> 26 #include <linux/mmc/sdio_func.h> 27 #include <linux/module.h> 28 #include <linux/devcoredump.h> 29 30 #include <net/bluetooth/bluetooth.h> 31 #include <net/bluetooth/hci_core.h> 32 33 #include "btmrvl_drv.h" 34 #include "btmrvl_sdio.h" 35 36 #define VERSION "1.0" 37 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}, 54 }; 55 56 static const struct of_device_id btmrvl_sdio_of_match_table[] = { 57 { .compatible = "marvell,sd8897-bt" }, 58 { .compatible = "marvell,sd8997-bt" }, 59 { } 60 }; 61 62 static irqreturn_t btmrvl_wake_irq_bt(int irq, void *priv) 63 { 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; 67 68 dev_info(dev, "wake by bt\n"); 69 cfg->wake_by_bt = true; 70 disable_irq_nosync(irq); 71 72 pm_wakeup_event(dev, 0); 73 pm_system_wakeup(); 74 75 return IRQ_HANDLED; 76 } 77 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. 82 */ 83 static int btmrvl_sdio_probe_of(struct device *dev, 84 struct btmrvl_sdio_card *card) 85 { 86 struct btmrvl_plt_wake_cfg *cfg; 87 int ret; 88 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; 93 } 94 95 card->plt_of_node = dev->of_node; 96 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); 113 } 114 disable_irq(cfg->irq_bt); 115 } 116 } 117 118 return 0; 119 } 120 121 /* The btmrvl_sdio_remove() callback function is called 122 * when user removes this module from kernel space or ejects 123 * the card from the slot. The driver handles these 2 cases 124 * differently. 125 * If the user is removing the module, a MODULE_SHUTDOWN_REQ 126 * command is sent to firmware and interrupt will be disabled. 127 * If the card is removed, there is no need to send command 128 * or disable interrupt. 129 * 130 * The variable 'user_rmmod' is used to distinguish these two 131 * scenarios. This flag is initialized as FALSE in case the card 132 * is removed, and will be set to TRUE for module removal when 133 * module_exit function is called. 134 */ 135 static u8 user_rmmod; 136 static u8 sdio_ireg; 137 138 static const struct btmrvl_sdio_card_reg btmrvl_reg_8688 = { 139 .cfg = 0x03, 140 .host_int_mask = 0x04, 141 .host_intstatus = 0x05, 142 .card_status = 0x20, 143 .sq_read_base_addr_a0 = 0x10, 144 .sq_read_base_addr_a1 = 0x11, 145 .card_fw_status0 = 0x40, 146 .card_fw_status1 = 0x41, 147 .card_rx_len = 0x42, 148 .card_rx_unit = 0x43, 149 .io_port_0 = 0x00, 150 .io_port_1 = 0x01, 151 .io_port_2 = 0x02, 152 .int_read_to_clear = false, 153 }; 154 static const struct btmrvl_sdio_card_reg btmrvl_reg_87xx = { 155 .cfg = 0x00, 156 .host_int_mask = 0x02, 157 .host_intstatus = 0x03, 158 .card_status = 0x30, 159 .sq_read_base_addr_a0 = 0x40, 160 .sq_read_base_addr_a1 = 0x41, 161 .card_revision = 0x5c, 162 .card_fw_status0 = 0x60, 163 .card_fw_status1 = 0x61, 164 .card_rx_len = 0x62, 165 .card_rx_unit = 0x63, 166 .io_port_0 = 0x78, 167 .io_port_1 = 0x79, 168 .io_port_2 = 0x7a, 169 .int_read_to_clear = false, 170 }; 171 172 static const struct btmrvl_sdio_card_reg btmrvl_reg_8887 = { 173 .cfg = 0x00, 174 .host_int_mask = 0x08, 175 .host_intstatus = 0x0C, 176 .card_status = 0x5C, 177 .sq_read_base_addr_a0 = 0x6C, 178 .sq_read_base_addr_a1 = 0x6D, 179 .card_revision = 0xC8, 180 .card_fw_status0 = 0x88, 181 .card_fw_status1 = 0x89, 182 .card_rx_len = 0x8A, 183 .card_rx_unit = 0x8B, 184 .io_port_0 = 0xE4, 185 .io_port_1 = 0xE5, 186 .io_port_2 = 0xE6, 187 .int_read_to_clear = true, 188 .host_int_rsr = 0x04, 189 .card_misc_cfg = 0xD8, 190 }; 191 192 static const struct btmrvl_sdio_card_reg btmrvl_reg_8897 = { 193 .cfg = 0x00, 194 .host_int_mask = 0x02, 195 .host_intstatus = 0x03, 196 .card_status = 0x50, 197 .sq_read_base_addr_a0 = 0x60, 198 .sq_read_base_addr_a1 = 0x61, 199 .card_revision = 0xbc, 200 .card_fw_status0 = 0xc0, 201 .card_fw_status1 = 0xc1, 202 .card_rx_len = 0xc2, 203 .card_rx_unit = 0xc3, 204 .io_port_0 = 0xd8, 205 .io_port_1 = 0xd9, 206 .io_port_2 = 0xda, 207 .int_read_to_clear = true, 208 .host_int_rsr = 0x01, 209 .card_misc_cfg = 0xcc, 210 .fw_dump_ctrl = 0xe2, 211 .fw_dump_start = 0xe3, 212 .fw_dump_end = 0xea, 213 }; 214 215 static const struct btmrvl_sdio_card_reg btmrvl_reg_8977 = { 216 .cfg = 0x00, 217 .host_int_mask = 0x08, 218 .host_intstatus = 0x0c, 219 .card_status = 0x5c, 220 .sq_read_base_addr_a0 = 0xf8, 221 .sq_read_base_addr_a1 = 0xf9, 222 .card_revision = 0xc8, 223 .card_fw_status0 = 0xe8, 224 .card_fw_status1 = 0xe9, 225 .card_rx_len = 0xea, 226 .card_rx_unit = 0xeb, 227 .io_port_0 = 0xe4, 228 .io_port_1 = 0xe5, 229 .io_port_2 = 0xe6, 230 .int_read_to_clear = true, 231 .host_int_rsr = 0x04, 232 .card_misc_cfg = 0xD8, 233 .fw_dump_ctrl = 0xf0, 234 .fw_dump_start = 0xf1, 235 .fw_dump_end = 0xf8, 236 }; 237 238 static const struct btmrvl_sdio_card_reg btmrvl_reg_8997 = { 239 .cfg = 0x00, 240 .host_int_mask = 0x08, 241 .host_intstatus = 0x0c, 242 .card_status = 0x5c, 243 .sq_read_base_addr_a0 = 0xf8, 244 .sq_read_base_addr_a1 = 0xf9, 245 .card_revision = 0xc8, 246 .card_fw_status0 = 0xe8, 247 .card_fw_status1 = 0xe9, 248 .card_rx_len = 0xea, 249 .card_rx_unit = 0xeb, 250 .io_port_0 = 0xe4, 251 .io_port_1 = 0xe5, 252 .io_port_2 = 0xe6, 253 .int_read_to_clear = true, 254 .host_int_rsr = 0x04, 255 .card_misc_cfg = 0xD8, 256 .fw_dump_ctrl = 0xf0, 257 .fw_dump_start = 0xf1, 258 .fw_dump_end = 0xf8, 259 }; 260 261 static const struct btmrvl_sdio_device btmrvl_sdio_sd8688 = { 262 .helper = "mrvl/sd8688_helper.bin", 263 .firmware = "mrvl/sd8688.bin", 264 .reg = &btmrvl_reg_8688, 265 .support_pscan_win_report = false, 266 .sd_blksz_fw_dl = 64, 267 .supports_fw_dump = false, 268 }; 269 270 static const struct btmrvl_sdio_device btmrvl_sdio_sd8787 = { 271 .helper = NULL, 272 .firmware = "mrvl/sd8787_uapsta.bin", 273 .reg = &btmrvl_reg_87xx, 274 .support_pscan_win_report = false, 275 .sd_blksz_fw_dl = 256, 276 .supports_fw_dump = false, 277 }; 278 279 static const struct btmrvl_sdio_device btmrvl_sdio_sd8797 = { 280 .helper = NULL, 281 .firmware = "mrvl/sd8797_uapsta.bin", 282 .reg = &btmrvl_reg_87xx, 283 .support_pscan_win_report = false, 284 .sd_blksz_fw_dl = 256, 285 .supports_fw_dump = false, 286 }; 287 288 static const struct btmrvl_sdio_device btmrvl_sdio_sd8887 = { 289 .helper = NULL, 290 .firmware = "mrvl/sd8887_uapsta.bin", 291 .reg = &btmrvl_reg_8887, 292 .support_pscan_win_report = true, 293 .sd_blksz_fw_dl = 256, 294 .supports_fw_dump = false, 295 }; 296 297 static const struct btmrvl_sdio_device btmrvl_sdio_sd8897 = { 298 .helper = NULL, 299 .firmware = "mrvl/sd8897_uapsta.bin", 300 .reg = &btmrvl_reg_8897, 301 .support_pscan_win_report = true, 302 .sd_blksz_fw_dl = 256, 303 .supports_fw_dump = true, 304 }; 305 306 static const struct btmrvl_sdio_device btmrvl_sdio_sd8977 = { 307 .helper = NULL, 308 .firmware = "mrvl/sd8977_uapsta.bin", 309 .reg = &btmrvl_reg_8977, 310 .support_pscan_win_report = true, 311 .sd_blksz_fw_dl = 256, 312 .supports_fw_dump = true, 313 }; 314 315 static const struct btmrvl_sdio_device btmrvl_sdio_sd8997 = { 316 .helper = NULL, 317 .firmware = "mrvl/sd8997_uapsta.bin", 318 .reg = &btmrvl_reg_8997, 319 .support_pscan_win_report = true, 320 .sd_blksz_fw_dl = 256, 321 .supports_fw_dump = true, 322 }; 323 324 static const struct sdio_device_id btmrvl_sdio_ids[] = { 325 /* Marvell SD8688 Bluetooth device */ 326 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x9105), 327 .driver_data = (unsigned long)&btmrvl_sdio_sd8688 }, 328 /* Marvell SD8787 Bluetooth device */ 329 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x911A), 330 .driver_data = (unsigned long)&btmrvl_sdio_sd8787 }, 331 /* Marvell SD8787 Bluetooth AMP device */ 332 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x911B), 333 .driver_data = (unsigned long)&btmrvl_sdio_sd8787 }, 334 /* Marvell SD8797 Bluetooth device */ 335 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x912A), 336 .driver_data = (unsigned long)&btmrvl_sdio_sd8797 }, 337 /* Marvell SD8887 Bluetooth device */ 338 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x9136), 339 .driver_data = (unsigned long)&btmrvl_sdio_sd8887 }, 340 /* Marvell SD8897 Bluetooth device */ 341 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x912E), 342 .driver_data = (unsigned long)&btmrvl_sdio_sd8897 }, 343 /* Marvell SD8977 Bluetooth device */ 344 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x9146), 345 .driver_data = (unsigned long)&btmrvl_sdio_sd8977 }, 346 /* Marvell SD8997 Bluetooth device */ 347 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x9142), 348 .driver_data = (unsigned long)&btmrvl_sdio_sd8997 }, 349 350 { } /* Terminating entry */ 351 }; 352 353 MODULE_DEVICE_TABLE(sdio, btmrvl_sdio_ids); 354 355 static int btmrvl_sdio_get_rx_unit(struct btmrvl_sdio_card *card) 356 { 357 u8 reg; 358 int ret; 359 360 reg = sdio_readb(card->func, card->reg->card_rx_unit, &ret); 361 if (!ret) 362 card->rx_unit = reg; 363 364 return ret; 365 } 366 367 static int btmrvl_sdio_read_fw_status(struct btmrvl_sdio_card *card, u16 *dat) 368 { 369 u8 fws0, fws1; 370 int ret; 371 372 *dat = 0; 373 374 fws0 = sdio_readb(card->func, card->reg->card_fw_status0, &ret); 375 if (ret) 376 return -EIO; 377 378 fws1 = sdio_readb(card->func, card->reg->card_fw_status1, &ret); 379 if (ret) 380 return -EIO; 381 382 *dat = (((u16) fws1) << 8) | fws0; 383 384 return 0; 385 } 386 387 static int btmrvl_sdio_read_rx_len(struct btmrvl_sdio_card *card, u16 *dat) 388 { 389 u8 reg; 390 int ret; 391 392 reg = sdio_readb(card->func, card->reg->card_rx_len, &ret); 393 if (!ret) 394 *dat = (u16) reg << card->rx_unit; 395 396 return ret; 397 } 398 399 static int btmrvl_sdio_enable_host_int_mask(struct btmrvl_sdio_card *card, 400 u8 mask) 401 { 402 int ret; 403 404 sdio_writeb(card->func, mask, card->reg->host_int_mask, &ret); 405 if (ret) { 406 BT_ERR("Unable to enable the host interrupt!"); 407 ret = -EIO; 408 } 409 410 return ret; 411 } 412 413 static int btmrvl_sdio_disable_host_int_mask(struct btmrvl_sdio_card *card, 414 u8 mask) 415 { 416 u8 host_int_mask; 417 int ret; 418 419 host_int_mask = sdio_readb(card->func, card->reg->host_int_mask, &ret); 420 if (ret) 421 return -EIO; 422 423 host_int_mask &= ~mask; 424 425 sdio_writeb(card->func, host_int_mask, card->reg->host_int_mask, &ret); 426 if (ret < 0) { 427 BT_ERR("Unable to disable the host interrupt!"); 428 return -EIO; 429 } 430 431 return 0; 432 } 433 434 static int btmrvl_sdio_poll_card_status(struct btmrvl_sdio_card *card, u8 bits) 435 { 436 unsigned int tries; 437 u8 status; 438 int ret; 439 440 for (tries = 0; tries < MAX_POLL_TRIES * 1000; tries++) { 441 status = sdio_readb(card->func, card->reg->card_status, &ret); 442 if (ret) 443 goto failed; 444 if ((status & bits) == bits) 445 return ret; 446 447 udelay(1); 448 } 449 450 ret = -ETIMEDOUT; 451 452 failed: 453 BT_ERR("FAILED! ret=%d", ret); 454 455 return ret; 456 } 457 458 static int btmrvl_sdio_verify_fw_download(struct btmrvl_sdio_card *card, 459 int pollnum) 460 { 461 u16 firmwarestat; 462 int tries, ret; 463 464 /* Wait for firmware to become ready */ 465 for (tries = 0; tries < pollnum; tries++) { 466 sdio_claim_host(card->func); 467 ret = btmrvl_sdio_read_fw_status(card, &firmwarestat); 468 sdio_release_host(card->func); 469 if (ret < 0) 470 continue; 471 472 if (firmwarestat == FIRMWARE_READY) 473 return 0; 474 475 msleep(100); 476 } 477 478 return -ETIMEDOUT; 479 } 480 481 static int btmrvl_sdio_download_helper(struct btmrvl_sdio_card *card) 482 { 483 const struct firmware *fw_helper = NULL; 484 const u8 *helper = NULL; 485 int ret; 486 void *tmphlprbuf = NULL; 487 int tmphlprbufsz, hlprblknow, helperlen; 488 u8 *helperbuf; 489 u32 tx_len; 490 491 ret = request_firmware(&fw_helper, card->helper, 492 &card->func->dev); 493 if ((ret < 0) || !fw_helper) { 494 BT_ERR("request_firmware(helper) failed, error code = %d", 495 ret); 496 ret = -ENOENT; 497 goto done; 498 } 499 500 helper = fw_helper->data; 501 helperlen = fw_helper->size; 502 503 BT_DBG("Downloading helper image (%d bytes), block size %d bytes", 504 helperlen, SDIO_BLOCK_SIZE); 505 506 tmphlprbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN); 507 508 tmphlprbuf = kzalloc(tmphlprbufsz, GFP_KERNEL); 509 if (!tmphlprbuf) { 510 BT_ERR("Unable to allocate buffer for helper." 511 " Terminating download"); 512 ret = -ENOMEM; 513 goto done; 514 } 515 516 helperbuf = (u8 *) ALIGN_ADDR(tmphlprbuf, BTSDIO_DMA_ALIGN); 517 518 /* Perform helper data transfer */ 519 tx_len = (FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE) 520 - SDIO_HEADER_LEN; 521 hlprblknow = 0; 522 523 do { 524 ret = btmrvl_sdio_poll_card_status(card, 525 CARD_IO_READY | DN_LD_CARD_RDY); 526 if (ret < 0) { 527 BT_ERR("Helper download poll status timeout @ %d", 528 hlprblknow); 529 goto done; 530 } 531 532 /* Check if there is more data? */ 533 if (hlprblknow >= helperlen) 534 break; 535 536 if (helperlen - hlprblknow < tx_len) 537 tx_len = helperlen - hlprblknow; 538 539 /* Little-endian */ 540 helperbuf[0] = ((tx_len & 0x000000ff) >> 0); 541 helperbuf[1] = ((tx_len & 0x0000ff00) >> 8); 542 helperbuf[2] = ((tx_len & 0x00ff0000) >> 16); 543 helperbuf[3] = ((tx_len & 0xff000000) >> 24); 544 545 memcpy(&helperbuf[SDIO_HEADER_LEN], &helper[hlprblknow], 546 tx_len); 547 548 /* Now send the data */ 549 ret = sdio_writesb(card->func, card->ioport, helperbuf, 550 FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE); 551 if (ret < 0) { 552 BT_ERR("IO error during helper download @ %d", 553 hlprblknow); 554 goto done; 555 } 556 557 hlprblknow += tx_len; 558 } while (true); 559 560 BT_DBG("Transferring helper image EOF block"); 561 562 memset(helperbuf, 0x0, SDIO_BLOCK_SIZE); 563 564 ret = sdio_writesb(card->func, card->ioport, helperbuf, 565 SDIO_BLOCK_SIZE); 566 if (ret < 0) { 567 BT_ERR("IO error in writing helper image EOF block"); 568 goto done; 569 } 570 571 ret = 0; 572 573 done: 574 kfree(tmphlprbuf); 575 release_firmware(fw_helper); 576 return ret; 577 } 578 579 static int btmrvl_sdio_download_fw_w_helper(struct btmrvl_sdio_card *card) 580 { 581 const struct firmware *fw_firmware = NULL; 582 const u8 *firmware = NULL; 583 int firmwarelen, tmpfwbufsz, ret; 584 unsigned int tries, offset; 585 u8 base0, base1; 586 void *tmpfwbuf = NULL; 587 u8 *fwbuf; 588 u16 len, blksz_dl = card->sd_blksz_fw_dl; 589 int txlen = 0, tx_blocks = 0, count = 0; 590 591 ret = request_firmware(&fw_firmware, card->firmware, 592 &card->func->dev); 593 if ((ret < 0) || !fw_firmware) { 594 BT_ERR("request_firmware(firmware) failed, error code = %d", 595 ret); 596 ret = -ENOENT; 597 goto done; 598 } 599 600 firmware = fw_firmware->data; 601 firmwarelen = fw_firmware->size; 602 603 BT_DBG("Downloading FW image (%d bytes)", firmwarelen); 604 605 tmpfwbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN); 606 tmpfwbuf = kzalloc(tmpfwbufsz, GFP_KERNEL); 607 if (!tmpfwbuf) { 608 BT_ERR("Unable to allocate buffer for firmware." 609 " Terminating download"); 610 ret = -ENOMEM; 611 goto done; 612 } 613 614 /* Ensure aligned firmware buffer */ 615 fwbuf = (u8 *) ALIGN_ADDR(tmpfwbuf, BTSDIO_DMA_ALIGN); 616 617 /* Perform firmware data transfer */ 618 offset = 0; 619 do { 620 ret = btmrvl_sdio_poll_card_status(card, 621 CARD_IO_READY | DN_LD_CARD_RDY); 622 if (ret < 0) { 623 BT_ERR("FW download with helper poll status" 624 " timeout @ %d", offset); 625 goto done; 626 } 627 628 /* Check if there is more data ? */ 629 if (offset >= firmwarelen) 630 break; 631 632 for (tries = 0; tries < MAX_POLL_TRIES; tries++) { 633 base0 = sdio_readb(card->func, 634 card->reg->sq_read_base_addr_a0, &ret); 635 if (ret) { 636 BT_ERR("BASE0 register read failed:" 637 " base0 = 0x%04X(%d)." 638 " Terminating download", 639 base0, base0); 640 ret = -EIO; 641 goto done; 642 } 643 base1 = sdio_readb(card->func, 644 card->reg->sq_read_base_addr_a1, &ret); 645 if (ret) { 646 BT_ERR("BASE1 register read failed:" 647 " base1 = 0x%04X(%d)." 648 " Terminating download", 649 base1, base1); 650 ret = -EIO; 651 goto done; 652 } 653 654 len = (((u16) base1) << 8) | base0; 655 if (len) 656 break; 657 658 udelay(10); 659 } 660 661 if (!len) 662 break; 663 else if (len > BTM_UPLD_SIZE) { 664 BT_ERR("FW download failure @%d, invalid length %d", 665 offset, len); 666 ret = -EINVAL; 667 goto done; 668 } 669 670 txlen = len; 671 672 if (len & BIT(0)) { 673 count++; 674 if (count > MAX_WRITE_IOMEM_RETRY) { 675 BT_ERR("FW download failure @%d, " 676 "over max retry count", offset); 677 ret = -EIO; 678 goto done; 679 } 680 BT_ERR("FW CRC error indicated by the helper: " 681 "len = 0x%04X, txlen = %d", len, txlen); 682 len &= ~BIT(0); 683 /* Set txlen to 0 so as to resend from same offset */ 684 txlen = 0; 685 } else { 686 count = 0; 687 688 /* Last block ? */ 689 if (firmwarelen - offset < txlen) 690 txlen = firmwarelen - offset; 691 692 tx_blocks = DIV_ROUND_UP(txlen, blksz_dl); 693 694 memcpy(fwbuf, &firmware[offset], txlen); 695 } 696 697 ret = sdio_writesb(card->func, card->ioport, fwbuf, 698 tx_blocks * blksz_dl); 699 700 if (ret < 0) { 701 BT_ERR("FW download, writesb(%d) failed @%d", 702 count, offset); 703 sdio_writeb(card->func, HOST_CMD53_FIN, 704 card->reg->cfg, &ret); 705 if (ret) 706 BT_ERR("writeb failed (CFG)"); 707 } 708 709 offset += txlen; 710 } while (true); 711 712 BT_INFO("FW download over, size %d bytes", offset); 713 714 ret = 0; 715 716 done: 717 kfree(tmpfwbuf); 718 release_firmware(fw_firmware); 719 return ret; 720 } 721 722 static int btmrvl_sdio_card_to_host(struct btmrvl_private *priv) 723 { 724 u16 buf_len = 0; 725 int ret, num_blocks, blksz; 726 struct sk_buff *skb = NULL; 727 u32 type; 728 u8 *payload; 729 struct hci_dev *hdev = priv->btmrvl_dev.hcidev; 730 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card; 731 732 if (!card || !card->func) { 733 BT_ERR("card or function is NULL!"); 734 ret = -EINVAL; 735 goto exit; 736 } 737 738 /* Read the length of data to be transferred */ 739 ret = btmrvl_sdio_read_rx_len(card, &buf_len); 740 if (ret < 0) { 741 BT_ERR("read rx_len failed"); 742 ret = -EIO; 743 goto exit; 744 } 745 746 blksz = SDIO_BLOCK_SIZE; 747 num_blocks = DIV_ROUND_UP(buf_len, blksz); 748 749 if (buf_len <= SDIO_HEADER_LEN 750 || (num_blocks * blksz) > ALLOC_BUF_SIZE) { 751 BT_ERR("invalid packet length: %d", buf_len); 752 ret = -EINVAL; 753 goto exit; 754 } 755 756 /* Allocate buffer */ 757 skb = bt_skb_alloc(num_blocks * blksz + BTSDIO_DMA_ALIGN, GFP_KERNEL); 758 if (!skb) { 759 BT_ERR("No free skb"); 760 ret = -ENOMEM; 761 goto exit; 762 } 763 764 if ((unsigned long) skb->data & (BTSDIO_DMA_ALIGN - 1)) { 765 skb_put(skb, (unsigned long) skb->data & 766 (BTSDIO_DMA_ALIGN - 1)); 767 skb_pull(skb, (unsigned long) skb->data & 768 (BTSDIO_DMA_ALIGN - 1)); 769 } 770 771 payload = skb->data; 772 773 ret = sdio_readsb(card->func, payload, card->ioport, 774 num_blocks * blksz); 775 if (ret < 0) { 776 BT_ERR("readsb failed: %d", ret); 777 ret = -EIO; 778 goto exit; 779 } 780 781 /* This is SDIO specific header length: byte[2][1][0], type: byte[3] 782 * (HCI_COMMAND = 1, ACL_DATA = 2, SCO_DATA = 3, 0xFE = Vendor) 783 */ 784 785 buf_len = payload[0]; 786 buf_len |= payload[1] << 8; 787 buf_len |= payload[2] << 16; 788 789 if (buf_len > blksz * num_blocks) { 790 BT_ERR("Skip incorrect packet: hdrlen %d buffer %d", 791 buf_len, blksz * num_blocks); 792 ret = -EIO; 793 goto exit; 794 } 795 796 type = payload[3]; 797 798 switch (type) { 799 case HCI_ACLDATA_PKT: 800 case HCI_SCODATA_PKT: 801 case HCI_EVENT_PKT: 802 hci_skb_pkt_type(skb) = type; 803 skb_put(skb, buf_len); 804 skb_pull(skb, SDIO_HEADER_LEN); 805 806 if (type == HCI_EVENT_PKT) { 807 if (btmrvl_check_evtpkt(priv, skb)) 808 hci_recv_frame(hdev, skb); 809 } else { 810 hci_recv_frame(hdev, skb); 811 } 812 813 hdev->stat.byte_rx += buf_len; 814 break; 815 816 case MRVL_VENDOR_PKT: 817 hci_skb_pkt_type(skb) = HCI_VENDOR_PKT; 818 skb_put(skb, buf_len); 819 skb_pull(skb, SDIO_HEADER_LEN); 820 821 if (btmrvl_process_event(priv, skb)) 822 hci_recv_frame(hdev, skb); 823 824 hdev->stat.byte_rx += buf_len; 825 break; 826 827 default: 828 BT_ERR("Unknown packet type:%d", type); 829 BT_ERR("hex: %*ph", blksz * num_blocks, payload); 830 831 kfree_skb(skb); 832 skb = NULL; 833 break; 834 } 835 836 exit: 837 if (ret) { 838 hdev->stat.err_rx++; 839 kfree_skb(skb); 840 } 841 842 return ret; 843 } 844 845 static int btmrvl_sdio_process_int_status(struct btmrvl_private *priv) 846 { 847 ulong flags; 848 u8 ireg; 849 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card; 850 851 spin_lock_irqsave(&priv->driver_lock, flags); 852 ireg = sdio_ireg; 853 sdio_ireg = 0; 854 spin_unlock_irqrestore(&priv->driver_lock, flags); 855 856 sdio_claim_host(card->func); 857 if (ireg & DN_LD_HOST_INT_STATUS) { 858 if (priv->btmrvl_dev.tx_dnld_rdy) 859 BT_DBG("tx_done already received: " 860 " int_status=0x%x", ireg); 861 else 862 priv->btmrvl_dev.tx_dnld_rdy = true; 863 } 864 865 if (ireg & UP_LD_HOST_INT_STATUS) 866 btmrvl_sdio_card_to_host(priv); 867 868 sdio_release_host(card->func); 869 870 return 0; 871 } 872 873 static int btmrvl_sdio_read_to_clear(struct btmrvl_sdio_card *card, u8 *ireg) 874 { 875 struct btmrvl_adapter *adapter = card->priv->adapter; 876 int ret; 877 878 ret = sdio_readsb(card->func, adapter->hw_regs, 0, SDIO_BLOCK_SIZE); 879 if (ret) { 880 BT_ERR("sdio_readsb: read int hw_regs failed: %d", ret); 881 return ret; 882 } 883 884 *ireg = adapter->hw_regs[card->reg->host_intstatus]; 885 BT_DBG("hw_regs[%#x]=%#x", card->reg->host_intstatus, *ireg); 886 887 return 0; 888 } 889 890 static int btmrvl_sdio_write_to_clear(struct btmrvl_sdio_card *card, u8 *ireg) 891 { 892 int ret; 893 894 *ireg = sdio_readb(card->func, card->reg->host_intstatus, &ret); 895 if (ret) { 896 BT_ERR("sdio_readb: read int status failed: %d", ret); 897 return ret; 898 } 899 900 if (*ireg) { 901 /* 902 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS 903 * Clear the interrupt status register and re-enable the 904 * interrupt. 905 */ 906 BT_DBG("int_status = 0x%x", *ireg); 907 908 sdio_writeb(card->func, ~(*ireg) & (DN_LD_HOST_INT_STATUS | 909 UP_LD_HOST_INT_STATUS), 910 card->reg->host_intstatus, &ret); 911 if (ret) { 912 BT_ERR("sdio_writeb: clear int status failed: %d", ret); 913 return ret; 914 } 915 } 916 917 return 0; 918 } 919 920 static void btmrvl_sdio_interrupt(struct sdio_func *func) 921 { 922 struct btmrvl_private *priv; 923 struct btmrvl_sdio_card *card; 924 ulong flags; 925 u8 ireg = 0; 926 int ret; 927 928 card = sdio_get_drvdata(func); 929 if (!card || !card->priv) { 930 BT_ERR("sbi_interrupt(%p) card or priv is NULL, card=%p", 931 func, card); 932 return; 933 } 934 935 priv = card->priv; 936 937 if (priv->surprise_removed) 938 return; 939 940 if (card->reg->int_read_to_clear) 941 ret = btmrvl_sdio_read_to_clear(card, &ireg); 942 else 943 ret = btmrvl_sdio_write_to_clear(card, &ireg); 944 945 if (ret) 946 return; 947 948 spin_lock_irqsave(&priv->driver_lock, flags); 949 sdio_ireg |= ireg; 950 spin_unlock_irqrestore(&priv->driver_lock, flags); 951 952 btmrvl_interrupt(priv); 953 } 954 955 static int btmrvl_sdio_register_dev(struct btmrvl_sdio_card *card) 956 { 957 struct sdio_func *func; 958 u8 reg; 959 int ret; 960 961 if (!card || !card->func) { 962 BT_ERR("Error: card or function is NULL!"); 963 ret = -EINVAL; 964 goto failed; 965 } 966 967 func = card->func; 968 969 sdio_claim_host(func); 970 971 ret = sdio_enable_func(func); 972 if (ret) { 973 BT_ERR("sdio_enable_func() failed: ret=%d", ret); 974 ret = -EIO; 975 goto release_host; 976 } 977 978 ret = sdio_claim_irq(func, btmrvl_sdio_interrupt); 979 if (ret) { 980 BT_ERR("sdio_claim_irq failed: ret=%d", ret); 981 ret = -EIO; 982 goto disable_func; 983 } 984 985 ret = sdio_set_block_size(card->func, SDIO_BLOCK_SIZE); 986 if (ret) { 987 BT_ERR("cannot set SDIO block size"); 988 ret = -EIO; 989 goto release_irq; 990 } 991 992 reg = sdio_readb(func, card->reg->io_port_0, &ret); 993 if (ret < 0) { 994 ret = -EIO; 995 goto release_irq; 996 } 997 998 card->ioport = reg; 999 1000 reg = sdio_readb(func, card->reg->io_port_1, &ret); 1001 if (ret < 0) { 1002 ret = -EIO; 1003 goto release_irq; 1004 } 1005 1006 card->ioport |= (reg << 8); 1007 1008 reg = sdio_readb(func, card->reg->io_port_2, &ret); 1009 if (ret < 0) { 1010 ret = -EIO; 1011 goto release_irq; 1012 } 1013 1014 card->ioport |= (reg << 16); 1015 1016 BT_DBG("SDIO FUNC%d IO port: 0x%x", func->num, card->ioport); 1017 1018 if (card->reg->int_read_to_clear) { 1019 reg = sdio_readb(func, card->reg->host_int_rsr, &ret); 1020 if (ret < 0) { 1021 ret = -EIO; 1022 goto release_irq; 1023 } 1024 sdio_writeb(func, reg | 0x3f, card->reg->host_int_rsr, &ret); 1025 if (ret < 0) { 1026 ret = -EIO; 1027 goto release_irq; 1028 } 1029 1030 reg = sdio_readb(func, card->reg->card_misc_cfg, &ret); 1031 if (ret < 0) { 1032 ret = -EIO; 1033 goto release_irq; 1034 } 1035 sdio_writeb(func, reg | 0x10, card->reg->card_misc_cfg, &ret); 1036 if (ret < 0) { 1037 ret = -EIO; 1038 goto release_irq; 1039 } 1040 } 1041 1042 sdio_set_drvdata(func, card); 1043 1044 sdio_release_host(func); 1045 1046 return 0; 1047 1048 release_irq: 1049 sdio_release_irq(func); 1050 1051 disable_func: 1052 sdio_disable_func(func); 1053 1054 release_host: 1055 sdio_release_host(func); 1056 1057 failed: 1058 return ret; 1059 } 1060 1061 static int btmrvl_sdio_unregister_dev(struct btmrvl_sdio_card *card) 1062 { 1063 if (card && card->func) { 1064 sdio_claim_host(card->func); 1065 sdio_release_irq(card->func); 1066 sdio_disable_func(card->func); 1067 sdio_release_host(card->func); 1068 sdio_set_drvdata(card->func, NULL); 1069 } 1070 1071 return 0; 1072 } 1073 1074 static int btmrvl_sdio_enable_host_int(struct btmrvl_sdio_card *card) 1075 { 1076 int ret; 1077 1078 if (!card || !card->func) 1079 return -EINVAL; 1080 1081 sdio_claim_host(card->func); 1082 1083 ret = btmrvl_sdio_enable_host_int_mask(card, HIM_ENABLE); 1084 1085 btmrvl_sdio_get_rx_unit(card); 1086 1087 sdio_release_host(card->func); 1088 1089 return ret; 1090 } 1091 1092 static int btmrvl_sdio_disable_host_int(struct btmrvl_sdio_card *card) 1093 { 1094 int ret; 1095 1096 if (!card || !card->func) 1097 return -EINVAL; 1098 1099 sdio_claim_host(card->func); 1100 1101 ret = btmrvl_sdio_disable_host_int_mask(card, HIM_DISABLE); 1102 1103 sdio_release_host(card->func); 1104 1105 return ret; 1106 } 1107 1108 static int btmrvl_sdio_host_to_card(struct btmrvl_private *priv, 1109 u8 *payload, u16 nb) 1110 { 1111 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card; 1112 int ret = 0; 1113 int blksz; 1114 int i = 0; 1115 u8 *buf = NULL; 1116 void *tmpbuf = NULL; 1117 int tmpbufsz; 1118 1119 if (!card || !card->func) { 1120 BT_ERR("card or function is NULL!"); 1121 return -EINVAL; 1122 } 1123 1124 blksz = DIV_ROUND_UP(nb, SDIO_BLOCK_SIZE) * SDIO_BLOCK_SIZE; 1125 1126 buf = payload; 1127 if ((unsigned long) payload & (BTSDIO_DMA_ALIGN - 1) || 1128 nb < blksz) { 1129 tmpbufsz = ALIGN_SZ(blksz, BTSDIO_DMA_ALIGN) + 1130 BTSDIO_DMA_ALIGN; 1131 tmpbuf = kzalloc(tmpbufsz, GFP_KERNEL); 1132 if (!tmpbuf) 1133 return -ENOMEM; 1134 buf = (u8 *) ALIGN_ADDR(tmpbuf, BTSDIO_DMA_ALIGN); 1135 memcpy(buf, payload, nb); 1136 } 1137 1138 sdio_claim_host(card->func); 1139 1140 do { 1141 /* Transfer data to card */ 1142 ret = sdio_writesb(card->func, card->ioport, buf, 1143 blksz); 1144 if (ret < 0) { 1145 i++; 1146 BT_ERR("i=%d writesb failed: %d", i, ret); 1147 BT_ERR("hex: %*ph", nb, payload); 1148 ret = -EIO; 1149 if (i > MAX_WRITE_IOMEM_RETRY) 1150 goto exit; 1151 } 1152 } while (ret); 1153 1154 priv->btmrvl_dev.tx_dnld_rdy = false; 1155 1156 exit: 1157 sdio_release_host(card->func); 1158 kfree(tmpbuf); 1159 1160 return ret; 1161 } 1162 1163 static int btmrvl_sdio_download_fw(struct btmrvl_sdio_card *card) 1164 { 1165 int ret; 1166 u8 fws0; 1167 int pollnum = MAX_POLL_TRIES; 1168 1169 if (!card || !card->func) { 1170 BT_ERR("card or function is NULL!"); 1171 return -EINVAL; 1172 } 1173 1174 if (!btmrvl_sdio_verify_fw_download(card, 1)) { 1175 BT_DBG("Firmware already downloaded!"); 1176 return 0; 1177 } 1178 1179 sdio_claim_host(card->func); 1180 1181 /* Check if other function driver is downloading the firmware */ 1182 fws0 = sdio_readb(card->func, card->reg->card_fw_status0, &ret); 1183 if (ret) { 1184 BT_ERR("Failed to read FW downloading status!"); 1185 ret = -EIO; 1186 goto done; 1187 } 1188 if (fws0) { 1189 BT_DBG("BT not the winner (%#x). Skip FW downloading", fws0); 1190 1191 /* Give other function more time to download the firmware */ 1192 pollnum *= 10; 1193 } else { 1194 if (card->helper) { 1195 ret = btmrvl_sdio_download_helper(card); 1196 if (ret) { 1197 BT_ERR("Failed to download helper!"); 1198 ret = -EIO; 1199 goto done; 1200 } 1201 } 1202 1203 if (btmrvl_sdio_download_fw_w_helper(card)) { 1204 BT_ERR("Failed to download firmware!"); 1205 ret = -EIO; 1206 goto done; 1207 } 1208 } 1209 1210 /* 1211 * winner or not, with this test the FW synchronizes when the 1212 * module can continue its initialization 1213 */ 1214 if (btmrvl_sdio_verify_fw_download(card, pollnum)) { 1215 BT_ERR("FW failed to be active in time!"); 1216 ret = -ETIMEDOUT; 1217 goto done; 1218 } 1219 1220 sdio_release_host(card->func); 1221 1222 return 0; 1223 1224 done: 1225 sdio_release_host(card->func); 1226 return ret; 1227 } 1228 1229 static int btmrvl_sdio_wakeup_fw(struct btmrvl_private *priv) 1230 { 1231 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card; 1232 int ret = 0; 1233 1234 if (!card || !card->func) { 1235 BT_ERR("card or function is NULL!"); 1236 return -EINVAL; 1237 } 1238 1239 sdio_claim_host(card->func); 1240 1241 sdio_writeb(card->func, HOST_POWER_UP, card->reg->cfg, &ret); 1242 1243 sdio_release_host(card->func); 1244 1245 BT_DBG("wake up firmware"); 1246 1247 return ret; 1248 } 1249 1250 static void btmrvl_sdio_dump_regs(struct btmrvl_private *priv) 1251 { 1252 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card; 1253 int ret = 0; 1254 unsigned int reg, reg_start, reg_end; 1255 char buf[256], *ptr; 1256 u8 loop, func, data; 1257 int MAX_LOOP = 2; 1258 1259 btmrvl_sdio_wakeup_fw(priv); 1260 sdio_claim_host(card->func); 1261 1262 for (loop = 0; loop < MAX_LOOP; loop++) { 1263 memset(buf, 0, sizeof(buf)); 1264 ptr = buf; 1265 1266 if (loop == 0) { 1267 /* Read the registers of SDIO function0 */ 1268 func = loop; 1269 reg_start = 0; 1270 reg_end = 9; 1271 } else { 1272 func = 2; 1273 reg_start = 0; 1274 reg_end = 0x09; 1275 } 1276 1277 ptr += sprintf(ptr, "SDIO Func%d (%#x-%#x): ", 1278 func, reg_start, reg_end); 1279 for (reg = reg_start; reg <= reg_end; reg++) { 1280 if (func == 0) 1281 data = sdio_f0_readb(card->func, reg, &ret); 1282 else 1283 data = sdio_readb(card->func, reg, &ret); 1284 1285 if (!ret) { 1286 ptr += sprintf(ptr, "%02x ", data); 1287 } else { 1288 ptr += sprintf(ptr, "ERR"); 1289 break; 1290 } 1291 } 1292 1293 BT_INFO("%s", buf); 1294 } 1295 1296 sdio_release_host(card->func); 1297 } 1298 1299 /* This function read/write firmware */ 1300 static enum 1301 rdwr_status btmrvl_sdio_rdwr_firmware(struct btmrvl_private *priv, 1302 u8 doneflag) 1303 { 1304 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card; 1305 int ret, tries; 1306 u8 ctrl_data = 0; 1307 1308 sdio_writeb(card->func, FW_DUMP_HOST_READY, card->reg->fw_dump_ctrl, 1309 &ret); 1310 1311 if (ret) { 1312 BT_ERR("SDIO write err"); 1313 return RDWR_STATUS_FAILURE; 1314 } 1315 1316 for (tries = 0; tries < MAX_POLL_TRIES; tries++) { 1317 ctrl_data = sdio_readb(card->func, card->reg->fw_dump_ctrl, 1318 &ret); 1319 1320 if (ret) { 1321 BT_ERR("SDIO read err"); 1322 return RDWR_STATUS_FAILURE; 1323 } 1324 1325 if (ctrl_data == FW_DUMP_DONE) 1326 break; 1327 if (doneflag && ctrl_data == doneflag) 1328 return RDWR_STATUS_DONE; 1329 if (ctrl_data != FW_DUMP_HOST_READY) { 1330 BT_INFO("The ctrl reg was changed, re-try again!"); 1331 sdio_writeb(card->func, FW_DUMP_HOST_READY, 1332 card->reg->fw_dump_ctrl, &ret); 1333 if (ret) { 1334 BT_ERR("SDIO write err"); 1335 return RDWR_STATUS_FAILURE; 1336 } 1337 } 1338 usleep_range(100, 200); 1339 } 1340 1341 if (ctrl_data == FW_DUMP_HOST_READY) { 1342 BT_ERR("Fail to pull ctrl_data"); 1343 return RDWR_STATUS_FAILURE; 1344 } 1345 1346 return RDWR_STATUS_SUCCESS; 1347 } 1348 1349 /* This function dump sdio register and memory data */ 1350 static void btmrvl_sdio_coredump(struct device *dev) 1351 { 1352 struct sdio_func *func = dev_to_sdio_func(dev); 1353 struct btmrvl_sdio_card *card; 1354 struct btmrvl_private *priv; 1355 int ret = 0; 1356 unsigned int reg, reg_start, reg_end; 1357 enum rdwr_status stat; 1358 u8 *dbg_ptr, *end_ptr, *fw_dump_data, *fw_dump_ptr; 1359 u8 dump_num = 0, idx, i, read_reg, doneflag = 0; 1360 u32 memory_size, fw_dump_len = 0; 1361 1362 card = sdio_get_drvdata(func); 1363 priv = card->priv; 1364 1365 /* dump sdio register first */ 1366 btmrvl_sdio_dump_regs(priv); 1367 1368 if (!card->supports_fw_dump) { 1369 BT_ERR("Firmware dump not supported for this card!"); 1370 return; 1371 } 1372 1373 for (idx = 0; idx < ARRAY_SIZE(mem_type_mapping_tbl); idx++) { 1374 struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx]; 1375 1376 if (entry->mem_ptr) { 1377 vfree(entry->mem_ptr); 1378 entry->mem_ptr = NULL; 1379 } 1380 entry->mem_size = 0; 1381 } 1382 1383 btmrvl_sdio_wakeup_fw(priv); 1384 sdio_claim_host(card->func); 1385 1386 BT_INFO("== btmrvl firmware dump start =="); 1387 1388 stat = btmrvl_sdio_rdwr_firmware(priv, doneflag); 1389 if (stat == RDWR_STATUS_FAILURE) 1390 goto done; 1391 1392 reg = card->reg->fw_dump_start; 1393 /* Read the number of the memories which will dump */ 1394 dump_num = sdio_readb(card->func, reg, &ret); 1395 1396 if (ret) { 1397 BT_ERR("SDIO read memory length err"); 1398 goto done; 1399 } 1400 1401 /* Read the length of every memory which will dump */ 1402 for (idx = 0; idx < dump_num; idx++) { 1403 struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx]; 1404 1405 stat = btmrvl_sdio_rdwr_firmware(priv, doneflag); 1406 if (stat == RDWR_STATUS_FAILURE) 1407 goto done; 1408 1409 memory_size = 0; 1410 reg = card->reg->fw_dump_start; 1411 for (i = 0; i < 4; i++) { 1412 read_reg = sdio_readb(card->func, reg, &ret); 1413 if (ret) { 1414 BT_ERR("SDIO read err"); 1415 goto done; 1416 } 1417 memory_size |= (read_reg << i*8); 1418 reg++; 1419 } 1420 1421 if (memory_size == 0) { 1422 BT_INFO("Firmware dump finished!"); 1423 sdio_writeb(card->func, FW_DUMP_READ_DONE, 1424 card->reg->fw_dump_ctrl, &ret); 1425 if (ret) { 1426 BT_ERR("SDIO Write MEMDUMP_FINISH ERR"); 1427 goto done; 1428 } 1429 break; 1430 } 1431 1432 BT_INFO("%s_SIZE=0x%x", entry->mem_name, memory_size); 1433 entry->mem_ptr = vzalloc(memory_size + 1); 1434 entry->mem_size = memory_size; 1435 if (!entry->mem_ptr) { 1436 BT_ERR("Vzalloc %s failed", entry->mem_name); 1437 goto done; 1438 } 1439 1440 fw_dump_len += (strlen("========Start dump ") + 1441 strlen(entry->mem_name) + 1442 strlen("========\n") + 1443 (memory_size + 1) + 1444 strlen("\n========End dump========\n")); 1445 1446 dbg_ptr = entry->mem_ptr; 1447 end_ptr = dbg_ptr + memory_size; 1448 1449 doneflag = entry->done_flag; 1450 BT_INFO("Start %s output, please wait...", 1451 entry->mem_name); 1452 1453 do { 1454 stat = btmrvl_sdio_rdwr_firmware(priv, doneflag); 1455 if (stat == RDWR_STATUS_FAILURE) 1456 goto done; 1457 1458 reg_start = card->reg->fw_dump_start; 1459 reg_end = card->reg->fw_dump_end; 1460 for (reg = reg_start; reg <= reg_end; reg++) { 1461 *dbg_ptr = sdio_readb(card->func, reg, &ret); 1462 if (ret) { 1463 BT_ERR("SDIO read err"); 1464 goto done; 1465 } 1466 if (dbg_ptr < end_ptr) 1467 dbg_ptr++; 1468 else 1469 BT_ERR("Allocated buffer not enough"); 1470 } 1471 1472 if (stat != RDWR_STATUS_DONE) { 1473 continue; 1474 } else { 1475 BT_INFO("%s done: size=0x%tx", 1476 entry->mem_name, 1477 dbg_ptr - entry->mem_ptr); 1478 break; 1479 } 1480 } while (1); 1481 } 1482 1483 BT_INFO("== btmrvl firmware dump end =="); 1484 1485 done: 1486 sdio_release_host(card->func); 1487 1488 if (fw_dump_len == 0) 1489 return; 1490 1491 fw_dump_data = vzalloc(fw_dump_len+1); 1492 if (!fw_dump_data) { 1493 BT_ERR("Vzalloc fw_dump_data fail!"); 1494 return; 1495 } 1496 fw_dump_ptr = fw_dump_data; 1497 1498 /* Dump all the memory data into single file, a userspace script will 1499 * be used to split all the memory data to multiple files 1500 */ 1501 BT_INFO("== btmrvl firmware dump to /sys/class/devcoredump start"); 1502 for (idx = 0; idx < dump_num; idx++) { 1503 struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx]; 1504 1505 if (entry->mem_ptr) { 1506 strcpy(fw_dump_ptr, "========Start dump "); 1507 fw_dump_ptr += strlen("========Start dump "); 1508 1509 strcpy(fw_dump_ptr, entry->mem_name); 1510 fw_dump_ptr += strlen(entry->mem_name); 1511 1512 strcpy(fw_dump_ptr, "========\n"); 1513 fw_dump_ptr += strlen("========\n"); 1514 1515 memcpy(fw_dump_ptr, entry->mem_ptr, entry->mem_size); 1516 fw_dump_ptr += entry->mem_size; 1517 1518 strcpy(fw_dump_ptr, "\n========End dump========\n"); 1519 fw_dump_ptr += strlen("\n========End dump========\n"); 1520 1521 vfree(mem_type_mapping_tbl[idx].mem_ptr); 1522 mem_type_mapping_tbl[idx].mem_ptr = NULL; 1523 } 1524 } 1525 1526 /* fw_dump_data will be free in device coredump release function 1527 * after 5 min 1528 */ 1529 dev_coredumpv(&card->func->dev, fw_dump_data, fw_dump_len, GFP_KERNEL); 1530 BT_INFO("== btmrvl firmware dump to /sys/class/devcoredump end"); 1531 } 1532 1533 static int btmrvl_sdio_probe(struct sdio_func *func, 1534 const struct sdio_device_id *id) 1535 { 1536 int ret = 0; 1537 struct btmrvl_private *priv = NULL; 1538 struct btmrvl_sdio_card *card = NULL; 1539 1540 BT_INFO("vendor=0x%x, device=0x%x, class=%d, fn=%d", 1541 id->vendor, id->device, id->class, func->num); 1542 1543 card = devm_kzalloc(&func->dev, sizeof(*card), GFP_KERNEL); 1544 if (!card) 1545 return -ENOMEM; 1546 1547 card->func = func; 1548 1549 if (id->driver_data) { 1550 struct btmrvl_sdio_device *data = (void *) id->driver_data; 1551 card->helper = data->helper; 1552 card->firmware = data->firmware; 1553 card->reg = data->reg; 1554 card->sd_blksz_fw_dl = data->sd_blksz_fw_dl; 1555 card->support_pscan_win_report = data->support_pscan_win_report; 1556 card->supports_fw_dump = data->supports_fw_dump; 1557 } 1558 1559 if (btmrvl_sdio_register_dev(card) < 0) { 1560 BT_ERR("Failed to register BT device!"); 1561 return -ENODEV; 1562 } 1563 1564 /* Disable the interrupts on the card */ 1565 btmrvl_sdio_disable_host_int(card); 1566 1567 if (btmrvl_sdio_download_fw(card)) { 1568 BT_ERR("Downloading firmware failed!"); 1569 ret = -ENODEV; 1570 goto unreg_dev; 1571 } 1572 1573 btmrvl_sdio_enable_host_int(card); 1574 1575 /* Device tree node parsing and platform specific configuration*/ 1576 btmrvl_sdio_probe_of(&func->dev, card); 1577 1578 priv = btmrvl_add_card(card); 1579 if (!priv) { 1580 BT_ERR("Initializing card failed!"); 1581 ret = -ENODEV; 1582 goto disable_host_int; 1583 } 1584 1585 card->priv = priv; 1586 1587 /* Initialize the interface specific function pointers */ 1588 priv->hw_host_to_card = btmrvl_sdio_host_to_card; 1589 priv->hw_wakeup_firmware = btmrvl_sdio_wakeup_fw; 1590 priv->hw_process_int_status = btmrvl_sdio_process_int_status; 1591 1592 if (btmrvl_register_hdev(priv)) { 1593 BT_ERR("Register hdev failed!"); 1594 ret = -ENODEV; 1595 goto disable_host_int; 1596 } 1597 1598 return 0; 1599 1600 disable_host_int: 1601 btmrvl_sdio_disable_host_int(card); 1602 unreg_dev: 1603 btmrvl_sdio_unregister_dev(card); 1604 return ret; 1605 } 1606 1607 static void btmrvl_sdio_remove(struct sdio_func *func) 1608 { 1609 struct btmrvl_sdio_card *card; 1610 1611 if (func) { 1612 card = sdio_get_drvdata(func); 1613 if (card) { 1614 /* Send SHUTDOWN command & disable interrupt 1615 * if user removes the module. 1616 */ 1617 if (user_rmmod) { 1618 btmrvl_send_module_cfg_cmd(card->priv, 1619 MODULE_SHUTDOWN_REQ); 1620 btmrvl_sdio_disable_host_int(card); 1621 } 1622 BT_DBG("unregister dev"); 1623 card->priv->surprise_removed = true; 1624 btmrvl_sdio_unregister_dev(card); 1625 btmrvl_remove_card(card->priv); 1626 } 1627 } 1628 } 1629 1630 static int btmrvl_sdio_suspend(struct device *dev) 1631 { 1632 struct sdio_func *func = dev_to_sdio_func(dev); 1633 struct btmrvl_sdio_card *card; 1634 struct btmrvl_private *priv; 1635 mmc_pm_flag_t pm_flags; 1636 struct hci_dev *hcidev; 1637 1638 if (func) { 1639 pm_flags = sdio_get_host_pm_caps(func); 1640 BT_DBG("%s: suspend: PM flags = 0x%x", sdio_func_id(func), 1641 pm_flags); 1642 if (!(pm_flags & MMC_PM_KEEP_POWER)) { 1643 BT_ERR("%s: cannot remain alive while suspended", 1644 sdio_func_id(func)); 1645 return -ENOSYS; 1646 } 1647 card = sdio_get_drvdata(func); 1648 if (!card || !card->priv) { 1649 BT_ERR("card or priv structure is not valid"); 1650 return 0; 1651 } 1652 } else { 1653 BT_ERR("sdio_func is not specified"); 1654 return 0; 1655 } 1656 1657 /* Enable platform specific wakeup interrupt */ 1658 if (card->plt_wake_cfg && card->plt_wake_cfg->irq_bt >= 0) { 1659 card->plt_wake_cfg->wake_by_bt = false; 1660 enable_irq(card->plt_wake_cfg->irq_bt); 1661 enable_irq_wake(card->plt_wake_cfg->irq_bt); 1662 } 1663 1664 priv = card->priv; 1665 priv->adapter->is_suspending = true; 1666 hcidev = priv->btmrvl_dev.hcidev; 1667 BT_DBG("%s: SDIO suspend", hcidev->name); 1668 hci_suspend_dev(hcidev); 1669 1670 if (priv->adapter->hs_state != HS_ACTIVATED) { 1671 if (btmrvl_enable_hs(priv)) { 1672 BT_ERR("HS not activated, suspend failed!"); 1673 /* Disable platform specific wakeup interrupt */ 1674 if (card->plt_wake_cfg && 1675 card->plt_wake_cfg->irq_bt >= 0) { 1676 disable_irq_wake(card->plt_wake_cfg->irq_bt); 1677 disable_irq(card->plt_wake_cfg->irq_bt); 1678 } 1679 1680 priv->adapter->is_suspending = false; 1681 return -EBUSY; 1682 } 1683 } 1684 1685 priv->adapter->is_suspending = false; 1686 priv->adapter->is_suspended = true; 1687 1688 /* We will keep the power when hs enabled successfully */ 1689 if (priv->adapter->hs_state == HS_ACTIVATED) { 1690 BT_DBG("suspend with MMC_PM_KEEP_POWER"); 1691 return sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER); 1692 } 1693 1694 BT_DBG("suspend without MMC_PM_KEEP_POWER"); 1695 return 0; 1696 } 1697 1698 static int btmrvl_sdio_resume(struct device *dev) 1699 { 1700 struct sdio_func *func = dev_to_sdio_func(dev); 1701 struct btmrvl_sdio_card *card; 1702 struct btmrvl_private *priv; 1703 mmc_pm_flag_t pm_flags; 1704 struct hci_dev *hcidev; 1705 1706 if (func) { 1707 pm_flags = sdio_get_host_pm_caps(func); 1708 BT_DBG("%s: resume: PM flags = 0x%x", sdio_func_id(func), 1709 pm_flags); 1710 card = sdio_get_drvdata(func); 1711 if (!card || !card->priv) { 1712 BT_ERR("card or priv structure is not valid"); 1713 return 0; 1714 } 1715 } else { 1716 BT_ERR("sdio_func is not specified"); 1717 return 0; 1718 } 1719 priv = card->priv; 1720 1721 if (!priv->adapter->is_suspended) { 1722 BT_DBG("device already resumed"); 1723 return 0; 1724 } 1725 1726 priv->hw_wakeup_firmware(priv); 1727 priv->adapter->hs_state = HS_DEACTIVATED; 1728 hcidev = priv->btmrvl_dev.hcidev; 1729 BT_DBG("%s: HS DEACTIVATED in resume!", hcidev->name); 1730 priv->adapter->is_suspended = false; 1731 BT_DBG("%s: SDIO resume", hcidev->name); 1732 hci_resume_dev(hcidev); 1733 1734 /* Disable platform specific wakeup interrupt */ 1735 if (card->plt_wake_cfg && card->plt_wake_cfg->irq_bt >= 0) { 1736 disable_irq_wake(card->plt_wake_cfg->irq_bt); 1737 disable_irq(card->plt_wake_cfg->irq_bt); 1738 if (card->plt_wake_cfg->wake_by_bt) 1739 /* Undo our disable, since interrupt handler already 1740 * did this. 1741 */ 1742 enable_irq(card->plt_wake_cfg->irq_bt); 1743 } 1744 1745 return 0; 1746 } 1747 1748 static const struct dev_pm_ops btmrvl_sdio_pm_ops = { 1749 .suspend = btmrvl_sdio_suspend, 1750 .resume = btmrvl_sdio_resume, 1751 }; 1752 1753 static struct sdio_driver bt_mrvl_sdio = { 1754 .name = "btmrvl_sdio", 1755 .id_table = btmrvl_sdio_ids, 1756 .probe = btmrvl_sdio_probe, 1757 .remove = btmrvl_sdio_remove, 1758 .drv = { 1759 .owner = THIS_MODULE, 1760 .coredump = btmrvl_sdio_coredump, 1761 .pm = &btmrvl_sdio_pm_ops, 1762 } 1763 }; 1764 1765 static int __init btmrvl_sdio_init_module(void) 1766 { 1767 if (sdio_register_driver(&bt_mrvl_sdio) != 0) { 1768 BT_ERR("SDIO Driver Registration Failed"); 1769 return -ENODEV; 1770 } 1771 1772 /* Clear the flag in case user removes the card. */ 1773 user_rmmod = 0; 1774 1775 return 0; 1776 } 1777 1778 static void __exit btmrvl_sdio_exit_module(void) 1779 { 1780 /* Set the flag as user is removing this module. */ 1781 user_rmmod = 1; 1782 1783 sdio_unregister_driver(&bt_mrvl_sdio); 1784 } 1785 1786 module_init(btmrvl_sdio_init_module); 1787 module_exit(btmrvl_sdio_exit_module); 1788 1789 MODULE_AUTHOR("Marvell International Ltd."); 1790 MODULE_DESCRIPTION("Marvell BT-over-SDIO driver ver " VERSION); 1791 MODULE_VERSION(VERSION); 1792 MODULE_LICENSE("GPL v2"); 1793 MODULE_FIRMWARE("mrvl/sd8688_helper.bin"); 1794 MODULE_FIRMWARE("mrvl/sd8688.bin"); 1795 MODULE_FIRMWARE("mrvl/sd8787_uapsta.bin"); 1796 MODULE_FIRMWARE("mrvl/sd8797_uapsta.bin"); 1797 MODULE_FIRMWARE("mrvl/sd8887_uapsta.bin"); 1798 MODULE_FIRMWARE("mrvl/sd8897_uapsta.bin"); 1799 MODULE_FIRMWARE("mrvl/sd8977_uapsta.bin"); 1800 MODULE_FIRMWARE("mrvl/sd8997_uapsta.bin"); 1801