2 * dm9000.c: Version 1.2 03/18/2003
4 * A Davicom DM9000 ISA NIC fast Ethernet driver for Linux.
5 * Copyright (C) 1997 Sten Wang
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * (C)Copyright 1997-1998 DAVICOM Semiconductor,Inc. All Rights Reserved.
19 * V0.11 06/20/2001 REG_0A bit3=1, default enable BP with DA match
20 * 06/22/2001 Support DM9801 progrmming
21 * E3: R25 = ((R24 + NF) & 0x00ff) | 0xf000
22 * E4: R25 = ((R24 + NF) & 0x00ff) | 0xc200
23 * R17 = (R17 & 0xfff0) | NF + 3
24 * E5: R25 = ((R24 + NF - 3) & 0x00ff) | 0xc200
25 * R17 = (R17 & 0xfff0) | NF
27 * v1.00 modify by simon 2001.9.5
28 * change for kernel 2.4.x
30 * v1.1 11/09/2001 fix force mode bug
32 * v1.2 03/18/2003 Weilun Huang <weilun_huang@davicom.com.tw>:
34 * Added tx/rx 32 bit mode.
35 * Cleaned up for kernel merge.
37 * 03/03/2004 Sascha Hauer <s.hauer@pengutronix.de>
40 * 24-Sep-2004 Ben Dooks <ben@simtec.co.uk>
41 * Cleanup of code to remove ifdefs
42 * Allowed platform device data to influence access width
43 * Reformatting areas of code
45 * 17-Mar-2005 Sascha Hauer <s.hauer@pengutronix.de>
46 * * removed 2.4 style module parameters
47 * * removed removed unused stat counter and fixed
49 * * introduced tx_timeout function
52 * 01-Jul-2005 Ben Dooks <ben@simtec.co.uk>
53 * * fixed spinlock call without pointer
54 * * ensure spinlock is initialised
57 #include <linux/module.h>
58 #include <linux/ioport.h>
59 #include <linux/netdevice.h>
60 #include <linux/etherdevice.h>
61 #include <linux/init.h>
62 #include <linux/skbuff.h>
63 #include <linux/spinlock.h>
64 #include <linux/crc32.h>
65 #include <linux/mii.h>
66 #include <linux/ethtool.h>
67 #include <linux/dm9000.h>
68 #include <linux/delay.h>
69 #include <linux/platform_device.h>
70 #include <linux/irq.h>
72 #include <asm/delay.h>
78 /* Board/System/Debug information/definition ---------------- */
80 #define DM9000_PHY 0x40 /* PHY address 0x01 */
82 #define CARDNAME "dm9000"
83 #define PFX CARDNAME ": "
84 #define DRV_VERSION "1.30"
86 #ifdef CONFIG_BLACKFIN
93 #define DEFAULT_TRIGGER IRQF_TRIGGER_HIGH
95 #define DEFAULT_TRIGGER (0)
99 * Transmit timeout, default 5 seconds.
101 static int watchdog
= 5000;
102 module_param(watchdog
, int, 0400);
103 MODULE_PARM_DESC(watchdog
, "transmit timeout in milliseconds");
105 /* Structure/enum declaration ------------------------------- */
106 typedef struct board_info
{
108 void __iomem
*io_addr
; /* Register I/O base address */
109 void __iomem
*io_data
; /* Data I/O address */
114 u16 queue_start_addr
;
116 u8 io_mode
; /* 0:word, 2:byte */
119 unsigned int in_suspend
:1;
123 void (*inblk
)(void __iomem
*port
, void *data
, int length
);
124 void (*outblk
)(void __iomem
*port
, void *data
, int length
);
125 void (*dumpblk
)(void __iomem
*port
, int length
);
127 struct device
*dev
; /* parent device */
129 struct resource
*addr_res
; /* resources found */
130 struct resource
*data_res
;
131 struct resource
*addr_req
; /* resources requested */
132 struct resource
*data_req
;
133 struct resource
*irq_res
;
135 unsigned char srom
[128];
138 struct mii_if_info mii
;
144 #define dm9000_dbg(db, lev, msg...) do { \
145 if ((lev) < CONFIG_DM9000_DEBUGLEVEL && \
146 (lev) < db->debug_level) { \
147 dev_dbg(db->dev, msg); \
151 static inline board_info_t
*to_dm9000_board(struct net_device
*dev
)
156 /* function declaration ------------------------------------- */
157 static int dm9000_probe(struct platform_device
*);
158 static int dm9000_open(struct net_device
*);
159 static int dm9000_start_xmit(struct sk_buff
*, struct net_device
*);
160 static int dm9000_stop(struct net_device
*);
162 static void dm9000_init_dm9000(struct net_device
*);
164 static irqreturn_t
dm9000_interrupt(int, void *);
166 static int dm9000_phy_read(struct net_device
*dev
, int phyaddr_unsused
, int reg
);
167 static void dm9000_phy_write(struct net_device
*dev
, int phyaddr_unused
, int reg
,
169 static u16
read_srom_word(board_info_t
*, int);
170 static void dm9000_rx(struct net_device
*);
171 static void dm9000_hash_table(struct net_device
*);
173 //#define DM9000_PROGRAM_EEPROM
174 #ifdef DM9000_PROGRAM_EEPROM
175 static void program_eeprom(board_info_t
* db
);
177 /* DM9000 network board routine ---------------------------- */
180 dm9000_reset(board_info_t
* db
)
182 dev_dbg(db
->dev
, "resetting device\n");
185 writeb(DM9000_NCR
, db
->io_addr
);
187 writeb(NCR_RST
, db
->io_data
);
192 * Read a byte from I/O port
195 ior(board_info_t
* db
, int reg
)
197 writeb(reg
, db
->io_addr
);
198 return readb(db
->io_data
);
202 * Write a byte to I/O port
206 iow(board_info_t
* db
, int reg
, int value
)
208 writeb(reg
, db
->io_addr
);
209 writeb(value
, db
->io_data
);
212 /* routines for sending block to chip */
214 static void dm9000_outblk_8bit(void __iomem
*reg
, void *data
, int count
)
216 writesb(reg
, data
, count
);
219 static void dm9000_outblk_16bit(void __iomem
*reg
, void *data
, int count
)
221 writesw(reg
, data
, (count
+1) >> 1);
224 static void dm9000_outblk_32bit(void __iomem
*reg
, void *data
, int count
)
226 writesl(reg
, data
, (count
+3) >> 2);
229 /* input block from chip to memory */
231 static void dm9000_inblk_8bit(void __iomem
*reg
, void *data
, int count
)
233 readsb(reg
, data
, count
);
237 static void dm9000_inblk_16bit(void __iomem
*reg
, void *data
, int count
)
239 readsw(reg
, data
, (count
+1) >> 1);
242 static void dm9000_inblk_32bit(void __iomem
*reg
, void *data
, int count
)
244 readsl(reg
, data
, (count
+3) >> 2);
247 /* dump block from chip to null */
249 static void dm9000_dumpblk_8bit(void __iomem
*reg
, int count
)
254 for (i
= 0; i
< count
; i
++)
258 static void dm9000_dumpblk_16bit(void __iomem
*reg
, int count
)
263 count
= (count
+ 1) >> 1;
265 for (i
= 0; i
< count
; i
++)
269 static void dm9000_dumpblk_32bit(void __iomem
*reg
, int count
)
274 count
= (count
+ 3) >> 2;
276 for (i
= 0; i
< count
; i
++)
282 * select the specified set of io routines to use with the
286 static void dm9000_set_io(struct board_info
*db
, int byte_width
)
288 /* use the size of the data resource to work out what IO
289 * routines we want to use
292 switch (byte_width
) {
294 db
->dumpblk
= dm9000_dumpblk_8bit
;
295 db
->outblk
= dm9000_outblk_8bit
;
296 db
->inblk
= dm9000_inblk_8bit
;
301 dev_dbg(db
->dev
, ": 3 byte IO, falling back to 16bit\n");
303 db
->dumpblk
= dm9000_dumpblk_16bit
;
304 db
->outblk
= dm9000_outblk_16bit
;
305 db
->inblk
= dm9000_inblk_16bit
;
310 db
->dumpblk
= dm9000_dumpblk_32bit
;
311 db
->outblk
= dm9000_outblk_32bit
;
312 db
->inblk
= dm9000_inblk_32bit
;
318 /* Our watchdog timed out. Called by the networking layer */
319 static void dm9000_timeout(struct net_device
*dev
)
321 board_info_t
*db
= (board_info_t
*) dev
->priv
;
325 /* Save previous register address */
326 reg_save
= readb(db
->io_addr
);
327 spin_lock_irqsave(&db
->lock
,flags
);
329 netif_stop_queue(dev
);
331 dm9000_init_dm9000(dev
);
332 /* We can accept TX packets again */
333 dev
->trans_start
= jiffies
;
334 netif_wake_queue(dev
);
336 /* Restore previous register address */
337 writeb(reg_save
, db
->io_addr
);
338 spin_unlock_irqrestore(&db
->lock
,flags
);
341 #ifdef CONFIG_NET_POLL_CONTROLLER
345 static void dm9000_poll_controller(struct net_device
*dev
)
347 disable_irq(dev
->irq
);
348 dm9000_interrupt(dev
->irq
,dev
);
349 enable_irq(dev
->irq
);
355 static void dm9000_get_drvinfo(struct net_device
*dev
,
356 struct ethtool_drvinfo
*info
)
358 board_info_t
*dm
= to_dm9000_board(dev
);
360 strcpy(info
->driver
, CARDNAME
);
361 strcpy(info
->version
, DRV_VERSION
);
362 strcpy(info
->bus_info
, to_platform_device(dm
->dev
)->name
);
365 static int dm9000_get_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
367 board_info_t
*dm
= to_dm9000_board(dev
);
370 spin_lock_irqsave(&dm
->lock
, flags
);
371 mii_ethtool_gset(&dm
->mii
, cmd
);
372 spin_lock_irqsave(&dm
->lock
, flags
);
377 static int dm9000_set_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
379 board_info_t
*dm
= to_dm9000_board(dev
);
383 spin_lock_irqsave(&dm
->lock
, flags
);
384 rc
= mii_ethtool_sset(&dm
->mii
, cmd
);
385 spin_lock_irqsave(&dm
->lock
, flags
);
390 static int dm9000_nway_reset(struct net_device
*dev
)
392 board_info_t
*dm
= to_dm9000_board(dev
);
393 return mii_nway_restart(&dm
->mii
);
396 static u32
dm9000_get_link(struct net_device
*dev
)
398 board_info_t
*dm
= to_dm9000_board(dev
);
399 return mii_link_ok(&dm
->mii
);
402 static const struct ethtool_ops dm9000_ethtool_ops
= {
403 .get_drvinfo
= dm9000_get_drvinfo
,
404 .get_settings
= dm9000_get_settings
,
405 .set_settings
= dm9000_set_settings
,
406 .nway_reset
= dm9000_nway_reset
,
407 .get_link
= dm9000_get_link
,
411 /* dm9000_release_board
413 * release a board, and any mapped resources
417 dm9000_release_board(struct platform_device
*pdev
, struct board_info
*db
)
419 if (db
->data_res
== NULL
) {
420 if (db
->addr_res
!= NULL
)
421 release_mem_region((unsigned long)db
->io_addr
, 4);
425 /* unmap our resources */
427 iounmap(db
->io_addr
);
428 iounmap(db
->io_data
);
430 /* release the resources */
432 if (db
->data_req
!= NULL
) {
433 release_resource(db
->data_req
);
437 if (db
->addr_req
!= NULL
) {
438 release_resource(db
->addr_req
);
443 #define res_size(_r) (((_r)->end - (_r)->start) + 1)
446 * Search DM9000 board, allocate space and register it
449 dm9000_probe(struct platform_device
*pdev
)
451 struct dm9000_plat_data
*pdata
= pdev
->dev
.platform_data
;
452 struct board_info
*db
; /* Point a board information structure */
453 struct net_device
*ndev
;
460 /* Init network device */
461 ndev
= alloc_etherdev(sizeof (struct board_info
));
463 dev_err(&pdev
->dev
, "could not allocate device.\n");
467 SET_NETDEV_DEV(ndev
, &pdev
->dev
);
469 dev_dbg(&pdev
->dev
, "dm9000_probe()");
471 /* setup board info structure */
472 db
= (struct board_info
*) ndev
->priv
;
473 memset(db
, 0, sizeof (*db
));
475 db
->dev
= &pdev
->dev
;
477 spin_lock_init(&db
->lock
);
479 if (pdev
->num_resources
< 2) {
482 } else if (pdev
->num_resources
== 2) {
483 base
= pdev
->resource
[0].start
;
485 if (!request_mem_region(base
, 4, ndev
->name
)) {
490 ndev
->base_addr
= base
;
491 ndev
->irq
= pdev
->resource
[1].start
;
492 db
->io_addr
= (void __iomem
*)base
;
493 db
->io_data
= (void __iomem
*)(base
+ 4);
495 /* ensure at least we have a default set of IO routines */
496 dm9000_set_io(db
, 2);
499 db
->addr_res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
500 db
->data_res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
501 db
->irq_res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
503 if (db
->addr_res
== NULL
|| db
->data_res
== NULL
||
504 db
->irq_res
== NULL
) {
505 dev_err(db
->dev
, "insufficient resources\n");
510 i
= res_size(db
->addr_res
);
511 db
->addr_req
= request_mem_region(db
->addr_res
->start
, i
,
514 if (db
->addr_req
== NULL
) {
515 dev_err(db
->dev
, "cannot claim address reg area\n");
520 db
->io_addr
= ioremap(db
->addr_res
->start
, i
);
522 if (db
->io_addr
== NULL
) {
523 dev_err(db
->dev
, "failed to ioremap address reg\n");
528 iosize
= res_size(db
->data_res
);
529 db
->data_req
= request_mem_region(db
->data_res
->start
, iosize
,
532 if (db
->data_req
== NULL
) {
533 dev_err(db
->dev
, "cannot claim data reg area\n");
538 db
->io_data
= ioremap(db
->data_res
->start
, iosize
);
540 if (db
->io_data
== NULL
) {
541 dev_err(db
->dev
,"failed to ioremap data reg\n");
546 /* fill in parameters for net-dev structure */
548 ndev
->base_addr
= (unsigned long)db
->io_addr
;
549 ndev
->irq
= db
->irq_res
->start
;
551 /* ensure at least we have a default set of IO routines */
552 dm9000_set_io(db
, iosize
);
555 /* check to see if anything is being over-ridden */
557 /* check to see if the driver wants to over-ride the
558 * default IO width */
560 if (pdata
->flags
& DM9000_PLATF_8BITONLY
)
561 dm9000_set_io(db
, 1);
563 if (pdata
->flags
& DM9000_PLATF_16BITONLY
)
564 dm9000_set_io(db
, 2);
566 if (pdata
->flags
& DM9000_PLATF_32BITONLY
)
567 dm9000_set_io(db
, 4);
569 /* check to see if there are any IO routine
572 if (pdata
->inblk
!= NULL
)
573 db
->inblk
= pdata
->inblk
;
575 if (pdata
->outblk
!= NULL
)
576 db
->outblk
= pdata
->outblk
;
578 if (pdata
->dumpblk
!= NULL
)
579 db
->dumpblk
= pdata
->dumpblk
;
581 db
->flags
= pdata
->flags
;
586 /* try two times, DM9000 sometimes gets the first read wrong */
587 for (i
= 0; i
< 2; i
++) {
588 id_val
= ior(db
, DM9000_VIDL
);
589 id_val
|= (u32
)ior(db
, DM9000_VIDH
) << 8;
590 id_val
|= (u32
)ior(db
, DM9000_PIDL
) << 16;
591 id_val
|= (u32
)ior(db
, DM9000_PIDH
) << 24;
593 if (id_val
== DM9000_ID
)
595 dev_err(db
->dev
, "read wrong id 0x%08x\n", id_val
);
598 if (id_val
!= DM9000_ID
) {
599 dev_err(db
->dev
, "wrong id: 0x%08x\n", id_val
);
604 /* from this point we assume that we have found a DM9000 */
606 /* driver system function */
609 ndev
->open
= &dm9000_open
;
610 ndev
->hard_start_xmit
= &dm9000_start_xmit
;
611 ndev
->tx_timeout
= &dm9000_timeout
;
612 ndev
->watchdog_timeo
= msecs_to_jiffies(watchdog
);
613 ndev
->stop
= &dm9000_stop
;
614 ndev
->set_multicast_list
= &dm9000_hash_table
;
615 ndev
->ethtool_ops
= &dm9000_ethtool_ops
;
617 #ifdef CONFIG_NET_POLL_CONTROLLER
618 ndev
->poll_controller
= &dm9000_poll_controller
;
621 #ifdef DM9000_PROGRAM_EEPROM
624 db
->msg_enable
= NETIF_MSG_LINK
;
625 db
->mii
.phy_id_mask
= 0x1f;
626 db
->mii
.reg_num_mask
= 0x1f;
627 db
->mii
.force_media
= 0;
628 db
->mii
.full_duplex
= 0;
630 db
->mii
.mdio_read
= dm9000_phy_read
;
631 db
->mii
.mdio_write
= dm9000_phy_write
;
633 /* Read SROM content */
634 for (i
= 0; i
< 64; i
++)
635 ((u16
*) db
->srom
)[i
] = read_srom_word(db
, i
);
637 /* Set Node Address */
638 for (i
= 0; i
< 6; i
++)
639 ndev
->dev_addr
[i
] = db
->srom
[i
];
641 if (!is_valid_ether_addr(ndev
->dev_addr
)) {
642 /* try reading from mac */
644 for (i
= 0; i
< 6; i
++)
645 ndev
->dev_addr
[i
] = ior(db
, i
+DM9000_PAR
);
648 if (!is_valid_ether_addr(ndev
->dev_addr
))
649 dev_warn(db
->dev
, "%s: Invalid ethernet MAC address. Please "
650 "set using ifconfig\n", ndev
->name
);
652 platform_set_drvdata(pdev
, ndev
);
653 ret
= register_netdev(ndev
);
656 DECLARE_MAC_BUF(mac
);
657 printk("%s: dm9000 at %p,%p IRQ %d MAC: %s\n",
658 ndev
->name
, db
->io_addr
, db
->io_data
, ndev
->irq
,
659 print_mac(mac
, ndev
->dev_addr
));
664 dev_err(db
->dev
, "not found (%d).\n", ret
);
666 dm9000_release_board(pdev
, db
);
673 * Open the interface.
674 * The interface is opened whenever "ifconfig" actives it.
677 dm9000_open(struct net_device
*dev
)
679 board_info_t
*db
= (board_info_t
*) dev
->priv
;
680 unsigned long irqflags
= db
->irq_res
->flags
& IRQF_TRIGGER_MASK
;
682 dev_dbg(db
->dev
, "entering %s\n", __func__
);
684 /* If there is no IRQ type specified, default to something that
685 * may work, and tell the user that this is a problem */
687 if (irqflags
== IRQF_TRIGGER_NONE
) {
688 dev_warn(db
->dev
, "WARNING: no IRQ resource flags set.\n");
689 irqflags
= DEFAULT_TRIGGER
;
692 irqflags
|= IRQF_SHARED
;
694 if (request_irq(dev
->irq
, &dm9000_interrupt
, irqflags
, dev
->name
, dev
))
697 /* Initialize DM9000 board */
699 dm9000_init_dm9000(dev
);
701 /* Init driver variable */
704 mii_check_media(&db
->mii
, netif_msg_link(db
), 1);
705 netif_start_queue(dev
);
711 * Initilize dm9000 board
714 dm9000_init_dm9000(struct net_device
*dev
)
716 board_info_t
*db
= (board_info_t
*) dev
->priv
;
718 dm9000_dbg(db
, 1, "entering %s\n", __func__
);
721 db
->io_mode
= ior(db
, DM9000_ISR
) >> 6; /* ISR bit7:6 keeps I/O mode */
723 /* GPIO0 on pre-activate PHY */
724 iow(db
, DM9000_GPR
, 0); /* REG_1F bit0 activate phyxcer */
725 iow(db
, DM9000_GPCR
, GPCR_GEP_CNTL
); /* Let GPIO0 output */
726 iow(db
, DM9000_GPR
, 0); /* Enable PHY */
728 if (db
->flags
& DM9000_PLATF_EXT_PHY
)
729 iow(db
, DM9000_NCR
, NCR_EXT_PHY
);
731 /* Program operating register */
732 iow(db
, DM9000_TCR
, 0); /* TX Polling clear */
733 iow(db
, DM9000_BPTR
, 0x3f); /* Less 3Kb, 200us */
734 iow(db
, DM9000_FCR
, 0xff); /* Flow Control */
735 iow(db
, DM9000_SMCR
, 0); /* Special Mode */
736 /* clear TX status */
737 iow(db
, DM9000_NSR
, NSR_WAKEST
| NSR_TX2END
| NSR_TX1END
);
738 iow(db
, DM9000_ISR
, ISR_CLR_STATUS
); /* Clear interrupt status */
740 /* Set address filter table */
741 dm9000_hash_table(dev
);
743 /* Activate DM9000 */
744 iow(db
, DM9000_RCR
, RCR_DIS_LONG
| RCR_DIS_CRC
| RCR_RXEN
);
745 /* Enable TX/RX interrupt mask */
746 iow(db
, DM9000_IMR
, IMR_PAR
| IMR_PTM
| IMR_PRM
);
748 /* Init Driver variable */
750 db
->queue_pkt_len
= 0;
751 dev
->trans_start
= 0;
755 * Hardware start transmission.
756 * Send a packet to media from the upper layer.
759 dm9000_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
762 board_info_t
*db
= (board_info_t
*) dev
->priv
;
764 dm9000_dbg(db
, 3, "%s:\n", __func__
);
766 if (db
->tx_pkt_cnt
> 1)
769 spin_lock_irqsave(&db
->lock
, flags
);
771 /* Move data to DM9000 TX RAM */
772 writeb(DM9000_MWCMD
, db
->io_addr
);
774 (db
->outblk
)(db
->io_data
, skb
->data
, skb
->len
);
775 dev
->stats
.tx_bytes
+= skb
->len
;
778 /* TX control: First packet immediately send, second packet queue */
779 if (db
->tx_pkt_cnt
== 1) {
780 /* Set TX length to DM9000 */
781 iow(db
, DM9000_TXPLL
, skb
->len
& 0xff);
782 iow(db
, DM9000_TXPLH
, (skb
->len
>> 8) & 0xff);
784 /* Issue TX polling command */
785 iow(db
, DM9000_TCR
, TCR_TXREQ
); /* Cleared after TX complete */
787 dev
->trans_start
= jiffies
; /* save the time stamp */
790 db
->queue_pkt_len
= skb
->len
;
791 netif_stop_queue(dev
);
794 spin_unlock_irqrestore(&db
->lock
, flags
);
803 dm9000_shutdown(struct net_device
*dev
)
805 board_info_t
*db
= (board_info_t
*) dev
->priv
;
808 dm9000_phy_write(dev
, 0, MII_BMCR
, BMCR_RESET
); /* PHY RESET */
809 iow(db
, DM9000_GPR
, 0x01); /* Power-Down PHY */
810 iow(db
, DM9000_IMR
, IMR_PAR
); /* Disable all interrupt */
811 iow(db
, DM9000_RCR
, 0x00); /* Disable RX */
815 * Stop the interface.
816 * The interface is stopped when it is brought.
819 dm9000_stop(struct net_device
*ndev
)
821 board_info_t
*db
= (board_info_t
*) ndev
->priv
;
823 dm9000_dbg(db
, 1, "entering %s\n", __func__
);
825 netif_stop_queue(ndev
);
826 netif_carrier_off(ndev
);
829 free_irq(ndev
->irq
, ndev
);
831 dm9000_shutdown(ndev
);
837 * DM9000 interrupt handler
838 * receive the packet to upper layer, free the transmitted packet
842 dm9000_tx_done(struct net_device
*dev
, board_info_t
* db
)
844 int tx_status
= ior(db
, DM9000_NSR
); /* Got TX status */
846 if (tx_status
& (NSR_TX2END
| NSR_TX1END
)) {
847 /* One packet sent complete */
849 dev
->stats
.tx_packets
++;
851 /* Queue packet check & send */
852 if (db
->tx_pkt_cnt
> 0) {
853 iow(db
, DM9000_TXPLL
, db
->queue_pkt_len
& 0xff);
854 iow(db
, DM9000_TXPLH
, (db
->queue_pkt_len
>> 8) & 0xff);
855 iow(db
, DM9000_TCR
, TCR_TXREQ
);
856 dev
->trans_start
= jiffies
;
858 netif_wake_queue(dev
);
863 dm9000_interrupt(int irq
, void *dev_id
)
865 struct net_device
*dev
= dev_id
;
866 board_info_t
*db
= (board_info_t
*) dev
->priv
;
870 dm9000_dbg(db
, 3, "entering %s\n", __func__
);
872 /* A real interrupt coming */
874 spin_lock(&db
->lock
);
876 /* Save previous register address */
877 reg_save
= readb(db
->io_addr
);
879 /* Disable all interrupts */
880 iow(db
, DM9000_IMR
, IMR_PAR
);
882 /* Got DM9000 interrupt status */
883 int_status
= ior(db
, DM9000_ISR
); /* Got ISR */
884 iow(db
, DM9000_ISR
, int_status
); /* Clear ISR status */
886 /* Received the coming packet */
887 if (int_status
& ISR_PRS
)
890 /* Trnasmit Interrupt check */
891 if (int_status
& ISR_PTS
)
892 dm9000_tx_done(dev
, db
);
894 /* Re-enable interrupt mask */
895 iow(db
, DM9000_IMR
, IMR_PAR
| IMR_PTM
| IMR_PRM
);
897 /* Restore previous register address */
898 writeb(reg_save
, db
->io_addr
);
900 spin_unlock(&db
->lock
);
905 struct dm9000_rxhdr
{
909 } __attribute__((__packed__
));
912 * Received a packet and pass to upper layer
915 dm9000_rx(struct net_device
*dev
)
917 board_info_t
*db
= (board_info_t
*) dev
->priv
;
918 struct dm9000_rxhdr rxhdr
;
924 /* Check packet ready or not */
926 ior(db
, DM9000_MRCMDX
); /* Dummy read */
928 /* Get most updated data */
929 rxbyte
= readb(db
->io_data
);
931 /* Status check: this byte must be 0 or 1 */
932 if (rxbyte
> DM9000_PKT_RDY
) {
933 dev_warn(db
->dev
, "status check fail: %d\n", rxbyte
);
934 iow(db
, DM9000_RCR
, 0x00); /* Stop Device */
935 iow(db
, DM9000_ISR
, IMR_PAR
); /* Stop INT request */
939 if (rxbyte
!= DM9000_PKT_RDY
)
942 /* A packet ready now & Get status/length */
944 writeb(DM9000_MRCMD
, db
->io_addr
);
946 (db
->inblk
)(db
->io_data
, &rxhdr
, sizeof(rxhdr
));
948 RxLen
= le16_to_cpu(rxhdr
.RxLen
);
950 /* Packet Status check */
953 dev_dbg(db
->dev
, "Bad Packet received (runt)\n");
956 if (RxLen
> DM9000_PKT_MAX
) {
957 dev_dbg(db
->dev
, "RST: RX Len:%x\n", RxLen
);
960 if (rxhdr
.RxStatus
& 0xbf) {
962 if (rxhdr
.RxStatus
& 0x01) {
963 dev_dbg(db
->dev
, "fifo error\n");
964 dev
->stats
.rx_fifo_errors
++;
966 if (rxhdr
.RxStatus
& 0x02) {
967 dev_dbg(db
->dev
, "crc error\n");
968 dev
->stats
.rx_crc_errors
++;
970 if (rxhdr
.RxStatus
& 0x80) {
971 dev_dbg(db
->dev
, "length error\n");
972 dev
->stats
.rx_length_errors
++;
976 /* Move data from DM9000 */
978 && ((skb
= dev_alloc_skb(RxLen
+ 4)) != NULL
)) {
980 rdptr
= (u8
*) skb_put(skb
, RxLen
- 4);
982 /* Read received packet from RX SRAM */
984 (db
->inblk
)(db
->io_data
, rdptr
, RxLen
);
985 dev
->stats
.rx_bytes
+= RxLen
;
987 /* Pass to upper layer */
988 skb
->protocol
= eth_type_trans(skb
, dev
);
990 dev
->stats
.rx_packets
++;
993 /* need to dump the packet's data */
995 (db
->dumpblk
)(db
->io_data
, RxLen
);
997 } while (rxbyte
== DM9000_PKT_RDY
);
1001 * Read a word data from SROM
1004 read_srom_word(board_info_t
* db
, int offset
)
1006 iow(db
, DM9000_EPAR
, offset
);
1007 iow(db
, DM9000_EPCR
, EPCR_ERPRR
);
1008 mdelay(8); /* according to the datasheet 200us should be enough,
1009 but it doesn't work */
1010 iow(db
, DM9000_EPCR
, 0x0);
1011 return (ior(db
, DM9000_EPDRL
) + (ior(db
, DM9000_EPDRH
) << 8));
1014 #ifdef DM9000_PROGRAM_EEPROM
1016 * Write a word data to SROM
1019 write_srom_word(board_info_t
* db
, int offset
, u16 val
)
1021 iow(db
, DM9000_EPAR
, offset
);
1022 iow(db
, DM9000_EPDRH
, ((val
>> 8) & 0xff));
1023 iow(db
, DM9000_EPDRL
, (val
& 0xff));
1024 iow(db
, DM9000_EPCR
, EPCR_WEP
| EPCR_ERPRW
);
1025 mdelay(8); /* same shit */
1026 iow(db
, DM9000_EPCR
, 0);
1030 * Only for development:
1031 * Here we write static data to the eeprom in case
1032 * we don't have valid content on a new board
1035 program_eeprom(board_info_t
* db
)
1037 u16 eeprom
[] = { 0x0c00, 0x007f, 0x1300, /* MAC Address */
1038 0x0000, /* Autoload: accept nothing */
1039 0x0a46, 0x9000, /* Vendor / Product ID */
1040 0x0000, /* pin control */
1042 }; /* Wake-up mode control */
1044 for (i
= 0; i
< 8; i
++)
1045 write_srom_word(db
, i
, eeprom
[i
]);
1051 * Calculate the CRC valude of the Rx packet
1052 * flag = 1 : return the reverse CRC (for the received packet CRC)
1053 * 0 : return the normal CRC (for Hash Table index)
1056 static unsigned long
1057 cal_CRC(unsigned char *Data
, unsigned int Len
, u8 flag
)
1060 u32 crc
= ether_crc_le(Len
, Data
);
1069 * Set DM9000 multicast address
1072 dm9000_hash_table(struct net_device
*dev
)
1074 board_info_t
*db
= (board_info_t
*) dev
->priv
;
1075 struct dev_mc_list
*mcptr
= dev
->mc_list
;
1076 int mc_cnt
= dev
->mc_count
;
1078 u16 i
, oft
, hash_table
[4];
1079 unsigned long flags
;
1081 dm9000_dbg(db
, 1, "entering %s\n", __func__
);
1083 spin_lock_irqsave(&db
->lock
,flags
);
1085 for (i
= 0, oft
= 0x10; i
< 6; i
++, oft
++)
1086 iow(db
, oft
, dev
->dev_addr
[i
]);
1088 /* Clear Hash Table */
1089 for (i
= 0; i
< 4; i
++)
1090 hash_table
[i
] = 0x0;
1092 /* broadcast address */
1093 hash_table
[3] = 0x8000;
1095 /* the multicast address in Hash Table : 64 bits */
1096 for (i
= 0; i
< mc_cnt
; i
++, mcptr
= mcptr
->next
) {
1097 hash_val
= cal_CRC((char *) mcptr
->dmi_addr
, 6, 0) & 0x3f;
1098 hash_table
[hash_val
/ 16] |= (u16
) 1 << (hash_val
% 16);
1101 /* Write the hash table to MAC MD table */
1102 for (i
= 0, oft
= 0x16; i
< 4; i
++) {
1103 iow(db
, oft
++, hash_table
[i
] & 0xff);
1104 iow(db
, oft
++, (hash_table
[i
] >> 8) & 0xff);
1107 spin_unlock_irqrestore(&db
->lock
,flags
);
1112 * Sleep, either by using msleep() or if we are suspending, then
1113 * use mdelay() to sleep.
1115 static void dm9000_msleep(board_info_t
*db
, unsigned int ms
)
1124 * Read a word from phyxcer
1127 dm9000_phy_read(struct net_device
*dev
, int phy_reg_unused
, int reg
)
1129 board_info_t
*db
= (board_info_t
*) dev
->priv
;
1130 unsigned long flags
;
1131 unsigned int reg_save
;
1134 spin_lock_irqsave(&db
->lock
,flags
);
1136 /* Save previous register address */
1137 reg_save
= readb(db
->io_addr
);
1139 /* Fill the phyxcer register into REG_0C */
1140 iow(db
, DM9000_EPAR
, DM9000_PHY
| reg
);
1142 iow(db
, DM9000_EPCR
, 0xc); /* Issue phyxcer read command */
1144 writeb(reg_save
, db
->io_addr
);
1145 spin_unlock_irqrestore(&db
->lock
,flags
);
1147 dm9000_msleep(db
, 1); /* Wait read complete */
1149 spin_lock_irqsave(&db
->lock
,flags
);
1150 reg_save
= readb(db
->io_addr
);
1152 iow(db
, DM9000_EPCR
, 0x0); /* Clear phyxcer read command */
1154 /* The read data keeps on REG_0D & REG_0E */
1155 ret
= (ior(db
, DM9000_EPDRH
) << 8) | ior(db
, DM9000_EPDRL
);
1157 /* restore the previous address */
1158 writeb(reg_save
, db
->io_addr
);
1159 spin_unlock_irqrestore(&db
->lock
,flags
);
1165 * Write a word to phyxcer
1168 dm9000_phy_write(struct net_device
*dev
, int phyaddr_unused
, int reg
, int value
)
1170 board_info_t
*db
= (board_info_t
*) dev
->priv
;
1171 unsigned long flags
;
1172 unsigned long reg_save
;
1174 spin_lock_irqsave(&db
->lock
,flags
);
1176 /* Save previous register address */
1177 reg_save
= readb(db
->io_addr
);
1179 /* Fill the phyxcer register into REG_0C */
1180 iow(db
, DM9000_EPAR
, DM9000_PHY
| reg
);
1182 /* Fill the written data into REG_0D & REG_0E */
1183 iow(db
, DM9000_EPDRL
, (value
& 0xff));
1184 iow(db
, DM9000_EPDRH
, ((value
>> 8) & 0xff));
1186 iow(db
, DM9000_EPCR
, 0xa); /* Issue phyxcer write command */
1188 writeb(reg_save
, db
->io_addr
);
1189 spin_unlock_irqrestore(&db
->lock
,flags
);
1191 dm9000_msleep(db
, 1); /* Wait write complete */
1193 spin_lock_irqsave(&db
->lock
,flags
);
1194 reg_save
= readb(db
->io_addr
);
1196 iow(db
, DM9000_EPCR
, 0x0); /* Clear phyxcer write command */
1198 /* restore the previous address */
1199 writeb(reg_save
, db
->io_addr
);
1201 spin_unlock_irqrestore(&db
->lock
,flags
);
1205 dm9000_drv_suspend(struct platform_device
*dev
, pm_message_t state
)
1207 struct net_device
*ndev
= platform_get_drvdata(dev
);
1211 db
= (board_info_t
*) ndev
->priv
;
1214 if (netif_running(ndev
)) {
1215 netif_device_detach(ndev
);
1216 dm9000_shutdown(ndev
);
1223 dm9000_drv_resume(struct platform_device
*dev
)
1225 struct net_device
*ndev
= platform_get_drvdata(dev
);
1226 board_info_t
*db
= (board_info_t
*) ndev
->priv
;
1230 if (netif_running(ndev
)) {
1232 dm9000_init_dm9000(ndev
);
1234 netif_device_attach(ndev
);
1243 dm9000_drv_remove(struct platform_device
*pdev
)
1245 struct net_device
*ndev
= platform_get_drvdata(pdev
);
1247 platform_set_drvdata(pdev
, NULL
);
1249 unregister_netdev(ndev
);
1250 dm9000_release_board(pdev
, (board_info_t
*) ndev
->priv
);
1251 free_netdev(ndev
); /* free device structure */
1253 dev_dbg(&pdev
->dev
, "released and freed device\n");
1257 static struct platform_driver dm9000_driver
= {
1260 .owner
= THIS_MODULE
,
1262 .probe
= dm9000_probe
,
1263 .remove
= dm9000_drv_remove
,
1264 .suspend
= dm9000_drv_suspend
,
1265 .resume
= dm9000_drv_resume
,
1271 printk(KERN_INFO
"%s Ethernet Driver, V%s\n", CARDNAME
, DRV_VERSION
);
1273 return platform_driver_register(&dm9000_driver
); /* search board and register */
1277 dm9000_cleanup(void)
1279 platform_driver_unregister(&dm9000_driver
);
1282 module_init(dm9000_init
);
1283 module_exit(dm9000_cleanup
);
1285 MODULE_AUTHOR("Sascha Hauer, Ben Dooks");
1286 MODULE_DESCRIPTION("Davicom DM9000 network driver");
1287 MODULE_LICENSE("GPL");