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;
61 u8 cmd_buffer[IF_SPI_CMD_BUF_SIZE];
63 /* A buffer of incoming packets from libertas core.
64 * Since we can't sleep in hw_host_to_card, we have to buffer
66 struct list_head cmd_packet_list;
67 struct list_head data_packet_list;
69 /* Protects cmd_packet_list and data_packet_list */
70 spinlock_t buffer_lock;
73 static void free_if_spi_card(struct if_spi_card *card)
75 struct list_head *cursor, *next;
76 struct if_spi_packet *packet;
78 list_for_each_safe(cursor, next, &card->cmd_packet_list) {
79 packet = container_of(cursor, struct if_spi_packet, list);
80 list_del(&packet->list);
83 list_for_each_safe(cursor, next, &card->data_packet_list) {
84 packet = container_of(cursor, struct if_spi_packet, list);
85 list_del(&packet->list);
88 spi_set_drvdata(card->spi, NULL);
92 #define MODEL_8385 0x04
93 #define MODEL_8686 0x0b
94 #define MODEL_8688 0x10
96 static const struct lbs_fw_table fw_table[] = {
97 { MODEL_8385, "libertas/gspi8385_helper.bin", "libertas/gspi8385.bin" },
98 { MODEL_8385, "libertas/gspi8385_hlp.bin", "libertas/gspi8385.bin" },
99 { MODEL_8686, "libertas/gspi8686_v9_helper.bin", "libertas/gspi8686_v9.bin" },
100 { MODEL_8686, "libertas/gspi8686_hlp.bin", "libertas/gspi8686.bin" },
101 { MODEL_8688, "libertas/gspi8688_helper.bin", "libertas/gspi8688.bin" },
104 MODULE_FIRMWARE("libertas/gspi8385_helper.bin");
105 MODULE_FIRMWARE("libertas/gspi8385_hlp.bin");
106 MODULE_FIRMWARE("libertas/gspi8385.bin");
107 MODULE_FIRMWARE("libertas/gspi8686_v9_helper.bin");
108 MODULE_FIRMWARE("libertas/gspi8686_v9.bin");
109 MODULE_FIRMWARE("libertas/gspi8686_hlp.bin");
110 MODULE_FIRMWARE("libertas/gspi8686.bin");
111 MODULE_FIRMWARE("libertas/gspi8688_helper.bin");
112 MODULE_FIRMWARE("libertas/gspi8688.bin");
116 * SPI Interface Unit Routines
118 * The SPU sits between the host and the WLAN module.
119 * All communication with the firmware is through SPU transactions.
121 * First we have to put a SPU register name on the bus. Then we can
122 * either read from or write to that register.
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. */
137 static void spu_transaction_finish(struct if_spi_card *card)
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 __le16 reg_out = cpu_to_le16(reg | IF_SPI_WRITE_OPERATION_MASK);
148 struct spi_message m;
149 struct spi_transfer reg_trans;
150 struct spi_transfer data_trans;
152 spi_message_init(&m);
153 memset(®_trans, 0, sizeof(reg_trans));
154 memset(&data_trans, 0, sizeof(data_trans));
156 /* You must give an even number of bytes to the SPU, even if it
157 * doesn't care about the last one. */
160 spu_transaction_init(card);
162 /* write SPU register index */
163 reg_trans.tx_buf = ®_out;
164 reg_trans.len = sizeof(reg_out);
166 data_trans.tx_buf = buf;
167 data_trans.len = len;
169 spi_message_add_tail(®_trans, &m);
170 spi_message_add_tail(&data_trans, &m);
172 err = spi_sync(card->spi, &m);
173 spu_transaction_finish(card);
177 static inline int spu_write_u16(struct if_spi_card *card, u16 reg, u16 val)
181 buff = cpu_to_le16(val);
182 return spu_write(card, reg, (u8 *)&buff, sizeof(u16));
185 static inline int spu_reg_is_port_reg(u16 reg)
188 case IF_SPI_IO_RDWRPORT_REG:
189 case IF_SPI_CMD_RDWRPORT_REG:
190 case IF_SPI_DATA_RDWRPORT_REG:
197 static int spu_read(struct if_spi_card *card, u16 reg, u8 *buf, int len)
201 __le16 reg_out = cpu_to_le16(reg | IF_SPI_READ_OPERATION_MASK);
202 struct spi_message m;
203 struct spi_transfer reg_trans;
204 struct spi_transfer dummy_trans;
205 struct spi_transfer data_trans;
207 /* You must take an even number of bytes from the SPU, even if you
208 * don't care about the last one. */
211 spu_transaction_init(card);
213 spi_message_init(&m);
214 memset(®_trans, 0, sizeof(reg_trans));
215 memset(&dummy_trans, 0, sizeof(dummy_trans));
216 memset(&data_trans, 0, sizeof(data_trans));
218 /* write SPU register index */
219 reg_trans.tx_buf = ®_out;
220 reg_trans.len = sizeof(reg_out);
221 spi_message_add_tail(®_trans, &m);
223 delay = spu_reg_is_port_reg(reg) ? card->spu_port_delay :
225 if (card->use_dummy_writes) {
226 /* Clock in dummy cycles while the SPU fills the FIFO */
227 dummy_trans.len = delay / 8;
228 spi_message_add_tail(&dummy_trans, &m);
230 /* Busy-wait while the SPU fills the FIFO */
231 reg_trans.delay_usecs =
232 DIV_ROUND_UP((100 + (delay * 10)), 1000);
236 data_trans.rx_buf = buf;
237 data_trans.len = len;
238 spi_message_add_tail(&data_trans, &m);
240 err = spi_sync(card->spi, &m);
241 spu_transaction_finish(card);
245 /* Read 16 bits from an SPI register */
246 static inline int spu_read_u16(struct if_spi_card *card, u16 reg, u16 *val)
251 ret = spu_read(card, reg, (u8 *)&buf, sizeof(buf));
253 *val = le16_to_cpup(&buf);
257 /* Read 32 bits from an SPI register.
258 * The low 16 bits are read first. */
259 static int spu_read_u32(struct if_spi_card *card, u16 reg, u32 *val)
264 err = spu_read(card, reg, (u8 *)&buf, sizeof(buf));
266 *val = le32_to_cpup(&buf);
270 /* Keep reading 16 bits from an SPI register until you get the correct result.
272 * If mask = 0, the correct result is any non-zero number.
273 * If mask != 0, the correct result is any number where
274 * number & target_mask == target
276 * Returns -ETIMEDOUT if a second passes without the correct result. */
277 static int spu_wait_for_u16(struct if_spi_card *card, u16 reg,
278 u16 target_mask, u16 target)
281 unsigned long timeout = jiffies + 5*HZ;
284 err = spu_read_u16(card, reg, &val);
288 if ((val & target_mask) == target)
295 if (time_after(jiffies, timeout)) {
296 lbs_pr_err("%s: timeout with val=%02x, "
297 "target_mask=%02x, target=%02x\n",
298 __func__, val, target_mask, target);
304 /* Read 16 bits from an SPI register until you receive a specific value.
305 * Returns -ETIMEDOUT if a 4 tries pass without success. */
306 static int spu_wait_for_u32(struct if_spi_card *card, u32 reg, u32 target)
309 for (try = 0; try < 4; ++try) {
311 err = spu_read_u32(card, reg, &val);
321 static int spu_set_interrupt_mode(struct if_spi_card *card,
322 int suppress_host_int,
327 /* We can suppress a host interrupt by clearing the appropriate
328 * bit in the "host interrupt status mask" register */
329 if (suppress_host_int) {
330 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG, 0);
334 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG,
335 IF_SPI_HISM_TX_DOWNLOAD_RDY |
336 IF_SPI_HISM_RX_UPLOAD_RDY |
337 IF_SPI_HISM_CMD_DOWNLOAD_RDY |
338 IF_SPI_HISM_CARDEVENT |
339 IF_SPI_HISM_CMD_UPLOAD_RDY);
344 /* If auto-interrupts are on, the completion of certain transactions
345 * will trigger an interrupt automatically. If auto-interrupts
346 * are off, we need to set the "Card Interrupt Cause" register to
347 * trigger a card interrupt. */
349 err = spu_write_u16(card, IF_SPI_HOST_INT_CTRL_REG,
350 IF_SPI_HICT_TX_DOWNLOAD_OVER_AUTO |
351 IF_SPI_HICT_RX_UPLOAD_OVER_AUTO |
352 IF_SPI_HICT_CMD_DOWNLOAD_OVER_AUTO |
353 IF_SPI_HICT_CMD_UPLOAD_OVER_AUTO);
357 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG, 0);
364 static int spu_get_chip_revision(struct if_spi_card *card,
365 u16 *card_id, u8 *card_rev)
369 err = spu_read_u32(card, IF_SPI_DEVICEID_CTRL_REG, &dev_ctrl);
372 *card_id = IF_SPI_DEVICEID_CTRL_REG_TO_CARD_ID(dev_ctrl);
373 *card_rev = IF_SPI_DEVICEID_CTRL_REG_TO_CARD_REV(dev_ctrl);
377 static int spu_set_bus_mode(struct if_spi_card *card, u16 mode)
382 err = spu_write_u16(card, IF_SPI_SPU_BUS_MODE_REG, mode);
385 /* Check that we were able to read back what we just wrote. */
386 err = spu_read_u16(card, IF_SPI_SPU_BUS_MODE_REG, &rval);
389 if ((rval & 0xF) != mode) {
390 lbs_pr_err("Can't read bus mode register.\n");
396 static int spu_init(struct if_spi_card *card, int use_dummy_writes)
401 /* We have to start up in timed delay mode so that we can safely
402 * read the Delay Read Register. */
403 card->use_dummy_writes = 0;
404 err = spu_set_bus_mode(card,
405 IF_SPI_BUS_MODE_SPI_CLOCK_PHASE_RISING |
406 IF_SPI_BUS_MODE_DELAY_METHOD_TIMED |
407 IF_SPI_BUS_MODE_16_BIT_ADDRESS_16_BIT_DATA);
410 card->spu_port_delay = 1000;
411 card->spu_reg_delay = 1000;
412 err = spu_read_u32(card, IF_SPI_DELAY_READ_REG, &delay);
415 card->spu_port_delay = delay & 0x0000ffff;
416 card->spu_reg_delay = (delay & 0xffff0000) >> 16;
418 /* If dummy clock delay mode has been requested, switch to it now */
419 if (use_dummy_writes) {
420 card->use_dummy_writes = 1;
421 err = spu_set_bus_mode(card,
422 IF_SPI_BUS_MODE_SPI_CLOCK_PHASE_RISING |
423 IF_SPI_BUS_MODE_DELAY_METHOD_DUMMY_CLOCK |
424 IF_SPI_BUS_MODE_16_BIT_ADDRESS_16_BIT_DATA);
429 lbs_deb_spi("Initialized SPU unit. "
430 "spu_port_delay=0x%04lx, spu_reg_delay=0x%04lx\n",
431 card->spu_port_delay, card->spu_reg_delay);
439 static int if_spi_prog_helper_firmware(struct if_spi_card *card,
440 const struct firmware *firmware)
445 u8 temp[HELPER_FW_LOAD_CHUNK_SZ];
447 lbs_deb_enter(LBS_DEB_SPI);
449 err = spu_set_interrupt_mode(card, 1, 0);
453 bytes_remaining = firmware->size;
456 /* Load helper firmware image */
457 while (bytes_remaining > 0) {
458 /* Scratch pad 1 should contain the number of bytes we
459 * want to download to the firmware */
460 err = spu_write_u16(card, IF_SPI_SCRATCH_1_REG,
461 HELPER_FW_LOAD_CHUNK_SZ);
465 err = spu_wait_for_u16(card, IF_SPI_HOST_INT_STATUS_REG,
466 IF_SPI_HIST_CMD_DOWNLOAD_RDY,
467 IF_SPI_HIST_CMD_DOWNLOAD_RDY);
471 /* Feed the data into the command read/write port reg
472 * in chunks of 64 bytes */
473 memset(temp, 0, sizeof(temp));
475 min(bytes_remaining, HELPER_FW_LOAD_CHUNK_SZ));
477 err = spu_write(card, IF_SPI_CMD_RDWRPORT_REG,
478 temp, HELPER_FW_LOAD_CHUNK_SZ);
482 /* Interrupt the boot code */
483 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
486 err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG,
487 IF_SPI_CIC_CMD_DOWNLOAD_OVER);
490 bytes_remaining -= HELPER_FW_LOAD_CHUNK_SZ;
491 fw += HELPER_FW_LOAD_CHUNK_SZ;
494 /* Once the helper / single stage firmware download is complete,
495 * write 0 to scratch pad 1 and interrupt the
496 * bootloader. This completes the helper download. */
497 err = spu_write_u16(card, IF_SPI_SCRATCH_1_REG, FIRMWARE_DNLD_OK);
500 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
503 err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG,
504 IF_SPI_CIC_CMD_DOWNLOAD_OVER);
507 lbs_deb_spi("waiting for helper to boot...\n");
511 lbs_pr_err("failed to load helper firmware (err=%d)\n", err);
512 lbs_deb_leave_args(LBS_DEB_SPI, "err %d", err);
516 /* Returns the length of the next packet the firmware expects us to send
517 * Sets crc_err if the previous transfer had a CRC error. */
518 static int if_spi_prog_main_firmware_check_len(struct if_spi_card *card,
524 /* wait until the host interrupt status register indicates
525 * that we are ready to download */
526 err = spu_wait_for_u16(card, IF_SPI_HOST_INT_STATUS_REG,
527 IF_SPI_HIST_CMD_DOWNLOAD_RDY,
528 IF_SPI_HIST_CMD_DOWNLOAD_RDY);
530 lbs_pr_err("timed out waiting for host_int_status\n");
534 /* Ask the device how many bytes of firmware it wants. */
535 err = spu_read_u16(card, IF_SPI_SCRATCH_1_REG, &len);
539 if (len > IF_SPI_CMD_BUF_SIZE) {
540 lbs_pr_err("firmware load device requested a larger "
541 "tranfer than we are prepared to "
542 "handle. (len = %d)\n", len);
546 lbs_deb_spi("%s: crc error\n", __func__);
555 static int if_spi_prog_main_firmware(struct if_spi_card *card,
556 const struct firmware *firmware)
559 int bytes, crc_err = 0, err = 0;
563 lbs_deb_enter(LBS_DEB_SPI);
565 err = spu_set_interrupt_mode(card, 1, 0);
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__);
578 bytes = firmware->size;
580 while ((len = if_spi_prog_main_firmware_check_len(card, &crc_err))) {
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");
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);
614 err = spu_write(card, IF_SPI_CMD_RDWRPORT_REG,
615 card->cmd_buffer, len);
618 err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG ,
619 IF_SPI_CIC_CMD_DOWNLOAD_OVER);
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");
639 lbs_pr_err("failed to load firmware (err=%d)\n", err);
640 lbs_deb_leave_args(LBS_DEB_SPI, "err %d", err);
645 * SPI Transfer Thread
647 * The SPI worker handles all SPI transfers, so there is no need for a lock.
650 /* Move a command from the card to the host */
651 static int if_spi_c2h_cmd(struct if_spi_card *card)
653 struct lbs_private *priv = card->priv;
659 /* We need a buffer big enough to handle whatever people send to
661 BUILD_BUG_ON(IF_SPI_CMD_BUF_SIZE < LBS_CMD_BUFFER_SIZE);
662 BUILD_BUG_ON(IF_SPI_CMD_BUF_SIZE < LBS_UPLD_SIZE);
664 /* It's just annoying if the buffer size isn't a multiple of 4, because
665 * then we might have len < IF_SPI_CMD_BUF_SIZE but
666 * ALIGN(len, 4) > IF_SPI_CMD_BUF_SIZE */
667 BUILD_BUG_ON(IF_SPI_CMD_BUF_SIZE % 4 != 0);
669 lbs_deb_enter(LBS_DEB_SPI);
671 /* How many bytes are there to read? */
672 err = spu_read_u16(card, IF_SPI_SCRATCH_2_REG, &len);
676 lbs_pr_err("%s: error: card has no data for host\n",
680 } else if (len > IF_SPI_CMD_BUF_SIZE) {
681 lbs_pr_err("%s: error: response packet too large: "
682 "%d bytes, but maximum is %d\n",
683 __func__, len, IF_SPI_CMD_BUF_SIZE);
688 /* Read the data from the WLAN module into our command buffer */
689 err = spu_read(card, IF_SPI_CMD_RDWRPORT_REG,
690 card->cmd_buffer, ALIGN(len, 4));
694 spin_lock_irqsave(&priv->driver_lock, flags);
695 i = (priv->resp_idx == 0) ? 1 : 0;
696 BUG_ON(priv->resp_len[i]);
697 priv->resp_len[i] = len;
698 memcpy(priv->resp_buf[i], card->cmd_buffer, len);
699 lbs_notify_command_response(priv, i);
700 spin_unlock_irqrestore(&priv->driver_lock, flags);
704 lbs_pr_err("%s: err=%d\n", __func__, err);
705 lbs_deb_leave(LBS_DEB_SPI);
709 /* Move data from the card to the host */
710 static int if_spi_c2h_data(struct if_spi_card *card)
717 lbs_deb_enter(LBS_DEB_SPI);
719 /* How many bytes are there to read? */
720 err = spu_read_u16(card, IF_SPI_SCRATCH_1_REG, &len);
724 lbs_pr_err("%s: error: card has no data for host\n",
728 } else if (len > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
729 lbs_pr_err("%s: error: card has %d bytes of data, but "
730 "our maximum skb size is %zu\n",
731 __func__, len, MRVDRV_ETH_RX_PACKET_BUFFER_SIZE);
736 /* TODO: should we allocate a smaller skb if we have less data? */
737 skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE);
742 skb_reserve(skb, IPFIELD_ALIGN_OFFSET);
743 data = skb_put(skb, len);
745 /* Read the data from the WLAN module into our skb... */
746 err = spu_read(card, IF_SPI_DATA_RDWRPORT_REG, data, ALIGN(len, 4));
750 /* pass the SKB to libertas */
751 err = lbs_process_rxed_packet(card->priv, skb);
762 lbs_pr_err("%s: err=%d\n", __func__, err);
763 lbs_deb_leave(LBS_DEB_SPI);
767 /* Move data or a command from the host to the card. */
768 static void if_spi_h2c(struct if_spi_card *card,
769 struct if_spi_packet *packet, int type)
772 u16 int_type, port_reg;
776 int_type = IF_SPI_CIC_TX_DOWNLOAD_OVER;
777 port_reg = IF_SPI_DATA_RDWRPORT_REG;
780 int_type = IF_SPI_CIC_CMD_DOWNLOAD_OVER;
781 port_reg = IF_SPI_CMD_RDWRPORT_REG;
784 lbs_pr_err("can't transfer buffer of type %d\n", type);
789 /* Write the data to the card */
790 err = spu_write(card, port_reg, packet->buffer, packet->blen);
798 lbs_pr_err("%s: error %d\n", __func__, err);
801 /* Inform the host about a card event */
802 static void if_spi_e2h(struct if_spi_card *card)
806 struct lbs_private *priv = card->priv;
808 err = spu_read_u32(card, IF_SPI_SCRATCH_3_REG, &cause);
812 /* re-enable the card event interrupt */
813 spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG,
814 ~IF_SPI_HICU_CARD_EVENT);
816 /* generate a card interrupt */
817 spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG, IF_SPI_CIC_HOST_EVENT);
819 lbs_queue_event(priv, cause & 0xff);
822 lbs_pr_err("%s: error %d\n", __func__, err);
825 static void if_spi_host_to_card_worker(struct work_struct *work)
828 struct if_spi_card *card;
831 struct if_spi_packet *packet;
833 card = container_of(work, struct if_spi_card, packet_work);
835 lbs_deb_enter(LBS_DEB_SPI);
837 /* Read the host interrupt status register to see what we
839 err = spu_read_u16(card, IF_SPI_HOST_INT_STATUS_REG,
842 lbs_pr_err("I/O error\n");
846 if (hiStatus & IF_SPI_HIST_CMD_UPLOAD_RDY) {
847 err = if_spi_c2h_cmd(card);
851 if (hiStatus & IF_SPI_HIST_RX_UPLOAD_RDY) {
852 err = if_spi_c2h_data(card);
857 /* workaround: in PS mode, the card does not set the Command
858 * Download Ready bit, but it sets TX Download Ready. */
859 if (hiStatus & IF_SPI_HIST_CMD_DOWNLOAD_RDY ||
860 (card->priv->psstate != PS_STATE_FULL_POWER &&
861 (hiStatus & IF_SPI_HIST_TX_DOWNLOAD_RDY))) {
862 /* This means two things. First of all,
863 * if there was a previous command sent, the card has
864 * successfully received it.
865 * Secondly, it is now ready to download another
868 lbs_host_to_card_done(card->priv);
870 /* Do we have any command packets from the host to
873 spin_lock_irqsave(&card->buffer_lock, flags);
874 if (!list_empty(&card->cmd_packet_list)) {
875 packet = (struct if_spi_packet *)(card->
876 cmd_packet_list.next);
877 list_del(&packet->list);
879 spin_unlock_irqrestore(&card->buffer_lock, flags);
882 if_spi_h2c(card, packet, MVMS_CMD);
884 if (hiStatus & IF_SPI_HIST_TX_DOWNLOAD_RDY) {
885 /* Do we have any data packets from the host to
888 spin_lock_irqsave(&card->buffer_lock, flags);
889 if (!list_empty(&card->data_packet_list)) {
890 packet = (struct if_spi_packet *)(card->
891 data_packet_list.next);
892 list_del(&packet->list);
894 spin_unlock_irqrestore(&card->buffer_lock, flags);
897 if_spi_h2c(card, packet, MVMS_DAT);
899 if (hiStatus & IF_SPI_HIST_CARD_EVENT)
904 lbs_pr_err("%s: got error %d\n", __func__, err);
906 lbs_deb_leave(LBS_DEB_SPI);
912 * Called from Libertas to transfer some data to the WLAN device
913 * We can't sleep here. */
914 static int if_spi_host_to_card(struct lbs_private *priv,
915 u8 type, u8 *buf, u16 nb)
919 struct if_spi_card *card = priv->card;
920 struct if_spi_packet *packet;
923 lbs_deb_enter_args(LBS_DEB_SPI, "type %d, bytes %d", type, nb);
926 lbs_pr_err("%s: invalid size requested: %d\n", __func__, nb);
931 packet = kzalloc(sizeof(struct if_spi_packet) + blen, GFP_ATOMIC);
937 memcpy(packet->buffer, buf, nb);
938 memset(packet->buffer + nb, 0, blen - nb);
942 priv->dnld_sent = DNLD_CMD_SENT;
943 spin_lock_irqsave(&card->buffer_lock, flags);
944 list_add_tail(&packet->list, &card->cmd_packet_list);
945 spin_unlock_irqrestore(&card->buffer_lock, flags);
948 priv->dnld_sent = DNLD_DATA_SENT;
949 spin_lock_irqsave(&card->buffer_lock, flags);
950 list_add_tail(&packet->list, &card->data_packet_list);
951 spin_unlock_irqrestore(&card->buffer_lock, flags);
954 lbs_pr_err("can't transfer buffer of type %d", type);
959 /* Queue spi xfer work */
960 queue_work(card->workqueue, &card->packet_work);
962 lbs_deb_leave_args(LBS_DEB_SPI, "err=%d", err);
969 * Service incoming interrupts from the WLAN device. We can't sleep here, so
970 * don't try to talk on the SPI bus, just queue the SPI xfer work.
972 static irqreturn_t if_spi_host_interrupt(int irq, void *dev_id)
974 struct if_spi_card *card = dev_id;
976 queue_work(card->workqueue, &card->packet_work);
985 static int if_spi_init_card(struct if_spi_card *card)
987 struct spi_device *spi = card->spi;
990 const struct firmware *helper = NULL;
991 const struct firmware *mainfw = NULL;
993 lbs_deb_enter(LBS_DEB_SPI);
995 err = spu_init(card, card->pdata->use_dummy_writes);
998 err = spu_get_chip_revision(card, &card->card_id, &card->card_rev);
1002 err = spu_read_u32(card, IF_SPI_SCRATCH_4_REG, &scratch);
1005 if (scratch == SUCCESSFUL_FW_DOWNLOAD_MAGIC)
1006 lbs_deb_spi("Firmware is already loaded for "
1007 "Marvell WLAN 802.11 adapter\n");
1009 /* Check if we support this card */
1010 for (i = 0; i < ARRAY_SIZE(fw_table); i++) {
1011 if (card->card_id == fw_table[i].model)
1014 if (i == ARRAY_SIZE(fw_table)) {
1015 lbs_pr_err("Unsupported chip_id: 0x%02x\n",
1021 err = lbs_get_firmware(&card->spi->dev, NULL, NULL,
1022 card->card_id, &fw_table[0], &helper,
1025 lbs_pr_err("failed to find firmware (%d)\n", err);
1029 lbs_deb_spi("Initializing FW for Marvell WLAN 802.11 adapter "
1030 "(chip_id = 0x%04x, chip_rev = 0x%02x) "
1031 "attached to SPI bus_num %d, chip_select %d. "
1032 "spi->max_speed_hz=%d\n",
1033 card->card_id, card->card_rev,
1034 spi->master->bus_num, spi->chip_select,
1036 err = if_spi_prog_helper_firmware(card, helper);
1039 err = if_spi_prog_main_firmware(card, mainfw);
1042 lbs_deb_spi("loaded FW for Marvell WLAN 802.11 adapter\n");
1045 err = spu_set_interrupt_mode(card, 0, 1);
1051 release_firmware(helper);
1053 release_firmware(mainfw);
1055 lbs_deb_leave_args(LBS_DEB_SPI, "err %d\n", err);
1060 static int __devinit if_spi_probe(struct spi_device *spi)
1062 struct if_spi_card *card;
1063 struct lbs_private *priv = NULL;
1064 struct libertas_spi_platform_data *pdata = spi->dev.platform_data;
1067 lbs_deb_enter(LBS_DEB_SPI);
1075 err = pdata->setup(spi);
1080 /* Allocate card structure to represent this specific device */
1081 card = kzalloc(sizeof(struct if_spi_card), GFP_KERNEL);
1086 spi_set_drvdata(spi, card);
1087 card->pdata = pdata;
1089 card->prev_xfer_time = jiffies;
1091 INIT_LIST_HEAD(&card->cmd_packet_list);
1092 INIT_LIST_HEAD(&card->data_packet_list);
1093 spin_lock_init(&card->buffer_lock);
1095 /* Initialize the SPI Interface Unit */
1098 err = if_spi_init_card(card);
1102 /* Register our card with libertas.
1103 * This will call alloc_etherdev */
1104 priv = lbs_add_card(card, &spi->dev);
1111 priv->hw_host_to_card = if_spi_host_to_card;
1112 priv->enter_deep_sleep = NULL;
1113 priv->exit_deep_sleep = NULL;
1114 priv->reset_deep_sleep_wakeup = NULL;
1117 /* Initialize interrupt handling stuff. */
1118 card->workqueue = create_workqueue("libertas_spi");
1119 INIT_WORK(&card->packet_work, if_spi_host_to_card_worker);
1121 err = request_irq(spi->irq, if_spi_host_interrupt,
1122 IRQF_TRIGGER_FALLING, "libertas_spi", card);
1124 lbs_pr_err("can't get host irq line-- request_irq failed\n");
1125 goto terminate_workqueue;
1129 * This will call register_netdev, and we'll start
1130 * getting interrupts... */
1131 err = lbs_start_card(priv);
1135 lbs_deb_spi("Finished initializing WLAN module.\n");
1137 /* successful exit */
1141 free_irq(spi->irq, card);
1142 terminate_workqueue:
1143 flush_workqueue(card->workqueue);
1144 destroy_workqueue(card->workqueue);
1145 lbs_remove_card(priv); /* will call free_netdev */
1147 free_if_spi_card(card);
1149 if (pdata->teardown)
1150 pdata->teardown(spi);
1152 lbs_deb_leave_args(LBS_DEB_SPI, "err %d\n", err);
1156 static int __devexit libertas_spi_remove(struct spi_device *spi)
1158 struct if_spi_card *card = spi_get_drvdata(spi);
1159 struct lbs_private *priv = card->priv;
1161 lbs_deb_spi("libertas_spi_remove\n");
1162 lbs_deb_enter(LBS_DEB_SPI);
1164 lbs_stop_card(priv);
1165 lbs_remove_card(priv); /* will call free_netdev */
1167 free_irq(spi->irq, card);
1168 flush_workqueue(card->workqueue);
1169 destroy_workqueue(card->workqueue);
1170 if (card->pdata->teardown)
1171 card->pdata->teardown(spi);
1172 free_if_spi_card(card);
1173 lbs_deb_leave(LBS_DEB_SPI);
1177 static struct spi_driver libertas_spi_driver = {
1178 .probe = if_spi_probe,
1179 .remove = __devexit_p(libertas_spi_remove),
1181 .name = "libertas_spi",
1182 .bus = &spi_bus_type,
1183 .owner = THIS_MODULE,
1191 static int __init if_spi_init_module(void)
1194 lbs_deb_enter(LBS_DEB_SPI);
1195 printk(KERN_INFO "libertas_spi: Libertas SPI driver\n");
1196 ret = spi_register_driver(&libertas_spi_driver);
1197 lbs_deb_leave(LBS_DEB_SPI);
1201 static void __exit if_spi_exit_module(void)
1203 lbs_deb_enter(LBS_DEB_SPI);
1204 spi_unregister_driver(&libertas_spi_driver);
1205 lbs_deb_leave(LBS_DEB_SPI);
1208 module_init(if_spi_init_module);
1209 module_exit(if_spi_exit_module);
1211 MODULE_DESCRIPTION("Libertas SPI WLAN Driver");
1212 MODULE_AUTHOR("Andrey Yurovsky <andrey@cozybit.com>, "
1213 "Colin McCabe <colin@cozybit.com>");
1214 MODULE_LICENSE("GPL");
1215 MODULE_ALIAS("spi:libertas_spi");