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