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/list.h>
24 #include <linux/netdevice.h>
25 #include <linux/slab.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 /* The card ID and card revision, as reported by the hardware. */
50 /* The last time that we initiated an SPU operation */
51 unsigned long prev_xfer_time;
54 unsigned long spu_port_delay;
55 unsigned long spu_reg_delay;
57 /* Handles all SPI communication (except for FW load) */
58 struct workqueue_struct *workqueue;
59 struct work_struct packet_work;
60 struct work_struct resume_work;
62 u8 cmd_buffer[IF_SPI_CMD_BUF_SIZE];
64 /* A buffer of incoming packets from libertas core.
65 * Since we can't sleep in hw_host_to_card, we have to buffer
67 struct list_head cmd_packet_list;
68 struct list_head data_packet_list;
70 /* Protects cmd_packet_list and data_packet_list */
71 spinlock_t buffer_lock;
73 /* True is card suspended */
77 static void free_if_spi_card(struct if_spi_card *card)
79 struct list_head *cursor, *next;
80 struct if_spi_packet *packet;
82 list_for_each_safe(cursor, next, &card->cmd_packet_list) {
83 packet = container_of(cursor, struct if_spi_packet, list);
84 list_del(&packet->list);
87 list_for_each_safe(cursor, next, &card->data_packet_list) {
88 packet = container_of(cursor, struct if_spi_packet, list);
89 list_del(&packet->list);
92 spi_set_drvdata(card->spi, NULL);
96 #define MODEL_8385 0x04
97 #define MODEL_8686 0x0b
98 #define MODEL_8688 0x10
100 static const struct lbs_fw_table fw_table[] = {
101 { MODEL_8385, "libertas/gspi8385_helper.bin", "libertas/gspi8385.bin" },
102 { MODEL_8385, "libertas/gspi8385_hlp.bin", "libertas/gspi8385.bin" },
103 { MODEL_8686, "libertas/gspi8686_v9_helper.bin", "libertas/gspi8686_v9.bin" },
104 { MODEL_8686, "libertas/gspi8686_hlp.bin", "libertas/gspi8686.bin" },
105 { MODEL_8688, "libertas/gspi8688_helper.bin", "libertas/gspi8688.bin" },
108 MODULE_FIRMWARE("libertas/gspi8385_helper.bin");
109 MODULE_FIRMWARE("libertas/gspi8385_hlp.bin");
110 MODULE_FIRMWARE("libertas/gspi8385.bin");
111 MODULE_FIRMWARE("libertas/gspi8686_v9_helper.bin");
112 MODULE_FIRMWARE("libertas/gspi8686_v9.bin");
113 MODULE_FIRMWARE("libertas/gspi8686_hlp.bin");
114 MODULE_FIRMWARE("libertas/gspi8686.bin");
115 MODULE_FIRMWARE("libertas/gspi8688_helper.bin");
116 MODULE_FIRMWARE("libertas/gspi8688.bin");
120 * SPI Interface Unit Routines
122 * The SPU sits between the host and the WLAN module.
123 * All communication with the firmware is through SPU transactions.
125 * First we have to put a SPU register name on the bus. Then we can
126 * either read from or write to that register.
130 static void spu_transaction_init(struct if_spi_card *card)
132 if (!time_after(jiffies, card->prev_xfer_time + 1)) {
133 /* Unfortunately, the SPU requires a delay between successive
134 * transactions. If our last transaction was more than a jiffy
135 * ago, we have obviously already delayed enough.
136 * If not, we have to busy-wait to be on the safe side. */
141 static void spu_transaction_finish(struct if_spi_card *card)
143 card->prev_xfer_time = jiffies;
146 /* Write out a byte buffer to an SPI register,
147 * using a series of 16-bit transfers. */
148 static int spu_write(struct if_spi_card *card, u16 reg, const u8 *buf, int len)
151 __le16 reg_out = cpu_to_le16(reg | IF_SPI_WRITE_OPERATION_MASK);
152 struct spi_message m;
153 struct spi_transfer reg_trans;
154 struct spi_transfer data_trans;
156 spi_message_init(&m);
157 memset(®_trans, 0, sizeof(reg_trans));
158 memset(&data_trans, 0, sizeof(data_trans));
160 /* You must give an even number of bytes to the SPU, even if it
161 * doesn't care about the last one. */
164 spu_transaction_init(card);
166 /* write SPU register index */
167 reg_trans.tx_buf = ®_out;
168 reg_trans.len = sizeof(reg_out);
170 data_trans.tx_buf = buf;
171 data_trans.len = len;
173 spi_message_add_tail(®_trans, &m);
174 spi_message_add_tail(&data_trans, &m);
176 err = spi_sync(card->spi, &m);
177 spu_transaction_finish(card);
181 static inline int spu_write_u16(struct if_spi_card *card, u16 reg, u16 val)
185 buff = cpu_to_le16(val);
186 return spu_write(card, reg, (u8 *)&buff, sizeof(u16));
189 static inline int spu_reg_is_port_reg(u16 reg)
192 case IF_SPI_IO_RDWRPORT_REG:
193 case IF_SPI_CMD_RDWRPORT_REG:
194 case IF_SPI_DATA_RDWRPORT_REG:
201 static int spu_read(struct if_spi_card *card, u16 reg, u8 *buf, int len)
205 __le16 reg_out = cpu_to_le16(reg | IF_SPI_READ_OPERATION_MASK);
206 struct spi_message m;
207 struct spi_transfer reg_trans;
208 struct spi_transfer dummy_trans;
209 struct spi_transfer data_trans;
211 /* You must take an even number of bytes from the SPU, even if you
212 * don't care about the last one. */
215 spu_transaction_init(card);
217 spi_message_init(&m);
218 memset(®_trans, 0, sizeof(reg_trans));
219 memset(&dummy_trans, 0, sizeof(dummy_trans));
220 memset(&data_trans, 0, sizeof(data_trans));
222 /* write SPU register index */
223 reg_trans.tx_buf = ®_out;
224 reg_trans.len = sizeof(reg_out);
225 spi_message_add_tail(®_trans, &m);
227 delay = spu_reg_is_port_reg(reg) ? card->spu_port_delay :
229 if (card->use_dummy_writes) {
230 /* Clock in dummy cycles while the SPU fills the FIFO */
231 dummy_trans.len = delay / 8;
232 spi_message_add_tail(&dummy_trans, &m);
234 /* Busy-wait while the SPU fills the FIFO */
235 reg_trans.delay_usecs =
236 DIV_ROUND_UP((100 + (delay * 10)), 1000);
240 data_trans.rx_buf = buf;
241 data_trans.len = len;
242 spi_message_add_tail(&data_trans, &m);
244 err = spi_sync(card->spi, &m);
245 spu_transaction_finish(card);
249 /* Read 16 bits from an SPI register */
250 static inline int spu_read_u16(struct if_spi_card *card, u16 reg, u16 *val)
255 ret = spu_read(card, reg, (u8 *)&buf, sizeof(buf));
257 *val = le16_to_cpup(&buf);
261 /* Read 32 bits from an SPI register.
262 * The low 16 bits are read first. */
263 static int spu_read_u32(struct if_spi_card *card, u16 reg, u32 *val)
268 err = spu_read(card, reg, (u8 *)&buf, sizeof(buf));
270 *val = le32_to_cpup(&buf);
274 /* Keep reading 16 bits from an SPI register until you get the correct result.
276 * If mask = 0, the correct result is any non-zero number.
277 * If mask != 0, the correct result is any number where
278 * number & target_mask == target
280 * Returns -ETIMEDOUT if a second passes without the correct result. */
281 static int spu_wait_for_u16(struct if_spi_card *card, u16 reg,
282 u16 target_mask, u16 target)
285 unsigned long timeout = jiffies + 5*HZ;
288 err = spu_read_u16(card, reg, &val);
292 if ((val & target_mask) == target)
299 if (time_after(jiffies, timeout)) {
300 lbs_pr_err("%s: timeout with val=%02x, "
301 "target_mask=%02x, target=%02x\n",
302 __func__, val, target_mask, target);
308 /* Read 16 bits from an SPI register until you receive a specific value.
309 * Returns -ETIMEDOUT if a 4 tries pass without success. */
310 static int spu_wait_for_u32(struct if_spi_card *card, u32 reg, u32 target)
313 for (try = 0; try < 4; ++try) {
315 err = spu_read_u32(card, reg, &val);
325 static int spu_set_interrupt_mode(struct if_spi_card *card,
326 int suppress_host_int,
331 /* We can suppress a host interrupt by clearing the appropriate
332 * bit in the "host interrupt status mask" register */
333 if (suppress_host_int) {
334 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG, 0);
338 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG,
339 IF_SPI_HISM_TX_DOWNLOAD_RDY |
340 IF_SPI_HISM_RX_UPLOAD_RDY |
341 IF_SPI_HISM_CMD_DOWNLOAD_RDY |
342 IF_SPI_HISM_CARDEVENT |
343 IF_SPI_HISM_CMD_UPLOAD_RDY);
348 /* If auto-interrupts are on, the completion of certain transactions
349 * will trigger an interrupt automatically. If auto-interrupts
350 * are off, we need to set the "Card Interrupt Cause" register to
351 * trigger a card interrupt. */
353 err = spu_write_u16(card, IF_SPI_HOST_INT_CTRL_REG,
354 IF_SPI_HICT_TX_DOWNLOAD_OVER_AUTO |
355 IF_SPI_HICT_RX_UPLOAD_OVER_AUTO |
356 IF_SPI_HICT_CMD_DOWNLOAD_OVER_AUTO |
357 IF_SPI_HICT_CMD_UPLOAD_OVER_AUTO);
361 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG, 0);
368 static int spu_get_chip_revision(struct if_spi_card *card,
369 u16 *card_id, u8 *card_rev)
373 err = spu_read_u32(card, IF_SPI_DEVICEID_CTRL_REG, &dev_ctrl);
376 *card_id = IF_SPI_DEVICEID_CTRL_REG_TO_CARD_ID(dev_ctrl);
377 *card_rev = IF_SPI_DEVICEID_CTRL_REG_TO_CARD_REV(dev_ctrl);
381 static int spu_set_bus_mode(struct if_spi_card *card, u16 mode)
386 err = spu_write_u16(card, IF_SPI_SPU_BUS_MODE_REG, mode);
389 /* Check that we were able to read back what we just wrote. */
390 err = spu_read_u16(card, IF_SPI_SPU_BUS_MODE_REG, &rval);
393 if ((rval & 0xF) != mode) {
394 lbs_pr_err("Can't read bus mode register.\n");
400 static int spu_init(struct if_spi_card *card, int use_dummy_writes)
405 /* We have to start up in timed delay mode so that we can safely
406 * read the Delay Read Register. */
407 card->use_dummy_writes = 0;
408 err = spu_set_bus_mode(card,
409 IF_SPI_BUS_MODE_SPI_CLOCK_PHASE_RISING |
410 IF_SPI_BUS_MODE_DELAY_METHOD_TIMED |
411 IF_SPI_BUS_MODE_16_BIT_ADDRESS_16_BIT_DATA);
414 card->spu_port_delay = 1000;
415 card->spu_reg_delay = 1000;
416 err = spu_read_u32(card, IF_SPI_DELAY_READ_REG, &delay);
419 card->spu_port_delay = delay & 0x0000ffff;
420 card->spu_reg_delay = (delay & 0xffff0000) >> 16;
422 /* If dummy clock delay mode has been requested, switch to it now */
423 if (use_dummy_writes) {
424 card->use_dummy_writes = 1;
425 err = spu_set_bus_mode(card,
426 IF_SPI_BUS_MODE_SPI_CLOCK_PHASE_RISING |
427 IF_SPI_BUS_MODE_DELAY_METHOD_DUMMY_CLOCK |
428 IF_SPI_BUS_MODE_16_BIT_ADDRESS_16_BIT_DATA);
433 lbs_deb_spi("Initialized SPU unit. "
434 "spu_port_delay=0x%04lx, spu_reg_delay=0x%04lx\n",
435 card->spu_port_delay, card->spu_reg_delay);
443 static int if_spi_prog_helper_firmware(struct if_spi_card *card,
444 const struct firmware *firmware)
449 u8 temp[HELPER_FW_LOAD_CHUNK_SZ];
451 lbs_deb_enter(LBS_DEB_SPI);
453 err = spu_set_interrupt_mode(card, 1, 0);
457 bytes_remaining = firmware->size;
460 /* Load helper firmware image */
461 while (bytes_remaining > 0) {
462 /* Scratch pad 1 should contain the number of bytes we
463 * want to download to the firmware */
464 err = spu_write_u16(card, IF_SPI_SCRATCH_1_REG,
465 HELPER_FW_LOAD_CHUNK_SZ);
469 err = spu_wait_for_u16(card, IF_SPI_HOST_INT_STATUS_REG,
470 IF_SPI_HIST_CMD_DOWNLOAD_RDY,
471 IF_SPI_HIST_CMD_DOWNLOAD_RDY);
475 /* Feed the data into the command read/write port reg
476 * in chunks of 64 bytes */
477 memset(temp, 0, sizeof(temp));
479 min(bytes_remaining, HELPER_FW_LOAD_CHUNK_SZ));
481 err = spu_write(card, IF_SPI_CMD_RDWRPORT_REG,
482 temp, HELPER_FW_LOAD_CHUNK_SZ);
486 /* Interrupt the boot code */
487 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
490 err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG,
491 IF_SPI_CIC_CMD_DOWNLOAD_OVER);
494 bytes_remaining -= HELPER_FW_LOAD_CHUNK_SZ;
495 fw += HELPER_FW_LOAD_CHUNK_SZ;
498 /* Once the helper / single stage firmware download is complete,
499 * write 0 to scratch pad 1 and interrupt the
500 * bootloader. This completes the helper download. */
501 err = spu_write_u16(card, IF_SPI_SCRATCH_1_REG, FIRMWARE_DNLD_OK);
504 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
507 err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG,
508 IF_SPI_CIC_CMD_DOWNLOAD_OVER);
511 lbs_deb_spi("waiting for helper to boot...\n");
515 lbs_pr_err("failed to load helper firmware (err=%d)\n", err);
516 lbs_deb_leave_args(LBS_DEB_SPI, "err %d", err);
520 /* Returns the length of the next packet the firmware expects us to send
521 * Sets crc_err if the previous transfer had a CRC error. */
522 static int if_spi_prog_main_firmware_check_len(struct if_spi_card *card,
528 /* wait until the host interrupt status register indicates
529 * that we are ready to download */
530 err = spu_wait_for_u16(card, IF_SPI_HOST_INT_STATUS_REG,
531 IF_SPI_HIST_CMD_DOWNLOAD_RDY,
532 IF_SPI_HIST_CMD_DOWNLOAD_RDY);
534 lbs_pr_err("timed out waiting for host_int_status\n");
538 /* Ask the device how many bytes of firmware it wants. */
539 err = spu_read_u16(card, IF_SPI_SCRATCH_1_REG, &len);
543 if (len > IF_SPI_CMD_BUF_SIZE) {
544 lbs_pr_err("firmware load device requested a larger "
545 "tranfer than we are prepared to "
546 "handle. (len = %d)\n", len);
550 lbs_deb_spi("%s: crc error\n", __func__);
559 static int if_spi_prog_main_firmware(struct if_spi_card *card,
560 const struct firmware *firmware)
563 int bytes, crc_err = 0, err = 0;
567 lbs_deb_enter(LBS_DEB_SPI);
569 err = spu_set_interrupt_mode(card, 1, 0);
573 err = spu_wait_for_u16(card, IF_SPI_SCRATCH_1_REG, 0, 0);
575 lbs_pr_err("%s: timed out waiting for initial "
576 "scratch reg = 0\n", __func__);
582 bytes = firmware->size;
584 while ((len = if_spi_prog_main_firmware_check_len(card, &crc_err))) {
590 /* If there are no more bytes left, we would normally
591 * expect to have terminated with len = 0 */
592 lbs_pr_err("Firmware load wants more bytes "
593 "than we have to offer.\n");
597 /* Previous transfer failed. */
598 if (++num_crc_errs > MAX_MAIN_FW_LOAD_CRC_ERR) {
599 lbs_pr_err("Too many CRC errors encountered "
600 "in firmware load.\n");
605 /* Previous transfer succeeded. Advance counters. */
610 memset(card->cmd_buffer, 0, len);
611 memcpy(card->cmd_buffer, fw, bytes);
613 memcpy(card->cmd_buffer, fw, len);
615 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
618 err = spu_write(card, IF_SPI_CMD_RDWRPORT_REG,
619 card->cmd_buffer, len);
622 err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG ,
623 IF_SPI_CIC_CMD_DOWNLOAD_OVER);
628 if (bytes > prev_len) {
629 lbs_pr_err("firmware load wants fewer bytes than "
630 "we have to offer.\n");
633 /* Confirm firmware download */
634 err = spu_wait_for_u32(card, IF_SPI_SCRATCH_4_REG,
635 SUCCESSFUL_FW_DOWNLOAD_MAGIC);
637 lbs_pr_err("failed to confirm the firmware download\n");
643 lbs_pr_err("failed to load firmware (err=%d)\n", err);
644 lbs_deb_leave_args(LBS_DEB_SPI, "err %d", err);
649 * SPI Transfer Thread
651 * The SPI worker handles all SPI transfers, so there is no need for a lock.
654 /* Move a command from the card to the host */
655 static int if_spi_c2h_cmd(struct if_spi_card *card)
657 struct lbs_private *priv = card->priv;
663 /* We need a buffer big enough to handle whatever people send to
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);
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);
673 lbs_deb_enter(LBS_DEB_SPI);
675 /* How many bytes are there to read? */
676 err = spu_read_u16(card, IF_SPI_SCRATCH_2_REG, &len);
680 lbs_pr_err("%s: error: card has no data for host\n",
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);
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));
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);
708 lbs_pr_err("%s: err=%d\n", __func__, err);
709 lbs_deb_leave(LBS_DEB_SPI);
713 /* Move data from the card to the host */
714 static int if_spi_c2h_data(struct if_spi_card *card)
721 lbs_deb_enter(LBS_DEB_SPI);
723 /* How many bytes are there to read? */
724 err = spu_read_u16(card, IF_SPI_SCRATCH_1_REG, &len);
728 lbs_pr_err("%s: error: card has no data for host\n",
732 } else if (len > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
733 lbs_pr_err("%s: error: card has %d bytes of data, but "
734 "our maximum skb size is %zu\n",
735 __func__, len, MRVDRV_ETH_RX_PACKET_BUFFER_SIZE);
740 /* TODO: should we allocate a smaller skb if we have less data? */
741 skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE);
746 skb_reserve(skb, IPFIELD_ALIGN_OFFSET);
747 data = skb_put(skb, len);
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));
754 /* pass the SKB to libertas */
755 err = lbs_process_rxed_packet(card->priv, skb);
766 lbs_pr_err("%s: err=%d\n", __func__, err);
767 lbs_deb_leave(LBS_DEB_SPI);
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)
776 u16 int_type, port_reg;
780 int_type = IF_SPI_CIC_TX_DOWNLOAD_OVER;
781 port_reg = IF_SPI_DATA_RDWRPORT_REG;
784 int_type = IF_SPI_CIC_CMD_DOWNLOAD_OVER;
785 port_reg = IF_SPI_CMD_RDWRPORT_REG;
788 lbs_pr_err("can't transfer buffer of type %d\n", type);
793 /* Write the data to the card */
794 err = spu_write(card, port_reg, packet->buffer, packet->blen);
802 lbs_pr_err("%s: error %d\n", __func__, err);
805 /* Inform the host about a card event */
806 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 /* re-enable the card event interrupt */
817 spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG,
818 ~IF_SPI_HICU_CARD_EVENT);
820 /* generate a card interrupt */
821 spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG, IF_SPI_CIC_HOST_EVENT);
823 lbs_queue_event(priv, cause & 0xff);
826 lbs_pr_err("%s: error %d\n", __func__, err);
829 static void if_spi_host_to_card_worker(struct work_struct *work)
832 struct if_spi_card *card;
835 struct if_spi_packet *packet;
837 card = container_of(work, struct if_spi_card, packet_work);
839 lbs_deb_enter(LBS_DEB_SPI);
841 /* Read the host interrupt status register to see what we
843 err = spu_read_u16(card, IF_SPI_HOST_INT_STATUS_REG,
846 lbs_pr_err("I/O error\n");
850 if (hiStatus & IF_SPI_HIST_CMD_UPLOAD_RDY) {
851 err = if_spi_c2h_cmd(card);
855 if (hiStatus & IF_SPI_HIST_RX_UPLOAD_RDY) {
856 err = if_spi_c2h_data(card);
861 /* workaround: in PS mode, the card does not set the Command
862 * Download Ready bit, but it sets TX Download Ready. */
863 if (hiStatus & IF_SPI_HIST_CMD_DOWNLOAD_RDY ||
864 (card->priv->psstate != PS_STATE_FULL_POWER &&
865 (hiStatus & IF_SPI_HIST_TX_DOWNLOAD_RDY))) {
866 /* This means two things. First of all,
867 * if there was a previous command sent, the card has
868 * successfully received it.
869 * Secondly, it is now ready to download another
872 lbs_host_to_card_done(card->priv);
874 /* Do we have any command packets from the host to
877 spin_lock_irqsave(&card->buffer_lock, flags);
878 if (!list_empty(&card->cmd_packet_list)) {
879 packet = (struct if_spi_packet *)(card->
880 cmd_packet_list.next);
881 list_del(&packet->list);
883 spin_unlock_irqrestore(&card->buffer_lock, flags);
886 if_spi_h2c(card, packet, MVMS_CMD);
888 if (hiStatus & IF_SPI_HIST_TX_DOWNLOAD_RDY) {
889 /* Do we have any data packets from the host to
892 spin_lock_irqsave(&card->buffer_lock, flags);
893 if (!list_empty(&card->data_packet_list)) {
894 packet = (struct if_spi_packet *)(card->
895 data_packet_list.next);
896 list_del(&packet->list);
898 spin_unlock_irqrestore(&card->buffer_lock, flags);
901 if_spi_h2c(card, packet, MVMS_DAT);
903 if (hiStatus & IF_SPI_HIST_CARD_EVENT)
908 lbs_pr_err("%s: got error %d\n", __func__, err);
910 lbs_deb_leave(LBS_DEB_SPI);
916 * Called from Libertas to transfer some data to the WLAN device
917 * We can't sleep here. */
918 static int if_spi_host_to_card(struct lbs_private *priv,
919 u8 type, u8 *buf, u16 nb)
923 struct if_spi_card *card = priv->card;
924 struct if_spi_packet *packet;
927 lbs_deb_enter_args(LBS_DEB_SPI, "type %d, bytes %d", type, nb);
930 lbs_pr_err("%s: invalid size requested: %d\n", __func__, nb);
935 packet = kzalloc(sizeof(struct if_spi_packet) + blen, GFP_ATOMIC);
941 memcpy(packet->buffer, buf, nb);
942 memset(packet->buffer + nb, 0, blen - nb);
946 priv->dnld_sent = DNLD_CMD_SENT;
947 spin_lock_irqsave(&card->buffer_lock, flags);
948 list_add_tail(&packet->list, &card->cmd_packet_list);
949 spin_unlock_irqrestore(&card->buffer_lock, flags);
952 priv->dnld_sent = DNLD_DATA_SENT;
953 spin_lock_irqsave(&card->buffer_lock, flags);
954 list_add_tail(&packet->list, &card->data_packet_list);
955 spin_unlock_irqrestore(&card->buffer_lock, flags);
958 lbs_pr_err("can't transfer buffer of type %d", type);
963 /* Queue spi xfer work */
964 queue_work(card->workqueue, &card->packet_work);
966 lbs_deb_leave_args(LBS_DEB_SPI, "err=%d", err);
973 * Service incoming interrupts from the WLAN device. We can't sleep here, so
974 * don't try to talk on the SPI bus, just queue the SPI xfer work.
976 static irqreturn_t if_spi_host_interrupt(int irq, void *dev_id)
978 struct if_spi_card *card = dev_id;
980 queue_work(card->workqueue, &card->packet_work);
989 static int if_spi_init_card(struct if_spi_card *card)
991 struct spi_device *spi = card->spi;
994 const struct firmware *helper = NULL;
995 const struct firmware *mainfw = NULL;
997 lbs_deb_enter(LBS_DEB_SPI);
999 err = spu_init(card, card->pdata->use_dummy_writes);
1002 err = spu_get_chip_revision(card, &card->card_id, &card->card_rev);
1006 err = spu_read_u32(card, IF_SPI_SCRATCH_4_REG, &scratch);
1009 if (scratch == SUCCESSFUL_FW_DOWNLOAD_MAGIC)
1010 lbs_deb_spi("Firmware is already loaded for "
1011 "Marvell WLAN 802.11 adapter\n");
1013 /* Check if we support this card */
1014 for (i = 0; i < ARRAY_SIZE(fw_table); i++) {
1015 if (card->card_id == fw_table[i].model)
1018 if (i == ARRAY_SIZE(fw_table)) {
1019 lbs_pr_err("Unsupported chip_id: 0x%02x\n",
1025 err = lbs_get_firmware(&card->spi->dev, NULL, NULL,
1026 card->card_id, &fw_table[0], &helper,
1029 lbs_pr_err("failed to find firmware (%d)\n", err);
1033 lbs_deb_spi("Initializing FW for Marvell WLAN 802.11 adapter "
1034 "(chip_id = 0x%04x, chip_rev = 0x%02x) "
1035 "attached to SPI bus_num %d, chip_select %d. "
1036 "spi->max_speed_hz=%d\n",
1037 card->card_id, card->card_rev,
1038 spi->master->bus_num, spi->chip_select,
1040 err = if_spi_prog_helper_firmware(card, helper);
1043 err = if_spi_prog_main_firmware(card, mainfw);
1046 lbs_deb_spi("loaded FW for Marvell WLAN 802.11 adapter\n");
1049 err = spu_set_interrupt_mode(card, 0, 1);
1055 release_firmware(helper);
1057 release_firmware(mainfw);
1059 lbs_deb_leave_args(LBS_DEB_SPI, "err %d\n", err);
1064 static void if_spi_resume_worker(struct work_struct *work)
1066 struct if_spi_card *card;
1068 card = container_of(work, struct if_spi_card, resume_work);
1070 if (card->suspended) {
1071 if (card->pdata->setup)
1072 card->pdata->setup(card->spi);
1075 if_spi_init_card(card);
1077 enable_irq(card->spi->irq);
1079 /* And resume it ... */
1080 lbs_resume(card->priv);
1082 card->suspended = 0;
1086 static int __devinit if_spi_probe(struct spi_device *spi)
1088 struct if_spi_card *card;
1089 struct lbs_private *priv = NULL;
1090 struct libertas_spi_platform_data *pdata = spi->dev.platform_data;
1093 lbs_deb_enter(LBS_DEB_SPI);
1101 err = pdata->setup(spi);
1106 /* Allocate card structure to represent this specific device */
1107 card = kzalloc(sizeof(struct if_spi_card), GFP_KERNEL);
1112 spi_set_drvdata(spi, card);
1113 card->pdata = pdata;
1115 card->prev_xfer_time = jiffies;
1117 INIT_LIST_HEAD(&card->cmd_packet_list);
1118 INIT_LIST_HEAD(&card->data_packet_list);
1119 spin_lock_init(&card->buffer_lock);
1121 /* Initialize the SPI Interface Unit */
1124 err = if_spi_init_card(card);
1128 /* Register our card with libertas.
1129 * This will call alloc_etherdev */
1130 priv = lbs_add_card(card, &spi->dev);
1136 priv->setup_fw_on_resume = 1;
1138 priv->hw_host_to_card = if_spi_host_to_card;
1139 priv->enter_deep_sleep = NULL;
1140 priv->exit_deep_sleep = NULL;
1141 priv->reset_deep_sleep_wakeup = NULL;
1144 /* Initialize interrupt handling stuff. */
1145 card->workqueue = create_workqueue("libertas_spi");
1146 INIT_WORK(&card->packet_work, if_spi_host_to_card_worker);
1147 INIT_WORK(&card->resume_work, if_spi_resume_worker);
1149 err = request_irq(spi->irq, if_spi_host_interrupt,
1150 IRQF_TRIGGER_FALLING, "libertas_spi", card);
1152 lbs_pr_err("can't get host irq line-- request_irq failed\n");
1153 goto terminate_workqueue;
1157 * This will call register_netdev, and we'll start
1158 * getting interrupts... */
1159 err = lbs_start_card(priv);
1163 lbs_deb_spi("Finished initializing WLAN module.\n");
1165 /* successful exit */
1169 free_irq(spi->irq, card);
1170 terminate_workqueue:
1171 flush_workqueue(card->workqueue);
1172 destroy_workqueue(card->workqueue);
1173 lbs_remove_card(priv); /* will call free_netdev */
1175 free_if_spi_card(card);
1177 if (pdata->teardown)
1178 pdata->teardown(spi);
1180 lbs_deb_leave_args(LBS_DEB_SPI, "err %d\n", err);
1184 static int __devexit libertas_spi_remove(struct spi_device *spi)
1186 struct if_spi_card *card = spi_get_drvdata(spi);
1187 struct lbs_private *priv = card->priv;
1189 lbs_deb_spi("libertas_spi_remove\n");
1190 lbs_deb_enter(LBS_DEB_SPI);
1192 cancel_work_sync(&card->resume_work);
1194 lbs_stop_card(priv);
1195 lbs_remove_card(priv); /* will call free_netdev */
1197 free_irq(spi->irq, card);
1198 flush_workqueue(card->workqueue);
1199 destroy_workqueue(card->workqueue);
1200 if (card->pdata->teardown)
1201 card->pdata->teardown(spi);
1202 free_if_spi_card(card);
1203 lbs_deb_leave(LBS_DEB_SPI);
1207 static int if_spi_suspend(struct device *dev)
1209 struct spi_device *spi = to_spi_device(dev);
1210 struct if_spi_card *card = spi_get_drvdata(spi);
1212 if (!card->suspended) {
1213 lbs_suspend(card->priv);
1214 flush_workqueue(card->workqueue);
1215 disable_irq(spi->irq);
1217 if (card->pdata->teardown)
1218 card->pdata->teardown(spi);
1219 card->suspended = 1;
1225 static int if_spi_resume(struct device *dev)
1227 struct spi_device *spi = to_spi_device(dev);
1228 struct if_spi_card *card = spi_get_drvdata(spi);
1230 /* Schedule delayed work */
1231 schedule_work(&card->resume_work);
1236 static const struct dev_pm_ops if_spi_pm_ops = {
1237 .suspend = if_spi_suspend,
1238 .resume = if_spi_resume,
1241 static struct spi_driver libertas_spi_driver = {
1242 .probe = if_spi_probe,
1243 .remove = __devexit_p(libertas_spi_remove),
1245 .name = "libertas_spi",
1246 .bus = &spi_bus_type,
1247 .owner = THIS_MODULE,
1248 .pm = &if_spi_pm_ops,
1256 static int __init if_spi_init_module(void)
1259 lbs_deb_enter(LBS_DEB_SPI);
1260 printk(KERN_INFO "libertas_spi: Libertas SPI driver\n");
1261 ret = spi_register_driver(&libertas_spi_driver);
1262 lbs_deb_leave(LBS_DEB_SPI);
1266 static void __exit if_spi_exit_module(void)
1268 lbs_deb_enter(LBS_DEB_SPI);
1269 spi_unregister_driver(&libertas_spi_driver);
1270 lbs_deb_leave(LBS_DEB_SPI);
1273 module_init(if_spi_init_module);
1274 module_exit(if_spi_exit_module);
1276 MODULE_DESCRIPTION("Libertas SPI WLAN Driver");
1277 MODULE_AUTHOR("Andrey Yurovsky <andrey@cozybit.com>, "
1278 "Colin McCabe <colin@cozybit.com>");
1279 MODULE_LICENSE("GPL");
1280 MODULE_ALIAS("spi:libertas_spi");