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
= &qca
->spi_msg2
;
103 struct spi_transfer
*transfer
= &qca
->spi_xfer2
[0];
106 cmd
= cpu_to_be16(QCA7K_SPI_WRITE
| QCA7K_SPI_EXTERNAL
);
107 transfer
->tx_buf
= &cmd
;
108 transfer
->rx_buf
= NULL
;
109 transfer
->len
= QCASPI_CMD_LEN
;
110 transfer
= &qca
->spi_xfer2
[1];
111 transfer
->tx_buf
= src
;
112 transfer
->rx_buf
= NULL
;
115 ret
= spi_sync(qca
->spi_dev
, msg
);
117 if (ret
|| (msg
->actual_length
!= QCASPI_CMD_LEN
+ len
)) {
118 qcaspi_spi_error(qca
);
126 qcaspi_write_legacy(struct qcaspi
*qca
, u8
*src
, u32 len
)
128 struct spi_message
*msg
= &qca
->spi_msg1
;
129 struct spi_transfer
*transfer
= &qca
->spi_xfer1
;
132 transfer
->tx_buf
= src
;
133 transfer
->rx_buf
= NULL
;
136 ret
= spi_sync(qca
->spi_dev
, msg
);
138 if (ret
|| (msg
->actual_length
!= len
)) {
139 qcaspi_spi_error(qca
);
147 qcaspi_read_burst(struct qcaspi
*qca
, u8
*dst
, u32 len
)
149 struct spi_message
*msg
= &qca
->spi_msg2
;
151 struct spi_transfer
*transfer
= &qca
->spi_xfer2
[0];
154 cmd
= cpu_to_be16(QCA7K_SPI_READ
| QCA7K_SPI_EXTERNAL
);
155 transfer
->tx_buf
= &cmd
;
156 transfer
->rx_buf
= NULL
;
157 transfer
->len
= QCASPI_CMD_LEN
;
158 transfer
= &qca
->spi_xfer2
[1];
159 transfer
->tx_buf
= NULL
;
160 transfer
->rx_buf
= dst
;
163 ret
= spi_sync(qca
->spi_dev
, msg
);
165 if (ret
|| (msg
->actual_length
!= QCASPI_CMD_LEN
+ len
)) {
166 qcaspi_spi_error(qca
);
174 qcaspi_read_legacy(struct qcaspi
*qca
, u8
*dst
, u32 len
)
176 struct spi_message
*msg
= &qca
->spi_msg1
;
177 struct spi_transfer
*transfer
= &qca
->spi_xfer1
;
180 transfer
->tx_buf
= NULL
;
181 transfer
->rx_buf
= dst
;
184 ret
= spi_sync(qca
->spi_dev
, msg
);
186 if (ret
|| (msg
->actual_length
!= len
)) {
187 qcaspi_spi_error(qca
);
195 qcaspi_tx_cmd(struct qcaspi
*qca
, u16 cmd
)
198 struct spi_message
*msg
= &qca
->spi_msg1
;
199 struct spi_transfer
*transfer
= &qca
->spi_xfer1
;
202 tx_data
= cpu_to_be16(cmd
);
203 transfer
->len
= sizeof(tx_data
);
204 transfer
->tx_buf
= &tx_data
;
205 transfer
->rx_buf
= NULL
;
207 ret
= spi_sync(qca
->spi_dev
, msg
);
213 qcaspi_spi_error(qca
);
219 qcaspi_tx_frame(struct qcaspi
*qca
, struct sk_buff
*skb
)
228 qcaspi_write_register(qca
, SPI_REG_BFR_SIZE
, len
);
229 if (qca
->legacy_mode
)
230 qcaspi_tx_cmd(qca
, QCA7K_SPI_WRITE
| QCA7K_SPI_EXTERNAL
);
235 if (count
> qca
->burst_len
)
236 count
= qca
->burst_len
;
238 if (qca
->legacy_mode
) {
239 written
= qcaspi_write_legacy(qca
,
243 written
= qcaspi_write_burst(qca
,
248 if (written
!= count
)
259 qcaspi_transmit(struct qcaspi
*qca
)
261 struct net_device_stats
*n_stats
= &qca
->net_dev
->stats
;
267 if (qca
->txr
.skb
[qca
->txr
.head
] == NULL
)
270 qcaspi_read_register(qca
, SPI_REG_WRBUF_SPC_AVA
, &available
);
272 while (qca
->txr
.skb
[qca
->txr
.head
]) {
273 pkt_len
= qca
->txr
.skb
[qca
->txr
.head
]->len
+ QCASPI_HW_PKT_LEN
;
275 if (available
< pkt_len
) {
277 qca
->stats
.write_buf_miss
++;
281 if (qcaspi_tx_frame(qca
, qca
->txr
.skb
[qca
->txr
.head
]) == -1) {
282 qca
->stats
.write_err
++;
287 n_stats
->tx_packets
++;
288 n_stats
->tx_bytes
+= qca
->txr
.skb
[qca
->txr
.head
]->len
;
289 available
-= pkt_len
;
291 /* remove the skb from the queue */
292 /* XXX After inconsistent lock states netif_tx_lock()
293 * has been replaced by netif_tx_lock_bh() and so on.
295 netif_tx_lock_bh(qca
->net_dev
);
296 dev_kfree_skb(qca
->txr
.skb
[qca
->txr
.head
]);
297 qca
->txr
.skb
[qca
->txr
.head
] = NULL
;
298 qca
->txr
.size
-= pkt_len
;
299 new_head
= qca
->txr
.head
+ 1;
300 if (new_head
>= qca
->txr
.count
)
302 qca
->txr
.head
= new_head
;
303 if (netif_queue_stopped(qca
->net_dev
))
304 netif_wake_queue(qca
->net_dev
);
305 netif_tx_unlock_bh(qca
->net_dev
);
312 qcaspi_receive(struct qcaspi
*qca
)
314 struct net_device
*net_dev
= qca
->net_dev
;
315 struct net_device_stats
*n_stats
= &net_dev
->stats
;
320 /* Allocate rx SKB if we don't have one available. */
322 qca
->rx_skb
= netdev_alloc_skb_ip_align(net_dev
,
326 netdev_dbg(net_dev
, "out of RX resources\n");
327 qca
->stats
.out_of_mem
++;
332 /* Read the packet size. */
333 qcaspi_read_register(qca
, SPI_REG_RDBUF_BYTE_AVA
, &available
);
334 netdev_dbg(net_dev
, "qcaspi_receive: SPI_REG_RDBUF_BYTE_AVA: Value: %08x\n",
337 if (available
== 0) {
338 netdev_dbg(net_dev
, "qcaspi_receive called without any data being available!\n");
342 qcaspi_write_register(qca
, SPI_REG_BFR_SIZE
, available
);
344 if (qca
->legacy_mode
)
345 qcaspi_tx_cmd(qca
, QCA7K_SPI_READ
| QCA7K_SPI_EXTERNAL
);
348 u32 count
= available
;
350 if (count
> qca
->burst_len
)
351 count
= qca
->burst_len
;
353 if (qca
->legacy_mode
) {
354 bytes_read
= qcaspi_read_legacy(qca
, qca
->rx_buffer
,
357 bytes_read
= qcaspi_read_burst(qca
, qca
->rx_buffer
,
361 netdev_dbg(net_dev
, "available: %d, byte read: %d\n",
362 available
, bytes_read
);
365 available
-= bytes_read
;
367 qca
->stats
.read_err
++;
373 while ((bytes_read
--) && (qca
->rx_skb
)) {
376 retcode
= qcafrm_fsm_decode(&qca
->frm_handle
,
378 skb_tailroom(qca
->rx_skb
),
386 netdev_dbg(net_dev
, "no RX tail\n");
387 n_stats
->rx_errors
++;
388 n_stats
->rx_dropped
++;
391 netdev_dbg(net_dev
, "invalid RX length\n");
392 n_stats
->rx_errors
++;
393 n_stats
->rx_dropped
++;
396 qca
->rx_skb
->dev
= qca
->net_dev
;
397 n_stats
->rx_packets
++;
398 n_stats
->rx_bytes
+= retcode
;
399 skb_put(qca
->rx_skb
, retcode
);
400 qca
->rx_skb
->protocol
= eth_type_trans(
401 qca
->rx_skb
, qca
->rx_skb
->dev
);
402 qca
->rx_skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
403 netif_rx_ni(qca
->rx_skb
);
404 qca
->rx_skb
= netdev_alloc_skb_ip_align(net_dev
,
405 net_dev
->mtu
+ VLAN_ETH_HLEN
);
407 netdev_dbg(net_dev
, "out of RX resources\n");
408 n_stats
->rx_errors
++;
409 qca
->stats
.out_of_mem
++;
419 /* Check that tx ring stores only so much bytes
420 * that fit into the internal QCA buffer.
424 qcaspi_tx_ring_has_space(struct tx_ring
*txr
)
426 if (txr
->skb
[txr
->tail
])
429 return (txr
->size
+ QCAFRM_MAX_LEN
< QCASPI_HW_BUF_LEN
) ? 1 : 0;
432 /* Flush the tx ring. This function is only safe to
433 * call from the qcaspi_spi_thread.
437 qcaspi_flush_tx_ring(struct qcaspi
*qca
)
441 /* XXX After inconsistent lock states netif_tx_lock()
442 * has been replaced by netif_tx_lock_bh() and so on.
444 netif_tx_lock_bh(qca
->net_dev
);
445 for (i
= 0; i
< TX_RING_MAX_LEN
; i
++) {
446 if (qca
->txr
.skb
[i
]) {
447 dev_kfree_skb(qca
->txr
.skb
[i
]);
448 qca
->txr
.skb
[i
] = NULL
;
449 qca
->net_dev
->stats
.tx_dropped
++;
455 netif_tx_unlock_bh(qca
->net_dev
);
459 qcaspi_qca7k_sync(struct qcaspi
*qca
, int event
)
464 static u16 reset_count
;
466 if (event
== QCASPI_EVENT_CPUON
) {
467 /* Read signature twice, if not valid
468 * go back to unknown state.
470 qcaspi_read_register(qca
, SPI_REG_SIGNATURE
, &signature
);
471 qcaspi_read_register(qca
, SPI_REG_SIGNATURE
, &signature
);
472 if (signature
!= QCASPI_GOOD_SIGNATURE
) {
473 qca
->sync
= QCASPI_SYNC_UNKNOWN
;
474 netdev_dbg(qca
->net_dev
, "sync: got CPU on, but signature was invalid, restart\n");
476 /* ensure that the WRBUF is empty */
477 qcaspi_read_register(qca
, SPI_REG_WRBUF_SPC_AVA
,
479 if (wrbuf_space
!= QCASPI_HW_BUF_LEN
) {
480 netdev_dbg(qca
->net_dev
, "sync: got CPU on, but wrbuf not empty. reset!\n");
481 qca
->sync
= QCASPI_SYNC_UNKNOWN
;
483 netdev_dbg(qca
->net_dev
, "sync: got CPU on, now in sync\n");
484 qca
->sync
= QCASPI_SYNC_READY
;
491 case QCASPI_SYNC_READY
:
492 /* Read signature, if not valid go to unknown state. */
493 qcaspi_read_register(qca
, SPI_REG_SIGNATURE
, &signature
);
494 if (signature
!= QCASPI_GOOD_SIGNATURE
) {
495 qca
->sync
= QCASPI_SYNC_UNKNOWN
;
496 netdev_dbg(qca
->net_dev
, "sync: bad signature, restart\n");
497 /* don't reset right away */
501 case QCASPI_SYNC_UNKNOWN
:
502 /* Read signature, if not valid stay in unknown state */
503 qcaspi_read_register(qca
, SPI_REG_SIGNATURE
, &signature
);
504 if (signature
!= QCASPI_GOOD_SIGNATURE
) {
505 netdev_dbg(qca
->net_dev
, "sync: could not read signature to reset device, retry.\n");
509 /* TODO: use GPIO to reset QCA7000 in legacy mode*/
510 netdev_dbg(qca
->net_dev
, "sync: resetting device.\n");
511 qcaspi_read_register(qca
, SPI_REG_SPI_CONFIG
, &spi_config
);
512 spi_config
|= QCASPI_SLAVE_RESET_BIT
;
513 qcaspi_write_register(qca
, SPI_REG_SPI_CONFIG
, spi_config
);
515 qca
->sync
= QCASPI_SYNC_RESET
;
516 qca
->stats
.trig_reset
++;
519 case QCASPI_SYNC_RESET
:
521 netdev_dbg(qca
->net_dev
, "sync: waiting for CPU on, count %u.\n",
523 if (reset_count
>= QCASPI_RESET_TIMEOUT
) {
524 /* reset did not seem to take place, try again */
525 qca
->sync
= QCASPI_SYNC_UNKNOWN
;
526 qca
->stats
.reset_timeout
++;
527 netdev_dbg(qca
->net_dev
, "sync: reset timeout, restarting process.\n");
534 qcaspi_spi_thread(void *data
)
536 struct qcaspi
*qca
= data
;
539 netdev_info(qca
->net_dev
, "SPI thread created\n");
540 while (!kthread_should_stop()) {
541 set_current_state(TASK_INTERRUPTIBLE
);
542 if ((qca
->intr_req
== qca
->intr_svc
) &&
543 (qca
->txr
.skb
[qca
->txr
.head
] == NULL
) &&
544 (qca
->sync
== QCASPI_SYNC_READY
))
547 set_current_state(TASK_RUNNING
);
549 netdev_dbg(qca
->net_dev
, "have work to do. int: %d, tx_skb: %p\n",
550 qca
->intr_req
- qca
->intr_svc
,
551 qca
->txr
.skb
[qca
->txr
.head
]);
553 qcaspi_qca7k_sync(qca
, QCASPI_EVENT_UPDATE
);
555 if (qca
->sync
!= QCASPI_SYNC_READY
) {
556 netdev_dbg(qca
->net_dev
, "sync: not ready %u, turn off carrier and flush\n",
557 (unsigned int)qca
->sync
);
558 netif_stop_queue(qca
->net_dev
);
559 netif_carrier_off(qca
->net_dev
);
560 qcaspi_flush_tx_ring(qca
);
561 msleep(QCASPI_QCA7K_REBOOT_TIME_MS
);
564 if (qca
->intr_svc
!= qca
->intr_req
) {
565 qca
->intr_svc
= qca
->intr_req
;
566 start_spi_intr_handling(qca
, &intr_cause
);
568 if (intr_cause
& SPI_INT_CPU_ON
) {
569 qcaspi_qca7k_sync(qca
, QCASPI_EVENT_CPUON
);
572 if (qca
->sync
!= QCASPI_SYNC_READY
)
575 qca
->stats
.device_reset
++;
576 netif_wake_queue(qca
->net_dev
);
577 netif_carrier_on(qca
->net_dev
);
580 if (intr_cause
& SPI_INT_RDBUF_ERR
) {
582 netdev_dbg(qca
->net_dev
, "===> rdbuf error!\n");
583 qca
->stats
.read_buf_err
++;
584 qca
->sync
= QCASPI_SYNC_UNKNOWN
;
588 if (intr_cause
& SPI_INT_WRBUF_ERR
) {
590 netdev_dbg(qca
->net_dev
, "===> wrbuf error!\n");
591 qca
->stats
.write_buf_err
++;
592 qca
->sync
= QCASPI_SYNC_UNKNOWN
;
596 /* can only handle other interrupts
597 * if sync has occurred
599 if (qca
->sync
== QCASPI_SYNC_READY
) {
600 if (intr_cause
& SPI_INT_PKT_AVLBL
)
604 end_spi_intr_handling(qca
, intr_cause
);
607 if (qca
->sync
== QCASPI_SYNC_READY
)
608 qcaspi_transmit(qca
);
610 set_current_state(TASK_RUNNING
);
611 netdev_info(qca
->net_dev
, "SPI thread exit\n");
617 qcaspi_intr_handler(int irq
, void *data
)
619 struct qcaspi
*qca
= data
;
622 if (qca
->spi_thread
&&
623 qca
->spi_thread
->state
!= TASK_RUNNING
)
624 wake_up_process(qca
->spi_thread
);
630 qcaspi_netdev_open(struct net_device
*dev
)
632 struct qcaspi
*qca
= netdev_priv(dev
);
640 qca
->sync
= QCASPI_SYNC_UNKNOWN
;
641 qcafrm_fsm_init_spi(&qca
->frm_handle
);
643 qca
->spi_thread
= kthread_run((void *)qcaspi_spi_thread
,
644 qca
, "%s", dev
->name
);
646 if (IS_ERR(qca
->spi_thread
)) {
647 netdev_err(dev
, "%s: unable to start kernel thread.\n",
649 return PTR_ERR(qca
->spi_thread
);
652 ret
= request_irq(qca
->spi_dev
->irq
, qcaspi_intr_handler
, 0,
655 netdev_err(dev
, "%s: unable to get IRQ %d (irqval=%d).\n",
656 QCASPI_DRV_NAME
, qca
->spi_dev
->irq
, ret
);
657 kthread_stop(qca
->spi_thread
);
661 netif_start_queue(qca
->net_dev
);
667 qcaspi_netdev_close(struct net_device
*dev
)
669 struct qcaspi
*qca
= netdev_priv(dev
);
671 netif_stop_queue(dev
);
673 qcaspi_write_register(qca
, SPI_REG_INTR_ENABLE
, 0);
674 free_irq(qca
->spi_dev
->irq
, qca
);
676 kthread_stop(qca
->spi_thread
);
677 qca
->spi_thread
= NULL
;
678 qcaspi_flush_tx_ring(qca
);
684 qcaspi_netdev_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
688 struct qcaspi
*qca
= netdev_priv(dev
);
690 struct sk_buff
*tskb
;
693 if (skb
->len
< QCAFRM_MIN_LEN
)
694 pad_len
= QCAFRM_MIN_LEN
- skb
->len
;
696 if (qca
->txr
.skb
[qca
->txr
.tail
]) {
697 netdev_warn(qca
->net_dev
, "queue was unexpectedly full!\n");
698 netif_stop_queue(qca
->net_dev
);
699 qca
->stats
.ring_full
++;
700 return NETDEV_TX_BUSY
;
703 if ((skb_headroom(skb
) < QCAFRM_HEADER_LEN
) ||
704 (skb_tailroom(skb
) < QCAFRM_FOOTER_LEN
+ pad_len
)) {
705 tskb
= skb_copy_expand(skb
, QCAFRM_HEADER_LEN
,
706 QCAFRM_FOOTER_LEN
+ pad_len
, GFP_ATOMIC
);
708 netdev_dbg(qca
->net_dev
, "could not allocate tx_buff\n");
709 qca
->stats
.out_of_mem
++;
710 return NETDEV_TX_BUSY
;
716 frame_len
= skb
->len
+ pad_len
;
718 ptmp
= skb_push(skb
, QCAFRM_HEADER_LEN
);
719 qcafrm_create_header(ptmp
, frame_len
);
722 ptmp
= skb_put_zero(skb
, pad_len
);
725 ptmp
= skb_put(skb
, QCAFRM_FOOTER_LEN
);
726 qcafrm_create_footer(ptmp
);
728 netdev_dbg(qca
->net_dev
, "Tx-ing packet: Size: 0x%08x\n",
731 qca
->txr
.size
+= skb
->len
+ QCASPI_HW_PKT_LEN
;
733 new_tail
= qca
->txr
.tail
+ 1;
734 if (new_tail
>= qca
->txr
.count
)
737 qca
->txr
.skb
[qca
->txr
.tail
] = skb
;
738 qca
->txr
.tail
= new_tail
;
740 if (!qcaspi_tx_ring_has_space(&qca
->txr
)) {
741 netif_stop_queue(qca
->net_dev
);
742 qca
->stats
.ring_full
++;
745 netif_trans_update(dev
);
747 if (qca
->spi_thread
&&
748 qca
->spi_thread
->state
!= TASK_RUNNING
)
749 wake_up_process(qca
->spi_thread
);
755 qcaspi_netdev_tx_timeout(struct net_device
*dev
)
757 struct qcaspi
*qca
= netdev_priv(dev
);
759 netdev_info(qca
->net_dev
, "Transmit timeout at %ld, latency %ld\n",
760 jiffies
, jiffies
- dev_trans_start(dev
));
761 qca
->net_dev
->stats
.tx_errors
++;
762 /* Trigger tx queue flush and QCA7000 reset */
763 qca
->sync
= QCASPI_SYNC_UNKNOWN
;
767 qcaspi_netdev_init(struct net_device
*dev
)
769 struct qcaspi
*qca
= netdev_priv(dev
);
771 dev
->mtu
= QCAFRM_MAX_MTU
;
772 dev
->type
= ARPHRD_ETHER
;
773 qca
->clkspeed
= qcaspi_clkspeed
;
774 qca
->burst_len
= qcaspi_burst_len
;
775 qca
->spi_thread
= NULL
;
776 qca
->buffer_size
= (dev
->mtu
+ VLAN_ETH_HLEN
+ QCAFRM_HEADER_LEN
+
777 QCAFRM_FOOTER_LEN
+ 4) * 4;
779 memset(&qca
->stats
, 0, sizeof(struct qcaspi_stats
));
781 qca
->rx_buffer
= kmalloc(qca
->buffer_size
, GFP_KERNEL
);
785 qca
->rx_skb
= netdev_alloc_skb_ip_align(dev
, qca
->net_dev
->mtu
+
788 kfree(qca
->rx_buffer
);
789 netdev_info(qca
->net_dev
, "Failed to allocate RX sk_buff.\n");
797 qcaspi_netdev_uninit(struct net_device
*dev
)
799 struct qcaspi
*qca
= netdev_priv(dev
);
801 kfree(qca
->rx_buffer
);
802 qca
->buffer_size
= 0;
804 dev_kfree_skb(qca
->rx_skb
);
807 static const struct net_device_ops qcaspi_netdev_ops
= {
808 .ndo_init
= qcaspi_netdev_init
,
809 .ndo_uninit
= qcaspi_netdev_uninit
,
810 .ndo_open
= qcaspi_netdev_open
,
811 .ndo_stop
= qcaspi_netdev_close
,
812 .ndo_start_xmit
= qcaspi_netdev_xmit
,
813 .ndo_set_mac_address
= eth_mac_addr
,
814 .ndo_tx_timeout
= qcaspi_netdev_tx_timeout
,
815 .ndo_validate_addr
= eth_validate_addr
,
819 qcaspi_netdev_setup(struct net_device
*dev
)
821 struct qcaspi
*qca
= NULL
;
823 dev
->netdev_ops
= &qcaspi_netdev_ops
;
824 qcaspi_set_ethtool_ops(dev
);
825 dev
->watchdog_timeo
= QCASPI_TX_TIMEOUT
;
826 dev
->priv_flags
&= ~IFF_TX_SKB_SHARING
;
827 dev
->tx_queue_len
= 100;
829 /* MTU range: 46 - 1500 */
830 dev
->min_mtu
= QCAFRM_MIN_MTU
;
831 dev
->max_mtu
= QCAFRM_MAX_MTU
;
833 qca
= netdev_priv(dev
);
834 memset(qca
, 0, sizeof(struct qcaspi
));
836 memset(&qca
->spi_xfer1
, 0, sizeof(struct spi_transfer
));
837 memset(&qca
->spi_xfer2
, 0, sizeof(struct spi_transfer
) * 2);
839 spi_message_init(&qca
->spi_msg1
);
840 spi_message_add_tail(&qca
->spi_xfer1
, &qca
->spi_msg1
);
842 spi_message_init(&qca
->spi_msg2
);
843 spi_message_add_tail(&qca
->spi_xfer2
[0], &qca
->spi_msg2
);
844 spi_message_add_tail(&qca
->spi_xfer2
[1], &qca
->spi_msg2
);
846 memset(&qca
->txr
, 0, sizeof(qca
->txr
));
847 qca
->txr
.count
= TX_RING_MAX_LEN
;
850 static const struct of_device_id qca_spi_of_match
[] = {
851 { .compatible
= "qca,qca7000" },
854 MODULE_DEVICE_TABLE(of
, qca_spi_of_match
);
857 qca_spi_probe(struct spi_device
*spi
)
859 struct qcaspi
*qca
= NULL
;
860 struct net_device
*qcaspi_devs
= NULL
;
865 if (!spi
->dev
.of_node
) {
866 dev_err(&spi
->dev
, "Missing device tree\n");
870 legacy_mode
= of_property_read_bool(spi
->dev
.of_node
,
873 if (qcaspi_clkspeed
== 0) {
874 if (spi
->max_speed_hz
)
875 qcaspi_clkspeed
= spi
->max_speed_hz
;
877 qcaspi_clkspeed
= QCASPI_CLK_SPEED
;
880 if ((qcaspi_clkspeed
< QCASPI_CLK_SPEED_MIN
) ||
881 (qcaspi_clkspeed
> QCASPI_CLK_SPEED_MAX
)) {
882 dev_info(&spi
->dev
, "Invalid clkspeed: %d\n",
887 if ((qcaspi_burst_len
< QCASPI_BURST_LEN_MIN
) ||
888 (qcaspi_burst_len
> QCASPI_BURST_LEN_MAX
)) {
889 dev_info(&spi
->dev
, "Invalid burst len: %d\n",
894 if ((qcaspi_pluggable
< QCASPI_PLUGGABLE_MIN
) ||
895 (qcaspi_pluggable
> QCASPI_PLUGGABLE_MAX
)) {
896 dev_info(&spi
->dev
, "Invalid pluggable: %d\n",
901 dev_info(&spi
->dev
, "ver=%s, clkspeed=%d, burst_len=%d, pluggable=%d\n",
907 spi
->mode
= SPI_MODE_3
;
908 spi
->max_speed_hz
= qcaspi_clkspeed
;
909 if (spi_setup(spi
) < 0) {
910 dev_err(&spi
->dev
, "Unable to setup SPI device\n");
914 qcaspi_devs
= alloc_etherdev(sizeof(struct qcaspi
));
918 qcaspi_netdev_setup(qcaspi_devs
);
919 SET_NETDEV_DEV(qcaspi_devs
, &spi
->dev
);
921 qca
= netdev_priv(qcaspi_devs
);
923 free_netdev(qcaspi_devs
);
924 dev_err(&spi
->dev
, "Fail to retrieve private structure\n");
927 qca
->net_dev
= qcaspi_devs
;
929 qca
->legacy_mode
= legacy_mode
;
931 spi_set_drvdata(spi
, qcaspi_devs
);
933 mac
= of_get_mac_address(spi
->dev
.of_node
);
936 ether_addr_copy(qca
->net_dev
->dev_addr
, mac
);
938 if (!is_valid_ether_addr(qca
->net_dev
->dev_addr
)) {
939 eth_hw_addr_random(qca
->net_dev
);
940 dev_info(&spi
->dev
, "Using random MAC address: %pM\n",
941 qca
->net_dev
->dev_addr
);
944 netif_carrier_off(qca
->net_dev
);
946 if (!qcaspi_pluggable
) {
947 qcaspi_read_register(qca
, SPI_REG_SIGNATURE
, &signature
);
948 qcaspi_read_register(qca
, SPI_REG_SIGNATURE
, &signature
);
950 if (signature
!= QCASPI_GOOD_SIGNATURE
) {
951 dev_err(&spi
->dev
, "Invalid signature (0x%04X)\n",
953 free_netdev(qcaspi_devs
);
958 if (register_netdev(qcaspi_devs
)) {
959 dev_info(&spi
->dev
, "Unable to register net device %s\n",
961 free_netdev(qcaspi_devs
);
965 qcaspi_init_device_debugfs(qca
);
971 qca_spi_remove(struct spi_device
*spi
)
973 struct net_device
*qcaspi_devs
= spi_get_drvdata(spi
);
974 struct qcaspi
*qca
= netdev_priv(qcaspi_devs
);
976 qcaspi_remove_device_debugfs(qca
);
978 unregister_netdev(qcaspi_devs
);
979 free_netdev(qcaspi_devs
);
984 static const struct spi_device_id qca_spi_id
[] = {
988 MODULE_DEVICE_TABLE(spi
, qca_spi_id
);
990 static struct spi_driver qca_spi_driver
= {
992 .name
= QCASPI_DRV_NAME
,
993 .of_match_table
= qca_spi_of_match
,
995 .id_table
= qca_spi_id
,
996 .probe
= qca_spi_probe
,
997 .remove
= qca_spi_remove
,
999 module_spi_driver(qca_spi_driver
);
1001 MODULE_DESCRIPTION("Qualcomm Atheros QCA7000 SPI Driver");
1002 MODULE_AUTHOR("Qualcomm Atheros Communications");
1003 MODULE_AUTHOR("Stefan Wahren <stefan.wahren@i2se.com>");
1004 MODULE_LICENSE("Dual BSD/GPL");
1005 MODULE_VERSION(QCASPI_DRV_VERSION
);