linux/drivers/net/wireless/marvell/libertas/if_spi.c
<<
>>
Prefs
   1/*
   2 *      linux/drivers/net/wireless/libertas/if_spi.c
   3 *
   4 *      Driver for Marvell SPI WLAN cards.
   5 *
   6 *      Copyright 2008 Analog Devices Inc.
   7 *
   8 *      Authors:
   9 *      Andrey Yurovsky <andrey@cozybit.com>
  10 *      Colin McCabe <colin@cozybit.com>
  11 *
  12 *      Inspired by if_sdio.c, Copyright 2007-2008 Pierre Ossman
  13 *
  14 * This program is free software; you can redistribute it and/or modify
  15 * it under the terms of the GNU General Public License as published by
  16 * the Free Software Foundation; either version 2 of the License, or
  17 * (at your option) any later version.
  18 */
  19
  20#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  21
  22#include <linux/hardirq.h>
  23#include <linux/interrupt.h>
  24#include <linux/module.h>
  25#include <linux/firmware.h>
  26#include <linux/jiffies.h>
  27#include <linux/list.h>
  28#include <linux/netdevice.h>
  29#include <linux/slab.h>
  30#include <linux/spi/libertas_spi.h>
  31#include <linux/spi/spi.h>
  32
  33#include "host.h"
  34#include "decl.h"
  35#include "defs.h"
  36#include "dev.h"
  37#include "if_spi.h"
  38
  39struct if_spi_packet {
  40        struct list_head                list;
  41        u16                             blen;
  42        u8                              buffer[0] __attribute__((aligned(4)));
  43};
  44
  45struct if_spi_card {
  46        struct spi_device               *spi;
  47        struct lbs_private              *priv;
  48        struct libertas_spi_platform_data *pdata;
  49
  50        /* The card ID and card revision, as reported by the hardware. */
  51        u16                             card_id;
  52        u8                              card_rev;
  53
  54        /* The last time that we initiated an SPU operation */
  55        unsigned long                   prev_xfer_time;
  56
  57        int                             use_dummy_writes;
  58        unsigned long                   spu_port_delay;
  59        unsigned long                   spu_reg_delay;
  60
  61        /* Handles all SPI communication (except for FW load) */
  62        struct workqueue_struct         *workqueue;
  63        struct work_struct              packet_work;
  64        struct work_struct              resume_work;
  65
  66        u8                              cmd_buffer[IF_SPI_CMD_BUF_SIZE];
  67
  68        /* A buffer of incoming packets from libertas core.
  69         * Since we can't sleep in hw_host_to_card, we have to buffer
  70         * them. */
  71        struct list_head                cmd_packet_list;
  72        struct list_head                data_packet_list;
  73
  74        /* Protects cmd_packet_list and data_packet_list */
  75        spinlock_t                      buffer_lock;
  76
  77        /* True is card suspended */
  78        u8                              suspended;
  79};
  80
  81static void free_if_spi_card(struct if_spi_card *card)
  82{
  83        struct list_head *cursor, *next;
  84        struct if_spi_packet *packet;
  85
  86        list_for_each_safe(cursor, next, &card->cmd_packet_list) {
  87                packet = container_of(cursor, struct if_spi_packet, list);
  88                list_del(&packet->list);
  89                kfree(packet);
  90        }
  91        list_for_each_safe(cursor, next, &card->data_packet_list) {
  92                packet = container_of(cursor, struct if_spi_packet, list);
  93                list_del(&packet->list);
  94                kfree(packet);
  95        }
  96        kfree(card);
  97}
  98
  99#define MODEL_8385      0x04
 100#define MODEL_8686      0x0b
 101#define MODEL_8688      0x10
 102
 103static const struct lbs_fw_table fw_table[] = {
 104        { MODEL_8385, "libertas/gspi8385_helper.bin", "libertas/gspi8385.bin" },
 105        { MODEL_8385, "libertas/gspi8385_hlp.bin", "libertas/gspi8385.bin" },
 106        { MODEL_8686, "libertas/gspi8686_v9_helper.bin", "libertas/gspi8686_v9.bin" },
 107        { MODEL_8686, "libertas/gspi8686_hlp.bin", "libertas/gspi8686.bin" },
 108        { MODEL_8688, "libertas/gspi8688_helper.bin", "libertas/gspi8688.bin" },
 109        { 0, NULL, NULL }
 110};
 111MODULE_FIRMWARE("libertas/gspi8385_helper.bin");
 112MODULE_FIRMWARE("libertas/gspi8385_hlp.bin");
 113MODULE_FIRMWARE("libertas/gspi8385.bin");
 114MODULE_FIRMWARE("libertas/gspi8686_v9_helper.bin");
 115MODULE_FIRMWARE("libertas/gspi8686_v9.bin");
 116MODULE_FIRMWARE("libertas/gspi8686_hlp.bin");
 117MODULE_FIRMWARE("libertas/gspi8686.bin");
 118MODULE_FIRMWARE("libertas/gspi8688_helper.bin");
 119MODULE_FIRMWARE("libertas/gspi8688.bin");
 120
 121
 122/*
 123 * SPI Interface Unit Routines
 124 *
 125 * The SPU sits between the host and the WLAN module.
 126 * All communication with the firmware is through SPU transactions.
 127 *
 128 * First we have to put a SPU register name on the bus. Then we can
 129 * either read from or write to that register.
 130 *
 131 */
 132
 133static void spu_transaction_init(struct if_spi_card *card)
 134{
 135        if (!time_after(jiffies, card->prev_xfer_time + 1)) {
 136                /* Unfortunately, the SPU requires a delay between successive
 137                 * transactions. If our last transaction was more than a jiffy
 138                 * ago, we have obviously already delayed enough.
 139                 * If not, we have to busy-wait to be on the safe side. */
 140                ndelay(400);
 141        }
 142}
 143
 144static void spu_transaction_finish(struct if_spi_card *card)
 145{
 146        card->prev_xfer_time = jiffies;
 147}
 148
 149/*
 150 * Write out a byte buffer to an SPI register,
 151 * using a series of 16-bit transfers.
 152 */
 153static int spu_write(struct if_spi_card *card, u16 reg, const u8 *buf, int len)
 154{
 155        int err = 0;
 156        __le16 reg_out = cpu_to_le16(reg | IF_SPI_WRITE_OPERATION_MASK);
 157        struct spi_message m;
 158        struct spi_transfer reg_trans;
 159        struct spi_transfer data_trans;
 160
 161        spi_message_init(&m);
 162        memset(&reg_trans, 0, sizeof(reg_trans));
 163        memset(&data_trans, 0, sizeof(data_trans));
 164
 165        /* You must give an even number of bytes to the SPU, even if it
 166         * doesn't care about the last one.  */
 167        BUG_ON(len & 0x1);
 168
 169        spu_transaction_init(card);
 170
 171        /* write SPU register index */
 172        reg_trans.tx_buf = &reg_out;
 173        reg_trans.len = sizeof(reg_out);
 174
 175        data_trans.tx_buf = buf;
 176        data_trans.len = len;
 177
 178        spi_message_add_tail(&reg_trans, &m);
 179        spi_message_add_tail(&data_trans, &m);
 180
 181        err = spi_sync(card->spi, &m);
 182        spu_transaction_finish(card);
 183        return err;
 184}
 185
 186static inline int spu_write_u16(struct if_spi_card *card, u16 reg, u16 val)
 187{
 188        __le16 buff;
 189
 190        buff = cpu_to_le16(val);
 191        return spu_write(card, reg, (u8 *)&buff, sizeof(u16));
 192}
 193
 194static inline int spu_reg_is_port_reg(u16 reg)
 195{
 196        switch (reg) {
 197        case IF_SPI_IO_RDWRPORT_REG:
 198        case IF_SPI_CMD_RDWRPORT_REG:
 199        case IF_SPI_DATA_RDWRPORT_REG:
 200                return 1;
 201        default:
 202                return 0;
 203        }
 204}
 205
 206static int spu_read(struct if_spi_card *card, u16 reg, u8 *buf, int len)
 207{
 208        unsigned int delay;
 209        int err = 0;
 210        __le16 reg_out = cpu_to_le16(reg | IF_SPI_READ_OPERATION_MASK);
 211        struct spi_message m;
 212        struct spi_transfer reg_trans;
 213        struct spi_transfer dummy_trans;
 214        struct spi_transfer data_trans;
 215
 216        /*
 217         * You must take an even number of bytes from the SPU, even if you
 218         * don't care about the last one.
 219         */
 220        BUG_ON(len & 0x1);
 221
 222        spu_transaction_init(card);
 223
 224        spi_message_init(&m);
 225        memset(&reg_trans, 0, sizeof(reg_trans));
 226        memset(&dummy_trans, 0, sizeof(dummy_trans));
 227        memset(&data_trans, 0, sizeof(data_trans));
 228
 229        /* write SPU register index */
 230        reg_trans.tx_buf = &reg_out;
 231        reg_trans.len = sizeof(reg_out);
 232        spi_message_add_tail(&reg_trans, &m);
 233
 234        delay = spu_reg_is_port_reg(reg) ? card->spu_port_delay :
 235                                                card->spu_reg_delay;
 236        if (card->use_dummy_writes) {
 237                /* Clock in dummy cycles while the SPU fills the FIFO */
 238                dummy_trans.len = delay / 8;
 239                spi_message_add_tail(&dummy_trans, &m);
 240        } else {
 241                /* Busy-wait while the SPU fills the FIFO */
 242                reg_trans.delay_usecs =
 243                        DIV_ROUND_UP((100 + (delay * 10)), 1000);
 244        }
 245
 246        /* read in data */
 247        data_trans.rx_buf = buf;
 248        data_trans.len = len;
 249        spi_message_add_tail(&data_trans, &m);
 250
 251        err = spi_sync(card->spi, &m);
 252        spu_transaction_finish(card);
 253        return err;
 254}
 255
 256/* Read 16 bits from an SPI register */
 257static inline int spu_read_u16(struct if_spi_card *card, u16 reg, u16 *val)
 258{
 259        __le16 buf;
 260        int ret;
 261
 262        ret = spu_read(card, reg, (u8 *)&buf, sizeof(buf));
 263        if (ret == 0)
 264                *val = le16_to_cpup(&buf);
 265        return ret;
 266}
 267
 268/*
 269 * Read 32 bits from an SPI register.
 270 * The low 16 bits are read first.
 271 */
 272static int spu_read_u32(struct if_spi_card *card, u16 reg, u32 *val)
 273{
 274        __le32 buf;
 275        int err;
 276
 277        err = spu_read(card, reg, (u8 *)&buf, sizeof(buf));
 278        if (!err)
 279                *val = le32_to_cpup(&buf);
 280        return err;
 281}
 282
 283/*
 284 * Keep reading 16 bits from an SPI register until you get the correct result.
 285 *
 286 * If mask = 0, the correct result is any non-zero number.
 287 * If mask != 0, the correct result is any number where
 288 * number & target_mask == target
 289 *
 290 * Returns -ETIMEDOUT if a second passes without the correct result.
 291 */
 292static int spu_wait_for_u16(struct if_spi_card *card, u16 reg,
 293                        u16 target_mask, u16 target)
 294{
 295        int err;
 296        unsigned long timeout = jiffies + 5*HZ;
 297        while (1) {
 298                u16 val;
 299                err = spu_read_u16(card, reg, &val);
 300                if (err)
 301                        return err;
 302                if (target_mask) {
 303                        if ((val & target_mask) == target)
 304                                return 0;
 305                } else {
 306                        if (val)
 307                                return 0;
 308                }
 309                udelay(100);
 310                if (time_after(jiffies, timeout)) {
 311                        pr_err("%s: timeout with val=%02x, target_mask=%02x, target=%02x\n",
 312                               __func__, val, target_mask, target);
 313                        return -ETIMEDOUT;
 314                }
 315        }
 316}
 317
 318/*
 319 * Read 16 bits from an SPI register until you receive a specific value.
 320 * Returns -ETIMEDOUT if a 4 tries pass without success.
 321 */
 322static int spu_wait_for_u32(struct if_spi_card *card, u32 reg, u32 target)
 323{
 324        int err, try;
 325        for (try = 0; try < 4; ++try) {
 326                u32 val = 0;
 327                err = spu_read_u32(card, reg, &val);
 328                if (err)
 329                        return err;
 330                if (val == target)
 331                        return 0;
 332                mdelay(100);
 333        }
 334        return -ETIMEDOUT;
 335}
 336
 337static int spu_set_interrupt_mode(struct if_spi_card *card,
 338                           int suppress_host_int,
 339                           int auto_int)
 340{
 341        int err = 0;
 342
 343        /*
 344         * We can suppress a host interrupt by clearing the appropriate
 345         * bit in the "host interrupt status mask" register
 346         */
 347        if (suppress_host_int) {
 348                err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG, 0);
 349                if (err)
 350                        return err;
 351        } else {
 352                err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG,
 353                              IF_SPI_HISM_TX_DOWNLOAD_RDY |
 354                              IF_SPI_HISM_RX_UPLOAD_RDY |
 355                              IF_SPI_HISM_CMD_DOWNLOAD_RDY |
 356                              IF_SPI_HISM_CARDEVENT |
 357                              IF_SPI_HISM_CMD_UPLOAD_RDY);
 358                if (err)
 359                        return err;
 360        }
 361
 362        /*
 363         * If auto-interrupts are on, the completion of certain transactions
 364         * will trigger an interrupt automatically. If auto-interrupts
 365         * are off, we need to set the "Card Interrupt Cause" register to
 366         * trigger a card interrupt.
 367         */
 368        if (auto_int) {
 369                err = spu_write_u16(card, IF_SPI_HOST_INT_CTRL_REG,
 370                                IF_SPI_HICT_TX_DOWNLOAD_OVER_AUTO |
 371                                IF_SPI_HICT_RX_UPLOAD_OVER_AUTO |
 372                                IF_SPI_HICT_CMD_DOWNLOAD_OVER_AUTO |
 373                                IF_SPI_HICT_CMD_UPLOAD_OVER_AUTO);
 374                if (err)
 375                        return err;
 376        } else {
 377                err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG, 0);
 378                if (err)
 379                        return err;
 380        }
 381        return err;
 382}
 383
 384static int spu_get_chip_revision(struct if_spi_card *card,
 385                                  u16 *card_id, u8 *card_rev)
 386{
 387        int err = 0;
 388        u32 dev_ctrl;
 389        err = spu_read_u32(card, IF_SPI_DEVICEID_CTRL_REG, &dev_ctrl);
 390        if (err)
 391                return err;
 392        *card_id = IF_SPI_DEVICEID_CTRL_REG_TO_CARD_ID(dev_ctrl);
 393        *card_rev = IF_SPI_DEVICEID_CTRL_REG_TO_CARD_REV(dev_ctrl);
 394        return err;
 395}
 396
 397static int spu_set_bus_mode(struct if_spi_card *card, u16 mode)
 398{
 399        int err = 0;
 400        u16 rval;
 401        /* set bus mode */
 402        err = spu_write_u16(card, IF_SPI_SPU_BUS_MODE_REG, mode);
 403        if (err)
 404                return err;
 405        /* Check that we were able to read back what we just wrote. */
 406        err = spu_read_u16(card, IF_SPI_SPU_BUS_MODE_REG, &rval);
 407        if (err)
 408                return err;
 409        if ((rval & 0xF) != mode) {
 410                pr_err("Can't read bus mode register\n");
 411                return -EIO;
 412        }
 413        return 0;
 414}
 415
 416static int spu_init(struct if_spi_card *card, int use_dummy_writes)
 417{
 418        int err = 0;
 419        u32 delay;
 420
 421        /*
 422         * We have to start up in timed delay mode so that we can safely
 423         * read the Delay Read Register.
 424         */
 425        card->use_dummy_writes = 0;
 426        err = spu_set_bus_mode(card,
 427                                IF_SPI_BUS_MODE_SPI_CLOCK_PHASE_RISING |
 428                                IF_SPI_BUS_MODE_DELAY_METHOD_TIMED |
 429                                IF_SPI_BUS_MODE_16_BIT_ADDRESS_16_BIT_DATA);
 430        if (err)
 431                return err;
 432        card->spu_port_delay = 1000;
 433        card->spu_reg_delay = 1000;
 434        err = spu_read_u32(card, IF_SPI_DELAY_READ_REG, &delay);
 435        if (err)
 436                return err;
 437        card->spu_port_delay = delay & 0x0000ffff;
 438        card->spu_reg_delay = (delay & 0xffff0000) >> 16;
 439
 440        /* If dummy clock delay mode has been requested, switch to it now */
 441        if (use_dummy_writes) {
 442                card->use_dummy_writes = 1;
 443                err = spu_set_bus_mode(card,
 444                                IF_SPI_BUS_MODE_SPI_CLOCK_PHASE_RISING |
 445                                IF_SPI_BUS_MODE_DELAY_METHOD_DUMMY_CLOCK |
 446                                IF_SPI_BUS_MODE_16_BIT_ADDRESS_16_BIT_DATA);
 447                if (err)
 448                        return err;
 449        }
 450
 451        lbs_deb_spi("Initialized SPU unit. "
 452                    "spu_port_delay=0x%04lx, spu_reg_delay=0x%04lx\n",
 453                    card->spu_port_delay, card->spu_reg_delay);
 454        return err;
 455}
 456
 457/*
 458 * Firmware Loading
 459 */
 460
 461static int if_spi_prog_helper_firmware(struct if_spi_card *card,
 462                                        const struct firmware *firmware)
 463{
 464        int err = 0;
 465        int bytes_remaining;
 466        const u8 *fw;
 467        u8 temp[HELPER_FW_LOAD_CHUNK_SZ];
 468
 469        err = spu_set_interrupt_mode(card, 1, 0);
 470        if (err)
 471                goto out;
 472
 473        bytes_remaining = firmware->size;
 474        fw = firmware->data;
 475
 476        /* Load helper firmware image */
 477        while (bytes_remaining > 0) {
 478                /*
 479                 * Scratch pad 1 should contain the number of bytes we
 480                 * want to download to the firmware
 481                 */
 482                err = spu_write_u16(card, IF_SPI_SCRATCH_1_REG,
 483                                        HELPER_FW_LOAD_CHUNK_SZ);
 484                if (err)
 485                        goto out;
 486
 487                err = spu_wait_for_u16(card, IF_SPI_HOST_INT_STATUS_REG,
 488                                        IF_SPI_HIST_CMD_DOWNLOAD_RDY,
 489                                        IF_SPI_HIST_CMD_DOWNLOAD_RDY);
 490                if (err)
 491                        goto out;
 492
 493                /*
 494                 * Feed the data into the command read/write port reg
 495                 * in chunks of 64 bytes
 496                 */
 497                memset(temp, 0, sizeof(temp));
 498                memcpy(temp, fw,
 499                       min(bytes_remaining, HELPER_FW_LOAD_CHUNK_SZ));
 500                mdelay(10);
 501                err = spu_write(card, IF_SPI_CMD_RDWRPORT_REG,
 502                                        temp, HELPER_FW_LOAD_CHUNK_SZ);
 503                if (err)
 504                        goto out;
 505
 506                /* Interrupt the boot code */
 507                err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
 508                if (err)
 509                        goto out;
 510                err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG,
 511                                       IF_SPI_CIC_CMD_DOWNLOAD_OVER);
 512                if (err)
 513                        goto out;
 514                bytes_remaining -= HELPER_FW_LOAD_CHUNK_SZ;
 515                fw += HELPER_FW_LOAD_CHUNK_SZ;
 516        }
 517
 518        /*
 519         * Once the helper / single stage firmware download is complete,
 520         * write 0 to scratch pad 1 and interrupt the
 521         * bootloader. This completes the helper download.
 522         */
 523        err = spu_write_u16(card, IF_SPI_SCRATCH_1_REG, FIRMWARE_DNLD_OK);
 524        if (err)
 525                goto out;
 526        err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
 527        if (err)
 528                goto out;
 529        err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG,
 530                                IF_SPI_CIC_CMD_DOWNLOAD_OVER);
 531out:
 532        if (err)
 533                pr_err("failed to load helper firmware (err=%d)\n", err);
 534
 535        return err;
 536}
 537
 538/*
 539 * Returns the length of the next packet the firmware expects us to send.
 540 * Sets crc_err if the previous transfer had a CRC error.
 541 */
 542static int if_spi_prog_main_firmware_check_len(struct if_spi_card *card,
 543                                                int *crc_err)
 544{
 545        u16 len;
 546        int err = 0;
 547
 548        /*
 549         * wait until the host interrupt status register indicates
 550         * that we are ready to download
 551         */
 552        err = spu_wait_for_u16(card, IF_SPI_HOST_INT_STATUS_REG,
 553                                IF_SPI_HIST_CMD_DOWNLOAD_RDY,
 554                                IF_SPI_HIST_CMD_DOWNLOAD_RDY);
 555        if (err) {
 556                pr_err("timed out waiting for host_int_status\n");
 557                return err;
 558        }
 559
 560        /* Ask the device how many bytes of firmware it wants. */
 561        err = spu_read_u16(card, IF_SPI_SCRATCH_1_REG, &len);
 562        if (err)
 563                return err;
 564
 565        if (len > IF_SPI_CMD_BUF_SIZE) {
 566                pr_err("firmware load device requested a larger transfer than we are prepared to handle (len = %d)\n",
 567                       len);
 568                return -EIO;
 569        }
 570        if (len & 0x1) {
 571                lbs_deb_spi("%s: crc error\n", __func__);
 572                len &= ~0x1;
 573                *crc_err = 1;
 574        } else
 575                *crc_err = 0;
 576
 577        return len;
 578}
 579
 580static int if_spi_prog_main_firmware(struct if_spi_card *card,
 581                                        const struct firmware *firmware)
 582{
 583        struct lbs_private *priv = card->priv;
 584        int len, prev_len;
 585        int bytes, crc_err = 0, err = 0;
 586        const u8 *fw;
 587        u16 num_crc_errs;
 588
 589        err = spu_set_interrupt_mode(card, 1, 0);
 590        if (err)
 591                goto out;
 592
 593        err = spu_wait_for_u16(card, IF_SPI_SCRATCH_1_REG, 0, 0);
 594        if (err) {
 595                netdev_err(priv->dev,
 596                           "%s: timed out waiting for initial scratch reg = 0\n",
 597                           __func__);
 598                goto out;
 599        }
 600
 601        num_crc_errs = 0;
 602        prev_len = 0;
 603        bytes = firmware->size;
 604        fw = firmware->data;
 605        while ((len = if_spi_prog_main_firmware_check_len(card, &crc_err))) {
 606                if (len < 0) {
 607                        err = len;
 608                        goto out;
 609                }
 610                if (bytes < 0) {
 611                        /*
 612                         * If there are no more bytes left, we would normally
 613                         * expect to have terminated with len = 0
 614                         */
 615                        netdev_err(priv->dev,
 616                                   "Firmware load wants more bytes than we have to offer.\n");
 617                        break;
 618                }
 619                if (crc_err) {
 620                        /* Previous transfer failed. */
 621                        if (++num_crc_errs > MAX_MAIN_FW_LOAD_CRC_ERR) {
 622                                pr_err("Too many CRC errors encountered in firmware load.\n");
 623                                err = -EIO;
 624                                goto out;
 625                        }
 626                } else {
 627                        /* Previous transfer succeeded. Advance counters. */
 628                        bytes -= prev_len;
 629                        fw += prev_len;
 630                }
 631                if (bytes < len) {
 632                        memset(card->cmd_buffer, 0, len);
 633                        memcpy(card->cmd_buffer, fw, bytes);
 634                } else
 635                        memcpy(card->cmd_buffer, fw, len);
 636
 637                err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
 638                if (err)
 639                        goto out;
 640                err = spu_write(card, IF_SPI_CMD_RDWRPORT_REG,
 641                                card->cmd_buffer, len);
 642                if (err)
 643                        goto out;
 644                err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG ,
 645                                        IF_SPI_CIC_CMD_DOWNLOAD_OVER);
 646                if (err)
 647                        goto out;
 648                prev_len = len;
 649        }
 650        if (bytes > prev_len) {
 651                pr_err("firmware load wants fewer bytes than we have to offer\n");
 652        }
 653
 654        /* Confirm firmware download */
 655        err = spu_wait_for_u32(card, IF_SPI_SCRATCH_4_REG,
 656                                        SUCCESSFUL_FW_DOWNLOAD_MAGIC);
 657        if (err) {
 658                pr_err("failed to confirm the firmware download\n");
 659                goto out;
 660        }
 661
 662out:
 663        if (err)
 664                pr_err("failed to load firmware (err=%d)\n", err);
 665
 666        return err;
 667}
 668
 669/*
 670 * SPI Transfer Thread
 671 *
 672 * The SPI worker handles all SPI transfers, so there is no need for a lock.
 673 */
 674
 675/* Move a command from the card to the host */
 676static int if_spi_c2h_cmd(struct if_spi_card *card)
 677{
 678        struct lbs_private *priv = card->priv;
 679        unsigned long flags;
 680        int err = 0;
 681        u16 len;
 682        u8 i;
 683
 684        /*
 685         * We need a buffer big enough to handle whatever people send to
 686         * hw_host_to_card
 687         */
 688        BUILD_BUG_ON(IF_SPI_CMD_BUF_SIZE < LBS_CMD_BUFFER_SIZE);
 689        BUILD_BUG_ON(IF_SPI_CMD_BUF_SIZE < LBS_UPLD_SIZE);
 690
 691        /*
 692         * It's just annoying if the buffer size isn't a multiple of 4, because
 693         * then we might have len < IF_SPI_CMD_BUF_SIZE but
 694         * ALIGN(len, 4) > IF_SPI_CMD_BUF_SIZE
 695         */
 696        BUILD_BUG_ON(IF_SPI_CMD_BUF_SIZE % 4 != 0);
 697
 698        /* How many bytes are there to read? */
 699        err = spu_read_u16(card, IF_SPI_SCRATCH_2_REG, &len);
 700        if (err)
 701                goto out;
 702        if (!len) {
 703                netdev_err(priv->dev, "%s: error: card has no data for host\n",
 704                           __func__);
 705                err = -EINVAL;
 706                goto out;
 707        } else if (len > IF_SPI_CMD_BUF_SIZE) {
 708                netdev_err(priv->dev,
 709                           "%s: error: response packet too large: %d bytes, but maximum is %d\n",
 710                           __func__, len, IF_SPI_CMD_BUF_SIZE);
 711                err = -EINVAL;
 712                goto out;
 713        }
 714
 715        /* Read the data from the WLAN module into our command buffer */
 716        err = spu_read(card, IF_SPI_CMD_RDWRPORT_REG,
 717                                card->cmd_buffer, ALIGN(len, 4));
 718        if (err)
 719                goto out;
 720
 721        spin_lock_irqsave(&priv->driver_lock, flags);
 722        i = (priv->resp_idx == 0) ? 1 : 0;
 723        BUG_ON(priv->resp_len[i]);
 724        priv->resp_len[i] = len;
 725        memcpy(priv->resp_buf[i], card->cmd_buffer, len);
 726        lbs_notify_command_response(priv, i);
 727        spin_unlock_irqrestore(&priv->driver_lock, flags);
 728
 729out:
 730        if (err)
 731                netdev_err(priv->dev, "%s: err=%d\n", __func__, err);
 732
 733        return err;
 734}
 735
 736/* Move data from the card to the host */
 737static int if_spi_c2h_data(struct if_spi_card *card)
 738{
 739        struct lbs_private *priv = card->priv;
 740        struct sk_buff *skb;
 741        char *data;
 742        u16 len;
 743        int err = 0;
 744
 745        /* How many bytes are there to read? */
 746        err = spu_read_u16(card, IF_SPI_SCRATCH_1_REG, &len);
 747        if (err)
 748                goto out;
 749        if (!len) {
 750                netdev_err(priv->dev, "%s: error: card has no data for host\n",
 751                           __func__);
 752                err = -EINVAL;
 753                goto out;
 754        } else if (len > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
 755                netdev_err(priv->dev,
 756                           "%s: error: card has %d bytes of data, but our maximum skb size is %zu\n",
 757                           __func__, len, MRVDRV_ETH_RX_PACKET_BUFFER_SIZE);
 758                err = -EINVAL;
 759                goto out;
 760        }
 761
 762        /* TODO: should we allocate a smaller skb if we have less data? */
 763        skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE);
 764        if (!skb) {
 765                err = -ENOBUFS;
 766                goto out;
 767        }
 768        skb_reserve(skb, IPFIELD_ALIGN_OFFSET);
 769        data = skb_put(skb, len);
 770
 771        /* Read the data from the WLAN module into our skb... */
 772        err = spu_read(card, IF_SPI_DATA_RDWRPORT_REG, data, ALIGN(len, 4));
 773        if (err)
 774                goto free_skb;
 775
 776        /* pass the SKB to libertas */
 777        err = lbs_process_rxed_packet(card->priv, skb);
 778        if (err)
 779                goto free_skb;
 780
 781        /* success */
 782        goto out;
 783
 784free_skb:
 785        dev_kfree_skb(skb);
 786out:
 787        if (err)
 788                netdev_err(priv->dev, "%s: err=%d\n", __func__, err);
 789
 790        return err;
 791}
 792
 793/* Move data or a command from the host to the card. */
 794static void if_spi_h2c(struct if_spi_card *card,
 795                        struct if_spi_packet *packet, int type)
 796{
 797        struct lbs_private *priv = card->priv;
 798        int err = 0;
 799        u16 int_type, port_reg;
 800
 801        switch (type) {
 802        case MVMS_DAT:
 803                int_type = IF_SPI_CIC_TX_DOWNLOAD_OVER;
 804                port_reg = IF_SPI_DATA_RDWRPORT_REG;
 805                break;
 806        case MVMS_CMD:
 807                int_type = IF_SPI_CIC_CMD_DOWNLOAD_OVER;
 808                port_reg = IF_SPI_CMD_RDWRPORT_REG;
 809                break;
 810        default:
 811                netdev_err(priv->dev, "can't transfer buffer of type %d\n",
 812                           type);
 813                err = -EINVAL;
 814                goto out;
 815        }
 816
 817        /* Write the data to the card */
 818        err = spu_write(card, port_reg, packet->buffer, packet->blen);
 819        if (err)
 820                goto out;
 821
 822out:
 823        kfree(packet);
 824
 825        if (err)
 826                netdev_err(priv->dev, "%s: error %d\n", __func__, err);
 827}
 828
 829/* Inform the host about a card event */
 830static void if_spi_e2h(struct if_spi_card *card)
 831{
 832        int err = 0;
 833        u32 cause;
 834        struct lbs_private *priv = card->priv;
 835
 836        err = spu_read_u32(card, IF_SPI_SCRATCH_3_REG, &cause);
 837        if (err)
 838                goto out;
 839
 840        /* re-enable the card event interrupt */
 841        spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG,
 842                        ~IF_SPI_HICU_CARD_EVENT);
 843
 844        /* generate a card interrupt */
 845        spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG, IF_SPI_CIC_HOST_EVENT);
 846
 847        lbs_queue_event(priv, cause & 0xff);
 848out:
 849        if (err)
 850                netdev_err(priv->dev, "%s: error %d\n", __func__, err);
 851}
 852
 853static void if_spi_host_to_card_worker(struct work_struct *work)
 854{
 855        int err;
 856        struct if_spi_card *card;
 857        u16 hiStatus;
 858        unsigned long flags;
 859        struct if_spi_packet *packet;
 860        struct lbs_private *priv;
 861
 862        card = container_of(work, struct if_spi_card, packet_work);
 863        priv = card->priv;
 864
 865        /*
 866         * Read the host interrupt status register to see what we
 867         * can do.
 868         */
 869        err = spu_read_u16(card, IF_SPI_HOST_INT_STATUS_REG,
 870                                &hiStatus);
 871        if (err) {
 872                netdev_err(priv->dev, "I/O error\n");
 873                goto err;
 874        }
 875
 876        if (hiStatus & IF_SPI_HIST_CMD_UPLOAD_RDY) {
 877                err = if_spi_c2h_cmd(card);
 878                if (err)
 879                        goto err;
 880        }
 881        if (hiStatus & IF_SPI_HIST_RX_UPLOAD_RDY) {
 882                err = if_spi_c2h_data(card);
 883                if (err)
 884                        goto err;
 885        }
 886
 887        /*
 888         * workaround: in PS mode, the card does not set the Command
 889         * Download Ready bit, but it sets TX Download Ready.
 890         */
 891        if (hiStatus & IF_SPI_HIST_CMD_DOWNLOAD_RDY ||
 892           (card->priv->psstate != PS_STATE_FULL_POWER &&
 893            (hiStatus & IF_SPI_HIST_TX_DOWNLOAD_RDY))) {
 894                /*
 895                 * This means two things. First of all,
 896                 * if there was a previous command sent, the card has
 897                 * successfully received it.
 898                 * Secondly, it is now ready to download another
 899                 * command.
 900                 */
 901                lbs_host_to_card_done(card->priv);
 902
 903                /* Do we have any command packets from the host to send? */
 904                packet = NULL;
 905                spin_lock_irqsave(&card->buffer_lock, flags);
 906                if (!list_empty(&card->cmd_packet_list)) {
 907                        packet = (struct if_spi_packet *)(card->
 908                                        cmd_packet_list.next);
 909                        list_del(&packet->list);
 910                }
 911                spin_unlock_irqrestore(&card->buffer_lock, flags);
 912
 913                if (packet)
 914                        if_spi_h2c(card, packet, MVMS_CMD);
 915        }
 916        if (hiStatus & IF_SPI_HIST_TX_DOWNLOAD_RDY) {
 917                /* Do we have any data packets from the host to send? */
 918                packet = NULL;
 919                spin_lock_irqsave(&card->buffer_lock, flags);
 920                if (!list_empty(&card->data_packet_list)) {
 921                        packet = (struct if_spi_packet *)(card->
 922                                        data_packet_list.next);
 923                        list_del(&packet->list);
 924                }
 925                spin_unlock_irqrestore(&card->buffer_lock, flags);
 926
 927                if (packet)
 928                        if_spi_h2c(card, packet, MVMS_DAT);
 929        }
 930        if (hiStatus & IF_SPI_HIST_CARD_EVENT)
 931                if_spi_e2h(card);
 932
 933err:
 934        if (err)
 935                netdev_err(priv->dev, "%s: got error %d\n", __func__, err);
 936}
 937
 938/*
 939 * Host to Card
 940 *
 941 * Called from Libertas to transfer some data to the WLAN device
 942 * We can't sleep here.
 943 */
 944static int if_spi_host_to_card(struct lbs_private *priv,
 945                                u8 type, u8 *buf, u16 nb)
 946{
 947        int err = 0;
 948        unsigned long flags;
 949        struct if_spi_card *card = priv->card;
 950        struct if_spi_packet *packet;
 951        u16 blen;
 952
 953        if (nb == 0) {
 954                netdev_err(priv->dev, "%s: invalid size requested: %d\n",
 955                           __func__, nb);
 956                err = -EINVAL;
 957                goto out;
 958        }
 959        blen = ALIGN(nb, 4);
 960        packet = kzalloc(sizeof(struct if_spi_packet) + blen, GFP_ATOMIC);
 961        if (!packet) {
 962                err = -ENOMEM;
 963                goto out;
 964        }
 965        packet->blen = blen;
 966        memcpy(packet->buffer, buf, nb);
 967        memset(packet->buffer + nb, 0, blen - nb);
 968
 969        switch (type) {
 970        case MVMS_CMD:
 971                priv->dnld_sent = DNLD_CMD_SENT;
 972                spin_lock_irqsave(&card->buffer_lock, flags);
 973                list_add_tail(&packet->list, &card->cmd_packet_list);
 974                spin_unlock_irqrestore(&card->buffer_lock, flags);
 975                break;
 976        case MVMS_DAT:
 977                priv->dnld_sent = DNLD_DATA_SENT;
 978                spin_lock_irqsave(&card->buffer_lock, flags);
 979                list_add_tail(&packet->list, &card->data_packet_list);
 980                spin_unlock_irqrestore(&card->buffer_lock, flags);
 981                break;
 982        default:
 983                kfree(packet);
 984                netdev_err(priv->dev, "can't transfer buffer of type %d\n",
 985                           type);
 986                err = -EINVAL;
 987                break;
 988        }
 989
 990        /* Queue spi xfer work */
 991        queue_work(card->workqueue, &card->packet_work);
 992out:
 993        return err;
 994}
 995
 996/*
 997 * Host Interrupts
 998 *
 999 * Service incoming interrupts from the WLAN device. We can't sleep here, so
1000 * don't try to talk on the SPI bus, just queue the SPI xfer work.
1001 */
1002static irqreturn_t if_spi_host_interrupt(int irq, void *dev_id)
1003{
1004        struct if_spi_card *card = dev_id;
1005
1006        queue_work(card->workqueue, &card->packet_work);
1007
1008        return IRQ_HANDLED;
1009}
1010
1011/*
1012 * SPI callbacks
1013 */
1014
1015static int if_spi_init_card(struct if_spi_card *card)
1016{
1017        struct lbs_private *priv = card->priv;
1018        int err, i;
1019        u32 scratch;
1020        const struct firmware *helper = NULL;
1021        const struct firmware *mainfw = NULL;
1022
1023        err = spu_init(card, card->pdata->use_dummy_writes);
1024        if (err)
1025                goto out;
1026        err = spu_get_chip_revision(card, &card->card_id, &card->card_rev);
1027        if (err)
1028                goto out;
1029
1030        err = spu_read_u32(card, IF_SPI_SCRATCH_4_REG, &scratch);
1031        if (err)
1032                goto out;
1033        if (scratch == SUCCESSFUL_FW_DOWNLOAD_MAGIC)
1034                lbs_deb_spi("Firmware is already loaded for "
1035                            "Marvell WLAN 802.11 adapter\n");
1036        else {
1037                /* Check if we support this card */
1038                for (i = 0; i < ARRAY_SIZE(fw_table); i++) {
1039                        if (card->card_id == fw_table[i].model)
1040                                break;
1041                }
1042                if (i == ARRAY_SIZE(fw_table)) {
1043                        netdev_err(priv->dev, "Unsupported chip_id: 0x%02x\n",
1044                                   card->card_id);
1045                        err = -ENODEV;
1046                        goto out;
1047                }
1048
1049                err = lbs_get_firmware(&card->spi->dev, card->card_id,
1050                                        &fw_table[0], &helper, &mainfw);
1051                if (err) {
1052                        netdev_err(priv->dev, "failed to find firmware (%d)\n",
1053                                   err);
1054                        goto out;
1055                }
1056
1057                lbs_deb_spi("Initializing FW for Marvell WLAN 802.11 adapter "
1058                                "(chip_id = 0x%04x, chip_rev = 0x%02x) "
1059                                "attached to SPI bus_num %d, chip_select %d. "
1060                                "spi->max_speed_hz=%d\n",
1061                                card->card_id, card->card_rev,
1062                                card->spi->master->bus_num,
1063                                card->spi->chip_select,
1064                                card->spi->max_speed_hz);
1065                err = if_spi_prog_helper_firmware(card, helper);
1066                if (err)
1067                        goto out;
1068                err = if_spi_prog_main_firmware(card, mainfw);
1069                if (err)
1070                        goto out;
1071                lbs_deb_spi("loaded FW for Marvell WLAN 802.11 adapter\n");
1072        }
1073
1074        err = spu_set_interrupt_mode(card, 0, 1);
1075        if (err)
1076                goto out;
1077
1078out:
1079        return err;
1080}
1081
1082static void if_spi_resume_worker(struct work_struct *work)
1083{
1084        struct if_spi_card *card;
1085
1086        card = container_of(work, struct if_spi_card, resume_work);
1087
1088        if (card->suspended) {
1089                if (card->pdata->setup)
1090                        card->pdata->setup(card->spi);
1091
1092                /* Init card ... */
1093                if_spi_init_card(card);
1094
1095                enable_irq(card->spi->irq);
1096
1097                /* And resume it ... */
1098                lbs_resume(card->priv);
1099
1100                card->suspended = 0;
1101        }
1102}
1103
1104static int if_spi_probe(struct spi_device *spi)
1105{
1106        struct if_spi_card *card;
1107        struct lbs_private *priv = NULL;
1108        struct libertas_spi_platform_data *pdata = dev_get_platdata(&spi->dev);
1109        int err = 0;
1110
1111        if (!pdata) {
1112                err = -EINVAL;
1113                goto out;
1114        }
1115
1116        if (pdata->setup) {
1117                err = pdata->setup(spi);
1118                if (err)
1119                        goto out;
1120        }
1121
1122        /* Allocate card structure to represent this specific device */
1123        card = kzalloc(sizeof(struct if_spi_card), GFP_KERNEL);
1124        if (!card) {
1125                err = -ENOMEM;
1126                goto teardown;
1127        }
1128        spi_set_drvdata(spi, card);
1129        card->pdata = pdata;
1130        card->spi = spi;
1131        card->prev_xfer_time = jiffies;
1132
1133        INIT_LIST_HEAD(&card->cmd_packet_list);
1134        INIT_LIST_HEAD(&card->data_packet_list);
1135        spin_lock_init(&card->buffer_lock);
1136
1137        /* Initialize the SPI Interface Unit */
1138
1139        /* Firmware load */
1140        err = if_spi_init_card(card);
1141        if (err)
1142                goto free_card;
1143
1144        /*
1145         * Register our card with libertas.
1146         * This will call alloc_etherdev.
1147         */
1148        priv = lbs_add_card(card, &spi->dev);
1149        if (!priv) {
1150                err = -ENOMEM;
1151                goto free_card;
1152        }
1153        card->priv = priv;
1154        priv->setup_fw_on_resume = 1;
1155        priv->card = card;
1156        priv->hw_host_to_card = if_spi_host_to_card;
1157        priv->enter_deep_sleep = NULL;
1158        priv->exit_deep_sleep = NULL;
1159        priv->reset_deep_sleep_wakeup = NULL;
1160        priv->fw_ready = 1;
1161
1162        /* Initialize interrupt handling stuff. */
1163        card->workqueue = alloc_workqueue("libertas_spi", WQ_MEM_RECLAIM, 0);
1164        if (!card->workqueue) {
1165                err = -ENOMEM;
1166                goto remove_card;
1167        }
1168        INIT_WORK(&card->packet_work, if_spi_host_to_card_worker);
1169        INIT_WORK(&card->resume_work, if_spi_resume_worker);
1170
1171        err = request_irq(spi->irq, if_spi_host_interrupt,
1172                        IRQF_TRIGGER_FALLING, "libertas_spi", card);
1173        if (err) {
1174                pr_err("can't get host irq line-- request_irq failed\n");
1175                goto terminate_workqueue;
1176        }
1177
1178        /*
1179         * Start the card.
1180         * This will call register_netdev, and we'll start
1181         * getting interrupts...
1182         */
1183        err = lbs_start_card(priv);
1184        if (err)
1185                goto release_irq;
1186
1187        lbs_deb_spi("Finished initializing WLAN module.\n");
1188
1189        /* successful exit */
1190        goto out;
1191
1192release_irq:
1193        free_irq(spi->irq, card);
1194terminate_workqueue:
1195        destroy_workqueue(card->workqueue);
1196remove_card:
1197        lbs_remove_card(priv); /* will call free_netdev */
1198free_card:
1199        free_if_spi_card(card);
1200teardown:
1201        if (pdata->teardown)
1202                pdata->teardown(spi);
1203out:
1204        return err;
1205}
1206
1207static int libertas_spi_remove(struct spi_device *spi)
1208{
1209        struct if_spi_card *card = spi_get_drvdata(spi);
1210        struct lbs_private *priv = card->priv;
1211
1212        lbs_deb_spi("libertas_spi_remove\n");
1213
1214        cancel_work_sync(&card->resume_work);
1215
1216        lbs_stop_card(priv);
1217        lbs_remove_card(priv); /* will call free_netdev */
1218
1219        free_irq(spi->irq, card);
1220        destroy_workqueue(card->workqueue);
1221        if (card->pdata->teardown)
1222                card->pdata->teardown(spi);
1223        free_if_spi_card(card);
1224
1225        return 0;
1226}
1227
1228static int if_spi_suspend(struct device *dev)
1229{
1230        struct spi_device *spi = to_spi_device(dev);
1231        struct if_spi_card *card = spi_get_drvdata(spi);
1232
1233        if (!card->suspended) {
1234                lbs_suspend(card->priv);
1235                flush_workqueue(card->workqueue);
1236                disable_irq(spi->irq);
1237
1238                if (card->pdata->teardown)
1239                        card->pdata->teardown(spi);
1240                card->suspended = 1;
1241        }
1242
1243        return 0;
1244}
1245
1246static int if_spi_resume(struct device *dev)
1247{
1248        struct spi_device *spi = to_spi_device(dev);
1249        struct if_spi_card *card = spi_get_drvdata(spi);
1250
1251        /* Schedule delayed work */
1252        schedule_work(&card->resume_work);
1253
1254        return 0;
1255}
1256
1257static const struct dev_pm_ops if_spi_pm_ops = {
1258        .suspend        = if_spi_suspend,
1259        .resume         = if_spi_resume,
1260};
1261
1262static struct spi_driver libertas_spi_driver = {
1263        .probe  = if_spi_probe,
1264        .remove = libertas_spi_remove,
1265        .driver = {
1266                .name   = "libertas_spi",
1267                .pm     = &if_spi_pm_ops,
1268        },
1269};
1270
1271/*
1272 * Module functions
1273 */
1274
1275static int __init if_spi_init_module(void)
1276{
1277        int ret = 0;
1278
1279        printk(KERN_INFO "libertas_spi: Libertas SPI driver\n");
1280        ret = spi_register_driver(&libertas_spi_driver);
1281
1282        return ret;
1283}
1284
1285static void __exit if_spi_exit_module(void)
1286{
1287        spi_unregister_driver(&libertas_spi_driver);
1288}
1289
1290module_init(if_spi_init_module);
1291module_exit(if_spi_exit_module);
1292
1293MODULE_DESCRIPTION("Libertas SPI WLAN Driver");
1294MODULE_AUTHOR("Andrey Yurovsky <andrey@cozybit.com>, "
1295              "Colin McCabe <colin@cozybit.com>");
1296MODULE_LICENSE("GPL");
1297MODULE_ALIAS("spi:libertas_spi");
1298