blob: a9628b62df6fbc5435bd45d73e4aa917521b526f [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
Vladimir Zapolskiy2e025c72011-08-20 14:15:55 -070059/*
60 * Debug messages level
61 */
62static int debug;
63module_param(debug, int, 0644);
64MODULE_PARM_DESC(debug, "dm9000 debug level (0-4)");
65
Ben Dooks9a2f0372008-02-05 00:02:10 +000066/* DM9000 register address locking.
67 *
68 * The DM9000 uses an address register to control where data written
69 * to the data register goes. This means that the address register
70 * must be preserved over interrupts or similar calls.
71 *
72 * During interrupt and other critical calls, a spinlock is used to
73 * protect the system, but the calls themselves save the address
74 * in the address register in case they are interrupting another
75 * access to the device.
76 *
77 * For general accesses a lock is provided so that calls which are
78 * allowed to sleep are serialised so that the address register does
79 * not need to be saved. This lock also serves to serialise access
80 * to the EEPROM and PHY access registers which are shared between
81 * these two devices.
82 */
83
Ben Dooks6d406b32008-06-24 22:15:59 +010084/* The driver supports the original DM9000E, and now the two newer
85 * devices, DM9000A and DM9000B.
86 */
87
88enum dm9000_type {
89 TYPE_DM9000E, /* original DM9000 */
90 TYPE_DM9000A,
91 TYPE_DM9000B
92};
93
Sascha Hauera1365272005-05-05 15:14:15 -070094/* Structure/enum declaration ------------------------------- */
95typedef struct board_info {
96
Ben Dooks59eae1f2008-06-24 22:16:01 +010097 void __iomem *io_addr; /* Register I/O base address */
98 void __iomem *io_data; /* Data I/O address */
99 u16 irq; /* IRQ */
Sascha Hauera1365272005-05-05 15:14:15 -0700100
Ben Dooks59eae1f2008-06-24 22:16:01 +0100101 u16 tx_pkt_cnt;
102 u16 queue_pkt_len;
103 u16 queue_start_addr;
Yeasah Pell5dcc60b2009-07-06 18:12:33 -0700104 u16 queue_ip_summed;
Ben Dooks59eae1f2008-06-24 22:16:01 +0100105 u16 dbug_cnt;
106 u8 io_mode; /* 0:word, 2:byte */
107 u8 phy_addr;
108 u8 imr_all;
109
110 unsigned int flags;
111 unsigned int in_suspend :1;
Ben Dooksc029f442009-11-10 07:22:24 +0000112 unsigned int wake_supported :1;
Sascha Hauera1365272005-05-05 15:14:15 -0700113
Ben Dooks6d406b32008-06-24 22:15:59 +0100114 enum dm9000_type type;
Ben Dooks5b2b4ff2008-02-05 00:02:03 +0000115
Sascha Hauera1365272005-05-05 15:14:15 -0700116 void (*inblk)(void __iomem *port, void *data, int length);
117 void (*outblk)(void __iomem *port, void *data, int length);
118 void (*dumpblk)(void __iomem *port, int length);
119
Ben Dooksa76836f2008-02-05 00:02:02 +0000120 struct device *dev; /* parent device */
121
Sascha Hauera1365272005-05-05 15:14:15 -0700122 struct resource *addr_res; /* resources found */
123 struct resource *data_res;
124 struct resource *addr_req; /* resources requested */
125 struct resource *data_req;
126 struct resource *irq_res;
127
Ben Dooksc029f442009-11-10 07:22:24 +0000128 int irq_wake;
129
Ben Dooks9a2f0372008-02-05 00:02:10 +0000130 struct mutex addr_lock; /* phy and eeprom access lock */
131
Ben Dooks8f5bf5f2008-05-08 11:36:42 +0100132 struct delayed_work phy_poll;
133 struct net_device *ndev;
134
Ben Dooks59eae1f2008-06-24 22:16:01 +0100135 spinlock_t lock;
Sascha Hauera1365272005-05-05 15:14:15 -0700136
137 struct mii_if_info mii;
Ben Dooks59eae1f2008-06-24 22:16:01 +0100138 u32 msg_enable;
Ben Dooksc029f442009-11-10 07:22:24 +0000139 u32 wake_state;
Yeasah Pell5dcc60b2009-07-06 18:12:33 -0700140
Yeasah Pell5dcc60b2009-07-06 18:12:33 -0700141 int ip_summed;
Sascha Hauera1365272005-05-05 15:14:15 -0700142} board_info_t;
143
Ben Dooks5b2b4ff2008-02-05 00:02:03 +0000144/* debug code */
145
146#define dm9000_dbg(db, lev, msg...) do { \
Vladimir Zapolskiy2e025c72011-08-20 14:15:55 -0700147 if ((lev) < debug) { \
Ben Dooks5b2b4ff2008-02-05 00:02:03 +0000148 dev_dbg(db->dev, msg); \
149 } \
150} while (0)
151
Ben Dooks7da99852008-02-05 00:02:06 +0000152static inline board_info_t *to_dm9000_board(struct net_device *dev)
153{
Wang Chen4cf16532008-11-12 23:38:14 -0800154 return netdev_priv(dev);
Ben Dooks7da99852008-02-05 00:02:06 +0000155}
156
Sascha Hauera1365272005-05-05 15:14:15 -0700157/* DM9000 network board routine ---------------------------- */
158
159static void
160dm9000_reset(board_info_t * db)
161{
Ben Dooksa76836f2008-02-05 00:02:02 +0000162 dev_dbg(db->dev, "resetting device\n");
163
Sascha Hauera1365272005-05-05 15:14:15 -0700164 /* RESET device */
165 writeb(DM9000_NCR, db->io_addr);
166 udelay(200);
167 writeb(NCR_RST, db->io_data);
168 udelay(200);
169}
170
171/*
172 * Read a byte from I/O port
173 */
174static u8
175ior(board_info_t * db, int reg)
176{
177 writeb(reg, db->io_addr);
178 return readb(db->io_data);
179}
180
181/*
182 * Write a byte to I/O port
183 */
184
185static void
186iow(board_info_t * db, int reg, int value)
187{
188 writeb(reg, db->io_addr);
189 writeb(value, db->io_data);
190}
191
192/* routines for sending block to chip */
193
194static void dm9000_outblk_8bit(void __iomem *reg, void *data, int count)
195{
196 writesb(reg, data, count);
197}
198
199static void dm9000_outblk_16bit(void __iomem *reg, void *data, int count)
200{
201 writesw(reg, data, (count+1) >> 1);
202}
203
204static void dm9000_outblk_32bit(void __iomem *reg, void *data, int count)
205{
206 writesl(reg, data, (count+3) >> 2);
207}
208
209/* input block from chip to memory */
210
211static void dm9000_inblk_8bit(void __iomem *reg, void *data, int count)
212{
Sascha Hauer5f6b5512005-06-20 15:32:51 -0700213 readsb(reg, data, count);
Sascha Hauera1365272005-05-05 15:14:15 -0700214}
215
216
217static void dm9000_inblk_16bit(void __iomem *reg, void *data, int count)
218{
219 readsw(reg, data, (count+1) >> 1);
220}
221
222static void dm9000_inblk_32bit(void __iomem *reg, void *data, int count)
223{
224 readsl(reg, data, (count+3) >> 2);
225}
226
227/* dump block from chip to null */
228
229static void dm9000_dumpblk_8bit(void __iomem *reg, int count)
230{
231 int i;
232 int tmp;
233
234 for (i = 0; i < count; i++)
235 tmp = readb(reg);
236}
237
238static void dm9000_dumpblk_16bit(void __iomem *reg, int count)
239{
240 int i;
241 int tmp;
242
243 count = (count + 1) >> 1;
244
245 for (i = 0; i < count; i++)
246 tmp = readw(reg);
247}
248
249static void dm9000_dumpblk_32bit(void __iomem *reg, int count)
250{
251 int i;
252 int tmp;
253
254 count = (count + 3) >> 2;
255
256 for (i = 0; i < count; i++)
257 tmp = readl(reg);
258}
259
Joseph CHANGccb926f2013-03-28 23:13:42 +0000260/*
261 * Sleep, either by using msleep() or if we are suspending, then
262 * use mdelay() to sleep.
263 */
264static void dm9000_msleep(board_info_t *db, unsigned int ms)
265{
266 if (db->in_suspend)
267 mdelay(ms);
268 else
269 msleep(ms);
270}
271
272/* Read a word from phyxcer */
273static int
274dm9000_phy_read(struct net_device *dev, int phy_reg_unused, int reg)
275{
276 board_info_t *db = netdev_priv(dev);
277 unsigned long flags;
278 unsigned int reg_save;
279 int ret;
280
281 mutex_lock(&db->addr_lock);
282
283 spin_lock_irqsave(&db->lock, flags);
284
285 /* Save previous register address */
286 reg_save = readb(db->io_addr);
287
288 /* Fill the phyxcer register into REG_0C */
289 iow(db, DM9000_EPAR, DM9000_PHY | reg);
290
291 /* Issue phyxcer read command */
292 iow(db, DM9000_EPCR, EPCR_ERPRR | EPCR_EPOS);
293
294 writeb(reg_save, db->io_addr);
295 spin_unlock_irqrestore(&db->lock, flags);
296
297 dm9000_msleep(db, 1); /* Wait read complete */
298
299 spin_lock_irqsave(&db->lock, flags);
300 reg_save = readb(db->io_addr);
301
302 iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer read command */
303
304 /* The read data keeps on REG_0D & REG_0E */
305 ret = (ior(db, DM9000_EPDRH) << 8) | ior(db, DM9000_EPDRL);
306
307 /* restore the previous address */
308 writeb(reg_save, db->io_addr);
309 spin_unlock_irqrestore(&db->lock, flags);
310
311 mutex_unlock(&db->addr_lock);
312
313 dm9000_dbg(db, 5, "phy_read[%02x] -> %04x\n", reg, ret);
314 return ret;
315}
316
317/* Write a word to phyxcer */
318static void
319dm9000_phy_write(struct net_device *dev,
320 int phyaddr_unused, int reg, int value)
321{
322 board_info_t *db = netdev_priv(dev);
323 unsigned long flags;
324 unsigned long reg_save;
325
326 dm9000_dbg(db, 5, "phy_write[%02x] = %04x\n", reg, value);
327 mutex_lock(&db->addr_lock);
328
329 spin_lock_irqsave(&db->lock, flags);
330
331 /* Save previous register address */
332 reg_save = readb(db->io_addr);
333
334 /* Fill the phyxcer register into REG_0C */
335 iow(db, DM9000_EPAR, DM9000_PHY | reg);
336
337 /* Fill the written data into REG_0D & REG_0E */
338 iow(db, DM9000_EPDRL, value);
339 iow(db, DM9000_EPDRH, value >> 8);
340
341 /* Issue phyxcer write command */
342 iow(db, DM9000_EPCR, EPCR_EPOS | EPCR_ERPRW);
343
344 writeb(reg_save, db->io_addr);
345 spin_unlock_irqrestore(&db->lock, flags);
346
347 dm9000_msleep(db, 1); /* Wait write complete */
348
349 spin_lock_irqsave(&db->lock, flags);
350 reg_save = readb(db->io_addr);
351
352 iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer write command */
353
354 /* restore the previous address */
355 writeb(reg_save, db->io_addr);
356
357 spin_unlock_irqrestore(&db->lock, flags);
358 mutex_unlock(&db->addr_lock);
359}
360
Sascha Hauera1365272005-05-05 15:14:15 -0700361/* dm9000_set_io
362 *
363 * select the specified set of io routines to use with the
364 * device
365 */
366
367static void dm9000_set_io(struct board_info *db, int byte_width)
368{
369 /* use the size of the data resource to work out what IO
370 * routines we want to use
371 */
372
373 switch (byte_width) {
374 case 1:
375 db->dumpblk = dm9000_dumpblk_8bit;
376 db->outblk = dm9000_outblk_8bit;
377 db->inblk = dm9000_inblk_8bit;
378 break;
379
Sascha Hauera1365272005-05-05 15:14:15 -0700380
381 case 3:
Ben Dooksa76836f2008-02-05 00:02:02 +0000382 dev_dbg(db->dev, ": 3 byte IO, falling back to 16bit\n");
383 case 2:
Sascha Hauera1365272005-05-05 15:14:15 -0700384 db->dumpblk = dm9000_dumpblk_16bit;
385 db->outblk = dm9000_outblk_16bit;
386 db->inblk = dm9000_inblk_16bit;
387 break;
388
389 case 4:
390 default:
391 db->dumpblk = dm9000_dumpblk_32bit;
392 db->outblk = dm9000_outblk_32bit;
393 db->inblk = dm9000_inblk_32bit;
394 break;
395 }
396}
397
Ben Dooks8f5bf5f2008-05-08 11:36:42 +0100398static void dm9000_schedule_poll(board_info_t *db)
399{
Ben Dooks6d406b32008-06-24 22:15:59 +0100400 if (db->type == TYPE_DM9000E)
401 schedule_delayed_work(&db->phy_poll, HZ * 2);
Ben Dooks8f5bf5f2008-05-08 11:36:42 +0100402}
Sascha Hauera1365272005-05-05 15:14:15 -0700403
Ben Dooksf42d8ae2008-02-05 00:02:21 +0000404static int dm9000_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
405{
406 board_info_t *dm = to_dm9000_board(dev);
407
408 if (!netif_running(dev))
409 return -EINVAL;
410
411 return generic_mii_ioctl(&dm->mii, if_mii(req), cmd, NULL);
412}
413
Ben Dooksf8d79e72008-06-24 22:16:02 +0100414static unsigned int
415dm9000_read_locked(board_info_t *db, int reg)
416{
417 unsigned long flags;
418 unsigned int ret;
419
420 spin_lock_irqsave(&db->lock, flags);
421 ret = ior(db, reg);
422 spin_unlock_irqrestore(&db->lock, flags);
423
424 return ret;
425}
426
427static int dm9000_wait_eeprom(board_info_t *db)
428{
429 unsigned int status;
430 int timeout = 8; /* wait max 8msec */
431
432 /* The DM9000 data sheets say we should be able to
433 * poll the ERRE bit in EPCR to wait for the EEPROM
434 * operation. From testing several chips, this bit
435 * does not seem to work.
436 *
437 * We attempt to use the bit, but fall back to the
438 * timeout (which is why we do not return an error
439 * on expiry) to say that the EEPROM operation has
440 * completed.
441 */
442
443 while (1) {
444 status = dm9000_read_locked(db, DM9000_EPCR);
445
446 if ((status & EPCR_ERRE) == 0)
447 break;
448
Ben Dooks2fcf06c2008-06-24 22:16:05 +0100449 msleep(1);
450
Ben Dooksf8d79e72008-06-24 22:16:02 +0100451 if (timeout-- < 0) {
452 dev_dbg(db->dev, "timeout waiting EEPROM\n");
453 break;
454 }
455 }
456
457 return 0;
458}
459
460/*
461 * Read a word data from EEPROM
462 */
463static void
464dm9000_read_eeprom(board_info_t *db, int offset, u8 *to)
465{
466 unsigned long flags;
467
468 if (db->flags & DM9000_PLATF_NO_EEPROM) {
469 to[0] = 0xff;
470 to[1] = 0xff;
471 return;
472 }
473
474 mutex_lock(&db->addr_lock);
475
476 spin_lock_irqsave(&db->lock, flags);
477
478 iow(db, DM9000_EPAR, offset);
479 iow(db, DM9000_EPCR, EPCR_ERPRR);
480
481 spin_unlock_irqrestore(&db->lock, flags);
482
483 dm9000_wait_eeprom(db);
484
485 /* delay for at-least 150uS */
486 msleep(1);
487
488 spin_lock_irqsave(&db->lock, flags);
489
490 iow(db, DM9000_EPCR, 0x0);
491
492 to[0] = ior(db, DM9000_EPDRL);
493 to[1] = ior(db, DM9000_EPDRH);
494
495 spin_unlock_irqrestore(&db->lock, flags);
496
497 mutex_unlock(&db->addr_lock);
498}
499
500/*
501 * Write a word data to SROM
502 */
503static void
504dm9000_write_eeprom(board_info_t *db, int offset, u8 *data)
505{
506 unsigned long flags;
507
508 if (db->flags & DM9000_PLATF_NO_EEPROM)
509 return;
510
511 mutex_lock(&db->addr_lock);
512
513 spin_lock_irqsave(&db->lock, flags);
514 iow(db, DM9000_EPAR, offset);
515 iow(db, DM9000_EPDRH, data[1]);
516 iow(db, DM9000_EPDRL, data[0]);
517 iow(db, DM9000_EPCR, EPCR_WEP | EPCR_ERPRW);
518 spin_unlock_irqrestore(&db->lock, flags);
519
520 dm9000_wait_eeprom(db);
521
522 mdelay(1); /* wait at least 150uS to clear */
523
524 spin_lock_irqsave(&db->lock, flags);
525 iow(db, DM9000_EPCR, 0);
526 spin_unlock_irqrestore(&db->lock, flags);
527
528 mutex_unlock(&db->addr_lock);
529}
530
Ben Dooks7da99852008-02-05 00:02:06 +0000531/* ethtool ops */
532
533static void dm9000_get_drvinfo(struct net_device *dev,
534 struct ethtool_drvinfo *info)
535{
536 board_info_t *dm = to_dm9000_board(dev);
537
538 strcpy(info->driver, CARDNAME);
539 strcpy(info->version, DRV_VERSION);
540 strcpy(info->bus_info, to_platform_device(dm->dev)->name);
541}
542
Ben Dookse662ee02008-02-05 00:02:12 +0000543static u32 dm9000_get_msglevel(struct net_device *dev)
544{
545 board_info_t *dm = to_dm9000_board(dev);
546
547 return dm->msg_enable;
548}
549
550static void dm9000_set_msglevel(struct net_device *dev, u32 value)
551{
552 board_info_t *dm = to_dm9000_board(dev);
553
554 dm->msg_enable = value;
555}
556
Ben Dooks7da99852008-02-05 00:02:06 +0000557static int dm9000_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
558{
559 board_info_t *dm = to_dm9000_board(dev);
Ben Dooks7da99852008-02-05 00:02:06 +0000560
Ben Dooks7da99852008-02-05 00:02:06 +0000561 mii_ethtool_gset(&dm->mii, cmd);
Ben Dooks7da99852008-02-05 00:02:06 +0000562 return 0;
563}
564
565static int dm9000_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
566{
567 board_info_t *dm = to_dm9000_board(dev);
Ben Dooks7da99852008-02-05 00:02:06 +0000568
Ben Dooks9a2f0372008-02-05 00:02:10 +0000569 return mii_ethtool_sset(&dm->mii, cmd);
Ben Dooks7da99852008-02-05 00:02:06 +0000570}
571
572static int dm9000_nway_reset(struct net_device *dev)
573{
574 board_info_t *dm = to_dm9000_board(dev);
575 return mii_nway_restart(&dm->mii);
576}
577
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000578static int dm9000_set_features(struct net_device *dev,
579 netdev_features_t features)
Yeasah Pell5dcc60b2009-07-06 18:12:33 -0700580{
581 board_info_t *dm = to_dm9000_board(dev);
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000582 netdev_features_t changed = dev->features ^ features;
Baruch Siach380fefb2010-05-17 17:45:48 -0700583 unsigned long flags;
Michał Mirosławc88fcb32011-04-15 04:50:49 +0000584
585 if (!(changed & NETIF_F_RXCSUM))
586 return 0;
Baruch Siach380fefb2010-05-17 17:45:48 -0700587
588 spin_lock_irqsave(&dm->lock, flags);
Michał Mirosławc88fcb32011-04-15 04:50:49 +0000589 iow(dm, DM9000_RCSR, (features & NETIF_F_RXCSUM) ? RCSR_CSUM : 0);
Baruch Siach380fefb2010-05-17 17:45:48 -0700590 spin_unlock_irqrestore(&dm->lock, flags);
591
Michał Mirosławc88fcb32011-04-15 04:50:49 +0000592 return 0;
Yeasah Pell5dcc60b2009-07-06 18:12:33 -0700593}
594
Ben Dooks7da99852008-02-05 00:02:06 +0000595static u32 dm9000_get_link(struct net_device *dev)
596{
597 board_info_t *dm = to_dm9000_board(dev);
Ben Dooksaa1eb452008-06-24 22:16:03 +0100598 u32 ret;
599
600 if (dm->flags & DM9000_PLATF_EXT_PHY)
601 ret = mii_link_ok(&dm->mii);
602 else
603 ret = dm9000_read_locked(dm, DM9000_NSR) & NSR_LINKST ? 1 : 0;
604
605 return ret;
Ben Dooks7da99852008-02-05 00:02:06 +0000606}
607
Ben Dooks29d52e52008-02-05 00:02:11 +0000608#define DM_EEPROM_MAGIC (0x444D394B)
609
610static int dm9000_get_eeprom_len(struct net_device *dev)
611{
612 return 128;
613}
614
615static int dm9000_get_eeprom(struct net_device *dev,
616 struct ethtool_eeprom *ee, u8 *data)
617{
618 board_info_t *dm = to_dm9000_board(dev);
619 int offset = ee->offset;
620 int len = ee->len;
621 int i;
622
623 /* EEPROM access is aligned to two bytes */
624
625 if ((len & 1) != 0 || (offset & 1) != 0)
626 return -EINVAL;
627
Ben Dooksbb44fb702008-02-05 00:02:20 +0000628 if (dm->flags & DM9000_PLATF_NO_EEPROM)
629 return -ENOENT;
630
Ben Dooks29d52e52008-02-05 00:02:11 +0000631 ee->magic = DM_EEPROM_MAGIC;
632
633 for (i = 0; i < len; i += 2)
634 dm9000_read_eeprom(dm, (offset + i) / 2, data + i);
635
636 return 0;
637}
638
639static int dm9000_set_eeprom(struct net_device *dev,
640 struct ethtool_eeprom *ee, u8 *data)
641{
642 board_info_t *dm = to_dm9000_board(dev);
643 int offset = ee->offset;
644 int len = ee->len;
Ben Dooks40d15cd2011-06-10 00:50:32 +0000645 int done;
Ben Dooks29d52e52008-02-05 00:02:11 +0000646
647 /* EEPROM access is aligned to two bytes */
648
Ben Dooksbb44fb702008-02-05 00:02:20 +0000649 if (dm->flags & DM9000_PLATF_NO_EEPROM)
650 return -ENOENT;
651
Ben Dooks29d52e52008-02-05 00:02:11 +0000652 if (ee->magic != DM_EEPROM_MAGIC)
653 return -EINVAL;
654
Ben Dooks40d15cd2011-06-10 00:50:32 +0000655 while (len > 0) {
656 if (len & 1 || offset & 1) {
657 int which = offset & 1;
658 u8 tmp[2];
659
660 dm9000_read_eeprom(dm, offset / 2, tmp);
661 tmp[which] = *data;
662 dm9000_write_eeprom(dm, offset / 2, tmp);
663
664 done = 1;
665 } else {
666 dm9000_write_eeprom(dm, offset / 2, data);
667 done = 2;
668 }
669
670 data += done;
671 offset += done;
672 len -= done;
673 }
Ben Dooks29d52e52008-02-05 00:02:11 +0000674
675 return 0;
676}
677
Ben Dooksc029f442009-11-10 07:22:24 +0000678static void dm9000_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
679{
680 board_info_t *dm = to_dm9000_board(dev);
681
682 memset(w, 0, sizeof(struct ethtool_wolinfo));
683
684 /* note, we could probably support wake-phy too */
685 w->supported = dm->wake_supported ? WAKE_MAGIC : 0;
686 w->wolopts = dm->wake_state;
687}
688
689static int dm9000_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
690{
691 board_info_t *dm = to_dm9000_board(dev);
692 unsigned long flags;
693 u32 opts = w->wolopts;
694 u32 wcr = 0;
695
696 if (!dm->wake_supported)
697 return -EOPNOTSUPP;
698
699 if (opts & ~WAKE_MAGIC)
700 return -EINVAL;
701
702 if (opts & WAKE_MAGIC)
703 wcr |= WCR_MAGICEN;
704
705 mutex_lock(&dm->addr_lock);
706
707 spin_lock_irqsave(&dm->lock, flags);
708 iow(dm, DM9000_WCR, wcr);
709 spin_unlock_irqrestore(&dm->lock, flags);
710
711 mutex_unlock(&dm->addr_lock);
712
713 if (dm->wake_state != opts) {
714 /* change in wol state, update IRQ state */
715
716 if (!dm->wake_state)
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200717 irq_set_irq_wake(dm->irq_wake, 1);
Mark Brown83b98fb2011-11-21 07:51:56 +0000718 else if (dm->wake_state && !opts)
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200719 irq_set_irq_wake(dm->irq_wake, 0);
Ben Dooksc029f442009-11-10 07:22:24 +0000720 }
721
722 dm->wake_state = opts;
723 return 0;
724}
725
Ben Dooks7da99852008-02-05 00:02:06 +0000726static const struct ethtool_ops dm9000_ethtool_ops = {
727 .get_drvinfo = dm9000_get_drvinfo,
728 .get_settings = dm9000_get_settings,
729 .set_settings = dm9000_set_settings,
Ben Dookse662ee02008-02-05 00:02:12 +0000730 .get_msglevel = dm9000_get_msglevel,
731 .set_msglevel = dm9000_set_msglevel,
Ben Dooks7da99852008-02-05 00:02:06 +0000732 .nway_reset = dm9000_nway_reset,
733 .get_link = dm9000_get_link,
Ben Dooksc029f442009-11-10 07:22:24 +0000734 .get_wol = dm9000_get_wol,
735 .set_wol = dm9000_set_wol,
Ben Dooks29d52e52008-02-05 00:02:11 +0000736 .get_eeprom_len = dm9000_get_eeprom_len,
737 .get_eeprom = dm9000_get_eeprom,
738 .set_eeprom = dm9000_set_eeprom,
Ben Dooks7da99852008-02-05 00:02:06 +0000739};
740
Ben Dooksf8dd0ec2008-06-24 22:16:04 +0100741static void dm9000_show_carrier(board_info_t *db,
742 unsigned carrier, unsigned nsr)
743{
744 struct net_device *ndev = db->ndev;
745 unsigned ncr = dm9000_read_locked(db, DM9000_NCR);
746
747 if (carrier)
748 dev_info(db->dev, "%s: link up, %dMbps, %s-duplex, no LPA\n",
749 ndev->name, (nsr & NSR_SPEED) ? 10 : 100,
750 (ncr & NCR_FDX) ? "full" : "half");
751 else
752 dev_info(db->dev, "%s: link down\n", ndev->name);
753}
754
Ben Dooks8f5bf5f2008-05-08 11:36:42 +0100755static void
756dm9000_poll_work(struct work_struct *w)
757{
Jean Delvarebf6aede2009-04-02 16:56:54 -0700758 struct delayed_work *dw = to_delayed_work(w);
Ben Dooks8f5bf5f2008-05-08 11:36:42 +0100759 board_info_t *db = container_of(dw, board_info_t, phy_poll);
Ben Dooksf8dd0ec2008-06-24 22:16:04 +0100760 struct net_device *ndev = db->ndev;
Ben Dooks8f5bf5f2008-05-08 11:36:42 +0100761
Ben Dooksf8dd0ec2008-06-24 22:16:04 +0100762 if (db->flags & DM9000_PLATF_SIMPLE_PHY &&
763 !(db->flags & DM9000_PLATF_EXT_PHY)) {
764 unsigned nsr = dm9000_read_locked(db, DM9000_NSR);
765 unsigned old_carrier = netif_carrier_ok(ndev) ? 1 : 0;
766 unsigned new_carrier;
767
768 new_carrier = (nsr & NSR_LINKST) ? 1 : 0;
769
770 if (old_carrier != new_carrier) {
771 if (netif_msg_link(db))
772 dm9000_show_carrier(db, new_carrier, nsr);
773
774 if (!new_carrier)
775 netif_carrier_off(ndev);
776 else
777 netif_carrier_on(ndev);
778 }
779 } else
780 mii_check_media(&db->mii, netif_msg_link(db), 0);
Ben Dooks8f5bf5f2008-05-08 11:36:42 +0100781
Ben Dooksf8dd0ec2008-06-24 22:16:04 +0100782 if (netif_running(ndev))
Ben Dooks8f5bf5f2008-05-08 11:36:42 +0100783 dm9000_schedule_poll(db);
784}
Ben Dooks7da99852008-02-05 00:02:06 +0000785
Sascha Hauera1365272005-05-05 15:14:15 -0700786/* dm9000_release_board
787 *
788 * release a board, and any mapped resources
789 */
790
791static void
792dm9000_release_board(struct platform_device *pdev, struct board_info *db)
793{
Sascha Hauera1365272005-05-05 15:14:15 -0700794 /* unmap our resources */
795
796 iounmap(db->io_addr);
797 iounmap(db->io_data);
798
799 /* release the resources */
800
Ben Dooks9088fa4f2008-06-24 22:16:00 +0100801 release_resource(db->data_req);
802 kfree(db->data_req);
Sascha Hauera1365272005-05-05 15:14:15 -0700803
Ben Dooks9088fa4f2008-06-24 22:16:00 +0100804 release_resource(db->addr_req);
805 kfree(db->addr_req);
Sascha Hauera1365272005-05-05 15:14:15 -0700806}
807
Ben Dooks6d406b32008-06-24 22:15:59 +0100808static unsigned char dm9000_type_to_char(enum dm9000_type type)
809{
810 switch (type) {
811 case TYPE_DM9000E: return 'e';
812 case TYPE_DM9000A: return 'a';
813 case TYPE_DM9000B: return 'b';
814 }
815
816 return '?';
817}
818
Ben Dooksf8d79e72008-06-24 22:16:02 +0100819/*
820 * Set DM9000 multicast address
821 */
822static void
Baruch Siach380fefb2010-05-17 17:45:48 -0700823dm9000_hash_table_unlocked(struct net_device *dev)
Ben Dooksf8d79e72008-06-24 22:16:02 +0100824{
Wang Chen4cf16532008-11-12 23:38:14 -0800825 board_info_t *db = netdev_priv(dev);
Jiri Pirko22bedad2010-04-01 21:22:57 +0000826 struct netdev_hw_addr *ha;
Ben Dooksf8d79e72008-06-24 22:16:02 +0100827 int i, oft;
828 u32 hash_val;
829 u16 hash_table[4];
830 u8 rcr = RCR_DIS_LONG | RCR_DIS_CRC | RCR_RXEN;
Ben Dooksf8d79e72008-06-24 22:16:02 +0100831
832 dm9000_dbg(db, 1, "entering %s\n", __func__);
833
Ben Dooksf8d79e72008-06-24 22:16:02 +0100834 for (i = 0, oft = DM9000_PAR; i < 6; i++, oft++)
835 iow(db, oft, dev->dev_addr[i]);
836
837 /* Clear Hash Table */
838 for (i = 0; i < 4; i++)
839 hash_table[i] = 0x0;
840
841 /* broadcast address */
842 hash_table[3] = 0x8000;
843
844 if (dev->flags & IFF_PROMISC)
845 rcr |= RCR_PRMSC;
846
847 if (dev->flags & IFF_ALLMULTI)
848 rcr |= RCR_ALL;
849
850 /* the multicast address in Hash Table : 64 bits */
Jiri Pirko22bedad2010-04-01 21:22:57 +0000851 netdev_for_each_mc_addr(ha, dev) {
852 hash_val = ether_crc_le(6, ha->addr) & 0x3f;
Ben Dooksf8d79e72008-06-24 22:16:02 +0100853 hash_table[hash_val / 16] |= (u16) 1 << (hash_val % 16);
854 }
855
856 /* Write the hash table to MAC MD table */
857 for (i = 0, oft = DM9000_MAR; i < 4; i++) {
858 iow(db, oft++, hash_table[i]);
859 iow(db, oft++, hash_table[i] >> 8);
860 }
861
862 iow(db, DM9000_RCR, rcr);
Baruch Siach380fefb2010-05-17 17:45:48 -0700863}
864
865static void
866dm9000_hash_table(struct net_device *dev)
867{
868 board_info_t *db = netdev_priv(dev);
869 unsigned long flags;
870
871 spin_lock_irqsave(&db->lock, flags);
872 dm9000_hash_table_unlocked(dev);
Ben Dooksf8d79e72008-06-24 22:16:02 +0100873 spin_unlock_irqrestore(&db->lock, flags);
874}
875
876/*
Eric Dumazet1ae5dc32010-05-10 05:01:31 -0700877 * Initialize dm9000 board
Ben Dooksf8d79e72008-06-24 22:16:02 +0100878 */
879static void
880dm9000_init_dm9000(struct net_device *dev)
881{
Wang Chen4cf16532008-11-12 23:38:14 -0800882 board_info_t *db = netdev_priv(dev);
Ben Dooksf8d79e72008-06-24 22:16:02 +0100883 unsigned int imr;
Ben Dooksc029f442009-11-10 07:22:24 +0000884 unsigned int ncr;
Ben Dooksf8d79e72008-06-24 22:16:02 +0100885
886 dm9000_dbg(db, 1, "entering %s\n", __func__);
887
888 /* I/O mode */
889 db->io_mode = ior(db, DM9000_ISR) >> 6; /* ISR bit7:6 keeps I/O mode */
890
Yeasah Pell5dcc60b2009-07-06 18:12:33 -0700891 /* Checksum mode */
Michał Mirosławc88fcb32011-04-15 04:50:49 +0000892 if (dev->hw_features & NETIF_F_RXCSUM)
Mark Brown56d37f12011-04-18 01:04:37 +0000893 iow(db, DM9000_RCSR,
Michał Mirosławc88fcb32011-04-15 04:50:49 +0000894 (dev->features & NETIF_F_RXCSUM) ? RCSR_CSUM : 0);
Yeasah Pell5dcc60b2009-07-06 18:12:33 -0700895
Ben Dooksf8d79e72008-06-24 22:16:02 +0100896 iow(db, DM9000_GPCR, GPCR_GEP_CNTL); /* Let GPIO0 output */
Ben Dooksf8d79e72008-06-24 22:16:02 +0100897
Joseph CHANGccb926f2013-03-28 23:13:42 +0000898 dm9000_phy_write(dev, 0, MII_BMCR, BMCR_RESET); /* PHY RESET */
899 dm9000_phy_write(dev, 0, MII_DM_DSPCR, DSPCR_INIT_PARAM); /* Init */
900
Ben Dooksc029f442009-11-10 07:22:24 +0000901 ncr = (db->flags & DM9000_PLATF_EXT_PHY) ? NCR_EXT_PHY : 0;
902
903 /* if wol is needed, then always set NCR_WAKEEN otherwise we end
904 * up dumping the wake events if we disable this. There is already
905 * a wake-mask in DM9000_WCR */
906 if (db->wake_supported)
907 ncr |= NCR_WAKEEN;
908
909 iow(db, DM9000_NCR, ncr);
Ben Dooksf8d79e72008-06-24 22:16:02 +0100910
911 /* Program operating register */
912 iow(db, DM9000_TCR, 0); /* TX Polling clear */
913 iow(db, DM9000_BPTR, 0x3f); /* Less 3Kb, 200us */
914 iow(db, DM9000_FCR, 0xff); /* Flow Control */
915 iow(db, DM9000_SMCR, 0); /* Special Mode */
916 /* clear TX status */
917 iow(db, DM9000_NSR, NSR_WAKEST | NSR_TX2END | NSR_TX1END);
918 iow(db, DM9000_ISR, ISR_CLR_STATUS); /* Clear interrupt status */
919
920 /* Set address filter table */
Baruch Siach380fefb2010-05-17 17:45:48 -0700921 dm9000_hash_table_unlocked(dev);
Ben Dooksf8d79e72008-06-24 22:16:02 +0100922
923 imr = IMR_PAR | IMR_PTM | IMR_PRM;
924 if (db->type != TYPE_DM9000E)
925 imr |= IMR_LNKCHNG;
926
927 db->imr_all = imr;
928
929 /* Enable TX/RX interrupt mask */
930 iow(db, DM9000_IMR, imr);
931
932 /* Init Driver variable */
933 db->tx_pkt_cnt = 0;
934 db->queue_pkt_len = 0;
Eric Dumazet1ae5dc32010-05-10 05:01:31 -0700935 dev->trans_start = jiffies;
Ben Dooksf8d79e72008-06-24 22:16:02 +0100936}
937
938/* Our watchdog timed out. Called by the networking layer */
939static void dm9000_timeout(struct net_device *dev)
940{
Wang Chen4cf16532008-11-12 23:38:14 -0800941 board_info_t *db = netdev_priv(dev);
Ben Dooksf8d79e72008-06-24 22:16:02 +0100942 u8 reg_save;
943 unsigned long flags;
944
945 /* Save previous register address */
Ben Dooksf8d79e72008-06-24 22:16:02 +0100946 spin_lock_irqsave(&db->lock, flags);
Henry Nestler8dde9242011-02-20 11:44:58 +0000947 reg_save = readb(db->io_addr);
Ben Dooksf8d79e72008-06-24 22:16:02 +0100948
949 netif_stop_queue(dev);
950 dm9000_reset(db);
951 dm9000_init_dm9000(dev);
952 /* We can accept TX packets again */
Eric Dumazet1ae5dc32010-05-10 05:01:31 -0700953 dev->trans_start = jiffies; /* prevent tx timeout */
Ben Dooksf8d79e72008-06-24 22:16:02 +0100954 netif_wake_queue(dev);
955
956 /* Restore previous register address */
957 writeb(reg_save, db->io_addr);
958 spin_unlock_irqrestore(&db->lock, flags);
959}
960
Yeasah Pell5dcc60b2009-07-06 18:12:33 -0700961static void dm9000_send_packet(struct net_device *dev,
962 int ip_summed,
963 u16 pkt_len)
964{
965 board_info_t *dm = to_dm9000_board(dev);
966
967 /* The DM9000 is not smart enough to leave fragmented packets alone. */
968 if (dm->ip_summed != ip_summed) {
969 if (ip_summed == CHECKSUM_NONE)
970 iow(dm, DM9000_TCCR, 0);
971 else
972 iow(dm, DM9000_TCCR, TCCR_IP | TCCR_UDP | TCCR_TCP);
973 dm->ip_summed = ip_summed;
974 }
975
976 /* Set TX length to DM9000 */
977 iow(dm, DM9000_TXPLL, pkt_len);
978 iow(dm, DM9000_TXPLH, pkt_len >> 8);
979
980 /* Issue TX polling command */
981 iow(dm, DM9000_TCR, TCR_TXREQ); /* Cleared after TX complete */
982}
983
Ben Dooksf8d79e72008-06-24 22:16:02 +0100984/*
985 * Hardware start transmission.
986 * Send a packet to media from the upper layer.
987 */
988static int
989dm9000_start_xmit(struct sk_buff *skb, struct net_device *dev)
990{
991 unsigned long flags;
Wang Chen4cf16532008-11-12 23:38:14 -0800992 board_info_t *db = netdev_priv(dev);
Ben Dooksf8d79e72008-06-24 22:16:02 +0100993
994 dm9000_dbg(db, 3, "%s:\n", __func__);
995
996 if (db->tx_pkt_cnt > 1)
Patrick McHardy5b548142009-06-12 06:22:29 +0000997 return NETDEV_TX_BUSY;
Ben Dooksf8d79e72008-06-24 22:16:02 +0100998
999 spin_lock_irqsave(&db->lock, flags);
1000
1001 /* Move data to DM9000 TX RAM */
1002 writeb(DM9000_MWCMD, db->io_addr);
1003
1004 (db->outblk)(db->io_data, skb->data, skb->len);
1005 dev->stats.tx_bytes += skb->len;
1006
1007 db->tx_pkt_cnt++;
1008 /* TX control: First packet immediately send, second packet queue */
1009 if (db->tx_pkt_cnt == 1) {
Yeasah Pell5dcc60b2009-07-06 18:12:33 -07001010 dm9000_send_packet(dev, skb->ip_summed, skb->len);
Ben Dooksf8d79e72008-06-24 22:16:02 +01001011 } else {
1012 /* Second packet */
1013 db->queue_pkt_len = skb->len;
Yeasah Pell5dcc60b2009-07-06 18:12:33 -07001014 db->queue_ip_summed = skb->ip_summed;
Ben Dooksf8d79e72008-06-24 22:16:02 +01001015 netif_stop_queue(dev);
1016 }
1017
1018 spin_unlock_irqrestore(&db->lock, flags);
1019
1020 /* free this SKB */
1021 dev_kfree_skb(skb);
1022
Patrick McHardy6ed10652009-06-23 06:03:08 +00001023 return NETDEV_TX_OK;
Ben Dooksf8d79e72008-06-24 22:16:02 +01001024}
1025
1026/*
1027 * DM9000 interrupt handler
1028 * receive the packet to upper layer, free the transmitted packet
1029 */
1030
1031static void dm9000_tx_done(struct net_device *dev, board_info_t *db)
1032{
1033 int tx_status = ior(db, DM9000_NSR); /* Got TX status */
1034
1035 if (tx_status & (NSR_TX2END | NSR_TX1END)) {
1036 /* One packet sent complete */
1037 db->tx_pkt_cnt--;
1038 dev->stats.tx_packets++;
1039
1040 if (netif_msg_tx_done(db))
1041 dev_dbg(db->dev, "tx done, NSR %02x\n", tx_status);
1042
1043 /* Queue packet check & send */
Yeasah Pell5dcc60b2009-07-06 18:12:33 -07001044 if (db->tx_pkt_cnt > 0)
1045 dm9000_send_packet(dev, db->queue_ip_summed,
1046 db->queue_pkt_len);
Ben Dooksf8d79e72008-06-24 22:16:02 +01001047 netif_wake_queue(dev);
1048 }
1049}
1050
1051struct dm9000_rxhdr {
1052 u8 RxPktReady;
1053 u8 RxStatus;
1054 __le16 RxLen;
Eric Dumazetba2d3582010-06-02 18:10:09 +00001055} __packed;
Ben Dooksf8d79e72008-06-24 22:16:02 +01001056
1057/*
1058 * Received a packet and pass to upper layer
1059 */
1060static void
1061dm9000_rx(struct net_device *dev)
1062{
Wang Chen4cf16532008-11-12 23:38:14 -08001063 board_info_t *db = netdev_priv(dev);
Ben Dooksf8d79e72008-06-24 22:16:02 +01001064 struct dm9000_rxhdr rxhdr;
1065 struct sk_buff *skb;
1066 u8 rxbyte, *rdptr;
1067 bool GoodPacket;
1068 int RxLen;
1069
1070 /* Check packet ready or not */
1071 do {
1072 ior(db, DM9000_MRCMDX); /* Dummy read */
1073
1074 /* Get most updated data */
1075 rxbyte = readb(db->io_data);
1076
1077 /* Status check: this byte must be 0 or 1 */
Yeasah Pell5dcc60b2009-07-06 18:12:33 -07001078 if (rxbyte & DM9000_PKT_ERR) {
Ben Dooksf8d79e72008-06-24 22:16:02 +01001079 dev_warn(db->dev, "status check fail: %d\n", rxbyte);
1080 iow(db, DM9000_RCR, 0x00); /* Stop Device */
1081 iow(db, DM9000_ISR, IMR_PAR); /* Stop INT request */
1082 return;
1083 }
1084
Yeasah Pell5dcc60b2009-07-06 18:12:33 -07001085 if (!(rxbyte & DM9000_PKT_RDY))
Ben Dooksf8d79e72008-06-24 22:16:02 +01001086 return;
1087
1088 /* A packet ready now & Get status/length */
1089 GoodPacket = true;
1090 writeb(DM9000_MRCMD, db->io_addr);
1091
1092 (db->inblk)(db->io_data, &rxhdr, sizeof(rxhdr));
1093
1094 RxLen = le16_to_cpu(rxhdr.RxLen);
1095
1096 if (netif_msg_rx_status(db))
1097 dev_dbg(db->dev, "RX: status %02x, length %04x\n",
1098 rxhdr.RxStatus, RxLen);
1099
1100 /* Packet Status check */
1101 if (RxLen < 0x40) {
1102 GoodPacket = false;
1103 if (netif_msg_rx_err(db))
1104 dev_dbg(db->dev, "RX: Bad Packet (runt)\n");
1105 }
1106
1107 if (RxLen > DM9000_PKT_MAX) {
1108 dev_dbg(db->dev, "RST: RX Len:%x\n", RxLen);
1109 }
1110
Ben Dooksf8e5e772008-07-17 20:29:13 +01001111 /* rxhdr.RxStatus is identical to RSR register. */
1112 if (rxhdr.RxStatus & (RSR_FOE | RSR_CE | RSR_AE |
1113 RSR_PLE | RSR_RWTO |
1114 RSR_LCS | RSR_RF)) {
Ben Dooksf8d79e72008-06-24 22:16:02 +01001115 GoodPacket = false;
Ben Dooksf8e5e772008-07-17 20:29:13 +01001116 if (rxhdr.RxStatus & RSR_FOE) {
Ben Dooksf8d79e72008-06-24 22:16:02 +01001117 if (netif_msg_rx_err(db))
1118 dev_dbg(db->dev, "fifo error\n");
1119 dev->stats.rx_fifo_errors++;
1120 }
Ben Dooksf8e5e772008-07-17 20:29:13 +01001121 if (rxhdr.RxStatus & RSR_CE) {
Ben Dooksf8d79e72008-06-24 22:16:02 +01001122 if (netif_msg_rx_err(db))
1123 dev_dbg(db->dev, "crc error\n");
1124 dev->stats.rx_crc_errors++;
1125 }
Ben Dooksf8e5e772008-07-17 20:29:13 +01001126 if (rxhdr.RxStatus & RSR_RF) {
Ben Dooksf8d79e72008-06-24 22:16:02 +01001127 if (netif_msg_rx_err(db))
1128 dev_dbg(db->dev, "length error\n");
1129 dev->stats.rx_length_errors++;
1130 }
1131 }
1132
1133 /* Move data from DM9000 */
Joe Perches8e95a202009-12-03 07:58:21 +00001134 if (GoodPacket &&
Pradeep A Dalvi21a4e462012-02-05 02:50:10 +00001135 ((skb = netdev_alloc_skb(dev, RxLen + 4)) != NULL)) {
Ben Dooksf8d79e72008-06-24 22:16:02 +01001136 skb_reserve(skb, 2);
1137 rdptr = (u8 *) skb_put(skb, RxLen - 4);
1138
1139 /* Read received packet from RX SRAM */
1140
1141 (db->inblk)(db->io_data, rdptr, RxLen);
1142 dev->stats.rx_bytes += RxLen;
1143
1144 /* Pass to upper layer */
1145 skb->protocol = eth_type_trans(skb, dev);
Michał Mirosławc88fcb32011-04-15 04:50:49 +00001146 if (dev->features & NETIF_F_RXCSUM) {
Yeasah Pell5dcc60b2009-07-06 18:12:33 -07001147 if ((((rxbyte & 0x1c) << 3) & rxbyte) == 0)
1148 skb->ip_summed = CHECKSUM_UNNECESSARY;
1149 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001150 skb_checksum_none_assert(skb);
Yeasah Pell5dcc60b2009-07-06 18:12:33 -07001151 }
Ben Dooksf8d79e72008-06-24 22:16:02 +01001152 netif_rx(skb);
1153 dev->stats.rx_packets++;
1154
1155 } else {
1156 /* need to dump the packet's data */
1157
1158 (db->dumpblk)(db->io_data, RxLen);
1159 }
Yeasah Pell5dcc60b2009-07-06 18:12:33 -07001160 } while (rxbyte & DM9000_PKT_RDY);
Ben Dooksf8d79e72008-06-24 22:16:02 +01001161}
1162
1163static irqreturn_t dm9000_interrupt(int irq, void *dev_id)
1164{
1165 struct net_device *dev = dev_id;
Wang Chen4cf16532008-11-12 23:38:14 -08001166 board_info_t *db = netdev_priv(dev);
Ben Dooksf8d79e72008-06-24 22:16:02 +01001167 int int_status;
David Brownelle3162d32009-03-22 21:28:39 -07001168 unsigned long flags;
Ben Dooksf8d79e72008-06-24 22:16:02 +01001169 u8 reg_save;
1170
1171 dm9000_dbg(db, 3, "entering %s\n", __func__);
1172
1173 /* A real interrupt coming */
1174
David Brownelle3162d32009-03-22 21:28:39 -07001175 /* holders of db->lock must always block IRQs */
1176 spin_lock_irqsave(&db->lock, flags);
Ben Dooksf8d79e72008-06-24 22:16:02 +01001177
1178 /* Save previous register address */
1179 reg_save = readb(db->io_addr);
1180
1181 /* Disable all interrupts */
1182 iow(db, DM9000_IMR, IMR_PAR);
1183
1184 /* Got DM9000 interrupt status */
1185 int_status = ior(db, DM9000_ISR); /* Got ISR */
1186 iow(db, DM9000_ISR, int_status); /* Clear ISR status */
1187
1188 if (netif_msg_intr(db))
1189 dev_dbg(db->dev, "interrupt status %02x\n", int_status);
1190
1191 /* Received the coming packet */
1192 if (int_status & ISR_PRS)
1193 dm9000_rx(dev);
1194
1195 /* Trnasmit Interrupt check */
1196 if (int_status & ISR_PTS)
1197 dm9000_tx_done(dev, db);
1198
1199 if (db->type != TYPE_DM9000E) {
1200 if (int_status & ISR_LNKCHNG) {
1201 /* fire a link-change request */
1202 schedule_delayed_work(&db->phy_poll, 1);
1203 }
1204 }
1205
1206 /* Re-enable interrupt mask */
1207 iow(db, DM9000_IMR, db->imr_all);
1208
1209 /* Restore previous register address */
1210 writeb(reg_save, db->io_addr);
1211
David Brownelle3162d32009-03-22 21:28:39 -07001212 spin_unlock_irqrestore(&db->lock, flags);
Ben Dooksf8d79e72008-06-24 22:16:02 +01001213
1214 return IRQ_HANDLED;
1215}
1216
Ben Dooksc029f442009-11-10 07:22:24 +00001217static irqreturn_t dm9000_wol_interrupt(int irq, void *dev_id)
1218{
1219 struct net_device *dev = dev_id;
1220 board_info_t *db = netdev_priv(dev);
1221 unsigned long flags;
1222 unsigned nsr, wcr;
1223
1224 spin_lock_irqsave(&db->lock, flags);
1225
1226 nsr = ior(db, DM9000_NSR);
1227 wcr = ior(db, DM9000_WCR);
1228
1229 dev_dbg(db->dev, "%s: NSR=0x%02x, WCR=0x%02x\n", __func__, nsr, wcr);
1230
1231 if (nsr & NSR_WAKEST) {
1232 /* clear, so we can avoid */
1233 iow(db, DM9000_NSR, NSR_WAKEST);
1234
1235 if (wcr & WCR_LINKST)
1236 dev_info(db->dev, "wake by link status change\n");
1237 if (wcr & WCR_SAMPLEST)
1238 dev_info(db->dev, "wake by sample packet\n");
1239 if (wcr & WCR_MAGICST )
1240 dev_info(db->dev, "wake by magic packet\n");
1241 if (!(wcr & (WCR_LINKST | WCR_SAMPLEST | WCR_MAGICST)))
1242 dev_err(db->dev, "wake signalled with no reason? "
1243 "NSR=0x%02x, WSR=0x%02x\n", nsr, wcr);
1244
1245 }
1246
1247 spin_unlock_irqrestore(&db->lock, flags);
1248
1249 return (nsr & NSR_WAKEST) ? IRQ_HANDLED : IRQ_NONE;
1250}
1251
Ben Dooksf8d79e72008-06-24 22:16:02 +01001252#ifdef CONFIG_NET_POLL_CONTROLLER
1253/*
1254 *Used by netconsole
1255 */
1256static void dm9000_poll_controller(struct net_device *dev)
1257{
1258 disable_irq(dev->irq);
1259 dm9000_interrupt(dev->irq, dev);
1260 enable_irq(dev->irq);
1261}
1262#endif
1263
1264/*
1265 * Open the interface.
1266 * The interface is opened whenever "ifconfig" actives it.
1267 */
1268static int
1269dm9000_open(struct net_device *dev)
1270{
Wang Chen4cf16532008-11-12 23:38:14 -08001271 board_info_t *db = netdev_priv(dev);
Ben Dooksf8d79e72008-06-24 22:16:02 +01001272 unsigned long irqflags = db->irq_res->flags & IRQF_TRIGGER_MASK;
1273
1274 if (netif_msg_ifup(db))
1275 dev_dbg(db->dev, "enabling %s\n", dev->name);
1276
1277 /* If there is no IRQ type specified, default to something that
1278 * may work, and tell the user that this is a problem */
1279
Ben Dooks6ff4ff02008-06-24 22:16:07 +01001280 if (irqflags == IRQF_TRIGGER_NONE)
Ben Dooksf8d79e72008-06-24 22:16:02 +01001281 dev_warn(db->dev, "WARNING: no IRQ resource flags set.\n");
Ben Dooks6ff4ff02008-06-24 22:16:07 +01001282
Ben Dooksf8d79e72008-06-24 22:16:02 +01001283 irqflags |= IRQF_SHARED;
1284
Henry Nestler108f5182011-02-22 11:29:42 +00001285 /* GPIO0 on pre-activate PHY, Reg 1F is not set by reset */
1286 iow(db, DM9000_GPR, 0); /* REG_1F bit0 activate phyxcer */
1287 mdelay(1); /* delay needs by DM9000B */
1288
Ben Dooksf8d79e72008-06-24 22:16:02 +01001289 /* Initialize DM9000 board */
1290 dm9000_reset(db);
1291 dm9000_init_dm9000(dev);
1292
Mark Brown6979d5d2011-06-01 10:18:09 +00001293 if (request_irq(dev->irq, dm9000_interrupt, irqflags, dev->name, dev))
1294 return -EAGAIN;
1295
Ben Dooksf8d79e72008-06-24 22:16:02 +01001296 /* Init driver variable */
1297 db->dbug_cnt = 0;
1298
1299 mii_check_media(&db->mii, netif_msg_link(db), 1);
1300 netif_start_queue(dev);
1301
1302 dm9000_schedule_poll(db);
1303
1304 return 0;
1305}
1306
Ben Dooksf8d79e72008-06-24 22:16:02 +01001307static void
1308dm9000_shutdown(struct net_device *dev)
1309{
Wang Chen4cf16532008-11-12 23:38:14 -08001310 board_info_t *db = netdev_priv(dev);
Ben Dooksf8d79e72008-06-24 22:16:02 +01001311
1312 /* RESET device */
1313 dm9000_phy_write(dev, 0, MII_BMCR, BMCR_RESET); /* PHY RESET */
1314 iow(db, DM9000_GPR, 0x01); /* Power-Down PHY */
1315 iow(db, DM9000_IMR, IMR_PAR); /* Disable all interrupt */
1316 iow(db, DM9000_RCR, 0x00); /* Disable RX */
1317}
1318
1319/*
1320 * Stop the interface.
1321 * The interface is stopped when it is brought.
1322 */
1323static int
1324dm9000_stop(struct net_device *ndev)
1325{
Wang Chen4cf16532008-11-12 23:38:14 -08001326 board_info_t *db = netdev_priv(ndev);
Ben Dooksf8d79e72008-06-24 22:16:02 +01001327
1328 if (netif_msg_ifdown(db))
1329 dev_dbg(db->dev, "shutting down %s\n", ndev->name);
1330
1331 cancel_delayed_work_sync(&db->phy_poll);
1332
1333 netif_stop_queue(ndev);
1334 netif_carrier_off(ndev);
1335
1336 /* free interrupt */
1337 free_irq(ndev->irq, ndev);
1338
1339 dm9000_shutdown(ndev);
1340
1341 return 0;
1342}
1343
Alexander Beregalovd88106b2009-04-15 12:52:37 +00001344static const struct net_device_ops dm9000_netdev_ops = {
1345 .ndo_open = dm9000_open,
1346 .ndo_stop = dm9000_stop,
1347 .ndo_start_xmit = dm9000_start_xmit,
1348 .ndo_tx_timeout = dm9000_timeout,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001349 .ndo_set_rx_mode = dm9000_hash_table,
Alexander Beregalovd88106b2009-04-15 12:52:37 +00001350 .ndo_do_ioctl = dm9000_ioctl,
1351 .ndo_change_mtu = eth_change_mtu,
Michał Mirosławc88fcb32011-04-15 04:50:49 +00001352 .ndo_set_features = dm9000_set_features,
Alexander Beregalovd88106b2009-04-15 12:52:37 +00001353 .ndo_validate_addr = eth_validate_addr,
1354 .ndo_set_mac_address = eth_mac_addr,
1355#ifdef CONFIG_NET_POLL_CONTROLLER
1356 .ndo_poll_controller = dm9000_poll_controller,
1357#endif
1358};
1359
Sascha Hauera1365272005-05-05 15:14:15 -07001360/*
1361 * Search DM9000 board, allocate space and register it
1362 */
Enrico Scholze21fd4f2008-05-08 11:33:03 +01001363static int __devinit
Russell King3ae5eae2005-11-09 22:32:44 +00001364dm9000_probe(struct platform_device *pdev)
Sascha Hauera1365272005-05-05 15:14:15 -07001365{
Sascha Hauera1365272005-05-05 15:14:15 -07001366 struct dm9000_plat_data *pdata = pdev->dev.platform_data;
1367 struct board_info *db; /* Point a board information structure */
1368 struct net_device *ndev;
Ben Dooks179c7432008-02-05 00:02:23 +00001369 const unsigned char *mac_src;
Sascha Hauera1365272005-05-05 15:14:15 -07001370 int ret = 0;
1371 int iosize;
1372 int i;
1373 u32 id_val;
1374
Sascha Hauera1365272005-05-05 15:14:15 -07001375 /* Init network device */
Ben Dooksf8d79e72008-06-24 22:16:02 +01001376 ndev = alloc_etherdev(sizeof(struct board_info));
Joe Perches41de8d42012-01-29 13:47:52 +00001377 if (!ndev)
Sascha Hauera1365272005-05-05 15:14:15 -07001378 return -ENOMEM;
Sascha Hauera1365272005-05-05 15:14:15 -07001379
Russell King3ae5eae2005-11-09 22:32:44 +00001380 SET_NETDEV_DEV(ndev, &pdev->dev);
Sascha Hauera1365272005-05-05 15:14:15 -07001381
Enrico Scholz37d5dca2008-05-08 11:35:13 +01001382 dev_dbg(&pdev->dev, "dm9000_probe()\n");
Sascha Hauera1365272005-05-05 15:14:15 -07001383
1384 /* setup board info structure */
Wang Chen4cf16532008-11-12 23:38:14 -08001385 db = netdev_priv(ndev);
Sascha Hauera1365272005-05-05 15:14:15 -07001386
Ben Dooksa76836f2008-02-05 00:02:02 +00001387 db->dev = &pdev->dev;
Ben Dooks8f5bf5f2008-05-08 11:36:42 +01001388 db->ndev = ndev;
Ben Dooksa76836f2008-02-05 00:02:02 +00001389
Ben Dooks9ef9ac52005-07-23 17:25:18 +01001390 spin_lock_init(&db->lock);
Ben Dooks9a2f0372008-02-05 00:02:10 +00001391 mutex_init(&db->addr_lock);
Ben Dooks9ef9ac52005-07-23 17:25:18 +01001392
Ben Dooks8f5bf5f2008-05-08 11:36:42 +01001393 INIT_DELAYED_WORK(&db->phy_poll, dm9000_poll_work);
1394
Laurent Pinchart08c3f572008-06-24 22:15:57 +01001395 db->addr_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1396 db->data_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1397 db->irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1398
1399 if (db->addr_res == NULL || db->data_res == NULL ||
1400 db->irq_res == NULL) {
1401 dev_err(db->dev, "insufficient resources\n");
1402 ret = -ENOENT;
1403 goto out;
1404 }
1405
Ben Dooksc029f442009-11-10 07:22:24 +00001406 db->irq_wake = platform_get_irq(pdev, 1);
1407 if (db->irq_wake >= 0) {
1408 dev_dbg(db->dev, "wakeup irq %d\n", db->irq_wake);
1409
1410 ret = request_irq(db->irq_wake, dm9000_wol_interrupt,
1411 IRQF_SHARED, dev_name(db->dev), ndev);
1412 if (ret) {
1413 dev_err(db->dev, "cannot get wakeup irq (%d)\n", ret);
1414 } else {
1415
1416 /* test to see if irq is really wakeup capable */
Thomas Gleixnerdced35a2011-03-28 17:49:12 +02001417 ret = irq_set_irq_wake(db->irq_wake, 1);
Ben Dooksc029f442009-11-10 07:22:24 +00001418 if (ret) {
1419 dev_err(db->dev, "irq %d cannot set wakeup (%d)\n",
1420 db->irq_wake, ret);
1421 ret = 0;
1422 } else {
Thomas Gleixnerdced35a2011-03-28 17:49:12 +02001423 irq_set_irq_wake(db->irq_wake, 0);
Ben Dooksc029f442009-11-10 07:22:24 +00001424 db->wake_supported = 1;
1425 }
1426 }
1427 }
1428
Tobias Klauserec282e92009-09-09 01:07:43 +00001429 iosize = resource_size(db->addr_res);
Laurent Pinchart08c3f572008-06-24 22:15:57 +01001430 db->addr_req = request_mem_region(db->addr_res->start, iosize,
1431 pdev->name);
1432
1433 if (db->addr_req == NULL) {
1434 dev_err(db->dev, "cannot claim address reg area\n");
1435 ret = -EIO;
1436 goto out;
1437 }
1438
1439 db->io_addr = ioremap(db->addr_res->start, iosize);
1440
1441 if (db->io_addr == NULL) {
1442 dev_err(db->dev, "failed to ioremap address reg\n");
1443 ret = -EINVAL;
1444 goto out;
1445 }
1446
Tobias Klauserec282e92009-09-09 01:07:43 +00001447 iosize = resource_size(db->data_res);
Laurent Pinchart08c3f572008-06-24 22:15:57 +01001448 db->data_req = request_mem_region(db->data_res->start, iosize,
1449 pdev->name);
1450
1451 if (db->data_req == NULL) {
1452 dev_err(db->dev, "cannot claim data reg area\n");
1453 ret = -EIO;
1454 goto out;
1455 }
1456
1457 db->io_data = ioremap(db->data_res->start, iosize);
1458
1459 if (db->io_data == NULL) {
1460 dev_err(db->dev, "failed to ioremap data reg\n");
1461 ret = -EINVAL;
1462 goto out;
1463 }
1464
1465 /* fill in parameters for net-dev structure */
1466 ndev->base_addr = (unsigned long)db->io_addr;
1467 ndev->irq = db->irq_res->start;
1468
1469 /* ensure at least we have a default set of IO routines */
1470 dm9000_set_io(db, iosize);
1471
Sascha Hauera1365272005-05-05 15:14:15 -07001472 /* check to see if anything is being over-ridden */
1473 if (pdata != NULL) {
1474 /* check to see if the driver wants to over-ride the
1475 * default IO width */
1476
1477 if (pdata->flags & DM9000_PLATF_8BITONLY)
1478 dm9000_set_io(db, 1);
1479
1480 if (pdata->flags & DM9000_PLATF_16BITONLY)
1481 dm9000_set_io(db, 2);
1482
1483 if (pdata->flags & DM9000_PLATF_32BITONLY)
1484 dm9000_set_io(db, 4);
1485
1486 /* check to see if there are any IO routine
1487 * over-rides */
1488
1489 if (pdata->inblk != NULL)
1490 db->inblk = pdata->inblk;
1491
1492 if (pdata->outblk != NULL)
1493 db->outblk = pdata->outblk;
1494
1495 if (pdata->dumpblk != NULL)
1496 db->dumpblk = pdata->dumpblk;
Ben Dooks33ba5092008-02-05 00:02:01 +00001497
1498 db->flags = pdata->flags;
Sascha Hauera1365272005-05-05 15:14:15 -07001499 }
1500
Ben Dooksf8dd0ec2008-06-24 22:16:04 +01001501#ifdef CONFIG_DM9000_FORCE_SIMPLE_PHY_POLL
1502 db->flags |= DM9000_PLATF_SIMPLE_PHY;
1503#endif
1504
Joseph CHANGccb926f2013-03-28 23:13:42 +00001505 /* Fixing bug on dm9000_probe, takeover dm9000_reset(db),
1506 * Need 'NCR_MAC_LBK' bit to indeed stable our DM9000 fifo
1507 * while probe stage.
1508 */
1509
1510 iow(db, DM9000_NCR, NCR_MAC_LBK | NCR_RST);
Sascha Hauera1365272005-05-05 15:14:15 -07001511
Ben Dooks59eae1f2008-06-24 22:16:01 +01001512 /* try multiple times, DM9000 sometimes gets the read wrong */
Ben Dooks513b6be2008-02-05 00:02:22 +00001513 for (i = 0; i < 8; i++) {
Sascha Hauera1365272005-05-05 15:14:15 -07001514 id_val = ior(db, DM9000_VIDL);
1515 id_val |= (u32)ior(db, DM9000_VIDH) << 8;
1516 id_val |= (u32)ior(db, DM9000_PIDL) << 16;
1517 id_val |= (u32)ior(db, DM9000_PIDH) << 24;
1518
1519 if (id_val == DM9000_ID)
1520 break;
Ben Dooksa76836f2008-02-05 00:02:02 +00001521 dev_err(db->dev, "read wrong id 0x%08x\n", id_val);
Sascha Hauera1365272005-05-05 15:14:15 -07001522 }
1523
1524 if (id_val != DM9000_ID) {
Ben Dooksa76836f2008-02-05 00:02:02 +00001525 dev_err(db->dev, "wrong id: 0x%08x\n", id_val);
Mike Rapoport418d6f82007-10-18 09:23:11 +02001526 ret = -ENODEV;
1527 goto out;
Sascha Hauera1365272005-05-05 15:14:15 -07001528 }
1529
Ben Dooks6d406b32008-06-24 22:15:59 +01001530 /* Identify what type of DM9000 we are working on */
1531
1532 id_val = ior(db, DM9000_CHIPR);
1533 dev_dbg(db->dev, "dm9000 revision 0x%02x\n", id_val);
1534
1535 switch (id_val) {
1536 case CHIPR_DM9000A:
1537 db->type = TYPE_DM9000A;
1538 break;
1539 case CHIPR_DM9000B:
1540 db->type = TYPE_DM9000B;
1541 break;
1542 default:
1543 dev_dbg(db->dev, "ID %02x => defaulting to DM9000E\n", id_val);
1544 db->type = TYPE_DM9000E;
1545 }
1546
Yeasah Pell5dcc60b2009-07-06 18:12:33 -07001547 /* dm9000a/b are capable of hardware checksum offload */
1548 if (db->type == TYPE_DM9000A || db->type == TYPE_DM9000B) {
Michał Mirosławc88fcb32011-04-15 04:50:49 +00001549 ndev->hw_features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM;
1550 ndev->features |= ndev->hw_features;
Yeasah Pell5dcc60b2009-07-06 18:12:33 -07001551 }
1552
Sascha Hauera1365272005-05-05 15:14:15 -07001553 /* from this point we assume that we have found a DM9000 */
1554
1555 /* driver system function */
1556 ether_setup(ndev);
1557
Alexander Beregalovd88106b2009-04-15 12:52:37 +00001558 ndev->netdev_ops = &dm9000_netdev_ops;
1559 ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
1560 ndev->ethtool_ops = &dm9000_ethtool_ops;
Sascha Hauera1365272005-05-05 15:14:15 -07001561
Sascha Hauera1365272005-05-05 15:14:15 -07001562 db->msg_enable = NETIF_MSG_LINK;
1563 db->mii.phy_id_mask = 0x1f;
1564 db->mii.reg_num_mask = 0x1f;
1565 db->mii.force_media = 0;
1566 db->mii.full_duplex = 0;
1567 db->mii.dev = ndev;
1568 db->mii.mdio_read = dm9000_phy_read;
1569 db->mii.mdio_write = dm9000_phy_write;
1570
Ben Dooks179c7432008-02-05 00:02:23 +00001571 mac_src = "eeprom";
1572
Ben Dooks86c62fa2008-02-05 00:02:09 +00001573 /* try reading the node address from the attached EEPROM */
1574 for (i = 0; i < 6; i += 2)
1575 dm9000_read_eeprom(db, i / 2, ndev->dev_addr+i);
Sascha Hauera1365272005-05-05 15:14:15 -07001576
Laurent Pinchartfe414242008-07-23 17:41:52 +02001577 if (!is_valid_ether_addr(ndev->dev_addr) && pdata != NULL) {
1578 mac_src = "platform data";
1579 memcpy(ndev->dev_addr, pdata->dev_addr, 6);
1580 }
1581
Ben Dooks5b55dda2006-06-13 23:50:15 +01001582 if (!is_valid_ether_addr(ndev->dev_addr)) {
1583 /* try reading from mac */
Ben Dooksf8d79e72008-06-24 22:16:02 +01001584
Ben Dooks179c7432008-02-05 00:02:23 +00001585 mac_src = "chip";
Ben Dooks5b55dda2006-06-13 23:50:15 +01001586 for (i = 0; i < 6; i++)
1587 ndev->dev_addr[i] = ior(db, i+DM9000_PAR);
1588 }
1589
Ben Dooks85e6b8c2011-02-24 03:17:12 +00001590 if (!is_valid_ether_addr(ndev->dev_addr)) {
Ben Dooksa76836f2008-02-05 00:02:02 +00001591 dev_warn(db->dev, "%s: Invalid ethernet MAC address. Please "
1592 "set using ifconfig\n", ndev->name);
Sascha Hauera1365272005-05-05 15:14:15 -07001593
Danny Kukawkaf2cedb62012-02-15 06:45:39 +00001594 eth_hw_addr_random(ndev);
Ben Dooks85e6b8c2011-02-24 03:17:12 +00001595 mac_src = "random";
1596 }
1597
1598
Russell King3ae5eae2005-11-09 22:32:44 +00001599 platform_set_drvdata(pdev, ndev);
Sascha Hauera1365272005-05-05 15:14:15 -07001600 ret = register_netdev(ndev);
1601
Johannes Berge1749612008-10-27 15:59:26 -07001602 if (ret == 0)
1603 printk(KERN_INFO "%s: dm9000%c at %p,%p IRQ %d MAC: %pM (%s)\n",
Ben Dooks6d406b32008-06-24 22:15:59 +01001604 ndev->name, dm9000_type_to_char(db->type),
1605 db->io_addr, db->io_data, ndev->irq,
Johannes Berge1749612008-10-27 15:59:26 -07001606 ndev->dev_addr, mac_src);
Sascha Hauera1365272005-05-05 15:14:15 -07001607 return 0;
1608
Mike Rapoport418d6f82007-10-18 09:23:11 +02001609out:
Ben Dooksa76836f2008-02-05 00:02:02 +00001610 dev_err(db->dev, "not found (%d).\n", ret);
Sascha Hauera1365272005-05-05 15:14:15 -07001611
1612 dm9000_release_board(pdev, db);
Ben Dooks9fd9f9b2007-05-07 11:13:25 +00001613 free_netdev(ndev);
Sascha Hauera1365272005-05-05 15:14:15 -07001614
1615 return ret;
1616}
1617
Sascha Hauera1365272005-05-05 15:14:15 -07001618static int
Mike Rapoport69222e22009-07-21 12:37:18 -07001619dm9000_drv_suspend(struct device *dev)
Sascha Hauera1365272005-05-05 15:14:15 -07001620{
Mike Rapoport69222e22009-07-21 12:37:18 -07001621 struct platform_device *pdev = to_platform_device(dev);
1622 struct net_device *ndev = platform_get_drvdata(pdev);
Ben Dooks321f69a2008-02-05 00:02:08 +00001623 board_info_t *db;
Sascha Hauera1365272005-05-05 15:14:15 -07001624
Russell King9480e302005-10-28 09:52:56 -07001625 if (ndev) {
Wang Chen4cf16532008-11-12 23:38:14 -08001626 db = netdev_priv(ndev);
Ben Dooks321f69a2008-02-05 00:02:08 +00001627 db->in_suspend = 1;
1628
Ben Dooksc029f442009-11-10 07:22:24 +00001629 if (!netif_running(ndev))
1630 return 0;
1631
1632 netif_device_detach(ndev);
1633
1634 /* only shutdown if not using WoL */
1635 if (!db->wake_state)
Sascha Hauera1365272005-05-05 15:14:15 -07001636 dm9000_shutdown(ndev);
Sascha Hauera1365272005-05-05 15:14:15 -07001637 }
1638 return 0;
1639}
1640
1641static int
Mike Rapoport69222e22009-07-21 12:37:18 -07001642dm9000_drv_resume(struct device *dev)
Sascha Hauera1365272005-05-05 15:14:15 -07001643{
Mike Rapoport69222e22009-07-21 12:37:18 -07001644 struct platform_device *pdev = to_platform_device(dev);
1645 struct net_device *ndev = platform_get_drvdata(pdev);
Wang Chen4cf16532008-11-12 23:38:14 -08001646 board_info_t *db = netdev_priv(ndev);
Sascha Hauera1365272005-05-05 15:14:15 -07001647
Russell King9480e302005-10-28 09:52:56 -07001648 if (ndev) {
Sascha Hauera1365272005-05-05 15:14:15 -07001649 if (netif_running(ndev)) {
Ben Dooksc029f442009-11-10 07:22:24 +00001650 /* reset if we were not in wake mode to ensure if
1651 * the device was powered off it is in a known state */
1652 if (!db->wake_state) {
1653 dm9000_reset(db);
1654 dm9000_init_dm9000(ndev);
1655 }
Sascha Hauera1365272005-05-05 15:14:15 -07001656
1657 netif_device_attach(ndev);
1658 }
Ben Dooks321f69a2008-02-05 00:02:08 +00001659
1660 db->in_suspend = 0;
Sascha Hauera1365272005-05-05 15:14:15 -07001661 }
1662 return 0;
1663}
1664
Alexey Dobriyan47145212009-12-14 18:00:08 -08001665static const struct dev_pm_ops dm9000_drv_pm_ops = {
Mike Rapoport69222e22009-07-21 12:37:18 -07001666 .suspend = dm9000_drv_suspend,
1667 .resume = dm9000_drv_resume,
1668};
1669
Enrico Scholze21fd4f2008-05-08 11:33:03 +01001670static int __devexit
Russell King3ae5eae2005-11-09 22:32:44 +00001671dm9000_drv_remove(struct platform_device *pdev)
Sascha Hauera1365272005-05-05 15:14:15 -07001672{
Russell King3ae5eae2005-11-09 22:32:44 +00001673 struct net_device *ndev = platform_get_drvdata(pdev);
Sascha Hauera1365272005-05-05 15:14:15 -07001674
Russell King3ae5eae2005-11-09 22:32:44 +00001675 platform_set_drvdata(pdev, NULL);
Sascha Hauera1365272005-05-05 15:14:15 -07001676
1677 unregister_netdev(ndev);
Joe Perchesece49152010-11-15 11:12:31 +00001678 dm9000_release_board(pdev, netdev_priv(ndev));
Ben Dooks9fd9f9b2007-05-07 11:13:25 +00001679 free_netdev(ndev); /* free device structure */
Sascha Hauera1365272005-05-05 15:14:15 -07001680
Ben Dooksa76836f2008-02-05 00:02:02 +00001681 dev_dbg(&pdev->dev, "released and freed device\n");
Sascha Hauera1365272005-05-05 15:14:15 -07001682 return 0;
1683}
1684
Russell King3ae5eae2005-11-09 22:32:44 +00001685static struct platform_driver dm9000_driver = {
Ben Dooks5d22a312006-06-14 00:09:17 +01001686 .driver = {
1687 .name = "dm9000",
1688 .owner = THIS_MODULE,
Mike Rapoport69222e22009-07-21 12:37:18 -07001689 .pm = &dm9000_drv_pm_ops,
Ben Dooks5d22a312006-06-14 00:09:17 +01001690 },
Sascha Hauera1365272005-05-05 15:14:15 -07001691 .probe = dm9000_probe,
Enrico Scholze21fd4f2008-05-08 11:33:03 +01001692 .remove = __devexit_p(dm9000_drv_remove),
Sascha Hauera1365272005-05-05 15:14:15 -07001693};
1694
1695static int __init
1696dm9000_init(void)
1697{
Ben Dooks7da99852008-02-05 00:02:06 +00001698 printk(KERN_INFO "%s Ethernet Driver, V%s\n", CARDNAME, DRV_VERSION);
Ben Dooks2ae2d772005-07-23 17:29:38 +01001699
Ben Dooks59eae1f2008-06-24 22:16:01 +01001700 return platform_driver_register(&dm9000_driver);
Sascha Hauera1365272005-05-05 15:14:15 -07001701}
1702
1703static void __exit
1704dm9000_cleanup(void)
1705{
Russell King3ae5eae2005-11-09 22:32:44 +00001706 platform_driver_unregister(&dm9000_driver);
Sascha Hauera1365272005-05-05 15:14:15 -07001707}
1708
1709module_init(dm9000_init);
1710module_exit(dm9000_cleanup);
1711
1712MODULE_AUTHOR("Sascha Hauer, Ben Dooks");
1713MODULE_DESCRIPTION("Davicom DM9000 network driver");
1714MODULE_LICENSE("GPL");
Kay Sievers72abb462008-04-18 13:50:44 -07001715MODULE_ALIAS("platform:dm9000");