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