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