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;
45 struct libertas_spi_platform_data *pdata;
47 char helper_fw_name[FIRMWARE_NAME_MAX];
48 char main_fw_name[FIRMWARE_NAME_MAX];
50 /* The card ID and card revision, as reported by the hardware. */
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
61 /* The last time that we initiated an SPU operation */
62 unsigned long prev_xfer_time;
65 unsigned long spu_port_delay;
66 unsigned long spu_reg_delay;
68 /* Handles all SPI communication (except for FW load) */
69 struct task_struct *spi_thread;
72 /* Used to wake up the spi_thread */
73 struct semaphore spi_ready;
74 struct semaphore spi_thread_terminated;
76 u8 cmd_buffer[IF_SPI_CMD_BUF_SIZE];
78 /* A buffer of incoming packets from libertas core.
79 * Since we can't sleep in hw_host_to_card, we have to buffer
81 struct list_head cmd_packet_list;
82 struct list_head data_packet_list;
84 /* Protects cmd_packet_list and data_packet_list */
85 spinlock_t buffer_lock;
88 static void free_if_spi_card(struct if_spi_card *card)
90 struct list_head *cursor, *next;
91 struct if_spi_packet *packet;
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);
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);
104 spi_set_drvdata(card->spi, NULL);
108 static struct chip_ident chip_id_to_device_name[] = {
109 { .chip_id = 0x04, .name = 8385 },
110 { .chip_id = 0x0b, .name = 8686 },
114 * SPI Interface Unit Routines
116 * The SPU sits between the host and the WLAN module.
117 * All communication with the firmware is through SPU transactions.
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.
124 static void spu_transaction_init(struct if_spi_card *card)
126 if (!time_after(jiffies, card->prev_xfer_time + 1)) {
127 /* Unfortunately, the SPU requires a delay between successive
128 * transactions. If our last transaction was more than a jiffy
129 * ago, we have obviously already delayed enough.
130 * If not, we have to busy-wait to be on the safe side. */
133 gpio_set_value(card->gpio_cs, 0); /* assert CS */
136 static void spu_transaction_finish(struct if_spi_card *card)
138 gpio_set_value(card->gpio_cs, 1); /* drop CS */
139 card->prev_xfer_time = jiffies;
142 /* Write out a byte buffer to an SPI register,
143 * using a series of 16-bit transfers. */
144 static int spu_write(struct if_spi_card *card, u16 reg, const u8 *buf, int len)
147 u16 reg_out = cpu_to_le16(reg | IF_SPI_WRITE_OPERATION_MASK);
149 /* You must give an even number of bytes to the SPU, even if it
150 * doesn't care about the last one. */
153 spu_transaction_init(card);
155 /* write SPU register index */
156 err = spi_write(card->spi, (u8 *)®_out, sizeof(u16));
160 err = spi_write(card->spi, buf, len);
163 spu_transaction_finish(card);
167 static inline int spu_write_u16(struct if_spi_card *card, u16 reg, u16 val)
171 buff = cpu_to_le16(val);
172 return spu_write(card, reg, (u8 *)&buff, sizeof(u16));
175 static inline int spu_reg_is_port_reg(u16 reg)
178 case IF_SPI_IO_RDWRPORT_REG:
179 case IF_SPI_CMD_RDWRPORT_REG:
180 case IF_SPI_DATA_RDWRPORT_REG:
187 static int spu_read(struct if_spi_card *card, u16 reg, u8 *buf, int len)
189 unsigned int i, delay;
192 u16 reg_out = cpu_to_le16(reg | IF_SPI_READ_OPERATION_MASK);
194 /* You must take an even number of bytes from the SPU, even if you
195 * don't care about the last one. */
198 spu_transaction_init(card);
200 /* write SPU register index */
201 err = spi_write(card->spi, (u8 *)®_out, sizeof(u16));
205 delay = spu_reg_is_port_reg(reg) ? card->spu_port_delay :
207 if (card->use_dummy_writes) {
208 /* Clock in dummy cycles while the SPU fills the FIFO */
209 for (i = 0; i < delay / 16; ++i) {
210 err = spi_write(card->spi, (u8 *)&zero, sizeof(u16));
215 /* Busy-wait while the SPU fills the FIFO */
216 ndelay(100 + (delay * 10));
220 err = spi_read(card->spi, buf, len);
223 spu_transaction_finish(card);
227 /* Read 16 bits from an SPI register */
228 static inline int spu_read_u16(struct if_spi_card *card, u16 reg, u16 *val)
233 ret = spu_read(card, reg, (u8 *)&buf, sizeof(buf));
235 *val = le16_to_cpup(&buf);
239 /* Read 32 bits from an SPI register.
240 * The low 16 bits are read first. */
241 static int spu_read_u32(struct if_spi_card *card, u16 reg, u32 *val)
246 err = spu_read(card, reg, (u8 *)&buf, sizeof(buf));
248 *val = le32_to_cpup(&buf);
252 /* Keep reading 16 bits from an SPI register until you get the correct result.
254 * If mask = 0, the correct result is any non-zero number.
255 * If mask != 0, the correct result is any number where
256 * number & target_mask == target
258 * Returns -ETIMEDOUT if a second passes without the correct result. */
259 static int spu_wait_for_u16(struct if_spi_card *card, u16 reg,
260 u16 target_mask, u16 target)
263 unsigned long timeout = jiffies + 5*HZ;
266 err = spu_read_u16(card, reg, &val);
270 if ((val & target_mask) == target)
277 if (time_after(jiffies, timeout)) {
278 lbs_pr_err("%s: timeout with val=%02x, "
279 "target_mask=%02x, target=%02x\n",
280 __func__, val, target_mask, target);
286 /* Read 16 bits from an SPI register until you receive a specific value.
287 * Returns -ETIMEDOUT if a 4 tries pass without success. */
288 static int spu_wait_for_u32(struct if_spi_card *card, u32 reg, u32 target)
291 for (try = 0; try < 4; ++try) {
293 err = spu_read_u32(card, reg, &val);
303 static int spu_set_interrupt_mode(struct if_spi_card *card,
304 int suppress_host_int,
309 /* We can suppress a host interrupt by clearing the appropriate
310 * bit in the "host interrupt status mask" register */
311 if (suppress_host_int) {
312 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG, 0);
316 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG,
317 IF_SPI_HISM_TX_DOWNLOAD_RDY |
318 IF_SPI_HISM_RX_UPLOAD_RDY |
319 IF_SPI_HISM_CMD_DOWNLOAD_RDY |
320 IF_SPI_HISM_CARDEVENT |
321 IF_SPI_HISM_CMD_UPLOAD_RDY);
326 /* If auto-interrupts are on, the completion of certain transactions
327 * will trigger an interrupt automatically. If auto-interrupts
328 * are off, we need to set the "Card Interrupt Cause" register to
329 * trigger a card interrupt. */
331 err = spu_write_u16(card, IF_SPI_HOST_INT_CTRL_REG,
332 IF_SPI_HICT_TX_DOWNLOAD_OVER_AUTO |
333 IF_SPI_HICT_RX_UPLOAD_OVER_AUTO |
334 IF_SPI_HICT_CMD_DOWNLOAD_OVER_AUTO |
335 IF_SPI_HICT_CMD_UPLOAD_OVER_AUTO);
339 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG, 0);
346 static int spu_get_chip_revision(struct if_spi_card *card,
347 u16 *card_id, u8 *card_rev)
351 err = spu_read_u32(card, IF_SPI_DEVICEID_CTRL_REG, &dev_ctrl);
354 *card_id = IF_SPI_DEVICEID_CTRL_REG_TO_CARD_ID(dev_ctrl);
355 *card_rev = IF_SPI_DEVICEID_CTRL_REG_TO_CARD_REV(dev_ctrl);
359 static int spu_set_bus_mode(struct if_spi_card *card, u16 mode)
364 err = spu_write_u16(card, IF_SPI_SPU_BUS_MODE_REG, mode);
367 /* Check that we were able to read back what we just wrote. */
368 err = spu_read_u16(card, IF_SPI_SPU_BUS_MODE_REG, &rval);
372 lbs_pr_err("Can't read bus mode register.\n");
378 static int spu_init(struct if_spi_card *card, int use_dummy_writes)
383 /* We have to start up in timed delay mode so that we can safely
384 * read the Delay Read Register. */
385 card->use_dummy_writes = 0;
386 err = spu_set_bus_mode(card,
387 IF_SPI_BUS_MODE_SPI_CLOCK_PHASE_RISING |
388 IF_SPI_BUS_MODE_DELAY_METHOD_TIMED |
389 IF_SPI_BUS_MODE_16_BIT_ADDRESS_16_BIT_DATA);
392 card->spu_port_delay = 1000;
393 card->spu_reg_delay = 1000;
394 err = spu_read_u32(card, IF_SPI_DELAY_READ_REG, &delay);
397 card->spu_port_delay = delay & 0x0000ffff;
398 card->spu_reg_delay = (delay & 0xffff0000) >> 16;
400 /* If dummy clock delay mode has been requested, switch to it now */
401 if (use_dummy_writes) {
402 card->use_dummy_writes = 1;
403 err = spu_set_bus_mode(card,
404 IF_SPI_BUS_MODE_SPI_CLOCK_PHASE_RISING |
405 IF_SPI_BUS_MODE_DELAY_METHOD_DUMMY_CLOCK |
406 IF_SPI_BUS_MODE_16_BIT_ADDRESS_16_BIT_DATA);
411 lbs_deb_spi("Initialized SPU unit. "
412 "spu_port_delay=0x%04lx, spu_reg_delay=0x%04lx\n",
413 card->spu_port_delay, card->spu_reg_delay);
421 static int if_spi_prog_helper_firmware(struct if_spi_card *card)
424 const struct firmware *firmware = NULL;
427 u8 temp[HELPER_FW_LOAD_CHUNK_SZ];
428 struct spi_device *spi = card->spi;
430 lbs_deb_enter(LBS_DEB_SPI);
432 err = spu_set_interrupt_mode(card, 1, 0);
435 /* Get helper firmware image */
436 err = request_firmware(&firmware, card->helper_fw_name, &spi->dev);
438 lbs_pr_err("request_firmware failed with err = %d\n", err);
441 bytes_remaining = firmware->size;
444 /* Load helper firmware image */
445 while (bytes_remaining > 0) {
446 /* Scratch pad 1 should contain the number of bytes we
447 * want to download to the firmware */
448 err = spu_write_u16(card, IF_SPI_SCRATCH_1_REG,
449 HELPER_FW_LOAD_CHUNK_SZ);
451 goto release_firmware;
453 err = spu_wait_for_u16(card, IF_SPI_HOST_INT_STATUS_REG,
454 IF_SPI_HIST_CMD_DOWNLOAD_RDY,
455 IF_SPI_HIST_CMD_DOWNLOAD_RDY);
457 goto release_firmware;
459 /* Feed the data into the command read/write port reg
460 * in chunks of 64 bytes */
461 memset(temp, 0, sizeof(temp));
463 min(bytes_remaining, HELPER_FW_LOAD_CHUNK_SZ));
465 err = spu_write(card, IF_SPI_CMD_RDWRPORT_REG,
466 temp, HELPER_FW_LOAD_CHUNK_SZ);
468 goto release_firmware;
470 /* Interrupt the boot code */
471 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
473 goto release_firmware;
474 err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG,
475 IF_SPI_CIC_CMD_DOWNLOAD_OVER);
477 goto release_firmware;
478 bytes_remaining -= HELPER_FW_LOAD_CHUNK_SZ;
479 fw += HELPER_FW_LOAD_CHUNK_SZ;
482 /* Once the helper / single stage firmware download is complete,
483 * write 0 to scratch pad 1 and interrupt the
484 * bootloader. This completes the helper download. */
485 err = spu_write_u16(card, IF_SPI_SCRATCH_1_REG, FIRMWARE_DNLD_OK);
487 goto release_firmware;
488 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
490 goto release_firmware;
491 err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG,
492 IF_SPI_CIC_CMD_DOWNLOAD_OVER);
493 goto release_firmware;
495 lbs_deb_spi("waiting for helper to boot...\n");
498 release_firmware(firmware);
501 lbs_pr_err("failed to load helper firmware (err=%d)\n", err);
502 lbs_deb_leave_args(LBS_DEB_SPI, "err %d", err);
506 /* Returns the length of the next packet the firmware expects us to send
507 * Sets crc_err if the previous transfer had a CRC error. */
508 static int if_spi_prog_main_firmware_check_len(struct if_spi_card *card,
514 /* wait until the host interrupt status register indicates
515 * that we are ready to download */
516 err = spu_wait_for_u16(card, IF_SPI_HOST_INT_STATUS_REG,
517 IF_SPI_HIST_CMD_DOWNLOAD_RDY,
518 IF_SPI_HIST_CMD_DOWNLOAD_RDY);
520 lbs_pr_err("timed out waiting for host_int_status\n");
524 /* Ask the device how many bytes of firmware it wants. */
525 err = spu_read_u16(card, IF_SPI_SCRATCH_1_REG, &len);
529 if (len > IF_SPI_CMD_BUF_SIZE) {
530 lbs_pr_err("firmware load device requested a larger "
531 "tranfer than we are prepared to "
532 "handle. (len = %d)\n", len);
536 lbs_deb_spi("%s: crc error\n", __func__);
545 static int if_spi_prog_main_firmware(struct if_spi_card *card)
548 int bytes, crc_err = 0, err = 0;
549 const struct firmware *firmware = NULL;
551 struct spi_device *spi = card->spi;
554 lbs_deb_enter(LBS_DEB_SPI);
556 err = spu_set_interrupt_mode(card, 1, 0);
560 /* Get firmware image */
561 err = request_firmware(&firmware, card->main_fw_name, &spi->dev);
563 lbs_pr_err("%s: can't get firmware '%s' from kernel. "
564 "err = %d\n", __func__, card->main_fw_name, err);
568 err = spu_wait_for_u16(card, IF_SPI_SCRATCH_1_REG, 0, 0);
570 lbs_pr_err("%s: timed out waiting for initial "
571 "scratch reg = 0\n", __func__);
572 goto release_firmware;
577 bytes = firmware->size;
579 while ((len = if_spi_prog_main_firmware_check_len(card, &crc_err))) {
582 goto release_firmware;
585 /* If there are no more bytes left, we would normally
586 * expect to have terminated with len = 0 */
587 lbs_pr_err("Firmware load wants more bytes "
588 "than we have to offer.\n");
592 /* Previous transfer failed. */
593 if (++num_crc_errs > MAX_MAIN_FW_LOAD_CRC_ERR) {
594 lbs_pr_err("Too many CRC errors encountered "
595 "in firmware load.\n");
597 goto release_firmware;
600 /* Previous transfer succeeded. Advance counters. */
605 memset(card->cmd_buffer, 0, len);
606 memcpy(card->cmd_buffer, fw, bytes);
608 memcpy(card->cmd_buffer, fw, len);
610 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
612 goto release_firmware;
613 err = spu_write(card, IF_SPI_CMD_RDWRPORT_REG,
614 card->cmd_buffer, len);
616 goto release_firmware;
617 err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG ,
618 IF_SPI_CIC_CMD_DOWNLOAD_OVER);
620 goto release_firmware;
623 if (bytes > prev_len) {
624 lbs_pr_err("firmware load wants fewer bytes than "
625 "we have to offer.\n");
628 /* Confirm firmware download */
629 err = spu_wait_for_u32(card, IF_SPI_SCRATCH_4_REG,
630 SUCCESSFUL_FW_DOWNLOAD_MAGIC);
632 lbs_pr_err("failed to confirm the firmware download\n");
633 goto release_firmware;
637 release_firmware(firmware);
641 lbs_pr_err("failed to load firmware (err=%d)\n", err);
642 lbs_deb_leave_args(LBS_DEB_SPI, "err %d", err);
647 * SPI Transfer Thread
649 * The SPI thread handles all SPI transfers, so there is no need for a lock.
652 /* Move a command from the card to the host */
653 static int if_spi_c2h_cmd(struct if_spi_card *card)
655 struct lbs_private *priv = card->priv;
661 /* We need a buffer big enough to handle whatever people send to
663 BUILD_BUG_ON(IF_SPI_CMD_BUF_SIZE < LBS_CMD_BUFFER_SIZE);
664 BUILD_BUG_ON(IF_SPI_CMD_BUF_SIZE < LBS_UPLD_SIZE);
666 /* It's just annoying if the buffer size isn't a multiple of 4, because
667 * then we might have len < IF_SPI_CMD_BUF_SIZE but
668 * ALIGN(len, 4) > IF_SPI_CMD_BUF_SIZE */
669 BUILD_BUG_ON(IF_SPI_CMD_BUF_SIZE % 4 != 0);
671 lbs_deb_enter(LBS_DEB_SPI);
673 /* How many bytes are there to read? */
674 err = spu_read_u16(card, IF_SPI_SCRATCH_2_REG, &len);
678 lbs_pr_err("%s: error: card has no data for host\n",
682 } else if (len > IF_SPI_CMD_BUF_SIZE) {
683 lbs_pr_err("%s: error: response packet too large: "
684 "%d bytes, but maximum is %d\n",
685 __func__, len, IF_SPI_CMD_BUF_SIZE);
690 /* Read the data from the WLAN module into our command buffer */
691 err = spu_read(card, IF_SPI_CMD_RDWRPORT_REG,
692 card->cmd_buffer, ALIGN(len, 4));
696 spin_lock_irqsave(&priv->driver_lock, flags);
697 i = (priv->resp_idx == 0) ? 1 : 0;
698 BUG_ON(priv->resp_len[i]);
699 priv->resp_len[i] = len;
700 memcpy(priv->resp_buf[i], card->cmd_buffer, len);
701 lbs_notify_command_response(priv, i);
702 spin_unlock_irqrestore(&priv->driver_lock, flags);
706 lbs_pr_err("%s: err=%d\n", __func__, err);
707 lbs_deb_leave(LBS_DEB_SPI);
711 /* Move data from the card to the host */
712 static int if_spi_c2h_data(struct if_spi_card *card)
719 lbs_deb_enter(LBS_DEB_SPI);
721 /* How many bytes are there to read? */
722 err = spu_read_u16(card, IF_SPI_SCRATCH_1_REG, &len);
726 lbs_pr_err("%s: error: card has no data for host\n",
730 } else if (len > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
731 lbs_pr_err("%s: error: card has %d bytes of data, but "
732 "our maximum skb size is %lu\n",
733 __func__, len, MRVDRV_ETH_RX_PACKET_BUFFER_SIZE);
738 /* TODO: should we allocate a smaller skb if we have less data? */
739 skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE);
744 skb_reserve(skb, IPFIELD_ALIGN_OFFSET);
745 data = skb_put(skb, len);
747 /* Read the data from the WLAN module into our skb... */
748 err = spu_read(card, IF_SPI_DATA_RDWRPORT_REG, data, ALIGN(len, 4));
752 /* pass the SKB to libertas */
753 err = lbs_process_rxed_packet(card->priv, skb);
764 lbs_pr_err("%s: err=%d\n", __func__, err);
765 lbs_deb_leave(LBS_DEB_SPI);
769 /* Move data or a command from the host to the card. */
770 static void if_spi_h2c(struct if_spi_card *card,
771 struct if_spi_packet *packet, int type)
774 u16 int_type, port_reg;
778 int_type = IF_SPI_CIC_TX_DOWNLOAD_OVER;
779 port_reg = IF_SPI_DATA_RDWRPORT_REG;
782 int_type = IF_SPI_CIC_CMD_DOWNLOAD_OVER;
783 port_reg = IF_SPI_CMD_RDWRPORT_REG;
786 lbs_pr_err("can't transfer buffer of type %d\n", type);
791 /* Write the data to the card */
792 err = spu_write(card, port_reg, packet->buffer, packet->blen);
800 lbs_pr_err("%s: error %d\n", __func__, err);
803 /* Inform the host about a card event */
804 static void if_spi_e2h(struct if_spi_card *card)
809 struct lbs_private *priv = card->priv;
811 err = spu_read_u32(card, IF_SPI_SCRATCH_3_REG, &cause);
815 /* re-enable the card event interrupt */
816 spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG,
817 ~IF_SPI_HICU_CARD_EVENT);
819 /* generate a card interrupt */
820 spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG, IF_SPI_CIC_HOST_EVENT);
822 spin_lock_irqsave(&priv->driver_lock, flags);
823 lbs_queue_event(priv, cause & 0xff);
824 spin_unlock_irqrestore(&priv->driver_lock, flags);
828 lbs_pr_err("%s: error %d\n", __func__, err);
831 static int lbs_spi_thread(void *data)
834 struct if_spi_card *card = data;
837 struct if_spi_packet *packet;
840 /* Wait to be woken up by one of two things. First, our ISR
841 * could tell us that something happened on the WLAN.
842 * Secondly, libertas could call hw_host_to_card with more
843 * data, which we might be able to send.
846 err = down_interruptible(&card->spi_ready);
847 if (!card->run_thread) {
848 up(&card->spi_thread_terminated);
851 } while (err == EINTR);
853 /* Read the host interrupt status register to see what we
855 err = spu_read_u16(card, IF_SPI_HOST_INT_STATUS_REG,
858 lbs_pr_err("I/O error\n");
862 if (hiStatus & IF_SPI_HIST_CMD_UPLOAD_RDY)
863 err = if_spi_c2h_cmd(card);
866 if (hiStatus & IF_SPI_HIST_RX_UPLOAD_RDY)
867 err = if_spi_c2h_data(card);
870 if (hiStatus & IF_SPI_HIST_CMD_DOWNLOAD_RDY) {
871 /* This means two things. First of all,
872 * if there was a previous command sent, the card has
873 * successfully received it.
874 * Secondly, it is now ready to download another
877 lbs_host_to_card_done(card->priv);
879 /* Do we have any command packets from the host to
882 spin_lock_irqsave(&card->buffer_lock, flags);
883 if (!list_empty(&card->cmd_packet_list)) {
884 packet = (struct if_spi_packet *)(card->
885 cmd_packet_list.next);
886 list_del(&packet->list);
888 spin_unlock_irqrestore(&card->buffer_lock, flags);
891 if_spi_h2c(card, packet, MVMS_CMD);
893 if (hiStatus & IF_SPI_HIST_TX_DOWNLOAD_RDY) {
894 /* Do we have any data packets from the host to
897 spin_lock_irqsave(&card->buffer_lock, flags);
898 if (!list_empty(&card->data_packet_list)) {
899 packet = (struct if_spi_packet *)(card->
900 data_packet_list.next);
901 list_del(&packet->list);
903 spin_unlock_irqrestore(&card->buffer_lock, flags);
906 if_spi_h2c(card, packet, MVMS_DAT);
908 if (hiStatus & IF_SPI_HIST_CARD_EVENT)
913 lbs_pr_err("%s: got error %d\n", __func__, err);
917 /* Block until lbs_spi_thread thread has terminated */
918 static void if_spi_terminate_spi_thread(struct if_spi_card *card)
920 /* It would be nice to use kthread_stop here, but that function
921 * can't wake threads waiting for a semaphore. */
922 card->run_thread = 0;
923 up(&card->spi_ready);
924 down(&card->spi_thread_terminated);
930 * Called from Libertas to transfer some data to the WLAN device
931 * We can't sleep here. */
932 static int if_spi_host_to_card(struct lbs_private *priv,
933 u8 type, u8 *buf, u16 nb)
937 struct if_spi_card *card = priv->card;
938 struct if_spi_packet *packet;
941 lbs_deb_enter_args(LBS_DEB_SPI, "type %d, bytes %d", type, nb);
944 lbs_pr_err("%s: invalid size requested: %d\n", __func__, nb);
949 packet = kzalloc(sizeof(struct if_spi_packet) + blen, GFP_ATOMIC);
955 memcpy(packet->buffer, buf, nb);
956 memset(packet->buffer + nb, 0, blen - nb);
960 priv->dnld_sent = DNLD_CMD_SENT;
961 spin_lock_irqsave(&card->buffer_lock, flags);
962 list_add_tail(&packet->list, &card->cmd_packet_list);
963 spin_unlock_irqrestore(&card->buffer_lock, flags);
966 priv->dnld_sent = DNLD_DATA_SENT;
967 spin_lock_irqsave(&card->buffer_lock, flags);
968 list_add_tail(&packet->list, &card->data_packet_list);
969 spin_unlock_irqrestore(&card->buffer_lock, flags);
972 lbs_pr_err("can't transfer buffer of type %d", type);
977 /* Wake up the spi thread */
978 up(&card->spi_ready);
980 lbs_deb_leave_args(LBS_DEB_SPI, "err=%d", err);
987 * Service incoming interrupts from the WLAN device. We can't sleep here, so
988 * don't try to talk on the SPI bus, just wake up the SPI thread.
990 static irqreturn_t if_spi_host_interrupt(int irq, void *dev_id)
992 struct if_spi_card *card = dev_id;
994 up(&card->spi_ready);
1002 static int if_spi_calculate_fw_names(u16 card_id,
1003 char *helper_fw, char *main_fw)
1006 for (i = 0; i < ARRAY_SIZE(chip_id_to_device_name); ++i) {
1007 if (card_id == chip_id_to_device_name[i].chip_id)
1010 if (i == ARRAY_SIZE(chip_id_to_device_name)) {
1011 lbs_pr_err("Unsupported chip_id: 0x%02x\n", card_id);
1012 return -EAFNOSUPPORT;
1014 snprintf(helper_fw, FIRMWARE_NAME_MAX, "libertas/gspi%d_hlp.bin",
1015 chip_id_to_device_name[i].name);
1016 snprintf(main_fw, FIRMWARE_NAME_MAX, "libertas/gspi%d.bin",
1017 chip_id_to_device_name[i].name);
1021 static int __devinit if_spi_probe(struct spi_device *spi)
1023 struct if_spi_card *card;
1024 struct lbs_private *priv = NULL;
1025 struct libertas_spi_platform_data *pdata = spi->dev.platform_data;
1028 struct sched_param param = { .sched_priority = 1 };
1030 lbs_deb_enter(LBS_DEB_SPI);
1038 err = pdata->setup(spi);
1043 /* Allocate card structure to represent this specific device */
1044 card = kzalloc(sizeof(struct if_spi_card), GFP_KERNEL);
1049 spi_set_drvdata(spi, card);
1050 card->pdata = pdata;
1052 card->gpio_cs = pdata->gpio_cs;
1053 card->prev_xfer_time = jiffies;
1055 sema_init(&card->spi_ready, 0);
1056 sema_init(&card->spi_thread_terminated, 0);
1057 INIT_LIST_HEAD(&card->cmd_packet_list);
1058 INIT_LIST_HEAD(&card->data_packet_list);
1059 spin_lock_init(&card->buffer_lock);
1061 /* set up GPIO CS line. TODO: use regular CS line */
1062 err = gpio_request(card->gpio_cs, "if_spi_gpio_chip_select");
1065 err = gpio_direction_output(card->gpio_cs, 1);
1069 /* Initialize the SPI Interface Unit */
1070 err = spu_init(card, pdata->use_dummy_writes);
1073 err = spu_get_chip_revision(card, &card->card_id, &card->card_rev);
1078 err = spu_read_u32(card, IF_SPI_SCRATCH_4_REG, &scratch);
1081 if (scratch == SUCCESSFUL_FW_DOWNLOAD_MAGIC)
1082 lbs_deb_spi("Firmware is already loaded for "
1083 "Marvell WLAN 802.11 adapter\n");
1085 err = if_spi_calculate_fw_names(card->card_id,
1086 card->helper_fw_name, card->main_fw_name);
1090 lbs_deb_spi("Initializing FW for Marvell WLAN 802.11 adapter "
1091 "(chip_id = 0x%04x, chip_rev = 0x%02x) "
1092 "attached to SPI bus_num %d, chip_select %d. "
1093 "spi->max_speed_hz=%d\n",
1094 card->card_id, card->card_rev,
1095 spi->master->bus_num, spi->chip_select,
1097 err = if_spi_prog_helper_firmware(card);
1100 err = if_spi_prog_main_firmware(card);
1103 lbs_deb_spi("loaded FW for Marvell WLAN 802.11 adapter\n");
1106 err = spu_set_interrupt_mode(card, 0, 1);
1110 /* Register our card with libertas.
1111 * This will call alloc_etherdev */
1112 priv = lbs_add_card(card, &spi->dev);
1119 priv->hw_host_to_card = if_spi_host_to_card;
1121 priv->ps_supported = 1;
1123 /* Initialize interrupt handling stuff. */
1124 card->run_thread = 1;
1125 card->spi_thread = kthread_run(lbs_spi_thread, card, "lbs_spi_thread");
1126 if (IS_ERR(card->spi_thread)) {
1127 card->run_thread = 0;
1128 err = PTR_ERR(card->spi_thread);
1129 lbs_pr_err("error creating SPI thread: err=%d\n", err);
1132 if (sched_setscheduler(card->spi_thread, SCHED_FIFO, ¶m))
1133 lbs_pr_err("Error setting scheduler, using default.\n");
1135 err = request_irq(spi->irq, if_spi_host_interrupt,
1136 IRQF_TRIGGER_FALLING, "libertas_spi", card);
1138 lbs_pr_err("can't get host irq line-- request_irq failed\n");
1139 goto terminate_thread;
1143 * This will call register_netdev, and we'll start
1144 * getting interrupts... */
1145 err = lbs_start_card(priv);
1149 lbs_deb_spi("Finished initializing WLAN module.\n");
1151 /* successful exit */
1155 free_irq(spi->irq, card);
1157 if_spi_terminate_spi_thread(card);
1159 lbs_remove_card(priv); /* will call free_netdev */
1161 gpio_free(card->gpio_cs);
1163 free_if_spi_card(card);
1165 lbs_deb_leave_args(LBS_DEB_SPI, "err %d\n", err);
1169 static int __devexit libertas_spi_remove(struct spi_device *spi)
1171 struct if_spi_card *card = spi_get_drvdata(spi);
1172 struct lbs_private *priv = card->priv;
1174 lbs_deb_spi("libertas_spi_remove\n");
1175 lbs_deb_enter(LBS_DEB_SPI);
1176 priv->surpriseremoved = 1;
1178 lbs_stop_card(priv);
1179 free_irq(spi->irq, card);
1180 if_spi_terminate_spi_thread(card);
1181 lbs_remove_card(priv); /* will call free_netdev */
1182 gpio_free(card->gpio_cs);
1183 if (card->pdata->teardown)
1184 card->pdata->teardown(spi);
1185 free_if_spi_card(card);
1186 lbs_deb_leave(LBS_DEB_SPI);
1190 static struct spi_driver libertas_spi_driver = {
1191 .probe = if_spi_probe,
1192 .remove = __devexit_p(libertas_spi_remove),
1194 .name = "libertas_spi",
1195 .bus = &spi_bus_type,
1196 .owner = THIS_MODULE,
1204 static int __init if_spi_init_module(void)
1207 lbs_deb_enter(LBS_DEB_SPI);
1208 printk(KERN_INFO "libertas_spi: Libertas SPI driver\n");
1209 ret = spi_register_driver(&libertas_spi_driver);
1210 lbs_deb_leave(LBS_DEB_SPI);
1214 static void __exit if_spi_exit_module(void)
1216 lbs_deb_enter(LBS_DEB_SPI);
1217 spi_unregister_driver(&libertas_spi_driver);
1218 lbs_deb_leave(LBS_DEB_SPI);
1221 module_init(if_spi_init_module);
1222 module_exit(if_spi_exit_module);
1224 MODULE_DESCRIPTION("Libertas SPI WLAN Driver");
1225 MODULE_AUTHOR("Andrey Yurovsky <andrey@cozybit.com>, "
1226 "Colin McCabe <colin@cozybit.com>");
1227 MODULE_LICENSE("GPL");