blob: ec9730aee1e3005409a247ea20c0a0ba350ec3cf [file] [log] [blame]
Sascha Hauera1365272005-05-05 15:14:15 -07001/*
2 * dm9000.c: Version 1.2 03/18/2003
3 *
4 * A Davicom DM9000 ISA NIC fast Ethernet driver for Linux.
5 * Copyright (C) 1997 Sten Wang
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * (C)Copyright 1997-1998 DAVICOM Semiconductor,Inc. All Rights Reserved.
18 *
19 * V0.11 06/20/2001 REG_0A bit3=1, default enable BP with DA match
20 * 06/22/2001 Support DM9801 progrmming
21 * E3: R25 = ((R24 + NF) & 0x00ff) | 0xf000
22 * E4: R25 = ((R24 + NF) & 0x00ff) | 0xc200
23 * R17 = (R17 & 0xfff0) | NF + 3
24 * E5: R25 = ((R24 + NF - 3) & 0x00ff) | 0xc200
25 * R17 = (R17 & 0xfff0) | NF
26 *
27 * v1.00 modify by simon 2001.9.5
28 * change for kernel 2.4.x
29 *
30 * v1.1 11/09/2001 fix force mode bug
31 *
32 * v1.2 03/18/2003 Weilun Huang <weilun_huang@davicom.com.tw>:
33 * Fixed phy reset.
34 * Added tx/rx 32 bit mode.
35 * Cleaned up for kernel merge.
36 *
37 * 03/03/2004 Sascha Hauer <s.hauer@pengutronix.de>
38 * Port to 2.6 kernel
39 *
40 * 24-Sep-2004 Ben Dooks <ben@simtec.co.uk>
41 * Cleanup of code to remove ifdefs
42 * Allowed platform device data to influence access width
43 * Reformatting areas of code
44 *
45 * 17-Mar-2005 Sascha Hauer <s.hauer@pengutronix.de>
46 * * removed 2.4 style module parameters
47 * * removed removed unused stat counter and fixed
48 * net_device_stats
49 * * introduced tx_timeout function
50 * * reworked locking
Ben Dooks9ef9ac52005-07-23 17:25:18 +010051 *
52 * 01-Jul-2005 Ben Dooks <ben@simtec.co.uk>
53 * * fixed spinlock call without pointer
54 * * ensure spinlock is initialised
Sascha Hauera1365272005-05-05 15:14:15 -070055 */
56
57#include <linux/module.h>
58#include <linux/ioport.h>
59#include <linux/netdevice.h>
60#include <linux/etherdevice.h>
61#include <linux/init.h>
62#include <linux/skbuff.h>
Sascha Hauera1365272005-05-05 15:14:15 -070063#include <linux/spinlock.h>
64#include <linux/crc32.h>
65#include <linux/mii.h>
66#include <linux/dm9000.h>
67#include <linux/delay.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010068#include <linux/platform_device.h>
Daniel Mack4e4fc052008-01-23 14:54:50 +010069#include <linux/irq.h>
Sascha Hauera1365272005-05-05 15:14:15 -070070
71#include <asm/delay.h>
72#include <asm/irq.h>
73#include <asm/io.h>
74
75#include "dm9000.h"
76
77/* Board/System/Debug information/definition ---------------- */
78
79#define DM9000_PHY 0x40 /* PHY address 0x01 */
80
Sascha Hauera1365272005-05-05 15:14:15 -070081#define CARDNAME "dm9000"
82#define PFX CARDNAME ": "
83
84#define DM9000_TIMER_WUT jiffies+(HZ*2) /* timer wakeup time : 2 second */
85
Alex Landauf40d24d2007-07-12 12:11:48 +080086#ifdef CONFIG_BLACKFIN
87#define readsb insb
88#define readsw insw
89#define readsl insl
90#define writesb outsb
91#define writesw outsw
92#define writesl outsl
Ben Dooks1a5f1c42008-02-05 00:02:04 +000093#define DEFAULT_TRIGGER IRQF_TRIGGER_HIGH
Alex Landauf40d24d2007-07-12 12:11:48 +080094#else
Ben Dooks1a5f1c42008-02-05 00:02:04 +000095#define DEFAULT_TRIGGER (0)
Alex Landauf40d24d2007-07-12 12:11:48 +080096#endif
97
Sascha Hauera1365272005-05-05 15:14:15 -070098/*
99 * Transmit timeout, default 5 seconds.
100 */
101static int watchdog = 5000;
102module_param(watchdog, int, 0400);
103MODULE_PARM_DESC(watchdog, "transmit timeout in milliseconds");
104
105/* Structure/enum declaration ------------------------------- */
106typedef struct board_info {
107
108 void __iomem *io_addr; /* Register I/O base address */
109 void __iomem *io_data; /* Data I/O address */
110 u16 irq; /* IRQ */
111
112 u16 tx_pkt_cnt;
113 u16 queue_pkt_len;
114 u16 queue_start_addr;
115 u16 dbug_cnt;
116 u8 io_mode; /* 0:word, 2:byte */
117 u8 phy_addr;
Ben Dooks33ba5092008-02-05 00:02:01 +0000118 unsigned int flags;
Sascha Hauera1365272005-05-05 15:14:15 -0700119
Ben Dooks5b2b4ff2008-02-05 00:02:03 +0000120 int debug_level;
121
Sascha Hauera1365272005-05-05 15:14:15 -0700122 void (*inblk)(void __iomem *port, void *data, int length);
123 void (*outblk)(void __iomem *port, void *data, int length);
124 void (*dumpblk)(void __iomem *port, int length);
125
Ben Dooksa76836f2008-02-05 00:02:02 +0000126 struct device *dev; /* parent device */
127
Sascha Hauera1365272005-05-05 15:14:15 -0700128 struct resource *addr_res; /* resources found */
129 struct resource *data_res;
130 struct resource *addr_req; /* resources requested */
131 struct resource *data_req;
132 struct resource *irq_res;
133
134 struct timer_list timer;
Sascha Hauera1365272005-05-05 15:14:15 -0700135 unsigned char srom[128];
136 spinlock_t lock;
137
138 struct mii_if_info mii;
139 u32 msg_enable;
140} board_info_t;
141
Ben Dooks5b2b4ff2008-02-05 00:02:03 +0000142/* debug code */
143
144#define dm9000_dbg(db, lev, msg...) do { \
145 if ((lev) < CONFIG_DM9000_DEBUGLEVEL && \
146 (lev) < db->debug_level) { \
147 dev_dbg(db->dev, msg); \
148 } \
149} while (0)
150
Sascha Hauera1365272005-05-05 15:14:15 -0700151/* function declaration ------------------------------------- */
Russell King3ae5eae2005-11-09 22:32:44 +0000152static int dm9000_probe(struct platform_device *);
Sascha Hauera1365272005-05-05 15:14:15 -0700153static int dm9000_open(struct net_device *);
154static int dm9000_start_xmit(struct sk_buff *, struct net_device *);
155static int dm9000_stop(struct net_device *);
Sascha Hauera1365272005-05-05 15:14:15 -0700156
157
158static void dm9000_timer(unsigned long);
159static void dm9000_init_dm9000(struct net_device *);
160
David Howells7d12e782006-10-05 14:55:46 +0100161static irqreturn_t dm9000_interrupt(int, void *);
Sascha Hauera1365272005-05-05 15:14:15 -0700162
163static int dm9000_phy_read(struct net_device *dev, int phyaddr_unsused, int reg);
164static void dm9000_phy_write(struct net_device *dev, int phyaddr_unused, int reg,
165 int value);
166static u16 read_srom_word(board_info_t *, int);
167static void dm9000_rx(struct net_device *);
168static void dm9000_hash_table(struct net_device *);
169
170//#define DM9000_PROGRAM_EEPROM
171#ifdef DM9000_PROGRAM_EEPROM
172static void program_eeprom(board_info_t * db);
173#endif
174/* DM9000 network board routine ---------------------------- */
175
176static void
177dm9000_reset(board_info_t * db)
178{
Ben Dooksa76836f2008-02-05 00:02:02 +0000179 dev_dbg(db->dev, "resetting device\n");
180
Sascha Hauera1365272005-05-05 15:14:15 -0700181 /* RESET device */
182 writeb(DM9000_NCR, db->io_addr);
183 udelay(200);
184 writeb(NCR_RST, db->io_data);
185 udelay(200);
186}
187
188/*
189 * Read a byte from I/O port
190 */
191static u8
192ior(board_info_t * db, int reg)
193{
194 writeb(reg, db->io_addr);
195 return readb(db->io_data);
196}
197
198/*
199 * Write a byte to I/O port
200 */
201
202static void
203iow(board_info_t * db, int reg, int value)
204{
205 writeb(reg, db->io_addr);
206 writeb(value, db->io_data);
207}
208
209/* routines for sending block to chip */
210
211static void dm9000_outblk_8bit(void __iomem *reg, void *data, int count)
212{
213 writesb(reg, data, count);
214}
215
216static void dm9000_outblk_16bit(void __iomem *reg, void *data, int count)
217{
218 writesw(reg, data, (count+1) >> 1);
219}
220
221static void dm9000_outblk_32bit(void __iomem *reg, void *data, int count)
222{
223 writesl(reg, data, (count+3) >> 2);
224}
225
226/* input block from chip to memory */
227
228static void dm9000_inblk_8bit(void __iomem *reg, void *data, int count)
229{
Sascha Hauer5f6b5512005-06-20 15:32:51 -0700230 readsb(reg, data, count);
Sascha Hauera1365272005-05-05 15:14:15 -0700231}
232
233
234static void dm9000_inblk_16bit(void __iomem *reg, void *data, int count)
235{
236 readsw(reg, data, (count+1) >> 1);
237}
238
239static void dm9000_inblk_32bit(void __iomem *reg, void *data, int count)
240{
241 readsl(reg, data, (count+3) >> 2);
242}
243
244/* dump block from chip to null */
245
246static void dm9000_dumpblk_8bit(void __iomem *reg, int count)
247{
248 int i;
249 int tmp;
250
251 for (i = 0; i < count; i++)
252 tmp = readb(reg);
253}
254
255static void dm9000_dumpblk_16bit(void __iomem *reg, int count)
256{
257 int i;
258 int tmp;
259
260 count = (count + 1) >> 1;
261
262 for (i = 0; i < count; i++)
263 tmp = readw(reg);
264}
265
266static void dm9000_dumpblk_32bit(void __iomem *reg, int count)
267{
268 int i;
269 int tmp;
270
271 count = (count + 3) >> 2;
272
273 for (i = 0; i < count; i++)
274 tmp = readl(reg);
275}
276
277/* dm9000_set_io
278 *
279 * select the specified set of io routines to use with the
280 * device
281 */
282
283static void dm9000_set_io(struct board_info *db, int byte_width)
284{
285 /* use the size of the data resource to work out what IO
286 * routines we want to use
287 */
288
289 switch (byte_width) {
290 case 1:
291 db->dumpblk = dm9000_dumpblk_8bit;
292 db->outblk = dm9000_outblk_8bit;
293 db->inblk = dm9000_inblk_8bit;
294 break;
295
Sascha Hauera1365272005-05-05 15:14:15 -0700296
297 case 3:
Ben Dooksa76836f2008-02-05 00:02:02 +0000298 dev_dbg(db->dev, ": 3 byte IO, falling back to 16bit\n");
299 case 2:
Sascha Hauera1365272005-05-05 15:14:15 -0700300 db->dumpblk = dm9000_dumpblk_16bit;
301 db->outblk = dm9000_outblk_16bit;
302 db->inblk = dm9000_inblk_16bit;
303 break;
304
305 case 4:
306 default:
307 db->dumpblk = dm9000_dumpblk_32bit;
308 db->outblk = dm9000_outblk_32bit;
309 db->inblk = dm9000_inblk_32bit;
310 break;
311 }
312}
313
314
315/* Our watchdog timed out. Called by the networking layer */
316static void dm9000_timeout(struct net_device *dev)
317{
318 board_info_t *db = (board_info_t *) dev->priv;
319 u8 reg_save;
320 unsigned long flags;
321
322 /* Save previous register address */
323 reg_save = readb(db->io_addr);
Ben Dooks9ef9ac52005-07-23 17:25:18 +0100324 spin_lock_irqsave(&db->lock,flags);
Sascha Hauera1365272005-05-05 15:14:15 -0700325
326 netif_stop_queue(dev);
327 dm9000_reset(db);
328 dm9000_init_dm9000(dev);
329 /* We can accept TX packets again */
330 dev->trans_start = jiffies;
331 netif_wake_queue(dev);
332
333 /* Restore previous register address */
334 writeb(reg_save, db->io_addr);
Ben Dooks9ef9ac52005-07-23 17:25:18 +0100335 spin_unlock_irqrestore(&db->lock,flags);
Sascha Hauera1365272005-05-05 15:14:15 -0700336}
337
Kevin Hao2fd0e332006-08-14 23:00:15 -0700338#ifdef CONFIG_NET_POLL_CONTROLLER
339/*
340 *Used by netconsole
341 */
342static void dm9000_poll_controller(struct net_device *dev)
343{
344 disable_irq(dev->irq);
Al Viro28431142006-10-08 15:00:12 +0100345 dm9000_interrupt(dev->irq,dev);
Kevin Hao2fd0e332006-08-14 23:00:15 -0700346 enable_irq(dev->irq);
347}
348#endif
Sascha Hauera1365272005-05-05 15:14:15 -0700349
350/* dm9000_release_board
351 *
352 * release a board, and any mapped resources
353 */
354
355static void
356dm9000_release_board(struct platform_device *pdev, struct board_info *db)
357{
358 if (db->data_res == NULL) {
359 if (db->addr_res != NULL)
360 release_mem_region((unsigned long)db->io_addr, 4);
361 return;
362 }
363
364 /* unmap our resources */
365
366 iounmap(db->io_addr);
367 iounmap(db->io_data);
368
369 /* release the resources */
370
371 if (db->data_req != NULL) {
372 release_resource(db->data_req);
373 kfree(db->data_req);
374 }
375
Dirk Opfer51985482006-09-06 19:53:32 +0200376 if (db->addr_req != NULL) {
377 release_resource(db->addr_req);
Sascha Hauera1365272005-05-05 15:14:15 -0700378 kfree(db->addr_req);
379 }
380}
381
382#define res_size(_r) (((_r)->end - (_r)->start) + 1)
383
384/*
385 * Search DM9000 board, allocate space and register it
386 */
387static int
Russell King3ae5eae2005-11-09 22:32:44 +0000388dm9000_probe(struct platform_device *pdev)
Sascha Hauera1365272005-05-05 15:14:15 -0700389{
Sascha Hauera1365272005-05-05 15:14:15 -0700390 struct dm9000_plat_data *pdata = pdev->dev.platform_data;
391 struct board_info *db; /* Point a board information structure */
392 struct net_device *ndev;
393 unsigned long base;
394 int ret = 0;
395 int iosize;
396 int i;
397 u32 id_val;
398
Sascha Hauera1365272005-05-05 15:14:15 -0700399 /* Init network device */
400 ndev = alloc_etherdev(sizeof (struct board_info));
401 if (!ndev) {
Ben Dooksa76836f2008-02-05 00:02:02 +0000402 dev_err(&pdev->dev, "could not allocate device.\n");
Sascha Hauera1365272005-05-05 15:14:15 -0700403 return -ENOMEM;
404 }
405
Russell King3ae5eae2005-11-09 22:32:44 +0000406 SET_NETDEV_DEV(ndev, &pdev->dev);
Sascha Hauera1365272005-05-05 15:14:15 -0700407
Ben Dooksa76836f2008-02-05 00:02:02 +0000408 dev_dbg(&pdev->dev, "dm9000_probe()");
Sascha Hauera1365272005-05-05 15:14:15 -0700409
410 /* setup board info structure */
411 db = (struct board_info *) ndev->priv;
412 memset(db, 0, sizeof (*db));
413
Ben Dooksa76836f2008-02-05 00:02:02 +0000414 db->dev = &pdev->dev;
415
Ben Dooks9ef9ac52005-07-23 17:25:18 +0100416 spin_lock_init(&db->lock);
417
Sascha Hauera1365272005-05-05 15:14:15 -0700418 if (pdev->num_resources < 2) {
419 ret = -ENODEV;
420 goto out;
Ben Dooksb4ed03f2006-06-13 23:47:19 +0100421 } else if (pdev->num_resources == 2) {
Sascha Hauera1365272005-05-05 15:14:15 -0700422 base = pdev->resource[0].start;
423
424 if (!request_mem_region(base, 4, ndev->name)) {
425 ret = -EBUSY;
426 goto out;
427 }
428
429 ndev->base_addr = base;
430 ndev->irq = pdev->resource[1].start;
Ben Dooksb4ed03f2006-06-13 23:47:19 +0100431 db->io_addr = (void __iomem *)base;
432 db->io_data = (void __iomem *)(base + 4);
Sascha Hauera1365272005-05-05 15:14:15 -0700433
Alex Landauf40d24d2007-07-12 12:11:48 +0800434 /* ensure at least we have a default set of IO routines */
435 dm9000_set_io(db, 2);
436
Ben Dooksb4ed03f2006-06-13 23:47:19 +0100437 } else {
Sascha Hauera1365272005-05-05 15:14:15 -0700438 db->addr_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
439 db->data_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
440 db->irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
441
Ben Dooksb4ed03f2006-06-13 23:47:19 +0100442 if (db->addr_res == NULL || db->data_res == NULL ||
443 db->irq_res == NULL) {
Ben Dooksa76836f2008-02-05 00:02:02 +0000444 dev_err(db->dev, "insufficient resources\n");
Sascha Hauera1365272005-05-05 15:14:15 -0700445 ret = -ENOENT;
446 goto out;
447 }
448
449 i = res_size(db->addr_res);
450 db->addr_req = request_mem_region(db->addr_res->start, i,
451 pdev->name);
452
453 if (db->addr_req == NULL) {
Ben Dooksa76836f2008-02-05 00:02:02 +0000454 dev_err(db->dev, "cannot claim address reg area\n");
Sascha Hauera1365272005-05-05 15:14:15 -0700455 ret = -EIO;
456 goto out;
457 }
458
459 db->io_addr = ioremap(db->addr_res->start, i);
460
461 if (db->io_addr == NULL) {
Ben Dooksa76836f2008-02-05 00:02:02 +0000462 dev_err(db->dev, "failed to ioremap address reg\n");
Sascha Hauera1365272005-05-05 15:14:15 -0700463 ret = -EINVAL;
464 goto out;
465 }
466
467 iosize = res_size(db->data_res);
468 db->data_req = request_mem_region(db->data_res->start, iosize,
469 pdev->name);
470
471 if (db->data_req == NULL) {
Ben Dooksa76836f2008-02-05 00:02:02 +0000472 dev_err(db->dev, "cannot claim data reg area\n");
Sascha Hauera1365272005-05-05 15:14:15 -0700473 ret = -EIO;
474 goto out;
475 }
476
477 db->io_data = ioremap(db->data_res->start, iosize);
478
479 if (db->io_data == NULL) {
Ben Dooksa76836f2008-02-05 00:02:02 +0000480 dev_err(db->dev,"failed to ioremap data reg\n");
Sascha Hauera1365272005-05-05 15:14:15 -0700481 ret = -EINVAL;
482 goto out;
483 }
484
485 /* fill in parameters for net-dev structure */
486
487 ndev->base_addr = (unsigned long)db->io_addr;
488 ndev->irq = db->irq_res->start;
489
490 /* ensure at least we have a default set of IO routines */
491 dm9000_set_io(db, iosize);
Sascha Hauera1365272005-05-05 15:14:15 -0700492 }
493
494 /* check to see if anything is being over-ridden */
495 if (pdata != NULL) {
496 /* check to see if the driver wants to over-ride the
497 * default IO width */
498
499 if (pdata->flags & DM9000_PLATF_8BITONLY)
500 dm9000_set_io(db, 1);
501
502 if (pdata->flags & DM9000_PLATF_16BITONLY)
503 dm9000_set_io(db, 2);
504
505 if (pdata->flags & DM9000_PLATF_32BITONLY)
506 dm9000_set_io(db, 4);
507
508 /* check to see if there are any IO routine
509 * over-rides */
510
511 if (pdata->inblk != NULL)
512 db->inblk = pdata->inblk;
513
514 if (pdata->outblk != NULL)
515 db->outblk = pdata->outblk;
516
517 if (pdata->dumpblk != NULL)
518 db->dumpblk = pdata->dumpblk;
Ben Dooks33ba5092008-02-05 00:02:01 +0000519
520 db->flags = pdata->flags;
Sascha Hauera1365272005-05-05 15:14:15 -0700521 }
522
523 dm9000_reset(db);
524
525 /* try two times, DM9000 sometimes gets the first read wrong */
526 for (i = 0; i < 2; i++) {
527 id_val = ior(db, DM9000_VIDL);
528 id_val |= (u32)ior(db, DM9000_VIDH) << 8;
529 id_val |= (u32)ior(db, DM9000_PIDL) << 16;
530 id_val |= (u32)ior(db, DM9000_PIDH) << 24;
531
532 if (id_val == DM9000_ID)
533 break;
Ben Dooksa76836f2008-02-05 00:02:02 +0000534 dev_err(db->dev, "read wrong id 0x%08x\n", id_val);
Sascha Hauera1365272005-05-05 15:14:15 -0700535 }
536
537 if (id_val != DM9000_ID) {
Ben Dooksa76836f2008-02-05 00:02:02 +0000538 dev_err(db->dev, "wrong id: 0x%08x\n", id_val);
Mike Rapoport418d6f82007-10-18 09:23:11 +0200539 ret = -ENODEV;
540 goto out;
Sascha Hauera1365272005-05-05 15:14:15 -0700541 }
542
543 /* from this point we assume that we have found a DM9000 */
544
545 /* driver system function */
546 ether_setup(ndev);
547
548 ndev->open = &dm9000_open;
549 ndev->hard_start_xmit = &dm9000_start_xmit;
550 ndev->tx_timeout = &dm9000_timeout;
551 ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
552 ndev->stop = &dm9000_stop;
Sascha Hauera1365272005-05-05 15:14:15 -0700553 ndev->set_multicast_list = &dm9000_hash_table;
Kevin Hao2fd0e332006-08-14 23:00:15 -0700554#ifdef CONFIG_NET_POLL_CONTROLLER
555 ndev->poll_controller = &dm9000_poll_controller;
556#endif
Sascha Hauera1365272005-05-05 15:14:15 -0700557
558#ifdef DM9000_PROGRAM_EEPROM
559 program_eeprom(db);
560#endif
561 db->msg_enable = NETIF_MSG_LINK;
562 db->mii.phy_id_mask = 0x1f;
563 db->mii.reg_num_mask = 0x1f;
564 db->mii.force_media = 0;
565 db->mii.full_duplex = 0;
566 db->mii.dev = ndev;
567 db->mii.mdio_read = dm9000_phy_read;
568 db->mii.mdio_write = dm9000_phy_write;
569
570 /* Read SROM content */
571 for (i = 0; i < 64; i++)
572 ((u16 *) db->srom)[i] = read_srom_word(db, i);
573
574 /* Set Node Address */
575 for (i = 0; i < 6; i++)
576 ndev->dev_addr[i] = db->srom[i];
577
Ben Dooks5b55dda2006-06-13 23:50:15 +0100578 if (!is_valid_ether_addr(ndev->dev_addr)) {
579 /* try reading from mac */
580
581 for (i = 0; i < 6; i++)
582 ndev->dev_addr[i] = ior(db, i+DM9000_PAR);
583 }
584
Sascha Hauera1365272005-05-05 15:14:15 -0700585 if (!is_valid_ether_addr(ndev->dev_addr))
Ben Dooksa76836f2008-02-05 00:02:02 +0000586 dev_warn(db->dev, "%s: Invalid ethernet MAC address. Please "
587 "set using ifconfig\n", ndev->name);
Sascha Hauera1365272005-05-05 15:14:15 -0700588
Russell King3ae5eae2005-11-09 22:32:44 +0000589 platform_set_drvdata(pdev, ndev);
Sascha Hauera1365272005-05-05 15:14:15 -0700590 ret = register_netdev(ndev);
591
592 if (ret == 0) {
Joe Perches0795af52007-10-03 17:59:30 -0700593 DECLARE_MAC_BUF(mac);
594 printk("%s: dm9000 at %p,%p IRQ %d MAC: %s\n",
595 ndev->name, db->io_addr, db->io_data, ndev->irq,
596 print_mac(mac, ndev->dev_addr));
Sascha Hauera1365272005-05-05 15:14:15 -0700597 }
598 return 0;
599
Mike Rapoport418d6f82007-10-18 09:23:11 +0200600out:
Ben Dooksa76836f2008-02-05 00:02:02 +0000601 dev_err(db->dev, "not found (%d).\n", ret);
Sascha Hauera1365272005-05-05 15:14:15 -0700602
603 dm9000_release_board(pdev, db);
Ben Dooks9fd9f9b2007-05-07 11:13:25 +0000604 free_netdev(ndev);
Sascha Hauera1365272005-05-05 15:14:15 -0700605
606 return ret;
607}
608
609/*
610 * Open the interface.
611 * The interface is opened whenever "ifconfig" actives it.
612 */
613static int
614dm9000_open(struct net_device *dev)
615{
616 board_info_t *db = (board_info_t *) dev->priv;
Ben Dooks1a5f1c42008-02-05 00:02:04 +0000617 unsigned long irqflags = db->irq_res->flags & IRQF_TRIGGER_MASK;
Sascha Hauera1365272005-05-05 15:14:15 -0700618
Ben Dooksa76836f2008-02-05 00:02:02 +0000619 dev_dbg(db->dev, "entering %s\n", __func__);
Sascha Hauera1365272005-05-05 15:14:15 -0700620
Ben Dooks1a5f1c42008-02-05 00:02:04 +0000621 /* If there is no IRQ type specified, default to something that
622 * may work, and tell the user that this is a problem */
623
624 if (irqflags == IRQF_TRIGGER_NONE) {
625 dev_warn(db->dev, "WARNING: no IRQ resource flags set.\n");
626 irqflags = DEFAULT_TRIGGER;
627 }
628
629 irqflags |= IRQF_SHARED;
630
631 if (request_irq(dev->irq, &dm9000_interrupt, irqflags, dev->name, dev))
Sascha Hauera1365272005-05-05 15:14:15 -0700632 return -EAGAIN;
633
634 /* Initialize DM9000 board */
635 dm9000_reset(db);
636 dm9000_init_dm9000(dev);
637
638 /* Init driver variable */
639 db->dbug_cnt = 0;
640
641 /* set and active a timer process */
642 init_timer(&db->timer);
Ben Dooks9ef9ac52005-07-23 17:25:18 +0100643 db->timer.expires = DM9000_TIMER_WUT;
Sascha Hauera1365272005-05-05 15:14:15 -0700644 db->timer.data = (unsigned long) dev;
645 db->timer.function = &dm9000_timer;
646 add_timer(&db->timer);
647
648 mii_check_media(&db->mii, netif_msg_link(db), 1);
649 netif_start_queue(dev);
650
651 return 0;
652}
653
654/*
655 * Initilize dm9000 board
656 */
657static void
658dm9000_init_dm9000(struct net_device *dev)
659{
660 board_info_t *db = (board_info_t *) dev->priv;
661
Ben Dooks5b2b4ff2008-02-05 00:02:03 +0000662 dm9000_dbg(db, 1, "entering %s\n", __func__);
Sascha Hauera1365272005-05-05 15:14:15 -0700663
664 /* I/O mode */
665 db->io_mode = ior(db, DM9000_ISR) >> 6; /* ISR bit7:6 keeps I/O mode */
666
667 /* GPIO0 on pre-activate PHY */
668 iow(db, DM9000_GPR, 0); /* REG_1F bit0 activate phyxcer */
669 iow(db, DM9000_GPCR, GPCR_GEP_CNTL); /* Let GPIO0 output */
670 iow(db, DM9000_GPR, 0); /* Enable PHY */
671
Ben Dooks33ba5092008-02-05 00:02:01 +0000672 if (db->flags & DM9000_PLATF_EXT_PHY)
673 iow(db, DM9000_NCR, NCR_EXT_PHY);
674
Sascha Hauera1365272005-05-05 15:14:15 -0700675 /* Program operating register */
676 iow(db, DM9000_TCR, 0); /* TX Polling clear */
677 iow(db, DM9000_BPTR, 0x3f); /* Less 3Kb, 200us */
678 iow(db, DM9000_FCR, 0xff); /* Flow Control */
679 iow(db, DM9000_SMCR, 0); /* Special Mode */
680 /* clear TX status */
681 iow(db, DM9000_NSR, NSR_WAKEST | NSR_TX2END | NSR_TX1END);
682 iow(db, DM9000_ISR, ISR_CLR_STATUS); /* Clear interrupt status */
683
684 /* Set address filter table */
685 dm9000_hash_table(dev);
686
687 /* Activate DM9000 */
688 iow(db, DM9000_RCR, RCR_DIS_LONG | RCR_DIS_CRC | RCR_RXEN);
689 /* Enable TX/RX interrupt mask */
690 iow(db, DM9000_IMR, IMR_PAR | IMR_PTM | IMR_PRM);
691
692 /* Init Driver variable */
693 db->tx_pkt_cnt = 0;
694 db->queue_pkt_len = 0;
695 dev->trans_start = 0;
Sascha Hauera1365272005-05-05 15:14:15 -0700696}
697
698/*
699 * Hardware start transmission.
700 * Send a packet to media from the upper layer.
701 */
702static int
703dm9000_start_xmit(struct sk_buff *skb, struct net_device *dev)
704{
Florian Westphalc46ac942007-08-21 01:33:42 +0200705 unsigned long flags;
Sascha Hauera1365272005-05-05 15:14:15 -0700706 board_info_t *db = (board_info_t *) dev->priv;
707
Ben Dooks5b2b4ff2008-02-05 00:02:03 +0000708 dm9000_dbg(db, 3, "%s:\n", __func__);
Sascha Hauera1365272005-05-05 15:14:15 -0700709
710 if (db->tx_pkt_cnt > 1)
711 return 1;
712
Florian Westphalc46ac942007-08-21 01:33:42 +0200713 spin_lock_irqsave(&db->lock, flags);
Sascha Hauera1365272005-05-05 15:14:15 -0700714
715 /* Move data to DM9000 TX RAM */
716 writeb(DM9000_MWCMD, db->io_addr);
717
718 (db->outblk)(db->io_data, skb->data, skb->len);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700719 dev->stats.tx_bytes += skb->len;
Sascha Hauera1365272005-05-05 15:14:15 -0700720
Florian Westphalc46ac942007-08-21 01:33:42 +0200721 db->tx_pkt_cnt++;
Sascha Hauera1365272005-05-05 15:14:15 -0700722 /* TX control: First packet immediately send, second packet queue */
Florian Westphalc46ac942007-08-21 01:33:42 +0200723 if (db->tx_pkt_cnt == 1) {
Sascha Hauera1365272005-05-05 15:14:15 -0700724 /* Set TX length to DM9000 */
725 iow(db, DM9000_TXPLL, skb->len & 0xff);
726 iow(db, DM9000_TXPLH, (skb->len >> 8) & 0xff);
727
728 /* Issue TX polling command */
729 iow(db, DM9000_TCR, TCR_TXREQ); /* Cleared after TX complete */
730
731 dev->trans_start = jiffies; /* save the time stamp */
Sascha Hauera1365272005-05-05 15:14:15 -0700732 } else {
733 /* Second packet */
Sascha Hauera1365272005-05-05 15:14:15 -0700734 db->queue_pkt_len = skb->len;
Florian Westphalc46ac942007-08-21 01:33:42 +0200735 netif_stop_queue(dev);
Sascha Hauera1365272005-05-05 15:14:15 -0700736 }
737
Florian Westphalc46ac942007-08-21 01:33:42 +0200738 spin_unlock_irqrestore(&db->lock, flags);
739
Sascha Hauera1365272005-05-05 15:14:15 -0700740 /* free this SKB */
741 dev_kfree_skb(skb);
742
Sascha Hauera1365272005-05-05 15:14:15 -0700743 return 0;
744}
745
746static void
747dm9000_shutdown(struct net_device *dev)
748{
749 board_info_t *db = (board_info_t *) dev->priv;
750
751 /* RESET device */
752 dm9000_phy_write(dev, 0, MII_BMCR, BMCR_RESET); /* PHY RESET */
753 iow(db, DM9000_GPR, 0x01); /* Power-Down PHY */
754 iow(db, DM9000_IMR, IMR_PAR); /* Disable all interrupt */
755 iow(db, DM9000_RCR, 0x00); /* Disable RX */
756}
757
758/*
759 * Stop the interface.
760 * The interface is stopped when it is brought.
761 */
762static int
763dm9000_stop(struct net_device *ndev)
764{
765 board_info_t *db = (board_info_t *) ndev->priv;
766
Ben Dooks5b2b4ff2008-02-05 00:02:03 +0000767 dm9000_dbg(db, 1, "entering %s\n", __func__);
Sascha Hauera1365272005-05-05 15:14:15 -0700768
769 /* deleted timer */
770 del_timer(&db->timer);
771
772 netif_stop_queue(ndev);
773 netif_carrier_off(ndev);
774
775 /* free interrupt */
776 free_irq(ndev->irq, ndev);
777
778 dm9000_shutdown(ndev);
779
780 return 0;
781}
782
783/*
784 * DM9000 interrupt handler
785 * receive the packet to upper layer, free the transmitted packet
786 */
787
Ben Dooks5d22a312006-06-14 00:09:17 +0100788static void
Sascha Hauera1365272005-05-05 15:14:15 -0700789dm9000_tx_done(struct net_device *dev, board_info_t * db)
790{
791 int tx_status = ior(db, DM9000_NSR); /* Got TX status */
792
793 if (tx_status & (NSR_TX2END | NSR_TX1END)) {
794 /* One packet sent complete */
795 db->tx_pkt_cnt--;
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700796 dev->stats.tx_packets++;
Sascha Hauera1365272005-05-05 15:14:15 -0700797
798 /* Queue packet check & send */
799 if (db->tx_pkt_cnt > 0) {
800 iow(db, DM9000_TXPLL, db->queue_pkt_len & 0xff);
801 iow(db, DM9000_TXPLH, (db->queue_pkt_len >> 8) & 0xff);
802 iow(db, DM9000_TCR, TCR_TXREQ);
803 dev->trans_start = jiffies;
804 }
805 netif_wake_queue(dev);
806 }
807}
808
809static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +0100810dm9000_interrupt(int irq, void *dev_id)
Sascha Hauera1365272005-05-05 15:14:15 -0700811{
812 struct net_device *dev = dev_id;
Ben Dooks5b2b4ff2008-02-05 00:02:03 +0000813 board_info_t *db = (board_info_t *) dev->priv;
Sascha Hauera1365272005-05-05 15:14:15 -0700814 int int_status;
815 u8 reg_save;
816
Ben Dooks5b2b4ff2008-02-05 00:02:03 +0000817 dm9000_dbg(db, 3, "entering %s\n", __func__);
Sascha Hauera1365272005-05-05 15:14:15 -0700818
819 /* A real interrupt coming */
Ben Dooks5b2b4ff2008-02-05 00:02:03 +0000820
Sascha Hauera1365272005-05-05 15:14:15 -0700821 spin_lock(&db->lock);
822
823 /* Save previous register address */
824 reg_save = readb(db->io_addr);
825
826 /* Disable all interrupts */
827 iow(db, DM9000_IMR, IMR_PAR);
828
829 /* Got DM9000 interrupt status */
830 int_status = ior(db, DM9000_ISR); /* Got ISR */
831 iow(db, DM9000_ISR, int_status); /* Clear ISR status */
832
833 /* Received the coming packet */
834 if (int_status & ISR_PRS)
835 dm9000_rx(dev);
836
837 /* Trnasmit Interrupt check */
838 if (int_status & ISR_PTS)
839 dm9000_tx_done(dev, db);
840
841 /* Re-enable interrupt mask */
842 iow(db, DM9000_IMR, IMR_PAR | IMR_PTM | IMR_PRM);
843
844 /* Restore previous register address */
845 writeb(reg_save, db->io_addr);
846
847 spin_unlock(&db->lock);
848
849 return IRQ_HANDLED;
850}
851
852/*
Sascha Hauera1365272005-05-05 15:14:15 -0700853 * A periodic timer routine
854 * Dynamic media sense, allocated Rx buffer...
855 */
856static void
857dm9000_timer(unsigned long data)
858{
859 struct net_device *dev = (struct net_device *) data;
860 board_info_t *db = (board_info_t *) dev->priv;
Sascha Hauera1365272005-05-05 15:14:15 -0700861
Ben Dooks5b2b4ff2008-02-05 00:02:03 +0000862 dm9000_dbg(db, 3, "entering %s\n", __func__);
Sascha Hauera1365272005-05-05 15:14:15 -0700863
Sascha Hauera1365272005-05-05 15:14:15 -0700864 mii_check_media(&db->mii, netif_msg_link(db), 0);
865
Sascha Hauera1365272005-05-05 15:14:15 -0700866 /* Set timer again */
867 db->timer.expires = DM9000_TIMER_WUT;
868 add_timer(&db->timer);
869}
870
871struct dm9000_rxhdr {
Ben Dooks93116572008-02-05 00:02:00 +0000872 u8 RxPktReady;
873 u8 RxStatus;
Sascha Hauera1365272005-05-05 15:14:15 -0700874 u16 RxLen;
875} __attribute__((__packed__));
876
877/*
878 * Received a packet and pass to upper layer
879 */
880static void
881dm9000_rx(struct net_device *dev)
882{
883 board_info_t *db = (board_info_t *) dev->priv;
884 struct dm9000_rxhdr rxhdr;
885 struct sk_buff *skb;
886 u8 rxbyte, *rdptr;
Richard Knutsson6478fac2007-05-01 18:43:27 +0200887 bool GoodPacket;
Sascha Hauera1365272005-05-05 15:14:15 -0700888 int RxLen;
889
890 /* Check packet ready or not */
891 do {
892 ior(db, DM9000_MRCMDX); /* Dummy read */
893
894 /* Get most updated data */
895 rxbyte = readb(db->io_data);
896
897 /* Status check: this byte must be 0 or 1 */
898 if (rxbyte > DM9000_PKT_RDY) {
Ben Dooksa76836f2008-02-05 00:02:02 +0000899 dev_warn(db->dev, "status check fail: %d\n", rxbyte);
Sascha Hauera1365272005-05-05 15:14:15 -0700900 iow(db, DM9000_RCR, 0x00); /* Stop Device */
901 iow(db, DM9000_ISR, IMR_PAR); /* Stop INT request */
902 return;
903 }
904
905 if (rxbyte != DM9000_PKT_RDY)
906 return;
907
908 /* A packet ready now & Get status/length */
Richard Knutsson6478fac2007-05-01 18:43:27 +0200909 GoodPacket = true;
Sascha Hauera1365272005-05-05 15:14:15 -0700910 writeb(DM9000_MRCMD, db->io_addr);
911
912 (db->inblk)(db->io_data, &rxhdr, sizeof(rxhdr));
913
Ben Dooks93116572008-02-05 00:02:00 +0000914 RxLen = le16_to_cpu(rxhdr.RxLen);
Sascha Hauera1365272005-05-05 15:14:15 -0700915
916 /* Packet Status check */
917 if (RxLen < 0x40) {
Richard Knutsson6478fac2007-05-01 18:43:27 +0200918 GoodPacket = false;
Ben Dooksa76836f2008-02-05 00:02:02 +0000919 dev_dbg(db->dev, "Bad Packet received (runt)\n");
Sascha Hauera1365272005-05-05 15:14:15 -0700920 }
921
922 if (RxLen > DM9000_PKT_MAX) {
Ben Dooksa76836f2008-02-05 00:02:02 +0000923 dev_dbg(db->dev, "RST: RX Len:%x\n", RxLen);
Sascha Hauera1365272005-05-05 15:14:15 -0700924 }
925
Ben Dooks93116572008-02-05 00:02:00 +0000926 if (rxhdr.RxStatus & 0xbf) {
Richard Knutsson6478fac2007-05-01 18:43:27 +0200927 GoodPacket = false;
Ben Dooks93116572008-02-05 00:02:00 +0000928 if (rxhdr.RxStatus & 0x01) {
Ben Dooksa76836f2008-02-05 00:02:02 +0000929 dev_dbg(db->dev, "fifo error\n");
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700930 dev->stats.rx_fifo_errors++;
Sascha Hauera1365272005-05-05 15:14:15 -0700931 }
Ben Dooks93116572008-02-05 00:02:00 +0000932 if (rxhdr.RxStatus & 0x02) {
Ben Dooksa76836f2008-02-05 00:02:02 +0000933 dev_dbg(db->dev, "crc error\n");
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700934 dev->stats.rx_crc_errors++;
Sascha Hauera1365272005-05-05 15:14:15 -0700935 }
Ben Dooks93116572008-02-05 00:02:00 +0000936 if (rxhdr.RxStatus & 0x80) {
Ben Dooksa76836f2008-02-05 00:02:02 +0000937 dev_dbg(db->dev, "length error\n");
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700938 dev->stats.rx_length_errors++;
Sascha Hauera1365272005-05-05 15:14:15 -0700939 }
940 }
941
942 /* Move data from DM9000 */
943 if (GoodPacket
944 && ((skb = dev_alloc_skb(RxLen + 4)) != NULL)) {
Sascha Hauera1365272005-05-05 15:14:15 -0700945 skb_reserve(skb, 2);
946 rdptr = (u8 *) skb_put(skb, RxLen - 4);
947
948 /* Read received packet from RX SRAM */
949
950 (db->inblk)(db->io_data, rdptr, RxLen);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700951 dev->stats.rx_bytes += RxLen;
Sascha Hauera1365272005-05-05 15:14:15 -0700952
953 /* Pass to upper layer */
954 skb->protocol = eth_type_trans(skb, dev);
955 netif_rx(skb);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700956 dev->stats.rx_packets++;
Sascha Hauera1365272005-05-05 15:14:15 -0700957
958 } else {
959 /* need to dump the packet's data */
960
961 (db->dumpblk)(db->io_data, RxLen);
962 }
963 } while (rxbyte == DM9000_PKT_RDY);
964}
965
966/*
967 * Read a word data from SROM
968 */
969static u16
970read_srom_word(board_info_t * db, int offset)
971{
972 iow(db, DM9000_EPAR, offset);
973 iow(db, DM9000_EPCR, EPCR_ERPRR);
974 mdelay(8); /* according to the datasheet 200us should be enough,
975 but it doesn't work */
976 iow(db, DM9000_EPCR, 0x0);
977 return (ior(db, DM9000_EPDRL) + (ior(db, DM9000_EPDRH) << 8));
978}
979
980#ifdef DM9000_PROGRAM_EEPROM
981/*
982 * Write a word data to SROM
983 */
984static void
985write_srom_word(board_info_t * db, int offset, u16 val)
986{
987 iow(db, DM9000_EPAR, offset);
988 iow(db, DM9000_EPDRH, ((val >> 8) & 0xff));
989 iow(db, DM9000_EPDRL, (val & 0xff));
990 iow(db, DM9000_EPCR, EPCR_WEP | EPCR_ERPRW);
991 mdelay(8); /* same shit */
992 iow(db, DM9000_EPCR, 0);
993}
994
995/*
996 * Only for development:
997 * Here we write static data to the eeprom in case
998 * we don't have valid content on a new board
999 */
1000static void
1001program_eeprom(board_info_t * db)
1002{
1003 u16 eeprom[] = { 0x0c00, 0x007f, 0x1300, /* MAC Address */
1004 0x0000, /* Autoload: accept nothing */
1005 0x0a46, 0x9000, /* Vendor / Product ID */
1006 0x0000, /* pin control */
1007 0x0000,
1008 }; /* Wake-up mode control */
1009 int i;
1010 for (i = 0; i < 8; i++)
1011 write_srom_word(db, i, eeprom[i]);
1012}
1013#endif
1014
1015
1016/*
1017 * Calculate the CRC valude of the Rx packet
1018 * flag = 1 : return the reverse CRC (for the received packet CRC)
1019 * 0 : return the normal CRC (for Hash Table index)
1020 */
1021
1022static unsigned long
1023cal_CRC(unsigned char *Data, unsigned int Len, u8 flag)
1024{
1025
1026 u32 crc = ether_crc_le(Len, Data);
1027
1028 if (flag)
1029 return ~crc;
1030
1031 return crc;
1032}
1033
1034/*
1035 * Set DM9000 multicast address
1036 */
1037static void
1038dm9000_hash_table(struct net_device *dev)
1039{
1040 board_info_t *db = (board_info_t *) dev->priv;
1041 struct dev_mc_list *mcptr = dev->mc_list;
1042 int mc_cnt = dev->mc_count;
1043 u32 hash_val;
1044 u16 i, oft, hash_table[4];
1045 unsigned long flags;
1046
Ben Dooks5b2b4ff2008-02-05 00:02:03 +00001047 dm9000_dbg(db, 1, "entering %s\n", __func__);
Sascha Hauera1365272005-05-05 15:14:15 -07001048
1049 spin_lock_irqsave(&db->lock,flags);
1050
1051 for (i = 0, oft = 0x10; i < 6; i++, oft++)
1052 iow(db, oft, dev->dev_addr[i]);
1053
1054 /* Clear Hash Table */
1055 for (i = 0; i < 4; i++)
1056 hash_table[i] = 0x0;
1057
1058 /* broadcast address */
1059 hash_table[3] = 0x8000;
1060
1061 /* the multicast address in Hash Table : 64 bits */
1062 for (i = 0; i < mc_cnt; i++, mcptr = mcptr->next) {
1063 hash_val = cal_CRC((char *) mcptr->dmi_addr, 6, 0) & 0x3f;
1064 hash_table[hash_val / 16] |= (u16) 1 << (hash_val % 16);
1065 }
1066
1067 /* Write the hash table to MAC MD table */
1068 for (i = 0, oft = 0x16; i < 4; i++) {
1069 iow(db, oft++, hash_table[i] & 0xff);
1070 iow(db, oft++, (hash_table[i] >> 8) & 0xff);
1071 }
1072
1073 spin_unlock_irqrestore(&db->lock,flags);
1074}
1075
1076
1077/*
1078 * Read a word from phyxcer
1079 */
1080static int
1081dm9000_phy_read(struct net_device *dev, int phy_reg_unused, int reg)
1082{
1083 board_info_t *db = (board_info_t *) dev->priv;
1084 unsigned long flags;
Ben Dooks9ef9ac52005-07-23 17:25:18 +01001085 unsigned int reg_save;
Sascha Hauera1365272005-05-05 15:14:15 -07001086 int ret;
1087
1088 spin_lock_irqsave(&db->lock,flags);
Ben Dooks9ef9ac52005-07-23 17:25:18 +01001089
1090 /* Save previous register address */
1091 reg_save = readb(db->io_addr);
1092
Sascha Hauera1365272005-05-05 15:14:15 -07001093 /* Fill the phyxcer register into REG_0C */
1094 iow(db, DM9000_EPAR, DM9000_PHY | reg);
1095
1096 iow(db, DM9000_EPCR, 0xc); /* Issue phyxcer read command */
1097 udelay(100); /* Wait read complete */
1098 iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer read command */
1099
1100 /* The read data keeps on REG_0D & REG_0E */
1101 ret = (ior(db, DM9000_EPDRH) << 8) | ior(db, DM9000_EPDRL);
1102
Ben Dooks9ef9ac52005-07-23 17:25:18 +01001103 /* restore the previous address */
1104 writeb(reg_save, db->io_addr);
1105
Sascha Hauera1365272005-05-05 15:14:15 -07001106 spin_unlock_irqrestore(&db->lock,flags);
1107
1108 return ret;
1109}
1110
1111/*
1112 * Write a word to phyxcer
1113 */
1114static void
1115dm9000_phy_write(struct net_device *dev, int phyaddr_unused, int reg, int value)
1116{
1117 board_info_t *db = (board_info_t *) dev->priv;
1118 unsigned long flags;
Ben Dooks9ef9ac52005-07-23 17:25:18 +01001119 unsigned long reg_save;
Sascha Hauera1365272005-05-05 15:14:15 -07001120
1121 spin_lock_irqsave(&db->lock,flags);
1122
Ben Dooks9ef9ac52005-07-23 17:25:18 +01001123 /* Save previous register address */
1124 reg_save = readb(db->io_addr);
1125
Sascha Hauera1365272005-05-05 15:14:15 -07001126 /* Fill the phyxcer register into REG_0C */
1127 iow(db, DM9000_EPAR, DM9000_PHY | reg);
1128
1129 /* Fill the written data into REG_0D & REG_0E */
1130 iow(db, DM9000_EPDRL, (value & 0xff));
1131 iow(db, DM9000_EPDRH, ((value >> 8) & 0xff));
1132
1133 iow(db, DM9000_EPCR, 0xa); /* Issue phyxcer write command */
1134 udelay(500); /* Wait write complete */
1135 iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer write command */
1136
Ben Dooks9ef9ac52005-07-23 17:25:18 +01001137 /* restore the previous address */
1138 writeb(reg_save, db->io_addr);
1139
Sascha Hauera1365272005-05-05 15:14:15 -07001140 spin_unlock_irqrestore(&db->lock,flags);
1141}
1142
1143static int
Russell King3ae5eae2005-11-09 22:32:44 +00001144dm9000_drv_suspend(struct platform_device *dev, pm_message_t state)
Sascha Hauera1365272005-05-05 15:14:15 -07001145{
Russell King3ae5eae2005-11-09 22:32:44 +00001146 struct net_device *ndev = platform_get_drvdata(dev);
Sascha Hauera1365272005-05-05 15:14:15 -07001147
Russell King9480e302005-10-28 09:52:56 -07001148 if (ndev) {
Sascha Hauera1365272005-05-05 15:14:15 -07001149 if (netif_running(ndev)) {
1150 netif_device_detach(ndev);
1151 dm9000_shutdown(ndev);
1152 }
1153 }
1154 return 0;
1155}
1156
1157static int
Russell King3ae5eae2005-11-09 22:32:44 +00001158dm9000_drv_resume(struct platform_device *dev)
Sascha Hauera1365272005-05-05 15:14:15 -07001159{
Russell King3ae5eae2005-11-09 22:32:44 +00001160 struct net_device *ndev = platform_get_drvdata(dev);
Sascha Hauera1365272005-05-05 15:14:15 -07001161 board_info_t *db = (board_info_t *) ndev->priv;
1162
Russell King9480e302005-10-28 09:52:56 -07001163 if (ndev) {
Sascha Hauera1365272005-05-05 15:14:15 -07001164
1165 if (netif_running(ndev)) {
1166 dm9000_reset(db);
1167 dm9000_init_dm9000(ndev);
1168
1169 netif_device_attach(ndev);
1170 }
1171 }
1172 return 0;
1173}
1174
1175static int
Russell King3ae5eae2005-11-09 22:32:44 +00001176dm9000_drv_remove(struct platform_device *pdev)
Sascha Hauera1365272005-05-05 15:14:15 -07001177{
Russell King3ae5eae2005-11-09 22:32:44 +00001178 struct net_device *ndev = platform_get_drvdata(pdev);
Sascha Hauera1365272005-05-05 15:14:15 -07001179
Russell King3ae5eae2005-11-09 22:32:44 +00001180 platform_set_drvdata(pdev, NULL);
Sascha Hauera1365272005-05-05 15:14:15 -07001181
1182 unregister_netdev(ndev);
1183 dm9000_release_board(pdev, (board_info_t *) ndev->priv);
Ben Dooks9fd9f9b2007-05-07 11:13:25 +00001184 free_netdev(ndev); /* free device structure */
Sascha Hauera1365272005-05-05 15:14:15 -07001185
Ben Dooksa76836f2008-02-05 00:02:02 +00001186 dev_dbg(&pdev->dev, "released and freed device\n");
Sascha Hauera1365272005-05-05 15:14:15 -07001187 return 0;
1188}
1189
Russell King3ae5eae2005-11-09 22:32:44 +00001190static struct platform_driver dm9000_driver = {
Ben Dooks5d22a312006-06-14 00:09:17 +01001191 .driver = {
1192 .name = "dm9000",
1193 .owner = THIS_MODULE,
1194 },
Sascha Hauera1365272005-05-05 15:14:15 -07001195 .probe = dm9000_probe,
1196 .remove = dm9000_drv_remove,
1197 .suspend = dm9000_drv_suspend,
1198 .resume = dm9000_drv_resume,
1199};
1200
1201static int __init
1202dm9000_init(void)
1203{
Ben Dooks2ae2d772005-07-23 17:29:38 +01001204 printk(KERN_INFO "%s Ethernet Driver\n", CARDNAME);
1205
Russell King3ae5eae2005-11-09 22:32:44 +00001206 return platform_driver_register(&dm9000_driver); /* search board and register */
Sascha Hauera1365272005-05-05 15:14:15 -07001207}
1208
1209static void __exit
1210dm9000_cleanup(void)
1211{
Russell King3ae5eae2005-11-09 22:32:44 +00001212 platform_driver_unregister(&dm9000_driver);
Sascha Hauera1365272005-05-05 15:14:15 -07001213}
1214
1215module_init(dm9000_init);
1216module_exit(dm9000_cleanup);
1217
1218MODULE_AUTHOR("Sascha Hauer, Ben Dooks");
1219MODULE_DESCRIPTION("Davicom DM9000 network driver");
1220MODULE_LICENSE("GPL");