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

source code of linux/drivers/net/wireless/marvell/libertas/if_spi.c