Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | |
| 3 | Driver for the Marvell 8385 based compact flash WLAN cards. |
| 4 | |
| 5 | (C) 2007 by Holger Schurig <hs4233@mail.mn-solutions.de> |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 2 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program; see the file COPYING. If not, write to |
| 19 | the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor, |
| 20 | Boston, MA 02110-1301, USA. |
| 21 | |
| 22 | */ |
| 23 | |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/delay.h> |
| 26 | #include <linux/moduleparam.h> |
| 27 | #include <linux/firmware.h> |
| 28 | #include <linux/netdevice.h> |
| 29 | |
| 30 | #include <pcmcia/cs_types.h> |
| 31 | #include <pcmcia/cs.h> |
| 32 | #include <pcmcia/cistpl.h> |
| 33 | #include <pcmcia/ds.h> |
| 34 | |
| 35 | #include <linux/io.h> |
| 36 | |
| 37 | #define DRV_NAME "libertas_cs" |
| 38 | |
| 39 | #include "decl.h" |
| 40 | #include "defs.h" |
| 41 | #include "dev.h" |
| 42 | |
| 43 | |
| 44 | /********************************************************************/ |
| 45 | /* Module stuff */ |
| 46 | /********************************************************************/ |
| 47 | |
| 48 | MODULE_AUTHOR("Holger Schurig <hs4233@mail.mn-solutions.de>"); |
| 49 | MODULE_DESCRIPTION("Driver for Marvell 83xx compact flash WLAN cards"); |
| 50 | MODULE_LICENSE("GPL"); |
| 51 | |
| 52 | |
| 53 | |
| 54 | /********************************************************************/ |
| 55 | /* Data structures */ |
| 56 | /********************************************************************/ |
| 57 | |
| 58 | struct if_cs_card { |
| 59 | struct pcmcia_device *p_dev; |
Holger Schurig | 69f9032 | 2007-11-23 15:43:44 +0100 | [diff] [blame] | 60 | struct lbs_private *priv; |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 61 | void __iomem *iobase; |
| 62 | }; |
| 63 | |
| 64 | |
| 65 | |
| 66 | /********************************************************************/ |
| 67 | /* Hardware access */ |
| 68 | /********************************************************************/ |
| 69 | |
| 70 | /* This define enables wrapper functions which allow you |
| 71 | to dump all register accesses. You normally won't this, |
| 72 | except for development */ |
| 73 | /* #define DEBUG_IO */ |
| 74 | |
| 75 | #ifdef DEBUG_IO |
| 76 | static int debug_output = 0; |
| 77 | #else |
| 78 | /* This way the compiler optimizes the printk's away */ |
| 79 | #define debug_output 0 |
| 80 | #endif |
| 81 | |
| 82 | static inline unsigned int if_cs_read8(struct if_cs_card *card, uint reg) |
| 83 | { |
| 84 | unsigned int val = ioread8(card->iobase + reg); |
| 85 | if (debug_output) |
Holger Schurig | 7919b89 | 2008-04-01 14:50:43 +0200 | [diff] [blame] | 86 | printk(KERN_INFO "inb %08x<%02x\n", reg, val); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 87 | return val; |
| 88 | } |
| 89 | static inline unsigned int if_cs_read16(struct if_cs_card *card, uint reg) |
| 90 | { |
| 91 | unsigned int val = ioread16(card->iobase + reg); |
| 92 | if (debug_output) |
Holger Schurig | 7919b89 | 2008-04-01 14:50:43 +0200 | [diff] [blame] | 93 | printk(KERN_INFO "inw %08x<%04x\n", reg, val); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 94 | return val; |
| 95 | } |
| 96 | static inline void if_cs_read16_rep( |
| 97 | struct if_cs_card *card, |
| 98 | uint reg, |
| 99 | void *buf, |
| 100 | unsigned long count) |
| 101 | { |
| 102 | if (debug_output) |
Holger Schurig | 7919b89 | 2008-04-01 14:50:43 +0200 | [diff] [blame] | 103 | printk(KERN_INFO "insw %08x<(0x%lx words)\n", |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 104 | reg, count); |
| 105 | ioread16_rep(card->iobase + reg, buf, count); |
| 106 | } |
| 107 | |
| 108 | static inline void if_cs_write8(struct if_cs_card *card, uint reg, u8 val) |
| 109 | { |
| 110 | if (debug_output) |
Holger Schurig | 7919b89 | 2008-04-01 14:50:43 +0200 | [diff] [blame] | 111 | printk(KERN_INFO "outb %08x>%02x\n", reg, val); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 112 | iowrite8(val, card->iobase + reg); |
| 113 | } |
| 114 | |
| 115 | static inline void if_cs_write16(struct if_cs_card *card, uint reg, u16 val) |
| 116 | { |
| 117 | if (debug_output) |
Holger Schurig | 7919b89 | 2008-04-01 14:50:43 +0200 | [diff] [blame] | 118 | printk(KERN_INFO "outw %08x>%04x\n", reg, val); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 119 | iowrite16(val, card->iobase + reg); |
| 120 | } |
| 121 | |
| 122 | static inline void if_cs_write16_rep( |
| 123 | struct if_cs_card *card, |
| 124 | uint reg, |
David Woodhouse | 6dfff89 | 2008-05-23 18:37:51 +0100 | [diff] [blame] | 125 | const void *buf, |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 126 | unsigned long count) |
| 127 | { |
| 128 | if (debug_output) |
Holger Schurig | 7919b89 | 2008-04-01 14:50:43 +0200 | [diff] [blame] | 129 | printk(KERN_INFO "outsw %08x>(0x%lx words)\n", |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 130 | reg, count); |
| 131 | iowrite16_rep(card->iobase + reg, buf, count); |
| 132 | } |
| 133 | |
| 134 | |
| 135 | /* |
| 136 | * I know that polling/delaying is frowned upon. However, this procedure |
| 137 | * with polling is needed while downloading the firmware. At this stage, |
| 138 | * the hardware does unfortunately not create any interrupts. |
| 139 | * |
| 140 | * Fortunately, this function is never used once the firmware is in |
| 141 | * the card. :-) |
| 142 | * |
| 143 | * As a reference, see the "Firmware Specification v5.1", page 18 |
| 144 | * and 19. I did not follow their suggested timing to the word, |
| 145 | * but this works nice & fast anyway. |
| 146 | */ |
| 147 | static int if_cs_poll_while_fw_download(struct if_cs_card *card, uint addr, u8 reg) |
| 148 | { |
| 149 | int i; |
| 150 | |
Holger Schurig | 93416c8 | 2008-05-23 10:18:26 +0200 | [diff] [blame] | 151 | for (i = 0; i < 100000; i++) { |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 152 | u8 val = if_cs_read8(card, addr); |
| 153 | if (val == reg) |
| 154 | return i; |
Holger Schurig | 93416c8 | 2008-05-23 10:18:26 +0200 | [diff] [blame] | 155 | udelay(5); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 156 | } |
| 157 | return -ETIME; |
| 158 | } |
| 159 | |
| 160 | |
| 161 | |
Holger Schurig | 5314325 | 2008-06-05 13:07:04 +0200 | [diff] [blame] | 162 | /* |
| 163 | * First the bitmasks for the host/card interrupt/status registers: |
| 164 | */ |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame] | 165 | #define IF_CS_BIT_TX 0x0001 |
| 166 | #define IF_CS_BIT_RX 0x0002 |
| 167 | #define IF_CS_BIT_COMMAND 0x0004 |
| 168 | #define IF_CS_BIT_RESP 0x0008 |
| 169 | #define IF_CS_BIT_EVENT 0x0010 |
| 170 | #define IF_CS_BIT_MASK 0x001f |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 171 | |
Holger Schurig | 5314325 | 2008-06-05 13:07:04 +0200 | [diff] [blame] | 172 | |
| 173 | |
| 174 | /* |
| 175 | * It's not really clear to me what the host status register is for. It |
| 176 | * needs to be set almost in union with "host int cause". The following |
| 177 | * bits from above are used: |
| 178 | * |
| 179 | * IF_CS_BIT_TX driver downloaded a data packet |
| 180 | * IF_CS_BIT_RX driver got a data packet |
| 181 | * IF_CS_BIT_COMMAND driver downloaded a command |
| 182 | * IF_CS_BIT_RESP not used (has some meaning with powerdown) |
| 183 | * IF_CS_BIT_EVENT driver read a host event |
| 184 | */ |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame] | 185 | #define IF_CS_HOST_STATUS 0x00000000 |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 186 | |
Holger Schurig | 5314325 | 2008-06-05 13:07:04 +0200 | [diff] [blame] | 187 | /* |
| 188 | * With the host int cause register can the host (that is, Linux) cause |
| 189 | * an interrupt in the firmware, to tell the firmware about those events: |
| 190 | * |
| 191 | * IF_CS_BIT_TX a data packet has been downloaded |
| 192 | * IF_CS_BIT_RX a received data packet has retrieved |
| 193 | * IF_CS_BIT_COMMAND a firmware block or a command has been downloaded |
| 194 | * IF_CS_BIT_RESP not used (has some meaning with powerdown) |
| 195 | * IF_CS_BIT_EVENT a host event (link lost etc) has been retrieved |
| 196 | */ |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame] | 197 | #define IF_CS_HOST_INT_CAUSE 0x00000002 |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 198 | |
Holger Schurig | 5314325 | 2008-06-05 13:07:04 +0200 | [diff] [blame] | 199 | /* |
| 200 | * The host int mask register is used to enable/disable interrupt. However, |
| 201 | * I have the suspicion that disabled interrupts are lost. |
| 202 | */ |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame] | 203 | #define IF_CS_HOST_INT_MASK 0x00000004 |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 204 | |
Holger Schurig | 5314325 | 2008-06-05 13:07:04 +0200 | [diff] [blame] | 205 | /* |
| 206 | * Used to send or receive data packets: |
| 207 | */ |
Holger Schurig | a78a832 | 2008-06-05 13:09:50 +0200 | [diff] [blame] | 208 | #define IF_CS_WRITE 0x00000016 |
| 209 | #define IF_CS_WRITE_LEN 0x00000014 |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame] | 210 | #define IF_CS_READ 0x00000010 |
| 211 | #define IF_CS_READ_LEN 0x00000024 |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 212 | |
Holger Schurig | 5314325 | 2008-06-05 13:07:04 +0200 | [diff] [blame] | 213 | /* |
| 214 | * Used to send commands (and to send firmware block) and to |
| 215 | * receive command responses: |
| 216 | */ |
Holger Schurig | a78a832 | 2008-06-05 13:09:50 +0200 | [diff] [blame] | 217 | #define IF_CS_CMD 0x0000001A |
| 218 | #define IF_CS_CMD_LEN 0x00000018 |
| 219 | #define IF_CS_RESP 0x00000012 |
| 220 | #define IF_CS_RESP_LEN 0x00000030 |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 221 | |
Holger Schurig | 5314325 | 2008-06-05 13:07:04 +0200 | [diff] [blame] | 222 | /* |
| 223 | * The card status registers shows what the card/firmware actually |
| 224 | * accepts: |
| 225 | * |
| 226 | * IF_CS_BIT_TX you may send a data packet |
| 227 | * IF_CS_BIT_RX you may retrieve a data packet |
| 228 | * IF_CS_BIT_COMMAND you may send a command |
| 229 | * IF_CS_BIT_RESP you may retrieve a command response |
| 230 | * IF_CS_BIT_EVENT the card has a event for use (link lost, snr low etc) |
| 231 | * |
| 232 | * When reading this register several times, you will get back the same |
| 233 | * results --- with one exception: the IF_CS_BIT_EVENT clear itself |
| 234 | * automatically. |
| 235 | * |
| 236 | * Not that we don't rely on BIT_RX,_BIT_RESP or BIT_EVENT because |
| 237 | * we handle this via the card int cause register. |
| 238 | */ |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame] | 239 | #define IF_CS_CARD_STATUS 0x00000020 |
| 240 | #define IF_CS_CARD_STATUS_MASK 0x7f00 |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 241 | |
Holger Schurig | 5314325 | 2008-06-05 13:07:04 +0200 | [diff] [blame] | 242 | /* |
| 243 | * The card int cause register is used by the card/firmware to notify us |
| 244 | * about the following events: |
| 245 | * |
| 246 | * IF_CS_BIT_TX a data packet has successfully been sentx |
| 247 | * IF_CS_BIT_RX a data packet has been received and can be retrieved |
| 248 | * IF_CS_BIT_COMMAND not used |
| 249 | * IF_CS_BIT_RESP the firmware has a command response for us |
| 250 | * IF_CS_BIT_EVENT the card has a event for use (link lost, snr low etc) |
| 251 | */ |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame] | 252 | #define IF_CS_CARD_INT_CAUSE 0x00000022 |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 253 | |
Holger Schurig | 5314325 | 2008-06-05 13:07:04 +0200 | [diff] [blame] | 254 | /* |
| 255 | * This is used to for handshaking with the card's bootloader/helper image |
| 256 | * to synchronize downloading of firmware blocks. |
| 257 | */ |
Holger Schurig | a78a832 | 2008-06-05 13:09:50 +0200 | [diff] [blame] | 258 | #define IF_CS_SQ_READ_LOW 0x00000028 |
| 259 | #define IF_CS_SQ_HELPER_OK 0x10 |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 260 | |
Holger Schurig | 5314325 | 2008-06-05 13:07:04 +0200 | [diff] [blame] | 261 | /* |
| 262 | * The scratch register tells us ... |
| 263 | * |
| 264 | * IF_CS_SCRATCH_BOOT_OK the bootloader runs |
| 265 | * IF_CS_SCRATCH_HELPER_OK the helper firmware already runs |
| 266 | */ |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 267 | #define IF_CS_SCRATCH 0x0000003F |
Holger Schurig | 5314325 | 2008-06-05 13:07:04 +0200 | [diff] [blame] | 268 | #define IF_CS_SCRATCH_BOOT_OK 0x00 |
| 269 | #define IF_CS_SCRATCH_HELPER_OK 0x5a |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 270 | |
Holger Schurig | 4c55523 | 2008-06-05 13:08:35 +0200 | [diff] [blame] | 271 | /* |
| 272 | * Used to detect ancient chips: |
| 273 | */ |
| 274 | #define IF_CS_PRODUCT_ID 0x0000001C |
| 275 | #define IF_CS_CF8385_B1_REV 0x12 |
| 276 | |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 277 | |
| 278 | /********************************************************************/ |
Holger Schurig | 43d01c5 | 2008-05-26 12:50:23 +0200 | [diff] [blame] | 279 | /* I/O and interrupt handling */ |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 280 | /********************************************************************/ |
| 281 | |
Holger Schurig | 43d01c5 | 2008-05-26 12:50:23 +0200 | [diff] [blame] | 282 | static inline void if_cs_enable_ints(struct if_cs_card *card) |
| 283 | { |
| 284 | lbs_deb_enter(LBS_DEB_CS); |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame] | 285 | if_cs_write16(card, IF_CS_HOST_INT_MASK, 0); |
Holger Schurig | 43d01c5 | 2008-05-26 12:50:23 +0200 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | static inline void if_cs_disable_ints(struct if_cs_card *card) |
| 289 | { |
| 290 | lbs_deb_enter(LBS_DEB_CS); |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame] | 291 | if_cs_write16(card, IF_CS_HOST_INT_MASK, IF_CS_BIT_MASK); |
Holger Schurig | 43d01c5 | 2008-05-26 12:50:23 +0200 | [diff] [blame] | 292 | } |
| 293 | |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 294 | /* |
| 295 | * Called from if_cs_host_to_card to send a command to the hardware |
| 296 | */ |
Holger Schurig | 69f9032 | 2007-11-23 15:43:44 +0100 | [diff] [blame] | 297 | static int if_cs_send_cmd(struct lbs_private *priv, u8 *buf, u16 nb) |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 298 | { |
| 299 | struct if_cs_card *card = (struct if_cs_card *)priv->card; |
| 300 | int ret = -1; |
| 301 | int loops = 0; |
| 302 | |
| 303 | lbs_deb_enter(LBS_DEB_CS); |
Holger Schurig | 43d01c5 | 2008-05-26 12:50:23 +0200 | [diff] [blame] | 304 | if_cs_disable_ints(card); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 305 | |
| 306 | /* Is hardware ready? */ |
| 307 | while (1) { |
Holger Schurig | a78a832 | 2008-06-05 13:09:50 +0200 | [diff] [blame] | 308 | u16 status = if_cs_read16(card, IF_CS_CARD_STATUS); |
| 309 | if (status & IF_CS_BIT_COMMAND) |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 310 | break; |
| 311 | if (++loops > 100) { |
| 312 | lbs_pr_err("card not ready for commands\n"); |
| 313 | goto done; |
| 314 | } |
| 315 | mdelay(1); |
| 316 | } |
| 317 | |
Holger Schurig | a78a832 | 2008-06-05 13:09:50 +0200 | [diff] [blame] | 318 | if_cs_write16(card, IF_CS_CMD_LEN, nb); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 319 | |
Holger Schurig | a78a832 | 2008-06-05 13:09:50 +0200 | [diff] [blame] | 320 | if_cs_write16_rep(card, IF_CS_CMD, buf, nb / 2); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 321 | /* Are we supposed to transfer an odd amount of bytes? */ |
| 322 | if (nb & 1) |
Holger Schurig | a78a832 | 2008-06-05 13:09:50 +0200 | [diff] [blame] | 323 | if_cs_write8(card, IF_CS_CMD, buf[nb-1]); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 324 | |
| 325 | /* "Assert the download over interrupt command in the Host |
| 326 | * status register" */ |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame] | 327 | if_cs_write16(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 328 | |
| 329 | /* "Assert the download over interrupt command in the Card |
| 330 | * interrupt case register" */ |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame] | 331 | if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 332 | ret = 0; |
| 333 | |
| 334 | done: |
Holger Schurig | 43d01c5 | 2008-05-26 12:50:23 +0200 | [diff] [blame] | 335 | if_cs_enable_ints(card); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 336 | lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret); |
| 337 | return ret; |
| 338 | } |
| 339 | |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 340 | /* |
| 341 | * Called from if_cs_host_to_card to send a data to the hardware |
| 342 | */ |
Holger Schurig | 69f9032 | 2007-11-23 15:43:44 +0100 | [diff] [blame] | 343 | static void if_cs_send_data(struct lbs_private *priv, u8 *buf, u16 nb) |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 344 | { |
| 345 | struct if_cs_card *card = (struct if_cs_card *)priv->card; |
Holger Schurig | 43d01c5 | 2008-05-26 12:50:23 +0200 | [diff] [blame] | 346 | u16 status; |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 347 | |
| 348 | lbs_deb_enter(LBS_DEB_CS); |
Holger Schurig | 43d01c5 | 2008-05-26 12:50:23 +0200 | [diff] [blame] | 349 | if_cs_disable_ints(card); |
| 350 | |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame] | 351 | status = if_cs_read16(card, IF_CS_CARD_STATUS); |
| 352 | BUG_ON((status & IF_CS_BIT_TX) == 0); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 353 | |
Holger Schurig | a78a832 | 2008-06-05 13:09:50 +0200 | [diff] [blame] | 354 | if_cs_write16(card, IF_CS_WRITE_LEN, nb); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 355 | |
| 356 | /* write even number of bytes, then odd byte if necessary */ |
Holger Schurig | a78a832 | 2008-06-05 13:09:50 +0200 | [diff] [blame] | 357 | if_cs_write16_rep(card, IF_CS_WRITE, buf, nb / 2); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 358 | if (nb & 1) |
Holger Schurig | a78a832 | 2008-06-05 13:09:50 +0200 | [diff] [blame] | 359 | if_cs_write8(card, IF_CS_WRITE, buf[nb-1]); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 360 | |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame] | 361 | if_cs_write16(card, IF_CS_HOST_STATUS, IF_CS_BIT_TX); |
| 362 | if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_TX); |
Holger Schurig | 43d01c5 | 2008-05-26 12:50:23 +0200 | [diff] [blame] | 363 | if_cs_enable_ints(card); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 364 | |
| 365 | lbs_deb_leave(LBS_DEB_CS); |
| 366 | } |
| 367 | |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 368 | /* |
| 369 | * Get the command result out of the card. |
| 370 | */ |
Holger Schurig | 69f9032 | 2007-11-23 15:43:44 +0100 | [diff] [blame] | 371 | static int if_cs_receive_cmdres(struct lbs_private *priv, u8 *data, u32 *len) |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 372 | { |
Holger Schurig | 7919b89 | 2008-04-01 14:50:43 +0200 | [diff] [blame] | 373 | unsigned long flags; |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 374 | int ret = -1; |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame] | 375 | u16 status; |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 376 | |
| 377 | lbs_deb_enter(LBS_DEB_CS); |
| 378 | |
| 379 | /* is hardware ready? */ |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame] | 380 | status = if_cs_read16(priv->card, IF_CS_CARD_STATUS); |
| 381 | if ((status & IF_CS_BIT_RESP) == 0) { |
| 382 | lbs_pr_err("no cmd response in card\n"); |
| 383 | *len = 0; |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 384 | goto out; |
| 385 | } |
| 386 | |
Holger Schurig | a78a832 | 2008-06-05 13:09:50 +0200 | [diff] [blame] | 387 | *len = if_cs_read16(priv->card, IF_CS_RESP_LEN); |
Dan Williams | ddac452 | 2007-12-11 13:49:39 -0500 | [diff] [blame] | 388 | if ((*len == 0) || (*len > LBS_CMD_BUFFER_SIZE)) { |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 389 | lbs_pr_err("card cmd buffer has invalid # of bytes (%d)\n", *len); |
| 390 | goto out; |
| 391 | } |
| 392 | |
| 393 | /* read even number of bytes, then odd byte if necessary */ |
Holger Schurig | a78a832 | 2008-06-05 13:09:50 +0200 | [diff] [blame] | 394 | if_cs_read16_rep(priv->card, IF_CS_RESP, data, *len/sizeof(u16)); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 395 | if (*len & 1) |
Holger Schurig | a78a832 | 2008-06-05 13:09:50 +0200 | [diff] [blame] | 396 | data[*len-1] = if_cs_read8(priv->card, IF_CS_RESP); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 397 | |
Holger Schurig | 09d4fad | 2007-12-06 13:50:30 +0100 | [diff] [blame] | 398 | /* This is a workaround for a firmware that reports too much |
| 399 | * bytes */ |
| 400 | *len -= 8; |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 401 | ret = 0; |
Holger Schurig | 7919b89 | 2008-04-01 14:50:43 +0200 | [diff] [blame] | 402 | |
| 403 | /* Clear this flag again */ |
| 404 | spin_lock_irqsave(&priv->driver_lock, flags); |
| 405 | priv->dnld_sent = DNLD_RES_RECEIVED; |
| 406 | spin_unlock_irqrestore(&priv->driver_lock, flags); |
| 407 | |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 408 | out: |
| 409 | lbs_deb_leave_args(LBS_DEB_CS, "ret %d, len %d", ret, *len); |
| 410 | return ret; |
| 411 | } |
| 412 | |
Holger Schurig | 69f9032 | 2007-11-23 15:43:44 +0100 | [diff] [blame] | 413 | static struct sk_buff *if_cs_receive_data(struct lbs_private *priv) |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 414 | { |
| 415 | struct sk_buff *skb = NULL; |
| 416 | u16 len; |
| 417 | u8 *data; |
| 418 | |
| 419 | lbs_deb_enter(LBS_DEB_CS); |
| 420 | |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame] | 421 | len = if_cs_read16(priv->card, IF_CS_READ_LEN); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 422 | if (len == 0 || len > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) { |
| 423 | lbs_pr_err("card data buffer has invalid # of bytes (%d)\n", len); |
| 424 | priv->stats.rx_dropped++; |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 425 | goto dat_err; |
| 426 | } |
| 427 | |
Vladimir Davydov | f31ce76 | 2007-09-06 21:41:02 -0400 | [diff] [blame] | 428 | skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + 2); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 429 | if (!skb) |
| 430 | goto out; |
Vladimir Davydov | f31ce76 | 2007-09-06 21:41:02 -0400 | [diff] [blame] | 431 | skb_put(skb, len); |
| 432 | skb_reserve(skb, 2);/* 16 byte align */ |
| 433 | data = skb->data; |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 434 | |
| 435 | /* read even number of bytes, then odd byte if necessary */ |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame] | 436 | if_cs_read16_rep(priv->card, IF_CS_READ, data, len/sizeof(u16)); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 437 | if (len & 1) |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame] | 438 | data[len-1] = if_cs_read8(priv->card, IF_CS_READ); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 439 | |
| 440 | dat_err: |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame] | 441 | if_cs_write16(priv->card, IF_CS_HOST_STATUS, IF_CS_BIT_RX); |
| 442 | if_cs_write16(priv->card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_RX); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 443 | |
| 444 | out: |
| 445 | lbs_deb_leave_args(LBS_DEB_CS, "ret %p", skb); |
| 446 | return skb; |
| 447 | } |
| 448 | |
Holger Schurig | 7919b89 | 2008-04-01 14:50:43 +0200 | [diff] [blame] | 449 | static irqreturn_t if_cs_interrupt(int irq, void *data) |
| 450 | { |
| 451 | struct if_cs_card *card = data; |
| 452 | struct lbs_private *priv = card->priv; |
| 453 | u16 cause; |
| 454 | |
| 455 | lbs_deb_enter(LBS_DEB_CS); |
| 456 | |
Holger Schurig | 43d01c5 | 2008-05-26 12:50:23 +0200 | [diff] [blame] | 457 | /* Ask card interrupt cause register if there is something for us */ |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame] | 458 | cause = if_cs_read16(card, IF_CS_CARD_INT_CAUSE); |
Holger Schurig | 3073556 | 2008-06-05 13:06:15 +0200 | [diff] [blame] | 459 | lbs_deb_cs("cause 0x%04x\n", cause); |
| 460 | |
Holger Schurig | 7919b89 | 2008-04-01 14:50:43 +0200 | [diff] [blame] | 461 | if (cause == 0) { |
| 462 | /* Not for us */ |
| 463 | return IRQ_NONE; |
| 464 | } |
| 465 | |
| 466 | if (cause == 0xffff) { |
| 467 | /* Read in junk, the card has probably been removed */ |
| 468 | card->priv->surpriseremoved = 1; |
| 469 | return IRQ_HANDLED; |
| 470 | } |
| 471 | |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame] | 472 | if (cause & IF_CS_BIT_RX) { |
Holger Schurig | 7919b89 | 2008-04-01 14:50:43 +0200 | [diff] [blame] | 473 | struct sk_buff *skb; |
| 474 | lbs_deb_cs("rx packet\n"); |
| 475 | skb = if_cs_receive_data(priv); |
| 476 | if (skb) |
| 477 | lbs_process_rxed_packet(priv, skb); |
| 478 | } |
| 479 | |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame] | 480 | if (cause & IF_CS_BIT_TX) { |
Holger Schurig | 43d01c5 | 2008-05-26 12:50:23 +0200 | [diff] [blame] | 481 | lbs_deb_cs("tx done\n"); |
Holger Schurig | 7919b89 | 2008-04-01 14:50:43 +0200 | [diff] [blame] | 482 | lbs_host_to_card_done(priv); |
| 483 | } |
| 484 | |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame] | 485 | if (cause & IF_CS_BIT_RESP) { |
Holger Schurig | 7919b89 | 2008-04-01 14:50:43 +0200 | [diff] [blame] | 486 | unsigned long flags; |
| 487 | u8 i; |
| 488 | |
Holger Schurig | 43d01c5 | 2008-05-26 12:50:23 +0200 | [diff] [blame] | 489 | lbs_deb_cs("cmd resp\n"); |
Holger Schurig | 7919b89 | 2008-04-01 14:50:43 +0200 | [diff] [blame] | 490 | spin_lock_irqsave(&priv->driver_lock, flags); |
| 491 | i = (priv->resp_idx == 0) ? 1 : 0; |
| 492 | spin_unlock_irqrestore(&priv->driver_lock, flags); |
| 493 | |
| 494 | BUG_ON(priv->resp_len[i]); |
| 495 | if_cs_receive_cmdres(priv, priv->resp_buf[i], |
| 496 | &priv->resp_len[i]); |
| 497 | |
| 498 | spin_lock_irqsave(&priv->driver_lock, flags); |
| 499 | lbs_notify_command_response(priv, i); |
| 500 | spin_unlock_irqrestore(&priv->driver_lock, flags); |
| 501 | } |
| 502 | |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame] | 503 | if (cause & IF_CS_BIT_EVENT) { |
Holger Schurig | a78a832 | 2008-06-05 13:09:50 +0200 | [diff] [blame] | 504 | u16 status = if_cs_read16(priv->card, IF_CS_CARD_STATUS); |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame] | 505 | if_cs_write16(priv->card, IF_CS_HOST_INT_CAUSE, |
| 506 | IF_CS_BIT_EVENT); |
Holger Schurig | a78a832 | 2008-06-05 13:09:50 +0200 | [diff] [blame] | 507 | lbs_queue_event(priv, (status & IF_CS_CARD_STATUS_MASK) >> 8); |
Holger Schurig | 7919b89 | 2008-04-01 14:50:43 +0200 | [diff] [blame] | 508 | } |
| 509 | |
Holger Schurig | 3073556 | 2008-06-05 13:06:15 +0200 | [diff] [blame] | 510 | /* Clear interrupt cause */ |
| 511 | if_cs_write16(card, IF_CS_CARD_INT_CAUSE, cause & IF_CS_BIT_MASK); |
| 512 | |
Holger Schurig | 43d01c5 | 2008-05-26 12:50:23 +0200 | [diff] [blame] | 513 | lbs_deb_leave(LBS_DEB_CS); |
Holger Schurig | 7919b89 | 2008-04-01 14:50:43 +0200 | [diff] [blame] | 514 | return IRQ_HANDLED; |
| 515 | } |
| 516 | |
| 517 | |
| 518 | |
| 519 | |
| 520 | /********************************************************************/ |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 521 | /* Firmware */ |
| 522 | /********************************************************************/ |
| 523 | |
| 524 | /* |
| 525 | * Tries to program the helper firmware. |
| 526 | * |
| 527 | * Return 0 on success |
| 528 | */ |
| 529 | static int if_cs_prog_helper(struct if_cs_card *card) |
| 530 | { |
| 531 | int ret = 0; |
| 532 | int sent = 0; |
| 533 | u8 scratch; |
| 534 | const struct firmware *fw; |
| 535 | |
| 536 | lbs_deb_enter(LBS_DEB_CS); |
| 537 | |
| 538 | scratch = if_cs_read8(card, IF_CS_SCRATCH); |
| 539 | |
| 540 | /* "If the value is 0x5a, the firmware is already |
| 541 | * downloaded successfully" |
| 542 | */ |
Holger Schurig | 5314325 | 2008-06-05 13:07:04 +0200 | [diff] [blame] | 543 | if (scratch == IF_CS_SCRATCH_HELPER_OK) |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 544 | goto done; |
| 545 | |
| 546 | /* "If the value is != 00, it is invalid value of register */ |
Holger Schurig | 5314325 | 2008-06-05 13:07:04 +0200 | [diff] [blame] | 547 | if (scratch != IF_CS_SCRATCH_BOOT_OK) { |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 548 | ret = -ENODEV; |
| 549 | goto done; |
| 550 | } |
| 551 | |
| 552 | /* TODO: make firmware file configurable */ |
| 553 | ret = request_firmware(&fw, "libertas_cs_helper.fw", |
| 554 | &handle_to_dev(card->p_dev)); |
| 555 | if (ret) { |
| 556 | lbs_pr_err("can't load helper firmware\n"); |
| 557 | ret = -ENODEV; |
| 558 | goto done; |
| 559 | } |
Andrew Morton | 4ecd41b | 2007-08-21 02:15:45 -0700 | [diff] [blame] | 560 | lbs_deb_cs("helper size %td\n", fw->size); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 561 | |
| 562 | /* "Set the 5 bytes of the helper image to 0" */ |
| 563 | /* Not needed, this contains an ARM branch instruction */ |
| 564 | |
| 565 | for (;;) { |
| 566 | /* "the number of bytes to send is 256" */ |
| 567 | int count = 256; |
| 568 | int remain = fw->size - sent; |
| 569 | |
| 570 | if (remain < count) |
| 571 | count = remain; |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 572 | |
| 573 | /* "write the number of bytes to be sent to the I/O Command |
| 574 | * write length register" */ |
Holger Schurig | a78a832 | 2008-06-05 13:09:50 +0200 | [diff] [blame] | 575 | if_cs_write16(card, IF_CS_CMD_LEN, count); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 576 | |
| 577 | /* "write this to I/O Command port register as 16 bit writes */ |
| 578 | if (count) |
Holger Schurig | a78a832 | 2008-06-05 13:09:50 +0200 | [diff] [blame] | 579 | if_cs_write16_rep(card, IF_CS_CMD, |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 580 | &fw->data[sent], |
| 581 | count >> 1); |
| 582 | |
| 583 | /* "Assert the download over interrupt command in the Host |
| 584 | * status register" */ |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame] | 585 | if_cs_write8(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 586 | |
| 587 | /* "Assert the download over interrupt command in the Card |
| 588 | * interrupt case register" */ |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame] | 589 | if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 590 | |
| 591 | /* "The host polls the Card Status register ... for 50 ms before |
| 592 | declaring a failure */ |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame] | 593 | ret = if_cs_poll_while_fw_download(card, IF_CS_CARD_STATUS, |
| 594 | IF_CS_BIT_COMMAND); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 595 | if (ret < 0) { |
| 596 | lbs_pr_err("can't download helper at 0x%x, ret %d\n", |
| 597 | sent, ret); |
Adrian Bunk | 9a52028 | 2008-08-28 01:05:08 +0300 | [diff] [blame] | 598 | goto err_release; |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | if (count == 0) |
| 602 | break; |
| 603 | |
| 604 | sent += count; |
| 605 | } |
| 606 | |
Adrian Bunk | 9a52028 | 2008-08-28 01:05:08 +0300 | [diff] [blame] | 607 | err_release: |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 608 | release_firmware(fw); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 609 | done: |
| 610 | lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret); |
| 611 | return ret; |
| 612 | } |
| 613 | |
| 614 | |
| 615 | static int if_cs_prog_real(struct if_cs_card *card) |
| 616 | { |
| 617 | const struct firmware *fw; |
| 618 | int ret = 0; |
| 619 | int retry = 0; |
| 620 | int len = 0; |
| 621 | int sent; |
| 622 | |
| 623 | lbs_deb_enter(LBS_DEB_CS); |
| 624 | |
| 625 | /* TODO: make firmware file configurable */ |
| 626 | ret = request_firmware(&fw, "libertas_cs.fw", |
| 627 | &handle_to_dev(card->p_dev)); |
| 628 | if (ret) { |
| 629 | lbs_pr_err("can't load firmware\n"); |
| 630 | ret = -ENODEV; |
| 631 | goto done; |
| 632 | } |
Andrew Morton | 4ecd41b | 2007-08-21 02:15:45 -0700 | [diff] [blame] | 633 | lbs_deb_cs("fw size %td\n", fw->size); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 634 | |
Holger Schurig | a78a832 | 2008-06-05 13:09:50 +0200 | [diff] [blame] | 635 | ret = if_cs_poll_while_fw_download(card, IF_CS_SQ_READ_LOW, |
| 636 | IF_CS_SQ_HELPER_OK); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 637 | if (ret < 0) { |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 638 | lbs_pr_err("helper firmware doesn't answer\n"); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 639 | goto err_release; |
| 640 | } |
| 641 | |
| 642 | for (sent = 0; sent < fw->size; sent += len) { |
Holger Schurig | a78a832 | 2008-06-05 13:09:50 +0200 | [diff] [blame] | 643 | len = if_cs_read16(card, IF_CS_SQ_READ_LOW); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 644 | if (len & 1) { |
| 645 | retry++; |
| 646 | lbs_pr_info("odd, need to retry this firmware block\n"); |
| 647 | } else { |
| 648 | retry = 0; |
| 649 | } |
| 650 | |
| 651 | if (retry > 20) { |
| 652 | lbs_pr_err("could not download firmware\n"); |
| 653 | ret = -ENODEV; |
| 654 | goto err_release; |
| 655 | } |
| 656 | if (retry) { |
| 657 | sent -= len; |
| 658 | } |
| 659 | |
| 660 | |
Holger Schurig | a78a832 | 2008-06-05 13:09:50 +0200 | [diff] [blame] | 661 | if_cs_write16(card, IF_CS_CMD_LEN, len); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 662 | |
Holger Schurig | a78a832 | 2008-06-05 13:09:50 +0200 | [diff] [blame] | 663 | if_cs_write16_rep(card, IF_CS_CMD, |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 664 | &fw->data[sent], |
| 665 | (len+1) >> 1); |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame] | 666 | if_cs_write8(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND); |
| 667 | if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 668 | |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame] | 669 | ret = if_cs_poll_while_fw_download(card, IF_CS_CARD_STATUS, |
| 670 | IF_CS_BIT_COMMAND); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 671 | if (ret < 0) { |
| 672 | lbs_pr_err("can't download firmware at 0x%x\n", sent); |
| 673 | goto err_release; |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | ret = if_cs_poll_while_fw_download(card, IF_CS_SCRATCH, 0x5a); |
Adrian Bunk | 9a52028 | 2008-08-28 01:05:08 +0300 | [diff] [blame] | 678 | if (ret < 0) |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 679 | lbs_pr_err("firmware download failed\n"); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 680 | |
| 681 | err_release: |
| 682 | release_firmware(fw); |
| 683 | |
| 684 | done: |
| 685 | lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret); |
| 686 | return ret; |
| 687 | } |
| 688 | |
| 689 | |
| 690 | |
| 691 | /********************************************************************/ |
| 692 | /* Callback functions for libertas.ko */ |
| 693 | /********************************************************************/ |
| 694 | |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 695 | /* Send commands or data packets to the card */ |
Holger Schurig | 69f9032 | 2007-11-23 15:43:44 +0100 | [diff] [blame] | 696 | static int if_cs_host_to_card(struct lbs_private *priv, |
| 697 | u8 type, |
| 698 | u8 *buf, |
| 699 | u16 nb) |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 700 | { |
| 701 | int ret = -1; |
| 702 | |
| 703 | lbs_deb_enter_args(LBS_DEB_CS, "type %d, bytes %d", type, nb); |
| 704 | |
| 705 | switch (type) { |
| 706 | case MVMS_DAT: |
Ryan Mallon | 6f05cbe | 2007-09-06 21:30:32 -0400 | [diff] [blame] | 707 | priv->dnld_sent = DNLD_DATA_SENT; |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 708 | if_cs_send_data(priv, buf, nb); |
| 709 | ret = 0; |
| 710 | break; |
| 711 | case MVMS_CMD: |
Ryan Mallon | 6f05cbe | 2007-09-06 21:30:32 -0400 | [diff] [blame] | 712 | priv->dnld_sent = DNLD_CMD_SENT; |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 713 | ret = if_cs_send_cmd(priv, buf, nb); |
| 714 | break; |
| 715 | default: |
Harvey Harrison | c94c93d | 2008-07-28 23:01:34 -0700 | [diff] [blame] | 716 | lbs_pr_err("%s: unsupported type %d\n", __func__, type); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret); |
| 720 | return ret; |
| 721 | } |
| 722 | |
| 723 | |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 724 | /********************************************************************/ |
| 725 | /* Card Services */ |
| 726 | /********************************************************************/ |
| 727 | |
| 728 | /* |
| 729 | * After a card is removed, if_cs_release() will unregister the |
| 730 | * device, and release the PCMCIA configuration. If the device is |
| 731 | * still open, this will be postponed until it is closed. |
| 732 | */ |
| 733 | static void if_cs_release(struct pcmcia_device *p_dev) |
| 734 | { |
| 735 | struct if_cs_card *card = p_dev->priv; |
| 736 | |
| 737 | lbs_deb_enter(LBS_DEB_CS); |
| 738 | |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 739 | free_irq(p_dev->irq.AssignedIRQ, card); |
Holger Schurig | fdfb92e | 2008-01-25 14:15:48 +0100 | [diff] [blame] | 740 | pcmcia_disable_device(p_dev); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 741 | if (card->iobase) |
| 742 | ioport_unmap(card->iobase); |
| 743 | |
| 744 | lbs_deb_leave(LBS_DEB_CS); |
| 745 | } |
| 746 | |
| 747 | |
| 748 | /* |
| 749 | * This creates an "instance" of the driver, allocating local data |
| 750 | * structures for one device. The device is registered with Card |
| 751 | * Services. |
| 752 | * |
| 753 | * The dev_link structure is initialized, but we don't actually |
| 754 | * configure the card at this point -- we wait until we receive a card |
| 755 | * insertion event. |
| 756 | */ |
| 757 | static int if_cs_probe(struct pcmcia_device *p_dev) |
| 758 | { |
| 759 | int ret = -ENOMEM; |
Holger Schurig | 69f9032 | 2007-11-23 15:43:44 +0100 | [diff] [blame] | 760 | struct lbs_private *priv; |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 761 | struct if_cs_card *card; |
| 762 | /* CIS parsing */ |
| 763 | tuple_t tuple; |
| 764 | cisparse_t parse; |
| 765 | cistpl_cftable_entry_t *cfg = &parse.cftable_entry; |
| 766 | cistpl_io_t *io = &cfg->io; |
| 767 | u_char buf[64]; |
| 768 | |
| 769 | lbs_deb_enter(LBS_DEB_CS); |
| 770 | |
| 771 | card = kzalloc(sizeof(struct if_cs_card), GFP_KERNEL); |
| 772 | if (!card) { |
| 773 | lbs_pr_err("error in kzalloc\n"); |
| 774 | goto out; |
| 775 | } |
| 776 | card->p_dev = p_dev; |
| 777 | p_dev->priv = card; |
| 778 | |
| 779 | p_dev->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING; |
| 780 | p_dev->irq.Handler = NULL; |
| 781 | p_dev->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID; |
| 782 | |
| 783 | p_dev->conf.Attributes = 0; |
| 784 | p_dev->conf.IntType = INT_MEMORY_AND_IO; |
| 785 | |
| 786 | tuple.Attributes = 0; |
| 787 | tuple.TupleData = buf; |
| 788 | tuple.TupleDataMax = sizeof(buf); |
| 789 | tuple.TupleOffset = 0; |
| 790 | |
| 791 | tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY; |
| 792 | if ((ret = pcmcia_get_first_tuple(p_dev, &tuple)) != 0 || |
| 793 | (ret = pcmcia_get_tuple_data(p_dev, &tuple)) != 0 || |
| 794 | (ret = pcmcia_parse_tuple(p_dev, &tuple, &parse)) != 0) |
| 795 | { |
| 796 | lbs_pr_err("error in pcmcia_get_first_tuple etc\n"); |
| 797 | goto out1; |
| 798 | } |
| 799 | |
| 800 | p_dev->conf.ConfigIndex = cfg->index; |
| 801 | |
| 802 | /* Do we need to allocate an interrupt? */ |
| 803 | if (cfg->irq.IRQInfo1) { |
| 804 | p_dev->conf.Attributes |= CONF_ENABLE_IRQ; |
| 805 | } |
| 806 | |
| 807 | /* IO window settings */ |
| 808 | if (cfg->io.nwin != 1) { |
| 809 | lbs_pr_err("wrong CIS (check number of IO windows)\n"); |
| 810 | ret = -ENODEV; |
| 811 | goto out1; |
| 812 | } |
| 813 | p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO; |
| 814 | p_dev->io.BasePort1 = io->win[0].base; |
| 815 | p_dev->io.NumPorts1 = io->win[0].len; |
| 816 | |
| 817 | /* This reserves IO space but doesn't actually enable it */ |
| 818 | ret = pcmcia_request_io(p_dev, &p_dev->io); |
| 819 | if (ret) { |
| 820 | lbs_pr_err("error in pcmcia_request_io\n"); |
| 821 | goto out1; |
| 822 | } |
| 823 | |
| 824 | /* |
| 825 | * Allocate an interrupt line. Note that this does not assign |
| 826 | * a handler to the interrupt, unless the 'Handler' member of |
| 827 | * the irq structure is initialized. |
| 828 | */ |
| 829 | if (p_dev->conf.Attributes & CONF_ENABLE_IRQ) { |
| 830 | ret = pcmcia_request_irq(p_dev, &p_dev->irq); |
| 831 | if (ret) { |
| 832 | lbs_pr_err("error in pcmcia_request_irq\n"); |
| 833 | goto out1; |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | /* Initialize io access */ |
| 838 | card->iobase = ioport_map(p_dev->io.BasePort1, p_dev->io.NumPorts1); |
| 839 | if (!card->iobase) { |
| 840 | lbs_pr_err("error in ioport_map\n"); |
| 841 | ret = -EIO; |
| 842 | goto out1; |
| 843 | } |
| 844 | |
| 845 | /* |
| 846 | * This actually configures the PCMCIA socket -- setting up |
| 847 | * the I/O windows and the interrupt mapping, and putting the |
| 848 | * card and host interface into "Memory and IO" mode. |
| 849 | */ |
| 850 | ret = pcmcia_request_configuration(p_dev, &p_dev->conf); |
| 851 | if (ret) { |
| 852 | lbs_pr_err("error in pcmcia_request_configuration\n"); |
| 853 | goto out2; |
| 854 | } |
| 855 | |
| 856 | /* Finally, report what we've done */ |
| 857 | lbs_deb_cs("irq %d, io 0x%04x-0x%04x\n", |
| 858 | p_dev->irq.AssignedIRQ, p_dev->io.BasePort1, |
| 859 | p_dev->io.BasePort1 + p_dev->io.NumPorts1 - 1); |
| 860 | |
Holger Schurig | 4c55523 | 2008-06-05 13:08:35 +0200 | [diff] [blame] | 861 | /* Check if we have a current silicon */ |
| 862 | if (if_cs_read8(card, IF_CS_PRODUCT_ID) < IF_CS_CF8385_B1_REV) { |
| 863 | lbs_pr_err("old chips like 8385 rev B1 aren't supported\n"); |
| 864 | ret = -ENODEV; |
| 865 | goto out2; |
| 866 | } |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 867 | |
| 868 | /* Load the firmware early, before calling into libertas.ko */ |
| 869 | ret = if_cs_prog_helper(card); |
| 870 | if (ret == 0) |
| 871 | ret = if_cs_prog_real(card); |
| 872 | if (ret) |
| 873 | goto out2; |
| 874 | |
| 875 | /* Make this card known to the libertas driver */ |
Holger Schurig | 1007832 | 2007-11-15 18:05:47 -0500 | [diff] [blame] | 876 | priv = lbs_add_card(card, &p_dev->dev); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 877 | if (!priv) { |
| 878 | ret = -ENOMEM; |
| 879 | goto out2; |
| 880 | } |
| 881 | |
Holger Schurig | 7919b89 | 2008-04-01 14:50:43 +0200 | [diff] [blame] | 882 | /* Finish setting up fields in lbs_private */ |
Dan Williams | 954ee16 | 2007-08-20 11:43:25 -0400 | [diff] [blame] | 883 | card->priv = priv; |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 884 | priv->card = card; |
Holger Schurig | 7919b89 | 2008-04-01 14:50:43 +0200 | [diff] [blame] | 885 | priv->hw_host_to_card = if_cs_host_to_card; |
David Woodhouse | aa21c00 | 2007-12-08 20:04:36 +0000 | [diff] [blame] | 886 | priv->fw_ready = 1; |
Dan Williams | 954ee16 | 2007-08-20 11:43:25 -0400 | [diff] [blame] | 887 | |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 888 | /* Now actually get the IRQ */ |
| 889 | ret = request_irq(p_dev->irq.AssignedIRQ, if_cs_interrupt, |
| 890 | IRQF_SHARED, DRV_NAME, card); |
| 891 | if (ret) { |
| 892 | lbs_pr_err("error in request_irq\n"); |
| 893 | goto out3; |
| 894 | } |
| 895 | |
Holger Schurig | 4ef3170 | 2007-10-09 10:41:57 +0200 | [diff] [blame] | 896 | /* Clear any interrupt cause that happend while sending |
| 897 | * firmware/initializing card */ |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame] | 898 | if_cs_write16(card, IF_CS_CARD_INT_CAUSE, IF_CS_BIT_MASK); |
Ryan Mallon | 28de0b3 | 2007-09-06 21:32:42 -0400 | [diff] [blame] | 899 | if_cs_enable_ints(card); |
| 900 | |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 901 | /* And finally bring the card up */ |
Holger Schurig | 1007832 | 2007-11-15 18:05:47 -0500 | [diff] [blame] | 902 | if (lbs_start_card(priv) != 0) { |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 903 | lbs_pr_err("could not activate card\n"); |
| 904 | goto out3; |
| 905 | } |
| 906 | |
Holger Schurig | d4ff0ef | 2008-03-19 14:25:18 +0100 | [diff] [blame] | 907 | /* The firmware for the CF card supports powersave */ |
| 908 | priv->ps_supported = 1; |
| 909 | |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 910 | ret = 0; |
| 911 | goto out; |
| 912 | |
| 913 | out3: |
Holger Schurig | 1007832 | 2007-11-15 18:05:47 -0500 | [diff] [blame] | 914 | lbs_remove_card(priv); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 915 | out2: |
| 916 | ioport_unmap(card->iobase); |
| 917 | out1: |
| 918 | pcmcia_disable_device(p_dev); |
| 919 | out: |
| 920 | lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret); |
| 921 | return ret; |
| 922 | } |
| 923 | |
| 924 | |
| 925 | /* |
| 926 | * This deletes a driver "instance". The device is de-registered with |
| 927 | * Card Services. If it has been released, all local data structures |
| 928 | * are freed. Otherwise, the structures will be freed when the device |
| 929 | * is released. |
| 930 | */ |
| 931 | static void if_cs_detach(struct pcmcia_device *p_dev) |
| 932 | { |
| 933 | struct if_cs_card *card = p_dev->priv; |
| 934 | |
| 935 | lbs_deb_enter(LBS_DEB_CS); |
| 936 | |
Holger Schurig | 1007832 | 2007-11-15 18:05:47 -0500 | [diff] [blame] | 937 | lbs_stop_card(card->priv); |
| 938 | lbs_remove_card(card->priv); |
Ryan Mallon | 28de0b3 | 2007-09-06 21:32:42 -0400 | [diff] [blame] | 939 | if_cs_disable_ints(card); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 940 | if_cs_release(p_dev); |
| 941 | kfree(card); |
| 942 | |
| 943 | lbs_deb_leave(LBS_DEB_CS); |
| 944 | } |
| 945 | |
| 946 | |
| 947 | |
| 948 | /********************************************************************/ |
| 949 | /* Module initialization */ |
| 950 | /********************************************************************/ |
| 951 | |
| 952 | static struct pcmcia_device_id if_cs_ids[] = { |
| 953 | PCMCIA_DEVICE_MANF_CARD(0x02df, 0x8103), |
| 954 | PCMCIA_DEVICE_NULL, |
| 955 | }; |
| 956 | MODULE_DEVICE_TABLE(pcmcia, if_cs_ids); |
| 957 | |
| 958 | |
Holger Schurig | 1007832 | 2007-11-15 18:05:47 -0500 | [diff] [blame] | 959 | static struct pcmcia_driver lbs_driver = { |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 960 | .owner = THIS_MODULE, |
| 961 | .drv = { |
| 962 | .name = DRV_NAME, |
| 963 | }, |
| 964 | .probe = if_cs_probe, |
| 965 | .remove = if_cs_detach, |
| 966 | .id_table = if_cs_ids, |
| 967 | }; |
| 968 | |
| 969 | |
| 970 | static int __init if_cs_init(void) |
| 971 | { |
| 972 | int ret; |
| 973 | |
| 974 | lbs_deb_enter(LBS_DEB_CS); |
Holger Schurig | 1007832 | 2007-11-15 18:05:47 -0500 | [diff] [blame] | 975 | ret = pcmcia_register_driver(&lbs_driver); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 976 | lbs_deb_leave(LBS_DEB_CS); |
| 977 | return ret; |
| 978 | } |
| 979 | |
| 980 | |
| 981 | static void __exit if_cs_exit(void) |
| 982 | { |
| 983 | lbs_deb_enter(LBS_DEB_CS); |
Holger Schurig | 1007832 | 2007-11-15 18:05:47 -0500 | [diff] [blame] | 984 | pcmcia_unregister_driver(&lbs_driver); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 985 | lbs_deb_leave(LBS_DEB_CS); |
| 986 | } |
| 987 | |
| 988 | |
| 989 | module_init(if_cs_init); |
| 990 | module_exit(if_cs_exit); |