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_TX_TIMEOUT (1 * HZ)
73 #define QCASPI_QCA7K_REBOOT_TIME_MS 1000
76 start_spi_intr_handling(struct qcaspi
*qca
, u16
*intr_cause
)
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
);
86 end_spi_intr_handling(struct qcaspi
*qca
, u16 intr_cause
)
88 u16 intr_enable
= (SPI_INT_CPU_ON
|
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
);
99 qcaspi_write_burst(struct qcaspi
*qca
, u8
*src
, u32 len
)
102 struct spi_message msg
;
103 struct spi_transfer transfer
[2];
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
);
128 qcaspi_write_legacy(struct qcaspi
*qca
, u8
*src
, u32 len
)
130 struct spi_message msg
;
131 struct spi_transfer transfer
;
134 memset(&transfer
, 0, sizeof(transfer
));
135 spi_message_init(&msg
);
137 transfer
.tx_buf
= src
;
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
);
152 qcaspi_read_burst(struct qcaspi
*qca
, u8
*dst
, u32 len
)
154 struct spi_message msg
;
156 struct spi_transfer transfer
[2];
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
);
181 qcaspi_read_legacy(struct qcaspi
*qca
, u8
*dst
, u32 len
)
183 struct spi_message msg
;
184 struct spi_transfer transfer
;
187 memset(&transfer
, 0, sizeof(transfer
));
188 spi_message_init(&msg
);
190 transfer
.rx_buf
= dst
;
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
);
205 qcaspi_tx_cmd(struct qcaspi
*qca
, u16 cmd
)
208 struct spi_message msg
;
209 struct spi_transfer transfer
;
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
);
227 qcaspi_spi_error(qca
);
233 qcaspi_tx_frame(struct qcaspi
*qca
, struct sk_buff
*skb
)
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
);
249 if (count
> qca
->burst_len
)
250 count
= qca
->burst_len
;
252 if (qca
->legacy_mode
) {
253 written
= qcaspi_write_legacy(qca
,
257 written
= qcaspi_write_burst(qca
,
262 if (written
!= count
)
273 qcaspi_transmit(struct qcaspi
*qca
)
275 struct net_device_stats
*n_stats
= &qca
->net_dev
->stats
;
281 if (qca
->txr
.skb
[qca
->txr
.head
] == NULL
)
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
) {
291 qca
->stats
.write_buf_miss
++;
295 if (qcaspi_tx_frame(qca
, qca
->txr
.skb
[qca
->txr
.head
]) == -1) {
296 qca
->stats
.write_err
++;
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
)
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
);
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
;
334 /* Allocate rx SKB if we don't have one available. */
336 qca
->rx_skb
= netdev_alloc_skb_ip_align(net_dev
,
340 netdev_dbg(net_dev
, "out of RX resources\n");
341 qca
->stats
.out_of_mem
++;
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",
351 if (available
== 0) {
352 netdev_dbg(net_dev
, "qcaspi_receive called without any data being available!\n");
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
);
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
,
371 bytes_read
= qcaspi_read_burst(qca
, qca
->rx_buffer
,
375 netdev_dbg(net_dev
, "available: %d, byte read: %d\n",
376 available
, bytes_read
);
379 available
-= bytes_read
;
381 qca
->stats
.read_err
++;
387 while ((bytes_read
--) && (qca
->rx_skb
)) {
390 retcode
= qcafrm_fsm_decode(&qca
->frm_handle
,
392 skb_tailroom(qca
->rx_skb
),
400 netdev_dbg(net_dev
, "no RX tail\n");
401 n_stats
->rx_errors
++;
402 n_stats
->rx_dropped
++;
405 netdev_dbg(net_dev
, "invalid RX length\n");
406 n_stats
->rx_errors
++;
407 n_stats
->rx_dropped
++;
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
);
421 netdev_dbg(net_dev
, "out of RX resources\n");
422 n_stats
->rx_errors
++;
423 qca
->stats
.out_of_mem
++;
433 /* Check that tx ring stores only so much bytes
434 * that fit into the internal QCA buffer.
438 qcaspi_tx_ring_has_space(struct tx_ring
*txr
)
440 if (txr
->skb
[txr
->tail
])
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.
451 qcaspi_flush_tx_ring(struct qcaspi
*qca
)
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
++;
469 netif_tx_unlock_bh(qca
->net_dev
);
473 qcaspi_qca7k_sync(struct qcaspi
*qca
, int event
)
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");
489 /* ensure that the WRBUF is empty */
490 qcaspi_read_register(qca
, SPI_REG_WRBUF_SPC_AVA
,
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
;
496 netdev_dbg(qca
->net_dev
, "sync: got CPU on, now in sync\n");
497 qca
->sync
= QCASPI_SYNC_READY
;
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 */
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");
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;
532 case QCASPI_SYNC_RESET
:
534 netdev_dbg(qca
->net_dev
, "sync: waiting for CPU on, count %u.\n",
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");
547 qcaspi_spi_thread(void *data
)
549 struct qcaspi
*qca
= data
;
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
))
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
);
585 if (qca
->sync
!= QCASPI_SYNC_READY
)
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
) {
595 netdev_dbg(qca
->net_dev
, "===> rdbuf error!\n");
596 qca
->stats
.read_buf_err
++;
597 qca
->sync
= QCASPI_SYNC_UNKNOWN
;
601 if (intr_cause
& SPI_INT_WRBUF_ERR
) {
603 netdev_dbg(qca
->net_dev
, "===> wrbuf error!\n");
604 qca
->stats
.write_buf_err
++;
605 qca
->sync
= QCASPI_SYNC_UNKNOWN
;
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
)
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");
630 qcaspi_intr_handler(int irq
, void *data
)
632 struct qcaspi
*qca
= data
;
635 if (qca
->spi_thread
&&
636 qca
->spi_thread
->state
!= TASK_RUNNING
)
637 wake_up_process(qca
->spi_thread
);
643 qcaspi_netdev_open(struct net_device
*dev
)
645 struct qcaspi
*qca
= netdev_priv(dev
);
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",
662 return PTR_ERR(qca
->spi_thread
);
665 ret
= request_irq(qca
->spi_dev
->irq
, qcaspi_intr_handler
, 0,
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
);
674 /* SPI thread takes care of TX queue */
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
);
697 qcaspi_netdev_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
701 struct qcaspi
*qca
= netdev_priv(dev
);
703 struct sk_buff
*tskb
;
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
);
721 qca
->stats
.out_of_mem
++;
722 return NETDEV_TX_BUSY
;
728 frame_len
= skb
->len
+ pad_len
;
730 ptmp
= skb_push(skb
, QCAFRM_HEADER_LEN
);
731 qcafrm_create_header(ptmp
, frame_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",
743 qca
->txr
.size
+= skb
->len
+ QCASPI_HW_PKT_LEN
;
745 new_tail
= qca
->txr
.tail
+ 1;
746 if (new_tail
>= qca
->txr
.count
)
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
);
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
;
778 wake_up_process(qca
->spi_thread
);
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
);
800 qca
->rx_skb
= netdev_alloc_skb_ip_align(dev
, qca
->net_dev
->mtu
+
803 kfree(qca
->rx_buffer
);
804 netdev_info(qca
->net_dev
, "Failed to allocate RX sk_buff.\n");
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;
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
,
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" },
859 MODULE_DEVICE_TABLE(of
, qca_spi_of_match
);
862 qca_spi_probe(struct spi_device
*spi
)
864 struct qcaspi
*qca
= NULL
;
865 struct net_device
*qcaspi_devs
= NULL
;
870 if (!spi
->dev
.of_node
) {
871 dev_err(&spi
->dev
, "Missing device tree\n");
875 legacy_mode
= of_property_read_bool(spi
->dev
.of_node
,
878 if (qcaspi_clkspeed
== 0) {
879 if (spi
->max_speed_hz
)
880 qcaspi_clkspeed
= spi
->max_speed_hz
;
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",
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",
899 if ((qcaspi_pluggable
< QCASPI_PLUGGABLE_MIN
) ||
900 (qcaspi_pluggable
> QCASPI_PLUGGABLE_MAX
)) {
901 dev_err(&spi
->dev
, "Invalid pluggable: %d\n",
906 dev_info(&spi
->dev
, "ver=%s, clkspeed=%d, burst_len=%d, pluggable=%d\n",
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");
919 qcaspi_devs
= alloc_etherdev(sizeof(struct qcaspi
));
923 qcaspi_netdev_setup(qcaspi_devs
);
924 SET_NETDEV_DEV(qcaspi_devs
, &spi
->dev
);
926 qca
= netdev_priv(qcaspi_devs
);
928 free_netdev(qcaspi_devs
);
929 dev_err(&spi
->dev
, "Fail to retrieve private structure\n");
932 qca
->net_dev
= qcaspi_devs
;
934 qca
->legacy_mode
= legacy_mode
;
936 spi_set_drvdata(spi
, qcaspi_devs
);
938 mac
= of_get_mac_address(spi
->dev
.of_node
);
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",
958 free_netdev(qcaspi_devs
);
963 if (register_netdev(qcaspi_devs
)) {
964 dev_err(&spi
->dev
, "Unable to register net device %s\n",
966 free_netdev(qcaspi_devs
);
970 qcaspi_init_device_debugfs(qca
);
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
);
989 static const struct spi_device_id qca_spi_id
[] = {
993 MODULE_DEVICE_TABLE(spi
, qca_spi_id
);
995 static struct spi_driver qca_spi_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
);