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