fix a small typo
[openwrt.git] / target / linux / rdc-2.6 / files / drivers / net / r6040.c
1 /* r6040.c: A RDC R6040 FastEthernet driver for linux. */
2 /*
3 Re-written 2004 by Sten Wang.
4
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.
10
11 This driver is for RDC R6040 FastEthernet MAC series.
12 For kernel version after 2.4.22
13
14 Modification List
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
27 Changed rdc to r6040
28 12-22-2004 Sten Init MAC MBCR register=0x012A
29 PHY_CAP = 0x01E1
30
31 Need to Do LIst:
32 1. If multicast address more than 4, use the multicast address hash
33 */
34
35 #define DRV_NAME "r6040"
36 #define DRV_VERSION "0.13"
37 #define DRV_RELDATE "24Aug2006"
38
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 */
44
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 */
48
49 /* RDC MAC ID */
50 #define RDC_MAC_ID 0x6040
51
52 /* RDC MAC I/O Size */
53 #define R6040_IO_SIZE 256
54
55 /* RDC Chip PCI Command */
56 #define R6040_PCI_CMD 0x0005 /* IO, Master */
57
58 /* MAX RDC MAC */
59 #define MAX_MAC 2
60
61 /* MAC setting */
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 */
67
68 /* Debug enable or not */
69 #define RDC_DEBUG 0
70
71 #if RDC_DEBUG > 1
72 #define RDC_DBUG(msg, value) printk("%s %x\n", msg, value);
73 #else
74 #define RDC_DBUG(msg, value)
75 #endif
76
77
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>
82 #endif
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>
100
101 #include <asm/processor.h>
102 #include <asm/bitops.h>
103 #include <asm/io.h>
104 #include <asm/irq.h>
105 #include <asm/uaccess.h>
106
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");
111 #else
112 MODULE_DESCRIPTION("RDC R6040 PCI FastEthernet Driver");
113 #endif
114
115 #define R6040_INT_MASK 0x0011
116
117 struct r6040_descriptor {
118 u16 status, len; /* 0-3 */
119 u32 buf; /* 4-7 */
120 u32 ndesc; /* 8-B */
121 u32 rev1; /* C-F */
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) ));
127
128 struct r6040_private {
129 struct net_device_stats stats;
130 spinlock_t lock;
131 struct timer_list timer;
132 struct pci_dev *pdev;
133
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;
139 u16 mcr0, mcr1;
140 dma_addr_t desc_dma;
141 char *desc_pool;
142 };
143
144 struct r6040_chip_info {
145 const char *name;
146 u16 pci_flags;
147 int io_size;
148 int drv_flags;
149 };
150
151 #ifdef CONFIG_R6040_NAPI
152 static int NAPI_status;
153 #endif
154
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";
159 #else
160 static char version[] __devinitdata =
161 KERN_INFO DRV_NAME ": RDC R6040 net driver, version "DRV_VERSION " (" DRV_RELDATE ")\n";
162 #endif
163 static struct r6040_chip_info r6040_chip_info[] __devinitdata =
164 {
165 { "RDC R6040 Knight", R6040_PCI_CMD, R6040_IO_SIZE, 0}
166 };
167
168 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
169 static int NUM_MAC_TABLE = 2 ;
170 #endif
171
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}};
174
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);
179 #else
180 MODULE_PARM(adr_table, "2-4i");
181 #endif
182 MODULE_PARM_DESC(adr_table, "MAC Address (assigned)");
183
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);
195
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);
202 #endif
203
204
205 static int __devinit r6040_init_one (struct pci_dev *pdev,
206 const struct pci_device_id *ent)
207 {
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;
213
214 RDC_DBUG("r6040_init_one()", 0);
215
216 if (printed_version++)
217 printk(version);
218
219 if ((err = pci_enable_device (pdev)))
220 return err;
221
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");
225 return -ENODEV;
226 }
227
228 /* IO Size check */
229 io_size = r6040_chip_info[chip_id].io_size;
230 if (pci_resource_len (pdev, 0) < io_size) {
231 return -ENODEV;
232 }
233
234 ioaddr = pci_resource_start (pdev, 0); /* IO map base address */
235 pci_set_master(pdev);
236
237 dev = alloc_etherdev(sizeof(struct r6040_private));
238 if (dev == NULL)
239 return -ENOMEM;
240 SET_MODULE_OWNER(dev);
241
242 if (pci_request_regions(pdev, DRV_NAME)) {
243 printk(KERN_ERR DRV_NAME ": Failed to request PCI regions\n");
244 err = -ENODEV;
245 goto err_out_disable;
246 }
247
248 /* Init system & device */
249 lp = dev->priv;
250 dev->base_addr = ioaddr;
251 dev->irq = pdev->irq;
252
253 spin_lock_init(&lp->lock);
254 pci_set_drvdata(pdev, dev);
255
256 /* Set MAC address */
257 card_idx++;
258 memcpy(dev->dev_addr, (u8 *)&adr_table[card_idx][0], 6);
259
260 /* Link new device into r6040_root_dev */
261 lp->pdev = pdev;
262
263 /* Init RDC private data */
264 lp->mcr0 = 0x1002;
265 lp->phy_addr = phy_table[card_idx];
266
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;
278 dev->weight = 64;
279 #endif
280
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");
284 goto err_out_res;
285 }
286
287 netif_carrier_on(dev);
288 return 0;
289
290 err_out_res:
291 pci_release_regions(pdev);
292 err_out_disable:
293 pci_disable_device(pdev);
294 pci_set_drvdata(pdev, NULL);
295 kfree(dev);
296
297 return err;
298 }
299
300 static void __devexit r6040_remove_one (struct pci_dev *pdev)
301 {
302 struct net_device *dev = pci_get_drvdata(pdev);
303
304 unregister_netdev(dev);
305 pci_release_regions(pdev);
306 kfree(dev);
307 pci_disable_device(pdev);
308 pci_set_drvdata(pdev, NULL);
309 }
310
311 static int
312 r6040_open(struct net_device *dev)
313 {
314 struct r6040_private *lp = dev->priv;
315 int i;
316
317 RDC_DBUG("r6040_open()", 0);
318
319 /* Request IRQ and Register interrupt handler */
320 i = request_irq(dev->irq, &r6040_interrupt, SA_SHIRQ, dev->name, dev);
321 if (i) return i;
322
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;
326
327 r6040_up(dev);
328
329 netif_start_queue(dev);
330
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);
337
338 return 0;
339 }
340
341 static void
342 r6040_tx_timeout (struct net_device *dev)
343 {
344 struct r6040_private *lp = dev->priv;
345 //int ioaddr = dev->base_addr;
346 //struct r6040_descriptor *descptr = lp->tx_remove_ptr;
347
348 RDC_DBUG("r6040_tx_timeout()", 0);
349
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);
355
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));
357
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);
359 }
360
361
362 static int
363 r6040_start_xmit(struct sk_buff *skb, struct net_device *dev)
364 {
365 struct r6040_private *lp = dev->priv;
366 struct r6040_descriptor *descptr;
367 int ioaddr = dev->base_addr;
368 unsigned long flags;
369
370 RDC_DBUG("r6040_start_xmit()", 0);
371
372 if (skb == NULL) /* NULL skb directly return */
373 return 0;
374 if (skb->len >= MAX_BUF_SIZE) { /* Packet too long, drop it */
375 dev_kfree_skb(skb);
376 return 0;
377 }
378
379 /* Critical Section */
380 spin_lock_irqsave(&lp->lock, flags);
381
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 ");
386 return 1;
387 }
388
389 /* Statistic Counter */
390 lp->stats.tx_packets++;
391 lp->stats.tx_bytes += skb->len;
392
393 /* Set TX descriptor & Transmit it */
394 lp->tx_free_desc--;
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;
403
404 #if RDC_DEBUG
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);
406 #endif
407
408 /* If no tx resource, stop */
409 if (!lp->tx_free_desc)
410 netif_stop_queue(dev);
411
412 dev->trans_start = jiffies;
413 spin_unlock_irqrestore(&lp->lock, flags);
414 return 0;
415 }
416
417 /* The RDC interrupt handler. */
418 static irqreturn_t
419 r6040_interrupt(int irq, void *dev_id)
420 {
421 struct net_device *dev = dev_id;
422 struct r6040_private *lp;
423 struct r6040_descriptor *descptr;
424 struct sk_buff *skb_ptr;
425 int ioaddr, status;
426 unsigned long flags;
427 #ifdef CONFIG_R6040_NAPI
428 int handled = 1;
429 #else
430 int handled = 0;
431 #endif
432
433 RDC_DBUG("r6040_interrupt()", 0);
434 if (dev == NULL) {
435 printk (KERN_ERR DRV_NAME ": INT() unknown device.\n");
436 return IRQ_RETVAL(handled);
437 }
438
439 lp = (struct r6040_private *)dev->priv;
440 spin_lock_irqsave(&lp->lock, flags);
441
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 */
446
447
448 #ifdef CONFIG_R6040_NAPI
449
450 if(netif_rx_schedule_prep(dev))
451 {
452 NAPI_status = status ;
453 __netif_rx_schedule(dev);
454 }
455
456 spin_unlock_irqrestore(&lp->lock, flags);
457 return IRQ_RETVAL(handled);
458 #else
459 /* TX interrupt request */
460 if (status & 0x10) {
461 handled = 1;
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 */
470 lp->tx_free_desc++;
471 }
472 lp->tx_remove_ptr = descptr;
473 if (lp->tx_free_desc) netif_wake_queue(dev);
474 }
475
476 /* RX interrupt request */
477 if (status & 0x01) {
478 handled = 1;
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;
484 skb_ptr->dev = dev;
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 */
492 lp->rx_free_desc--;
493 }
494 lp->rx_remove_ptr = descptr;
495 }
496
497 /* Allocate new RX buffer */
498 if (lp->rx_free_desc < RX_DCNT) rx_buf_alloc(lp,dev);
499
500 outw(R6040_INT_MASK, ioaddr + 0x40); /* TX/RX interrupt enable */
501 spin_unlock_irqrestore(&lp->lock, flags);
502 #endif
503 return IRQ_RETVAL(handled);
504 }
505
506
507 static struct net_device_stats *
508 r6040_get_stats(struct net_device *dev)
509 {
510 struct r6040_private *lp = dev->priv;
511
512 RDC_DBUG("r6040_get_stats()", 0);
513 return &lp->stats;
514 }
515
516 /*
517 * Set or clear the multicast filter for this adaptor.
518 */
519 static void
520 set_multicast_list(struct net_device *dev)
521 {
522 struct r6040_private *lp = dev->priv;
523 struct dev_mc_list *mcptr;
524 int ioaddr = dev->base_addr;
525 u16 *adrp, i;
526 unsigned long flags;
527
528 RDC_DBUG("set_multicast_list()", 0);
529
530 /* MAC Address */
531 adrp = (u16 *) dev->dev_addr;
532 outw(adrp[0], ioaddr + 0x68);
533 outw(adrp[1], ioaddr + 0x6A);
534 outw(adrp[2], ioaddr + 0x6C);
535
536
537 #if RDC_DEBUG
538 printk("MAC ADDR: %04x %04x %04x\n", adrp[0], adrp[1], adrp[2]);
539 #endif
540
541 /* Promiscous Mode */
542 spin_lock_irqsave(&lp->lock, flags);
543 i = inw(ioaddr) & ~0x0120; /* Clear AMCP & PROM */
544 if (dev->flags & IFF_PROMISC)
545 {
546 i |= 0x0020;
547 lp->mcr0 |= 0x0020 ;
548 }
549 if (dev->mc_count > 4) i |= 0x0020; /* Too many multicast address */
550 outw(i, ioaddr);
551 spin_unlock_irqrestore(&lp->lock, flags);
552
553 /* Multicast Address */
554 if (dev->mc_count > 4) /* Wait to do: Hash Table for multicast */
555 return;
556
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);
563 mcptr = mcptr->next;
564 #if RDC_DEBUG
565 printk("M_ADDR: %04x %04x %04x\n", adrp[0], adrp[1], adrp[2]);
566 #endif
567 }
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);
572 }
573 }
574
575 static void netdev_get_drvinfo (struct net_device *dev, struct ethtool_drvinfo *info)
576 {
577 struct r6040_private *rp = dev->priv;
578
579 strcpy (info->driver, DRV_NAME);
580 strcpy (info->version, DRV_VERSION);
581 strcpy (info->bus_info, pci_name(rp->pdev));
582 }
583
584 static struct ethtool_ops netdev_ethtool_ops = {
585 .get_drvinfo = netdev_get_drvinfo,
586 };
587
588 static int
589 r6040_close(struct net_device *dev)
590 {
591 struct r6040_private *lp = dev->priv;
592
593 RDC_DBUG("r6040_close()", 0);
594
595 /* deleted timer */
596 del_timer_sync(&lp->timer);
597
598 spin_lock_irq(&lp->lock);
599
600 netif_stop_queue(dev);
601
602 r6040_down(dev);
603
604 spin_unlock_irq(&lp->lock);
605
606 return 0;
607 }
608
609 /**
610 Stop RDC MAC and Free the allocated resource
611 */
612 static void r6040_down(struct net_device *dev)
613 {
614 struct r6040_private *lp = dev->priv;
615 int i;
616 int ioaddr = dev->base_addr;
617
618 RDC_DBUG("r6040_down()", 0);
619
620 /* Stop MAC */
621 outw(0x0000, ioaddr + 0x40); /* Mask Off Interrupt */
622 outw(0x0001, ioaddr + 0x04); /* Reset RDC MAC */
623 i = 0;
624 do{}while((i++ < 2048) && (inw(ioaddr + 0x04) & 0x1));
625
626 free_irq(dev->irq, dev);
627
628 /* Free RX buffer */
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;
634 }
635 lp->rx_insert_ptr = lp->rx_insert_ptr->vndescp;
636 }
637
638 /* Free TX buffer */
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;
644 }
645 lp->tx_insert_ptr = lp->tx_insert_ptr->vndescp;
646 }
647
648 /* Free Descriptor memory */
649 pci_free_consistent(lp->pdev, ALLOC_DESC_SIZE, lp->desc_pool, lp->desc_dma);
650 }
651
652
653
654 #ifdef CONFIG_R6040_NAPI
655 static int r6040_poll(struct net_device *dev, int *budget)
656 {
657 struct r6040_private *lp;
658 struct r6040_descriptor *descptr;
659 struct sk_buff *skb_ptr;
660 int ioaddr, status;
661 unsigned long flags;
662
663
664 ioaddr = dev->base_addr;
665 lp = (struct r6040_private *)dev->priv;
666 unsigned long rx_work = dev->quota ;
667 unsigned long rx ;
668
669
670 #if 1
671 /* TX interrupt request */
672 if (NAPI_status & 0x10) {
673
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 */
682 lp->tx_free_desc++;
683 }
684 lp->tx_remove_ptr = descptr;
685 if (lp->tx_free_desc) netif_wake_queue(dev);
686 }
687 #endif
688 #if 1
689 /* RX interrupt request */
690 if (NAPI_status & 0x01) {
691
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;
697 skb_ptr->dev = dev;
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 */
705 lp->rx_free_desc--;
706 }
707 lp->rx_remove_ptr = descptr;
708
709 }
710 /* Allocate new RX buffer */
711 if (lp->rx_free_desc < RX_DCNT) rx_buf_alloc(lp,dev);
712
713 local_irq_disable();
714 netif_rx_complete(dev);
715 outw(R6040_INT_MASK,ioaddr + 0x40);
716 local_irq_enable();
717 return 0;
718 #endif
719 }
720 #endif
721
722 /* Init RDC MAC */
723 static void r6040_up(struct net_device *dev)
724 {
725 struct r6040_private *lp = dev->priv;
726 struct r6040_descriptor *descptr;
727 int i;
728 int ioaddr = dev->base_addr;
729 u32 tmp_addr;
730 dma_addr_t desc_dma, start_dma;
731
732 RDC_DBUG("r6040_up()", 0);
733
734 /* Initilize */
735 lp->tx_free_desc = TX_DCNT;
736 lp->rx_free_desc = 0;
737
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;
744
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);
754 }
755 (descptr - 1)->ndesc = cpu_to_le32(start_dma);
756 (descptr - 1)->vndescp = lp->tx_insert_ptr;
757
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);
766 }
767 (descptr - 1)->ndesc = cpu_to_le32(start_dma);
768 (descptr - 1)->vndescp = lp->rx_insert_ptr;
769
770 /* Allocate buffer for RX descriptor */
771 rx_buf_alloc(lp,dev);
772
773 #if RDC_DEBUG
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;
778 }
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;
783 }
784 #endif
785
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);
790 udelay(5000);
791
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);
801
802 /* Buffer Size Register */
803 outw(MAX_BUF_SIZE, ioaddr+0x18);
804
805 /* PHY Mode Check */
806 phy_write(ioaddr, lp->phy_addr, 4, PHY_CAP);
807 phy_write(ioaddr, lp->phy_addr, 0, PHY_MODE);
808
809 if (PHY_MODE == 0x3100)
810 lp->phy_mode = phy_mode_chk(dev);
811 else lp->phy_mode = (PHY_MODE & 0x0100) ? 0x8000:0x0;
812
813 /* MAC Bus Control Register */
814 outw(MBCR_DEFAULT, ioaddr+0x8);
815
816 /* MAC TX/RX Enable */
817 lp->mcr0 |= lp->phy_mode;
818 outw(lp->mcr0, ioaddr);
819
820 /* Interrupt Mask Register */
821 outw(R6040_INT_MASK, ioaddr + 0x40);
822 }
823
824 /*
825 A periodic timer routine
826 Polling PHY Chip Link Status
827 */
828 static void r6040_timer(unsigned long data)
829 {
830 struct net_device *dev=(struct net_device *)data;
831 struct r6040_private *lp = dev->priv;
832 u16 ioaddr = dev->base_addr, phy_mode;
833
834 RDC_DBUG("r6040_timer()", 0);
835
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;
840
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));
846 }
847
848 /* Debug */
849 // printk("<RDC> Timer: CR0 %x CR40 %x CR3C %x\n", inw(ioaddr), inw(ioaddr+0x40), inw(ioaddr+0x3c));
850
851 /* Timer active again */
852 lp->timer.expires = TIMER_WUT;
853 add_timer(&lp->timer);
854 }
855
856 /* Allocate skb buffer for rx descriptor */
857 static void rx_buf_alloc(struct r6040_private *lp,struct net_device *dev)
858 {
859 struct r6040_descriptor *descptr;
860 int ioaddr = dev->base_addr ;
861
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;
870 lp->rx_free_desc++;
871 outw(lp->mcr0 | 0x0002, ioaddr); //Trigger Rx DMA
872 }
873 lp->rx_insert_ptr = descptr;
874 }
875
876 /* Status of PHY CHIP */
877 static int phy_mode_chk(struct net_device *dev)
878 {
879 struct r6040_private *lp = dev->priv;
880 int ioaddr = dev->base_addr, phy_dat;
881
882 RDC_DBUG("phy_mode_chk()", 0);
883
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 */
887
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;
895 else phy_dat = 0;
896 } else {
897 /* Force Mode */
898 phy_dat = phy_read(ioaddr, lp->phy_addr, 0);
899 if (phy_dat & 0x100) phy_dat = 0x8000;
900 else phy_dat = 0x0000;
901 }
902
903 return phy_dat;
904 };
905
906 /* Read a word data from PHY Chip */
907 static int phy_read(int ioaddr, int phy_addr, int reg_idx)
908 {
909 int i = 0;
910
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) );
914
915 return inw(ioaddr + 0x24);
916 }
917
918 /* Write a word data from PHY Chip */
919 static void phy_write(int ioaddr, int phy_addr, int reg_idx, int dat)
920 {
921 int i = 0;
922
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) );
927 }
928
929 enum {
930 RDC_6040 = 0
931 };
932
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 */
937 };
938 MODULE_DEVICE_TABLE(pci, r6040_pci_tbl);
939
940 static struct pci_driver r6040_driver = {
941 .name = "r6040",
942 .id_table = r6040_pci_tbl,
943 .probe = r6040_init_one,
944 .remove = __devexit_p(r6040_remove_one),
945 };
946
947
948 static int __init r6040_init (void)
949 {
950 RDC_DBUG("r6040_init()", 0);
951
952 printk(version);
953 printed_version = 1;
954
955 return pci_module_init (&r6040_driver);
956 }
957
958
959 static void __exit r6040_cleanup (void)
960 {
961 RDC_DBUG("r6040_cleanup()", 0);
962 pci_unregister_driver (&r6040_driver);
963 }
964
965 module_init(r6040_init);
966 module_exit(r6040_cleanup);
967
968
969 /*
970 * Local variables:
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`"
972 * c-indent-level: 4
973 * c-basic-offset: 4
974 * tab-width: 4
975 * End:
976 */
This page took 0.093831 seconds and 5 git commands to generate.