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