1d7058a79SJeff Kirsher /* 2d7058a79SJeff Kirsher * Davicom DM9000 Fast Ethernet driver for Linux. 3d7058a79SJeff Kirsher * Copyright (C) 1997 Sten Wang 4d7058a79SJeff Kirsher * 5d7058a79SJeff Kirsher * This program is free software; you can redistribute it and/or 6d7058a79SJeff Kirsher * modify it under the terms of the GNU General Public License 7d7058a79SJeff Kirsher * as published by the Free Software Foundation; either version 2 8d7058a79SJeff Kirsher * of the License, or (at your option) any later version. 9d7058a79SJeff Kirsher * 10d7058a79SJeff Kirsher * This program is distributed in the hope that it will be useful, 11d7058a79SJeff Kirsher * but WITHOUT ANY WARRANTY; without even the implied warranty of 12d7058a79SJeff Kirsher * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13d7058a79SJeff Kirsher * GNU General Public License for more details. 14d7058a79SJeff Kirsher * 15d7058a79SJeff Kirsher * (C) Copyright 1997-1998 DAVICOM Semiconductor,Inc. All Rights Reserved. 16d7058a79SJeff Kirsher * 17d7058a79SJeff Kirsher * Additional updates, Copyright: 18d7058a79SJeff Kirsher * Ben Dooks <ben@simtec.co.uk> 19d7058a79SJeff Kirsher * Sascha Hauer <s.hauer@pengutronix.de> 20d7058a79SJeff Kirsher */ 21d7058a79SJeff Kirsher 22d7058a79SJeff Kirsher #include <linux/module.h> 23d7058a79SJeff Kirsher #include <linux/ioport.h> 24d7058a79SJeff Kirsher #include <linux/netdevice.h> 25d7058a79SJeff Kirsher #include <linux/etherdevice.h> 26d7058a79SJeff Kirsher #include <linux/interrupt.h> 27d7058a79SJeff Kirsher #include <linux/skbuff.h> 28d7058a79SJeff Kirsher #include <linux/spinlock.h> 29d7058a79SJeff Kirsher #include <linux/crc32.h> 30d7058a79SJeff Kirsher #include <linux/mii.h> 310b8bf1baSTomasz Figa #include <linux/of.h> 320b8bf1baSTomasz Figa #include <linux/of_net.h> 33d7058a79SJeff Kirsher #include <linux/ethtool.h> 34d7058a79SJeff Kirsher #include <linux/dm9000.h> 35d7058a79SJeff Kirsher #include <linux/delay.h> 36d7058a79SJeff Kirsher #include <linux/platform_device.h> 37d7058a79SJeff Kirsher #include <linux/irq.h> 38d7058a79SJeff Kirsher #include <linux/slab.h> 39d7058a79SJeff Kirsher 40d7058a79SJeff Kirsher #include <asm/delay.h> 41d7058a79SJeff Kirsher #include <asm/irq.h> 42d7058a79SJeff Kirsher #include <asm/io.h> 43d7058a79SJeff Kirsher 44d7058a79SJeff Kirsher #include "dm9000.h" 45d7058a79SJeff Kirsher 46d7058a79SJeff Kirsher /* Board/System/Debug information/definition ---------------- */ 47d7058a79SJeff Kirsher 48d7058a79SJeff Kirsher #define DM9000_PHY 0x40 /* PHY address 0x01 */ 49d7058a79SJeff Kirsher 50d7058a79SJeff Kirsher #define CARDNAME "dm9000" 51d7058a79SJeff Kirsher #define DRV_VERSION "1.31" 52d7058a79SJeff Kirsher 53d7058a79SJeff Kirsher /* 54d7058a79SJeff Kirsher * Transmit timeout, default 5 seconds. 55d7058a79SJeff Kirsher */ 56d7058a79SJeff Kirsher static int watchdog = 5000; 57d7058a79SJeff Kirsher module_param(watchdog, int, 0400); 58d7058a79SJeff Kirsher MODULE_PARM_DESC(watchdog, "transmit timeout in milliseconds"); 59d7058a79SJeff Kirsher 602e025c71SVladimir Zapolskiy /* 612e025c71SVladimir Zapolskiy * Debug messages level 622e025c71SVladimir Zapolskiy */ 632e025c71SVladimir Zapolskiy static int debug; 642e025c71SVladimir Zapolskiy module_param(debug, int, 0644); 652e025c71SVladimir Zapolskiy MODULE_PARM_DESC(debug, "dm9000 debug level (0-4)"); 662e025c71SVladimir Zapolskiy 67d7058a79SJeff Kirsher /* DM9000 register address locking. 68d7058a79SJeff Kirsher * 69d7058a79SJeff Kirsher * The DM9000 uses an address register to control where data written 70d7058a79SJeff Kirsher * to the data register goes. This means that the address register 71d7058a79SJeff Kirsher * must be preserved over interrupts or similar calls. 72d7058a79SJeff Kirsher * 73d7058a79SJeff Kirsher * During interrupt and other critical calls, a spinlock is used to 74d7058a79SJeff Kirsher * protect the system, but the calls themselves save the address 75d7058a79SJeff Kirsher * in the address register in case they are interrupting another 76d7058a79SJeff Kirsher * access to the device. 77d7058a79SJeff Kirsher * 78d7058a79SJeff Kirsher * For general accesses a lock is provided so that calls which are 79d7058a79SJeff Kirsher * allowed to sleep are serialised so that the address register does 80d7058a79SJeff Kirsher * not need to be saved. This lock also serves to serialise access 81d7058a79SJeff Kirsher * to the EEPROM and PHY access registers which are shared between 82d7058a79SJeff Kirsher * these two devices. 83d7058a79SJeff Kirsher */ 84d7058a79SJeff Kirsher 85d7058a79SJeff Kirsher /* The driver supports the original DM9000E, and now the two newer 86d7058a79SJeff Kirsher * devices, DM9000A and DM9000B. 87d7058a79SJeff Kirsher */ 88d7058a79SJeff Kirsher 89d7058a79SJeff Kirsher enum dm9000_type { 90d7058a79SJeff Kirsher TYPE_DM9000E, /* original DM9000 */ 91d7058a79SJeff Kirsher TYPE_DM9000A, 92d7058a79SJeff Kirsher TYPE_DM9000B 93d7058a79SJeff Kirsher }; 94d7058a79SJeff Kirsher 95d7058a79SJeff Kirsher /* Structure/enum declaration ------------------------------- */ 96*2b162928SHimangi Saraogi struct board_info { 97d7058a79SJeff Kirsher 98d7058a79SJeff Kirsher void __iomem *io_addr; /* Register I/O base address */ 99d7058a79SJeff Kirsher void __iomem *io_data; /* Data I/O address */ 100d7058a79SJeff Kirsher u16 irq; /* IRQ */ 101d7058a79SJeff Kirsher 102d7058a79SJeff Kirsher u16 tx_pkt_cnt; 103d7058a79SJeff Kirsher u16 queue_pkt_len; 104d7058a79SJeff Kirsher u16 queue_start_addr; 105d7058a79SJeff Kirsher u16 queue_ip_summed; 106d7058a79SJeff Kirsher u16 dbug_cnt; 107d7058a79SJeff Kirsher u8 io_mode; /* 0:word, 2:byte */ 108d7058a79SJeff Kirsher u8 phy_addr; 109d7058a79SJeff Kirsher u8 imr_all; 110d7058a79SJeff Kirsher 111d7058a79SJeff Kirsher unsigned int flags; 11258237983SAndrew Ruder unsigned int in_timeout:1; 113d7058a79SJeff Kirsher unsigned int in_suspend:1; 114d7058a79SJeff Kirsher unsigned int wake_supported:1; 115d7058a79SJeff Kirsher 116d7058a79SJeff Kirsher enum dm9000_type type; 117d7058a79SJeff Kirsher 118d7058a79SJeff Kirsher void (*inblk)(void __iomem *port, void *data, int length); 119d7058a79SJeff Kirsher void (*outblk)(void __iomem *port, void *data, int length); 120d7058a79SJeff Kirsher void (*dumpblk)(void __iomem *port, int length); 121d7058a79SJeff Kirsher 122d7058a79SJeff Kirsher struct device *dev; /* parent device */ 123d7058a79SJeff Kirsher 124d7058a79SJeff Kirsher struct resource *addr_res; /* resources found */ 125d7058a79SJeff Kirsher struct resource *data_res; 126d7058a79SJeff Kirsher struct resource *addr_req; /* resources requested */ 127d7058a79SJeff Kirsher struct resource *data_req; 128d7058a79SJeff Kirsher struct resource *irq_res; 129d7058a79SJeff Kirsher 130d7058a79SJeff Kirsher int irq_wake; 131d7058a79SJeff Kirsher 132d7058a79SJeff Kirsher struct mutex addr_lock; /* phy and eeprom access lock */ 133d7058a79SJeff Kirsher 134d7058a79SJeff Kirsher struct delayed_work phy_poll; 135d7058a79SJeff Kirsher struct net_device *ndev; 136d7058a79SJeff Kirsher 137d7058a79SJeff Kirsher spinlock_t lock; 138d7058a79SJeff Kirsher 139d7058a79SJeff Kirsher struct mii_if_info mii; 140d7058a79SJeff Kirsher u32 msg_enable; 141d7058a79SJeff Kirsher u32 wake_state; 142d7058a79SJeff Kirsher 143d7058a79SJeff Kirsher int ip_summed; 144*2b162928SHimangi Saraogi }; 145d7058a79SJeff Kirsher 146d7058a79SJeff Kirsher /* debug code */ 147d7058a79SJeff Kirsher 148d7058a79SJeff Kirsher #define dm9000_dbg(db, lev, msg...) do { \ 1492e025c71SVladimir Zapolskiy if ((lev) < debug) { \ 150d7058a79SJeff Kirsher dev_dbg(db->dev, msg); \ 151d7058a79SJeff Kirsher } \ 152d7058a79SJeff Kirsher } while (0) 153d7058a79SJeff Kirsher 154*2b162928SHimangi Saraogi static inline struct board_info *to_dm9000_board(struct net_device *dev) 155d7058a79SJeff Kirsher { 156d7058a79SJeff Kirsher return netdev_priv(dev); 157d7058a79SJeff Kirsher } 158d7058a79SJeff Kirsher 159d7058a79SJeff Kirsher /* DM9000 network board routine ---------------------------- */ 160d7058a79SJeff Kirsher 161d7058a79SJeff Kirsher /* 162d7058a79SJeff Kirsher * Read a byte from I/O port 163d7058a79SJeff Kirsher */ 164d7058a79SJeff Kirsher static u8 165*2b162928SHimangi Saraogi ior(struct board_info *db, int reg) 166d7058a79SJeff Kirsher { 167d7058a79SJeff Kirsher writeb(reg, db->io_addr); 168d7058a79SJeff Kirsher return readb(db->io_data); 169d7058a79SJeff Kirsher } 170d7058a79SJeff Kirsher 171d7058a79SJeff Kirsher /* 172d7058a79SJeff Kirsher * Write a byte to I/O port 173d7058a79SJeff Kirsher */ 174d7058a79SJeff Kirsher 175d7058a79SJeff Kirsher static void 176*2b162928SHimangi Saraogi iow(struct board_info *db, int reg, int value) 177d7058a79SJeff Kirsher { 178d7058a79SJeff Kirsher writeb(reg, db->io_addr); 179d7058a79SJeff Kirsher writeb(value, db->io_data); 180d7058a79SJeff Kirsher } 181d7058a79SJeff Kirsher 18209ee9f87SMichael Abbott static void 183*2b162928SHimangi Saraogi dm9000_reset(struct board_info *db) 18409ee9f87SMichael Abbott { 18509ee9f87SMichael Abbott dev_dbg(db->dev, "resetting device\n"); 18609ee9f87SMichael Abbott 18709ee9f87SMichael Abbott /* Reset DM9000, see DM9000 Application Notes V1.22 Jun 11, 2004 page 29 18809ee9f87SMichael Abbott * The essential point is that we have to do a double reset, and the 18909ee9f87SMichael Abbott * instruction is to set LBK into MAC internal loopback mode. 19009ee9f87SMichael Abbott */ 191751bb6fdSAndrew Ruder iow(db, DM9000_NCR, NCR_RST | NCR_MAC_LBK); 19209ee9f87SMichael Abbott udelay(100); /* Application note says at least 20 us */ 19309ee9f87SMichael Abbott if (ior(db, DM9000_NCR) & 1) 19409ee9f87SMichael Abbott dev_err(db->dev, "dm9000 did not respond to first reset\n"); 19509ee9f87SMichael Abbott 19609ee9f87SMichael Abbott iow(db, DM9000_NCR, 0); 197751bb6fdSAndrew Ruder iow(db, DM9000_NCR, NCR_RST | NCR_MAC_LBK); 19809ee9f87SMichael Abbott udelay(100); 19909ee9f87SMichael Abbott if (ior(db, DM9000_NCR) & 1) 20009ee9f87SMichael Abbott dev_err(db->dev, "dm9000 did not respond to second reset\n"); 20109ee9f87SMichael Abbott } 20209ee9f87SMichael Abbott 203d7058a79SJeff Kirsher /* routines for sending block to chip */ 204d7058a79SJeff Kirsher 205d7058a79SJeff Kirsher static void dm9000_outblk_8bit(void __iomem *reg, void *data, int count) 206d7058a79SJeff Kirsher { 207daadaf6fSMatthew Leach iowrite8_rep(reg, data, count); 208d7058a79SJeff Kirsher } 209d7058a79SJeff Kirsher 210d7058a79SJeff Kirsher static void dm9000_outblk_16bit(void __iomem *reg, void *data, int count) 211d7058a79SJeff Kirsher { 212daadaf6fSMatthew Leach iowrite16_rep(reg, data, (count+1) >> 1); 213d7058a79SJeff Kirsher } 214d7058a79SJeff Kirsher 215d7058a79SJeff Kirsher static void dm9000_outblk_32bit(void __iomem *reg, void *data, int count) 216d7058a79SJeff Kirsher { 217daadaf6fSMatthew Leach iowrite32_rep(reg, data, (count+3) >> 2); 218d7058a79SJeff Kirsher } 219d7058a79SJeff Kirsher 220d7058a79SJeff Kirsher /* input block from chip to memory */ 221d7058a79SJeff Kirsher 222d7058a79SJeff Kirsher static void dm9000_inblk_8bit(void __iomem *reg, void *data, int count) 223d7058a79SJeff Kirsher { 224daadaf6fSMatthew Leach ioread8_rep(reg, data, count); 225d7058a79SJeff Kirsher } 226d7058a79SJeff Kirsher 227d7058a79SJeff Kirsher 228d7058a79SJeff Kirsher static void dm9000_inblk_16bit(void __iomem *reg, void *data, int count) 229d7058a79SJeff Kirsher { 230daadaf6fSMatthew Leach ioread16_rep(reg, data, (count+1) >> 1); 231d7058a79SJeff Kirsher } 232d7058a79SJeff Kirsher 233d7058a79SJeff Kirsher static void dm9000_inblk_32bit(void __iomem *reg, void *data, int count) 234d7058a79SJeff Kirsher { 235daadaf6fSMatthew Leach ioread32_rep(reg, data, (count+3) >> 2); 236d7058a79SJeff Kirsher } 237d7058a79SJeff Kirsher 238d7058a79SJeff Kirsher /* dump block from chip to null */ 239d7058a79SJeff Kirsher 240d7058a79SJeff Kirsher static void dm9000_dumpblk_8bit(void __iomem *reg, int count) 241d7058a79SJeff Kirsher { 242d7058a79SJeff Kirsher int i; 243d7058a79SJeff Kirsher int tmp; 244d7058a79SJeff Kirsher 245d7058a79SJeff Kirsher for (i = 0; i < count; i++) 246d7058a79SJeff Kirsher tmp = readb(reg); 247d7058a79SJeff Kirsher } 248d7058a79SJeff Kirsher 249d7058a79SJeff Kirsher static void dm9000_dumpblk_16bit(void __iomem *reg, int count) 250d7058a79SJeff Kirsher { 251d7058a79SJeff Kirsher int i; 252d7058a79SJeff Kirsher int tmp; 253d7058a79SJeff Kirsher 254d7058a79SJeff Kirsher count = (count + 1) >> 1; 255d7058a79SJeff Kirsher 256d7058a79SJeff Kirsher for (i = 0; i < count; i++) 257d7058a79SJeff Kirsher tmp = readw(reg); 258d7058a79SJeff Kirsher } 259d7058a79SJeff Kirsher 260d7058a79SJeff Kirsher static void dm9000_dumpblk_32bit(void __iomem *reg, int count) 261d7058a79SJeff Kirsher { 262d7058a79SJeff Kirsher int i; 263d7058a79SJeff Kirsher int tmp; 264d7058a79SJeff Kirsher 265d7058a79SJeff Kirsher count = (count + 3) >> 2; 266d7058a79SJeff Kirsher 267d7058a79SJeff Kirsher for (i = 0; i < count; i++) 268d7058a79SJeff Kirsher tmp = readl(reg); 269d7058a79SJeff Kirsher } 270d7058a79SJeff Kirsher 2716741f40dSJoseph CHANG /* 2726741f40dSJoseph CHANG * Sleep, either by using msleep() or if we are suspending, then 2736741f40dSJoseph CHANG * use mdelay() to sleep. 2746741f40dSJoseph CHANG */ 275*2b162928SHimangi Saraogi static void dm9000_msleep(struct board_info *db, unsigned int ms) 2766741f40dSJoseph CHANG { 27758237983SAndrew Ruder if (db->in_suspend || db->in_timeout) 2786741f40dSJoseph CHANG mdelay(ms); 2796741f40dSJoseph CHANG else 2806741f40dSJoseph CHANG msleep(ms); 2816741f40dSJoseph CHANG } 2826741f40dSJoseph CHANG 2836741f40dSJoseph CHANG /* Read a word from phyxcer */ 2846741f40dSJoseph CHANG static int 2856741f40dSJoseph CHANG dm9000_phy_read(struct net_device *dev, int phy_reg_unused, int reg) 2866741f40dSJoseph CHANG { 287*2b162928SHimangi Saraogi struct board_info *db = netdev_priv(dev); 2886741f40dSJoseph CHANG unsigned long flags; 2896741f40dSJoseph CHANG unsigned int reg_save; 2906741f40dSJoseph CHANG int ret; 2916741f40dSJoseph CHANG 2926741f40dSJoseph CHANG mutex_lock(&db->addr_lock); 2936741f40dSJoseph CHANG 2946741f40dSJoseph CHANG spin_lock_irqsave(&db->lock, flags); 2956741f40dSJoseph CHANG 2966741f40dSJoseph CHANG /* Save previous register address */ 2976741f40dSJoseph CHANG reg_save = readb(db->io_addr); 2986741f40dSJoseph CHANG 2996741f40dSJoseph CHANG /* Fill the phyxcer register into REG_0C */ 3006741f40dSJoseph CHANG iow(db, DM9000_EPAR, DM9000_PHY | reg); 3016741f40dSJoseph CHANG 3026741f40dSJoseph CHANG /* Issue phyxcer read command */ 3036741f40dSJoseph CHANG iow(db, DM9000_EPCR, EPCR_ERPRR | EPCR_EPOS); 3046741f40dSJoseph CHANG 3056741f40dSJoseph CHANG writeb(reg_save, db->io_addr); 3066741f40dSJoseph CHANG spin_unlock_irqrestore(&db->lock, flags); 3076741f40dSJoseph CHANG 3086741f40dSJoseph CHANG dm9000_msleep(db, 1); /* Wait read complete */ 3096741f40dSJoseph CHANG 3106741f40dSJoseph CHANG spin_lock_irqsave(&db->lock, flags); 3116741f40dSJoseph CHANG reg_save = readb(db->io_addr); 3126741f40dSJoseph CHANG 3136741f40dSJoseph CHANG iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer read command */ 3146741f40dSJoseph CHANG 3156741f40dSJoseph CHANG /* The read data keeps on REG_0D & REG_0E */ 3166741f40dSJoseph CHANG ret = (ior(db, DM9000_EPDRH) << 8) | ior(db, DM9000_EPDRL); 3176741f40dSJoseph CHANG 3186741f40dSJoseph CHANG /* restore the previous address */ 3196741f40dSJoseph CHANG writeb(reg_save, db->io_addr); 3206741f40dSJoseph CHANG spin_unlock_irqrestore(&db->lock, flags); 3216741f40dSJoseph CHANG 3226741f40dSJoseph CHANG mutex_unlock(&db->addr_lock); 3236741f40dSJoseph CHANG 3246741f40dSJoseph CHANG dm9000_dbg(db, 5, "phy_read[%02x] -> %04x\n", reg, ret); 3256741f40dSJoseph CHANG return ret; 3266741f40dSJoseph CHANG } 3276741f40dSJoseph CHANG 3286741f40dSJoseph CHANG /* Write a word to phyxcer */ 3296741f40dSJoseph CHANG static void 3306741f40dSJoseph CHANG dm9000_phy_write(struct net_device *dev, 3316741f40dSJoseph CHANG int phyaddr_unused, int reg, int value) 3326741f40dSJoseph CHANG { 333*2b162928SHimangi Saraogi struct board_info *db = netdev_priv(dev); 3346741f40dSJoseph CHANG unsigned long flags; 3356741f40dSJoseph CHANG unsigned long reg_save; 3366741f40dSJoseph CHANG 3376741f40dSJoseph CHANG dm9000_dbg(db, 5, "phy_write[%02x] = %04x\n", reg, value); 33858237983SAndrew Ruder if (!db->in_timeout) 3396741f40dSJoseph CHANG mutex_lock(&db->addr_lock); 3406741f40dSJoseph CHANG 3416741f40dSJoseph CHANG spin_lock_irqsave(&db->lock, flags); 3426741f40dSJoseph CHANG 3436741f40dSJoseph CHANG /* Save previous register address */ 3446741f40dSJoseph CHANG reg_save = readb(db->io_addr); 3456741f40dSJoseph CHANG 3466741f40dSJoseph CHANG /* Fill the phyxcer register into REG_0C */ 3476741f40dSJoseph CHANG iow(db, DM9000_EPAR, DM9000_PHY | reg); 3486741f40dSJoseph CHANG 3496741f40dSJoseph CHANG /* Fill the written data into REG_0D & REG_0E */ 3506741f40dSJoseph CHANG iow(db, DM9000_EPDRL, value); 3516741f40dSJoseph CHANG iow(db, DM9000_EPDRH, value >> 8); 3526741f40dSJoseph CHANG 3536741f40dSJoseph CHANG /* Issue phyxcer write command */ 3546741f40dSJoseph CHANG iow(db, DM9000_EPCR, EPCR_EPOS | EPCR_ERPRW); 3556741f40dSJoseph CHANG 3566741f40dSJoseph CHANG writeb(reg_save, db->io_addr); 3576741f40dSJoseph CHANG spin_unlock_irqrestore(&db->lock, flags); 3586741f40dSJoseph CHANG 3596741f40dSJoseph CHANG dm9000_msleep(db, 1); /* Wait write complete */ 3606741f40dSJoseph CHANG 3616741f40dSJoseph CHANG spin_lock_irqsave(&db->lock, flags); 3626741f40dSJoseph CHANG reg_save = readb(db->io_addr); 3636741f40dSJoseph CHANG 3646741f40dSJoseph CHANG iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer write command */ 3656741f40dSJoseph CHANG 3666741f40dSJoseph CHANG /* restore the previous address */ 3676741f40dSJoseph CHANG writeb(reg_save, db->io_addr); 3686741f40dSJoseph CHANG 3696741f40dSJoseph CHANG spin_unlock_irqrestore(&db->lock, flags); 37058237983SAndrew Ruder if (!db->in_timeout) 3716741f40dSJoseph CHANG mutex_unlock(&db->addr_lock); 3726741f40dSJoseph CHANG } 3736741f40dSJoseph CHANG 374d7058a79SJeff Kirsher /* dm9000_set_io 375d7058a79SJeff Kirsher * 376d7058a79SJeff Kirsher * select the specified set of io routines to use with the 377d7058a79SJeff Kirsher * device 378d7058a79SJeff Kirsher */ 379d7058a79SJeff Kirsher 380d7058a79SJeff Kirsher static void dm9000_set_io(struct board_info *db, int byte_width) 381d7058a79SJeff Kirsher { 382d7058a79SJeff Kirsher /* use the size of the data resource to work out what IO 383d7058a79SJeff Kirsher * routines we want to use 384d7058a79SJeff Kirsher */ 385d7058a79SJeff Kirsher 386d7058a79SJeff Kirsher switch (byte_width) { 387d7058a79SJeff Kirsher case 1: 388d7058a79SJeff Kirsher db->dumpblk = dm9000_dumpblk_8bit; 389d7058a79SJeff Kirsher db->outblk = dm9000_outblk_8bit; 390d7058a79SJeff Kirsher db->inblk = dm9000_inblk_8bit; 391d7058a79SJeff Kirsher break; 392d7058a79SJeff Kirsher 393d7058a79SJeff Kirsher 394d7058a79SJeff Kirsher case 3: 395d7058a79SJeff Kirsher dev_dbg(db->dev, ": 3 byte IO, falling back to 16bit\n"); 396d7058a79SJeff Kirsher case 2: 397d7058a79SJeff Kirsher db->dumpblk = dm9000_dumpblk_16bit; 398d7058a79SJeff Kirsher db->outblk = dm9000_outblk_16bit; 399d7058a79SJeff Kirsher db->inblk = dm9000_inblk_16bit; 400d7058a79SJeff Kirsher break; 401d7058a79SJeff Kirsher 402d7058a79SJeff Kirsher case 4: 403d7058a79SJeff Kirsher default: 404d7058a79SJeff Kirsher db->dumpblk = dm9000_dumpblk_32bit; 405d7058a79SJeff Kirsher db->outblk = dm9000_outblk_32bit; 406d7058a79SJeff Kirsher db->inblk = dm9000_inblk_32bit; 407d7058a79SJeff Kirsher break; 408d7058a79SJeff Kirsher } 409d7058a79SJeff Kirsher } 410d7058a79SJeff Kirsher 411*2b162928SHimangi Saraogi static void dm9000_schedule_poll(struct board_info *db) 412d7058a79SJeff Kirsher { 413d7058a79SJeff Kirsher if (db->type == TYPE_DM9000E) 414d7058a79SJeff Kirsher schedule_delayed_work(&db->phy_poll, HZ * 2); 415d7058a79SJeff Kirsher } 416d7058a79SJeff Kirsher 417d7058a79SJeff Kirsher static int dm9000_ioctl(struct net_device *dev, struct ifreq *req, int cmd) 418d7058a79SJeff Kirsher { 419*2b162928SHimangi Saraogi struct board_info *dm = to_dm9000_board(dev); 420d7058a79SJeff Kirsher 421d7058a79SJeff Kirsher if (!netif_running(dev)) 422d7058a79SJeff Kirsher return -EINVAL; 423d7058a79SJeff Kirsher 424d7058a79SJeff Kirsher return generic_mii_ioctl(&dm->mii, if_mii(req), cmd, NULL); 425d7058a79SJeff Kirsher } 426d7058a79SJeff Kirsher 427d7058a79SJeff Kirsher static unsigned int 428*2b162928SHimangi Saraogi dm9000_read_locked(struct board_info *db, int reg) 429d7058a79SJeff Kirsher { 430d7058a79SJeff Kirsher unsigned long flags; 431d7058a79SJeff Kirsher unsigned int ret; 432d7058a79SJeff Kirsher 433d7058a79SJeff Kirsher spin_lock_irqsave(&db->lock, flags); 434d7058a79SJeff Kirsher ret = ior(db, reg); 435d7058a79SJeff Kirsher spin_unlock_irqrestore(&db->lock, flags); 436d7058a79SJeff Kirsher 437d7058a79SJeff Kirsher return ret; 438d7058a79SJeff Kirsher } 439d7058a79SJeff Kirsher 440*2b162928SHimangi Saraogi static int dm9000_wait_eeprom(struct board_info *db) 441d7058a79SJeff Kirsher { 442d7058a79SJeff Kirsher unsigned int status; 443d7058a79SJeff Kirsher int timeout = 8; /* wait max 8msec */ 444d7058a79SJeff Kirsher 445d7058a79SJeff Kirsher /* The DM9000 data sheets say we should be able to 446d7058a79SJeff Kirsher * poll the ERRE bit in EPCR to wait for the EEPROM 447d7058a79SJeff Kirsher * operation. From testing several chips, this bit 448d7058a79SJeff Kirsher * does not seem to work. 449d7058a79SJeff Kirsher * 450d7058a79SJeff Kirsher * We attempt to use the bit, but fall back to the 451d7058a79SJeff Kirsher * timeout (which is why we do not return an error 452d7058a79SJeff Kirsher * on expiry) to say that the EEPROM operation has 453d7058a79SJeff Kirsher * completed. 454d7058a79SJeff Kirsher */ 455d7058a79SJeff Kirsher 456d7058a79SJeff Kirsher while (1) { 457d7058a79SJeff Kirsher status = dm9000_read_locked(db, DM9000_EPCR); 458d7058a79SJeff Kirsher 459d7058a79SJeff Kirsher if ((status & EPCR_ERRE) == 0) 460d7058a79SJeff Kirsher break; 461d7058a79SJeff Kirsher 462d7058a79SJeff Kirsher msleep(1); 463d7058a79SJeff Kirsher 464d7058a79SJeff Kirsher if (timeout-- < 0) { 465d7058a79SJeff Kirsher dev_dbg(db->dev, "timeout waiting EEPROM\n"); 466d7058a79SJeff Kirsher break; 467d7058a79SJeff Kirsher } 468d7058a79SJeff Kirsher } 469d7058a79SJeff Kirsher 470d7058a79SJeff Kirsher return 0; 471d7058a79SJeff Kirsher } 472d7058a79SJeff Kirsher 473d7058a79SJeff Kirsher /* 474d7058a79SJeff Kirsher * Read a word data from EEPROM 475d7058a79SJeff Kirsher */ 476d7058a79SJeff Kirsher static void 477*2b162928SHimangi Saraogi dm9000_read_eeprom(struct board_info *db, int offset, u8 *to) 478d7058a79SJeff Kirsher { 479d7058a79SJeff Kirsher unsigned long flags; 480d7058a79SJeff Kirsher 481d7058a79SJeff Kirsher if (db->flags & DM9000_PLATF_NO_EEPROM) { 482d7058a79SJeff Kirsher to[0] = 0xff; 483d7058a79SJeff Kirsher to[1] = 0xff; 484d7058a79SJeff Kirsher return; 485d7058a79SJeff Kirsher } 486d7058a79SJeff Kirsher 487d7058a79SJeff Kirsher mutex_lock(&db->addr_lock); 488d7058a79SJeff Kirsher 489d7058a79SJeff Kirsher spin_lock_irqsave(&db->lock, flags); 490d7058a79SJeff Kirsher 491d7058a79SJeff Kirsher iow(db, DM9000_EPAR, offset); 492d7058a79SJeff Kirsher iow(db, DM9000_EPCR, EPCR_ERPRR); 493d7058a79SJeff Kirsher 494d7058a79SJeff Kirsher spin_unlock_irqrestore(&db->lock, flags); 495d7058a79SJeff Kirsher 496d7058a79SJeff Kirsher dm9000_wait_eeprom(db); 497d7058a79SJeff Kirsher 498d7058a79SJeff Kirsher /* delay for at-least 150uS */ 499d7058a79SJeff Kirsher msleep(1); 500d7058a79SJeff Kirsher 501d7058a79SJeff Kirsher spin_lock_irqsave(&db->lock, flags); 502d7058a79SJeff Kirsher 503d7058a79SJeff Kirsher iow(db, DM9000_EPCR, 0x0); 504d7058a79SJeff Kirsher 505d7058a79SJeff Kirsher to[0] = ior(db, DM9000_EPDRL); 506d7058a79SJeff Kirsher to[1] = ior(db, DM9000_EPDRH); 507d7058a79SJeff Kirsher 508d7058a79SJeff Kirsher spin_unlock_irqrestore(&db->lock, flags); 509d7058a79SJeff Kirsher 510d7058a79SJeff Kirsher mutex_unlock(&db->addr_lock); 511d7058a79SJeff Kirsher } 512d7058a79SJeff Kirsher 513d7058a79SJeff Kirsher /* 514d7058a79SJeff Kirsher * Write a word data to SROM 515d7058a79SJeff Kirsher */ 516d7058a79SJeff Kirsher static void 517*2b162928SHimangi Saraogi dm9000_write_eeprom(struct board_info *db, int offset, u8 *data) 518d7058a79SJeff Kirsher { 519d7058a79SJeff Kirsher unsigned long flags; 520d7058a79SJeff Kirsher 521d7058a79SJeff Kirsher if (db->flags & DM9000_PLATF_NO_EEPROM) 522d7058a79SJeff Kirsher return; 523d7058a79SJeff Kirsher 524d7058a79SJeff Kirsher mutex_lock(&db->addr_lock); 525d7058a79SJeff Kirsher 526d7058a79SJeff Kirsher spin_lock_irqsave(&db->lock, flags); 527d7058a79SJeff Kirsher iow(db, DM9000_EPAR, offset); 528d7058a79SJeff Kirsher iow(db, DM9000_EPDRH, data[1]); 529d7058a79SJeff Kirsher iow(db, DM9000_EPDRL, data[0]); 530d7058a79SJeff Kirsher iow(db, DM9000_EPCR, EPCR_WEP | EPCR_ERPRW); 531d7058a79SJeff Kirsher spin_unlock_irqrestore(&db->lock, flags); 532d7058a79SJeff Kirsher 533d7058a79SJeff Kirsher dm9000_wait_eeprom(db); 534d7058a79SJeff Kirsher 535d7058a79SJeff Kirsher mdelay(1); /* wait at least 150uS to clear */ 536d7058a79SJeff Kirsher 537d7058a79SJeff Kirsher spin_lock_irqsave(&db->lock, flags); 538d7058a79SJeff Kirsher iow(db, DM9000_EPCR, 0); 539d7058a79SJeff Kirsher spin_unlock_irqrestore(&db->lock, flags); 540d7058a79SJeff Kirsher 541d7058a79SJeff Kirsher mutex_unlock(&db->addr_lock); 542d7058a79SJeff Kirsher } 543d7058a79SJeff Kirsher 544d7058a79SJeff Kirsher /* ethtool ops */ 545d7058a79SJeff Kirsher 546d7058a79SJeff Kirsher static void dm9000_get_drvinfo(struct net_device *dev, 547d7058a79SJeff Kirsher struct ethtool_drvinfo *info) 548d7058a79SJeff Kirsher { 549*2b162928SHimangi Saraogi struct board_info *dm = to_dm9000_board(dev); 550d7058a79SJeff Kirsher 5517826d43fSJiri Pirko strlcpy(info->driver, CARDNAME, sizeof(info->driver)); 5527826d43fSJiri Pirko strlcpy(info->version, DRV_VERSION, sizeof(info->version)); 5537826d43fSJiri Pirko strlcpy(info->bus_info, to_platform_device(dm->dev)->name, 5547826d43fSJiri Pirko sizeof(info->bus_info)); 555d7058a79SJeff Kirsher } 556d7058a79SJeff Kirsher 557d7058a79SJeff Kirsher static u32 dm9000_get_msglevel(struct net_device *dev) 558d7058a79SJeff Kirsher { 559*2b162928SHimangi Saraogi struct board_info *dm = to_dm9000_board(dev); 560d7058a79SJeff Kirsher 561d7058a79SJeff Kirsher return dm->msg_enable; 562d7058a79SJeff Kirsher } 563d7058a79SJeff Kirsher 564d7058a79SJeff Kirsher static void dm9000_set_msglevel(struct net_device *dev, u32 value) 565d7058a79SJeff Kirsher { 566*2b162928SHimangi Saraogi struct board_info *dm = to_dm9000_board(dev); 567d7058a79SJeff Kirsher 568d7058a79SJeff Kirsher dm->msg_enable = value; 569d7058a79SJeff Kirsher } 570d7058a79SJeff Kirsher 571d7058a79SJeff Kirsher static int dm9000_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) 572d7058a79SJeff Kirsher { 573*2b162928SHimangi Saraogi struct board_info *dm = to_dm9000_board(dev); 574d7058a79SJeff Kirsher 575d7058a79SJeff Kirsher mii_ethtool_gset(&dm->mii, cmd); 576d7058a79SJeff Kirsher return 0; 577d7058a79SJeff Kirsher } 578d7058a79SJeff Kirsher 579d7058a79SJeff Kirsher static int dm9000_set_settings(struct net_device *dev, struct ethtool_cmd *cmd) 580d7058a79SJeff Kirsher { 581*2b162928SHimangi Saraogi struct board_info *dm = to_dm9000_board(dev); 582d7058a79SJeff Kirsher 583d7058a79SJeff Kirsher return mii_ethtool_sset(&dm->mii, cmd); 584d7058a79SJeff Kirsher } 585d7058a79SJeff Kirsher 586d7058a79SJeff Kirsher static int dm9000_nway_reset(struct net_device *dev) 587d7058a79SJeff Kirsher { 588*2b162928SHimangi Saraogi struct board_info *dm = to_dm9000_board(dev); 589d7058a79SJeff Kirsher return mii_nway_restart(&dm->mii); 590d7058a79SJeff Kirsher } 591d7058a79SJeff Kirsher 592c8f44affSMichał Mirosław static int dm9000_set_features(struct net_device *dev, 593c8f44affSMichał Mirosław netdev_features_t features) 594d7058a79SJeff Kirsher { 595*2b162928SHimangi Saraogi struct board_info *dm = to_dm9000_board(dev); 596c8f44affSMichał Mirosław netdev_features_t changed = dev->features ^ features; 597d7058a79SJeff Kirsher unsigned long flags; 598d7058a79SJeff Kirsher 599d7058a79SJeff Kirsher if (!(changed & NETIF_F_RXCSUM)) 600d7058a79SJeff Kirsher return 0; 601d7058a79SJeff Kirsher 602d7058a79SJeff Kirsher spin_lock_irqsave(&dm->lock, flags); 603d7058a79SJeff Kirsher iow(dm, DM9000_RCSR, (features & NETIF_F_RXCSUM) ? RCSR_CSUM : 0); 604d7058a79SJeff Kirsher spin_unlock_irqrestore(&dm->lock, flags); 605d7058a79SJeff Kirsher 606d7058a79SJeff Kirsher return 0; 607d7058a79SJeff Kirsher } 608d7058a79SJeff Kirsher 609d7058a79SJeff Kirsher static u32 dm9000_get_link(struct net_device *dev) 610d7058a79SJeff Kirsher { 611*2b162928SHimangi Saraogi struct board_info *dm = to_dm9000_board(dev); 612d7058a79SJeff Kirsher u32 ret; 613d7058a79SJeff Kirsher 614d7058a79SJeff Kirsher if (dm->flags & DM9000_PLATF_EXT_PHY) 615d7058a79SJeff Kirsher ret = mii_link_ok(&dm->mii); 616d7058a79SJeff Kirsher else 617d7058a79SJeff Kirsher ret = dm9000_read_locked(dm, DM9000_NSR) & NSR_LINKST ? 1 : 0; 618d7058a79SJeff Kirsher 619d7058a79SJeff Kirsher return ret; 620d7058a79SJeff Kirsher } 621d7058a79SJeff Kirsher 622d7058a79SJeff Kirsher #define DM_EEPROM_MAGIC (0x444D394B) 623d7058a79SJeff Kirsher 624d7058a79SJeff Kirsher static int dm9000_get_eeprom_len(struct net_device *dev) 625d7058a79SJeff Kirsher { 626d7058a79SJeff Kirsher return 128; 627d7058a79SJeff Kirsher } 628d7058a79SJeff Kirsher 629d7058a79SJeff Kirsher static int dm9000_get_eeprom(struct net_device *dev, 630d7058a79SJeff Kirsher struct ethtool_eeprom *ee, u8 *data) 631d7058a79SJeff Kirsher { 632*2b162928SHimangi Saraogi struct board_info *dm = to_dm9000_board(dev); 633d7058a79SJeff Kirsher int offset = ee->offset; 634d7058a79SJeff Kirsher int len = ee->len; 635d7058a79SJeff Kirsher int i; 636d7058a79SJeff Kirsher 637d7058a79SJeff Kirsher /* EEPROM access is aligned to two bytes */ 638d7058a79SJeff Kirsher 639d7058a79SJeff Kirsher if ((len & 1) != 0 || (offset & 1) != 0) 640d7058a79SJeff Kirsher return -EINVAL; 641d7058a79SJeff Kirsher 642d7058a79SJeff Kirsher if (dm->flags & DM9000_PLATF_NO_EEPROM) 643d7058a79SJeff Kirsher return -ENOENT; 644d7058a79SJeff Kirsher 645d7058a79SJeff Kirsher ee->magic = DM_EEPROM_MAGIC; 646d7058a79SJeff Kirsher 647d7058a79SJeff Kirsher for (i = 0; i < len; i += 2) 648d7058a79SJeff Kirsher dm9000_read_eeprom(dm, (offset + i) / 2, data + i); 649d7058a79SJeff Kirsher 650d7058a79SJeff Kirsher return 0; 651d7058a79SJeff Kirsher } 652d7058a79SJeff Kirsher 653d7058a79SJeff Kirsher static int dm9000_set_eeprom(struct net_device *dev, 654d7058a79SJeff Kirsher struct ethtool_eeprom *ee, u8 *data) 655d7058a79SJeff Kirsher { 656*2b162928SHimangi Saraogi struct board_info *dm = to_dm9000_board(dev); 657d7058a79SJeff Kirsher int offset = ee->offset; 658d7058a79SJeff Kirsher int len = ee->len; 659d7058a79SJeff Kirsher int done; 660d7058a79SJeff Kirsher 661d7058a79SJeff Kirsher /* EEPROM access is aligned to two bytes */ 662d7058a79SJeff Kirsher 663d7058a79SJeff Kirsher if (dm->flags & DM9000_PLATF_NO_EEPROM) 664d7058a79SJeff Kirsher return -ENOENT; 665d7058a79SJeff Kirsher 666d7058a79SJeff Kirsher if (ee->magic != DM_EEPROM_MAGIC) 667d7058a79SJeff Kirsher return -EINVAL; 668d7058a79SJeff Kirsher 669d7058a79SJeff Kirsher while (len > 0) { 670d7058a79SJeff Kirsher if (len & 1 || offset & 1) { 671d7058a79SJeff Kirsher int which = offset & 1; 672d7058a79SJeff Kirsher u8 tmp[2]; 673d7058a79SJeff Kirsher 674d7058a79SJeff Kirsher dm9000_read_eeprom(dm, offset / 2, tmp); 675d7058a79SJeff Kirsher tmp[which] = *data; 676d7058a79SJeff Kirsher dm9000_write_eeprom(dm, offset / 2, tmp); 677d7058a79SJeff Kirsher 678d7058a79SJeff Kirsher done = 1; 679d7058a79SJeff Kirsher } else { 680d7058a79SJeff Kirsher dm9000_write_eeprom(dm, offset / 2, data); 681d7058a79SJeff Kirsher done = 2; 682d7058a79SJeff Kirsher } 683d7058a79SJeff Kirsher 684d7058a79SJeff Kirsher data += done; 685d7058a79SJeff Kirsher offset += done; 686d7058a79SJeff Kirsher len -= done; 687d7058a79SJeff Kirsher } 688d7058a79SJeff Kirsher 689d7058a79SJeff Kirsher return 0; 690d7058a79SJeff Kirsher } 691d7058a79SJeff Kirsher 692d7058a79SJeff Kirsher static void dm9000_get_wol(struct net_device *dev, struct ethtool_wolinfo *w) 693d7058a79SJeff Kirsher { 694*2b162928SHimangi Saraogi struct board_info *dm = to_dm9000_board(dev); 695d7058a79SJeff Kirsher 696d7058a79SJeff Kirsher memset(w, 0, sizeof(struct ethtool_wolinfo)); 697d7058a79SJeff Kirsher 698d7058a79SJeff Kirsher /* note, we could probably support wake-phy too */ 699d7058a79SJeff Kirsher w->supported = dm->wake_supported ? WAKE_MAGIC : 0; 700d7058a79SJeff Kirsher w->wolopts = dm->wake_state; 701d7058a79SJeff Kirsher } 702d7058a79SJeff Kirsher 703d7058a79SJeff Kirsher static int dm9000_set_wol(struct net_device *dev, struct ethtool_wolinfo *w) 704d7058a79SJeff Kirsher { 705*2b162928SHimangi Saraogi struct board_info *dm = to_dm9000_board(dev); 706d7058a79SJeff Kirsher unsigned long flags; 707d7058a79SJeff Kirsher u32 opts = w->wolopts; 708d7058a79SJeff Kirsher u32 wcr = 0; 709d7058a79SJeff Kirsher 710d7058a79SJeff Kirsher if (!dm->wake_supported) 711d7058a79SJeff Kirsher return -EOPNOTSUPP; 712d7058a79SJeff Kirsher 713d7058a79SJeff Kirsher if (opts & ~WAKE_MAGIC) 714d7058a79SJeff Kirsher return -EINVAL; 715d7058a79SJeff Kirsher 716d7058a79SJeff Kirsher if (opts & WAKE_MAGIC) 717d7058a79SJeff Kirsher wcr |= WCR_MAGICEN; 718d7058a79SJeff Kirsher 719d7058a79SJeff Kirsher mutex_lock(&dm->addr_lock); 720d7058a79SJeff Kirsher 721d7058a79SJeff Kirsher spin_lock_irqsave(&dm->lock, flags); 722d7058a79SJeff Kirsher iow(dm, DM9000_WCR, wcr); 723d7058a79SJeff Kirsher spin_unlock_irqrestore(&dm->lock, flags); 724d7058a79SJeff Kirsher 725d7058a79SJeff Kirsher mutex_unlock(&dm->addr_lock); 726d7058a79SJeff Kirsher 727d7058a79SJeff Kirsher if (dm->wake_state != opts) { 728d7058a79SJeff Kirsher /* change in wol state, update IRQ state */ 729d7058a79SJeff Kirsher 730d7058a79SJeff Kirsher if (!dm->wake_state) 731d7058a79SJeff Kirsher irq_set_irq_wake(dm->irq_wake, 1); 73283b98fb4SMark Brown else if (dm->wake_state && !opts) 733d7058a79SJeff Kirsher irq_set_irq_wake(dm->irq_wake, 0); 734d7058a79SJeff Kirsher } 735d7058a79SJeff Kirsher 736d7058a79SJeff Kirsher dm->wake_state = opts; 737d7058a79SJeff Kirsher return 0; 738d7058a79SJeff Kirsher } 739d7058a79SJeff Kirsher 740d7058a79SJeff Kirsher static const struct ethtool_ops dm9000_ethtool_ops = { 741d7058a79SJeff Kirsher .get_drvinfo = dm9000_get_drvinfo, 742d7058a79SJeff Kirsher .get_settings = dm9000_get_settings, 743d7058a79SJeff Kirsher .set_settings = dm9000_set_settings, 744d7058a79SJeff Kirsher .get_msglevel = dm9000_get_msglevel, 745d7058a79SJeff Kirsher .set_msglevel = dm9000_set_msglevel, 746d7058a79SJeff Kirsher .nway_reset = dm9000_nway_reset, 747d7058a79SJeff Kirsher .get_link = dm9000_get_link, 748d7058a79SJeff Kirsher .get_wol = dm9000_get_wol, 749d7058a79SJeff Kirsher .set_wol = dm9000_set_wol, 750d7058a79SJeff Kirsher .get_eeprom_len = dm9000_get_eeprom_len, 751d7058a79SJeff Kirsher .get_eeprom = dm9000_get_eeprom, 752d7058a79SJeff Kirsher .set_eeprom = dm9000_set_eeprom, 753d7058a79SJeff Kirsher }; 754d7058a79SJeff Kirsher 755*2b162928SHimangi Saraogi static void dm9000_show_carrier(struct board_info *db, 756d7058a79SJeff Kirsher unsigned carrier, unsigned nsr) 757d7058a79SJeff Kirsher { 758727a282fSNikita Kiryanov int lpa; 759d7058a79SJeff Kirsher struct net_device *ndev = db->ndev; 760727a282fSNikita Kiryanov struct mii_if_info *mii = &db->mii; 761d7058a79SJeff Kirsher unsigned ncr = dm9000_read_locked(db, DM9000_NCR); 762d7058a79SJeff Kirsher 763727a282fSNikita Kiryanov if (carrier) { 764727a282fSNikita Kiryanov lpa = mii->mdio_read(mii->dev, mii->phy_id, MII_LPA); 765727a282fSNikita Kiryanov dev_info(db->dev, 766727a282fSNikita Kiryanov "%s: link up, %dMbps, %s-duplex, lpa 0x%04X\n", 767d7058a79SJeff Kirsher ndev->name, (nsr & NSR_SPEED) ? 10 : 100, 768727a282fSNikita Kiryanov (ncr & NCR_FDX) ? "full" : "half", lpa); 769727a282fSNikita Kiryanov } else { 770d7058a79SJeff Kirsher dev_info(db->dev, "%s: link down\n", ndev->name); 771d7058a79SJeff Kirsher } 772727a282fSNikita Kiryanov } 773d7058a79SJeff Kirsher 774d7058a79SJeff Kirsher static void 775d7058a79SJeff Kirsher dm9000_poll_work(struct work_struct *w) 776d7058a79SJeff Kirsher { 777d7058a79SJeff Kirsher struct delayed_work *dw = to_delayed_work(w); 778*2b162928SHimangi Saraogi struct board_info *db = container_of(dw, struct board_info, phy_poll); 779d7058a79SJeff Kirsher struct net_device *ndev = db->ndev; 780d7058a79SJeff Kirsher 781d7058a79SJeff Kirsher if (db->flags & DM9000_PLATF_SIMPLE_PHY && 782d7058a79SJeff Kirsher !(db->flags & DM9000_PLATF_EXT_PHY)) { 783d7058a79SJeff Kirsher unsigned nsr = dm9000_read_locked(db, DM9000_NSR); 784d7058a79SJeff Kirsher unsigned old_carrier = netif_carrier_ok(ndev) ? 1 : 0; 785d7058a79SJeff Kirsher unsigned new_carrier; 786d7058a79SJeff Kirsher 787d7058a79SJeff Kirsher new_carrier = (nsr & NSR_LINKST) ? 1 : 0; 788d7058a79SJeff Kirsher 789d7058a79SJeff Kirsher if (old_carrier != new_carrier) { 790d7058a79SJeff Kirsher if (netif_msg_link(db)) 791d7058a79SJeff Kirsher dm9000_show_carrier(db, new_carrier, nsr); 792d7058a79SJeff Kirsher 793d7058a79SJeff Kirsher if (!new_carrier) 794d7058a79SJeff Kirsher netif_carrier_off(ndev); 795d7058a79SJeff Kirsher else 796d7058a79SJeff Kirsher netif_carrier_on(ndev); 797d7058a79SJeff Kirsher } 798d7058a79SJeff Kirsher } else 799d7058a79SJeff Kirsher mii_check_media(&db->mii, netif_msg_link(db), 0); 800d7058a79SJeff Kirsher 801d7058a79SJeff Kirsher if (netif_running(ndev)) 802d7058a79SJeff Kirsher dm9000_schedule_poll(db); 803d7058a79SJeff Kirsher } 804d7058a79SJeff Kirsher 805d7058a79SJeff Kirsher /* dm9000_release_board 806d7058a79SJeff Kirsher * 807d7058a79SJeff Kirsher * release a board, and any mapped resources 808d7058a79SJeff Kirsher */ 809d7058a79SJeff Kirsher 810d7058a79SJeff Kirsher static void 811d7058a79SJeff Kirsher dm9000_release_board(struct platform_device *pdev, struct board_info *db) 812d7058a79SJeff Kirsher { 813d7058a79SJeff Kirsher /* unmap our resources */ 814d7058a79SJeff Kirsher 815d7058a79SJeff Kirsher iounmap(db->io_addr); 816d7058a79SJeff Kirsher iounmap(db->io_data); 817d7058a79SJeff Kirsher 818d7058a79SJeff Kirsher /* release the resources */ 819d7058a79SJeff Kirsher 820a5536e10SDan Carpenter if (db->data_req) 821d7058a79SJeff Kirsher release_resource(db->data_req); 822d7058a79SJeff Kirsher kfree(db->data_req); 823d7058a79SJeff Kirsher 824a5536e10SDan Carpenter if (db->addr_req) 825d7058a79SJeff Kirsher release_resource(db->addr_req); 826d7058a79SJeff Kirsher kfree(db->addr_req); 827d7058a79SJeff Kirsher } 828d7058a79SJeff Kirsher 829d7058a79SJeff Kirsher static unsigned char dm9000_type_to_char(enum dm9000_type type) 830d7058a79SJeff Kirsher { 831d7058a79SJeff Kirsher switch (type) { 832d7058a79SJeff Kirsher case TYPE_DM9000E: return 'e'; 833d7058a79SJeff Kirsher case TYPE_DM9000A: return 'a'; 834d7058a79SJeff Kirsher case TYPE_DM9000B: return 'b'; 835d7058a79SJeff Kirsher } 836d7058a79SJeff Kirsher 837d7058a79SJeff Kirsher return '?'; 838d7058a79SJeff Kirsher } 839d7058a79SJeff Kirsher 840d7058a79SJeff Kirsher /* 841d7058a79SJeff Kirsher * Set DM9000 multicast address 842d7058a79SJeff Kirsher */ 843d7058a79SJeff Kirsher static void 844d7058a79SJeff Kirsher dm9000_hash_table_unlocked(struct net_device *dev) 845d7058a79SJeff Kirsher { 846*2b162928SHimangi Saraogi struct board_info *db = netdev_priv(dev); 847d7058a79SJeff Kirsher struct netdev_hw_addr *ha; 848d7058a79SJeff Kirsher int i, oft; 849d7058a79SJeff Kirsher u32 hash_val; 85035e729acSEmilio López u16 hash_table[4] = { 0, 0, 0, 0x8000 }; /* broadcast address */ 851d7058a79SJeff Kirsher u8 rcr = RCR_DIS_LONG | RCR_DIS_CRC | RCR_RXEN; 852d7058a79SJeff Kirsher 853d7058a79SJeff Kirsher dm9000_dbg(db, 1, "entering %s\n", __func__); 854d7058a79SJeff Kirsher 855d7058a79SJeff Kirsher for (i = 0, oft = DM9000_PAR; i < 6; i++, oft++) 856d7058a79SJeff Kirsher iow(db, oft, dev->dev_addr[i]); 857d7058a79SJeff Kirsher 858d7058a79SJeff Kirsher if (dev->flags & IFF_PROMISC) 859d7058a79SJeff Kirsher rcr |= RCR_PRMSC; 860d7058a79SJeff Kirsher 861d7058a79SJeff Kirsher if (dev->flags & IFF_ALLMULTI) 862d7058a79SJeff Kirsher rcr |= RCR_ALL; 863d7058a79SJeff Kirsher 864d7058a79SJeff Kirsher /* the multicast address in Hash Table : 64 bits */ 865d7058a79SJeff Kirsher netdev_for_each_mc_addr(ha, dev) { 866d7058a79SJeff Kirsher hash_val = ether_crc_le(6, ha->addr) & 0x3f; 867d7058a79SJeff Kirsher hash_table[hash_val / 16] |= (u16) 1 << (hash_val % 16); 868d7058a79SJeff Kirsher } 869d7058a79SJeff Kirsher 870d7058a79SJeff Kirsher /* Write the hash table to MAC MD table */ 871d7058a79SJeff Kirsher for (i = 0, oft = DM9000_MAR; i < 4; i++) { 872d7058a79SJeff Kirsher iow(db, oft++, hash_table[i]); 873d7058a79SJeff Kirsher iow(db, oft++, hash_table[i] >> 8); 874d7058a79SJeff Kirsher } 875d7058a79SJeff Kirsher 876d7058a79SJeff Kirsher iow(db, DM9000_RCR, rcr); 877d7058a79SJeff Kirsher } 878d7058a79SJeff Kirsher 879d7058a79SJeff Kirsher static void 880d7058a79SJeff Kirsher dm9000_hash_table(struct net_device *dev) 881d7058a79SJeff Kirsher { 882*2b162928SHimangi Saraogi struct board_info *db = netdev_priv(dev); 883d7058a79SJeff Kirsher unsigned long flags; 884d7058a79SJeff Kirsher 885d7058a79SJeff Kirsher spin_lock_irqsave(&db->lock, flags); 886d7058a79SJeff Kirsher dm9000_hash_table_unlocked(dev); 887d7058a79SJeff Kirsher spin_unlock_irqrestore(&db->lock, flags); 888d7058a79SJeff Kirsher } 889d7058a79SJeff Kirsher 89017ad78deSAndrew Ruder static void 891*2b162928SHimangi Saraogi dm9000_mask_interrupts(struct board_info *db) 89217ad78deSAndrew Ruder { 89317ad78deSAndrew Ruder iow(db, DM9000_IMR, IMR_PAR); 89417ad78deSAndrew Ruder } 89517ad78deSAndrew Ruder 89617ad78deSAndrew Ruder static void 897*2b162928SHimangi Saraogi dm9000_unmask_interrupts(struct board_info *db) 89817ad78deSAndrew Ruder { 89917ad78deSAndrew Ruder iow(db, DM9000_IMR, db->imr_all); 90017ad78deSAndrew Ruder } 90117ad78deSAndrew Ruder 902d7058a79SJeff Kirsher /* 903d7058a79SJeff Kirsher * Initialize dm9000 board 904d7058a79SJeff Kirsher */ 905d7058a79SJeff Kirsher static void 906d7058a79SJeff Kirsher dm9000_init_dm9000(struct net_device *dev) 907d7058a79SJeff Kirsher { 908*2b162928SHimangi Saraogi struct board_info *db = netdev_priv(dev); 909d7058a79SJeff Kirsher unsigned int imr; 910d7058a79SJeff Kirsher unsigned int ncr; 911d7058a79SJeff Kirsher 912d7058a79SJeff Kirsher dm9000_dbg(db, 1, "entering %s\n", __func__); 913d7058a79SJeff Kirsher 914751bb6fdSAndrew Ruder dm9000_reset(db); 91517ad78deSAndrew Ruder dm9000_mask_interrupts(db); 916751bb6fdSAndrew Ruder 917d7058a79SJeff Kirsher /* I/O mode */ 918d7058a79SJeff Kirsher db->io_mode = ior(db, DM9000_ISR) >> 6; /* ISR bit7:6 keeps I/O mode */ 919d7058a79SJeff Kirsher 920d7058a79SJeff Kirsher /* Checksum mode */ 921d7058a79SJeff Kirsher if (dev->hw_features & NETIF_F_RXCSUM) 922d7058a79SJeff Kirsher iow(db, DM9000_RCSR, 923d7058a79SJeff Kirsher (dev->features & NETIF_F_RXCSUM) ? RCSR_CSUM : 0); 924d7058a79SJeff Kirsher 925d7058a79SJeff Kirsher iow(db, DM9000_GPCR, GPCR_GEP_CNTL); /* Let GPIO0 output */ 926677d7d28SNikita Kiryanov iow(db, DM9000_GPR, 0); 927d7058a79SJeff Kirsher 9286649b205SNikita Kiryanov /* If we are dealing with DM9000B, some extra steps are required: a 9296649b205SNikita Kiryanov * manual phy reset, and setting init params. 9306649b205SNikita Kiryanov */ 9316649b205SNikita Kiryanov if (db->type == TYPE_DM9000B) { 9326649b205SNikita Kiryanov dm9000_phy_write(dev, 0, MII_BMCR, BMCR_RESET); 9336649b205SNikita Kiryanov dm9000_phy_write(dev, 0, MII_DM_DSPCR, DSPCR_INIT_PARAM); 9346649b205SNikita Kiryanov } 9356741f40dSJoseph CHANG 936d7058a79SJeff Kirsher ncr = (db->flags & DM9000_PLATF_EXT_PHY) ? NCR_EXT_PHY : 0; 937d7058a79SJeff Kirsher 938d7058a79SJeff Kirsher /* if wol is needed, then always set NCR_WAKEEN otherwise we end 939d7058a79SJeff Kirsher * up dumping the wake events if we disable this. There is already 940d7058a79SJeff Kirsher * a wake-mask in DM9000_WCR */ 941d7058a79SJeff Kirsher if (db->wake_supported) 942d7058a79SJeff Kirsher ncr |= NCR_WAKEEN; 943d7058a79SJeff Kirsher 944d7058a79SJeff Kirsher iow(db, DM9000_NCR, ncr); 945d7058a79SJeff Kirsher 946d7058a79SJeff Kirsher /* Program operating register */ 947d7058a79SJeff Kirsher iow(db, DM9000_TCR, 0); /* TX Polling clear */ 948d7058a79SJeff Kirsher iow(db, DM9000_BPTR, 0x3f); /* Less 3Kb, 200us */ 949d7058a79SJeff Kirsher iow(db, DM9000_FCR, 0xff); /* Flow Control */ 950d7058a79SJeff Kirsher iow(db, DM9000_SMCR, 0); /* Special Mode */ 951d7058a79SJeff Kirsher /* clear TX status */ 952d7058a79SJeff Kirsher iow(db, DM9000_NSR, NSR_WAKEST | NSR_TX2END | NSR_TX1END); 953d7058a79SJeff Kirsher iow(db, DM9000_ISR, ISR_CLR_STATUS); /* Clear interrupt status */ 954d7058a79SJeff Kirsher 955d7058a79SJeff Kirsher /* Set address filter table */ 956d7058a79SJeff Kirsher dm9000_hash_table_unlocked(dev); 957d7058a79SJeff Kirsher 958d7058a79SJeff Kirsher imr = IMR_PAR | IMR_PTM | IMR_PRM; 959d7058a79SJeff Kirsher if (db->type != TYPE_DM9000E) 960d7058a79SJeff Kirsher imr |= IMR_LNKCHNG; 961d7058a79SJeff Kirsher 962d7058a79SJeff Kirsher db->imr_all = imr; 963d7058a79SJeff Kirsher 964d7058a79SJeff Kirsher /* Init Driver variable */ 965d7058a79SJeff Kirsher db->tx_pkt_cnt = 0; 966d7058a79SJeff Kirsher db->queue_pkt_len = 0; 967d7058a79SJeff Kirsher dev->trans_start = jiffies; 968d7058a79SJeff Kirsher } 969d7058a79SJeff Kirsher 970d7058a79SJeff Kirsher /* Our watchdog timed out. Called by the networking layer */ 971d7058a79SJeff Kirsher static void dm9000_timeout(struct net_device *dev) 972d7058a79SJeff Kirsher { 973*2b162928SHimangi Saraogi struct board_info *db = netdev_priv(dev); 974d7058a79SJeff Kirsher u8 reg_save; 975d7058a79SJeff Kirsher unsigned long flags; 976d7058a79SJeff Kirsher 977d7058a79SJeff Kirsher /* Save previous register address */ 978d7058a79SJeff Kirsher spin_lock_irqsave(&db->lock, flags); 97958237983SAndrew Ruder db->in_timeout = 1; 980d7058a79SJeff Kirsher reg_save = readb(db->io_addr); 981d7058a79SJeff Kirsher 982d7058a79SJeff Kirsher netif_stop_queue(dev); 983d7058a79SJeff Kirsher dm9000_init_dm9000(dev); 98417ad78deSAndrew Ruder dm9000_unmask_interrupts(db); 985d7058a79SJeff Kirsher /* We can accept TX packets again */ 986d7058a79SJeff Kirsher dev->trans_start = jiffies; /* prevent tx timeout */ 987d7058a79SJeff Kirsher netif_wake_queue(dev); 988d7058a79SJeff Kirsher 989d7058a79SJeff Kirsher /* Restore previous register address */ 990d7058a79SJeff Kirsher writeb(reg_save, db->io_addr); 99158237983SAndrew Ruder db->in_timeout = 0; 992d7058a79SJeff Kirsher spin_unlock_irqrestore(&db->lock, flags); 993d7058a79SJeff Kirsher } 994d7058a79SJeff Kirsher 995d7058a79SJeff Kirsher static void dm9000_send_packet(struct net_device *dev, 996d7058a79SJeff Kirsher int ip_summed, 997d7058a79SJeff Kirsher u16 pkt_len) 998d7058a79SJeff Kirsher { 999*2b162928SHimangi Saraogi struct board_info *dm = to_dm9000_board(dev); 1000d7058a79SJeff Kirsher 1001d7058a79SJeff Kirsher /* The DM9000 is not smart enough to leave fragmented packets alone. */ 1002d7058a79SJeff Kirsher if (dm->ip_summed != ip_summed) { 1003d7058a79SJeff Kirsher if (ip_summed == CHECKSUM_NONE) 1004d7058a79SJeff Kirsher iow(dm, DM9000_TCCR, 0); 1005d7058a79SJeff Kirsher else 1006d7058a79SJeff Kirsher iow(dm, DM9000_TCCR, TCCR_IP | TCCR_UDP | TCCR_TCP); 1007d7058a79SJeff Kirsher dm->ip_summed = ip_summed; 1008d7058a79SJeff Kirsher } 1009d7058a79SJeff Kirsher 1010d7058a79SJeff Kirsher /* Set TX length to DM9000 */ 1011d7058a79SJeff Kirsher iow(dm, DM9000_TXPLL, pkt_len); 1012d7058a79SJeff Kirsher iow(dm, DM9000_TXPLH, pkt_len >> 8); 1013d7058a79SJeff Kirsher 1014d7058a79SJeff Kirsher /* Issue TX polling command */ 1015d7058a79SJeff Kirsher iow(dm, DM9000_TCR, TCR_TXREQ); /* Cleared after TX complete */ 1016d7058a79SJeff Kirsher } 1017d7058a79SJeff Kirsher 1018d7058a79SJeff Kirsher /* 1019d7058a79SJeff Kirsher * Hardware start transmission. 1020d7058a79SJeff Kirsher * Send a packet to media from the upper layer. 1021d7058a79SJeff Kirsher */ 1022d7058a79SJeff Kirsher static int 1023d7058a79SJeff Kirsher dm9000_start_xmit(struct sk_buff *skb, struct net_device *dev) 1024d7058a79SJeff Kirsher { 1025d7058a79SJeff Kirsher unsigned long flags; 1026*2b162928SHimangi Saraogi struct board_info *db = netdev_priv(dev); 1027d7058a79SJeff Kirsher 1028d7058a79SJeff Kirsher dm9000_dbg(db, 3, "%s:\n", __func__); 1029d7058a79SJeff Kirsher 1030d7058a79SJeff Kirsher if (db->tx_pkt_cnt > 1) 1031d7058a79SJeff Kirsher return NETDEV_TX_BUSY; 1032d7058a79SJeff Kirsher 1033d7058a79SJeff Kirsher spin_lock_irqsave(&db->lock, flags); 1034d7058a79SJeff Kirsher 1035d7058a79SJeff Kirsher /* Move data to DM9000 TX RAM */ 1036d7058a79SJeff Kirsher writeb(DM9000_MWCMD, db->io_addr); 1037d7058a79SJeff Kirsher 1038d7058a79SJeff Kirsher (db->outblk)(db->io_data, skb->data, skb->len); 1039d7058a79SJeff Kirsher dev->stats.tx_bytes += skb->len; 1040d7058a79SJeff Kirsher 1041d7058a79SJeff Kirsher db->tx_pkt_cnt++; 1042d7058a79SJeff Kirsher /* TX control: First packet immediately send, second packet queue */ 1043d7058a79SJeff Kirsher if (db->tx_pkt_cnt == 1) { 1044d7058a79SJeff Kirsher dm9000_send_packet(dev, skb->ip_summed, skb->len); 1045d7058a79SJeff Kirsher } else { 1046d7058a79SJeff Kirsher /* Second packet */ 1047d7058a79SJeff Kirsher db->queue_pkt_len = skb->len; 1048d7058a79SJeff Kirsher db->queue_ip_summed = skb->ip_summed; 1049d7058a79SJeff Kirsher netif_stop_queue(dev); 1050d7058a79SJeff Kirsher } 1051d7058a79SJeff Kirsher 1052d7058a79SJeff Kirsher spin_unlock_irqrestore(&db->lock, flags); 1053d7058a79SJeff Kirsher 1054d7058a79SJeff Kirsher /* free this SKB */ 10552c3d0bc0SEric W. Biederman dev_consume_skb_any(skb); 1056d7058a79SJeff Kirsher 1057d7058a79SJeff Kirsher return NETDEV_TX_OK; 1058d7058a79SJeff Kirsher } 1059d7058a79SJeff Kirsher 1060d7058a79SJeff Kirsher /* 1061d7058a79SJeff Kirsher * DM9000 interrupt handler 1062d7058a79SJeff Kirsher * receive the packet to upper layer, free the transmitted packet 1063d7058a79SJeff Kirsher */ 1064d7058a79SJeff Kirsher 1065*2b162928SHimangi Saraogi static void dm9000_tx_done(struct net_device *dev, struct board_info *db) 1066d7058a79SJeff Kirsher { 1067d7058a79SJeff Kirsher int tx_status = ior(db, DM9000_NSR); /* Got TX status */ 1068d7058a79SJeff Kirsher 1069d7058a79SJeff Kirsher if (tx_status & (NSR_TX2END | NSR_TX1END)) { 1070d7058a79SJeff Kirsher /* One packet sent complete */ 1071d7058a79SJeff Kirsher db->tx_pkt_cnt--; 1072d7058a79SJeff Kirsher dev->stats.tx_packets++; 1073d7058a79SJeff Kirsher 1074d7058a79SJeff Kirsher if (netif_msg_tx_done(db)) 1075d7058a79SJeff Kirsher dev_dbg(db->dev, "tx done, NSR %02x\n", tx_status); 1076d7058a79SJeff Kirsher 1077d7058a79SJeff Kirsher /* Queue packet check & send */ 1078d7058a79SJeff Kirsher if (db->tx_pkt_cnt > 0) 1079d7058a79SJeff Kirsher dm9000_send_packet(dev, db->queue_ip_summed, 1080d7058a79SJeff Kirsher db->queue_pkt_len); 1081d7058a79SJeff Kirsher netif_wake_queue(dev); 1082d7058a79SJeff Kirsher } 1083d7058a79SJeff Kirsher } 1084d7058a79SJeff Kirsher 1085d7058a79SJeff Kirsher struct dm9000_rxhdr { 1086d7058a79SJeff Kirsher u8 RxPktReady; 1087d7058a79SJeff Kirsher u8 RxStatus; 1088d7058a79SJeff Kirsher __le16 RxLen; 1089d7058a79SJeff Kirsher } __packed; 1090d7058a79SJeff Kirsher 1091d7058a79SJeff Kirsher /* 1092d7058a79SJeff Kirsher * Received a packet and pass to upper layer 1093d7058a79SJeff Kirsher */ 1094d7058a79SJeff Kirsher static void 1095d7058a79SJeff Kirsher dm9000_rx(struct net_device *dev) 1096d7058a79SJeff Kirsher { 1097*2b162928SHimangi Saraogi struct board_info *db = netdev_priv(dev); 1098d7058a79SJeff Kirsher struct dm9000_rxhdr rxhdr; 1099d7058a79SJeff Kirsher struct sk_buff *skb; 1100d7058a79SJeff Kirsher u8 rxbyte, *rdptr; 1101d7058a79SJeff Kirsher bool GoodPacket; 1102d7058a79SJeff Kirsher int RxLen; 1103d7058a79SJeff Kirsher 1104d7058a79SJeff Kirsher /* Check packet ready or not */ 1105d7058a79SJeff Kirsher do { 1106d7058a79SJeff Kirsher ior(db, DM9000_MRCMDX); /* Dummy read */ 1107d7058a79SJeff Kirsher 1108d7058a79SJeff Kirsher /* Get most updated data */ 1109d7058a79SJeff Kirsher rxbyte = readb(db->io_data); 1110d7058a79SJeff Kirsher 1111d7058a79SJeff Kirsher /* Status check: this byte must be 0 or 1 */ 1112d7058a79SJeff Kirsher if (rxbyte & DM9000_PKT_ERR) { 1113d7058a79SJeff Kirsher dev_warn(db->dev, "status check fail: %d\n", rxbyte); 1114d7058a79SJeff Kirsher iow(db, DM9000_RCR, 0x00); /* Stop Device */ 1115d7058a79SJeff Kirsher return; 1116d7058a79SJeff Kirsher } 1117d7058a79SJeff Kirsher 1118d7058a79SJeff Kirsher if (!(rxbyte & DM9000_PKT_RDY)) 1119d7058a79SJeff Kirsher return; 1120d7058a79SJeff Kirsher 1121d7058a79SJeff Kirsher /* A packet ready now & Get status/length */ 1122d7058a79SJeff Kirsher GoodPacket = true; 1123d7058a79SJeff Kirsher writeb(DM9000_MRCMD, db->io_addr); 1124d7058a79SJeff Kirsher 1125d7058a79SJeff Kirsher (db->inblk)(db->io_data, &rxhdr, sizeof(rxhdr)); 1126d7058a79SJeff Kirsher 1127d7058a79SJeff Kirsher RxLen = le16_to_cpu(rxhdr.RxLen); 1128d7058a79SJeff Kirsher 1129d7058a79SJeff Kirsher if (netif_msg_rx_status(db)) 1130d7058a79SJeff Kirsher dev_dbg(db->dev, "RX: status %02x, length %04x\n", 1131d7058a79SJeff Kirsher rxhdr.RxStatus, RxLen); 1132d7058a79SJeff Kirsher 1133d7058a79SJeff Kirsher /* Packet Status check */ 1134d7058a79SJeff Kirsher if (RxLen < 0x40) { 1135d7058a79SJeff Kirsher GoodPacket = false; 1136d7058a79SJeff Kirsher if (netif_msg_rx_err(db)) 1137d7058a79SJeff Kirsher dev_dbg(db->dev, "RX: Bad Packet (runt)\n"); 1138d7058a79SJeff Kirsher } 1139d7058a79SJeff Kirsher 1140d7058a79SJeff Kirsher if (RxLen > DM9000_PKT_MAX) { 1141d7058a79SJeff Kirsher dev_dbg(db->dev, "RST: RX Len:%x\n", RxLen); 1142d7058a79SJeff Kirsher } 1143d7058a79SJeff Kirsher 1144d7058a79SJeff Kirsher /* rxhdr.RxStatus is identical to RSR register. */ 1145d7058a79SJeff Kirsher if (rxhdr.RxStatus & (RSR_FOE | RSR_CE | RSR_AE | 1146d7058a79SJeff Kirsher RSR_PLE | RSR_RWTO | 1147d7058a79SJeff Kirsher RSR_LCS | RSR_RF)) { 1148d7058a79SJeff Kirsher GoodPacket = false; 1149d7058a79SJeff Kirsher if (rxhdr.RxStatus & RSR_FOE) { 1150d7058a79SJeff Kirsher if (netif_msg_rx_err(db)) 1151d7058a79SJeff Kirsher dev_dbg(db->dev, "fifo error\n"); 1152d7058a79SJeff Kirsher dev->stats.rx_fifo_errors++; 1153d7058a79SJeff Kirsher } 1154d7058a79SJeff Kirsher if (rxhdr.RxStatus & RSR_CE) { 1155d7058a79SJeff Kirsher if (netif_msg_rx_err(db)) 1156d7058a79SJeff Kirsher dev_dbg(db->dev, "crc error\n"); 1157d7058a79SJeff Kirsher dev->stats.rx_crc_errors++; 1158d7058a79SJeff Kirsher } 1159d7058a79SJeff Kirsher if (rxhdr.RxStatus & RSR_RF) { 1160d7058a79SJeff Kirsher if (netif_msg_rx_err(db)) 1161d7058a79SJeff Kirsher dev_dbg(db->dev, "length error\n"); 1162d7058a79SJeff Kirsher dev->stats.rx_length_errors++; 1163d7058a79SJeff Kirsher } 1164d7058a79SJeff Kirsher } 1165d7058a79SJeff Kirsher 1166d7058a79SJeff Kirsher /* Move data from DM9000 */ 1167d7058a79SJeff Kirsher if (GoodPacket && 116821a4e469SPradeep A Dalvi ((skb = netdev_alloc_skb(dev, RxLen + 4)) != NULL)) { 1169d7058a79SJeff Kirsher skb_reserve(skb, 2); 1170d7058a79SJeff Kirsher rdptr = (u8 *) skb_put(skb, RxLen - 4); 1171d7058a79SJeff Kirsher 1172d7058a79SJeff Kirsher /* Read received packet from RX SRAM */ 1173d7058a79SJeff Kirsher 1174d7058a79SJeff Kirsher (db->inblk)(db->io_data, rdptr, RxLen); 1175d7058a79SJeff Kirsher dev->stats.rx_bytes += RxLen; 1176d7058a79SJeff Kirsher 1177d7058a79SJeff Kirsher /* Pass to upper layer */ 1178d7058a79SJeff Kirsher skb->protocol = eth_type_trans(skb, dev); 1179d7058a79SJeff Kirsher if (dev->features & NETIF_F_RXCSUM) { 1180d7058a79SJeff Kirsher if ((((rxbyte & 0x1c) << 3) & rxbyte) == 0) 1181d7058a79SJeff Kirsher skb->ip_summed = CHECKSUM_UNNECESSARY; 1182d7058a79SJeff Kirsher else 1183d7058a79SJeff Kirsher skb_checksum_none_assert(skb); 1184d7058a79SJeff Kirsher } 1185d7058a79SJeff Kirsher netif_rx(skb); 1186d7058a79SJeff Kirsher dev->stats.rx_packets++; 1187d7058a79SJeff Kirsher 1188d7058a79SJeff Kirsher } else { 1189d7058a79SJeff Kirsher /* need to dump the packet's data */ 1190d7058a79SJeff Kirsher 1191d7058a79SJeff Kirsher (db->dumpblk)(db->io_data, RxLen); 1192d7058a79SJeff Kirsher } 1193d7058a79SJeff Kirsher } while (rxbyte & DM9000_PKT_RDY); 1194d7058a79SJeff Kirsher } 1195d7058a79SJeff Kirsher 1196d7058a79SJeff Kirsher static irqreturn_t dm9000_interrupt(int irq, void *dev_id) 1197d7058a79SJeff Kirsher { 1198d7058a79SJeff Kirsher struct net_device *dev = dev_id; 1199*2b162928SHimangi Saraogi struct board_info *db = netdev_priv(dev); 1200d7058a79SJeff Kirsher int int_status; 1201d7058a79SJeff Kirsher unsigned long flags; 1202d7058a79SJeff Kirsher u8 reg_save; 1203d7058a79SJeff Kirsher 1204d7058a79SJeff Kirsher dm9000_dbg(db, 3, "entering %s\n", __func__); 1205d7058a79SJeff Kirsher 1206d7058a79SJeff Kirsher /* A real interrupt coming */ 1207d7058a79SJeff Kirsher 1208d7058a79SJeff Kirsher /* holders of db->lock must always block IRQs */ 1209d7058a79SJeff Kirsher spin_lock_irqsave(&db->lock, flags); 1210d7058a79SJeff Kirsher 1211d7058a79SJeff Kirsher /* Save previous register address */ 1212d7058a79SJeff Kirsher reg_save = readb(db->io_addr); 1213d7058a79SJeff Kirsher 121417ad78deSAndrew Ruder dm9000_mask_interrupts(db); 1215d7058a79SJeff Kirsher /* Got DM9000 interrupt status */ 1216d7058a79SJeff Kirsher int_status = ior(db, DM9000_ISR); /* Got ISR */ 1217d7058a79SJeff Kirsher iow(db, DM9000_ISR, int_status); /* Clear ISR status */ 1218d7058a79SJeff Kirsher 1219d7058a79SJeff Kirsher if (netif_msg_intr(db)) 1220d7058a79SJeff Kirsher dev_dbg(db->dev, "interrupt status %02x\n", int_status); 1221d7058a79SJeff Kirsher 1222d7058a79SJeff Kirsher /* Received the coming packet */ 1223d7058a79SJeff Kirsher if (int_status & ISR_PRS) 1224d7058a79SJeff Kirsher dm9000_rx(dev); 1225d7058a79SJeff Kirsher 1226d7058a79SJeff Kirsher /* Trnasmit Interrupt check */ 1227d7058a79SJeff Kirsher if (int_status & ISR_PTS) 1228d7058a79SJeff Kirsher dm9000_tx_done(dev, db); 1229d7058a79SJeff Kirsher 1230d7058a79SJeff Kirsher if (db->type != TYPE_DM9000E) { 1231d7058a79SJeff Kirsher if (int_status & ISR_LNKCHNG) { 1232d7058a79SJeff Kirsher /* fire a link-change request */ 1233d7058a79SJeff Kirsher schedule_delayed_work(&db->phy_poll, 1); 1234d7058a79SJeff Kirsher } 1235d7058a79SJeff Kirsher } 1236d7058a79SJeff Kirsher 123717ad78deSAndrew Ruder dm9000_unmask_interrupts(db); 1238d7058a79SJeff Kirsher /* Restore previous register address */ 1239d7058a79SJeff Kirsher writeb(reg_save, db->io_addr); 1240d7058a79SJeff Kirsher 1241d7058a79SJeff Kirsher spin_unlock_irqrestore(&db->lock, flags); 1242d7058a79SJeff Kirsher 1243d7058a79SJeff Kirsher return IRQ_HANDLED; 1244d7058a79SJeff Kirsher } 1245d7058a79SJeff Kirsher 1246d7058a79SJeff Kirsher static irqreturn_t dm9000_wol_interrupt(int irq, void *dev_id) 1247d7058a79SJeff Kirsher { 1248d7058a79SJeff Kirsher struct net_device *dev = dev_id; 1249*2b162928SHimangi Saraogi struct board_info *db = netdev_priv(dev); 1250d7058a79SJeff Kirsher unsigned long flags; 1251d7058a79SJeff Kirsher unsigned nsr, wcr; 1252d7058a79SJeff Kirsher 1253d7058a79SJeff Kirsher spin_lock_irqsave(&db->lock, flags); 1254d7058a79SJeff Kirsher 1255d7058a79SJeff Kirsher nsr = ior(db, DM9000_NSR); 1256d7058a79SJeff Kirsher wcr = ior(db, DM9000_WCR); 1257d7058a79SJeff Kirsher 1258d7058a79SJeff Kirsher dev_dbg(db->dev, "%s: NSR=0x%02x, WCR=0x%02x\n", __func__, nsr, wcr); 1259d7058a79SJeff Kirsher 1260d7058a79SJeff Kirsher if (nsr & NSR_WAKEST) { 1261d7058a79SJeff Kirsher /* clear, so we can avoid */ 1262d7058a79SJeff Kirsher iow(db, DM9000_NSR, NSR_WAKEST); 1263d7058a79SJeff Kirsher 1264d7058a79SJeff Kirsher if (wcr & WCR_LINKST) 1265d7058a79SJeff Kirsher dev_info(db->dev, "wake by link status change\n"); 1266d7058a79SJeff Kirsher if (wcr & WCR_SAMPLEST) 1267d7058a79SJeff Kirsher dev_info(db->dev, "wake by sample packet\n"); 1268d7058a79SJeff Kirsher if (wcr & WCR_MAGICST) 1269d7058a79SJeff Kirsher dev_info(db->dev, "wake by magic packet\n"); 1270d7058a79SJeff Kirsher if (!(wcr & (WCR_LINKST | WCR_SAMPLEST | WCR_MAGICST))) 1271d7058a79SJeff Kirsher dev_err(db->dev, "wake signalled with no reason? " 1272d7058a79SJeff Kirsher "NSR=0x%02x, WSR=0x%02x\n", nsr, wcr); 1273d7058a79SJeff Kirsher } 1274d7058a79SJeff Kirsher 1275d7058a79SJeff Kirsher spin_unlock_irqrestore(&db->lock, flags); 1276d7058a79SJeff Kirsher 1277d7058a79SJeff Kirsher return (nsr & NSR_WAKEST) ? IRQ_HANDLED : IRQ_NONE; 1278d7058a79SJeff Kirsher } 1279d7058a79SJeff Kirsher 1280d7058a79SJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER 1281d7058a79SJeff Kirsher /* 1282d7058a79SJeff Kirsher *Used by netconsole 1283d7058a79SJeff Kirsher */ 1284d7058a79SJeff Kirsher static void dm9000_poll_controller(struct net_device *dev) 1285d7058a79SJeff Kirsher { 1286d7058a79SJeff Kirsher disable_irq(dev->irq); 1287d7058a79SJeff Kirsher dm9000_interrupt(dev->irq, dev); 1288d7058a79SJeff Kirsher enable_irq(dev->irq); 1289d7058a79SJeff Kirsher } 1290d7058a79SJeff Kirsher #endif 1291d7058a79SJeff Kirsher 1292d7058a79SJeff Kirsher /* 1293d7058a79SJeff Kirsher * Open the interface. 1294d7058a79SJeff Kirsher * The interface is opened whenever "ifconfig" actives it. 1295d7058a79SJeff Kirsher */ 1296d7058a79SJeff Kirsher static int 1297d7058a79SJeff Kirsher dm9000_open(struct net_device *dev) 1298d7058a79SJeff Kirsher { 1299*2b162928SHimangi Saraogi struct board_info *db = netdev_priv(dev); 1300d7058a79SJeff Kirsher unsigned long irqflags = db->irq_res->flags & IRQF_TRIGGER_MASK; 1301d7058a79SJeff Kirsher 1302d7058a79SJeff Kirsher if (netif_msg_ifup(db)) 1303d7058a79SJeff Kirsher dev_dbg(db->dev, "enabling %s\n", dev->name); 1304d7058a79SJeff Kirsher 1305d7058a79SJeff Kirsher /* If there is no IRQ type specified, default to something that 1306d7058a79SJeff Kirsher * may work, and tell the user that this is a problem */ 1307d7058a79SJeff Kirsher 1308d7058a79SJeff Kirsher if (irqflags == IRQF_TRIGGER_NONE) 13096b50d038SAndrew Ruder irqflags = irq_get_trigger_type(dev->irq); 13106b50d038SAndrew Ruder 13116b50d038SAndrew Ruder if (irqflags == IRQF_TRIGGER_NONE) 1312d7058a79SJeff Kirsher dev_warn(db->dev, "WARNING: no IRQ resource flags set.\n"); 1313d7058a79SJeff Kirsher 1314d7058a79SJeff Kirsher irqflags |= IRQF_SHARED; 1315d7058a79SJeff Kirsher 1316d7058a79SJeff Kirsher /* GPIO0 on pre-activate PHY, Reg 1F is not set by reset */ 1317d7058a79SJeff Kirsher iow(db, DM9000_GPR, 0); /* REG_1F bit0 activate phyxcer */ 1318d7058a79SJeff Kirsher mdelay(1); /* delay needs by DM9000B */ 1319d7058a79SJeff Kirsher 1320d7058a79SJeff Kirsher /* Initialize DM9000 board */ 1321d7058a79SJeff Kirsher dm9000_init_dm9000(dev); 1322d7058a79SJeff Kirsher 1323d7058a79SJeff Kirsher if (request_irq(dev->irq, dm9000_interrupt, irqflags, dev->name, dev)) 1324d7058a79SJeff Kirsher return -EAGAIN; 132517ad78deSAndrew Ruder /* Now that we have an interrupt handler hooked up we can unmask 132617ad78deSAndrew Ruder * our interrupts 132717ad78deSAndrew Ruder */ 132817ad78deSAndrew Ruder dm9000_unmask_interrupts(db); 1329d7058a79SJeff Kirsher 1330d7058a79SJeff Kirsher /* Init driver variable */ 1331d7058a79SJeff Kirsher db->dbug_cnt = 0; 1332d7058a79SJeff Kirsher 1333d7058a79SJeff Kirsher mii_check_media(&db->mii, netif_msg_link(db), 1); 1334d7058a79SJeff Kirsher netif_start_queue(dev); 1335d7058a79SJeff Kirsher 1336aac6d022SAndrew Ruder /* Poll initial link status */ 1337aac6d022SAndrew Ruder schedule_delayed_work(&db->phy_poll, 1); 1338d7058a79SJeff Kirsher 1339d7058a79SJeff Kirsher return 0; 1340d7058a79SJeff Kirsher } 1341d7058a79SJeff Kirsher 1342d7058a79SJeff Kirsher static void 1343d7058a79SJeff Kirsher dm9000_shutdown(struct net_device *dev) 1344d7058a79SJeff Kirsher { 1345*2b162928SHimangi Saraogi struct board_info *db = netdev_priv(dev); 1346d7058a79SJeff Kirsher 1347d7058a79SJeff Kirsher /* RESET device */ 1348d7058a79SJeff Kirsher dm9000_phy_write(dev, 0, MII_BMCR, BMCR_RESET); /* PHY RESET */ 1349d7058a79SJeff Kirsher iow(db, DM9000_GPR, 0x01); /* Power-Down PHY */ 135017ad78deSAndrew Ruder dm9000_mask_interrupts(db); 1351d7058a79SJeff Kirsher iow(db, DM9000_RCR, 0x00); /* Disable RX */ 1352d7058a79SJeff Kirsher } 1353d7058a79SJeff Kirsher 1354d7058a79SJeff Kirsher /* 1355d7058a79SJeff Kirsher * Stop the interface. 1356d7058a79SJeff Kirsher * The interface is stopped when it is brought. 1357d7058a79SJeff Kirsher */ 1358d7058a79SJeff Kirsher static int 1359d7058a79SJeff Kirsher dm9000_stop(struct net_device *ndev) 1360d7058a79SJeff Kirsher { 1361*2b162928SHimangi Saraogi struct board_info *db = netdev_priv(ndev); 1362d7058a79SJeff Kirsher 1363d7058a79SJeff Kirsher if (netif_msg_ifdown(db)) 1364d7058a79SJeff Kirsher dev_dbg(db->dev, "shutting down %s\n", ndev->name); 1365d7058a79SJeff Kirsher 1366d7058a79SJeff Kirsher cancel_delayed_work_sync(&db->phy_poll); 1367d7058a79SJeff Kirsher 1368d7058a79SJeff Kirsher netif_stop_queue(ndev); 1369d7058a79SJeff Kirsher netif_carrier_off(ndev); 1370d7058a79SJeff Kirsher 1371d7058a79SJeff Kirsher /* free interrupt */ 1372d7058a79SJeff Kirsher free_irq(ndev->irq, ndev); 1373d7058a79SJeff Kirsher 1374d7058a79SJeff Kirsher dm9000_shutdown(ndev); 1375d7058a79SJeff Kirsher 1376d7058a79SJeff Kirsher return 0; 1377d7058a79SJeff Kirsher } 1378d7058a79SJeff Kirsher 1379d7058a79SJeff Kirsher static const struct net_device_ops dm9000_netdev_ops = { 1380d7058a79SJeff Kirsher .ndo_open = dm9000_open, 1381d7058a79SJeff Kirsher .ndo_stop = dm9000_stop, 1382d7058a79SJeff Kirsher .ndo_start_xmit = dm9000_start_xmit, 1383d7058a79SJeff Kirsher .ndo_tx_timeout = dm9000_timeout, 1384afc4b13dSJiri Pirko .ndo_set_rx_mode = dm9000_hash_table, 1385d7058a79SJeff Kirsher .ndo_do_ioctl = dm9000_ioctl, 1386d7058a79SJeff Kirsher .ndo_change_mtu = eth_change_mtu, 1387d7058a79SJeff Kirsher .ndo_set_features = dm9000_set_features, 1388d7058a79SJeff Kirsher .ndo_validate_addr = eth_validate_addr, 1389d7058a79SJeff Kirsher .ndo_set_mac_address = eth_mac_addr, 1390d7058a79SJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER 1391d7058a79SJeff Kirsher .ndo_poll_controller = dm9000_poll_controller, 1392d7058a79SJeff Kirsher #endif 1393d7058a79SJeff Kirsher }; 1394d7058a79SJeff Kirsher 13950b8bf1baSTomasz Figa static struct dm9000_plat_data *dm9000_parse_dt(struct device *dev) 13960b8bf1baSTomasz Figa { 13970b8bf1baSTomasz Figa struct dm9000_plat_data *pdata; 13980b8bf1baSTomasz Figa struct device_node *np = dev->of_node; 13990b8bf1baSTomasz Figa const void *mac_addr; 14000b8bf1baSTomasz Figa 14010b8bf1baSTomasz Figa if (!IS_ENABLED(CONFIG_OF) || !np) 14020b8bf1baSTomasz Figa return NULL; 14030b8bf1baSTomasz Figa 14040b8bf1baSTomasz Figa pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); 14050b8bf1baSTomasz Figa if (!pdata) 14060b8bf1baSTomasz Figa return ERR_PTR(-ENOMEM); 14070b8bf1baSTomasz Figa 14080b8bf1baSTomasz Figa if (of_find_property(np, "davicom,ext-phy", NULL)) 14090b8bf1baSTomasz Figa pdata->flags |= DM9000_PLATF_EXT_PHY; 14100b8bf1baSTomasz Figa if (of_find_property(np, "davicom,no-eeprom", NULL)) 14110b8bf1baSTomasz Figa pdata->flags |= DM9000_PLATF_NO_EEPROM; 14120b8bf1baSTomasz Figa 14130b8bf1baSTomasz Figa mac_addr = of_get_mac_address(np); 14140b8bf1baSTomasz Figa if (mac_addr) 14150b8bf1baSTomasz Figa memcpy(pdata->dev_addr, mac_addr, sizeof(pdata->dev_addr)); 14160b8bf1baSTomasz Figa 14170b8bf1baSTomasz Figa return pdata; 14180b8bf1baSTomasz Figa } 14190b8bf1baSTomasz Figa 1420d7058a79SJeff Kirsher /* 1421d7058a79SJeff Kirsher * Search DM9000 board, allocate space and register it 1422d7058a79SJeff Kirsher */ 14236b6a3e7fSBill Pemberton static int 1424d7058a79SJeff Kirsher dm9000_probe(struct platform_device *pdev) 1425d7058a79SJeff Kirsher { 1426cd4e2e4bSJingoo Han struct dm9000_plat_data *pdata = dev_get_platdata(&pdev->dev); 1427d7058a79SJeff Kirsher struct board_info *db; /* Point a board information structure */ 1428d7058a79SJeff Kirsher struct net_device *ndev; 1429d7058a79SJeff Kirsher const unsigned char *mac_src; 1430d7058a79SJeff Kirsher int ret = 0; 1431d7058a79SJeff Kirsher int iosize; 1432d7058a79SJeff Kirsher int i; 1433d7058a79SJeff Kirsher u32 id_val; 1434d7058a79SJeff Kirsher 14350b8bf1baSTomasz Figa if (!pdata) { 14360b8bf1baSTomasz Figa pdata = dm9000_parse_dt(&pdev->dev); 14370b8bf1baSTomasz Figa if (IS_ERR(pdata)) 14380b8bf1baSTomasz Figa return PTR_ERR(pdata); 14390b8bf1baSTomasz Figa } 14400b8bf1baSTomasz Figa 1441d7058a79SJeff Kirsher /* Init network device */ 1442d7058a79SJeff Kirsher ndev = alloc_etherdev(sizeof(struct board_info)); 144341de8d4cSJoe Perches if (!ndev) 1444d7058a79SJeff Kirsher return -ENOMEM; 1445d7058a79SJeff Kirsher 1446d7058a79SJeff Kirsher SET_NETDEV_DEV(ndev, &pdev->dev); 1447d7058a79SJeff Kirsher 1448d7058a79SJeff Kirsher dev_dbg(&pdev->dev, "dm9000_probe()\n"); 1449d7058a79SJeff Kirsher 1450d7058a79SJeff Kirsher /* setup board info structure */ 1451d7058a79SJeff Kirsher db = netdev_priv(ndev); 1452d7058a79SJeff Kirsher 1453d7058a79SJeff Kirsher db->dev = &pdev->dev; 1454d7058a79SJeff Kirsher db->ndev = ndev; 1455d7058a79SJeff Kirsher 1456d7058a79SJeff Kirsher spin_lock_init(&db->lock); 1457d7058a79SJeff Kirsher mutex_init(&db->addr_lock); 1458d7058a79SJeff Kirsher 1459d7058a79SJeff Kirsher INIT_DELAYED_WORK(&db->phy_poll, dm9000_poll_work); 1460d7058a79SJeff Kirsher 1461d7058a79SJeff Kirsher db->addr_res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1462d7058a79SJeff Kirsher db->data_res = platform_get_resource(pdev, IORESOURCE_MEM, 1); 1463d7058a79SJeff Kirsher db->irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); 1464d7058a79SJeff Kirsher 1465d7058a79SJeff Kirsher if (db->addr_res == NULL || db->data_res == NULL || 1466d7058a79SJeff Kirsher db->irq_res == NULL) { 1467d7058a79SJeff Kirsher dev_err(db->dev, "insufficient resources\n"); 1468d7058a79SJeff Kirsher ret = -ENOENT; 1469d7058a79SJeff Kirsher goto out; 1470d7058a79SJeff Kirsher } 1471d7058a79SJeff Kirsher 1472d7058a79SJeff Kirsher db->irq_wake = platform_get_irq(pdev, 1); 1473d7058a79SJeff Kirsher if (db->irq_wake >= 0) { 1474d7058a79SJeff Kirsher dev_dbg(db->dev, "wakeup irq %d\n", db->irq_wake); 1475d7058a79SJeff Kirsher 1476d7058a79SJeff Kirsher ret = request_irq(db->irq_wake, dm9000_wol_interrupt, 1477d7058a79SJeff Kirsher IRQF_SHARED, dev_name(db->dev), ndev); 1478d7058a79SJeff Kirsher if (ret) { 1479d7058a79SJeff Kirsher dev_err(db->dev, "cannot get wakeup irq (%d)\n", ret); 1480d7058a79SJeff Kirsher } else { 1481d7058a79SJeff Kirsher 1482d7058a79SJeff Kirsher /* test to see if irq is really wakeup capable */ 1483d7058a79SJeff Kirsher ret = irq_set_irq_wake(db->irq_wake, 1); 1484d7058a79SJeff Kirsher if (ret) { 1485d7058a79SJeff Kirsher dev_err(db->dev, "irq %d cannot set wakeup (%d)\n", 1486d7058a79SJeff Kirsher db->irq_wake, ret); 1487d7058a79SJeff Kirsher ret = 0; 1488d7058a79SJeff Kirsher } else { 1489d7058a79SJeff Kirsher irq_set_irq_wake(db->irq_wake, 0); 1490d7058a79SJeff Kirsher db->wake_supported = 1; 1491d7058a79SJeff Kirsher } 1492d7058a79SJeff Kirsher } 1493d7058a79SJeff Kirsher } 1494d7058a79SJeff Kirsher 1495d7058a79SJeff Kirsher iosize = resource_size(db->addr_res); 1496d7058a79SJeff Kirsher db->addr_req = request_mem_region(db->addr_res->start, iosize, 1497d7058a79SJeff Kirsher pdev->name); 1498d7058a79SJeff Kirsher 1499d7058a79SJeff Kirsher if (db->addr_req == NULL) { 1500d7058a79SJeff Kirsher dev_err(db->dev, "cannot claim address reg area\n"); 1501d7058a79SJeff Kirsher ret = -EIO; 1502d7058a79SJeff Kirsher goto out; 1503d7058a79SJeff Kirsher } 1504d7058a79SJeff Kirsher 1505d7058a79SJeff Kirsher db->io_addr = ioremap(db->addr_res->start, iosize); 1506d7058a79SJeff Kirsher 1507d7058a79SJeff Kirsher if (db->io_addr == NULL) { 1508d7058a79SJeff Kirsher dev_err(db->dev, "failed to ioremap address reg\n"); 1509d7058a79SJeff Kirsher ret = -EINVAL; 1510d7058a79SJeff Kirsher goto out; 1511d7058a79SJeff Kirsher } 1512d7058a79SJeff Kirsher 1513d7058a79SJeff Kirsher iosize = resource_size(db->data_res); 1514d7058a79SJeff Kirsher db->data_req = request_mem_region(db->data_res->start, iosize, 1515d7058a79SJeff Kirsher pdev->name); 1516d7058a79SJeff Kirsher 1517d7058a79SJeff Kirsher if (db->data_req == NULL) { 1518d7058a79SJeff Kirsher dev_err(db->dev, "cannot claim data reg area\n"); 1519d7058a79SJeff Kirsher ret = -EIO; 1520d7058a79SJeff Kirsher goto out; 1521d7058a79SJeff Kirsher } 1522d7058a79SJeff Kirsher 1523d7058a79SJeff Kirsher db->io_data = ioremap(db->data_res->start, iosize); 1524d7058a79SJeff Kirsher 1525d7058a79SJeff Kirsher if (db->io_data == NULL) { 1526d7058a79SJeff Kirsher dev_err(db->dev, "failed to ioremap data reg\n"); 1527d7058a79SJeff Kirsher ret = -EINVAL; 1528d7058a79SJeff Kirsher goto out; 1529d7058a79SJeff Kirsher } 1530d7058a79SJeff Kirsher 1531d7058a79SJeff Kirsher /* fill in parameters for net-dev structure */ 1532d7058a79SJeff Kirsher ndev->base_addr = (unsigned long)db->io_addr; 1533d7058a79SJeff Kirsher ndev->irq = db->irq_res->start; 1534d7058a79SJeff Kirsher 1535d7058a79SJeff Kirsher /* ensure at least we have a default set of IO routines */ 1536d7058a79SJeff Kirsher dm9000_set_io(db, iosize); 1537d7058a79SJeff Kirsher 1538d7058a79SJeff Kirsher /* check to see if anything is being over-ridden */ 1539d7058a79SJeff Kirsher if (pdata != NULL) { 1540d7058a79SJeff Kirsher /* check to see if the driver wants to over-ride the 1541d7058a79SJeff Kirsher * default IO width */ 1542d7058a79SJeff Kirsher 1543d7058a79SJeff Kirsher if (pdata->flags & DM9000_PLATF_8BITONLY) 1544d7058a79SJeff Kirsher dm9000_set_io(db, 1); 1545d7058a79SJeff Kirsher 1546d7058a79SJeff Kirsher if (pdata->flags & DM9000_PLATF_16BITONLY) 1547d7058a79SJeff Kirsher dm9000_set_io(db, 2); 1548d7058a79SJeff Kirsher 1549d7058a79SJeff Kirsher if (pdata->flags & DM9000_PLATF_32BITONLY) 1550d7058a79SJeff Kirsher dm9000_set_io(db, 4); 1551d7058a79SJeff Kirsher 1552d7058a79SJeff Kirsher /* check to see if there are any IO routine 1553d7058a79SJeff Kirsher * over-rides */ 1554d7058a79SJeff Kirsher 1555d7058a79SJeff Kirsher if (pdata->inblk != NULL) 1556d7058a79SJeff Kirsher db->inblk = pdata->inblk; 1557d7058a79SJeff Kirsher 1558d7058a79SJeff Kirsher if (pdata->outblk != NULL) 1559d7058a79SJeff Kirsher db->outblk = pdata->outblk; 1560d7058a79SJeff Kirsher 1561d7058a79SJeff Kirsher if (pdata->dumpblk != NULL) 1562d7058a79SJeff Kirsher db->dumpblk = pdata->dumpblk; 1563d7058a79SJeff Kirsher 1564d7058a79SJeff Kirsher db->flags = pdata->flags; 1565d7058a79SJeff Kirsher } 1566d7058a79SJeff Kirsher 1567d7058a79SJeff Kirsher #ifdef CONFIG_DM9000_FORCE_SIMPLE_PHY_POLL 1568d7058a79SJeff Kirsher db->flags |= DM9000_PLATF_SIMPLE_PHY; 1569d7058a79SJeff Kirsher #endif 1570d7058a79SJeff Kirsher 1571751bb6fdSAndrew Ruder dm9000_reset(db); 1572d7058a79SJeff Kirsher 1573d7058a79SJeff Kirsher /* try multiple times, DM9000 sometimes gets the read wrong */ 1574d7058a79SJeff Kirsher for (i = 0; i < 8; i++) { 1575d7058a79SJeff Kirsher id_val = ior(db, DM9000_VIDL); 1576d7058a79SJeff Kirsher id_val |= (u32)ior(db, DM9000_VIDH) << 8; 1577d7058a79SJeff Kirsher id_val |= (u32)ior(db, DM9000_PIDL) << 16; 1578d7058a79SJeff Kirsher id_val |= (u32)ior(db, DM9000_PIDH) << 24; 1579d7058a79SJeff Kirsher 1580d7058a79SJeff Kirsher if (id_val == DM9000_ID) 1581d7058a79SJeff Kirsher break; 1582d7058a79SJeff Kirsher dev_err(db->dev, "read wrong id 0x%08x\n", id_val); 1583d7058a79SJeff Kirsher } 1584d7058a79SJeff Kirsher 1585d7058a79SJeff Kirsher if (id_val != DM9000_ID) { 1586d7058a79SJeff Kirsher dev_err(db->dev, "wrong id: 0x%08x\n", id_val); 1587d7058a79SJeff Kirsher ret = -ENODEV; 1588d7058a79SJeff Kirsher goto out; 1589d7058a79SJeff Kirsher } 1590d7058a79SJeff Kirsher 1591d7058a79SJeff Kirsher /* Identify what type of DM9000 we are working on */ 1592d7058a79SJeff Kirsher 1593d7058a79SJeff Kirsher id_val = ior(db, DM9000_CHIPR); 1594d7058a79SJeff Kirsher dev_dbg(db->dev, "dm9000 revision 0x%02x\n", id_val); 1595d7058a79SJeff Kirsher 1596d7058a79SJeff Kirsher switch (id_val) { 1597d7058a79SJeff Kirsher case CHIPR_DM9000A: 1598d7058a79SJeff Kirsher db->type = TYPE_DM9000A; 1599d7058a79SJeff Kirsher break; 1600d7058a79SJeff Kirsher case CHIPR_DM9000B: 1601d7058a79SJeff Kirsher db->type = TYPE_DM9000B; 1602d7058a79SJeff Kirsher break; 1603d7058a79SJeff Kirsher default: 1604d7058a79SJeff Kirsher dev_dbg(db->dev, "ID %02x => defaulting to DM9000E\n", id_val); 1605d7058a79SJeff Kirsher db->type = TYPE_DM9000E; 1606d7058a79SJeff Kirsher } 1607d7058a79SJeff Kirsher 1608d7058a79SJeff Kirsher /* dm9000a/b are capable of hardware checksum offload */ 1609d7058a79SJeff Kirsher if (db->type == TYPE_DM9000A || db->type == TYPE_DM9000B) { 1610d7058a79SJeff Kirsher ndev->hw_features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM; 1611d7058a79SJeff Kirsher ndev->features |= ndev->hw_features; 1612d7058a79SJeff Kirsher } 1613d7058a79SJeff Kirsher 1614d7058a79SJeff Kirsher /* from this point we assume that we have found a DM9000 */ 1615d7058a79SJeff Kirsher 1616d7058a79SJeff Kirsher /* driver system function */ 1617d7058a79SJeff Kirsher ether_setup(ndev); 1618d7058a79SJeff Kirsher 1619d7058a79SJeff Kirsher ndev->netdev_ops = &dm9000_netdev_ops; 1620d7058a79SJeff Kirsher ndev->watchdog_timeo = msecs_to_jiffies(watchdog); 1621d7058a79SJeff Kirsher ndev->ethtool_ops = &dm9000_ethtool_ops; 1622d7058a79SJeff Kirsher 1623d7058a79SJeff Kirsher db->msg_enable = NETIF_MSG_LINK; 1624d7058a79SJeff Kirsher db->mii.phy_id_mask = 0x1f; 1625d7058a79SJeff Kirsher db->mii.reg_num_mask = 0x1f; 1626d7058a79SJeff Kirsher db->mii.force_media = 0; 1627d7058a79SJeff Kirsher db->mii.full_duplex = 0; 1628d7058a79SJeff Kirsher db->mii.dev = ndev; 1629d7058a79SJeff Kirsher db->mii.mdio_read = dm9000_phy_read; 1630d7058a79SJeff Kirsher db->mii.mdio_write = dm9000_phy_write; 1631d7058a79SJeff Kirsher 1632d7058a79SJeff Kirsher mac_src = "eeprom"; 1633d7058a79SJeff Kirsher 1634d7058a79SJeff Kirsher /* try reading the node address from the attached EEPROM */ 1635d7058a79SJeff Kirsher for (i = 0; i < 6; i += 2) 1636d7058a79SJeff Kirsher dm9000_read_eeprom(db, i / 2, ndev->dev_addr+i); 1637d7058a79SJeff Kirsher 1638d7058a79SJeff Kirsher if (!is_valid_ether_addr(ndev->dev_addr) && pdata != NULL) { 1639d7058a79SJeff Kirsher mac_src = "platform data"; 1640d458cdf7SJoe Perches memcpy(ndev->dev_addr, pdata->dev_addr, ETH_ALEN); 1641d7058a79SJeff Kirsher } 1642d7058a79SJeff Kirsher 1643d7058a79SJeff Kirsher if (!is_valid_ether_addr(ndev->dev_addr)) { 1644d7058a79SJeff Kirsher /* try reading from mac */ 1645d7058a79SJeff Kirsher 1646d7058a79SJeff Kirsher mac_src = "chip"; 1647d7058a79SJeff Kirsher for (i = 0; i < 6; i++) 1648d7058a79SJeff Kirsher ndev->dev_addr[i] = ior(db, i+DM9000_PAR); 1649d7058a79SJeff Kirsher } 1650d7058a79SJeff Kirsher 1651d7058a79SJeff Kirsher if (!is_valid_ether_addr(ndev->dev_addr)) { 1652d7058a79SJeff Kirsher dev_warn(db->dev, "%s: Invalid ethernet MAC address. Please " 1653d7058a79SJeff Kirsher "set using ifconfig\n", ndev->name); 1654d7058a79SJeff Kirsher 1655f2cedb63SDanny Kukawka eth_hw_addr_random(ndev); 1656d7058a79SJeff Kirsher mac_src = "random"; 1657d7058a79SJeff Kirsher } 1658d7058a79SJeff Kirsher 1659d7058a79SJeff Kirsher 1660d7058a79SJeff Kirsher platform_set_drvdata(pdev, ndev); 1661d7058a79SJeff Kirsher ret = register_netdev(ndev); 1662d7058a79SJeff Kirsher 1663d7058a79SJeff Kirsher if (ret == 0) 1664d7058a79SJeff Kirsher printk(KERN_INFO "%s: dm9000%c at %p,%p IRQ %d MAC: %pM (%s)\n", 1665d7058a79SJeff Kirsher ndev->name, dm9000_type_to_char(db->type), 1666d7058a79SJeff Kirsher db->io_addr, db->io_data, ndev->irq, 1667d7058a79SJeff Kirsher ndev->dev_addr, mac_src); 1668d7058a79SJeff Kirsher return 0; 1669d7058a79SJeff Kirsher 1670d7058a79SJeff Kirsher out: 1671d7058a79SJeff Kirsher dev_err(db->dev, "not found (%d).\n", ret); 1672d7058a79SJeff Kirsher 1673d7058a79SJeff Kirsher dm9000_release_board(pdev, db); 1674d7058a79SJeff Kirsher free_netdev(ndev); 1675d7058a79SJeff Kirsher 1676d7058a79SJeff Kirsher return ret; 1677d7058a79SJeff Kirsher } 1678d7058a79SJeff Kirsher 1679d7058a79SJeff Kirsher static int 1680d7058a79SJeff Kirsher dm9000_drv_suspend(struct device *dev) 1681d7058a79SJeff Kirsher { 1682d7058a79SJeff Kirsher struct platform_device *pdev = to_platform_device(dev); 1683d7058a79SJeff Kirsher struct net_device *ndev = platform_get_drvdata(pdev); 1684*2b162928SHimangi Saraogi struct board_info *db; 1685d7058a79SJeff Kirsher 1686d7058a79SJeff Kirsher if (ndev) { 1687d7058a79SJeff Kirsher db = netdev_priv(ndev); 1688d7058a79SJeff Kirsher db->in_suspend = 1; 1689d7058a79SJeff Kirsher 1690d7058a79SJeff Kirsher if (!netif_running(ndev)) 1691d7058a79SJeff Kirsher return 0; 1692d7058a79SJeff Kirsher 1693d7058a79SJeff Kirsher netif_device_detach(ndev); 1694d7058a79SJeff Kirsher 1695d7058a79SJeff Kirsher /* only shutdown if not using WoL */ 1696d7058a79SJeff Kirsher if (!db->wake_state) 1697d7058a79SJeff Kirsher dm9000_shutdown(ndev); 1698d7058a79SJeff Kirsher } 1699d7058a79SJeff Kirsher return 0; 1700d7058a79SJeff Kirsher } 1701d7058a79SJeff Kirsher 1702d7058a79SJeff Kirsher static int 1703d7058a79SJeff Kirsher dm9000_drv_resume(struct device *dev) 1704d7058a79SJeff Kirsher { 1705d7058a79SJeff Kirsher struct platform_device *pdev = to_platform_device(dev); 1706d7058a79SJeff Kirsher struct net_device *ndev = platform_get_drvdata(pdev); 1707*2b162928SHimangi Saraogi struct board_info *db = netdev_priv(ndev); 1708d7058a79SJeff Kirsher 1709d7058a79SJeff Kirsher if (ndev) { 1710d7058a79SJeff Kirsher if (netif_running(ndev)) { 1711d7058a79SJeff Kirsher /* reset if we were not in wake mode to ensure if 1712d7058a79SJeff Kirsher * the device was powered off it is in a known state */ 1713d7058a79SJeff Kirsher if (!db->wake_state) { 1714d7058a79SJeff Kirsher dm9000_init_dm9000(ndev); 171517ad78deSAndrew Ruder dm9000_unmask_interrupts(db); 1716d7058a79SJeff Kirsher } 1717d7058a79SJeff Kirsher 1718d7058a79SJeff Kirsher netif_device_attach(ndev); 1719d7058a79SJeff Kirsher } 1720d7058a79SJeff Kirsher 1721d7058a79SJeff Kirsher db->in_suspend = 0; 1722d7058a79SJeff Kirsher } 1723d7058a79SJeff Kirsher return 0; 1724d7058a79SJeff Kirsher } 1725d7058a79SJeff Kirsher 1726d7058a79SJeff Kirsher static const struct dev_pm_ops dm9000_drv_pm_ops = { 1727d7058a79SJeff Kirsher .suspend = dm9000_drv_suspend, 1728d7058a79SJeff Kirsher .resume = dm9000_drv_resume, 1729d7058a79SJeff Kirsher }; 1730d7058a79SJeff Kirsher 17316b6a3e7fSBill Pemberton static int 1732d7058a79SJeff Kirsher dm9000_drv_remove(struct platform_device *pdev) 1733d7058a79SJeff Kirsher { 1734d7058a79SJeff Kirsher struct net_device *ndev = platform_get_drvdata(pdev); 1735d7058a79SJeff Kirsher 1736d7058a79SJeff Kirsher unregister_netdev(ndev); 1737d7058a79SJeff Kirsher dm9000_release_board(pdev, netdev_priv(ndev)); 1738d7058a79SJeff Kirsher free_netdev(ndev); /* free device structure */ 1739d7058a79SJeff Kirsher 1740d7058a79SJeff Kirsher dev_dbg(&pdev->dev, "released and freed device\n"); 1741d7058a79SJeff Kirsher return 0; 1742d7058a79SJeff Kirsher } 1743d7058a79SJeff Kirsher 17440b8bf1baSTomasz Figa #ifdef CONFIG_OF 17450b8bf1baSTomasz Figa static const struct of_device_id dm9000_of_matches[] = { 17460b8bf1baSTomasz Figa { .compatible = "davicom,dm9000", }, 17470b8bf1baSTomasz Figa { /* sentinel */ } 17480b8bf1baSTomasz Figa }; 17490b8bf1baSTomasz Figa MODULE_DEVICE_TABLE(of, dm9000_of_matches); 17500b8bf1baSTomasz Figa #endif 17510b8bf1baSTomasz Figa 1752d7058a79SJeff Kirsher static struct platform_driver dm9000_driver = { 1753d7058a79SJeff Kirsher .driver = { 1754d7058a79SJeff Kirsher .name = "dm9000", 1755d7058a79SJeff Kirsher .owner = THIS_MODULE, 1756d7058a79SJeff Kirsher .pm = &dm9000_drv_pm_ops, 17570b8bf1baSTomasz Figa .of_match_table = of_match_ptr(dm9000_of_matches), 1758d7058a79SJeff Kirsher }, 1759d7058a79SJeff Kirsher .probe = dm9000_probe, 17606b6a3e7fSBill Pemberton .remove = dm9000_drv_remove, 1761d7058a79SJeff Kirsher }; 1762d7058a79SJeff Kirsher 1763a8f9c3e4SSachin Kamat module_platform_driver(dm9000_driver); 1764d7058a79SJeff Kirsher 1765d7058a79SJeff Kirsher MODULE_AUTHOR("Sascha Hauer, Ben Dooks"); 1766d7058a79SJeff Kirsher MODULE_DESCRIPTION("Davicom DM9000 network driver"); 1767d7058a79SJeff Kirsher MODULE_LICENSE("GPL"); 1768d7058a79SJeff Kirsher MODULE_ALIAS("platform:dm9000"); 1769