1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Hisilicon Fast Ethernet MAC Driver
5 * Copyright (c) 2016 HiSilicon Technologies Co., Ltd.
8 #include <linux/circ_buf.h>
10 #include <linux/etherdevice.h>
11 #include <linux/interrupt.h>
12 #include <linux/module.h>
13 #include <linux/of_mdio.h>
14 #include <linux/of_net.h>
15 #include <linux/platform_device.h>
16 #include <linux/reset.h>
18 /* MAC control register list */
19 #define MAC_PORTSEL 0x0200
20 #define MAC_PORTSEL_STAT_CPU BIT(0)
21 #define MAC_PORTSEL_RMII BIT(1)
22 #define MAC_PORTSET 0x0208
23 #define MAC_PORTSET_DUPLEX_FULL BIT(0)
24 #define MAC_PORTSET_LINKED BIT(1)
25 #define MAC_PORTSET_SPEED_100M BIT(2)
26 #define MAC_SET 0x0210
27 #define MAX_FRAME_SIZE 1600
28 #define MAX_FRAME_SIZE_MASK GENMASK(10, 0)
29 #define BIT_PAUSE_EN BIT(18)
30 #define RX_COALESCE_SET 0x0340
31 #define RX_COALESCED_FRAME_OFFSET 24
32 #define RX_COALESCED_FRAMES 8
33 #define RX_COALESCED_TIMER 0x74
34 #define QLEN_SET 0x0344
35 #define RX_DEPTH_OFFSET 8
36 #define MAX_HW_FIFO_DEPTH 64
37 #define HW_TX_FIFO_DEPTH 12
38 #define HW_RX_FIFO_DEPTH (MAX_HW_FIFO_DEPTH - HW_TX_FIFO_DEPTH)
39 #define IQFRM_DES 0x0354
40 #define RX_FRAME_LEN_MASK GENMASK(11, 0)
41 #define IQ_ADDR 0x0358
42 #define EQ_ADDR 0x0360
43 #define EQFRM_LEN 0x0364
44 #define ADDRQ_STAT 0x036C
45 #define TX_CNT_INUSE_MASK GENMASK(5, 0)
46 #define BIT_TX_READY BIT(24)
47 #define BIT_RX_READY BIT(25)
48 /* global control register list */
49 #define GLB_HOSTMAC_L32 0x0000
50 #define GLB_HOSTMAC_H16 0x0004
51 #define GLB_SOFT_RESET 0x0008
52 #define SOFT_RESET_ALL BIT(0)
53 #define GLB_FWCTRL 0x0010
54 #define FWCTRL_VLAN_ENABLE BIT(0)
55 #define FWCTRL_FW2CPU_ENA BIT(5)
56 #define FWCTRL_FWALL2CPU BIT(7)
57 #define GLB_MACTCTRL 0x0014
58 #define MACTCTRL_UNI2CPU BIT(1)
59 #define MACTCTRL_MULTI2CPU BIT(3)
60 #define MACTCTRL_BROAD2CPU BIT(5)
61 #define MACTCTRL_MACT_ENA BIT(7)
62 #define GLB_IRQ_STAT 0x0030
63 #define GLB_IRQ_ENA 0x0034
64 #define IRQ_ENA_PORT0_MASK GENMASK(7, 0)
65 #define IRQ_ENA_PORT0 BIT(18)
66 #define IRQ_ENA_ALL BIT(19)
67 #define GLB_IRQ_RAW 0x0038
68 #define IRQ_INT_RX_RDY BIT(0)
69 #define IRQ_INT_TX_PER_PACKET BIT(1)
70 #define IRQ_INT_TX_FIFO_EMPTY BIT(6)
71 #define IRQ_INT_MULTI_RXRDY BIT(7)
72 #define DEF_INT_MASK (IRQ_INT_MULTI_RXRDY | \
73 IRQ_INT_TX_PER_PACKET | \
74 IRQ_INT_TX_FIFO_EMPTY)
75 #define GLB_MAC_L32_BASE 0x0100
76 #define GLB_MAC_H16_BASE 0x0104
77 #define MACFLT_HI16_MASK GENMASK(15, 0)
78 #define BIT_MACFLT_ENA BIT(17)
79 #define BIT_MACFLT_FW2CPU BIT(21)
80 #define GLB_MAC_H16(reg) (GLB_MAC_H16_BASE + ((reg) * 0x8))
81 #define GLB_MAC_L32(reg) (GLB_MAC_L32_BASE + ((reg) * 0x8))
82 #define MAX_MAC_FILTER_NUM 8
83 #define MAX_UNICAST_ADDRESSES 2
84 #define MAX_MULTICAST_ADDRESSES (MAX_MAC_FILTER_NUM - \
85 MAX_UNICAST_ADDRESSES)
86 /* software tx and rx queue number, should be power of 2 */
89 #define FEMAC_POLL_WEIGHT 16
91 #define PHY_RESET_DELAYS_PROPERTY "hisilicon,phy-reset-delays-us"
93 enum phy_reset_delays
{
100 struct hisi_femac_queue
{
101 struct sk_buff
**skb
;
102 dma_addr_t
*dma_phys
;
108 struct hisi_femac_priv
{
109 void __iomem
*port_base
;
110 void __iomem
*glb_base
;
112 struct reset_control
*mac_rst
;
113 struct reset_control
*phy_rst
;
114 u32 phy_reset_delays
[DELAYS_NUM
];
118 struct net_device
*ndev
;
120 struct hisi_femac_queue txq
;
121 struct hisi_femac_queue rxq
;
122 u32 tx_fifo_used_cnt
;
123 struct napi_struct napi
;
126 static void hisi_femac_irq_enable(struct hisi_femac_priv
*priv
, int irqs
)
130 val
= readl(priv
->glb_base
+ GLB_IRQ_ENA
);
131 writel(val
| irqs
, priv
->glb_base
+ GLB_IRQ_ENA
);
134 static void hisi_femac_irq_disable(struct hisi_femac_priv
*priv
, int irqs
)
138 val
= readl(priv
->glb_base
+ GLB_IRQ_ENA
);
139 writel(val
& (~irqs
), priv
->glb_base
+ GLB_IRQ_ENA
);
142 static void hisi_femac_tx_dma_unmap(struct hisi_femac_priv
*priv
,
143 struct sk_buff
*skb
, unsigned int pos
)
147 dma_addr
= priv
->txq
.dma_phys
[pos
];
148 dma_unmap_single(priv
->dev
, dma_addr
, skb
->len
, DMA_TO_DEVICE
);
151 static void hisi_femac_xmit_reclaim(struct net_device
*dev
)
154 struct hisi_femac_priv
*priv
= netdev_priv(dev
);
155 struct hisi_femac_queue
*txq
= &priv
->txq
;
156 unsigned int bytes_compl
= 0, pkts_compl
= 0;
161 val
= readl(priv
->port_base
+ ADDRQ_STAT
) & TX_CNT_INUSE_MASK
;
162 while (val
< priv
->tx_fifo_used_cnt
) {
163 skb
= txq
->skb
[txq
->tail
];
164 if (unlikely(!skb
)) {
165 netdev_err(dev
, "xmitq_cnt_inuse=%d, tx_fifo_used=%d\n",
166 val
, priv
->tx_fifo_used_cnt
);
169 hisi_femac_tx_dma_unmap(priv
, skb
, txq
->tail
);
171 bytes_compl
+= skb
->len
;
172 dev_kfree_skb_any(skb
);
174 priv
->tx_fifo_used_cnt
--;
176 val
= readl(priv
->port_base
+ ADDRQ_STAT
) & TX_CNT_INUSE_MASK
;
177 txq
->skb
[txq
->tail
] = NULL
;
178 txq
->tail
= (txq
->tail
+ 1) % txq
->num
;
181 netdev_completed_queue(dev
, pkts_compl
, bytes_compl
);
183 if (unlikely(netif_queue_stopped(dev
)) && pkts_compl
)
184 netif_wake_queue(dev
);
186 netif_tx_unlock(dev
);
189 static void hisi_femac_adjust_link(struct net_device
*dev
)
191 struct hisi_femac_priv
*priv
= netdev_priv(dev
);
192 struct phy_device
*phy
= dev
->phydev
;
196 status
|= MAC_PORTSET_LINKED
;
197 if (phy
->duplex
== DUPLEX_FULL
)
198 status
|= MAC_PORTSET_DUPLEX_FULL
;
199 if (phy
->speed
== SPEED_100
)
200 status
|= MAC_PORTSET_SPEED_100M
;
202 if ((status
!= priv
->link_status
) &&
203 ((status
| priv
->link_status
) & MAC_PORTSET_LINKED
)) {
204 writel(status
, priv
->port_base
+ MAC_PORTSET
);
205 priv
->link_status
= status
;
206 phy_print_status(phy
);
210 static void hisi_femac_rx_refill(struct hisi_femac_priv
*priv
)
212 struct hisi_femac_queue
*rxq
= &priv
->rxq
;
215 u32 len
= MAX_FRAME_SIZE
;
219 while (readl(priv
->port_base
+ ADDRQ_STAT
) & BIT_RX_READY
) {
220 if (!CIRC_SPACE(pos
, rxq
->tail
, rxq
->num
))
222 if (unlikely(rxq
->skb
[pos
])) {
223 netdev_err(priv
->ndev
, "err skb[%d]=%p\n",
227 skb
= netdev_alloc_skb_ip_align(priv
->ndev
, len
);
231 addr
= dma_map_single(priv
->dev
, skb
->data
, len
,
233 if (dma_mapping_error(priv
->dev
, addr
)) {
234 dev_kfree_skb_any(skb
);
237 rxq
->dma_phys
[pos
] = addr
;
239 writel(addr
, priv
->port_base
+ IQ_ADDR
);
240 pos
= (pos
+ 1) % rxq
->num
;
245 static int hisi_femac_rx(struct net_device
*dev
, int limit
)
247 struct hisi_femac_priv
*priv
= netdev_priv(dev
);
248 struct hisi_femac_queue
*rxq
= &priv
->rxq
;
251 u32 rx_pkt_info
, pos
, len
, rx_pkts_num
= 0;
254 while (readl(priv
->glb_base
+ GLB_IRQ_RAW
) & IRQ_INT_RX_RDY
) {
255 rx_pkt_info
= readl(priv
->port_base
+ IQFRM_DES
);
256 len
= rx_pkt_info
& RX_FRAME_LEN_MASK
;
259 /* tell hardware we will deal with this packet */
260 writel(IRQ_INT_RX_RDY
, priv
->glb_base
+ GLB_IRQ_RAW
);
265 if (unlikely(!skb
)) {
266 netdev_err(dev
, "rx skb NULL. pos=%d\n", pos
);
269 rxq
->skb
[pos
] = NULL
;
271 addr
= rxq
->dma_phys
[pos
];
272 dma_unmap_single(priv
->dev
, addr
, MAX_FRAME_SIZE
,
275 if (unlikely(skb
->len
> MAX_FRAME_SIZE
)) {
276 netdev_err(dev
, "rcv len err, len = %d\n", skb
->len
);
277 dev
->stats
.rx_errors
++;
278 dev
->stats
.rx_length_errors
++;
279 dev_kfree_skb_any(skb
);
283 skb
->protocol
= eth_type_trans(skb
, dev
);
284 napi_gro_receive(&priv
->napi
, skb
);
285 dev
->stats
.rx_packets
++;
286 dev
->stats
.rx_bytes
+= skb
->len
;
288 pos
= (pos
+ 1) % rxq
->num
;
289 if (rx_pkts_num
>= limit
)
294 hisi_femac_rx_refill(priv
);
299 static int hisi_femac_poll(struct napi_struct
*napi
, int budget
)
301 struct hisi_femac_priv
*priv
= container_of(napi
,
302 struct hisi_femac_priv
, napi
);
303 struct net_device
*dev
= priv
->ndev
;
304 int work_done
= 0, task
= budget
;
308 hisi_femac_xmit_reclaim(dev
);
309 num
= hisi_femac_rx(dev
, task
);
312 if (work_done
>= budget
)
315 ints
= readl(priv
->glb_base
+ GLB_IRQ_RAW
);
316 writel(ints
& DEF_INT_MASK
,
317 priv
->glb_base
+ GLB_IRQ_RAW
);
318 } while (ints
& DEF_INT_MASK
);
320 if (work_done
< budget
) {
321 napi_complete_done(napi
, work_done
);
322 hisi_femac_irq_enable(priv
, DEF_INT_MASK
&
323 (~IRQ_INT_TX_PER_PACKET
));
329 static irqreturn_t
hisi_femac_interrupt(int irq
, void *dev_id
)
332 struct net_device
*dev
= (struct net_device
*)dev_id
;
333 struct hisi_femac_priv
*priv
= netdev_priv(dev
);
335 ints
= readl(priv
->glb_base
+ GLB_IRQ_RAW
);
337 if (likely(ints
& DEF_INT_MASK
)) {
338 writel(ints
& DEF_INT_MASK
,
339 priv
->glb_base
+ GLB_IRQ_RAW
);
340 hisi_femac_irq_disable(priv
, DEF_INT_MASK
);
341 napi_schedule(&priv
->napi
);
347 static int hisi_femac_init_queue(struct device
*dev
,
348 struct hisi_femac_queue
*queue
,
351 queue
->skb
= devm_kcalloc(dev
, num
, sizeof(struct sk_buff
*),
356 queue
->dma_phys
= devm_kcalloc(dev
, num
, sizeof(dma_addr_t
),
358 if (!queue
->dma_phys
)
368 static int hisi_femac_init_tx_and_rx_queues(struct hisi_femac_priv
*priv
)
372 ret
= hisi_femac_init_queue(priv
->dev
, &priv
->txq
, TXQ_NUM
);
376 ret
= hisi_femac_init_queue(priv
->dev
, &priv
->rxq
, RXQ_NUM
);
380 priv
->tx_fifo_used_cnt
= 0;
385 static void hisi_femac_free_skb_rings(struct hisi_femac_priv
*priv
)
387 struct hisi_femac_queue
*txq
= &priv
->txq
;
388 struct hisi_femac_queue
*rxq
= &priv
->rxq
;
394 while (pos
!= rxq
->head
) {
396 if (unlikely(!skb
)) {
397 netdev_err(priv
->ndev
, "NULL rx skb. pos=%d, head=%d\n",
402 dma_addr
= rxq
->dma_phys
[pos
];
403 dma_unmap_single(priv
->dev
, dma_addr
, MAX_FRAME_SIZE
,
406 dev_kfree_skb_any(skb
);
407 rxq
->skb
[pos
] = NULL
;
408 pos
= (pos
+ 1) % rxq
->num
;
413 while (pos
!= txq
->head
) {
415 if (unlikely(!skb
)) {
416 netdev_err(priv
->ndev
, "NULL tx skb. pos=%d, head=%d\n",
420 hisi_femac_tx_dma_unmap(priv
, skb
, pos
);
421 dev_kfree_skb_any(skb
);
422 txq
->skb
[pos
] = NULL
;
423 pos
= (pos
+ 1) % txq
->num
;
426 priv
->tx_fifo_used_cnt
= 0;
429 static int hisi_femac_set_hw_mac_addr(struct hisi_femac_priv
*priv
,
434 reg
= mac
[1] | (mac
[0] << 8);
435 writel(reg
, priv
->glb_base
+ GLB_HOSTMAC_H16
);
437 reg
= mac
[5] | (mac
[4] << 8) | (mac
[3] << 16) | (mac
[2] << 24);
438 writel(reg
, priv
->glb_base
+ GLB_HOSTMAC_L32
);
443 static int hisi_femac_port_reset(struct hisi_femac_priv
*priv
)
447 val
= readl(priv
->glb_base
+ GLB_SOFT_RESET
);
448 val
|= SOFT_RESET_ALL
;
449 writel(val
, priv
->glb_base
+ GLB_SOFT_RESET
);
451 usleep_range(500, 800);
453 val
&= ~SOFT_RESET_ALL
;
454 writel(val
, priv
->glb_base
+ GLB_SOFT_RESET
);
459 static int hisi_femac_net_open(struct net_device
*dev
)
461 struct hisi_femac_priv
*priv
= netdev_priv(dev
);
463 hisi_femac_port_reset(priv
);
464 hisi_femac_set_hw_mac_addr(priv
, dev
->dev_addr
);
465 hisi_femac_rx_refill(priv
);
467 netif_carrier_off(dev
);
468 netdev_reset_queue(dev
);
469 netif_start_queue(dev
);
470 napi_enable(&priv
->napi
);
472 priv
->link_status
= 0;
474 phy_start(dev
->phydev
);
476 writel(IRQ_ENA_PORT0_MASK
, priv
->glb_base
+ GLB_IRQ_RAW
);
477 hisi_femac_irq_enable(priv
, IRQ_ENA_ALL
| IRQ_ENA_PORT0
| DEF_INT_MASK
);
482 static int hisi_femac_net_close(struct net_device
*dev
)
484 struct hisi_femac_priv
*priv
= netdev_priv(dev
);
486 hisi_femac_irq_disable(priv
, IRQ_ENA_PORT0
);
489 phy_stop(dev
->phydev
);
491 netif_stop_queue(dev
);
492 napi_disable(&priv
->napi
);
494 hisi_femac_free_skb_rings(priv
);
499 static netdev_tx_t
hisi_femac_net_xmit(struct sk_buff
*skb
,
500 struct net_device
*dev
)
502 struct hisi_femac_priv
*priv
= netdev_priv(dev
);
503 struct hisi_femac_queue
*txq
= &priv
->txq
;
507 val
= readl(priv
->port_base
+ ADDRQ_STAT
);
510 hisi_femac_irq_enable(priv
, IRQ_INT_TX_PER_PACKET
);
511 dev
->stats
.tx_dropped
++;
512 dev
->stats
.tx_fifo_errors
++;
513 netif_stop_queue(dev
);
514 return NETDEV_TX_BUSY
;
517 if (unlikely(!CIRC_SPACE(txq
->head
, txq
->tail
,
519 hisi_femac_irq_enable(priv
, IRQ_INT_TX_PER_PACKET
);
520 dev
->stats
.tx_dropped
++;
521 dev
->stats
.tx_fifo_errors
++;
522 netif_stop_queue(dev
);
523 return NETDEV_TX_BUSY
;
526 addr
= dma_map_single(priv
->dev
, skb
->data
,
527 skb
->len
, DMA_TO_DEVICE
);
528 if (unlikely(dma_mapping_error(priv
->dev
, addr
))) {
529 dev_kfree_skb_any(skb
);
530 dev
->stats
.tx_dropped
++;
533 txq
->dma_phys
[txq
->head
] = addr
;
535 txq
->skb
[txq
->head
] = skb
;
536 txq
->head
= (txq
->head
+ 1) % txq
->num
;
538 writel(addr
, priv
->port_base
+ EQ_ADDR
);
539 writel(skb
->len
+ ETH_FCS_LEN
, priv
->port_base
+ EQFRM_LEN
);
541 priv
->tx_fifo_used_cnt
++;
543 dev
->stats
.tx_packets
++;
544 dev
->stats
.tx_bytes
+= skb
->len
;
545 netdev_sent_queue(dev
, skb
->len
);
550 static int hisi_femac_set_mac_address(struct net_device
*dev
, void *p
)
552 struct hisi_femac_priv
*priv
= netdev_priv(dev
);
553 struct sockaddr
*skaddr
= p
;
555 if (!is_valid_ether_addr(skaddr
->sa_data
))
556 return -EADDRNOTAVAIL
;
558 memcpy(dev
->dev_addr
, skaddr
->sa_data
, dev
->addr_len
);
559 dev
->addr_assign_type
&= ~NET_ADDR_RANDOM
;
561 hisi_femac_set_hw_mac_addr(priv
, dev
->dev_addr
);
566 static void hisi_femac_enable_hw_addr_filter(struct hisi_femac_priv
*priv
,
567 unsigned int reg_n
, bool enable
)
571 val
= readl(priv
->glb_base
+ GLB_MAC_H16(reg_n
));
573 val
|= BIT_MACFLT_ENA
;
575 val
&= ~BIT_MACFLT_ENA
;
576 writel(val
, priv
->glb_base
+ GLB_MAC_H16(reg_n
));
579 static void hisi_femac_set_hw_addr_filter(struct hisi_femac_priv
*priv
,
583 unsigned int high
, low
;
586 high
= GLB_MAC_H16(reg_n
);
587 low
= GLB_MAC_L32(reg_n
);
589 val
= (addr
[2] << 24) | (addr
[3] << 16) | (addr
[4] << 8) | addr
[5];
590 writel(val
, priv
->glb_base
+ low
);
592 val
= readl(priv
->glb_base
+ high
);
593 val
&= ~MACFLT_HI16_MASK
;
594 val
|= ((addr
[0] << 8) | addr
[1]);
595 val
|= (BIT_MACFLT_ENA
| BIT_MACFLT_FW2CPU
);
596 writel(val
, priv
->glb_base
+ high
);
599 static void hisi_femac_set_promisc_mode(struct hisi_femac_priv
*priv
,
604 val
= readl(priv
->glb_base
+ GLB_FWCTRL
);
606 val
|= FWCTRL_FWALL2CPU
;
608 val
&= ~FWCTRL_FWALL2CPU
;
609 writel(val
, priv
->glb_base
+ GLB_FWCTRL
);
612 /* Handle multiple multicast addresses (perfect filtering)*/
613 static void hisi_femac_set_mc_addr_filter(struct hisi_femac_priv
*priv
)
615 struct net_device
*dev
= priv
->ndev
;
618 val
= readl(priv
->glb_base
+ GLB_MACTCTRL
);
619 if ((netdev_mc_count(dev
) > MAX_MULTICAST_ADDRESSES
) ||
620 (dev
->flags
& IFF_ALLMULTI
)) {
621 val
|= MACTCTRL_MULTI2CPU
;
623 int reg
= MAX_UNICAST_ADDRESSES
;
625 struct netdev_hw_addr
*ha
;
627 for (i
= reg
; i
< MAX_MAC_FILTER_NUM
; i
++)
628 hisi_femac_enable_hw_addr_filter(priv
, i
, false);
630 netdev_for_each_mc_addr(ha
, dev
) {
631 hisi_femac_set_hw_addr_filter(priv
, ha
->addr
, reg
);
634 val
&= ~MACTCTRL_MULTI2CPU
;
636 writel(val
, priv
->glb_base
+ GLB_MACTCTRL
);
639 /* Handle multiple unicast addresses (perfect filtering)*/
640 static void hisi_femac_set_uc_addr_filter(struct hisi_femac_priv
*priv
)
642 struct net_device
*dev
= priv
->ndev
;
645 val
= readl(priv
->glb_base
+ GLB_MACTCTRL
);
646 if (netdev_uc_count(dev
) > MAX_UNICAST_ADDRESSES
) {
647 val
|= MACTCTRL_UNI2CPU
;
651 struct netdev_hw_addr
*ha
;
653 for (i
= reg
; i
< MAX_UNICAST_ADDRESSES
; i
++)
654 hisi_femac_enable_hw_addr_filter(priv
, i
, false);
656 netdev_for_each_uc_addr(ha
, dev
) {
657 hisi_femac_set_hw_addr_filter(priv
, ha
->addr
, reg
);
660 val
&= ~MACTCTRL_UNI2CPU
;
662 writel(val
, priv
->glb_base
+ GLB_MACTCTRL
);
665 static void hisi_femac_net_set_rx_mode(struct net_device
*dev
)
667 struct hisi_femac_priv
*priv
= netdev_priv(dev
);
669 if (dev
->flags
& IFF_PROMISC
) {
670 hisi_femac_set_promisc_mode(priv
, true);
672 hisi_femac_set_promisc_mode(priv
, false);
673 hisi_femac_set_mc_addr_filter(priv
);
674 hisi_femac_set_uc_addr_filter(priv
);
678 static const struct ethtool_ops hisi_femac_ethtools_ops
= {
679 .get_link
= ethtool_op_get_link
,
680 .get_link_ksettings
= phy_ethtool_get_link_ksettings
,
681 .set_link_ksettings
= phy_ethtool_set_link_ksettings
,
684 static const struct net_device_ops hisi_femac_netdev_ops
= {
685 .ndo_open
= hisi_femac_net_open
,
686 .ndo_stop
= hisi_femac_net_close
,
687 .ndo_start_xmit
= hisi_femac_net_xmit
,
688 .ndo_do_ioctl
= phy_do_ioctl_running
,
689 .ndo_set_mac_address
= hisi_femac_set_mac_address
,
690 .ndo_set_rx_mode
= hisi_femac_net_set_rx_mode
,
693 static void hisi_femac_core_reset(struct hisi_femac_priv
*priv
)
695 reset_control_assert(priv
->mac_rst
);
696 reset_control_deassert(priv
->mac_rst
);
699 static void hisi_femac_sleep_us(u32 time_us
)
706 time_ms
= DIV_ROUND_UP(time_us
, 1000);
708 usleep_range(time_us
, time_us
+ 500);
713 static void hisi_femac_phy_reset(struct hisi_femac_priv
*priv
)
715 /* To make sure PHY hardware reset success,
716 * we must keep PHY in deassert state first and
717 * then complete the hardware reset operation
719 reset_control_deassert(priv
->phy_rst
);
720 hisi_femac_sleep_us(priv
->phy_reset_delays
[PRE_DELAY
]);
722 reset_control_assert(priv
->phy_rst
);
723 /* delay some time to ensure reset ok,
724 * this depends on PHY hardware feature
726 hisi_femac_sleep_us(priv
->phy_reset_delays
[PULSE
]);
727 reset_control_deassert(priv
->phy_rst
);
728 /* delay some time to ensure later MDIO access */
729 hisi_femac_sleep_us(priv
->phy_reset_delays
[POST_DELAY
]);
732 static void hisi_femac_port_init(struct hisi_femac_priv
*priv
)
736 /* MAC gets link status info and phy mode by software config */
737 val
= MAC_PORTSEL_STAT_CPU
;
738 if (priv
->ndev
->phydev
->interface
== PHY_INTERFACE_MODE_RMII
)
739 val
|= MAC_PORTSEL_RMII
;
740 writel(val
, priv
->port_base
+ MAC_PORTSEL
);
742 /*clear all interrupt status */
743 writel(IRQ_ENA_PORT0_MASK
, priv
->glb_base
+ GLB_IRQ_RAW
);
744 hisi_femac_irq_disable(priv
, IRQ_ENA_PORT0_MASK
| IRQ_ENA_PORT0
);
746 val
= readl(priv
->glb_base
+ GLB_FWCTRL
);
747 val
&= ~(FWCTRL_VLAN_ENABLE
| FWCTRL_FWALL2CPU
);
748 val
|= FWCTRL_FW2CPU_ENA
;
749 writel(val
, priv
->glb_base
+ GLB_FWCTRL
);
751 val
= readl(priv
->glb_base
+ GLB_MACTCTRL
);
752 val
|= (MACTCTRL_BROAD2CPU
| MACTCTRL_MACT_ENA
);
753 writel(val
, priv
->glb_base
+ GLB_MACTCTRL
);
755 val
= readl(priv
->port_base
+ MAC_SET
);
756 val
&= ~MAX_FRAME_SIZE_MASK
;
757 val
|= MAX_FRAME_SIZE
;
758 writel(val
, priv
->port_base
+ MAC_SET
);
760 val
= RX_COALESCED_TIMER
|
761 (RX_COALESCED_FRAMES
<< RX_COALESCED_FRAME_OFFSET
);
762 writel(val
, priv
->port_base
+ RX_COALESCE_SET
);
764 val
= (HW_RX_FIFO_DEPTH
<< RX_DEPTH_OFFSET
) | HW_TX_FIFO_DEPTH
;
765 writel(val
, priv
->port_base
+ QLEN_SET
);
768 static int hisi_femac_drv_probe(struct platform_device
*pdev
)
770 struct device
*dev
= &pdev
->dev
;
771 struct device_node
*node
= dev
->of_node
;
772 struct net_device
*ndev
;
773 struct hisi_femac_priv
*priv
;
774 struct phy_device
*phy
;
775 const char *mac_addr
;
778 ndev
= alloc_etherdev(sizeof(*priv
));
782 platform_set_drvdata(pdev
, ndev
);
783 SET_NETDEV_DEV(ndev
, &pdev
->dev
);
785 priv
= netdev_priv(ndev
);
789 priv
->port_base
= devm_platform_ioremap_resource(pdev
, 0);
790 if (IS_ERR(priv
->port_base
)) {
791 ret
= PTR_ERR(priv
->port_base
);
792 goto out_free_netdev
;
795 priv
->glb_base
= devm_platform_ioremap_resource(pdev
, 1);
796 if (IS_ERR(priv
->glb_base
)) {
797 ret
= PTR_ERR(priv
->glb_base
);
798 goto out_free_netdev
;
801 priv
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
802 if (IS_ERR(priv
->clk
)) {
803 dev_err(dev
, "failed to get clk\n");
805 goto out_free_netdev
;
808 ret
= clk_prepare_enable(priv
->clk
);
810 dev_err(dev
, "failed to enable clk %d\n", ret
);
811 goto out_free_netdev
;
814 priv
->mac_rst
= devm_reset_control_get(dev
, "mac");
815 if (IS_ERR(priv
->mac_rst
)) {
816 ret
= PTR_ERR(priv
->mac_rst
);
817 goto out_disable_clk
;
819 hisi_femac_core_reset(priv
);
821 priv
->phy_rst
= devm_reset_control_get(dev
, "phy");
822 if (IS_ERR(priv
->phy_rst
)) {
823 priv
->phy_rst
= NULL
;
825 ret
= of_property_read_u32_array(node
,
826 PHY_RESET_DELAYS_PROPERTY
,
827 priv
->phy_reset_delays
,
830 goto out_disable_clk
;
831 hisi_femac_phy_reset(priv
);
834 phy
= of_phy_get_and_connect(ndev
, node
, hisi_femac_adjust_link
);
836 dev_err(dev
, "connect to PHY failed!\n");
838 goto out_disable_clk
;
841 phy_attached_print(phy
, "phy_id=0x%.8lx, phy_mode=%s\n",
842 (unsigned long)phy
->phy_id
,
843 phy_modes(phy
->interface
));
845 mac_addr
= of_get_mac_address(node
);
846 if (!IS_ERR(mac_addr
))
847 ether_addr_copy(ndev
->dev_addr
, mac_addr
);
848 if (!is_valid_ether_addr(ndev
->dev_addr
)) {
849 eth_hw_addr_random(ndev
);
850 dev_warn(dev
, "using random MAC address %pM\n",
854 ndev
->watchdog_timeo
= 6 * HZ
;
855 ndev
->priv_flags
|= IFF_UNICAST_FLT
;
856 ndev
->netdev_ops
= &hisi_femac_netdev_ops
;
857 ndev
->ethtool_ops
= &hisi_femac_ethtools_ops
;
858 netif_napi_add(ndev
, &priv
->napi
, hisi_femac_poll
, FEMAC_POLL_WEIGHT
);
860 hisi_femac_port_init(priv
);
862 ret
= hisi_femac_init_tx_and_rx_queues(priv
);
864 goto out_disconnect_phy
;
866 ndev
->irq
= platform_get_irq(pdev
, 0);
867 if (ndev
->irq
<= 0) {
869 goto out_disconnect_phy
;
872 ret
= devm_request_irq(dev
, ndev
->irq
, hisi_femac_interrupt
,
873 IRQF_SHARED
, pdev
->name
, ndev
);
875 dev_err(dev
, "devm_request_irq %d failed!\n", ndev
->irq
);
876 goto out_disconnect_phy
;
879 ret
= register_netdev(ndev
);
881 dev_err(dev
, "register_netdev failed!\n");
882 goto out_disconnect_phy
;
888 netif_napi_del(&priv
->napi
);
891 clk_disable_unprepare(priv
->clk
);
898 static int hisi_femac_drv_remove(struct platform_device
*pdev
)
900 struct net_device
*ndev
= platform_get_drvdata(pdev
);
901 struct hisi_femac_priv
*priv
= netdev_priv(ndev
);
903 netif_napi_del(&priv
->napi
);
904 unregister_netdev(ndev
);
906 phy_disconnect(ndev
->phydev
);
907 clk_disable_unprepare(priv
->clk
);
914 static int hisi_femac_drv_suspend(struct platform_device
*pdev
,
917 struct net_device
*ndev
= platform_get_drvdata(pdev
);
918 struct hisi_femac_priv
*priv
= netdev_priv(ndev
);
920 disable_irq(ndev
->irq
);
921 if (netif_running(ndev
)) {
922 hisi_femac_net_close(ndev
);
923 netif_device_detach(ndev
);
926 clk_disable_unprepare(priv
->clk
);
931 static int hisi_femac_drv_resume(struct platform_device
*pdev
)
933 struct net_device
*ndev
= platform_get_drvdata(pdev
);
934 struct hisi_femac_priv
*priv
= netdev_priv(ndev
);
936 clk_prepare_enable(priv
->clk
);
938 hisi_femac_phy_reset(priv
);
940 if (netif_running(ndev
)) {
941 hisi_femac_port_init(priv
);
942 hisi_femac_net_open(ndev
);
943 netif_device_attach(ndev
);
945 enable_irq(ndev
->irq
);
951 static const struct of_device_id hisi_femac_match
[] = {
952 {.compatible
= "hisilicon,hisi-femac-v1",},
953 {.compatible
= "hisilicon,hisi-femac-v2",},
954 {.compatible
= "hisilicon,hi3516cv300-femac",},
958 MODULE_DEVICE_TABLE(of
, hisi_femac_match
);
960 static struct platform_driver hisi_femac_driver
= {
962 .name
= "hisi-femac",
963 .of_match_table
= hisi_femac_match
,
965 .probe
= hisi_femac_drv_probe
,
966 .remove
= hisi_femac_drv_remove
,
968 .suspend
= hisi_femac_drv_suspend
,
969 .resume
= hisi_femac_drv_resume
,
973 module_platform_driver(hisi_femac_driver
);
975 MODULE_DESCRIPTION("Hisilicon Fast Ethernet MAC driver");
976 MODULE_AUTHOR("Dongpo Li <lidongpo@hisilicon.com>");
977 MODULE_LICENSE("GPL v2");
978 MODULE_ALIAS("platform:hisi-femac");