Merge branch 'slabh' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/misc
[sfrench/cifs-2.6.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/kthread.h>
24 #include <linux/list.h>
25 #include <linux/netdevice.h>
26 #include <linux/semaphore.h>
27 #include <linux/slab.h>
28 #include <linux/spi/libertas_spi.h>
29 #include <linux/spi/spi.h>
30
31 #include "host.h"
32 #include "decl.h"
33 #include "defs.h"
34 #include "dev.h"
35 #include "if_spi.h"
36
37 struct if_spi_card {
38         struct spi_device               *spi;
39         struct lbs_private              *priv;
40         struct libertas_spi_platform_data *pdata;
41
42         char                            helper_fw_name[IF_SPI_FW_NAME_MAX];
43         char                            main_fw_name[IF_SPI_FW_NAME_MAX];
44
45         /* The card ID and card revision, as reported by the hardware. */
46         u16                             card_id;
47         u8                              card_rev;
48
49         /* The last time that we initiated an SPU operation */
50         unsigned long                   prev_xfer_time;
51
52         int                             use_dummy_writes;
53         unsigned long                   spu_port_delay;
54         unsigned long                   spu_reg_delay;
55
56         /* Handles all SPI communication (except for FW load) */
57         struct task_struct              *spi_thread;
58         int                             run_thread;
59
60         /* Used to wake up the spi_thread */
61         struct semaphore                spi_ready;
62         struct semaphore                spi_thread_terminated;
63
64         u8                              cmd_buffer[IF_SPI_CMD_BUF_SIZE];
65 };
66
67 static void free_if_spi_card(struct if_spi_card *card)
68 {
69         spi_set_drvdata(card->spi, NULL);
70         kfree(card);
71 }
72
73 static struct chip_ident chip_id_to_device_name[] = {
74         { .chip_id = 0x04, .name = 8385 },
75         { .chip_id = 0x0b, .name = 8686 },
76 };
77
78 /*
79  * SPI Interface Unit Routines
80  *
81  * The SPU sits between the host and the WLAN module.
82  * All communication with the firmware is through SPU transactions.
83  *
84  * First we have to put a SPU register name on the bus. Then we can
85  * either read from or write to that register.
86  *
87  */
88
89 static void spu_transaction_init(struct if_spi_card *card)
90 {
91         if (!time_after(jiffies, card->prev_xfer_time + 1)) {
92                 /* Unfortunately, the SPU requires a delay between successive
93                  * transactions. If our last transaction was more than a jiffy
94                  * ago, we have obviously already delayed enough.
95                  * If not, we have to busy-wait to be on the safe side. */
96                 ndelay(400);
97         }
98 }
99
100 static void spu_transaction_finish(struct if_spi_card *card)
101 {
102         card->prev_xfer_time = jiffies;
103 }
104
105 /* Write out a byte buffer to an SPI register,
106  * using a series of 16-bit transfers. */
107 static int spu_write(struct if_spi_card *card, u16 reg, const u8 *buf, int len)
108 {
109         int err = 0;
110         __le16 reg_out = cpu_to_le16(reg | IF_SPI_WRITE_OPERATION_MASK);
111         struct spi_message m;
112         struct spi_transfer reg_trans;
113         struct spi_transfer data_trans;
114
115         spi_message_init(&m);
116         memset(&reg_trans, 0, sizeof(reg_trans));
117         memset(&data_trans, 0, sizeof(data_trans));
118
119         /* You must give an even number of bytes to the SPU, even if it
120          * doesn't care about the last one.  */
121         BUG_ON(len & 0x1);
122
123         spu_transaction_init(card);
124
125         /* write SPU register index */
126         reg_trans.tx_buf = &reg_out;
127         reg_trans.len = sizeof(reg_out);
128
129         data_trans.tx_buf = buf;
130         data_trans.len = len;
131
132         spi_message_add_tail(&reg_trans, &m);
133         spi_message_add_tail(&data_trans, &m);
134
135         err = spi_sync(card->spi, &m);
136         spu_transaction_finish(card);
137         return err;
138 }
139
140 static inline int spu_write_u16(struct if_spi_card *card, u16 reg, u16 val)
141 {
142         __le16 buff;
143
144         buff = cpu_to_le16(val);
145         return spu_write(card, reg, (u8 *)&buff, sizeof(u16));
146 }
147
148 static inline int spu_reg_is_port_reg(u16 reg)
149 {
150         switch (reg) {
151         case IF_SPI_IO_RDWRPORT_REG:
152         case IF_SPI_CMD_RDWRPORT_REG:
153         case IF_SPI_DATA_RDWRPORT_REG:
154                 return 1;
155         default:
156                 return 0;
157         }
158 }
159
160 static int spu_read(struct if_spi_card *card, u16 reg, u8 *buf, int len)
161 {
162         unsigned int delay;
163         int err = 0;
164         __le16 reg_out = cpu_to_le16(reg | IF_SPI_READ_OPERATION_MASK);
165         struct spi_message m;
166         struct spi_transfer reg_trans;
167         struct spi_transfer dummy_trans;
168         struct spi_transfer data_trans;
169
170         /* You must take an even number of bytes from the SPU, even if you
171          * don't care about the last one.  */
172         BUG_ON(len & 0x1);
173
174         spu_transaction_init(card);
175
176         spi_message_init(&m);
177         memset(&reg_trans, 0, sizeof(reg_trans));
178         memset(&dummy_trans, 0, sizeof(dummy_trans));
179         memset(&data_trans, 0, sizeof(data_trans));
180
181         /* write SPU register index */
182         reg_trans.tx_buf = &reg_out;
183         reg_trans.len = sizeof(reg_out);
184         spi_message_add_tail(&reg_trans, &m);
185
186         delay = spu_reg_is_port_reg(reg) ? card->spu_port_delay :
187                                                 card->spu_reg_delay;
188         if (card->use_dummy_writes) {
189                 /* Clock in dummy cycles while the SPU fills the FIFO */
190                 dummy_trans.len = delay / 8;
191                 spi_message_add_tail(&dummy_trans, &m);
192         } else {
193                 /* Busy-wait while the SPU fills the FIFO */
194                 reg_trans.delay_usecs =
195                         DIV_ROUND_UP((100 + (delay * 10)), 1000);
196         }
197
198         /* read in data */
199         data_trans.rx_buf = buf;
200         data_trans.len = len;
201         spi_message_add_tail(&data_trans, &m);
202
203         err = spi_sync(card->spi, &m);
204         spu_transaction_finish(card);
205         return err;
206 }
207
208 /* Read 16 bits from an SPI register */
209 static inline int spu_read_u16(struct if_spi_card *card, u16 reg, u16 *val)
210 {
211         __le16 buf;
212         int ret;
213
214         ret = spu_read(card, reg, (u8 *)&buf, sizeof(buf));
215         if (ret == 0)
216                 *val = le16_to_cpup(&buf);
217         return ret;
218 }
219
220 /* Read 32 bits from an SPI register.
221  * The low 16 bits are read first. */
222 static int spu_read_u32(struct if_spi_card *card, u16 reg, u32 *val)
223 {
224         __le32 buf;
225         int err;
226
227         err = spu_read(card, reg, (u8 *)&buf, sizeof(buf));
228         if (!err)
229                 *val = le32_to_cpup(&buf);
230         return err;
231 }
232
233 /* Keep reading 16 bits from an SPI register until you get the correct result.
234  *
235  * If mask = 0, the correct result is any non-zero number.
236  * If mask != 0, the correct result is any number where
237  * number & target_mask == target
238  *
239  * Returns -ETIMEDOUT if a second passes without the correct result. */
240 static int spu_wait_for_u16(struct if_spi_card *card, u16 reg,
241                         u16 target_mask, u16 target)
242 {
243         int err;
244         unsigned long timeout = jiffies + 5*HZ;
245         while (1) {
246                 u16 val;
247                 err = spu_read_u16(card, reg, &val);
248                 if (err)
249                         return err;
250                 if (target_mask) {
251                         if ((val & target_mask) == target)
252                                 return 0;
253                 } else {
254                         if (val)
255                                 return 0;
256                 }
257                 udelay(100);
258                 if (time_after(jiffies, timeout)) {
259                         lbs_pr_err("%s: timeout with val=%02x, "
260                                "target_mask=%02x, target=%02x\n",
261                                __func__, val, target_mask, target);
262                         return -ETIMEDOUT;
263                 }
264         }
265 }
266
267 /* Read 16 bits from an SPI register until you receive a specific value.
268  * Returns -ETIMEDOUT if a 4 tries pass without success. */
269 static int spu_wait_for_u32(struct if_spi_card *card, u32 reg, u32 target)
270 {
271         int err, try;
272         for (try = 0; try < 4; ++try) {
273                 u32 val = 0;
274                 err = spu_read_u32(card, reg, &val);
275                 if (err)
276                         return err;
277                 if (val == target)
278                         return 0;
279                 mdelay(100);
280         }
281         return -ETIMEDOUT;
282 }
283
284 static int spu_set_interrupt_mode(struct if_spi_card *card,
285                            int suppress_host_int,
286                            int auto_int)
287 {
288         int err = 0;
289
290         /* We can suppress a host interrupt by clearing the appropriate
291          * bit in the "host interrupt status mask" register */
292         if (suppress_host_int) {
293                 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG, 0);
294                 if (err)
295                         return err;
296         } else {
297                 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG,
298                               IF_SPI_HISM_TX_DOWNLOAD_RDY |
299                               IF_SPI_HISM_RX_UPLOAD_RDY |
300                               IF_SPI_HISM_CMD_DOWNLOAD_RDY |
301                               IF_SPI_HISM_CARDEVENT |
302                               IF_SPI_HISM_CMD_UPLOAD_RDY);
303                 if (err)
304                         return err;
305         }
306
307         /* If auto-interrupts are on, the completion of certain transactions
308          * will trigger an interrupt automatically. If auto-interrupts
309          * are off, we need to set the "Card Interrupt Cause" register to
310          * trigger a card interrupt. */
311         if (auto_int) {
312                 err = spu_write_u16(card, IF_SPI_HOST_INT_CTRL_REG,
313                                 IF_SPI_HICT_TX_DOWNLOAD_OVER_AUTO |
314                                 IF_SPI_HICT_RX_UPLOAD_OVER_AUTO |
315                                 IF_SPI_HICT_CMD_DOWNLOAD_OVER_AUTO |
316                                 IF_SPI_HICT_CMD_UPLOAD_OVER_AUTO);
317                 if (err)
318                         return err;
319         } else {
320                 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG, 0);
321                 if (err)
322                         return err;
323         }
324         return err;
325 }
326
327 static int spu_get_chip_revision(struct if_spi_card *card,
328                                   u16 *card_id, u8 *card_rev)
329 {
330         int err = 0;
331         u32 dev_ctrl;
332         err = spu_read_u32(card, IF_SPI_DEVICEID_CTRL_REG, &dev_ctrl);
333         if (err)
334                 return err;
335         *card_id = IF_SPI_DEVICEID_CTRL_REG_TO_CARD_ID(dev_ctrl);
336         *card_rev = IF_SPI_DEVICEID_CTRL_REG_TO_CARD_REV(dev_ctrl);
337         return err;
338 }
339
340 static int spu_set_bus_mode(struct if_spi_card *card, u16 mode)
341 {
342         int err = 0;
343         u16 rval;
344         /* set bus mode */
345         err = spu_write_u16(card, IF_SPI_SPU_BUS_MODE_REG, mode);
346         if (err)
347                 return err;
348         /* Check that we were able to read back what we just wrote. */
349         err = spu_read_u16(card, IF_SPI_SPU_BUS_MODE_REG, &rval);
350         if (err)
351                 return err;
352         if ((rval & 0xF) != mode) {
353                 lbs_pr_err("Can't read bus mode register.\n");
354                 return -EIO;
355         }
356         return 0;
357 }
358
359 static int spu_init(struct if_spi_card *card, int use_dummy_writes)
360 {
361         int err = 0;
362         u32 delay;
363
364         /* We have to start up in timed delay mode so that we can safely
365          * read the Delay Read Register. */
366         card->use_dummy_writes = 0;
367         err = spu_set_bus_mode(card,
368                                 IF_SPI_BUS_MODE_SPI_CLOCK_PHASE_RISING |
369                                 IF_SPI_BUS_MODE_DELAY_METHOD_TIMED |
370                                 IF_SPI_BUS_MODE_16_BIT_ADDRESS_16_BIT_DATA);
371         if (err)
372                 return err;
373         card->spu_port_delay = 1000;
374         card->spu_reg_delay = 1000;
375         err = spu_read_u32(card, IF_SPI_DELAY_READ_REG, &delay);
376         if (err)
377                 return err;
378         card->spu_port_delay = delay & 0x0000ffff;
379         card->spu_reg_delay = (delay & 0xffff0000) >> 16;
380
381         /* If dummy clock delay mode has been requested, switch to it now */
382         if (use_dummy_writes) {
383                 card->use_dummy_writes = 1;
384                 err = spu_set_bus_mode(card,
385                                 IF_SPI_BUS_MODE_SPI_CLOCK_PHASE_RISING |
386                                 IF_SPI_BUS_MODE_DELAY_METHOD_DUMMY_CLOCK |
387                                 IF_SPI_BUS_MODE_16_BIT_ADDRESS_16_BIT_DATA);
388                 if (err)
389                         return err;
390         }
391
392         lbs_deb_spi("Initialized SPU unit. "
393                     "spu_port_delay=0x%04lx, spu_reg_delay=0x%04lx\n",
394                     card->spu_port_delay, card->spu_reg_delay);
395         return err;
396 }
397
398 /*
399  * Firmware Loading
400  */
401
402 static int if_spi_prog_helper_firmware(struct if_spi_card *card)
403 {
404         int err = 0;
405         const struct firmware *firmware = NULL;
406         int bytes_remaining;
407         const u8 *fw;
408         u8 temp[HELPER_FW_LOAD_CHUNK_SZ];
409         struct spi_device *spi = card->spi;
410
411         lbs_deb_enter(LBS_DEB_SPI);
412
413         err = spu_set_interrupt_mode(card, 1, 0);
414         if (err)
415                 goto out;
416         /* Get helper firmware image */
417         err = request_firmware(&firmware, card->helper_fw_name, &spi->dev);
418         if (err) {
419                 lbs_pr_err("request_firmware failed with err = %d\n", err);
420                 goto out;
421         }
422         bytes_remaining = firmware->size;
423         fw = firmware->data;
424
425         /* Load helper firmware image */
426         while (bytes_remaining > 0) {
427                 /* Scratch pad 1 should contain the number of bytes we
428                  * want to download to the firmware */
429                 err = spu_write_u16(card, IF_SPI_SCRATCH_1_REG,
430                                         HELPER_FW_LOAD_CHUNK_SZ);
431                 if (err)
432                         goto release_firmware;
433
434                 err = spu_wait_for_u16(card, IF_SPI_HOST_INT_STATUS_REG,
435                                         IF_SPI_HIST_CMD_DOWNLOAD_RDY,
436                                         IF_SPI_HIST_CMD_DOWNLOAD_RDY);
437                 if (err)
438                         goto release_firmware;
439
440                 /* Feed the data into the command read/write port reg
441                  * in chunks of 64 bytes */
442                 memset(temp, 0, sizeof(temp));
443                 memcpy(temp, fw,
444                        min(bytes_remaining, HELPER_FW_LOAD_CHUNK_SZ));
445                 mdelay(10);
446                 err = spu_write(card, IF_SPI_CMD_RDWRPORT_REG,
447                                         temp, HELPER_FW_LOAD_CHUNK_SZ);
448                 if (err)
449                         goto release_firmware;
450
451                 /* Interrupt the boot code */
452                 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
453                 if (err)
454                         goto release_firmware;
455                 err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG,
456                                        IF_SPI_CIC_CMD_DOWNLOAD_OVER);
457                 if (err)
458                         goto release_firmware;
459                 bytes_remaining -= HELPER_FW_LOAD_CHUNK_SZ;
460                 fw += HELPER_FW_LOAD_CHUNK_SZ;
461         }
462
463         /* Once the helper / single stage firmware download is complete,
464          * write 0 to scratch pad 1 and interrupt the
465          * bootloader. This completes the helper download. */
466         err = spu_write_u16(card, IF_SPI_SCRATCH_1_REG, FIRMWARE_DNLD_OK);
467         if (err)
468                 goto release_firmware;
469         err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
470         if (err)
471                 goto release_firmware;
472         err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG,
473                                 IF_SPI_CIC_CMD_DOWNLOAD_OVER);
474                 goto release_firmware;
475
476         lbs_deb_spi("waiting for helper to boot...\n");
477
478 release_firmware:
479         release_firmware(firmware);
480 out:
481         if (err)
482                 lbs_pr_err("failed to load helper firmware (err=%d)\n", err);
483         lbs_deb_leave_args(LBS_DEB_SPI, "err %d", err);
484         return err;
485 }
486
487 /* Returns the length of the next packet the firmware expects us to send
488  * Sets crc_err if the previous transfer had a CRC error. */
489 static int if_spi_prog_main_firmware_check_len(struct if_spi_card *card,
490                                                 int *crc_err)
491 {
492         u16 len;
493         int err = 0;
494
495         /* wait until the host interrupt status register indicates
496          * that we are ready to download */
497         err = spu_wait_for_u16(card, IF_SPI_HOST_INT_STATUS_REG,
498                                 IF_SPI_HIST_CMD_DOWNLOAD_RDY,
499                                 IF_SPI_HIST_CMD_DOWNLOAD_RDY);
500         if (err) {
501                 lbs_pr_err("timed out waiting for host_int_status\n");
502                 return err;
503         }
504
505         /* Ask the device how many bytes of firmware it wants. */
506         err = spu_read_u16(card, IF_SPI_SCRATCH_1_REG, &len);
507         if (err)
508                 return err;
509
510         if (len > IF_SPI_CMD_BUF_SIZE) {
511                 lbs_pr_err("firmware load device requested a larger "
512                            "tranfer than we are prepared to "
513                            "handle. (len = %d)\n", len);
514                 return -EIO;
515         }
516         if (len & 0x1) {
517                 lbs_deb_spi("%s: crc error\n", __func__);
518                 len &= ~0x1;
519                 *crc_err = 1;
520         } else
521                 *crc_err = 0;
522
523         return len;
524 }
525
526 static int if_spi_prog_main_firmware(struct if_spi_card *card)
527 {
528         int len, prev_len;
529         int bytes, crc_err = 0, err = 0;
530         const struct firmware *firmware = NULL;
531         const u8 *fw;
532         struct spi_device *spi = card->spi;
533         u16 num_crc_errs;
534
535         lbs_deb_enter(LBS_DEB_SPI);
536
537         err = spu_set_interrupt_mode(card, 1, 0);
538         if (err)
539                 goto out;
540
541         /* Get firmware image */
542         err = request_firmware(&firmware, card->main_fw_name, &spi->dev);
543         if (err) {
544                 lbs_pr_err("%s: can't get firmware '%s' from kernel. "
545                         "err = %d\n", __func__, card->main_fw_name, err);
546                 goto out;
547         }
548
549         err = spu_wait_for_u16(card, IF_SPI_SCRATCH_1_REG, 0, 0);
550         if (err) {
551                 lbs_pr_err("%s: timed out waiting for initial "
552                            "scratch reg = 0\n", __func__);
553                 goto release_firmware;
554         }
555
556         num_crc_errs = 0;
557         prev_len = 0;
558         bytes = firmware->size;
559         fw = firmware->data;
560         while ((len = if_spi_prog_main_firmware_check_len(card, &crc_err))) {
561                 if (len < 0) {
562                         err = len;
563                         goto release_firmware;
564                 }
565                 if (bytes < 0) {
566                         /* If there are no more bytes left, we would normally
567                          * expect to have terminated with len = 0 */
568                         lbs_pr_err("Firmware load wants more bytes "
569                                    "than we have to offer.\n");
570                         break;
571                 }
572                 if (crc_err) {
573                         /* Previous transfer failed. */
574                         if (++num_crc_errs > MAX_MAIN_FW_LOAD_CRC_ERR) {
575                                 lbs_pr_err("Too many CRC errors encountered "
576                                            "in firmware load.\n");
577                                 err = -EIO;
578                                 goto release_firmware;
579                         }
580                 } else {
581                         /* Previous transfer succeeded. Advance counters. */
582                         bytes -= prev_len;
583                         fw += prev_len;
584                 }
585                 if (bytes < len) {
586                         memset(card->cmd_buffer, 0, len);
587                         memcpy(card->cmd_buffer, fw, bytes);
588                 } else
589                         memcpy(card->cmd_buffer, fw, len);
590
591                 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
592                 if (err)
593                         goto release_firmware;
594                 err = spu_write(card, IF_SPI_CMD_RDWRPORT_REG,
595                                 card->cmd_buffer, len);
596                 if (err)
597                         goto release_firmware;
598                 err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG ,
599                                         IF_SPI_CIC_CMD_DOWNLOAD_OVER);
600                 if (err)
601                         goto release_firmware;
602                 prev_len = len;
603         }
604         if (bytes > prev_len) {
605                 lbs_pr_err("firmware load wants fewer bytes than "
606                            "we have to offer.\n");
607         }
608
609         /* Confirm firmware download */
610         err = spu_wait_for_u32(card, IF_SPI_SCRATCH_4_REG,
611                                         SUCCESSFUL_FW_DOWNLOAD_MAGIC);
612         if (err) {
613                 lbs_pr_err("failed to confirm the firmware download\n");
614                 goto release_firmware;
615         }
616
617 release_firmware:
618         release_firmware(firmware);
619
620 out:
621         if (err)
622                 lbs_pr_err("failed to load firmware (err=%d)\n", err);
623         lbs_deb_leave_args(LBS_DEB_SPI, "err %d", err);
624         return err;
625 }
626
627 /*
628  * SPI Transfer Thread
629  *
630  * The SPI thread handles all SPI transfers, so there is no need for a lock.
631  */
632
633 /* Move a command from the card to the host */
634 static int if_spi_c2h_cmd(struct if_spi_card *card)
635 {
636         struct lbs_private *priv = card->priv;
637         unsigned long flags;
638         int err = 0;
639         u16 len;
640         u8 i;
641
642         /* We need a buffer big enough to handle whatever people send to
643          * hw_host_to_card */
644         BUILD_BUG_ON(IF_SPI_CMD_BUF_SIZE < LBS_CMD_BUFFER_SIZE);
645         BUILD_BUG_ON(IF_SPI_CMD_BUF_SIZE < LBS_UPLD_SIZE);
646
647         /* It's just annoying if the buffer size isn't a multiple of 4, because
648          * then we might have len <  IF_SPI_CMD_BUF_SIZE but
649          * ALIGN(len, 4) > IF_SPI_CMD_BUF_SIZE */
650         BUILD_BUG_ON(IF_SPI_CMD_BUF_SIZE % 4 != 0);
651
652         lbs_deb_enter(LBS_DEB_SPI);
653
654         /* How many bytes are there to read? */
655         err = spu_read_u16(card, IF_SPI_SCRATCH_2_REG, &len);
656         if (err)
657                 goto out;
658         if (!len) {
659                 lbs_pr_err("%s: error: card has no data for host\n",
660                            __func__);
661                 err = -EINVAL;
662                 goto out;
663         } else if (len > IF_SPI_CMD_BUF_SIZE) {
664                 lbs_pr_err("%s: error: response packet too large: "
665                            "%d bytes, but maximum is %d\n",
666                            __func__, len, IF_SPI_CMD_BUF_SIZE);
667                 err = -EINVAL;
668                 goto out;
669         }
670
671         /* Read the data from the WLAN module into our command buffer */
672         err = spu_read(card, IF_SPI_CMD_RDWRPORT_REG,
673                                 card->cmd_buffer, ALIGN(len, 4));
674         if (err)
675                 goto out;
676
677         spin_lock_irqsave(&priv->driver_lock, flags);
678         i = (priv->resp_idx == 0) ? 1 : 0;
679         BUG_ON(priv->resp_len[i]);
680         priv->resp_len[i] = len;
681         memcpy(priv->resp_buf[i], card->cmd_buffer, len);
682         lbs_notify_command_response(priv, i);
683         spin_unlock_irqrestore(&priv->driver_lock, flags);
684
685 out:
686         if (err)
687                 lbs_pr_err("%s: err=%d\n", __func__, err);
688         lbs_deb_leave(LBS_DEB_SPI);
689         return err;
690 }
691
692 /* Move data from the card to the host */
693 static int if_spi_c2h_data(struct if_spi_card *card)
694 {
695         struct sk_buff *skb;
696         char *data;
697         u16 len;
698         int err = 0;
699
700         lbs_deb_enter(LBS_DEB_SPI);
701
702         /* How many bytes are there to read? */
703         err = spu_read_u16(card, IF_SPI_SCRATCH_1_REG, &len);
704         if (err)
705                 goto out;
706         if (!len) {
707                 lbs_pr_err("%s: error: card has no data for host\n",
708                            __func__);
709                 err = -EINVAL;
710                 goto out;
711         } else if (len > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
712                 lbs_pr_err("%s: error: card has %d bytes of data, but "
713                            "our maximum skb size is %zu\n",
714                            __func__, len, MRVDRV_ETH_RX_PACKET_BUFFER_SIZE);
715                 err = -EINVAL;
716                 goto out;
717         }
718
719         /* TODO: should we allocate a smaller skb if we have less data? */
720         skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE);
721         if (!skb) {
722                 err = -ENOBUFS;
723                 goto out;
724         }
725         skb_reserve(skb, IPFIELD_ALIGN_OFFSET);
726         data = skb_put(skb, len);
727
728         /* Read the data from the WLAN module into our skb... */
729         err = spu_read(card, IF_SPI_DATA_RDWRPORT_REG, data, ALIGN(len, 4));
730         if (err)
731                 goto free_skb;
732
733         /* pass the SKB to libertas */
734         err = lbs_process_rxed_packet(card->priv, skb);
735         if (err)
736                 goto free_skb;
737
738         /* success */
739         goto out;
740
741 free_skb:
742         dev_kfree_skb(skb);
743 out:
744         if (err)
745                 lbs_pr_err("%s: err=%d\n", __func__, err);
746         lbs_deb_leave(LBS_DEB_SPI);
747         return err;
748 }
749
750 /* Inform the host about a card event */
751 static void if_spi_e2h(struct if_spi_card *card)
752 {
753         int err = 0;
754         u32 cause;
755         struct lbs_private *priv = card->priv;
756
757         err = spu_read_u32(card, IF_SPI_SCRATCH_3_REG, &cause);
758         if (err)
759                 goto out;
760
761         /* re-enable the card event interrupt */
762         spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG,
763                         ~IF_SPI_HICU_CARD_EVENT);
764
765         /* generate a card interrupt */
766         spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG, IF_SPI_CIC_HOST_EVENT);
767
768         lbs_queue_event(priv, cause & 0xff);
769 out:
770         if (err)
771                 lbs_pr_err("%s: error %d\n", __func__, err);
772 }
773
774 static int lbs_spi_thread(void *data)
775 {
776         int err;
777         struct if_spi_card *card = data;
778         u16 hiStatus;
779
780         while (1) {
781                 /* Wait to be woken up by one of two things.  First, our ISR
782                  * could tell us that something happened on the WLAN.
783                  * Secondly, libertas could call hw_host_to_card with more
784                  * data, which we might be able to send.
785                  */
786                 do {
787                         err = down_interruptible(&card->spi_ready);
788                         if (!card->run_thread) {
789                                 up(&card->spi_thread_terminated);
790                                 do_exit(0);
791                         }
792                 } while (err == EINTR);
793
794                 /* Read the host interrupt status register to see what we
795                  * can do. */
796                 err = spu_read_u16(card, IF_SPI_HOST_INT_STATUS_REG,
797                                         &hiStatus);
798                 if (err) {
799                         lbs_pr_err("I/O error\n");
800                         goto err;
801                 }
802
803                 if (hiStatus & IF_SPI_HIST_CMD_UPLOAD_RDY)
804                         err = if_spi_c2h_cmd(card);
805                         if (err)
806                                 goto err;
807                 if (hiStatus & IF_SPI_HIST_RX_UPLOAD_RDY)
808                         err = if_spi_c2h_data(card);
809                         if (err)
810                                 goto err;
811
812                 /* workaround: in PS mode, the card does not set the Command
813                  * Download Ready bit, but it sets TX Download Ready. */
814                 if (hiStatus & IF_SPI_HIST_CMD_DOWNLOAD_RDY ||
815                    (card->priv->psstate != PS_STATE_FULL_POWER &&
816                     (hiStatus & IF_SPI_HIST_TX_DOWNLOAD_RDY))) {
817                         lbs_host_to_card_done(card->priv);
818                 }
819
820                 if (hiStatus & IF_SPI_HIST_CARD_EVENT)
821                         if_spi_e2h(card);
822
823 err:
824                 if (err)
825                         lbs_pr_err("%s: got error %d\n", __func__, err);
826         }
827 }
828
829 /* Block until lbs_spi_thread thread has terminated */
830 static void if_spi_terminate_spi_thread(struct if_spi_card *card)
831 {
832         /* It would be nice to use kthread_stop here, but that function
833          * can't wake threads waiting for a semaphore. */
834         card->run_thread = 0;
835         up(&card->spi_ready);
836         down(&card->spi_thread_terminated);
837 }
838
839 /*
840  * Host to Card
841  *
842  * Called from Libertas to transfer some data to the WLAN device
843  * We can't sleep here. */
844 static int if_spi_host_to_card(struct lbs_private *priv,
845                                 u8 type, u8 *buf, u16 nb)
846 {
847         int err = 0;
848         struct if_spi_card *card = priv->card;
849
850         lbs_deb_enter_args(LBS_DEB_SPI, "type %d, bytes %d", type, nb);
851
852         nb = ALIGN(nb, 4);
853
854         switch (type) {
855         case MVMS_CMD:
856                 err = spu_write(card, IF_SPI_CMD_RDWRPORT_REG, buf, nb);
857                 break;
858         case MVMS_DAT:
859                 err = spu_write(card, IF_SPI_DATA_RDWRPORT_REG, buf, nb);
860                 break;
861         default:
862                 lbs_pr_err("can't transfer buffer of type %d", type);
863                 err = -EINVAL;
864                 break;
865         }
866
867         lbs_deb_leave_args(LBS_DEB_SPI, "err=%d", err);
868         return err;
869 }
870
871 /*
872  * Host Interrupts
873  *
874  * Service incoming interrupts from the WLAN device. We can't sleep here, so
875  * don't try to talk on the SPI bus, just wake up the SPI thread.
876  */
877 static irqreturn_t if_spi_host_interrupt(int irq, void *dev_id)
878 {
879         struct if_spi_card *card = dev_id;
880
881         up(&card->spi_ready);
882         return IRQ_HANDLED;
883 }
884
885 /*
886  * SPI callbacks
887  */
888
889 static int if_spi_calculate_fw_names(u16 card_id,
890                               char *helper_fw, char *main_fw)
891 {
892         int i;
893         for (i = 0; i < ARRAY_SIZE(chip_id_to_device_name); ++i) {
894                 if (card_id == chip_id_to_device_name[i].chip_id)
895                         break;
896         }
897         if (i == ARRAY_SIZE(chip_id_to_device_name)) {
898                 lbs_pr_err("Unsupported chip_id: 0x%02x\n", card_id);
899                 return -EAFNOSUPPORT;
900         }
901         snprintf(helper_fw, IF_SPI_FW_NAME_MAX, "libertas/gspi%d_hlp.bin",
902                  chip_id_to_device_name[i].name);
903         snprintf(main_fw, IF_SPI_FW_NAME_MAX, "libertas/gspi%d.bin",
904                  chip_id_to_device_name[i].name);
905         return 0;
906 }
907 MODULE_FIRMWARE("libertas/gspi8385_hlp.bin");
908 MODULE_FIRMWARE("libertas/gspi8385.bin");
909 MODULE_FIRMWARE("libertas/gspi8686_hlp.bin");
910 MODULE_FIRMWARE("libertas/gspi8686.bin");
911
912 static int __devinit if_spi_probe(struct spi_device *spi)
913 {
914         struct if_spi_card *card;
915         struct lbs_private *priv = NULL;
916         struct libertas_spi_platform_data *pdata = spi->dev.platform_data;
917         int err = 0;
918         u32 scratch;
919         struct sched_param param = { .sched_priority = 1 };
920
921         lbs_deb_enter(LBS_DEB_SPI);
922
923         if (!pdata) {
924                 err = -EINVAL;
925                 goto out;
926         }
927
928         if (pdata->setup) {
929                 err = pdata->setup(spi);
930                 if (err)
931                         goto out;
932         }
933
934         /* Allocate card structure to represent this specific device */
935         card = kzalloc(sizeof(struct if_spi_card), GFP_KERNEL);
936         if (!card) {
937                 err = -ENOMEM;
938                 goto out;
939         }
940         spi_set_drvdata(spi, card);
941         card->pdata = pdata;
942         card->spi = spi;
943         card->prev_xfer_time = jiffies;
944
945         sema_init(&card->spi_ready, 0);
946         sema_init(&card->spi_thread_terminated, 0);
947
948         /* Initialize the SPI Interface Unit */
949         err = spu_init(card, pdata->use_dummy_writes);
950         if (err)
951                 goto free_card;
952         err = spu_get_chip_revision(card, &card->card_id, &card->card_rev);
953         if (err)
954                 goto free_card;
955
956         /* Firmware load */
957         err = spu_read_u32(card, IF_SPI_SCRATCH_4_REG, &scratch);
958         if (err)
959                 goto free_card;
960         if (scratch == SUCCESSFUL_FW_DOWNLOAD_MAGIC)
961                 lbs_deb_spi("Firmware is already loaded for "
962                             "Marvell WLAN 802.11 adapter\n");
963         else {
964                 err = if_spi_calculate_fw_names(card->card_id,
965                                 card->helper_fw_name, card->main_fw_name);
966                 if (err)
967                         goto free_card;
968
969                 lbs_deb_spi("Initializing FW for Marvell WLAN 802.11 adapter "
970                                 "(chip_id = 0x%04x, chip_rev = 0x%02x) "
971                                 "attached to SPI bus_num %d, chip_select %d. "
972                                 "spi->max_speed_hz=%d\n",
973                                 card->card_id, card->card_rev,
974                                 spi->master->bus_num, spi->chip_select,
975                                 spi->max_speed_hz);
976                 err = if_spi_prog_helper_firmware(card);
977                 if (err)
978                         goto free_card;
979                 err = if_spi_prog_main_firmware(card);
980                 if (err)
981                         goto free_card;
982                 lbs_deb_spi("loaded FW for Marvell WLAN 802.11 adapter\n");
983         }
984
985         err = spu_set_interrupt_mode(card, 0, 1);
986         if (err)
987                 goto free_card;
988
989         /* Register our card with libertas.
990          * This will call alloc_etherdev */
991         priv = lbs_add_card(card, &spi->dev);
992         if (!priv) {
993                 err = -ENOMEM;
994                 goto free_card;
995         }
996         card->priv = priv;
997         priv->card = card;
998         priv->hw_host_to_card = if_spi_host_to_card;
999         priv->enter_deep_sleep = NULL;
1000         priv->exit_deep_sleep = NULL;
1001         priv->reset_deep_sleep_wakeup = NULL;
1002         priv->fw_ready = 1;
1003
1004         /* Initialize interrupt handling stuff. */
1005         card->run_thread = 1;
1006         card->spi_thread = kthread_run(lbs_spi_thread, card, "lbs_spi_thread");
1007         if (IS_ERR(card->spi_thread)) {
1008                 card->run_thread = 0;
1009                 err = PTR_ERR(card->spi_thread);
1010                 lbs_pr_err("error creating SPI thread: err=%d\n", err);
1011                 goto remove_card;
1012         }
1013         if (sched_setscheduler(card->spi_thread, SCHED_FIFO, &param))
1014                 lbs_pr_err("Error setting scheduler, using default.\n");
1015
1016         err = request_irq(spi->irq, if_spi_host_interrupt,
1017                         IRQF_TRIGGER_FALLING, "libertas_spi", card);
1018         if (err) {
1019                 lbs_pr_err("can't get host irq line-- request_irq failed\n");
1020                 goto terminate_thread;
1021         }
1022
1023         /* poke the IRQ handler so that we don't miss the first interrupt */
1024         up(&card->spi_ready);
1025
1026         /* Start the card.
1027          * This will call register_netdev, and we'll start
1028          * getting interrupts... */
1029         err = lbs_start_card(priv);
1030         if (err)
1031                 goto release_irq;
1032
1033         lbs_deb_spi("Finished initializing WLAN module.\n");
1034
1035         /* successful exit */
1036         goto out;
1037
1038 release_irq:
1039         free_irq(spi->irq, card);
1040 terminate_thread:
1041         if_spi_terminate_spi_thread(card);
1042 remove_card:
1043         lbs_remove_card(priv); /* will call free_netdev */
1044 free_card:
1045         free_if_spi_card(card);
1046 out:
1047         lbs_deb_leave_args(LBS_DEB_SPI, "err %d\n", err);
1048         return err;
1049 }
1050
1051 static int __devexit libertas_spi_remove(struct spi_device *spi)
1052 {
1053         struct if_spi_card *card = spi_get_drvdata(spi);
1054         struct lbs_private *priv = card->priv;
1055
1056         lbs_deb_spi("libertas_spi_remove\n");
1057         lbs_deb_enter(LBS_DEB_SPI);
1058
1059         lbs_stop_card(priv);
1060         lbs_remove_card(priv); /* will call free_netdev */
1061
1062         priv->surpriseremoved = 1;
1063         free_irq(spi->irq, card);
1064         if_spi_terminate_spi_thread(card);
1065         if (card->pdata->teardown)
1066                 card->pdata->teardown(spi);
1067         free_if_spi_card(card);
1068         lbs_deb_leave(LBS_DEB_SPI);
1069         return 0;
1070 }
1071
1072 static struct spi_driver libertas_spi_driver = {
1073         .probe  = if_spi_probe,
1074         .remove = __devexit_p(libertas_spi_remove),
1075         .driver = {
1076                 .name   = "libertas_spi",
1077                 .bus    = &spi_bus_type,
1078                 .owner  = THIS_MODULE,
1079         },
1080 };
1081
1082 /*
1083  * Module functions
1084  */
1085
1086 static int __init if_spi_init_module(void)
1087 {
1088         int ret = 0;
1089         lbs_deb_enter(LBS_DEB_SPI);
1090         printk(KERN_INFO "libertas_spi: Libertas SPI driver\n");
1091         ret = spi_register_driver(&libertas_spi_driver);
1092         lbs_deb_leave(LBS_DEB_SPI);
1093         return ret;
1094 }
1095
1096 static void __exit if_spi_exit_module(void)
1097 {
1098         lbs_deb_enter(LBS_DEB_SPI);
1099         spi_unregister_driver(&libertas_spi_driver);
1100         lbs_deb_leave(LBS_DEB_SPI);
1101 }
1102
1103 module_init(if_spi_init_module);
1104 module_exit(if_spi_exit_module);
1105
1106 MODULE_DESCRIPTION("Libertas SPI WLAN Driver");
1107 MODULE_AUTHOR("Andrey Yurovsky <andrey@cozybit.com>, "
1108               "Colin McCabe <colin@cozybit.com>");
1109 MODULE_LICENSE("GPL");
1110 MODULE_ALIAS("spi:libertas_spi");