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