blob: 1d92ddd1ec351e3b0a54b4f6f27163e509892460 [file] [log] [blame]
Sascha Hauera1365272005-05-05 15:14:15 -07001/*
2 * dm9000.c: Version 1.2 03/18/2003
3 *
4 * A Davicom DM9000 ISA NIC fast Ethernet driver for Linux.
5 * Copyright (C) 1997 Sten Wang
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * (C)Copyright 1997-1998 DAVICOM Semiconductor,Inc. All Rights Reserved.
18 *
19 * V0.11 06/20/2001 REG_0A bit3=1, default enable BP with DA match
20 * 06/22/2001 Support DM9801 progrmming
21 * E3: R25 = ((R24 + NF) & 0x00ff) | 0xf000
22 * E4: R25 = ((R24 + NF) & 0x00ff) | 0xc200
23 * R17 = (R17 & 0xfff0) | NF + 3
24 * E5: R25 = ((R24 + NF - 3) & 0x00ff) | 0xc200
25 * R17 = (R17 & 0xfff0) | NF
26 *
27 * v1.00 modify by simon 2001.9.5
28 * change for kernel 2.4.x
29 *
30 * v1.1 11/09/2001 fix force mode bug
31 *
32 * v1.2 03/18/2003 Weilun Huang <weilun_huang@davicom.com.tw>:
33 * Fixed phy reset.
34 * Added tx/rx 32 bit mode.
35 * Cleaned up for kernel merge.
36 *
37 * 03/03/2004 Sascha Hauer <s.hauer@pengutronix.de>
38 * Port to 2.6 kernel
39 *
40 * 24-Sep-2004 Ben Dooks <ben@simtec.co.uk>
41 * Cleanup of code to remove ifdefs
42 * Allowed platform device data to influence access width
43 * Reformatting areas of code
44 *
45 * 17-Mar-2005 Sascha Hauer <s.hauer@pengutronix.de>
46 * * removed 2.4 style module parameters
47 * * removed removed unused stat counter and fixed
48 * net_device_stats
49 * * introduced tx_timeout function
50 * * reworked locking
Ben Dooks9ef9ac52005-07-23 17:25:18 +010051 *
52 * 01-Jul-2005 Ben Dooks <ben@simtec.co.uk>
53 * * fixed spinlock call without pointer
54 * * ensure spinlock is initialised
Sascha Hauera1365272005-05-05 15:14:15 -070055 */
56
57#include <linux/module.h>
58#include <linux/ioport.h>
59#include <linux/netdevice.h>
60#include <linux/etherdevice.h>
61#include <linux/init.h>
62#include <linux/skbuff.h>
63#include <linux/version.h>
64#include <linux/spinlock.h>
65#include <linux/crc32.h>
66#include <linux/mii.h>
67#include <linux/dm9000.h>
68#include <linux/delay.h>
69
70#include <asm/delay.h>
71#include <asm/irq.h>
72#include <asm/io.h>
73
74#include "dm9000.h"
75
76/* Board/System/Debug information/definition ---------------- */
77
78#define DM9000_PHY 0x40 /* PHY address 0x01 */
79
80#define TRUE 1
81#define FALSE 0
82
83#define CARDNAME "dm9000"
84#define PFX CARDNAME ": "
85
86#define DM9000_TIMER_WUT jiffies+(HZ*2) /* timer wakeup time : 2 second */
87
88#define DM9000_DEBUG 0
89
90#if DM9000_DEBUG > 2
91#define PRINTK3(args...) printk(CARDNAME ": " args)
92#else
93#define PRINTK3(args...) do { } while(0)
94#endif
95
96#if DM9000_DEBUG > 1
97#define PRINTK2(args...) printk(CARDNAME ": " args)
98#else
99#define PRINTK2(args...) do { } while(0)
100#endif
101
102#if DM9000_DEBUG > 0
103#define PRINTK1(args...) printk(CARDNAME ": " args)
104#define PRINTK(args...) printk(CARDNAME ": " args)
105#else
106#define PRINTK1(args...) do { } while(0)
107#define PRINTK(args...) printk(KERN_DEBUG args)
108#endif
109
110/*
111 * Transmit timeout, default 5 seconds.
112 */
113static int watchdog = 5000;
114module_param(watchdog, int, 0400);
115MODULE_PARM_DESC(watchdog, "transmit timeout in milliseconds");
116
117/* Structure/enum declaration ------------------------------- */
118typedef struct board_info {
119
120 void __iomem *io_addr; /* Register I/O base address */
121 void __iomem *io_data; /* Data I/O address */
122 u16 irq; /* IRQ */
123
124 u16 tx_pkt_cnt;
125 u16 queue_pkt_len;
126 u16 queue_start_addr;
127 u16 dbug_cnt;
128 u8 io_mode; /* 0:word, 2:byte */
129 u8 phy_addr;
130
131 void (*inblk)(void __iomem *port, void *data, int length);
132 void (*outblk)(void __iomem *port, void *data, int length);
133 void (*dumpblk)(void __iomem *port, int length);
134
135 struct resource *addr_res; /* resources found */
136 struct resource *data_res;
137 struct resource *addr_req; /* resources requested */
138 struct resource *data_req;
139 struct resource *irq_res;
140
141 struct timer_list timer;
142 struct net_device_stats stats;
143 unsigned char srom[128];
144 spinlock_t lock;
145
146 struct mii_if_info mii;
147 u32 msg_enable;
148} board_info_t;
149
150/* function declaration ------------------------------------- */
151static int dm9000_probe(struct device *);
152static int dm9000_open(struct net_device *);
153static int dm9000_start_xmit(struct sk_buff *, struct net_device *);
154static int dm9000_stop(struct net_device *);
155static int dm9000_do_ioctl(struct net_device *, struct ifreq *, int);
156
157
158static void dm9000_timer(unsigned long);
159static void dm9000_init_dm9000(struct net_device *);
160
161static struct net_device_stats *dm9000_get_stats(struct net_device *);
162
163static irqreturn_t dm9000_interrupt(int, void *, struct pt_regs *);
164
165static int dm9000_phy_read(struct net_device *dev, int phyaddr_unsused, int reg);
166static void dm9000_phy_write(struct net_device *dev, int phyaddr_unused, int reg,
167 int value);
168static u16 read_srom_word(board_info_t *, int);
169static void dm9000_rx(struct net_device *);
170static void dm9000_hash_table(struct net_device *);
171
172//#define DM9000_PROGRAM_EEPROM
173#ifdef DM9000_PROGRAM_EEPROM
174static void program_eeprom(board_info_t * db);
175#endif
176/* DM9000 network board routine ---------------------------- */
177
178static void
179dm9000_reset(board_info_t * db)
180{
181 PRINTK1("dm9000x: resetting\n");
182 /* 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
297 case 2:
298 db->dumpblk = dm9000_dumpblk_16bit;
299 db->outblk = dm9000_outblk_16bit;
300 db->inblk = dm9000_inblk_16bit;
301 break;
302
303 case 3:
304 printk(KERN_ERR PFX ": 3 byte IO, falling back to 16bit\n");
305 db->dumpblk = dm9000_dumpblk_16bit;
306 db->outblk = dm9000_outblk_16bit;
307 db->inblk = dm9000_inblk_16bit;
308 break;
309
310 case 4:
311 default:
312 db->dumpblk = dm9000_dumpblk_32bit;
313 db->outblk = dm9000_outblk_32bit;
314 db->inblk = dm9000_inblk_32bit;
315 break;
316 }
317}
318
319
320/* Our watchdog timed out. Called by the networking layer */
321static void dm9000_timeout(struct net_device *dev)
322{
323 board_info_t *db = (board_info_t *) dev->priv;
324 u8 reg_save;
325 unsigned long flags;
326
327 /* Save previous register address */
328 reg_save = readb(db->io_addr);
Ben Dooks9ef9ac52005-07-23 17:25:18 +0100329 spin_lock_irqsave(&db->lock,flags);
Sascha Hauera1365272005-05-05 15:14:15 -0700330
331 netif_stop_queue(dev);
332 dm9000_reset(db);
333 dm9000_init_dm9000(dev);
334 /* We can accept TX packets again */
335 dev->trans_start = jiffies;
336 netif_wake_queue(dev);
337
338 /* Restore previous register address */
339 writeb(reg_save, db->io_addr);
Ben Dooks9ef9ac52005-07-23 17:25:18 +0100340 spin_unlock_irqrestore(&db->lock,flags);
Sascha Hauera1365272005-05-05 15:14:15 -0700341}
342
343
344/* dm9000_release_board
345 *
346 * release a board, and any mapped resources
347 */
348
349static void
350dm9000_release_board(struct platform_device *pdev, struct board_info *db)
351{
352 if (db->data_res == NULL) {
353 if (db->addr_res != NULL)
354 release_mem_region((unsigned long)db->io_addr, 4);
355 return;
356 }
357
358 /* unmap our resources */
359
360 iounmap(db->io_addr);
361 iounmap(db->io_data);
362
363 /* release the resources */
364
365 if (db->data_req != NULL) {
366 release_resource(db->data_req);
367 kfree(db->data_req);
368 }
369
370 if (db->addr_res != NULL) {
Sascha Hauer5f6b5512005-06-20 15:32:51 -0700371 release_resource(db->addr_res);
Sascha Hauera1365272005-05-05 15:14:15 -0700372 kfree(db->addr_req);
373 }
374}
375
376#define res_size(_r) (((_r)->end - (_r)->start) + 1)
377
378/*
379 * Search DM9000 board, allocate space and register it
380 */
381static int
382dm9000_probe(struct device *dev)
383{
384 struct platform_device *pdev = to_platform_device(dev);
385 struct dm9000_plat_data *pdata = pdev->dev.platform_data;
386 struct board_info *db; /* Point a board information structure */
387 struct net_device *ndev;
388 unsigned long base;
389 int ret = 0;
390 int iosize;
391 int i;
392 u32 id_val;
393
394 printk(KERN_INFO "%s Ethernet Driver\n", CARDNAME);
395
396 /* Init network device */
397 ndev = alloc_etherdev(sizeof (struct board_info));
398 if (!ndev) {
399 printk("%s: could not allocate device.\n", CARDNAME);
400 return -ENOMEM;
401 }
402
403 SET_MODULE_OWNER(ndev);
404 SET_NETDEV_DEV(ndev, dev);
405
406 PRINTK2("dm9000_probe()");
407
408 /* setup board info structure */
409 db = (struct board_info *) ndev->priv;
410 memset(db, 0, sizeof (*db));
411
Ben Dooks9ef9ac52005-07-23 17:25:18 +0100412 spin_lock_init(&db->lock);
413
Sascha Hauera1365272005-05-05 15:14:15 -0700414 if (pdev->num_resources < 2) {
415 ret = -ENODEV;
416 goto out;
417 }
418
419 switch (pdev->num_resources) {
420 case 2:
421 base = pdev->resource[0].start;
422
423 if (!request_mem_region(base, 4, ndev->name)) {
424 ret = -EBUSY;
425 goto out;
426 }
427
428 ndev->base_addr = base;
429 ndev->irq = pdev->resource[1].start;
430 db->io_addr = (void *)base;
431 db->io_data = (void *)(base + 4);
432
433 break;
434
435 case 3:
436 db->addr_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
437 db->data_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
438 db->irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
439
440 if (db->addr_res == NULL || db->data_res == NULL) {
441 printk(KERN_ERR PFX "insufficient resources\n");
442 ret = -ENOENT;
443 goto out;
444 }
445
446 i = res_size(db->addr_res);
447 db->addr_req = request_mem_region(db->addr_res->start, i,
448 pdev->name);
449
450 if (db->addr_req == NULL) {
451 printk(KERN_ERR PFX "cannot claim address reg area\n");
452 ret = -EIO;
453 goto out;
454 }
455
456 db->io_addr = ioremap(db->addr_res->start, i);
457
458 if (db->io_addr == NULL) {
459 printk(KERN_ERR "failed to ioremap address reg\n");
460 ret = -EINVAL;
461 goto out;
462 }
463
464 iosize = res_size(db->data_res);
465 db->data_req = request_mem_region(db->data_res->start, iosize,
466 pdev->name);
467
468 if (db->data_req == NULL) {
469 printk(KERN_ERR PFX "cannot claim data reg area\n");
470 ret = -EIO;
471 goto out;
472 }
473
474 db->io_data = ioremap(db->data_res->start, iosize);
475
476 if (db->io_data == NULL) {
477 printk(KERN_ERR "failed to ioremap data reg\n");
478 ret = -EINVAL;
479 goto out;
480 }
481
482 /* fill in parameters for net-dev structure */
483
484 ndev->base_addr = (unsigned long)db->io_addr;
485 ndev->irq = db->irq_res->start;
486
487 /* ensure at least we have a default set of IO routines */
488 dm9000_set_io(db, iosize);
489
490 }
491
492 /* check to see if anything is being over-ridden */
493 if (pdata != NULL) {
494 /* check to see if the driver wants to over-ride the
495 * default IO width */
496
497 if (pdata->flags & DM9000_PLATF_8BITONLY)
498 dm9000_set_io(db, 1);
499
500 if (pdata->flags & DM9000_PLATF_16BITONLY)
501 dm9000_set_io(db, 2);
502
503 if (pdata->flags & DM9000_PLATF_32BITONLY)
504 dm9000_set_io(db, 4);
505
506 /* check to see if there are any IO routine
507 * over-rides */
508
509 if (pdata->inblk != NULL)
510 db->inblk = pdata->inblk;
511
512 if (pdata->outblk != NULL)
513 db->outblk = pdata->outblk;
514
515 if (pdata->dumpblk != NULL)
516 db->dumpblk = pdata->dumpblk;
517 }
518
519 dm9000_reset(db);
520
521 /* try two times, DM9000 sometimes gets the first read wrong */
522 for (i = 0; i < 2; i++) {
523 id_val = ior(db, DM9000_VIDL);
524 id_val |= (u32)ior(db, DM9000_VIDH) << 8;
525 id_val |= (u32)ior(db, DM9000_PIDL) << 16;
526 id_val |= (u32)ior(db, DM9000_PIDH) << 24;
527
528 if (id_val == DM9000_ID)
529 break;
530 printk("%s: read wrong id 0x%08x\n", CARDNAME, id_val);
531 }
532
533 if (id_val != DM9000_ID) {
534 printk("%s: wrong id: 0x%08x\n", CARDNAME, id_val);
535 goto release;
536 }
537
538 /* from this point we assume that we have found a DM9000 */
539
540 /* driver system function */
541 ether_setup(ndev);
542
543 ndev->open = &dm9000_open;
544 ndev->hard_start_xmit = &dm9000_start_xmit;
545 ndev->tx_timeout = &dm9000_timeout;
546 ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
547 ndev->stop = &dm9000_stop;
548 ndev->get_stats = &dm9000_get_stats;
549 ndev->set_multicast_list = &dm9000_hash_table;
550 ndev->do_ioctl = &dm9000_do_ioctl;
551
552#ifdef DM9000_PROGRAM_EEPROM
553 program_eeprom(db);
554#endif
555 db->msg_enable = NETIF_MSG_LINK;
556 db->mii.phy_id_mask = 0x1f;
557 db->mii.reg_num_mask = 0x1f;
558 db->mii.force_media = 0;
559 db->mii.full_duplex = 0;
560 db->mii.dev = ndev;
561 db->mii.mdio_read = dm9000_phy_read;
562 db->mii.mdio_write = dm9000_phy_write;
563
564 /* Read SROM content */
565 for (i = 0; i < 64; i++)
566 ((u16 *) db->srom)[i] = read_srom_word(db, i);
567
568 /* Set Node Address */
569 for (i = 0; i < 6; i++)
570 ndev->dev_addr[i] = db->srom[i];
571
572 if (!is_valid_ether_addr(ndev->dev_addr))
573 printk("%s: Invalid ethernet MAC address. Please "
574 "set using ifconfig\n", ndev->name);
575
576 dev_set_drvdata(dev, ndev);
577 ret = register_netdev(ndev);
578
579 if (ret == 0) {
580 printk("%s: dm9000 at %p,%p IRQ %d MAC: ",
581 ndev->name, db->io_addr, db->io_data, ndev->irq);
582 for (i = 0; i < 5; i++)
583 printk("%02x:", ndev->dev_addr[i]);
584 printk("%02x\n", ndev->dev_addr[5]);
585 }
586 return 0;
587
588 release:
589 out:
590 printk("%s: not found (%d).\n", CARDNAME, ret);
591
592 dm9000_release_board(pdev, db);
593 kfree(ndev);
594
595 return ret;
596}
597
598/*
599 * Open the interface.
600 * The interface is opened whenever "ifconfig" actives it.
601 */
602static int
603dm9000_open(struct net_device *dev)
604{
605 board_info_t *db = (board_info_t *) dev->priv;
606
607 PRINTK2("entering dm9000_open\n");
608
609 if (request_irq(dev->irq, &dm9000_interrupt, SA_SHIRQ, dev->name, dev))
610 return -EAGAIN;
611
612 /* Initialize DM9000 board */
613 dm9000_reset(db);
614 dm9000_init_dm9000(dev);
615
616 /* Init driver variable */
617 db->dbug_cnt = 0;
618
619 /* set and active a timer process */
620 init_timer(&db->timer);
Ben Dooks9ef9ac52005-07-23 17:25:18 +0100621 db->timer.expires = DM9000_TIMER_WUT;
Sascha Hauera1365272005-05-05 15:14:15 -0700622 db->timer.data = (unsigned long) dev;
623 db->timer.function = &dm9000_timer;
624 add_timer(&db->timer);
625
626 mii_check_media(&db->mii, netif_msg_link(db), 1);
627 netif_start_queue(dev);
628
629 return 0;
630}
631
632/*
633 * Initilize dm9000 board
634 */
635static void
636dm9000_init_dm9000(struct net_device *dev)
637{
638 board_info_t *db = (board_info_t *) dev->priv;
639
640 PRINTK1("entering %s\n",__FUNCTION__);
641
642 /* I/O mode */
643 db->io_mode = ior(db, DM9000_ISR) >> 6; /* ISR bit7:6 keeps I/O mode */
644
645 /* GPIO0 on pre-activate PHY */
646 iow(db, DM9000_GPR, 0); /* REG_1F bit0 activate phyxcer */
647 iow(db, DM9000_GPCR, GPCR_GEP_CNTL); /* Let GPIO0 output */
648 iow(db, DM9000_GPR, 0); /* Enable PHY */
649
650 /* Program operating register */
651 iow(db, DM9000_TCR, 0); /* TX Polling clear */
652 iow(db, DM9000_BPTR, 0x3f); /* Less 3Kb, 200us */
653 iow(db, DM9000_FCR, 0xff); /* Flow Control */
654 iow(db, DM9000_SMCR, 0); /* Special Mode */
655 /* clear TX status */
656 iow(db, DM9000_NSR, NSR_WAKEST | NSR_TX2END | NSR_TX1END);
657 iow(db, DM9000_ISR, ISR_CLR_STATUS); /* Clear interrupt status */
658
659 /* Set address filter table */
660 dm9000_hash_table(dev);
661
662 /* Activate DM9000 */
663 iow(db, DM9000_RCR, RCR_DIS_LONG | RCR_DIS_CRC | RCR_RXEN);
664 /* Enable TX/RX interrupt mask */
665 iow(db, DM9000_IMR, IMR_PAR | IMR_PTM | IMR_PRM);
666
667 /* Init Driver variable */
668 db->tx_pkt_cnt = 0;
669 db->queue_pkt_len = 0;
670 dev->trans_start = 0;
671 spin_lock_init(&db->lock);
672}
673
674/*
675 * Hardware start transmission.
676 * Send a packet to media from the upper layer.
677 */
678static int
679dm9000_start_xmit(struct sk_buff *skb, struct net_device *dev)
680{
681 board_info_t *db = (board_info_t *) dev->priv;
682
683 PRINTK3("dm9000_start_xmit\n");
684
685 if (db->tx_pkt_cnt > 1)
686 return 1;
687
688 netif_stop_queue(dev);
689
690 /* Disable all interrupts */
691 iow(db, DM9000_IMR, IMR_PAR);
692
693 /* Move data to DM9000 TX RAM */
694 writeb(DM9000_MWCMD, db->io_addr);
695
696 (db->outblk)(db->io_data, skb->data, skb->len);
697 db->stats.tx_bytes += skb->len;
698
699 /* TX control: First packet immediately send, second packet queue */
700 if (db->tx_pkt_cnt == 0) {
701
702 /* First Packet */
703 db->tx_pkt_cnt++;
704
705 /* Set TX length to DM9000 */
706 iow(db, DM9000_TXPLL, skb->len & 0xff);
707 iow(db, DM9000_TXPLH, (skb->len >> 8) & 0xff);
708
709 /* Issue TX polling command */
710 iow(db, DM9000_TCR, TCR_TXREQ); /* Cleared after TX complete */
711
712 dev->trans_start = jiffies; /* save the time stamp */
713
714 } else {
715 /* Second packet */
716 db->tx_pkt_cnt++;
717 db->queue_pkt_len = skb->len;
718 }
719
720 /* free this SKB */
721 dev_kfree_skb(skb);
722
723 /* Re-enable resource check */
724 if (db->tx_pkt_cnt == 1)
725 netif_wake_queue(dev);
726
727 /* Re-enable interrupt */
728 iow(db, DM9000_IMR, IMR_PAR | IMR_PTM | IMR_PRM);
729
730 return 0;
731}
732
733static void
734dm9000_shutdown(struct net_device *dev)
735{
736 board_info_t *db = (board_info_t *) dev->priv;
737
738 /* RESET device */
739 dm9000_phy_write(dev, 0, MII_BMCR, BMCR_RESET); /* PHY RESET */
740 iow(db, DM9000_GPR, 0x01); /* Power-Down PHY */
741 iow(db, DM9000_IMR, IMR_PAR); /* Disable all interrupt */
742 iow(db, DM9000_RCR, 0x00); /* Disable RX */
743}
744
745/*
746 * Stop the interface.
747 * The interface is stopped when it is brought.
748 */
749static int
750dm9000_stop(struct net_device *ndev)
751{
752 board_info_t *db = (board_info_t *) ndev->priv;
753
754 PRINTK1("entering %s\n",__FUNCTION__);
755
756 /* deleted timer */
757 del_timer(&db->timer);
758
759 netif_stop_queue(ndev);
760 netif_carrier_off(ndev);
761
762 /* free interrupt */
763 free_irq(ndev->irq, ndev);
764
765 dm9000_shutdown(ndev);
766
767 return 0;
768}
769
770/*
771 * DM9000 interrupt handler
772 * receive the packet to upper layer, free the transmitted packet
773 */
774
775void
776dm9000_tx_done(struct net_device *dev, board_info_t * db)
777{
778 int tx_status = ior(db, DM9000_NSR); /* Got TX status */
779
780 if (tx_status & (NSR_TX2END | NSR_TX1END)) {
781 /* One packet sent complete */
782 db->tx_pkt_cnt--;
783 db->stats.tx_packets++;
784
785 /* Queue packet check & send */
786 if (db->tx_pkt_cnt > 0) {
787 iow(db, DM9000_TXPLL, db->queue_pkt_len & 0xff);
788 iow(db, DM9000_TXPLH, (db->queue_pkt_len >> 8) & 0xff);
789 iow(db, DM9000_TCR, TCR_TXREQ);
790 dev->trans_start = jiffies;
791 }
792 netif_wake_queue(dev);
793 }
794}
795
796static irqreturn_t
797dm9000_interrupt(int irq, void *dev_id, struct pt_regs *regs)
798{
799 struct net_device *dev = dev_id;
800 board_info_t *db;
801 int int_status;
802 u8 reg_save;
803
804 PRINTK3("entering %s\n",__FUNCTION__);
805
806 if (!dev) {
807 PRINTK1("dm9000_interrupt() without DEVICE arg\n");
808 return IRQ_HANDLED;
809 }
810
811 /* A real interrupt coming */
812 db = (board_info_t *) dev->priv;
813 spin_lock(&db->lock);
814
815 /* Save previous register address */
816 reg_save = readb(db->io_addr);
817
818 /* Disable all interrupts */
819 iow(db, DM9000_IMR, IMR_PAR);
820
821 /* Got DM9000 interrupt status */
822 int_status = ior(db, DM9000_ISR); /* Got ISR */
823 iow(db, DM9000_ISR, int_status); /* Clear ISR status */
824
825 /* Received the coming packet */
826 if (int_status & ISR_PRS)
827 dm9000_rx(dev);
828
829 /* Trnasmit Interrupt check */
830 if (int_status & ISR_PTS)
831 dm9000_tx_done(dev, db);
832
833 /* Re-enable interrupt mask */
834 iow(db, DM9000_IMR, IMR_PAR | IMR_PTM | IMR_PRM);
835
836 /* Restore previous register address */
837 writeb(reg_save, db->io_addr);
838
839 spin_unlock(&db->lock);
840
841 return IRQ_HANDLED;
842}
843
844/*
845 * Get statistics from driver.
846 */
847static struct net_device_stats *
848dm9000_get_stats(struct net_device *dev)
849{
850 board_info_t *db = (board_info_t *) dev->priv;
851 return &db->stats;
852}
853
854/*
855 * Process the upper socket ioctl command
856 */
857static int
858dm9000_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
859{
860 PRINTK1("entering %s\n",__FUNCTION__);
861 return 0;
862}
863
864/*
865 * A periodic timer routine
866 * Dynamic media sense, allocated Rx buffer...
867 */
868static void
869dm9000_timer(unsigned long data)
870{
871 struct net_device *dev = (struct net_device *) data;
872 board_info_t *db = (board_info_t *) dev->priv;
Sascha Hauera1365272005-05-05 15:14:15 -0700873
874 PRINTK3("dm9000_timer()\n");
875
Sascha Hauera1365272005-05-05 15:14:15 -0700876 mii_check_media(&db->mii, netif_msg_link(db), 0);
877
Sascha Hauera1365272005-05-05 15:14:15 -0700878 /* Set timer again */
879 db->timer.expires = DM9000_TIMER_WUT;
880 add_timer(&db->timer);
881}
882
883struct dm9000_rxhdr {
884 u16 RxStatus;
885 u16 RxLen;
886} __attribute__((__packed__));
887
888/*
889 * Received a packet and pass to upper layer
890 */
891static void
892dm9000_rx(struct net_device *dev)
893{
894 board_info_t *db = (board_info_t *) dev->priv;
895 struct dm9000_rxhdr rxhdr;
896 struct sk_buff *skb;
897 u8 rxbyte, *rdptr;
898 int GoodPacket;
899 int RxLen;
900
901 /* Check packet ready or not */
902 do {
903 ior(db, DM9000_MRCMDX); /* Dummy read */
904
905 /* Get most updated data */
906 rxbyte = readb(db->io_data);
907
908 /* Status check: this byte must be 0 or 1 */
909 if (rxbyte > DM9000_PKT_RDY) {
910 printk("status check failed: %d\n", rxbyte);
911 iow(db, DM9000_RCR, 0x00); /* Stop Device */
912 iow(db, DM9000_ISR, IMR_PAR); /* Stop INT request */
913 return;
914 }
915
916 if (rxbyte != DM9000_PKT_RDY)
917 return;
918
919 /* A packet ready now & Get status/length */
920 GoodPacket = TRUE;
921 writeb(DM9000_MRCMD, db->io_addr);
922
923 (db->inblk)(db->io_data, &rxhdr, sizeof(rxhdr));
924
925 RxLen = rxhdr.RxLen;
926
927 /* Packet Status check */
928 if (RxLen < 0x40) {
929 GoodPacket = FALSE;
930 PRINTK1("Bad Packet received (runt)\n");
931 }
932
933 if (RxLen > DM9000_PKT_MAX) {
934 PRINTK1("RST: RX Len:%x\n", RxLen);
935 }
936
937 if (rxhdr.RxStatus & 0xbf00) {
938 GoodPacket = FALSE;
939 if (rxhdr.RxStatus & 0x100) {
940 PRINTK1("fifo error\n");
941 db->stats.rx_fifo_errors++;
942 }
943 if (rxhdr.RxStatus & 0x200) {
944 PRINTK1("crc error\n");
945 db->stats.rx_crc_errors++;
946 }
947 if (rxhdr.RxStatus & 0x8000) {
948 PRINTK1("length error\n");
949 db->stats.rx_length_errors++;
950 }
951 }
952
953 /* Move data from DM9000 */
954 if (GoodPacket
955 && ((skb = dev_alloc_skb(RxLen + 4)) != NULL)) {
956 skb->dev = dev;
957 skb_reserve(skb, 2);
958 rdptr = (u8 *) skb_put(skb, RxLen - 4);
959
960 /* Read received packet from RX SRAM */
961
962 (db->inblk)(db->io_data, rdptr, RxLen);
963 db->stats.rx_bytes += RxLen;
964
965 /* Pass to upper layer */
966 skb->protocol = eth_type_trans(skb, dev);
967 netif_rx(skb);
968 db->stats.rx_packets++;
969
970 } else {
971 /* need to dump the packet's data */
972
973 (db->dumpblk)(db->io_data, RxLen);
974 }
975 } while (rxbyte == DM9000_PKT_RDY);
976}
977
978/*
979 * Read a word data from SROM
980 */
981static u16
982read_srom_word(board_info_t * db, int offset)
983{
984 iow(db, DM9000_EPAR, offset);
985 iow(db, DM9000_EPCR, EPCR_ERPRR);
986 mdelay(8); /* according to the datasheet 200us should be enough,
987 but it doesn't work */
988 iow(db, DM9000_EPCR, 0x0);
989 return (ior(db, DM9000_EPDRL) + (ior(db, DM9000_EPDRH) << 8));
990}
991
992#ifdef DM9000_PROGRAM_EEPROM
993/*
994 * Write a word data to SROM
995 */
996static void
997write_srom_word(board_info_t * db, int offset, u16 val)
998{
999 iow(db, DM9000_EPAR, offset);
1000 iow(db, DM9000_EPDRH, ((val >> 8) & 0xff));
1001 iow(db, DM9000_EPDRL, (val & 0xff));
1002 iow(db, DM9000_EPCR, EPCR_WEP | EPCR_ERPRW);
1003 mdelay(8); /* same shit */
1004 iow(db, DM9000_EPCR, 0);
1005}
1006
1007/*
1008 * Only for development:
1009 * Here we write static data to the eeprom in case
1010 * we don't have valid content on a new board
1011 */
1012static void
1013program_eeprom(board_info_t * db)
1014{
1015 u16 eeprom[] = { 0x0c00, 0x007f, 0x1300, /* MAC Address */
1016 0x0000, /* Autoload: accept nothing */
1017 0x0a46, 0x9000, /* Vendor / Product ID */
1018 0x0000, /* pin control */
1019 0x0000,
1020 }; /* Wake-up mode control */
1021 int i;
1022 for (i = 0; i < 8; i++)
1023 write_srom_word(db, i, eeprom[i]);
1024}
1025#endif
1026
1027
1028/*
1029 * Calculate the CRC valude of the Rx packet
1030 * flag = 1 : return the reverse CRC (for the received packet CRC)
1031 * 0 : return the normal CRC (for Hash Table index)
1032 */
1033
1034static unsigned long
1035cal_CRC(unsigned char *Data, unsigned int Len, u8 flag)
1036{
1037
1038 u32 crc = ether_crc_le(Len, Data);
1039
1040 if (flag)
1041 return ~crc;
1042
1043 return crc;
1044}
1045
1046/*
1047 * Set DM9000 multicast address
1048 */
1049static void
1050dm9000_hash_table(struct net_device *dev)
1051{
1052 board_info_t *db = (board_info_t *) dev->priv;
1053 struct dev_mc_list *mcptr = dev->mc_list;
1054 int mc_cnt = dev->mc_count;
1055 u32 hash_val;
1056 u16 i, oft, hash_table[4];
1057 unsigned long flags;
1058
1059 PRINTK2("dm9000_hash_table()\n");
1060
1061 spin_lock_irqsave(&db->lock,flags);
1062
1063 for (i = 0, oft = 0x10; i < 6; i++, oft++)
1064 iow(db, oft, dev->dev_addr[i]);
1065
1066 /* Clear Hash Table */
1067 for (i = 0; i < 4; i++)
1068 hash_table[i] = 0x0;
1069
1070 /* broadcast address */
1071 hash_table[3] = 0x8000;
1072
1073 /* the multicast address in Hash Table : 64 bits */
1074 for (i = 0; i < mc_cnt; i++, mcptr = mcptr->next) {
1075 hash_val = cal_CRC((char *) mcptr->dmi_addr, 6, 0) & 0x3f;
1076 hash_table[hash_val / 16] |= (u16) 1 << (hash_val % 16);
1077 }
1078
1079 /* Write the hash table to MAC MD table */
1080 for (i = 0, oft = 0x16; i < 4; i++) {
1081 iow(db, oft++, hash_table[i] & 0xff);
1082 iow(db, oft++, (hash_table[i] >> 8) & 0xff);
1083 }
1084
1085 spin_unlock_irqrestore(&db->lock,flags);
1086}
1087
1088
1089/*
1090 * Read a word from phyxcer
1091 */
1092static int
1093dm9000_phy_read(struct net_device *dev, int phy_reg_unused, int reg)
1094{
1095 board_info_t *db = (board_info_t *) dev->priv;
1096 unsigned long flags;
Ben Dooks9ef9ac52005-07-23 17:25:18 +01001097 unsigned int reg_save;
Sascha Hauera1365272005-05-05 15:14:15 -07001098 int ret;
1099
1100 spin_lock_irqsave(&db->lock,flags);
Ben Dooks9ef9ac52005-07-23 17:25:18 +01001101
1102 /* Save previous register address */
1103 reg_save = readb(db->io_addr);
1104
Sascha Hauera1365272005-05-05 15:14:15 -07001105 /* Fill the phyxcer register into REG_0C */
1106 iow(db, DM9000_EPAR, DM9000_PHY | reg);
1107
1108 iow(db, DM9000_EPCR, 0xc); /* Issue phyxcer read command */
1109 udelay(100); /* Wait read complete */
1110 iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer read command */
1111
1112 /* The read data keeps on REG_0D & REG_0E */
1113 ret = (ior(db, DM9000_EPDRH) << 8) | ior(db, DM9000_EPDRL);
1114
Ben Dooks9ef9ac52005-07-23 17:25:18 +01001115 /* restore the previous address */
1116 writeb(reg_save, db->io_addr);
1117
Sascha Hauera1365272005-05-05 15:14:15 -07001118 spin_unlock_irqrestore(&db->lock,flags);
1119
1120 return ret;
1121}
1122
1123/*
1124 * Write a word to phyxcer
1125 */
1126static void
1127dm9000_phy_write(struct net_device *dev, int phyaddr_unused, int reg, int value)
1128{
1129 board_info_t *db = (board_info_t *) dev->priv;
1130 unsigned long flags;
Ben Dooks9ef9ac52005-07-23 17:25:18 +01001131 unsigned long reg_save;
Sascha Hauera1365272005-05-05 15:14:15 -07001132
1133 spin_lock_irqsave(&db->lock,flags);
1134
Ben Dooks9ef9ac52005-07-23 17:25:18 +01001135 /* Save previous register address */
1136 reg_save = readb(db->io_addr);
1137
Sascha Hauera1365272005-05-05 15:14:15 -07001138 /* Fill the phyxcer register into REG_0C */
1139 iow(db, DM9000_EPAR, DM9000_PHY | reg);
1140
1141 /* Fill the written data into REG_0D & REG_0E */
1142 iow(db, DM9000_EPDRL, (value & 0xff));
1143 iow(db, DM9000_EPDRH, ((value >> 8) & 0xff));
1144
1145 iow(db, DM9000_EPCR, 0xa); /* Issue phyxcer write command */
1146 udelay(500); /* Wait write complete */
1147 iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer write command */
1148
Ben Dooks9ef9ac52005-07-23 17:25:18 +01001149 /* restore the previous address */
1150 writeb(reg_save, db->io_addr);
1151
Sascha Hauera1365272005-05-05 15:14:15 -07001152 spin_unlock_irqrestore(&db->lock,flags);
1153}
1154
1155static int
1156dm9000_drv_suspend(struct device *dev, u32 state, u32 level)
1157{
1158 struct net_device *ndev = dev_get_drvdata(dev);
1159
1160 if (ndev && level == SUSPEND_DISABLE) {
1161 if (netif_running(ndev)) {
1162 netif_device_detach(ndev);
1163 dm9000_shutdown(ndev);
1164 }
1165 }
1166 return 0;
1167}
1168
1169static int
1170dm9000_drv_resume(struct device *dev, u32 level)
1171{
1172 struct net_device *ndev = dev_get_drvdata(dev);
1173 board_info_t *db = (board_info_t *) ndev->priv;
1174
1175 if (ndev && level == RESUME_ENABLE) {
1176
1177 if (netif_running(ndev)) {
1178 dm9000_reset(db);
1179 dm9000_init_dm9000(ndev);
1180
1181 netif_device_attach(ndev);
1182 }
1183 }
1184 return 0;
1185}
1186
1187static int
1188dm9000_drv_remove(struct device *dev)
1189{
1190 struct platform_device *pdev = to_platform_device(dev);
1191 struct net_device *ndev = dev_get_drvdata(dev);
1192
1193 dev_set_drvdata(dev, NULL);
1194
1195 unregister_netdev(ndev);
1196 dm9000_release_board(pdev, (board_info_t *) ndev->priv);
1197 kfree(ndev); /* free device structure */
1198
1199 PRINTK1("clean_module() exit\n");
1200
1201 return 0;
1202}
1203
1204static struct device_driver dm9000_driver = {
1205 .name = "dm9000",
1206 .bus = &platform_bus_type,
1207 .probe = dm9000_probe,
1208 .remove = dm9000_drv_remove,
1209 .suspend = dm9000_drv_suspend,
1210 .resume = dm9000_drv_resume,
1211};
1212
1213static int __init
1214dm9000_init(void)
1215{
1216 return driver_register(&dm9000_driver); /* search board and register */
1217}
1218
1219static void __exit
1220dm9000_cleanup(void)
1221{
1222 driver_unregister(&dm9000_driver);
1223}
1224
1225module_init(dm9000_init);
1226module_exit(dm9000_cleanup);
1227
1228MODULE_AUTHOR("Sascha Hauer, Ben Dooks");
1229MODULE_DESCRIPTION("Davicom DM9000 network driver");
1230MODULE_LICENSE("GPL");