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_debug.h"
47 #include "qca_framing.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_MTU QCAFRM_ETHMAXMTU
73 #define QCASPI_TX_TIMEOUT (1 * HZ)
74 #define QCASPI_QCA7K_REBOOT_TIME_MS 1000
77 start_spi_intr_handling(struct qcaspi *qca, u16 *intr_cause)
81 qcaspi_write_register(qca, SPI_REG_INTR_ENABLE, 0);
82 qcaspi_read_register(qca, SPI_REG_INTR_CAUSE, intr_cause);
83 netdev_dbg(qca->net_dev, "interrupts: 0x%04x\n", *intr_cause);
87 end_spi_intr_handling(struct qcaspi *qca, u16 intr_cause)
89 u16 intr_enable = (SPI_INT_CPU_ON |
94 qcaspi_write_register(qca, SPI_REG_INTR_CAUSE, intr_cause);
95 qcaspi_write_register(qca, SPI_REG_INTR_ENABLE, intr_enable);
96 netdev_dbg(qca->net_dev, "acking int: 0x%04x\n", intr_cause);
100 qcaspi_write_burst(struct qcaspi *qca, u8 *src, u32 len)
103 struct spi_message *msg = &qca->spi_msg2;
104 struct spi_transfer *transfer = &qca->spi_xfer2[0];
107 cmd = cpu_to_be16(QCA7K_SPI_WRITE | QCA7K_SPI_EXTERNAL);
108 transfer->tx_buf = &cmd;
109 transfer->rx_buf = NULL;
110 transfer->len = QCASPI_CMD_LEN;
111 transfer = &qca->spi_xfer2[1];
112 transfer->tx_buf = src;
113 transfer->rx_buf = NULL;
116 ret = spi_sync(qca->spi_dev, msg);
118 if (ret || (msg->actual_length != QCASPI_CMD_LEN + len)) {
119 qcaspi_spi_error(qca);
127 qcaspi_write_legacy(struct qcaspi *qca, u8 *src, u32 len)
129 struct spi_message *msg = &qca->spi_msg1;
130 struct spi_transfer *transfer = &qca->spi_xfer1;
133 transfer->tx_buf = src;
134 transfer->rx_buf = NULL;
137 ret = spi_sync(qca->spi_dev, msg);
139 if (ret || (msg->actual_length != len)) {
140 qcaspi_spi_error(qca);
148 qcaspi_read_burst(struct qcaspi *qca, u8 *dst, u32 len)
150 struct spi_message *msg = &qca->spi_msg2;
152 struct spi_transfer *transfer = &qca->spi_xfer2[0];
155 cmd = cpu_to_be16(QCA7K_SPI_READ | QCA7K_SPI_EXTERNAL);
156 transfer->tx_buf = &cmd;
157 transfer->rx_buf = NULL;
158 transfer->len = QCASPI_CMD_LEN;
159 transfer = &qca->spi_xfer2[1];
160 transfer->tx_buf = NULL;
161 transfer->rx_buf = dst;
164 ret = spi_sync(qca->spi_dev, msg);
166 if (ret || (msg->actual_length != QCASPI_CMD_LEN + len)) {
167 qcaspi_spi_error(qca);
175 qcaspi_read_legacy(struct qcaspi *qca, u8 *dst, u32 len)
177 struct spi_message *msg = &qca->spi_msg1;
178 struct spi_transfer *transfer = &qca->spi_xfer1;
181 transfer->tx_buf = NULL;
182 transfer->rx_buf = dst;
185 ret = spi_sync(qca->spi_dev, msg);
187 if (ret || (msg->actual_length != len)) {
188 qcaspi_spi_error(qca);
196 qcaspi_tx_frame(struct qcaspi *qca, struct sk_buff *skb)
205 qcaspi_write_register(qca, SPI_REG_BFR_SIZE, len);
206 if (qca->legacy_mode)
207 qcaspi_tx_cmd(qca, QCA7K_SPI_WRITE | QCA7K_SPI_EXTERNAL);
212 if (count > qca->burst_len)
213 count = qca->burst_len;
215 if (qca->legacy_mode) {
216 written = qcaspi_write_legacy(qca,
220 written = qcaspi_write_burst(qca,
225 if (written != count)
236 qcaspi_transmit(struct qcaspi *qca)
238 struct net_device_stats *n_stats = &qca->net_dev->stats;
244 if (qca->txr.skb[qca->txr.head] == NULL)
247 qcaspi_read_register(qca, SPI_REG_WRBUF_SPC_AVA, &available);
249 while (qca->txr.skb[qca->txr.head]) {
250 pkt_len = qca->txr.skb[qca->txr.head]->len + QCASPI_HW_PKT_LEN;
252 if (available < pkt_len) {
254 qca->stats.write_buf_miss++;
258 if (qcaspi_tx_frame(qca, qca->txr.skb[qca->txr.head]) == -1) {
259 qca->stats.write_err++;
264 n_stats->tx_packets++;
265 n_stats->tx_bytes += qca->txr.skb[qca->txr.head]->len;
266 available -= pkt_len;
268 /* remove the skb from the queue */
269 /* XXX After inconsistent lock states netif_tx_lock()
270 * has been replaced by netif_tx_lock_bh() and so on.
272 netif_tx_lock_bh(qca->net_dev);
273 dev_kfree_skb(qca->txr.skb[qca->txr.head]);
274 qca->txr.skb[qca->txr.head] = NULL;
275 qca->txr.size -= pkt_len;
276 new_head = qca->txr.head + 1;
277 if (new_head >= qca->txr.count)
279 qca->txr.head = new_head;
280 if (netif_queue_stopped(qca->net_dev))
281 netif_wake_queue(qca->net_dev);
282 netif_tx_unlock_bh(qca->net_dev);
289 qcaspi_receive(struct qcaspi *qca)
291 struct net_device *net_dev = qca->net_dev;
292 struct net_device_stats *n_stats = &net_dev->stats;
297 /* Allocate rx SKB if we don't have one available. */
299 qca->rx_skb = netdev_alloc_skb(net_dev,
300 net_dev->mtu + VLAN_ETH_HLEN);
302 netdev_dbg(net_dev, "out of RX resources\n");
303 qca->stats.out_of_mem++;
308 /* Read the packet size. */
309 qcaspi_read_register(qca, SPI_REG_RDBUF_BYTE_AVA, &available);
310 netdev_dbg(net_dev, "qcaspi_receive: SPI_REG_RDBUF_BYTE_AVA: Value: %08x\n",
313 if (available == 0) {
314 netdev_dbg(net_dev, "qcaspi_receive called without any data being available!\n");
318 qcaspi_write_register(qca, SPI_REG_BFR_SIZE, available);
320 if (qca->legacy_mode)
321 qcaspi_tx_cmd(qca, QCA7K_SPI_READ | QCA7K_SPI_EXTERNAL);
324 u32 count = available;
326 if (count > qca->burst_len)
327 count = qca->burst_len;
329 if (qca->legacy_mode) {
330 bytes_read = qcaspi_read_legacy(qca, qca->rx_buffer,
333 bytes_read = qcaspi_read_burst(qca, qca->rx_buffer,
337 netdev_dbg(net_dev, "available: %d, byte read: %d\n",
338 available, bytes_read);
341 available -= bytes_read;
343 qca->stats.read_err++;
349 while ((bytes_read--) && (qca->rx_skb)) {
352 retcode = qcafrm_fsm_decode(&qca->frm_handle,
354 skb_tailroom(qca->rx_skb),
362 netdev_dbg(net_dev, "no RX tail\n");
363 n_stats->rx_errors++;
364 n_stats->rx_dropped++;
367 netdev_dbg(net_dev, "invalid RX length\n");
368 n_stats->rx_errors++;
369 n_stats->rx_dropped++;
372 qca->rx_skb->dev = qca->net_dev;
373 n_stats->rx_packets++;
374 n_stats->rx_bytes += retcode;
375 skb_put(qca->rx_skb, retcode);
376 qca->rx_skb->protocol = eth_type_trans(
377 qca->rx_skb, qca->rx_skb->dev);
378 qca->rx_skb->ip_summed = CHECKSUM_UNNECESSARY;
379 netif_rx_ni(qca->rx_skb);
380 qca->rx_skb = netdev_alloc_skb(net_dev,
381 net_dev->mtu + VLAN_ETH_HLEN);
383 netdev_dbg(net_dev, "out of RX resources\n");
384 n_stats->rx_errors++;
385 qca->stats.out_of_mem++;
395 /* Check that tx ring stores only so much bytes
396 * that fit into the internal QCA buffer.
400 qcaspi_tx_ring_has_space(struct tx_ring *txr)
402 if (txr->skb[txr->tail])
405 return (txr->size + QCAFRM_ETHMAXLEN < QCASPI_HW_BUF_LEN) ? 1 : 0;
408 /* Flush the tx ring. This function is only safe to
409 * call from the qcaspi_spi_thread.
413 qcaspi_flush_tx_ring(struct qcaspi *qca)
417 /* XXX After inconsistent lock states netif_tx_lock()
418 * has been replaced by netif_tx_lock_bh() and so on.
420 netif_tx_lock_bh(qca->net_dev);
421 for (i = 0; i < TX_RING_MAX_LEN; i++) {
422 if (qca->txr.skb[i]) {
423 dev_kfree_skb(qca->txr.skb[i]);
424 qca->txr.skb[i] = NULL;
425 qca->net_dev->stats.tx_dropped++;
431 netif_tx_unlock_bh(qca->net_dev);
435 qcaspi_qca7k_sync(struct qcaspi *qca, int event)
440 static u16 reset_count;
442 if (event == QCASPI_EVENT_CPUON) {
443 /* Read signature twice, if not valid
444 * go back to unknown state.
446 qcaspi_read_register(qca, SPI_REG_SIGNATURE, &signature);
447 qcaspi_read_register(qca, SPI_REG_SIGNATURE, &signature);
448 if (signature != QCASPI_GOOD_SIGNATURE) {
449 qca->sync = QCASPI_SYNC_UNKNOWN;
450 netdev_dbg(qca->net_dev, "sync: got CPU on, but signature was invalid, restart\n");
452 /* ensure that the WRBUF is empty */
453 qcaspi_read_register(qca, SPI_REG_WRBUF_SPC_AVA,
455 if (wrbuf_space != QCASPI_HW_BUF_LEN) {
456 netdev_dbg(qca->net_dev, "sync: got CPU on, but wrbuf not empty. reset!\n");
457 qca->sync = QCASPI_SYNC_UNKNOWN;
459 netdev_dbg(qca->net_dev, "sync: got CPU on, now in sync\n");
460 qca->sync = QCASPI_SYNC_READY;
467 case QCASPI_SYNC_READY:
468 /* Read signature, if not valid go to unknown state. */
469 qcaspi_read_register(qca, SPI_REG_SIGNATURE, &signature);
470 if (signature != QCASPI_GOOD_SIGNATURE) {
471 qca->sync = QCASPI_SYNC_UNKNOWN;
472 netdev_dbg(qca->net_dev, "sync: bad signature, restart\n");
473 /* don't reset right away */
477 case QCASPI_SYNC_UNKNOWN:
478 /* Read signature, if not valid stay in unknown state */
479 qcaspi_read_register(qca, SPI_REG_SIGNATURE, &signature);
480 if (signature != QCASPI_GOOD_SIGNATURE) {
481 netdev_dbg(qca->net_dev, "sync: could not read signature to reset device, retry.\n");
485 /* TODO: use GPIO to reset QCA7000 in legacy mode*/
486 netdev_dbg(qca->net_dev, "sync: resetting device.\n");
487 qcaspi_read_register(qca, SPI_REG_SPI_CONFIG, &spi_config);
488 spi_config |= QCASPI_SLAVE_RESET_BIT;
489 qcaspi_write_register(qca, SPI_REG_SPI_CONFIG, spi_config);
491 qca->sync = QCASPI_SYNC_RESET;
492 qca->stats.trig_reset++;
495 case QCASPI_SYNC_RESET:
497 netdev_dbg(qca->net_dev, "sync: waiting for CPU on, count %u.\n",
499 if (reset_count >= QCASPI_RESET_TIMEOUT) {
500 /* reset did not seem to take place, try again */
501 qca->sync = QCASPI_SYNC_UNKNOWN;
502 qca->stats.reset_timeout++;
503 netdev_dbg(qca->net_dev, "sync: reset timeout, restarting process.\n");
510 qcaspi_spi_thread(void *data)
512 struct qcaspi *qca = data;
515 netdev_info(qca->net_dev, "SPI thread created\n");
516 while (!kthread_should_stop()) {
517 set_current_state(TASK_INTERRUPTIBLE);
518 if ((qca->intr_req == qca->intr_svc) &&
519 (qca->txr.skb[qca->txr.head] == NULL) &&
520 (qca->sync == QCASPI_SYNC_READY))
523 set_current_state(TASK_RUNNING);
525 netdev_dbg(qca->net_dev, "have work to do. int: %d, tx_skb: %p\n",
526 qca->intr_req - qca->intr_svc,
527 qca->txr.skb[qca->txr.head]);
529 qcaspi_qca7k_sync(qca, QCASPI_EVENT_UPDATE);
531 if (qca->sync != QCASPI_SYNC_READY) {
532 netdev_dbg(qca->net_dev, "sync: not ready %u, turn off carrier and flush\n",
533 (unsigned int)qca->sync);
534 netif_stop_queue(qca->net_dev);
535 netif_carrier_off(qca->net_dev);
536 qcaspi_flush_tx_ring(qca);
537 msleep(QCASPI_QCA7K_REBOOT_TIME_MS);
540 if (qca->intr_svc != qca->intr_req) {
541 qca->intr_svc = qca->intr_req;
542 start_spi_intr_handling(qca, &intr_cause);
544 if (intr_cause & SPI_INT_CPU_ON) {
545 qcaspi_qca7k_sync(qca, QCASPI_EVENT_CPUON);
548 if (qca->sync != QCASPI_SYNC_READY)
551 qca->stats.device_reset++;
552 netif_wake_queue(qca->net_dev);
553 netif_carrier_on(qca->net_dev);
556 if (intr_cause & SPI_INT_RDBUF_ERR) {
558 netdev_dbg(qca->net_dev, "===> rdbuf error!\n");
559 qca->stats.read_buf_err++;
560 qca->sync = QCASPI_SYNC_UNKNOWN;
564 if (intr_cause & SPI_INT_WRBUF_ERR) {
566 netdev_dbg(qca->net_dev, "===> wrbuf error!\n");
567 qca->stats.write_buf_err++;
568 qca->sync = QCASPI_SYNC_UNKNOWN;
572 /* can only handle other interrupts
573 * if sync has occurred
575 if (qca->sync == QCASPI_SYNC_READY) {
576 if (intr_cause & SPI_INT_PKT_AVLBL)
580 end_spi_intr_handling(qca, intr_cause);
583 if (qca->sync == QCASPI_SYNC_READY)
584 qcaspi_transmit(qca);
586 set_current_state(TASK_RUNNING);
587 netdev_info(qca->net_dev, "SPI thread exit\n");
593 qcaspi_intr_handler(int irq, void *data)
595 struct qcaspi *qca = data;
598 if (qca->spi_thread &&
599 qca->spi_thread->state != TASK_RUNNING)
600 wake_up_process(qca->spi_thread);
606 qcaspi_netdev_open(struct net_device *dev)
608 struct qcaspi *qca = netdev_priv(dev);
616 qca->sync = QCASPI_SYNC_UNKNOWN;
617 qcafrm_fsm_init(&qca->frm_handle);
619 qca->spi_thread = kthread_run((void *)qcaspi_spi_thread,
620 qca, "%s", dev->name);
622 if (IS_ERR(qca->spi_thread)) {
623 netdev_err(dev, "%s: unable to start kernel thread.\n",
625 return PTR_ERR(qca->spi_thread);
628 ret = request_irq(qca->spi_dev->irq, qcaspi_intr_handler, 0,
631 netdev_err(dev, "%s: unable to get IRQ %d (irqval=%d).\n",
632 QCASPI_DRV_NAME, qca->spi_dev->irq, ret);
633 kthread_stop(qca->spi_thread);
637 netif_start_queue(qca->net_dev);
643 qcaspi_netdev_close(struct net_device *dev)
645 struct qcaspi *qca = netdev_priv(dev);
647 netif_stop_queue(dev);
649 qcaspi_write_register(qca, SPI_REG_INTR_ENABLE, 0);
650 free_irq(qca->spi_dev->irq, qca);
652 kthread_stop(qca->spi_thread);
653 qca->spi_thread = NULL;
654 qcaspi_flush_tx_ring(qca);
660 qcaspi_netdev_xmit(struct sk_buff *skb, struct net_device *dev)
664 struct qcaspi *qca = netdev_priv(dev);
666 struct sk_buff *tskb;
669 if (skb->len < QCAFRM_ETHMINLEN)
670 pad_len = QCAFRM_ETHMINLEN - skb->len;
672 if (qca->txr.skb[qca->txr.tail]) {
673 netdev_warn(qca->net_dev, "queue was unexpectedly full!\n");
674 netif_stop_queue(qca->net_dev);
675 qca->stats.ring_full++;
676 return NETDEV_TX_BUSY;
679 if ((skb_headroom(skb) < QCAFRM_HEADER_LEN) ||
680 (skb_tailroom(skb) < QCAFRM_FOOTER_LEN + pad_len)) {
681 tskb = skb_copy_expand(skb, QCAFRM_HEADER_LEN,
682 QCAFRM_FOOTER_LEN + pad_len, GFP_ATOMIC);
684 netdev_dbg(qca->net_dev, "could not allocate tx_buff\n");
685 qca->stats.out_of_mem++;
686 return NETDEV_TX_BUSY;
692 frame_len = skb->len + pad_len;
694 ptmp = skb_push(skb, QCAFRM_HEADER_LEN);
695 qcafrm_create_header(ptmp, frame_len);
698 ptmp = skb_put(skb, pad_len);
699 memset(ptmp, 0, pad_len);
702 ptmp = skb_put(skb, QCAFRM_FOOTER_LEN);
703 qcafrm_create_footer(ptmp);
705 netdev_dbg(qca->net_dev, "Tx-ing packet: Size: 0x%08x\n",
708 qca->txr.size += skb->len + QCASPI_HW_PKT_LEN;
710 new_tail = qca->txr.tail + 1;
711 if (new_tail >= qca->txr.count)
714 qca->txr.skb[qca->txr.tail] = skb;
715 qca->txr.tail = new_tail;
717 if (!qcaspi_tx_ring_has_space(&qca->txr)) {
718 netif_stop_queue(qca->net_dev);
719 qca->stats.ring_full++;
722 netif_trans_update(dev);
724 if (qca->spi_thread &&
725 qca->spi_thread->state != TASK_RUNNING)
726 wake_up_process(qca->spi_thread);
732 qcaspi_netdev_tx_timeout(struct net_device *dev)
734 struct qcaspi *qca = netdev_priv(dev);
736 netdev_info(qca->net_dev, "Transmit timeout at %ld, latency %ld\n",
737 jiffies, jiffies - dev_trans_start(dev));
738 qca->net_dev->stats.tx_errors++;
739 /* Trigger tx queue flush and QCA7000 reset */
740 qca->sync = QCASPI_SYNC_UNKNOWN;
744 qcaspi_netdev_init(struct net_device *dev)
746 struct qcaspi *qca = netdev_priv(dev);
748 dev->mtu = QCASPI_MTU;
749 dev->type = ARPHRD_ETHER;
750 qca->clkspeed = qcaspi_clkspeed;
751 qca->burst_len = qcaspi_burst_len;
752 qca->spi_thread = NULL;
753 qca->buffer_size = (dev->mtu + VLAN_ETH_HLEN + QCAFRM_HEADER_LEN +
754 QCAFRM_FOOTER_LEN + 4) * 4;
756 memset(&qca->stats, 0, sizeof(struct qcaspi_stats));
758 qca->rx_buffer = kmalloc(qca->buffer_size, GFP_KERNEL);
762 qca->rx_skb = netdev_alloc_skb(dev, qca->net_dev->mtu + VLAN_ETH_HLEN);
764 kfree(qca->rx_buffer);
765 netdev_info(qca->net_dev, "Failed to allocate RX sk_buff.\n");
773 qcaspi_netdev_uninit(struct net_device *dev)
775 struct qcaspi *qca = netdev_priv(dev);
777 kfree(qca->rx_buffer);
778 qca->buffer_size = 0;
780 dev_kfree_skb(qca->rx_skb);
783 static const struct net_device_ops qcaspi_netdev_ops = {
784 .ndo_init = qcaspi_netdev_init,
785 .ndo_uninit = qcaspi_netdev_uninit,
786 .ndo_open = qcaspi_netdev_open,
787 .ndo_stop = qcaspi_netdev_close,
788 .ndo_start_xmit = qcaspi_netdev_xmit,
789 .ndo_set_mac_address = eth_mac_addr,
790 .ndo_tx_timeout = qcaspi_netdev_tx_timeout,
791 .ndo_validate_addr = eth_validate_addr,
795 qcaspi_netdev_setup(struct net_device *dev)
797 struct qcaspi *qca = NULL;
799 dev->netdev_ops = &qcaspi_netdev_ops;
800 qcaspi_set_ethtool_ops(dev);
801 dev->watchdog_timeo = QCASPI_TX_TIMEOUT;
802 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
803 dev->tx_queue_len = 100;
805 /* MTU range: 46 - 1500 */
806 dev->min_mtu = QCAFRM_ETHMINMTU;
807 dev->max_mtu = QCAFRM_ETHMAXMTU;
809 qca = netdev_priv(dev);
810 memset(qca, 0, sizeof(struct qcaspi));
812 memset(&qca->spi_xfer1, 0, sizeof(struct spi_transfer));
813 memset(&qca->spi_xfer2, 0, sizeof(struct spi_transfer) * 2);
815 spi_message_init(&qca->spi_msg1);
816 spi_message_add_tail(&qca->spi_xfer1, &qca->spi_msg1);
818 spi_message_init(&qca->spi_msg2);
819 spi_message_add_tail(&qca->spi_xfer2[0], &qca->spi_msg2);
820 spi_message_add_tail(&qca->spi_xfer2[1], &qca->spi_msg2);
822 memset(&qca->txr, 0, sizeof(qca->txr));
823 qca->txr.count = TX_RING_MAX_LEN;
826 static const struct of_device_id qca_spi_of_match[] = {
827 { .compatible = "qca,qca7000" },
830 MODULE_DEVICE_TABLE(of, qca_spi_of_match);
833 qca_spi_probe(struct spi_device *spi)
835 struct qcaspi *qca = NULL;
836 struct net_device *qcaspi_devs = NULL;
841 if (!spi->dev.of_node) {
842 dev_err(&spi->dev, "Missing device tree\n");
846 legacy_mode = of_property_read_bool(spi->dev.of_node,
849 if (qcaspi_clkspeed == 0) {
850 if (spi->max_speed_hz)
851 qcaspi_clkspeed = spi->max_speed_hz;
853 qcaspi_clkspeed = QCASPI_CLK_SPEED;
856 if ((qcaspi_clkspeed < QCASPI_CLK_SPEED_MIN) ||
857 (qcaspi_clkspeed > QCASPI_CLK_SPEED_MAX)) {
858 dev_info(&spi->dev, "Invalid clkspeed: %d\n",
863 if ((qcaspi_burst_len < QCASPI_BURST_LEN_MIN) ||
864 (qcaspi_burst_len > QCASPI_BURST_LEN_MAX)) {
865 dev_info(&spi->dev, "Invalid burst len: %d\n",
870 if ((qcaspi_pluggable < QCASPI_PLUGGABLE_MIN) ||
871 (qcaspi_pluggable > QCASPI_PLUGGABLE_MAX)) {
872 dev_info(&spi->dev, "Invalid pluggable: %d\n",
877 dev_info(&spi->dev, "ver=%s, clkspeed=%d, burst_len=%d, pluggable=%d\n",
883 spi->mode = SPI_MODE_3;
884 spi->max_speed_hz = qcaspi_clkspeed;
885 if (spi_setup(spi) < 0) {
886 dev_err(&spi->dev, "Unable to setup SPI device\n");
890 qcaspi_devs = alloc_etherdev(sizeof(struct qcaspi));
894 qcaspi_netdev_setup(qcaspi_devs);
896 qca = netdev_priv(qcaspi_devs);
898 free_netdev(qcaspi_devs);
899 dev_err(&spi->dev, "Fail to retrieve private structure\n");
902 qca->net_dev = qcaspi_devs;
904 qca->legacy_mode = legacy_mode;
906 spi_set_drvdata(spi, qcaspi_devs);
908 mac = of_get_mac_address(spi->dev.of_node);
911 ether_addr_copy(qca->net_dev->dev_addr, mac);
913 if (!is_valid_ether_addr(qca->net_dev->dev_addr)) {
914 eth_hw_addr_random(qca->net_dev);
915 dev_info(&spi->dev, "Using random MAC address: %pM\n",
916 qca->net_dev->dev_addr);
919 netif_carrier_off(qca->net_dev);
921 if (!qcaspi_pluggable) {
922 qcaspi_read_register(qca, SPI_REG_SIGNATURE, &signature);
923 qcaspi_read_register(qca, SPI_REG_SIGNATURE, &signature);
925 if (signature != QCASPI_GOOD_SIGNATURE) {
926 dev_err(&spi->dev, "Invalid signature (0x%04X)\n",
928 free_netdev(qcaspi_devs);
933 if (register_netdev(qcaspi_devs)) {
934 dev_info(&spi->dev, "Unable to register net device %s\n",
936 free_netdev(qcaspi_devs);
940 qcaspi_init_device_debugfs(qca);
946 qca_spi_remove(struct spi_device *spi)
948 struct net_device *qcaspi_devs = spi_get_drvdata(spi);
949 struct qcaspi *qca = netdev_priv(qcaspi_devs);
951 qcaspi_remove_device_debugfs(qca);
953 unregister_netdev(qcaspi_devs);
954 free_netdev(qcaspi_devs);
959 static const struct spi_device_id qca_spi_id[] = {
963 MODULE_DEVICE_TABLE(spi, qca_spi_id);
965 static struct spi_driver qca_spi_driver = {
967 .name = QCASPI_DRV_NAME,
968 .of_match_table = qca_spi_of_match,
970 .id_table = qca_spi_id,
971 .probe = qca_spi_probe,
972 .remove = qca_spi_remove,
974 module_spi_driver(qca_spi_driver);
976 MODULE_DESCRIPTION("Qualcomm Atheros SPI Driver");
977 MODULE_AUTHOR("Qualcomm Atheros Communications");
978 MODULE_AUTHOR("Stefan Wahren <stefan.wahren@i2se.com>");
979 MODULE_LICENSE("Dual BSD/GPL");
980 MODULE_VERSION(QCASPI_DRV_VERSION);