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
);
620 static int netdev_ioctl (struct net_device
*dev
, struct ifreq
*rq
, int cmd
)
622 struct r6040_private
*lp
= dev
->priv
;
624 RDC_DBUG("netdev_ioctl()", 0);
625 if (lp
->switch_sig
== 0x0243 && cmd
== SIOCDEVPRIVATE
)
627 unsigned long *data
= (unsigned long *)rq
->ifr_data
, args
[4];
628 int ioaddr
= dev
->base_addr
;
631 data
= (unsigned long *)rq
->ifr_data
;
632 if (copy_from_user(args
, data
, 4*sizeof(unsigned long)))
636 if(args
[0]&(1<<31))phy_write(ioaddr
,29,19,(phy_read(ioaddr
,29,19)|0x2000)); /* port 0 */
637 if(args
[0]&(1<<29))phy_write(ioaddr
,29,19,(phy_read(ioaddr
,29,19)|0x0020)); /* port 1 */
638 if(args
[0]&(1<<27))phy_write(ioaddr
,29,20,(phy_read(ioaddr
,29,20)|0x2000)); /* port 2 */
639 if(args
[0]&(1<<25))phy_write(ioaddr
,29,20,(phy_read(ioaddr
,29,20)|0x0020)); /* port 3 */
646 Stop RDC MAC and Free the allocated resource
648 static void r6040_down(struct net_device
*dev
)
650 struct r6040_private
*lp
= dev
->priv
;
652 int ioaddr
= dev
->base_addr
;
654 RDC_DBUG("r6040_down()", 0);
657 outw(0x0000, ioaddr
+ 0x40); /* Mask Off Interrupt */
658 outw(0x0001, ioaddr
+ 0x04); /* Reset RDC MAC */
660 do{}while((i
++ < 2048) && (inw(ioaddr
+ 0x04) & 0x1));
662 free_irq(dev
->irq
, dev
);
665 for (i
= 0; i
< RX_DCNT
; i
++) {
666 if (lp
->rx_insert_ptr
->skb_ptr
) {
667 pci_unmap_single(lp
->pdev
, lp
->rx_insert_ptr
->buf
, MAX_BUF_SIZE
, PCI_DMA_FROMDEVICE
);
668 dev_kfree_skb(lp
->rx_insert_ptr
->skb_ptr
);
669 lp
->rx_insert_ptr
->skb_ptr
= 0;
671 lp
->rx_insert_ptr
= lp
->rx_insert_ptr
->vndescp
;
675 for (i
= 0; i
< TX_DCNT
; i
++) {
676 if (lp
->tx_insert_ptr
->skb_ptr
) {
677 pci_unmap_single(lp
->pdev
, lp
->tx_insert_ptr
->buf
, MAX_BUF_SIZE
, PCI_DMA_TODEVICE
);
678 dev_kfree_skb(lp
->tx_insert_ptr
->skb_ptr
);
679 lp
->rx_insert_ptr
->skb_ptr
= 0;
681 lp
->tx_insert_ptr
= lp
->tx_insert_ptr
->vndescp
;
684 /* Free Descriptor memory */
685 pci_free_consistent(lp
->pdev
, ALLOC_DESC_SIZE
, lp
->desc_pool
, lp
->desc_dma
);
690 #ifdef CONFIG_R6040_NAPI
691 static int r6040_poll(struct net_device
*dev
, int *budget
)
693 struct r6040_private
*lp
;
694 struct r6040_descriptor
*descptr
;
695 struct sk_buff
*skb_ptr
;
700 ioaddr
= dev
->base_addr
;
701 lp
= (struct r6040_private
*)dev
->priv
;
702 unsigned long rx_work
= dev
->quota
;
707 /* TX interrupt request */
708 if (NAPI_status
& 0x10) {
710 descptr
= lp
->tx_remove_ptr
;
711 while(lp
->tx_free_desc
< TX_DCNT
) {
712 if (descptr
->status
& 0x8000) break; /* Not complte */
713 skb_ptr
= descptr
->skb_ptr
;
714 pci_unmap_single(lp
->pdev
, descptr
->buf
, skb_ptr
->len
, PCI_DMA_TODEVICE
);
715 dev_kfree_skb_irq(skb_ptr
); /* Free buffer */
716 descptr
->skb_ptr
= 0;
717 descptr
= descptr
->vndescp
; /* To next descriptor */
720 lp
->tx_remove_ptr
= descptr
;
721 if (lp
->tx_free_desc
) netif_wake_queue(dev
);
725 /* RX interrupt request */
726 if (NAPI_status
& 0x01) {
728 descptr
= lp
->rx_remove_ptr
;
729 while(lp
->rx_free_desc
) {
730 if (descptr
->status
& 0x8000) break; /* No Rx packet */
731 skb_ptr
= descptr
->skb_ptr
;
732 descptr
->skb_ptr
= 0;
734 skb_put(skb_ptr
, descptr
->len
- 4);
735 pci_unmap_single(lp
->pdev
, descptr
->buf
, MAX_BUF_SIZE
, PCI_DMA_FROMDEVICE
);
736 skb_ptr
->protocol
= eth_type_trans(skb_ptr
, dev
);
737 netif_receive_skb(skb_ptr
); /* Send to upper layer */
738 lp
->stats
.rx_packets
++;
739 lp
->stats
.rx_bytes
+= descptr
->len
;
740 descptr
= descptr
->vndescp
; /* To next descriptor */
743 lp
->rx_remove_ptr
= descptr
;
746 /* Allocate new RX buffer */
747 if (lp
->rx_free_desc
< RX_DCNT
) rx_buf_alloc(lp
,dev
);
750 netif_rx_complete(dev
);
751 outw(R6040_INT_MASK
,ioaddr
+ 0x40);
759 static void r6040_up(struct net_device
*dev
)
761 struct r6040_private
*lp
= dev
->priv
;
762 struct r6040_descriptor
*descptr
;
764 int ioaddr
= dev
->base_addr
;
766 dma_addr_t desc_dma
, start_dma
;
768 RDC_DBUG("r6040_up()", 0);
771 lp
->tx_free_desc
= TX_DCNT
;
772 lp
->rx_free_desc
= 0;
774 /* Init descriptor */
775 memset(lp
->desc_pool
, 0, ALLOC_DESC_SIZE
); /* Let all descriptor = 0 */
776 lp
->tx_insert_ptr
= (struct r6040_descriptor
*)lp
->desc_pool
;
777 lp
->tx_remove_ptr
= lp
->tx_insert_ptr
;
778 lp
->rx_insert_ptr
= (struct r6040_descriptor
*)lp
->tx_insert_ptr
+TX_DCNT
;
779 lp
->rx_remove_ptr
= lp
->rx_insert_ptr
;
781 /* Init TX descriptor */
782 descptr
= lp
->tx_insert_ptr
;
783 desc_dma
= lp
->desc_dma
;
784 start_dma
= desc_dma
;
785 for (i
= 0; i
< TX_DCNT
; i
++) {
786 descptr
->ndesc
= cpu_to_le32(desc_dma
+ sizeof(struct r6040_descriptor
));
787 descptr
->vndescp
= (descptr
+ 1);
788 descptr
= (descptr
+ 1);
789 desc_dma
+= sizeof(struct r6040_descriptor
);
791 (descptr
- 1)->ndesc
= cpu_to_le32(start_dma
);
792 (descptr
- 1)->vndescp
= lp
->tx_insert_ptr
;
794 /* Init RX descriptor */
795 start_dma
= desc_dma
;
796 descptr
= lp
->rx_insert_ptr
;
797 for (i
= 0; i
< RX_DCNT
; i
++) {
798 descptr
->ndesc
= cpu_to_le32(desc_dma
+ sizeof(struct r6040_descriptor
));
799 descptr
->vndescp
= (descptr
+ 1);
800 descptr
= (descptr
+ 1);
801 desc_dma
+= sizeof(struct r6040_descriptor
);
803 (descptr
- 1)->ndesc
= cpu_to_le32(start_dma
);
804 (descptr
- 1)->vndescp
= lp
->rx_insert_ptr
;
806 /* Allocate buffer for RX descriptor */
807 rx_buf_alloc(lp
,dev
);
810 descptr
= lp
->tx_insert_ptr
;
811 for (i
= 0; i
< TX_DCNT
; i
++) {
812 printk("%08lx:%04x %04x %08lx %08lx %08lx %08lx\n", descptr
, descptr
->status
, descptr
->len
, descptr
->buf
, descptr
->skb_ptr
, descptr
->ndesc
, descptr
->vndescp
);
813 descptr
= descptr
->vndescp
;
815 descptr
= lp
->rx_insert_ptr
;
816 for (i
= 0; i
< RX_DCNT
; i
++) {
817 printk("%08lx:%04x %04x %08lx %08lx %08lx %08lx\n", descptr
, descptr
->status
, descptr
->len
, descptr
->buf
, descptr
->skb_ptr
, descptr
->ndesc
, descptr
->vndescp
);
818 descptr
= descptr
->vndescp
;
822 /* MAC operation register */
823 outw(0x01, ioaddr
+0x04); /* Reset MAC */
824 outw(2 , ioaddr
+0xAC); /* Reset internal state machine */
825 outw(0 , ioaddr
+0xAC);
828 /* TX and RX descriptor start Register */
829 tmp_addr
= cpu_to_le32(lp
->tx_insert_ptr
);
830 tmp_addr
= virt_to_bus((volatile void *)tmp_addr
);
831 outw((u16
) tmp_addr
, ioaddr
+0x2c);
832 outw(tmp_addr
>> 16, ioaddr
+0x30);
833 tmp_addr
= cpu_to_le32(lp
->rx_insert_ptr
);
834 tmp_addr
= virt_to_bus((volatile void *)tmp_addr
);
835 outw((u16
) tmp_addr
, ioaddr
+0x34);
836 outw(tmp_addr
>> 16, ioaddr
+0x38);
838 /* Buffer Size Register */
839 outw(MAX_BUF_SIZE
, ioaddr
+0x18);
841 if ((lp
->switch_sig
= phy_read(ioaddr
, 0, 2)) == 0x0243) // ICPlus IP175C Signature
843 phy_write(ioaddr
, 29,31, 0x175C); //Enable registers
844 lp
->phy_mode
= 0x8000;
847 phy_write(ioaddr
, lp
->phy_addr
, 4, PHY_CAP
);
848 phy_write(ioaddr
, lp
->phy_addr
, 0, PHY_MODE
);
850 if (PHY_MODE
== 0x3100)
851 lp
->phy_mode
= phy_mode_chk(dev
);
853 lp
->phy_mode
= (PHY_MODE
& 0x0100) ? 0x8000:0x0;
855 /* MAC Bus Control Register */
856 outw(MBCR_DEFAULT
, ioaddr
+0x8);
858 /* MAC TX/RX Enable */
859 lp
->mcr0
|= lp
->phy_mode
;
860 outw(lp
->mcr0
, ioaddr
);
862 /* set interrupt waiting time and packet numbers */
863 outw(0x0802, ioaddr
+ 0x0C);
864 outw(0x0802, ioaddr
+ 0x10);
866 /* upgrade performance (by RDC guys) */
867 phy_write(ioaddr
,30,17,(phy_read(ioaddr
,30,17)|0x4000)); //bit 14=1
868 phy_write(ioaddr
,30,17,~((~phy_read(ioaddr
,30,17))|0x2000)); //bit 13=0
869 phy_write(ioaddr
,0,19,0x0000);
870 phy_write(ioaddr
,0,30,0x01F0);
872 /* Interrupt Mask Register */
873 outw(R6040_INT_MASK
, ioaddr
+ 0x40);
877 A periodic timer routine
878 Polling PHY Chip Link Status
880 static void r6040_timer(unsigned long data
)
882 struct net_device
*dev
=(struct net_device
*)data
;
883 struct r6040_private
*lp
= dev
->priv
;
884 u16 ioaddr
= dev
->base_addr
, phy_mode
;
886 RDC_DBUG("r6040_timer()", 0);
888 /* Polling PHY Chip Status */
889 if (PHY_MODE
== 0x3100)
890 phy_mode
= phy_mode_chk(dev
);
891 else phy_mode
= (PHY_MODE
& 0x0100) ? 0x8000:0x0;
893 if (phy_mode
!= lp
->phy_mode
) {
894 lp
->phy_mode
= phy_mode
;
895 lp
->mcr0
= (lp
->mcr0
& 0x7fff) | phy_mode
;
896 outw(lp
->mcr0
, ioaddr
);
897 printk("<RDC> Link Change %x \n", inw(ioaddr
));
901 // printk("<RDC> Timer: CR0 %x CR40 %x CR3C %x\n", inw(ioaddr), inw(ioaddr+0x40), inw(ioaddr+0x3c));
903 /* Timer active again */
904 lp
->timer
.expires
= TIMER_WUT
;
905 add_timer(&lp
->timer
);
908 /* Allocate skb buffer for rx descriptor */
909 static void rx_buf_alloc(struct r6040_private
*lp
,struct net_device
*dev
)
911 struct r6040_descriptor
*descptr
;
912 int ioaddr
= dev
->base_addr
;
914 RDC_DBUG("rx_buf_alloc()", 0);
915 descptr
= lp
->rx_insert_ptr
;
916 while(lp
->rx_free_desc
< RX_DCNT
){
917 descptr
->skb_ptr
= dev_alloc_skb(MAX_BUF_SIZE
);
918 if (!descptr
->skb_ptr
) break;
919 descptr
->buf
= cpu_to_le32(pci_map_single(lp
->pdev
, descptr
->skb_ptr
->tail
, MAX_BUF_SIZE
, PCI_DMA_FROMDEVICE
));
920 descptr
->status
= 0x8000;
921 descptr
= descptr
->vndescp
;
923 outw(lp
->mcr0
| 0x0002, ioaddr
); //Trigger Rx DMA
925 lp
->rx_insert_ptr
= descptr
;
928 /* Status of PHY CHIP */
929 static int phy_mode_chk(struct net_device
*dev
)
931 struct r6040_private
*lp
= dev
->priv
;
932 int ioaddr
= dev
->base_addr
, phy_dat
;
934 RDC_DBUG("phy_mode_chk()", 0);
936 /* PHY Link Status Check */
937 phy_dat
= phy_read(ioaddr
, lp
->phy_addr
, 1);
938 if (!(phy_dat
& 0x4)) return 0x8000; /* Link Failed, full duplex */
940 /* PHY Chip Auto-Negotiation Status */
941 phy_dat
= phy_read(ioaddr
, lp
->phy_addr
, 1);
942 if (phy_dat
& 0x0020) {
943 /* Auto Negotiation Mode */
944 phy_dat
= phy_read(ioaddr
, lp
->phy_addr
, 5);
945 phy_dat
&= phy_read(ioaddr
, lp
->phy_addr
, 4);
946 if (phy_dat
& 0x140) phy_dat
= 0x8000;
950 phy_dat
= phy_read(ioaddr
, lp
->phy_addr
, 0);
951 if (phy_dat
& 0x100) phy_dat
= 0x8000;
952 else phy_dat
= 0x0000;
958 /* Read a word data from PHY Chip */
959 static int phy_read(int ioaddr
, int phy_addr
, int reg_idx
)
963 RDC_DBUG("phy_read()", 0);
964 outw(0x2000 + reg_idx
+ (phy_addr
<< 8), ioaddr
+ 0x20);
965 do{}while( (i
++ < 2048) && (inw(ioaddr
+ 0x20) & 0x2000) );
967 return inw(ioaddr
+ 0x24);
970 /* Write a word data from PHY Chip */
971 static void phy_write(int ioaddr
, int phy_addr
, int reg_idx
, int dat
)
975 RDC_DBUG("phy_write()", 0);
976 outw(dat
, ioaddr
+ 0x28);
977 outw(0x4000 + reg_idx
+ (phy_addr
<< 8), ioaddr
+ 0x20);
978 do{}while( (i
++ < 2048) && (inw(ioaddr
+ 0x20) & 0x4000) );
985 static struct pci_device_id r6040_pci_tbl
[] = {
986 {0x17F3, 0x6040, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, RDC_6040
},
987 //{0x1106, 0x3065, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RDC_6040},
988 {0,} /* terminate list */
990 MODULE_DEVICE_TABLE(pci
, r6040_pci_tbl
);
992 static struct pci_driver r6040_driver
= {
994 .id_table
= r6040_pci_tbl
,
995 .probe
= r6040_init_one
,
996 .remove
= __devexit_p(r6040_remove_one
),
1000 static int __init
r6040_init (void)
1002 RDC_DBUG("r6040_init()", 0);
1005 printed_version
= 1;
1009 struct net_device
*the_parent
= dev_get_by_name(parent
);
1011 if (the_parent
== NULL
)
1013 printk (KERN_ERR DRV_NAME
": Unknown device \"%s\" specified.\n", parent
);
1016 memcpy((u8
*)&adr_table
[0][0], the_parent
->dev_addr
, 6);
1017 memcpy((u8
*)&adr_table
[1][0], the_parent
->dev_addr
, 6);
1018 ++*(u8
*)&adr_table
[0][5];
1020 return pci_register_driver (&r6040_driver
);
1024 static void __exit
r6040_cleanup (void)
1026 RDC_DBUG("r6040_cleanup()", 0);
1027 pci_unregister_driver (&r6040_driver
);
1030 module_init(r6040_init
);
1031 module_exit(r6040_cleanup
);
1036 * 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`"