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