Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  linux/drivers/net/wireless/libertas/if_spi.c
0004  *
0005  *  Driver for Marvell SPI WLAN cards.
0006  *
0007  *  Copyright 2008 Analog Devices Inc.
0008  *
0009  *  Authors:
0010  *  Andrey Yurovsky <andrey@cozybit.com>
0011  *  Colin McCabe <colin@cozybit.com>
0012  *
0013  *  Inspired by if_sdio.c, Copyright 2007-2008 Pierre Ossman
0014  */
0015 
0016 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0017 
0018 #include <linux/hardirq.h>
0019 #include <linux/interrupt.h>
0020 #include <linux/module.h>
0021 #include <linux/firmware.h>
0022 #include <linux/jiffies.h>
0023 #include <linux/list.h>
0024 #include <linux/netdevice.h>
0025 #include <linux/slab.h>
0026 #include <linux/spi/libertas_spi.h>
0027 #include <linux/spi/spi.h>
0028 
0029 #include "host.h"
0030 #include "decl.h"
0031 #include "defs.h"
0032 #include "dev.h"
0033 #include "if_spi.h"
0034 
0035 struct if_spi_packet {
0036     struct list_head        list;
0037     u16             blen;
0038     u8              buffer[] __aligned(4);
0039 };
0040 
0041 struct if_spi_card {
0042     struct spi_device       *spi;
0043     struct lbs_private      *priv;
0044     struct libertas_spi_platform_data *pdata;
0045 
0046     /* The card ID and card revision, as reported by the hardware. */
0047     u16             card_id;
0048     u8              card_rev;
0049 
0050     /* The last time that we initiated an SPU operation */
0051     unsigned long           prev_xfer_time;
0052 
0053     int             use_dummy_writes;
0054     unsigned long           spu_port_delay;
0055     unsigned long           spu_reg_delay;
0056 
0057     /* Handles all SPI communication (except for FW load) */
0058     struct workqueue_struct     *workqueue;
0059     struct work_struct      packet_work;
0060     struct work_struct      resume_work;
0061 
0062     u8              cmd_buffer[IF_SPI_CMD_BUF_SIZE];
0063 
0064     /* A buffer of incoming packets from libertas core.
0065      * Since we can't sleep in hw_host_to_card, we have to buffer
0066      * them. */
0067     struct list_head        cmd_packet_list;
0068     struct list_head        data_packet_list;
0069 
0070     /* Protects cmd_packet_list and data_packet_list */
0071     spinlock_t          buffer_lock;
0072 
0073     /* True is card suspended */
0074     u8              suspended;
0075 };
0076 
0077 static void free_if_spi_card(struct if_spi_card *card)
0078 {
0079     struct list_head *cursor, *next;
0080     struct if_spi_packet *packet;
0081 
0082     list_for_each_safe(cursor, next, &card->cmd_packet_list) {
0083         packet = container_of(cursor, struct if_spi_packet, list);
0084         list_del(&packet->list);
0085         kfree(packet);
0086     }
0087     list_for_each_safe(cursor, next, &card->data_packet_list) {
0088         packet = container_of(cursor, struct if_spi_packet, list);
0089         list_del(&packet->list);
0090         kfree(packet);
0091     }
0092     kfree(card);
0093 }
0094 
0095 #define MODEL_8385  0x04
0096 #define MODEL_8686  0x0b
0097 #define MODEL_8688  0x10
0098 
0099 static const struct lbs_fw_table fw_table[] = {
0100     { MODEL_8385, "libertas/gspi8385_helper.bin", "libertas/gspi8385.bin" },
0101     { MODEL_8385, "libertas/gspi8385_hlp.bin", "libertas/gspi8385.bin" },
0102     { MODEL_8686, "libertas/gspi8686_v9_helper.bin", "libertas/gspi8686_v9.bin" },
0103     { MODEL_8686, "libertas/gspi8686_hlp.bin", "libertas/gspi8686.bin" },
0104     { MODEL_8688, "libertas/gspi8688_helper.bin", "libertas/gspi8688.bin" },
0105     { 0, NULL, NULL }
0106 };
0107 MODULE_FIRMWARE("libertas/gspi8385_helper.bin");
0108 MODULE_FIRMWARE("libertas/gspi8385_hlp.bin");
0109 MODULE_FIRMWARE("libertas/gspi8385.bin");
0110 MODULE_FIRMWARE("libertas/gspi8686_v9_helper.bin");
0111 MODULE_FIRMWARE("libertas/gspi8686_v9.bin");
0112 MODULE_FIRMWARE("libertas/gspi8686_hlp.bin");
0113 MODULE_FIRMWARE("libertas/gspi8686.bin");
0114 MODULE_FIRMWARE("libertas/gspi8688_helper.bin");
0115 MODULE_FIRMWARE("libertas/gspi8688.bin");
0116 
0117 
0118 /*
0119  * SPI Interface Unit Routines
0120  *
0121  * The SPU sits between the host and the WLAN module.
0122  * All communication with the firmware is through SPU transactions.
0123  *
0124  * First we have to put a SPU register name on the bus. Then we can
0125  * either read from or write to that register.
0126  *
0127  */
0128 
0129 static void spu_transaction_init(struct if_spi_card *card)
0130 {
0131     if (!time_after(jiffies, card->prev_xfer_time + 1)) {
0132         /* Unfortunately, the SPU requires a delay between successive
0133          * transactions. If our last transaction was more than a jiffy
0134          * ago, we have obviously already delayed enough.
0135          * If not, we have to busy-wait to be on the safe side. */
0136         ndelay(400);
0137     }
0138 }
0139 
0140 static void spu_transaction_finish(struct if_spi_card *card)
0141 {
0142     card->prev_xfer_time = jiffies;
0143 }
0144 
0145 /*
0146  * Write out a byte buffer to an SPI register,
0147  * using a series of 16-bit transfers.
0148  */
0149 static int spu_write(struct if_spi_card *card, u16 reg, const u8 *buf, int len)
0150 {
0151     int err = 0;
0152     __le16 reg_out = cpu_to_le16(reg | IF_SPI_WRITE_OPERATION_MASK);
0153     struct spi_message m;
0154     struct spi_transfer reg_trans;
0155     struct spi_transfer data_trans;
0156 
0157     spi_message_init(&m);
0158     memset(&reg_trans, 0, sizeof(reg_trans));
0159     memset(&data_trans, 0, sizeof(data_trans));
0160 
0161     /* You must give an even number of bytes to the SPU, even if it
0162      * doesn't care about the last one.  */
0163     BUG_ON(len & 0x1);
0164 
0165     spu_transaction_init(card);
0166 
0167     /* write SPU register index */
0168     reg_trans.tx_buf = &reg_out;
0169     reg_trans.len = sizeof(reg_out);
0170 
0171     data_trans.tx_buf = buf;
0172     data_trans.len = len;
0173 
0174     spi_message_add_tail(&reg_trans, &m);
0175     spi_message_add_tail(&data_trans, &m);
0176 
0177     err = spi_sync(card->spi, &m);
0178     spu_transaction_finish(card);
0179     return err;
0180 }
0181 
0182 static inline int spu_write_u16(struct if_spi_card *card, u16 reg, u16 val)
0183 {
0184     __le16 buff;
0185 
0186     buff = cpu_to_le16(val);
0187     return spu_write(card, reg, (u8 *)&buff, sizeof(u16));
0188 }
0189 
0190 static inline int spu_reg_is_port_reg(u16 reg)
0191 {
0192     switch (reg) {
0193     case IF_SPI_IO_RDWRPORT_REG:
0194     case IF_SPI_CMD_RDWRPORT_REG:
0195     case IF_SPI_DATA_RDWRPORT_REG:
0196         return 1;
0197     default:
0198         return 0;
0199     }
0200 }
0201 
0202 static int spu_read(struct if_spi_card *card, u16 reg, u8 *buf, int len)
0203 {
0204     unsigned int delay;
0205     int err = 0;
0206     __le16 reg_out = cpu_to_le16(reg | IF_SPI_READ_OPERATION_MASK);
0207     struct spi_message m;
0208     struct spi_transfer reg_trans;
0209     struct spi_transfer dummy_trans;
0210     struct spi_transfer data_trans;
0211 
0212     /*
0213      * You must take an even number of bytes from the SPU, even if you
0214      * don't care about the last one.
0215      */
0216     BUG_ON(len & 0x1);
0217 
0218     spu_transaction_init(card);
0219 
0220     spi_message_init(&m);
0221     memset(&reg_trans, 0, sizeof(reg_trans));
0222     memset(&dummy_trans, 0, sizeof(dummy_trans));
0223     memset(&data_trans, 0, sizeof(data_trans));
0224 
0225     /* write SPU register index */
0226     reg_trans.tx_buf = &reg_out;
0227     reg_trans.len = sizeof(reg_out);
0228     spi_message_add_tail(&reg_trans, &m);
0229 
0230     delay = spu_reg_is_port_reg(reg) ? card->spu_port_delay :
0231                         card->spu_reg_delay;
0232     if (card->use_dummy_writes) {
0233         /* Clock in dummy cycles while the SPU fills the FIFO */
0234         dummy_trans.len = delay / 8;
0235         spi_message_add_tail(&dummy_trans, &m);
0236     } else {
0237         /* Busy-wait while the SPU fills the FIFO */
0238         reg_trans.delay.value =
0239             DIV_ROUND_UP((100 + (delay * 10)), 1000);
0240         reg_trans.delay.unit = SPI_DELAY_UNIT_USECS;
0241     }
0242 
0243     /* read in data */
0244     data_trans.rx_buf = buf;
0245     data_trans.len = len;
0246     spi_message_add_tail(&data_trans, &m);
0247 
0248     err = spi_sync(card->spi, &m);
0249     spu_transaction_finish(card);
0250     return err;
0251 }
0252 
0253 /* Read 16 bits from an SPI register */
0254 static inline int spu_read_u16(struct if_spi_card *card, u16 reg, u16 *val)
0255 {
0256     __le16 buf;
0257     int ret;
0258 
0259     ret = spu_read(card, reg, (u8 *)&buf, sizeof(buf));
0260     if (ret == 0)
0261         *val = le16_to_cpup(&buf);
0262     return ret;
0263 }
0264 
0265 /*
0266  * Read 32 bits from an SPI register.
0267  * The low 16 bits are read first.
0268  */
0269 static int spu_read_u32(struct if_spi_card *card, u16 reg, u32 *val)
0270 {
0271     __le32 buf;
0272     int err;
0273 
0274     err = spu_read(card, reg, (u8 *)&buf, sizeof(buf));
0275     if (!err)
0276         *val = le32_to_cpup(&buf);
0277     return err;
0278 }
0279 
0280 /*
0281  * Keep reading 16 bits from an SPI register until you get the correct result.
0282  *
0283  * If mask = 0, the correct result is any non-zero number.
0284  * If mask != 0, the correct result is any number where
0285  * number & target_mask == target
0286  *
0287  * Returns -ETIMEDOUT if a second passes without the correct result.
0288  */
0289 static int spu_wait_for_u16(struct if_spi_card *card, u16 reg,
0290             u16 target_mask, u16 target)
0291 {
0292     int err;
0293     unsigned long timeout = jiffies + 5*HZ;
0294     while (1) {
0295         u16 val;
0296         err = spu_read_u16(card, reg, &val);
0297         if (err)
0298             return err;
0299         if (target_mask) {
0300             if ((val & target_mask) == target)
0301                 return 0;
0302         } else {
0303             if (val)
0304                 return 0;
0305         }
0306         udelay(100);
0307         if (time_after(jiffies, timeout)) {
0308             pr_err("%s: timeout with val=%02x, target_mask=%02x, target=%02x\n",
0309                    __func__, val, target_mask, target);
0310             return -ETIMEDOUT;
0311         }
0312     }
0313 }
0314 
0315 /*
0316  * Read 16 bits from an SPI register until you receive a specific value.
0317  * Returns -ETIMEDOUT if a 4 tries pass without success.
0318  */
0319 static int spu_wait_for_u32(struct if_spi_card *card, u32 reg, u32 target)
0320 {
0321     int err, try;
0322     for (try = 0; try < 4; ++try) {
0323         u32 val = 0;
0324         err = spu_read_u32(card, reg, &val);
0325         if (err)
0326             return err;
0327         if (val == target)
0328             return 0;
0329         mdelay(100);
0330     }
0331     return -ETIMEDOUT;
0332 }
0333 
0334 static int spu_set_interrupt_mode(struct if_spi_card *card,
0335                int suppress_host_int,
0336                int auto_int)
0337 {
0338     int err = 0;
0339 
0340     /*
0341      * We can suppress a host interrupt by clearing the appropriate
0342      * bit in the "host interrupt status mask" register
0343      */
0344     if (suppress_host_int) {
0345         err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG, 0);
0346         if (err)
0347             return err;
0348     } else {
0349         err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG,
0350                   IF_SPI_HISM_TX_DOWNLOAD_RDY |
0351                   IF_SPI_HISM_RX_UPLOAD_RDY |
0352                   IF_SPI_HISM_CMD_DOWNLOAD_RDY |
0353                   IF_SPI_HISM_CARDEVENT |
0354                   IF_SPI_HISM_CMD_UPLOAD_RDY);
0355         if (err)
0356             return err;
0357     }
0358 
0359     /*
0360      * If auto-interrupts are on, the completion of certain transactions
0361      * will trigger an interrupt automatically. If auto-interrupts
0362      * are off, we need to set the "Card Interrupt Cause" register to
0363      * trigger a card interrupt.
0364      */
0365     if (auto_int) {
0366         err = spu_write_u16(card, IF_SPI_HOST_INT_CTRL_REG,
0367                 IF_SPI_HICT_TX_DOWNLOAD_OVER_AUTO |
0368                 IF_SPI_HICT_RX_UPLOAD_OVER_AUTO |
0369                 IF_SPI_HICT_CMD_DOWNLOAD_OVER_AUTO |
0370                 IF_SPI_HICT_CMD_UPLOAD_OVER_AUTO);
0371         if (err)
0372             return err;
0373     } else {
0374         err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG, 0);
0375         if (err)
0376             return err;
0377     }
0378     return err;
0379 }
0380 
0381 static int spu_get_chip_revision(struct if_spi_card *card,
0382                   u16 *card_id, u8 *card_rev)
0383 {
0384     int err = 0;
0385     u32 dev_ctrl;
0386     err = spu_read_u32(card, IF_SPI_DEVICEID_CTRL_REG, &dev_ctrl);
0387     if (err)
0388         return err;
0389     *card_id = IF_SPI_DEVICEID_CTRL_REG_TO_CARD_ID(dev_ctrl);
0390     *card_rev = IF_SPI_DEVICEID_CTRL_REG_TO_CARD_REV(dev_ctrl);
0391     return err;
0392 }
0393 
0394 static int spu_set_bus_mode(struct if_spi_card *card, u16 mode)
0395 {
0396     int err = 0;
0397     u16 rval;
0398     /* set bus mode */
0399     err = spu_write_u16(card, IF_SPI_SPU_BUS_MODE_REG, mode);
0400     if (err)
0401         return err;
0402     /* Check that we were able to read back what we just wrote. */
0403     err = spu_read_u16(card, IF_SPI_SPU_BUS_MODE_REG, &rval);
0404     if (err)
0405         return err;
0406     if ((rval & 0xF) != mode) {
0407         pr_err("Can't read bus mode register\n");
0408         return -EIO;
0409     }
0410     return 0;
0411 }
0412 
0413 static int spu_init(struct if_spi_card *card, int use_dummy_writes)
0414 {
0415     int err = 0;
0416     u32 delay;
0417 
0418     /*
0419      * We have to start up in timed delay mode so that we can safely
0420      * read the Delay Read Register.
0421      */
0422     card->use_dummy_writes = 0;
0423     err = spu_set_bus_mode(card,
0424                 IF_SPI_BUS_MODE_SPI_CLOCK_PHASE_RISING |
0425                 IF_SPI_BUS_MODE_DELAY_METHOD_TIMED |
0426                 IF_SPI_BUS_MODE_16_BIT_ADDRESS_16_BIT_DATA);
0427     if (err)
0428         return err;
0429     card->spu_port_delay = 1000;
0430     card->spu_reg_delay = 1000;
0431     err = spu_read_u32(card, IF_SPI_DELAY_READ_REG, &delay);
0432     if (err)
0433         return err;
0434     card->spu_port_delay = delay & 0x0000ffff;
0435     card->spu_reg_delay = (delay & 0xffff0000) >> 16;
0436 
0437     /* If dummy clock delay mode has been requested, switch to it now */
0438     if (use_dummy_writes) {
0439         card->use_dummy_writes = 1;
0440         err = spu_set_bus_mode(card,
0441                 IF_SPI_BUS_MODE_SPI_CLOCK_PHASE_RISING |
0442                 IF_SPI_BUS_MODE_DELAY_METHOD_DUMMY_CLOCK |
0443                 IF_SPI_BUS_MODE_16_BIT_ADDRESS_16_BIT_DATA);
0444         if (err)
0445             return err;
0446     }
0447 
0448     lbs_deb_spi("Initialized SPU unit. "
0449             "spu_port_delay=0x%04lx, spu_reg_delay=0x%04lx\n",
0450             card->spu_port_delay, card->spu_reg_delay);
0451     return err;
0452 }
0453 
0454 /*
0455  * Firmware Loading
0456  */
0457 
0458 static int if_spi_prog_helper_firmware(struct if_spi_card *card,
0459                     const struct firmware *firmware)
0460 {
0461     int err = 0;
0462     int bytes_remaining;
0463     const u8 *fw;
0464     u8 temp[HELPER_FW_LOAD_CHUNK_SZ];
0465 
0466     err = spu_set_interrupt_mode(card, 1, 0);
0467     if (err)
0468         goto out;
0469 
0470     bytes_remaining = firmware->size;
0471     fw = firmware->data;
0472 
0473     /* Load helper firmware image */
0474     while (bytes_remaining > 0) {
0475         /*
0476          * Scratch pad 1 should contain the number of bytes we
0477          * want to download to the firmware
0478          */
0479         err = spu_write_u16(card, IF_SPI_SCRATCH_1_REG,
0480                     HELPER_FW_LOAD_CHUNK_SZ);
0481         if (err)
0482             goto out;
0483 
0484         err = spu_wait_for_u16(card, IF_SPI_HOST_INT_STATUS_REG,
0485                     IF_SPI_HIST_CMD_DOWNLOAD_RDY,
0486                     IF_SPI_HIST_CMD_DOWNLOAD_RDY);
0487         if (err)
0488             goto out;
0489 
0490         /*
0491          * Feed the data into the command read/write port reg
0492          * in chunks of 64 bytes
0493          */
0494         memset(temp, 0, sizeof(temp));
0495         memcpy(temp, fw,
0496                min(bytes_remaining, HELPER_FW_LOAD_CHUNK_SZ));
0497         mdelay(10);
0498         err = spu_write(card, IF_SPI_CMD_RDWRPORT_REG,
0499                     temp, HELPER_FW_LOAD_CHUNK_SZ);
0500         if (err)
0501             goto out;
0502 
0503         /* Interrupt the boot code */
0504         err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
0505         if (err)
0506             goto out;
0507         err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG,
0508                        IF_SPI_CIC_CMD_DOWNLOAD_OVER);
0509         if (err)
0510             goto out;
0511         bytes_remaining -= HELPER_FW_LOAD_CHUNK_SZ;
0512         fw += HELPER_FW_LOAD_CHUNK_SZ;
0513     }
0514 
0515     /*
0516      * Once the helper / single stage firmware download is complete,
0517      * write 0 to scratch pad 1 and interrupt the
0518      * bootloader. This completes the helper download.
0519      */
0520     err = spu_write_u16(card, IF_SPI_SCRATCH_1_REG, FIRMWARE_DNLD_OK);
0521     if (err)
0522         goto out;
0523     err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
0524     if (err)
0525         goto out;
0526     err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG,
0527                 IF_SPI_CIC_CMD_DOWNLOAD_OVER);
0528 out:
0529     if (err)
0530         pr_err("failed to load helper firmware (err=%d)\n", err);
0531 
0532     return err;
0533 }
0534 
0535 /*
0536  * Returns the length of the next packet the firmware expects us to send.
0537  * Sets crc_err if the previous transfer had a CRC error.
0538  */
0539 static int if_spi_prog_main_firmware_check_len(struct if_spi_card *card,
0540                         int *crc_err)
0541 {
0542     u16 len;
0543     int err = 0;
0544 
0545     /*
0546      * wait until the host interrupt status register indicates
0547      * that we are ready to download
0548      */
0549     err = spu_wait_for_u16(card, IF_SPI_HOST_INT_STATUS_REG,
0550                 IF_SPI_HIST_CMD_DOWNLOAD_RDY,
0551                 IF_SPI_HIST_CMD_DOWNLOAD_RDY);
0552     if (err) {
0553         pr_err("timed out waiting for host_int_status\n");
0554         return err;
0555     }
0556 
0557     /* Ask the device how many bytes of firmware it wants. */
0558     err = spu_read_u16(card, IF_SPI_SCRATCH_1_REG, &len);
0559     if (err)
0560         return err;
0561 
0562     if (len > IF_SPI_CMD_BUF_SIZE) {
0563         pr_err("firmware load device requested a larger transfer than we are prepared to handle (len = %d)\n",
0564                len);
0565         return -EIO;
0566     }
0567     if (len & 0x1) {
0568         lbs_deb_spi("%s: crc error\n", __func__);
0569         len &= ~0x1;
0570         *crc_err = 1;
0571     } else
0572         *crc_err = 0;
0573 
0574     return len;
0575 }
0576 
0577 static int if_spi_prog_main_firmware(struct if_spi_card *card,
0578                     const struct firmware *firmware)
0579 {
0580     struct lbs_private *priv = card->priv;
0581     int len, prev_len;
0582     int bytes, crc_err = 0, err = 0;
0583     const u8 *fw;
0584     u16 num_crc_errs;
0585 
0586     err = spu_set_interrupt_mode(card, 1, 0);
0587     if (err)
0588         goto out;
0589 
0590     err = spu_wait_for_u16(card, IF_SPI_SCRATCH_1_REG, 0, 0);
0591     if (err) {
0592         netdev_err(priv->dev,
0593                "%s: timed out waiting for initial scratch reg = 0\n",
0594                __func__);
0595         goto out;
0596     }
0597 
0598     num_crc_errs = 0;
0599     prev_len = 0;
0600     bytes = firmware->size;
0601     fw = firmware->data;
0602     while ((len = if_spi_prog_main_firmware_check_len(card, &crc_err))) {
0603         if (len < 0) {
0604             err = len;
0605             goto out;
0606         }
0607         if (bytes < 0) {
0608             /*
0609              * If there are no more bytes left, we would normally
0610              * expect to have terminated with len = 0
0611              */
0612             netdev_err(priv->dev,
0613                    "Firmware load wants more bytes than we have to offer.\n");
0614             break;
0615         }
0616         if (crc_err) {
0617             /* Previous transfer failed. */
0618             if (++num_crc_errs > MAX_MAIN_FW_LOAD_CRC_ERR) {
0619                 pr_err("Too many CRC errors encountered in firmware load.\n");
0620                 err = -EIO;
0621                 goto out;
0622             }
0623         } else {
0624             /* Previous transfer succeeded. Advance counters. */
0625             bytes -= prev_len;
0626             fw += prev_len;
0627         }
0628         if (bytes < len) {
0629             memset(card->cmd_buffer, 0, len);
0630             memcpy(card->cmd_buffer, fw, bytes);
0631         } else
0632             memcpy(card->cmd_buffer, fw, len);
0633 
0634         err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
0635         if (err)
0636             goto out;
0637         err = spu_write(card, IF_SPI_CMD_RDWRPORT_REG,
0638                 card->cmd_buffer, len);
0639         if (err)
0640             goto out;
0641         err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG ,
0642                     IF_SPI_CIC_CMD_DOWNLOAD_OVER);
0643         if (err)
0644             goto out;
0645         prev_len = len;
0646     }
0647     if (bytes > prev_len) {
0648         pr_err("firmware load wants fewer bytes than we have to offer\n");
0649     }
0650 
0651     /* Confirm firmware download */
0652     err = spu_wait_for_u32(card, IF_SPI_SCRATCH_4_REG,
0653                     SUCCESSFUL_FW_DOWNLOAD_MAGIC);
0654     if (err) {
0655         pr_err("failed to confirm the firmware download\n");
0656         goto out;
0657     }
0658 
0659 out:
0660     if (err)
0661         pr_err("failed to load firmware (err=%d)\n", err);
0662 
0663     return err;
0664 }
0665 
0666 /*
0667  * SPI Transfer Thread
0668  *
0669  * The SPI worker handles all SPI transfers, so there is no need for a lock.
0670  */
0671 
0672 /* Move a command from the card to the host */
0673 static int if_spi_c2h_cmd(struct if_spi_card *card)
0674 {
0675     struct lbs_private *priv = card->priv;
0676     unsigned long flags;
0677     int err = 0;
0678     u16 len;
0679     u8 i;
0680 
0681     /*
0682      * We need a buffer big enough to handle whatever people send to
0683      * hw_host_to_card
0684      */
0685     BUILD_BUG_ON(IF_SPI_CMD_BUF_SIZE < LBS_CMD_BUFFER_SIZE);
0686     BUILD_BUG_ON(IF_SPI_CMD_BUF_SIZE < LBS_UPLD_SIZE);
0687 
0688     /*
0689      * It's just annoying if the buffer size isn't a multiple of 4, because
0690      * then we might have len < IF_SPI_CMD_BUF_SIZE but
0691      * ALIGN(len, 4) > IF_SPI_CMD_BUF_SIZE
0692      */
0693     BUILD_BUG_ON(IF_SPI_CMD_BUF_SIZE % 4 != 0);
0694 
0695     /* How many bytes are there to read? */
0696     err = spu_read_u16(card, IF_SPI_SCRATCH_2_REG, &len);
0697     if (err)
0698         goto out;
0699     if (!len) {
0700         netdev_err(priv->dev, "%s: error: card has no data for host\n",
0701                __func__);
0702         err = -EINVAL;
0703         goto out;
0704     } else if (len > IF_SPI_CMD_BUF_SIZE) {
0705         netdev_err(priv->dev,
0706                "%s: error: response packet too large: %d bytes, but maximum is %d\n",
0707                __func__, len, IF_SPI_CMD_BUF_SIZE);
0708         err = -EINVAL;
0709         goto out;
0710     }
0711 
0712     /* Read the data from the WLAN module into our command buffer */
0713     err = spu_read(card, IF_SPI_CMD_RDWRPORT_REG,
0714                 card->cmd_buffer, ALIGN(len, 4));
0715     if (err)
0716         goto out;
0717 
0718     spin_lock_irqsave(&priv->driver_lock, flags);
0719     i = (priv->resp_idx == 0) ? 1 : 0;
0720     BUG_ON(priv->resp_len[i]);
0721     priv->resp_len[i] = len;
0722     memcpy(priv->resp_buf[i], card->cmd_buffer, len);
0723     lbs_notify_command_response(priv, i);
0724     spin_unlock_irqrestore(&priv->driver_lock, flags);
0725 
0726 out:
0727     if (err)
0728         netdev_err(priv->dev, "%s: err=%d\n", __func__, err);
0729 
0730     return err;
0731 }
0732 
0733 /* Move data from the card to the host */
0734 static int if_spi_c2h_data(struct if_spi_card *card)
0735 {
0736     struct lbs_private *priv = card->priv;
0737     struct sk_buff *skb;
0738     char *data;
0739     u16 len;
0740     int err = 0;
0741 
0742     /* How many bytes are there to read? */
0743     err = spu_read_u16(card, IF_SPI_SCRATCH_1_REG, &len);
0744     if (err)
0745         goto out;
0746     if (!len) {
0747         netdev_err(priv->dev, "%s: error: card has no data for host\n",
0748                __func__);
0749         err = -EINVAL;
0750         goto out;
0751     } else if (len > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
0752         netdev_err(priv->dev,
0753                "%s: error: card has %d bytes of data, but our maximum skb size is %zu\n",
0754                __func__, len, MRVDRV_ETH_RX_PACKET_BUFFER_SIZE);
0755         err = -EINVAL;
0756         goto out;
0757     }
0758 
0759     /* TODO: should we allocate a smaller skb if we have less data? */
0760     skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE);
0761     if (!skb) {
0762         err = -ENOBUFS;
0763         goto out;
0764     }
0765     skb_reserve(skb, IPFIELD_ALIGN_OFFSET);
0766     data = skb_put(skb, len);
0767 
0768     /* Read the data from the WLAN module into our skb... */
0769     err = spu_read(card, IF_SPI_DATA_RDWRPORT_REG, data, ALIGN(len, 4));
0770     if (err) {
0771         dev_kfree_skb(skb);
0772         goto out;
0773     }
0774 
0775     /* pass the SKB to libertas */
0776     err = lbs_process_rxed_packet(card->priv, skb);
0777     /* lbs_process_rxed_packet() consumes the skb */
0778 
0779 out:
0780     if (err)
0781         netdev_err(priv->dev, "%s: err=%d\n", __func__, err);
0782 
0783     return err;
0784 }
0785 
0786 /* Move data or a command from the host to the card. */
0787 static void if_spi_h2c(struct if_spi_card *card,
0788             struct if_spi_packet *packet, int type)
0789 {
0790     struct lbs_private *priv = card->priv;
0791     int err = 0;
0792     u16 port_reg;
0793 
0794     switch (type) {
0795     case MVMS_DAT:
0796         port_reg = IF_SPI_DATA_RDWRPORT_REG;
0797         break;
0798     case MVMS_CMD:
0799         port_reg = IF_SPI_CMD_RDWRPORT_REG;
0800         break;
0801     default:
0802         netdev_err(priv->dev, "can't transfer buffer of type %d\n",
0803                type);
0804         err = -EINVAL;
0805         goto out;
0806     }
0807 
0808     /* Write the data to the card */
0809     err = spu_write(card, port_reg, packet->buffer, packet->blen);
0810     if (err)
0811         goto out;
0812 
0813 out:
0814     kfree(packet);
0815 
0816     if (err)
0817         netdev_err(priv->dev, "%s: error %d\n", __func__, err);
0818 }
0819 
0820 /* Inform the host about a card event */
0821 static void if_spi_e2h(struct if_spi_card *card)
0822 {
0823     int err = 0;
0824     u32 cause;
0825     struct lbs_private *priv = card->priv;
0826 
0827     err = spu_read_u32(card, IF_SPI_SCRATCH_3_REG, &cause);
0828     if (err)
0829         goto out;
0830 
0831     /* re-enable the card event interrupt */
0832     spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG,
0833             ~IF_SPI_HICU_CARD_EVENT);
0834 
0835     /* generate a card interrupt */
0836     spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG, IF_SPI_CIC_HOST_EVENT);
0837 
0838     lbs_queue_event(priv, cause & 0xff);
0839 out:
0840     if (err)
0841         netdev_err(priv->dev, "%s: error %d\n", __func__, err);
0842 }
0843 
0844 static void if_spi_host_to_card_worker(struct work_struct *work)
0845 {
0846     int err;
0847     struct if_spi_card *card;
0848     u16 hiStatus;
0849     unsigned long flags;
0850     struct if_spi_packet *packet;
0851     struct lbs_private *priv;
0852 
0853     card = container_of(work, struct if_spi_card, packet_work);
0854     priv = card->priv;
0855 
0856     /*
0857      * Read the host interrupt status register to see what we
0858      * can do.
0859      */
0860     err = spu_read_u16(card, IF_SPI_HOST_INT_STATUS_REG,
0861                 &hiStatus);
0862     if (err) {
0863         netdev_err(priv->dev, "I/O error\n");
0864         goto err;
0865     }
0866 
0867     if (hiStatus & IF_SPI_HIST_CMD_UPLOAD_RDY) {
0868         err = if_spi_c2h_cmd(card);
0869         if (err)
0870             goto err;
0871     }
0872     if (hiStatus & IF_SPI_HIST_RX_UPLOAD_RDY) {
0873         err = if_spi_c2h_data(card);
0874         if (err)
0875             goto err;
0876     }
0877 
0878     /*
0879      * workaround: in PS mode, the card does not set the Command
0880      * Download Ready bit, but it sets TX Download Ready.
0881      */
0882     if (hiStatus & IF_SPI_HIST_CMD_DOWNLOAD_RDY ||
0883        (card->priv->psstate != PS_STATE_FULL_POWER &&
0884         (hiStatus & IF_SPI_HIST_TX_DOWNLOAD_RDY))) {
0885         /*
0886          * This means two things. First of all,
0887          * if there was a previous command sent, the card has
0888          * successfully received it.
0889          * Secondly, it is now ready to download another
0890          * command.
0891          */
0892         lbs_host_to_card_done(card->priv);
0893 
0894         /* Do we have any command packets from the host to send? */
0895         packet = NULL;
0896         spin_lock_irqsave(&card->buffer_lock, flags);
0897         if (!list_empty(&card->cmd_packet_list)) {
0898             packet = (struct if_spi_packet *)(card->
0899                     cmd_packet_list.next);
0900             list_del(&packet->list);
0901         }
0902         spin_unlock_irqrestore(&card->buffer_lock, flags);
0903 
0904         if (packet)
0905             if_spi_h2c(card, packet, MVMS_CMD);
0906     }
0907     if (hiStatus & IF_SPI_HIST_TX_DOWNLOAD_RDY) {
0908         /* Do we have any data packets from the host to send? */
0909         packet = NULL;
0910         spin_lock_irqsave(&card->buffer_lock, flags);
0911         if (!list_empty(&card->data_packet_list)) {
0912             packet = (struct if_spi_packet *)(card->
0913                     data_packet_list.next);
0914             list_del(&packet->list);
0915         }
0916         spin_unlock_irqrestore(&card->buffer_lock, flags);
0917 
0918         if (packet)
0919             if_spi_h2c(card, packet, MVMS_DAT);
0920     }
0921     if (hiStatus & IF_SPI_HIST_CARD_EVENT)
0922         if_spi_e2h(card);
0923 
0924 err:
0925     if (err)
0926         netdev_err(priv->dev, "%s: got error %d\n", __func__, err);
0927 }
0928 
0929 /*
0930  * Host to Card
0931  *
0932  * Called from Libertas to transfer some data to the WLAN device
0933  * We can't sleep here.
0934  */
0935 static int if_spi_host_to_card(struct lbs_private *priv,
0936                 u8 type, u8 *buf, u16 nb)
0937 {
0938     int err = 0;
0939     unsigned long flags;
0940     struct if_spi_card *card = priv->card;
0941     struct if_spi_packet *packet;
0942     u16 blen;
0943 
0944     if (nb == 0) {
0945         netdev_err(priv->dev, "%s: invalid size requested: %d\n",
0946                __func__, nb);
0947         err = -EINVAL;
0948         goto out;
0949     }
0950     blen = ALIGN(nb, 4);
0951     packet = kzalloc(sizeof(struct if_spi_packet) + blen, GFP_ATOMIC);
0952     if (!packet) {
0953         err = -ENOMEM;
0954         goto out;
0955     }
0956     packet->blen = blen;
0957     memcpy(packet->buffer, buf, nb);
0958     memset(packet->buffer + nb, 0, blen - nb);
0959 
0960     switch (type) {
0961     case MVMS_CMD:
0962         priv->dnld_sent = DNLD_CMD_SENT;
0963         spin_lock_irqsave(&card->buffer_lock, flags);
0964         list_add_tail(&packet->list, &card->cmd_packet_list);
0965         spin_unlock_irqrestore(&card->buffer_lock, flags);
0966         break;
0967     case MVMS_DAT:
0968         priv->dnld_sent = DNLD_DATA_SENT;
0969         spin_lock_irqsave(&card->buffer_lock, flags);
0970         list_add_tail(&packet->list, &card->data_packet_list);
0971         spin_unlock_irqrestore(&card->buffer_lock, flags);
0972         break;
0973     default:
0974         kfree(packet);
0975         netdev_err(priv->dev, "can't transfer buffer of type %d\n",
0976                type);
0977         err = -EINVAL;
0978         break;
0979     }
0980 
0981     /* Queue spi xfer work */
0982     queue_work(card->workqueue, &card->packet_work);
0983 out:
0984     return err;
0985 }
0986 
0987 /*
0988  * Host Interrupts
0989  *
0990  * Service incoming interrupts from the WLAN device. We can't sleep here, so
0991  * don't try to talk on the SPI bus, just queue the SPI xfer work.
0992  */
0993 static irqreturn_t if_spi_host_interrupt(int irq, void *dev_id)
0994 {
0995     struct if_spi_card *card = dev_id;
0996 
0997     queue_work(card->workqueue, &card->packet_work);
0998 
0999     return IRQ_HANDLED;
1000 }
1001 
1002 /*
1003  * SPI callbacks
1004  */
1005 
1006 static int if_spi_init_card(struct if_spi_card *card)
1007 {
1008     struct lbs_private *priv = card->priv;
1009     int err, i;
1010     u32 scratch;
1011     const struct firmware *helper = NULL;
1012     const struct firmware *mainfw = NULL;
1013 
1014     err = spu_init(card, card->pdata->use_dummy_writes);
1015     if (err)
1016         goto out;
1017     err = spu_get_chip_revision(card, &card->card_id, &card->card_rev);
1018     if (err)
1019         goto out;
1020 
1021     err = spu_read_u32(card, IF_SPI_SCRATCH_4_REG, &scratch);
1022     if (err)
1023         goto out;
1024     if (scratch == SUCCESSFUL_FW_DOWNLOAD_MAGIC)
1025         lbs_deb_spi("Firmware is already loaded for "
1026                 "Marvell WLAN 802.11 adapter\n");
1027     else {
1028         /* Check if we support this card */
1029         for (i = 0; i < ARRAY_SIZE(fw_table); i++) {
1030             if (card->card_id == fw_table[i].model)
1031                 break;
1032         }
1033         if (i == ARRAY_SIZE(fw_table)) {
1034             netdev_err(priv->dev, "Unsupported chip_id: 0x%02x\n",
1035                    card->card_id);
1036             err = -ENODEV;
1037             goto out;
1038         }
1039 
1040         err = lbs_get_firmware(&card->spi->dev, card->card_id,
1041                     &fw_table[0], &helper, &mainfw);
1042         if (err) {
1043             netdev_err(priv->dev, "failed to find firmware (%d)\n",
1044                    err);
1045             goto out;
1046         }
1047 
1048         lbs_deb_spi("Initializing FW for Marvell WLAN 802.11 adapter "
1049                 "(chip_id = 0x%04x, chip_rev = 0x%02x) "
1050                 "attached to SPI bus_num %d, chip_select %d. "
1051                 "spi->max_speed_hz=%d\n",
1052                 card->card_id, card->card_rev,
1053                 card->spi->master->bus_num,
1054                 card->spi->chip_select,
1055                 card->spi->max_speed_hz);
1056         err = if_spi_prog_helper_firmware(card, helper);
1057         if (err)
1058             goto out;
1059         err = if_spi_prog_main_firmware(card, mainfw);
1060         if (err)
1061             goto out;
1062         lbs_deb_spi("loaded FW for Marvell WLAN 802.11 adapter\n");
1063     }
1064 
1065     err = spu_set_interrupt_mode(card, 0, 1);
1066     if (err)
1067         goto out;
1068 
1069 out:
1070     return err;
1071 }
1072 
1073 static void if_spi_resume_worker(struct work_struct *work)
1074 {
1075     struct if_spi_card *card;
1076 
1077     card = container_of(work, struct if_spi_card, resume_work);
1078 
1079     if (card->suspended) {
1080         if (card->pdata->setup)
1081             card->pdata->setup(card->spi);
1082 
1083         /* Init card ... */
1084         if_spi_init_card(card);
1085 
1086         enable_irq(card->spi->irq);
1087 
1088         /* And resume it ... */
1089         lbs_resume(card->priv);
1090 
1091         card->suspended = 0;
1092     }
1093 }
1094 
1095 static int if_spi_probe(struct spi_device *spi)
1096 {
1097     struct if_spi_card *card;
1098     struct lbs_private *priv = NULL;
1099     struct libertas_spi_platform_data *pdata = dev_get_platdata(&spi->dev);
1100     int err = 0;
1101 
1102     if (!pdata) {
1103         err = -EINVAL;
1104         goto out;
1105     }
1106 
1107     if (pdata->setup) {
1108         err = pdata->setup(spi);
1109         if (err)
1110             goto out;
1111     }
1112 
1113     /* Allocate card structure to represent this specific device */
1114     card = kzalloc(sizeof(struct if_spi_card), GFP_KERNEL);
1115     if (!card) {
1116         err = -ENOMEM;
1117         goto teardown;
1118     }
1119     spi_set_drvdata(spi, card);
1120     card->pdata = pdata;
1121     card->spi = spi;
1122     card->prev_xfer_time = jiffies;
1123 
1124     INIT_LIST_HEAD(&card->cmd_packet_list);
1125     INIT_LIST_HEAD(&card->data_packet_list);
1126     spin_lock_init(&card->buffer_lock);
1127 
1128     /* Initialize the SPI Interface Unit */
1129 
1130     /* Firmware load */
1131     err = if_spi_init_card(card);
1132     if (err)
1133         goto free_card;
1134 
1135     /*
1136      * Register our card with libertas.
1137      * This will call alloc_etherdev.
1138      */
1139     priv = lbs_add_card(card, &spi->dev);
1140     if (IS_ERR(priv)) {
1141         err = PTR_ERR(priv);
1142         goto free_card;
1143     }
1144     card->priv = priv;
1145     priv->setup_fw_on_resume = 1;
1146     priv->card = card;
1147     priv->hw_host_to_card = if_spi_host_to_card;
1148     priv->enter_deep_sleep = NULL;
1149     priv->exit_deep_sleep = NULL;
1150     priv->reset_deep_sleep_wakeup = NULL;
1151     priv->fw_ready = 1;
1152 
1153     /* Initialize interrupt handling stuff. */
1154     card->workqueue = alloc_workqueue("libertas_spi", WQ_MEM_RECLAIM, 0);
1155     if (!card->workqueue) {
1156         err = -ENOMEM;
1157         goto remove_card;
1158     }
1159     INIT_WORK(&card->packet_work, if_spi_host_to_card_worker);
1160     INIT_WORK(&card->resume_work, if_spi_resume_worker);
1161 
1162     err = request_irq(spi->irq, if_spi_host_interrupt,
1163             IRQF_TRIGGER_FALLING, "libertas_spi", card);
1164     if (err) {
1165         pr_err("can't get host irq line-- request_irq failed\n");
1166         goto terminate_workqueue;
1167     }
1168 
1169     /*
1170      * Start the card.
1171      * This will call register_netdev, and we'll start
1172      * getting interrupts...
1173      */
1174     err = lbs_start_card(priv);
1175     if (err)
1176         goto release_irq;
1177 
1178     lbs_deb_spi("Finished initializing WLAN module.\n");
1179 
1180     /* successful exit */
1181     goto out;
1182 
1183 release_irq:
1184     free_irq(spi->irq, card);
1185 terminate_workqueue:
1186     destroy_workqueue(card->workqueue);
1187 remove_card:
1188     lbs_remove_card(priv); /* will call free_netdev */
1189 free_card:
1190     free_if_spi_card(card);
1191 teardown:
1192     if (pdata->teardown)
1193         pdata->teardown(spi);
1194 out:
1195     return err;
1196 }
1197 
1198 static void libertas_spi_remove(struct spi_device *spi)
1199 {
1200     struct if_spi_card *card = spi_get_drvdata(spi);
1201     struct lbs_private *priv = card->priv;
1202 
1203     lbs_deb_spi("libertas_spi_remove\n");
1204 
1205     cancel_work_sync(&card->resume_work);
1206 
1207     lbs_stop_card(priv);
1208     lbs_remove_card(priv); /* will call free_netdev */
1209 
1210     free_irq(spi->irq, card);
1211     destroy_workqueue(card->workqueue);
1212     if (card->pdata->teardown)
1213         card->pdata->teardown(spi);
1214     free_if_spi_card(card);
1215 }
1216 
1217 static int if_spi_suspend(struct device *dev)
1218 {
1219     struct spi_device *spi = to_spi_device(dev);
1220     struct if_spi_card *card = spi_get_drvdata(spi);
1221 
1222     if (!card->suspended) {
1223         lbs_suspend(card->priv);
1224         flush_workqueue(card->workqueue);
1225         disable_irq(spi->irq);
1226 
1227         if (card->pdata->teardown)
1228             card->pdata->teardown(spi);
1229         card->suspended = 1;
1230     }
1231 
1232     return 0;
1233 }
1234 
1235 static int if_spi_resume(struct device *dev)
1236 {
1237     struct spi_device *spi = to_spi_device(dev);
1238     struct if_spi_card *card = spi_get_drvdata(spi);
1239 
1240     /* Schedule delayed work */
1241     schedule_work(&card->resume_work);
1242 
1243     return 0;
1244 }
1245 
1246 static const struct dev_pm_ops if_spi_pm_ops = {
1247     .suspend    = if_spi_suspend,
1248     .resume     = if_spi_resume,
1249 };
1250 
1251 static struct spi_driver libertas_spi_driver = {
1252     .probe  = if_spi_probe,
1253     .remove = libertas_spi_remove,
1254     .driver = {
1255         .name   = "libertas_spi",
1256         .pm = &if_spi_pm_ops,
1257     },
1258 };
1259 
1260 /*
1261  * Module functions
1262  */
1263 
1264 static int __init if_spi_init_module(void)
1265 {
1266     int ret = 0;
1267 
1268     printk(KERN_INFO "libertas_spi: Libertas SPI driver\n");
1269     ret = spi_register_driver(&libertas_spi_driver);
1270 
1271     return ret;
1272 }
1273 
1274 static void __exit if_spi_exit_module(void)
1275 {
1276     spi_unregister_driver(&libertas_spi_driver);
1277 }
1278 
1279 module_init(if_spi_init_module);
1280 module_exit(if_spi_exit_module);
1281 
1282 MODULE_DESCRIPTION("Libertas SPI WLAN Driver");
1283 MODULE_AUTHOR("Andrey Yurovsky <andrey@cozybit.com>, "
1284           "Colin McCabe <colin@cozybit.com>");
1285 MODULE_LICENSE("GPL");
1286 MODULE_ALIAS("spi:libertas_spi");