2 * Moschip MCS8140 Ethernet MAC driver
4 * Copyright (C) 2003, Moschip Semiconductors
5 * Copyright (C) 2012, Florian Fainelli <florian@openwrt.org>
9 #include <linux/module.h>
10 #include <linux/slab.h>
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/netdevice.h>
14 #include <linux/etherdevice.h>
15 #include <linux/delay.h>
16 #include <linux/ethtool.h>
17 #include <linux/mii.h>
19 #include <linux/interrupt.h>
20 #include <linux/platform_device.h>
22 #include <linux/of_mdio.h>
23 #include <linux/of_net.h>
24 #include <linux/irq.h>
25 #include <linux/err.h>
26 #include <linux/phy.h>
27 #include <linux/clk.h>
28 #include <linux/dma-mapping.h>
30 #include <asm/unaligned.h>
31 #include <asm/sizes.h>
33 /* Hardware registers */
34 #define MAC_BASE_ADDR ((priv->mac_base))
36 #define CTRL_REG (MAC_BASE_ADDR)
37 #define MII_BUSY (1 << 0)
38 #define MII_WRITE (1 << 1)
39 #define RX_ENABLE (1 << 2)
40 #define TX_ENABLE (1 << 3)
41 #define DEFER_CHECK (1 << 5)
42 #define STRIP_PAD (1 << 8)
43 #define DRTRY_DISABLE (1 << 10)
44 #define FULL_DUPLEX (1 << 20)
45 #define HBD_DISABLE (1 << 28)
46 #define MAC_ADDR_HIGH_REG (MAC_BASE_ADDR + 0x04)
47 #define MAC_ADDR_LOW_REG (MAC_BASE_ADDR + 0x08)
48 #define MII_ADDR_REG (MAC_BASE_ADDR + 0x14)
49 #define MII_ADDR_SHIFT (11)
50 #define MII_REG_SHIFT (6)
51 #define MII_DATA_REG (MAC_BASE_ADDR + 0x18)
52 /* Link interrupt registers */
53 #define LINK_INT_CSR (MAC_BASE_ADDR + 0xD0)
54 #define LINK_INT_EN (1 << 0)
55 #define LINK_PHY_ADDR_SHIFT (1)
56 #define LINK_PHY_REG_SHIFT (6)
57 #define LINK_BIT_UP_SHIFT (11)
58 #define LINK_UP (1 << 16)
59 #define LINK_INT_POLL_TIME (MAC_BASE_ADDR + 0xD4)
60 #define LINK_POLL_MASK ((1 << 20) - 1)
62 #define DMA_CHAN_WIDTH 32
66 /* Receive DMA registers */
67 #define RX_DMA_BASE ((priv->dma_base) + \
68 (DMA_CHAN_WIDTH * DMA_RX_CHAN))
69 #define RX_BUFFER_ADDR (RX_DMA_BASE + 0x00)
70 #define RX_MAX_BYTES (RX_DMA_BASE + 0x04)
71 #define RX_ACT_BYTES (RX_DMA_BASE + 0x08)
72 #define RX_START_DMA (RX_DMA_BASE + 0x0C)
73 #define RX_DMA_ENABLE (1 << 0)
74 #define RX_DMA_RESET (1 << 1)
75 #define RX_DMA_STATUS_FIFO (1 << 12)
76 #define RX_DMA_ENH (RX_DMA_BASE + 0x14)
77 #define RX_DMA_INT_ENABLE (1 << 1)
79 /* Transmit DMA registers */
80 #define TX_DMA_BASE ((priv->dma_base) + \
81 (DMA_CHAN_WIDTH * DMA_TX_CHAN))
82 #define TX_BUFFER_ADDR (TX_DMA_BASE + 0x00)
83 #define TX_PKT_BYTES (TX_DMA_BASE + 0x04)
84 #define TX_BYTES_SENT (TX_DMA_BASE + 0x08)
85 #define TX_START_DMA (TX_DMA_BASE + 0x0C)
86 #define TX_DMA_ENABLE (1 << 0)
87 #define TX_DMA_START_FRAME (1 << 2)
88 #define TX_DMA_END_FRAME (1 << 3)
89 #define TX_DMA_PAD_DISABLE (1 << 8)
90 #define TX_DMA_CRC_DISABLE (1 << 9)
91 #define TX_DMA_FIFO_FULL (1 << 16)
92 #define TX_DMA_FIFO_EMPTY (1 << 17)
93 #define TX_DMA_STATUS_AVAIL (1 << 18)
94 #define TX_DMA_RESET (1 << 24)
95 #define TX_DMA_STATUS (TX_DMA_BASE + 0x10)
96 #define TX_DMA_ENH (TX_DMA_BASE + 0x14)
97 #define TX_DMA_ENH_ENABLE (1 << 0)
98 #define TX_DMA_INT_FIFO (1 << 1)
100 #define RX_ALLOC_SIZE SZ_2K
101 #define MAX_ETH_FRAME_SIZE 1536
102 #define RX_SKB_TAILROOM 128
103 #define RX_SKB_HEADROOM (RX_ALLOC_SIZE - \
104 (MAX_ETH_FRAME_SIZE + RX_SKB_TAILROOM) + 0)
106 /* WDT Late COL Lenght COL Type */
107 #define ERROR_FILTER_MASK ((1<<14) | (1<<15) | (1<<16) | (1<<17) | (0<<18) | \
108 /* MII Dribbling CRC Len/type Control */\
109 (1<<19) | (1<<20) | (1<<21) | (0<<24) | (1<<25) | \
112 #define TX_RING_SIZE 30
113 #define RX_RING_SIZE 30
115 static inline u32
nuport_mac_readl(void __iomem
*reg
)
117 return readl_relaxed(reg
);
120 static inline u8
nuport_mac_readb(void __iomem
*reg
)
122 return readb_relaxed(reg
);
125 static inline void nuport_mac_writel(u32 value
, void __iomem
*reg
)
127 writel_relaxed(value
, reg
);
130 static inline void nuport_mac_writeb(u8 value
, void __iomem
*reg
)
132 writel_relaxed(value
, reg
);
135 /* MAC private data */
136 struct nuport_mac_priv
{
139 void __iomem
*mac_base
;
140 void __iomem
*dma_base
;
145 struct clk
*emac_clk
;
146 struct clk
*ephy_clk
;
148 /* Transmit buffers */
149 struct sk_buff
*tx_skb
[TX_RING_SIZE
];
151 unsigned int valid_txskb
[TX_RING_SIZE
];
154 unsigned int tx_full
;
156 /* Receive buffers */
157 struct sk_buff
*rx_skb
[RX_RING_SIZE
];
159 unsigned int irq_rxskb
[RX_RING_SIZE
];
160 int pkt_len
[RX_RING_SIZE
];
163 unsigned int rx_full
;
165 unsigned int first_pkt
;
168 struct napi_struct napi
;
169 struct net_device
*dev
;
170 struct platform_device
*pdev
;
171 struct mii_bus
*mii_bus
;
172 struct phy_device
*phydev
;
173 struct device_node
*phy_node
;
174 phy_interface_t phy_interface
;
178 unsigned int buffer_shifting_len
;
181 static inline int nuport_mac_mii_busy_wait(struct nuport_mac_priv
*priv
)
184 unsigned long finish
= jiffies
+ 3 * HZ
;
188 if (!(nuport_mac_readl(MII_ADDR_REG
) & MII_BUSY
))
191 } while (!time_after_eq(curr
, finish
));
196 /* Read from PHY registers */
197 static int nuport_mac_mii_read(struct mii_bus
*bus
,
198 int mii_id
, int regnum
)
200 struct net_device
*dev
= bus
->priv
;
201 struct nuport_mac_priv
*priv
= netdev_priv(dev
);
205 ret
= nuport_mac_mii_busy_wait(priv
);
209 val
|= (mii_id
<< MII_ADDR_SHIFT
) | (regnum
<< MII_REG_SHIFT
) | MII_BUSY
;
210 nuport_mac_writel(val
, MII_ADDR_REG
);
211 ret
= nuport_mac_mii_busy_wait(priv
);
215 return nuport_mac_readl(MII_DATA_REG
);
218 static int nuport_mac_mii_write(struct mii_bus
*bus
, int mii_id
,
219 int regnum
, u16 value
)
221 struct net_device
*dev
= bus
->priv
;
222 struct nuport_mac_priv
*priv
= netdev_priv(dev
);
226 ret
= nuport_mac_mii_busy_wait(priv
);
230 val
|= (mii_id
<< MII_ADDR_SHIFT
) | (regnum
<< MII_REG_SHIFT
);
231 val
|= MII_BUSY
| MII_WRITE
;
232 nuport_mac_writel(value
, MII_DATA_REG
);
233 nuport_mac_writel(val
, MII_ADDR_REG
);
235 return nuport_mac_mii_busy_wait(priv
);
238 static int nuport_mac_mii_reset(struct mii_bus
*bus
)
243 static int nuport_mac_start_tx_dma(struct nuport_mac_priv
*priv
,
247 unsigned int timeout
= 2048;
250 reg
= nuport_mac_readl(TX_START_DMA
);
251 if (!(reg
& TX_DMA_ENABLE
)) {
252 netdev_dbg(priv
->dev
, "dma ready\n");
261 priv
->tx_addr
= dma_map_single(&priv
->pdev
->dev
, skb
->data
,
262 skb
->len
, DMA_TO_DEVICE
);
263 if (dma_mapping_error(&priv
->pdev
->dev
, priv
->tx_addr
))
266 /* enable enhanced mode */
267 nuport_mac_writel(TX_DMA_ENH_ENABLE
, TX_DMA_ENH
);
268 nuport_mac_writel(priv
->tx_addr
, TX_BUFFER_ADDR
);
269 nuport_mac_writel((skb
->len
) - 1, TX_PKT_BYTES
);
271 reg
= TX_DMA_ENABLE
| TX_DMA_START_FRAME
| TX_DMA_END_FRAME
;
272 nuport_mac_writel(reg
, TX_START_DMA
);
277 static void nuport_mac_reset_tx_dma(struct nuport_mac_priv
*priv
)
281 reg
= nuport_mac_readl(TX_START_DMA
);
283 nuport_mac_writel(reg
, TX_START_DMA
);
286 static int nuport_mac_start_rx_dma(struct nuport_mac_priv
*priv
,
290 unsigned int timeout
= 2048;
293 reg
= nuport_mac_readl(RX_START_DMA
);
294 if (!(reg
& RX_DMA_ENABLE
)) {
295 netdev_dbg(priv
->dev
, "dma ready\n");
304 priv
->rx_addr
= dma_map_single(&priv
->pdev
->dev
, skb
->data
,
305 RX_ALLOC_SIZE
, DMA_FROM_DEVICE
);
306 if (dma_mapping_error(&priv
->pdev
->dev
, priv
->rx_addr
))
309 nuport_mac_writel(priv
->rx_addr
, RX_BUFFER_ADDR
);
311 nuport_mac_writel(RX_DMA_ENABLE
, RX_START_DMA
);
316 static void nuport_mac_reset_rx_dma(struct nuport_mac_priv
*priv
)
320 reg
= nuport_mac_readl(RX_START_DMA
);
322 nuport_mac_writel(reg
, RX_START_DMA
);
325 /* I suppose this might do something, but I am not sure actually */
326 static void nuport_mac_disable_rx_dma(struct nuport_mac_priv
*priv
)
330 reg
= nuport_mac_readl(RX_DMA_ENH
);
331 reg
&= ~RX_DMA_INT_ENABLE
;
332 nuport_mac_writel(reg
, RX_DMA_ENH
);
335 static void nuport_mac_enable_rx_dma(struct nuport_mac_priv
*priv
)
339 reg
= nuport_mac_readl(RX_DMA_ENH
);
340 reg
|= RX_DMA_INT_ENABLE
;
341 nuport_mac_writel(reg
, RX_DMA_ENH
);
344 /* Add packets to the transmit queue */
345 static int nuport_mac_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
348 struct nuport_mac_priv
*priv
= netdev_priv(dev
);
351 if (netif_queue_stopped(dev
)) {
352 netdev_warn(dev
, "netif queue was stopped, restarting\n");
353 netif_start_queue(dev
);
356 spin_lock_irqsave(&priv
->lock
, flags
);
357 if (priv
->first_pkt
) {
358 ret
= nuport_mac_start_tx_dma(priv
, skb
);
360 netif_stop_queue(dev
);
361 spin_unlock_irqrestore(&priv
->lock
, flags
);
362 netdev_err(dev
, "transmit path busy\n");
363 return NETDEV_TX_BUSY
;
368 priv
->tx_skb
[priv
->cur_tx
] = skb
;
369 dev
->stats
.tx_bytes
+= skb
->len
;
370 dev
->stats
.tx_packets
++;
371 priv
->valid_txskb
[priv
->cur_tx
] = 1;
373 dev
->trans_start
= jiffies
;
375 if (priv
->cur_tx
>= TX_RING_SIZE
)
378 spin_unlock_irqrestore(&priv
->lock
, flags
);
380 if (priv
->valid_txskb
[priv
->cur_tx
]) {
382 netdev_err(dev
, "stopping queue\n");
383 netif_stop_queue(dev
);
389 static void nuport_mac_adjust_link(struct net_device
*dev
)
391 struct nuport_mac_priv
*priv
= netdev_priv(dev
);
392 struct phy_device
*phydev
= priv
->phydev
;
393 unsigned int status_changed
= 0;
398 if (priv
->old_link
!= phydev
->link
) {
400 priv
->old_link
= phydev
->link
;
403 if (phydev
->link
&& (priv
->old_duplex
!= phydev
->duplex
)) {
404 reg
= nuport_mac_readl(CTRL_REG
);
405 if (phydev
->duplex
== DUPLEX_FULL
)
409 nuport_mac_writel(reg
, CTRL_REG
);
412 priv
->old_duplex
= phydev
->duplex
;
418 pr_info("%s: link %s", dev
->name
, phydev
->link
?
421 pr_cont(" - %d/%s", phydev
->speed
,
422 phydev
->duplex
== DUPLEX_FULL
? "full" : "half");
427 static irqreturn_t
nuport_mac_link_interrupt(int irq
, void *dev_id
)
429 struct net_device
*dev
= dev_id
;
430 struct nuport_mac_priv
*priv
= netdev_priv(dev
);
434 irqreturn_t ret
= IRQ_HANDLED
;
436 spin_lock_irqsave(&priv
->lock
, flags
);
437 reg
= nuport_mac_readl(LINK_INT_CSR
);
438 phy_addr
= (reg
>> LINK_PHY_ADDR_SHIFT
) & (PHY_MAX_ADDR
- 1);
440 if (phy_addr
!= priv
->phydev
->addr
) {
441 netdev_err(dev
, "spurious PHY irq (phy: %d)\n", phy_addr
);
446 priv
->phydev
->link
= (reg
& LINK_UP
);
447 nuport_mac_adjust_link(dev
);
450 spin_unlock_irqrestore(&priv
->lock
, flags
);
454 static irqreturn_t
nuport_mac_tx_interrupt(int irq
, void *dev_id
)
456 struct net_device
*dev
= (struct net_device
*)dev_id
;
457 struct nuport_mac_priv
*priv
= netdev_priv(dev
);
463 spin_lock_irqsave(&priv
->lock
, flags
);
464 /* clear status word available if ready */
465 reg
= nuport_mac_readl(TX_START_DMA
);
466 if (reg
& TX_DMA_STATUS_AVAIL
) {
467 nuport_mac_writel(reg
, TX_START_DMA
);
468 reg
= nuport_mac_readl(TX_DMA_STATUS
);
471 dev
->stats
.tx_errors
++;
473 netdev_dbg(dev
, "no status word: %08x\n", reg
);
475 skb
= priv
->tx_skb
[priv
->dma_tx
];
476 priv
->tx_skb
[priv
->dma_tx
] = NULL
;
477 priv
->valid_txskb
[priv
->dma_tx
] = 0;
478 dma_unmap_single(&priv
->pdev
->dev
, priv
->rx_addr
, skb
->len
,
480 dev_kfree_skb_irq(skb
);
483 if (priv
->dma_tx
>= TX_RING_SIZE
)
486 if (!priv
->valid_txskb
[priv
->dma_tx
])
489 ret
= nuport_mac_start_tx_dma(priv
, priv
->tx_skb
[priv
->dma_tx
]);
491 netdev_err(dev
, "failed to restart TX dma\n");
495 netdev_dbg(dev
, "restarting transmit queue\n");
496 netif_wake_queue(dev
);
500 spin_unlock_irqrestore(&priv
->lock
, flags
);
505 static unsigned int nuport_mac_has_work(struct nuport_mac_priv
*priv
)
509 for (i
= 0; i
< RX_RING_SIZE
; i
++)
516 static irqreturn_t
nuport_mac_rx_interrupt(int irq
, void *dev_id
)
518 struct net_device
*dev
= (struct net_device
*)dev_id
;
519 struct nuport_mac_priv
*priv
= netdev_priv(dev
);
523 spin_lock_irqsave(&priv
->lock
, flags
);
524 if (!priv
->rx_full
) {
525 priv
->pkt_len
[priv
->dma_rx
] = nuport_mac_readl(RX_ACT_BYTES
) - 4;
526 priv
->irq_rxskb
[priv
->dma_rx
] = 0;
529 if (priv
->dma_rx
>= RX_RING_SIZE
)
534 if (priv
->irq_rxskb
[priv
->dma_rx
] == 1) {
535 ret
= nuport_mac_start_rx_dma(priv
, priv
->rx_skb
[priv
->dma_rx
]);
537 netdev_err(dev
, "failed to start rx dma\n");
540 netdev_dbg(dev
, "RX ring full\n");
543 if (likely(nuport_mac_has_work(priv
))) {
544 /* find a way to disable DMA rx irq */
545 nuport_mac_disable_rx_dma(priv
);
546 napi_schedule(&priv
->napi
);
548 spin_unlock_irqrestore(&priv
->lock
, flags
);
553 /* Process received packets in tasklet */
554 static int nuport_mac_rx(struct net_device
*dev
, int limit
)
556 struct nuport_mac_priv
*priv
= netdev_priv(dev
);
561 while (count
< limit
&& !priv
->irq_rxskb
[priv
->cur_rx
]) {
562 skb
= priv
->rx_skb
[priv
->cur_rx
];
563 len
= priv
->pkt_len
[priv
->cur_rx
];
565 /* Remove 2 bytes added by RX buffer shifting */
566 len
= len
- priv
->buffer_shifting_len
;
567 skb
->data
= skb
->data
+ priv
->buffer_shifting_len
;
569 /* Get packet status */
570 status
= get_unaligned((u32
*) (skb
->data
+ len
));
572 dma_unmap_single(&priv
->pdev
->dev
, priv
->rx_addr
, skb
->len
,
575 /* packet filter failed */
576 if (!(status
& (1 << 30))) {
577 dev_kfree_skb_irq(skb
);
582 if (status
& (1 << 31)) {
583 dev
->stats
.rx_missed_errors
++;
584 dev_kfree_skb_irq(skb
);
588 /* Not ethernet type */
589 if ((!(status
& (1 << 18))) || (status
& ERROR_FILTER_MASK
))
590 dev
->stats
.rx_errors
++;
592 if (len
> MAX_ETH_FRAME_SIZE
) {
593 dev_kfree_skb_irq(skb
);
598 skb
->protocol
= eth_type_trans(skb
, dev
);
599 dev
->stats
.rx_packets
++;
601 if (status
& (1 << 29))
602 skb
->pkt_type
= PACKET_OTHERHOST
;
603 if (status
& (1 << 27))
604 skb
->pkt_type
= PACKET_MULTICAST
;
605 if (status
& (1 << 28))
606 skb
->pkt_type
= PACKET_BROADCAST
;
608 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
610 /* Pass the received packet to network layer */
611 status
= netif_receive_skb(skb
);
612 if (status
!= NET_RX_DROP
)
613 dev
->stats
.rx_bytes
+= len
- 4; /* Without CRC */
615 dev
->stats
.rx_dropped
++;
617 dev
->last_rx
= jiffies
;
620 skb
= netdev_alloc_skb(dev
, RX_ALLOC_SIZE
);
624 skb_reserve(skb
, RX_SKB_HEADROOM
);
625 priv
->rx_skb
[priv
->cur_rx
] = skb
;
626 priv
->irq_rxskb
[priv
->cur_rx
] = 1;
629 if (priv
->cur_rx
>= RX_RING_SIZE
)
637 static int nuport_mac_poll(struct napi_struct
*napi
, int budget
)
639 struct nuport_mac_priv
*priv
=
640 container_of(napi
, struct nuport_mac_priv
, napi
);
641 struct net_device
*dev
= priv
->dev
;
644 work_done
= nuport_mac_rx(dev
, budget
);
646 if (work_done
< budget
) {
648 nuport_mac_enable_rx_dma(priv
);
654 static void nuport_mac_init_tx_ring(struct nuport_mac_priv
*priv
)
658 priv
->cur_tx
= priv
->dma_tx
= priv
->tx_full
= 0;
659 for (i
= 0; i
< TX_RING_SIZE
; i
++) {
660 priv
->tx_skb
[i
] = NULL
;
661 priv
->valid_txskb
[i
] = 0;
666 static int nuport_mac_init_rx_ring(struct net_device
*dev
)
668 struct nuport_mac_priv
*priv
= netdev_priv(dev
);
672 priv
->cur_rx
= priv
->dma_rx
= priv
->rx_full
= 0;
674 for (i
= 0; i
< RX_RING_SIZE
; i
++) {
675 skb
= netdev_alloc_skb(dev
, RX_ALLOC_SIZE
);
678 skb_reserve(skb
, RX_SKB_HEADROOM
);
679 priv
->rx_skb
[i
] = skb
;
680 priv
->irq_rxskb
[i
] = 1;
686 static void nuport_mac_free_rx_ring(struct nuport_mac_priv
*priv
)
690 for (i
= 0; i
< RX_RING_SIZE
; i
++) {
691 if (!priv
->rx_skb
[i
])
694 dev_kfree_skb(priv
->rx_skb
[i
]);
695 priv
->rx_skb
[i
] = NULL
;
699 dma_unmap_single(&priv
->pdev
->dev
, priv
->rx_addr
, RX_ALLOC_SIZE
,
703 static void nuport_mac_read_mac_address(struct net_device
*dev
)
705 struct nuport_mac_priv
*priv
= netdev_priv(dev
);
708 for (i
= 0; i
< 4; i
++)
709 dev
->dev_addr
[i
] = nuport_mac_readb(MAC_ADDR_LOW_REG
+ i
);
710 dev
->dev_addr
[4] = nuport_mac_readb(MAC_ADDR_HIGH_REG
);
711 dev
->dev_addr
[5] = nuport_mac_readb(MAC_ADDR_HIGH_REG
+ 1);
713 if (!is_valid_ether_addr(dev
->dev_addr
)) {
714 dev_info(&priv
->pdev
->dev
, "using random address\n");
715 random_ether_addr(dev
->dev_addr
);
719 static int nuport_mac_change_mac_address(struct net_device
*dev
, void *mac_addr
)
721 struct sockaddr
*addr
= mac_addr
;
722 struct nuport_mac_priv
*priv
= netdev_priv(dev
);
723 unsigned long *temp
= (unsigned long *)dev
->dev_addr
;
726 if (netif_running(dev
))
729 memcpy(dev
->dev_addr
, addr
->sa_data
, ETH_ALEN
);
731 spin_lock_irq(&priv
->lock
);
733 nuport_mac_writel(*temp
, MAC_ADDR_LOW_REG
);
734 temp
= (unsigned long *)(dev
->dev_addr
+ 4);
735 nuport_mac_writel(*temp
, MAC_ADDR_HIGH_REG
);
737 low
= nuport_mac_readl(MAC_ADDR_LOW_REG
);
738 high
= nuport_mac_readl(MAC_ADDR_HIGH_REG
);
740 spin_unlock_irq(&priv
->lock
);
745 static int nuport_mac_open(struct net_device
*dev
)
748 struct nuport_mac_priv
*priv
= netdev_priv(dev
);
752 ret
= clk_enable(priv
->emac_clk
);
754 netdev_err(dev
, "failed to enable EMAC clock\n");
758 /* Set MAC into full duplex mode by default */
759 reg
|= RX_ENABLE
| TX_ENABLE
;
760 reg
|= DEFER_CHECK
| STRIP_PAD
| DRTRY_DISABLE
;
761 reg
|= FULL_DUPLEX
| HBD_DISABLE
;
762 nuport_mac_writel(reg
, CTRL_REG
);
764 /* set mac address in hardware in case it was not already */
765 nuport_mac_change_mac_address(dev
, dev
->dev_addr
);
767 ret
= request_irq(priv
->link_irq
, &nuport_mac_link_interrupt
,
770 netdev_err(dev
, "unable to request link interrupt\n");
774 ret
= request_irq(priv
->tx_irq
, &nuport_mac_tx_interrupt
,
777 netdev_err(dev
, "unable to request rx interrupt\n");
781 /* Enable link interrupt monitoring for our PHY address */
782 reg
= LINK_INT_EN
| (priv
->phydev
->addr
<< LINK_PHY_ADDR_SHIFT
);
783 /* MII_BMSR register to be watched */
784 reg
|= (1 << LINK_PHY_REG_SHIFT
);
785 /* BMSR_STATUS to be watched in particular */
786 reg
|= (2 << LINK_BIT_UP_SHIFT
);
788 spin_lock_irqsave(&priv
->lock
, flags
);
789 nuport_mac_writel(reg
, LINK_INT_CSR
);
790 nuport_mac_writel(LINK_POLL_MASK
, LINK_INT_POLL_TIME
);
791 spin_unlock_irqrestore(&priv
->lock
, flags
);
793 phy_start(priv
->phydev
);
795 ret
= request_irq(priv
->rx_irq
, &nuport_mac_rx_interrupt
,
798 netdev_err(dev
, "unable to request tx interrupt\n");
802 netif_start_queue(dev
);
804 nuport_mac_init_tx_ring(priv
);
806 ret
= nuport_mac_init_rx_ring(dev
);
808 netdev_err(dev
, "rx ring init failed\n");
812 nuport_mac_reset_tx_dma(priv
);
813 nuport_mac_reset_rx_dma(priv
);
816 spin_lock_irqsave(&priv
->lock
, flags
);
817 ret
= nuport_mac_start_rx_dma(priv
, priv
->rx_skb
[0]);
818 spin_unlock_irqrestore(&priv
->lock
, flags
);
820 napi_enable(&priv
->napi
);
825 nuport_mac_free_rx_ring(priv
);
826 free_irq(priv
->rx_irq
, dev
);
828 free_irq(priv
->tx_irq
, dev
);
830 free_irq(priv
->link_irq
, dev
);
832 clk_disable(priv
->emac_clk
);
836 static int nuport_mac_close(struct net_device
*dev
)
839 struct nuport_mac_priv
*priv
= netdev_priv(dev
);
841 spin_lock_irq(&priv
->lock
);
842 reg
= nuport_mac_readl(CTRL_REG
);
843 reg
&= ~(RX_ENABLE
| TX_ENABLE
);
844 nuport_mac_writel(reg
, CTRL_REG
);
846 napi_disable(&priv
->napi
);
847 netif_stop_queue(dev
);
849 free_irq(priv
->link_irq
, dev
);
850 /* disable PHY polling */
851 nuport_mac_writel(0, LINK_INT_CSR
);
852 nuport_mac_writel(0, LINK_INT_POLL_TIME
);
853 phy_stop(priv
->phydev
);
855 free_irq(priv
->tx_irq
, dev
);
856 free_irq(priv
->rx_irq
, dev
);
857 spin_unlock_irq(&priv
->lock
);
859 nuport_mac_free_rx_ring(priv
);
861 clk_disable(priv
->emac_clk
);
866 static void nuport_mac_tx_timeout(struct net_device
*dev
)
868 struct nuport_mac_priv
*priv
= netdev_priv(dev
);
871 netdev_warn(dev
, "transmit timeout, attempting recovery\n");
873 netdev_info(dev
, "TX DMA regs\n");
874 for (i
= 0; i
< DMA_CHAN_WIDTH
; i
+= 4)
875 netdev_info(dev
, "[%02x]: 0x%08x\n", i
, nuport_mac_readl(TX_DMA_BASE
+ i
));
876 netdev_info(dev
, "RX DMA regs\n");
877 for (i
= 0; i
< DMA_CHAN_WIDTH
; i
+= 4)
878 netdev_info(dev
, "[%02x]: 0x%08x\n", i
, nuport_mac_readl(RX_DMA_BASE
+ i
));
880 nuport_mac_init_tx_ring(priv
);
881 nuport_mac_reset_tx_dma(priv
);
883 netif_wake_queue(dev
);
886 static int nuport_mac_mii_probe(struct net_device
*dev
)
888 struct nuport_mac_priv
*priv
= netdev_priv(dev
);
889 struct phy_device
*phydev
= NULL
;
892 ret
= clk_enable(priv
->ephy_clk
);
894 netdev_err(dev
, "unable to enable ePHY clk\n");
898 phydev
= phy_find_first(priv
->mii_bus
);
900 netdev_err(dev
, "no PHYs found\n");
905 phydev
= of_phy_connect(dev
, priv
->phy_node
,
906 nuport_mac_adjust_link
, 0,
907 priv
->phy_interface
);
908 if (IS_ERR(phydev
)) {
909 netdev_err(dev
, "could not attach PHY\n");
910 ret
= PTR_ERR(phydev
);
914 phydev
->supported
&= PHY_BASIC_FEATURES
;
915 phydev
->advertising
= phydev
->supported
;
916 priv
->phydev
= phydev
;
918 priv
->old_duplex
= DUPLEX_FULL
;
920 dev_info(&priv
->pdev
->dev
, "attached PHY driver [%s] "
921 "(mii_bus:phy_addr=%d)\n",
922 phydev
->drv
->name
, phydev
->addr
);
927 /* disable the Ethernet PHY clock for the moment */
928 clk_disable(priv
->ephy_clk
);
933 static void nuport_mac_ethtool_drvinfo(struct net_device
*dev
,
934 struct ethtool_drvinfo
*info
)
936 strncpy(info
->driver
, "nuport-mac", sizeof(info
->driver
));
937 strncpy(info
->version
, "0.1", sizeof(info
->version
));
938 strncpy(info
->fw_version
, "N/A", sizeof(info
->fw_version
));
939 strncpy(info
->bus_info
, "internal", sizeof(info
->bus_info
));
941 info
->testinfo_len
= 0;
942 info
->regdump_len
= 0;
943 info
->eedump_len
= 0;
946 static int nuport_mac_ethtool_get_settings(struct net_device
*dev
,
947 struct ethtool_cmd
*cmd
)
949 struct nuport_mac_priv
*priv
= netdev_priv(dev
);
952 return phy_ethtool_gset(priv
->phydev
, cmd
);
957 static int nuport_mac_ethtool_set_settings(struct net_device
*dev
,
958 struct ethtool_cmd
*cmd
)
960 struct nuport_mac_priv
*priv
= netdev_priv(dev
);
963 return phy_ethtool_sset(priv
->phydev
, cmd
);
968 static void nuport_mac_set_msglevel(struct net_device
*dev
, u32 msg_level
)
970 struct nuport_mac_priv
*priv
= netdev_priv(dev
);
972 priv
->msg_level
= msg_level
;
975 static u32
nuport_mac_get_msglevel(struct net_device
*dev
)
977 struct nuport_mac_priv
*priv
= netdev_priv(dev
);
979 return priv
->msg_level
;
982 static const struct ethtool_ops nuport_mac_ethtool_ops
= {
983 .get_drvinfo
= nuport_mac_ethtool_drvinfo
,
984 .get_link
= ethtool_op_get_link
,
985 .get_settings
= nuport_mac_ethtool_get_settings
,
986 .set_settings
= nuport_mac_ethtool_set_settings
,
987 .set_msglevel
= nuport_mac_set_msglevel
,
988 .get_msglevel
= nuport_mac_get_msglevel
,
991 static const struct net_device_ops nuport_mac_ops
= {
992 .ndo_open
= nuport_mac_open
,
993 .ndo_stop
= nuport_mac_close
,
994 .ndo_start_xmit
= nuport_mac_start_xmit
,
995 .ndo_change_mtu
= eth_change_mtu
,
996 .ndo_validate_addr
= eth_validate_addr
,
997 .ndo_set_mac_address
= nuport_mac_change_mac_address
,
998 .ndo_tx_timeout
= nuport_mac_tx_timeout
,
1001 static int __init
nuport_mac_probe(struct platform_device
*pdev
)
1003 struct net_device
*dev
;
1004 struct nuport_mac_priv
*priv
= NULL
;
1005 struct resource
*regs
, *dma
;
1007 int rx_irq
, tx_irq
, link_irq
;
1009 const unsigned int *intspec
;
1011 dev
= alloc_etherdev(sizeof(struct nuport_mac_priv
));
1013 dev_err(&pdev
->dev
, "no memory for net_device\n");
1017 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1018 dma
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
1019 if (!regs
|| !dma
) {
1020 dev_err(&pdev
->dev
, "failed to get regs resources\n");
1025 rx_irq
= platform_get_irq(pdev
, 0);
1026 tx_irq
= platform_get_irq(pdev
, 1);
1027 link_irq
= platform_get_irq(pdev
, 2);
1028 if (rx_irq
< 0 || tx_irq
< 0 || link_irq
< 0) {
1033 platform_set_drvdata(pdev
, dev
);
1034 SET_NETDEV_DEV(dev
, &pdev
->dev
);
1035 priv
= netdev_priv(dev
);
1038 spin_lock_init(&priv
->lock
);
1040 intspec
= of_get_property(pdev
->dev
.of_node
,
1041 "nuport-mac,buffer-shifting", NULL
);
1043 priv
->buffer_shifting_len
= 0;
1045 priv
->buffer_shifting_len
= 2;
1047 priv
->mac_base
= devm_request_and_ioremap(&pdev
->dev
, regs
);
1048 if (!priv
->mac_base
) {
1049 dev_err(&pdev
->dev
, "failed to remap regs\n");
1054 priv
->dma_base
= devm_request_and_ioremap(&pdev
->dev
, dma
);
1055 if (!priv
->dma_base
) {
1056 dev_err(&pdev
->dev
, "failed to remap dma-regs\n");
1061 priv
->emac_clk
= clk_get(&pdev
->dev
, "emac");
1062 if (IS_ERR_OR_NULL(priv
->emac_clk
)) {
1063 dev_err(&pdev
->dev
, "failed to get emac clk\n");
1064 ret
= PTR_ERR(priv
->emac_clk
);
1068 priv
->ephy_clk
= clk_get(&pdev
->dev
, "ephy");
1069 if (IS_ERR_OR_NULL(priv
->ephy_clk
)) {
1070 dev_err(&pdev
->dev
, "failed to get ephy clk\n");
1071 ret
= PTR_ERR(priv
->ephy_clk
);
1075 priv
->link_irq
= link_irq
;
1076 priv
->rx_irq
= rx_irq
;
1077 priv
->tx_irq
= tx_irq
;
1078 priv
->msg_level
= NETIF_MSG_DRV
| NETIF_MSG_PROBE
| NETIF_MSG_LINK
;
1079 dev
->netdev_ops
= &nuport_mac_ops
;
1080 dev
->ethtool_ops
= &nuport_mac_ethtool_ops
;
1081 dev
->watchdog_timeo
= HZ
;
1082 dev
->flags
= IFF_BROADCAST
; /* Supports Broadcast */
1083 dev
->tx_queue_len
= TX_RING_SIZE
/ 2;
1085 netif_napi_add(dev
, &priv
->napi
, nuport_mac_poll
, 64);
1087 priv
->phy_node
= of_parse_phandle(pdev
->dev
.of_node
, "phy", 0);
1088 if (!priv
->phy_node
) {
1089 dev_err(&pdev
->dev
, "no associated PHY\n");
1094 priv
->phy_interface
= of_get_phy_mode(pdev
->dev
.of_node
);
1095 if (priv
->phy_interface
< 0) {
1096 dev_err(&pdev
->dev
, "invalid PHY mode\n");
1101 priv
->mii_bus
= mdiobus_alloc();
1102 if (!priv
->mii_bus
) {
1103 dev_err(&pdev
->dev
, "mii bus allocation failed\n");
1107 priv
->mii_bus
->priv
= dev
;
1108 priv
->mii_bus
->read
= nuport_mac_mii_read
;
1109 priv
->mii_bus
->write
= nuport_mac_mii_write
;
1110 priv
->mii_bus
->reset
= nuport_mac_mii_reset
;
1111 priv
->mii_bus
->name
= "nuport-mac-mii";
1112 priv
->mii_bus
->phy_mask
= (1 << 0);
1113 snprintf(priv
->mii_bus
->id
, MII_BUS_ID_SIZE
, "%s", pdev
->name
);
1114 priv
->mii_bus
->irq
= kzalloc(PHY_MAX_ADDR
* sizeof(int), GFP_KERNEL
);
1115 if (!priv
->mii_bus
->irq
) {
1116 dev_err(&pdev
->dev
, "failed to allocate mii_bus irqs\n");
1121 /* We support PHY interrupts routed back to the MAC */
1122 for (i
= 0; i
< PHY_MAX_ADDR
; i
++)
1123 priv
->mii_bus
->irq
[i
] = PHY_IGNORE_INTERRUPT
;
1125 ret
= of_mdiobus_register(priv
->mii_bus
, pdev
->dev
.of_node
);
1127 dev_err(&pdev
->dev
, "failed to register mii_bus\n");
1131 ret
= nuport_mac_mii_probe(dev
);
1133 dev_err(&pdev
->dev
, "failed to probe MII bus\n");
1134 goto out_mdio_unregister
;
1137 ret
= register_netdev(dev
);
1139 dev_err(&pdev
->dev
, "failed to register net_device\n");
1140 goto out_mdio_probe
;
1143 /* read existing mac address */
1144 nuport_mac_read_mac_address(dev
);
1146 dev_info(&pdev
->dev
, "registered (MAC: %pM)\n", dev
->dev_addr
);
1151 phy_disconnect(priv
->phydev
);
1152 out_mdio_unregister
:
1153 mdiobus_unregister(priv
->mii_bus
);
1155 kfree(priv
->mii_bus
->irq
);
1157 mdiobus_free(priv
->mii_bus
);
1159 platform_set_drvdata(pdev
, NULL
);
1161 clk_put(priv
->ephy_clk
);
1162 clk_put(priv
->emac_clk
);
1164 platform_set_drvdata(pdev
, NULL
);
1168 static int nuport_mac_remove(struct platform_device
*pdev
)
1170 struct net_device
*dev
= platform_get_drvdata(pdev
);
1171 struct nuport_mac_priv
*priv
= netdev_priv(dev
);
1173 unregister_netdev(dev
);
1174 phy_disconnect(priv
->phydev
);
1175 mdiobus_unregister(priv
->mii_bus
);
1176 kfree(priv
->mii_bus
->irq
);
1177 mdiobus_free(priv
->mii_bus
);
1178 clk_put(priv
->ephy_clk
);
1179 clk_put(priv
->emac_clk
);
1182 platform_set_drvdata(pdev
, NULL
);
1187 static struct of_device_id nuport_eth_ids
[] __initdata
= {
1188 {.compatible
= "moschip,nuport-mac",},
1192 static struct platform_driver nuport_eth_driver
= {
1194 .name
= "nuport-mac",
1195 .owner
= THIS_MODULE
,
1196 .of_match_table
= nuport_eth_ids
,
1198 .probe
= nuport_mac_probe
,
1199 .remove
= nuport_mac_remove
,
1202 module_platform_driver(nuport_eth_driver
);
1204 MODULE_AUTHOR("Moschip Semiconductors Ltd.");
1205 MODULE_DESCRIPTION("Moschip MCS8140 Ethernet MAC driver");
1206 MODULE_LICENSE("GPL");