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/gpio.h>
23 #include <linux/jiffies.h>
24 #include <linux/kthread.h>
25 #include <linux/list.h>
26 #include <linux/netdevice.h>
27 #include <linux/spi/libertas_spi.h>
28 #include <linux/spi/spi.h>
36 struct if_spi_packet
{
37 struct list_head list
;
39 u8 buffer
[0] __attribute__((aligned(4)));
43 struct spi_device
*spi
;
44 struct lbs_private
*priv
;
45 struct libertas_spi_platform_data
*pdata
;
47 char helper_fw_name
[FIRMWARE_NAME_MAX
];
48 char main_fw_name
[FIRMWARE_NAME_MAX
];
50 /* The card ID and card revision, as reported by the hardware. */
54 /* Pin number for our GPIO chip-select. */
55 /* TODO: Once the generic SPI layer has some additional features, we
56 * should take this out and use the normal chip select here.
57 * We need support for chip select delays, and not dropping chipselect
61 /* The last time that we initiated an SPU operation */
62 unsigned long prev_xfer_time
;
65 unsigned long spu_port_delay
;
66 unsigned long spu_reg_delay
;
68 /* Handles all SPI communication (except for FW load) */
69 struct task_struct
*spi_thread
;
72 /* Used to wake up the spi_thread */
73 struct semaphore spi_ready
;
74 struct semaphore spi_thread_terminated
;
76 u8 cmd_buffer
[IF_SPI_CMD_BUF_SIZE
];
78 /* A buffer of incoming packets from libertas core.
79 * Since we can't sleep in hw_host_to_card, we have to buffer
81 struct list_head cmd_packet_list
;
82 struct list_head data_packet_list
;
84 /* Protects cmd_packet_list and data_packet_list */
85 spinlock_t buffer_lock
;
88 static void free_if_spi_card(struct if_spi_card
*card
)
90 struct list_head
*cursor
, *next
;
91 struct if_spi_packet
*packet
;
93 BUG_ON(card
->run_thread
);
94 list_for_each_safe(cursor
, next
, &card
->cmd_packet_list
) {
95 packet
= container_of(cursor
, struct if_spi_packet
, list
);
96 list_del(&packet
->list
);
99 list_for_each_safe(cursor
, next
, &card
->data_packet_list
) {
100 packet
= container_of(cursor
, struct if_spi_packet
, list
);
101 list_del(&packet
->list
);
104 spi_set_drvdata(card
->spi
, NULL
);
108 static struct chip_ident chip_id_to_device_name
[] = {
109 { .chip_id
= 0x04, .name
= 8385 },
110 { .chip_id
= 0x0b, .name
= 8686 },
114 * SPI Interface Unit Routines
116 * The SPU sits between the host and the WLAN module.
117 * All communication with the firmware is through SPU transactions.
119 * First we have to put a SPU register name on the bus. Then we can
120 * either read from or write to that register.
122 * For 16-bit transactions, byte order on the bus is big-endian.
123 * We don't have to worry about that here, though.
124 * The translation takes place in the SPI routines.
127 static void spu_transaction_init(struct if_spi_card
*card
)
129 if (!time_after(jiffies
, card
->prev_xfer_time
+ 1)) {
130 /* Unfortunately, the SPU requires a delay between successive
131 * transactions. If our last transaction was more than a jiffy
132 * ago, we have obviously already delayed enough.
133 * If not, we have to busy-wait to be on the safe side. */
136 gpio_set_value(card
->gpio_cs
, 0); /* assert CS */
139 static void spu_transaction_finish(struct if_spi_card
*card
)
141 gpio_set_value(card
->gpio_cs
, 1); /* drop CS */
142 card
->prev_xfer_time
= jiffies
;
145 /* Write out a byte buffer to an SPI register,
146 * using a series of 16-bit transfers. */
147 static int spu_write(struct if_spi_card
*card
, u16 reg
, const u8
*buf
, int len
)
150 u16 reg_out
= reg
| IF_SPI_WRITE_OPERATION_MASK
;
152 /* You must give an even number of bytes to the SPU, even if it
153 * doesn't care about the last one. */
156 spu_transaction_init(card
);
158 /* write SPU register index */
159 err
= spi_write(card
->spi
, (u8
*)®_out
, sizeof(u16
));
163 err
= spi_write(card
->spi
, buf
, len
);
166 spu_transaction_finish(card
);
170 static inline int spu_write_u16(struct if_spi_card
*card
, u16 reg
, u16 val
)
172 return spu_write(card
, reg
, (u8
*)&val
, sizeof(u16
));
175 static inline int spu_write_u32(struct if_spi_card
*card
, u16 reg
, u32 val
)
177 /* The lower 16 bits are written first. */
179 out
[0] = val
& 0xffff;
180 out
[1] = (val
& 0xffff0000) >> 16;
181 return spu_write(card
, reg
, (u8
*)&out
, sizeof(u32
));
184 static inline int spu_reg_is_port_reg(u16 reg
)
187 case IF_SPI_IO_RDWRPORT_REG
:
188 case IF_SPI_CMD_RDWRPORT_REG
:
189 case IF_SPI_DATA_RDWRPORT_REG
:
196 static int spu_read(struct if_spi_card
*card
, u16 reg
, u8
*buf
, int len
)
198 unsigned int i
, delay
;
201 u16 reg_out
= reg
| IF_SPI_READ_OPERATION_MASK
;
203 /* You must take an even number of bytes from the SPU, even if you
204 * don't care about the last one. */
207 spu_transaction_init(card
);
209 /* write SPU register index */
210 err
= spi_write(card
->spi
, (u8
*)®_out
, sizeof(u16
));
214 delay
= spu_reg_is_port_reg(reg
) ? card
->spu_port_delay
:
216 if (card
->use_dummy_writes
) {
217 /* Clock in dummy cycles while the SPU fills the FIFO */
218 for (i
= 0; i
< delay
/ 16; ++i
) {
219 err
= spi_write(card
->spi
, (u8
*)&zero
, sizeof(u16
));
224 /* Busy-wait while the SPU fills the FIFO */
225 ndelay(100 + (delay
* 10));
229 err
= spi_read(card
->spi
, buf
, len
);
232 spu_transaction_finish(card
);
236 /* Read 16 bits from an SPI register */
237 static inline int spu_read_u16(struct if_spi_card
*card
, u16 reg
, u16
*val
)
239 return spu_read(card
, reg
, (u8
*)val
, sizeof(u16
));
242 /* Read 32 bits from an SPI register.
243 * The low 16 bits are read first. */
244 static int spu_read_u32(struct if_spi_card
*card
, u16 reg
, u32
*val
)
248 err
= spu_read(card
, reg
, (u8
*)buf
, sizeof(u32
));
250 *val
= buf
[0] | (buf
[1] << 16);
254 /* Keep reading 16 bits from an SPI register until you get the correct result.
256 * If mask = 0, the correct result is any non-zero number.
257 * If mask != 0, the correct result is any number where
258 * number & target_mask == target
260 * Returns -ETIMEDOUT if a second passes without the correct result. */
261 static int spu_wait_for_u16(struct if_spi_card
*card
, u16 reg
,
262 u16 target_mask
, u16 target
)
265 unsigned long timeout
= jiffies
+ 5*HZ
;
268 err
= spu_read_u16(card
, reg
, &val
);
272 if ((val
& target_mask
) == target
)
279 if (time_after(jiffies
, timeout
)) {
280 lbs_pr_err("%s: timeout with val=%02x, "
281 "target_mask=%02x, target=%02x\n",
282 __func__
, val
, target_mask
, target
);
288 /* Read 16 bits from an SPI register until you receive a specific value.
289 * Returns -ETIMEDOUT if a 4 tries pass without success. */
290 static int spu_wait_for_u32(struct if_spi_card
*card
, u32 reg
, u32 target
)
293 for (try = 0; try < 4; ++try) {
295 err
= spu_read_u32(card
, reg
, &val
);
305 static int spu_set_interrupt_mode(struct if_spi_card
*card
,
306 int suppress_host_int
,
311 /* We can suppress a host interrupt by clearing the appropriate
312 * bit in the "host interrupt status mask" register */
313 if (suppress_host_int
) {
314 err
= spu_write_u16(card
, IF_SPI_HOST_INT_STATUS_MASK_REG
, 0);
318 err
= spu_write_u16(card
, IF_SPI_HOST_INT_STATUS_MASK_REG
,
319 IF_SPI_HISM_TX_DOWNLOAD_RDY
|
320 IF_SPI_HISM_RX_UPLOAD_RDY
|
321 IF_SPI_HISM_CMD_DOWNLOAD_RDY
|
322 IF_SPI_HISM_CARDEVENT
|
323 IF_SPI_HISM_CMD_UPLOAD_RDY
);
328 /* If auto-interrupts are on, the completion of certain transactions
329 * will trigger an interrupt automatically. If auto-interrupts
330 * are off, we need to set the "Card Interrupt Cause" register to
331 * trigger a card interrupt. */
333 err
= spu_write_u16(card
, IF_SPI_HOST_INT_CTRL_REG
,
334 IF_SPI_HICT_TX_DOWNLOAD_OVER_AUTO
|
335 IF_SPI_HICT_RX_UPLOAD_OVER_AUTO
|
336 IF_SPI_HICT_CMD_DOWNLOAD_OVER_AUTO
|
337 IF_SPI_HICT_CMD_UPLOAD_OVER_AUTO
);
341 err
= spu_write_u16(card
, IF_SPI_HOST_INT_STATUS_MASK_REG
, 0);
348 static int spu_get_chip_revision(struct if_spi_card
*card
,
349 u16
*card_id
, u8
*card_rev
)
353 err
= spu_read_u32(card
, IF_SPI_DEVICEID_CTRL_REG
, &dev_ctrl
);
356 *card_id
= IF_SPI_DEVICEID_CTRL_REG_TO_CARD_ID(dev_ctrl
);
357 *card_rev
= IF_SPI_DEVICEID_CTRL_REG_TO_CARD_REV(dev_ctrl
);
361 static int spu_set_bus_mode(struct if_spi_card
*card
, u16 mode
)
366 err
= spu_write_u16(card
, IF_SPI_SPU_BUS_MODE_REG
, mode
);
369 /* Check that we were able to read back what we just wrote. */
370 err
= spu_read_u16(card
, IF_SPI_SPU_BUS_MODE_REG
, &rval
);
374 lbs_pr_err("Can't read bus mode register.\n");
380 static int spu_init(struct if_spi_card
*card
, int use_dummy_writes
)
385 /* We have to start up in timed delay mode so that we can safely
386 * read the Delay Read Register. */
387 card
->use_dummy_writes
= 0;
388 err
= spu_set_bus_mode(card
,
389 IF_SPI_BUS_MODE_SPI_CLOCK_PHASE_RISING
|
390 IF_SPI_BUS_MODE_DELAY_METHOD_TIMED
|
391 IF_SPI_BUS_MODE_16_BIT_ADDRESS_16_BIT_DATA
);
394 card
->spu_port_delay
= 1000;
395 card
->spu_reg_delay
= 1000;
396 err
= spu_read_u32(card
, IF_SPI_DELAY_READ_REG
, &delay
);
399 card
->spu_port_delay
= delay
& 0x0000ffff;
400 card
->spu_reg_delay
= (delay
& 0xffff0000) >> 16;
402 /* If dummy clock delay mode has been requested, switch to it now */
403 if (use_dummy_writes
) {
404 card
->use_dummy_writes
= 1;
405 err
= spu_set_bus_mode(card
,
406 IF_SPI_BUS_MODE_SPI_CLOCK_PHASE_RISING
|
407 IF_SPI_BUS_MODE_DELAY_METHOD_DUMMY_CLOCK
|
408 IF_SPI_BUS_MODE_16_BIT_ADDRESS_16_BIT_DATA
);
413 lbs_deb_spi("Initialized SPU unit. "
414 "spu_port_delay=0x%04lx, spu_reg_delay=0x%04lx\n",
415 card
->spu_port_delay
, card
->spu_reg_delay
);
423 static int if_spi_prog_helper_firmware(struct if_spi_card
*card
)
426 const struct firmware
*firmware
= NULL
;
429 u8 temp
[HELPER_FW_LOAD_CHUNK_SZ
];
430 struct spi_device
*spi
= card
->spi
;
432 lbs_deb_enter(LBS_DEB_SPI
);
434 err
= spu_set_interrupt_mode(card
, 1, 0);
437 /* Get helper firmware image */
438 err
= request_firmware(&firmware
, card
->helper_fw_name
, &spi
->dev
);
440 lbs_pr_err("request_firmware failed with err = %d\n", err
);
443 bytes_remaining
= firmware
->size
;
446 /* Load helper firmware image */
447 while (bytes_remaining
> 0) {
448 /* Scratch pad 1 should contain the number of bytes we
449 * want to download to the firmware */
450 err
= spu_write_u16(card
, IF_SPI_SCRATCH_1_REG
,
451 HELPER_FW_LOAD_CHUNK_SZ
);
453 goto release_firmware
;
455 err
= spu_wait_for_u16(card
, IF_SPI_HOST_INT_STATUS_REG
,
456 IF_SPI_HIST_CMD_DOWNLOAD_RDY
,
457 IF_SPI_HIST_CMD_DOWNLOAD_RDY
);
459 goto release_firmware
;
461 /* Feed the data into the command read/write port reg
462 * in chunks of 64 bytes */
463 memset(temp
, 0, sizeof(temp
));
465 min(bytes_remaining
, HELPER_FW_LOAD_CHUNK_SZ
));
467 err
= spu_write(card
, IF_SPI_CMD_RDWRPORT_REG
,
468 temp
, HELPER_FW_LOAD_CHUNK_SZ
);
470 goto release_firmware
;
472 /* Interrupt the boot code */
473 err
= spu_write_u16(card
, IF_SPI_HOST_INT_STATUS_REG
, 0);
475 goto release_firmware
;
476 err
= spu_write_u16(card
, IF_SPI_CARD_INT_CAUSE_REG
,
477 IF_SPI_CIC_CMD_DOWNLOAD_OVER
);
479 goto release_firmware
;
480 bytes_remaining
-= HELPER_FW_LOAD_CHUNK_SZ
;
481 fw
+= HELPER_FW_LOAD_CHUNK_SZ
;
484 /* Once the helper / single stage firmware download is complete,
485 * write 0 to scratch pad 1 and interrupt the
486 * bootloader. This completes the helper download. */
487 err
= spu_write_u16(card
, IF_SPI_SCRATCH_1_REG
, FIRMWARE_DNLD_OK
);
489 goto release_firmware
;
490 err
= spu_write_u16(card
, IF_SPI_HOST_INT_STATUS_REG
, 0);
492 goto release_firmware
;
493 err
= spu_write_u16(card
, IF_SPI_CARD_INT_CAUSE_REG
,
494 IF_SPI_CIC_CMD_DOWNLOAD_OVER
);
495 goto release_firmware
;
497 lbs_deb_spi("waiting for helper to boot...\n");
500 release_firmware(firmware
);
503 lbs_pr_err("failed to load helper firmware (err=%d)\n", err
);
504 lbs_deb_leave_args(LBS_DEB_SPI
, "err %d", err
);
508 /* Returns the length of the next packet the firmware expects us to send
509 * Sets crc_err if the previous transfer had a CRC error. */
510 static int if_spi_prog_main_firmware_check_len(struct if_spi_card
*card
,
516 /* wait until the host interrupt status register indicates
517 * that we are ready to download */
518 err
= spu_wait_for_u16(card
, IF_SPI_HOST_INT_STATUS_REG
,
519 IF_SPI_HIST_CMD_DOWNLOAD_RDY
,
520 IF_SPI_HIST_CMD_DOWNLOAD_RDY
);
522 lbs_pr_err("timed out waiting for host_int_status\n");
526 /* Ask the device how many bytes of firmware it wants. */
527 err
= spu_read_u16(card
, IF_SPI_SCRATCH_1_REG
, &len
);
531 if (len
> IF_SPI_CMD_BUF_SIZE
) {
532 lbs_pr_err("firmware load device requested a larger "
533 "tranfer than we are prepared to "
534 "handle. (len = %d)\n", len
);
538 lbs_deb_spi("%s: crc error\n", __func__
);
547 static int if_spi_prog_main_firmware(struct if_spi_card
*card
)
550 int bytes
, crc_err
= 0, err
= 0;
551 const struct firmware
*firmware
= NULL
;
553 struct spi_device
*spi
= card
->spi
;
556 lbs_deb_enter(LBS_DEB_SPI
);
558 err
= spu_set_interrupt_mode(card
, 1, 0);
562 /* Get firmware image */
563 err
= request_firmware(&firmware
, card
->main_fw_name
, &spi
->dev
);
565 lbs_pr_err("%s: can't get firmware '%s' from kernel. "
566 "err = %d\n", __func__
, card
->main_fw_name
, err
);
570 err
= spu_wait_for_u16(card
, IF_SPI_SCRATCH_1_REG
, 0, 0);
572 lbs_pr_err("%s: timed out waiting for initial "
573 "scratch reg = 0\n", __func__
);
574 goto release_firmware
;
579 bytes
= firmware
->size
;
581 while ((len
= if_spi_prog_main_firmware_check_len(card
, &crc_err
))) {
584 goto release_firmware
;
587 /* If there are no more bytes left, we would normally
588 * expect to have terminated with len = 0 */
589 lbs_pr_err("Firmware load wants more bytes "
590 "than we have to offer.\n");
594 /* Previous transfer failed. */
595 if (++num_crc_errs
> MAX_MAIN_FW_LOAD_CRC_ERR
) {
596 lbs_pr_err("Too many CRC errors encountered "
597 "in firmware load.\n");
599 goto release_firmware
;
602 /* Previous transfer succeeded. Advance counters. */
607 memset(card
->cmd_buffer
, 0, len
);
608 memcpy(card
->cmd_buffer
, fw
, bytes
);
610 memcpy(card
->cmd_buffer
, fw
, len
);
612 err
= spu_write_u16(card
, IF_SPI_HOST_INT_STATUS_REG
, 0);
614 goto release_firmware
;
615 err
= spu_write(card
, IF_SPI_CMD_RDWRPORT_REG
,
616 card
->cmd_buffer
, len
);
618 goto release_firmware
;
619 err
= spu_write_u16(card
, IF_SPI_CARD_INT_CAUSE_REG
,
620 IF_SPI_CIC_CMD_DOWNLOAD_OVER
);
622 goto release_firmware
;
625 if (bytes
> prev_len
) {
626 lbs_pr_err("firmware load wants fewer bytes than "
627 "we have to offer.\n");
630 /* Confirm firmware download */
631 err
= spu_wait_for_u32(card
, IF_SPI_SCRATCH_4_REG
,
632 SUCCESSFUL_FW_DOWNLOAD_MAGIC
);
634 lbs_pr_err("failed to confirm the firmware download\n");
635 goto release_firmware
;
639 release_firmware(firmware
);
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 thread 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 %u\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
)
811 struct lbs_private
*priv
= card
->priv
;
813 err
= spu_read_u32(card
, IF_SPI_SCRATCH_3_REG
, &cause
);
817 spin_lock_irqsave(&priv
->driver_lock
, flags
);
818 lbs_queue_event(priv
, cause
& 0xff);
819 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
823 lbs_pr_err("%s: error %d\n", __func__
, err
);
826 static int lbs_spi_thread(void *data
)
829 struct if_spi_card
*card
= data
;
832 struct if_spi_packet
*packet
;
835 /* Wait to be woken up by one of two things. First, our ISR
836 * could tell us that something happened on the WLAN.
837 * Secondly, libertas could call hw_host_to_card with more
838 * data, which we might be able to send.
841 err
= down_interruptible(&card
->spi_ready
);
842 if (!card
->run_thread
) {
843 up(&card
->spi_thread_terminated
);
846 } while (err
== EINTR
);
848 /* Read the host interrupt status register to see what we
850 err
= spu_read_u16(card
, IF_SPI_HOST_INT_STATUS_REG
,
853 lbs_pr_err("I/O error\n");
857 if (hiStatus
& IF_SPI_HIST_CMD_UPLOAD_RDY
)
858 err
= if_spi_c2h_cmd(card
);
861 if (hiStatus
& IF_SPI_HIST_RX_UPLOAD_RDY
)
862 err
= if_spi_c2h_data(card
);
865 if (hiStatus
& IF_SPI_HIST_CMD_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
);
912 /* Block until lbs_spi_thread thread has terminated */
913 static void if_spi_terminate_spi_thread(struct if_spi_card
*card
)
915 /* It would be nice to use kthread_stop here, but that function
916 * can't wake threads waiting for a semaphore. */
917 card
->run_thread
= 0;
918 up(&card
->spi_ready
);
919 down(&card
->spi_thread_terminated
);
925 * Called from Libertas to transfer some data to the WLAN device
926 * We can't sleep here. */
927 static int if_spi_host_to_card(struct lbs_private
*priv
,
928 u8 type
, u8
*buf
, u16 nb
)
932 struct if_spi_card
*card
= priv
->card
;
933 struct if_spi_packet
*packet
;
936 lbs_deb_enter_args(LBS_DEB_SPI
, "type %d, bytes %d", type
, nb
);
939 lbs_pr_err("%s: invalid size requested: %d\n", __func__
, nb
);
944 packet
= kzalloc(sizeof(struct if_spi_packet
) + blen
, GFP_ATOMIC
);
950 memcpy(packet
->buffer
, buf
, nb
);
951 memset(packet
->buffer
+ nb
, 0, blen
- nb
);
955 priv
->dnld_sent
= DNLD_CMD_SENT
;
956 spin_lock_irqsave(&card
->buffer_lock
, flags
);
957 list_add_tail(&packet
->list
, &card
->cmd_packet_list
);
958 spin_unlock_irqrestore(&card
->buffer_lock
, flags
);
961 priv
->dnld_sent
= DNLD_DATA_SENT
;
962 spin_lock_irqsave(&card
->buffer_lock
, flags
);
963 list_add_tail(&packet
->list
, &card
->data_packet_list
);
964 spin_unlock_irqrestore(&card
->buffer_lock
, flags
);
967 lbs_pr_err("can't transfer buffer of type %d", type
);
972 /* Wake up the spi thread */
973 up(&card
->spi_ready
);
975 lbs_deb_leave_args(LBS_DEB_SPI
, "err=%d", err
);
982 * Service incoming interrupts from the WLAN device. We can't sleep here, so
983 * don't try to talk on the SPI bus, just wake up the SPI thread.
985 static irqreturn_t
if_spi_host_interrupt(int irq
, void *dev_id
)
987 struct if_spi_card
*card
= dev_id
;
989 up(&card
->spi_ready
);
997 static int if_spi_calculate_fw_names(u16 card_id
,
998 char *helper_fw
, char *main_fw
)
1001 for (i
= 0; i
< ARRAY_SIZE(chip_id_to_device_name
); ++i
) {
1002 if (card_id
== chip_id_to_device_name
[i
].chip_id
)
1005 if (i
== ARRAY_SIZE(chip_id_to_device_name
)) {
1006 lbs_pr_err("Unsupported chip_id: 0x%02x\n", card_id
);
1007 return -EAFNOSUPPORT
;
1009 snprintf(helper_fw
, FIRMWARE_NAME_MAX
, "libertas/gspi%d_hlp.bin",
1010 chip_id_to_device_name
[i
].name
);
1011 snprintf(main_fw
, FIRMWARE_NAME_MAX
, "libertas/gspi%d.bin",
1012 chip_id_to_device_name
[i
].name
);
1016 static int __devinit
if_spi_probe(struct spi_device
*spi
)
1018 struct if_spi_card
*card
;
1019 struct lbs_private
*priv
= NULL
;
1020 struct libertas_spi_platform_data
*pdata
= spi
->dev
.platform_data
;
1024 lbs_deb_enter(LBS_DEB_SPI
);
1032 err
= pdata
->setup(spi
);
1037 /* Allocate card structure to represent this specific device */
1038 card
= kzalloc(sizeof(struct if_spi_card
), GFP_KERNEL
);
1043 spi_set_drvdata(spi
, card
);
1044 card
->pdata
= pdata
;
1046 card
->gpio_cs
= pdata
->gpio_cs
;
1047 card
->prev_xfer_time
= jiffies
;
1049 sema_init(&card
->spi_ready
, 0);
1050 sema_init(&card
->spi_thread_terminated
, 0);
1051 INIT_LIST_HEAD(&card
->cmd_packet_list
);
1052 INIT_LIST_HEAD(&card
->data_packet_list
);
1053 spin_lock_init(&card
->buffer_lock
);
1055 /* set up GPIO CS line. TODO: use regular CS line */
1056 err
= gpio_request(card
->gpio_cs
, "if_spi_gpio_chip_select");
1059 err
= gpio_direction_output(card
->gpio_cs
, 1);
1063 /* Initialize the SPI Interface Unit */
1064 err
= spu_init(card
, pdata
->use_dummy_writes
);
1067 err
= spu_get_chip_revision(card
, &card
->card_id
, &card
->card_rev
);
1072 err
= spu_read_u32(card
, IF_SPI_SCRATCH_4_REG
, &scratch
);
1075 if (scratch
== SUCCESSFUL_FW_DOWNLOAD_MAGIC
)
1076 lbs_deb_spi("Firmware is already loaded for "
1077 "Marvell WLAN 802.11 adapter\n");
1079 err
= if_spi_calculate_fw_names(card
->card_id
,
1080 card
->helper_fw_name
, card
->main_fw_name
);
1084 lbs_deb_spi("Initializing FW for Marvell WLAN 802.11 adapter "
1085 "(chip_id = 0x%04x, chip_rev = 0x%02x) "
1086 "attached to SPI bus_num %d, chip_select %d. "
1087 "spi->max_speed_hz=%d\n",
1088 card
->card_id
, card
->card_rev
,
1089 spi
->master
->bus_num
, spi
->chip_select
,
1091 err
= if_spi_prog_helper_firmware(card
);
1094 err
= if_spi_prog_main_firmware(card
);
1097 lbs_deb_spi("loaded FW for Marvell WLAN 802.11 adapter\n");
1100 err
= spu_set_interrupt_mode(card
, 0, 1);
1104 /* Register our card with libertas.
1105 * This will call alloc_etherdev */
1106 priv
= lbs_add_card(card
, &spi
->dev
);
1113 priv
->hw_host_to_card
= if_spi_host_to_card
;
1115 priv
->ps_supported
= 1;
1117 /* Initialize interrupt handling stuff. */
1118 card
->run_thread
= 1;
1119 card
->spi_thread
= kthread_run(lbs_spi_thread
, card
, "lbs_spi_thread");
1120 if (IS_ERR(card
->spi_thread
)) {
1121 card
->run_thread
= 0;
1122 err
= PTR_ERR(card
->spi_thread
);
1123 lbs_pr_err("error creating SPI thread: err=%d\n", err
);
1126 err
= request_irq(spi
->irq
, if_spi_host_interrupt
,
1127 IRQF_TRIGGER_FALLING
, "libertas_spi", card
);
1129 lbs_pr_err("can't get host irq line-- request_irq failed\n");
1130 goto terminate_thread
;
1134 * This will call register_netdev, and we'll start
1135 * getting interrupts... */
1136 err
= lbs_start_card(priv
);
1140 lbs_deb_spi("Finished initializing WLAN module.\n");
1142 /* successful exit */
1146 free_irq(spi
->irq
, card
);
1148 if_spi_terminate_spi_thread(card
);
1150 lbs_remove_card(priv
); /* will call free_netdev */
1152 gpio_free(card
->gpio_cs
);
1154 free_if_spi_card(card
);
1156 lbs_deb_leave_args(LBS_DEB_SPI
, "err %d\n", err
);
1160 static int __devexit
libertas_spi_remove(struct spi_device
*spi
)
1162 struct if_spi_card
*card
= spi_get_drvdata(spi
);
1163 struct lbs_private
*priv
= card
->priv
;
1165 lbs_deb_spi("libertas_spi_remove\n");
1166 lbs_deb_enter(LBS_DEB_SPI
);
1167 priv
->surpriseremoved
= 1;
1169 lbs_stop_card(priv
);
1170 free_irq(spi
->irq
, card
);
1171 if_spi_terminate_spi_thread(card
);
1172 lbs_remove_card(priv
); /* will call free_netdev */
1173 gpio_free(card
->gpio_cs
);
1174 if (card
->pdata
->teardown
)
1175 card
->pdata
->teardown(spi
);
1176 free_if_spi_card(card
);
1177 lbs_deb_leave(LBS_DEB_SPI
);
1181 static struct spi_driver libertas_spi_driver
= {
1182 .probe
= if_spi_probe
,
1183 .remove
= __devexit_p(libertas_spi_remove
),
1185 .name
= "libertas_spi",
1186 .bus
= &spi_bus_type
,
1187 .owner
= THIS_MODULE
,
1195 static int __init
if_spi_init_module(void)
1198 lbs_deb_enter(LBS_DEB_SPI
);
1199 printk(KERN_INFO
"libertas_spi: Libertas SPI driver\n");
1200 ret
= spi_register_driver(&libertas_spi_driver
);
1201 lbs_deb_leave(LBS_DEB_SPI
);
1205 static void __exit
if_spi_exit_module(void)
1207 lbs_deb_enter(LBS_DEB_SPI
);
1208 spi_unregister_driver(&libertas_spi_driver
);
1209 lbs_deb_leave(LBS_DEB_SPI
);
1212 module_init(if_spi_init_module
);
1213 module_exit(if_spi_exit_module
);
1215 MODULE_DESCRIPTION("Libertas SPI WLAN Driver");
1216 MODULE_AUTHOR("Andrey Yurovsky <andrey@cozybit.com>, "
1217 "Colin McCabe <colin@cozybit.com>");
1218 MODULE_LICENSE("GPL");