Merge branch 'linus' into tracing/core
[linux-2.6] / 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/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>
29
30 #include "host.h"
31 #include "decl.h"
32 #include "defs.h"
33 #include "dev.h"
34 #include "if_spi.h"
35
36 struct if_spi_packet {
37         struct list_head                list;
38         u16                             blen;
39         u8                              buffer[0] __attribute__((aligned(4)));
40 };
41
42 struct if_spi_card {
43         struct spi_device               *spi;
44         struct lbs_private              *priv;
45         struct libertas_spi_platform_data *pdata;
46
47         char                            helper_fw_name[FIRMWARE_NAME_MAX];
48         char                            main_fw_name[FIRMWARE_NAME_MAX];
49
50         /* The card ID and card revision, as reported by the hardware. */
51         u16                             card_id;
52         u8                              card_rev;
53
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
58          * after each word. */
59         int                             gpio_cs;
60
61         /* The last time that we initiated an SPU operation */
62         unsigned long                   prev_xfer_time;
63
64         int                             use_dummy_writes;
65         unsigned long                   spu_port_delay;
66         unsigned long                   spu_reg_delay;
67
68         /* Handles all SPI communication (except for FW load) */
69         struct task_struct              *spi_thread;
70         int                             run_thread;
71
72         /* Used to wake up the spi_thread */
73         struct semaphore                spi_ready;
74         struct semaphore                spi_thread_terminated;
75
76         u8                              cmd_buffer[IF_SPI_CMD_BUF_SIZE];
77
78         /* A buffer of incoming packets from libertas core.
79          * Since we can't sleep in hw_host_to_card, we have to buffer
80          * them. */
81         struct list_head                cmd_packet_list;
82         struct list_head                data_packet_list;
83
84         /* Protects cmd_packet_list and data_packet_list */
85         spinlock_t                      buffer_lock;
86 };
87
88 static void free_if_spi_card(struct if_spi_card *card)
89 {
90         struct list_head *cursor, *next;
91         struct if_spi_packet *packet;
92
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);
97                 kfree(packet);
98         }
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);
102                 kfree(packet);
103         }
104         spi_set_drvdata(card->spi, NULL);
105         kfree(card);
106 }
107
108 static struct chip_ident chip_id_to_device_name[] = {
109         { .chip_id = 0x04, .name = 8385 },
110         { .chip_id = 0x0b, .name = 8686 },
111 };
112
113 /*
114  * SPI Interface Unit Routines
115  *
116  * The SPU sits between the host and the WLAN module.
117  * All communication with the firmware is through SPU transactions.
118  *
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.
121  *
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.
125  */
126
127 static void spu_transaction_init(struct if_spi_card *card)
128 {
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. */
134                 ndelay(400);
135         }
136         gpio_set_value(card->gpio_cs, 0); /* assert CS */
137 }
138
139 static void spu_transaction_finish(struct if_spi_card *card)
140 {
141         gpio_set_value(card->gpio_cs, 1); /* drop CS */
142         card->prev_xfer_time = jiffies;
143 }
144
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)
148 {
149         int err = 0;
150         u16 reg_out = reg | IF_SPI_WRITE_OPERATION_MASK;
151
152         /* You must give an even number of bytes to the SPU, even if it
153          * doesn't care about the last one.  */
154         BUG_ON(len & 0x1);
155
156         spu_transaction_init(card);
157
158         /* write SPU register index */
159         err = spi_write(card->spi, (u8 *)&reg_out, sizeof(u16));
160         if (err)
161                 goto out;
162
163         err = spi_write(card->spi, buf, len);
164
165 out:
166         spu_transaction_finish(card);
167         return err;
168 }
169
170 static inline int spu_write_u16(struct if_spi_card *card, u16 reg, u16 val)
171 {
172         return spu_write(card, reg, (u8 *)&val, sizeof(u16));
173 }
174
175 static inline int spu_write_u32(struct if_spi_card *card, u16 reg, u32 val)
176 {
177         /* The lower 16 bits are written first. */
178         u16 out[2];
179         out[0] = val & 0xffff;
180         out[1] = (val & 0xffff0000) >> 16;
181         return spu_write(card, reg, (u8 *)&out, sizeof(u32));
182 }
183
184 static inline int spu_reg_is_port_reg(u16 reg)
185 {
186         switch (reg) {
187         case IF_SPI_IO_RDWRPORT_REG:
188         case IF_SPI_CMD_RDWRPORT_REG:
189         case IF_SPI_DATA_RDWRPORT_REG:
190                 return 1;
191         default:
192                 return 0;
193         }
194 }
195
196 static int spu_read(struct if_spi_card *card, u16 reg, u8 *buf, int len)
197 {
198         unsigned int i, delay;
199         int err = 0;
200         u16 zero = 0;
201         u16 reg_out = reg | IF_SPI_READ_OPERATION_MASK;
202
203         /* You must take an even number of bytes from the SPU, even if you
204          * don't care about the last one.  */
205         BUG_ON(len & 0x1);
206
207         spu_transaction_init(card);
208
209         /* write SPU register index */
210         err = spi_write(card->spi, (u8 *)&reg_out, sizeof(u16));
211         if (err)
212                 goto out;
213
214         delay = spu_reg_is_port_reg(reg) ? card->spu_port_delay :
215                                                 card->spu_reg_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));
220                         if (err)
221                                 return err;
222                 }
223         } else {
224                 /* Busy-wait while the SPU fills the FIFO */
225                 ndelay(100 + (delay * 10));
226         }
227
228         /* read in data */
229         err = spi_read(card->spi, buf, len);
230
231 out:
232         spu_transaction_finish(card);
233         return err;
234 }
235
236 /* Read 16 bits from an SPI register */
237 static inline int spu_read_u16(struct if_spi_card *card, u16 reg, u16 *val)
238 {
239         return spu_read(card, reg, (u8 *)val, sizeof(u16));
240 }
241
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)
245 {
246         u16 buf[2];
247         int err;
248         err = spu_read(card, reg, (u8 *)buf, sizeof(u32));
249         if (!err)
250                 *val = buf[0] | (buf[1] << 16);
251         return err;
252 }
253
254 /* Keep reading 16 bits from an SPI register until you get the correct result.
255  *
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
259  *
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)
263 {
264         int err;
265         unsigned long timeout = jiffies + 5*HZ;
266         while (1) {
267                 u16 val;
268                 err = spu_read_u16(card, reg, &val);
269                 if (err)
270                         return err;
271                 if (target_mask) {
272                         if ((val & target_mask) == target)
273                                 return 0;
274                 } else {
275                         if (val)
276                                 return 0;
277                 }
278                 udelay(100);
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);
283                         return -ETIMEDOUT;
284                 }
285         }
286 }
287
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)
291 {
292         int err, try;
293         for (try = 0; try < 4; ++try) {
294                 u32 val = 0;
295                 err = spu_read_u32(card, reg, &val);
296                 if (err)
297                         return err;
298                 if (val == target)
299                         return 0;
300                 mdelay(100);
301         }
302         return -ETIMEDOUT;
303 }
304
305 static int spu_set_interrupt_mode(struct if_spi_card *card,
306                            int suppress_host_int,
307                            int auto_int)
308 {
309         int err = 0;
310
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);
315                 if (err)
316                         return err;
317         } else {
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);
324                 if (err)
325                         return err;
326         }
327
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. */
332         if (auto_int) {
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);
338                 if (err)
339                         return err;
340         } else {
341                 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG, 0);
342                 if (err)
343                         return err;
344         }
345         return err;
346 }
347
348 static int spu_get_chip_revision(struct if_spi_card *card,
349                                   u16 *card_id, u8 *card_rev)
350 {
351         int err = 0;
352         u32 dev_ctrl;
353         err = spu_read_u32(card, IF_SPI_DEVICEID_CTRL_REG, &dev_ctrl);
354         if (err)
355                 return err;
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);
358         return err;
359 }
360
361 static int spu_set_bus_mode(struct if_spi_card *card, u16 mode)
362 {
363         int err = 0;
364         u16 rval;
365         /* set bus mode */
366         err = spu_write_u16(card, IF_SPI_SPU_BUS_MODE_REG, mode);
367         if (err)
368                 return err;
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);
371         if (err)
372                 return err;
373         if (rval != mode) {
374                 lbs_pr_err("Can't read bus mode register.\n");
375                 return -EIO;
376         }
377         return 0;
378 }
379
380 static int spu_init(struct if_spi_card *card, int use_dummy_writes)
381 {
382         int err = 0;
383         u32 delay;
384
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);
392         if (err)
393                 return err;
394         card->spu_port_delay = 1000;
395         card->spu_reg_delay = 1000;
396         err = spu_read_u32(card, IF_SPI_DELAY_READ_REG, &delay);
397         if (err)
398                 return err;
399         card->spu_port_delay = delay & 0x0000ffff;
400         card->spu_reg_delay = (delay & 0xffff0000) >> 16;
401
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);
409                 if (err)
410                         return err;
411         }
412
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);
416         return err;
417 }
418
419 /*
420  * Firmware Loading
421  */
422
423 static int if_spi_prog_helper_firmware(struct if_spi_card *card)
424 {
425         int err = 0;
426         const struct firmware *firmware = NULL;
427         int bytes_remaining;
428         const u8 *fw;
429         u8 temp[HELPER_FW_LOAD_CHUNK_SZ];
430         struct spi_device *spi = card->spi;
431
432         lbs_deb_enter(LBS_DEB_SPI);
433
434         err = spu_set_interrupt_mode(card, 1, 0);
435         if (err)
436                 goto out;
437         /* Get helper firmware image */
438         err = request_firmware(&firmware, card->helper_fw_name, &spi->dev);
439         if (err) {
440                 lbs_pr_err("request_firmware failed with err = %d\n", err);
441                 goto out;
442         }
443         bytes_remaining = firmware->size;
444         fw = firmware->data;
445
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);
452                 if (err)
453                         goto release_firmware;
454
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);
458                 if (err)
459                         goto release_firmware;
460
461                 /* Feed the data into the command read/write port reg
462                  * in chunks of 64 bytes */
463                 memset(temp, 0, sizeof(temp));
464                 memcpy(temp, fw,
465                        min(bytes_remaining, HELPER_FW_LOAD_CHUNK_SZ));
466                 mdelay(10);
467                 err = spu_write(card, IF_SPI_CMD_RDWRPORT_REG,
468                                         temp, HELPER_FW_LOAD_CHUNK_SZ);
469                 if (err)
470                         goto release_firmware;
471
472                 /* Interrupt the boot code */
473                 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
474                 if (err)
475                         goto release_firmware;
476                 err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG,
477                                        IF_SPI_CIC_CMD_DOWNLOAD_OVER);
478                 if (err)
479                         goto release_firmware;
480                 bytes_remaining -= HELPER_FW_LOAD_CHUNK_SZ;
481                 fw += HELPER_FW_LOAD_CHUNK_SZ;
482         }
483
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);
488         if (err)
489                 goto release_firmware;
490         err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
491         if (err)
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;
496
497         lbs_deb_spi("waiting for helper to boot...\n");
498
499 release_firmware:
500         release_firmware(firmware);
501 out:
502         if (err)
503                 lbs_pr_err("failed to load helper firmware (err=%d)\n", err);
504         lbs_deb_leave_args(LBS_DEB_SPI, "err %d", err);
505         return err;
506 }
507
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,
511                                                 int *crc_err)
512 {
513         u16 len;
514         int err = 0;
515
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);
521         if (err) {
522                 lbs_pr_err("timed out waiting for host_int_status\n");
523                 return err;
524         }
525
526         /* Ask the device how many bytes of firmware it wants. */
527         err = spu_read_u16(card, IF_SPI_SCRATCH_1_REG, &len);
528         if (err)
529                 return err;
530
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);
535                 return -EIO;
536         }
537         if (len & 0x1) {
538                 lbs_deb_spi("%s: crc error\n", __func__);
539                 len &= ~0x1;
540                 *crc_err = 1;
541         } else
542                 *crc_err = 0;
543
544         return len;
545 }
546
547 static int if_spi_prog_main_firmware(struct if_spi_card *card)
548 {
549         int len, prev_len;
550         int bytes, crc_err = 0, err = 0;
551         const struct firmware *firmware = NULL;
552         const u8 *fw;
553         struct spi_device *spi = card->spi;
554         u16 num_crc_errs;
555
556         lbs_deb_enter(LBS_DEB_SPI);
557
558         err = spu_set_interrupt_mode(card, 1, 0);
559         if (err)
560                 goto out;
561
562         /* Get firmware image */
563         err = request_firmware(&firmware, card->main_fw_name, &spi->dev);
564         if (err) {
565                 lbs_pr_err("%s: can't get firmware '%s' from kernel. "
566                         "err = %d\n", __func__, card->main_fw_name, err);
567                 goto out;
568         }
569
570         err = spu_wait_for_u16(card, IF_SPI_SCRATCH_1_REG, 0, 0);
571         if (err) {
572                 lbs_pr_err("%s: timed out waiting for initial "
573                            "scratch reg = 0\n", __func__);
574                 goto release_firmware;
575         }
576
577         num_crc_errs = 0;
578         prev_len = 0;
579         bytes = firmware->size;
580         fw = firmware->data;
581         while ((len = if_spi_prog_main_firmware_check_len(card, &crc_err))) {
582                 if (len < 0) {
583                         err = len;
584                         goto release_firmware;
585                 }
586                 if (bytes < 0) {
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");
591                         break;
592                 }
593                 if (crc_err) {
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");
598                                 err = -EIO;
599                                 goto release_firmware;
600                         }
601                 } else {
602                         /* Previous transfer succeeded. Advance counters. */
603                         bytes -= prev_len;
604                         fw += prev_len;
605                 }
606                 if (bytes < len) {
607                         memset(card->cmd_buffer, 0, len);
608                         memcpy(card->cmd_buffer, fw, bytes);
609                 } else
610                         memcpy(card->cmd_buffer, fw, len);
611
612                 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
613                 if (err)
614                         goto release_firmware;
615                 err = spu_write(card, IF_SPI_CMD_RDWRPORT_REG,
616                                 card->cmd_buffer, len);
617                 if (err)
618                         goto release_firmware;
619                 err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG ,
620                                         IF_SPI_CIC_CMD_DOWNLOAD_OVER);
621                 if (err)
622                         goto release_firmware;
623                 prev_len = len;
624         }
625         if (bytes > prev_len) {
626                 lbs_pr_err("firmware load wants fewer bytes than "
627                            "we have to offer.\n");
628         }
629
630         /* Confirm firmware download */
631         err = spu_wait_for_u32(card, IF_SPI_SCRATCH_4_REG,
632                                         SUCCESSFUL_FW_DOWNLOAD_MAGIC);
633         if (err) {
634                 lbs_pr_err("failed to confirm the firmware download\n");
635                 goto release_firmware;
636         }
637
638 release_firmware:
639         release_firmware(firmware);
640
641 out:
642         if (err)
643                 lbs_pr_err("failed to load firmware (err=%d)\n", err);
644         lbs_deb_leave_args(LBS_DEB_SPI, "err %d", err);
645         return err;
646 }
647
648 /*
649  * SPI Transfer Thread
650  *
651  * The SPI thread handles all SPI transfers, so there is no need for a lock.
652  */
653
654 /* Move a command from the card to the host */
655 static int if_spi_c2h_cmd(struct if_spi_card *card)
656 {
657         struct lbs_private *priv = card->priv;
658         unsigned long flags;
659         int err = 0;
660         u16 len;
661         u8 i;
662
663         /* We need a buffer big enough to handle whatever people send to
664          * hw_host_to_card */
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);
667
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);
672
673         lbs_deb_enter(LBS_DEB_SPI);
674
675         /* How many bytes are there to read? */
676         err = spu_read_u16(card, IF_SPI_SCRATCH_2_REG, &len);
677         if (err)
678                 goto out;
679         if (!len) {
680                 lbs_pr_err("%s: error: card has no data for host\n",
681                            __func__);
682                 err = -EINVAL;
683                 goto out;
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);
688                 err = -EINVAL;
689                 goto out;
690         }
691
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));
695         if (err)
696                 goto out;
697
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);
705
706 out:
707         if (err)
708                 lbs_pr_err("%s: err=%d\n", __func__, err);
709         lbs_deb_leave(LBS_DEB_SPI);
710         return err;
711 }
712
713 /* Move data from the card to the host */
714 static int if_spi_c2h_data(struct if_spi_card *card)
715 {
716         struct sk_buff *skb;
717         char *data;
718         u16 len;
719         int err = 0;
720
721         lbs_deb_enter(LBS_DEB_SPI);
722
723         /* How many bytes are there to read? */
724         err = spu_read_u16(card, IF_SPI_SCRATCH_1_REG, &len);
725         if (err)
726                 goto out;
727         if (!len) {
728                 lbs_pr_err("%s: error: card has no data for host\n",
729                            __func__);
730                 err = -EINVAL;
731                 goto out;
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);
736                 err = -EINVAL;
737                 goto out;
738         }
739
740         /* TODO: should we allocate a smaller skb if we have less data? */
741         skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE);
742         if (!skb) {
743                 err = -ENOBUFS;
744                 goto out;
745         }
746         skb_reserve(skb, IPFIELD_ALIGN_OFFSET);
747         data = skb_put(skb, len);
748
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));
751         if (err)
752                 goto free_skb;
753
754         /* pass the SKB to libertas */
755         err = lbs_process_rxed_packet(card->priv, skb);
756         if (err)
757                 goto free_skb;
758
759         /* success */
760         goto out;
761
762 free_skb:
763         dev_kfree_skb(skb);
764 out:
765         if (err)
766                 lbs_pr_err("%s: err=%d\n", __func__, err);
767         lbs_deb_leave(LBS_DEB_SPI);
768         return err;
769 }
770
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)
774 {
775         int err = 0;
776         u16 int_type, port_reg;
777
778         switch (type) {
779         case MVMS_DAT:
780                 int_type = IF_SPI_CIC_TX_DOWNLOAD_OVER;
781                 port_reg = IF_SPI_DATA_RDWRPORT_REG;
782                 break;
783         case MVMS_CMD:
784                 int_type = IF_SPI_CIC_CMD_DOWNLOAD_OVER;
785                 port_reg = IF_SPI_CMD_RDWRPORT_REG;
786                 break;
787         default:
788                 lbs_pr_err("can't transfer buffer of type %d\n", type);
789                 err = -EINVAL;
790                 goto out;
791         }
792
793         /* Write the data to the card */
794         err = spu_write(card, port_reg, packet->buffer, packet->blen);
795         if (err)
796                 goto out;
797
798 out:
799         kfree(packet);
800
801         if (err)
802                 lbs_pr_err("%s: error %d\n", __func__, err);
803 }
804
805 /* Inform the host about a card event */
806 static void if_spi_e2h(struct if_spi_card *card)
807 {
808         int err = 0;
809         unsigned long flags;
810         u32 cause;
811         struct lbs_private *priv = card->priv;
812
813         err = spu_read_u32(card, IF_SPI_SCRATCH_3_REG, &cause);
814         if (err)
815                 goto out;
816
817         spin_lock_irqsave(&priv->driver_lock, flags);
818         lbs_queue_event(priv, cause & 0xff);
819         spin_unlock_irqrestore(&priv->driver_lock, flags);
820
821 out:
822         if (err)
823                 lbs_pr_err("%s: error %d\n", __func__, err);
824 }
825
826 static int lbs_spi_thread(void *data)
827 {
828         int err;
829         struct if_spi_card *card = data;
830         u16 hiStatus;
831         unsigned long flags;
832         struct if_spi_packet *packet;
833
834         while (1) {
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.
839                  */
840                 do {
841                         err = down_interruptible(&card->spi_ready);
842                         if (!card->run_thread) {
843                                 up(&card->spi_thread_terminated);
844                                 do_exit(0);
845                         }
846                 } while (err == EINTR);
847
848                 /* Read the host interrupt status register to see what we
849                  * can do. */
850                 err = spu_read_u16(card, IF_SPI_HOST_INT_STATUS_REG,
851                                         &hiStatus);
852                 if (err) {
853                         lbs_pr_err("I/O error\n");
854                         goto err;
855                 }
856
857                 if (hiStatus & IF_SPI_HIST_CMD_UPLOAD_RDY)
858                         err = if_spi_c2h_cmd(card);
859                         if (err)
860                                 goto err;
861                 if (hiStatus & IF_SPI_HIST_RX_UPLOAD_RDY)
862                         err = if_spi_c2h_data(card);
863                         if (err)
864                                 goto err;
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
870                          * command.
871                          */
872                         lbs_host_to_card_done(card->priv);
873
874                         /* Do we have any command packets from the host to
875                          * send? */
876                         packet = NULL;
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);
882                         }
883                         spin_unlock_irqrestore(&card->buffer_lock, flags);
884
885                         if (packet)
886                                 if_spi_h2c(card, packet, MVMS_CMD);
887                 }
888                 if (hiStatus & IF_SPI_HIST_TX_DOWNLOAD_RDY) {
889                         /* Do we have any data packets from the host to
890                          * send? */
891                         packet = NULL;
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);
897                         }
898                         spin_unlock_irqrestore(&card->buffer_lock, flags);
899
900                         if (packet)
901                                 if_spi_h2c(card, packet, MVMS_DAT);
902                 }
903                 if (hiStatus & IF_SPI_HIST_CARD_EVENT)
904                         if_spi_e2h(card);
905
906 err:
907                 if (err)
908                         lbs_pr_err("%s: got error %d\n", __func__, err);
909         }
910 }
911
912 /* Block until lbs_spi_thread thread has terminated */
913 static void if_spi_terminate_spi_thread(struct if_spi_card *card)
914 {
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);
920 }
921
922 /*
923  * Host to Card
924  *
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)
929 {
930         int err = 0;
931         unsigned long flags;
932         struct if_spi_card *card = priv->card;
933         struct if_spi_packet *packet;
934         u16 blen;
935
936         lbs_deb_enter_args(LBS_DEB_SPI, "type %d, bytes %d", type, nb);
937
938         if (nb == 0) {
939                 lbs_pr_err("%s: invalid size requested: %d\n", __func__, nb);
940                 err = -EINVAL;
941                 goto out;
942         }
943         blen = ALIGN(nb, 4);
944         packet = kzalloc(sizeof(struct if_spi_packet) + blen, GFP_ATOMIC);
945         if (!packet) {
946                 err = -ENOMEM;
947                 goto out;
948         }
949         packet->blen = blen;
950         memcpy(packet->buffer, buf, nb);
951         memset(packet->buffer + nb, 0, blen - nb);
952
953         switch (type) {
954         case MVMS_CMD:
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);
959                 break;
960         case MVMS_DAT:
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);
965                 break;
966         default:
967                 lbs_pr_err("can't transfer buffer of type %d", type);
968                 err = -EINVAL;
969                 break;
970         }
971
972         /* Wake up the spi thread */
973         up(&card->spi_ready);
974 out:
975         lbs_deb_leave_args(LBS_DEB_SPI, "err=%d", err);
976         return err;
977 }
978
979 /*
980  * Host Interrupts
981  *
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.
984  */
985 static irqreturn_t if_spi_host_interrupt(int irq, void *dev_id)
986 {
987         struct if_spi_card *card = dev_id;
988
989         up(&card->spi_ready);
990         return IRQ_HANDLED;
991 }
992
993 /*
994  * SPI callbacks
995  */
996
997 static int if_spi_calculate_fw_names(u16 card_id,
998                               char *helper_fw, char *main_fw)
999 {
1000         int i;
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)
1003                         break;
1004         }
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;
1008         }
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);
1013         return 0;
1014 }
1015
1016 static int __devinit if_spi_probe(struct spi_device *spi)
1017 {
1018         struct if_spi_card *card;
1019         struct lbs_private *priv = NULL;
1020         struct libertas_spi_platform_data *pdata = spi->dev.platform_data;
1021         int err = 0;
1022         u32 scratch;
1023
1024         lbs_deb_enter(LBS_DEB_SPI);
1025
1026         if (!pdata) {
1027                 err = -EINVAL;
1028                 goto out;
1029         }
1030
1031         if (pdata->setup) {
1032                 err = pdata->setup(spi);
1033                 if (err)
1034                         goto out;
1035         }
1036
1037         /* Allocate card structure to represent this specific device */
1038         card = kzalloc(sizeof(struct if_spi_card), GFP_KERNEL);
1039         if (!card) {
1040                 err = -ENOMEM;
1041                 goto out;
1042         }
1043         spi_set_drvdata(spi, card);
1044         card->pdata = pdata;
1045         card->spi = spi;
1046         card->gpio_cs = pdata->gpio_cs;
1047         card->prev_xfer_time = jiffies;
1048
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);
1054
1055         /* set up GPIO CS line. TODO: use  regular CS line */
1056         err = gpio_request(card->gpio_cs, "if_spi_gpio_chip_select");
1057         if (err)
1058                 goto free_card;
1059         err = gpio_direction_output(card->gpio_cs, 1);
1060         if (err)
1061                 goto free_gpio;
1062
1063         /* Initialize the SPI Interface Unit */
1064         err = spu_init(card, pdata->use_dummy_writes);
1065         if (err)
1066                 goto free_gpio;
1067         err = spu_get_chip_revision(card, &card->card_id, &card->card_rev);
1068         if (err)
1069                 goto free_gpio;
1070
1071         /* Firmware load */
1072         err = spu_read_u32(card, IF_SPI_SCRATCH_4_REG, &scratch);
1073         if (err)
1074                 goto free_gpio;
1075         if (scratch == SUCCESSFUL_FW_DOWNLOAD_MAGIC)
1076                 lbs_deb_spi("Firmware is already loaded for "
1077                             "Marvell WLAN 802.11 adapter\n");
1078         else {
1079                 err = if_spi_calculate_fw_names(card->card_id,
1080                                 card->helper_fw_name, card->main_fw_name);
1081                 if (err)
1082                         goto free_gpio;
1083
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,
1090                                 spi->max_speed_hz);
1091                 err = if_spi_prog_helper_firmware(card);
1092                 if (err)
1093                         goto free_gpio;
1094                 err = if_spi_prog_main_firmware(card);
1095                 if (err)
1096                         goto free_gpio;
1097                 lbs_deb_spi("loaded FW for Marvell WLAN 802.11 adapter\n");
1098         }
1099
1100         err = spu_set_interrupt_mode(card, 0, 1);
1101         if (err)
1102                 goto free_gpio;
1103
1104         /* Register our card with libertas.
1105          * This will call alloc_etherdev */
1106         priv = lbs_add_card(card, &spi->dev);
1107         if (!priv) {
1108                 err = -ENOMEM;
1109                 goto free_gpio;
1110         }
1111         card->priv = priv;
1112         priv->card = card;
1113         priv->hw_host_to_card = if_spi_host_to_card;
1114         priv->fw_ready = 1;
1115         priv->ps_supported = 1;
1116
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);
1124                 goto remove_card;
1125         }
1126         err = request_irq(spi->irq, if_spi_host_interrupt,
1127                         IRQF_TRIGGER_FALLING, "libertas_spi", card);
1128         if (err) {
1129                 lbs_pr_err("can't get host irq line-- request_irq failed\n");
1130                 goto terminate_thread;
1131         }
1132
1133         /* Start the card.
1134          * This will call register_netdev, and we'll start
1135          * getting interrupts... */
1136         err = lbs_start_card(priv);
1137         if (err)
1138                 goto release_irq;
1139
1140         lbs_deb_spi("Finished initializing WLAN module.\n");
1141
1142         /* successful exit */
1143         goto out;
1144
1145 release_irq:
1146         free_irq(spi->irq, card);
1147 terminate_thread:
1148         if_spi_terminate_spi_thread(card);
1149 remove_card:
1150         lbs_remove_card(priv); /* will call free_netdev */
1151 free_gpio:
1152         gpio_free(card->gpio_cs);
1153 free_card:
1154         free_if_spi_card(card);
1155 out:
1156         lbs_deb_leave_args(LBS_DEB_SPI, "err %d\n", err);
1157         return err;
1158 }
1159
1160 static int __devexit libertas_spi_remove(struct spi_device *spi)
1161 {
1162         struct if_spi_card *card = spi_get_drvdata(spi);
1163         struct lbs_private *priv = card->priv;
1164
1165         lbs_deb_spi("libertas_spi_remove\n");
1166         lbs_deb_enter(LBS_DEB_SPI);
1167         priv->surpriseremoved = 1;
1168
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);
1178         return 0;
1179 }
1180
1181 static struct spi_driver libertas_spi_driver = {
1182         .probe  = if_spi_probe,
1183         .remove = __devexit_p(libertas_spi_remove),
1184         .driver = {
1185                 .name   = "libertas_spi",
1186                 .bus    = &spi_bus_type,
1187                 .owner  = THIS_MODULE,
1188         },
1189 };
1190
1191 /*
1192  * Module functions
1193  */
1194
1195 static int __init if_spi_init_module(void)
1196 {
1197         int ret = 0;
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);
1202         return ret;
1203 }
1204
1205 static void __exit if_spi_exit_module(void)
1206 {
1207         lbs_deb_enter(LBS_DEB_SPI);
1208         spi_unregister_driver(&libertas_spi_driver);
1209         lbs_deb_leave(LBS_DEB_SPI);
1210 }
1211
1212 module_init(if_spi_init_module);
1213 module_exit(if_spi_exit_module);
1214
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");