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