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
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
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>
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>
46 #include "qca_7k_common.h"
47 #include "qca_debug.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_WRITE_VERIFY_MIN 0
73 #define QCASPI_WRITE_VERIFY_MAX 3
74 static int wr_verify = QCASPI_WRITE_VERIFY_MIN;
75 module_param(wr_verify, int, 0);
76 MODULE_PARM_DESC(wr_verify, "SPI register write verify trails. Use 0-3.");
78 #define QCASPI_TX_TIMEOUT (1 * HZ)
79 #define QCASPI_QCA7K_REBOOT_TIME_MS 1000
82 start_spi_intr_handling(struct qcaspi *qca, u16 *intr_cause)
86 qcaspi_write_register(qca, SPI_REG_INTR_ENABLE, 0, wr_verify);
87 qcaspi_read_register(qca, SPI_REG_INTR_CAUSE, intr_cause);
88 netdev_dbg(qca->net_dev, "interrupts: 0x%04x\n", *intr_cause);
92 end_spi_intr_handling(struct qcaspi *qca, u16 intr_cause)
94 u16 intr_enable = (SPI_INT_CPU_ON |
99 qcaspi_write_register(qca, SPI_REG_INTR_CAUSE, intr_cause, 0);
100 qcaspi_write_register(qca, SPI_REG_INTR_ENABLE, intr_enable, wr_verify);
101 netdev_dbg(qca->net_dev, "acking int: 0x%04x\n", intr_cause);
105 qcaspi_write_burst(struct qcaspi *qca, u8 *src, u32 len)
108 struct spi_message msg;
109 struct spi_transfer transfer[2];
112 memset(&transfer, 0, sizeof(transfer));
113 spi_message_init(&msg);
115 cmd = cpu_to_be16(QCA7K_SPI_WRITE | QCA7K_SPI_EXTERNAL);
116 transfer[0].tx_buf = &cmd;
117 transfer[0].len = QCASPI_CMD_LEN;
118 transfer[1].tx_buf = src;
119 transfer[1].len = len;
121 spi_message_add_tail(&transfer[0], &msg);
122 spi_message_add_tail(&transfer[1], &msg);
123 ret = spi_sync(qca->spi_dev, &msg);
125 if (ret || (msg.actual_length != QCASPI_CMD_LEN + len)) {
126 qcaspi_spi_error(qca);
134 qcaspi_write_legacy(struct qcaspi *qca, u8 *src, u32 len)
136 struct spi_message msg;
137 struct spi_transfer transfer;
140 memset(&transfer, 0, sizeof(transfer));
141 spi_message_init(&msg);
143 transfer.tx_buf = src;
146 spi_message_add_tail(&transfer, &msg);
147 ret = spi_sync(qca->spi_dev, &msg);
149 if (ret || (msg.actual_length != len)) {
150 qcaspi_spi_error(qca);
158 qcaspi_read_burst(struct qcaspi *qca, u8 *dst, u32 len)
160 struct spi_message msg;
162 struct spi_transfer transfer[2];
165 memset(&transfer, 0, sizeof(transfer));
166 spi_message_init(&msg);
168 cmd = cpu_to_be16(QCA7K_SPI_READ | QCA7K_SPI_EXTERNAL);
169 transfer[0].tx_buf = &cmd;
170 transfer[0].len = QCASPI_CMD_LEN;
171 transfer[1].rx_buf = dst;
172 transfer[1].len = len;
174 spi_message_add_tail(&transfer[0], &msg);
175 spi_message_add_tail(&transfer[1], &msg);
176 ret = spi_sync(qca->spi_dev, &msg);
178 if (ret || (msg.actual_length != QCASPI_CMD_LEN + len)) {
179 qcaspi_spi_error(qca);
187 qcaspi_read_legacy(struct qcaspi *qca, u8 *dst, u32 len)
189 struct spi_message msg;
190 struct spi_transfer transfer;
193 memset(&transfer, 0, sizeof(transfer));
194 spi_message_init(&msg);
196 transfer.rx_buf = dst;
199 spi_message_add_tail(&transfer, &msg);
200 ret = spi_sync(qca->spi_dev, &msg);
202 if (ret || (msg.actual_length != len)) {
203 qcaspi_spi_error(qca);
211 qcaspi_tx_cmd(struct qcaspi *qca, u16 cmd)
214 struct spi_message msg;
215 struct spi_transfer transfer;
218 memset(&transfer, 0, sizeof(transfer));
220 spi_message_init(&msg);
222 tx_data = cpu_to_be16(cmd);
223 transfer.len = sizeof(cmd);
224 transfer.tx_buf = &tx_data;
225 spi_message_add_tail(&transfer, &msg);
227 ret = spi_sync(qca->spi_dev, &msg);
233 qcaspi_spi_error(qca);
239 qcaspi_tx_frame(struct qcaspi *qca, struct sk_buff *skb)
248 qcaspi_write_register(qca, SPI_REG_BFR_SIZE, len, wr_verify);
249 if (qca->legacy_mode)
250 qcaspi_tx_cmd(qca, QCA7K_SPI_WRITE | QCA7K_SPI_EXTERNAL);
255 if (count > qca->burst_len)
256 count = qca->burst_len;
258 if (qca->legacy_mode) {
259 written = qcaspi_write_legacy(qca,
263 written = qcaspi_write_burst(qca,
268 if (written != count)
279 qcaspi_transmit(struct qcaspi *qca)
281 struct net_device_stats *n_stats = &qca->net_dev->stats;
287 if (qca->txr.skb[qca->txr.head] == NULL)
290 qcaspi_read_register(qca, SPI_REG_WRBUF_SPC_AVA, &available);
292 if (available > QCASPI_HW_BUF_LEN) {
293 /* This could only happen by interferences on the SPI line.
296 qca->stats.buf_avail_err++;
300 while (qca->txr.skb[qca->txr.head]) {
301 pkt_len = qca->txr.skb[qca->txr.head]->len + QCASPI_HW_PKT_LEN;
303 if (available < pkt_len) {
305 qca->stats.write_buf_miss++;
309 if (qcaspi_tx_frame(qca, qca->txr.skb[qca->txr.head]) == -1) {
310 qca->stats.write_err++;
315 n_stats->tx_packets++;
316 n_stats->tx_bytes += qca->txr.skb[qca->txr.head]->len;
317 available -= pkt_len;
319 /* remove the skb from the queue */
320 /* XXX After inconsistent lock states netif_tx_lock()
321 * has been replaced by netif_tx_lock_bh() and so on.
323 netif_tx_lock_bh(qca->net_dev);
324 dev_kfree_skb(qca->txr.skb[qca->txr.head]);
325 qca->txr.skb[qca->txr.head] = NULL;
326 qca->txr.size -= pkt_len;
327 new_head = qca->txr.head + 1;
328 if (new_head >= qca->txr.count)
330 qca->txr.head = new_head;
331 if (netif_queue_stopped(qca->net_dev))
332 netif_wake_queue(qca->net_dev);
333 netif_tx_unlock_bh(qca->net_dev);
340 qcaspi_receive(struct qcaspi *qca)
342 struct net_device *net_dev = qca->net_dev;
343 struct net_device_stats *n_stats = &net_dev->stats;
348 /* Allocate rx SKB if we don't have one available. */
350 qca->rx_skb = netdev_alloc_skb_ip_align(net_dev,
354 netdev_dbg(net_dev, "out of RX resources\n");
355 qca->stats.out_of_mem++;
360 /* Read the packet size. */
361 qcaspi_read_register(qca, SPI_REG_RDBUF_BYTE_AVA, &available);
363 netdev_dbg(net_dev, "qcaspi_receive: SPI_REG_RDBUF_BYTE_AVA: Value: %08x\n",
366 if (available > QCASPI_HW_BUF_LEN + QCASPI_HW_PKT_LEN) {
367 /* This could only happen by interferences on the SPI line.
370 qca->stats.buf_avail_err++;
372 } else if (available == 0) {
373 netdev_dbg(net_dev, "qcaspi_receive called without any data being available!\n");
377 qcaspi_write_register(qca, SPI_REG_BFR_SIZE, available, wr_verify);
379 if (qca->legacy_mode)
380 qcaspi_tx_cmd(qca, QCA7K_SPI_READ | QCA7K_SPI_EXTERNAL);
383 u32 count = available;
385 if (count > qca->burst_len)
386 count = qca->burst_len;
388 if (qca->legacy_mode) {
389 bytes_read = qcaspi_read_legacy(qca, qca->rx_buffer,
392 bytes_read = qcaspi_read_burst(qca, qca->rx_buffer,
396 netdev_dbg(net_dev, "available: %d, byte read: %d\n",
397 available, bytes_read);
400 available -= bytes_read;
402 qca->stats.read_err++;
408 while ((bytes_read--) && (qca->rx_skb)) {
411 retcode = qcafrm_fsm_decode(&qca->frm_handle,
413 skb_tailroom(qca->rx_skb),
421 netdev_dbg(net_dev, "no RX tail\n");
422 n_stats->rx_errors++;
423 n_stats->rx_dropped++;
426 netdev_dbg(net_dev, "invalid RX length\n");
427 n_stats->rx_errors++;
428 n_stats->rx_dropped++;
431 qca->rx_skb->dev = qca->net_dev;
432 n_stats->rx_packets++;
433 n_stats->rx_bytes += retcode;
434 skb_put(qca->rx_skb, retcode);
435 qca->rx_skb->protocol = eth_type_trans(
436 qca->rx_skb, qca->rx_skb->dev);
437 skb_checksum_none_assert(qca->rx_skb);
438 netif_rx_ni(qca->rx_skb);
439 qca->rx_skb = netdev_alloc_skb_ip_align(net_dev,
440 net_dev->mtu + VLAN_ETH_HLEN);
442 netdev_dbg(net_dev, "out of RX resources\n");
443 n_stats->rx_errors++;
444 qca->stats.out_of_mem++;
454 /* Check that tx ring stores only so much bytes
455 * that fit into the internal QCA buffer.
459 qcaspi_tx_ring_has_space(struct tx_ring *txr)
461 if (txr->skb[txr->tail])
464 return (txr->size + QCAFRM_MAX_LEN < QCASPI_HW_BUF_LEN) ? 1 : 0;
467 /* Flush the tx ring. This function is only safe to
468 * call from the qcaspi_spi_thread.
472 qcaspi_flush_tx_ring(struct qcaspi *qca)
476 /* XXX After inconsistent lock states netif_tx_lock()
477 * has been replaced by netif_tx_lock_bh() and so on.
479 netif_tx_lock_bh(qca->net_dev);
480 for (i = 0; i < TX_RING_MAX_LEN; i++) {
481 if (qca->txr.skb[i]) {
482 dev_kfree_skb(qca->txr.skb[i]);
483 qca->txr.skb[i] = NULL;
484 qca->net_dev->stats.tx_dropped++;
490 netif_tx_unlock_bh(qca->net_dev);
494 qcaspi_qca7k_sync(struct qcaspi *qca, int event)
500 if (event == QCASPI_EVENT_CPUON) {
501 /* Read signature twice, if not valid
502 * go back to unknown state.
504 qcaspi_read_register(qca, SPI_REG_SIGNATURE, &signature);
505 qcaspi_read_register(qca, SPI_REG_SIGNATURE, &signature);
506 if (signature != QCASPI_GOOD_SIGNATURE) {
507 qca->sync = QCASPI_SYNC_UNKNOWN;
508 netdev_dbg(qca->net_dev, "sync: got CPU on, but signature was invalid, restart\n");
510 /* ensure that the WRBUF is empty */
511 qcaspi_read_register(qca, SPI_REG_WRBUF_SPC_AVA,
513 if (wrbuf_space != QCASPI_HW_BUF_LEN) {
514 netdev_dbg(qca->net_dev, "sync: got CPU on, but wrbuf not empty. reset!\n");
515 qca->sync = QCASPI_SYNC_UNKNOWN;
517 netdev_dbg(qca->net_dev, "sync: got CPU on, now in sync\n");
518 qca->sync = QCASPI_SYNC_READY;
525 case QCASPI_SYNC_READY:
526 /* Read signature, if not valid go to unknown state. */
527 qcaspi_read_register(qca, SPI_REG_SIGNATURE, &signature);
528 if (signature != QCASPI_GOOD_SIGNATURE) {
529 qca->sync = QCASPI_SYNC_UNKNOWN;
530 netdev_dbg(qca->net_dev, "sync: bad signature, restart\n");
531 /* don't reset right away */
535 case QCASPI_SYNC_UNKNOWN:
536 /* Read signature, if not valid stay in unknown state */
537 qcaspi_read_register(qca, SPI_REG_SIGNATURE, &signature);
538 if (signature != QCASPI_GOOD_SIGNATURE) {
539 netdev_dbg(qca->net_dev, "sync: could not read signature to reset device, retry.\n");
543 /* TODO: use GPIO to reset QCA7000 in legacy mode*/
544 netdev_dbg(qca->net_dev, "sync: resetting device.\n");
545 qcaspi_read_register(qca, SPI_REG_SPI_CONFIG, &spi_config);
546 spi_config |= QCASPI_SLAVE_RESET_BIT;
547 qcaspi_write_register(qca, SPI_REG_SPI_CONFIG, spi_config, 0);
549 qca->sync = QCASPI_SYNC_RESET;
550 qca->stats.trig_reset++;
551 qca->reset_count = 0;
553 case QCASPI_SYNC_RESET:
555 netdev_dbg(qca->net_dev, "sync: waiting for CPU on, count %u.\n",
557 if (qca->reset_count >= QCASPI_RESET_TIMEOUT) {
558 /* reset did not seem to take place, try again */
559 qca->sync = QCASPI_SYNC_UNKNOWN;
560 qca->stats.reset_timeout++;
561 netdev_dbg(qca->net_dev, "sync: reset timeout, restarting process.\n");
568 qcaspi_spi_thread(void *data)
570 struct qcaspi *qca = data;
573 netdev_info(qca->net_dev, "SPI thread created\n");
574 while (!kthread_should_stop()) {
575 set_current_state(TASK_INTERRUPTIBLE);
576 if (kthread_should_park()) {
577 netif_tx_disable(qca->net_dev);
578 netif_carrier_off(qca->net_dev);
579 qcaspi_flush_tx_ring(qca);
581 if (qca->sync == QCASPI_SYNC_READY) {
582 netif_carrier_on(qca->net_dev);
583 netif_wake_queue(qca->net_dev);
588 if ((qca->intr_req == qca->intr_svc) &&
589 !qca->txr.skb[qca->txr.head])
592 set_current_state(TASK_RUNNING);
594 netdev_dbg(qca->net_dev, "have work to do. int: %d, tx_skb: %p\n",
595 qca->intr_req - qca->intr_svc,
596 qca->txr.skb[qca->txr.head]);
598 qcaspi_qca7k_sync(qca, QCASPI_EVENT_UPDATE);
600 if (qca->sync != QCASPI_SYNC_READY) {
601 netdev_dbg(qca->net_dev, "sync: not ready %u, turn off carrier and flush\n",
602 (unsigned int)qca->sync);
603 netif_stop_queue(qca->net_dev);
604 netif_carrier_off(qca->net_dev);
605 qcaspi_flush_tx_ring(qca);
606 msleep(QCASPI_QCA7K_REBOOT_TIME_MS);
609 if (qca->intr_svc != qca->intr_req) {
610 qca->intr_svc = qca->intr_req;
611 start_spi_intr_handling(qca, &intr_cause);
613 if (intr_cause & SPI_INT_CPU_ON) {
614 qcaspi_qca7k_sync(qca, QCASPI_EVENT_CPUON);
616 /* Frame decoding in progress */
617 if (qca->frm_handle.state != qca->frm_handle.init)
618 qca->net_dev->stats.rx_dropped++;
620 qcafrm_fsm_init_spi(&qca->frm_handle);
621 qca->stats.device_reset++;
624 if (qca->sync != QCASPI_SYNC_READY)
627 netif_wake_queue(qca->net_dev);
628 netif_carrier_on(qca->net_dev);
631 if (intr_cause & SPI_INT_RDBUF_ERR) {
633 netdev_dbg(qca->net_dev, "===> rdbuf error!\n");
634 qca->stats.read_buf_err++;
635 qca->sync = QCASPI_SYNC_UNKNOWN;
639 if (intr_cause & SPI_INT_WRBUF_ERR) {
641 netdev_dbg(qca->net_dev, "===> wrbuf error!\n");
642 qca->stats.write_buf_err++;
643 qca->sync = QCASPI_SYNC_UNKNOWN;
647 /* can only handle other interrupts
648 * if sync has occurred
650 if (qca->sync == QCASPI_SYNC_READY) {
651 if (intr_cause & SPI_INT_PKT_AVLBL)
655 end_spi_intr_handling(qca, intr_cause);
658 if (qca->sync == QCASPI_SYNC_READY)
659 qcaspi_transmit(qca);
661 set_current_state(TASK_RUNNING);
662 netdev_info(qca->net_dev, "SPI thread exit\n");
668 qcaspi_intr_handler(int irq, void *data)
670 struct qcaspi *qca = data;
673 if (qca->spi_thread &&
674 qca->spi_thread->state != TASK_RUNNING)
675 wake_up_process(qca->spi_thread);
681 qcaspi_netdev_open(struct net_device *dev)
683 struct qcaspi *qca = netdev_priv(dev);
691 qca->sync = QCASPI_SYNC_UNKNOWN;
692 qcafrm_fsm_init_spi(&qca->frm_handle);
694 qca->spi_thread = kthread_run((void *)qcaspi_spi_thread,
695 qca, "%s", dev->name);
697 if (IS_ERR(qca->spi_thread)) {
698 netdev_err(dev, "%s: unable to start kernel thread.\n",
700 return PTR_ERR(qca->spi_thread);
703 ret = request_irq(qca->spi_dev->irq, qcaspi_intr_handler, 0,
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);
712 /* SPI thread takes care of TX queue */
718 qcaspi_netdev_close(struct net_device *dev)
720 struct qcaspi *qca = netdev_priv(dev);
722 netif_stop_queue(dev);
724 qcaspi_write_register(qca, SPI_REG_INTR_ENABLE, 0, wr_verify);
725 free_irq(qca->spi_dev->irq, qca);
727 kthread_stop(qca->spi_thread);
728 qca->spi_thread = NULL;
729 qcaspi_flush_tx_ring(qca);
735 qcaspi_netdev_xmit(struct sk_buff *skb, struct net_device *dev)
739 struct qcaspi *qca = netdev_priv(dev);
741 struct sk_buff *tskb;
744 if (skb->len < QCAFRM_MIN_LEN)
745 pad_len = QCAFRM_MIN_LEN - skb->len;
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;
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);
759 qca->stats.out_of_mem++;
760 return NETDEV_TX_BUSY;
766 frame_len = skb->len + pad_len;
768 ptmp = skb_push(skb, QCAFRM_HEADER_LEN);
769 qcafrm_create_header(ptmp, frame_len);
772 ptmp = skb_put_zero(skb, pad_len);
775 ptmp = skb_put(skb, QCAFRM_FOOTER_LEN);
776 qcafrm_create_footer(ptmp);
778 netdev_dbg(qca->net_dev, "Tx-ing packet: Size: 0x%08x\n",
781 qca->txr.size += skb->len + QCASPI_HW_PKT_LEN;
783 new_tail = qca->txr.tail + 1;
784 if (new_tail >= qca->txr.count)
787 qca->txr.skb[qca->txr.tail] = skb;
788 qca->txr.tail = new_tail;
790 if (!qcaspi_tx_ring_has_space(&qca->txr)) {
791 netif_stop_queue(qca->net_dev);
792 qca->stats.ring_full++;
795 netif_trans_update(dev);
797 if (qca->spi_thread &&
798 qca->spi_thread->state != TASK_RUNNING)
799 wake_up_process(qca->spi_thread);
805 qcaspi_netdev_tx_timeout(struct net_device *dev, unsigned int txqueue)
807 struct qcaspi *qca = netdev_priv(dev);
809 netdev_info(qca->net_dev, "Transmit timeout at %ld, latency %ld\n",
810 jiffies, jiffies - dev_trans_start(dev));
811 qca->net_dev->stats.tx_errors++;
812 /* Trigger tx queue flush and QCA7000 reset */
813 qca->sync = QCASPI_SYNC_UNKNOWN;
816 wake_up_process(qca->spi_thread);
820 qcaspi_netdev_init(struct net_device *dev)
822 struct qcaspi *qca = netdev_priv(dev);
824 dev->mtu = QCAFRM_MAX_MTU;
825 dev->type = ARPHRD_ETHER;
826 qca->clkspeed = qcaspi_clkspeed;
827 qca->burst_len = qcaspi_burst_len;
828 qca->spi_thread = NULL;
829 qca->buffer_size = (dev->mtu + VLAN_ETH_HLEN + QCAFRM_HEADER_LEN +
830 QCAFRM_FOOTER_LEN + 4) * 4;
832 memset(&qca->stats, 0, sizeof(struct qcaspi_stats));
834 qca->rx_buffer = kmalloc(qca->buffer_size, GFP_KERNEL);
838 qca->rx_skb = netdev_alloc_skb_ip_align(dev, qca->net_dev->mtu +
841 kfree(qca->rx_buffer);
842 netdev_info(qca->net_dev, "Failed to allocate RX sk_buff.\n");
850 qcaspi_netdev_uninit(struct net_device *dev)
852 struct qcaspi *qca = netdev_priv(dev);
854 kfree(qca->rx_buffer);
855 qca->buffer_size = 0;
856 dev_kfree_skb(qca->rx_skb);
859 static const struct net_device_ops qcaspi_netdev_ops = {
860 .ndo_init = qcaspi_netdev_init,
861 .ndo_uninit = qcaspi_netdev_uninit,
862 .ndo_open = qcaspi_netdev_open,
863 .ndo_stop = qcaspi_netdev_close,
864 .ndo_start_xmit = qcaspi_netdev_xmit,
865 .ndo_set_mac_address = eth_mac_addr,
866 .ndo_tx_timeout = qcaspi_netdev_tx_timeout,
867 .ndo_validate_addr = eth_validate_addr,
871 qcaspi_netdev_setup(struct net_device *dev)
873 struct qcaspi *qca = NULL;
875 dev->netdev_ops = &qcaspi_netdev_ops;
876 qcaspi_set_ethtool_ops(dev);
877 dev->watchdog_timeo = QCASPI_TX_TIMEOUT;
878 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
879 dev->tx_queue_len = 100;
881 /* MTU range: 46 - 1500 */
882 dev->min_mtu = QCAFRM_MIN_MTU;
883 dev->max_mtu = QCAFRM_MAX_MTU;
885 qca = netdev_priv(dev);
886 memset(qca, 0, sizeof(struct qcaspi));
888 memset(&qca->txr, 0, sizeof(qca->txr));
889 qca->txr.count = TX_RING_MAX_LEN;
892 static const struct of_device_id qca_spi_of_match[] = {
893 { .compatible = "qca,qca7000" },
896 MODULE_DEVICE_TABLE(of, qca_spi_of_match);
899 qca_spi_probe(struct spi_device *spi)
901 struct qcaspi *qca = NULL;
902 struct net_device *qcaspi_devs = NULL;
907 if (!spi->dev.of_node) {
908 dev_err(&spi->dev, "Missing device tree\n");
912 legacy_mode = of_property_read_bool(spi->dev.of_node,
915 if (qcaspi_clkspeed == 0) {
916 if (spi->max_speed_hz)
917 qcaspi_clkspeed = spi->max_speed_hz;
919 qcaspi_clkspeed = QCASPI_CLK_SPEED;
922 if ((qcaspi_clkspeed < QCASPI_CLK_SPEED_MIN) ||
923 (qcaspi_clkspeed > QCASPI_CLK_SPEED_MAX)) {
924 dev_err(&spi->dev, "Invalid clkspeed: %d\n",
929 if ((qcaspi_burst_len < QCASPI_BURST_LEN_MIN) ||
930 (qcaspi_burst_len > QCASPI_BURST_LEN_MAX)) {
931 dev_err(&spi->dev, "Invalid burst len: %d\n",
936 if ((qcaspi_pluggable < QCASPI_PLUGGABLE_MIN) ||
937 (qcaspi_pluggable > QCASPI_PLUGGABLE_MAX)) {
938 dev_err(&spi->dev, "Invalid pluggable: %d\n",
943 if (wr_verify < QCASPI_WRITE_VERIFY_MIN ||
944 wr_verify > QCASPI_WRITE_VERIFY_MAX) {
945 dev_err(&spi->dev, "Invalid write verify: %d\n",
950 dev_info(&spi->dev, "ver=%s, clkspeed=%d, burst_len=%d, pluggable=%d\n",
956 spi->mode = SPI_MODE_3;
957 spi->max_speed_hz = qcaspi_clkspeed;
958 if (spi_setup(spi) < 0) {
959 dev_err(&spi->dev, "Unable to setup SPI device\n");
963 qcaspi_devs = alloc_etherdev(sizeof(struct qcaspi));
967 qcaspi_netdev_setup(qcaspi_devs);
968 SET_NETDEV_DEV(qcaspi_devs, &spi->dev);
970 qca = netdev_priv(qcaspi_devs);
972 free_netdev(qcaspi_devs);
973 dev_err(&spi->dev, "Fail to retrieve private structure\n");
976 qca->net_dev = qcaspi_devs;
978 qca->legacy_mode = legacy_mode;
980 spi_set_drvdata(spi, qcaspi_devs);
982 mac = of_get_mac_address(spi->dev.of_node);
985 ether_addr_copy(qca->net_dev->dev_addr, mac);
987 if (!is_valid_ether_addr(qca->net_dev->dev_addr)) {
988 eth_hw_addr_random(qca->net_dev);
989 dev_info(&spi->dev, "Using random MAC address: %pM\n",
990 qca->net_dev->dev_addr);
993 netif_carrier_off(qca->net_dev);
995 if (!qcaspi_pluggable) {
996 qcaspi_read_register(qca, SPI_REG_SIGNATURE, &signature);
997 qcaspi_read_register(qca, SPI_REG_SIGNATURE, &signature);
999 if (signature != QCASPI_GOOD_SIGNATURE) {
1000 dev_err(&spi->dev, "Invalid signature (0x%04X)\n",
1002 free_netdev(qcaspi_devs);
1007 if (register_netdev(qcaspi_devs)) {
1008 dev_err(&spi->dev, "Unable to register net device %s\n",
1010 free_netdev(qcaspi_devs);
1014 qcaspi_init_device_debugfs(qca);
1020 qca_spi_remove(struct spi_device *spi)
1022 struct net_device *qcaspi_devs = spi_get_drvdata(spi);
1023 struct qcaspi *qca = netdev_priv(qcaspi_devs);
1025 qcaspi_remove_device_debugfs(qca);
1027 unregister_netdev(qcaspi_devs);
1028 free_netdev(qcaspi_devs);
1033 static const struct spi_device_id qca_spi_id[] = {
1037 MODULE_DEVICE_TABLE(spi, qca_spi_id);
1039 static struct spi_driver qca_spi_driver = {
1041 .name = QCASPI_DRV_NAME,
1042 .of_match_table = qca_spi_of_match,
1044 .id_table = qca_spi_id,
1045 .probe = qca_spi_probe,
1046 .remove = qca_spi_remove,
1048 module_spi_driver(qca_spi_driver);
1050 MODULE_DESCRIPTION("Qualcomm Atheros QCA7000 SPI Driver");
1051 MODULE_AUTHOR("Qualcomm Atheros Communications");
1052 MODULE_AUTHOR("Stefan Wahren <stefan.wahren@i2se.com>");
1053 MODULE_LICENSE("Dual BSD/GPL");
1054 MODULE_VERSION(QCASPI_DRV_VERSION);