Linux-libre 3.4.28-gnu1
[librecmc/linux-libre.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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21
22 #include <linux/hardirq.h>
23 #include <linux/interrupt.h>
24 #include <linux/module.h>
25 #include <linux/firmware.h>
26 #include <linux/jiffies.h>
27 #include <linux/list.h>
28 #include <linux/netdevice.h>
29 #include <linux/slab.h>
30 #include <linux/spi/libertas_spi.h>
31 #include <linux/spi/spi.h>
32
33 #include "host.h"
34 #include "decl.h"
35 #include "defs.h"
36 #include "dev.h"
37 #include "if_spi.h"
38
39 struct if_spi_packet {
40         struct list_head                list;
41         u16                             blen;
42         u8                              buffer[0] __attribute__((aligned(4)));
43 };
44
45 struct if_spi_card {
46         struct spi_device               *spi;
47         struct lbs_private              *priv;
48         struct libertas_spi_platform_data *pdata;
49
50         /* The card ID and card revision, as reported by the hardware. */
51         u16                             card_id;
52         u8                              card_rev;
53
54         /* The last time that we initiated an SPU operation */
55         unsigned long                   prev_xfer_time;
56
57         int                             use_dummy_writes;
58         unsigned long                   spu_port_delay;
59         unsigned long                   spu_reg_delay;
60
61         /* Handles all SPI communication (except for FW load) */
62         struct workqueue_struct         *workqueue;
63         struct work_struct              packet_work;
64         struct work_struct              resume_work;
65
66         u8                              cmd_buffer[IF_SPI_CMD_BUF_SIZE];
67
68         /* A buffer of incoming packets from libertas core.
69          * Since we can't sleep in hw_host_to_card, we have to buffer
70          * them. */
71         struct list_head                cmd_packet_list;
72         struct list_head                data_packet_list;
73
74         /* Protects cmd_packet_list and data_packet_list */
75         spinlock_t                      buffer_lock;
76
77         /* True is card suspended */
78         u8                              suspended;
79 };
80
81 static void free_if_spi_card(struct if_spi_card *card)
82 {
83         struct list_head *cursor, *next;
84         struct if_spi_packet *packet;
85
86         list_for_each_safe(cursor, next, &card->cmd_packet_list) {
87                 packet = container_of(cursor, struct if_spi_packet, list);
88                 list_del(&packet->list);
89                 kfree(packet);
90         }
91         list_for_each_safe(cursor, next, &card->data_packet_list) {
92                 packet = container_of(cursor, struct if_spi_packet, list);
93                 list_del(&packet->list);
94                 kfree(packet);
95         }
96         spi_set_drvdata(card->spi, NULL);
97         kfree(card);
98 }
99
100 #define MODEL_8385      0x04
101 #define MODEL_8686      0x0b
102 #define MODEL_8688      0x10
103
104 static const struct lbs_fw_table fw_table[] = {
105         { MODEL_8385, "/*(DEBLOBBED)*/", "/*(DEBLOBBED)*/" },
106         { MODEL_8385, "/*(DEBLOBBED)*/", "/*(DEBLOBBED)*/" },
107         { MODEL_8686, "/*(DEBLOBBED)*/", "/*(DEBLOBBED)*/" },
108         { MODEL_8686, "/*(DEBLOBBED)*/", "/*(DEBLOBBED)*/" },
109         { MODEL_8688, "/*(DEBLOBBED)*/", "/*(DEBLOBBED)*/" },
110         { 0, NULL, NULL }
111 };
112 /*(DEBLOBBED)*/
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 /*
143  * Write out a byte buffer to an SPI register,
144  * using a series of 16-bit transfers.
145  */
146 static int spu_write(struct if_spi_card *card, u16 reg, const u8 *buf, int len)
147 {
148         int err = 0;
149         __le16 reg_out = cpu_to_le16(reg | IF_SPI_WRITE_OPERATION_MASK);
150         struct spi_message m;
151         struct spi_transfer reg_trans;
152         struct spi_transfer data_trans;
153
154         spi_message_init(&m);
155         memset(&reg_trans, 0, sizeof(reg_trans));
156         memset(&data_trans, 0, sizeof(data_trans));
157
158         /* You must give an even number of bytes to the SPU, even if it
159          * doesn't care about the last one.  */
160         BUG_ON(len & 0x1);
161
162         spu_transaction_init(card);
163
164         /* write SPU register index */
165         reg_trans.tx_buf = &reg_out;
166         reg_trans.len = sizeof(reg_out);
167
168         data_trans.tx_buf = buf;
169         data_trans.len = len;
170
171         spi_message_add_tail(&reg_trans, &m);
172         spi_message_add_tail(&data_trans, &m);
173
174         err = spi_sync(card->spi, &m);
175         spu_transaction_finish(card);
176         return err;
177 }
178
179 static inline int spu_write_u16(struct if_spi_card *card, u16 reg, u16 val)
180 {
181         __le16 buff;
182
183         buff = cpu_to_le16(val);
184         return spu_write(card, reg, (u8 *)&buff, sizeof(u16));
185 }
186
187 static inline int spu_reg_is_port_reg(u16 reg)
188 {
189         switch (reg) {
190         case IF_SPI_IO_RDWRPORT_REG:
191         case IF_SPI_CMD_RDWRPORT_REG:
192         case IF_SPI_DATA_RDWRPORT_REG:
193                 return 1;
194         default:
195                 return 0;
196         }
197 }
198
199 static int spu_read(struct if_spi_card *card, u16 reg, u8 *buf, int len)
200 {
201         unsigned int delay;
202         int err = 0;
203         __le16 reg_out = cpu_to_le16(reg | IF_SPI_READ_OPERATION_MASK);
204         struct spi_message m;
205         struct spi_transfer reg_trans;
206         struct spi_transfer dummy_trans;
207         struct spi_transfer data_trans;
208
209         /*
210          * You must take an even number of bytes from the SPU, even if you
211          * don't care about the last one.
212          */
213         BUG_ON(len & 0x1);
214
215         spu_transaction_init(card);
216
217         spi_message_init(&m);
218         memset(&reg_trans, 0, sizeof(reg_trans));
219         memset(&dummy_trans, 0, sizeof(dummy_trans));
220         memset(&data_trans, 0, sizeof(data_trans));
221
222         /* write SPU register index */
223         reg_trans.tx_buf = &reg_out;
224         reg_trans.len = sizeof(reg_out);
225         spi_message_add_tail(&reg_trans, &m);
226
227         delay = spu_reg_is_port_reg(reg) ? card->spu_port_delay :
228                                                 card->spu_reg_delay;
229         if (card->use_dummy_writes) {
230                 /* Clock in dummy cycles while the SPU fills the FIFO */
231                 dummy_trans.len = delay / 8;
232                 spi_message_add_tail(&dummy_trans, &m);
233         } else {
234                 /* Busy-wait while the SPU fills the FIFO */
235                 reg_trans.delay_usecs =
236                         DIV_ROUND_UP((100 + (delay * 10)), 1000);
237         }
238
239         /* read in data */
240         data_trans.rx_buf = buf;
241         data_trans.len = len;
242         spi_message_add_tail(&data_trans, &m);
243
244         err = spi_sync(card->spi, &m);
245         spu_transaction_finish(card);
246         return err;
247 }
248
249 /* Read 16 bits from an SPI register */
250 static inline int spu_read_u16(struct if_spi_card *card, u16 reg, u16 *val)
251 {
252         __le16 buf;
253         int ret;
254
255         ret = spu_read(card, reg, (u8 *)&buf, sizeof(buf));
256         if (ret == 0)
257                 *val = le16_to_cpup(&buf);
258         return ret;
259 }
260
261 /*
262  * Read 32 bits from an SPI register.
263  * The low 16 bits are read first.
264  */
265 static int spu_read_u32(struct if_spi_card *card, u16 reg, u32 *val)
266 {
267         __le32 buf;
268         int err;
269
270         err = spu_read(card, reg, (u8 *)&buf, sizeof(buf));
271         if (!err)
272                 *val = le32_to_cpup(&buf);
273         return err;
274 }
275
276 /*
277  * Keep reading 16 bits from an SPI register until you get the correct result.
278  *
279  * If mask = 0, the correct result is any non-zero number.
280  * If mask != 0, the correct result is any number where
281  * number & target_mask == target
282  *
283  * Returns -ETIMEDOUT if a second passes without the correct result.
284  */
285 static int spu_wait_for_u16(struct if_spi_card *card, u16 reg,
286                         u16 target_mask, u16 target)
287 {
288         int err;
289         unsigned long timeout = jiffies + 5*HZ;
290         while (1) {
291                 u16 val;
292                 err = spu_read_u16(card, reg, &val);
293                 if (err)
294                         return err;
295                 if (target_mask) {
296                         if ((val & target_mask) == target)
297                                 return 0;
298                 } else {
299                         if (val)
300                                 return 0;
301                 }
302                 udelay(100);
303                 if (time_after(jiffies, timeout)) {
304                         pr_err("%s: timeout with val=%02x, target_mask=%02x, target=%02x\n",
305                                __func__, val, target_mask, target);
306                         return -ETIMEDOUT;
307                 }
308         }
309 }
310
311 /*
312  * Read 16 bits from an SPI register until you receive a specific value.
313  * Returns -ETIMEDOUT if a 4 tries pass without success.
314  */
315 static int spu_wait_for_u32(struct if_spi_card *card, u32 reg, u32 target)
316 {
317         int err, try;
318         for (try = 0; try < 4; ++try) {
319                 u32 val = 0;
320                 err = spu_read_u32(card, reg, &val);
321                 if (err)
322                         return err;
323                 if (val == target)
324                         return 0;
325                 mdelay(100);
326         }
327         return -ETIMEDOUT;
328 }
329
330 static int spu_set_interrupt_mode(struct if_spi_card *card,
331                            int suppress_host_int,
332                            int auto_int)
333 {
334         int err = 0;
335
336         /*
337          * We can suppress a host interrupt by clearing the appropriate
338          * bit in the "host interrupt status mask" register
339          */
340         if (suppress_host_int) {
341                 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG, 0);
342                 if (err)
343                         return err;
344         } else {
345                 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG,
346                               IF_SPI_HISM_TX_DOWNLOAD_RDY |
347                               IF_SPI_HISM_RX_UPLOAD_RDY |
348                               IF_SPI_HISM_CMD_DOWNLOAD_RDY |
349                               IF_SPI_HISM_CARDEVENT |
350                               IF_SPI_HISM_CMD_UPLOAD_RDY);
351                 if (err)
352                         return err;
353         }
354
355         /*
356          * If auto-interrupts are on, the completion of certain transactions
357          * will trigger an interrupt automatically. If auto-interrupts
358          * are off, we need to set the "Card Interrupt Cause" register to
359          * trigger a card interrupt.
360          */
361         if (auto_int) {
362                 err = spu_write_u16(card, IF_SPI_HOST_INT_CTRL_REG,
363                                 IF_SPI_HICT_TX_DOWNLOAD_OVER_AUTO |
364                                 IF_SPI_HICT_RX_UPLOAD_OVER_AUTO |
365                                 IF_SPI_HICT_CMD_DOWNLOAD_OVER_AUTO |
366                                 IF_SPI_HICT_CMD_UPLOAD_OVER_AUTO);
367                 if (err)
368                         return err;
369         } else {
370                 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG, 0);
371                 if (err)
372                         return err;
373         }
374         return err;
375 }
376
377 static int spu_get_chip_revision(struct if_spi_card *card,
378                                   u16 *card_id, u8 *card_rev)
379 {
380         int err = 0;
381         u32 dev_ctrl;
382         err = spu_read_u32(card, IF_SPI_DEVICEID_CTRL_REG, &dev_ctrl);
383         if (err)
384                 return err;
385         *card_id = IF_SPI_DEVICEID_CTRL_REG_TO_CARD_ID(dev_ctrl);
386         *card_rev = IF_SPI_DEVICEID_CTRL_REG_TO_CARD_REV(dev_ctrl);
387         return err;
388 }
389
390 static int spu_set_bus_mode(struct if_spi_card *card, u16 mode)
391 {
392         int err = 0;
393         u16 rval;
394         /* set bus mode */
395         err = spu_write_u16(card, IF_SPI_SPU_BUS_MODE_REG, mode);
396         if (err)
397                 return err;
398         /* Check that we were able to read back what we just wrote. */
399         err = spu_read_u16(card, IF_SPI_SPU_BUS_MODE_REG, &rval);
400         if (err)
401                 return err;
402         if ((rval & 0xF) != mode) {
403                 pr_err("Can't read bus mode register\n");
404                 return -EIO;
405         }
406         return 0;
407 }
408
409 static int spu_init(struct if_spi_card *card, int use_dummy_writes)
410 {
411         int err = 0;
412         u32 delay;
413
414         /*
415          * We have to start up in timed delay mode so that we can safely
416          * read the Delay Read Register.
417          */
418         card->use_dummy_writes = 0;
419         err = spu_set_bus_mode(card,
420                                 IF_SPI_BUS_MODE_SPI_CLOCK_PHASE_RISING |
421                                 IF_SPI_BUS_MODE_DELAY_METHOD_TIMED |
422                                 IF_SPI_BUS_MODE_16_BIT_ADDRESS_16_BIT_DATA);
423         if (err)
424                 return err;
425         card->spu_port_delay = 1000;
426         card->spu_reg_delay = 1000;
427         err = spu_read_u32(card, IF_SPI_DELAY_READ_REG, &delay);
428         if (err)
429                 return err;
430         card->spu_port_delay = delay & 0x0000ffff;
431         card->spu_reg_delay = (delay & 0xffff0000) >> 16;
432
433         /* If dummy clock delay mode has been requested, switch to it now */
434         if (use_dummy_writes) {
435                 card->use_dummy_writes = 1;
436                 err = spu_set_bus_mode(card,
437                                 IF_SPI_BUS_MODE_SPI_CLOCK_PHASE_RISING |
438                                 IF_SPI_BUS_MODE_DELAY_METHOD_DUMMY_CLOCK |
439                                 IF_SPI_BUS_MODE_16_BIT_ADDRESS_16_BIT_DATA);
440                 if (err)
441                         return err;
442         }
443
444         lbs_deb_spi("Initialized SPU unit. "
445                     "spu_port_delay=0x%04lx, spu_reg_delay=0x%04lx\n",
446                     card->spu_port_delay, card->spu_reg_delay);
447         return err;
448 }
449
450 /*
451  * Firmware Loading
452  */
453
454 static int if_spi_prog_helper_firmware(struct if_spi_card *card,
455                                         const struct firmware *firmware)
456 {
457         int err = 0;
458         int bytes_remaining;
459         const u8 *fw;
460         u8 temp[HELPER_FW_LOAD_CHUNK_SZ];
461
462         lbs_deb_enter(LBS_DEB_SPI);
463
464         err = spu_set_interrupt_mode(card, 1, 0);
465         if (err)
466                 goto out;
467
468         bytes_remaining = firmware->size;
469         fw = firmware->data;
470
471         /* Load helper firmware image */
472         while (bytes_remaining > 0) {
473                 /*
474                  * Scratch pad 1 should contain the number of bytes we
475                  * want to download to the firmware
476                  */
477                 err = spu_write_u16(card, IF_SPI_SCRATCH_1_REG,
478                                         HELPER_FW_LOAD_CHUNK_SZ);
479                 if (err)
480                         goto out;
481
482                 err = spu_wait_for_u16(card, IF_SPI_HOST_INT_STATUS_REG,
483                                         IF_SPI_HIST_CMD_DOWNLOAD_RDY,
484                                         IF_SPI_HIST_CMD_DOWNLOAD_RDY);
485                 if (err)
486                         goto out;
487
488                 /*
489                  * Feed the data into the command read/write port reg
490                  * in chunks of 64 bytes
491                  */
492                 memset(temp, 0, sizeof(temp));
493                 memcpy(temp, fw,
494                        min(bytes_remaining, HELPER_FW_LOAD_CHUNK_SZ));
495                 mdelay(10);
496                 err = spu_write(card, IF_SPI_CMD_RDWRPORT_REG,
497                                         temp, HELPER_FW_LOAD_CHUNK_SZ);
498                 if (err)
499                         goto out;
500
501                 /* Interrupt the boot code */
502                 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
503                 if (err)
504                         goto out;
505                 err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG,
506                                        IF_SPI_CIC_CMD_DOWNLOAD_OVER);
507                 if (err)
508                         goto out;
509                 bytes_remaining -= HELPER_FW_LOAD_CHUNK_SZ;
510                 fw += HELPER_FW_LOAD_CHUNK_SZ;
511         }
512
513         /*
514          * Once the helper / single stage firmware download is complete,
515          * write 0 to scratch pad 1 and interrupt the
516          * bootloader. This completes the helper download.
517          */
518         err = spu_write_u16(card, IF_SPI_SCRATCH_1_REG, FIRMWARE_DNLD_OK);
519         if (err)
520                 goto out;
521         err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
522         if (err)
523                 goto out;
524         err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG,
525                                 IF_SPI_CIC_CMD_DOWNLOAD_OVER);
526 out:
527         if (err)
528                 pr_err("failed to load helper firmware (err=%d)\n", err);
529         lbs_deb_leave_args(LBS_DEB_SPI, "err %d", err);
530         return err;
531 }
532
533 /*
534  * Returns the length of the next packet the firmware expects us to send.
535  * Sets crc_err if the previous transfer had a CRC error.
536  */
537 static int if_spi_prog_main_firmware_check_len(struct if_spi_card *card,
538                                                 int *crc_err)
539 {
540         u16 len;
541         int err = 0;
542
543         /*
544          * wait until the host interrupt status register indicates
545          * that we are ready to download
546          */
547         err = spu_wait_for_u16(card, IF_SPI_HOST_INT_STATUS_REG,
548                                 IF_SPI_HIST_CMD_DOWNLOAD_RDY,
549                                 IF_SPI_HIST_CMD_DOWNLOAD_RDY);
550         if (err) {
551                 pr_err("timed out waiting for host_int_status\n");
552                 return err;
553         }
554
555         /* Ask the device how many bytes of firmware it wants. */
556         err = spu_read_u16(card, IF_SPI_SCRATCH_1_REG, &len);
557         if (err)
558                 return err;
559
560         if (len > IF_SPI_CMD_BUF_SIZE) {
561                 pr_err("firmware load device requested a larger transfer than we are prepared to handle (len = %d)\n",
562                        len);
563                 return -EIO;
564         }
565         if (len & 0x1) {
566                 lbs_deb_spi("%s: crc error\n", __func__);
567                 len &= ~0x1;
568                 *crc_err = 1;
569         } else
570                 *crc_err = 0;
571
572         return len;
573 }
574
575 static int if_spi_prog_main_firmware(struct if_spi_card *card,
576                                         const struct firmware *firmware)
577 {
578         struct lbs_private *priv = card->priv;
579         int len, prev_len;
580         int bytes, crc_err = 0, err = 0;
581         const u8 *fw;
582         u16 num_crc_errs;
583
584         lbs_deb_enter(LBS_DEB_SPI);
585
586         err = spu_set_interrupt_mode(card, 1, 0);
587         if (err)
588                 goto out;
589
590         err = spu_wait_for_u16(card, IF_SPI_SCRATCH_1_REG, 0, 0);
591         if (err) {
592                 netdev_err(priv->dev,
593                            "%s: timed out waiting for initial scratch reg = 0\n",
594                            __func__);
595                 goto out;
596         }
597
598         num_crc_errs = 0;
599         prev_len = 0;
600         bytes = firmware->size;
601         fw = firmware->data;
602         while ((len = if_spi_prog_main_firmware_check_len(card, &crc_err))) {
603                 if (len < 0) {
604                         err = len;
605                         goto out;
606                 }
607                 if (bytes < 0) {
608                         /*
609                          * If there are no more bytes left, we would normally
610                          * expect to have terminated with len = 0
611                          */
612                         netdev_err(priv->dev,
613                                    "Firmware load wants more bytes than we have to offer.\n");
614                         break;
615                 }
616                 if (crc_err) {
617                         /* Previous transfer failed. */
618                         if (++num_crc_errs > MAX_MAIN_FW_LOAD_CRC_ERR) {
619                                 pr_err("Too many CRC errors encountered in firmware load.\n");
620                                 err = -EIO;
621                                 goto out;
622                         }
623                 } else {
624                         /* Previous transfer succeeded. Advance counters. */
625                         bytes -= prev_len;
626                         fw += prev_len;
627                 }
628                 if (bytes < len) {
629                         memset(card->cmd_buffer, 0, len);
630                         memcpy(card->cmd_buffer, fw, bytes);
631                 } else
632                         memcpy(card->cmd_buffer, fw, len);
633
634                 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
635                 if (err)
636                         goto out;
637                 err = spu_write(card, IF_SPI_CMD_RDWRPORT_REG,
638                                 card->cmd_buffer, len);
639                 if (err)
640                         goto out;
641                 err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG ,
642                                         IF_SPI_CIC_CMD_DOWNLOAD_OVER);
643                 if (err)
644                         goto out;
645                 prev_len = len;
646         }
647         if (bytes > prev_len) {
648                 pr_err("firmware load wants fewer bytes than we have to offer\n");
649         }
650
651         /* Confirm firmware download */
652         err = spu_wait_for_u32(card, IF_SPI_SCRATCH_4_REG,
653                                         SUCCESSFUL_FW_DOWNLOAD_MAGIC);
654         if (err) {
655                 pr_err("failed to confirm the firmware download\n");
656                 goto out;
657         }
658
659 out:
660         if (err)
661                 pr_err("failed to load firmware (err=%d)\n", err);
662         lbs_deb_leave_args(LBS_DEB_SPI, "err %d", err);
663         return err;
664 }
665
666 /*
667  * SPI Transfer Thread
668  *
669  * The SPI worker handles all SPI transfers, so there is no need for a lock.
670  */
671
672 /* Move a command from the card to the host */
673 static int if_spi_c2h_cmd(struct if_spi_card *card)
674 {
675         struct lbs_private *priv = card->priv;
676         unsigned long flags;
677         int err = 0;
678         u16 len;
679         u8 i;
680
681         /*
682          * We need a buffer big enough to handle whatever people send to
683          * hw_host_to_card
684          */
685         BUILD_BUG_ON(IF_SPI_CMD_BUF_SIZE < LBS_CMD_BUFFER_SIZE);
686         BUILD_BUG_ON(IF_SPI_CMD_BUF_SIZE < LBS_UPLD_SIZE);
687
688         /*
689          * It's just annoying if the buffer size isn't a multiple of 4, because
690          * then we might have len < IF_SPI_CMD_BUF_SIZE but
691          * ALIGN(len, 4) > IF_SPI_CMD_BUF_SIZE
692          */
693         BUILD_BUG_ON(IF_SPI_CMD_BUF_SIZE % 4 != 0);
694
695         lbs_deb_enter(LBS_DEB_SPI);
696
697         /* How many bytes are there to read? */
698         err = spu_read_u16(card, IF_SPI_SCRATCH_2_REG, &len);
699         if (err)
700                 goto out;
701         if (!len) {
702                 netdev_err(priv->dev, "%s: error: card has no data for host\n",
703                            __func__);
704                 err = -EINVAL;
705                 goto out;
706         } else if (len > IF_SPI_CMD_BUF_SIZE) {
707                 netdev_err(priv->dev,
708                            "%s: error: response packet too large: %d bytes, but maximum is %d\n",
709                            __func__, len, IF_SPI_CMD_BUF_SIZE);
710                 err = -EINVAL;
711                 goto out;
712         }
713
714         /* Read the data from the WLAN module into our command buffer */
715         err = spu_read(card, IF_SPI_CMD_RDWRPORT_REG,
716                                 card->cmd_buffer, ALIGN(len, 4));
717         if (err)
718                 goto out;
719
720         spin_lock_irqsave(&priv->driver_lock, flags);
721         i = (priv->resp_idx == 0) ? 1 : 0;
722         BUG_ON(priv->resp_len[i]);
723         priv->resp_len[i] = len;
724         memcpy(priv->resp_buf[i], card->cmd_buffer, len);
725         lbs_notify_command_response(priv, i);
726         spin_unlock_irqrestore(&priv->driver_lock, flags);
727
728 out:
729         if (err)
730                 netdev_err(priv->dev, "%s: err=%d\n", __func__, err);
731         lbs_deb_leave(LBS_DEB_SPI);
732         return err;
733 }
734
735 /* Move data from the card to the host */
736 static int if_spi_c2h_data(struct if_spi_card *card)
737 {
738         struct lbs_private *priv = card->priv;
739         struct sk_buff *skb;
740         char *data;
741         u16 len;
742         int err = 0;
743
744         lbs_deb_enter(LBS_DEB_SPI);
745
746         /* How many bytes are there to read? */
747         err = spu_read_u16(card, IF_SPI_SCRATCH_1_REG, &len);
748         if (err)
749                 goto out;
750         if (!len) {
751                 netdev_err(priv->dev, "%s: error: card has no data for host\n",
752                            __func__);
753                 err = -EINVAL;
754                 goto out;
755         } else if (len > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
756                 netdev_err(priv->dev,
757                            "%s: error: card has %d bytes of data, but our maximum skb size is %zu\n",
758                            __func__, len, MRVDRV_ETH_RX_PACKET_BUFFER_SIZE);
759                 err = -EINVAL;
760                 goto out;
761         }
762
763         /* TODO: should we allocate a smaller skb if we have less data? */
764         skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE);
765         if (!skb) {
766                 err = -ENOBUFS;
767                 goto out;
768         }
769         skb_reserve(skb, IPFIELD_ALIGN_OFFSET);
770         data = skb_put(skb, len);
771
772         /* Read the data from the WLAN module into our skb... */
773         err = spu_read(card, IF_SPI_DATA_RDWRPORT_REG, data, ALIGN(len, 4));
774         if (err)
775                 goto free_skb;
776
777         /* pass the SKB to libertas */
778         err = lbs_process_rxed_packet(card->priv, skb);
779         if (err)
780                 goto free_skb;
781
782         /* success */
783         goto out;
784
785 free_skb:
786         dev_kfree_skb(skb);
787 out:
788         if (err)
789                 netdev_err(priv->dev, "%s: err=%d\n", __func__, err);
790         lbs_deb_leave(LBS_DEB_SPI);
791         return err;
792 }
793
794 /* Move data or a command from the host to the card. */
795 static void if_spi_h2c(struct if_spi_card *card,
796                         struct if_spi_packet *packet, int type)
797 {
798         struct lbs_private *priv = card->priv;
799         int err = 0;
800         u16 int_type, port_reg;
801
802         switch (type) {
803         case MVMS_DAT:
804                 int_type = IF_SPI_CIC_TX_DOWNLOAD_OVER;
805                 port_reg = IF_SPI_DATA_RDWRPORT_REG;
806                 break;
807         case MVMS_CMD:
808                 int_type = IF_SPI_CIC_CMD_DOWNLOAD_OVER;
809                 port_reg = IF_SPI_CMD_RDWRPORT_REG;
810                 break;
811         default:
812                 netdev_err(priv->dev, "can't transfer buffer of type %d\n",
813                            type);
814                 err = -EINVAL;
815                 goto out;
816         }
817
818         /* Write the data to the card */
819         err = spu_write(card, port_reg, packet->buffer, packet->blen);
820         if (err)
821                 goto out;
822
823 out:
824         kfree(packet);
825
826         if (err)
827                 netdev_err(priv->dev, "%s: error %d\n", __func__, err);
828 }
829
830 /* Inform the host about a card event */
831 static void if_spi_e2h(struct if_spi_card *card)
832 {
833         int err = 0;
834         u32 cause;
835         struct lbs_private *priv = card->priv;
836
837         err = spu_read_u32(card, IF_SPI_SCRATCH_3_REG, &cause);
838         if (err)
839                 goto out;
840
841         /* re-enable the card event interrupt */
842         spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG,
843                         ~IF_SPI_HICU_CARD_EVENT);
844
845         /* generate a card interrupt */
846         spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG, IF_SPI_CIC_HOST_EVENT);
847
848         lbs_queue_event(priv, cause & 0xff);
849 out:
850         if (err)
851                 netdev_err(priv->dev, "%s: error %d\n", __func__, err);
852 }
853
854 static void if_spi_host_to_card_worker(struct work_struct *work)
855 {
856         int err;
857         struct if_spi_card *card;
858         u16 hiStatus;
859         unsigned long flags;
860         struct if_spi_packet *packet;
861         struct lbs_private *priv;
862
863         card = container_of(work, struct if_spi_card, packet_work);
864         priv = card->priv;
865
866         lbs_deb_enter(LBS_DEB_SPI);
867
868         /*
869          * Read the host interrupt status register to see what we
870          * can do.
871          */
872         err = spu_read_u16(card, IF_SPI_HOST_INT_STATUS_REG,
873                                 &hiStatus);
874         if (err) {
875                 netdev_err(priv->dev, "I/O error\n");
876                 goto err;
877         }
878
879         if (hiStatus & IF_SPI_HIST_CMD_UPLOAD_RDY) {
880                 err = if_spi_c2h_cmd(card);
881                 if (err)
882                         goto err;
883         }
884         if (hiStatus & IF_SPI_HIST_RX_UPLOAD_RDY) {
885                 err = if_spi_c2h_data(card);
886                 if (err)
887                         goto err;
888         }
889
890         /*
891          * workaround: in PS mode, the card does not set the Command
892          * Download Ready bit, but it sets TX Download Ready.
893          */
894         if (hiStatus & IF_SPI_HIST_CMD_DOWNLOAD_RDY ||
895            (card->priv->psstate != PS_STATE_FULL_POWER &&
896             (hiStatus & IF_SPI_HIST_TX_DOWNLOAD_RDY))) {
897                 /*
898                  * This means two things. First of all,
899                  * if there was a previous command sent, the card has
900                  * successfully received it.
901                  * Secondly, it is now ready to download another
902                  * command.
903                  */
904                 lbs_host_to_card_done(card->priv);
905
906                 /* Do we have any command packets from the host to send? */
907                 packet = NULL;
908                 spin_lock_irqsave(&card->buffer_lock, flags);
909                 if (!list_empty(&card->cmd_packet_list)) {
910                         packet = (struct if_spi_packet *)(card->
911                                         cmd_packet_list.next);
912                         list_del(&packet->list);
913                 }
914                 spin_unlock_irqrestore(&card->buffer_lock, flags);
915
916                 if (packet)
917                         if_spi_h2c(card, packet, MVMS_CMD);
918         }
919         if (hiStatus & IF_SPI_HIST_TX_DOWNLOAD_RDY) {
920                 /* Do we have any data packets from the host to send? */
921                 packet = NULL;
922                 spin_lock_irqsave(&card->buffer_lock, flags);
923                 if (!list_empty(&card->data_packet_list)) {
924                         packet = (struct if_spi_packet *)(card->
925                                         data_packet_list.next);
926                         list_del(&packet->list);
927                 }
928                 spin_unlock_irqrestore(&card->buffer_lock, flags);
929
930                 if (packet)
931                         if_spi_h2c(card, packet, MVMS_DAT);
932         }
933         if (hiStatus & IF_SPI_HIST_CARD_EVENT)
934                 if_spi_e2h(card);
935
936 err:
937         if (err)
938                 netdev_err(priv->dev, "%s: got error %d\n", __func__, err);
939
940         lbs_deb_leave(LBS_DEB_SPI);
941 }
942
943 /*
944  * Host to Card
945  *
946  * Called from Libertas to transfer some data to the WLAN device
947  * We can't sleep here.
948  */
949 static int if_spi_host_to_card(struct lbs_private *priv,
950                                 u8 type, u8 *buf, u16 nb)
951 {
952         int err = 0;
953         unsigned long flags;
954         struct if_spi_card *card = priv->card;
955         struct if_spi_packet *packet;
956         u16 blen;
957
958         lbs_deb_enter_args(LBS_DEB_SPI, "type %d, bytes %d", type, nb);
959
960         if (nb == 0) {
961                 netdev_err(priv->dev, "%s: invalid size requested: %d\n",
962                            __func__, nb);
963                 err = -EINVAL;
964                 goto out;
965         }
966         blen = ALIGN(nb, 4);
967         packet = kzalloc(sizeof(struct if_spi_packet) + blen, GFP_ATOMIC);
968         if (!packet) {
969                 err = -ENOMEM;
970                 goto out;
971         }
972         packet->blen = blen;
973         memcpy(packet->buffer, buf, nb);
974         memset(packet->buffer + nb, 0, blen - nb);
975
976         switch (type) {
977         case MVMS_CMD:
978                 priv->dnld_sent = DNLD_CMD_SENT;
979                 spin_lock_irqsave(&card->buffer_lock, flags);
980                 list_add_tail(&packet->list, &card->cmd_packet_list);
981                 spin_unlock_irqrestore(&card->buffer_lock, flags);
982                 break;
983         case MVMS_DAT:
984                 priv->dnld_sent = DNLD_DATA_SENT;
985                 spin_lock_irqsave(&card->buffer_lock, flags);
986                 list_add_tail(&packet->list, &card->data_packet_list);
987                 spin_unlock_irqrestore(&card->buffer_lock, flags);
988                 break;
989         default:
990                 kfree(packet);
991                 netdev_err(priv->dev, "can't transfer buffer of type %d\n",
992                            type);
993                 err = -EINVAL;
994                 break;
995         }
996
997         /* Queue spi xfer work */
998         queue_work(card->workqueue, &card->packet_work);
999 out:
1000         lbs_deb_leave_args(LBS_DEB_SPI, "err=%d", err);
1001         return err;
1002 }
1003
1004 /*
1005  * Host Interrupts
1006  *
1007  * Service incoming interrupts from the WLAN device. We can't sleep here, so
1008  * don't try to talk on the SPI bus, just queue the SPI xfer work.
1009  */
1010 static irqreturn_t if_spi_host_interrupt(int irq, void *dev_id)
1011 {
1012         struct if_spi_card *card = dev_id;
1013
1014         queue_work(card->workqueue, &card->packet_work);
1015
1016         return IRQ_HANDLED;
1017 }
1018
1019 /*
1020  * SPI callbacks
1021  */
1022
1023 static int if_spi_init_card(struct if_spi_card *card)
1024 {
1025         struct lbs_private *priv = card->priv;
1026         int err, i;
1027         u32 scratch;
1028         const struct firmware *helper = NULL;
1029         const struct firmware *mainfw = NULL;
1030
1031         lbs_deb_enter(LBS_DEB_SPI);
1032
1033         err = spu_init(card, card->pdata->use_dummy_writes);
1034         if (err)
1035                 goto out;
1036         err = spu_get_chip_revision(card, &card->card_id, &card->card_rev);
1037         if (err)
1038                 goto out;
1039
1040         err = spu_read_u32(card, IF_SPI_SCRATCH_4_REG, &scratch);
1041         if (err)
1042                 goto out;
1043         if (scratch == SUCCESSFUL_FW_DOWNLOAD_MAGIC)
1044                 lbs_deb_spi("Firmware is already loaded for "
1045                             "Marvell WLAN 802.11 adapter\n");
1046         else {
1047                 /* Check if we support this card */
1048                 for (i = 0; i < ARRAY_SIZE(fw_table); i++) {
1049                         if (card->card_id == fw_table[i].model)
1050                                 break;
1051                 }
1052                 if (i == ARRAY_SIZE(fw_table)) {
1053                         netdev_err(priv->dev, "Unsupported chip_id: 0x%02x\n",
1054                                    card->card_id);
1055                         err = -ENODEV;
1056                         goto out;
1057                 }
1058
1059                 err = lbs_get_firmware(&card->spi->dev, NULL, NULL,
1060                                         card->card_id, &fw_table[0], &helper,
1061                                         &mainfw);
1062                 if (err) {
1063                         netdev_err(priv->dev, "failed to find firmware (%d)\n",
1064                                    err);
1065                         goto out;
1066                 }
1067
1068                 lbs_deb_spi("Initializing FW for Marvell WLAN 802.11 adapter "
1069                                 "(chip_id = 0x%04x, chip_rev = 0x%02x) "
1070                                 "attached to SPI bus_num %d, chip_select %d. "
1071                                 "spi->max_speed_hz=%d\n",
1072                                 card->card_id, card->card_rev,
1073                                 card->spi->master->bus_num,
1074                                 card->spi->chip_select,
1075                                 card->spi->max_speed_hz);
1076                 err = if_spi_prog_helper_firmware(card, helper);
1077                 if (err)
1078                         goto out;
1079                 err = if_spi_prog_main_firmware(card, mainfw);
1080                 if (err)
1081                         goto out;
1082                 lbs_deb_spi("loaded FW for Marvell WLAN 802.11 adapter\n");
1083         }
1084
1085         err = spu_set_interrupt_mode(card, 0, 1);
1086         if (err)
1087                 goto out;
1088
1089 out:
1090         if (helper)
1091                 release_firmware(helper);
1092         if (mainfw)
1093                 release_firmware(mainfw);
1094
1095         lbs_deb_leave_args(LBS_DEB_SPI, "err %d\n", err);
1096
1097         return err;
1098 }
1099
1100 static void if_spi_resume_worker(struct work_struct *work)
1101 {
1102         struct if_spi_card *card;
1103
1104         card = container_of(work, struct if_spi_card, resume_work);
1105
1106         if (card->suspended) {
1107                 if (card->pdata->setup)
1108                         card->pdata->setup(card->spi);
1109
1110                 /* Init card ... */
1111                 if_spi_init_card(card);
1112
1113                 enable_irq(card->spi->irq);
1114
1115                 /* And resume it ... */
1116                 lbs_resume(card->priv);
1117
1118                 card->suspended = 0;
1119         }
1120 }
1121
1122 static int __devinit if_spi_probe(struct spi_device *spi)
1123 {
1124         struct if_spi_card *card;
1125         struct lbs_private *priv = NULL;
1126         struct libertas_spi_platform_data *pdata = spi->dev.platform_data;
1127         int err = 0;
1128
1129         lbs_deb_enter(LBS_DEB_SPI);
1130
1131         if (!pdata) {
1132                 err = -EINVAL;
1133                 goto out;
1134         }
1135
1136         if (pdata->setup) {
1137                 err = pdata->setup(spi);
1138                 if (err)
1139                         goto out;
1140         }
1141
1142         /* Allocate card structure to represent this specific device */
1143         card = kzalloc(sizeof(struct if_spi_card), GFP_KERNEL);
1144         if (!card) {
1145                 err = -ENOMEM;
1146                 goto teardown;
1147         }
1148         spi_set_drvdata(spi, card);
1149         card->pdata = pdata;
1150         card->spi = spi;
1151         card->prev_xfer_time = jiffies;
1152
1153         INIT_LIST_HEAD(&card->cmd_packet_list);
1154         INIT_LIST_HEAD(&card->data_packet_list);
1155         spin_lock_init(&card->buffer_lock);
1156
1157         /* Initialize the SPI Interface Unit */
1158
1159         /* Firmware load */
1160         err = if_spi_init_card(card);
1161         if (err)
1162                 goto free_card;
1163
1164         /*
1165          * Register our card with libertas.
1166          * This will call alloc_etherdev.
1167          */
1168         priv = lbs_add_card(card, &spi->dev);
1169         if (!priv) {
1170                 err = -ENOMEM;
1171                 goto free_card;
1172         }
1173         card->priv = priv;
1174         priv->setup_fw_on_resume = 1;
1175         priv->card = card;
1176         priv->hw_host_to_card = if_spi_host_to_card;
1177         priv->enter_deep_sleep = NULL;
1178         priv->exit_deep_sleep = NULL;
1179         priv->reset_deep_sleep_wakeup = NULL;
1180         priv->fw_ready = 1;
1181
1182         /* Initialize interrupt handling stuff. */
1183         card->workqueue = create_workqueue("libertas_spi");
1184         INIT_WORK(&card->packet_work, if_spi_host_to_card_worker);
1185         INIT_WORK(&card->resume_work, if_spi_resume_worker);
1186
1187         err = request_irq(spi->irq, if_spi_host_interrupt,
1188                         IRQF_TRIGGER_FALLING, "libertas_spi", card);
1189         if (err) {
1190                 pr_err("can't get host irq line-- request_irq failed\n");
1191                 goto terminate_workqueue;
1192         }
1193
1194         /*
1195          * Start the card.
1196          * This will call register_netdev, and we'll start
1197          * getting interrupts...
1198          */
1199         err = lbs_start_card(priv);
1200         if (err)
1201                 goto release_irq;
1202
1203         lbs_deb_spi("Finished initializing WLAN module.\n");
1204
1205         /* successful exit */
1206         goto out;
1207
1208 release_irq:
1209         free_irq(spi->irq, card);
1210 terminate_workqueue:
1211         flush_workqueue(card->workqueue);
1212         destroy_workqueue(card->workqueue);
1213         lbs_remove_card(priv); /* will call free_netdev */
1214 free_card:
1215         free_if_spi_card(card);
1216 teardown:
1217         if (pdata->teardown)
1218                 pdata->teardown(spi);
1219 out:
1220         lbs_deb_leave_args(LBS_DEB_SPI, "err %d\n", err);
1221         return err;
1222 }
1223
1224 static int __devexit libertas_spi_remove(struct spi_device *spi)
1225 {
1226         struct if_spi_card *card = spi_get_drvdata(spi);
1227         struct lbs_private *priv = card->priv;
1228
1229         lbs_deb_spi("libertas_spi_remove\n");
1230         lbs_deb_enter(LBS_DEB_SPI);
1231
1232         cancel_work_sync(&card->resume_work);
1233
1234         lbs_stop_card(priv);
1235         lbs_remove_card(priv); /* will call free_netdev */
1236
1237         free_irq(spi->irq, card);
1238         flush_workqueue(card->workqueue);
1239         destroy_workqueue(card->workqueue);
1240         if (card->pdata->teardown)
1241                 card->pdata->teardown(spi);
1242         free_if_spi_card(card);
1243         lbs_deb_leave(LBS_DEB_SPI);
1244         return 0;
1245 }
1246
1247 static int if_spi_suspend(struct device *dev)
1248 {
1249         struct spi_device *spi = to_spi_device(dev);
1250         struct if_spi_card *card = spi_get_drvdata(spi);
1251
1252         if (!card->suspended) {
1253                 lbs_suspend(card->priv);
1254                 flush_workqueue(card->workqueue);
1255                 disable_irq(spi->irq);
1256
1257                 if (card->pdata->teardown)
1258                         card->pdata->teardown(spi);
1259                 card->suspended = 1;
1260         }
1261
1262         return 0;
1263 }
1264
1265 static int if_spi_resume(struct device *dev)
1266 {
1267         struct spi_device *spi = to_spi_device(dev);
1268         struct if_spi_card *card = spi_get_drvdata(spi);
1269
1270         /* Schedule delayed work */
1271         schedule_work(&card->resume_work);
1272
1273         return 0;
1274 }
1275
1276 static const struct dev_pm_ops if_spi_pm_ops = {
1277         .suspend        = if_spi_suspend,
1278         .resume         = if_spi_resume,
1279 };
1280
1281 static struct spi_driver libertas_spi_driver = {
1282         .probe  = if_spi_probe,
1283         .remove = __devexit_p(libertas_spi_remove),
1284         .driver = {
1285                 .name   = "libertas_spi",
1286                 .owner  = THIS_MODULE,
1287                 .pm     = &if_spi_pm_ops,
1288         },
1289 };
1290
1291 /*
1292  * Module functions
1293  */
1294
1295 static int __init if_spi_init_module(void)
1296 {
1297         int ret = 0;
1298         lbs_deb_enter(LBS_DEB_SPI);
1299         printk(KERN_INFO "libertas_spi: Libertas SPI driver\n");
1300         ret = spi_register_driver(&libertas_spi_driver);
1301         lbs_deb_leave(LBS_DEB_SPI);
1302         return ret;
1303 }
1304
1305 static void __exit if_spi_exit_module(void)
1306 {
1307         lbs_deb_enter(LBS_DEB_SPI);
1308         spi_unregister_driver(&libertas_spi_driver);
1309         lbs_deb_leave(LBS_DEB_SPI);
1310 }
1311
1312 module_init(if_spi_init_module);
1313 module_exit(if_spi_exit_module);
1314
1315 MODULE_DESCRIPTION("Libertas SPI WLAN Driver");
1316 MODULE_AUTHOR("Andrey Yurovsky <andrey@cozybit.com>, "
1317               "Colin McCabe <colin@cozybit.com>");
1318 MODULE_LICENSE("GPL");
1319 MODULE_ALIAS("spi:libertas_spi");