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