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 10-07-2007 Clean up the driver using checkpatch
17 08-24-2006 Support at linux 2.6.10 above
18 03-24-2006 Support NAPI
19 03-21-2006 By Charies,change spin_lock_irqsave(lp->lock, flags)
20 to spin_lock_irqsave(&lp->lock, flags)
22 03-15-2006 Modify the set_multicast_list ,due to when re-plug the ethernet,
23 it will forget the previous setting
24 07-12-2005 Tim, modify the set_multicast_list
25 03-28-2005 Tim, modify some error mac register offset in
26 function set_multicast_list
27 03-27-2005 Tim, Add the internal state machine reset
28 Sten, If multicast address more than 4, enter PROM mode
30 12-22-2004 Sten Init MAC MBCR register=0x012A
34 1. If multicast address more than 4, use the multicast address hash
37 #define DRV_NAME "r6040"
38 #define DRV_VERSION "0.13"
39 #define DRV_RELDATE "24Aug2006"
41 /* PHY CHIP Address */
42 #define PHY1_ADDR 1 /* For MAC1 */
43 #define PHY2_ADDR 2 /* For MAC2 */
44 #define PHY_MODE 0x3100 /* PHY CHIP Register 0 */
45 #define PHY_CAP 0x01E1 /* PHY CHIP Register 4 */
47 /* Time in jiffies before concluding the transmitter is hung. */
48 #define TX_TIMEOUT (6000 * HZ / 1000)
49 #define TIMER_WUT (jiffies + HZ * 1)/* timer wakeup time : 1 second */
52 #define RDC_MAC_ID 0x6040
54 /* RDC MAC I/O Size */
55 #define R6040_IO_SIZE 256
57 /* RDC Chip PCI Command */
58 #define R6040_PCI_CMD 0x0005 /* IO, Master */
64 #define TX_DCNT 0x80 /* TX descriptor count */
65 #define RX_DCNT 0x80 /* RX descriptor count */
66 #define MAX_BUF_SIZE 0x600
67 #define ALLOC_DESC_SIZE ((TX_DCNT+RX_DCNT)*sizeof(struct r6040_descriptor)+0x10)
68 #define MBCR_DEFAULT 0x012A /* MAC Bus Control Register */
71 #define ICPLUS_PHY_ID 0x0243
73 /* Debug enable or not */
77 #define RDC_DBUG(msg, value) printk(KERN_ERR "%s %x\n", msg, value);
79 #define RDC_DBUG(msg, value)
83 #include <linux/module.h>
84 #include <linux/version.h>
85 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
86 #include <linux/moduleparam.h>
88 #include <linux/kernel.h>
89 #include <linux/string.h>
90 #include <linux/timer.h>
91 #include <linux/errno.h>
92 #include <linux/ioport.h>
93 #include <linux/slab.h>
94 #include <linux/interrupt.h>
95 #include <linux/pci.h>
96 #include <linux/netdevice.h>
97 #include <linux/etherdevice.h>
98 #include <linux/skbuff.h>
99 #include <linux/init.h>
100 #include <linux/delay.h>
101 #include <linux/mii.h>
102 #include <linux/ethtool.h>
103 #include <linux/crc32.h>
104 #include <linux/spinlock.h>
106 #include <asm/processor.h>
107 #include <asm/bitops.h>
110 #include <asm/uaccess.h>
112 MODULE_AUTHOR("Sten Wang <sten.wang@rdc.com.tw>, Daniel Gimpelevich <daniel@gimpelevich.san-francisco.ca.us>, Florian Fainelli <florian@openwrt.org>");
113 MODULE_LICENSE("GPL");
114 #ifdef CONFIG_R6040_NAPI
115 MODULE_DESCRIPTION("RDC R6040 NAPI PCI FastEthernet Driver");
117 MODULE_DESCRIPTION("RDC R6040 PCI FastEthernet Driver");
120 #define R6040_INT_MASK 0x0011
122 struct r6040_descriptor
{
123 u16 status
, len
; /* 0-3 */
127 char *vbufp
; /* 10-13 */
128 struct r6040_descriptor
*vndescp
; /* 14-17 */
129 struct sk_buff
*skb_ptr
; /* 18-1B */
130 u32 rev2
; /* 1C-1F */
131 } __attribute__((aligned(32)));
133 struct r6040_private
{
134 struct net_device_stats stats
;
135 spinlock_t lock
; /* driver lock */
136 struct timer_list timer
;
137 struct pci_dev
*pdev
;
139 struct r6040_descriptor
*rx_insert_ptr
;
140 struct r6040_descriptor
*rx_remove_ptr
;
141 struct r6040_descriptor
*tx_insert_ptr
;
142 struct r6040_descriptor
*tx_remove_ptr
;
143 u16 tx_free_desc
, rx_free_desc
, phy_addr
, phy_mode
;
150 struct r6040_chip_info
{
157 #ifdef CONFIG_R6040_NAPI
158 static int NAPI_status
;
161 static int __devinitdata printed_version
;
162 #ifdef CONFIG_R6040_NAPI
163 static char version
[] __devinitdata
=
164 KERN_INFO DRV_NAME
": RDC R6040 NAPI net driver, version "DRV_VERSION
" (" DRV_RELDATE
")\n";
166 static char version
[] __devinitdata
=
167 KERN_INFO DRV_NAME
": RDC R6040 net driver, version "DRV_VERSION
" (" DRV_RELDATE
")\n";
169 static struct r6040_chip_info r6040_chip_info
[] __devinitdata
=
171 { "RDC R6040 Knight", R6040_PCI_CMD
, R6040_IO_SIZE
, 0}
175 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
176 static int NUM_MAC_TABLE
= 2 ;
177 module_param(parent
, charp
, 0444);
179 MODULE_PARM(parent
, "s");
181 MODULE_PARM_DESC(parent
, "Parent network device name");
183 static int phy_table
[] = { 0x1, 0x2};
184 static u8 adr_table
[2][8] = {
185 {0x00, 0x00, 0x60, 0x00, 0x00, 0x01},
186 {0x00, 0x00, 0x60, 0x00, 0x00, 0x02}
189 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 10)
190 module_param_array(adr_table
, int, &NUM_MAC_TABLE
, 0644);
191 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
192 module_param_array(adr_table
, int, NUM_MAC_TABLE
, 0644);
194 MODULE_PARM(adr_table
, "2-4i");
196 MODULE_PARM_DESC(adr_table
, "MAC Address (assigned)");
198 static int r6040_open(struct net_device
*dev
);
199 static int r6040_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
);
200 static irqreturn_t
r6040_interrupt(int irq
, void *dev_id
);
201 static struct net_device_stats
*r6040_get_stats(struct net_device
*dev
);
202 static int r6040_close(struct net_device
*dev
);
203 static void set_multicast_list(struct net_device
*dev
);
204 static struct ethtool_ops netdev_ethtool_ops
;
205 static int netdev_ioctl (struct net_device
*dev
, struct ifreq
*rq
, int cmd
);
206 static void r6040_down(struct net_device
*dev
);
207 static void r6040_up(struct net_device
*dev
);
208 static void r6040_tx_timeout (struct net_device
*dev
);
209 static void r6040_timer(unsigned long);
211 static int phy_mode_chk(struct net_device
*dev
);
212 static int phy_read(int ioaddr
, int phy_adr
, int reg_idx
);
213 static void phy_write(int ioaddr
, int phy_adr
, int reg_idx
, int dat
);
214 static void rx_buf_alloc(struct r6040_private
*lp
, struct net_device
*dev
);
215 #ifdef CONFIG_R6040_NAPI
216 static int r6040_poll(struct net_device
*netdev
, int *budget
);
220 static int __devinit
r6040_init_one (struct pci_dev
*pdev
,
221 const struct pci_device_id
*ent
)
223 struct net_device
*dev
;
224 struct r6040_private
*lp
;
225 int ioaddr
, io_size
, err
;
226 static int card_idx
= -1;
227 int chip_id
= (int)ent
->driver_data
;
229 RDC_DBUG("r6040_init_one()", 0);
231 if (printed_version
++)
232 printk(KERN_INFO
"%s\n", version
);
234 err
= pci_enable_device(pdev
);
238 /* this should always be supported */
239 if (pci_set_dma_mask(pdev
, 0xffffffff)) {
240 printk(KERN_ERR DRV_NAME
"32-bit PCI DMA addresses not supported by the card!?\n");
245 io_size
= r6040_chip_info
[chip_id
].io_size
;
246 if (pci_resource_len (pdev
, 0) < io_size
) {
250 ioaddr
= pci_resource_start (pdev
, 0); /* IO map base address */
251 pci_set_master(pdev
);
253 dev
= alloc_etherdev(sizeof(struct r6040_private
));
256 SET_MODULE_OWNER(dev
);
258 if (pci_request_regions(pdev
, DRV_NAME
)) {
259 printk(KERN_ERR DRV_NAME
": Failed to request PCI regions\n");
261 goto err_out_disable
;
264 /* Init system & device */
266 dev
->base_addr
= ioaddr
;
267 dev
->irq
= pdev
->irq
;
269 spin_lock_init(&lp
->lock
);
270 pci_set_drvdata(pdev
, dev
);
272 /* Set MAC address */
274 memcpy(dev
->dev_addr
, (u8
*)&adr_table
[card_idx
][0], 6);
276 /* Link new device into r6040_root_dev */
279 /* Init RDC private data */
281 lp
->phy_addr
= phy_table
[card_idx
];
284 /* The RDC-specific entries in the device structure. */
285 dev
->open
= &r6040_open
;
286 dev
->hard_start_xmit
= &r6040_start_xmit
;
287 dev
->stop
= &r6040_close
;
288 dev
->get_stats
= &r6040_get_stats
;
289 dev
->set_multicast_list
= &set_multicast_list
;
290 dev
->do_ioctl
= &netdev_ioctl
;
291 dev
->ethtool_ops
= &netdev_ethtool_ops
;
292 dev
->tx_timeout
= &r6040_tx_timeout
;
293 dev
->watchdog_timeo
= TX_TIMEOUT
;
294 #ifdef CONFIG_R6040_NAPI
295 dev
->poll
= &r6040_poll
;
299 /* Register net device. After this dev->name assign */
300 err
= register_netdev(dev
);
302 printk(KERN_ERR DRV_NAME
": Failed to register net device\n");
306 netif_carrier_on(dev
);
310 pci_release_regions(pdev
);
312 pci_disable_device(pdev
);
313 pci_set_drvdata(pdev
, NULL
);
319 static void __devexit
r6040_remove_one (struct pci_dev
*pdev
)
321 struct net_device
*dev
= pci_get_drvdata(pdev
);
323 unregister_netdev(dev
);
324 pci_release_regions(pdev
);
326 pci_disable_device(pdev
);
327 pci_set_drvdata(pdev
, NULL
);
331 r6040_open(struct net_device
*dev
)
333 struct r6040_private
*lp
= dev
->priv
;
336 RDC_DBUG("r6040_open()", 0);
338 /* Request IRQ and Register interrupt handler */
339 i
= request_irq(dev
->irq
, &r6040_interrupt
, SA_SHIRQ
, dev
->name
, dev
);
342 /* Allocate Descriptor memory */
343 lp
->desc_pool
= pci_alloc_consistent(lp
->pdev
, ALLOC_DESC_SIZE
, &lp
->desc_dma
);
349 netif_start_queue(dev
);
351 if (lp
->switch_sig
!= ICPLUS_PHY_ID
) {
352 /* set and active a timer process */
353 init_timer(&lp
->timer
);
354 lp
->timer
.expires
= TIMER_WUT
;
355 lp
->timer
.data
= (unsigned long)dev
;
356 lp
->timer
.function
= &r6040_timer
;
357 add_timer(&lp
->timer
);
363 r6040_tx_timeout (struct net_device
*dev
)
365 struct r6040_private
*lp
= dev
->priv
;
366 /* int ioaddr = dev->base_addr;
367 struct r6040_descriptor *descptr = lp->tx_remove_ptr; */
369 RDC_DBUG("r6040_tx_timeout()", 0);
371 /* Transmitter timeout, serious problems. */
372 /* Sten: Nothing need to do so far. */
373 printk(KERN_ERR DRV_NAME
": Big Trobule, transmit timeout/n");
374 lp
->stats
.tx_errors
++;
375 netif_stop_queue(dev
);
377 //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));
379 //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);
384 r6040_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
386 struct r6040_private
*lp
= dev
->priv
;
387 struct r6040_descriptor
*descptr
;
388 int ioaddr
= dev
->base_addr
;
391 RDC_DBUG("r6040_start_xmit()", 0);
393 if (skb
== NULL
) /* NULL skb directly return */
395 if (skb
->len
>= MAX_BUF_SIZE
) { /* Packet too long, drop it */
400 /* Critical Section */
401 spin_lock_irqsave(&lp
->lock
, flags
);
403 /* TX resource check */
404 if (!lp
->tx_free_desc
) {
405 spin_unlock_irqrestore(&lp
->lock
, flags
);
406 printk(KERN_ERR DRV_NAME
": NO TX DESC ");
410 /* Statistic Counter */
411 lp
->stats
.tx_packets
++;
412 lp
->stats
.tx_bytes
+= skb
->len
;
414 /* Set TX descriptor & Transmit it */
416 descptr
= lp
->tx_insert_ptr
;
420 descptr
->len
= skb
->len
;
422 descptr
->skb_ptr
= skb
;
423 descptr
->buf
= cpu_to_le32(pci_map_single(lp
->pdev
, skb
->data
, skb
->len
, PCI_DMA_TODEVICE
));
424 descptr
->status
= 0x8000;
425 outw(0x01, ioaddr
+ 0x14);
426 lp
->tx_insert_ptr
= descptr
->vndescp
;
429 printk("Xmit(): %08lx:%04x %04x %08lx %08lx %08lx %08lx\n", descptr
, descptr
->status
, descptr
->len
, descptr
->buf
, descptr
->skb_ptr
, descptr
->ndesc
, descptr
->vndescp
);
432 /* If no tx resource, stop */
433 if (!lp
->tx_free_desc
)
434 netif_stop_queue(dev
);
436 dev
->trans_start
= jiffies
;
437 spin_unlock_irqrestore(&lp
->lock
, flags
);
441 /* The RDC interrupt handler. */
443 r6040_interrupt(int irq
, void *dev_id
)
445 struct net_device
*dev
= dev_id
;
446 struct r6040_private
*lp
;
447 struct r6040_descriptor
*descptr
;
448 struct sk_buff
*skb_ptr
;
451 #ifdef CONFIG_R6040_NAPI
457 RDC_DBUG("r6040_interrupt()", 0);
459 printk (KERN_ERR DRV_NAME
": INT() unknown device.\n");
460 return IRQ_RETVAL(handled
);
463 lp
= (struct r6040_private
*)dev
->priv
;
464 spin_lock_irqsave(&lp
->lock
, flags
);
466 /* Check MAC Interrupt status */
467 ioaddr
= dev
->base_addr
;
468 outw(0x0, ioaddr
+ 0x40); /* Mask Off RDC MAC interrupt */
469 status
= inw(ioaddr
+ 0x3c); /* Read INTR status and clear */
472 #ifdef CONFIG_R6040_NAPI
473 if (netif_rx_schedule_prep(dev
)) {
474 NAPI_status
= status
;
475 __netif_rx_schedule(dev
);
478 spin_unlock_irqrestore(&lp
->lock
, flags
);
479 return IRQ_RETVAL(handled
);
481 /* TX interrupt request */
484 descptr
= lp
->tx_remove_ptr
;
485 while (lp
->tx_free_desc
< TX_DCNT
) {
486 if (descptr
->status
& 0x8000)
487 break; /* Not complte */
488 skb_ptr
= descptr
->skb_ptr
;
489 pci_unmap_single(lp
->pdev
, descptr
->buf
, skb_ptr
->len
, PCI_DMA_TODEVICE
);
490 dev_kfree_skb_irq(skb_ptr
); /* Free buffer */
491 descptr
->skb_ptr
= 0;
492 descptr
= descptr
->vndescp
; /* To next descriptor */
495 lp
->tx_remove_ptr
= descptr
;
496 if (lp
->tx_free_desc
)
497 netif_wake_queue(dev
);
500 /* RX interrupt request */
503 descptr
= lp
->rx_remove_ptr
;
504 while(lp
->rx_free_desc
) {
505 if (descptr
->status
& 0x8000) break; /* No Rx packet */
506 skb_ptr
= descptr
->skb_ptr
;
507 descptr
->skb_ptr
= 0;
509 skb_put(skb_ptr
, descptr
->len
- 4);
510 pci_unmap_single(lp
->pdev
, descptr
->buf
, MAX_BUF_SIZE
, PCI_DMA_FROMDEVICE
);
511 skb_ptr
->protocol
= eth_type_trans(skb_ptr
, dev
);
512 netif_rx(skb_ptr
); /* Send to upper layer */
513 lp
->stats
.rx_packets
++;
514 lp
->stats
.rx_bytes
+= descptr
->len
;
515 descptr
= descptr
->vndescp
; /* To next descriptor */
518 lp
->rx_remove_ptr
= descptr
;
521 /* Allocate new RX buffer */
522 if (lp
->rx_free_desc
< RX_DCNT
) rx_buf_alloc(lp
,dev
);
524 outw(R6040_INT_MASK
, ioaddr
+ 0x40); /* TX/RX interrupt enable */
525 spin_unlock_irqrestore(&lp
->lock
, flags
);
527 return IRQ_RETVAL(handled
);
531 static struct net_device_stats
*
532 r6040_get_stats(struct net_device
*dev
)
534 struct r6040_private
*lp
= dev
->priv
;
536 RDC_DBUG("r6040_get_stats()", 0);
541 * Set or clear the multicast filter for this adaptor.
544 set_multicast_list(struct net_device
*dev
)
546 struct r6040_private
*lp
= dev
->priv
;
547 struct dev_mc_list
*mcptr
;
548 int ioaddr
= dev
->base_addr
;
552 RDC_DBUG("set_multicast_list()", 0);
555 adrp
= (u16
*) dev
->dev_addr
;
556 outw(adrp
[0], ioaddr
+ 0x68);
557 outw(adrp
[1], ioaddr
+ 0x6A);
558 outw(adrp
[2], ioaddr
+ 0x6C);
562 printk("MAC ADDR: %04x %04x %04x\n", adrp
[0], adrp
[1], adrp
[2]);
565 /* Promiscous Mode */
566 spin_lock_irqsave(&lp
->lock
, flags
);
567 i
= inw(ioaddr
) & ~0x0120; /* Clear AMCP & PROM */
568 if (dev
->flags
& IFF_PROMISC
) {
572 if (dev
->mc_count
> 4)
573 i
|= 0x0020; /* Too many multicast address */
576 spin_unlock_irqrestore(&lp
->lock
, flags
);
578 /* Multicast Address */
579 if (dev
->mc_count
> 4) /* Wait to do: Hash Table for multicast */
582 /* Multicast Address 1~4 case */
583 for (i
= 0, mcptr
= dev
->mc_list
; (i
< dev
->mc_count
) && (i
< 4); i
++) {
584 adrp
= (u16
*)mcptr
->dmi_addr
;
585 outw(adrp
[0], ioaddr
+ 0x70 + 8*i
);
586 outw(adrp
[1], ioaddr
+ 0x72 + 8*i
);
587 outw(adrp
[2], ioaddr
+ 0x74 + 8*i
);
590 printk("M_ADDR: %04x %04x %04x\n", adrp
[0], adrp
[1], adrp
[2]);
593 for (i
= dev
->mc_count
; i
< 4; i
++) {
594 outw(0xffff, ioaddr
+ 0x68 + 8*i
);
595 outw(0xffff, ioaddr
+ 0x6A + 8*i
);
596 outw(0xffff, ioaddr
+ 0x6C + 8*i
);
600 static void netdev_get_drvinfo (struct net_device
*dev
, struct ethtool_drvinfo
*info
)
602 struct r6040_private
*rp
= dev
->priv
;
604 strcpy (info
->driver
, DRV_NAME
);
605 strcpy (info
->version
, DRV_VERSION
);
606 strcpy (info
->bus_info
, pci_name(rp
->pdev
));
609 static struct ethtool_ops netdev_ethtool_ops
= {
610 .get_drvinfo
= netdev_get_drvinfo
,
614 r6040_close(struct net_device
*dev
)
616 struct r6040_private
*lp
= dev
->priv
;
618 RDC_DBUG("r6040_close()", 0);
621 del_timer_sync(&lp
->timer
);
623 spin_lock_irq(&lp
->lock
);
625 netif_stop_queue(dev
);
629 spin_unlock_irq(&lp
->lock
);
634 static int netdev_ioctl (struct net_device
*dev
, struct ifreq
*rq
, int cmd
)
636 struct r6040_private
*lp
= dev
->priv
;
638 RDC_DBUG("netdev_ioctl()", 0);
640 if (lp
->switch_sig
== ICPLUS_PHY_ID
&& cmd
== SIOCDEVPRIVATE
) {
641 unsigned long *data
= (unsigned long *)rq
->ifr_data
, args
[4];
642 int ioaddr
= dev
->base_addr
;
645 data
= (unsigned long *)rq
->ifr_data
;
646 if (copy_from_user(args
, data
, 4*sizeof(unsigned long)))
650 if(args
[0]&(1<<31))phy_write(ioaddr
, 29, 19, (phy_read(ioaddr
, 29, 19) | 0x2000)); /* port 0 */
651 if(args
[0]&(1<<29))phy_write(ioaddr
, 29, 19, (phy_read(ioaddr
, 29, 19) | 0x0020)); /* port 1 */
652 if(args
[0]&(1<<27))phy_write(ioaddr
, 29, 20, (phy_read(ioaddr
, 29, 20) | 0x2000)); /* port 2 */
653 if(args
[0]&(1<<25))phy_write(ioaddr
, 29, 20, (phy_read(ioaddr
, 29, 20) | 0x0020)); /* port 3 */
660 Stop RDC MAC and Free the allocated resource
662 static void r6040_down(struct net_device
*dev
)
664 struct r6040_private
*lp
= dev
->priv
;
666 int ioaddr
= dev
->base_addr
;
668 RDC_DBUG("r6040_down()", 0);
671 outw(0x0000, ioaddr
+ 0x40); /* Mask Off Interrupt */
672 outw(0x0001, ioaddr
+ 0x04); /* Reset RDC MAC */
674 do{}while((i
++ < 2048) && (inw(ioaddr
+ 0x04) & 0x1));
676 free_irq(dev
->irq
, dev
);
679 for (i
= 0; i
< RX_DCNT
; i
++) {
680 if (lp
->rx_insert_ptr
->skb_ptr
) {
681 pci_unmap_single(lp
->pdev
, lp
->rx_insert_ptr
->buf
, MAX_BUF_SIZE
, PCI_DMA_FROMDEVICE
);
682 dev_kfree_skb(lp
->rx_insert_ptr
->skb_ptr
);
683 lp
->rx_insert_ptr
->skb_ptr
= 0;
685 lp
->rx_insert_ptr
= lp
->rx_insert_ptr
->vndescp
;
689 for (i
= 0; i
< TX_DCNT
; i
++) {
690 if (lp
->tx_insert_ptr
->skb_ptr
) {
691 pci_unmap_single(lp
->pdev
, lp
->tx_insert_ptr
->buf
, MAX_BUF_SIZE
, PCI_DMA_TODEVICE
);
692 dev_kfree_skb(lp
->tx_insert_ptr
->skb_ptr
);
693 lp
->rx_insert_ptr
->skb_ptr
= 0;
695 lp
->tx_insert_ptr
= lp
->tx_insert_ptr
->vndescp
;
698 /* Free Descriptor memory */
699 pci_free_consistent(lp
->pdev
, ALLOC_DESC_SIZE
, lp
->desc_pool
, lp
->desc_dma
);
704 #ifdef CONFIG_R6040_NAPI
705 static int r6040_poll(struct net_device
*dev
, int *budget
)
707 struct r6040_private
*lp
;
708 struct r6040_descriptor
*descptr
;
709 struct sk_buff
*skb_ptr
;
714 ioaddr
= dev
->base_addr
;
715 lp
= (struct r6040_private
*)dev
->priv
;
716 unsigned long rx_work
= dev
->quota
;
719 /* TX interrupt request */
720 if (NAPI_status
& 0x10) {
721 descptr
= lp
->tx_remove_ptr
;
722 while (lp
->tx_free_desc
< TX_DCNT
) {
723 if (descptr
->status
& 0x8000)
724 break; /* Not complte */
726 skb_ptr
= descptr
->skb_ptr
;
727 pci_unmap_single(lp
->pdev
, descptr
->buf
, skb_ptr
->len
, PCI_DMA_TODEVICE
);
728 dev_kfree_skb_irq(skb_ptr
); /* Free buffer */
729 descptr
->skb_ptr
= 0;
730 descptr
= descptr
->vndescp
; /* To next descriptor */
733 lp
->tx_remove_ptr
= descptr
;
734 if (lp
->tx_free_desc
) netif_wake_queue(dev
);
738 /* RX interrupt request */
739 if (NAPI_status
& 0x01) {
740 descptr
= lp
->rx_remove_ptr
;
741 while (lp
->rx_free_desc
) {
742 if (descptr
->status
& 0x8000)
743 break; /* No Rx packet */
744 skb_ptr
= descptr
->skb_ptr
;
745 descptr
->skb_ptr
= 0;
747 skb_put(skb_ptr
, descptr
->len
- 4);
748 pci_unmap_single(lp
->pdev
, descptr
->buf
, MAX_BUF_SIZE
, PCI_DMA_FROMDEVICE
);
749 skb_ptr
->protocol
= eth_type_trans(skb_ptr
, dev
);
750 netif_receive_skb(skb_ptr
); /* Send to upper layer */
751 lp
->stats
.rx_packets
++;
752 lp
->stats
.rx_bytes
+= descptr
->len
;
753 descptr
= descptr
->vndescp
; /* To next descriptor */
756 lp
->rx_remove_ptr
= descptr
;
758 /* Allocate new RX buffer */
759 if (lp
->rx_free_desc
< RX_DCNT
)
760 rx_buf_alloc(lp
, dev
);
763 netif_rx_complete(dev
);
764 outw(R6040_INT_MASK
, ioaddr
+ 0x40);
772 static void r6040_up(struct net_device
*dev
)
774 struct r6040_private
*lp
= dev
->priv
;
775 struct r6040_descriptor
*descptr
;
777 int ioaddr
= dev
->base_addr
;
779 dma_addr_t desc_dma
, start_dma
;
781 RDC_DBUG("r6040_up()", 0);
784 lp
->tx_free_desc
= TX_DCNT
;
785 lp
->rx_free_desc
= 0;
787 /* Init descriptor */
788 memset(lp
->desc_pool
, 0, ALLOC_DESC_SIZE
); /* Let all descriptor = 0 */
789 lp
->tx_insert_ptr
= (struct r6040_descriptor
*)lp
->desc_pool
;
790 lp
->tx_remove_ptr
= lp
->tx_insert_ptr
;
791 lp
->rx_insert_ptr
= (struct r6040_descriptor
*)lp
->tx_insert_ptr
+TX_DCNT
;
792 lp
->rx_remove_ptr
= lp
->rx_insert_ptr
;
794 /* Init TX descriptor */
795 descptr
= lp
->tx_insert_ptr
;
796 desc_dma
= lp
->desc_dma
;
797 start_dma
= desc_dma
;
798 for (i
= 0; i
< TX_DCNT
; i
++) {
799 descptr
->ndesc
= cpu_to_le32(desc_dma
+ sizeof(struct r6040_descriptor
));
800 descptr
->vndescp
= (descptr
+ 1);
801 descptr
= (descptr
+ 1);
802 desc_dma
+= sizeof(struct r6040_descriptor
);
804 (descptr
- 1)->ndesc
= cpu_to_le32(start_dma
);
805 (descptr
- 1)->vndescp
= lp
->tx_insert_ptr
;
807 /* Init RX descriptor */
808 start_dma
= desc_dma
;
809 descptr
= lp
->rx_insert_ptr
;
810 for (i
= 0; i
< RX_DCNT
; i
++) {
811 descptr
->ndesc
= cpu_to_le32(desc_dma
+ sizeof(struct r6040_descriptor
));
812 descptr
->vndescp
= (descptr
+ 1);
813 descptr
= (descptr
+ 1);
814 desc_dma
+= sizeof(struct r6040_descriptor
);
816 (descptr
- 1)->ndesc
= cpu_to_le32(start_dma
);
817 (descptr
- 1)->vndescp
= lp
->rx_insert_ptr
;
819 /* Allocate buffer for RX descriptor */
820 rx_buf_alloc(lp
, dev
);
823 descptr
= lp
->tx_insert_ptr
;
824 for (i
= 0; i
< TX_DCNT
; i
++) {
825 printk("%08lx:%04x %04x %08lx %08lx %08lx %08lx\n", descptr
, descptr
->status
, descptr
->len
, descptr
->buf
, descptr
->skb_ptr
, descptr
->ndesc
, descptr
->vndescp
);
826 descptr
= descptr
->vndescp
;
828 descptr
= lp
->rx_insert_ptr
;
829 for (i
= 0; i
< RX_DCNT
; i
++) {
830 printk("%08lx:%04x %04x %08lx %08lx %08lx %08lx\n", descptr
, descptr
->status
, descptr
->len
, descptr
->buf
, descptr
->skb_ptr
, descptr
->ndesc
, descptr
->vndescp
);
831 descptr
= descptr
->vndescp
;
835 /* MAC operation register */
836 outw(0x01, ioaddr
+0x04); /* Reset MAC */
837 outw(2, ioaddr
+0xAC); /* Reset internal state machine */
838 outw(0, ioaddr
+0xAC);
841 /* TX and RX descriptor start Register */
842 tmp_addr
= cpu_to_le32(lp
->tx_insert_ptr
);
843 tmp_addr
= virt_to_bus((volatile void *)tmp_addr
);
844 outw((u16
) tmp_addr
, ioaddr
+0x2c);
845 outw(tmp_addr
>> 16, ioaddr
+0x30);
846 tmp_addr
= cpu_to_le32(lp
->rx_insert_ptr
);
847 tmp_addr
= virt_to_bus((volatile void *)tmp_addr
);
848 outw((u16
) tmp_addr
, ioaddr
+0x34);
849 outw(tmp_addr
>> 16, ioaddr
+0x38);
851 /* Buffer Size Register */
852 outw(MAX_BUF_SIZE
, ioaddr
+0x18);
854 if ((lp
->switch_sig
= phy_read(ioaddr
, 0, 2)) == ICPLUS_PHY_ID
) {
855 phy_write(ioaddr
, 29,31, 0x175C); /* Enable registers */
856 lp
->phy_mode
= 0x8000;
859 phy_write(ioaddr
, lp
->phy_addr
, 4, PHY_CAP
);
860 phy_write(ioaddr
, lp
->phy_addr
, 0, PHY_MODE
);
862 if (PHY_MODE
== 0x3100)
863 lp
->phy_mode
= phy_mode_chk(dev
);
865 lp
->phy_mode
= (PHY_MODE
& 0x0100) ? 0x8000:0x0;
867 /* MAC Bus Control Register */
868 outw(MBCR_DEFAULT
, ioaddr
+0x8);
870 /* MAC TX/RX Enable */
871 lp
->mcr0
|= lp
->phy_mode
;
872 outw(lp
->mcr0
, ioaddr
);
874 /* set interrupt waiting time and packet numbers */
875 outw(0x0802, ioaddr
+ 0x0C);
876 outw(0x0802, ioaddr
+ 0x10);
878 /* upgrade performance (by RDC guys) */
879 phy_write(ioaddr
, 30, 17, (phy_read(ioaddr
, 30, 17) | 0x4000)); //bit 14=1
880 phy_write(ioaddr
, 30, 17, ~((~phy_read(ioaddr
, 30, 17)) | 0x2000)); //bit 13=0
881 phy_write(ioaddr
, 0, 19, 0x0000);
882 phy_write(ioaddr
, 0, 30, 0x01F0);
884 /* Interrupt Mask Register */
885 outw(R6040_INT_MASK
, ioaddr
+ 0x40);
889 A periodic timer routine
890 Polling PHY Chip Link Status
892 static void r6040_timer(unsigned long data
)
894 struct net_device
*dev
= (struct net_device
*)data
;
895 struct r6040_private
*lp
= dev
->priv
;
896 u16 ioaddr
= dev
->base_addr
, phy_mode
;
898 RDC_DBUG("r6040_timer()", 0);
900 /* Polling PHY Chip Status */
901 if (PHY_MODE
== 0x3100)
902 phy_mode
= phy_mode_chk(dev
);
904 phy_mode
= (PHY_MODE
& 0x0100) ? 0x8000:0x0;
906 if (phy_mode
!= lp
->phy_mode
) {
907 lp
->phy_mode
= phy_mode
;
908 lp
->mcr0
= (lp
->mcr0
& 0x7fff) | phy_mode
;
909 outw(lp
->mcr0
, ioaddr
);
910 printk(KERN_INFO
"Link Change %x \n", inw(ioaddr
));
914 // printk("<RDC> Timer: CR0 %x CR40 %x CR3C %x\n", inw(ioaddr), inw(ioaddr+0x40), inw(ioaddr+0x3c));
916 /* Timer active again */
917 lp
->timer
.expires
= TIMER_WUT
;
918 add_timer(&lp
->timer
);
921 /* Allocate skb buffer for rx descriptor */
922 static void rx_buf_alloc(struct r6040_private
*lp
, struct net_device
*dev
)
924 struct r6040_descriptor
*descptr
;
925 int ioaddr
= dev
->base_addr
;
927 RDC_DBUG("rx_buf_alloc()", 0);
929 descptr
= lp
->rx_insert_ptr
;
930 while (lp
->rx_free_desc
< RX_DCNT
) {
931 descptr
->skb_ptr
= dev_alloc_skb(MAX_BUF_SIZE
);
933 if (!descptr
->skb_ptr
)
935 descptr
->buf
= cpu_to_le32(pci_map_single(lp
->pdev
, descptr
->skb_ptr
->tail
, MAX_BUF_SIZE
, PCI_DMA_FROMDEVICE
));
936 descptr
->status
= 0x8000;
937 descptr
= descptr
->vndescp
;
939 outw(lp
->mcr0
| 0x0002, ioaddr
); /* Trigger Rx DMA */
941 lp
->rx_insert_ptr
= descptr
;
944 /* Status of PHY CHIP */
945 static int phy_mode_chk(struct net_device
*dev
)
947 struct r6040_private
*lp
= dev
->priv
;
948 int ioaddr
= dev
->base_addr
, phy_dat
;
950 RDC_DBUG("phy_mode_chk()", 0);
952 /* PHY Link Status Check */
953 phy_dat
= phy_read(ioaddr
, lp
->phy_addr
, 1);
954 if (!(phy_dat
& 0x4))
955 return 0x8000; /* Link Failed, full duplex */
957 /* PHY Chip Auto-Negotiation Status */
958 phy_dat
= phy_read(ioaddr
, lp
->phy_addr
, 1);
959 if (phy_dat
& 0x0020) {
960 /* Auto Negotiation Mode */
961 phy_dat
= phy_read(ioaddr
, lp
->phy_addr
, 5);
962 phy_dat
&= phy_read(ioaddr
, lp
->phy_addr
, 4);
969 phy_dat
= phy_read(ioaddr
, lp
->phy_addr
, 0);
970 if (phy_dat
& 0x100) phy_dat
= 0x8000;
971 else phy_dat
= 0x0000;
977 /* Read a word data from PHY Chip */
978 static int phy_read(int ioaddr
, int phy_addr
, int reg_idx
)
982 RDC_DBUG("phy_read()", 0);
983 outw(0x2000 + reg_idx
+ (phy_addr
<< 8), ioaddr
+ 0x20);
984 do {} while ((i
++ < 2048) && (inw(ioaddr
+ 0x20) & 0x2000));
986 return inw(ioaddr
+ 0x24);
989 /* Write a word data from PHY Chip */
990 static void phy_write(int ioaddr
, int phy_addr
, int reg_idx
, int dat
)
994 RDC_DBUG("phy_write()", 0);
995 outw(dat
, ioaddr
+ 0x28);
996 outw(0x4000 + reg_idx
+ (phy_addr
<< 8), ioaddr
+ 0x20);
997 do{}while( (i
++ < 2048) && (inw(ioaddr
+ 0x20) & 0x4000) );
1004 static struct pci_device_id r6040_pci_tbl
[] = {
1005 {PCI_VENDOR_ID_RDC
, PCI_DEVICE_ID_RDC_R6040
, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, RDC_6040
},
1006 /*{0x1106, 0x3065, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RDC_6040},*/
1009 MODULE_DEVICE_TABLE(pci
, r6040_pci_tbl
);
1011 static struct pci_driver r6040_driver
= {
1013 .id_table
= r6040_pci_tbl
,
1014 .probe
= r6040_init_one
,
1015 .remove
= __devexit_p(r6040_remove_one
),
1019 static int __init
r6040_init (void)
1021 RDC_DBUG("r6040_init()", 0);
1023 printk(KERN_INFO
"%s\n", version
);
1024 printed_version
= 1;
1026 if (parent
!= NULL
) {
1027 struct net_device
*the_parent
= dev_get_by_name(parent
);
1029 if (the_parent
== NULL
) {
1030 printk (KERN_ERR DRV_NAME
": Unknown device \"%s\" specified.\n", parent
);
1033 memcpy((u8
*)&adr_table
[0][0], the_parent
->dev_addr
, 6);
1034 memcpy((u8
*)&adr_table
[1][0], the_parent
->dev_addr
, 6);
1035 ++*(u8
*)&adr_table
[0][5];
1037 return pci_register_driver (&r6040_driver
);
1041 static void __exit
r6040_cleanup (void)
1043 RDC_DBUG("r6040_cleanup()", 0);
1044 pci_unregister_driver (&r6040_driver
);
1047 module_init(r6040_init
);
1048 module_exit(r6040_cleanup
);