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