DM9000: Add platform flag for no attached EEPROM
[linux-2.6/kvm.git] / drivers / net / dm9000.c
blobafd2cf509073382dfc11bc17d143577b78846b56
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 *);
146 static void dm9000_init_dm9000(struct net_device *);
148 static irqreturn_t dm9000_interrupt(int, void *);
150 static int dm9000_phy_read(struct net_device *dev, int phyaddr_unsused, int reg);
151 static void dm9000_phy_write(struct net_device *dev, int phyaddr_unused, int reg,
152 int value);
154 static void dm9000_read_eeprom(board_info_t *, int addr, u8 *to);
155 static void dm9000_write_eeprom(board_info_t *, int addr, u8 *dp);
156 static void dm9000_rx(struct net_device *);
157 static void dm9000_hash_table(struct net_device *);
159 /* DM9000 network board routine ---------------------------- */
161 static void
162 dm9000_reset(board_info_t * db)
164 dev_dbg(db->dev, "resetting device\n");
166 /* RESET device */
167 writeb(DM9000_NCR, db->io_addr);
168 udelay(200);
169 writeb(NCR_RST, db->io_data);
170 udelay(200);
174 * Read a byte from I/O port
176 static u8
177 ior(board_info_t * db, int reg)
179 writeb(reg, db->io_addr);
180 return readb(db->io_data);
184 * Write a byte to I/O port
187 static void
188 iow(board_info_t * db, int reg, int value)
190 writeb(reg, db->io_addr);
191 writeb(value, db->io_data);
194 /* routines for sending block to chip */
196 static void dm9000_outblk_8bit(void __iomem *reg, void *data, int count)
198 writesb(reg, data, count);
201 static void dm9000_outblk_16bit(void __iomem *reg, void *data, int count)
203 writesw(reg, data, (count+1) >> 1);
206 static void dm9000_outblk_32bit(void __iomem *reg, void *data, int count)
208 writesl(reg, data, (count+3) >> 2);
211 /* input block from chip to memory */
213 static void dm9000_inblk_8bit(void __iomem *reg, void *data, int count)
215 readsb(reg, data, count);
219 static void dm9000_inblk_16bit(void __iomem *reg, void *data, int count)
221 readsw(reg, data, (count+1) >> 1);
224 static void dm9000_inblk_32bit(void __iomem *reg, void *data, int count)
226 readsl(reg, data, (count+3) >> 2);
229 /* dump block from chip to null */
231 static void dm9000_dumpblk_8bit(void __iomem *reg, int count)
233 int i;
234 int tmp;
236 for (i = 0; i < count; i++)
237 tmp = readb(reg);
240 static void dm9000_dumpblk_16bit(void __iomem *reg, int count)
242 int i;
243 int tmp;
245 count = (count + 1) >> 1;
247 for (i = 0; i < count; i++)
248 tmp = readw(reg);
251 static void dm9000_dumpblk_32bit(void __iomem *reg, int count)
253 int i;
254 int tmp;
256 count = (count + 3) >> 2;
258 for (i = 0; i < count; i++)
259 tmp = readl(reg);
262 /* dm9000_set_io
264 * select the specified set of io routines to use with the
265 * device
268 static void dm9000_set_io(struct board_info *db, int byte_width)
270 /* use the size of the data resource to work out what IO
271 * routines we want to use
274 switch (byte_width) {
275 case 1:
276 db->dumpblk = dm9000_dumpblk_8bit;
277 db->outblk = dm9000_outblk_8bit;
278 db->inblk = dm9000_inblk_8bit;
279 break;
282 case 3:
283 dev_dbg(db->dev, ": 3 byte IO, falling back to 16bit\n");
284 case 2:
285 db->dumpblk = dm9000_dumpblk_16bit;
286 db->outblk = dm9000_outblk_16bit;
287 db->inblk = dm9000_inblk_16bit;
288 break;
290 case 4:
291 default:
292 db->dumpblk = dm9000_dumpblk_32bit;
293 db->outblk = dm9000_outblk_32bit;
294 db->inblk = dm9000_inblk_32bit;
295 break;
300 /* Our watchdog timed out. Called by the networking layer */
301 static void dm9000_timeout(struct net_device *dev)
303 board_info_t *db = (board_info_t *) dev->priv;
304 u8 reg_save;
305 unsigned long flags;
307 /* Save previous register address */
308 reg_save = readb(db->io_addr);
309 spin_lock_irqsave(&db->lock,flags);
311 netif_stop_queue(dev);
312 dm9000_reset(db);
313 dm9000_init_dm9000(dev);
314 /* We can accept TX packets again */
315 dev->trans_start = jiffies;
316 netif_wake_queue(dev);
318 /* Restore previous register address */
319 writeb(reg_save, db->io_addr);
320 spin_unlock_irqrestore(&db->lock,flags);
323 #ifdef CONFIG_NET_POLL_CONTROLLER
325 *Used by netconsole
327 static void dm9000_poll_controller(struct net_device *dev)
329 disable_irq(dev->irq);
330 dm9000_interrupt(dev->irq,dev);
331 enable_irq(dev->irq);
333 #endif
335 /* ethtool ops */
337 static void dm9000_get_drvinfo(struct net_device *dev,
338 struct ethtool_drvinfo *info)
340 board_info_t *dm = to_dm9000_board(dev);
342 strcpy(info->driver, CARDNAME);
343 strcpy(info->version, DRV_VERSION);
344 strcpy(info->bus_info, to_platform_device(dm->dev)->name);
347 static u32 dm9000_get_msglevel(struct net_device *dev)
349 board_info_t *dm = to_dm9000_board(dev);
351 return dm->msg_enable;
354 static void dm9000_set_msglevel(struct net_device *dev, u32 value)
356 board_info_t *dm = to_dm9000_board(dev);
358 dm->msg_enable = value;
361 static int dm9000_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
363 board_info_t *dm = to_dm9000_board(dev);
365 mii_ethtool_gset(&dm->mii, cmd);
366 return 0;
369 static int dm9000_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
371 board_info_t *dm = to_dm9000_board(dev);
373 return mii_ethtool_sset(&dm->mii, cmd);
376 static int dm9000_nway_reset(struct net_device *dev)
378 board_info_t *dm = to_dm9000_board(dev);
379 return mii_nway_restart(&dm->mii);
382 static u32 dm9000_get_link(struct net_device *dev)
384 board_info_t *dm = to_dm9000_board(dev);
385 return mii_link_ok(&dm->mii);
388 #define DM_EEPROM_MAGIC (0x444D394B)
390 static int dm9000_get_eeprom_len(struct net_device *dev)
392 return 128;
395 static int dm9000_get_eeprom(struct net_device *dev,
396 struct ethtool_eeprom *ee, u8 *data)
398 board_info_t *dm = to_dm9000_board(dev);
399 int offset = ee->offset;
400 int len = ee->len;
401 int i;
403 /* EEPROM access is aligned to two bytes */
405 if ((len & 1) != 0 || (offset & 1) != 0)
406 return -EINVAL;
408 if (dm->flags & DM9000_PLATF_NO_EEPROM)
409 return -ENOENT;
411 ee->magic = DM_EEPROM_MAGIC;
413 for (i = 0; i < len; i += 2)
414 dm9000_read_eeprom(dm, (offset + i) / 2, data + i);
416 return 0;
419 static int dm9000_set_eeprom(struct net_device *dev,
420 struct ethtool_eeprom *ee, u8 *data)
422 board_info_t *dm = to_dm9000_board(dev);
423 int offset = ee->offset;
424 int len = ee->len;
425 int i;
427 /* EEPROM access is aligned to two bytes */
429 if ((len & 1) != 0 || (offset & 1) != 0)
430 return -EINVAL;
432 if (dm->flags & DM9000_PLATF_NO_EEPROM)
433 return -ENOENT;
435 if (ee->magic != DM_EEPROM_MAGIC)
436 return -EINVAL;
438 for (i = 0; i < len; i += 2)
439 dm9000_write_eeprom(dm, (offset + i) / 2, data + i);
441 return 0;
444 static const struct ethtool_ops dm9000_ethtool_ops = {
445 .get_drvinfo = dm9000_get_drvinfo,
446 .get_settings = dm9000_get_settings,
447 .set_settings = dm9000_set_settings,
448 .get_msglevel = dm9000_get_msglevel,
449 .set_msglevel = dm9000_set_msglevel,
450 .nway_reset = dm9000_nway_reset,
451 .get_link = dm9000_get_link,
452 .get_eeprom_len = dm9000_get_eeprom_len,
453 .get_eeprom = dm9000_get_eeprom,
454 .set_eeprom = dm9000_set_eeprom,
458 /* dm9000_release_board
460 * release a board, and any mapped resources
463 static void
464 dm9000_release_board(struct platform_device *pdev, struct board_info *db)
466 if (db->data_res == NULL) {
467 if (db->addr_res != NULL)
468 release_mem_region((unsigned long)db->io_addr, 4);
469 return;
472 /* unmap our resources */
474 iounmap(db->io_addr);
475 iounmap(db->io_data);
477 /* release the resources */
479 if (db->data_req != NULL) {
480 release_resource(db->data_req);
481 kfree(db->data_req);
484 if (db->addr_req != NULL) {
485 release_resource(db->addr_req);
486 kfree(db->addr_req);
490 #define res_size(_r) (((_r)->end - (_r)->start) + 1)
493 * Search DM9000 board, allocate space and register it
495 static int
496 dm9000_probe(struct platform_device *pdev)
498 struct dm9000_plat_data *pdata = pdev->dev.platform_data;
499 struct board_info *db; /* Point a board information structure */
500 struct net_device *ndev;
501 unsigned long base;
502 int ret = 0;
503 int iosize;
504 int i;
505 u32 id_val;
507 /* Init network device */
508 ndev = alloc_etherdev(sizeof (struct board_info));
509 if (!ndev) {
510 dev_err(&pdev->dev, "could not allocate device.\n");
511 return -ENOMEM;
514 SET_NETDEV_DEV(ndev, &pdev->dev);
516 dev_dbg(&pdev->dev, "dm9000_probe()");
518 /* setup board info structure */
519 db = (struct board_info *) ndev->priv;
520 memset(db, 0, sizeof (*db));
522 db->dev = &pdev->dev;
524 spin_lock_init(&db->lock);
525 mutex_init(&db->addr_lock);
527 if (pdev->num_resources < 2) {
528 ret = -ENODEV;
529 goto out;
530 } else if (pdev->num_resources == 2) {
531 base = pdev->resource[0].start;
533 if (!request_mem_region(base, 4, ndev->name)) {
534 ret = -EBUSY;
535 goto out;
538 ndev->base_addr = base;
539 ndev->irq = pdev->resource[1].start;
540 db->io_addr = (void __iomem *)base;
541 db->io_data = (void __iomem *)(base + 4);
543 /* ensure at least we have a default set of IO routines */
544 dm9000_set_io(db, 2);
546 } else {
547 db->addr_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
548 db->data_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
549 db->irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
551 if (db->addr_res == NULL || db->data_res == NULL ||
552 db->irq_res == NULL) {
553 dev_err(db->dev, "insufficient resources\n");
554 ret = -ENOENT;
555 goto out;
558 i = res_size(db->addr_res);
559 db->addr_req = request_mem_region(db->addr_res->start, i,
560 pdev->name);
562 if (db->addr_req == NULL) {
563 dev_err(db->dev, "cannot claim address reg area\n");
564 ret = -EIO;
565 goto out;
568 db->io_addr = ioremap(db->addr_res->start, i);
570 if (db->io_addr == NULL) {
571 dev_err(db->dev, "failed to ioremap address reg\n");
572 ret = -EINVAL;
573 goto out;
576 iosize = res_size(db->data_res);
577 db->data_req = request_mem_region(db->data_res->start, iosize,
578 pdev->name);
580 if (db->data_req == NULL) {
581 dev_err(db->dev, "cannot claim data reg area\n");
582 ret = -EIO;
583 goto out;
586 db->io_data = ioremap(db->data_res->start, iosize);
588 if (db->io_data == NULL) {
589 dev_err(db->dev,"failed to ioremap data reg\n");
590 ret = -EINVAL;
591 goto out;
594 /* fill in parameters for net-dev structure */
596 ndev->base_addr = (unsigned long)db->io_addr;
597 ndev->irq = db->irq_res->start;
599 /* ensure at least we have a default set of IO routines */
600 dm9000_set_io(db, iosize);
603 /* check to see if anything is being over-ridden */
604 if (pdata != NULL) {
605 /* check to see if the driver wants to over-ride the
606 * default IO width */
608 if (pdata->flags & DM9000_PLATF_8BITONLY)
609 dm9000_set_io(db, 1);
611 if (pdata->flags & DM9000_PLATF_16BITONLY)
612 dm9000_set_io(db, 2);
614 if (pdata->flags & DM9000_PLATF_32BITONLY)
615 dm9000_set_io(db, 4);
617 /* check to see if there are any IO routine
618 * over-rides */
620 if (pdata->inblk != NULL)
621 db->inblk = pdata->inblk;
623 if (pdata->outblk != NULL)
624 db->outblk = pdata->outblk;
626 if (pdata->dumpblk != NULL)
627 db->dumpblk = pdata->dumpblk;
629 db->flags = pdata->flags;
632 dm9000_reset(db);
634 /* try two times, DM9000 sometimes gets the first read wrong */
635 for (i = 0; i < 2; i++) {
636 id_val = ior(db, DM9000_VIDL);
637 id_val |= (u32)ior(db, DM9000_VIDH) << 8;
638 id_val |= (u32)ior(db, DM9000_PIDL) << 16;
639 id_val |= (u32)ior(db, DM9000_PIDH) << 24;
641 if (id_val == DM9000_ID)
642 break;
643 dev_err(db->dev, "read wrong id 0x%08x\n", id_val);
646 if (id_val != DM9000_ID) {
647 dev_err(db->dev, "wrong id: 0x%08x\n", id_val);
648 ret = -ENODEV;
649 goto out;
652 /* from this point we assume that we have found a DM9000 */
654 /* driver system function */
655 ether_setup(ndev);
657 ndev->open = &dm9000_open;
658 ndev->hard_start_xmit = &dm9000_start_xmit;
659 ndev->tx_timeout = &dm9000_timeout;
660 ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
661 ndev->stop = &dm9000_stop;
662 ndev->set_multicast_list = &dm9000_hash_table;
663 ndev->ethtool_ops = &dm9000_ethtool_ops;
665 #ifdef CONFIG_NET_POLL_CONTROLLER
666 ndev->poll_controller = &dm9000_poll_controller;
667 #endif
669 db->msg_enable = NETIF_MSG_LINK;
670 db->mii.phy_id_mask = 0x1f;
671 db->mii.reg_num_mask = 0x1f;
672 db->mii.force_media = 0;
673 db->mii.full_duplex = 0;
674 db->mii.dev = ndev;
675 db->mii.mdio_read = dm9000_phy_read;
676 db->mii.mdio_write = dm9000_phy_write;
678 /* try reading the node address from the attached EEPROM */
679 for (i = 0; i < 6; i += 2)
680 dm9000_read_eeprom(db, i / 2, ndev->dev_addr+i);
682 if (!is_valid_ether_addr(ndev->dev_addr)) {
683 /* try reading from mac */
685 for (i = 0; i < 6; i++)
686 ndev->dev_addr[i] = ior(db, i+DM9000_PAR);
689 if (!is_valid_ether_addr(ndev->dev_addr))
690 dev_warn(db->dev, "%s: Invalid ethernet MAC address. Please "
691 "set using ifconfig\n", ndev->name);
693 platform_set_drvdata(pdev, ndev);
694 ret = register_netdev(ndev);
696 if (ret == 0) {
697 DECLARE_MAC_BUF(mac);
698 printk("%s: dm9000 at %p,%p IRQ %d MAC: %s\n",
699 ndev->name, db->io_addr, db->io_data, ndev->irq,
700 print_mac(mac, ndev->dev_addr));
702 return 0;
704 out:
705 dev_err(db->dev, "not found (%d).\n", ret);
707 dm9000_release_board(pdev, db);
708 free_netdev(ndev);
710 return ret;
714 * Open the interface.
715 * The interface is opened whenever "ifconfig" actives it.
717 static int
718 dm9000_open(struct net_device *dev)
720 board_info_t *db = (board_info_t *) dev->priv;
721 unsigned long irqflags = db->irq_res->flags & IRQF_TRIGGER_MASK;
723 if (netif_msg_ifup(db))
724 dev_dbg(db->dev, "enabling %s\n", dev->name);
726 /* If there is no IRQ type specified, default to something that
727 * may work, and tell the user that this is a problem */
729 if (irqflags == IRQF_TRIGGER_NONE) {
730 dev_warn(db->dev, "WARNING: no IRQ resource flags set.\n");
731 irqflags = DEFAULT_TRIGGER;
734 irqflags |= IRQF_SHARED;
736 if (request_irq(dev->irq, &dm9000_interrupt, irqflags, dev->name, dev))
737 return -EAGAIN;
739 /* Initialize DM9000 board */
740 dm9000_reset(db);
741 dm9000_init_dm9000(dev);
743 /* Init driver variable */
744 db->dbug_cnt = 0;
746 mii_check_media(&db->mii, netif_msg_link(db), 1);
747 netif_start_queue(dev);
749 return 0;
753 * Initilize dm9000 board
755 static void
756 dm9000_init_dm9000(struct net_device *dev)
758 board_info_t *db = (board_info_t *) dev->priv;
760 dm9000_dbg(db, 1, "entering %s\n", __func__);
762 /* I/O mode */
763 db->io_mode = ior(db, DM9000_ISR) >> 6; /* ISR bit7:6 keeps I/O mode */
765 /* GPIO0 on pre-activate PHY */
766 iow(db, DM9000_GPR, 0); /* REG_1F bit0 activate phyxcer */
767 iow(db, DM9000_GPCR, GPCR_GEP_CNTL); /* Let GPIO0 output */
768 iow(db, DM9000_GPR, 0); /* Enable PHY */
770 if (db->flags & DM9000_PLATF_EXT_PHY)
771 iow(db, DM9000_NCR, NCR_EXT_PHY);
773 /* Program operating register */
774 iow(db, DM9000_TCR, 0); /* TX Polling clear */
775 iow(db, DM9000_BPTR, 0x3f); /* Less 3Kb, 200us */
776 iow(db, DM9000_FCR, 0xff); /* Flow Control */
777 iow(db, DM9000_SMCR, 0); /* Special Mode */
778 /* clear TX status */
779 iow(db, DM9000_NSR, NSR_WAKEST | NSR_TX2END | NSR_TX1END);
780 iow(db, DM9000_ISR, ISR_CLR_STATUS); /* Clear interrupt status */
782 /* Set address filter table */
783 dm9000_hash_table(dev);
785 /* Activate DM9000 */
786 iow(db, DM9000_RCR, RCR_DIS_LONG | RCR_DIS_CRC | RCR_RXEN);
787 /* Enable TX/RX interrupt mask */
788 iow(db, DM9000_IMR, IMR_PAR | IMR_PTM | IMR_PRM);
790 /* Init Driver variable */
791 db->tx_pkt_cnt = 0;
792 db->queue_pkt_len = 0;
793 dev->trans_start = 0;
797 * Hardware start transmission.
798 * Send a packet to media from the upper layer.
800 static int
801 dm9000_start_xmit(struct sk_buff *skb, struct net_device *dev)
803 unsigned long flags;
804 board_info_t *db = (board_info_t *) dev->priv;
806 dm9000_dbg(db, 3, "%s:\n", __func__);
808 if (db->tx_pkt_cnt > 1)
809 return 1;
811 spin_lock_irqsave(&db->lock, flags);
813 /* Move data to DM9000 TX RAM */
814 writeb(DM9000_MWCMD, db->io_addr);
816 (db->outblk)(db->io_data, skb->data, skb->len);
817 dev->stats.tx_bytes += skb->len;
819 db->tx_pkt_cnt++;
820 /* TX control: First packet immediately send, second packet queue */
821 if (db->tx_pkt_cnt == 1) {
822 /* Set TX length to DM9000 */
823 iow(db, DM9000_TXPLL, skb->len);
824 iow(db, DM9000_TXPLH, skb->len >> 8);
826 /* Issue TX polling command */
827 iow(db, DM9000_TCR, TCR_TXREQ); /* Cleared after TX complete */
829 dev->trans_start = jiffies; /* save the time stamp */
830 } else {
831 /* Second packet */
832 db->queue_pkt_len = skb->len;
833 netif_stop_queue(dev);
836 spin_unlock_irqrestore(&db->lock, flags);
838 /* free this SKB */
839 dev_kfree_skb(skb);
841 return 0;
844 static void
845 dm9000_shutdown(struct net_device *dev)
847 board_info_t *db = (board_info_t *) dev->priv;
849 /* RESET device */
850 dm9000_phy_write(dev, 0, MII_BMCR, BMCR_RESET); /* PHY RESET */
851 iow(db, DM9000_GPR, 0x01); /* Power-Down PHY */
852 iow(db, DM9000_IMR, IMR_PAR); /* Disable all interrupt */
853 iow(db, DM9000_RCR, 0x00); /* Disable RX */
857 * Stop the interface.
858 * The interface is stopped when it is brought.
860 static int
861 dm9000_stop(struct net_device *ndev)
863 board_info_t *db = (board_info_t *) ndev->priv;
865 if (netif_msg_ifdown(db))
866 dev_dbg(db->dev, "shutting down %s\n", ndev->name);
868 netif_stop_queue(ndev);
869 netif_carrier_off(ndev);
871 /* free interrupt */
872 free_irq(ndev->irq, ndev);
874 dm9000_shutdown(ndev);
876 return 0;
880 * DM9000 interrupt handler
881 * receive the packet to upper layer, free the transmitted packet
884 static void
885 dm9000_tx_done(struct net_device *dev, board_info_t * db)
887 int tx_status = ior(db, DM9000_NSR); /* Got TX status */
889 if (tx_status & (NSR_TX2END | NSR_TX1END)) {
890 /* One packet sent complete */
891 db->tx_pkt_cnt--;
892 dev->stats.tx_packets++;
894 if (netif_msg_tx_done(db))
895 dev_dbg(db->dev, "tx done, NSR %02x\n", tx_status);
897 /* Queue packet check & send */
898 if (db->tx_pkt_cnt > 0) {
899 iow(db, DM9000_TXPLL, db->queue_pkt_len);
900 iow(db, DM9000_TXPLH, db->queue_pkt_len >> 8);
901 iow(db, DM9000_TCR, TCR_TXREQ);
902 dev->trans_start = jiffies;
904 netif_wake_queue(dev);
908 static irqreturn_t
909 dm9000_interrupt(int irq, void *dev_id)
911 struct net_device *dev = dev_id;
912 board_info_t *db = (board_info_t *) dev->priv;
913 int int_status;
914 u8 reg_save;
916 dm9000_dbg(db, 3, "entering %s\n", __func__);
918 /* A real interrupt coming */
920 spin_lock(&db->lock);
922 /* Save previous register address */
923 reg_save = readb(db->io_addr);
925 /* Disable all interrupts */
926 iow(db, DM9000_IMR, IMR_PAR);
928 /* Got DM9000 interrupt status */
929 int_status = ior(db, DM9000_ISR); /* Got ISR */
930 iow(db, DM9000_ISR, int_status); /* Clear ISR status */
932 if (netif_msg_intr(db))
933 dev_dbg(db->dev, "interrupt status %02x\n", int_status);
935 /* Received the coming packet */
936 if (int_status & ISR_PRS)
937 dm9000_rx(dev);
939 /* Trnasmit Interrupt check */
940 if (int_status & ISR_PTS)
941 dm9000_tx_done(dev, db);
943 /* Re-enable interrupt mask */
944 iow(db, DM9000_IMR, IMR_PAR | IMR_PTM | IMR_PRM);
946 /* Restore previous register address */
947 writeb(reg_save, db->io_addr);
949 spin_unlock(&db->lock);
951 return IRQ_HANDLED;
954 struct dm9000_rxhdr {
955 u8 RxPktReady;
956 u8 RxStatus;
957 u16 RxLen;
958 } __attribute__((__packed__));
961 * Received a packet and pass to upper layer
963 static void
964 dm9000_rx(struct net_device *dev)
966 board_info_t *db = (board_info_t *) dev->priv;
967 struct dm9000_rxhdr rxhdr;
968 struct sk_buff *skb;
969 u8 rxbyte, *rdptr;
970 bool GoodPacket;
971 int RxLen;
973 /* Check packet ready or not */
974 do {
975 ior(db, DM9000_MRCMDX); /* Dummy read */
977 /* Get most updated data */
978 rxbyte = readb(db->io_data);
980 /* Status check: this byte must be 0 or 1 */
981 if (rxbyte > DM9000_PKT_RDY) {
982 dev_warn(db->dev, "status check fail: %d\n", rxbyte);
983 iow(db, DM9000_RCR, 0x00); /* Stop Device */
984 iow(db, DM9000_ISR, IMR_PAR); /* Stop INT request */
985 return;
988 if (rxbyte != DM9000_PKT_RDY)
989 return;
991 /* A packet ready now & Get status/length */
992 GoodPacket = true;
993 writeb(DM9000_MRCMD, db->io_addr);
995 (db->inblk)(db->io_data, &rxhdr, sizeof(rxhdr));
997 RxLen = le16_to_cpu(rxhdr.RxLen);
999 if (netif_msg_rx_status(db))
1000 dev_dbg(db->dev, "RX: status %02x, length %04x\n",
1001 rxhdr.RxStatus, RxLen);
1003 /* Packet Status check */
1004 if (RxLen < 0x40) {
1005 GoodPacket = false;
1006 if (netif_msg_rx_err(db))
1007 dev_dbg(db->dev, "RX: Bad Packet (runt)\n");
1010 if (RxLen > DM9000_PKT_MAX) {
1011 dev_dbg(db->dev, "RST: RX Len:%x\n", RxLen);
1014 if (rxhdr.RxStatus & 0xbf) {
1015 GoodPacket = false;
1016 if (rxhdr.RxStatus & 0x01) {
1017 if (netif_msg_rx_err(db))
1018 dev_dbg(db->dev, "fifo error\n");
1019 dev->stats.rx_fifo_errors++;
1021 if (rxhdr.RxStatus & 0x02) {
1022 if (netif_msg_rx_err(db))
1023 dev_dbg(db->dev, "crc error\n");
1024 dev->stats.rx_crc_errors++;
1026 if (rxhdr.RxStatus & 0x80) {
1027 if (netif_msg_rx_err(db))
1028 dev_dbg(db->dev, "length error\n");
1029 dev->stats.rx_length_errors++;
1033 /* Move data from DM9000 */
1034 if (GoodPacket
1035 && ((skb = dev_alloc_skb(RxLen + 4)) != NULL)) {
1036 skb_reserve(skb, 2);
1037 rdptr = (u8 *) skb_put(skb, RxLen - 4);
1039 /* Read received packet from RX SRAM */
1041 (db->inblk)(db->io_data, rdptr, RxLen);
1042 dev->stats.rx_bytes += RxLen;
1044 /* Pass to upper layer */
1045 skb->protocol = eth_type_trans(skb, dev);
1046 netif_rx(skb);
1047 dev->stats.rx_packets++;
1049 } else {
1050 /* need to dump the packet's data */
1052 (db->dumpblk)(db->io_data, RxLen);
1054 } while (rxbyte == DM9000_PKT_RDY);
1057 static unsigned int
1058 dm9000_read_locked(board_info_t *db, int reg)
1060 unsigned long flags;
1061 unsigned int ret;
1063 spin_lock_irqsave(&db->lock, flags);
1064 ret = ior(db, reg);
1065 spin_unlock_irqrestore(&db->lock, flags);
1067 return ret;
1070 static int dm9000_wait_eeprom(board_info_t *db)
1072 unsigned int status;
1073 int timeout = 8; /* wait max 8msec */
1075 /* The DM9000 data sheets say we should be able to
1076 * poll the ERRE bit in EPCR to wait for the EEPROM
1077 * operation. From testing several chips, this bit
1078 * does not seem to work.
1080 * We attempt to use the bit, but fall back to the
1081 * timeout (which is why we do not return an error
1082 * on expiry) to say that the EEPROM operation has
1083 * completed.
1086 while (1) {
1087 status = dm9000_read_locked(db, DM9000_EPCR);
1089 if ((status & EPCR_ERRE) == 0)
1090 break;
1092 if (timeout-- < 0) {
1093 dev_dbg(db->dev, "timeout waiting EEPROM\n");
1094 break;
1098 return 0;
1102 * Read a word data from EEPROM
1104 static void
1105 dm9000_read_eeprom(board_info_t *db, int offset, u8 *to)
1107 unsigned long flags;
1109 if (db->flags & DM9000_PLATF_NO_EEPROM) {
1110 to[0] = 0xff;
1111 to[1] = 0xff;
1112 return;
1115 mutex_lock(&db->addr_lock);
1117 spin_lock_irqsave(&db->lock, flags);
1119 iow(db, DM9000_EPAR, offset);
1120 iow(db, DM9000_EPCR, EPCR_ERPRR);
1122 spin_unlock_irqrestore(&db->lock, flags);
1124 dm9000_wait_eeprom(db);
1126 /* delay for at-least 150uS */
1127 msleep(1);
1129 spin_lock_irqsave(&db->lock, flags);
1131 iow(db, DM9000_EPCR, 0x0);
1133 to[0] = ior(db, DM9000_EPDRL);
1134 to[1] = ior(db, DM9000_EPDRH);
1136 spin_unlock_irqrestore(&db->lock, flags);
1138 mutex_unlock(&db->addr_lock);
1142 * Write a word data to SROM
1144 static void
1145 dm9000_write_eeprom(board_info_t *db, int offset, u8 *data)
1147 unsigned long flags;
1149 if (db->flags & DM9000_PLATF_NO_EEPROM)
1150 return;
1152 mutex_lock(&db->addr_lock);
1154 spin_lock_irqsave(&db->lock, flags);
1155 iow(db, DM9000_EPAR, offset);
1156 iow(db, DM9000_EPDRH, data[1]);
1157 iow(db, DM9000_EPDRL, data[0]);
1158 iow(db, DM9000_EPCR, EPCR_WEP | EPCR_ERPRW);
1159 spin_unlock_irqrestore(&db->lock, flags);
1161 dm9000_wait_eeprom(db);
1163 mdelay(1); /* wait at least 150uS to clear */
1165 spin_lock_irqsave(&db->lock, flags);
1166 iow(db, DM9000_EPCR, 0);
1167 spin_unlock_irqrestore(&db->lock, flags);
1169 mutex_unlock(&db->addr_lock);
1173 * Set DM9000 multicast address
1175 static void
1176 dm9000_hash_table(struct net_device *dev)
1178 board_info_t *db = (board_info_t *) dev->priv;
1179 struct dev_mc_list *mcptr = dev->mc_list;
1180 int mc_cnt = dev->mc_count;
1181 int i, oft;
1182 u32 hash_val;
1183 u16 hash_table[4];
1184 unsigned long flags;
1186 dm9000_dbg(db, 1, "entering %s\n", __func__);
1188 spin_lock_irqsave(&db->lock, flags);
1190 for (i = 0, oft = DM9000_PAR; i < 6; i++, oft++)
1191 iow(db, oft, dev->dev_addr[i]);
1193 /* Clear Hash Table */
1194 for (i = 0; i < 4; i++)
1195 hash_table[i] = 0x0;
1197 /* broadcast address */
1198 hash_table[3] = 0x8000;
1200 /* the multicast address in Hash Table : 64 bits */
1201 for (i = 0; i < mc_cnt; i++, mcptr = mcptr->next) {
1202 hash_val = ether_crc_le(6, mcptr->dmi_addr) & 0x3f;
1203 hash_table[hash_val / 16] |= (u16) 1 << (hash_val % 16);
1206 /* Write the hash table to MAC MD table */
1207 for (i = 0, oft = DM9000_MAR; i < 4; i++) {
1208 iow(db, oft++, hash_table[i]);
1209 iow(db, oft++, hash_table[i] >> 8);
1212 spin_unlock_irqrestore(&db->lock, flags);
1217 * Sleep, either by using msleep() or if we are suspending, then
1218 * use mdelay() to sleep.
1220 static void dm9000_msleep(board_info_t *db, unsigned int ms)
1222 if (db->in_suspend)
1223 mdelay(ms);
1224 else
1225 msleep(ms);
1229 * Read a word from phyxcer
1231 static int
1232 dm9000_phy_read(struct net_device *dev, int phy_reg_unused, int reg)
1234 board_info_t *db = (board_info_t *) dev->priv;
1235 unsigned long flags;
1236 unsigned int reg_save;
1237 int ret;
1239 mutex_lock(&db->addr_lock);
1241 spin_lock_irqsave(&db->lock,flags);
1243 /* Save previous register address */
1244 reg_save = readb(db->io_addr);
1246 /* Fill the phyxcer register into REG_0C */
1247 iow(db, DM9000_EPAR, DM9000_PHY | reg);
1249 iow(db, DM9000_EPCR, 0xc); /* Issue phyxcer read command */
1251 writeb(reg_save, db->io_addr);
1252 spin_unlock_irqrestore(&db->lock,flags);
1254 dm9000_msleep(db, 1); /* Wait read complete */
1256 spin_lock_irqsave(&db->lock,flags);
1257 reg_save = readb(db->io_addr);
1259 iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer read command */
1261 /* The read data keeps on REG_0D & REG_0E */
1262 ret = (ior(db, DM9000_EPDRH) << 8) | ior(db, DM9000_EPDRL);
1264 /* restore the previous address */
1265 writeb(reg_save, db->io_addr);
1266 spin_unlock_irqrestore(&db->lock,flags);
1268 mutex_unlock(&db->addr_lock);
1269 return ret;
1273 * Write a word to phyxcer
1275 static void
1276 dm9000_phy_write(struct net_device *dev, int phyaddr_unused, int reg, int value)
1278 board_info_t *db = (board_info_t *) dev->priv;
1279 unsigned long flags;
1280 unsigned long reg_save;
1282 mutex_lock(&db->addr_lock);
1284 spin_lock_irqsave(&db->lock,flags);
1286 /* Save previous register address */
1287 reg_save = readb(db->io_addr);
1289 /* Fill the phyxcer register into REG_0C */
1290 iow(db, DM9000_EPAR, DM9000_PHY | reg);
1292 /* Fill the written data into REG_0D & REG_0E */
1293 iow(db, DM9000_EPDRL, value);
1294 iow(db, DM9000_EPDRH, value >> 8);
1296 iow(db, DM9000_EPCR, 0xa); /* Issue phyxcer write command */
1298 writeb(reg_save, db->io_addr);
1299 spin_unlock_irqrestore(&db->lock, flags);
1301 dm9000_msleep(db, 1); /* Wait write complete */
1303 spin_lock_irqsave(&db->lock,flags);
1304 reg_save = readb(db->io_addr);
1306 iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer write command */
1308 /* restore the previous address */
1309 writeb(reg_save, db->io_addr);
1311 spin_unlock_irqrestore(&db->lock, flags);
1312 mutex_unlock(&db->addr_lock);
1315 static int
1316 dm9000_drv_suspend(struct platform_device *dev, pm_message_t state)
1318 struct net_device *ndev = platform_get_drvdata(dev);
1319 board_info_t *db;
1321 if (ndev) {
1322 db = (board_info_t *) ndev->priv;
1323 db->in_suspend = 1;
1325 if (netif_running(ndev)) {
1326 netif_device_detach(ndev);
1327 dm9000_shutdown(ndev);
1330 return 0;
1333 static int
1334 dm9000_drv_resume(struct platform_device *dev)
1336 struct net_device *ndev = platform_get_drvdata(dev);
1337 board_info_t *db = (board_info_t *) ndev->priv;
1339 if (ndev) {
1341 if (netif_running(ndev)) {
1342 dm9000_reset(db);
1343 dm9000_init_dm9000(ndev);
1345 netif_device_attach(ndev);
1348 db->in_suspend = 0;
1350 return 0;
1353 static int
1354 dm9000_drv_remove(struct platform_device *pdev)
1356 struct net_device *ndev = platform_get_drvdata(pdev);
1358 platform_set_drvdata(pdev, NULL);
1360 unregister_netdev(ndev);
1361 dm9000_release_board(pdev, (board_info_t *) ndev->priv);
1362 free_netdev(ndev); /* free device structure */
1364 dev_dbg(&pdev->dev, "released and freed device\n");
1365 return 0;
1368 static struct platform_driver dm9000_driver = {
1369 .driver = {
1370 .name = "dm9000",
1371 .owner = THIS_MODULE,
1373 .probe = dm9000_probe,
1374 .remove = dm9000_drv_remove,
1375 .suspend = dm9000_drv_suspend,
1376 .resume = dm9000_drv_resume,
1379 static int __init
1380 dm9000_init(void)
1382 printk(KERN_INFO "%s Ethernet Driver, V%s\n", CARDNAME, DRV_VERSION);
1384 return platform_driver_register(&dm9000_driver); /* search board and register */
1387 static void __exit
1388 dm9000_cleanup(void)
1390 platform_driver_unregister(&dm9000_driver);
1393 module_init(dm9000_init);
1394 module_exit(dm9000_cleanup);
1396 MODULE_AUTHOR("Sascha Hauer, Ben Dooks");
1397 MODULE_DESCRIPTION("Davicom DM9000 network driver");
1398 MODULE_LICENSE("GPL");