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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22 #include <linux/hardirq.h>
23 #include <linux/interrupt.h>
24 #include <linux/module.h>
25 #include <linux/firmware.h>
26 #include <linux/jiffies.h>
27 #include <linux/list.h>
28 #include <linux/netdevice.h>
29 #include <linux/slab.h>
30 #include <linux/spi/libertas_spi.h>
31 #include <linux/spi/spi.h>
39 struct if_spi_packet {
40 struct list_head list;
42 u8 buffer[0] __attribute__((aligned(4)));
46 struct spi_device *spi;
47 struct lbs_private *priv;
48 struct libertas_spi_platform_data *pdata;
50 /* The card ID and card revision, as reported by the hardware. */
54 /* The last time that we initiated an SPU operation */
55 unsigned long prev_xfer_time;
58 unsigned long spu_port_delay;
59 unsigned long spu_reg_delay;
61 /* Handles all SPI communication (except for FW load) */
62 struct workqueue_struct *workqueue;
63 struct work_struct packet_work;
64 struct work_struct resume_work;
66 u8 cmd_buffer[IF_SPI_CMD_BUF_SIZE];
68 /* A buffer of incoming packets from libertas core.
69 * Since we can't sleep in hw_host_to_card, we have to buffer
71 struct list_head cmd_packet_list;
72 struct list_head data_packet_list;
74 /* Protects cmd_packet_list and data_packet_list */
75 spinlock_t buffer_lock;
77 /* True is card suspended */
81 static void free_if_spi_card(struct if_spi_card *card)
83 struct list_head *cursor, *next;
84 struct if_spi_packet *packet;
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);
99 #define MODEL_8385 0x04
100 #define MODEL_8686 0x0b
101 #define MODEL_8688 0x10
103 static const struct lbs_fw_table fw_table[] = {
104 { MODEL_8385, "/*(DEBLOBBED)*/", "/*(DEBLOBBED)*/" },
105 { MODEL_8385, "/*(DEBLOBBED)*/", "/*(DEBLOBBED)*/" },
106 { MODEL_8686, "/*(DEBLOBBED)*/", "/*(DEBLOBBED)*/" },
107 { MODEL_8686, "/*(DEBLOBBED)*/", "/*(DEBLOBBED)*/" },
108 { MODEL_8688, "/*(DEBLOBBED)*/", "/*(DEBLOBBED)*/" },
115 * SPI Interface Unit Routines
117 * The SPU sits between the host and the WLAN module.
118 * All communication with the firmware is through SPU transactions.
120 * First we have to put a SPU register name on the bus. Then we can
121 * either read from or write to that register.
125 static void spu_transaction_init(struct if_spi_card *card)
127 if (!time_after(jiffies, card->prev_xfer_time + 1)) {
128 /* Unfortunately, the SPU requires a delay between successive
129 * transactions. If our last transaction was more than a jiffy
130 * ago, we have obviously already delayed enough.
131 * If not, we have to busy-wait to be on the safe side. */
136 static void spu_transaction_finish(struct if_spi_card *card)
138 card->prev_xfer_time = jiffies;
142 * Write out a byte buffer to an SPI register,
143 * using a series of 16-bit transfers.
145 static int spu_write(struct if_spi_card *card, u16 reg, const u8 *buf, int len)
148 __le16 reg_out = cpu_to_le16(reg | IF_SPI_WRITE_OPERATION_MASK);
149 struct spi_message m;
150 struct spi_transfer reg_trans;
151 struct spi_transfer data_trans;
153 spi_message_init(&m);
154 memset(®_trans, 0, sizeof(reg_trans));
155 memset(&data_trans, 0, sizeof(data_trans));
157 /* You must give an even number of bytes to the SPU, even if it
158 * doesn't care about the last one. */
161 spu_transaction_init(card);
163 /* write SPU register index */
164 reg_trans.tx_buf = ®_out;
165 reg_trans.len = sizeof(reg_out);
167 data_trans.tx_buf = buf;
168 data_trans.len = len;
170 spi_message_add_tail(®_trans, &m);
171 spi_message_add_tail(&data_trans, &m);
173 err = spi_sync(card->spi, &m);
174 spu_transaction_finish(card);
178 static inline int spu_write_u16(struct if_spi_card *card, u16 reg, u16 val)
182 buff = cpu_to_le16(val);
183 return spu_write(card, reg, (u8 *)&buff, sizeof(u16));
186 static inline int spu_reg_is_port_reg(u16 reg)
189 case IF_SPI_IO_RDWRPORT_REG:
190 case IF_SPI_CMD_RDWRPORT_REG:
191 case IF_SPI_DATA_RDWRPORT_REG:
198 static int spu_read(struct if_spi_card *card, u16 reg, u8 *buf, int len)
202 __le16 reg_out = cpu_to_le16(reg | IF_SPI_READ_OPERATION_MASK);
203 struct spi_message m;
204 struct spi_transfer reg_trans;
205 struct spi_transfer dummy_trans;
206 struct spi_transfer data_trans;
209 * You must take an even number of bytes from the SPU, even if you
210 * don't care about the last one.
214 spu_transaction_init(card);
216 spi_message_init(&m);
217 memset(®_trans, 0, sizeof(reg_trans));
218 memset(&dummy_trans, 0, sizeof(dummy_trans));
219 memset(&data_trans, 0, sizeof(data_trans));
221 /* write SPU register index */
222 reg_trans.tx_buf = ®_out;
223 reg_trans.len = sizeof(reg_out);
224 spi_message_add_tail(®_trans, &m);
226 delay = spu_reg_is_port_reg(reg) ? card->spu_port_delay :
228 if (card->use_dummy_writes) {
229 /* Clock in dummy cycles while the SPU fills the FIFO */
230 dummy_trans.len = delay / 8;
231 spi_message_add_tail(&dummy_trans, &m);
233 /* Busy-wait while the SPU fills the FIFO */
234 reg_trans.delay_usecs =
235 DIV_ROUND_UP((100 + (delay * 10)), 1000);
239 data_trans.rx_buf = buf;
240 data_trans.len = len;
241 spi_message_add_tail(&data_trans, &m);
243 err = spi_sync(card->spi, &m);
244 spu_transaction_finish(card);
248 /* Read 16 bits from an SPI register */
249 static inline int spu_read_u16(struct if_spi_card *card, u16 reg, u16 *val)
254 ret = spu_read(card, reg, (u8 *)&buf, sizeof(buf));
256 *val = le16_to_cpup(&buf);
261 * Read 32 bits from an SPI register.
262 * The low 16 bits are read first.
264 static int spu_read_u32(struct if_spi_card *card, u16 reg, u32 *val)
269 err = spu_read(card, reg, (u8 *)&buf, sizeof(buf));
271 *val = le32_to_cpup(&buf);
276 * Keep reading 16 bits from an SPI register until you get the correct result.
278 * If mask = 0, the correct result is any non-zero number.
279 * If mask != 0, the correct result is any number where
280 * number & target_mask == target
282 * Returns -ETIMEDOUT if a second passes without the correct result.
284 static int spu_wait_for_u16(struct if_spi_card *card, u16 reg,
285 u16 target_mask, u16 target)
288 unsigned long timeout = jiffies + 5*HZ;
291 err = spu_read_u16(card, reg, &val);
295 if ((val & target_mask) == target)
302 if (time_after(jiffies, timeout)) {
303 pr_err("%s: timeout with val=%02x, target_mask=%02x, target=%02x\n",
304 __func__, val, target_mask, target);
311 * Read 16 bits from an SPI register until you receive a specific value.
312 * Returns -ETIMEDOUT if a 4 tries pass without success.
314 static int spu_wait_for_u32(struct if_spi_card *card, u32 reg, u32 target)
317 for (try = 0; try < 4; ++try) {
319 err = spu_read_u32(card, reg, &val);
329 static int spu_set_interrupt_mode(struct if_spi_card *card,
330 int suppress_host_int,
336 * We can suppress a host interrupt by clearing the appropriate
337 * bit in the "host interrupt status mask" register
339 if (suppress_host_int) {
340 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG, 0);
344 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG,
345 IF_SPI_HISM_TX_DOWNLOAD_RDY |
346 IF_SPI_HISM_RX_UPLOAD_RDY |
347 IF_SPI_HISM_CMD_DOWNLOAD_RDY |
348 IF_SPI_HISM_CARDEVENT |
349 IF_SPI_HISM_CMD_UPLOAD_RDY);
355 * If auto-interrupts are on, the completion of certain transactions
356 * will trigger an interrupt automatically. If auto-interrupts
357 * are off, we need to set the "Card Interrupt Cause" register to
358 * trigger a card interrupt.
361 err = spu_write_u16(card, IF_SPI_HOST_INT_CTRL_REG,
362 IF_SPI_HICT_TX_DOWNLOAD_OVER_AUTO |
363 IF_SPI_HICT_RX_UPLOAD_OVER_AUTO |
364 IF_SPI_HICT_CMD_DOWNLOAD_OVER_AUTO |
365 IF_SPI_HICT_CMD_UPLOAD_OVER_AUTO);
369 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG, 0);
376 static int spu_get_chip_revision(struct if_spi_card *card,
377 u16 *card_id, u8 *card_rev)
381 err = spu_read_u32(card, IF_SPI_DEVICEID_CTRL_REG, &dev_ctrl);
384 *card_id = IF_SPI_DEVICEID_CTRL_REG_TO_CARD_ID(dev_ctrl);
385 *card_rev = IF_SPI_DEVICEID_CTRL_REG_TO_CARD_REV(dev_ctrl);
389 static int spu_set_bus_mode(struct if_spi_card *card, u16 mode)
394 err = spu_write_u16(card, IF_SPI_SPU_BUS_MODE_REG, mode);
397 /* Check that we were able to read back what we just wrote. */
398 err = spu_read_u16(card, IF_SPI_SPU_BUS_MODE_REG, &rval);
401 if ((rval & 0xF) != mode) {
402 pr_err("Can't read bus mode register\n");
408 static int spu_init(struct if_spi_card *card, int use_dummy_writes)
414 * We have to start up in timed delay mode so that we can safely
415 * read the Delay Read Register.
417 card->use_dummy_writes = 0;
418 err = spu_set_bus_mode(card,
419 IF_SPI_BUS_MODE_SPI_CLOCK_PHASE_RISING |
420 IF_SPI_BUS_MODE_DELAY_METHOD_TIMED |
421 IF_SPI_BUS_MODE_16_BIT_ADDRESS_16_BIT_DATA);
424 card->spu_port_delay = 1000;
425 card->spu_reg_delay = 1000;
426 err = spu_read_u32(card, IF_SPI_DELAY_READ_REG, &delay);
429 card->spu_port_delay = delay & 0x0000ffff;
430 card->spu_reg_delay = (delay & 0xffff0000) >> 16;
432 /* If dummy clock delay mode has been requested, switch to it now */
433 if (use_dummy_writes) {
434 card->use_dummy_writes = 1;
435 err = spu_set_bus_mode(card,
436 IF_SPI_BUS_MODE_SPI_CLOCK_PHASE_RISING |
437 IF_SPI_BUS_MODE_DELAY_METHOD_DUMMY_CLOCK |
438 IF_SPI_BUS_MODE_16_BIT_ADDRESS_16_BIT_DATA);
443 lbs_deb_spi("Initialized SPU unit. "
444 "spu_port_delay=0x%04lx, spu_reg_delay=0x%04lx\n",
445 card->spu_port_delay, card->spu_reg_delay);
453 static int if_spi_prog_helper_firmware(struct if_spi_card *card,
454 const struct firmware *firmware)
459 u8 temp[HELPER_FW_LOAD_CHUNK_SZ];
461 lbs_deb_enter(LBS_DEB_SPI);
463 err = spu_set_interrupt_mode(card, 1, 0);
467 bytes_remaining = firmware->size;
470 /* Load helper firmware image */
471 while (bytes_remaining > 0) {
473 * Scratch pad 1 should contain the number of bytes we
474 * want to download to the firmware
476 err = spu_write_u16(card, IF_SPI_SCRATCH_1_REG,
477 HELPER_FW_LOAD_CHUNK_SZ);
481 err = spu_wait_for_u16(card, IF_SPI_HOST_INT_STATUS_REG,
482 IF_SPI_HIST_CMD_DOWNLOAD_RDY,
483 IF_SPI_HIST_CMD_DOWNLOAD_RDY);
488 * Feed the data into the command read/write port reg
489 * in chunks of 64 bytes
491 memset(temp, 0, sizeof(temp));
493 min(bytes_remaining, HELPER_FW_LOAD_CHUNK_SZ));
495 err = spu_write(card, IF_SPI_CMD_RDWRPORT_REG,
496 temp, HELPER_FW_LOAD_CHUNK_SZ);
500 /* Interrupt the boot code */
501 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
504 err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG,
505 IF_SPI_CIC_CMD_DOWNLOAD_OVER);
508 bytes_remaining -= HELPER_FW_LOAD_CHUNK_SZ;
509 fw += HELPER_FW_LOAD_CHUNK_SZ;
513 * Once the helper / single stage firmware download is complete,
514 * write 0 to scratch pad 1 and interrupt the
515 * bootloader. This completes the helper download.
517 err = spu_write_u16(card, IF_SPI_SCRATCH_1_REG, FIRMWARE_DNLD_OK);
520 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
523 err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG,
524 IF_SPI_CIC_CMD_DOWNLOAD_OVER);
527 pr_err("failed to load helper firmware (err=%d)\n", err);
528 lbs_deb_leave_args(LBS_DEB_SPI, "err %d", err);
533 * Returns the length of the next packet the firmware expects us to send.
534 * Sets crc_err if the previous transfer had a CRC error.
536 static int if_spi_prog_main_firmware_check_len(struct if_spi_card *card,
543 * wait until the host interrupt status register indicates
544 * that we are ready to download
546 err = spu_wait_for_u16(card, IF_SPI_HOST_INT_STATUS_REG,
547 IF_SPI_HIST_CMD_DOWNLOAD_RDY,
548 IF_SPI_HIST_CMD_DOWNLOAD_RDY);
550 pr_err("timed out waiting for host_int_status\n");
554 /* Ask the device how many bytes of firmware it wants. */
555 err = spu_read_u16(card, IF_SPI_SCRATCH_1_REG, &len);
559 if (len > IF_SPI_CMD_BUF_SIZE) {
560 pr_err("firmware load device requested a larger transfer than we are prepared to handle (len = %d)\n",
565 lbs_deb_spi("%s: crc error\n", __func__);
574 static int if_spi_prog_main_firmware(struct if_spi_card *card,
575 const struct firmware *firmware)
577 struct lbs_private *priv = card->priv;
579 int bytes, crc_err = 0, err = 0;
583 lbs_deb_enter(LBS_DEB_SPI);
585 err = spu_set_interrupt_mode(card, 1, 0);
589 err = spu_wait_for_u16(card, IF_SPI_SCRATCH_1_REG, 0, 0);
591 netdev_err(priv->dev,
592 "%s: timed out waiting for initial scratch reg = 0\n",
599 bytes = firmware->size;
601 while ((len = if_spi_prog_main_firmware_check_len(card, &crc_err))) {
608 * If there are no more bytes left, we would normally
609 * expect to have terminated with len = 0
611 netdev_err(priv->dev,
612 "Firmware load wants more bytes than we have to offer.\n");
616 /* Previous transfer failed. */
617 if (++num_crc_errs > MAX_MAIN_FW_LOAD_CRC_ERR) {
618 pr_err("Too many CRC errors encountered in firmware load.\n");
623 /* Previous transfer succeeded. Advance counters. */
628 memset(card->cmd_buffer, 0, len);
629 memcpy(card->cmd_buffer, fw, bytes);
631 memcpy(card->cmd_buffer, fw, len);
633 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
636 err = spu_write(card, IF_SPI_CMD_RDWRPORT_REG,
637 card->cmd_buffer, len);
640 err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG ,
641 IF_SPI_CIC_CMD_DOWNLOAD_OVER);
646 if (bytes > prev_len) {
647 pr_err("firmware load wants fewer bytes than we have to offer\n");
650 /* Confirm firmware download */
651 err = spu_wait_for_u32(card, IF_SPI_SCRATCH_4_REG,
652 SUCCESSFUL_FW_DOWNLOAD_MAGIC);
654 pr_err("failed to confirm the firmware download\n");
660 pr_err("failed to load firmware (err=%d)\n", err);
661 lbs_deb_leave_args(LBS_DEB_SPI, "err %d", err);
666 * SPI Transfer Thread
668 * The SPI worker handles all SPI transfers, so there is no need for a lock.
671 /* Move a command from the card to the host */
672 static int if_spi_c2h_cmd(struct if_spi_card *card)
674 struct lbs_private *priv = card->priv;
681 * We need a buffer big enough to handle whatever people send to
684 BUILD_BUG_ON(IF_SPI_CMD_BUF_SIZE < LBS_CMD_BUFFER_SIZE);
685 BUILD_BUG_ON(IF_SPI_CMD_BUF_SIZE < LBS_UPLD_SIZE);
688 * It's just annoying if the buffer size isn't a multiple of 4, because
689 * then we might have len < IF_SPI_CMD_BUF_SIZE but
690 * ALIGN(len, 4) > IF_SPI_CMD_BUF_SIZE
692 BUILD_BUG_ON(IF_SPI_CMD_BUF_SIZE % 4 != 0);
694 lbs_deb_enter(LBS_DEB_SPI);
696 /* How many bytes are there to read? */
697 err = spu_read_u16(card, IF_SPI_SCRATCH_2_REG, &len);
701 netdev_err(priv->dev, "%s: error: card has no data for host\n",
705 } else if (len > IF_SPI_CMD_BUF_SIZE) {
706 netdev_err(priv->dev,
707 "%s: error: response packet too large: %d bytes, but maximum is %d\n",
708 __func__, len, IF_SPI_CMD_BUF_SIZE);
713 /* Read the data from the WLAN module into our command buffer */
714 err = spu_read(card, IF_SPI_CMD_RDWRPORT_REG,
715 card->cmd_buffer, ALIGN(len, 4));
719 spin_lock_irqsave(&priv->driver_lock, flags);
720 i = (priv->resp_idx == 0) ? 1 : 0;
721 BUG_ON(priv->resp_len[i]);
722 priv->resp_len[i] = len;
723 memcpy(priv->resp_buf[i], card->cmd_buffer, len);
724 lbs_notify_command_response(priv, i);
725 spin_unlock_irqrestore(&priv->driver_lock, flags);
729 netdev_err(priv->dev, "%s: err=%d\n", __func__, err);
730 lbs_deb_leave(LBS_DEB_SPI);
734 /* Move data from the card to the host */
735 static int if_spi_c2h_data(struct if_spi_card *card)
737 struct lbs_private *priv = card->priv;
743 lbs_deb_enter(LBS_DEB_SPI);
745 /* How many bytes are there to read? */
746 err = spu_read_u16(card, IF_SPI_SCRATCH_1_REG, &len);
750 netdev_err(priv->dev, "%s: error: card has no data for host\n",
754 } else if (len > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
755 netdev_err(priv->dev,
756 "%s: error: card has %d bytes of data, but our maximum skb size is %zu\n",
757 __func__, len, MRVDRV_ETH_RX_PACKET_BUFFER_SIZE);
762 /* TODO: should we allocate a smaller skb if we have less data? */
763 skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE);
768 skb_reserve(skb, IPFIELD_ALIGN_OFFSET);
769 data = skb_put(skb, len);
771 /* Read the data from the WLAN module into our skb... */
772 err = spu_read(card, IF_SPI_DATA_RDWRPORT_REG, data, ALIGN(len, 4));
776 /* pass the SKB to libertas */
777 err = lbs_process_rxed_packet(card->priv, skb);
788 netdev_err(priv->dev, "%s: err=%d\n", __func__, err);
789 lbs_deb_leave(LBS_DEB_SPI);
793 /* Move data or a command from the host to the card. */
794 static void if_spi_h2c(struct if_spi_card *card,
795 struct if_spi_packet *packet, int type)
797 struct lbs_private *priv = card->priv;
799 u16 int_type, port_reg;
803 int_type = IF_SPI_CIC_TX_DOWNLOAD_OVER;
804 port_reg = IF_SPI_DATA_RDWRPORT_REG;
807 int_type = IF_SPI_CIC_CMD_DOWNLOAD_OVER;
808 port_reg = IF_SPI_CMD_RDWRPORT_REG;
811 netdev_err(priv->dev, "can't transfer buffer of type %d\n",
817 /* Write the data to the card */
818 err = spu_write(card, port_reg, packet->buffer, packet->blen);
826 netdev_err(priv->dev, "%s: error %d\n", __func__, err);
829 /* Inform the host about a card event */
830 static void if_spi_e2h(struct if_spi_card *card)
834 struct lbs_private *priv = card->priv;
836 err = spu_read_u32(card, IF_SPI_SCRATCH_3_REG, &cause);
840 /* re-enable the card event interrupt */
841 spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG,
842 ~IF_SPI_HICU_CARD_EVENT);
844 /* generate a card interrupt */
845 spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG, IF_SPI_CIC_HOST_EVENT);
847 lbs_queue_event(priv, cause & 0xff);
850 netdev_err(priv->dev, "%s: error %d\n", __func__, err);
853 static void if_spi_host_to_card_worker(struct work_struct *work)
856 struct if_spi_card *card;
859 struct if_spi_packet *packet;
860 struct lbs_private *priv;
862 card = container_of(work, struct if_spi_card, packet_work);
865 lbs_deb_enter(LBS_DEB_SPI);
868 * Read the host interrupt status register to see what we
871 err = spu_read_u16(card, IF_SPI_HOST_INT_STATUS_REG,
874 netdev_err(priv->dev, "I/O error\n");
878 if (hiStatus & IF_SPI_HIST_CMD_UPLOAD_RDY) {
879 err = if_spi_c2h_cmd(card);
883 if (hiStatus & IF_SPI_HIST_RX_UPLOAD_RDY) {
884 err = if_spi_c2h_data(card);
890 * workaround: in PS mode, the card does not set the Command
891 * Download Ready bit, but it sets TX Download Ready.
893 if (hiStatus & IF_SPI_HIST_CMD_DOWNLOAD_RDY ||
894 (card->priv->psstate != PS_STATE_FULL_POWER &&
895 (hiStatus & IF_SPI_HIST_TX_DOWNLOAD_RDY))) {
897 * This means two things. First of all,
898 * if there was a previous command sent, the card has
899 * successfully received it.
900 * Secondly, it is now ready to download another
903 lbs_host_to_card_done(card->priv);
905 /* Do we have any command packets from the host to send? */
907 spin_lock_irqsave(&card->buffer_lock, flags);
908 if (!list_empty(&card->cmd_packet_list)) {
909 packet = (struct if_spi_packet *)(card->
910 cmd_packet_list.next);
911 list_del(&packet->list);
913 spin_unlock_irqrestore(&card->buffer_lock, flags);
916 if_spi_h2c(card, packet, MVMS_CMD);
918 if (hiStatus & IF_SPI_HIST_TX_DOWNLOAD_RDY) {
919 /* Do we have any data packets from the host to send? */
921 spin_lock_irqsave(&card->buffer_lock, flags);
922 if (!list_empty(&card->data_packet_list)) {
923 packet = (struct if_spi_packet *)(card->
924 data_packet_list.next);
925 list_del(&packet->list);
927 spin_unlock_irqrestore(&card->buffer_lock, flags);
930 if_spi_h2c(card, packet, MVMS_DAT);
932 if (hiStatus & IF_SPI_HIST_CARD_EVENT)
937 netdev_err(priv->dev, "%s: got error %d\n", __func__, err);
939 lbs_deb_leave(LBS_DEB_SPI);
945 * Called from Libertas to transfer some data to the WLAN device
946 * We can't sleep here.
948 static int if_spi_host_to_card(struct lbs_private *priv,
949 u8 type, u8 *buf, u16 nb)
953 struct if_spi_card *card = priv->card;
954 struct if_spi_packet *packet;
957 lbs_deb_enter_args(LBS_DEB_SPI, "type %d, bytes %d", type, nb);
960 netdev_err(priv->dev, "%s: invalid size requested: %d\n",
966 packet = kzalloc(sizeof(struct if_spi_packet) + blen, GFP_ATOMIC);
972 memcpy(packet->buffer, buf, nb);
973 memset(packet->buffer + nb, 0, blen - nb);
977 priv->dnld_sent = DNLD_CMD_SENT;
978 spin_lock_irqsave(&card->buffer_lock, flags);
979 list_add_tail(&packet->list, &card->cmd_packet_list);
980 spin_unlock_irqrestore(&card->buffer_lock, flags);
983 priv->dnld_sent = DNLD_DATA_SENT;
984 spin_lock_irqsave(&card->buffer_lock, flags);
985 list_add_tail(&packet->list, &card->data_packet_list);
986 spin_unlock_irqrestore(&card->buffer_lock, flags);
990 netdev_err(priv->dev, "can't transfer buffer of type %d\n",
996 /* Queue spi xfer work */
997 queue_work(card->workqueue, &card->packet_work);
999 lbs_deb_leave_args(LBS_DEB_SPI, "err=%d", err);
1006 * Service incoming interrupts from the WLAN device. We can't sleep here, so
1007 * don't try to talk on the SPI bus, just queue the SPI xfer work.
1009 static irqreturn_t if_spi_host_interrupt(int irq, void *dev_id)
1011 struct if_spi_card *card = dev_id;
1013 queue_work(card->workqueue, &card->packet_work);
1022 static int if_spi_init_card(struct if_spi_card *card)
1024 struct lbs_private *priv = card->priv;
1027 const struct firmware *helper = NULL;
1028 const struct firmware *mainfw = NULL;
1030 lbs_deb_enter(LBS_DEB_SPI);
1032 err = spu_init(card, card->pdata->use_dummy_writes);
1035 err = spu_get_chip_revision(card, &card->card_id, &card->card_rev);
1039 err = spu_read_u32(card, IF_SPI_SCRATCH_4_REG, &scratch);
1042 if (scratch == SUCCESSFUL_FW_DOWNLOAD_MAGIC)
1043 lbs_deb_spi("Firmware is already loaded for "
1044 "Marvell WLAN 802.11 adapter\n");
1046 /* Check if we support this card */
1047 for (i = 0; i < ARRAY_SIZE(fw_table); i++) {
1048 if (card->card_id == fw_table[i].model)
1051 if (i == ARRAY_SIZE(fw_table)) {
1052 netdev_err(priv->dev, "Unsupported chip_id: 0x%02x\n",
1058 err = lbs_get_firmware(&card->spi->dev, card->card_id,
1059 &fw_table[0], &helper, &mainfw);
1061 netdev_err(priv->dev, "failed to find firmware (%d)\n",
1066 lbs_deb_spi("Initializing FW for Marvell WLAN 802.11 adapter "
1067 "(chip_id = 0x%04x, chip_rev = 0x%02x) "
1068 "attached to SPI bus_num %d, chip_select %d. "
1069 "spi->max_speed_hz=%d\n",
1070 card->card_id, card->card_rev,
1071 card->spi->master->bus_num,
1072 card->spi->chip_select,
1073 card->spi->max_speed_hz);
1074 err = if_spi_prog_helper_firmware(card, helper);
1077 err = if_spi_prog_main_firmware(card, mainfw);
1080 lbs_deb_spi("loaded FW for Marvell WLAN 802.11 adapter\n");
1083 err = spu_set_interrupt_mode(card, 0, 1);
1088 lbs_deb_leave_args(LBS_DEB_SPI, "err %d\n", err);
1092 static void if_spi_resume_worker(struct work_struct *work)
1094 struct if_spi_card *card;
1096 card = container_of(work, struct if_spi_card, resume_work);
1098 if (card->suspended) {
1099 if (card->pdata->setup)
1100 card->pdata->setup(card->spi);
1103 if_spi_init_card(card);
1105 enable_irq(card->spi->irq);
1107 /* And resume it ... */
1108 lbs_resume(card->priv);
1110 card->suspended = 0;
1114 static int if_spi_probe(struct spi_device *spi)
1116 struct if_spi_card *card;
1117 struct lbs_private *priv = NULL;
1118 struct libertas_spi_platform_data *pdata = dev_get_platdata(&spi->dev);
1121 lbs_deb_enter(LBS_DEB_SPI);
1129 err = pdata->setup(spi);
1134 /* Allocate card structure to represent this specific device */
1135 card = kzalloc(sizeof(struct if_spi_card), GFP_KERNEL);
1140 spi_set_drvdata(spi, card);
1141 card->pdata = pdata;
1143 card->prev_xfer_time = jiffies;
1145 INIT_LIST_HEAD(&card->cmd_packet_list);
1146 INIT_LIST_HEAD(&card->data_packet_list);
1147 spin_lock_init(&card->buffer_lock);
1149 /* Initialize the SPI Interface Unit */
1152 err = if_spi_init_card(card);
1157 * Register our card with libertas.
1158 * This will call alloc_etherdev.
1160 priv = lbs_add_card(card, &spi->dev);
1166 priv->setup_fw_on_resume = 1;
1168 priv->hw_host_to_card = if_spi_host_to_card;
1169 priv->enter_deep_sleep = NULL;
1170 priv->exit_deep_sleep = NULL;
1171 priv->reset_deep_sleep_wakeup = NULL;
1174 /* Initialize interrupt handling stuff. */
1175 card->workqueue = alloc_workqueue("libertas_spi", WQ_MEM_RECLAIM, 0);
1176 if (!card->workqueue) {
1180 INIT_WORK(&card->packet_work, if_spi_host_to_card_worker);
1181 INIT_WORK(&card->resume_work, if_spi_resume_worker);
1183 err = request_irq(spi->irq, if_spi_host_interrupt,
1184 IRQF_TRIGGER_FALLING, "libertas_spi", card);
1186 pr_err("can't get host irq line-- request_irq failed\n");
1187 goto terminate_workqueue;
1192 * This will call register_netdev, and we'll start
1193 * getting interrupts...
1195 err = lbs_start_card(priv);
1199 lbs_deb_spi("Finished initializing WLAN module.\n");
1201 /* successful exit */
1205 free_irq(spi->irq, card);
1206 terminate_workqueue:
1207 destroy_workqueue(card->workqueue);
1209 lbs_remove_card(priv); /* will call free_netdev */
1211 free_if_spi_card(card);
1213 if (pdata->teardown)
1214 pdata->teardown(spi);
1216 lbs_deb_leave_args(LBS_DEB_SPI, "err %d\n", err);
1220 static int libertas_spi_remove(struct spi_device *spi)
1222 struct if_spi_card *card = spi_get_drvdata(spi);
1223 struct lbs_private *priv = card->priv;
1225 lbs_deb_spi("libertas_spi_remove\n");
1226 lbs_deb_enter(LBS_DEB_SPI);
1228 cancel_work_sync(&card->resume_work);
1230 lbs_stop_card(priv);
1231 lbs_remove_card(priv); /* will call free_netdev */
1233 free_irq(spi->irq, card);
1234 destroy_workqueue(card->workqueue);
1235 if (card->pdata->teardown)
1236 card->pdata->teardown(spi);
1237 free_if_spi_card(card);
1238 lbs_deb_leave(LBS_DEB_SPI);
1242 static int if_spi_suspend(struct device *dev)
1244 struct spi_device *spi = to_spi_device(dev);
1245 struct if_spi_card *card = spi_get_drvdata(spi);
1247 if (!card->suspended) {
1248 lbs_suspend(card->priv);
1249 flush_workqueue(card->workqueue);
1250 disable_irq(spi->irq);
1252 if (card->pdata->teardown)
1253 card->pdata->teardown(spi);
1254 card->suspended = 1;
1260 static int if_spi_resume(struct device *dev)
1262 struct spi_device *spi = to_spi_device(dev);
1263 struct if_spi_card *card = spi_get_drvdata(spi);
1265 /* Schedule delayed work */
1266 schedule_work(&card->resume_work);
1271 static const struct dev_pm_ops if_spi_pm_ops = {
1272 .suspend = if_spi_suspend,
1273 .resume = if_spi_resume,
1276 static struct spi_driver libertas_spi_driver = {
1277 .probe = if_spi_probe,
1278 .remove = libertas_spi_remove,
1280 .name = "libertas_spi",
1281 .pm = &if_spi_pm_ops,
1289 static int __init if_spi_init_module(void)
1292 lbs_deb_enter(LBS_DEB_SPI);
1293 printk(KERN_INFO "libertas_spi: Libertas SPI driver\n");
1294 ret = spi_register_driver(&libertas_spi_driver);
1295 lbs_deb_leave(LBS_DEB_SPI);
1299 static void __exit if_spi_exit_module(void)
1301 lbs_deb_enter(LBS_DEB_SPI);
1302 spi_unregister_driver(&libertas_spi_driver);
1303 lbs_deb_leave(LBS_DEB_SPI);
1306 module_init(if_spi_init_module);
1307 module_exit(if_spi_exit_module);
1309 MODULE_DESCRIPTION("Libertas SPI WLAN Driver");
1310 MODULE_AUTHOR("Andrey Yurovsky <andrey@cozybit.com>, "
1311 "Colin McCabe <colin@cozybit.com>");
1312 MODULE_LICENSE("GPL");
1313 MODULE_ALIAS("spi:libertas_spi");