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;
144 void dcache_invalidate_only(unsigned long start, unsigned long end)
148 "1: mcr p15, 0, r0, c7, c6, 1\n"
150 " cmp r0, r1\n" " blo 1b\n");
153 void dcache_clean_range(unsigned long start, unsigned long end)
157 "1: mcr p15, 0, r0, c7, c10, 1 @ clean D entry\n"
161 " mcr p15, 0, r0, c7, c10, 4 @ drain WB\n");
164 static inline int nuport_mac_mii_busy_wait(struct nuport_mac_priv *priv)
167 unsigned long finish = jiffies + 3 * HZ;
171 if (!(nuport_mac_readl(MII_ADDR_REG) & MII_BUSY))
174 } while (!time_after_eq(curr, finish));
179 /* Read from PHY registers */
180 static int nuport_mac_mii_read(struct mii_bus *bus,
181 int mii_id, int regnum)
183 struct net_device *dev = bus->priv;
184 struct nuport_mac_priv *priv = netdev_priv(dev);
188 ret = nuport_mac_mii_busy_wait(priv);
192 val |= (mii_id << 11) | (regnum << 6) | MII_BUSY;
193 nuport_mac_writel(val, MII_ADDR_REG);
194 ret = nuport_mac_mii_busy_wait(priv);
198 return nuport_mac_readl(MII_DATA_REG);
201 static int nuport_mac_mii_write(struct mii_bus *bus, int mii_id,
202 int regnum, u16 value)
204 struct net_device *dev = bus->priv;
205 struct nuport_mac_priv *priv = netdev_priv(dev);
209 ret = nuport_mac_mii_busy_wait(priv);
213 val |= (mii_id << 11) | (regnum << 6) | MII_BUSY | MII_WRITE;
214 nuport_mac_writel(value, MII_DATA_REG);
215 nuport_mac_writel(val, MII_ADDR_REG);
217 return nuport_mac_mii_busy_wait(priv);
220 static int nuport_mac_mii_reset(struct mii_bus *bus)
225 static int nuport_mac_start_tx_dma(struct nuport_mac_priv *priv,
230 unsigned int timeout = 2048;
233 reg = nuport_mac_readl(TX_START_DMA);
242 p = dma_map_single(&priv->pdev->dev, skb->data,
243 skb->len, DMA_TO_DEVICE);
245 /* enable enhanced mode */
246 nuport_mac_writel(0x03, TX_DMA_ENH);
247 nuport_mac_writel(p, TX_BUFFER_ADDR);
248 nuport_mac_writel((skb->len) - 1, TX_PKT_BYTES);
250 nuport_mac_writel(0x0D, TX_START_DMA);
255 static void nuport_mac_reset_tx_dma(struct nuport_mac_priv *priv)
259 reg = nuport_mac_readl(TX_START_DMA);
261 nuport_mac_writel(reg, TX_START_DMA);
264 static void nuport_mac_start_rx_dma(struct nuport_mac_priv *priv,
269 p = dma_map_single(&priv->pdev->dev, skb->data,
270 RX_ALLOC_SIZE, DMA_FROM_DEVICE);
272 nuport_mac_writel(p, RX_BUFFER_ADDR);
274 nuport_mac_writel(0x01, RX_START_DMA);
277 static void nuport_mac_reset_rx_dma(struct nuport_mac_priv *priv)
281 reg = nuport_mac_readl(RX_START_DMA);
283 nuport_mac_writel(reg, RX_START_DMA);
286 /* I suppose this might do something, but I am not sure actually */
287 static void nuport_mac_disable_rx_dma(struct nuport_mac_priv *priv)
291 reg = nuport_mac_readl(RX_DMA_ENH);
293 nuport_mac_writel(reg, RX_DMA_ENH);
296 static void nuport_mac_enable_rx_dma(struct nuport_mac_priv *priv)
300 reg = nuport_mac_readl(RX_DMA_ENH);
302 nuport_mac_writel(reg, RX_DMA_ENH);
305 /* Add packets to the transmit queue */
306 static int nuport_mac_start_xmit(struct sk_buff *skb, struct net_device *dev)
309 struct nuport_mac_priv *priv = netdev_priv(dev);
312 dcache_clean_range((u32) skb->data, (u32)(skb->data + skb->len));
313 spin_lock_irqsave(&priv->lock, flags);
314 if (priv->first_pkt) {
315 ret = nuport_mac_start_tx_dma(priv, skb);
317 spin_unlock_irqrestore(&priv->lock, flags);
318 netdev_err(dev, "transmit path busy\n");
319 return NETDEV_TX_BUSY;
324 priv->tx_skb[priv->cur_tx] = skb;
325 dev->stats.tx_bytes += skb->len;
326 dev->stats.tx_packets++;
327 priv->valid_txskb[priv->cur_tx] = 1;
329 dev->trans_start = jiffies;
331 if (priv->cur_tx >= TX_RING_SIZE)
334 spin_unlock_irqrestore(&priv->lock, flags);
336 if (priv->valid_txskb[priv->cur_tx]) {
338 netif_stop_queue(dev);
344 static void nuport_mac_adjust_link(struct net_device *dev)
346 struct nuport_mac_priv *priv = netdev_priv(dev);
347 struct phy_device *phydev = priv->phydev;
348 unsigned int status_changed = 0;
353 if (priv->old_link != phydev->link) {
355 priv->old_link = phydev->link;
358 if (phydev->link & (priv->old_duplex != phydev->duplex)) {
359 reg = nuport_mac_readl(CTRL_REG);
360 if (phydev->duplex == DUPLEX_FULL)
364 nuport_mac_writel(reg, CTRL_REG);
367 priv->old_duplex = phydev->duplex;
373 pr_info("%s: link %s", dev->name, phydev->link ?
376 pr_cont(" - %d/%s", phydev->speed,
377 phydev->duplex == DUPLEX_FULL ? "full" : "half");
382 static irqreturn_t nuport_mac_link_interrupt(int irq, void *dev_id)
384 struct net_device *dev = dev_id;
385 struct nuport_mac_priv *priv = netdev_priv(dev);
389 reg = nuport_mac_readl(LINK_INT_CSR);
390 phy_addr = (reg >> 1) & 0x0f;
392 if (phy_addr != priv->phydev->addr) {
393 netdev_err(dev, "spurious PHY irq (phy: %d)\n", phy_addr);
397 priv->phydev->link = (reg & (1 << 16));
398 nuport_mac_adjust_link(dev);
403 static irqreturn_t nuport_mac_tx_interrupt(int irq, void *dev_id)
405 struct net_device *dev = (struct net_device *)dev_id;
406 struct nuport_mac_priv *priv = netdev_priv(dev);
411 spin_lock_irqsave(&priv->lock, flags);
412 skb = priv->tx_skb[priv->dma_tx];
413 priv->tx_skb[priv->dma_tx] = NULL;
414 priv->valid_txskb[priv->dma_tx] = 0;
415 dev_kfree_skb_irq(skb);
418 if (priv->dma_tx >= TX_RING_SIZE)
421 if (!priv->valid_txskb[priv->dma_tx])
424 ret = nuport_mac_start_tx_dma(priv, priv->tx_skb[priv->dma_tx]);
426 netdev_err(dev, "failed to restart TX dma\n");
430 netdev_err(dev, "restarting transmit queue\n");
431 netif_wake_queue(dev);
435 spin_unlock_irqrestore(&priv->lock, flags);
440 static unsigned int nuport_mac_has_work(struct nuport_mac_priv *priv)
444 for (i = 0; i < RX_RING_SIZE; i++)
445 if (priv->irq_rxskb[i])
451 static irqreturn_t nuport_mac_rx_interrupt(int irq, void *dev_id)
453 struct net_device *dev = (struct net_device *)dev_id;
454 struct nuport_mac_priv *priv = netdev_priv(dev);
457 spin_lock_irqsave(&priv->lock, flags);
458 priv->pkt_len[priv->dma_rx] = nuport_mac_readl(RX_ACT_BYTES) - 4;
459 priv->irq_rxskb[priv->dma_rx] = 0;
462 if (priv->dma_rx >= RX_RING_SIZE)
465 if (priv->irq_rxskb[priv->dma_rx] == 1)
466 nuport_mac_start_rx_dma(priv, priv->rx_skb[priv->dma_rx]);
468 if (likely(nuport_mac_has_work(priv))) {
469 /* find a way to disable DMA rx irq */
470 nuport_mac_disable_rx_dma(priv);
471 napi_schedule(&priv->napi);
474 spin_unlock_irqrestore(&priv->lock, flags);
479 /* Process received packets in tasklet */
480 static int nuport_mac_rx(struct net_device *dev, int limit)
482 struct nuport_mac_priv *priv = netdev_priv(dev);
487 while (count < limit && !priv->irq_rxskb[priv->cur_rx]) {
488 skb = priv->rx_skb[priv->cur_rx];
489 len = priv->pkt_len[priv->cur_rx];
490 dcache_invalidate_only(((u32) skb->data),
491 ((u32) (skb->data + len + 4)));
493 /* Remove 2 bytes added by RX buffer shifting */
495 skb->data = skb->data + 2;
497 /* Get packet status */
498 status = get_unaligned((u32 *) (skb->data + len));
501 /* packet filter failed */
502 if (!(status & (1 << 30))) {
503 dev_kfree_skb_irq(skb);
508 if (status & (1 << 31)) {
509 dev->stats.rx_missed_errors++;
510 dev_kfree_skb_irq(skb);
514 /* Not ethernet type */
515 if ((!(status & (1 << 18))) || (status & ERROR_FILTER_MASK))
516 dev->stats.rx_errors++;
518 if (len > MAX_ETH_FRAME_SIZE) {
519 dev_kfree_skb_irq(skb);
524 skb->protocol = eth_type_trans(skb, dev);
525 dev->stats.rx_packets++;
527 if (status & (1 << 29))
528 skb->pkt_type = PACKET_OTHERHOST;
529 if (status & (1 << 27))
530 skb->pkt_type = PACKET_MULTICAST;
531 if (status & (1 << 28))
532 skb->pkt_type = PACKET_BROADCAST;
534 skb->ip_summed = CHECKSUM_UNNECESSARY;
536 /* Pass the received packet to network layer */
537 netif_receive_skb(skb);
539 if (status != NET_RX_DROP)
540 dev->stats.rx_bytes += len - 4; /* Without CRC */
542 dev->stats.rx_dropped++;
544 dev->last_rx = jiffies;
547 skb = netdev_alloc_skb(dev, RX_ALLOC_SIZE);
548 skb_reserve(skb, RX_SKB_HEADROOM);
549 priv->rx_skb[priv->cur_rx] = skb;
550 priv->irq_rxskb[priv->cur_rx] = 1;
553 if (priv->cur_rx >= RX_RING_SIZE)
561 static int nuport_mac_poll(struct napi_struct *napi, int budget)
563 struct nuport_mac_priv *priv =
564 container_of(napi, struct nuport_mac_priv, napi);
565 struct net_device *dev = priv->dev;
568 work_done = nuport_mac_rx(dev, budget);
570 if (work_done < budget) {
572 nuport_mac_enable_rx_dma(priv);
578 static void nuport_mac_init_tx_ring(struct nuport_mac_priv *priv)
582 priv->cur_tx = priv->dma_tx = priv->tx_full = 0;
583 for (i = 0; i < TX_RING_SIZE; i++) {
584 priv->tx_skb[i] = NULL;
585 priv->valid_txskb[i] = 0;
590 static int nuport_mac_init_rx_ring(struct net_device *dev)
592 struct nuport_mac_priv *priv = netdev_priv(dev);
596 priv->cur_rx = priv->dma_rx = priv->rx_full = 0;
598 for (i = 0; i < RX_RING_SIZE; i++) {
599 skb = netdev_alloc_skb(dev, RX_ALLOC_SIZE);
602 skb_reserve(skb, RX_SKB_HEADROOM);
603 priv->rx_skb[i] = skb;
604 priv->irq_rxskb[i] = 1;
610 static void nuport_mac_free_rx_ring(struct nuport_mac_priv *priv)
614 for (i = 0; i < RX_RING_SIZE; i++) {
615 if (!priv->rx_skb[i])
618 dev_kfree_skb(priv->rx_skb[i]);
619 priv->rx_skb[i] = NULL;
623 static void nuport_mac_read_mac_address(struct net_device *dev)
625 struct nuport_mac_priv *priv = netdev_priv(dev);
628 for (i = 0; i < 4; i++)
629 dev->dev_addr[i] = nuport_mac_readb(MAC_ADDR_LOW_REG + i);
630 dev->dev_addr[4] = nuport_mac_readb(MAC_ADDR_HIGH_REG);
631 dev->dev_addr[5] = nuport_mac_readb(MAC_ADDR_HIGH_REG + 1);
633 if (!is_valid_ether_addr(dev->dev_addr)) {
634 dev_info(&priv->pdev->dev, "using random address\n");
635 random_ether_addr(dev->dev_addr);
639 static int nuport_mac_change_mac_address(struct net_device *dev, void *mac_addr)
641 struct sockaddr *addr = mac_addr;
642 struct nuport_mac_priv *priv = netdev_priv(dev);
643 unsigned long *temp = (unsigned long *)dev->dev_addr;
646 if (netif_running(dev))
649 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
651 spin_lock_irq(&priv->lock);
653 nuport_mac_writel(*temp, MAC_ADDR_LOW_REG);
654 temp = (unsigned long *)(dev->dev_addr + 4);
655 nuport_mac_writel(*temp, MAC_ADDR_HIGH_REG);
657 low = nuport_mac_readl(MAC_ADDR_LOW_REG);
658 high = nuport_mac_readl(MAC_ADDR_HIGH_REG);
660 spin_unlock_irq(&priv->lock);
665 static int nuport_mac_open(struct net_device *dev)
668 struct nuport_mac_priv *priv = netdev_priv(dev);
673 /* Enable hardware filters */
674 reg = nuport_mac_readl((void __iomem *)_CONFADDR_DBGLED);
676 nuport_mac_writel(reg, (void __iomem *)_CONFADDR_DBGLED);
678 /* Set LEDs to Link act and RX/TX mode */
679 reg = nuport_mac_readl((void __iomem *)(_CONFADDR_SYSDBG + 0x04));
681 nuport_mac_writel(reg, (void __iomem *)(_CONFADDR_SYSDBG + 0x04));
683 ret = clk_enable(priv->emac_clk);
685 netdev_err(dev, "failed to enable EMAC clock\n");
689 /* Set MAC into full duplex mode by default */
690 nuport_mac_writel(0x1010052C, CTRL_REG);
692 /* set mac address in hardware in case it was not already */
693 nuport_mac_change_mac_address(dev, dev->dev_addr);
695 ret = request_irq(priv->link_irq, &nuport_mac_link_interrupt,
698 netdev_err(dev, "unable to request link interrupt\n");
702 phy_start(priv->phydev);
704 /* Enable link interrupt monitoring */
705 spin_lock_irqsave(&priv->lock, flags);
706 nuport_mac_writel(0x1041 | (priv->phydev->addr << 1), LINK_INT_CSR);
707 nuport_mac_writel(0xFFFFF, LINK_INT_POLL_TIME);
708 spin_unlock_irqrestore(&priv->lock, flags);
710 ret = request_irq(priv->tx_irq, &nuport_mac_tx_interrupt,
713 netdev_err(dev, "unable to request rx interrupt\n");
717 napi_enable(&priv->napi);
719 ret = request_irq(priv->rx_irq, &nuport_mac_rx_interrupt,
722 netdev_err(dev, "unable to request tx interrupt\n");
726 /* Enable buffer shifting in RX */
727 tmp = nuport_mac_readb((void __iomem *)(_CONFADDR_SYSDBG + 0x1D));
729 nuport_mac_writeb(tmp, (void __iomem *)(_CONFADDR_SYSDBG + 0x1D));
731 netif_start_queue(dev);
733 nuport_mac_init_tx_ring(priv);
735 ret = nuport_mac_init_rx_ring(dev);
737 netdev_err(dev, "rx ring init failed\n");
741 nuport_mac_reset_tx_dma(priv);
742 nuport_mac_reset_rx_dma(priv);
745 nuport_mac_start_rx_dma(priv, priv->rx_skb[0]);
750 nuport_mac_free_rx_ring(priv);
751 free_irq(priv->rx_irq, dev);
753 free_irq(priv->tx_irq, dev);
755 free_irq(priv->link_irq, dev);
757 clk_disable(priv->emac_clk);
761 static int nuport_mac_close(struct net_device *dev)
763 struct nuport_mac_priv *priv = netdev_priv(dev);
765 spin_lock_irq(&priv->lock);
766 napi_disable(&priv->napi);
767 netif_stop_queue(dev);
769 free_irq(priv->link_irq, dev);
770 nuport_mac_writel(0x00, LINK_INT_CSR);
771 nuport_mac_writel(0x00, LINK_INT_POLL_TIME);
772 phy_stop(priv->phydev);
774 free_irq(priv->tx_irq, dev);
775 free_irq(priv->rx_irq, dev);
776 spin_unlock_irq(&priv->lock);
778 nuport_mac_free_rx_ring(priv);
780 clk_disable(priv->emac_clk);
785 static int nuport_mac_mii_probe(struct net_device *dev)
787 struct nuport_mac_priv *priv = netdev_priv(dev);
788 struct phy_device *phydev = NULL;
791 ret = clk_enable(priv->ephy_clk);
793 netdev_err(dev, "unable to enable ePHY clk\n");
797 phydev = phy_find_first(priv->mii_bus);
799 netdev_err(dev, "no PHYs found\n");
804 phydev = phy_connect(dev, dev_name(&phydev->dev),
805 nuport_mac_adjust_link, 0,
806 PHY_INTERFACE_MODE_MII);
807 if (IS_ERR(phydev)) {
808 netdev_err(dev, "could not attach PHY\n");
809 ret = PTR_ERR(phydev);
813 phydev->supported &= PHY_BASIC_FEATURES;
814 phydev->advertising = phydev->supported;
815 priv->phydev = phydev;
817 priv->old_duplex = -1;
819 dev_info(&priv->pdev->dev, "attached PHY driver [%s] "
820 "(mii_bus:phy_addr=%d)\n",
821 phydev->drv->name, phydev->addr);
826 /* disable the Ethernet PHY clock for the moment */
827 clk_disable(priv->ephy_clk);
832 static void nuport_mac_ethtool_drvinfo(struct net_device *dev,
833 struct ethtool_drvinfo *info)
835 strncpy(info->driver, "nuport-mac", sizeof(info->driver));
836 strncpy(info->version, "0.1", sizeof(info->version));
837 strncpy(info->fw_version, "N/A", sizeof(info->fw_version));
838 strncpy(info->bus_info, "internal", sizeof(info->bus_info));
840 info->testinfo_len = 0;
841 info->regdump_len = 0;
842 info->eedump_len = 0;
845 static int nuport_mac_ethtool_get_settings(struct net_device *dev,
846 struct ethtool_cmd *cmd)
848 struct nuport_mac_priv *priv = netdev_priv(dev);
851 return phy_ethtool_gset(priv->phydev, cmd);
856 static int nuport_mac_ethtool_set_settings(struct net_device *dev,
857 struct ethtool_cmd *cmd)
859 struct nuport_mac_priv *priv = netdev_priv(dev);
862 return phy_ethtool_sset(priv->phydev, cmd);
867 static const struct ethtool_ops nuport_mac_ethtool_ops = {
868 .get_drvinfo = nuport_mac_ethtool_drvinfo,
869 .get_link = ethtool_op_get_link,
870 .get_settings = nuport_mac_ethtool_get_settings,
871 .set_settings = nuport_mac_ethtool_set_settings,
874 static const struct net_device_ops nuport_mac_ops = {
875 .ndo_open = nuport_mac_open,
876 .ndo_stop = nuport_mac_close,
877 .ndo_start_xmit = nuport_mac_start_xmit,
878 .ndo_change_mtu = eth_change_mtu,
879 .ndo_validate_addr = eth_validate_addr,
880 .ndo_set_mac_address = nuport_mac_change_mac_address,
883 static int __init nuport_mac_probe(struct platform_device *pdev)
885 struct net_device *dev;
886 struct nuport_mac_priv *priv = NULL;
887 struct resource *regs, *dma;
889 int rx_irq, tx_irq, link_irq;
892 dev = alloc_etherdev(sizeof(struct nuport_mac_priv));
894 dev_err(&pdev->dev, "no memory for net_device\n");
898 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
899 dma = platform_get_resource(pdev, IORESOURCE_MEM, 1);
901 dev_err(&pdev->dev, "failed to get regs resources\n");
906 rx_irq = platform_get_irq(pdev, 0);
907 tx_irq = platform_get_irq(pdev, 1);
908 link_irq = platform_get_irq(pdev, 2);
909 if (rx_irq < 0 || tx_irq < 0 || link_irq < 0) {
914 platform_set_drvdata(pdev, dev);
915 SET_NETDEV_DEV(dev, &pdev->dev);
916 priv = netdev_priv(dev);
919 spin_lock_init(&priv->lock);
921 priv->mac_base = devm_ioremap(&pdev->dev,
922 regs->start, resource_size(regs));
923 if (!priv->mac_base) {
924 dev_err(&pdev->dev, "failed to remap regs\n");
929 priv->dma_base = devm_ioremap(&pdev->dev,
930 dma->start, resource_size(dma));
931 if (!priv->dma_base) {
932 dev_err(&pdev->dev, "failed to remap dma-regs\n");
937 priv->emac_clk = clk_get(&pdev->dev, "emac");
938 if (IS_ERR_OR_NULL(priv->emac_clk)) {
939 dev_err(&pdev->dev, "failed to get emac clk\n");
940 ret = PTR_ERR(priv->emac_clk);
944 priv->ephy_clk = clk_get(&pdev->dev, "ephy");
945 if (IS_ERR_OR_NULL(priv->ephy_clk)) {
946 dev_err(&pdev->dev, "failed to get ephy clk\n");
947 ret = PTR_ERR(priv->ephy_clk);
951 priv->link_irq = link_irq;
952 priv->rx_irq = rx_irq;
953 priv->tx_irq = tx_irq;
954 dev->netdev_ops = &nuport_mac_ops;
955 dev->ethtool_ops = &nuport_mac_ethtool_ops;
956 dev->watchdog_timeo = HZ;
957 dev->flags = IFF_BROADCAST; /* Supports Broadcast */
959 netif_napi_add(dev, &priv->napi, nuport_mac_poll, 64);
961 priv->mii_bus = mdiobus_alloc();
962 if (!priv->mii_bus) {
963 dev_err(&pdev->dev, "mii bus allocation failed\n");
967 priv->mii_bus->priv = dev;
968 priv->mii_bus->read = nuport_mac_mii_read;
969 priv->mii_bus->write = nuport_mac_mii_write;
970 priv->mii_bus->reset = nuport_mac_mii_reset;
971 priv->mii_bus->name = "nuport-mac-mii";
972 priv->mii_bus->phy_mask = (1 << 0);
973 snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%s", pdev->name);
974 priv->mii_bus->irq = kzalloc(PHY_MAX_ADDR * sizeof(int), GFP_KERNEL);
975 if (!priv->mii_bus->irq) {
976 dev_err(&pdev->dev, "failed to allocate mii_bus irqs\n");
981 /* We support PHY interrupts routed back to the MAC */
982 for (i = 0; i < PHY_MAX_ADDR; i++)
983 priv->mii_bus->irq[i] = PHY_IGNORE_INTERRUPT;
985 ret = mdiobus_register(priv->mii_bus);
987 dev_err(&pdev->dev, "failed to register mii_bus\n");
991 ret = nuport_mac_mii_probe(dev);
993 dev_err(&pdev->dev, "failed to probe MII bus\n");
994 goto out_mdio_unregister;
997 ret = register_netdev(dev);
999 dev_err(&pdev->dev, "failed to register net_device\n");
1000 goto out_mdio_probe;
1003 /* read existing mac address */
1004 nuport_mac_read_mac_address(dev);
1006 dev_info(&pdev->dev, "registered (MAC: %pM)\n", dev->dev_addr);
1011 phy_disconnect(priv->phydev);
1012 out_mdio_unregister:
1013 mdiobus_unregister(priv->mii_bus);
1015 kfree(priv->mii_bus->irq);
1017 mdiobus_free(priv->mii_bus);
1019 platform_set_drvdata(pdev, NULL);
1021 clk_put(priv->ephy_clk);
1022 clk_put(priv->emac_clk);
1024 platform_set_drvdata(pdev, NULL);
1028 static int nuport_mac_remove(struct platform_device *pdev)
1030 struct net_device *dev = platform_get_drvdata(pdev);
1031 struct nuport_mac_priv *priv = netdev_priv(dev);
1033 unregister_netdev(dev);
1034 phy_disconnect(priv->phydev);
1035 mdiobus_unregister(priv->mii_bus);
1036 kfree(priv->mii_bus->irq);
1037 mdiobus_free(priv->mii_bus);
1038 clk_put(priv->ephy_clk);
1039 clk_put(priv->emac_clk);
1042 platform_set_drvdata(pdev, NULL);
1047 static struct of_device_id nuport_eth_ids[] __initdata = {
1048 {.compatible = "moschip,nuport-mac",},
1052 static struct platform_driver nuport_eth_driver = {
1054 .name = "nuport-mac",
1055 .owner = THIS_MODULE,
1056 .of_match_table = nuport_eth_ids,
1058 .probe = nuport_mac_probe,
1059 .remove = __devexit_p(nuport_mac_remove),
1062 module_platform_driver(nuport_eth_driver);
1064 MODULE_AUTHOR("Moschip Semiconductors Ltd.");
1065 MODULE_DESCRIPTION("Moschip MCS8140 Ethernet MAC driver");
1066 MODULE_LICENSE("GPL");