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