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