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, |
| 125 | void *buf, |
| 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 | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame^] | 162 | /* First the bitmasks for the host/card interrupt/status registers: */ |
| 163 | #define IF_CS_BIT_TX 0x0001 |
| 164 | #define IF_CS_BIT_RX 0x0002 |
| 165 | #define IF_CS_BIT_COMMAND 0x0004 |
| 166 | #define IF_CS_BIT_RESP 0x0008 |
| 167 | #define IF_CS_BIT_EVENT 0x0010 |
| 168 | #define IF_CS_BIT_MASK 0x001f |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 169 | |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame^] | 170 | /* And now the individual registers and assorted masks */ |
| 171 | #define IF_CS_HOST_STATUS 0x00000000 |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 172 | |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame^] | 173 | #define IF_CS_HOST_INT_CAUSE 0x00000002 |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 174 | |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame^] | 175 | #define IF_CS_HOST_INT_MASK 0x00000004 |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 176 | |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame^] | 177 | #define IF_CS_HOST_WRITE 0x00000016 |
| 178 | #define IF_CS_HOST_WRITE_LEN 0x00000014 |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 179 | |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame^] | 180 | #define IF_CS_HOST_CMD 0x0000001A |
| 181 | #define IF_CS_HOST_CMD_LEN 0x00000018 |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 182 | |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame^] | 183 | #define IF_CS_READ 0x00000010 |
| 184 | #define IF_CS_READ_LEN 0x00000024 |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 185 | |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame^] | 186 | #define IF_CS_CARD_CMD 0x00000012 |
| 187 | #define IF_CS_CARD_CMD_LEN 0x00000030 |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 188 | |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame^] | 189 | #define IF_CS_CARD_STATUS 0x00000020 |
| 190 | #define IF_CS_CARD_STATUS_MASK 0x7f00 |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 191 | |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame^] | 192 | #define IF_CS_CARD_INT_CAUSE 0x00000022 |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 193 | |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame^] | 194 | #define IF_CS_CARD_SQ_READ_LOW 0x00000028 |
| 195 | #define IF_CS_CARD_SQ_HELPER_OK 0x10 |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 196 | |
| 197 | #define IF_CS_SCRATCH 0x0000003F |
| 198 | |
| 199 | |
| 200 | |
| 201 | /********************************************************************/ |
Holger Schurig | 43d01c5 | 2008-05-26 12:50:23 +0200 | [diff] [blame] | 202 | /* I/O and interrupt handling */ |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 203 | /********************************************************************/ |
| 204 | |
Holger Schurig | 43d01c5 | 2008-05-26 12:50:23 +0200 | [diff] [blame] | 205 | static inline void if_cs_enable_ints(struct if_cs_card *card) |
| 206 | { |
| 207 | lbs_deb_enter(LBS_DEB_CS); |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame^] | 208 | if_cs_write16(card, IF_CS_HOST_INT_MASK, 0); |
Holger Schurig | 43d01c5 | 2008-05-26 12:50:23 +0200 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | static inline void if_cs_disable_ints(struct if_cs_card *card) |
| 212 | { |
| 213 | lbs_deb_enter(LBS_DEB_CS); |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame^] | 214 | 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] | 215 | } |
| 216 | |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 217 | /* |
| 218 | * Called from if_cs_host_to_card to send a command to the hardware |
| 219 | */ |
Holger Schurig | 69f9032 | 2007-11-23 15:43:44 +0100 | [diff] [blame] | 220 | 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] | 221 | { |
| 222 | struct if_cs_card *card = (struct if_cs_card *)priv->card; |
| 223 | int ret = -1; |
| 224 | int loops = 0; |
| 225 | |
| 226 | lbs_deb_enter(LBS_DEB_CS); |
Holger Schurig | 43d01c5 | 2008-05-26 12:50:23 +0200 | [diff] [blame] | 227 | if_cs_disable_ints(card); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 228 | |
| 229 | /* Is hardware ready? */ |
| 230 | while (1) { |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame^] | 231 | u16 val = if_cs_read16(card, IF_CS_CARD_STATUS); |
| 232 | if (val & IF_CS_BIT_COMMAND) |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 233 | break; |
| 234 | if (++loops > 100) { |
| 235 | lbs_pr_err("card not ready for commands\n"); |
| 236 | goto done; |
| 237 | } |
| 238 | mdelay(1); |
| 239 | } |
| 240 | |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame^] | 241 | if_cs_write16(card, IF_CS_HOST_CMD_LEN, nb); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 242 | |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame^] | 243 | if_cs_write16_rep(card, IF_CS_HOST_CMD, buf, nb / 2); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 244 | /* Are we supposed to transfer an odd amount of bytes? */ |
| 245 | if (nb & 1) |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame^] | 246 | if_cs_write8(card, IF_CS_HOST_CMD, buf[nb-1]); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 247 | |
| 248 | /* "Assert the download over interrupt command in the Host |
| 249 | * status register" */ |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame^] | 250 | if_cs_write16(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 251 | |
| 252 | /* "Assert the download over interrupt command in the Card |
| 253 | * interrupt case register" */ |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame^] | 254 | 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] | 255 | ret = 0; |
| 256 | |
| 257 | done: |
Holger Schurig | 43d01c5 | 2008-05-26 12:50:23 +0200 | [diff] [blame] | 258 | if_cs_enable_ints(card); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 259 | lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret); |
| 260 | return ret; |
| 261 | } |
| 262 | |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 263 | /* |
| 264 | * Called from if_cs_host_to_card to send a data to the hardware |
| 265 | */ |
Holger Schurig | 69f9032 | 2007-11-23 15:43:44 +0100 | [diff] [blame] | 266 | 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] | 267 | { |
| 268 | struct if_cs_card *card = (struct if_cs_card *)priv->card; |
Holger Schurig | 43d01c5 | 2008-05-26 12:50:23 +0200 | [diff] [blame] | 269 | u16 status; |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 270 | |
| 271 | lbs_deb_enter(LBS_DEB_CS); |
Holger Schurig | 43d01c5 | 2008-05-26 12:50:23 +0200 | [diff] [blame] | 272 | if_cs_disable_ints(card); |
| 273 | |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame^] | 274 | status = if_cs_read16(card, IF_CS_CARD_STATUS); |
| 275 | BUG_ON((status & IF_CS_BIT_TX) == 0); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 276 | |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame^] | 277 | if_cs_write16(card, IF_CS_HOST_WRITE_LEN, nb); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 278 | |
| 279 | /* write even number of bytes, then odd byte if necessary */ |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame^] | 280 | if_cs_write16_rep(card, IF_CS_HOST_WRITE, buf, nb / 2); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 281 | if (nb & 1) |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame^] | 282 | if_cs_write8(card, IF_CS_HOST_WRITE, buf[nb-1]); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 283 | |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame^] | 284 | if_cs_write16(card, IF_CS_HOST_STATUS, IF_CS_BIT_TX); |
| 285 | 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] | 286 | if_cs_enable_ints(card); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 287 | |
| 288 | lbs_deb_leave(LBS_DEB_CS); |
| 289 | } |
| 290 | |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 291 | /* |
| 292 | * Get the command result out of the card. |
| 293 | */ |
Holger Schurig | 69f9032 | 2007-11-23 15:43:44 +0100 | [diff] [blame] | 294 | 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] | 295 | { |
Holger Schurig | 7919b89 | 2008-04-01 14:50:43 +0200 | [diff] [blame] | 296 | unsigned long flags; |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 297 | int ret = -1; |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame^] | 298 | u16 status; |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 299 | |
| 300 | lbs_deb_enter(LBS_DEB_CS); |
| 301 | |
| 302 | /* is hardware ready? */ |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame^] | 303 | status = if_cs_read16(priv->card, IF_CS_CARD_STATUS); |
| 304 | if ((status & IF_CS_BIT_RESP) == 0) { |
| 305 | lbs_pr_err("no cmd response in card\n"); |
| 306 | *len = 0; |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 307 | goto out; |
| 308 | } |
| 309 | |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame^] | 310 | *len = if_cs_read16(priv->card, IF_CS_CARD_CMD_LEN); |
Dan Williams | ddac452 | 2007-12-11 13:49:39 -0500 | [diff] [blame] | 311 | if ((*len == 0) || (*len > LBS_CMD_BUFFER_SIZE)) { |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 312 | lbs_pr_err("card cmd buffer has invalid # of bytes (%d)\n", *len); |
| 313 | goto out; |
| 314 | } |
| 315 | |
| 316 | /* read even number of bytes, then odd byte if necessary */ |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame^] | 317 | if_cs_read16_rep(priv->card, IF_CS_CARD_CMD, data, *len/sizeof(u16)); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 318 | if (*len & 1) |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame^] | 319 | data[*len-1] = if_cs_read8(priv->card, IF_CS_CARD_CMD); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 320 | |
Holger Schurig | 09d4fad | 2007-12-06 13:50:30 +0100 | [diff] [blame] | 321 | /* This is a workaround for a firmware that reports too much |
| 322 | * bytes */ |
| 323 | *len -= 8; |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 324 | ret = 0; |
Holger Schurig | 7919b89 | 2008-04-01 14:50:43 +0200 | [diff] [blame] | 325 | |
| 326 | /* Clear this flag again */ |
| 327 | spin_lock_irqsave(&priv->driver_lock, flags); |
| 328 | priv->dnld_sent = DNLD_RES_RECEIVED; |
| 329 | spin_unlock_irqrestore(&priv->driver_lock, flags); |
| 330 | |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 331 | out: |
| 332 | lbs_deb_leave_args(LBS_DEB_CS, "ret %d, len %d", ret, *len); |
| 333 | return ret; |
| 334 | } |
| 335 | |
Holger Schurig | 69f9032 | 2007-11-23 15:43:44 +0100 | [diff] [blame] | 336 | static struct sk_buff *if_cs_receive_data(struct lbs_private *priv) |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 337 | { |
| 338 | struct sk_buff *skb = NULL; |
| 339 | u16 len; |
| 340 | u8 *data; |
| 341 | |
| 342 | lbs_deb_enter(LBS_DEB_CS); |
| 343 | |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame^] | 344 | len = if_cs_read16(priv->card, IF_CS_READ_LEN); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 345 | if (len == 0 || len > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) { |
| 346 | lbs_pr_err("card data buffer has invalid # of bytes (%d)\n", len); |
| 347 | priv->stats.rx_dropped++; |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 348 | goto dat_err; |
| 349 | } |
| 350 | |
Vladimir Davydov | f31ce76 | 2007-09-06 21:41:02 -0400 | [diff] [blame] | 351 | skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + 2); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 352 | if (!skb) |
| 353 | goto out; |
Vladimir Davydov | f31ce76 | 2007-09-06 21:41:02 -0400 | [diff] [blame] | 354 | skb_put(skb, len); |
| 355 | skb_reserve(skb, 2);/* 16 byte align */ |
| 356 | data = skb->data; |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 357 | |
| 358 | /* read even number of bytes, then odd byte if necessary */ |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame^] | 359 | 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] | 360 | if (len & 1) |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame^] | 361 | data[len-1] = if_cs_read8(priv->card, IF_CS_READ); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 362 | |
| 363 | dat_err: |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame^] | 364 | if_cs_write16(priv->card, IF_CS_HOST_STATUS, IF_CS_BIT_RX); |
| 365 | 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] | 366 | |
| 367 | out: |
| 368 | lbs_deb_leave_args(LBS_DEB_CS, "ret %p", skb); |
| 369 | return skb; |
| 370 | } |
| 371 | |
Holger Schurig | 7919b89 | 2008-04-01 14:50:43 +0200 | [diff] [blame] | 372 | static irqreturn_t if_cs_interrupt(int irq, void *data) |
| 373 | { |
| 374 | struct if_cs_card *card = data; |
| 375 | struct lbs_private *priv = card->priv; |
| 376 | u16 cause; |
| 377 | |
| 378 | lbs_deb_enter(LBS_DEB_CS); |
| 379 | |
Holger Schurig | 43d01c5 | 2008-05-26 12:50:23 +0200 | [diff] [blame] | 380 | /* Ask card interrupt cause register if there is something for us */ |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame^] | 381 | cause = if_cs_read16(card, IF_CS_CARD_INT_CAUSE); |
Holger Schurig | 7919b89 | 2008-04-01 14:50:43 +0200 | [diff] [blame] | 382 | if (cause == 0) { |
| 383 | /* Not for us */ |
| 384 | return IRQ_NONE; |
| 385 | } |
| 386 | |
| 387 | if (cause == 0xffff) { |
| 388 | /* Read in junk, the card has probably been removed */ |
| 389 | card->priv->surpriseremoved = 1; |
| 390 | return IRQ_HANDLED; |
| 391 | } |
| 392 | |
Holger Schurig | 43d01c5 | 2008-05-26 12:50:23 +0200 | [diff] [blame] | 393 | /* Clear interrupt cause */ |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame^] | 394 | if_cs_write16(card, IF_CS_CARD_INT_CAUSE, cause & IF_CS_BIT_MASK); |
Holger Schurig | 43d01c5 | 2008-05-26 12:50:23 +0200 | [diff] [blame] | 395 | lbs_deb_cs("cause 0x%04x\n", cause); |
Holger Schurig | 7919b89 | 2008-04-01 14:50:43 +0200 | [diff] [blame] | 396 | |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame^] | 397 | if (cause & IF_CS_BIT_RX) { |
Holger Schurig | 7919b89 | 2008-04-01 14:50:43 +0200 | [diff] [blame] | 398 | struct sk_buff *skb; |
| 399 | lbs_deb_cs("rx packet\n"); |
| 400 | skb = if_cs_receive_data(priv); |
| 401 | if (skb) |
| 402 | lbs_process_rxed_packet(priv, skb); |
| 403 | } |
| 404 | |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame^] | 405 | if (cause & IF_CS_BIT_TX) { |
Holger Schurig | 43d01c5 | 2008-05-26 12:50:23 +0200 | [diff] [blame] | 406 | lbs_deb_cs("tx done\n"); |
Holger Schurig | 7919b89 | 2008-04-01 14:50:43 +0200 | [diff] [blame] | 407 | lbs_host_to_card_done(priv); |
| 408 | } |
| 409 | |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame^] | 410 | if (cause & IF_CS_BIT_RESP) { |
Holger Schurig | 7919b89 | 2008-04-01 14:50:43 +0200 | [diff] [blame] | 411 | unsigned long flags; |
| 412 | u8 i; |
| 413 | |
Holger Schurig | 43d01c5 | 2008-05-26 12:50:23 +0200 | [diff] [blame] | 414 | lbs_deb_cs("cmd resp\n"); |
Holger Schurig | 7919b89 | 2008-04-01 14:50:43 +0200 | [diff] [blame] | 415 | spin_lock_irqsave(&priv->driver_lock, flags); |
| 416 | i = (priv->resp_idx == 0) ? 1 : 0; |
| 417 | spin_unlock_irqrestore(&priv->driver_lock, flags); |
| 418 | |
| 419 | BUG_ON(priv->resp_len[i]); |
| 420 | if_cs_receive_cmdres(priv, priv->resp_buf[i], |
| 421 | &priv->resp_len[i]); |
| 422 | |
| 423 | spin_lock_irqsave(&priv->driver_lock, flags); |
| 424 | lbs_notify_command_response(priv, i); |
| 425 | spin_unlock_irqrestore(&priv->driver_lock, flags); |
| 426 | } |
| 427 | |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame^] | 428 | if (cause & IF_CS_BIT_EVENT) { |
| 429 | u16 event = if_cs_read16(priv->card, IF_CS_CARD_STATUS) |
| 430 | & IF_CS_CARD_STATUS_MASK; |
| 431 | if_cs_write16(priv->card, IF_CS_HOST_INT_CAUSE, |
| 432 | IF_CS_BIT_EVENT); |
Holger Schurig | 43d01c5 | 2008-05-26 12:50:23 +0200 | [diff] [blame] | 433 | lbs_deb_cs("host event 0x%04x\n", event); |
Holger Schurig | 7919b89 | 2008-04-01 14:50:43 +0200 | [diff] [blame] | 434 | lbs_queue_event(priv, event >> 8 & 0xff); |
| 435 | } |
| 436 | |
Holger Schurig | 43d01c5 | 2008-05-26 12:50:23 +0200 | [diff] [blame] | 437 | lbs_deb_leave(LBS_DEB_CS); |
Holger Schurig | 7919b89 | 2008-04-01 14:50:43 +0200 | [diff] [blame] | 438 | return IRQ_HANDLED; |
| 439 | } |
| 440 | |
| 441 | |
| 442 | |
| 443 | |
| 444 | /********************************************************************/ |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 445 | /* Firmware */ |
| 446 | /********************************************************************/ |
| 447 | |
| 448 | /* |
| 449 | * Tries to program the helper firmware. |
| 450 | * |
| 451 | * Return 0 on success |
| 452 | */ |
| 453 | static int if_cs_prog_helper(struct if_cs_card *card) |
| 454 | { |
| 455 | int ret = 0; |
| 456 | int sent = 0; |
| 457 | u8 scratch; |
| 458 | const struct firmware *fw; |
| 459 | |
| 460 | lbs_deb_enter(LBS_DEB_CS); |
| 461 | |
| 462 | scratch = if_cs_read8(card, IF_CS_SCRATCH); |
| 463 | |
| 464 | /* "If the value is 0x5a, the firmware is already |
| 465 | * downloaded successfully" |
| 466 | */ |
| 467 | if (scratch == 0x5a) |
| 468 | goto done; |
| 469 | |
| 470 | /* "If the value is != 00, it is invalid value of register */ |
| 471 | if (scratch != 0x00) { |
| 472 | ret = -ENODEV; |
| 473 | goto done; |
| 474 | } |
| 475 | |
| 476 | /* TODO: make firmware file configurable */ |
| 477 | ret = request_firmware(&fw, "libertas_cs_helper.fw", |
| 478 | &handle_to_dev(card->p_dev)); |
| 479 | if (ret) { |
| 480 | lbs_pr_err("can't load helper firmware\n"); |
| 481 | ret = -ENODEV; |
| 482 | goto done; |
| 483 | } |
Andrew Morton | 4ecd41b | 2007-08-21 02:15:45 -0700 | [diff] [blame] | 484 | lbs_deb_cs("helper size %td\n", fw->size); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 485 | |
| 486 | /* "Set the 5 bytes of the helper image to 0" */ |
| 487 | /* Not needed, this contains an ARM branch instruction */ |
| 488 | |
| 489 | for (;;) { |
| 490 | /* "the number of bytes to send is 256" */ |
| 491 | int count = 256; |
| 492 | int remain = fw->size - sent; |
| 493 | |
| 494 | if (remain < count) |
| 495 | count = remain; |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 496 | |
| 497 | /* "write the number of bytes to be sent to the I/O Command |
| 498 | * write length register" */ |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame^] | 499 | if_cs_write16(card, IF_CS_HOST_CMD_LEN, count); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 500 | |
| 501 | /* "write this to I/O Command port register as 16 bit writes */ |
| 502 | if (count) |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame^] | 503 | if_cs_write16_rep(card, IF_CS_HOST_CMD, |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 504 | &fw->data[sent], |
| 505 | count >> 1); |
| 506 | |
| 507 | /* "Assert the download over interrupt command in the Host |
| 508 | * status register" */ |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame^] | 509 | if_cs_write8(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 510 | |
| 511 | /* "Assert the download over interrupt command in the Card |
| 512 | * interrupt case register" */ |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame^] | 513 | 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] | 514 | |
| 515 | /* "The host polls the Card Status register ... for 50 ms before |
| 516 | declaring a failure */ |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame^] | 517 | ret = if_cs_poll_while_fw_download(card, IF_CS_CARD_STATUS, |
| 518 | IF_CS_BIT_COMMAND); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 519 | if (ret < 0) { |
| 520 | lbs_pr_err("can't download helper at 0x%x, ret %d\n", |
| 521 | sent, ret); |
| 522 | goto done; |
| 523 | } |
| 524 | |
| 525 | if (count == 0) |
| 526 | break; |
| 527 | |
| 528 | sent += count; |
| 529 | } |
| 530 | |
| 531 | release_firmware(fw); |
| 532 | ret = 0; |
| 533 | |
| 534 | done: |
| 535 | lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret); |
| 536 | return ret; |
| 537 | } |
| 538 | |
| 539 | |
| 540 | static int if_cs_prog_real(struct if_cs_card *card) |
| 541 | { |
| 542 | const struct firmware *fw; |
| 543 | int ret = 0; |
| 544 | int retry = 0; |
| 545 | int len = 0; |
| 546 | int sent; |
| 547 | |
| 548 | lbs_deb_enter(LBS_DEB_CS); |
| 549 | |
| 550 | /* TODO: make firmware file configurable */ |
| 551 | ret = request_firmware(&fw, "libertas_cs.fw", |
| 552 | &handle_to_dev(card->p_dev)); |
| 553 | if (ret) { |
| 554 | lbs_pr_err("can't load firmware\n"); |
| 555 | ret = -ENODEV; |
| 556 | goto done; |
| 557 | } |
Andrew Morton | 4ecd41b | 2007-08-21 02:15:45 -0700 | [diff] [blame] | 558 | lbs_deb_cs("fw size %td\n", fw->size); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 559 | |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame^] | 560 | ret = if_cs_poll_while_fw_download(card, IF_CS_CARD_SQ_READ_LOW, |
| 561 | IF_CS_CARD_SQ_HELPER_OK); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 562 | if (ret < 0) { |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 563 | lbs_pr_err("helper firmware doesn't answer\n"); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 564 | goto err_release; |
| 565 | } |
| 566 | |
| 567 | for (sent = 0; sent < fw->size; sent += len) { |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame^] | 568 | len = if_cs_read16(card, IF_CS_CARD_SQ_READ_LOW); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 569 | if (len & 1) { |
| 570 | retry++; |
| 571 | lbs_pr_info("odd, need to retry this firmware block\n"); |
| 572 | } else { |
| 573 | retry = 0; |
| 574 | } |
| 575 | |
| 576 | if (retry > 20) { |
| 577 | lbs_pr_err("could not download firmware\n"); |
| 578 | ret = -ENODEV; |
| 579 | goto err_release; |
| 580 | } |
| 581 | if (retry) { |
| 582 | sent -= len; |
| 583 | } |
| 584 | |
| 585 | |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame^] | 586 | if_cs_write16(card, IF_CS_HOST_CMD_LEN, len); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 587 | |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame^] | 588 | if_cs_write16_rep(card, IF_CS_HOST_CMD, |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 589 | &fw->data[sent], |
| 590 | (len+1) >> 1); |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame^] | 591 | if_cs_write8(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND); |
| 592 | 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] | 593 | |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame^] | 594 | ret = if_cs_poll_while_fw_download(card, IF_CS_CARD_STATUS, |
| 595 | IF_CS_BIT_COMMAND); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 596 | if (ret < 0) { |
| 597 | lbs_pr_err("can't download firmware at 0x%x\n", sent); |
| 598 | goto err_release; |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | ret = if_cs_poll_while_fw_download(card, IF_CS_SCRATCH, 0x5a); |
| 603 | if (ret < 0) { |
| 604 | lbs_pr_err("firmware download failed\n"); |
| 605 | goto err_release; |
| 606 | } |
| 607 | |
| 608 | ret = 0; |
| 609 | goto done; |
| 610 | |
| 611 | |
| 612 | err_release: |
| 613 | release_firmware(fw); |
| 614 | |
| 615 | done: |
| 616 | lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret); |
| 617 | return ret; |
| 618 | } |
| 619 | |
| 620 | |
| 621 | |
| 622 | /********************************************************************/ |
| 623 | /* Callback functions for libertas.ko */ |
| 624 | /********************************************************************/ |
| 625 | |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 626 | /* Send commands or data packets to the card */ |
Holger Schurig | 69f9032 | 2007-11-23 15:43:44 +0100 | [diff] [blame] | 627 | static int if_cs_host_to_card(struct lbs_private *priv, |
| 628 | u8 type, |
| 629 | u8 *buf, |
| 630 | u16 nb) |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 631 | { |
| 632 | int ret = -1; |
| 633 | |
| 634 | lbs_deb_enter_args(LBS_DEB_CS, "type %d, bytes %d", type, nb); |
| 635 | |
| 636 | switch (type) { |
| 637 | case MVMS_DAT: |
Ryan Mallon | 6f05cbe | 2007-09-06 21:30:32 -0400 | [diff] [blame] | 638 | priv->dnld_sent = DNLD_DATA_SENT; |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 639 | if_cs_send_data(priv, buf, nb); |
| 640 | ret = 0; |
| 641 | break; |
| 642 | case MVMS_CMD: |
Ryan Mallon | 6f05cbe | 2007-09-06 21:30:32 -0400 | [diff] [blame] | 643 | priv->dnld_sent = DNLD_CMD_SENT; |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 644 | ret = if_cs_send_cmd(priv, buf, nb); |
| 645 | break; |
| 646 | default: |
| 647 | lbs_pr_err("%s: unsupported type %d\n", __FUNCTION__, type); |
| 648 | } |
| 649 | |
| 650 | lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret); |
| 651 | return ret; |
| 652 | } |
| 653 | |
| 654 | |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 655 | /********************************************************************/ |
| 656 | /* Card Services */ |
| 657 | /********************************************************************/ |
| 658 | |
| 659 | /* |
| 660 | * After a card is removed, if_cs_release() will unregister the |
| 661 | * device, and release the PCMCIA configuration. If the device is |
| 662 | * still open, this will be postponed until it is closed. |
| 663 | */ |
| 664 | static void if_cs_release(struct pcmcia_device *p_dev) |
| 665 | { |
| 666 | struct if_cs_card *card = p_dev->priv; |
| 667 | |
| 668 | lbs_deb_enter(LBS_DEB_CS); |
| 669 | |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 670 | free_irq(p_dev->irq.AssignedIRQ, card); |
Holger Schurig | fdfb92e | 2008-01-25 14:15:48 +0100 | [diff] [blame] | 671 | pcmcia_disable_device(p_dev); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 672 | if (card->iobase) |
| 673 | ioport_unmap(card->iobase); |
| 674 | |
| 675 | lbs_deb_leave(LBS_DEB_CS); |
| 676 | } |
| 677 | |
| 678 | |
| 679 | /* |
| 680 | * This creates an "instance" of the driver, allocating local data |
| 681 | * structures for one device. The device is registered with Card |
| 682 | * Services. |
| 683 | * |
| 684 | * The dev_link structure is initialized, but we don't actually |
| 685 | * configure the card at this point -- we wait until we receive a card |
| 686 | * insertion event. |
| 687 | */ |
| 688 | static int if_cs_probe(struct pcmcia_device *p_dev) |
| 689 | { |
| 690 | int ret = -ENOMEM; |
Holger Schurig | 69f9032 | 2007-11-23 15:43:44 +0100 | [diff] [blame] | 691 | struct lbs_private *priv; |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 692 | struct if_cs_card *card; |
| 693 | /* CIS parsing */ |
| 694 | tuple_t tuple; |
| 695 | cisparse_t parse; |
| 696 | cistpl_cftable_entry_t *cfg = &parse.cftable_entry; |
| 697 | cistpl_io_t *io = &cfg->io; |
| 698 | u_char buf[64]; |
| 699 | |
| 700 | lbs_deb_enter(LBS_DEB_CS); |
| 701 | |
| 702 | card = kzalloc(sizeof(struct if_cs_card), GFP_KERNEL); |
| 703 | if (!card) { |
| 704 | lbs_pr_err("error in kzalloc\n"); |
| 705 | goto out; |
| 706 | } |
| 707 | card->p_dev = p_dev; |
| 708 | p_dev->priv = card; |
| 709 | |
| 710 | p_dev->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING; |
| 711 | p_dev->irq.Handler = NULL; |
| 712 | p_dev->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID; |
| 713 | |
| 714 | p_dev->conf.Attributes = 0; |
| 715 | p_dev->conf.IntType = INT_MEMORY_AND_IO; |
| 716 | |
| 717 | tuple.Attributes = 0; |
| 718 | tuple.TupleData = buf; |
| 719 | tuple.TupleDataMax = sizeof(buf); |
| 720 | tuple.TupleOffset = 0; |
| 721 | |
| 722 | tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY; |
| 723 | if ((ret = pcmcia_get_first_tuple(p_dev, &tuple)) != 0 || |
| 724 | (ret = pcmcia_get_tuple_data(p_dev, &tuple)) != 0 || |
| 725 | (ret = pcmcia_parse_tuple(p_dev, &tuple, &parse)) != 0) |
| 726 | { |
| 727 | lbs_pr_err("error in pcmcia_get_first_tuple etc\n"); |
| 728 | goto out1; |
| 729 | } |
| 730 | |
| 731 | p_dev->conf.ConfigIndex = cfg->index; |
| 732 | |
| 733 | /* Do we need to allocate an interrupt? */ |
| 734 | if (cfg->irq.IRQInfo1) { |
| 735 | p_dev->conf.Attributes |= CONF_ENABLE_IRQ; |
| 736 | } |
| 737 | |
| 738 | /* IO window settings */ |
| 739 | if (cfg->io.nwin != 1) { |
| 740 | lbs_pr_err("wrong CIS (check number of IO windows)\n"); |
| 741 | ret = -ENODEV; |
| 742 | goto out1; |
| 743 | } |
| 744 | p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO; |
| 745 | p_dev->io.BasePort1 = io->win[0].base; |
| 746 | p_dev->io.NumPorts1 = io->win[0].len; |
| 747 | |
| 748 | /* This reserves IO space but doesn't actually enable it */ |
| 749 | ret = pcmcia_request_io(p_dev, &p_dev->io); |
| 750 | if (ret) { |
| 751 | lbs_pr_err("error in pcmcia_request_io\n"); |
| 752 | goto out1; |
| 753 | } |
| 754 | |
| 755 | /* |
| 756 | * Allocate an interrupt line. Note that this does not assign |
| 757 | * a handler to the interrupt, unless the 'Handler' member of |
| 758 | * the irq structure is initialized. |
| 759 | */ |
| 760 | if (p_dev->conf.Attributes & CONF_ENABLE_IRQ) { |
| 761 | ret = pcmcia_request_irq(p_dev, &p_dev->irq); |
| 762 | if (ret) { |
| 763 | lbs_pr_err("error in pcmcia_request_irq\n"); |
| 764 | goto out1; |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | /* Initialize io access */ |
| 769 | card->iobase = ioport_map(p_dev->io.BasePort1, p_dev->io.NumPorts1); |
| 770 | if (!card->iobase) { |
| 771 | lbs_pr_err("error in ioport_map\n"); |
| 772 | ret = -EIO; |
| 773 | goto out1; |
| 774 | } |
| 775 | |
| 776 | /* |
| 777 | * This actually configures the PCMCIA socket -- setting up |
| 778 | * the I/O windows and the interrupt mapping, and putting the |
| 779 | * card and host interface into "Memory and IO" mode. |
| 780 | */ |
| 781 | ret = pcmcia_request_configuration(p_dev, &p_dev->conf); |
| 782 | if (ret) { |
| 783 | lbs_pr_err("error in pcmcia_request_configuration\n"); |
| 784 | goto out2; |
| 785 | } |
| 786 | |
| 787 | /* Finally, report what we've done */ |
| 788 | lbs_deb_cs("irq %d, io 0x%04x-0x%04x\n", |
| 789 | p_dev->irq.AssignedIRQ, p_dev->io.BasePort1, |
| 790 | p_dev->io.BasePort1 + p_dev->io.NumPorts1 - 1); |
| 791 | |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 792 | |
| 793 | /* Load the firmware early, before calling into libertas.ko */ |
| 794 | ret = if_cs_prog_helper(card); |
| 795 | if (ret == 0) |
| 796 | ret = if_cs_prog_real(card); |
| 797 | if (ret) |
| 798 | goto out2; |
| 799 | |
| 800 | /* Make this card known to the libertas driver */ |
Holger Schurig | 1007832 | 2007-11-15 18:05:47 -0500 | [diff] [blame] | 801 | priv = lbs_add_card(card, &p_dev->dev); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 802 | if (!priv) { |
| 803 | ret = -ENOMEM; |
| 804 | goto out2; |
| 805 | } |
| 806 | |
Holger Schurig | 7919b89 | 2008-04-01 14:50:43 +0200 | [diff] [blame] | 807 | /* Finish setting up fields in lbs_private */ |
Dan Williams | 954ee16 | 2007-08-20 11:43:25 -0400 | [diff] [blame] | 808 | card->priv = priv; |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 809 | priv->card = card; |
Holger Schurig | 7919b89 | 2008-04-01 14:50:43 +0200 | [diff] [blame] | 810 | priv->hw_host_to_card = if_cs_host_to_card; |
David Woodhouse | aa21c00 | 2007-12-08 20:04:36 +0000 | [diff] [blame] | 811 | priv->fw_ready = 1; |
Dan Williams | 954ee16 | 2007-08-20 11:43:25 -0400 | [diff] [blame] | 812 | |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 813 | /* Now actually get the IRQ */ |
| 814 | ret = request_irq(p_dev->irq.AssignedIRQ, if_cs_interrupt, |
| 815 | IRQF_SHARED, DRV_NAME, card); |
| 816 | if (ret) { |
| 817 | lbs_pr_err("error in request_irq\n"); |
| 818 | goto out3; |
| 819 | } |
| 820 | |
Holger Schurig | 4ef3170 | 2007-10-09 10:41:57 +0200 | [diff] [blame] | 821 | /* Clear any interrupt cause that happend while sending |
| 822 | * firmware/initializing card */ |
Holger Schurig | 78cf074 | 2008-06-02 09:25:05 +0200 | [diff] [blame^] | 823 | 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] | 824 | if_cs_enable_ints(card); |
| 825 | |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 826 | /* And finally bring the card up */ |
Holger Schurig | 1007832 | 2007-11-15 18:05:47 -0500 | [diff] [blame] | 827 | if (lbs_start_card(priv) != 0) { |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 828 | lbs_pr_err("could not activate card\n"); |
| 829 | goto out3; |
| 830 | } |
| 831 | |
Holger Schurig | d4ff0ef | 2008-03-19 14:25:18 +0100 | [diff] [blame] | 832 | /* The firmware for the CF card supports powersave */ |
| 833 | priv->ps_supported = 1; |
| 834 | |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 835 | ret = 0; |
| 836 | goto out; |
| 837 | |
| 838 | out3: |
Holger Schurig | 1007832 | 2007-11-15 18:05:47 -0500 | [diff] [blame] | 839 | lbs_remove_card(priv); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 840 | out2: |
| 841 | ioport_unmap(card->iobase); |
| 842 | out1: |
| 843 | pcmcia_disable_device(p_dev); |
| 844 | out: |
| 845 | lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret); |
| 846 | return ret; |
| 847 | } |
| 848 | |
| 849 | |
| 850 | /* |
| 851 | * This deletes a driver "instance". The device is de-registered with |
| 852 | * Card Services. If it has been released, all local data structures |
| 853 | * are freed. Otherwise, the structures will be freed when the device |
| 854 | * is released. |
| 855 | */ |
| 856 | static void if_cs_detach(struct pcmcia_device *p_dev) |
| 857 | { |
| 858 | struct if_cs_card *card = p_dev->priv; |
| 859 | |
| 860 | lbs_deb_enter(LBS_DEB_CS); |
| 861 | |
Holger Schurig | 1007832 | 2007-11-15 18:05:47 -0500 | [diff] [blame] | 862 | lbs_stop_card(card->priv); |
| 863 | lbs_remove_card(card->priv); |
Ryan Mallon | 28de0b3 | 2007-09-06 21:32:42 -0400 | [diff] [blame] | 864 | if_cs_disable_ints(card); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 865 | if_cs_release(p_dev); |
| 866 | kfree(card); |
| 867 | |
| 868 | lbs_deb_leave(LBS_DEB_CS); |
| 869 | } |
| 870 | |
| 871 | |
| 872 | |
| 873 | /********************************************************************/ |
| 874 | /* Module initialization */ |
| 875 | /********************************************************************/ |
| 876 | |
| 877 | static struct pcmcia_device_id if_cs_ids[] = { |
| 878 | PCMCIA_DEVICE_MANF_CARD(0x02df, 0x8103), |
| 879 | PCMCIA_DEVICE_NULL, |
| 880 | }; |
| 881 | MODULE_DEVICE_TABLE(pcmcia, if_cs_ids); |
| 882 | |
| 883 | |
Holger Schurig | 1007832 | 2007-11-15 18:05:47 -0500 | [diff] [blame] | 884 | static struct pcmcia_driver lbs_driver = { |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 885 | .owner = THIS_MODULE, |
| 886 | .drv = { |
| 887 | .name = DRV_NAME, |
| 888 | }, |
| 889 | .probe = if_cs_probe, |
| 890 | .remove = if_cs_detach, |
| 891 | .id_table = if_cs_ids, |
| 892 | }; |
| 893 | |
| 894 | |
| 895 | static int __init if_cs_init(void) |
| 896 | { |
| 897 | int ret; |
| 898 | |
| 899 | lbs_deb_enter(LBS_DEB_CS); |
Holger Schurig | 1007832 | 2007-11-15 18:05:47 -0500 | [diff] [blame] | 900 | ret = pcmcia_register_driver(&lbs_driver); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 901 | lbs_deb_leave(LBS_DEB_CS); |
| 902 | return ret; |
| 903 | } |
| 904 | |
| 905 | |
| 906 | static void __exit if_cs_exit(void) |
| 907 | { |
| 908 | lbs_deb_enter(LBS_DEB_CS); |
Holger Schurig | 1007832 | 2007-11-15 18:05:47 -0500 | [diff] [blame] | 909 | pcmcia_unregister_driver(&lbs_driver); |
Holger Schurig | 27590d0 | 2007-10-03 17:25:41 -0700 | [diff] [blame] | 910 | lbs_deb_leave(LBS_DEB_CS); |
| 911 | } |
| 912 | |
| 913 | |
| 914 | module_init(if_cs_init); |
| 915 | module_exit(if_cs_exit); |