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
;
144 struct r6040_chip_info
{
151 #ifdef CONFIG_R6040_NAPI
152 static int NAPI_status
;
155 static int __devinitdata printed_version
;
156 #ifdef CONFIG_R6040_NAPI
157 static char version
[] __devinitdata
=
158 KERN_INFO DRV_NAME
": RDC R6040 NAPI net driver, version "DRV_VERSION
" (" DRV_RELDATE
")\n";
160 static char version
[] __devinitdata
=
161 KERN_INFO DRV_NAME
": RDC R6040 net driver, version "DRV_VERSION
" (" DRV_RELDATE
")\n";
163 static struct r6040_chip_info r6040_chip_info
[] __devinitdata
=
165 { "RDC R6040 Knight", R6040_PCI_CMD
, R6040_IO_SIZE
, 0}
168 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
169 static int NUM_MAC_TABLE
= 2 ;
172 static int phy_table
[] = { 0x1, 0x2};
173 static u8 adr_table
[2][8] = {{0x00, 0x00, 0x60, 0x00, 0x00, 0x01}, {0x00, 0x00, 0x60, 0x00, 0x00, 0x02}};
175 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 10)
176 module_param_array(adr_table
, int, &NUM_MAC_TABLE
, 0644);
177 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
178 module_param_array(adr_table
, int, NUM_MAC_TABLE
, 0644);
180 MODULE_PARM(adr_table
, "2-4i");
182 MODULE_PARM_DESC(adr_table
, "MAC Address (assigned)");
184 static int r6040_open(struct net_device
*dev
);
185 static int r6040_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
);
186 static irqreturn_t
r6040_interrupt(int irq
, void *dev_id
);
187 static struct net_device_stats
*r6040_get_stats(struct net_device
*dev
);
188 static int r6040_close(struct net_device
*dev
);
189 static void set_multicast_list(struct net_device
*dev
);
190 static struct ethtool_ops netdev_ethtool_ops
;
191 static void r6040_down(struct net_device
*dev
);
192 static void r6040_up(struct net_device
*dev
);
193 static void r6040_tx_timeout (struct net_device
*dev
);
194 static void r6040_timer(unsigned long);
196 static int phy_mode_chk(struct net_device
*dev
);
197 static int phy_read(int ioaddr
, int phy_adr
, int reg_idx
);
198 static void phy_write(int ioaddr
, int phy_adr
, int reg_idx
, int dat
);
199 static void rx_buf_alloc(struct r6040_private
*lp
,struct net_device
*dev
);
200 #ifdef CONFIG_R6040_NAPI
201 static int r6040_poll(struct net_device
*netdev
, int *budget
);
205 static int __devinit
r6040_init_one (struct pci_dev
*pdev
,
206 const struct pci_device_id
*ent
)
208 struct net_device
*dev
;
209 struct r6040_private
*lp
;
210 int ioaddr
, io_size
, err
;
211 static int card_idx
= -1;
212 int chip_id
= (int)ent
->driver_data
;
214 RDC_DBUG("r6040_init_one()", 0);
216 if (printed_version
++)
219 if ((err
= pci_enable_device (pdev
)))
222 /* this should always be supported */
223 if (pci_set_dma_mask(pdev
, 0xffffffff)) {
224 printk(KERN_ERR DRV_NAME
"32-bit PCI DMA addresses not supported by the card!?\n");
229 io_size
= r6040_chip_info
[chip_id
].io_size
;
230 if (pci_resource_len (pdev
, 0) < io_size
) {
234 ioaddr
= pci_resource_start (pdev
, 0); /* IO map base address */
235 pci_set_master(pdev
);
237 dev
= alloc_etherdev(sizeof(struct r6040_private
));
240 SET_MODULE_OWNER(dev
);
242 if (pci_request_regions(pdev
, DRV_NAME
)) {
243 printk(KERN_ERR DRV_NAME
": Failed to request PCI regions\n");
245 goto err_out_disable
;
248 /* Init system & device */
250 dev
->base_addr
= ioaddr
;
251 dev
->irq
= pdev
->irq
;
253 spin_lock_init(&lp
->lock
);
254 pci_set_drvdata(pdev
, dev
);
256 /* Set MAC address */
258 memcpy(dev
->dev_addr
, (u8
*)&adr_table
[card_idx
][0], 6);
260 /* Link new device into r6040_root_dev */
263 /* Init RDC private data */
265 lp
->phy_addr
= phy_table
[card_idx
];
267 /* The RDC-specific entries in the device structure. */
268 dev
->open
= &r6040_open
;
269 dev
->hard_start_xmit
= &r6040_start_xmit
;
270 dev
->stop
= &r6040_close
;
271 dev
->get_stats
= &r6040_get_stats
;
272 dev
->set_multicast_list
= &set_multicast_list
;
273 dev
->ethtool_ops
= &netdev_ethtool_ops
;
274 dev
->tx_timeout
= &r6040_tx_timeout
;
275 dev
->watchdog_timeo
= TX_TIMEOUT
;
276 #ifdef CONFIG_R6040_NAPI
277 dev
->poll
= &r6040_poll
;
281 /* Register net device. After this dev->name assign */
282 if ((err
= register_netdev(dev
))) {
283 printk(KERN_ERR DRV_NAME
": Failed to register net device\n");
287 netif_carrier_on(dev
);
291 pci_release_regions(pdev
);
293 pci_disable_device(pdev
);
294 pci_set_drvdata(pdev
, NULL
);
300 static void __devexit
r6040_remove_one (struct pci_dev
*pdev
)
302 struct net_device
*dev
= pci_get_drvdata(pdev
);
304 unregister_netdev(dev
);
305 pci_release_regions(pdev
);
307 pci_disable_device(pdev
);
308 pci_set_drvdata(pdev
, NULL
);
312 r6040_open(struct net_device
*dev
)
314 struct r6040_private
*lp
= dev
->priv
;
317 RDC_DBUG("r6040_open()", 0);
319 /* Request IRQ and Register interrupt handler */
320 i
= request_irq(dev
->irq
, &r6040_interrupt
, SA_SHIRQ
, dev
->name
, dev
);
323 /* Allocate Descriptor memory */
324 lp
->desc_pool
= pci_alloc_consistent(lp
->pdev
, ALLOC_DESC_SIZE
, &lp
->desc_dma
);
325 if (!lp
->desc_pool
) return -ENOMEM
;
329 netif_start_queue(dev
);
331 /* set and active a timer process */
332 init_timer(&lp
->timer
);
333 lp
->timer
.expires
= TIMER_WUT
;
334 lp
->timer
.data
= (unsigned long)dev
;
335 lp
->timer
.function
= &r6040_timer
;
336 add_timer(&lp
->timer
);
342 r6040_tx_timeout (struct net_device
*dev
)
344 struct r6040_private
*lp
= dev
->priv
;
345 //int ioaddr = dev->base_addr;
346 //struct r6040_descriptor *descptr = lp->tx_remove_ptr;
348 RDC_DBUG("r6040_tx_timeout()", 0);
350 /* Transmitter timeout, serious problems. */
351 /* Sten: Nothing need to do so far. */
352 printk(KERN_ERR DRV_NAME
": Big Trobule, transmit timeout/n");
353 lp
->stats
.tx_errors
++;
354 netif_stop_queue(dev
);
356 //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));
358 //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);
363 r6040_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
365 struct r6040_private
*lp
= dev
->priv
;
366 struct r6040_descriptor
*descptr
;
367 int ioaddr
= dev
->base_addr
;
370 RDC_DBUG("r6040_start_xmit()", 0);
372 if (skb
== NULL
) /* NULL skb directly return */
374 if (skb
->len
>= MAX_BUF_SIZE
) { /* Packet too long, drop it */
379 /* Critical Section */
380 spin_lock_irqsave(&lp
->lock
, flags
);
382 /* TX resource check */
383 if (!lp
->tx_free_desc
) {
384 spin_unlock_irqrestore(&lp
->lock
, flags
);
385 printk(KERN_ERR DRV_NAME
": NO TX DESC ");
389 /* Statistic Counter */
390 lp
->stats
.tx_packets
++;
391 lp
->stats
.tx_bytes
+= skb
->len
;
393 /* Set TX descriptor & Transmit it */
395 descptr
= lp
->tx_insert_ptr
;
396 if (skb
->len
< 0x3c) descptr
->len
= 0x3c;
397 else descptr
->len
= skb
->len
;
398 descptr
->skb_ptr
= skb
;
399 descptr
->buf
= cpu_to_le32(pci_map_single(lp
->pdev
, skb
->data
, skb
->len
, PCI_DMA_TODEVICE
));
400 descptr
->status
= 0x8000;
401 outw(0x01, ioaddr
+ 0x14);
402 lp
->tx_insert_ptr
= descptr
->vndescp
;
405 printk("Xmit(): %08lx:%04x %04x %08lx %08lx %08lx %08lx\n", descptr
, descptr
->status
, descptr
->len
, descptr
->buf
, descptr
->skb_ptr
, descptr
->ndesc
, descptr
->vndescp
);
408 /* If no tx resource, stop */
409 if (!lp
->tx_free_desc
)
410 netif_stop_queue(dev
);
412 dev
->trans_start
= jiffies
;
413 spin_unlock_irqrestore(&lp
->lock
, flags
);
417 /* The RDC interrupt handler. */
419 r6040_interrupt(int irq
, void *dev_id
)
421 struct net_device
*dev
= dev_id
;
422 struct r6040_private
*lp
;
423 struct r6040_descriptor
*descptr
;
424 struct sk_buff
*skb_ptr
;
427 #ifdef CONFIG_R6040_NAPI
433 RDC_DBUG("r6040_interrupt()", 0);
435 printk (KERN_ERR DRV_NAME
": INT() unknown device.\n");
436 return IRQ_RETVAL(handled
);
439 lp
= (struct r6040_private
*)dev
->priv
;
440 spin_lock_irqsave(&lp
->lock
, flags
);
442 /* Check MAC Interrupt status */
443 ioaddr
= dev
->base_addr
;
444 outw(0x0, ioaddr
+ 0x40); /* Mask Off RDC MAC interrupt */
445 status
= inw(ioaddr
+ 0x3c); /* Read INTR status and clear */
448 #ifdef CONFIG_R6040_NAPI
450 if(netif_rx_schedule_prep(dev
))
452 NAPI_status
= status
;
453 __netif_rx_schedule(dev
);
456 spin_unlock_irqrestore(&lp
->lock
, flags
);
457 return IRQ_RETVAL(handled
);
459 /* TX interrupt request */
462 descptr
= lp
->tx_remove_ptr
;
463 while(lp
->tx_free_desc
< TX_DCNT
) {
464 if (descptr
->status
& 0x8000) break; /* Not complte */
465 skb_ptr
= descptr
->skb_ptr
;
466 pci_unmap_single(lp
->pdev
, descptr
->buf
, skb_ptr
->len
, PCI_DMA_TODEVICE
);
467 dev_kfree_skb_irq(skb_ptr
); /* Free buffer */
468 descptr
->skb_ptr
= 0;
469 descptr
= descptr
->vndescp
; /* To next descriptor */
472 lp
->tx_remove_ptr
= descptr
;
473 if (lp
->tx_free_desc
) netif_wake_queue(dev
);
476 /* RX interrupt request */
479 descptr
= lp
->rx_remove_ptr
;
480 while(lp
->rx_free_desc
) {
481 if (descptr
->status
& 0x8000) break; /* No Rx packet */
482 skb_ptr
= descptr
->skb_ptr
;
483 descptr
->skb_ptr
= 0;
485 skb_put(skb_ptr
, descptr
->len
- 4);
486 pci_unmap_single(lp
->pdev
, descptr
->buf
, MAX_BUF_SIZE
, PCI_DMA_FROMDEVICE
);
487 skb_ptr
->protocol
= eth_type_trans(skb_ptr
, dev
);
488 netif_rx(skb_ptr
); /* Send to upper layer */
489 lp
->stats
.rx_packets
++;
490 lp
->stats
.rx_bytes
+= descptr
->len
;
491 descptr
= descptr
->vndescp
; /* To next descriptor */
494 lp
->rx_remove_ptr
= descptr
;
497 /* Allocate new RX buffer */
498 if (lp
->rx_free_desc
< RX_DCNT
) rx_buf_alloc(lp
,dev
);
500 outw(R6040_INT_MASK
, ioaddr
+ 0x40); /* TX/RX interrupt enable */
501 spin_unlock_irqrestore(&lp
->lock
, flags
);
503 return IRQ_RETVAL(handled
);
507 static struct net_device_stats
*
508 r6040_get_stats(struct net_device
*dev
)
510 struct r6040_private
*lp
= dev
->priv
;
512 RDC_DBUG("r6040_get_stats()", 0);
517 * Set or clear the multicast filter for this adaptor.
520 set_multicast_list(struct net_device
*dev
)
522 struct r6040_private
*lp
= dev
->priv
;
523 struct dev_mc_list
*mcptr
;
524 int ioaddr
= dev
->base_addr
;
528 RDC_DBUG("set_multicast_list()", 0);
531 adrp
= (u16
*) dev
->dev_addr
;
532 outw(adrp
[0], ioaddr
+ 0x68);
533 outw(adrp
[1], ioaddr
+ 0x6A);
534 outw(adrp
[2], ioaddr
+ 0x6C);
538 printk("MAC ADDR: %04x %04x %04x\n", adrp
[0], adrp
[1], adrp
[2]);
541 /* Promiscous Mode */
542 spin_lock_irqsave(&lp
->lock
, flags
);
543 i
= inw(ioaddr
) & ~0x0120; /* Clear AMCP & PROM */
544 if (dev
->flags
& IFF_PROMISC
)
549 if (dev
->mc_count
> 4) i
|= 0x0020; /* Too many multicast address */
551 spin_unlock_irqrestore(&lp
->lock
, flags
);
553 /* Multicast Address */
554 if (dev
->mc_count
> 4) /* Wait to do: Hash Table for multicast */
557 /* Multicast Address 1~4 case */
558 for (i
= 0, mcptr
= dev
->mc_list
; (i
<dev
->mc_count
) && (i
<4); i
++) {
559 adrp
= (u16
*)mcptr
->dmi_addr
;
560 outw(adrp
[0], ioaddr
+ 0x70 + 8*i
);
561 outw(adrp
[1], ioaddr
+ 0x72 + 8*i
);
562 outw(adrp
[2], ioaddr
+ 0x74 + 8*i
);
565 printk("M_ADDR: %04x %04x %04x\n", adrp
[0], adrp
[1], adrp
[2]);
568 for (i
= dev
->mc_count
; i
< 4; i
++) {
569 outw(0xffff, ioaddr
+ 0x68 + 8*i
);
570 outw(0xffff, ioaddr
+ 0x6A + 8*i
);
571 outw(0xffff, ioaddr
+ 0x6C + 8*i
);
575 static void netdev_get_drvinfo (struct net_device
*dev
, struct ethtool_drvinfo
*info
)
577 struct r6040_private
*rp
= dev
->priv
;
579 strcpy (info
->driver
, DRV_NAME
);
580 strcpy (info
->version
, DRV_VERSION
);
581 strcpy (info
->bus_info
, pci_name(rp
->pdev
));
584 static struct ethtool_ops netdev_ethtool_ops
= {
585 .get_drvinfo
= netdev_get_drvinfo
,
589 r6040_close(struct net_device
*dev
)
591 struct r6040_private
*lp
= dev
->priv
;
593 RDC_DBUG("r6040_close()", 0);
596 del_timer_sync(&lp
->timer
);
598 spin_lock_irq(&lp
->lock
);
600 netif_stop_queue(dev
);
604 spin_unlock_irq(&lp
->lock
);
610 Stop RDC MAC and Free the allocated resource
612 static void r6040_down(struct net_device
*dev
)
614 struct r6040_private
*lp
= dev
->priv
;
616 int ioaddr
= dev
->base_addr
;
618 RDC_DBUG("r6040_down()", 0);
621 outw(0x0000, ioaddr
+ 0x40); /* Mask Off Interrupt */
622 outw(0x0001, ioaddr
+ 0x04); /* Reset RDC MAC */
624 do{}while((i
++ < 2048) && (inw(ioaddr
+ 0x04) & 0x1));
626 free_irq(dev
->irq
, dev
);
629 for (i
= 0; i
< RX_DCNT
; i
++) {
630 if (lp
->rx_insert_ptr
->skb_ptr
) {
631 pci_unmap_single(lp
->pdev
, lp
->rx_insert_ptr
->buf
, MAX_BUF_SIZE
, PCI_DMA_FROMDEVICE
);
632 dev_kfree_skb(lp
->rx_insert_ptr
->skb_ptr
);
633 lp
->rx_insert_ptr
->skb_ptr
= 0;
635 lp
->rx_insert_ptr
= lp
->rx_insert_ptr
->vndescp
;
639 for (i
= 0; i
< TX_DCNT
; i
++) {
640 if (lp
->tx_insert_ptr
->skb_ptr
) {
641 pci_unmap_single(lp
->pdev
, lp
->tx_insert_ptr
->buf
, MAX_BUF_SIZE
, PCI_DMA_TODEVICE
);
642 dev_kfree_skb(lp
->tx_insert_ptr
->skb_ptr
);
643 lp
->rx_insert_ptr
->skb_ptr
= 0;
645 lp
->tx_insert_ptr
= lp
->tx_insert_ptr
->vndescp
;
648 /* Free Descriptor memory */
649 pci_free_consistent(lp
->pdev
, ALLOC_DESC_SIZE
, lp
->desc_pool
, lp
->desc_dma
);
654 #ifdef CONFIG_R6040_NAPI
655 static int r6040_poll(struct net_device
*dev
, int *budget
)
657 struct r6040_private
*lp
;
658 struct r6040_descriptor
*descptr
;
659 struct sk_buff
*skb_ptr
;
664 ioaddr
= dev
->base_addr
;
665 lp
= (struct r6040_private
*)dev
->priv
;
666 unsigned long rx_work
= dev
->quota
;
671 /* TX interrupt request */
672 if (NAPI_status
& 0x10) {
674 descptr
= lp
->tx_remove_ptr
;
675 while(lp
->tx_free_desc
< TX_DCNT
) {
676 if (descptr
->status
& 0x8000) break; /* Not complte */
677 skb_ptr
= descptr
->skb_ptr
;
678 pci_unmap_single(lp
->pdev
, descptr
->buf
, skb_ptr
->len
, PCI_DMA_TODEVICE
);
679 dev_kfree_skb_irq(skb_ptr
); /* Free buffer */
680 descptr
->skb_ptr
= 0;
681 descptr
= descptr
->vndescp
; /* To next descriptor */
684 lp
->tx_remove_ptr
= descptr
;
685 if (lp
->tx_free_desc
) netif_wake_queue(dev
);
689 /* RX interrupt request */
690 if (NAPI_status
& 0x01) {
692 descptr
= lp
->rx_remove_ptr
;
693 while(lp
->rx_free_desc
) {
694 if (descptr
->status
& 0x8000) break; /* No Rx packet */
695 skb_ptr
= descptr
->skb_ptr
;
696 descptr
->skb_ptr
= 0;
698 skb_put(skb_ptr
, descptr
->len
- 4);
699 pci_unmap_single(lp
->pdev
, descptr
->buf
, MAX_BUF_SIZE
, PCI_DMA_FROMDEVICE
);
700 skb_ptr
->protocol
= eth_type_trans(skb_ptr
, dev
);
701 netif_receive_skb(skb_ptr
); /* Send to upper layer */
702 lp
->stats
.rx_packets
++;
703 lp
->stats
.rx_bytes
+= descptr
->len
;
704 descptr
= descptr
->vndescp
; /* To next descriptor */
707 lp
->rx_remove_ptr
= descptr
;
710 /* Allocate new RX buffer */
711 if (lp
->rx_free_desc
< RX_DCNT
) rx_buf_alloc(lp
,dev
);
714 netif_rx_complete(dev
);
715 outw(R6040_INT_MASK
,ioaddr
+ 0x40);
723 static void r6040_up(struct net_device
*dev
)
725 struct r6040_private
*lp
= dev
->priv
;
726 struct r6040_descriptor
*descptr
;
728 int ioaddr
= dev
->base_addr
;
730 dma_addr_t desc_dma
, start_dma
;
732 RDC_DBUG("r6040_up()", 0);
735 lp
->tx_free_desc
= TX_DCNT
;
736 lp
->rx_free_desc
= 0;
738 /* Init descriptor */
739 memset(lp
->desc_pool
, 0, ALLOC_DESC_SIZE
); /* Let all descriptor = 0 */
740 lp
->tx_insert_ptr
= (struct r6040_descriptor
*)lp
->desc_pool
;
741 lp
->tx_remove_ptr
= lp
->tx_insert_ptr
;
742 lp
->rx_insert_ptr
= (struct r6040_descriptor
*)lp
->tx_insert_ptr
+TX_DCNT
;
743 lp
->rx_remove_ptr
= lp
->rx_insert_ptr
;
745 /* Init TX descriptor */
746 descptr
= lp
->tx_insert_ptr
;
747 desc_dma
= lp
->desc_dma
;
748 start_dma
= desc_dma
;
749 for (i
= 0; i
< TX_DCNT
; i
++) {
750 descptr
->ndesc
= cpu_to_le32(desc_dma
+ sizeof(struct r6040_descriptor
));
751 descptr
->vndescp
= (descptr
+ 1);
752 descptr
= (descptr
+ 1);
753 desc_dma
+= sizeof(struct r6040_descriptor
);
755 (descptr
- 1)->ndesc
= cpu_to_le32(start_dma
);
756 (descptr
- 1)->vndescp
= lp
->tx_insert_ptr
;
758 /* Init RX descriptor */
759 start_dma
= desc_dma
;
760 descptr
= lp
->rx_insert_ptr
;
761 for (i
= 0; i
< RX_DCNT
; i
++) {
762 descptr
->ndesc
= cpu_to_le32(desc_dma
+ sizeof(struct r6040_descriptor
));
763 descptr
->vndescp
= (descptr
+ 1);
764 descptr
= (descptr
+ 1);
765 desc_dma
+= sizeof(struct r6040_descriptor
);
767 (descptr
- 1)->ndesc
= cpu_to_le32(start_dma
);
768 (descptr
- 1)->vndescp
= lp
->rx_insert_ptr
;
770 /* Allocate buffer for RX descriptor */
771 rx_buf_alloc(lp
,dev
);
774 descptr
= lp
->tx_insert_ptr
;
775 for (i
= 0; i
< TX_DCNT
; i
++) {
776 printk("%08lx:%04x %04x %08lx %08lx %08lx %08lx\n", descptr
, descptr
->status
, descptr
->len
, descptr
->buf
, descptr
->skb_ptr
, descptr
->ndesc
, descptr
->vndescp
);
777 descptr
= descptr
->vndescp
;
779 descptr
= lp
->rx_insert_ptr
;
780 for (i
= 0; i
< RX_DCNT
; i
++) {
781 printk("%08lx:%04x %04x %08lx %08lx %08lx %08lx\n", descptr
, descptr
->status
, descptr
->len
, descptr
->buf
, descptr
->skb_ptr
, descptr
->ndesc
, descptr
->vndescp
);
782 descptr
= descptr
->vndescp
;
786 /* MAC operation register */
787 outw(0x01, ioaddr
+0x04); /* Reset MAC */
788 outw(2 , ioaddr
+0xAC); /* Reset internal state machine */
789 outw(0 , ioaddr
+0xAC);
792 /* TX and RX descriptor start Register */
793 tmp_addr
= cpu_to_le32(lp
->tx_insert_ptr
);
794 tmp_addr
= virt_to_bus((volatile void *)tmp_addr
);
795 outw((u16
) tmp_addr
, ioaddr
+0x2c);
796 outw(tmp_addr
>> 16, ioaddr
+0x30);
797 tmp_addr
= cpu_to_le32(lp
->rx_insert_ptr
);
798 tmp_addr
= virt_to_bus((volatile void *)tmp_addr
);
799 outw((u16
) tmp_addr
, ioaddr
+0x34);
800 outw(tmp_addr
>> 16, ioaddr
+0x38);
802 /* Buffer Size Register */
803 outw(MAX_BUF_SIZE
, ioaddr
+0x18);
806 phy_write(ioaddr
, lp
->phy_addr
, 4, PHY_CAP
);
807 phy_write(ioaddr
, lp
->phy_addr
, 0, PHY_MODE
);
809 if (PHY_MODE
== 0x3100)
810 lp
->phy_mode
= phy_mode_chk(dev
);
811 else lp
->phy_mode
= (PHY_MODE
& 0x0100) ? 0x8000:0x0;
813 /* MAC Bus Control Register */
814 outw(MBCR_DEFAULT
, ioaddr
+0x8);
816 /* MAC TX/RX Enable */
817 lp
->mcr0
|= lp
->phy_mode
;
818 outw(lp
->mcr0
, ioaddr
);
820 /* Interrupt Mask Register */
821 outw(R6040_INT_MASK
, ioaddr
+ 0x40);
825 A periodic timer routine
826 Polling PHY Chip Link Status
828 static void r6040_timer(unsigned long data
)
830 struct net_device
*dev
=(struct net_device
*)data
;
831 struct r6040_private
*lp
= dev
->priv
;
832 u16 ioaddr
= dev
->base_addr
, phy_mode
;
834 RDC_DBUG("r6040_timer()", 0);
836 /* Polling PHY Chip Status */
837 if (PHY_MODE
== 0x3100)
838 phy_mode
= phy_mode_chk(dev
);
839 else phy_mode
= (PHY_MODE
& 0x0100) ? 0x8000:0x0;
841 if (phy_mode
!= lp
->phy_mode
) {
842 lp
->phy_mode
= phy_mode
;
843 lp
->mcr0
= (lp
->mcr0
& 0x7fff) | phy_mode
;
844 outw(lp
->mcr0
, ioaddr
);
845 printk("<RDC> Link Change %x \n", inw(ioaddr
));
849 // printk("<RDC> Timer: CR0 %x CR40 %x CR3C %x\n", inw(ioaddr), inw(ioaddr+0x40), inw(ioaddr+0x3c));
851 /* Timer active again */
852 lp
->timer
.expires
= TIMER_WUT
;
853 add_timer(&lp
->timer
);
856 /* Allocate skb buffer for rx descriptor */
857 static void rx_buf_alloc(struct r6040_private
*lp
,struct net_device
*dev
)
859 struct r6040_descriptor
*descptr
;
860 int ioaddr
= dev
->base_addr
;
862 RDC_DBUG("rx_buf_alloc()", 0);
863 descptr
= lp
->rx_insert_ptr
;
864 while(lp
->rx_free_desc
< RX_DCNT
){
865 descptr
->skb_ptr
= dev_alloc_skb(MAX_BUF_SIZE
);
866 if (!descptr
->skb_ptr
) break;
867 descptr
->buf
= cpu_to_le32(pci_map_single(lp
->pdev
, descptr
->skb_ptr
->tail
, MAX_BUF_SIZE
, PCI_DMA_FROMDEVICE
));
868 descptr
->status
= 0x8000;
869 descptr
= descptr
->vndescp
;
871 outw(lp
->mcr0
| 0x0002, ioaddr
); //Trigger Rx DMA
873 lp
->rx_insert_ptr
= descptr
;
876 /* Status of PHY CHIP */
877 static int phy_mode_chk(struct net_device
*dev
)
879 struct r6040_private
*lp
= dev
->priv
;
880 int ioaddr
= dev
->base_addr
, phy_dat
;
882 RDC_DBUG("phy_mode_chk()", 0);
884 /* PHY Link Status Check */
885 phy_dat
= phy_read(ioaddr
, lp
->phy_addr
, 1);
886 if (!(phy_dat
& 0x4)) return 0x8000; /* Link Failed, full duplex */
888 /* PHY Chip Auto-Negotiation Status */
889 phy_dat
= phy_read(ioaddr
, lp
->phy_addr
, 1);
890 if (phy_dat
& 0x0020) {
891 /* Auto Negotiation Mode */
892 phy_dat
= phy_read(ioaddr
, lp
->phy_addr
, 5);
893 phy_dat
&= phy_read(ioaddr
, lp
->phy_addr
, 4);
894 if (phy_dat
& 0x140) phy_dat
= 0x8000;
898 phy_dat
= phy_read(ioaddr
, lp
->phy_addr
, 0);
899 if (phy_dat
& 0x100) phy_dat
= 0x8000;
900 else phy_dat
= 0x0000;
906 /* Read a word data from PHY Chip */
907 static int phy_read(int ioaddr
, int phy_addr
, int reg_idx
)
911 RDC_DBUG("phy_read()", 0);
912 outw(0x2000 + reg_idx
+ (phy_addr
<< 8), ioaddr
+ 0x20);
913 do{}while( (i
++ < 2048) && (inw(ioaddr
+ 0x20) & 0x2000) );
915 return inw(ioaddr
+ 0x24);
918 /* Write a word data from PHY Chip */
919 static void phy_write(int ioaddr
, int phy_addr
, int reg_idx
, int dat
)
923 RDC_DBUG("phy_write()", 0);
924 outw(dat
, ioaddr
+ 0x28);
925 outw(0x4000 + reg_idx
+ (phy_addr
<< 8), ioaddr
+ 0x20);
926 do{}while( (i
++ < 2048) && (inw(ioaddr
+ 0x20) & 0x4000) );
933 static struct pci_device_id r6040_pci_tbl
[] = {
934 {0x17F3, 0x6040, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, RDC_6040
},
935 //{0x1106, 0x3065, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RDC_6040},
936 {0,} /* terminate list */
938 MODULE_DEVICE_TABLE(pci
, r6040_pci_tbl
);
940 static struct pci_driver r6040_driver
= {
942 .id_table
= r6040_pci_tbl
,
943 .probe
= r6040_init_one
,
944 .remove
= __devexit_p(r6040_remove_one
),
948 static int __init
r6040_init (void)
950 RDC_DBUG("r6040_init()", 0);
955 return pci_module_init (&r6040_driver
);
959 static void __exit
r6040_cleanup (void)
961 RDC_DBUG("r6040_cleanup()", 0);
962 pci_unregister_driver (&r6040_driver
);
965 module_init(r6040_init
);
966 module_exit(r6040_cleanup
);
971 * 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`"