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