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/irq.h>
23 #include <linux/err.h>
24 #include <linux/phy.h>
25 #include <linux/clk.h>
26 #include <linux/dma-mapping.h>
28 #include <asm/unaligned.h>
29 #include <asm/sizes.h>
30 #include <mach/hardware.h>
32 /* Hardware registers */
33 #define MAC_BASE_ADDR ((priv->mac_base))
35 #define CTRL_REG (MAC_BASE_ADDR)
36 #define MII_BUSY 0x00000001
37 #define MII_WRITE 0x00000002
38 #define MAC_ADDR_HIGH_REG (MAC_BASE_ADDR + 0x04)
39 #define MAC_ADDR_LOW_REG (MAC_BASE_ADDR + 0x08)
40 #define MII_ADDR_REG (MAC_BASE_ADDR + 0x14)
41 #define MII_DATA_REG (MAC_BASE_ADDR + 0x18)
42 /* Link interrupt registers */
43 #define LINK_INT_CSR (MAC_BASE_ADDR + 0xD0)
44 #define LINK_INT_POLL_TIME (MAC_BASE_ADDR + 0xD4)
46 #define DMA_CHAN_WIDTH 32
50 /* Receive DMA registers */
51 #define RX_DMA_BASE ((priv->dma_base) + \
52 (DMA_CHAN_WIDTH * DMA_RX_CHAN))
53 #define RX_BUFFER_ADDR (RX_DMA_BASE + 0x00)
54 #define RX_MAX_BYTES (RX_DMA_BASE + 0x04)
55 #define RX_ACT_BYTES (RX_DMA_BASE + 0x08)
56 #define RX_START_DMA (RX_DMA_BASE + 0x0C)
57 #define RX_DMA_ENH (RX_DMA_BASE + 0x14)
59 /* Transmit DMA registers */
60 #define TX_DMA_BASE ((priv->dma_base) + \
61 (DMA_CHAN_WIDTH * DMA_TX_CHAN))
62 #define TX_BUFFER_ADDR (TX_DMA_BASE + 0x00)
63 #define TX_PKT_BYTES (TX_DMA_BASE + 0x04)
64 #define TX_BYTES_SENT (TX_DMA_BASE + 0x08)
65 #define TX_START_DMA (TX_DMA_BASE + 0x0C)
66 #define TX_DMA_STATUS (TX_DMA_BASE + 0x10)
67 #define TX_DMA_ENH (TX_DMA_BASE + 0x14)
69 #define RX_ALLOC_SIZE SZ_2K
70 #define MAX_ETH_FRAME_SIZE 1536
71 #define RX_SKB_TAILROOM 128
72 #define RX_SKB_HEADROOM (RX_ALLOC_SIZE - \
73 (MAX_ETH_FRAME_SIZE + RX_SKB_TAILROOM) + 0)
75 /* WDT Late COL Lenght COL Type */
76 #define ERROR_FILTER_MASK ((1<<14) | (1<<15) | (1<<16) | (1<<17) | (0<<18) | \
77 /* MII Dribbling CRC Len/type Control */\
78 (1<<19) | (1<<20) | (1<<21) | (0<<24) | (1<<25) | \
81 #define TX_RING_SIZE 30
82 #define RX_RING_SIZE 30
84 static inline u32
nuport_mac_readl(void __iomem
*reg
)
86 return __raw_readl(reg
);
89 static inline u8
nuport_mac_readb(void __iomem
*reg
)
91 return __raw_readb(reg
);
94 static inline void nuport_mac_writel(u32 value
, void __iomem
*reg
)
96 __raw_writel(value
, reg
);
99 static inline void nuport_mac_writeb(u8 value
, void __iomem
*reg
)
101 __raw_writel(value
, reg
);
104 /* MAC private data */
105 struct nuport_mac_priv
{
108 void __iomem
*mac_base
;
109 void __iomem
*dma_base
;
114 struct clk
*emac_clk
;
115 struct clk
*ephy_clk
;
117 /* Transmit buffers */
118 struct sk_buff
*tx_skb
[TX_RING_SIZE
];
119 unsigned int valid_txskb
[TX_RING_SIZE
];
122 unsigned int tx_full
;
124 /* Receive buffers */
125 struct sk_buff
*rx_skb
[RX_RING_SIZE
];
126 unsigned int irq_rxskb
[RX_RING_SIZE
];
127 int pkt_len
[RX_RING_SIZE
];
130 unsigned int rx_full
;
132 unsigned int first_pkt
;
135 struct napi_struct napi
;
136 struct net_device
*dev
;
137 struct platform_device
*pdev
;
138 struct mii_bus
*mii_bus
;
139 struct phy_device
*phydev
;
145 void dcache_invalidate_only(unsigned long start
, unsigned long end
)
149 "1: mcr p15, 0, r0, c7, c6, 1\n"
151 " cmp r0, r1\n" " blo 1b\n");
154 void dcache_clean_range(unsigned long start
, unsigned long end
)
158 "1: mcr p15, 0, r0, c7, c10, 1 @ clean D entry\n"
162 " mcr p15, 0, r0, c7, c10, 4 @ drain WB\n");
165 static inline int nuport_mac_mii_busy_wait(struct nuport_mac_priv
*priv
)
168 unsigned long finish
= jiffies
+ 3 * HZ
;
172 if (!(nuport_mac_readl(MII_ADDR_REG
) & MII_BUSY
))
175 } while (!time_after_eq(curr
, finish
));
180 /* Read from PHY registers */
181 static int nuport_mac_mii_read(struct mii_bus
*bus
,
182 int mii_id
, int regnum
)
184 struct net_device
*dev
= bus
->priv
;
185 struct nuport_mac_priv
*priv
= netdev_priv(dev
);
189 ret
= nuport_mac_mii_busy_wait(priv
);
193 val
|= (mii_id
<< 11) | (regnum
<< 6) | MII_BUSY
;
194 nuport_mac_writel(val
, MII_ADDR_REG
);
195 ret
= nuport_mac_mii_busy_wait(priv
);
199 return nuport_mac_readl(MII_DATA_REG
);
202 static int nuport_mac_mii_write(struct mii_bus
*bus
, int mii_id
,
203 int regnum
, u16 value
)
205 struct net_device
*dev
= bus
->priv
;
206 struct nuport_mac_priv
*priv
= netdev_priv(dev
);
210 ret
= nuport_mac_mii_busy_wait(priv
);
214 val
|= (mii_id
<< 11) | (regnum
<< 6) | MII_BUSY
| MII_WRITE
;
215 nuport_mac_writel(value
, MII_DATA_REG
);
216 nuport_mac_writel(val
, MII_ADDR_REG
);
218 return nuport_mac_mii_busy_wait(priv
);
221 static int nuport_mac_mii_reset(struct mii_bus
*bus
)
226 static int nuport_mac_start_tx_dma(struct nuport_mac_priv
*priv
,
231 unsigned int timeout
= 2048;
234 reg
= nuport_mac_readl(TX_START_DMA
);
236 netdev_dbg(priv
->dev
, "dma ready\n");
245 p
= dma_map_single(&priv
->pdev
->dev
, skb
->data
,
246 skb
->len
, DMA_TO_DEVICE
);
248 /* enable enhanced mode */
249 nuport_mac_writel(0x01, TX_DMA_ENH
);
250 nuport_mac_writel(p
, TX_BUFFER_ADDR
);
251 nuport_mac_writel((skb
->len
) - 1, TX_PKT_BYTES
);
253 nuport_mac_writel(0x0D, TX_START_DMA
);
258 static void nuport_mac_reset_tx_dma(struct nuport_mac_priv
*priv
)
262 reg
= nuport_mac_readl(TX_START_DMA
);
264 nuport_mac_writel(reg
, TX_START_DMA
);
267 static int nuport_mac_start_rx_dma(struct nuport_mac_priv
*priv
,
272 unsigned int timeout
= 2048;
275 reg
= nuport_mac_readl(RX_START_DMA
);
277 netdev_dbg(priv
->dev
, "dma ready\n");
286 p
= dma_map_single(&priv
->pdev
->dev
, skb
->data
,
287 RX_ALLOC_SIZE
, DMA_FROM_DEVICE
);
289 nuport_mac_writel(p
, RX_BUFFER_ADDR
);
291 nuport_mac_writel(0x01, RX_START_DMA
);
296 static void nuport_mac_reset_rx_dma(struct nuport_mac_priv
*priv
)
300 reg
= nuport_mac_readl(RX_START_DMA
);
302 nuport_mac_writel(reg
, RX_START_DMA
);
305 /* I suppose this might do something, but I am not sure actually */
306 static void nuport_mac_disable_rx_dma(struct nuport_mac_priv
*priv
)
310 reg
= nuport_mac_readl(RX_DMA_ENH
);
312 nuport_mac_writel(reg
, RX_DMA_ENH
);
315 static void nuport_mac_enable_rx_dma(struct nuport_mac_priv
*priv
)
319 reg
= nuport_mac_readl(RX_DMA_ENH
);
321 nuport_mac_writel(reg
, RX_DMA_ENH
);
324 /* Add packets to the transmit queue */
325 static int nuport_mac_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
328 struct nuport_mac_priv
*priv
= netdev_priv(dev
);
331 if (netif_queue_stopped(dev
)) {
332 netdev_warn(dev
, "netif queue was stopped, restarting\n");
333 netif_start_queue(dev
);
336 spin_lock_irqsave(&priv
->lock
, flags
);
337 if (priv
->first_pkt
) {
338 ret
= nuport_mac_start_tx_dma(priv
, skb
);
340 netif_stop_queue(dev
);
341 spin_unlock_irqrestore(&priv
->lock
, flags
);
342 netdev_err(dev
, "transmit path busy\n");
343 return NETDEV_TX_BUSY
;
348 priv
->tx_skb
[priv
->cur_tx
] = skb
;
349 dev
->stats
.tx_bytes
+= skb
->len
;
350 dev
->stats
.tx_packets
++;
351 priv
->valid_txskb
[priv
->cur_tx
] = 1;
353 dev
->trans_start
= jiffies
;
355 if (priv
->cur_tx
>= TX_RING_SIZE
)
358 spin_unlock_irqrestore(&priv
->lock
, flags
);
360 if (priv
->valid_txskb
[priv
->cur_tx
]) {
362 netdev_err(dev
, "stopping queue\n");
363 netif_stop_queue(dev
);
369 static void nuport_mac_adjust_link(struct net_device
*dev
)
371 struct nuport_mac_priv
*priv
= netdev_priv(dev
);
372 struct phy_device
*phydev
= priv
->phydev
;
373 unsigned int status_changed
= 0;
378 if (priv
->old_link
!= phydev
->link
) {
380 priv
->old_link
= phydev
->link
;
383 if (phydev
->link
& (priv
->old_duplex
!= phydev
->duplex
)) {
384 reg
= nuport_mac_readl(CTRL_REG
);
385 if (phydev
->duplex
== DUPLEX_FULL
)
389 nuport_mac_writel(reg
, CTRL_REG
);
392 priv
->old_duplex
= phydev
->duplex
;
398 pr_info("%s: link %s", dev
->name
, phydev
->link
?
401 pr_cont(" - %d/%s", phydev
->speed
,
402 phydev
->duplex
== DUPLEX_FULL
? "full" : "half");
407 static irqreturn_t
nuport_mac_link_interrupt(int irq
, void *dev_id
)
409 struct net_device
*dev
= dev_id
;
410 struct nuport_mac_priv
*priv
= netdev_priv(dev
);
414 reg
= nuport_mac_readl(LINK_INT_CSR
);
415 phy_addr
= (reg
>> 1) & 0x0f;
417 if (phy_addr
!= priv
->phydev
->addr
) {
418 netdev_err(dev
, "spurious PHY irq (phy: %d)\n", phy_addr
);
422 priv
->phydev
->link
= (reg
& (1 << 16));
423 nuport_mac_adjust_link(dev
);
428 static irqreturn_t
nuport_mac_tx_interrupt(int irq
, void *dev_id
)
430 struct net_device
*dev
= (struct net_device
*)dev_id
;
431 struct nuport_mac_priv
*priv
= netdev_priv(dev
);
437 spin_lock_irqsave(&priv
->lock
, flags
);
438 /* clear status word available if ready */
439 reg
= nuport_mac_readl(TX_START_DMA
);
440 if (reg
& (1 << 18)) {
441 nuport_mac_writel(reg
, TX_START_DMA
);
442 reg
= nuport_mac_readl(TX_DMA_STATUS
);
445 dev
->stats
.tx_errors
++;
447 netdev_dbg(dev
, "no status word: %08x\n", reg
);
449 skb
= priv
->tx_skb
[priv
->dma_tx
];
450 priv
->tx_skb
[priv
->dma_tx
] = NULL
;
451 priv
->valid_txskb
[priv
->dma_tx
] = 0;
452 dev_kfree_skb_irq(skb
);
455 if (priv
->dma_tx
>= TX_RING_SIZE
)
458 if (!priv
->valid_txskb
[priv
->dma_tx
])
461 ret
= nuport_mac_start_tx_dma(priv
, priv
->tx_skb
[priv
->dma_tx
]);
463 netdev_err(dev
, "failed to restart TX dma\n");
467 netdev_dbg(dev
, "restarting transmit queue\n");
468 netif_wake_queue(dev
);
472 spin_unlock_irqrestore(&priv
->lock
, flags
);
477 static unsigned int nuport_mac_has_work(struct nuport_mac_priv
*priv
)
481 for (i
= 0; i
< RX_RING_SIZE
; i
++)
488 static irqreturn_t
nuport_mac_rx_interrupt(int irq
, void *dev_id
)
490 struct net_device
*dev
= (struct net_device
*)dev_id
;
491 struct nuport_mac_priv
*priv
= netdev_priv(dev
);
495 spin_lock_irqsave(&priv
->lock
, flags
);
496 if (!priv
->rx_full
) {
497 priv
->pkt_len
[priv
->dma_rx
] = nuport_mac_readl(RX_ACT_BYTES
) - 4;
498 priv
->irq_rxskb
[priv
->dma_rx
] = 0;
501 if (priv
->dma_rx
>= RX_RING_SIZE
)
506 if (priv
->irq_rxskb
[priv
->dma_rx
] == 1) {
507 ret
= nuport_mac_start_rx_dma(priv
, priv
->rx_skb
[priv
->dma_rx
]);
509 netdev_err(dev
, "failed to start rx dma\n");
512 netdev_dbg(dev
, "RX ring full\n");
515 if (likely(nuport_mac_has_work(priv
))) {
516 /* find a way to disable DMA rx irq */
517 nuport_mac_disable_rx_dma(priv
);
518 napi_schedule(&priv
->napi
);
520 spin_unlock_irqrestore(&priv
->lock
, flags
);
525 /* Process received packets in tasklet */
526 static int nuport_mac_rx(struct net_device
*dev
, int limit
)
528 struct nuport_mac_priv
*priv
= netdev_priv(dev
);
533 while (count
< limit
&& !priv
->irq_rxskb
[priv
->cur_rx
]) {
534 skb
= priv
->rx_skb
[priv
->cur_rx
];
535 len
= priv
->pkt_len
[priv
->cur_rx
];
536 dcache_invalidate_only(((u32
) skb
->data
),
537 ((u32
) (skb
->data
+ len
+ 4)));
539 /* Remove 2 bytes added by RX buffer shifting */
541 skb
->data
= skb
->data
+ 2;
543 /* Get packet status */
544 status
= get_unaligned((u32
*) (skb
->data
+ len
));
547 /* packet filter failed */
548 if (!(status
& (1 << 30))) {
549 dev_kfree_skb_irq(skb
);
554 if (status
& (1 << 31)) {
555 dev
->stats
.rx_missed_errors
++;
556 dev_kfree_skb_irq(skb
);
560 /* Not ethernet type */
561 if ((!(status
& (1 << 18))) || (status
& ERROR_FILTER_MASK
))
562 dev
->stats
.rx_errors
++;
564 if (len
> MAX_ETH_FRAME_SIZE
) {
565 dev_kfree_skb_irq(skb
);
570 skb
->protocol
= eth_type_trans(skb
, dev
);
571 dev
->stats
.rx_packets
++;
573 if (status
& (1 << 29))
574 skb
->pkt_type
= PACKET_OTHERHOST
;
575 if (status
& (1 << 27))
576 skb
->pkt_type
= PACKET_MULTICAST
;
577 if (status
& (1 << 28))
578 skb
->pkt_type
= PACKET_BROADCAST
;
580 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
582 /* Pass the received packet to network layer */
583 netif_receive_skb(skb
);
585 if (status
!= NET_RX_DROP
)
586 dev
->stats
.rx_bytes
+= len
- 4; /* Without CRC */
588 dev
->stats
.rx_dropped
++;
590 dev
->last_rx
= jiffies
;
593 skb
= netdev_alloc_skb(dev
, RX_ALLOC_SIZE
);
594 skb_reserve(skb
, RX_SKB_HEADROOM
);
595 priv
->rx_skb
[priv
->cur_rx
] = skb
;
596 priv
->irq_rxskb
[priv
->cur_rx
] = 1;
599 if (priv
->cur_rx
>= RX_RING_SIZE
)
607 static int nuport_mac_poll(struct napi_struct
*napi
, int budget
)
609 struct nuport_mac_priv
*priv
=
610 container_of(napi
, struct nuport_mac_priv
, napi
);
611 struct net_device
*dev
= priv
->dev
;
614 work_done
= nuport_mac_rx(dev
, budget
);
616 if (work_done
< budget
) {
618 nuport_mac_enable_rx_dma(priv
);
624 static void nuport_mac_init_tx_ring(struct nuport_mac_priv
*priv
)
628 priv
->cur_tx
= priv
->dma_tx
= priv
->tx_full
= 0;
629 for (i
= 0; i
< TX_RING_SIZE
; i
++) {
630 priv
->tx_skb
[i
] = NULL
;
631 priv
->valid_txskb
[i
] = 0;
636 static int nuport_mac_init_rx_ring(struct net_device
*dev
)
638 struct nuport_mac_priv
*priv
= netdev_priv(dev
);
642 priv
->cur_rx
= priv
->dma_rx
= priv
->rx_full
= 0;
644 for (i
= 0; i
< RX_RING_SIZE
; i
++) {
645 skb
= netdev_alloc_skb(dev
, RX_ALLOC_SIZE
);
648 skb_reserve(skb
, RX_SKB_HEADROOM
);
649 priv
->rx_skb
[i
] = skb
;
650 priv
->irq_rxskb
[i
] = 1;
656 static void nuport_mac_free_rx_ring(struct nuport_mac_priv
*priv
)
660 for (i
= 0; i
< RX_RING_SIZE
; i
++) {
661 if (!priv
->rx_skb
[i
])
664 dev_kfree_skb(priv
->rx_skb
[i
]);
665 priv
->rx_skb
[i
] = NULL
;
669 static void nuport_mac_read_mac_address(struct net_device
*dev
)
671 struct nuport_mac_priv
*priv
= netdev_priv(dev
);
674 for (i
= 0; i
< 4; i
++)
675 dev
->dev_addr
[i
] = nuport_mac_readb(MAC_ADDR_LOW_REG
+ i
);
676 dev
->dev_addr
[4] = nuport_mac_readb(MAC_ADDR_HIGH_REG
);
677 dev
->dev_addr
[5] = nuport_mac_readb(MAC_ADDR_HIGH_REG
+ 1);
679 if (!is_valid_ether_addr(dev
->dev_addr
)) {
680 dev_info(&priv
->pdev
->dev
, "using random address\n");
681 random_ether_addr(dev
->dev_addr
);
685 static int nuport_mac_change_mac_address(struct net_device
*dev
, void *mac_addr
)
687 struct sockaddr
*addr
= mac_addr
;
688 struct nuport_mac_priv
*priv
= netdev_priv(dev
);
689 unsigned long *temp
= (unsigned long *)dev
->dev_addr
;
692 if (netif_running(dev
))
695 memcpy(dev
->dev_addr
, addr
->sa_data
, ETH_ALEN
);
697 spin_lock_irq(&priv
->lock
);
699 nuport_mac_writel(*temp
, MAC_ADDR_LOW_REG
);
700 temp
= (unsigned long *)(dev
->dev_addr
+ 4);
701 nuport_mac_writel(*temp
, MAC_ADDR_HIGH_REG
);
703 low
= nuport_mac_readl(MAC_ADDR_LOW_REG
);
704 high
= nuport_mac_readl(MAC_ADDR_HIGH_REG
);
706 spin_unlock_irq(&priv
->lock
);
711 static int nuport_mac_open(struct net_device
*dev
)
714 struct nuport_mac_priv
*priv
= netdev_priv(dev
);
719 /* Enable hardware filters */
720 reg
= nuport_mac_readl((void __iomem
*)_CONFADDR_DBGLED
);
722 nuport_mac_writel(reg
, (void __iomem
*)_CONFADDR_DBGLED
);
724 /* Set LEDs to Link act and RX/TX mode */
725 reg
= nuport_mac_readl((void __iomem
*)(_CONFADDR_SYSDBG
+ 0x04));
727 nuport_mac_writel(reg
, (void __iomem
*)(_CONFADDR_SYSDBG
+ 0x04));
729 ret
= clk_enable(priv
->emac_clk
);
731 netdev_err(dev
, "failed to enable EMAC clock\n");
735 /* Set MAC into full duplex mode by default */
736 nuport_mac_writel(0x1010052C, CTRL_REG
);
738 /* set mac address in hardware in case it was not already */
739 nuport_mac_change_mac_address(dev
, dev
->dev_addr
);
741 ret
= request_irq(priv
->link_irq
, &nuport_mac_link_interrupt
,
744 netdev_err(dev
, "unable to request link interrupt\n");
748 phy_start(priv
->phydev
);
750 /* Enable link interrupt monitoring */
751 spin_lock_irqsave(&priv
->lock
, flags
);
752 nuport_mac_writel(0x1041 | (priv
->phydev
->addr
<< 1), LINK_INT_CSR
);
753 nuport_mac_writel(0xFFFFF, LINK_INT_POLL_TIME
);
754 spin_unlock_irqrestore(&priv
->lock
, flags
);
756 ret
= request_irq(priv
->tx_irq
, &nuport_mac_tx_interrupt
,
759 netdev_err(dev
, "unable to request rx interrupt\n");
763 napi_enable(&priv
->napi
);
765 ret
= request_irq(priv
->rx_irq
, &nuport_mac_rx_interrupt
,
768 netdev_err(dev
, "unable to request tx interrupt\n");
772 /* Enable buffer shifting in RX */
773 tmp
= nuport_mac_readb((void __iomem
*)(_CONFADDR_SYSDBG
+ 0x1D));
775 nuport_mac_writeb(tmp
, (void __iomem
*)(_CONFADDR_SYSDBG
+ 0x1D));
777 netif_start_queue(dev
);
779 nuport_mac_init_tx_ring(priv
);
781 ret
= nuport_mac_init_rx_ring(dev
);
783 netdev_err(dev
, "rx ring init failed\n");
787 nuport_mac_reset_tx_dma(priv
);
788 nuport_mac_reset_rx_dma(priv
);
791 return nuport_mac_start_rx_dma(priv
, priv
->rx_skb
[0]);
794 nuport_mac_free_rx_ring(priv
);
795 free_irq(priv
->rx_irq
, dev
);
797 free_irq(priv
->tx_irq
, dev
);
799 free_irq(priv
->link_irq
, dev
);
801 clk_disable(priv
->emac_clk
);
805 static int nuport_mac_close(struct net_device
*dev
)
807 struct nuport_mac_priv
*priv
= netdev_priv(dev
);
809 spin_lock_irq(&priv
->lock
);
810 napi_disable(&priv
->napi
);
811 netif_stop_queue(dev
);
813 free_irq(priv
->link_irq
, dev
);
814 nuport_mac_writel(0x00, LINK_INT_CSR
);
815 nuport_mac_writel(0x00, LINK_INT_POLL_TIME
);
816 phy_stop(priv
->phydev
);
818 free_irq(priv
->tx_irq
, dev
);
819 free_irq(priv
->rx_irq
, dev
);
820 spin_unlock_irq(&priv
->lock
);
822 nuport_mac_free_rx_ring(priv
);
824 clk_disable(priv
->emac_clk
);
829 static void nuport_mac_tx_timeout(struct net_device
*dev
)
831 struct nuport_mac_priv
*priv
= netdev_priv(dev
);
834 netdev_warn(dev
, "transmit timeout, attempting recovery\n");
836 netdev_info(dev
, "TX DMA regs\n");
837 for (i
= 0; i
< DMA_CHAN_WIDTH
; i
+= 4)
838 netdev_info(dev
, "[%02x]: 0x%08x\n", i
, nuport_mac_readl(TX_DMA_BASE
+ i
));
839 netdev_info(dev
, "RX DMA regs\n");
840 for (i
= 0; i
< DMA_CHAN_WIDTH
; i
+= 4)
841 netdev_info(dev
, "[%02x]: 0x%08x\n", i
, nuport_mac_readl(RX_DMA_BASE
+ i
));
843 nuport_mac_init_tx_ring(priv
);
844 nuport_mac_reset_tx_dma(priv
);
846 netif_wake_queue(dev
);
849 static int nuport_mac_mii_probe(struct net_device
*dev
)
851 struct nuport_mac_priv
*priv
= netdev_priv(dev
);
852 struct phy_device
*phydev
= NULL
;
855 ret
= clk_enable(priv
->ephy_clk
);
857 netdev_err(dev
, "unable to enable ePHY clk\n");
861 phydev
= phy_find_first(priv
->mii_bus
);
863 netdev_err(dev
, "no PHYs found\n");
868 phydev
= phy_connect(dev
, dev_name(&phydev
->dev
),
869 nuport_mac_adjust_link
, 0,
870 PHY_INTERFACE_MODE_MII
);
871 if (IS_ERR(phydev
)) {
872 netdev_err(dev
, "could not attach PHY\n");
873 ret
= PTR_ERR(phydev
);
877 phydev
->supported
&= PHY_BASIC_FEATURES
;
878 phydev
->advertising
= phydev
->supported
;
879 priv
->phydev
= phydev
;
881 priv
->old_duplex
= -1;
883 dev_info(&priv
->pdev
->dev
, "attached PHY driver [%s] "
884 "(mii_bus:phy_addr=%d)\n",
885 phydev
->drv
->name
, phydev
->addr
);
890 /* disable the Ethernet PHY clock for the moment */
891 clk_disable(priv
->ephy_clk
);
896 static void nuport_mac_ethtool_drvinfo(struct net_device
*dev
,
897 struct ethtool_drvinfo
*info
)
899 strncpy(info
->driver
, "nuport-mac", sizeof(info
->driver
));
900 strncpy(info
->version
, "0.1", sizeof(info
->version
));
901 strncpy(info
->fw_version
, "N/A", sizeof(info
->fw_version
));
902 strncpy(info
->bus_info
, "internal", sizeof(info
->bus_info
));
904 info
->testinfo_len
= 0;
905 info
->regdump_len
= 0;
906 info
->eedump_len
= 0;
909 static int nuport_mac_ethtool_get_settings(struct net_device
*dev
,
910 struct ethtool_cmd
*cmd
)
912 struct nuport_mac_priv
*priv
= netdev_priv(dev
);
915 return phy_ethtool_gset(priv
->phydev
, cmd
);
920 static int nuport_mac_ethtool_set_settings(struct net_device
*dev
,
921 struct ethtool_cmd
*cmd
)
923 struct nuport_mac_priv
*priv
= netdev_priv(dev
);
926 return phy_ethtool_sset(priv
->phydev
, cmd
);
931 static void nuport_mac_set_msglevel(struct net_device
*dev
, u32 msg_level
)
933 struct nuport_mac_priv
*priv
= netdev_priv(dev
);
935 priv
->msg_level
= msg_level
;
938 static u32
nuport_mac_get_msglevel(struct net_device
*dev
)
940 struct nuport_mac_priv
*priv
= netdev_priv(dev
);
942 return priv
->msg_level
;
945 static const struct ethtool_ops nuport_mac_ethtool_ops
= {
946 .get_drvinfo
= nuport_mac_ethtool_drvinfo
,
947 .get_link
= ethtool_op_get_link
,
948 .get_settings
= nuport_mac_ethtool_get_settings
,
949 .set_settings
= nuport_mac_ethtool_set_settings
,
950 .set_msglevel
= nuport_mac_set_msglevel
,
951 .get_msglevel
= nuport_mac_get_msglevel
,
954 static const struct net_device_ops nuport_mac_ops
= {
955 .ndo_open
= nuport_mac_open
,
956 .ndo_stop
= nuport_mac_close
,
957 .ndo_start_xmit
= nuport_mac_start_xmit
,
958 .ndo_change_mtu
= eth_change_mtu
,
959 .ndo_validate_addr
= eth_validate_addr
,
960 .ndo_set_mac_address
= nuport_mac_change_mac_address
,
961 .ndo_tx_timeout
= nuport_mac_tx_timeout
,
964 static int __init
nuport_mac_probe(struct platform_device
*pdev
)
966 struct net_device
*dev
;
967 struct nuport_mac_priv
*priv
= NULL
;
968 struct resource
*regs
, *dma
;
970 int rx_irq
, tx_irq
, link_irq
;
973 dev
= alloc_etherdev(sizeof(struct nuport_mac_priv
));
975 dev_err(&pdev
->dev
, "no memory for net_device\n");
979 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
980 dma
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
982 dev_err(&pdev
->dev
, "failed to get regs resources\n");
987 rx_irq
= platform_get_irq(pdev
, 0);
988 tx_irq
= platform_get_irq(pdev
, 1);
989 link_irq
= platform_get_irq(pdev
, 2);
990 if (rx_irq
< 0 || tx_irq
< 0 || link_irq
< 0) {
995 platform_set_drvdata(pdev
, dev
);
996 SET_NETDEV_DEV(dev
, &pdev
->dev
);
997 priv
= netdev_priv(dev
);
1000 spin_lock_init(&priv
->lock
);
1002 priv
->mac_base
= devm_ioremap(&pdev
->dev
,
1003 regs
->start
, resource_size(regs
));
1004 if (!priv
->mac_base
) {
1005 dev_err(&pdev
->dev
, "failed to remap regs\n");
1010 priv
->dma_base
= devm_ioremap(&pdev
->dev
,
1011 dma
->start
, resource_size(dma
));
1012 if (!priv
->dma_base
) {
1013 dev_err(&pdev
->dev
, "failed to remap dma-regs\n");
1018 priv
->emac_clk
= clk_get(&pdev
->dev
, "emac");
1019 if (IS_ERR_OR_NULL(priv
->emac_clk
)) {
1020 dev_err(&pdev
->dev
, "failed to get emac clk\n");
1021 ret
= PTR_ERR(priv
->emac_clk
);
1025 priv
->ephy_clk
= clk_get(&pdev
->dev
, "ephy");
1026 if (IS_ERR_OR_NULL(priv
->ephy_clk
)) {
1027 dev_err(&pdev
->dev
, "failed to get ephy clk\n");
1028 ret
= PTR_ERR(priv
->ephy_clk
);
1032 priv
->link_irq
= link_irq
;
1033 priv
->rx_irq
= rx_irq
;
1034 priv
->tx_irq
= tx_irq
;
1035 priv
->msg_level
= NETIF_MSG_DRV
| NETIF_MSG_PROBE
| NETIF_MSG_LINK
;
1036 dev
->netdev_ops
= &nuport_mac_ops
;
1037 dev
->ethtool_ops
= &nuport_mac_ethtool_ops
;
1038 dev
->watchdog_timeo
= HZ
;
1039 dev
->flags
= IFF_BROADCAST
; /* Supports Broadcast */
1040 dev
->tx_queue_len
= TX_RING_SIZE
/ 2;
1042 netif_napi_add(dev
, &priv
->napi
, nuport_mac_poll
, 64);
1044 priv
->mii_bus
= mdiobus_alloc();
1045 if (!priv
->mii_bus
) {
1046 dev_err(&pdev
->dev
, "mii bus allocation failed\n");
1050 priv
->mii_bus
->priv
= dev
;
1051 priv
->mii_bus
->read
= nuport_mac_mii_read
;
1052 priv
->mii_bus
->write
= nuport_mac_mii_write
;
1053 priv
->mii_bus
->reset
= nuport_mac_mii_reset
;
1054 priv
->mii_bus
->name
= "nuport-mac-mii";
1055 priv
->mii_bus
->phy_mask
= (1 << 0);
1056 snprintf(priv
->mii_bus
->id
, MII_BUS_ID_SIZE
, "%s", pdev
->name
);
1057 priv
->mii_bus
->irq
= kzalloc(PHY_MAX_ADDR
* sizeof(int), GFP_KERNEL
);
1058 if (!priv
->mii_bus
->irq
) {
1059 dev_err(&pdev
->dev
, "failed to allocate mii_bus irqs\n");
1064 /* We support PHY interrupts routed back to the MAC */
1065 for (i
= 0; i
< PHY_MAX_ADDR
; i
++)
1066 priv
->mii_bus
->irq
[i
] = PHY_IGNORE_INTERRUPT
;
1068 ret
= mdiobus_register(priv
->mii_bus
);
1070 dev_err(&pdev
->dev
, "failed to register mii_bus\n");
1074 ret
= nuport_mac_mii_probe(dev
);
1076 dev_err(&pdev
->dev
, "failed to probe MII bus\n");
1077 goto out_mdio_unregister
;
1080 ret
= register_netdev(dev
);
1082 dev_err(&pdev
->dev
, "failed to register net_device\n");
1083 goto out_mdio_probe
;
1086 /* read existing mac address */
1087 nuport_mac_read_mac_address(dev
);
1089 dev_info(&pdev
->dev
, "registered (MAC: %pM)\n", dev
->dev_addr
);
1094 phy_disconnect(priv
->phydev
);
1095 out_mdio_unregister
:
1096 mdiobus_unregister(priv
->mii_bus
);
1098 kfree(priv
->mii_bus
->irq
);
1100 mdiobus_free(priv
->mii_bus
);
1102 platform_set_drvdata(pdev
, NULL
);
1104 clk_put(priv
->ephy_clk
);
1105 clk_put(priv
->emac_clk
);
1107 platform_set_drvdata(pdev
, NULL
);
1111 static int nuport_mac_remove(struct platform_device
*pdev
)
1113 struct net_device
*dev
= platform_get_drvdata(pdev
);
1114 struct nuport_mac_priv
*priv
= netdev_priv(dev
);
1116 unregister_netdev(dev
);
1117 phy_disconnect(priv
->phydev
);
1118 mdiobus_unregister(priv
->mii_bus
);
1119 kfree(priv
->mii_bus
->irq
);
1120 mdiobus_free(priv
->mii_bus
);
1121 clk_put(priv
->ephy_clk
);
1122 clk_put(priv
->emac_clk
);
1125 platform_set_drvdata(pdev
, NULL
);
1130 static struct of_device_id nuport_eth_ids
[] __initdata
= {
1131 {.compatible
= "moschip,nuport-mac",},
1135 static struct platform_driver nuport_eth_driver
= {
1137 .name
= "nuport-mac",
1138 .owner
= THIS_MODULE
,
1139 .of_match_table
= nuport_eth_ids
,
1141 .probe
= nuport_mac_probe
,
1142 .remove
= __devexit_p(nuport_mac_remove
),
1145 module_platform_driver(nuport_eth_driver
);
1147 MODULE_AUTHOR("Moschip Semiconductors Ltd.");
1148 MODULE_DESCRIPTION("Moschip MCS8140 Ethernet MAC driver");
1149 MODULE_LICENSE("GPL");