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 */
142 void (*inblk)(void __iomem *port, void *data, int length);
143 void (*outblk)(void __iomem *port, void *data, int length);
144 void (*dumpblk)(void __iomem *port, int length);
146 struct resource *addr_res; /* resources found */
147 struct resource *data_res;
148 struct resource *addr_req; /* resources requested */
149 struct resource *data_req;
150 struct resource *irq_res;
152 struct timer_list timer;
153 unsigned char srom[128];
156 struct mii_if_info mii;
160 /* function declaration ------------------------------------- */
161 static int dm9000_probe(struct platform_device *);
162 static int dm9000_open(struct net_device *);
163 static int dm9000_start_xmit(struct sk_buff *, struct net_device *);
164 static int dm9000_stop(struct net_device *);
167 static void dm9000_timer(unsigned long);
168 static void dm9000_init_dm9000(struct net_device *);
170 static irqreturn_t dm9000_interrupt(int, void *);
172 static int dm9000_phy_read(struct net_device *dev, int phyaddr_unsused, int reg);
173 static void dm9000_phy_write(struct net_device *dev, int phyaddr_unused, int reg,
175 static u16 read_srom_word(board_info_t *, int);
176 static void dm9000_rx(struct net_device *);
177 static void dm9000_hash_table(struct net_device *);
179 //#define DM9000_PROGRAM_EEPROM
180 #ifdef DM9000_PROGRAM_EEPROM
181 static void program_eeprom(board_info_t * db);
183 /* DM9000 network board routine ---------------------------- */
186 dm9000_reset(board_info_t * db)
188 PRINTK1("dm9000x: resetting\n");
190 writeb(DM9000_NCR, db->io_addr);
192 writeb(NCR_RST, db->io_data);
197 * Read a byte from I/O port
200 ior(board_info_t * db, int reg)
202 writeb(reg, db->io_addr);
203 return readb(db->io_data);
207 * Write a byte to I/O port
211 iow(board_info_t * db, int reg, int value)
213 writeb(reg, db->io_addr);
214 writeb(value, db->io_data);
217 /* routines for sending block to chip */
219 static void dm9000_outblk_8bit(void __iomem *reg, void *data, int count)
221 writesb(reg, data, count);
224 static void dm9000_outblk_16bit(void __iomem *reg, void *data, int count)
226 writesw(reg, data, (count+1) >> 1);
229 static void dm9000_outblk_32bit(void __iomem *reg, void *data, int count)
231 writesl(reg, data, (count+3) >> 2);
234 /* input block from chip to memory */
236 static void dm9000_inblk_8bit(void __iomem *reg, void *data, int count)
238 readsb(reg, data, count);
242 static void dm9000_inblk_16bit(void __iomem *reg, void *data, int count)
244 readsw(reg, data, (count+1) >> 1);
247 static void dm9000_inblk_32bit(void __iomem *reg, void *data, int count)
249 readsl(reg, data, (count+3) >> 2);
252 /* dump block from chip to null */
254 static void dm9000_dumpblk_8bit(void __iomem *reg, int count)
259 for (i = 0; i < count; i++)
263 static void dm9000_dumpblk_16bit(void __iomem *reg, int count)
268 count = (count + 1) >> 1;
270 for (i = 0; i < count; i++)
274 static void dm9000_dumpblk_32bit(void __iomem *reg, int count)
279 count = (count + 3) >> 2;
281 for (i = 0; i < count; i++)
287 * select the specified set of io routines to use with the
291 static void dm9000_set_io(struct board_info *db, int byte_width)
293 /* use the size of the data resource to work out what IO
294 * routines we want to use
297 switch (byte_width) {
299 db->dumpblk = dm9000_dumpblk_8bit;
300 db->outblk = dm9000_outblk_8bit;
301 db->inblk = dm9000_inblk_8bit;
305 db->dumpblk = dm9000_dumpblk_16bit;
306 db->outblk = dm9000_outblk_16bit;
307 db->inblk = dm9000_inblk_16bit;
311 printk(KERN_ERR PFX ": 3 byte IO, falling back to 16bit\n");
312 db->dumpblk = dm9000_dumpblk_16bit;
313 db->outblk = dm9000_outblk_16bit;
314 db->inblk = dm9000_inblk_16bit;
319 db->dumpblk = dm9000_dumpblk_32bit;
320 db->outblk = dm9000_outblk_32bit;
321 db->inblk = dm9000_inblk_32bit;
327 /* Our watchdog timed out. Called by the networking layer */
328 static void dm9000_timeout(struct net_device *dev)
330 board_info_t *db = (board_info_t *) dev->priv;
334 /* Save previous register address */
335 reg_save = readb(db->io_addr);
336 spin_lock_irqsave(&db->lock,flags);
338 netif_stop_queue(dev);
340 dm9000_init_dm9000(dev);
341 /* We can accept TX packets again */
342 dev->trans_start = jiffies;
343 netif_wake_queue(dev);
345 /* Restore previous register address */
346 writeb(reg_save, db->io_addr);
347 spin_unlock_irqrestore(&db->lock,flags);
350 #ifdef CONFIG_NET_POLL_CONTROLLER
354 static void dm9000_poll_controller(struct net_device *dev)
356 disable_irq(dev->irq);
357 dm9000_interrupt(dev->irq,dev);
358 enable_irq(dev->irq);
362 /* dm9000_release_board
364 * release a board, and any mapped resources
368 dm9000_release_board(struct platform_device *pdev, struct board_info *db)
370 if (db->data_res == NULL) {
371 if (db->addr_res != NULL)
372 release_mem_region((unsigned long)db->io_addr, 4);
376 /* unmap our resources */
378 iounmap(db->io_addr);
379 iounmap(db->io_data);
381 /* release the resources */
383 if (db->data_req != NULL) {
384 release_resource(db->data_req);
388 if (db->addr_req != NULL) {
389 release_resource(db->addr_req);
394 #define res_size(_r) (((_r)->end - (_r)->start) + 1)
397 * Search DM9000 board, allocate space and register it
400 dm9000_probe(struct platform_device *pdev)
402 struct dm9000_plat_data *pdata = pdev->dev.platform_data;
403 struct board_info *db; /* Point a board information structure */
404 struct net_device *ndev;
411 /* Init network device */
412 ndev = alloc_etherdev(sizeof (struct board_info));
414 printk("%s: could not allocate device.\n", CARDNAME);
418 SET_NETDEV_DEV(ndev, &pdev->dev);
420 PRINTK2("dm9000_probe()");
422 /* setup board info structure */
423 db = (struct board_info *) ndev->priv;
424 memset(db, 0, sizeof (*db));
426 spin_lock_init(&db->lock);
428 if (pdev->num_resources < 2) {
431 } else if (pdev->num_resources == 2) {
432 base = pdev->resource[0].start;
434 if (!request_mem_region(base, 4, ndev->name)) {
439 ndev->base_addr = base;
440 ndev->irq = pdev->resource[1].start;
441 db->io_addr = (void __iomem *)base;
442 db->io_data = (void __iomem *)(base + 4);
444 /* ensure at least we have a default set of IO routines */
445 dm9000_set_io(db, 2);
448 db->addr_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
449 db->data_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
450 db->irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
452 if (db->addr_res == NULL || db->data_res == NULL ||
453 db->irq_res == NULL) {
454 printk(KERN_ERR PFX "insufficient resources\n");
459 i = res_size(db->addr_res);
460 db->addr_req = request_mem_region(db->addr_res->start, i,
463 if (db->addr_req == NULL) {
464 printk(KERN_ERR PFX "cannot claim address reg area\n");
469 db->io_addr = ioremap(db->addr_res->start, i);
471 if (db->io_addr == NULL) {
472 printk(KERN_ERR "failed to ioremap address reg\n");
477 iosize = res_size(db->data_res);
478 db->data_req = request_mem_region(db->data_res->start, iosize,
481 if (db->data_req == NULL) {
482 printk(KERN_ERR PFX "cannot claim data reg area\n");
487 db->io_data = ioremap(db->data_res->start, iosize);
489 if (db->io_data == NULL) {
490 printk(KERN_ERR "failed to ioremap data reg\n");
495 /* fill in parameters for net-dev structure */
497 ndev->base_addr = (unsigned long)db->io_addr;
498 ndev->irq = db->irq_res->start;
500 /* ensure at least we have a default set of IO routines */
501 dm9000_set_io(db, iosize);
504 /* check to see if anything is being over-ridden */
506 /* check to see if the driver wants to over-ride the
507 * default IO width */
509 if (pdata->flags & DM9000_PLATF_8BITONLY)
510 dm9000_set_io(db, 1);
512 if (pdata->flags & DM9000_PLATF_16BITONLY)
513 dm9000_set_io(db, 2);
515 if (pdata->flags & DM9000_PLATF_32BITONLY)
516 dm9000_set_io(db, 4);
518 /* check to see if there are any IO routine
521 if (pdata->inblk != NULL)
522 db->inblk = pdata->inblk;
524 if (pdata->outblk != NULL)
525 db->outblk = pdata->outblk;
527 if (pdata->dumpblk != NULL)
528 db->dumpblk = pdata->dumpblk;
530 db->flags = pdata->flags;
535 /* try two times, DM9000 sometimes gets the first read wrong */
536 for (i = 0; i < 2; i++) {
537 id_val = ior(db, DM9000_VIDL);
538 id_val |= (u32)ior(db, DM9000_VIDH) << 8;
539 id_val |= (u32)ior(db, DM9000_PIDL) << 16;
540 id_val |= (u32)ior(db, DM9000_PIDH) << 24;
542 if (id_val == DM9000_ID)
544 printk("%s: read wrong id 0x%08x\n", CARDNAME, id_val);
547 if (id_val != DM9000_ID) {
548 printk("%s: wrong id: 0x%08x\n", CARDNAME, id_val);
553 /* from this point we assume that we have found a DM9000 */
555 /* driver system function */
558 ndev->open = &dm9000_open;
559 ndev->hard_start_xmit = &dm9000_start_xmit;
560 ndev->tx_timeout = &dm9000_timeout;
561 ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
562 ndev->stop = &dm9000_stop;
563 ndev->set_multicast_list = &dm9000_hash_table;
564 #ifdef CONFIG_NET_POLL_CONTROLLER
565 ndev->poll_controller = &dm9000_poll_controller;
568 #ifdef DM9000_PROGRAM_EEPROM
571 db->msg_enable = NETIF_MSG_LINK;
572 db->mii.phy_id_mask = 0x1f;
573 db->mii.reg_num_mask = 0x1f;
574 db->mii.force_media = 0;
575 db->mii.full_duplex = 0;
577 db->mii.mdio_read = dm9000_phy_read;
578 db->mii.mdio_write = dm9000_phy_write;
580 /* Read SROM content */
581 for (i = 0; i < 64; i++)
582 ((u16 *) db->srom)[i] = read_srom_word(db, i);
584 /* Set Node Address */
585 for (i = 0; i < 6; i++)
586 ndev->dev_addr[i] = db->srom[i];
588 if (!is_valid_ether_addr(ndev->dev_addr)) {
589 /* try reading from mac */
591 for (i = 0; i < 6; i++)
592 ndev->dev_addr[i] = ior(db, i+DM9000_PAR);
595 if (!is_valid_ether_addr(ndev->dev_addr))
596 printk("%s: Invalid ethernet MAC address. Please "
597 "set using ifconfig\n", ndev->name);
599 platform_set_drvdata(pdev, ndev);
600 ret = register_netdev(ndev);
603 DECLARE_MAC_BUF(mac);
604 printk("%s: dm9000 at %p,%p IRQ %d MAC: %s\n",
605 ndev->name, db->io_addr, db->io_data, ndev->irq,
606 print_mac(mac, ndev->dev_addr));
611 printk("%s: not found (%d).\n", CARDNAME, ret);
613 dm9000_release_board(pdev, db);
620 * Open the interface.
621 * The interface is opened whenever "ifconfig" actives it.
624 dm9000_open(struct net_device *dev)
626 board_info_t *db = (board_info_t *) dev->priv;
628 PRINTK2("entering dm9000_open\n");
630 if (request_irq(dev->irq, &dm9000_interrupt, DM9000_IRQ_FLAGS, dev->name, dev))
633 /* Initialize DM9000 board */
635 dm9000_init_dm9000(dev);
637 /* Init driver variable */
640 /* set and active a timer process */
641 init_timer(&db->timer);
642 db->timer.expires = DM9000_TIMER_WUT;
643 db->timer.data = (unsigned long) dev;
644 db->timer.function = &dm9000_timer;
645 add_timer(&db->timer);
647 mii_check_media(&db->mii, netif_msg_link(db), 1);
648 netif_start_queue(dev);
654 * Initilize dm9000 board
657 dm9000_init_dm9000(struct net_device *dev)
659 board_info_t *db = (board_info_t *) dev->priv;
661 PRINTK1("entering %s\n",__FUNCTION__);
664 db->io_mode = ior(db, DM9000_ISR) >> 6; /* ISR bit7:6 keeps I/O mode */
666 /* GPIO0 on pre-activate PHY */
667 iow(db, DM9000_GPR, 0); /* REG_1F bit0 activate phyxcer */
668 iow(db, DM9000_GPCR, GPCR_GEP_CNTL); /* Let GPIO0 output */
669 iow(db, DM9000_GPR, 0); /* Enable PHY */
671 if (db->flags & DM9000_PLATF_EXT_PHY)
672 iow(db, DM9000_NCR, NCR_EXT_PHY);
674 /* Program operating register */
675 iow(db, DM9000_TCR, 0); /* TX Polling clear */
676 iow(db, DM9000_BPTR, 0x3f); /* Less 3Kb, 200us */
677 iow(db, DM9000_FCR, 0xff); /* Flow Control */
678 iow(db, DM9000_SMCR, 0); /* Special Mode */
679 /* clear TX status */
680 iow(db, DM9000_NSR, NSR_WAKEST | NSR_TX2END | NSR_TX1END);
681 iow(db, DM9000_ISR, ISR_CLR_STATUS); /* Clear interrupt status */
683 /* Set address filter table */
684 dm9000_hash_table(dev);
686 /* Activate DM9000 */
687 iow(db, DM9000_RCR, RCR_DIS_LONG | RCR_DIS_CRC | RCR_RXEN);
688 /* Enable TX/RX interrupt mask */
689 iow(db, DM9000_IMR, IMR_PAR | IMR_PTM | IMR_PRM);
691 /* Init Driver variable */
693 db->queue_pkt_len = 0;
694 dev->trans_start = 0;
698 * Hardware start transmission.
699 * Send a packet to media from the upper layer.
702 dm9000_start_xmit(struct sk_buff *skb, struct net_device *dev)
705 board_info_t *db = (board_info_t *) dev->priv;
707 PRINTK3("dm9000_start_xmit\n");
709 if (db->tx_pkt_cnt > 1)
712 spin_lock_irqsave(&db->lock, flags);
714 /* Move data to DM9000 TX RAM */
715 writeb(DM9000_MWCMD, db->io_addr);
717 (db->outblk)(db->io_data, skb->data, skb->len);
718 dev->stats.tx_bytes += skb->len;
721 /* TX control: First packet immediately send, second packet queue */
722 if (db->tx_pkt_cnt == 1) {
723 /* Set TX length to DM9000 */
724 iow(db, DM9000_TXPLL, skb->len & 0xff);
725 iow(db, DM9000_TXPLH, (skb->len >> 8) & 0xff);
727 /* Issue TX polling command */
728 iow(db, DM9000_TCR, TCR_TXREQ); /* Cleared after TX complete */
730 dev->trans_start = jiffies; /* save the time stamp */
733 db->queue_pkt_len = skb->len;
734 netif_stop_queue(dev);
737 spin_unlock_irqrestore(&db->lock, flags);
746 dm9000_shutdown(struct net_device *dev)
748 board_info_t *db = (board_info_t *) dev->priv;
751 dm9000_phy_write(dev, 0, MII_BMCR, BMCR_RESET); /* PHY RESET */
752 iow(db, DM9000_GPR, 0x01); /* Power-Down PHY */
753 iow(db, DM9000_IMR, IMR_PAR); /* Disable all interrupt */
754 iow(db, DM9000_RCR, 0x00); /* Disable RX */
758 * Stop the interface.
759 * The interface is stopped when it is brought.
762 dm9000_stop(struct net_device *ndev)
764 board_info_t *db = (board_info_t *) ndev->priv;
766 PRINTK1("entering %s\n",__FUNCTION__);
769 del_timer(&db->timer);
771 netif_stop_queue(ndev);
772 netif_carrier_off(ndev);
775 free_irq(ndev->irq, ndev);
777 dm9000_shutdown(ndev);
783 * DM9000 interrupt handler
784 * receive the packet to upper layer, free the transmitted packet
788 dm9000_tx_done(struct net_device *dev, board_info_t * db)
790 int tx_status = ior(db, DM9000_NSR); /* Got TX status */
792 if (tx_status & (NSR_TX2END | NSR_TX1END)) {
793 /* One packet sent complete */
795 dev->stats.tx_packets++;
797 /* Queue packet check & send */
798 if (db->tx_pkt_cnt > 0) {
799 iow(db, DM9000_TXPLL, db->queue_pkt_len & 0xff);
800 iow(db, DM9000_TXPLH, (db->queue_pkt_len >> 8) & 0xff);
801 iow(db, DM9000_TCR, TCR_TXREQ);
802 dev->trans_start = jiffies;
804 netif_wake_queue(dev);
809 dm9000_interrupt(int irq, void *dev_id)
811 struct net_device *dev = dev_id;
816 PRINTK3("entering %s\n",__FUNCTION__);
819 PRINTK1("dm9000_interrupt() without DEVICE arg\n");
823 /* A real interrupt coming */
824 db = (board_info_t *) dev->priv;
825 spin_lock(&db->lock);
827 /* Save previous register address */
828 reg_save = readb(db->io_addr);
830 /* Disable all interrupts */
831 iow(db, DM9000_IMR, IMR_PAR);
833 /* Got DM9000 interrupt status */
834 int_status = ior(db, DM9000_ISR); /* Got ISR */
835 iow(db, DM9000_ISR, int_status); /* Clear ISR status */
837 /* Received the coming packet */
838 if (int_status & ISR_PRS)
841 /* Trnasmit Interrupt check */
842 if (int_status & ISR_PTS)
843 dm9000_tx_done(dev, db);
845 /* Re-enable interrupt mask */
846 iow(db, DM9000_IMR, IMR_PAR | IMR_PTM | IMR_PRM);
848 /* Restore previous register address */
849 writeb(reg_save, db->io_addr);
851 spin_unlock(&db->lock);
857 * A periodic timer routine
858 * Dynamic media sense, allocated Rx buffer...
861 dm9000_timer(unsigned long data)
863 struct net_device *dev = (struct net_device *) data;
864 board_info_t *db = (board_info_t *) dev->priv;
866 PRINTK3("dm9000_timer()\n");
868 mii_check_media(&db->mii, netif_msg_link(db), 0);
870 /* Set timer again */
871 db->timer.expires = DM9000_TIMER_WUT;
872 add_timer(&db->timer);
875 struct dm9000_rxhdr {
879 } __attribute__((__packed__));
882 * Received a packet and pass to upper layer
885 dm9000_rx(struct net_device *dev)
887 board_info_t *db = (board_info_t *) dev->priv;
888 struct dm9000_rxhdr rxhdr;
894 /* Check packet ready or not */
896 ior(db, DM9000_MRCMDX); /* Dummy read */
898 /* Get most updated data */
899 rxbyte = readb(db->io_data);
901 /* Status check: this byte must be 0 or 1 */
902 if (rxbyte > DM9000_PKT_RDY) {
903 printk("status check failed: %d\n", rxbyte);
904 iow(db, DM9000_RCR, 0x00); /* Stop Device */
905 iow(db, DM9000_ISR, IMR_PAR); /* Stop INT request */
909 if (rxbyte != DM9000_PKT_RDY)
912 /* A packet ready now & Get status/length */
914 writeb(DM9000_MRCMD, db->io_addr);
916 (db->inblk)(db->io_data, &rxhdr, sizeof(rxhdr));
918 RxLen = le16_to_cpu(rxhdr.RxLen);
920 /* Packet Status check */
923 PRINTK1("Bad Packet received (runt)\n");
926 if (RxLen > DM9000_PKT_MAX) {
927 PRINTK1("RST: RX Len:%x\n", RxLen);
930 if (rxhdr.RxStatus & 0xbf) {
932 if (rxhdr.RxStatus & 0x01) {
933 PRINTK1("fifo error\n");
934 dev->stats.rx_fifo_errors++;
936 if (rxhdr.RxStatus & 0x02) {
937 PRINTK1("crc error\n");
938 dev->stats.rx_crc_errors++;
940 if (rxhdr.RxStatus & 0x80) {
941 PRINTK1("length error\n");
942 dev->stats.rx_length_errors++;
946 /* Move data from DM9000 */
948 && ((skb = dev_alloc_skb(RxLen + 4)) != NULL)) {
950 rdptr = (u8 *) skb_put(skb, RxLen - 4);
952 /* Read received packet from RX SRAM */
954 (db->inblk)(db->io_data, rdptr, RxLen);
955 dev->stats.rx_bytes += RxLen;
957 /* Pass to upper layer */
958 skb->protocol = eth_type_trans(skb, dev);
960 dev->stats.rx_packets++;
963 /* need to dump the packet's data */
965 (db->dumpblk)(db->io_data, RxLen);
967 } while (rxbyte == DM9000_PKT_RDY);
971 * Read a word data from SROM
974 read_srom_word(board_info_t * db, int offset)
976 iow(db, DM9000_EPAR, offset);
977 iow(db, DM9000_EPCR, EPCR_ERPRR);
978 mdelay(8); /* according to the datasheet 200us should be enough,
979 but it doesn't work */
980 iow(db, DM9000_EPCR, 0x0);
981 return (ior(db, DM9000_EPDRL) + (ior(db, DM9000_EPDRH) << 8));
984 #ifdef DM9000_PROGRAM_EEPROM
986 * Write a word data to SROM
989 write_srom_word(board_info_t * db, int offset, u16 val)
991 iow(db, DM9000_EPAR, offset);
992 iow(db, DM9000_EPDRH, ((val >> 8) & 0xff));
993 iow(db, DM9000_EPDRL, (val & 0xff));
994 iow(db, DM9000_EPCR, EPCR_WEP | EPCR_ERPRW);
995 mdelay(8); /* same shit */
996 iow(db, DM9000_EPCR, 0);
1000 * Only for development:
1001 * Here we write static data to the eeprom in case
1002 * we don't have valid content on a new board
1005 program_eeprom(board_info_t * db)
1007 u16 eeprom[] = { 0x0c00, 0x007f, 0x1300, /* MAC Address */
1008 0x0000, /* Autoload: accept nothing */
1009 0x0a46, 0x9000, /* Vendor / Product ID */
1010 0x0000, /* pin control */
1012 }; /* Wake-up mode control */
1014 for (i = 0; i < 8; i++)
1015 write_srom_word(db, i, eeprom[i]);
1021 * Calculate the CRC valude of the Rx packet
1022 * flag = 1 : return the reverse CRC (for the received packet CRC)
1023 * 0 : return the normal CRC (for Hash Table index)
1026 static unsigned long
1027 cal_CRC(unsigned char *Data, unsigned int Len, u8 flag)
1030 u32 crc = ether_crc_le(Len, Data);
1039 * Set DM9000 multicast address
1042 dm9000_hash_table(struct net_device *dev)
1044 board_info_t *db = (board_info_t *) dev->priv;
1045 struct dev_mc_list *mcptr = dev->mc_list;
1046 int mc_cnt = dev->mc_count;
1048 u16 i, oft, hash_table[4];
1049 unsigned long flags;
1051 PRINTK2("dm9000_hash_table()\n");
1053 spin_lock_irqsave(&db->lock,flags);
1055 for (i = 0, oft = 0x10; i < 6; i++, oft++)
1056 iow(db, oft, dev->dev_addr[i]);
1058 /* Clear Hash Table */
1059 for (i = 0; i < 4; i++)
1060 hash_table[i] = 0x0;
1062 /* broadcast address */
1063 hash_table[3] = 0x8000;
1065 /* the multicast address in Hash Table : 64 bits */
1066 for (i = 0; i < mc_cnt; i++, mcptr = mcptr->next) {
1067 hash_val = cal_CRC((char *) mcptr->dmi_addr, 6, 0) & 0x3f;
1068 hash_table[hash_val / 16] |= (u16) 1 << (hash_val % 16);
1071 /* Write the hash table to MAC MD table */
1072 for (i = 0, oft = 0x16; i < 4; i++) {
1073 iow(db, oft++, hash_table[i] & 0xff);
1074 iow(db, oft++, (hash_table[i] >> 8) & 0xff);
1077 spin_unlock_irqrestore(&db->lock,flags);
1082 * Read a word from phyxcer
1085 dm9000_phy_read(struct net_device *dev, int phy_reg_unused, int reg)
1087 board_info_t *db = (board_info_t *) dev->priv;
1088 unsigned long flags;
1089 unsigned int reg_save;
1092 spin_lock_irqsave(&db->lock,flags);
1094 /* Save previous register address */
1095 reg_save = readb(db->io_addr);
1097 /* Fill the phyxcer register into REG_0C */
1098 iow(db, DM9000_EPAR, DM9000_PHY | reg);
1100 iow(db, DM9000_EPCR, 0xc); /* Issue phyxcer read command */
1101 udelay(100); /* Wait read complete */
1102 iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer read command */
1104 /* The read data keeps on REG_0D & REG_0E */
1105 ret = (ior(db, DM9000_EPDRH) << 8) | ior(db, DM9000_EPDRL);
1107 /* restore the previous address */
1108 writeb(reg_save, db->io_addr);
1110 spin_unlock_irqrestore(&db->lock,flags);
1116 * Write a word to phyxcer
1119 dm9000_phy_write(struct net_device *dev, int phyaddr_unused, int reg, int value)
1121 board_info_t *db = (board_info_t *) dev->priv;
1122 unsigned long flags;
1123 unsigned long reg_save;
1125 spin_lock_irqsave(&db->lock,flags);
1127 /* Save previous register address */
1128 reg_save = readb(db->io_addr);
1130 /* Fill the phyxcer register into REG_0C */
1131 iow(db, DM9000_EPAR, DM9000_PHY | reg);
1133 /* Fill the written data into REG_0D & REG_0E */
1134 iow(db, DM9000_EPDRL, (value & 0xff));
1135 iow(db, DM9000_EPDRH, ((value >> 8) & 0xff));
1137 iow(db, DM9000_EPCR, 0xa); /* Issue phyxcer write command */
1138 udelay(500); /* Wait write complete */
1139 iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer write command */
1141 /* restore the previous address */
1142 writeb(reg_save, db->io_addr);
1144 spin_unlock_irqrestore(&db->lock,flags);
1148 dm9000_drv_suspend(struct platform_device *dev, pm_message_t state)
1150 struct net_device *ndev = platform_get_drvdata(dev);
1153 if (netif_running(ndev)) {
1154 netif_device_detach(ndev);
1155 dm9000_shutdown(ndev);
1162 dm9000_drv_resume(struct platform_device *dev)
1164 struct net_device *ndev = platform_get_drvdata(dev);
1165 board_info_t *db = (board_info_t *) ndev->priv;
1169 if (netif_running(ndev)) {
1171 dm9000_init_dm9000(ndev);
1173 netif_device_attach(ndev);
1180 dm9000_drv_remove(struct platform_device *pdev)
1182 struct net_device *ndev = platform_get_drvdata(pdev);
1184 platform_set_drvdata(pdev, NULL);
1186 unregister_netdev(ndev);
1187 dm9000_release_board(pdev, (board_info_t *) ndev->priv);
1188 free_netdev(ndev); /* free device structure */
1190 PRINTK1("clean_module() exit\n");
1195 static struct platform_driver dm9000_driver = {
1198 .owner = THIS_MODULE,
1200 .probe = dm9000_probe,
1201 .remove = dm9000_drv_remove,
1202 .suspend = dm9000_drv_suspend,
1203 .resume = dm9000_drv_resume,
1209 printk(KERN_INFO "%s Ethernet Driver\n", CARDNAME);
1211 return platform_driver_register(&dm9000_driver); /* search board and register */
1215 dm9000_cleanup(void)
1217 platform_driver_unregister(&dm9000_driver);
1220 module_init(dm9000_init);
1221 module_exit(dm9000_cleanup);
1223 MODULE_AUTHOR("Sascha Hauer, Ben Dooks");
1224 MODULE_DESCRIPTION("Davicom DM9000 network driver");
1225 MODULE_LICENSE("GPL");