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