xref: /openbmc/linux/drivers/net/ethernet/dnet.c (revision 92efe48e)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
29c8571daSJeff Kirsher /*
39c8571daSJeff Kirsher  * Dave DNET Ethernet Controller driver
49c8571daSJeff Kirsher  *
59c8571daSJeff Kirsher  * Copyright (C) 2008 Dave S.r.l. <www.dave.eu>
69c8571daSJeff Kirsher  * Copyright (C) 2009 Ilya Yanok, Emcraft Systems Ltd, <yanok@emcraft.com>
79c8571daSJeff Kirsher  */
89c8571daSJeff Kirsher #include <linux/io.h>
99c8571daSJeff Kirsher #include <linux/module.h>
109c8571daSJeff Kirsher #include <linux/moduleparam.h>
119c8571daSJeff Kirsher #include <linux/kernel.h>
129c8571daSJeff Kirsher #include <linux/types.h>
139c8571daSJeff Kirsher #include <linux/slab.h>
149c8571daSJeff Kirsher #include <linux/delay.h>
159c8571daSJeff Kirsher #include <linux/interrupt.h>
169c8571daSJeff Kirsher #include <linux/netdevice.h>
179c8571daSJeff Kirsher #include <linux/etherdevice.h>
189c8571daSJeff Kirsher #include <linux/dma-mapping.h>
199c8571daSJeff Kirsher #include <linux/platform_device.h>
209c8571daSJeff Kirsher #include <linux/phy.h>
219c8571daSJeff Kirsher 
229c8571daSJeff Kirsher #include "dnet.h"
239c8571daSJeff Kirsher 
249c8571daSJeff Kirsher #undef DEBUG
259c8571daSJeff Kirsher 
269c8571daSJeff Kirsher /* function for reading internal MAC register */
279c8571daSJeff Kirsher static u16 dnet_readw_mac(struct dnet *bp, u16 reg)
289c8571daSJeff Kirsher {
299c8571daSJeff Kirsher 	u16 data_read;
309c8571daSJeff Kirsher 
319c8571daSJeff Kirsher 	/* issue a read */
329c8571daSJeff Kirsher 	dnet_writel(bp, reg, MACREG_ADDR);
339c8571daSJeff Kirsher 
349c8571daSJeff Kirsher 	/* since a read/write op to the MAC is very slow,
359c8571daSJeff Kirsher 	 * we must wait before reading the data */
369c8571daSJeff Kirsher 	ndelay(500);
379c8571daSJeff Kirsher 
389c8571daSJeff Kirsher 	/* read data read from the MAC register */
399c8571daSJeff Kirsher 	data_read = dnet_readl(bp, MACREG_DATA);
409c8571daSJeff Kirsher 
419c8571daSJeff Kirsher 	/* all done */
429c8571daSJeff Kirsher 	return data_read;
439c8571daSJeff Kirsher }
449c8571daSJeff Kirsher 
459c8571daSJeff Kirsher /* function for writing internal MAC register */
469c8571daSJeff Kirsher static void dnet_writew_mac(struct dnet *bp, u16 reg, u16 val)
479c8571daSJeff Kirsher {
489c8571daSJeff Kirsher 	/* load data to write */
499c8571daSJeff Kirsher 	dnet_writel(bp, val, MACREG_DATA);
509c8571daSJeff Kirsher 
519c8571daSJeff Kirsher 	/* issue a write */
529c8571daSJeff Kirsher 	dnet_writel(bp, reg | DNET_INTERNAL_WRITE, MACREG_ADDR);
539c8571daSJeff Kirsher 
549c8571daSJeff Kirsher 	/* since a read/write op to the MAC is very slow,
559c8571daSJeff Kirsher 	 * we must wait before exiting */
569c8571daSJeff Kirsher 	ndelay(500);
579c8571daSJeff Kirsher }
589c8571daSJeff Kirsher 
599c8571daSJeff Kirsher static void __dnet_set_hwaddr(struct dnet *bp)
609c8571daSJeff Kirsher {
619c8571daSJeff Kirsher 	u16 tmp;
629c8571daSJeff Kirsher 
639c8571daSJeff Kirsher 	tmp = be16_to_cpup((__be16 *)bp->dev->dev_addr);
649c8571daSJeff Kirsher 	dnet_writew_mac(bp, DNET_INTERNAL_MAC_ADDR_0_REG, tmp);
659c8571daSJeff Kirsher 	tmp = be16_to_cpup((__be16 *)(bp->dev->dev_addr + 2));
669c8571daSJeff Kirsher 	dnet_writew_mac(bp, DNET_INTERNAL_MAC_ADDR_1_REG, tmp);
679c8571daSJeff Kirsher 	tmp = be16_to_cpup((__be16 *)(bp->dev->dev_addr + 4));
689c8571daSJeff Kirsher 	dnet_writew_mac(bp, DNET_INTERNAL_MAC_ADDR_2_REG, tmp);
699c8571daSJeff Kirsher }
709c8571daSJeff Kirsher 
71a0a4efedSBill Pemberton static void dnet_get_hwaddr(struct dnet *bp)
729c8571daSJeff Kirsher {
739c8571daSJeff Kirsher 	u16 tmp;
749c8571daSJeff Kirsher 	u8 addr[6];
759c8571daSJeff Kirsher 
769c8571daSJeff Kirsher 	/*
779c8571daSJeff Kirsher 	 * from MAC docs:
789c8571daSJeff Kirsher 	 * "Note that the MAC address is stored in the registers in Hexadecimal
799c8571daSJeff Kirsher 	 * form. For example, to set the MAC Address to: AC-DE-48-00-00-80
809c8571daSJeff Kirsher 	 * would require writing 0xAC (octet 0) to address 0x0B (high byte of
819c8571daSJeff Kirsher 	 * Mac_addr[15:0]), 0xDE (octet 1) to address 0x0A (Low byte of
829c8571daSJeff Kirsher 	 * Mac_addr[15:0]), 0x48 (octet 2) to address 0x0D (high byte of
839c8571daSJeff Kirsher 	 * Mac_addr[15:0]), 0x00 (octet 3) to address 0x0C (Low byte of
849c8571daSJeff Kirsher 	 * Mac_addr[15:0]), 0x00 (octet 4) to address 0x0F (high byte of
859c8571daSJeff Kirsher 	 * Mac_addr[15:0]), and 0x80 (octet 5) to address * 0x0E (Low byte of
869c8571daSJeff Kirsher 	 * Mac_addr[15:0]).
879c8571daSJeff Kirsher 	 */
889c8571daSJeff Kirsher 	tmp = dnet_readw_mac(bp, DNET_INTERNAL_MAC_ADDR_0_REG);
899c8571daSJeff Kirsher 	*((__be16 *)addr) = cpu_to_be16(tmp);
909c8571daSJeff Kirsher 	tmp = dnet_readw_mac(bp, DNET_INTERNAL_MAC_ADDR_1_REG);
919c8571daSJeff Kirsher 	*((__be16 *)(addr + 2)) = cpu_to_be16(tmp);
929c8571daSJeff Kirsher 	tmp = dnet_readw_mac(bp, DNET_INTERNAL_MAC_ADDR_2_REG);
939c8571daSJeff Kirsher 	*((__be16 *)(addr + 4)) = cpu_to_be16(tmp);
949c8571daSJeff Kirsher 
959c8571daSJeff Kirsher 	if (is_valid_ether_addr(addr))
969c8571daSJeff Kirsher 		memcpy(bp->dev->dev_addr, addr, sizeof(addr));
979c8571daSJeff Kirsher }
989c8571daSJeff Kirsher 
999c8571daSJeff Kirsher static int dnet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
1009c8571daSJeff Kirsher {
1019c8571daSJeff Kirsher 	struct dnet *bp = bus->priv;
1029c8571daSJeff Kirsher 	u16 value;
1039c8571daSJeff Kirsher 
1049c8571daSJeff Kirsher 	while (!(dnet_readw_mac(bp, DNET_INTERNAL_GMII_MNG_CTL_REG)
1059c8571daSJeff Kirsher 				& DNET_INTERNAL_GMII_MNG_CMD_FIN))
1069c8571daSJeff Kirsher 		cpu_relax();
1079c8571daSJeff Kirsher 
1089c8571daSJeff Kirsher 	/* only 5 bits allowed for phy-addr and reg_offset */
1099c8571daSJeff Kirsher 	mii_id &= 0x1f;
1109c8571daSJeff Kirsher 	regnum &= 0x1f;
1119c8571daSJeff Kirsher 
1129c8571daSJeff Kirsher 	/* prepare reg_value for a read */
1139c8571daSJeff Kirsher 	value = (mii_id << 8);
1149c8571daSJeff Kirsher 	value |= regnum;
1159c8571daSJeff Kirsher 
1169c8571daSJeff Kirsher 	/* write control word */
1179c8571daSJeff Kirsher 	dnet_writew_mac(bp, DNET_INTERNAL_GMII_MNG_CTL_REG, value);
1189c8571daSJeff Kirsher 
1199c8571daSJeff Kirsher 	/* wait for end of transfer */
1209c8571daSJeff Kirsher 	while (!(dnet_readw_mac(bp, DNET_INTERNAL_GMII_MNG_CTL_REG)
1219c8571daSJeff Kirsher 				& DNET_INTERNAL_GMII_MNG_CMD_FIN))
1229c8571daSJeff Kirsher 		cpu_relax();
1239c8571daSJeff Kirsher 
1249c8571daSJeff Kirsher 	value = dnet_readw_mac(bp, DNET_INTERNAL_GMII_MNG_DAT_REG);
1259c8571daSJeff Kirsher 
1269c8571daSJeff Kirsher 	pr_debug("mdio_read %02x:%02x <- %04x\n", mii_id, regnum, value);
1279c8571daSJeff Kirsher 
1289c8571daSJeff Kirsher 	return value;
1299c8571daSJeff Kirsher }
1309c8571daSJeff Kirsher 
1319c8571daSJeff Kirsher static int dnet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
1329c8571daSJeff Kirsher 			   u16 value)
1339c8571daSJeff Kirsher {
1349c8571daSJeff Kirsher 	struct dnet *bp = bus->priv;
1359c8571daSJeff Kirsher 	u16 tmp;
1369c8571daSJeff Kirsher 
1379c8571daSJeff Kirsher 	pr_debug("mdio_write %02x:%02x <- %04x\n", mii_id, regnum, value);
1389c8571daSJeff Kirsher 
1399c8571daSJeff Kirsher 	while (!(dnet_readw_mac(bp, DNET_INTERNAL_GMII_MNG_CTL_REG)
1409c8571daSJeff Kirsher 				& DNET_INTERNAL_GMII_MNG_CMD_FIN))
1419c8571daSJeff Kirsher 		cpu_relax();
1429c8571daSJeff Kirsher 
1439c8571daSJeff Kirsher 	/* prepare for a write operation */
1449c8571daSJeff Kirsher 	tmp = (1 << 13);
1459c8571daSJeff Kirsher 
1469c8571daSJeff Kirsher 	/* only 5 bits allowed for phy-addr and reg_offset */
1479c8571daSJeff Kirsher 	mii_id &= 0x1f;
1489c8571daSJeff Kirsher 	regnum &= 0x1f;
1499c8571daSJeff Kirsher 
1509c8571daSJeff Kirsher 	/* only 16 bits on data */
1519c8571daSJeff Kirsher 	value &= 0xffff;
1529c8571daSJeff Kirsher 
1539c8571daSJeff Kirsher 	/* prepare reg_value for a write */
1549c8571daSJeff Kirsher 	tmp |= (mii_id << 8);
1559c8571daSJeff Kirsher 	tmp |= regnum;
1569c8571daSJeff Kirsher 
1579c8571daSJeff Kirsher 	/* write data to write first */
1589c8571daSJeff Kirsher 	dnet_writew_mac(bp, DNET_INTERNAL_GMII_MNG_DAT_REG, value);
1599c8571daSJeff Kirsher 
1609c8571daSJeff Kirsher 	/* write control word */
1619c8571daSJeff Kirsher 	dnet_writew_mac(bp, DNET_INTERNAL_GMII_MNG_CTL_REG, tmp);
1629c8571daSJeff Kirsher 
1639c8571daSJeff Kirsher 	while (!(dnet_readw_mac(bp, DNET_INTERNAL_GMII_MNG_CTL_REG)
1649c8571daSJeff Kirsher 				& DNET_INTERNAL_GMII_MNG_CMD_FIN))
1659c8571daSJeff Kirsher 		cpu_relax();
1669c8571daSJeff Kirsher 
1679c8571daSJeff Kirsher 	return 0;
1689c8571daSJeff Kirsher }
1699c8571daSJeff Kirsher 
1709c8571daSJeff Kirsher static void dnet_handle_link_change(struct net_device *dev)
1719c8571daSJeff Kirsher {
1729c8571daSJeff Kirsher 	struct dnet *bp = netdev_priv(dev);
173958974fdSPhilippe Reynes 	struct phy_device *phydev = dev->phydev;
1749c8571daSJeff Kirsher 	unsigned long flags;
1759c8571daSJeff Kirsher 	u32 mode_reg, ctl_reg;
1769c8571daSJeff Kirsher 
1779c8571daSJeff Kirsher 	int status_change = 0;
1789c8571daSJeff Kirsher 
1799c8571daSJeff Kirsher 	spin_lock_irqsave(&bp->lock, flags);
1809c8571daSJeff Kirsher 
1819c8571daSJeff Kirsher 	mode_reg = dnet_readw_mac(bp, DNET_INTERNAL_MODE_REG);
1829c8571daSJeff Kirsher 	ctl_reg = dnet_readw_mac(bp, DNET_INTERNAL_RXTX_CONTROL_REG);
1839c8571daSJeff Kirsher 
1849c8571daSJeff Kirsher 	if (phydev->link) {
1859c8571daSJeff Kirsher 		if (bp->duplex != phydev->duplex) {
1869c8571daSJeff Kirsher 			if (phydev->duplex)
1879c8571daSJeff Kirsher 				ctl_reg &=
1889c8571daSJeff Kirsher 				    ~(DNET_INTERNAL_RXTX_CONTROL_ENABLEHALFDUP);
1899c8571daSJeff Kirsher 			else
1909c8571daSJeff Kirsher 				ctl_reg |=
1919c8571daSJeff Kirsher 				    DNET_INTERNAL_RXTX_CONTROL_ENABLEHALFDUP;
1929c8571daSJeff Kirsher 
1939c8571daSJeff Kirsher 			bp->duplex = phydev->duplex;
1949c8571daSJeff Kirsher 			status_change = 1;
1959c8571daSJeff Kirsher 		}
1969c8571daSJeff Kirsher 
1979c8571daSJeff Kirsher 		if (bp->speed != phydev->speed) {
1989c8571daSJeff Kirsher 			status_change = 1;
1999c8571daSJeff Kirsher 			switch (phydev->speed) {
2009c8571daSJeff Kirsher 			case 1000:
2019c8571daSJeff Kirsher 				mode_reg |= DNET_INTERNAL_MODE_GBITEN;
2029c8571daSJeff Kirsher 				break;
2039c8571daSJeff Kirsher 			case 100:
2049c8571daSJeff Kirsher 			case 10:
2059c8571daSJeff Kirsher 				mode_reg &= ~DNET_INTERNAL_MODE_GBITEN;
2069c8571daSJeff Kirsher 				break;
2079c8571daSJeff Kirsher 			default:
2089c8571daSJeff Kirsher 				printk(KERN_WARNING
2099c8571daSJeff Kirsher 				       "%s: Ack!  Speed (%d) is not "
2109c8571daSJeff Kirsher 				       "10/100/1000!\n", dev->name,
2119c8571daSJeff Kirsher 				       phydev->speed);
2129c8571daSJeff Kirsher 				break;
2139c8571daSJeff Kirsher 			}
2149c8571daSJeff Kirsher 			bp->speed = phydev->speed;
2159c8571daSJeff Kirsher 		}
2169c8571daSJeff Kirsher 	}
2179c8571daSJeff Kirsher 
2189c8571daSJeff Kirsher 	if (phydev->link != bp->link) {
2199c8571daSJeff Kirsher 		if (phydev->link) {
2209c8571daSJeff Kirsher 			mode_reg |=
2219c8571daSJeff Kirsher 			    (DNET_INTERNAL_MODE_RXEN | DNET_INTERNAL_MODE_TXEN);
2229c8571daSJeff Kirsher 		} else {
2239c8571daSJeff Kirsher 			mode_reg &=
2249c8571daSJeff Kirsher 			    ~(DNET_INTERNAL_MODE_RXEN |
2259c8571daSJeff Kirsher 			      DNET_INTERNAL_MODE_TXEN);
2269c8571daSJeff Kirsher 			bp->speed = 0;
2279c8571daSJeff Kirsher 			bp->duplex = -1;
2289c8571daSJeff Kirsher 		}
2299c8571daSJeff Kirsher 		bp->link = phydev->link;
2309c8571daSJeff Kirsher 
2319c8571daSJeff Kirsher 		status_change = 1;
2329c8571daSJeff Kirsher 	}
2339c8571daSJeff Kirsher 
2349c8571daSJeff Kirsher 	if (status_change) {
2359c8571daSJeff Kirsher 		dnet_writew_mac(bp, DNET_INTERNAL_RXTX_CONTROL_REG, ctl_reg);
2369c8571daSJeff Kirsher 		dnet_writew_mac(bp, DNET_INTERNAL_MODE_REG, mode_reg);
2379c8571daSJeff Kirsher 	}
2389c8571daSJeff Kirsher 
2399c8571daSJeff Kirsher 	spin_unlock_irqrestore(&bp->lock, flags);
2409c8571daSJeff Kirsher 
2419c8571daSJeff Kirsher 	if (status_change) {
2429c8571daSJeff Kirsher 		if (phydev->link)
2439c8571daSJeff Kirsher 			printk(KERN_INFO "%s: link up (%d/%s)\n",
2449c8571daSJeff Kirsher 			       dev->name, phydev->speed,
2459c8571daSJeff Kirsher 			       DUPLEX_FULL == phydev->duplex ? "Full" : "Half");
2469c8571daSJeff Kirsher 		else
2479c8571daSJeff Kirsher 			printk(KERN_INFO "%s: link down\n", dev->name);
2489c8571daSJeff Kirsher 	}
2499c8571daSJeff Kirsher }
2509c8571daSJeff Kirsher 
2519c8571daSJeff Kirsher static int dnet_mii_probe(struct net_device *dev)
2529c8571daSJeff Kirsher {
2539c8571daSJeff Kirsher 	struct dnet *bp = netdev_priv(dev);
2549c8571daSJeff Kirsher 	struct phy_device *phydev = NULL;
2559c8571daSJeff Kirsher 
2569c8571daSJeff Kirsher 	/* find the first phy */
25704521bf8SAndrew Lunn 	phydev = phy_find_first(bp->mii_bus);
2589c8571daSJeff Kirsher 
2599c8571daSJeff Kirsher 	if (!phydev) {
2609c8571daSJeff Kirsher 		printk(KERN_ERR "%s: no PHY found\n", dev->name);
2619c8571daSJeff Kirsher 		return -ENODEV;
2629c8571daSJeff Kirsher 	}
2639c8571daSJeff Kirsher 
2649c8571daSJeff Kirsher 	/* TODO : add pin_irq */
2659c8571daSJeff Kirsher 
2669c8571daSJeff Kirsher 	/* attach the mac to the phy */
2679c8571daSJeff Kirsher 	if (bp->capabilities & DNET_HAS_RMII) {
26884eff6d1SAndrew Lunn 		phydev = phy_connect(dev, phydev_name(phydev),
269f9a8f83bSFlorian Fainelli 				     &dnet_handle_link_change,
2709c8571daSJeff Kirsher 				     PHY_INTERFACE_MODE_RMII);
2719c8571daSJeff Kirsher 	} else {
27284eff6d1SAndrew Lunn 		phydev = phy_connect(dev, phydev_name(phydev),
273f9a8f83bSFlorian Fainelli 				     &dnet_handle_link_change,
2749c8571daSJeff Kirsher 				     PHY_INTERFACE_MODE_MII);
2759c8571daSJeff Kirsher 	}
2769c8571daSJeff Kirsher 
2779c8571daSJeff Kirsher 	if (IS_ERR(phydev)) {
2789c8571daSJeff Kirsher 		printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
2799c8571daSJeff Kirsher 		return PTR_ERR(phydev);
2809c8571daSJeff Kirsher 	}
2819c8571daSJeff Kirsher 
2829c8571daSJeff Kirsher 	/* mask with MAC supported features */
2839c8571daSJeff Kirsher 	if (bp->capabilities & DNET_HAS_GIGABIT)
28458056c1eSAndrew Lunn 		phy_set_max_speed(phydev, SPEED_1000);
2859c8571daSJeff Kirsher 	else
28658056c1eSAndrew Lunn 		phy_set_max_speed(phydev, SPEED_100);
2879c8571daSJeff Kirsher 
288af8d9bb2SAndrew Lunn 	phy_support_asym_pause(phydev);
2899c8571daSJeff Kirsher 
2909c8571daSJeff Kirsher 	bp->link = 0;
2919c8571daSJeff Kirsher 	bp->speed = 0;
2929c8571daSJeff Kirsher 	bp->duplex = -1;
2939c8571daSJeff Kirsher 
2949c8571daSJeff Kirsher 	return 0;
2959c8571daSJeff Kirsher }
2969c8571daSJeff Kirsher 
2979c8571daSJeff Kirsher static int dnet_mii_init(struct dnet *bp)
2989c8571daSJeff Kirsher {
299e7f4dc35SAndrew Lunn 	int err;
3009c8571daSJeff Kirsher 
3019c8571daSJeff Kirsher 	bp->mii_bus = mdiobus_alloc();
3029c8571daSJeff Kirsher 	if (bp->mii_bus == NULL)
3039c8571daSJeff Kirsher 		return -ENOMEM;
3049c8571daSJeff Kirsher 
3059c8571daSJeff Kirsher 	bp->mii_bus->name = "dnet_mii_bus";
3069c8571daSJeff Kirsher 	bp->mii_bus->read = &dnet_mdio_read;
3079c8571daSJeff Kirsher 	bp->mii_bus->write = &dnet_mdio_write;
3089c8571daSJeff Kirsher 
30963f67830SFlorian Fainelli 	snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
31063f67830SFlorian Fainelli 		bp->pdev->name, bp->pdev->id);
3119c8571daSJeff Kirsher 
3129c8571daSJeff Kirsher 	bp->mii_bus->priv = bp;
3139c8571daSJeff Kirsher 
3149c8571daSJeff Kirsher 	if (mdiobus_register(bp->mii_bus)) {
3159c8571daSJeff Kirsher 		err = -ENXIO;
316ae29223eSHimangi Saraogi 		goto err_out;
3179c8571daSJeff Kirsher 	}
3189c8571daSJeff Kirsher 
3199c8571daSJeff Kirsher 	if (dnet_mii_probe(bp->dev) != 0) {
3209c8571daSJeff Kirsher 		err = -ENXIO;
3219c8571daSJeff Kirsher 		goto err_out_unregister_bus;
3229c8571daSJeff Kirsher 	}
3239c8571daSJeff Kirsher 
3249c8571daSJeff Kirsher 	return 0;
3259c8571daSJeff Kirsher 
3269c8571daSJeff Kirsher err_out_unregister_bus:
3279c8571daSJeff Kirsher 	mdiobus_unregister(bp->mii_bus);
3289c8571daSJeff Kirsher err_out:
3299c8571daSJeff Kirsher 	mdiobus_free(bp->mii_bus);
3309c8571daSJeff Kirsher 	return err;
3319c8571daSJeff Kirsher }
3329c8571daSJeff Kirsher 
3339c8571daSJeff Kirsher /* For Neptune board: LINK1000 as Link LED and TX as activity LED */
3349c8571daSJeff Kirsher static int dnet_phy_marvell_fixup(struct phy_device *phydev)
3359c8571daSJeff Kirsher {
3369c8571daSJeff Kirsher 	return phy_write(phydev, 0x18, 0x4148);
3379c8571daSJeff Kirsher }
3389c8571daSJeff Kirsher 
3399c8571daSJeff Kirsher static void dnet_update_stats(struct dnet *bp)
3409c8571daSJeff Kirsher {
3419c8571daSJeff Kirsher 	u32 __iomem *reg = bp->regs + DNET_RX_PKT_IGNR_CNT;
3429c8571daSJeff Kirsher 	u32 *p = &bp->hw_stats.rx_pkt_ignr;
3439c8571daSJeff Kirsher 	u32 *end = &bp->hw_stats.rx_byte + 1;
3449c8571daSJeff Kirsher 
3459c8571daSJeff Kirsher 	WARN_ON((unsigned long)(end - p - 1) !=
3469c8571daSJeff Kirsher 		(DNET_RX_BYTE_CNT - DNET_RX_PKT_IGNR_CNT) / 4);
3479c8571daSJeff Kirsher 
3489c8571daSJeff Kirsher 	for (; p < end; p++, reg++)
3499c8571daSJeff Kirsher 		*p += readl(reg);
3509c8571daSJeff Kirsher 
3519c8571daSJeff Kirsher 	reg = bp->regs + DNET_TX_UNICAST_CNT;
3529c8571daSJeff Kirsher 	p = &bp->hw_stats.tx_unicast;
3539c8571daSJeff Kirsher 	end = &bp->hw_stats.tx_byte + 1;
3549c8571daSJeff Kirsher 
3559c8571daSJeff Kirsher 	WARN_ON((unsigned long)(end - p - 1) !=
3569c8571daSJeff Kirsher 		(DNET_TX_BYTE_CNT - DNET_TX_UNICAST_CNT) / 4);
3579c8571daSJeff Kirsher 
3589c8571daSJeff Kirsher 	for (; p < end; p++, reg++)
3599c8571daSJeff Kirsher 		*p += readl(reg);
3609c8571daSJeff Kirsher }
3619c8571daSJeff Kirsher 
3629c8571daSJeff Kirsher static int dnet_poll(struct napi_struct *napi, int budget)
3639c8571daSJeff Kirsher {
3649c8571daSJeff Kirsher 	struct dnet *bp = container_of(napi, struct dnet, napi);
3659c8571daSJeff Kirsher 	struct net_device *dev = bp->dev;
3669c8571daSJeff Kirsher 	int npackets = 0;
3679c8571daSJeff Kirsher 	unsigned int pkt_len;
3689c8571daSJeff Kirsher 	struct sk_buff *skb;
3699c8571daSJeff Kirsher 	unsigned int *data_ptr;
3709c8571daSJeff Kirsher 	u32 int_enable;
3719c8571daSJeff Kirsher 	u32 cmd_word;
3729c8571daSJeff Kirsher 	int i;
3739c8571daSJeff Kirsher 
3749c8571daSJeff Kirsher 	while (npackets < budget) {
3759c8571daSJeff Kirsher 		/*
3769c8571daSJeff Kirsher 		 * break out of while loop if there are no more
3779c8571daSJeff Kirsher 		 * packets waiting
3789c8571daSJeff Kirsher 		 */
37912d80ac4SEric Dumazet 		if (!(dnet_readl(bp, RX_FIFO_WCNT) >> 16))
38012d80ac4SEric Dumazet 			break;
3819c8571daSJeff Kirsher 
3829c8571daSJeff Kirsher 		cmd_word = dnet_readl(bp, RX_LEN_FIFO);
3839c8571daSJeff Kirsher 		pkt_len = cmd_word & 0xFFFF;
3849c8571daSJeff Kirsher 
3859c8571daSJeff Kirsher 		if (cmd_word & 0xDF180000)
3869c8571daSJeff Kirsher 			printk(KERN_ERR "%s packet receive error %x\n",
3879c8571daSJeff Kirsher 			       __func__, cmd_word);
3889c8571daSJeff Kirsher 
38921a4e469SPradeep A Dalvi 		skb = netdev_alloc_skb(dev, pkt_len + 5);
3909c8571daSJeff Kirsher 		if (skb != NULL) {
3919c8571daSJeff Kirsher 			/* Align IP on 16 byte boundaries */
3929c8571daSJeff Kirsher 			skb_reserve(skb, 2);
3939c8571daSJeff Kirsher 			/*
3949c8571daSJeff Kirsher 			 * 'skb_put()' points to the start of sk_buff
3959c8571daSJeff Kirsher 			 * data area.
3969c8571daSJeff Kirsher 			 */
3974df864c1SJohannes Berg 			data_ptr = skb_put(skb, pkt_len);
3989c8571daSJeff Kirsher 			for (i = 0; i < (pkt_len + 3) >> 2; i++)
3999c8571daSJeff Kirsher 				*data_ptr++ = dnet_readl(bp, RX_DATA_FIFO);
4009c8571daSJeff Kirsher 			skb->protocol = eth_type_trans(skb, dev);
4019c8571daSJeff Kirsher 			netif_receive_skb(skb);
4029c8571daSJeff Kirsher 			npackets++;
4039c8571daSJeff Kirsher 		} else
4049c8571daSJeff Kirsher 			printk(KERN_NOTICE
4059c8571daSJeff Kirsher 			       "%s: No memory to allocate a sk_buff of "
4069c8571daSJeff Kirsher 			       "size %u.\n", dev->name, pkt_len);
4079c8571daSJeff Kirsher 	}
4089c8571daSJeff Kirsher 
4099c8571daSJeff Kirsher 	if (npackets < budget) {
4109c8571daSJeff Kirsher 		/* We processed all packets available.  Tell NAPI it can
41112d80ac4SEric Dumazet 		 * stop polling then re-enable rx interrupts.
41212d80ac4SEric Dumazet 		 */
4136ad20165SEric Dumazet 		napi_complete_done(napi, npackets);
4149c8571daSJeff Kirsher 		int_enable = dnet_readl(bp, INTR_ENB);
4159c8571daSJeff Kirsher 		int_enable |= DNET_INTR_SRC_RX_CMDFIFOAF;
4169c8571daSJeff Kirsher 		dnet_writel(bp, int_enable, INTR_ENB);
4179c8571daSJeff Kirsher 	}
4189c8571daSJeff Kirsher 
41912d80ac4SEric Dumazet 	return npackets;
4209c8571daSJeff Kirsher }
4219c8571daSJeff Kirsher 
4229c8571daSJeff Kirsher static irqreturn_t dnet_interrupt(int irq, void *dev_id)
4239c8571daSJeff Kirsher {
4249c8571daSJeff Kirsher 	struct net_device *dev = dev_id;
4259c8571daSJeff Kirsher 	struct dnet *bp = netdev_priv(dev);
4269c8571daSJeff Kirsher 	u32 int_src, int_enable, int_current;
4279c8571daSJeff Kirsher 	unsigned long flags;
4289c8571daSJeff Kirsher 	unsigned int handled = 0;
4299c8571daSJeff Kirsher 
4309c8571daSJeff Kirsher 	spin_lock_irqsave(&bp->lock, flags);
4319c8571daSJeff Kirsher 
4329c8571daSJeff Kirsher 	/* read and clear the DNET irq (clear on read) */
4339c8571daSJeff Kirsher 	int_src = dnet_readl(bp, INTR_SRC);
4349c8571daSJeff Kirsher 	int_enable = dnet_readl(bp, INTR_ENB);
4359c8571daSJeff Kirsher 	int_current = int_src & int_enable;
4369c8571daSJeff Kirsher 
4379c8571daSJeff Kirsher 	/* restart the queue if we had stopped it for TX fifo almost full */
4389c8571daSJeff Kirsher 	if (int_current & DNET_INTR_SRC_TX_FIFOAE) {
4399c8571daSJeff Kirsher 		int_enable = dnet_readl(bp, INTR_ENB);
4409c8571daSJeff Kirsher 		int_enable &= ~DNET_INTR_ENB_TX_FIFOAE;
4419c8571daSJeff Kirsher 		dnet_writel(bp, int_enable, INTR_ENB);
4429c8571daSJeff Kirsher 		netif_wake_queue(dev);
4439c8571daSJeff Kirsher 		handled = 1;
4449c8571daSJeff Kirsher 	}
4459c8571daSJeff Kirsher 
4469c8571daSJeff Kirsher 	/* RX FIFO error checking */
4479c8571daSJeff Kirsher 	if (int_current &
4489c8571daSJeff Kirsher 	    (DNET_INTR_SRC_RX_CMDFIFOFF | DNET_INTR_SRC_RX_DATAFIFOFF)) {
4499c8571daSJeff Kirsher 		printk(KERN_ERR "%s: RX fifo error %x, irq %x\n", __func__,
4509c8571daSJeff Kirsher 		       dnet_readl(bp, RX_STATUS), int_current);
4519c8571daSJeff Kirsher 		/* we can only flush the RX FIFOs */
4529c8571daSJeff Kirsher 		dnet_writel(bp, DNET_SYS_CTL_RXFIFOFLUSH, SYS_CTL);
4539c8571daSJeff Kirsher 		ndelay(500);
4549c8571daSJeff Kirsher 		dnet_writel(bp, 0, SYS_CTL);
4559c8571daSJeff Kirsher 		handled = 1;
4569c8571daSJeff Kirsher 	}
4579c8571daSJeff Kirsher 
4589c8571daSJeff Kirsher 	/* TX FIFO error checking */
4599c8571daSJeff Kirsher 	if (int_current &
4609c8571daSJeff Kirsher 	    (DNET_INTR_SRC_TX_FIFOFULL | DNET_INTR_SRC_TX_DISCFRM)) {
4619c8571daSJeff Kirsher 		printk(KERN_ERR "%s: TX fifo error %x, irq %x\n", __func__,
4629c8571daSJeff Kirsher 		       dnet_readl(bp, TX_STATUS), int_current);
4639c8571daSJeff Kirsher 		/* we can only flush the TX FIFOs */
4649c8571daSJeff Kirsher 		dnet_writel(bp, DNET_SYS_CTL_TXFIFOFLUSH, SYS_CTL);
4659c8571daSJeff Kirsher 		ndelay(500);
4669c8571daSJeff Kirsher 		dnet_writel(bp, 0, SYS_CTL);
4679c8571daSJeff Kirsher 		handled = 1;
4689c8571daSJeff Kirsher 	}
4699c8571daSJeff Kirsher 
4709c8571daSJeff Kirsher 	if (int_current & DNET_INTR_SRC_RX_CMDFIFOAF) {
4719c8571daSJeff Kirsher 		if (napi_schedule_prep(&bp->napi)) {
4729c8571daSJeff Kirsher 			/*
4739c8571daSJeff Kirsher 			 * There's no point taking any more interrupts
4749c8571daSJeff Kirsher 			 * until we have processed the buffers
4759c8571daSJeff Kirsher 			 */
4769c8571daSJeff Kirsher 			/* Disable Rx interrupts and schedule NAPI poll */
4779c8571daSJeff Kirsher 			int_enable = dnet_readl(bp, INTR_ENB);
4789c8571daSJeff Kirsher 			int_enable &= ~DNET_INTR_SRC_RX_CMDFIFOAF;
4799c8571daSJeff Kirsher 			dnet_writel(bp, int_enable, INTR_ENB);
4809c8571daSJeff Kirsher 			__napi_schedule(&bp->napi);
4819c8571daSJeff Kirsher 		}
4829c8571daSJeff Kirsher 		handled = 1;
4839c8571daSJeff Kirsher 	}
4849c8571daSJeff Kirsher 
4859c8571daSJeff Kirsher 	if (!handled)
4869c8571daSJeff Kirsher 		pr_debug("%s: irq %x remains\n", __func__, int_current);
4879c8571daSJeff Kirsher 
4889c8571daSJeff Kirsher 	spin_unlock_irqrestore(&bp->lock, flags);
4899c8571daSJeff Kirsher 
4909c8571daSJeff Kirsher 	return IRQ_RETVAL(handled);
4919c8571daSJeff Kirsher }
4929c8571daSJeff Kirsher 
4939c8571daSJeff Kirsher #ifdef DEBUG
4949c8571daSJeff Kirsher static inline void dnet_print_skb(struct sk_buff *skb)
4959c8571daSJeff Kirsher {
4969c8571daSJeff Kirsher 	int k;
4979c8571daSJeff Kirsher 	printk(KERN_DEBUG PFX "data:");
4989c8571daSJeff Kirsher 	for (k = 0; k < skb->len; k++)
4999c8571daSJeff Kirsher 		printk(" %02x", (unsigned int)skb->data[k]);
5009c8571daSJeff Kirsher 	printk("\n");
5019c8571daSJeff Kirsher }
5029c8571daSJeff Kirsher #else
5039c8571daSJeff Kirsher #define dnet_print_skb(skb)	do {} while (0)
5049c8571daSJeff Kirsher #endif
5059c8571daSJeff Kirsher 
5069c8571daSJeff Kirsher static netdev_tx_t dnet_start_xmit(struct sk_buff *skb, struct net_device *dev)
5079c8571daSJeff Kirsher {
5089c8571daSJeff Kirsher 
5099c8571daSJeff Kirsher 	struct dnet *bp = netdev_priv(dev);
5109c8571daSJeff Kirsher 	u32 tx_status, irq_enable;
5119c8571daSJeff Kirsher 	unsigned int len, i, tx_cmd, wrsz;
5129c8571daSJeff Kirsher 	unsigned long flags;
5139c8571daSJeff Kirsher 	unsigned int *bufp;
5149c8571daSJeff Kirsher 
5159c8571daSJeff Kirsher 	tx_status = dnet_readl(bp, TX_STATUS);
5169c8571daSJeff Kirsher 
5179c8571daSJeff Kirsher 	pr_debug("start_xmit: len %u head %p data %p\n",
5189c8571daSJeff Kirsher 	       skb->len, skb->head, skb->data);
5199c8571daSJeff Kirsher 	dnet_print_skb(skb);
5209c8571daSJeff Kirsher 
5219c8571daSJeff Kirsher 	/* frame size (words) */
5229c8571daSJeff Kirsher 	len = (skb->len + 3) >> 2;
5239c8571daSJeff Kirsher 
5249c8571daSJeff Kirsher 	spin_lock_irqsave(&bp->lock, flags);
5259c8571daSJeff Kirsher 
5269c8571daSJeff Kirsher 	tx_status = dnet_readl(bp, TX_STATUS);
5279c8571daSJeff Kirsher 
5289c8571daSJeff Kirsher 	bufp = (unsigned int *)(((unsigned long) skb->data) & ~0x3UL);
5299c8571daSJeff Kirsher 	wrsz = (u32) skb->len + 3;
5309c8571daSJeff Kirsher 	wrsz += ((unsigned long) skb->data) & 0x3;
5319c8571daSJeff Kirsher 	wrsz >>= 2;
5329c8571daSJeff Kirsher 	tx_cmd = ((((unsigned long)(skb->data)) & 0x03) << 16) | (u32) skb->len;
5339c8571daSJeff Kirsher 
5349c8571daSJeff Kirsher 	/* check if there is enough room for the current frame */
5359c8571daSJeff Kirsher 	if (wrsz < (DNET_FIFO_SIZE - dnet_readl(bp, TX_FIFO_WCNT))) {
5369c8571daSJeff Kirsher 		for (i = 0; i < wrsz; i++)
5379c8571daSJeff Kirsher 			dnet_writel(bp, *bufp++, TX_DATA_FIFO);
5389c8571daSJeff Kirsher 
5399c8571daSJeff Kirsher 		/*
5409c8571daSJeff Kirsher 		 * inform MAC that a packet's written and ready to be
5419c8571daSJeff Kirsher 		 * shipped out
5429c8571daSJeff Kirsher 		 */
5439c8571daSJeff Kirsher 		dnet_writel(bp, tx_cmd, TX_LEN_FIFO);
5449c8571daSJeff Kirsher 	}
5459c8571daSJeff Kirsher 
5469c8571daSJeff Kirsher 	if (dnet_readl(bp, TX_FIFO_WCNT) > DNET_FIFO_TX_DATA_AF_TH) {
5479c8571daSJeff Kirsher 		netif_stop_queue(dev);
5489c8571daSJeff Kirsher 		tx_status = dnet_readl(bp, INTR_SRC);
5499c8571daSJeff Kirsher 		irq_enable = dnet_readl(bp, INTR_ENB);
5509c8571daSJeff Kirsher 		irq_enable |= DNET_INTR_ENB_TX_FIFOAE;
5519c8571daSJeff Kirsher 		dnet_writel(bp, irq_enable, INTR_ENB);
5529c8571daSJeff Kirsher 	}
5539c8571daSJeff Kirsher 
5549c8571daSJeff Kirsher 	skb_tx_timestamp(skb);
5559c8571daSJeff Kirsher 
5569c8571daSJeff Kirsher 	/* free the buffer */
5579c8571daSJeff Kirsher 	dev_kfree_skb(skb);
5589c8571daSJeff Kirsher 
5599c8571daSJeff Kirsher 	spin_unlock_irqrestore(&bp->lock, flags);
5609c8571daSJeff Kirsher 
5619c8571daSJeff Kirsher 	return NETDEV_TX_OK;
5629c8571daSJeff Kirsher }
5639c8571daSJeff Kirsher 
5649c8571daSJeff Kirsher static void dnet_reset_hw(struct dnet *bp)
5659c8571daSJeff Kirsher {
5669c8571daSJeff Kirsher 	/* put ts_mac in IDLE state i.e. disable rx/tx */
5679c8571daSJeff Kirsher 	dnet_writew_mac(bp, DNET_INTERNAL_MODE_REG, DNET_INTERNAL_MODE_FCEN);
5689c8571daSJeff Kirsher 
5699c8571daSJeff Kirsher 	/*
5709c8571daSJeff Kirsher 	 * RX FIFO almost full threshold: only cmd FIFO almost full is
5719c8571daSJeff Kirsher 	 * implemented for RX side
5729c8571daSJeff Kirsher 	 */
5739c8571daSJeff Kirsher 	dnet_writel(bp, DNET_FIFO_RX_CMD_AF_TH, RX_FIFO_TH);
5749c8571daSJeff Kirsher 	/*
5759c8571daSJeff Kirsher 	 * TX FIFO almost empty threshold: only data FIFO almost empty
5769c8571daSJeff Kirsher 	 * is implemented for TX side
5779c8571daSJeff Kirsher 	 */
5789c8571daSJeff Kirsher 	dnet_writel(bp, DNET_FIFO_TX_DATA_AE_TH, TX_FIFO_TH);
5799c8571daSJeff Kirsher 
5809c8571daSJeff Kirsher 	/* flush rx/tx fifos */
5819c8571daSJeff Kirsher 	dnet_writel(bp, DNET_SYS_CTL_RXFIFOFLUSH | DNET_SYS_CTL_TXFIFOFLUSH,
5829c8571daSJeff Kirsher 			SYS_CTL);
5839c8571daSJeff Kirsher 	msleep(1);
5849c8571daSJeff Kirsher 	dnet_writel(bp, 0, SYS_CTL);
5859c8571daSJeff Kirsher }
5869c8571daSJeff Kirsher 
5879c8571daSJeff Kirsher static void dnet_init_hw(struct dnet *bp)
5889c8571daSJeff Kirsher {
5899c8571daSJeff Kirsher 	u32 config;
5909c8571daSJeff Kirsher 
5919c8571daSJeff Kirsher 	dnet_reset_hw(bp);
5929c8571daSJeff Kirsher 	__dnet_set_hwaddr(bp);
5939c8571daSJeff Kirsher 
5949c8571daSJeff Kirsher 	config = dnet_readw_mac(bp, DNET_INTERNAL_RXTX_CONTROL_REG);
5959c8571daSJeff Kirsher 
5969c8571daSJeff Kirsher 	if (bp->dev->flags & IFF_PROMISC)
5979c8571daSJeff Kirsher 		/* Copy All Frames */
5989c8571daSJeff Kirsher 		config |= DNET_INTERNAL_RXTX_CONTROL_ENPROMISC;
5999c8571daSJeff Kirsher 	if (!(bp->dev->flags & IFF_BROADCAST))
6009c8571daSJeff Kirsher 		/* No BroadCast */
6019c8571daSJeff Kirsher 		config |= DNET_INTERNAL_RXTX_CONTROL_RXMULTICAST;
6029c8571daSJeff Kirsher 
6039c8571daSJeff Kirsher 	config |= DNET_INTERNAL_RXTX_CONTROL_RXPAUSE |
6049c8571daSJeff Kirsher 	    DNET_INTERNAL_RXTX_CONTROL_RXBROADCAST |
6059c8571daSJeff Kirsher 	    DNET_INTERNAL_RXTX_CONTROL_DROPCONTROL |
6069c8571daSJeff Kirsher 	    DNET_INTERNAL_RXTX_CONTROL_DISCFXFCS;
6079c8571daSJeff Kirsher 
6089c8571daSJeff Kirsher 	dnet_writew_mac(bp, DNET_INTERNAL_RXTX_CONTROL_REG, config);
6099c8571daSJeff Kirsher 
6109c8571daSJeff Kirsher 	/* clear irq before enabling them */
6119c8571daSJeff Kirsher 	config = dnet_readl(bp, INTR_SRC);
6129c8571daSJeff Kirsher 
6139c8571daSJeff Kirsher 	/* enable RX/TX interrupt, recv packet ready interrupt */
6149c8571daSJeff Kirsher 	dnet_writel(bp, DNET_INTR_ENB_GLOBAL_ENABLE | DNET_INTR_ENB_RX_SUMMARY |
6159c8571daSJeff Kirsher 			DNET_INTR_ENB_TX_SUMMARY | DNET_INTR_ENB_RX_FIFOERR |
6169c8571daSJeff Kirsher 			DNET_INTR_ENB_RX_ERROR | DNET_INTR_ENB_RX_FIFOFULL |
6179c8571daSJeff Kirsher 			DNET_INTR_ENB_TX_FIFOFULL | DNET_INTR_ENB_TX_DISCFRM |
6189c8571daSJeff Kirsher 			DNET_INTR_ENB_RX_PKTRDY, INTR_ENB);
6199c8571daSJeff Kirsher }
6209c8571daSJeff Kirsher 
6219c8571daSJeff Kirsher static int dnet_open(struct net_device *dev)
6229c8571daSJeff Kirsher {
6239c8571daSJeff Kirsher 	struct dnet *bp = netdev_priv(dev);
6249c8571daSJeff Kirsher 
6259c8571daSJeff Kirsher 	/* if the phy is not yet register, retry later */
626958974fdSPhilippe Reynes 	if (!dev->phydev)
6279c8571daSJeff Kirsher 		return -EAGAIN;
6289c8571daSJeff Kirsher 
6299c8571daSJeff Kirsher 	napi_enable(&bp->napi);
6309c8571daSJeff Kirsher 	dnet_init_hw(bp);
6319c8571daSJeff Kirsher 
632958974fdSPhilippe Reynes 	phy_start_aneg(dev->phydev);
6339c8571daSJeff Kirsher 
6349c8571daSJeff Kirsher 	/* schedule a link state check */
635958974fdSPhilippe Reynes 	phy_start(dev->phydev);
6369c8571daSJeff Kirsher 
6379c8571daSJeff Kirsher 	netif_start_queue(dev);
6389c8571daSJeff Kirsher 
6399c8571daSJeff Kirsher 	return 0;
6409c8571daSJeff Kirsher }
6419c8571daSJeff Kirsher 
6429c8571daSJeff Kirsher static int dnet_close(struct net_device *dev)
6439c8571daSJeff Kirsher {
6449c8571daSJeff Kirsher 	struct dnet *bp = netdev_priv(dev);
6459c8571daSJeff Kirsher 
6469c8571daSJeff Kirsher 	netif_stop_queue(dev);
6479c8571daSJeff Kirsher 	napi_disable(&bp->napi);
6489c8571daSJeff Kirsher 
649958974fdSPhilippe Reynes 	if (dev->phydev)
650958974fdSPhilippe Reynes 		phy_stop(dev->phydev);
6519c8571daSJeff Kirsher 
6529c8571daSJeff Kirsher 	dnet_reset_hw(bp);
6539c8571daSJeff Kirsher 	netif_carrier_off(dev);
6549c8571daSJeff Kirsher 
6559c8571daSJeff Kirsher 	return 0;
6569c8571daSJeff Kirsher }
6579c8571daSJeff Kirsher 
6589c8571daSJeff Kirsher static inline void dnet_print_pretty_hwstats(struct dnet_stats *hwstat)
6599c8571daSJeff Kirsher {
6609c8571daSJeff Kirsher 	pr_debug("%s\n", __func__);
6619c8571daSJeff Kirsher 	pr_debug("----------------------------- RX statistics "
6629c8571daSJeff Kirsher 		 "-------------------------------\n");
6639c8571daSJeff Kirsher 	pr_debug("RX_PKT_IGNR_CNT %-8x\n", hwstat->rx_pkt_ignr);
6649c8571daSJeff Kirsher 	pr_debug("RX_LEN_CHK_ERR_CNT %-8x\n", hwstat->rx_len_chk_err);
6659c8571daSJeff Kirsher 	pr_debug("RX_LNG_FRM_CNT %-8x\n", hwstat->rx_lng_frm);
6669c8571daSJeff Kirsher 	pr_debug("RX_SHRT_FRM_CNT %-8x\n", hwstat->rx_shrt_frm);
6679c8571daSJeff Kirsher 	pr_debug("RX_IPG_VIOL_CNT %-8x\n", hwstat->rx_ipg_viol);
6689c8571daSJeff Kirsher 	pr_debug("RX_CRC_ERR_CNT %-8x\n", hwstat->rx_crc_err);
6699c8571daSJeff Kirsher 	pr_debug("RX_OK_PKT_CNT %-8x\n", hwstat->rx_ok_pkt);
6709c8571daSJeff Kirsher 	pr_debug("RX_CTL_FRM_CNT %-8x\n", hwstat->rx_ctl_frm);
6719c8571daSJeff Kirsher 	pr_debug("RX_PAUSE_FRM_CNT %-8x\n", hwstat->rx_pause_frm);
6729c8571daSJeff Kirsher 	pr_debug("RX_MULTICAST_CNT %-8x\n", hwstat->rx_multicast);
6739c8571daSJeff Kirsher 	pr_debug("RX_BROADCAST_CNT %-8x\n", hwstat->rx_broadcast);
6749c8571daSJeff Kirsher 	pr_debug("RX_VLAN_TAG_CNT %-8x\n", hwstat->rx_vlan_tag);
6759c8571daSJeff Kirsher 	pr_debug("RX_PRE_SHRINK_CNT %-8x\n", hwstat->rx_pre_shrink);
6769c8571daSJeff Kirsher 	pr_debug("RX_DRIB_NIB_CNT %-8x\n", hwstat->rx_drib_nib);
6779c8571daSJeff Kirsher 	pr_debug("RX_UNSUP_OPCD_CNT %-8x\n", hwstat->rx_unsup_opcd);
6789c8571daSJeff Kirsher 	pr_debug("RX_BYTE_CNT %-8x\n", hwstat->rx_byte);
6799c8571daSJeff Kirsher 	pr_debug("----------------------------- TX statistics "
6809c8571daSJeff Kirsher 		 "-------------------------------\n");
6819c8571daSJeff Kirsher 	pr_debug("TX_UNICAST_CNT %-8x\n", hwstat->tx_unicast);
6829c8571daSJeff Kirsher 	pr_debug("TX_PAUSE_FRM_CNT %-8x\n", hwstat->tx_pause_frm);
6839c8571daSJeff Kirsher 	pr_debug("TX_MULTICAST_CNT %-8x\n", hwstat->tx_multicast);
6849c8571daSJeff Kirsher 	pr_debug("TX_BRDCAST_CNT %-8x\n", hwstat->tx_brdcast);
6859c8571daSJeff Kirsher 	pr_debug("TX_VLAN_TAG_CNT %-8x\n", hwstat->tx_vlan_tag);
6869c8571daSJeff Kirsher 	pr_debug("TX_BAD_FCS_CNT %-8x\n", hwstat->tx_bad_fcs);
6879c8571daSJeff Kirsher 	pr_debug("TX_JUMBO_CNT %-8x\n", hwstat->tx_jumbo);
6889c8571daSJeff Kirsher 	pr_debug("TX_BYTE_CNT %-8x\n", hwstat->tx_byte);
6899c8571daSJeff Kirsher }
6909c8571daSJeff Kirsher 
6919c8571daSJeff Kirsher static struct net_device_stats *dnet_get_stats(struct net_device *dev)
6929c8571daSJeff Kirsher {
6939c8571daSJeff Kirsher 
6949c8571daSJeff Kirsher 	struct dnet *bp = netdev_priv(dev);
6959c8571daSJeff Kirsher 	struct net_device_stats *nstat = &dev->stats;
6969c8571daSJeff Kirsher 	struct dnet_stats *hwstat = &bp->hw_stats;
6979c8571daSJeff Kirsher 
6989c8571daSJeff Kirsher 	/* read stats from hardware */
6999c8571daSJeff Kirsher 	dnet_update_stats(bp);
7009c8571daSJeff Kirsher 
7019c8571daSJeff Kirsher 	/* Convert HW stats into netdevice stats */
7029c8571daSJeff Kirsher 	nstat->rx_errors = (hwstat->rx_len_chk_err +
7039c8571daSJeff Kirsher 			    hwstat->rx_lng_frm + hwstat->rx_shrt_frm +
7049c8571daSJeff Kirsher 			    /* ignore IGP violation error
7059c8571daSJeff Kirsher 			    hwstat->rx_ipg_viol + */
7069c8571daSJeff Kirsher 			    hwstat->rx_crc_err +
7079c8571daSJeff Kirsher 			    hwstat->rx_pre_shrink +
7089c8571daSJeff Kirsher 			    hwstat->rx_drib_nib + hwstat->rx_unsup_opcd);
7099c8571daSJeff Kirsher 	nstat->tx_errors = hwstat->tx_bad_fcs;
7109c8571daSJeff Kirsher 	nstat->rx_length_errors = (hwstat->rx_len_chk_err +
7119c8571daSJeff Kirsher 				   hwstat->rx_lng_frm +
7129c8571daSJeff Kirsher 				   hwstat->rx_shrt_frm + hwstat->rx_pre_shrink);
7139c8571daSJeff Kirsher 	nstat->rx_crc_errors = hwstat->rx_crc_err;
7149c8571daSJeff Kirsher 	nstat->rx_frame_errors = hwstat->rx_pre_shrink + hwstat->rx_drib_nib;
7159c8571daSJeff Kirsher 	nstat->rx_packets = hwstat->rx_ok_pkt;
7169c8571daSJeff Kirsher 	nstat->tx_packets = (hwstat->tx_unicast +
7179c8571daSJeff Kirsher 			     hwstat->tx_multicast + hwstat->tx_brdcast);
7189c8571daSJeff Kirsher 	nstat->rx_bytes = hwstat->rx_byte;
7199c8571daSJeff Kirsher 	nstat->tx_bytes = hwstat->tx_byte;
7209c8571daSJeff Kirsher 	nstat->multicast = hwstat->rx_multicast;
7219c8571daSJeff Kirsher 	nstat->rx_missed_errors = hwstat->rx_pkt_ignr;
7229c8571daSJeff Kirsher 
7239c8571daSJeff Kirsher 	dnet_print_pretty_hwstats(hwstat);
7249c8571daSJeff Kirsher 
7259c8571daSJeff Kirsher 	return nstat;
7269c8571daSJeff Kirsher }
7279c8571daSJeff Kirsher 
7289c8571daSJeff Kirsher static void dnet_get_drvinfo(struct net_device *dev,
7299c8571daSJeff Kirsher 			     struct ethtool_drvinfo *info)
7309c8571daSJeff Kirsher {
73168aad78cSRick Jones 	strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
73268aad78cSRick Jones 	strlcpy(info->bus_info, "0", sizeof(info->bus_info));
7339c8571daSJeff Kirsher }
7349c8571daSJeff Kirsher 
7359c8571daSJeff Kirsher static const struct ethtool_ops dnet_ethtool_ops = {
7369c8571daSJeff Kirsher 	.get_drvinfo		= dnet_get_drvinfo,
7379c8571daSJeff Kirsher 	.get_link		= ethtool_op_get_link,
738eb774cbeSRichard Cochran 	.get_ts_info		= ethtool_op_get_ts_info,
7391ba44a1fSPhilippe Reynes 	.get_link_ksettings     = phy_ethtool_get_link_ksettings,
7401ba44a1fSPhilippe Reynes 	.set_link_ksettings     = phy_ethtool_set_link_ksettings,
7419c8571daSJeff Kirsher };
7429c8571daSJeff Kirsher 
7439c8571daSJeff Kirsher static const struct net_device_ops dnet_netdev_ops = {
7449c8571daSJeff Kirsher 	.ndo_open		= dnet_open,
7459c8571daSJeff Kirsher 	.ndo_stop		= dnet_close,
7469c8571daSJeff Kirsher 	.ndo_get_stats		= dnet_get_stats,
7479c8571daSJeff Kirsher 	.ndo_start_xmit		= dnet_start_xmit,
748fd786fb1SHeiner Kallweit 	.ndo_do_ioctl		= phy_do_ioctl_running,
7499c8571daSJeff Kirsher 	.ndo_set_mac_address	= eth_mac_addr,
7509c8571daSJeff Kirsher 	.ndo_validate_addr	= eth_validate_addr,
7519c8571daSJeff Kirsher };
7529c8571daSJeff Kirsher 
753a0a4efedSBill Pemberton static int dnet_probe(struct platform_device *pdev)
7549c8571daSJeff Kirsher {
7559c8571daSJeff Kirsher 	struct resource *res;
7569c8571daSJeff Kirsher 	struct net_device *dev;
7579c8571daSJeff Kirsher 	struct dnet *bp;
7589c8571daSJeff Kirsher 	struct phy_device *phydev;
759ae29223eSHimangi Saraogi 	int err;
760ae29223eSHimangi Saraogi 	unsigned int irq;
7619c8571daSJeff Kirsher 
7629c8571daSJeff Kirsher 	irq = platform_get_irq(pdev, 0);
7639c8571daSJeff Kirsher 
7649c8571daSJeff Kirsher 	dev = alloc_etherdev(sizeof(*bp));
76541de8d4cSJoe Perches 	if (!dev)
766ae29223eSHimangi Saraogi 		return -ENOMEM;
7679c8571daSJeff Kirsher 
7689c8571daSJeff Kirsher 	/* TODO: Actually, we have some interesting features... */
7699c8571daSJeff Kirsher 	dev->features |= 0;
7709c8571daSJeff Kirsher 
7719c8571daSJeff Kirsher 	bp = netdev_priv(dev);
7729c8571daSJeff Kirsher 	bp->dev = dev;
7739c8571daSJeff Kirsher 
7749c8571daSJeff Kirsher 	platform_set_drvdata(pdev, dev);
7759c8571daSJeff Kirsher 	SET_NETDEV_DEV(dev, &pdev->dev);
7769c8571daSJeff Kirsher 
7779c8571daSJeff Kirsher 	spin_lock_init(&bp->lock);
7789c8571daSJeff Kirsher 
77992efe48eSDejin Zheng 	bp->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
780ae29223eSHimangi Saraogi 	if (IS_ERR(bp->regs)) {
781ae29223eSHimangi Saraogi 		err = PTR_ERR(bp->regs);
7829c8571daSJeff Kirsher 		goto err_out_free_dev;
7839c8571daSJeff Kirsher 	}
7849c8571daSJeff Kirsher 
7859c8571daSJeff Kirsher 	dev->irq = irq;
7869c8571daSJeff Kirsher 	err = request_irq(dev->irq, dnet_interrupt, 0, DRV_NAME, dev);
7879c8571daSJeff Kirsher 	if (err) {
7889c8571daSJeff Kirsher 		dev_err(&pdev->dev, "Unable to request IRQ %d (error %d)\n",
7899c8571daSJeff Kirsher 		       irq, err);
790ae29223eSHimangi Saraogi 		goto err_out_free_dev;
7919c8571daSJeff Kirsher 	}
7929c8571daSJeff Kirsher 
7939c8571daSJeff Kirsher 	dev->netdev_ops = &dnet_netdev_ops;
7949c8571daSJeff Kirsher 	netif_napi_add(dev, &bp->napi, dnet_poll, 64);
7959c8571daSJeff Kirsher 	dev->ethtool_ops = &dnet_ethtool_ops;
7969c8571daSJeff Kirsher 
7979c8571daSJeff Kirsher 	dev->base_addr = (unsigned long)bp->regs;
7989c8571daSJeff Kirsher 
7999c8571daSJeff Kirsher 	bp->capabilities = dnet_readl(bp, VERCAPS) & DNET_CAPS_MASK;
8009c8571daSJeff Kirsher 
8019c8571daSJeff Kirsher 	dnet_get_hwaddr(bp);
8029c8571daSJeff Kirsher 
8039c8571daSJeff Kirsher 	if (!is_valid_ether_addr(dev->dev_addr)) {
8049c8571daSJeff Kirsher 		/* choose a random ethernet address */
805f2cedb63SDanny Kukawka 		eth_hw_addr_random(dev);
8069c8571daSJeff Kirsher 		__dnet_set_hwaddr(bp);
8079c8571daSJeff Kirsher 	}
8089c8571daSJeff Kirsher 
8099c8571daSJeff Kirsher 	err = register_netdev(dev);
8109c8571daSJeff Kirsher 	if (err) {
8119c8571daSJeff Kirsher 		dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
8129c8571daSJeff Kirsher 		goto err_out_free_irq;
8139c8571daSJeff Kirsher 	}
8149c8571daSJeff Kirsher 
8159c8571daSJeff Kirsher 	/* register the PHY board fixup (for Marvell 88E1111) */
8169c8571daSJeff Kirsher 	err = phy_register_fixup_for_uid(0x01410cc0, 0xfffffff0,
8179c8571daSJeff Kirsher 					 dnet_phy_marvell_fixup);
8189c8571daSJeff Kirsher 	/* we can live without it, so just issue a warning */
8199c8571daSJeff Kirsher 	if (err)
8209c8571daSJeff Kirsher 		dev_warn(&pdev->dev, "Cannot register PHY board fixup.\n");
8219c8571daSJeff Kirsher 
8229c8571daSJeff Kirsher 	err = dnet_mii_init(bp);
8239c8571daSJeff Kirsher 	if (err)
8249c8571daSJeff Kirsher 		goto err_out_unregister_netdev;
8259c8571daSJeff Kirsher 
8269c8571daSJeff Kirsher 	dev_info(&pdev->dev, "Dave DNET at 0x%p (0x%08x) irq %d %pM\n",
827ae29223eSHimangi Saraogi 	       bp->regs, (unsigned int)res->start, dev->irq, dev->dev_addr);
8289c8571daSJeff Kirsher 	dev_info(&pdev->dev, "has %smdio, %sirq, %sgigabit, %sdma\n",
8299c8571daSJeff Kirsher 	       (bp->capabilities & DNET_HAS_MDIO) ? "" : "no ",
8309c8571daSJeff Kirsher 	       (bp->capabilities & DNET_HAS_IRQ) ? "" : "no ",
8319c8571daSJeff Kirsher 	       (bp->capabilities & DNET_HAS_GIGABIT) ? "" : "no ",
8329c8571daSJeff Kirsher 	       (bp->capabilities & DNET_HAS_DMA) ? "" : "no ");
833958974fdSPhilippe Reynes 	phydev = dev->phydev;
8342220943aSAndrew Lunn 	phy_attached_info(phydev);
8359c8571daSJeff Kirsher 
8369c8571daSJeff Kirsher 	return 0;
8379c8571daSJeff Kirsher 
8389c8571daSJeff Kirsher err_out_unregister_netdev:
8399c8571daSJeff Kirsher 	unregister_netdev(dev);
8409c8571daSJeff Kirsher err_out_free_irq:
8419c8571daSJeff Kirsher 	free_irq(dev->irq, dev);
8429c8571daSJeff Kirsher err_out_free_dev:
8439c8571daSJeff Kirsher 	free_netdev(dev);
8449c8571daSJeff Kirsher 	return err;
8459c8571daSJeff Kirsher }
8469c8571daSJeff Kirsher 
847a0a4efedSBill Pemberton static int dnet_remove(struct platform_device *pdev)
8489c8571daSJeff Kirsher {
8499c8571daSJeff Kirsher 
8509c8571daSJeff Kirsher 	struct net_device *dev;
8519c8571daSJeff Kirsher 	struct dnet *bp;
8529c8571daSJeff Kirsher 
8539c8571daSJeff Kirsher 	dev = platform_get_drvdata(pdev);
8549c8571daSJeff Kirsher 
8559c8571daSJeff Kirsher 	if (dev) {
8569c8571daSJeff Kirsher 		bp = netdev_priv(dev);
857958974fdSPhilippe Reynes 		if (dev->phydev)
858958974fdSPhilippe Reynes 			phy_disconnect(dev->phydev);
8599c8571daSJeff Kirsher 		mdiobus_unregister(bp->mii_bus);
8609c8571daSJeff Kirsher 		mdiobus_free(bp->mii_bus);
8619c8571daSJeff Kirsher 		unregister_netdev(dev);
8629c8571daSJeff Kirsher 		free_irq(dev->irq, dev);
8639c8571daSJeff Kirsher 		free_netdev(dev);
8649c8571daSJeff Kirsher 	}
8659c8571daSJeff Kirsher 
8669c8571daSJeff Kirsher 	return 0;
8679c8571daSJeff Kirsher }
8689c8571daSJeff Kirsher 
8699c8571daSJeff Kirsher static struct platform_driver dnet_driver = {
8709c8571daSJeff Kirsher 	.probe		= dnet_probe,
871a0a4efedSBill Pemberton 	.remove		= dnet_remove,
8729c8571daSJeff Kirsher 	.driver		= {
8739c8571daSJeff Kirsher 		.name		= "dnet",
8749c8571daSJeff Kirsher 	},
8759c8571daSJeff Kirsher };
8769c8571daSJeff Kirsher 
877db62f684SAxel Lin module_platform_driver(dnet_driver);
8789c8571daSJeff Kirsher 
8799c8571daSJeff Kirsher MODULE_LICENSE("GPL");
8809c8571daSJeff Kirsher MODULE_DESCRIPTION("Dave DNET Ethernet driver");
8819c8571daSJeff Kirsher MODULE_AUTHOR("Ilya Yanok <yanok@emcraft.com>, "
8829c8571daSJeff Kirsher 	      "Matteo Vit <matteo.vit@dave.eu>");
883