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/jiffies.h>
23 #include <linux/kthread.h>
24 #include <linux/list.h>
25 #include <linux/netdevice.h>
26 #include <linux/spi/libertas_spi.h>
27 #include <linux/spi/spi.h>
35 struct if_spi_packet {
36 struct list_head list;
38 u8 buffer[0] __attribute__((aligned(4)));
42 struct spi_device *spi;
43 struct lbs_private *priv;
44 struct libertas_spi_platform_data *pdata;
46 char helper_fw_name[IF_SPI_FW_NAME_MAX];
47 char main_fw_name[IF_SPI_FW_NAME_MAX];
49 /* The card ID and card revision, as reported by the hardware. */
53 /* The last time that we initiated an SPU operation */
54 unsigned long prev_xfer_time;
57 unsigned long spu_port_delay;
58 unsigned long spu_reg_delay;
60 /* Handles all SPI communication (except for FW load) */
61 struct task_struct *spi_thread;
64 /* Used to wake up the spi_thread */
65 struct semaphore spi_ready;
66 struct semaphore spi_thread_terminated;
68 u8 cmd_buffer[IF_SPI_CMD_BUF_SIZE];
70 /* A buffer of incoming packets from libertas core.
71 * Since we can't sleep in hw_host_to_card, we have to buffer
73 struct list_head cmd_packet_list;
74 struct list_head data_packet_list;
76 /* Protects cmd_packet_list and data_packet_list */
77 spinlock_t buffer_lock;
80 static void free_if_spi_card(struct if_spi_card *card)
82 struct list_head *cursor, *next;
83 struct if_spi_packet *packet;
85 BUG_ON(card->run_thread);
86 list_for_each_safe(cursor, next, &card->cmd_packet_list) {
87 packet = container_of(cursor, struct if_spi_packet, list);
88 list_del(&packet->list);
91 list_for_each_safe(cursor, next, &card->data_packet_list) {
92 packet = container_of(cursor, struct if_spi_packet, list);
93 list_del(&packet->list);
96 spi_set_drvdata(card->spi, NULL);
100 static struct chip_ident chip_id_to_device_name[] = {
101 { .chip_id = 0x04, .name = 8385 },
102 { .chip_id = 0x0b, .name = 8686 },
106 * SPI Interface Unit Routines
108 * The SPU sits between the host and the WLAN module.
109 * All communication with the firmware is through SPU transactions.
111 * First we have to put a SPU register name on the bus. Then we can
112 * either read from or write to that register.
116 static void spu_transaction_init(struct if_spi_card *card)
118 if (!time_after(jiffies, card->prev_xfer_time + 1)) {
119 /* Unfortunately, the SPU requires a delay between successive
120 * transactions. If our last transaction was more than a jiffy
121 * ago, we have obviously already delayed enough.
122 * If not, we have to busy-wait to be on the safe side. */
127 static void spu_transaction_finish(struct if_spi_card *card)
129 card->prev_xfer_time = jiffies;
132 /* Write out a byte buffer to an SPI register,
133 * using a series of 16-bit transfers. */
134 static int spu_write(struct if_spi_card *card, u16 reg, const u8 *buf, int len)
137 u16 reg_out = cpu_to_le16(reg | IF_SPI_WRITE_OPERATION_MASK);
138 struct spi_message m;
139 struct spi_transfer reg_trans;
140 struct spi_transfer data_trans;
142 spi_message_init(&m);
143 memset(®_trans, 0, sizeof(reg_trans));
144 memset(&data_trans, 0, sizeof(data_trans));
146 /* You must give an even number of bytes to the SPU, even if it
147 * doesn't care about the last one. */
150 spu_transaction_init(card);
152 /* write SPU register index */
153 reg_trans.tx_buf = ®_out;
154 reg_trans.len = sizeof(reg_out);
156 data_trans.tx_buf = buf;
157 data_trans.len = len;
159 spi_message_add_tail(®_trans, &m);
160 spi_message_add_tail(&data_trans, &m);
162 err = spi_sync(card->spi, &m);
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)
191 u16 reg_out = cpu_to_le16(reg | IF_SPI_READ_OPERATION_MASK);
192 struct spi_message m;
193 struct spi_transfer reg_trans;
194 struct spi_transfer dummy_trans;
195 struct spi_transfer data_trans;
197 /* You must take an even number of bytes from the SPU, even if you
198 * don't care about the last one. */
201 spu_transaction_init(card);
203 spi_message_init(&m);
204 memset(®_trans, 0, sizeof(reg_trans));
205 memset(&dummy_trans, 0, sizeof(dummy_trans));
206 memset(&data_trans, 0, sizeof(data_trans));
208 /* write SPU register index */
209 reg_trans.tx_buf = ®_out;
210 reg_trans.len = sizeof(reg_out);
211 spi_message_add_tail(®_trans, &m);
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 dummy_trans.len = delay / 8;
218 spi_message_add_tail(&dummy_trans, &m);
220 /* Busy-wait while the SPU fills the FIFO */
221 reg_trans.delay_usecs =
222 DIV_ROUND_UP((100 + (delay * 10)), 1000);
226 data_trans.rx_buf = buf;
227 data_trans.len = len;
228 spi_message_add_tail(&data_trans, &m);
230 err = spi_sync(card->spi, &m);
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)
241 ret = spu_read(card, reg, (u8 *)&buf, sizeof(buf));
243 *val = le16_to_cpup(&buf);
247 /* Read 32 bits from an SPI register.
248 * The low 16 bits are read first. */
249 static int spu_read_u32(struct if_spi_card *card, u16 reg, u32 *val)
254 err = spu_read(card, reg, (u8 *)&buf, sizeof(buf));
256 *val = le32_to_cpup(&buf);
260 /* Keep reading 16 bits from an SPI register until you get the correct result.
262 * If mask = 0, the correct result is any non-zero number.
263 * If mask != 0, the correct result is any number where
264 * number & target_mask == target
266 * Returns -ETIMEDOUT if a second passes without the correct result. */
267 static int spu_wait_for_u16(struct if_spi_card *card, u16 reg,
268 u16 target_mask, u16 target)
271 unsigned long timeout = jiffies + 5*HZ;
274 err = spu_read_u16(card, reg, &val);
278 if ((val & target_mask) == target)
285 if (time_after(jiffies, timeout)) {
286 lbs_pr_err("%s: timeout with val=%02x, "
287 "target_mask=%02x, target=%02x\n",
288 __func__, val, target_mask, target);
294 /* Read 16 bits from an SPI register until you receive a specific value.
295 * Returns -ETIMEDOUT if a 4 tries pass without success. */
296 static int spu_wait_for_u32(struct if_spi_card *card, u32 reg, u32 target)
299 for (try = 0; try < 4; ++try) {
301 err = spu_read_u32(card, reg, &val);
311 static int spu_set_interrupt_mode(struct if_spi_card *card,
312 int suppress_host_int,
317 /* We can suppress a host interrupt by clearing the appropriate
318 * bit in the "host interrupt status mask" register */
319 if (suppress_host_int) {
320 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG, 0);
324 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG,
325 IF_SPI_HISM_TX_DOWNLOAD_RDY |
326 IF_SPI_HISM_RX_UPLOAD_RDY |
327 IF_SPI_HISM_CMD_DOWNLOAD_RDY |
328 IF_SPI_HISM_CARDEVENT |
329 IF_SPI_HISM_CMD_UPLOAD_RDY);
334 /* If auto-interrupts are on, the completion of certain transactions
335 * will trigger an interrupt automatically. If auto-interrupts
336 * are off, we need to set the "Card Interrupt Cause" register to
337 * trigger a card interrupt. */
339 err = spu_write_u16(card, IF_SPI_HOST_INT_CTRL_REG,
340 IF_SPI_HICT_TX_DOWNLOAD_OVER_AUTO |
341 IF_SPI_HICT_RX_UPLOAD_OVER_AUTO |
342 IF_SPI_HICT_CMD_DOWNLOAD_OVER_AUTO |
343 IF_SPI_HICT_CMD_UPLOAD_OVER_AUTO);
347 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG, 0);
354 static int spu_get_chip_revision(struct if_spi_card *card,
355 u16 *card_id, u8 *card_rev)
359 err = spu_read_u32(card, IF_SPI_DEVICEID_CTRL_REG, &dev_ctrl);
362 *card_id = IF_SPI_DEVICEID_CTRL_REG_TO_CARD_ID(dev_ctrl);
363 *card_rev = IF_SPI_DEVICEID_CTRL_REG_TO_CARD_REV(dev_ctrl);
367 static int spu_set_bus_mode(struct if_spi_card *card, u16 mode)
372 err = spu_write_u16(card, IF_SPI_SPU_BUS_MODE_REG, mode);
375 /* Check that we were able to read back what we just wrote. */
376 err = spu_read_u16(card, IF_SPI_SPU_BUS_MODE_REG, &rval);
380 lbs_pr_err("Can't read bus mode register.\n");
386 static int spu_init(struct if_spi_card *card, int use_dummy_writes)
391 /* We have to start up in timed delay mode so that we can safely
392 * read the Delay Read Register. */
393 card->use_dummy_writes = 0;
394 err = spu_set_bus_mode(card,
395 IF_SPI_BUS_MODE_SPI_CLOCK_PHASE_RISING |
396 IF_SPI_BUS_MODE_DELAY_METHOD_TIMED |
397 IF_SPI_BUS_MODE_16_BIT_ADDRESS_16_BIT_DATA);
400 card->spu_port_delay = 1000;
401 card->spu_reg_delay = 1000;
402 err = spu_read_u32(card, IF_SPI_DELAY_READ_REG, &delay);
405 card->spu_port_delay = delay & 0x0000ffff;
406 card->spu_reg_delay = (delay & 0xffff0000) >> 16;
408 /* If dummy clock delay mode has been requested, switch to it now */
409 if (use_dummy_writes) {
410 card->use_dummy_writes = 1;
411 err = spu_set_bus_mode(card,
412 IF_SPI_BUS_MODE_SPI_CLOCK_PHASE_RISING |
413 IF_SPI_BUS_MODE_DELAY_METHOD_DUMMY_CLOCK |
414 IF_SPI_BUS_MODE_16_BIT_ADDRESS_16_BIT_DATA);
419 lbs_deb_spi("Initialized SPU unit. "
420 "spu_port_delay=0x%04lx, spu_reg_delay=0x%04lx\n",
421 card->spu_port_delay, card->spu_reg_delay);
429 static int if_spi_prog_helper_firmware(struct if_spi_card *card)
432 const struct firmware *firmware = NULL;
435 u8 temp[HELPER_FW_LOAD_CHUNK_SZ];
436 struct spi_device *spi = card->spi;
438 lbs_deb_enter(LBS_DEB_SPI);
440 err = spu_set_interrupt_mode(card, 1, 0);
443 /* Get helper firmware image */
444 err = request_firmware(&firmware, card->helper_fw_name, &spi->dev);
446 lbs_pr_err("request_firmware failed with err = %d\n", err);
449 bytes_remaining = firmware->size;
452 /* Load helper firmware image */
453 while (bytes_remaining > 0) {
454 /* Scratch pad 1 should contain the number of bytes we
455 * want to download to the firmware */
456 err = spu_write_u16(card, IF_SPI_SCRATCH_1_REG,
457 HELPER_FW_LOAD_CHUNK_SZ);
459 goto release_firmware;
461 err = spu_wait_for_u16(card, IF_SPI_HOST_INT_STATUS_REG,
462 IF_SPI_HIST_CMD_DOWNLOAD_RDY,
463 IF_SPI_HIST_CMD_DOWNLOAD_RDY);
465 goto release_firmware;
467 /* Feed the data into the command read/write port reg
468 * in chunks of 64 bytes */
469 memset(temp, 0, sizeof(temp));
471 min(bytes_remaining, HELPER_FW_LOAD_CHUNK_SZ));
473 err = spu_write(card, IF_SPI_CMD_RDWRPORT_REG,
474 temp, HELPER_FW_LOAD_CHUNK_SZ);
476 goto release_firmware;
478 /* Interrupt the boot code */
479 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
481 goto release_firmware;
482 err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG,
483 IF_SPI_CIC_CMD_DOWNLOAD_OVER);
485 goto release_firmware;
486 bytes_remaining -= HELPER_FW_LOAD_CHUNK_SZ;
487 fw += HELPER_FW_LOAD_CHUNK_SZ;
490 /* Once the helper / single stage firmware download is complete,
491 * write 0 to scratch pad 1 and interrupt the
492 * bootloader. This completes the helper download. */
493 err = spu_write_u16(card, IF_SPI_SCRATCH_1_REG, FIRMWARE_DNLD_OK);
495 goto release_firmware;
496 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
498 goto release_firmware;
499 err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG,
500 IF_SPI_CIC_CMD_DOWNLOAD_OVER);
501 goto release_firmware;
503 lbs_deb_spi("waiting for helper to boot...\n");
506 release_firmware(firmware);
509 lbs_pr_err("failed to load helper firmware (err=%d)\n", err);
510 lbs_deb_leave_args(LBS_DEB_SPI, "err %d", err);
514 /* Returns the length of the next packet the firmware expects us to send
515 * Sets crc_err if the previous transfer had a CRC error. */
516 static int if_spi_prog_main_firmware_check_len(struct if_spi_card *card,
522 /* wait until the host interrupt status register indicates
523 * that we are ready to download */
524 err = spu_wait_for_u16(card, IF_SPI_HOST_INT_STATUS_REG,
525 IF_SPI_HIST_CMD_DOWNLOAD_RDY,
526 IF_SPI_HIST_CMD_DOWNLOAD_RDY);
528 lbs_pr_err("timed out waiting for host_int_status\n");
532 /* Ask the device how many bytes of firmware it wants. */
533 err = spu_read_u16(card, IF_SPI_SCRATCH_1_REG, &len);
537 if (len > IF_SPI_CMD_BUF_SIZE) {
538 lbs_pr_err("firmware load device requested a larger "
539 "tranfer than we are prepared to "
540 "handle. (len = %d)\n", len);
544 lbs_deb_spi("%s: crc error\n", __func__);
553 static int if_spi_prog_main_firmware(struct if_spi_card *card)
556 int bytes, crc_err = 0, err = 0;
557 const struct firmware *firmware = NULL;
559 struct spi_device *spi = card->spi;
562 lbs_deb_enter(LBS_DEB_SPI);
564 err = spu_set_interrupt_mode(card, 1, 0);
568 /* Get firmware image */
569 err = request_firmware(&firmware, card->main_fw_name, &spi->dev);
571 lbs_pr_err("%s: can't get firmware '%s' from kernel. "
572 "err = %d\n", __func__, card->main_fw_name, err);
576 err = spu_wait_for_u16(card, IF_SPI_SCRATCH_1_REG, 0, 0);
578 lbs_pr_err("%s: timed out waiting for initial "
579 "scratch reg = 0\n", __func__);
580 goto release_firmware;
585 bytes = firmware->size;
587 while ((len = if_spi_prog_main_firmware_check_len(card, &crc_err))) {
590 goto release_firmware;
593 /* If there are no more bytes left, we would normally
594 * expect to have terminated with len = 0 */
595 lbs_pr_err("Firmware load wants more bytes "
596 "than we have to offer.\n");
600 /* Previous transfer failed. */
601 if (++num_crc_errs > MAX_MAIN_FW_LOAD_CRC_ERR) {
602 lbs_pr_err("Too many CRC errors encountered "
603 "in firmware load.\n");
605 goto release_firmware;
608 /* Previous transfer succeeded. Advance counters. */
613 memset(card->cmd_buffer, 0, len);
614 memcpy(card->cmd_buffer, fw, bytes);
616 memcpy(card->cmd_buffer, fw, len);
618 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
620 goto release_firmware;
621 err = spu_write(card, IF_SPI_CMD_RDWRPORT_REG,
622 card->cmd_buffer, len);
624 goto release_firmware;
625 err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG ,
626 IF_SPI_CIC_CMD_DOWNLOAD_OVER);
628 goto release_firmware;
631 if (bytes > prev_len) {
632 lbs_pr_err("firmware load wants fewer bytes than "
633 "we have to offer.\n");
636 /* Confirm firmware download */
637 err = spu_wait_for_u32(card, IF_SPI_SCRATCH_4_REG,
638 SUCCESSFUL_FW_DOWNLOAD_MAGIC);
640 lbs_pr_err("failed to confirm the firmware download\n");
641 goto release_firmware;
645 release_firmware(firmware);
649 lbs_pr_err("failed to load firmware (err=%d)\n", err);
650 lbs_deb_leave_args(LBS_DEB_SPI, "err %d", err);
655 * SPI Transfer Thread
657 * The SPI thread handles all SPI transfers, so there is no need for a lock.
660 /* Move a command from the card to the host */
661 static int if_spi_c2h_cmd(struct if_spi_card *card)
663 struct lbs_private *priv = card->priv;
669 /* We need a buffer big enough to handle whatever people send to
671 BUILD_BUG_ON(IF_SPI_CMD_BUF_SIZE < LBS_CMD_BUFFER_SIZE);
672 BUILD_BUG_ON(IF_SPI_CMD_BUF_SIZE < LBS_UPLD_SIZE);
674 /* It's just annoying if the buffer size isn't a multiple of 4, because
675 * then we might have len < IF_SPI_CMD_BUF_SIZE but
676 * ALIGN(len, 4) > IF_SPI_CMD_BUF_SIZE */
677 BUILD_BUG_ON(IF_SPI_CMD_BUF_SIZE % 4 != 0);
679 lbs_deb_enter(LBS_DEB_SPI);
681 /* How many bytes are there to read? */
682 err = spu_read_u16(card, IF_SPI_SCRATCH_2_REG, &len);
686 lbs_pr_err("%s: error: card has no data for host\n",
690 } else if (len > IF_SPI_CMD_BUF_SIZE) {
691 lbs_pr_err("%s: error: response packet too large: "
692 "%d bytes, but maximum is %d\n",
693 __func__, len, IF_SPI_CMD_BUF_SIZE);
698 /* Read the data from the WLAN module into our command buffer */
699 err = spu_read(card, IF_SPI_CMD_RDWRPORT_REG,
700 card->cmd_buffer, ALIGN(len, 4));
704 spin_lock_irqsave(&priv->driver_lock, flags);
705 i = (priv->resp_idx == 0) ? 1 : 0;
706 BUG_ON(priv->resp_len[i]);
707 priv->resp_len[i] = len;
708 memcpy(priv->resp_buf[i], card->cmd_buffer, len);
709 lbs_notify_command_response(priv, i);
710 spin_unlock_irqrestore(&priv->driver_lock, flags);
714 lbs_pr_err("%s: err=%d\n", __func__, err);
715 lbs_deb_leave(LBS_DEB_SPI);
719 /* Move data from the card to the host */
720 static int if_spi_c2h_data(struct if_spi_card *card)
727 lbs_deb_enter(LBS_DEB_SPI);
729 /* How many bytes are there to read? */
730 err = spu_read_u16(card, IF_SPI_SCRATCH_1_REG, &len);
734 lbs_pr_err("%s: error: card has no data for host\n",
738 } else if (len > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
739 lbs_pr_err("%s: error: card has %d bytes of data, but "
740 "our maximum skb size is %lu\n",
741 __func__, len, MRVDRV_ETH_RX_PACKET_BUFFER_SIZE);
746 /* TODO: should we allocate a smaller skb if we have less data? */
747 skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE);
752 skb_reserve(skb, IPFIELD_ALIGN_OFFSET);
753 data = skb_put(skb, len);
755 /* Read the data from the WLAN module into our skb... */
756 err = spu_read(card, IF_SPI_DATA_RDWRPORT_REG, data, ALIGN(len, 4));
760 /* pass the SKB to libertas */
761 err = lbs_process_rxed_packet(card->priv, skb);
772 lbs_pr_err("%s: err=%d\n", __func__, err);
773 lbs_deb_leave(LBS_DEB_SPI);
777 /* Move data or a command from the host to the card. */
778 static void if_spi_h2c(struct if_spi_card *card,
779 struct if_spi_packet *packet, int type)
782 u16 int_type, port_reg;
786 int_type = IF_SPI_CIC_TX_DOWNLOAD_OVER;
787 port_reg = IF_SPI_DATA_RDWRPORT_REG;
790 int_type = IF_SPI_CIC_CMD_DOWNLOAD_OVER;
791 port_reg = IF_SPI_CMD_RDWRPORT_REG;
794 lbs_pr_err("can't transfer buffer of type %d\n", type);
799 /* Write the data to the card */
800 err = spu_write(card, port_reg, packet->buffer, packet->blen);
808 lbs_pr_err("%s: error %d\n", __func__, err);
811 /* Inform the host about a card event */
812 static void if_spi_e2h(struct if_spi_card *card)
817 struct lbs_private *priv = card->priv;
819 err = spu_read_u32(card, IF_SPI_SCRATCH_3_REG, &cause);
823 /* re-enable the card event interrupt */
824 spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG,
825 ~IF_SPI_HICU_CARD_EVENT);
827 /* generate a card interrupt */
828 spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG, IF_SPI_CIC_HOST_EVENT);
830 spin_lock_irqsave(&priv->driver_lock, flags);
831 lbs_queue_event(priv, cause & 0xff);
832 spin_unlock_irqrestore(&priv->driver_lock, flags);
836 lbs_pr_err("%s: error %d\n", __func__, err);
839 static int lbs_spi_thread(void *data)
842 struct if_spi_card *card = data;
845 struct if_spi_packet *packet;
848 /* Wait to be woken up by one of two things. First, our ISR
849 * could tell us that something happened on the WLAN.
850 * Secondly, libertas could call hw_host_to_card with more
851 * data, which we might be able to send.
854 err = down_interruptible(&card->spi_ready);
855 if (!card->run_thread) {
856 up(&card->spi_thread_terminated);
859 } while (err == EINTR);
861 /* Read the host interrupt status register to see what we
863 err = spu_read_u16(card, IF_SPI_HOST_INT_STATUS_REG,
866 lbs_pr_err("I/O error\n");
870 if (hiStatus & IF_SPI_HIST_CMD_UPLOAD_RDY)
871 err = if_spi_c2h_cmd(card);
874 if (hiStatus & IF_SPI_HIST_RX_UPLOAD_RDY)
875 err = if_spi_c2h_data(card);
878 if (hiStatus & IF_SPI_HIST_CMD_DOWNLOAD_RDY) {
879 /* This means two things. First of all,
880 * if there was a previous command sent, the card has
881 * successfully received it.
882 * Secondly, it is now ready to download another
885 lbs_host_to_card_done(card->priv);
887 /* Do we have any command packets from the host to
890 spin_lock_irqsave(&card->buffer_lock, flags);
891 if (!list_empty(&card->cmd_packet_list)) {
892 packet = (struct if_spi_packet *)(card->
893 cmd_packet_list.next);
894 list_del(&packet->list);
896 spin_unlock_irqrestore(&card->buffer_lock, flags);
899 if_spi_h2c(card, packet, MVMS_CMD);
901 if (hiStatus & IF_SPI_HIST_TX_DOWNLOAD_RDY) {
902 /* Do we have any data packets from the host to
905 spin_lock_irqsave(&card->buffer_lock, flags);
906 if (!list_empty(&card->data_packet_list)) {
907 packet = (struct if_spi_packet *)(card->
908 data_packet_list.next);
909 list_del(&packet->list);
911 spin_unlock_irqrestore(&card->buffer_lock, flags);
914 if_spi_h2c(card, packet, MVMS_DAT);
916 if (hiStatus & IF_SPI_HIST_CARD_EVENT)
921 lbs_pr_err("%s: got error %d\n", __func__, err);
925 /* Block until lbs_spi_thread thread has terminated */
926 static void if_spi_terminate_spi_thread(struct if_spi_card *card)
928 /* It would be nice to use kthread_stop here, but that function
929 * can't wake threads waiting for a semaphore. */
930 card->run_thread = 0;
931 up(&card->spi_ready);
932 down(&card->spi_thread_terminated);
938 * Called from Libertas to transfer some data to the WLAN device
939 * We can't sleep here. */
940 static int if_spi_host_to_card(struct lbs_private *priv,
941 u8 type, u8 *buf, u16 nb)
945 struct if_spi_card *card = priv->card;
946 struct if_spi_packet *packet;
949 lbs_deb_enter_args(LBS_DEB_SPI, "type %d, bytes %d", type, nb);
952 lbs_pr_err("%s: invalid size requested: %d\n", __func__, nb);
957 packet = kzalloc(sizeof(struct if_spi_packet) + blen, GFP_ATOMIC);
963 memcpy(packet->buffer, buf, nb);
964 memset(packet->buffer + nb, 0, blen - nb);
968 priv->dnld_sent = DNLD_CMD_SENT;
969 spin_lock_irqsave(&card->buffer_lock, flags);
970 list_add_tail(&packet->list, &card->cmd_packet_list);
971 spin_unlock_irqrestore(&card->buffer_lock, flags);
974 priv->dnld_sent = DNLD_DATA_SENT;
975 spin_lock_irqsave(&card->buffer_lock, flags);
976 list_add_tail(&packet->list, &card->data_packet_list);
977 spin_unlock_irqrestore(&card->buffer_lock, flags);
980 lbs_pr_err("can't transfer buffer of type %d", type);
985 /* Wake up the spi thread */
986 up(&card->spi_ready);
988 lbs_deb_leave_args(LBS_DEB_SPI, "err=%d", err);
995 * Service incoming interrupts from the WLAN device. We can't sleep here, so
996 * don't try to talk on the SPI bus, just wake up the SPI thread.
998 static irqreturn_t if_spi_host_interrupt(int irq, void *dev_id)
1000 struct if_spi_card *card = dev_id;
1002 up(&card->spi_ready);
1010 static int if_spi_calculate_fw_names(u16 card_id,
1011 char *helper_fw, char *main_fw)
1014 for (i = 0; i < ARRAY_SIZE(chip_id_to_device_name); ++i) {
1015 if (card_id == chip_id_to_device_name[i].chip_id)
1018 if (i == ARRAY_SIZE(chip_id_to_device_name)) {
1019 lbs_pr_err("Unsupported chip_id: 0x%02x\n", card_id);
1020 return -EAFNOSUPPORT;
1022 snprintf(helper_fw, IF_SPI_FW_NAME_MAX, "libertas/gspi%d_hlp.bin",
1023 chip_id_to_device_name[i].name);
1024 snprintf(main_fw, IF_SPI_FW_NAME_MAX, "libertas/gspi%d.bin",
1025 chip_id_to_device_name[i].name);
1029 static int __devinit if_spi_probe(struct spi_device *spi)
1031 struct if_spi_card *card;
1032 struct lbs_private *priv = NULL;
1033 struct libertas_spi_platform_data *pdata = spi->dev.platform_data;
1036 struct sched_param param = { .sched_priority = 1 };
1038 lbs_deb_enter(LBS_DEB_SPI);
1046 err = pdata->setup(spi);
1051 /* Allocate card structure to represent this specific device */
1052 card = kzalloc(sizeof(struct if_spi_card), GFP_KERNEL);
1057 spi_set_drvdata(spi, card);
1058 card->pdata = pdata;
1060 card->prev_xfer_time = jiffies;
1062 sema_init(&card->spi_ready, 0);
1063 sema_init(&card->spi_thread_terminated, 0);
1064 INIT_LIST_HEAD(&card->cmd_packet_list);
1065 INIT_LIST_HEAD(&card->data_packet_list);
1066 spin_lock_init(&card->buffer_lock);
1068 /* Initialize the SPI Interface Unit */
1069 err = spu_init(card, pdata->use_dummy_writes);
1072 err = spu_get_chip_revision(card, &card->card_id, &card->card_rev);
1077 err = spu_read_u32(card, IF_SPI_SCRATCH_4_REG, &scratch);
1080 if (scratch == SUCCESSFUL_FW_DOWNLOAD_MAGIC)
1081 lbs_deb_spi("Firmware is already loaded for "
1082 "Marvell WLAN 802.11 adapter\n");
1084 err = if_spi_calculate_fw_names(card->card_id,
1085 card->helper_fw_name, card->main_fw_name);
1089 lbs_deb_spi("Initializing FW for Marvell WLAN 802.11 adapter "
1090 "(chip_id = 0x%04x, chip_rev = 0x%02x) "
1091 "attached to SPI bus_num %d, chip_select %d. "
1092 "spi->max_speed_hz=%d\n",
1093 card->card_id, card->card_rev,
1094 spi->master->bus_num, spi->chip_select,
1096 err = if_spi_prog_helper_firmware(card);
1099 err = if_spi_prog_main_firmware(card);
1102 lbs_deb_spi("loaded FW for Marvell WLAN 802.11 adapter\n");
1105 err = spu_set_interrupt_mode(card, 0, 1);
1109 /* Register our card with libertas.
1110 * This will call alloc_etherdev */
1111 priv = lbs_add_card(card, &spi->dev);
1118 priv->hw_host_to_card = if_spi_host_to_card;
1120 priv->ps_supported = 1;
1122 /* Initialize interrupt handling stuff. */
1123 card->run_thread = 1;
1124 card->spi_thread = kthread_run(lbs_spi_thread, card, "lbs_spi_thread");
1125 if (IS_ERR(card->spi_thread)) {
1126 card->run_thread = 0;
1127 err = PTR_ERR(card->spi_thread);
1128 lbs_pr_err("error creating SPI thread: err=%d\n", err);
1131 if (sched_setscheduler(card->spi_thread, SCHED_FIFO, ¶m))
1132 lbs_pr_err("Error setting scheduler, using default.\n");
1134 err = request_irq(spi->irq, if_spi_host_interrupt,
1135 IRQF_TRIGGER_FALLING, "libertas_spi", card);
1137 lbs_pr_err("can't get host irq line-- request_irq failed\n");
1138 goto terminate_thread;
1142 * This will call register_netdev, and we'll start
1143 * getting interrupts... */
1144 err = lbs_start_card(priv);
1148 lbs_deb_spi("Finished initializing WLAN module.\n");
1150 /* successful exit */
1154 free_irq(spi->irq, card);
1156 if_spi_terminate_spi_thread(card);
1158 lbs_remove_card(priv); /* will call free_netdev */
1160 free_if_spi_card(card);
1162 lbs_deb_leave_args(LBS_DEB_SPI, "err %d\n", err);
1166 static int __devexit libertas_spi_remove(struct spi_device *spi)
1168 struct if_spi_card *card = spi_get_drvdata(spi);
1169 struct lbs_private *priv = card->priv;
1171 lbs_deb_spi("libertas_spi_remove\n");
1172 lbs_deb_enter(LBS_DEB_SPI);
1173 priv->surpriseremoved = 1;
1175 lbs_stop_card(priv);
1176 free_irq(spi->irq, card);
1177 if_spi_terminate_spi_thread(card);
1178 lbs_remove_card(priv); /* will call free_netdev */
1179 if (card->pdata->teardown)
1180 card->pdata->teardown(spi);
1181 free_if_spi_card(card);
1182 lbs_deb_leave(LBS_DEB_SPI);
1186 static struct spi_driver libertas_spi_driver = {
1187 .probe = if_spi_probe,
1188 .remove = __devexit_p(libertas_spi_remove),
1190 .name = "libertas_spi",
1191 .bus = &spi_bus_type,
1192 .owner = THIS_MODULE,
1200 static int __init if_spi_init_module(void)
1203 lbs_deb_enter(LBS_DEB_SPI);
1204 printk(KERN_INFO "libertas_spi: Libertas SPI driver\n");
1205 ret = spi_register_driver(&libertas_spi_driver);
1206 lbs_deb_leave(LBS_DEB_SPI);
1210 static void __exit if_spi_exit_module(void)
1212 lbs_deb_enter(LBS_DEB_SPI);
1213 spi_unregister_driver(&libertas_spi_driver);
1214 lbs_deb_leave(LBS_DEB_SPI);
1217 module_init(if_spi_init_module);
1218 module_exit(if_spi_exit_module);
1220 MODULE_DESCRIPTION("Libertas SPI WLAN Driver");
1221 MODULE_AUTHOR("Andrey Yurovsky <andrey@cozybit.com>, "
1222 "Colin McCabe <colin@cozybit.com>");
1223 MODULE_LICENSE("GPL");