51
55
unsigned long spu_reg_delay;
53
57
/* Handles all SPI communication (except for FW load) */
54
struct task_struct *spi_thread;
57
/* Used to wake up the spi_thread */
58
struct semaphore spi_ready;
59
struct semaphore spi_thread_terminated;
58
struct workqueue_struct *workqueue;
59
struct work_struct packet_work;
61
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;
64
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);
66
88
spi_set_drvdata(card->spi, NULL);
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);
745
801
/* Inform the host about a card event */
746
802
static void if_spi_e2h(struct if_spi_card *card)
766
822
lbs_pr_err("%s: error %d\n", __func__, err);
769
static int lbs_spi_thread(void *data)
825
static void if_spi_host_to_card_worker(struct work_struct *work)
772
struct if_spi_card *card = data;
828
struct if_spi_card *card;
776
/* Wait to be woken up by one of two things. First, our ISR
777
* could tell us that something happened on the WLAN.
778
* Secondly, libertas could call hw_host_to_card with more
779
* data, which we might be able to send.
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
782
err = down_interruptible(&card->spi_ready);
783
if (!card->run_thread) {
784
up(&card->spi_thread_terminated);
787
} while (err == -EINTR);
789
/* Read the host interrupt status register to see what we
791
err = spu_read_u16(card, IF_SPI_HOST_INT_STATUS_REG,
794
lbs_pr_err("I/O error\n");
798
if (hiStatus & IF_SPI_HIST_CMD_UPLOAD_RDY) {
799
err = if_spi_c2h_cmd(card);
803
if (hiStatus & IF_SPI_HIST_RX_UPLOAD_RDY) {
804
err = if_spi_c2h_data(card);
809
/* workaround: in PS mode, the card does not set the Command
810
* Download Ready bit, but it sets TX Download Ready. */
811
if (hiStatus & IF_SPI_HIST_CMD_DOWNLOAD_RDY ||
812
(card->priv->psstate != PS_STATE_FULL_POWER &&
813
(hiStatus & IF_SPI_HIST_TX_DOWNLOAD_RDY))) {
814
lbs_host_to_card_done(card->priv);
817
if (hiStatus & IF_SPI_HIST_CARD_EVENT)
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)
822
lbs_pr_err("%s: got error %d\n", __func__, err);
904
lbs_pr_err("%s: got error %d\n", __func__, err);
826
/* Block until lbs_spi_thread thread has terminated */
827
static void if_spi_terminate_spi_thread(struct if_spi_card *card)
829
/* It would be nice to use kthread_stop here, but that function
830
* can't wake threads waiting for a semaphore. */
831
card->run_thread = 0;
832
up(&card->spi_ready);
833
down(&card->spi_thread_terminated);
906
lbs_deb_leave(LBS_DEB_SPI);
842
915
u8 type, u8 *buf, u16 nb)
845
919
struct if_spi_card *card = priv->card;
920
struct if_spi_packet *packet;
847
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);
853
err = spu_write(card, IF_SPI_CMD_RDWRPORT_REG, buf, 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);
856
err = spu_write(card, IF_SPI_DATA_RDWRPORT_REG, buf, nb);
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);
859
954
lbs_pr_err("can't transfer buffer of type %d", type);
886
static int __devinit if_spi_probe(struct spi_device *spi)
985
static int if_spi_init_card(struct if_spi_card *card)
888
struct if_spi_card *card;
889
struct lbs_private *priv = NULL;
890
struct libertas_spi_platform_data *pdata = spi->dev.platform_data;
987
struct spi_device *spi = card->spi;
893
struct sched_param param = { .sched_priority = 1 };
894
990
const struct firmware *helper = NULL;
895
991
const struct firmware *mainfw = NULL;
897
993
lbs_deb_enter(LBS_DEB_SPI);
905
err = pdata->setup(spi);
910
/* Allocate card structure to represent this specific device */
911
card = kzalloc(sizeof(struct if_spi_card), GFP_KERNEL);
916
spi_set_drvdata(spi, card);
919
card->prev_xfer_time = jiffies;
921
sema_init(&card->spi_ready, 0);
922
sema_init(&card->spi_thread_terminated, 0);
924
/* Initialize the SPI Interface Unit */
925
err = spu_init(card, pdata->use_dummy_writes);
995
err = spu_init(card, card->pdata->use_dummy_writes);
928
998
err = spu_get_chip_revision(card, &card->card_id, &card->card_rev);
933
1002
err = spu_read_u32(card, IF_SPI_SCRATCH_4_REG, &scratch);
936
1005
if (scratch == SUCCESSFUL_FW_DOWNLOAD_MAGIC)
937
1006
lbs_deb_spi("Firmware is already loaded for "
938
1007
"Marvell WLAN 802.11 adapter\n");
966
1035
spi->max_speed_hz);
967
1036
err = if_spi_prog_helper_firmware(card, helper);
970
1039
err = if_spi_prog_main_firmware(card, mainfw);
973
1042
lbs_deb_spi("loaded FW for Marvell WLAN 802.11 adapter\n");
976
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);
980
1102
/* Register our card with libertas.
993
1115
priv->fw_ready = 1;
995
1117
/* Initialize interrupt handling stuff. */
996
card->run_thread = 1;
997
card->spi_thread = kthread_run(lbs_spi_thread, card, "lbs_spi_thread");
998
if (IS_ERR(card->spi_thread)) {
999
card->run_thread = 0;
1000
err = PTR_ERR(card->spi_thread);
1001
lbs_pr_err("error creating SPI thread: err=%d\n", err);
1004
if (sched_setscheduler(card->spi_thread, SCHED_FIFO, ¶m))
1005
lbs_pr_err("Error setting scheduler, using default.\n");
1118
card->workqueue = create_workqueue("libertas_spi");
1119
INIT_WORK(&card->packet_work, if_spi_host_to_card_worker);
1007
1121
err = request_irq(spi->irq, if_spi_host_interrupt,
1008
1122
IRQF_TRIGGER_FALLING, "libertas_spi", card);
1010
1124
lbs_pr_err("can't get host irq line-- request_irq failed\n");
1011
goto terminate_thread;
1125
goto terminate_workqueue;
1014
/* poke the IRQ handler so that we don't miss the first interrupt */
1015
up(&card->spi_ready);
1017
1128
/* Start the card.
1018
1129
* This will call register_netdev, and we'll start
1019
1130
* getting interrupts... */