Put back 2.6.23 files for rdc, allowing to switch from one kernel version to another...
[openwrt.git] / target / linux / rdc / patches-2.6.23 / 002-r6040_ethernet.patch
1 diff -urN linux-2.6.17/drivers/net/Kconfig linux-2.6.17.new/drivers/net/Kconfig
2 --- linux-2.6.17/drivers/net/Kconfig 2006-06-18 03:49:35.000000000 +0200
3 +++ linux-2.6.17.new/drivers/net/Kconfig 2006-09-25 13:14:27.000000000 +0200
4 @@ -1358,6 +1358,24 @@
5 <file:Documentation/networking/net-modules.txt>. The module will be
6 called apricot.
7
8 +config R6040
9 + tristate "RDC Fast-Ethernet support (EXPERIMENTAL)"
10 + depends on NET_PCI && EXPERIMENTAL
11 + select MII
12 + help
13 + If you have a network (Ethernet) controller of this type, say Y and
14 + read the Ethernet-HOWTO, available from
15 + <http://www.tldp.org/docs.html#howto>.
16 +
17 + To compile this driver as a module, choose M here and read
18 + <file:Documentation/networking/net-modules.txt>. The module will be
19 + called r6040.
20 +
21 +
22 +config R6040_NAPI
23 + bool "NAPI support for R6040"
24 + depends on R6040
25 + default y
26 config B44
27 tristate "Broadcom 4400 ethernet support (EXPERIMENTAL)"
28 depends on NET_PCI && PCI && EXPERIMENTAL
29 diff -urN linux-2.6.17/drivers/net/Makefile linux-2.6.17.new/drivers/net/Makefile
30 --- linux-2.6.17/drivers/net/Makefile 2006-06-18 03:49:35.000000000 +0200
31 +++ linux-2.6.17.new/drivers/net/Makefile 2006-09-25 13:14:45.000000000 +0200
32 @@ -106,6 +106,7 @@
33 obj-$(CONFIG_NE3210) += ne3210.o 8390.o
34 obj-$(CONFIG_NET_SB1250_MAC) += sb1250-mac.o
35 obj-$(CONFIG_B44) += b44.o
36 +obj-$(CONFIG_R6040) += r6040.o
37 obj-$(CONFIG_FORCEDETH) += forcedeth.o
38 obj-$(CONFIG_NE_H8300) += ne-h8300.o 8390.o
39 diff -urN linux-2.6.23/drivers/net/r6040.c linux-2.6.23.new/drivers/net/r6040.c
40 --- linux-2.6.23/drivers/net/r6040.c 2006-06-18 03:49:35.000000000 +0200
41 +++ linux-2.6.23.new/drivers/net/r6040.c 2006-09-25 13:14:45.000000000 +0200
42 @@ -0,0 +1,1048 @@
43 +/* r6040.c: A RDC R6040 FastEthernet driver for linux. */
44 +/*
45 + Re-written 2004 by Sten Wang.
46 +
47 + Copyright 1994-2000 by Donald Becker.
48 + Copyright 1993 United States Government as represented by the
49 + Director, National Security Agency. This software may be used and
50 + distributed according to the terms of the GNU General Public License,
51 + incorporated herein by reference.
52 +
53 + This driver is for RDC R6040 FastEthernet MAC series.
54 + For kernel version after 2.4.22
55 +
56 + Modification List
57 + ---------- ------------------------------------------------
58 + 10-07-2007 Clean up the driver using checkpatch
59 + 08-24-2006 Support at linux 2.6.10 above
60 + 03-24-2006 Support NAPI
61 + 03-21-2006 By Charies,change spin_lock_irqsave(lp->lock, flags)
62 + to spin_lock_irqsave(&lp->lock, flags)
63 + in set_multicast_list
64 + 03-15-2006 Modify the set_multicast_list ,due to when re-plug the ethernet,
65 + it will forget the previous setting
66 + 07-12-2005 Tim, modify the set_multicast_list
67 + 03-28-2005 Tim, modify some error mac register offset in
68 + function set_multicast_list
69 + 03-27-2005 Tim, Add the internal state machine reset
70 + Sten, If multicast address more than 4, enter PROM mode
71 + Changed rdc to r6040
72 + 12-22-2004 Sten Init MAC MBCR register=0x012A
73 + PHY_CAP = 0x01E1
74 +
75 + Need to Do LIst:
76 + 1. If multicast address more than 4, use the multicast address hash
77 +*/
78 +
79 +#define DRV_NAME "r6040"
80 +#define DRV_VERSION "0.13"
81 +#define DRV_RELDATE "24Aug2006"
82 +
83 +/* PHY CHIP Address */
84 +#define PHY1_ADDR 1 /* For MAC1 */
85 +#define PHY2_ADDR 2 /* For MAC2 */
86 +#define PHY_MODE 0x3100 /* PHY CHIP Register 0 */
87 +#define PHY_CAP 0x01E1 /* PHY CHIP Register 4 */
88 +
89 +/* Time in jiffies before concluding the transmitter is hung. */
90 +#define TX_TIMEOUT (6000 * HZ / 1000)
91 +#define TIMER_WUT (jiffies + HZ * 1)/* timer wakeup time : 1 second */
92 +
93 +/* RDC MAC ID */
94 +#define RDC_MAC_ID 0x6040
95 +
96 +/* RDC MAC I/O Size */
97 +#define R6040_IO_SIZE 256
98 +
99 +/* RDC Chip PCI Command */
100 +#define R6040_PCI_CMD 0x0005 /* IO, Master */
101 +
102 +/* MAX RDC MAC */
103 +#define MAX_MAC 2
104 +
105 +/* MAC setting */
106 +#define TX_DCNT 0x80 /* TX descriptor count */
107 +#define RX_DCNT 0x80 /* RX descriptor count */
108 +#define MAX_BUF_SIZE 0x600
109 +#define ALLOC_DESC_SIZE ((TX_DCNT+RX_DCNT)*sizeof(struct r6040_descriptor)+0x10)
110 +#define MBCR_DEFAULT 0x012A /* MAC Bus Control Register */
111 +
112 +/* PHY settings */
113 +#define ICPLUS_PHY_ID 0x0243
114 +
115 +/* Debug enable or not */
116 +#define RDC_DEBUG 0
117 +
118 +#if RDC_DEBUG > 1
119 +#define RDC_DBUG(msg, value) printk(KERN_ERR "%s %x\n", msg, value);
120 +#else
121 +#define RDC_DBUG(msg, value)
122 +#endif
123 +
124 +
125 +#include <linux/module.h>
126 +#include <linux/version.h>
127 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
128 +#include <linux/moduleparam.h>
129 +#endif
130 +#include <linux/kernel.h>
131 +#include <linux/string.h>
132 +#include <linux/timer.h>
133 +#include <linux/errno.h>
134 +#include <linux/ioport.h>
135 +#include <linux/slab.h>
136 +#include <linux/interrupt.h>
137 +#include <linux/pci.h>
138 +#include <linux/netdevice.h>
139 +#include <linux/etherdevice.h>
140 +#include <linux/skbuff.h>
141 +#include <linux/init.h>
142 +#include <linux/delay.h>
143 +#include <linux/mii.h>
144 +#include <linux/ethtool.h>
145 +#include <linux/crc32.h>
146 +#include <linux/spinlock.h>
147 +
148 +#include <asm/processor.h>
149 +#include <asm/bitops.h>
150 +#include <asm/io.h>
151 +#include <asm/irq.h>
152 +#include <asm/uaccess.h>
153 +
154 +MODULE_AUTHOR("Sten Wang <sten.wang@rdc.com.tw>, Daniel Gimpelevich <daniel@gimpelevich.san-francisco.ca.us>, Florian Fainelli <florian@openwrt.org>");
155 +MODULE_LICENSE("GPL");
156 +#ifdef CONFIG_R6040_NAPI
157 +MODULE_DESCRIPTION("RDC R6040 NAPI PCI FastEthernet Driver");
158 +#else
159 +MODULE_DESCRIPTION("RDC R6040 PCI FastEthernet Driver");
160 +#endif
161 +
162 +#define R6040_INT_MASK 0x0011
163 +
164 +struct r6040_descriptor {
165 + u16 status, len; /* 0-3 */
166 + u32 buf; /* 4-7 */
167 + u32 ndesc; /* 8-B */
168 + u32 rev1; /* C-F */
169 + char *vbufp; /* 10-13 */
170 + struct r6040_descriptor *vndescp; /* 14-17 */
171 + struct sk_buff *skb_ptr; /* 18-1B */
172 + u32 rev2; /* 1C-1F */
173 +} __attribute__((aligned(32)));
174 +
175 +struct r6040_private {
176 + struct net_device_stats stats;
177 + spinlock_t lock; /* driver lock */
178 + struct timer_list timer;
179 + struct pci_dev *pdev;
180 +
181 + struct r6040_descriptor *rx_insert_ptr;
182 + struct r6040_descriptor *rx_remove_ptr;
183 + struct r6040_descriptor *tx_insert_ptr;
184 + struct r6040_descriptor *tx_remove_ptr;
185 + u16 tx_free_desc, rx_free_desc, phy_addr, phy_mode;
186 + u16 mcr0, mcr1;
187 + dma_addr_t desc_dma;
188 + char *desc_pool;
189 + u16 switch_sig;
190 +};
191 +
192 +struct r6040_chip_info {
193 + const char *name;
194 + u16 pci_flags;
195 + int io_size;
196 + int drv_flags;
197 +};
198 +
199 +#ifdef CONFIG_R6040_NAPI
200 +static int NAPI_status;
201 +#endif
202 +
203 +static int __devinitdata printed_version;
204 +#ifdef CONFIG_R6040_NAPI
205 +static char version[] __devinitdata =
206 + KERN_INFO DRV_NAME ": RDC R6040 NAPI net driver, version "DRV_VERSION " (" DRV_RELDATE ")\n";
207 +#else
208 +static char version[] __devinitdata =
209 + KERN_INFO DRV_NAME ": RDC R6040 net driver, version "DRV_VERSION " (" DRV_RELDATE ")\n";
210 +#endif
211 +static struct r6040_chip_info r6040_chip_info[] __devinitdata =
212 +{
213 + { "RDC R6040 Knight", R6040_PCI_CMD, R6040_IO_SIZE, 0}
214 +};
215 +static char *parent;
216 +
217 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
218 +static int NUM_MAC_TABLE = 2 ;
219 +module_param(parent, charp, 0444);
220 +#else
221 +MODULE_PARM(parent, "s");
222 +#endif
223 +MODULE_PARM_DESC(parent, "Parent network device name");
224 +
225 +static int phy_table[] = { 0x1, 0x2};
226 +static u8 adr_table[2][8] = {
227 + {0x00, 0x00, 0x60, 0x00, 0x00, 0x01},
228 + {0x00, 0x00, 0x60, 0x00, 0x00, 0x02}
229 +};
230 +
231 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 10)
232 + module_param_array(adr_table, int, &NUM_MAC_TABLE, 0644);
233 +#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
234 + module_param_array(adr_table, int, NUM_MAC_TABLE, 0644);
235 +#else
236 + MODULE_PARM(adr_table, "2-4i");
237 +#endif
238 +MODULE_PARM_DESC(adr_table, "MAC Address (assigned)");
239 +
240 +static int r6040_open(struct net_device *dev);
241 +static int r6040_start_xmit(struct sk_buff *skb, struct net_device *dev);
242 +static irqreturn_t r6040_interrupt(int irq, void *dev_id);
243 +static struct net_device_stats *r6040_get_stats(struct net_device *dev);
244 +static int r6040_close(struct net_device *dev);
245 +static void set_multicast_list(struct net_device *dev);
246 +static struct ethtool_ops netdev_ethtool_ops;
247 +static int netdev_ioctl (struct net_device *dev, struct ifreq *rq, int cmd);
248 +static void r6040_down(struct net_device *dev);
249 +static void r6040_up(struct net_device *dev);
250 +static void r6040_tx_timeout (struct net_device *dev);
251 +static void r6040_timer(unsigned long);
252 +
253 +static int phy_mode_chk(struct net_device *dev);
254 +static int phy_read(int ioaddr, int phy_adr, int reg_idx);
255 +static void phy_write(int ioaddr, int phy_adr, int reg_idx, int dat);
256 +static void rx_buf_alloc(struct r6040_private *lp, struct net_device *dev);
257 +#ifdef CONFIG_R6040_NAPI
258 +static int r6040_poll(struct net_device *netdev, int *budget);
259 +#endif
260 +
261 +
262 +static int __devinit r6040_init_one (struct pci_dev *pdev,
263 + const struct pci_device_id *ent)
264 +{
265 + struct net_device *dev;
266 + struct r6040_private *lp;
267 + int ioaddr, io_size, err;
268 + static int card_idx = -1;
269 + int chip_id = (int)ent->driver_data;
270 +
271 + RDC_DBUG("r6040_init_one()", 0);
272 +
273 + if (printed_version++)
274 + printk(KERN_INFO "%s\n", version);
275 +
276 + err = pci_enable_device(pdev);
277 + if (err)
278 + return err;
279 +
280 + /* this should always be supported */
281 + if (pci_set_dma_mask(pdev, 0xffffffff)) {
282 + printk(KERN_ERR DRV_NAME "32-bit PCI DMA addresses not supported by the card!?\n");
283 + return -ENODEV;
284 + }
285 +
286 + /* IO Size check */
287 + io_size = r6040_chip_info[chip_id].io_size;
288 + if (pci_resource_len (pdev, 0) < io_size) {
289 + return -ENODEV;
290 + }
291 +
292 + ioaddr = pci_resource_start (pdev, 0); /* IO map base address */
293 + pci_set_master(pdev);
294 +
295 + dev = alloc_etherdev(sizeof(struct r6040_private));
296 + if (dev == NULL)
297 + return -ENOMEM;
298 + SET_MODULE_OWNER(dev);
299 +
300 + if (pci_request_regions(pdev, DRV_NAME)) {
301 + printk(KERN_ERR DRV_NAME ": Failed to request PCI regions\n");
302 + err = -ENODEV;
303 + goto err_out_disable;
304 + }
305 +
306 + /* Init system & device */
307 + lp = dev->priv;
308 + dev->base_addr = ioaddr;
309 + dev->irq = pdev->irq;
310 +
311 + spin_lock_init(&lp->lock);
312 + pci_set_drvdata(pdev, dev);
313 +
314 + /* Set MAC address */
315 + card_idx++;
316 + memcpy(dev->dev_addr, (u8 *)&adr_table[card_idx][0], 6);
317 +
318 + /* Link new device into r6040_root_dev */
319 + lp->pdev = pdev;
320 +
321 + /* Init RDC private data */
322 + lp->mcr0 = 0x1002;
323 + lp->phy_addr = phy_table[card_idx];
324 + lp->switch_sig = 0;
325 +
326 + /* The RDC-specific entries in the device structure. */
327 + dev->open = &r6040_open;
328 + dev->hard_start_xmit = &r6040_start_xmit;
329 + dev->stop = &r6040_close;
330 + dev->get_stats = &r6040_get_stats;
331 + dev->set_multicast_list = &set_multicast_list;
332 + dev->do_ioctl = &netdev_ioctl;
333 + dev->ethtool_ops = &netdev_ethtool_ops;
334 + dev->tx_timeout = &r6040_tx_timeout;
335 + dev->watchdog_timeo = TX_TIMEOUT;
336 +#ifdef CONFIG_R6040_NAPI
337 + dev->poll = &r6040_poll;
338 + dev->weight = 64;
339 +#endif
340 +
341 + /* Register net device. After this dev->name assign */
342 + err = register_netdev(dev);
343 + if (err) {
344 + printk(KERN_ERR DRV_NAME ": Failed to register net device\n");
345 + goto err_out_res;
346 + }
347 +
348 + netif_carrier_on(dev);
349 + return 0;
350 +
351 +err_out_res:
352 + pci_release_regions(pdev);
353 +err_out_disable:
354 + pci_disable_device(pdev);
355 + pci_set_drvdata(pdev, NULL);
356 + kfree(dev);
357 +
358 + return err;
359 +}
360 +
361 +static void __devexit r6040_remove_one (struct pci_dev *pdev)
362 +{
363 + struct net_device *dev = pci_get_drvdata(pdev);
364 +
365 + unregister_netdev(dev);
366 + pci_release_regions(pdev);
367 + kfree(dev);
368 + pci_disable_device(pdev);
369 + pci_set_drvdata(pdev, NULL);
370 +}
371 +
372 +static int
373 +r6040_open(struct net_device *dev)
374 +{
375 + struct r6040_private *lp = dev->priv;
376 + int i;
377 +
378 + RDC_DBUG("r6040_open()", 0);
379 +
380 + /* Request IRQ and Register interrupt handler */
381 + i = request_irq(dev->irq, &r6040_interrupt, SA_SHIRQ, dev->name, dev);
382 + if (i) return i;
383 +
384 + /* Allocate Descriptor memory */
385 + lp->desc_pool = pci_alloc_consistent(lp->pdev, ALLOC_DESC_SIZE, &lp->desc_dma);
386 + if (!lp->desc_pool)
387 + return -ENOMEM;
388 +
389 + r6040_up(dev);
390 +
391 + netif_start_queue(dev);
392 +
393 + if (lp->switch_sig != ICPLUS_PHY_ID) {
394 + /* set and active a timer process */
395 + init_timer(&lp->timer);
396 + lp->timer.expires = TIMER_WUT;
397 + lp->timer.data = (unsigned long)dev;
398 + lp->timer.function = &r6040_timer;
399 + add_timer(&lp->timer);
400 + }
401 + return 0;
402 +}
403 +
404 +static void
405 +r6040_tx_timeout (struct net_device *dev)
406 +{
407 + struct r6040_private *lp = dev->priv;
408 + /* int ioaddr = dev->base_addr;
409 + struct r6040_descriptor *descptr = lp->tx_remove_ptr; */
410 +
411 + RDC_DBUG("r6040_tx_timeout()", 0);
412 +
413 + /* Transmitter timeout, serious problems. */
414 + /* Sten: Nothing need to do so far. */
415 + printk(KERN_ERR DRV_NAME ": Big Trobule, transmit timeout/n");
416 + lp->stats.tx_errors++;
417 + netif_stop_queue(dev);
418 +
419 +//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));
420 +
421 +//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);
422 +}
423 +
424 +
425 +static int
426 +r6040_start_xmit(struct sk_buff *skb, struct net_device *dev)
427 +{
428 + struct r6040_private *lp = dev->priv;
429 + struct r6040_descriptor *descptr;
430 + int ioaddr = dev->base_addr;
431 + unsigned long flags;
432 +
433 + RDC_DBUG("r6040_start_xmit()", 0);
434 +
435 + if (skb == NULL) /* NULL skb directly return */
436 + return 0;
437 + if (skb->len >= MAX_BUF_SIZE) { /* Packet too long, drop it */
438 + dev_kfree_skb(skb);
439 + return 0;
440 + }
441 +
442 + /* Critical Section */
443 + spin_lock_irqsave(&lp->lock, flags);
444 +
445 + /* TX resource check */
446 + if (!lp->tx_free_desc) {
447 + spin_unlock_irqrestore(&lp->lock, flags);
448 + printk(KERN_ERR DRV_NAME ": NO TX DESC ");
449 + return 1;
450 + }
451 +
452 + /* Statistic Counter */
453 + lp->stats.tx_packets++;
454 + lp->stats.tx_bytes += skb->len;
455 +
456 + /* Set TX descriptor & Transmit it */
457 + lp->tx_free_desc--;
458 + descptr = lp->tx_insert_ptr;
459 + if (skb->len < 0x3c)
460 + descptr->len = 0x3c;
461 + else
462 + descptr->len = skb->len;
463 +
464 + descptr->skb_ptr = skb;
465 + descptr->buf = cpu_to_le32(pci_map_single(lp->pdev, skb->data, skb->len, PCI_DMA_TODEVICE));
466 + descptr->status = 0x8000;
467 + outw(0x01, ioaddr + 0x14);
468 + lp->tx_insert_ptr = descptr->vndescp;
469 +
470 +#if RDC_DEBUG
471 + printk("Xmit(): %08lx:%04x %04x %08lx %08lx %08lx %08lx\n", descptr, descptr->status, descptr->len, descptr->buf, descptr->skb_ptr, descptr->ndesc, descptr->vndescp);
472 +#endif
473 +
474 + /* If no tx resource, stop */
475 + if (!lp->tx_free_desc)
476 + netif_stop_queue(dev);
477 +
478 + dev->trans_start = jiffies;
479 + spin_unlock_irqrestore(&lp->lock, flags);
480 + return 0;
481 +}
482 +
483 +/* The RDC interrupt handler. */
484 +static irqreturn_t
485 +r6040_interrupt(int irq, void *dev_id)
486 +{
487 + struct net_device *dev = dev_id;
488 + struct r6040_private *lp;
489 + struct r6040_descriptor *descptr;
490 + struct sk_buff *skb_ptr;
491 + int ioaddr, status;
492 + unsigned long flags;
493 +#ifdef CONFIG_R6040_NAPI
494 + int handled = 1;
495 +#else
496 + int handled = 0;
497 +#endif
498 +
499 + RDC_DBUG("r6040_interrupt()", 0);
500 + if (dev == NULL) {
501 + printk (KERN_ERR DRV_NAME ": INT() unknown device.\n");
502 + return IRQ_RETVAL(handled);
503 + }
504 +
505 + lp = (struct r6040_private *)dev->priv;
506 + spin_lock_irqsave(&lp->lock, flags);
507 +
508 + /* Check MAC Interrupt status */
509 + ioaddr = dev->base_addr;
510 + outw(0x0, ioaddr + 0x40); /* Mask Off RDC MAC interrupt */
511 + status = inw(ioaddr + 0x3c); /* Read INTR status and clear */
512 +
513 +
514 +#ifdef CONFIG_R6040_NAPI
515 + if (netif_rx_schedule_prep(dev)) {
516 + NAPI_status = status;
517 + __netif_rx_schedule(dev);
518 + }
519 +
520 + spin_unlock_irqrestore(&lp->lock, flags);
521 + return IRQ_RETVAL(handled);
522 +#else
523 + /* TX interrupt request */
524 + if (status & 0x10) {
525 + handled = 1;
526 + descptr = lp->tx_remove_ptr;
527 + while (lp->tx_free_desc < TX_DCNT) {
528 + if (descptr->status & 0x8000)
529 + break; /* Not complte */
530 + skb_ptr = descptr->skb_ptr;
531 + pci_unmap_single(lp->pdev, descptr->buf, skb_ptr->len, PCI_DMA_TODEVICE);
532 + dev_kfree_skb_irq(skb_ptr); /* Free buffer */
533 + descptr->skb_ptr = 0;
534 + descptr = descptr->vndescp; /* To next descriptor */
535 + lp->tx_free_desc++;
536 + }
537 + lp->tx_remove_ptr = descptr;
538 + if (lp->tx_free_desc)
539 + netif_wake_queue(dev);
540 + }
541 +
542 + /* RX interrupt request */
543 + if (status & 0x01) {
544 + handled = 1;
545 + descptr = lp->rx_remove_ptr;
546 + while(lp->rx_free_desc) {
547 + if (descptr->status & 0x8000) break; /* No Rx packet */
548 + skb_ptr = descptr->skb_ptr;
549 + descptr->skb_ptr = 0;
550 + skb_ptr->dev = dev;
551 + skb_put(skb_ptr, descptr->len - 4);
552 + pci_unmap_single(lp->pdev, descptr->buf, MAX_BUF_SIZE, PCI_DMA_FROMDEVICE);
553 + skb_ptr->protocol = eth_type_trans(skb_ptr, dev);
554 + netif_rx(skb_ptr); /* Send to upper layer */
555 + lp->stats.rx_packets++;
556 + lp->stats.rx_bytes += descptr->len;
557 + descptr = descptr->vndescp; /* To next descriptor */
558 + lp->rx_free_desc--;
559 + }
560 + lp->rx_remove_ptr = descptr;
561 + }
562 +
563 + /* Allocate new RX buffer */
564 + if (lp->rx_free_desc < RX_DCNT) rx_buf_alloc(lp,dev);
565 +
566 + outw(R6040_INT_MASK, ioaddr + 0x40); /* TX/RX interrupt enable */
567 + spin_unlock_irqrestore(&lp->lock, flags);
568 +#endif
569 + return IRQ_RETVAL(handled);
570 +}
571 +
572 +
573 +static struct net_device_stats *
574 +r6040_get_stats(struct net_device *dev)
575 +{
576 + struct r6040_private *lp = dev->priv;
577 +
578 + RDC_DBUG("r6040_get_stats()", 0);
579 + return &lp->stats;
580 +}
581 +
582 +/*
583 + * Set or clear the multicast filter for this adaptor.
584 + */
585 +static void
586 +set_multicast_list(struct net_device *dev)
587 +{
588 + struct r6040_private *lp = dev->priv;
589 + struct dev_mc_list *mcptr;
590 + int ioaddr = dev->base_addr;
591 + u16 *adrp, i;
592 + unsigned long flags;
593 +
594 + RDC_DBUG("set_multicast_list()", 0);
595 +
596 + /* MAC Address */
597 + adrp = (u16 *) dev->dev_addr;
598 + outw(adrp[0], ioaddr + 0x68);
599 + outw(adrp[1], ioaddr + 0x6A);
600 + outw(adrp[2], ioaddr + 0x6C);
601 +
602 +
603 +#if RDC_DEBUG
604 + printk("MAC ADDR: %04x %04x %04x\n", adrp[0], adrp[1], adrp[2]);
605 +#endif
606 +
607 + /* Promiscous Mode */
608 + spin_lock_irqsave(&lp->lock, flags);
609 + i = inw(ioaddr) & ~0x0120; /* Clear AMCP & PROM */
610 + if (dev->flags & IFF_PROMISC) {
611 + i |= 0x0020;
612 + lp->mcr0 |= 0x0020;
613 + }
614 + if (dev->mc_count > 4)
615 + i |= 0x0020; /* Too many multicast address */
616 +
617 + outw(i, ioaddr);
618 + spin_unlock_irqrestore(&lp->lock, flags);
619 +
620 + /* Multicast Address */
621 + if (dev->mc_count > 4) /* Wait to do: Hash Table for multicast */
622 + return;
623 +
624 + /* Multicast Address 1~4 case */
625 + for (i = 0, mcptr = dev->mc_list; (i < dev->mc_count) && (i < 4); i++) {
626 + adrp = (u16 *)mcptr->dmi_addr;
627 + outw(adrp[0], ioaddr + 0x70 + 8*i);
628 + outw(adrp[1], ioaddr + 0x72 + 8*i);
629 + outw(adrp[2], ioaddr + 0x74 + 8*i);
630 + mcptr = mcptr->next;
631 +#if RDC_DEBUG
632 + printk("M_ADDR: %04x %04x %04x\n", adrp[0], adrp[1], adrp[2]);
633 +#endif
634 + }
635 + for (i = dev->mc_count; i < 4; i++) {
636 + outw(0xffff, ioaddr + 0x68 + 8*i);
637 + outw(0xffff, ioaddr + 0x6A + 8*i);
638 + outw(0xffff, ioaddr + 0x6C + 8*i);
639 + }
640 +}
641 +
642 +static void netdev_get_drvinfo (struct net_device *dev, struct ethtool_drvinfo *info)
643 +{
644 + struct r6040_private *rp = dev->priv;
645 +
646 + strcpy (info->driver, DRV_NAME);
647 + strcpy (info->version, DRV_VERSION);
648 + strcpy (info->bus_info, pci_name(rp->pdev));
649 +}
650 +
651 +static struct ethtool_ops netdev_ethtool_ops = {
652 + .get_drvinfo = netdev_get_drvinfo,
653 +};
654 +
655 +static int
656 +r6040_close(struct net_device *dev)
657 +{
658 + struct r6040_private *lp = dev->priv;
659 +
660 + RDC_DBUG("r6040_close()", 0);
661 +
662 + /* deleted timer */
663 + del_timer_sync(&lp->timer);
664 +
665 + spin_lock_irq(&lp->lock);
666 +
667 + netif_stop_queue(dev);
668 +
669 + r6040_down(dev);
670 +
671 + spin_unlock_irq(&lp->lock);
672 +
673 + return 0;
674 +}
675 +
676 +static int netdev_ioctl (struct net_device *dev, struct ifreq *rq, int cmd)
677 +{
678 + struct r6040_private *lp = dev->priv;
679 +
680 + RDC_DBUG("netdev_ioctl()", 0);
681 +
682 + if (lp->switch_sig == ICPLUS_PHY_ID && cmd == SIOCDEVPRIVATE) {
683 + unsigned long *data = (unsigned long *)rq->ifr_data, args[4];
684 + int ioaddr = dev->base_addr;
685 + unsigned int val;
686 +
687 + data = (unsigned long *)rq->ifr_data;
688 + if (copy_from_user(args, data, 4*sizeof(unsigned long)))
689 + return -EFAULT;
690 +
691 + /* port priority */
692 + if(args[0]&(1<<31))phy_write(ioaddr, 29, 19, (phy_read(ioaddr, 29, 19) | 0x2000)); /* port 0 */
693 + if(args[0]&(1<<29))phy_write(ioaddr, 29, 19, (phy_read(ioaddr, 29, 19) | 0x0020)); /* port 1 */
694 + if(args[0]&(1<<27))phy_write(ioaddr, 29, 20, (phy_read(ioaddr, 29, 20) | 0x2000)); /* port 2 */
695 + if(args[0]&(1<<25))phy_write(ioaddr, 29, 20, (phy_read(ioaddr, 29, 20) | 0x0020)); /* port 3 */
696 +
697 + }
698 + return -EOPNOTSUPP;
699 +}
700 +
701 +/**
702 + Stop RDC MAC and Free the allocated resource
703 + */
704 +static void r6040_down(struct net_device *dev)
705 +{
706 + struct r6040_private *lp = dev->priv;
707 + int i;
708 + int ioaddr = dev->base_addr;
709 +
710 + RDC_DBUG("r6040_down()", 0);
711 +
712 + /* Stop MAC */
713 + outw(0x0000, ioaddr + 0x40); /* Mask Off Interrupt */
714 + outw(0x0001, ioaddr + 0x04); /* Reset RDC MAC */
715 + i = 0;
716 + do{}while((i++ < 2048) && (inw(ioaddr + 0x04) & 0x1));
717 +
718 + free_irq(dev->irq, dev);
719 +
720 + /* Free RX buffer */
721 + for (i = 0; i < RX_DCNT; i++) {
722 + if (lp->rx_insert_ptr->skb_ptr) {
723 + pci_unmap_single(lp->pdev, lp->rx_insert_ptr->buf, MAX_BUF_SIZE, PCI_DMA_FROMDEVICE);
724 + dev_kfree_skb(lp->rx_insert_ptr->skb_ptr);
725 + lp->rx_insert_ptr->skb_ptr = 0;
726 + }
727 + lp->rx_insert_ptr = lp->rx_insert_ptr->vndescp;
728 + }
729 +
730 + /* Free TX buffer */
731 + for (i = 0; i < TX_DCNT; i++) {
732 + if (lp->tx_insert_ptr->skb_ptr) {
733 + pci_unmap_single(lp->pdev, lp->tx_insert_ptr->buf, MAX_BUF_SIZE, PCI_DMA_TODEVICE);
734 + dev_kfree_skb(lp->tx_insert_ptr->skb_ptr);
735 + lp->rx_insert_ptr->skb_ptr = 0;
736 + }
737 + lp->tx_insert_ptr = lp->tx_insert_ptr->vndescp;
738 + }
739 +
740 + /* Free Descriptor memory */
741 + pci_free_consistent(lp->pdev, ALLOC_DESC_SIZE, lp->desc_pool, lp->desc_dma);
742 +}
743 +
744 +
745 +
746 +#ifdef CONFIG_R6040_NAPI
747 +static int r6040_poll(struct net_device *dev, int *budget)
748 +{
749 + struct r6040_private *lp;
750 + struct r6040_descriptor *descptr;
751 + struct sk_buff *skb_ptr;
752 + int ioaddr, status;
753 + unsigned long flags;
754 +
755 +
756 + ioaddr = dev->base_addr;
757 + lp = (struct r6040_private *)dev->priv;
758 + unsigned long rx_work = dev->quota;
759 + unsigned long rx;
760 +#if 1
761 + /* TX interrupt request */
762 + if (NAPI_status & 0x10) {
763 + descptr = lp->tx_remove_ptr;
764 + while (lp->tx_free_desc < TX_DCNT) {
765 + if (descptr->status & 0x8000)
766 + break; /* Not complte */
767 +
768 + skb_ptr = descptr->skb_ptr;
769 + pci_unmap_single(lp->pdev, descptr->buf, skb_ptr->len, PCI_DMA_TODEVICE);
770 + dev_kfree_skb_irq(skb_ptr); /* Free buffer */
771 + descptr->skb_ptr = 0;
772 + descptr = descptr->vndescp; /* To next descriptor */
773 + lp->tx_free_desc++;
774 + }
775 + lp->tx_remove_ptr = descptr;
776 + if (lp->tx_free_desc) netif_wake_queue(dev);
777 + }
778 +#endif
779 +#if 1
780 + /* RX interrupt request */
781 + if (NAPI_status & 0x01) {
782 + descptr = lp->rx_remove_ptr;
783 + while (lp->rx_free_desc) {
784 + if (descptr->status & 0x8000)
785 + break; /* No Rx packet */
786 + skb_ptr = descptr->skb_ptr;
787 + descptr->skb_ptr = 0;
788 + skb_ptr->dev = dev;
789 + skb_put(skb_ptr, descptr->len - 4);
790 + pci_unmap_single(lp->pdev, descptr->buf, MAX_BUF_SIZE, PCI_DMA_FROMDEVICE);
791 + skb_ptr->protocol = eth_type_trans(skb_ptr, dev);
792 + netif_receive_skb(skb_ptr); /* Send to upper layer */
793 + lp->stats.rx_packets++;
794 + lp->stats.rx_bytes += descptr->len;
795 + descptr = descptr->vndescp; /* To next descriptor */
796 + lp->rx_free_desc--;
797 + }
798 + lp->rx_remove_ptr = descptr;
799 + }
800 + /* Allocate new RX buffer */
801 + if (lp->rx_free_desc < RX_DCNT)
802 + rx_buf_alloc(lp, dev);
803 +
804 + local_irq_disable();
805 + netif_rx_complete(dev);
806 + outw(R6040_INT_MASK, ioaddr + 0x40);
807 + local_irq_enable();
808 + return 0;
809 +#endif
810 +}
811 +#endif
812 +
813 +/* Init RDC MAC */
814 +static void r6040_up(struct net_device *dev)
815 +{
816 + struct r6040_private *lp = dev->priv;
817 + struct r6040_descriptor *descptr;
818 + int i;
819 + int ioaddr = dev->base_addr;
820 + u32 tmp_addr;
821 + dma_addr_t desc_dma, start_dma;
822 +
823 + RDC_DBUG("r6040_up()", 0);
824 +
825 + /* Initilize */
826 + lp->tx_free_desc = TX_DCNT;
827 + lp->rx_free_desc = 0;
828 +
829 + /* Init descriptor */
830 + memset(lp->desc_pool, 0, ALLOC_DESC_SIZE); /* Let all descriptor = 0 */
831 + lp->tx_insert_ptr = (struct r6040_descriptor *)lp->desc_pool;
832 + lp->tx_remove_ptr = lp->tx_insert_ptr;
833 + lp->rx_insert_ptr = (struct r6040_descriptor *)lp->tx_insert_ptr+TX_DCNT;
834 + lp->rx_remove_ptr = lp->rx_insert_ptr;
835 +
836 + /* Init TX descriptor */
837 + descptr = lp->tx_insert_ptr;
838 + desc_dma = lp->desc_dma;
839 + start_dma = desc_dma;
840 + for (i = 0; i < TX_DCNT; i++) {
841 + descptr->ndesc = cpu_to_le32(desc_dma + sizeof(struct r6040_descriptor));
842 + descptr->vndescp = (descptr + 1);
843 + descptr = (descptr + 1);
844 + desc_dma += sizeof(struct r6040_descriptor);
845 + }
846 + (descptr - 1)->ndesc = cpu_to_le32(start_dma);
847 + (descptr - 1)->vndescp = lp->tx_insert_ptr;
848 +
849 + /* Init RX descriptor */
850 + start_dma = desc_dma;
851 + descptr = lp->rx_insert_ptr;
852 + for (i = 0; i < RX_DCNT; i++) {
853 + descptr->ndesc = cpu_to_le32(desc_dma + sizeof(struct r6040_descriptor));
854 + descptr->vndescp = (descptr + 1);
855 + descptr = (descptr + 1);
856 + desc_dma += sizeof(struct r6040_descriptor);
857 + }
858 + (descptr - 1)->ndesc = cpu_to_le32(start_dma);
859 + (descptr - 1)->vndescp = lp->rx_insert_ptr;
860 +
861 + /* Allocate buffer for RX descriptor */
862 + rx_buf_alloc(lp, dev);
863 +
864 +#if RDC_DEBUG
865 +descptr = lp->tx_insert_ptr;
866 +for (i = 0; i < TX_DCNT; i++) {
867 + printk("%08lx:%04x %04x %08lx %08lx %08lx %08lx\n", descptr, descptr->status, descptr->len, descptr->buf, descptr->skb_ptr, descptr->ndesc, descptr->vndescp);
868 + descptr = descptr->vndescp;
869 +}
870 +descptr = lp->rx_insert_ptr;
871 +for (i = 0; i < RX_DCNT; i++) {
872 + printk("%08lx:%04x %04x %08lx %08lx %08lx %08lx\n", descptr, descptr->status, descptr->len, descptr->buf, descptr->skb_ptr, descptr->ndesc, descptr->vndescp);
873 + descptr = descptr->vndescp;
874 +}
875 +#endif
876 +
877 + /* MAC operation register */
878 + outw(0x01, ioaddr+0x04); /* Reset MAC */
879 + outw(2, ioaddr+0xAC); /* Reset internal state machine */
880 + outw(0, ioaddr+0xAC);
881 + udelay(5000);
882 +
883 + /* TX and RX descriptor start Register */
884 + tmp_addr = cpu_to_le32(lp->tx_insert_ptr);
885 + tmp_addr = virt_to_bus((volatile void *)tmp_addr);
886 + outw((u16) tmp_addr, ioaddr+0x2c);
887 + outw(tmp_addr >> 16, ioaddr+0x30);
888 + tmp_addr = cpu_to_le32(lp->rx_insert_ptr);
889 + tmp_addr = virt_to_bus((volatile void *)tmp_addr);
890 + outw((u16) tmp_addr, ioaddr+0x34);
891 + outw(tmp_addr >> 16, ioaddr+0x38);
892 +
893 + /* Buffer Size Register */
894 + outw(MAX_BUF_SIZE, ioaddr+0x18);
895 +
896 + if ((lp->switch_sig = phy_read(ioaddr, 0, 2)) == ICPLUS_PHY_ID) {
897 + phy_write(ioaddr, 29,31, 0x175C); /* Enable registers */
898 + lp->phy_mode = 0x8000;
899 + } else {
900 + /* PHY Mode Check */
901 + phy_write(ioaddr, lp->phy_addr, 4, PHY_CAP);
902 + phy_write(ioaddr, lp->phy_addr, 0, PHY_MODE);
903 +
904 + if (PHY_MODE == 0x3100)
905 + lp->phy_mode = phy_mode_chk(dev);
906 + else
907 + lp->phy_mode = (PHY_MODE & 0x0100) ? 0x8000:0x0;
908 + }
909 + /* MAC Bus Control Register */
910 + outw(MBCR_DEFAULT, ioaddr+0x8);
911 +
912 + /* MAC TX/RX Enable */
913 + lp->mcr0 |= lp->phy_mode;
914 + outw(lp->mcr0, ioaddr);
915 +
916 + /* set interrupt waiting time and packet numbers */
917 + outw(0x0802, ioaddr + 0x0C);
918 + outw(0x0802, ioaddr + 0x10);
919 +
920 + /* upgrade performance (by RDC guys) */
921 + phy_write(ioaddr, 30, 17, (phy_read(ioaddr, 30, 17) | 0x4000)); //bit 14=1
922 + phy_write(ioaddr, 30, 17, ~((~phy_read(ioaddr, 30, 17)) | 0x2000)); //bit 13=0
923 + phy_write(ioaddr, 0, 19, 0x0000);
924 + phy_write(ioaddr, 0, 30, 0x01F0);
925 +
926 + /* Interrupt Mask Register */
927 + outw(R6040_INT_MASK, ioaddr + 0x40);
928 +}
929 +
930 +/*
931 + A periodic timer routine
932 + Polling PHY Chip Link Status
933 +*/
934 +static void r6040_timer(unsigned long data)
935 +{
936 + struct net_device *dev = (struct net_device *)data;
937 + struct r6040_private *lp = dev->priv;
938 + u16 ioaddr = dev->base_addr, phy_mode;
939 +
940 + RDC_DBUG("r6040_timer()", 0);
941 +
942 + /* Polling PHY Chip Status */
943 + if (PHY_MODE == 0x3100)
944 + phy_mode = phy_mode_chk(dev);
945 + else
946 + phy_mode = (PHY_MODE & 0x0100) ? 0x8000:0x0;
947 +
948 + if (phy_mode != lp->phy_mode) {
949 + lp->phy_mode = phy_mode;
950 + lp->mcr0 = (lp->mcr0 & 0x7fff) | phy_mode;
951 + outw(lp->mcr0, ioaddr);
952 + printk(KERN_INFO "Link Change %x \n", inw(ioaddr));
953 + }
954 +
955 + /* Debug */
956 +// printk("<RDC> Timer: CR0 %x CR40 %x CR3C %x\n", inw(ioaddr), inw(ioaddr+0x40), inw(ioaddr+0x3c));
957 +
958 + /* Timer active again */
959 + lp->timer.expires = TIMER_WUT;
960 + add_timer(&lp->timer);
961 +}
962 +
963 +/* Allocate skb buffer for rx descriptor */
964 +static void rx_buf_alloc(struct r6040_private *lp, struct net_device *dev)
965 +{
966 + struct r6040_descriptor *descptr;
967 + int ioaddr = dev->base_addr ;
968 +
969 + RDC_DBUG("rx_buf_alloc()", 0);
970 +
971 + descptr = lp->rx_insert_ptr;
972 + while (lp->rx_free_desc < RX_DCNT) {
973 + descptr->skb_ptr = dev_alloc_skb(MAX_BUF_SIZE);
974 +
975 + if (!descptr->skb_ptr)
976 + break;
977 + descptr->buf = cpu_to_le32(pci_map_single(lp->pdev, descptr->skb_ptr->tail, MAX_BUF_SIZE, PCI_DMA_FROMDEVICE));
978 + descptr->status = 0x8000;
979 + descptr = descptr->vndescp;
980 + lp->rx_free_desc++;
981 + outw(lp->mcr0 | 0x0002, ioaddr); /* Trigger Rx DMA */
982 + }
983 + lp->rx_insert_ptr = descptr;
984 +}
985 +
986 +/* Status of PHY CHIP */
987 +static int phy_mode_chk(struct net_device *dev)
988 +{
989 + struct r6040_private *lp = dev->priv;
990 + int ioaddr = dev->base_addr, phy_dat;
991 +
992 + RDC_DBUG("phy_mode_chk()", 0);
993 +
994 + /* PHY Link Status Check */
995 + phy_dat = phy_read(ioaddr, lp->phy_addr, 1);
996 + if (!(phy_dat & 0x4))
997 + return 0x8000; /* Link Failed, full duplex */
998 +
999 + /* PHY Chip Auto-Negotiation Status */
1000 + phy_dat = phy_read(ioaddr, lp->phy_addr, 1);
1001 + if (phy_dat & 0x0020) {
1002 + /* Auto Negotiation Mode */
1003 + phy_dat = phy_read(ioaddr, lp->phy_addr, 5);
1004 + phy_dat &= phy_read(ioaddr, lp->phy_addr, 4);
1005 + if (phy_dat & 0x140)
1006 + phy_dat = 0x8000;
1007 + else
1008 + phy_dat = 0;
1009 + } else {
1010 + /* Force Mode */
1011 + phy_dat = phy_read(ioaddr, lp->phy_addr, 0);
1012 + if (phy_dat & 0x100) phy_dat = 0x8000;
1013 + else phy_dat = 0x0000;
1014 + }
1015 +
1016 + return phy_dat;
1017 +};
1018 +
1019 +/* Read a word data from PHY Chip */
1020 +static int phy_read(int ioaddr, int phy_addr, int reg_idx)
1021 +{
1022 + int i = 0;
1023 +
1024 + RDC_DBUG("phy_read()", 0);
1025 + outw(0x2000 + reg_idx + (phy_addr << 8), ioaddr + 0x20);
1026 + do {} while ((i++ < 2048) && (inw(ioaddr + 0x20) & 0x2000));
1027 +
1028 + return inw(ioaddr + 0x24);
1029 +}
1030 +
1031 +/* Write a word data from PHY Chip */
1032 +static void phy_write(int ioaddr, int phy_addr, int reg_idx, int dat)
1033 +{
1034 + int i = 0;
1035 +
1036 + RDC_DBUG("phy_write()", 0);
1037 + outw(dat, ioaddr + 0x28);
1038 + outw(0x4000 + reg_idx + (phy_addr << 8), ioaddr + 0x20);
1039 + do{}while( (i++ < 2048) && (inw(ioaddr + 0x20) & 0x4000) );
1040 +}
1041 +
1042 +enum {
1043 + RDC_6040 = 0
1044 +};
1045 +
1046 +static struct pci_device_id r6040_pci_tbl[] = {
1047 + {PCI_VENDOR_ID_RDC, PCI_DEVICE_ID_RDC_R6040, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RDC_6040},
1048 + /*{0x1106, 0x3065, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RDC_6040},*/
1049 + {0,}
1050 +};
1051 +MODULE_DEVICE_TABLE(pci, r6040_pci_tbl);
1052 +
1053 +static struct pci_driver r6040_driver = {
1054 + .name = "r6040",
1055 + .id_table = r6040_pci_tbl,
1056 + .probe = r6040_init_one,
1057 + .remove = __devexit_p(r6040_remove_one),
1058 +};
1059 +
1060 +
1061 +static int __init r6040_init (void)
1062 +{
1063 + RDC_DBUG("r6040_init()", 0);
1064 +
1065 + printk(KERN_INFO "%s\n", version);
1066 + printed_version = 1;
1067 +
1068 + if (parent != NULL) {
1069 + struct net_device *the_parent = dev_get_by_name(parent);
1070 +
1071 + if (the_parent == NULL) {
1072 + printk (KERN_ERR DRV_NAME ": Unknown device \"%s\" specified.\n", parent);
1073 + return -EINVAL;
1074 + }
1075 + memcpy((u8 *)&adr_table[0][0], the_parent->dev_addr, 6);
1076 + memcpy((u8 *)&adr_table[1][0], the_parent->dev_addr, 6);
1077 + ++*(u8 *)&adr_table[0][5];
1078 + }
1079 + return pci_register_driver (&r6040_driver);
1080 +}
1081 +
1082 +
1083 +static void __exit r6040_cleanup (void)
1084 +{
1085 + RDC_DBUG("r6040_cleanup()", 0);
1086 + pci_unregister_driver (&r6040_driver);
1087 +}
1088 +
1089 +module_init(r6040_init);
1090 +module_exit(r6040_cleanup);
This page took 0.086109 seconds and 5 git commands to generate.