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