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>
36 struct spi_device *spi;
37 struct lbs_private *priv;
38 struct libertas_spi_platform_data *pdata;
40 char helper_fw_name[IF_SPI_FW_NAME_MAX];
41 char main_fw_name[IF_SPI_FW_NAME_MAX];
43 /* The card ID and card revision, as reported by the hardware. */
47 /* The last time that we initiated an SPU operation */
48 unsigned long prev_xfer_time;
51 unsigned long spu_port_delay;
52 unsigned long spu_reg_delay;
54 /* Handles all SPI communication (except for FW load) */
55 struct task_struct *spi_thread;
58 /* Used to wake up the spi_thread */
59 struct semaphore spi_ready;
60 struct semaphore spi_thread_terminated;
62 u8 cmd_buffer[IF_SPI_CMD_BUF_SIZE];
65 static void free_if_spi_card(struct if_spi_card *card)
67 spi_set_drvdata(card->spi, NULL);
71 static struct chip_ident chip_id_to_device_name[] = {
72 { .chip_id = 0x04, .name = 8385 },
73 { .chip_id = 0x0b, .name = 8686 },
77 * SPI Interface Unit Routines
79 * The SPU sits between the host and the WLAN module.
80 * All communication with the firmware is through SPU transactions.
82 * First we have to put a SPU register name on the bus. Then we can
83 * either read from or write to that register.
87 static void spu_transaction_init(struct if_spi_card *card)
89 if (!time_after(jiffies, card->prev_xfer_time + 1)) {
90 /* Unfortunately, the SPU requires a delay between successive
91 * transactions. If our last transaction was more than a jiffy
92 * ago, we have obviously already delayed enough.
93 * If not, we have to busy-wait to be on the safe side. */
98 static void spu_transaction_finish(struct if_spi_card *card)
100 card->prev_xfer_time = jiffies;
103 /* Write out a byte buffer to an SPI register,
104 * using a series of 16-bit transfers. */
105 static int spu_write(struct if_spi_card *card, u16 reg, const u8 *buf, int len)
108 __le16 reg_out = cpu_to_le16(reg | IF_SPI_WRITE_OPERATION_MASK);
109 struct spi_message m;
110 struct spi_transfer reg_trans;
111 struct spi_transfer data_trans;
113 spi_message_init(&m);
114 memset(®_trans, 0, sizeof(reg_trans));
115 memset(&data_trans, 0, sizeof(data_trans));
117 /* You must give an even number of bytes to the SPU, even if it
118 * doesn't care about the last one. */
121 spu_transaction_init(card);
123 /* write SPU register index */
124 reg_trans.tx_buf = ®_out;
125 reg_trans.len = sizeof(reg_out);
127 data_trans.tx_buf = buf;
128 data_trans.len = len;
130 spi_message_add_tail(®_trans, &m);
131 spi_message_add_tail(&data_trans, &m);
133 err = spi_sync(card->spi, &m);
134 spu_transaction_finish(card);
138 static inline int spu_write_u16(struct if_spi_card *card, u16 reg, u16 val)
142 buff = cpu_to_le16(val);
143 return spu_write(card, reg, (u8 *)&buff, sizeof(u16));
146 static inline int spu_reg_is_port_reg(u16 reg)
149 case IF_SPI_IO_RDWRPORT_REG:
150 case IF_SPI_CMD_RDWRPORT_REG:
151 case IF_SPI_DATA_RDWRPORT_REG:
158 static int spu_read(struct if_spi_card *card, u16 reg, u8 *buf, int len)
162 __le16 reg_out = cpu_to_le16(reg | IF_SPI_READ_OPERATION_MASK);
163 struct spi_message m;
164 struct spi_transfer reg_trans;
165 struct spi_transfer dummy_trans;
166 struct spi_transfer data_trans;
168 /* You must take an even number of bytes from the SPU, even if you
169 * don't care about the last one. */
172 spu_transaction_init(card);
174 spi_message_init(&m);
175 memset(®_trans, 0, sizeof(reg_trans));
176 memset(&dummy_trans, 0, sizeof(dummy_trans));
177 memset(&data_trans, 0, sizeof(data_trans));
179 /* write SPU register index */
180 reg_trans.tx_buf = ®_out;
181 reg_trans.len = sizeof(reg_out);
182 spi_message_add_tail(®_trans, &m);
184 delay = spu_reg_is_port_reg(reg) ? card->spu_port_delay :
186 if (card->use_dummy_writes) {
187 /* Clock in dummy cycles while the SPU fills the FIFO */
188 dummy_trans.len = delay / 8;
189 spi_message_add_tail(&dummy_trans, &m);
191 /* Busy-wait while the SPU fills the FIFO */
192 reg_trans.delay_usecs =
193 DIV_ROUND_UP((100 + (delay * 10)), 1000);
197 data_trans.rx_buf = buf;
198 data_trans.len = len;
199 spi_message_add_tail(&data_trans, &m);
201 err = spi_sync(card->spi, &m);
202 spu_transaction_finish(card);
206 /* Read 16 bits from an SPI register */
207 static inline int spu_read_u16(struct if_spi_card *card, u16 reg, u16 *val)
212 ret = spu_read(card, reg, (u8 *)&buf, sizeof(buf));
214 *val = le16_to_cpup(&buf);
218 /* Read 32 bits from an SPI register.
219 * The low 16 bits are read first. */
220 static int spu_read_u32(struct if_spi_card *card, u16 reg, u32 *val)
225 err = spu_read(card, reg, (u8 *)&buf, sizeof(buf));
227 *val = le32_to_cpup(&buf);
231 /* Keep reading 16 bits from an SPI register until you get the correct result.
233 * If mask = 0, the correct result is any non-zero number.
234 * If mask != 0, the correct result is any number where
235 * number & target_mask == target
237 * Returns -ETIMEDOUT if a second passes without the correct result. */
238 static int spu_wait_for_u16(struct if_spi_card *card, u16 reg,
239 u16 target_mask, u16 target)
242 unsigned long timeout = jiffies + 5*HZ;
245 err = spu_read_u16(card, reg, &val);
249 if ((val & target_mask) == target)
256 if (time_after(jiffies, timeout)) {
257 lbs_pr_err("%s: timeout with val=%02x, "
258 "target_mask=%02x, target=%02x\n",
259 __func__, val, target_mask, target);
265 /* Read 16 bits from an SPI register until you receive a specific value.
266 * Returns -ETIMEDOUT if a 4 tries pass without success. */
267 static int spu_wait_for_u32(struct if_spi_card *card, u32 reg, u32 target)
270 for (try = 0; try < 4; ++try) {
272 err = spu_read_u32(card, reg, &val);
282 static int spu_set_interrupt_mode(struct if_spi_card *card,
283 int suppress_host_int,
288 /* We can suppress a host interrupt by clearing the appropriate
289 * bit in the "host interrupt status mask" register */
290 if (suppress_host_int) {
291 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG, 0);
295 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG,
296 IF_SPI_HISM_TX_DOWNLOAD_RDY |
297 IF_SPI_HISM_RX_UPLOAD_RDY |
298 IF_SPI_HISM_CMD_DOWNLOAD_RDY |
299 IF_SPI_HISM_CARDEVENT |
300 IF_SPI_HISM_CMD_UPLOAD_RDY);
305 /* If auto-interrupts are on, the completion of certain transactions
306 * will trigger an interrupt automatically. If auto-interrupts
307 * are off, we need to set the "Card Interrupt Cause" register to
308 * trigger a card interrupt. */
310 err = spu_write_u16(card, IF_SPI_HOST_INT_CTRL_REG,
311 IF_SPI_HICT_TX_DOWNLOAD_OVER_AUTO |
312 IF_SPI_HICT_RX_UPLOAD_OVER_AUTO |
313 IF_SPI_HICT_CMD_DOWNLOAD_OVER_AUTO |
314 IF_SPI_HICT_CMD_UPLOAD_OVER_AUTO);
318 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG, 0);
325 static int spu_get_chip_revision(struct if_spi_card *card,
326 u16 *card_id, u8 *card_rev)
330 err = spu_read_u32(card, IF_SPI_DEVICEID_CTRL_REG, &dev_ctrl);
333 *card_id = IF_SPI_DEVICEID_CTRL_REG_TO_CARD_ID(dev_ctrl);
334 *card_rev = IF_SPI_DEVICEID_CTRL_REG_TO_CARD_REV(dev_ctrl);
338 static int spu_set_bus_mode(struct if_spi_card *card, u16 mode)
343 err = spu_write_u16(card, IF_SPI_SPU_BUS_MODE_REG, mode);
346 /* Check that we were able to read back what we just wrote. */
347 err = spu_read_u16(card, IF_SPI_SPU_BUS_MODE_REG, &rval);
350 if ((rval & 0xF) != mode) {
351 lbs_pr_err("Can't read bus mode register.\n");
357 static int spu_init(struct if_spi_card *card, int use_dummy_writes)
362 /* We have to start up in timed delay mode so that we can safely
363 * read the Delay Read Register. */
364 card->use_dummy_writes = 0;
365 err = spu_set_bus_mode(card,
366 IF_SPI_BUS_MODE_SPI_CLOCK_PHASE_RISING |
367 IF_SPI_BUS_MODE_DELAY_METHOD_TIMED |
368 IF_SPI_BUS_MODE_16_BIT_ADDRESS_16_BIT_DATA);
371 card->spu_port_delay = 1000;
372 card->spu_reg_delay = 1000;
373 err = spu_read_u32(card, IF_SPI_DELAY_READ_REG, &delay);
376 card->spu_port_delay = delay & 0x0000ffff;
377 card->spu_reg_delay = (delay & 0xffff0000) >> 16;
379 /* If dummy clock delay mode has been requested, switch to it now */
380 if (use_dummy_writes) {
381 card->use_dummy_writes = 1;
382 err = spu_set_bus_mode(card,
383 IF_SPI_BUS_MODE_SPI_CLOCK_PHASE_RISING |
384 IF_SPI_BUS_MODE_DELAY_METHOD_DUMMY_CLOCK |
385 IF_SPI_BUS_MODE_16_BIT_ADDRESS_16_BIT_DATA);
390 lbs_deb_spi("Initialized SPU unit. "
391 "spu_port_delay=0x%04lx, spu_reg_delay=0x%04lx\n",
392 card->spu_port_delay, card->spu_reg_delay);
400 static int if_spi_prog_helper_firmware(struct if_spi_card *card)
403 const struct firmware *firmware = NULL;
406 u8 temp[HELPER_FW_LOAD_CHUNK_SZ];
407 struct spi_device *spi = card->spi;
409 lbs_deb_enter(LBS_DEB_SPI);
411 err = spu_set_interrupt_mode(card, 1, 0);
414 /* Get helper firmware image */
415 err = request_firmware(&firmware, card->helper_fw_name, &spi->dev);
417 lbs_pr_err("request_firmware failed with err = %d\n", err);
420 bytes_remaining = firmware->size;
423 /* Load helper firmware image */
424 while (bytes_remaining > 0) {
425 /* Scratch pad 1 should contain the number of bytes we
426 * want to download to the firmware */
427 err = spu_write_u16(card, IF_SPI_SCRATCH_1_REG,
428 HELPER_FW_LOAD_CHUNK_SZ);
430 goto release_firmware;
432 err = spu_wait_for_u16(card, IF_SPI_HOST_INT_STATUS_REG,
433 IF_SPI_HIST_CMD_DOWNLOAD_RDY,
434 IF_SPI_HIST_CMD_DOWNLOAD_RDY);
436 goto release_firmware;
438 /* Feed the data into the command read/write port reg
439 * in chunks of 64 bytes */
440 memset(temp, 0, sizeof(temp));
442 min(bytes_remaining, HELPER_FW_LOAD_CHUNK_SZ));
444 err = spu_write(card, IF_SPI_CMD_RDWRPORT_REG,
445 temp, HELPER_FW_LOAD_CHUNK_SZ);
447 goto release_firmware;
449 /* Interrupt the boot code */
450 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
452 goto release_firmware;
453 err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG,
454 IF_SPI_CIC_CMD_DOWNLOAD_OVER);
456 goto release_firmware;
457 bytes_remaining -= HELPER_FW_LOAD_CHUNK_SZ;
458 fw += HELPER_FW_LOAD_CHUNK_SZ;
461 /* Once the helper / single stage firmware download is complete,
462 * write 0 to scratch pad 1 and interrupt the
463 * bootloader. This completes the helper download. */
464 err = spu_write_u16(card, IF_SPI_SCRATCH_1_REG, FIRMWARE_DNLD_OK);
466 goto release_firmware;
467 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
469 goto release_firmware;
470 err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG,
471 IF_SPI_CIC_CMD_DOWNLOAD_OVER);
472 goto release_firmware;
474 lbs_deb_spi("waiting for helper to boot...\n");
477 release_firmware(firmware);
480 lbs_pr_err("failed to load helper firmware (err=%d)\n", err);
481 lbs_deb_leave_args(LBS_DEB_SPI, "err %d", err);
485 /* Returns the length of the next packet the firmware expects us to send
486 * Sets crc_err if the previous transfer had a CRC error. */
487 static int if_spi_prog_main_firmware_check_len(struct if_spi_card *card,
493 /* wait until the host interrupt status register indicates
494 * that we are ready to download */
495 err = spu_wait_for_u16(card, IF_SPI_HOST_INT_STATUS_REG,
496 IF_SPI_HIST_CMD_DOWNLOAD_RDY,
497 IF_SPI_HIST_CMD_DOWNLOAD_RDY);
499 lbs_pr_err("timed out waiting for host_int_status\n");
503 /* Ask the device how many bytes of firmware it wants. */
504 err = spu_read_u16(card, IF_SPI_SCRATCH_1_REG, &len);
508 if (len > IF_SPI_CMD_BUF_SIZE) {
509 lbs_pr_err("firmware load device requested a larger "
510 "tranfer than we are prepared to "
511 "handle. (len = %d)\n", len);
515 lbs_deb_spi("%s: crc error\n", __func__);
524 static int if_spi_prog_main_firmware(struct if_spi_card *card)
527 int bytes, crc_err = 0, err = 0;
528 const struct firmware *firmware = NULL;
530 struct spi_device *spi = card->spi;
533 lbs_deb_enter(LBS_DEB_SPI);
535 err = spu_set_interrupt_mode(card, 1, 0);
539 /* Get firmware image */
540 err = request_firmware(&firmware, card->main_fw_name, &spi->dev);
542 lbs_pr_err("%s: can't get firmware '%s' from kernel. "
543 "err = %d\n", __func__, card->main_fw_name, err);
547 err = spu_wait_for_u16(card, IF_SPI_SCRATCH_1_REG, 0, 0);
549 lbs_pr_err("%s: timed out waiting for initial "
550 "scratch reg = 0\n", __func__);
551 goto release_firmware;
556 bytes = firmware->size;
558 while ((len = if_spi_prog_main_firmware_check_len(card, &crc_err))) {
561 goto release_firmware;
564 /* If there are no more bytes left, we would normally
565 * expect to have terminated with len = 0 */
566 lbs_pr_err("Firmware load wants more bytes "
567 "than we have to offer.\n");
571 /* Previous transfer failed. */
572 if (++num_crc_errs > MAX_MAIN_FW_LOAD_CRC_ERR) {
573 lbs_pr_err("Too many CRC errors encountered "
574 "in firmware load.\n");
576 goto release_firmware;
579 /* Previous transfer succeeded. Advance counters. */
584 memset(card->cmd_buffer, 0, len);
585 memcpy(card->cmd_buffer, fw, bytes);
587 memcpy(card->cmd_buffer, fw, len);
589 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
591 goto release_firmware;
592 err = spu_write(card, IF_SPI_CMD_RDWRPORT_REG,
593 card->cmd_buffer, len);
595 goto release_firmware;
596 err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG ,
597 IF_SPI_CIC_CMD_DOWNLOAD_OVER);
599 goto release_firmware;
602 if (bytes > prev_len) {
603 lbs_pr_err("firmware load wants fewer bytes than "
604 "we have to offer.\n");
607 /* Confirm firmware download */
608 err = spu_wait_for_u32(card, IF_SPI_SCRATCH_4_REG,
609 SUCCESSFUL_FW_DOWNLOAD_MAGIC);
611 lbs_pr_err("failed to confirm the firmware download\n");
612 goto release_firmware;
616 release_firmware(firmware);
620 lbs_pr_err("failed to load firmware (err=%d)\n", err);
621 lbs_deb_leave_args(LBS_DEB_SPI, "err %d", err);
626 * SPI Transfer Thread
628 * The SPI thread handles all SPI transfers, so there is no need for a lock.
631 /* Move a command from the card to the host */
632 static int if_spi_c2h_cmd(struct if_spi_card *card)
634 struct lbs_private *priv = card->priv;
640 /* We need a buffer big enough to handle whatever people send to
642 BUILD_BUG_ON(IF_SPI_CMD_BUF_SIZE < LBS_CMD_BUFFER_SIZE);
643 BUILD_BUG_ON(IF_SPI_CMD_BUF_SIZE < LBS_UPLD_SIZE);
645 /* It's just annoying if the buffer size isn't a multiple of 4, because
646 * then we might have len < IF_SPI_CMD_BUF_SIZE but
647 * ALIGN(len, 4) > IF_SPI_CMD_BUF_SIZE */
648 BUILD_BUG_ON(IF_SPI_CMD_BUF_SIZE % 4 != 0);
650 lbs_deb_enter(LBS_DEB_SPI);
652 /* How many bytes are there to read? */
653 err = spu_read_u16(card, IF_SPI_SCRATCH_2_REG, &len);
657 lbs_pr_err("%s: error: card has no data for host\n",
661 } else if (len > IF_SPI_CMD_BUF_SIZE) {
662 lbs_pr_err("%s: error: response packet too large: "
663 "%d bytes, but maximum is %d\n",
664 __func__, len, IF_SPI_CMD_BUF_SIZE);
669 /* Read the data from the WLAN module into our command buffer */
670 err = spu_read(card, IF_SPI_CMD_RDWRPORT_REG,
671 card->cmd_buffer, ALIGN(len, 4));
675 spin_lock_irqsave(&priv->driver_lock, flags);
676 i = (priv->resp_idx == 0) ? 1 : 0;
677 BUG_ON(priv->resp_len[i]);
678 priv->resp_len[i] = len;
679 memcpy(priv->resp_buf[i], card->cmd_buffer, len);
680 lbs_notify_command_response(priv, i);
681 spin_unlock_irqrestore(&priv->driver_lock, flags);
685 lbs_pr_err("%s: err=%d\n", __func__, err);
686 lbs_deb_leave(LBS_DEB_SPI);
690 /* Move data from the card to the host */
691 static int if_spi_c2h_data(struct if_spi_card *card)
698 lbs_deb_enter(LBS_DEB_SPI);
700 /* How many bytes are there to read? */
701 err = spu_read_u16(card, IF_SPI_SCRATCH_1_REG, &len);
705 lbs_pr_err("%s: error: card has no data for host\n",
709 } else if (len > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
710 lbs_pr_err("%s: error: card has %d bytes of data, but "
711 "our maximum skb size is %zu\n",
712 __func__, len, MRVDRV_ETH_RX_PACKET_BUFFER_SIZE);
717 /* TODO: should we allocate a smaller skb if we have less data? */
718 skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE);
723 skb_reserve(skb, IPFIELD_ALIGN_OFFSET);
724 data = skb_put(skb, len);
726 /* Read the data from the WLAN module into our skb... */
727 err = spu_read(card, IF_SPI_DATA_RDWRPORT_REG, data, ALIGN(len, 4));
731 /* pass the SKB to libertas */
732 err = lbs_process_rxed_packet(card->priv, skb);
743 lbs_pr_err("%s: err=%d\n", __func__, err);
744 lbs_deb_leave(LBS_DEB_SPI);
748 /* Inform the host about a card event */
749 static void if_spi_e2h(struct if_spi_card *card)
753 struct lbs_private *priv = card->priv;
755 err = spu_read_u32(card, IF_SPI_SCRATCH_3_REG, &cause);
759 /* re-enable the card event interrupt */
760 spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG,
761 ~IF_SPI_HICU_CARD_EVENT);
763 /* generate a card interrupt */
764 spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG, IF_SPI_CIC_HOST_EVENT);
766 lbs_queue_event(priv, cause & 0xff);
769 lbs_pr_err("%s: error %d\n", __func__, err);
772 static int lbs_spi_thread(void *data)
775 struct if_spi_card *card = data;
779 /* Wait to be woken up by one of two things. First, our ISR
780 * could tell us that something happened on the WLAN.
781 * Secondly, libertas could call hw_host_to_card with more
782 * data, which we might be able to send.
785 err = down_interruptible(&card->spi_ready);
786 if (!card->run_thread) {
787 up(&card->spi_thread_terminated);
790 } while (err == EINTR);
792 /* Read the host interrupt status register to see what we
794 err = spu_read_u16(card, IF_SPI_HOST_INT_STATUS_REG,
797 lbs_pr_err("I/O error\n");
801 if (hiStatus & IF_SPI_HIST_CMD_UPLOAD_RDY)
802 err = if_spi_c2h_cmd(card);
805 if (hiStatus & IF_SPI_HIST_RX_UPLOAD_RDY)
806 err = if_spi_c2h_data(card);
810 /* workaround: in PS mode, the card does not set the Command
811 * Download Ready bit, but it sets TX Download Ready. */
812 if (hiStatus & IF_SPI_HIST_CMD_DOWNLOAD_RDY ||
813 (card->priv->psstate != PS_STATE_FULL_POWER &&
814 (hiStatus & IF_SPI_HIST_TX_DOWNLOAD_RDY))) {
815 lbs_host_to_card_done(card->priv);
818 if (hiStatus & IF_SPI_HIST_CARD_EVENT)
823 lbs_pr_err("%s: got error %d\n", __func__, err);
827 /* Block until lbs_spi_thread thread has terminated */
828 static void if_spi_terminate_spi_thread(struct if_spi_card *card)
830 /* It would be nice to use kthread_stop here, but that function
831 * can't wake threads waiting for a semaphore. */
832 card->run_thread = 0;
833 up(&card->spi_ready);
834 down(&card->spi_thread_terminated);
840 * Called from Libertas to transfer some data to the WLAN device
841 * We can't sleep here. */
842 static int if_spi_host_to_card(struct lbs_private *priv,
843 u8 type, u8 *buf, u16 nb)
846 struct if_spi_card *card = priv->card;
848 lbs_deb_enter_args(LBS_DEB_SPI, "type %d, bytes %d", type, nb);
854 err = spu_write(card, IF_SPI_CMD_RDWRPORT_REG, buf, nb);
857 err = spu_write(card, IF_SPI_DATA_RDWRPORT_REG, buf, nb);
860 lbs_pr_err("can't transfer buffer of type %d", type);
865 lbs_deb_leave_args(LBS_DEB_SPI, "err=%d", err);
872 * Service incoming interrupts from the WLAN device. We can't sleep here, so
873 * don't try to talk on the SPI bus, just wake up the SPI thread.
875 static irqreturn_t if_spi_host_interrupt(int irq, void *dev_id)
877 struct if_spi_card *card = dev_id;
879 up(&card->spi_ready);
887 static int if_spi_calculate_fw_names(u16 card_id,
888 char *helper_fw, char *main_fw)
891 for (i = 0; i < ARRAY_SIZE(chip_id_to_device_name); ++i) {
892 if (card_id == chip_id_to_device_name[i].chip_id)
895 if (i == ARRAY_SIZE(chip_id_to_device_name)) {
896 lbs_pr_err("Unsupported chip_id: 0x%02x\n", card_id);
897 return -EAFNOSUPPORT;
899 snprintf(helper_fw, IF_SPI_FW_NAME_MAX, "libertas/gspi%d_hlp.bin",
900 chip_id_to_device_name[i].name);
901 snprintf(main_fw, IF_SPI_FW_NAME_MAX, "libertas/gspi%d.bin",
902 chip_id_to_device_name[i].name);
905 MODULE_FIRMWARE("libertas/gspi8385_hlp.bin");
906 MODULE_FIRMWARE("libertas/gspi8385.bin");
907 MODULE_FIRMWARE("libertas/gspi8686_hlp.bin");
908 MODULE_FIRMWARE("libertas/gspi8686.bin");
910 static int __devinit if_spi_probe(struct spi_device *spi)
912 struct if_spi_card *card;
913 struct lbs_private *priv = NULL;
914 struct libertas_spi_platform_data *pdata = spi->dev.platform_data;
917 struct sched_param param = { .sched_priority = 1 };
919 lbs_deb_enter(LBS_DEB_SPI);
927 err = pdata->setup(spi);
932 /* Allocate card structure to represent this specific device */
933 card = kzalloc(sizeof(struct if_spi_card), GFP_KERNEL);
938 spi_set_drvdata(spi, card);
941 card->prev_xfer_time = jiffies;
943 sema_init(&card->spi_ready, 0);
944 sema_init(&card->spi_thread_terminated, 0);
946 /* Initialize the SPI Interface Unit */
947 err = spu_init(card, pdata->use_dummy_writes);
950 err = spu_get_chip_revision(card, &card->card_id, &card->card_rev);
955 err = spu_read_u32(card, IF_SPI_SCRATCH_4_REG, &scratch);
958 if (scratch == SUCCESSFUL_FW_DOWNLOAD_MAGIC)
959 lbs_deb_spi("Firmware is already loaded for "
960 "Marvell WLAN 802.11 adapter\n");
962 err = if_spi_calculate_fw_names(card->card_id,
963 card->helper_fw_name, card->main_fw_name);
967 lbs_deb_spi("Initializing FW for Marvell WLAN 802.11 adapter "
968 "(chip_id = 0x%04x, chip_rev = 0x%02x) "
969 "attached to SPI bus_num %d, chip_select %d. "
970 "spi->max_speed_hz=%d\n",
971 card->card_id, card->card_rev,
972 spi->master->bus_num, spi->chip_select,
974 err = if_spi_prog_helper_firmware(card);
977 err = if_spi_prog_main_firmware(card);
980 lbs_deb_spi("loaded FW for Marvell WLAN 802.11 adapter\n");
983 err = spu_set_interrupt_mode(card, 0, 1);
987 /* Register our card with libertas.
988 * This will call alloc_etherdev */
989 priv = lbs_add_card(card, &spi->dev);
996 priv->hw_host_to_card = if_spi_host_to_card;
997 priv->enter_deep_sleep = NULL;
998 priv->exit_deep_sleep = NULL;
999 priv->reset_deep_sleep_wakeup = NULL;
1002 /* Initialize interrupt handling stuff. */
1003 card->run_thread = 1;
1004 card->spi_thread = kthread_run(lbs_spi_thread, card, "lbs_spi_thread");
1005 if (IS_ERR(card->spi_thread)) {
1006 card->run_thread = 0;
1007 err = PTR_ERR(card->spi_thread);
1008 lbs_pr_err("error creating SPI thread: err=%d\n", err);
1011 if (sched_setscheduler(card->spi_thread, SCHED_FIFO, ¶m))
1012 lbs_pr_err("Error setting scheduler, using default.\n");
1014 err = request_irq(spi->irq, if_spi_host_interrupt,
1015 IRQF_TRIGGER_FALLING, "libertas_spi", card);
1017 lbs_pr_err("can't get host irq line-- request_irq failed\n");
1018 goto terminate_thread;
1021 /* poke the IRQ handler so that we don't miss the first interrupt */
1022 up(&card->spi_ready);
1025 * This will call register_netdev, and we'll start
1026 * getting interrupts... */
1027 err = lbs_start_card(priv);
1031 lbs_deb_spi("Finished initializing WLAN module.\n");
1033 /* successful exit */
1037 free_irq(spi->irq, card);
1039 if_spi_terminate_spi_thread(card);
1041 lbs_remove_card(priv); /* will call free_netdev */
1043 free_if_spi_card(card);
1045 lbs_deb_leave_args(LBS_DEB_SPI, "err %d\n", err);
1049 static int __devexit libertas_spi_remove(struct spi_device *spi)
1051 struct if_spi_card *card = spi_get_drvdata(spi);
1052 struct lbs_private *priv = card->priv;
1054 lbs_deb_spi("libertas_spi_remove\n");
1055 lbs_deb_enter(LBS_DEB_SPI);
1057 lbs_stop_card(priv);
1058 lbs_remove_card(priv); /* will call free_netdev */
1060 priv->surpriseremoved = 1;
1061 free_irq(spi->irq, card);
1062 if_spi_terminate_spi_thread(card);
1063 if (card->pdata->teardown)
1064 card->pdata->teardown(spi);
1065 free_if_spi_card(card);
1066 lbs_deb_leave(LBS_DEB_SPI);
1070 static struct spi_driver libertas_spi_driver = {
1071 .probe = if_spi_probe,
1072 .remove = __devexit_p(libertas_spi_remove),
1074 .name = "libertas_spi",
1075 .bus = &spi_bus_type,
1076 .owner = THIS_MODULE,
1084 static int __init if_spi_init_module(void)
1087 lbs_deb_enter(LBS_DEB_SPI);
1088 printk(KERN_INFO "libertas_spi: Libertas SPI driver\n");
1089 ret = spi_register_driver(&libertas_spi_driver);
1090 lbs_deb_leave(LBS_DEB_SPI);
1094 static void __exit if_spi_exit_module(void)
1096 lbs_deb_enter(LBS_DEB_SPI);
1097 spi_unregister_driver(&libertas_spi_driver);
1098 lbs_deb_leave(LBS_DEB_SPI);
1101 module_init(if_spi_init_module);
1102 module_exit(if_spi_exit_module);
1104 MODULE_DESCRIPTION("Libertas SPI WLAN Driver");
1105 MODULE_AUTHOR("Andrey Yurovsky <andrey@cozybit.com>, "
1106 "Colin McCabe <colin@cozybit.com>");
1107 MODULE_LICENSE("GPL");
1108 MODULE_ALIAS("spi:libertas_spi");