blob: 1bdf69985c13f7a545d81e1feae73d505a7bb3ee [file] [log] [blame]
Sascha Hauera1365272005-05-05 15:14:15 -07001/*
Ben Dooks41c340f2008-02-05 00:02:15 +00002 * Davicom DM9000 Fast Ethernet driver for Linux.
Sascha Hauera1365272005-05-05 15:14:15 -07003 * Copyright (C) 1997 Sten Wang
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
Ben Dooks41c340f2008-02-05 00:02:15 +000015 * (C) Copyright 1997-1998 DAVICOM Semiconductor,Inc. All Rights Reserved.
Sascha Hauera1365272005-05-05 15:14:15 -070016 *
Ben Dooks41c340f2008-02-05 00:02:15 +000017 * Additional updates, Copyright:
18 * Ben Dooks <ben@simtec.co.uk>
19 * Sascha Hauer <s.hauer@pengutronix.de>
Sascha Hauera1365272005-05-05 15:14:15 -070020 */
21
22#include <linux/module.h>
23#include <linux/ioport.h>
24#include <linux/netdevice.h>
25#include <linux/etherdevice.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000026#include <linux/interrupt.h>
Sascha Hauera1365272005-05-05 15:14:15 -070027#include <linux/skbuff.h>
Sascha Hauera1365272005-05-05 15:14:15 -070028#include <linux/spinlock.h>
29#include <linux/crc32.h>
30#include <linux/mii.h>
Tomasz Figa0b8bf1b2013-05-20 09:16:58 +000031#include <linux/of.h>
32#include <linux/of_net.h>
Ben Dooks7da99852008-02-05 00:02:06 +000033#include <linux/ethtool.h>
Sascha Hauera1365272005-05-05 15:14:15 -070034#include <linux/dm9000.h>
35#include <linux/delay.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010036#include <linux/platform_device.h>
Daniel Mack4e4fc052008-01-23 14:54:50 +010037#include <linux/irq.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
Sascha Hauera1365272005-05-05 15:14:15 -070039
40#include <asm/delay.h>
41#include <asm/irq.h>
42#include <asm/io.h>
43
44#include "dm9000.h"
45
46/* Board/System/Debug information/definition ---------------- */
47
48#define DM9000_PHY 0x40 /* PHY address 0x01 */
49
Ben Dooks59eae1f2008-06-24 22:16:01 +010050#define CARDNAME "dm9000"
51#define DRV_VERSION "1.31"
Sascha Hauera1365272005-05-05 15:14:15 -070052
Sascha Hauera1365272005-05-05 15:14:15 -070053/*
54 * Transmit timeout, default 5 seconds.
55 */
56static int watchdog = 5000;
57module_param(watchdog, int, 0400);
58MODULE_PARM_DESC(watchdog, "transmit timeout in milliseconds");
59
Vladimir Zapolskiy2e025c72011-08-20 14:15:55 -070060/*
61 * Debug messages level
62 */
63static int debug;
64module_param(debug, int, 0644);
65MODULE_PARM_DESC(debug, "dm9000 debug level (0-4)");
66
Ben Dooks9a2f0372008-02-05 00:02:10 +000067/* DM9000 register address locking.
68 *
69 * The DM9000 uses an address register to control where data written
70 * to the data register goes. This means that the address register
71 * must be preserved over interrupts or similar calls.
72 *
73 * During interrupt and other critical calls, a spinlock is used to
74 * protect the system, but the calls themselves save the address
75 * in the address register in case they are interrupting another
76 * access to the device.
77 *
78 * For general accesses a lock is provided so that calls which are
79 * allowed to sleep are serialised so that the address register does
80 * not need to be saved. This lock also serves to serialise access
81 * to the EEPROM and PHY access registers which are shared between
82 * these two devices.
83 */
84
Ben Dooks6d406b32008-06-24 22:15:59 +010085/* The driver supports the original DM9000E, and now the two newer
86 * devices, DM9000A and DM9000B.
87 */
88
89enum dm9000_type {
90 TYPE_DM9000E, /* original DM9000 */
91 TYPE_DM9000A,
92 TYPE_DM9000B
93};
94
Sascha Hauera1365272005-05-05 15:14:15 -070095/* Structure/enum declaration ------------------------------- */
96typedef struct board_info {
97
Ben Dooks59eae1f2008-06-24 22:16:01 +010098 void __iomem *io_addr; /* Register I/O base address */
99 void __iomem *io_data; /* Data I/O address */
100 u16 irq; /* IRQ */
Sascha Hauera1365272005-05-05 15:14:15 -0700101
Ben Dooks59eae1f2008-06-24 22:16:01 +0100102 u16 tx_pkt_cnt;
103 u16 queue_pkt_len;
104 u16 queue_start_addr;
Yeasah Pell5dcc60b2009-07-06 18:12:33 -0700105 u16 queue_ip_summed;
Ben Dooks59eae1f2008-06-24 22:16:01 +0100106 u16 dbug_cnt;
107 u8 io_mode; /* 0:word, 2:byte */
108 u8 phy_addr;
109 u8 imr_all;
110
111 unsigned int flags;
Barry Song5b227212014-01-15 23:31:46 +0800112 unsigned int in_suspend:1;
113 unsigned int wake_supported:1;
Sascha Hauera1365272005-05-05 15:14:15 -0700114
Ben Dooks6d406b32008-06-24 22:15:59 +0100115 enum dm9000_type type;
Ben Dooks5b2b4ff2008-02-05 00:02:03 +0000116
Sascha Hauera1365272005-05-05 15:14:15 -0700117 void (*inblk)(void __iomem *port, void *data, int length);
118 void (*outblk)(void __iomem *port, void *data, int length);
119 void (*dumpblk)(void __iomem *port, int length);
120
Ben Dooksa76836f2008-02-05 00:02:02 +0000121 struct device *dev; /* parent device */
122
Sascha Hauera1365272005-05-05 15:14:15 -0700123 struct resource *addr_res; /* resources found */
124 struct resource *data_res;
125 struct resource *addr_req; /* resources requested */
126 struct resource *data_req;
127 struct resource *irq_res;
128
Ben Dooksc029f442009-11-10 07:22:24 +0000129 int irq_wake;
130
Ben Dooks9a2f0372008-02-05 00:02:10 +0000131 struct mutex addr_lock; /* phy and eeprom access lock */
132
Ben Dooks8f5bf5f22008-05-08 11:36:42 +0100133 struct delayed_work phy_poll;
134 struct net_device *ndev;
135
Ben Dooks59eae1f2008-06-24 22:16:01 +0100136 spinlock_t lock;
Sascha Hauera1365272005-05-05 15:14:15 -0700137
138 struct mii_if_info mii;
Ben Dooks59eae1f2008-06-24 22:16:01 +0100139 u32 msg_enable;
Ben Dooksc029f442009-11-10 07:22:24 +0000140 u32 wake_state;
Yeasah Pell5dcc60b2009-07-06 18:12:33 -0700141
Yeasah Pell5dcc60b2009-07-06 18:12:33 -0700142 int ip_summed;
Sascha Hauera1365272005-05-05 15:14:15 -0700143} board_info_t;
144
Ben Dooks5b2b4ff2008-02-05 00:02:03 +0000145/* debug code */
146
147#define dm9000_dbg(db, lev, msg...) do { \
Vladimir Zapolskiy2e025c72011-08-20 14:15:55 -0700148 if ((lev) < debug) { \
Ben Dooks5b2b4ff2008-02-05 00:02:03 +0000149 dev_dbg(db->dev, msg); \
150 } \
151} while (0)
152
Ben Dooks7da99852008-02-05 00:02:06 +0000153static inline board_info_t *to_dm9000_board(struct net_device *dev)
154{
Wang Chen4cf16532008-11-12 23:38:14 -0800155 return netdev_priv(dev);
Ben Dooks7da99852008-02-05 00:02:06 +0000156}
157
Sascha Hauera1365272005-05-05 15:14:15 -0700158/* DM9000 network board routine ---------------------------- */
159
Sascha Hauera1365272005-05-05 15:14:15 -0700160/*
161 * Read a byte from I/O port
162 */
163static u8
Barry Song5b227212014-01-15 23:31:46 +0800164ior(board_info_t *db, int reg)
Sascha Hauera1365272005-05-05 15:14:15 -0700165{
166 writeb(reg, db->io_addr);
167 return readb(db->io_data);
168}
169
170/*
171 * Write a byte to I/O port
172 */
173
174static void
Barry Song5b227212014-01-15 23:31:46 +0800175iow(board_info_t *db, int reg, int value)
Sascha Hauera1365272005-05-05 15:14:15 -0700176{
177 writeb(reg, db->io_addr);
178 writeb(value, db->io_data);
179}
180
Michael Abbott09ee9f82013-10-16 11:41:33 +0300181static void
182dm9000_reset(board_info_t *db)
183{
184 dev_dbg(db->dev, "resetting device\n");
185
186 /* Reset DM9000, see DM9000 Application Notes V1.22 Jun 11, 2004 page 29
187 * The essential point is that we have to do a double reset, and the
188 * instruction is to set LBK into MAC internal loopback mode.
189 */
190 iow(db, DM9000_NCR, 0x03);
191 udelay(100); /* Application note says at least 20 us */
192 if (ior(db, DM9000_NCR) & 1)
193 dev_err(db->dev, "dm9000 did not respond to first reset\n");
194
195 iow(db, DM9000_NCR, 0);
196 iow(db, DM9000_NCR, 0x03);
197 udelay(100);
198 if (ior(db, DM9000_NCR) & 1)
199 dev_err(db->dev, "dm9000 did not respond to second reset\n");
200}
201
Sascha Hauera1365272005-05-05 15:14:15 -0700202/* routines for sending block to chip */
203
204static void dm9000_outblk_8bit(void __iomem *reg, void *data, int count)
205{
Matthew Leachdaadaf62012-12-10 09:12:37 +0000206 iowrite8_rep(reg, data, count);
Sascha Hauera1365272005-05-05 15:14:15 -0700207}
208
209static void dm9000_outblk_16bit(void __iomem *reg, void *data, int count)
210{
Matthew Leachdaadaf62012-12-10 09:12:37 +0000211 iowrite16_rep(reg, data, (count+1) >> 1);
Sascha Hauera1365272005-05-05 15:14:15 -0700212}
213
214static void dm9000_outblk_32bit(void __iomem *reg, void *data, int count)
215{
Matthew Leachdaadaf62012-12-10 09:12:37 +0000216 iowrite32_rep(reg, data, (count+3) >> 2);
Sascha Hauera1365272005-05-05 15:14:15 -0700217}
218
219/* input block from chip to memory */
220
221static void dm9000_inblk_8bit(void __iomem *reg, void *data, int count)
222{
Matthew Leachdaadaf62012-12-10 09:12:37 +0000223 ioread8_rep(reg, data, count);
Sascha Hauera1365272005-05-05 15:14:15 -0700224}
225
226
227static void dm9000_inblk_16bit(void __iomem *reg, void *data, int count)
228{
Matthew Leachdaadaf62012-12-10 09:12:37 +0000229 ioread16_rep(reg, data, (count+1) >> 1);
Sascha Hauera1365272005-05-05 15:14:15 -0700230}
231
232static void dm9000_inblk_32bit(void __iomem *reg, void *data, int count)
233{
Matthew Leachdaadaf62012-12-10 09:12:37 +0000234 ioread32_rep(reg, data, (count+3) >> 2);
Sascha Hauera1365272005-05-05 15:14:15 -0700235}
236
237/* dump block from chip to null */
238
239static void dm9000_dumpblk_8bit(void __iomem *reg, int count)
240{
241 int i;
242 int tmp;
243
244 for (i = 0; i < count; i++)
245 tmp = readb(reg);
246}
247
248static void dm9000_dumpblk_16bit(void __iomem *reg, int count)
249{
250 int i;
251 int tmp;
252
253 count = (count + 1) >> 1;
254
255 for (i = 0; i < count; i++)
256 tmp = readw(reg);
257}
258
259static void dm9000_dumpblk_32bit(void __iomem *reg, int count)
260{
261 int i;
262 int tmp;
263
264 count = (count + 3) >> 2;
265
266 for (i = 0; i < count; i++)
267 tmp = readl(reg);
268}
269
Joseph CHANG6741f40d2013-03-28 23:13:42 +0000270/*
271 * Sleep, either by using msleep() or if we are suspending, then
272 * use mdelay() to sleep.
273 */
274static void dm9000_msleep(board_info_t *db, unsigned int ms)
275{
276 if (db->in_suspend)
277 mdelay(ms);
278 else
279 msleep(ms);
280}
281
282/* Read a word from phyxcer */
283static int
284dm9000_phy_read(struct net_device *dev, int phy_reg_unused, int reg)
285{
286 board_info_t *db = netdev_priv(dev);
287 unsigned long flags;
288 unsigned int reg_save;
289 int ret;
290
291 mutex_lock(&db->addr_lock);
292
293 spin_lock_irqsave(&db->lock, flags);
294
295 /* Save previous register address */
296 reg_save = readb(db->io_addr);
297
298 /* Fill the phyxcer register into REG_0C */
299 iow(db, DM9000_EPAR, DM9000_PHY | reg);
300
301 /* Issue phyxcer read command */
302 iow(db, DM9000_EPCR, EPCR_ERPRR | EPCR_EPOS);
303
304 writeb(reg_save, db->io_addr);
305 spin_unlock_irqrestore(&db->lock, flags);
306
307 dm9000_msleep(db, 1); /* Wait read complete */
308
309 spin_lock_irqsave(&db->lock, flags);
310 reg_save = readb(db->io_addr);
311
312 iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer read command */
313
314 /* The read data keeps on REG_0D & REG_0E */
315 ret = (ior(db, DM9000_EPDRH) << 8) | ior(db, DM9000_EPDRL);
316
317 /* restore the previous address */
318 writeb(reg_save, db->io_addr);
319 spin_unlock_irqrestore(&db->lock, flags);
320
321 mutex_unlock(&db->addr_lock);
322
323 dm9000_dbg(db, 5, "phy_read[%02x] -> %04x\n", reg, ret);
324 return ret;
325}
326
327/* Write a word to phyxcer */
328static void
329dm9000_phy_write(struct net_device *dev,
330 int phyaddr_unused, int reg, int value)
331{
332 board_info_t *db = netdev_priv(dev);
333 unsigned long flags;
334 unsigned long reg_save;
335
336 dm9000_dbg(db, 5, "phy_write[%02x] = %04x\n", reg, value);
337 mutex_lock(&db->addr_lock);
338
339 spin_lock_irqsave(&db->lock, flags);
340
341 /* Save previous register address */
342 reg_save = readb(db->io_addr);
343
344 /* Fill the phyxcer register into REG_0C */
345 iow(db, DM9000_EPAR, DM9000_PHY | reg);
346
347 /* Fill the written data into REG_0D & REG_0E */
348 iow(db, DM9000_EPDRL, value);
349 iow(db, DM9000_EPDRH, value >> 8);
350
351 /* Issue phyxcer write command */
352 iow(db, DM9000_EPCR, EPCR_EPOS | EPCR_ERPRW);
353
354 writeb(reg_save, db->io_addr);
355 spin_unlock_irqrestore(&db->lock, flags);
356
357 dm9000_msleep(db, 1); /* Wait write complete */
358
359 spin_lock_irqsave(&db->lock, flags);
360 reg_save = readb(db->io_addr);
361
362 iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer write command */
363
364 /* restore the previous address */
365 writeb(reg_save, db->io_addr);
366
367 spin_unlock_irqrestore(&db->lock, flags);
368 mutex_unlock(&db->addr_lock);
369}
370
Sascha Hauera1365272005-05-05 15:14:15 -0700371/* dm9000_set_io
372 *
373 * select the specified set of io routines to use with the
374 * device
375 */
376
377static void dm9000_set_io(struct board_info *db, int byte_width)
378{
379 /* use the size of the data resource to work out what IO
380 * routines we want to use
381 */
382
383 switch (byte_width) {
384 case 1:
385 db->dumpblk = dm9000_dumpblk_8bit;
386 db->outblk = dm9000_outblk_8bit;
387 db->inblk = dm9000_inblk_8bit;
388 break;
389
Sascha Hauera1365272005-05-05 15:14:15 -0700390
391 case 3:
Ben Dooksa76836f2008-02-05 00:02:02 +0000392 dev_dbg(db->dev, ": 3 byte IO, falling back to 16bit\n");
393 case 2:
Sascha Hauera1365272005-05-05 15:14:15 -0700394 db->dumpblk = dm9000_dumpblk_16bit;
395 db->outblk = dm9000_outblk_16bit;
396 db->inblk = dm9000_inblk_16bit;
397 break;
398
399 case 4:
400 default:
401 db->dumpblk = dm9000_dumpblk_32bit;
402 db->outblk = dm9000_outblk_32bit;
403 db->inblk = dm9000_inblk_32bit;
404 break;
405 }
406}
407
Ben Dooks8f5bf5f22008-05-08 11:36:42 +0100408static void dm9000_schedule_poll(board_info_t *db)
409{
Ben Dooks6d406b32008-06-24 22:15:59 +0100410 if (db->type == TYPE_DM9000E)
411 schedule_delayed_work(&db->phy_poll, HZ * 2);
Ben Dooks8f5bf5f22008-05-08 11:36:42 +0100412}
Sascha Hauera1365272005-05-05 15:14:15 -0700413
Ben Dooksf42d8ae2008-02-05 00:02:21 +0000414static int dm9000_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
415{
416 board_info_t *dm = to_dm9000_board(dev);
417
418 if (!netif_running(dev))
419 return -EINVAL;
420
421 return generic_mii_ioctl(&dm->mii, if_mii(req), cmd, NULL);
422}
423
Ben Dooksf8d79e72008-06-24 22:16:02 +0100424static unsigned int
425dm9000_read_locked(board_info_t *db, int reg)
426{
427 unsigned long flags;
428 unsigned int ret;
429
430 spin_lock_irqsave(&db->lock, flags);
431 ret = ior(db, reg);
432 spin_unlock_irqrestore(&db->lock, flags);
433
434 return ret;
435}
436
437static int dm9000_wait_eeprom(board_info_t *db)
438{
439 unsigned int status;
440 int timeout = 8; /* wait max 8msec */
441
442 /* The DM9000 data sheets say we should be able to
443 * poll the ERRE bit in EPCR to wait for the EEPROM
444 * operation. From testing several chips, this bit
445 * does not seem to work.
446 *
447 * We attempt to use the bit, but fall back to the
448 * timeout (which is why we do not return an error
449 * on expiry) to say that the EEPROM operation has
450 * completed.
451 */
452
453 while (1) {
454 status = dm9000_read_locked(db, DM9000_EPCR);
455
456 if ((status & EPCR_ERRE) == 0)
457 break;
458
Ben Dooks2fcf06c2008-06-24 22:16:05 +0100459 msleep(1);
460
Ben Dooksf8d79e72008-06-24 22:16:02 +0100461 if (timeout-- < 0) {
462 dev_dbg(db->dev, "timeout waiting EEPROM\n");
463 break;
464 }
465 }
466
467 return 0;
468}
469
470/*
471 * Read a word data from EEPROM
472 */
473static void
474dm9000_read_eeprom(board_info_t *db, int offset, u8 *to)
475{
476 unsigned long flags;
477
478 if (db->flags & DM9000_PLATF_NO_EEPROM) {
479 to[0] = 0xff;
480 to[1] = 0xff;
481 return;
482 }
483
484 mutex_lock(&db->addr_lock);
485
486 spin_lock_irqsave(&db->lock, flags);
487
488 iow(db, DM9000_EPAR, offset);
489 iow(db, DM9000_EPCR, EPCR_ERPRR);
490
491 spin_unlock_irqrestore(&db->lock, flags);
492
493 dm9000_wait_eeprom(db);
494
495 /* delay for at-least 150uS */
496 msleep(1);
497
498 spin_lock_irqsave(&db->lock, flags);
499
500 iow(db, DM9000_EPCR, 0x0);
501
502 to[0] = ior(db, DM9000_EPDRL);
503 to[1] = ior(db, DM9000_EPDRH);
504
505 spin_unlock_irqrestore(&db->lock, flags);
506
507 mutex_unlock(&db->addr_lock);
508}
509
510/*
511 * Write a word data to SROM
512 */
513static void
514dm9000_write_eeprom(board_info_t *db, int offset, u8 *data)
515{
516 unsigned long flags;
517
518 if (db->flags & DM9000_PLATF_NO_EEPROM)
519 return;
520
521 mutex_lock(&db->addr_lock);
522
523 spin_lock_irqsave(&db->lock, flags);
524 iow(db, DM9000_EPAR, offset);
525 iow(db, DM9000_EPDRH, data[1]);
526 iow(db, DM9000_EPDRL, data[0]);
527 iow(db, DM9000_EPCR, EPCR_WEP | EPCR_ERPRW);
528 spin_unlock_irqrestore(&db->lock, flags);
529
530 dm9000_wait_eeprom(db);
531
532 mdelay(1); /* wait at least 150uS to clear */
533
534 spin_lock_irqsave(&db->lock, flags);
535 iow(db, DM9000_EPCR, 0);
536 spin_unlock_irqrestore(&db->lock, flags);
537
538 mutex_unlock(&db->addr_lock);
539}
540
Ben Dooks7da99852008-02-05 00:02:06 +0000541/* ethtool ops */
542
543static void dm9000_get_drvinfo(struct net_device *dev,
544 struct ethtool_drvinfo *info)
545{
546 board_info_t *dm = to_dm9000_board(dev);
547
Jiri Pirko7826d432013-01-06 00:44:26 +0000548 strlcpy(info->driver, CARDNAME, sizeof(info->driver));
549 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
550 strlcpy(info->bus_info, to_platform_device(dm->dev)->name,
551 sizeof(info->bus_info));
Ben Dooks7da99852008-02-05 00:02:06 +0000552}
553
Ben Dookse662ee02008-02-05 00:02:12 +0000554static u32 dm9000_get_msglevel(struct net_device *dev)
555{
556 board_info_t *dm = to_dm9000_board(dev);
557
558 return dm->msg_enable;
559}
560
561static void dm9000_set_msglevel(struct net_device *dev, u32 value)
562{
563 board_info_t *dm = to_dm9000_board(dev);
564
565 dm->msg_enable = value;
566}
567
Ben Dooks7da99852008-02-05 00:02:06 +0000568static int dm9000_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
569{
570 board_info_t *dm = to_dm9000_board(dev);
Ben Dooks7da99852008-02-05 00:02:06 +0000571
Ben Dooks7da99852008-02-05 00:02:06 +0000572 mii_ethtool_gset(&dm->mii, cmd);
Ben Dooks7da99852008-02-05 00:02:06 +0000573 return 0;
574}
575
576static int dm9000_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
577{
578 board_info_t *dm = to_dm9000_board(dev);
Ben Dooks7da99852008-02-05 00:02:06 +0000579
Ben Dooks9a2f0372008-02-05 00:02:10 +0000580 return mii_ethtool_sset(&dm->mii, cmd);
Ben Dooks7da99852008-02-05 00:02:06 +0000581}
582
583static int dm9000_nway_reset(struct net_device *dev)
584{
585 board_info_t *dm = to_dm9000_board(dev);
586 return mii_nway_restart(&dm->mii);
587}
588
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000589static int dm9000_set_features(struct net_device *dev,
590 netdev_features_t features)
Yeasah Pell5dcc60b2009-07-06 18:12:33 -0700591{
592 board_info_t *dm = to_dm9000_board(dev);
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000593 netdev_features_t changed = dev->features ^ features;
Baruch Siach380fefb2010-05-17 17:45:48 -0700594 unsigned long flags;
Michał Mirosławc88fcb32011-04-15 04:50:49 +0000595
596 if (!(changed & NETIF_F_RXCSUM))
597 return 0;
Baruch Siach380fefb2010-05-17 17:45:48 -0700598
599 spin_lock_irqsave(&dm->lock, flags);
Michał Mirosławc88fcb32011-04-15 04:50:49 +0000600 iow(dm, DM9000_RCSR, (features & NETIF_F_RXCSUM) ? RCSR_CSUM : 0);
Baruch Siach380fefb2010-05-17 17:45:48 -0700601 spin_unlock_irqrestore(&dm->lock, flags);
602
Michał Mirosławc88fcb32011-04-15 04:50:49 +0000603 return 0;
Yeasah Pell5dcc60b2009-07-06 18:12:33 -0700604}
605
Ben Dooks7da99852008-02-05 00:02:06 +0000606static u32 dm9000_get_link(struct net_device *dev)
607{
608 board_info_t *dm = to_dm9000_board(dev);
Ben Dooksaa1eb452008-06-24 22:16:03 +0100609 u32 ret;
610
611 if (dm->flags & DM9000_PLATF_EXT_PHY)
612 ret = mii_link_ok(&dm->mii);
613 else
614 ret = dm9000_read_locked(dm, DM9000_NSR) & NSR_LINKST ? 1 : 0;
615
616 return ret;
Ben Dooks7da99852008-02-05 00:02:06 +0000617}
618
Ben Dooks29d52e52008-02-05 00:02:11 +0000619#define DM_EEPROM_MAGIC (0x444D394B)
620
621static int dm9000_get_eeprom_len(struct net_device *dev)
622{
623 return 128;
624}
625
626static int dm9000_get_eeprom(struct net_device *dev,
627 struct ethtool_eeprom *ee, u8 *data)
628{
629 board_info_t *dm = to_dm9000_board(dev);
630 int offset = ee->offset;
631 int len = ee->len;
632 int i;
633
634 /* EEPROM access is aligned to two bytes */
635
636 if ((len & 1) != 0 || (offset & 1) != 0)
637 return -EINVAL;
638
Ben Dooksbb44fb702008-02-05 00:02:20 +0000639 if (dm->flags & DM9000_PLATF_NO_EEPROM)
640 return -ENOENT;
641
Ben Dooks29d52e52008-02-05 00:02:11 +0000642 ee->magic = DM_EEPROM_MAGIC;
643
644 for (i = 0; i < len; i += 2)
645 dm9000_read_eeprom(dm, (offset + i) / 2, data + i);
646
647 return 0;
648}
649
650static int dm9000_set_eeprom(struct net_device *dev,
651 struct ethtool_eeprom *ee, u8 *data)
652{
653 board_info_t *dm = to_dm9000_board(dev);
654 int offset = ee->offset;
655 int len = ee->len;
Ben Dooks40d15cd2011-06-10 00:50:32 +0000656 int done;
Ben Dooks29d52e52008-02-05 00:02:11 +0000657
658 /* EEPROM access is aligned to two bytes */
659
Ben Dooksbb44fb702008-02-05 00:02:20 +0000660 if (dm->flags & DM9000_PLATF_NO_EEPROM)
661 return -ENOENT;
662
Ben Dooks29d52e52008-02-05 00:02:11 +0000663 if (ee->magic != DM_EEPROM_MAGIC)
664 return -EINVAL;
665
Ben Dooks40d15cd2011-06-10 00:50:32 +0000666 while (len > 0) {
667 if (len & 1 || offset & 1) {
668 int which = offset & 1;
669 u8 tmp[2];
670
671 dm9000_read_eeprom(dm, offset / 2, tmp);
672 tmp[which] = *data;
673 dm9000_write_eeprom(dm, offset / 2, tmp);
674
675 done = 1;
676 } else {
677 dm9000_write_eeprom(dm, offset / 2, data);
678 done = 2;
679 }
680
681 data += done;
682 offset += done;
683 len -= done;
684 }
Ben Dooks29d52e52008-02-05 00:02:11 +0000685
686 return 0;
687}
688
Ben Dooksc029f442009-11-10 07:22:24 +0000689static void dm9000_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
690{
691 board_info_t *dm = to_dm9000_board(dev);
692
693 memset(w, 0, sizeof(struct ethtool_wolinfo));
694
695 /* note, we could probably support wake-phy too */
696 w->supported = dm->wake_supported ? WAKE_MAGIC : 0;
697 w->wolopts = dm->wake_state;
698}
699
700static int dm9000_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
701{
702 board_info_t *dm = to_dm9000_board(dev);
703 unsigned long flags;
704 u32 opts = w->wolopts;
705 u32 wcr = 0;
706
707 if (!dm->wake_supported)
708 return -EOPNOTSUPP;
709
710 if (opts & ~WAKE_MAGIC)
711 return -EINVAL;
712
713 if (opts & WAKE_MAGIC)
714 wcr |= WCR_MAGICEN;
715
716 mutex_lock(&dm->addr_lock);
717
718 spin_lock_irqsave(&dm->lock, flags);
719 iow(dm, DM9000_WCR, wcr);
720 spin_unlock_irqrestore(&dm->lock, flags);
721
722 mutex_unlock(&dm->addr_lock);
723
724 if (dm->wake_state != opts) {
725 /* change in wol state, update IRQ state */
726
727 if (!dm->wake_state)
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200728 irq_set_irq_wake(dm->irq_wake, 1);
Mark Brown83b98fb2011-11-21 07:51:56 +0000729 else if (dm->wake_state && !opts)
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200730 irq_set_irq_wake(dm->irq_wake, 0);
Ben Dooksc029f442009-11-10 07:22:24 +0000731 }
732
733 dm->wake_state = opts;
734 return 0;
735}
736
Ben Dooks7da99852008-02-05 00:02:06 +0000737static const struct ethtool_ops dm9000_ethtool_ops = {
738 .get_drvinfo = dm9000_get_drvinfo,
739 .get_settings = dm9000_get_settings,
740 .set_settings = dm9000_set_settings,
Ben Dookse662ee02008-02-05 00:02:12 +0000741 .get_msglevel = dm9000_get_msglevel,
742 .set_msglevel = dm9000_set_msglevel,
Ben Dooks7da99852008-02-05 00:02:06 +0000743 .nway_reset = dm9000_nway_reset,
744 .get_link = dm9000_get_link,
Ben Dooksc029f442009-11-10 07:22:24 +0000745 .get_wol = dm9000_get_wol,
746 .set_wol = dm9000_set_wol,
Barry Song5b227212014-01-15 23:31:46 +0800747 .get_eeprom_len = dm9000_get_eeprom_len,
748 .get_eeprom = dm9000_get_eeprom,
749 .set_eeprom = dm9000_set_eeprom,
Ben Dooks7da99852008-02-05 00:02:06 +0000750};
751
Ben Dooksf8dd0ec2008-06-24 22:16:04 +0100752static void dm9000_show_carrier(board_info_t *db,
753 unsigned carrier, unsigned nsr)
754{
Nikita Kiryanov727a2822013-10-16 11:41:34 +0300755 int lpa;
Ben Dooksf8dd0ec2008-06-24 22:16:04 +0100756 struct net_device *ndev = db->ndev;
Nikita Kiryanov727a2822013-10-16 11:41:34 +0300757 struct mii_if_info *mii = &db->mii;
Ben Dooksf8dd0ec2008-06-24 22:16:04 +0100758 unsigned ncr = dm9000_read_locked(db, DM9000_NCR);
759
Nikita Kiryanov727a2822013-10-16 11:41:34 +0300760 if (carrier) {
761 lpa = mii->mdio_read(mii->dev, mii->phy_id, MII_LPA);
762 dev_info(db->dev,
763 "%s: link up, %dMbps, %s-duplex, lpa 0x%04X\n",
Ben Dooksf8dd0ec2008-06-24 22:16:04 +0100764 ndev->name, (nsr & NSR_SPEED) ? 10 : 100,
Nikita Kiryanov727a2822013-10-16 11:41:34 +0300765 (ncr & NCR_FDX) ? "full" : "half", lpa);
766 } else {
Ben Dooksf8dd0ec2008-06-24 22:16:04 +0100767 dev_info(db->dev, "%s: link down\n", ndev->name);
Nikita Kiryanov727a2822013-10-16 11:41:34 +0300768 }
Ben Dooksf8dd0ec2008-06-24 22:16:04 +0100769}
770
Ben Dooks8f5bf5f22008-05-08 11:36:42 +0100771static void
772dm9000_poll_work(struct work_struct *w)
773{
Jean Delvarebf6aede2009-04-02 16:56:54 -0700774 struct delayed_work *dw = to_delayed_work(w);
Ben Dooks8f5bf5f22008-05-08 11:36:42 +0100775 board_info_t *db = container_of(dw, board_info_t, phy_poll);
Ben Dooksf8dd0ec2008-06-24 22:16:04 +0100776 struct net_device *ndev = db->ndev;
Ben Dooks8f5bf5f22008-05-08 11:36:42 +0100777
Ben Dooksf8dd0ec2008-06-24 22:16:04 +0100778 if (db->flags & DM9000_PLATF_SIMPLE_PHY &&
779 !(db->flags & DM9000_PLATF_EXT_PHY)) {
780 unsigned nsr = dm9000_read_locked(db, DM9000_NSR);
781 unsigned old_carrier = netif_carrier_ok(ndev) ? 1 : 0;
782 unsigned new_carrier;
783
784 new_carrier = (nsr & NSR_LINKST) ? 1 : 0;
785
786 if (old_carrier != new_carrier) {
787 if (netif_msg_link(db))
788 dm9000_show_carrier(db, new_carrier, nsr);
789
790 if (!new_carrier)
791 netif_carrier_off(ndev);
792 else
793 netif_carrier_on(ndev);
794 }
795 } else
796 mii_check_media(&db->mii, netif_msg_link(db), 0);
Barry Song5b227212014-01-15 23:31:46 +0800797
Ben Dooksf8dd0ec2008-06-24 22:16:04 +0100798 if (netif_running(ndev))
Ben Dooks8f5bf5f22008-05-08 11:36:42 +0100799 dm9000_schedule_poll(db);
800}
Ben Dooks7da99852008-02-05 00:02:06 +0000801
Sascha Hauera1365272005-05-05 15:14:15 -0700802/* dm9000_release_board
803 *
804 * release a board, and any mapped resources
805 */
806
807static void
808dm9000_release_board(struct platform_device *pdev, struct board_info *db)
809{
Sascha Hauera1365272005-05-05 15:14:15 -0700810 /* unmap our resources */
811
812 iounmap(db->io_addr);
813 iounmap(db->io_data);
814
815 /* release the resources */
816
Ben Dooks9088fa4f2008-06-24 22:16:00 +0100817 release_resource(db->data_req);
818 kfree(db->data_req);
Sascha Hauera1365272005-05-05 15:14:15 -0700819
Ben Dooks9088fa4f2008-06-24 22:16:00 +0100820 release_resource(db->addr_req);
821 kfree(db->addr_req);
Sascha Hauera1365272005-05-05 15:14:15 -0700822}
823
Ben Dooks6d406b32008-06-24 22:15:59 +0100824static unsigned char dm9000_type_to_char(enum dm9000_type type)
825{
826 switch (type) {
827 case TYPE_DM9000E: return 'e';
828 case TYPE_DM9000A: return 'a';
829 case TYPE_DM9000B: return 'b';
830 }
831
832 return '?';
833}
834
Ben Dooksf8d79e72008-06-24 22:16:02 +0100835/*
836 * Set DM9000 multicast address
837 */
838static void
Baruch Siach380fefb2010-05-17 17:45:48 -0700839dm9000_hash_table_unlocked(struct net_device *dev)
Ben Dooksf8d79e72008-06-24 22:16:02 +0100840{
Wang Chen4cf16532008-11-12 23:38:14 -0800841 board_info_t *db = netdev_priv(dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +0000842 struct netdev_hw_addr *ha;
Ben Dooksf8d79e72008-06-24 22:16:02 +0100843 int i, oft;
844 u32 hash_val;
Emilio López35e729a2013-05-17 10:42:55 +0000845 u16 hash_table[4] = { 0, 0, 0, 0x8000 }; /* broadcast address */
Ben Dooksf8d79e72008-06-24 22:16:02 +0100846 u8 rcr = RCR_DIS_LONG | RCR_DIS_CRC | RCR_RXEN;
Ben Dooksf8d79e72008-06-24 22:16:02 +0100847
848 dm9000_dbg(db, 1, "entering %s\n", __func__);
849
Ben Dooksf8d79e72008-06-24 22:16:02 +0100850 for (i = 0, oft = DM9000_PAR; i < 6; i++, oft++)
851 iow(db, oft, dev->dev_addr[i]);
852
Ben Dooksf8d79e72008-06-24 22:16:02 +0100853 if (dev->flags & IFF_PROMISC)
854 rcr |= RCR_PRMSC;
855
856 if (dev->flags & IFF_ALLMULTI)
857 rcr |= RCR_ALL;
858
859 /* the multicast address in Hash Table : 64 bits */
Jiri Pirko22bedad32010-04-01 21:22:57 +0000860 netdev_for_each_mc_addr(ha, dev) {
861 hash_val = ether_crc_le(6, ha->addr) & 0x3f;
Ben Dooksf8d79e72008-06-24 22:16:02 +0100862 hash_table[hash_val / 16] |= (u16) 1 << (hash_val % 16);
863 }
864
865 /* Write the hash table to MAC MD table */
866 for (i = 0, oft = DM9000_MAR; i < 4; i++) {
867 iow(db, oft++, hash_table[i]);
868 iow(db, oft++, hash_table[i] >> 8);
869 }
870
871 iow(db, DM9000_RCR, rcr);
Baruch Siach380fefb2010-05-17 17:45:48 -0700872}
873
874static void
875dm9000_hash_table(struct net_device *dev)
876{
877 board_info_t *db = netdev_priv(dev);
878 unsigned long flags;
879
880 spin_lock_irqsave(&db->lock, flags);
881 dm9000_hash_table_unlocked(dev);
Ben Dooksf8d79e72008-06-24 22:16:02 +0100882 spin_unlock_irqrestore(&db->lock, flags);
883}
884
885/*
Eric Dumazet1ae5dc32010-05-10 05:01:31 -0700886 * Initialize dm9000 board
Ben Dooksf8d79e72008-06-24 22:16:02 +0100887 */
888static void
889dm9000_init_dm9000(struct net_device *dev)
890{
Wang Chen4cf16532008-11-12 23:38:14 -0800891 board_info_t *db = netdev_priv(dev);
Ben Dooksf8d79e72008-06-24 22:16:02 +0100892 unsigned int imr;
Ben Dooksc029f442009-11-10 07:22:24 +0000893 unsigned int ncr;
Ben Dooksf8d79e72008-06-24 22:16:02 +0100894
895 dm9000_dbg(db, 1, "entering %s\n", __func__);
896
897 /* I/O mode */
898 db->io_mode = ior(db, DM9000_ISR) >> 6; /* ISR bit7:6 keeps I/O mode */
899
Yeasah Pell5dcc60b2009-07-06 18:12:33 -0700900 /* Checksum mode */
Michał Mirosławc88fcb32011-04-15 04:50:49 +0000901 if (dev->hw_features & NETIF_F_RXCSUM)
Mark Brown56d37f12011-04-18 01:04:37 +0000902 iow(db, DM9000_RCSR,
Michał Mirosławc88fcb32011-04-15 04:50:49 +0000903 (dev->features & NETIF_F_RXCSUM) ? RCSR_CSUM : 0);
Yeasah Pell5dcc60b2009-07-06 18:12:33 -0700904
Ben Dooksf8d79e72008-06-24 22:16:02 +0100905 iow(db, DM9000_GPCR, GPCR_GEP_CNTL); /* Let GPIO0 output */
Nikita Kiryanov677d7d22013-10-16 11:41:32 +0300906 iow(db, DM9000_GPR, 0);
Ben Dooksf8d79e72008-06-24 22:16:02 +0100907
Nikita Kiryanov6649b202013-10-16 11:41:31 +0300908 /* If we are dealing with DM9000B, some extra steps are required: a
909 * manual phy reset, and setting init params.
910 */
911 if (db->type == TYPE_DM9000B) {
912 dm9000_phy_write(dev, 0, MII_BMCR, BMCR_RESET);
913 dm9000_phy_write(dev, 0, MII_DM_DSPCR, DSPCR_INIT_PARAM);
914 }
Joseph CHANG6741f40d2013-03-28 23:13:42 +0000915
Ben Dooksc029f442009-11-10 07:22:24 +0000916 ncr = (db->flags & DM9000_PLATF_EXT_PHY) ? NCR_EXT_PHY : 0;
917
918 /* if wol is needed, then always set NCR_WAKEEN otherwise we end
919 * up dumping the wake events if we disable this. There is already
920 * a wake-mask in DM9000_WCR */
921 if (db->wake_supported)
922 ncr |= NCR_WAKEEN;
923
924 iow(db, DM9000_NCR, ncr);
Ben Dooksf8d79e72008-06-24 22:16:02 +0100925
926 /* Program operating register */
927 iow(db, DM9000_TCR, 0); /* TX Polling clear */
928 iow(db, DM9000_BPTR, 0x3f); /* Less 3Kb, 200us */
929 iow(db, DM9000_FCR, 0xff); /* Flow Control */
930 iow(db, DM9000_SMCR, 0); /* Special Mode */
931 /* clear TX status */
932 iow(db, DM9000_NSR, NSR_WAKEST | NSR_TX2END | NSR_TX1END);
933 iow(db, DM9000_ISR, ISR_CLR_STATUS); /* Clear interrupt status */
934
935 /* Set address filter table */
Baruch Siach380fefb2010-05-17 17:45:48 -0700936 dm9000_hash_table_unlocked(dev);
Ben Dooksf8d79e72008-06-24 22:16:02 +0100937
938 imr = IMR_PAR | IMR_PTM | IMR_PRM;
939 if (db->type != TYPE_DM9000E)
940 imr |= IMR_LNKCHNG;
941
942 db->imr_all = imr;
943
944 /* Enable TX/RX interrupt mask */
945 iow(db, DM9000_IMR, imr);
946
947 /* Init Driver variable */
948 db->tx_pkt_cnt = 0;
949 db->queue_pkt_len = 0;
Eric Dumazet1ae5dc32010-05-10 05:01:31 -0700950 dev->trans_start = jiffies;
Ben Dooksf8d79e72008-06-24 22:16:02 +0100951}
952
953/* Our watchdog timed out. Called by the networking layer */
954static void dm9000_timeout(struct net_device *dev)
955{
Wang Chen4cf16532008-11-12 23:38:14 -0800956 board_info_t *db = netdev_priv(dev);
Ben Dooksf8d79e72008-06-24 22:16:02 +0100957 u8 reg_save;
958 unsigned long flags;
959
960 /* Save previous register address */
Ben Dooksf8d79e72008-06-24 22:16:02 +0100961 spin_lock_irqsave(&db->lock, flags);
Henry Nestler8dde9242011-02-20 11:44:58 +0000962 reg_save = readb(db->io_addr);
Ben Dooksf8d79e72008-06-24 22:16:02 +0100963
964 netif_stop_queue(dev);
965 dm9000_reset(db);
966 dm9000_init_dm9000(dev);
967 /* We can accept TX packets again */
Eric Dumazet1ae5dc32010-05-10 05:01:31 -0700968 dev->trans_start = jiffies; /* prevent tx timeout */
Ben Dooksf8d79e72008-06-24 22:16:02 +0100969 netif_wake_queue(dev);
970
971 /* Restore previous register address */
972 writeb(reg_save, db->io_addr);
973 spin_unlock_irqrestore(&db->lock, flags);
974}
975
Yeasah Pell5dcc60b2009-07-06 18:12:33 -0700976static void dm9000_send_packet(struct net_device *dev,
977 int ip_summed,
978 u16 pkt_len)
979{
980 board_info_t *dm = to_dm9000_board(dev);
981
982 /* The DM9000 is not smart enough to leave fragmented packets alone. */
983 if (dm->ip_summed != ip_summed) {
984 if (ip_summed == CHECKSUM_NONE)
985 iow(dm, DM9000_TCCR, 0);
986 else
987 iow(dm, DM9000_TCCR, TCCR_IP | TCCR_UDP | TCCR_TCP);
988 dm->ip_summed = ip_summed;
989 }
990
991 /* Set TX length to DM9000 */
992 iow(dm, DM9000_TXPLL, pkt_len);
993 iow(dm, DM9000_TXPLH, pkt_len >> 8);
994
995 /* Issue TX polling command */
996 iow(dm, DM9000_TCR, TCR_TXREQ); /* Cleared after TX complete */
997}
998
Ben Dooksf8d79e72008-06-24 22:16:02 +0100999/*
1000 * Hardware start transmission.
1001 * Send a packet to media from the upper layer.
1002 */
1003static int
1004dm9000_start_xmit(struct sk_buff *skb, struct net_device *dev)
1005{
1006 unsigned long flags;
Wang Chen4cf16532008-11-12 23:38:14 -08001007 board_info_t *db = netdev_priv(dev);
Ben Dooksf8d79e72008-06-24 22:16:02 +01001008
1009 dm9000_dbg(db, 3, "%s:\n", __func__);
1010
1011 if (db->tx_pkt_cnt > 1)
Patrick McHardy5b548142009-06-12 06:22:29 +00001012 return NETDEV_TX_BUSY;
Ben Dooksf8d79e72008-06-24 22:16:02 +01001013
1014 spin_lock_irqsave(&db->lock, flags);
1015
1016 /* Move data to DM9000 TX RAM */
1017 writeb(DM9000_MWCMD, db->io_addr);
1018
1019 (db->outblk)(db->io_data, skb->data, skb->len);
1020 dev->stats.tx_bytes += skb->len;
1021
1022 db->tx_pkt_cnt++;
1023 /* TX control: First packet immediately send, second packet queue */
1024 if (db->tx_pkt_cnt == 1) {
Yeasah Pell5dcc60b2009-07-06 18:12:33 -07001025 dm9000_send_packet(dev, skb->ip_summed, skb->len);
Ben Dooksf8d79e72008-06-24 22:16:02 +01001026 } else {
1027 /* Second packet */
1028 db->queue_pkt_len = skb->len;
Yeasah Pell5dcc60b2009-07-06 18:12:33 -07001029 db->queue_ip_summed = skb->ip_summed;
Ben Dooksf8d79e72008-06-24 22:16:02 +01001030 netif_stop_queue(dev);
1031 }
1032
1033 spin_unlock_irqrestore(&db->lock, flags);
1034
1035 /* free this SKB */
Eric W. Biederman2c3d0bc2014-03-15 16:53:09 -07001036 dev_consume_skb_any(skb);
Ben Dooksf8d79e72008-06-24 22:16:02 +01001037
Patrick McHardy6ed10652009-06-23 06:03:08 +00001038 return NETDEV_TX_OK;
Ben Dooksf8d79e72008-06-24 22:16:02 +01001039}
1040
1041/*
1042 * DM9000 interrupt handler
1043 * receive the packet to upper layer, free the transmitted packet
1044 */
1045
1046static void dm9000_tx_done(struct net_device *dev, board_info_t *db)
1047{
1048 int tx_status = ior(db, DM9000_NSR); /* Got TX status */
1049
1050 if (tx_status & (NSR_TX2END | NSR_TX1END)) {
1051 /* One packet sent complete */
1052 db->tx_pkt_cnt--;
1053 dev->stats.tx_packets++;
1054
1055 if (netif_msg_tx_done(db))
1056 dev_dbg(db->dev, "tx done, NSR %02x\n", tx_status);
1057
1058 /* Queue packet check & send */
Yeasah Pell5dcc60b2009-07-06 18:12:33 -07001059 if (db->tx_pkt_cnt > 0)
1060 dm9000_send_packet(dev, db->queue_ip_summed,
1061 db->queue_pkt_len);
Ben Dooksf8d79e72008-06-24 22:16:02 +01001062 netif_wake_queue(dev);
1063 }
1064}
1065
1066struct dm9000_rxhdr {
1067 u8 RxPktReady;
1068 u8 RxStatus;
1069 __le16 RxLen;
Eric Dumazetba2d3582010-06-02 18:10:09 +00001070} __packed;
Ben Dooksf8d79e72008-06-24 22:16:02 +01001071
1072/*
1073 * Received a packet and pass to upper layer
1074 */
1075static void
1076dm9000_rx(struct net_device *dev)
1077{
Wang Chen4cf16532008-11-12 23:38:14 -08001078 board_info_t *db = netdev_priv(dev);
Ben Dooksf8d79e72008-06-24 22:16:02 +01001079 struct dm9000_rxhdr rxhdr;
1080 struct sk_buff *skb;
1081 u8 rxbyte, *rdptr;
1082 bool GoodPacket;
1083 int RxLen;
1084
1085 /* Check packet ready or not */
1086 do {
1087 ior(db, DM9000_MRCMDX); /* Dummy read */
1088
1089 /* Get most updated data */
1090 rxbyte = readb(db->io_data);
1091
1092 /* Status check: this byte must be 0 or 1 */
Yeasah Pell5dcc60b2009-07-06 18:12:33 -07001093 if (rxbyte & DM9000_PKT_ERR) {
Ben Dooksf8d79e72008-06-24 22:16:02 +01001094 dev_warn(db->dev, "status check fail: %d\n", rxbyte);
1095 iow(db, DM9000_RCR, 0x00); /* Stop Device */
1096 iow(db, DM9000_ISR, IMR_PAR); /* Stop INT request */
1097 return;
1098 }
1099
Yeasah Pell5dcc60b2009-07-06 18:12:33 -07001100 if (!(rxbyte & DM9000_PKT_RDY))
Ben Dooksf8d79e72008-06-24 22:16:02 +01001101 return;
1102
1103 /* A packet ready now & Get status/length */
1104 GoodPacket = true;
1105 writeb(DM9000_MRCMD, db->io_addr);
1106
1107 (db->inblk)(db->io_data, &rxhdr, sizeof(rxhdr));
1108
1109 RxLen = le16_to_cpu(rxhdr.RxLen);
1110
1111 if (netif_msg_rx_status(db))
1112 dev_dbg(db->dev, "RX: status %02x, length %04x\n",
1113 rxhdr.RxStatus, RxLen);
1114
1115 /* Packet Status check */
1116 if (RxLen < 0x40) {
1117 GoodPacket = false;
1118 if (netif_msg_rx_err(db))
1119 dev_dbg(db->dev, "RX: Bad Packet (runt)\n");
1120 }
1121
1122 if (RxLen > DM9000_PKT_MAX) {
1123 dev_dbg(db->dev, "RST: RX Len:%x\n", RxLen);
1124 }
1125
Ben Dooksf8e5e772008-07-17 20:29:13 +01001126 /* rxhdr.RxStatus is identical to RSR register. */
1127 if (rxhdr.RxStatus & (RSR_FOE | RSR_CE | RSR_AE |
1128 RSR_PLE | RSR_RWTO |
1129 RSR_LCS | RSR_RF)) {
Ben Dooksf8d79e72008-06-24 22:16:02 +01001130 GoodPacket = false;
Ben Dooksf8e5e772008-07-17 20:29:13 +01001131 if (rxhdr.RxStatus & RSR_FOE) {
Ben Dooksf8d79e72008-06-24 22:16:02 +01001132 if (netif_msg_rx_err(db))
1133 dev_dbg(db->dev, "fifo error\n");
1134 dev->stats.rx_fifo_errors++;
1135 }
Ben Dooksf8e5e772008-07-17 20:29:13 +01001136 if (rxhdr.RxStatus & RSR_CE) {
Ben Dooksf8d79e72008-06-24 22:16:02 +01001137 if (netif_msg_rx_err(db))
1138 dev_dbg(db->dev, "crc error\n");
1139 dev->stats.rx_crc_errors++;
1140 }
Ben Dooksf8e5e772008-07-17 20:29:13 +01001141 if (rxhdr.RxStatus & RSR_RF) {
Ben Dooksf8d79e72008-06-24 22:16:02 +01001142 if (netif_msg_rx_err(db))
1143 dev_dbg(db->dev, "length error\n");
1144 dev->stats.rx_length_errors++;
1145 }
1146 }
1147
1148 /* Move data from DM9000 */
Joe Perches8e95a202009-12-03 07:58:21 +00001149 if (GoodPacket &&
Pradeep A Dalvi21a4e462012-02-05 02:50:10 +00001150 ((skb = netdev_alloc_skb(dev, RxLen + 4)) != NULL)) {
Ben Dooksf8d79e72008-06-24 22:16:02 +01001151 skb_reserve(skb, 2);
1152 rdptr = (u8 *) skb_put(skb, RxLen - 4);
1153
1154 /* Read received packet from RX SRAM */
1155
1156 (db->inblk)(db->io_data, rdptr, RxLen);
1157 dev->stats.rx_bytes += RxLen;
1158
1159 /* Pass to upper layer */
1160 skb->protocol = eth_type_trans(skb, dev);
Michał Mirosławc88fcb32011-04-15 04:50:49 +00001161 if (dev->features & NETIF_F_RXCSUM) {
Yeasah Pell5dcc60b2009-07-06 18:12:33 -07001162 if ((((rxbyte & 0x1c) << 3) & rxbyte) == 0)
1163 skb->ip_summed = CHECKSUM_UNNECESSARY;
1164 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001165 skb_checksum_none_assert(skb);
Yeasah Pell5dcc60b2009-07-06 18:12:33 -07001166 }
Ben Dooksf8d79e72008-06-24 22:16:02 +01001167 netif_rx(skb);
1168 dev->stats.rx_packets++;
1169
1170 } else {
1171 /* need to dump the packet's data */
1172
1173 (db->dumpblk)(db->io_data, RxLen);
1174 }
Yeasah Pell5dcc60b2009-07-06 18:12:33 -07001175 } while (rxbyte & DM9000_PKT_RDY);
Ben Dooksf8d79e72008-06-24 22:16:02 +01001176}
1177
1178static irqreturn_t dm9000_interrupt(int irq, void *dev_id)
1179{
1180 struct net_device *dev = dev_id;
Wang Chen4cf16532008-11-12 23:38:14 -08001181 board_info_t *db = netdev_priv(dev);
Ben Dooksf8d79e72008-06-24 22:16:02 +01001182 int int_status;
David Brownelle3162d32009-03-22 21:28:39 -07001183 unsigned long flags;
Ben Dooksf8d79e72008-06-24 22:16:02 +01001184 u8 reg_save;
1185
1186 dm9000_dbg(db, 3, "entering %s\n", __func__);
1187
1188 /* A real interrupt coming */
1189
David Brownelle3162d32009-03-22 21:28:39 -07001190 /* holders of db->lock must always block IRQs */
1191 spin_lock_irqsave(&db->lock, flags);
Ben Dooksf8d79e72008-06-24 22:16:02 +01001192
1193 /* Save previous register address */
1194 reg_save = readb(db->io_addr);
1195
1196 /* Disable all interrupts */
1197 iow(db, DM9000_IMR, IMR_PAR);
1198
1199 /* Got DM9000 interrupt status */
1200 int_status = ior(db, DM9000_ISR); /* Got ISR */
1201 iow(db, DM9000_ISR, int_status); /* Clear ISR status */
1202
1203 if (netif_msg_intr(db))
1204 dev_dbg(db->dev, "interrupt status %02x\n", int_status);
1205
1206 /* Received the coming packet */
1207 if (int_status & ISR_PRS)
1208 dm9000_rx(dev);
1209
1210 /* Trnasmit Interrupt check */
1211 if (int_status & ISR_PTS)
1212 dm9000_tx_done(dev, db);
1213
1214 if (db->type != TYPE_DM9000E) {
1215 if (int_status & ISR_LNKCHNG) {
1216 /* fire a link-change request */
1217 schedule_delayed_work(&db->phy_poll, 1);
1218 }
1219 }
1220
1221 /* Re-enable interrupt mask */
1222 iow(db, DM9000_IMR, db->imr_all);
1223
1224 /* Restore previous register address */
1225 writeb(reg_save, db->io_addr);
1226
David Brownelle3162d32009-03-22 21:28:39 -07001227 spin_unlock_irqrestore(&db->lock, flags);
Ben Dooksf8d79e72008-06-24 22:16:02 +01001228
1229 return IRQ_HANDLED;
1230}
1231
Ben Dooksc029f442009-11-10 07:22:24 +00001232static irqreturn_t dm9000_wol_interrupt(int irq, void *dev_id)
1233{
1234 struct net_device *dev = dev_id;
1235 board_info_t *db = netdev_priv(dev);
1236 unsigned long flags;
1237 unsigned nsr, wcr;
1238
1239 spin_lock_irqsave(&db->lock, flags);
1240
1241 nsr = ior(db, DM9000_NSR);
1242 wcr = ior(db, DM9000_WCR);
1243
1244 dev_dbg(db->dev, "%s: NSR=0x%02x, WCR=0x%02x\n", __func__, nsr, wcr);
1245
1246 if (nsr & NSR_WAKEST) {
1247 /* clear, so we can avoid */
1248 iow(db, DM9000_NSR, NSR_WAKEST);
1249
1250 if (wcr & WCR_LINKST)
1251 dev_info(db->dev, "wake by link status change\n");
1252 if (wcr & WCR_SAMPLEST)
1253 dev_info(db->dev, "wake by sample packet\n");
Barry Song5b227212014-01-15 23:31:46 +08001254 if (wcr & WCR_MAGICST)
Ben Dooksc029f442009-11-10 07:22:24 +00001255 dev_info(db->dev, "wake by magic packet\n");
1256 if (!(wcr & (WCR_LINKST | WCR_SAMPLEST | WCR_MAGICST)))
1257 dev_err(db->dev, "wake signalled with no reason? "
1258 "NSR=0x%02x, WSR=0x%02x\n", nsr, wcr);
Ben Dooksc029f442009-11-10 07:22:24 +00001259 }
1260
1261 spin_unlock_irqrestore(&db->lock, flags);
1262
1263 return (nsr & NSR_WAKEST) ? IRQ_HANDLED : IRQ_NONE;
1264}
1265
Ben Dooksf8d79e72008-06-24 22:16:02 +01001266#ifdef CONFIG_NET_POLL_CONTROLLER
1267/*
1268 *Used by netconsole
1269 */
1270static void dm9000_poll_controller(struct net_device *dev)
1271{
1272 disable_irq(dev->irq);
1273 dm9000_interrupt(dev->irq, dev);
1274 enable_irq(dev->irq);
1275}
1276#endif
1277
1278/*
1279 * Open the interface.
1280 * The interface is opened whenever "ifconfig" actives it.
1281 */
1282static int
1283dm9000_open(struct net_device *dev)
1284{
Wang Chen4cf16532008-11-12 23:38:14 -08001285 board_info_t *db = netdev_priv(dev);
Ben Dooksf8d79e72008-06-24 22:16:02 +01001286 unsigned long irqflags = db->irq_res->flags & IRQF_TRIGGER_MASK;
1287
1288 if (netif_msg_ifup(db))
1289 dev_dbg(db->dev, "enabling %s\n", dev->name);
1290
1291 /* If there is no IRQ type specified, default to something that
1292 * may work, and tell the user that this is a problem */
1293
Ben Dooks6ff4ff02008-06-24 22:16:07 +01001294 if (irqflags == IRQF_TRIGGER_NONE)
Andrew Ruder6b50d032014-06-04 17:28:44 -05001295 irqflags = irq_get_trigger_type(dev->irq);
1296
1297 if (irqflags == IRQF_TRIGGER_NONE)
Ben Dooksf8d79e72008-06-24 22:16:02 +01001298 dev_warn(db->dev, "WARNING: no IRQ resource flags set.\n");
Ben Dooks6ff4ff02008-06-24 22:16:07 +01001299
Ben Dooksf8d79e72008-06-24 22:16:02 +01001300 irqflags |= IRQF_SHARED;
1301
Henry Nestler108f518c2011-02-22 11:29:42 +00001302 /* GPIO0 on pre-activate PHY, Reg 1F is not set by reset */
1303 iow(db, DM9000_GPR, 0); /* REG_1F bit0 activate phyxcer */
1304 mdelay(1); /* delay needs by DM9000B */
1305
Ben Dooksf8d79e72008-06-24 22:16:02 +01001306 /* Initialize DM9000 board */
1307 dm9000_reset(db);
1308 dm9000_init_dm9000(dev);
1309
Mark Brown6979d5d2011-06-01 10:18:09 +00001310 if (request_irq(dev->irq, dm9000_interrupt, irqflags, dev->name, dev))
1311 return -EAGAIN;
1312
Ben Dooksf8d79e72008-06-24 22:16:02 +01001313 /* Init driver variable */
1314 db->dbug_cnt = 0;
1315
1316 mii_check_media(&db->mii, netif_msg_link(db), 1);
1317 netif_start_queue(dev);
Barry Song5b227212014-01-15 23:31:46 +08001318
Ben Dooksf8d79e72008-06-24 22:16:02 +01001319 dm9000_schedule_poll(db);
1320
1321 return 0;
1322}
1323
Ben Dooksf8d79e72008-06-24 22:16:02 +01001324static void
1325dm9000_shutdown(struct net_device *dev)
1326{
Wang Chen4cf16532008-11-12 23:38:14 -08001327 board_info_t *db = netdev_priv(dev);
Ben Dooksf8d79e72008-06-24 22:16:02 +01001328
1329 /* RESET device */
1330 dm9000_phy_write(dev, 0, MII_BMCR, BMCR_RESET); /* PHY RESET */
1331 iow(db, DM9000_GPR, 0x01); /* Power-Down PHY */
1332 iow(db, DM9000_IMR, IMR_PAR); /* Disable all interrupt */
1333 iow(db, DM9000_RCR, 0x00); /* Disable RX */
1334}
1335
1336/*
1337 * Stop the interface.
1338 * The interface is stopped when it is brought.
1339 */
1340static int
1341dm9000_stop(struct net_device *ndev)
1342{
Wang Chen4cf16532008-11-12 23:38:14 -08001343 board_info_t *db = netdev_priv(ndev);
Ben Dooksf8d79e72008-06-24 22:16:02 +01001344
1345 if (netif_msg_ifdown(db))
1346 dev_dbg(db->dev, "shutting down %s\n", ndev->name);
1347
1348 cancel_delayed_work_sync(&db->phy_poll);
1349
1350 netif_stop_queue(ndev);
1351 netif_carrier_off(ndev);
1352
1353 /* free interrupt */
1354 free_irq(ndev->irq, ndev);
1355
1356 dm9000_shutdown(ndev);
1357
1358 return 0;
1359}
1360
Alexander Beregalovd88106b2009-04-15 12:52:37 +00001361static const struct net_device_ops dm9000_netdev_ops = {
1362 .ndo_open = dm9000_open,
1363 .ndo_stop = dm9000_stop,
1364 .ndo_start_xmit = dm9000_start_xmit,
1365 .ndo_tx_timeout = dm9000_timeout,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001366 .ndo_set_rx_mode = dm9000_hash_table,
Alexander Beregalovd88106b2009-04-15 12:52:37 +00001367 .ndo_do_ioctl = dm9000_ioctl,
1368 .ndo_change_mtu = eth_change_mtu,
Michał Mirosławc88fcb32011-04-15 04:50:49 +00001369 .ndo_set_features = dm9000_set_features,
Alexander Beregalovd88106b2009-04-15 12:52:37 +00001370 .ndo_validate_addr = eth_validate_addr,
1371 .ndo_set_mac_address = eth_mac_addr,
1372#ifdef CONFIG_NET_POLL_CONTROLLER
1373 .ndo_poll_controller = dm9000_poll_controller,
1374#endif
1375};
1376
Tomasz Figa0b8bf1b2013-05-20 09:16:58 +00001377static struct dm9000_plat_data *dm9000_parse_dt(struct device *dev)
1378{
1379 struct dm9000_plat_data *pdata;
1380 struct device_node *np = dev->of_node;
1381 const void *mac_addr;
1382
1383 if (!IS_ENABLED(CONFIG_OF) || !np)
1384 return NULL;
1385
1386 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1387 if (!pdata)
1388 return ERR_PTR(-ENOMEM);
1389
1390 if (of_find_property(np, "davicom,ext-phy", NULL))
1391 pdata->flags |= DM9000_PLATF_EXT_PHY;
1392 if (of_find_property(np, "davicom,no-eeprom", NULL))
1393 pdata->flags |= DM9000_PLATF_NO_EEPROM;
1394
1395 mac_addr = of_get_mac_address(np);
1396 if (mac_addr)
1397 memcpy(pdata->dev_addr, mac_addr, sizeof(pdata->dev_addr));
1398
1399 return pdata;
1400}
1401
Sascha Hauera1365272005-05-05 15:14:15 -07001402/*
1403 * Search DM9000 board, allocate space and register it
1404 */
Bill Pemberton6b6a3e72012-12-03 09:23:06 -05001405static int
Russell King3ae5eae2005-11-09 22:32:44 +00001406dm9000_probe(struct platform_device *pdev)
Sascha Hauera1365272005-05-05 15:14:15 -07001407{
Jingoo Hancd4e2e42013-08-30 13:55:14 +09001408 struct dm9000_plat_data *pdata = dev_get_platdata(&pdev->dev);
Sascha Hauera1365272005-05-05 15:14:15 -07001409 struct board_info *db; /* Point a board information structure */
1410 struct net_device *ndev;
Ben Dooks179c7432008-02-05 00:02:23 +00001411 const unsigned char *mac_src;
Sascha Hauera1365272005-05-05 15:14:15 -07001412 int ret = 0;
1413 int iosize;
1414 int i;
1415 u32 id_val;
1416
Tomasz Figa0b8bf1b2013-05-20 09:16:58 +00001417 if (!pdata) {
1418 pdata = dm9000_parse_dt(&pdev->dev);
1419 if (IS_ERR(pdata))
1420 return PTR_ERR(pdata);
1421 }
1422
Sascha Hauera1365272005-05-05 15:14:15 -07001423 /* Init network device */
Ben Dooksf8d79e72008-06-24 22:16:02 +01001424 ndev = alloc_etherdev(sizeof(struct board_info));
Joe Perches41de8d42012-01-29 13:47:52 +00001425 if (!ndev)
Sascha Hauera1365272005-05-05 15:14:15 -07001426 return -ENOMEM;
Sascha Hauera1365272005-05-05 15:14:15 -07001427
Russell King3ae5eae2005-11-09 22:32:44 +00001428 SET_NETDEV_DEV(ndev, &pdev->dev);
Sascha Hauera1365272005-05-05 15:14:15 -07001429
Enrico Scholz37d5dca2008-05-08 11:35:13 +01001430 dev_dbg(&pdev->dev, "dm9000_probe()\n");
Sascha Hauera1365272005-05-05 15:14:15 -07001431
1432 /* setup board info structure */
Wang Chen4cf16532008-11-12 23:38:14 -08001433 db = netdev_priv(ndev);
Sascha Hauera1365272005-05-05 15:14:15 -07001434
Ben Dooksa76836f2008-02-05 00:02:02 +00001435 db->dev = &pdev->dev;
Ben Dooks8f5bf5f22008-05-08 11:36:42 +01001436 db->ndev = ndev;
Ben Dooksa76836f2008-02-05 00:02:02 +00001437
Ben Dooks9ef9ac52005-07-23 17:25:18 +01001438 spin_lock_init(&db->lock);
Ben Dooks9a2f0372008-02-05 00:02:10 +00001439 mutex_init(&db->addr_lock);
Ben Dooks9ef9ac52005-07-23 17:25:18 +01001440
Ben Dooks8f5bf5f22008-05-08 11:36:42 +01001441 INIT_DELAYED_WORK(&db->phy_poll, dm9000_poll_work);
1442
Laurent Pinchart08c3f572008-06-24 22:15:57 +01001443 db->addr_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1444 db->data_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1445 db->irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1446
1447 if (db->addr_res == NULL || db->data_res == NULL ||
1448 db->irq_res == NULL) {
1449 dev_err(db->dev, "insufficient resources\n");
1450 ret = -ENOENT;
1451 goto out;
1452 }
1453
Ben Dooksc029f442009-11-10 07:22:24 +00001454 db->irq_wake = platform_get_irq(pdev, 1);
1455 if (db->irq_wake >= 0) {
1456 dev_dbg(db->dev, "wakeup irq %d\n", db->irq_wake);
1457
1458 ret = request_irq(db->irq_wake, dm9000_wol_interrupt,
1459 IRQF_SHARED, dev_name(db->dev), ndev);
1460 if (ret) {
1461 dev_err(db->dev, "cannot get wakeup irq (%d)\n", ret);
1462 } else {
1463
1464 /* test to see if irq is really wakeup capable */
Thomas Gleixnerdced35a2011-03-28 17:49:12 +02001465 ret = irq_set_irq_wake(db->irq_wake, 1);
Ben Dooksc029f442009-11-10 07:22:24 +00001466 if (ret) {
1467 dev_err(db->dev, "irq %d cannot set wakeup (%d)\n",
1468 db->irq_wake, ret);
1469 ret = 0;
1470 } else {
Thomas Gleixnerdced35a2011-03-28 17:49:12 +02001471 irq_set_irq_wake(db->irq_wake, 0);
Ben Dooksc029f442009-11-10 07:22:24 +00001472 db->wake_supported = 1;
1473 }
1474 }
1475 }
1476
Tobias Klauserec282e92009-09-09 01:07:43 +00001477 iosize = resource_size(db->addr_res);
Laurent Pinchart08c3f572008-06-24 22:15:57 +01001478 db->addr_req = request_mem_region(db->addr_res->start, iosize,
1479 pdev->name);
1480
1481 if (db->addr_req == NULL) {
1482 dev_err(db->dev, "cannot claim address reg area\n");
1483 ret = -EIO;
1484 goto out;
1485 }
1486
1487 db->io_addr = ioremap(db->addr_res->start, iosize);
1488
1489 if (db->io_addr == NULL) {
1490 dev_err(db->dev, "failed to ioremap address reg\n");
1491 ret = -EINVAL;
1492 goto out;
1493 }
1494
Tobias Klauserec282e92009-09-09 01:07:43 +00001495 iosize = resource_size(db->data_res);
Laurent Pinchart08c3f572008-06-24 22:15:57 +01001496 db->data_req = request_mem_region(db->data_res->start, iosize,
1497 pdev->name);
1498
1499 if (db->data_req == NULL) {
1500 dev_err(db->dev, "cannot claim data reg area\n");
1501 ret = -EIO;
1502 goto out;
1503 }
1504
1505 db->io_data = ioremap(db->data_res->start, iosize);
1506
1507 if (db->io_data == NULL) {
1508 dev_err(db->dev, "failed to ioremap data reg\n");
1509 ret = -EINVAL;
1510 goto out;
1511 }
1512
1513 /* fill in parameters for net-dev structure */
1514 ndev->base_addr = (unsigned long)db->io_addr;
1515 ndev->irq = db->irq_res->start;
1516
1517 /* ensure at least we have a default set of IO routines */
1518 dm9000_set_io(db, iosize);
1519
Sascha Hauera1365272005-05-05 15:14:15 -07001520 /* check to see if anything is being over-ridden */
1521 if (pdata != NULL) {
1522 /* check to see if the driver wants to over-ride the
1523 * default IO width */
1524
1525 if (pdata->flags & DM9000_PLATF_8BITONLY)
1526 dm9000_set_io(db, 1);
1527
1528 if (pdata->flags & DM9000_PLATF_16BITONLY)
1529 dm9000_set_io(db, 2);
1530
1531 if (pdata->flags & DM9000_PLATF_32BITONLY)
1532 dm9000_set_io(db, 4);
1533
1534 /* check to see if there are any IO routine
1535 * over-rides */
1536
1537 if (pdata->inblk != NULL)
1538 db->inblk = pdata->inblk;
1539
1540 if (pdata->outblk != NULL)
1541 db->outblk = pdata->outblk;
1542
1543 if (pdata->dumpblk != NULL)
1544 db->dumpblk = pdata->dumpblk;
Ben Dooks33ba5092008-02-05 00:02:01 +00001545
1546 db->flags = pdata->flags;
Sascha Hauera1365272005-05-05 15:14:15 -07001547 }
1548
Ben Dooksf8dd0ec2008-06-24 22:16:04 +01001549#ifdef CONFIG_DM9000_FORCE_SIMPLE_PHY_POLL
1550 db->flags |= DM9000_PLATF_SIMPLE_PHY;
1551#endif
1552
Joseph CHANG6741f40d2013-03-28 23:13:42 +00001553 /* Fixing bug on dm9000_probe, takeover dm9000_reset(db),
1554 * Need 'NCR_MAC_LBK' bit to indeed stable our DM9000 fifo
1555 * while probe stage.
1556 */
1557
1558 iow(db, DM9000_NCR, NCR_MAC_LBK | NCR_RST);
Sascha Hauera1365272005-05-05 15:14:15 -07001559
Ben Dooks59eae1f2008-06-24 22:16:01 +01001560 /* try multiple times, DM9000 sometimes gets the read wrong */
Ben Dooks513b6be2008-02-05 00:02:22 +00001561 for (i = 0; i < 8; i++) {
Sascha Hauera1365272005-05-05 15:14:15 -07001562 id_val = ior(db, DM9000_VIDL);
1563 id_val |= (u32)ior(db, DM9000_VIDH) << 8;
1564 id_val |= (u32)ior(db, DM9000_PIDL) << 16;
1565 id_val |= (u32)ior(db, DM9000_PIDH) << 24;
1566
1567 if (id_val == DM9000_ID)
1568 break;
Ben Dooksa76836f2008-02-05 00:02:02 +00001569 dev_err(db->dev, "read wrong id 0x%08x\n", id_val);
Sascha Hauera1365272005-05-05 15:14:15 -07001570 }
1571
1572 if (id_val != DM9000_ID) {
Ben Dooksa76836f2008-02-05 00:02:02 +00001573 dev_err(db->dev, "wrong id: 0x%08x\n", id_val);
Mike Rapoport418d6f82007-10-18 09:23:11 +02001574 ret = -ENODEV;
1575 goto out;
Sascha Hauera1365272005-05-05 15:14:15 -07001576 }
1577
Ben Dooks6d406b32008-06-24 22:15:59 +01001578 /* Identify what type of DM9000 we are working on */
1579
1580 id_val = ior(db, DM9000_CHIPR);
1581 dev_dbg(db->dev, "dm9000 revision 0x%02x\n", id_val);
1582
1583 switch (id_val) {
1584 case CHIPR_DM9000A:
1585 db->type = TYPE_DM9000A;
1586 break;
1587 case CHIPR_DM9000B:
1588 db->type = TYPE_DM9000B;
1589 break;
1590 default:
1591 dev_dbg(db->dev, "ID %02x => defaulting to DM9000E\n", id_val);
1592 db->type = TYPE_DM9000E;
1593 }
1594
Yeasah Pell5dcc60b2009-07-06 18:12:33 -07001595 /* dm9000a/b are capable of hardware checksum offload */
1596 if (db->type == TYPE_DM9000A || db->type == TYPE_DM9000B) {
Michał Mirosławc88fcb32011-04-15 04:50:49 +00001597 ndev->hw_features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM;
1598 ndev->features |= ndev->hw_features;
Yeasah Pell5dcc60b2009-07-06 18:12:33 -07001599 }
1600
Sascha Hauera1365272005-05-05 15:14:15 -07001601 /* from this point we assume that we have found a DM9000 */
1602
1603 /* driver system function */
1604 ether_setup(ndev);
1605
Alexander Beregalovd88106b2009-04-15 12:52:37 +00001606 ndev->netdev_ops = &dm9000_netdev_ops;
1607 ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
1608 ndev->ethtool_ops = &dm9000_ethtool_ops;
Sascha Hauera1365272005-05-05 15:14:15 -07001609
Sascha Hauera1365272005-05-05 15:14:15 -07001610 db->msg_enable = NETIF_MSG_LINK;
1611 db->mii.phy_id_mask = 0x1f;
1612 db->mii.reg_num_mask = 0x1f;
1613 db->mii.force_media = 0;
1614 db->mii.full_duplex = 0;
1615 db->mii.dev = ndev;
1616 db->mii.mdio_read = dm9000_phy_read;
1617 db->mii.mdio_write = dm9000_phy_write;
1618
Ben Dooks179c7432008-02-05 00:02:23 +00001619 mac_src = "eeprom";
1620
Ben Dooks86c62fa2008-02-05 00:02:09 +00001621 /* try reading the node address from the attached EEPROM */
1622 for (i = 0; i < 6; i += 2)
1623 dm9000_read_eeprom(db, i / 2, ndev->dev_addr+i);
Sascha Hauera1365272005-05-05 15:14:15 -07001624
Laurent Pinchartfe414242008-07-23 17:41:52 +02001625 if (!is_valid_ether_addr(ndev->dev_addr) && pdata != NULL) {
1626 mac_src = "platform data";
Joe Perchesd458cdf2013-10-01 19:04:40 -07001627 memcpy(ndev->dev_addr, pdata->dev_addr, ETH_ALEN);
Laurent Pinchartfe414242008-07-23 17:41:52 +02001628 }
1629
Ben Dooks5b55dda2006-06-13 23:50:15 +01001630 if (!is_valid_ether_addr(ndev->dev_addr)) {
1631 /* try reading from mac */
Barry Song5b227212014-01-15 23:31:46 +08001632
Ben Dooks179c7432008-02-05 00:02:23 +00001633 mac_src = "chip";
Ben Dooks5b55dda2006-06-13 23:50:15 +01001634 for (i = 0; i < 6; i++)
1635 ndev->dev_addr[i] = ior(db, i+DM9000_PAR);
1636 }
1637
Ben Dooks85e6b8c2011-02-24 03:17:12 +00001638 if (!is_valid_ether_addr(ndev->dev_addr)) {
Ben Dooksa76836f2008-02-05 00:02:02 +00001639 dev_warn(db->dev, "%s: Invalid ethernet MAC address. Please "
1640 "set using ifconfig\n", ndev->name);
Sascha Hauera1365272005-05-05 15:14:15 -07001641
Danny Kukawkaf2cedb62012-02-15 06:45:39 +00001642 eth_hw_addr_random(ndev);
Ben Dooks85e6b8c2011-02-24 03:17:12 +00001643 mac_src = "random";
1644 }
1645
1646
Russell King3ae5eae2005-11-09 22:32:44 +00001647 platform_set_drvdata(pdev, ndev);
Sascha Hauera1365272005-05-05 15:14:15 -07001648 ret = register_netdev(ndev);
1649
Johannes Berge1749612008-10-27 15:59:26 -07001650 if (ret == 0)
1651 printk(KERN_INFO "%s: dm9000%c at %p,%p IRQ %d MAC: %pM (%s)\n",
Ben Dooks6d406b32008-06-24 22:15:59 +01001652 ndev->name, dm9000_type_to_char(db->type),
1653 db->io_addr, db->io_data, ndev->irq,
Johannes Berge1749612008-10-27 15:59:26 -07001654 ndev->dev_addr, mac_src);
Sascha Hauera1365272005-05-05 15:14:15 -07001655 return 0;
1656
Mike Rapoport418d6f82007-10-18 09:23:11 +02001657out:
Ben Dooksa76836f2008-02-05 00:02:02 +00001658 dev_err(db->dev, "not found (%d).\n", ret);
Sascha Hauera1365272005-05-05 15:14:15 -07001659
1660 dm9000_release_board(pdev, db);
Ben Dooks9fd9f9b2007-05-07 11:13:25 +00001661 free_netdev(ndev);
Sascha Hauera1365272005-05-05 15:14:15 -07001662
1663 return ret;
1664}
1665
Sascha Hauera1365272005-05-05 15:14:15 -07001666static int
Mike Rapoport69222e22009-07-21 12:37:18 -07001667dm9000_drv_suspend(struct device *dev)
Sascha Hauera1365272005-05-05 15:14:15 -07001668{
Mike Rapoport69222e22009-07-21 12:37:18 -07001669 struct platform_device *pdev = to_platform_device(dev);
1670 struct net_device *ndev = platform_get_drvdata(pdev);
Ben Dooks321f69a2008-02-05 00:02:08 +00001671 board_info_t *db;
Sascha Hauera1365272005-05-05 15:14:15 -07001672
Russell King9480e302005-10-28 09:52:56 -07001673 if (ndev) {
Wang Chen4cf16532008-11-12 23:38:14 -08001674 db = netdev_priv(ndev);
Ben Dooks321f69a2008-02-05 00:02:08 +00001675 db->in_suspend = 1;
1676
Ben Dooksc029f442009-11-10 07:22:24 +00001677 if (!netif_running(ndev))
1678 return 0;
1679
1680 netif_device_detach(ndev);
1681
1682 /* only shutdown if not using WoL */
1683 if (!db->wake_state)
Sascha Hauera1365272005-05-05 15:14:15 -07001684 dm9000_shutdown(ndev);
Sascha Hauera1365272005-05-05 15:14:15 -07001685 }
1686 return 0;
1687}
1688
1689static int
Mike Rapoport69222e22009-07-21 12:37:18 -07001690dm9000_drv_resume(struct device *dev)
Sascha Hauera1365272005-05-05 15:14:15 -07001691{
Mike Rapoport69222e22009-07-21 12:37:18 -07001692 struct platform_device *pdev = to_platform_device(dev);
1693 struct net_device *ndev = platform_get_drvdata(pdev);
Wang Chen4cf16532008-11-12 23:38:14 -08001694 board_info_t *db = netdev_priv(ndev);
Sascha Hauera1365272005-05-05 15:14:15 -07001695
Russell King9480e302005-10-28 09:52:56 -07001696 if (ndev) {
Sascha Hauera1365272005-05-05 15:14:15 -07001697 if (netif_running(ndev)) {
Ben Dooksc029f442009-11-10 07:22:24 +00001698 /* reset if we were not in wake mode to ensure if
1699 * the device was powered off it is in a known state */
1700 if (!db->wake_state) {
1701 dm9000_reset(db);
1702 dm9000_init_dm9000(ndev);
1703 }
Sascha Hauera1365272005-05-05 15:14:15 -07001704
1705 netif_device_attach(ndev);
1706 }
Ben Dooks321f69a2008-02-05 00:02:08 +00001707
1708 db->in_suspend = 0;
Sascha Hauera1365272005-05-05 15:14:15 -07001709 }
1710 return 0;
1711}
1712
Alexey Dobriyan47145212009-12-14 18:00:08 -08001713static const struct dev_pm_ops dm9000_drv_pm_ops = {
Mike Rapoport69222e22009-07-21 12:37:18 -07001714 .suspend = dm9000_drv_suspend,
1715 .resume = dm9000_drv_resume,
1716};
1717
Bill Pemberton6b6a3e72012-12-03 09:23:06 -05001718static int
Russell King3ae5eae2005-11-09 22:32:44 +00001719dm9000_drv_remove(struct platform_device *pdev)
Sascha Hauera1365272005-05-05 15:14:15 -07001720{
Russell King3ae5eae2005-11-09 22:32:44 +00001721 struct net_device *ndev = platform_get_drvdata(pdev);
Sascha Hauera1365272005-05-05 15:14:15 -07001722
Sascha Hauera1365272005-05-05 15:14:15 -07001723 unregister_netdev(ndev);
Joe Perchesece49152010-11-15 11:12:31 +00001724 dm9000_release_board(pdev, netdev_priv(ndev));
Ben Dooks9fd9f9b2007-05-07 11:13:25 +00001725 free_netdev(ndev); /* free device structure */
Sascha Hauera1365272005-05-05 15:14:15 -07001726
Ben Dooksa76836f2008-02-05 00:02:02 +00001727 dev_dbg(&pdev->dev, "released and freed device\n");
Sascha Hauera1365272005-05-05 15:14:15 -07001728 return 0;
1729}
1730
Tomasz Figa0b8bf1b2013-05-20 09:16:58 +00001731#ifdef CONFIG_OF
1732static const struct of_device_id dm9000_of_matches[] = {
1733 { .compatible = "davicom,dm9000", },
1734 { /* sentinel */ }
1735};
1736MODULE_DEVICE_TABLE(of, dm9000_of_matches);
1737#endif
1738
Russell King3ae5eae2005-11-09 22:32:44 +00001739static struct platform_driver dm9000_driver = {
Ben Dooks5d22a312006-06-14 00:09:17 +01001740 .driver = {
1741 .name = "dm9000",
1742 .owner = THIS_MODULE,
Mike Rapoport69222e22009-07-21 12:37:18 -07001743 .pm = &dm9000_drv_pm_ops,
Tomasz Figa0b8bf1b2013-05-20 09:16:58 +00001744 .of_match_table = of_match_ptr(dm9000_of_matches),
Ben Dooks5d22a312006-06-14 00:09:17 +01001745 },
Sascha Hauera1365272005-05-05 15:14:15 -07001746 .probe = dm9000_probe,
Bill Pemberton6b6a3e72012-12-03 09:23:06 -05001747 .remove = dm9000_drv_remove,
Sascha Hauera1365272005-05-05 15:14:15 -07001748};
1749
Sachin Kamata8f9c3e2013-03-18 01:50:46 +00001750module_platform_driver(dm9000_driver);
Sascha Hauera1365272005-05-05 15:14:15 -07001751
1752MODULE_AUTHOR("Sascha Hauer, Ben Dooks");
1753MODULE_DESCRIPTION("Davicom DM9000 network driver");
1754MODULE_LICENSE("GPL");
Kay Sievers72abb462008-04-18 13:50:44 -07001755MODULE_ALIAS("platform:dm9000");