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