]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/net/ethernet/davicom/dm9000.c
net: davicom: Fix regulator not turned off on failed probe
[mirror_ubuntu-hirsute-kernel.git] / drivers / net / ethernet / davicom / dm9000.c
CommitLineData
c942fddf 1// SPDX-License-Identifier: GPL-2.0-or-later
a1365275 2/*
41c340f0 3 * Davicom DM9000 Fast Ethernet driver for Linux.
a1365275
SH
4 * Copyright (C) 1997 Sten Wang
5 *
41c340f0 6 * (C) Copyright 1997-1998 DAVICOM Semiconductor,Inc. All Rights Reserved.
9ef9ac51 7 *
41c340f0
BD
8 * Additional updates, Copyright:
9 * Ben Dooks <ben@simtec.co.uk>
10 * Sascha Hauer <s.hauer@pengutronix.de>
a1365275
SH
11 */
12
13#include <linux/module.h>
14#include <linux/ioport.h>
15#include <linux/netdevice.h>
16#include <linux/etherdevice.h>
a6b7a407 17#include <linux/interrupt.h>
a1365275 18#include <linux/skbuff.h>
a1365275
SH
19#include <linux/spinlock.h>
20#include <linux/crc32.h>
21#include <linux/mii.h>
0b8bf1ba
TF
22#include <linux/of.h>
23#include <linux/of_net.h>
7da99859 24#include <linux/ethtool.h>
a1365275
SH
25#include <linux/dm9000.h>
26#include <linux/delay.h>
d052d1be 27#include <linux/platform_device.h>
4e4fc05a 28#include <linux/irq.h>
5a0e3ad6 29#include <linux/slab.h>
7994fe55
ZLK
30#include <linux/regulator/consumer.h>
31#include <linux/gpio.h>
32#include <linux/of_gpio.h>
a1365275
SH
33
34#include <asm/delay.h>
35#include <asm/irq.h>
36#include <asm/io.h>
37
38#include "dm9000.h"
39
40/* Board/System/Debug information/definition ---------------- */
41
42#define DM9000_PHY 0x40 /* PHY address 0x01 */
43
59eae1fa 44#define CARDNAME "dm9000"
a1365275 45
a1365275
SH
46/*
47 * Transmit timeout, default 5 seconds.
48 */
49static int watchdog = 5000;
50module_param(watchdog, int, 0400);
51MODULE_PARM_DESC(watchdog, "transmit timeout in milliseconds");
52
2e025c71
VZ
53/*
54 * Debug messages level
55 */
56static int debug;
57module_param(debug, int, 0644);
0fdbedc7 58MODULE_PARM_DESC(debug, "dm9000 debug level (0-6)");
2e025c71 59
9a2f037c
BD
60/* DM9000 register address locking.
61 *
62 * The DM9000 uses an address register to control where data written
63 * to the data register goes. This means that the address register
64 * must be preserved over interrupts or similar calls.
65 *
66 * During interrupt and other critical calls, a spinlock is used to
67 * protect the system, but the calls themselves save the address
68 * in the address register in case they are interrupting another
69 * access to the device.
70 *
71 * For general accesses a lock is provided so that calls which are
72 * allowed to sleep are serialised so that the address register does
73 * not need to be saved. This lock also serves to serialise access
74 * to the EEPROM and PHY access registers which are shared between
75 * these two devices.
76 */
77
6d406b3c
BD
78/* The driver supports the original DM9000E, and now the two newer
79 * devices, DM9000A and DM9000B.
80 */
81
82enum dm9000_type {
83 TYPE_DM9000E, /* original DM9000 */
84 TYPE_DM9000A,
85 TYPE_DM9000B
86};
87
a1365275 88/* Structure/enum declaration ------------------------------- */
2b162928 89struct board_info {
a1365275 90
59eae1fa
BD
91 void __iomem *io_addr; /* Register I/O base address */
92 void __iomem *io_data; /* Data I/O address */
93 u16 irq; /* IRQ */
a1365275 94
59eae1fa
BD
95 u16 tx_pkt_cnt;
96 u16 queue_pkt_len;
97 u16 queue_start_addr;
5dcc60b7 98 u16 queue_ip_summed;
59eae1fa
BD
99 u16 dbug_cnt;
100 u8 io_mode; /* 0:word, 2:byte */
101 u8 phy_addr;
102 u8 imr_all;
103
104 unsigned int flags;
58237983 105 unsigned int in_timeout:1;
5b22721d
BS
106 unsigned int in_suspend:1;
107 unsigned int wake_supported:1;
a1365275 108
6d406b3c 109 enum dm9000_type type;
5b2b4ff0 110
a1365275
SH
111 void (*inblk)(void __iomem *port, void *data, int length);
112 void (*outblk)(void __iomem *port, void *data, int length);
113 void (*dumpblk)(void __iomem *port, int length);
114
a76836f9
BD
115 struct device *dev; /* parent device */
116
a1365275
SH
117 struct resource *addr_res; /* resources found */
118 struct resource *data_res;
119 struct resource *addr_req; /* resources requested */
120 struct resource *data_req;
a1365275 121
c029f444
BD
122 int irq_wake;
123
9a2f037c
BD
124 struct mutex addr_lock; /* phy and eeprom access lock */
125
8f5bf5f2
BD
126 struct delayed_work phy_poll;
127 struct net_device *ndev;
128
59eae1fa 129 spinlock_t lock;
a1365275
SH
130
131 struct mii_if_info mii;
59eae1fa 132 u32 msg_enable;
c029f444 133 u32 wake_state;
5dcc60b7 134
5dcc60b7 135 int ip_summed;
c4eb7e7c
PC
136
137 struct regulator *power_supply;
2b162928 138};
a1365275 139
5b2b4ff0
BD
140/* debug code */
141
142#define dm9000_dbg(db, lev, msg...) do { \
2e025c71 143 if ((lev) < debug) { \
5b2b4ff0
BD
144 dev_dbg(db->dev, msg); \
145 } \
146} while (0)
147
2b162928 148static inline struct board_info *to_dm9000_board(struct net_device *dev)
7da99859 149{
4cf1653a 150 return netdev_priv(dev);
7da99859
BD
151}
152
a1365275
SH
153/* DM9000 network board routine ---------------------------- */
154
a1365275
SH
155/*
156 * Read a byte from I/O port
157 */
158static u8
2b162928 159ior(struct board_info *db, int reg)
a1365275
SH
160{
161 writeb(reg, db->io_addr);
162 return readb(db->io_data);
163}
164
165/*
166 * Write a byte to I/O port
167 */
168
169static void
2b162928 170iow(struct board_info *db, int reg, int value)
a1365275
SH
171{
172 writeb(reg, db->io_addr);
173 writeb(value, db->io_data);
174}
175
09ee9f87 176static void
2b162928 177dm9000_reset(struct board_info *db)
09ee9f87
MA
178{
179 dev_dbg(db->dev, "resetting device\n");
180
181 /* Reset DM9000, see DM9000 Application Notes V1.22 Jun 11, 2004 page 29
182 * The essential point is that we have to do a double reset, and the
183 * instruction is to set LBK into MAC internal loopback mode.
184 */
751bb6fd 185 iow(db, DM9000_NCR, NCR_RST | NCR_MAC_LBK);
09ee9f87
MA
186 udelay(100); /* Application note says at least 20 us */
187 if (ior(db, DM9000_NCR) & 1)
188 dev_err(db->dev, "dm9000 did not respond to first reset\n");
189
190 iow(db, DM9000_NCR, 0);
751bb6fd 191 iow(db, DM9000_NCR, NCR_RST | NCR_MAC_LBK);
09ee9f87
MA
192 udelay(100);
193 if (ior(db, DM9000_NCR) & 1)
194 dev_err(db->dev, "dm9000 did not respond to second reset\n");
195}
196
a1365275
SH
197/* routines for sending block to chip */
198
199static void dm9000_outblk_8bit(void __iomem *reg, void *data, int count)
200{
daadaf6f 201 iowrite8_rep(reg, data, count);
a1365275
SH
202}
203
204static void dm9000_outblk_16bit(void __iomem *reg, void *data, int count)
205{
daadaf6f 206 iowrite16_rep(reg, data, (count+1) >> 1);
a1365275
SH
207}
208
209static void dm9000_outblk_32bit(void __iomem *reg, void *data, int count)
210{
daadaf6f 211 iowrite32_rep(reg, data, (count+3) >> 2);
a1365275
SH
212}
213
214/* input block from chip to memory */
215
216static void dm9000_inblk_8bit(void __iomem *reg, void *data, int count)
217{
daadaf6f 218 ioread8_rep(reg, data, count);
a1365275
SH
219}
220
221
222static void dm9000_inblk_16bit(void __iomem *reg, void *data, int count)
223{
daadaf6f 224 ioread16_rep(reg, data, (count+1) >> 1);
a1365275
SH
225}
226
227static void dm9000_inblk_32bit(void __iomem *reg, void *data, int count)
228{
daadaf6f 229 ioread32_rep(reg, data, (count+3) >> 2);
a1365275
SH
230}
231
232/* dump block from chip to null */
233
234static void dm9000_dumpblk_8bit(void __iomem *reg, int count)
235{
236 int i;
a1365275
SH
237
238 for (i = 0; i < count; i++)
3858632a 239 readb(reg);
a1365275
SH
240}
241
242static void dm9000_dumpblk_16bit(void __iomem *reg, int count)
243{
244 int i;
a1365275
SH
245
246 count = (count + 1) >> 1;
247
248 for (i = 0; i < count; i++)
3858632a 249 readw(reg);
a1365275
SH
250}
251
252static void dm9000_dumpblk_32bit(void __iomem *reg, int count)
253{
254 int i;
a1365275
SH
255
256 count = (count + 3) >> 2;
257
258 for (i = 0; i < count; i++)
3858632a 259 readl(reg);
a1365275
SH
260}
261
6741f40d
JC
262/*
263 * Sleep, either by using msleep() or if we are suspending, then
264 * use mdelay() to sleep.
265 */
2b162928 266static void dm9000_msleep(struct board_info *db, unsigned int ms)
6741f40d 267{
58237983 268 if (db->in_suspend || db->in_timeout)
6741f40d
JC
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{
2b162928 278 struct board_info *db = netdev_priv(dev);
6741f40d
JC
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{
2b162928 324 struct board_info *db = netdev_priv(dev);
6741f40d
JC
325 unsigned long flags;
326 unsigned long reg_save;
327
328 dm9000_dbg(db, 5, "phy_write[%02x] = %04x\n", reg, value);
58237983
AR
329 if (!db->in_timeout)
330 mutex_lock(&db->addr_lock);
6741f40d
JC
331
332 spin_lock_irqsave(&db->lock, flags);
333
334 /* Save previous register address */
335 reg_save = readb(db->io_addr);
336
337 /* Fill the phyxcer register into REG_0C */
338 iow(db, DM9000_EPAR, DM9000_PHY | reg);
339
340 /* Fill the written data into REG_0D & REG_0E */
341 iow(db, DM9000_EPDRL, value);
342 iow(db, DM9000_EPDRH, value >> 8);
343
344 /* Issue phyxcer write command */
345 iow(db, DM9000_EPCR, EPCR_EPOS | EPCR_ERPRW);
346
347 writeb(reg_save, db->io_addr);
348 spin_unlock_irqrestore(&db->lock, flags);
349
350 dm9000_msleep(db, 1); /* Wait write complete */
351
352 spin_lock_irqsave(&db->lock, flags);
353 reg_save = readb(db->io_addr);
354
355 iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer write command */
356
357 /* restore the previous address */
358 writeb(reg_save, db->io_addr);
359
360 spin_unlock_irqrestore(&db->lock, flags);
58237983
AR
361 if (!db->in_timeout)
362 mutex_unlock(&db->addr_lock);
6741f40d
JC
363}
364
a1365275
SH
365/* dm9000_set_io
366 *
367 * select the specified set of io routines to use with the
368 * device
369 */
370
371static void dm9000_set_io(struct board_info *db, int byte_width)
372{
373 /* use the size of the data resource to work out what IO
374 * routines we want to use
375 */
376
377 switch (byte_width) {
378 case 1:
379 db->dumpblk = dm9000_dumpblk_8bit;
380 db->outblk = dm9000_outblk_8bit;
381 db->inblk = dm9000_inblk_8bit;
382 break;
383
a1365275
SH
384
385 case 3:
a76836f9 386 dev_dbg(db->dev, ": 3 byte IO, falling back to 16bit\n");
df561f66 387 fallthrough;
a76836f9 388 case 2:
a1365275
SH
389 db->dumpblk = dm9000_dumpblk_16bit;
390 db->outblk = dm9000_outblk_16bit;
391 db->inblk = dm9000_inblk_16bit;
392 break;
393
394 case 4:
395 default:
396 db->dumpblk = dm9000_dumpblk_32bit;
397 db->outblk = dm9000_outblk_32bit;
398 db->inblk = dm9000_inblk_32bit;
399 break;
400 }
401}
402
2b162928 403static void dm9000_schedule_poll(struct board_info *db)
8f5bf5f2 404{
6d406b3c
BD
405 if (db->type == TYPE_DM9000E)
406 schedule_delayed_work(&db->phy_poll, HZ * 2);
8f5bf5f2 407}
a1365275 408
f8d79e79
BD
409static int dm9000_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
410{
2b162928 411 struct board_info *dm = to_dm9000_board(dev);
f8d79e79
BD
412
413 if (!netif_running(dev))
414 return -EINVAL;
415
416 return generic_mii_ioctl(&dm->mii, if_mii(req), cmd, NULL);
417}
418
419static unsigned int
2b162928 420dm9000_read_locked(struct board_info *db, int reg)
a1365275 421{
a1365275 422 unsigned long flags;
f8d79e79 423 unsigned int ret;
a1365275 424
f8d79e79
BD
425 spin_lock_irqsave(&db->lock, flags);
426 ret = ior(db, reg);
427 spin_unlock_irqrestore(&db->lock, flags);
a1365275 428
f8d79e79
BD
429 return ret;
430}
a1365275 431
2b162928 432static int dm9000_wait_eeprom(struct board_info *db)
f8d79e79
BD
433{
434 unsigned int status;
435 int timeout = 8; /* wait max 8msec */
436
437 /* The DM9000 data sheets say we should be able to
438 * poll the ERRE bit in EPCR to wait for the EEPROM
439 * operation. From testing several chips, this bit
440 * does not seem to work.
441 *
442 * We attempt to use the bit, but fall back to the
443 * timeout (which is why we do not return an error
444 * on expiry) to say that the EEPROM operation has
445 * completed.
446 */
447
448 while (1) {
449 status = dm9000_read_locked(db, DM9000_EPCR);
450
451 if ((status & EPCR_ERRE) == 0)
452 break;
453
2fcf06ca
BD
454 msleep(1);
455
f8d79e79
BD
456 if (timeout-- < 0) {
457 dev_dbg(db->dev, "timeout waiting EEPROM\n");
458 break;
459 }
460 }
461
462 return 0;
a1365275
SH
463}
464
2fd0e33f 465/*
f8d79e79 466 * Read a word data from EEPROM
2fd0e33f 467 */
f8d79e79 468static void
2b162928 469dm9000_read_eeprom(struct board_info *db, int offset, u8 *to)
2fd0e33f 470{
f8d79e79
BD
471 unsigned long flags;
472
473 if (db->flags & DM9000_PLATF_NO_EEPROM) {
474 to[0] = 0xff;
475 to[1] = 0xff;
476 return;
477 }
478
479 mutex_lock(&db->addr_lock);
480
481 spin_lock_irqsave(&db->lock, flags);
482
483 iow(db, DM9000_EPAR, offset);
484 iow(db, DM9000_EPCR, EPCR_ERPRR);
485
486 spin_unlock_irqrestore(&db->lock, flags);
487
488 dm9000_wait_eeprom(db);
489
490 /* delay for at-least 150uS */
491 msleep(1);
492
493 spin_lock_irqsave(&db->lock, flags);
494
495 iow(db, DM9000_EPCR, 0x0);
496
497 to[0] = ior(db, DM9000_EPDRL);
498 to[1] = ior(db, DM9000_EPDRH);
499
500 spin_unlock_irqrestore(&db->lock, flags);
501
502 mutex_unlock(&db->addr_lock);
2fd0e33f 503}
a1365275 504
f8d79e79
BD
505/*
506 * Write a word data to SROM
507 */
508static void
2b162928 509dm9000_write_eeprom(struct board_info *db, int offset, u8 *data)
f42d8aea 510{
f8d79e79 511 unsigned long flags;
f42d8aea 512
f8d79e79
BD
513 if (db->flags & DM9000_PLATF_NO_EEPROM)
514 return;
f42d8aea 515
f8d79e79
BD
516 mutex_lock(&db->addr_lock);
517
518 spin_lock_irqsave(&db->lock, flags);
519 iow(db, DM9000_EPAR, offset);
520 iow(db, DM9000_EPDRH, data[1]);
521 iow(db, DM9000_EPDRL, data[0]);
522 iow(db, DM9000_EPCR, EPCR_WEP | EPCR_ERPRW);
523 spin_unlock_irqrestore(&db->lock, flags);
524
525 dm9000_wait_eeprom(db);
526
527 mdelay(1); /* wait at least 150uS to clear */
528
529 spin_lock_irqsave(&db->lock, flags);
530 iow(db, DM9000_EPCR, 0);
531 spin_unlock_irqrestore(&db->lock, flags);
532
533 mutex_unlock(&db->addr_lock);
f42d8aea
BD
534}
535
7da99859
BD
536/* ethtool ops */
537
538static void dm9000_get_drvinfo(struct net_device *dev,
539 struct ethtool_drvinfo *info)
540{
2b162928 541 struct board_info *dm = to_dm9000_board(dev);
7da99859 542
7826d43f 543 strlcpy(info->driver, CARDNAME, sizeof(info->driver));
7826d43f
JP
544 strlcpy(info->bus_info, to_platform_device(dm->dev)->name,
545 sizeof(info->bus_info));
7da99859
BD
546}
547
e662ee02
BD
548static u32 dm9000_get_msglevel(struct net_device *dev)
549{
2b162928 550 struct board_info *dm = to_dm9000_board(dev);
e662ee02
BD
551
552 return dm->msg_enable;
553}
554
555static void dm9000_set_msglevel(struct net_device *dev, u32 value)
556{
2b162928 557 struct board_info *dm = to_dm9000_board(dev);
e662ee02
BD
558
559 dm->msg_enable = value;
560}
561
99bff5ee
PR
562static int dm9000_get_link_ksettings(struct net_device *dev,
563 struct ethtool_link_ksettings *cmd)
7da99859 564{
2b162928 565 struct board_info *dm = to_dm9000_board(dev);
7da99859 566
99bff5ee 567 mii_ethtool_get_link_ksettings(&dm->mii, cmd);
7da99859
BD
568 return 0;
569}
570
99bff5ee
PR
571static int dm9000_set_link_ksettings(struct net_device *dev,
572 const struct ethtool_link_ksettings *cmd)
7da99859 573{
2b162928 574 struct board_info *dm = to_dm9000_board(dev);
7da99859 575
99bff5ee 576 return mii_ethtool_set_link_ksettings(&dm->mii, cmd);
7da99859
BD
577}
578
579static int dm9000_nway_reset(struct net_device *dev)
580{
2b162928 581 struct board_info *dm = to_dm9000_board(dev);
7da99859
BD
582 return mii_nway_restart(&dm->mii);
583}
584
c8f44aff
MM
585static int dm9000_set_features(struct net_device *dev,
586 netdev_features_t features)
5dcc60b7 587{
2b162928 588 struct board_info *dm = to_dm9000_board(dev);
c8f44aff 589 netdev_features_t changed = dev->features ^ features;
c88fcb3d 590 unsigned long flags;
5dcc60b7 591
c88fcb3d 592 if (!(changed & NETIF_F_RXCSUM))
5dcc60b7 593 return 0;
380fefb2
BS
594
595 spin_lock_irqsave(&dm->lock, flags);
c88fcb3d 596 iow(dm, DM9000_RCSR, (features & NETIF_F_RXCSUM) ? RCSR_CSUM : 0);
380fefb2
BS
597 spin_unlock_irqrestore(&dm->lock, flags);
598
c88fcb3d 599 return 0;
5dcc60b7
YP
600}
601
7da99859
BD
602static u32 dm9000_get_link(struct net_device *dev)
603{
2b162928 604 struct board_info *dm = to_dm9000_board(dev);
aa1eb452
BD
605 u32 ret;
606
607 if (dm->flags & DM9000_PLATF_EXT_PHY)
608 ret = mii_link_ok(&dm->mii);
609 else
610 ret = dm9000_read_locked(dm, DM9000_NSR) & NSR_LINKST ? 1 : 0;
611
612 return ret;
7da99859
BD
613}
614
29d52e54
BD
615#define DM_EEPROM_MAGIC (0x444D394B)
616
617static int dm9000_get_eeprom_len(struct net_device *dev)
618{
619 return 128;
620}
621
622static int dm9000_get_eeprom(struct net_device *dev,
623 struct ethtool_eeprom *ee, u8 *data)
624{
2b162928 625 struct board_info *dm = to_dm9000_board(dev);
29d52e54
BD
626 int offset = ee->offset;
627 int len = ee->len;
628 int i;
629
630 /* EEPROM access is aligned to two bytes */
631
632 if ((len & 1) != 0 || (offset & 1) != 0)
633 return -EINVAL;
634
bb44fb70
BD
635 if (dm->flags & DM9000_PLATF_NO_EEPROM)
636 return -ENOENT;
637
29d52e54
BD
638 ee->magic = DM_EEPROM_MAGIC;
639
640 for (i = 0; i < len; i += 2)
641 dm9000_read_eeprom(dm, (offset + i) / 2, data + i);
642
643 return 0;
644}
645
646static int dm9000_set_eeprom(struct net_device *dev,
647 struct ethtool_eeprom *ee, u8 *data)
648{
2b162928 649 struct board_info *dm = to_dm9000_board(dev);
29d52e54
BD
650 int offset = ee->offset;
651 int len = ee->len;
40d15cd0 652 int done;
29d52e54
BD
653
654 /* EEPROM access is aligned to two bytes */
655
bb44fb70
BD
656 if (dm->flags & DM9000_PLATF_NO_EEPROM)
657 return -ENOENT;
658
29d52e54
BD
659 if (ee->magic != DM_EEPROM_MAGIC)
660 return -EINVAL;
661
40d15cd0
BD
662 while (len > 0) {
663 if (len & 1 || offset & 1) {
664 int which = offset & 1;
665 u8 tmp[2];
666
667 dm9000_read_eeprom(dm, offset / 2, tmp);
668 tmp[which] = *data;
669 dm9000_write_eeprom(dm, offset / 2, tmp);
670
671 done = 1;
672 } else {
673 dm9000_write_eeprom(dm, offset / 2, data);
674 done = 2;
675 }
676
677 data += done;
678 offset += done;
679 len -= done;
680 }
29d52e54
BD
681
682 return 0;
683}
684
c029f444
BD
685static void dm9000_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
686{
2b162928 687 struct board_info *dm = to_dm9000_board(dev);
c029f444
BD
688
689 memset(w, 0, sizeof(struct ethtool_wolinfo));
690
691 /* note, we could probably support wake-phy too */
692 w->supported = dm->wake_supported ? WAKE_MAGIC : 0;
693 w->wolopts = dm->wake_state;
694}
695
696static int dm9000_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
697{
2b162928 698 struct board_info *dm = to_dm9000_board(dev);
c029f444
BD
699 unsigned long flags;
700 u32 opts = w->wolopts;
701 u32 wcr = 0;
702
703 if (!dm->wake_supported)
704 return -EOPNOTSUPP;
705
706 if (opts & ~WAKE_MAGIC)
707 return -EINVAL;
708
709 if (opts & WAKE_MAGIC)
710 wcr |= WCR_MAGICEN;
711
712 mutex_lock(&dm->addr_lock);
713
714 spin_lock_irqsave(&dm->lock, flags);
715 iow(dm, DM9000_WCR, wcr);
716 spin_unlock_irqrestore(&dm->lock, flags);
717
718 mutex_unlock(&dm->addr_lock);
719
720 if (dm->wake_state != opts) {
721 /* change in wol state, update IRQ state */
722
723 if (!dm->wake_state)
dced35ae 724 irq_set_irq_wake(dm->irq_wake, 1);
83b98fb4 725 else if (dm->wake_state && !opts)
dced35ae 726 irq_set_irq_wake(dm->irq_wake, 0);
c029f444
BD
727 }
728
729 dm->wake_state = opts;
730 return 0;
731}
732
7da99859
BD
733static const struct ethtool_ops dm9000_ethtool_ops = {
734 .get_drvinfo = dm9000_get_drvinfo,
e662ee02
BD
735 .get_msglevel = dm9000_get_msglevel,
736 .set_msglevel = dm9000_set_msglevel,
7da99859
BD
737 .nway_reset = dm9000_nway_reset,
738 .get_link = dm9000_get_link,
c029f444
BD
739 .get_wol = dm9000_get_wol,
740 .set_wol = dm9000_set_wol,
5b22721d
BS
741 .get_eeprom_len = dm9000_get_eeprom_len,
742 .get_eeprom = dm9000_get_eeprom,
743 .set_eeprom = dm9000_set_eeprom,
99bff5ee
PR
744 .get_link_ksettings = dm9000_get_link_ksettings,
745 .set_link_ksettings = dm9000_set_link_ksettings,
7da99859
BD
746};
747
2b162928 748static void dm9000_show_carrier(struct board_info *db,
f8dd0ecb
BD
749 unsigned carrier, unsigned nsr)
750{
727a282f 751 int lpa;
f8dd0ecb 752 struct net_device *ndev = db->ndev;
727a282f 753 struct mii_if_info *mii = &db->mii;
f8dd0ecb
BD
754 unsigned ncr = dm9000_read_locked(db, DM9000_NCR);
755
727a282f
NK
756 if (carrier) {
757 lpa = mii->mdio_read(mii->dev, mii->phy_id, MII_LPA);
758 dev_info(db->dev,
759 "%s: link up, %dMbps, %s-duplex, lpa 0x%04X\n",
f8dd0ecb 760 ndev->name, (nsr & NSR_SPEED) ? 10 : 100,
727a282f
NK
761 (ncr & NCR_FDX) ? "full" : "half", lpa);
762 } else {
f8dd0ecb 763 dev_info(db->dev, "%s: link down\n", ndev->name);
727a282f 764 }
f8dd0ecb
BD
765}
766
8f5bf5f2
BD
767static void
768dm9000_poll_work(struct work_struct *w)
769{
bf6aede7 770 struct delayed_work *dw = to_delayed_work(w);
2b162928 771 struct board_info *db = container_of(dw, struct board_info, phy_poll);
f8dd0ecb
BD
772 struct net_device *ndev = db->ndev;
773
774 if (db->flags & DM9000_PLATF_SIMPLE_PHY &&
775 !(db->flags & DM9000_PLATF_EXT_PHY)) {
776 unsigned nsr = dm9000_read_locked(db, DM9000_NSR);
777 unsigned old_carrier = netif_carrier_ok(ndev) ? 1 : 0;
778 unsigned new_carrier;
8f5bf5f2 779
f8dd0ecb
BD
780 new_carrier = (nsr & NSR_LINKST) ? 1 : 0;
781
782 if (old_carrier != new_carrier) {
783 if (netif_msg_link(db))
784 dm9000_show_carrier(db, new_carrier, nsr);
785
786 if (!new_carrier)
787 netif_carrier_off(ndev);
788 else
789 netif_carrier_on(ndev);
790 }
791 } else
792 mii_check_media(&db->mii, netif_msg_link(db), 0);
5b22721d 793
f8dd0ecb 794 if (netif_running(ndev))
8f5bf5f2
BD
795 dm9000_schedule_poll(db);
796}
7da99859 797
a1365275
SH
798/* dm9000_release_board
799 *
800 * release a board, and any mapped resources
801 */
802
803static void
804dm9000_release_board(struct platform_device *pdev, struct board_info *db)
805{
a1365275
SH
806 /* unmap our resources */
807
808 iounmap(db->io_addr);
809 iounmap(db->io_data);
810
811 /* release the resources */
812
a5536e10
DC
813 if (db->data_req)
814 release_resource(db->data_req);
9088fa4f 815 kfree(db->data_req);
a1365275 816
a5536e10
DC
817 if (db->addr_req)
818 release_resource(db->addr_req);
9088fa4f 819 kfree(db->addr_req);
a1365275
SH
820}
821
6d406b3c
BD
822static unsigned char dm9000_type_to_char(enum dm9000_type type)
823{
824 switch (type) {
825 case TYPE_DM9000E: return 'e';
826 case TYPE_DM9000A: return 'a';
827 case TYPE_DM9000B: return 'b';
828 }
829
830 return '?';
831}
832
a1365275 833/*
f8d79e79 834 * Set DM9000 multicast address
a1365275 835 */
f8d79e79 836static void
380fefb2 837dm9000_hash_table_unlocked(struct net_device *dev)
a1365275 838{
2b162928 839 struct board_info *db = netdev_priv(dev);
22bedad3 840 struct netdev_hw_addr *ha;
f8d79e79
BD
841 int i, oft;
842 u32 hash_val;
35e729ac 843 u16 hash_table[4] = { 0, 0, 0, 0x8000 }; /* broadcast address */
f8d79e79 844 u8 rcr = RCR_DIS_LONG | RCR_DIS_CRC | RCR_RXEN;
a1365275 845
f8d79e79 846 dm9000_dbg(db, 1, "entering %s\n", __func__);
a1365275 847
f8d79e79
BD
848 for (i = 0, oft = DM9000_PAR; i < 6; i++, oft++)
849 iow(db, oft, dev->dev_addr[i]);
a1365275 850
f8d79e79
BD
851 if (dev->flags & IFF_PROMISC)
852 rcr |= RCR_PRMSC;
8f5bf5f2 853
f8d79e79
BD
854 if (dev->flags & IFF_ALLMULTI)
855 rcr |= RCR_ALL;
08c3f57c 856
f8d79e79 857 /* the multicast address in Hash Table : 64 bits */
22bedad3
JP
858 netdev_for_each_mc_addr(ha, dev) {
859 hash_val = ether_crc_le(6, ha->addr) & 0x3f;
f8d79e79 860 hash_table[hash_val / 16] |= (u16) 1 << (hash_val % 16);
08c3f57c
LP
861 }
862
f8d79e79
BD
863 /* Write the hash table to MAC MD table */
864 for (i = 0, oft = DM9000_MAR; i < 4; i++) {
865 iow(db, oft++, hash_table[i]);
866 iow(db, oft++, hash_table[i] >> 8);
08c3f57c
LP
867 }
868
f8d79e79 869 iow(db, DM9000_RCR, rcr);
380fefb2
BS
870}
871
872static void
873dm9000_hash_table(struct net_device *dev)
874{
2b162928 875 struct board_info *db = netdev_priv(dev);
380fefb2
BS
876 unsigned long flags;
877
878 spin_lock_irqsave(&db->lock, flags);
879 dm9000_hash_table_unlocked(dev);
f8d79e79
BD
880 spin_unlock_irqrestore(&db->lock, flags);
881}
08c3f57c 882
17ad78de 883static void
2b162928 884dm9000_mask_interrupts(struct board_info *db)
17ad78de
AR
885{
886 iow(db, DM9000_IMR, IMR_PAR);
887}
888
889static void
2b162928 890dm9000_unmask_interrupts(struct board_info *db)
17ad78de
AR
891{
892 iow(db, DM9000_IMR, db->imr_all);
893}
894
f8d79e79 895/*
1ae5dc34 896 * Initialize dm9000 board
f8d79e79
BD
897 */
898static void
899dm9000_init_dm9000(struct net_device *dev)
900{
2b162928 901 struct board_info *db = netdev_priv(dev);
f8d79e79 902 unsigned int imr;
c029f444 903 unsigned int ncr;
08c3f57c 904
f8d79e79 905 dm9000_dbg(db, 1, "entering %s\n", __func__);
08c3f57c 906
751bb6fd 907 dm9000_reset(db);
17ad78de 908 dm9000_mask_interrupts(db);
751bb6fd 909
f8d79e79
BD
910 /* I/O mode */
911 db->io_mode = ior(db, DM9000_ISR) >> 6; /* ISR bit7:6 keeps I/O mode */
08c3f57c 912
5dcc60b7 913 /* Checksum mode */
c88fcb3d 914 if (dev->hw_features & NETIF_F_RXCSUM)
56d37f17 915 iow(db, DM9000_RCSR,
c88fcb3d 916 (dev->features & NETIF_F_RXCSUM) ? RCSR_CSUM : 0);
5dcc60b7 917
f8d79e79 918 iow(db, DM9000_GPCR, GPCR_GEP_CNTL); /* Let GPIO0 output */
677d7d28 919 iow(db, DM9000_GPR, 0);
08c3f57c 920
6649b205
NK
921 /* If we are dealing with DM9000B, some extra steps are required: a
922 * manual phy reset, and setting init params.
923 */
924 if (db->type == TYPE_DM9000B) {
925 dm9000_phy_write(dev, 0, MII_BMCR, BMCR_RESET);
926 dm9000_phy_write(dev, 0, MII_DM_DSPCR, DSPCR_INIT_PARAM);
927 }
6741f40d 928
c029f444
BD
929 ncr = (db->flags & DM9000_PLATF_EXT_PHY) ? NCR_EXT_PHY : 0;
930
931 /* if wol is needed, then always set NCR_WAKEEN otherwise we end
932 * up dumping the wake events if we disable this. There is already
933 * a wake-mask in DM9000_WCR */
934 if (db->wake_supported)
935 ncr |= NCR_WAKEEN;
936
937 iow(db, DM9000_NCR, ncr);
33ba5091 938
a1365275
SH
939 /* Program operating register */
940 iow(db, DM9000_TCR, 0); /* TX Polling clear */
941 iow(db, DM9000_BPTR, 0x3f); /* Less 3Kb, 200us */
942 iow(db, DM9000_FCR, 0xff); /* Flow Control */
943 iow(db, DM9000_SMCR, 0); /* Special Mode */
944 /* clear TX status */
945 iow(db, DM9000_NSR, NSR_WAKEST | NSR_TX2END | NSR_TX1END);
946 iow(db, DM9000_ISR, ISR_CLR_STATUS); /* Clear interrupt status */
947
948 /* Set address filter table */
380fefb2 949 dm9000_hash_table_unlocked(dev);
a1365275 950
6d406b3c
BD
951 imr = IMR_PAR | IMR_PTM | IMR_PRM;
952 if (db->type != TYPE_DM9000E)
953 imr |= IMR_LNKCHNG;
954
955 db->imr_all = imr;
956
a1365275
SH
957 /* Init Driver variable */
958 db->tx_pkt_cnt = 0;
959 db->queue_pkt_len = 0;
860e9538 960 netif_trans_update(dev);
a1365275
SH
961}
962
f8d79e79 963/* Our watchdog timed out. Called by the networking layer */
0290bd29 964static void dm9000_timeout(struct net_device *dev, unsigned int txqueue)
f8d79e79 965{
2b162928 966 struct board_info *db = netdev_priv(dev);
f8d79e79
BD
967 u8 reg_save;
968 unsigned long flags;
969
970 /* Save previous register address */
f8d79e79 971 spin_lock_irqsave(&db->lock, flags);
58237983 972 db->in_timeout = 1;
8dde9242 973 reg_save = readb(db->io_addr);
f8d79e79
BD
974
975 netif_stop_queue(dev);
f8d79e79 976 dm9000_init_dm9000(dev);
17ad78de 977 dm9000_unmask_interrupts(db);
f8d79e79 978 /* We can accept TX packets again */
860e9538 979 netif_trans_update(dev); /* prevent tx timeout */
f8d79e79
BD
980 netif_wake_queue(dev);
981
982 /* Restore previous register address */
983 writeb(reg_save, db->io_addr);
58237983 984 db->in_timeout = 0;
f8d79e79
BD
985 spin_unlock_irqrestore(&db->lock, flags);
986}
987
5dcc60b7
YP
988static void dm9000_send_packet(struct net_device *dev,
989 int ip_summed,
990 u16 pkt_len)
991{
2b162928 992 struct board_info *dm = to_dm9000_board(dev);
5dcc60b7
YP
993
994 /* The DM9000 is not smart enough to leave fragmented packets alone. */
995 if (dm->ip_summed != ip_summed) {
996 if (ip_summed == CHECKSUM_NONE)
997 iow(dm, DM9000_TCCR, 0);
998 else
999 iow(dm, DM9000_TCCR, TCCR_IP | TCCR_UDP | TCCR_TCP);
1000 dm->ip_summed = ip_summed;
1001 }
1002
1003 /* Set TX length to DM9000 */
1004 iow(dm, DM9000_TXPLL, pkt_len);
1005 iow(dm, DM9000_TXPLH, pkt_len >> 8);
1006
1007 /* Issue TX polling command */
1008 iow(dm, DM9000_TCR, TCR_TXREQ); /* Cleared after TX complete */
1009}
1010
a1365275
SH
1011/*
1012 * Hardware start transmission.
1013 * Send a packet to media from the upper layer.
1014 */
1015static int
1016dm9000_start_xmit(struct sk_buff *skb, struct net_device *dev)
1017{
c46ac946 1018 unsigned long flags;
2b162928 1019 struct board_info *db = netdev_priv(dev);
a1365275 1020
5b2b4ff0 1021 dm9000_dbg(db, 3, "%s:\n", __func__);
a1365275
SH
1022
1023 if (db->tx_pkt_cnt > 1)
5b548140 1024 return NETDEV_TX_BUSY;
a1365275 1025
c46ac946 1026 spin_lock_irqsave(&db->lock, flags);
a1365275
SH
1027
1028 /* Move data to DM9000 TX RAM */
1029 writeb(DM9000_MWCMD, db->io_addr);
1030
1031 (db->outblk)(db->io_data, skb->data, skb->len);
09f75cd7 1032 dev->stats.tx_bytes += skb->len;
a1365275 1033
c46ac946 1034 db->tx_pkt_cnt++;
a1365275 1035 /* TX control: First packet immediately send, second packet queue */
c46ac946 1036 if (db->tx_pkt_cnt == 1) {
5dcc60b7 1037 dm9000_send_packet(dev, skb->ip_summed, skb->len);
a1365275
SH
1038 } else {
1039 /* Second packet */
a1365275 1040 db->queue_pkt_len = skb->len;
5dcc60b7 1041 db->queue_ip_summed = skb->ip_summed;
c46ac946 1042 netif_stop_queue(dev);
a1365275
SH
1043 }
1044
c46ac946
FW
1045 spin_unlock_irqrestore(&db->lock, flags);
1046
a1365275 1047 /* free this SKB */
2c3d0bc0 1048 dev_consume_skb_any(skb);
a1365275 1049
6ed10654 1050 return NETDEV_TX_OK;
a1365275
SH
1051}
1052
a1365275 1053/*
f8d79e79
BD
1054 * DM9000 interrupt handler
1055 * receive the packet to upper layer, free the transmitted packet
a1365275 1056 */
f8d79e79 1057
2b162928 1058static void dm9000_tx_done(struct net_device *dev, struct board_info *db)
a1365275 1059{
f8d79e79 1060 int tx_status = ior(db, DM9000_NSR); /* Got TX status */
a1365275 1061
f8d79e79
BD
1062 if (tx_status & (NSR_TX2END | NSR_TX1END)) {
1063 /* One packet sent complete */
1064 db->tx_pkt_cnt--;
1065 dev->stats.tx_packets++;
a1365275 1066
f8d79e79
BD
1067 if (netif_msg_tx_done(db))
1068 dev_dbg(db->dev, "tx done, NSR %02x\n", tx_status);
c991d168 1069
a1365275 1070 /* Queue packet check & send */
5dcc60b7
YP
1071 if (db->tx_pkt_cnt > 0)
1072 dm9000_send_packet(dev, db->queue_ip_summed,
1073 db->queue_pkt_len);
a1365275
SH
1074 netif_wake_queue(dev);
1075 }
1076}
1077
a1365275 1078struct dm9000_rxhdr {
93116573
BD
1079 u8 RxPktReady;
1080 u8 RxStatus;
8b9fc8ae 1081 __le16 RxLen;
ba2d3587 1082} __packed;
a1365275
SH
1083
1084/*
1085 * Received a packet and pass to upper layer
1086 */
1087static void
1088dm9000_rx(struct net_device *dev)
1089{
2b162928 1090 struct board_info *db = netdev_priv(dev);
a1365275
SH
1091 struct dm9000_rxhdr rxhdr;
1092 struct sk_buff *skb;
1093 u8 rxbyte, *rdptr;
6478fac6 1094 bool GoodPacket;
a1365275
SH
1095 int RxLen;
1096
1097 /* Check packet ready or not */
1098 do {
1099 ior(db, DM9000_MRCMDX); /* Dummy read */
1100
1101 /* Get most updated data */
1102 rxbyte = readb(db->io_data);
1103
1104 /* Status check: this byte must be 0 or 1 */
5dcc60b7 1105 if (rxbyte & DM9000_PKT_ERR) {
a76836f9 1106 dev_warn(db->dev, "status check fail: %d\n", rxbyte);
a1365275 1107 iow(db, DM9000_RCR, 0x00); /* Stop Device */
a1365275
SH
1108 return;
1109 }
1110
5dcc60b7 1111 if (!(rxbyte & DM9000_PKT_RDY))
a1365275
SH
1112 return;
1113
1114 /* A packet ready now & Get status/length */
6478fac6 1115 GoodPacket = true;
a1365275
SH
1116 writeb(DM9000_MRCMD, db->io_addr);
1117
1118 (db->inblk)(db->io_data, &rxhdr, sizeof(rxhdr));
1119
93116573 1120 RxLen = le16_to_cpu(rxhdr.RxLen);
a1365275 1121
c991d168
BD
1122 if (netif_msg_rx_status(db))
1123 dev_dbg(db->dev, "RX: status %02x, length %04x\n",
1124 rxhdr.RxStatus, RxLen);
1125
a1365275
SH
1126 /* Packet Status check */
1127 if (RxLen < 0x40) {
6478fac6 1128 GoodPacket = false;
c991d168
BD
1129 if (netif_msg_rx_err(db))
1130 dev_dbg(db->dev, "RX: Bad Packet (runt)\n");
a1365275
SH
1131 }
1132
1133 if (RxLen > DM9000_PKT_MAX) {
a76836f9 1134 dev_dbg(db->dev, "RST: RX Len:%x\n", RxLen);
a1365275
SH
1135 }
1136
f8e5e776
BD
1137 /* rxhdr.RxStatus is identical to RSR register. */
1138 if (rxhdr.RxStatus & (RSR_FOE | RSR_CE | RSR_AE |
1139 RSR_PLE | RSR_RWTO |
1140 RSR_LCS | RSR_RF)) {
6478fac6 1141 GoodPacket = false;
f8e5e776 1142 if (rxhdr.RxStatus & RSR_FOE) {
c991d168
BD
1143 if (netif_msg_rx_err(db))
1144 dev_dbg(db->dev, "fifo error\n");
09f75cd7 1145 dev->stats.rx_fifo_errors++;
a1365275 1146 }
f8e5e776 1147 if (rxhdr.RxStatus & RSR_CE) {
c991d168
BD
1148 if (netif_msg_rx_err(db))
1149 dev_dbg(db->dev, "crc error\n");
09f75cd7 1150 dev->stats.rx_crc_errors++;
a1365275 1151 }
f8e5e776 1152 if (rxhdr.RxStatus & RSR_RF) {
c991d168
BD
1153 if (netif_msg_rx_err(db))
1154 dev_dbg(db->dev, "length error\n");
09f75cd7 1155 dev->stats.rx_length_errors++;
a1365275
SH
1156 }
1157 }
1158
1159 /* Move data from DM9000 */
8e95a202 1160 if (GoodPacket &&
21a4e469 1161 ((skb = netdev_alloc_skb(dev, RxLen + 4)) != NULL)) {
a1365275 1162 skb_reserve(skb, 2);
4df864c1 1163 rdptr = skb_put(skb, RxLen - 4);
a1365275
SH
1164
1165 /* Read received packet from RX SRAM */
1166
1167 (db->inblk)(db->io_data, rdptr, RxLen);
09f75cd7 1168 dev->stats.rx_bytes += RxLen;
a1365275
SH
1169
1170 /* Pass to upper layer */
1171 skb->protocol = eth_type_trans(skb, dev);
c88fcb3d 1172 if (dev->features & NETIF_F_RXCSUM) {
5dcc60b7
YP
1173 if ((((rxbyte & 0x1c) << 3) & rxbyte) == 0)
1174 skb->ip_summed = CHECKSUM_UNNECESSARY;
1175 else
bc8acf2c 1176 skb_checksum_none_assert(skb);
5dcc60b7 1177 }
a1365275 1178 netif_rx(skb);
09f75cd7 1179 dev->stats.rx_packets++;
a1365275
SH
1180
1181 } else {
1182 /* need to dump the packet's data */
1183
1184 (db->dumpblk)(db->io_data, RxLen);
1185 }
5dcc60b7 1186 } while (rxbyte & DM9000_PKT_RDY);
a1365275
SH
1187}
1188
f8d79e79 1189static irqreturn_t dm9000_interrupt(int irq, void *dev_id)
39c341a8 1190{
f8d79e79 1191 struct net_device *dev = dev_id;
2b162928 1192 struct board_info *db = netdev_priv(dev);
f8d79e79 1193 int int_status;
e3162d38 1194 unsigned long flags;
f8d79e79 1195 u8 reg_save;
39c341a8 1196
f8d79e79 1197 dm9000_dbg(db, 3, "entering %s\n", __func__);
39c341a8 1198
f8d79e79 1199 /* A real interrupt coming */
39c341a8 1200
e3162d38
DB
1201 /* holders of db->lock must always block IRQs */
1202 spin_lock_irqsave(&db->lock, flags);
39c341a8 1203
f8d79e79
BD
1204 /* Save previous register address */
1205 reg_save = readb(db->io_addr);
39c341a8 1206
17ad78de 1207 dm9000_mask_interrupts(db);
f8d79e79
BD
1208 /* Got DM9000 interrupt status */
1209 int_status = ior(db, DM9000_ISR); /* Got ISR */
1210 iow(db, DM9000_ISR, int_status); /* Clear ISR status */
39c341a8 1211
f8d79e79
BD
1212 if (netif_msg_intr(db))
1213 dev_dbg(db->dev, "interrupt status %02x\n", int_status);
1214
1215 /* Received the coming packet */
1216 if (int_status & ISR_PRS)
1217 dm9000_rx(dev);
1218
7b901873 1219 /* Transmit Interrupt check */
f8d79e79
BD
1220 if (int_status & ISR_PTS)
1221 dm9000_tx_done(dev, db);
1222
1223 if (db->type != TYPE_DM9000E) {
1224 if (int_status & ISR_LNKCHNG) {
1225 /* fire a link-change request */
1226 schedule_delayed_work(&db->phy_poll, 1);
39c341a8
BD
1227 }
1228 }
1229
17ad78de 1230 dm9000_unmask_interrupts(db);
f8d79e79
BD
1231 /* Restore previous register address */
1232 writeb(reg_save, db->io_addr);
1233
e3162d38 1234 spin_unlock_irqrestore(&db->lock, flags);
f8d79e79
BD
1235
1236 return IRQ_HANDLED;
39c341a8
BD
1237}
1238
c029f444
BD
1239static irqreturn_t dm9000_wol_interrupt(int irq, void *dev_id)
1240{
1241 struct net_device *dev = dev_id;
2b162928 1242 struct board_info *db = netdev_priv(dev);
c029f444
BD
1243 unsigned long flags;
1244 unsigned nsr, wcr;
1245
1246 spin_lock_irqsave(&db->lock, flags);
1247
1248 nsr = ior(db, DM9000_NSR);
1249 wcr = ior(db, DM9000_WCR);
1250
1251 dev_dbg(db->dev, "%s: NSR=0x%02x, WCR=0x%02x\n", __func__, nsr, wcr);
1252
1253 if (nsr & NSR_WAKEST) {
1254 /* clear, so we can avoid */
1255 iow(db, DM9000_NSR, NSR_WAKEST);
1256
1257 if (wcr & WCR_LINKST)
1258 dev_info(db->dev, "wake by link status change\n");
1259 if (wcr & WCR_SAMPLEST)
1260 dev_info(db->dev, "wake by sample packet\n");
5b22721d 1261 if (wcr & WCR_MAGICST)
c029f444
BD
1262 dev_info(db->dev, "wake by magic packet\n");
1263 if (!(wcr & (WCR_LINKST | WCR_SAMPLEST | WCR_MAGICST)))
1264 dev_err(db->dev, "wake signalled with no reason? "
1265 "NSR=0x%02x, WSR=0x%02x\n", nsr, wcr);
c029f444
BD
1266 }
1267
1268 spin_unlock_irqrestore(&db->lock, flags);
1269
1270 return (nsr & NSR_WAKEST) ? IRQ_HANDLED : IRQ_NONE;
1271}
1272
f8d79e79 1273#ifdef CONFIG_NET_POLL_CONTROLLER
a1365275 1274/*
f8d79e79 1275 *Used by netconsole
a1365275 1276 */
f8d79e79 1277static void dm9000_poll_controller(struct net_device *dev)
a1365275 1278{
f8d79e79
BD
1279 disable_irq(dev->irq);
1280 dm9000_interrupt(dev->irq, dev);
1281 enable_irq(dev->irq);
1282}
1283#endif
9a2f037c 1284
f8d79e79
BD
1285/*
1286 * Open the interface.
1287 * The interface is opened whenever "ifconfig" actives it.
1288 */
1289static int
1290dm9000_open(struct net_device *dev)
1291{
2b162928 1292 struct board_info *db = netdev_priv(dev);
a96d3b75 1293 unsigned int irq_flags = irq_get_trigger_type(dev->irq);
621ddcb0 1294
f8d79e79
BD
1295 if (netif_msg_ifup(db))
1296 dev_dbg(db->dev, "enabling %s\n", dev->name);
621ddcb0 1297
b5a099c6
RJ
1298 /* If there is no IRQ type specified, tell the user that this is a
1299 * problem
1300 */
a96d3b75 1301 if (irq_flags == IRQF_TRIGGER_NONE)
f8d79e79 1302 dev_warn(db->dev, "WARNING: no IRQ resource flags set.\n");
6ff4ff06 1303
a96d3b75
SN
1304 irq_flags |= IRQF_SHARED;
1305
108f518c
HN
1306 /* GPIO0 on pre-activate PHY, Reg 1F is not set by reset */
1307 iow(db, DM9000_GPR, 0); /* REG_1F bit0 activate phyxcer */
1308 mdelay(1); /* delay needs by DM9000B */
1309
f8d79e79 1310 /* Initialize DM9000 board */
f8d79e79 1311 dm9000_init_dm9000(dev);
621ddcb0 1312
a96d3b75 1313 if (request_irq(dev->irq, dm9000_interrupt, irq_flags, dev->name, dev))
6979d5dd 1314 return -EAGAIN;
17ad78de
AR
1315 /* Now that we have an interrupt handler hooked up we can unmask
1316 * our interrupts
1317 */
1318 dm9000_unmask_interrupts(db);
6979d5dd 1319
f8d79e79
BD
1320 /* Init driver variable */
1321 db->dbug_cnt = 0;
86c62fab 1322
f8d79e79
BD
1323 mii_check_media(&db->mii, netif_msg_link(db), 1);
1324 netif_start_queue(dev);
5b22721d 1325
aac6d022
AR
1326 /* Poll initial link status */
1327 schedule_delayed_work(&db->phy_poll, 1);
9a2f037c 1328
f8d79e79
BD
1329 return 0;
1330}
621ddcb0 1331
f8d79e79
BD
1332static void
1333dm9000_shutdown(struct net_device *dev)
1334{
2b162928 1335 struct board_info *db = netdev_priv(dev);
f8d79e79
BD
1336
1337 /* RESET device */
1338 dm9000_phy_write(dev, 0, MII_BMCR, BMCR_RESET); /* PHY RESET */
1339 iow(db, DM9000_GPR, 0x01); /* Power-Down PHY */
17ad78de 1340 dm9000_mask_interrupts(db);
f8d79e79
BD
1341 iow(db, DM9000_RCR, 0x00); /* Disable RX */
1342}
1343
1344/*
1345 * Stop the interface.
1346 * The interface is stopped when it is brought.
1347 */
1348static int
1349dm9000_stop(struct net_device *ndev)
1350{
2b162928 1351 struct board_info *db = netdev_priv(ndev);
f8d79e79
BD
1352
1353 if (netif_msg_ifdown(db))
1354 dev_dbg(db->dev, "shutting down %s\n", ndev->name);
1355
1356 cancel_delayed_work_sync(&db->phy_poll);
1357
1358 netif_stop_queue(ndev);
1359 netif_carrier_off(ndev);
1360
1361 /* free interrupt */
1362 free_irq(ndev->irq, ndev);
1363
1364 dm9000_shutdown(ndev);
1365
1366 return 0;
1367}
1368
d88106b7
AB
1369static const struct net_device_ops dm9000_netdev_ops = {
1370 .ndo_open = dm9000_open,
1371 .ndo_stop = dm9000_stop,
1372 .ndo_start_xmit = dm9000_start_xmit,
1373 .ndo_tx_timeout = dm9000_timeout,
afc4b13d 1374 .ndo_set_rx_mode = dm9000_hash_table,
d88106b7 1375 .ndo_do_ioctl = dm9000_ioctl,
c88fcb3d 1376 .ndo_set_features = dm9000_set_features,
d88106b7
AB
1377 .ndo_validate_addr = eth_validate_addr,
1378 .ndo_set_mac_address = eth_mac_addr,
1379#ifdef CONFIG_NET_POLL_CONTROLLER
1380 .ndo_poll_controller = dm9000_poll_controller,
1381#endif
1382};
1383
0b8bf1ba
TF
1384static struct dm9000_plat_data *dm9000_parse_dt(struct device *dev)
1385{
1386 struct dm9000_plat_data *pdata;
1387 struct device_node *np = dev->of_node;
1388 const void *mac_addr;
1389
1390 if (!IS_ENABLED(CONFIG_OF) || !np)
09f3756b 1391 return ERR_PTR(-ENXIO);
0b8bf1ba
TF
1392
1393 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1394 if (!pdata)
1395 return ERR_PTR(-ENOMEM);
1396
1397 if (of_find_property(np, "davicom,ext-phy", NULL))
1398 pdata->flags |= DM9000_PLATF_EXT_PHY;
1399 if (of_find_property(np, "davicom,no-eeprom", NULL))
1400 pdata->flags |= DM9000_PLATF_NO_EEPROM;
1401
1402 mac_addr = of_get_mac_address(np);
a51645f7 1403 if (!IS_ERR(mac_addr))
2d2924af 1404 ether_addr_copy(pdata->dev_addr, mac_addr);
9a6a0dea
PC
1405 else if (PTR_ERR(mac_addr) == -EPROBE_DEFER)
1406 return ERR_CAST(mac_addr);
0b8bf1ba
TF
1407
1408 return pdata;
1409}
1410
f8d79e79
BD
1411/*
1412 * Search DM9000 board, allocate space and register it
1413 */
6b6a3e7f 1414static int
f8d79e79
BD
1415dm9000_probe(struct platform_device *pdev)
1416{
cd4e2e4b 1417 struct dm9000_plat_data *pdata = dev_get_platdata(&pdev->dev);
f8d79e79
BD
1418 struct board_info *db; /* Point a board information structure */
1419 struct net_device *ndev;
7994fe55 1420 struct device *dev = &pdev->dev;
f8d79e79
BD
1421 const unsigned char *mac_src;
1422 int ret = 0;
1423 int iosize;
1424 int i;
1425 u32 id_val;
7994fe55
ZLK
1426 int reset_gpios;
1427 enum of_gpio_flags flags;
1428 struct regulator *power;
3274940b 1429 bool inv_mac_addr = false;
7994fe55
ZLK
1430
1431 power = devm_regulator_get(dev, "vcc");
1432 if (IS_ERR(power)) {
1433 if (PTR_ERR(power) == -EPROBE_DEFER)
1434 return -EPROBE_DEFER;
1435 dev_dbg(dev, "no regulator provided\n");
1436 } else {
1437 ret = regulator_enable(power);
1438 if (ret != 0) {
1439 dev_err(dev,
1440 "Failed to enable power regulator: %d\n", ret);
1441 return ret;
1442 }
1443 dev_dbg(dev, "regulator enabled\n");
1444 }
1445
1446 reset_gpios = of_get_named_gpio_flags(dev->of_node, "reset-gpios", 0,
1447 &flags);
1448 if (gpio_is_valid(reset_gpios)) {
1449 ret = devm_gpio_request_one(dev, reset_gpios, flags,
1450 "dm9000_reset");
1451 if (ret) {
1452 dev_err(dev, "failed to request reset gpio %d: %d\n",
1453 reset_gpios, ret);
3746ccf3 1454 goto out_regulator_disable;
7994fe55
ZLK
1455 }
1456
1457 /* According to manual PWRST# Low Period Min 1ms */
1458 msleep(2);
1459 gpio_set_value(reset_gpios, 1);
1460 /* Needs 3ms to read eeprom when PWRST is deasserted */
1461 msleep(4);
1462 }
f8d79e79 1463
0b8bf1ba
TF
1464 if (!pdata) {
1465 pdata = dm9000_parse_dt(&pdev->dev);
3746ccf3
PC
1466 if (IS_ERR(pdata)) {
1467 ret = PTR_ERR(pdata);
1468 goto out_regulator_disable;
1469 }
0b8bf1ba
TF
1470 }
1471
f8d79e79
BD
1472 /* Init network device */
1473 ndev = alloc_etherdev(sizeof(struct board_info));
b9b5e868
CJ
1474 if (!ndev) {
1475 ret = -ENOMEM;
1476 goto out_regulator_disable;
1477 }
f8d79e79
BD
1478
1479 SET_NETDEV_DEV(ndev, &pdev->dev);
1480
1481 dev_dbg(&pdev->dev, "dm9000_probe()\n");
1482
1483 /* setup board info structure */
4cf1653a 1484 db = netdev_priv(ndev);
f8d79e79
BD
1485
1486 db->dev = &pdev->dev;
1487 db->ndev = ndev;
c4eb7e7c
PC
1488 if (!IS_ERR(power))
1489 db->power_supply = power;
f8d79e79
BD
1490
1491 spin_lock_init(&db->lock);
1492 mutex_init(&db->addr_lock);
1493
1494 INIT_DELAYED_WORK(&db->phy_poll, dm9000_poll_work);
1495
1496 db->addr_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1497 db->data_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
f8d79e79 1498
b5a099c6
RJ
1499 if (!db->addr_res || !db->data_res) {
1500 dev_err(db->dev, "insufficient resources addr=%p data=%p\n",
1501 db->addr_res, db->data_res);
f8d79e79
BD
1502 ret = -ENOENT;
1503 goto out;
1504 }
1505
b5a099c6
RJ
1506 ndev->irq = platform_get_irq(pdev, 0);
1507 if (ndev->irq < 0) {
b5a099c6
RJ
1508 ret = ndev->irq;
1509 goto out;
1510 }
1511
d52b8151 1512 db->irq_wake = platform_get_irq_optional(pdev, 1);
c029f444
BD
1513 if (db->irq_wake >= 0) {
1514 dev_dbg(db->dev, "wakeup irq %d\n", db->irq_wake);
1515
1516 ret = request_irq(db->irq_wake, dm9000_wol_interrupt,
1517 IRQF_SHARED, dev_name(db->dev), ndev);
1518 if (ret) {
1519 dev_err(db->dev, "cannot get wakeup irq (%d)\n", ret);
1520 } else {
1521
1522 /* test to see if irq is really wakeup capable */
dced35ae 1523 ret = irq_set_irq_wake(db->irq_wake, 1);
c029f444
BD
1524 if (ret) {
1525 dev_err(db->dev, "irq %d cannot set wakeup (%d)\n",
1526 db->irq_wake, ret);
1527 ret = 0;
1528 } else {
dced35ae 1529 irq_set_irq_wake(db->irq_wake, 0);
c029f444
BD
1530 db->wake_supported = 1;
1531 }
1532 }
1533 }
1534
ec282e92 1535 iosize = resource_size(db->addr_res);
f8d79e79
BD
1536 db->addr_req = request_mem_region(db->addr_res->start, iosize,
1537 pdev->name);
1538
1539 if (db->addr_req == NULL) {
1540 dev_err(db->dev, "cannot claim address reg area\n");
1541 ret = -EIO;
1542 goto out;
1543 }
1544
1545 db->io_addr = ioremap(db->addr_res->start, iosize);
1546
1547 if (db->io_addr == NULL) {
1548 dev_err(db->dev, "failed to ioremap address reg\n");
1549 ret = -EINVAL;
1550 goto out;
1551 }
1552
ec282e92 1553 iosize = resource_size(db->data_res);
f8d79e79
BD
1554 db->data_req = request_mem_region(db->data_res->start, iosize,
1555 pdev->name);
1556
1557 if (db->data_req == NULL) {
1558 dev_err(db->dev, "cannot claim data reg area\n");
1559 ret = -EIO;
1560 goto out;
1561 }
1562
1563 db->io_data = ioremap(db->data_res->start, iosize);
1564
1565 if (db->io_data == NULL) {
1566 dev_err(db->dev, "failed to ioremap data reg\n");
1567 ret = -EINVAL;
1568 goto out;
1569 }
1570
1571 /* fill in parameters for net-dev structure */
1572 ndev->base_addr = (unsigned long)db->io_addr;
f8d79e79
BD
1573
1574 /* ensure at least we have a default set of IO routines */
1575 dm9000_set_io(db, iosize);
1576
1577 /* check to see if anything is being over-ridden */
1578 if (pdata != NULL) {
1579 /* check to see if the driver wants to over-ride the
1580 * default IO width */
1581
1582 if (pdata->flags & DM9000_PLATF_8BITONLY)
1583 dm9000_set_io(db, 1);
1584
1585 if (pdata->flags & DM9000_PLATF_16BITONLY)
1586 dm9000_set_io(db, 2);
1587
1588 if (pdata->flags & DM9000_PLATF_32BITONLY)
1589 dm9000_set_io(db, 4);
1590
1591 /* check to see if there are any IO routine
1592 * over-rides */
1593
1594 if (pdata->inblk != NULL)
1595 db->inblk = pdata->inblk;
1596
1597 if (pdata->outblk != NULL)
1598 db->outblk = pdata->outblk;
1599
1600 if (pdata->dumpblk != NULL)
1601 db->dumpblk = pdata->dumpblk;
1602
1603 db->flags = pdata->flags;
1604 }
1605
f8dd0ecb
BD
1606#ifdef CONFIG_DM9000_FORCE_SIMPLE_PHY_POLL
1607 db->flags |= DM9000_PLATF_SIMPLE_PHY;
1608#endif
1609
751bb6fd 1610 dm9000_reset(db);
f8d79e79
BD
1611
1612 /* try multiple times, DM9000 sometimes gets the read wrong */
1613 for (i = 0; i < 8; i++) {
1614 id_val = ior(db, DM9000_VIDL);
1615 id_val |= (u32)ior(db, DM9000_VIDH) << 8;
1616 id_val |= (u32)ior(db, DM9000_PIDL) << 16;
1617 id_val |= (u32)ior(db, DM9000_PIDH) << 24;
1618
1619 if (id_val == DM9000_ID)
1620 break;
1621 dev_err(db->dev, "read wrong id 0x%08x\n", id_val);
1622 }
1623
1624 if (id_val != DM9000_ID) {
1625 dev_err(db->dev, "wrong id: 0x%08x\n", id_val);
1626 ret = -ENODEV;
1627 goto out;
1628 }
1629
1630 /* Identify what type of DM9000 we are working on */
1631
1632 id_val = ior(db, DM9000_CHIPR);
1633 dev_dbg(db->dev, "dm9000 revision 0x%02x\n", id_val);
1634
1635 switch (id_val) {
1636 case CHIPR_DM9000A:
1637 db->type = TYPE_DM9000A;
1638 break;
1639 case CHIPR_DM9000B:
1640 db->type = TYPE_DM9000B;
1641 break;
1642 default:
1643 dev_dbg(db->dev, "ID %02x => defaulting to DM9000E\n", id_val);
1644 db->type = TYPE_DM9000E;
1645 }
1646
5dcc60b7
YP
1647 /* dm9000a/b are capable of hardware checksum offload */
1648 if (db->type == TYPE_DM9000A || db->type == TYPE_DM9000B) {
c88fcb3d
MM
1649 ndev->hw_features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM;
1650 ndev->features |= ndev->hw_features;
5dcc60b7
YP
1651 }
1652
f8d79e79
BD
1653 /* from this point we assume that we have found a DM9000 */
1654
d88106b7
AB
1655 ndev->netdev_ops = &dm9000_netdev_ops;
1656 ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
1657 ndev->ethtool_ops = &dm9000_ethtool_ops;
f8d79e79
BD
1658
1659 db->msg_enable = NETIF_MSG_LINK;
1660 db->mii.phy_id_mask = 0x1f;
1661 db->mii.reg_num_mask = 0x1f;
1662 db->mii.force_media = 0;
1663 db->mii.full_duplex = 0;
1664 db->mii.dev = ndev;
1665 db->mii.mdio_read = dm9000_phy_read;
1666 db->mii.mdio_write = dm9000_phy_write;
1667
1668 mac_src = "eeprom";
1669
1670 /* try reading the node address from the attached EEPROM */
1671 for (i = 0; i < 6; i += 2)
1672 dm9000_read_eeprom(db, i / 2, ndev->dev_addr+i);
1673
fe414248
LP
1674 if (!is_valid_ether_addr(ndev->dev_addr) && pdata != NULL) {
1675 mac_src = "platform data";
d458cdf7 1676 memcpy(ndev->dev_addr, pdata->dev_addr, ETH_ALEN);
fe414248
LP
1677 }
1678
f8d79e79
BD
1679 if (!is_valid_ether_addr(ndev->dev_addr)) {
1680 /* try reading from mac */
5b22721d 1681
f8d79e79
BD
1682 mac_src = "chip";
1683 for (i = 0; i < 6; i++)
1684 ndev->dev_addr[i] = ior(db, i+DM9000_PAR);
1685 }
1686
85e6b8c5 1687 if (!is_valid_ether_addr(ndev->dev_addr)) {
3274940b 1688 inv_mac_addr = true;
f2cedb63 1689 eth_hw_addr_random(ndev);
85e6b8c5
BD
1690 mac_src = "random";
1691 }
1692
1693
f8d79e79
BD
1694 platform_set_drvdata(pdev, ndev);
1695 ret = register_netdev(ndev);
1696
3274940b
HH
1697 if (ret == 0) {
1698 if (inv_mac_addr)
1699 dev_warn(db->dev, "%s: Invalid ethernet MAC address. Please set using ip\n",
1700 ndev->name);
e174961c 1701 printk(KERN_INFO "%s: dm9000%c at %p,%p IRQ %d MAC: %pM (%s)\n",
f8d79e79
BD
1702 ndev->name, dm9000_type_to_char(db->type),
1703 db->io_addr, db->io_data, ndev->irq,
e174961c 1704 ndev->dev_addr, mac_src);
3274940b 1705 }
f8d79e79
BD
1706 return 0;
1707
1708out:
1709 dev_err(db->dev, "not found (%d).\n", ret);
1710
1711 dm9000_release_board(pdev, db);
1712 free_netdev(ndev);
1713
3746ccf3
PC
1714out_regulator_disable:
1715 if (!IS_ERR(power))
1716 regulator_disable(power);
1717
f8d79e79
BD
1718 return ret;
1719}
1720
a1365275 1721static int
69222e2c 1722dm9000_drv_suspend(struct device *dev)
a1365275 1723{
3fcdaad3 1724 struct net_device *ndev = dev_get_drvdata(dev);
2b162928 1725 struct board_info *db;
a1365275 1726
9480e307 1727 if (ndev) {
4cf1653a 1728 db = netdev_priv(ndev);
321f69a4
BD
1729 db->in_suspend = 1;
1730
c029f444
BD
1731 if (!netif_running(ndev))
1732 return 0;
1733
1734 netif_device_detach(ndev);
1735
1736 /* only shutdown if not using WoL */
1737 if (!db->wake_state)
a1365275 1738 dm9000_shutdown(ndev);
a1365275
SH
1739 }
1740 return 0;
1741}
1742
1743static int
69222e2c 1744dm9000_drv_resume(struct device *dev)
a1365275 1745{
3fcdaad3 1746 struct net_device *ndev = dev_get_drvdata(dev);
2b162928 1747 struct board_info *db = netdev_priv(ndev);
a1365275 1748
9480e307 1749 if (ndev) {
a1365275 1750 if (netif_running(ndev)) {
c029f444
BD
1751 /* reset if we were not in wake mode to ensure if
1752 * the device was powered off it is in a known state */
1753 if (!db->wake_state) {
c029f444 1754 dm9000_init_dm9000(ndev);
17ad78de 1755 dm9000_unmask_interrupts(db);
c029f444 1756 }
a1365275
SH
1757
1758 netif_device_attach(ndev);
1759 }
321f69a4
BD
1760
1761 db->in_suspend = 0;
a1365275
SH
1762 }
1763 return 0;
1764}
1765
47145210 1766static const struct dev_pm_ops dm9000_drv_pm_ops = {
69222e2c
MR
1767 .suspend = dm9000_drv_suspend,
1768 .resume = dm9000_drv_resume,
1769};
1770
6b6a3e7f 1771static int
3ae5eaec 1772dm9000_drv_remove(struct platform_device *pdev)
a1365275 1773{
3ae5eaec 1774 struct net_device *ndev = platform_get_drvdata(pdev);
c4eb7e7c 1775 struct board_info *dm = to_dm9000_board(ndev);
a1365275 1776
a1365275 1777 unregister_netdev(ndev);
c4eb7e7c 1778 dm9000_release_board(pdev, dm);
9fd9f9b6 1779 free_netdev(ndev); /* free device structure */
c4eb7e7c
PC
1780 if (dm->power_supply)
1781 regulator_disable(dm->power_supply);
a1365275 1782
a76836f9 1783 dev_dbg(&pdev->dev, "released and freed device\n");
a1365275
SH
1784 return 0;
1785}
1786
0b8bf1ba
TF
1787#ifdef CONFIG_OF
1788static const struct of_device_id dm9000_of_matches[] = {
1789 { .compatible = "davicom,dm9000", },
1790 { /* sentinel */ }
1791};
1792MODULE_DEVICE_TABLE(of, dm9000_of_matches);
1793#endif
1794
3ae5eaec 1795static struct platform_driver dm9000_driver = {
5d22a312
BD
1796 .driver = {
1797 .name = "dm9000",
69222e2c 1798 .pm = &dm9000_drv_pm_ops,
0b8bf1ba 1799 .of_match_table = of_match_ptr(dm9000_of_matches),
5d22a312 1800 },
a1365275 1801 .probe = dm9000_probe,
6b6a3e7f 1802 .remove = dm9000_drv_remove,
a1365275
SH
1803};
1804
a8f9c3e4 1805module_platform_driver(dm9000_driver);
a1365275
SH
1806
1807MODULE_AUTHOR("Sascha Hauer, Ben Dooks");
1808MODULE_DESCRIPTION("Davicom DM9000 network driver");
1809MODULE_LICENSE("GPL");
72abb461 1810MODULE_ALIAS("platform:dm9000");