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