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