1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2d7058a79SJeff Kirsher /* 3d7058a79SJeff Kirsher * Davicom DM9000 Fast Ethernet driver for Linux. 4d7058a79SJeff Kirsher * Copyright (C) 1997 Sten Wang 5d7058a79SJeff Kirsher * 6d7058a79SJeff Kirsher * (C) Copyright 1997-1998 DAVICOM Semiconductor,Inc. All Rights Reserved. 7d7058a79SJeff Kirsher * 8d7058a79SJeff Kirsher * Additional updates, Copyright: 9d7058a79SJeff Kirsher * Ben Dooks <ben@simtec.co.uk> 10d7058a79SJeff Kirsher * Sascha Hauer <s.hauer@pengutronix.de> 11d7058a79SJeff Kirsher */ 12d7058a79SJeff Kirsher 13d7058a79SJeff Kirsher #include <linux/module.h> 14d7058a79SJeff Kirsher #include <linux/ioport.h> 15d7058a79SJeff Kirsher #include <linux/netdevice.h> 16d7058a79SJeff Kirsher #include <linux/etherdevice.h> 17d7058a79SJeff Kirsher #include <linux/interrupt.h> 18d7058a79SJeff Kirsher #include <linux/skbuff.h> 19d7058a79SJeff Kirsher #include <linux/spinlock.h> 20d7058a79SJeff Kirsher #include <linux/crc32.h> 21d7058a79SJeff Kirsher #include <linux/mii.h> 220b8bf1baSTomasz Figa #include <linux/of.h> 230b8bf1baSTomasz Figa #include <linux/of_net.h> 24d7058a79SJeff Kirsher #include <linux/ethtool.h> 25d7058a79SJeff Kirsher #include <linux/dm9000.h> 26d7058a79SJeff Kirsher #include <linux/delay.h> 27d7058a79SJeff Kirsher #include <linux/platform_device.h> 28d7058a79SJeff Kirsher #include <linux/irq.h> 29d7058a79SJeff Kirsher #include <linux/slab.h> 307994fe55SZubair Lutfullah Kakakhel #include <linux/regulator/consumer.h> 31db49ca38SDmitry Torokhov #include <linux/gpio/consumer.h> 32d7058a79SJeff Kirsher 33d7058a79SJeff Kirsher #include <asm/delay.h> 34d7058a79SJeff Kirsher #include <asm/irq.h> 35d7058a79SJeff Kirsher #include <asm/io.h> 36d7058a79SJeff Kirsher 37d7058a79SJeff Kirsher #include "dm9000.h" 38d7058a79SJeff Kirsher 39d7058a79SJeff Kirsher /* Board/System/Debug information/definition ---------------- */ 40d7058a79SJeff Kirsher 41d7058a79SJeff Kirsher #define DM9000_PHY 0x40 /* PHY address 0x01 */ 42d7058a79SJeff Kirsher 43d7058a79SJeff Kirsher #define CARDNAME "dm9000" 44d7058a79SJeff Kirsher 45d7058a79SJeff Kirsher /* 46d7058a79SJeff Kirsher * Transmit timeout, default 5 seconds. 47d7058a79SJeff Kirsher */ 48d7058a79SJeff Kirsher static int watchdog = 5000; 49d7058a79SJeff Kirsher module_param(watchdog, int, 0400); 50d7058a79SJeff Kirsher MODULE_PARM_DESC(watchdog, "transmit timeout in milliseconds"); 51d7058a79SJeff Kirsher 522e025c71SVladimir Zapolskiy /* 532e025c71SVladimir Zapolskiy * Debug messages level 542e025c71SVladimir Zapolskiy */ 552e025c71SVladimir Zapolskiy static int debug; 562e025c71SVladimir Zapolskiy module_param(debug, int, 0644); 570fdbedc7SMathieu Malaterre MODULE_PARM_DESC(debug, "dm9000 debug level (0-6)"); 582e025c71SVladimir Zapolskiy 59d7058a79SJeff Kirsher /* DM9000 register address locking. 60d7058a79SJeff Kirsher * 61d7058a79SJeff Kirsher * The DM9000 uses an address register to control where data written 62d7058a79SJeff Kirsher * to the data register goes. This means that the address register 63d7058a79SJeff Kirsher * must be preserved over interrupts or similar calls. 64d7058a79SJeff Kirsher * 65d7058a79SJeff Kirsher * During interrupt and other critical calls, a spinlock is used to 66d7058a79SJeff Kirsher * protect the system, but the calls themselves save the address 67d7058a79SJeff Kirsher * in the address register in case they are interrupting another 68d7058a79SJeff Kirsher * access to the device. 69d7058a79SJeff Kirsher * 70d7058a79SJeff Kirsher * For general accesses a lock is provided so that calls which are 71d7058a79SJeff Kirsher * allowed to sleep are serialised so that the address register does 72d7058a79SJeff Kirsher * not need to be saved. This lock also serves to serialise access 73d7058a79SJeff Kirsher * to the EEPROM and PHY access registers which are shared between 74d7058a79SJeff Kirsher * these two devices. 75d7058a79SJeff Kirsher */ 76d7058a79SJeff Kirsher 77d7058a79SJeff Kirsher /* The driver supports the original DM9000E, and now the two newer 78d7058a79SJeff Kirsher * devices, DM9000A and DM9000B. 79d7058a79SJeff Kirsher */ 80d7058a79SJeff Kirsher 81d7058a79SJeff Kirsher enum dm9000_type { 82d7058a79SJeff Kirsher TYPE_DM9000E, /* original DM9000 */ 83d7058a79SJeff Kirsher TYPE_DM9000A, 84d7058a79SJeff Kirsher TYPE_DM9000B 85d7058a79SJeff Kirsher }; 86d7058a79SJeff Kirsher 87d7058a79SJeff Kirsher /* Structure/enum declaration ------------------------------- */ 882b162928SHimangi Saraogi struct board_info { 89d7058a79SJeff Kirsher 90d7058a79SJeff Kirsher void __iomem *io_addr; /* Register I/O base address */ 91d7058a79SJeff Kirsher void __iomem *io_data; /* Data I/O address */ 92d7058a79SJeff Kirsher u16 irq; /* IRQ */ 93d7058a79SJeff Kirsher 94d7058a79SJeff Kirsher u16 tx_pkt_cnt; 95d7058a79SJeff Kirsher u16 queue_pkt_len; 96d7058a79SJeff Kirsher u16 queue_start_addr; 97d7058a79SJeff Kirsher u16 queue_ip_summed; 98d7058a79SJeff Kirsher u16 dbug_cnt; 99d7058a79SJeff Kirsher u8 io_mode; /* 0:word, 2:byte */ 100d7058a79SJeff Kirsher u8 phy_addr; 101d7058a79SJeff Kirsher u8 imr_all; 102d7058a79SJeff Kirsher 103d7058a79SJeff Kirsher unsigned int flags; 10458237983SAndrew Ruder unsigned int in_timeout:1; 105d7058a79SJeff Kirsher unsigned int in_suspend:1; 106d7058a79SJeff Kirsher unsigned int wake_supported:1; 107d7058a79SJeff Kirsher 108d7058a79SJeff Kirsher enum dm9000_type type; 109d7058a79SJeff Kirsher 110d7058a79SJeff Kirsher void (*inblk)(void __iomem *port, void *data, int length); 111d7058a79SJeff Kirsher void (*outblk)(void __iomem *port, void *data, int length); 112d7058a79SJeff Kirsher void (*dumpblk)(void __iomem *port, int length); 113d7058a79SJeff Kirsher 114d7058a79SJeff Kirsher struct device *dev; /* parent device */ 115d7058a79SJeff Kirsher 116d7058a79SJeff Kirsher struct resource *addr_res; /* resources found */ 117d7058a79SJeff Kirsher struct resource *data_res; 118d7058a79SJeff Kirsher struct resource *addr_req; /* resources requested */ 119d7058a79SJeff Kirsher struct resource *data_req; 120d7058a79SJeff Kirsher 121d7058a79SJeff Kirsher int irq_wake; 122d7058a79SJeff Kirsher 123d7058a79SJeff Kirsher struct mutex addr_lock; /* phy and eeprom access lock */ 124d7058a79SJeff Kirsher 125d7058a79SJeff Kirsher struct delayed_work phy_poll; 126d7058a79SJeff Kirsher struct net_device *ndev; 127d7058a79SJeff Kirsher 128d7058a79SJeff Kirsher spinlock_t lock; 129d7058a79SJeff Kirsher 130d7058a79SJeff Kirsher struct mii_if_info mii; 131d7058a79SJeff Kirsher u32 msg_enable; 132d7058a79SJeff Kirsher u32 wake_state; 133d7058a79SJeff Kirsher 134d7058a79SJeff Kirsher int ip_summed; 135cf9e60aaSPaul Cercueil 136cf9e60aaSPaul Cercueil struct regulator *power_supply; 1372b162928SHimangi Saraogi }; 138d7058a79SJeff Kirsher 139d7058a79SJeff Kirsher /* debug code */ 140d7058a79SJeff Kirsher 141d7058a79SJeff Kirsher #define dm9000_dbg(db, lev, msg...) do { \ 1422e025c71SVladimir Zapolskiy if ((lev) < debug) { \ 143d7058a79SJeff Kirsher dev_dbg(db->dev, msg); \ 144d7058a79SJeff Kirsher } \ 145d7058a79SJeff Kirsher } while (0) 146d7058a79SJeff Kirsher 1472b162928SHimangi Saraogi static inline struct board_info *to_dm9000_board(struct net_device *dev) 148d7058a79SJeff Kirsher { 149d7058a79SJeff Kirsher return netdev_priv(dev); 150d7058a79SJeff Kirsher } 151d7058a79SJeff Kirsher 152d7058a79SJeff Kirsher /* DM9000 network board routine ---------------------------- */ 153d7058a79SJeff Kirsher 154d7058a79SJeff Kirsher /* 155d7058a79SJeff Kirsher * Read a byte from I/O port 156d7058a79SJeff Kirsher */ 157d7058a79SJeff Kirsher static u8 1582b162928SHimangi Saraogi ior(struct board_info *db, int reg) 159d7058a79SJeff Kirsher { 160d7058a79SJeff Kirsher writeb(reg, db->io_addr); 161d7058a79SJeff Kirsher return readb(db->io_data); 162d7058a79SJeff Kirsher } 163d7058a79SJeff Kirsher 164d7058a79SJeff Kirsher /* 165d7058a79SJeff Kirsher * Write a byte to I/O port 166d7058a79SJeff Kirsher */ 167d7058a79SJeff Kirsher 168d7058a79SJeff Kirsher static void 1692b162928SHimangi Saraogi iow(struct board_info *db, int reg, int value) 170d7058a79SJeff Kirsher { 171d7058a79SJeff Kirsher writeb(reg, db->io_addr); 172d7058a79SJeff Kirsher writeb(value, db->io_data); 173d7058a79SJeff Kirsher } 174d7058a79SJeff Kirsher 17509ee9f87SMichael Abbott static void 1762b162928SHimangi Saraogi dm9000_reset(struct board_info *db) 17709ee9f87SMichael Abbott { 17809ee9f87SMichael Abbott dev_dbg(db->dev, "resetting device\n"); 17909ee9f87SMichael Abbott 18009ee9f87SMichael Abbott /* Reset DM9000, see DM9000 Application Notes V1.22 Jun 11, 2004 page 29 18109ee9f87SMichael Abbott * The essential point is that we have to do a double reset, and the 18209ee9f87SMichael Abbott * instruction is to set LBK into MAC internal loopback mode. 18309ee9f87SMichael Abbott */ 184751bb6fdSAndrew Ruder iow(db, DM9000_NCR, NCR_RST | NCR_MAC_LBK); 18509ee9f87SMichael Abbott udelay(100); /* Application note says at least 20 us */ 18609ee9f87SMichael Abbott if (ior(db, DM9000_NCR) & 1) 18709ee9f87SMichael Abbott dev_err(db->dev, "dm9000 did not respond to first reset\n"); 18809ee9f87SMichael Abbott 18909ee9f87SMichael Abbott iow(db, DM9000_NCR, 0); 190751bb6fdSAndrew Ruder iow(db, DM9000_NCR, NCR_RST | NCR_MAC_LBK); 19109ee9f87SMichael Abbott udelay(100); 19209ee9f87SMichael Abbott if (ior(db, DM9000_NCR) & 1) 19309ee9f87SMichael Abbott dev_err(db->dev, "dm9000 did not respond to second reset\n"); 19409ee9f87SMichael Abbott } 19509ee9f87SMichael Abbott 196d7058a79SJeff Kirsher /* routines for sending block to chip */ 197d7058a79SJeff Kirsher 198d7058a79SJeff Kirsher static void dm9000_outblk_8bit(void __iomem *reg, void *data, int count) 199d7058a79SJeff Kirsher { 200daadaf6fSMatthew Leach iowrite8_rep(reg, data, count); 201d7058a79SJeff Kirsher } 202d7058a79SJeff Kirsher 203d7058a79SJeff Kirsher static void dm9000_outblk_16bit(void __iomem *reg, void *data, int count) 204d7058a79SJeff Kirsher { 205daadaf6fSMatthew Leach iowrite16_rep(reg, data, (count+1) >> 1); 206d7058a79SJeff Kirsher } 207d7058a79SJeff Kirsher 208d7058a79SJeff Kirsher static void dm9000_outblk_32bit(void __iomem *reg, void *data, int count) 209d7058a79SJeff Kirsher { 210daadaf6fSMatthew Leach iowrite32_rep(reg, data, (count+3) >> 2); 211d7058a79SJeff Kirsher } 212d7058a79SJeff Kirsher 213d7058a79SJeff Kirsher /* input block from chip to memory */ 214d7058a79SJeff Kirsher 215d7058a79SJeff Kirsher static void dm9000_inblk_8bit(void __iomem *reg, void *data, int count) 216d7058a79SJeff Kirsher { 217daadaf6fSMatthew Leach ioread8_rep(reg, data, count); 218d7058a79SJeff Kirsher } 219d7058a79SJeff Kirsher 220d7058a79SJeff Kirsher 221d7058a79SJeff Kirsher static void dm9000_inblk_16bit(void __iomem *reg, void *data, int count) 222d7058a79SJeff Kirsher { 223daadaf6fSMatthew Leach ioread16_rep(reg, data, (count+1) >> 1); 224d7058a79SJeff Kirsher } 225d7058a79SJeff Kirsher 226d7058a79SJeff Kirsher static void dm9000_inblk_32bit(void __iomem *reg, void *data, int count) 227d7058a79SJeff Kirsher { 228daadaf6fSMatthew Leach ioread32_rep(reg, data, (count+3) >> 2); 229d7058a79SJeff Kirsher } 230d7058a79SJeff Kirsher 231d7058a79SJeff Kirsher /* dump block from chip to null */ 232d7058a79SJeff Kirsher 233d7058a79SJeff Kirsher static void dm9000_dumpblk_8bit(void __iomem *reg, int count) 234d7058a79SJeff Kirsher { 235d7058a79SJeff Kirsher int i; 236d7058a79SJeff Kirsher 237d7058a79SJeff Kirsher for (i = 0; i < count; i++) 2383858632aSAndrew Lunn readb(reg); 239d7058a79SJeff Kirsher } 240d7058a79SJeff Kirsher 241d7058a79SJeff Kirsher static void dm9000_dumpblk_16bit(void __iomem *reg, int count) 242d7058a79SJeff Kirsher { 243d7058a79SJeff Kirsher int i; 244d7058a79SJeff Kirsher 245d7058a79SJeff Kirsher count = (count + 1) >> 1; 246d7058a79SJeff Kirsher 247d7058a79SJeff Kirsher for (i = 0; i < count; i++) 2483858632aSAndrew Lunn readw(reg); 249d7058a79SJeff Kirsher } 250d7058a79SJeff Kirsher 251d7058a79SJeff Kirsher static void dm9000_dumpblk_32bit(void __iomem *reg, int count) 252d7058a79SJeff Kirsher { 253d7058a79SJeff Kirsher int i; 254d7058a79SJeff Kirsher 255d7058a79SJeff Kirsher count = (count + 3) >> 2; 256d7058a79SJeff Kirsher 257d7058a79SJeff Kirsher for (i = 0; i < count; i++) 2583858632aSAndrew Lunn readl(reg); 259d7058a79SJeff Kirsher } 260d7058a79SJeff Kirsher 2616741f40dSJoseph CHANG /* 2626741f40dSJoseph CHANG * Sleep, either by using msleep() or if we are suspending, then 2636741f40dSJoseph CHANG * use mdelay() to sleep. 2646741f40dSJoseph CHANG */ 2652b162928SHimangi Saraogi static void dm9000_msleep(struct board_info *db, unsigned int ms) 2666741f40dSJoseph CHANG { 26758237983SAndrew Ruder if (db->in_suspend || db->in_timeout) 2686741f40dSJoseph CHANG mdelay(ms); 2696741f40dSJoseph CHANG else 2706741f40dSJoseph CHANG msleep(ms); 2716741f40dSJoseph CHANG } 2726741f40dSJoseph CHANG 2736741f40dSJoseph CHANG /* Read a word from phyxcer */ 2746741f40dSJoseph CHANG static int 2756741f40dSJoseph CHANG dm9000_phy_read(struct net_device *dev, int phy_reg_unused, int reg) 2766741f40dSJoseph CHANG { 2772b162928SHimangi Saraogi struct board_info *db = netdev_priv(dev); 2786741f40dSJoseph CHANG unsigned long flags; 2796741f40dSJoseph CHANG unsigned int reg_save; 2806741f40dSJoseph CHANG int ret; 2816741f40dSJoseph CHANG 2826741f40dSJoseph CHANG mutex_lock(&db->addr_lock); 2836741f40dSJoseph CHANG 2846741f40dSJoseph CHANG spin_lock_irqsave(&db->lock, flags); 2856741f40dSJoseph CHANG 2866741f40dSJoseph CHANG /* Save previous register address */ 2876741f40dSJoseph CHANG reg_save = readb(db->io_addr); 2886741f40dSJoseph CHANG 2896741f40dSJoseph CHANG /* Fill the phyxcer register into REG_0C */ 2906741f40dSJoseph CHANG iow(db, DM9000_EPAR, DM9000_PHY | reg); 2916741f40dSJoseph CHANG 2926741f40dSJoseph CHANG /* Issue phyxcer read command */ 2936741f40dSJoseph CHANG iow(db, DM9000_EPCR, EPCR_ERPRR | EPCR_EPOS); 2946741f40dSJoseph CHANG 2956741f40dSJoseph CHANG writeb(reg_save, db->io_addr); 2966741f40dSJoseph CHANG spin_unlock_irqrestore(&db->lock, flags); 2976741f40dSJoseph CHANG 2986741f40dSJoseph CHANG dm9000_msleep(db, 1); /* Wait read complete */ 2996741f40dSJoseph CHANG 3006741f40dSJoseph CHANG spin_lock_irqsave(&db->lock, flags); 3016741f40dSJoseph CHANG reg_save = readb(db->io_addr); 3026741f40dSJoseph CHANG 3036741f40dSJoseph CHANG iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer read command */ 3046741f40dSJoseph CHANG 3056741f40dSJoseph CHANG /* The read data keeps on REG_0D & REG_0E */ 3066741f40dSJoseph CHANG ret = (ior(db, DM9000_EPDRH) << 8) | ior(db, DM9000_EPDRL); 3076741f40dSJoseph CHANG 3086741f40dSJoseph CHANG /* restore the previous address */ 3096741f40dSJoseph CHANG writeb(reg_save, db->io_addr); 3106741f40dSJoseph CHANG spin_unlock_irqrestore(&db->lock, flags); 3116741f40dSJoseph CHANG 3126741f40dSJoseph CHANG mutex_unlock(&db->addr_lock); 3136741f40dSJoseph CHANG 3146741f40dSJoseph CHANG dm9000_dbg(db, 5, "phy_read[%02x] -> %04x\n", reg, ret); 3156741f40dSJoseph CHANG return ret; 3166741f40dSJoseph CHANG } 3176741f40dSJoseph CHANG 3186741f40dSJoseph CHANG /* Write a word to phyxcer */ 3196741f40dSJoseph CHANG static void 3206741f40dSJoseph CHANG dm9000_phy_write(struct net_device *dev, 3216741f40dSJoseph CHANG int phyaddr_unused, int reg, int value) 3226741f40dSJoseph CHANG { 3232b162928SHimangi Saraogi struct board_info *db = netdev_priv(dev); 3246741f40dSJoseph CHANG unsigned long flags; 3256741f40dSJoseph CHANG unsigned long reg_save; 3266741f40dSJoseph CHANG 3276741f40dSJoseph CHANG dm9000_dbg(db, 5, "phy_write[%02x] = %04x\n", reg, value); 32858237983SAndrew Ruder if (!db->in_timeout) 3296741f40dSJoseph CHANG mutex_lock(&db->addr_lock); 3306741f40dSJoseph CHANG 3316741f40dSJoseph CHANG spin_lock_irqsave(&db->lock, flags); 3326741f40dSJoseph CHANG 3336741f40dSJoseph CHANG /* Save previous register address */ 3346741f40dSJoseph CHANG reg_save = readb(db->io_addr); 3356741f40dSJoseph CHANG 3366741f40dSJoseph CHANG /* Fill the phyxcer register into REG_0C */ 3376741f40dSJoseph CHANG iow(db, DM9000_EPAR, DM9000_PHY | reg); 3386741f40dSJoseph CHANG 3396741f40dSJoseph CHANG /* Fill the written data into REG_0D & REG_0E */ 3406741f40dSJoseph CHANG iow(db, DM9000_EPDRL, value); 3416741f40dSJoseph CHANG iow(db, DM9000_EPDRH, value >> 8); 3426741f40dSJoseph CHANG 3436741f40dSJoseph CHANG /* Issue phyxcer write command */ 3446741f40dSJoseph CHANG iow(db, DM9000_EPCR, EPCR_EPOS | EPCR_ERPRW); 3456741f40dSJoseph CHANG 3466741f40dSJoseph CHANG writeb(reg_save, db->io_addr); 3476741f40dSJoseph CHANG spin_unlock_irqrestore(&db->lock, flags); 3486741f40dSJoseph CHANG 3496741f40dSJoseph CHANG dm9000_msleep(db, 1); /* Wait write complete */ 3506741f40dSJoseph CHANG 3516741f40dSJoseph CHANG spin_lock_irqsave(&db->lock, flags); 3526741f40dSJoseph CHANG reg_save = readb(db->io_addr); 3536741f40dSJoseph CHANG 3546741f40dSJoseph CHANG iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer write command */ 3556741f40dSJoseph CHANG 3566741f40dSJoseph CHANG /* restore the previous address */ 3576741f40dSJoseph CHANG writeb(reg_save, db->io_addr); 3586741f40dSJoseph CHANG 3596741f40dSJoseph CHANG spin_unlock_irqrestore(&db->lock, flags); 36058237983SAndrew Ruder if (!db->in_timeout) 3616741f40dSJoseph CHANG mutex_unlock(&db->addr_lock); 3626741f40dSJoseph CHANG } 3636741f40dSJoseph CHANG 364d7058a79SJeff Kirsher /* dm9000_set_io 365d7058a79SJeff Kirsher * 366d7058a79SJeff Kirsher * select the specified set of io routines to use with the 367d7058a79SJeff Kirsher * device 368d7058a79SJeff Kirsher */ 369d7058a79SJeff Kirsher 370d7058a79SJeff Kirsher static void dm9000_set_io(struct board_info *db, int byte_width) 371d7058a79SJeff Kirsher { 372d7058a79SJeff Kirsher /* use the size of the data resource to work out what IO 373d7058a79SJeff Kirsher * routines we want to use 374d7058a79SJeff Kirsher */ 375d7058a79SJeff Kirsher 376d7058a79SJeff Kirsher switch (byte_width) { 377d7058a79SJeff Kirsher case 1: 378d7058a79SJeff Kirsher db->dumpblk = dm9000_dumpblk_8bit; 379d7058a79SJeff Kirsher db->outblk = dm9000_outblk_8bit; 380d7058a79SJeff Kirsher db->inblk = dm9000_inblk_8bit; 381d7058a79SJeff Kirsher break; 382d7058a79SJeff Kirsher 383d7058a79SJeff Kirsher 384d7058a79SJeff Kirsher case 3: 385d7058a79SJeff Kirsher dev_dbg(db->dev, ": 3 byte IO, falling back to 16bit\n"); 386df561f66SGustavo A. R. Silva fallthrough; 387d7058a79SJeff Kirsher case 2: 388d7058a79SJeff Kirsher db->dumpblk = dm9000_dumpblk_16bit; 389d7058a79SJeff Kirsher db->outblk = dm9000_outblk_16bit; 390d7058a79SJeff Kirsher db->inblk = dm9000_inblk_16bit; 391d7058a79SJeff Kirsher break; 392d7058a79SJeff Kirsher 393d7058a79SJeff Kirsher case 4: 394d7058a79SJeff Kirsher default: 395d7058a79SJeff Kirsher db->dumpblk = dm9000_dumpblk_32bit; 396d7058a79SJeff Kirsher db->outblk = dm9000_outblk_32bit; 397d7058a79SJeff Kirsher db->inblk = dm9000_inblk_32bit; 398d7058a79SJeff Kirsher break; 399d7058a79SJeff Kirsher } 400d7058a79SJeff Kirsher } 401d7058a79SJeff Kirsher 4022b162928SHimangi Saraogi static void dm9000_schedule_poll(struct board_info *db) 403d7058a79SJeff Kirsher { 404d7058a79SJeff Kirsher if (db->type == TYPE_DM9000E) 405d7058a79SJeff Kirsher schedule_delayed_work(&db->phy_poll, HZ * 2); 406d7058a79SJeff Kirsher } 407d7058a79SJeff Kirsher 408d7058a79SJeff Kirsher static int dm9000_ioctl(struct net_device *dev, struct ifreq *req, int cmd) 409d7058a79SJeff Kirsher { 4102b162928SHimangi Saraogi struct board_info *dm = to_dm9000_board(dev); 411d7058a79SJeff Kirsher 412d7058a79SJeff Kirsher if (!netif_running(dev)) 413d7058a79SJeff Kirsher return -EINVAL; 414d7058a79SJeff Kirsher 415d7058a79SJeff Kirsher return generic_mii_ioctl(&dm->mii, if_mii(req), cmd, NULL); 416d7058a79SJeff Kirsher } 417d7058a79SJeff Kirsher 418d7058a79SJeff Kirsher static unsigned int 4192b162928SHimangi Saraogi dm9000_read_locked(struct board_info *db, int reg) 420d7058a79SJeff Kirsher { 421d7058a79SJeff Kirsher unsigned long flags; 422d7058a79SJeff Kirsher unsigned int ret; 423d7058a79SJeff Kirsher 424d7058a79SJeff Kirsher spin_lock_irqsave(&db->lock, flags); 425d7058a79SJeff Kirsher ret = ior(db, reg); 426d7058a79SJeff Kirsher spin_unlock_irqrestore(&db->lock, flags); 427d7058a79SJeff Kirsher 428d7058a79SJeff Kirsher return ret; 429d7058a79SJeff Kirsher } 430d7058a79SJeff Kirsher 4312b162928SHimangi Saraogi static int dm9000_wait_eeprom(struct board_info *db) 432d7058a79SJeff Kirsher { 433d7058a79SJeff Kirsher unsigned int status; 434d7058a79SJeff Kirsher int timeout = 8; /* wait max 8msec */ 435d7058a79SJeff Kirsher 436d7058a79SJeff Kirsher /* The DM9000 data sheets say we should be able to 437d7058a79SJeff Kirsher * poll the ERRE bit in EPCR to wait for the EEPROM 438d7058a79SJeff Kirsher * operation. From testing several chips, this bit 439d7058a79SJeff Kirsher * does not seem to work. 440d7058a79SJeff Kirsher * 441d7058a79SJeff Kirsher * We attempt to use the bit, but fall back to the 442d7058a79SJeff Kirsher * timeout (which is why we do not return an error 443d7058a79SJeff Kirsher * on expiry) to say that the EEPROM operation has 444d7058a79SJeff Kirsher * completed. 445d7058a79SJeff Kirsher */ 446d7058a79SJeff Kirsher 447d7058a79SJeff Kirsher while (1) { 448d7058a79SJeff Kirsher status = dm9000_read_locked(db, DM9000_EPCR); 449d7058a79SJeff Kirsher 450d7058a79SJeff Kirsher if ((status & EPCR_ERRE) == 0) 451d7058a79SJeff Kirsher break; 452d7058a79SJeff Kirsher 453d7058a79SJeff Kirsher msleep(1); 454d7058a79SJeff Kirsher 455d7058a79SJeff Kirsher if (timeout-- < 0) { 456d7058a79SJeff Kirsher dev_dbg(db->dev, "timeout waiting EEPROM\n"); 457d7058a79SJeff Kirsher break; 458d7058a79SJeff Kirsher } 459d7058a79SJeff Kirsher } 460d7058a79SJeff Kirsher 461d7058a79SJeff Kirsher return 0; 462d7058a79SJeff Kirsher } 463d7058a79SJeff Kirsher 464d7058a79SJeff Kirsher /* 465d7058a79SJeff Kirsher * Read a word data from EEPROM 466d7058a79SJeff Kirsher */ 467d7058a79SJeff Kirsher static void 4682b162928SHimangi Saraogi dm9000_read_eeprom(struct board_info *db, int offset, u8 *to) 469d7058a79SJeff Kirsher { 470d7058a79SJeff Kirsher unsigned long flags; 471d7058a79SJeff Kirsher 472d7058a79SJeff Kirsher if (db->flags & DM9000_PLATF_NO_EEPROM) { 473d7058a79SJeff Kirsher to[0] = 0xff; 474d7058a79SJeff Kirsher to[1] = 0xff; 475d7058a79SJeff Kirsher return; 476d7058a79SJeff Kirsher } 477d7058a79SJeff Kirsher 478d7058a79SJeff Kirsher mutex_lock(&db->addr_lock); 479d7058a79SJeff Kirsher 480d7058a79SJeff Kirsher spin_lock_irqsave(&db->lock, flags); 481d7058a79SJeff Kirsher 482d7058a79SJeff Kirsher iow(db, DM9000_EPAR, offset); 483d7058a79SJeff Kirsher iow(db, DM9000_EPCR, EPCR_ERPRR); 484d7058a79SJeff Kirsher 485d7058a79SJeff Kirsher spin_unlock_irqrestore(&db->lock, flags); 486d7058a79SJeff Kirsher 487d7058a79SJeff Kirsher dm9000_wait_eeprom(db); 488d7058a79SJeff Kirsher 489d7058a79SJeff Kirsher /* delay for at-least 150uS */ 490d7058a79SJeff Kirsher msleep(1); 491d7058a79SJeff Kirsher 492d7058a79SJeff Kirsher spin_lock_irqsave(&db->lock, flags); 493d7058a79SJeff Kirsher 494d7058a79SJeff Kirsher iow(db, DM9000_EPCR, 0x0); 495d7058a79SJeff Kirsher 496d7058a79SJeff Kirsher to[0] = ior(db, DM9000_EPDRL); 497d7058a79SJeff Kirsher to[1] = ior(db, DM9000_EPDRH); 498d7058a79SJeff Kirsher 499d7058a79SJeff Kirsher spin_unlock_irqrestore(&db->lock, flags); 500d7058a79SJeff Kirsher 501d7058a79SJeff Kirsher mutex_unlock(&db->addr_lock); 502d7058a79SJeff Kirsher } 503d7058a79SJeff Kirsher 504d7058a79SJeff Kirsher /* 505d7058a79SJeff Kirsher * Write a word data to SROM 506d7058a79SJeff Kirsher */ 507d7058a79SJeff Kirsher static void 5082b162928SHimangi Saraogi dm9000_write_eeprom(struct board_info *db, int offset, u8 *data) 509d7058a79SJeff Kirsher { 510d7058a79SJeff Kirsher unsigned long flags; 511d7058a79SJeff Kirsher 512d7058a79SJeff Kirsher if (db->flags & DM9000_PLATF_NO_EEPROM) 513d7058a79SJeff Kirsher return; 514d7058a79SJeff Kirsher 515d7058a79SJeff Kirsher mutex_lock(&db->addr_lock); 516d7058a79SJeff Kirsher 517d7058a79SJeff Kirsher spin_lock_irqsave(&db->lock, flags); 518d7058a79SJeff Kirsher iow(db, DM9000_EPAR, offset); 519d7058a79SJeff Kirsher iow(db, DM9000_EPDRH, data[1]); 520d7058a79SJeff Kirsher iow(db, DM9000_EPDRL, data[0]); 521d7058a79SJeff Kirsher iow(db, DM9000_EPCR, EPCR_WEP | EPCR_ERPRW); 522d7058a79SJeff Kirsher spin_unlock_irqrestore(&db->lock, flags); 523d7058a79SJeff Kirsher 524d7058a79SJeff Kirsher dm9000_wait_eeprom(db); 525d7058a79SJeff Kirsher 526d7058a79SJeff Kirsher mdelay(1); /* wait at least 150uS to clear */ 527d7058a79SJeff Kirsher 528d7058a79SJeff Kirsher spin_lock_irqsave(&db->lock, flags); 529d7058a79SJeff Kirsher iow(db, DM9000_EPCR, 0); 530d7058a79SJeff Kirsher spin_unlock_irqrestore(&db->lock, flags); 531d7058a79SJeff Kirsher 532d7058a79SJeff Kirsher mutex_unlock(&db->addr_lock); 533d7058a79SJeff Kirsher } 534d7058a79SJeff Kirsher 535d7058a79SJeff Kirsher /* ethtool ops */ 536d7058a79SJeff Kirsher 537d7058a79SJeff Kirsher static void dm9000_get_drvinfo(struct net_device *dev, 538d7058a79SJeff Kirsher struct ethtool_drvinfo *info) 539d7058a79SJeff Kirsher { 5402b162928SHimangi Saraogi struct board_info *dm = to_dm9000_board(dev); 541d7058a79SJeff Kirsher 542f029c781SWolfram Sang strscpy(info->driver, CARDNAME, sizeof(info->driver)); 543f029c781SWolfram Sang strscpy(info->bus_info, to_platform_device(dm->dev)->name, 5447826d43fSJiri Pirko sizeof(info->bus_info)); 545d7058a79SJeff Kirsher } 546d7058a79SJeff Kirsher 547d7058a79SJeff Kirsher static u32 dm9000_get_msglevel(struct net_device *dev) 548d7058a79SJeff Kirsher { 5492b162928SHimangi Saraogi struct board_info *dm = to_dm9000_board(dev); 550d7058a79SJeff Kirsher 551d7058a79SJeff Kirsher return dm->msg_enable; 552d7058a79SJeff Kirsher } 553d7058a79SJeff Kirsher 554d7058a79SJeff Kirsher static void dm9000_set_msglevel(struct net_device *dev, u32 value) 555d7058a79SJeff Kirsher { 5562b162928SHimangi Saraogi struct board_info *dm = to_dm9000_board(dev); 557d7058a79SJeff Kirsher 558d7058a79SJeff Kirsher dm->msg_enable = value; 559d7058a79SJeff Kirsher } 560d7058a79SJeff Kirsher 56199bff5eeSPhilippe Reynes static int dm9000_get_link_ksettings(struct net_device *dev, 56299bff5eeSPhilippe Reynes struct ethtool_link_ksettings *cmd) 563d7058a79SJeff Kirsher { 5642b162928SHimangi Saraogi struct board_info *dm = to_dm9000_board(dev); 565d7058a79SJeff Kirsher 56699bff5eeSPhilippe Reynes mii_ethtool_get_link_ksettings(&dm->mii, cmd); 567d7058a79SJeff Kirsher return 0; 568d7058a79SJeff Kirsher } 569d7058a79SJeff Kirsher 57099bff5eeSPhilippe Reynes static int dm9000_set_link_ksettings(struct net_device *dev, 57199bff5eeSPhilippe Reynes const struct ethtool_link_ksettings *cmd) 572d7058a79SJeff Kirsher { 5732b162928SHimangi Saraogi struct board_info *dm = to_dm9000_board(dev); 574d7058a79SJeff Kirsher 57599bff5eeSPhilippe Reynes return mii_ethtool_set_link_ksettings(&dm->mii, cmd); 576d7058a79SJeff Kirsher } 577d7058a79SJeff Kirsher 578d7058a79SJeff Kirsher static int dm9000_nway_reset(struct net_device *dev) 579d7058a79SJeff Kirsher { 5802b162928SHimangi Saraogi struct board_info *dm = to_dm9000_board(dev); 581d7058a79SJeff Kirsher return mii_nway_restart(&dm->mii); 582d7058a79SJeff Kirsher } 583d7058a79SJeff Kirsher 584c8f44affSMichał Mirosław static int dm9000_set_features(struct net_device *dev, 585c8f44affSMichał Mirosław netdev_features_t features) 586d7058a79SJeff Kirsher { 5872b162928SHimangi Saraogi struct board_info *dm = to_dm9000_board(dev); 588c8f44affSMichał Mirosław netdev_features_t changed = dev->features ^ features; 589d7058a79SJeff Kirsher unsigned long flags; 590d7058a79SJeff Kirsher 591d7058a79SJeff Kirsher if (!(changed & NETIF_F_RXCSUM)) 592d7058a79SJeff Kirsher return 0; 593d7058a79SJeff Kirsher 594d7058a79SJeff Kirsher spin_lock_irqsave(&dm->lock, flags); 595d7058a79SJeff Kirsher iow(dm, DM9000_RCSR, (features & NETIF_F_RXCSUM) ? RCSR_CSUM : 0); 596d7058a79SJeff Kirsher spin_unlock_irqrestore(&dm->lock, flags); 597d7058a79SJeff Kirsher 598d7058a79SJeff Kirsher return 0; 599d7058a79SJeff Kirsher } 600d7058a79SJeff Kirsher 601d7058a79SJeff Kirsher static u32 dm9000_get_link(struct net_device *dev) 602d7058a79SJeff Kirsher { 6032b162928SHimangi Saraogi struct board_info *dm = to_dm9000_board(dev); 604d7058a79SJeff Kirsher u32 ret; 605d7058a79SJeff Kirsher 606d7058a79SJeff Kirsher if (dm->flags & DM9000_PLATF_EXT_PHY) 607d7058a79SJeff Kirsher ret = mii_link_ok(&dm->mii); 608d7058a79SJeff Kirsher else 609d7058a79SJeff Kirsher ret = dm9000_read_locked(dm, DM9000_NSR) & NSR_LINKST ? 1 : 0; 610d7058a79SJeff Kirsher 611d7058a79SJeff Kirsher return ret; 612d7058a79SJeff Kirsher } 613d7058a79SJeff Kirsher 614d7058a79SJeff Kirsher #define DM_EEPROM_MAGIC (0x444D394B) 615d7058a79SJeff Kirsher 616d7058a79SJeff Kirsher static int dm9000_get_eeprom_len(struct net_device *dev) 617d7058a79SJeff Kirsher { 618d7058a79SJeff Kirsher return 128; 619d7058a79SJeff Kirsher } 620d7058a79SJeff Kirsher 621d7058a79SJeff Kirsher static int dm9000_get_eeprom(struct net_device *dev, 622d7058a79SJeff Kirsher struct ethtool_eeprom *ee, u8 *data) 623d7058a79SJeff Kirsher { 6242b162928SHimangi Saraogi struct board_info *dm = to_dm9000_board(dev); 625d7058a79SJeff Kirsher int offset = ee->offset; 626d7058a79SJeff Kirsher int len = ee->len; 627d7058a79SJeff Kirsher int i; 628d7058a79SJeff Kirsher 629d7058a79SJeff Kirsher /* EEPROM access is aligned to two bytes */ 630d7058a79SJeff Kirsher 631d7058a79SJeff Kirsher if ((len & 1) != 0 || (offset & 1) != 0) 632d7058a79SJeff Kirsher return -EINVAL; 633d7058a79SJeff Kirsher 634d7058a79SJeff Kirsher if (dm->flags & DM9000_PLATF_NO_EEPROM) 635d7058a79SJeff Kirsher return -ENOENT; 636d7058a79SJeff Kirsher 637d7058a79SJeff Kirsher ee->magic = DM_EEPROM_MAGIC; 638d7058a79SJeff Kirsher 639d7058a79SJeff Kirsher for (i = 0; i < len; i += 2) 640d7058a79SJeff Kirsher dm9000_read_eeprom(dm, (offset + i) / 2, data + i); 641d7058a79SJeff Kirsher 642d7058a79SJeff Kirsher return 0; 643d7058a79SJeff Kirsher } 644d7058a79SJeff Kirsher 645d7058a79SJeff Kirsher static int dm9000_set_eeprom(struct net_device *dev, 646d7058a79SJeff Kirsher struct ethtool_eeprom *ee, u8 *data) 647d7058a79SJeff Kirsher { 6482b162928SHimangi Saraogi struct board_info *dm = to_dm9000_board(dev); 649d7058a79SJeff Kirsher int offset = ee->offset; 650d7058a79SJeff Kirsher int len = ee->len; 651d7058a79SJeff Kirsher int done; 652d7058a79SJeff Kirsher 653d7058a79SJeff Kirsher /* EEPROM access is aligned to two bytes */ 654d7058a79SJeff Kirsher 655d7058a79SJeff Kirsher if (dm->flags & DM9000_PLATF_NO_EEPROM) 656d7058a79SJeff Kirsher return -ENOENT; 657d7058a79SJeff Kirsher 658d7058a79SJeff Kirsher if (ee->magic != DM_EEPROM_MAGIC) 659d7058a79SJeff Kirsher return -EINVAL; 660d7058a79SJeff Kirsher 661d7058a79SJeff Kirsher while (len > 0) { 662d7058a79SJeff Kirsher if (len & 1 || offset & 1) { 663d7058a79SJeff Kirsher int which = offset & 1; 664d7058a79SJeff Kirsher u8 tmp[2]; 665d7058a79SJeff Kirsher 666d7058a79SJeff Kirsher dm9000_read_eeprom(dm, offset / 2, tmp); 667d7058a79SJeff Kirsher tmp[which] = *data; 668d7058a79SJeff Kirsher dm9000_write_eeprom(dm, offset / 2, tmp); 669d7058a79SJeff Kirsher 670d7058a79SJeff Kirsher done = 1; 671d7058a79SJeff Kirsher } else { 672d7058a79SJeff Kirsher dm9000_write_eeprom(dm, offset / 2, data); 673d7058a79SJeff Kirsher done = 2; 674d7058a79SJeff Kirsher } 675d7058a79SJeff Kirsher 676d7058a79SJeff Kirsher data += done; 677d7058a79SJeff Kirsher offset += done; 678d7058a79SJeff Kirsher len -= done; 679d7058a79SJeff Kirsher } 680d7058a79SJeff Kirsher 681d7058a79SJeff Kirsher return 0; 682d7058a79SJeff Kirsher } 683d7058a79SJeff Kirsher 684d7058a79SJeff Kirsher static void dm9000_get_wol(struct net_device *dev, struct ethtool_wolinfo *w) 685d7058a79SJeff Kirsher { 6862b162928SHimangi Saraogi struct board_info *dm = to_dm9000_board(dev); 687d7058a79SJeff Kirsher 688d7058a79SJeff Kirsher memset(w, 0, sizeof(struct ethtool_wolinfo)); 689d7058a79SJeff Kirsher 690d7058a79SJeff Kirsher /* note, we could probably support wake-phy too */ 691d7058a79SJeff Kirsher w->supported = dm->wake_supported ? WAKE_MAGIC : 0; 692d7058a79SJeff Kirsher w->wolopts = dm->wake_state; 693d7058a79SJeff Kirsher } 694d7058a79SJeff Kirsher 695d7058a79SJeff Kirsher static int dm9000_set_wol(struct net_device *dev, struct ethtool_wolinfo *w) 696d7058a79SJeff Kirsher { 6972b162928SHimangi Saraogi struct board_info *dm = to_dm9000_board(dev); 698d7058a79SJeff Kirsher unsigned long flags; 699d7058a79SJeff Kirsher u32 opts = w->wolopts; 700d7058a79SJeff Kirsher u32 wcr = 0; 701d7058a79SJeff Kirsher 702d7058a79SJeff Kirsher if (!dm->wake_supported) 703d7058a79SJeff Kirsher return -EOPNOTSUPP; 704d7058a79SJeff Kirsher 705d7058a79SJeff Kirsher if (opts & ~WAKE_MAGIC) 706d7058a79SJeff Kirsher return -EINVAL; 707d7058a79SJeff Kirsher 708d7058a79SJeff Kirsher if (opts & WAKE_MAGIC) 709d7058a79SJeff Kirsher wcr |= WCR_MAGICEN; 710d7058a79SJeff Kirsher 711d7058a79SJeff Kirsher mutex_lock(&dm->addr_lock); 712d7058a79SJeff Kirsher 713d7058a79SJeff Kirsher spin_lock_irqsave(&dm->lock, flags); 714d7058a79SJeff Kirsher iow(dm, DM9000_WCR, wcr); 715d7058a79SJeff Kirsher spin_unlock_irqrestore(&dm->lock, flags); 716d7058a79SJeff Kirsher 717d7058a79SJeff Kirsher mutex_unlock(&dm->addr_lock); 718d7058a79SJeff Kirsher 719d7058a79SJeff Kirsher if (dm->wake_state != opts) { 720d7058a79SJeff Kirsher /* change in wol state, update IRQ state */ 721d7058a79SJeff Kirsher 722d7058a79SJeff Kirsher if (!dm->wake_state) 723d7058a79SJeff Kirsher irq_set_irq_wake(dm->irq_wake, 1); 72483b98fb4SMark Brown else if (dm->wake_state && !opts) 725d7058a79SJeff Kirsher irq_set_irq_wake(dm->irq_wake, 0); 726d7058a79SJeff Kirsher } 727d7058a79SJeff Kirsher 728d7058a79SJeff Kirsher dm->wake_state = opts; 729d7058a79SJeff Kirsher return 0; 730d7058a79SJeff Kirsher } 731d7058a79SJeff Kirsher 732d7058a79SJeff Kirsher static const struct ethtool_ops dm9000_ethtool_ops = { 733d7058a79SJeff Kirsher .get_drvinfo = dm9000_get_drvinfo, 734d7058a79SJeff Kirsher .get_msglevel = dm9000_get_msglevel, 735d7058a79SJeff Kirsher .set_msglevel = dm9000_set_msglevel, 736d7058a79SJeff Kirsher .nway_reset = dm9000_nway_reset, 737d7058a79SJeff Kirsher .get_link = dm9000_get_link, 738d7058a79SJeff Kirsher .get_wol = dm9000_get_wol, 739d7058a79SJeff Kirsher .set_wol = dm9000_set_wol, 740d7058a79SJeff Kirsher .get_eeprom_len = dm9000_get_eeprom_len, 741d7058a79SJeff Kirsher .get_eeprom = dm9000_get_eeprom, 742d7058a79SJeff Kirsher .set_eeprom = dm9000_set_eeprom, 74399bff5eeSPhilippe Reynes .get_link_ksettings = dm9000_get_link_ksettings, 74499bff5eeSPhilippe Reynes .set_link_ksettings = dm9000_set_link_ksettings, 745d7058a79SJeff Kirsher }; 746d7058a79SJeff Kirsher 7472b162928SHimangi Saraogi static void dm9000_show_carrier(struct board_info *db, 748d7058a79SJeff Kirsher unsigned carrier, unsigned nsr) 749d7058a79SJeff Kirsher { 750727a282fSNikita Kiryanov int lpa; 751d7058a79SJeff Kirsher struct net_device *ndev = db->ndev; 752727a282fSNikita Kiryanov struct mii_if_info *mii = &db->mii; 753d7058a79SJeff Kirsher unsigned ncr = dm9000_read_locked(db, DM9000_NCR); 754d7058a79SJeff Kirsher 755727a282fSNikita Kiryanov if (carrier) { 756727a282fSNikita Kiryanov lpa = mii->mdio_read(mii->dev, mii->phy_id, MII_LPA); 757727a282fSNikita Kiryanov dev_info(db->dev, 758727a282fSNikita Kiryanov "%s: link up, %dMbps, %s-duplex, lpa 0x%04X\n", 759d7058a79SJeff Kirsher ndev->name, (nsr & NSR_SPEED) ? 10 : 100, 760727a282fSNikita Kiryanov (ncr & NCR_FDX) ? "full" : "half", lpa); 761727a282fSNikita Kiryanov } else { 762d7058a79SJeff Kirsher dev_info(db->dev, "%s: link down\n", ndev->name); 763d7058a79SJeff Kirsher } 764727a282fSNikita Kiryanov } 765d7058a79SJeff Kirsher 766d7058a79SJeff Kirsher static void 767d7058a79SJeff Kirsher dm9000_poll_work(struct work_struct *w) 768d7058a79SJeff Kirsher { 769d7058a79SJeff Kirsher struct delayed_work *dw = to_delayed_work(w); 7702b162928SHimangi Saraogi struct board_info *db = container_of(dw, struct board_info, phy_poll); 771d7058a79SJeff Kirsher struct net_device *ndev = db->ndev; 772d7058a79SJeff Kirsher 773d7058a79SJeff Kirsher if (db->flags & DM9000_PLATF_SIMPLE_PHY && 774d7058a79SJeff Kirsher !(db->flags & DM9000_PLATF_EXT_PHY)) { 775d7058a79SJeff Kirsher unsigned nsr = dm9000_read_locked(db, DM9000_NSR); 776d7058a79SJeff Kirsher unsigned old_carrier = netif_carrier_ok(ndev) ? 1 : 0; 777d7058a79SJeff Kirsher unsigned new_carrier; 778d7058a79SJeff Kirsher 779d7058a79SJeff Kirsher new_carrier = (nsr & NSR_LINKST) ? 1 : 0; 780d7058a79SJeff Kirsher 781d7058a79SJeff Kirsher if (old_carrier != new_carrier) { 782d7058a79SJeff Kirsher if (netif_msg_link(db)) 783d7058a79SJeff Kirsher dm9000_show_carrier(db, new_carrier, nsr); 784d7058a79SJeff Kirsher 785d7058a79SJeff Kirsher if (!new_carrier) 786d7058a79SJeff Kirsher netif_carrier_off(ndev); 787d7058a79SJeff Kirsher else 788d7058a79SJeff Kirsher netif_carrier_on(ndev); 789d7058a79SJeff Kirsher } 790d7058a79SJeff Kirsher } else 791d7058a79SJeff Kirsher mii_check_media(&db->mii, netif_msg_link(db), 0); 792d7058a79SJeff Kirsher 793d7058a79SJeff Kirsher if (netif_running(ndev)) 794d7058a79SJeff Kirsher dm9000_schedule_poll(db); 795d7058a79SJeff Kirsher } 796d7058a79SJeff Kirsher 797d7058a79SJeff Kirsher /* dm9000_release_board 798d7058a79SJeff Kirsher * 799d7058a79SJeff Kirsher * release a board, and any mapped resources 800d7058a79SJeff Kirsher */ 801d7058a79SJeff Kirsher 802d7058a79SJeff Kirsher static void 803d7058a79SJeff Kirsher dm9000_release_board(struct platform_device *pdev, struct board_info *db) 804d7058a79SJeff Kirsher { 805d7058a79SJeff Kirsher /* unmap our resources */ 806d7058a79SJeff Kirsher 807d7058a79SJeff Kirsher iounmap(db->io_addr); 808d7058a79SJeff Kirsher iounmap(db->io_data); 809d7058a79SJeff Kirsher 810d7058a79SJeff Kirsher /* release the resources */ 811d7058a79SJeff Kirsher 812a5536e10SDan Carpenter if (db->data_req) 813d7058a79SJeff Kirsher release_resource(db->data_req); 814d7058a79SJeff Kirsher kfree(db->data_req); 815d7058a79SJeff Kirsher 816a5536e10SDan Carpenter if (db->addr_req) 817d7058a79SJeff Kirsher release_resource(db->addr_req); 818d7058a79SJeff Kirsher kfree(db->addr_req); 819d7058a79SJeff Kirsher } 820d7058a79SJeff Kirsher 821d7058a79SJeff Kirsher static unsigned char dm9000_type_to_char(enum dm9000_type type) 822d7058a79SJeff Kirsher { 823d7058a79SJeff Kirsher switch (type) { 824d7058a79SJeff Kirsher case TYPE_DM9000E: return 'e'; 825d7058a79SJeff Kirsher case TYPE_DM9000A: return 'a'; 826d7058a79SJeff Kirsher case TYPE_DM9000B: return 'b'; 827d7058a79SJeff Kirsher } 828d7058a79SJeff Kirsher 829d7058a79SJeff Kirsher return '?'; 830d7058a79SJeff Kirsher } 831d7058a79SJeff Kirsher 832d7058a79SJeff Kirsher /* 833d7058a79SJeff Kirsher * Set DM9000 multicast address 834d7058a79SJeff Kirsher */ 835d7058a79SJeff Kirsher static void 836d7058a79SJeff Kirsher dm9000_hash_table_unlocked(struct net_device *dev) 837d7058a79SJeff Kirsher { 8382b162928SHimangi Saraogi struct board_info *db = netdev_priv(dev); 839d7058a79SJeff Kirsher struct netdev_hw_addr *ha; 840d7058a79SJeff Kirsher int i, oft; 841d7058a79SJeff Kirsher u32 hash_val; 84235e729acSEmilio López u16 hash_table[4] = { 0, 0, 0, 0x8000 }; /* broadcast address */ 843d7058a79SJeff Kirsher u8 rcr = RCR_DIS_LONG | RCR_DIS_CRC | RCR_RXEN; 844d7058a79SJeff Kirsher 845d7058a79SJeff Kirsher dm9000_dbg(db, 1, "entering %s\n", __func__); 846d7058a79SJeff Kirsher 847d7058a79SJeff Kirsher for (i = 0, oft = DM9000_PAR; i < 6; i++, oft++) 848d7058a79SJeff Kirsher iow(db, oft, dev->dev_addr[i]); 849d7058a79SJeff Kirsher 850d7058a79SJeff Kirsher if (dev->flags & IFF_PROMISC) 851d7058a79SJeff Kirsher rcr |= RCR_PRMSC; 852d7058a79SJeff Kirsher 853d7058a79SJeff Kirsher if (dev->flags & IFF_ALLMULTI) 854d7058a79SJeff Kirsher rcr |= RCR_ALL; 855d7058a79SJeff Kirsher 856d7058a79SJeff Kirsher /* the multicast address in Hash Table : 64 bits */ 857d7058a79SJeff Kirsher netdev_for_each_mc_addr(ha, dev) { 858d7058a79SJeff Kirsher hash_val = ether_crc_le(6, ha->addr) & 0x3f; 859d7058a79SJeff Kirsher hash_table[hash_val / 16] |= (u16) 1 << (hash_val % 16); 860d7058a79SJeff Kirsher } 861d7058a79SJeff Kirsher 862d7058a79SJeff Kirsher /* Write the hash table to MAC MD table */ 863d7058a79SJeff Kirsher for (i = 0, oft = DM9000_MAR; i < 4; i++) { 864d7058a79SJeff Kirsher iow(db, oft++, hash_table[i]); 865d7058a79SJeff Kirsher iow(db, oft++, hash_table[i] >> 8); 866d7058a79SJeff Kirsher } 867d7058a79SJeff Kirsher 868d7058a79SJeff Kirsher iow(db, DM9000_RCR, rcr); 869d7058a79SJeff Kirsher } 870d7058a79SJeff Kirsher 871d7058a79SJeff Kirsher static void 872d7058a79SJeff Kirsher dm9000_hash_table(struct net_device *dev) 873d7058a79SJeff Kirsher { 8742b162928SHimangi Saraogi struct board_info *db = netdev_priv(dev); 875d7058a79SJeff Kirsher unsigned long flags; 876d7058a79SJeff Kirsher 877d7058a79SJeff Kirsher spin_lock_irqsave(&db->lock, flags); 878d7058a79SJeff Kirsher dm9000_hash_table_unlocked(dev); 879d7058a79SJeff Kirsher spin_unlock_irqrestore(&db->lock, flags); 880d7058a79SJeff Kirsher } 881d7058a79SJeff Kirsher 88217ad78deSAndrew Ruder static void 8832b162928SHimangi Saraogi dm9000_mask_interrupts(struct board_info *db) 88417ad78deSAndrew Ruder { 88517ad78deSAndrew Ruder iow(db, DM9000_IMR, IMR_PAR); 88617ad78deSAndrew Ruder } 88717ad78deSAndrew Ruder 88817ad78deSAndrew Ruder static void 8892b162928SHimangi Saraogi dm9000_unmask_interrupts(struct board_info *db) 89017ad78deSAndrew Ruder { 89117ad78deSAndrew Ruder iow(db, DM9000_IMR, db->imr_all); 89217ad78deSAndrew Ruder } 89317ad78deSAndrew Ruder 894d7058a79SJeff Kirsher /* 895d7058a79SJeff Kirsher * Initialize dm9000 board 896d7058a79SJeff Kirsher */ 897d7058a79SJeff Kirsher static void 898d7058a79SJeff Kirsher dm9000_init_dm9000(struct net_device *dev) 899d7058a79SJeff Kirsher { 9002b162928SHimangi Saraogi struct board_info *db = netdev_priv(dev); 901d7058a79SJeff Kirsher unsigned int imr; 902d7058a79SJeff Kirsher unsigned int ncr; 903d7058a79SJeff Kirsher 904d7058a79SJeff Kirsher dm9000_dbg(db, 1, "entering %s\n", __func__); 905d7058a79SJeff Kirsher 906751bb6fdSAndrew Ruder dm9000_reset(db); 90717ad78deSAndrew Ruder dm9000_mask_interrupts(db); 908751bb6fdSAndrew Ruder 909d7058a79SJeff Kirsher /* I/O mode */ 910d7058a79SJeff Kirsher db->io_mode = ior(db, DM9000_ISR) >> 6; /* ISR bit7:6 keeps I/O mode */ 911d7058a79SJeff Kirsher 912d7058a79SJeff Kirsher /* Checksum mode */ 913d7058a79SJeff Kirsher if (dev->hw_features & NETIF_F_RXCSUM) 914d7058a79SJeff Kirsher iow(db, DM9000_RCSR, 915d7058a79SJeff Kirsher (dev->features & NETIF_F_RXCSUM) ? RCSR_CSUM : 0); 916d7058a79SJeff Kirsher 917d7058a79SJeff Kirsher iow(db, DM9000_GPCR, GPCR_GEP_CNTL); /* Let GPIO0 output */ 918677d7d28SNikita Kiryanov iow(db, DM9000_GPR, 0); 919d7058a79SJeff Kirsher 9206649b205SNikita Kiryanov /* If we are dealing with DM9000B, some extra steps are required: a 9216649b205SNikita Kiryanov * manual phy reset, and setting init params. 9226649b205SNikita Kiryanov */ 9236649b205SNikita Kiryanov if (db->type == TYPE_DM9000B) { 9246649b205SNikita Kiryanov dm9000_phy_write(dev, 0, MII_BMCR, BMCR_RESET); 9256649b205SNikita Kiryanov dm9000_phy_write(dev, 0, MII_DM_DSPCR, DSPCR_INIT_PARAM); 9266649b205SNikita Kiryanov } 9276741f40dSJoseph CHANG 928d7058a79SJeff Kirsher ncr = (db->flags & DM9000_PLATF_EXT_PHY) ? NCR_EXT_PHY : 0; 929d7058a79SJeff Kirsher 930d7058a79SJeff Kirsher /* if wol is needed, then always set NCR_WAKEEN otherwise we end 931d7058a79SJeff Kirsher * up dumping the wake events if we disable this. There is already 932d7058a79SJeff Kirsher * a wake-mask in DM9000_WCR */ 933d7058a79SJeff Kirsher if (db->wake_supported) 934d7058a79SJeff Kirsher ncr |= NCR_WAKEEN; 935d7058a79SJeff Kirsher 936d7058a79SJeff Kirsher iow(db, DM9000_NCR, ncr); 937d7058a79SJeff Kirsher 938d7058a79SJeff Kirsher /* Program operating register */ 939d7058a79SJeff Kirsher iow(db, DM9000_TCR, 0); /* TX Polling clear */ 940d7058a79SJeff Kirsher iow(db, DM9000_BPTR, 0x3f); /* Less 3Kb, 200us */ 941d7058a79SJeff Kirsher iow(db, DM9000_FCR, 0xff); /* Flow Control */ 942d7058a79SJeff Kirsher iow(db, DM9000_SMCR, 0); /* Special Mode */ 943d7058a79SJeff Kirsher /* clear TX status */ 944d7058a79SJeff Kirsher iow(db, DM9000_NSR, NSR_WAKEST | NSR_TX2END | NSR_TX1END); 945d7058a79SJeff Kirsher iow(db, DM9000_ISR, ISR_CLR_STATUS); /* Clear interrupt status */ 946d7058a79SJeff Kirsher 947d7058a79SJeff Kirsher /* Set address filter table */ 948d7058a79SJeff Kirsher dm9000_hash_table_unlocked(dev); 949d7058a79SJeff Kirsher 950d7058a79SJeff Kirsher imr = IMR_PAR | IMR_PTM | IMR_PRM; 951d7058a79SJeff Kirsher if (db->type != TYPE_DM9000E) 952d7058a79SJeff Kirsher imr |= IMR_LNKCHNG; 953d7058a79SJeff Kirsher 954d7058a79SJeff Kirsher db->imr_all = imr; 955d7058a79SJeff Kirsher 956d7058a79SJeff Kirsher /* Init Driver variable */ 957d7058a79SJeff Kirsher db->tx_pkt_cnt = 0; 958d7058a79SJeff Kirsher db->queue_pkt_len = 0; 959860e9538SFlorian Westphal netif_trans_update(dev); 960d7058a79SJeff Kirsher } 961d7058a79SJeff Kirsher 962d7058a79SJeff Kirsher /* Our watchdog timed out. Called by the networking layer */ 9630290bd29SMichael S. Tsirkin static void dm9000_timeout(struct net_device *dev, unsigned int txqueue) 964d7058a79SJeff Kirsher { 9652b162928SHimangi Saraogi struct board_info *db = netdev_priv(dev); 966d7058a79SJeff Kirsher u8 reg_save; 967d7058a79SJeff Kirsher unsigned long flags; 968d7058a79SJeff Kirsher 969d7058a79SJeff Kirsher /* Save previous register address */ 970d7058a79SJeff Kirsher spin_lock_irqsave(&db->lock, flags); 97158237983SAndrew Ruder db->in_timeout = 1; 972d7058a79SJeff Kirsher reg_save = readb(db->io_addr); 973d7058a79SJeff Kirsher 974d7058a79SJeff Kirsher netif_stop_queue(dev); 975d7058a79SJeff Kirsher dm9000_init_dm9000(dev); 97617ad78deSAndrew Ruder dm9000_unmask_interrupts(db); 977d7058a79SJeff Kirsher /* We can accept TX packets again */ 978860e9538SFlorian Westphal netif_trans_update(dev); /* prevent tx timeout */ 979d7058a79SJeff Kirsher netif_wake_queue(dev); 980d7058a79SJeff Kirsher 981d7058a79SJeff Kirsher /* Restore previous register address */ 982d7058a79SJeff Kirsher writeb(reg_save, db->io_addr); 98358237983SAndrew Ruder db->in_timeout = 0; 984d7058a79SJeff Kirsher spin_unlock_irqrestore(&db->lock, flags); 985d7058a79SJeff Kirsher } 986d7058a79SJeff Kirsher 987d7058a79SJeff Kirsher static void dm9000_send_packet(struct net_device *dev, 988d7058a79SJeff Kirsher int ip_summed, 989d7058a79SJeff Kirsher u16 pkt_len) 990d7058a79SJeff Kirsher { 9912b162928SHimangi Saraogi struct board_info *dm = to_dm9000_board(dev); 992d7058a79SJeff Kirsher 993d7058a79SJeff Kirsher /* The DM9000 is not smart enough to leave fragmented packets alone. */ 994d7058a79SJeff Kirsher if (dm->ip_summed != ip_summed) { 995d7058a79SJeff Kirsher if (ip_summed == CHECKSUM_NONE) 996d7058a79SJeff Kirsher iow(dm, DM9000_TCCR, 0); 997d7058a79SJeff Kirsher else 998d7058a79SJeff Kirsher iow(dm, DM9000_TCCR, TCCR_IP | TCCR_UDP | TCCR_TCP); 999d7058a79SJeff Kirsher dm->ip_summed = ip_summed; 1000d7058a79SJeff Kirsher } 1001d7058a79SJeff Kirsher 1002d7058a79SJeff Kirsher /* Set TX length to DM9000 */ 1003d7058a79SJeff Kirsher iow(dm, DM9000_TXPLL, pkt_len); 1004d7058a79SJeff Kirsher iow(dm, DM9000_TXPLH, pkt_len >> 8); 1005d7058a79SJeff Kirsher 1006d7058a79SJeff Kirsher /* Issue TX polling command */ 1007d7058a79SJeff Kirsher iow(dm, DM9000_TCR, TCR_TXREQ); /* Cleared after TX complete */ 1008d7058a79SJeff Kirsher } 1009d7058a79SJeff Kirsher 1010d7058a79SJeff Kirsher /* 1011d7058a79SJeff Kirsher * Hardware start transmission. 1012d7058a79SJeff Kirsher * Send a packet to media from the upper layer. 1013d7058a79SJeff Kirsher */ 10140191580bSNathan Huckleberry static netdev_tx_t 1015d7058a79SJeff Kirsher dm9000_start_xmit(struct sk_buff *skb, struct net_device *dev) 1016d7058a79SJeff Kirsher { 1017d7058a79SJeff Kirsher unsigned long flags; 10182b162928SHimangi Saraogi struct board_info *db = netdev_priv(dev); 1019d7058a79SJeff Kirsher 1020d7058a79SJeff Kirsher dm9000_dbg(db, 3, "%s:\n", __func__); 1021d7058a79SJeff Kirsher 1022d7058a79SJeff Kirsher if (db->tx_pkt_cnt > 1) 1023d7058a79SJeff Kirsher return NETDEV_TX_BUSY; 1024d7058a79SJeff Kirsher 1025d7058a79SJeff Kirsher spin_lock_irqsave(&db->lock, flags); 1026d7058a79SJeff Kirsher 1027d7058a79SJeff Kirsher /* Move data to DM9000 TX RAM */ 1028d7058a79SJeff Kirsher writeb(DM9000_MWCMD, db->io_addr); 1029d7058a79SJeff Kirsher 1030d7058a79SJeff Kirsher (db->outblk)(db->io_data, skb->data, skb->len); 1031d7058a79SJeff Kirsher dev->stats.tx_bytes += skb->len; 1032d7058a79SJeff Kirsher 1033d7058a79SJeff Kirsher db->tx_pkt_cnt++; 1034d7058a79SJeff Kirsher /* TX control: First packet immediately send, second packet queue */ 1035d7058a79SJeff Kirsher if (db->tx_pkt_cnt == 1) { 1036d7058a79SJeff Kirsher dm9000_send_packet(dev, skb->ip_summed, skb->len); 1037d7058a79SJeff Kirsher } else { 1038d7058a79SJeff Kirsher /* Second packet */ 1039d7058a79SJeff Kirsher db->queue_pkt_len = skb->len; 1040d7058a79SJeff Kirsher db->queue_ip_summed = skb->ip_summed; 1041d7058a79SJeff Kirsher netif_stop_queue(dev); 1042d7058a79SJeff Kirsher } 1043d7058a79SJeff Kirsher 1044d7058a79SJeff Kirsher spin_unlock_irqrestore(&db->lock, flags); 1045d7058a79SJeff Kirsher 1046d7058a79SJeff Kirsher /* free this SKB */ 10472c3d0bc0SEric W. Biederman dev_consume_skb_any(skb); 1048d7058a79SJeff Kirsher 1049d7058a79SJeff Kirsher return NETDEV_TX_OK; 1050d7058a79SJeff Kirsher } 1051d7058a79SJeff Kirsher 1052d7058a79SJeff Kirsher /* 1053d7058a79SJeff Kirsher * DM9000 interrupt handler 1054d7058a79SJeff Kirsher * receive the packet to upper layer, free the transmitted packet 1055d7058a79SJeff Kirsher */ 1056d7058a79SJeff Kirsher 10572b162928SHimangi Saraogi static void dm9000_tx_done(struct net_device *dev, struct board_info *db) 1058d7058a79SJeff Kirsher { 1059d7058a79SJeff Kirsher int tx_status = ior(db, DM9000_NSR); /* Got TX status */ 1060d7058a79SJeff Kirsher 1061d7058a79SJeff Kirsher if (tx_status & (NSR_TX2END | NSR_TX1END)) { 1062d7058a79SJeff Kirsher /* One packet sent complete */ 1063d7058a79SJeff Kirsher db->tx_pkt_cnt--; 1064d7058a79SJeff Kirsher dev->stats.tx_packets++; 1065d7058a79SJeff Kirsher 1066d7058a79SJeff Kirsher if (netif_msg_tx_done(db)) 1067d7058a79SJeff Kirsher dev_dbg(db->dev, "tx done, NSR %02x\n", tx_status); 1068d7058a79SJeff Kirsher 1069d7058a79SJeff Kirsher /* Queue packet check & send */ 1070d7058a79SJeff Kirsher if (db->tx_pkt_cnt > 0) 1071d7058a79SJeff Kirsher dm9000_send_packet(dev, db->queue_ip_summed, 1072d7058a79SJeff Kirsher db->queue_pkt_len); 1073d7058a79SJeff Kirsher netif_wake_queue(dev); 1074d7058a79SJeff Kirsher } 1075d7058a79SJeff Kirsher } 1076d7058a79SJeff Kirsher 1077d7058a79SJeff Kirsher struct dm9000_rxhdr { 1078d7058a79SJeff Kirsher u8 RxPktReady; 1079d7058a79SJeff Kirsher u8 RxStatus; 1080d7058a79SJeff Kirsher __le16 RxLen; 1081d7058a79SJeff Kirsher } __packed; 1082d7058a79SJeff Kirsher 1083d7058a79SJeff Kirsher /* 1084d7058a79SJeff Kirsher * Received a packet and pass to upper layer 1085d7058a79SJeff Kirsher */ 1086d7058a79SJeff Kirsher static void 1087d7058a79SJeff Kirsher dm9000_rx(struct net_device *dev) 1088d7058a79SJeff Kirsher { 10892b162928SHimangi Saraogi struct board_info *db = netdev_priv(dev); 1090d7058a79SJeff Kirsher struct dm9000_rxhdr rxhdr; 1091d7058a79SJeff Kirsher struct sk_buff *skb; 1092d7058a79SJeff Kirsher u8 rxbyte, *rdptr; 1093d7058a79SJeff Kirsher bool GoodPacket; 1094d7058a79SJeff Kirsher int RxLen; 1095d7058a79SJeff Kirsher 1096d7058a79SJeff Kirsher /* Check packet ready or not */ 1097d7058a79SJeff Kirsher do { 1098d7058a79SJeff Kirsher ior(db, DM9000_MRCMDX); /* Dummy read */ 1099d7058a79SJeff Kirsher 1100d7058a79SJeff Kirsher /* Get most updated data */ 1101d7058a79SJeff Kirsher rxbyte = readb(db->io_data); 1102d7058a79SJeff Kirsher 1103d7058a79SJeff Kirsher /* Status check: this byte must be 0 or 1 */ 1104d7058a79SJeff Kirsher if (rxbyte & DM9000_PKT_ERR) { 1105d7058a79SJeff Kirsher dev_warn(db->dev, "status check fail: %d\n", rxbyte); 1106d7058a79SJeff Kirsher iow(db, DM9000_RCR, 0x00); /* Stop Device */ 1107d7058a79SJeff Kirsher return; 1108d7058a79SJeff Kirsher } 1109d7058a79SJeff Kirsher 1110d7058a79SJeff Kirsher if (!(rxbyte & DM9000_PKT_RDY)) 1111d7058a79SJeff Kirsher return; 1112d7058a79SJeff Kirsher 1113d7058a79SJeff Kirsher /* A packet ready now & Get status/length */ 1114d7058a79SJeff Kirsher GoodPacket = true; 1115d7058a79SJeff Kirsher writeb(DM9000_MRCMD, db->io_addr); 1116d7058a79SJeff Kirsher 1117d7058a79SJeff Kirsher (db->inblk)(db->io_data, &rxhdr, sizeof(rxhdr)); 1118d7058a79SJeff Kirsher 1119d7058a79SJeff Kirsher RxLen = le16_to_cpu(rxhdr.RxLen); 1120d7058a79SJeff Kirsher 1121d7058a79SJeff Kirsher if (netif_msg_rx_status(db)) 1122d7058a79SJeff Kirsher dev_dbg(db->dev, "RX: status %02x, length %04x\n", 1123d7058a79SJeff Kirsher rxhdr.RxStatus, RxLen); 1124d7058a79SJeff Kirsher 1125d7058a79SJeff Kirsher /* Packet Status check */ 1126d7058a79SJeff Kirsher if (RxLen < 0x40) { 1127d7058a79SJeff Kirsher GoodPacket = false; 1128d7058a79SJeff Kirsher if (netif_msg_rx_err(db)) 1129d7058a79SJeff Kirsher dev_dbg(db->dev, "RX: Bad Packet (runt)\n"); 1130d7058a79SJeff Kirsher } 1131d7058a79SJeff Kirsher 1132d7058a79SJeff Kirsher if (RxLen > DM9000_PKT_MAX) { 1133d7058a79SJeff Kirsher dev_dbg(db->dev, "RST: RX Len:%x\n", RxLen); 1134d7058a79SJeff Kirsher } 1135d7058a79SJeff Kirsher 1136d7058a79SJeff Kirsher /* rxhdr.RxStatus is identical to RSR register. */ 1137d7058a79SJeff Kirsher if (rxhdr.RxStatus & (RSR_FOE | RSR_CE | RSR_AE | 1138d7058a79SJeff Kirsher RSR_PLE | RSR_RWTO | 1139d7058a79SJeff Kirsher RSR_LCS | RSR_RF)) { 1140d7058a79SJeff Kirsher GoodPacket = false; 1141d7058a79SJeff Kirsher if (rxhdr.RxStatus & RSR_FOE) { 1142d7058a79SJeff Kirsher if (netif_msg_rx_err(db)) 1143d7058a79SJeff Kirsher dev_dbg(db->dev, "fifo error\n"); 1144d7058a79SJeff Kirsher dev->stats.rx_fifo_errors++; 1145d7058a79SJeff Kirsher } 1146d7058a79SJeff Kirsher if (rxhdr.RxStatus & RSR_CE) { 1147d7058a79SJeff Kirsher if (netif_msg_rx_err(db)) 1148d7058a79SJeff Kirsher dev_dbg(db->dev, "crc error\n"); 1149d7058a79SJeff Kirsher dev->stats.rx_crc_errors++; 1150d7058a79SJeff Kirsher } 1151d7058a79SJeff Kirsher if (rxhdr.RxStatus & RSR_RF) { 1152d7058a79SJeff Kirsher if (netif_msg_rx_err(db)) 1153d7058a79SJeff Kirsher dev_dbg(db->dev, "length error\n"); 1154d7058a79SJeff Kirsher dev->stats.rx_length_errors++; 1155d7058a79SJeff Kirsher } 1156d7058a79SJeff Kirsher } 1157d7058a79SJeff Kirsher 1158d7058a79SJeff Kirsher /* Move data from DM9000 */ 1159d7058a79SJeff Kirsher if (GoodPacket && 116021a4e469SPradeep A Dalvi ((skb = netdev_alloc_skb(dev, RxLen + 4)) != NULL)) { 1161d7058a79SJeff Kirsher skb_reserve(skb, 2); 11624df864c1SJohannes Berg rdptr = skb_put(skb, RxLen - 4); 1163d7058a79SJeff Kirsher 1164d7058a79SJeff Kirsher /* Read received packet from RX SRAM */ 1165d7058a79SJeff Kirsher 1166d7058a79SJeff Kirsher (db->inblk)(db->io_data, rdptr, RxLen); 1167d7058a79SJeff Kirsher dev->stats.rx_bytes += RxLen; 1168d7058a79SJeff Kirsher 1169d7058a79SJeff Kirsher /* Pass to upper layer */ 1170d7058a79SJeff Kirsher skb->protocol = eth_type_trans(skb, dev); 1171d7058a79SJeff Kirsher if (dev->features & NETIF_F_RXCSUM) { 1172d7058a79SJeff Kirsher if ((((rxbyte & 0x1c) << 3) & rxbyte) == 0) 1173d7058a79SJeff Kirsher skb->ip_summed = CHECKSUM_UNNECESSARY; 1174d7058a79SJeff Kirsher else 1175d7058a79SJeff Kirsher skb_checksum_none_assert(skb); 1176d7058a79SJeff Kirsher } 1177d7058a79SJeff Kirsher netif_rx(skb); 1178d7058a79SJeff Kirsher dev->stats.rx_packets++; 1179d7058a79SJeff Kirsher 1180d7058a79SJeff Kirsher } else { 1181d7058a79SJeff Kirsher /* need to dump the packet's data */ 1182d7058a79SJeff Kirsher 1183d7058a79SJeff Kirsher (db->dumpblk)(db->io_data, RxLen); 1184d7058a79SJeff Kirsher } 1185d7058a79SJeff Kirsher } while (rxbyte & DM9000_PKT_RDY); 1186d7058a79SJeff Kirsher } 1187d7058a79SJeff Kirsher 1188d7058a79SJeff Kirsher static irqreturn_t dm9000_interrupt(int irq, void *dev_id) 1189d7058a79SJeff Kirsher { 1190d7058a79SJeff Kirsher struct net_device *dev = dev_id; 11912b162928SHimangi Saraogi struct board_info *db = netdev_priv(dev); 1192d7058a79SJeff Kirsher int int_status; 1193d7058a79SJeff Kirsher unsigned long flags; 1194d7058a79SJeff Kirsher u8 reg_save; 1195d7058a79SJeff Kirsher 1196d7058a79SJeff Kirsher dm9000_dbg(db, 3, "entering %s\n", __func__); 1197d7058a79SJeff Kirsher 1198d7058a79SJeff Kirsher /* A real interrupt coming */ 1199d7058a79SJeff Kirsher 1200d7058a79SJeff Kirsher /* holders of db->lock must always block IRQs */ 1201d7058a79SJeff Kirsher spin_lock_irqsave(&db->lock, flags); 1202d7058a79SJeff Kirsher 1203d7058a79SJeff Kirsher /* Save previous register address */ 1204d7058a79SJeff Kirsher reg_save = readb(db->io_addr); 1205d7058a79SJeff Kirsher 120617ad78deSAndrew Ruder dm9000_mask_interrupts(db); 1207d7058a79SJeff Kirsher /* Got DM9000 interrupt status */ 1208d7058a79SJeff Kirsher int_status = ior(db, DM9000_ISR); /* Got ISR */ 1209d7058a79SJeff Kirsher iow(db, DM9000_ISR, int_status); /* Clear ISR status */ 1210d7058a79SJeff Kirsher 1211d7058a79SJeff Kirsher if (netif_msg_intr(db)) 1212d7058a79SJeff Kirsher dev_dbg(db->dev, "interrupt status %02x\n", int_status); 1213d7058a79SJeff Kirsher 1214d7058a79SJeff Kirsher /* Received the coming packet */ 1215d7058a79SJeff Kirsher if (int_status & ISR_PRS) 1216d7058a79SJeff Kirsher dm9000_rx(dev); 1217d7058a79SJeff Kirsher 12187b901873SBarry Song /* Transmit Interrupt check */ 1219d7058a79SJeff Kirsher if (int_status & ISR_PTS) 1220d7058a79SJeff Kirsher dm9000_tx_done(dev, db); 1221d7058a79SJeff Kirsher 1222d7058a79SJeff Kirsher if (db->type != TYPE_DM9000E) { 1223d7058a79SJeff Kirsher if (int_status & ISR_LNKCHNG) { 1224d7058a79SJeff Kirsher /* fire a link-change request */ 1225d7058a79SJeff Kirsher schedule_delayed_work(&db->phy_poll, 1); 1226d7058a79SJeff Kirsher } 1227d7058a79SJeff Kirsher } 1228d7058a79SJeff Kirsher 122917ad78deSAndrew Ruder dm9000_unmask_interrupts(db); 1230d7058a79SJeff Kirsher /* Restore previous register address */ 1231d7058a79SJeff Kirsher writeb(reg_save, db->io_addr); 1232d7058a79SJeff Kirsher 1233d7058a79SJeff Kirsher spin_unlock_irqrestore(&db->lock, flags); 1234d7058a79SJeff Kirsher 1235d7058a79SJeff Kirsher return IRQ_HANDLED; 1236d7058a79SJeff Kirsher } 1237d7058a79SJeff Kirsher 1238d7058a79SJeff Kirsher static irqreturn_t dm9000_wol_interrupt(int irq, void *dev_id) 1239d7058a79SJeff Kirsher { 1240d7058a79SJeff Kirsher struct net_device *dev = dev_id; 12412b162928SHimangi Saraogi struct board_info *db = netdev_priv(dev); 1242d7058a79SJeff Kirsher unsigned long flags; 1243d7058a79SJeff Kirsher unsigned nsr, wcr; 1244d7058a79SJeff Kirsher 1245d7058a79SJeff Kirsher spin_lock_irqsave(&db->lock, flags); 1246d7058a79SJeff Kirsher 1247d7058a79SJeff Kirsher nsr = ior(db, DM9000_NSR); 1248d7058a79SJeff Kirsher wcr = ior(db, DM9000_WCR); 1249d7058a79SJeff Kirsher 1250d7058a79SJeff Kirsher dev_dbg(db->dev, "%s: NSR=0x%02x, WCR=0x%02x\n", __func__, nsr, wcr); 1251d7058a79SJeff Kirsher 1252d7058a79SJeff Kirsher if (nsr & NSR_WAKEST) { 1253d7058a79SJeff Kirsher /* clear, so we can avoid */ 1254d7058a79SJeff Kirsher iow(db, DM9000_NSR, NSR_WAKEST); 1255d7058a79SJeff Kirsher 1256d7058a79SJeff Kirsher if (wcr & WCR_LINKST) 1257d7058a79SJeff Kirsher dev_info(db->dev, "wake by link status change\n"); 1258d7058a79SJeff Kirsher if (wcr & WCR_SAMPLEST) 1259d7058a79SJeff Kirsher dev_info(db->dev, "wake by sample packet\n"); 1260d7058a79SJeff Kirsher if (wcr & WCR_MAGICST) 1261d7058a79SJeff Kirsher dev_info(db->dev, "wake by magic packet\n"); 1262d7058a79SJeff Kirsher if (!(wcr & (WCR_LINKST | WCR_SAMPLEST | WCR_MAGICST))) 1263d7058a79SJeff Kirsher dev_err(db->dev, "wake signalled with no reason? " 1264d7058a79SJeff Kirsher "NSR=0x%02x, WSR=0x%02x\n", nsr, wcr); 1265d7058a79SJeff Kirsher } 1266d7058a79SJeff Kirsher 1267d7058a79SJeff Kirsher spin_unlock_irqrestore(&db->lock, flags); 1268d7058a79SJeff Kirsher 1269d7058a79SJeff Kirsher return (nsr & NSR_WAKEST) ? IRQ_HANDLED : IRQ_NONE; 1270d7058a79SJeff Kirsher } 1271d7058a79SJeff Kirsher 1272d7058a79SJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER 1273d7058a79SJeff Kirsher /* 1274d7058a79SJeff Kirsher *Used by netconsole 1275d7058a79SJeff Kirsher */ 1276d7058a79SJeff Kirsher static void dm9000_poll_controller(struct net_device *dev) 1277d7058a79SJeff Kirsher { 1278d7058a79SJeff Kirsher disable_irq(dev->irq); 1279d7058a79SJeff Kirsher dm9000_interrupt(dev->irq, dev); 1280d7058a79SJeff Kirsher enable_irq(dev->irq); 1281d7058a79SJeff Kirsher } 1282d7058a79SJeff Kirsher #endif 1283d7058a79SJeff Kirsher 1284d7058a79SJeff Kirsher /* 1285d7058a79SJeff Kirsher * Open the interface. 1286d7058a79SJeff Kirsher * The interface is opened whenever "ifconfig" actives it. 1287d7058a79SJeff Kirsher */ 1288d7058a79SJeff Kirsher static int 1289d7058a79SJeff Kirsher dm9000_open(struct net_device *dev) 1290d7058a79SJeff Kirsher { 12912b162928SHimangi Saraogi struct board_info *db = netdev_priv(dev); 1292a96d3b75SSylwester Nawrocki unsigned int irq_flags = irq_get_trigger_type(dev->irq); 1293d7058a79SJeff Kirsher 1294d7058a79SJeff Kirsher if (netif_msg_ifup(db)) 1295d7058a79SJeff Kirsher dev_dbg(db->dev, "enabling %s\n", dev->name); 1296d7058a79SJeff Kirsher 1297b5a099c6SRobert Jarzmik /* If there is no IRQ type specified, tell the user that this is a 1298b5a099c6SRobert Jarzmik * problem 1299b5a099c6SRobert Jarzmik */ 1300a96d3b75SSylwester Nawrocki if (irq_flags == IRQF_TRIGGER_NONE) 1301d7058a79SJeff Kirsher dev_warn(db->dev, "WARNING: no IRQ resource flags set.\n"); 1302d7058a79SJeff Kirsher 1303a96d3b75SSylwester Nawrocki irq_flags |= IRQF_SHARED; 1304a96d3b75SSylwester Nawrocki 1305d7058a79SJeff Kirsher /* GPIO0 on pre-activate PHY, Reg 1F is not set by reset */ 1306d7058a79SJeff Kirsher iow(db, DM9000_GPR, 0); /* REG_1F bit0 activate phyxcer */ 1307d7058a79SJeff Kirsher mdelay(1); /* delay needs by DM9000B */ 1308d7058a79SJeff Kirsher 1309d7058a79SJeff Kirsher /* Initialize DM9000 board */ 1310d7058a79SJeff Kirsher dm9000_init_dm9000(dev); 1311d7058a79SJeff Kirsher 1312a96d3b75SSylwester Nawrocki if (request_irq(dev->irq, dm9000_interrupt, irq_flags, dev->name, dev)) 1313d7058a79SJeff Kirsher return -EAGAIN; 131417ad78deSAndrew Ruder /* Now that we have an interrupt handler hooked up we can unmask 131517ad78deSAndrew Ruder * our interrupts 131617ad78deSAndrew Ruder */ 131717ad78deSAndrew Ruder dm9000_unmask_interrupts(db); 1318d7058a79SJeff Kirsher 1319d7058a79SJeff Kirsher /* Init driver variable */ 1320d7058a79SJeff Kirsher db->dbug_cnt = 0; 1321d7058a79SJeff Kirsher 1322d7058a79SJeff Kirsher mii_check_media(&db->mii, netif_msg_link(db), 1); 1323d7058a79SJeff Kirsher netif_start_queue(dev); 1324d7058a79SJeff Kirsher 1325aac6d022SAndrew Ruder /* Poll initial link status */ 1326aac6d022SAndrew Ruder schedule_delayed_work(&db->phy_poll, 1); 1327d7058a79SJeff Kirsher 1328d7058a79SJeff Kirsher return 0; 1329d7058a79SJeff Kirsher } 1330d7058a79SJeff Kirsher 1331d7058a79SJeff Kirsher static void 1332d7058a79SJeff Kirsher dm9000_shutdown(struct net_device *dev) 1333d7058a79SJeff Kirsher { 13342b162928SHimangi Saraogi struct board_info *db = netdev_priv(dev); 1335d7058a79SJeff Kirsher 1336d7058a79SJeff Kirsher /* RESET device */ 1337d7058a79SJeff Kirsher dm9000_phy_write(dev, 0, MII_BMCR, BMCR_RESET); /* PHY RESET */ 1338d7058a79SJeff Kirsher iow(db, DM9000_GPR, 0x01); /* Power-Down PHY */ 133917ad78deSAndrew Ruder dm9000_mask_interrupts(db); 1340d7058a79SJeff Kirsher iow(db, DM9000_RCR, 0x00); /* Disable RX */ 1341d7058a79SJeff Kirsher } 1342d7058a79SJeff Kirsher 1343d7058a79SJeff Kirsher /* 1344d7058a79SJeff Kirsher * Stop the interface. 1345d7058a79SJeff Kirsher * The interface is stopped when it is brought. 1346d7058a79SJeff Kirsher */ 1347d7058a79SJeff Kirsher static int 1348d7058a79SJeff Kirsher dm9000_stop(struct net_device *ndev) 1349d7058a79SJeff Kirsher { 13502b162928SHimangi Saraogi struct board_info *db = netdev_priv(ndev); 1351d7058a79SJeff Kirsher 1352d7058a79SJeff Kirsher if (netif_msg_ifdown(db)) 1353d7058a79SJeff Kirsher dev_dbg(db->dev, "shutting down %s\n", ndev->name); 1354d7058a79SJeff Kirsher 1355d7058a79SJeff Kirsher cancel_delayed_work_sync(&db->phy_poll); 1356d7058a79SJeff Kirsher 1357d7058a79SJeff Kirsher netif_stop_queue(ndev); 1358d7058a79SJeff Kirsher netif_carrier_off(ndev); 1359d7058a79SJeff Kirsher 1360d7058a79SJeff Kirsher /* free interrupt */ 1361d7058a79SJeff Kirsher free_irq(ndev->irq, ndev); 1362d7058a79SJeff Kirsher 1363d7058a79SJeff Kirsher dm9000_shutdown(ndev); 1364d7058a79SJeff Kirsher 1365d7058a79SJeff Kirsher return 0; 1366d7058a79SJeff Kirsher } 1367d7058a79SJeff Kirsher 1368d7058a79SJeff Kirsher static const struct net_device_ops dm9000_netdev_ops = { 1369d7058a79SJeff Kirsher .ndo_open = dm9000_open, 1370d7058a79SJeff Kirsher .ndo_stop = dm9000_stop, 1371d7058a79SJeff Kirsher .ndo_start_xmit = dm9000_start_xmit, 1372d7058a79SJeff Kirsher .ndo_tx_timeout = dm9000_timeout, 1373afc4b13dSJiri Pirko .ndo_set_rx_mode = dm9000_hash_table, 1374a7605370SArnd Bergmann .ndo_eth_ioctl = dm9000_ioctl, 1375d7058a79SJeff Kirsher .ndo_set_features = dm9000_set_features, 1376d7058a79SJeff Kirsher .ndo_validate_addr = eth_validate_addr, 1377d7058a79SJeff Kirsher .ndo_set_mac_address = eth_mac_addr, 1378d7058a79SJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER 1379d7058a79SJeff Kirsher .ndo_poll_controller = dm9000_poll_controller, 1380d7058a79SJeff Kirsher #endif 1381d7058a79SJeff Kirsher }; 1382d7058a79SJeff Kirsher 13830b8bf1baSTomasz Figa static struct dm9000_plat_data *dm9000_parse_dt(struct device *dev) 13840b8bf1baSTomasz Figa { 13850b8bf1baSTomasz Figa struct dm9000_plat_data *pdata; 13860b8bf1baSTomasz Figa struct device_node *np = dev->of_node; 138783216e39SMichael Walle int ret; 13880b8bf1baSTomasz Figa 13890b8bf1baSTomasz Figa if (!IS_ENABLED(CONFIG_OF) || !np) 139009f3756bSTobias Klauser return ERR_PTR(-ENXIO); 13910b8bf1baSTomasz Figa 13920b8bf1baSTomasz Figa pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); 13930b8bf1baSTomasz Figa if (!pdata) 13940b8bf1baSTomasz Figa return ERR_PTR(-ENOMEM); 13950b8bf1baSTomasz Figa 1396*1a87e641SRob Herring if (of_property_read_bool(np, "davicom,ext-phy")) 13970b8bf1baSTomasz Figa pdata->flags |= DM9000_PLATF_EXT_PHY; 1398*1a87e641SRob Herring if (of_property_read_bool(np, "davicom,no-eeprom")) 13990b8bf1baSTomasz Figa pdata->flags |= DM9000_PLATF_NO_EEPROM; 14000b8bf1baSTomasz Figa 140183216e39SMichael Walle ret = of_get_mac_address(np, pdata->dev_addr); 140283216e39SMichael Walle if (ret == -EPROBE_DEFER) 140383216e39SMichael Walle return ERR_PTR(ret); 14040b8bf1baSTomasz Figa 14050b8bf1baSTomasz Figa return pdata; 14060b8bf1baSTomasz Figa } 14070b8bf1baSTomasz Figa 1408d7058a79SJeff Kirsher /* 1409d7058a79SJeff Kirsher * Search DM9000 board, allocate space and register it 1410d7058a79SJeff Kirsher */ 14116b6a3e7fSBill Pemberton static int 1412d7058a79SJeff Kirsher dm9000_probe(struct platform_device *pdev) 1413d7058a79SJeff Kirsher { 1414cd4e2e4bSJingoo Han struct dm9000_plat_data *pdata = dev_get_platdata(&pdev->dev); 1415d7058a79SJeff Kirsher struct board_info *db; /* Point a board information structure */ 1416d7058a79SJeff Kirsher struct net_device *ndev; 14177994fe55SZubair Lutfullah Kakakhel struct device *dev = &pdev->dev; 1418d7058a79SJeff Kirsher const unsigned char *mac_src; 1419d7058a79SJeff Kirsher int ret = 0; 1420d7058a79SJeff Kirsher int iosize; 1421d7058a79SJeff Kirsher int i; 1422d7058a79SJeff Kirsher u32 id_val; 1423db49ca38SDmitry Torokhov struct gpio_desc *reset_gpio; 14247994fe55SZubair Lutfullah Kakakhel struct regulator *power; 14253274940bSHarvey Hunt bool inv_mac_addr = false; 14264abd7cffSJakub Kicinski u8 addr[ETH_ALEN]; 14277994fe55SZubair Lutfullah Kakakhel 14287994fe55SZubair Lutfullah Kakakhel power = devm_regulator_get(dev, "vcc"); 14297994fe55SZubair Lutfullah Kakakhel if (IS_ERR(power)) { 14307994fe55SZubair Lutfullah Kakakhel if (PTR_ERR(power) == -EPROBE_DEFER) 14317994fe55SZubair Lutfullah Kakakhel return -EPROBE_DEFER; 14327994fe55SZubair Lutfullah Kakakhel dev_dbg(dev, "no regulator provided\n"); 14337994fe55SZubair Lutfullah Kakakhel } else { 14347994fe55SZubair Lutfullah Kakakhel ret = regulator_enable(power); 14357994fe55SZubair Lutfullah Kakakhel if (ret != 0) { 14367994fe55SZubair Lutfullah Kakakhel dev_err(dev, 14377994fe55SZubair Lutfullah Kakakhel "Failed to enable power regulator: %d\n", ret); 14387994fe55SZubair Lutfullah Kakakhel return ret; 14397994fe55SZubair Lutfullah Kakakhel } 14407994fe55SZubair Lutfullah Kakakhel dev_dbg(dev, "regulator enabled\n"); 14417994fe55SZubair Lutfullah Kakakhel } 14427994fe55SZubair Lutfullah Kakakhel 1443db49ca38SDmitry Torokhov reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH); 1444db49ca38SDmitry Torokhov ret = PTR_ERR_OR_ZERO(reset_gpio); 14457994fe55SZubair Lutfullah Kakakhel if (ret) { 1446db49ca38SDmitry Torokhov dev_err(dev, "failed to request reset gpio: %d\n", ret); 1447db49ca38SDmitry Torokhov goto out_regulator_disable; 1448db49ca38SDmitry Torokhov } 1449db49ca38SDmitry Torokhov 1450db49ca38SDmitry Torokhov if (reset_gpio) { 1451db49ca38SDmitry Torokhov ret = gpiod_set_consumer_name(reset_gpio, "dm9000_reset"); 1452db49ca38SDmitry Torokhov if (ret) { 1453db49ca38SDmitry Torokhov dev_err(dev, "failed to set reset gpio name: %d\n", 1454db49ca38SDmitry Torokhov ret); 1455ac88c531SPaul Cercueil goto out_regulator_disable; 14567994fe55SZubair Lutfullah Kakakhel } 14577994fe55SZubair Lutfullah Kakakhel 14587994fe55SZubair Lutfullah Kakakhel /* According to manual PWRST# Low Period Min 1ms */ 14597994fe55SZubair Lutfullah Kakakhel msleep(2); 1460db49ca38SDmitry Torokhov gpiod_set_value_cansleep(reset_gpio, 0); 14617994fe55SZubair Lutfullah Kakakhel /* Needs 3ms to read eeprom when PWRST is deasserted */ 14627994fe55SZubair Lutfullah Kakakhel msleep(4); 14637994fe55SZubair Lutfullah Kakakhel } 1464d7058a79SJeff Kirsher 14650b8bf1baSTomasz Figa if (!pdata) { 14660b8bf1baSTomasz Figa pdata = dm9000_parse_dt(&pdev->dev); 1467ac88c531SPaul Cercueil if (IS_ERR(pdata)) { 1468ac88c531SPaul Cercueil ret = PTR_ERR(pdata); 1469ac88c531SPaul Cercueil goto out_regulator_disable; 1470ac88c531SPaul Cercueil } 14710b8bf1baSTomasz Figa } 14720b8bf1baSTomasz Figa 1473d7058a79SJeff Kirsher /* Init network device */ 1474d7058a79SJeff Kirsher ndev = alloc_etherdev(sizeof(struct board_info)); 147531457db3SChristophe JAILLET if (!ndev) { 147631457db3SChristophe JAILLET ret = -ENOMEM; 147731457db3SChristophe JAILLET goto out_regulator_disable; 147831457db3SChristophe JAILLET } 1479d7058a79SJeff Kirsher 1480d7058a79SJeff Kirsher SET_NETDEV_DEV(ndev, &pdev->dev); 1481d7058a79SJeff Kirsher 1482d7058a79SJeff Kirsher dev_dbg(&pdev->dev, "dm9000_probe()\n"); 1483d7058a79SJeff Kirsher 1484d7058a79SJeff Kirsher /* setup board info structure */ 1485d7058a79SJeff Kirsher db = netdev_priv(ndev); 1486d7058a79SJeff Kirsher 1487d7058a79SJeff Kirsher db->dev = &pdev->dev; 1488d7058a79SJeff Kirsher db->ndev = ndev; 1489cf9e60aaSPaul Cercueil if (!IS_ERR(power)) 1490cf9e60aaSPaul Cercueil db->power_supply = power; 1491d7058a79SJeff Kirsher 1492d7058a79SJeff Kirsher spin_lock_init(&db->lock); 1493d7058a79SJeff Kirsher mutex_init(&db->addr_lock); 1494d7058a79SJeff Kirsher 1495d7058a79SJeff Kirsher INIT_DELAYED_WORK(&db->phy_poll, dm9000_poll_work); 1496d7058a79SJeff Kirsher 1497d7058a79SJeff Kirsher db->addr_res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1498d7058a79SJeff Kirsher db->data_res = platform_get_resource(pdev, IORESOURCE_MEM, 1); 1499d7058a79SJeff Kirsher 1500b5a099c6SRobert Jarzmik if (!db->addr_res || !db->data_res) { 1501b5a099c6SRobert Jarzmik dev_err(db->dev, "insufficient resources addr=%p data=%p\n", 1502b5a099c6SRobert Jarzmik db->addr_res, db->data_res); 1503d7058a79SJeff Kirsher ret = -ENOENT; 1504d7058a79SJeff Kirsher goto out; 1505d7058a79SJeff Kirsher } 1506d7058a79SJeff Kirsher 1507b5a099c6SRobert Jarzmik ndev->irq = platform_get_irq(pdev, 0); 1508b5a099c6SRobert Jarzmik if (ndev->irq < 0) { 1509b5a099c6SRobert Jarzmik ret = ndev->irq; 1510b5a099c6SRobert Jarzmik goto out; 1511b5a099c6SRobert Jarzmik } 1512b5a099c6SRobert Jarzmik 15132e269622SPaul Cercueil db->irq_wake = platform_get_irq_optional(pdev, 1); 1514d7058a79SJeff Kirsher if (db->irq_wake >= 0) { 1515d7058a79SJeff Kirsher dev_dbg(db->dev, "wakeup irq %d\n", db->irq_wake); 1516d7058a79SJeff Kirsher 1517d7058a79SJeff Kirsher ret = request_irq(db->irq_wake, dm9000_wol_interrupt, 1518d7058a79SJeff Kirsher IRQF_SHARED, dev_name(db->dev), ndev); 1519d7058a79SJeff Kirsher if (ret) { 1520d7058a79SJeff Kirsher dev_err(db->dev, "cannot get wakeup irq (%d)\n", ret); 1521d7058a79SJeff Kirsher } else { 1522d7058a79SJeff Kirsher 1523d7058a79SJeff Kirsher /* test to see if irq is really wakeup capable */ 1524d7058a79SJeff Kirsher ret = irq_set_irq_wake(db->irq_wake, 1); 1525d7058a79SJeff Kirsher if (ret) { 1526d7058a79SJeff Kirsher dev_err(db->dev, "irq %d cannot set wakeup (%d)\n", 1527d7058a79SJeff Kirsher db->irq_wake, ret); 1528d7058a79SJeff Kirsher } else { 1529d7058a79SJeff Kirsher irq_set_irq_wake(db->irq_wake, 0); 1530d7058a79SJeff Kirsher db->wake_supported = 1; 1531d7058a79SJeff Kirsher } 1532d7058a79SJeff Kirsher } 1533d7058a79SJeff Kirsher } 1534d7058a79SJeff Kirsher 1535d7058a79SJeff Kirsher iosize = resource_size(db->addr_res); 1536d7058a79SJeff Kirsher db->addr_req = request_mem_region(db->addr_res->start, iosize, 1537d7058a79SJeff Kirsher pdev->name); 1538d7058a79SJeff Kirsher 1539d7058a79SJeff Kirsher if (db->addr_req == NULL) { 1540d7058a79SJeff Kirsher dev_err(db->dev, "cannot claim address reg area\n"); 1541d7058a79SJeff Kirsher ret = -EIO; 1542d7058a79SJeff Kirsher goto out; 1543d7058a79SJeff Kirsher } 1544d7058a79SJeff Kirsher 1545d7058a79SJeff Kirsher db->io_addr = ioremap(db->addr_res->start, iosize); 1546d7058a79SJeff Kirsher 1547d7058a79SJeff Kirsher if (db->io_addr == NULL) { 1548d7058a79SJeff Kirsher dev_err(db->dev, "failed to ioremap address reg\n"); 1549d7058a79SJeff Kirsher ret = -EINVAL; 1550d7058a79SJeff Kirsher goto out; 1551d7058a79SJeff Kirsher } 1552d7058a79SJeff Kirsher 1553d7058a79SJeff Kirsher iosize = resource_size(db->data_res); 1554d7058a79SJeff Kirsher db->data_req = request_mem_region(db->data_res->start, iosize, 1555d7058a79SJeff Kirsher pdev->name); 1556d7058a79SJeff Kirsher 1557d7058a79SJeff Kirsher if (db->data_req == NULL) { 1558d7058a79SJeff Kirsher dev_err(db->dev, "cannot claim data reg area\n"); 1559d7058a79SJeff Kirsher ret = -EIO; 1560d7058a79SJeff Kirsher goto out; 1561d7058a79SJeff Kirsher } 1562d7058a79SJeff Kirsher 1563d7058a79SJeff Kirsher db->io_data = ioremap(db->data_res->start, iosize); 1564d7058a79SJeff Kirsher 1565d7058a79SJeff Kirsher if (db->io_data == NULL) { 1566d7058a79SJeff Kirsher dev_err(db->dev, "failed to ioremap data reg\n"); 1567d7058a79SJeff Kirsher ret = -EINVAL; 1568d7058a79SJeff Kirsher goto out; 1569d7058a79SJeff Kirsher } 1570d7058a79SJeff Kirsher 1571d7058a79SJeff Kirsher /* fill in parameters for net-dev structure */ 1572d7058a79SJeff Kirsher ndev->base_addr = (unsigned long)db->io_addr; 1573d7058a79SJeff Kirsher 1574d7058a79SJeff Kirsher /* ensure at least we have a default set of IO routines */ 1575d7058a79SJeff Kirsher dm9000_set_io(db, iosize); 1576d7058a79SJeff Kirsher 1577d7058a79SJeff Kirsher /* check to see if anything is being over-ridden */ 1578d7058a79SJeff Kirsher if (pdata != NULL) { 1579d7058a79SJeff Kirsher /* check to see if the driver wants to over-ride the 1580d7058a79SJeff Kirsher * default IO width */ 1581d7058a79SJeff Kirsher 1582d7058a79SJeff Kirsher if (pdata->flags & DM9000_PLATF_8BITONLY) 1583d7058a79SJeff Kirsher dm9000_set_io(db, 1); 1584d7058a79SJeff Kirsher 1585d7058a79SJeff Kirsher if (pdata->flags & DM9000_PLATF_16BITONLY) 1586d7058a79SJeff Kirsher dm9000_set_io(db, 2); 1587d7058a79SJeff Kirsher 1588d7058a79SJeff Kirsher if (pdata->flags & DM9000_PLATF_32BITONLY) 1589d7058a79SJeff Kirsher dm9000_set_io(db, 4); 1590d7058a79SJeff Kirsher 1591d7058a79SJeff Kirsher /* check to see if there are any IO routine 1592d7058a79SJeff Kirsher * over-rides */ 1593d7058a79SJeff Kirsher 1594d7058a79SJeff Kirsher if (pdata->inblk != NULL) 1595d7058a79SJeff Kirsher db->inblk = pdata->inblk; 1596d7058a79SJeff Kirsher 1597d7058a79SJeff Kirsher if (pdata->outblk != NULL) 1598d7058a79SJeff Kirsher db->outblk = pdata->outblk; 1599d7058a79SJeff Kirsher 1600d7058a79SJeff Kirsher if (pdata->dumpblk != NULL) 1601d7058a79SJeff Kirsher db->dumpblk = pdata->dumpblk; 1602d7058a79SJeff Kirsher 1603d7058a79SJeff Kirsher db->flags = pdata->flags; 1604d7058a79SJeff Kirsher } 1605d7058a79SJeff Kirsher 1606d7058a79SJeff Kirsher #ifdef CONFIG_DM9000_FORCE_SIMPLE_PHY_POLL 1607d7058a79SJeff Kirsher db->flags |= DM9000_PLATF_SIMPLE_PHY; 1608d7058a79SJeff Kirsher #endif 1609d7058a79SJeff Kirsher 1610751bb6fdSAndrew Ruder dm9000_reset(db); 1611d7058a79SJeff Kirsher 1612d7058a79SJeff Kirsher /* try multiple times, DM9000 sometimes gets the read wrong */ 1613d7058a79SJeff Kirsher for (i = 0; i < 8; i++) { 1614d7058a79SJeff Kirsher id_val = ior(db, DM9000_VIDL); 1615d7058a79SJeff Kirsher id_val |= (u32)ior(db, DM9000_VIDH) << 8; 1616d7058a79SJeff Kirsher id_val |= (u32)ior(db, DM9000_PIDL) << 16; 1617d7058a79SJeff Kirsher id_val |= (u32)ior(db, DM9000_PIDH) << 24; 1618d7058a79SJeff Kirsher 1619d7058a79SJeff Kirsher if (id_val == DM9000_ID) 1620d7058a79SJeff Kirsher break; 1621d7058a79SJeff Kirsher dev_err(db->dev, "read wrong id 0x%08x\n", id_val); 1622d7058a79SJeff Kirsher } 1623d7058a79SJeff Kirsher 1624d7058a79SJeff Kirsher if (id_val != DM9000_ID) { 1625d7058a79SJeff Kirsher dev_err(db->dev, "wrong id: 0x%08x\n", id_val); 1626d7058a79SJeff Kirsher ret = -ENODEV; 1627d7058a79SJeff Kirsher goto out; 1628d7058a79SJeff Kirsher } 1629d7058a79SJeff Kirsher 1630d7058a79SJeff Kirsher /* Identify what type of DM9000 we are working on */ 1631d7058a79SJeff Kirsher 1632d7058a79SJeff Kirsher id_val = ior(db, DM9000_CHIPR); 1633d7058a79SJeff Kirsher dev_dbg(db->dev, "dm9000 revision 0x%02x\n", id_val); 1634d7058a79SJeff Kirsher 1635d7058a79SJeff Kirsher switch (id_val) { 1636d7058a79SJeff Kirsher case CHIPR_DM9000A: 1637d7058a79SJeff Kirsher db->type = TYPE_DM9000A; 1638d7058a79SJeff Kirsher break; 1639d7058a79SJeff Kirsher case CHIPR_DM9000B: 1640d7058a79SJeff Kirsher db->type = TYPE_DM9000B; 1641d7058a79SJeff Kirsher break; 1642d7058a79SJeff Kirsher default: 1643d7058a79SJeff Kirsher dev_dbg(db->dev, "ID %02x => defaulting to DM9000E\n", id_val); 1644d7058a79SJeff Kirsher db->type = TYPE_DM9000E; 1645d7058a79SJeff Kirsher } 1646d7058a79SJeff Kirsher 1647d7058a79SJeff Kirsher /* dm9000a/b are capable of hardware checksum offload */ 1648d7058a79SJeff Kirsher if (db->type == TYPE_DM9000A || db->type == TYPE_DM9000B) { 1649d7058a79SJeff Kirsher ndev->hw_features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM; 1650d7058a79SJeff Kirsher ndev->features |= ndev->hw_features; 1651d7058a79SJeff Kirsher } 1652d7058a79SJeff Kirsher 1653d7058a79SJeff Kirsher /* from this point we assume that we have found a DM9000 */ 1654d7058a79SJeff Kirsher 1655d7058a79SJeff Kirsher ndev->netdev_ops = &dm9000_netdev_ops; 1656d7058a79SJeff Kirsher ndev->watchdog_timeo = msecs_to_jiffies(watchdog); 1657d7058a79SJeff Kirsher ndev->ethtool_ops = &dm9000_ethtool_ops; 1658d7058a79SJeff Kirsher 1659d7058a79SJeff Kirsher db->msg_enable = NETIF_MSG_LINK; 1660d7058a79SJeff Kirsher db->mii.phy_id_mask = 0x1f; 1661d7058a79SJeff Kirsher db->mii.reg_num_mask = 0x1f; 1662d7058a79SJeff Kirsher db->mii.force_media = 0; 1663d7058a79SJeff Kirsher db->mii.full_duplex = 0; 1664d7058a79SJeff Kirsher db->mii.dev = ndev; 1665d7058a79SJeff Kirsher db->mii.mdio_read = dm9000_phy_read; 1666d7058a79SJeff Kirsher db->mii.mdio_write = dm9000_phy_write; 1667d7058a79SJeff Kirsher 1668d7058a79SJeff Kirsher mac_src = "eeprom"; 1669d7058a79SJeff Kirsher 1670d7058a79SJeff Kirsher /* try reading the node address from the attached EEPROM */ 1671d7058a79SJeff Kirsher for (i = 0; i < 6; i += 2) 16724abd7cffSJakub Kicinski dm9000_read_eeprom(db, i / 2, addr + i); 16734abd7cffSJakub Kicinski eth_hw_addr_set(ndev, addr); 1674d7058a79SJeff Kirsher 1675d7058a79SJeff Kirsher if (!is_valid_ether_addr(ndev->dev_addr) && pdata != NULL) { 1676d7058a79SJeff Kirsher mac_src = "platform data"; 1677a96d317fSJakub Kicinski eth_hw_addr_set(ndev, pdata->dev_addr); 1678d7058a79SJeff Kirsher } 1679d7058a79SJeff Kirsher 1680d7058a79SJeff Kirsher if (!is_valid_ether_addr(ndev->dev_addr)) { 1681d7058a79SJeff Kirsher /* try reading from mac */ 1682d7058a79SJeff Kirsher 1683d7058a79SJeff Kirsher mac_src = "chip"; 1684d7058a79SJeff Kirsher for (i = 0; i < 6; i++) 16854abd7cffSJakub Kicinski addr[i] = ior(db, i + DM9000_PAR); 16864abd7cffSJakub Kicinski eth_hw_addr_set(ndev, pdata->dev_addr); 1687d7058a79SJeff Kirsher } 1688d7058a79SJeff Kirsher 1689d7058a79SJeff Kirsher if (!is_valid_ether_addr(ndev->dev_addr)) { 16903274940bSHarvey Hunt inv_mac_addr = true; 1691f2cedb63SDanny Kukawka eth_hw_addr_random(ndev); 1692d7058a79SJeff Kirsher mac_src = "random"; 1693d7058a79SJeff Kirsher } 1694d7058a79SJeff Kirsher 1695d7058a79SJeff Kirsher 1696d7058a79SJeff Kirsher platform_set_drvdata(pdev, ndev); 1697d7058a79SJeff Kirsher ret = register_netdev(ndev); 1698d7058a79SJeff Kirsher 16993274940bSHarvey Hunt if (ret == 0) { 17003274940bSHarvey Hunt if (inv_mac_addr) 17013274940bSHarvey Hunt dev_warn(db->dev, "%s: Invalid ethernet MAC address. Please set using ip\n", 17023274940bSHarvey Hunt ndev->name); 1703d7058a79SJeff Kirsher printk(KERN_INFO "%s: dm9000%c at %p,%p IRQ %d MAC: %pM (%s)\n", 1704d7058a79SJeff Kirsher ndev->name, dm9000_type_to_char(db->type), 1705d7058a79SJeff Kirsher db->io_addr, db->io_data, ndev->irq, 1706d7058a79SJeff Kirsher ndev->dev_addr, mac_src); 17073274940bSHarvey Hunt } 1708d7058a79SJeff Kirsher return 0; 1709d7058a79SJeff Kirsher 1710d7058a79SJeff Kirsher out: 1711d7058a79SJeff Kirsher dev_err(db->dev, "not found (%d).\n", ret); 1712d7058a79SJeff Kirsher 1713d7058a79SJeff Kirsher dm9000_release_board(pdev, db); 1714d7058a79SJeff Kirsher free_netdev(ndev); 1715d7058a79SJeff Kirsher 1716ac88c531SPaul Cercueil out_regulator_disable: 1717ac88c531SPaul Cercueil if (!IS_ERR(power)) 1718ac88c531SPaul Cercueil regulator_disable(power); 1719ac88c531SPaul Cercueil 1720d7058a79SJeff Kirsher return ret; 1721d7058a79SJeff Kirsher } 1722d7058a79SJeff Kirsher 1723d7058a79SJeff Kirsher static int 1724d7058a79SJeff Kirsher dm9000_drv_suspend(struct device *dev) 1725d7058a79SJeff Kirsher { 17263fcdaad3SWolfram Sang struct net_device *ndev = dev_get_drvdata(dev); 17272b162928SHimangi Saraogi struct board_info *db; 1728d7058a79SJeff Kirsher 1729d7058a79SJeff Kirsher if (ndev) { 1730d7058a79SJeff Kirsher db = netdev_priv(ndev); 1731d7058a79SJeff Kirsher db->in_suspend = 1; 1732d7058a79SJeff Kirsher 1733d7058a79SJeff Kirsher if (!netif_running(ndev)) 1734d7058a79SJeff Kirsher return 0; 1735d7058a79SJeff Kirsher 1736d7058a79SJeff Kirsher netif_device_detach(ndev); 1737d7058a79SJeff Kirsher 1738d7058a79SJeff Kirsher /* only shutdown if not using WoL */ 1739d7058a79SJeff Kirsher if (!db->wake_state) 1740d7058a79SJeff Kirsher dm9000_shutdown(ndev); 1741d7058a79SJeff Kirsher } 1742d7058a79SJeff Kirsher return 0; 1743d7058a79SJeff Kirsher } 1744d7058a79SJeff Kirsher 1745d7058a79SJeff Kirsher static int 1746d7058a79SJeff Kirsher dm9000_drv_resume(struct device *dev) 1747d7058a79SJeff Kirsher { 17483fcdaad3SWolfram Sang struct net_device *ndev = dev_get_drvdata(dev); 17492b162928SHimangi Saraogi struct board_info *db = netdev_priv(ndev); 1750d7058a79SJeff Kirsher 1751d7058a79SJeff Kirsher if (ndev) { 1752d7058a79SJeff Kirsher if (netif_running(ndev)) { 1753d7058a79SJeff Kirsher /* reset if we were not in wake mode to ensure if 1754d7058a79SJeff Kirsher * the device was powered off it is in a known state */ 1755d7058a79SJeff Kirsher if (!db->wake_state) { 1756d7058a79SJeff Kirsher dm9000_init_dm9000(ndev); 175717ad78deSAndrew Ruder dm9000_unmask_interrupts(db); 1758d7058a79SJeff Kirsher } 1759d7058a79SJeff Kirsher 1760d7058a79SJeff Kirsher netif_device_attach(ndev); 1761d7058a79SJeff Kirsher } 1762d7058a79SJeff Kirsher 1763d7058a79SJeff Kirsher db->in_suspend = 0; 1764d7058a79SJeff Kirsher } 1765d7058a79SJeff Kirsher return 0; 1766d7058a79SJeff Kirsher } 1767d7058a79SJeff Kirsher 1768d7058a79SJeff Kirsher static const struct dev_pm_ops dm9000_drv_pm_ops = { 1769d7058a79SJeff Kirsher .suspend = dm9000_drv_suspend, 1770d7058a79SJeff Kirsher .resume = dm9000_drv_resume, 1771d7058a79SJeff Kirsher }; 1772d7058a79SJeff Kirsher 17736b6a3e7fSBill Pemberton static int 1774d7058a79SJeff Kirsher dm9000_drv_remove(struct platform_device *pdev) 1775d7058a79SJeff Kirsher { 1776d7058a79SJeff Kirsher struct net_device *ndev = platform_get_drvdata(pdev); 1777cf9e60aaSPaul Cercueil struct board_info *dm = to_dm9000_board(ndev); 1778d7058a79SJeff Kirsher 1779d7058a79SJeff Kirsher unregister_netdev(ndev); 1780cf9e60aaSPaul Cercueil dm9000_release_board(pdev, dm); 1781d7058a79SJeff Kirsher free_netdev(ndev); /* free device structure */ 1782cf9e60aaSPaul Cercueil if (dm->power_supply) 1783cf9e60aaSPaul Cercueil regulator_disable(dm->power_supply); 1784d7058a79SJeff Kirsher 1785d7058a79SJeff Kirsher dev_dbg(&pdev->dev, "released and freed device\n"); 1786d7058a79SJeff Kirsher return 0; 1787d7058a79SJeff Kirsher } 1788d7058a79SJeff Kirsher 17890b8bf1baSTomasz Figa #ifdef CONFIG_OF 17900b8bf1baSTomasz Figa static const struct of_device_id dm9000_of_matches[] = { 17910b8bf1baSTomasz Figa { .compatible = "davicom,dm9000", }, 17920b8bf1baSTomasz Figa { /* sentinel */ } 17930b8bf1baSTomasz Figa }; 17940b8bf1baSTomasz Figa MODULE_DEVICE_TABLE(of, dm9000_of_matches); 17950b8bf1baSTomasz Figa #endif 17960b8bf1baSTomasz Figa 1797d7058a79SJeff Kirsher static struct platform_driver dm9000_driver = { 1798d7058a79SJeff Kirsher .driver = { 1799d7058a79SJeff Kirsher .name = "dm9000", 1800d7058a79SJeff Kirsher .pm = &dm9000_drv_pm_ops, 18010b8bf1baSTomasz Figa .of_match_table = of_match_ptr(dm9000_of_matches), 1802d7058a79SJeff Kirsher }, 1803d7058a79SJeff Kirsher .probe = dm9000_probe, 18046b6a3e7fSBill Pemberton .remove = dm9000_drv_remove, 1805d7058a79SJeff Kirsher }; 1806d7058a79SJeff Kirsher 1807a8f9c3e4SSachin Kamat module_platform_driver(dm9000_driver); 1808d7058a79SJeff Kirsher 1809d7058a79SJeff Kirsher MODULE_AUTHOR("Sascha Hauer, Ben Dooks"); 1810d7058a79SJeff Kirsher MODULE_DESCRIPTION("Davicom DM9000 network driver"); 1811d7058a79SJeff Kirsher MODULE_LICENSE("GPL"); 1812d7058a79SJeff Kirsher MODULE_ALIAS("platform:dm9000"); 1813