1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2010 ASIX Electronics Corporation
4 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
6 * ASIX AX88796C SPI Fast Ethernet Linux driver
9 #define pr_fmt(fmt) "ax88796c: " fmt
11 #include "ax88796c_main.h"
12 #include "ax88796c_ioctl.h"
14 #include <linux/bitmap.h>
15 #include <linux/etherdevice.h>
16 #include <linux/iopoll.h>
17 #include <linux/lockdep.h>
18 #include <linux/mdio.h>
19 #include <linux/minmax.h>
20 #include <linux/module.h>
21 #include <linux/netdevice.h>
23 #include <linux/phy.h>
24 #include <linux/skbuff.h>
25 #include <linux/spi/spi.h>
27 static int comp
= IS_ENABLED(CONFIG_SPI_AX88796C_COMPRESSION
);
28 static int msg_enable
= NETIF_MSG_PROBE
|
33 static const char *no_regs_list
= "80018001,e1918001,8001a001,fc0d0000";
34 unsigned long ax88796c_no_regs_mask
[AX88796C_REGDUMP_LEN
/ (sizeof(unsigned long) * 8)];
36 module_param(msg_enable
, int, 0444);
37 MODULE_PARM_DESC(msg_enable
, "Message mask (see linux/netdevice.h for bitmap)");
39 static int ax88796c_soft_reset(struct ax88796c_device
*ax_local
)
44 lockdep_assert_held(&ax_local
->spi_lock
);
46 AX_WRITE(&ax_local
->ax_spi
, PSR_RESET
, P0_PSR
);
47 AX_WRITE(&ax_local
->ax_spi
, PSR_RESET_CLR
, P0_PSR
);
49 ret
= read_poll_timeout(AX_READ
, ret
,
50 (ret
& PSR_DEV_READY
),
51 0, jiffies_to_usecs(160 * HZ
/ 1000), false,
52 &ax_local
->ax_spi
, P0_PSR
);
56 temp
= AX_READ(&ax_local
->ax_spi
, P4_SPICR
);
57 if (ax_local
->priv_flags
& AX_CAP_COMP
) {
58 AX_WRITE(&ax_local
->ax_spi
,
59 (temp
| SPICR_RCEN
| SPICR_QCEN
), P4_SPICR
);
60 ax_local
->ax_spi
.comp
= 1;
62 AX_WRITE(&ax_local
->ax_spi
,
63 (temp
& ~(SPICR_RCEN
| SPICR_QCEN
)), P4_SPICR
);
64 ax_local
->ax_spi
.comp
= 0;
70 static int ax88796c_reload_eeprom(struct ax88796c_device
*ax_local
)
74 lockdep_assert_held(&ax_local
->spi_lock
);
76 AX_WRITE(&ax_local
->ax_spi
, EECR_RELOAD
, P3_EECR
);
78 ret
= read_poll_timeout(AX_READ
, ret
,
79 (ret
& PSR_DEV_READY
),
80 0, jiffies_to_usecs(2 * HZ
/ 1000), false,
81 &ax_local
->ax_spi
, P0_PSR
);
83 dev_err(&ax_local
->spi
->dev
,
84 "timeout waiting for reload eeprom\n");
91 static void ax88796c_set_hw_multicast(struct net_device
*ndev
)
93 struct ax88796c_device
*ax_local
= to_ax88796c_device(ndev
);
94 int mc_count
= netdev_mc_count(ndev
);
97 lockdep_assert_held(&ax_local
->spi_lock
);
99 memset(ax_local
->multi_filter
, 0, AX_MCAST_FILTER_SIZE
);
101 if (ndev
->flags
& IFF_PROMISC
) {
104 } else if (ndev
->flags
& IFF_ALLMULTI
|| mc_count
> AX_MAX_MCAST
) {
105 rx_ctl
|= RXCR_AMALL
;
107 } else if (mc_count
== 0) {
108 /* just broadcast and directed */
112 struct netdev_hw_addr
*ha
;
114 netdev_for_each_mc_addr(ha
, ndev
) {
115 crc_bits
= ether_crc(ETH_ALEN
, ha
->addr
);
116 ax_local
->multi_filter
[crc_bits
>> 29] |=
117 (1 << ((crc_bits
>> 26) & 7));
120 for (i
= 0; i
< 4; i
++) {
121 AX_WRITE(&ax_local
->ax_spi
,
122 ((ax_local
->multi_filter
[i
* 2 + 1] << 8) |
123 ax_local
->multi_filter
[i
* 2]), P3_MFAR(i
));
127 AX_WRITE(&ax_local
->ax_spi
, rx_ctl
, P2_RXCR
);
130 static void ax88796c_set_mac_addr(struct net_device
*ndev
)
132 struct ax88796c_device
*ax_local
= to_ax88796c_device(ndev
);
134 lockdep_assert_held(&ax_local
->spi_lock
);
136 AX_WRITE(&ax_local
->ax_spi
, ((u16
)(ndev
->dev_addr
[4] << 8) |
137 (u16
)ndev
->dev_addr
[5]), P3_MACASR0
);
138 AX_WRITE(&ax_local
->ax_spi
, ((u16
)(ndev
->dev_addr
[2] << 8) |
139 (u16
)ndev
->dev_addr
[3]), P3_MACASR1
);
140 AX_WRITE(&ax_local
->ax_spi
, ((u16
)(ndev
->dev_addr
[0] << 8) |
141 (u16
)ndev
->dev_addr
[1]), P3_MACASR2
);
144 static void ax88796c_load_mac_addr(struct net_device
*ndev
)
146 struct ax88796c_device
*ax_local
= to_ax88796c_device(ndev
);
150 lockdep_assert_held(&ax_local
->spi_lock
);
152 /* Try the device tree first */
153 if (!platform_get_ethdev_address(&ax_local
->spi
->dev
, ndev
) &&
154 is_valid_ether_addr(ndev
->dev_addr
)) {
155 if (netif_msg_probe(ax_local
))
156 dev_info(&ax_local
->spi
->dev
,
157 "MAC address read from device tree\n");
161 /* Read the MAC address from AX88796C */
162 temp
= AX_READ(&ax_local
->ax_spi
, P3_MACASR0
);
164 addr
[4] = (u8
)(temp
>> 8);
166 temp
= AX_READ(&ax_local
->ax_spi
, P3_MACASR1
);
168 addr
[2] = (u8
)(temp
>> 8);
170 temp
= AX_READ(&ax_local
->ax_spi
, P3_MACASR2
);
172 addr
[0] = (u8
)(temp
>> 8);
174 if (is_valid_ether_addr(addr
)) {
175 eth_hw_addr_set(ndev
, addr
);
176 if (netif_msg_probe(ax_local
))
177 dev_info(&ax_local
->spi
->dev
,
178 "MAC address read from ASIX chip\n");
182 /* Use random address if none found */
183 if (netif_msg_probe(ax_local
))
184 dev_info(&ax_local
->spi
->dev
, "Use random MAC address\n");
185 eth_hw_addr_random(ndev
);
188 static void ax88796c_proc_tx_hdr(struct tx_pkt_info
*info
, u8 ip_summed
)
190 u16 pkt_len_bar
= (~info
->pkt_len
& TX_HDR_SOP_PKTLENBAR
);
192 /* Prepare SOP header */
193 info
->sop
.flags_len
= info
->pkt_len
|
194 ((ip_summed
== CHECKSUM_NONE
) ||
195 (ip_summed
== CHECKSUM_UNNECESSARY
) ? TX_HDR_SOP_DICF
: 0);
197 info
->sop
.seq_lenbar
= ((info
->seq_num
<< 11) & TX_HDR_SOP_SEQNUM
)
199 cpu_to_be16s(&info
->sop
.flags_len
);
200 cpu_to_be16s(&info
->sop
.seq_lenbar
);
202 /* Prepare Segment header */
203 info
->seg
.flags_seqnum_seglen
= TX_HDR_SEG_FS
| TX_HDR_SEG_LS
206 info
->seg
.eo_so_seglenbar
= pkt_len_bar
;
208 cpu_to_be16s(&info
->seg
.flags_seqnum_seglen
);
209 cpu_to_be16s(&info
->seg
.eo_so_seglenbar
);
211 /* Prepare EOP header */
212 info
->eop
.seq_len
= ((info
->seq_num
<< 11) &
213 TX_HDR_EOP_SEQNUM
) | info
->pkt_len
;
214 info
->eop
.seqbar_lenbar
= ((~info
->seq_num
<< 11) &
215 TX_HDR_EOP_SEQNUMBAR
) | pkt_len_bar
;
217 cpu_to_be16s(&info
->eop
.seq_len
);
218 cpu_to_be16s(&info
->eop
.seqbar_lenbar
);
222 ax88796c_check_free_pages(struct ax88796c_device
*ax_local
, u8 need_pages
)
227 lockdep_assert_held(&ax_local
->spi_lock
);
229 free_pages
= AX_READ(&ax_local
->ax_spi
, P0_TFBFCR
) & TX_FREEBUF_MASK
;
230 if (free_pages
< need_pages
) {
231 /* schedule free page interrupt */
232 tmp
= AX_READ(&ax_local
->ax_spi
, P0_TFBFCR
)
233 & TFBFCR_SCHE_FREE_PAGE
;
234 AX_WRITE(&ax_local
->ax_spi
, tmp
| TFBFCR_TX_PAGE_SET
|
235 TFBFCR_SET_FREE_PAGE(need_pages
),
243 static struct sk_buff
*
244 ax88796c_tx_fixup(struct net_device
*ndev
, struct sk_buff_head
*q
)
246 struct ax88796c_device
*ax_local
= to_ax88796c_device(ndev
);
247 u8 spi_len
= ax_local
->ax_spi
.comp
? 1 : 4;
249 struct tx_pkt_info info
;
250 struct skb_data
*entry
;
257 if (skb_queue_empty(q
))
262 need_pages
= (pkt_len
+ TX_OVERHEAD
+ 127) >> 7;
263 if (ax88796c_check_free_pages(ax_local
, need_pages
) != 0)
266 headroom
= skb_headroom(skb
);
267 tailroom
= skb_tailroom(skb
);
268 padlen
= round_up(pkt_len
, 4) - pkt_len
;
269 seq_num
= ++ax_local
->seq_num
& 0x1F;
271 info
.pkt_len
= pkt_len
;
273 if (skb_cloned(skb
) ||
274 (headroom
< (TX_OVERHEAD
+ spi_len
)) ||
275 (tailroom
< (padlen
+ TX_EOP_SIZE
))) {
276 size_t h
= max((TX_OVERHEAD
+ spi_len
) - headroom
, 0);
277 size_t t
= max((padlen
+ TX_EOP_SIZE
) - tailroom
, 0);
279 if (pskb_expand_head(skb
, h
, t
, GFP_KERNEL
))
283 info
.seq_num
= seq_num
;
284 ax88796c_proc_tx_hdr(&info
, skb
->ip_summed
);
286 /* SOP and SEG header */
287 memcpy(skb_push(skb
, TX_OVERHEAD
), &info
.tx_overhead
, TX_OVERHEAD
);
289 /* Write SPI TXQ header */
290 memcpy(skb_push(skb
, spi_len
), ax88796c_tx_cmd_buf
, spi_len
);
292 /* Make 32-bit alignment */
293 skb_put(skb
, padlen
);
296 skb_put_data(skb
, &info
.eop
, TX_EOP_SIZE
);
300 entry
= (struct skb_data
*)skb
->cb
;
301 memset(entry
, 0, sizeof(*entry
));
302 entry
->len
= pkt_len
;
304 if (netif_msg_pktdata(ax_local
)) {
305 char pfx
[IFNAMSIZ
+ 7];
307 snprintf(pfx
, sizeof(pfx
), "%s: ", ndev
->name
);
309 netdev_info(ndev
, "TX packet len %d, total len %d, seq %d\n",
310 pkt_len
, skb
->len
, seq_num
);
312 netdev_info(ndev
, " SPI Header:\n");
313 print_hex_dump(KERN_INFO
, pfx
, DUMP_PREFIX_OFFSET
, 16, 1,
316 netdev_info(ndev
, " TX SOP:\n");
317 print_hex_dump(KERN_INFO
, pfx
, DUMP_PREFIX_OFFSET
, 16, 1,
318 skb
->data
+ 4, TX_OVERHEAD
, 0);
320 netdev_info(ndev
, " TX packet:\n");
321 print_hex_dump(KERN_INFO
, pfx
, DUMP_PREFIX_OFFSET
, 16, 1,
322 skb
->data
+ 4 + TX_OVERHEAD
,
323 skb
->len
- TX_EOP_SIZE
- 4 - TX_OVERHEAD
, 0);
325 netdev_info(ndev
, " TX EOP:\n");
326 print_hex_dump(KERN_INFO
, pfx
, DUMP_PREFIX_OFFSET
, 16, 1,
327 skb
->data
+ skb
->len
- 4, 4, 0);
333 static int ax88796c_hard_xmit(struct ax88796c_device
*ax_local
)
335 struct ax88796c_pcpu_stats
*stats
;
336 struct sk_buff
*tx_skb
;
337 struct skb_data
*entry
;
340 lockdep_assert_held(&ax_local
->spi_lock
);
342 stats
= this_cpu_ptr(ax_local
->stats
);
343 tx_skb
= ax88796c_tx_fixup(ax_local
->ndev
, &ax_local
->tx_wait_q
);
346 this_cpu_inc(ax_local
->stats
->tx_dropped
);
349 entry
= (struct skb_data
*)tx_skb
->cb
;
351 AX_WRITE(&ax_local
->ax_spi
,
352 (TSNR_TXB_START
| TSNR_PKT_CNT(1)), P0_TSNR
);
354 axspi_write_txq(&ax_local
->ax_spi
, tx_skb
->data
, tx_skb
->len
);
356 if (((AX_READ(&ax_local
->ax_spi
, P0_TSNR
) & TXNR_TXB_IDLE
) == 0) ||
357 ((ISR_TXERR
& AX_READ(&ax_local
->ax_spi
, P0_ISR
)) != 0)) {
358 /* Ack tx error int */
359 AX_WRITE(&ax_local
->ax_spi
, ISR_TXERR
, P0_ISR
);
361 this_cpu_inc(ax_local
->stats
->tx_dropped
);
364 netif_err(ax_local
, tx_err
, ax_local
->ndev
,
365 "TX FIFO error, re-initialize the TX bridge\n");
367 /* Reinitial tx bridge */
368 AX_WRITE(&ax_local
->ax_spi
, TXNR_TXB_REINIT
|
369 AX_READ(&ax_local
->ax_spi
, P0_TSNR
), P0_TSNR
);
370 ax_local
->seq_num
= 0;
372 flags
= u64_stats_update_begin_irqsave(&stats
->syncp
);
373 u64_stats_inc(&stats
->tx_packets
);
374 u64_stats_add(&stats
->tx_bytes
, entry
->len
);
375 u64_stats_update_end_irqrestore(&stats
->syncp
, flags
);
378 entry
->state
= tx_done
;
379 dev_kfree_skb(tx_skb
);
385 ax88796c_start_xmit(struct sk_buff
*skb
, struct net_device
*ndev
)
387 struct ax88796c_device
*ax_local
= to_ax88796c_device(ndev
);
389 skb_queue_tail(&ax_local
->tx_wait_q
, skb
);
390 if (skb_queue_len(&ax_local
->tx_wait_q
) > TX_QUEUE_HIGH_WATER
)
391 netif_stop_queue(ndev
);
393 set_bit(EVENT_TX
, &ax_local
->flags
);
394 schedule_work(&ax_local
->ax_work
);
400 ax88796c_skb_return(struct ax88796c_device
*ax_local
,
401 struct sk_buff
*skb
, struct rx_header
*rxhdr
)
403 struct net_device
*ndev
= ax_local
->ndev
;
404 struct ax88796c_pcpu_stats
*stats
;
408 stats
= this_cpu_ptr(ax_local
->stats
);
411 if (!(ndev
->features
& NETIF_F_RXCSUM
))
414 /* checksum error bit is set */
415 if ((rxhdr
->flags
& RX_HDR3_L3_ERR
) ||
416 (rxhdr
->flags
& RX_HDR3_L4_ERR
))
419 /* Other types may be indicated by more than one bit. */
420 if ((rxhdr
->flags
& RX_HDR3_L4_TYPE_TCP
) ||
421 (rxhdr
->flags
& RX_HDR3_L4_TYPE_UDP
))
422 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
425 flags
= u64_stats_update_begin_irqsave(&stats
->syncp
);
426 u64_stats_inc(&stats
->rx_packets
);
427 u64_stats_add(&stats
->rx_bytes
, skb
->len
);
428 u64_stats_update_end_irqrestore(&stats
->syncp
, flags
);
431 skb
->protocol
= eth_type_trans(skb
, ax_local
->ndev
);
433 netif_info(ax_local
, rx_status
, ndev
, "< rx, len %zu, type 0x%x\n",
434 skb
->len
+ sizeof(struct ethhdr
), skb
->protocol
);
436 status
= netif_rx(skb
);
437 if (status
!= NET_RX_SUCCESS
&& net_ratelimit())
438 netif_info(ax_local
, rx_err
, ndev
,
439 "netif_rx status %d\n", status
);
443 ax88796c_rx_fixup(struct ax88796c_device
*ax_local
, struct sk_buff
*rx_skb
)
445 struct rx_header
*rxhdr
= (struct rx_header
*)rx_skb
->data
;
446 struct net_device
*ndev
= ax_local
->ndev
;
449 be16_to_cpus(&rxhdr
->flags_len
);
450 be16_to_cpus(&rxhdr
->seq_lenbar
);
451 be16_to_cpus(&rxhdr
->flags
);
453 if ((rxhdr
->flags_len
& RX_HDR1_PKT_LEN
) !=
454 (~rxhdr
->seq_lenbar
& 0x7FF)) {
455 netif_err(ax_local
, rx_err
, ndev
, "Header error\n");
457 this_cpu_inc(ax_local
->stats
->rx_frame_errors
);
462 if ((rxhdr
->flags_len
& RX_HDR1_MII_ERR
) ||
463 (rxhdr
->flags_len
& RX_HDR1_CRC_ERR
)) {
464 netif_err(ax_local
, rx_err
, ndev
, "CRC or MII error\n");
466 this_cpu_inc(ax_local
->stats
->rx_crc_errors
);
471 len
= rxhdr
->flags_len
& RX_HDR1_PKT_LEN
;
472 if (netif_msg_pktdata(ax_local
)) {
473 char pfx
[IFNAMSIZ
+ 7];
475 snprintf(pfx
, sizeof(pfx
), "%s: ", ndev
->name
);
476 netdev_info(ndev
, "RX data, total len %d, packet len %d\n",
479 netdev_info(ndev
, " Dump RX packet header:");
480 print_hex_dump(KERN_INFO
, pfx
, DUMP_PREFIX_OFFSET
, 16, 1,
481 rx_skb
->data
, sizeof(*rxhdr
), 0);
483 netdev_info(ndev
, " Dump RX packet:");
484 print_hex_dump(KERN_INFO
, pfx
, DUMP_PREFIX_OFFSET
, 16, 1,
485 rx_skb
->data
+ sizeof(*rxhdr
), len
, 0);
488 skb_pull(rx_skb
, sizeof(*rxhdr
));
489 pskb_trim(rx_skb
, len
);
491 ax88796c_skb_return(ax_local
, rx_skb
, rxhdr
);
494 static int ax88796c_receive(struct net_device
*ndev
)
496 struct ax88796c_device
*ax_local
= to_ax88796c_device(ndev
);
497 struct skb_data
*entry
;
498 u16 w_count
, pkt_len
;
502 lockdep_assert_held(&ax_local
->spi_lock
);
504 /* check rx packet and total word count */
505 AX_WRITE(&ax_local
->ax_spi
, AX_READ(&ax_local
->ax_spi
, P0_RTWCR
)
506 | RTWCR_RX_LATCH
, P0_RTWCR
);
508 pkt_cnt
= AX_READ(&ax_local
->ax_spi
, P0_RXBCR2
) & RXBCR2_PKT_MASK
;
512 pkt_len
= AX_READ(&ax_local
->ax_spi
, P0_RCPHR
) & 0x7FF;
514 w_count
= round_up(pkt_len
+ 6, 4) >> 1;
516 skb
= netdev_alloc_skb(ndev
, w_count
* 2);
518 AX_WRITE(&ax_local
->ax_spi
, RXBCR1_RXB_DISCARD
, P0_RXBCR1
);
519 this_cpu_inc(ax_local
->stats
->rx_dropped
);
522 entry
= (struct skb_data
*)skb
->cb
;
524 AX_WRITE(&ax_local
->ax_spi
, RXBCR1_RXB_START
| w_count
, P0_RXBCR1
);
526 axspi_read_rxq(&ax_local
->ax_spi
,
527 skb_put(skb
, w_count
* 2), skb
->len
);
529 /* Check if rx bridge is idle */
530 if ((AX_READ(&ax_local
->ax_spi
, P0_RXBCR2
) & RXBCR2_RXB_IDLE
) == 0) {
532 netif_err(ax_local
, rx_err
, ndev
,
533 "Rx Bridge is not idle\n");
534 AX_WRITE(&ax_local
->ax_spi
, RXBCR2_RXB_REINIT
, P0_RXBCR2
);
536 entry
->state
= rx_err
;
538 entry
->state
= rx_done
;
541 AX_WRITE(&ax_local
->ax_spi
, ISR_RXPKT
, P0_ISR
);
543 ax88796c_rx_fixup(ax_local
, skb
);
548 static int ax88796c_process_isr(struct ax88796c_device
*ax_local
)
550 struct net_device
*ndev
= ax_local
->ndev
;
554 lockdep_assert_held(&ax_local
->spi_lock
);
556 isr
= AX_READ(&ax_local
->ax_spi
, P0_ISR
);
557 AX_WRITE(&ax_local
->ax_spi
, isr
, P0_ISR
);
559 netif_dbg(ax_local
, intr
, ndev
, " ISR 0x%04x\n", isr
);
561 if (isr
& ISR_TXERR
) {
562 netif_dbg(ax_local
, intr
, ndev
, " TXERR interrupt\n");
563 AX_WRITE(&ax_local
->ax_spi
, TXNR_TXB_REINIT
, P0_TSNR
);
564 ax_local
->seq_num
= 0x1f;
567 if (isr
& ISR_TXPAGES
) {
568 netif_dbg(ax_local
, intr
, ndev
, " TXPAGES interrupt\n");
569 set_bit(EVENT_TX
, &ax_local
->flags
);
572 if (isr
& ISR_LINK
) {
573 netif_dbg(ax_local
, intr
, ndev
, " Link change interrupt\n");
574 phy_mac_interrupt(ax_local
->ndev
->phydev
);
577 if (isr
& ISR_RXPKT
) {
578 netif_dbg(ax_local
, intr
, ndev
, " RX interrupt\n");
579 todo
= ax88796c_receive(ax_local
->ndev
);
585 static irqreturn_t
ax88796c_interrupt(int irq
, void *dev_instance
)
587 struct ax88796c_device
*ax_local
;
588 struct net_device
*ndev
;
592 pr_err("irq %d for unknown device.\n", irq
);
593 return IRQ_RETVAL(0);
595 ax_local
= to_ax88796c_device(ndev
);
597 disable_irq_nosync(irq
);
599 netif_dbg(ax_local
, intr
, ndev
, "Interrupt occurred\n");
601 set_bit(EVENT_INTR
, &ax_local
->flags
);
602 schedule_work(&ax_local
->ax_work
);
607 static void ax88796c_work(struct work_struct
*work
)
609 struct ax88796c_device
*ax_local
=
610 container_of(work
, struct ax88796c_device
, ax_work
);
612 mutex_lock(&ax_local
->spi_lock
);
614 if (test_bit(EVENT_SET_MULTI
, &ax_local
->flags
)) {
615 ax88796c_set_hw_multicast(ax_local
->ndev
);
616 clear_bit(EVENT_SET_MULTI
, &ax_local
->flags
);
619 if (test_bit(EVENT_INTR
, &ax_local
->flags
)) {
620 AX_WRITE(&ax_local
->ax_spi
, IMR_MASKALL
, P0_IMR
);
622 while (ax88796c_process_isr(ax_local
))
625 clear_bit(EVENT_INTR
, &ax_local
->flags
);
627 AX_WRITE(&ax_local
->ax_spi
, IMR_DEFAULT
, P0_IMR
);
629 enable_irq(ax_local
->ndev
->irq
);
632 if (test_bit(EVENT_TX
, &ax_local
->flags
)) {
633 while (skb_queue_len(&ax_local
->tx_wait_q
)) {
634 if (!ax88796c_hard_xmit(ax_local
))
638 clear_bit(EVENT_TX
, &ax_local
->flags
);
640 if (netif_queue_stopped(ax_local
->ndev
) &&
641 (skb_queue_len(&ax_local
->tx_wait_q
) < TX_QUEUE_LOW_WATER
))
642 netif_wake_queue(ax_local
->ndev
);
645 mutex_unlock(&ax_local
->spi_lock
);
648 static void ax88796c_get_stats64(struct net_device
*ndev
,
649 struct rtnl_link_stats64
*stats
)
651 struct ax88796c_device
*ax_local
= to_ax88796c_device(ndev
);
652 u32 rx_frame_errors
= 0, rx_crc_errors
= 0;
653 u32 rx_dropped
= 0, tx_dropped
= 0;
657 for_each_possible_cpu(cpu
) {
658 struct ax88796c_pcpu_stats
*s
;
659 u64 rx_packets
, rx_bytes
;
660 u64 tx_packets
, tx_bytes
;
662 s
= per_cpu_ptr(ax_local
->stats
, cpu
);
665 start
= u64_stats_fetch_begin(&s
->syncp
);
666 rx_packets
= u64_stats_read(&s
->rx_packets
);
667 rx_bytes
= u64_stats_read(&s
->rx_bytes
);
668 tx_packets
= u64_stats_read(&s
->tx_packets
);
669 tx_bytes
= u64_stats_read(&s
->tx_bytes
);
670 } while (u64_stats_fetch_retry(&s
->syncp
, start
));
672 stats
->rx_packets
+= rx_packets
;
673 stats
->rx_bytes
+= rx_bytes
;
674 stats
->tx_packets
+= tx_packets
;
675 stats
->tx_bytes
+= tx_bytes
;
677 rx_dropped
+= s
->rx_dropped
;
678 tx_dropped
+= s
->tx_dropped
;
679 rx_frame_errors
+= s
->rx_frame_errors
;
680 rx_crc_errors
+= s
->rx_crc_errors
;
683 stats
->rx_dropped
= rx_dropped
;
684 stats
->tx_dropped
= tx_dropped
;
685 stats
->rx_frame_errors
= rx_frame_errors
;
686 stats
->rx_crc_errors
= rx_crc_errors
;
689 static void ax88796c_set_mac(struct ax88796c_device
*ax_local
)
693 maccr
= (ax_local
->link
) ? MACCR_RXEN
: 0;
695 switch (ax_local
->speed
) {
697 maccr
|= MACCR_SPEED_100
;
706 switch (ax_local
->duplex
) {
708 maccr
|= MACCR_SPEED_100
;
717 if (ax_local
->flowctrl
& AX_FC_ANEG
&&
718 ax_local
->phydev
->autoneg
) {
719 maccr
|= ax_local
->pause
? MACCR_RXFC_ENABLE
: 0;
720 maccr
|= !ax_local
->pause
!= !ax_local
->asym_pause
?
721 MACCR_TXFC_ENABLE
: 0;
723 maccr
|= (ax_local
->flowctrl
& AX_FC_RX
) ? MACCR_RXFC_ENABLE
: 0;
724 maccr
|= (ax_local
->flowctrl
& AX_FC_TX
) ? MACCR_TXFC_ENABLE
: 0;
727 mutex_lock(&ax_local
->spi_lock
);
729 maccr
|= AX_READ(&ax_local
->ax_spi
, P0_MACCR
) &
730 ~(MACCR_DUPLEX_FULL
| MACCR_SPEED_100
|
731 MACCR_TXFC_ENABLE
| MACCR_RXFC_ENABLE
);
732 AX_WRITE(&ax_local
->ax_spi
, maccr
, P0_MACCR
);
734 mutex_unlock(&ax_local
->spi_lock
);
737 static void ax88796c_handle_link_change(struct net_device
*ndev
)
739 struct ax88796c_device
*ax_local
= to_ax88796c_device(ndev
);
740 struct phy_device
*phydev
= ndev
->phydev
;
743 if (phydev
->link
&& (ax_local
->speed
!= phydev
->speed
||
744 ax_local
->duplex
!= phydev
->duplex
||
745 ax_local
->pause
!= phydev
->pause
||
746 ax_local
->asym_pause
!= phydev
->asym_pause
)) {
747 ax_local
->speed
= phydev
->speed
;
748 ax_local
->duplex
= phydev
->duplex
;
749 ax_local
->pause
= phydev
->pause
;
750 ax_local
->asym_pause
= phydev
->asym_pause
;
754 if (phydev
->link
!= ax_local
->link
) {
756 ax_local
->speed
= SPEED_UNKNOWN
;
757 ax_local
->duplex
= DUPLEX_UNKNOWN
;
760 ax_local
->link
= phydev
->link
;
765 ax88796c_set_mac(ax_local
);
768 phy_print_status(ndev
->phydev
);
771 static void ax88796c_set_csums(struct ax88796c_device
*ax_local
)
773 struct net_device
*ndev
= ax_local
->ndev
;
775 lockdep_assert_held(&ax_local
->spi_lock
);
777 if (ndev
->features
& NETIF_F_RXCSUM
) {
778 AX_WRITE(&ax_local
->ax_spi
, COERCR0_DEFAULT
, P4_COERCR0
);
779 AX_WRITE(&ax_local
->ax_spi
, COERCR1_DEFAULT
, P4_COERCR1
);
781 AX_WRITE(&ax_local
->ax_spi
, 0, P4_COERCR0
);
782 AX_WRITE(&ax_local
->ax_spi
, 0, P4_COERCR1
);
785 if (ndev
->features
& NETIF_F_HW_CSUM
) {
786 AX_WRITE(&ax_local
->ax_spi
, COETCR0_DEFAULT
, P4_COETCR0
);
787 AX_WRITE(&ax_local
->ax_spi
, COETCR1_TXPPPE
, P4_COETCR1
);
789 AX_WRITE(&ax_local
->ax_spi
, 0, P4_COETCR0
);
790 AX_WRITE(&ax_local
->ax_spi
, 0, P4_COETCR1
);
795 ax88796c_open(struct net_device
*ndev
)
797 struct ax88796c_device
*ax_local
= to_ax88796c_device(ndev
);
798 unsigned long irq_flag
= 0;
803 ret
= request_irq(ndev
->irq
, ax88796c_interrupt
,
804 irq_flag
, ndev
->name
, ndev
);
806 netdev_err(ndev
, "unable to get IRQ %d (errno=%d).\n",
811 mutex_lock(&ax_local
->spi_lock
);
813 ret
= ax88796c_soft_reset(ax_local
);
815 free_irq(ndev
->irq
, ndev
);
816 mutex_unlock(&ax_local
->spi_lock
);
819 ax_local
->seq_num
= 0x1f;
821 ax88796c_set_mac_addr(ndev
);
822 ax88796c_set_csums(ax_local
);
824 /* Disable stuffing packet */
825 t
= AX_READ(&ax_local
->ax_spi
, P1_RXBSPCR
);
826 t
&= ~RXBSPCR_STUF_ENABLE
;
827 AX_WRITE(&ax_local
->ax_spi
, t
, P1_RXBSPCR
);
829 /* Enable RX packet process */
830 AX_WRITE(&ax_local
->ax_spi
, RPPER_RXEN
, P1_RPPER
);
832 t
= AX_READ(&ax_local
->ax_spi
, P0_FER
);
833 t
|= FER_RXEN
| FER_TXEN
| FER_BSWAP
| FER_IRQ_PULL
;
834 AX_WRITE(&ax_local
->ax_spi
, t
, P0_FER
);
837 AX_WRITE(&ax_local
->ax_spi
,
838 (LCR_LED0_EN
| LCR_LED0_DUPLEX
| LCR_LED1_EN
|
839 LCR_LED1_100MODE
), P2_LCR0
);
840 AX_WRITE(&ax_local
->ax_spi
,
841 (AX_READ(&ax_local
->ax_spi
, P2_LCR1
) & LCR_LED2_MASK
) |
842 LCR_LED2_EN
| LCR_LED2_LINK
, P2_LCR1
);
844 /* Disable PHY auto-polling */
845 AX_WRITE(&ax_local
->ax_spi
, PCR_PHYID(AX88796C_PHY_ID
), P2_PCR
);
847 /* Enable MAC interrupts */
848 AX_WRITE(&ax_local
->ax_spi
, IMR_DEFAULT
, P0_IMR
);
850 mutex_unlock(&ax_local
->spi_lock
);
852 /* Setup flow-control configuration */
853 phy_support_asym_pause(ax_local
->phydev
);
855 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT
,
856 ax_local
->phydev
->advertising
) ||
857 linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT
,
858 ax_local
->phydev
->advertising
))
861 fc
|= linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT
,
862 ax_local
->phydev
->advertising
) ? AX_FC_RX
: 0;
863 fc
|= (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT
,
864 ax_local
->phydev
->advertising
) !=
865 linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT
,
866 ax_local
->phydev
->advertising
)) ? AX_FC_TX
: 0;
867 ax_local
->flowctrl
= fc
;
869 phy_start(ax_local
->ndev
->phydev
);
871 netif_start_queue(ndev
);
873 spi_message_init(&ax_local
->ax_spi
.rx_msg
);
879 ax88796c_close(struct net_device
*ndev
)
881 struct ax88796c_device
*ax_local
= to_ax88796c_device(ndev
);
883 phy_stop(ndev
->phydev
);
885 /* We lock the mutex early not only to protect the device
886 * against concurrent access, but also avoid waking up the
887 * queue in ax88796c_work(). phy_stop() needs to be called
888 * before because it locks the mutex to access SPI.
890 mutex_lock(&ax_local
->spi_lock
);
892 netif_stop_queue(ndev
);
894 /* No more work can be scheduled now. Make any pending work,
895 * including one already waiting for the mutex to be unlocked,
898 netif_dbg(ax_local
, ifdown
, ndev
, "clearing bits\n");
899 clear_bit(EVENT_SET_MULTI
, &ax_local
->flags
);
900 clear_bit(EVENT_INTR
, &ax_local
->flags
);
901 clear_bit(EVENT_TX
, &ax_local
->flags
);
903 /* Disable MAC interrupts */
904 AX_WRITE(&ax_local
->ax_spi
, IMR_MASKALL
, P0_IMR
);
905 __skb_queue_purge(&ax_local
->tx_wait_q
);
906 ax88796c_soft_reset(ax_local
);
908 mutex_unlock(&ax_local
->spi_lock
);
910 cancel_work_sync(&ax_local
->ax_work
);
912 free_irq(ndev
->irq
, ndev
);
918 ax88796c_set_features(struct net_device
*ndev
, netdev_features_t features
)
920 struct ax88796c_device
*ax_local
= to_ax88796c_device(ndev
);
921 netdev_features_t changed
= features
^ ndev
->features
;
923 if (!(changed
& (NETIF_F_RXCSUM
| NETIF_F_HW_CSUM
)))
926 ndev
->features
= features
;
928 if (changed
& (NETIF_F_RXCSUM
| NETIF_F_HW_CSUM
))
929 ax88796c_set_csums(ax_local
);
934 static const struct net_device_ops ax88796c_netdev_ops
= {
935 .ndo_open
= ax88796c_open
,
936 .ndo_stop
= ax88796c_close
,
937 .ndo_start_xmit
= ax88796c_start_xmit
,
938 .ndo_get_stats64
= ax88796c_get_stats64
,
939 .ndo_eth_ioctl
= ax88796c_ioctl
,
940 .ndo_set_mac_address
= eth_mac_addr
,
941 .ndo_set_features
= ax88796c_set_features
,
944 static int ax88796c_hard_reset(struct ax88796c_device
*ax_local
)
946 struct device
*dev
= (struct device
*)&ax_local
->spi
->dev
;
947 struct gpio_desc
*reset_gpio
;
950 reset_gpio
= gpiod_get(dev
, "reset", 0);
951 if (IS_ERR(reset_gpio
)) {
952 dev_err(dev
, "Could not get 'reset' GPIO: %ld", PTR_ERR(reset_gpio
));
953 return PTR_ERR(reset_gpio
);
957 gpiod_direction_output(reset_gpio
, 1);
959 gpiod_direction_output(reset_gpio
, 0);
960 gpiod_put(reset_gpio
);
966 static int ax88796c_probe(struct spi_device
*spi
)
968 char phy_id
[MII_BUS_ID_SIZE
+ 3];
969 struct ax88796c_device
*ax_local
;
970 struct net_device
*ndev
;
974 ndev
= devm_alloc_etherdev(&spi
->dev
, sizeof(*ax_local
));
978 SET_NETDEV_DEV(ndev
, &spi
->dev
);
980 ax_local
= to_ax88796c_device(ndev
);
982 dev_set_drvdata(&spi
->dev
, ax_local
);
984 ax_local
->ax_spi
.spi
= spi
;
987 devm_netdev_alloc_pcpu_stats(&spi
->dev
,
988 struct ax88796c_pcpu_stats
);
989 if (!ax_local
->stats
)
992 ax_local
->ndev
= ndev
;
993 ax_local
->priv_flags
|= comp
? AX_CAP_COMP
: 0;
994 ax_local
->msg_enable
= msg_enable
;
995 mutex_init(&ax_local
->spi_lock
);
997 ax_local
->mdiobus
= devm_mdiobus_alloc(&spi
->dev
);
998 if (!ax_local
->mdiobus
)
1001 ax_local
->mdiobus
->priv
= ax_local
;
1002 ax_local
->mdiobus
->read
= ax88796c_mdio_read
;
1003 ax_local
->mdiobus
->write
= ax88796c_mdio_write
;
1004 ax_local
->mdiobus
->name
= "ax88976c-mdiobus";
1005 ax_local
->mdiobus
->phy_mask
= (u32
)~BIT(AX88796C_PHY_ID
);
1006 ax_local
->mdiobus
->parent
= &spi
->dev
;
1008 snprintf(ax_local
->mdiobus
->id
, MII_BUS_ID_SIZE
,
1009 "ax88796c-%s.%u", dev_name(&spi
->dev
), spi_get_chipselect(spi
, 0));
1011 ret
= devm_mdiobus_register(&spi
->dev
, ax_local
->mdiobus
);
1013 dev_err(&spi
->dev
, "Could not register MDIO bus\n");
1017 if (netif_msg_probe(ax_local
)) {
1018 dev_info(&spi
->dev
, "AX88796C-SPI Configuration:\n");
1019 dev_info(&spi
->dev
, " Compression : %s\n",
1020 ax_local
->priv_flags
& AX_CAP_COMP
? "ON" : "OFF");
1023 ndev
->irq
= spi
->irq
;
1024 ndev
->netdev_ops
= &ax88796c_netdev_ops
;
1025 ndev
->ethtool_ops
= &ax88796c_ethtool_ops
;
1026 ndev
->hw_features
|= NETIF_F_HW_CSUM
| NETIF_F_RXCSUM
;
1027 ndev
->features
|= NETIF_F_HW_CSUM
| NETIF_F_RXCSUM
;
1028 ndev
->needed_headroom
= TX_OVERHEAD
;
1029 ndev
->needed_tailroom
= TX_EOP_SIZE
;
1031 mutex_lock(&ax_local
->spi_lock
);
1033 /* ax88796c gpio reset */
1034 ax88796c_hard_reset(ax_local
);
1036 /* Reset AX88796C */
1037 ret
= ax88796c_soft_reset(ax_local
);
1040 mutex_unlock(&ax_local
->spi_lock
);
1043 /* Check board revision */
1044 temp
= AX_READ(&ax_local
->ax_spi
, P2_CRIR
);
1045 if ((temp
& 0xF) != 0x0) {
1046 dev_err(&spi
->dev
, "spi read failed: %d\n", temp
);
1048 mutex_unlock(&ax_local
->spi_lock
);
1053 ax88796c_reload_eeprom(ax_local
);
1055 ax88796c_load_mac_addr(ndev
);
1057 if (netif_msg_probe(ax_local
))
1059 "irq %d, MAC addr %02X:%02X:%02X:%02X:%02X:%02X\n",
1061 ndev
->dev_addr
[0], ndev
->dev_addr
[1],
1062 ndev
->dev_addr
[2], ndev
->dev_addr
[3],
1063 ndev
->dev_addr
[4], ndev
->dev_addr
[5]);
1065 /* Disable power saving */
1066 AX_WRITE(&ax_local
->ax_spi
, (AX_READ(&ax_local
->ax_spi
, P0_PSCR
)
1067 & PSCR_PS_MASK
) | PSCR_PS_D0
, P0_PSCR
);
1069 mutex_unlock(&ax_local
->spi_lock
);
1071 INIT_WORK(&ax_local
->ax_work
, ax88796c_work
);
1073 skb_queue_head_init(&ax_local
->tx_wait_q
);
1075 snprintf(phy_id
, MII_BUS_ID_SIZE
+ 3, PHY_ID_FMT
,
1076 ax_local
->mdiobus
->id
, AX88796C_PHY_ID
);
1077 ax_local
->phydev
= phy_connect(ax_local
->ndev
, phy_id
,
1078 ax88796c_handle_link_change
,
1079 PHY_INTERFACE_MODE_MII
);
1080 if (IS_ERR(ax_local
->phydev
)) {
1081 ret
= PTR_ERR(ax_local
->phydev
);
1084 ax_local
->phydev
->irq
= PHY_POLL
;
1086 ret
= devm_register_netdev(&spi
->dev
, ndev
);
1088 dev_err(&spi
->dev
, "failed to register a network device\n");
1092 netif_info(ax_local
, probe
, ndev
, "%s %s registered\n",
1093 dev_driver_string(&spi
->dev
),
1094 dev_name(&spi
->dev
));
1095 phy_attached_info(ax_local
->phydev
);
1100 phy_disconnect(ax_local
->phydev
);
1105 static void ax88796c_remove(struct spi_device
*spi
)
1107 struct ax88796c_device
*ax_local
= dev_get_drvdata(&spi
->dev
);
1108 struct net_device
*ndev
= ax_local
->ndev
;
1110 phy_disconnect(ndev
->phydev
);
1112 netif_info(ax_local
, probe
, ndev
, "removing network device %s %s\n",
1113 dev_driver_string(&spi
->dev
),
1114 dev_name(&spi
->dev
));
1118 static const struct of_device_id ax88796c_dt_ids
[] = {
1119 { .compatible
= "asix,ax88796c" },
1122 MODULE_DEVICE_TABLE(of
, ax88796c_dt_ids
);
1125 static const struct spi_device_id asix_id
[] = {
1129 MODULE_DEVICE_TABLE(spi
, asix_id
);
1131 static struct spi_driver ax88796c_spi_driver
= {
1134 .of_match_table
= of_match_ptr(ax88796c_dt_ids
),
1136 .probe
= ax88796c_probe
,
1137 .remove
= ax88796c_remove
,
1138 .id_table
= asix_id
,
1141 static __init
int ax88796c_spi_init(void)
1145 bitmap_zero(ax88796c_no_regs_mask
, AX88796C_REGDUMP_LEN
);
1146 ret
= bitmap_parse(no_regs_list
, 35,
1147 ax88796c_no_regs_mask
, AX88796C_REGDUMP_LEN
);
1149 bitmap_fill(ax88796c_no_regs_mask
, AX88796C_REGDUMP_LEN
);
1150 pr_err("Invalid bitmap description, masking all registers\n");
1153 return spi_register_driver(&ax88796c_spi_driver
);
1156 static __exit
void ax88796c_spi_exit(void)
1158 spi_unregister_driver(&ax88796c_spi_driver
);
1161 module_init(ax88796c_spi_init
);
1162 module_exit(ax88796c_spi_exit
);
1164 MODULE_AUTHOR("Ćukasz Stelmach <l.stelmach@samsung.com>");
1165 MODULE_DESCRIPTION("ASIX AX88796C SPI Ethernet driver");
1166 MODULE_LICENSE("GPL");