xref: /openbmc/linux/drivers/net/ethernet/qualcomm/qca_spi.c (revision 21b9dc814d3fe447b8f1fef67efeb7a3bc0f1502)
1 /*
2  *   Copyright (c) 2011, 2012, Qualcomm Atheros Communications Inc.
3  *   Copyright (c) 2014, I2SE GmbH
4  *
5  *   Permission to use, copy, modify, and/or distribute this software
6  *   for any purpose with or without fee is hereby granted, provided
7  *   that the above copyright notice and this permission notice appear
8  *   in all copies.
9  *
10  *   THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11  *   WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12  *   WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
13  *   THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
14  *   CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15  *   LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
16  *   NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
17  *   CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /*   This module implements the Qualcomm Atheros SPI protocol for
21  *   kernel-based SPI device; it is essentially an Ethernet-to-SPI
22  *   serial converter;
23  */
24 
25 #include <linux/errno.h>
26 #include <linux/etherdevice.h>
27 #include <linux/if_arp.h>
28 #include <linux/if_ether.h>
29 #include <linux/init.h>
30 #include <linux/interrupt.h>
31 #include <linux/jiffies.h>
32 #include <linux/kernel.h>
33 #include <linux/kthread.h>
34 #include <linux/module.h>
35 #include <linux/moduleparam.h>
36 #include <linux/netdevice.h>
37 #include <linux/of.h>
38 #include <linux/of_net.h>
39 #include <linux/sched.h>
40 #include <linux/skbuff.h>
41 #include <linux/spi/spi.h>
42 #include <linux/types.h>
43 
44 #include "qca_7k.h"
45 #include "qca_7k_common.h"
46 #include "qca_debug.h"
47 #include "qca_spi.h"
48 
49 #define MAX_DMA_BURST_LEN 5000
50 
51 /*   Modules parameters     */
52 #define QCASPI_CLK_SPEED_MIN 1000000
53 #define QCASPI_CLK_SPEED_MAX 16000000
54 #define QCASPI_CLK_SPEED     8000000
55 static int qcaspi_clkspeed;
56 module_param(qcaspi_clkspeed, int, 0);
57 MODULE_PARM_DESC(qcaspi_clkspeed, "SPI bus clock speed (Hz). Use 1000000-16000000.");
58 
59 #define QCASPI_BURST_LEN_MIN 1
60 #define QCASPI_BURST_LEN_MAX MAX_DMA_BURST_LEN
61 static int qcaspi_burst_len = MAX_DMA_BURST_LEN;
62 module_param(qcaspi_burst_len, int, 0);
63 MODULE_PARM_DESC(qcaspi_burst_len, "Number of data bytes per burst. Use 1-5000.");
64 
65 #define QCASPI_PLUGGABLE_MIN 0
66 #define QCASPI_PLUGGABLE_MAX 1
67 static int qcaspi_pluggable = QCASPI_PLUGGABLE_MIN;
68 module_param(qcaspi_pluggable, int, 0);
69 MODULE_PARM_DESC(qcaspi_pluggable, "Pluggable SPI connection (yes/no).");
70 
71 #define QCASPI_WRITE_VERIFY_MIN 0
72 #define QCASPI_WRITE_VERIFY_MAX 3
73 static int wr_verify = QCASPI_WRITE_VERIFY_MIN;
74 module_param(wr_verify, int, 0);
75 MODULE_PARM_DESC(wr_verify, "SPI register write verify trails. Use 0-3.");
76 
77 #define QCASPI_TX_TIMEOUT (1 * HZ)
78 #define QCASPI_QCA7K_REBOOT_TIME_MS 1000
79 
80 static void
81 start_spi_intr_handling(struct qcaspi *qca, u16 *intr_cause)
82 {
83 	*intr_cause = 0;
84 
85 	qcaspi_write_register(qca, SPI_REG_INTR_ENABLE, 0, wr_verify);
86 	qcaspi_read_register(qca, SPI_REG_INTR_CAUSE, intr_cause);
87 	netdev_dbg(qca->net_dev, "interrupts: 0x%04x\n", *intr_cause);
88 }
89 
90 static void
91 end_spi_intr_handling(struct qcaspi *qca, u16 intr_cause)
92 {
93 	u16 intr_enable = (SPI_INT_CPU_ON |
94 			   SPI_INT_PKT_AVLBL |
95 			   SPI_INT_RDBUF_ERR |
96 			   SPI_INT_WRBUF_ERR);
97 
98 	qcaspi_write_register(qca, SPI_REG_INTR_CAUSE, intr_cause, 0);
99 	qcaspi_write_register(qca, SPI_REG_INTR_ENABLE, intr_enable, wr_verify);
100 	netdev_dbg(qca->net_dev, "acking int: 0x%04x\n", intr_cause);
101 }
102 
103 static u32
104 qcaspi_write_burst(struct qcaspi *qca, u8 *src, u32 len)
105 {
106 	__be16 cmd;
107 	struct spi_message msg;
108 	struct spi_transfer transfer[2];
109 	int ret;
110 
111 	memset(&transfer, 0, sizeof(transfer));
112 	spi_message_init(&msg);
113 
114 	cmd = cpu_to_be16(QCA7K_SPI_WRITE | QCA7K_SPI_EXTERNAL);
115 	transfer[0].tx_buf = &cmd;
116 	transfer[0].len = QCASPI_CMD_LEN;
117 	transfer[1].tx_buf = src;
118 	transfer[1].len = len;
119 
120 	spi_message_add_tail(&transfer[0], &msg);
121 	spi_message_add_tail(&transfer[1], &msg);
122 	ret = spi_sync(qca->spi_dev, &msg);
123 
124 	if (ret || (msg.actual_length != QCASPI_CMD_LEN + len)) {
125 		qcaspi_spi_error(qca);
126 		return 0;
127 	}
128 
129 	return len;
130 }
131 
132 static u32
133 qcaspi_write_legacy(struct qcaspi *qca, u8 *src, u32 len)
134 {
135 	struct spi_message msg;
136 	struct spi_transfer transfer;
137 	int ret;
138 
139 	memset(&transfer, 0, sizeof(transfer));
140 	spi_message_init(&msg);
141 
142 	transfer.tx_buf = src;
143 	transfer.len = len;
144 
145 	spi_message_add_tail(&transfer, &msg);
146 	ret = spi_sync(qca->spi_dev, &msg);
147 
148 	if (ret || (msg.actual_length != len)) {
149 		qcaspi_spi_error(qca);
150 		return 0;
151 	}
152 
153 	return len;
154 }
155 
156 static u32
157 qcaspi_read_burst(struct qcaspi *qca, u8 *dst, u32 len)
158 {
159 	struct spi_message msg;
160 	__be16 cmd;
161 	struct spi_transfer transfer[2];
162 	int ret;
163 
164 	memset(&transfer, 0, sizeof(transfer));
165 	spi_message_init(&msg);
166 
167 	cmd = cpu_to_be16(QCA7K_SPI_READ | QCA7K_SPI_EXTERNAL);
168 	transfer[0].tx_buf = &cmd;
169 	transfer[0].len = QCASPI_CMD_LEN;
170 	transfer[1].rx_buf = dst;
171 	transfer[1].len = len;
172 
173 	spi_message_add_tail(&transfer[0], &msg);
174 	spi_message_add_tail(&transfer[1], &msg);
175 	ret = spi_sync(qca->spi_dev, &msg);
176 
177 	if (ret || (msg.actual_length != QCASPI_CMD_LEN + len)) {
178 		qcaspi_spi_error(qca);
179 		return 0;
180 	}
181 
182 	return len;
183 }
184 
185 static u32
186 qcaspi_read_legacy(struct qcaspi *qca, u8 *dst, u32 len)
187 {
188 	struct spi_message msg;
189 	struct spi_transfer transfer;
190 	int ret;
191 
192 	memset(&transfer, 0, sizeof(transfer));
193 	spi_message_init(&msg);
194 
195 	transfer.rx_buf = dst;
196 	transfer.len = len;
197 
198 	spi_message_add_tail(&transfer, &msg);
199 	ret = spi_sync(qca->spi_dev, &msg);
200 
201 	if (ret || (msg.actual_length != len)) {
202 		qcaspi_spi_error(qca);
203 		return 0;
204 	}
205 
206 	return len;
207 }
208 
209 static int
210 qcaspi_tx_cmd(struct qcaspi *qca, u16 cmd)
211 {
212 	__be16 tx_data;
213 	struct spi_message msg;
214 	struct spi_transfer transfer;
215 	int ret;
216 
217 	memset(&transfer, 0, sizeof(transfer));
218 
219 	spi_message_init(&msg);
220 
221 	tx_data = cpu_to_be16(cmd);
222 	transfer.len = sizeof(cmd);
223 	transfer.tx_buf = &tx_data;
224 	spi_message_add_tail(&transfer, &msg);
225 
226 	ret = spi_sync(qca->spi_dev, &msg);
227 
228 	if (!ret)
229 		ret = msg.status;
230 
231 	if (ret)
232 		qcaspi_spi_error(qca);
233 
234 	return ret;
235 }
236 
237 static int
238 qcaspi_tx_frame(struct qcaspi *qca, struct sk_buff *skb)
239 {
240 	u32 count;
241 	u32 written;
242 	u32 offset;
243 	u32 len;
244 
245 	len = skb->len;
246 
247 	qcaspi_write_register(qca, SPI_REG_BFR_SIZE, len, wr_verify);
248 	if (qca->legacy_mode)
249 		qcaspi_tx_cmd(qca, QCA7K_SPI_WRITE | QCA7K_SPI_EXTERNAL);
250 
251 	offset = 0;
252 	while (len) {
253 		count = len;
254 		if (count > qca->burst_len)
255 			count = qca->burst_len;
256 
257 		if (qca->legacy_mode) {
258 			written = qcaspi_write_legacy(qca,
259 						      skb->data + offset,
260 						      count);
261 		} else {
262 			written = qcaspi_write_burst(qca,
263 						     skb->data + offset,
264 						     count);
265 		}
266 
267 		if (written != count)
268 			return -1;
269 
270 		offset += count;
271 		len -= count;
272 	}
273 
274 	return 0;
275 }
276 
277 static int
278 qcaspi_transmit(struct qcaspi *qca)
279 {
280 	struct net_device_stats *n_stats = &qca->net_dev->stats;
281 	u16 available = 0;
282 	u32 pkt_len;
283 	u16 new_head;
284 	u16 packets = 0;
285 
286 	if (qca->txr.skb[qca->txr.head] == NULL)
287 		return 0;
288 
289 	qcaspi_read_register(qca, SPI_REG_WRBUF_SPC_AVA, &available);
290 
291 	if (available > QCASPI_HW_BUF_LEN) {
292 		/* This could only happen by interferences on the SPI line.
293 		 * So retry later ...
294 		 */
295 		qca->stats.buf_avail_err++;
296 		return -1;
297 	}
298 
299 	while (qca->txr.skb[qca->txr.head]) {
300 		pkt_len = qca->txr.skb[qca->txr.head]->len + QCASPI_HW_PKT_LEN;
301 
302 		if (available < pkt_len) {
303 			if (packets == 0)
304 				qca->stats.write_buf_miss++;
305 			break;
306 		}
307 
308 		if (qcaspi_tx_frame(qca, qca->txr.skb[qca->txr.head]) == -1) {
309 			qca->stats.write_err++;
310 			return -1;
311 		}
312 
313 		packets++;
314 		n_stats->tx_packets++;
315 		n_stats->tx_bytes += qca->txr.skb[qca->txr.head]->len;
316 		available -= pkt_len;
317 
318 		/* remove the skb from the queue */
319 		/* XXX After inconsistent lock states netif_tx_lock()
320 		 * has been replaced by netif_tx_lock_bh() and so on.
321 		 */
322 		netif_tx_lock_bh(qca->net_dev);
323 		dev_kfree_skb(qca->txr.skb[qca->txr.head]);
324 		qca->txr.skb[qca->txr.head] = NULL;
325 		qca->txr.size -= pkt_len;
326 		new_head = qca->txr.head + 1;
327 		if (new_head >= qca->txr.count)
328 			new_head = 0;
329 		qca->txr.head = new_head;
330 		if (netif_queue_stopped(qca->net_dev))
331 			netif_wake_queue(qca->net_dev);
332 		netif_tx_unlock_bh(qca->net_dev);
333 	}
334 
335 	return 0;
336 }
337 
338 static int
339 qcaspi_receive(struct qcaspi *qca)
340 {
341 	struct net_device *net_dev = qca->net_dev;
342 	struct net_device_stats *n_stats = &net_dev->stats;
343 	u16 available = 0;
344 	u32 bytes_read;
345 	u8 *cp;
346 
347 	/* Allocate rx SKB if we don't have one available. */
348 	if (!qca->rx_skb) {
349 		qca->rx_skb = netdev_alloc_skb_ip_align(net_dev,
350 							net_dev->mtu +
351 							VLAN_ETH_HLEN);
352 		if (!qca->rx_skb) {
353 			netdev_dbg(net_dev, "out of RX resources\n");
354 			qca->stats.out_of_mem++;
355 			return -1;
356 		}
357 	}
358 
359 	/* Read the packet size. */
360 	qcaspi_read_register(qca, SPI_REG_RDBUF_BYTE_AVA, &available);
361 
362 	netdev_dbg(net_dev, "qcaspi_receive: SPI_REG_RDBUF_BYTE_AVA: Value: %08x\n",
363 		   available);
364 
365 	if (available > QCASPI_HW_BUF_LEN + QCASPI_HW_PKT_LEN) {
366 		/* This could only happen by interferences on the SPI line.
367 		 * So retry later ...
368 		 */
369 		qca->stats.buf_avail_err++;
370 		return -1;
371 	} else if (available == 0) {
372 		netdev_dbg(net_dev, "qcaspi_receive called without any data being available!\n");
373 		return -1;
374 	}
375 
376 	qcaspi_write_register(qca, SPI_REG_BFR_SIZE, available, wr_verify);
377 
378 	if (qca->legacy_mode)
379 		qcaspi_tx_cmd(qca, QCA7K_SPI_READ | QCA7K_SPI_EXTERNAL);
380 
381 	while (available) {
382 		u32 count = available;
383 
384 		if (count > qca->burst_len)
385 			count = qca->burst_len;
386 
387 		if (qca->legacy_mode) {
388 			bytes_read = qcaspi_read_legacy(qca, qca->rx_buffer,
389 							count);
390 		} else {
391 			bytes_read = qcaspi_read_burst(qca, qca->rx_buffer,
392 						       count);
393 		}
394 
395 		netdev_dbg(net_dev, "available: %d, byte read: %d\n",
396 			   available, bytes_read);
397 
398 		if (bytes_read) {
399 			available -= bytes_read;
400 		} else {
401 			qca->stats.read_err++;
402 			return -1;
403 		}
404 
405 		cp = qca->rx_buffer;
406 
407 		while ((bytes_read--) && (qca->rx_skb)) {
408 			s32 retcode;
409 
410 			retcode = qcafrm_fsm_decode(&qca->frm_handle,
411 						    qca->rx_skb->data,
412 						    skb_tailroom(qca->rx_skb),
413 						    *cp);
414 			cp++;
415 			switch (retcode) {
416 			case QCAFRM_GATHER:
417 			case QCAFRM_NOHEAD:
418 				break;
419 			case QCAFRM_NOTAIL:
420 				netdev_dbg(net_dev, "no RX tail\n");
421 				n_stats->rx_errors++;
422 				n_stats->rx_dropped++;
423 				break;
424 			case QCAFRM_INVLEN:
425 				netdev_dbg(net_dev, "invalid RX length\n");
426 				n_stats->rx_errors++;
427 				n_stats->rx_dropped++;
428 				break;
429 			default:
430 				qca->rx_skb->dev = qca->net_dev;
431 				n_stats->rx_packets++;
432 				n_stats->rx_bytes += retcode;
433 				skb_put(qca->rx_skb, retcode);
434 				qca->rx_skb->protocol = eth_type_trans(
435 					qca->rx_skb, qca->rx_skb->dev);
436 				skb_checksum_none_assert(qca->rx_skb);
437 				netif_rx(qca->rx_skb);
438 				qca->rx_skb = netdev_alloc_skb_ip_align(net_dev,
439 					net_dev->mtu + VLAN_ETH_HLEN);
440 				if (!qca->rx_skb) {
441 					netdev_dbg(net_dev, "out of RX resources\n");
442 					n_stats->rx_errors++;
443 					qca->stats.out_of_mem++;
444 					break;
445 				}
446 			}
447 		}
448 	}
449 
450 	return 0;
451 }
452 
453 /*   Check that tx ring stores only so much bytes
454  *   that fit into the internal QCA buffer.
455  */
456 
457 static int
458 qcaspi_tx_ring_has_space(struct tx_ring *txr)
459 {
460 	if (txr->skb[txr->tail])
461 		return 0;
462 
463 	return (txr->size + QCAFRM_MAX_LEN < QCASPI_HW_BUF_LEN) ? 1 : 0;
464 }
465 
466 /*   Flush the tx ring. This function is only safe to
467  *   call from the qcaspi_spi_thread.
468  */
469 
470 static void
471 qcaspi_flush_tx_ring(struct qcaspi *qca)
472 {
473 	int i;
474 
475 	/* XXX After inconsistent lock states netif_tx_lock()
476 	 * has been replaced by netif_tx_lock_bh() and so on.
477 	 */
478 	netif_tx_lock_bh(qca->net_dev);
479 	for (i = 0; i < TX_RING_MAX_LEN; i++) {
480 		if (qca->txr.skb[i]) {
481 			dev_kfree_skb(qca->txr.skb[i]);
482 			qca->txr.skb[i] = NULL;
483 			qca->net_dev->stats.tx_dropped++;
484 		}
485 	}
486 	qca->txr.tail = 0;
487 	qca->txr.head = 0;
488 	qca->txr.size = 0;
489 	netif_tx_unlock_bh(qca->net_dev);
490 }
491 
492 static void
493 qcaspi_qca7k_sync(struct qcaspi *qca, int event)
494 {
495 	u16 signature = 0;
496 	u16 spi_config;
497 	u16 wrbuf_space = 0;
498 
499 	if (event == QCASPI_EVENT_CPUON) {
500 		/* Read signature twice, if not valid
501 		 * go back to unknown state.
502 		 */
503 		qcaspi_read_register(qca, SPI_REG_SIGNATURE, &signature);
504 		qcaspi_read_register(qca, SPI_REG_SIGNATURE, &signature);
505 		if (signature != QCASPI_GOOD_SIGNATURE) {
506 			if (qca->sync == QCASPI_SYNC_READY)
507 				qca->stats.bad_signature++;
508 
509 			qca->sync = QCASPI_SYNC_UNKNOWN;
510 			netdev_dbg(qca->net_dev, "sync: got CPU on, but signature was invalid, restart\n");
511 			return;
512 		} else {
513 			/* ensure that the WRBUF is empty */
514 			qcaspi_read_register(qca, SPI_REG_WRBUF_SPC_AVA,
515 					     &wrbuf_space);
516 			if (wrbuf_space != QCASPI_HW_BUF_LEN) {
517 				netdev_dbg(qca->net_dev, "sync: got CPU on, but wrbuf not empty. reset!\n");
518 				qca->sync = QCASPI_SYNC_UNKNOWN;
519 			} else {
520 				netdev_dbg(qca->net_dev, "sync: got CPU on, now in sync\n");
521 				qca->sync = QCASPI_SYNC_READY;
522 				return;
523 			}
524 		}
525 	}
526 
527 	switch (qca->sync) {
528 	case QCASPI_SYNC_READY:
529 		/* Check signature twice, if not valid go to unknown state. */
530 		qcaspi_read_register(qca, SPI_REG_SIGNATURE, &signature);
531 		if (signature != QCASPI_GOOD_SIGNATURE)
532 			qcaspi_read_register(qca, SPI_REG_SIGNATURE, &signature);
533 
534 		if (signature != QCASPI_GOOD_SIGNATURE) {
535 			qca->sync = QCASPI_SYNC_UNKNOWN;
536 			qca->stats.bad_signature++;
537 			netdev_dbg(qca->net_dev, "sync: bad signature, restart\n");
538 			/* don't reset right away */
539 			return;
540 		}
541 		break;
542 	case QCASPI_SYNC_UNKNOWN:
543 		/* Read signature, if not valid stay in unknown state */
544 		qcaspi_read_register(qca, SPI_REG_SIGNATURE, &signature);
545 		if (signature != QCASPI_GOOD_SIGNATURE) {
546 			netdev_dbg(qca->net_dev, "sync: could not read signature to reset device, retry.\n");
547 			return;
548 		}
549 
550 		/* TODO: use GPIO to reset QCA7000 in legacy mode*/
551 		netdev_dbg(qca->net_dev, "sync: resetting device.\n");
552 		qcaspi_read_register(qca, SPI_REG_SPI_CONFIG, &spi_config);
553 		spi_config |= QCASPI_SLAVE_RESET_BIT;
554 		qcaspi_write_register(qca, SPI_REG_SPI_CONFIG, spi_config, 0);
555 
556 		qca->sync = QCASPI_SYNC_RESET;
557 		qca->stats.trig_reset++;
558 		qca->reset_count = 0;
559 		break;
560 	case QCASPI_SYNC_RESET:
561 		qca->reset_count++;
562 		netdev_dbg(qca->net_dev, "sync: waiting for CPU on, count %u.\n",
563 			   qca->reset_count);
564 		if (qca->reset_count >= QCASPI_RESET_TIMEOUT) {
565 			/* reset did not seem to take place, try again */
566 			qca->sync = QCASPI_SYNC_UNKNOWN;
567 			qca->stats.reset_timeout++;
568 			netdev_dbg(qca->net_dev, "sync: reset timeout, restarting process.\n");
569 		}
570 		break;
571 	}
572 }
573 
574 static int
575 qcaspi_spi_thread(void *data)
576 {
577 	struct qcaspi *qca = data;
578 	u16 intr_cause = 0;
579 
580 	netdev_info(qca->net_dev, "SPI thread created\n");
581 	while (!kthread_should_stop()) {
582 		set_current_state(TASK_INTERRUPTIBLE);
583 		if (kthread_should_park()) {
584 			netif_tx_disable(qca->net_dev);
585 			netif_carrier_off(qca->net_dev);
586 			qcaspi_flush_tx_ring(qca);
587 			kthread_parkme();
588 			if (qca->sync == QCASPI_SYNC_READY) {
589 				netif_carrier_on(qca->net_dev);
590 				netif_wake_queue(qca->net_dev);
591 			}
592 			continue;
593 		}
594 
595 		if ((qca->intr_req == qca->intr_svc) &&
596 		    !qca->txr.skb[qca->txr.head])
597 			schedule();
598 
599 		set_current_state(TASK_RUNNING);
600 
601 		netdev_dbg(qca->net_dev, "have work to do. int: %d, tx_skb: %p\n",
602 			   qca->intr_req - qca->intr_svc,
603 			   qca->txr.skb[qca->txr.head]);
604 
605 		qcaspi_qca7k_sync(qca, QCASPI_EVENT_UPDATE);
606 
607 		if (qca->sync != QCASPI_SYNC_READY) {
608 			netdev_dbg(qca->net_dev, "sync: not ready %u, turn off carrier and flush\n",
609 				   (unsigned int)qca->sync);
610 			netif_stop_queue(qca->net_dev);
611 			netif_carrier_off(qca->net_dev);
612 			qcaspi_flush_tx_ring(qca);
613 			msleep(QCASPI_QCA7K_REBOOT_TIME_MS);
614 		}
615 
616 		if (qca->intr_svc != qca->intr_req) {
617 			qca->intr_svc = qca->intr_req;
618 			start_spi_intr_handling(qca, &intr_cause);
619 
620 			if (intr_cause & SPI_INT_CPU_ON) {
621 				qcaspi_qca7k_sync(qca, QCASPI_EVENT_CPUON);
622 
623 				/* not synced. */
624 				if (qca->sync != QCASPI_SYNC_READY)
625 					continue;
626 
627 				qca->stats.device_reset++;
628 				netif_wake_queue(qca->net_dev);
629 				netif_carrier_on(qca->net_dev);
630 			}
631 
632 			if (intr_cause & SPI_INT_RDBUF_ERR) {
633 				/* restart sync */
634 				netdev_dbg(qca->net_dev, "===> rdbuf error!\n");
635 				qca->stats.read_buf_err++;
636 				qca->sync = QCASPI_SYNC_UNKNOWN;
637 				continue;
638 			}
639 
640 			if (intr_cause & SPI_INT_WRBUF_ERR) {
641 				/* restart sync */
642 				netdev_dbg(qca->net_dev, "===> wrbuf error!\n");
643 				qca->stats.write_buf_err++;
644 				qca->sync = QCASPI_SYNC_UNKNOWN;
645 				continue;
646 			}
647 
648 			/* can only handle other interrupts
649 			 * if sync has occurred
650 			 */
651 			if (qca->sync == QCASPI_SYNC_READY) {
652 				if (intr_cause & SPI_INT_PKT_AVLBL)
653 					qcaspi_receive(qca);
654 			}
655 
656 			end_spi_intr_handling(qca, intr_cause);
657 		}
658 
659 		if (qca->sync == QCASPI_SYNC_READY)
660 			qcaspi_transmit(qca);
661 	}
662 	set_current_state(TASK_RUNNING);
663 	netdev_info(qca->net_dev, "SPI thread exit\n");
664 
665 	return 0;
666 }
667 
668 static irqreturn_t
669 qcaspi_intr_handler(int irq, void *data)
670 {
671 	struct qcaspi *qca = data;
672 
673 	qca->intr_req++;
674 	if (qca->spi_thread)
675 		wake_up_process(qca->spi_thread);
676 
677 	return IRQ_HANDLED;
678 }
679 
680 static int
681 qcaspi_netdev_open(struct net_device *dev)
682 {
683 	struct qcaspi *qca = netdev_priv(dev);
684 	int ret = 0;
685 
686 	if (!qca)
687 		return -EINVAL;
688 
689 	qca->intr_req = 1;
690 	qca->intr_svc = 0;
691 	qca->sync = QCASPI_SYNC_UNKNOWN;
692 	qcafrm_fsm_init_spi(&qca->frm_handle);
693 
694 	qca->spi_thread = kthread_run((void *)qcaspi_spi_thread,
695 				      qca, "%s", dev->name);
696 
697 	if (IS_ERR(qca->spi_thread)) {
698 		netdev_err(dev, "%s: unable to start kernel thread.\n",
699 			   QCASPI_DRV_NAME);
700 		return PTR_ERR(qca->spi_thread);
701 	}
702 
703 	ret = request_irq(qca->spi_dev->irq, qcaspi_intr_handler, 0,
704 			  dev->name, qca);
705 	if (ret) {
706 		netdev_err(dev, "%s: unable to get IRQ %d (irqval=%d).\n",
707 			   QCASPI_DRV_NAME, qca->spi_dev->irq, ret);
708 		kthread_stop(qca->spi_thread);
709 		return ret;
710 	}
711 
712 	/* SPI thread takes care of TX queue */
713 
714 	return 0;
715 }
716 
717 static int
718 qcaspi_netdev_close(struct net_device *dev)
719 {
720 	struct qcaspi *qca = netdev_priv(dev);
721 
722 	netif_stop_queue(dev);
723 
724 	qcaspi_write_register(qca, SPI_REG_INTR_ENABLE, 0, wr_verify);
725 	free_irq(qca->spi_dev->irq, qca);
726 
727 	kthread_stop(qca->spi_thread);
728 	qca->spi_thread = NULL;
729 	qcaspi_flush_tx_ring(qca);
730 
731 	return 0;
732 }
733 
734 static netdev_tx_t
735 qcaspi_netdev_xmit(struct sk_buff *skb, struct net_device *dev)
736 {
737 	u32 frame_len;
738 	u8 *ptmp;
739 	struct qcaspi *qca = netdev_priv(dev);
740 	u16 new_tail;
741 	struct sk_buff *tskb;
742 	u8 pad_len = 0;
743 
744 	if (skb->len < QCAFRM_MIN_LEN)
745 		pad_len = QCAFRM_MIN_LEN - skb->len;
746 
747 	if (qca->txr.skb[qca->txr.tail]) {
748 		netdev_warn(qca->net_dev, "queue was unexpectedly full!\n");
749 		netif_stop_queue(qca->net_dev);
750 		qca->stats.ring_full++;
751 		return NETDEV_TX_BUSY;
752 	}
753 
754 	if ((skb_headroom(skb) < QCAFRM_HEADER_LEN) ||
755 	    (skb_tailroom(skb) < QCAFRM_FOOTER_LEN + pad_len)) {
756 		tskb = skb_copy_expand(skb, QCAFRM_HEADER_LEN,
757 				       QCAFRM_FOOTER_LEN + pad_len, GFP_ATOMIC);
758 		if (!tskb) {
759 			qca->stats.out_of_mem++;
760 			return NETDEV_TX_BUSY;
761 		}
762 		dev_kfree_skb(skb);
763 		skb = tskb;
764 	}
765 
766 	frame_len = skb->len + pad_len;
767 
768 	ptmp = skb_push(skb, QCAFRM_HEADER_LEN);
769 	qcafrm_create_header(ptmp, frame_len);
770 
771 	if (pad_len) {
772 		ptmp = skb_put_zero(skb, pad_len);
773 	}
774 
775 	ptmp = skb_put(skb, QCAFRM_FOOTER_LEN);
776 	qcafrm_create_footer(ptmp);
777 
778 	netdev_dbg(qca->net_dev, "Tx-ing packet: Size: 0x%08x\n",
779 		   skb->len);
780 
781 	qca->txr.size += skb->len + QCASPI_HW_PKT_LEN;
782 
783 	new_tail = qca->txr.tail + 1;
784 	if (new_tail >= qca->txr.count)
785 		new_tail = 0;
786 
787 	qca->txr.skb[qca->txr.tail] = skb;
788 	qca->txr.tail = new_tail;
789 
790 	if (!qcaspi_tx_ring_has_space(&qca->txr)) {
791 		netif_stop_queue(qca->net_dev);
792 		qca->stats.ring_full++;
793 	}
794 
795 	netif_trans_update(dev);
796 
797 	if (qca->spi_thread)
798 		wake_up_process(qca->spi_thread);
799 
800 	return NETDEV_TX_OK;
801 }
802 
803 static void
804 qcaspi_netdev_tx_timeout(struct net_device *dev, unsigned int txqueue)
805 {
806 	struct qcaspi *qca = netdev_priv(dev);
807 
808 	netdev_info(qca->net_dev, "Transmit timeout at %ld, latency %ld\n",
809 		    jiffies, jiffies - dev_trans_start(dev));
810 	qca->net_dev->stats.tx_errors++;
811 	/* Trigger tx queue flush and QCA7000 reset */
812 	qca->sync = QCASPI_SYNC_UNKNOWN;
813 
814 	if (qca->spi_thread)
815 		wake_up_process(qca->spi_thread);
816 }
817 
818 static int
819 qcaspi_netdev_init(struct net_device *dev)
820 {
821 	struct qcaspi *qca = netdev_priv(dev);
822 
823 	dev->mtu = QCAFRM_MAX_MTU;
824 	dev->type = ARPHRD_ETHER;
825 	qca->clkspeed = qcaspi_clkspeed;
826 	qca->burst_len = qcaspi_burst_len;
827 	qca->spi_thread = NULL;
828 	qca->buffer_size = (dev->mtu + VLAN_ETH_HLEN + QCAFRM_HEADER_LEN +
829 		QCAFRM_FOOTER_LEN + 4) * 4;
830 
831 	memset(&qca->stats, 0, sizeof(struct qcaspi_stats));
832 
833 	qca->rx_buffer = kmalloc(qca->buffer_size, GFP_KERNEL);
834 	if (!qca->rx_buffer)
835 		return -ENOBUFS;
836 
837 	qca->rx_skb = netdev_alloc_skb_ip_align(dev, qca->net_dev->mtu +
838 						VLAN_ETH_HLEN);
839 	if (!qca->rx_skb) {
840 		kfree(qca->rx_buffer);
841 		netdev_info(qca->net_dev, "Failed to allocate RX sk_buff.\n");
842 		return -ENOBUFS;
843 	}
844 
845 	return 0;
846 }
847 
848 static void
849 qcaspi_netdev_uninit(struct net_device *dev)
850 {
851 	struct qcaspi *qca = netdev_priv(dev);
852 
853 	kfree(qca->rx_buffer);
854 	qca->buffer_size = 0;
855 	dev_kfree_skb(qca->rx_skb);
856 }
857 
858 static const struct net_device_ops qcaspi_netdev_ops = {
859 	.ndo_init = qcaspi_netdev_init,
860 	.ndo_uninit = qcaspi_netdev_uninit,
861 	.ndo_open = qcaspi_netdev_open,
862 	.ndo_stop = qcaspi_netdev_close,
863 	.ndo_start_xmit = qcaspi_netdev_xmit,
864 	.ndo_set_mac_address = eth_mac_addr,
865 	.ndo_tx_timeout = qcaspi_netdev_tx_timeout,
866 	.ndo_validate_addr = eth_validate_addr,
867 };
868 
869 static void
870 qcaspi_netdev_setup(struct net_device *dev)
871 {
872 	struct qcaspi *qca = NULL;
873 
874 	dev->netdev_ops = &qcaspi_netdev_ops;
875 	qcaspi_set_ethtool_ops(dev);
876 	dev->watchdog_timeo = QCASPI_TX_TIMEOUT;
877 	dev->priv_flags &= ~IFF_TX_SKB_SHARING;
878 	dev->tx_queue_len = 100;
879 
880 	/* MTU range: 46 - 1500 */
881 	dev->min_mtu = QCAFRM_MIN_MTU;
882 	dev->max_mtu = QCAFRM_MAX_MTU;
883 
884 	qca = netdev_priv(dev);
885 	memset(qca, 0, sizeof(struct qcaspi));
886 
887 	memset(&qca->txr, 0, sizeof(qca->txr));
888 	qca->txr.count = TX_RING_MAX_LEN;
889 }
890 
891 static const struct of_device_id qca_spi_of_match[] = {
892 	{ .compatible = "qca,qca7000" },
893 	{ /* sentinel */ }
894 };
895 MODULE_DEVICE_TABLE(of, qca_spi_of_match);
896 
897 static int
898 qca_spi_probe(struct spi_device *spi)
899 {
900 	struct qcaspi *qca = NULL;
901 	struct net_device *qcaspi_devs = NULL;
902 	u8 legacy_mode = 0;
903 	u16 signature;
904 	int ret;
905 
906 	if (!spi->dev.of_node) {
907 		dev_err(&spi->dev, "Missing device tree\n");
908 		return -EINVAL;
909 	}
910 
911 	legacy_mode = of_property_read_bool(spi->dev.of_node,
912 					    "qca,legacy-mode");
913 
914 	if (qcaspi_clkspeed == 0) {
915 		if (spi->max_speed_hz)
916 			qcaspi_clkspeed = spi->max_speed_hz;
917 		else
918 			qcaspi_clkspeed = QCASPI_CLK_SPEED;
919 	}
920 
921 	if ((qcaspi_clkspeed < QCASPI_CLK_SPEED_MIN) ||
922 	    (qcaspi_clkspeed > QCASPI_CLK_SPEED_MAX)) {
923 		dev_err(&spi->dev, "Invalid clkspeed: %d\n",
924 			qcaspi_clkspeed);
925 		return -EINVAL;
926 	}
927 
928 	if ((qcaspi_burst_len < QCASPI_BURST_LEN_MIN) ||
929 	    (qcaspi_burst_len > QCASPI_BURST_LEN_MAX)) {
930 		dev_err(&spi->dev, "Invalid burst len: %d\n",
931 			qcaspi_burst_len);
932 		return -EINVAL;
933 	}
934 
935 	if ((qcaspi_pluggable < QCASPI_PLUGGABLE_MIN) ||
936 	    (qcaspi_pluggable > QCASPI_PLUGGABLE_MAX)) {
937 		dev_err(&spi->dev, "Invalid pluggable: %d\n",
938 			qcaspi_pluggable);
939 		return -EINVAL;
940 	}
941 
942 	if (wr_verify < QCASPI_WRITE_VERIFY_MIN ||
943 	    wr_verify > QCASPI_WRITE_VERIFY_MAX) {
944 		dev_err(&spi->dev, "Invalid write verify: %d\n",
945 			wr_verify);
946 		return -EINVAL;
947 	}
948 
949 	dev_info(&spi->dev, "ver=%s, clkspeed=%d, burst_len=%d, pluggable=%d\n",
950 		 QCASPI_DRV_VERSION,
951 		 qcaspi_clkspeed,
952 		 qcaspi_burst_len,
953 		 qcaspi_pluggable);
954 
955 	spi->mode = SPI_MODE_3;
956 	spi->max_speed_hz = qcaspi_clkspeed;
957 	if (spi_setup(spi) < 0) {
958 		dev_err(&spi->dev, "Unable to setup SPI device\n");
959 		return -EFAULT;
960 	}
961 
962 	qcaspi_devs = alloc_etherdev(sizeof(struct qcaspi));
963 	if (!qcaspi_devs)
964 		return -ENOMEM;
965 
966 	qcaspi_netdev_setup(qcaspi_devs);
967 	SET_NETDEV_DEV(qcaspi_devs, &spi->dev);
968 
969 	qca = netdev_priv(qcaspi_devs);
970 	if (!qca) {
971 		free_netdev(qcaspi_devs);
972 		dev_err(&spi->dev, "Fail to retrieve private structure\n");
973 		return -ENOMEM;
974 	}
975 	qca->net_dev = qcaspi_devs;
976 	qca->spi_dev = spi;
977 	qca->legacy_mode = legacy_mode;
978 
979 	spi_set_drvdata(spi, qcaspi_devs);
980 
981 	ret = of_get_ethdev_address(spi->dev.of_node, qca->net_dev);
982 	if (ret) {
983 		eth_hw_addr_random(qca->net_dev);
984 		dev_info(&spi->dev, "Using random MAC address: %pM\n",
985 			 qca->net_dev->dev_addr);
986 	}
987 
988 	netif_carrier_off(qca->net_dev);
989 
990 	if (!qcaspi_pluggable) {
991 		qcaspi_read_register(qca, SPI_REG_SIGNATURE, &signature);
992 		qcaspi_read_register(qca, SPI_REG_SIGNATURE, &signature);
993 
994 		if (signature != QCASPI_GOOD_SIGNATURE) {
995 			dev_err(&spi->dev, "Invalid signature (0x%04X)\n",
996 				signature);
997 			free_netdev(qcaspi_devs);
998 			return -EFAULT;
999 		}
1000 	}
1001 
1002 	if (register_netdev(qcaspi_devs)) {
1003 		dev_err(&spi->dev, "Unable to register net device %s\n",
1004 			qcaspi_devs->name);
1005 		free_netdev(qcaspi_devs);
1006 		return -EFAULT;
1007 	}
1008 
1009 	qcaspi_init_device_debugfs(qca);
1010 
1011 	return 0;
1012 }
1013 
1014 static void
1015 qca_spi_remove(struct spi_device *spi)
1016 {
1017 	struct net_device *qcaspi_devs = spi_get_drvdata(spi);
1018 	struct qcaspi *qca = netdev_priv(qcaspi_devs);
1019 
1020 	qcaspi_remove_device_debugfs(qca);
1021 
1022 	unregister_netdev(qcaspi_devs);
1023 	free_netdev(qcaspi_devs);
1024 }
1025 
1026 static const struct spi_device_id qca_spi_id[] = {
1027 	{ "qca7000", 0 },
1028 	{ /* sentinel */ }
1029 };
1030 MODULE_DEVICE_TABLE(spi, qca_spi_id);
1031 
1032 static struct spi_driver qca_spi_driver = {
1033 	.driver	= {
1034 		.name	= QCASPI_DRV_NAME,
1035 		.of_match_table = qca_spi_of_match,
1036 	},
1037 	.id_table = qca_spi_id,
1038 	.probe    = qca_spi_probe,
1039 	.remove   = qca_spi_remove,
1040 };
1041 module_spi_driver(qca_spi_driver);
1042 
1043 MODULE_DESCRIPTION("Qualcomm Atheros QCA7000 SPI Driver");
1044 MODULE_AUTHOR("Qualcomm Atheros Communications");
1045 MODULE_AUTHOR("Stefan Wahren <stefan.wahren@i2se.com>");
1046 MODULE_LICENSE("Dual BSD/GPL");
1047 MODULE_VERSION(QCASPI_DRV_VERSION);
1048