libertas_spi: Use workqueue in hw_host_to_card
[pandora-kernel.git] / drivers / net / wireless / libertas / if_spi.c
1 /*
2  *      linux/drivers/net/wireless/libertas/if_spi.c
3  *
4  *      Driver for Marvell SPI WLAN cards.
5  *
6  *      Copyright 2008 Analog Devices Inc.
7  *
8  *      Authors:
9  *      Andrey Yurovsky <andrey@cozybit.com>
10  *      Colin McCabe <colin@cozybit.com>
11  *
12  *      Inspired by if_sdio.c, Copyright 2007-2008 Pierre Ossman
13  *
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.
18  */
19
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>
28
29 #include "host.h"
30 #include "decl.h"
31 #include "defs.h"
32 #include "dev.h"
33 #include "if_spi.h"
34
35 struct if_spi_packet {
36         struct list_head                list;
37         u16                             blen;
38         u8                              buffer[0] __attribute__((aligned(4)));
39 };
40
41 struct if_spi_card {
42         struct spi_device               *spi;
43         struct lbs_private              *priv;
44         struct libertas_spi_platform_data *pdata;
45
46         /* The card ID and card revision, as reported by the hardware. */
47         u16                             card_id;
48         u8                              card_rev;
49
50         /* The last time that we initiated an SPU operation */
51         unsigned long                   prev_xfer_time;
52
53         int                             use_dummy_writes;
54         unsigned long                   spu_port_delay;
55         unsigned long                   spu_reg_delay;
56
57         /* Handles all SPI communication (except for FW load) */
58         struct workqueue_struct         *workqueue;
59         struct work_struct              packet_work;
60
61         u8                              cmd_buffer[IF_SPI_CMD_BUF_SIZE];
62
63         /* A buffer of incoming packets from libertas core.
64          * Since we can't sleep in hw_host_to_card, we have to buffer
65          * them. */
66         struct list_head                cmd_packet_list;
67         struct list_head                data_packet_list;
68
69         /* Protects cmd_packet_list and data_packet_list */
70         spinlock_t                      buffer_lock;
71 };
72
73 static void free_if_spi_card(struct if_spi_card *card)
74 {
75         struct list_head *cursor, *next;
76         struct if_spi_packet *packet;
77
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);
81                 kfree(packet);
82         }
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);
86                 kfree(packet);
87         }
88         spi_set_drvdata(card->spi, NULL);
89         kfree(card);
90 }
91
92 #define MODEL_8385      0x04
93 #define MODEL_8686      0x0b
94 #define MODEL_8688      0x10
95
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" },
102         { 0, NULL, NULL }
103 };
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");
113
114
115 /*
116  * SPI Interface Unit Routines
117  *
118  * The SPU sits between the host and the WLAN module.
119  * All communication with the firmware is through SPU transactions.
120  *
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.
123  *
124  */
125
126 static void spu_transaction_init(struct if_spi_card *card)
127 {
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. */
133                 ndelay(400);
134         }
135 }
136
137 static void spu_transaction_finish(struct if_spi_card *card)
138 {
139         card->prev_xfer_time = jiffies;
140 }
141
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)
145 {
146         int err = 0;
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;
151
152         spi_message_init(&m);
153         memset(&reg_trans, 0, sizeof(reg_trans));
154         memset(&data_trans, 0, sizeof(data_trans));
155
156         /* You must give an even number of bytes to the SPU, even if it
157          * doesn't care about the last one.  */
158         BUG_ON(len & 0x1);
159
160         spu_transaction_init(card);
161
162         /* write SPU register index */
163         reg_trans.tx_buf = &reg_out;
164         reg_trans.len = sizeof(reg_out);
165
166         data_trans.tx_buf = buf;
167         data_trans.len = len;
168
169         spi_message_add_tail(&reg_trans, &m);
170         spi_message_add_tail(&data_trans, &m);
171
172         err = spi_sync(card->spi, &m);
173         spu_transaction_finish(card);
174         return err;
175 }
176
177 static inline int spu_write_u16(struct if_spi_card *card, u16 reg, u16 val)
178 {
179         __le16 buff;
180
181         buff = cpu_to_le16(val);
182         return spu_write(card, reg, (u8 *)&buff, sizeof(u16));
183 }
184
185 static inline int spu_reg_is_port_reg(u16 reg)
186 {
187         switch (reg) {
188         case IF_SPI_IO_RDWRPORT_REG:
189         case IF_SPI_CMD_RDWRPORT_REG:
190         case IF_SPI_DATA_RDWRPORT_REG:
191                 return 1;
192         default:
193                 return 0;
194         }
195 }
196
197 static int spu_read(struct if_spi_card *card, u16 reg, u8 *buf, int len)
198 {
199         unsigned int delay;
200         int err = 0;
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;
206
207         /* You must take an even number of bytes from the SPU, even if you
208          * don't care about the last one.  */
209         BUG_ON(len & 0x1);
210
211         spu_transaction_init(card);
212
213         spi_message_init(&m);
214         memset(&reg_trans, 0, sizeof(reg_trans));
215         memset(&dummy_trans, 0, sizeof(dummy_trans));
216         memset(&data_trans, 0, sizeof(data_trans));
217
218         /* write SPU register index */
219         reg_trans.tx_buf = &reg_out;
220         reg_trans.len = sizeof(reg_out);
221         spi_message_add_tail(&reg_trans, &m);
222
223         delay = spu_reg_is_port_reg(reg) ? card->spu_port_delay :
224                                                 card->spu_reg_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);
229         } else {
230                 /* Busy-wait while the SPU fills the FIFO */
231                 reg_trans.delay_usecs =
232                         DIV_ROUND_UP((100 + (delay * 10)), 1000);
233         }
234
235         /* read in data */
236         data_trans.rx_buf = buf;
237         data_trans.len = len;
238         spi_message_add_tail(&data_trans, &m);
239
240         err = spi_sync(card->spi, &m);
241         spu_transaction_finish(card);
242         return err;
243 }
244
245 /* Read 16 bits from an SPI register */
246 static inline int spu_read_u16(struct if_spi_card *card, u16 reg, u16 *val)
247 {
248         __le16 buf;
249         int ret;
250
251         ret = spu_read(card, reg, (u8 *)&buf, sizeof(buf));
252         if (ret == 0)
253                 *val = le16_to_cpup(&buf);
254         return ret;
255 }
256
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)
260 {
261         __le32 buf;
262         int err;
263
264         err = spu_read(card, reg, (u8 *)&buf, sizeof(buf));
265         if (!err)
266                 *val = le32_to_cpup(&buf);
267         return err;
268 }
269
270 /* Keep reading 16 bits from an SPI register until you get the correct result.
271  *
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
275  *
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)
279 {
280         int err;
281         unsigned long timeout = jiffies + 5*HZ;
282         while (1) {
283                 u16 val;
284                 err = spu_read_u16(card, reg, &val);
285                 if (err)
286                         return err;
287                 if (target_mask) {
288                         if ((val & target_mask) == target)
289                                 return 0;
290                 } else {
291                         if (val)
292                                 return 0;
293                 }
294                 udelay(100);
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);
299                         return -ETIMEDOUT;
300                 }
301         }
302 }
303
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)
307 {
308         int err, try;
309         for (try = 0; try < 4; ++try) {
310                 u32 val = 0;
311                 err = spu_read_u32(card, reg, &val);
312                 if (err)
313                         return err;
314                 if (val == target)
315                         return 0;
316                 mdelay(100);
317         }
318         return -ETIMEDOUT;
319 }
320
321 static int spu_set_interrupt_mode(struct if_spi_card *card,
322                            int suppress_host_int,
323                            int auto_int)
324 {
325         int err = 0;
326
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);
331                 if (err)
332                         return err;
333         } else {
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);
340                 if (err)
341                         return err;
342         }
343
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. */
348         if (auto_int) {
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);
354                 if (err)
355                         return err;
356         } else {
357                 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG, 0);
358                 if (err)
359                         return err;
360         }
361         return err;
362 }
363
364 static int spu_get_chip_revision(struct if_spi_card *card,
365                                   u16 *card_id, u8 *card_rev)
366 {
367         int err = 0;
368         u32 dev_ctrl;
369         err = spu_read_u32(card, IF_SPI_DEVICEID_CTRL_REG, &dev_ctrl);
370         if (err)
371                 return err;
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);
374         return err;
375 }
376
377 static int spu_set_bus_mode(struct if_spi_card *card, u16 mode)
378 {
379         int err = 0;
380         u16 rval;
381         /* set bus mode */
382         err = spu_write_u16(card, IF_SPI_SPU_BUS_MODE_REG, mode);
383         if (err)
384                 return err;
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);
387         if (err)
388                 return err;
389         if ((rval & 0xF) != mode) {
390                 lbs_pr_err("Can't read bus mode register.\n");
391                 return -EIO;
392         }
393         return 0;
394 }
395
396 static int spu_init(struct if_spi_card *card, int use_dummy_writes)
397 {
398         int err = 0;
399         u32 delay;
400
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);
408         if (err)
409                 return err;
410         card->spu_port_delay = 1000;
411         card->spu_reg_delay = 1000;
412         err = spu_read_u32(card, IF_SPI_DELAY_READ_REG, &delay);
413         if (err)
414                 return err;
415         card->spu_port_delay = delay & 0x0000ffff;
416         card->spu_reg_delay = (delay & 0xffff0000) >> 16;
417
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);
425                 if (err)
426                         return err;
427         }
428
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);
432         return err;
433 }
434
435 /*
436  * Firmware Loading
437  */
438
439 static int if_spi_prog_helper_firmware(struct if_spi_card *card,
440                                         const struct firmware *firmware)
441 {
442         int err = 0;
443         int bytes_remaining;
444         const u8 *fw;
445         u8 temp[HELPER_FW_LOAD_CHUNK_SZ];
446
447         lbs_deb_enter(LBS_DEB_SPI);
448
449         err = spu_set_interrupt_mode(card, 1, 0);
450         if (err)
451                 goto out;
452
453         bytes_remaining = firmware->size;
454         fw = firmware->data;
455
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);
462                 if (err)
463                         goto out;
464
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);
468                 if (err)
469                         goto out;
470
471                 /* Feed the data into the command read/write port reg
472                  * in chunks of 64 bytes */
473                 memset(temp, 0, sizeof(temp));
474                 memcpy(temp, fw,
475                        min(bytes_remaining, HELPER_FW_LOAD_CHUNK_SZ));
476                 mdelay(10);
477                 err = spu_write(card, IF_SPI_CMD_RDWRPORT_REG,
478                                         temp, HELPER_FW_LOAD_CHUNK_SZ);
479                 if (err)
480                         goto out;
481
482                 /* Interrupt the boot code */
483                 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
484                 if (err)
485                         goto out;
486                 err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG,
487                                        IF_SPI_CIC_CMD_DOWNLOAD_OVER);
488                 if (err)
489                         goto out;
490                 bytes_remaining -= HELPER_FW_LOAD_CHUNK_SZ;
491                 fw += HELPER_FW_LOAD_CHUNK_SZ;
492         }
493
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);
498         if (err)
499                 goto out;
500         err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
501         if (err)
502                 goto out;
503         err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG,
504                                 IF_SPI_CIC_CMD_DOWNLOAD_OVER);
505                 goto out;
506
507         lbs_deb_spi("waiting for helper to boot...\n");
508
509 out:
510         if (err)
511                 lbs_pr_err("failed to load helper firmware (err=%d)\n", err);
512         lbs_deb_leave_args(LBS_DEB_SPI, "err %d", err);
513         return err;
514 }
515
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,
519                                                 int *crc_err)
520 {
521         u16 len;
522         int err = 0;
523
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);
529         if (err) {
530                 lbs_pr_err("timed out waiting for host_int_status\n");
531                 return err;
532         }
533
534         /* Ask the device how many bytes of firmware it wants. */
535         err = spu_read_u16(card, IF_SPI_SCRATCH_1_REG, &len);
536         if (err)
537                 return err;
538
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);
543                 return -EIO;
544         }
545         if (len & 0x1) {
546                 lbs_deb_spi("%s: crc error\n", __func__);
547                 len &= ~0x1;
548                 *crc_err = 1;
549         } else
550                 *crc_err = 0;
551
552         return len;
553 }
554
555 static int if_spi_prog_main_firmware(struct if_spi_card *card,
556                                         const struct firmware *firmware)
557 {
558         int len, prev_len;
559         int bytes, crc_err = 0, err = 0;
560         const u8 *fw;
561         u16 num_crc_errs;
562
563         lbs_deb_enter(LBS_DEB_SPI);
564
565         err = spu_set_interrupt_mode(card, 1, 0);
566         if (err)
567                 goto out;
568
569         err = spu_wait_for_u16(card, IF_SPI_SCRATCH_1_REG, 0, 0);
570         if (err) {
571                 lbs_pr_err("%s: timed out waiting for initial "
572                            "scratch reg = 0\n", __func__);
573                 goto out;
574         }
575
576         num_crc_errs = 0;
577         prev_len = 0;
578         bytes = firmware->size;
579         fw = firmware->data;
580         while ((len = if_spi_prog_main_firmware_check_len(card, &crc_err))) {
581                 if (len < 0) {
582                         err = len;
583                         goto out;
584                 }
585                 if (bytes < 0) {
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");
590                         break;
591                 }
592                 if (crc_err) {
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");
597                                 err = -EIO;
598                                 goto out;
599                         }
600                 } else {
601                         /* Previous transfer succeeded. Advance counters. */
602                         bytes -= prev_len;
603                         fw += prev_len;
604                 }
605                 if (bytes < len) {
606                         memset(card->cmd_buffer, 0, len);
607                         memcpy(card->cmd_buffer, fw, bytes);
608                 } else
609                         memcpy(card->cmd_buffer, fw, len);
610
611                 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
612                 if (err)
613                         goto out;
614                 err = spu_write(card, IF_SPI_CMD_RDWRPORT_REG,
615                                 card->cmd_buffer, len);
616                 if (err)
617                         goto out;
618                 err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG ,
619                                         IF_SPI_CIC_CMD_DOWNLOAD_OVER);
620                 if (err)
621                         goto out;
622                 prev_len = len;
623         }
624         if (bytes > prev_len) {
625                 lbs_pr_err("firmware load wants fewer bytes than "
626                            "we have to offer.\n");
627         }
628
629         /* Confirm firmware download */
630         err = spu_wait_for_u32(card, IF_SPI_SCRATCH_4_REG,
631                                         SUCCESSFUL_FW_DOWNLOAD_MAGIC);
632         if (err) {
633                 lbs_pr_err("failed to confirm the firmware download\n");
634                 goto out;
635         }
636
637 out:
638         if (err)
639                 lbs_pr_err("failed to load firmware (err=%d)\n", err);
640         lbs_deb_leave_args(LBS_DEB_SPI, "err %d", err);
641         return err;
642 }
643
644 /*
645  * SPI Transfer Thread
646  *
647  * The SPI worker handles all SPI transfers, so there is no need for a lock.
648  */
649
650 /* Move a command from the card to the host */
651 static int if_spi_c2h_cmd(struct if_spi_card *card)
652 {
653         struct lbs_private *priv = card->priv;
654         unsigned long flags;
655         int err = 0;
656         u16 len;
657         u8 i;
658
659         /* We need a buffer big enough to handle whatever people send to
660          * hw_host_to_card */
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);
663
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);
668
669         lbs_deb_enter(LBS_DEB_SPI);
670
671         /* How many bytes are there to read? */
672         err = spu_read_u16(card, IF_SPI_SCRATCH_2_REG, &len);
673         if (err)
674                 goto out;
675         if (!len) {
676                 lbs_pr_err("%s: error: card has no data for host\n",
677                            __func__);
678                 err = -EINVAL;
679                 goto out;
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);
684                 err = -EINVAL;
685                 goto out;
686         }
687
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));
691         if (err)
692                 goto out;
693
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);
701
702 out:
703         if (err)
704                 lbs_pr_err("%s: err=%d\n", __func__, err);
705         lbs_deb_leave(LBS_DEB_SPI);
706         return err;
707 }
708
709 /* Move data from the card to the host */
710 static int if_spi_c2h_data(struct if_spi_card *card)
711 {
712         struct sk_buff *skb;
713         char *data;
714         u16 len;
715         int err = 0;
716
717         lbs_deb_enter(LBS_DEB_SPI);
718
719         /* How many bytes are there to read? */
720         err = spu_read_u16(card, IF_SPI_SCRATCH_1_REG, &len);
721         if (err)
722                 goto out;
723         if (!len) {
724                 lbs_pr_err("%s: error: card has no data for host\n",
725                            __func__);
726                 err = -EINVAL;
727                 goto out;
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);
732                 err = -EINVAL;
733                 goto out;
734         }
735
736         /* TODO: should we allocate a smaller skb if we have less data? */
737         skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE);
738         if (!skb) {
739                 err = -ENOBUFS;
740                 goto out;
741         }
742         skb_reserve(skb, IPFIELD_ALIGN_OFFSET);
743         data = skb_put(skb, len);
744
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));
747         if (err)
748                 goto free_skb;
749
750         /* pass the SKB to libertas */
751         err = lbs_process_rxed_packet(card->priv, skb);
752         if (err)
753                 goto free_skb;
754
755         /* success */
756         goto out;
757
758 free_skb:
759         dev_kfree_skb(skb);
760 out:
761         if (err)
762                 lbs_pr_err("%s: err=%d\n", __func__, err);
763         lbs_deb_leave(LBS_DEB_SPI);
764         return err;
765 }
766
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)
770 {
771         int err = 0;
772         u16 int_type, port_reg;
773
774         switch (type) {
775         case MVMS_DAT:
776                 int_type = IF_SPI_CIC_TX_DOWNLOAD_OVER;
777                 port_reg = IF_SPI_DATA_RDWRPORT_REG;
778                 break;
779         case MVMS_CMD:
780                 int_type = IF_SPI_CIC_CMD_DOWNLOAD_OVER;
781                 port_reg = IF_SPI_CMD_RDWRPORT_REG;
782                 break;
783         default:
784                 lbs_pr_err("can't transfer buffer of type %d\n", type);
785                 err = -EINVAL;
786                 goto out;
787         }
788
789         /* Write the data to the card */
790         err = spu_write(card, port_reg, packet->buffer, packet->blen);
791         if (err)
792                 goto out;
793
794 out:
795         kfree(packet);
796
797         if (err)
798                 lbs_pr_err("%s: error %d\n", __func__, err);
799 }
800
801 /* Inform the host about a card event */
802 static void if_spi_e2h(struct if_spi_card *card)
803 {
804         int err = 0;
805         u32 cause;
806         struct lbs_private *priv = card->priv;
807
808         err = spu_read_u32(card, IF_SPI_SCRATCH_3_REG, &cause);
809         if (err)
810                 goto out;
811
812         /* re-enable the card event interrupt */
813         spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG,
814                         ~IF_SPI_HICU_CARD_EVENT);
815
816         /* generate a card interrupt */
817         spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG, IF_SPI_CIC_HOST_EVENT);
818
819         lbs_queue_event(priv, cause & 0xff);
820 out:
821         if (err)
822                 lbs_pr_err("%s: error %d\n", __func__, err);
823 }
824
825 static void if_spi_host_to_card_worker(struct work_struct *work)
826 {
827         int err;
828         struct if_spi_card *card;
829         u16 hiStatus;
830         unsigned long flags;
831         struct if_spi_packet *packet;
832
833         card = container_of(work, struct if_spi_card, packet_work);
834
835         lbs_deb_enter(LBS_DEB_SPI);
836
837         /* Read the host interrupt status register to see what we
838          * can do. */
839         err = spu_read_u16(card, IF_SPI_HOST_INT_STATUS_REG,
840                                 &hiStatus);
841         if (err) {
842                 lbs_pr_err("I/O error\n");
843                 goto err;
844         }
845
846         if (hiStatus & IF_SPI_HIST_CMD_UPLOAD_RDY) {
847                 err = if_spi_c2h_cmd(card);
848                 if (err)
849                         goto err;
850         }
851         if (hiStatus & IF_SPI_HIST_RX_UPLOAD_RDY) {
852                 err = if_spi_c2h_data(card);
853                 if (err)
854                         goto err;
855         }
856
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
866                  * command.
867                  */
868                 lbs_host_to_card_done(card->priv);
869
870                 /* Do we have any command packets from the host to
871                  * send? */
872                 packet = NULL;
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);
878                 }
879                 spin_unlock_irqrestore(&card->buffer_lock, flags);
880
881                 if (packet)
882                         if_spi_h2c(card, packet, MVMS_CMD);
883         }
884         if (hiStatus & IF_SPI_HIST_TX_DOWNLOAD_RDY) {
885                 /* Do we have any data packets from the host to
886                  * send? */
887                 packet = NULL;
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);
893                 }
894                 spin_unlock_irqrestore(&card->buffer_lock, flags);
895
896                 if (packet)
897                         if_spi_h2c(card, packet, MVMS_DAT);
898         }
899         if (hiStatus & IF_SPI_HIST_CARD_EVENT)
900                 if_spi_e2h(card);
901
902 err:
903         if (err)
904                 lbs_pr_err("%s: got error %d\n", __func__, err);
905
906         lbs_deb_leave(LBS_DEB_SPI);
907 }
908
909 /*
910  * Host to Card
911  *
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)
916 {
917         int err = 0;
918         unsigned long flags;
919         struct if_spi_card *card = priv->card;
920         struct if_spi_packet *packet;
921         u16 blen;
922
923         lbs_deb_enter_args(LBS_DEB_SPI, "type %d, bytes %d", type, nb);
924
925         if (nb == 0) {
926                 lbs_pr_err("%s: invalid size requested: %d\n", __func__, nb);
927                 err = -EINVAL;
928                 goto out;
929         }
930         blen = ALIGN(nb, 4);
931         packet = kzalloc(sizeof(struct if_spi_packet) + blen, GFP_ATOMIC);
932         if (!packet) {
933                 err = -ENOMEM;
934                 goto out;
935         }
936         packet->blen = blen;
937         memcpy(packet->buffer, buf, nb);
938         memset(packet->buffer + nb, 0, blen - nb);
939
940         switch (type) {
941         case MVMS_CMD:
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);
946                 break;
947         case MVMS_DAT:
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);
952                 break;
953         default:
954                 lbs_pr_err("can't transfer buffer of type %d", type);
955                 err = -EINVAL;
956                 break;
957         }
958
959         /* Queue spi xfer work */
960         queue_work(card->workqueue, &card->packet_work);
961 out:
962         lbs_deb_leave_args(LBS_DEB_SPI, "err=%d", err);
963         return err;
964 }
965
966 /*
967  * Host Interrupts
968  *
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.
971  */
972 static irqreturn_t if_spi_host_interrupt(int irq, void *dev_id)
973 {
974         struct if_spi_card *card = dev_id;
975
976         queue_work(card->workqueue, &card->packet_work);
977
978         return IRQ_HANDLED;
979 }
980
981 /*
982  * SPI callbacks
983  */
984
985 static int if_spi_init_card(struct if_spi_card *card)
986 {
987         struct spi_device *spi = card->spi;
988         int err, i;
989         u32 scratch;
990         const struct firmware *helper = NULL;
991         const struct firmware *mainfw = NULL;
992
993         lbs_deb_enter(LBS_DEB_SPI);
994
995         err = spu_init(card, card->pdata->use_dummy_writes);
996         if (err)
997                 goto out;
998         err = spu_get_chip_revision(card, &card->card_id, &card->card_rev);
999         if (err)
1000                 goto out;
1001
1002         err = spu_read_u32(card, IF_SPI_SCRATCH_4_REG, &scratch);
1003         if (err)
1004                 goto out;
1005         if (scratch == SUCCESSFUL_FW_DOWNLOAD_MAGIC)
1006                 lbs_deb_spi("Firmware is already loaded for "
1007                             "Marvell WLAN 802.11 adapter\n");
1008         else {
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)
1012                                 break;
1013                 }
1014                 if (i == ARRAY_SIZE(fw_table)) {
1015                         lbs_pr_err("Unsupported chip_id: 0x%02x\n",
1016                                         card->card_id);
1017                         err = -ENODEV;
1018                         goto out;
1019                 }
1020
1021                 err = lbs_get_firmware(&card->spi->dev, NULL, NULL,
1022                                         card->card_id, &fw_table[0], &helper,
1023                                         &mainfw);
1024                 if (err) {
1025                         lbs_pr_err("failed to find firmware (%d)\n", err);
1026                         goto out;
1027                 }
1028
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,
1035                                 spi->max_speed_hz);
1036                 err = if_spi_prog_helper_firmware(card, helper);
1037                 if (err)
1038                         goto out;
1039                 err = if_spi_prog_main_firmware(card, mainfw);
1040                 if (err)
1041                         goto out;
1042                 lbs_deb_spi("loaded FW for Marvell WLAN 802.11 adapter\n");
1043         }
1044
1045         err = spu_set_interrupt_mode(card, 0, 1);
1046         if (err)
1047                 goto out;
1048
1049 out:
1050         if (helper)
1051                 release_firmware(helper);
1052         if (mainfw)
1053                 release_firmware(mainfw);
1054
1055         lbs_deb_leave_args(LBS_DEB_SPI, "err %d\n", err);
1056
1057         return err;
1058 }
1059
1060 static int __devinit if_spi_probe(struct spi_device *spi)
1061 {
1062         struct if_spi_card *card;
1063         struct lbs_private *priv = NULL;
1064         struct libertas_spi_platform_data *pdata = spi->dev.platform_data;
1065         int err = 0;
1066
1067         lbs_deb_enter(LBS_DEB_SPI);
1068
1069         if (!pdata) {
1070                 err = -EINVAL;
1071                 goto out;
1072         }
1073
1074         if (pdata->setup) {
1075                 err = pdata->setup(spi);
1076                 if (err)
1077                         goto out;
1078         }
1079
1080         /* Allocate card structure to represent this specific device */
1081         card = kzalloc(sizeof(struct if_spi_card), GFP_KERNEL);
1082         if (!card) {
1083                 err = -ENOMEM;
1084                 goto teardown;
1085         }
1086         spi_set_drvdata(spi, card);
1087         card->pdata = pdata;
1088         card->spi = spi;
1089         card->prev_xfer_time = jiffies;
1090
1091         INIT_LIST_HEAD(&card->cmd_packet_list);
1092         INIT_LIST_HEAD(&card->data_packet_list);
1093         spin_lock_init(&card->buffer_lock);
1094
1095         /* Initialize the SPI Interface Unit */
1096
1097         /* Firmware load */
1098         err = if_spi_init_card(card);
1099         if (err)
1100                 goto free_card;
1101
1102         /* Register our card with libertas.
1103          * This will call alloc_etherdev */
1104         priv = lbs_add_card(card, &spi->dev);
1105         if (!priv) {
1106                 err = -ENOMEM;
1107                 goto free_card;
1108         }
1109         card->priv = priv;
1110         priv->card = card;
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;
1115         priv->fw_ready = 1;
1116
1117         /* Initialize interrupt handling stuff. */
1118         card->workqueue = create_workqueue("libertas_spi");
1119         INIT_WORK(&card->packet_work, if_spi_host_to_card_worker);
1120
1121         err = request_irq(spi->irq, if_spi_host_interrupt,
1122                         IRQF_TRIGGER_FALLING, "libertas_spi", card);
1123         if (err) {
1124                 lbs_pr_err("can't get host irq line-- request_irq failed\n");
1125                 goto terminate_workqueue;
1126         }
1127
1128         /* Start the card.
1129          * This will call register_netdev, and we'll start
1130          * getting interrupts... */
1131         err = lbs_start_card(priv);
1132         if (err)
1133                 goto release_irq;
1134
1135         lbs_deb_spi("Finished initializing WLAN module.\n");
1136
1137         /* successful exit */
1138         goto out;
1139
1140 release_irq:
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 */
1146 free_card:
1147         free_if_spi_card(card);
1148 teardown:
1149         if (pdata->teardown)
1150                 pdata->teardown(spi);
1151 out:
1152         lbs_deb_leave_args(LBS_DEB_SPI, "err %d\n", err);
1153         return err;
1154 }
1155
1156 static int __devexit libertas_spi_remove(struct spi_device *spi)
1157 {
1158         struct if_spi_card *card = spi_get_drvdata(spi);
1159         struct lbs_private *priv = card->priv;
1160
1161         lbs_deb_spi("libertas_spi_remove\n");
1162         lbs_deb_enter(LBS_DEB_SPI);
1163
1164         lbs_stop_card(priv);
1165         lbs_remove_card(priv); /* will call free_netdev */
1166
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);
1174         return 0;
1175 }
1176
1177 static struct spi_driver libertas_spi_driver = {
1178         .probe  = if_spi_probe,
1179         .remove = __devexit_p(libertas_spi_remove),
1180         .driver = {
1181                 .name   = "libertas_spi",
1182                 .bus    = &spi_bus_type,
1183                 .owner  = THIS_MODULE,
1184         },
1185 };
1186
1187 /*
1188  * Module functions
1189  */
1190
1191 static int __init if_spi_init_module(void)
1192 {
1193         int ret = 0;
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);
1198         return ret;
1199 }
1200
1201 static void __exit if_spi_exit_module(void)
1202 {
1203         lbs_deb_enter(LBS_DEB_SPI);
1204         spi_unregister_driver(&libertas_spi_driver);
1205         lbs_deb_leave(LBS_DEB_SPI);
1206 }
1207
1208 module_init(if_spi_init_module);
1209 module_exit(if_spi_exit_module);
1210
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");