1 /* r6040.c: A RDC R6040 FastEthernet driver for linux. */
3 Re-written 2004 by Sten Wang.
5 Copyright 1994-2000 by Donald Becker.
6 Copyright 1993 United States Government as represented by the
7 Director, National Security Agency. This software may be used and
8 distributed according to the terms of the GNU General Public License,
9 incorporated herein by reference.
11 This driver is for RDC R6040 FastEthernet MAC series.
12 For kernel version after 2.4.22
15 ---------- ------------------------------------------------
16 08-24-2006 Support at linux 2.6.10 above
17 03-24-2006 Support NAPI
18 03-21-2006 By Charies,change spin_lock_irqsave(lp->lock, flags) to
19 spin_lock_irqsave(&lp->lock, flags) in set_multicast_list
20 03-15-2006 Modify the set_multicast_list ,due to when re-plug the ethernet,
21 it will forget the previous setting
22 07-12-2005 Tim, modify the set_multicast_list
23 03-28-2005 Tim, modify some error mac register offset in
24 function set_multicast_list
25 03-27-2005 Tim, Add the internal state machine reset
26 Sten, If multicast address more than 4, enter PROM mode
28 12-22-2004 Sten Init MAC MBCR register=0x012A
32 1. If multicast address more than 4, use the multicast address hash
35 #define DRV_NAME "r6040"
36 #define DRV_VERSION "0.13"
37 #define DRV_RELDATE "24Aug2006"
39 /* PHY CHIP Address */
40 #define PHY1_ADDR 1 /* For MAC1 */
41 #define PHY2_ADDR 2 /* For MAC2 */
42 #define PHY_MODE 0x3100 /* PHY CHIP Register 0 */
43 #define PHY_CAP 0x01E1 /* PHY CHIP Register 4 */
45 /* Time in jiffies before concluding the transmitter is hung. */
46 #define TX_TIMEOUT (6000 * HZ / 1000)
47 #define TIMER_WUT (jiffies + HZ * 1)/* timer wakeup time : 1 second */
50 #define RDC_MAC_ID 0x6040
52 /* RDC MAC I/O Size */
53 #define R6040_IO_SIZE 256
55 /* RDC Chip PCI Command */
56 #define R6040_PCI_CMD 0x0005 /* IO, Master */
62 #define TX_DCNT 0x80 /* TX descriptor count */
63 #define RX_DCNT 0x80 /* RX descriptor count */
64 #define MAX_BUF_SIZE 0x600
65 #define ALLOC_DESC_SIZE ((TX_DCNT+RX_DCNT)*sizeof(struct r6040_descriptor)+0x10)
66 #define MBCR_DEFAULT 0x012A /* MAC Bus Control Register */
68 /* Debug enable or not */
72 #define RDC_DBUG(msg, value) printk("%s %x\n", msg, value);
74 #define RDC_DBUG(msg, value)
78 #include <linux/module.h>
79 #include <linux/version.h>
80 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
81 #include <linux/moduleparam.h>
83 #include <linux/kernel.h>
84 #include <linux/string.h>
85 #include <linux/timer.h>
86 #include <linux/errno.h>
87 #include <linux/ioport.h>
88 #include <linux/slab.h>
89 #include <linux/interrupt.h>
90 #include <linux/pci.h>
91 #include <linux/netdevice.h>
92 #include <linux/etherdevice.h>
93 #include <linux/skbuff.h>
94 #include <linux/init.h>
95 #include <linux/delay.h> /* for udelay() */
96 #include <linux/mii.h>
97 #include <linux/ethtool.h>
98 #include <linux/crc32.h>
99 #include <linux/spinlock.h>
101 #include <asm/processor.h>
102 #include <asm/bitops.h>
105 #include <asm/uaccess.h>
107 MODULE_AUTHOR("Sten Wang <sten.wang@rdc.com.tw>");
108 MODULE_LICENSE("GPL");
109 #ifdef CONFIG_R6040_NAPI
110 MODULE_DESCRIPTION("RDC R6040 NAPI PCI FastEthernet Driver");
112 MODULE_DESCRIPTION("RDC R6040 PCI FastEthernet Driver");
115 #define R6040_INT_MASK 0x0011
117 struct r6040_descriptor
{
118 u16 status
, len
; /* 0-3 */
122 char *vbufp
; /* 10-13 */
123 struct r6040_descriptor
*vndescp
; /* 14-17 */
124 struct sk_buff
*skb_ptr
; /* 18-1B */
125 u32 rev2
; /* 1C-1F */
126 } __attribute__(( aligned(32) ));
128 struct r6040_private
{
129 struct net_device_stats stats
;
131 struct timer_list timer
;
132 struct pci_dev
*pdev
;
134 struct r6040_descriptor
*rx_insert_ptr
;
135 struct r6040_descriptor
*rx_remove_ptr
;
136 struct r6040_descriptor
*tx_insert_ptr
;
137 struct r6040_descriptor
*tx_remove_ptr
;
138 u16 tx_free_desc
, rx_free_desc
, phy_addr
, phy_mode
;
145 struct r6040_chip_info
{
152 #ifdef CONFIG_R6040_NAPI
153 static int NAPI_status
;
156 static int __devinitdata printed_version
;
157 #ifdef CONFIG_R6040_NAPI
158 static char version
[] __devinitdata
=
159 KERN_INFO DRV_NAME
": RDC R6040 NAPI net driver, version "DRV_VERSION
" (" DRV_RELDATE
")\n";
161 static char version
[] __devinitdata
=
162 KERN_INFO DRV_NAME
": RDC R6040 net driver, version "DRV_VERSION
" (" DRV_RELDATE
")\n";
164 static struct r6040_chip_info r6040_chip_info
[] __devinitdata
=
166 { "RDC R6040 Knight", R6040_PCI_CMD
, R6040_IO_SIZE
, 0}
168 static char *parent
= NULL
;
170 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
171 static int NUM_MAC_TABLE
= 2 ;
172 module_param(parent
, charp
, 0444);
174 MODULE_PARM(parent
, "s");
176 MODULE_PARM_DESC(parent
, "Parent network device name");
178 static int phy_table
[] = { 0x1, 0x2};
179 static u8 adr_table
[2][8] = {{0x00, 0x00, 0x60, 0x00, 0x00, 0x01}, {0x00, 0x00, 0x60, 0x00, 0x00, 0x02}};
181 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 10)
182 module_param_array(adr_table
, int, &NUM_MAC_TABLE
, 0644);
183 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
184 module_param_array(adr_table
, int, NUM_MAC_TABLE
, 0644);
186 MODULE_PARM(adr_table
, "2-4i");
188 MODULE_PARM_DESC(adr_table
, "MAC Address (assigned)");
190 static int r6040_open(struct net_device
*dev
);
191 static int r6040_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
);
192 static irqreturn_t
r6040_interrupt(int irq
, void *dev_id
);
193 static struct net_device_stats
*r6040_get_stats(struct net_device
*dev
);
194 static int r6040_close(struct net_device
*dev
);
195 static void set_multicast_list(struct net_device
*dev
);
196 static struct ethtool_ops netdev_ethtool_ops
;
197 static int netdev_ioctl (struct net_device
*dev
, struct ifreq
*rq
, int cmd
);
198 static void r6040_down(struct net_device
*dev
);
199 static void r6040_up(struct net_device
*dev
);
200 static void r6040_tx_timeout (struct net_device
*dev
);
201 static void r6040_timer(unsigned long);
203 static int phy_mode_chk(struct net_device
*dev
);
204 static int phy_read(int ioaddr
, int phy_adr
, int reg_idx
);
205 static void phy_write(int ioaddr
, int phy_adr
, int reg_idx
, int dat
);
206 static void rx_buf_alloc(struct r6040_private
*lp
,struct net_device
*dev
);
207 #ifdef CONFIG_R6040_NAPI
208 static int r6040_poll(struct net_device
*netdev
, int *budget
);
212 static int __devinit
r6040_init_one (struct pci_dev
*pdev
,
213 const struct pci_device_id
*ent
)
215 struct net_device
*dev
;
216 struct r6040_private
*lp
;
217 int ioaddr
, io_size
, err
;
218 static int card_idx
= -1;
219 int chip_id
= (int)ent
->driver_data
;
221 RDC_DBUG("r6040_init_one()", 0);
223 if (printed_version
++)
226 if ((err
= pci_enable_device (pdev
)))
229 /* this should always be supported */
230 if (pci_set_dma_mask(pdev
, 0xffffffff)) {
231 printk(KERN_ERR DRV_NAME
"32-bit PCI DMA addresses not supported by the card!?\n");
236 io_size
= r6040_chip_info
[chip_id
].io_size
;
237 if (pci_resource_len (pdev
, 0) < io_size
) {
241 ioaddr
= pci_resource_start (pdev
, 0); /* IO map base address */
242 pci_set_master(pdev
);
244 dev
= alloc_etherdev(sizeof(struct r6040_private
));
247 SET_MODULE_OWNER(dev
);
249 if (pci_request_regions(pdev
, DRV_NAME
)) {
250 printk(KERN_ERR DRV_NAME
": Failed to request PCI regions\n");
252 goto err_out_disable
;
255 /* Init system & device */
257 dev
->base_addr
= ioaddr
;
258 dev
->irq
= pdev
->irq
;
260 spin_lock_init(&lp
->lock
);
261 pci_set_drvdata(pdev
, dev
);
263 /* Set MAC address */
265 memcpy(dev
->dev_addr
, (u8
*)&adr_table
[card_idx
][0], 6);
267 /* Link new device into r6040_root_dev */
270 /* Init RDC private data */
272 lp
->phy_addr
= phy_table
[card_idx
];
275 /* The RDC-specific entries in the device structure. */
276 dev
->open
= &r6040_open
;
277 dev
->hard_start_xmit
= &r6040_start_xmit
;
278 dev
->stop
= &r6040_close
;
279 dev
->get_stats
= &r6040_get_stats
;
280 dev
->set_multicast_list
= &set_multicast_list
;
281 dev
->do_ioctl
= &netdev_ioctl
;
282 dev
->ethtool_ops
= &netdev_ethtool_ops
;
283 dev
->tx_timeout
= &r6040_tx_timeout
;
284 dev
->watchdog_timeo
= TX_TIMEOUT
;
285 #ifdef CONFIG_R6040_NAPI
286 dev
->poll
= &r6040_poll
;
290 /* Register net device. After this dev->name assign */
291 if ((err
= register_netdev(dev
))) {
292 printk(KERN_ERR DRV_NAME
": Failed to register net device\n");
296 netif_carrier_on(dev
);
300 pci_release_regions(pdev
);
302 pci_disable_device(pdev
);
303 pci_set_drvdata(pdev
, NULL
);
309 static void __devexit
r6040_remove_one (struct pci_dev
*pdev
)
311 struct net_device
*dev
= pci_get_drvdata(pdev
);
313 unregister_netdev(dev
);
314 pci_release_regions(pdev
);
316 pci_disable_device(pdev
);
317 pci_set_drvdata(pdev
, NULL
);
321 r6040_open(struct net_device
*dev
)
323 struct r6040_private
*lp
= dev
->priv
;
326 RDC_DBUG("r6040_open()", 0);
328 /* Request IRQ and Register interrupt handler */
329 i
= request_irq(dev
->irq
, &r6040_interrupt
, SA_SHIRQ
, dev
->name
, dev
);
332 /* Allocate Descriptor memory */
333 lp
->desc_pool
= pci_alloc_consistent(lp
->pdev
, ALLOC_DESC_SIZE
, &lp
->desc_dma
);
334 if (!lp
->desc_pool
) return -ENOMEM
;
338 netif_start_queue(dev
);
340 if (lp
->switch_sig
!= 0x0243)
342 /* set and active a timer process */
343 init_timer(&lp
->timer
);
344 lp
->timer
.expires
= TIMER_WUT
;
345 lp
->timer
.data
= (unsigned long)dev
;
346 lp
->timer
.function
= &r6040_timer
;
347 add_timer(&lp
->timer
);
353 r6040_tx_timeout (struct net_device
*dev
)
355 struct r6040_private
*lp
= dev
->priv
;
356 //int ioaddr = dev->base_addr;
357 //struct r6040_descriptor *descptr = lp->tx_remove_ptr;
359 RDC_DBUG("r6040_tx_timeout()", 0);
361 /* Transmitter timeout, serious problems. */
362 /* Sten: Nothing need to do so far. */
363 printk(KERN_ERR DRV_NAME
": Big Trobule, transmit timeout/n");
364 lp
->stats
.tx_errors
++;
365 netif_stop_queue(dev
);
367 //printk("<RDC> XMT timedout: CR0 %x, CR40 %x, CR3C %x, CR2C %x, CR30 %x, CR34 %x, CR38 %x\n", inw(ioaddr), inw(ioaddr+0x40), inw(ioaddr+0x3c), inw(ioaddr+0x2c), inw(ioaddr+0x30), inw(ioaddr+0x34), inw(ioaddr+0x38));
369 //printk("<RDC> XMT_TO: %08lx:%04x %04x %08lx %08lx %08lx %08lx\n", descptr, descptr->status, descptr->len, descptr->buf, descptr->skb_ptr, descptr->ndesc, descptr->vndescp);
374 r6040_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
376 struct r6040_private
*lp
= dev
->priv
;
377 struct r6040_descriptor
*descptr
;
378 int ioaddr
= dev
->base_addr
;
381 RDC_DBUG("r6040_start_xmit()", 0);
383 if (skb
== NULL
) /* NULL skb directly return */
385 if (skb
->len
>= MAX_BUF_SIZE
) { /* Packet too long, drop it */
390 /* Critical Section */
391 spin_lock_irqsave(&lp
->lock
, flags
);
393 /* TX resource check */
394 if (!lp
->tx_free_desc
) {
395 spin_unlock_irqrestore(&lp
->lock
, flags
);
396 printk(KERN_ERR DRV_NAME
": NO TX DESC ");
400 /* Statistic Counter */
401 lp
->stats
.tx_packets
++;
402 lp
->stats
.tx_bytes
+= skb
->len
;
404 /* Set TX descriptor & Transmit it */
406 descptr
= lp
->tx_insert_ptr
;
407 if (skb
->len
< 0x3c) descptr
->len
= 0x3c;
408 else descptr
->len
= skb
->len
;
409 descptr
->skb_ptr
= skb
;
410 descptr
->buf
= cpu_to_le32(pci_map_single(lp
->pdev
, skb
->data
, skb
->len
, PCI_DMA_TODEVICE
));
411 descptr
->status
= 0x8000;
412 outw(0x01, ioaddr
+ 0x14);
413 lp
->tx_insert_ptr
= descptr
->vndescp
;
416 printk("Xmit(): %08lx:%04x %04x %08lx %08lx %08lx %08lx\n", descptr
, descptr
->status
, descptr
->len
, descptr
->buf
, descptr
->skb_ptr
, descptr
->ndesc
, descptr
->vndescp
);
419 /* If no tx resource, stop */
420 if (!lp
->tx_free_desc
)
421 netif_stop_queue(dev
);
423 dev
->trans_start
= jiffies
;
424 spin_unlock_irqrestore(&lp
->lock
, flags
);
428 /* The RDC interrupt handler. */
430 r6040_interrupt(int irq
, void *dev_id
)
432 struct net_device
*dev
= dev_id
;
433 struct r6040_private
*lp
;
434 struct r6040_descriptor
*descptr
;
435 struct sk_buff
*skb_ptr
;
438 #ifdef CONFIG_R6040_NAPI
444 RDC_DBUG("r6040_interrupt()", 0);
446 printk (KERN_ERR DRV_NAME
": INT() unknown device.\n");
447 return IRQ_RETVAL(handled
);
450 lp
= (struct r6040_private
*)dev
->priv
;
451 spin_lock_irqsave(&lp
->lock
, flags
);
453 /* Check MAC Interrupt status */
454 ioaddr
= dev
->base_addr
;
455 outw(0x0, ioaddr
+ 0x40); /* Mask Off RDC MAC interrupt */
456 status
= inw(ioaddr
+ 0x3c); /* Read INTR status and clear */
459 #ifdef CONFIG_R6040_NAPI
461 if(netif_rx_schedule_prep(dev
))
463 NAPI_status
= status
;
464 __netif_rx_schedule(dev
);
467 spin_unlock_irqrestore(&lp
->lock
, flags
);
468 return IRQ_RETVAL(handled
);
470 /* TX interrupt request */
473 descptr
= lp
->tx_remove_ptr
;
474 while(lp
->tx_free_desc
< TX_DCNT
) {
475 if (descptr
->status
& 0x8000) break; /* Not complte */
476 skb_ptr
= descptr
->skb_ptr
;
477 pci_unmap_single(lp
->pdev
, descptr
->buf
, skb_ptr
->len
, PCI_DMA_TODEVICE
);
478 dev_kfree_skb_irq(skb_ptr
); /* Free buffer */
479 descptr
->skb_ptr
= 0;
480 descptr
= descptr
->vndescp
; /* To next descriptor */
483 lp
->tx_remove_ptr
= descptr
;
484 if (lp
->tx_free_desc
) netif_wake_queue(dev
);
487 /* RX interrupt request */
490 descptr
= lp
->rx_remove_ptr
;
491 while(lp
->rx_free_desc
) {
492 if (descptr
->status
& 0x8000) break; /* No Rx packet */
493 skb_ptr
= descptr
->skb_ptr
;
494 descptr
->skb_ptr
= 0;
496 skb_put(skb_ptr
, descptr
->len
- 4);
497 pci_unmap_single(lp
->pdev
, descptr
->buf
, MAX_BUF_SIZE
, PCI_DMA_FROMDEVICE
);
498 skb_ptr
->protocol
= eth_type_trans(skb_ptr
, dev
);
499 netif_rx(skb_ptr
); /* Send to upper layer */
500 lp
->stats
.rx_packets
++;
501 lp
->stats
.rx_bytes
+= descptr
->len
;
502 descptr
= descptr
->vndescp
; /* To next descriptor */
505 lp
->rx_remove_ptr
= descptr
;
508 /* Allocate new RX buffer */
509 if (lp
->rx_free_desc
< RX_DCNT
) rx_buf_alloc(lp
,dev
);
511 outw(R6040_INT_MASK
, ioaddr
+ 0x40); /* TX/RX interrupt enable */
512 spin_unlock_irqrestore(&lp
->lock
, flags
);
514 return IRQ_RETVAL(handled
);
518 static struct net_device_stats
*
519 r6040_get_stats(struct net_device
*dev
)
521 struct r6040_private
*lp
= dev
->priv
;
523 RDC_DBUG("r6040_get_stats()", 0);
528 * Set or clear the multicast filter for this adaptor.
531 set_multicast_list(struct net_device
*dev
)
533 struct r6040_private
*lp
= dev
->priv
;
534 struct dev_mc_list
*mcptr
;
535 int ioaddr
= dev
->base_addr
;
539 RDC_DBUG("set_multicast_list()", 0);
542 adrp
= (u16
*) dev
->dev_addr
;
543 outw(adrp
[0], ioaddr
+ 0x68);
544 outw(adrp
[1], ioaddr
+ 0x6A);
545 outw(adrp
[2], ioaddr
+ 0x6C);
549 printk("MAC ADDR: %04x %04x %04x\n", adrp
[0], adrp
[1], adrp
[2]);
552 /* Promiscous Mode */
553 spin_lock_irqsave(&lp
->lock
, flags
);
554 i
= inw(ioaddr
) & ~0x0120; /* Clear AMCP & PROM */
555 if (dev
->flags
& IFF_PROMISC
)
560 if (dev
->mc_count
> 4) i
|= 0x0020; /* Too many multicast address */
562 spin_unlock_irqrestore(&lp
->lock
, flags
);
564 /* Multicast Address */
565 if (dev
->mc_count
> 4) /* Wait to do: Hash Table for multicast */
568 /* Multicast Address 1~4 case */
569 for (i
= 0, mcptr
= dev
->mc_list
; (i
<dev
->mc_count
) && (i
<4); i
++) {
570 adrp
= (u16
*)mcptr
->dmi_addr
;
571 outw(adrp
[0], ioaddr
+ 0x70 + 8*i
);
572 outw(adrp
[1], ioaddr
+ 0x72 + 8*i
);
573 outw(adrp
[2], ioaddr
+ 0x74 + 8*i
);
576 printk("M_ADDR: %04x %04x %04x\n", adrp
[0], adrp
[1], adrp
[2]);
579 for (i
= dev
->mc_count
; i
< 4; i
++) {
580 outw(0xffff, ioaddr
+ 0x68 + 8*i
);
581 outw(0xffff, ioaddr
+ 0x6A + 8*i
);
582 outw(0xffff, ioaddr
+ 0x6C + 8*i
);
586 static void netdev_get_drvinfo (struct net_device
*dev
, struct ethtool_drvinfo
*info
)
588 struct r6040_private
*rp
= dev
->priv
;
590 strcpy (info
->driver
, DRV_NAME
);
591 strcpy (info
->version
, DRV_VERSION
);
592 strcpy (info
->bus_info
, pci_name(rp
->pdev
));
595 static struct ethtool_ops netdev_ethtool_ops
= {
596 .get_drvinfo
= netdev_get_drvinfo
,
600 r6040_close(struct net_device
*dev
)
602 struct r6040_private
*lp
= dev
->priv
;
604 RDC_DBUG("r6040_close()", 0);
607 del_timer_sync(&lp
->timer
);
609 spin_lock_irq(&lp
->lock
);
611 netif_stop_queue(dev
);
615 spin_unlock_irq(&lp
->lock
);
621 #define RDC3210_CFGREG_ADDR 0x0CF8
622 #define RDC3210_CFGREG_DATA 0x0CFC
623 static int netdev_ioctl (struct net_device
*dev
, struct ifreq
*rq
, int cmd
)
625 struct r6040_private
*lp
= dev
->priv
;
627 RDC_DBUG("netdev_ioctl()", 0);
628 if (lp
->switch_sig
== 0x0243 && cmd
== SIOCDEVPRIVATE
)
630 unsigned long *data
= (unsigned long *)rq
->ifr_data
, args
[4];
631 int ioaddr
= dev
->base_addr
;
634 data
= (unsigned long *)rq
->ifr_data
;
635 if (copy_from_user(args
, data
, 4*sizeof(unsigned long)))
639 if(args
[0]&(1<<31))phy_write(ioaddr
,29,19,(phy_read(ioaddr
,29,19)|0x2000)); /* port 0 */
640 if(args
[0]&(1<<29))phy_write(ioaddr
,29,19,(phy_read(ioaddr
,29,19)|0x0020)); /* port 1 */
641 if(args
[0]&(1<<27))phy_write(ioaddr
,29,20,(phy_read(ioaddr
,29,20)|0x2000)); /* port 2 */
642 if(args
[0]&(1<<25))phy_write(ioaddr
,29,20,(phy_read(ioaddr
,29,20)|0x0020)); /* port 3 */
645 val
= 0x80000000 | (7 << 11) | ((0x48));
646 outl(val
, RDC3210_CFGREG_ADDR
);
648 val
= inl(RDC3210_CFGREG_DATA
);
650 val
|= (0x1 << DMZ_GPIO
);
651 outl(val
, RDC3210_CFGREG_DATA
);
654 val
= 0x80000000 | (7 << 11) | ((0x4C));
655 outl(val
, RDC3210_CFGREG_ADDR
);
657 val
= inl(RDC3210_CFGREG_DATA
);
658 if(args
[0]&(1<<23)) /* DMZ enabled */
659 val
&= ~(0x1 << DMZ_GPIO
); /* low activated */
660 else val
|= (0x1 << DMZ_GPIO
);
661 outl(val
, RDC3210_CFGREG_DATA
);
668 Stop RDC MAC and Free the allocated resource
670 static void r6040_down(struct net_device
*dev
)
672 struct r6040_private
*lp
= dev
->priv
;
674 int ioaddr
= dev
->base_addr
;
676 RDC_DBUG("r6040_down()", 0);
679 outw(0x0000, ioaddr
+ 0x40); /* Mask Off Interrupt */
680 outw(0x0001, ioaddr
+ 0x04); /* Reset RDC MAC */
682 do{}while((i
++ < 2048) && (inw(ioaddr
+ 0x04) & 0x1));
684 free_irq(dev
->irq
, dev
);
687 for (i
= 0; i
< RX_DCNT
; i
++) {
688 if (lp
->rx_insert_ptr
->skb_ptr
) {
689 pci_unmap_single(lp
->pdev
, lp
->rx_insert_ptr
->buf
, MAX_BUF_SIZE
, PCI_DMA_FROMDEVICE
);
690 dev_kfree_skb(lp
->rx_insert_ptr
->skb_ptr
);
691 lp
->rx_insert_ptr
->skb_ptr
= 0;
693 lp
->rx_insert_ptr
= lp
->rx_insert_ptr
->vndescp
;
697 for (i
= 0; i
< TX_DCNT
; i
++) {
698 if (lp
->tx_insert_ptr
->skb_ptr
) {
699 pci_unmap_single(lp
->pdev
, lp
->tx_insert_ptr
->buf
, MAX_BUF_SIZE
, PCI_DMA_TODEVICE
);
700 dev_kfree_skb(lp
->tx_insert_ptr
->skb_ptr
);
701 lp
->rx_insert_ptr
->skb_ptr
= 0;
703 lp
->tx_insert_ptr
= lp
->tx_insert_ptr
->vndescp
;
706 /* Free Descriptor memory */
707 pci_free_consistent(lp
->pdev
, ALLOC_DESC_SIZE
, lp
->desc_pool
, lp
->desc_dma
);
712 #ifdef CONFIG_R6040_NAPI
713 static int r6040_poll(struct net_device
*dev
, int *budget
)
715 struct r6040_private
*lp
;
716 struct r6040_descriptor
*descptr
;
717 struct sk_buff
*skb_ptr
;
722 ioaddr
= dev
->base_addr
;
723 lp
= (struct r6040_private
*)dev
->priv
;
724 unsigned long rx_work
= dev
->quota
;
729 /* TX interrupt request */
730 if (NAPI_status
& 0x10) {
732 descptr
= lp
->tx_remove_ptr
;
733 while(lp
->tx_free_desc
< TX_DCNT
) {
734 if (descptr
->status
& 0x8000) break; /* Not complte */
735 skb_ptr
= descptr
->skb_ptr
;
736 pci_unmap_single(lp
->pdev
, descptr
->buf
, skb_ptr
->len
, PCI_DMA_TODEVICE
);
737 dev_kfree_skb_irq(skb_ptr
); /* Free buffer */
738 descptr
->skb_ptr
= 0;
739 descptr
= descptr
->vndescp
; /* To next descriptor */
742 lp
->tx_remove_ptr
= descptr
;
743 if (lp
->tx_free_desc
) netif_wake_queue(dev
);
747 /* RX interrupt request */
748 if (NAPI_status
& 0x01) {
750 descptr
= lp
->rx_remove_ptr
;
751 while(lp
->rx_free_desc
) {
752 if (descptr
->status
& 0x8000) break; /* No Rx packet */
753 skb_ptr
= descptr
->skb_ptr
;
754 descptr
->skb_ptr
= 0;
756 skb_put(skb_ptr
, descptr
->len
- 4);
757 pci_unmap_single(lp
->pdev
, descptr
->buf
, MAX_BUF_SIZE
, PCI_DMA_FROMDEVICE
);
758 skb_ptr
->protocol
= eth_type_trans(skb_ptr
, dev
);
759 netif_receive_skb(skb_ptr
); /* Send to upper layer */
760 lp
->stats
.rx_packets
++;
761 lp
->stats
.rx_bytes
+= descptr
->len
;
762 descptr
= descptr
->vndescp
; /* To next descriptor */
765 lp
->rx_remove_ptr
= descptr
;
768 /* Allocate new RX buffer */
769 if (lp
->rx_free_desc
< RX_DCNT
) rx_buf_alloc(lp
,dev
);
772 netif_rx_complete(dev
);
773 outw(R6040_INT_MASK
,ioaddr
+ 0x40);
781 static void r6040_up(struct net_device
*dev
)
783 struct r6040_private
*lp
= dev
->priv
;
784 struct r6040_descriptor
*descptr
;
786 int ioaddr
= dev
->base_addr
;
788 dma_addr_t desc_dma
, start_dma
;
790 RDC_DBUG("r6040_up()", 0);
793 lp
->tx_free_desc
= TX_DCNT
;
794 lp
->rx_free_desc
= 0;
796 /* Init descriptor */
797 memset(lp
->desc_pool
, 0, ALLOC_DESC_SIZE
); /* Let all descriptor = 0 */
798 lp
->tx_insert_ptr
= (struct r6040_descriptor
*)lp
->desc_pool
;
799 lp
->tx_remove_ptr
= lp
->tx_insert_ptr
;
800 lp
->rx_insert_ptr
= (struct r6040_descriptor
*)lp
->tx_insert_ptr
+TX_DCNT
;
801 lp
->rx_remove_ptr
= lp
->rx_insert_ptr
;
803 /* Init TX descriptor */
804 descptr
= lp
->tx_insert_ptr
;
805 desc_dma
= lp
->desc_dma
;
806 start_dma
= desc_dma
;
807 for (i
= 0; i
< TX_DCNT
; i
++) {
808 descptr
->ndesc
= cpu_to_le32(desc_dma
+ sizeof(struct r6040_descriptor
));
809 descptr
->vndescp
= (descptr
+ 1);
810 descptr
= (descptr
+ 1);
811 desc_dma
+= sizeof(struct r6040_descriptor
);
813 (descptr
- 1)->ndesc
= cpu_to_le32(start_dma
);
814 (descptr
- 1)->vndescp
= lp
->tx_insert_ptr
;
816 /* Init RX descriptor */
817 start_dma
= desc_dma
;
818 descptr
= lp
->rx_insert_ptr
;
819 for (i
= 0; i
< RX_DCNT
; i
++) {
820 descptr
->ndesc
= cpu_to_le32(desc_dma
+ sizeof(struct r6040_descriptor
));
821 descptr
->vndescp
= (descptr
+ 1);
822 descptr
= (descptr
+ 1);
823 desc_dma
+= sizeof(struct r6040_descriptor
);
825 (descptr
- 1)->ndesc
= cpu_to_le32(start_dma
);
826 (descptr
- 1)->vndescp
= lp
->rx_insert_ptr
;
828 /* Allocate buffer for RX descriptor */
829 rx_buf_alloc(lp
,dev
);
832 descptr
= lp
->tx_insert_ptr
;
833 for (i
= 0; i
< TX_DCNT
; i
++) {
834 printk("%08lx:%04x %04x %08lx %08lx %08lx %08lx\n", descptr
, descptr
->status
, descptr
->len
, descptr
->buf
, descptr
->skb_ptr
, descptr
->ndesc
, descptr
->vndescp
);
835 descptr
= descptr
->vndescp
;
837 descptr
= lp
->rx_insert_ptr
;
838 for (i
= 0; i
< RX_DCNT
; i
++) {
839 printk("%08lx:%04x %04x %08lx %08lx %08lx %08lx\n", descptr
, descptr
->status
, descptr
->len
, descptr
->buf
, descptr
->skb_ptr
, descptr
->ndesc
, descptr
->vndescp
);
840 descptr
= descptr
->vndescp
;
844 /* MAC operation register */
845 outw(0x01, ioaddr
+0x04); /* Reset MAC */
846 outw(2 , ioaddr
+0xAC); /* Reset internal state machine */
847 outw(0 , ioaddr
+0xAC);
850 /* TX and RX descriptor start Register */
851 tmp_addr
= cpu_to_le32(lp
->tx_insert_ptr
);
852 tmp_addr
= virt_to_bus((volatile void *)tmp_addr
);
853 outw((u16
) tmp_addr
, ioaddr
+0x2c);
854 outw(tmp_addr
>> 16, ioaddr
+0x30);
855 tmp_addr
= cpu_to_le32(lp
->rx_insert_ptr
);
856 tmp_addr
= virt_to_bus((volatile void *)tmp_addr
);
857 outw((u16
) tmp_addr
, ioaddr
+0x34);
858 outw(tmp_addr
>> 16, ioaddr
+0x38);
860 /* Buffer Size Register */
861 outw(MAX_BUF_SIZE
, ioaddr
+0x18);
863 if ((lp
->switch_sig
= phy_read(ioaddr
, 0, 2)) == 0x0243) // ICPlus IP175C Signature
865 phy_write(ioaddr
, 29,31, 0x175C); //Enable registers
866 lp
->phy_mode
= 0x8000;
869 phy_write(ioaddr
, lp
->phy_addr
, 4, PHY_CAP
);
870 phy_write(ioaddr
, lp
->phy_addr
, 0, PHY_MODE
);
872 if (PHY_MODE
== 0x3100)
873 lp
->phy_mode
= phy_mode_chk(dev
);
874 else lp
->phy_mode
= (PHY_MODE
& 0x0100) ? 0x8000:0x0;
876 /* MAC Bus Control Register */
877 outw(MBCR_DEFAULT
, ioaddr
+0x8);
879 /* MAC TX/RX Enable */
880 lp
->mcr0
|= lp
->phy_mode
;
881 outw(lp
->mcr0
, ioaddr
);
883 /* Interrupt Mask Register */
884 outw(R6040_INT_MASK
, ioaddr
+ 0x40);
888 A periodic timer routine
889 Polling PHY Chip Link Status
891 static void r6040_timer(unsigned long data
)
893 struct net_device
*dev
=(struct net_device
*)data
;
894 struct r6040_private
*lp
= dev
->priv
;
895 u16 ioaddr
= dev
->base_addr
, phy_mode
;
897 RDC_DBUG("r6040_timer()", 0);
899 /* Polling PHY Chip Status */
900 if (PHY_MODE
== 0x3100)
901 phy_mode
= phy_mode_chk(dev
);
902 else phy_mode
= (PHY_MODE
& 0x0100) ? 0x8000:0x0;
904 if (phy_mode
!= lp
->phy_mode
) {
905 lp
->phy_mode
= phy_mode
;
906 lp
->mcr0
= (lp
->mcr0
& 0x7fff) | phy_mode
;
907 outw(lp
->mcr0
, ioaddr
);
908 printk("<RDC> Link Change %x \n", inw(ioaddr
));
912 // printk("<RDC> Timer: CR0 %x CR40 %x CR3C %x\n", inw(ioaddr), inw(ioaddr+0x40), inw(ioaddr+0x3c));
914 /* Timer active again */
915 lp
->timer
.expires
= TIMER_WUT
;
916 add_timer(&lp
->timer
);
919 /* Allocate skb buffer for rx descriptor */
920 static void rx_buf_alloc(struct r6040_private
*lp
,struct net_device
*dev
)
922 struct r6040_descriptor
*descptr
;
923 int ioaddr
= dev
->base_addr
;
925 RDC_DBUG("rx_buf_alloc()", 0);
926 descptr
= lp
->rx_insert_ptr
;
927 while(lp
->rx_free_desc
< RX_DCNT
){
928 descptr
->skb_ptr
= dev_alloc_skb(MAX_BUF_SIZE
);
929 if (!descptr
->skb_ptr
) break;
930 descptr
->buf
= cpu_to_le32(pci_map_single(lp
->pdev
, descptr
->skb_ptr
->tail
, MAX_BUF_SIZE
, PCI_DMA_FROMDEVICE
));
931 descptr
->status
= 0x8000;
932 descptr
= descptr
->vndescp
;
934 outw(lp
->mcr0
| 0x0002, ioaddr
); //Trigger Rx DMA
936 lp
->rx_insert_ptr
= descptr
;
939 /* Status of PHY CHIP */
940 static int phy_mode_chk(struct net_device
*dev
)
942 struct r6040_private
*lp
= dev
->priv
;
943 int ioaddr
= dev
->base_addr
, phy_dat
;
945 RDC_DBUG("phy_mode_chk()", 0);
947 /* PHY Link Status Check */
948 phy_dat
= phy_read(ioaddr
, lp
->phy_addr
, 1);
949 if (!(phy_dat
& 0x4)) return 0x8000; /* Link Failed, full duplex */
951 /* PHY Chip Auto-Negotiation Status */
952 phy_dat
= phy_read(ioaddr
, lp
->phy_addr
, 1);
953 if (phy_dat
& 0x0020) {
954 /* Auto Negotiation Mode */
955 phy_dat
= phy_read(ioaddr
, lp
->phy_addr
, 5);
956 phy_dat
&= phy_read(ioaddr
, lp
->phy_addr
, 4);
957 if (phy_dat
& 0x140) phy_dat
= 0x8000;
961 phy_dat
= phy_read(ioaddr
, lp
->phy_addr
, 0);
962 if (phy_dat
& 0x100) phy_dat
= 0x8000;
963 else phy_dat
= 0x0000;
969 /* Read a word data from PHY Chip */
970 static int phy_read(int ioaddr
, int phy_addr
, int reg_idx
)
974 RDC_DBUG("phy_read()", 0);
975 outw(0x2000 + reg_idx
+ (phy_addr
<< 8), ioaddr
+ 0x20);
976 do{}while( (i
++ < 2048) && (inw(ioaddr
+ 0x20) & 0x2000) );
978 return inw(ioaddr
+ 0x24);
981 /* Write a word data from PHY Chip */
982 static void phy_write(int ioaddr
, int phy_addr
, int reg_idx
, int dat
)
986 RDC_DBUG("phy_write()", 0);
987 outw(dat
, ioaddr
+ 0x28);
988 outw(0x4000 + reg_idx
+ (phy_addr
<< 8), ioaddr
+ 0x20);
989 do{}while( (i
++ < 2048) && (inw(ioaddr
+ 0x20) & 0x4000) );
996 static struct pci_device_id r6040_pci_tbl
[] = {
997 {0x17F3, 0x6040, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, RDC_6040
},
998 //{0x1106, 0x3065, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RDC_6040},
999 {0,} /* terminate list */
1001 MODULE_DEVICE_TABLE(pci
, r6040_pci_tbl
);
1003 static struct pci_driver r6040_driver
= {
1005 .id_table
= r6040_pci_tbl
,
1006 .probe
= r6040_init_one
,
1007 .remove
= __devexit_p(r6040_remove_one
),
1011 static int __init
r6040_init (void)
1013 RDC_DBUG("r6040_init()", 0);
1016 printed_version
= 1;
1020 struct net_device
*the_parent
= dev_get_by_name(parent
);
1022 if (the_parent
== NULL
)
1024 printk (KERN_ERR DRV_NAME
": Unknown device \"%s\" specified.\n", parent
);
1027 memcpy((u8
*)&adr_table
[0][0], the_parent
->dev_addr
, 6);
1028 memcpy((u8
*)&adr_table
[1][0], the_parent
->dev_addr
, 6);
1029 ++*(u8
*)&adr_table
[0][5];
1031 return pci_register_driver (&r6040_driver
);
1035 static void __exit
r6040_cleanup (void)
1037 RDC_DBUG("r6040_cleanup()", 0);
1038 pci_unregister_driver (&r6040_driver
);
1041 module_init(r6040_init
);
1042 module_exit(r6040_cleanup
);
1047 * compile-command: "gcc -DMODULE -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -c r6040.c `[ -f /usr/include/linux/modversions.h ] && echo -DMODVERSIONS`"