Linux 4.19.133
[linux/fpc-iii.git] / drivers / net / ethernet / qualcomm / qca_spi.c
blob9d188931bc09e5196d5c7182250fc8c2b559e16c
1 /*
2 * Copyright (c) 2011, 2012, Qualcomm Atheros Communications Inc.
3 * Copyright (c) 2014, I2SE GmbH
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.
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.
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;
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>
45 #include "qca_7k.h"
46 #include "qca_7k_common.h"
47 #include "qca_debug.h"
48 #include "qca_spi.h"
50 #define MAX_DMA_BURST_LEN 5000
52 /* Modules parameters */
53 #define QCASPI_CLK_SPEED_MIN 1000000
54 #define QCASPI_CLK_SPEED_MAX 16000000
55 #define QCASPI_CLK_SPEED 8000000
56 static int qcaspi_clkspeed;
57 module_param(qcaspi_clkspeed, int, 0);
58 MODULE_PARM_DESC(qcaspi_clkspeed, "SPI bus clock speed (Hz). Use 1000000-16000000.");
60 #define QCASPI_BURST_LEN_MIN 1
61 #define QCASPI_BURST_LEN_MAX MAX_DMA_BURST_LEN
62 static int qcaspi_burst_len = MAX_DMA_BURST_LEN;
63 module_param(qcaspi_burst_len, int, 0);
64 MODULE_PARM_DESC(qcaspi_burst_len, "Number of data bytes per burst. Use 1-5000.");
66 #define QCASPI_PLUGGABLE_MIN 0
67 #define QCASPI_PLUGGABLE_MAX 1
68 static int qcaspi_pluggable = QCASPI_PLUGGABLE_MIN;
69 module_param(qcaspi_pluggable, int, 0);
70 MODULE_PARM_DESC(qcaspi_pluggable, "Pluggable SPI connection (yes/no).");
72 #define QCASPI_TX_TIMEOUT (1 * HZ)
73 #define QCASPI_QCA7K_REBOOT_TIME_MS 1000
75 static void
76 start_spi_intr_handling(struct qcaspi *qca, u16 *intr_cause)
78 *intr_cause = 0;
80 qcaspi_write_register(qca, SPI_REG_INTR_ENABLE, 0);
81 qcaspi_read_register(qca, SPI_REG_INTR_CAUSE, intr_cause);
82 netdev_dbg(qca->net_dev, "interrupts: 0x%04x\n", *intr_cause);
85 static void
86 end_spi_intr_handling(struct qcaspi *qca, u16 intr_cause)
88 u16 intr_enable = (SPI_INT_CPU_ON |
89 SPI_INT_PKT_AVLBL |
90 SPI_INT_RDBUF_ERR |
91 SPI_INT_WRBUF_ERR);
93 qcaspi_write_register(qca, SPI_REG_INTR_CAUSE, intr_cause);
94 qcaspi_write_register(qca, SPI_REG_INTR_ENABLE, intr_enable);
95 netdev_dbg(qca->net_dev, "acking int: 0x%04x\n", intr_cause);
98 static u32
99 qcaspi_write_burst(struct qcaspi *qca, u8 *src, u32 len)
101 __be16 cmd;
102 struct spi_message msg;
103 struct spi_transfer transfer[2];
104 int ret;
106 memset(&transfer, 0, sizeof(transfer));
107 spi_message_init(&msg);
109 cmd = cpu_to_be16(QCA7K_SPI_WRITE | QCA7K_SPI_EXTERNAL);
110 transfer[0].tx_buf = &cmd;
111 transfer[0].len = QCASPI_CMD_LEN;
112 transfer[1].tx_buf = src;
113 transfer[1].len = len;
115 spi_message_add_tail(&transfer[0], &msg);
116 spi_message_add_tail(&transfer[1], &msg);
117 ret = spi_sync(qca->spi_dev, &msg);
119 if (ret || (msg.actual_length != QCASPI_CMD_LEN + len)) {
120 qcaspi_spi_error(qca);
121 return 0;
124 return len;
127 static u32
128 qcaspi_write_legacy(struct qcaspi *qca, u8 *src, u32 len)
130 struct spi_message msg;
131 struct spi_transfer transfer;
132 int ret;
134 memset(&transfer, 0, sizeof(transfer));
135 spi_message_init(&msg);
137 transfer.tx_buf = src;
138 transfer.len = len;
140 spi_message_add_tail(&transfer, &msg);
141 ret = spi_sync(qca->spi_dev, &msg);
143 if (ret || (msg.actual_length != len)) {
144 qcaspi_spi_error(qca);
145 return 0;
148 return len;
151 static u32
152 qcaspi_read_burst(struct qcaspi *qca, u8 *dst, u32 len)
154 struct spi_message msg;
155 __be16 cmd;
156 struct spi_transfer transfer[2];
157 int ret;
159 memset(&transfer, 0, sizeof(transfer));
160 spi_message_init(&msg);
162 cmd = cpu_to_be16(QCA7K_SPI_READ | QCA7K_SPI_EXTERNAL);
163 transfer[0].tx_buf = &cmd;
164 transfer[0].len = QCASPI_CMD_LEN;
165 transfer[1].rx_buf = dst;
166 transfer[1].len = len;
168 spi_message_add_tail(&transfer[0], &msg);
169 spi_message_add_tail(&transfer[1], &msg);
170 ret = spi_sync(qca->spi_dev, &msg);
172 if (ret || (msg.actual_length != QCASPI_CMD_LEN + len)) {
173 qcaspi_spi_error(qca);
174 return 0;
177 return len;
180 static u32
181 qcaspi_read_legacy(struct qcaspi *qca, u8 *dst, u32 len)
183 struct spi_message msg;
184 struct spi_transfer transfer;
185 int ret;
187 memset(&transfer, 0, sizeof(transfer));
188 spi_message_init(&msg);
190 transfer.rx_buf = dst;
191 transfer.len = len;
193 spi_message_add_tail(&transfer, &msg);
194 ret = spi_sync(qca->spi_dev, &msg);
196 if (ret || (msg.actual_length != len)) {
197 qcaspi_spi_error(qca);
198 return 0;
201 return len;
204 static int
205 qcaspi_tx_cmd(struct qcaspi *qca, u16 cmd)
207 __be16 tx_data;
208 struct spi_message msg;
209 struct spi_transfer transfer;
210 int ret;
212 memset(&transfer, 0, sizeof(transfer));
214 spi_message_init(&msg);
216 tx_data = cpu_to_be16(cmd);
217 transfer.len = sizeof(cmd);
218 transfer.tx_buf = &tx_data;
219 spi_message_add_tail(&transfer, &msg);
221 ret = spi_sync(qca->spi_dev, &msg);
223 if (!ret)
224 ret = msg.status;
226 if (ret)
227 qcaspi_spi_error(qca);
229 return ret;
232 static int
233 qcaspi_tx_frame(struct qcaspi *qca, struct sk_buff *skb)
235 u32 count;
236 u32 written;
237 u32 offset;
238 u32 len;
240 len = skb->len;
242 qcaspi_write_register(qca, SPI_REG_BFR_SIZE, len);
243 if (qca->legacy_mode)
244 qcaspi_tx_cmd(qca, QCA7K_SPI_WRITE | QCA7K_SPI_EXTERNAL);
246 offset = 0;
247 while (len) {
248 count = len;
249 if (count > qca->burst_len)
250 count = qca->burst_len;
252 if (qca->legacy_mode) {
253 written = qcaspi_write_legacy(qca,
254 skb->data + offset,
255 count);
256 } else {
257 written = qcaspi_write_burst(qca,
258 skb->data + offset,
259 count);
262 if (written != count)
263 return -1;
265 offset += count;
266 len -= count;
269 return 0;
272 static int
273 qcaspi_transmit(struct qcaspi *qca)
275 struct net_device_stats *n_stats = &qca->net_dev->stats;
276 u16 available = 0;
277 u32 pkt_len;
278 u16 new_head;
279 u16 packets = 0;
281 if (qca->txr.skb[qca->txr.head] == NULL)
282 return 0;
284 qcaspi_read_register(qca, SPI_REG_WRBUF_SPC_AVA, &available);
286 while (qca->txr.skb[qca->txr.head]) {
287 pkt_len = qca->txr.skb[qca->txr.head]->len + QCASPI_HW_PKT_LEN;
289 if (available < pkt_len) {
290 if (packets == 0)
291 qca->stats.write_buf_miss++;
292 break;
295 if (qcaspi_tx_frame(qca, qca->txr.skb[qca->txr.head]) == -1) {
296 qca->stats.write_err++;
297 return -1;
300 packets++;
301 n_stats->tx_packets++;
302 n_stats->tx_bytes += qca->txr.skb[qca->txr.head]->len;
303 available -= pkt_len;
305 /* remove the skb from the queue */
306 /* XXX After inconsistent lock states netif_tx_lock()
307 * has been replaced by netif_tx_lock_bh() and so on.
309 netif_tx_lock_bh(qca->net_dev);
310 dev_kfree_skb(qca->txr.skb[qca->txr.head]);
311 qca->txr.skb[qca->txr.head] = NULL;
312 qca->txr.size -= pkt_len;
313 new_head = qca->txr.head + 1;
314 if (new_head >= qca->txr.count)
315 new_head = 0;
316 qca->txr.head = new_head;
317 if (netif_queue_stopped(qca->net_dev))
318 netif_wake_queue(qca->net_dev);
319 netif_tx_unlock_bh(qca->net_dev);
322 return 0;
325 static int
326 qcaspi_receive(struct qcaspi *qca)
328 struct net_device *net_dev = qca->net_dev;
329 struct net_device_stats *n_stats = &net_dev->stats;
330 u16 available = 0;
331 u32 bytes_read;
332 u8 *cp;
334 /* Allocate rx SKB if we don't have one available. */
335 if (!qca->rx_skb) {
336 qca->rx_skb = netdev_alloc_skb_ip_align(net_dev,
337 net_dev->mtu +
338 VLAN_ETH_HLEN);
339 if (!qca->rx_skb) {
340 netdev_dbg(net_dev, "out of RX resources\n");
341 qca->stats.out_of_mem++;
342 return -1;
346 /* Read the packet size. */
347 qcaspi_read_register(qca, SPI_REG_RDBUF_BYTE_AVA, &available);
348 netdev_dbg(net_dev, "qcaspi_receive: SPI_REG_RDBUF_BYTE_AVA: Value: %08x\n",
349 available);
351 if (available == 0) {
352 netdev_dbg(net_dev, "qcaspi_receive called without any data being available!\n");
353 return -1;
356 qcaspi_write_register(qca, SPI_REG_BFR_SIZE, available);
358 if (qca->legacy_mode)
359 qcaspi_tx_cmd(qca, QCA7K_SPI_READ | QCA7K_SPI_EXTERNAL);
361 while (available) {
362 u32 count = available;
364 if (count > qca->burst_len)
365 count = qca->burst_len;
367 if (qca->legacy_mode) {
368 bytes_read = qcaspi_read_legacy(qca, qca->rx_buffer,
369 count);
370 } else {
371 bytes_read = qcaspi_read_burst(qca, qca->rx_buffer,
372 count);
375 netdev_dbg(net_dev, "available: %d, byte read: %d\n",
376 available, bytes_read);
378 if (bytes_read) {
379 available -= bytes_read;
380 } else {
381 qca->stats.read_err++;
382 return -1;
385 cp = qca->rx_buffer;
387 while ((bytes_read--) && (qca->rx_skb)) {
388 s32 retcode;
390 retcode = qcafrm_fsm_decode(&qca->frm_handle,
391 qca->rx_skb->data,
392 skb_tailroom(qca->rx_skb),
393 *cp);
394 cp++;
395 switch (retcode) {
396 case QCAFRM_GATHER:
397 case QCAFRM_NOHEAD:
398 break;
399 case QCAFRM_NOTAIL:
400 netdev_dbg(net_dev, "no RX tail\n");
401 n_stats->rx_errors++;
402 n_stats->rx_dropped++;
403 break;
404 case QCAFRM_INVLEN:
405 netdev_dbg(net_dev, "invalid RX length\n");
406 n_stats->rx_errors++;
407 n_stats->rx_dropped++;
408 break;
409 default:
410 qca->rx_skb->dev = qca->net_dev;
411 n_stats->rx_packets++;
412 n_stats->rx_bytes += retcode;
413 skb_put(qca->rx_skb, retcode);
414 qca->rx_skb->protocol = eth_type_trans(
415 qca->rx_skb, qca->rx_skb->dev);
416 qca->rx_skb->ip_summed = CHECKSUM_UNNECESSARY;
417 netif_rx_ni(qca->rx_skb);
418 qca->rx_skb = netdev_alloc_skb_ip_align(net_dev,
419 net_dev->mtu + VLAN_ETH_HLEN);
420 if (!qca->rx_skb) {
421 netdev_dbg(net_dev, "out of RX resources\n");
422 n_stats->rx_errors++;
423 qca->stats.out_of_mem++;
424 break;
430 return 0;
433 /* Check that tx ring stores only so much bytes
434 * that fit into the internal QCA buffer.
437 static int
438 qcaspi_tx_ring_has_space(struct tx_ring *txr)
440 if (txr->skb[txr->tail])
441 return 0;
443 return (txr->size + QCAFRM_MAX_LEN < QCASPI_HW_BUF_LEN) ? 1 : 0;
446 /* Flush the tx ring. This function is only safe to
447 * call from the qcaspi_spi_thread.
450 static void
451 qcaspi_flush_tx_ring(struct qcaspi *qca)
453 int i;
455 /* XXX After inconsistent lock states netif_tx_lock()
456 * has been replaced by netif_tx_lock_bh() and so on.
458 netif_tx_lock_bh(qca->net_dev);
459 for (i = 0; i < TX_RING_MAX_LEN; i++) {
460 if (qca->txr.skb[i]) {
461 dev_kfree_skb(qca->txr.skb[i]);
462 qca->txr.skb[i] = NULL;
463 qca->net_dev->stats.tx_dropped++;
466 qca->txr.tail = 0;
467 qca->txr.head = 0;
468 qca->txr.size = 0;
469 netif_tx_unlock_bh(qca->net_dev);
472 static void
473 qcaspi_qca7k_sync(struct qcaspi *qca, int event)
475 u16 signature = 0;
476 u16 spi_config;
477 u16 wrbuf_space = 0;
479 if (event == QCASPI_EVENT_CPUON) {
480 /* Read signature twice, if not valid
481 * go back to unknown state.
483 qcaspi_read_register(qca, SPI_REG_SIGNATURE, &signature);
484 qcaspi_read_register(qca, SPI_REG_SIGNATURE, &signature);
485 if (signature != QCASPI_GOOD_SIGNATURE) {
486 qca->sync = QCASPI_SYNC_UNKNOWN;
487 netdev_dbg(qca->net_dev, "sync: got CPU on, but signature was invalid, restart\n");
488 } else {
489 /* ensure that the WRBUF is empty */
490 qcaspi_read_register(qca, SPI_REG_WRBUF_SPC_AVA,
491 &wrbuf_space);
492 if (wrbuf_space != QCASPI_HW_BUF_LEN) {
493 netdev_dbg(qca->net_dev, "sync: got CPU on, but wrbuf not empty. reset!\n");
494 qca->sync = QCASPI_SYNC_UNKNOWN;
495 } else {
496 netdev_dbg(qca->net_dev, "sync: got CPU on, now in sync\n");
497 qca->sync = QCASPI_SYNC_READY;
498 return;
503 switch (qca->sync) {
504 case QCASPI_SYNC_READY:
505 /* Read signature, if not valid go to unknown state. */
506 qcaspi_read_register(qca, SPI_REG_SIGNATURE, &signature);
507 if (signature != QCASPI_GOOD_SIGNATURE) {
508 qca->sync = QCASPI_SYNC_UNKNOWN;
509 netdev_dbg(qca->net_dev, "sync: bad signature, restart\n");
510 /* don't reset right away */
511 return;
513 break;
514 case QCASPI_SYNC_UNKNOWN:
515 /* Read signature, if not valid stay in unknown state */
516 qcaspi_read_register(qca, SPI_REG_SIGNATURE, &signature);
517 if (signature != QCASPI_GOOD_SIGNATURE) {
518 netdev_dbg(qca->net_dev, "sync: could not read signature to reset device, retry.\n");
519 return;
522 /* TODO: use GPIO to reset QCA7000 in legacy mode*/
523 netdev_dbg(qca->net_dev, "sync: resetting device.\n");
524 qcaspi_read_register(qca, SPI_REG_SPI_CONFIG, &spi_config);
525 spi_config |= QCASPI_SLAVE_RESET_BIT;
526 qcaspi_write_register(qca, SPI_REG_SPI_CONFIG, spi_config);
528 qca->sync = QCASPI_SYNC_RESET;
529 qca->stats.trig_reset++;
530 qca->reset_count = 0;
531 break;
532 case QCASPI_SYNC_RESET:
533 qca->reset_count++;
534 netdev_dbg(qca->net_dev, "sync: waiting for CPU on, count %u.\n",
535 qca->reset_count);
536 if (qca->reset_count >= QCASPI_RESET_TIMEOUT) {
537 /* reset did not seem to take place, try again */
538 qca->sync = QCASPI_SYNC_UNKNOWN;
539 qca->stats.reset_timeout++;
540 netdev_dbg(qca->net_dev, "sync: reset timeout, restarting process.\n");
542 break;
546 static int
547 qcaspi_spi_thread(void *data)
549 struct qcaspi *qca = data;
550 u16 intr_cause = 0;
552 netdev_info(qca->net_dev, "SPI thread created\n");
553 while (!kthread_should_stop()) {
554 set_current_state(TASK_INTERRUPTIBLE);
555 if ((qca->intr_req == qca->intr_svc) &&
556 (qca->txr.skb[qca->txr.head] == NULL) &&
557 (qca->sync == QCASPI_SYNC_READY))
558 schedule();
560 set_current_state(TASK_RUNNING);
562 netdev_dbg(qca->net_dev, "have work to do. int: %d, tx_skb: %p\n",
563 qca->intr_req - qca->intr_svc,
564 qca->txr.skb[qca->txr.head]);
566 qcaspi_qca7k_sync(qca, QCASPI_EVENT_UPDATE);
568 if (qca->sync != QCASPI_SYNC_READY) {
569 netdev_dbg(qca->net_dev, "sync: not ready %u, turn off carrier and flush\n",
570 (unsigned int)qca->sync);
571 netif_stop_queue(qca->net_dev);
572 netif_carrier_off(qca->net_dev);
573 qcaspi_flush_tx_ring(qca);
574 msleep(QCASPI_QCA7K_REBOOT_TIME_MS);
577 if (qca->intr_svc != qca->intr_req) {
578 qca->intr_svc = qca->intr_req;
579 start_spi_intr_handling(qca, &intr_cause);
581 if (intr_cause & SPI_INT_CPU_ON) {
582 qcaspi_qca7k_sync(qca, QCASPI_EVENT_CPUON);
584 /* not synced. */
585 if (qca->sync != QCASPI_SYNC_READY)
586 continue;
588 qca->stats.device_reset++;
589 netif_wake_queue(qca->net_dev);
590 netif_carrier_on(qca->net_dev);
593 if (intr_cause & SPI_INT_RDBUF_ERR) {
594 /* restart sync */
595 netdev_dbg(qca->net_dev, "===> rdbuf error!\n");
596 qca->stats.read_buf_err++;
597 qca->sync = QCASPI_SYNC_UNKNOWN;
598 continue;
601 if (intr_cause & SPI_INT_WRBUF_ERR) {
602 /* restart sync */
603 netdev_dbg(qca->net_dev, "===> wrbuf error!\n");
604 qca->stats.write_buf_err++;
605 qca->sync = QCASPI_SYNC_UNKNOWN;
606 continue;
609 /* can only handle other interrupts
610 * if sync has occurred
612 if (qca->sync == QCASPI_SYNC_READY) {
613 if (intr_cause & SPI_INT_PKT_AVLBL)
614 qcaspi_receive(qca);
617 end_spi_intr_handling(qca, intr_cause);
620 if (qca->sync == QCASPI_SYNC_READY)
621 qcaspi_transmit(qca);
623 set_current_state(TASK_RUNNING);
624 netdev_info(qca->net_dev, "SPI thread exit\n");
626 return 0;
629 static irqreturn_t
630 qcaspi_intr_handler(int irq, void *data)
632 struct qcaspi *qca = data;
634 qca->intr_req++;
635 if (qca->spi_thread &&
636 qca->spi_thread->state != TASK_RUNNING)
637 wake_up_process(qca->spi_thread);
639 return IRQ_HANDLED;
642 static int
643 qcaspi_netdev_open(struct net_device *dev)
645 struct qcaspi *qca = netdev_priv(dev);
646 int ret = 0;
648 if (!qca)
649 return -EINVAL;
651 qca->intr_req = 1;
652 qca->intr_svc = 0;
653 qca->sync = QCASPI_SYNC_UNKNOWN;
654 qcafrm_fsm_init_spi(&qca->frm_handle);
656 qca->spi_thread = kthread_run((void *)qcaspi_spi_thread,
657 qca, "%s", dev->name);
659 if (IS_ERR(qca->spi_thread)) {
660 netdev_err(dev, "%s: unable to start kernel thread.\n",
661 QCASPI_DRV_NAME);
662 return PTR_ERR(qca->spi_thread);
665 ret = request_irq(qca->spi_dev->irq, qcaspi_intr_handler, 0,
666 dev->name, qca);
667 if (ret) {
668 netdev_err(dev, "%s: unable to get IRQ %d (irqval=%d).\n",
669 QCASPI_DRV_NAME, qca->spi_dev->irq, ret);
670 kthread_stop(qca->spi_thread);
671 return ret;
674 /* SPI thread takes care of TX queue */
676 return 0;
679 static int
680 qcaspi_netdev_close(struct net_device *dev)
682 struct qcaspi *qca = netdev_priv(dev);
684 netif_stop_queue(dev);
686 qcaspi_write_register(qca, SPI_REG_INTR_ENABLE, 0);
687 free_irq(qca->spi_dev->irq, qca);
689 kthread_stop(qca->spi_thread);
690 qca->spi_thread = NULL;
691 qcaspi_flush_tx_ring(qca);
693 return 0;
696 static netdev_tx_t
697 qcaspi_netdev_xmit(struct sk_buff *skb, struct net_device *dev)
699 u32 frame_len;
700 u8 *ptmp;
701 struct qcaspi *qca = netdev_priv(dev);
702 u16 new_tail;
703 struct sk_buff *tskb;
704 u8 pad_len = 0;
706 if (skb->len < QCAFRM_MIN_LEN)
707 pad_len = QCAFRM_MIN_LEN - skb->len;
709 if (qca->txr.skb[qca->txr.tail]) {
710 netdev_warn(qca->net_dev, "queue was unexpectedly full!\n");
711 netif_stop_queue(qca->net_dev);
712 qca->stats.ring_full++;
713 return NETDEV_TX_BUSY;
716 if ((skb_headroom(skb) < QCAFRM_HEADER_LEN) ||
717 (skb_tailroom(skb) < QCAFRM_FOOTER_LEN + pad_len)) {
718 tskb = skb_copy_expand(skb, QCAFRM_HEADER_LEN,
719 QCAFRM_FOOTER_LEN + pad_len, GFP_ATOMIC);
720 if (!tskb) {
721 qca->stats.out_of_mem++;
722 return NETDEV_TX_BUSY;
724 dev_kfree_skb(skb);
725 skb = tskb;
728 frame_len = skb->len + pad_len;
730 ptmp = skb_push(skb, QCAFRM_HEADER_LEN);
731 qcafrm_create_header(ptmp, frame_len);
733 if (pad_len) {
734 ptmp = skb_put_zero(skb, pad_len);
737 ptmp = skb_put(skb, QCAFRM_FOOTER_LEN);
738 qcafrm_create_footer(ptmp);
740 netdev_dbg(qca->net_dev, "Tx-ing packet: Size: 0x%08x\n",
741 skb->len);
743 qca->txr.size += skb->len + QCASPI_HW_PKT_LEN;
745 new_tail = qca->txr.tail + 1;
746 if (new_tail >= qca->txr.count)
747 new_tail = 0;
749 qca->txr.skb[qca->txr.tail] = skb;
750 qca->txr.tail = new_tail;
752 if (!qcaspi_tx_ring_has_space(&qca->txr)) {
753 netif_stop_queue(qca->net_dev);
754 qca->stats.ring_full++;
757 netif_trans_update(dev);
759 if (qca->spi_thread &&
760 qca->spi_thread->state != TASK_RUNNING)
761 wake_up_process(qca->spi_thread);
763 return NETDEV_TX_OK;
766 static void
767 qcaspi_netdev_tx_timeout(struct net_device *dev)
769 struct qcaspi *qca = netdev_priv(dev);
771 netdev_info(qca->net_dev, "Transmit timeout at %ld, latency %ld\n",
772 jiffies, jiffies - dev_trans_start(dev));
773 qca->net_dev->stats.tx_errors++;
774 /* Trigger tx queue flush and QCA7000 reset */
775 qca->sync = QCASPI_SYNC_UNKNOWN;
777 if (qca->spi_thread)
778 wake_up_process(qca->spi_thread);
781 static int
782 qcaspi_netdev_init(struct net_device *dev)
784 struct qcaspi *qca = netdev_priv(dev);
786 dev->mtu = QCAFRM_MAX_MTU;
787 dev->type = ARPHRD_ETHER;
788 qca->clkspeed = qcaspi_clkspeed;
789 qca->burst_len = qcaspi_burst_len;
790 qca->spi_thread = NULL;
791 qca->buffer_size = (dev->mtu + VLAN_ETH_HLEN + QCAFRM_HEADER_LEN +
792 QCAFRM_FOOTER_LEN + 4) * 4;
794 memset(&qca->stats, 0, sizeof(struct qcaspi_stats));
796 qca->rx_buffer = kmalloc(qca->buffer_size, GFP_KERNEL);
797 if (!qca->rx_buffer)
798 return -ENOBUFS;
800 qca->rx_skb = netdev_alloc_skb_ip_align(dev, qca->net_dev->mtu +
801 VLAN_ETH_HLEN);
802 if (!qca->rx_skb) {
803 kfree(qca->rx_buffer);
804 netdev_info(qca->net_dev, "Failed to allocate RX sk_buff.\n");
805 return -ENOBUFS;
808 return 0;
811 static void
812 qcaspi_netdev_uninit(struct net_device *dev)
814 struct qcaspi *qca = netdev_priv(dev);
816 kfree(qca->rx_buffer);
817 qca->buffer_size = 0;
818 if (qca->rx_skb)
819 dev_kfree_skb(qca->rx_skb);
822 static const struct net_device_ops qcaspi_netdev_ops = {
823 .ndo_init = qcaspi_netdev_init,
824 .ndo_uninit = qcaspi_netdev_uninit,
825 .ndo_open = qcaspi_netdev_open,
826 .ndo_stop = qcaspi_netdev_close,
827 .ndo_start_xmit = qcaspi_netdev_xmit,
828 .ndo_set_mac_address = eth_mac_addr,
829 .ndo_tx_timeout = qcaspi_netdev_tx_timeout,
830 .ndo_validate_addr = eth_validate_addr,
833 static void
834 qcaspi_netdev_setup(struct net_device *dev)
836 struct qcaspi *qca = NULL;
838 dev->netdev_ops = &qcaspi_netdev_ops;
839 qcaspi_set_ethtool_ops(dev);
840 dev->watchdog_timeo = QCASPI_TX_TIMEOUT;
841 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
842 dev->tx_queue_len = 100;
844 /* MTU range: 46 - 1500 */
845 dev->min_mtu = QCAFRM_MIN_MTU;
846 dev->max_mtu = QCAFRM_MAX_MTU;
848 qca = netdev_priv(dev);
849 memset(qca, 0, sizeof(struct qcaspi));
851 memset(&qca->txr, 0, sizeof(qca->txr));
852 qca->txr.count = TX_RING_MAX_LEN;
855 static const struct of_device_id qca_spi_of_match[] = {
856 { .compatible = "qca,qca7000" },
857 { /* sentinel */ }
859 MODULE_DEVICE_TABLE(of, qca_spi_of_match);
861 static int
862 qca_spi_probe(struct spi_device *spi)
864 struct qcaspi *qca = NULL;
865 struct net_device *qcaspi_devs = NULL;
866 u8 legacy_mode = 0;
867 u16 signature;
868 const char *mac;
870 if (!spi->dev.of_node) {
871 dev_err(&spi->dev, "Missing device tree\n");
872 return -EINVAL;
875 legacy_mode = of_property_read_bool(spi->dev.of_node,
876 "qca,legacy-mode");
878 if (qcaspi_clkspeed == 0) {
879 if (spi->max_speed_hz)
880 qcaspi_clkspeed = spi->max_speed_hz;
881 else
882 qcaspi_clkspeed = QCASPI_CLK_SPEED;
885 if ((qcaspi_clkspeed < QCASPI_CLK_SPEED_MIN) ||
886 (qcaspi_clkspeed > QCASPI_CLK_SPEED_MAX)) {
887 dev_err(&spi->dev, "Invalid clkspeed: %d\n",
888 qcaspi_clkspeed);
889 return -EINVAL;
892 if ((qcaspi_burst_len < QCASPI_BURST_LEN_MIN) ||
893 (qcaspi_burst_len > QCASPI_BURST_LEN_MAX)) {
894 dev_err(&spi->dev, "Invalid burst len: %d\n",
895 qcaspi_burst_len);
896 return -EINVAL;
899 if ((qcaspi_pluggable < QCASPI_PLUGGABLE_MIN) ||
900 (qcaspi_pluggable > QCASPI_PLUGGABLE_MAX)) {
901 dev_err(&spi->dev, "Invalid pluggable: %d\n",
902 qcaspi_pluggable);
903 return -EINVAL;
906 dev_info(&spi->dev, "ver=%s, clkspeed=%d, burst_len=%d, pluggable=%d\n",
907 QCASPI_DRV_VERSION,
908 qcaspi_clkspeed,
909 qcaspi_burst_len,
910 qcaspi_pluggable);
912 spi->mode = SPI_MODE_3;
913 spi->max_speed_hz = qcaspi_clkspeed;
914 if (spi_setup(spi) < 0) {
915 dev_err(&spi->dev, "Unable to setup SPI device\n");
916 return -EFAULT;
919 qcaspi_devs = alloc_etherdev(sizeof(struct qcaspi));
920 if (!qcaspi_devs)
921 return -ENOMEM;
923 qcaspi_netdev_setup(qcaspi_devs);
924 SET_NETDEV_DEV(qcaspi_devs, &spi->dev);
926 qca = netdev_priv(qcaspi_devs);
927 if (!qca) {
928 free_netdev(qcaspi_devs);
929 dev_err(&spi->dev, "Fail to retrieve private structure\n");
930 return -ENOMEM;
932 qca->net_dev = qcaspi_devs;
933 qca->spi_dev = spi;
934 qca->legacy_mode = legacy_mode;
936 spi_set_drvdata(spi, qcaspi_devs);
938 mac = of_get_mac_address(spi->dev.of_node);
940 if (mac)
941 ether_addr_copy(qca->net_dev->dev_addr, mac);
943 if (!is_valid_ether_addr(qca->net_dev->dev_addr)) {
944 eth_hw_addr_random(qca->net_dev);
945 dev_info(&spi->dev, "Using random MAC address: %pM\n",
946 qca->net_dev->dev_addr);
949 netif_carrier_off(qca->net_dev);
951 if (!qcaspi_pluggable) {
952 qcaspi_read_register(qca, SPI_REG_SIGNATURE, &signature);
953 qcaspi_read_register(qca, SPI_REG_SIGNATURE, &signature);
955 if (signature != QCASPI_GOOD_SIGNATURE) {
956 dev_err(&spi->dev, "Invalid signature (0x%04X)\n",
957 signature);
958 free_netdev(qcaspi_devs);
959 return -EFAULT;
963 if (register_netdev(qcaspi_devs)) {
964 dev_err(&spi->dev, "Unable to register net device %s\n",
965 qcaspi_devs->name);
966 free_netdev(qcaspi_devs);
967 return -EFAULT;
970 qcaspi_init_device_debugfs(qca);
972 return 0;
975 static int
976 qca_spi_remove(struct spi_device *spi)
978 struct net_device *qcaspi_devs = spi_get_drvdata(spi);
979 struct qcaspi *qca = netdev_priv(qcaspi_devs);
981 qcaspi_remove_device_debugfs(qca);
983 unregister_netdev(qcaspi_devs);
984 free_netdev(qcaspi_devs);
986 return 0;
989 static const struct spi_device_id qca_spi_id[] = {
990 { "qca7000", 0 },
991 { /* sentinel */ }
993 MODULE_DEVICE_TABLE(spi, qca_spi_id);
995 static struct spi_driver qca_spi_driver = {
996 .driver = {
997 .name = QCASPI_DRV_NAME,
998 .of_match_table = qca_spi_of_match,
1000 .id_table = qca_spi_id,
1001 .probe = qca_spi_probe,
1002 .remove = qca_spi_remove,
1004 module_spi_driver(qca_spi_driver);
1006 MODULE_DESCRIPTION("Qualcomm Atheros QCA7000 SPI Driver");
1007 MODULE_AUTHOR("Qualcomm Atheros Communications");
1008 MODULE_AUTHOR("Stefan Wahren <stefan.wahren@i2se.com>");
1009 MODULE_LICENSE("Dual BSD/GPL");
1010 MODULE_VERSION(QCASPI_DRV_VERSION);