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>
37 #include <asm/delay.h>
43 /* Board/System/Debug information/definition ---------------- */
45 #define DM9000_PHY 0x40 /* PHY address 0x01 */
47 #define CARDNAME "dm9000"
48 #define DRV_VERSION "1.31"
50 #ifdef CONFIG_BLACKFIN
57 #define DEFAULT_TRIGGER IRQF_TRIGGER_HIGH
59 #define DEFAULT_TRIGGER (0)
63 * Transmit timeout, default 5 seconds.
65 static int watchdog = 5000;
66 module_param(watchdog, int, 0400);
67 MODULE_PARM_DESC(watchdog, "transmit timeout in milliseconds");
69 /* DM9000 register address locking.
71 * The DM9000 uses an address register to control where data written
72 * to the data register goes. This means that the address register
73 * must be preserved over interrupts or similar calls.
75 * During interrupt and other critical calls, a spinlock is used to
76 * protect the system, but the calls themselves save the address
77 * in the address register in case they are interrupting another
78 * access to the device.
80 * For general accesses a lock is provided so that calls which are
81 * allowed to sleep are serialised so that the address register does
82 * not need to be saved. This lock also serves to serialise access
83 * to the EEPROM and PHY access registers which are shared between
87 /* The driver supports the original DM9000E, and now the two newer
88 * devices, DM9000A and DM9000B.
92 TYPE_DM9000E, /* original DM9000 */
97 /* Structure/enum declaration ------------------------------- */
98 typedef struct board_info {
100 void __iomem *io_addr; /* Register I/O base address */
101 void __iomem *io_data; /* Data I/O address */
106 u16 queue_start_addr;
108 u8 io_mode; /* 0:word, 2:byte */
113 unsigned int in_suspend :1;
116 enum dm9000_type type;
118 void (*inblk)(void __iomem *port, void *data, int length);
119 void (*outblk)(void __iomem *port, void *data, int length);
120 void (*dumpblk)(void __iomem *port, int length);
122 struct device *dev; /* parent device */
124 struct resource *addr_res; /* resources found */
125 struct resource *data_res;
126 struct resource *addr_req; /* resources requested */
127 struct resource *data_req;
128 struct resource *irq_res;
130 struct mutex addr_lock; /* phy and eeprom access lock */
132 struct delayed_work phy_poll;
133 struct net_device *ndev;
137 struct mii_if_info mii;
143 #define dm9000_dbg(db, lev, msg...) do { \
144 if ((lev) < CONFIG_DM9000_DEBUGLEVEL && \
145 (lev) < db->debug_level) { \
146 dev_dbg(db->dev, msg); \
150 static inline board_info_t *to_dm9000_board(struct net_device *dev)
155 /* DM9000 network board routine ---------------------------- */
158 dm9000_reset(board_info_t * db)
160 dev_dbg(db->dev, "resetting device\n");
163 writeb(DM9000_NCR, db->io_addr);
165 writeb(NCR_RST, db->io_data);
170 * Read a byte from I/O port
173 ior(board_info_t * db, int reg)
175 writeb(reg, db->io_addr);
176 return readb(db->io_data);
180 * Write a byte to I/O port
184 iow(board_info_t * db, int reg, int value)
186 writeb(reg, db->io_addr);
187 writeb(value, db->io_data);
190 /* routines for sending block to chip */
192 static void dm9000_outblk_8bit(void __iomem *reg, void *data, int count)
194 writesb(reg, data, count);
197 static void dm9000_outblk_16bit(void __iomem *reg, void *data, int count)
199 writesw(reg, data, (count+1) >> 1);
202 static void dm9000_outblk_32bit(void __iomem *reg, void *data, int count)
204 writesl(reg, data, (count+3) >> 2);
207 /* input block from chip to memory */
209 static void dm9000_inblk_8bit(void __iomem *reg, void *data, int count)
211 readsb(reg, data, count);
215 static void dm9000_inblk_16bit(void __iomem *reg, void *data, int count)
217 readsw(reg, data, (count+1) >> 1);
220 static void dm9000_inblk_32bit(void __iomem *reg, void *data, int count)
222 readsl(reg, data, (count+3) >> 2);
225 /* dump block from chip to null */
227 static void dm9000_dumpblk_8bit(void __iomem *reg, int count)
232 for (i = 0; i < count; i++)
236 static void dm9000_dumpblk_16bit(void __iomem *reg, int count)
241 count = (count + 1) >> 1;
243 for (i = 0; i < count; i++)
247 static void dm9000_dumpblk_32bit(void __iomem *reg, int count)
252 count = (count + 3) >> 2;
254 for (i = 0; i < count; i++)
260 * select the specified set of io routines to use with the
264 static void dm9000_set_io(struct board_info *db, int byte_width)
266 /* use the size of the data resource to work out what IO
267 * routines we want to use
270 switch (byte_width) {
272 db->dumpblk = dm9000_dumpblk_8bit;
273 db->outblk = dm9000_outblk_8bit;
274 db->inblk = dm9000_inblk_8bit;
279 dev_dbg(db->dev, ": 3 byte IO, falling back to 16bit\n");
281 db->dumpblk = dm9000_dumpblk_16bit;
282 db->outblk = dm9000_outblk_16bit;
283 db->inblk = dm9000_inblk_16bit;
288 db->dumpblk = dm9000_dumpblk_32bit;
289 db->outblk = dm9000_outblk_32bit;
290 db->inblk = dm9000_inblk_32bit;
295 static void dm9000_schedule_poll(board_info_t *db)
297 if (db->type == TYPE_DM9000E)
298 schedule_delayed_work(&db->phy_poll, HZ * 2);
301 static int dm9000_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
303 board_info_t *dm = to_dm9000_board(dev);
305 if (!netif_running(dev))
308 return generic_mii_ioctl(&dm->mii, if_mii(req), cmd, NULL);
312 dm9000_read_locked(board_info_t *db, int reg)
317 spin_lock_irqsave(&db->lock, flags);
319 spin_unlock_irqrestore(&db->lock, flags);
324 static int dm9000_wait_eeprom(board_info_t *db)
327 int timeout = 8; /* wait max 8msec */
329 /* The DM9000 data sheets say we should be able to
330 * poll the ERRE bit in EPCR to wait for the EEPROM
331 * operation. From testing several chips, this bit
332 * does not seem to work.
334 * We attempt to use the bit, but fall back to the
335 * timeout (which is why we do not return an error
336 * on expiry) to say that the EEPROM operation has
341 status = dm9000_read_locked(db, DM9000_EPCR);
343 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 u32 dm9000_get_link(struct net_device *dev)
475 board_info_t *dm = to_dm9000_board(dev);
478 if (dm->flags & DM9000_PLATF_EXT_PHY)
479 ret = mii_link_ok(&dm->mii);
481 ret = dm9000_read_locked(dm, DM9000_NSR) & NSR_LINKST ? 1 : 0;
486 #define DM_EEPROM_MAGIC (0x444D394B)
488 static int dm9000_get_eeprom_len(struct net_device *dev)
493 static int dm9000_get_eeprom(struct net_device *dev,
494 struct ethtool_eeprom *ee, u8 *data)
496 board_info_t *dm = to_dm9000_board(dev);
497 int offset = ee->offset;
501 /* EEPROM access is aligned to two bytes */
503 if ((len & 1) != 0 || (offset & 1) != 0)
506 if (dm->flags & DM9000_PLATF_NO_EEPROM)
509 ee->magic = DM_EEPROM_MAGIC;
511 for (i = 0; i < len; i += 2)
512 dm9000_read_eeprom(dm, (offset + i) / 2, data + i);
517 static int dm9000_set_eeprom(struct net_device *dev,
518 struct ethtool_eeprom *ee, u8 *data)
520 board_info_t *dm = to_dm9000_board(dev);
521 int offset = ee->offset;
525 /* EEPROM access is aligned to two bytes */
527 if ((len & 1) != 0 || (offset & 1) != 0)
530 if (dm->flags & DM9000_PLATF_NO_EEPROM)
533 if (ee->magic != DM_EEPROM_MAGIC)
536 for (i = 0; i < len; i += 2)
537 dm9000_write_eeprom(dm, (offset + i) / 2, data + i);
542 static const struct ethtool_ops dm9000_ethtool_ops = {
543 .get_drvinfo = dm9000_get_drvinfo,
544 .get_settings = dm9000_get_settings,
545 .set_settings = dm9000_set_settings,
546 .get_msglevel = dm9000_get_msglevel,
547 .set_msglevel = dm9000_set_msglevel,
548 .nway_reset = dm9000_nway_reset,
549 .get_link = dm9000_get_link,
550 .get_eeprom_len = dm9000_get_eeprom_len,
551 .get_eeprom = dm9000_get_eeprom,
552 .set_eeprom = dm9000_set_eeprom,
556 dm9000_poll_work(struct work_struct *w)
558 struct delayed_work *dw = container_of(w, struct delayed_work, work);
559 board_info_t *db = container_of(dw, board_info_t, phy_poll);
561 mii_check_media(&db->mii, netif_msg_link(db), 0);
563 if (netif_running(db->ndev))
564 dm9000_schedule_poll(db);
567 /* dm9000_release_board
569 * release a board, and any mapped resources
573 dm9000_release_board(struct platform_device *pdev, struct board_info *db)
575 /* unmap our resources */
577 iounmap(db->io_addr);
578 iounmap(db->io_data);
580 /* release the resources */
582 release_resource(db->data_req);
585 release_resource(db->addr_req);
589 static unsigned char dm9000_type_to_char(enum dm9000_type type)
592 case TYPE_DM9000E: return 'e';
593 case TYPE_DM9000A: return 'a';
594 case TYPE_DM9000B: return 'b';
601 * Set DM9000 multicast address
604 dm9000_hash_table(struct net_device *dev)
606 board_info_t *db = (board_info_t *) dev->priv;
607 struct dev_mc_list *mcptr = dev->mc_list;
608 int mc_cnt = dev->mc_count;
612 u8 rcr = RCR_DIS_LONG | RCR_DIS_CRC | RCR_RXEN;
615 dm9000_dbg(db, 1, "entering %s\n", __func__);
617 spin_lock_irqsave(&db->lock, flags);
619 for (i = 0, oft = DM9000_PAR; i < 6; i++, oft++)
620 iow(db, oft, dev->dev_addr[i]);
622 /* Clear Hash Table */
623 for (i = 0; i < 4; i++)
626 /* broadcast address */
627 hash_table[3] = 0x8000;
629 if (dev->flags & IFF_PROMISC)
632 if (dev->flags & IFF_ALLMULTI)
635 /* the multicast address in Hash Table : 64 bits */
636 for (i = 0; i < mc_cnt; i++, mcptr = mcptr->next) {
637 hash_val = ether_crc_le(6, mcptr->dmi_addr) & 0x3f;
638 hash_table[hash_val / 16] |= (u16) 1 << (hash_val % 16);
641 /* Write the hash table to MAC MD table */
642 for (i = 0, oft = DM9000_MAR; i < 4; i++) {
643 iow(db, oft++, hash_table[i]);
644 iow(db, oft++, hash_table[i] >> 8);
647 iow(db, DM9000_RCR, rcr);
648 spin_unlock_irqrestore(&db->lock, flags);
652 * Initilize dm9000 board
655 dm9000_init_dm9000(struct net_device *dev)
657 board_info_t *db = dev->priv;
660 dm9000_dbg(db, 1, "entering %s\n", __func__);
663 db->io_mode = ior(db, DM9000_ISR) >> 6; /* ISR bit7:6 keeps I/O mode */
665 /* GPIO0 on pre-activate PHY */
666 iow(db, DM9000_GPR, 0); /* REG_1F bit0 activate phyxcer */
667 iow(db, DM9000_GPCR, GPCR_GEP_CNTL); /* Let GPIO0 output */
668 iow(db, DM9000_GPR, 0); /* Enable PHY */
670 if (db->flags & DM9000_PLATF_EXT_PHY)
671 iow(db, DM9000_NCR, NCR_EXT_PHY);
673 /* Program operating register */
674 iow(db, DM9000_TCR, 0); /* TX Polling clear */
675 iow(db, DM9000_BPTR, 0x3f); /* Less 3Kb, 200us */
676 iow(db, DM9000_FCR, 0xff); /* Flow Control */
677 iow(db, DM9000_SMCR, 0); /* Special Mode */
678 /* clear TX status */
679 iow(db, DM9000_NSR, NSR_WAKEST | NSR_TX2END | NSR_TX1END);
680 iow(db, DM9000_ISR, ISR_CLR_STATUS); /* Clear interrupt status */
682 /* Set address filter table */
683 dm9000_hash_table(dev);
685 imr = IMR_PAR | IMR_PTM | IMR_PRM;
686 if (db->type != TYPE_DM9000E)
691 /* Enable TX/RX interrupt mask */
692 iow(db, DM9000_IMR, imr);
694 /* Init Driver variable */
696 db->queue_pkt_len = 0;
697 dev->trans_start = 0;
700 /* Our watchdog timed out. Called by the networking layer */
701 static void dm9000_timeout(struct net_device *dev)
703 board_info_t *db = (board_info_t *) dev->priv;
707 /* Save previous register address */
708 reg_save = readb(db->io_addr);
709 spin_lock_irqsave(&db->lock, flags);
711 netif_stop_queue(dev);
713 dm9000_init_dm9000(dev);
714 /* We can accept TX packets again */
715 dev->trans_start = jiffies;
716 netif_wake_queue(dev);
718 /* Restore previous register address */
719 writeb(reg_save, db->io_addr);
720 spin_unlock_irqrestore(&db->lock, flags);
724 * Hardware start transmission.
725 * Send a packet to media from the upper layer.
728 dm9000_start_xmit(struct sk_buff *skb, struct net_device *dev)
731 board_info_t *db = dev->priv;
733 dm9000_dbg(db, 3, "%s:\n", __func__);
735 if (db->tx_pkt_cnt > 1)
738 spin_lock_irqsave(&db->lock, flags);
740 /* Move data to DM9000 TX RAM */
741 writeb(DM9000_MWCMD, db->io_addr);
743 (db->outblk)(db->io_data, skb->data, skb->len);
744 dev->stats.tx_bytes += skb->len;
747 /* TX control: First packet immediately send, second packet queue */
748 if (db->tx_pkt_cnt == 1) {
749 /* Set TX length to DM9000 */
750 iow(db, DM9000_TXPLL, skb->len);
751 iow(db, DM9000_TXPLH, skb->len >> 8);
753 /* Issue TX polling command */
754 iow(db, DM9000_TCR, TCR_TXREQ); /* Cleared after TX complete */
756 dev->trans_start = jiffies; /* save the time stamp */
759 db->queue_pkt_len = skb->len;
760 netif_stop_queue(dev);
763 spin_unlock_irqrestore(&db->lock, flags);
772 * DM9000 interrupt handler
773 * receive the packet to upper layer, free the transmitted packet
776 static void dm9000_tx_done(struct net_device *dev, board_info_t *db)
778 int tx_status = ior(db, DM9000_NSR); /* Got TX status */
780 if (tx_status & (NSR_TX2END | NSR_TX1END)) {
781 /* One packet sent complete */
783 dev->stats.tx_packets++;
785 if (netif_msg_tx_done(db))
786 dev_dbg(db->dev, "tx done, NSR %02x\n", tx_status);
788 /* Queue packet check & send */
789 if (db->tx_pkt_cnt > 0) {
790 iow(db, DM9000_TXPLL, db->queue_pkt_len);
791 iow(db, DM9000_TXPLH, db->queue_pkt_len >> 8);
792 iow(db, DM9000_TCR, TCR_TXREQ);
793 dev->trans_start = jiffies;
795 netif_wake_queue(dev);
799 struct dm9000_rxhdr {
803 } __attribute__((__packed__));
806 * Received a packet and pass to upper layer
809 dm9000_rx(struct net_device *dev)
811 board_info_t *db = (board_info_t *) dev->priv;
812 struct dm9000_rxhdr rxhdr;
818 /* Check packet ready or not */
820 ior(db, DM9000_MRCMDX); /* Dummy read */
822 /* Get most updated data */
823 rxbyte = readb(db->io_data);
825 /* Status check: this byte must be 0 or 1 */
826 if (rxbyte > DM9000_PKT_RDY) {
827 dev_warn(db->dev, "status check fail: %d\n", rxbyte);
828 iow(db, DM9000_RCR, 0x00); /* Stop Device */
829 iow(db, DM9000_ISR, IMR_PAR); /* Stop INT request */
833 if (rxbyte != DM9000_PKT_RDY)
836 /* A packet ready now & Get status/length */
838 writeb(DM9000_MRCMD, db->io_addr);
840 (db->inblk)(db->io_data, &rxhdr, sizeof(rxhdr));
842 RxLen = le16_to_cpu(rxhdr.RxLen);
844 if (netif_msg_rx_status(db))
845 dev_dbg(db->dev, "RX: status %02x, length %04x\n",
846 rxhdr.RxStatus, RxLen);
848 /* Packet Status check */
851 if (netif_msg_rx_err(db))
852 dev_dbg(db->dev, "RX: Bad Packet (runt)\n");
855 if (RxLen > DM9000_PKT_MAX) {
856 dev_dbg(db->dev, "RST: RX Len:%x\n", RxLen);
859 if (rxhdr.RxStatus & 0xbf) {
861 if (rxhdr.RxStatus & 0x01) {
862 if (netif_msg_rx_err(db))
863 dev_dbg(db->dev, "fifo error\n");
864 dev->stats.rx_fifo_errors++;
866 if (rxhdr.RxStatus & 0x02) {
867 if (netif_msg_rx_err(db))
868 dev_dbg(db->dev, "crc error\n");
869 dev->stats.rx_crc_errors++;
871 if (rxhdr.RxStatus & 0x80) {
872 if (netif_msg_rx_err(db))
873 dev_dbg(db->dev, "length error\n");
874 dev->stats.rx_length_errors++;
878 /* Move data from DM9000 */
880 && ((skb = dev_alloc_skb(RxLen + 4)) != NULL)) {
882 rdptr = (u8 *) skb_put(skb, RxLen - 4);
884 /* Read received packet from RX SRAM */
886 (db->inblk)(db->io_data, rdptr, RxLen);
887 dev->stats.rx_bytes += RxLen;
889 /* Pass to upper layer */
890 skb->protocol = eth_type_trans(skb, dev);
892 dev->stats.rx_packets++;
895 /* need to dump the packet's data */
897 (db->dumpblk)(db->io_data, RxLen);
899 } while (rxbyte == DM9000_PKT_RDY);
902 static irqreturn_t dm9000_interrupt(int irq, void *dev_id)
904 struct net_device *dev = dev_id;
905 board_info_t *db = dev->priv;
909 dm9000_dbg(db, 3, "entering %s\n", __func__);
911 /* A real interrupt coming */
913 spin_lock(&db->lock);
915 /* Save previous register address */
916 reg_save = readb(db->io_addr);
918 /* Disable all interrupts */
919 iow(db, DM9000_IMR, IMR_PAR);
921 /* Got DM9000 interrupt status */
922 int_status = ior(db, DM9000_ISR); /* Got ISR */
923 iow(db, DM9000_ISR, int_status); /* Clear ISR status */
925 if (netif_msg_intr(db))
926 dev_dbg(db->dev, "interrupt status %02x\n", int_status);
928 /* Received the coming packet */
929 if (int_status & ISR_PRS)
932 /* Trnasmit Interrupt check */
933 if (int_status & ISR_PTS)
934 dm9000_tx_done(dev, db);
936 if (db->type != TYPE_DM9000E) {
937 if (int_status & ISR_LNKCHNG) {
938 /* fire a link-change request */
939 schedule_delayed_work(&db->phy_poll, 1);
943 /* Re-enable interrupt mask */
944 iow(db, DM9000_IMR, db->imr_all);
946 /* Restore previous register address */
947 writeb(reg_save, db->io_addr);
949 spin_unlock(&db->lock);
954 #ifdef CONFIG_NET_POLL_CONTROLLER
958 static void dm9000_poll_controller(struct net_device *dev)
960 disable_irq(dev->irq);
961 dm9000_interrupt(dev->irq, dev);
962 enable_irq(dev->irq);
967 * Open the interface.
968 * The interface is opened whenever "ifconfig" actives it.
971 dm9000_open(struct net_device *dev)
973 board_info_t *db = dev->priv;
974 unsigned long irqflags = db->irq_res->flags & IRQF_TRIGGER_MASK;
976 if (netif_msg_ifup(db))
977 dev_dbg(db->dev, "enabling %s\n", dev->name);
979 /* If there is no IRQ type specified, default to something that
980 * may work, and tell the user that this is a problem */
982 if (irqflags == IRQF_TRIGGER_NONE) {
983 dev_warn(db->dev, "WARNING: no IRQ resource flags set.\n");
984 irqflags = DEFAULT_TRIGGER;
987 irqflags |= IRQF_SHARED;
989 if (request_irq(dev->irq, &dm9000_interrupt, irqflags, dev->name, dev))
992 /* Initialize DM9000 board */
994 dm9000_init_dm9000(dev);
996 /* Init driver variable */
999 mii_check_media(&db->mii, netif_msg_link(db), 1);
1000 netif_start_queue(dev);
1002 dm9000_schedule_poll(db);
1008 * Sleep, either by using msleep() or if we are suspending, then
1009 * use mdelay() to sleep.
1011 static void dm9000_msleep(board_info_t *db, unsigned int ms)
1020 * Read a word from phyxcer
1023 dm9000_phy_read(struct net_device *dev, int phy_reg_unused, int reg)
1025 board_info_t *db = (board_info_t *) dev->priv;
1026 unsigned long flags;
1027 unsigned int reg_save;
1030 mutex_lock(&db->addr_lock);
1032 spin_lock_irqsave(&db->lock,flags);
1034 /* Save previous register address */
1035 reg_save = readb(db->io_addr);
1037 /* Fill the phyxcer register into REG_0C */
1038 iow(db, DM9000_EPAR, DM9000_PHY | reg);
1040 iow(db, DM9000_EPCR, 0xc); /* Issue phyxcer read command */
1042 writeb(reg_save, db->io_addr);
1043 spin_unlock_irqrestore(&db->lock,flags);
1045 dm9000_msleep(db, 1); /* Wait read complete */
1047 spin_lock_irqsave(&db->lock,flags);
1048 reg_save = readb(db->io_addr);
1050 iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer read command */
1052 /* The read data keeps on REG_0D & REG_0E */
1053 ret = (ior(db, DM9000_EPDRH) << 8) | ior(db, DM9000_EPDRL);
1055 /* restore the previous address */
1056 writeb(reg_save, db->io_addr);
1057 spin_unlock_irqrestore(&db->lock,flags);
1059 mutex_unlock(&db->addr_lock);
1061 dm9000_dbg(db, 5, "phy_read[%02x] -> %04x\n", reg, ret);
1066 * Write a word to phyxcer
1069 dm9000_phy_write(struct net_device *dev,
1070 int phyaddr_unused, int reg, int value)
1072 board_info_t *db = (board_info_t *) dev->priv;
1073 unsigned long flags;
1074 unsigned long reg_save;
1076 dm9000_dbg(db, 5, "phy_write[%02x] = %04x\n", reg, value);
1077 mutex_lock(&db->addr_lock);
1079 spin_lock_irqsave(&db->lock,flags);
1081 /* Save previous register address */
1082 reg_save = readb(db->io_addr);
1084 /* Fill the phyxcer register into REG_0C */
1085 iow(db, DM9000_EPAR, DM9000_PHY | reg);
1087 /* Fill the written data into REG_0D & REG_0E */
1088 iow(db, DM9000_EPDRL, value);
1089 iow(db, DM9000_EPDRH, value >> 8);
1091 iow(db, DM9000_EPCR, 0xa); /* Issue phyxcer write command */
1093 writeb(reg_save, db->io_addr);
1094 spin_unlock_irqrestore(&db->lock, flags);
1096 dm9000_msleep(db, 1); /* Wait write complete */
1098 spin_lock_irqsave(&db->lock,flags);
1099 reg_save = readb(db->io_addr);
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);
1107 mutex_unlock(&db->addr_lock);
1111 dm9000_shutdown(struct net_device *dev)
1113 board_info_t *db = dev->priv;
1116 dm9000_phy_write(dev, 0, MII_BMCR, BMCR_RESET); /* PHY RESET */
1117 iow(db, DM9000_GPR, 0x01); /* Power-Down PHY */
1118 iow(db, DM9000_IMR, IMR_PAR); /* Disable all interrupt */
1119 iow(db, DM9000_RCR, 0x00); /* Disable RX */
1123 * Stop the interface.
1124 * The interface is stopped when it is brought.
1127 dm9000_stop(struct net_device *ndev)
1129 board_info_t *db = ndev->priv;
1131 if (netif_msg_ifdown(db))
1132 dev_dbg(db->dev, "shutting down %s\n", ndev->name);
1134 cancel_delayed_work_sync(&db->phy_poll);
1136 netif_stop_queue(ndev);
1137 netif_carrier_off(ndev);
1139 /* free interrupt */
1140 free_irq(ndev->irq, ndev);
1142 dm9000_shutdown(ndev);
1147 #define res_size(_r) (((_r)->end - (_r)->start) + 1)
1150 * Search DM9000 board, allocate space and register it
1152 static int __devinit
1153 dm9000_probe(struct platform_device *pdev)
1155 struct dm9000_plat_data *pdata = pdev->dev.platform_data;
1156 struct board_info *db; /* Point a board information structure */
1157 struct net_device *ndev;
1158 const unsigned char *mac_src;
1164 /* Init network device */
1165 ndev = alloc_etherdev(sizeof(struct board_info));
1167 dev_err(&pdev->dev, "could not allocate device.\n");
1171 SET_NETDEV_DEV(ndev, &pdev->dev);
1173 dev_dbg(&pdev->dev, "dm9000_probe()\n");
1175 /* setup board info structure */
1177 memset(db, 0, sizeof(*db));
1179 db->dev = &pdev->dev;
1182 spin_lock_init(&db->lock);
1183 mutex_init(&db->addr_lock);
1185 INIT_DELAYED_WORK(&db->phy_poll, dm9000_poll_work);
1187 db->addr_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1188 db->data_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1189 db->irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1191 if (db->addr_res == NULL || db->data_res == NULL ||
1192 db->irq_res == NULL) {
1193 dev_err(db->dev, "insufficient resources\n");
1198 iosize = res_size(db->addr_res);
1199 db->addr_req = request_mem_region(db->addr_res->start, iosize,
1202 if (db->addr_req == NULL) {
1203 dev_err(db->dev, "cannot claim address reg area\n");
1208 db->io_addr = ioremap(db->addr_res->start, iosize);
1210 if (db->io_addr == NULL) {
1211 dev_err(db->dev, "failed to ioremap address reg\n");
1216 iosize = res_size(db->data_res);
1217 db->data_req = request_mem_region(db->data_res->start, iosize,
1220 if (db->data_req == NULL) {
1221 dev_err(db->dev, "cannot claim data reg area\n");
1226 db->io_data = ioremap(db->data_res->start, iosize);
1228 if (db->io_data == NULL) {
1229 dev_err(db->dev, "failed to ioremap data reg\n");
1234 /* fill in parameters for net-dev structure */
1235 ndev->base_addr = (unsigned long)db->io_addr;
1236 ndev->irq = db->irq_res->start;
1238 /* ensure at least we have a default set of IO routines */
1239 dm9000_set_io(db, iosize);
1241 /* check to see if anything is being over-ridden */
1242 if (pdata != NULL) {
1243 /* check to see if the driver wants to over-ride the
1244 * default IO width */
1246 if (pdata->flags & DM9000_PLATF_8BITONLY)
1247 dm9000_set_io(db, 1);
1249 if (pdata->flags & DM9000_PLATF_16BITONLY)
1250 dm9000_set_io(db, 2);
1252 if (pdata->flags & DM9000_PLATF_32BITONLY)
1253 dm9000_set_io(db, 4);
1255 /* check to see if there are any IO routine
1258 if (pdata->inblk != NULL)
1259 db->inblk = pdata->inblk;
1261 if (pdata->outblk != NULL)
1262 db->outblk = pdata->outblk;
1264 if (pdata->dumpblk != NULL)
1265 db->dumpblk = pdata->dumpblk;
1267 db->flags = pdata->flags;
1272 /* try multiple times, DM9000 sometimes gets the read wrong */
1273 for (i = 0; i < 8; i++) {
1274 id_val = ior(db, DM9000_VIDL);
1275 id_val |= (u32)ior(db, DM9000_VIDH) << 8;
1276 id_val |= (u32)ior(db, DM9000_PIDL) << 16;
1277 id_val |= (u32)ior(db, DM9000_PIDH) << 24;
1279 if (id_val == DM9000_ID)
1281 dev_err(db->dev, "read wrong id 0x%08x\n", id_val);
1284 if (id_val != DM9000_ID) {
1285 dev_err(db->dev, "wrong id: 0x%08x\n", id_val);
1290 /* Identify what type of DM9000 we are working on */
1292 id_val = ior(db, DM9000_CHIPR);
1293 dev_dbg(db->dev, "dm9000 revision 0x%02x\n", id_val);
1297 db->type = TYPE_DM9000A;
1300 db->type = TYPE_DM9000B;
1303 dev_dbg(db->dev, "ID %02x => defaulting to DM9000E\n", id_val);
1304 db->type = TYPE_DM9000E;
1307 /* from this point we assume that we have found a DM9000 */
1309 /* driver system function */
1312 ndev->open = &dm9000_open;
1313 ndev->hard_start_xmit = &dm9000_start_xmit;
1314 ndev->tx_timeout = &dm9000_timeout;
1315 ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
1316 ndev->stop = &dm9000_stop;
1317 ndev->set_multicast_list = &dm9000_hash_table;
1318 ndev->ethtool_ops = &dm9000_ethtool_ops;
1319 ndev->do_ioctl = &dm9000_ioctl;
1321 #ifdef CONFIG_NET_POLL_CONTROLLER
1322 ndev->poll_controller = &dm9000_poll_controller;
1325 db->msg_enable = NETIF_MSG_LINK;
1326 db->mii.phy_id_mask = 0x1f;
1327 db->mii.reg_num_mask = 0x1f;
1328 db->mii.force_media = 0;
1329 db->mii.full_duplex = 0;
1331 db->mii.mdio_read = dm9000_phy_read;
1332 db->mii.mdio_write = dm9000_phy_write;
1336 /* try reading the node address from the attached EEPROM */
1337 for (i = 0; i < 6; i += 2)
1338 dm9000_read_eeprom(db, i / 2, ndev->dev_addr+i);
1340 if (!is_valid_ether_addr(ndev->dev_addr)) {
1341 /* try reading from mac */
1344 for (i = 0; i < 6; i++)
1345 ndev->dev_addr[i] = ior(db, i+DM9000_PAR);
1348 if (!is_valid_ether_addr(ndev->dev_addr))
1349 dev_warn(db->dev, "%s: Invalid ethernet MAC address. Please "
1350 "set using ifconfig\n", ndev->name);
1352 platform_set_drvdata(pdev, ndev);
1353 ret = register_netdev(ndev);
1356 DECLARE_MAC_BUF(mac);
1357 printk(KERN_INFO "%s: dm9000%c at %p,%p IRQ %d MAC: %s (%s)\n",
1358 ndev->name, dm9000_type_to_char(db->type),
1359 db->io_addr, db->io_data, ndev->irq,
1360 print_mac(mac, ndev->dev_addr), mac_src);
1365 dev_err(db->dev, "not found (%d).\n", ret);
1367 dm9000_release_board(pdev, db);
1374 dm9000_drv_suspend(struct platform_device *dev, pm_message_t state)
1376 struct net_device *ndev = platform_get_drvdata(dev);
1380 db = (board_info_t *) ndev->priv;
1383 if (netif_running(ndev)) {
1384 netif_device_detach(ndev);
1385 dm9000_shutdown(ndev);
1392 dm9000_drv_resume(struct platform_device *dev)
1394 struct net_device *ndev = platform_get_drvdata(dev);
1395 board_info_t *db = (board_info_t *) ndev->priv;
1399 if (netif_running(ndev)) {
1401 dm9000_init_dm9000(ndev);
1403 netif_device_attach(ndev);
1411 static int __devexit
1412 dm9000_drv_remove(struct platform_device *pdev)
1414 struct net_device *ndev = platform_get_drvdata(pdev);
1416 platform_set_drvdata(pdev, NULL);
1418 unregister_netdev(ndev);
1419 dm9000_release_board(pdev, (board_info_t *) ndev->priv);
1420 free_netdev(ndev); /* free device structure */
1422 dev_dbg(&pdev->dev, "released and freed device\n");
1426 static struct platform_driver dm9000_driver = {
1429 .owner = THIS_MODULE,
1431 .probe = dm9000_probe,
1432 .remove = __devexit_p(dm9000_drv_remove),
1433 .suspend = dm9000_drv_suspend,
1434 .resume = dm9000_drv_resume,
1440 printk(KERN_INFO "%s Ethernet Driver, V%s\n", CARDNAME, DRV_VERSION);
1442 return platform_driver_register(&dm9000_driver);
1446 dm9000_cleanup(void)
1448 platform_driver_unregister(&dm9000_driver);
1451 module_init(dm9000_init);
1452 module_exit(dm9000_cleanup);
1454 MODULE_AUTHOR("Sascha Hauer, Ben Dooks");
1455 MODULE_DESCRIPTION("Davicom DM9000 network driver");
1456 MODULE_LICENSE("GPL");
1457 MODULE_ALIAS("platform:dm9000");