Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / drivers / net / dm9000.c
blobd63cc93f055dfd2be2c8a493a514b20a743fd9fa
1 /*
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>
38 #include <asm/irq.h>
39 #include <asm/io.h>
41 #include "dm9000.h"
43 /* Board/System/Debug information/definition ---------------- */
45 #define DM9000_PHY 0x40 /* PHY address 0x01 */
47 #define CARDNAME "dm9000"
48 #define PFX CARDNAME ": "
49 #define DRV_VERSION "1.30"
51 #ifdef CONFIG_BLACKFIN
52 #define readsb insb
53 #define readsw insw
54 #define readsl insl
55 #define writesb outsb
56 #define writesw outsw
57 #define writesl outsl
58 #define DEFAULT_TRIGGER IRQF_TRIGGER_HIGH
59 #else
60 #define DEFAULT_TRIGGER (0)
61 #endif
64 * Transmit timeout, default 5 seconds.
66 static int watchdog = 5000;
67 module_param(watchdog, int, 0400);
68 MODULE_PARM_DESC(watchdog, "transmit timeout in milliseconds");
70 /* DM9000 register address locking.
72 * The DM9000 uses an address register to control where data written
73 * to the data register goes. This means that the address register
74 * must be preserved over interrupts or similar calls.
76 * During interrupt and other critical calls, a spinlock is used to
77 * protect the system, but the calls themselves save the address
78 * in the address register in case they are interrupting another
79 * access to the device.
81 * For general accesses a lock is provided so that calls which are
82 * allowed to sleep are serialised so that the address register does
83 * not need to be saved. This lock also serves to serialise access
84 * to the EEPROM and PHY access registers which are shared between
85 * these two devices.
88 /* Structure/enum declaration ------------------------------- */
89 typedef struct board_info {
91 void __iomem *io_addr; /* Register I/O base address */
92 void __iomem *io_data; /* Data I/O address */
93 u16 irq; /* IRQ */
95 u16 tx_pkt_cnt;
96 u16 queue_pkt_len;
97 u16 queue_start_addr;
98 u16 dbug_cnt;
99 u8 io_mode; /* 0:word, 2:byte */
100 u8 phy_addr;
101 unsigned int flags;
102 unsigned int in_suspend :1;
104 int debug_level;
106 void (*inblk)(void __iomem *port, void *data, int length);
107 void (*outblk)(void __iomem *port, void *data, int length);
108 void (*dumpblk)(void __iomem *port, int length);
110 struct device *dev; /* parent device */
112 struct resource *addr_res; /* resources found */
113 struct resource *data_res;
114 struct resource *addr_req; /* resources requested */
115 struct resource *data_req;
116 struct resource *irq_res;
118 struct mutex addr_lock; /* phy and eeprom access lock */
120 spinlock_t lock;
122 struct mii_if_info mii;
123 u32 msg_enable;
124 } board_info_t;
126 /* debug code */
128 #define dm9000_dbg(db, lev, msg...) do { \
129 if ((lev) < CONFIG_DM9000_DEBUGLEVEL && \
130 (lev) < db->debug_level) { \
131 dev_dbg(db->dev, msg); \
133 } while (0)
135 static inline board_info_t *to_dm9000_board(struct net_device *dev)
137 return dev->priv;
140 /* function declaration ------------------------------------- */
141 static int dm9000_probe(struct platform_device *);
142 static int dm9000_open(struct net_device *);
143 static int dm9000_start_xmit(struct sk_buff *, struct net_device *);
144 static int dm9000_stop(struct net_device *);
145 static int dm9000_ioctl(struct net_device *dev, struct ifreq *req, int cmd);
147 static void dm9000_init_dm9000(struct net_device *);
149 static irqreturn_t dm9000_interrupt(int, void *);
151 static int dm9000_phy_read(struct net_device *dev, int phyaddr_unsused, int reg);
152 static void dm9000_phy_write(struct net_device *dev, int phyaddr_unused, int reg,
153 int value);
155 static void dm9000_read_eeprom(board_info_t *, int addr, u8 *to);
156 static void dm9000_write_eeprom(board_info_t *, int addr, u8 *dp);
157 static void dm9000_rx(struct net_device *);
158 static void dm9000_hash_table(struct net_device *);
160 /* DM9000 network board routine ---------------------------- */
162 static void
163 dm9000_reset(board_info_t * db)
165 dev_dbg(db->dev, "resetting device\n");
167 /* RESET device */
168 writeb(DM9000_NCR, db->io_addr);
169 udelay(200);
170 writeb(NCR_RST, db->io_data);
171 udelay(200);
175 * Read a byte from I/O port
177 static u8
178 ior(board_info_t * db, int reg)
180 writeb(reg, db->io_addr);
181 return readb(db->io_data);
185 * Write a byte to I/O port
188 static void
189 iow(board_info_t * db, int reg, int value)
191 writeb(reg, db->io_addr);
192 writeb(value, db->io_data);
195 /* routines for sending block to chip */
197 static void dm9000_outblk_8bit(void __iomem *reg, void *data, int count)
199 writesb(reg, data, count);
202 static void dm9000_outblk_16bit(void __iomem *reg, void *data, int count)
204 writesw(reg, data, (count+1) >> 1);
207 static void dm9000_outblk_32bit(void __iomem *reg, void *data, int count)
209 writesl(reg, data, (count+3) >> 2);
212 /* input block from chip to memory */
214 static void dm9000_inblk_8bit(void __iomem *reg, void *data, int count)
216 readsb(reg, data, count);
220 static void dm9000_inblk_16bit(void __iomem *reg, void *data, int count)
222 readsw(reg, data, (count+1) >> 1);
225 static void dm9000_inblk_32bit(void __iomem *reg, void *data, int count)
227 readsl(reg, data, (count+3) >> 2);
230 /* dump block from chip to null */
232 static void dm9000_dumpblk_8bit(void __iomem *reg, int count)
234 int i;
235 int tmp;
237 for (i = 0; i < count; i++)
238 tmp = readb(reg);
241 static void dm9000_dumpblk_16bit(void __iomem *reg, int count)
243 int i;
244 int tmp;
246 count = (count + 1) >> 1;
248 for (i = 0; i < count; i++)
249 tmp = readw(reg);
252 static void dm9000_dumpblk_32bit(void __iomem *reg, int count)
254 int i;
255 int tmp;
257 count = (count + 3) >> 2;
259 for (i = 0; i < count; i++)
260 tmp = readl(reg);
263 /* dm9000_set_io
265 * select the specified set of io routines to use with the
266 * device
269 static void dm9000_set_io(struct board_info *db, int byte_width)
271 /* use the size of the data resource to work out what IO
272 * routines we want to use
275 switch (byte_width) {
276 case 1:
277 db->dumpblk = dm9000_dumpblk_8bit;
278 db->outblk = dm9000_outblk_8bit;
279 db->inblk = dm9000_inblk_8bit;
280 break;
283 case 3:
284 dev_dbg(db->dev, ": 3 byte IO, falling back to 16bit\n");
285 case 2:
286 db->dumpblk = dm9000_dumpblk_16bit;
287 db->outblk = dm9000_outblk_16bit;
288 db->inblk = dm9000_inblk_16bit;
289 break;
291 case 4:
292 default:
293 db->dumpblk = dm9000_dumpblk_32bit;
294 db->outblk = dm9000_outblk_32bit;
295 db->inblk = dm9000_inblk_32bit;
296 break;
301 /* Our watchdog timed out. Called by the networking layer */
302 static void dm9000_timeout(struct net_device *dev)
304 board_info_t *db = (board_info_t *) dev->priv;
305 u8 reg_save;
306 unsigned long flags;
308 /* Save previous register address */
309 reg_save = readb(db->io_addr);
310 spin_lock_irqsave(&db->lock,flags);
312 netif_stop_queue(dev);
313 dm9000_reset(db);
314 dm9000_init_dm9000(dev);
315 /* We can accept TX packets again */
316 dev->trans_start = jiffies;
317 netif_wake_queue(dev);
319 /* Restore previous register address */
320 writeb(reg_save, db->io_addr);
321 spin_unlock_irqrestore(&db->lock,flags);
324 #ifdef CONFIG_NET_POLL_CONTROLLER
326 *Used by netconsole
328 static void dm9000_poll_controller(struct net_device *dev)
330 disable_irq(dev->irq);
331 dm9000_interrupt(dev->irq,dev);
332 enable_irq(dev->irq);
334 #endif
336 static int dm9000_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
338 board_info_t *dm = to_dm9000_board(dev);
340 if (!netif_running(dev))
341 return -EINVAL;
343 return generic_mii_ioctl(&dm->mii, if_mii(req), cmd, NULL);
346 /* ethtool ops */
348 static void dm9000_get_drvinfo(struct net_device *dev,
349 struct ethtool_drvinfo *info)
351 board_info_t *dm = to_dm9000_board(dev);
353 strcpy(info->driver, CARDNAME);
354 strcpy(info->version, DRV_VERSION);
355 strcpy(info->bus_info, to_platform_device(dm->dev)->name);
358 static u32 dm9000_get_msglevel(struct net_device *dev)
360 board_info_t *dm = to_dm9000_board(dev);
362 return dm->msg_enable;
365 static void dm9000_set_msglevel(struct net_device *dev, u32 value)
367 board_info_t *dm = to_dm9000_board(dev);
369 dm->msg_enable = value;
372 static int dm9000_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
374 board_info_t *dm = to_dm9000_board(dev);
376 mii_ethtool_gset(&dm->mii, cmd);
377 return 0;
380 static int dm9000_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
382 board_info_t *dm = to_dm9000_board(dev);
384 return mii_ethtool_sset(&dm->mii, cmd);
387 static int dm9000_nway_reset(struct net_device *dev)
389 board_info_t *dm = to_dm9000_board(dev);
390 return mii_nway_restart(&dm->mii);
393 static u32 dm9000_get_link(struct net_device *dev)
395 board_info_t *dm = to_dm9000_board(dev);
396 return mii_link_ok(&dm->mii);
399 #define DM_EEPROM_MAGIC (0x444D394B)
401 static int dm9000_get_eeprom_len(struct net_device *dev)
403 return 128;
406 static int dm9000_get_eeprom(struct net_device *dev,
407 struct ethtool_eeprom *ee, u8 *data)
409 board_info_t *dm = to_dm9000_board(dev);
410 int offset = ee->offset;
411 int len = ee->len;
412 int i;
414 /* EEPROM access is aligned to two bytes */
416 if ((len & 1) != 0 || (offset & 1) != 0)
417 return -EINVAL;
419 if (dm->flags & DM9000_PLATF_NO_EEPROM)
420 return -ENOENT;
422 ee->magic = DM_EEPROM_MAGIC;
424 for (i = 0; i < len; i += 2)
425 dm9000_read_eeprom(dm, (offset + i) / 2, data + i);
427 return 0;
430 static int dm9000_set_eeprom(struct net_device *dev,
431 struct ethtool_eeprom *ee, u8 *data)
433 board_info_t *dm = to_dm9000_board(dev);
434 int offset = ee->offset;
435 int len = ee->len;
436 int i;
438 /* EEPROM access is aligned to two bytes */
440 if ((len & 1) != 0 || (offset & 1) != 0)
441 return -EINVAL;
443 if (dm->flags & DM9000_PLATF_NO_EEPROM)
444 return -ENOENT;
446 if (ee->magic != DM_EEPROM_MAGIC)
447 return -EINVAL;
449 for (i = 0; i < len; i += 2)
450 dm9000_write_eeprom(dm, (offset + i) / 2, data + i);
452 return 0;
455 static const struct ethtool_ops dm9000_ethtool_ops = {
456 .get_drvinfo = dm9000_get_drvinfo,
457 .get_settings = dm9000_get_settings,
458 .set_settings = dm9000_set_settings,
459 .get_msglevel = dm9000_get_msglevel,
460 .set_msglevel = dm9000_set_msglevel,
461 .nway_reset = dm9000_nway_reset,
462 .get_link = dm9000_get_link,
463 .get_eeprom_len = dm9000_get_eeprom_len,
464 .get_eeprom = dm9000_get_eeprom,
465 .set_eeprom = dm9000_set_eeprom,
469 /* dm9000_release_board
471 * release a board, and any mapped resources
474 static void
475 dm9000_release_board(struct platform_device *pdev, struct board_info *db)
477 if (db->data_res == NULL) {
478 if (db->addr_res != NULL)
479 release_mem_region((unsigned long)db->io_addr, 4);
480 return;
483 /* unmap our resources */
485 iounmap(db->io_addr);
486 iounmap(db->io_data);
488 /* release the resources */
490 if (db->data_req != NULL) {
491 release_resource(db->data_req);
492 kfree(db->data_req);
495 if (db->addr_req != NULL) {
496 release_resource(db->addr_req);
497 kfree(db->addr_req);
501 #define res_size(_r) (((_r)->end - (_r)->start) + 1)
504 * Search DM9000 board, allocate space and register it
506 static int
507 dm9000_probe(struct platform_device *pdev)
509 struct dm9000_plat_data *pdata = pdev->dev.platform_data;
510 struct board_info *db; /* Point a board information structure */
511 struct net_device *ndev;
512 const unsigned char *mac_src;
513 unsigned long base;
514 int ret = 0;
515 int iosize;
516 int i;
517 u32 id_val;
519 /* Init network device */
520 ndev = alloc_etherdev(sizeof (struct board_info));
521 if (!ndev) {
522 dev_err(&pdev->dev, "could not allocate device.\n");
523 return -ENOMEM;
526 SET_NETDEV_DEV(ndev, &pdev->dev);
528 dev_dbg(&pdev->dev, "dm9000_probe()");
530 /* setup board info structure */
531 db = (struct board_info *) ndev->priv;
532 memset(db, 0, sizeof (*db));
534 db->dev = &pdev->dev;
536 spin_lock_init(&db->lock);
537 mutex_init(&db->addr_lock);
539 if (pdev->num_resources < 2) {
540 ret = -ENODEV;
541 goto out;
542 } else if (pdev->num_resources == 2) {
543 base = pdev->resource[0].start;
545 if (!request_mem_region(base, 4, ndev->name)) {
546 ret = -EBUSY;
547 goto out;
550 ndev->base_addr = base;
551 ndev->irq = pdev->resource[1].start;
552 db->io_addr = (void __iomem *)base;
553 db->io_data = (void __iomem *)(base + 4);
555 /* ensure at least we have a default set of IO routines */
556 dm9000_set_io(db, 2);
558 } else {
559 db->addr_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
560 db->data_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
561 db->irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
563 if (db->addr_res == NULL || db->data_res == NULL ||
564 db->irq_res == NULL) {
565 dev_err(db->dev, "insufficient resources\n");
566 ret = -ENOENT;
567 goto out;
570 i = res_size(db->addr_res);
571 db->addr_req = request_mem_region(db->addr_res->start, i,
572 pdev->name);
574 if (db->addr_req == NULL) {
575 dev_err(db->dev, "cannot claim address reg area\n");
576 ret = -EIO;
577 goto out;
580 db->io_addr = ioremap(db->addr_res->start, i);
582 if (db->io_addr == NULL) {
583 dev_err(db->dev, "failed to ioremap address reg\n");
584 ret = -EINVAL;
585 goto out;
588 iosize = res_size(db->data_res);
589 db->data_req = request_mem_region(db->data_res->start, iosize,
590 pdev->name);
592 if (db->data_req == NULL) {
593 dev_err(db->dev, "cannot claim data reg area\n");
594 ret = -EIO;
595 goto out;
598 db->io_data = ioremap(db->data_res->start, iosize);
600 if (db->io_data == NULL) {
601 dev_err(db->dev,"failed to ioremap data reg\n");
602 ret = -EINVAL;
603 goto out;
606 /* fill in parameters for net-dev structure */
608 ndev->base_addr = (unsigned long)db->io_addr;
609 ndev->irq = db->irq_res->start;
611 /* ensure at least we have a default set of IO routines */
612 dm9000_set_io(db, iosize);
615 /* check to see if anything is being over-ridden */
616 if (pdata != NULL) {
617 /* check to see if the driver wants to over-ride the
618 * default IO width */
620 if (pdata->flags & DM9000_PLATF_8BITONLY)
621 dm9000_set_io(db, 1);
623 if (pdata->flags & DM9000_PLATF_16BITONLY)
624 dm9000_set_io(db, 2);
626 if (pdata->flags & DM9000_PLATF_32BITONLY)
627 dm9000_set_io(db, 4);
629 /* check to see if there are any IO routine
630 * over-rides */
632 if (pdata->inblk != NULL)
633 db->inblk = pdata->inblk;
635 if (pdata->outblk != NULL)
636 db->outblk = pdata->outblk;
638 if (pdata->dumpblk != NULL)
639 db->dumpblk = pdata->dumpblk;
641 db->flags = pdata->flags;
644 dm9000_reset(db);
646 /* try two times, DM9000 sometimes gets the first read wrong */
647 for (i = 0; i < 8; i++) {
648 id_val = ior(db, DM9000_VIDL);
649 id_val |= (u32)ior(db, DM9000_VIDH) << 8;
650 id_val |= (u32)ior(db, DM9000_PIDL) << 16;
651 id_val |= (u32)ior(db, DM9000_PIDH) << 24;
653 if (id_val == DM9000_ID)
654 break;
655 dev_err(db->dev, "read wrong id 0x%08x\n", id_val);
658 if (id_val != DM9000_ID) {
659 dev_err(db->dev, "wrong id: 0x%08x\n", id_val);
660 ret = -ENODEV;
661 goto out;
664 /* from this point we assume that we have found a DM9000 */
666 /* driver system function */
667 ether_setup(ndev);
669 ndev->open = &dm9000_open;
670 ndev->hard_start_xmit = &dm9000_start_xmit;
671 ndev->tx_timeout = &dm9000_timeout;
672 ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
673 ndev->stop = &dm9000_stop;
674 ndev->set_multicast_list = &dm9000_hash_table;
675 ndev->ethtool_ops = &dm9000_ethtool_ops;
676 ndev->do_ioctl = &dm9000_ioctl;
678 #ifdef CONFIG_NET_POLL_CONTROLLER
679 ndev->poll_controller = &dm9000_poll_controller;
680 #endif
682 db->msg_enable = NETIF_MSG_LINK;
683 db->mii.phy_id_mask = 0x1f;
684 db->mii.reg_num_mask = 0x1f;
685 db->mii.force_media = 0;
686 db->mii.full_duplex = 0;
687 db->mii.dev = ndev;
688 db->mii.mdio_read = dm9000_phy_read;
689 db->mii.mdio_write = dm9000_phy_write;
691 mac_src = "eeprom";
693 /* try reading the node address from the attached EEPROM */
694 for (i = 0; i < 6; i += 2)
695 dm9000_read_eeprom(db, i / 2, ndev->dev_addr+i);
697 if (!is_valid_ether_addr(ndev->dev_addr)) {
698 /* try reading from mac */
700 mac_src = "chip";
701 for (i = 0; i < 6; i++)
702 ndev->dev_addr[i] = ior(db, i+DM9000_PAR);
705 if (!is_valid_ether_addr(ndev->dev_addr))
706 dev_warn(db->dev, "%s: Invalid ethernet MAC address. Please "
707 "set using ifconfig\n", ndev->name);
709 platform_set_drvdata(pdev, ndev);
710 ret = register_netdev(ndev);
712 if (ret == 0) {
713 DECLARE_MAC_BUF(mac);
714 printk("%s: dm9000 at %p,%p IRQ %d MAC: %s (%s)\n",
715 ndev->name, db->io_addr, db->io_data, ndev->irq,
716 print_mac(mac, ndev->dev_addr), mac_src);
718 return 0;
720 out:
721 dev_err(db->dev, "not found (%d).\n", ret);
723 dm9000_release_board(pdev, db);
724 free_netdev(ndev);
726 return ret;
730 * Open the interface.
731 * The interface is opened whenever "ifconfig" actives it.
733 static int
734 dm9000_open(struct net_device *dev)
736 board_info_t *db = (board_info_t *) dev->priv;
737 unsigned long irqflags = db->irq_res->flags & IRQF_TRIGGER_MASK;
739 if (netif_msg_ifup(db))
740 dev_dbg(db->dev, "enabling %s\n", dev->name);
742 /* If there is no IRQ type specified, default to something that
743 * may work, and tell the user that this is a problem */
745 if (irqflags == IRQF_TRIGGER_NONE) {
746 dev_warn(db->dev, "WARNING: no IRQ resource flags set.\n");
747 irqflags = DEFAULT_TRIGGER;
750 irqflags |= IRQF_SHARED;
752 if (request_irq(dev->irq, &dm9000_interrupt, irqflags, dev->name, dev))
753 return -EAGAIN;
755 /* Initialize DM9000 board */
756 dm9000_reset(db);
757 dm9000_init_dm9000(dev);
759 /* Init driver variable */
760 db->dbug_cnt = 0;
762 mii_check_media(&db->mii, netif_msg_link(db), 1);
763 netif_start_queue(dev);
765 return 0;
769 * Initilize dm9000 board
771 static void
772 dm9000_init_dm9000(struct net_device *dev)
774 board_info_t *db = (board_info_t *) dev->priv;
776 dm9000_dbg(db, 1, "entering %s\n", __func__);
778 /* I/O mode */
779 db->io_mode = ior(db, DM9000_ISR) >> 6; /* ISR bit7:6 keeps I/O mode */
781 /* GPIO0 on pre-activate PHY */
782 iow(db, DM9000_GPR, 0); /* REG_1F bit0 activate phyxcer */
783 iow(db, DM9000_GPCR, GPCR_GEP_CNTL); /* Let GPIO0 output */
784 iow(db, DM9000_GPR, 0); /* Enable PHY */
786 if (db->flags & DM9000_PLATF_EXT_PHY)
787 iow(db, DM9000_NCR, NCR_EXT_PHY);
789 /* Program operating register */
790 iow(db, DM9000_TCR, 0); /* TX Polling clear */
791 iow(db, DM9000_BPTR, 0x3f); /* Less 3Kb, 200us */
792 iow(db, DM9000_FCR, 0xff); /* Flow Control */
793 iow(db, DM9000_SMCR, 0); /* Special Mode */
794 /* clear TX status */
795 iow(db, DM9000_NSR, NSR_WAKEST | NSR_TX2END | NSR_TX1END);
796 iow(db, DM9000_ISR, ISR_CLR_STATUS); /* Clear interrupt status */
798 /* Set address filter table */
799 dm9000_hash_table(dev);
801 /* Enable TX/RX interrupt mask */
802 iow(db, DM9000_IMR, IMR_PAR | IMR_PTM | IMR_PRM);
804 /* Init Driver variable */
805 db->tx_pkt_cnt = 0;
806 db->queue_pkt_len = 0;
807 dev->trans_start = 0;
811 * Hardware start transmission.
812 * Send a packet to media from the upper layer.
814 static int
815 dm9000_start_xmit(struct sk_buff *skb, struct net_device *dev)
817 unsigned long flags;
818 board_info_t *db = (board_info_t *) dev->priv;
820 dm9000_dbg(db, 3, "%s:\n", __func__);
822 if (db->tx_pkt_cnt > 1)
823 return 1;
825 spin_lock_irqsave(&db->lock, flags);
827 /* Move data to DM9000 TX RAM */
828 writeb(DM9000_MWCMD, db->io_addr);
830 (db->outblk)(db->io_data, skb->data, skb->len);
831 dev->stats.tx_bytes += skb->len;
833 db->tx_pkt_cnt++;
834 /* TX control: First packet immediately send, second packet queue */
835 if (db->tx_pkt_cnt == 1) {
836 /* Set TX length to DM9000 */
837 iow(db, DM9000_TXPLL, skb->len);
838 iow(db, DM9000_TXPLH, skb->len >> 8);
840 /* Issue TX polling command */
841 iow(db, DM9000_TCR, TCR_TXREQ); /* Cleared after TX complete */
843 dev->trans_start = jiffies; /* save the time stamp */
844 } else {
845 /* Second packet */
846 db->queue_pkt_len = skb->len;
847 netif_stop_queue(dev);
850 spin_unlock_irqrestore(&db->lock, flags);
852 /* free this SKB */
853 dev_kfree_skb(skb);
855 return 0;
858 static void
859 dm9000_shutdown(struct net_device *dev)
861 board_info_t *db = (board_info_t *) dev->priv;
863 /* RESET device */
864 dm9000_phy_write(dev, 0, MII_BMCR, BMCR_RESET); /* PHY RESET */
865 iow(db, DM9000_GPR, 0x01); /* Power-Down PHY */
866 iow(db, DM9000_IMR, IMR_PAR); /* Disable all interrupt */
867 iow(db, DM9000_RCR, 0x00); /* Disable RX */
871 * Stop the interface.
872 * The interface is stopped when it is brought.
874 static int
875 dm9000_stop(struct net_device *ndev)
877 board_info_t *db = (board_info_t *) ndev->priv;
879 if (netif_msg_ifdown(db))
880 dev_dbg(db->dev, "shutting down %s\n", ndev->name);
882 netif_stop_queue(ndev);
883 netif_carrier_off(ndev);
885 /* free interrupt */
886 free_irq(ndev->irq, ndev);
888 dm9000_shutdown(ndev);
890 return 0;
894 * DM9000 interrupt handler
895 * receive the packet to upper layer, free the transmitted packet
898 static void
899 dm9000_tx_done(struct net_device *dev, board_info_t * db)
901 int tx_status = ior(db, DM9000_NSR); /* Got TX status */
903 if (tx_status & (NSR_TX2END | NSR_TX1END)) {
904 /* One packet sent complete */
905 db->tx_pkt_cnt--;
906 dev->stats.tx_packets++;
908 if (netif_msg_tx_done(db))
909 dev_dbg(db->dev, "tx done, NSR %02x\n", tx_status);
911 /* Queue packet check & send */
912 if (db->tx_pkt_cnt > 0) {
913 iow(db, DM9000_TXPLL, db->queue_pkt_len);
914 iow(db, DM9000_TXPLH, db->queue_pkt_len >> 8);
915 iow(db, DM9000_TCR, TCR_TXREQ);
916 dev->trans_start = jiffies;
918 netif_wake_queue(dev);
922 static irqreturn_t
923 dm9000_interrupt(int irq, void *dev_id)
925 struct net_device *dev = dev_id;
926 board_info_t *db = (board_info_t *) dev->priv;
927 int int_status;
928 u8 reg_save;
930 dm9000_dbg(db, 3, "entering %s\n", __func__);
932 /* A real interrupt coming */
934 spin_lock(&db->lock);
936 /* Save previous register address */
937 reg_save = readb(db->io_addr);
939 /* Disable all interrupts */
940 iow(db, DM9000_IMR, IMR_PAR);
942 /* Got DM9000 interrupt status */
943 int_status = ior(db, DM9000_ISR); /* Got ISR */
944 iow(db, DM9000_ISR, int_status); /* Clear ISR status */
946 if (netif_msg_intr(db))
947 dev_dbg(db->dev, "interrupt status %02x\n", int_status);
949 /* Received the coming packet */
950 if (int_status & ISR_PRS)
951 dm9000_rx(dev);
953 /* Trnasmit Interrupt check */
954 if (int_status & ISR_PTS)
955 dm9000_tx_done(dev, db);
957 /* Re-enable interrupt mask */
958 iow(db, DM9000_IMR, IMR_PAR | IMR_PTM | IMR_PRM);
960 /* Restore previous register address */
961 writeb(reg_save, db->io_addr);
963 spin_unlock(&db->lock);
965 return IRQ_HANDLED;
968 struct dm9000_rxhdr {
969 u8 RxPktReady;
970 u8 RxStatus;
971 __le16 RxLen;
972 } __attribute__((__packed__));
975 * Received a packet and pass to upper layer
977 static void
978 dm9000_rx(struct net_device *dev)
980 board_info_t *db = (board_info_t *) dev->priv;
981 struct dm9000_rxhdr rxhdr;
982 struct sk_buff *skb;
983 u8 rxbyte, *rdptr;
984 bool GoodPacket;
985 int RxLen;
987 /* Check packet ready or not */
988 do {
989 ior(db, DM9000_MRCMDX); /* Dummy read */
991 /* Get most updated data */
992 rxbyte = readb(db->io_data);
994 /* Status check: this byte must be 0 or 1 */
995 if (rxbyte > DM9000_PKT_RDY) {
996 dev_warn(db->dev, "status check fail: %d\n", rxbyte);
997 iow(db, DM9000_RCR, 0x00); /* Stop Device */
998 iow(db, DM9000_ISR, IMR_PAR); /* Stop INT request */
999 return;
1002 if (rxbyte != DM9000_PKT_RDY)
1003 return;
1005 /* A packet ready now & Get status/length */
1006 GoodPacket = true;
1007 writeb(DM9000_MRCMD, db->io_addr);
1009 (db->inblk)(db->io_data, &rxhdr, sizeof(rxhdr));
1011 RxLen = le16_to_cpu(rxhdr.RxLen);
1013 if (netif_msg_rx_status(db))
1014 dev_dbg(db->dev, "RX: status %02x, length %04x\n",
1015 rxhdr.RxStatus, RxLen);
1017 /* Packet Status check */
1018 if (RxLen < 0x40) {
1019 GoodPacket = false;
1020 if (netif_msg_rx_err(db))
1021 dev_dbg(db->dev, "RX: Bad Packet (runt)\n");
1024 if (RxLen > DM9000_PKT_MAX) {
1025 dev_dbg(db->dev, "RST: RX Len:%x\n", RxLen);
1028 if (rxhdr.RxStatus & 0xbf) {
1029 GoodPacket = false;
1030 if (rxhdr.RxStatus & 0x01) {
1031 if (netif_msg_rx_err(db))
1032 dev_dbg(db->dev, "fifo error\n");
1033 dev->stats.rx_fifo_errors++;
1035 if (rxhdr.RxStatus & 0x02) {
1036 if (netif_msg_rx_err(db))
1037 dev_dbg(db->dev, "crc error\n");
1038 dev->stats.rx_crc_errors++;
1040 if (rxhdr.RxStatus & 0x80) {
1041 if (netif_msg_rx_err(db))
1042 dev_dbg(db->dev, "length error\n");
1043 dev->stats.rx_length_errors++;
1047 /* Move data from DM9000 */
1048 if (GoodPacket
1049 && ((skb = dev_alloc_skb(RxLen + 4)) != NULL)) {
1050 skb_reserve(skb, 2);
1051 rdptr = (u8 *) skb_put(skb, RxLen - 4);
1053 /* Read received packet from RX SRAM */
1055 (db->inblk)(db->io_data, rdptr, RxLen);
1056 dev->stats.rx_bytes += RxLen;
1058 /* Pass to upper layer */
1059 skb->protocol = eth_type_trans(skb, dev);
1060 netif_rx(skb);
1061 dev->stats.rx_packets++;
1063 } else {
1064 /* need to dump the packet's data */
1066 (db->dumpblk)(db->io_data, RxLen);
1068 } while (rxbyte == DM9000_PKT_RDY);
1071 static unsigned int
1072 dm9000_read_locked(board_info_t *db, int reg)
1074 unsigned long flags;
1075 unsigned int ret;
1077 spin_lock_irqsave(&db->lock, flags);
1078 ret = ior(db, reg);
1079 spin_unlock_irqrestore(&db->lock, flags);
1081 return ret;
1084 static int dm9000_wait_eeprom(board_info_t *db)
1086 unsigned int status;
1087 int timeout = 8; /* wait max 8msec */
1089 /* The DM9000 data sheets say we should be able to
1090 * poll the ERRE bit in EPCR to wait for the EEPROM
1091 * operation. From testing several chips, this bit
1092 * does not seem to work.
1094 * We attempt to use the bit, but fall back to the
1095 * timeout (which is why we do not return an error
1096 * on expiry) to say that the EEPROM operation has
1097 * completed.
1100 while (1) {
1101 status = dm9000_read_locked(db, DM9000_EPCR);
1103 if ((status & EPCR_ERRE) == 0)
1104 break;
1106 if (timeout-- < 0) {
1107 dev_dbg(db->dev, "timeout waiting EEPROM\n");
1108 break;
1112 return 0;
1116 * Read a word data from EEPROM
1118 static void
1119 dm9000_read_eeprom(board_info_t *db, int offset, u8 *to)
1121 unsigned long flags;
1123 if (db->flags & DM9000_PLATF_NO_EEPROM) {
1124 to[0] = 0xff;
1125 to[1] = 0xff;
1126 return;
1129 mutex_lock(&db->addr_lock);
1131 spin_lock_irqsave(&db->lock, flags);
1133 iow(db, DM9000_EPAR, offset);
1134 iow(db, DM9000_EPCR, EPCR_ERPRR);
1136 spin_unlock_irqrestore(&db->lock, flags);
1138 dm9000_wait_eeprom(db);
1140 /* delay for at-least 150uS */
1141 msleep(1);
1143 spin_lock_irqsave(&db->lock, flags);
1145 iow(db, DM9000_EPCR, 0x0);
1147 to[0] = ior(db, DM9000_EPDRL);
1148 to[1] = ior(db, DM9000_EPDRH);
1150 spin_unlock_irqrestore(&db->lock, flags);
1152 mutex_unlock(&db->addr_lock);
1156 * Write a word data to SROM
1158 static void
1159 dm9000_write_eeprom(board_info_t *db, int offset, u8 *data)
1161 unsigned long flags;
1163 if (db->flags & DM9000_PLATF_NO_EEPROM)
1164 return;
1166 mutex_lock(&db->addr_lock);
1168 spin_lock_irqsave(&db->lock, flags);
1169 iow(db, DM9000_EPAR, offset);
1170 iow(db, DM9000_EPDRH, data[1]);
1171 iow(db, DM9000_EPDRL, data[0]);
1172 iow(db, DM9000_EPCR, EPCR_WEP | EPCR_ERPRW);
1173 spin_unlock_irqrestore(&db->lock, flags);
1175 dm9000_wait_eeprom(db);
1177 mdelay(1); /* wait at least 150uS to clear */
1179 spin_lock_irqsave(&db->lock, flags);
1180 iow(db, DM9000_EPCR, 0);
1181 spin_unlock_irqrestore(&db->lock, flags);
1183 mutex_unlock(&db->addr_lock);
1187 * Set DM9000 multicast address
1189 static void
1190 dm9000_hash_table(struct net_device *dev)
1192 board_info_t *db = (board_info_t *) dev->priv;
1193 struct dev_mc_list *mcptr = dev->mc_list;
1194 int mc_cnt = dev->mc_count;
1195 int i, oft;
1196 u32 hash_val;
1197 u16 hash_table[4];
1198 u8 rcr = RCR_DIS_LONG | RCR_DIS_CRC | RCR_RXEN;
1199 unsigned long flags;
1201 dm9000_dbg(db, 1, "entering %s\n", __func__);
1203 spin_lock_irqsave(&db->lock, flags);
1205 for (i = 0, oft = DM9000_PAR; i < 6; i++, oft++)
1206 iow(db, oft, dev->dev_addr[i]);
1208 /* Clear Hash Table */
1209 for (i = 0; i < 4; i++)
1210 hash_table[i] = 0x0;
1212 /* broadcast address */
1213 hash_table[3] = 0x8000;
1215 if (dev->flags & IFF_PROMISC)
1216 rcr |= RCR_PRMSC;
1218 if (dev->flags & IFF_ALLMULTI)
1219 rcr |= RCR_ALL;
1221 /* the multicast address in Hash Table : 64 bits */
1222 for (i = 0; i < mc_cnt; i++, mcptr = mcptr->next) {
1223 hash_val = ether_crc_le(6, mcptr->dmi_addr) & 0x3f;
1224 hash_table[hash_val / 16] |= (u16) 1 << (hash_val % 16);
1227 /* Write the hash table to MAC MD table */
1228 for (i = 0, oft = DM9000_MAR; i < 4; i++) {
1229 iow(db, oft++, hash_table[i]);
1230 iow(db, oft++, hash_table[i] >> 8);
1233 iow(db, DM9000_RCR, rcr);
1234 spin_unlock_irqrestore(&db->lock, flags);
1239 * Sleep, either by using msleep() or if we are suspending, then
1240 * use mdelay() to sleep.
1242 static void dm9000_msleep(board_info_t *db, unsigned int ms)
1244 if (db->in_suspend)
1245 mdelay(ms);
1246 else
1247 msleep(ms);
1251 * Read a word from phyxcer
1253 static int
1254 dm9000_phy_read(struct net_device *dev, int phy_reg_unused, int reg)
1256 board_info_t *db = (board_info_t *) dev->priv;
1257 unsigned long flags;
1258 unsigned int reg_save;
1259 int ret;
1261 mutex_lock(&db->addr_lock);
1263 spin_lock_irqsave(&db->lock,flags);
1265 /* Save previous register address */
1266 reg_save = readb(db->io_addr);
1268 /* Fill the phyxcer register into REG_0C */
1269 iow(db, DM9000_EPAR, DM9000_PHY | reg);
1271 iow(db, DM9000_EPCR, 0xc); /* Issue phyxcer read command */
1273 writeb(reg_save, db->io_addr);
1274 spin_unlock_irqrestore(&db->lock,flags);
1276 dm9000_msleep(db, 1); /* Wait read complete */
1278 spin_lock_irqsave(&db->lock,flags);
1279 reg_save = readb(db->io_addr);
1281 iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer read command */
1283 /* The read data keeps on REG_0D & REG_0E */
1284 ret = (ior(db, DM9000_EPDRH) << 8) | ior(db, DM9000_EPDRL);
1286 /* restore the previous address */
1287 writeb(reg_save, db->io_addr);
1288 spin_unlock_irqrestore(&db->lock,flags);
1290 mutex_unlock(&db->addr_lock);
1291 return ret;
1295 * Write a word to phyxcer
1297 static void
1298 dm9000_phy_write(struct net_device *dev, int phyaddr_unused, int reg, int value)
1300 board_info_t *db = (board_info_t *) dev->priv;
1301 unsigned long flags;
1302 unsigned long reg_save;
1304 mutex_lock(&db->addr_lock);
1306 spin_lock_irqsave(&db->lock,flags);
1308 /* Save previous register address */
1309 reg_save = readb(db->io_addr);
1311 /* Fill the phyxcer register into REG_0C */
1312 iow(db, DM9000_EPAR, DM9000_PHY | reg);
1314 /* Fill the written data into REG_0D & REG_0E */
1315 iow(db, DM9000_EPDRL, value);
1316 iow(db, DM9000_EPDRH, value >> 8);
1318 iow(db, DM9000_EPCR, 0xa); /* Issue phyxcer write command */
1320 writeb(reg_save, db->io_addr);
1321 spin_unlock_irqrestore(&db->lock, flags);
1323 dm9000_msleep(db, 1); /* Wait write complete */
1325 spin_lock_irqsave(&db->lock,flags);
1326 reg_save = readb(db->io_addr);
1328 iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer write command */
1330 /* restore the previous address */
1331 writeb(reg_save, db->io_addr);
1333 spin_unlock_irqrestore(&db->lock, flags);
1334 mutex_unlock(&db->addr_lock);
1337 static int
1338 dm9000_drv_suspend(struct platform_device *dev, pm_message_t state)
1340 struct net_device *ndev = platform_get_drvdata(dev);
1341 board_info_t *db;
1343 if (ndev) {
1344 db = (board_info_t *) ndev->priv;
1345 db->in_suspend = 1;
1347 if (netif_running(ndev)) {
1348 netif_device_detach(ndev);
1349 dm9000_shutdown(ndev);
1352 return 0;
1355 static int
1356 dm9000_drv_resume(struct platform_device *dev)
1358 struct net_device *ndev = platform_get_drvdata(dev);
1359 board_info_t *db = (board_info_t *) ndev->priv;
1361 if (ndev) {
1363 if (netif_running(ndev)) {
1364 dm9000_reset(db);
1365 dm9000_init_dm9000(ndev);
1367 netif_device_attach(ndev);
1370 db->in_suspend = 0;
1372 return 0;
1375 static int
1376 dm9000_drv_remove(struct platform_device *pdev)
1378 struct net_device *ndev = platform_get_drvdata(pdev);
1380 platform_set_drvdata(pdev, NULL);
1382 unregister_netdev(ndev);
1383 dm9000_release_board(pdev, (board_info_t *) ndev->priv);
1384 free_netdev(ndev); /* free device structure */
1386 dev_dbg(&pdev->dev, "released and freed device\n");
1387 return 0;
1390 static struct platform_driver dm9000_driver = {
1391 .driver = {
1392 .name = "dm9000",
1393 .owner = THIS_MODULE,
1395 .probe = dm9000_probe,
1396 .remove = dm9000_drv_remove,
1397 .suspend = dm9000_drv_suspend,
1398 .resume = dm9000_drv_resume,
1401 static int __init
1402 dm9000_init(void)
1404 printk(KERN_INFO "%s Ethernet Driver, V%s\n", CARDNAME, DRV_VERSION);
1406 return platform_driver_register(&dm9000_driver); /* search board and register */
1409 static void __exit
1410 dm9000_cleanup(void)
1412 platform_driver_unregister(&dm9000_driver);
1415 module_init(dm9000_init);
1416 module_exit(dm9000_cleanup);
1418 MODULE_AUTHOR("Sascha Hauer, Ben Dooks");
1419 MODULE_DESCRIPTION("Davicom DM9000 network driver");
1420 MODULE_LICENSE("GPL");