2 * Davicom DM9000 Fast Ethernet driver for Linux.
3 * Copyright (C) 1997 Sten Wang
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * (C) Copyright 1997-1998 DAVICOM Semiconductor,Inc. All Rights Reserved.
17 * Additional updates, Copyright:
18 * Ben Dooks <ben@simtec.co.uk>
19 * Sascha Hauer <s.hauer@pengutronix.de>
22 #include <linux/module.h>
23 #include <linux/ioport.h>
24 #include <linux/netdevice.h>
25 #include <linux/etherdevice.h>
26 #include <linux/init.h>
27 #include <linux/skbuff.h>
28 #include <linux/spinlock.h>
29 #include <linux/crc32.h>
30 #include <linux/mii.h>
31 #include <linux/ethtool.h>
32 #include <linux/dm9000.h>
33 #include <linux/delay.h>
34 #include <linux/platform_device.h>
35 #include <linux/irq.h>
36 #include <linux/slab.h>
38 #include <asm/delay.h>
41 #include <mach/gpio.h>
45 /* Board/System/Debug information/definition ---------------- */
47 #define DM9000_PHY 0x40 /* PHY address 0x01 */
49 #define CARDNAME "dm9000"
50 #define DRV_VERSION "1.31"
53 * Transmit timeout, default 5 seconds.
55 static int watchdog = 5000;
56 module_param(watchdog, int, 0400);
57 MODULE_PARM_DESC(watchdog, "transmit timeout in milliseconds");
59 /* DM9000 register address locking.
61 * The DM9000 uses an address register to control where data written
62 * to the data register goes. This means that the address register
63 * must be preserved over interrupts or similar calls.
65 * During interrupt and other critical calls, a spinlock is used to
66 * protect the system, but the calls themselves save the address
67 * in the address register in case they are interrupting another
68 * access to the device.
70 * For general accesses a lock is provided so that calls which are
71 * allowed to sleep are serialised so that the address register does
72 * not need to be saved. This lock also serves to serialise access
73 * to the EEPROM and PHY access registers which are shared between
77 /* The driver supports the original DM9000E, and now the two newer
78 * devices, DM9000A and DM9000B.
82 TYPE_DM9000E, /* original DM9000 */
87 /* Structure/enum declaration ------------------------------- */
88 typedef struct board_info {
90 void __iomem *io_addr; /* Register I/O base address */
91 void __iomem *io_data; /* Data I/O address */
99 u8 io_mode; /* 0:word, 2:byte */
104 unsigned int in_suspend :1;
105 unsigned int wake_supported :1;
108 enum dm9000_type type;
110 void (*inblk)(void __iomem *port, void *data, int length);
111 void (*outblk)(void __iomem *port, void *data, int length);
112 void (*dumpblk)(void __iomem *port, int length);
114 struct device *dev; /* parent device */
116 struct resource *addr_res; /* resources found */
117 struct resource *data_res;
118 struct resource *addr_req; /* resources requested */
119 struct resource *data_req;
120 struct resource *irq_res;
124 struct mutex addr_lock; /* phy and eeprom access lock */
126 struct delayed_work phy_poll;
127 struct net_device *ndev;
131 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 static inline board_info_t *to_dm9000_board(struct net_device *dev)
150 return netdev_priv(dev);
153 /* DM9000 network board routine ---------------------------- */
156 dm9000_reset(board_info_t * db)
158 dev_dbg(db->dev, "resetting device\n");
161 writeb(DM9000_NCR, db->io_addr);
163 writeb(NCR_RST, db->io_data);
168 * Read a byte from I/O port
171 ior(board_info_t * db, int reg)
173 writeb(reg, db->io_addr);
174 return readb(db->io_data);
178 * Write a byte to I/O port
182 iow(board_info_t * db, int reg, int value)
184 writeb(reg, db->io_addr);
185 writeb(value, db->io_data);
188 /* routines for sending block to chip */
190 static void dm9000_outblk_8bit(void __iomem *reg, void *data, int count)
192 writesb(reg, data, count);
195 static void dm9000_outblk_16bit(void __iomem *reg, void *data, int count)
197 writesw(reg, data, (count+1) >> 1);
200 static void dm9000_outblk_32bit(void __iomem *reg, void *data, int count)
202 writesl(reg, data, (count+3) >> 2);
205 /* input block from chip to memory */
207 static void dm9000_inblk_8bit(void __iomem *reg, void *data, int count)
209 readsb(reg, data, count);
213 static void dm9000_inblk_16bit(void __iomem *reg, void *data, int count)
215 readsw(reg, data, (count+1) >> 1);
218 static void dm9000_inblk_32bit(void __iomem *reg, void *data, int count)
220 readsl(reg, data, (count+3) >> 2);
223 /* dump block from chip to null */
225 static void dm9000_dumpblk_8bit(void __iomem *reg, int count)
230 for (i = 0; i < count; i++)
234 static void dm9000_dumpblk_16bit(void __iomem *reg, int count)
239 count = (count + 1) >> 1;
241 for (i = 0; i < count; i++)
245 static void dm9000_dumpblk_32bit(void __iomem *reg, int count)
250 count = (count + 3) >> 2;
252 for (i = 0; i < count; i++)
258 * select the specified set of io routines to use with the
262 static void dm9000_set_io(struct board_info *db, int byte_width)
264 /* use the size of the data resource to work out what IO
265 * routines we want to use
268 switch (byte_width) {
270 db->dumpblk = dm9000_dumpblk_8bit;
271 db->outblk = dm9000_outblk_8bit;
272 db->inblk = dm9000_inblk_8bit;
277 dev_dbg(db->dev, ": 3 byte IO, falling back to 16bit\n");
279 db->dumpblk = dm9000_dumpblk_16bit;
280 db->outblk = dm9000_outblk_16bit;
281 db->inblk = dm9000_inblk_16bit;
286 db->dumpblk = dm9000_dumpblk_32bit;
287 db->outblk = dm9000_outblk_32bit;
288 db->inblk = dm9000_inblk_32bit;
293 static void dm9000_schedule_poll(board_info_t *db)
295 if (db->type == TYPE_DM9000E)
296 schedule_delayed_work(&db->phy_poll, HZ * 2);
299 static int dm9000_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
301 board_info_t *dm = to_dm9000_board(dev);
303 if (!netif_running(dev))
306 return generic_mii_ioctl(&dm->mii, if_mii(req), cmd, NULL);
310 dm9000_read_locked(board_info_t *db, int reg)
315 spin_lock_irqsave(&db->lock, flags);
317 spin_unlock_irqrestore(&db->lock, flags);
322 static int dm9000_wait_eeprom(board_info_t *db)
325 int timeout = 8; /* wait max 8msec */
327 /* The DM9000 data sheets say we should be able to
328 * poll the ERRE bit in EPCR to wait for the EEPROM
329 * operation. From testing several chips, this bit
330 * does not seem to work.
332 * We attempt to use the bit, but fall back to the
333 * timeout (which is why we do not return an error
334 * on expiry) to say that the EEPROM operation has
339 status = dm9000_read_locked(db, DM9000_EPCR);
341 if ((status & EPCR_ERRE) == 0)
347 dev_dbg(db->dev, "timeout waiting EEPROM\n");
356 * Read a word data from EEPROM
359 dm9000_read_eeprom(board_info_t *db, int offset, u8 *to)
363 if (db->flags & DM9000_PLATF_NO_EEPROM) {
369 mutex_lock(&db->addr_lock);
371 spin_lock_irqsave(&db->lock, flags);
373 iow(db, DM9000_EPAR, offset);
374 iow(db, DM9000_EPCR, EPCR_ERPRR);
376 spin_unlock_irqrestore(&db->lock, flags);
378 dm9000_wait_eeprom(db);
380 /* delay for at-least 150uS */
383 spin_lock_irqsave(&db->lock, flags);
385 iow(db, DM9000_EPCR, 0x0);
387 to[0] = ior(db, DM9000_EPDRL);
388 to[1] = ior(db, DM9000_EPDRH);
390 spin_unlock_irqrestore(&db->lock, flags);
392 mutex_unlock(&db->addr_lock);
396 * Write a word data to SROM
399 dm9000_write_eeprom(board_info_t *db, int offset, u8 *data)
403 if (db->flags & DM9000_PLATF_NO_EEPROM)
406 mutex_lock(&db->addr_lock);
408 spin_lock_irqsave(&db->lock, flags);
409 iow(db, DM9000_EPAR, offset);
410 iow(db, DM9000_EPDRH, data[1]);
411 iow(db, DM9000_EPDRL, data[0]);
412 iow(db, DM9000_EPCR, EPCR_WEP | EPCR_ERPRW);
413 spin_unlock_irqrestore(&db->lock, flags);
415 dm9000_wait_eeprom(db);
417 mdelay(1); /* wait at least 150uS to clear */
419 spin_lock_irqsave(&db->lock, flags);
420 iow(db, DM9000_EPCR, 0);
421 spin_unlock_irqrestore(&db->lock, flags);
423 mutex_unlock(&db->addr_lock);
428 static void dm9000_get_drvinfo(struct net_device *dev,
429 struct ethtool_drvinfo *info)
431 board_info_t *dm = to_dm9000_board(dev);
433 strcpy(info->driver, CARDNAME);
434 strcpy(info->version, DRV_VERSION);
435 strcpy(info->bus_info, to_platform_device(dm->dev)->name);
438 static u32 dm9000_get_msglevel(struct net_device *dev)
440 board_info_t *dm = to_dm9000_board(dev);
442 return dm->msg_enable;
445 static void dm9000_set_msglevel(struct net_device *dev, u32 value)
447 board_info_t *dm = to_dm9000_board(dev);
449 dm->msg_enable = value;
452 static int dm9000_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
454 board_info_t *dm = to_dm9000_board(dev);
456 mii_ethtool_gset(&dm->mii, cmd);
460 static int dm9000_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
462 board_info_t *dm = to_dm9000_board(dev);
464 return mii_ethtool_sset(&dm->mii, cmd);
467 static int dm9000_nway_reset(struct net_device *dev)
469 board_info_t *dm = to_dm9000_board(dev);
470 return mii_nway_restart(&dm->mii);
473 static uint32_t dm9000_get_rx_csum(struct net_device *dev)
475 board_info_t *dm = to_dm9000_board(dev);
479 static int dm9000_set_rx_csum_unlocked(struct net_device *dev, uint32_t data)
481 board_info_t *dm = to_dm9000_board(dev);
485 iow(dm, DM9000_RCSR, dm->rx_csum ? RCSR_CSUM : 0);
493 static int dm9000_set_rx_csum(struct net_device *dev, uint32_t data)
495 board_info_t *dm = to_dm9000_board(dev);
499 spin_lock_irqsave(&dm->lock, flags);
500 ret = dm9000_set_rx_csum_unlocked(dev, data);
501 spin_unlock_irqrestore(&dm->lock, flags);
506 static int dm9000_set_tx_csum(struct net_device *dev, uint32_t data)
508 board_info_t *dm = to_dm9000_board(dev);
509 int ret = -EOPNOTSUPP;
512 ret = ethtool_op_set_tx_csum(dev, data);
516 static u32 dm9000_get_link(struct net_device *dev)
518 board_info_t *dm = to_dm9000_board(dev);
521 if (dm->flags & DM9000_PLATF_EXT_PHY)
522 ret = mii_link_ok(&dm->mii);
524 ret = dm9000_read_locked(dm, DM9000_NSR) & NSR_LINKST ? 1 : 0;
529 #define DM_EEPROM_MAGIC (0x444D394B)
531 static int dm9000_get_eeprom_len(struct net_device *dev)
536 static int dm9000_get_eeprom(struct net_device *dev,
537 struct ethtool_eeprom *ee, u8 *data)
539 board_info_t *dm = to_dm9000_board(dev);
540 int offset = ee->offset;
544 /* EEPROM access is aligned to two bytes */
546 if ((len & 1) != 0 || (offset & 1) != 0)
549 if (dm->flags & DM9000_PLATF_NO_EEPROM)
552 ee->magic = DM_EEPROM_MAGIC;
554 for (i = 0; i < len; i += 2)
555 dm9000_read_eeprom(dm, (offset + i) / 2, data + i);
560 static int dm9000_set_eeprom(struct net_device *dev,
561 struct ethtool_eeprom *ee, u8 *data)
563 board_info_t *dm = to_dm9000_board(dev);
564 int offset = ee->offset;
568 /* EEPROM access is aligned to two bytes */
570 if ((len & 1) != 0 || (offset & 1) != 0)
573 if (dm->flags & DM9000_PLATF_NO_EEPROM)
576 if (ee->magic != DM_EEPROM_MAGIC)
579 for (i = 0; i < len; i += 2)
580 dm9000_write_eeprom(dm, (offset + i) / 2, data + i);
585 static void dm9000_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
587 board_info_t *dm = to_dm9000_board(dev);
589 memset(w, 0, sizeof(struct ethtool_wolinfo));
591 /* note, we could probably support wake-phy too */
592 w->supported = dm->wake_supported ? WAKE_MAGIC : 0;
593 w->wolopts = dm->wake_state;
596 static int dm9000_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
598 board_info_t *dm = to_dm9000_board(dev);
600 u32 opts = w->wolopts;
603 if (!dm->wake_supported)
606 if (opts & ~WAKE_MAGIC)
609 if (opts & WAKE_MAGIC)
612 mutex_lock(&dm->addr_lock);
614 spin_lock_irqsave(&dm->lock, flags);
615 iow(dm, DM9000_WCR, wcr);
616 spin_unlock_irqrestore(&dm->lock, flags);
618 mutex_unlock(&dm->addr_lock);
620 if (dm->wake_state != opts) {
621 /* change in wol state, update IRQ state */
624 set_irq_wake(dm->irq_wake, 1);
625 else if (dm->wake_state & !opts)
626 set_irq_wake(dm->irq_wake, 0);
629 dm->wake_state = opts;
633 static const struct ethtool_ops dm9000_ethtool_ops = {
634 .get_drvinfo = dm9000_get_drvinfo,
635 .get_settings = dm9000_get_settings,
636 .set_settings = dm9000_set_settings,
637 .get_msglevel = dm9000_get_msglevel,
638 .set_msglevel = dm9000_set_msglevel,
639 .nway_reset = dm9000_nway_reset,
640 .get_link = dm9000_get_link,
641 .get_wol = dm9000_get_wol,
642 .set_wol = dm9000_set_wol,
643 .get_eeprom_len = dm9000_get_eeprom_len,
644 .get_eeprom = dm9000_get_eeprom,
645 .set_eeprom = dm9000_set_eeprom,
646 .get_rx_csum = dm9000_get_rx_csum,
647 .set_rx_csum = dm9000_set_rx_csum,
648 .get_tx_csum = ethtool_op_get_tx_csum,
649 .set_tx_csum = dm9000_set_tx_csum,
652 static void dm9000_show_carrier(board_info_t *db,
653 unsigned carrier, unsigned nsr)
655 struct net_device *ndev = db->ndev;
656 unsigned ncr = dm9000_read_locked(db, DM9000_NCR);
659 dev_info(db->dev, "%s: link up, %dMbps, %s-duplex, no LPA\n",
660 ndev->name, (nsr & NSR_SPEED) ? 10 : 100,
661 (ncr & NCR_FDX) ? "full" : "half");
663 dev_info(db->dev, "%s: link down\n", ndev->name);
667 dm9000_poll_work(struct work_struct *w)
669 struct delayed_work *dw = to_delayed_work(w);
670 board_info_t *db = container_of(dw, board_info_t, phy_poll);
671 struct net_device *ndev = db->ndev;
673 if (db->flags & DM9000_PLATF_SIMPLE_PHY &&
674 !(db->flags & DM9000_PLATF_EXT_PHY)) {
675 unsigned nsr = dm9000_read_locked(db, DM9000_NSR);
676 unsigned old_carrier = netif_carrier_ok(ndev) ? 1 : 0;
677 unsigned new_carrier;
679 new_carrier = (nsr & NSR_LINKST) ? 1 : 0;
681 if (old_carrier != new_carrier) {
682 if (netif_msg_link(db))
683 dm9000_show_carrier(db, new_carrier, nsr);
686 netif_carrier_off(ndev);
688 netif_carrier_on(ndev);
691 mii_check_media(&db->mii, netif_msg_link(db), 0);
693 if (netif_running(ndev))
694 dm9000_schedule_poll(db);
697 /* dm9000_release_board
699 * release a board, and any mapped resources
703 dm9000_release_board(struct platform_device *pdev, struct board_info *db)
705 /* unmap our resources */
707 iounmap(db->io_addr);
708 iounmap(db->io_data);
710 /* release the resources */
712 release_resource(db->data_req);
715 release_resource(db->addr_req);
719 static unsigned char dm9000_type_to_char(enum dm9000_type type)
722 case TYPE_DM9000E: return 'e';
723 case TYPE_DM9000A: return 'a';
724 case TYPE_DM9000B: return 'b';
731 * Set DM9000 multicast address
734 dm9000_hash_table_unlocked(struct net_device *dev)
736 board_info_t *db = netdev_priv(dev);
737 struct netdev_hw_addr *ha;
741 u8 rcr = RCR_DIS_LONG | RCR_DIS_CRC | RCR_RXEN;
743 dm9000_dbg(db, 1, "entering %s\n", __func__);
745 for (i = 0, oft = DM9000_PAR; i < 6; i++, oft++)
746 iow(db, oft, dev->dev_addr[i]);
748 /* Clear Hash Table */
749 for (i = 0; i < 4; i++)
752 /* broadcast address */
753 hash_table[3] = 0x8000;
755 if (dev->flags & IFF_PROMISC)
758 if (dev->flags & IFF_ALLMULTI)
761 /* the multicast address in Hash Table : 64 bits */
762 netdev_for_each_mc_addr(ha, dev) {
763 hash_val = ether_crc_le(6, ha->addr) & 0x3f;
764 hash_table[hash_val / 16] |= (u16) 1 << (hash_val % 16);
767 /* Write the hash table to MAC MD table */
768 for (i = 0, oft = DM9000_MAR; i < 4; i++) {
769 iow(db, oft++, hash_table[i]);
770 iow(db, oft++, hash_table[i] >> 8);
773 iow(db, DM9000_RCR, rcr);
777 dm9000_hash_table(struct net_device *dev)
779 board_info_t *db = netdev_priv(dev);
782 spin_lock_irqsave(&db->lock, flags);
783 dm9000_hash_table_unlocked(dev);
784 spin_unlock_irqrestore(&db->lock, flags);
788 * Initialize dm9000 board
791 dm9000_init_dm9000(struct net_device *dev)
793 board_info_t *db = netdev_priv(dev);
797 dm9000_dbg(db, 1, "entering %s\n", __func__);
800 db->io_mode = ior(db, DM9000_ISR) >> 6; /* ISR bit7:6 keeps I/O mode */
803 dm9000_set_rx_csum_unlocked(dev, db->rx_csum);
805 /* GPIO0 on pre-activate PHY */
806 iow(db, DM9000_GPR, 0); /* REG_1F bit0 activate phyxcer */
807 iow(db, DM9000_GPCR, GPCR_GEP_CNTL); /* Let GPIO0 output */
808 iow(db, DM9000_GPR, 0); /* Enable PHY */
810 ncr = (db->flags & DM9000_PLATF_EXT_PHY) ? NCR_EXT_PHY : 0;
812 /* if wol is needed, then always set NCR_WAKEEN otherwise we end
813 * up dumping the wake events if we disable this. There is already
814 * a wake-mask in DM9000_WCR */
815 if (db->wake_supported)
818 iow(db, DM9000_NCR, ncr);
820 /* Program operating register */
821 iow(db, DM9000_TCR, 0); /* TX Polling clear */
822 iow(db, DM9000_BPTR, 0x3f); /* Less 3Kb, 200us */
823 iow(db, DM9000_FCR, 0xff); /* Flow Control */
824 iow(db, DM9000_SMCR, 0); /* Special Mode */
825 /* clear TX status */
826 iow(db, DM9000_NSR, NSR_WAKEST | NSR_TX2END | NSR_TX1END);
827 iow(db, DM9000_ISR, ISR_CLR_STATUS); /* Clear interrupt status */
829 /* Set address filter table */
830 dm9000_hash_table_unlocked(dev);
832 imr = IMR_PAR | IMR_PTM | IMR_PRM;
833 if (db->type != TYPE_DM9000E)
838 /* Enable TX/RX interrupt mask */
839 iow(db, DM9000_IMR, imr);
841 /* Init Driver variable */
843 db->queue_pkt_len = 0;
844 dev->trans_start = jiffies;
847 /* Our watchdog timed out. Called by the networking layer */
848 static void dm9000_timeout(struct net_device *dev)
850 board_info_t *db = netdev_priv(dev);
854 /* Save previous register address */
855 reg_save = readb(db->io_addr);
856 spin_lock_irqsave(&db->lock, flags);
858 netif_stop_queue(dev);
860 dm9000_init_dm9000(dev);
861 /* We can accept TX packets again */
862 dev->trans_start = jiffies; /* prevent tx timeout */
863 netif_wake_queue(dev);
865 /* Restore previous register address */
866 writeb(reg_save, db->io_addr);
867 spin_unlock_irqrestore(&db->lock, flags);
870 static void dm9000_send_packet(struct net_device *dev,
874 board_info_t *dm = to_dm9000_board(dev);
876 /* The DM9000 is not smart enough to leave fragmented packets alone. */
877 if (dm->ip_summed != ip_summed) {
878 if (ip_summed == CHECKSUM_NONE)
879 iow(dm, DM9000_TCCR, 0);
881 iow(dm, DM9000_TCCR, TCCR_IP | TCCR_UDP | TCCR_TCP);
882 dm->ip_summed = ip_summed;
885 /* Set TX length to DM9000 */
886 iow(dm, DM9000_TXPLL, pkt_len);
887 iow(dm, DM9000_TXPLH, pkt_len >> 8);
889 /* Issue TX polling command */
890 iow(dm, DM9000_TCR, TCR_TXREQ); /* Cleared after TX complete */
894 * Hardware start transmission.
895 * Send a packet to media from the upper layer.
898 dm9000_start_xmit(struct sk_buff *skb, struct net_device *dev)
901 board_info_t *db = netdev_priv(dev);
903 dm9000_dbg(db, 3, "%s:\n", __func__);
905 if (db->tx_pkt_cnt > 1)
906 return NETDEV_TX_BUSY;
908 spin_lock_irqsave(&db->lock, flags);
910 /* Move data to DM9000 TX RAM */
911 writeb(DM9000_MWCMD, db->io_addr);
913 (db->outblk)(db->io_data, skb->data, skb->len);
914 dev->stats.tx_bytes += skb->len;
917 /* TX control: First packet immediately send, second packet queue */
918 if (db->tx_pkt_cnt == 1) {
919 dm9000_send_packet(dev, skb->ip_summed, skb->len);
922 db->queue_pkt_len = skb->len;
923 db->queue_ip_summed = skb->ip_summed;
924 netif_stop_queue(dev);
927 spin_unlock_irqrestore(&db->lock, flags);
936 * DM9000 interrupt handler
937 * receive the packet to upper layer, free the transmitted packet
940 static void dm9000_tx_done(struct net_device *dev, board_info_t *db)
942 int tx_status = ior(db, DM9000_NSR); /* Got TX status */
944 if (tx_status & (NSR_TX2END | NSR_TX1END)) {
945 /* One packet sent complete */
947 dev->stats.tx_packets++;
949 if (netif_msg_tx_done(db))
950 dev_dbg(db->dev, "tx done, NSR %02x\n", tx_status);
952 /* Queue packet check & send */
953 if (db->tx_pkt_cnt > 0)
954 dm9000_send_packet(dev, db->queue_ip_summed,
956 netif_wake_queue(dev);
960 struct dm9000_rxhdr {
967 * Received a packet and pass to upper layer
970 dm9000_rx(struct net_device *dev)
972 board_info_t *db = netdev_priv(dev);
973 struct dm9000_rxhdr rxhdr;
979 /* Check packet ready or not */
981 ior(db, DM9000_MRCMDX); /* Dummy read */
983 udelay(1);//add by lyx@20100713,or dm9000_rx will be error in high frequence
986 /* Get most updated data */
987 rxbyte = ior(db, DM9000_MRCMDX); /* Dummy read */
989 rxbyte = readb(db->io_data);
992 /* Status check: this byte must be 0 or 1 */
993 if (rxbyte & DM9000_PKT_ERR) {
994 dev_warn(db->dev, "status check fail: %d\n", rxbyte);
996 iow(db, DM9000_RCR, 0x00); /* Stop Device */
997 iow(db, DM9000_ISR, IMR_PAR); /* Stop INT request */
1004 if (!(rxbyte & DM9000_PKT_RDY))
1007 /* A packet ready now & Get status/length */
1009 writeb(DM9000_MRCMD, db->io_addr);
1011 (db->inblk)(db->io_data, &rxhdr, sizeof(rxhdr));
1013 RxLen = le16_to_cpu(rxhdr.RxLen);
1015 if (netif_msg_rx_status(db))
1016 dev_dbg(db->dev, "RX: status %02x, length %04x\n",
1017 rxhdr.RxStatus, RxLen);
1019 /* Packet Status check */
1022 if (netif_msg_rx_err(db))
1023 dev_dbg(db->dev, "RX: Bad Packet (runt)\n");
1026 if (RxLen > DM9000_PKT_MAX) {
1027 dev_dbg(db->dev, "RST: RX Len:%x\n", RxLen);
1030 /* rxhdr.RxStatus is identical to RSR register. */
1031 if (rxhdr.RxStatus & (RSR_FOE | RSR_CE | RSR_AE |
1032 RSR_PLE | RSR_RWTO |
1033 RSR_LCS | RSR_RF)) {
1035 if (rxhdr.RxStatus & RSR_FOE) {
1036 if (netif_msg_rx_err(db))
1037 dev_dbg(db->dev, "fifo error\n");
1038 dev->stats.rx_fifo_errors++;
1040 if (rxhdr.RxStatus & RSR_CE) {
1041 if (netif_msg_rx_err(db))
1042 dev_dbg(db->dev, "crc error\n");
1043 dev->stats.rx_crc_errors++;
1045 if (rxhdr.RxStatus & RSR_RF) {
1046 if (netif_msg_rx_err(db))
1047 dev_dbg(db->dev, "length error\n");
1048 dev->stats.rx_length_errors++;
1052 /* Move data from DM9000 */
1054 ((skb = dev_alloc_skb(RxLen + 4)) != NULL)) {
1055 skb_reserve(skb, 2);
1056 rdptr = (u8 *) skb_put(skb, RxLen - 4);
1058 /* Read received packet from RX SRAM */
1060 (db->inblk)(db->io_data, rdptr, RxLen);
1061 dev->stats.rx_bytes += RxLen;
1063 /* Pass to upper layer */
1064 skb->protocol = eth_type_trans(skb, dev);
1066 if ((((rxbyte & 0x1c) << 3) & rxbyte) == 0)
1067 skb->ip_summed = CHECKSUM_UNNECESSARY;
1069 skb->ip_summed = CHECKSUM_NONE;
1072 dev->stats.rx_packets++;
1075 /* need to dump the packet's data */
1077 (db->dumpblk)(db->io_data, RxLen);
1079 } while (rxbyte & DM9000_PKT_RDY);
1082 static irqreturn_t dm9000_interrupt(int irq, void *dev_id)
1084 struct net_device *dev = dev_id;
1085 board_info_t *db = netdev_priv(dev);
1087 unsigned long flags;
1090 dm9000_dbg(db, 3, "entering %s\n", __func__);
1092 /* A real interrupt coming */
1094 /* holders of db->lock must always block IRQs */
1095 spin_lock_irqsave(&db->lock, flags);
1097 /* Save previous register address */
1098 reg_save = readb(db->io_addr);
1100 /* Disable all interrupts */
1101 iow(db, DM9000_IMR, IMR_PAR);
1103 /* Got DM9000 interrupt status */
1104 int_status = ior(db, DM9000_ISR); /* Got ISR */
1105 iow(db, DM9000_ISR, int_status); /* Clear ISR status */
1107 if (netif_msg_intr(db))
1108 dev_dbg(db->dev, "interrupt status %02x\n", int_status);
1110 /* Received the coming packet */
1111 if (int_status & ISR_PRS)
1114 /* Trnasmit Interrupt check */
1115 if (int_status & ISR_PTS)
1116 dm9000_tx_done(dev, db);
1118 if (db->type != TYPE_DM9000E) {
1119 if (int_status & ISR_LNKCHNG) {
1120 /* fire a link-change request */
1121 schedule_delayed_work(&db->phy_poll, 1);
1125 /* Re-enable interrupt mask */
1126 iow(db, DM9000_IMR, db->imr_all);
1128 /* Restore previous register address */
1129 writeb(reg_save, db->io_addr);
1131 spin_unlock_irqrestore(&db->lock, flags);
1136 static irqreturn_t dm9000_wol_interrupt(int irq, void *dev_id)
1138 struct net_device *dev = dev_id;
1139 board_info_t *db = netdev_priv(dev);
1140 unsigned long flags;
1143 spin_lock_irqsave(&db->lock, flags);
1145 nsr = ior(db, DM9000_NSR);
1146 wcr = ior(db, DM9000_WCR);
1148 dev_dbg(db->dev, "%s: NSR=0x%02x, WCR=0x%02x\n", __func__, nsr, wcr);
1150 if (nsr & NSR_WAKEST) {
1151 /* clear, so we can avoid */
1152 iow(db, DM9000_NSR, NSR_WAKEST);
1154 if (wcr & WCR_LINKST)
1155 dev_info(db->dev, "wake by link status change\n");
1156 if (wcr & WCR_SAMPLEST)
1157 dev_info(db->dev, "wake by sample packet\n");
1158 if (wcr & WCR_MAGICST )
1159 dev_info(db->dev, "wake by magic packet\n");
1160 if (!(wcr & (WCR_LINKST | WCR_SAMPLEST | WCR_MAGICST)))
1161 dev_err(db->dev, "wake signalled with no reason? "
1162 "NSR=0x%02x, WSR=0x%02x\n", nsr, wcr);
1166 spin_unlock_irqrestore(&db->lock, flags);
1168 return (nsr & NSR_WAKEST) ? IRQ_HANDLED : IRQ_NONE;
1171 #ifdef CONFIG_NET_POLL_CONTROLLER
1175 static void dm9000_poll_controller(struct net_device *dev)
1177 disable_irq(dev->irq);
1178 dm9000_interrupt(dev->irq, dev);
1179 enable_irq(dev->irq);
1184 * Open the interface.
1185 * The interface is opened whenever "ifconfig" actives it.
1188 dm9000_open(struct net_device *dev)
1190 board_info_t *db = netdev_priv(dev);
1191 unsigned long irqflags = db->irq_res->flags & IRQF_TRIGGER_MASK;
1193 if (netif_msg_ifup(db))
1194 dev_dbg(db->dev, "enabling %s\n", dev->name);
1196 /* If there is no IRQ type specified, default to something that
1197 * may work, and tell the user that this is a problem */
1199 if (irqflags == IRQF_TRIGGER_NONE)
1200 dev_warn(db->dev, "WARNING: no IRQ resource flags set.\n");
1202 //irqflags |= IRQF_SHARED;
1204 if (request_irq(dev->irq, dm9000_interrupt, irqflags, dev->name, dev))
1207 /* Initialize DM9000 board */
1209 dm9000_init_dm9000(dev);
1211 /* Init driver variable */
1214 mii_check_media(&db->mii, netif_msg_link(db), 1);
1215 netif_start_queue(dev);
1217 dm9000_schedule_poll(db);
1223 * Sleep, either by using msleep() or if we are suspending, then
1224 * use mdelay() to sleep.
1226 static void dm9000_msleep(board_info_t *db, unsigned int ms)
1235 * Read a word from phyxcer
1238 dm9000_phy_read(struct net_device *dev, int phy_reg_unused, int reg)
1240 board_info_t *db = netdev_priv(dev);
1241 unsigned long flags;
1242 unsigned int reg_save;
1245 mutex_lock(&db->addr_lock);
1247 spin_lock_irqsave(&db->lock,flags);
1249 /* Save previous register address */
1250 reg_save = readb(db->io_addr);
1252 /* Fill the phyxcer register into REG_0C */
1253 iow(db, DM9000_EPAR, DM9000_PHY | reg);
1255 iow(db, DM9000_EPCR, EPCR_ERPRR | EPCR_EPOS); /* Issue phyxcer read command */
1257 writeb(reg_save, db->io_addr);
1258 spin_unlock_irqrestore(&db->lock,flags);
1260 dm9000_msleep(db, 1); /* Wait read complete */
1262 spin_lock_irqsave(&db->lock,flags);
1263 reg_save = readb(db->io_addr);
1265 iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer read command */
1267 /* The read data keeps on REG_0D & REG_0E */
1268 ret = (ior(db, DM9000_EPDRH) << 8) | ior(db, DM9000_EPDRL);
1270 /* restore the previous address */
1271 writeb(reg_save, db->io_addr);
1272 spin_unlock_irqrestore(&db->lock,flags);
1274 mutex_unlock(&db->addr_lock);
1276 dm9000_dbg(db, 5, "phy_read[%02x] -> %04x\n", reg, ret);
1281 * Write a word to phyxcer
1284 dm9000_phy_write(struct net_device *dev,
1285 int phyaddr_unused, int reg, int value)
1287 board_info_t *db = netdev_priv(dev);
1288 unsigned long flags;
1289 unsigned long reg_save;
1291 dm9000_dbg(db, 5, "phy_write[%02x] = %04x\n", reg, value);
1292 mutex_lock(&db->addr_lock);
1294 spin_lock_irqsave(&db->lock,flags);
1296 /* Save previous register address */
1297 reg_save = readb(db->io_addr);
1299 /* Fill the phyxcer register into REG_0C */
1300 iow(db, DM9000_EPAR, DM9000_PHY | reg);
1302 /* Fill the written data into REG_0D & REG_0E */
1303 iow(db, DM9000_EPDRL, value);
1304 iow(db, DM9000_EPDRH, value >> 8);
1306 iow(db, DM9000_EPCR, EPCR_EPOS | EPCR_ERPRW); /* Issue phyxcer write command */
1308 writeb(reg_save, db->io_addr);
1309 spin_unlock_irqrestore(&db->lock, flags);
1311 dm9000_msleep(db, 1); /* Wait write complete */
1313 spin_lock_irqsave(&db->lock,flags);
1314 reg_save = readb(db->io_addr);
1316 iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer write command */
1318 /* restore the previous address */
1319 writeb(reg_save, db->io_addr);
1321 spin_unlock_irqrestore(&db->lock, flags);
1322 mutex_unlock(&db->addr_lock);
1326 dm9000_shutdown(struct net_device *dev)
1328 board_info_t *db = netdev_priv(dev);
1331 dm9000_phy_write(dev, 0, MII_BMCR, BMCR_RESET); /* PHY RESET */
1332 iow(db, DM9000_GPR, 0x01); /* Power-Down PHY */
1333 iow(db, DM9000_IMR, IMR_PAR); /* Disable all interrupt */
1334 iow(db, DM9000_RCR, 0x00); /* Disable RX */
1338 * Stop the interface.
1339 * The interface is stopped when it is brought.
1342 dm9000_stop(struct net_device *ndev)
1344 board_info_t *db = netdev_priv(ndev);
1346 if (netif_msg_ifdown(db))
1347 dev_dbg(db->dev, "shutting down %s\n", ndev->name);
1349 cancel_delayed_work_sync(&db->phy_poll);
1351 netif_stop_queue(ndev);
1352 netif_carrier_off(ndev);
1354 /* free interrupt */
1355 free_irq(ndev->irq, ndev);
1357 dm9000_shutdown(ndev);
1362 static const struct net_device_ops dm9000_netdev_ops = {
1363 .ndo_open = dm9000_open,
1364 .ndo_stop = dm9000_stop,
1365 .ndo_start_xmit = dm9000_start_xmit,
1366 .ndo_tx_timeout = dm9000_timeout,
1367 .ndo_set_multicast_list = dm9000_hash_table,
1368 .ndo_do_ioctl = dm9000_ioctl,
1369 .ndo_change_mtu = eth_change_mtu,
1370 .ndo_validate_addr = eth_validate_addr,
1371 .ndo_set_mac_address = eth_mac_addr,
1372 #ifdef CONFIG_NET_POLL_CONTROLLER
1373 .ndo_poll_controller = dm9000_poll_controller,
1378 * Search DM9000 board, allocate space and register it
1380 static int __devinit
1381 dm9000_probe(struct platform_device *pdev)
1383 struct dm9000_plat_data *pdata = pdev->dev.platform_data;
1384 struct board_info *db; /* Point a board information structure */
1385 struct net_device *ndev;
1386 const unsigned char *mac_src;
1392 /* Init network device */
1393 ndev = alloc_etherdev(sizeof(struct board_info));
1395 dev_err(&pdev->dev, "could not allocate device.\n");
1399 SET_NETDEV_DEV(ndev, &pdev->dev);
1401 dev_dbg(&pdev->dev, "dm9000_probe()\n");
1403 /* setup board info structure */
1404 db = netdev_priv(ndev);
1406 db->dev = &pdev->dev;
1409 spin_lock_init(&db->lock);
1410 mutex_init(&db->addr_lock);
1412 INIT_DELAYED_WORK(&db->phy_poll, dm9000_poll_work);
1414 db->addr_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1415 db->data_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1416 db->irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1418 if (db->addr_res == NULL || db->data_res == NULL ||
1419 db->irq_res == NULL) {
1420 dev_err(db->dev, "insufficient resources\n");
1425 db->irq_wake = platform_get_irq(pdev, 1);
1426 if (db->irq_wake >= 0) {
1427 dev_dbg(db->dev, "wakeup irq %d\n", db->irq_wake);
1429 ret = request_irq(db->irq_wake, dm9000_wol_interrupt,
1430 IRQF_SHARED, dev_name(db->dev), ndev);
1432 dev_err(db->dev, "cannot get wakeup irq (%d)\n", ret);
1435 /* test to see if irq is really wakeup capable */
1436 ret = set_irq_wake(db->irq_wake, 1);
1438 dev_err(db->dev, "irq %d cannot set wakeup (%d)\n",
1442 set_irq_wake(db->irq_wake, 0);
1443 db->wake_supported = 1;
1448 iosize = resource_size(db->addr_res);
1449 db->addr_req = request_mem_region(db->addr_res->start, iosize,
1452 if (db->addr_req == NULL) {
1453 dev_err(db->dev, "cannot claim address reg area\n");
1458 db->io_addr = ioremap(db->addr_res->start, iosize);
1460 if (db->io_addr == NULL) {
1461 dev_err(db->dev, "failed to ioremap address reg\n");
1466 iosize = resource_size(db->data_res);
1467 db->data_req = request_mem_region(db->data_res->start, iosize,
1470 if (db->data_req == NULL) {
1471 dev_err(db->dev, "cannot claim data reg area\n");
1476 db->io_data = ioremap(db->data_res->start, iosize);
1478 if (db->io_data == NULL) {
1479 dev_err(db->dev, "failed to ioremap data reg\n");
1484 /* fill in parameters for net-dev structure */
1485 ndev->base_addr = (unsigned long)db->io_addr;
1487 //io init for dm9000 , modify by lyx@20100809
1488 if (pdata && pdata->io_init) {
1489 if (pdata->io_init()) {
1495 if (gpio_request(pdata->irq_pin, "dm9000 interrupt")) {
1496 gpio_free(pdata->irq_pin);
1497 if (pdata->io_deinit)
1499 printk("[fun:%s line:%d], request gpio for net interrupt fail\n", __func__,__LINE__);
1503 gpio_pull_updown(pdata->irq_pin, pdata->irq_pin_value);
1504 gpio_direction_input(pdata->irq_pin);
1506 ndev->irq = gpio_to_irq(pdata->irq_pin);
1507 //ndev->irq = db->irq_res->start;
1509 /* ensure at least we have a default set of IO routines */
1510 dm9000_set_io(db, iosize);
1512 /* check to see if anything is being over-ridden */
1513 if (pdata != NULL) {
1514 /* check to see if the driver wants to over-ride the
1515 * default IO width */
1517 if (pdata->flags & DM9000_PLATF_8BITONLY)
1518 dm9000_set_io(db, 1);
1520 if (pdata->flags & DM9000_PLATF_16BITONLY)
1521 dm9000_set_io(db, 2);
1523 if (pdata->flags & DM9000_PLATF_32BITONLY)
1524 dm9000_set_io(db, 4);
1526 /* check to see if there are any IO routine
1529 if (pdata->inblk != NULL)
1530 db->inblk = pdata->inblk;
1532 if (pdata->outblk != NULL)
1533 db->outblk = pdata->outblk;
1535 if (pdata->dumpblk != NULL)
1536 db->dumpblk = pdata->dumpblk;
1538 db->flags = pdata->flags;
1541 #ifdef CONFIG_DM9000_FORCE_SIMPLE_PHY_POLL
1542 db->flags |= DM9000_PLATF_SIMPLE_PHY;
1547 /* try multiple times, DM9000 sometimes gets the read wrong */
1548 for (i = 0; i < 8; i++) {
1549 id_val = ior(db, DM9000_VIDL);
1550 id_val |= (u32)ior(db, DM9000_VIDH) << 8;
1551 id_val |= (u32)ior(db, DM9000_PIDL) << 16;
1552 id_val |= (u32)ior(db, DM9000_PIDH) << 24;
1554 if (id_val == DM9000_ID)
1556 dev_err(db->dev, "read wrong id 0x%08x\n", id_val);
1559 if (id_val != DM9000_ID) {
1560 dev_err(db->dev, "wrong id: 0x%08x\n", id_val);
1565 /* Identify what type of DM9000 we are working on */
1567 id_val = ior(db, DM9000_CHIPR);
1568 dev_dbg(db->dev, "dm9000 revision 0x%02x\n", id_val);
1572 db->type = TYPE_DM9000A;
1575 db->type = TYPE_DM9000B;
1578 dev_dbg(db->dev, "ID %02x => defaulting to DM9000E\n", id_val);
1579 db->type = TYPE_DM9000E;
1582 /* dm9000a/b are capable of hardware checksum offload */
1583 if (db->type == TYPE_DM9000A || db->type == TYPE_DM9000B) {
1586 ndev->features |= NETIF_F_IP_CSUM;
1589 /* from this point we assume that we have found a DM9000 */
1591 /* driver system function */
1594 ndev->netdev_ops = &dm9000_netdev_ops;
1595 ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
1596 ndev->ethtool_ops = &dm9000_ethtool_ops;
1598 db->msg_enable = NETIF_MSG_LINK;
1599 db->mii.phy_id_mask = 0x1f;
1600 db->mii.reg_num_mask = 0x1f;
1601 db->mii.force_media = 0;
1602 db->mii.full_duplex = 0;
1604 db->mii.mdio_read = dm9000_phy_read;
1605 db->mii.mdio_write = dm9000_phy_write;
1609 /* try reading the node address from the attached EEPROM */
1610 for (i = 0; i < 6; i += 2)
1611 dm9000_read_eeprom(db, i / 2, ndev->dev_addr+i);
1613 if (!is_valid_ether_addr(ndev->dev_addr) && pdata != NULL) {
1614 mac_src = "platform data";
1615 memcpy(ndev->dev_addr, pdata->dev_addr, 6);
1618 if (!is_valid_ether_addr(ndev->dev_addr)) {
1619 /* try reading from mac */
1622 for (i = 0; i < 6; i++)
1623 ndev->dev_addr[i] = ior(db, i+DM9000_PAR);
1626 if (!is_valid_ether_addr(ndev->dev_addr))
1627 dev_warn(db->dev, "%s: Invalid ethernet MAC address. Please "
1628 "set using ifconfig\n", ndev->name);
1630 platform_set_drvdata(pdev, ndev);
1631 ret = register_netdev(ndev);
1634 printk(KERN_INFO "%s: dm9000%c at %p,%p IRQ %d MAC: %pM (%s)\n",
1635 ndev->name, dm9000_type_to_char(db->type),
1636 db->io_addr, db->io_data, ndev->irq,
1637 ndev->dev_addr, mac_src);
1639 dm9000_shutdown(ndev);//add by lyx@20100713, reduce power consume
1644 dev_err(db->dev, "not found (%d).\n", ret);
1646 dm9000_release_board(pdev, db);
1653 dm9000_drv_suspend(struct device *dev)
1655 struct platform_device *pdev = to_platform_device(dev);
1656 struct net_device *ndev = platform_get_drvdata(pdev);
1660 db = netdev_priv(ndev);
1663 if (!netif_running(ndev))
1666 netif_device_detach(ndev);
1668 /* only shutdown if not using WoL */
1669 if (!db->wake_state)
1670 dm9000_shutdown(ndev);
1676 dm9000_drv_resume(struct device *dev)
1678 struct platform_device *pdev = to_platform_device(dev);
1679 struct net_device *ndev = platform_get_drvdata(pdev);
1680 board_info_t *db = netdev_priv(ndev);
1683 if (netif_running(ndev)) {
1684 /* reset if we were not in wake mode to ensure if
1685 * the device was powered off it is in a known state */
1686 if (!db->wake_state) {
1688 dm9000_init_dm9000(ndev);
1691 netif_device_attach(ndev);
1699 static const struct dev_pm_ops dm9000_drv_pm_ops = {
1700 .suspend = dm9000_drv_suspend,
1701 .resume = dm9000_drv_resume,
1704 static int __devexit
1705 dm9000_drv_remove(struct platform_device *pdev)
1707 struct net_device *ndev = platform_get_drvdata(pdev);
1708 struct dm9000_plat_data *pdata = pdev->dev.platform_data;
1710 //deinit io for dm9000
1711 gpio_free(pdata->irq_pin);
1712 if (pdata && pdata->io_deinit)
1715 platform_set_drvdata(pdev, NULL);
1717 unregister_netdev(ndev);
1718 dm9000_release_board(pdev, (board_info_t *) netdev_priv(ndev));
1719 free_netdev(ndev); /* free device structure */
1721 dev_dbg(&pdev->dev, "released and freed device\n");
1725 static struct platform_driver dm9000_driver = {
1728 .owner = THIS_MODULE,
1729 .pm = &dm9000_drv_pm_ops,
1731 .probe = dm9000_probe,
1732 .remove = __devexit_p(dm9000_drv_remove),
1738 printk(KERN_INFO "%s Ethernet Driver, V%s\n", CARDNAME, DRV_VERSION);
1740 return platform_driver_register(&dm9000_driver);
1744 dm9000_cleanup(void)
1746 platform_driver_unregister(&dm9000_driver);
1749 module_init(dm9000_init);
1750 module_exit(dm9000_cleanup);
1752 MODULE_AUTHOR("Sascha Hauer, Ben Dooks");
1753 MODULE_DESCRIPTION("Davicom DM9000 network driver");
1754 MODULE_LICENSE("GPL");
1755 MODULE_ALIAS("platform:dm9000");