blob: 863e9c459e65314cb942557ddb61ac21f753fa94 [file] [log] [blame]
Sascha Hauera1365272005-05-05 15:14:15 -07001/*
Ben Dooks41c340f2008-02-05 00:02:15 +00002 * Davicom DM9000 Fast Ethernet driver for Linux.
Sascha Hauera1365272005-05-05 15:14:15 -07003 * 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 *
Ben Dooks41c340f2008-02-05 00:02:15 +000015 * (C) Copyright 1997-1998 DAVICOM Semiconductor,Inc. All Rights Reserved.
Sascha Hauera1365272005-05-05 15:14:15 -070016 *
Ben Dooks41c340f2008-02-05 00:02:15 +000017 * Additional updates, Copyright:
18 * Ben Dooks <ben@simtec.co.uk>
19 * Sascha Hauer <s.hauer@pengutronix.de>
Sascha Hauera1365272005-05-05 15:14:15 -070020 */
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>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000027#include <linux/interrupt.h>
Sascha Hauera1365272005-05-05 15:14:15 -070028#include <linux/skbuff.h>
Sascha Hauera1365272005-05-05 15:14:15 -070029#include <linux/spinlock.h>
30#include <linux/crc32.h>
31#include <linux/mii.h>
Ben Dooks7da99852008-02-05 00:02:06 +000032#include <linux/ethtool.h>
Sascha Hauera1365272005-05-05 15:14:15 -070033#include <linux/dm9000.h>
34#include <linux/delay.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010035#include <linux/platform_device.h>
Daniel Mack4e4fc052008-01-23 14:54:50 +010036#include <linux/irq.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
Sascha Hauera1365272005-05-05 15:14:15 -070038
39#include <asm/delay.h>
40#include <asm/irq.h>
41#include <asm/io.h>
42
43#include "dm9000.h"
44
45/* Board/System/Debug information/definition ---------------- */
46
47#define DM9000_PHY 0x40 /* PHY address 0x01 */
48
Ben Dooks59eae1f2008-06-24 22:16:01 +010049#define CARDNAME "dm9000"
50#define DRV_VERSION "1.31"
Sascha Hauera1365272005-05-05 15:14:15 -070051
Sascha Hauera1365272005-05-05 15:14:15 -070052/*
53 * Transmit timeout, default 5 seconds.
54 */
55static int watchdog = 5000;
56module_param(watchdog, int, 0400);
57MODULE_PARM_DESC(watchdog, "transmit timeout in milliseconds");
58
Ben Dooks9a2f0372008-02-05 00:02:10 +000059/* DM9000 register address locking.
60 *
61 * The DM9000 uses an address register to control where data written
62 * to the data register goes. This means that the address register
63 * must be preserved over interrupts or similar calls.
64 *
65 * During interrupt and other critical calls, a spinlock is used to
66 * protect the system, but the calls themselves save the address
67 * in the address register in case they are interrupting another
68 * access to the device.
69 *
70 * For general accesses a lock is provided so that calls which are
71 * allowed to sleep are serialised so that the address register does
72 * not need to be saved. This lock also serves to serialise access
73 * to the EEPROM and PHY access registers which are shared between
74 * these two devices.
75 */
76
Ben Dooks6d406b32008-06-24 22:15:59 +010077/* The driver supports the original DM9000E, and now the two newer
78 * devices, DM9000A and DM9000B.
79 */
80
81enum dm9000_type {
82 TYPE_DM9000E, /* original DM9000 */
83 TYPE_DM9000A,
84 TYPE_DM9000B
85};
86
Sascha Hauera1365272005-05-05 15:14:15 -070087/* Structure/enum declaration ------------------------------- */
88typedef struct board_info {
89
Ben Dooks59eae1f2008-06-24 22:16:01 +010090 void __iomem *io_addr; /* Register I/O base address */
91 void __iomem *io_data; /* Data I/O address */
92 u16 irq; /* IRQ */
Sascha Hauera1365272005-05-05 15:14:15 -070093
Ben Dooks59eae1f2008-06-24 22:16:01 +010094 u16 tx_pkt_cnt;
95 u16 queue_pkt_len;
96 u16 queue_start_addr;
Yeasah Pell5dcc60b2009-07-06 18:12:33 -070097 u16 queue_ip_summed;
Ben Dooks59eae1f2008-06-24 22:16:01 +010098 u16 dbug_cnt;
99 u8 io_mode; /* 0:word, 2:byte */
100 u8 phy_addr;
101 u8 imr_all;
102
103 unsigned int flags;
104 unsigned int in_suspend :1;
Ben Dooksc029f442009-11-10 07:22:24 +0000105 unsigned int wake_supported :1;
Ben Dooks59eae1f2008-06-24 22:16:01 +0100106 int debug_level;
Sascha Hauera1365272005-05-05 15:14:15 -0700107
Ben Dooks6d406b32008-06-24 22:15:59 +0100108 enum dm9000_type type;
Ben Dooks5b2b4ff2008-02-05 00:02:03 +0000109
Sascha Hauera1365272005-05-05 15:14:15 -0700110 void (*inblk)(void __iomem *port, void *data, int length);
111 void (*outblk)(void __iomem *port, void *data, int length);
112 void (*dumpblk)(void __iomem *port, int length);
113
Ben Dooksa76836f2008-02-05 00:02:02 +0000114 struct device *dev; /* parent device */
115
Sascha Hauera1365272005-05-05 15:14:15 -0700116 struct resource *addr_res; /* resources found */
117 struct resource *data_res;
118 struct resource *addr_req; /* resources requested */
119 struct resource *data_req;
120 struct resource *irq_res;
121
Ben Dooksc029f442009-11-10 07:22:24 +0000122 int irq_wake;
123
Ben Dooks9a2f0372008-02-05 00:02:10 +0000124 struct mutex addr_lock; /* phy and eeprom access lock */
125
Ben Dooks8f5bf5f2008-05-08 11:36:42 +0100126 struct delayed_work phy_poll;
127 struct net_device *ndev;
128
Ben Dooks59eae1f2008-06-24 22:16:01 +0100129 spinlock_t lock;
Sascha Hauera1365272005-05-05 15:14:15 -0700130
131 struct mii_if_info mii;
Ben Dooks59eae1f2008-06-24 22:16:01 +0100132 u32 msg_enable;
Ben Dooksc029f442009-11-10 07:22:24 +0000133 u32 wake_state;
Yeasah Pell5dcc60b2009-07-06 18:12:33 -0700134
Yeasah Pell5dcc60b2009-07-06 18:12:33 -0700135 int ip_summed;
Sascha Hauera1365272005-05-05 15:14:15 -0700136} board_info_t;
137
Ben Dooks5b2b4ff2008-02-05 00:02:03 +0000138/* debug code */
139
140#define dm9000_dbg(db, lev, msg...) do { \
141 if ((lev) < CONFIG_DM9000_DEBUGLEVEL && \
142 (lev) < db->debug_level) { \
143 dev_dbg(db->dev, msg); \
144 } \
145} while (0)
146
Ben Dooks7da99852008-02-05 00:02:06 +0000147static inline board_info_t *to_dm9000_board(struct net_device *dev)
148{
Wang Chen4cf16532008-11-12 23:38:14 -0800149 return netdev_priv(dev);
Ben Dooks7da99852008-02-05 00:02:06 +0000150}
151
Sascha Hauera1365272005-05-05 15:14:15 -0700152/* DM9000 network board routine ---------------------------- */
153
154static void
155dm9000_reset(board_info_t * db)
156{
Ben Dooksa76836f2008-02-05 00:02:02 +0000157 dev_dbg(db->dev, "resetting device\n");
158
Sascha Hauera1365272005-05-05 15:14:15 -0700159 /* RESET device */
160 writeb(DM9000_NCR, db->io_addr);
161 udelay(200);
162 writeb(NCR_RST, db->io_data);
163 udelay(200);
164}
165
166/*
167 * Read a byte from I/O port
168 */
169static u8
170ior(board_info_t * db, int reg)
171{
172 writeb(reg, db->io_addr);
173 return readb(db->io_data);
174}
175
176/*
177 * Write a byte to I/O port
178 */
179
180static void
181iow(board_info_t * db, int reg, int value)
182{
183 writeb(reg, db->io_addr);
184 writeb(value, db->io_data);
185}
186
187/* routines for sending block to chip */
188
189static void dm9000_outblk_8bit(void __iomem *reg, void *data, int count)
190{
191 writesb(reg, data, count);
192}
193
194static void dm9000_outblk_16bit(void __iomem *reg, void *data, int count)
195{
196 writesw(reg, data, (count+1) >> 1);
197}
198
199static void dm9000_outblk_32bit(void __iomem *reg, void *data, int count)
200{
201 writesl(reg, data, (count+3) >> 2);
202}
203
204/* input block from chip to memory */
205
206static void dm9000_inblk_8bit(void __iomem *reg, void *data, int count)
207{
Sascha Hauer5f6b5512005-06-20 15:32:51 -0700208 readsb(reg, data, count);
Sascha Hauera1365272005-05-05 15:14:15 -0700209}
210
211
212static void dm9000_inblk_16bit(void __iomem *reg, void *data, int count)
213{
214 readsw(reg, data, (count+1) >> 1);
215}
216
217static void dm9000_inblk_32bit(void __iomem *reg, void *data, int count)
218{
219 readsl(reg, data, (count+3) >> 2);
220}
221
222/* dump block from chip to null */
223
224static void dm9000_dumpblk_8bit(void __iomem *reg, int count)
225{
226 int i;
227 int tmp;
228
229 for (i = 0; i < count; i++)
230 tmp = readb(reg);
231}
232
233static void dm9000_dumpblk_16bit(void __iomem *reg, int count)
234{
235 int i;
236 int tmp;
237
238 count = (count + 1) >> 1;
239
240 for (i = 0; i < count; i++)
241 tmp = readw(reg);
242}
243
244static void dm9000_dumpblk_32bit(void __iomem *reg, int count)
245{
246 int i;
247 int tmp;
248
249 count = (count + 3) >> 2;
250
251 for (i = 0; i < count; i++)
252 tmp = readl(reg);
253}
254
255/* dm9000_set_io
256 *
257 * select the specified set of io routines to use with the
258 * device
259 */
260
261static void dm9000_set_io(struct board_info *db, int byte_width)
262{
263 /* use the size of the data resource to work out what IO
264 * routines we want to use
265 */
266
267 switch (byte_width) {
268 case 1:
269 db->dumpblk = dm9000_dumpblk_8bit;
270 db->outblk = dm9000_outblk_8bit;
271 db->inblk = dm9000_inblk_8bit;
272 break;
273
Sascha Hauera1365272005-05-05 15:14:15 -0700274
275 case 3:
Ben Dooksa76836f2008-02-05 00:02:02 +0000276 dev_dbg(db->dev, ": 3 byte IO, falling back to 16bit\n");
277 case 2:
Sascha Hauera1365272005-05-05 15:14:15 -0700278 db->dumpblk = dm9000_dumpblk_16bit;
279 db->outblk = dm9000_outblk_16bit;
280 db->inblk = dm9000_inblk_16bit;
281 break;
282
283 case 4:
284 default:
285 db->dumpblk = dm9000_dumpblk_32bit;
286 db->outblk = dm9000_outblk_32bit;
287 db->inblk = dm9000_inblk_32bit;
288 break;
289 }
290}
291
Ben Dooks8f5bf5f2008-05-08 11:36:42 +0100292static void dm9000_schedule_poll(board_info_t *db)
293{
Ben Dooks6d406b32008-06-24 22:15:59 +0100294 if (db->type == TYPE_DM9000E)
295 schedule_delayed_work(&db->phy_poll, HZ * 2);
Ben Dooks8f5bf5f2008-05-08 11:36:42 +0100296}
Sascha Hauera1365272005-05-05 15:14:15 -0700297
Ben Dooksf42d8ae2008-02-05 00:02:21 +0000298static int dm9000_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
299{
300 board_info_t *dm = to_dm9000_board(dev);
301
302 if (!netif_running(dev))
303 return -EINVAL;
304
305 return generic_mii_ioctl(&dm->mii, if_mii(req), cmd, NULL);
306}
307
Ben Dooksf8d79e72008-06-24 22:16:02 +0100308static unsigned int
309dm9000_read_locked(board_info_t *db, int reg)
310{
311 unsigned long flags;
312 unsigned int ret;
313
314 spin_lock_irqsave(&db->lock, flags);
315 ret = ior(db, reg);
316 spin_unlock_irqrestore(&db->lock, flags);
317
318 return ret;
319}
320
321static int dm9000_wait_eeprom(board_info_t *db)
322{
323 unsigned int status;
324 int timeout = 8; /* wait max 8msec */
325
326 /* The DM9000 data sheets say we should be able to
327 * poll the ERRE bit in EPCR to wait for the EEPROM
328 * operation. From testing several chips, this bit
329 * does not seem to work.
330 *
331 * We attempt to use the bit, but fall back to the
332 * timeout (which is why we do not return an error
333 * on expiry) to say that the EEPROM operation has
334 * completed.
335 */
336
337 while (1) {
338 status = dm9000_read_locked(db, DM9000_EPCR);
339
340 if ((status & EPCR_ERRE) == 0)
341 break;
342
Ben Dooks2fcf06c2008-06-24 22:16:05 +0100343 msleep(1);
344
Ben Dooksf8d79e72008-06-24 22:16:02 +0100345 if (timeout-- < 0) {
346 dev_dbg(db->dev, "timeout waiting EEPROM\n");
347 break;
348 }
349 }
350
351 return 0;
352}
353
354/*
355 * Read a word data from EEPROM
356 */
357static void
358dm9000_read_eeprom(board_info_t *db, int offset, u8 *to)
359{
360 unsigned long flags;
361
362 if (db->flags & DM9000_PLATF_NO_EEPROM) {
363 to[0] = 0xff;
364 to[1] = 0xff;
365 return;
366 }
367
368 mutex_lock(&db->addr_lock);
369
370 spin_lock_irqsave(&db->lock, flags);
371
372 iow(db, DM9000_EPAR, offset);
373 iow(db, DM9000_EPCR, EPCR_ERPRR);
374
375 spin_unlock_irqrestore(&db->lock, flags);
376
377 dm9000_wait_eeprom(db);
378
379 /* delay for at-least 150uS */
380 msleep(1);
381
382 spin_lock_irqsave(&db->lock, flags);
383
384 iow(db, DM9000_EPCR, 0x0);
385
386 to[0] = ior(db, DM9000_EPDRL);
387 to[1] = ior(db, DM9000_EPDRH);
388
389 spin_unlock_irqrestore(&db->lock, flags);
390
391 mutex_unlock(&db->addr_lock);
392}
393
394/*
395 * Write a word data to SROM
396 */
397static void
398dm9000_write_eeprom(board_info_t *db, int offset, u8 *data)
399{
400 unsigned long flags;
401
402 if (db->flags & DM9000_PLATF_NO_EEPROM)
403 return;
404
405 mutex_lock(&db->addr_lock);
406
407 spin_lock_irqsave(&db->lock, flags);
408 iow(db, DM9000_EPAR, offset);
409 iow(db, DM9000_EPDRH, data[1]);
410 iow(db, DM9000_EPDRL, data[0]);
411 iow(db, DM9000_EPCR, EPCR_WEP | EPCR_ERPRW);
412 spin_unlock_irqrestore(&db->lock, flags);
413
414 dm9000_wait_eeprom(db);
415
416 mdelay(1); /* wait at least 150uS to clear */
417
418 spin_lock_irqsave(&db->lock, flags);
419 iow(db, DM9000_EPCR, 0);
420 spin_unlock_irqrestore(&db->lock, flags);
421
422 mutex_unlock(&db->addr_lock);
423}
424
Ben Dooks7da99852008-02-05 00:02:06 +0000425/* ethtool ops */
426
427static void dm9000_get_drvinfo(struct net_device *dev,
428 struct ethtool_drvinfo *info)
429{
430 board_info_t *dm = to_dm9000_board(dev);
431
432 strcpy(info->driver, CARDNAME);
433 strcpy(info->version, DRV_VERSION);
434 strcpy(info->bus_info, to_platform_device(dm->dev)->name);
435}
436
Ben Dookse662ee02008-02-05 00:02:12 +0000437static u32 dm9000_get_msglevel(struct net_device *dev)
438{
439 board_info_t *dm = to_dm9000_board(dev);
440
441 return dm->msg_enable;
442}
443
444static void dm9000_set_msglevel(struct net_device *dev, u32 value)
445{
446 board_info_t *dm = to_dm9000_board(dev);
447
448 dm->msg_enable = value;
449}
450
Ben Dooks7da99852008-02-05 00:02:06 +0000451static int dm9000_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
452{
453 board_info_t *dm = to_dm9000_board(dev);
Ben Dooks7da99852008-02-05 00:02:06 +0000454
Ben Dooks7da99852008-02-05 00:02:06 +0000455 mii_ethtool_gset(&dm->mii, cmd);
Ben Dooks7da99852008-02-05 00:02:06 +0000456 return 0;
457}
458
459static int dm9000_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
460{
461 board_info_t *dm = to_dm9000_board(dev);
Ben Dooks7da99852008-02-05 00:02:06 +0000462
Ben Dooks9a2f0372008-02-05 00:02:10 +0000463 return mii_ethtool_sset(&dm->mii, cmd);
Ben Dooks7da99852008-02-05 00:02:06 +0000464}
465
466static int dm9000_nway_reset(struct net_device *dev)
467{
468 board_info_t *dm = to_dm9000_board(dev);
469 return mii_nway_restart(&dm->mii);
470}
471
Michał Mirosławc88fcb32011-04-15 04:50:49 +0000472static int dm9000_set_features(struct net_device *dev, u32 features)
Yeasah Pell5dcc60b2009-07-06 18:12:33 -0700473{
474 board_info_t *dm = to_dm9000_board(dev);
Michał Mirosławc88fcb32011-04-15 04:50:49 +0000475 u32 changed = dev->features ^ features;
Baruch Siach380fefb2010-05-17 17:45:48 -0700476 unsigned long flags;
Michał Mirosławc88fcb32011-04-15 04:50:49 +0000477
478 if (!(changed & NETIF_F_RXCSUM))
479 return 0;
Baruch Siach380fefb2010-05-17 17:45:48 -0700480
481 spin_lock_irqsave(&dm->lock, flags);
Michał Mirosławc88fcb32011-04-15 04:50:49 +0000482 iow(dm, DM9000_RCSR, (features & NETIF_F_RXCSUM) ? RCSR_CSUM : 0);
Baruch Siach380fefb2010-05-17 17:45:48 -0700483 spin_unlock_irqrestore(&dm->lock, flags);
484
Michał Mirosławc88fcb32011-04-15 04:50:49 +0000485 return 0;
Yeasah Pell5dcc60b2009-07-06 18:12:33 -0700486}
487
Ben Dooks7da99852008-02-05 00:02:06 +0000488static u32 dm9000_get_link(struct net_device *dev)
489{
490 board_info_t *dm = to_dm9000_board(dev);
Ben Dooksaa1eb452008-06-24 22:16:03 +0100491 u32 ret;
492
493 if (dm->flags & DM9000_PLATF_EXT_PHY)
494 ret = mii_link_ok(&dm->mii);
495 else
496 ret = dm9000_read_locked(dm, DM9000_NSR) & NSR_LINKST ? 1 : 0;
497
498 return ret;
Ben Dooks7da99852008-02-05 00:02:06 +0000499}
500
Ben Dooks29d52e52008-02-05 00:02:11 +0000501#define DM_EEPROM_MAGIC (0x444D394B)
502
503static int dm9000_get_eeprom_len(struct net_device *dev)
504{
505 return 128;
506}
507
508static int dm9000_get_eeprom(struct net_device *dev,
509 struct ethtool_eeprom *ee, u8 *data)
510{
511 board_info_t *dm = to_dm9000_board(dev);
512 int offset = ee->offset;
513 int len = ee->len;
514 int i;
515
516 /* EEPROM access is aligned to two bytes */
517
518 if ((len & 1) != 0 || (offset & 1) != 0)
519 return -EINVAL;
520
Ben Dooksbb44fb702008-02-05 00:02:20 +0000521 if (dm->flags & DM9000_PLATF_NO_EEPROM)
522 return -ENOENT;
523
Ben Dooks29d52e52008-02-05 00:02:11 +0000524 ee->magic = DM_EEPROM_MAGIC;
525
526 for (i = 0; i < len; i += 2)
527 dm9000_read_eeprom(dm, (offset + i) / 2, data + i);
528
529 return 0;
530}
531
532static int dm9000_set_eeprom(struct net_device *dev,
533 struct ethtool_eeprom *ee, u8 *data)
534{
535 board_info_t *dm = to_dm9000_board(dev);
536 int offset = ee->offset;
537 int len = ee->len;
538 int i;
539
540 /* EEPROM access is aligned to two bytes */
541
542 if ((len & 1) != 0 || (offset & 1) != 0)
543 return -EINVAL;
544
Ben Dooksbb44fb702008-02-05 00:02:20 +0000545 if (dm->flags & DM9000_PLATF_NO_EEPROM)
546 return -ENOENT;
547
Ben Dooks29d52e52008-02-05 00:02:11 +0000548 if (ee->magic != DM_EEPROM_MAGIC)
549 return -EINVAL;
550
551 for (i = 0; i < len; i += 2)
552 dm9000_write_eeprom(dm, (offset + i) / 2, data + i);
553
554 return 0;
555}
556
Ben Dooksc029f442009-11-10 07:22:24 +0000557static void dm9000_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
558{
559 board_info_t *dm = to_dm9000_board(dev);
560
561 memset(w, 0, sizeof(struct ethtool_wolinfo));
562
563 /* note, we could probably support wake-phy too */
564 w->supported = dm->wake_supported ? WAKE_MAGIC : 0;
565 w->wolopts = dm->wake_state;
566}
567
568static int dm9000_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
569{
570 board_info_t *dm = to_dm9000_board(dev);
571 unsigned long flags;
572 u32 opts = w->wolopts;
573 u32 wcr = 0;
574
575 if (!dm->wake_supported)
576 return -EOPNOTSUPP;
577
578 if (opts & ~WAKE_MAGIC)
579 return -EINVAL;
580
581 if (opts & WAKE_MAGIC)
582 wcr |= WCR_MAGICEN;
583
584 mutex_lock(&dm->addr_lock);
585
586 spin_lock_irqsave(&dm->lock, flags);
587 iow(dm, DM9000_WCR, wcr);
588 spin_unlock_irqrestore(&dm->lock, flags);
589
590 mutex_unlock(&dm->addr_lock);
591
592 if (dm->wake_state != opts) {
593 /* change in wol state, update IRQ state */
594
595 if (!dm->wake_state)
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200596 irq_set_irq_wake(dm->irq_wake, 1);
Ben Dooksc029f442009-11-10 07:22:24 +0000597 else if (dm->wake_state & !opts)
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200598 irq_set_irq_wake(dm->irq_wake, 0);
Ben Dooksc029f442009-11-10 07:22:24 +0000599 }
600
601 dm->wake_state = opts;
602 return 0;
603}
604
Ben Dooks7da99852008-02-05 00:02:06 +0000605static const struct ethtool_ops dm9000_ethtool_ops = {
606 .get_drvinfo = dm9000_get_drvinfo,
607 .get_settings = dm9000_get_settings,
608 .set_settings = dm9000_set_settings,
Ben Dookse662ee02008-02-05 00:02:12 +0000609 .get_msglevel = dm9000_get_msglevel,
610 .set_msglevel = dm9000_set_msglevel,
Ben Dooks7da99852008-02-05 00:02:06 +0000611 .nway_reset = dm9000_nway_reset,
612 .get_link = dm9000_get_link,
Ben Dooksc029f442009-11-10 07:22:24 +0000613 .get_wol = dm9000_get_wol,
614 .set_wol = dm9000_set_wol,
Ben Dooks29d52e52008-02-05 00:02:11 +0000615 .get_eeprom_len = dm9000_get_eeprom_len,
616 .get_eeprom = dm9000_get_eeprom,
617 .set_eeprom = dm9000_set_eeprom,
Ben Dooks7da99852008-02-05 00:02:06 +0000618};
619
Ben Dooksf8dd0ec2008-06-24 22:16:04 +0100620static void dm9000_show_carrier(board_info_t *db,
621 unsigned carrier, unsigned nsr)
622{
623 struct net_device *ndev = db->ndev;
624 unsigned ncr = dm9000_read_locked(db, DM9000_NCR);
625
626 if (carrier)
627 dev_info(db->dev, "%s: link up, %dMbps, %s-duplex, no LPA\n",
628 ndev->name, (nsr & NSR_SPEED) ? 10 : 100,
629 (ncr & NCR_FDX) ? "full" : "half");
630 else
631 dev_info(db->dev, "%s: link down\n", ndev->name);
632}
633
Ben Dooks8f5bf5f2008-05-08 11:36:42 +0100634static void
635dm9000_poll_work(struct work_struct *w)
636{
Jean Delvarebf6aede2009-04-02 16:56:54 -0700637 struct delayed_work *dw = to_delayed_work(w);
Ben Dooks8f5bf5f2008-05-08 11:36:42 +0100638 board_info_t *db = container_of(dw, board_info_t, phy_poll);
Ben Dooksf8dd0ec2008-06-24 22:16:04 +0100639 struct net_device *ndev = db->ndev;
Ben Dooks8f5bf5f2008-05-08 11:36:42 +0100640
Ben Dooksf8dd0ec2008-06-24 22:16:04 +0100641 if (db->flags & DM9000_PLATF_SIMPLE_PHY &&
642 !(db->flags & DM9000_PLATF_EXT_PHY)) {
643 unsigned nsr = dm9000_read_locked(db, DM9000_NSR);
644 unsigned old_carrier = netif_carrier_ok(ndev) ? 1 : 0;
645 unsigned new_carrier;
646
647 new_carrier = (nsr & NSR_LINKST) ? 1 : 0;
648
649 if (old_carrier != new_carrier) {
650 if (netif_msg_link(db))
651 dm9000_show_carrier(db, new_carrier, nsr);
652
653 if (!new_carrier)
654 netif_carrier_off(ndev);
655 else
656 netif_carrier_on(ndev);
657 }
658 } else
659 mii_check_media(&db->mii, netif_msg_link(db), 0);
Ben Dooks8f5bf5f2008-05-08 11:36:42 +0100660
Ben Dooksf8dd0ec2008-06-24 22:16:04 +0100661 if (netif_running(ndev))
Ben Dooks8f5bf5f2008-05-08 11:36:42 +0100662 dm9000_schedule_poll(db);
663}
Ben Dooks7da99852008-02-05 00:02:06 +0000664
Sascha Hauera1365272005-05-05 15:14:15 -0700665/* dm9000_release_board
666 *
667 * release a board, and any mapped resources
668 */
669
670static void
671dm9000_release_board(struct platform_device *pdev, struct board_info *db)
672{
Sascha Hauera1365272005-05-05 15:14:15 -0700673 /* unmap our resources */
674
675 iounmap(db->io_addr);
676 iounmap(db->io_data);
677
678 /* release the resources */
679
Ben Dooks9088fa4f2008-06-24 22:16:00 +0100680 release_resource(db->data_req);
681 kfree(db->data_req);
Sascha Hauera1365272005-05-05 15:14:15 -0700682
Ben Dooks9088fa4f2008-06-24 22:16:00 +0100683 release_resource(db->addr_req);
684 kfree(db->addr_req);
Sascha Hauera1365272005-05-05 15:14:15 -0700685}
686
Ben Dooks6d406b32008-06-24 22:15:59 +0100687static unsigned char dm9000_type_to_char(enum dm9000_type type)
688{
689 switch (type) {
690 case TYPE_DM9000E: return 'e';
691 case TYPE_DM9000A: return 'a';
692 case TYPE_DM9000B: return 'b';
693 }
694
695 return '?';
696}
697
Ben Dooksf8d79e72008-06-24 22:16:02 +0100698/*
699 * Set DM9000 multicast address
700 */
701static void
Baruch Siach380fefb2010-05-17 17:45:48 -0700702dm9000_hash_table_unlocked(struct net_device *dev)
Ben Dooksf8d79e72008-06-24 22:16:02 +0100703{
Wang Chen4cf16532008-11-12 23:38:14 -0800704 board_info_t *db = netdev_priv(dev);
Jiri Pirko22bedad2010-04-01 21:22:57 +0000705 struct netdev_hw_addr *ha;
Ben Dooksf8d79e72008-06-24 22:16:02 +0100706 int i, oft;
707 u32 hash_val;
708 u16 hash_table[4];
709 u8 rcr = RCR_DIS_LONG | RCR_DIS_CRC | RCR_RXEN;
Ben Dooksf8d79e72008-06-24 22:16:02 +0100710
711 dm9000_dbg(db, 1, "entering %s\n", __func__);
712
Ben Dooksf8d79e72008-06-24 22:16:02 +0100713 for (i = 0, oft = DM9000_PAR; i < 6; i++, oft++)
714 iow(db, oft, dev->dev_addr[i]);
715
716 /* Clear Hash Table */
717 for (i = 0; i < 4; i++)
718 hash_table[i] = 0x0;
719
720 /* broadcast address */
721 hash_table[3] = 0x8000;
722
723 if (dev->flags & IFF_PROMISC)
724 rcr |= RCR_PRMSC;
725
726 if (dev->flags & IFF_ALLMULTI)
727 rcr |= RCR_ALL;
728
729 /* the multicast address in Hash Table : 64 bits */
Jiri Pirko22bedad2010-04-01 21:22:57 +0000730 netdev_for_each_mc_addr(ha, dev) {
731 hash_val = ether_crc_le(6, ha->addr) & 0x3f;
Ben Dooksf8d79e72008-06-24 22:16:02 +0100732 hash_table[hash_val / 16] |= (u16) 1 << (hash_val % 16);
733 }
734
735 /* Write the hash table to MAC MD table */
736 for (i = 0, oft = DM9000_MAR; i < 4; i++) {
737 iow(db, oft++, hash_table[i]);
738 iow(db, oft++, hash_table[i] >> 8);
739 }
740
741 iow(db, DM9000_RCR, rcr);
Baruch Siach380fefb2010-05-17 17:45:48 -0700742}
743
744static void
745dm9000_hash_table(struct net_device *dev)
746{
747 board_info_t *db = netdev_priv(dev);
748 unsigned long flags;
749
750 spin_lock_irqsave(&db->lock, flags);
751 dm9000_hash_table_unlocked(dev);
Ben Dooksf8d79e72008-06-24 22:16:02 +0100752 spin_unlock_irqrestore(&db->lock, flags);
753}
754
755/*
Eric Dumazet1ae5dc32010-05-10 05:01:31 -0700756 * Initialize dm9000 board
Ben Dooksf8d79e72008-06-24 22:16:02 +0100757 */
758static void
759dm9000_init_dm9000(struct net_device *dev)
760{
Wang Chen4cf16532008-11-12 23:38:14 -0800761 board_info_t *db = netdev_priv(dev);
Ben Dooksf8d79e72008-06-24 22:16:02 +0100762 unsigned int imr;
Ben Dooksc029f442009-11-10 07:22:24 +0000763 unsigned int ncr;
Ben Dooksf8d79e72008-06-24 22:16:02 +0100764
765 dm9000_dbg(db, 1, "entering %s\n", __func__);
766
767 /* I/O mode */
768 db->io_mode = ior(db, DM9000_ISR) >> 6; /* ISR bit7:6 keeps I/O mode */
769
Yeasah Pell5dcc60b2009-07-06 18:12:33 -0700770 /* Checksum mode */
Michał Mirosławc88fcb32011-04-15 04:50:49 +0000771 if (dev->hw_features & NETIF_F_RXCSUM)
Mark Brown56d37f12011-04-18 01:04:37 +0000772 iow(db, DM9000_RCSR,
Michał Mirosławc88fcb32011-04-15 04:50:49 +0000773 (dev->features & NETIF_F_RXCSUM) ? RCSR_CSUM : 0);
Yeasah Pell5dcc60b2009-07-06 18:12:33 -0700774
Ben Dooksf8d79e72008-06-24 22:16:02 +0100775 iow(db, DM9000_GPCR, GPCR_GEP_CNTL); /* Let GPIO0 output */
Ben Dooksf8d79e72008-06-24 22:16:02 +0100776
Ben Dooksc029f442009-11-10 07:22:24 +0000777 ncr = (db->flags & DM9000_PLATF_EXT_PHY) ? NCR_EXT_PHY : 0;
778
779 /* if wol is needed, then always set NCR_WAKEEN otherwise we end
780 * up dumping the wake events if we disable this. There is already
781 * a wake-mask in DM9000_WCR */
782 if (db->wake_supported)
783 ncr |= NCR_WAKEEN;
784
785 iow(db, DM9000_NCR, ncr);
Ben Dooksf8d79e72008-06-24 22:16:02 +0100786
787 /* Program operating register */
788 iow(db, DM9000_TCR, 0); /* TX Polling clear */
789 iow(db, DM9000_BPTR, 0x3f); /* Less 3Kb, 200us */
790 iow(db, DM9000_FCR, 0xff); /* Flow Control */
791 iow(db, DM9000_SMCR, 0); /* Special Mode */
792 /* clear TX status */
793 iow(db, DM9000_NSR, NSR_WAKEST | NSR_TX2END | NSR_TX1END);
794 iow(db, DM9000_ISR, ISR_CLR_STATUS); /* Clear interrupt status */
795
796 /* Set address filter table */
Baruch Siach380fefb2010-05-17 17:45:48 -0700797 dm9000_hash_table_unlocked(dev);
Ben Dooksf8d79e72008-06-24 22:16:02 +0100798
799 imr = IMR_PAR | IMR_PTM | IMR_PRM;
800 if (db->type != TYPE_DM9000E)
801 imr |= IMR_LNKCHNG;
802
803 db->imr_all = imr;
804
805 /* Enable TX/RX interrupt mask */
806 iow(db, DM9000_IMR, imr);
807
808 /* Init Driver variable */
809 db->tx_pkt_cnt = 0;
810 db->queue_pkt_len = 0;
Eric Dumazet1ae5dc32010-05-10 05:01:31 -0700811 dev->trans_start = jiffies;
Ben Dooksf8d79e72008-06-24 22:16:02 +0100812}
813
814/* Our watchdog timed out. Called by the networking layer */
815static void dm9000_timeout(struct net_device *dev)
816{
Wang Chen4cf16532008-11-12 23:38:14 -0800817 board_info_t *db = netdev_priv(dev);
Ben Dooksf8d79e72008-06-24 22:16:02 +0100818 u8 reg_save;
819 unsigned long flags;
820
821 /* Save previous register address */
Ben Dooksf8d79e72008-06-24 22:16:02 +0100822 spin_lock_irqsave(&db->lock, flags);
Henry Nestler8dde9242011-02-20 11:44:58 +0000823 reg_save = readb(db->io_addr);
Ben Dooksf8d79e72008-06-24 22:16:02 +0100824
825 netif_stop_queue(dev);
826 dm9000_reset(db);
827 dm9000_init_dm9000(dev);
828 /* We can accept TX packets again */
Eric Dumazet1ae5dc32010-05-10 05:01:31 -0700829 dev->trans_start = jiffies; /* prevent tx timeout */
Ben Dooksf8d79e72008-06-24 22:16:02 +0100830 netif_wake_queue(dev);
831
832 /* Restore previous register address */
833 writeb(reg_save, db->io_addr);
834 spin_unlock_irqrestore(&db->lock, flags);
835}
836
Yeasah Pell5dcc60b2009-07-06 18:12:33 -0700837static void dm9000_send_packet(struct net_device *dev,
838 int ip_summed,
839 u16 pkt_len)
840{
841 board_info_t *dm = to_dm9000_board(dev);
842
843 /* The DM9000 is not smart enough to leave fragmented packets alone. */
844 if (dm->ip_summed != ip_summed) {
845 if (ip_summed == CHECKSUM_NONE)
846 iow(dm, DM9000_TCCR, 0);
847 else
848 iow(dm, DM9000_TCCR, TCCR_IP | TCCR_UDP | TCCR_TCP);
849 dm->ip_summed = ip_summed;
850 }
851
852 /* Set TX length to DM9000 */
853 iow(dm, DM9000_TXPLL, pkt_len);
854 iow(dm, DM9000_TXPLH, pkt_len >> 8);
855
856 /* Issue TX polling command */
857 iow(dm, DM9000_TCR, TCR_TXREQ); /* Cleared after TX complete */
858}
859
Ben Dooksf8d79e72008-06-24 22:16:02 +0100860/*
861 * Hardware start transmission.
862 * Send a packet to media from the upper layer.
863 */
864static int
865dm9000_start_xmit(struct sk_buff *skb, struct net_device *dev)
866{
867 unsigned long flags;
Wang Chen4cf16532008-11-12 23:38:14 -0800868 board_info_t *db = netdev_priv(dev);
Ben Dooksf8d79e72008-06-24 22:16:02 +0100869
870 dm9000_dbg(db, 3, "%s:\n", __func__);
871
872 if (db->tx_pkt_cnt > 1)
Patrick McHardy5b548142009-06-12 06:22:29 +0000873 return NETDEV_TX_BUSY;
Ben Dooksf8d79e72008-06-24 22:16:02 +0100874
875 spin_lock_irqsave(&db->lock, flags);
876
877 /* Move data to DM9000 TX RAM */
878 writeb(DM9000_MWCMD, db->io_addr);
879
880 (db->outblk)(db->io_data, skb->data, skb->len);
881 dev->stats.tx_bytes += skb->len;
882
883 db->tx_pkt_cnt++;
884 /* TX control: First packet immediately send, second packet queue */
885 if (db->tx_pkt_cnt == 1) {
Yeasah Pell5dcc60b2009-07-06 18:12:33 -0700886 dm9000_send_packet(dev, skb->ip_summed, skb->len);
Ben Dooksf8d79e72008-06-24 22:16:02 +0100887 } else {
888 /* Second packet */
889 db->queue_pkt_len = skb->len;
Yeasah Pell5dcc60b2009-07-06 18:12:33 -0700890 db->queue_ip_summed = skb->ip_summed;
Ben Dooksf8d79e72008-06-24 22:16:02 +0100891 netif_stop_queue(dev);
892 }
893
894 spin_unlock_irqrestore(&db->lock, flags);
895
896 /* free this SKB */
897 dev_kfree_skb(skb);
898
Patrick McHardy6ed10652009-06-23 06:03:08 +0000899 return NETDEV_TX_OK;
Ben Dooksf8d79e72008-06-24 22:16:02 +0100900}
901
902/*
903 * DM9000 interrupt handler
904 * receive the packet to upper layer, free the transmitted packet
905 */
906
907static void dm9000_tx_done(struct net_device *dev, board_info_t *db)
908{
909 int tx_status = ior(db, DM9000_NSR); /* Got TX status */
910
911 if (tx_status & (NSR_TX2END | NSR_TX1END)) {
912 /* One packet sent complete */
913 db->tx_pkt_cnt--;
914 dev->stats.tx_packets++;
915
916 if (netif_msg_tx_done(db))
917 dev_dbg(db->dev, "tx done, NSR %02x\n", tx_status);
918
919 /* Queue packet check & send */
Yeasah Pell5dcc60b2009-07-06 18:12:33 -0700920 if (db->tx_pkt_cnt > 0)
921 dm9000_send_packet(dev, db->queue_ip_summed,
922 db->queue_pkt_len);
Ben Dooksf8d79e72008-06-24 22:16:02 +0100923 netif_wake_queue(dev);
924 }
925}
926
927struct dm9000_rxhdr {
928 u8 RxPktReady;
929 u8 RxStatus;
930 __le16 RxLen;
Eric Dumazetba2d3582010-06-02 18:10:09 +0000931} __packed;
Ben Dooksf8d79e72008-06-24 22:16:02 +0100932
933/*
934 * Received a packet and pass to upper layer
935 */
936static void
937dm9000_rx(struct net_device *dev)
938{
Wang Chen4cf16532008-11-12 23:38:14 -0800939 board_info_t *db = netdev_priv(dev);
Ben Dooksf8d79e72008-06-24 22:16:02 +0100940 struct dm9000_rxhdr rxhdr;
941 struct sk_buff *skb;
942 u8 rxbyte, *rdptr;
943 bool GoodPacket;
944 int RxLen;
945
946 /* Check packet ready or not */
947 do {
948 ior(db, DM9000_MRCMDX); /* Dummy read */
949
950 /* Get most updated data */
951 rxbyte = readb(db->io_data);
952
953 /* Status check: this byte must be 0 or 1 */
Yeasah Pell5dcc60b2009-07-06 18:12:33 -0700954 if (rxbyte & DM9000_PKT_ERR) {
Ben Dooksf8d79e72008-06-24 22:16:02 +0100955 dev_warn(db->dev, "status check fail: %d\n", rxbyte);
956 iow(db, DM9000_RCR, 0x00); /* Stop Device */
957 iow(db, DM9000_ISR, IMR_PAR); /* Stop INT request */
958 return;
959 }
960
Yeasah Pell5dcc60b2009-07-06 18:12:33 -0700961 if (!(rxbyte & DM9000_PKT_RDY))
Ben Dooksf8d79e72008-06-24 22:16:02 +0100962 return;
963
964 /* A packet ready now & Get status/length */
965 GoodPacket = true;
966 writeb(DM9000_MRCMD, db->io_addr);
967
968 (db->inblk)(db->io_data, &rxhdr, sizeof(rxhdr));
969
970 RxLen = le16_to_cpu(rxhdr.RxLen);
971
972 if (netif_msg_rx_status(db))
973 dev_dbg(db->dev, "RX: status %02x, length %04x\n",
974 rxhdr.RxStatus, RxLen);
975
976 /* Packet Status check */
977 if (RxLen < 0x40) {
978 GoodPacket = false;
979 if (netif_msg_rx_err(db))
980 dev_dbg(db->dev, "RX: Bad Packet (runt)\n");
981 }
982
983 if (RxLen > DM9000_PKT_MAX) {
984 dev_dbg(db->dev, "RST: RX Len:%x\n", RxLen);
985 }
986
Ben Dooksf8e5e772008-07-17 20:29:13 +0100987 /* rxhdr.RxStatus is identical to RSR register. */
988 if (rxhdr.RxStatus & (RSR_FOE | RSR_CE | RSR_AE |
989 RSR_PLE | RSR_RWTO |
990 RSR_LCS | RSR_RF)) {
Ben Dooksf8d79e72008-06-24 22:16:02 +0100991 GoodPacket = false;
Ben Dooksf8e5e772008-07-17 20:29:13 +0100992 if (rxhdr.RxStatus & RSR_FOE) {
Ben Dooksf8d79e72008-06-24 22:16:02 +0100993 if (netif_msg_rx_err(db))
994 dev_dbg(db->dev, "fifo error\n");
995 dev->stats.rx_fifo_errors++;
996 }
Ben Dooksf8e5e772008-07-17 20:29:13 +0100997 if (rxhdr.RxStatus & RSR_CE) {
Ben Dooksf8d79e72008-06-24 22:16:02 +0100998 if (netif_msg_rx_err(db))
999 dev_dbg(db->dev, "crc error\n");
1000 dev->stats.rx_crc_errors++;
1001 }
Ben Dooksf8e5e772008-07-17 20:29:13 +01001002 if (rxhdr.RxStatus & RSR_RF) {
Ben Dooksf8d79e72008-06-24 22:16:02 +01001003 if (netif_msg_rx_err(db))
1004 dev_dbg(db->dev, "length error\n");
1005 dev->stats.rx_length_errors++;
1006 }
1007 }
1008
1009 /* Move data from DM9000 */
Joe Perches8e95a202009-12-03 07:58:21 +00001010 if (GoodPacket &&
1011 ((skb = dev_alloc_skb(RxLen + 4)) != NULL)) {
Ben Dooksf8d79e72008-06-24 22:16:02 +01001012 skb_reserve(skb, 2);
1013 rdptr = (u8 *) skb_put(skb, RxLen - 4);
1014
1015 /* Read received packet from RX SRAM */
1016
1017 (db->inblk)(db->io_data, rdptr, RxLen);
1018 dev->stats.rx_bytes += RxLen;
1019
1020 /* Pass to upper layer */
1021 skb->protocol = eth_type_trans(skb, dev);
Michał Mirosławc88fcb32011-04-15 04:50:49 +00001022 if (dev->features & NETIF_F_RXCSUM) {
Yeasah Pell5dcc60b2009-07-06 18:12:33 -07001023 if ((((rxbyte & 0x1c) << 3) & rxbyte) == 0)
1024 skb->ip_summed = CHECKSUM_UNNECESSARY;
1025 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001026 skb_checksum_none_assert(skb);
Yeasah Pell5dcc60b2009-07-06 18:12:33 -07001027 }
Ben Dooksf8d79e72008-06-24 22:16:02 +01001028 netif_rx(skb);
1029 dev->stats.rx_packets++;
1030
1031 } else {
1032 /* need to dump the packet's data */
1033
1034 (db->dumpblk)(db->io_data, RxLen);
1035 }
Yeasah Pell5dcc60b2009-07-06 18:12:33 -07001036 } while (rxbyte & DM9000_PKT_RDY);
Ben Dooksf8d79e72008-06-24 22:16:02 +01001037}
1038
1039static irqreturn_t dm9000_interrupt(int irq, void *dev_id)
1040{
1041 struct net_device *dev = dev_id;
Wang Chen4cf16532008-11-12 23:38:14 -08001042 board_info_t *db = netdev_priv(dev);
Ben Dooksf8d79e72008-06-24 22:16:02 +01001043 int int_status;
David Brownelle3162d32009-03-22 21:28:39 -07001044 unsigned long flags;
Ben Dooksf8d79e72008-06-24 22:16:02 +01001045 u8 reg_save;
1046
1047 dm9000_dbg(db, 3, "entering %s\n", __func__);
1048
1049 /* A real interrupt coming */
1050
David Brownelle3162d32009-03-22 21:28:39 -07001051 /* holders of db->lock must always block IRQs */
1052 spin_lock_irqsave(&db->lock, flags);
Ben Dooksf8d79e72008-06-24 22:16:02 +01001053
1054 /* Save previous register address */
1055 reg_save = readb(db->io_addr);
1056
1057 /* Disable all interrupts */
1058 iow(db, DM9000_IMR, IMR_PAR);
1059
1060 /* Got DM9000 interrupt status */
1061 int_status = ior(db, DM9000_ISR); /* Got ISR */
1062 iow(db, DM9000_ISR, int_status); /* Clear ISR status */
1063
1064 if (netif_msg_intr(db))
1065 dev_dbg(db->dev, "interrupt status %02x\n", int_status);
1066
1067 /* Received the coming packet */
1068 if (int_status & ISR_PRS)
1069 dm9000_rx(dev);
1070
1071 /* Trnasmit Interrupt check */
1072 if (int_status & ISR_PTS)
1073 dm9000_tx_done(dev, db);
1074
1075 if (db->type != TYPE_DM9000E) {
1076 if (int_status & ISR_LNKCHNG) {
1077 /* fire a link-change request */
1078 schedule_delayed_work(&db->phy_poll, 1);
1079 }
1080 }
1081
1082 /* Re-enable interrupt mask */
1083 iow(db, DM9000_IMR, db->imr_all);
1084
1085 /* Restore previous register address */
1086 writeb(reg_save, db->io_addr);
1087
David Brownelle3162d32009-03-22 21:28:39 -07001088 spin_unlock_irqrestore(&db->lock, flags);
Ben Dooksf8d79e72008-06-24 22:16:02 +01001089
1090 return IRQ_HANDLED;
1091}
1092
Ben Dooksc029f442009-11-10 07:22:24 +00001093static irqreturn_t dm9000_wol_interrupt(int irq, void *dev_id)
1094{
1095 struct net_device *dev = dev_id;
1096 board_info_t *db = netdev_priv(dev);
1097 unsigned long flags;
1098 unsigned nsr, wcr;
1099
1100 spin_lock_irqsave(&db->lock, flags);
1101
1102 nsr = ior(db, DM9000_NSR);
1103 wcr = ior(db, DM9000_WCR);
1104
1105 dev_dbg(db->dev, "%s: NSR=0x%02x, WCR=0x%02x\n", __func__, nsr, wcr);
1106
1107 if (nsr & NSR_WAKEST) {
1108 /* clear, so we can avoid */
1109 iow(db, DM9000_NSR, NSR_WAKEST);
1110
1111 if (wcr & WCR_LINKST)
1112 dev_info(db->dev, "wake by link status change\n");
1113 if (wcr & WCR_SAMPLEST)
1114 dev_info(db->dev, "wake by sample packet\n");
1115 if (wcr & WCR_MAGICST )
1116 dev_info(db->dev, "wake by magic packet\n");
1117 if (!(wcr & (WCR_LINKST | WCR_SAMPLEST | WCR_MAGICST)))
1118 dev_err(db->dev, "wake signalled with no reason? "
1119 "NSR=0x%02x, WSR=0x%02x\n", nsr, wcr);
1120
1121 }
1122
1123 spin_unlock_irqrestore(&db->lock, flags);
1124
1125 return (nsr & NSR_WAKEST) ? IRQ_HANDLED : IRQ_NONE;
1126}
1127
Ben Dooksf8d79e72008-06-24 22:16:02 +01001128#ifdef CONFIG_NET_POLL_CONTROLLER
1129/*
1130 *Used by netconsole
1131 */
1132static void dm9000_poll_controller(struct net_device *dev)
1133{
1134 disable_irq(dev->irq);
1135 dm9000_interrupt(dev->irq, dev);
1136 enable_irq(dev->irq);
1137}
1138#endif
1139
1140/*
1141 * Open the interface.
1142 * The interface is opened whenever "ifconfig" actives it.
1143 */
1144static int
1145dm9000_open(struct net_device *dev)
1146{
Wang Chen4cf16532008-11-12 23:38:14 -08001147 board_info_t *db = netdev_priv(dev);
Ben Dooksf8d79e72008-06-24 22:16:02 +01001148 unsigned long irqflags = db->irq_res->flags & IRQF_TRIGGER_MASK;
1149
1150 if (netif_msg_ifup(db))
1151 dev_dbg(db->dev, "enabling %s\n", dev->name);
1152
1153 /* If there is no IRQ type specified, default to something that
1154 * may work, and tell the user that this is a problem */
1155
Ben Dooks6ff4ff02008-06-24 22:16:07 +01001156 if (irqflags == IRQF_TRIGGER_NONE)
Ben Dooksf8d79e72008-06-24 22:16:02 +01001157 dev_warn(db->dev, "WARNING: no IRQ resource flags set.\n");
Ben Dooks6ff4ff02008-06-24 22:16:07 +01001158
Ben Dooksf8d79e72008-06-24 22:16:02 +01001159 irqflags |= IRQF_SHARED;
1160
Henry Nestler108f5182011-02-22 11:29:42 +00001161 /* GPIO0 on pre-activate PHY, Reg 1F is not set by reset */
1162 iow(db, DM9000_GPR, 0); /* REG_1F bit0 activate phyxcer */
1163 mdelay(1); /* delay needs by DM9000B */
1164
Ben Dooksf8d79e72008-06-24 22:16:02 +01001165 /* Initialize DM9000 board */
1166 dm9000_reset(db);
1167 dm9000_init_dm9000(dev);
1168
Mark Brown6979d5d2011-06-01 10:18:09 +00001169 if (request_irq(dev->irq, dm9000_interrupt, irqflags, dev->name, dev))
1170 return -EAGAIN;
1171
Ben Dooksf8d79e72008-06-24 22:16:02 +01001172 /* Init driver variable */
1173 db->dbug_cnt = 0;
1174
1175 mii_check_media(&db->mii, netif_msg_link(db), 1);
1176 netif_start_queue(dev);
1177
1178 dm9000_schedule_poll(db);
1179
1180 return 0;
1181}
1182
1183/*
1184 * Sleep, either by using msleep() or if we are suspending, then
1185 * use mdelay() to sleep.
1186 */
1187static void dm9000_msleep(board_info_t *db, unsigned int ms)
1188{
1189 if (db->in_suspend)
1190 mdelay(ms);
1191 else
1192 msleep(ms);
1193}
1194
1195/*
1196 * Read a word from phyxcer
1197 */
1198static int
1199dm9000_phy_read(struct net_device *dev, int phy_reg_unused, int reg)
1200{
Wang Chen4cf16532008-11-12 23:38:14 -08001201 board_info_t *db = netdev_priv(dev);
Ben Dooksf8d79e72008-06-24 22:16:02 +01001202 unsigned long flags;
1203 unsigned int reg_save;
1204 int ret;
1205
1206 mutex_lock(&db->addr_lock);
1207
1208 spin_lock_irqsave(&db->lock,flags);
1209
1210 /* Save previous register address */
1211 reg_save = readb(db->io_addr);
1212
1213 /* Fill the phyxcer register into REG_0C */
1214 iow(db, DM9000_EPAR, DM9000_PHY | reg);
1215
Ben Dooksf8e5e772008-07-17 20:29:13 +01001216 iow(db, DM9000_EPCR, EPCR_ERPRR | EPCR_EPOS); /* Issue phyxcer read command */
Ben Dooksf8d79e72008-06-24 22:16:02 +01001217
1218 writeb(reg_save, db->io_addr);
1219 spin_unlock_irqrestore(&db->lock,flags);
1220
1221 dm9000_msleep(db, 1); /* Wait read complete */
1222
1223 spin_lock_irqsave(&db->lock,flags);
1224 reg_save = readb(db->io_addr);
1225
1226 iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer read command */
1227
1228 /* The read data keeps on REG_0D & REG_0E */
1229 ret = (ior(db, DM9000_EPDRH) << 8) | ior(db, DM9000_EPDRL);
1230
1231 /* restore the previous address */
1232 writeb(reg_save, db->io_addr);
1233 spin_unlock_irqrestore(&db->lock,flags);
1234
1235 mutex_unlock(&db->addr_lock);
1236
1237 dm9000_dbg(db, 5, "phy_read[%02x] -> %04x\n", reg, ret);
1238 return ret;
1239}
1240
1241/*
1242 * Write a word to phyxcer
1243 */
1244static void
1245dm9000_phy_write(struct net_device *dev,
1246 int phyaddr_unused, int reg, int value)
1247{
Wang Chen4cf16532008-11-12 23:38:14 -08001248 board_info_t *db = netdev_priv(dev);
Ben Dooksf8d79e72008-06-24 22:16:02 +01001249 unsigned long flags;
1250 unsigned long reg_save;
1251
1252 dm9000_dbg(db, 5, "phy_write[%02x] = %04x\n", reg, value);
1253 mutex_lock(&db->addr_lock);
1254
1255 spin_lock_irqsave(&db->lock,flags);
1256
1257 /* Save previous register address */
1258 reg_save = readb(db->io_addr);
1259
1260 /* Fill the phyxcer register into REG_0C */
1261 iow(db, DM9000_EPAR, DM9000_PHY | reg);
1262
1263 /* Fill the written data into REG_0D & REG_0E */
1264 iow(db, DM9000_EPDRL, value);
1265 iow(db, DM9000_EPDRH, value >> 8);
1266
Ben Dooksf8e5e772008-07-17 20:29:13 +01001267 iow(db, DM9000_EPCR, EPCR_EPOS | EPCR_ERPRW); /* Issue phyxcer write command */
Ben Dooksf8d79e72008-06-24 22:16:02 +01001268
1269 writeb(reg_save, db->io_addr);
1270 spin_unlock_irqrestore(&db->lock, flags);
1271
1272 dm9000_msleep(db, 1); /* Wait write complete */
1273
1274 spin_lock_irqsave(&db->lock,flags);
1275 reg_save = readb(db->io_addr);
1276
1277 iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer write command */
1278
1279 /* restore the previous address */
1280 writeb(reg_save, db->io_addr);
1281
1282 spin_unlock_irqrestore(&db->lock, flags);
1283 mutex_unlock(&db->addr_lock);
1284}
1285
1286static void
1287dm9000_shutdown(struct net_device *dev)
1288{
Wang Chen4cf16532008-11-12 23:38:14 -08001289 board_info_t *db = netdev_priv(dev);
Ben Dooksf8d79e72008-06-24 22:16:02 +01001290
1291 /* RESET device */
1292 dm9000_phy_write(dev, 0, MII_BMCR, BMCR_RESET); /* PHY RESET */
1293 iow(db, DM9000_GPR, 0x01); /* Power-Down PHY */
1294 iow(db, DM9000_IMR, IMR_PAR); /* Disable all interrupt */
1295 iow(db, DM9000_RCR, 0x00); /* Disable RX */
1296}
1297
1298/*
1299 * Stop the interface.
1300 * The interface is stopped when it is brought.
1301 */
1302static int
1303dm9000_stop(struct net_device *ndev)
1304{
Wang Chen4cf16532008-11-12 23:38:14 -08001305 board_info_t *db = netdev_priv(ndev);
Ben Dooksf8d79e72008-06-24 22:16:02 +01001306
1307 if (netif_msg_ifdown(db))
1308 dev_dbg(db->dev, "shutting down %s\n", ndev->name);
1309
1310 cancel_delayed_work_sync(&db->phy_poll);
1311
1312 netif_stop_queue(ndev);
1313 netif_carrier_off(ndev);
1314
1315 /* free interrupt */
1316 free_irq(ndev->irq, ndev);
1317
1318 dm9000_shutdown(ndev);
1319
1320 return 0;
1321}
1322
Alexander Beregalovd88106b2009-04-15 12:52:37 +00001323static const struct net_device_ops dm9000_netdev_ops = {
1324 .ndo_open = dm9000_open,
1325 .ndo_stop = dm9000_stop,
1326 .ndo_start_xmit = dm9000_start_xmit,
1327 .ndo_tx_timeout = dm9000_timeout,
1328 .ndo_set_multicast_list = dm9000_hash_table,
1329 .ndo_do_ioctl = dm9000_ioctl,
1330 .ndo_change_mtu = eth_change_mtu,
Michał Mirosławc88fcb32011-04-15 04:50:49 +00001331 .ndo_set_features = dm9000_set_features,
Alexander Beregalovd88106b2009-04-15 12:52:37 +00001332 .ndo_validate_addr = eth_validate_addr,
1333 .ndo_set_mac_address = eth_mac_addr,
1334#ifdef CONFIG_NET_POLL_CONTROLLER
1335 .ndo_poll_controller = dm9000_poll_controller,
1336#endif
1337};
1338
Sascha Hauera1365272005-05-05 15:14:15 -07001339/*
1340 * Search DM9000 board, allocate space and register it
1341 */
Enrico Scholze21fd4f2008-05-08 11:33:03 +01001342static int __devinit
Russell King3ae5eae2005-11-09 22:32:44 +00001343dm9000_probe(struct platform_device *pdev)
Sascha Hauera1365272005-05-05 15:14:15 -07001344{
Sascha Hauera1365272005-05-05 15:14:15 -07001345 struct dm9000_plat_data *pdata = pdev->dev.platform_data;
1346 struct board_info *db; /* Point a board information structure */
1347 struct net_device *ndev;
Ben Dooks179c7432008-02-05 00:02:23 +00001348 const unsigned char *mac_src;
Sascha Hauera1365272005-05-05 15:14:15 -07001349 int ret = 0;
1350 int iosize;
1351 int i;
1352 u32 id_val;
1353
Sascha Hauera1365272005-05-05 15:14:15 -07001354 /* Init network device */
Ben Dooksf8d79e72008-06-24 22:16:02 +01001355 ndev = alloc_etherdev(sizeof(struct board_info));
Sascha Hauera1365272005-05-05 15:14:15 -07001356 if (!ndev) {
Ben Dooksa76836f2008-02-05 00:02:02 +00001357 dev_err(&pdev->dev, "could not allocate device.\n");
Sascha Hauera1365272005-05-05 15:14:15 -07001358 return -ENOMEM;
1359 }
1360
Russell King3ae5eae2005-11-09 22:32:44 +00001361 SET_NETDEV_DEV(ndev, &pdev->dev);
Sascha Hauera1365272005-05-05 15:14:15 -07001362
Enrico Scholz37d5dca2008-05-08 11:35:13 +01001363 dev_dbg(&pdev->dev, "dm9000_probe()\n");
Sascha Hauera1365272005-05-05 15:14:15 -07001364
1365 /* setup board info structure */
Wang Chen4cf16532008-11-12 23:38:14 -08001366 db = netdev_priv(ndev);
Sascha Hauera1365272005-05-05 15:14:15 -07001367
Ben Dooksa76836f2008-02-05 00:02:02 +00001368 db->dev = &pdev->dev;
Ben Dooks8f5bf5f2008-05-08 11:36:42 +01001369 db->ndev = ndev;
Ben Dooksa76836f2008-02-05 00:02:02 +00001370
Ben Dooks9ef9ac52005-07-23 17:25:18 +01001371 spin_lock_init(&db->lock);
Ben Dooks9a2f0372008-02-05 00:02:10 +00001372 mutex_init(&db->addr_lock);
Ben Dooks9ef9ac52005-07-23 17:25:18 +01001373
Ben Dooks8f5bf5f2008-05-08 11:36:42 +01001374 INIT_DELAYED_WORK(&db->phy_poll, dm9000_poll_work);
1375
Laurent Pinchart08c3f572008-06-24 22:15:57 +01001376 db->addr_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1377 db->data_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1378 db->irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1379
1380 if (db->addr_res == NULL || db->data_res == NULL ||
1381 db->irq_res == NULL) {
1382 dev_err(db->dev, "insufficient resources\n");
1383 ret = -ENOENT;
1384 goto out;
1385 }
1386
Ben Dooksc029f442009-11-10 07:22:24 +00001387 db->irq_wake = platform_get_irq(pdev, 1);
1388 if (db->irq_wake >= 0) {
1389 dev_dbg(db->dev, "wakeup irq %d\n", db->irq_wake);
1390
1391 ret = request_irq(db->irq_wake, dm9000_wol_interrupt,
1392 IRQF_SHARED, dev_name(db->dev), ndev);
1393 if (ret) {
1394 dev_err(db->dev, "cannot get wakeup irq (%d)\n", ret);
1395 } else {
1396
1397 /* test to see if irq is really wakeup capable */
Thomas Gleixnerdced35a2011-03-28 17:49:12 +02001398 ret = irq_set_irq_wake(db->irq_wake, 1);
Ben Dooksc029f442009-11-10 07:22:24 +00001399 if (ret) {
1400 dev_err(db->dev, "irq %d cannot set wakeup (%d)\n",
1401 db->irq_wake, ret);
1402 ret = 0;
1403 } else {
Thomas Gleixnerdced35a2011-03-28 17:49:12 +02001404 irq_set_irq_wake(db->irq_wake, 0);
Ben Dooksc029f442009-11-10 07:22:24 +00001405 db->wake_supported = 1;
1406 }
1407 }
1408 }
1409
Tobias Klauserec282e92009-09-09 01:07:43 +00001410 iosize = resource_size(db->addr_res);
Laurent Pinchart08c3f572008-06-24 22:15:57 +01001411 db->addr_req = request_mem_region(db->addr_res->start, iosize,
1412 pdev->name);
1413
1414 if (db->addr_req == NULL) {
1415 dev_err(db->dev, "cannot claim address reg area\n");
1416 ret = -EIO;
1417 goto out;
1418 }
1419
1420 db->io_addr = ioremap(db->addr_res->start, iosize);
1421
1422 if (db->io_addr == NULL) {
1423 dev_err(db->dev, "failed to ioremap address reg\n");
1424 ret = -EINVAL;
1425 goto out;
1426 }
1427
Tobias Klauserec282e92009-09-09 01:07:43 +00001428 iosize = resource_size(db->data_res);
Laurent Pinchart08c3f572008-06-24 22:15:57 +01001429 db->data_req = request_mem_region(db->data_res->start, iosize,
1430 pdev->name);
1431
1432 if (db->data_req == NULL) {
1433 dev_err(db->dev, "cannot claim data reg area\n");
1434 ret = -EIO;
1435 goto out;
1436 }
1437
1438 db->io_data = ioremap(db->data_res->start, iosize);
1439
1440 if (db->io_data == NULL) {
1441 dev_err(db->dev, "failed to ioremap data reg\n");
1442 ret = -EINVAL;
1443 goto out;
1444 }
1445
1446 /* fill in parameters for net-dev structure */
1447 ndev->base_addr = (unsigned long)db->io_addr;
1448 ndev->irq = db->irq_res->start;
1449
1450 /* ensure at least we have a default set of IO routines */
1451 dm9000_set_io(db, iosize);
1452
Sascha Hauera1365272005-05-05 15:14:15 -07001453 /* check to see if anything is being over-ridden */
1454 if (pdata != NULL) {
1455 /* check to see if the driver wants to over-ride the
1456 * default IO width */
1457
1458 if (pdata->flags & DM9000_PLATF_8BITONLY)
1459 dm9000_set_io(db, 1);
1460
1461 if (pdata->flags & DM9000_PLATF_16BITONLY)
1462 dm9000_set_io(db, 2);
1463
1464 if (pdata->flags & DM9000_PLATF_32BITONLY)
1465 dm9000_set_io(db, 4);
1466
1467 /* check to see if there are any IO routine
1468 * over-rides */
1469
1470 if (pdata->inblk != NULL)
1471 db->inblk = pdata->inblk;
1472
1473 if (pdata->outblk != NULL)
1474 db->outblk = pdata->outblk;
1475
1476 if (pdata->dumpblk != NULL)
1477 db->dumpblk = pdata->dumpblk;
Ben Dooks33ba5092008-02-05 00:02:01 +00001478
1479 db->flags = pdata->flags;
Sascha Hauera1365272005-05-05 15:14:15 -07001480 }
1481
Ben Dooksf8dd0ec2008-06-24 22:16:04 +01001482#ifdef CONFIG_DM9000_FORCE_SIMPLE_PHY_POLL
1483 db->flags |= DM9000_PLATF_SIMPLE_PHY;
1484#endif
1485
Sascha Hauera1365272005-05-05 15:14:15 -07001486 dm9000_reset(db);
1487
Ben Dooks59eae1f2008-06-24 22:16:01 +01001488 /* try multiple times, DM9000 sometimes gets the read wrong */
Ben Dooks513b6be2008-02-05 00:02:22 +00001489 for (i = 0; i < 8; i++) {
Sascha Hauera1365272005-05-05 15:14:15 -07001490 id_val = ior(db, DM9000_VIDL);
1491 id_val |= (u32)ior(db, DM9000_VIDH) << 8;
1492 id_val |= (u32)ior(db, DM9000_PIDL) << 16;
1493 id_val |= (u32)ior(db, DM9000_PIDH) << 24;
1494
1495 if (id_val == DM9000_ID)
1496 break;
Ben Dooksa76836f2008-02-05 00:02:02 +00001497 dev_err(db->dev, "read wrong id 0x%08x\n", id_val);
Sascha Hauera1365272005-05-05 15:14:15 -07001498 }
1499
1500 if (id_val != DM9000_ID) {
Ben Dooksa76836f2008-02-05 00:02:02 +00001501 dev_err(db->dev, "wrong id: 0x%08x\n", id_val);
Mike Rapoport418d6f82007-10-18 09:23:11 +02001502 ret = -ENODEV;
1503 goto out;
Sascha Hauera1365272005-05-05 15:14:15 -07001504 }
1505
Ben Dooks6d406b32008-06-24 22:15:59 +01001506 /* Identify what type of DM9000 we are working on */
1507
1508 id_val = ior(db, DM9000_CHIPR);
1509 dev_dbg(db->dev, "dm9000 revision 0x%02x\n", id_val);
1510
1511 switch (id_val) {
1512 case CHIPR_DM9000A:
1513 db->type = TYPE_DM9000A;
1514 break;
1515 case CHIPR_DM9000B:
1516 db->type = TYPE_DM9000B;
1517 break;
1518 default:
1519 dev_dbg(db->dev, "ID %02x => defaulting to DM9000E\n", id_val);
1520 db->type = TYPE_DM9000E;
1521 }
1522
Yeasah Pell5dcc60b2009-07-06 18:12:33 -07001523 /* dm9000a/b are capable of hardware checksum offload */
1524 if (db->type == TYPE_DM9000A || db->type == TYPE_DM9000B) {
Michał Mirosławc88fcb32011-04-15 04:50:49 +00001525 ndev->hw_features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM;
1526 ndev->features |= ndev->hw_features;
Yeasah Pell5dcc60b2009-07-06 18:12:33 -07001527 }
1528
Sascha Hauera1365272005-05-05 15:14:15 -07001529 /* from this point we assume that we have found a DM9000 */
1530
1531 /* driver system function */
1532 ether_setup(ndev);
1533
Alexander Beregalovd88106b2009-04-15 12:52:37 +00001534 ndev->netdev_ops = &dm9000_netdev_ops;
1535 ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
1536 ndev->ethtool_ops = &dm9000_ethtool_ops;
Sascha Hauera1365272005-05-05 15:14:15 -07001537
Sascha Hauera1365272005-05-05 15:14:15 -07001538 db->msg_enable = NETIF_MSG_LINK;
1539 db->mii.phy_id_mask = 0x1f;
1540 db->mii.reg_num_mask = 0x1f;
1541 db->mii.force_media = 0;
1542 db->mii.full_duplex = 0;
1543 db->mii.dev = ndev;
1544 db->mii.mdio_read = dm9000_phy_read;
1545 db->mii.mdio_write = dm9000_phy_write;
1546
Ben Dooks179c7432008-02-05 00:02:23 +00001547 mac_src = "eeprom";
1548
Ben Dooks86c62fa2008-02-05 00:02:09 +00001549 /* try reading the node address from the attached EEPROM */
1550 for (i = 0; i < 6; i += 2)
1551 dm9000_read_eeprom(db, i / 2, ndev->dev_addr+i);
Sascha Hauera1365272005-05-05 15:14:15 -07001552
Laurent Pinchartfe414242008-07-23 17:41:52 +02001553 if (!is_valid_ether_addr(ndev->dev_addr) && pdata != NULL) {
1554 mac_src = "platform data";
1555 memcpy(ndev->dev_addr, pdata->dev_addr, 6);
1556 }
1557
Ben Dooks5b55dda2006-06-13 23:50:15 +01001558 if (!is_valid_ether_addr(ndev->dev_addr)) {
1559 /* try reading from mac */
Ben Dooksf8d79e72008-06-24 22:16:02 +01001560
Ben Dooks179c7432008-02-05 00:02:23 +00001561 mac_src = "chip";
Ben Dooks5b55dda2006-06-13 23:50:15 +01001562 for (i = 0; i < 6; i++)
1563 ndev->dev_addr[i] = ior(db, i+DM9000_PAR);
1564 }
1565
Ben Dooks85e6b8c2011-02-24 03:17:12 +00001566 if (!is_valid_ether_addr(ndev->dev_addr)) {
Ben Dooksa76836f2008-02-05 00:02:02 +00001567 dev_warn(db->dev, "%s: Invalid ethernet MAC address. Please "
1568 "set using ifconfig\n", ndev->name);
Sascha Hauera1365272005-05-05 15:14:15 -07001569
Ben Dooks85e6b8c2011-02-24 03:17:12 +00001570 random_ether_addr(ndev->dev_addr);
1571 mac_src = "random";
1572 }
1573
1574
Russell King3ae5eae2005-11-09 22:32:44 +00001575 platform_set_drvdata(pdev, ndev);
Sascha Hauera1365272005-05-05 15:14:15 -07001576 ret = register_netdev(ndev);
1577
Johannes Berge1749612008-10-27 15:59:26 -07001578 if (ret == 0)
1579 printk(KERN_INFO "%s: dm9000%c at %p,%p IRQ %d MAC: %pM (%s)\n",
Ben Dooks6d406b32008-06-24 22:15:59 +01001580 ndev->name, dm9000_type_to_char(db->type),
1581 db->io_addr, db->io_data, ndev->irq,
Johannes Berge1749612008-10-27 15:59:26 -07001582 ndev->dev_addr, mac_src);
Sascha Hauera1365272005-05-05 15:14:15 -07001583 return 0;
1584
Mike Rapoport418d6f82007-10-18 09:23:11 +02001585out:
Ben Dooksa76836f2008-02-05 00:02:02 +00001586 dev_err(db->dev, "not found (%d).\n", ret);
Sascha Hauera1365272005-05-05 15:14:15 -07001587
1588 dm9000_release_board(pdev, db);
Ben Dooks9fd9f9b2007-05-07 11:13:25 +00001589 free_netdev(ndev);
Sascha Hauera1365272005-05-05 15:14:15 -07001590
1591 return ret;
1592}
1593
Sascha Hauera1365272005-05-05 15:14:15 -07001594static int
Mike Rapoport69222e22009-07-21 12:37:18 -07001595dm9000_drv_suspend(struct device *dev)
Sascha Hauera1365272005-05-05 15:14:15 -07001596{
Mike Rapoport69222e22009-07-21 12:37:18 -07001597 struct platform_device *pdev = to_platform_device(dev);
1598 struct net_device *ndev = platform_get_drvdata(pdev);
Ben Dooks321f69a2008-02-05 00:02:08 +00001599 board_info_t *db;
Sascha Hauera1365272005-05-05 15:14:15 -07001600
Russell King9480e302005-10-28 09:52:56 -07001601 if (ndev) {
Wang Chen4cf16532008-11-12 23:38:14 -08001602 db = netdev_priv(ndev);
Ben Dooks321f69a2008-02-05 00:02:08 +00001603 db->in_suspend = 1;
1604
Ben Dooksc029f442009-11-10 07:22:24 +00001605 if (!netif_running(ndev))
1606 return 0;
1607
1608 netif_device_detach(ndev);
1609
1610 /* only shutdown if not using WoL */
1611 if (!db->wake_state)
Sascha Hauera1365272005-05-05 15:14:15 -07001612 dm9000_shutdown(ndev);
Sascha Hauera1365272005-05-05 15:14:15 -07001613 }
1614 return 0;
1615}
1616
1617static int
Mike Rapoport69222e22009-07-21 12:37:18 -07001618dm9000_drv_resume(struct device *dev)
Sascha Hauera1365272005-05-05 15:14:15 -07001619{
Mike Rapoport69222e22009-07-21 12:37:18 -07001620 struct platform_device *pdev = to_platform_device(dev);
1621 struct net_device *ndev = platform_get_drvdata(pdev);
Wang Chen4cf16532008-11-12 23:38:14 -08001622 board_info_t *db = netdev_priv(ndev);
Sascha Hauera1365272005-05-05 15:14:15 -07001623
Russell King9480e302005-10-28 09:52:56 -07001624 if (ndev) {
Sascha Hauera1365272005-05-05 15:14:15 -07001625 if (netif_running(ndev)) {
Ben Dooksc029f442009-11-10 07:22:24 +00001626 /* reset if we were not in wake mode to ensure if
1627 * the device was powered off it is in a known state */
1628 if (!db->wake_state) {
1629 dm9000_reset(db);
1630 dm9000_init_dm9000(ndev);
1631 }
Sascha Hauera1365272005-05-05 15:14:15 -07001632
1633 netif_device_attach(ndev);
1634 }
Ben Dooks321f69a2008-02-05 00:02:08 +00001635
1636 db->in_suspend = 0;
Sascha Hauera1365272005-05-05 15:14:15 -07001637 }
1638 return 0;
1639}
1640
Alexey Dobriyan47145212009-12-14 18:00:08 -08001641static const struct dev_pm_ops dm9000_drv_pm_ops = {
Mike Rapoport69222e22009-07-21 12:37:18 -07001642 .suspend = dm9000_drv_suspend,
1643 .resume = dm9000_drv_resume,
1644};
1645
Enrico Scholze21fd4f2008-05-08 11:33:03 +01001646static int __devexit
Russell King3ae5eae2005-11-09 22:32:44 +00001647dm9000_drv_remove(struct platform_device *pdev)
Sascha Hauera1365272005-05-05 15:14:15 -07001648{
Russell King3ae5eae2005-11-09 22:32:44 +00001649 struct net_device *ndev = platform_get_drvdata(pdev);
Sascha Hauera1365272005-05-05 15:14:15 -07001650
Russell King3ae5eae2005-11-09 22:32:44 +00001651 platform_set_drvdata(pdev, NULL);
Sascha Hauera1365272005-05-05 15:14:15 -07001652
1653 unregister_netdev(ndev);
Joe Perchesece49152010-11-15 11:12:31 +00001654 dm9000_release_board(pdev, netdev_priv(ndev));
Ben Dooks9fd9f9b2007-05-07 11:13:25 +00001655 free_netdev(ndev); /* free device structure */
Sascha Hauera1365272005-05-05 15:14:15 -07001656
Ben Dooksa76836f2008-02-05 00:02:02 +00001657 dev_dbg(&pdev->dev, "released and freed device\n");
Sascha Hauera1365272005-05-05 15:14:15 -07001658 return 0;
1659}
1660
Russell King3ae5eae2005-11-09 22:32:44 +00001661static struct platform_driver dm9000_driver = {
Ben Dooks5d22a312006-06-14 00:09:17 +01001662 .driver = {
1663 .name = "dm9000",
1664 .owner = THIS_MODULE,
Mike Rapoport69222e22009-07-21 12:37:18 -07001665 .pm = &dm9000_drv_pm_ops,
Ben Dooks5d22a312006-06-14 00:09:17 +01001666 },
Sascha Hauera1365272005-05-05 15:14:15 -07001667 .probe = dm9000_probe,
Enrico Scholze21fd4f2008-05-08 11:33:03 +01001668 .remove = __devexit_p(dm9000_drv_remove),
Sascha Hauera1365272005-05-05 15:14:15 -07001669};
1670
1671static int __init
1672dm9000_init(void)
1673{
Ben Dooks7da99852008-02-05 00:02:06 +00001674 printk(KERN_INFO "%s Ethernet Driver, V%s\n", CARDNAME, DRV_VERSION);
Ben Dooks2ae2d772005-07-23 17:29:38 +01001675
Ben Dooks59eae1f2008-06-24 22:16:01 +01001676 return platform_driver_register(&dm9000_driver);
Sascha Hauera1365272005-05-05 15:14:15 -07001677}
1678
1679static void __exit
1680dm9000_cleanup(void)
1681{
Russell King3ae5eae2005-11-09 22:32:44 +00001682 platform_driver_unregister(&dm9000_driver);
Sascha Hauera1365272005-05-05 15:14:15 -07001683}
1684
1685module_init(dm9000_init);
1686module_exit(dm9000_cleanup);
1687
1688MODULE_AUTHOR("Sascha Hauer, Ben Dooks");
1689MODULE_DESCRIPTION("Davicom DM9000 network driver");
1690MODULE_LICENSE("GPL");
Kay Sievers72abb462008-04-18 13:50:44 -07001691MODULE_ALIAS("platform:dm9000");