Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003 
0004   Driver for the Marvell 8385 based compact flash WLAN cards.
0005 
0006   (C) 2007 by Holger Schurig <hs4233@mail.mn-solutions.de>
0007 
0008 
0009 */
0010 
0011 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0012 
0013 #include <linux/module.h>
0014 #include <linux/slab.h>
0015 #include <linux/delay.h>
0016 #include <linux/moduleparam.h>
0017 #include <linux/firmware.h>
0018 #include <linux/netdevice.h>
0019 
0020 #include <pcmcia/cistpl.h>
0021 #include <pcmcia/ds.h>
0022 
0023 #include <linux/io.h>
0024 
0025 #define DRV_NAME "libertas_cs"
0026 
0027 #include "decl.h"
0028 #include "defs.h"
0029 #include "dev.h"
0030 
0031 
0032 /********************************************************************/
0033 /* Module stuff                                                     */
0034 /********************************************************************/
0035 
0036 MODULE_AUTHOR("Holger Schurig <hs4233@mail.mn-solutions.de>");
0037 MODULE_DESCRIPTION("Driver for Marvell 83xx compact flash WLAN cards");
0038 MODULE_LICENSE("GPL");
0039 
0040 
0041 
0042 /********************************************************************/
0043 /* Data structures                                                  */
0044 /********************************************************************/
0045 
0046 struct if_cs_card {
0047     struct pcmcia_device *p_dev;
0048     struct lbs_private *priv;
0049     void __iomem *iobase;
0050     bool align_regs;
0051     u32 model;
0052 };
0053 
0054 
0055 enum {
0056     MODEL_UNKNOWN = 0x00,
0057     MODEL_8305 = 0x01,
0058     MODEL_8381 = 0x02,
0059     MODEL_8385 = 0x03
0060 };
0061 
0062 static const struct lbs_fw_table fw_table[] = {
0063     { MODEL_8305, "libertas/cf8305.bin", NULL },
0064     { MODEL_8305, "libertas_cs_helper.fw", NULL },
0065     { MODEL_8381, "libertas/cf8381_helper.bin", "libertas/cf8381.bin" },
0066     { MODEL_8381, "libertas_cs_helper.fw", "libertas_cs.fw" },
0067     { MODEL_8385, "libertas/cf8385_helper.bin", "libertas/cf8385.bin" },
0068     { MODEL_8385, "libertas_cs_helper.fw", "libertas_cs.fw" },
0069     { 0, NULL, NULL }
0070 };
0071 MODULE_FIRMWARE("libertas/cf8305.bin");
0072 MODULE_FIRMWARE("libertas/cf8381_helper.bin");
0073 MODULE_FIRMWARE("libertas/cf8381.bin");
0074 MODULE_FIRMWARE("libertas/cf8385_helper.bin");
0075 MODULE_FIRMWARE("libertas/cf8385.bin");
0076 MODULE_FIRMWARE("libertas_cs_helper.fw");
0077 MODULE_FIRMWARE("libertas_cs.fw");
0078 
0079 
0080 /********************************************************************/
0081 /* Hardware access                                                  */
0082 /********************************************************************/
0083 
0084 /* This define enables wrapper functions which allow you
0085    to dump all register accesses. You normally won't this,
0086    except for development */
0087 /* #define DEBUG_IO */
0088 
0089 #ifdef DEBUG_IO
0090 static int debug_output = 0;
0091 #else
0092 /* This way the compiler optimizes the printk's away */
0093 #define debug_output 0
0094 #endif
0095 
0096 static inline unsigned int if_cs_read8(struct if_cs_card *card, uint reg)
0097 {
0098     unsigned int val = ioread8(card->iobase + reg);
0099     if (debug_output)
0100         printk(KERN_INFO "inb %08x<%02x\n", reg, val);
0101     return val;
0102 }
0103 static inline unsigned int if_cs_read16(struct if_cs_card *card, uint reg)
0104 {
0105     unsigned int val = ioread16(card->iobase + reg);
0106     if (debug_output)
0107         printk(KERN_INFO "inw %08x<%04x\n", reg, val);
0108     return val;
0109 }
0110 static inline void if_cs_read16_rep(
0111     struct if_cs_card *card,
0112     uint reg,
0113     void *buf,
0114     unsigned long count)
0115 {
0116     if (debug_output)
0117         printk(KERN_INFO "insw %08x<(0x%lx words)\n",
0118             reg, count);
0119     ioread16_rep(card->iobase + reg, buf, count);
0120 }
0121 
0122 static inline void if_cs_write8(struct if_cs_card *card, uint reg, u8 val)
0123 {
0124     if (debug_output)
0125         printk(KERN_INFO "outb %08x>%02x\n", reg, val);
0126     iowrite8(val, card->iobase + reg);
0127 }
0128 
0129 static inline void if_cs_write16(struct if_cs_card *card, uint reg, u16 val)
0130 {
0131     if (debug_output)
0132         printk(KERN_INFO "outw %08x>%04x\n", reg, val);
0133     iowrite16(val, card->iobase + reg);
0134 }
0135 
0136 static inline void if_cs_write16_rep(
0137     struct if_cs_card *card,
0138     uint reg,
0139     const void *buf,
0140     unsigned long count)
0141 {
0142     if (debug_output)
0143         printk(KERN_INFO "outsw %08x>(0x%lx words)\n",
0144             reg, count);
0145     iowrite16_rep(card->iobase + reg, buf, count);
0146 }
0147 
0148 
0149 /*
0150  * I know that polling/delaying is frowned upon. However, this procedure
0151  * with polling is needed while downloading the firmware. At this stage,
0152  * the hardware does unfortunately not create any interrupts.
0153  *
0154  * Fortunately, this function is never used once the firmware is in
0155  * the card. :-)
0156  *
0157  * As a reference, see the "Firmware Specification v5.1", page 18
0158  * and 19. I did not follow their suggested timing to the word,
0159  * but this works nice & fast anyway.
0160  */
0161 static int if_cs_poll_while_fw_download(struct if_cs_card *card, uint addr, u8 reg)
0162 {
0163     int i;
0164 
0165     for (i = 0; i < 100000; i++) {
0166         u8 val = if_cs_read8(card, addr);
0167         if (val == reg)
0168             return 0;
0169         udelay(5);
0170     }
0171     return -ETIME;
0172 }
0173 
0174 
0175 
0176 /*
0177  * First the bitmasks for the host/card interrupt/status registers:
0178  */
0179 #define IF_CS_BIT_TX            0x0001
0180 #define IF_CS_BIT_RX            0x0002
0181 #define IF_CS_BIT_COMMAND       0x0004
0182 #define IF_CS_BIT_RESP          0x0008
0183 #define IF_CS_BIT_EVENT         0x0010
0184 #define IF_CS_BIT_MASK          0x001f
0185 
0186 
0187 
0188 /*
0189  * It's not really clear to me what the host status register is for. It
0190  * needs to be set almost in union with "host int cause". The following
0191  * bits from above are used:
0192  *
0193  *   IF_CS_BIT_TX         driver downloaded a data packet
0194  *   IF_CS_BIT_RX         driver got a data packet
0195  *   IF_CS_BIT_COMMAND    driver downloaded a command
0196  *   IF_CS_BIT_RESP       not used (has some meaning with powerdown)
0197  *   IF_CS_BIT_EVENT      driver read a host event
0198  */
0199 #define IF_CS_HOST_STATUS       0x00000000
0200 
0201 /*
0202  * With the host int cause register can the host (that is, Linux) cause
0203  * an interrupt in the firmware, to tell the firmware about those events:
0204  *
0205  *   IF_CS_BIT_TX         a data packet has been downloaded
0206  *   IF_CS_BIT_RX         a received data packet has retrieved
0207  *   IF_CS_BIT_COMMAND    a firmware block or a command has been downloaded
0208  *   IF_CS_BIT_RESP       not used (has some meaning with powerdown)
0209  *   IF_CS_BIT_EVENT      a host event (link lost etc) has been retrieved
0210  */
0211 #define IF_CS_HOST_INT_CAUSE        0x00000002
0212 
0213 /*
0214  * The host int mask register is used to enable/disable interrupt.  However,
0215  * I have the suspicion that disabled interrupts are lost.
0216  */
0217 #define IF_CS_HOST_INT_MASK     0x00000004
0218 
0219 /*
0220  * Used to send or receive data packets:
0221  */
0222 #define IF_CS_WRITE         0x00000016
0223 #define IF_CS_WRITE_LEN         0x00000014
0224 #define IF_CS_READ          0x00000010
0225 #define IF_CS_READ_LEN          0x00000024
0226 
0227 /*
0228  * Used to send commands (and to send firmware block) and to
0229  * receive command responses:
0230  */
0231 #define IF_CS_CMD           0x0000001A
0232 #define IF_CS_CMD_LEN           0x00000018
0233 #define IF_CS_RESP          0x00000012
0234 #define IF_CS_RESP_LEN          0x00000030
0235 
0236 /*
0237  * The card status registers shows what the card/firmware actually
0238  * accepts:
0239  *
0240  *   IF_CS_BIT_TX        you may send a data packet
0241  *   IF_CS_BIT_RX        you may retrieve a data packet
0242  *   IF_CS_BIT_COMMAND   you may send a command
0243  *   IF_CS_BIT_RESP      you may retrieve a command response
0244  *   IF_CS_BIT_EVENT     the card has a event for use (link lost, snr low etc)
0245  *
0246  * When reading this register several times, you will get back the same
0247  * results --- with one exception: the IF_CS_BIT_EVENT clear itself
0248  * automatically.
0249  *
0250  * Not that we don't rely on BIT_RX,_BIT_RESP or BIT_EVENT because
0251  * we handle this via the card int cause register.
0252  */
0253 #define IF_CS_CARD_STATUS       0x00000020
0254 #define IF_CS_CARD_STATUS_MASK      0x7f00
0255 
0256 /*
0257  * The card int cause register is used by the card/firmware to notify us
0258  * about the following events:
0259  *
0260  *   IF_CS_BIT_TX        a data packet has successfully been sentx
0261  *   IF_CS_BIT_RX        a data packet has been received and can be retrieved
0262  *   IF_CS_BIT_COMMAND   not used
0263  *   IF_CS_BIT_RESP      the firmware has a command response for us
0264  *   IF_CS_BIT_EVENT     the card has a event for use (link lost, snr low etc)
0265  */
0266 #define IF_CS_CARD_INT_CAUSE        0x00000022
0267 
0268 /*
0269  * This is used to for handshaking with the card's bootloader/helper image
0270  * to synchronize downloading of firmware blocks.
0271  */
0272 #define IF_CS_SQ_READ_LOW       0x00000028
0273 #define IF_CS_SQ_HELPER_OK      0x10
0274 
0275 /*
0276  * The scratch register tells us ...
0277  *
0278  * IF_CS_SCRATCH_BOOT_OK     the bootloader runs
0279  * IF_CS_SCRATCH_HELPER_OK   the helper firmware already runs
0280  */
0281 #define IF_CS_SCRATCH           0x0000003F
0282 #define IF_CS_SCRATCH_BOOT_OK       0x00
0283 #define IF_CS_SCRATCH_HELPER_OK     0x5a
0284 
0285 /*
0286  * Used to detect ancient chips:
0287  */
0288 #define IF_CS_PRODUCT_ID        0x0000001C
0289 #define IF_CS_CF8385_B1_REV     0x12
0290 #define IF_CS_CF8381_B3_REV     0x04
0291 #define IF_CS_CF8305_B1_REV     0x03
0292 
0293 /*
0294  * Used to detect other cards than CF8385 since their revisions of silicon
0295  * doesn't match those from CF8385, eg. CF8381 B3 works with this driver.
0296  */
0297 #define CF8305_MANFID       0x02db
0298 #define CF8305_CARDID       0x8103
0299 #define CF8381_MANFID       0x02db
0300 #define CF8381_CARDID       0x6064
0301 #define CF8385_MANFID       0x02df
0302 #define CF8385_CARDID       0x8103
0303 
0304 /*
0305  * FIXME: just use the 'driver_info' field of 'struct pcmcia_device_id' when
0306  * that gets fixed.  Currently there's no way to access it from the probe hook.
0307  */
0308 static inline u32 get_model(u16 manf_id, u16 card_id)
0309 {
0310     /* NOTE: keep in sync with if_cs_ids */
0311     if (manf_id == CF8305_MANFID && card_id == CF8305_CARDID)
0312         return MODEL_8305;
0313     else if (manf_id == CF8381_MANFID && card_id == CF8381_CARDID)
0314         return MODEL_8381;
0315     else if (manf_id == CF8385_MANFID && card_id == CF8385_CARDID)
0316         return MODEL_8385;
0317     return MODEL_UNKNOWN;
0318 }
0319 
0320 /********************************************************************/
0321 /* I/O and interrupt handling                                       */
0322 /********************************************************************/
0323 
0324 static inline void if_cs_enable_ints(struct if_cs_card *card)
0325 {
0326     if_cs_write16(card, IF_CS_HOST_INT_MASK, 0);
0327 }
0328 
0329 static inline void if_cs_disable_ints(struct if_cs_card *card)
0330 {
0331     if_cs_write16(card, IF_CS_HOST_INT_MASK, IF_CS_BIT_MASK);
0332 }
0333 
0334 /*
0335  * Called from if_cs_host_to_card to send a command to the hardware
0336  */
0337 static int if_cs_send_cmd(struct lbs_private *priv, u8 *buf, u16 nb)
0338 {
0339     struct if_cs_card *card = (struct if_cs_card *)priv->card;
0340     int ret = -1;
0341     int loops = 0;
0342 
0343     if_cs_disable_ints(card);
0344 
0345     /* Is hardware ready? */
0346     while (1) {
0347         u16 status = if_cs_read16(card, IF_CS_CARD_STATUS);
0348         if (status & IF_CS_BIT_COMMAND)
0349             break;
0350         if (++loops > 100) {
0351             netdev_err(priv->dev, "card not ready for commands\n");
0352             goto done;
0353         }
0354         mdelay(1);
0355     }
0356 
0357     if_cs_write16(card, IF_CS_CMD_LEN, nb);
0358 
0359     if_cs_write16_rep(card, IF_CS_CMD, buf, nb / 2);
0360     /* Are we supposed to transfer an odd amount of bytes? */
0361     if (nb & 1)
0362         if_cs_write8(card, IF_CS_CMD, buf[nb-1]);
0363 
0364     /* "Assert the download over interrupt command in the Host
0365      * status register" */
0366     if_cs_write16(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND);
0367 
0368     /* "Assert the download over interrupt command in the Card
0369      * interrupt case register" */
0370     if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND);
0371     ret = 0;
0372 
0373 done:
0374     if_cs_enable_ints(card);
0375     return ret;
0376 }
0377 
0378 /*
0379  * Called from if_cs_host_to_card to send a data to the hardware
0380  */
0381 static void if_cs_send_data(struct lbs_private *priv, u8 *buf, u16 nb)
0382 {
0383     struct if_cs_card *card = (struct if_cs_card *)priv->card;
0384     u16 status;
0385 
0386     if_cs_disable_ints(card);
0387 
0388     status = if_cs_read16(card, IF_CS_CARD_STATUS);
0389     BUG_ON((status & IF_CS_BIT_TX) == 0);
0390 
0391     if_cs_write16(card, IF_CS_WRITE_LEN, nb);
0392 
0393     /* write even number of bytes, then odd byte if necessary */
0394     if_cs_write16_rep(card, IF_CS_WRITE, buf, nb / 2);
0395     if (nb & 1)
0396         if_cs_write8(card, IF_CS_WRITE, buf[nb-1]);
0397 
0398     if_cs_write16(card, IF_CS_HOST_STATUS, IF_CS_BIT_TX);
0399     if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_TX);
0400     if_cs_enable_ints(card);
0401 }
0402 
0403 /*
0404  * Get the command result out of the card.
0405  */
0406 static int if_cs_receive_cmdres(struct lbs_private *priv, u8 *data, u32 *len)
0407 {
0408     unsigned long flags;
0409     int ret = -1;
0410     u16 status;
0411 
0412     /* is hardware ready? */
0413     status = if_cs_read16(priv->card, IF_CS_CARD_STATUS);
0414     if ((status & IF_CS_BIT_RESP) == 0) {
0415         netdev_err(priv->dev, "no cmd response in card\n");
0416         *len = 0;
0417         goto out;
0418     }
0419 
0420     *len = if_cs_read16(priv->card, IF_CS_RESP_LEN);
0421     if ((*len == 0) || (*len > LBS_CMD_BUFFER_SIZE)) {
0422         netdev_err(priv->dev,
0423                "card cmd buffer has invalid # of bytes (%d)\n",
0424                *len);
0425         goto out;
0426     }
0427 
0428     /* read even number of bytes, then odd byte if necessary */
0429     if_cs_read16_rep(priv->card, IF_CS_RESP, data, *len/sizeof(u16));
0430     if (*len & 1)
0431         data[*len-1] = if_cs_read8(priv->card, IF_CS_RESP);
0432 
0433     /* This is a workaround for a firmware that reports too much
0434      * bytes */
0435     *len -= 8;
0436     ret = 0;
0437 
0438     /* Clear this flag again */
0439     spin_lock_irqsave(&priv->driver_lock, flags);
0440     priv->dnld_sent = DNLD_RES_RECEIVED;
0441     spin_unlock_irqrestore(&priv->driver_lock, flags);
0442 
0443 out:
0444     return ret;
0445 }
0446 
0447 static struct sk_buff *if_cs_receive_data(struct lbs_private *priv)
0448 {
0449     struct sk_buff *skb = NULL;
0450     u16 len;
0451     u8 *data;
0452 
0453     len = if_cs_read16(priv->card, IF_CS_READ_LEN);
0454     if (len == 0 || len > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
0455         netdev_err(priv->dev,
0456                "card data buffer has invalid # of bytes (%d)\n",
0457                len);
0458         priv->dev->stats.rx_dropped++;
0459         goto dat_err;
0460     }
0461 
0462     skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + 2);
0463     if (!skb)
0464         goto out;
0465     skb_put(skb, len);
0466     skb_reserve(skb, 2);/* 16 byte align */
0467     data = skb->data;
0468 
0469     /* read even number of bytes, then odd byte if necessary */
0470     if_cs_read16_rep(priv->card, IF_CS_READ, data, len/sizeof(u16));
0471     if (len & 1)
0472         data[len-1] = if_cs_read8(priv->card, IF_CS_READ);
0473 
0474 dat_err:
0475     if_cs_write16(priv->card, IF_CS_HOST_STATUS, IF_CS_BIT_RX);
0476     if_cs_write16(priv->card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_RX);
0477 
0478 out:
0479     return skb;
0480 }
0481 
0482 static irqreturn_t if_cs_interrupt(int irq, void *data)
0483 {
0484     struct if_cs_card *card = data;
0485     struct lbs_private *priv = card->priv;
0486     u16 cause;
0487 
0488     /* Ask card interrupt cause register if there is something for us */
0489     cause = if_cs_read16(card, IF_CS_CARD_INT_CAUSE);
0490     lbs_deb_cs("cause 0x%04x\n", cause);
0491 
0492     if (cause == 0) {
0493         /* Not for us */
0494         return IRQ_NONE;
0495     }
0496 
0497     if (cause == 0xffff) {
0498         /* Read in junk, the card has probably been removed */
0499         card->priv->surpriseremoved = 1;
0500         return IRQ_HANDLED;
0501     }
0502 
0503     if (cause & IF_CS_BIT_RX) {
0504         struct sk_buff *skb;
0505         lbs_deb_cs("rx packet\n");
0506         skb = if_cs_receive_data(priv);
0507         if (skb)
0508             lbs_process_rxed_packet(priv, skb);
0509     }
0510 
0511     if (cause & IF_CS_BIT_TX) {
0512         lbs_deb_cs("tx done\n");
0513         lbs_host_to_card_done(priv);
0514     }
0515 
0516     if (cause & IF_CS_BIT_RESP) {
0517         unsigned long flags;
0518         u8 i;
0519 
0520         lbs_deb_cs("cmd resp\n");
0521         spin_lock_irqsave(&priv->driver_lock, flags);
0522         i = (priv->resp_idx == 0) ? 1 : 0;
0523         spin_unlock_irqrestore(&priv->driver_lock, flags);
0524 
0525         BUG_ON(priv->resp_len[i]);
0526         if_cs_receive_cmdres(priv, priv->resp_buf[i],
0527             &priv->resp_len[i]);
0528 
0529         spin_lock_irqsave(&priv->driver_lock, flags);
0530         lbs_notify_command_response(priv, i);
0531         spin_unlock_irqrestore(&priv->driver_lock, flags);
0532     }
0533 
0534     if (cause & IF_CS_BIT_EVENT) {
0535         u16 status = if_cs_read16(priv->card, IF_CS_CARD_STATUS);
0536         if_cs_write16(priv->card, IF_CS_HOST_INT_CAUSE,
0537             IF_CS_BIT_EVENT);
0538         lbs_queue_event(priv, (status & IF_CS_CARD_STATUS_MASK) >> 8);
0539     }
0540 
0541     /* Clear interrupt cause */
0542     if_cs_write16(card, IF_CS_CARD_INT_CAUSE, cause & IF_CS_BIT_MASK);
0543 
0544     return IRQ_HANDLED;
0545 }
0546 
0547 
0548 
0549 
0550 /********************************************************************/
0551 /* Firmware                                                         */
0552 /********************************************************************/
0553 
0554 /*
0555  * Tries to program the helper firmware.
0556  *
0557  * Return 0 on success
0558  */
0559 static int if_cs_prog_helper(struct if_cs_card *card, const struct firmware *fw)
0560 {
0561     int ret = 0;
0562     int sent = 0;
0563     u8  scratch;
0564 
0565     /*
0566      * This is the only place where an unaligned register access happens on
0567      * the CF8305 card, therefore for the sake of speed of the driver, we do
0568      * the alignment correction here.
0569      */
0570     if (card->align_regs)
0571         scratch = if_cs_read16(card, IF_CS_SCRATCH) >> 8;
0572     else
0573         scratch = if_cs_read8(card, IF_CS_SCRATCH);
0574 
0575     /* "If the value is 0x5a, the firmware is already
0576      * downloaded successfully"
0577      */
0578     if (scratch == IF_CS_SCRATCH_HELPER_OK)
0579         goto done;
0580 
0581     /* "If the value is != 00, it is invalid value of register */
0582     if (scratch != IF_CS_SCRATCH_BOOT_OK) {
0583         ret = -ENODEV;
0584         goto done;
0585     }
0586 
0587     lbs_deb_cs("helper size %td\n", fw->size);
0588 
0589     /* "Set the 5 bytes of the helper image to 0" */
0590     /* Not needed, this contains an ARM branch instruction */
0591 
0592     for (;;) {
0593         /* "the number of bytes to send is 256" */
0594         int count = 256;
0595         int remain = fw->size - sent;
0596 
0597         if (remain < count)
0598             count = remain;
0599 
0600         /*
0601          * "write the number of bytes to be sent to the I/O Command
0602          * write length register"
0603          */
0604         if_cs_write16(card, IF_CS_CMD_LEN, count);
0605 
0606         /* "write this to I/O Command port register as 16 bit writes */
0607         if (count)
0608             if_cs_write16_rep(card, IF_CS_CMD,
0609                 &fw->data[sent],
0610                 count >> 1);
0611 
0612         /*
0613          * "Assert the download over interrupt command in the Host
0614          * status register"
0615          */
0616         if_cs_write8(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND);
0617 
0618         /*
0619          * "Assert the download over interrupt command in the Card
0620          * interrupt case register"
0621          */
0622         if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND);
0623 
0624         /*
0625          * "The host polls the Card Status register ... for 50 ms before
0626          * declaring a failure"
0627          */
0628         ret = if_cs_poll_while_fw_download(card, IF_CS_CARD_STATUS,
0629             IF_CS_BIT_COMMAND);
0630         if (ret < 0) {
0631             pr_err("can't download helper at 0x%x, ret %d\n",
0632                    sent, ret);
0633             goto done;
0634         }
0635 
0636         if (count == 0)
0637             break;
0638 
0639         sent += count;
0640     }
0641 
0642 done:
0643     return ret;
0644 }
0645 
0646 
0647 static int if_cs_prog_real(struct if_cs_card *card, const struct firmware *fw)
0648 {
0649     int ret = 0;
0650     int retry = 0;
0651     int len = 0;
0652     int sent;
0653 
0654     lbs_deb_cs("fw size %td\n", fw->size);
0655 
0656     ret = if_cs_poll_while_fw_download(card, IF_CS_SQ_READ_LOW,
0657         IF_CS_SQ_HELPER_OK);
0658     if (ret < 0) {
0659         pr_err("helper firmware doesn't answer\n");
0660         goto done;
0661     }
0662 
0663     for (sent = 0; sent < fw->size; sent += len) {
0664         len = if_cs_read16(card, IF_CS_SQ_READ_LOW);
0665         if (len & 1) {
0666             retry++;
0667             pr_info("odd, need to retry this firmware block\n");
0668         } else {
0669             retry = 0;
0670         }
0671 
0672         if (retry > 20) {
0673             pr_err("could not download firmware\n");
0674             ret = -ENODEV;
0675             goto done;
0676         }
0677         if (retry) {
0678             sent -= len;
0679         }
0680 
0681 
0682         if_cs_write16(card, IF_CS_CMD_LEN, len);
0683 
0684         if_cs_write16_rep(card, IF_CS_CMD,
0685             &fw->data[sent],
0686             (len+1) >> 1);
0687         if_cs_write8(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND);
0688         if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND);
0689 
0690         ret = if_cs_poll_while_fw_download(card, IF_CS_CARD_STATUS,
0691             IF_CS_BIT_COMMAND);
0692         if (ret < 0) {
0693             pr_err("can't download firmware at 0x%x\n", sent);
0694             goto done;
0695         }
0696     }
0697 
0698     ret = if_cs_poll_while_fw_download(card, IF_CS_SCRATCH, 0x5a);
0699     if (ret < 0)
0700         pr_err("firmware download failed\n");
0701 
0702 done:
0703     return ret;
0704 }
0705 
0706 static void if_cs_prog_firmware(struct lbs_private *priv, int ret,
0707                  const struct firmware *helper,
0708                  const struct firmware *mainfw)
0709 {
0710     struct if_cs_card *card = priv->card;
0711 
0712     if (ret) {
0713         pr_err("failed to find firmware (%d)\n", ret);
0714         return;
0715     }
0716 
0717     /* Load the firmware */
0718     ret = if_cs_prog_helper(card, helper);
0719     if (ret == 0 && (card->model != MODEL_8305))
0720         ret = if_cs_prog_real(card, mainfw);
0721     if (ret)
0722         return;
0723 
0724     /* Now actually get the IRQ */
0725     ret = request_irq(card->p_dev->irq, if_cs_interrupt,
0726         IRQF_SHARED, DRV_NAME, card);
0727     if (ret) {
0728         pr_err("error in request_irq\n");
0729         return;
0730     }
0731 
0732     /*
0733      * Clear any interrupt cause that happened while sending
0734      * firmware/initializing card
0735      */
0736     if_cs_write16(card, IF_CS_CARD_INT_CAUSE, IF_CS_BIT_MASK);
0737     if_cs_enable_ints(card);
0738 
0739     /* And finally bring the card up */
0740     priv->fw_ready = 1;
0741     if (lbs_start_card(priv) != 0) {
0742         pr_err("could not activate card\n");
0743         free_irq(card->p_dev->irq, card);
0744     }
0745 }
0746 
0747 
0748 /********************************************************************/
0749 /* Callback functions for libertas.ko                               */
0750 /********************************************************************/
0751 
0752 /* Send commands or data packets to the card */
0753 static int if_cs_host_to_card(struct lbs_private *priv,
0754     u8 type,
0755     u8 *buf,
0756     u16 nb)
0757 {
0758     int ret = -1;
0759 
0760     switch (type) {
0761     case MVMS_DAT:
0762         priv->dnld_sent = DNLD_DATA_SENT;
0763         if_cs_send_data(priv, buf, nb);
0764         ret = 0;
0765         break;
0766     case MVMS_CMD:
0767         priv->dnld_sent = DNLD_CMD_SENT;
0768         ret = if_cs_send_cmd(priv, buf, nb);
0769         break;
0770     default:
0771         netdev_err(priv->dev, "%s: unsupported type %d\n",
0772                __func__, type);
0773     }
0774 
0775     return ret;
0776 }
0777 
0778 
0779 static void if_cs_release(struct pcmcia_device *p_dev)
0780 {
0781     struct if_cs_card *card = p_dev->priv;
0782 
0783     free_irq(p_dev->irq, card);
0784     pcmcia_disable_device(p_dev);
0785     if (card->iobase)
0786         ioport_unmap(card->iobase);
0787 }
0788 
0789 
0790 static int if_cs_ioprobe(struct pcmcia_device *p_dev, void *priv_data)
0791 {
0792     p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
0793     p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_AUTO;
0794 
0795     if (p_dev->resource[1]->end) {
0796         pr_err("wrong CIS (check number of IO windows)\n");
0797         return -ENODEV;
0798     }
0799 
0800     /* This reserves IO space but doesn't actually enable it */
0801     return pcmcia_request_io(p_dev);
0802 }
0803 
0804 static int if_cs_probe(struct pcmcia_device *p_dev)
0805 {
0806     int ret = -ENOMEM;
0807     unsigned int prod_id;
0808     struct lbs_private *priv;
0809     struct if_cs_card *card;
0810 
0811     card = kzalloc(sizeof(struct if_cs_card), GFP_KERNEL);
0812     if (!card)
0813         goto out;
0814 
0815     card->p_dev = p_dev;
0816     p_dev->priv = card;
0817 
0818     p_dev->config_flags |= CONF_ENABLE_IRQ | CONF_AUTO_SET_IO;
0819 
0820     if (pcmcia_loop_config(p_dev, if_cs_ioprobe, NULL)) {
0821         pr_err("error in pcmcia_loop_config\n");
0822         goto out1;
0823     }
0824 
0825     /*
0826      * Allocate an interrupt line.  Note that this does not assign
0827      * a handler to the interrupt, unless the 'Handler' member of
0828      * the irq structure is initialized.
0829      */
0830     if (!p_dev->irq)
0831         goto out1;
0832 
0833     /* Initialize io access */
0834     card->iobase = ioport_map(p_dev->resource[0]->start,
0835                 resource_size(p_dev->resource[0]));
0836     if (!card->iobase) {
0837         pr_err("error in ioport_map\n");
0838         ret = -EIO;
0839         goto out1;
0840     }
0841 
0842     ret = pcmcia_enable_device(p_dev);
0843     if (ret) {
0844         pr_err("error in pcmcia_enable_device\n");
0845         goto out2;
0846     }
0847 
0848     /* Finally, report what we've done */
0849     lbs_deb_cs("irq %d, io %pR", p_dev->irq, p_dev->resource[0]);
0850 
0851     /*
0852      * Most of the libertas cards can do unaligned register access, but some
0853      * weird ones cannot. That's especially true for the CF8305 card.
0854      */
0855     card->align_regs = false;
0856 
0857     card->model = get_model(p_dev->manf_id, p_dev->card_id);
0858     if (card->model == MODEL_UNKNOWN) {
0859         pr_err("unsupported manf_id 0x%04x / card_id 0x%04x\n",
0860                p_dev->manf_id, p_dev->card_id);
0861         ret = -ENODEV;
0862         goto out2;
0863     }
0864 
0865     /* Check if we have a current silicon */
0866     prod_id = if_cs_read8(card, IF_CS_PRODUCT_ID);
0867     if (card->model == MODEL_8305) {
0868         card->align_regs = true;
0869         if (prod_id < IF_CS_CF8305_B1_REV) {
0870             pr_err("8305 rev B0 and older are not supported\n");
0871             ret = -ENODEV;
0872             goto out2;
0873         }
0874     }
0875 
0876     if ((card->model == MODEL_8381) && prod_id < IF_CS_CF8381_B3_REV) {
0877         pr_err("8381 rev B2 and older are not supported\n");
0878         ret = -ENODEV;
0879         goto out2;
0880     }
0881 
0882     if ((card->model == MODEL_8385) && prod_id < IF_CS_CF8385_B1_REV) {
0883         pr_err("8385 rev B0 and older are not supported\n");
0884         ret = -ENODEV;
0885         goto out2;
0886     }
0887 
0888     /* Make this card known to the libertas driver */
0889     priv = lbs_add_card(card, &p_dev->dev);
0890     if (IS_ERR(priv)) {
0891         ret = PTR_ERR(priv);
0892         goto out2;
0893     }
0894 
0895     /* Set up fields in lbs_private */
0896     card->priv = priv;
0897     priv->card = card;
0898     priv->hw_host_to_card = if_cs_host_to_card;
0899     priv->enter_deep_sleep = NULL;
0900     priv->exit_deep_sleep = NULL;
0901     priv->reset_deep_sleep_wakeup = NULL;
0902 
0903     /* Get firmware */
0904     ret = lbs_get_firmware_async(priv, &p_dev->dev, card->model, fw_table,
0905                      if_cs_prog_firmware);
0906     if (ret) {
0907         pr_err("failed to find firmware (%d)\n", ret);
0908         goto out3;
0909     }
0910 
0911     goto out;
0912 
0913 out3:
0914     lbs_remove_card(priv);
0915 out2:
0916     ioport_unmap(card->iobase);
0917 out1:
0918     pcmcia_disable_device(p_dev);
0919 out:
0920     return ret;
0921 }
0922 
0923 
0924 static void if_cs_detach(struct pcmcia_device *p_dev)
0925 {
0926     struct if_cs_card *card = p_dev->priv;
0927 
0928     lbs_stop_card(card->priv);
0929     lbs_remove_card(card->priv);
0930     if_cs_disable_ints(card);
0931     if_cs_release(p_dev);
0932     kfree(card);
0933 }
0934 
0935 
0936 
0937 /********************************************************************/
0938 /* Module initialization                                            */
0939 /********************************************************************/
0940 
0941 static const struct pcmcia_device_id if_cs_ids[] = {
0942     PCMCIA_DEVICE_MANF_CARD(CF8305_MANFID, CF8305_CARDID),
0943     PCMCIA_DEVICE_MANF_CARD(CF8381_MANFID, CF8381_CARDID),
0944     PCMCIA_DEVICE_MANF_CARD(CF8385_MANFID, CF8385_CARDID),
0945     /* NOTE: keep in sync with get_model() */
0946     PCMCIA_DEVICE_NULL,
0947 };
0948 MODULE_DEVICE_TABLE(pcmcia, if_cs_ids);
0949 
0950 static struct pcmcia_driver lbs_driver = {
0951     .owner      = THIS_MODULE,
0952     .name       = DRV_NAME,
0953     .probe      = if_cs_probe,
0954     .remove     = if_cs_detach,
0955     .id_table       = if_cs_ids,
0956 };
0957 module_pcmcia_driver(lbs_driver);