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 #ifdef CONFIG_BLACKFIN
91 #define DEFAULT_TRIGGER IRQF_TRIGGER_HIGH
93 #define DEFAULT_TRIGGER (0)
97 * Transmit timeout, default 5 seconds.
99 static int watchdog = 5000;
100 module_param(watchdog, int, 0400);
101 MODULE_PARM_DESC(watchdog, "transmit timeout in milliseconds");
103 /* Structure/enum declaration ------------------------------- */
104 typedef struct board_info {
106 void __iomem *io_addr; /* Register I/O base address */
107 void __iomem *io_data; /* Data I/O address */
112 u16 queue_start_addr;
114 u8 io_mode; /* 0:word, 2:byte */
120 void (*inblk)(void __iomem *port, void *data, int length);
121 void (*outblk)(void __iomem *port, void *data, int length);
122 void (*dumpblk)(void __iomem *port, int length);
124 struct device *dev; /* parent device */
126 struct resource *addr_res; /* resources found */
127 struct resource *data_res;
128 struct resource *addr_req; /* resources requested */
129 struct resource *data_req;
130 struct resource *irq_res;
132 unsigned char srom[128];
135 struct mii_if_info mii;
141 #define dm9000_dbg(db, lev, msg...) do { \
142 if ((lev) < CONFIG_DM9000_DEBUGLEVEL && \
143 (lev) < db->debug_level) { \
144 dev_dbg(db->dev, msg); \
148 /* function declaration ------------------------------------- */
149 static int dm9000_probe(struct platform_device *);
150 static int dm9000_open(struct net_device *);
151 static int dm9000_start_xmit(struct sk_buff *, struct net_device *);
152 static int dm9000_stop(struct net_device *);
154 static void dm9000_init_dm9000(struct net_device *);
156 static irqreturn_t dm9000_interrupt(int, void *);
158 static int dm9000_phy_read(struct net_device *dev, int phyaddr_unsused, int reg);
159 static void dm9000_phy_write(struct net_device *dev, int phyaddr_unused, int reg,
161 static u16 read_srom_word(board_info_t *, int);
162 static void dm9000_rx(struct net_device *);
163 static void dm9000_hash_table(struct net_device *);
165 //#define DM9000_PROGRAM_EEPROM
166 #ifdef DM9000_PROGRAM_EEPROM
167 static void program_eeprom(board_info_t * db);
169 /* DM9000 network board routine ---------------------------- */
172 dm9000_reset(board_info_t * db)
174 dev_dbg(db->dev, "resetting device\n");
177 writeb(DM9000_NCR, db->io_addr);
179 writeb(NCR_RST, db->io_data);
184 * Read a byte from I/O port
187 ior(board_info_t * db, int reg)
189 writeb(reg, db->io_addr);
190 return readb(db->io_data);
194 * Write a byte to I/O port
198 iow(board_info_t * db, int reg, int value)
200 writeb(reg, db->io_addr);
201 writeb(value, db->io_data);
204 /* routines for sending block to chip */
206 static void dm9000_outblk_8bit(void __iomem *reg, void *data, int count)
208 writesb(reg, data, count);
211 static void dm9000_outblk_16bit(void __iomem *reg, void *data, int count)
213 writesw(reg, data, (count+1) >> 1);
216 static void dm9000_outblk_32bit(void __iomem *reg, void *data, int count)
218 writesl(reg, data, (count+3) >> 2);
221 /* input block from chip to memory */
223 static void dm9000_inblk_8bit(void __iomem *reg, void *data, int count)
225 readsb(reg, data, count);
229 static void dm9000_inblk_16bit(void __iomem *reg, void *data, int count)
231 readsw(reg, data, (count+1) >> 1);
234 static void dm9000_inblk_32bit(void __iomem *reg, void *data, int count)
236 readsl(reg, data, (count+3) >> 2);
239 /* dump block from chip to null */
241 static void dm9000_dumpblk_8bit(void __iomem *reg, int count)
246 for (i = 0; i < count; i++)
250 static void dm9000_dumpblk_16bit(void __iomem *reg, int count)
255 count = (count + 1) >> 1;
257 for (i = 0; i < count; i++)
261 static void dm9000_dumpblk_32bit(void __iomem *reg, int count)
266 count = (count + 3) >> 2;
268 for (i = 0; i < count; i++)
274 * select the specified set of io routines to use with the
278 static void dm9000_set_io(struct board_info *db, int byte_width)
280 /* use the size of the data resource to work out what IO
281 * routines we want to use
284 switch (byte_width) {
286 db->dumpblk = dm9000_dumpblk_8bit;
287 db->outblk = dm9000_outblk_8bit;
288 db->inblk = dm9000_inblk_8bit;
293 dev_dbg(db->dev, ": 3 byte IO, falling back to 16bit\n");
295 db->dumpblk = dm9000_dumpblk_16bit;
296 db->outblk = dm9000_outblk_16bit;
297 db->inblk = dm9000_inblk_16bit;
302 db->dumpblk = dm9000_dumpblk_32bit;
303 db->outblk = dm9000_outblk_32bit;
304 db->inblk = dm9000_inblk_32bit;
310 /* Our watchdog timed out. Called by the networking layer */
311 static void dm9000_timeout(struct net_device *dev)
313 board_info_t *db = (board_info_t *) dev->priv;
317 /* Save previous register address */
318 reg_save = readb(db->io_addr);
319 spin_lock_irqsave(&db->lock,flags);
321 netif_stop_queue(dev);
323 dm9000_init_dm9000(dev);
324 /* We can accept TX packets again */
325 dev->trans_start = jiffies;
326 netif_wake_queue(dev);
328 /* Restore previous register address */
329 writeb(reg_save, db->io_addr);
330 spin_unlock_irqrestore(&db->lock,flags);
333 #ifdef CONFIG_NET_POLL_CONTROLLER
337 static void dm9000_poll_controller(struct net_device *dev)
339 disable_irq(dev->irq);
340 dm9000_interrupt(dev->irq,dev);
341 enable_irq(dev->irq);
345 /* dm9000_release_board
347 * release a board, and any mapped resources
351 dm9000_release_board(struct platform_device *pdev, struct board_info *db)
353 if (db->data_res == NULL) {
354 if (db->addr_res != NULL)
355 release_mem_region((unsigned long)db->io_addr, 4);
359 /* unmap our resources */
361 iounmap(db->io_addr);
362 iounmap(db->io_data);
364 /* release the resources */
366 if (db->data_req != NULL) {
367 release_resource(db->data_req);
371 if (db->addr_req != NULL) {
372 release_resource(db->addr_req);
377 #define res_size(_r) (((_r)->end - (_r)->start) + 1)
380 * Search DM9000 board, allocate space and register it
383 dm9000_probe(struct platform_device *pdev)
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;
394 /* Init network device */
395 ndev = alloc_etherdev(sizeof (struct board_info));
397 dev_err(&pdev->dev, "could not allocate device.\n");
401 SET_NETDEV_DEV(ndev, &pdev->dev);
403 dev_dbg(&pdev->dev, "dm9000_probe()");
405 /* setup board info structure */
406 db = (struct board_info *) ndev->priv;
407 memset(db, 0, sizeof (*db));
409 db->dev = &pdev->dev;
411 spin_lock_init(&db->lock);
413 if (pdev->num_resources < 2) {
416 } else if (pdev->num_resources == 2) {
417 base = pdev->resource[0].start;
419 if (!request_mem_region(base, 4, ndev->name)) {
424 ndev->base_addr = base;
425 ndev->irq = pdev->resource[1].start;
426 db->io_addr = (void __iomem *)base;
427 db->io_data = (void __iomem *)(base + 4);
429 /* ensure at least we have a default set of IO routines */
430 dm9000_set_io(db, 2);
433 db->addr_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
434 db->data_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
435 db->irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
437 if (db->addr_res == NULL || db->data_res == NULL ||
438 db->irq_res == NULL) {
439 dev_err(db->dev, "insufficient resources\n");
444 i = res_size(db->addr_res);
445 db->addr_req = request_mem_region(db->addr_res->start, i,
448 if (db->addr_req == NULL) {
449 dev_err(db->dev, "cannot claim address reg area\n");
454 db->io_addr = ioremap(db->addr_res->start, i);
456 if (db->io_addr == NULL) {
457 dev_err(db->dev, "failed to ioremap address reg\n");
462 iosize = res_size(db->data_res);
463 db->data_req = request_mem_region(db->data_res->start, iosize,
466 if (db->data_req == NULL) {
467 dev_err(db->dev, "cannot claim data reg area\n");
472 db->io_data = ioremap(db->data_res->start, iosize);
474 if (db->io_data == NULL) {
475 dev_err(db->dev,"failed to ioremap data reg\n");
480 /* fill in parameters for net-dev structure */
482 ndev->base_addr = (unsigned long)db->io_addr;
483 ndev->irq = db->irq_res->start;
485 /* ensure at least we have a default set of IO routines */
486 dm9000_set_io(db, iosize);
489 /* check to see if anything is being over-ridden */
491 /* check to see if the driver wants to over-ride the
492 * default IO width */
494 if (pdata->flags & DM9000_PLATF_8BITONLY)
495 dm9000_set_io(db, 1);
497 if (pdata->flags & DM9000_PLATF_16BITONLY)
498 dm9000_set_io(db, 2);
500 if (pdata->flags & DM9000_PLATF_32BITONLY)
501 dm9000_set_io(db, 4);
503 /* check to see if there are any IO routine
506 if (pdata->inblk != NULL)
507 db->inblk = pdata->inblk;
509 if (pdata->outblk != NULL)
510 db->outblk = pdata->outblk;
512 if (pdata->dumpblk != NULL)
513 db->dumpblk = pdata->dumpblk;
515 db->flags = pdata->flags;
520 /* try two times, DM9000 sometimes gets the first read wrong */
521 for (i = 0; i < 2; i++) {
522 id_val = ior(db, DM9000_VIDL);
523 id_val |= (u32)ior(db, DM9000_VIDH) << 8;
524 id_val |= (u32)ior(db, DM9000_PIDL) << 16;
525 id_val |= (u32)ior(db, DM9000_PIDH) << 24;
527 if (id_val == DM9000_ID)
529 dev_err(db->dev, "read wrong id 0x%08x\n", id_val);
532 if (id_val != DM9000_ID) {
533 dev_err(db->dev, "wrong id: 0x%08x\n", id_val);
538 /* from this point we assume that we have found a DM9000 */
540 /* driver system function */
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->set_multicast_list = &dm9000_hash_table;
549 #ifdef CONFIG_NET_POLL_CONTROLLER
550 ndev->poll_controller = &dm9000_poll_controller;
553 #ifdef DM9000_PROGRAM_EEPROM
556 db->msg_enable = NETIF_MSG_LINK;
557 db->mii.phy_id_mask = 0x1f;
558 db->mii.reg_num_mask = 0x1f;
559 db->mii.force_media = 0;
560 db->mii.full_duplex = 0;
562 db->mii.mdio_read = dm9000_phy_read;
563 db->mii.mdio_write = dm9000_phy_write;
565 /* Read SROM content */
566 for (i = 0; i < 64; i++)
567 ((u16 *) db->srom)[i] = read_srom_word(db, i);
569 /* Set Node Address */
570 for (i = 0; i < 6; i++)
571 ndev->dev_addr[i] = db->srom[i];
573 if (!is_valid_ether_addr(ndev->dev_addr)) {
574 /* try reading from mac */
576 for (i = 0; i < 6; i++)
577 ndev->dev_addr[i] = ior(db, i+DM9000_PAR);
580 if (!is_valid_ether_addr(ndev->dev_addr))
581 dev_warn(db->dev, "%s: Invalid ethernet MAC address. Please "
582 "set using ifconfig\n", ndev->name);
584 platform_set_drvdata(pdev, ndev);
585 ret = register_netdev(ndev);
588 DECLARE_MAC_BUF(mac);
589 printk("%s: dm9000 at %p,%p IRQ %d MAC: %s\n",
590 ndev->name, db->io_addr, db->io_data, ndev->irq,
591 print_mac(mac, ndev->dev_addr));
596 dev_err(db->dev, "not found (%d).\n", ret);
598 dm9000_release_board(pdev, db);
605 * Open the interface.
606 * The interface is opened whenever "ifconfig" actives it.
609 dm9000_open(struct net_device *dev)
611 board_info_t *db = (board_info_t *) dev->priv;
612 unsigned long irqflags = db->irq_res->flags & IRQF_TRIGGER_MASK;
614 dev_dbg(db->dev, "entering %s\n", __func__);
616 /* If there is no IRQ type specified, default to something that
617 * may work, and tell the user that this is a problem */
619 if (irqflags == IRQF_TRIGGER_NONE) {
620 dev_warn(db->dev, "WARNING: no IRQ resource flags set.\n");
621 irqflags = DEFAULT_TRIGGER;
624 irqflags |= IRQF_SHARED;
626 if (request_irq(dev->irq, &dm9000_interrupt, irqflags, dev->name, dev))
629 /* Initialize DM9000 board */
631 dm9000_init_dm9000(dev);
633 /* Init driver variable */
636 mii_check_media(&db->mii, netif_msg_link(db), 1);
637 netif_start_queue(dev);
643 * Initilize dm9000 board
646 dm9000_init_dm9000(struct net_device *dev)
648 board_info_t *db = (board_info_t *) dev->priv;
650 dm9000_dbg(db, 1, "entering %s\n", __func__);
653 db->io_mode = ior(db, DM9000_ISR) >> 6; /* ISR bit7:6 keeps I/O mode */
655 /* GPIO0 on pre-activate PHY */
656 iow(db, DM9000_GPR, 0); /* REG_1F bit0 activate phyxcer */
657 iow(db, DM9000_GPCR, GPCR_GEP_CNTL); /* Let GPIO0 output */
658 iow(db, DM9000_GPR, 0); /* Enable PHY */
660 if (db->flags & DM9000_PLATF_EXT_PHY)
661 iow(db, DM9000_NCR, NCR_EXT_PHY);
663 /* Program operating register */
664 iow(db, DM9000_TCR, 0); /* TX Polling clear */
665 iow(db, DM9000_BPTR, 0x3f); /* Less 3Kb, 200us */
666 iow(db, DM9000_FCR, 0xff); /* Flow Control */
667 iow(db, DM9000_SMCR, 0); /* Special Mode */
668 /* clear TX status */
669 iow(db, DM9000_NSR, NSR_WAKEST | NSR_TX2END | NSR_TX1END);
670 iow(db, DM9000_ISR, ISR_CLR_STATUS); /* Clear interrupt status */
672 /* Set address filter table */
673 dm9000_hash_table(dev);
675 /* Activate DM9000 */
676 iow(db, DM9000_RCR, RCR_DIS_LONG | RCR_DIS_CRC | RCR_RXEN);
677 /* Enable TX/RX interrupt mask */
678 iow(db, DM9000_IMR, IMR_PAR | IMR_PTM | IMR_PRM);
680 /* Init Driver variable */
682 db->queue_pkt_len = 0;
683 dev->trans_start = 0;
687 * Hardware start transmission.
688 * Send a packet to media from the upper layer.
691 dm9000_start_xmit(struct sk_buff *skb, struct net_device *dev)
694 board_info_t *db = (board_info_t *) dev->priv;
696 dm9000_dbg(db, 3, "%s:\n", __func__);
698 if (db->tx_pkt_cnt > 1)
701 spin_lock_irqsave(&db->lock, flags);
703 /* Move data to DM9000 TX RAM */
704 writeb(DM9000_MWCMD, db->io_addr);
706 (db->outblk)(db->io_data, skb->data, skb->len);
707 dev->stats.tx_bytes += skb->len;
710 /* TX control: First packet immediately send, second packet queue */
711 if (db->tx_pkt_cnt == 1) {
712 /* Set TX length to DM9000 */
713 iow(db, DM9000_TXPLL, skb->len & 0xff);
714 iow(db, DM9000_TXPLH, (skb->len >> 8) & 0xff);
716 /* Issue TX polling command */
717 iow(db, DM9000_TCR, TCR_TXREQ); /* Cleared after TX complete */
719 dev->trans_start = jiffies; /* save the time stamp */
722 db->queue_pkt_len = skb->len;
723 netif_stop_queue(dev);
726 spin_unlock_irqrestore(&db->lock, flags);
735 dm9000_shutdown(struct net_device *dev)
737 board_info_t *db = (board_info_t *) dev->priv;
740 dm9000_phy_write(dev, 0, MII_BMCR, BMCR_RESET); /* PHY RESET */
741 iow(db, DM9000_GPR, 0x01); /* Power-Down PHY */
742 iow(db, DM9000_IMR, IMR_PAR); /* Disable all interrupt */
743 iow(db, DM9000_RCR, 0x00); /* Disable RX */
747 * Stop the interface.
748 * The interface is stopped when it is brought.
751 dm9000_stop(struct net_device *ndev)
753 board_info_t *db = (board_info_t *) ndev->priv;
755 dm9000_dbg(db, 1, "entering %s\n", __func__);
757 netif_stop_queue(ndev);
758 netif_carrier_off(ndev);
761 free_irq(ndev->irq, ndev);
763 dm9000_shutdown(ndev);
769 * DM9000 interrupt handler
770 * receive the packet to upper layer, free the transmitted packet
774 dm9000_tx_done(struct net_device *dev, board_info_t * db)
776 int tx_status = ior(db, DM9000_NSR); /* Got TX status */
778 if (tx_status & (NSR_TX2END | NSR_TX1END)) {
779 /* One packet sent complete */
781 dev->stats.tx_packets++;
783 /* Queue packet check & send */
784 if (db->tx_pkt_cnt > 0) {
785 iow(db, DM9000_TXPLL, db->queue_pkt_len & 0xff);
786 iow(db, DM9000_TXPLH, (db->queue_pkt_len >> 8) & 0xff);
787 iow(db, DM9000_TCR, TCR_TXREQ);
788 dev->trans_start = jiffies;
790 netif_wake_queue(dev);
795 dm9000_interrupt(int irq, void *dev_id)
797 struct net_device *dev = dev_id;
798 board_info_t *db = (board_info_t *) dev->priv;
802 dm9000_dbg(db, 3, "entering %s\n", __func__);
804 /* A real interrupt coming */
806 spin_lock(&db->lock);
808 /* Save previous register address */
809 reg_save = readb(db->io_addr);
811 /* Disable all interrupts */
812 iow(db, DM9000_IMR, IMR_PAR);
814 /* Got DM9000 interrupt status */
815 int_status = ior(db, DM9000_ISR); /* Got ISR */
816 iow(db, DM9000_ISR, int_status); /* Clear ISR status */
818 /* Received the coming packet */
819 if (int_status & ISR_PRS)
822 /* Trnasmit Interrupt check */
823 if (int_status & ISR_PTS)
824 dm9000_tx_done(dev, db);
826 /* Re-enable interrupt mask */
827 iow(db, DM9000_IMR, IMR_PAR | IMR_PTM | IMR_PRM);
829 /* Restore previous register address */
830 writeb(reg_save, db->io_addr);
832 spin_unlock(&db->lock);
837 struct dm9000_rxhdr {
841 } __attribute__((__packed__));
844 * Received a packet and pass to upper layer
847 dm9000_rx(struct net_device *dev)
849 board_info_t *db = (board_info_t *) dev->priv;
850 struct dm9000_rxhdr rxhdr;
856 /* Check packet ready or not */
858 ior(db, DM9000_MRCMDX); /* Dummy read */
860 /* Get most updated data */
861 rxbyte = readb(db->io_data);
863 /* Status check: this byte must be 0 or 1 */
864 if (rxbyte > DM9000_PKT_RDY) {
865 dev_warn(db->dev, "status check fail: %d\n", rxbyte);
866 iow(db, DM9000_RCR, 0x00); /* Stop Device */
867 iow(db, DM9000_ISR, IMR_PAR); /* Stop INT request */
871 if (rxbyte != DM9000_PKT_RDY)
874 /* A packet ready now & Get status/length */
876 writeb(DM9000_MRCMD, db->io_addr);
878 (db->inblk)(db->io_data, &rxhdr, sizeof(rxhdr));
880 RxLen = le16_to_cpu(rxhdr.RxLen);
882 /* Packet Status check */
885 dev_dbg(db->dev, "Bad Packet received (runt)\n");
888 if (RxLen > DM9000_PKT_MAX) {
889 dev_dbg(db->dev, "RST: RX Len:%x\n", RxLen);
892 if (rxhdr.RxStatus & 0xbf) {
894 if (rxhdr.RxStatus & 0x01) {
895 dev_dbg(db->dev, "fifo error\n");
896 dev->stats.rx_fifo_errors++;
898 if (rxhdr.RxStatus & 0x02) {
899 dev_dbg(db->dev, "crc error\n");
900 dev->stats.rx_crc_errors++;
902 if (rxhdr.RxStatus & 0x80) {
903 dev_dbg(db->dev, "length error\n");
904 dev->stats.rx_length_errors++;
908 /* Move data from DM9000 */
910 && ((skb = dev_alloc_skb(RxLen + 4)) != NULL)) {
912 rdptr = (u8 *) skb_put(skb, RxLen - 4);
914 /* Read received packet from RX SRAM */
916 (db->inblk)(db->io_data, rdptr, RxLen);
917 dev->stats.rx_bytes += RxLen;
919 /* Pass to upper layer */
920 skb->protocol = eth_type_trans(skb, dev);
922 dev->stats.rx_packets++;
925 /* need to dump the packet's data */
927 (db->dumpblk)(db->io_data, RxLen);
929 } while (rxbyte == DM9000_PKT_RDY);
933 * Read a word data from SROM
936 read_srom_word(board_info_t * db, int offset)
938 iow(db, DM9000_EPAR, offset);
939 iow(db, DM9000_EPCR, EPCR_ERPRR);
940 mdelay(8); /* according to the datasheet 200us should be enough,
941 but it doesn't work */
942 iow(db, DM9000_EPCR, 0x0);
943 return (ior(db, DM9000_EPDRL) + (ior(db, DM9000_EPDRH) << 8));
946 #ifdef DM9000_PROGRAM_EEPROM
948 * Write a word data to SROM
951 write_srom_word(board_info_t * db, int offset, u16 val)
953 iow(db, DM9000_EPAR, offset);
954 iow(db, DM9000_EPDRH, ((val >> 8) & 0xff));
955 iow(db, DM9000_EPDRL, (val & 0xff));
956 iow(db, DM9000_EPCR, EPCR_WEP | EPCR_ERPRW);
957 mdelay(8); /* same shit */
958 iow(db, DM9000_EPCR, 0);
962 * Only for development:
963 * Here we write static data to the eeprom in case
964 * we don't have valid content on a new board
967 program_eeprom(board_info_t * db)
969 u16 eeprom[] = { 0x0c00, 0x007f, 0x1300, /* MAC Address */
970 0x0000, /* Autoload: accept nothing */
971 0x0a46, 0x9000, /* Vendor / Product ID */
972 0x0000, /* pin control */
974 }; /* Wake-up mode control */
976 for (i = 0; i < 8; i++)
977 write_srom_word(db, i, eeprom[i]);
983 * Calculate the CRC valude of the Rx packet
984 * flag = 1 : return the reverse CRC (for the received packet CRC)
985 * 0 : return the normal CRC (for Hash Table index)
989 cal_CRC(unsigned char *Data, unsigned int Len, u8 flag)
992 u32 crc = ether_crc_le(Len, Data);
1001 * Set DM9000 multicast address
1004 dm9000_hash_table(struct net_device *dev)
1006 board_info_t *db = (board_info_t *) dev->priv;
1007 struct dev_mc_list *mcptr = dev->mc_list;
1008 int mc_cnt = dev->mc_count;
1010 u16 i, oft, hash_table[4];
1011 unsigned long flags;
1013 dm9000_dbg(db, 1, "entering %s\n", __func__);
1015 spin_lock_irqsave(&db->lock,flags);
1017 for (i = 0, oft = 0x10; i < 6; i++, oft++)
1018 iow(db, oft, dev->dev_addr[i]);
1020 /* Clear Hash Table */
1021 for (i = 0; i < 4; i++)
1022 hash_table[i] = 0x0;
1024 /* broadcast address */
1025 hash_table[3] = 0x8000;
1027 /* the multicast address in Hash Table : 64 bits */
1028 for (i = 0; i < mc_cnt; i++, mcptr = mcptr->next) {
1029 hash_val = cal_CRC((char *) mcptr->dmi_addr, 6, 0) & 0x3f;
1030 hash_table[hash_val / 16] |= (u16) 1 << (hash_val % 16);
1033 /* Write the hash table to MAC MD table */
1034 for (i = 0, oft = 0x16; i < 4; i++) {
1035 iow(db, oft++, hash_table[i] & 0xff);
1036 iow(db, oft++, (hash_table[i] >> 8) & 0xff);
1039 spin_unlock_irqrestore(&db->lock,flags);
1044 * Read a word from phyxcer
1047 dm9000_phy_read(struct net_device *dev, int phy_reg_unused, int reg)
1049 board_info_t *db = (board_info_t *) dev->priv;
1050 unsigned long flags;
1051 unsigned int reg_save;
1054 spin_lock_irqsave(&db->lock,flags);
1056 /* Save previous register address */
1057 reg_save = readb(db->io_addr);
1059 /* Fill the phyxcer register into REG_0C */
1060 iow(db, DM9000_EPAR, DM9000_PHY | reg);
1062 iow(db, DM9000_EPCR, 0xc); /* Issue phyxcer read command */
1063 udelay(100); /* Wait read complete */
1064 iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer read command */
1066 /* The read data keeps on REG_0D & REG_0E */
1067 ret = (ior(db, DM9000_EPDRH) << 8) | ior(db, DM9000_EPDRL);
1069 /* restore the previous address */
1070 writeb(reg_save, db->io_addr);
1072 spin_unlock_irqrestore(&db->lock,flags);
1078 * Write a word to phyxcer
1081 dm9000_phy_write(struct net_device *dev, int phyaddr_unused, int reg, int value)
1083 board_info_t *db = (board_info_t *) dev->priv;
1084 unsigned long flags;
1085 unsigned long reg_save;
1087 spin_lock_irqsave(&db->lock,flags);
1089 /* Save previous register address */
1090 reg_save = readb(db->io_addr);
1092 /* Fill the phyxcer register into REG_0C */
1093 iow(db, DM9000_EPAR, DM9000_PHY | reg);
1095 /* Fill the written data into REG_0D & REG_0E */
1096 iow(db, DM9000_EPDRL, (value & 0xff));
1097 iow(db, DM9000_EPDRH, ((value >> 8) & 0xff));
1099 iow(db, DM9000_EPCR, 0xa); /* Issue phyxcer write command */
1100 udelay(500); /* Wait write complete */
1101 iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer write command */
1103 /* restore the previous address */
1104 writeb(reg_save, db->io_addr);
1106 spin_unlock_irqrestore(&db->lock,flags);
1110 dm9000_drv_suspend(struct platform_device *dev, pm_message_t state)
1112 struct net_device *ndev = platform_get_drvdata(dev);
1115 if (netif_running(ndev)) {
1116 netif_device_detach(ndev);
1117 dm9000_shutdown(ndev);
1124 dm9000_drv_resume(struct platform_device *dev)
1126 struct net_device *ndev = platform_get_drvdata(dev);
1127 board_info_t *db = (board_info_t *) ndev->priv;
1131 if (netif_running(ndev)) {
1133 dm9000_init_dm9000(ndev);
1135 netif_device_attach(ndev);
1142 dm9000_drv_remove(struct platform_device *pdev)
1144 struct net_device *ndev = platform_get_drvdata(pdev);
1146 platform_set_drvdata(pdev, NULL);
1148 unregister_netdev(ndev);
1149 dm9000_release_board(pdev, (board_info_t *) ndev->priv);
1150 free_netdev(ndev); /* free device structure */
1152 dev_dbg(&pdev->dev, "released and freed device\n");
1156 static struct platform_driver dm9000_driver = {
1159 .owner = THIS_MODULE,
1161 .probe = dm9000_probe,
1162 .remove = dm9000_drv_remove,
1163 .suspend = dm9000_drv_suspend,
1164 .resume = dm9000_drv_resume,
1170 printk(KERN_INFO "%s Ethernet Driver\n", CARDNAME);
1172 return platform_driver_register(&dm9000_driver); /* search board and register */
1176 dm9000_cleanup(void)
1178 platform_driver_unregister(&dm9000_driver);
1181 module_init(dm9000_init);
1182 module_exit(dm9000_cleanup);
1184 MODULE_AUTHOR("Sascha Hauer, Ben Dooks");
1185 MODULE_DESCRIPTION("Davicom DM9000 network driver");
1186 MODULE_LICENSE("GPL");