blob: 79bdd9e29a01fe919d8783c4a5c9712a50a2f23f [file] [log] [blame]
Sascha Hauera1365272005-05-05 15:14:15 -07001/*
Ben Dooks41c340f2008-02-05 00:02:15 +00002 * Davicom DM9000 Fast Ethernet driver for Linux.
Sascha Hauera1365272005-05-05 15:14:15 -07003 * Copyright (C) 1997 Sten Wang
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
Ben Dooks41c340f2008-02-05 00:02:15 +000015 * (C) Copyright 1997-1998 DAVICOM Semiconductor,Inc. All Rights Reserved.
Sascha Hauera1365272005-05-05 15:14:15 -070016 *
Ben Dooks41c340f2008-02-05 00:02:15 +000017 * Additional updates, Copyright:
18 * Ben Dooks <ben@simtec.co.uk>
19 * Sascha Hauer <s.hauer@pengutronix.de>
Sascha Hauera1365272005-05-05 15:14:15 -070020 */
21
22#include <linux/module.h>
23#include <linux/ioport.h>
24#include <linux/netdevice.h>
25#include <linux/etherdevice.h>
26#include <linux/init.h>
27#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>
Ben Dooks7da99852008-02-05 00:02:06 +000031#include <linux/ethtool.h>
Sascha Hauera1365272005-05-05 15:14:15 -070032#include <linux/dm9000.h>
33#include <linux/delay.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010034#include <linux/platform_device.h>
Daniel Mack4e4fc052008-01-23 14:54:50 +010035#include <linux/irq.h>
Sascha Hauera1365272005-05-05 15:14:15 -070036
37#include <asm/delay.h>
38#include <asm/irq.h>
39#include <asm/io.h>
40
41#include "dm9000.h"
42
43/* Board/System/Debug information/definition ---------------- */
44
45#define DM9000_PHY 0x40 /* PHY address 0x01 */
46
Sascha Hauera1365272005-05-05 15:14:15 -070047#define CARDNAME "dm9000"
48#define PFX CARDNAME ": "
Ben Dooks7da99852008-02-05 00:02:06 +000049#define DRV_VERSION "1.30"
Sascha Hauera1365272005-05-05 15:14:15 -070050
Alex Landauf40d24d2007-07-12 12:11:48 +080051#ifdef CONFIG_BLACKFIN
52#define readsb insb
53#define readsw insw
54#define readsl insl
55#define writesb outsb
56#define writesw outsw
57#define writesl outsl
Ben Dooks1a5f1c42008-02-05 00:02:04 +000058#define DEFAULT_TRIGGER IRQF_TRIGGER_HIGH
Alex Landauf40d24d2007-07-12 12:11:48 +080059#else
Ben Dooks1a5f1c42008-02-05 00:02:04 +000060#define DEFAULT_TRIGGER (0)
Alex Landauf40d24d2007-07-12 12:11:48 +080061#endif
62
Sascha Hauera1365272005-05-05 15:14:15 -070063/*
64 * Transmit timeout, default 5 seconds.
65 */
66static int watchdog = 5000;
67module_param(watchdog, int, 0400);
68MODULE_PARM_DESC(watchdog, "transmit timeout in milliseconds");
69
Ben Dooks9a2f0372008-02-05 00:02:10 +000070/* 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
Ben Dooks6d406b32008-06-24 22:15:59 +010088/* 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
Sascha Hauera1365272005-05-05 15:14:15 -070098/* Structure/enum declaration ------------------------------- */
99typedef struct board_info {
100
101 void __iomem *io_addr; /* Register I/O base address */
102 void __iomem *io_data; /* Data I/O address */
103 u16 irq; /* IRQ */
104
105 u16 tx_pkt_cnt;
106 u16 queue_pkt_len;
107 u16 queue_start_addr;
108 u16 dbug_cnt;
109 u8 io_mode; /* 0:word, 2:byte */
110 u8 phy_addr;
Ben Dooks6d406b32008-06-24 22:15:59 +0100111 u8 imr_all;
Ben Dooks33ba5092008-02-05 00:02:01 +0000112 unsigned int flags;
Ben Dooks321f69a2008-02-05 00:02:08 +0000113 unsigned int in_suspend :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 int debug_level;
117
Sascha Hauera1365272005-05-05 15:14:15 -0700118 void (*inblk)(void __iomem *port, void *data, int length);
119 void (*outblk)(void __iomem *port, void *data, int length);
120 void (*dumpblk)(void __iomem *port, int length);
121
Ben Dooksa76836f2008-02-05 00:02:02 +0000122 struct device *dev; /* parent device */
123
Sascha Hauera1365272005-05-05 15:14:15 -0700124 struct resource *addr_res; /* resources found */
125 struct resource *data_res;
126 struct resource *addr_req; /* resources requested */
127 struct resource *data_req;
128 struct resource *irq_res;
129
Ben Dooks9a2f0372008-02-05 00:02:10 +0000130 struct mutex addr_lock; /* phy and eeprom access lock */
131
Ben Dooks8f5bf5f22008-05-08 11:36:42 +0100132 struct delayed_work phy_poll;
133 struct net_device *ndev;
134
Sascha Hauera1365272005-05-05 15:14:15 -0700135 spinlock_t lock;
136
137 struct mii_if_info mii;
138 u32 msg_enable;
139} board_info_t;
140
Ben Dooks5b2b4ff2008-02-05 00:02:03 +0000141/* debug code */
142
143#define dm9000_dbg(db, lev, msg...) do { \
144 if ((lev) < CONFIG_DM9000_DEBUGLEVEL && \
145 (lev) < db->debug_level) { \
146 dev_dbg(db->dev, msg); \
147 } \
148} while (0)
149
Ben Dooks7da99852008-02-05 00:02:06 +0000150static inline board_info_t *to_dm9000_board(struct net_device *dev)
151{
152 return dev->priv;
153}
154
Sascha Hauera1365272005-05-05 15:14:15 -0700155/* function declaration ------------------------------------- */
Russell King3ae5eae2005-11-09 22:32:44 +0000156static int dm9000_probe(struct platform_device *);
Sascha Hauera1365272005-05-05 15:14:15 -0700157static int dm9000_open(struct net_device *);
158static int dm9000_start_xmit(struct sk_buff *, struct net_device *);
159static int dm9000_stop(struct net_device *);
Ben Dooksf42d8ae2008-02-05 00:02:21 +0000160static int dm9000_ioctl(struct net_device *dev, struct ifreq *req, int cmd);
Sascha Hauera1365272005-05-05 15:14:15 -0700161
Sascha Hauera1365272005-05-05 15:14:15 -0700162static void dm9000_init_dm9000(struct net_device *);
163
David Howells7d12e782006-10-05 14:55:46 +0100164static irqreturn_t dm9000_interrupt(int, void *);
Sascha Hauera1365272005-05-05 15:14:15 -0700165
166static int dm9000_phy_read(struct net_device *dev, int phyaddr_unsused, int reg);
167static void dm9000_phy_write(struct net_device *dev, int phyaddr_unused, int reg,
168 int value);
Ben Dooks86c62fa2008-02-05 00:02:09 +0000169
Ben Dooks29d52e52008-02-05 00:02:11 +0000170static void dm9000_read_eeprom(board_info_t *, int addr, u8 *to);
171static void dm9000_write_eeprom(board_info_t *, int addr, u8 *dp);
Sascha Hauera1365272005-05-05 15:14:15 -0700172static void dm9000_rx(struct net_device *);
173static void dm9000_hash_table(struct net_device *);
174
Sascha Hauera1365272005-05-05 15:14:15 -0700175/* DM9000 network board routine ---------------------------- */
176
177static void
178dm9000_reset(board_info_t * db)
179{
Ben Dooksa76836f2008-02-05 00:02:02 +0000180 dev_dbg(db->dev, "resetting device\n");
181
Sascha Hauera1365272005-05-05 15:14:15 -0700182 /* RESET device */
183 writeb(DM9000_NCR, db->io_addr);
184 udelay(200);
185 writeb(NCR_RST, db->io_data);
186 udelay(200);
187}
188
189/*
190 * Read a byte from I/O port
191 */
192static u8
193ior(board_info_t * db, int reg)
194{
195 writeb(reg, db->io_addr);
196 return readb(db->io_data);
197}
198
199/*
200 * Write a byte to I/O port
201 */
202
203static void
204iow(board_info_t * db, int reg, int value)
205{
206 writeb(reg, db->io_addr);
207 writeb(value, db->io_data);
208}
209
210/* routines for sending block to chip */
211
212static void dm9000_outblk_8bit(void __iomem *reg, void *data, int count)
213{
214 writesb(reg, data, count);
215}
216
217static void dm9000_outblk_16bit(void __iomem *reg, void *data, int count)
218{
219 writesw(reg, data, (count+1) >> 1);
220}
221
222static void dm9000_outblk_32bit(void __iomem *reg, void *data, int count)
223{
224 writesl(reg, data, (count+3) >> 2);
225}
226
227/* input block from chip to memory */
228
229static void dm9000_inblk_8bit(void __iomem *reg, void *data, int count)
230{
Sascha Hauer5f6b5512005-06-20 15:32:51 -0700231 readsb(reg, data, count);
Sascha Hauera1365272005-05-05 15:14:15 -0700232}
233
234
235static void dm9000_inblk_16bit(void __iomem *reg, void *data, int count)
236{
237 readsw(reg, data, (count+1) >> 1);
238}
239
240static void dm9000_inblk_32bit(void __iomem *reg, void *data, int count)
241{
242 readsl(reg, data, (count+3) >> 2);
243}
244
245/* dump block from chip to null */
246
247static void dm9000_dumpblk_8bit(void __iomem *reg, int count)
248{
249 int i;
250 int tmp;
251
252 for (i = 0; i < count; i++)
253 tmp = readb(reg);
254}
255
256static void dm9000_dumpblk_16bit(void __iomem *reg, int count)
257{
258 int i;
259 int tmp;
260
261 count = (count + 1) >> 1;
262
263 for (i = 0; i < count; i++)
264 tmp = readw(reg);
265}
266
267static void dm9000_dumpblk_32bit(void __iomem *reg, int count)
268{
269 int i;
270 int tmp;
271
272 count = (count + 3) >> 2;
273
274 for (i = 0; i < count; i++)
275 tmp = readl(reg);
276}
277
278/* dm9000_set_io
279 *
280 * select the specified set of io routines to use with the
281 * device
282 */
283
284static void dm9000_set_io(struct board_info *db, int byte_width)
285{
286 /* use the size of the data resource to work out what IO
287 * routines we want to use
288 */
289
290 switch (byte_width) {
291 case 1:
292 db->dumpblk = dm9000_dumpblk_8bit;
293 db->outblk = dm9000_outblk_8bit;
294 db->inblk = dm9000_inblk_8bit;
295 break;
296
Sascha Hauera1365272005-05-05 15:14:15 -0700297
298 case 3:
Ben Dooksa76836f2008-02-05 00:02:02 +0000299 dev_dbg(db->dev, ": 3 byte IO, falling back to 16bit\n");
300 case 2:
Sascha Hauera1365272005-05-05 15:14:15 -0700301 db->dumpblk = dm9000_dumpblk_16bit;
302 db->outblk = dm9000_outblk_16bit;
303 db->inblk = dm9000_inblk_16bit;
304 break;
305
306 case 4:
307 default:
308 db->dumpblk = dm9000_dumpblk_32bit;
309 db->outblk = dm9000_outblk_32bit;
310 db->inblk = dm9000_inblk_32bit;
311 break;
312 }
313}
314
Ben Dooks8f5bf5f22008-05-08 11:36:42 +0100315static void dm9000_schedule_poll(board_info_t *db)
316{
Ben Dooks6d406b32008-06-24 22:15:59 +0100317 if (db->type == TYPE_DM9000E)
318 schedule_delayed_work(&db->phy_poll, HZ * 2);
Ben Dooks8f5bf5f22008-05-08 11:36:42 +0100319}
Sascha Hauera1365272005-05-05 15:14:15 -0700320
321/* Our watchdog timed out. Called by the networking layer */
322static void dm9000_timeout(struct net_device *dev)
323{
324 board_info_t *db = (board_info_t *) dev->priv;
325 u8 reg_save;
326 unsigned long flags;
327
328 /* Save previous register address */
329 reg_save = readb(db->io_addr);
Ben Dooks9ef9ac52005-07-23 17:25:18 +0100330 spin_lock_irqsave(&db->lock,flags);
Sascha Hauera1365272005-05-05 15:14:15 -0700331
332 netif_stop_queue(dev);
333 dm9000_reset(db);
334 dm9000_init_dm9000(dev);
335 /* We can accept TX packets again */
336 dev->trans_start = jiffies;
337 netif_wake_queue(dev);
338
339 /* Restore previous register address */
340 writeb(reg_save, db->io_addr);
Ben Dooks9ef9ac52005-07-23 17:25:18 +0100341 spin_unlock_irqrestore(&db->lock,flags);
Sascha Hauera1365272005-05-05 15:14:15 -0700342}
343
Kevin Hao2fd0e332006-08-14 23:00:15 -0700344#ifdef CONFIG_NET_POLL_CONTROLLER
345/*
346 *Used by netconsole
347 */
348static void dm9000_poll_controller(struct net_device *dev)
349{
350 disable_irq(dev->irq);
Al Viro28431142006-10-08 15:00:12 +0100351 dm9000_interrupt(dev->irq,dev);
Kevin Hao2fd0e332006-08-14 23:00:15 -0700352 enable_irq(dev->irq);
353}
354#endif
Sascha Hauera1365272005-05-05 15:14:15 -0700355
Ben Dooksf42d8ae2008-02-05 00:02:21 +0000356static int dm9000_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
357{
358 board_info_t *dm = to_dm9000_board(dev);
359
360 if (!netif_running(dev))
361 return -EINVAL;
362
363 return generic_mii_ioctl(&dm->mii, if_mii(req), cmd, NULL);
364}
365
Ben Dooks7da99852008-02-05 00:02:06 +0000366/* ethtool ops */
367
368static void dm9000_get_drvinfo(struct net_device *dev,
369 struct ethtool_drvinfo *info)
370{
371 board_info_t *dm = to_dm9000_board(dev);
372
373 strcpy(info->driver, CARDNAME);
374 strcpy(info->version, DRV_VERSION);
375 strcpy(info->bus_info, to_platform_device(dm->dev)->name);
376}
377
Ben Dookse662ee02008-02-05 00:02:12 +0000378static u32 dm9000_get_msglevel(struct net_device *dev)
379{
380 board_info_t *dm = to_dm9000_board(dev);
381
382 return dm->msg_enable;
383}
384
385static void dm9000_set_msglevel(struct net_device *dev, u32 value)
386{
387 board_info_t *dm = to_dm9000_board(dev);
388
389 dm->msg_enable = value;
390}
391
Ben Dooks7da99852008-02-05 00:02:06 +0000392static int dm9000_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
393{
394 board_info_t *dm = to_dm9000_board(dev);
Ben Dooks7da99852008-02-05 00:02:06 +0000395
Ben Dooks7da99852008-02-05 00:02:06 +0000396 mii_ethtool_gset(&dm->mii, cmd);
Ben Dooks7da99852008-02-05 00:02:06 +0000397 return 0;
398}
399
400static int dm9000_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
401{
402 board_info_t *dm = to_dm9000_board(dev);
Ben Dooks7da99852008-02-05 00:02:06 +0000403
Ben Dooks9a2f0372008-02-05 00:02:10 +0000404 return mii_ethtool_sset(&dm->mii, cmd);
Ben Dooks7da99852008-02-05 00:02:06 +0000405}
406
407static int dm9000_nway_reset(struct net_device *dev)
408{
409 board_info_t *dm = to_dm9000_board(dev);
410 return mii_nway_restart(&dm->mii);
411}
412
413static u32 dm9000_get_link(struct net_device *dev)
414{
415 board_info_t *dm = to_dm9000_board(dev);
416 return mii_link_ok(&dm->mii);
417}
418
Ben Dooks29d52e52008-02-05 00:02:11 +0000419#define DM_EEPROM_MAGIC (0x444D394B)
420
421static int dm9000_get_eeprom_len(struct net_device *dev)
422{
423 return 128;
424}
425
426static int dm9000_get_eeprom(struct net_device *dev,
427 struct ethtool_eeprom *ee, u8 *data)
428{
429 board_info_t *dm = to_dm9000_board(dev);
430 int offset = ee->offset;
431 int len = ee->len;
432 int i;
433
434 /* EEPROM access is aligned to two bytes */
435
436 if ((len & 1) != 0 || (offset & 1) != 0)
437 return -EINVAL;
438
Ben Dooksbb44fb702008-02-05 00:02:20 +0000439 if (dm->flags & DM9000_PLATF_NO_EEPROM)
440 return -ENOENT;
441
Ben Dooks29d52e52008-02-05 00:02:11 +0000442 ee->magic = DM_EEPROM_MAGIC;
443
444 for (i = 0; i < len; i += 2)
445 dm9000_read_eeprom(dm, (offset + i) / 2, data + i);
446
447 return 0;
448}
449
450static int dm9000_set_eeprom(struct net_device *dev,
451 struct ethtool_eeprom *ee, u8 *data)
452{
453 board_info_t *dm = to_dm9000_board(dev);
454 int offset = ee->offset;
455 int len = ee->len;
456 int i;
457
458 /* EEPROM access is aligned to two bytes */
459
460 if ((len & 1) != 0 || (offset & 1) != 0)
461 return -EINVAL;
462
Ben Dooksbb44fb702008-02-05 00:02:20 +0000463 if (dm->flags & DM9000_PLATF_NO_EEPROM)
464 return -ENOENT;
465
Ben Dooks29d52e52008-02-05 00:02:11 +0000466 if (ee->magic != DM_EEPROM_MAGIC)
467 return -EINVAL;
468
469 for (i = 0; i < len; i += 2)
470 dm9000_write_eeprom(dm, (offset + i) / 2, data + i);
471
472 return 0;
473}
474
Ben Dooks7da99852008-02-05 00:02:06 +0000475static const struct ethtool_ops dm9000_ethtool_ops = {
476 .get_drvinfo = dm9000_get_drvinfo,
477 .get_settings = dm9000_get_settings,
478 .set_settings = dm9000_set_settings,
Ben Dookse662ee02008-02-05 00:02:12 +0000479 .get_msglevel = dm9000_get_msglevel,
480 .set_msglevel = dm9000_set_msglevel,
Ben Dooks7da99852008-02-05 00:02:06 +0000481 .nway_reset = dm9000_nway_reset,
482 .get_link = dm9000_get_link,
Ben Dooks29d52e52008-02-05 00:02:11 +0000483 .get_eeprom_len = dm9000_get_eeprom_len,
484 .get_eeprom = dm9000_get_eeprom,
485 .set_eeprom = dm9000_set_eeprom,
Ben Dooks7da99852008-02-05 00:02:06 +0000486};
487
Ben Dooks8f5bf5f22008-05-08 11:36:42 +0100488static void
489dm9000_poll_work(struct work_struct *w)
490{
491 struct delayed_work *dw = container_of(w, struct delayed_work, work);
492 board_info_t *db = container_of(dw, board_info_t, phy_poll);
493
494 mii_check_media(&db->mii, netif_msg_link(db), 0);
495
496 if (netif_running(db->ndev))
497 dm9000_schedule_poll(db);
498}
Ben Dooks7da99852008-02-05 00:02:06 +0000499
Sascha Hauera1365272005-05-05 15:14:15 -0700500/* dm9000_release_board
501 *
502 * release a board, and any mapped resources
503 */
504
505static void
506dm9000_release_board(struct platform_device *pdev, struct board_info *db)
507{
Sascha Hauera1365272005-05-05 15:14:15 -0700508 /* unmap our resources */
509
510 iounmap(db->io_addr);
511 iounmap(db->io_data);
512
513 /* release the resources */
514
Ben Dooks9088fa4f2008-06-24 22:16:00 +0100515 release_resource(db->data_req);
516 kfree(db->data_req);
Sascha Hauera1365272005-05-05 15:14:15 -0700517
Ben Dooks9088fa4f2008-06-24 22:16:00 +0100518 release_resource(db->addr_req);
519 kfree(db->addr_req);
Sascha Hauera1365272005-05-05 15:14:15 -0700520}
521
Ben Dooks6d406b32008-06-24 22:15:59 +0100522static unsigned char dm9000_type_to_char(enum dm9000_type type)
523{
524 switch (type) {
525 case TYPE_DM9000E: return 'e';
526 case TYPE_DM9000A: return 'a';
527 case TYPE_DM9000B: return 'b';
528 }
529
530 return '?';
531}
532
Sascha Hauera1365272005-05-05 15:14:15 -0700533#define res_size(_r) (((_r)->end - (_r)->start) + 1)
534
535/*
536 * Search DM9000 board, allocate space and register it
537 */
Enrico Scholze21fd4f2008-05-08 11:33:03 +0100538static int __devinit
Russell King3ae5eae2005-11-09 22:32:44 +0000539dm9000_probe(struct platform_device *pdev)
Sascha Hauera1365272005-05-05 15:14:15 -0700540{
Sascha Hauera1365272005-05-05 15:14:15 -0700541 struct dm9000_plat_data *pdata = pdev->dev.platform_data;
542 struct board_info *db; /* Point a board information structure */
543 struct net_device *ndev;
Ben Dooks179c7432008-02-05 00:02:23 +0000544 const unsigned char *mac_src;
Sascha Hauera1365272005-05-05 15:14:15 -0700545 int ret = 0;
546 int iosize;
547 int i;
548 u32 id_val;
549
Sascha Hauera1365272005-05-05 15:14:15 -0700550 /* Init network device */
551 ndev = alloc_etherdev(sizeof (struct board_info));
552 if (!ndev) {
Ben Dooksa76836f2008-02-05 00:02:02 +0000553 dev_err(&pdev->dev, "could not allocate device.\n");
Sascha Hauera1365272005-05-05 15:14:15 -0700554 return -ENOMEM;
555 }
556
Russell King3ae5eae2005-11-09 22:32:44 +0000557 SET_NETDEV_DEV(ndev, &pdev->dev);
Sascha Hauera1365272005-05-05 15:14:15 -0700558
Enrico Scholz37d5dca2008-05-08 11:35:13 +0100559 dev_dbg(&pdev->dev, "dm9000_probe()\n");
Sascha Hauera1365272005-05-05 15:14:15 -0700560
561 /* setup board info structure */
562 db = (struct board_info *) ndev->priv;
563 memset(db, 0, sizeof (*db));
564
Ben Dooksa76836f2008-02-05 00:02:02 +0000565 db->dev = &pdev->dev;
Ben Dooks8f5bf5f22008-05-08 11:36:42 +0100566 db->ndev = ndev;
Ben Dooksa76836f2008-02-05 00:02:02 +0000567
Ben Dooks9ef9ac52005-07-23 17:25:18 +0100568 spin_lock_init(&db->lock);
Ben Dooks9a2f0372008-02-05 00:02:10 +0000569 mutex_init(&db->addr_lock);
Ben Dooks9ef9ac52005-07-23 17:25:18 +0100570
Ben Dooks8f5bf5f22008-05-08 11:36:42 +0100571 INIT_DELAYED_WORK(&db->phy_poll, dm9000_poll_work);
572
Laurent Pinchart08c3f572008-06-24 22:15:57 +0100573 db->addr_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
574 db->data_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
575 db->irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
576
577 if (db->addr_res == NULL || db->data_res == NULL ||
578 db->irq_res == NULL) {
579 dev_err(db->dev, "insufficient resources\n");
580 ret = -ENOENT;
581 goto out;
582 }
583
584 iosize = res_size(db->addr_res);
585 db->addr_req = request_mem_region(db->addr_res->start, iosize,
586 pdev->name);
587
588 if (db->addr_req == NULL) {
589 dev_err(db->dev, "cannot claim address reg area\n");
590 ret = -EIO;
591 goto out;
592 }
593
594 db->io_addr = ioremap(db->addr_res->start, iosize);
595
596 if (db->io_addr == NULL) {
597 dev_err(db->dev, "failed to ioremap address reg\n");
598 ret = -EINVAL;
599 goto out;
600 }
601
602 iosize = res_size(db->data_res);
603 db->data_req = request_mem_region(db->data_res->start, iosize,
604 pdev->name);
605
606 if (db->data_req == NULL) {
607 dev_err(db->dev, "cannot claim data reg area\n");
608 ret = -EIO;
609 goto out;
610 }
611
612 db->io_data = ioremap(db->data_res->start, iosize);
613
614 if (db->io_data == NULL) {
615 dev_err(db->dev, "failed to ioremap data reg\n");
616 ret = -EINVAL;
617 goto out;
618 }
619
620 /* fill in parameters for net-dev structure */
621 ndev->base_addr = (unsigned long)db->io_addr;
622 ndev->irq = db->irq_res->start;
623
624 /* ensure at least we have a default set of IO routines */
625 dm9000_set_io(db, iosize);
626
Sascha Hauera1365272005-05-05 15:14:15 -0700627 /* check to see if anything is being over-ridden */
628 if (pdata != NULL) {
629 /* check to see if the driver wants to over-ride the
630 * default IO width */
631
632 if (pdata->flags & DM9000_PLATF_8BITONLY)
633 dm9000_set_io(db, 1);
634
635 if (pdata->flags & DM9000_PLATF_16BITONLY)
636 dm9000_set_io(db, 2);
637
638 if (pdata->flags & DM9000_PLATF_32BITONLY)
639 dm9000_set_io(db, 4);
640
641 /* check to see if there are any IO routine
642 * over-rides */
643
644 if (pdata->inblk != NULL)
645 db->inblk = pdata->inblk;
646
647 if (pdata->outblk != NULL)
648 db->outblk = pdata->outblk;
649
650 if (pdata->dumpblk != NULL)
651 db->dumpblk = pdata->dumpblk;
Ben Dooks33ba5092008-02-05 00:02:01 +0000652
653 db->flags = pdata->flags;
Sascha Hauera1365272005-05-05 15:14:15 -0700654 }
655
656 dm9000_reset(db);
657
658 /* try two times, DM9000 sometimes gets the first read wrong */
Ben Dooks513b6be2008-02-05 00:02:22 +0000659 for (i = 0; i < 8; i++) {
Sascha Hauera1365272005-05-05 15:14:15 -0700660 id_val = ior(db, DM9000_VIDL);
661 id_val |= (u32)ior(db, DM9000_VIDH) << 8;
662 id_val |= (u32)ior(db, DM9000_PIDL) << 16;
663 id_val |= (u32)ior(db, DM9000_PIDH) << 24;
664
665 if (id_val == DM9000_ID)
666 break;
Ben Dooksa76836f2008-02-05 00:02:02 +0000667 dev_err(db->dev, "read wrong id 0x%08x\n", id_val);
Sascha Hauera1365272005-05-05 15:14:15 -0700668 }
669
670 if (id_val != DM9000_ID) {
Ben Dooksa76836f2008-02-05 00:02:02 +0000671 dev_err(db->dev, "wrong id: 0x%08x\n", id_val);
Mike Rapoport418d6f82007-10-18 09:23:11 +0200672 ret = -ENODEV;
673 goto out;
Sascha Hauera1365272005-05-05 15:14:15 -0700674 }
675
Ben Dooks6d406b32008-06-24 22:15:59 +0100676 /* Identify what type of DM9000 we are working on */
677
678 id_val = ior(db, DM9000_CHIPR);
679 dev_dbg(db->dev, "dm9000 revision 0x%02x\n", id_val);
680
681 switch (id_val) {
682 case CHIPR_DM9000A:
683 db->type = TYPE_DM9000A;
684 break;
685 case CHIPR_DM9000B:
686 db->type = TYPE_DM9000B;
687 break;
688 default:
689 dev_dbg(db->dev, "ID %02x => defaulting to DM9000E\n", id_val);
690 db->type = TYPE_DM9000E;
691 }
692
Sascha Hauera1365272005-05-05 15:14:15 -0700693 /* from this point we assume that we have found a DM9000 */
694
695 /* driver system function */
696 ether_setup(ndev);
697
698 ndev->open = &dm9000_open;
699 ndev->hard_start_xmit = &dm9000_start_xmit;
700 ndev->tx_timeout = &dm9000_timeout;
701 ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
702 ndev->stop = &dm9000_stop;
Sascha Hauera1365272005-05-05 15:14:15 -0700703 ndev->set_multicast_list = &dm9000_hash_table;
Ben Dooks7da99852008-02-05 00:02:06 +0000704 ndev->ethtool_ops = &dm9000_ethtool_ops;
Ben Dooksf42d8ae2008-02-05 00:02:21 +0000705 ndev->do_ioctl = &dm9000_ioctl;
Ben Dooks7da99852008-02-05 00:02:06 +0000706
Kevin Hao2fd0e332006-08-14 23:00:15 -0700707#ifdef CONFIG_NET_POLL_CONTROLLER
708 ndev->poll_controller = &dm9000_poll_controller;
709#endif
Sascha Hauera1365272005-05-05 15:14:15 -0700710
Sascha Hauera1365272005-05-05 15:14:15 -0700711 db->msg_enable = NETIF_MSG_LINK;
712 db->mii.phy_id_mask = 0x1f;
713 db->mii.reg_num_mask = 0x1f;
714 db->mii.force_media = 0;
715 db->mii.full_duplex = 0;
716 db->mii.dev = ndev;
717 db->mii.mdio_read = dm9000_phy_read;
718 db->mii.mdio_write = dm9000_phy_write;
719
Ben Dooks179c7432008-02-05 00:02:23 +0000720 mac_src = "eeprom";
721
Ben Dooks86c62fa2008-02-05 00:02:09 +0000722 /* try reading the node address from the attached EEPROM */
723 for (i = 0; i < 6; i += 2)
724 dm9000_read_eeprom(db, i / 2, ndev->dev_addr+i);
Sascha Hauera1365272005-05-05 15:14:15 -0700725
Ben Dooks5b55dda2006-06-13 23:50:15 +0100726 if (!is_valid_ether_addr(ndev->dev_addr)) {
727 /* try reading from mac */
Jeff Garzik7d2e3cb2008-05-13 01:41:58 -0400728
Ben Dooks179c7432008-02-05 00:02:23 +0000729 mac_src = "chip";
Ben Dooks5b55dda2006-06-13 23:50:15 +0100730 for (i = 0; i < 6; i++)
731 ndev->dev_addr[i] = ior(db, i+DM9000_PAR);
732 }
733
Sascha Hauera1365272005-05-05 15:14:15 -0700734 if (!is_valid_ether_addr(ndev->dev_addr))
Ben Dooksa76836f2008-02-05 00:02:02 +0000735 dev_warn(db->dev, "%s: Invalid ethernet MAC address. Please "
736 "set using ifconfig\n", ndev->name);
Sascha Hauera1365272005-05-05 15:14:15 -0700737
Russell King3ae5eae2005-11-09 22:32:44 +0000738 platform_set_drvdata(pdev, ndev);
Sascha Hauera1365272005-05-05 15:14:15 -0700739 ret = register_netdev(ndev);
740
741 if (ret == 0) {
Joe Perches0795af52007-10-03 17:59:30 -0700742 DECLARE_MAC_BUF(mac);
Ben Dooks6d406b32008-06-24 22:15:59 +0100743 printk(KERN_INFO "%s: dm9000%c at %p,%p IRQ %d MAC: %s (%s)\n",
744 ndev->name, dm9000_type_to_char(db->type),
745 db->io_addr, db->io_data, ndev->irq,
Ben Dooks179c7432008-02-05 00:02:23 +0000746 print_mac(mac, ndev->dev_addr), mac_src);
Sascha Hauera1365272005-05-05 15:14:15 -0700747 }
748 return 0;
749
Mike Rapoport418d6f82007-10-18 09:23:11 +0200750out:
Ben Dooksa76836f2008-02-05 00:02:02 +0000751 dev_err(db->dev, "not found (%d).\n", ret);
Sascha Hauera1365272005-05-05 15:14:15 -0700752
753 dm9000_release_board(pdev, db);
Ben Dooks9fd9f9b2007-05-07 11:13:25 +0000754 free_netdev(ndev);
Sascha Hauera1365272005-05-05 15:14:15 -0700755
756 return ret;
757}
758
759/*
760 * Open the interface.
761 * The interface is opened whenever "ifconfig" actives it.
762 */
763static int
764dm9000_open(struct net_device *dev)
765{
766 board_info_t *db = (board_info_t *) dev->priv;
Ben Dooks1a5f1c42008-02-05 00:02:04 +0000767 unsigned long irqflags = db->irq_res->flags & IRQF_TRIGGER_MASK;
Sascha Hauera1365272005-05-05 15:14:15 -0700768
Ben Dooksc991d162008-02-05 00:02:16 +0000769 if (netif_msg_ifup(db))
770 dev_dbg(db->dev, "enabling %s\n", dev->name);
Sascha Hauera1365272005-05-05 15:14:15 -0700771
Ben Dooks1a5f1c42008-02-05 00:02:04 +0000772 /* If there is no IRQ type specified, default to something that
773 * may work, and tell the user that this is a problem */
774
775 if (irqflags == IRQF_TRIGGER_NONE) {
776 dev_warn(db->dev, "WARNING: no IRQ resource flags set.\n");
777 irqflags = DEFAULT_TRIGGER;
778 }
Jeff Garzik7d2e3cb2008-05-13 01:41:58 -0400779
Ben Dooks1a5f1c42008-02-05 00:02:04 +0000780 irqflags |= IRQF_SHARED;
781
782 if (request_irq(dev->irq, &dm9000_interrupt, irqflags, dev->name, dev))
Sascha Hauera1365272005-05-05 15:14:15 -0700783 return -EAGAIN;
784
785 /* Initialize DM9000 board */
786 dm9000_reset(db);
787 dm9000_init_dm9000(dev);
788
789 /* Init driver variable */
790 db->dbug_cnt = 0;
791
Sascha Hauera1365272005-05-05 15:14:15 -0700792 mii_check_media(&db->mii, netif_msg_link(db), 1);
793 netif_start_queue(dev);
Ben Dooks8f5bf5f22008-05-08 11:36:42 +0100794
795 dm9000_schedule_poll(db);
Sascha Hauera1365272005-05-05 15:14:15 -0700796
797 return 0;
798}
799
800/*
801 * Initilize dm9000 board
802 */
803static void
804dm9000_init_dm9000(struct net_device *dev)
805{
806 board_info_t *db = (board_info_t *) dev->priv;
Ben Dooks6d406b32008-06-24 22:15:59 +0100807 unsigned int imr;
Sascha Hauera1365272005-05-05 15:14:15 -0700808
Ben Dooks5b2b4ff2008-02-05 00:02:03 +0000809 dm9000_dbg(db, 1, "entering %s\n", __func__);
Sascha Hauera1365272005-05-05 15:14:15 -0700810
811 /* I/O mode */
812 db->io_mode = ior(db, DM9000_ISR) >> 6; /* ISR bit7:6 keeps I/O mode */
813
814 /* GPIO0 on pre-activate PHY */
815 iow(db, DM9000_GPR, 0); /* REG_1F bit0 activate phyxcer */
816 iow(db, DM9000_GPCR, GPCR_GEP_CNTL); /* Let GPIO0 output */
817 iow(db, DM9000_GPR, 0); /* Enable PHY */
818
Ben Dooks33ba5092008-02-05 00:02:01 +0000819 if (db->flags & DM9000_PLATF_EXT_PHY)
820 iow(db, DM9000_NCR, NCR_EXT_PHY);
821
Sascha Hauera1365272005-05-05 15:14:15 -0700822 /* Program operating register */
823 iow(db, DM9000_TCR, 0); /* TX Polling clear */
824 iow(db, DM9000_BPTR, 0x3f); /* Less 3Kb, 200us */
825 iow(db, DM9000_FCR, 0xff); /* Flow Control */
826 iow(db, DM9000_SMCR, 0); /* Special Mode */
827 /* clear TX status */
828 iow(db, DM9000_NSR, NSR_WAKEST | NSR_TX2END | NSR_TX1END);
829 iow(db, DM9000_ISR, ISR_CLR_STATUS); /* Clear interrupt status */
830
831 /* Set address filter table */
832 dm9000_hash_table(dev);
833
Ben Dooks6d406b32008-06-24 22:15:59 +0100834 imr = IMR_PAR | IMR_PTM | IMR_PRM;
835 if (db->type != TYPE_DM9000E)
836 imr |= IMR_LNKCHNG;
837
838 db->imr_all = imr;
839
Sascha Hauera1365272005-05-05 15:14:15 -0700840 /* Enable TX/RX interrupt mask */
Ben Dooks6d406b32008-06-24 22:15:59 +0100841 iow(db, DM9000_IMR, imr);
Sascha Hauera1365272005-05-05 15:14:15 -0700842
843 /* Init Driver variable */
844 db->tx_pkt_cnt = 0;
845 db->queue_pkt_len = 0;
846 dev->trans_start = 0;
Sascha Hauera1365272005-05-05 15:14:15 -0700847}
848
849/*
850 * Hardware start transmission.
851 * Send a packet to media from the upper layer.
852 */
853static int
854dm9000_start_xmit(struct sk_buff *skb, struct net_device *dev)
855{
Florian Westphalc46ac942007-08-21 01:33:42 +0200856 unsigned long flags;
Sascha Hauera1365272005-05-05 15:14:15 -0700857 board_info_t *db = (board_info_t *) dev->priv;
858
Ben Dooks5b2b4ff2008-02-05 00:02:03 +0000859 dm9000_dbg(db, 3, "%s:\n", __func__);
Sascha Hauera1365272005-05-05 15:14:15 -0700860
861 if (db->tx_pkt_cnt > 1)
862 return 1;
863
Florian Westphalc46ac942007-08-21 01:33:42 +0200864 spin_lock_irqsave(&db->lock, flags);
Sascha Hauera1365272005-05-05 15:14:15 -0700865
866 /* Move data to DM9000 TX RAM */
867 writeb(DM9000_MWCMD, db->io_addr);
868
869 (db->outblk)(db->io_data, skb->data, skb->len);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700870 dev->stats.tx_bytes += skb->len;
Sascha Hauera1365272005-05-05 15:14:15 -0700871
Florian Westphalc46ac942007-08-21 01:33:42 +0200872 db->tx_pkt_cnt++;
Sascha Hauera1365272005-05-05 15:14:15 -0700873 /* TX control: First packet immediately send, second packet queue */
Florian Westphalc46ac942007-08-21 01:33:42 +0200874 if (db->tx_pkt_cnt == 1) {
Sascha Hauera1365272005-05-05 15:14:15 -0700875 /* Set TX length to DM9000 */
Ben Dooks073d3f42008-02-05 00:02:19 +0000876 iow(db, DM9000_TXPLL, skb->len);
877 iow(db, DM9000_TXPLH, skb->len >> 8);
Sascha Hauera1365272005-05-05 15:14:15 -0700878
879 /* Issue TX polling command */
880 iow(db, DM9000_TCR, TCR_TXREQ); /* Cleared after TX complete */
881
882 dev->trans_start = jiffies; /* save the time stamp */
Sascha Hauera1365272005-05-05 15:14:15 -0700883 } else {
884 /* Second packet */
Sascha Hauera1365272005-05-05 15:14:15 -0700885 db->queue_pkt_len = skb->len;
Florian Westphalc46ac942007-08-21 01:33:42 +0200886 netif_stop_queue(dev);
Sascha Hauera1365272005-05-05 15:14:15 -0700887 }
888
Florian Westphalc46ac942007-08-21 01:33:42 +0200889 spin_unlock_irqrestore(&db->lock, flags);
890
Sascha Hauera1365272005-05-05 15:14:15 -0700891 /* free this SKB */
892 dev_kfree_skb(skb);
893
Sascha Hauera1365272005-05-05 15:14:15 -0700894 return 0;
895}
896
897static void
898dm9000_shutdown(struct net_device *dev)
899{
900 board_info_t *db = (board_info_t *) dev->priv;
901
902 /* RESET device */
903 dm9000_phy_write(dev, 0, MII_BMCR, BMCR_RESET); /* PHY RESET */
904 iow(db, DM9000_GPR, 0x01); /* Power-Down PHY */
905 iow(db, DM9000_IMR, IMR_PAR); /* Disable all interrupt */
906 iow(db, DM9000_RCR, 0x00); /* Disable RX */
907}
908
909/*
910 * Stop the interface.
911 * The interface is stopped when it is brought.
912 */
913static int
914dm9000_stop(struct net_device *ndev)
915{
916 board_info_t *db = (board_info_t *) ndev->priv;
917
Ben Dooksc991d162008-02-05 00:02:16 +0000918 if (netif_msg_ifdown(db))
919 dev_dbg(db->dev, "shutting down %s\n", ndev->name);
Sascha Hauera1365272005-05-05 15:14:15 -0700920
Andrew Morton5bceeda2008-05-14 16:20:12 -0700921 cancel_delayed_work_sync(&db->phy_poll);
Ben Dooks8f5bf5f22008-05-08 11:36:42 +0100922
Sascha Hauera1365272005-05-05 15:14:15 -0700923 netif_stop_queue(ndev);
924 netif_carrier_off(ndev);
925
926 /* free interrupt */
927 free_irq(ndev->irq, ndev);
928
929 dm9000_shutdown(ndev);
930
931 return 0;
932}
933
934/*
935 * DM9000 interrupt handler
936 * receive the packet to upper layer, free the transmitted packet
937 */
938
Ben Dooks5d22a312006-06-14 00:09:17 +0100939static void
Sascha Hauera1365272005-05-05 15:14:15 -0700940dm9000_tx_done(struct net_device *dev, board_info_t * db)
941{
942 int tx_status = ior(db, DM9000_NSR); /* Got TX status */
943
944 if (tx_status & (NSR_TX2END | NSR_TX1END)) {
945 /* One packet sent complete */
946 db->tx_pkt_cnt--;
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700947 dev->stats.tx_packets++;
Sascha Hauera1365272005-05-05 15:14:15 -0700948
Ben Dooksc991d162008-02-05 00:02:16 +0000949 if (netif_msg_tx_done(db))
950 dev_dbg(db->dev, "tx done, NSR %02x\n", tx_status);
951
Sascha Hauera1365272005-05-05 15:14:15 -0700952 /* Queue packet check & send */
953 if (db->tx_pkt_cnt > 0) {
Ben Dooks073d3f42008-02-05 00:02:19 +0000954 iow(db, DM9000_TXPLL, db->queue_pkt_len);
955 iow(db, DM9000_TXPLH, db->queue_pkt_len >> 8);
Sascha Hauera1365272005-05-05 15:14:15 -0700956 iow(db, DM9000_TCR, TCR_TXREQ);
957 dev->trans_start = jiffies;
958 }
959 netif_wake_queue(dev);
960 }
961}
962
963static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +0100964dm9000_interrupt(int irq, void *dev_id)
Sascha Hauera1365272005-05-05 15:14:15 -0700965{
966 struct net_device *dev = dev_id;
Ben Dooks5b2b4ff2008-02-05 00:02:03 +0000967 board_info_t *db = (board_info_t *) dev->priv;
Sascha Hauera1365272005-05-05 15:14:15 -0700968 int int_status;
969 u8 reg_save;
970
Ben Dooks5b2b4ff2008-02-05 00:02:03 +0000971 dm9000_dbg(db, 3, "entering %s\n", __func__);
Sascha Hauera1365272005-05-05 15:14:15 -0700972
973 /* A real interrupt coming */
Ben Dooks5b2b4ff2008-02-05 00:02:03 +0000974
Sascha Hauera1365272005-05-05 15:14:15 -0700975 spin_lock(&db->lock);
976
977 /* Save previous register address */
978 reg_save = readb(db->io_addr);
979
980 /* Disable all interrupts */
981 iow(db, DM9000_IMR, IMR_PAR);
982
983 /* Got DM9000 interrupt status */
984 int_status = ior(db, DM9000_ISR); /* Got ISR */
985 iow(db, DM9000_ISR, int_status); /* Clear ISR status */
986
Ben Dooksc991d162008-02-05 00:02:16 +0000987 if (netif_msg_intr(db))
988 dev_dbg(db->dev, "interrupt status %02x\n", int_status);
989
Sascha Hauera1365272005-05-05 15:14:15 -0700990 /* Received the coming packet */
991 if (int_status & ISR_PRS)
992 dm9000_rx(dev);
993
994 /* Trnasmit Interrupt check */
995 if (int_status & ISR_PTS)
996 dm9000_tx_done(dev, db);
997
Ben Dooks6d406b32008-06-24 22:15:59 +0100998 if (db->type != TYPE_DM9000E) {
999 if (int_status & ISR_LNKCHNG) {
1000 /* fire a link-change request */
1001 schedule_delayed_work(&db->phy_poll, 1);
1002 }
1003 }
1004
Sascha Hauera1365272005-05-05 15:14:15 -07001005 /* Re-enable interrupt mask */
Ben Dooks6d406b32008-06-24 22:15:59 +01001006 iow(db, DM9000_IMR, db->imr_all);
Sascha Hauera1365272005-05-05 15:14:15 -07001007
1008 /* Restore previous register address */
1009 writeb(reg_save, db->io_addr);
1010
1011 spin_unlock(&db->lock);
1012
1013 return IRQ_HANDLED;
1014}
1015
Sascha Hauera1365272005-05-05 15:14:15 -07001016struct dm9000_rxhdr {
Ben Dooks93116572008-02-05 00:02:00 +00001017 u8 RxPktReady;
1018 u8 RxStatus;
Al Viro8b9fc8a2008-03-29 03:11:08 +00001019 __le16 RxLen;
Sascha Hauera1365272005-05-05 15:14:15 -07001020} __attribute__((__packed__));
1021
1022/*
1023 * Received a packet and pass to upper layer
1024 */
1025static void
1026dm9000_rx(struct net_device *dev)
1027{
1028 board_info_t *db = (board_info_t *) dev->priv;
1029 struct dm9000_rxhdr rxhdr;
1030 struct sk_buff *skb;
1031 u8 rxbyte, *rdptr;
Richard Knutsson6478fac2007-05-01 18:43:27 +02001032 bool GoodPacket;
Sascha Hauera1365272005-05-05 15:14:15 -07001033 int RxLen;
1034
1035 /* Check packet ready or not */
1036 do {
1037 ior(db, DM9000_MRCMDX); /* Dummy read */
1038
1039 /* Get most updated data */
1040 rxbyte = readb(db->io_data);
1041
1042 /* Status check: this byte must be 0 or 1 */
1043 if (rxbyte > DM9000_PKT_RDY) {
Ben Dooksa76836f2008-02-05 00:02:02 +00001044 dev_warn(db->dev, "status check fail: %d\n", rxbyte);
Sascha Hauera1365272005-05-05 15:14:15 -07001045 iow(db, DM9000_RCR, 0x00); /* Stop Device */
1046 iow(db, DM9000_ISR, IMR_PAR); /* Stop INT request */
1047 return;
1048 }
1049
1050 if (rxbyte != DM9000_PKT_RDY)
1051 return;
1052
1053 /* A packet ready now & Get status/length */
Richard Knutsson6478fac2007-05-01 18:43:27 +02001054 GoodPacket = true;
Sascha Hauera1365272005-05-05 15:14:15 -07001055 writeb(DM9000_MRCMD, db->io_addr);
1056
1057 (db->inblk)(db->io_data, &rxhdr, sizeof(rxhdr));
1058
Ben Dooks93116572008-02-05 00:02:00 +00001059 RxLen = le16_to_cpu(rxhdr.RxLen);
Sascha Hauera1365272005-05-05 15:14:15 -07001060
Ben Dooksc991d162008-02-05 00:02:16 +00001061 if (netif_msg_rx_status(db))
1062 dev_dbg(db->dev, "RX: status %02x, length %04x\n",
1063 rxhdr.RxStatus, RxLen);
1064
Sascha Hauera1365272005-05-05 15:14:15 -07001065 /* Packet Status check */
1066 if (RxLen < 0x40) {
Richard Knutsson6478fac2007-05-01 18:43:27 +02001067 GoodPacket = false;
Ben Dooksc991d162008-02-05 00:02:16 +00001068 if (netif_msg_rx_err(db))
1069 dev_dbg(db->dev, "RX: Bad Packet (runt)\n");
Sascha Hauera1365272005-05-05 15:14:15 -07001070 }
1071
1072 if (RxLen > DM9000_PKT_MAX) {
Ben Dooksa76836f2008-02-05 00:02:02 +00001073 dev_dbg(db->dev, "RST: RX Len:%x\n", RxLen);
Sascha Hauera1365272005-05-05 15:14:15 -07001074 }
1075
Ben Dooks93116572008-02-05 00:02:00 +00001076 if (rxhdr.RxStatus & 0xbf) {
Richard Knutsson6478fac2007-05-01 18:43:27 +02001077 GoodPacket = false;
Ben Dooks93116572008-02-05 00:02:00 +00001078 if (rxhdr.RxStatus & 0x01) {
Ben Dooksc991d162008-02-05 00:02:16 +00001079 if (netif_msg_rx_err(db))
1080 dev_dbg(db->dev, "fifo error\n");
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001081 dev->stats.rx_fifo_errors++;
Sascha Hauera1365272005-05-05 15:14:15 -07001082 }
Ben Dooks93116572008-02-05 00:02:00 +00001083 if (rxhdr.RxStatus & 0x02) {
Ben Dooksc991d162008-02-05 00:02:16 +00001084 if (netif_msg_rx_err(db))
1085 dev_dbg(db->dev, "crc error\n");
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001086 dev->stats.rx_crc_errors++;
Sascha Hauera1365272005-05-05 15:14:15 -07001087 }
Ben Dooks93116572008-02-05 00:02:00 +00001088 if (rxhdr.RxStatus & 0x80) {
Ben Dooksc991d162008-02-05 00:02:16 +00001089 if (netif_msg_rx_err(db))
1090 dev_dbg(db->dev, "length error\n");
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001091 dev->stats.rx_length_errors++;
Sascha Hauera1365272005-05-05 15:14:15 -07001092 }
1093 }
1094
1095 /* Move data from DM9000 */
1096 if (GoodPacket
1097 && ((skb = dev_alloc_skb(RxLen + 4)) != NULL)) {
Sascha Hauera1365272005-05-05 15:14:15 -07001098 skb_reserve(skb, 2);
1099 rdptr = (u8 *) skb_put(skb, RxLen - 4);
1100
1101 /* Read received packet from RX SRAM */
1102
1103 (db->inblk)(db->io_data, rdptr, RxLen);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001104 dev->stats.rx_bytes += RxLen;
Sascha Hauera1365272005-05-05 15:14:15 -07001105
1106 /* Pass to upper layer */
1107 skb->protocol = eth_type_trans(skb, dev);
1108 netif_rx(skb);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001109 dev->stats.rx_packets++;
Sascha Hauera1365272005-05-05 15:14:15 -07001110
1111 } else {
1112 /* need to dump the packet's data */
1113
1114 (db->dumpblk)(db->io_data, RxLen);
1115 }
1116 } while (rxbyte == DM9000_PKT_RDY);
1117}
1118
Ben Dooks39c341a2008-02-05 00:02:17 +00001119static unsigned int
1120dm9000_read_locked(board_info_t *db, int reg)
1121{
1122 unsigned long flags;
1123 unsigned int ret;
1124
1125 spin_lock_irqsave(&db->lock, flags);
1126 ret = ior(db, reg);
1127 spin_unlock_irqrestore(&db->lock, flags);
1128
1129 return ret;
1130}
1131
1132static int dm9000_wait_eeprom(board_info_t *db)
1133{
1134 unsigned int status;
1135 int timeout = 8; /* wait max 8msec */
1136
1137 /* The DM9000 data sheets say we should be able to
1138 * poll the ERRE bit in EPCR to wait for the EEPROM
1139 * operation. From testing several chips, this bit
Jeff Garzik7d2e3cb2008-05-13 01:41:58 -04001140 * does not seem to work.
Ben Dooks39c341a2008-02-05 00:02:17 +00001141 *
1142 * We attempt to use the bit, but fall back to the
1143 * timeout (which is why we do not return an error
1144 * on expiry) to say that the EEPROM operation has
1145 * completed.
1146 */
1147
1148 while (1) {
1149 status = dm9000_read_locked(db, DM9000_EPCR);
1150
1151 if ((status & EPCR_ERRE) == 0)
1152 break;
1153
1154 if (timeout-- < 0) {
1155 dev_dbg(db->dev, "timeout waiting EEPROM\n");
1156 break;
1157 }
1158 }
1159
1160 return 0;
1161}
1162
Sascha Hauera1365272005-05-05 15:14:15 -07001163/*
Ben Dooks86c62fa2008-02-05 00:02:09 +00001164 * Read a word data from EEPROM
Sascha Hauera1365272005-05-05 15:14:15 -07001165 */
Ben Dooks86c62fa2008-02-05 00:02:09 +00001166static void
Ben Dooks29d52e52008-02-05 00:02:11 +00001167dm9000_read_eeprom(board_info_t *db, int offset, u8 *to)
Sascha Hauera1365272005-05-05 15:14:15 -07001168{
Ben Dooks621ddcb2008-02-05 00:02:14 +00001169 unsigned long flags;
1170
Ben Dooksbb44fb702008-02-05 00:02:20 +00001171 if (db->flags & DM9000_PLATF_NO_EEPROM) {
1172 to[0] = 0xff;
1173 to[1] = 0xff;
1174 return;
1175 }
1176
Ben Dooks9a2f0372008-02-05 00:02:10 +00001177 mutex_lock(&db->addr_lock);
1178
Ben Dooks621ddcb2008-02-05 00:02:14 +00001179 spin_lock_irqsave(&db->lock, flags);
1180
Sascha Hauera1365272005-05-05 15:14:15 -07001181 iow(db, DM9000_EPAR, offset);
1182 iow(db, DM9000_EPCR, EPCR_ERPRR);
Ben Dooks621ddcb2008-02-05 00:02:14 +00001183
1184 spin_unlock_irqrestore(&db->lock, flags);
1185
Ben Dooks39c341a2008-02-05 00:02:17 +00001186 dm9000_wait_eeprom(db);
1187
1188 /* delay for at-least 150uS */
1189 msleep(1);
Ben Dooks621ddcb2008-02-05 00:02:14 +00001190
1191 spin_lock_irqsave(&db->lock, flags);
1192
Sascha Hauera1365272005-05-05 15:14:15 -07001193 iow(db, DM9000_EPCR, 0x0);
Ben Dooks86c62fa2008-02-05 00:02:09 +00001194
1195 to[0] = ior(db, DM9000_EPDRL);
1196 to[1] = ior(db, DM9000_EPDRH);
Ben Dooks9a2f0372008-02-05 00:02:10 +00001197
Ben Dooks621ddcb2008-02-05 00:02:14 +00001198 spin_unlock_irqrestore(&db->lock, flags);
1199
Ben Dooks9a2f0372008-02-05 00:02:10 +00001200 mutex_unlock(&db->addr_lock);
Sascha Hauera1365272005-05-05 15:14:15 -07001201}
1202
Sascha Hauera1365272005-05-05 15:14:15 -07001203/*
1204 * Write a word data to SROM
1205 */
1206static void
Ben Dooks29d52e52008-02-05 00:02:11 +00001207dm9000_write_eeprom(board_info_t *db, int offset, u8 *data)
Sascha Hauera1365272005-05-05 15:14:15 -07001208{
Ben Dooks621ddcb2008-02-05 00:02:14 +00001209 unsigned long flags;
1210
Ben Dooksbb44fb702008-02-05 00:02:20 +00001211 if (db->flags & DM9000_PLATF_NO_EEPROM)
1212 return;
1213
Ben Dooks9a2f0372008-02-05 00:02:10 +00001214 mutex_lock(&db->addr_lock);
1215
Ben Dooks621ddcb2008-02-05 00:02:14 +00001216 spin_lock_irqsave(&db->lock, flags);
Sascha Hauera1365272005-05-05 15:14:15 -07001217 iow(db, DM9000_EPAR, offset);
Ben Dooks29d52e52008-02-05 00:02:11 +00001218 iow(db, DM9000_EPDRH, data[1]);
1219 iow(db, DM9000_EPDRL, data[0]);
Sascha Hauera1365272005-05-05 15:14:15 -07001220 iow(db, DM9000_EPCR, EPCR_WEP | EPCR_ERPRW);
Ben Dooks621ddcb2008-02-05 00:02:14 +00001221 spin_unlock_irqrestore(&db->lock, flags);
1222
Ben Dooks39c341a2008-02-05 00:02:17 +00001223 dm9000_wait_eeprom(db);
1224
1225 mdelay(1); /* wait at least 150uS to clear */
Ben Dooks621ddcb2008-02-05 00:02:14 +00001226
1227 spin_lock_irqsave(&db->lock, flags);
Sascha Hauera1365272005-05-05 15:14:15 -07001228 iow(db, DM9000_EPCR, 0);
Ben Dooks621ddcb2008-02-05 00:02:14 +00001229 spin_unlock_irqrestore(&db->lock, flags);
Ben Dooks9a2f0372008-02-05 00:02:10 +00001230
1231 mutex_unlock(&db->addr_lock);
Sascha Hauera1365272005-05-05 15:14:15 -07001232}
1233
Sascha Hauera1365272005-05-05 15:14:15 -07001234/*
Sascha Hauera1365272005-05-05 15:14:15 -07001235 * Set DM9000 multicast address
1236 */
1237static void
1238dm9000_hash_table(struct net_device *dev)
1239{
1240 board_info_t *db = (board_info_t *) dev->priv;
1241 struct dev_mc_list *mcptr = dev->mc_list;
1242 int mc_cnt = dev->mc_count;
Ben Dooksd39cb7862008-02-05 00:02:18 +00001243 int i, oft;
Sascha Hauera1365272005-05-05 15:14:15 -07001244 u32 hash_val;
Ben Dooksd39cb7862008-02-05 00:02:18 +00001245 u16 hash_table[4];
Peter Korsgaard23d245b2008-03-18 23:17:16 +01001246 u8 rcr = RCR_DIS_LONG | RCR_DIS_CRC | RCR_RXEN;
Sascha Hauera1365272005-05-05 15:14:15 -07001247 unsigned long flags;
1248
Ben Dooks5b2b4ff2008-02-05 00:02:03 +00001249 dm9000_dbg(db, 1, "entering %s\n", __func__);
Sascha Hauera1365272005-05-05 15:14:15 -07001250
Ben Dooksd39cb7862008-02-05 00:02:18 +00001251 spin_lock_irqsave(&db->lock, flags);
Sascha Hauera1365272005-05-05 15:14:15 -07001252
Ben Dooksd39cb7862008-02-05 00:02:18 +00001253 for (i = 0, oft = DM9000_PAR; i < 6; i++, oft++)
Sascha Hauera1365272005-05-05 15:14:15 -07001254 iow(db, oft, dev->dev_addr[i]);
1255
1256 /* Clear Hash Table */
1257 for (i = 0; i < 4; i++)
1258 hash_table[i] = 0x0;
1259
1260 /* broadcast address */
1261 hash_table[3] = 0x8000;
1262
Peter Korsgaard23d245b2008-03-18 23:17:16 +01001263 if (dev->flags & IFF_PROMISC)
1264 rcr |= RCR_PRMSC;
1265
1266 if (dev->flags & IFF_ALLMULTI)
1267 rcr |= RCR_ALL;
1268
Sascha Hauera1365272005-05-05 15:14:15 -07001269 /* the multicast address in Hash Table : 64 bits */
1270 for (i = 0; i < mc_cnt; i++, mcptr = mcptr->next) {
Ben Dooksd39cb7862008-02-05 00:02:18 +00001271 hash_val = ether_crc_le(6, mcptr->dmi_addr) & 0x3f;
Sascha Hauera1365272005-05-05 15:14:15 -07001272 hash_table[hash_val / 16] |= (u16) 1 << (hash_val % 16);
1273 }
1274
1275 /* Write the hash table to MAC MD table */
Ben Dooksd39cb7862008-02-05 00:02:18 +00001276 for (i = 0, oft = DM9000_MAR; i < 4; i++) {
1277 iow(db, oft++, hash_table[i]);
1278 iow(db, oft++, hash_table[i] >> 8);
Sascha Hauera1365272005-05-05 15:14:15 -07001279 }
1280
Peter Korsgaard23d245b2008-03-18 23:17:16 +01001281 iow(db, DM9000_RCR, rcr);
Ben Dooksd39cb7862008-02-05 00:02:18 +00001282 spin_unlock_irqrestore(&db->lock, flags);
Sascha Hauera1365272005-05-05 15:14:15 -07001283}
1284
1285
1286/*
Ben Dooks321f69a2008-02-05 00:02:08 +00001287 * Sleep, either by using msleep() or if we are suspending, then
1288 * use mdelay() to sleep.
1289 */
1290static void dm9000_msleep(board_info_t *db, unsigned int ms)
1291{
1292 if (db->in_suspend)
1293 mdelay(ms);
1294 else
1295 msleep(ms);
1296}
1297
1298/*
Sascha Hauera1365272005-05-05 15:14:15 -07001299 * Read a word from phyxcer
1300 */
1301static int
1302dm9000_phy_read(struct net_device *dev, int phy_reg_unused, int reg)
1303{
1304 board_info_t *db = (board_info_t *) dev->priv;
1305 unsigned long flags;
Ben Dooks9ef9ac52005-07-23 17:25:18 +01001306 unsigned int reg_save;
Sascha Hauera1365272005-05-05 15:14:15 -07001307 int ret;
1308
Ben Dooks9a2f0372008-02-05 00:02:10 +00001309 mutex_lock(&db->addr_lock);
1310
Sascha Hauera1365272005-05-05 15:14:15 -07001311 spin_lock_irqsave(&db->lock,flags);
Ben Dooks9ef9ac52005-07-23 17:25:18 +01001312
1313 /* Save previous register address */
1314 reg_save = readb(db->io_addr);
1315
Sascha Hauera1365272005-05-05 15:14:15 -07001316 /* Fill the phyxcer register into REG_0C */
1317 iow(db, DM9000_EPAR, DM9000_PHY | reg);
1318
1319 iow(db, DM9000_EPCR, 0xc); /* Issue phyxcer read command */
Ben Dooks89c8b0e2008-02-05 00:02:07 +00001320
1321 writeb(reg_save, db->io_addr);
1322 spin_unlock_irqrestore(&db->lock,flags);
1323
Ben Dooks321f69a2008-02-05 00:02:08 +00001324 dm9000_msleep(db, 1); /* Wait read complete */
Ben Dooks89c8b0e2008-02-05 00:02:07 +00001325
1326 spin_lock_irqsave(&db->lock,flags);
1327 reg_save = readb(db->io_addr);
1328
Sascha Hauera1365272005-05-05 15:14:15 -07001329 iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer read command */
1330
1331 /* The read data keeps on REG_0D & REG_0E */
1332 ret = (ior(db, DM9000_EPDRH) << 8) | ior(db, DM9000_EPDRL);
1333
Ben Dooks9ef9ac52005-07-23 17:25:18 +01001334 /* restore the previous address */
1335 writeb(reg_save, db->io_addr);
Sascha Hauera1365272005-05-05 15:14:15 -07001336 spin_unlock_irqrestore(&db->lock,flags);
1337
Ben Dooks9a2f0372008-02-05 00:02:10 +00001338 mutex_unlock(&db->addr_lock);
Enrico Scholz37d5dca2008-05-08 11:35:13 +01001339
1340 dm9000_dbg(db, 5, "phy_read[%02x] -> %04x\n", reg, ret);
Sascha Hauera1365272005-05-05 15:14:15 -07001341 return ret;
1342}
1343
1344/*
1345 * Write a word to phyxcer
1346 */
1347static void
1348dm9000_phy_write(struct net_device *dev, int phyaddr_unused, int reg, int value)
1349{
1350 board_info_t *db = (board_info_t *) dev->priv;
1351 unsigned long flags;
Ben Dooks9ef9ac52005-07-23 17:25:18 +01001352 unsigned long reg_save;
Sascha Hauera1365272005-05-05 15:14:15 -07001353
Enrico Scholz37d5dca2008-05-08 11:35:13 +01001354 dm9000_dbg(db, 5, "phy_write[%02x] = %04x\n", reg, value);
Ben Dooks9a2f0372008-02-05 00:02:10 +00001355 mutex_lock(&db->addr_lock);
1356
Sascha Hauera1365272005-05-05 15:14:15 -07001357 spin_lock_irqsave(&db->lock,flags);
1358
Ben Dooks9ef9ac52005-07-23 17:25:18 +01001359 /* Save previous register address */
1360 reg_save = readb(db->io_addr);
1361
Sascha Hauera1365272005-05-05 15:14:15 -07001362 /* Fill the phyxcer register into REG_0C */
1363 iow(db, DM9000_EPAR, DM9000_PHY | reg);
1364
1365 /* Fill the written data into REG_0D & REG_0E */
Ben Dooks073d3f42008-02-05 00:02:19 +00001366 iow(db, DM9000_EPDRL, value);
1367 iow(db, DM9000_EPDRH, value >> 8);
Sascha Hauera1365272005-05-05 15:14:15 -07001368
1369 iow(db, DM9000_EPCR, 0xa); /* Issue phyxcer write command */
Ben Dooks89c8b0e2008-02-05 00:02:07 +00001370
1371 writeb(reg_save, db->io_addr);
Ben Dooks9a2f0372008-02-05 00:02:10 +00001372 spin_unlock_irqrestore(&db->lock, flags);
Ben Dooks89c8b0e2008-02-05 00:02:07 +00001373
Ben Dooks321f69a2008-02-05 00:02:08 +00001374 dm9000_msleep(db, 1); /* Wait write complete */
Ben Dooks89c8b0e2008-02-05 00:02:07 +00001375
1376 spin_lock_irqsave(&db->lock,flags);
1377 reg_save = readb(db->io_addr);
1378
Sascha Hauera1365272005-05-05 15:14:15 -07001379 iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer write command */
1380
Ben Dooks9ef9ac52005-07-23 17:25:18 +01001381 /* restore the previous address */
1382 writeb(reg_save, db->io_addr);
1383
Ben Dooks9a2f0372008-02-05 00:02:10 +00001384 spin_unlock_irqrestore(&db->lock, flags);
1385 mutex_unlock(&db->addr_lock);
Sascha Hauera1365272005-05-05 15:14:15 -07001386}
1387
1388static int
Russell King3ae5eae2005-11-09 22:32:44 +00001389dm9000_drv_suspend(struct platform_device *dev, pm_message_t state)
Sascha Hauera1365272005-05-05 15:14:15 -07001390{
Russell King3ae5eae2005-11-09 22:32:44 +00001391 struct net_device *ndev = platform_get_drvdata(dev);
Ben Dooks321f69a2008-02-05 00:02:08 +00001392 board_info_t *db;
Sascha Hauera1365272005-05-05 15:14:15 -07001393
Russell King9480e302005-10-28 09:52:56 -07001394 if (ndev) {
Ben Dooks321f69a2008-02-05 00:02:08 +00001395 db = (board_info_t *) ndev->priv;
1396 db->in_suspend = 1;
1397
Sascha Hauera1365272005-05-05 15:14:15 -07001398 if (netif_running(ndev)) {
1399 netif_device_detach(ndev);
1400 dm9000_shutdown(ndev);
1401 }
1402 }
1403 return 0;
1404}
1405
1406static int
Russell King3ae5eae2005-11-09 22:32:44 +00001407dm9000_drv_resume(struct platform_device *dev)
Sascha Hauera1365272005-05-05 15:14:15 -07001408{
Russell King3ae5eae2005-11-09 22:32:44 +00001409 struct net_device *ndev = platform_get_drvdata(dev);
Sascha Hauera1365272005-05-05 15:14:15 -07001410 board_info_t *db = (board_info_t *) ndev->priv;
1411
Russell King9480e302005-10-28 09:52:56 -07001412 if (ndev) {
Sascha Hauera1365272005-05-05 15:14:15 -07001413
1414 if (netif_running(ndev)) {
1415 dm9000_reset(db);
1416 dm9000_init_dm9000(ndev);
1417
1418 netif_device_attach(ndev);
1419 }
Ben Dooks321f69a2008-02-05 00:02:08 +00001420
1421 db->in_suspend = 0;
Sascha Hauera1365272005-05-05 15:14:15 -07001422 }
1423 return 0;
1424}
1425
Enrico Scholze21fd4f2008-05-08 11:33:03 +01001426static int __devexit
Russell King3ae5eae2005-11-09 22:32:44 +00001427dm9000_drv_remove(struct platform_device *pdev)
Sascha Hauera1365272005-05-05 15:14:15 -07001428{
Russell King3ae5eae2005-11-09 22:32:44 +00001429 struct net_device *ndev = platform_get_drvdata(pdev);
Sascha Hauera1365272005-05-05 15:14:15 -07001430
Russell King3ae5eae2005-11-09 22:32:44 +00001431 platform_set_drvdata(pdev, NULL);
Sascha Hauera1365272005-05-05 15:14:15 -07001432
1433 unregister_netdev(ndev);
1434 dm9000_release_board(pdev, (board_info_t *) ndev->priv);
Ben Dooks9fd9f9b2007-05-07 11:13:25 +00001435 free_netdev(ndev); /* free device structure */
Sascha Hauera1365272005-05-05 15:14:15 -07001436
Ben Dooksa76836f2008-02-05 00:02:02 +00001437 dev_dbg(&pdev->dev, "released and freed device\n");
Sascha Hauera1365272005-05-05 15:14:15 -07001438 return 0;
1439}
1440
Russell King3ae5eae2005-11-09 22:32:44 +00001441static struct platform_driver dm9000_driver = {
Ben Dooks5d22a312006-06-14 00:09:17 +01001442 .driver = {
1443 .name = "dm9000",
1444 .owner = THIS_MODULE,
1445 },
Sascha Hauera1365272005-05-05 15:14:15 -07001446 .probe = dm9000_probe,
Enrico Scholze21fd4f2008-05-08 11:33:03 +01001447 .remove = __devexit_p(dm9000_drv_remove),
Sascha Hauera1365272005-05-05 15:14:15 -07001448 .suspend = dm9000_drv_suspend,
1449 .resume = dm9000_drv_resume,
1450};
1451
1452static int __init
1453dm9000_init(void)
1454{
Ben Dooks7da99852008-02-05 00:02:06 +00001455 printk(KERN_INFO "%s Ethernet Driver, V%s\n", CARDNAME, DRV_VERSION);
Ben Dooks2ae2d772005-07-23 17:29:38 +01001456
Russell King3ae5eae2005-11-09 22:32:44 +00001457 return platform_driver_register(&dm9000_driver); /* search board and register */
Sascha Hauera1365272005-05-05 15:14:15 -07001458}
1459
1460static void __exit
1461dm9000_cleanup(void)
1462{
Russell King3ae5eae2005-11-09 22:32:44 +00001463 platform_driver_unregister(&dm9000_driver);
Sascha Hauera1365272005-05-05 15:14:15 -07001464}
1465
1466module_init(dm9000_init);
1467module_exit(dm9000_cleanup);
1468
1469MODULE_AUTHOR("Sascha Hauer, Ben Dooks");
1470MODULE_DESCRIPTION("Davicom DM9000 network driver");
1471MODULE_LICENSE("GPL");
Kay Sievers72abb462008-04-18 13:50:44 -07001472MODULE_ALIAS("platform:dm9000");