blob: 3f650ce10cf40767c125336337a9f6e02d57bd4d [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>
Tomasz Figa0b8bf1b2013-05-20 09:16:58 +000032#include <linux/of.h>
33#include <linux/of_net.h>
Ben Dooks7da99852008-02-05 00:02:06 +000034#include <linux/ethtool.h>
Sascha Hauera1365272005-05-05 15:14:15 -070035#include <linux/dm9000.h>
36#include <linux/delay.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010037#include <linux/platform_device.h>
Daniel Mack4e4fc052008-01-23 14:54:50 +010038#include <linux/irq.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
Sascha Hauera1365272005-05-05 15:14:15 -070040
41#include <asm/delay.h>
42#include <asm/irq.h>
43#include <asm/io.h>
44
45#include "dm9000.h"
46
47/* Board/System/Debug information/definition ---------------- */
48
49#define DM9000_PHY 0x40 /* PHY address 0x01 */
50
Ben Dooks59eae1f2008-06-24 22:16:01 +010051#define CARDNAME "dm9000"
52#define DRV_VERSION "1.31"
Sascha Hauera1365272005-05-05 15:14:15 -070053
Sascha Hauera1365272005-05-05 15:14:15 -070054/*
55 * Transmit timeout, default 5 seconds.
56 */
57static int watchdog = 5000;
58module_param(watchdog, int, 0400);
59MODULE_PARM_DESC(watchdog, "transmit timeout in milliseconds");
60
Vladimir Zapolskiy2e025c72011-08-20 14:15:55 -070061/*
62 * Debug messages level
63 */
64static int debug;
65module_param(debug, int, 0644);
66MODULE_PARM_DESC(debug, "dm9000 debug level (0-4)");
67
Ben Dooks9a2f0372008-02-05 00:02:10 +000068/* DM9000 register address locking.
69 *
70 * The DM9000 uses an address register to control where data written
71 * to the data register goes. This means that the address register
72 * must be preserved over interrupts or similar calls.
73 *
74 * During interrupt and other critical calls, a spinlock is used to
75 * protect the system, but the calls themselves save the address
76 * in the address register in case they are interrupting another
77 * access to the device.
78 *
79 * For general accesses a lock is provided so that calls which are
80 * allowed to sleep are serialised so that the address register does
81 * not need to be saved. This lock also serves to serialise access
82 * to the EEPROM and PHY access registers which are shared between
83 * these two devices.
84 */
85
Ben Dooks6d406b32008-06-24 22:15:59 +010086/* The driver supports the original DM9000E, and now the two newer
87 * devices, DM9000A and DM9000B.
88 */
89
90enum dm9000_type {
91 TYPE_DM9000E, /* original DM9000 */
92 TYPE_DM9000A,
93 TYPE_DM9000B
94};
95
Sascha Hauera1365272005-05-05 15:14:15 -070096/* Structure/enum declaration ------------------------------- */
97typedef struct board_info {
98
Ben Dooks59eae1f2008-06-24 22:16:01 +010099 void __iomem *io_addr; /* Register I/O base address */
100 void __iomem *io_data; /* Data I/O address */
101 u16 irq; /* IRQ */
Sascha Hauera1365272005-05-05 15:14:15 -0700102
Ben Dooks59eae1f2008-06-24 22:16:01 +0100103 u16 tx_pkt_cnt;
104 u16 queue_pkt_len;
105 u16 queue_start_addr;
Yeasah Pell5dcc60b2009-07-06 18:12:33 -0700106 u16 queue_ip_summed;
Ben Dooks59eae1f2008-06-24 22:16:01 +0100107 u16 dbug_cnt;
108 u8 io_mode; /* 0:word, 2:byte */
109 u8 phy_addr;
110 u8 imr_all;
111
112 unsigned int flags;
113 unsigned int in_suspend :1;
Ben Dooksc029f442009-11-10 07:22:24 +0000114 unsigned int wake_supported :1;
Sascha Hauera1365272005-05-05 15:14:15 -0700115
Ben Dooks6d406b32008-06-24 22:15:59 +0100116 enum dm9000_type type;
Ben Dooks5b2b4ff2008-02-05 00:02:03 +0000117
Sascha Hauera1365272005-05-05 15:14:15 -0700118 void (*inblk)(void __iomem *port, void *data, int length);
119 void (*outblk)(void __iomem *port, void *data, int length);
120 void (*dumpblk)(void __iomem *port, int length);
121
Ben Dooksa76836f2008-02-05 00:02:02 +0000122 struct device *dev; /* parent device */
123
Sascha Hauera1365272005-05-05 15:14:15 -0700124 struct resource *addr_res; /* resources found */
125 struct resource *data_res;
126 struct resource *addr_req; /* resources requested */
127 struct resource *data_req;
128 struct resource *irq_res;
129
Ben Dooksc029f442009-11-10 07:22:24 +0000130 int irq_wake;
131
Ben Dooks9a2f0372008-02-05 00:02:10 +0000132 struct mutex addr_lock; /* phy and eeprom access lock */
133
Ben Dooks8f5bf5f22008-05-08 11:36:42 +0100134 struct delayed_work phy_poll;
135 struct net_device *ndev;
136
Ben Dooks59eae1f2008-06-24 22:16:01 +0100137 spinlock_t lock;
Sascha Hauera1365272005-05-05 15:14:15 -0700138
139 struct mii_if_info mii;
Ben Dooks59eae1f2008-06-24 22:16:01 +0100140 u32 msg_enable;
Ben Dooksc029f442009-11-10 07:22:24 +0000141 u32 wake_state;
Yeasah Pell5dcc60b2009-07-06 18:12:33 -0700142
Yeasah Pell5dcc60b2009-07-06 18:12:33 -0700143 int ip_summed;
Sascha Hauera1365272005-05-05 15:14:15 -0700144} board_info_t;
145
Ben Dooks5b2b4ff2008-02-05 00:02:03 +0000146/* debug code */
147
148#define dm9000_dbg(db, lev, msg...) do { \
Vladimir Zapolskiy2e025c72011-08-20 14:15:55 -0700149 if ((lev) < debug) { \
Ben Dooks5b2b4ff2008-02-05 00:02:03 +0000150 dev_dbg(db->dev, msg); \
151 } \
152} while (0)
153
Ben Dooks7da99852008-02-05 00:02:06 +0000154static inline board_info_t *to_dm9000_board(struct net_device *dev)
155{
Wang Chen4cf16532008-11-12 23:38:14 -0800156 return netdev_priv(dev);
Ben Dooks7da99852008-02-05 00:02:06 +0000157}
158
Sascha Hauera1365272005-05-05 15:14:15 -0700159/* DM9000 network board routine ---------------------------- */
160
161static void
162dm9000_reset(board_info_t * db)
163{
Ben Dooksa76836f2008-02-05 00:02:02 +0000164 dev_dbg(db->dev, "resetting device\n");
165
Sascha Hauera1365272005-05-05 15:14:15 -0700166 /* RESET device */
167 writeb(DM9000_NCR, db->io_addr);
168 udelay(200);
169 writeb(NCR_RST, db->io_data);
170 udelay(200);
171}
172
173/*
174 * Read a byte from I/O port
175 */
176static u8
177ior(board_info_t * db, int reg)
178{
179 writeb(reg, db->io_addr);
180 return readb(db->io_data);
181}
182
183/*
184 * Write a byte to I/O port
185 */
186
187static void
188iow(board_info_t * db, int reg, int value)
189{
190 writeb(reg, db->io_addr);
191 writeb(value, db->io_data);
192}
193
194/* routines for sending block to chip */
195
196static void dm9000_outblk_8bit(void __iomem *reg, void *data, int count)
197{
Matthew Leachdaadaf62012-12-10 09:12:37 +0000198 iowrite8_rep(reg, data, count);
Sascha Hauera1365272005-05-05 15:14:15 -0700199}
200
201static void dm9000_outblk_16bit(void __iomem *reg, void *data, int count)
202{
Matthew Leachdaadaf62012-12-10 09:12:37 +0000203 iowrite16_rep(reg, data, (count+1) >> 1);
Sascha Hauera1365272005-05-05 15:14:15 -0700204}
205
206static void dm9000_outblk_32bit(void __iomem *reg, void *data, int count)
207{
Matthew Leachdaadaf62012-12-10 09:12:37 +0000208 iowrite32_rep(reg, data, (count+3) >> 2);
Sascha Hauera1365272005-05-05 15:14:15 -0700209}
210
211/* input block from chip to memory */
212
213static void dm9000_inblk_8bit(void __iomem *reg, void *data, int count)
214{
Matthew Leachdaadaf62012-12-10 09:12:37 +0000215 ioread8_rep(reg, data, count);
Sascha Hauera1365272005-05-05 15:14:15 -0700216}
217
218
219static void dm9000_inblk_16bit(void __iomem *reg, void *data, int count)
220{
Matthew Leachdaadaf62012-12-10 09:12:37 +0000221 ioread16_rep(reg, data, (count+1) >> 1);
Sascha Hauera1365272005-05-05 15:14:15 -0700222}
223
224static void dm9000_inblk_32bit(void __iomem *reg, void *data, int count)
225{
Matthew Leachdaadaf62012-12-10 09:12:37 +0000226 ioread32_rep(reg, data, (count+3) >> 2);
Sascha Hauera1365272005-05-05 15:14:15 -0700227}
228
229/* dump block from chip to null */
230
231static void dm9000_dumpblk_8bit(void __iomem *reg, int count)
232{
233 int i;
234 int tmp;
235
236 for (i = 0; i < count; i++)
237 tmp = readb(reg);
238}
239
240static void dm9000_dumpblk_16bit(void __iomem *reg, int count)
241{
242 int i;
243 int tmp;
244
245 count = (count + 1) >> 1;
246
247 for (i = 0; i < count; i++)
248 tmp = readw(reg);
249}
250
251static void dm9000_dumpblk_32bit(void __iomem *reg, int count)
252{
253 int i;
254 int tmp;
255
256 count = (count + 3) >> 2;
257
258 for (i = 0; i < count; i++)
259 tmp = readl(reg);
260}
261
Joseph CHANG6741f40d2013-03-28 23:13:42 +0000262/*
263 * Sleep, either by using msleep() or if we are suspending, then
264 * use mdelay() to sleep.
265 */
266static void dm9000_msleep(board_info_t *db, unsigned int ms)
267{
268 if (db->in_suspend)
269 mdelay(ms);
270 else
271 msleep(ms);
272}
273
274/* Read a word from phyxcer */
275static int
276dm9000_phy_read(struct net_device *dev, int phy_reg_unused, int reg)
277{
278 board_info_t *db = netdev_priv(dev);
279 unsigned long flags;
280 unsigned int reg_save;
281 int ret;
282
283 mutex_lock(&db->addr_lock);
284
285 spin_lock_irqsave(&db->lock, flags);
286
287 /* Save previous register address */
288 reg_save = readb(db->io_addr);
289
290 /* Fill the phyxcer register into REG_0C */
291 iow(db, DM9000_EPAR, DM9000_PHY | reg);
292
293 /* Issue phyxcer read command */
294 iow(db, DM9000_EPCR, EPCR_ERPRR | EPCR_EPOS);
295
296 writeb(reg_save, db->io_addr);
297 spin_unlock_irqrestore(&db->lock, flags);
298
299 dm9000_msleep(db, 1); /* Wait read complete */
300
301 spin_lock_irqsave(&db->lock, flags);
302 reg_save = readb(db->io_addr);
303
304 iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer read command */
305
306 /* The read data keeps on REG_0D & REG_0E */
307 ret = (ior(db, DM9000_EPDRH) << 8) | ior(db, DM9000_EPDRL);
308
309 /* restore the previous address */
310 writeb(reg_save, db->io_addr);
311 spin_unlock_irqrestore(&db->lock, flags);
312
313 mutex_unlock(&db->addr_lock);
314
315 dm9000_dbg(db, 5, "phy_read[%02x] -> %04x\n", reg, ret);
316 return ret;
317}
318
319/* Write a word to phyxcer */
320static void
321dm9000_phy_write(struct net_device *dev,
322 int phyaddr_unused, int reg, int value)
323{
324 board_info_t *db = netdev_priv(dev);
325 unsigned long flags;
326 unsigned long reg_save;
327
328 dm9000_dbg(db, 5, "phy_write[%02x] = %04x\n", reg, value);
329 mutex_lock(&db->addr_lock);
330
331 spin_lock_irqsave(&db->lock, flags);
332
333 /* Save previous register address */
334 reg_save = readb(db->io_addr);
335
336 /* Fill the phyxcer register into REG_0C */
337 iow(db, DM9000_EPAR, DM9000_PHY | reg);
338
339 /* Fill the written data into REG_0D & REG_0E */
340 iow(db, DM9000_EPDRL, value);
341 iow(db, DM9000_EPDRH, value >> 8);
342
343 /* Issue phyxcer write command */
344 iow(db, DM9000_EPCR, EPCR_EPOS | EPCR_ERPRW);
345
346 writeb(reg_save, db->io_addr);
347 spin_unlock_irqrestore(&db->lock, flags);
348
349 dm9000_msleep(db, 1); /* Wait write complete */
350
351 spin_lock_irqsave(&db->lock, flags);
352 reg_save = readb(db->io_addr);
353
354 iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer write command */
355
356 /* restore the previous address */
357 writeb(reg_save, db->io_addr);
358
359 spin_unlock_irqrestore(&db->lock, flags);
360 mutex_unlock(&db->addr_lock);
361}
362
Sascha Hauera1365272005-05-05 15:14:15 -0700363/* dm9000_set_io
364 *
365 * select the specified set of io routines to use with the
366 * device
367 */
368
369static void dm9000_set_io(struct board_info *db, int byte_width)
370{
371 /* use the size of the data resource to work out what IO
372 * routines we want to use
373 */
374
375 switch (byte_width) {
376 case 1:
377 db->dumpblk = dm9000_dumpblk_8bit;
378 db->outblk = dm9000_outblk_8bit;
379 db->inblk = dm9000_inblk_8bit;
380 break;
381
Sascha Hauera1365272005-05-05 15:14:15 -0700382
383 case 3:
Ben Dooksa76836f2008-02-05 00:02:02 +0000384 dev_dbg(db->dev, ": 3 byte IO, falling back to 16bit\n");
385 case 2:
Sascha Hauera1365272005-05-05 15:14:15 -0700386 db->dumpblk = dm9000_dumpblk_16bit;
387 db->outblk = dm9000_outblk_16bit;
388 db->inblk = dm9000_inblk_16bit;
389 break;
390
391 case 4:
392 default:
393 db->dumpblk = dm9000_dumpblk_32bit;
394 db->outblk = dm9000_outblk_32bit;
395 db->inblk = dm9000_inblk_32bit;
396 break;
397 }
398}
399
Ben Dooks8f5bf5f22008-05-08 11:36:42 +0100400static void dm9000_schedule_poll(board_info_t *db)
401{
Ben Dooks6d406b32008-06-24 22:15:59 +0100402 if (db->type == TYPE_DM9000E)
403 schedule_delayed_work(&db->phy_poll, HZ * 2);
Ben Dooks8f5bf5f22008-05-08 11:36:42 +0100404}
Sascha Hauera1365272005-05-05 15:14:15 -0700405
Ben Dooksf42d8ae2008-02-05 00:02:21 +0000406static int dm9000_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
407{
408 board_info_t *dm = to_dm9000_board(dev);
409
410 if (!netif_running(dev))
411 return -EINVAL;
412
413 return generic_mii_ioctl(&dm->mii, if_mii(req), cmd, NULL);
414}
415
Ben Dooksf8d79e72008-06-24 22:16:02 +0100416static unsigned int
417dm9000_read_locked(board_info_t *db, int reg)
418{
419 unsigned long flags;
420 unsigned int ret;
421
422 spin_lock_irqsave(&db->lock, flags);
423 ret = ior(db, reg);
424 spin_unlock_irqrestore(&db->lock, flags);
425
426 return ret;
427}
428
429static int dm9000_wait_eeprom(board_info_t *db)
430{
431 unsigned int status;
432 int timeout = 8; /* wait max 8msec */
433
434 /* The DM9000 data sheets say we should be able to
435 * poll the ERRE bit in EPCR to wait for the EEPROM
436 * operation. From testing several chips, this bit
437 * does not seem to work.
438 *
439 * We attempt to use the bit, but fall back to the
440 * timeout (which is why we do not return an error
441 * on expiry) to say that the EEPROM operation has
442 * completed.
443 */
444
445 while (1) {
446 status = dm9000_read_locked(db, DM9000_EPCR);
447
448 if ((status & EPCR_ERRE) == 0)
449 break;
450
Ben Dooks2fcf06c2008-06-24 22:16:05 +0100451 msleep(1);
452
Ben Dooksf8d79e72008-06-24 22:16:02 +0100453 if (timeout-- < 0) {
454 dev_dbg(db->dev, "timeout waiting EEPROM\n");
455 break;
456 }
457 }
458
459 return 0;
460}
461
462/*
463 * Read a word data from EEPROM
464 */
465static void
466dm9000_read_eeprom(board_info_t *db, int offset, u8 *to)
467{
468 unsigned long flags;
469
470 if (db->flags & DM9000_PLATF_NO_EEPROM) {
471 to[0] = 0xff;
472 to[1] = 0xff;
473 return;
474 }
475
476 mutex_lock(&db->addr_lock);
477
478 spin_lock_irqsave(&db->lock, flags);
479
480 iow(db, DM9000_EPAR, offset);
481 iow(db, DM9000_EPCR, EPCR_ERPRR);
482
483 spin_unlock_irqrestore(&db->lock, flags);
484
485 dm9000_wait_eeprom(db);
486
487 /* delay for at-least 150uS */
488 msleep(1);
489
490 spin_lock_irqsave(&db->lock, flags);
491
492 iow(db, DM9000_EPCR, 0x0);
493
494 to[0] = ior(db, DM9000_EPDRL);
495 to[1] = ior(db, DM9000_EPDRH);
496
497 spin_unlock_irqrestore(&db->lock, flags);
498
499 mutex_unlock(&db->addr_lock);
500}
501
502/*
503 * Write a word data to SROM
504 */
505static void
506dm9000_write_eeprom(board_info_t *db, int offset, u8 *data)
507{
508 unsigned long flags;
509
510 if (db->flags & DM9000_PLATF_NO_EEPROM)
511 return;
512
513 mutex_lock(&db->addr_lock);
514
515 spin_lock_irqsave(&db->lock, flags);
516 iow(db, DM9000_EPAR, offset);
517 iow(db, DM9000_EPDRH, data[1]);
518 iow(db, DM9000_EPDRL, data[0]);
519 iow(db, DM9000_EPCR, EPCR_WEP | EPCR_ERPRW);
520 spin_unlock_irqrestore(&db->lock, flags);
521
522 dm9000_wait_eeprom(db);
523
524 mdelay(1); /* wait at least 150uS to clear */
525
526 spin_lock_irqsave(&db->lock, flags);
527 iow(db, DM9000_EPCR, 0);
528 spin_unlock_irqrestore(&db->lock, flags);
529
530 mutex_unlock(&db->addr_lock);
531}
532
Ben Dooks7da99852008-02-05 00:02:06 +0000533/* ethtool ops */
534
535static void dm9000_get_drvinfo(struct net_device *dev,
536 struct ethtool_drvinfo *info)
537{
538 board_info_t *dm = to_dm9000_board(dev);
539
Jiri Pirko7826d432013-01-06 00:44:26 +0000540 strlcpy(info->driver, CARDNAME, sizeof(info->driver));
541 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
542 strlcpy(info->bus_info, to_platform_device(dm->dev)->name,
543 sizeof(info->bus_info));
Ben Dooks7da99852008-02-05 00:02:06 +0000544}
545
Ben Dookse662ee02008-02-05 00:02:12 +0000546static u32 dm9000_get_msglevel(struct net_device *dev)
547{
548 board_info_t *dm = to_dm9000_board(dev);
549
550 return dm->msg_enable;
551}
552
553static void dm9000_set_msglevel(struct net_device *dev, u32 value)
554{
555 board_info_t *dm = to_dm9000_board(dev);
556
557 dm->msg_enable = value;
558}
559
Ben Dooks7da99852008-02-05 00:02:06 +0000560static int dm9000_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
561{
562 board_info_t *dm = to_dm9000_board(dev);
Ben Dooks7da99852008-02-05 00:02:06 +0000563
Ben Dooks7da99852008-02-05 00:02:06 +0000564 mii_ethtool_gset(&dm->mii, cmd);
Ben Dooks7da99852008-02-05 00:02:06 +0000565 return 0;
566}
567
568static int dm9000_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
569{
570 board_info_t *dm = to_dm9000_board(dev);
Ben Dooks7da99852008-02-05 00:02:06 +0000571
Ben Dooks9a2f0372008-02-05 00:02:10 +0000572 return mii_ethtool_sset(&dm->mii, cmd);
Ben Dooks7da99852008-02-05 00:02:06 +0000573}
574
575static int dm9000_nway_reset(struct net_device *dev)
576{
577 board_info_t *dm = to_dm9000_board(dev);
578 return mii_nway_restart(&dm->mii);
579}
580
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000581static int dm9000_set_features(struct net_device *dev,
582 netdev_features_t features)
Yeasah Pell5dcc60b2009-07-06 18:12:33 -0700583{
584 board_info_t *dm = to_dm9000_board(dev);
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000585 netdev_features_t changed = dev->features ^ features;
Baruch Siach380fefb2010-05-17 17:45:48 -0700586 unsigned long flags;
Michał Mirosławc88fcb32011-04-15 04:50:49 +0000587
588 if (!(changed & NETIF_F_RXCSUM))
589 return 0;
Baruch Siach380fefb2010-05-17 17:45:48 -0700590
591 spin_lock_irqsave(&dm->lock, flags);
Michał Mirosławc88fcb32011-04-15 04:50:49 +0000592 iow(dm, DM9000_RCSR, (features & NETIF_F_RXCSUM) ? RCSR_CSUM : 0);
Baruch Siach380fefb2010-05-17 17:45:48 -0700593 spin_unlock_irqrestore(&dm->lock, flags);
594
Michał Mirosławc88fcb32011-04-15 04:50:49 +0000595 return 0;
Yeasah Pell5dcc60b2009-07-06 18:12:33 -0700596}
597
Ben Dooks7da99852008-02-05 00:02:06 +0000598static u32 dm9000_get_link(struct net_device *dev)
599{
600 board_info_t *dm = to_dm9000_board(dev);
Ben Dooksaa1eb452008-06-24 22:16:03 +0100601 u32 ret;
602
603 if (dm->flags & DM9000_PLATF_EXT_PHY)
604 ret = mii_link_ok(&dm->mii);
605 else
606 ret = dm9000_read_locked(dm, DM9000_NSR) & NSR_LINKST ? 1 : 0;
607
608 return ret;
Ben Dooks7da99852008-02-05 00:02:06 +0000609}
610
Ben Dooks29d52e52008-02-05 00:02:11 +0000611#define DM_EEPROM_MAGIC (0x444D394B)
612
613static int dm9000_get_eeprom_len(struct net_device *dev)
614{
615 return 128;
616}
617
618static int dm9000_get_eeprom(struct net_device *dev,
619 struct ethtool_eeprom *ee, u8 *data)
620{
621 board_info_t *dm = to_dm9000_board(dev);
622 int offset = ee->offset;
623 int len = ee->len;
624 int i;
625
626 /* EEPROM access is aligned to two bytes */
627
628 if ((len & 1) != 0 || (offset & 1) != 0)
629 return -EINVAL;
630
Ben Dooksbb44fb702008-02-05 00:02:20 +0000631 if (dm->flags & DM9000_PLATF_NO_EEPROM)
632 return -ENOENT;
633
Ben Dooks29d52e52008-02-05 00:02:11 +0000634 ee->magic = DM_EEPROM_MAGIC;
635
636 for (i = 0; i < len; i += 2)
637 dm9000_read_eeprom(dm, (offset + i) / 2, data + i);
638
639 return 0;
640}
641
642static int dm9000_set_eeprom(struct net_device *dev,
643 struct ethtool_eeprom *ee, u8 *data)
644{
645 board_info_t *dm = to_dm9000_board(dev);
646 int offset = ee->offset;
647 int len = ee->len;
Ben Dooks40d15cd2011-06-10 00:50:32 +0000648 int done;
Ben Dooks29d52e52008-02-05 00:02:11 +0000649
650 /* EEPROM access is aligned to two bytes */
651
Ben Dooksbb44fb702008-02-05 00:02:20 +0000652 if (dm->flags & DM9000_PLATF_NO_EEPROM)
653 return -ENOENT;
654
Ben Dooks29d52e52008-02-05 00:02:11 +0000655 if (ee->magic != DM_EEPROM_MAGIC)
656 return -EINVAL;
657
Ben Dooks40d15cd2011-06-10 00:50:32 +0000658 while (len > 0) {
659 if (len & 1 || offset & 1) {
660 int which = offset & 1;
661 u8 tmp[2];
662
663 dm9000_read_eeprom(dm, offset / 2, tmp);
664 tmp[which] = *data;
665 dm9000_write_eeprom(dm, offset / 2, tmp);
666
667 done = 1;
668 } else {
669 dm9000_write_eeprom(dm, offset / 2, data);
670 done = 2;
671 }
672
673 data += done;
674 offset += done;
675 len -= done;
676 }
Ben Dooks29d52e52008-02-05 00:02:11 +0000677
678 return 0;
679}
680
Ben Dooksc029f442009-11-10 07:22:24 +0000681static void dm9000_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
682{
683 board_info_t *dm = to_dm9000_board(dev);
684
685 memset(w, 0, sizeof(struct ethtool_wolinfo));
686
687 /* note, we could probably support wake-phy too */
688 w->supported = dm->wake_supported ? WAKE_MAGIC : 0;
689 w->wolopts = dm->wake_state;
690}
691
692static int dm9000_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
693{
694 board_info_t *dm = to_dm9000_board(dev);
695 unsigned long flags;
696 u32 opts = w->wolopts;
697 u32 wcr = 0;
698
699 if (!dm->wake_supported)
700 return -EOPNOTSUPP;
701
702 if (opts & ~WAKE_MAGIC)
703 return -EINVAL;
704
705 if (opts & WAKE_MAGIC)
706 wcr |= WCR_MAGICEN;
707
708 mutex_lock(&dm->addr_lock);
709
710 spin_lock_irqsave(&dm->lock, flags);
711 iow(dm, DM9000_WCR, wcr);
712 spin_unlock_irqrestore(&dm->lock, flags);
713
714 mutex_unlock(&dm->addr_lock);
715
716 if (dm->wake_state != opts) {
717 /* change in wol state, update IRQ state */
718
719 if (!dm->wake_state)
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200720 irq_set_irq_wake(dm->irq_wake, 1);
Mark Brown83b98fb2011-11-21 07:51:56 +0000721 else if (dm->wake_state && !opts)
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200722 irq_set_irq_wake(dm->irq_wake, 0);
Ben Dooksc029f442009-11-10 07:22:24 +0000723 }
724
725 dm->wake_state = opts;
726 return 0;
727}
728
Ben Dooks7da99852008-02-05 00:02:06 +0000729static const struct ethtool_ops dm9000_ethtool_ops = {
730 .get_drvinfo = dm9000_get_drvinfo,
731 .get_settings = dm9000_get_settings,
732 .set_settings = dm9000_set_settings,
Ben Dookse662ee02008-02-05 00:02:12 +0000733 .get_msglevel = dm9000_get_msglevel,
734 .set_msglevel = dm9000_set_msglevel,
Ben Dooks7da99852008-02-05 00:02:06 +0000735 .nway_reset = dm9000_nway_reset,
736 .get_link = dm9000_get_link,
Ben Dooksc029f442009-11-10 07:22:24 +0000737 .get_wol = dm9000_get_wol,
738 .set_wol = dm9000_set_wol,
Ben Dooks29d52e52008-02-05 00:02:11 +0000739 .get_eeprom_len = dm9000_get_eeprom_len,
740 .get_eeprom = dm9000_get_eeprom,
741 .set_eeprom = dm9000_set_eeprom,
Ben Dooks7da99852008-02-05 00:02:06 +0000742};
743
Ben Dooksf8dd0ec2008-06-24 22:16:04 +0100744static void dm9000_show_carrier(board_info_t *db,
745 unsigned carrier, unsigned nsr)
746{
747 struct net_device *ndev = db->ndev;
748 unsigned ncr = dm9000_read_locked(db, DM9000_NCR);
749
750 if (carrier)
751 dev_info(db->dev, "%s: link up, %dMbps, %s-duplex, no LPA\n",
752 ndev->name, (nsr & NSR_SPEED) ? 10 : 100,
753 (ncr & NCR_FDX) ? "full" : "half");
754 else
755 dev_info(db->dev, "%s: link down\n", ndev->name);
756}
757
Ben Dooks8f5bf5f22008-05-08 11:36:42 +0100758static void
759dm9000_poll_work(struct work_struct *w)
760{
Jean Delvarebf6aede2009-04-02 16:56:54 -0700761 struct delayed_work *dw = to_delayed_work(w);
Ben Dooks8f5bf5f22008-05-08 11:36:42 +0100762 board_info_t *db = container_of(dw, board_info_t, phy_poll);
Ben Dooksf8dd0ec2008-06-24 22:16:04 +0100763 struct net_device *ndev = db->ndev;
Ben Dooks8f5bf5f22008-05-08 11:36:42 +0100764
Ben Dooksf8dd0ec2008-06-24 22:16:04 +0100765 if (db->flags & DM9000_PLATF_SIMPLE_PHY &&
766 !(db->flags & DM9000_PLATF_EXT_PHY)) {
767 unsigned nsr = dm9000_read_locked(db, DM9000_NSR);
768 unsigned old_carrier = netif_carrier_ok(ndev) ? 1 : 0;
769 unsigned new_carrier;
770
771 new_carrier = (nsr & NSR_LINKST) ? 1 : 0;
772
773 if (old_carrier != new_carrier) {
774 if (netif_msg_link(db))
775 dm9000_show_carrier(db, new_carrier, nsr);
776
777 if (!new_carrier)
778 netif_carrier_off(ndev);
779 else
780 netif_carrier_on(ndev);
781 }
782 } else
783 mii_check_media(&db->mii, netif_msg_link(db), 0);
Ben Dooks8f5bf5f22008-05-08 11:36:42 +0100784
Ben Dooksf8dd0ec2008-06-24 22:16:04 +0100785 if (netif_running(ndev))
Ben Dooks8f5bf5f22008-05-08 11:36:42 +0100786 dm9000_schedule_poll(db);
787}
Ben Dooks7da99852008-02-05 00:02:06 +0000788
Sascha Hauera1365272005-05-05 15:14:15 -0700789/* dm9000_release_board
790 *
791 * release a board, and any mapped resources
792 */
793
794static void
795dm9000_release_board(struct platform_device *pdev, struct board_info *db)
796{
Sascha Hauera1365272005-05-05 15:14:15 -0700797 /* unmap our resources */
798
799 iounmap(db->io_addr);
800 iounmap(db->io_data);
801
802 /* release the resources */
803
Ben Dooks9088fa4f2008-06-24 22:16:00 +0100804 release_resource(db->data_req);
805 kfree(db->data_req);
Sascha Hauera1365272005-05-05 15:14:15 -0700806
Ben Dooks9088fa4f2008-06-24 22:16:00 +0100807 release_resource(db->addr_req);
808 kfree(db->addr_req);
Sascha Hauera1365272005-05-05 15:14:15 -0700809}
810
Ben Dooks6d406b32008-06-24 22:15:59 +0100811static unsigned char dm9000_type_to_char(enum dm9000_type type)
812{
813 switch (type) {
814 case TYPE_DM9000E: return 'e';
815 case TYPE_DM9000A: return 'a';
816 case TYPE_DM9000B: return 'b';
817 }
818
819 return '?';
820}
821
Ben Dooksf8d79e72008-06-24 22:16:02 +0100822/*
823 * Set DM9000 multicast address
824 */
825static void
Baruch Siach380fefb2010-05-17 17:45:48 -0700826dm9000_hash_table_unlocked(struct net_device *dev)
Ben Dooksf8d79e72008-06-24 22:16:02 +0100827{
Wang Chen4cf16532008-11-12 23:38:14 -0800828 board_info_t *db = netdev_priv(dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +0000829 struct netdev_hw_addr *ha;
Ben Dooksf8d79e72008-06-24 22:16:02 +0100830 int i, oft;
831 u32 hash_val;
Emilio López35e729a2013-05-17 10:42:55 +0000832 u16 hash_table[4] = { 0, 0, 0, 0x8000 }; /* broadcast address */
Ben Dooksf8d79e72008-06-24 22:16:02 +0100833 u8 rcr = RCR_DIS_LONG | RCR_DIS_CRC | RCR_RXEN;
Ben Dooksf8d79e72008-06-24 22:16:02 +0100834
835 dm9000_dbg(db, 1, "entering %s\n", __func__);
836
Ben Dooksf8d79e72008-06-24 22:16:02 +0100837 for (i = 0, oft = DM9000_PAR; i < 6; i++, oft++)
838 iow(db, oft, dev->dev_addr[i]);
839
Ben Dooksf8d79e72008-06-24 22:16:02 +0100840 if (dev->flags & IFF_PROMISC)
841 rcr |= RCR_PRMSC;
842
843 if (dev->flags & IFF_ALLMULTI)
844 rcr |= RCR_ALL;
845
846 /* the multicast address in Hash Table : 64 bits */
Jiri Pirko22bedad32010-04-01 21:22:57 +0000847 netdev_for_each_mc_addr(ha, dev) {
848 hash_val = ether_crc_le(6, ha->addr) & 0x3f;
Ben Dooksf8d79e72008-06-24 22:16:02 +0100849 hash_table[hash_val / 16] |= (u16) 1 << (hash_val % 16);
850 }
851
852 /* Write the hash table to MAC MD table */
853 for (i = 0, oft = DM9000_MAR; i < 4; i++) {
854 iow(db, oft++, hash_table[i]);
855 iow(db, oft++, hash_table[i] >> 8);
856 }
857
858 iow(db, DM9000_RCR, rcr);
Baruch Siach380fefb2010-05-17 17:45:48 -0700859}
860
861static void
862dm9000_hash_table(struct net_device *dev)
863{
864 board_info_t *db = netdev_priv(dev);
865 unsigned long flags;
866
867 spin_lock_irqsave(&db->lock, flags);
868 dm9000_hash_table_unlocked(dev);
Ben Dooksf8d79e72008-06-24 22:16:02 +0100869 spin_unlock_irqrestore(&db->lock, flags);
870}
871
872/*
Eric Dumazet1ae5dc32010-05-10 05:01:31 -0700873 * Initialize dm9000 board
Ben Dooksf8d79e72008-06-24 22:16:02 +0100874 */
875static void
876dm9000_init_dm9000(struct net_device *dev)
877{
Wang Chen4cf16532008-11-12 23:38:14 -0800878 board_info_t *db = netdev_priv(dev);
Ben Dooksf8d79e72008-06-24 22:16:02 +0100879 unsigned int imr;
Ben Dooksc029f442009-11-10 07:22:24 +0000880 unsigned int ncr;
Ben Dooksf8d79e72008-06-24 22:16:02 +0100881
882 dm9000_dbg(db, 1, "entering %s\n", __func__);
883
884 /* I/O mode */
885 db->io_mode = ior(db, DM9000_ISR) >> 6; /* ISR bit7:6 keeps I/O mode */
886
Yeasah Pell5dcc60b2009-07-06 18:12:33 -0700887 /* Checksum mode */
Michał Mirosławc88fcb32011-04-15 04:50:49 +0000888 if (dev->hw_features & NETIF_F_RXCSUM)
Mark Brown56d37f12011-04-18 01:04:37 +0000889 iow(db, DM9000_RCSR,
Michał Mirosławc88fcb32011-04-15 04:50:49 +0000890 (dev->features & NETIF_F_RXCSUM) ? RCSR_CSUM : 0);
Yeasah Pell5dcc60b2009-07-06 18:12:33 -0700891
Ben Dooksf8d79e72008-06-24 22:16:02 +0100892 iow(db, DM9000_GPCR, GPCR_GEP_CNTL); /* Let GPIO0 output */
Nikita Kiryanov677d7d22013-10-16 11:41:32 +0300893 iow(db, DM9000_GPR, 0);
Ben Dooksf8d79e72008-06-24 22:16:02 +0100894
Nikita Kiryanov6649b202013-10-16 11:41:31 +0300895 /* If we are dealing with DM9000B, some extra steps are required: a
896 * manual phy reset, and setting init params.
897 */
898 if (db->type == TYPE_DM9000B) {
899 dm9000_phy_write(dev, 0, MII_BMCR, BMCR_RESET);
900 dm9000_phy_write(dev, 0, MII_DM_DSPCR, DSPCR_INIT_PARAM);
901 }
Joseph CHANG6741f40d2013-03-28 23:13:42 +0000902
Ben Dooksc029f442009-11-10 07:22:24 +0000903 ncr = (db->flags & DM9000_PLATF_EXT_PHY) ? NCR_EXT_PHY : 0;
904
905 /* if wol is needed, then always set NCR_WAKEEN otherwise we end
906 * up dumping the wake events if we disable this. There is already
907 * a wake-mask in DM9000_WCR */
908 if (db->wake_supported)
909 ncr |= NCR_WAKEEN;
910
911 iow(db, DM9000_NCR, ncr);
Ben Dooksf8d79e72008-06-24 22:16:02 +0100912
913 /* Program operating register */
914 iow(db, DM9000_TCR, 0); /* TX Polling clear */
915 iow(db, DM9000_BPTR, 0x3f); /* Less 3Kb, 200us */
916 iow(db, DM9000_FCR, 0xff); /* Flow Control */
917 iow(db, DM9000_SMCR, 0); /* Special Mode */
918 /* clear TX status */
919 iow(db, DM9000_NSR, NSR_WAKEST | NSR_TX2END | NSR_TX1END);
920 iow(db, DM9000_ISR, ISR_CLR_STATUS); /* Clear interrupt status */
921
922 /* Set address filter table */
Baruch Siach380fefb2010-05-17 17:45:48 -0700923 dm9000_hash_table_unlocked(dev);
Ben Dooksf8d79e72008-06-24 22:16:02 +0100924
925 imr = IMR_PAR | IMR_PTM | IMR_PRM;
926 if (db->type != TYPE_DM9000E)
927 imr |= IMR_LNKCHNG;
928
929 db->imr_all = imr;
930
931 /* Enable TX/RX interrupt mask */
932 iow(db, DM9000_IMR, imr);
933
934 /* Init Driver variable */
935 db->tx_pkt_cnt = 0;
936 db->queue_pkt_len = 0;
Eric Dumazet1ae5dc32010-05-10 05:01:31 -0700937 dev->trans_start = jiffies;
Ben Dooksf8d79e72008-06-24 22:16:02 +0100938}
939
940/* Our watchdog timed out. Called by the networking layer */
941static void dm9000_timeout(struct net_device *dev)
942{
Wang Chen4cf16532008-11-12 23:38:14 -0800943 board_info_t *db = netdev_priv(dev);
Ben Dooksf8d79e72008-06-24 22:16:02 +0100944 u8 reg_save;
945 unsigned long flags;
946
947 /* Save previous register address */
Ben Dooksf8d79e72008-06-24 22:16:02 +0100948 spin_lock_irqsave(&db->lock, flags);
Henry Nestler8dde9242011-02-20 11:44:58 +0000949 reg_save = readb(db->io_addr);
Ben Dooksf8d79e72008-06-24 22:16:02 +0100950
951 netif_stop_queue(dev);
952 dm9000_reset(db);
953 dm9000_init_dm9000(dev);
954 /* We can accept TX packets again */
Eric Dumazet1ae5dc32010-05-10 05:01:31 -0700955 dev->trans_start = jiffies; /* prevent tx timeout */
Ben Dooksf8d79e72008-06-24 22:16:02 +0100956 netif_wake_queue(dev);
957
958 /* Restore previous register address */
959 writeb(reg_save, db->io_addr);
960 spin_unlock_irqrestore(&db->lock, flags);
961}
962
Yeasah Pell5dcc60b2009-07-06 18:12:33 -0700963static void dm9000_send_packet(struct net_device *dev,
964 int ip_summed,
965 u16 pkt_len)
966{
967 board_info_t *dm = to_dm9000_board(dev);
968
969 /* The DM9000 is not smart enough to leave fragmented packets alone. */
970 if (dm->ip_summed != ip_summed) {
971 if (ip_summed == CHECKSUM_NONE)
972 iow(dm, DM9000_TCCR, 0);
973 else
974 iow(dm, DM9000_TCCR, TCCR_IP | TCCR_UDP | TCCR_TCP);
975 dm->ip_summed = ip_summed;
976 }
977
978 /* Set TX length to DM9000 */
979 iow(dm, DM9000_TXPLL, pkt_len);
980 iow(dm, DM9000_TXPLH, pkt_len >> 8);
981
982 /* Issue TX polling command */
983 iow(dm, DM9000_TCR, TCR_TXREQ); /* Cleared after TX complete */
984}
985
Ben Dooksf8d79e72008-06-24 22:16:02 +0100986/*
987 * Hardware start transmission.
988 * Send a packet to media from the upper layer.
989 */
990static int
991dm9000_start_xmit(struct sk_buff *skb, struct net_device *dev)
992{
993 unsigned long flags;
Wang Chen4cf16532008-11-12 23:38:14 -0800994 board_info_t *db = netdev_priv(dev);
Ben Dooksf8d79e72008-06-24 22:16:02 +0100995
996 dm9000_dbg(db, 3, "%s:\n", __func__);
997
998 if (db->tx_pkt_cnt > 1)
Patrick McHardy5b548142009-06-12 06:22:29 +0000999 return NETDEV_TX_BUSY;
Ben Dooksf8d79e72008-06-24 22:16:02 +01001000
1001 spin_lock_irqsave(&db->lock, flags);
1002
1003 /* Move data to DM9000 TX RAM */
1004 writeb(DM9000_MWCMD, db->io_addr);
1005
1006 (db->outblk)(db->io_data, skb->data, skb->len);
1007 dev->stats.tx_bytes += skb->len;
1008
1009 db->tx_pkt_cnt++;
1010 /* TX control: First packet immediately send, second packet queue */
1011 if (db->tx_pkt_cnt == 1) {
Yeasah Pell5dcc60b2009-07-06 18:12:33 -07001012 dm9000_send_packet(dev, skb->ip_summed, skb->len);
Ben Dooksf8d79e72008-06-24 22:16:02 +01001013 } else {
1014 /* Second packet */
1015 db->queue_pkt_len = skb->len;
Yeasah Pell5dcc60b2009-07-06 18:12:33 -07001016 db->queue_ip_summed = skb->ip_summed;
Ben Dooksf8d79e72008-06-24 22:16:02 +01001017 netif_stop_queue(dev);
1018 }
1019
1020 spin_unlock_irqrestore(&db->lock, flags);
1021
1022 /* free this SKB */
1023 dev_kfree_skb(skb);
1024
Patrick McHardy6ed10652009-06-23 06:03:08 +00001025 return NETDEV_TX_OK;
Ben Dooksf8d79e72008-06-24 22:16:02 +01001026}
1027
1028/*
1029 * DM9000 interrupt handler
1030 * receive the packet to upper layer, free the transmitted packet
1031 */
1032
1033static void dm9000_tx_done(struct net_device *dev, board_info_t *db)
1034{
1035 int tx_status = ior(db, DM9000_NSR); /* Got TX status */
1036
1037 if (tx_status & (NSR_TX2END | NSR_TX1END)) {
1038 /* One packet sent complete */
1039 db->tx_pkt_cnt--;
1040 dev->stats.tx_packets++;
1041
1042 if (netif_msg_tx_done(db))
1043 dev_dbg(db->dev, "tx done, NSR %02x\n", tx_status);
1044
1045 /* Queue packet check & send */
Yeasah Pell5dcc60b2009-07-06 18:12:33 -07001046 if (db->tx_pkt_cnt > 0)
1047 dm9000_send_packet(dev, db->queue_ip_summed,
1048 db->queue_pkt_len);
Ben Dooksf8d79e72008-06-24 22:16:02 +01001049 netif_wake_queue(dev);
1050 }
1051}
1052
1053struct dm9000_rxhdr {
1054 u8 RxPktReady;
1055 u8 RxStatus;
1056 __le16 RxLen;
Eric Dumazetba2d3582010-06-02 18:10:09 +00001057} __packed;
Ben Dooksf8d79e72008-06-24 22:16:02 +01001058
1059/*
1060 * Received a packet and pass to upper layer
1061 */
1062static void
1063dm9000_rx(struct net_device *dev)
1064{
Wang Chen4cf16532008-11-12 23:38:14 -08001065 board_info_t *db = netdev_priv(dev);
Ben Dooksf8d79e72008-06-24 22:16:02 +01001066 struct dm9000_rxhdr rxhdr;
1067 struct sk_buff *skb;
1068 u8 rxbyte, *rdptr;
1069 bool GoodPacket;
1070 int RxLen;
1071
1072 /* Check packet ready or not */
1073 do {
1074 ior(db, DM9000_MRCMDX); /* Dummy read */
1075
1076 /* Get most updated data */
1077 rxbyte = readb(db->io_data);
1078
1079 /* Status check: this byte must be 0 or 1 */
Yeasah Pell5dcc60b2009-07-06 18:12:33 -07001080 if (rxbyte & DM9000_PKT_ERR) {
Ben Dooksf8d79e72008-06-24 22:16:02 +01001081 dev_warn(db->dev, "status check fail: %d\n", rxbyte);
1082 iow(db, DM9000_RCR, 0x00); /* Stop Device */
1083 iow(db, DM9000_ISR, IMR_PAR); /* Stop INT request */
1084 return;
1085 }
1086
Yeasah Pell5dcc60b2009-07-06 18:12:33 -07001087 if (!(rxbyte & DM9000_PKT_RDY))
Ben Dooksf8d79e72008-06-24 22:16:02 +01001088 return;
1089
1090 /* A packet ready now & Get status/length */
1091 GoodPacket = true;
1092 writeb(DM9000_MRCMD, db->io_addr);
1093
1094 (db->inblk)(db->io_data, &rxhdr, sizeof(rxhdr));
1095
1096 RxLen = le16_to_cpu(rxhdr.RxLen);
1097
1098 if (netif_msg_rx_status(db))
1099 dev_dbg(db->dev, "RX: status %02x, length %04x\n",
1100 rxhdr.RxStatus, RxLen);
1101
1102 /* Packet Status check */
1103 if (RxLen < 0x40) {
1104 GoodPacket = false;
1105 if (netif_msg_rx_err(db))
1106 dev_dbg(db->dev, "RX: Bad Packet (runt)\n");
1107 }
1108
1109 if (RxLen > DM9000_PKT_MAX) {
1110 dev_dbg(db->dev, "RST: RX Len:%x\n", RxLen);
1111 }
1112
Ben Dooksf8e5e772008-07-17 20:29:13 +01001113 /* rxhdr.RxStatus is identical to RSR register. */
1114 if (rxhdr.RxStatus & (RSR_FOE | RSR_CE | RSR_AE |
1115 RSR_PLE | RSR_RWTO |
1116 RSR_LCS | RSR_RF)) {
Ben Dooksf8d79e72008-06-24 22:16:02 +01001117 GoodPacket = false;
Ben Dooksf8e5e772008-07-17 20:29:13 +01001118 if (rxhdr.RxStatus & RSR_FOE) {
Ben Dooksf8d79e72008-06-24 22:16:02 +01001119 if (netif_msg_rx_err(db))
1120 dev_dbg(db->dev, "fifo error\n");
1121 dev->stats.rx_fifo_errors++;
1122 }
Ben Dooksf8e5e772008-07-17 20:29:13 +01001123 if (rxhdr.RxStatus & RSR_CE) {
Ben Dooksf8d79e72008-06-24 22:16:02 +01001124 if (netif_msg_rx_err(db))
1125 dev_dbg(db->dev, "crc error\n");
1126 dev->stats.rx_crc_errors++;
1127 }
Ben Dooksf8e5e772008-07-17 20:29:13 +01001128 if (rxhdr.RxStatus & RSR_RF) {
Ben Dooksf8d79e72008-06-24 22:16:02 +01001129 if (netif_msg_rx_err(db))
1130 dev_dbg(db->dev, "length error\n");
1131 dev->stats.rx_length_errors++;
1132 }
1133 }
1134
1135 /* Move data from DM9000 */
Joe Perches8e95a202009-12-03 07:58:21 +00001136 if (GoodPacket &&
Pradeep A Dalvi21a4e462012-02-05 02:50:10 +00001137 ((skb = netdev_alloc_skb(dev, RxLen + 4)) != NULL)) {
Ben Dooksf8d79e72008-06-24 22:16:02 +01001138 skb_reserve(skb, 2);
1139 rdptr = (u8 *) skb_put(skb, RxLen - 4);
1140
1141 /* Read received packet from RX SRAM */
1142
1143 (db->inblk)(db->io_data, rdptr, RxLen);
1144 dev->stats.rx_bytes += RxLen;
1145
1146 /* Pass to upper layer */
1147 skb->protocol = eth_type_trans(skb, dev);
Michał Mirosławc88fcb32011-04-15 04:50:49 +00001148 if (dev->features & NETIF_F_RXCSUM) {
Yeasah Pell5dcc60b2009-07-06 18:12:33 -07001149 if ((((rxbyte & 0x1c) << 3) & rxbyte) == 0)
1150 skb->ip_summed = CHECKSUM_UNNECESSARY;
1151 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001152 skb_checksum_none_assert(skb);
Yeasah Pell5dcc60b2009-07-06 18:12:33 -07001153 }
Ben Dooksf8d79e72008-06-24 22:16:02 +01001154 netif_rx(skb);
1155 dev->stats.rx_packets++;
1156
1157 } else {
1158 /* need to dump the packet's data */
1159
1160 (db->dumpblk)(db->io_data, RxLen);
1161 }
Yeasah Pell5dcc60b2009-07-06 18:12:33 -07001162 } while (rxbyte & DM9000_PKT_RDY);
Ben Dooksf8d79e72008-06-24 22:16:02 +01001163}
1164
1165static irqreturn_t dm9000_interrupt(int irq, void *dev_id)
1166{
1167 struct net_device *dev = dev_id;
Wang Chen4cf16532008-11-12 23:38:14 -08001168 board_info_t *db = netdev_priv(dev);
Ben Dooksf8d79e72008-06-24 22:16:02 +01001169 int int_status;
David Brownelle3162d32009-03-22 21:28:39 -07001170 unsigned long flags;
Ben Dooksf8d79e72008-06-24 22:16:02 +01001171 u8 reg_save;
1172
1173 dm9000_dbg(db, 3, "entering %s\n", __func__);
1174
1175 /* A real interrupt coming */
1176
David Brownelle3162d32009-03-22 21:28:39 -07001177 /* holders of db->lock must always block IRQs */
1178 spin_lock_irqsave(&db->lock, flags);
Ben Dooksf8d79e72008-06-24 22:16:02 +01001179
1180 /* Save previous register address */
1181 reg_save = readb(db->io_addr);
1182
1183 /* Disable all interrupts */
1184 iow(db, DM9000_IMR, IMR_PAR);
1185
1186 /* Got DM9000 interrupt status */
1187 int_status = ior(db, DM9000_ISR); /* Got ISR */
1188 iow(db, DM9000_ISR, int_status); /* Clear ISR status */
1189
1190 if (netif_msg_intr(db))
1191 dev_dbg(db->dev, "interrupt status %02x\n", int_status);
1192
1193 /* Received the coming packet */
1194 if (int_status & ISR_PRS)
1195 dm9000_rx(dev);
1196
1197 /* Trnasmit Interrupt check */
1198 if (int_status & ISR_PTS)
1199 dm9000_tx_done(dev, db);
1200
1201 if (db->type != TYPE_DM9000E) {
1202 if (int_status & ISR_LNKCHNG) {
1203 /* fire a link-change request */
1204 schedule_delayed_work(&db->phy_poll, 1);
1205 }
1206 }
1207
1208 /* Re-enable interrupt mask */
1209 iow(db, DM9000_IMR, db->imr_all);
1210
1211 /* Restore previous register address */
1212 writeb(reg_save, db->io_addr);
1213
David Brownelle3162d32009-03-22 21:28:39 -07001214 spin_unlock_irqrestore(&db->lock, flags);
Ben Dooksf8d79e72008-06-24 22:16:02 +01001215
1216 return IRQ_HANDLED;
1217}
1218
Ben Dooksc029f442009-11-10 07:22:24 +00001219static irqreturn_t dm9000_wol_interrupt(int irq, void *dev_id)
1220{
1221 struct net_device *dev = dev_id;
1222 board_info_t *db = netdev_priv(dev);
1223 unsigned long flags;
1224 unsigned nsr, wcr;
1225
1226 spin_lock_irqsave(&db->lock, flags);
1227
1228 nsr = ior(db, DM9000_NSR);
1229 wcr = ior(db, DM9000_WCR);
1230
1231 dev_dbg(db->dev, "%s: NSR=0x%02x, WCR=0x%02x\n", __func__, nsr, wcr);
1232
1233 if (nsr & NSR_WAKEST) {
1234 /* clear, so we can avoid */
1235 iow(db, DM9000_NSR, NSR_WAKEST);
1236
1237 if (wcr & WCR_LINKST)
1238 dev_info(db->dev, "wake by link status change\n");
1239 if (wcr & WCR_SAMPLEST)
1240 dev_info(db->dev, "wake by sample packet\n");
1241 if (wcr & WCR_MAGICST )
1242 dev_info(db->dev, "wake by magic packet\n");
1243 if (!(wcr & (WCR_LINKST | WCR_SAMPLEST | WCR_MAGICST)))
1244 dev_err(db->dev, "wake signalled with no reason? "
1245 "NSR=0x%02x, WSR=0x%02x\n", nsr, wcr);
1246
1247 }
1248
1249 spin_unlock_irqrestore(&db->lock, flags);
1250
1251 return (nsr & NSR_WAKEST) ? IRQ_HANDLED : IRQ_NONE;
1252}
1253
Ben Dooksf8d79e72008-06-24 22:16:02 +01001254#ifdef CONFIG_NET_POLL_CONTROLLER
1255/*
1256 *Used by netconsole
1257 */
1258static void dm9000_poll_controller(struct net_device *dev)
1259{
1260 disable_irq(dev->irq);
1261 dm9000_interrupt(dev->irq, dev);
1262 enable_irq(dev->irq);
1263}
1264#endif
1265
1266/*
1267 * Open the interface.
1268 * The interface is opened whenever "ifconfig" actives it.
1269 */
1270static int
1271dm9000_open(struct net_device *dev)
1272{
Wang Chen4cf16532008-11-12 23:38:14 -08001273 board_info_t *db = netdev_priv(dev);
Ben Dooksf8d79e72008-06-24 22:16:02 +01001274 unsigned long irqflags = db->irq_res->flags & IRQF_TRIGGER_MASK;
1275
1276 if (netif_msg_ifup(db))
1277 dev_dbg(db->dev, "enabling %s\n", dev->name);
1278
1279 /* If there is no IRQ type specified, default to something that
1280 * may work, and tell the user that this is a problem */
1281
Ben Dooks6ff4ff02008-06-24 22:16:07 +01001282 if (irqflags == IRQF_TRIGGER_NONE)
Ben Dooksf8d79e72008-06-24 22:16:02 +01001283 dev_warn(db->dev, "WARNING: no IRQ resource flags set.\n");
Ben Dooks6ff4ff02008-06-24 22:16:07 +01001284
Ben Dooksf8d79e72008-06-24 22:16:02 +01001285 irqflags |= IRQF_SHARED;
1286
Henry Nestler108f518c2011-02-22 11:29:42 +00001287 /* GPIO0 on pre-activate PHY, Reg 1F is not set by reset */
1288 iow(db, DM9000_GPR, 0); /* REG_1F bit0 activate phyxcer */
1289 mdelay(1); /* delay needs by DM9000B */
1290
Ben Dooksf8d79e72008-06-24 22:16:02 +01001291 /* Initialize DM9000 board */
1292 dm9000_reset(db);
1293 dm9000_init_dm9000(dev);
1294
Mark Brown6979d5d2011-06-01 10:18:09 +00001295 if (request_irq(dev->irq, dm9000_interrupt, irqflags, dev->name, dev))
1296 return -EAGAIN;
1297
Ben Dooksf8d79e72008-06-24 22:16:02 +01001298 /* Init driver variable */
1299 db->dbug_cnt = 0;
1300
1301 mii_check_media(&db->mii, netif_msg_link(db), 1);
1302 netif_start_queue(dev);
1303
1304 dm9000_schedule_poll(db);
1305
1306 return 0;
1307}
1308
Ben Dooksf8d79e72008-06-24 22:16:02 +01001309static void
1310dm9000_shutdown(struct net_device *dev)
1311{
Wang Chen4cf16532008-11-12 23:38:14 -08001312 board_info_t *db = netdev_priv(dev);
Ben Dooksf8d79e72008-06-24 22:16:02 +01001313
1314 /* RESET device */
1315 dm9000_phy_write(dev, 0, MII_BMCR, BMCR_RESET); /* PHY RESET */
1316 iow(db, DM9000_GPR, 0x01); /* Power-Down PHY */
1317 iow(db, DM9000_IMR, IMR_PAR); /* Disable all interrupt */
1318 iow(db, DM9000_RCR, 0x00); /* Disable RX */
1319}
1320
1321/*
1322 * Stop the interface.
1323 * The interface is stopped when it is brought.
1324 */
1325static int
1326dm9000_stop(struct net_device *ndev)
1327{
Wang Chen4cf16532008-11-12 23:38:14 -08001328 board_info_t *db = netdev_priv(ndev);
Ben Dooksf8d79e72008-06-24 22:16:02 +01001329
1330 if (netif_msg_ifdown(db))
1331 dev_dbg(db->dev, "shutting down %s\n", ndev->name);
1332
1333 cancel_delayed_work_sync(&db->phy_poll);
1334
1335 netif_stop_queue(ndev);
1336 netif_carrier_off(ndev);
1337
1338 /* free interrupt */
1339 free_irq(ndev->irq, ndev);
1340
1341 dm9000_shutdown(ndev);
1342
1343 return 0;
1344}
1345
Alexander Beregalovd88106b2009-04-15 12:52:37 +00001346static const struct net_device_ops dm9000_netdev_ops = {
1347 .ndo_open = dm9000_open,
1348 .ndo_stop = dm9000_stop,
1349 .ndo_start_xmit = dm9000_start_xmit,
1350 .ndo_tx_timeout = dm9000_timeout,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001351 .ndo_set_rx_mode = dm9000_hash_table,
Alexander Beregalovd88106b2009-04-15 12:52:37 +00001352 .ndo_do_ioctl = dm9000_ioctl,
1353 .ndo_change_mtu = eth_change_mtu,
Michał Mirosławc88fcb32011-04-15 04:50:49 +00001354 .ndo_set_features = dm9000_set_features,
Alexander Beregalovd88106b2009-04-15 12:52:37 +00001355 .ndo_validate_addr = eth_validate_addr,
1356 .ndo_set_mac_address = eth_mac_addr,
1357#ifdef CONFIG_NET_POLL_CONTROLLER
1358 .ndo_poll_controller = dm9000_poll_controller,
1359#endif
1360};
1361
Tomasz Figa0b8bf1b2013-05-20 09:16:58 +00001362static struct dm9000_plat_data *dm9000_parse_dt(struct device *dev)
1363{
1364 struct dm9000_plat_data *pdata;
1365 struct device_node *np = dev->of_node;
1366 const void *mac_addr;
1367
1368 if (!IS_ENABLED(CONFIG_OF) || !np)
1369 return NULL;
1370
1371 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1372 if (!pdata)
1373 return ERR_PTR(-ENOMEM);
1374
1375 if (of_find_property(np, "davicom,ext-phy", NULL))
1376 pdata->flags |= DM9000_PLATF_EXT_PHY;
1377 if (of_find_property(np, "davicom,no-eeprom", NULL))
1378 pdata->flags |= DM9000_PLATF_NO_EEPROM;
1379
1380 mac_addr = of_get_mac_address(np);
1381 if (mac_addr)
1382 memcpy(pdata->dev_addr, mac_addr, sizeof(pdata->dev_addr));
1383
1384 return pdata;
1385}
1386
Sascha Hauera1365272005-05-05 15:14:15 -07001387/*
1388 * Search DM9000 board, allocate space and register it
1389 */
Bill Pemberton6b6a3e72012-12-03 09:23:06 -05001390static int
Russell King3ae5eae2005-11-09 22:32:44 +00001391dm9000_probe(struct platform_device *pdev)
Sascha Hauera1365272005-05-05 15:14:15 -07001392{
Jingoo Hancd4e2e42013-08-30 13:55:14 +09001393 struct dm9000_plat_data *pdata = dev_get_platdata(&pdev->dev);
Sascha Hauera1365272005-05-05 15:14:15 -07001394 struct board_info *db; /* Point a board information structure */
1395 struct net_device *ndev;
Ben Dooks179c7432008-02-05 00:02:23 +00001396 const unsigned char *mac_src;
Sascha Hauera1365272005-05-05 15:14:15 -07001397 int ret = 0;
1398 int iosize;
1399 int i;
1400 u32 id_val;
1401
Tomasz Figa0b8bf1b2013-05-20 09:16:58 +00001402 if (!pdata) {
1403 pdata = dm9000_parse_dt(&pdev->dev);
1404 if (IS_ERR(pdata))
1405 return PTR_ERR(pdata);
1406 }
1407
Sascha Hauera1365272005-05-05 15:14:15 -07001408 /* Init network device */
Ben Dooksf8d79e72008-06-24 22:16:02 +01001409 ndev = alloc_etherdev(sizeof(struct board_info));
Joe Perches41de8d42012-01-29 13:47:52 +00001410 if (!ndev)
Sascha Hauera1365272005-05-05 15:14:15 -07001411 return -ENOMEM;
Sascha Hauera1365272005-05-05 15:14:15 -07001412
Russell King3ae5eae2005-11-09 22:32:44 +00001413 SET_NETDEV_DEV(ndev, &pdev->dev);
Sascha Hauera1365272005-05-05 15:14:15 -07001414
Enrico Scholz37d5dca2008-05-08 11:35:13 +01001415 dev_dbg(&pdev->dev, "dm9000_probe()\n");
Sascha Hauera1365272005-05-05 15:14:15 -07001416
1417 /* setup board info structure */
Wang Chen4cf16532008-11-12 23:38:14 -08001418 db = netdev_priv(ndev);
Sascha Hauera1365272005-05-05 15:14:15 -07001419
Ben Dooksa76836f2008-02-05 00:02:02 +00001420 db->dev = &pdev->dev;
Ben Dooks8f5bf5f22008-05-08 11:36:42 +01001421 db->ndev = ndev;
Ben Dooksa76836f2008-02-05 00:02:02 +00001422
Ben Dooks9ef9ac52005-07-23 17:25:18 +01001423 spin_lock_init(&db->lock);
Ben Dooks9a2f0372008-02-05 00:02:10 +00001424 mutex_init(&db->addr_lock);
Ben Dooks9ef9ac52005-07-23 17:25:18 +01001425
Ben Dooks8f5bf5f22008-05-08 11:36:42 +01001426 INIT_DELAYED_WORK(&db->phy_poll, dm9000_poll_work);
1427
Laurent Pinchart08c3f572008-06-24 22:15:57 +01001428 db->addr_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1429 db->data_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1430 db->irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1431
1432 if (db->addr_res == NULL || db->data_res == NULL ||
1433 db->irq_res == NULL) {
1434 dev_err(db->dev, "insufficient resources\n");
1435 ret = -ENOENT;
1436 goto out;
1437 }
1438
Ben Dooksc029f442009-11-10 07:22:24 +00001439 db->irq_wake = platform_get_irq(pdev, 1);
1440 if (db->irq_wake >= 0) {
1441 dev_dbg(db->dev, "wakeup irq %d\n", db->irq_wake);
1442
1443 ret = request_irq(db->irq_wake, dm9000_wol_interrupt,
1444 IRQF_SHARED, dev_name(db->dev), ndev);
1445 if (ret) {
1446 dev_err(db->dev, "cannot get wakeup irq (%d)\n", ret);
1447 } else {
1448
1449 /* test to see if irq is really wakeup capable */
Thomas Gleixnerdced35a2011-03-28 17:49:12 +02001450 ret = irq_set_irq_wake(db->irq_wake, 1);
Ben Dooksc029f442009-11-10 07:22:24 +00001451 if (ret) {
1452 dev_err(db->dev, "irq %d cannot set wakeup (%d)\n",
1453 db->irq_wake, ret);
1454 ret = 0;
1455 } else {
Thomas Gleixnerdced35a2011-03-28 17:49:12 +02001456 irq_set_irq_wake(db->irq_wake, 0);
Ben Dooksc029f442009-11-10 07:22:24 +00001457 db->wake_supported = 1;
1458 }
1459 }
1460 }
1461
Tobias Klauserec282e92009-09-09 01:07:43 +00001462 iosize = resource_size(db->addr_res);
Laurent Pinchart08c3f572008-06-24 22:15:57 +01001463 db->addr_req = request_mem_region(db->addr_res->start, iosize,
1464 pdev->name);
1465
1466 if (db->addr_req == NULL) {
1467 dev_err(db->dev, "cannot claim address reg area\n");
1468 ret = -EIO;
1469 goto out;
1470 }
1471
1472 db->io_addr = ioremap(db->addr_res->start, iosize);
1473
1474 if (db->io_addr == NULL) {
1475 dev_err(db->dev, "failed to ioremap address reg\n");
1476 ret = -EINVAL;
1477 goto out;
1478 }
1479
Tobias Klauserec282e92009-09-09 01:07:43 +00001480 iosize = resource_size(db->data_res);
Laurent Pinchart08c3f572008-06-24 22:15:57 +01001481 db->data_req = request_mem_region(db->data_res->start, iosize,
1482 pdev->name);
1483
1484 if (db->data_req == NULL) {
1485 dev_err(db->dev, "cannot claim data reg area\n");
1486 ret = -EIO;
1487 goto out;
1488 }
1489
1490 db->io_data = ioremap(db->data_res->start, iosize);
1491
1492 if (db->io_data == NULL) {
1493 dev_err(db->dev, "failed to ioremap data reg\n");
1494 ret = -EINVAL;
1495 goto out;
1496 }
1497
1498 /* fill in parameters for net-dev structure */
1499 ndev->base_addr = (unsigned long)db->io_addr;
1500 ndev->irq = db->irq_res->start;
1501
1502 /* ensure at least we have a default set of IO routines */
1503 dm9000_set_io(db, iosize);
1504
Sascha Hauera1365272005-05-05 15:14:15 -07001505 /* check to see if anything is being over-ridden */
1506 if (pdata != NULL) {
1507 /* check to see if the driver wants to over-ride the
1508 * default IO width */
1509
1510 if (pdata->flags & DM9000_PLATF_8BITONLY)
1511 dm9000_set_io(db, 1);
1512
1513 if (pdata->flags & DM9000_PLATF_16BITONLY)
1514 dm9000_set_io(db, 2);
1515
1516 if (pdata->flags & DM9000_PLATF_32BITONLY)
1517 dm9000_set_io(db, 4);
1518
1519 /* check to see if there are any IO routine
1520 * over-rides */
1521
1522 if (pdata->inblk != NULL)
1523 db->inblk = pdata->inblk;
1524
1525 if (pdata->outblk != NULL)
1526 db->outblk = pdata->outblk;
1527
1528 if (pdata->dumpblk != NULL)
1529 db->dumpblk = pdata->dumpblk;
Ben Dooks33ba5092008-02-05 00:02:01 +00001530
1531 db->flags = pdata->flags;
Sascha Hauera1365272005-05-05 15:14:15 -07001532 }
1533
Ben Dooksf8dd0ec2008-06-24 22:16:04 +01001534#ifdef CONFIG_DM9000_FORCE_SIMPLE_PHY_POLL
1535 db->flags |= DM9000_PLATF_SIMPLE_PHY;
1536#endif
1537
Joseph CHANG6741f40d2013-03-28 23:13:42 +00001538 /* Fixing bug on dm9000_probe, takeover dm9000_reset(db),
1539 * Need 'NCR_MAC_LBK' bit to indeed stable our DM9000 fifo
1540 * while probe stage.
1541 */
1542
1543 iow(db, DM9000_NCR, NCR_MAC_LBK | NCR_RST);
Sascha Hauera1365272005-05-05 15:14:15 -07001544
Ben Dooks59eae1f2008-06-24 22:16:01 +01001545 /* try multiple times, DM9000 sometimes gets the read wrong */
Ben Dooks513b6be2008-02-05 00:02:22 +00001546 for (i = 0; i < 8; i++) {
Sascha Hauera1365272005-05-05 15:14:15 -07001547 id_val = ior(db, DM9000_VIDL);
1548 id_val |= (u32)ior(db, DM9000_VIDH) << 8;
1549 id_val |= (u32)ior(db, DM9000_PIDL) << 16;
1550 id_val |= (u32)ior(db, DM9000_PIDH) << 24;
1551
1552 if (id_val == DM9000_ID)
1553 break;
Ben Dooksa76836f2008-02-05 00:02:02 +00001554 dev_err(db->dev, "read wrong id 0x%08x\n", id_val);
Sascha Hauera1365272005-05-05 15:14:15 -07001555 }
1556
1557 if (id_val != DM9000_ID) {
Ben Dooksa76836f2008-02-05 00:02:02 +00001558 dev_err(db->dev, "wrong id: 0x%08x\n", id_val);
Mike Rapoport418d6f82007-10-18 09:23:11 +02001559 ret = -ENODEV;
1560 goto out;
Sascha Hauera1365272005-05-05 15:14:15 -07001561 }
1562
Ben Dooks6d406b32008-06-24 22:15:59 +01001563 /* Identify what type of DM9000 we are working on */
1564
1565 id_val = ior(db, DM9000_CHIPR);
1566 dev_dbg(db->dev, "dm9000 revision 0x%02x\n", id_val);
1567
1568 switch (id_val) {
1569 case CHIPR_DM9000A:
1570 db->type = TYPE_DM9000A;
1571 break;
1572 case CHIPR_DM9000B:
1573 db->type = TYPE_DM9000B;
1574 break;
1575 default:
1576 dev_dbg(db->dev, "ID %02x => defaulting to DM9000E\n", id_val);
1577 db->type = TYPE_DM9000E;
1578 }
1579
Yeasah Pell5dcc60b2009-07-06 18:12:33 -07001580 /* dm9000a/b are capable of hardware checksum offload */
1581 if (db->type == TYPE_DM9000A || db->type == TYPE_DM9000B) {
Michał Mirosławc88fcb32011-04-15 04:50:49 +00001582 ndev->hw_features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM;
1583 ndev->features |= ndev->hw_features;
Yeasah Pell5dcc60b2009-07-06 18:12:33 -07001584 }
1585
Sascha Hauera1365272005-05-05 15:14:15 -07001586 /* from this point we assume that we have found a DM9000 */
1587
1588 /* driver system function */
1589 ether_setup(ndev);
1590
Alexander Beregalovd88106b2009-04-15 12:52:37 +00001591 ndev->netdev_ops = &dm9000_netdev_ops;
1592 ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
1593 ndev->ethtool_ops = &dm9000_ethtool_ops;
Sascha Hauera1365272005-05-05 15:14:15 -07001594
Sascha Hauera1365272005-05-05 15:14:15 -07001595 db->msg_enable = NETIF_MSG_LINK;
1596 db->mii.phy_id_mask = 0x1f;
1597 db->mii.reg_num_mask = 0x1f;
1598 db->mii.force_media = 0;
1599 db->mii.full_duplex = 0;
1600 db->mii.dev = ndev;
1601 db->mii.mdio_read = dm9000_phy_read;
1602 db->mii.mdio_write = dm9000_phy_write;
1603
Ben Dooks179c7432008-02-05 00:02:23 +00001604 mac_src = "eeprom";
1605
Ben Dooks86c62fa2008-02-05 00:02:09 +00001606 /* try reading the node address from the attached EEPROM */
1607 for (i = 0; i < 6; i += 2)
1608 dm9000_read_eeprom(db, i / 2, ndev->dev_addr+i);
Sascha Hauera1365272005-05-05 15:14:15 -07001609
Laurent Pinchartfe414242008-07-23 17:41:52 +02001610 if (!is_valid_ether_addr(ndev->dev_addr) && pdata != NULL) {
1611 mac_src = "platform data";
1612 memcpy(ndev->dev_addr, pdata->dev_addr, 6);
1613 }
1614
Ben Dooks5b55dda2006-06-13 23:50:15 +01001615 if (!is_valid_ether_addr(ndev->dev_addr)) {
1616 /* try reading from mac */
Ben Dooksf8d79e72008-06-24 22:16:02 +01001617
Ben Dooks179c7432008-02-05 00:02:23 +00001618 mac_src = "chip";
Ben Dooks5b55dda2006-06-13 23:50:15 +01001619 for (i = 0; i < 6; i++)
1620 ndev->dev_addr[i] = ior(db, i+DM9000_PAR);
1621 }
1622
Ben Dooks85e6b8c2011-02-24 03:17:12 +00001623 if (!is_valid_ether_addr(ndev->dev_addr)) {
Ben Dooksa76836f2008-02-05 00:02:02 +00001624 dev_warn(db->dev, "%s: Invalid ethernet MAC address. Please "
1625 "set using ifconfig\n", ndev->name);
Sascha Hauera1365272005-05-05 15:14:15 -07001626
Danny Kukawkaf2cedb62012-02-15 06:45:39 +00001627 eth_hw_addr_random(ndev);
Ben Dooks85e6b8c2011-02-24 03:17:12 +00001628 mac_src = "random";
1629 }
1630
1631
Russell King3ae5eae2005-11-09 22:32:44 +00001632 platform_set_drvdata(pdev, ndev);
Sascha Hauera1365272005-05-05 15:14:15 -07001633 ret = register_netdev(ndev);
1634
Johannes Berge1749612008-10-27 15:59:26 -07001635 if (ret == 0)
1636 printk(KERN_INFO "%s: dm9000%c at %p,%p IRQ %d MAC: %pM (%s)\n",
Ben Dooks6d406b32008-06-24 22:15:59 +01001637 ndev->name, dm9000_type_to_char(db->type),
1638 db->io_addr, db->io_data, ndev->irq,
Johannes Berge1749612008-10-27 15:59:26 -07001639 ndev->dev_addr, mac_src);
Sascha Hauera1365272005-05-05 15:14:15 -07001640 return 0;
1641
Mike Rapoport418d6f82007-10-18 09:23:11 +02001642out:
Ben Dooksa76836f2008-02-05 00:02:02 +00001643 dev_err(db->dev, "not found (%d).\n", ret);
Sascha Hauera1365272005-05-05 15:14:15 -07001644
1645 dm9000_release_board(pdev, db);
Ben Dooks9fd9f9b2007-05-07 11:13:25 +00001646 free_netdev(ndev);
Sascha Hauera1365272005-05-05 15:14:15 -07001647
1648 return ret;
1649}
1650
Sascha Hauera1365272005-05-05 15:14:15 -07001651static int
Mike Rapoport69222e22009-07-21 12:37:18 -07001652dm9000_drv_suspend(struct device *dev)
Sascha Hauera1365272005-05-05 15:14:15 -07001653{
Mike Rapoport69222e22009-07-21 12:37:18 -07001654 struct platform_device *pdev = to_platform_device(dev);
1655 struct net_device *ndev = platform_get_drvdata(pdev);
Ben Dooks321f69a2008-02-05 00:02:08 +00001656 board_info_t *db;
Sascha Hauera1365272005-05-05 15:14:15 -07001657
Russell King9480e302005-10-28 09:52:56 -07001658 if (ndev) {
Wang Chen4cf16532008-11-12 23:38:14 -08001659 db = netdev_priv(ndev);
Ben Dooks321f69a2008-02-05 00:02:08 +00001660 db->in_suspend = 1;
1661
Ben Dooksc029f442009-11-10 07:22:24 +00001662 if (!netif_running(ndev))
1663 return 0;
1664
1665 netif_device_detach(ndev);
1666
1667 /* only shutdown if not using WoL */
1668 if (!db->wake_state)
Sascha Hauera1365272005-05-05 15:14:15 -07001669 dm9000_shutdown(ndev);
Sascha Hauera1365272005-05-05 15:14:15 -07001670 }
1671 return 0;
1672}
1673
1674static int
Mike Rapoport69222e22009-07-21 12:37:18 -07001675dm9000_drv_resume(struct device *dev)
Sascha Hauera1365272005-05-05 15:14:15 -07001676{
Mike Rapoport69222e22009-07-21 12:37:18 -07001677 struct platform_device *pdev = to_platform_device(dev);
1678 struct net_device *ndev = platform_get_drvdata(pdev);
Wang Chen4cf16532008-11-12 23:38:14 -08001679 board_info_t *db = netdev_priv(ndev);
Sascha Hauera1365272005-05-05 15:14:15 -07001680
Russell King9480e302005-10-28 09:52:56 -07001681 if (ndev) {
Sascha Hauera1365272005-05-05 15:14:15 -07001682 if (netif_running(ndev)) {
Ben Dooksc029f442009-11-10 07:22:24 +00001683 /* reset if we were not in wake mode to ensure if
1684 * the device was powered off it is in a known state */
1685 if (!db->wake_state) {
1686 dm9000_reset(db);
1687 dm9000_init_dm9000(ndev);
1688 }
Sascha Hauera1365272005-05-05 15:14:15 -07001689
1690 netif_device_attach(ndev);
1691 }
Ben Dooks321f69a2008-02-05 00:02:08 +00001692
1693 db->in_suspend = 0;
Sascha Hauera1365272005-05-05 15:14:15 -07001694 }
1695 return 0;
1696}
1697
Alexey Dobriyan47145212009-12-14 18:00:08 -08001698static const struct dev_pm_ops dm9000_drv_pm_ops = {
Mike Rapoport69222e22009-07-21 12:37:18 -07001699 .suspend = dm9000_drv_suspend,
1700 .resume = dm9000_drv_resume,
1701};
1702
Bill Pemberton6b6a3e72012-12-03 09:23:06 -05001703static int
Russell King3ae5eae2005-11-09 22:32:44 +00001704dm9000_drv_remove(struct platform_device *pdev)
Sascha Hauera1365272005-05-05 15:14:15 -07001705{
Russell King3ae5eae2005-11-09 22:32:44 +00001706 struct net_device *ndev = platform_get_drvdata(pdev);
Sascha Hauera1365272005-05-05 15:14:15 -07001707
Sascha Hauera1365272005-05-05 15:14:15 -07001708 unregister_netdev(ndev);
Joe Perchesece49152010-11-15 11:12:31 +00001709 dm9000_release_board(pdev, netdev_priv(ndev));
Ben Dooks9fd9f9b2007-05-07 11:13:25 +00001710 free_netdev(ndev); /* free device structure */
Sascha Hauera1365272005-05-05 15:14:15 -07001711
Ben Dooksa76836f2008-02-05 00:02:02 +00001712 dev_dbg(&pdev->dev, "released and freed device\n");
Sascha Hauera1365272005-05-05 15:14:15 -07001713 return 0;
1714}
1715
Tomasz Figa0b8bf1b2013-05-20 09:16:58 +00001716#ifdef CONFIG_OF
1717static const struct of_device_id dm9000_of_matches[] = {
1718 { .compatible = "davicom,dm9000", },
1719 { /* sentinel */ }
1720};
1721MODULE_DEVICE_TABLE(of, dm9000_of_matches);
1722#endif
1723
Russell King3ae5eae2005-11-09 22:32:44 +00001724static struct platform_driver dm9000_driver = {
Ben Dooks5d22a312006-06-14 00:09:17 +01001725 .driver = {
1726 .name = "dm9000",
1727 .owner = THIS_MODULE,
Mike Rapoport69222e22009-07-21 12:37:18 -07001728 .pm = &dm9000_drv_pm_ops,
Tomasz Figa0b8bf1b2013-05-20 09:16:58 +00001729 .of_match_table = of_match_ptr(dm9000_of_matches),
Ben Dooks5d22a312006-06-14 00:09:17 +01001730 },
Sascha Hauera1365272005-05-05 15:14:15 -07001731 .probe = dm9000_probe,
Bill Pemberton6b6a3e72012-12-03 09:23:06 -05001732 .remove = dm9000_drv_remove,
Sascha Hauera1365272005-05-05 15:14:15 -07001733};
1734
Sachin Kamata8f9c3e2013-03-18 01:50:46 +00001735module_platform_driver(dm9000_driver);
Sascha Hauera1365272005-05-05 15:14:15 -07001736
1737MODULE_AUTHOR("Sascha Hauer, Ben Dooks");
1738MODULE_DESCRIPTION("Davicom DM9000 network driver");
1739MODULE_LICENSE("GPL");
Kay Sievers72abb462008-04-18 13:50:44 -07001740MODULE_ALIAS("platform:dm9000");