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 24 #include <linux/mmc/sdio_ids.h> 25 #include <linux/mmc/sdio_func.h> 26 #include <linux/module.h> 27 28 #include <net/bluetooth/bluetooth.h> 29 #include <net/bluetooth/hci_core.h> 30 31 #include "btmrvl_drv.h" 32 #include "btmrvl_sdio.h" 33 34 #define VERSION "1.0" 35 36 /* The btmrvl_sdio_remove() callback function is called 37 * when user removes this module from kernel space or ejects 38 * the card from the slot. The driver handles these 2 cases 39 * differently. 40 * If the user is removing the module, a MODULE_SHUTDOWN_REQ 41 * command is sent to firmware and interrupt will be disabled. 42 * If the card is removed, there is no need to send command 43 * or disable interrupt. 44 * 45 * The variable 'user_rmmod' is used to distinguish these two 46 * scenarios. This flag is initialized as FALSE in case the card 47 * is removed, and will be set to TRUE for module removal when 48 * module_exit function is called. 49 */ 50 static u8 user_rmmod; 51 static u8 sdio_ireg; 52 53 static const struct btmrvl_sdio_card_reg btmrvl_reg_8688 = { 54 .cfg = 0x03, 55 .host_int_mask = 0x04, 56 .host_intstatus = 0x05, 57 .card_status = 0x20, 58 .sq_read_base_addr_a0 = 0x10, 59 .sq_read_base_addr_a1 = 0x11, 60 .card_fw_status0 = 0x40, 61 .card_fw_status1 = 0x41, 62 .card_rx_len = 0x42, 63 .card_rx_unit = 0x43, 64 .io_port_0 = 0x00, 65 .io_port_1 = 0x01, 66 .io_port_2 = 0x02, 67 .int_read_to_clear = false, 68 }; 69 static const struct btmrvl_sdio_card_reg btmrvl_reg_87xx = { 70 .cfg = 0x00, 71 .host_int_mask = 0x02, 72 .host_intstatus = 0x03, 73 .card_status = 0x30, 74 .sq_read_base_addr_a0 = 0x40, 75 .sq_read_base_addr_a1 = 0x41, 76 .card_revision = 0x5c, 77 .card_fw_status0 = 0x60, 78 .card_fw_status1 = 0x61, 79 .card_rx_len = 0x62, 80 .card_rx_unit = 0x63, 81 .io_port_0 = 0x78, 82 .io_port_1 = 0x79, 83 .io_port_2 = 0x7a, 84 .int_read_to_clear = false, 85 }; 86 87 static const struct btmrvl_sdio_card_reg btmrvl_reg_8887 = { 88 .cfg = 0x00, 89 .host_int_mask = 0x08, 90 .host_intstatus = 0x0C, 91 .card_status = 0x5C, 92 .sq_read_base_addr_a0 = 0x6C, 93 .sq_read_base_addr_a1 = 0x6D, 94 .card_revision = 0xC8, 95 .card_fw_status0 = 0x88, 96 .card_fw_status1 = 0x89, 97 .card_rx_len = 0x8A, 98 .card_rx_unit = 0x8B, 99 .io_port_0 = 0xE4, 100 .io_port_1 = 0xE5, 101 .io_port_2 = 0xE6, 102 .int_read_to_clear = true, 103 .host_int_rsr = 0x04, 104 .card_misc_cfg = 0xD8, 105 }; 106 107 static const struct btmrvl_sdio_card_reg btmrvl_reg_8897 = { 108 .cfg = 0x00, 109 .host_int_mask = 0x02, 110 .host_intstatus = 0x03, 111 .card_status = 0x50, 112 .sq_read_base_addr_a0 = 0x60, 113 .sq_read_base_addr_a1 = 0x61, 114 .card_revision = 0xbc, 115 .card_fw_status0 = 0xc0, 116 .card_fw_status1 = 0xc1, 117 .card_rx_len = 0xc2, 118 .card_rx_unit = 0xc3, 119 .io_port_0 = 0xd8, 120 .io_port_1 = 0xd9, 121 .io_port_2 = 0xda, 122 .int_read_to_clear = true, 123 .host_int_rsr = 0x01, 124 .card_misc_cfg = 0xcc, 125 }; 126 127 static const struct btmrvl_sdio_device btmrvl_sdio_sd8688 = { 128 .helper = "mrvl/sd8688_helper.bin", 129 .firmware = "mrvl/sd8688.bin", 130 .reg = &btmrvl_reg_8688, 131 .support_pscan_win_report = false, 132 .sd_blksz_fw_dl = 64, 133 }; 134 135 static const struct btmrvl_sdio_device btmrvl_sdio_sd8787 = { 136 .helper = NULL, 137 .firmware = "mrvl/sd8787_uapsta.bin", 138 .reg = &btmrvl_reg_87xx, 139 .support_pscan_win_report = false, 140 .sd_blksz_fw_dl = 256, 141 }; 142 143 static const struct btmrvl_sdio_device btmrvl_sdio_sd8797 = { 144 .helper = NULL, 145 .firmware = "mrvl/sd8797_uapsta.bin", 146 .reg = &btmrvl_reg_87xx, 147 .support_pscan_win_report = false, 148 .sd_blksz_fw_dl = 256, 149 }; 150 151 static const struct btmrvl_sdio_device btmrvl_sdio_sd8887 = { 152 .helper = NULL, 153 .firmware = "mrvl/sd8887_uapsta.bin", 154 .reg = &btmrvl_reg_8887, 155 .support_pscan_win_report = true, 156 .sd_blksz_fw_dl = 256, 157 }; 158 159 static const struct btmrvl_sdio_device btmrvl_sdio_sd8897 = { 160 .helper = NULL, 161 .firmware = "mrvl/sd8897_uapsta.bin", 162 .reg = &btmrvl_reg_8897, 163 .support_pscan_win_report = true, 164 .sd_blksz_fw_dl = 256, 165 }; 166 167 static const struct sdio_device_id btmrvl_sdio_ids[] = { 168 /* Marvell SD8688 Bluetooth device */ 169 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x9105), 170 .driver_data = (unsigned long) &btmrvl_sdio_sd8688 }, 171 /* Marvell SD8787 Bluetooth device */ 172 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x911A), 173 .driver_data = (unsigned long) &btmrvl_sdio_sd8787 }, 174 /* Marvell SD8787 Bluetooth AMP device */ 175 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x911B), 176 .driver_data = (unsigned long) &btmrvl_sdio_sd8787 }, 177 /* Marvell SD8797 Bluetooth device */ 178 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x912A), 179 .driver_data = (unsigned long) &btmrvl_sdio_sd8797 }, 180 /* Marvell SD8887 Bluetooth device */ 181 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x9136), 182 .driver_data = (unsigned long)&btmrvl_sdio_sd8887 }, 183 /* Marvell SD8897 Bluetooth device */ 184 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x912E), 185 .driver_data = (unsigned long) &btmrvl_sdio_sd8897 }, 186 187 { } /* Terminating entry */ 188 }; 189 190 MODULE_DEVICE_TABLE(sdio, btmrvl_sdio_ids); 191 192 static int btmrvl_sdio_get_rx_unit(struct btmrvl_sdio_card *card) 193 { 194 u8 reg; 195 int ret; 196 197 reg = sdio_readb(card->func, card->reg->card_rx_unit, &ret); 198 if (!ret) 199 card->rx_unit = reg; 200 201 return ret; 202 } 203 204 static int btmrvl_sdio_read_fw_status(struct btmrvl_sdio_card *card, u16 *dat) 205 { 206 u8 fws0, fws1; 207 int ret; 208 209 *dat = 0; 210 211 fws0 = sdio_readb(card->func, card->reg->card_fw_status0, &ret); 212 if (ret) 213 return -EIO; 214 215 fws1 = sdio_readb(card->func, card->reg->card_fw_status1, &ret); 216 if (ret) 217 return -EIO; 218 219 *dat = (((u16) fws1) << 8) | fws0; 220 221 return 0; 222 } 223 224 static int btmrvl_sdio_read_rx_len(struct btmrvl_sdio_card *card, u16 *dat) 225 { 226 u8 reg; 227 int ret; 228 229 reg = sdio_readb(card->func, card->reg->card_rx_len, &ret); 230 if (!ret) 231 *dat = (u16) reg << card->rx_unit; 232 233 return ret; 234 } 235 236 static int btmrvl_sdio_enable_host_int_mask(struct btmrvl_sdio_card *card, 237 u8 mask) 238 { 239 int ret; 240 241 sdio_writeb(card->func, mask, card->reg->host_int_mask, &ret); 242 if (ret) { 243 BT_ERR("Unable to enable the host interrupt!"); 244 ret = -EIO; 245 } 246 247 return ret; 248 } 249 250 static int btmrvl_sdio_disable_host_int_mask(struct btmrvl_sdio_card *card, 251 u8 mask) 252 { 253 u8 host_int_mask; 254 int ret; 255 256 host_int_mask = sdio_readb(card->func, card->reg->host_int_mask, &ret); 257 if (ret) 258 return -EIO; 259 260 host_int_mask &= ~mask; 261 262 sdio_writeb(card->func, host_int_mask, card->reg->host_int_mask, &ret); 263 if (ret < 0) { 264 BT_ERR("Unable to disable the host interrupt!"); 265 return -EIO; 266 } 267 268 return 0; 269 } 270 271 static int btmrvl_sdio_poll_card_status(struct btmrvl_sdio_card *card, u8 bits) 272 { 273 unsigned int tries; 274 u8 status; 275 int ret; 276 277 for (tries = 0; tries < MAX_POLL_TRIES * 1000; tries++) { 278 status = sdio_readb(card->func, card->reg->card_status, &ret); 279 if (ret) 280 goto failed; 281 if ((status & bits) == bits) 282 return ret; 283 284 udelay(1); 285 } 286 287 ret = -ETIMEDOUT; 288 289 failed: 290 BT_ERR("FAILED! ret=%d", ret); 291 292 return ret; 293 } 294 295 static int btmrvl_sdio_verify_fw_download(struct btmrvl_sdio_card *card, 296 int pollnum) 297 { 298 u16 firmwarestat; 299 int tries, ret; 300 301 /* Wait for firmware to become ready */ 302 for (tries = 0; tries < pollnum; tries++) { 303 sdio_claim_host(card->func); 304 ret = btmrvl_sdio_read_fw_status(card, &firmwarestat); 305 sdio_release_host(card->func); 306 if (ret < 0) 307 continue; 308 309 if (firmwarestat == FIRMWARE_READY) 310 return 0; 311 312 msleep(10); 313 } 314 315 return -ETIMEDOUT; 316 } 317 318 static int btmrvl_sdio_download_helper(struct btmrvl_sdio_card *card) 319 { 320 const struct firmware *fw_helper = NULL; 321 const u8 *helper = NULL; 322 int ret; 323 void *tmphlprbuf = NULL; 324 int tmphlprbufsz, hlprblknow, helperlen; 325 u8 *helperbuf; 326 u32 tx_len; 327 328 ret = request_firmware(&fw_helper, card->helper, 329 &card->func->dev); 330 if ((ret < 0) || !fw_helper) { 331 BT_ERR("request_firmware(helper) failed, error code = %d", 332 ret); 333 ret = -ENOENT; 334 goto done; 335 } 336 337 helper = fw_helper->data; 338 helperlen = fw_helper->size; 339 340 BT_DBG("Downloading helper image (%d bytes), block size %d bytes", 341 helperlen, SDIO_BLOCK_SIZE); 342 343 tmphlprbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN); 344 345 tmphlprbuf = kzalloc(tmphlprbufsz, GFP_KERNEL); 346 if (!tmphlprbuf) { 347 BT_ERR("Unable to allocate buffer for helper." 348 " Terminating download"); 349 ret = -ENOMEM; 350 goto done; 351 } 352 353 helperbuf = (u8 *) ALIGN_ADDR(tmphlprbuf, BTSDIO_DMA_ALIGN); 354 355 /* Perform helper data transfer */ 356 tx_len = (FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE) 357 - SDIO_HEADER_LEN; 358 hlprblknow = 0; 359 360 do { 361 ret = btmrvl_sdio_poll_card_status(card, 362 CARD_IO_READY | DN_LD_CARD_RDY); 363 if (ret < 0) { 364 BT_ERR("Helper download poll status timeout @ %d", 365 hlprblknow); 366 goto done; 367 } 368 369 /* Check if there is more data? */ 370 if (hlprblknow >= helperlen) 371 break; 372 373 if (helperlen - hlprblknow < tx_len) 374 tx_len = helperlen - hlprblknow; 375 376 /* Little-endian */ 377 helperbuf[0] = ((tx_len & 0x000000ff) >> 0); 378 helperbuf[1] = ((tx_len & 0x0000ff00) >> 8); 379 helperbuf[2] = ((tx_len & 0x00ff0000) >> 16); 380 helperbuf[3] = ((tx_len & 0xff000000) >> 24); 381 382 memcpy(&helperbuf[SDIO_HEADER_LEN], &helper[hlprblknow], 383 tx_len); 384 385 /* Now send the data */ 386 ret = sdio_writesb(card->func, card->ioport, helperbuf, 387 FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE); 388 if (ret < 0) { 389 BT_ERR("IO error during helper download @ %d", 390 hlprblknow); 391 goto done; 392 } 393 394 hlprblknow += tx_len; 395 } while (true); 396 397 BT_DBG("Transferring helper image EOF block"); 398 399 memset(helperbuf, 0x0, SDIO_BLOCK_SIZE); 400 401 ret = sdio_writesb(card->func, card->ioport, helperbuf, 402 SDIO_BLOCK_SIZE); 403 if (ret < 0) { 404 BT_ERR("IO error in writing helper image EOF block"); 405 goto done; 406 } 407 408 ret = 0; 409 410 done: 411 kfree(tmphlprbuf); 412 release_firmware(fw_helper); 413 return ret; 414 } 415 416 static int btmrvl_sdio_download_fw_w_helper(struct btmrvl_sdio_card *card) 417 { 418 const struct firmware *fw_firmware = NULL; 419 const u8 *firmware = NULL; 420 int firmwarelen, tmpfwbufsz, ret; 421 unsigned int tries, offset; 422 u8 base0, base1; 423 void *tmpfwbuf = NULL; 424 u8 *fwbuf; 425 u16 len, blksz_dl = card->sd_blksz_fw_dl; 426 int txlen = 0, tx_blocks = 0, count = 0; 427 428 ret = request_firmware(&fw_firmware, card->firmware, 429 &card->func->dev); 430 if ((ret < 0) || !fw_firmware) { 431 BT_ERR("request_firmware(firmware) failed, error code = %d", 432 ret); 433 ret = -ENOENT; 434 goto done; 435 } 436 437 firmware = fw_firmware->data; 438 firmwarelen = fw_firmware->size; 439 440 BT_DBG("Downloading FW image (%d bytes)", firmwarelen); 441 442 tmpfwbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN); 443 tmpfwbuf = kzalloc(tmpfwbufsz, GFP_KERNEL); 444 if (!tmpfwbuf) { 445 BT_ERR("Unable to allocate buffer for firmware." 446 " Terminating download"); 447 ret = -ENOMEM; 448 goto done; 449 } 450 451 /* Ensure aligned firmware buffer */ 452 fwbuf = (u8 *) ALIGN_ADDR(tmpfwbuf, BTSDIO_DMA_ALIGN); 453 454 /* Perform firmware data transfer */ 455 offset = 0; 456 do { 457 ret = btmrvl_sdio_poll_card_status(card, 458 CARD_IO_READY | DN_LD_CARD_RDY); 459 if (ret < 0) { 460 BT_ERR("FW download with helper poll status" 461 " timeout @ %d", offset); 462 goto done; 463 } 464 465 /* Check if there is more data ? */ 466 if (offset >= firmwarelen) 467 break; 468 469 for (tries = 0; tries < MAX_POLL_TRIES; tries++) { 470 base0 = sdio_readb(card->func, 471 card->reg->sq_read_base_addr_a0, &ret); 472 if (ret) { 473 BT_ERR("BASE0 register read failed:" 474 " base0 = 0x%04X(%d)." 475 " Terminating download", 476 base0, base0); 477 ret = -EIO; 478 goto done; 479 } 480 base1 = sdio_readb(card->func, 481 card->reg->sq_read_base_addr_a1, &ret); 482 if (ret) { 483 BT_ERR("BASE1 register read failed:" 484 " base1 = 0x%04X(%d)." 485 " Terminating download", 486 base1, base1); 487 ret = -EIO; 488 goto done; 489 } 490 491 len = (((u16) base1) << 8) | base0; 492 if (len) 493 break; 494 495 udelay(10); 496 } 497 498 if (!len) 499 break; 500 else if (len > BTM_UPLD_SIZE) { 501 BT_ERR("FW download failure @%d, invalid length %d", 502 offset, len); 503 ret = -EINVAL; 504 goto done; 505 } 506 507 txlen = len; 508 509 if (len & BIT(0)) { 510 count++; 511 if (count > MAX_WRITE_IOMEM_RETRY) { 512 BT_ERR("FW download failure @%d, " 513 "over max retry count", offset); 514 ret = -EIO; 515 goto done; 516 } 517 BT_ERR("FW CRC error indicated by the helper: " 518 "len = 0x%04X, txlen = %d", len, txlen); 519 len &= ~BIT(0); 520 /* Set txlen to 0 so as to resend from same offset */ 521 txlen = 0; 522 } else { 523 count = 0; 524 525 /* Last block ? */ 526 if (firmwarelen - offset < txlen) 527 txlen = firmwarelen - offset; 528 529 tx_blocks = DIV_ROUND_UP(txlen, blksz_dl); 530 531 memcpy(fwbuf, &firmware[offset], txlen); 532 } 533 534 ret = sdio_writesb(card->func, card->ioport, fwbuf, 535 tx_blocks * blksz_dl); 536 537 if (ret < 0) { 538 BT_ERR("FW download, writesb(%d) failed @%d", 539 count, offset); 540 sdio_writeb(card->func, HOST_CMD53_FIN, 541 card->reg->cfg, &ret); 542 if (ret) 543 BT_ERR("writeb failed (CFG)"); 544 } 545 546 offset += txlen; 547 } while (true); 548 549 BT_DBG("FW download over, size %d bytes", offset); 550 551 ret = 0; 552 553 done: 554 kfree(tmpfwbuf); 555 release_firmware(fw_firmware); 556 return ret; 557 } 558 559 static int btmrvl_sdio_card_to_host(struct btmrvl_private *priv) 560 { 561 u16 buf_len = 0; 562 int ret, num_blocks, blksz; 563 struct sk_buff *skb = NULL; 564 u32 type; 565 u8 *payload = NULL; 566 struct hci_dev *hdev = priv->btmrvl_dev.hcidev; 567 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card; 568 569 if (!card || !card->func) { 570 BT_ERR("card or function is NULL!"); 571 ret = -EINVAL; 572 goto exit; 573 } 574 575 /* Read the length of data to be transferred */ 576 ret = btmrvl_sdio_read_rx_len(card, &buf_len); 577 if (ret < 0) { 578 BT_ERR("read rx_len failed"); 579 ret = -EIO; 580 goto exit; 581 } 582 583 blksz = SDIO_BLOCK_SIZE; 584 num_blocks = DIV_ROUND_UP(buf_len, blksz); 585 586 if (buf_len <= SDIO_HEADER_LEN 587 || (num_blocks * blksz) > ALLOC_BUF_SIZE) { 588 BT_ERR("invalid packet length: %d", buf_len); 589 ret = -EINVAL; 590 goto exit; 591 } 592 593 /* Allocate buffer */ 594 skb = bt_skb_alloc(num_blocks * blksz + BTSDIO_DMA_ALIGN, GFP_ATOMIC); 595 if (skb == NULL) { 596 BT_ERR("No free skb"); 597 ret = -ENOMEM; 598 goto exit; 599 } 600 601 if ((unsigned long) skb->data & (BTSDIO_DMA_ALIGN - 1)) { 602 skb_put(skb, (unsigned long) skb->data & 603 (BTSDIO_DMA_ALIGN - 1)); 604 skb_pull(skb, (unsigned long) skb->data & 605 (BTSDIO_DMA_ALIGN - 1)); 606 } 607 608 payload = skb->data; 609 610 ret = sdio_readsb(card->func, payload, card->ioport, 611 num_blocks * blksz); 612 if (ret < 0) { 613 BT_ERR("readsb failed: %d", ret); 614 ret = -EIO; 615 goto exit; 616 } 617 618 /* This is SDIO specific header length: byte[2][1][0], type: byte[3] 619 * (HCI_COMMAND = 1, ACL_DATA = 2, SCO_DATA = 3, 0xFE = Vendor) 620 */ 621 622 buf_len = payload[0]; 623 buf_len |= payload[1] << 8; 624 buf_len |= payload[2] << 16; 625 626 if (buf_len > blksz * num_blocks) { 627 BT_ERR("Skip incorrect packet: hdrlen %d buffer %d", 628 buf_len, blksz * num_blocks); 629 ret = -EIO; 630 goto exit; 631 } 632 633 type = payload[3]; 634 635 switch (type) { 636 case HCI_ACLDATA_PKT: 637 case HCI_SCODATA_PKT: 638 case HCI_EVENT_PKT: 639 bt_cb(skb)->pkt_type = type; 640 skb_put(skb, buf_len); 641 skb_pull(skb, SDIO_HEADER_LEN); 642 643 if (type == HCI_EVENT_PKT) { 644 if (btmrvl_check_evtpkt(priv, skb)) 645 hci_recv_frame(hdev, skb); 646 } else { 647 hci_recv_frame(hdev, skb); 648 } 649 650 hdev->stat.byte_rx += buf_len; 651 break; 652 653 case MRVL_VENDOR_PKT: 654 bt_cb(skb)->pkt_type = HCI_VENDOR_PKT; 655 skb_put(skb, buf_len); 656 skb_pull(skb, SDIO_HEADER_LEN); 657 658 if (btmrvl_process_event(priv, skb)) 659 hci_recv_frame(hdev, skb); 660 661 hdev->stat.byte_rx += buf_len; 662 break; 663 664 default: 665 BT_ERR("Unknown packet type:%d", type); 666 BT_ERR("hex: %*ph", blksz * num_blocks, payload); 667 668 kfree_skb(skb); 669 skb = NULL; 670 break; 671 } 672 673 exit: 674 if (ret) { 675 hdev->stat.err_rx++; 676 kfree_skb(skb); 677 } 678 679 return ret; 680 } 681 682 static int btmrvl_sdio_process_int_status(struct btmrvl_private *priv) 683 { 684 ulong flags; 685 u8 ireg; 686 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card; 687 688 spin_lock_irqsave(&priv->driver_lock, flags); 689 ireg = sdio_ireg; 690 sdio_ireg = 0; 691 spin_unlock_irqrestore(&priv->driver_lock, flags); 692 693 sdio_claim_host(card->func); 694 if (ireg & DN_LD_HOST_INT_STATUS) { 695 if (priv->btmrvl_dev.tx_dnld_rdy) 696 BT_DBG("tx_done already received: " 697 " int_status=0x%x", ireg); 698 else 699 priv->btmrvl_dev.tx_dnld_rdy = true; 700 } 701 702 if (ireg & UP_LD_HOST_INT_STATUS) 703 btmrvl_sdio_card_to_host(priv); 704 705 sdio_release_host(card->func); 706 707 return 0; 708 } 709 710 static int btmrvl_sdio_read_to_clear(struct btmrvl_sdio_card *card, u8 *ireg) 711 { 712 struct btmrvl_adapter *adapter = card->priv->adapter; 713 int ret; 714 715 ret = sdio_readsb(card->func, adapter->hw_regs, 0, SDIO_BLOCK_SIZE); 716 if (ret) { 717 BT_ERR("sdio_readsb: read int hw_regs failed: %d", ret); 718 return ret; 719 } 720 721 *ireg = adapter->hw_regs[card->reg->host_intstatus]; 722 BT_DBG("hw_regs[%#x]=%#x", card->reg->host_intstatus, *ireg); 723 724 return 0; 725 } 726 727 static int btmrvl_sdio_write_to_clear(struct btmrvl_sdio_card *card, u8 *ireg) 728 { 729 int ret; 730 731 *ireg = sdio_readb(card->func, card->reg->host_intstatus, &ret); 732 if (ret) { 733 BT_ERR("sdio_readb: read int status failed: %d", ret); 734 return ret; 735 } 736 737 if (*ireg) { 738 /* 739 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS 740 * Clear the interrupt status register and re-enable the 741 * interrupt. 742 */ 743 BT_DBG("int_status = 0x%x", *ireg); 744 745 sdio_writeb(card->func, ~(*ireg) & (DN_LD_HOST_INT_STATUS | 746 UP_LD_HOST_INT_STATUS), 747 card->reg->host_intstatus, &ret); 748 if (ret) { 749 BT_ERR("sdio_writeb: clear int status failed: %d", ret); 750 return ret; 751 } 752 } 753 754 return 0; 755 } 756 757 static void btmrvl_sdio_interrupt(struct sdio_func *func) 758 { 759 struct btmrvl_private *priv; 760 struct btmrvl_sdio_card *card; 761 ulong flags; 762 u8 ireg = 0; 763 int ret; 764 765 card = sdio_get_drvdata(func); 766 if (!card || !card->priv) { 767 BT_ERR("sbi_interrupt(%p) card or priv is " 768 "NULL, card=%p\n", func, card); 769 return; 770 } 771 772 priv = card->priv; 773 774 if (card->reg->int_read_to_clear) 775 ret = btmrvl_sdio_read_to_clear(card, &ireg); 776 else 777 ret = btmrvl_sdio_write_to_clear(card, &ireg); 778 779 if (ret) 780 return; 781 782 spin_lock_irqsave(&priv->driver_lock, flags); 783 sdio_ireg |= ireg; 784 spin_unlock_irqrestore(&priv->driver_lock, flags); 785 786 btmrvl_interrupt(priv); 787 } 788 789 static int btmrvl_sdio_register_dev(struct btmrvl_sdio_card *card) 790 { 791 struct sdio_func *func; 792 u8 reg; 793 int ret = 0; 794 795 if (!card || !card->func) { 796 BT_ERR("Error: card or function is NULL!"); 797 ret = -EINVAL; 798 goto failed; 799 } 800 801 func = card->func; 802 803 sdio_claim_host(func); 804 805 ret = sdio_enable_func(func); 806 if (ret) { 807 BT_ERR("sdio_enable_func() failed: ret=%d", ret); 808 ret = -EIO; 809 goto release_host; 810 } 811 812 ret = sdio_claim_irq(func, btmrvl_sdio_interrupt); 813 if (ret) { 814 BT_ERR("sdio_claim_irq failed: ret=%d", ret); 815 ret = -EIO; 816 goto disable_func; 817 } 818 819 ret = sdio_set_block_size(card->func, SDIO_BLOCK_SIZE); 820 if (ret) { 821 BT_ERR("cannot set SDIO block size"); 822 ret = -EIO; 823 goto release_irq; 824 } 825 826 reg = sdio_readb(func, card->reg->io_port_0, &ret); 827 if (ret < 0) { 828 ret = -EIO; 829 goto release_irq; 830 } 831 832 card->ioport = reg; 833 834 reg = sdio_readb(func, card->reg->io_port_1, &ret); 835 if (ret < 0) { 836 ret = -EIO; 837 goto release_irq; 838 } 839 840 card->ioport |= (reg << 8); 841 842 reg = sdio_readb(func, card->reg->io_port_2, &ret); 843 if (ret < 0) { 844 ret = -EIO; 845 goto release_irq; 846 } 847 848 card->ioport |= (reg << 16); 849 850 BT_DBG("SDIO FUNC%d IO port: 0x%x", func->num, card->ioport); 851 852 if (card->reg->int_read_to_clear) { 853 reg = sdio_readb(func, card->reg->host_int_rsr, &ret); 854 if (ret < 0) { 855 ret = -EIO; 856 goto release_irq; 857 } 858 sdio_writeb(func, reg | 0x3f, card->reg->host_int_rsr, &ret); 859 if (ret < 0) { 860 ret = -EIO; 861 goto release_irq; 862 } 863 864 reg = sdio_readb(func, card->reg->card_misc_cfg, &ret); 865 if (ret < 0) { 866 ret = -EIO; 867 goto release_irq; 868 } 869 sdio_writeb(func, reg | 0x10, card->reg->card_misc_cfg, &ret); 870 if (ret < 0) { 871 ret = -EIO; 872 goto release_irq; 873 } 874 } 875 876 sdio_set_drvdata(func, card); 877 878 sdio_release_host(func); 879 880 return 0; 881 882 release_irq: 883 sdio_release_irq(func); 884 885 disable_func: 886 sdio_disable_func(func); 887 888 release_host: 889 sdio_release_host(func); 890 891 failed: 892 return ret; 893 } 894 895 static int btmrvl_sdio_unregister_dev(struct btmrvl_sdio_card *card) 896 { 897 if (card && card->func) { 898 sdio_claim_host(card->func); 899 sdio_release_irq(card->func); 900 sdio_disable_func(card->func); 901 sdio_release_host(card->func); 902 sdio_set_drvdata(card->func, NULL); 903 } 904 905 return 0; 906 } 907 908 static int btmrvl_sdio_enable_host_int(struct btmrvl_sdio_card *card) 909 { 910 int ret; 911 912 if (!card || !card->func) 913 return -EINVAL; 914 915 sdio_claim_host(card->func); 916 917 ret = btmrvl_sdio_enable_host_int_mask(card, HIM_ENABLE); 918 919 btmrvl_sdio_get_rx_unit(card); 920 921 sdio_release_host(card->func); 922 923 return ret; 924 } 925 926 static int btmrvl_sdio_disable_host_int(struct btmrvl_sdio_card *card) 927 { 928 int ret; 929 930 if (!card || !card->func) 931 return -EINVAL; 932 933 sdio_claim_host(card->func); 934 935 ret = btmrvl_sdio_disable_host_int_mask(card, HIM_DISABLE); 936 937 sdio_release_host(card->func); 938 939 return ret; 940 } 941 942 static int btmrvl_sdio_host_to_card(struct btmrvl_private *priv, 943 u8 *payload, u16 nb) 944 { 945 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card; 946 int ret = 0; 947 int buf_block_len; 948 int blksz; 949 int i = 0; 950 u8 *buf = NULL; 951 void *tmpbuf = NULL; 952 int tmpbufsz; 953 954 if (!card || !card->func) { 955 BT_ERR("card or function is NULL!"); 956 return -EINVAL; 957 } 958 959 buf = payload; 960 if ((unsigned long) payload & (BTSDIO_DMA_ALIGN - 1)) { 961 tmpbufsz = ALIGN_SZ(nb, BTSDIO_DMA_ALIGN); 962 tmpbuf = kzalloc(tmpbufsz, GFP_KERNEL); 963 if (!tmpbuf) 964 return -ENOMEM; 965 buf = (u8 *) ALIGN_ADDR(tmpbuf, BTSDIO_DMA_ALIGN); 966 memcpy(buf, payload, nb); 967 } 968 969 blksz = SDIO_BLOCK_SIZE; 970 buf_block_len = DIV_ROUND_UP(nb, blksz); 971 972 sdio_claim_host(card->func); 973 974 do { 975 /* Transfer data to card */ 976 ret = sdio_writesb(card->func, card->ioport, buf, 977 buf_block_len * blksz); 978 if (ret < 0) { 979 i++; 980 BT_ERR("i=%d writesb failed: %d", i, ret); 981 BT_ERR("hex: %*ph", nb, payload); 982 ret = -EIO; 983 if (i > MAX_WRITE_IOMEM_RETRY) 984 goto exit; 985 } 986 } while (ret); 987 988 priv->btmrvl_dev.tx_dnld_rdy = false; 989 990 exit: 991 sdio_release_host(card->func); 992 kfree(tmpbuf); 993 994 return ret; 995 } 996 997 static int btmrvl_sdio_download_fw(struct btmrvl_sdio_card *card) 998 { 999 int ret; 1000 u8 fws0; 1001 int pollnum = MAX_POLL_TRIES; 1002 1003 if (!card || !card->func) { 1004 BT_ERR("card or function is NULL!"); 1005 return -EINVAL; 1006 } 1007 1008 if (!btmrvl_sdio_verify_fw_download(card, 1)) { 1009 BT_DBG("Firmware already downloaded!"); 1010 return 0; 1011 } 1012 1013 sdio_claim_host(card->func); 1014 1015 /* Check if other function driver is downloading the firmware */ 1016 fws0 = sdio_readb(card->func, card->reg->card_fw_status0, &ret); 1017 if (ret) { 1018 BT_ERR("Failed to read FW downloading status!"); 1019 ret = -EIO; 1020 goto done; 1021 } 1022 if (fws0) { 1023 BT_DBG("BT not the winner (%#x). Skip FW downloading", fws0); 1024 1025 /* Give other function more time to download the firmware */ 1026 pollnum *= 10; 1027 } else { 1028 if (card->helper) { 1029 ret = btmrvl_sdio_download_helper(card); 1030 if (ret) { 1031 BT_ERR("Failed to download helper!"); 1032 ret = -EIO; 1033 goto done; 1034 } 1035 } 1036 1037 if (btmrvl_sdio_download_fw_w_helper(card)) { 1038 BT_ERR("Failed to download firmware!"); 1039 ret = -EIO; 1040 goto done; 1041 } 1042 } 1043 1044 sdio_release_host(card->func); 1045 1046 /* 1047 * winner or not, with this test the FW synchronizes when the 1048 * module can continue its initialization 1049 */ 1050 if (btmrvl_sdio_verify_fw_download(card, pollnum)) { 1051 BT_ERR("FW failed to be active in time!"); 1052 return -ETIMEDOUT; 1053 } 1054 1055 return 0; 1056 1057 done: 1058 sdio_release_host(card->func); 1059 return ret; 1060 } 1061 1062 static int btmrvl_sdio_wakeup_fw(struct btmrvl_private *priv) 1063 { 1064 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card; 1065 int ret = 0; 1066 1067 if (!card || !card->func) { 1068 BT_ERR("card or function is NULL!"); 1069 return -EINVAL; 1070 } 1071 1072 sdio_claim_host(card->func); 1073 1074 sdio_writeb(card->func, HOST_POWER_UP, card->reg->cfg, &ret); 1075 1076 sdio_release_host(card->func); 1077 1078 BT_DBG("wake up firmware"); 1079 1080 return ret; 1081 } 1082 1083 static int btmrvl_sdio_probe(struct sdio_func *func, 1084 const struct sdio_device_id *id) 1085 { 1086 int ret = 0; 1087 struct btmrvl_private *priv = NULL; 1088 struct btmrvl_sdio_card *card = NULL; 1089 1090 BT_INFO("vendor=0x%x, device=0x%x, class=%d, fn=%d", 1091 id->vendor, id->device, id->class, func->num); 1092 1093 card = devm_kzalloc(&func->dev, sizeof(*card), GFP_KERNEL); 1094 if (!card) 1095 return -ENOMEM; 1096 1097 card->func = func; 1098 1099 if (id->driver_data) { 1100 struct btmrvl_sdio_device *data = (void *) id->driver_data; 1101 card->helper = data->helper; 1102 card->firmware = data->firmware; 1103 card->reg = data->reg; 1104 card->sd_blksz_fw_dl = data->sd_blksz_fw_dl; 1105 card->support_pscan_win_report = data->support_pscan_win_report; 1106 } 1107 1108 if (btmrvl_sdio_register_dev(card) < 0) { 1109 BT_ERR("Failed to register BT device!"); 1110 return -ENODEV; 1111 } 1112 1113 /* Disable the interrupts on the card */ 1114 btmrvl_sdio_disable_host_int(card); 1115 1116 if (btmrvl_sdio_download_fw(card)) { 1117 BT_ERR("Downloading firmware failed!"); 1118 ret = -ENODEV; 1119 goto unreg_dev; 1120 } 1121 1122 btmrvl_sdio_enable_host_int(card); 1123 1124 priv = btmrvl_add_card(card); 1125 if (!priv) { 1126 BT_ERR("Initializing card failed!"); 1127 ret = -ENODEV; 1128 goto disable_host_int; 1129 } 1130 1131 card->priv = priv; 1132 1133 /* Initialize the interface specific function pointers */ 1134 priv->hw_host_to_card = btmrvl_sdio_host_to_card; 1135 priv->hw_wakeup_firmware = btmrvl_sdio_wakeup_fw; 1136 priv->hw_process_int_status = btmrvl_sdio_process_int_status; 1137 1138 if (btmrvl_register_hdev(priv)) { 1139 BT_ERR("Register hdev failed!"); 1140 ret = -ENODEV; 1141 goto disable_host_int; 1142 } 1143 1144 return 0; 1145 1146 disable_host_int: 1147 btmrvl_sdio_disable_host_int(card); 1148 unreg_dev: 1149 btmrvl_sdio_unregister_dev(card); 1150 return ret; 1151 } 1152 1153 static void btmrvl_sdio_remove(struct sdio_func *func) 1154 { 1155 struct btmrvl_sdio_card *card; 1156 1157 if (func) { 1158 card = sdio_get_drvdata(func); 1159 if (card) { 1160 /* Send SHUTDOWN command & disable interrupt 1161 * if user removes the module. 1162 */ 1163 if (user_rmmod) { 1164 btmrvl_send_module_cfg_cmd(card->priv, 1165 MODULE_SHUTDOWN_REQ); 1166 btmrvl_sdio_disable_host_int(card); 1167 } 1168 BT_DBG("unregester dev"); 1169 btmrvl_sdio_unregister_dev(card); 1170 btmrvl_remove_card(card->priv); 1171 } 1172 } 1173 } 1174 1175 static int btmrvl_sdio_suspend(struct device *dev) 1176 { 1177 struct sdio_func *func = dev_to_sdio_func(dev); 1178 struct btmrvl_sdio_card *card; 1179 struct btmrvl_private *priv; 1180 mmc_pm_flag_t pm_flags; 1181 struct hci_dev *hcidev; 1182 1183 if (func) { 1184 pm_flags = sdio_get_host_pm_caps(func); 1185 BT_DBG("%s: suspend: PM flags = 0x%x", sdio_func_id(func), 1186 pm_flags); 1187 if (!(pm_flags & MMC_PM_KEEP_POWER)) { 1188 BT_ERR("%s: cannot remain alive while suspended", 1189 sdio_func_id(func)); 1190 return -ENOSYS; 1191 } 1192 card = sdio_get_drvdata(func); 1193 if (!card || !card->priv) { 1194 BT_ERR("card or priv structure is not valid"); 1195 return 0; 1196 } 1197 } else { 1198 BT_ERR("sdio_func is not specified"); 1199 return 0; 1200 } 1201 1202 priv = card->priv; 1203 hcidev = priv->btmrvl_dev.hcidev; 1204 BT_DBG("%s: SDIO suspend", hcidev->name); 1205 hci_suspend_dev(hcidev); 1206 skb_queue_purge(&priv->adapter->tx_queue); 1207 1208 if (priv->adapter->hs_state != HS_ACTIVATED) { 1209 if (btmrvl_enable_hs(priv)) { 1210 BT_ERR("HS not actived, suspend failed!"); 1211 return -EBUSY; 1212 } 1213 } 1214 1215 priv->adapter->is_suspended = true; 1216 1217 /* We will keep the power when hs enabled successfully */ 1218 if (priv->adapter->hs_state == HS_ACTIVATED) { 1219 BT_DBG("suspend with MMC_PM_KEEP_POWER"); 1220 return sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER); 1221 } else { 1222 BT_DBG("suspend without MMC_PM_KEEP_POWER"); 1223 return 0; 1224 } 1225 } 1226 1227 static int btmrvl_sdio_resume(struct device *dev) 1228 { 1229 struct sdio_func *func = dev_to_sdio_func(dev); 1230 struct btmrvl_sdio_card *card; 1231 struct btmrvl_private *priv; 1232 mmc_pm_flag_t pm_flags; 1233 struct hci_dev *hcidev; 1234 1235 if (func) { 1236 pm_flags = sdio_get_host_pm_caps(func); 1237 BT_DBG("%s: resume: PM flags = 0x%x", sdio_func_id(func), 1238 pm_flags); 1239 card = sdio_get_drvdata(func); 1240 if (!card || !card->priv) { 1241 BT_ERR("card or priv structure is not valid"); 1242 return 0; 1243 } 1244 } else { 1245 BT_ERR("sdio_func is not specified"); 1246 return 0; 1247 } 1248 priv = card->priv; 1249 1250 if (!priv->adapter->is_suspended) { 1251 BT_DBG("device already resumed"); 1252 return 0; 1253 } 1254 1255 priv->hw_wakeup_firmware(priv); 1256 priv->adapter->hs_state = HS_DEACTIVATED; 1257 hcidev = priv->btmrvl_dev.hcidev; 1258 BT_DBG("%s: HS DEACTIVATED in resume!", hcidev->name); 1259 priv->adapter->is_suspended = false; 1260 BT_DBG("%s: SDIO resume", hcidev->name); 1261 hci_resume_dev(hcidev); 1262 1263 return 0; 1264 } 1265 1266 static const struct dev_pm_ops btmrvl_sdio_pm_ops = { 1267 .suspend = btmrvl_sdio_suspend, 1268 .resume = btmrvl_sdio_resume, 1269 }; 1270 1271 static struct sdio_driver bt_mrvl_sdio = { 1272 .name = "btmrvl_sdio", 1273 .id_table = btmrvl_sdio_ids, 1274 .probe = btmrvl_sdio_probe, 1275 .remove = btmrvl_sdio_remove, 1276 .drv = { 1277 .owner = THIS_MODULE, 1278 .pm = &btmrvl_sdio_pm_ops, 1279 } 1280 }; 1281 1282 static int __init btmrvl_sdio_init_module(void) 1283 { 1284 if (sdio_register_driver(&bt_mrvl_sdio) != 0) { 1285 BT_ERR("SDIO Driver Registration Failed"); 1286 return -ENODEV; 1287 } 1288 1289 /* Clear the flag in case user removes the card. */ 1290 user_rmmod = 0; 1291 1292 return 0; 1293 } 1294 1295 static void __exit btmrvl_sdio_exit_module(void) 1296 { 1297 /* Set the flag as user is removing this module. */ 1298 user_rmmod = 1; 1299 1300 sdio_unregister_driver(&bt_mrvl_sdio); 1301 } 1302 1303 module_init(btmrvl_sdio_init_module); 1304 module_exit(btmrvl_sdio_exit_module); 1305 1306 MODULE_AUTHOR("Marvell International Ltd."); 1307 MODULE_DESCRIPTION("Marvell BT-over-SDIO driver ver " VERSION); 1308 MODULE_VERSION(VERSION); 1309 MODULE_LICENSE("GPL v2"); 1310 MODULE_FIRMWARE("mrvl/sd8688_helper.bin"); 1311 MODULE_FIRMWARE("mrvl/sd8688.bin"); 1312 MODULE_FIRMWARE("mrvl/sd8787_uapsta.bin"); 1313 MODULE_FIRMWARE("mrvl/sd8797_uapsta.bin"); 1314 MODULE_FIRMWARE("mrvl/sd8887_uapsta.bin"); 1315 MODULE_FIRMWARE("mrvl/sd8897_uapsta.bin"); 1316