blob: 4d19b5726c15eddcb3d9da49652cafbf0d3072fa [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
Joe Perches0e4e06a2011-05-02 16:49:14 -070020#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21
Colin McCabed2b21f12009-01-09 14:58:09 -080022#include <linux/moduleparam.h>
23#include <linux/firmware.h>
Colin McCabed2b21f12009-01-09 14:58:09 -080024#include <linux/jiffies.h>
Colin McCabed2b21f12009-01-09 14:58:09 -080025#include <linux/list.h>
26#include <linux/netdevice.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Colin McCabed2b21f12009-01-09 14:58:09 -080028#include <linux/spi/libertas_spi.h>
29#include <linux/spi/spi.h>
30
31#include "host.h"
32#include "decl.h"
33#include "defs.h"
34#include "dev.h"
35#include "if_spi.h"
36
Vasily Khoruzhick16f775b2011-01-21 22:44:48 +020037struct if_spi_packet {
38 struct list_head list;
39 u16 blen;
40 u8 buffer[0] __attribute__((aligned(4)));
41};
42
Colin McCabed2b21f12009-01-09 14:58:09 -080043struct if_spi_card {
44 struct spi_device *spi;
45 struct lbs_private *priv;
Mike Rapoport0c2bec92009-02-03 09:04:20 +020046 struct libertas_spi_platform_data *pdata;
Colin McCabed2b21f12009-01-09 14:58:09 -080047
Colin McCabed2b21f12009-01-09 14:58:09 -080048 /* The card ID and card revision, as reported by the hardware. */
49 u16 card_id;
50 u8 card_rev;
51
Colin McCabed2b21f12009-01-09 14:58:09 -080052 /* The last time that we initiated an SPU operation */
53 unsigned long prev_xfer_time;
54
55 int use_dummy_writes;
56 unsigned long spu_port_delay;
57 unsigned long spu_reg_delay;
58
59 /* Handles all SPI communication (except for FW load) */
Vasily Khoruzhick16f775b2011-01-21 22:44:48 +020060 struct workqueue_struct *workqueue;
61 struct work_struct packet_work;
Vasily Khoruzhickf39de992011-03-16 16:41:46 +020062 struct work_struct resume_work;
Colin McCabed2b21f12009-01-09 14:58:09 -080063
64 u8 cmd_buffer[IF_SPI_CMD_BUF_SIZE];
Vasily Khoruzhick16f775b2011-01-21 22:44:48 +020065
66 /* A buffer of incoming packets from libertas core.
67 * Since we can't sleep in hw_host_to_card, we have to buffer
68 * them. */
69 struct list_head cmd_packet_list;
70 struct list_head data_packet_list;
71
72 /* Protects cmd_packet_list and data_packet_list */
73 spinlock_t buffer_lock;
Vasily Khoruzhickf39de992011-03-16 16:41:46 +020074
75 /* True is card suspended */
76 u8 suspended;
Colin McCabed2b21f12009-01-09 14:58:09 -080077};
78
79static void free_if_spi_card(struct if_spi_card *card)
80{
Vasily Khoruzhick16f775b2011-01-21 22:44:48 +020081 struct list_head *cursor, *next;
82 struct if_spi_packet *packet;
83
84 list_for_each_safe(cursor, next, &card->cmd_packet_list) {
85 packet = container_of(cursor, struct if_spi_packet, list);
86 list_del(&packet->list);
87 kfree(packet);
88 }
89 list_for_each_safe(cursor, next, &card->data_packet_list) {
90 packet = container_of(cursor, struct if_spi_packet, list);
91 list_del(&packet->list);
92 kfree(packet);
93 }
Colin McCabed2b21f12009-01-09 14:58:09 -080094 spi_set_drvdata(card->spi, NULL);
95 kfree(card);
96}
97
Dan Williams915a8242010-08-07 21:16:30 -050098#define MODEL_8385 0x04
99#define MODEL_8686 0x0b
100#define MODEL_8688 0x10
101
102static const struct lbs_fw_table fw_table[] = {
103 { MODEL_8385, "libertas/gspi8385_helper.bin", "libertas/gspi8385.bin" },
104 { MODEL_8385, "libertas/gspi8385_hlp.bin", "libertas/gspi8385.bin" },
105 { MODEL_8686, "libertas/gspi8686_v9_helper.bin", "libertas/gspi8686_v9.bin" },
106 { MODEL_8686, "libertas/gspi8686_hlp.bin", "libertas/gspi8686.bin" },
107 { MODEL_8688, "libertas/gspi8688_helper.bin", "libertas/gspi8688.bin" },
108 { 0, NULL, NULL }
Colin McCabed2b21f12009-01-09 14:58:09 -0800109};
Dan Williams915a8242010-08-07 21:16:30 -0500110MODULE_FIRMWARE("libertas/gspi8385_helper.bin");
111MODULE_FIRMWARE("libertas/gspi8385_hlp.bin");
112MODULE_FIRMWARE("libertas/gspi8385.bin");
113MODULE_FIRMWARE("libertas/gspi8686_v9_helper.bin");
114MODULE_FIRMWARE("libertas/gspi8686_v9.bin");
115MODULE_FIRMWARE("libertas/gspi8686_hlp.bin");
116MODULE_FIRMWARE("libertas/gspi8686.bin");
117MODULE_FIRMWARE("libertas/gspi8688_helper.bin");
118MODULE_FIRMWARE("libertas/gspi8688.bin");
119
Colin McCabed2b21f12009-01-09 14:58:09 -0800120
121/*
122 * SPI Interface Unit Routines
123 *
124 * The SPU sits between the host and the WLAN module.
125 * All communication with the firmware is through SPU transactions.
126 *
127 * First we have to put a SPU register name on the bus. Then we can
128 * either read from or write to that register.
129 *
Colin McCabed2b21f12009-01-09 14:58:09 -0800130 */
131
132static void spu_transaction_init(struct if_spi_card *card)
133{
134 if (!time_after(jiffies, card->prev_xfer_time + 1)) {
135 /* Unfortunately, the SPU requires a delay between successive
136 * transactions. If our last transaction was more than a jiffy
137 * ago, we have obviously already delayed enough.
138 * If not, we have to busy-wait to be on the safe side. */
139 ndelay(400);
140 }
Colin McCabed2b21f12009-01-09 14:58:09 -0800141}
142
143static void spu_transaction_finish(struct if_spi_card *card)
144{
Colin McCabed2b21f12009-01-09 14:58:09 -0800145 card->prev_xfer_time = jiffies;
146}
147
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700148/*
149 * Write out a byte buffer to an SPI register,
150 * using a series of 16-bit transfers.
151 */
Colin McCabed2b21f12009-01-09 14:58:09 -0800152static int spu_write(struct if_spi_card *card, u16 reg, const u8 *buf, int len)
153{
154 int err = 0;
Holger Schurigd18ba452009-10-22 15:30:44 +0200155 __le16 reg_out = cpu_to_le16(reg | IF_SPI_WRITE_OPERATION_MASK);
Sebastian Andrzej Siewior4d1d4982009-06-04 21:57:03 +0200156 struct spi_message m;
157 struct spi_transfer reg_trans;
158 struct spi_transfer data_trans;
159
160 spi_message_init(&m);
161 memset(&reg_trans, 0, sizeof(reg_trans));
162 memset(&data_trans, 0, sizeof(data_trans));
Colin McCabed2b21f12009-01-09 14:58:09 -0800163
164 /* You must give an even number of bytes to the SPU, even if it
165 * doesn't care about the last one. */
166 BUG_ON(len & 0x1);
167
168 spu_transaction_init(card);
169
170 /* write SPU register index */
Sebastian Andrzej Siewior4d1d4982009-06-04 21:57:03 +0200171 reg_trans.tx_buf = &reg_out;
172 reg_trans.len = sizeof(reg_out);
Colin McCabed2b21f12009-01-09 14:58:09 -0800173
Sebastian Andrzej Siewior4d1d4982009-06-04 21:57:03 +0200174 data_trans.tx_buf = buf;
175 data_trans.len = len;
Colin McCabed2b21f12009-01-09 14:58:09 -0800176
Sebastian Andrzej Siewior4d1d4982009-06-04 21:57:03 +0200177 spi_message_add_tail(&reg_trans, &m);
178 spi_message_add_tail(&data_trans, &m);
179
180 err = spi_sync(card->spi, &m);
Colin McCabed2b21f12009-01-09 14:58:09 -0800181 spu_transaction_finish(card);
182 return err;
183}
184
185static inline int spu_write_u16(struct if_spi_card *card, u16 reg, u16 val)
186{
Holger Schurigd18ba452009-10-22 15:30:44 +0200187 __le16 buff;
Colin McCabed2b21f12009-01-09 14:58:09 -0800188
Sebastian Andrzej Siewiorf488b722009-05-22 21:19:40 +0200189 buff = cpu_to_le16(val);
190 return spu_write(card, reg, (u8 *)&buff, sizeof(u16));
Colin McCabed2b21f12009-01-09 14:58:09 -0800191}
192
193static inline int spu_reg_is_port_reg(u16 reg)
194{
195 switch (reg) {
196 case IF_SPI_IO_RDWRPORT_REG:
197 case IF_SPI_CMD_RDWRPORT_REG:
198 case IF_SPI_DATA_RDWRPORT_REG:
199 return 1;
200 default:
201 return 0;
202 }
203}
204
205static int spu_read(struct if_spi_card *card, u16 reg, u8 *buf, int len)
206{
Sebastian Andrzej Siewior4d1d4982009-06-04 21:57:03 +0200207 unsigned int delay;
Colin McCabed2b21f12009-01-09 14:58:09 -0800208 int err = 0;
Holger Schurigd18ba452009-10-22 15:30:44 +0200209 __le16 reg_out = cpu_to_le16(reg | IF_SPI_READ_OPERATION_MASK);
Sebastian Andrzej Siewior4d1d4982009-06-04 21:57:03 +0200210 struct spi_message m;
211 struct spi_transfer reg_trans;
212 struct spi_transfer dummy_trans;
213 struct spi_transfer data_trans;
Colin McCabed2b21f12009-01-09 14:58:09 -0800214
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700215 /*
216 * You must take an even number of bytes from the SPU, even if you
217 * don't care about the last one.
218 */
Colin McCabed2b21f12009-01-09 14:58:09 -0800219 BUG_ON(len & 0x1);
220
221 spu_transaction_init(card);
222
Sebastian Andrzej Siewior4d1d4982009-06-04 21:57:03 +0200223 spi_message_init(&m);
224 memset(&reg_trans, 0, sizeof(reg_trans));
225 memset(&dummy_trans, 0, sizeof(dummy_trans));
226 memset(&data_trans, 0, sizeof(data_trans));
227
Colin McCabed2b21f12009-01-09 14:58:09 -0800228 /* write SPU register index */
Sebastian Andrzej Siewior4d1d4982009-06-04 21:57:03 +0200229 reg_trans.tx_buf = &reg_out;
230 reg_trans.len = sizeof(reg_out);
231 spi_message_add_tail(&reg_trans, &m);
Colin McCabed2b21f12009-01-09 14:58:09 -0800232
233 delay = spu_reg_is_port_reg(reg) ? card->spu_port_delay :
234 card->spu_reg_delay;
235 if (card->use_dummy_writes) {
236 /* Clock in dummy cycles while the SPU fills the FIFO */
Sebastian Andrzej Siewior4d1d4982009-06-04 21:57:03 +0200237 dummy_trans.len = delay / 8;
238 spi_message_add_tail(&dummy_trans, &m);
Colin McCabed2b21f12009-01-09 14:58:09 -0800239 } else {
240 /* Busy-wait while the SPU fills the FIFO */
Sebastian Andrzej Siewior4d1d4982009-06-04 21:57:03 +0200241 reg_trans.delay_usecs =
242 DIV_ROUND_UP((100 + (delay * 10)), 1000);
Colin McCabed2b21f12009-01-09 14:58:09 -0800243 }
244
245 /* read in data */
Sebastian Andrzej Siewior4d1d4982009-06-04 21:57:03 +0200246 data_trans.rx_buf = buf;
247 data_trans.len = len;
248 spi_message_add_tail(&data_trans, &m);
Colin McCabed2b21f12009-01-09 14:58:09 -0800249
Sebastian Andrzej Siewior4d1d4982009-06-04 21:57:03 +0200250 err = spi_sync(card->spi, &m);
Colin McCabed2b21f12009-01-09 14:58:09 -0800251 spu_transaction_finish(card);
252 return err;
253}
254
255/* Read 16 bits from an SPI register */
256static inline int spu_read_u16(struct if_spi_card *card, u16 reg, u16 *val)
257{
Holger Schurigd18ba452009-10-22 15:30:44 +0200258 __le16 buf;
Sebastian Andrzej Siewiorf488b722009-05-22 21:19:40 +0200259 int ret;
260
261 ret = spu_read(card, reg, (u8 *)&buf, sizeof(buf));
262 if (ret == 0)
263 *val = le16_to_cpup(&buf);
264 return ret;
Colin McCabed2b21f12009-01-09 14:58:09 -0800265}
266
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700267/*
268 * Read 32 bits from an SPI register.
269 * The low 16 bits are read first.
270 */
Colin McCabed2b21f12009-01-09 14:58:09 -0800271static int spu_read_u32(struct if_spi_card *card, u16 reg, u32 *val)
272{
Holger Schurigd18ba452009-10-22 15:30:44 +0200273 __le32 buf;
Colin McCabed2b21f12009-01-09 14:58:09 -0800274 int err;
Sebastian Andrzej Siewiorf488b722009-05-22 21:19:40 +0200275
276 err = spu_read(card, reg, (u8 *)&buf, sizeof(buf));
Colin McCabed2b21f12009-01-09 14:58:09 -0800277 if (!err)
Sebastian Andrzej Siewiorf488b722009-05-22 21:19:40 +0200278 *val = le32_to_cpup(&buf);
Colin McCabed2b21f12009-01-09 14:58:09 -0800279 return err;
280}
281
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700282/*
283 * Keep reading 16 bits from an SPI register until you get the correct result.
Colin McCabed2b21f12009-01-09 14:58:09 -0800284 *
285 * If mask = 0, the correct result is any non-zero number.
286 * If mask != 0, the correct result is any number where
287 * number & target_mask == target
288 *
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700289 * Returns -ETIMEDOUT if a second passes without the correct result.
290 */
Colin McCabed2b21f12009-01-09 14:58:09 -0800291static int spu_wait_for_u16(struct if_spi_card *card, u16 reg,
292 u16 target_mask, u16 target)
293{
294 int err;
295 unsigned long timeout = jiffies + 5*HZ;
296 while (1) {
297 u16 val;
298 err = spu_read_u16(card, reg, &val);
299 if (err)
300 return err;
301 if (target_mask) {
302 if ((val & target_mask) == target)
303 return 0;
304 } else {
305 if (val)
306 return 0;
307 }
308 udelay(100);
309 if (time_after(jiffies, timeout)) {
Joe Perches0e4e06a2011-05-02 16:49:14 -0700310 pr_err("%s: timeout with val=%02x, target_mask=%02x, target=%02x\n",
Colin McCabed2b21f12009-01-09 14:58:09 -0800311 __func__, val, target_mask, target);
312 return -ETIMEDOUT;
313 }
314 }
315}
316
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700317/*
318 * Read 16 bits from an SPI register until you receive a specific value.
319 * Returns -ETIMEDOUT if a 4 tries pass without success.
320 */
Colin McCabed2b21f12009-01-09 14:58:09 -0800321static int spu_wait_for_u32(struct if_spi_card *card, u32 reg, u32 target)
322{
323 int err, try;
324 for (try = 0; try < 4; ++try) {
325 u32 val = 0;
326 err = spu_read_u32(card, reg, &val);
327 if (err)
328 return err;
329 if (val == target)
330 return 0;
331 mdelay(100);
332 }
333 return -ETIMEDOUT;
334}
335
336static int spu_set_interrupt_mode(struct if_spi_card *card,
337 int suppress_host_int,
338 int auto_int)
339{
340 int err = 0;
341
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700342 /*
343 * We can suppress a host interrupt by clearing the appropriate
344 * bit in the "host interrupt status mask" register
345 */
Colin McCabed2b21f12009-01-09 14:58:09 -0800346 if (suppress_host_int) {
347 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG, 0);
348 if (err)
349 return err;
350 } else {
351 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG,
352 IF_SPI_HISM_TX_DOWNLOAD_RDY |
353 IF_SPI_HISM_RX_UPLOAD_RDY |
354 IF_SPI_HISM_CMD_DOWNLOAD_RDY |
355 IF_SPI_HISM_CARDEVENT |
356 IF_SPI_HISM_CMD_UPLOAD_RDY);
357 if (err)
358 return err;
359 }
360
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700361 /*
362 * If auto-interrupts are on, the completion of certain transactions
Colin McCabed2b21f12009-01-09 14:58:09 -0800363 * will trigger an interrupt automatically. If auto-interrupts
364 * are off, we need to set the "Card Interrupt Cause" register to
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700365 * trigger a card interrupt.
366 */
Colin McCabed2b21f12009-01-09 14:58:09 -0800367 if (auto_int) {
368 err = spu_write_u16(card, IF_SPI_HOST_INT_CTRL_REG,
369 IF_SPI_HICT_TX_DOWNLOAD_OVER_AUTO |
370 IF_SPI_HICT_RX_UPLOAD_OVER_AUTO |
371 IF_SPI_HICT_CMD_DOWNLOAD_OVER_AUTO |
372 IF_SPI_HICT_CMD_UPLOAD_OVER_AUTO);
373 if (err)
374 return err;
375 } else {
376 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG, 0);
377 if (err)
378 return err;
379 }
380 return err;
381}
382
383static int spu_get_chip_revision(struct if_spi_card *card,
384 u16 *card_id, u8 *card_rev)
385{
386 int err = 0;
387 u32 dev_ctrl;
388 err = spu_read_u32(card, IF_SPI_DEVICEID_CTRL_REG, &dev_ctrl);
389 if (err)
390 return err;
391 *card_id = IF_SPI_DEVICEID_CTRL_REG_TO_CARD_ID(dev_ctrl);
392 *card_rev = IF_SPI_DEVICEID_CTRL_REG_TO_CARD_REV(dev_ctrl);
393 return err;
394}
395
396static int spu_set_bus_mode(struct if_spi_card *card, u16 mode)
397{
398 int err = 0;
399 u16 rval;
400 /* set bus mode */
401 err = spu_write_u16(card, IF_SPI_SPU_BUS_MODE_REG, mode);
402 if (err)
403 return err;
404 /* Check that we were able to read back what we just wrote. */
405 err = spu_read_u16(card, IF_SPI_SPU_BUS_MODE_REG, &rval);
406 if (err)
407 return err;
Andrey Yurovsky88d89522009-07-31 11:35:19 -0700408 if ((rval & 0xF) != mode) {
Joe Perches0e4e06a2011-05-02 16:49:14 -0700409 pr_err("Can't read bus mode register\n");
Colin McCabed2b21f12009-01-09 14:58:09 -0800410 return -EIO;
411 }
412 return 0;
413}
414
415static int spu_init(struct if_spi_card *card, int use_dummy_writes)
416{
417 int err = 0;
418 u32 delay;
419
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700420 /*
421 * We have to start up in timed delay mode so that we can safely
422 * read the Delay Read Register.
423 */
Colin McCabed2b21f12009-01-09 14:58:09 -0800424 card->use_dummy_writes = 0;
425 err = spu_set_bus_mode(card,
426 IF_SPI_BUS_MODE_SPI_CLOCK_PHASE_RISING |
427 IF_SPI_BUS_MODE_DELAY_METHOD_TIMED |
428 IF_SPI_BUS_MODE_16_BIT_ADDRESS_16_BIT_DATA);
429 if (err)
430 return err;
431 card->spu_port_delay = 1000;
432 card->spu_reg_delay = 1000;
433 err = spu_read_u32(card, IF_SPI_DELAY_READ_REG, &delay);
434 if (err)
435 return err;
436 card->spu_port_delay = delay & 0x0000ffff;
437 card->spu_reg_delay = (delay & 0xffff0000) >> 16;
438
439 /* If dummy clock delay mode has been requested, switch to it now */
440 if (use_dummy_writes) {
441 card->use_dummy_writes = 1;
442 err = spu_set_bus_mode(card,
443 IF_SPI_BUS_MODE_SPI_CLOCK_PHASE_RISING |
444 IF_SPI_BUS_MODE_DELAY_METHOD_DUMMY_CLOCK |
445 IF_SPI_BUS_MODE_16_BIT_ADDRESS_16_BIT_DATA);
446 if (err)
447 return err;
448 }
449
450 lbs_deb_spi("Initialized SPU unit. "
451 "spu_port_delay=0x%04lx, spu_reg_delay=0x%04lx\n",
452 card->spu_port_delay, card->spu_reg_delay);
453 return err;
454}
455
456/*
457 * Firmware Loading
458 */
459
Dan Williams915a8242010-08-07 21:16:30 -0500460static int if_spi_prog_helper_firmware(struct if_spi_card *card,
461 const struct firmware *firmware)
Colin McCabed2b21f12009-01-09 14:58:09 -0800462{
463 int err = 0;
Colin McCabed2b21f12009-01-09 14:58:09 -0800464 int bytes_remaining;
465 const u8 *fw;
466 u8 temp[HELPER_FW_LOAD_CHUNK_SZ];
Colin McCabed2b21f12009-01-09 14:58:09 -0800467
468 lbs_deb_enter(LBS_DEB_SPI);
469
470 err = spu_set_interrupt_mode(card, 1, 0);
471 if (err)
472 goto out;
Dan Williams915a8242010-08-07 21:16:30 -0500473
Colin McCabed2b21f12009-01-09 14:58:09 -0800474 bytes_remaining = firmware->size;
475 fw = firmware->data;
476
477 /* Load helper firmware image */
478 while (bytes_remaining > 0) {
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700479 /*
480 * Scratch pad 1 should contain the number of bytes we
481 * want to download to the firmware
482 */
Colin McCabed2b21f12009-01-09 14:58:09 -0800483 err = spu_write_u16(card, IF_SPI_SCRATCH_1_REG,
484 HELPER_FW_LOAD_CHUNK_SZ);
485 if (err)
Dan Williams915a8242010-08-07 21:16:30 -0500486 goto out;
Colin McCabed2b21f12009-01-09 14:58:09 -0800487
488 err = spu_wait_for_u16(card, IF_SPI_HOST_INT_STATUS_REG,
489 IF_SPI_HIST_CMD_DOWNLOAD_RDY,
490 IF_SPI_HIST_CMD_DOWNLOAD_RDY);
491 if (err)
Dan Williams915a8242010-08-07 21:16:30 -0500492 goto out;
Colin McCabed2b21f12009-01-09 14:58:09 -0800493
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700494 /*
495 * Feed the data into the command read/write port reg
496 * in chunks of 64 bytes
497 */
Colin McCabed2b21f12009-01-09 14:58:09 -0800498 memset(temp, 0, sizeof(temp));
499 memcpy(temp, fw,
500 min(bytes_remaining, HELPER_FW_LOAD_CHUNK_SZ));
501 mdelay(10);
502 err = spu_write(card, IF_SPI_CMD_RDWRPORT_REG,
503 temp, HELPER_FW_LOAD_CHUNK_SZ);
504 if (err)
Dan Williams915a8242010-08-07 21:16:30 -0500505 goto out;
Colin McCabed2b21f12009-01-09 14:58:09 -0800506
507 /* Interrupt the boot code */
508 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
509 if (err)
Dan Williams915a8242010-08-07 21:16:30 -0500510 goto out;
Colin McCabed2b21f12009-01-09 14:58:09 -0800511 err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG,
512 IF_SPI_CIC_CMD_DOWNLOAD_OVER);
513 if (err)
Dan Williams915a8242010-08-07 21:16:30 -0500514 goto out;
Colin McCabed2b21f12009-01-09 14:58:09 -0800515 bytes_remaining -= HELPER_FW_LOAD_CHUNK_SZ;
516 fw += HELPER_FW_LOAD_CHUNK_SZ;
517 }
518
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700519 /*
520 * Once the helper / single stage firmware download is complete,
Colin McCabed2b21f12009-01-09 14:58:09 -0800521 * write 0 to scratch pad 1 and interrupt the
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700522 * bootloader. This completes the helper download.
523 */
Colin McCabed2b21f12009-01-09 14:58:09 -0800524 err = spu_write_u16(card, IF_SPI_SCRATCH_1_REG, FIRMWARE_DNLD_OK);
525 if (err)
Dan Williams915a8242010-08-07 21:16:30 -0500526 goto out;
Colin McCabed2b21f12009-01-09 14:58:09 -0800527 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
528 if (err)
Dan Williams915a8242010-08-07 21:16:30 -0500529 goto out;
Colin McCabed2b21f12009-01-09 14:58:09 -0800530 err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG,
531 IF_SPI_CIC_CMD_DOWNLOAD_OVER);
Dan Williams915a8242010-08-07 21:16:30 -0500532 goto out;
Colin McCabed2b21f12009-01-09 14:58:09 -0800533
534 lbs_deb_spi("waiting for helper to boot...\n");
535
Colin McCabed2b21f12009-01-09 14:58:09 -0800536out:
537 if (err)
Joe Perches0e4e06a2011-05-02 16:49:14 -0700538 pr_err("failed to load helper firmware (err=%d)\n", err);
Colin McCabed2b21f12009-01-09 14:58:09 -0800539 lbs_deb_leave_args(LBS_DEB_SPI, "err %d", err);
540 return err;
541}
542
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700543/*
544 * Returns the length of the next packet the firmware expects us to send.
545 * Sets crc_err if the previous transfer had a CRC error.
546 */
Colin McCabed2b21f12009-01-09 14:58:09 -0800547static int if_spi_prog_main_firmware_check_len(struct if_spi_card *card,
548 int *crc_err)
549{
550 u16 len;
551 int err = 0;
552
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700553 /*
554 * wait until the host interrupt status register indicates
555 * that we are ready to download
556 */
Colin McCabed2b21f12009-01-09 14:58:09 -0800557 err = spu_wait_for_u16(card, IF_SPI_HOST_INT_STATUS_REG,
558 IF_SPI_HIST_CMD_DOWNLOAD_RDY,
559 IF_SPI_HIST_CMD_DOWNLOAD_RDY);
560 if (err) {
Joe Perches0e4e06a2011-05-02 16:49:14 -0700561 pr_err("timed out waiting for host_int_status\n");
Colin McCabed2b21f12009-01-09 14:58:09 -0800562 return err;
563 }
564
565 /* Ask the device how many bytes of firmware it wants. */
566 err = spu_read_u16(card, IF_SPI_SCRATCH_1_REG, &len);
567 if (err)
568 return err;
569
570 if (len > IF_SPI_CMD_BUF_SIZE) {
Joe Perches0e4e06a2011-05-02 16:49:14 -0700571 pr_err("firmware load device requested a larger transfer than we are prepared to handle (len = %d)\n",
572 len);
Colin McCabed2b21f12009-01-09 14:58:09 -0800573 return -EIO;
574 }
575 if (len & 0x1) {
576 lbs_deb_spi("%s: crc error\n", __func__);
577 len &= ~0x1;
578 *crc_err = 1;
579 } else
580 *crc_err = 0;
581
582 return len;
583}
584
Dan Williams915a8242010-08-07 21:16:30 -0500585static int if_spi_prog_main_firmware(struct if_spi_card *card,
586 const struct firmware *firmware)
Colin McCabed2b21f12009-01-09 14:58:09 -0800587{
588 int len, prev_len;
589 int bytes, crc_err = 0, err = 0;
Colin McCabed2b21f12009-01-09 14:58:09 -0800590 const u8 *fw;
Colin McCabed2b21f12009-01-09 14:58:09 -0800591 u16 num_crc_errs;
592
593 lbs_deb_enter(LBS_DEB_SPI);
594
595 err = spu_set_interrupt_mode(card, 1, 0);
596 if (err)
597 goto out;
598
Colin McCabed2b21f12009-01-09 14:58:09 -0800599 err = spu_wait_for_u16(card, IF_SPI_SCRATCH_1_REG, 0, 0);
600 if (err) {
Joe Perches0e4e06a2011-05-02 16:49:14 -0700601 pr_err("%s: timed out waiting for initial scratch reg = 0\n",
602 __func__);
Dan Williams915a8242010-08-07 21:16:30 -0500603 goto out;
Colin McCabed2b21f12009-01-09 14:58:09 -0800604 }
605
606 num_crc_errs = 0;
607 prev_len = 0;
608 bytes = firmware->size;
609 fw = firmware->data;
610 while ((len = if_spi_prog_main_firmware_check_len(card, &crc_err))) {
611 if (len < 0) {
612 err = len;
Dan Williams915a8242010-08-07 21:16:30 -0500613 goto out;
Colin McCabed2b21f12009-01-09 14:58:09 -0800614 }
615 if (bytes < 0) {
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700616 /*
617 * If there are no more bytes left, we would normally
618 * expect to have terminated with len = 0
619 */
Joe Perches0e4e06a2011-05-02 16:49:14 -0700620 pr_err("Firmware load wants more bytes than we have to offer.\n");
Colin McCabed2b21f12009-01-09 14:58:09 -0800621 break;
622 }
623 if (crc_err) {
624 /* Previous transfer failed. */
625 if (++num_crc_errs > MAX_MAIN_FW_LOAD_CRC_ERR) {
Joe Perches0e4e06a2011-05-02 16:49:14 -0700626 pr_err("Too many CRC errors encountered in firmware load.\n");
Colin McCabed2b21f12009-01-09 14:58:09 -0800627 err = -EIO;
Dan Williams915a8242010-08-07 21:16:30 -0500628 goto out;
Colin McCabed2b21f12009-01-09 14:58:09 -0800629 }
630 } else {
631 /* Previous transfer succeeded. Advance counters. */
632 bytes -= prev_len;
633 fw += prev_len;
634 }
635 if (bytes < len) {
636 memset(card->cmd_buffer, 0, len);
637 memcpy(card->cmd_buffer, fw, bytes);
638 } else
639 memcpy(card->cmd_buffer, fw, len);
640
641 err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
642 if (err)
Dan Williams915a8242010-08-07 21:16:30 -0500643 goto out;
Colin McCabed2b21f12009-01-09 14:58:09 -0800644 err = spu_write(card, IF_SPI_CMD_RDWRPORT_REG,
645 card->cmd_buffer, len);
646 if (err)
Dan Williams915a8242010-08-07 21:16:30 -0500647 goto out;
Colin McCabed2b21f12009-01-09 14:58:09 -0800648 err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG ,
649 IF_SPI_CIC_CMD_DOWNLOAD_OVER);
650 if (err)
Dan Williams915a8242010-08-07 21:16:30 -0500651 goto out;
Colin McCabed2b21f12009-01-09 14:58:09 -0800652 prev_len = len;
653 }
654 if (bytes > prev_len) {
Joe Perches0e4e06a2011-05-02 16:49:14 -0700655 pr_err("firmware load wants fewer bytes than we have to offer\n");
Colin McCabed2b21f12009-01-09 14:58:09 -0800656 }
657
658 /* Confirm firmware download */
659 err = spu_wait_for_u32(card, IF_SPI_SCRATCH_4_REG,
660 SUCCESSFUL_FW_DOWNLOAD_MAGIC);
661 if (err) {
Joe Perches0e4e06a2011-05-02 16:49:14 -0700662 pr_err("failed to confirm the firmware download\n");
Dan Williams915a8242010-08-07 21:16:30 -0500663 goto out;
Colin McCabed2b21f12009-01-09 14:58:09 -0800664 }
665
Colin McCabed2b21f12009-01-09 14:58:09 -0800666out:
667 if (err)
Joe Perches0e4e06a2011-05-02 16:49:14 -0700668 pr_err("failed to load firmware (err=%d)\n", err);
Colin McCabed2b21f12009-01-09 14:58:09 -0800669 lbs_deb_leave_args(LBS_DEB_SPI, "err %d", err);
670 return err;
671}
672
673/*
674 * SPI Transfer Thread
675 *
Vasily Khoruzhick16f775b2011-01-21 22:44:48 +0200676 * The SPI worker handles all SPI transfers, so there is no need for a lock.
Colin McCabed2b21f12009-01-09 14:58:09 -0800677 */
678
679/* Move a command from the card to the host */
680static int if_spi_c2h_cmd(struct if_spi_card *card)
681{
682 struct lbs_private *priv = card->priv;
683 unsigned long flags;
684 int err = 0;
685 u16 len;
686 u8 i;
687
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700688 /*
689 * We need a buffer big enough to handle whatever people send to
690 * hw_host_to_card
691 */
Colin McCabed2b21f12009-01-09 14:58:09 -0800692 BUILD_BUG_ON(IF_SPI_CMD_BUF_SIZE < LBS_CMD_BUFFER_SIZE);
693 BUILD_BUG_ON(IF_SPI_CMD_BUF_SIZE < LBS_UPLD_SIZE);
694
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700695 /*
696 * It's just annoying if the buffer size isn't a multiple of 4, because
697 * then we might have len < IF_SPI_CMD_BUF_SIZE but
698 * ALIGN(len, 4) > IF_SPI_CMD_BUF_SIZE
699 */
Colin McCabed2b21f12009-01-09 14:58:09 -0800700 BUILD_BUG_ON(IF_SPI_CMD_BUF_SIZE % 4 != 0);
701
702 lbs_deb_enter(LBS_DEB_SPI);
703
704 /* How many bytes are there to read? */
705 err = spu_read_u16(card, IF_SPI_SCRATCH_2_REG, &len);
706 if (err)
707 goto out;
708 if (!len) {
Joe Perches0e4e06a2011-05-02 16:49:14 -0700709 pr_err("%s: error: card has no data for host\n", __func__);
Colin McCabed2b21f12009-01-09 14:58:09 -0800710 err = -EINVAL;
711 goto out;
712 } else if (len > IF_SPI_CMD_BUF_SIZE) {
Joe Perches0e4e06a2011-05-02 16:49:14 -0700713 pr_err("%s: error: response packet too large: %d bytes, but maximum is %d\n",
714 __func__, len, IF_SPI_CMD_BUF_SIZE);
Colin McCabed2b21f12009-01-09 14:58:09 -0800715 err = -EINVAL;
716 goto out;
717 }
718
719 /* Read the data from the WLAN module into our command buffer */
720 err = spu_read(card, IF_SPI_CMD_RDWRPORT_REG,
721 card->cmd_buffer, ALIGN(len, 4));
722 if (err)
723 goto out;
724
725 spin_lock_irqsave(&priv->driver_lock, flags);
726 i = (priv->resp_idx == 0) ? 1 : 0;
727 BUG_ON(priv->resp_len[i]);
728 priv->resp_len[i] = len;
729 memcpy(priv->resp_buf[i], card->cmd_buffer, len);
730 lbs_notify_command_response(priv, i);
731 spin_unlock_irqrestore(&priv->driver_lock, flags);
732
733out:
734 if (err)
Joe Perches0e4e06a2011-05-02 16:49:14 -0700735 pr_err("%s: err=%d\n", __func__, err);
Colin McCabed2b21f12009-01-09 14:58:09 -0800736 lbs_deb_leave(LBS_DEB_SPI);
737 return err;
738}
739
740/* Move data from the card to the host */
741static int if_spi_c2h_data(struct if_spi_card *card)
742{
743 struct sk_buff *skb;
744 char *data;
745 u16 len;
746 int err = 0;
747
748 lbs_deb_enter(LBS_DEB_SPI);
749
750 /* How many bytes are there to read? */
751 err = spu_read_u16(card, IF_SPI_SCRATCH_1_REG, &len);
752 if (err)
753 goto out;
754 if (!len) {
Joe Perches0e4e06a2011-05-02 16:49:14 -0700755 pr_err("%s: error: card has no data for host\n", __func__);
Colin McCabed2b21f12009-01-09 14:58:09 -0800756 err = -EINVAL;
757 goto out;
758 } else if (len > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
Joe Perches0e4e06a2011-05-02 16:49:14 -0700759 pr_err("%s: error: card has %d bytes of data, but our maximum skb size is %zu\n",
760 __func__, len, MRVDRV_ETH_RX_PACKET_BUFFER_SIZE);
Colin McCabed2b21f12009-01-09 14:58:09 -0800761 err = -EINVAL;
762 goto out;
763 }
764
765 /* TODO: should we allocate a smaller skb if we have less data? */
766 skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE);
767 if (!skb) {
768 err = -ENOBUFS;
769 goto out;
770 }
771 skb_reserve(skb, IPFIELD_ALIGN_OFFSET);
772 data = skb_put(skb, len);
773
774 /* Read the data from the WLAN module into our skb... */
775 err = spu_read(card, IF_SPI_DATA_RDWRPORT_REG, data, ALIGN(len, 4));
776 if (err)
777 goto free_skb;
778
779 /* pass the SKB to libertas */
780 err = lbs_process_rxed_packet(card->priv, skb);
781 if (err)
782 goto free_skb;
783
784 /* success */
785 goto out;
786
787free_skb:
788 dev_kfree_skb(skb);
789out:
790 if (err)
Joe Perches0e4e06a2011-05-02 16:49:14 -0700791 pr_err("%s: err=%d\n", __func__, err);
Colin McCabed2b21f12009-01-09 14:58:09 -0800792 lbs_deb_leave(LBS_DEB_SPI);
793 return err;
794}
795
Vasily Khoruzhick16f775b2011-01-21 22:44:48 +0200796/* Move data or a command from the host to the card. */
797static void if_spi_h2c(struct if_spi_card *card,
798 struct if_spi_packet *packet, int type)
799{
800 int err = 0;
801 u16 int_type, port_reg;
802
803 switch (type) {
804 case MVMS_DAT:
805 int_type = IF_SPI_CIC_TX_DOWNLOAD_OVER;
806 port_reg = IF_SPI_DATA_RDWRPORT_REG;
807 break;
808 case MVMS_CMD:
809 int_type = IF_SPI_CIC_CMD_DOWNLOAD_OVER;
810 port_reg = IF_SPI_CMD_RDWRPORT_REG;
811 break;
812 default:
Joe Perches0e4e06a2011-05-02 16:49:14 -0700813 pr_err("can't transfer buffer of type %d\n", type);
Vasily Khoruzhick16f775b2011-01-21 22:44:48 +0200814 err = -EINVAL;
815 goto out;
816 }
817
818 /* Write the data to the card */
819 err = spu_write(card, port_reg, packet->buffer, packet->blen);
820 if (err)
821 goto out;
822
823out:
824 kfree(packet);
825
826 if (err)
Joe Perches0e4e06a2011-05-02 16:49:14 -0700827 pr_err("%s: error %d\n", __func__, err);
Vasily Khoruzhick16f775b2011-01-21 22:44:48 +0200828}
829
Colin McCabed2b21f12009-01-09 14:58:09 -0800830/* Inform the host about a card event */
831static void if_spi_e2h(struct if_spi_card *card)
832{
833 int err = 0;
Colin McCabed2b21f12009-01-09 14:58:09 -0800834 u32 cause;
835 struct lbs_private *priv = card->priv;
836
837 err = spu_read_u32(card, IF_SPI_SCRATCH_3_REG, &cause);
838 if (err)
839 goto out;
840
andrey@cozybit.comea2d0632009-05-19 17:20:13 -0700841 /* re-enable the card event interrupt */
842 spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG,
843 ~IF_SPI_HICU_CARD_EVENT);
844
845 /* generate a card interrupt */
846 spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG, IF_SPI_CIC_HOST_EVENT);
847
Colin McCabed2b21f12009-01-09 14:58:09 -0800848 lbs_queue_event(priv, cause & 0xff);
Colin McCabed2b21f12009-01-09 14:58:09 -0800849out:
850 if (err)
Joe Perches0e4e06a2011-05-02 16:49:14 -0700851 pr_err("%s: error %d\n", __func__, err);
Colin McCabed2b21f12009-01-09 14:58:09 -0800852}
853
Vasily Khoruzhick16f775b2011-01-21 22:44:48 +0200854static void if_spi_host_to_card_worker(struct work_struct *work)
Colin McCabed2b21f12009-01-09 14:58:09 -0800855{
856 int err;
Vasily Khoruzhick16f775b2011-01-21 22:44:48 +0200857 struct if_spi_card *card;
Colin McCabed2b21f12009-01-09 14:58:09 -0800858 u16 hiStatus;
Vasily Khoruzhick16f775b2011-01-21 22:44:48 +0200859 unsigned long flags;
860 struct if_spi_packet *packet;
Colin McCabed2b21f12009-01-09 14:58:09 -0800861
Vasily Khoruzhick16f775b2011-01-21 22:44:48 +0200862 card = container_of(work, struct if_spi_card, packet_work);
Colin McCabed2b21f12009-01-09 14:58:09 -0800863
Vasily Khoruzhick16f775b2011-01-21 22:44:48 +0200864 lbs_deb_enter(LBS_DEB_SPI);
865
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700866 /*
867 * Read the host interrupt status register to see what we
868 * can do.
869 */
Vasily Khoruzhick16f775b2011-01-21 22:44:48 +0200870 err = spu_read_u16(card, IF_SPI_HOST_INT_STATUS_REG,
871 &hiStatus);
872 if (err) {
Joe Perches0e4e06a2011-05-02 16:49:14 -0700873 pr_err("I/O error\n");
Vasily Khoruzhick16f775b2011-01-21 22:44:48 +0200874 goto err;
875 }
876
877 if (hiStatus & IF_SPI_HIST_CMD_UPLOAD_RDY) {
878 err = if_spi_c2h_cmd(card);
879 if (err)
Colin McCabed2b21f12009-01-09 14:58:09 -0800880 goto err;
Vasily Khoruzhick16f775b2011-01-21 22:44:48 +0200881 }
882 if (hiStatus & IF_SPI_HIST_RX_UPLOAD_RDY) {
883 err = if_spi_c2h_data(card);
884 if (err)
885 goto err;
886 }
Colin McCabed2b21f12009-01-09 14:58:09 -0800887
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700888 /*
889 * workaround: in PS mode, the card does not set the Command
890 * Download Ready bit, but it sets TX Download Ready.
891 */
Vasily Khoruzhick16f775b2011-01-21 22:44:48 +0200892 if (hiStatus & IF_SPI_HIST_CMD_DOWNLOAD_RDY ||
893 (card->priv->psstate != PS_STATE_FULL_POWER &&
894 (hiStatus & IF_SPI_HIST_TX_DOWNLOAD_RDY))) {
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700895 /*
896 * This means two things. First of all,
Vasily Khoruzhick16f775b2011-01-21 22:44:48 +0200897 * if there was a previous command sent, the card has
898 * successfully received it.
899 * Secondly, it is now ready to download another
900 * command.
901 */
902 lbs_host_to_card_done(card->priv);
Andrey Yurovskyb3781c72009-06-12 12:45:36 -0700903
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700904 /* Do we have any command packets from the host to send? */
Vasily Khoruzhick16f775b2011-01-21 22:44:48 +0200905 packet = NULL;
906 spin_lock_irqsave(&card->buffer_lock, flags);
907 if (!list_empty(&card->cmd_packet_list)) {
908 packet = (struct if_spi_packet *)(card->
909 cmd_packet_list.next);
910 list_del(&packet->list);
Colin McCabed2b21f12009-01-09 14:58:09 -0800911 }
Vasily Khoruzhick16f775b2011-01-21 22:44:48 +0200912 spin_unlock_irqrestore(&card->buffer_lock, flags);
Colin McCabed2b21f12009-01-09 14:58:09 -0800913
Vasily Khoruzhick16f775b2011-01-21 22:44:48 +0200914 if (packet)
915 if_spi_h2c(card, packet, MVMS_CMD);
916 }
917 if (hiStatus & IF_SPI_HIST_TX_DOWNLOAD_RDY) {
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700918 /* Do we have any data packets from the host to send? */
Vasily Khoruzhick16f775b2011-01-21 22:44:48 +0200919 packet = NULL;
920 spin_lock_irqsave(&card->buffer_lock, flags);
921 if (!list_empty(&card->data_packet_list)) {
922 packet = (struct if_spi_packet *)(card->
923 data_packet_list.next);
924 list_del(&packet->list);
925 }
926 spin_unlock_irqrestore(&card->buffer_lock, flags);
927
928 if (packet)
929 if_spi_h2c(card, packet, MVMS_DAT);
930 }
931 if (hiStatus & IF_SPI_HIST_CARD_EVENT)
932 if_spi_e2h(card);
Colin McCabed2b21f12009-01-09 14:58:09 -0800933
934err:
Vasily Khoruzhick16f775b2011-01-21 22:44:48 +0200935 if (err)
Joe Perches0e4e06a2011-05-02 16:49:14 -0700936 pr_err("%s: got error %d\n", __func__, err);
Colin McCabed2b21f12009-01-09 14:58:09 -0800937
Vasily Khoruzhick16f775b2011-01-21 22:44:48 +0200938 lbs_deb_leave(LBS_DEB_SPI);
Colin McCabed2b21f12009-01-09 14:58:09 -0800939}
940
941/*
942 * Host to Card
943 *
944 * Called from Libertas to transfer some data to the WLAN device
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700945 * We can't sleep here.
946 */
Colin McCabed2b21f12009-01-09 14:58:09 -0800947static int if_spi_host_to_card(struct lbs_private *priv,
948 u8 type, u8 *buf, u16 nb)
949{
950 int err = 0;
Vasily Khoruzhick16f775b2011-01-21 22:44:48 +0200951 unsigned long flags;
Colin McCabed2b21f12009-01-09 14:58:09 -0800952 struct if_spi_card *card = priv->card;
Vasily Khoruzhick16f775b2011-01-21 22:44:48 +0200953 struct if_spi_packet *packet;
954 u16 blen;
Colin McCabed2b21f12009-01-09 14:58:09 -0800955
956 lbs_deb_enter_args(LBS_DEB_SPI, "type %d, bytes %d", type, nb);
957
Vasily Khoruzhick16f775b2011-01-21 22:44:48 +0200958 if (nb == 0) {
Joe Perches0e4e06a2011-05-02 16:49:14 -0700959 pr_err("%s: invalid size requested: %d\n", __func__, nb);
Vasily Khoruzhick16f775b2011-01-21 22:44:48 +0200960 err = -EINVAL;
961 goto out;
962 }
963 blen = ALIGN(nb, 4);
964 packet = kzalloc(sizeof(struct if_spi_packet) + blen, GFP_ATOMIC);
965 if (!packet) {
966 err = -ENOMEM;
967 goto out;
968 }
969 packet->blen = blen;
970 memcpy(packet->buffer, buf, nb);
971 memset(packet->buffer + nb, 0, blen - nb);
Colin McCabed2b21f12009-01-09 14:58:09 -0800972
973 switch (type) {
974 case MVMS_CMD:
Vasily Khoruzhick16f775b2011-01-21 22:44:48 +0200975 priv->dnld_sent = DNLD_CMD_SENT;
976 spin_lock_irqsave(&card->buffer_lock, flags);
977 list_add_tail(&packet->list, &card->cmd_packet_list);
978 spin_unlock_irqrestore(&card->buffer_lock, flags);
Colin McCabed2b21f12009-01-09 14:58:09 -0800979 break;
980 case MVMS_DAT:
Vasily Khoruzhick16f775b2011-01-21 22:44:48 +0200981 priv->dnld_sent = DNLD_DATA_SENT;
982 spin_lock_irqsave(&card->buffer_lock, flags);
983 list_add_tail(&packet->list, &card->data_packet_list);
984 spin_unlock_irqrestore(&card->buffer_lock, flags);
Colin McCabed2b21f12009-01-09 14:58:09 -0800985 break;
986 default:
Joe Perches0e4e06a2011-05-02 16:49:14 -0700987 pr_err("can't transfer buffer of type %d\n", type);
Colin McCabed2b21f12009-01-09 14:58:09 -0800988 err = -EINVAL;
989 break;
990 }
991
Vasily Khoruzhick16f775b2011-01-21 22:44:48 +0200992 /* Queue spi xfer work */
993 queue_work(card->workqueue, &card->packet_work);
994out:
Colin McCabed2b21f12009-01-09 14:58:09 -0800995 lbs_deb_leave_args(LBS_DEB_SPI, "err=%d", err);
996 return err;
997}
998
999/*
1000 * Host Interrupts
1001 *
1002 * Service incoming interrupts from the WLAN device. We can't sleep here, so
Vasily Khoruzhick16f775b2011-01-21 22:44:48 +02001003 * don't try to talk on the SPI bus, just queue the SPI xfer work.
Colin McCabed2b21f12009-01-09 14:58:09 -08001004 */
1005static irqreturn_t if_spi_host_interrupt(int irq, void *dev_id)
1006{
1007 struct if_spi_card *card = dev_id;
1008
Vasily Khoruzhick16f775b2011-01-21 22:44:48 +02001009 queue_work(card->workqueue, &card->packet_work);
1010
Colin McCabed2b21f12009-01-09 14:58:09 -08001011 return IRQ_HANDLED;
1012}
1013
1014/*
1015 * SPI callbacks
1016 */
1017
Vasily Khoruzhick16f775b2011-01-21 22:44:48 +02001018static int if_spi_init_card(struct if_spi_card *card)
1019{
1020 struct spi_device *spi = card->spi;
1021 int err, i;
1022 u32 scratch;
1023 const struct firmware *helper = NULL;
1024 const struct firmware *mainfw = NULL;
1025
1026 lbs_deb_enter(LBS_DEB_SPI);
1027
1028 err = spu_init(card, card->pdata->use_dummy_writes);
1029 if (err)
1030 goto out;
1031 err = spu_get_chip_revision(card, &card->card_id, &card->card_rev);
1032 if (err)
1033 goto out;
1034
1035 err = spu_read_u32(card, IF_SPI_SCRATCH_4_REG, &scratch);
1036 if (err)
1037 goto out;
1038 if (scratch == SUCCESSFUL_FW_DOWNLOAD_MAGIC)
1039 lbs_deb_spi("Firmware is already loaded for "
1040 "Marvell WLAN 802.11 adapter\n");
1041 else {
1042 /* Check if we support this card */
1043 for (i = 0; i < ARRAY_SIZE(fw_table); i++) {
1044 if (card->card_id == fw_table[i].model)
1045 break;
1046 }
1047 if (i == ARRAY_SIZE(fw_table)) {
Joe Perches0e4e06a2011-05-02 16:49:14 -07001048 pr_err("Unsupported chip_id: 0x%02x\n", card->card_id);
Vasily Khoruzhick16f775b2011-01-21 22:44:48 +02001049 err = -ENODEV;
1050 goto out;
1051 }
1052
1053 err = lbs_get_firmware(&card->spi->dev, NULL, NULL,
1054 card->card_id, &fw_table[0], &helper,
1055 &mainfw);
1056 if (err) {
Joe Perches0e4e06a2011-05-02 16:49:14 -07001057 pr_err("failed to find firmware (%d)\n", err);
Vasily Khoruzhick16f775b2011-01-21 22:44:48 +02001058 goto out;
1059 }
1060
1061 lbs_deb_spi("Initializing FW for Marvell WLAN 802.11 adapter "
1062 "(chip_id = 0x%04x, chip_rev = 0x%02x) "
1063 "attached to SPI bus_num %d, chip_select %d. "
1064 "spi->max_speed_hz=%d\n",
1065 card->card_id, card->card_rev,
1066 spi->master->bus_num, spi->chip_select,
1067 spi->max_speed_hz);
1068 err = if_spi_prog_helper_firmware(card, helper);
1069 if (err)
1070 goto out;
1071 err = if_spi_prog_main_firmware(card, mainfw);
1072 if (err)
1073 goto out;
1074 lbs_deb_spi("loaded FW for Marvell WLAN 802.11 adapter\n");
1075 }
1076
1077 err = spu_set_interrupt_mode(card, 0, 1);
1078 if (err)
1079 goto out;
1080
1081out:
1082 if (helper)
1083 release_firmware(helper);
1084 if (mainfw)
1085 release_firmware(mainfw);
1086
1087 lbs_deb_leave_args(LBS_DEB_SPI, "err %d\n", err);
1088
1089 return err;
1090}
1091
Vasily Khoruzhickf39de992011-03-16 16:41:46 +02001092static void if_spi_resume_worker(struct work_struct *work)
1093{
1094 struct if_spi_card *card;
1095
1096 card = container_of(work, struct if_spi_card, resume_work);
1097
1098 if (card->suspended) {
1099 if (card->pdata->setup)
1100 card->pdata->setup(card->spi);
1101
1102 /* Init card ... */
1103 if_spi_init_card(card);
1104
1105 enable_irq(card->spi->irq);
1106
1107 /* And resume it ... */
1108 lbs_resume(card->priv);
1109
1110 card->suspended = 0;
1111 }
1112}
1113
Colin McCabed2b21f12009-01-09 14:58:09 -08001114static int __devinit if_spi_probe(struct spi_device *spi)
1115{
1116 struct if_spi_card *card;
1117 struct lbs_private *priv = NULL;
1118 struct libertas_spi_platform_data *pdata = spi->dev.platform_data;
Vasily Khoruzhick16f775b2011-01-21 22:44:48 +02001119 int err = 0;
Colin McCabed2b21f12009-01-09 14:58:09 -08001120
1121 lbs_deb_enter(LBS_DEB_SPI);
1122
Mike Rapoport0c2bec92009-02-03 09:04:20 +02001123 if (!pdata) {
1124 err = -EINVAL;
1125 goto out;
1126 }
1127
1128 if (pdata->setup) {
1129 err = pdata->setup(spi);
1130 if (err)
1131 goto out;
1132 }
1133
Colin McCabed2b21f12009-01-09 14:58:09 -08001134 /* Allocate card structure to represent this specific device */
1135 card = kzalloc(sizeof(struct if_spi_card), GFP_KERNEL);
1136 if (!card) {
1137 err = -ENOMEM;
Vasily Khoruzhick16f775b2011-01-21 22:44:48 +02001138 goto teardown;
Colin McCabed2b21f12009-01-09 14:58:09 -08001139 }
1140 spi_set_drvdata(spi, card);
Mike Rapoport0c2bec92009-02-03 09:04:20 +02001141 card->pdata = pdata;
Colin McCabed2b21f12009-01-09 14:58:09 -08001142 card->spi = spi;
Colin McCabed2b21f12009-01-09 14:58:09 -08001143 card->prev_xfer_time = jiffies;
1144
Vasily Khoruzhick16f775b2011-01-21 22:44:48 +02001145 INIT_LIST_HEAD(&card->cmd_packet_list);
1146 INIT_LIST_HEAD(&card->data_packet_list);
1147 spin_lock_init(&card->buffer_lock);
Colin McCabed2b21f12009-01-09 14:58:09 -08001148
Colin McCabed2b21f12009-01-09 14:58:09 -08001149 /* Initialize the SPI Interface Unit */
Colin McCabed2b21f12009-01-09 14:58:09 -08001150
1151 /* Firmware load */
Vasily Khoruzhick16f775b2011-01-21 22:44:48 +02001152 err = if_spi_init_card(card);
Colin McCabed2b21f12009-01-09 14:58:09 -08001153 if (err)
Sebastian Andrzej Siewior4d1d4982009-06-04 21:57:03 +02001154 goto free_card;
Colin McCabed2b21f12009-01-09 14:58:09 -08001155
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001156 /*
1157 * Register our card with libertas.
1158 * This will call alloc_etherdev.
1159 */
Colin McCabed2b21f12009-01-09 14:58:09 -08001160 priv = lbs_add_card(card, &spi->dev);
1161 if (!priv) {
1162 err = -ENOMEM;
Sebastian Andrzej Siewior4d1d4982009-06-04 21:57:03 +02001163 goto free_card;
Colin McCabed2b21f12009-01-09 14:58:09 -08001164 }
1165 card->priv = priv;
Vasily Khoruzhickf39de992011-03-16 16:41:46 +02001166 priv->setup_fw_on_resume = 1;
Colin McCabed2b21f12009-01-09 14:58:09 -08001167 priv->card = card;
1168 priv->hw_host_to_card = if_spi_host_to_card;
Amitkumar Karwar49125452009-09-30 20:04:38 -07001169 priv->enter_deep_sleep = NULL;
1170 priv->exit_deep_sleep = NULL;
1171 priv->reset_deep_sleep_wakeup = NULL;
Colin McCabed2b21f12009-01-09 14:58:09 -08001172 priv->fw_ready = 1;
Colin McCabed2b21f12009-01-09 14:58:09 -08001173
1174 /* Initialize interrupt handling stuff. */
Vasily Khoruzhick16f775b2011-01-21 22:44:48 +02001175 card->workqueue = create_workqueue("libertas_spi");
1176 INIT_WORK(&card->packet_work, if_spi_host_to_card_worker);
Vasily Khoruzhickf39de992011-03-16 16:41:46 +02001177 INIT_WORK(&card->resume_work, if_spi_resume_worker);
Anna Nealb26ed972009-04-02 14:44:09 -07001178
Colin McCabed2b21f12009-01-09 14:58:09 -08001179 err = request_irq(spi->irq, if_spi_host_interrupt,
1180 IRQF_TRIGGER_FALLING, "libertas_spi", card);
1181 if (err) {
Joe Perches0e4e06a2011-05-02 16:49:14 -07001182 pr_err("can't get host irq line-- request_irq failed\n");
Vasily Khoruzhick16f775b2011-01-21 22:44:48 +02001183 goto terminate_workqueue;
Colin McCabed2b21f12009-01-09 14:58:09 -08001184 }
1185
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001186 /*
1187 * Start the card.
Colin McCabed2b21f12009-01-09 14:58:09 -08001188 * This will call register_netdev, and we'll start
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001189 * getting interrupts...
1190 */
Colin McCabed2b21f12009-01-09 14:58:09 -08001191 err = lbs_start_card(priv);
1192 if (err)
1193 goto release_irq;
1194
1195 lbs_deb_spi("Finished initializing WLAN module.\n");
1196
1197 /* successful exit */
1198 goto out;
1199
1200release_irq:
1201 free_irq(spi->irq, card);
Vasily Khoruzhick16f775b2011-01-21 22:44:48 +02001202terminate_workqueue:
1203 flush_workqueue(card->workqueue);
1204 destroy_workqueue(card->workqueue);
Colin McCabed2b21f12009-01-09 14:58:09 -08001205 lbs_remove_card(priv); /* will call free_netdev */
Colin McCabed2b21f12009-01-09 14:58:09 -08001206free_card:
1207 free_if_spi_card(card);
Vasily Khoruzhick16f775b2011-01-21 22:44:48 +02001208teardown:
1209 if (pdata->teardown)
1210 pdata->teardown(spi);
Colin McCabed2b21f12009-01-09 14:58:09 -08001211out:
1212 lbs_deb_leave_args(LBS_DEB_SPI, "err %d\n", err);
1213 return err;
1214}
1215
1216static int __devexit libertas_spi_remove(struct spi_device *spi)
1217{
1218 struct if_spi_card *card = spi_get_drvdata(spi);
1219 struct lbs_private *priv = card->priv;
1220
1221 lbs_deb_spi("libertas_spi_remove\n");
1222 lbs_deb_enter(LBS_DEB_SPI);
Colin McCabed2b21f12009-01-09 14:58:09 -08001223
Vasily Khoruzhickf39de992011-03-16 16:41:46 +02001224 cancel_work_sync(&card->resume_work);
1225
Colin McCabed2b21f12009-01-09 14:58:09 -08001226 lbs_stop_card(priv);
Andrey Yurovskyefcfd1f2009-06-18 09:51:57 -07001227 lbs_remove_card(priv); /* will call free_netdev */
1228
Colin McCabed2b21f12009-01-09 14:58:09 -08001229 free_irq(spi->irq, card);
Vasily Khoruzhick16f775b2011-01-21 22:44:48 +02001230 flush_workqueue(card->workqueue);
1231 destroy_workqueue(card->workqueue);
Mike Rapoport0c2bec92009-02-03 09:04:20 +02001232 if (card->pdata->teardown)
1233 card->pdata->teardown(spi);
Colin McCabed2b21f12009-01-09 14:58:09 -08001234 free_if_spi_card(card);
1235 lbs_deb_leave(LBS_DEB_SPI);
1236 return 0;
1237}
1238
Vasily Khoruzhickf39de992011-03-16 16:41:46 +02001239static int if_spi_suspend(struct device *dev)
1240{
1241 struct spi_device *spi = to_spi_device(dev);
1242 struct if_spi_card *card = spi_get_drvdata(spi);
1243
1244 if (!card->suspended) {
1245 lbs_suspend(card->priv);
1246 flush_workqueue(card->workqueue);
1247 disable_irq(spi->irq);
1248
1249 if (card->pdata->teardown)
1250 card->pdata->teardown(spi);
1251 card->suspended = 1;
1252 }
1253
1254 return 0;
1255}
1256
1257static int if_spi_resume(struct device *dev)
1258{
1259 struct spi_device *spi = to_spi_device(dev);
1260 struct if_spi_card *card = spi_get_drvdata(spi);
1261
1262 /* Schedule delayed work */
1263 schedule_work(&card->resume_work);
1264
1265 return 0;
1266}
1267
1268static const struct dev_pm_ops if_spi_pm_ops = {
1269 .suspend = if_spi_suspend,
1270 .resume = if_spi_resume,
1271};
1272
Colin McCabed2b21f12009-01-09 14:58:09 -08001273static struct spi_driver libertas_spi_driver = {
1274 .probe = if_spi_probe,
1275 .remove = __devexit_p(libertas_spi_remove),
1276 .driver = {
1277 .name = "libertas_spi",
1278 .bus = &spi_bus_type,
1279 .owner = THIS_MODULE,
Vasily Khoruzhickf39de992011-03-16 16:41:46 +02001280 .pm = &if_spi_pm_ops,
Colin McCabed2b21f12009-01-09 14:58:09 -08001281 },
1282};
1283
1284/*
1285 * Module functions
1286 */
1287
1288static int __init if_spi_init_module(void)
1289{
1290 int ret = 0;
1291 lbs_deb_enter(LBS_DEB_SPI);
1292 printk(KERN_INFO "libertas_spi: Libertas SPI driver\n");
1293 ret = spi_register_driver(&libertas_spi_driver);
1294 lbs_deb_leave(LBS_DEB_SPI);
1295 return ret;
1296}
1297
1298static void __exit if_spi_exit_module(void)
1299{
1300 lbs_deb_enter(LBS_DEB_SPI);
1301 spi_unregister_driver(&libertas_spi_driver);
1302 lbs_deb_leave(LBS_DEB_SPI);
1303}
1304
1305module_init(if_spi_init_module);
1306module_exit(if_spi_exit_module);
1307
1308MODULE_DESCRIPTION("Libertas SPI WLAN Driver");
1309MODULE_AUTHOR("Andrey Yurovsky <andrey@cozybit.com>, "
1310 "Colin McCabe <colin@cozybit.com>");
1311MODULE_LICENSE("GPL");
Anton Vorontsove0626e32009-09-22 16:46:08 -07001312MODULE_ALIAS("spi:libertas_spi");