2 * linux/drivers/net/wireless/libertas/if_spi.c
4 * Driver for Marvell SPI WLAN cards.
6 * Copyright 2008 Analog Devices Inc.
9 * Andrey Yurovsky <andrey@cozybit.com>
10 * Colin McCabe <colin@cozybit.com>
12 * Inspired by if_sdio.c, Copyright 2007-2008 Pierre Ossman
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.
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>
36 struct if_spi_packet {
37 struct list_head list;
39 u8 buffer[0] __attribute__((aligned(4)));
43 struct spi_device *spi;
44 struct lbs_private *priv;
46 char helper_fw_name[FIRMWARE_NAME_MAX];
47 char main_fw_name[FIRMWARE_NAME_MAX];
49 /* The card ID and card revision, as reported by the hardware. */
53 /* Pin number for our GPIO chip-select. */
54 /* TODO: Once the generic SPI layer has some additional features, we
55 * should take this out and use the normal chip select here.
56 * We need support for chip select delays, and not dropping chipselect
60 /* The last time that we initiated an SPU operation */
61 unsigned long prev_xfer_time;
64 unsigned long spu_port_delay;
65 unsigned long spu_reg_delay;
67 /* Handles all SPI communication (except for FW load) */
68 struct task_struct *spi_thread;
71 /* Used to wake up the spi_thread */
72 struct semaphore spi_ready;
73 struct semaphore spi_thread_terminated;
75 u8 cmd_buffer[IF_SPI_CMD_BUF_SIZE];
77 /* A buffer of incoming packets from libertas core.
78 * Since we can't sleep in hw_host_to_card, we have to buffer
80 struct list_head cmd_packet_list;
81 struct list_head data_packet_list;
83 /* Protects cmd_packet_list and data_packet_list */
84 spinlock_t buffer_lock;
87 static void free_if_spi_card(struct if_spi_card *card)
89 struct list_head *cursor, *next;
90 struct if_spi_packet *packet;
92 BUG_ON(card->run_thread);
93 list_for_each_safe(cursor, next, &card->cmd_packet_list) {
94 packet = container_of(cursor, struct if_spi_packet, list);
95 list_del(&packet->list);
98 list_for_each_safe(cursor, next, &card->data_packet_list) {
99 packet = container_of(cursor, struct if_spi_packet, list);
100 list_del(&packet->list);
103 spi_set_drvdata(card->spi, NULL);
107 static struct chip_ident chip_id_to_device_name[] = {
108 { .chip_id = 0x04, .name = 8385 },
109 { .chip_id = 0x0b, .name = 8686 },
113 * SPI Interface Unit Routines
115 * The SPU sits between the host and the WLAN module.
116 * All communication with the firmware is through SPU transactions.
118 * First we have to put a SPU register name on the bus. Then we can
119 * either read from or write to that register.
121 * For 16-bit transactions, byte order on the bus is big-endian.
122 * We don't have to worry about that here, though.
123 * The translation takes place in the SPI routines.
126 static void spu_transaction_init(struct if_spi_card *card)
128 if (!time_after(jiffies, card->prev_xfer_time + 1)) {
129 /* Unfortunately, the SPU requires a delay between successive
130 * transactions. If our last transaction was more than a jiffy
131 * ago, we have obviously already delayed enough.
132 * If not, we have to busy-wait to be on the safe side. */
135 gpio_set_value(card->gpio_cs, 0); /* assert CS */
138 static void spu_transaction_finish(struct if_spi_card *card)
140 gpio_set_value(card->gpio_cs, 1); /* drop CS */
141 card->prev_xfer_time = jiffies;
144 /* Write out a byte buffer to an SPI register,
145 * using a series of 16-bit transfers. */
146 static int spu_write(struct if_spi_card *card, u16 reg, const u8 *buf, int len)
149 u16 reg_out = reg | IF_SPI_WRITE_OPERATION_MASK;
151 /* You must give an even number of bytes to the SPU, even if it
152 * doesn't care about the last one. */
155 spu_transaction_init(card);
157 /* write SPU register index */
158 err = spi_write(card->spi, (u8 *)®_out, sizeof(u16));
162 err = spi_write(card->spi, buf, len);
165 spu_transaction_finish(card);
169 static inline int spu_write_u16(struct if_spi_card *card, u16 reg, u16 val)
171 return spu_write(card, reg, (u8 *)&val, sizeof(u16));
174 static inline int spu_write_u32(struct if_spi_card *card, u16 reg, u32 val)
176 /* The lower 16 bits are written first. */
178 out[0] = val & 0xffff;
179 out[1] = (val & 0xffff0000) >> 16;
180 return spu_write(card, reg, (u8 *)&out, sizeof(u32));
183 static inline int spu_reg_is_port_reg(u16 reg)
186 case IF_SPI_IO_RDWRPORT_REG:
187 case IF_SPI_CMD_RDWRPORT_REG:
188 case IF_SPI_DATA_RDWRPORT_REG:
195 static int spu_read(struct if_spi_card *card, u16 reg, u8 *buf, int len)
197 unsigned int i, delay;
200 u16 reg_out = reg | IF_SPI_READ_OPERATION_MASK;
202 /* You must take an even number of bytes from the SPU, even if you
203 * don't care about the last one. */
206 spu_transaction_init(card);
208 /* write SPU register index */
209 err = spi_write(card->spi, (u8 *)®_out, sizeof(u16));
213 delay = spu_reg_is_port_reg(reg) ? card->spu_port_delay :
215 if (card->use_dummy_writes) {
216 /* Clock in dummy cycles while the SPU fills the FIFO */
217 for (i = 0; i < delay / 16; ++i) {
218 err = spi_write(card->spi, (u8 *)&zero, sizeof(u16));
223 /* Busy-wait while the SPU fills the FIFO */
224 ndelay(100 + (delay * 10));
228 err = spi_read(card->spi, buf, len);
231 spu_transaction_finish(card);
235 /* Read 16 bits from an SPI register */
236 static inline int spu_read_u16(struct if_spi_card *card, u16 reg, u16 *val)
238 return spu_read(card, reg, (u8 *)val, sizeof(u16));
241 /* Read 32 bits from an SPI register.
242 * The low 16 bits are read first. */
243 static int spu_read_u32(struct if_spi_card *card, u16 reg, u32 *val)
247 err = spu_read(card, reg, (u8 *)buf, sizeof(u32));
249 *val = buf[0] | (buf[1] << 16);
253 /* Keep reading 16 bits from an SPI register until you get the correct result.
255 * If mask = 0, the correct result is any non-zero number.
256 * If mask != 0, the correct result is any number where
257 * number & target_mask == target
259 * Returns -ETIMEDOUT if a second passes without the correct result. */
260 static int spu_wait_for_u16(struct if_spi_card *card, u16 reg,
261 u16 target_mask, u16 target)
264 unsigned long timeout = jiffies + 5*HZ;
267 err = spu_read_u16(card, reg, &val);
271 if ((val & target_mask) == target)
278 if (time_after(jiffies, timeout)) {
279 lbs_pr_err("%s: timeout with val=%02x, "
280 "target_mask=%02x, target=%02x\n",
281 __func__, val, target_mask, target);
287 /* Read 16 bits from an SPI register until you receive a specific value.
288 * Returns -ETIMEDOUT if a 4 tries pass without success. */
289 static int spu_wait_for_u32(struct if_spi_card *card, u32 reg, u32 target)
292 for (try = 0; try < 4; ++try) {
294 err = spu_read_u32(card, reg, &val);
304 static int spu_set_interrupt_mode(struct if_spi_card *card,
305 int suppress_host_int,
310 /* We can suppress a host interrupt by clearing the appropriate
311 * bit in the "host interrupt status mask" register */
312 if (suppress_host_int) {
313 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG, 0);
317 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG,
318 IF_SPI_HISM_TX_DOWNLOAD_RDY |
319 IF_SPI_HISM_RX_UPLOAD_RDY |
320 IF_SPI_HISM_CMD_DOWNLOAD_RDY |
321 IF_SPI_HISM_CARDEVENT |
322 IF_SPI_HISM_CMD_UPLOAD_RDY);
327 /* If auto-interrupts are on, the completion of certain transactions
328 * will trigger an interrupt automatically. If auto-interrupts
329 * are off, we need to set the "Card Interrupt Cause" register to
330 * trigger a card interrupt. */
332 err = spu_write_u16(card, IF_SPI_HOST_INT_CTRL_REG,
333 IF_SPI_HICT_TX_DOWNLOAD_OVER_AUTO |
334 IF_SPI_HICT_RX_UPLOAD_OVER_AUTO |
335 IF_SPI_HICT_CMD_DOWNLOAD_OVER_AUTO |
336 IF_SPI_HICT_CMD_UPLOAD_OVER_AUTO);
340 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG, 0);
347 static int spu_get_chip_revision(struct if_spi_card *card,
348 u16 *card_id, u8 *card_rev)
352 err = spu_read_u32(card, IF_SPI_DEVICEID_CTRL_REG, &dev_ctrl);
355 *card_id = IF_SPI_DEVICEID_CTRL_REG_TO_CARD_ID(dev_ctrl);
356 *card_rev = IF_SPI_DEVICEID_CTRL_REG_TO_CARD_REV(dev_ctrl);
360 static int spu_set_bus_mode(struct if_spi_card *card, u16 mode)
365 err = spu_write_u16(card, IF_SPI_SPU_BUS_MODE_REG, mode);
368 /* Check that we were able to read back what we just wrote. */
369 err = spu_read_u16(card, IF_SPI_SPU_BUS_MODE_REG, &rval);
373 lbs_pr_err("Can't read bus mode register.\n");
379 static int spu_init(struct if_spi_card *card, int use_dummy_writes)
384 /* We have to start up in timed delay mode so that we can safely
385 * read the Delay Read Register. */
386 card->use_dummy_writes = 0;
387 err = spu_set_bus_mode(card,
388 IF_SPI_BUS_MODE_SPI_CLOCK_PHASE_RISING |
389 IF_SPI_BUS_MODE_DELAY_METHOD_TIMED |
390 IF_SPI_BUS_MODE_16_BIT_ADDRESS_16_BIT_DATA);
393 card->spu_port_delay = 1000;
394 card->spu_reg_delay = 1000;
395 err = spu_read_u32(card, IF_SPI_DELAY_READ_REG, &delay);
398 card->spu_port_delay = delay & 0x0000ffff;
399 card->spu_reg_delay = (delay & 0xffff0000) >> 16;
401 /* If dummy clock delay mode has been requested, switch to it now */
402 if (use_dummy_writes) {
403 card->use_dummy_writes = 1;
404 err = spu_set_bus_mode(card,
405 IF_SPI_BUS_MODE_SPI_CLOCK_PHASE_RISING |
406 IF_SPI_BUS_MODE_DELAY_METHOD_DUMMY_CLOCK |
407 IF_SPI_BUS_MODE_16_BIT_ADDRESS_16_BIT_DATA);
412 lbs_deb_spi("Initialized SPU unit. "
413 "spu_port_delay=0x%04lx, spu_reg_delay=0x%04lx\n",
414 card->spu_port_delay, card->spu_reg_delay);
422 static int if_spi_prog_helper_firmware(struct if_spi_card *card)
425 const struct firmware *firmware = NULL;
428 u8 temp[HELPER_FW_LOAD_CHUNK_SZ];
429 struct spi_device *spi = card->spi;
431 lbs_deb_enter(LBS_DEB_SPI);
433 err = spu_set_interrupt_mode(card, 1, 0);
436 /* Get helper firmware image */
437 err = request_firmware(&firmware, card->helper_fw_name, &spi->dev);
439 lbs_pr_err("request_firmware failed with err = %d\n", err);
442 bytes_remaining = firmware->size;
445 /* Load helper firmware image */
446 while (bytes_remaining > 0) {
447 /* Scratch pad 1 should contain the number of bytes we
448 * want to download to the firmware */
449 err = spu_write_u16(card, IF_SPI_SCRATCH_1_REG,
450 HELPER_FW_LOAD_CHUNK_SZ);
452 goto release_firmware;
454 err = spu_wait_for_u16(card, IF_SPI_HOST_INT_STATUS_REG,
455 IF_SPI_HIST_CMD_DOWNLOAD_RDY,
456 IF_SPI_HIST_CMD_DOWNLOAD_RDY);
458 goto release_firmware;
460 /* Feed the data into the command read/write port reg
461 * in chunks of 64 bytes */
462 memset(temp, 0, sizeof(temp));
464 min(bytes_remaining, HELPER_FW_LOAD_CHUNK_SZ));
466 err = spu_write(card, IF_SPI_CMD_RDWRPORT_REG,
467 temp, HELPER_FW_LOAD_CHUNK_SZ);
469 goto release_firmware;
471 /* Interrupt the boot code */
472 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
474 goto release_firmware;
475 err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG,
476 IF_SPI_CIC_CMD_DOWNLOAD_OVER);
478 goto release_firmware;
479 bytes_remaining -= HELPER_FW_LOAD_CHUNK_SZ;
480 fw += HELPER_FW_LOAD_CHUNK_SZ;
483 /* Once the helper / single stage firmware download is complete,
484 * write 0 to scratch pad 1 and interrupt the
485 * bootloader. This completes the helper download. */
486 err = spu_write_u16(card, IF_SPI_SCRATCH_1_REG, FIRMWARE_DNLD_OK);
488 goto release_firmware;
489 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
491 goto release_firmware;
492 err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG,
493 IF_SPI_CIC_CMD_DOWNLOAD_OVER);
494 goto release_firmware;
496 lbs_deb_spi("waiting for helper to boot...\n");
499 release_firmware(firmware);
502 lbs_pr_err("failed to load helper firmware (err=%d)\n", err);
503 lbs_deb_leave_args(LBS_DEB_SPI, "err %d", err);
507 /* Returns the length of the next packet the firmware expects us to send
508 * Sets crc_err if the previous transfer had a CRC error. */
509 static int if_spi_prog_main_firmware_check_len(struct if_spi_card *card,
515 /* wait until the host interrupt status register indicates
516 * that we are ready to download */
517 err = spu_wait_for_u16(card, IF_SPI_HOST_INT_STATUS_REG,
518 IF_SPI_HIST_CMD_DOWNLOAD_RDY,
519 IF_SPI_HIST_CMD_DOWNLOAD_RDY);
521 lbs_pr_err("timed out waiting for host_int_status\n");
525 /* Ask the device how many bytes of firmware it wants. */
526 err = spu_read_u16(card, IF_SPI_SCRATCH_1_REG, &len);
530 if (len > IF_SPI_CMD_BUF_SIZE) {
531 lbs_pr_err("firmware load device requested a larger "
532 "tranfer than we are prepared to "
533 "handle. (len = %d)\n", len);
537 lbs_deb_spi("%s: crc error\n", __func__);
546 static int if_spi_prog_main_firmware(struct if_spi_card *card)
549 int bytes, crc_err = 0, err = 0;
550 const struct firmware *firmware = NULL;
552 struct spi_device *spi = card->spi;
555 lbs_deb_enter(LBS_DEB_SPI);
557 err = spu_set_interrupt_mode(card, 1, 0);
561 /* Get firmware image */
562 err = request_firmware(&firmware, card->main_fw_name, &spi->dev);
564 lbs_pr_err("%s: can't get firmware '%s' from kernel. "
565 "err = %d\n", __func__, card->main_fw_name, err);
569 err = spu_wait_for_u16(card, IF_SPI_SCRATCH_1_REG, 0, 0);
571 lbs_pr_err("%s: timed out waiting for initial "
572 "scratch reg = 0\n", __func__);
573 goto release_firmware;
578 bytes = firmware->size;
580 while ((len = if_spi_prog_main_firmware_check_len(card, &crc_err))) {
583 goto release_firmware;
586 /* If there are no more bytes left, we would normally
587 * expect to have terminated with len = 0 */
588 lbs_pr_err("Firmware load wants more bytes "
589 "than we have to offer.\n");
593 /* Previous transfer failed. */
594 if (++num_crc_errs > MAX_MAIN_FW_LOAD_CRC_ERR) {
595 lbs_pr_err("Too many CRC errors encountered "
596 "in firmware load.\n");
598 goto release_firmware;
601 /* Previous transfer succeeded. Advance counters. */
606 memset(card->cmd_buffer, 0, len);
607 memcpy(card->cmd_buffer, fw, bytes);
609 memcpy(card->cmd_buffer, fw, len);
611 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
613 goto release_firmware;
614 err = spu_write(card, IF_SPI_CMD_RDWRPORT_REG,
615 card->cmd_buffer, len);
617 goto release_firmware;
618 err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG ,
619 IF_SPI_CIC_CMD_DOWNLOAD_OVER);
621 goto release_firmware;
624 if (bytes > prev_len) {
625 lbs_pr_err("firmware load wants fewer bytes than "
626 "we have to offer.\n");
629 /* Confirm firmware download */
630 err = spu_wait_for_u32(card, IF_SPI_SCRATCH_4_REG,
631 SUCCESSFUL_FW_DOWNLOAD_MAGIC);
633 lbs_pr_err("failed to confirm the firmware download\n");
634 goto release_firmware;
638 release_firmware(firmware);
642 lbs_pr_err("failed to load firmware (err=%d)\n", err);
643 lbs_deb_leave_args(LBS_DEB_SPI, "err %d", err);
648 * SPI Transfer Thread
650 * The SPI thread handles all SPI transfers, so there is no need for a lock.
653 /* Move a command from the card to the host */
654 static int if_spi_c2h_cmd(struct if_spi_card *card)
656 struct lbs_private *priv = card->priv;
662 /* We need a buffer big enough to handle whatever people send to
664 BUILD_BUG_ON(IF_SPI_CMD_BUF_SIZE < LBS_CMD_BUFFER_SIZE);
665 BUILD_BUG_ON(IF_SPI_CMD_BUF_SIZE < LBS_UPLD_SIZE);
667 /* It's just annoying if the buffer size isn't a multiple of 4, because
668 * then we might have len < IF_SPI_CMD_BUF_SIZE but
669 * ALIGN(len, 4) > IF_SPI_CMD_BUF_SIZE */
670 BUILD_BUG_ON(IF_SPI_CMD_BUF_SIZE % 4 != 0);
672 lbs_deb_enter(LBS_DEB_SPI);
674 /* How many bytes are there to read? */
675 err = spu_read_u16(card, IF_SPI_SCRATCH_2_REG, &len);
679 lbs_pr_err("%s: error: card has no data for host\n",
683 } else if (len > IF_SPI_CMD_BUF_SIZE) {
684 lbs_pr_err("%s: error: response packet too large: "
685 "%d bytes, but maximum is %d\n",
686 __func__, len, IF_SPI_CMD_BUF_SIZE);
691 /* Read the data from the WLAN module into our command buffer */
692 err = spu_read(card, IF_SPI_CMD_RDWRPORT_REG,
693 card->cmd_buffer, ALIGN(len, 4));
697 spin_lock_irqsave(&priv->driver_lock, flags);
698 i = (priv->resp_idx == 0) ? 1 : 0;
699 BUG_ON(priv->resp_len[i]);
700 priv->resp_len[i] = len;
701 memcpy(priv->resp_buf[i], card->cmd_buffer, len);
702 lbs_notify_command_response(priv, i);
703 spin_unlock_irqrestore(&priv->driver_lock, flags);
707 lbs_pr_err("%s: err=%d\n", __func__, err);
708 lbs_deb_leave(LBS_DEB_SPI);
712 /* Move data from the card to the host */
713 static int if_spi_c2h_data(struct if_spi_card *card)
720 lbs_deb_enter(LBS_DEB_SPI);
722 /* How many bytes are there to read? */
723 err = spu_read_u16(card, IF_SPI_SCRATCH_1_REG, &len);
727 lbs_pr_err("%s: error: card has no data for host\n",
731 } else if (len > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
732 lbs_pr_err("%s: error: card has %d bytes of data, but "
733 "our maximum skb size is %u\n",
734 __func__, len, MRVDRV_ETH_RX_PACKET_BUFFER_SIZE);
739 /* TODO: should we allocate a smaller skb if we have less data? */
740 skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE);
745 skb_reserve(skb, IPFIELD_ALIGN_OFFSET);
746 data = skb_put(skb, len);
748 /* Read the data from the WLAN module into our skb... */
749 err = spu_read(card, IF_SPI_DATA_RDWRPORT_REG, data, ALIGN(len, 4));
753 /* pass the SKB to libertas */
754 err = lbs_process_rxed_packet(card->priv, skb);
765 lbs_pr_err("%s: err=%d\n", __func__, err);
766 lbs_deb_leave(LBS_DEB_SPI);
770 /* Move data or a command from the host to the card. */
771 static void if_spi_h2c(struct if_spi_card *card,
772 struct if_spi_packet *packet, int type)
775 u16 int_type, port_reg;
779 int_type = IF_SPI_CIC_TX_DOWNLOAD_OVER;
780 port_reg = IF_SPI_DATA_RDWRPORT_REG;
783 int_type = IF_SPI_CIC_CMD_DOWNLOAD_OVER;
784 port_reg = IF_SPI_CMD_RDWRPORT_REG;
787 lbs_pr_err("can't transfer buffer of type %d\n", type);
792 /* Write the data to the card */
793 err = spu_write(card, port_reg, packet->buffer, packet->blen);
801 lbs_pr_err("%s: error %d\n", __func__, err);
804 /* Inform the host about a card event */
805 static void if_spi_e2h(struct if_spi_card *card)
810 struct lbs_private *priv = card->priv;
812 err = spu_read_u32(card, IF_SPI_SCRATCH_3_REG, &cause);
816 spin_lock_irqsave(&priv->driver_lock, flags);
817 lbs_queue_event(priv, cause & 0xff);
818 spin_unlock_irqrestore(&priv->driver_lock, flags);
822 lbs_pr_err("%s: error %d\n", __func__, err);
825 static int lbs_spi_thread(void *data)
828 struct if_spi_card *card = data;
831 struct if_spi_packet *packet;
834 /* Wait to be woken up by one of two things. First, our ISR
835 * could tell us that something happened on the WLAN.
836 * Secondly, libertas could call hw_host_to_card with more
837 * data, which we might be able to send.
840 err = down_interruptible(&card->spi_ready);
841 if (!card->run_thread) {
842 up(&card->spi_thread_terminated);
845 } while (err == EINTR);
847 /* Read the host interrupt status register to see what we
849 err = spu_read_u16(card, IF_SPI_HOST_INT_STATUS_REG,
852 lbs_pr_err("I/O error\n");
856 if (hiStatus & IF_SPI_HIST_CMD_UPLOAD_RDY)
857 err = if_spi_c2h_cmd(card);
860 if (hiStatus & IF_SPI_HIST_RX_UPLOAD_RDY)
861 err = if_spi_c2h_data(card);
864 if (hiStatus & IF_SPI_HIST_CMD_DOWNLOAD_RDY) {
865 /* This means two things. First of all,
866 * if there was a previous command sent, the card has
867 * successfully received it.
868 * Secondly, it is now ready to download another
871 lbs_host_to_card_done(card->priv);
873 /* Do we have any command packets from the host to
876 spin_lock_irqsave(&card->buffer_lock, flags);
877 if (!list_empty(&card->cmd_packet_list)) {
878 packet = (struct if_spi_packet *)(card->
879 cmd_packet_list.next);
880 list_del(&packet->list);
882 spin_unlock_irqrestore(&card->buffer_lock, flags);
885 if_spi_h2c(card, packet, MVMS_CMD);
887 if (hiStatus & IF_SPI_HIST_TX_DOWNLOAD_RDY) {
888 /* Do we have any data packets from the host to
891 spin_lock_irqsave(&card->buffer_lock, flags);
892 if (!list_empty(&card->data_packet_list)) {
893 packet = (struct if_spi_packet *)(card->
894 data_packet_list.next);
895 list_del(&packet->list);
897 spin_unlock_irqrestore(&card->buffer_lock, flags);
900 if_spi_h2c(card, packet, MVMS_DAT);
902 if (hiStatus & IF_SPI_HIST_CARD_EVENT)
907 lbs_pr_err("%s: got error %d\n", __func__, err);
911 /* Block until lbs_spi_thread thread has terminated */
912 static void if_spi_terminate_spi_thread(struct if_spi_card *card)
914 /* It would be nice to use kthread_stop here, but that function
915 * can't wake threads waiting for a semaphore. */
916 card->run_thread = 0;
917 up(&card->spi_ready);
918 down(&card->spi_thread_terminated);
924 * Called from Libertas to transfer some data to the WLAN device
925 * We can't sleep here. */
926 static int if_spi_host_to_card(struct lbs_private *priv,
927 u8 type, u8 *buf, u16 nb)
931 struct if_spi_card *card = priv->card;
932 struct if_spi_packet *packet;
935 lbs_deb_enter_args(LBS_DEB_SPI, "type %d, bytes %d", type, nb);
938 lbs_pr_err("%s: invalid size requested: %d\n", __func__, nb);
943 packet = kzalloc(sizeof(struct if_spi_packet) + blen, GFP_ATOMIC);
949 memcpy(packet->buffer, buf, nb);
950 memset(packet->buffer + nb, 0, blen - nb);
954 priv->dnld_sent = DNLD_CMD_SENT;
955 spin_lock_irqsave(&card->buffer_lock, flags);
956 list_add_tail(&packet->list, &card->cmd_packet_list);
957 spin_unlock_irqrestore(&card->buffer_lock, flags);
960 priv->dnld_sent = DNLD_DATA_SENT;
961 spin_lock_irqsave(&card->buffer_lock, flags);
962 list_add_tail(&packet->list, &card->data_packet_list);
963 spin_unlock_irqrestore(&card->buffer_lock, flags);
966 lbs_pr_err("can't transfer buffer of type %d", type);
971 /* Wake up the spi thread */
972 up(&card->spi_ready);
974 lbs_deb_leave_args(LBS_DEB_SPI, "err=%d", err);
981 * Service incoming interrupts from the WLAN device. We can't sleep here, so
982 * don't try to talk on the SPI bus, just wake up the SPI thread.
984 static irqreturn_t if_spi_host_interrupt(int irq, void *dev_id)
986 struct if_spi_card *card = dev_id;
988 up(&card->spi_ready);
996 static int if_spi_calculate_fw_names(u16 card_id,
997 char *helper_fw, char *main_fw)
1000 for (i = 0; i < ARRAY_SIZE(chip_id_to_device_name); ++i) {
1001 if (card_id == chip_id_to_device_name[i].chip_id)
1004 if (i == ARRAY_SIZE(chip_id_to_device_name)) {
1005 lbs_pr_err("Unsupported chip_id: 0x%02x\n", card_id);
1006 return -EAFNOSUPPORT;
1008 snprintf(helper_fw, FIRMWARE_NAME_MAX, "libertas/gspi%d_hlp.bin",
1009 chip_id_to_device_name[i].name);
1010 snprintf(main_fw, FIRMWARE_NAME_MAX, "libertas/gspi%d.bin",
1011 chip_id_to_device_name[i].name);
1015 static int __devinit if_spi_probe(struct spi_device *spi)
1017 struct if_spi_card *card;
1018 struct lbs_private *priv = NULL;
1019 struct libertas_spi_platform_data *pdata = spi->dev.platform_data;
1023 lbs_deb_enter(LBS_DEB_SPI);
1025 /* Allocate card structure to represent this specific device */
1026 card = kzalloc(sizeof(struct if_spi_card), GFP_KERNEL);
1031 spi_set_drvdata(spi, card);
1033 card->gpio_cs = pdata->gpio_cs;
1034 card->prev_xfer_time = jiffies;
1036 sema_init(&card->spi_ready, 0);
1037 sema_init(&card->spi_thread_terminated, 0);
1038 INIT_LIST_HEAD(&card->cmd_packet_list);
1039 INIT_LIST_HEAD(&card->data_packet_list);
1040 spin_lock_init(&card->buffer_lock);
1042 /* set up GPIO CS line. TODO: use regular CS line */
1043 err = gpio_request(card->gpio_cs, "if_spi_gpio_chip_select");
1046 err = gpio_direction_output(card->gpio_cs, 1);
1050 /* Initialize the SPI Interface Unit */
1051 err = spu_init(card, pdata->use_dummy_writes);
1054 err = spu_get_chip_revision(card, &card->card_id, &card->card_rev);
1059 err = spu_read_u32(card, IF_SPI_SCRATCH_4_REG, &scratch);
1062 if (scratch == SUCCESSFUL_FW_DOWNLOAD_MAGIC)
1063 lbs_deb_spi("Firmware is already loaded for "
1064 "Marvell WLAN 802.11 adapter\n");
1066 err = if_spi_calculate_fw_names(card->card_id,
1067 card->helper_fw_name, card->main_fw_name);
1071 lbs_deb_spi("Initializing FW for Marvell WLAN 802.11 adapter "
1072 "(chip_id = 0x%04x, chip_rev = 0x%02x) "
1073 "attached to SPI bus_num %d, chip_select %d. "
1074 "spi->max_speed_hz=%d\n",
1075 card->card_id, card->card_rev,
1076 spi->master->bus_num, spi->chip_select,
1078 err = if_spi_prog_helper_firmware(card);
1081 err = if_spi_prog_main_firmware(card);
1084 lbs_deb_spi("loaded FW for Marvell WLAN 802.11 adapter\n");
1087 err = spu_set_interrupt_mode(card, 0, 1);
1091 /* Register our card with libertas.
1092 * This will call alloc_etherdev */
1093 priv = lbs_add_card(card, &spi->dev);
1100 priv->hw_host_to_card = if_spi_host_to_card;
1102 priv->ps_supported = 1;
1104 /* Initialize interrupt handling stuff. */
1105 card->run_thread = 1;
1106 card->spi_thread = kthread_run(lbs_spi_thread, card, "lbs_spi_thread");
1107 if (IS_ERR(card->spi_thread)) {
1108 card->run_thread = 0;
1109 err = PTR_ERR(card->spi_thread);
1110 lbs_pr_err("error creating SPI thread: err=%d\n", err);
1113 err = request_irq(spi->irq, if_spi_host_interrupt,
1114 IRQF_TRIGGER_FALLING, "libertas_spi", card);
1116 lbs_pr_err("can't get host irq line-- request_irq failed\n");
1117 goto terminate_thread;
1121 * This will call register_netdev, and we'll start
1122 * getting interrupts... */
1123 err = lbs_start_card(priv);
1127 lbs_deb_spi("Finished initializing WLAN module.\n");
1129 /* successful exit */
1133 free_irq(spi->irq, card);
1135 if_spi_terminate_spi_thread(card);
1137 lbs_remove_card(priv); /* will call free_netdev */
1139 gpio_free(card->gpio_cs);
1141 free_if_spi_card(card);
1143 lbs_deb_leave_args(LBS_DEB_SPI, "err %d\n", err);
1147 static int __devexit libertas_spi_remove(struct spi_device *spi)
1149 struct if_spi_card *card = spi_get_drvdata(spi);
1150 struct lbs_private *priv = card->priv;
1152 lbs_deb_spi("libertas_spi_remove\n");
1153 lbs_deb_enter(LBS_DEB_SPI);
1154 priv->surpriseremoved = 1;
1156 lbs_stop_card(priv);
1157 free_irq(spi->irq, card);
1158 if_spi_terminate_spi_thread(card);
1159 lbs_remove_card(priv); /* will call free_netdev */
1160 gpio_free(card->gpio_cs);
1161 free_if_spi_card(card);
1162 lbs_deb_leave(LBS_DEB_SPI);
1166 static struct spi_driver libertas_spi_driver = {
1167 .probe = if_spi_probe,
1168 .remove = __devexit_p(libertas_spi_remove),
1170 .name = "libertas_spi",
1171 .bus = &spi_bus_type,
1172 .owner = THIS_MODULE,
1180 static int __init if_spi_init_module(void)
1183 lbs_deb_enter(LBS_DEB_SPI);
1184 printk(KERN_INFO "libertas_spi: Libertas SPI driver\n");
1185 ret = spi_register_driver(&libertas_spi_driver);
1186 lbs_deb_leave(LBS_DEB_SPI);
1190 static void __exit if_spi_exit_module(void)
1192 lbs_deb_enter(LBS_DEB_SPI);
1193 spi_unregister_driver(&libertas_spi_driver);
1194 lbs_deb_leave(LBS_DEB_SPI);
1197 module_init(if_spi_init_module);
1198 module_exit(if_spi_exit_module);
1200 MODULE_DESCRIPTION("Libertas SPI WLAN Driver");
1201 MODULE_AUTHOR("Andrey Yurovsky <andrey@cozybit.com>, "
1202 "Colin McCabe <colin@cozybit.com>");
1203 MODULE_LICENSE("GPL");