add broadcom b44 driver support
[openwrt.git] / target / linux / rdc / 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 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)
21 in set_multicast_list
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
29 Changed rdc to r6040
30 12-22-2004 Sten Init MAC MBCR register=0x012A
31 PHY_CAP = 0x01E1
32
33 Need to Do LIst:
34 1. If multicast address more than 4, use the multicast address hash
35 */
36
37 #define DRV_NAME "r6040"
38 #define DRV_VERSION "0.13"
39 #define DRV_RELDATE "24Aug2006"
40
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 */
46
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 */
50
51 /* RDC MAC ID */
52 #define RDC_MAC_ID 0x6040
53
54 /* RDC MAC I/O Size */
55 #define R6040_IO_SIZE 256
56
57 /* RDC Chip PCI Command */
58 #define R6040_PCI_CMD 0x0005 /* IO, Master */
59
60 /* MAX RDC MAC */
61 #define MAX_MAC 2
62
63 /* MAC setting */
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 */
69
70 /* PHY settings */
71 #define ICPLUS_PHY_ID 0x0243
72
73 /* Debug enable or not */
74 #define RDC_DEBUG 0
75
76 #if RDC_DEBUG > 1
77 #define RDC_DBUG(msg, value) printk(KERN_ERR "%s %x\n", msg, value);
78 #else
79 #define RDC_DBUG(msg, value)
80 #endif
81
82
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>
87 #endif
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>
105
106 #include <asm/processor.h>
107 #include <asm/bitops.h>
108 #include <asm/io.h>
109 #include <asm/irq.h>
110 #include <asm/uaccess.h>
111
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");
116 #else
117 MODULE_DESCRIPTION("RDC R6040 PCI FastEthernet Driver");
118 #endif
119
120 #define R6040_INT_MASK 0x0011
121
122 struct r6040_descriptor {
123 u16 status, len; /* 0-3 */
124 u32 buf; /* 4-7 */
125 u32 ndesc; /* 8-B */
126 u32 rev1; /* C-F */
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)));
132
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;
138
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;
144 u16 mcr0, mcr1;
145 dma_addr_t desc_dma;
146 char *desc_pool;
147 u16 switch_sig;
148 };
149
150 struct r6040_chip_info {
151 const char *name;
152 u16 pci_flags;
153 int io_size;
154 int drv_flags;
155 };
156
157 #ifdef CONFIG_R6040_NAPI
158 static int NAPI_status;
159 #endif
160
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";
165 #else
166 static char version[] __devinitdata =
167 KERN_INFO DRV_NAME ": RDC R6040 net driver, version "DRV_VERSION " (" DRV_RELDATE ")\n";
168 #endif
169 static struct r6040_chip_info r6040_chip_info[] __devinitdata =
170 {
171 { "RDC R6040 Knight", R6040_PCI_CMD, R6040_IO_SIZE, 0}
172 };
173 static char *parent;
174
175 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
176 static int NUM_MAC_TABLE = 2 ;
177 module_param(parent, charp, 0444);
178 #else
179 MODULE_PARM(parent, "s");
180 #endif
181 MODULE_PARM_DESC(parent, "Parent network device name");
182
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}
187 };
188
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);
193 #else
194 MODULE_PARM(adr_table, "2-4i");
195 #endif
196 MODULE_PARM_DESC(adr_table, "MAC Address (assigned)");
197
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);
210
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);
217 #endif
218
219
220 static int __devinit r6040_init_one (struct pci_dev *pdev,
221 const struct pci_device_id *ent)
222 {
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;
228
229 RDC_DBUG("r6040_init_one()", 0);
230
231 if (printed_version++)
232 printk(KERN_INFO "%s\n", version);
233
234 err = pci_enable_device(pdev);
235 if (err)
236 return err;
237
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");
241 return -ENODEV;
242 }
243
244 /* IO Size check */
245 io_size = r6040_chip_info[chip_id].io_size;
246 if (pci_resource_len (pdev, 0) < io_size) {
247 return -ENODEV;
248 }
249
250 ioaddr = pci_resource_start (pdev, 0); /* IO map base address */
251 pci_set_master(pdev);
252
253 dev = alloc_etherdev(sizeof(struct r6040_private));
254 if (dev == NULL)
255 return -ENOMEM;
256 SET_MODULE_OWNER(dev);
257
258 if (pci_request_regions(pdev, DRV_NAME)) {
259 printk(KERN_ERR DRV_NAME ": Failed to request PCI regions\n");
260 err = -ENODEV;
261 goto err_out_disable;
262 }
263
264 /* Init system & device */
265 lp = dev->priv;
266 dev->base_addr = ioaddr;
267 dev->irq = pdev->irq;
268
269 spin_lock_init(&lp->lock);
270 pci_set_drvdata(pdev, dev);
271
272 /* Set MAC address */
273 card_idx++;
274 memcpy(dev->dev_addr, (u8 *)&adr_table[card_idx][0], 6);
275
276 /* Link new device into r6040_root_dev */
277 lp->pdev = pdev;
278
279 /* Init RDC private data */
280 lp->mcr0 = 0x1002;
281 lp->phy_addr = phy_table[card_idx];
282 lp->switch_sig = 0;
283
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;
296 dev->weight = 64;
297 #endif
298
299 /* Register net device. After this dev->name assign */
300 err = register_netdev(dev);
301 if (err) {
302 printk(KERN_ERR DRV_NAME ": Failed to register net device\n");
303 goto err_out_res;
304 }
305
306 netif_carrier_on(dev);
307 return 0;
308
309 err_out_res:
310 pci_release_regions(pdev);
311 err_out_disable:
312 pci_disable_device(pdev);
313 pci_set_drvdata(pdev, NULL);
314 kfree(dev);
315
316 return err;
317 }
318
319 static void __devexit r6040_remove_one (struct pci_dev *pdev)
320 {
321 struct net_device *dev = pci_get_drvdata(pdev);
322
323 unregister_netdev(dev);
324 pci_release_regions(pdev);
325 kfree(dev);
326 pci_disable_device(pdev);
327 pci_set_drvdata(pdev, NULL);
328 }
329
330 static int
331 r6040_open(struct net_device *dev)
332 {
333 struct r6040_private *lp = dev->priv;
334 int i;
335
336 RDC_DBUG("r6040_open()", 0);
337
338 /* Request IRQ and Register interrupt handler */
339 i = request_irq(dev->irq, &r6040_interrupt, SA_SHIRQ, dev->name, dev);
340 if (i) return i;
341
342 /* Allocate Descriptor memory */
343 lp->desc_pool = pci_alloc_consistent(lp->pdev, ALLOC_DESC_SIZE, &lp->desc_dma);
344 if (!lp->desc_pool)
345 return -ENOMEM;
346
347 r6040_up(dev);
348
349 netif_start_queue(dev);
350
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);
358 }
359 return 0;
360 }
361
362 static void
363 r6040_tx_timeout (struct net_device *dev)
364 {
365 struct r6040_private *lp = dev->priv;
366 /* int ioaddr = dev->base_addr;
367 struct r6040_descriptor *descptr = lp->tx_remove_ptr; */
368
369 RDC_DBUG("r6040_tx_timeout()", 0);
370
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);
376
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));
378
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);
380 }
381
382
383 static int
384 r6040_start_xmit(struct sk_buff *skb, struct net_device *dev)
385 {
386 struct r6040_private *lp = dev->priv;
387 struct r6040_descriptor *descptr;
388 int ioaddr = dev->base_addr;
389 unsigned long flags;
390
391 RDC_DBUG("r6040_start_xmit()", 0);
392
393 if (skb == NULL) /* NULL skb directly return */
394 return 0;
395 if (skb->len >= MAX_BUF_SIZE) { /* Packet too long, drop it */
396 dev_kfree_skb(skb);
397 return 0;
398 }
399
400 /* Critical Section */
401 spin_lock_irqsave(&lp->lock, flags);
402
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 ");
407 return 1;
408 }
409
410 /* Statistic Counter */
411 lp->stats.tx_packets++;
412 lp->stats.tx_bytes += skb->len;
413
414 /* Set TX descriptor & Transmit it */
415 lp->tx_free_desc--;
416 descptr = lp->tx_insert_ptr;
417 if (skb->len < 0x3c)
418 descptr->len = 0x3c;
419 else
420 descptr->len = skb->len;
421
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;
427
428 #if RDC_DEBUG
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);
430 #endif
431
432 /* If no tx resource, stop */
433 if (!lp->tx_free_desc)
434 netif_stop_queue(dev);
435
436 dev->trans_start = jiffies;
437 spin_unlock_irqrestore(&lp->lock, flags);
438 return 0;
439 }
440
441 /* The RDC interrupt handler. */
442 static irqreturn_t
443 r6040_interrupt(int irq, void *dev_id)
444 {
445 struct net_device *dev = dev_id;
446 struct r6040_private *lp;
447 struct r6040_descriptor *descptr;
448 struct sk_buff *skb_ptr;
449 int ioaddr, status;
450 unsigned long flags;
451 #ifdef CONFIG_R6040_NAPI
452 int handled = 1;
453 #else
454 int handled = 0;
455 #endif
456
457 RDC_DBUG("r6040_interrupt()", 0);
458 if (dev == NULL) {
459 printk (KERN_ERR DRV_NAME ": INT() unknown device.\n");
460 return IRQ_RETVAL(handled);
461 }
462
463 lp = (struct r6040_private *)dev->priv;
464 spin_lock_irqsave(&lp->lock, flags);
465
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 */
470
471
472 #ifdef CONFIG_R6040_NAPI
473 if (netif_rx_schedule_prep(dev)) {
474 NAPI_status = status;
475 __netif_rx_schedule(dev);
476 }
477
478 spin_unlock_irqrestore(&lp->lock, flags);
479 return IRQ_RETVAL(handled);
480 #else
481 /* TX interrupt request */
482 if (status & 0x10) {
483 handled = 1;
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 */
493 lp->tx_free_desc++;
494 }
495 lp->tx_remove_ptr = descptr;
496 if (lp->tx_free_desc)
497 netif_wake_queue(dev);
498 }
499
500 /* RX interrupt request */
501 if (status & 0x01) {
502 handled = 1;
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;
508 skb_ptr->dev = dev;
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 */
516 lp->rx_free_desc--;
517 }
518 lp->rx_remove_ptr = descptr;
519 }
520
521 /* Allocate new RX buffer */
522 if (lp->rx_free_desc < RX_DCNT) rx_buf_alloc(lp,dev);
523
524 outw(R6040_INT_MASK, ioaddr + 0x40); /* TX/RX interrupt enable */
525 spin_unlock_irqrestore(&lp->lock, flags);
526 #endif
527 return IRQ_RETVAL(handled);
528 }
529
530
531 static struct net_device_stats *
532 r6040_get_stats(struct net_device *dev)
533 {
534 struct r6040_private *lp = dev->priv;
535
536 RDC_DBUG("r6040_get_stats()", 0);
537 return &lp->stats;
538 }
539
540 /*
541 * Set or clear the multicast filter for this adaptor.
542 */
543 static void
544 set_multicast_list(struct net_device *dev)
545 {
546 struct r6040_private *lp = dev->priv;
547 struct dev_mc_list *mcptr;
548 int ioaddr = dev->base_addr;
549 u16 *adrp, i;
550 unsigned long flags;
551
552 RDC_DBUG("set_multicast_list()", 0);
553
554 /* MAC Address */
555 adrp = (u16 *) dev->dev_addr;
556 outw(adrp[0], ioaddr + 0x68);
557 outw(adrp[1], ioaddr + 0x6A);
558 outw(adrp[2], ioaddr + 0x6C);
559
560
561 #if RDC_DEBUG
562 printk("MAC ADDR: %04x %04x %04x\n", adrp[0], adrp[1], adrp[2]);
563 #endif
564
565 /* Promiscous Mode */
566 spin_lock_irqsave(&lp->lock, flags);
567 i = inw(ioaddr) & ~0x0120; /* Clear AMCP & PROM */
568 if (dev->flags & IFF_PROMISC) {
569 i |= 0x0020;
570 lp->mcr0 |= 0x0020;
571 }
572 if (dev->mc_count > 4)
573 i |= 0x0020; /* Too many multicast address */
574
575 outw(i, ioaddr);
576 spin_unlock_irqrestore(&lp->lock, flags);
577
578 /* Multicast Address */
579 if (dev->mc_count > 4) /* Wait to do: Hash Table for multicast */
580 return;
581
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);
588 mcptr = mcptr->next;
589 #if RDC_DEBUG
590 printk("M_ADDR: %04x %04x %04x\n", adrp[0], adrp[1], adrp[2]);
591 #endif
592 }
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);
597 }
598 }
599
600 static void netdev_get_drvinfo (struct net_device *dev, struct ethtool_drvinfo *info)
601 {
602 struct r6040_private *rp = dev->priv;
603
604 strcpy (info->driver, DRV_NAME);
605 strcpy (info->version, DRV_VERSION);
606 strcpy (info->bus_info, pci_name(rp->pdev));
607 }
608
609 static struct ethtool_ops netdev_ethtool_ops = {
610 .get_drvinfo = netdev_get_drvinfo,
611 };
612
613 static int
614 r6040_close(struct net_device *dev)
615 {
616 struct r6040_private *lp = dev->priv;
617
618 RDC_DBUG("r6040_close()", 0);
619
620 /* deleted timer */
621 del_timer_sync(&lp->timer);
622
623 spin_lock_irq(&lp->lock);
624
625 netif_stop_queue(dev);
626
627 r6040_down(dev);
628
629 spin_unlock_irq(&lp->lock);
630
631 return 0;
632 }
633
634 static int netdev_ioctl (struct net_device *dev, struct ifreq *rq, int cmd)
635 {
636 struct r6040_private *lp = dev->priv;
637
638 RDC_DBUG("netdev_ioctl()", 0);
639
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;
643 unsigned int val;
644
645 data = (unsigned long *)rq->ifr_data;
646 if (copy_from_user(args, data, 4*sizeof(unsigned long)))
647 return -EFAULT;
648
649 /* port priority */
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 */
654
655 }
656 return -EOPNOTSUPP;
657 }
658
659 /**
660 Stop RDC MAC and Free the allocated resource
661 */
662 static void r6040_down(struct net_device *dev)
663 {
664 struct r6040_private *lp = dev->priv;
665 int i;
666 int ioaddr = dev->base_addr;
667
668 RDC_DBUG("r6040_down()", 0);
669
670 /* Stop MAC */
671 outw(0x0000, ioaddr + 0x40); /* Mask Off Interrupt */
672 outw(0x0001, ioaddr + 0x04); /* Reset RDC MAC */
673 i = 0;
674 do{}while((i++ < 2048) && (inw(ioaddr + 0x04) & 0x1));
675
676 free_irq(dev->irq, dev);
677
678 /* Free RX buffer */
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;
684 }
685 lp->rx_insert_ptr = lp->rx_insert_ptr->vndescp;
686 }
687
688 /* Free TX buffer */
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;
694 }
695 lp->tx_insert_ptr = lp->tx_insert_ptr->vndescp;
696 }
697
698 /* Free Descriptor memory */
699 pci_free_consistent(lp->pdev, ALLOC_DESC_SIZE, lp->desc_pool, lp->desc_dma);
700 }
701
702
703
704 #ifdef CONFIG_R6040_NAPI
705 static int r6040_poll(struct net_device *dev, int *budget)
706 {
707 struct r6040_private *lp;
708 struct r6040_descriptor *descptr;
709 struct sk_buff *skb_ptr;
710 int ioaddr, status;
711 unsigned long flags;
712
713
714 ioaddr = dev->base_addr;
715 lp = (struct r6040_private *)dev->priv;
716 unsigned long rx_work = dev->quota;
717 unsigned long rx;
718 #if 1
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 */
725
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 */
731 lp->tx_free_desc++;
732 }
733 lp->tx_remove_ptr = descptr;
734 if (lp->tx_free_desc) netif_wake_queue(dev);
735 }
736 #endif
737 #if 1
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;
746 skb_ptr->dev = dev;
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 */
754 lp->rx_free_desc--;
755 }
756 lp->rx_remove_ptr = descptr;
757 }
758 /* Allocate new RX buffer */
759 if (lp->rx_free_desc < RX_DCNT)
760 rx_buf_alloc(lp, dev);
761
762 local_irq_disable();
763 netif_rx_complete(dev);
764 outw(R6040_INT_MASK, ioaddr + 0x40);
765 local_irq_enable();
766 return 0;
767 #endif
768 }
769 #endif
770
771 /* Init RDC MAC */
772 static void r6040_up(struct net_device *dev)
773 {
774 struct r6040_private *lp = dev->priv;
775 struct r6040_descriptor *descptr;
776 int i;
777 int ioaddr = dev->base_addr;
778 u32 tmp_addr;
779 dma_addr_t desc_dma, start_dma;
780
781 RDC_DBUG("r6040_up()", 0);
782
783 /* Initilize */
784 lp->tx_free_desc = TX_DCNT;
785 lp->rx_free_desc = 0;
786
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;
793
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);
803 }
804 (descptr - 1)->ndesc = cpu_to_le32(start_dma);
805 (descptr - 1)->vndescp = lp->tx_insert_ptr;
806
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);
815 }
816 (descptr - 1)->ndesc = cpu_to_le32(start_dma);
817 (descptr - 1)->vndescp = lp->rx_insert_ptr;
818
819 /* Allocate buffer for RX descriptor */
820 rx_buf_alloc(lp, dev);
821
822 #if RDC_DEBUG
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;
827 }
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;
832 }
833 #endif
834
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);
839 udelay(5000);
840
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);
850
851 /* Buffer Size Register */
852 outw(MAX_BUF_SIZE, ioaddr+0x18);
853
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;
857 } else {
858 /* PHY Mode Check */
859 phy_write(ioaddr, lp->phy_addr, 4, PHY_CAP);
860 phy_write(ioaddr, lp->phy_addr, 0, PHY_MODE);
861
862 if (PHY_MODE == 0x3100)
863 lp->phy_mode = phy_mode_chk(dev);
864 else
865 lp->phy_mode = (PHY_MODE & 0x0100) ? 0x8000:0x0;
866 }
867 /* MAC Bus Control Register */
868 outw(MBCR_DEFAULT, ioaddr+0x8);
869
870 /* MAC TX/RX Enable */
871 lp->mcr0 |= lp->phy_mode;
872 outw(lp->mcr0, ioaddr);
873
874 /* set interrupt waiting time and packet numbers */
875 outw(0x0802, ioaddr + 0x0C);
876 outw(0x0802, ioaddr + 0x10);
877
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);
883
884 /* Interrupt Mask Register */
885 outw(R6040_INT_MASK, ioaddr + 0x40);
886 }
887
888 /*
889 A periodic timer routine
890 Polling PHY Chip Link Status
891 */
892 static void r6040_timer(unsigned long data)
893 {
894 struct net_device *dev = (struct net_device *)data;
895 struct r6040_private *lp = dev->priv;
896 u16 ioaddr = dev->base_addr, phy_mode;
897
898 RDC_DBUG("r6040_timer()", 0);
899
900 /* Polling PHY Chip Status */
901 if (PHY_MODE == 0x3100)
902 phy_mode = phy_mode_chk(dev);
903 else
904 phy_mode = (PHY_MODE & 0x0100) ? 0x8000:0x0;
905
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));
911 }
912
913 /* Debug */
914 // printk("<RDC> Timer: CR0 %x CR40 %x CR3C %x\n", inw(ioaddr), inw(ioaddr+0x40), inw(ioaddr+0x3c));
915
916 /* Timer active again */
917 lp->timer.expires = TIMER_WUT;
918 add_timer(&lp->timer);
919 }
920
921 /* Allocate skb buffer for rx descriptor */
922 static void rx_buf_alloc(struct r6040_private *lp, struct net_device *dev)
923 {
924 struct r6040_descriptor *descptr;
925 int ioaddr = dev->base_addr ;
926
927 RDC_DBUG("rx_buf_alloc()", 0);
928
929 descptr = lp->rx_insert_ptr;
930 while (lp->rx_free_desc < RX_DCNT) {
931 descptr->skb_ptr = dev_alloc_skb(MAX_BUF_SIZE);
932
933 if (!descptr->skb_ptr)
934 break;
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;
938 lp->rx_free_desc++;
939 outw(lp->mcr0 | 0x0002, ioaddr); /* Trigger Rx DMA */
940 }
941 lp->rx_insert_ptr = descptr;
942 }
943
944 /* Status of PHY CHIP */
945 static int phy_mode_chk(struct net_device *dev)
946 {
947 struct r6040_private *lp = dev->priv;
948 int ioaddr = dev->base_addr, phy_dat;
949
950 RDC_DBUG("phy_mode_chk()", 0);
951
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 */
956
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);
963 if (phy_dat & 0x140)
964 phy_dat = 0x8000;
965 else
966 phy_dat = 0;
967 } else {
968 /* Force Mode */
969 phy_dat = phy_read(ioaddr, lp->phy_addr, 0);
970 if (phy_dat & 0x100) phy_dat = 0x8000;
971 else phy_dat = 0x0000;
972 }
973
974 return phy_dat;
975 };
976
977 /* Read a word data from PHY Chip */
978 static int phy_read(int ioaddr, int phy_addr, int reg_idx)
979 {
980 int i = 0;
981
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));
985
986 return inw(ioaddr + 0x24);
987 }
988
989 /* Write a word data from PHY Chip */
990 static void phy_write(int ioaddr, int phy_addr, int reg_idx, int dat)
991 {
992 int i = 0;
993
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) );
998 }
999
1000 enum {
1001 RDC_6040 = 0
1002 };
1003
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},*/
1007 {0,}
1008 };
1009 MODULE_DEVICE_TABLE(pci, r6040_pci_tbl);
1010
1011 static struct pci_driver r6040_driver = {
1012 .name = "r6040",
1013 .id_table = r6040_pci_tbl,
1014 .probe = r6040_init_one,
1015 .remove = __devexit_p(r6040_remove_one),
1016 };
1017
1018
1019 static int __init r6040_init (void)
1020 {
1021 RDC_DBUG("r6040_init()", 0);
1022
1023 printk(KERN_INFO "%s\n", version);
1024 printed_version = 1;
1025
1026 if (parent != NULL) {
1027 struct net_device *the_parent = dev_get_by_name(parent);
1028
1029 if (the_parent == NULL) {
1030 printk (KERN_ERR DRV_NAME ": Unknown device \"%s\" specified.\n", parent);
1031 return -EINVAL;
1032 }
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];
1036 }
1037 return pci_register_driver (&r6040_driver);
1038 }
1039
1040
1041 static void __exit r6040_cleanup (void)
1042 {
1043 RDC_DBUG("r6040_cleanup()", 0);
1044 pci_unregister_driver (&r6040_driver);
1045 }
1046
1047 module_init(r6040_init);
1048 module_exit(r6040_cleanup);
This page took 0.103086 seconds and 5 git commands to generate.