2 * Copyright (C) 2015 Mans Rullgard <mans@mansr.com>
4 * Mostly rewritten, based on driver from Sigma Designs. Original
5 * copyright notice below.
8 * Driver for tangox SMP864x/SMP865x/SMP867x/SMP868x builtin Ethernet Mac.
10 * Copyright (C) 2005 Maxime Bizon <mbizon@freebox.fr>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
23 #include <linux/module.h>
24 #include <linux/etherdevice.h>
25 #include <linux/delay.h>
26 #include <linux/ethtool.h>
27 #include <linux/interrupt.h>
28 #include <linux/platform_device.h>
29 #include <linux/of_device.h>
30 #include <linux/of_mdio.h>
31 #include <linux/of_net.h>
32 #include <linux/dma-mapping.h>
33 #include <linux/phy.h>
34 #include <linux/cache.h>
35 #include <linux/jiffies.h>
37 #include <linux/iopoll.h>
38 #include <asm/barrier.h>
42 static void nb8800_tx_done(struct net_device
*dev
);
43 static int nb8800_dma_stop(struct net_device
*dev
);
45 static inline u8
nb8800_readb(struct nb8800_priv
*priv
, int reg
)
47 return readb_relaxed(priv
->base
+ reg
);
50 static inline u32
nb8800_readl(struct nb8800_priv
*priv
, int reg
)
52 return readl_relaxed(priv
->base
+ reg
);
55 static inline void nb8800_writeb(struct nb8800_priv
*priv
, int reg
, u8 val
)
57 writeb_relaxed(val
, priv
->base
+ reg
);
60 static inline void nb8800_writew(struct nb8800_priv
*priv
, int reg
, u16 val
)
62 writew_relaxed(val
, priv
->base
+ reg
);
65 static inline void nb8800_writel(struct nb8800_priv
*priv
, int reg
, u32 val
)
67 writel_relaxed(val
, priv
->base
+ reg
);
70 static inline void nb8800_maskb(struct nb8800_priv
*priv
, int reg
,
73 u32 old
= nb8800_readb(priv
, reg
);
74 u32
new = (old
& ~mask
) | (val
& mask
);
77 nb8800_writeb(priv
, reg
, new);
80 static inline void nb8800_maskl(struct nb8800_priv
*priv
, int reg
,
83 u32 old
= nb8800_readl(priv
, reg
);
84 u32
new = (old
& ~mask
) | (val
& mask
);
87 nb8800_writel(priv
, reg
, new);
90 static inline void nb8800_modb(struct nb8800_priv
*priv
, int reg
, u8 bits
,
93 nb8800_maskb(priv
, reg
, bits
, set
? bits
: 0);
96 static inline void nb8800_setb(struct nb8800_priv
*priv
, int reg
, u8 bits
)
98 nb8800_maskb(priv
, reg
, bits
, bits
);
101 static inline void nb8800_clearb(struct nb8800_priv
*priv
, int reg
, u8 bits
)
103 nb8800_maskb(priv
, reg
, bits
, 0);
106 static inline void nb8800_modl(struct nb8800_priv
*priv
, int reg
, u32 bits
,
109 nb8800_maskl(priv
, reg
, bits
, set
? bits
: 0);
112 static inline void nb8800_setl(struct nb8800_priv
*priv
, int reg
, u32 bits
)
114 nb8800_maskl(priv
, reg
, bits
, bits
);
117 static inline void nb8800_clearl(struct nb8800_priv
*priv
, int reg
, u32 bits
)
119 nb8800_maskl(priv
, reg
, bits
, 0);
122 static int nb8800_mdio_wait(struct mii_bus
*bus
)
124 struct nb8800_priv
*priv
= bus
->priv
;
127 return readl_poll_timeout_atomic(priv
->base
+ NB8800_MDIO_CMD
,
128 val
, !(val
& MDIO_CMD_GO
), 1, 1000);
131 static int nb8800_mdio_cmd(struct mii_bus
*bus
, u32 cmd
)
133 struct nb8800_priv
*priv
= bus
->priv
;
136 err
= nb8800_mdio_wait(bus
);
140 nb8800_writel(priv
, NB8800_MDIO_CMD
, cmd
);
142 nb8800_writel(priv
, NB8800_MDIO_CMD
, cmd
| MDIO_CMD_GO
);
144 return nb8800_mdio_wait(bus
);
147 static int nb8800_mdio_read(struct mii_bus
*bus
, int phy_id
, int reg
)
149 struct nb8800_priv
*priv
= bus
->priv
;
153 err
= nb8800_mdio_cmd(bus
, MDIO_CMD_ADDR(phy_id
) | MDIO_CMD_REG(reg
));
157 val
= nb8800_readl(priv
, NB8800_MDIO_STS
);
158 if (val
& MDIO_STS_ERR
)
164 static int nb8800_mdio_write(struct mii_bus
*bus
, int phy_id
, int reg
, u16 val
)
166 u32 cmd
= MDIO_CMD_ADDR(phy_id
) | MDIO_CMD_REG(reg
) |
167 MDIO_CMD_DATA(val
) | MDIO_CMD_WR
;
169 return nb8800_mdio_cmd(bus
, cmd
);
172 static void nb8800_mac_tx(struct net_device
*dev
, bool enable
)
174 struct nb8800_priv
*priv
= netdev_priv(dev
);
176 while (nb8800_readl(priv
, NB8800_TXC_CR
) & TCR_EN
)
179 nb8800_modb(priv
, NB8800_TX_CTL1
, TX_EN
, enable
);
182 static void nb8800_mac_rx(struct net_device
*dev
, bool enable
)
184 nb8800_modb(netdev_priv(dev
), NB8800_RX_CTL
, RX_EN
, enable
);
187 static void nb8800_mac_af(struct net_device
*dev
, bool enable
)
189 nb8800_modb(netdev_priv(dev
), NB8800_RX_CTL
, RX_AF_EN
, enable
);
192 static void nb8800_start_rx(struct net_device
*dev
)
194 nb8800_setl(netdev_priv(dev
), NB8800_RXC_CR
, RCR_EN
);
197 static int nb8800_alloc_rx(struct net_device
*dev
, unsigned int i
, bool napi
)
199 struct nb8800_priv
*priv
= netdev_priv(dev
);
200 struct nb8800_rx_desc
*rxd
= &priv
->rx_descs
[i
];
201 struct nb8800_rx_buf
*rxb
= &priv
->rx_bufs
[i
];
202 int size
= L1_CACHE_ALIGN(RX_BUF_SIZE
);
205 unsigned long offset
;
208 data
= napi
? napi_alloc_frag(size
) : netdev_alloc_frag(size
);
212 page
= virt_to_head_page(data
);
213 offset
= data
- page_address(page
);
215 dma_addr
= dma_map_page(&dev
->dev
, page
, offset
, RX_BUF_SIZE
,
218 if (dma_mapping_error(&dev
->dev
, dma_addr
)) {
224 rxb
->offset
= offset
;
225 rxd
->desc
.s_addr
= dma_addr
;
230 static void nb8800_receive(struct net_device
*dev
, unsigned int i
,
233 struct nb8800_priv
*priv
= netdev_priv(dev
);
234 struct nb8800_rx_desc
*rxd
= &priv
->rx_descs
[i
];
235 struct page
*page
= priv
->rx_bufs
[i
].page
;
236 int offset
= priv
->rx_bufs
[i
].offset
;
237 void *data
= page_address(page
) + offset
;
238 dma_addr_t dma
= rxd
->desc
.s_addr
;
243 size
= len
<= RX_COPYBREAK
? len
: RX_COPYHDR
;
245 skb
= napi_alloc_skb(&priv
->napi
, size
);
247 netdev_err(dev
, "rx skb allocation failed\n");
248 dev
->stats
.rx_dropped
++;
252 if (len
<= RX_COPYBREAK
) {
253 dma_sync_single_for_cpu(&dev
->dev
, dma
, len
, DMA_FROM_DEVICE
);
254 memcpy(skb_put(skb
, len
), data
, len
);
255 dma_sync_single_for_device(&dev
->dev
, dma
, len
,
258 err
= nb8800_alloc_rx(dev
, i
, true);
260 netdev_err(dev
, "rx buffer allocation failed\n");
261 dev
->stats
.rx_dropped
++;
265 dma_unmap_page(&dev
->dev
, dma
, RX_BUF_SIZE
, DMA_FROM_DEVICE
);
266 memcpy(skb_put(skb
, RX_COPYHDR
), data
, RX_COPYHDR
);
267 skb_add_rx_frag(skb
, skb_shinfo(skb
)->nr_frags
, page
,
268 offset
+ RX_COPYHDR
, len
- RX_COPYHDR
,
272 skb
->protocol
= eth_type_trans(skb
, dev
);
273 napi_gro_receive(&priv
->napi
, skb
);
276 static void nb8800_rx_error(struct net_device
*dev
, u32 report
)
278 if (report
& RX_LENGTH_ERR
)
279 dev
->stats
.rx_length_errors
++;
281 if (report
& RX_FCS_ERR
)
282 dev
->stats
.rx_crc_errors
++;
284 if (report
& RX_FIFO_OVERRUN
)
285 dev
->stats
.rx_fifo_errors
++;
287 if (report
& RX_ALIGNMENT_ERROR
)
288 dev
->stats
.rx_frame_errors
++;
290 dev
->stats
.rx_errors
++;
293 static int nb8800_poll(struct napi_struct
*napi
, int budget
)
295 struct net_device
*dev
= napi
->dev
;
296 struct nb8800_priv
*priv
= netdev_priv(dev
);
297 struct nb8800_rx_desc
*rxd
;
298 unsigned int last
= priv
->rx_eoc
;
306 struct nb8800_rx_buf
*rxb
;
309 next
= (last
+ 1) % RX_DESC_COUNT
;
311 rxb
= &priv
->rx_bufs
[next
];
312 rxd
= &priv
->rx_descs
[next
];
317 len
= RX_BYTES_TRANSFERRED(rxd
->report
);
319 if (IS_RX_ERROR(rxd
->report
))
320 nb8800_rx_error(dev
, rxd
->report
);
322 nb8800_receive(dev
, next
, len
);
324 dev
->stats
.rx_packets
++;
325 dev
->stats
.rx_bytes
+= len
;
327 if (rxd
->report
& RX_MULTICAST_PKT
)
328 dev
->stats
.multicast
++;
333 } while (work
< budget
);
336 priv
->rx_descs
[last
].desc
.config
|= DESC_EOC
;
337 wmb(); /* ensure new EOC is written before clearing old */
338 priv
->rx_descs
[priv
->rx_eoc
].desc
.config
&= ~DESC_EOC
;
340 nb8800_start_rx(dev
);
344 nb8800_writel(priv
, NB8800_RX_ITR
, priv
->rx_itr_irq
);
346 /* If a packet arrived after we last checked but
347 * before writing RX_ITR, the interrupt will be
348 * delayed, so we retrieve it now.
350 if (priv
->rx_descs
[next
].report
)
353 napi_complete_done(napi
, work
);
359 static void __nb8800_tx_dma_start(struct net_device
*dev
)
361 struct nb8800_priv
*priv
= netdev_priv(dev
);
362 struct nb8800_tx_buf
*txb
;
365 txb
= &priv
->tx_bufs
[priv
->tx_queue
];
369 txc_cr
= nb8800_readl(priv
, NB8800_TXC_CR
);
373 nb8800_writel(priv
, NB8800_TX_DESC_ADDR
, txb
->dma_desc
);
374 wmb(); /* ensure desc addr is written before starting DMA */
375 nb8800_writel(priv
, NB8800_TXC_CR
, txc_cr
| TCR_EN
);
377 priv
->tx_queue
= (priv
->tx_queue
+ txb
->chain_len
) % TX_DESC_COUNT
;
380 static void nb8800_tx_dma_start(struct net_device
*dev
)
382 struct nb8800_priv
*priv
= netdev_priv(dev
);
384 spin_lock_irq(&priv
->tx_lock
);
385 __nb8800_tx_dma_start(dev
);
386 spin_unlock_irq(&priv
->tx_lock
);
389 static void nb8800_tx_dma_start_irq(struct net_device
*dev
)
391 struct nb8800_priv
*priv
= netdev_priv(dev
);
393 spin_lock(&priv
->tx_lock
);
394 __nb8800_tx_dma_start(dev
);
395 spin_unlock(&priv
->tx_lock
);
398 static int nb8800_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
400 struct nb8800_priv
*priv
= netdev_priv(dev
);
401 struct nb8800_tx_desc
*txd
;
402 struct nb8800_tx_buf
*txb
;
403 struct nb8800_dma_desc
*desc
;
405 unsigned int dma_len
;
409 if (atomic_read(&priv
->tx_free
) <= NB8800_DESC_LOW
) {
410 netif_stop_queue(dev
);
411 return NETDEV_TX_BUSY
;
414 align
= (8 - (uintptr_t)skb
->data
) & 7;
416 dma_len
= skb
->len
- align
;
417 dma_addr
= dma_map_single(&dev
->dev
, skb
->data
+ align
,
418 dma_len
, DMA_TO_DEVICE
);
420 if (dma_mapping_error(&dev
->dev
, dma_addr
)) {
421 netdev_err(dev
, "tx dma mapping error\n");
423 dev
->stats
.tx_dropped
++;
427 if (atomic_dec_return(&priv
->tx_free
) <= NB8800_DESC_LOW
) {
428 netif_stop_queue(dev
);
432 next
= priv
->tx_next
;
433 txb
= &priv
->tx_bufs
[next
];
434 txd
= &priv
->tx_descs
[next
];
435 desc
= &txd
->desc
[0];
437 next
= (next
+ 1) % TX_DESC_COUNT
;
440 memcpy(txd
->buf
, skb
->data
, align
);
443 txb
->dma_desc
+ offsetof(struct nb8800_tx_desc
, buf
);
444 desc
->n_addr
= txb
->dma_desc
+ sizeof(txd
->desc
[0]);
445 desc
->config
= DESC_BTS(2) | DESC_DS
| align
;
450 desc
->s_addr
= dma_addr
;
451 desc
->n_addr
= priv
->tx_bufs
[next
].dma_desc
;
452 desc
->config
= DESC_BTS(2) | DESC_DS
| DESC_EOF
| dma_len
;
455 desc
->config
|= DESC_EOC
;
458 txb
->dma_addr
= dma_addr
;
459 txb
->dma_len
= dma_len
;
461 if (!priv
->tx_chain
) {
463 priv
->tx_chain
= txb
;
465 priv
->tx_chain
->chain_len
++;
468 netdev_sent_queue(dev
, skb
->len
);
470 priv
->tx_next
= next
;
472 if (!skb
->xmit_more
) {
474 priv
->tx_chain
->ready
= true;
475 priv
->tx_chain
= NULL
;
476 nb8800_tx_dma_start(dev
);
482 static void nb8800_tx_error(struct net_device
*dev
, u32 report
)
484 if (report
& TX_LATE_COLLISION
)
485 dev
->stats
.collisions
++;
487 if (report
& TX_PACKET_DROPPED
)
488 dev
->stats
.tx_dropped
++;
490 if (report
& TX_FIFO_UNDERRUN
)
491 dev
->stats
.tx_fifo_errors
++;
493 dev
->stats
.tx_errors
++;
496 static void nb8800_tx_done(struct net_device
*dev
)
498 struct nb8800_priv
*priv
= netdev_priv(dev
);
499 unsigned int limit
= priv
->tx_next
;
500 unsigned int done
= priv
->tx_done
;
501 unsigned int packets
= 0;
502 unsigned int len
= 0;
504 while (done
!= limit
) {
505 struct nb8800_tx_desc
*txd
= &priv
->tx_descs
[done
];
506 struct nb8800_tx_buf
*txb
= &priv
->tx_bufs
[done
];
515 dma_unmap_single(&dev
->dev
, txb
->dma_addr
, txb
->dma_len
,
518 if (IS_TX_ERROR(txd
->report
)) {
519 nb8800_tx_error(dev
, txd
->report
);
525 dev
->stats
.tx_packets
++;
526 dev
->stats
.tx_bytes
+= TX_BYTES_TRANSFERRED(txd
->report
);
527 dev
->stats
.collisions
+= TX_EARLY_COLLISIONS(txd
->report
);
533 done
= (done
+ 1) % TX_DESC_COUNT
;
538 smp_mb__before_atomic();
539 atomic_add(packets
, &priv
->tx_free
);
540 netdev_completed_queue(dev
, packets
, len
);
541 netif_wake_queue(dev
);
542 priv
->tx_done
= done
;
546 static irqreturn_t
nb8800_irq(int irq
, void *dev_id
)
548 struct net_device
*dev
= dev_id
;
549 struct nb8800_priv
*priv
= netdev_priv(dev
);
550 irqreturn_t ret
= IRQ_NONE
;
554 val
= nb8800_readl(priv
, NB8800_TXC_SR
);
556 nb8800_writel(priv
, NB8800_TXC_SR
, val
);
559 nb8800_tx_dma_start_irq(dev
);
562 napi_schedule_irqoff(&priv
->napi
);
564 if (unlikely(val
& TSR_DE
))
565 netdev_err(dev
, "TX DMA error\n");
567 /* should never happen with automatic status retrieval */
568 if (unlikely(val
& TSR_TO
))
569 netdev_err(dev
, "TX Status FIFO overflow\n");
575 val
= nb8800_readl(priv
, NB8800_RXC_SR
);
577 nb8800_writel(priv
, NB8800_RXC_SR
, val
);
579 if (likely(val
& (RSR_RI
| RSR_DI
))) {
580 nb8800_writel(priv
, NB8800_RX_ITR
, priv
->rx_itr_poll
);
581 napi_schedule_irqoff(&priv
->napi
);
584 if (unlikely(val
& RSR_DE
))
585 netdev_err(dev
, "RX DMA error\n");
587 /* should never happen with automatic status retrieval */
588 if (unlikely(val
& RSR_RO
))
589 netdev_err(dev
, "RX Status FIFO overflow\n");
597 static void nb8800_mac_config(struct net_device
*dev
)
599 struct nb8800_priv
*priv
= netdev_priv(dev
);
600 bool gigabit
= priv
->speed
== SPEED_1000
;
601 u32 mac_mode_mask
= RGMII_MODE
| HALF_DUPLEX
| GMAC_MODE
;
608 mac_mode
|= HALF_DUPLEX
;
611 if (priv
->phy_mode
== PHY_INTERFACE_MODE_RGMII
)
612 mac_mode
|= RGMII_MODE
;
614 mac_mode
|= GMAC_MODE
;
617 /* Should be 512 but register is only 8 bits */
624 ict
= DIV_ROUND_UP(phy_clk
, clk_get_rate(priv
->clk
));
626 nb8800_writeb(priv
, NB8800_IC_THRESHOLD
, ict
);
627 nb8800_writeb(priv
, NB8800_SLOT_TIME
, slot_time
);
628 nb8800_maskb(priv
, NB8800_MAC_MODE
, mac_mode_mask
, mac_mode
);
631 static void nb8800_pause_config(struct net_device
*dev
)
633 struct nb8800_priv
*priv
= netdev_priv(dev
);
634 struct phy_device
*phydev
= priv
->phydev
;
637 if (priv
->pause_aneg
) {
638 if (!phydev
|| !phydev
->link
)
641 priv
->pause_rx
= phydev
->pause
;
642 priv
->pause_tx
= phydev
->pause
^ phydev
->asym_pause
;
645 nb8800_modb(priv
, NB8800_RX_CTL
, RX_PAUSE_EN
, priv
->pause_rx
);
647 rxcr
= nb8800_readl(priv
, NB8800_RXC_CR
);
648 if (!!(rxcr
& RCR_FL
) == priv
->pause_tx
)
651 if (netif_running(dev
)) {
652 napi_disable(&priv
->napi
);
653 netif_tx_lock_bh(dev
);
654 nb8800_dma_stop(dev
);
655 nb8800_modl(priv
, NB8800_RXC_CR
, RCR_FL
, priv
->pause_tx
);
656 nb8800_start_rx(dev
);
657 netif_tx_unlock_bh(dev
);
658 napi_enable(&priv
->napi
);
660 nb8800_modl(priv
, NB8800_RXC_CR
, RCR_FL
, priv
->pause_tx
);
664 static void nb8800_link_reconfigure(struct net_device
*dev
)
666 struct nb8800_priv
*priv
= netdev_priv(dev
);
667 struct phy_device
*phydev
= priv
->phydev
;
671 if (phydev
->speed
!= priv
->speed
) {
672 priv
->speed
= phydev
->speed
;
676 if (phydev
->duplex
!= priv
->duplex
) {
677 priv
->duplex
= phydev
->duplex
;
682 nb8800_mac_config(dev
);
684 nb8800_pause_config(dev
);
687 if (phydev
->link
!= priv
->link
) {
688 priv
->link
= phydev
->link
;
693 phy_print_status(priv
->phydev
);
696 static void nb8800_update_mac_addr(struct net_device
*dev
)
698 struct nb8800_priv
*priv
= netdev_priv(dev
);
701 for (i
= 0; i
< ETH_ALEN
; i
++)
702 nb8800_writeb(priv
, NB8800_SRC_ADDR(i
), dev
->dev_addr
[i
]);
704 for (i
= 0; i
< ETH_ALEN
; i
++)
705 nb8800_writeb(priv
, NB8800_UC_ADDR(i
), dev
->dev_addr
[i
]);
708 static int nb8800_set_mac_address(struct net_device
*dev
, void *addr
)
710 struct sockaddr
*sock
= addr
;
712 if (netif_running(dev
))
715 ether_addr_copy(dev
->dev_addr
, sock
->sa_data
);
716 nb8800_update_mac_addr(dev
);
721 static void nb8800_mc_init(struct net_device
*dev
, int val
)
723 struct nb8800_priv
*priv
= netdev_priv(dev
);
725 nb8800_writeb(priv
, NB8800_MC_INIT
, val
);
726 readb_poll_timeout_atomic(priv
->base
+ NB8800_MC_INIT
, val
, !val
,
730 static void nb8800_set_rx_mode(struct net_device
*dev
)
732 struct nb8800_priv
*priv
= netdev_priv(dev
);
733 struct netdev_hw_addr
*ha
;
736 if (dev
->flags
& (IFF_PROMISC
| IFF_ALLMULTI
)) {
737 nb8800_mac_af(dev
, false);
741 nb8800_mac_af(dev
, true);
742 nb8800_mc_init(dev
, 0);
744 netdev_for_each_mc_addr(ha
, dev
) {
745 for (i
= 0; i
< ETH_ALEN
; i
++)
746 nb8800_writeb(priv
, NB8800_MC_ADDR(i
), ha
->addr
[i
]);
748 nb8800_mc_init(dev
, 0xff);
752 #define RX_DESC_SIZE (RX_DESC_COUNT * sizeof(struct nb8800_rx_desc))
753 #define TX_DESC_SIZE (TX_DESC_COUNT * sizeof(struct nb8800_tx_desc))
755 static void nb8800_dma_free(struct net_device
*dev
)
757 struct nb8800_priv
*priv
= netdev_priv(dev
);
761 for (i
= 0; i
< RX_DESC_COUNT
; i
++)
762 if (priv
->rx_bufs
[i
].page
)
763 put_page(priv
->rx_bufs
[i
].page
);
765 kfree(priv
->rx_bufs
);
766 priv
->rx_bufs
= NULL
;
770 for (i
= 0; i
< TX_DESC_COUNT
; i
++)
771 kfree_skb(priv
->tx_bufs
[i
].skb
);
773 kfree(priv
->tx_bufs
);
774 priv
->tx_bufs
= NULL
;
777 if (priv
->rx_descs
) {
778 dma_free_coherent(dev
->dev
.parent
, RX_DESC_SIZE
, priv
->rx_descs
,
780 priv
->rx_descs
= NULL
;
783 if (priv
->tx_descs
) {
784 dma_free_coherent(dev
->dev
.parent
, TX_DESC_SIZE
, priv
->tx_descs
,
786 priv
->tx_descs
= NULL
;
790 static void nb8800_dma_reset(struct net_device
*dev
)
792 struct nb8800_priv
*priv
= netdev_priv(dev
);
793 struct nb8800_rx_desc
*rxd
;
794 struct nb8800_tx_desc
*txd
;
797 for (i
= 0; i
< RX_DESC_COUNT
; i
++) {
798 dma_addr_t rx_dma
= priv
->rx_desc_dma
+ i
* sizeof(*rxd
);
800 rxd
= &priv
->rx_descs
[i
];
801 rxd
->desc
.n_addr
= rx_dma
+ sizeof(*rxd
);
803 rx_dma
+ offsetof(struct nb8800_rx_desc
, report
);
804 rxd
->desc
.config
= priv
->rx_dma_config
;
808 rxd
->desc
.n_addr
= priv
->rx_desc_dma
;
809 rxd
->desc
.config
|= DESC_EOC
;
811 priv
->rx_eoc
= RX_DESC_COUNT
- 1;
813 for (i
= 0; i
< TX_DESC_COUNT
; i
++) {
814 struct nb8800_tx_buf
*txb
= &priv
->tx_bufs
[i
];
815 dma_addr_t r_dma
= txb
->dma_desc
+
816 offsetof(struct nb8800_tx_desc
, report
);
818 txd
= &priv
->tx_descs
[i
];
819 txd
->desc
[0].r_addr
= r_dma
;
820 txd
->desc
[1].r_addr
= r_dma
;
827 atomic_set(&priv
->tx_free
, TX_DESC_COUNT
);
829 nb8800_writel(priv
, NB8800_RX_DESC_ADDR
, priv
->rx_desc_dma
);
831 wmb(); /* ensure all setup is written before starting */
834 static int nb8800_dma_init(struct net_device
*dev
)
836 struct nb8800_priv
*priv
= netdev_priv(dev
);
837 unsigned int n_rx
= RX_DESC_COUNT
;
838 unsigned int n_tx
= TX_DESC_COUNT
;
842 priv
->rx_descs
= dma_alloc_coherent(dev
->dev
.parent
, RX_DESC_SIZE
,
843 &priv
->rx_desc_dma
, GFP_KERNEL
);
847 priv
->rx_bufs
= kcalloc(n_rx
, sizeof(*priv
->rx_bufs
), GFP_KERNEL
);
851 for (i
= 0; i
< n_rx
; i
++) {
852 err
= nb8800_alloc_rx(dev
, i
, false);
857 priv
->tx_descs
= dma_alloc_coherent(dev
->dev
.parent
, TX_DESC_SIZE
,
858 &priv
->tx_desc_dma
, GFP_KERNEL
);
862 priv
->tx_bufs
= kcalloc(n_tx
, sizeof(*priv
->tx_bufs
), GFP_KERNEL
);
866 for (i
= 0; i
< n_tx
; i
++)
867 priv
->tx_bufs
[i
].dma_desc
=
868 priv
->tx_desc_dma
+ i
* sizeof(struct nb8800_tx_desc
);
870 nb8800_dma_reset(dev
);
875 nb8800_dma_free(dev
);
880 static int nb8800_dma_stop(struct net_device
*dev
)
882 struct nb8800_priv
*priv
= netdev_priv(dev
);
883 struct nb8800_tx_buf
*txb
= &priv
->tx_bufs
[0];
884 struct nb8800_tx_desc
*txd
= &priv
->tx_descs
[0];
891 /* wait for tx to finish */
892 err
= readl_poll_timeout_atomic(priv
->base
+ NB8800_TXC_CR
, txcr
,
894 priv
->tx_done
== priv
->tx_next
,
899 /* The rx DMA only stops if it reaches the end of chain.
900 * To make this happen, we set the EOC flag on all rx
901 * descriptors, put the device in loopback mode, and send
902 * a few dummy frames. The interrupt handler will ignore
903 * these since NAPI is disabled and no real frames are in
907 for (i
= 0; i
< RX_DESC_COUNT
; i
++)
908 priv
->rx_descs
[i
].desc
.config
|= DESC_EOC
;
910 txd
->desc
[0].s_addr
=
911 txb
->dma_desc
+ offsetof(struct nb8800_tx_desc
, buf
);
912 txd
->desc
[0].config
= DESC_BTS(2) | DESC_DS
| DESC_EOF
| DESC_EOC
| 8;
913 memset(txd
->buf
, 0, sizeof(txd
->buf
));
915 nb8800_mac_af(dev
, false);
916 nb8800_setb(priv
, NB8800_MAC_MODE
, LOOPBACK_EN
);
919 nb8800_writel(priv
, NB8800_TX_DESC_ADDR
, txb
->dma_desc
);
921 nb8800_writel(priv
, NB8800_TXC_CR
, txcr
| TCR_EN
);
923 err
= readl_poll_timeout_atomic(priv
->base
+ NB8800_RXC_CR
,
924 rxcr
, !(rxcr
& RCR_EN
),
926 } while (err
&& --retry
);
928 nb8800_mac_af(dev
, true);
929 nb8800_clearb(priv
, NB8800_MAC_MODE
, LOOPBACK_EN
);
930 nb8800_dma_reset(dev
);
932 return retry
? 0 : -ETIMEDOUT
;
935 static void nb8800_pause_adv(struct net_device
*dev
)
937 struct nb8800_priv
*priv
= netdev_priv(dev
);
944 adv
|= ADVERTISED_Pause
| ADVERTISED_Asym_Pause
;
946 adv
^= ADVERTISED_Asym_Pause
;
948 priv
->phydev
->supported
|= adv
;
949 priv
->phydev
->advertising
|= adv
;
952 static int nb8800_open(struct net_device
*dev
)
954 struct nb8800_priv
*priv
= netdev_priv(dev
);
957 /* clear any pending interrupts */
958 nb8800_writel(priv
, NB8800_RXC_SR
, 0xf);
959 nb8800_writel(priv
, NB8800_TXC_SR
, 0xf);
961 err
= nb8800_dma_init(dev
);
965 err
= request_irq(dev
->irq
, nb8800_irq
, 0, dev_name(&dev
->dev
), dev
);
969 nb8800_mac_rx(dev
, true);
970 nb8800_mac_tx(dev
, true);
972 priv
->phydev
= of_phy_connect(dev
, priv
->phy_node
,
973 nb8800_link_reconfigure
, 0,
978 nb8800_pause_adv(dev
);
980 netdev_reset_queue(dev
);
981 napi_enable(&priv
->napi
);
982 netif_start_queue(dev
);
984 nb8800_start_rx(dev
);
985 phy_start(priv
->phydev
);
990 free_irq(dev
->irq
, dev
);
992 nb8800_dma_free(dev
);
997 static int nb8800_stop(struct net_device
*dev
)
999 struct nb8800_priv
*priv
= netdev_priv(dev
);
1001 phy_stop(priv
->phydev
);
1003 netif_stop_queue(dev
);
1004 napi_disable(&priv
->napi
);
1006 nb8800_dma_stop(dev
);
1007 nb8800_mac_rx(dev
, false);
1008 nb8800_mac_tx(dev
, false);
1010 phy_disconnect(priv
->phydev
);
1011 priv
->phydev
= NULL
;
1013 free_irq(dev
->irq
, dev
);
1015 nb8800_dma_free(dev
);
1020 static int nb8800_ioctl(struct net_device
*dev
, struct ifreq
*rq
, int cmd
)
1022 struct nb8800_priv
*priv
= netdev_priv(dev
);
1024 return phy_mii_ioctl(priv
->phydev
, rq
, cmd
);
1027 static const struct net_device_ops nb8800_netdev_ops
= {
1028 .ndo_open
= nb8800_open
,
1029 .ndo_stop
= nb8800_stop
,
1030 .ndo_start_xmit
= nb8800_xmit
,
1031 .ndo_set_mac_address
= nb8800_set_mac_address
,
1032 .ndo_set_rx_mode
= nb8800_set_rx_mode
,
1033 .ndo_do_ioctl
= nb8800_ioctl
,
1034 .ndo_change_mtu
= eth_change_mtu
,
1035 .ndo_validate_addr
= eth_validate_addr
,
1038 static int nb8800_get_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
1040 struct nb8800_priv
*priv
= netdev_priv(dev
);
1045 return phy_ethtool_gset(priv
->phydev
, cmd
);
1048 static int nb8800_set_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
1050 struct nb8800_priv
*priv
= netdev_priv(dev
);
1055 return phy_ethtool_sset(priv
->phydev
, cmd
);
1058 static int nb8800_nway_reset(struct net_device
*dev
)
1060 struct nb8800_priv
*priv
= netdev_priv(dev
);
1065 return genphy_restart_aneg(priv
->phydev
);
1068 static void nb8800_get_pauseparam(struct net_device
*dev
,
1069 struct ethtool_pauseparam
*pp
)
1071 struct nb8800_priv
*priv
= netdev_priv(dev
);
1073 pp
->autoneg
= priv
->pause_aneg
;
1074 pp
->rx_pause
= priv
->pause_rx
;
1075 pp
->tx_pause
= priv
->pause_tx
;
1078 static int nb8800_set_pauseparam(struct net_device
*dev
,
1079 struct ethtool_pauseparam
*pp
)
1081 struct nb8800_priv
*priv
= netdev_priv(dev
);
1083 priv
->pause_aneg
= pp
->autoneg
;
1084 priv
->pause_rx
= pp
->rx_pause
;
1085 priv
->pause_tx
= pp
->tx_pause
;
1087 nb8800_pause_adv(dev
);
1089 if (!priv
->pause_aneg
)
1090 nb8800_pause_config(dev
);
1091 else if (priv
->phydev
)
1092 phy_start_aneg(priv
->phydev
);
1097 static const char nb8800_stats_names
[][ETH_GSTRING_LEN
] = {
1100 "rx_undersize_frames",
1101 "rx_fragment_frames",
1102 "rx_64_byte_frames",
1103 "rx_127_byte_frames",
1104 "rx_255_byte_frames",
1105 "rx_511_byte_frames",
1106 "rx_1023_byte_frames",
1107 "rx_max_size_frames",
1108 "rx_oversize_frames",
1109 "rx_bad_fcs_frames",
1110 "rx_broadcast_frames",
1111 "rx_multicast_frames",
1112 "rx_control_frames",
1114 "rx_unsup_control_frames",
1115 "rx_align_error_frames",
1116 "rx_overrun_frames",
1123 "tx_64_byte_frames",
1124 "tx_127_byte_frames",
1125 "tx_255_byte_frames",
1126 "tx_511_byte_frames",
1127 "tx_1023_byte_frames",
1128 "tx_max_size_frames",
1129 "tx_oversize_frames",
1130 "tx_broadcast_frames",
1131 "tx_multicast_frames",
1132 "tx_control_frames",
1134 "tx_underrun_frames",
1135 "tx_single_collision_frames",
1136 "tx_multi_collision_frames",
1137 "tx_deferred_collision_frames",
1138 "tx_late_collision_frames",
1139 "tx_excessive_collision_frames",
1145 #define NB8800_NUM_STATS ARRAY_SIZE(nb8800_stats_names)
1147 static int nb8800_get_sset_count(struct net_device
*dev
, int sset
)
1149 if (sset
== ETH_SS_STATS
)
1150 return NB8800_NUM_STATS
;
1155 static void nb8800_get_strings(struct net_device
*dev
, u32 sset
, u8
*buf
)
1157 if (sset
== ETH_SS_STATS
)
1158 memcpy(buf
, &nb8800_stats_names
, sizeof(nb8800_stats_names
));
1161 static u32
nb8800_read_stat(struct net_device
*dev
, int index
)
1163 struct nb8800_priv
*priv
= netdev_priv(dev
);
1165 nb8800_writeb(priv
, NB8800_STAT_INDEX
, index
);
1167 return nb8800_readl(priv
, NB8800_STAT_DATA
);
1170 static void nb8800_get_ethtool_stats(struct net_device
*dev
,
1171 struct ethtool_stats
*estats
, u64
*st
)
1176 for (i
= 0; i
< NB8800_NUM_STATS
/ 2; i
++) {
1177 rx
= nb8800_read_stat(dev
, i
);
1178 tx
= nb8800_read_stat(dev
, i
| 0x80);
1180 st
[i
+ NB8800_NUM_STATS
/ 2] = tx
;
1184 static const struct ethtool_ops nb8800_ethtool_ops
= {
1185 .get_settings
= nb8800_get_settings
,
1186 .set_settings
= nb8800_set_settings
,
1187 .nway_reset
= nb8800_nway_reset
,
1188 .get_link
= ethtool_op_get_link
,
1189 .get_pauseparam
= nb8800_get_pauseparam
,
1190 .set_pauseparam
= nb8800_set_pauseparam
,
1191 .get_sset_count
= nb8800_get_sset_count
,
1192 .get_strings
= nb8800_get_strings
,
1193 .get_ethtool_stats
= nb8800_get_ethtool_stats
,
1196 static int nb8800_hw_init(struct net_device
*dev
)
1198 struct nb8800_priv
*priv
= netdev_priv(dev
);
1201 val
= TX_RETRY_EN
| TX_PAD_EN
| TX_APPEND_FCS
;
1202 nb8800_writeb(priv
, NB8800_TX_CTL1
, val
);
1204 /* Collision retry count */
1205 nb8800_writeb(priv
, NB8800_TX_CTL2
, 5);
1207 val
= RX_PAD_STRIP
| RX_AF_EN
;
1208 nb8800_writeb(priv
, NB8800_RX_CTL
, val
);
1210 /* Chosen by fair dice roll */
1211 nb8800_writeb(priv
, NB8800_RANDOM_SEED
, 4);
1213 /* TX cycles per deferral period */
1214 nb8800_writeb(priv
, NB8800_TX_SDP
, 12);
1216 /* The following three threshold values have been
1217 * experimentally determined for good results.
1220 /* RX/TX FIFO threshold for partial empty (64-bit entries) */
1221 nb8800_writeb(priv
, NB8800_PE_THRESHOLD
, 0);
1223 /* RX/TX FIFO threshold for partial full (64-bit entries) */
1224 nb8800_writeb(priv
, NB8800_PF_THRESHOLD
, 255);
1226 /* Buffer size for transmit (64-bit entries) */
1227 nb8800_writeb(priv
, NB8800_TX_BUFSIZE
, 64);
1229 /* Configure tx DMA */
1231 val
= nb8800_readl(priv
, NB8800_TXC_CR
);
1232 val
&= TCR_LE
; /* keep endian setting */
1233 val
|= TCR_DM
; /* DMA descriptor mode */
1234 val
|= TCR_RS
; /* automatically store tx status */
1235 val
|= TCR_DIE
; /* interrupt on DMA chain completion */
1236 val
|= TCR_TFI(7); /* interrupt after 7 frames transmitted */
1237 val
|= TCR_BTS(2); /* 32-byte bus transaction size */
1238 nb8800_writel(priv
, NB8800_TXC_CR
, val
);
1240 /* TX complete interrupt after 10 ms or 7 frames (see above) */
1241 val
= clk_get_rate(priv
->clk
) / 100;
1242 nb8800_writel(priv
, NB8800_TX_ITR
, val
);
1244 /* Configure rx DMA */
1246 val
= nb8800_readl(priv
, NB8800_RXC_CR
);
1247 val
&= RCR_LE
; /* keep endian setting */
1248 val
|= RCR_DM
; /* DMA descriptor mode */
1249 val
|= RCR_RS
; /* automatically store rx status */
1250 val
|= RCR_DIE
; /* interrupt at end of DMA chain */
1251 val
|= RCR_RFI(7); /* interrupt after 7 frames received */
1252 val
|= RCR_BTS(2); /* 32-byte bus transaction size */
1253 nb8800_writel(priv
, NB8800_RXC_CR
, val
);
1255 /* The rx interrupt can fire before the DMA has completed
1256 * unless a small delay is added. 50 us is hopefully enough.
1258 priv
->rx_itr_irq
= clk_get_rate(priv
->clk
) / 20000;
1260 /* In NAPI poll mode we want to disable interrupts, but the
1261 * hardware does not permit this. Delay 10 ms instead.
1263 priv
->rx_itr_poll
= clk_get_rate(priv
->clk
) / 100;
1265 nb8800_writel(priv
, NB8800_RX_ITR
, priv
->rx_itr_irq
);
1267 priv
->rx_dma_config
= RX_BUF_SIZE
| DESC_BTS(2) | DESC_DS
| DESC_EOF
;
1269 /* Flow control settings */
1271 /* Pause time of 0.1 ms */
1273 nb8800_writeb(priv
, NB8800_PQ1
, val
>> 8);
1274 nb8800_writeb(priv
, NB8800_PQ2
, val
& 0xff);
1276 /* Auto-negotiate by default */
1277 priv
->pause_aneg
= true;
1278 priv
->pause_rx
= true;
1279 priv
->pause_tx
= true;
1281 nb8800_mc_init(dev
, 0);
1286 static int nb8800_tangox_init(struct net_device
*dev
)
1288 struct nb8800_priv
*priv
= netdev_priv(dev
);
1289 u32 pad_mode
= PAD_MODE_MII
;
1291 switch (priv
->phy_mode
) {
1292 case PHY_INTERFACE_MODE_MII
:
1293 case PHY_INTERFACE_MODE_GMII
:
1294 pad_mode
= PAD_MODE_MII
;
1297 case PHY_INTERFACE_MODE_RGMII
:
1298 pad_mode
= PAD_MODE_RGMII
;
1301 case PHY_INTERFACE_MODE_RGMII_TXID
:
1302 pad_mode
= PAD_MODE_RGMII
| PAD_MODE_GTX_CLK_DELAY
;
1306 dev_err(dev
->dev
.parent
, "unsupported phy mode %s\n",
1307 phy_modes(priv
->phy_mode
));
1311 nb8800_writeb(priv
, NB8800_TANGOX_PAD_MODE
, pad_mode
);
1316 static int nb8800_tangox_reset(struct net_device
*dev
)
1318 struct nb8800_priv
*priv
= netdev_priv(dev
);
1321 nb8800_writeb(priv
, NB8800_TANGOX_RESET
, 0);
1322 usleep_range(1000, 10000);
1323 nb8800_writeb(priv
, NB8800_TANGOX_RESET
, 1);
1325 wmb(); /* ensure reset is cleared before proceeding */
1327 clk_div
= DIV_ROUND_UP(clk_get_rate(priv
->clk
), 2 * MAX_MDC_CLOCK
);
1328 nb8800_writew(priv
, NB8800_TANGOX_MDIO_CLKDIV
, clk_div
);
1333 static const struct nb8800_ops nb8800_tangox_ops
= {
1334 .init
= nb8800_tangox_init
,
1335 .reset
= nb8800_tangox_reset
,
1338 static int nb8800_tango4_init(struct net_device
*dev
)
1340 struct nb8800_priv
*priv
= netdev_priv(dev
);
1343 err
= nb8800_tangox_init(dev
);
1347 /* On tango4 interrupt on DMA completion per frame works and gives
1348 * better performance despite generating more rx interrupts.
1351 /* Disable unnecessary interrupt on rx completion */
1352 nb8800_clearl(priv
, NB8800_RXC_CR
, RCR_RFI(7));
1354 /* Request interrupt on descriptor DMA completion */
1355 priv
->rx_dma_config
|= DESC_ID
;
1360 static const struct nb8800_ops nb8800_tango4_ops
= {
1361 .init
= nb8800_tango4_init
,
1362 .reset
= nb8800_tangox_reset
,
1365 static const struct of_device_id nb8800_dt_ids
[] = {
1367 .compatible
= "aurora,nb8800",
1370 .compatible
= "sigma,smp8642-ethernet",
1371 .data
= &nb8800_tangox_ops
,
1374 .compatible
= "sigma,smp8734-ethernet",
1375 .data
= &nb8800_tango4_ops
,
1380 static int nb8800_probe(struct platform_device
*pdev
)
1382 const struct of_device_id
*match
;
1383 const struct nb8800_ops
*ops
= NULL
;
1384 struct nb8800_priv
*priv
;
1385 struct resource
*res
;
1386 struct net_device
*dev
;
1387 struct mii_bus
*bus
;
1388 const unsigned char *mac
;
1393 match
= of_match_device(nb8800_dt_ids
, &pdev
->dev
);
1397 irq
= platform_get_irq(pdev
, 0);
1399 dev_err(&pdev
->dev
, "No IRQ\n");
1403 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1404 base
= devm_ioremap_resource(&pdev
->dev
, res
);
1406 return PTR_ERR(base
);
1408 dev_dbg(&pdev
->dev
, "AU-NB8800 Ethernet at %pa\n", &res
->start
);
1410 dev
= alloc_etherdev(sizeof(*priv
));
1414 platform_set_drvdata(pdev
, dev
);
1415 SET_NETDEV_DEV(dev
, &pdev
->dev
);
1417 priv
= netdev_priv(dev
);
1420 priv
->phy_mode
= of_get_phy_mode(pdev
->dev
.of_node
);
1421 if (priv
->phy_mode
< 0)
1422 priv
->phy_mode
= PHY_INTERFACE_MODE_RGMII
;
1424 priv
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
1425 if (IS_ERR(priv
->clk
)) {
1426 dev_err(&pdev
->dev
, "failed to get clock\n");
1427 ret
= PTR_ERR(priv
->clk
);
1431 ret
= clk_prepare_enable(priv
->clk
);
1435 spin_lock_init(&priv
->tx_lock
);
1437 if (ops
&& ops
->reset
) {
1438 ret
= ops
->reset(dev
);
1443 bus
= devm_mdiobus_alloc(&pdev
->dev
);
1446 goto err_disable_clk
;
1449 bus
->name
= "nb8800-mii";
1450 bus
->read
= nb8800_mdio_read
;
1451 bus
->write
= nb8800_mdio_write
;
1452 bus
->parent
= &pdev
->dev
;
1453 snprintf(bus
->id
, MII_BUS_ID_SIZE
, "%lx.nb8800-mii",
1454 (unsigned long)res
->start
);
1457 ret
= of_mdiobus_register(bus
, pdev
->dev
.of_node
);
1459 dev_err(&pdev
->dev
, "failed to register MII bus\n");
1460 goto err_disable_clk
;
1463 if (of_phy_is_fixed_link(pdev
->dev
.of_node
)) {
1464 ret
= of_phy_register_fixed_link(pdev
->dev
.of_node
);
1466 dev_err(&pdev
->dev
, "bad fixed-link spec\n");
1469 priv
->phy_node
= of_node_get(pdev
->dev
.of_node
);
1472 if (!priv
->phy_node
)
1473 priv
->phy_node
= of_parse_phandle(pdev
->dev
.of_node
,
1476 if (!priv
->phy_node
) {
1477 dev_err(&pdev
->dev
, "no PHY specified\n");
1482 priv
->mii_bus
= bus
;
1484 ret
= nb8800_hw_init(dev
);
1488 if (ops
&& ops
->init
) {
1489 ret
= ops
->init(dev
);
1494 dev
->netdev_ops
= &nb8800_netdev_ops
;
1495 dev
->ethtool_ops
= &nb8800_ethtool_ops
;
1496 dev
->flags
|= IFF_MULTICAST
;
1499 mac
= of_get_mac_address(pdev
->dev
.of_node
);
1501 ether_addr_copy(dev
->dev_addr
, mac
);
1503 if (!is_valid_ether_addr(dev
->dev_addr
))
1504 eth_hw_addr_random(dev
);
1506 nb8800_update_mac_addr(dev
);
1508 netif_carrier_off(dev
);
1510 ret
= register_netdev(dev
);
1512 netdev_err(dev
, "failed to register netdev\n");
1516 netif_napi_add(dev
, &priv
->napi
, nb8800_poll
, NAPI_POLL_WEIGHT
);
1518 netdev_info(dev
, "MAC address %pM\n", dev
->dev_addr
);
1523 nb8800_dma_free(dev
);
1525 mdiobus_unregister(bus
);
1527 clk_disable_unprepare(priv
->clk
);
1534 static int nb8800_remove(struct platform_device
*pdev
)
1536 struct net_device
*ndev
= platform_get_drvdata(pdev
);
1537 struct nb8800_priv
*priv
= netdev_priv(ndev
);
1539 unregister_netdev(ndev
);
1541 mdiobus_unregister(priv
->mii_bus
);
1543 clk_disable_unprepare(priv
->clk
);
1545 nb8800_dma_free(ndev
);
1551 static struct platform_driver nb8800_driver
= {
1554 .of_match_table
= nb8800_dt_ids
,
1556 .probe
= nb8800_probe
,
1557 .remove
= nb8800_remove
,
1560 module_platform_driver(nb8800_driver
);
1562 MODULE_DESCRIPTION("Aurora AU-NB8800 Ethernet driver");
1563 MODULE_AUTHOR("Mans Rullgard <mans@mansr.com>");
1564 MODULE_LICENSE("GPL");