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>:
34 * Added tx/rx 32 bit mode.
35 * Cleaned up for kernel merge.
37 * 03/03/2004 Sascha Hauer <s.hauer@pengutronix.de>
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
49 * * introduced tx_timeout function
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>
69 #include <linux/irq.h>
71 #include <asm/delay.h>
77 /* Board/System/Debug information/definition ---------------- */
79 #define DM9000_PHY 0x40 /* PHY address 0x01 */
81 #define CARDNAME "dm9000"
82 #define PFX CARDNAME ": "
84 #define DM9000_TIMER_WUT jiffies+(HZ*2) /* timer wakeup time : 2 second */
86 #define DM9000_DEBUG 0
89 #define PRINTK3(args...) printk(CARDNAME ": " args)
91 #define PRINTK3(args...) do { } while(0)
95 #define PRINTK2(args...) printk(CARDNAME ": " args)
97 #define PRINTK2(args...) do { } while(0)
101 #define PRINTK1(args...) printk(CARDNAME ": " args)
102 #define PRINTK(args...) printk(CARDNAME ": " args)
104 #define PRINTK1(args...) do { } while(0)
105 #define PRINTK(args...) printk(KERN_DEBUG args)
108 #ifdef CONFIG_BLACKFIN
112 #define writesb outsb
113 #define writesw outsw
114 #define writesl outsl
115 #define DM9000_IRQ_FLAGS (IRQF_SHARED | IRQF_TRIGGER_HIGH)
117 #define DM9000_IRQ_FLAGS (IRQF_SHARED | IRQT_RISING)
121 * Transmit timeout, default 5 seconds.
123 static int watchdog = 5000;
124 module_param(watchdog, int, 0400);
125 MODULE_PARM_DESC(watchdog, "transmit timeout in milliseconds");
127 /* Structure/enum declaration ------------------------------- */
128 typedef struct board_info {
130 void __iomem *io_addr; /* Register I/O base address */
131 void __iomem *io_data; /* Data I/O address */
136 u16 queue_start_addr;
138 u8 io_mode; /* 0:word, 2:byte */
141 void (*inblk)(void __iomem *port, void *data, int length);
142 void (*outblk)(void __iomem *port, void *data, int length);
143 void (*dumpblk)(void __iomem *port, int length);
145 struct resource *addr_res; /* resources found */
146 struct resource *data_res;
147 struct resource *addr_req; /* resources requested */
148 struct resource *data_req;
149 struct resource *irq_res;
151 struct timer_list timer;
152 unsigned char srom[128];
155 struct mii_if_info mii;
159 /* function declaration ------------------------------------- */
160 static int dm9000_probe(struct platform_device *);
161 static int dm9000_open(struct net_device *);
162 static int dm9000_start_xmit(struct sk_buff *, struct net_device *);
163 static int dm9000_stop(struct net_device *);
166 static void dm9000_timer(unsigned long);
167 static void dm9000_init_dm9000(struct net_device *);
169 static irqreturn_t dm9000_interrupt(int, void *);
171 static int dm9000_phy_read(struct net_device *dev, int phyaddr_unsused, int reg);
172 static void dm9000_phy_write(struct net_device *dev, int phyaddr_unused, int reg,
174 static u16 read_srom_word(board_info_t *, int);
175 static void dm9000_rx(struct net_device *);
176 static void dm9000_hash_table(struct net_device *);
178 //#define DM9000_PROGRAM_EEPROM
179 #ifdef DM9000_PROGRAM_EEPROM
180 static void program_eeprom(board_info_t * db);
182 /* DM9000 network board routine ---------------------------- */
185 dm9000_reset(board_info_t * db)
187 PRINTK1("dm9000x: resetting\n");
189 writeb(DM9000_NCR, db->io_addr);
191 writeb(NCR_RST, db->io_data);
196 * Read a byte from I/O port
199 ior(board_info_t * db, int reg)
201 writeb(reg, db->io_addr);
202 return readb(db->io_data);
206 * Write a byte to I/O port
210 iow(board_info_t * db, int reg, int value)
212 writeb(reg, db->io_addr);
213 writeb(value, db->io_data);
216 /* routines for sending block to chip */
218 static void dm9000_outblk_8bit(void __iomem *reg, void *data, int count)
220 writesb(reg, data, count);
223 static void dm9000_outblk_16bit(void __iomem *reg, void *data, int count)
225 writesw(reg, data, (count+1) >> 1);
228 static void dm9000_outblk_32bit(void __iomem *reg, void *data, int count)
230 writesl(reg, data, (count+3) >> 2);
233 /* input block from chip to memory */
235 static void dm9000_inblk_8bit(void __iomem *reg, void *data, int count)
237 readsb(reg, data, count);
241 static void dm9000_inblk_16bit(void __iomem *reg, void *data, int count)
243 readsw(reg, data, (count+1) >> 1);
246 static void dm9000_inblk_32bit(void __iomem *reg, void *data, int count)
248 readsl(reg, data, (count+3) >> 2);
251 /* dump block from chip to null */
253 static void dm9000_dumpblk_8bit(void __iomem *reg, int count)
258 for (i = 0; i < count; i++)
262 static void dm9000_dumpblk_16bit(void __iomem *reg, int count)
267 count = (count + 1) >> 1;
269 for (i = 0; i < count; i++)
273 static void dm9000_dumpblk_32bit(void __iomem *reg, int count)
278 count = (count + 3) >> 2;
280 for (i = 0; i < count; i++)
286 * select the specified set of io routines to use with the
290 static void dm9000_set_io(struct board_info *db, int byte_width)
292 /* use the size of the data resource to work out what IO
293 * routines we want to use
296 switch (byte_width) {
298 db->dumpblk = dm9000_dumpblk_8bit;
299 db->outblk = dm9000_outblk_8bit;
300 db->inblk = dm9000_inblk_8bit;
304 db->dumpblk = dm9000_dumpblk_16bit;
305 db->outblk = dm9000_outblk_16bit;
306 db->inblk = dm9000_inblk_16bit;
310 printk(KERN_ERR PFX ": 3 byte IO, falling back to 16bit\n");
311 db->dumpblk = dm9000_dumpblk_16bit;
312 db->outblk = dm9000_outblk_16bit;
313 db->inblk = dm9000_inblk_16bit;
318 db->dumpblk = dm9000_dumpblk_32bit;
319 db->outblk = dm9000_outblk_32bit;
320 db->inblk = dm9000_inblk_32bit;
326 /* Our watchdog timed out. Called by the networking layer */
327 static void dm9000_timeout(struct net_device *dev)
329 board_info_t *db = (board_info_t *) dev->priv;
333 /* Save previous register address */
334 reg_save = readb(db->io_addr);
335 spin_lock_irqsave(&db->lock,flags);
337 netif_stop_queue(dev);
339 dm9000_init_dm9000(dev);
340 /* We can accept TX packets again */
341 dev->trans_start = jiffies;
342 netif_wake_queue(dev);
344 /* Restore previous register address */
345 writeb(reg_save, db->io_addr);
346 spin_unlock_irqrestore(&db->lock,flags);
349 #ifdef CONFIG_NET_POLL_CONTROLLER
353 static void dm9000_poll_controller(struct net_device *dev)
355 disable_irq(dev->irq);
356 dm9000_interrupt(dev->irq,dev);
357 enable_irq(dev->irq);
361 /* dm9000_release_board
363 * release a board, and any mapped resources
367 dm9000_release_board(struct platform_device *pdev, struct board_info *db)
369 if (db->data_res == NULL) {
370 if (db->addr_res != NULL)
371 release_mem_region((unsigned long)db->io_addr, 4);
375 /* unmap our resources */
377 iounmap(db->io_addr);
378 iounmap(db->io_data);
380 /* release the resources */
382 if (db->data_req != NULL) {
383 release_resource(db->data_req);
387 if (db->addr_req != NULL) {
388 release_resource(db->addr_req);
393 #define res_size(_r) (((_r)->end - (_r)->start) + 1)
396 * Search DM9000 board, allocate space and register it
399 dm9000_probe(struct platform_device *pdev)
401 struct dm9000_plat_data *pdata = pdev->dev.platform_data;
402 struct board_info *db; /* Point a board information structure */
403 struct net_device *ndev;
410 /* Init network device */
411 ndev = alloc_etherdev(sizeof (struct board_info));
413 printk("%s: could not allocate device.\n", CARDNAME);
417 SET_NETDEV_DEV(ndev, &pdev->dev);
419 PRINTK2("dm9000_probe()");
421 /* setup board info structure */
422 db = (struct board_info *) ndev->priv;
423 memset(db, 0, sizeof (*db));
425 spin_lock_init(&db->lock);
427 if (pdev->num_resources < 2) {
430 } else if (pdev->num_resources == 2) {
431 base = pdev->resource[0].start;
433 if (!request_mem_region(base, 4, ndev->name)) {
438 ndev->base_addr = base;
439 ndev->irq = pdev->resource[1].start;
440 db->io_addr = (void __iomem *)base;
441 db->io_data = (void __iomem *)(base + 4);
443 /* ensure at least we have a default set of IO routines */
444 dm9000_set_io(db, 2);
447 db->addr_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
448 db->data_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
449 db->irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
451 if (db->addr_res == NULL || db->data_res == NULL ||
452 db->irq_res == NULL) {
453 printk(KERN_ERR PFX "insufficient resources\n");
458 i = res_size(db->addr_res);
459 db->addr_req = request_mem_region(db->addr_res->start, i,
462 if (db->addr_req == NULL) {
463 printk(KERN_ERR PFX "cannot claim address reg area\n");
468 db->io_addr = ioremap(db->addr_res->start, i);
470 if (db->io_addr == NULL) {
471 printk(KERN_ERR "failed to ioremap address reg\n");
476 iosize = res_size(db->data_res);
477 db->data_req = request_mem_region(db->data_res->start, iosize,
480 if (db->data_req == NULL) {
481 printk(KERN_ERR PFX "cannot claim data reg area\n");
486 db->io_data = ioremap(db->data_res->start, iosize);
488 if (db->io_data == NULL) {
489 printk(KERN_ERR "failed to ioremap data reg\n");
494 /* fill in parameters for net-dev structure */
496 ndev->base_addr = (unsigned long)db->io_addr;
497 ndev->irq = db->irq_res->start;
499 /* ensure at least we have a default set of IO routines */
500 dm9000_set_io(db, iosize);
503 /* check to see if anything is being over-ridden */
505 /* check to see if the driver wants to over-ride the
506 * default IO width */
508 if (pdata->flags & DM9000_PLATF_8BITONLY)
509 dm9000_set_io(db, 1);
511 if (pdata->flags & DM9000_PLATF_16BITONLY)
512 dm9000_set_io(db, 2);
514 if (pdata->flags & DM9000_PLATF_32BITONLY)
515 dm9000_set_io(db, 4);
517 /* check to see if there are any IO routine
520 if (pdata->inblk != NULL)
521 db->inblk = pdata->inblk;
523 if (pdata->outblk != NULL)
524 db->outblk = pdata->outblk;
526 if (pdata->dumpblk != NULL)
527 db->dumpblk = pdata->dumpblk;
532 /* try two times, DM9000 sometimes gets the first read wrong */
533 for (i = 0; i < 2; i++) {
534 id_val = ior(db, DM9000_VIDL);
535 id_val |= (u32)ior(db, DM9000_VIDH) << 8;
536 id_val |= (u32)ior(db, DM9000_PIDL) << 16;
537 id_val |= (u32)ior(db, DM9000_PIDH) << 24;
539 if (id_val == DM9000_ID)
541 printk("%s: read wrong id 0x%08x\n", CARDNAME, id_val);
544 if (id_val != DM9000_ID) {
545 printk("%s: wrong id: 0x%08x\n", CARDNAME, id_val);
550 /* from this point we assume that we have found a DM9000 */
552 /* driver system function */
555 ndev->open = &dm9000_open;
556 ndev->hard_start_xmit = &dm9000_start_xmit;
557 ndev->tx_timeout = &dm9000_timeout;
558 ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
559 ndev->stop = &dm9000_stop;
560 ndev->set_multicast_list = &dm9000_hash_table;
561 #ifdef CONFIG_NET_POLL_CONTROLLER
562 ndev->poll_controller = &dm9000_poll_controller;
565 #ifdef DM9000_PROGRAM_EEPROM
568 db->msg_enable = NETIF_MSG_LINK;
569 db->mii.phy_id_mask = 0x1f;
570 db->mii.reg_num_mask = 0x1f;
571 db->mii.force_media = 0;
572 db->mii.full_duplex = 0;
574 db->mii.mdio_read = dm9000_phy_read;
575 db->mii.mdio_write = dm9000_phy_write;
577 /* Read SROM content */
578 for (i = 0; i < 64; i++)
579 ((u16 *) db->srom)[i] = read_srom_word(db, i);
581 /* Set Node Address */
582 for (i = 0; i < 6; i++)
583 ndev->dev_addr[i] = db->srom[i];
585 if (!is_valid_ether_addr(ndev->dev_addr)) {
586 /* try reading from mac */
588 for (i = 0; i < 6; i++)
589 ndev->dev_addr[i] = ior(db, i+DM9000_PAR);
592 if (!is_valid_ether_addr(ndev->dev_addr))
593 printk("%s: Invalid ethernet MAC address. Please "
594 "set using ifconfig\n", ndev->name);
596 platform_set_drvdata(pdev, ndev);
597 ret = register_netdev(ndev);
600 DECLARE_MAC_BUF(mac);
601 printk("%s: dm9000 at %p,%p IRQ %d MAC: %s\n",
602 ndev->name, db->io_addr, db->io_data, ndev->irq,
603 print_mac(mac, ndev->dev_addr));
608 printk("%s: not found (%d).\n", CARDNAME, ret);
610 dm9000_release_board(pdev, db);
617 * Open the interface.
618 * The interface is opened whenever "ifconfig" actives it.
621 dm9000_open(struct net_device *dev)
623 board_info_t *db = (board_info_t *) dev->priv;
625 PRINTK2("entering dm9000_open\n");
627 if (request_irq(dev->irq, &dm9000_interrupt, DM9000_IRQ_FLAGS, dev->name, dev))
630 /* Initialize DM9000 board */
632 dm9000_init_dm9000(dev);
634 /* Init driver variable */
637 /* set and active a timer process */
638 init_timer(&db->timer);
639 db->timer.expires = DM9000_TIMER_WUT;
640 db->timer.data = (unsigned long) dev;
641 db->timer.function = &dm9000_timer;
642 add_timer(&db->timer);
644 mii_check_media(&db->mii, netif_msg_link(db), 1);
645 netif_start_queue(dev);
651 * Initilize dm9000 board
654 dm9000_init_dm9000(struct net_device *dev)
656 board_info_t *db = (board_info_t *) dev->priv;
658 PRINTK1("entering %s\n",__FUNCTION__);
661 db->io_mode = ior(db, DM9000_ISR) >> 6; /* ISR bit7:6 keeps I/O mode */
663 /* GPIO0 on pre-activate PHY */
664 iow(db, DM9000_GPR, 0); /* REG_1F bit0 activate phyxcer */
665 iow(db, DM9000_GPCR, GPCR_GEP_CNTL); /* Let GPIO0 output */
666 iow(db, DM9000_GPR, 0); /* Enable PHY */
668 /* Program operating register */
669 iow(db, DM9000_TCR, 0); /* TX Polling clear */
670 iow(db, DM9000_BPTR, 0x3f); /* Less 3Kb, 200us */
671 iow(db, DM9000_FCR, 0xff); /* Flow Control */
672 iow(db, DM9000_SMCR, 0); /* Special Mode */
673 /* clear TX status */
674 iow(db, DM9000_NSR, NSR_WAKEST | NSR_TX2END | NSR_TX1END);
675 iow(db, DM9000_ISR, ISR_CLR_STATUS); /* Clear interrupt status */
677 /* Set address filter table */
678 dm9000_hash_table(dev);
680 /* Activate DM9000 */
681 iow(db, DM9000_RCR, RCR_DIS_LONG | RCR_DIS_CRC | RCR_RXEN);
682 /* Enable TX/RX interrupt mask */
683 iow(db, DM9000_IMR, IMR_PAR | IMR_PTM | IMR_PRM);
685 /* Init Driver variable */
687 db->queue_pkt_len = 0;
688 dev->trans_start = 0;
692 * Hardware start transmission.
693 * Send a packet to media from the upper layer.
696 dm9000_start_xmit(struct sk_buff *skb, struct net_device *dev)
699 board_info_t *db = (board_info_t *) dev->priv;
701 PRINTK3("dm9000_start_xmit\n");
703 if (db->tx_pkt_cnt > 1)
706 spin_lock_irqsave(&db->lock, flags);
708 /* Move data to DM9000 TX RAM */
709 writeb(DM9000_MWCMD, db->io_addr);
711 (db->outblk)(db->io_data, skb->data, skb->len);
712 dev->stats.tx_bytes += skb->len;
715 /* TX control: First packet immediately send, second packet queue */
716 if (db->tx_pkt_cnt == 1) {
717 /* Set TX length to DM9000 */
718 iow(db, DM9000_TXPLL, skb->len & 0xff);
719 iow(db, DM9000_TXPLH, (skb->len >> 8) & 0xff);
721 /* Issue TX polling command */
722 iow(db, DM9000_TCR, TCR_TXREQ); /* Cleared after TX complete */
724 dev->trans_start = jiffies; /* save the time stamp */
727 db->queue_pkt_len = skb->len;
728 netif_stop_queue(dev);
731 spin_unlock_irqrestore(&db->lock, flags);
740 dm9000_shutdown(struct net_device *dev)
742 board_info_t *db = (board_info_t *) dev->priv;
745 dm9000_phy_write(dev, 0, MII_BMCR, BMCR_RESET); /* PHY RESET */
746 iow(db, DM9000_GPR, 0x01); /* Power-Down PHY */
747 iow(db, DM9000_IMR, IMR_PAR); /* Disable all interrupt */
748 iow(db, DM9000_RCR, 0x00); /* Disable RX */
752 * Stop the interface.
753 * The interface is stopped when it is brought.
756 dm9000_stop(struct net_device *ndev)
758 board_info_t *db = (board_info_t *) ndev->priv;
760 PRINTK1("entering %s\n",__FUNCTION__);
763 del_timer(&db->timer);
765 netif_stop_queue(ndev);
766 netif_carrier_off(ndev);
769 free_irq(ndev->irq, ndev);
771 dm9000_shutdown(ndev);
777 * DM9000 interrupt handler
778 * receive the packet to upper layer, free the transmitted packet
782 dm9000_tx_done(struct net_device *dev, board_info_t * db)
784 int tx_status = ior(db, DM9000_NSR); /* Got TX status */
786 if (tx_status & (NSR_TX2END | NSR_TX1END)) {
787 /* One packet sent complete */
789 dev->stats.tx_packets++;
791 /* Queue packet check & send */
792 if (db->tx_pkt_cnt > 0) {
793 iow(db, DM9000_TXPLL, db->queue_pkt_len & 0xff);
794 iow(db, DM9000_TXPLH, (db->queue_pkt_len >> 8) & 0xff);
795 iow(db, DM9000_TCR, TCR_TXREQ);
796 dev->trans_start = jiffies;
798 netif_wake_queue(dev);
803 dm9000_interrupt(int irq, void *dev_id)
805 struct net_device *dev = dev_id;
810 PRINTK3("entering %s\n",__FUNCTION__);
813 PRINTK1("dm9000_interrupt() without DEVICE arg\n");
817 /* A real interrupt coming */
818 db = (board_info_t *) dev->priv;
819 spin_lock(&db->lock);
821 /* Save previous register address */
822 reg_save = readb(db->io_addr);
824 /* Disable all interrupts */
825 iow(db, DM9000_IMR, IMR_PAR);
827 /* Got DM9000 interrupt status */
828 int_status = ior(db, DM9000_ISR); /* Got ISR */
829 iow(db, DM9000_ISR, int_status); /* Clear ISR status */
831 /* Received the coming packet */
832 if (int_status & ISR_PRS)
835 /* Trnasmit Interrupt check */
836 if (int_status & ISR_PTS)
837 dm9000_tx_done(dev, db);
839 /* Re-enable interrupt mask */
840 iow(db, DM9000_IMR, IMR_PAR | IMR_PTM | IMR_PRM);
842 /* Restore previous register address */
843 writeb(reg_save, db->io_addr);
845 spin_unlock(&db->lock);
851 * A periodic timer routine
852 * Dynamic media sense, allocated Rx buffer...
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 {
872 } __attribute__((__packed__));
875 * Received a packet and pass to upper layer
878 dm9000_rx(struct net_device *dev)
880 board_info_t *db = (board_info_t *) dev->priv;
881 struct dm9000_rxhdr rxhdr;
887 /* Check packet ready or not */
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 */
902 if (rxbyte != DM9000_PKT_RDY)
905 /* A packet ready now & Get status/length */
907 writeb(DM9000_MRCMD, db->io_addr);
909 (db->inblk)(db->io_data, &rxhdr, sizeof(rxhdr));
913 /* Packet Status check */
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) {
925 if (rxhdr.RxStatus & 0x100) {
926 PRINTK1("fifo error\n");
927 dev->stats.rx_fifo_errors++;
929 if (rxhdr.RxStatus & 0x200) {
930 PRINTK1("crc error\n");
931 dev->stats.rx_crc_errors++;
933 if (rxhdr.RxStatus & 0x8000) {
934 PRINTK1("length error\n");
935 dev->stats.rx_length_errors++;
939 /* Move data from DM9000 */
941 && ((skb = dev_alloc_skb(RxLen + 4)) != NULL)) {
943 rdptr = (u8 *) skb_put(skb, RxLen - 4);
945 /* Read received packet from RX SRAM */
947 (db->inblk)(db->io_data, rdptr, RxLen);
948 dev->stats.rx_bytes += RxLen;
950 /* Pass to upper layer */
951 skb->protocol = eth_type_trans(skb, dev);
953 dev->stats.rx_packets++;
956 /* need to dump the packet's data */
958 (db->dumpblk)(db->io_data, RxLen);
960 } while (rxbyte == DM9000_PKT_RDY);
964 * Read a word data from SROM
967 read_srom_word(board_info_t * db, int offset)
969 iow(db, DM9000_EPAR, offset);
970 iow(db, DM9000_EPCR, EPCR_ERPRR);
971 mdelay(8); /* according to the datasheet 200us should be enough,
972 but it doesn't work */
973 iow(db, DM9000_EPCR, 0x0);
974 return (ior(db, DM9000_EPDRL) + (ior(db, DM9000_EPDRH) << 8));
977 #ifdef DM9000_PROGRAM_EEPROM
979 * Write a word data to SROM
982 write_srom_word(board_info_t * db, int offset, u16 val)
984 iow(db, DM9000_EPAR, offset);
985 iow(db, DM9000_EPDRH, ((val >> 8) & 0xff));
986 iow(db, DM9000_EPDRL, (val & 0xff));
987 iow(db, DM9000_EPCR, EPCR_WEP | EPCR_ERPRW);
988 mdelay(8); /* same shit */
989 iow(db, DM9000_EPCR, 0);
993 * Only for development:
994 * Here we write static data to the eeprom in case
995 * we don't have valid content on a new board
998 program_eeprom(board_info_t * db)
1000 u16 eeprom[] = { 0x0c00, 0x007f, 0x1300, /* MAC Address */
1001 0x0000, /* Autoload: accept nothing */
1002 0x0a46, 0x9000, /* Vendor / Product ID */
1003 0x0000, /* pin control */
1005 }; /* Wake-up mode control */
1007 for (i = 0; i < 8; i++)
1008 write_srom_word(db, i, eeprom[i]);
1014 * Calculate the CRC valude of the Rx packet
1015 * flag = 1 : return the reverse CRC (for the received packet CRC)
1016 * 0 : return the normal CRC (for Hash Table index)
1019 static unsigned long
1020 cal_CRC(unsigned char *Data, unsigned int Len, u8 flag)
1023 u32 crc = ether_crc_le(Len, Data);
1032 * Set DM9000 multicast address
1035 dm9000_hash_table(struct net_device *dev)
1037 board_info_t *db = (board_info_t *) dev->priv;
1038 struct dev_mc_list *mcptr = dev->mc_list;
1039 int mc_cnt = dev->mc_count;
1041 u16 i, oft, hash_table[4];
1042 unsigned long flags;
1044 PRINTK2("dm9000_hash_table()\n");
1046 spin_lock_irqsave(&db->lock,flags);
1048 for (i = 0, oft = 0x10; i < 6; i++, oft++)
1049 iow(db, oft, dev->dev_addr[i]);
1051 /* Clear Hash Table */
1052 for (i = 0; i < 4; i++)
1053 hash_table[i] = 0x0;
1055 /* broadcast address */
1056 hash_table[3] = 0x8000;
1058 /* the multicast address in Hash Table : 64 bits */
1059 for (i = 0; i < mc_cnt; i++, mcptr = mcptr->next) {
1060 hash_val = cal_CRC((char *) mcptr->dmi_addr, 6, 0) & 0x3f;
1061 hash_table[hash_val / 16] |= (u16) 1 << (hash_val % 16);
1064 /* Write the hash table to MAC MD table */
1065 for (i = 0, oft = 0x16; i < 4; i++) {
1066 iow(db, oft++, hash_table[i] & 0xff);
1067 iow(db, oft++, (hash_table[i] >> 8) & 0xff);
1070 spin_unlock_irqrestore(&db->lock,flags);
1075 * Read a word from phyxcer
1078 dm9000_phy_read(struct net_device *dev, int phy_reg_unused, int reg)
1080 board_info_t *db = (board_info_t *) dev->priv;
1081 unsigned long flags;
1082 unsigned int reg_save;
1085 spin_lock_irqsave(&db->lock,flags);
1087 /* Save previous register address */
1088 reg_save = readb(db->io_addr);
1090 /* Fill the phyxcer register into REG_0C */
1091 iow(db, DM9000_EPAR, DM9000_PHY | reg);
1093 iow(db, DM9000_EPCR, 0xc); /* Issue phyxcer read command */
1094 udelay(100); /* Wait read complete */
1095 iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer read command */
1097 /* The read data keeps on REG_0D & REG_0E */
1098 ret = (ior(db, DM9000_EPDRH) << 8) | ior(db, DM9000_EPDRL);
1100 /* restore the previous address */
1101 writeb(reg_save, db->io_addr);
1103 spin_unlock_irqrestore(&db->lock,flags);
1109 * Write a word to phyxcer
1112 dm9000_phy_write(struct net_device *dev, int phyaddr_unused, int reg, int value)
1114 board_info_t *db = (board_info_t *) dev->priv;
1115 unsigned long flags;
1116 unsigned long reg_save;
1118 spin_lock_irqsave(&db->lock,flags);
1120 /* Save previous register address */
1121 reg_save = readb(db->io_addr);
1123 /* Fill the phyxcer register into REG_0C */
1124 iow(db, DM9000_EPAR, DM9000_PHY | reg);
1126 /* Fill the written data into REG_0D & REG_0E */
1127 iow(db, DM9000_EPDRL, (value & 0xff));
1128 iow(db, DM9000_EPDRH, ((value >> 8) & 0xff));
1130 iow(db, DM9000_EPCR, 0xa); /* Issue phyxcer write command */
1131 udelay(500); /* Wait write complete */
1132 iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer write command */
1134 /* restore the previous address */
1135 writeb(reg_save, db->io_addr);
1137 spin_unlock_irqrestore(&db->lock,flags);
1141 dm9000_drv_suspend(struct platform_device *dev, pm_message_t state)
1143 struct net_device *ndev = platform_get_drvdata(dev);
1146 if (netif_running(ndev)) {
1147 netif_device_detach(ndev);
1148 dm9000_shutdown(ndev);
1155 dm9000_drv_resume(struct platform_device *dev)
1157 struct net_device *ndev = platform_get_drvdata(dev);
1158 board_info_t *db = (board_info_t *) ndev->priv;
1162 if (netif_running(ndev)) {
1164 dm9000_init_dm9000(ndev);
1166 netif_device_attach(ndev);
1173 dm9000_drv_remove(struct platform_device *pdev)
1175 struct net_device *ndev = platform_get_drvdata(pdev);
1177 platform_set_drvdata(pdev, NULL);
1179 unregister_netdev(ndev);
1180 dm9000_release_board(pdev, (board_info_t *) ndev->priv);
1181 free_netdev(ndev); /* free device structure */
1183 PRINTK1("clean_module() exit\n");
1188 static struct platform_driver dm9000_driver = {
1191 .owner = THIS_MODULE,
1193 .probe = dm9000_probe,
1194 .remove = dm9000_drv_remove,
1195 .suspend = dm9000_drv_suspend,
1196 .resume = dm9000_drv_resume,
1202 printk(KERN_INFO "%s Ethernet Driver\n", CARDNAME);
1204 return platform_driver_register(&dm9000_driver); /* search board and register */
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");