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 
free_if_spi_card(struct if_spi_card * card)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 
spu_transaction_init(struct if_spi_card * card)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 
spu_transaction_finish(struct if_spi_card * card)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  */
spu_write(struct if_spi_card * card,u16 reg,const u8 * buf,int len)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 
spu_write_u16(struct if_spi_card * card,u16 reg,u16 val)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 
spu_reg_is_port_reg(u16 reg)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 
spu_read(struct if_spi_card * card,u16 reg,u8 * buf,int len)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 */
spu_read_u16(struct if_spi_card * card,u16 reg,u16 * val)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  */
spu_read_u32(struct if_spi_card * card,u16 reg,u32 * val)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  */
spu_wait_for_u16(struct if_spi_card * card,u16 reg,u16 target_mask,u16 target)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  */
spu_wait_for_u32(struct if_spi_card * card,u32 reg,u32 target)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 
spu_set_interrupt_mode(struct if_spi_card * card,int suppress_host_int,int auto_int)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 
spu_get_chip_revision(struct if_spi_card * card,u16 * card_id,u8 * card_rev)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 
spu_set_bus_mode(struct if_spi_card * card,u16 mode)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 
spu_init(struct if_spi_card * card,int use_dummy_writes)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 
if_spi_prog_helper_firmware(struct if_spi_card * card,const struct firmware * firmware)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  */
if_spi_prog_main_firmware_check_len(struct if_spi_card * card,int * crc_err)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 
if_spi_prog_main_firmware(struct if_spi_card * card,const struct firmware * firmware)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 */
if_spi_c2h_cmd(struct if_spi_card * card)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 */
if_spi_c2h_data(struct if_spi_card * card)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 		dev_kfree_skb(skb);
771 		goto out;
772 	}
773 
774 	/* pass the SKB to libertas */
775 	err = lbs_process_rxed_packet(card->priv, skb);
776 	/* lbs_process_rxed_packet() consumes the skb */
777 
778 out:
779 	if (err)
780 		netdev_err(priv->dev, "%s: err=%d\n", __func__, err);
781 
782 	return err;
783 }
784 
785 /* Move data or a command from the host to the card. */
if_spi_h2c(struct if_spi_card * card,struct if_spi_packet * packet,int type)786 static void if_spi_h2c(struct if_spi_card *card,
787 			struct if_spi_packet *packet, int type)
788 {
789 	struct lbs_private *priv = card->priv;
790 	int err = 0;
791 	u16 port_reg;
792 
793 	switch (type) {
794 	case MVMS_DAT:
795 		port_reg = IF_SPI_DATA_RDWRPORT_REG;
796 		break;
797 	case MVMS_CMD:
798 		port_reg = IF_SPI_CMD_RDWRPORT_REG;
799 		break;
800 	default:
801 		netdev_err(priv->dev, "can't transfer buffer of type %d\n",
802 			   type);
803 		err = -EINVAL;
804 		goto out;
805 	}
806 
807 	/* Write the data to the card */
808 	err = spu_write(card, port_reg, packet->buffer, packet->blen);
809 	if (err)
810 		goto out;
811 
812 out:
813 	kfree(packet);
814 
815 	if (err)
816 		netdev_err(priv->dev, "%s: error %d\n", __func__, err);
817 }
818 
819 /* Inform the host about a card event */
if_spi_e2h(struct if_spi_card * card)820 static void if_spi_e2h(struct if_spi_card *card)
821 {
822 	int err = 0;
823 	u32 cause;
824 	struct lbs_private *priv = card->priv;
825 
826 	err = spu_read_u32(card, IF_SPI_SCRATCH_3_REG, &cause);
827 	if (err)
828 		goto out;
829 
830 	/* re-enable the card event interrupt */
831 	spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG,
832 			~IF_SPI_HICU_CARD_EVENT);
833 
834 	/* generate a card interrupt */
835 	spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG, IF_SPI_CIC_HOST_EVENT);
836 
837 	lbs_queue_event(priv, cause & 0xff);
838 out:
839 	if (err)
840 		netdev_err(priv->dev, "%s: error %d\n", __func__, err);
841 }
842 
if_spi_host_to_card_worker(struct work_struct * work)843 static void if_spi_host_to_card_worker(struct work_struct *work)
844 {
845 	int err;
846 	struct if_spi_card *card;
847 	u16 hiStatus;
848 	unsigned long flags;
849 	struct if_spi_packet *packet;
850 	struct lbs_private *priv;
851 
852 	card = container_of(work, struct if_spi_card, packet_work);
853 	priv = card->priv;
854 
855 	/*
856 	 * Read the host interrupt status register to see what we
857 	 * can do.
858 	 */
859 	err = spu_read_u16(card, IF_SPI_HOST_INT_STATUS_REG,
860 				&hiStatus);
861 	if (err) {
862 		netdev_err(priv->dev, "I/O error\n");
863 		goto err;
864 	}
865 
866 	if (hiStatus & IF_SPI_HIST_CMD_UPLOAD_RDY) {
867 		err = if_spi_c2h_cmd(card);
868 		if (err)
869 			goto err;
870 	}
871 	if (hiStatus & IF_SPI_HIST_RX_UPLOAD_RDY) {
872 		err = if_spi_c2h_data(card);
873 		if (err)
874 			goto err;
875 	}
876 
877 	/*
878 	 * workaround: in PS mode, the card does not set the Command
879 	 * Download Ready bit, but it sets TX Download Ready.
880 	 */
881 	if (hiStatus & IF_SPI_HIST_CMD_DOWNLOAD_RDY ||
882 	   (card->priv->psstate != PS_STATE_FULL_POWER &&
883 	    (hiStatus & IF_SPI_HIST_TX_DOWNLOAD_RDY))) {
884 		/*
885 		 * This means two things. First of all,
886 		 * if there was a previous command sent, the card has
887 		 * successfully received it.
888 		 * Secondly, it is now ready to download another
889 		 * command.
890 		 */
891 		lbs_host_to_card_done(card->priv);
892 
893 		/* Do we have any command packets from the host to send? */
894 		packet = NULL;
895 		spin_lock_irqsave(&card->buffer_lock, flags);
896 		if (!list_empty(&card->cmd_packet_list)) {
897 			packet = (struct if_spi_packet *)(card->
898 					cmd_packet_list.next);
899 			list_del(&packet->list);
900 		}
901 		spin_unlock_irqrestore(&card->buffer_lock, flags);
902 
903 		if (packet)
904 			if_spi_h2c(card, packet, MVMS_CMD);
905 	}
906 	if (hiStatus & IF_SPI_HIST_TX_DOWNLOAD_RDY) {
907 		/* Do we have any data packets from the host to send? */
908 		packet = NULL;
909 		spin_lock_irqsave(&card->buffer_lock, flags);
910 		if (!list_empty(&card->data_packet_list)) {
911 			packet = (struct if_spi_packet *)(card->
912 					data_packet_list.next);
913 			list_del(&packet->list);
914 		}
915 		spin_unlock_irqrestore(&card->buffer_lock, flags);
916 
917 		if (packet)
918 			if_spi_h2c(card, packet, MVMS_DAT);
919 	}
920 	if (hiStatus & IF_SPI_HIST_CARD_EVENT)
921 		if_spi_e2h(card);
922 
923 err:
924 	if (err)
925 		netdev_err(priv->dev, "%s: got error %d\n", __func__, err);
926 }
927 
928 /*
929  * Host to Card
930  *
931  * Called from Libertas to transfer some data to the WLAN device
932  * We can't sleep here.
933  */
if_spi_host_to_card(struct lbs_private * priv,u8 type,u8 * buf,u16 nb)934 static int if_spi_host_to_card(struct lbs_private *priv,
935 				u8 type, u8 *buf, u16 nb)
936 {
937 	int err = 0;
938 	unsigned long flags;
939 	struct if_spi_card *card = priv->card;
940 	struct if_spi_packet *packet;
941 	u16 blen;
942 
943 	if (nb == 0) {
944 		netdev_err(priv->dev, "%s: invalid size requested: %d\n",
945 			   __func__, nb);
946 		err = -EINVAL;
947 		goto out;
948 	}
949 	blen = ALIGN(nb, 4);
950 	packet = kzalloc(sizeof(struct if_spi_packet) + blen, GFP_ATOMIC);
951 	if (!packet) {
952 		err = -ENOMEM;
953 		goto out;
954 	}
955 	packet->blen = blen;
956 	memcpy(packet->buffer, buf, nb);
957 	memset(packet->buffer + nb, 0, blen - nb);
958 
959 	switch (type) {
960 	case MVMS_CMD:
961 		priv->dnld_sent = DNLD_CMD_SENT;
962 		spin_lock_irqsave(&card->buffer_lock, flags);
963 		list_add_tail(&packet->list, &card->cmd_packet_list);
964 		spin_unlock_irqrestore(&card->buffer_lock, flags);
965 		break;
966 	case MVMS_DAT:
967 		priv->dnld_sent = DNLD_DATA_SENT;
968 		spin_lock_irqsave(&card->buffer_lock, flags);
969 		list_add_tail(&packet->list, &card->data_packet_list);
970 		spin_unlock_irqrestore(&card->buffer_lock, flags);
971 		break;
972 	default:
973 		kfree(packet);
974 		netdev_err(priv->dev, "can't transfer buffer of type %d\n",
975 			   type);
976 		err = -EINVAL;
977 		break;
978 	}
979 
980 	/* Queue spi xfer work */
981 	queue_work(card->workqueue, &card->packet_work);
982 out:
983 	return err;
984 }
985 
986 /*
987  * Host Interrupts
988  *
989  * Service incoming interrupts from the WLAN device. We can't sleep here, so
990  * don't try to talk on the SPI bus, just queue the SPI xfer work.
991  */
if_spi_host_interrupt(int irq,void * dev_id)992 static irqreturn_t if_spi_host_interrupt(int irq, void *dev_id)
993 {
994 	struct if_spi_card *card = dev_id;
995 
996 	queue_work(card->workqueue, &card->packet_work);
997 
998 	return IRQ_HANDLED;
999 }
1000 
1001 /*
1002  * SPI callbacks
1003  */
1004 
if_spi_init_card(struct if_spi_card * card)1005 static int if_spi_init_card(struct if_spi_card *card)
1006 {
1007 	struct lbs_private *priv = card->priv;
1008 	int err, i;
1009 	u32 scratch;
1010 	const struct firmware *helper = NULL;
1011 	const struct firmware *mainfw = NULL;
1012 
1013 	err = spu_init(card, card->pdata->use_dummy_writes);
1014 	if (err)
1015 		goto out;
1016 	err = spu_get_chip_revision(card, &card->card_id, &card->card_rev);
1017 	if (err)
1018 		goto out;
1019 
1020 	err = spu_read_u32(card, IF_SPI_SCRATCH_4_REG, &scratch);
1021 	if (err)
1022 		goto out;
1023 	if (scratch == SUCCESSFUL_FW_DOWNLOAD_MAGIC)
1024 		lbs_deb_spi("Firmware is already loaded for "
1025 			    "Marvell WLAN 802.11 adapter\n");
1026 	else {
1027 		/* Check if we support this card */
1028 		for (i = 0; i < ARRAY_SIZE(fw_table); i++) {
1029 			if (card->card_id == fw_table[i].model)
1030 				break;
1031 		}
1032 		if (i == ARRAY_SIZE(fw_table)) {
1033 			netdev_err(priv->dev, "Unsupported chip_id: 0x%02x\n",
1034 				   card->card_id);
1035 			err = -ENODEV;
1036 			goto out;
1037 		}
1038 
1039 		err = lbs_get_firmware(&card->spi->dev, card->card_id,
1040 					&fw_table[0], &helper, &mainfw);
1041 		if (err) {
1042 			netdev_err(priv->dev, "failed to find firmware (%d)\n",
1043 				   err);
1044 			goto out;
1045 		}
1046 
1047 		lbs_deb_spi("Initializing FW for Marvell WLAN 802.11 adapter "
1048 				"(chip_id = 0x%04x, chip_rev = 0x%02x) "
1049 				"attached to SPI bus_num %d, chip_select %d. "
1050 				"spi->max_speed_hz=%d\n",
1051 				card->card_id, card->card_rev,
1052 				card->spi->master->bus_num,
1053 				card->spi->chip_select,
1054 				card->spi->max_speed_hz);
1055 		err = if_spi_prog_helper_firmware(card, helper);
1056 		if (err)
1057 			goto out;
1058 		err = if_spi_prog_main_firmware(card, mainfw);
1059 		if (err)
1060 			goto out;
1061 		lbs_deb_spi("loaded FW for Marvell WLAN 802.11 adapter\n");
1062 	}
1063 
1064 	err = spu_set_interrupt_mode(card, 0, 1);
1065 	if (err)
1066 		goto out;
1067 
1068 out:
1069 	return err;
1070 }
1071 
if_spi_resume_worker(struct work_struct * work)1072 static void if_spi_resume_worker(struct work_struct *work)
1073 {
1074 	struct if_spi_card *card;
1075 
1076 	card = container_of(work, struct if_spi_card, resume_work);
1077 
1078 	if (card->suspended) {
1079 		if (card->pdata->setup)
1080 			card->pdata->setup(card->spi);
1081 
1082 		/* Init card ... */
1083 		if_spi_init_card(card);
1084 
1085 		enable_irq(card->spi->irq);
1086 
1087 		/* And resume it ... */
1088 		lbs_resume(card->priv);
1089 
1090 		card->suspended = 0;
1091 	}
1092 }
1093 
if_spi_probe(struct spi_device * spi)1094 static int if_spi_probe(struct spi_device *spi)
1095 {
1096 	struct if_spi_card *card;
1097 	struct lbs_private *priv = NULL;
1098 	struct libertas_spi_platform_data *pdata = dev_get_platdata(&spi->dev);
1099 	int err = 0;
1100 
1101 	if (!pdata) {
1102 		err = -EINVAL;
1103 		goto out;
1104 	}
1105 
1106 	if (pdata->setup) {
1107 		err = pdata->setup(spi);
1108 		if (err)
1109 			goto out;
1110 	}
1111 
1112 	/* Allocate card structure to represent this specific device */
1113 	card = kzalloc(sizeof(struct if_spi_card), GFP_KERNEL);
1114 	if (!card) {
1115 		err = -ENOMEM;
1116 		goto teardown;
1117 	}
1118 	spi_set_drvdata(spi, card);
1119 	card->pdata = pdata;
1120 	card->spi = spi;
1121 	card->prev_xfer_time = jiffies;
1122 
1123 	INIT_LIST_HEAD(&card->cmd_packet_list);
1124 	INIT_LIST_HEAD(&card->data_packet_list);
1125 	spin_lock_init(&card->buffer_lock);
1126 
1127 	/* Initialize the SPI Interface Unit */
1128 
1129 	/* Firmware load */
1130 	err = if_spi_init_card(card);
1131 	if (err)
1132 		goto free_card;
1133 
1134 	/*
1135 	 * Register our card with libertas.
1136 	 * This will call alloc_etherdev.
1137 	 */
1138 	priv = lbs_add_card(card, &spi->dev);
1139 	if (IS_ERR(priv)) {
1140 		err = PTR_ERR(priv);
1141 		goto free_card;
1142 	}
1143 	card->priv = priv;
1144 	priv->setup_fw_on_resume = 1;
1145 	priv->card = card;
1146 	priv->hw_host_to_card = if_spi_host_to_card;
1147 	priv->enter_deep_sleep = NULL;
1148 	priv->exit_deep_sleep = NULL;
1149 	priv->reset_deep_sleep_wakeup = NULL;
1150 	priv->fw_ready = 1;
1151 
1152 	/* Initialize interrupt handling stuff. */
1153 	card->workqueue = alloc_workqueue("libertas_spi", WQ_MEM_RECLAIM, 0);
1154 	if (!card->workqueue) {
1155 		err = -ENOMEM;
1156 		goto remove_card;
1157 	}
1158 	INIT_WORK(&card->packet_work, if_spi_host_to_card_worker);
1159 	INIT_WORK(&card->resume_work, if_spi_resume_worker);
1160 
1161 	err = request_irq(spi->irq, if_spi_host_interrupt,
1162 			IRQF_TRIGGER_FALLING, "libertas_spi", card);
1163 	if (err) {
1164 		pr_err("can't get host irq line-- request_irq failed\n");
1165 		goto terminate_workqueue;
1166 	}
1167 
1168 	/*
1169 	 * Start the card.
1170 	 * This will call register_netdev, and we'll start
1171 	 * getting interrupts...
1172 	 */
1173 	err = lbs_start_card(priv);
1174 	if (err)
1175 		goto release_irq;
1176 
1177 	lbs_deb_spi("Finished initializing WLAN module.\n");
1178 
1179 	/* successful exit */
1180 	goto out;
1181 
1182 release_irq:
1183 	free_irq(spi->irq, card);
1184 terminate_workqueue:
1185 	destroy_workqueue(card->workqueue);
1186 remove_card:
1187 	lbs_remove_card(priv); /* will call free_netdev */
1188 free_card:
1189 	free_if_spi_card(card);
1190 teardown:
1191 	if (pdata->teardown)
1192 		pdata->teardown(spi);
1193 out:
1194 	return err;
1195 }
1196 
libertas_spi_remove(struct spi_device * spi)1197 static int libertas_spi_remove(struct spi_device *spi)
1198 {
1199 	struct if_spi_card *card = spi_get_drvdata(spi);
1200 	struct lbs_private *priv = card->priv;
1201 
1202 	lbs_deb_spi("libertas_spi_remove\n");
1203 
1204 	cancel_work_sync(&card->resume_work);
1205 
1206 	lbs_stop_card(priv);
1207 	lbs_remove_card(priv); /* will call free_netdev */
1208 
1209 	free_irq(spi->irq, card);
1210 	destroy_workqueue(card->workqueue);
1211 	if (card->pdata->teardown)
1212 		card->pdata->teardown(spi);
1213 	free_if_spi_card(card);
1214 
1215 	return 0;
1216 }
1217 
if_spi_suspend(struct device * dev)1218 static int if_spi_suspend(struct device *dev)
1219 {
1220 	struct spi_device *spi = to_spi_device(dev);
1221 	struct if_spi_card *card = spi_get_drvdata(spi);
1222 
1223 	if (!card->suspended) {
1224 		lbs_suspend(card->priv);
1225 		flush_workqueue(card->workqueue);
1226 		disable_irq(spi->irq);
1227 
1228 		if (card->pdata->teardown)
1229 			card->pdata->teardown(spi);
1230 		card->suspended = 1;
1231 	}
1232 
1233 	return 0;
1234 }
1235 
if_spi_resume(struct device * dev)1236 static int if_spi_resume(struct device *dev)
1237 {
1238 	struct spi_device *spi = to_spi_device(dev);
1239 	struct if_spi_card *card = spi_get_drvdata(spi);
1240 
1241 	/* Schedule delayed work */
1242 	schedule_work(&card->resume_work);
1243 
1244 	return 0;
1245 }
1246 
1247 static const struct dev_pm_ops if_spi_pm_ops = {
1248 	.suspend	= if_spi_suspend,
1249 	.resume		= if_spi_resume,
1250 };
1251 
1252 static struct spi_driver libertas_spi_driver = {
1253 	.probe	= if_spi_probe,
1254 	.remove = libertas_spi_remove,
1255 	.driver = {
1256 		.name	= "libertas_spi",
1257 		.pm	= &if_spi_pm_ops,
1258 	},
1259 };
1260 
1261 /*
1262  * Module functions
1263  */
1264 
if_spi_init_module(void)1265 static int __init if_spi_init_module(void)
1266 {
1267 	int ret = 0;
1268 
1269 	printk(KERN_INFO "libertas_spi: Libertas SPI driver\n");
1270 	ret = spi_register_driver(&libertas_spi_driver);
1271 
1272 	return ret;
1273 }
1274 
if_spi_exit_module(void)1275 static void __exit if_spi_exit_module(void)
1276 {
1277 	spi_unregister_driver(&libertas_spi_driver);
1278 }
1279 
1280 module_init(if_spi_init_module);
1281 module_exit(if_spi_exit_module);
1282 
1283 MODULE_DESCRIPTION("Libertas SPI WLAN Driver");
1284 MODULE_AUTHOR("Andrey Yurovsky <andrey@cozybit.com>, "
1285 	      "Colin McCabe <colin@cozybit.com>");
1286 MODULE_LICENSE("GPL");
1287 MODULE_ALIAS("spi:libertas_spi");
1288