DM9000: Add support for MII ioctl() calls
[linux-2.6-block.git] / drivers / net / dm9000.c
CommitLineData
a1365275 1/*
41c340f0 2 * Davicom DM9000 Fast Ethernet driver for Linux.
a1365275
SH
3 * Copyright (C) 1997 Sten Wang
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
41c340f0 15 * (C) Copyright 1997-1998 DAVICOM Semiconductor,Inc. All Rights Reserved.
9ef9ac51 16 *
41c340f0
BD
17 * Additional updates, Copyright:
18 * Ben Dooks <ben@simtec.co.uk>
19 * Sascha Hauer <s.hauer@pengutronix.de>
a1365275
SH
20 */
21
22#include <linux/module.h>
23#include <linux/ioport.h>
24#include <linux/netdevice.h>
25#include <linux/etherdevice.h>
26#include <linux/init.h>
27#include <linux/skbuff.h>
a1365275
SH
28#include <linux/spinlock.h>
29#include <linux/crc32.h>
30#include <linux/mii.h>
7da99859 31#include <linux/ethtool.h>
a1365275
SH
32#include <linux/dm9000.h>
33#include <linux/delay.h>
d052d1be 34#include <linux/platform_device.h>
4e4fc05a 35#include <linux/irq.h>
a1365275
SH
36
37#include <asm/delay.h>
38#include <asm/irq.h>
39#include <asm/io.h>
40
41#include "dm9000.h"
42
43/* Board/System/Debug information/definition ---------------- */
44
45#define DM9000_PHY 0x40 /* PHY address 0x01 */
46
a1365275
SH
47#define CARDNAME "dm9000"
48#define PFX CARDNAME ": "
7da99859 49#define DRV_VERSION "1.30"
a1365275 50
f40d24d9
AL
51#ifdef CONFIG_BLACKFIN
52#define readsb insb
53#define readsw insw
54#define readsl insl
55#define writesb outsb
56#define writesw outsw
57#define writesl outsl
1a5f1c4f 58#define DEFAULT_TRIGGER IRQF_TRIGGER_HIGH
f40d24d9 59#else
1a5f1c4f 60#define DEFAULT_TRIGGER (0)
f40d24d9
AL
61#endif
62
a1365275
SH
63/*
64 * Transmit timeout, default 5 seconds.
65 */
66static int watchdog = 5000;
67module_param(watchdog, int, 0400);
68MODULE_PARM_DESC(watchdog, "transmit timeout in milliseconds");
69
9a2f037c
BD
70/* DM9000 register address locking.
71 *
72 * The DM9000 uses an address register to control where data written
73 * to the data register goes. This means that the address register
74 * must be preserved over interrupts or similar calls.
75 *
76 * During interrupt and other critical calls, a spinlock is used to
77 * protect the system, but the calls themselves save the address
78 * in the address register in case they are interrupting another
79 * access to the device.
80 *
81 * For general accesses a lock is provided so that calls which are
82 * allowed to sleep are serialised so that the address register does
83 * not need to be saved. This lock also serves to serialise access
84 * to the EEPROM and PHY access registers which are shared between
85 * these two devices.
86 */
87
a1365275
SH
88/* Structure/enum declaration ------------------------------- */
89typedef struct board_info {
90
91 void __iomem *io_addr; /* Register I/O base address */
92 void __iomem *io_data; /* Data I/O address */
93 u16 irq; /* IRQ */
94
95 u16 tx_pkt_cnt;
96 u16 queue_pkt_len;
97 u16 queue_start_addr;
98 u16 dbug_cnt;
99 u8 io_mode; /* 0:word, 2:byte */
100 u8 phy_addr;
33ba5091 101 unsigned int flags;
321f69a4 102 unsigned int in_suspend :1;
a1365275 103
5b2b4ff0
BD
104 int debug_level;
105
a1365275
SH
106 void (*inblk)(void __iomem *port, void *data, int length);
107 void (*outblk)(void __iomem *port, void *data, int length);
108 void (*dumpblk)(void __iomem *port, int length);
109
a76836f9
BD
110 struct device *dev; /* parent device */
111
a1365275
SH
112 struct resource *addr_res; /* resources found */
113 struct resource *data_res;
114 struct resource *addr_req; /* resources requested */
115 struct resource *data_req;
116 struct resource *irq_res;
117
9a2f037c
BD
118 struct mutex addr_lock; /* phy and eeprom access lock */
119
a1365275
SH
120 spinlock_t lock;
121
122 struct mii_if_info mii;
123 u32 msg_enable;
124} board_info_t;
125
5b2b4ff0
BD
126/* debug code */
127
128#define dm9000_dbg(db, lev, msg...) do { \
129 if ((lev) < CONFIG_DM9000_DEBUGLEVEL && \
130 (lev) < db->debug_level) { \
131 dev_dbg(db->dev, msg); \
132 } \
133} while (0)
134
7da99859
BD
135static inline board_info_t *to_dm9000_board(struct net_device *dev)
136{
137 return dev->priv;
138}
139
a1365275 140/* function declaration ------------------------------------- */
3ae5eaec 141static int dm9000_probe(struct platform_device *);
a1365275
SH
142static int dm9000_open(struct net_device *);
143static int dm9000_start_xmit(struct sk_buff *, struct net_device *);
144static int dm9000_stop(struct net_device *);
f42d8aea 145static int dm9000_ioctl(struct net_device *dev, struct ifreq *req, int cmd);
a1365275 146
a1365275
SH
147static void dm9000_init_dm9000(struct net_device *);
148
7d12e780 149static irqreturn_t dm9000_interrupt(int, void *);
a1365275
SH
150
151static int dm9000_phy_read(struct net_device *dev, int phyaddr_unsused, int reg);
152static void dm9000_phy_write(struct net_device *dev, int phyaddr_unused, int reg,
153 int value);
86c62fab 154
29d52e54
BD
155static void dm9000_read_eeprom(board_info_t *, int addr, u8 *to);
156static void dm9000_write_eeprom(board_info_t *, int addr, u8 *dp);
a1365275
SH
157static void dm9000_rx(struct net_device *);
158static void dm9000_hash_table(struct net_device *);
159
a1365275
SH
160/* DM9000 network board routine ---------------------------- */
161
162static void
163dm9000_reset(board_info_t * db)
164{
a76836f9
BD
165 dev_dbg(db->dev, "resetting device\n");
166
a1365275
SH
167 /* RESET device */
168 writeb(DM9000_NCR, db->io_addr);
169 udelay(200);
170 writeb(NCR_RST, db->io_data);
171 udelay(200);
172}
173
174/*
175 * Read a byte from I/O port
176 */
177static u8
178ior(board_info_t * db, int reg)
179{
180 writeb(reg, db->io_addr);
181 return readb(db->io_data);
182}
183
184/*
185 * Write a byte to I/O port
186 */
187
188static void
189iow(board_info_t * db, int reg, int value)
190{
191 writeb(reg, db->io_addr);
192 writeb(value, db->io_data);
193}
194
195/* routines for sending block to chip */
196
197static void dm9000_outblk_8bit(void __iomem *reg, void *data, int count)
198{
199 writesb(reg, data, count);
200}
201
202static void dm9000_outblk_16bit(void __iomem *reg, void *data, int count)
203{
204 writesw(reg, data, (count+1) >> 1);
205}
206
207static void dm9000_outblk_32bit(void __iomem *reg, void *data, int count)
208{
209 writesl(reg, data, (count+3) >> 2);
210}
211
212/* input block from chip to memory */
213
214static void dm9000_inblk_8bit(void __iomem *reg, void *data, int count)
215{
5f6b5517 216 readsb(reg, data, count);
a1365275
SH
217}
218
219
220static void dm9000_inblk_16bit(void __iomem *reg, void *data, int count)
221{
222 readsw(reg, data, (count+1) >> 1);
223}
224
225static void dm9000_inblk_32bit(void __iomem *reg, void *data, int count)
226{
227 readsl(reg, data, (count+3) >> 2);
228}
229
230/* dump block from chip to null */
231
232static void dm9000_dumpblk_8bit(void __iomem *reg, int count)
233{
234 int i;
235 int tmp;
236
237 for (i = 0; i < count; i++)
238 tmp = readb(reg);
239}
240
241static void dm9000_dumpblk_16bit(void __iomem *reg, int count)
242{
243 int i;
244 int tmp;
245
246 count = (count + 1) >> 1;
247
248 for (i = 0; i < count; i++)
249 tmp = readw(reg);
250}
251
252static void dm9000_dumpblk_32bit(void __iomem *reg, int count)
253{
254 int i;
255 int tmp;
256
257 count = (count + 3) >> 2;
258
259 for (i = 0; i < count; i++)
260 tmp = readl(reg);
261}
262
263/* dm9000_set_io
264 *
265 * select the specified set of io routines to use with the
266 * device
267 */
268
269static void dm9000_set_io(struct board_info *db, int byte_width)
270{
271 /* use the size of the data resource to work out what IO
272 * routines we want to use
273 */
274
275 switch (byte_width) {
276 case 1:
277 db->dumpblk = dm9000_dumpblk_8bit;
278 db->outblk = dm9000_outblk_8bit;
279 db->inblk = dm9000_inblk_8bit;
280 break;
281
a1365275
SH
282
283 case 3:
a76836f9
BD
284 dev_dbg(db->dev, ": 3 byte IO, falling back to 16bit\n");
285 case 2:
a1365275
SH
286 db->dumpblk = dm9000_dumpblk_16bit;
287 db->outblk = dm9000_outblk_16bit;
288 db->inblk = dm9000_inblk_16bit;
289 break;
290
291 case 4:
292 default:
293 db->dumpblk = dm9000_dumpblk_32bit;
294 db->outblk = dm9000_outblk_32bit;
295 db->inblk = dm9000_inblk_32bit;
296 break;
297 }
298}
299
300
301/* Our watchdog timed out. Called by the networking layer */
302static void dm9000_timeout(struct net_device *dev)
303{
304 board_info_t *db = (board_info_t *) dev->priv;
305 u8 reg_save;
306 unsigned long flags;
307
308 /* Save previous register address */
309 reg_save = readb(db->io_addr);
9ef9ac51 310 spin_lock_irqsave(&db->lock,flags);
a1365275
SH
311
312 netif_stop_queue(dev);
313 dm9000_reset(db);
314 dm9000_init_dm9000(dev);
315 /* We can accept TX packets again */
316 dev->trans_start = jiffies;
317 netif_wake_queue(dev);
318
319 /* Restore previous register address */
320 writeb(reg_save, db->io_addr);
9ef9ac51 321 spin_unlock_irqrestore(&db->lock,flags);
a1365275
SH
322}
323
2fd0e33f
KH
324#ifdef CONFIG_NET_POLL_CONTROLLER
325/*
326 *Used by netconsole
327 */
328static void dm9000_poll_controller(struct net_device *dev)
329{
330 disable_irq(dev->irq);
28431146 331 dm9000_interrupt(dev->irq,dev);
2fd0e33f
KH
332 enable_irq(dev->irq);
333}
334#endif
a1365275 335
f42d8aea
BD
336static int dm9000_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
337{
338 board_info_t *dm = to_dm9000_board(dev);
339
340 if (!netif_running(dev))
341 return -EINVAL;
342
343 return generic_mii_ioctl(&dm->mii, if_mii(req), cmd, NULL);
344}
345
7da99859
BD
346/* ethtool ops */
347
348static void dm9000_get_drvinfo(struct net_device *dev,
349 struct ethtool_drvinfo *info)
350{
351 board_info_t *dm = to_dm9000_board(dev);
352
353 strcpy(info->driver, CARDNAME);
354 strcpy(info->version, DRV_VERSION);
355 strcpy(info->bus_info, to_platform_device(dm->dev)->name);
356}
357
e662ee02
BD
358static u32 dm9000_get_msglevel(struct net_device *dev)
359{
360 board_info_t *dm = to_dm9000_board(dev);
361
362 return dm->msg_enable;
363}
364
365static void dm9000_set_msglevel(struct net_device *dev, u32 value)
366{
367 board_info_t *dm = to_dm9000_board(dev);
368
369 dm->msg_enable = value;
370}
371
7da99859
BD
372static int dm9000_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
373{
374 board_info_t *dm = to_dm9000_board(dev);
7da99859 375
7da99859 376 mii_ethtool_gset(&dm->mii, cmd);
7da99859
BD
377 return 0;
378}
379
380static int dm9000_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
381{
382 board_info_t *dm = to_dm9000_board(dev);
7da99859 383
9a2f037c 384 return mii_ethtool_sset(&dm->mii, cmd);
7da99859
BD
385}
386
387static int dm9000_nway_reset(struct net_device *dev)
388{
389 board_info_t *dm = to_dm9000_board(dev);
390 return mii_nway_restart(&dm->mii);
391}
392
393static u32 dm9000_get_link(struct net_device *dev)
394{
395 board_info_t *dm = to_dm9000_board(dev);
396 return mii_link_ok(&dm->mii);
397}
398
29d52e54
BD
399#define DM_EEPROM_MAGIC (0x444D394B)
400
401static int dm9000_get_eeprom_len(struct net_device *dev)
402{
403 return 128;
404}
405
406static int dm9000_get_eeprom(struct net_device *dev,
407 struct ethtool_eeprom *ee, u8 *data)
408{
409 board_info_t *dm = to_dm9000_board(dev);
410 int offset = ee->offset;
411 int len = ee->len;
412 int i;
413
414 /* EEPROM access is aligned to two bytes */
415
416 if ((len & 1) != 0 || (offset & 1) != 0)
417 return -EINVAL;
418
bb44fb70
BD
419 if (dm->flags & DM9000_PLATF_NO_EEPROM)
420 return -ENOENT;
421
29d52e54
BD
422 ee->magic = DM_EEPROM_MAGIC;
423
424 for (i = 0; i < len; i += 2)
425 dm9000_read_eeprom(dm, (offset + i) / 2, data + i);
426
427 return 0;
428}
429
430static int dm9000_set_eeprom(struct net_device *dev,
431 struct ethtool_eeprom *ee, u8 *data)
432{
433 board_info_t *dm = to_dm9000_board(dev);
434 int offset = ee->offset;
435 int len = ee->len;
436 int i;
437
438 /* EEPROM access is aligned to two bytes */
439
440 if ((len & 1) != 0 || (offset & 1) != 0)
441 return -EINVAL;
442
bb44fb70
BD
443 if (dm->flags & DM9000_PLATF_NO_EEPROM)
444 return -ENOENT;
445
29d52e54
BD
446 if (ee->magic != DM_EEPROM_MAGIC)
447 return -EINVAL;
448
449 for (i = 0; i < len; i += 2)
450 dm9000_write_eeprom(dm, (offset + i) / 2, data + i);
451
452 return 0;
453}
454
7da99859
BD
455static const struct ethtool_ops dm9000_ethtool_ops = {
456 .get_drvinfo = dm9000_get_drvinfo,
457 .get_settings = dm9000_get_settings,
458 .set_settings = dm9000_set_settings,
e662ee02
BD
459 .get_msglevel = dm9000_get_msglevel,
460 .set_msglevel = dm9000_set_msglevel,
7da99859
BD
461 .nway_reset = dm9000_nway_reset,
462 .get_link = dm9000_get_link,
29d52e54
BD
463 .get_eeprom_len = dm9000_get_eeprom_len,
464 .get_eeprom = dm9000_get_eeprom,
465 .set_eeprom = dm9000_set_eeprom,
7da99859
BD
466};
467
468
a1365275
SH
469/* dm9000_release_board
470 *
471 * release a board, and any mapped resources
472 */
473
474static void
475dm9000_release_board(struct platform_device *pdev, struct board_info *db)
476{
477 if (db->data_res == NULL) {
478 if (db->addr_res != NULL)
479 release_mem_region((unsigned long)db->io_addr, 4);
480 return;
481 }
482
483 /* unmap our resources */
484
485 iounmap(db->io_addr);
486 iounmap(db->io_data);
487
488 /* release the resources */
489
490 if (db->data_req != NULL) {
491 release_resource(db->data_req);
492 kfree(db->data_req);
493 }
494
51985487
DO
495 if (db->addr_req != NULL) {
496 release_resource(db->addr_req);
a1365275
SH
497 kfree(db->addr_req);
498 }
499}
500
501#define res_size(_r) (((_r)->end - (_r)->start) + 1)
502
503/*
504 * Search DM9000 board, allocate space and register it
505 */
506static int
3ae5eaec 507dm9000_probe(struct platform_device *pdev)
a1365275 508{
a1365275
SH
509 struct dm9000_plat_data *pdata = pdev->dev.platform_data;
510 struct board_info *db; /* Point a board information structure */
511 struct net_device *ndev;
512 unsigned long base;
513 int ret = 0;
514 int iosize;
515 int i;
516 u32 id_val;
517
a1365275
SH
518 /* Init network device */
519 ndev = alloc_etherdev(sizeof (struct board_info));
520 if (!ndev) {
a76836f9 521 dev_err(&pdev->dev, "could not allocate device.\n");
a1365275
SH
522 return -ENOMEM;
523 }
524
3ae5eaec 525 SET_NETDEV_DEV(ndev, &pdev->dev);
a1365275 526
a76836f9 527 dev_dbg(&pdev->dev, "dm9000_probe()");
a1365275
SH
528
529 /* setup board info structure */
530 db = (struct board_info *) ndev->priv;
531 memset(db, 0, sizeof (*db));
532
a76836f9
BD
533 db->dev = &pdev->dev;
534
9ef9ac51 535 spin_lock_init(&db->lock);
9a2f037c 536 mutex_init(&db->addr_lock);
9ef9ac51 537
a1365275
SH
538 if (pdev->num_resources < 2) {
539 ret = -ENODEV;
540 goto out;
b4ed03ff 541 } else if (pdev->num_resources == 2) {
a1365275
SH
542 base = pdev->resource[0].start;
543
544 if (!request_mem_region(base, 4, ndev->name)) {
545 ret = -EBUSY;
546 goto out;
547 }
548
549 ndev->base_addr = base;
550 ndev->irq = pdev->resource[1].start;
b4ed03ff
BD
551 db->io_addr = (void __iomem *)base;
552 db->io_data = (void __iomem *)(base + 4);
a1365275 553
f40d24d9
AL
554 /* ensure at least we have a default set of IO routines */
555 dm9000_set_io(db, 2);
556
b4ed03ff 557 } else {
a1365275
SH
558 db->addr_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
559 db->data_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
560 db->irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
561
b4ed03ff
BD
562 if (db->addr_res == NULL || db->data_res == NULL ||
563 db->irq_res == NULL) {
a76836f9 564 dev_err(db->dev, "insufficient resources\n");
a1365275
SH
565 ret = -ENOENT;
566 goto out;
567 }
568
569 i = res_size(db->addr_res);
570 db->addr_req = request_mem_region(db->addr_res->start, i,
571 pdev->name);
572
573 if (db->addr_req == NULL) {
a76836f9 574 dev_err(db->dev, "cannot claim address reg area\n");
a1365275
SH
575 ret = -EIO;
576 goto out;
577 }
578
579 db->io_addr = ioremap(db->addr_res->start, i);
580
581 if (db->io_addr == NULL) {
a76836f9 582 dev_err(db->dev, "failed to ioremap address reg\n");
a1365275
SH
583 ret = -EINVAL;
584 goto out;
585 }
586
587 iosize = res_size(db->data_res);
588 db->data_req = request_mem_region(db->data_res->start, iosize,
589 pdev->name);
590
591 if (db->data_req == NULL) {
a76836f9 592 dev_err(db->dev, "cannot claim data reg area\n");
a1365275
SH
593 ret = -EIO;
594 goto out;
595 }
596
597 db->io_data = ioremap(db->data_res->start, iosize);
598
599 if (db->io_data == NULL) {
a76836f9 600 dev_err(db->dev,"failed to ioremap data reg\n");
a1365275
SH
601 ret = -EINVAL;
602 goto out;
603 }
604
605 /* fill in parameters for net-dev structure */
606
607 ndev->base_addr = (unsigned long)db->io_addr;
608 ndev->irq = db->irq_res->start;
609
610 /* ensure at least we have a default set of IO routines */
611 dm9000_set_io(db, iosize);
a1365275
SH
612 }
613
614 /* check to see if anything is being over-ridden */
615 if (pdata != NULL) {
616 /* check to see if the driver wants to over-ride the
617 * default IO width */
618
619 if (pdata->flags & DM9000_PLATF_8BITONLY)
620 dm9000_set_io(db, 1);
621
622 if (pdata->flags & DM9000_PLATF_16BITONLY)
623 dm9000_set_io(db, 2);
624
625 if (pdata->flags & DM9000_PLATF_32BITONLY)
626 dm9000_set_io(db, 4);
627
628 /* check to see if there are any IO routine
629 * over-rides */
630
631 if (pdata->inblk != NULL)
632 db->inblk = pdata->inblk;
633
634 if (pdata->outblk != NULL)
635 db->outblk = pdata->outblk;
636
637 if (pdata->dumpblk != NULL)
638 db->dumpblk = pdata->dumpblk;
33ba5091
BD
639
640 db->flags = pdata->flags;
a1365275
SH
641 }
642
643 dm9000_reset(db);
644
645 /* try two times, DM9000 sometimes gets the first read wrong */
646 for (i = 0; i < 2; i++) {
647 id_val = ior(db, DM9000_VIDL);
648 id_val |= (u32)ior(db, DM9000_VIDH) << 8;
649 id_val |= (u32)ior(db, DM9000_PIDL) << 16;
650 id_val |= (u32)ior(db, DM9000_PIDH) << 24;
651
652 if (id_val == DM9000_ID)
653 break;
a76836f9 654 dev_err(db->dev, "read wrong id 0x%08x\n", id_val);
a1365275
SH
655 }
656
657 if (id_val != DM9000_ID) {
a76836f9 658 dev_err(db->dev, "wrong id: 0x%08x\n", id_val);
418d6f87
MR
659 ret = -ENODEV;
660 goto out;
a1365275
SH
661 }
662
663 /* from this point we assume that we have found a DM9000 */
664
665 /* driver system function */
666 ether_setup(ndev);
667
668 ndev->open = &dm9000_open;
669 ndev->hard_start_xmit = &dm9000_start_xmit;
670 ndev->tx_timeout = &dm9000_timeout;
671 ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
672 ndev->stop = &dm9000_stop;
a1365275 673 ndev->set_multicast_list = &dm9000_hash_table;
7da99859 674 ndev->ethtool_ops = &dm9000_ethtool_ops;
f42d8aea 675 ndev->do_ioctl = &dm9000_ioctl;
7da99859 676
2fd0e33f
KH
677#ifdef CONFIG_NET_POLL_CONTROLLER
678 ndev->poll_controller = &dm9000_poll_controller;
679#endif
a1365275 680
a1365275
SH
681 db->msg_enable = NETIF_MSG_LINK;
682 db->mii.phy_id_mask = 0x1f;
683 db->mii.reg_num_mask = 0x1f;
684 db->mii.force_media = 0;
685 db->mii.full_duplex = 0;
686 db->mii.dev = ndev;
687 db->mii.mdio_read = dm9000_phy_read;
688 db->mii.mdio_write = dm9000_phy_write;
689
86c62fab
BD
690 /* try reading the node address from the attached EEPROM */
691 for (i = 0; i < 6; i += 2)
692 dm9000_read_eeprom(db, i / 2, ndev->dev_addr+i);
a1365275 693
5b55dda6
BD
694 if (!is_valid_ether_addr(ndev->dev_addr)) {
695 /* try reading from mac */
696
697 for (i = 0; i < 6; i++)
698 ndev->dev_addr[i] = ior(db, i+DM9000_PAR);
699 }
700
a1365275 701 if (!is_valid_ether_addr(ndev->dev_addr))
a76836f9
BD
702 dev_warn(db->dev, "%s: Invalid ethernet MAC address. Please "
703 "set using ifconfig\n", ndev->name);
a1365275 704
3ae5eaec 705 platform_set_drvdata(pdev, ndev);
a1365275
SH
706 ret = register_netdev(ndev);
707
708 if (ret == 0) {
0795af57
JP
709 DECLARE_MAC_BUF(mac);
710 printk("%s: dm9000 at %p,%p IRQ %d MAC: %s\n",
711 ndev->name, db->io_addr, db->io_data, ndev->irq,
712 print_mac(mac, ndev->dev_addr));
a1365275
SH
713 }
714 return 0;
715
418d6f87 716out:
a76836f9 717 dev_err(db->dev, "not found (%d).\n", ret);
a1365275
SH
718
719 dm9000_release_board(pdev, db);
9fd9f9b6 720 free_netdev(ndev);
a1365275
SH
721
722 return ret;
723}
724
725/*
726 * Open the interface.
727 * The interface is opened whenever "ifconfig" actives it.
728 */
729static int
730dm9000_open(struct net_device *dev)
731{
732 board_info_t *db = (board_info_t *) dev->priv;
1a5f1c4f 733 unsigned long irqflags = db->irq_res->flags & IRQF_TRIGGER_MASK;
a1365275 734
c991d168
BD
735 if (netif_msg_ifup(db))
736 dev_dbg(db->dev, "enabling %s\n", dev->name);
a1365275 737
1a5f1c4f
BD
738 /* If there is no IRQ type specified, default to something that
739 * may work, and tell the user that this is a problem */
740
741 if (irqflags == IRQF_TRIGGER_NONE) {
742 dev_warn(db->dev, "WARNING: no IRQ resource flags set.\n");
743 irqflags = DEFAULT_TRIGGER;
744 }
745
746 irqflags |= IRQF_SHARED;
747
748 if (request_irq(dev->irq, &dm9000_interrupt, irqflags, dev->name, dev))
a1365275
SH
749 return -EAGAIN;
750
751 /* Initialize DM9000 board */
752 dm9000_reset(db);
753 dm9000_init_dm9000(dev);
754
755 /* Init driver variable */
756 db->dbug_cnt = 0;
757
a1365275
SH
758 mii_check_media(&db->mii, netif_msg_link(db), 1);
759 netif_start_queue(dev);
760
761 return 0;
762}
763
764/*
765 * Initilize dm9000 board
766 */
767static void
768dm9000_init_dm9000(struct net_device *dev)
769{
770 board_info_t *db = (board_info_t *) dev->priv;
771
5b2b4ff0 772 dm9000_dbg(db, 1, "entering %s\n", __func__);
a1365275
SH
773
774 /* I/O mode */
775 db->io_mode = ior(db, DM9000_ISR) >> 6; /* ISR bit7:6 keeps I/O mode */
776
777 /* GPIO0 on pre-activate PHY */
778 iow(db, DM9000_GPR, 0); /* REG_1F bit0 activate phyxcer */
779 iow(db, DM9000_GPCR, GPCR_GEP_CNTL); /* Let GPIO0 output */
780 iow(db, DM9000_GPR, 0); /* Enable PHY */
781
33ba5091
BD
782 if (db->flags & DM9000_PLATF_EXT_PHY)
783 iow(db, DM9000_NCR, NCR_EXT_PHY);
784
a1365275
SH
785 /* Program operating register */
786 iow(db, DM9000_TCR, 0); /* TX Polling clear */
787 iow(db, DM9000_BPTR, 0x3f); /* Less 3Kb, 200us */
788 iow(db, DM9000_FCR, 0xff); /* Flow Control */
789 iow(db, DM9000_SMCR, 0); /* Special Mode */
790 /* clear TX status */
791 iow(db, DM9000_NSR, NSR_WAKEST | NSR_TX2END | NSR_TX1END);
792 iow(db, DM9000_ISR, ISR_CLR_STATUS); /* Clear interrupt status */
793
794 /* Set address filter table */
795 dm9000_hash_table(dev);
796
797 /* Activate DM9000 */
798 iow(db, DM9000_RCR, RCR_DIS_LONG | RCR_DIS_CRC | RCR_RXEN);
799 /* Enable TX/RX interrupt mask */
800 iow(db, DM9000_IMR, IMR_PAR | IMR_PTM | IMR_PRM);
801
802 /* Init Driver variable */
803 db->tx_pkt_cnt = 0;
804 db->queue_pkt_len = 0;
805 dev->trans_start = 0;
a1365275
SH
806}
807
808/*
809 * Hardware start transmission.
810 * Send a packet to media from the upper layer.
811 */
812static int
813dm9000_start_xmit(struct sk_buff *skb, struct net_device *dev)
814{
c46ac946 815 unsigned long flags;
a1365275
SH
816 board_info_t *db = (board_info_t *) dev->priv;
817
5b2b4ff0 818 dm9000_dbg(db, 3, "%s:\n", __func__);
a1365275
SH
819
820 if (db->tx_pkt_cnt > 1)
821 return 1;
822
c46ac946 823 spin_lock_irqsave(&db->lock, flags);
a1365275
SH
824
825 /* Move data to DM9000 TX RAM */
826 writeb(DM9000_MWCMD, db->io_addr);
827
828 (db->outblk)(db->io_data, skb->data, skb->len);
09f75cd7 829 dev->stats.tx_bytes += skb->len;
a1365275 830
c46ac946 831 db->tx_pkt_cnt++;
a1365275 832 /* TX control: First packet immediately send, second packet queue */
c46ac946 833 if (db->tx_pkt_cnt == 1) {
a1365275 834 /* Set TX length to DM9000 */
073d3f46
BD
835 iow(db, DM9000_TXPLL, skb->len);
836 iow(db, DM9000_TXPLH, skb->len >> 8);
a1365275
SH
837
838 /* Issue TX polling command */
839 iow(db, DM9000_TCR, TCR_TXREQ); /* Cleared after TX complete */
840
841 dev->trans_start = jiffies; /* save the time stamp */
a1365275
SH
842 } else {
843 /* Second packet */
a1365275 844 db->queue_pkt_len = skb->len;
c46ac946 845 netif_stop_queue(dev);
a1365275
SH
846 }
847
c46ac946
FW
848 spin_unlock_irqrestore(&db->lock, flags);
849
a1365275
SH
850 /* free this SKB */
851 dev_kfree_skb(skb);
852
a1365275
SH
853 return 0;
854}
855
856static void
857dm9000_shutdown(struct net_device *dev)
858{
859 board_info_t *db = (board_info_t *) dev->priv;
860
861 /* RESET device */
862 dm9000_phy_write(dev, 0, MII_BMCR, BMCR_RESET); /* PHY RESET */
863 iow(db, DM9000_GPR, 0x01); /* Power-Down PHY */
864 iow(db, DM9000_IMR, IMR_PAR); /* Disable all interrupt */
865 iow(db, DM9000_RCR, 0x00); /* Disable RX */
866}
867
868/*
869 * Stop the interface.
870 * The interface is stopped when it is brought.
871 */
872static int
873dm9000_stop(struct net_device *ndev)
874{
875 board_info_t *db = (board_info_t *) ndev->priv;
876
c991d168
BD
877 if (netif_msg_ifdown(db))
878 dev_dbg(db->dev, "shutting down %s\n", ndev->name);
a1365275 879
a1365275
SH
880 netif_stop_queue(ndev);
881 netif_carrier_off(ndev);
882
883 /* free interrupt */
884 free_irq(ndev->irq, ndev);
885
886 dm9000_shutdown(ndev);
887
888 return 0;
889}
890
891/*
892 * DM9000 interrupt handler
893 * receive the packet to upper layer, free the transmitted packet
894 */
895
5d22a312 896static void
a1365275
SH
897dm9000_tx_done(struct net_device *dev, board_info_t * db)
898{
899 int tx_status = ior(db, DM9000_NSR); /* Got TX status */
900
901 if (tx_status & (NSR_TX2END | NSR_TX1END)) {
902 /* One packet sent complete */
903 db->tx_pkt_cnt--;
09f75cd7 904 dev->stats.tx_packets++;
a1365275 905
c991d168
BD
906 if (netif_msg_tx_done(db))
907 dev_dbg(db->dev, "tx done, NSR %02x\n", tx_status);
908
a1365275
SH
909 /* Queue packet check & send */
910 if (db->tx_pkt_cnt > 0) {
073d3f46
BD
911 iow(db, DM9000_TXPLL, db->queue_pkt_len);
912 iow(db, DM9000_TXPLH, db->queue_pkt_len >> 8);
a1365275
SH
913 iow(db, DM9000_TCR, TCR_TXREQ);
914 dev->trans_start = jiffies;
915 }
916 netif_wake_queue(dev);
917 }
918}
919
920static irqreturn_t
7d12e780 921dm9000_interrupt(int irq, void *dev_id)
a1365275
SH
922{
923 struct net_device *dev = dev_id;
5b2b4ff0 924 board_info_t *db = (board_info_t *) dev->priv;
a1365275
SH
925 int int_status;
926 u8 reg_save;
927
5b2b4ff0 928 dm9000_dbg(db, 3, "entering %s\n", __func__);
a1365275
SH
929
930 /* A real interrupt coming */
5b2b4ff0 931
a1365275
SH
932 spin_lock(&db->lock);
933
934 /* Save previous register address */
935 reg_save = readb(db->io_addr);
936
937 /* Disable all interrupts */
938 iow(db, DM9000_IMR, IMR_PAR);
939
940 /* Got DM9000 interrupt status */
941 int_status = ior(db, DM9000_ISR); /* Got ISR */
942 iow(db, DM9000_ISR, int_status); /* Clear ISR status */
943
c991d168
BD
944 if (netif_msg_intr(db))
945 dev_dbg(db->dev, "interrupt status %02x\n", int_status);
946
a1365275
SH
947 /* Received the coming packet */
948 if (int_status & ISR_PRS)
949 dm9000_rx(dev);
950
951 /* Trnasmit Interrupt check */
952 if (int_status & ISR_PTS)
953 dm9000_tx_done(dev, db);
954
955 /* Re-enable interrupt mask */
956 iow(db, DM9000_IMR, IMR_PAR | IMR_PTM | IMR_PRM);
957
958 /* Restore previous register address */
959 writeb(reg_save, db->io_addr);
960
961 spin_unlock(&db->lock);
962
963 return IRQ_HANDLED;
964}
965
a1365275 966struct dm9000_rxhdr {
93116573
BD
967 u8 RxPktReady;
968 u8 RxStatus;
a1365275
SH
969 u16 RxLen;
970} __attribute__((__packed__));
971
972/*
973 * Received a packet and pass to upper layer
974 */
975static void
976dm9000_rx(struct net_device *dev)
977{
978 board_info_t *db = (board_info_t *) dev->priv;
979 struct dm9000_rxhdr rxhdr;
980 struct sk_buff *skb;
981 u8 rxbyte, *rdptr;
6478fac6 982 bool GoodPacket;
a1365275
SH
983 int RxLen;
984
985 /* Check packet ready or not */
986 do {
987 ior(db, DM9000_MRCMDX); /* Dummy read */
988
989 /* Get most updated data */
990 rxbyte = readb(db->io_data);
991
992 /* Status check: this byte must be 0 or 1 */
993 if (rxbyte > DM9000_PKT_RDY) {
a76836f9 994 dev_warn(db->dev, "status check fail: %d\n", rxbyte);
a1365275
SH
995 iow(db, DM9000_RCR, 0x00); /* Stop Device */
996 iow(db, DM9000_ISR, IMR_PAR); /* Stop INT request */
997 return;
998 }
999
1000 if (rxbyte != DM9000_PKT_RDY)
1001 return;
1002
1003 /* A packet ready now & Get status/length */
6478fac6 1004 GoodPacket = true;
a1365275
SH
1005 writeb(DM9000_MRCMD, db->io_addr);
1006
1007 (db->inblk)(db->io_data, &rxhdr, sizeof(rxhdr));
1008
93116573 1009 RxLen = le16_to_cpu(rxhdr.RxLen);
a1365275 1010
c991d168
BD
1011 if (netif_msg_rx_status(db))
1012 dev_dbg(db->dev, "RX: status %02x, length %04x\n",
1013 rxhdr.RxStatus, RxLen);
1014
a1365275
SH
1015 /* Packet Status check */
1016 if (RxLen < 0x40) {
6478fac6 1017 GoodPacket = false;
c991d168
BD
1018 if (netif_msg_rx_err(db))
1019 dev_dbg(db->dev, "RX: Bad Packet (runt)\n");
a1365275
SH
1020 }
1021
1022 if (RxLen > DM9000_PKT_MAX) {
a76836f9 1023 dev_dbg(db->dev, "RST: RX Len:%x\n", RxLen);
a1365275
SH
1024 }
1025
93116573 1026 if (rxhdr.RxStatus & 0xbf) {
6478fac6 1027 GoodPacket = false;
93116573 1028 if (rxhdr.RxStatus & 0x01) {
c991d168
BD
1029 if (netif_msg_rx_err(db))
1030 dev_dbg(db->dev, "fifo error\n");
09f75cd7 1031 dev->stats.rx_fifo_errors++;
a1365275 1032 }
93116573 1033 if (rxhdr.RxStatus & 0x02) {
c991d168
BD
1034 if (netif_msg_rx_err(db))
1035 dev_dbg(db->dev, "crc error\n");
09f75cd7 1036 dev->stats.rx_crc_errors++;
a1365275 1037 }
93116573 1038 if (rxhdr.RxStatus & 0x80) {
c991d168
BD
1039 if (netif_msg_rx_err(db))
1040 dev_dbg(db->dev, "length error\n");
09f75cd7 1041 dev->stats.rx_length_errors++;
a1365275
SH
1042 }
1043 }
1044
1045 /* Move data from DM9000 */
1046 if (GoodPacket
1047 && ((skb = dev_alloc_skb(RxLen + 4)) != NULL)) {
a1365275
SH
1048 skb_reserve(skb, 2);
1049 rdptr = (u8 *) skb_put(skb, RxLen - 4);
1050
1051 /* Read received packet from RX SRAM */
1052
1053 (db->inblk)(db->io_data, rdptr, RxLen);
09f75cd7 1054 dev->stats.rx_bytes += RxLen;
a1365275
SH
1055
1056 /* Pass to upper layer */
1057 skb->protocol = eth_type_trans(skb, dev);
1058 netif_rx(skb);
09f75cd7 1059 dev->stats.rx_packets++;
a1365275
SH
1060
1061 } else {
1062 /* need to dump the packet's data */
1063
1064 (db->dumpblk)(db->io_data, RxLen);
1065 }
1066 } while (rxbyte == DM9000_PKT_RDY);
1067}
1068
39c341a8
BD
1069static unsigned int
1070dm9000_read_locked(board_info_t *db, int reg)
1071{
1072 unsigned long flags;
1073 unsigned int ret;
1074
1075 spin_lock_irqsave(&db->lock, flags);
1076 ret = ior(db, reg);
1077 spin_unlock_irqrestore(&db->lock, flags);
1078
1079 return ret;
1080}
1081
1082static int dm9000_wait_eeprom(board_info_t *db)
1083{
1084 unsigned int status;
1085 int timeout = 8; /* wait max 8msec */
1086
1087 /* The DM9000 data sheets say we should be able to
1088 * poll the ERRE bit in EPCR to wait for the EEPROM
1089 * operation. From testing several chips, this bit
1090 * does not seem to work.
1091 *
1092 * We attempt to use the bit, but fall back to the
1093 * timeout (which is why we do not return an error
1094 * on expiry) to say that the EEPROM operation has
1095 * completed.
1096 */
1097
1098 while (1) {
1099 status = dm9000_read_locked(db, DM9000_EPCR);
1100
1101 if ((status & EPCR_ERRE) == 0)
1102 break;
1103
1104 if (timeout-- < 0) {
1105 dev_dbg(db->dev, "timeout waiting EEPROM\n");
1106 break;
1107 }
1108 }
1109
1110 return 0;
1111}
1112
a1365275 1113/*
86c62fab 1114 * Read a word data from EEPROM
a1365275 1115 */
86c62fab 1116static void
29d52e54 1117dm9000_read_eeprom(board_info_t *db, int offset, u8 *to)
a1365275 1118{
621ddcb0
BD
1119 unsigned long flags;
1120
bb44fb70
BD
1121 if (db->flags & DM9000_PLATF_NO_EEPROM) {
1122 to[0] = 0xff;
1123 to[1] = 0xff;
1124 return;
1125 }
1126
9a2f037c
BD
1127 mutex_lock(&db->addr_lock);
1128
621ddcb0
BD
1129 spin_lock_irqsave(&db->lock, flags);
1130
a1365275
SH
1131 iow(db, DM9000_EPAR, offset);
1132 iow(db, DM9000_EPCR, EPCR_ERPRR);
621ddcb0
BD
1133
1134 spin_unlock_irqrestore(&db->lock, flags);
1135
39c341a8
BD
1136 dm9000_wait_eeprom(db);
1137
1138 /* delay for at-least 150uS */
1139 msleep(1);
621ddcb0
BD
1140
1141 spin_lock_irqsave(&db->lock, flags);
1142
a1365275 1143 iow(db, DM9000_EPCR, 0x0);
86c62fab
BD
1144
1145 to[0] = ior(db, DM9000_EPDRL);
1146 to[1] = ior(db, DM9000_EPDRH);
9a2f037c 1147
621ddcb0
BD
1148 spin_unlock_irqrestore(&db->lock, flags);
1149
9a2f037c 1150 mutex_unlock(&db->addr_lock);
a1365275
SH
1151}
1152
a1365275
SH
1153/*
1154 * Write a word data to SROM
1155 */
1156static void
29d52e54 1157dm9000_write_eeprom(board_info_t *db, int offset, u8 *data)
a1365275 1158{
621ddcb0
BD
1159 unsigned long flags;
1160
bb44fb70
BD
1161 if (db->flags & DM9000_PLATF_NO_EEPROM)
1162 return;
1163
9a2f037c
BD
1164 mutex_lock(&db->addr_lock);
1165
621ddcb0 1166 spin_lock_irqsave(&db->lock, flags);
a1365275 1167 iow(db, DM9000_EPAR, offset);
29d52e54
BD
1168 iow(db, DM9000_EPDRH, data[1]);
1169 iow(db, DM9000_EPDRL, data[0]);
a1365275 1170 iow(db, DM9000_EPCR, EPCR_WEP | EPCR_ERPRW);
621ddcb0
BD
1171 spin_unlock_irqrestore(&db->lock, flags);
1172
39c341a8
BD
1173 dm9000_wait_eeprom(db);
1174
1175 mdelay(1); /* wait at least 150uS to clear */
621ddcb0
BD
1176
1177 spin_lock_irqsave(&db->lock, flags);
a1365275 1178 iow(db, DM9000_EPCR, 0);
621ddcb0 1179 spin_unlock_irqrestore(&db->lock, flags);
9a2f037c
BD
1180
1181 mutex_unlock(&db->addr_lock);
a1365275
SH
1182}
1183
a1365275
SH
1184/*
1185 * Set DM9000 multicast address
1186 */
1187static void
1188dm9000_hash_table(struct net_device *dev)
1189{
1190 board_info_t *db = (board_info_t *) dev->priv;
1191 struct dev_mc_list *mcptr = dev->mc_list;
1192 int mc_cnt = dev->mc_count;
d39cb786 1193 int i, oft;
a1365275 1194 u32 hash_val;
d39cb786 1195 u16 hash_table[4];
a1365275
SH
1196 unsigned long flags;
1197
5b2b4ff0 1198 dm9000_dbg(db, 1, "entering %s\n", __func__);
a1365275 1199
d39cb786 1200 spin_lock_irqsave(&db->lock, flags);
a1365275 1201
d39cb786 1202 for (i = 0, oft = DM9000_PAR; i < 6; i++, oft++)
a1365275
SH
1203 iow(db, oft, dev->dev_addr[i]);
1204
1205 /* Clear Hash Table */
1206 for (i = 0; i < 4; i++)
1207 hash_table[i] = 0x0;
1208
1209 /* broadcast address */
1210 hash_table[3] = 0x8000;
1211
1212 /* the multicast address in Hash Table : 64 bits */
1213 for (i = 0; i < mc_cnt; i++, mcptr = mcptr->next) {
d39cb786 1214 hash_val = ether_crc_le(6, mcptr->dmi_addr) & 0x3f;
a1365275
SH
1215 hash_table[hash_val / 16] |= (u16) 1 << (hash_val % 16);
1216 }
1217
1218 /* Write the hash table to MAC MD table */
d39cb786
BD
1219 for (i = 0, oft = DM9000_MAR; i < 4; i++) {
1220 iow(db, oft++, hash_table[i]);
1221 iow(db, oft++, hash_table[i] >> 8);
a1365275
SH
1222 }
1223
d39cb786 1224 spin_unlock_irqrestore(&db->lock, flags);
a1365275
SH
1225}
1226
1227
321f69a4
BD
1228/*
1229 * Sleep, either by using msleep() or if we are suspending, then
1230 * use mdelay() to sleep.
1231 */
1232static void dm9000_msleep(board_info_t *db, unsigned int ms)
1233{
1234 if (db->in_suspend)
1235 mdelay(ms);
1236 else
1237 msleep(ms);
1238}
1239
a1365275
SH
1240/*
1241 * Read a word from phyxcer
1242 */
1243static int
1244dm9000_phy_read(struct net_device *dev, int phy_reg_unused, int reg)
1245{
1246 board_info_t *db = (board_info_t *) dev->priv;
1247 unsigned long flags;
9ef9ac51 1248 unsigned int reg_save;
a1365275
SH
1249 int ret;
1250
9a2f037c
BD
1251 mutex_lock(&db->addr_lock);
1252
a1365275 1253 spin_lock_irqsave(&db->lock,flags);
9ef9ac51
BD
1254
1255 /* Save previous register address */
1256 reg_save = readb(db->io_addr);
1257
a1365275
SH
1258 /* Fill the phyxcer register into REG_0C */
1259 iow(db, DM9000_EPAR, DM9000_PHY | reg);
1260
1261 iow(db, DM9000_EPCR, 0xc); /* Issue phyxcer read command */
89c8b0e6
BD
1262
1263 writeb(reg_save, db->io_addr);
1264 spin_unlock_irqrestore(&db->lock,flags);
1265
321f69a4 1266 dm9000_msleep(db, 1); /* Wait read complete */
89c8b0e6
BD
1267
1268 spin_lock_irqsave(&db->lock,flags);
1269 reg_save = readb(db->io_addr);
1270
a1365275
SH
1271 iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer read command */
1272
1273 /* The read data keeps on REG_0D & REG_0E */
1274 ret = (ior(db, DM9000_EPDRH) << 8) | ior(db, DM9000_EPDRL);
1275
9ef9ac51
BD
1276 /* restore the previous address */
1277 writeb(reg_save, db->io_addr);
a1365275
SH
1278 spin_unlock_irqrestore(&db->lock,flags);
1279
9a2f037c 1280 mutex_unlock(&db->addr_lock);
a1365275
SH
1281 return ret;
1282}
1283
1284/*
1285 * Write a word to phyxcer
1286 */
1287static void
1288dm9000_phy_write(struct net_device *dev, int phyaddr_unused, int reg, int value)
1289{
1290 board_info_t *db = (board_info_t *) dev->priv;
1291 unsigned long flags;
9ef9ac51 1292 unsigned long reg_save;
a1365275 1293
9a2f037c
BD
1294 mutex_lock(&db->addr_lock);
1295
a1365275
SH
1296 spin_lock_irqsave(&db->lock,flags);
1297
9ef9ac51
BD
1298 /* Save previous register address */
1299 reg_save = readb(db->io_addr);
1300
a1365275
SH
1301 /* Fill the phyxcer register into REG_0C */
1302 iow(db, DM9000_EPAR, DM9000_PHY | reg);
1303
1304 /* Fill the written data into REG_0D & REG_0E */
073d3f46
BD
1305 iow(db, DM9000_EPDRL, value);
1306 iow(db, DM9000_EPDRH, value >> 8);
a1365275
SH
1307
1308 iow(db, DM9000_EPCR, 0xa); /* Issue phyxcer write command */
89c8b0e6
BD
1309
1310 writeb(reg_save, db->io_addr);
9a2f037c 1311 spin_unlock_irqrestore(&db->lock, flags);
89c8b0e6 1312
321f69a4 1313 dm9000_msleep(db, 1); /* Wait write complete */
89c8b0e6
BD
1314
1315 spin_lock_irqsave(&db->lock,flags);
1316 reg_save = readb(db->io_addr);
1317
a1365275
SH
1318 iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer write command */
1319
9ef9ac51
BD
1320 /* restore the previous address */
1321 writeb(reg_save, db->io_addr);
1322
9a2f037c
BD
1323 spin_unlock_irqrestore(&db->lock, flags);
1324 mutex_unlock(&db->addr_lock);
a1365275
SH
1325}
1326
1327static int
3ae5eaec 1328dm9000_drv_suspend(struct platform_device *dev, pm_message_t state)
a1365275 1329{
3ae5eaec 1330 struct net_device *ndev = platform_get_drvdata(dev);
321f69a4 1331 board_info_t *db;
a1365275 1332
9480e307 1333 if (ndev) {
321f69a4
BD
1334 db = (board_info_t *) ndev->priv;
1335 db->in_suspend = 1;
1336
a1365275
SH
1337 if (netif_running(ndev)) {
1338 netif_device_detach(ndev);
1339 dm9000_shutdown(ndev);
1340 }
1341 }
1342 return 0;
1343}
1344
1345static int
3ae5eaec 1346dm9000_drv_resume(struct platform_device *dev)
a1365275 1347{
3ae5eaec 1348 struct net_device *ndev = platform_get_drvdata(dev);
a1365275
SH
1349 board_info_t *db = (board_info_t *) ndev->priv;
1350
9480e307 1351 if (ndev) {
a1365275
SH
1352
1353 if (netif_running(ndev)) {
1354 dm9000_reset(db);
1355 dm9000_init_dm9000(ndev);
1356
1357 netif_device_attach(ndev);
1358 }
321f69a4
BD
1359
1360 db->in_suspend = 0;
a1365275
SH
1361 }
1362 return 0;
1363}
1364
1365static int
3ae5eaec 1366dm9000_drv_remove(struct platform_device *pdev)
a1365275 1367{
3ae5eaec 1368 struct net_device *ndev = platform_get_drvdata(pdev);
a1365275 1369
3ae5eaec 1370 platform_set_drvdata(pdev, NULL);
a1365275
SH
1371
1372 unregister_netdev(ndev);
1373 dm9000_release_board(pdev, (board_info_t *) ndev->priv);
9fd9f9b6 1374 free_netdev(ndev); /* free device structure */
a1365275 1375
a76836f9 1376 dev_dbg(&pdev->dev, "released and freed device\n");
a1365275
SH
1377 return 0;
1378}
1379
3ae5eaec 1380static struct platform_driver dm9000_driver = {
5d22a312
BD
1381 .driver = {
1382 .name = "dm9000",
1383 .owner = THIS_MODULE,
1384 },
a1365275
SH
1385 .probe = dm9000_probe,
1386 .remove = dm9000_drv_remove,
1387 .suspend = dm9000_drv_suspend,
1388 .resume = dm9000_drv_resume,
1389};
1390
1391static int __init
1392dm9000_init(void)
1393{
7da99859 1394 printk(KERN_INFO "%s Ethernet Driver, V%s\n", CARDNAME, DRV_VERSION);
2ae2d77c 1395
3ae5eaec 1396 return platform_driver_register(&dm9000_driver); /* search board and register */
a1365275
SH
1397}
1398
1399static void __exit
1400dm9000_cleanup(void)
1401{
3ae5eaec 1402 platform_driver_unregister(&dm9000_driver);
a1365275
SH
1403}
1404
1405module_init(dm9000_init);
1406module_exit(dm9000_cleanup);
1407
1408MODULE_AUTHOR("Sascha Hauer, Ben Dooks");
1409MODULE_DESCRIPTION("Davicom DM9000 network driver");
1410MODULE_LICENSE("GPL");