Linux 2.6.17.7
[linux/fpc-iii.git] / drivers / net / dm9000.c
blob24996da4c1c44f9919b3a6277422c158c0312232
1 /*
2 * dm9000.c: Version 1.2 03/18/2003
4 * A Davicom DM9000 ISA NIC fast Ethernet driver for Linux.
5 * Copyright (C) 1997 Sten Wang
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.
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.
17 * (C)Copyright 1997-1998 DAVICOM Semiconductor,Inc. All Rights Reserved.
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
27 * v1.00 modify by simon 2001.9.5
28 * change for kernel 2.4.x
30 * v1.1 11/09/2001 fix force mode bug
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.
37 * 03/03/2004 Sascha Hauer <s.hauer@pengutronix.de>
38 * Port to 2.6 kernel
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
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
52 * 01-Jul-2005 Ben Dooks <ben@simtec.co.uk>
53 * * fixed spinlock call without pointer
54 * * ensure spinlock is initialised
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/spinlock.h>
64 #include <linux/crc32.h>
65 #include <linux/mii.h>
66 #include <linux/dm9000.h>
67 #include <linux/delay.h>
68 #include <linux/platform_device.h>
70 #include <asm/delay.h>
71 #include <asm/irq.h>
72 #include <asm/io.h>
74 #include "dm9000.h"
76 /* Board/System/Debug information/definition ---------------- */
78 #define DM9000_PHY 0x40 /* PHY address 0x01 */
80 #define TRUE 1
81 #define FALSE 0
83 #define CARDNAME "dm9000"
84 #define PFX CARDNAME ": "
86 #define DM9000_TIMER_WUT jiffies+(HZ*2) /* timer wakeup time : 2 second */
88 #define DM9000_DEBUG 0
90 #if DM9000_DEBUG > 2
91 #define PRINTK3(args...) printk(CARDNAME ": " args)
92 #else
93 #define PRINTK3(args...) do { } while(0)
94 #endif
96 #if DM9000_DEBUG > 1
97 #define PRINTK2(args...) printk(CARDNAME ": " args)
98 #else
99 #define PRINTK2(args...) do { } while(0)
100 #endif
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
111 * Transmit timeout, default 5 seconds.
113 static int watchdog = 5000;
114 module_param(watchdog, int, 0400);
115 MODULE_PARM_DESC(watchdog, "transmit timeout in milliseconds");
117 /* Structure/enum declaration ------------------------------- */
118 typedef struct board_info {
120 void __iomem *io_addr; /* Register I/O base address */
121 void __iomem *io_data; /* Data I/O address */
122 u16 irq; /* IRQ */
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;
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);
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;
141 struct timer_list timer;
142 struct net_device_stats stats;
143 unsigned char srom[128];
144 spinlock_t lock;
146 struct mii_if_info mii;
147 u32 msg_enable;
148 } board_info_t;
150 /* function declaration ------------------------------------- */
151 static int dm9000_probe(struct platform_device *);
152 static int dm9000_open(struct net_device *);
153 static int dm9000_start_xmit(struct sk_buff *, struct net_device *);
154 static int dm9000_stop(struct net_device *);
157 static void dm9000_timer(unsigned long);
158 static void dm9000_init_dm9000(struct net_device *);
160 static struct net_device_stats *dm9000_get_stats(struct net_device *);
162 static irqreturn_t dm9000_interrupt(int, void *, struct pt_regs *);
164 static int dm9000_phy_read(struct net_device *dev, int phyaddr_unsused, int reg);
165 static void dm9000_phy_write(struct net_device *dev, int phyaddr_unused, int reg,
166 int value);
167 static u16 read_srom_word(board_info_t *, int);
168 static void dm9000_rx(struct net_device *);
169 static void dm9000_hash_table(struct net_device *);
171 //#define DM9000_PROGRAM_EEPROM
172 #ifdef DM9000_PROGRAM_EEPROM
173 static void program_eeprom(board_info_t * db);
174 #endif
175 /* DM9000 network board routine ---------------------------- */
177 static void
178 dm9000_reset(board_info_t * db)
180 PRINTK1("dm9000x: resetting\n");
181 /* RESET device */
182 writeb(DM9000_NCR, db->io_addr);
183 udelay(200);
184 writeb(NCR_RST, db->io_data);
185 udelay(200);
189 * Read a byte from I/O port
191 static u8
192 ior(board_info_t * db, int reg)
194 writeb(reg, db->io_addr);
195 return readb(db->io_data);
199 * Write a byte to I/O port
202 static void
203 iow(board_info_t * db, int reg, int value)
205 writeb(reg, db->io_addr);
206 writeb(value, db->io_data);
209 /* routines for sending block to chip */
211 static void dm9000_outblk_8bit(void __iomem *reg, void *data, int count)
213 writesb(reg, data, count);
216 static void dm9000_outblk_16bit(void __iomem *reg, void *data, int count)
218 writesw(reg, data, (count+1) >> 1);
221 static void dm9000_outblk_32bit(void __iomem *reg, void *data, int count)
223 writesl(reg, data, (count+3) >> 2);
226 /* input block from chip to memory */
228 static void dm9000_inblk_8bit(void __iomem *reg, void *data, int count)
230 readsb(reg, data, count);
234 static void dm9000_inblk_16bit(void __iomem *reg, void *data, int count)
236 readsw(reg, data, (count+1) >> 1);
239 static void dm9000_inblk_32bit(void __iomem *reg, void *data, int count)
241 readsl(reg, data, (count+3) >> 2);
244 /* dump block from chip to null */
246 static void dm9000_dumpblk_8bit(void __iomem *reg, int count)
248 int i;
249 int tmp;
251 for (i = 0; i < count; i++)
252 tmp = readb(reg);
255 static void dm9000_dumpblk_16bit(void __iomem *reg, int count)
257 int i;
258 int tmp;
260 count = (count + 1) >> 1;
262 for (i = 0; i < count; i++)
263 tmp = readw(reg);
266 static void dm9000_dumpblk_32bit(void __iomem *reg, int count)
268 int i;
269 int tmp;
271 count = (count + 3) >> 2;
273 for (i = 0; i < count; i++)
274 tmp = readl(reg);
277 /* dm9000_set_io
279 * select the specified set of io routines to use with the
280 * device
283 static void dm9000_set_io(struct board_info *db, int byte_width)
285 /* use the size of the data resource to work out what IO
286 * routines we want to use
289 switch (byte_width) {
290 case 1:
291 db->dumpblk = dm9000_dumpblk_8bit;
292 db->outblk = dm9000_outblk_8bit;
293 db->inblk = dm9000_inblk_8bit;
294 break;
296 case 2:
297 db->dumpblk = dm9000_dumpblk_16bit;
298 db->outblk = dm9000_outblk_16bit;
299 db->inblk = dm9000_inblk_16bit;
300 break;
302 case 3:
303 printk(KERN_ERR PFX ": 3 byte IO, falling back to 16bit\n");
304 db->dumpblk = dm9000_dumpblk_16bit;
305 db->outblk = dm9000_outblk_16bit;
306 db->inblk = dm9000_inblk_16bit;
307 break;
309 case 4:
310 default:
311 db->dumpblk = dm9000_dumpblk_32bit;
312 db->outblk = dm9000_outblk_32bit;
313 db->inblk = dm9000_inblk_32bit;
314 break;
319 /* Our watchdog timed out. Called by the networking layer */
320 static void dm9000_timeout(struct net_device *dev)
322 board_info_t *db = (board_info_t *) dev->priv;
323 u8 reg_save;
324 unsigned long flags;
326 /* Save previous register address */
327 reg_save = readb(db->io_addr);
328 spin_lock_irqsave(&db->lock,flags);
330 netif_stop_queue(dev);
331 dm9000_reset(db);
332 dm9000_init_dm9000(dev);
333 /* We can accept TX packets again */
334 dev->trans_start = jiffies;
335 netif_wake_queue(dev);
337 /* Restore previous register address */
338 writeb(reg_save, db->io_addr);
339 spin_unlock_irqrestore(&db->lock,flags);
343 /* dm9000_release_board
345 * release a board, and any mapped resources
348 static void
349 dm9000_release_board(struct platform_device *pdev, struct board_info *db)
351 if (db->data_res == NULL) {
352 if (db->addr_res != NULL)
353 release_mem_region((unsigned long)db->io_addr, 4);
354 return;
357 /* unmap our resources */
359 iounmap(db->io_addr);
360 iounmap(db->io_data);
362 /* release the resources */
364 if (db->data_req != NULL) {
365 release_resource(db->data_req);
366 kfree(db->data_req);
369 if (db->addr_res != NULL) {
370 release_resource(db->addr_res);
371 kfree(db->addr_req);
375 #define res_size(_r) (((_r)->end - (_r)->start) + 1)
378 * Search DM9000 board, allocate space and register it
380 static int
381 dm9000_probe(struct platform_device *pdev)
383 struct dm9000_plat_data *pdata = pdev->dev.platform_data;
384 struct board_info *db; /* Point a board information structure */
385 struct net_device *ndev;
386 unsigned long base;
387 int ret = 0;
388 int iosize;
389 int i;
390 u32 id_val;
392 /* Init network device */
393 ndev = alloc_etherdev(sizeof (struct board_info));
394 if (!ndev) {
395 printk("%s: could not allocate device.\n", CARDNAME);
396 return -ENOMEM;
399 SET_MODULE_OWNER(ndev);
400 SET_NETDEV_DEV(ndev, &pdev->dev);
402 PRINTK2("dm9000_probe()");
404 /* setup board info structure */
405 db = (struct board_info *) ndev->priv;
406 memset(db, 0, sizeof (*db));
408 spin_lock_init(&db->lock);
410 if (pdev->num_resources < 2) {
411 ret = -ENODEV;
412 goto out;
415 switch (pdev->num_resources) {
416 case 2:
417 base = pdev->resource[0].start;
419 if (!request_mem_region(base, 4, ndev->name)) {
420 ret = -EBUSY;
421 goto out;
424 ndev->base_addr = base;
425 ndev->irq = pdev->resource[1].start;
426 db->io_addr = (void *)base;
427 db->io_data = (void *)(base + 4);
429 break;
431 case 3:
432 db->addr_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
433 db->data_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
434 db->irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
436 if (db->addr_res == NULL || db->data_res == NULL) {
437 printk(KERN_ERR PFX "insufficient resources\n");
438 ret = -ENOENT;
439 goto out;
442 i = res_size(db->addr_res);
443 db->addr_req = request_mem_region(db->addr_res->start, i,
444 pdev->name);
446 if (db->addr_req == NULL) {
447 printk(KERN_ERR PFX "cannot claim address reg area\n");
448 ret = -EIO;
449 goto out;
452 db->io_addr = ioremap(db->addr_res->start, i);
454 if (db->io_addr == NULL) {
455 printk(KERN_ERR "failed to ioremap address reg\n");
456 ret = -EINVAL;
457 goto out;
460 iosize = res_size(db->data_res);
461 db->data_req = request_mem_region(db->data_res->start, iosize,
462 pdev->name);
464 if (db->data_req == NULL) {
465 printk(KERN_ERR PFX "cannot claim data reg area\n");
466 ret = -EIO;
467 goto out;
470 db->io_data = ioremap(db->data_res->start, iosize);
472 if (db->io_data == NULL) {
473 printk(KERN_ERR "failed to ioremap data reg\n");
474 ret = -EINVAL;
475 goto out;
478 /* fill in parameters for net-dev structure */
480 ndev->base_addr = (unsigned long)db->io_addr;
481 ndev->irq = db->irq_res->start;
483 /* ensure at least we have a default set of IO routines */
484 dm9000_set_io(db, iosize);
488 /* check to see if anything is being over-ridden */
489 if (pdata != NULL) {
490 /* check to see if the driver wants to over-ride the
491 * default IO width */
493 if (pdata->flags & DM9000_PLATF_8BITONLY)
494 dm9000_set_io(db, 1);
496 if (pdata->flags & DM9000_PLATF_16BITONLY)
497 dm9000_set_io(db, 2);
499 if (pdata->flags & DM9000_PLATF_32BITONLY)
500 dm9000_set_io(db, 4);
502 /* check to see if there are any IO routine
503 * over-rides */
505 if (pdata->inblk != NULL)
506 db->inblk = pdata->inblk;
508 if (pdata->outblk != NULL)
509 db->outblk = pdata->outblk;
511 if (pdata->dumpblk != NULL)
512 db->dumpblk = pdata->dumpblk;
515 dm9000_reset(db);
517 /* try two times, DM9000 sometimes gets the first read wrong */
518 for (i = 0; i < 2; i++) {
519 id_val = ior(db, DM9000_VIDL);
520 id_val |= (u32)ior(db, DM9000_VIDH) << 8;
521 id_val |= (u32)ior(db, DM9000_PIDL) << 16;
522 id_val |= (u32)ior(db, DM9000_PIDH) << 24;
524 if (id_val == DM9000_ID)
525 break;
526 printk("%s: read wrong id 0x%08x\n", CARDNAME, id_val);
529 if (id_val != DM9000_ID) {
530 printk("%s: wrong id: 0x%08x\n", CARDNAME, id_val);
531 goto release;
534 /* from this point we assume that we have found a DM9000 */
536 /* driver system function */
537 ether_setup(ndev);
539 ndev->open = &dm9000_open;
540 ndev->hard_start_xmit = &dm9000_start_xmit;
541 ndev->tx_timeout = &dm9000_timeout;
542 ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
543 ndev->stop = &dm9000_stop;
544 ndev->get_stats = &dm9000_get_stats;
545 ndev->set_multicast_list = &dm9000_hash_table;
547 #ifdef DM9000_PROGRAM_EEPROM
548 program_eeprom(db);
549 #endif
550 db->msg_enable = NETIF_MSG_LINK;
551 db->mii.phy_id_mask = 0x1f;
552 db->mii.reg_num_mask = 0x1f;
553 db->mii.force_media = 0;
554 db->mii.full_duplex = 0;
555 db->mii.dev = ndev;
556 db->mii.mdio_read = dm9000_phy_read;
557 db->mii.mdio_write = dm9000_phy_write;
559 /* Read SROM content */
560 for (i = 0; i < 64; i++)
561 ((u16 *) db->srom)[i] = read_srom_word(db, i);
563 /* Set Node Address */
564 for (i = 0; i < 6; i++)
565 ndev->dev_addr[i] = db->srom[i];
567 if (!is_valid_ether_addr(ndev->dev_addr))
568 printk("%s: Invalid ethernet MAC address. Please "
569 "set using ifconfig\n", ndev->name);
571 platform_set_drvdata(pdev, ndev);
572 ret = register_netdev(ndev);
574 if (ret == 0) {
575 printk("%s: dm9000 at %p,%p IRQ %d MAC: ",
576 ndev->name, db->io_addr, db->io_data, ndev->irq);
577 for (i = 0; i < 5; i++)
578 printk("%02x:", ndev->dev_addr[i]);
579 printk("%02x\n", ndev->dev_addr[5]);
581 return 0;
583 release:
584 out:
585 printk("%s: not found (%d).\n", CARDNAME, ret);
587 dm9000_release_board(pdev, db);
588 kfree(ndev);
590 return ret;
594 * Open the interface.
595 * The interface is opened whenever "ifconfig" actives it.
597 static int
598 dm9000_open(struct net_device *dev)
600 board_info_t *db = (board_info_t *) dev->priv;
602 PRINTK2("entering dm9000_open\n");
604 if (request_irq(dev->irq, &dm9000_interrupt, SA_SHIRQ, dev->name, dev))
605 return -EAGAIN;
607 /* Initialize DM9000 board */
608 dm9000_reset(db);
609 dm9000_init_dm9000(dev);
611 /* Init driver variable */
612 db->dbug_cnt = 0;
614 /* set and active a timer process */
615 init_timer(&db->timer);
616 db->timer.expires = DM9000_TIMER_WUT;
617 db->timer.data = (unsigned long) dev;
618 db->timer.function = &dm9000_timer;
619 add_timer(&db->timer);
621 mii_check_media(&db->mii, netif_msg_link(db), 1);
622 netif_start_queue(dev);
624 return 0;
628 * Initilize dm9000 board
630 static void
631 dm9000_init_dm9000(struct net_device *dev)
633 board_info_t *db = (board_info_t *) dev->priv;
635 PRINTK1("entering %s\n",__FUNCTION__);
637 /* I/O mode */
638 db->io_mode = ior(db, DM9000_ISR) >> 6; /* ISR bit7:6 keeps I/O mode */
640 /* GPIO0 on pre-activate PHY */
641 iow(db, DM9000_GPR, 0); /* REG_1F bit0 activate phyxcer */
642 iow(db, DM9000_GPCR, GPCR_GEP_CNTL); /* Let GPIO0 output */
643 iow(db, DM9000_GPR, 0); /* Enable PHY */
645 /* Program operating register */
646 iow(db, DM9000_TCR, 0); /* TX Polling clear */
647 iow(db, DM9000_BPTR, 0x3f); /* Less 3Kb, 200us */
648 iow(db, DM9000_FCR, 0xff); /* Flow Control */
649 iow(db, DM9000_SMCR, 0); /* Special Mode */
650 /* clear TX status */
651 iow(db, DM9000_NSR, NSR_WAKEST | NSR_TX2END | NSR_TX1END);
652 iow(db, DM9000_ISR, ISR_CLR_STATUS); /* Clear interrupt status */
654 /* Set address filter table */
655 dm9000_hash_table(dev);
657 /* Activate DM9000 */
658 iow(db, DM9000_RCR, RCR_DIS_LONG | RCR_DIS_CRC | RCR_RXEN);
659 /* Enable TX/RX interrupt mask */
660 iow(db, DM9000_IMR, IMR_PAR | IMR_PTM | IMR_PRM);
662 /* Init Driver variable */
663 db->tx_pkt_cnt = 0;
664 db->queue_pkt_len = 0;
665 dev->trans_start = 0;
666 spin_lock_init(&db->lock);
670 * Hardware start transmission.
671 * Send a packet to media from the upper layer.
673 static int
674 dm9000_start_xmit(struct sk_buff *skb, struct net_device *dev)
676 board_info_t *db = (board_info_t *) dev->priv;
678 PRINTK3("dm9000_start_xmit\n");
680 if (db->tx_pkt_cnt > 1)
681 return 1;
683 netif_stop_queue(dev);
685 /* Disable all interrupts */
686 iow(db, DM9000_IMR, IMR_PAR);
688 /* Move data to DM9000 TX RAM */
689 writeb(DM9000_MWCMD, db->io_addr);
691 (db->outblk)(db->io_data, skb->data, skb->len);
692 db->stats.tx_bytes += skb->len;
694 /* TX control: First packet immediately send, second packet queue */
695 if (db->tx_pkt_cnt == 0) {
697 /* First Packet */
698 db->tx_pkt_cnt++;
700 /* Set TX length to DM9000 */
701 iow(db, DM9000_TXPLL, skb->len & 0xff);
702 iow(db, DM9000_TXPLH, (skb->len >> 8) & 0xff);
704 /* Issue TX polling command */
705 iow(db, DM9000_TCR, TCR_TXREQ); /* Cleared after TX complete */
707 dev->trans_start = jiffies; /* save the time stamp */
709 } else {
710 /* Second packet */
711 db->tx_pkt_cnt++;
712 db->queue_pkt_len = skb->len;
715 /* free this SKB */
716 dev_kfree_skb(skb);
718 /* Re-enable resource check */
719 if (db->tx_pkt_cnt == 1)
720 netif_wake_queue(dev);
722 /* Re-enable interrupt */
723 iow(db, DM9000_IMR, IMR_PAR | IMR_PTM | IMR_PRM);
725 return 0;
728 static void
729 dm9000_shutdown(struct net_device *dev)
731 board_info_t *db = (board_info_t *) dev->priv;
733 /* RESET device */
734 dm9000_phy_write(dev, 0, MII_BMCR, BMCR_RESET); /* PHY RESET */
735 iow(db, DM9000_GPR, 0x01); /* Power-Down PHY */
736 iow(db, DM9000_IMR, IMR_PAR); /* Disable all interrupt */
737 iow(db, DM9000_RCR, 0x00); /* Disable RX */
741 * Stop the interface.
742 * The interface is stopped when it is brought.
744 static int
745 dm9000_stop(struct net_device *ndev)
747 board_info_t *db = (board_info_t *) ndev->priv;
749 PRINTK1("entering %s\n",__FUNCTION__);
751 /* deleted timer */
752 del_timer(&db->timer);
754 netif_stop_queue(ndev);
755 netif_carrier_off(ndev);
757 /* free interrupt */
758 free_irq(ndev->irq, ndev);
760 dm9000_shutdown(ndev);
762 return 0;
766 * DM9000 interrupt handler
767 * receive the packet to upper layer, free the transmitted packet
770 void
771 dm9000_tx_done(struct net_device *dev, board_info_t * db)
773 int tx_status = ior(db, DM9000_NSR); /* Got TX status */
775 if (tx_status & (NSR_TX2END | NSR_TX1END)) {
776 /* One packet sent complete */
777 db->tx_pkt_cnt--;
778 db->stats.tx_packets++;
780 /* Queue packet check & send */
781 if (db->tx_pkt_cnt > 0) {
782 iow(db, DM9000_TXPLL, db->queue_pkt_len & 0xff);
783 iow(db, DM9000_TXPLH, (db->queue_pkt_len >> 8) & 0xff);
784 iow(db, DM9000_TCR, TCR_TXREQ);
785 dev->trans_start = jiffies;
787 netif_wake_queue(dev);
791 static irqreturn_t
792 dm9000_interrupt(int irq, void *dev_id, struct pt_regs *regs)
794 struct net_device *dev = dev_id;
795 board_info_t *db;
796 int int_status;
797 u8 reg_save;
799 PRINTK3("entering %s\n",__FUNCTION__);
801 if (!dev) {
802 PRINTK1("dm9000_interrupt() without DEVICE arg\n");
803 return IRQ_HANDLED;
806 /* A real interrupt coming */
807 db = (board_info_t *) dev->priv;
808 spin_lock(&db->lock);
810 /* Save previous register address */
811 reg_save = readb(db->io_addr);
813 /* Disable all interrupts */
814 iow(db, DM9000_IMR, IMR_PAR);
816 /* Got DM9000 interrupt status */
817 int_status = ior(db, DM9000_ISR); /* Got ISR */
818 iow(db, DM9000_ISR, int_status); /* Clear ISR status */
820 /* Received the coming packet */
821 if (int_status & ISR_PRS)
822 dm9000_rx(dev);
824 /* Trnasmit Interrupt check */
825 if (int_status & ISR_PTS)
826 dm9000_tx_done(dev, db);
828 /* Re-enable interrupt mask */
829 iow(db, DM9000_IMR, IMR_PAR | IMR_PTM | IMR_PRM);
831 /* Restore previous register address */
832 writeb(reg_save, db->io_addr);
834 spin_unlock(&db->lock);
836 return IRQ_HANDLED;
840 * Get statistics from driver.
842 static struct net_device_stats *
843 dm9000_get_stats(struct net_device *dev)
845 board_info_t *db = (board_info_t *) dev->priv;
846 return &db->stats;
851 * A periodic timer routine
852 * Dynamic media sense, allocated Rx buffer...
854 static void
855 dm9000_timer(unsigned long data)
857 struct net_device *dev = (struct net_device *) data;
858 board_info_t *db = (board_info_t *) dev->priv;
860 PRINTK3("dm9000_timer()\n");
862 mii_check_media(&db->mii, netif_msg_link(db), 0);
864 /* Set timer again */
865 db->timer.expires = DM9000_TIMER_WUT;
866 add_timer(&db->timer);
869 struct dm9000_rxhdr {
870 u16 RxStatus;
871 u16 RxLen;
872 } __attribute__((__packed__));
875 * Received a packet and pass to upper layer
877 static void
878 dm9000_rx(struct net_device *dev)
880 board_info_t *db = (board_info_t *) dev->priv;
881 struct dm9000_rxhdr rxhdr;
882 struct sk_buff *skb;
883 u8 rxbyte, *rdptr;
884 int GoodPacket;
885 int RxLen;
887 /* Check packet ready or not */
888 do {
889 ior(db, DM9000_MRCMDX); /* Dummy read */
891 /* Get most updated data */
892 rxbyte = readb(db->io_data);
894 /* Status check: this byte must be 0 or 1 */
895 if (rxbyte > DM9000_PKT_RDY) {
896 printk("status check failed: %d\n", rxbyte);
897 iow(db, DM9000_RCR, 0x00); /* Stop Device */
898 iow(db, DM9000_ISR, IMR_PAR); /* Stop INT request */
899 return;
902 if (rxbyte != DM9000_PKT_RDY)
903 return;
905 /* A packet ready now & Get status/length */
906 GoodPacket = TRUE;
907 writeb(DM9000_MRCMD, db->io_addr);
909 (db->inblk)(db->io_data, &rxhdr, sizeof(rxhdr));
911 RxLen = rxhdr.RxLen;
913 /* Packet Status check */
914 if (RxLen < 0x40) {
915 GoodPacket = FALSE;
916 PRINTK1("Bad Packet received (runt)\n");
919 if (RxLen > DM9000_PKT_MAX) {
920 PRINTK1("RST: RX Len:%x\n", RxLen);
923 if (rxhdr.RxStatus & 0xbf00) {
924 GoodPacket = FALSE;
925 if (rxhdr.RxStatus & 0x100) {
926 PRINTK1("fifo error\n");
927 db->stats.rx_fifo_errors++;
929 if (rxhdr.RxStatus & 0x200) {
930 PRINTK1("crc error\n");
931 db->stats.rx_crc_errors++;
933 if (rxhdr.RxStatus & 0x8000) {
934 PRINTK1("length error\n");
935 db->stats.rx_length_errors++;
939 /* Move data from DM9000 */
940 if (GoodPacket
941 && ((skb = dev_alloc_skb(RxLen + 4)) != NULL)) {
942 skb->dev = dev;
943 skb_reserve(skb, 2);
944 rdptr = (u8 *) skb_put(skb, RxLen - 4);
946 /* Read received packet from RX SRAM */
948 (db->inblk)(db->io_data, rdptr, RxLen);
949 db->stats.rx_bytes += RxLen;
951 /* Pass to upper layer */
952 skb->protocol = eth_type_trans(skb, dev);
953 netif_rx(skb);
954 db->stats.rx_packets++;
956 } else {
957 /* need to dump the packet's data */
959 (db->dumpblk)(db->io_data, RxLen);
961 } while (rxbyte == DM9000_PKT_RDY);
965 * Read a word data from SROM
967 static u16
968 read_srom_word(board_info_t * db, int offset)
970 iow(db, DM9000_EPAR, offset);
971 iow(db, DM9000_EPCR, EPCR_ERPRR);
972 mdelay(8); /* according to the datasheet 200us should be enough,
973 but it doesn't work */
974 iow(db, DM9000_EPCR, 0x0);
975 return (ior(db, DM9000_EPDRL) + (ior(db, DM9000_EPDRH) << 8));
978 #ifdef DM9000_PROGRAM_EEPROM
980 * Write a word data to SROM
982 static void
983 write_srom_word(board_info_t * db, int offset, u16 val)
985 iow(db, DM9000_EPAR, offset);
986 iow(db, DM9000_EPDRH, ((val >> 8) & 0xff));
987 iow(db, DM9000_EPDRL, (val & 0xff));
988 iow(db, DM9000_EPCR, EPCR_WEP | EPCR_ERPRW);
989 mdelay(8); /* same shit */
990 iow(db, DM9000_EPCR, 0);
994 * Only for development:
995 * Here we write static data to the eeprom in case
996 * we don't have valid content on a new board
998 static void
999 program_eeprom(board_info_t * db)
1001 u16 eeprom[] = { 0x0c00, 0x007f, 0x1300, /* MAC Address */
1002 0x0000, /* Autoload: accept nothing */
1003 0x0a46, 0x9000, /* Vendor / Product ID */
1004 0x0000, /* pin control */
1005 0x0000,
1006 }; /* Wake-up mode control */
1007 int i;
1008 for (i = 0; i < 8; i++)
1009 write_srom_word(db, i, eeprom[i]);
1011 #endif
1015 * Calculate the CRC valude of the Rx packet
1016 * flag = 1 : return the reverse CRC (for the received packet CRC)
1017 * 0 : return the normal CRC (for Hash Table index)
1020 static unsigned long
1021 cal_CRC(unsigned char *Data, unsigned int Len, u8 flag)
1024 u32 crc = ether_crc_le(Len, Data);
1026 if (flag)
1027 return ~crc;
1029 return crc;
1033 * Set DM9000 multicast address
1035 static void
1036 dm9000_hash_table(struct net_device *dev)
1038 board_info_t *db = (board_info_t *) dev->priv;
1039 struct dev_mc_list *mcptr = dev->mc_list;
1040 int mc_cnt = dev->mc_count;
1041 u32 hash_val;
1042 u16 i, oft, hash_table[4];
1043 unsigned long flags;
1045 PRINTK2("dm9000_hash_table()\n");
1047 spin_lock_irqsave(&db->lock,flags);
1049 for (i = 0, oft = 0x10; i < 6; i++, oft++)
1050 iow(db, oft, dev->dev_addr[i]);
1052 /* Clear Hash Table */
1053 for (i = 0; i < 4; i++)
1054 hash_table[i] = 0x0;
1056 /* broadcast address */
1057 hash_table[3] = 0x8000;
1059 /* the multicast address in Hash Table : 64 bits */
1060 for (i = 0; i < mc_cnt; i++, mcptr = mcptr->next) {
1061 hash_val = cal_CRC((char *) mcptr->dmi_addr, 6, 0) & 0x3f;
1062 hash_table[hash_val / 16] |= (u16) 1 << (hash_val % 16);
1065 /* Write the hash table to MAC MD table */
1066 for (i = 0, oft = 0x16; i < 4; i++) {
1067 iow(db, oft++, hash_table[i] & 0xff);
1068 iow(db, oft++, (hash_table[i] >> 8) & 0xff);
1071 spin_unlock_irqrestore(&db->lock,flags);
1076 * Read a word from phyxcer
1078 static int
1079 dm9000_phy_read(struct net_device *dev, int phy_reg_unused, int reg)
1081 board_info_t *db = (board_info_t *) dev->priv;
1082 unsigned long flags;
1083 unsigned int reg_save;
1084 int ret;
1086 spin_lock_irqsave(&db->lock,flags);
1088 /* Save previous register address */
1089 reg_save = readb(db->io_addr);
1091 /* Fill the phyxcer register into REG_0C */
1092 iow(db, DM9000_EPAR, DM9000_PHY | reg);
1094 iow(db, DM9000_EPCR, 0xc); /* Issue phyxcer read command */
1095 udelay(100); /* Wait read complete */
1096 iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer read command */
1098 /* The read data keeps on REG_0D & REG_0E */
1099 ret = (ior(db, DM9000_EPDRH) << 8) | ior(db, DM9000_EPDRL);
1101 /* restore the previous address */
1102 writeb(reg_save, db->io_addr);
1104 spin_unlock_irqrestore(&db->lock,flags);
1106 return ret;
1110 * Write a word to phyxcer
1112 static void
1113 dm9000_phy_write(struct net_device *dev, int phyaddr_unused, int reg, int value)
1115 board_info_t *db = (board_info_t *) dev->priv;
1116 unsigned long flags;
1117 unsigned long reg_save;
1119 spin_lock_irqsave(&db->lock,flags);
1121 /* Save previous register address */
1122 reg_save = readb(db->io_addr);
1124 /* Fill the phyxcer register into REG_0C */
1125 iow(db, DM9000_EPAR, DM9000_PHY | reg);
1127 /* Fill the written data into REG_0D & REG_0E */
1128 iow(db, DM9000_EPDRL, (value & 0xff));
1129 iow(db, DM9000_EPDRH, ((value >> 8) & 0xff));
1131 iow(db, DM9000_EPCR, 0xa); /* Issue phyxcer write command */
1132 udelay(500); /* Wait write complete */
1133 iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer write command */
1135 /* restore the previous address */
1136 writeb(reg_save, db->io_addr);
1138 spin_unlock_irqrestore(&db->lock,flags);
1141 static int
1142 dm9000_drv_suspend(struct platform_device *dev, pm_message_t state)
1144 struct net_device *ndev = platform_get_drvdata(dev);
1146 if (ndev) {
1147 if (netif_running(ndev)) {
1148 netif_device_detach(ndev);
1149 dm9000_shutdown(ndev);
1152 return 0;
1155 static int
1156 dm9000_drv_resume(struct platform_device *dev)
1158 struct net_device *ndev = platform_get_drvdata(dev);
1159 board_info_t *db = (board_info_t *) ndev->priv;
1161 if (ndev) {
1163 if (netif_running(ndev)) {
1164 dm9000_reset(db);
1165 dm9000_init_dm9000(ndev);
1167 netif_device_attach(ndev);
1170 return 0;
1173 static int
1174 dm9000_drv_remove(struct platform_device *pdev)
1176 struct net_device *ndev = platform_get_drvdata(pdev);
1178 platform_set_drvdata(pdev, NULL);
1180 unregister_netdev(ndev);
1181 dm9000_release_board(pdev, (board_info_t *) ndev->priv);
1182 kfree(ndev); /* free device structure */
1184 PRINTK1("clean_module() exit\n");
1186 return 0;
1189 static struct platform_driver dm9000_driver = {
1190 .probe = dm9000_probe,
1191 .remove = dm9000_drv_remove,
1192 .suspend = dm9000_drv_suspend,
1193 .resume = dm9000_drv_resume,
1194 .driver = {
1195 .name = "dm9000",
1199 static int __init
1200 dm9000_init(void)
1202 printk(KERN_INFO "%s Ethernet Driver\n", CARDNAME);
1204 return platform_driver_register(&dm9000_driver); /* search board and register */
1207 static void __exit
1208 dm9000_cleanup(void)
1210 platform_driver_unregister(&dm9000_driver);
1213 module_init(dm9000_init);
1214 module_exit(dm9000_cleanup);
1216 MODULE_AUTHOR("Sascha Hauer, Ben Dooks");
1217 MODULE_DESCRIPTION("Davicom DM9000 network driver");
1218 MODULE_LICENSE("GPL");