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