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 #ifdef CONFIG_BLACKFIN
93 #define DM9000_IRQ_FLAGS (IRQF_SHARED | IRQF_TRIGGER_HIGH)
95 #define DM9000_IRQ_FLAGS (IRQF_SHARED | IRQT_RISING)
99 * Transmit timeout, default 5 seconds.
101 static int watchdog = 5000;
102 module_param(watchdog, int, 0400);
103 MODULE_PARM_DESC(watchdog, "transmit timeout in milliseconds");
105 /* Structure/enum declaration ------------------------------- */
106 typedef struct board_info {
108 void __iomem *io_addr; /* Register I/O base address */
109 void __iomem *io_data; /* Data I/O address */
114 u16 queue_start_addr;
116 u8 io_mode; /* 0:word, 2:byte */
122 void (*inblk)(void __iomem *port, void *data, int length);
123 void (*outblk)(void __iomem *port, void *data, int length);
124 void (*dumpblk)(void __iomem *port, int length);
126 struct device *dev; /* parent device */
128 struct resource *addr_res; /* resources found */
129 struct resource *data_res;
130 struct resource *addr_req; /* resources requested */
131 struct resource *data_req;
132 struct resource *irq_res;
134 struct timer_list timer;
135 unsigned char srom[128];
138 struct mii_if_info mii;
144 #define dm9000_dbg(db, lev, msg...) do { \
145 if ((lev) < CONFIG_DM9000_DEBUGLEVEL && \
146 (lev) < db->debug_level) { \
147 dev_dbg(db->dev, msg); \
151 /* function declaration ------------------------------------- */
152 static int dm9000_probe(struct platform_device *);
153 static int dm9000_open(struct net_device *);
154 static int dm9000_start_xmit(struct sk_buff *, struct net_device *);
155 static int dm9000_stop(struct net_device *);
158 static void dm9000_timer(unsigned long);
159 static void dm9000_init_dm9000(struct net_device *);
161 static irqreturn_t dm9000_interrupt(int, void *);
163 static int dm9000_phy_read(struct net_device *dev, int phyaddr_unsused, int reg);
164 static void dm9000_phy_write(struct net_device *dev, int phyaddr_unused, int reg,
166 static u16 read_srom_word(board_info_t *, int);
167 static void dm9000_rx(struct net_device *);
168 static void dm9000_hash_table(struct net_device *);
170 //#define DM9000_PROGRAM_EEPROM
171 #ifdef DM9000_PROGRAM_EEPROM
172 static void program_eeprom(board_info_t * db);
174 /* DM9000 network board routine ---------------------------- */
177 dm9000_reset(board_info_t * db)
179 dev_dbg(db->dev, "resetting device\n");
182 writeb(DM9000_NCR, db->io_addr);
184 writeb(NCR_RST, db->io_data);
189 * Read a byte from I/O port
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
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)
251 for (i = 0; i < count; i++)
255 static void dm9000_dumpblk_16bit(void __iomem *reg, int count)
260 count = (count + 1) >> 1;
262 for (i = 0; i < count; i++)
266 static void dm9000_dumpblk_32bit(void __iomem *reg, int count)
271 count = (count + 3) >> 2;
273 for (i = 0; i < count; i++)
279 * select the specified set of io routines to use with the
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) {
291 db->dumpblk = dm9000_dumpblk_8bit;
292 db->outblk = dm9000_outblk_8bit;
293 db->inblk = dm9000_inblk_8bit;
298 dev_dbg(db->dev, ": 3 byte IO, falling back to 16bit\n");
300 db->dumpblk = dm9000_dumpblk_16bit;
301 db->outblk = dm9000_outblk_16bit;
302 db->inblk = dm9000_inblk_16bit;
307 db->dumpblk = dm9000_dumpblk_32bit;
308 db->outblk = dm9000_outblk_32bit;
309 db->inblk = dm9000_inblk_32bit;
315 /* Our watchdog timed out. Called by the networking layer */
316 static void dm9000_timeout(struct net_device *dev)
318 board_info_t *db = (board_info_t *) dev->priv;
322 /* Save previous register address */
323 reg_save = readb(db->io_addr);
324 spin_lock_irqsave(&db->lock,flags);
326 netif_stop_queue(dev);
328 dm9000_init_dm9000(dev);
329 /* We can accept TX packets again */
330 dev->trans_start = jiffies;
331 netif_wake_queue(dev);
333 /* Restore previous register address */
334 writeb(reg_save, db->io_addr);
335 spin_unlock_irqrestore(&db->lock,flags);
338 #ifdef CONFIG_NET_POLL_CONTROLLER
342 static void dm9000_poll_controller(struct net_device *dev)
344 disable_irq(dev->irq);
345 dm9000_interrupt(dev->irq,dev);
346 enable_irq(dev->irq);
350 /* dm9000_release_board
352 * release a board, and any mapped resources
356 dm9000_release_board(struct platform_device *pdev, struct board_info *db)
358 if (db->data_res == NULL) {
359 if (db->addr_res != NULL)
360 release_mem_region((unsigned long)db->io_addr, 4);
364 /* unmap our resources */
366 iounmap(db->io_addr);
367 iounmap(db->io_data);
369 /* release the resources */
371 if (db->data_req != NULL) {
372 release_resource(db->data_req);
376 if (db->addr_req != NULL) {
377 release_resource(db->addr_req);
382 #define res_size(_r) (((_r)->end - (_r)->start) + 1)
385 * Search DM9000 board, allocate space and register it
388 dm9000_probe(struct platform_device *pdev)
390 struct dm9000_plat_data *pdata = pdev->dev.platform_data;
391 struct board_info *db; /* Point a board information structure */
392 struct net_device *ndev;
399 /* Init network device */
400 ndev = alloc_etherdev(sizeof (struct board_info));
402 dev_err(&pdev->dev, "could not allocate device.\n");
406 SET_NETDEV_DEV(ndev, &pdev->dev);
408 dev_dbg(&pdev->dev, "dm9000_probe()");
410 /* setup board info structure */
411 db = (struct board_info *) ndev->priv;
412 memset(db, 0, sizeof (*db));
414 db->dev = &pdev->dev;
416 spin_lock_init(&db->lock);
418 if (pdev->num_resources < 2) {
421 } else if (pdev->num_resources == 2) {
422 base = pdev->resource[0].start;
424 if (!request_mem_region(base, 4, ndev->name)) {
429 ndev->base_addr = base;
430 ndev->irq = pdev->resource[1].start;
431 db->io_addr = (void __iomem *)base;
432 db->io_data = (void __iomem *)(base + 4);
434 /* ensure at least we have a default set of IO routines */
435 dm9000_set_io(db, 2);
438 db->addr_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
439 db->data_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
440 db->irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
442 if (db->addr_res == NULL || db->data_res == NULL ||
443 db->irq_res == NULL) {
444 dev_err(db->dev, "insufficient resources\n");
449 i = res_size(db->addr_res);
450 db->addr_req = request_mem_region(db->addr_res->start, i,
453 if (db->addr_req == NULL) {
454 dev_err(db->dev, "cannot claim address reg area\n");
459 db->io_addr = ioremap(db->addr_res->start, i);
461 if (db->io_addr == NULL) {
462 dev_err(db->dev, "failed to ioremap address reg\n");
467 iosize = res_size(db->data_res);
468 db->data_req = request_mem_region(db->data_res->start, iosize,
471 if (db->data_req == NULL) {
472 dev_err(db->dev, "cannot claim data reg area\n");
477 db->io_data = ioremap(db->data_res->start, iosize);
479 if (db->io_data == NULL) {
480 dev_err(db->dev,"failed to ioremap data reg\n");
485 /* fill in parameters for net-dev structure */
487 ndev->base_addr = (unsigned long)db->io_addr;
488 ndev->irq = db->irq_res->start;
490 /* ensure at least we have a default set of IO routines */
491 dm9000_set_io(db, iosize);
494 /* check to see if anything is being over-ridden */
496 /* check to see if the driver wants to over-ride the
497 * default IO width */
499 if (pdata->flags & DM9000_PLATF_8BITONLY)
500 dm9000_set_io(db, 1);
502 if (pdata->flags & DM9000_PLATF_16BITONLY)
503 dm9000_set_io(db, 2);
505 if (pdata->flags & DM9000_PLATF_32BITONLY)
506 dm9000_set_io(db, 4);
508 /* check to see if there are any IO routine
511 if (pdata->inblk != NULL)
512 db->inblk = pdata->inblk;
514 if (pdata->outblk != NULL)
515 db->outblk = pdata->outblk;
517 if (pdata->dumpblk != NULL)
518 db->dumpblk = pdata->dumpblk;
520 db->flags = pdata->flags;
525 /* try two times, DM9000 sometimes gets the first read wrong */
526 for (i = 0; i < 2; i++) {
527 id_val = ior(db, DM9000_VIDL);
528 id_val |= (u32)ior(db, DM9000_VIDH) << 8;
529 id_val |= (u32)ior(db, DM9000_PIDL) << 16;
530 id_val |= (u32)ior(db, DM9000_PIDH) << 24;
532 if (id_val == DM9000_ID)
534 dev_err(db->dev, "read wrong id 0x%08x\n", id_val);
537 if (id_val != DM9000_ID) {
538 dev_err(db->dev, "wrong id: 0x%08x\n", id_val);
543 /* from this point we assume that we have found a DM9000 */
545 /* driver system function */
548 ndev->open = &dm9000_open;
549 ndev->hard_start_xmit = &dm9000_start_xmit;
550 ndev->tx_timeout = &dm9000_timeout;
551 ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
552 ndev->stop = &dm9000_stop;
553 ndev->set_multicast_list = &dm9000_hash_table;
554 #ifdef CONFIG_NET_POLL_CONTROLLER
555 ndev->poll_controller = &dm9000_poll_controller;
558 #ifdef DM9000_PROGRAM_EEPROM
561 db->msg_enable = NETIF_MSG_LINK;
562 db->mii.phy_id_mask = 0x1f;
563 db->mii.reg_num_mask = 0x1f;
564 db->mii.force_media = 0;
565 db->mii.full_duplex = 0;
567 db->mii.mdio_read = dm9000_phy_read;
568 db->mii.mdio_write = dm9000_phy_write;
570 /* Read SROM content */
571 for (i = 0; i < 64; i++)
572 ((u16 *) db->srom)[i] = read_srom_word(db, i);
574 /* Set Node Address */
575 for (i = 0; i < 6; i++)
576 ndev->dev_addr[i] = db->srom[i];
578 if (!is_valid_ether_addr(ndev->dev_addr)) {
579 /* try reading from mac */
581 for (i = 0; i < 6; i++)
582 ndev->dev_addr[i] = ior(db, i+DM9000_PAR);
585 if (!is_valid_ether_addr(ndev->dev_addr))
586 dev_warn(db->dev, "%s: Invalid ethernet MAC address. Please "
587 "set using ifconfig\n", ndev->name);
589 platform_set_drvdata(pdev, ndev);
590 ret = register_netdev(ndev);
593 DECLARE_MAC_BUF(mac);
594 printk("%s: dm9000 at %p,%p IRQ %d MAC: %s\n",
595 ndev->name, db->io_addr, db->io_data, ndev->irq,
596 print_mac(mac, ndev->dev_addr));
601 dev_err(db->dev, "not found (%d).\n", ret);
603 dm9000_release_board(pdev, db);
610 * Open the interface.
611 * The interface is opened whenever "ifconfig" actives it.
614 dm9000_open(struct net_device *dev)
616 board_info_t *db = (board_info_t *) dev->priv;
618 dev_dbg(db->dev, "entering %s\n", __func__);
620 if (request_irq(dev->irq, &dm9000_interrupt, DM9000_IRQ_FLAGS, dev->name, dev))
623 /* Initialize DM9000 board */
625 dm9000_init_dm9000(dev);
627 /* Init driver variable */
630 /* set and active a timer process */
631 init_timer(&db->timer);
632 db->timer.expires = DM9000_TIMER_WUT;
633 db->timer.data = (unsigned long) dev;
634 db->timer.function = &dm9000_timer;
635 add_timer(&db->timer);
637 mii_check_media(&db->mii, netif_msg_link(db), 1);
638 netif_start_queue(dev);
644 * Initilize dm9000 board
647 dm9000_init_dm9000(struct net_device *dev)
649 board_info_t *db = (board_info_t *) dev->priv;
651 dm9000_dbg(db, 1, "entering %s\n", __func__);
654 db->io_mode = ior(db, DM9000_ISR) >> 6; /* ISR bit7:6 keeps I/O mode */
656 /* GPIO0 on pre-activate PHY */
657 iow(db, DM9000_GPR, 0); /* REG_1F bit0 activate phyxcer */
658 iow(db, DM9000_GPCR, GPCR_GEP_CNTL); /* Let GPIO0 output */
659 iow(db, DM9000_GPR, 0); /* Enable PHY */
661 if (db->flags & DM9000_PLATF_EXT_PHY)
662 iow(db, DM9000_NCR, NCR_EXT_PHY);
664 /* Program operating register */
665 iow(db, DM9000_TCR, 0); /* TX Polling clear */
666 iow(db, DM9000_BPTR, 0x3f); /* Less 3Kb, 200us */
667 iow(db, DM9000_FCR, 0xff); /* Flow Control */
668 iow(db, DM9000_SMCR, 0); /* Special Mode */
669 /* clear TX status */
670 iow(db, DM9000_NSR, NSR_WAKEST | NSR_TX2END | NSR_TX1END);
671 iow(db, DM9000_ISR, ISR_CLR_STATUS); /* Clear interrupt status */
673 /* Set address filter table */
674 dm9000_hash_table(dev);
676 /* Activate DM9000 */
677 iow(db, DM9000_RCR, RCR_DIS_LONG | RCR_DIS_CRC | RCR_RXEN);
678 /* Enable TX/RX interrupt mask */
679 iow(db, DM9000_IMR, IMR_PAR | IMR_PTM | IMR_PRM);
681 /* Init Driver variable */
683 db->queue_pkt_len = 0;
684 dev->trans_start = 0;
688 * Hardware start transmission.
689 * Send a packet to media from the upper layer.
692 dm9000_start_xmit(struct sk_buff *skb, struct net_device *dev)
695 board_info_t *db = (board_info_t *) dev->priv;
697 dm9000_dbg(db, 3, "%s:\n", __func__);
699 if (db->tx_pkt_cnt > 1)
702 spin_lock_irqsave(&db->lock, flags);
704 /* Move data to DM9000 TX RAM */
705 writeb(DM9000_MWCMD, db->io_addr);
707 (db->outblk)(db->io_data, skb->data, skb->len);
708 dev->stats.tx_bytes += skb->len;
711 /* TX control: First packet immediately send, second packet queue */
712 if (db->tx_pkt_cnt == 1) {
713 /* Set TX length to DM9000 */
714 iow(db, DM9000_TXPLL, skb->len & 0xff);
715 iow(db, DM9000_TXPLH, (skb->len >> 8) & 0xff);
717 /* Issue TX polling command */
718 iow(db, DM9000_TCR, TCR_TXREQ); /* Cleared after TX complete */
720 dev->trans_start = jiffies; /* save the time stamp */
723 db->queue_pkt_len = skb->len;
724 netif_stop_queue(dev);
727 spin_unlock_irqrestore(&db->lock, flags);
736 dm9000_shutdown(struct net_device *dev)
738 board_info_t *db = (board_info_t *) dev->priv;
741 dm9000_phy_write(dev, 0, MII_BMCR, BMCR_RESET); /* PHY RESET */
742 iow(db, DM9000_GPR, 0x01); /* Power-Down PHY */
743 iow(db, DM9000_IMR, IMR_PAR); /* Disable all interrupt */
744 iow(db, DM9000_RCR, 0x00); /* Disable RX */
748 * Stop the interface.
749 * The interface is stopped when it is brought.
752 dm9000_stop(struct net_device *ndev)
754 board_info_t *db = (board_info_t *) ndev->priv;
756 dm9000_dbg(db, 1, "entering %s\n", __func__);
759 del_timer(&db->timer);
761 netif_stop_queue(ndev);
762 netif_carrier_off(ndev);
765 free_irq(ndev->irq, ndev);
767 dm9000_shutdown(ndev);
773 * DM9000 interrupt handler
774 * receive the packet to upper layer, free the transmitted packet
778 dm9000_tx_done(struct net_device *dev, board_info_t * db)
780 int tx_status = ior(db, DM9000_NSR); /* Got TX status */
782 if (tx_status & (NSR_TX2END | NSR_TX1END)) {
783 /* One packet sent complete */
785 dev->stats.tx_packets++;
787 /* Queue packet check & send */
788 if (db->tx_pkt_cnt > 0) {
789 iow(db, DM9000_TXPLL, db->queue_pkt_len & 0xff);
790 iow(db, DM9000_TXPLH, (db->queue_pkt_len >> 8) & 0xff);
791 iow(db, DM9000_TCR, TCR_TXREQ);
792 dev->trans_start = jiffies;
794 netif_wake_queue(dev);
799 dm9000_interrupt(int irq, void *dev_id)
801 struct net_device *dev = dev_id;
802 board_info_t *db = (board_info_t *) dev->priv;
806 dm9000_dbg(db, 3, "entering %s\n", __func__);
808 /* A real interrupt coming */
810 spin_lock(&db->lock);
812 /* Save previous register address */
813 reg_save = readb(db->io_addr);
815 /* Disable all interrupts */
816 iow(db, DM9000_IMR, IMR_PAR);
818 /* Got DM9000 interrupt status */
819 int_status = ior(db, DM9000_ISR); /* Got ISR */
820 iow(db, DM9000_ISR, int_status); /* Clear ISR status */
822 /* Received the coming packet */
823 if (int_status & ISR_PRS)
826 /* Trnasmit Interrupt check */
827 if (int_status & ISR_PTS)
828 dm9000_tx_done(dev, db);
830 /* Re-enable interrupt mask */
831 iow(db, DM9000_IMR, IMR_PAR | IMR_PTM | IMR_PRM);
833 /* Restore previous register address */
834 writeb(reg_save, db->io_addr);
836 spin_unlock(&db->lock);
842 * A periodic timer routine
843 * Dynamic media sense, allocated Rx buffer...
846 dm9000_timer(unsigned long data)
848 struct net_device *dev = (struct net_device *) data;
849 board_info_t *db = (board_info_t *) dev->priv;
851 dm9000_dbg(db, 3, "entering %s\n", __func__);
853 mii_check_media(&db->mii, netif_msg_link(db), 0);
855 /* Set timer again */
856 db->timer.expires = DM9000_TIMER_WUT;
857 add_timer(&db->timer);
860 struct dm9000_rxhdr {
864 } __attribute__((__packed__));
867 * Received a packet and pass to upper layer
870 dm9000_rx(struct net_device *dev)
872 board_info_t *db = (board_info_t *) dev->priv;
873 struct dm9000_rxhdr rxhdr;
879 /* Check packet ready or not */
881 ior(db, DM9000_MRCMDX); /* Dummy read */
883 /* Get most updated data */
884 rxbyte = readb(db->io_data);
886 /* Status check: this byte must be 0 or 1 */
887 if (rxbyte > DM9000_PKT_RDY) {
888 dev_warn(db->dev, "status check fail: %d\n", rxbyte);
889 iow(db, DM9000_RCR, 0x00); /* Stop Device */
890 iow(db, DM9000_ISR, IMR_PAR); /* Stop INT request */
894 if (rxbyte != DM9000_PKT_RDY)
897 /* A packet ready now & Get status/length */
899 writeb(DM9000_MRCMD, db->io_addr);
901 (db->inblk)(db->io_data, &rxhdr, sizeof(rxhdr));
903 RxLen = le16_to_cpu(rxhdr.RxLen);
905 /* Packet Status check */
908 dev_dbg(db->dev, "Bad Packet received (runt)\n");
911 if (RxLen > DM9000_PKT_MAX) {
912 dev_dbg(db->dev, "RST: RX Len:%x\n", RxLen);
915 if (rxhdr.RxStatus & 0xbf) {
917 if (rxhdr.RxStatus & 0x01) {
918 dev_dbg(db->dev, "fifo error\n");
919 dev->stats.rx_fifo_errors++;
921 if (rxhdr.RxStatus & 0x02) {
922 dev_dbg(db->dev, "crc error\n");
923 dev->stats.rx_crc_errors++;
925 if (rxhdr.RxStatus & 0x80) {
926 dev_dbg(db->dev, "length error\n");
927 dev->stats.rx_length_errors++;
931 /* Move data from DM9000 */
933 && ((skb = dev_alloc_skb(RxLen + 4)) != NULL)) {
935 rdptr = (u8 *) skb_put(skb, RxLen - 4);
937 /* Read received packet from RX SRAM */
939 (db->inblk)(db->io_data, rdptr, RxLen);
940 dev->stats.rx_bytes += RxLen;
942 /* Pass to upper layer */
943 skb->protocol = eth_type_trans(skb, dev);
945 dev->stats.rx_packets++;
948 /* need to dump the packet's data */
950 (db->dumpblk)(db->io_data, RxLen);
952 } while (rxbyte == DM9000_PKT_RDY);
956 * Read a word data from SROM
959 read_srom_word(board_info_t * db, int offset)
961 iow(db, DM9000_EPAR, offset);
962 iow(db, DM9000_EPCR, EPCR_ERPRR);
963 mdelay(8); /* according to the datasheet 200us should be enough,
964 but it doesn't work */
965 iow(db, DM9000_EPCR, 0x0);
966 return (ior(db, DM9000_EPDRL) + (ior(db, DM9000_EPDRH) << 8));
969 #ifdef DM9000_PROGRAM_EEPROM
971 * Write a word data to SROM
974 write_srom_word(board_info_t * db, int offset, u16 val)
976 iow(db, DM9000_EPAR, offset);
977 iow(db, DM9000_EPDRH, ((val >> 8) & 0xff));
978 iow(db, DM9000_EPDRL, (val & 0xff));
979 iow(db, DM9000_EPCR, EPCR_WEP | EPCR_ERPRW);
980 mdelay(8); /* same shit */
981 iow(db, DM9000_EPCR, 0);
985 * Only for development:
986 * Here we write static data to the eeprom in case
987 * we don't have valid content on a new board
990 program_eeprom(board_info_t * db)
992 u16 eeprom[] = { 0x0c00, 0x007f, 0x1300, /* MAC Address */
993 0x0000, /* Autoload: accept nothing */
994 0x0a46, 0x9000, /* Vendor / Product ID */
995 0x0000, /* pin control */
997 }; /* Wake-up mode control */
999 for (i = 0; i < 8; i++)
1000 write_srom_word(db, i, eeprom[i]);
1006 * Calculate the CRC valude of the Rx packet
1007 * flag = 1 : return the reverse CRC (for the received packet CRC)
1008 * 0 : return the normal CRC (for Hash Table index)
1011 static unsigned long
1012 cal_CRC(unsigned char *Data, unsigned int Len, u8 flag)
1015 u32 crc = ether_crc_le(Len, Data);
1024 * Set DM9000 multicast address
1027 dm9000_hash_table(struct net_device *dev)
1029 board_info_t *db = (board_info_t *) dev->priv;
1030 struct dev_mc_list *mcptr = dev->mc_list;
1031 int mc_cnt = dev->mc_count;
1033 u16 i, oft, hash_table[4];
1034 unsigned long flags;
1036 dm9000_dbg(db, 1, "entering %s\n", __func__);
1038 spin_lock_irqsave(&db->lock,flags);
1040 for (i = 0, oft = 0x10; i < 6; i++, oft++)
1041 iow(db, oft, dev->dev_addr[i]);
1043 /* Clear Hash Table */
1044 for (i = 0; i < 4; i++)
1045 hash_table[i] = 0x0;
1047 /* broadcast address */
1048 hash_table[3] = 0x8000;
1050 /* the multicast address in Hash Table : 64 bits */
1051 for (i = 0; i < mc_cnt; i++, mcptr = mcptr->next) {
1052 hash_val = cal_CRC((char *) mcptr->dmi_addr, 6, 0) & 0x3f;
1053 hash_table[hash_val / 16] |= (u16) 1 << (hash_val % 16);
1056 /* Write the hash table to MAC MD table */
1057 for (i = 0, oft = 0x16; i < 4; i++) {
1058 iow(db, oft++, hash_table[i] & 0xff);
1059 iow(db, oft++, (hash_table[i] >> 8) & 0xff);
1062 spin_unlock_irqrestore(&db->lock,flags);
1067 * Read a word from phyxcer
1070 dm9000_phy_read(struct net_device *dev, int phy_reg_unused, int reg)
1072 board_info_t *db = (board_info_t *) dev->priv;
1073 unsigned long flags;
1074 unsigned int reg_save;
1077 spin_lock_irqsave(&db->lock,flags);
1079 /* Save previous register address */
1080 reg_save = readb(db->io_addr);
1082 /* Fill the phyxcer register into REG_0C */
1083 iow(db, DM9000_EPAR, DM9000_PHY | reg);
1085 iow(db, DM9000_EPCR, 0xc); /* Issue phyxcer read command */
1086 udelay(100); /* Wait read complete */
1087 iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer read command */
1089 /* The read data keeps on REG_0D & REG_0E */
1090 ret = (ior(db, DM9000_EPDRH) << 8) | ior(db, DM9000_EPDRL);
1092 /* restore the previous address */
1093 writeb(reg_save, db->io_addr);
1095 spin_unlock_irqrestore(&db->lock,flags);
1101 * Write a word to phyxcer
1104 dm9000_phy_write(struct net_device *dev, int phyaddr_unused, int reg, int value)
1106 board_info_t *db = (board_info_t *) dev->priv;
1107 unsigned long flags;
1108 unsigned long reg_save;
1110 spin_lock_irqsave(&db->lock,flags);
1112 /* Save previous register address */
1113 reg_save = readb(db->io_addr);
1115 /* Fill the phyxcer register into REG_0C */
1116 iow(db, DM9000_EPAR, DM9000_PHY | reg);
1118 /* Fill the written data into REG_0D & REG_0E */
1119 iow(db, DM9000_EPDRL, (value & 0xff));
1120 iow(db, DM9000_EPDRH, ((value >> 8) & 0xff));
1122 iow(db, DM9000_EPCR, 0xa); /* Issue phyxcer write command */
1123 udelay(500); /* Wait write complete */
1124 iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer write command */
1126 /* restore the previous address */
1127 writeb(reg_save, db->io_addr);
1129 spin_unlock_irqrestore(&db->lock,flags);
1133 dm9000_drv_suspend(struct platform_device *dev, pm_message_t state)
1135 struct net_device *ndev = platform_get_drvdata(dev);
1138 if (netif_running(ndev)) {
1139 netif_device_detach(ndev);
1140 dm9000_shutdown(ndev);
1147 dm9000_drv_resume(struct platform_device *dev)
1149 struct net_device *ndev = platform_get_drvdata(dev);
1150 board_info_t *db = (board_info_t *) ndev->priv;
1154 if (netif_running(ndev)) {
1156 dm9000_init_dm9000(ndev);
1158 netif_device_attach(ndev);
1165 dm9000_drv_remove(struct platform_device *pdev)
1167 struct net_device *ndev = platform_get_drvdata(pdev);
1169 platform_set_drvdata(pdev, NULL);
1171 unregister_netdev(ndev);
1172 dm9000_release_board(pdev, (board_info_t *) ndev->priv);
1173 free_netdev(ndev); /* free device structure */
1175 dev_dbg(&pdev->dev, "released and freed device\n");
1179 static struct platform_driver dm9000_driver = {
1182 .owner = THIS_MODULE,
1184 .probe = dm9000_probe,
1185 .remove = dm9000_drv_remove,
1186 .suspend = dm9000_drv_suspend,
1187 .resume = dm9000_drv_resume,
1193 printk(KERN_INFO "%s Ethernet Driver\n", CARDNAME);
1195 return platform_driver_register(&dm9000_driver); /* search board and register */
1199 dm9000_cleanup(void)
1201 platform_driver_unregister(&dm9000_driver);
1204 module_init(dm9000_init);
1205 module_exit(dm9000_cleanup);
1207 MODULE_AUTHOR("Sascha Hauer, Ben Dooks");
1208 MODULE_DESCRIPTION("Davicom DM9000 network driver");
1209 MODULE_LICENSE("GPL");