blob: 3ea03f259ee764c864b29fc819917f7750d4c2ae [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>
Colin McCabed2b21f12009-01-09 14:58:09 -080022#include <linux/jiffies.h>
23#include <linux/kthread.h>
24#include <linux/list.h>
25#include <linux/netdevice.h>
Randy Dunlap371ba882010-02-03 15:31:07 -080026#include <linux/semaphore.h>
Colin McCabed2b21f12009-01-09 14:58:09 -080027#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
Colin McCabed2b21f12009-01-09 14:58:09 -080036struct if_spi_card {
37 struct spi_device *spi;
38 struct lbs_private *priv;
Mike Rapoport0c2bec92009-02-03 09:04:20 +020039 struct libertas_spi_platform_data *pdata;
Colin McCabed2b21f12009-01-09 14:58:09 -080040
John W. Linvillecadeba32009-05-27 00:49:36 +020041 char helper_fw_name[IF_SPI_FW_NAME_MAX];
42 char main_fw_name[IF_SPI_FW_NAME_MAX];
Colin McCabed2b21f12009-01-09 14:58:09 -080043
44 /* The card ID and card revision, as reported by the hardware. */
45 u16 card_id;
46 u8 card_rev;
47
Colin McCabed2b21f12009-01-09 14:58:09 -080048 /* The last time that we initiated an SPU operation */
49 unsigned long prev_xfer_time;
50
51 int use_dummy_writes;
52 unsigned long spu_port_delay;
53 unsigned long spu_reg_delay;
54
55 /* Handles all SPI communication (except for FW load) */
56 struct task_struct *spi_thread;
57 int run_thread;
58
59 /* Used to wake up the spi_thread */
60 struct semaphore spi_ready;
61 struct semaphore spi_thread_terminated;
62
63 u8 cmd_buffer[IF_SPI_CMD_BUF_SIZE];
Colin McCabed2b21f12009-01-09 14:58:09 -080064};
65
66static void free_if_spi_card(struct if_spi_card *card)
67{
Colin McCabed2b21f12009-01-09 14:58:09 -080068 spi_set_drvdata(card->spi, NULL);
69 kfree(card);
70}
71
72static struct chip_ident chip_id_to_device_name[] = {
73 { .chip_id = 0x04, .name = 8385 },
74 { .chip_id = 0x0b, .name = 8686 },
75};
76
77/*
78 * SPI Interface Unit Routines
79 *
80 * The SPU sits between the host and the WLAN module.
81 * All communication with the firmware is through SPU transactions.
82 *
83 * First we have to put a SPU register name on the bus. Then we can
84 * either read from or write to that register.
85 *
Colin McCabed2b21f12009-01-09 14:58:09 -080086 */
87
88static void spu_transaction_init(struct if_spi_card *card)
89{
90 if (!time_after(jiffies, card->prev_xfer_time + 1)) {
91 /* Unfortunately, the SPU requires a delay between successive
92 * transactions. If our last transaction was more than a jiffy
93 * ago, we have obviously already delayed enough.
94 * If not, we have to busy-wait to be on the safe side. */
95 ndelay(400);
96 }
Colin McCabed2b21f12009-01-09 14:58:09 -080097}
98
99static void spu_transaction_finish(struct if_spi_card *card)
100{
Colin McCabed2b21f12009-01-09 14:58:09 -0800101 card->prev_xfer_time = jiffies;
102}
103
104/* Write out a byte buffer to an SPI register,
105 * using a series of 16-bit transfers. */
106static int spu_write(struct if_spi_card *card, u16 reg, const u8 *buf, int len)
107{
108 int err = 0;
Holger Schurigd18ba452009-10-22 15:30:44 +0200109 __le16 reg_out = cpu_to_le16(reg | IF_SPI_WRITE_OPERATION_MASK);
Sebastian Andrzej Siewior4d1d4982009-06-04 21:57:03 +0200110 struct spi_message m;
111 struct spi_transfer reg_trans;
112 struct spi_transfer data_trans;
113
114 spi_message_init(&m);
115 memset(&reg_trans, 0, sizeof(reg_trans));
116 memset(&data_trans, 0, sizeof(data_trans));
Colin McCabed2b21f12009-01-09 14:58:09 -0800117
118 /* You must give an even number of bytes to the SPU, even if it
119 * doesn't care about the last one. */
120 BUG_ON(len & 0x1);
121
122 spu_transaction_init(card);
123
124 /* write SPU register index */
Sebastian Andrzej Siewior4d1d4982009-06-04 21:57:03 +0200125 reg_trans.tx_buf = &reg_out;
126 reg_trans.len = sizeof(reg_out);
Colin McCabed2b21f12009-01-09 14:58:09 -0800127
Sebastian Andrzej Siewior4d1d4982009-06-04 21:57:03 +0200128 data_trans.tx_buf = buf;
129 data_trans.len = len;
Colin McCabed2b21f12009-01-09 14:58:09 -0800130
Sebastian Andrzej Siewior4d1d4982009-06-04 21:57:03 +0200131 spi_message_add_tail(&reg_trans, &m);
132 spi_message_add_tail(&data_trans, &m);
133
134 err = spi_sync(card->spi, &m);
Colin McCabed2b21f12009-01-09 14:58:09 -0800135 spu_transaction_finish(card);
136 return err;
137}
138
139static inline int spu_write_u16(struct if_spi_card *card, u16 reg, u16 val)
140{
Holger Schurigd18ba452009-10-22 15:30:44 +0200141 __le16 buff;
Colin McCabed2b21f12009-01-09 14:58:09 -0800142
Sebastian Andrzej Siewiorf488b722009-05-22 21:19:40 +0200143 buff = cpu_to_le16(val);
144 return spu_write(card, reg, (u8 *)&buff, sizeof(u16));
Colin McCabed2b21f12009-01-09 14:58:09 -0800145}
146
147static inline int spu_reg_is_port_reg(u16 reg)
148{
149 switch (reg) {
150 case IF_SPI_IO_RDWRPORT_REG:
151 case IF_SPI_CMD_RDWRPORT_REG:
152 case IF_SPI_DATA_RDWRPORT_REG:
153 return 1;
154 default:
155 return 0;
156 }
157}
158
159static int spu_read(struct if_spi_card *card, u16 reg, u8 *buf, int len)
160{
Sebastian Andrzej Siewior4d1d4982009-06-04 21:57:03 +0200161 unsigned int delay;
Colin McCabed2b21f12009-01-09 14:58:09 -0800162 int err = 0;
Holger Schurigd18ba452009-10-22 15:30:44 +0200163 __le16 reg_out = cpu_to_le16(reg | IF_SPI_READ_OPERATION_MASK);
Sebastian Andrzej Siewior4d1d4982009-06-04 21:57:03 +0200164 struct spi_message m;
165 struct spi_transfer reg_trans;
166 struct spi_transfer dummy_trans;
167 struct spi_transfer data_trans;
Colin McCabed2b21f12009-01-09 14:58:09 -0800168
169 /* You must take an even number of bytes from the SPU, even if you
170 * don't care about the last one. */
171 BUG_ON(len & 0x1);
172
173 spu_transaction_init(card);
174
Sebastian Andrzej Siewior4d1d4982009-06-04 21:57:03 +0200175 spi_message_init(&m);
176 memset(&reg_trans, 0, sizeof(reg_trans));
177 memset(&dummy_trans, 0, sizeof(dummy_trans));
178 memset(&data_trans, 0, sizeof(data_trans));
179
Colin McCabed2b21f12009-01-09 14:58:09 -0800180 /* write SPU register index */
Sebastian Andrzej Siewior4d1d4982009-06-04 21:57:03 +0200181 reg_trans.tx_buf = &reg_out;
182 reg_trans.len = sizeof(reg_out);
183 spi_message_add_tail(&reg_trans, &m);
Colin McCabed2b21f12009-01-09 14:58:09 -0800184
185 delay = spu_reg_is_port_reg(reg) ? card->spu_port_delay :
186 card->spu_reg_delay;
187 if (card->use_dummy_writes) {
188 /* Clock in dummy cycles while the SPU fills the FIFO */
Sebastian Andrzej Siewior4d1d4982009-06-04 21:57:03 +0200189 dummy_trans.len = delay / 8;
190 spi_message_add_tail(&dummy_trans, &m);
Colin McCabed2b21f12009-01-09 14:58:09 -0800191 } else {
192 /* Busy-wait while the SPU fills the FIFO */
Sebastian Andrzej Siewior4d1d4982009-06-04 21:57:03 +0200193 reg_trans.delay_usecs =
194 DIV_ROUND_UP((100 + (delay * 10)), 1000);
Colin McCabed2b21f12009-01-09 14:58:09 -0800195 }
196
197 /* read in data */
Sebastian Andrzej Siewior4d1d4982009-06-04 21:57:03 +0200198 data_trans.rx_buf = buf;
199 data_trans.len = len;
200 spi_message_add_tail(&data_trans, &m);
Colin McCabed2b21f12009-01-09 14:58:09 -0800201
Sebastian Andrzej Siewior4d1d4982009-06-04 21:57:03 +0200202 err = spi_sync(card->spi, &m);
Colin McCabed2b21f12009-01-09 14:58:09 -0800203 spu_transaction_finish(card);
204 return err;
205}
206
207/* Read 16 bits from an SPI register */
208static inline int spu_read_u16(struct if_spi_card *card, u16 reg, u16 *val)
209{
Holger Schurigd18ba452009-10-22 15:30:44 +0200210 __le16 buf;
Sebastian Andrzej Siewiorf488b722009-05-22 21:19:40 +0200211 int ret;
212
213 ret = spu_read(card, reg, (u8 *)&buf, sizeof(buf));
214 if (ret == 0)
215 *val = le16_to_cpup(&buf);
216 return ret;
Colin McCabed2b21f12009-01-09 14:58:09 -0800217}
218
219/* Read 32 bits from an SPI register.
220 * The low 16 bits are read first. */
221static int spu_read_u32(struct if_spi_card *card, u16 reg, u32 *val)
222{
Holger Schurigd18ba452009-10-22 15:30:44 +0200223 __le32 buf;
Colin McCabed2b21f12009-01-09 14:58:09 -0800224 int err;
Sebastian Andrzej Siewiorf488b722009-05-22 21:19:40 +0200225
226 err = spu_read(card, reg, (u8 *)&buf, sizeof(buf));
Colin McCabed2b21f12009-01-09 14:58:09 -0800227 if (!err)
Sebastian Andrzej Siewiorf488b722009-05-22 21:19:40 +0200228 *val = le32_to_cpup(&buf);
Colin McCabed2b21f12009-01-09 14:58:09 -0800229 return err;
230}
231
232/* Keep reading 16 bits from an SPI register until you get the correct result.
233 *
234 * If mask = 0, the correct result is any non-zero number.
235 * If mask != 0, the correct result is any number where
236 * number & target_mask == target
237 *
238 * Returns -ETIMEDOUT if a second passes without the correct result. */
239static int spu_wait_for_u16(struct if_spi_card *card, u16 reg,
240 u16 target_mask, u16 target)
241{
242 int err;
243 unsigned long timeout = jiffies + 5*HZ;
244 while (1) {
245 u16 val;
246 err = spu_read_u16(card, reg, &val);
247 if (err)
248 return err;
249 if (target_mask) {
250 if ((val & target_mask) == target)
251 return 0;
252 } else {
253 if (val)
254 return 0;
255 }
256 udelay(100);
257 if (time_after(jiffies, timeout)) {
258 lbs_pr_err("%s: timeout with val=%02x, "
259 "target_mask=%02x, target=%02x\n",
260 __func__, val, target_mask, target);
261 return -ETIMEDOUT;
262 }
263 }
264}
265
266/* Read 16 bits from an SPI register until you receive a specific value.
267 * Returns -ETIMEDOUT if a 4 tries pass without success. */
268static int spu_wait_for_u32(struct if_spi_card *card, u32 reg, u32 target)
269{
270 int err, try;
271 for (try = 0; try < 4; ++try) {
272 u32 val = 0;
273 err = spu_read_u32(card, reg, &val);
274 if (err)
275 return err;
276 if (val == target)
277 return 0;
278 mdelay(100);
279 }
280 return -ETIMEDOUT;
281}
282
283static int spu_set_interrupt_mode(struct if_spi_card *card,
284 int suppress_host_int,
285 int auto_int)
286{
287 int err = 0;
288
289 /* We can suppress a host interrupt by clearing the appropriate
290 * bit in the "host interrupt status mask" register */
291 if (suppress_host_int) {
292 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG, 0);
293 if (err)
294 return err;
295 } else {
296 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG,
297 IF_SPI_HISM_TX_DOWNLOAD_RDY |
298 IF_SPI_HISM_RX_UPLOAD_RDY |
299 IF_SPI_HISM_CMD_DOWNLOAD_RDY |
300 IF_SPI_HISM_CARDEVENT |
301 IF_SPI_HISM_CMD_UPLOAD_RDY);
302 if (err)
303 return err;
304 }
305
306 /* If auto-interrupts are on, the completion of certain transactions
307 * will trigger an interrupt automatically. If auto-interrupts
308 * are off, we need to set the "Card Interrupt Cause" register to
309 * trigger a card interrupt. */
310 if (auto_int) {
311 err = spu_write_u16(card, IF_SPI_HOST_INT_CTRL_REG,
312 IF_SPI_HICT_TX_DOWNLOAD_OVER_AUTO |
313 IF_SPI_HICT_RX_UPLOAD_OVER_AUTO |
314 IF_SPI_HICT_CMD_DOWNLOAD_OVER_AUTO |
315 IF_SPI_HICT_CMD_UPLOAD_OVER_AUTO);
316 if (err)
317 return err;
318 } else {
319 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG, 0);
320 if (err)
321 return err;
322 }
323 return err;
324}
325
326static int spu_get_chip_revision(struct if_spi_card *card,
327 u16 *card_id, u8 *card_rev)
328{
329 int err = 0;
330 u32 dev_ctrl;
331 err = spu_read_u32(card, IF_SPI_DEVICEID_CTRL_REG, &dev_ctrl);
332 if (err)
333 return err;
334 *card_id = IF_SPI_DEVICEID_CTRL_REG_TO_CARD_ID(dev_ctrl);
335 *card_rev = IF_SPI_DEVICEID_CTRL_REG_TO_CARD_REV(dev_ctrl);
336 return err;
337}
338
339static int spu_set_bus_mode(struct if_spi_card *card, u16 mode)
340{
341 int err = 0;
342 u16 rval;
343 /* set bus mode */
344 err = spu_write_u16(card, IF_SPI_SPU_BUS_MODE_REG, mode);
345 if (err)
346 return err;
347 /* Check that we were able to read back what we just wrote. */
348 err = spu_read_u16(card, IF_SPI_SPU_BUS_MODE_REG, &rval);
349 if (err)
350 return err;
Andrey Yurovsky88d89522009-07-31 11:35:19 -0700351 if ((rval & 0xF) != mode) {
Colin McCabed2b21f12009-01-09 14:58:09 -0800352 lbs_pr_err("Can't read bus mode register.\n");
353 return -EIO;
354 }
355 return 0;
356}
357
358static int spu_init(struct if_spi_card *card, int use_dummy_writes)
359{
360 int err = 0;
361 u32 delay;
362
363 /* We have to start up in timed delay mode so that we can safely
364 * read the Delay Read Register. */
365 card->use_dummy_writes = 0;
366 err = spu_set_bus_mode(card,
367 IF_SPI_BUS_MODE_SPI_CLOCK_PHASE_RISING |
368 IF_SPI_BUS_MODE_DELAY_METHOD_TIMED |
369 IF_SPI_BUS_MODE_16_BIT_ADDRESS_16_BIT_DATA);
370 if (err)
371 return err;
372 card->spu_port_delay = 1000;
373 card->spu_reg_delay = 1000;
374 err = spu_read_u32(card, IF_SPI_DELAY_READ_REG, &delay);
375 if (err)
376 return err;
377 card->spu_port_delay = delay & 0x0000ffff;
378 card->spu_reg_delay = (delay & 0xffff0000) >> 16;
379
380 /* If dummy clock delay mode has been requested, switch to it now */
381 if (use_dummy_writes) {
382 card->use_dummy_writes = 1;
383 err = spu_set_bus_mode(card,
384 IF_SPI_BUS_MODE_SPI_CLOCK_PHASE_RISING |
385 IF_SPI_BUS_MODE_DELAY_METHOD_DUMMY_CLOCK |
386 IF_SPI_BUS_MODE_16_BIT_ADDRESS_16_BIT_DATA);
387 if (err)
388 return err;
389 }
390
391 lbs_deb_spi("Initialized SPU unit. "
392 "spu_port_delay=0x%04lx, spu_reg_delay=0x%04lx\n",
393 card->spu_port_delay, card->spu_reg_delay);
394 return err;
395}
396
397/*
398 * Firmware Loading
399 */
400
401static int if_spi_prog_helper_firmware(struct if_spi_card *card)
402{
403 int err = 0;
404 const struct firmware *firmware = NULL;
405 int bytes_remaining;
406 const u8 *fw;
407 u8 temp[HELPER_FW_LOAD_CHUNK_SZ];
408 struct spi_device *spi = card->spi;
409
410 lbs_deb_enter(LBS_DEB_SPI);
411
412 err = spu_set_interrupt_mode(card, 1, 0);
413 if (err)
414 goto out;
415 /* Get helper firmware image */
416 err = request_firmware(&firmware, card->helper_fw_name, &spi->dev);
417 if (err) {
418 lbs_pr_err("request_firmware failed with err = %d\n", err);
419 goto out;
420 }
421 bytes_remaining = firmware->size;
422 fw = firmware->data;
423
424 /* Load helper firmware image */
425 while (bytes_remaining > 0) {
426 /* Scratch pad 1 should contain the number of bytes we
427 * want to download to the firmware */
428 err = spu_write_u16(card, IF_SPI_SCRATCH_1_REG,
429 HELPER_FW_LOAD_CHUNK_SZ);
430 if (err)
431 goto release_firmware;
432
433 err = spu_wait_for_u16(card, IF_SPI_HOST_INT_STATUS_REG,
434 IF_SPI_HIST_CMD_DOWNLOAD_RDY,
435 IF_SPI_HIST_CMD_DOWNLOAD_RDY);
436 if (err)
437 goto release_firmware;
438
439 /* Feed the data into the command read/write port reg
440 * in chunks of 64 bytes */
441 memset(temp, 0, sizeof(temp));
442 memcpy(temp, fw,
443 min(bytes_remaining, HELPER_FW_LOAD_CHUNK_SZ));
444 mdelay(10);
445 err = spu_write(card, IF_SPI_CMD_RDWRPORT_REG,
446 temp, HELPER_FW_LOAD_CHUNK_SZ);
447 if (err)
448 goto release_firmware;
449
450 /* Interrupt the boot code */
451 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
452 if (err)
453 goto release_firmware;
454 err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG,
455 IF_SPI_CIC_CMD_DOWNLOAD_OVER);
456 if (err)
457 goto release_firmware;
458 bytes_remaining -= HELPER_FW_LOAD_CHUNK_SZ;
459 fw += HELPER_FW_LOAD_CHUNK_SZ;
460 }
461
462 /* Once the helper / single stage firmware download is complete,
463 * write 0 to scratch pad 1 and interrupt the
464 * bootloader. This completes the helper download. */
465 err = spu_write_u16(card, IF_SPI_SCRATCH_1_REG, FIRMWARE_DNLD_OK);
466 if (err)
467 goto release_firmware;
468 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
469 if (err)
470 goto release_firmware;
471 err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG,
472 IF_SPI_CIC_CMD_DOWNLOAD_OVER);
473 goto release_firmware;
474
475 lbs_deb_spi("waiting for helper to boot...\n");
476
477release_firmware:
478 release_firmware(firmware);
479out:
480 if (err)
481 lbs_pr_err("failed to load helper firmware (err=%d)\n", err);
482 lbs_deb_leave_args(LBS_DEB_SPI, "err %d", err);
483 return err;
484}
485
486/* Returns the length of the next packet the firmware expects us to send
487 * Sets crc_err if the previous transfer had a CRC error. */
488static int if_spi_prog_main_firmware_check_len(struct if_spi_card *card,
489 int *crc_err)
490{
491 u16 len;
492 int err = 0;
493
494 /* wait until the host interrupt status register indicates
495 * that we are ready to download */
496 err = spu_wait_for_u16(card, IF_SPI_HOST_INT_STATUS_REG,
497 IF_SPI_HIST_CMD_DOWNLOAD_RDY,
498 IF_SPI_HIST_CMD_DOWNLOAD_RDY);
499 if (err) {
500 lbs_pr_err("timed out waiting for host_int_status\n");
501 return err;
502 }
503
504 /* Ask the device how many bytes of firmware it wants. */
505 err = spu_read_u16(card, IF_SPI_SCRATCH_1_REG, &len);
506 if (err)
507 return err;
508
509 if (len > IF_SPI_CMD_BUF_SIZE) {
510 lbs_pr_err("firmware load device requested a larger "
511 "tranfer than we are prepared to "
512 "handle. (len = %d)\n", len);
513 return -EIO;
514 }
515 if (len & 0x1) {
516 lbs_deb_spi("%s: crc error\n", __func__);
517 len &= ~0x1;
518 *crc_err = 1;
519 } else
520 *crc_err = 0;
521
522 return len;
523}
524
525static int if_spi_prog_main_firmware(struct if_spi_card *card)
526{
527 int len, prev_len;
528 int bytes, crc_err = 0, err = 0;
529 const struct firmware *firmware = NULL;
530 const u8 *fw;
531 struct spi_device *spi = card->spi;
532 u16 num_crc_errs;
533
534 lbs_deb_enter(LBS_DEB_SPI);
535
536 err = spu_set_interrupt_mode(card, 1, 0);
537 if (err)
538 goto out;
539
540 /* Get firmware image */
541 err = request_firmware(&firmware, card->main_fw_name, &spi->dev);
542 if (err) {
543 lbs_pr_err("%s: can't get firmware '%s' from kernel. "
544 "err = %d\n", __func__, card->main_fw_name, err);
545 goto out;
546 }
547
548 err = spu_wait_for_u16(card, IF_SPI_SCRATCH_1_REG, 0, 0);
549 if (err) {
550 lbs_pr_err("%s: timed out waiting for initial "
551 "scratch reg = 0\n", __func__);
552 goto release_firmware;
553 }
554
555 num_crc_errs = 0;
556 prev_len = 0;
557 bytes = firmware->size;
558 fw = firmware->data;
559 while ((len = if_spi_prog_main_firmware_check_len(card, &crc_err))) {
560 if (len < 0) {
561 err = len;
562 goto release_firmware;
563 }
564 if (bytes < 0) {
565 /* If there are no more bytes left, we would normally
566 * expect to have terminated with len = 0 */
567 lbs_pr_err("Firmware load wants more bytes "
568 "than we have to offer.\n");
569 break;
570 }
571 if (crc_err) {
572 /* Previous transfer failed. */
573 if (++num_crc_errs > MAX_MAIN_FW_LOAD_CRC_ERR) {
574 lbs_pr_err("Too many CRC errors encountered "
575 "in firmware load.\n");
576 err = -EIO;
577 goto release_firmware;
578 }
579 } else {
580 /* Previous transfer succeeded. Advance counters. */
581 bytes -= prev_len;
582 fw += prev_len;
583 }
584 if (bytes < len) {
585 memset(card->cmd_buffer, 0, len);
586 memcpy(card->cmd_buffer, fw, bytes);
587 } else
588 memcpy(card->cmd_buffer, fw, len);
589
590 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
591 if (err)
592 goto release_firmware;
593 err = spu_write(card, IF_SPI_CMD_RDWRPORT_REG,
594 card->cmd_buffer, len);
595 if (err)
596 goto release_firmware;
597 err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG ,
598 IF_SPI_CIC_CMD_DOWNLOAD_OVER);
599 if (err)
600 goto release_firmware;
601 prev_len = len;
602 }
603 if (bytes > prev_len) {
604 lbs_pr_err("firmware load wants fewer bytes than "
605 "we have to offer.\n");
606 }
607
608 /* Confirm firmware download */
609 err = spu_wait_for_u32(card, IF_SPI_SCRATCH_4_REG,
610 SUCCESSFUL_FW_DOWNLOAD_MAGIC);
611 if (err) {
612 lbs_pr_err("failed to confirm the firmware download\n");
613 goto release_firmware;
614 }
615
616release_firmware:
617 release_firmware(firmware);
618
619out:
620 if (err)
621 lbs_pr_err("failed to load firmware (err=%d)\n", err);
622 lbs_deb_leave_args(LBS_DEB_SPI, "err %d", err);
623 return err;
624}
625
626/*
627 * SPI Transfer Thread
628 *
629 * The SPI thread handles all SPI transfers, so there is no need for a lock.
630 */
631
632/* Move a command from the card to the host */
633static int if_spi_c2h_cmd(struct if_spi_card *card)
634{
635 struct lbs_private *priv = card->priv;
636 unsigned long flags;
637 int err = 0;
638 u16 len;
639 u8 i;
640
641 /* We need a buffer big enough to handle whatever people send to
642 * hw_host_to_card */
643 BUILD_BUG_ON(IF_SPI_CMD_BUF_SIZE < LBS_CMD_BUFFER_SIZE);
644 BUILD_BUG_ON(IF_SPI_CMD_BUF_SIZE < LBS_UPLD_SIZE);
645
646 /* It's just annoying if the buffer size isn't a multiple of 4, because
647 * then we might have len < IF_SPI_CMD_BUF_SIZE but
648 * ALIGN(len, 4) > IF_SPI_CMD_BUF_SIZE */
649 BUILD_BUG_ON(IF_SPI_CMD_BUF_SIZE % 4 != 0);
650
651 lbs_deb_enter(LBS_DEB_SPI);
652
653 /* How many bytes are there to read? */
654 err = spu_read_u16(card, IF_SPI_SCRATCH_2_REG, &len);
655 if (err)
656 goto out;
657 if (!len) {
658 lbs_pr_err("%s: error: card has no data for host\n",
659 __func__);
660 err = -EINVAL;
661 goto out;
662 } else if (len > IF_SPI_CMD_BUF_SIZE) {
663 lbs_pr_err("%s: error: response packet too large: "
664 "%d bytes, but maximum is %d\n",
665 __func__, len, IF_SPI_CMD_BUF_SIZE);
666 err = -EINVAL;
667 goto out;
668 }
669
670 /* Read the data from the WLAN module into our command buffer */
671 err = spu_read(card, IF_SPI_CMD_RDWRPORT_REG,
672 card->cmd_buffer, ALIGN(len, 4));
673 if (err)
674 goto out;
675
676 spin_lock_irqsave(&priv->driver_lock, flags);
677 i = (priv->resp_idx == 0) ? 1 : 0;
678 BUG_ON(priv->resp_len[i]);
679 priv->resp_len[i] = len;
680 memcpy(priv->resp_buf[i], card->cmd_buffer, len);
681 lbs_notify_command_response(priv, i);
682 spin_unlock_irqrestore(&priv->driver_lock, flags);
683
684out:
685 if (err)
686 lbs_pr_err("%s: err=%d\n", __func__, err);
687 lbs_deb_leave(LBS_DEB_SPI);
688 return err;
689}
690
691/* Move data from the card to the host */
692static int if_spi_c2h_data(struct if_spi_card *card)
693{
694 struct sk_buff *skb;
695 char *data;
696 u16 len;
697 int err = 0;
698
699 lbs_deb_enter(LBS_DEB_SPI);
700
701 /* How many bytes are there to read? */
702 err = spu_read_u16(card, IF_SPI_SCRATCH_1_REG, &len);
703 if (err)
704 goto out;
705 if (!len) {
706 lbs_pr_err("%s: error: card has no data for host\n",
707 __func__);
708 err = -EINVAL;
709 goto out;
710 } else if (len > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
711 lbs_pr_err("%s: error: card has %d bytes of data, but "
Andrey Yurovskyefcfd1f2009-06-18 09:51:57 -0700712 "our maximum skb size is %zu\n",
Colin McCabed2b21f12009-01-09 14:58:09 -0800713 __func__, len, MRVDRV_ETH_RX_PACKET_BUFFER_SIZE);
714 err = -EINVAL;
715 goto out;
716 }
717
718 /* TODO: should we allocate a smaller skb if we have less data? */
719 skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE);
720 if (!skb) {
721 err = -ENOBUFS;
722 goto out;
723 }
724 skb_reserve(skb, IPFIELD_ALIGN_OFFSET);
725 data = skb_put(skb, len);
726
727 /* Read the data from the WLAN module into our skb... */
728 err = spu_read(card, IF_SPI_DATA_RDWRPORT_REG, data, ALIGN(len, 4));
729 if (err)
730 goto free_skb;
731
732 /* pass the SKB to libertas */
733 err = lbs_process_rxed_packet(card->priv, skb);
734 if (err)
735 goto free_skb;
736
737 /* success */
738 goto out;
739
740free_skb:
741 dev_kfree_skb(skb);
742out:
743 if (err)
744 lbs_pr_err("%s: err=%d\n", __func__, err);
745 lbs_deb_leave(LBS_DEB_SPI);
746 return err;
747}
748
Colin McCabed2b21f12009-01-09 14:58:09 -0800749/* Inform the host about a card event */
750static void if_spi_e2h(struct if_spi_card *card)
751{
752 int err = 0;
Colin McCabed2b21f12009-01-09 14:58:09 -0800753 u32 cause;
754 struct lbs_private *priv = card->priv;
755
756 err = spu_read_u32(card, IF_SPI_SCRATCH_3_REG, &cause);
757 if (err)
758 goto out;
759
andrey@cozybit.comea2d0632009-05-19 17:20:13 -0700760 /* re-enable the card event interrupt */
761 spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG,
762 ~IF_SPI_HICU_CARD_EVENT);
763
764 /* generate a card interrupt */
765 spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG, IF_SPI_CIC_HOST_EVENT);
766
Colin McCabed2b21f12009-01-09 14:58:09 -0800767 lbs_queue_event(priv, cause & 0xff);
Colin McCabed2b21f12009-01-09 14:58:09 -0800768out:
769 if (err)
770 lbs_pr_err("%s: error %d\n", __func__, err);
771}
772
773static int lbs_spi_thread(void *data)
774{
775 int err;
776 struct if_spi_card *card = data;
777 u16 hiStatus;
Colin McCabed2b21f12009-01-09 14:58:09 -0800778
779 while (1) {
780 /* Wait to be woken up by one of two things. First, our ISR
781 * could tell us that something happened on the WLAN.
782 * Secondly, libertas could call hw_host_to_card with more
783 * data, which we might be able to send.
784 */
785 do {
786 err = down_interruptible(&card->spi_ready);
787 if (!card->run_thread) {
788 up(&card->spi_thread_terminated);
789 do_exit(0);
790 }
791 } while (err == EINTR);
792
793 /* Read the host interrupt status register to see what we
794 * can do. */
795 err = spu_read_u16(card, IF_SPI_HOST_INT_STATUS_REG,
796 &hiStatus);
797 if (err) {
798 lbs_pr_err("I/O error\n");
799 goto err;
800 }
801
802 if (hiStatus & IF_SPI_HIST_CMD_UPLOAD_RDY)
803 err = if_spi_c2h_cmd(card);
804 if (err)
805 goto err;
806 if (hiStatus & IF_SPI_HIST_RX_UPLOAD_RDY)
807 err = if_spi_c2h_data(card);
808 if (err)
809 goto err;
Andrey Yurovskyb3781c72009-06-12 12:45:36 -0700810
811 /* workaround: in PS mode, the card does not set the Command
812 * Download Ready bit, but it sets TX Download Ready. */
813 if (hiStatus & IF_SPI_HIST_CMD_DOWNLOAD_RDY ||
814 (card->priv->psstate != PS_STATE_FULL_POWER &&
815 (hiStatus & IF_SPI_HIST_TX_DOWNLOAD_RDY))) {
Colin McCabed2b21f12009-01-09 14:58:09 -0800816 lbs_host_to_card_done(card->priv);
Colin McCabed2b21f12009-01-09 14:58:09 -0800817 }
Colin McCabed2b21f12009-01-09 14:58:09 -0800818
Colin McCabed2b21f12009-01-09 14:58:09 -0800819 if (hiStatus & IF_SPI_HIST_CARD_EVENT)
820 if_spi_e2h(card);
821
822err:
823 if (err)
824 lbs_pr_err("%s: got error %d\n", __func__, err);
825 }
826}
827
828/* Block until lbs_spi_thread thread has terminated */
829static void if_spi_terminate_spi_thread(struct if_spi_card *card)
830{
831 /* It would be nice to use kthread_stop here, but that function
832 * can't wake threads waiting for a semaphore. */
833 card->run_thread = 0;
834 up(&card->spi_ready);
835 down(&card->spi_thread_terminated);
836}
837
838/*
839 * Host to Card
840 *
841 * Called from Libertas to transfer some data to the WLAN device
842 * We can't sleep here. */
843static int if_spi_host_to_card(struct lbs_private *priv,
844 u8 type, u8 *buf, u16 nb)
845{
846 int err = 0;
Colin McCabed2b21f12009-01-09 14:58:09 -0800847 struct if_spi_card *card = priv->card;
Colin McCabed2b21f12009-01-09 14:58:09 -0800848
849 lbs_deb_enter_args(LBS_DEB_SPI, "type %d, bytes %d", type, nb);
850
Andrey Yurovsky86c34fe2009-10-27 16:51:40 -0700851 nb = ALIGN(nb, 4);
Colin McCabed2b21f12009-01-09 14:58:09 -0800852
853 switch (type) {
854 case MVMS_CMD:
Andrey Yurovsky86c34fe2009-10-27 16:51:40 -0700855 err = spu_write(card, IF_SPI_CMD_RDWRPORT_REG, buf, nb);
Colin McCabed2b21f12009-01-09 14:58:09 -0800856 break;
857 case MVMS_DAT:
Andrey Yurovsky86c34fe2009-10-27 16:51:40 -0700858 err = spu_write(card, IF_SPI_DATA_RDWRPORT_REG, buf, nb);
Colin McCabed2b21f12009-01-09 14:58:09 -0800859 break;
860 default:
861 lbs_pr_err("can't transfer buffer of type %d", type);
862 err = -EINVAL;
863 break;
864 }
865
Colin McCabed2b21f12009-01-09 14:58:09 -0800866 lbs_deb_leave_args(LBS_DEB_SPI, "err=%d", err);
867 return err;
868}
869
870/*
871 * Host Interrupts
872 *
873 * Service incoming interrupts from the WLAN device. We can't sleep here, so
874 * don't try to talk on the SPI bus, just wake up the SPI thread.
875 */
876static irqreturn_t if_spi_host_interrupt(int irq, void *dev_id)
877{
878 struct if_spi_card *card = dev_id;
879
880 up(&card->spi_ready);
881 return IRQ_HANDLED;
882}
883
884/*
885 * SPI callbacks
886 */
887
888static int if_spi_calculate_fw_names(u16 card_id,
889 char *helper_fw, char *main_fw)
890{
891 int i;
892 for (i = 0; i < ARRAY_SIZE(chip_id_to_device_name); ++i) {
893 if (card_id == chip_id_to_device_name[i].chip_id)
894 break;
895 }
896 if (i == ARRAY_SIZE(chip_id_to_device_name)) {
897 lbs_pr_err("Unsupported chip_id: 0x%02x\n", card_id);
898 return -EAFNOSUPPORT;
899 }
John W. Linvillecadeba32009-05-27 00:49:36 +0200900 snprintf(helper_fw, IF_SPI_FW_NAME_MAX, "libertas/gspi%d_hlp.bin",
Colin McCabed2b21f12009-01-09 14:58:09 -0800901 chip_id_to_device_name[i].name);
John W. Linvillecadeba32009-05-27 00:49:36 +0200902 snprintf(main_fw, IF_SPI_FW_NAME_MAX, "libertas/gspi%d.bin",
Colin McCabed2b21f12009-01-09 14:58:09 -0800903 chip_id_to_device_name[i].name);
904 return 0;
905}
Ben Hutchingsa974a4b2009-11-07 22:00:03 +0000906MODULE_FIRMWARE("libertas/gspi8385_hlp.bin");
907MODULE_FIRMWARE("libertas/gspi8385.bin");
908MODULE_FIRMWARE("libertas/gspi8686_hlp.bin");
909MODULE_FIRMWARE("libertas/gspi8686.bin");
Colin McCabed2b21f12009-01-09 14:58:09 -0800910
911static int __devinit if_spi_probe(struct spi_device *spi)
912{
913 struct if_spi_card *card;
914 struct lbs_private *priv = NULL;
915 struct libertas_spi_platform_data *pdata = spi->dev.platform_data;
916 int err = 0;
917 u32 scratch;
Anna Nealb26ed972009-04-02 14:44:09 -0700918 struct sched_param param = { .sched_priority = 1 };
Colin McCabed2b21f12009-01-09 14:58:09 -0800919
920 lbs_deb_enter(LBS_DEB_SPI);
921
Mike Rapoport0c2bec92009-02-03 09:04:20 +0200922 if (!pdata) {
923 err = -EINVAL;
924 goto out;
925 }
926
927 if (pdata->setup) {
928 err = pdata->setup(spi);
929 if (err)
930 goto out;
931 }
932
Colin McCabed2b21f12009-01-09 14:58:09 -0800933 /* Allocate card structure to represent this specific device */
934 card = kzalloc(sizeof(struct if_spi_card), GFP_KERNEL);
935 if (!card) {
936 err = -ENOMEM;
937 goto out;
938 }
939 spi_set_drvdata(spi, card);
Mike Rapoport0c2bec92009-02-03 09:04:20 +0200940 card->pdata = pdata;
Colin McCabed2b21f12009-01-09 14:58:09 -0800941 card->spi = spi;
Colin McCabed2b21f12009-01-09 14:58:09 -0800942 card->prev_xfer_time = jiffies;
943
944 sema_init(&card->spi_ready, 0);
945 sema_init(&card->spi_thread_terminated, 0);
Colin McCabed2b21f12009-01-09 14:58:09 -0800946
Colin McCabed2b21f12009-01-09 14:58:09 -0800947 /* Initialize the SPI Interface Unit */
948 err = spu_init(card, pdata->use_dummy_writes);
949 if (err)
Sebastian Andrzej Siewior4d1d4982009-06-04 21:57:03 +0200950 goto free_card;
Colin McCabed2b21f12009-01-09 14:58:09 -0800951 err = spu_get_chip_revision(card, &card->card_id, &card->card_rev);
952 if (err)
Sebastian Andrzej Siewior4d1d4982009-06-04 21:57:03 +0200953 goto free_card;
Colin McCabed2b21f12009-01-09 14:58:09 -0800954
955 /* Firmware load */
956 err = spu_read_u32(card, IF_SPI_SCRATCH_4_REG, &scratch);
957 if (err)
Sebastian Andrzej Siewior4d1d4982009-06-04 21:57:03 +0200958 goto free_card;
Colin McCabed2b21f12009-01-09 14:58:09 -0800959 if (scratch == SUCCESSFUL_FW_DOWNLOAD_MAGIC)
960 lbs_deb_spi("Firmware is already loaded for "
961 "Marvell WLAN 802.11 adapter\n");
962 else {
963 err = if_spi_calculate_fw_names(card->card_id,
964 card->helper_fw_name, card->main_fw_name);
965 if (err)
Sebastian Andrzej Siewior4d1d4982009-06-04 21:57:03 +0200966 goto free_card;
Colin McCabed2b21f12009-01-09 14:58:09 -0800967
968 lbs_deb_spi("Initializing FW for Marvell WLAN 802.11 adapter "
969 "(chip_id = 0x%04x, chip_rev = 0x%02x) "
970 "attached to SPI bus_num %d, chip_select %d. "
971 "spi->max_speed_hz=%d\n",
972 card->card_id, card->card_rev,
973 spi->master->bus_num, spi->chip_select,
974 spi->max_speed_hz);
975 err = if_spi_prog_helper_firmware(card);
976 if (err)
Sebastian Andrzej Siewior4d1d4982009-06-04 21:57:03 +0200977 goto free_card;
Colin McCabed2b21f12009-01-09 14:58:09 -0800978 err = if_spi_prog_main_firmware(card);
979 if (err)
Sebastian Andrzej Siewior4d1d4982009-06-04 21:57:03 +0200980 goto free_card;
Colin McCabed2b21f12009-01-09 14:58:09 -0800981 lbs_deb_spi("loaded FW for Marvell WLAN 802.11 adapter\n");
982 }
983
984 err = spu_set_interrupt_mode(card, 0, 1);
985 if (err)
Sebastian Andrzej Siewior4d1d4982009-06-04 21:57:03 +0200986 goto free_card;
Colin McCabed2b21f12009-01-09 14:58:09 -0800987
988 /* Register our card with libertas.
989 * This will call alloc_etherdev */
990 priv = lbs_add_card(card, &spi->dev);
991 if (!priv) {
992 err = -ENOMEM;
Sebastian Andrzej Siewior4d1d4982009-06-04 21:57:03 +0200993 goto free_card;
Colin McCabed2b21f12009-01-09 14:58:09 -0800994 }
995 card->priv = priv;
996 priv->card = card;
997 priv->hw_host_to_card = if_spi_host_to_card;
Amitkumar Karwar49125452009-09-30 20:04:38 -0700998 priv->enter_deep_sleep = NULL;
999 priv->exit_deep_sleep = NULL;
1000 priv->reset_deep_sleep_wakeup = NULL;
Colin McCabed2b21f12009-01-09 14:58:09 -08001001 priv->fw_ready = 1;
Colin McCabed2b21f12009-01-09 14:58:09 -08001002
1003 /* Initialize interrupt handling stuff. */
1004 card->run_thread = 1;
1005 card->spi_thread = kthread_run(lbs_spi_thread, card, "lbs_spi_thread");
1006 if (IS_ERR(card->spi_thread)) {
1007 card->run_thread = 0;
1008 err = PTR_ERR(card->spi_thread);
1009 lbs_pr_err("error creating SPI thread: err=%d\n", err);
1010 goto remove_card;
1011 }
Anna Nealb26ed972009-04-02 14:44:09 -07001012 if (sched_setscheduler(card->spi_thread, SCHED_FIFO, &param))
1013 lbs_pr_err("Error setting scheduler, using default.\n");
1014
Colin McCabed2b21f12009-01-09 14:58:09 -08001015 err = request_irq(spi->irq, if_spi_host_interrupt,
1016 IRQF_TRIGGER_FALLING, "libertas_spi", card);
1017 if (err) {
1018 lbs_pr_err("can't get host irq line-- request_irq failed\n");
1019 goto terminate_thread;
1020 }
1021
Andrey Yurovsky86c34fe2009-10-27 16:51:40 -07001022 /* poke the IRQ handler so that we don't miss the first interrupt */
1023 up(&card->spi_ready);
1024
Colin McCabed2b21f12009-01-09 14:58:09 -08001025 /* Start the card.
1026 * This will call register_netdev, and we'll start
1027 * getting interrupts... */
1028 err = lbs_start_card(priv);
1029 if (err)
1030 goto release_irq;
1031
1032 lbs_deb_spi("Finished initializing WLAN module.\n");
1033
1034 /* successful exit */
1035 goto out;
1036
1037release_irq:
1038 free_irq(spi->irq, card);
1039terminate_thread:
1040 if_spi_terminate_spi_thread(card);
1041remove_card:
1042 lbs_remove_card(priv); /* will call free_netdev */
Colin McCabed2b21f12009-01-09 14:58:09 -08001043free_card:
1044 free_if_spi_card(card);
1045out:
1046 lbs_deb_leave_args(LBS_DEB_SPI, "err %d\n", err);
1047 return err;
1048}
1049
1050static int __devexit libertas_spi_remove(struct spi_device *spi)
1051{
1052 struct if_spi_card *card = spi_get_drvdata(spi);
1053 struct lbs_private *priv = card->priv;
1054
1055 lbs_deb_spi("libertas_spi_remove\n");
1056 lbs_deb_enter(LBS_DEB_SPI);
Colin McCabed2b21f12009-01-09 14:58:09 -08001057
1058 lbs_stop_card(priv);
Andrey Yurovskyefcfd1f2009-06-18 09:51:57 -07001059 lbs_remove_card(priv); /* will call free_netdev */
1060
1061 priv->surpriseremoved = 1;
Colin McCabed2b21f12009-01-09 14:58:09 -08001062 free_irq(spi->irq, card);
1063 if_spi_terminate_spi_thread(card);
Mike Rapoport0c2bec92009-02-03 09:04:20 +02001064 if (card->pdata->teardown)
1065 card->pdata->teardown(spi);
Colin McCabed2b21f12009-01-09 14:58:09 -08001066 free_if_spi_card(card);
1067 lbs_deb_leave(LBS_DEB_SPI);
1068 return 0;
1069}
1070
1071static struct spi_driver libertas_spi_driver = {
1072 .probe = if_spi_probe,
1073 .remove = __devexit_p(libertas_spi_remove),
1074 .driver = {
1075 .name = "libertas_spi",
1076 .bus = &spi_bus_type,
1077 .owner = THIS_MODULE,
1078 },
1079};
1080
1081/*
1082 * Module functions
1083 */
1084
1085static int __init if_spi_init_module(void)
1086{
1087 int ret = 0;
1088 lbs_deb_enter(LBS_DEB_SPI);
1089 printk(KERN_INFO "libertas_spi: Libertas SPI driver\n");
1090 ret = spi_register_driver(&libertas_spi_driver);
1091 lbs_deb_leave(LBS_DEB_SPI);
1092 return ret;
1093}
1094
1095static void __exit if_spi_exit_module(void)
1096{
1097 lbs_deb_enter(LBS_DEB_SPI);
1098 spi_unregister_driver(&libertas_spi_driver);
1099 lbs_deb_leave(LBS_DEB_SPI);
1100}
1101
1102module_init(if_spi_init_module);
1103module_exit(if_spi_exit_module);
1104
1105MODULE_DESCRIPTION("Libertas SPI WLAN Driver");
1106MODULE_AUTHOR("Andrey Yurovsky <andrey@cozybit.com>, "
1107 "Colin McCabe <colin@cozybit.com>");
1108MODULE_LICENSE("GPL");
Anton Vorontsove0626e32009-09-22 16:46:08 -07001109MODULE_ALIAS("spi:libertas_spi");