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