root/src/linux/ar531x/linux-2.6.23/drivers/net/ar2313/ar2313.c

Revision 12398, 36.6 kB (checked in by BrainSlayer, 5 months ago)

ar8216 switch support

Line 
1 /*
2  * ar2313.c: Linux driver for the Atheros AR231x Ethernet device.
3  *
4  * Copyright (C) 2004 by Sameer Dekate <sdekate@arubanetworks.com>
5  * Copyright (C) 2006 Imre Kaloz <kaloz@openwrt.org>
6  * Copyright (C) 2006-2007 Felix Fietkau <nbd@openwrt.org>
7  *
8  * Thanks to Atheros for providing hardware and documentation
9  * enabling me to write this driver.
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * Additional credits:
17  *      This code is taken from John Taylor's Sibyte driver and then
18  *      modified for the AR2313.
19  */
20
21 #include <linux/autoconf.h>
22 #include <linux/module.h>
23 #include <linux/version.h>
24 #include <linux/types.h>
25 #include <linux/errno.h>
26 #include <linux/ioport.h>
27 #include <linux/pci.h>
28 #include <linux/netdevice.h>
29 #include <linux/etherdevice.h>
30 #include <linux/skbuff.h>
31 #include <linux/init.h>
32 #include <linux/delay.h>
33 #include <linux/mm.h>
34 #include <linux/highmem.h>
35 #include <linux/sockios.h>
36 #include <linux/pkt_sched.h>
37 #include <linux/compile.h>
38 #include <linux/mii.h>
39 #include <linux/phy.h>
40 #include <linux/ethtool.h>
41 #include <linux/ctype.h>
42 #include <linux/platform_device.h>
43
44 #include <net/sock.h>
45 #include <net/ip.h>
46
47 #include <asm/system.h>
48 #include <asm/io.h>
49 #include <asm/irq.h>
50 #include <asm/byteorder.h>
51 #include <asm/uaccess.h>
52 #include <asm/bootinfo.h>
53
54 #define AR2313_MTU                     1692
55 #define AR2313_PRIOS                   1
56 #define AR2313_QUEUES                  (2*AR2313_PRIOS)
57 #define AR2313_DESCR_ENTRIES           64
58
59 #undef INDEX_DEBUG
60 #define DEBUG     0
61 #define DEBUG_TX  0
62 #define DEBUG_RX  0
63 #define DEBUG_INT 0
64 #define DEBUG_MC  0
65 #define DEBUG_ERR 1
66
67 #ifndef min
68 #define min(a,b)        (((a)<(b))?(a):(b))
69 #endif
70
71 #ifndef SMP_CACHE_BYTES
72 #define SMP_CACHE_BYTES L1_CACHE_BYTES
73 #endif
74
75 #define AR2313_MBOX_SET_BIT  0x8
76
77 #define BOARD_IDX_STATIC        0
78 #define BOARD_IDX_OVERFLOW      -1
79
80 #include "dma.h"
81 #include "ar2313.h"
82
83 /*
84  * New interrupt handler strategy:
85  *
86  * An old interrupt handler worked using the traditional method of
87  * replacing an skbuff with a new one when a packet arrives. However
88  * the rx rings do not need to contain a static number of buffer
89  * descriptors, thus it makes sense to move the memory allocation out
90  * of the main interrupt handler and do it in a bottom half handler
91  * and only allocate new buffers when the number of buffers in the
92  * ring is below a certain threshold. In order to avoid starving the
93  * NIC under heavy load it is however necessary to force allocation
94  * when hitting a minimum threshold. The strategy for alloction is as
95  * follows:
96  *
97  *     RX_LOW_BUF_THRES    - allocate buffers in the bottom half
98  *     RX_PANIC_LOW_THRES  - we are very low on buffers, allocate
99  *                           the buffers in the interrupt handler
100  *     RX_RING_THRES       - maximum number of buffers in the rx ring
101  *
102  * One advantagous side effect of this allocation approach is that the
103  * entire rx processing can be done without holding any spin lock
104  * since the rx rings and registers are totally independent of the tx
105  * ring and its registers.  This of course includes the kmalloc's of
106  * new skb's. Thus start_xmit can run in parallel with rx processing
107  * and the memory allocation on SMP systems.
108  *
109  * Note that running the skb reallocation in a bottom half opens up
110  * another can of races which needs to be handled properly. In
111  * particular it can happen that the interrupt handler tries to run
112  * the reallocation while the bottom half is either running on another
113  * CPU or was interrupted on the same CPU. To get around this the
114  * driver uses bitops to prevent the reallocation routines from being
115  * reentered.
116  *
117  * TX handling can also be done without holding any spin lock, wheee
118  * this is fun! since tx_csm is only written to by the interrupt
119  * handler.
120  */
121
122 /*
123  * Threshold values for RX buffer allocation - the low water marks for
124  * when to start refilling the rings are set to 75% of the ring
125  * sizes. It seems to make sense to refill the rings entirely from the
126  * intrrupt handler once it gets below the panic threshold, that way
127  * we don't risk that the refilling is moved to another CPU when the
128  * one running the interrupt handler just got the slab code hot in its
129  * cache.
130  */
131 #define RX_RING_SIZE            AR2313_DESCR_ENTRIES
132 #define RX_PANIC_THRES          (RX_RING_SIZE/4)
133 #define RX_LOW_THRES            ((3*RX_RING_SIZE)/4)
134 #define CRC_LEN                 4
135 #define RX_OFFSET               2
136
137 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
138 #define VLAN_HDR                4
139 #else
140 #define VLAN_HDR                0
141 #endif
142
143 #define RXBUFF_RESERVE   96
144
145 //#define AR2313_BUFSIZE (((RXBUFF_RESERVE + ETH_HLEN + AR2313_MTU + VLAN_HDR + 4) + 3) & ~3)
146
147 #define AR2313_BUFSIZE          (AR2313_MTU + VLAN_HDR + ETH_HLEN + CRC_LEN + RX_OFFSET)
148
149 #ifdef MODULE
150 MODULE_LICENSE("GPL");
151 MODULE_AUTHOR("Sameer Dekate <sdekate@arubanetworks.com>, Imre Kaloz <kaloz@openwrt.org>, Felix Fietkau <nbd@openwrt.org>");
152 MODULE_DESCRIPTION("AR2313 Ethernet driver");
153 #endif
154
155 #define virt_to_phys(x) ((u32)(x) & 0x1fffffff)
156
157 // prototypes
158 #ifdef TX_TIMEOUT
159 static void ar2313_tx_timeout(struct net_device *dev);
160 #endif
161 static void ar2313_halt(struct net_device *dev);
162 static void rx_tasklet_func(unsigned long data);
163 static void rx_tasklet_cleanup(struct net_device *dev);
164 static void ar2313_multicast_list(struct net_device *dev);
165
166 static int mdiobus_read(struct mii_bus *bus, int phy_addr, int regnum);
167 static int mdiobus_write(struct mii_bus *bus, int phy_addr, int regnum, u16 value);
168 static int mdiobus_reset(struct mii_bus *bus);
169 static int mdiobus_probe (struct net_device *dev);
170 static void ar2313_adjust_link(struct net_device *dev);
171
172 #ifndef ERR
173 #define ERR(fmt, args...) printk("%s: " fmt, __func__, ##args)
174 #endif
175
176
177 int __init ar2313_probe(struct platform_device *pdev)
178 {
179         struct net_device *dev;
180         struct ar2313_private *sp;
181         struct resource *res;
182         unsigned long ar_eth_base;
183         char buf[64];
184
185         dev = alloc_etherdev(sizeof(struct ar2313_private));
186
187         if (dev == NULL) {
188                 printk(KERN_ERR
189                            "ar2313: Unable to allocate net_device structure!\n");
190                 return -ENOMEM;
191         }
192
193         platform_set_drvdata(pdev, dev);
194
195         sp = netdev_priv(dev);
196         sp->dev = dev;
197         sp->cfg = pdev->dev.platform_data;
198
199         sprintf(buf, "eth%d_membase", pdev->id);
200         res = platform_get_resource_byname(pdev, IORESOURCE_MEM, buf);
201         if (!res)
202                 return -ENODEV;
203
204         sp->link = 0;
205         ar_eth_base = res->start;
206         sp->phy = sp->cfg->phy;
207
208         sprintf(buf, "eth%d_irq", pdev->id);
209         dev->irq = platform_get_irq_byname(pdev, buf);
210
211         spin_lock_init(&sp->lock);
212
213         /* initialize func pointers */
214         dev->open = &ar2313_open;
215         dev->stop = &ar2313_close;
216         dev->hard_start_xmit = &ar2313_start_xmit;
217
218         dev->set_multicast_list = &ar2313_multicast_list;
219 #ifdef TX_TIMEOUT
220         dev->tx_timeout = ar2313_tx_timeout;
221         dev->watchdog_timeo = AR2313_TX_TIMEOUT;
222 #endif
223         dev->do_ioctl = &ar2313_ioctl;
224
225         // SAMEER: do we need this?
226 //      dev->features |= NETIF_F_SG | NETIF_F_HIGHDMA;
227         dev->features |= NETIF_F_HIGHDMA;
228 //      dev->features |= NETIF_F_HIGHDMA | NETIF_F_HW_CSUM;
229
230         tasklet_init(&sp->rx_tasklet, rx_tasklet_func, (unsigned long) dev);
231         tasklet_disable(&sp->rx_tasklet);
232
233         sp->eth_regs =
234                 ioremap_nocache(virt_to_phys(ar_eth_base), sizeof(*sp->eth_regs));
235         if (!sp->eth_regs) {
236                 printk("Can't remap eth registers\n");
237                 return (-ENXIO);
238         }
239
240         /*
241          * When there's only one MAC, PHY regs are typically on ENET0,
242          * even though the MAC might be on ENET1.
243          * Needto remap PHY regs separately in this case
244          */
245         if (virt_to_phys(ar_eth_base) == virt_to_phys(sp->phy_regs))
246                 sp->phy_regs = sp->eth_regs;
247         else {
248                 sp->phy_regs =
249                         ioremap_nocache(virt_to_phys(sp->cfg->phy_base),
250                                                         sizeof(*sp->phy_regs));
251                 if (!sp->phy_regs) {
252                         printk("Can't remap phy registers\n");
253                         return (-ENXIO);
254                 }
255         }
256
257         sp->dma_regs =
258                 ioremap_nocache(virt_to_phys(ar_eth_base + 0x1000),
259                                                 sizeof(*sp->dma_regs));
260         dev->base_addr = (unsigned int) sp->dma_regs;
261         if (!sp->dma_regs) {
262                 printk("Can't remap DMA registers\n");
263                 return (-ENXIO);
264         }
265
266         sp->int_regs = ioremap_nocache(virt_to_phys(sp->cfg->reset_base), 4);
267         if (!sp->int_regs) {
268                 printk("Can't remap INTERRUPT registers\n");
269                 return (-ENXIO);
270         }
271
272         strncpy(sp->name, "Atheros AR231x", sizeof(sp->name) - 1);
273         sp->name[sizeof(sp->name) - 1] = '\0';
274         memcpy(dev->dev_addr, sp->cfg->macaddr, 6);
275         sp->board_idx = BOARD_IDX_STATIC;
276
277         if (ar2313_init(dev)) {
278                 /*
279                  * ar2313_init() calls ar2313_init_cleanup() on error.
280                  */
281                 kfree(dev);
282                 return -ENODEV;
283         }
284
285         if (register_netdev(dev)) {
286                 printk("%s: register_netdev failed\n", __func__);
287                 return -1;
288         }
289
290         printk("%s: %s: %02x:%02x:%02x:%02x:%02x:%02x, irq %d\n",
291                    dev->name, sp->name,
292                    dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
293                    dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5], dev->irq);
294
295         sp->mii_bus.priv = dev;
296         sp->mii_bus.read = mdiobus_read;
297         sp->mii_bus.write = mdiobus_write;
298         sp->mii_bus.reset = mdiobus_reset;
299         sp->mii_bus.name = "ar2313_eth_mii";
300         sp->mii_bus.id = pdev->id;
301         sp->mii_bus.irq = kmalloc(sizeof(int), GFP_KERNEL);
302         *sp->mii_bus.irq = PHY_POLL;
303
304         mdiobus_register(&sp->mii_bus);
305
306         if (mdiobus_probe(dev) != 0) {
307                 printk(KERN_ERR "ar2313: mdiobus_probe failed");
308                 rx_tasklet_cleanup(dev);
309                 ar2313_init_cleanup(dev);
310                 unregister_netdev(dev);
311                 kfree(dev);
312         } else {
313                 /* start link poll timer */
314                 ar2313_setup_timer(dev);
315         }
316
317         return 0;
318 }
319
320 #if 0
321 static void ar2313_dump_regs(struct net_device *dev)
322 {
323         unsigned int *ptr, i;
324         struct ar2313_private *sp = netdev_priv(dev);
325
326         ptr = (unsigned int *) sp->eth_regs;
327         for (i = 0; i < (sizeof(ETHERNET_STRUCT) / sizeof(unsigned int));
328                  i++, ptr++) {
329                 printk("ENET: %08x = %08x\n", (int) ptr, *ptr);
330         }
331
332         ptr = (unsigned int *) sp->dma_regs;
333         for (i = 0; i < (sizeof(DMA) / sizeof(unsigned int)); i++, ptr++) {
334                 printk("DMA: %08x = %08x\n", (int) ptr, *ptr);
335         }
336
337         ptr = (unsigned int *) sp->int_regs;
338         for (i = 0; i < (sizeof(INTERRUPT) / sizeof(unsigned int)); i++, ptr++) {
339                 printk("INT: %08x = %08x\n", (int) ptr, *ptr);
340         }
341
342         for (i = 0; i < AR2313_DESCR_ENTRIES; i++) {
343                 ar2313_descr_t *td = &sp->tx_ring[i];
344                 printk("Tx desc %2d: %08x %08x %08x %08x\n", i,
345                            td->status, td->devcs, td->addr, td->descr);
346         }
347 }
348 #endif
349
350 #ifdef TX_TIMEOUT
351 static void ar2313_tx_timeout(struct net_device *dev)
352 {
353         struct ar2313_private *sp = netdev_priv(dev);
354         unsigned long flags;
355
356 #if DEBUG_TX
357         printk("Tx timeout\n");
358 #endif
359         spin_lock_irqsave(&sp->lock, flags);
360         ar2313_restart(dev);
361         spin_unlock_irqrestore(&sp->lock, flags);
362 }
363 #endif
364
365 #if DEBUG_MC
366 static void printMcList(struct net_device *dev)
367 {
368         struct dev_mc_list *list = dev->mc_list;
369         int num = 0, i;
370         while (list) {
371                 printk("%d MC ADDR ", num);
372                 for (i = 0; i < list->dmi_addrlen; i++) {
373                         printk(":%02x", list->dmi_addr[i]);
374                 }
375                 list = list->next;
376                 printk("\n");
377         }
378 }
379 #endif
380
381 /*
382  * Set or clear the multicast filter for this adaptor.
383  * THIS IS ABSOLUTE CRAP, disabled
384  */
385 static void ar2313_multicast_list(struct net_device *dev)
386 {
387         /*
388          * Always listen to broadcasts and
389          * treat IFF bits independently
390          */
391         struct ar2313_private *sp = netdev_priv(dev);
392         unsigned int recognise;
393
394         recognise = sp->eth_regs->mac_control;
395
396         if (dev->flags & IFF_PROMISC) { /* set promiscuous mode */
397                 recognise |= MAC_CONTROL_PR;
398         } else {
399                 recognise &= ~MAC_CONTROL_PR;
400         }
401
402         if ((dev->flags & IFF_ALLMULTI) || (dev->mc_count > 15)) {
403 #if DEBUG_MC
404                 printMcList(dev);
405                 printk("%s: all MULTICAST mc_count %d\n", __FUNCTION__,
406                            dev->mc_count);
407 #endif
408                 recognise |= MAC_CONTROL_PM;    /* all multicast */
409         } else if (dev->mc_count > 0) {
410 #if DEBUG_MC
411                 printMcList(dev);
412                 printk("%s: mc_count %d\n", __FUNCTION__, dev->mc_count);
413 #endif
414                 recognise |= MAC_CONTROL_PM;    /* for the time being */
415         }
416 #if DEBUG_MC
417         printk("%s: setting %08x to %08x\n", __FUNCTION__, (int) sp->eth_regs,
418                    recognise);
419 #endif
420
421         sp->eth_regs->mac_control = recognise;
422 }
423
424 static void rx_tasklet_cleanup(struct net_device *dev)
425 {
426         struct ar2313_private *sp = netdev_priv(dev);
427
428         /*
429          * Tasklet may be scheduled. Need to get it removed from the list
430          * since we're about to free the struct.
431          */
432
433         sp->unloading = 1;
434         tasklet_enable(&sp->rx_tasklet);
435         tasklet_kill(&sp->rx_tasklet);
436 }
437
438 static int __exit ar2313_remove(struct platform_device *pdev)
439 {
440         struct net_device *dev = platform_get_drvdata(pdev);
441         rx_tasklet_cleanup(dev);
442         ar2313_init_cleanup(dev);
443         unregister_netdev(dev);
444         kfree(dev);
445         return 0;
446 }
447
448
449 /*
450  * Restart the AR2313 ethernet controller.
451  */
452 static int ar2313_restart(struct net_device *dev)
453 {
454         /* disable interrupts */
455         disable_irq(dev->irq);
456
457         /* stop mac */
458         ar2313_halt(dev);
459
460         /* initialize */
461         ar2313_init(dev);
462
463         /* enable interrupts */
464         enable_irq(dev->irq);
465
466         return 0;
467 }
468
469 static struct platform_driver ar2313_driver = {
470         .driver.name = "ar531x-eth",
471         .probe = ar2313_probe,
472         .remove = ar2313_remove,
473 };
474
475 int __init ar2313_module_init(void)
476 {
477         return platform_driver_register(&ar2313_driver);
478 }
479
480 void __exit ar2313_module_cleanup(void)
481 {
482         platform_driver_unregister(&ar2313_driver);
483 }
484
485 module_init(ar2313_module_init);
486 module_exit(ar2313_module_cleanup);
487
488
489 static void ar2313_free_descriptors(struct net_device *dev)
490 {
491         struct ar2313_private *sp = netdev_priv(dev);
492         if (sp->rx_ring != NULL) {
493                 kfree((void *) KSEG0ADDR(sp->rx_ring));
494                 sp->rx_ring = NULL;
495                 sp->tx_ring = NULL;
496         }
497 }
498
499
500 static int ar2313_allocate_descriptors(struct net_device *dev)
501 {
502         struct ar2313_private *sp = netdev_priv(dev);
503         int size;
504         int j;
505         ar2313_descr_t *space;
506
507         if (sp->rx_ring != NULL) {
508                 printk("%s: already done.\n", __FUNCTION__);
509                 return 0;
510         }
511
512         size =
513                 (sizeof(ar2313_descr_t) * (AR2313_DESCR_ENTRIES * AR2313_QUEUES));
514         space = kmalloc(size, GFP_KERNEL);
515         if (space == NULL)
516                 return 1;
517
518         /* invalidate caches */
519         dma_cache_inv((unsigned int) space, size);
520
521         /* now convert pointer to KSEG1 */
522         space = (ar2313_descr_t *) KSEG1ADDR(space);
523
524         memset((void *) space, 0, size);
525
526         sp->rx_ring = space;
527         space += AR2313_DESCR_ENTRIES;
528
529         sp->tx_ring = space;
530         space += AR2313_DESCR_ENTRIES;
531
532         /* Initialize the transmit Descriptors */
533         for (j = 0; j < AR2313_DESCR_ENTRIES; j++) {
534                 ar2313_descr_t *td = &sp->tx_ring[j];
535                 td->status = 0;
536                 td->devcs = DMA_TX1_CHAINED;
537                 td->addr = 0;
538                 td->descr =
539                         virt_to_phys(&sp->
540                                                  tx_ring[(j + 1) & (AR2313_DESCR_ENTRIES - 1)]);
541         }
542
543         return 0;
544 }
545
546
547 /*
548  * Generic cleanup handling data allocated during init. Used when the
549  * module is unloaded or if an error occurs during initialization
550  */
551 static void ar2313_init_cleanup(struct net_device *dev)
552 {
553         struct ar2313_private *sp = netdev_priv(dev);
554         struct sk_buff *skb;
555         int j;
556
557         ar2313_free_descriptors(dev);
558
559         if (sp->eth_regs)
560                 iounmap((void *) sp->eth_regs);
561         if (sp->dma_regs)
562                 iounmap((void *) sp->dma_regs);
563
564         if (sp->rx_skb) {
565                 for (j = 0; j < AR2313_DESCR_ENTRIES; j++) {
566                         skb = sp->rx_skb[j];
567                         if (skb) {
568                                 sp->rx_skb[j] = NULL;
569                                 dev_kfree_skb(skb);
570                         }
571                 }
572                 kfree(sp->rx_skb);
573                 sp->rx_skb = NULL;
574         }
575
576         if (sp->tx_skb) {
577                 for (j = 0; j < AR2313_DESCR_ENTRIES; j++) {
578                         skb = sp->tx_skb[j];
579                         if (skb) {
580                                 sp->tx_skb[j] = NULL;
581                                 dev_kfree_skb(skb);
582                         }
583                 }
584                 kfree(sp->tx_skb);
585                 sp->tx_skb = NULL;
586         }
587 }
588
589 static int ar2313_setup_timer(struct net_device *dev)
590 {
591         struct ar2313_private *sp = netdev_priv(dev);
592
593         init_timer(&sp->link_timer);
594
595         sp->link_timer.function = ar2313_link_timer_fn;
596         sp->link_timer.data = (int) dev;
597         sp->link_timer.expires = jiffies + HZ;
598
599         add_timer(&sp->link_timer);
600         return 0;
601
602 }
603
604 static void ar2313_link_timer_fn(unsigned long data)
605 {
606         struct net_device *dev = (struct net_device *) data;
607         struct ar2313_private *sp = netdev_priv(dev);
608
609         // see if the link status changed
610         // This was needed to make sure we set the PHY to the
611         // autonegotiated value of half or full duplex.
612         ar2313_check_link(dev);
613
614         // Loop faster when we don't have link.
615         // This was needed to speed up the AP bootstrap time.
616         if (sp->link == 0) {
617                 mod_timer(&sp->link_timer, jiffies + HZ / 2);
618         } else {
619                 mod_timer(&sp->link_timer, jiffies + LINK_TIMER);
620         }
621 }
622
623 static void ar2313_check_link(struct net_device *dev)
624 {
625         struct ar2313_private *sp = netdev_priv(dev);
626         u16 phyData;
627
628         phyData = mdiobus_read(&sp->mii_bus, sp->phy, MII_BMSR);
629         if (sp->phyData != phyData) {
630                 if (phyData & BMSR_LSTATUS) {
631                         /* link is present, ready link partner ability to deterine
632                            duplexity */
633                         int duplex = 0;
634                         u16 reg;
635
636                         sp->link = 1;
637                         reg = mdiobus_read(&sp->mii_bus, sp->phy, MII_BMCR);
638                         if (reg & BMCR_ANENABLE) {
639                                 /* auto neg enabled */
640                                 reg = mdiobus_read(&sp->mii_bus, sp->phy, MII_LPA);
641                                 duplex = (reg & (LPA_100FULL | LPA_10FULL)) ? 1 : 0;
642                         } else {
643                                 /* no auto neg, just read duplex config */
644                                 duplex = (reg & BMCR_FULLDPLX) ? 1 : 0;
645                         }
646
647                         printk(KERN_INFO "%s: Configuring MAC for %s duplex\n",
648                                    dev->name, (duplex) ? "full" : "half");
649
650                         if (duplex) {
651                                 /* full duplex */
652                                 sp->eth_regs->mac_control =
653                                         ((sp->eth_regs->
654                                           mac_control | MAC_CONTROL_F) & ~MAC_CONTROL_DRO);
655                         } else {
656                                 /* half duplex */
657                                 sp->eth_regs->mac_control =
658                                         ((sp->eth_regs->
659                                           mac_control | MAC_CONTROL_DRO) & ~MAC_CONTROL_F);
660                         }
661                 } else {
662                         /* no link */
663                         sp->link = 0;
664                 }
665                 sp->phyData = phyData;
666         }
667 }
668
669 static int ar2313_reset_reg(struct net_device *dev)
670 {
671         struct ar2313_private *sp = netdev_priv(dev);
672         unsigned int ethsal, ethsah;
673         unsigned int flags;
674
675         *sp->int_regs |= sp->cfg->reset_mac;
676         mdelay(10);
677         *sp->int_regs &= ~sp->cfg->reset_mac;
678         mdelay(10);
679         *sp->int_regs |= sp->cfg->reset_phy;
680         mdelay(10);
681         *sp->int_regs &= ~sp->cfg->reset_phy;
682         mdelay(10);
683
684         sp->dma_regs->bus_mode = (DMA_BUS_MODE_SWR);
685         mdelay(10);
686         sp->dma_regs->bus_mode =
687                 ((32 << DMA_BUS_MODE_PBL_SHIFT) | DMA_BUS_MODE_BLE);
688
689         /* enable interrupts */
690         sp->dma_regs->intr_ena = (DMA_STATUS_AIS |
691                                                           DMA_STATUS_NIS |
692                                                           DMA_STATUS_RI |
693                                                           DMA_STATUS_TI | DMA_STATUS_FBE);
694         sp->dma_regs->xmt_base = virt_to_phys(sp->tx_ring);
695         sp->dma_regs->rcv_base = virt_to_phys(sp->rx_ring);
696         sp->dma_regs->control =
697                 (DMA_CONTROL_SR | DMA_CONTROL_ST | DMA_CONTROL_SF);
698
699         sp->eth_regs->flow_control = (FLOW_CONTROL_FCE);
700         sp->eth_regs->vlan_tag = (0x8100);
701
702         /* Enable Ethernet Interface */
703         flags = (MAC_CONTROL_TE |       /* transmit enable */
704                          MAC_CONTROL_PM |       /* pass mcast */
705                          MAC_CONTROL_F |        /* full duplex */
706                          MAC_CONTROL_HBD);      /* heart beat disabled */
707
708         if (dev->flags & IFF_PROMISC) { /* set promiscuous mode */
709                 flags |= MAC_CONTROL_PR;
710         }
711         sp->eth_regs->mac_control = flags;
712
713         /* Set all Ethernet station address registers to their initial values */
714         ethsah = ((((u_int) (dev->dev_addr[5]) << 8) & (u_int) 0x0000FF00) |
715                           (((u_int) (dev->dev_addr[4]) << 0) & (u_int) 0x000000FF));
716
717         ethsal = ((((u_int) (dev->dev_addr[3]) << 24) & (u_int) 0xFF000000) |
718                           (((u_int) (dev->dev_addr[2]) << 16) & (u_int) 0x00FF0000) |
719                           (((u_int) (dev->dev_addr[1]) << 8) & (u_int) 0x0000FF00) |
720                           (((u_int) (dev->dev_addr[0]) << 0) & (u_int) 0x000000FF));
721
722         sp->eth_regs->mac_addr[0] = ethsah;
723         sp->eth_regs->mac_addr[1] = ethsal;
724
725         mdelay(10);
726
727         return (0);
728 }
729
730
731 static int ar2313_init(struct net_device *dev)
732 {
733         struct ar2313_private *sp = netdev_priv(dev);
734         int ecode = 0;
735
736         /*
737          * Allocate descriptors
738          */
739         if (ar2313_allocate_descriptors(dev)) {
740                 printk("%s: %s: ar2313_allocate_descriptors failed\n",
741                            dev->name, __FUNCTION__);
742                 ecode = -EAGAIN;
743                 goto init_error;
744         }
745
746         /*
747          * Get the memory for the skb rings.
748          */
749         if (sp->rx_skb == NULL) {
750                 sp->rx_skb =
751                         kmalloc(sizeof(struct sk_buff *) * AR2313_DESCR_ENTRIES,
752                                         GFP_KERNEL);
753                 if (!(sp->rx_skb)) {
754                         printk("%s: %s: rx_skb kmalloc failed\n",
755                                    dev->name, __FUNCTION__);
756                         ecode = -EAGAIN;
757                         goto init_error;
758                 }
759         }
760         memset(sp->rx_skb, 0, sizeof(struct sk_buff *) * AR2313_DESCR_ENTRIES);
761
762         if (sp->tx_skb == NULL) {
763                 sp->tx_skb =
764                         kmalloc(sizeof(struct sk_buff *) * AR2313_DESCR_ENTRIES,
765                                         GFP_KERNEL);
766                 if (!(sp->tx_skb)) {
767                         printk("%s: %s: tx_skb kmalloc failed\n",
768                                    dev->name, __FUNCTION__);
769                         ecode = -EAGAIN;
770                         goto init_error;
771                 }
772         }
773         memset(sp->tx_skb, 0, sizeof(struct sk_buff *) * AR2313_DESCR_ENTRIES);
774
775         /*
776          * Set tx_csm before we start receiving interrupts, otherwise
777          * the interrupt handler might think it is supposed to process
778          * tx ints before we are up and running, which may cause a null
779          * pointer access in the int handler.
780          */
781         sp->rx_skbprd = 0;
782         sp->cur_rx = 0;
783         sp->tx_prd = 0;
784         sp->tx_csm = 0;
785
786         /*
787          * Zero the stats before starting the interface
788          */
789         memset(&dev->stats, 0, sizeof(dev->stats));
790
791         /*
792          * We load the ring here as there seem to be no way to tell the
793          * firmware to wipe the ring without re-initializing it.
794          */
795         ar2313_load_rx_ring(dev, RX_RING_SIZE);
796
797         /*
798          * Init hardware
799          */
800         ar2313_reset_reg(dev);
801
802         /*
803          * Get the IRQ
804          */
805         ecode =
806                 request_irq(dev->irq, &ar2313_interrupt,
807                                         IRQF_SHARED | IRQF_DISABLED | IRQF_SAMPLE_RANDOM,
808                                         dev->name, dev);
809         if (ecode) {
810                 printk(KERN_WARNING "%s: %s: Requested IRQ %d is busy\n",
811                            dev->name, __FUNCTION__, dev->irq);
812                 goto init_error;
813         }
814
815
816         tasklet_enable(&sp->rx_tasklet);
817
818         return 0;
819
820   init_error:
821         ar2313_init_cleanup(dev);
822         return ecode;
823 }
824
825 /*
826  * Load the rx ring.
827  *
828  * Loading rings is safe without holding the spin lock since this is
829  * done only before the device is enabled, thus no interrupts are
830  * generated and by the interrupt handler/tasklet handler.
831  */
832 static void ar2313_load_rx_ring(struct net_device *dev, int nr_bufs)
833 {
834
835         struct ar2313_private *sp = netdev_priv(dev);
836         short i, idx;
837
838         idx = sp->rx_skbprd;
839
840         for (i = 0; i < nr_bufs; i++) {
841                 struct sk_buff *skb;
842                 ar2313_descr_t *rd;
843                 int offset = RX_OFFSET;
844
845                 if (sp->rx_skb[idx]) {
846 #if DEBUG_RX
847                         printk(KERN_INFO "ar2313 rx refill full\n");
848 #endif                                                  /* DEBUG */
849                         break;
850                 }
851                 // partha: create additional room for the second GRE fragment
852                 skb = alloc_skb(AR2313_BUFSIZE + 128, GFP_ATOMIC);
853                 if (!skb) {
854                         printk("\n\n\n\n %s: No memory in system\n\n\n\n",
855                                    __FUNCTION__);
856                         break;
857                 }
858                 // partha: create additional room in the front for tx pkt capture
859                 skb_reserve(skb, 32);
860
861                 /*
862                  * Make sure IP header starts on a fresh cache line.
863                  */
864                 skb->dev = dev;
865                 if (sp->phy_dev)
866                         offset += sp->phy_dev->pkt_align;
867                 skb_reserve(skb, offset);
868                 sp->rx_skb[idx] = skb;
869
870                 rd = (ar2313_descr_t *) & sp->rx_ring[idx];
871
872                 /* initialize dma descriptor */
873                 rd->devcs = ((AR2313_BUFSIZE << DMA_RX1_BSIZE_SHIFT) |
874                                          DMA_RX1_CHAINED);
875                 rd->addr = virt_to_phys(skb->data);
876                 rd->descr =
877                         virt_to_phys(&sp->
878                                                  rx_ring[(idx + 1) & (AR2313_DESCR_ENTRIES - 1)]);
879                 rd->status = DMA_RX_OWN;
880
881                 idx = DSC_NEXT(idx);
882         }
883
884         if (!i) {
885 #if DEBUG_ERR
886                 printk(KERN_INFO
887                            "Out of memory when allocating standard receive buffers\n");
888 #endif                                                  /* DEBUG */
889         } else {
890                 sp->rx_skbprd = idx;
891         }
892
893         return;
894 }
895
896 #define AR2313_MAX_PKTS_PER_CALL        64
897
898 static int ar2313_rx_int(struct net_device *dev)
899 {
900         struct ar2313_private *sp = netdev_priv(dev);
901         struct sk_buff *skb, *skb_new;
902         ar2313_descr_t *rxdesc;
903         unsigned int status;
904         u32 idx;
905         int pkts = 0;
906         int rval;
907
908         idx = sp->cur_rx;
909
910         /* process at most the entire ring and then wait for another interrupt
911          */
912         while (1) {
913
914                 rxdesc = &sp->rx_ring[idx];
915                 status = rxdesc->status;
916                 if (status & DMA_RX_OWN) {
917                         /* SiByte owns descriptor or descr not yet filled in */
918                         rval = 0;
919                         break;
920                 }
921
922                 if (++pkts > AR2313_MAX_PKTS_PER_CALL) {
923                         rval = 1;
924                         break;
925                 }
926 #if DEBUG_RX
927                 printk("index %d\n", idx);
928                 printk("RX status %08x\n", rxdesc->status);
929                 printk("RX devcs  %08x\n", rxdesc->devcs);
930                 printk("RX addr   %08x\n", rxdesc->addr);
931                 printk("RX descr  %08x\n", rxdesc->descr);
932 #endif
933
934                 if ((status & DMA_RX_ERROR) &&
935                         (!(status & DMA_RX_LONG))) {
936 #if DEBUG_RX
937                         printk("%s: rx ERROR %08x\n", __FUNCTION__, status);
938 #endif
939                         dev->stats.rx_errors++;
940                         dev->stats.rx_dropped++;
941
942                         /* add statistics counters */
943                         if (status & DMA_RX_ERR_CRC)
944                                 dev->stats.rx_crc_errors++;
945                         if (status & DMA_RX_ERR_COL)
946                                 dev->stats.rx_over_errors++;
947                         if (status & DMA_RX_ERR_LENGTH)
948                                 dev->stats.rx_length_errors++;
949                         if (status & DMA_RX_ERR_RUNT)
950                                 dev->stats.rx_over_errors++;
951                         if (status & DMA_RX_ERR_DESC)
952                                 dev->stats.rx_over_errors++;
953
954                 } else {
955                         /* alloc new buffer. */
956                         skb_new = dev_alloc_skb(AR2313_BUFSIZE + RX_OFFSET + 128);
957                         if (skb_new != NULL) {
958                                 int offset;
959
960                                 skb = sp->rx_skb[idx];
961 #if 0//CONFIG_AR8216_PHY
962 #define HEADER_LEN 2
963                     /* check and remove the header for s26*/
964                     if ((skb->data[0] & 0xf) == 5) { /* wan port */
965                         skb->data[14 + HEADER_LEN] &= 0xf0; /* change VLAN ID for the wan port */
966                         skb->data[15 + HEADER_LEN] = 2;     /* to VLAN ID 2 */
967                     }
968 #endif                         
969                                 /* set skb */
970                     skb_put(skb,((status >> DMA_RX_LEN_SHIFT) & 0x3fff) - CRC_LEN);
971 #if 0//CONFIG_AR8216_PHY
972                     skb_pull(skb, HEADER_LEN); /* remove the header */
973 #endif
974
975                                 dev->stats.rx_bytes += skb->len;
976
977                                 /* pass the packet to upper layers */
978                                 sp->rx(skb);
979
980                                 skb_new->dev = dev;
981                                 /* 16 bit align */
982                                 offset = RX_OFFSET + 32;
983                                 if (sp->phy_dev)
984                                         offset += sp->phy_dev->pkt_align;
985                                 skb_reserve(skb_new, offset);
986                                 /* reset descriptor's curr_addr */
987                                 rxdesc->addr = virt_to_phys(skb_new->data);
988
989                                 dev->stats.rx_packets++;
990                                 sp->rx_skb[idx] = skb_new;
991                         } else {
992                                 dev->stats.rx_dropped++;
993                         }
994                 }
995
996                 rxdesc->devcs = ((AR2313_BUFSIZE << DMA_RX1_BSIZE_SHIFT) |
997                                                  DMA_RX1_CHAINED);
998                 rxdesc->status = DMA_RX_OWN;
999
1000                 idx = DSC_NEXT(idx);
1001         }
1002
1003         sp->cur_rx = idx;
1004
1005         return rval;
1006 }
1007
1008
1009 static void ar2313_tx_int(struct net_device *dev)
1010 {
1011         struct ar2313_private *sp = netdev_priv(dev);
1012         u32 idx;
1013         struct sk_buff *skb;
1014         ar2313_descr_t *txdesc;
1015         unsigned int status = 0;
1016
1017         idx = sp->tx_csm;
1018
1019         while (idx != sp->tx_prd) {
1020
1021                 txdesc = &sp->tx_ring[idx];
1022
1023 #if DEBUG_TX
1024                 printk
1025                         ("%s: TXINT: csm=%d idx=%d prd=%d status=%x devcs=%x addr=%08x descr=%x\n",
1026                          dev->name, sp->tx_csm, idx, sp->tx_prd, txdesc->status,
1027                          txdesc->devcs, txdesc->addr, txdesc->descr);
1028 #endif                                                  /* DEBUG */
1029
1030                 if ((status = txdesc->status) & DMA_TX_OWN) {
1031                         /* ar2313 dma still owns descr */
1032                         break;
1033                 }
1034                 /* done with this descriptor */
1035                 dma_unmap_single(NULL, txdesc->addr,
1036                                                  txdesc->devcs & DMA_TX1_BSIZE_MASK,
1037                                                  DMA_TO_DEVICE);
1038                 txdesc->status = 0;
1039
1040                 if (status & DMA_TX_ERROR) {
1041                         dev->stats.tx_errors++;
1042                         dev->stats.tx_dropped++;
1043                         if (status & DMA_TX_ERR_UNDER)
1044                                 dev->stats.tx_fifo_errors++;
1045                         if (status & DMA_TX_ERR_HB)
1046                                 dev->stats.tx_heartbeat_errors++;
1047                         if (status & (DMA_TX_ERR_LOSS | DMA_TX_ERR_LINK))
1048                                 dev->stats.tx_carrier_errors++;
1049                         if (status & (DMA_TX_ERR_LATE |
1050                                                   DMA_TX_ERR_COL |
1051                                                   DMA_TX_ERR_JABBER | DMA_TX_ERR_DEFER))
1052                                 dev->stats.tx_aborted_errors++;
1053                 } else {
1054                         /* transmit OK */
1055                         dev->stats.tx_packets++;
1056                 }
1057
1058                 skb = sp->tx_skb[idx];
1059                 sp->tx_skb[idx] = NULL;
1060                 idx = DSC_NEXT(idx);
1061                 dev->stats.tx_bytes += skb->len;
1062                 dev_kfree_skb_irq(skb);
1063         }
1064
1065         sp->tx_csm = idx;
1066
1067         return;
1068 }
1069
1070
1071 static void rx_tasklet_func(unsigned long data)
1072 {
1073         struct net_device *dev = (struct net_device *) data;
1074         struct ar2313_private *sp = netdev_priv(dev);
1075
1076         if (sp->unloading) {
1077                 return;
1078         }
1079
1080         if (ar2313_rx_int(dev)) {
1081                 tasklet_hi_schedule(&sp->rx_tasklet);
1082         } else {
1083                 unsigned long flags;
1084                 spin_lock_irqsave(&sp->lock, flags);
1085                 sp->dma_regs->intr_ena |= DMA_STATUS_RI;
1086                 spin_unlock_irqrestore(&sp->lock, flags);
1087         }
1088 }
1089
1090 static void rx_schedule(struct net_device *dev)
1091 {
1092         struct ar2313_private *sp = netdev_priv(dev);
1093
1094         sp->dma_regs->intr_ena &= ~DMA_STATUS_RI;
1095
1096         tasklet_hi_schedule(&sp->rx_tasklet);
1097 }
1098
1099 static irqreturn_t ar2313_interrupt(int irq, void *dev_id)
1100 {
1101         struct net_device *dev = (struct net_device *) dev_id;
1102         struct ar2313_private *sp = netdev_priv(dev);
1103         unsigned int status, enabled;
1104
1105         /* clear interrupt */
1106         /*
1107          * Don't clear RI bit if currently disabled.
1108          */
1109         status = sp->dma_regs->status;
1110         enabled = sp->dma_regs->intr_ena;
1111         sp->dma_regs->status = status & enabled;
1112
1113         if (status & DMA_STATUS_NIS) {
1114                 /* normal status */
1115                 /*
1116                  * Don't schedule rx processing if interrupt
1117                  * is already disabled.
1118                  */
1119                 if (status & enabled & DMA_STATUS_RI) {
1120                         /* receive interrupt */
1121                         rx_schedule(dev);
1122                 }
1123                 if (status & DMA_STATUS_TI) {
1124                         /* transmit interrupt */
1125                         ar2313_tx_int(dev);
1126                 }
1127         }
1128
1129         if (status & DMA_STATUS_AIS) {
1130 #if DEBUG_INT
1131                 printk("%s: AIS set %08x & %x\n", __FUNCTION__,
1132                            status, (DMA_STATUS_FBE | DMA_STATUS_TPS));
1133 #endif
1134                 /* abnormal status */
1135                 if (status & (DMA_STATUS_FBE | DMA_STATUS_TPS)) {
1136                         ar2313_restart(dev);
1137                 }
1138         }
1139         return IRQ_HANDLED;
1140 }
1141
1142
1143 static int ar2313_open(struct net_device *dev)
1144 {
1145         struct ar2313_private *sp = netdev_priv(dev);
1146         unsigned int ethsal, ethsah;
1147
1148         /* reset the hardware, in case the MAC address changed */
1149         ethsah = ((((u_int) (dev->dev_addr[5]) << 8) & (u_int) 0x0000FF00) |
1150                           (((u_int) (dev->dev_addr[4]) << 0) & (u_int) 0x000000FF));
1151
1152         ethsal = ((((u_int) (dev->dev_addr[3]) << 24) & (u_int) 0xFF000000) |
1153                           (((u_int) (dev->dev_addr[2]) << 16) & (u_int) 0x00FF0000) |
1154                           (((u_int) (dev->dev_addr[1]) << 8) & (u_int) 0x0000FF00) |
1155                           (((u_int) (dev->dev_addr[0]) << 0) & (u_int) 0x000000FF));
1156
1157         sp->eth_regs->mac_addr[0] = ethsah;
1158         sp->eth_regs->mac_addr[1] = ethsal;
1159
1160         mdelay(10);
1161
1162         dev->mtu = 1500;
1163         netif_start_queue(dev);
1164
1165         sp->eth_regs->mac_control |= MAC_CONTROL_RE;
1166
1167         return 0;
1168 }
1169
1170 static void ar2313_halt(struct net_device *dev)
1171 {
1172         struct ar2313_private *sp = netdev_priv(dev);
1173         int j;
1174
1175         tasklet_disable(&sp->rx_tasklet);
1176
1177         /* kill the MAC */
1178         sp->eth_regs->mac_control &= ~(MAC_CONTROL_RE | /* disable Receives */
1179                                                                    MAC_CONTROL_TE);     /* disable Transmits */
1180         /* stop dma */
1181         sp->dma_regs->control = 0;
1182         sp->dma_regs->bus_mode = DMA_BUS_MODE_SWR;
1183
1184         /* place phy and MAC in reset */
1185         *sp->int_regs |= (sp->cfg->reset_mac | sp->cfg->reset_phy);
1186
1187         /* free buffers on tx ring */
1188         for (j = 0; j < AR2313_DESCR_ENTRIES; j++) {
1189                 struct sk_buff *skb;
1190                 ar2313_descr_t *txdesc;
1191
1192                 txdesc = &sp->tx_ring[j];
1193                 txdesc->descr = 0;
1194
1195                 skb = sp->tx_skb[j];
1196                 if (skb) {
1197                         dev_kfree_skb(skb);
1198                         sp->tx_skb[j] = NULL;
1199                 }
1200         }
1201 }
1202
1203 /*
1204  * close should do nothing. Here's why. It's called when
1205  * 'ifconfig bond0 down' is run. If it calls free_irq then
1206  * the irq is gone forever ! When bond0 is made 'up' again,
1207  * the ar2313_open () does not call request_irq (). Worse,
1208  * the call to ar2313_halt() generates a WDOG reset due to
1209  * the write to 'sp->int_regs' and the box reboots.
1210  * Commenting this out is good since it allows the
1211  * system to resume when bond0 is made up again.
1212  */
1213 static int ar2313_close(struct net_device *dev)
1214 {
1215 #if 0
1216         /*
1217          * Disable interrupts
1218          */
1219         disable_irq(dev->irq);
1220
1221         /*
1222          * Without (or before) releasing irq and stopping hardware, this
1223          * is an absolute non-sense, by the way. It will be reset instantly
1224          * by the first irq.
1225          */
1226         netif_stop_queue(dev);
1227
1228         /* stop the MAC and DMA engines */
1229         ar2313_halt(dev);
1230
1231         /* release the interrupt */
1232         free_irq(dev->irq, dev);
1233
1234 #endif
1235         return 0;
1236 }
1237
1238 static int ar2313_start_xmit(struct sk_buff *skb, struct net_device *dev)
1239 {
1240         struct ar2313_private *sp = netdev_priv(dev);
1241         ar2313_descr_t *td;
1242         u32 idx;
1243
1244         idx = sp->tx_prd;
1245         td = &sp->tx_ring[idx];
1246
1247         if (td->status & DMA_TX_OWN) {
1248 #if DEBUG_TX
1249                 printk("%s: No space left to Tx\n", __FUNCTION__);
1250 #endif
1251                 /* free skbuf and lie to the caller that we sent it out */
1252                 dev->stats.tx_dropped++;
1253                 dev_kfree_skb(skb);
1254
1255                 /* restart transmitter in case locked */
1256                 sp->dma_regs->xmt_poll = 0;
1257                 return 0;
1258         }
1259
1260 #if 0///CONFIG_AR8216_PHY
1261     /* add a header for s26*/
1262     skb_push(skb, HEADER_LEN);
1263     skb->data[0] = 0x10; /* broadcast = 0; from_cpu = 0; reserved = 1; port_num = 0 */
1264     skb->data[1] = 0x80; /* reserved = 0b10; priority = 0; type = 0 (normal) */
1265 #endif
1266
1267         /* Setup the transmit descriptor. */
1268         td->devcs = ((skb->len << DMA_TX1_BSIZE_SHIFT) |
1269                                  (DMA_TX1_LS | DMA_TX1_IC | DMA_TX1_CHAINED));
1270         td->addr = dma_map_single(NULL, skb->data, skb->len, DMA_TO_DEVICE);
1271         td->status = DMA_TX_OWN;
1272
1273         /* kick transmitter last */
1274         sp->dma_regs->xmt_poll = 0;
1275
1276 #if DEBUG_TX
1277         printk("index %d\n", idx);
1278         printk("TX status %08x\n", td->status);
1279         printk("TX devcs  %08x\n", td->devcs);
1280         printk("TX addr   %08x\n", td->addr);
1281         printk("TX descr  %08x\n", td->descr);
1282 #endif
1283
1284         sp->tx_skb[idx] = skb;
1285         idx = DSC_NEXT(idx);
1286         sp->tx_prd = idx;
1287
1288         return 0;
1289 }
1290 static short armiiread (struct net_device *dev, short phy, short reg);
1291 static void armiiwrite (struct net_device *dev, short phy, short reg,
1292                         short data);
1293
1294 static int ar2313_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1295 {
1296         struct mii_ioctl_data *data = (struct mii_ioctl_data *) &ifr->ifr_data;
1297         struct ar2313_private *sp = netdev_priv(dev);
1298         int ret;
1299
1300         switch (cmd) {
1301
1302         case SIOCETHTOOL:
1303                 spin_lock_irq(&sp->lock);
1304                 ret = phy_ethtool_ioctl(sp->phy_dev, (void *) ifr->ifr_data);
1305                 spin_unlock_irq(&sp->lock);
1306                 return ret;
1307
1308         case SIOCSIFHWADDR:
1309                 if (copy_from_user
1310                         (dev->dev_addr, ifr->ifr_data, sizeof(dev->dev_addr)))
1311                         return -EFAULT;
1312                 return 0;
1313
1314         case SIOCGIFHWADDR:
1315                 if (copy_to_user
1316                         (ifr->ifr_data, dev->dev_addr, sizeof(dev->dev_addr)))
1317                         return -EFAULT;
1318                 return 0;
1319
1320     case SIOCGMIIPHY:           /* Get address of MII PHY in use. */
1321       data->phy_id = 1;
1322       /* Fall Through */
1323
1324     case SIOCGMIIREG:           /* Read MII PHY register. */
1325       data->val_out = armiiread (dev, data->phy_id & 0x1f,
1326                                  data->reg_num & 0x1f);
1327       return 0;
1328     case SIOCSMIIREG:           /* Write MII PHY register. */
1329       if (!capable (CAP_NET_ADMIN))
1330         return -EPERM;
1331       armiiwrite (dev, data->phy_id & 0x1f,
1332                   data->reg_num & 0x1f, data->val_in);
1333       return 0;
1334
1335         default:
1336                 break;
1337         }
1338
1339         return -EOPNOTSUPP;
1340 }
1341
1342 static void ar2313_adjust_link(struct net_device *dev)
1343 {
1344         struct ar2313_private *sp = netdev_priv(dev);
1345         unsigned int mc;
1346
1347         if (!sp->phy_dev->link)
1348                 return;
1349
1350         if (sp->phy_dev->duplex != sp->oldduplex) {
1351                 mc = readl(&sp->eth_regs->mac_control);
1352                 mc &= ~(MAC_CONTROL_F | MAC_CONTROL_DRO);
1353                 if (sp->phy_dev->duplex)
1354                         mc |= MAC_CONTROL_F;
1355                 else
1356                         mc |= MAC_CONTROL_DRO;
1357                 writel(mc, &sp->eth_regs->mac_control);
1358                 sp->oldduplex = sp->phy_dev->duplex;
1359         }
1360 }
1361
1362 #define MII_ADDR(phy, reg) \
1363         ((reg << MII_ADDR_REG_SHIFT) | (phy << MII_ADDR_PHY_SHIFT))
1364
1365
1366
1367
1368 static short
1369 armiiread (struct net_device *dev, short phy, short reg)
1370 {
1371
1372   short outp;
1373
1374   struct ar2313_private *sp = (struct ar2313_private *) dev->priv;
1375   volatile ETHERNET_STRUCT *ethernet = sp->phy_regs;
1376   ethernet->mii_addr = MII_ADDR (phy, reg);
1377   while (ethernet->mii_addr & MII_ADDR_BUSY);
1378   outp = ethernet->mii_data >> MII_DATA_SHIFT;
1379   return (outp);
1380 }
1381
1382 static void
1383 armiiwrite (struct net_device *dev, short phy, short reg, short data)
1384 {
1385   struct ar2313_private *sp = (struct ar2313_private *) dev->priv;
1386   volatile ETHERNET_STRUCT *ethernet = sp->phy_regs;
1387   while (ethernet->mii_addr & MII_ADDR_BUSY);
1388   ethernet->mii_data = data << MII_DATA_SHIFT;
1389   ethernet->mii_addr = MII_ADDR (phy, reg) | MII_ADDR_WRITE;
1390 }
1391
1392 static int
1393 mdiobus_read(struct mii_bus *bus, int phy_addr, int regnum)
1394 {
1395         struct net_device *const dev = bus->priv;
1396         struct ar2313_private *sp = netdev_priv(dev);
1397         volatile ETHERNET_STRUCT *ethernet = sp->phy_regs;
1398
1399         ethernet->mii_addr = MII_ADDR(phy_addr, regnum);
1400         while (ethernet->mii_addr & MII_ADDR_BUSY);
1401         return (ethernet->mii_data >> MII_DATA_SHIFT);
1402 }
1403
1404 static int
1405 mdiobus_write(struct mii_bus *bus, int phy_addr, int regnum,
1406              u16 value)
1407 {
1408         struct net_device *const dev = bus->priv;
1409         struct ar2313_private *sp = netdev_priv(dev);
1410         volatile ETHERNET_STRUCT *ethernet = sp->phy_regs;
1411
1412         while (ethernet->mii_addr & MII_ADDR_BUSY);
1413         ethernet->mii_data = value << MII_DATA_SHIFT;
1414         ethernet->mii_addr = MII_ADDR(phy_addr, regnum) | MII_ADDR_WRITE;
1415
1416         return 0;
1417 }
1418
1419 static int mdiobus_reset(struct mii_bus *bus)
1420 {
1421         struct net_device *const dev = bus->priv;
1422
1423         ar2313_reset_reg(dev);
1424
1425         return 0;
1426 }
1427
1428 static int mdiobus_probe (struct net_device *dev)
1429 {
1430         struct ar2313_private *const sp = netdev_priv(dev);
1431         struct phy_device *phydev = NULL;
1432         int phy_addr;
1433
1434         /* find the first (lowest address) PHY on the current MAC's MII bus */
1435         for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++)
1436                 if (sp->mii_bus.phy_map[phy_addr]) {
1437                         phydev = sp->mii_bus.phy_map[phy_addr];
1438                         break; /* break out with first one found */
1439                 }
1440
1441         if (!phydev) {
1442                 printk (KERN_ERR "ar2313:%s: no PHY found\n", dev->name);
1443                 return -1;
1444         }
1445
1446         /* now we are supposed to have a proper phydev, to attach to... */
1447         BUG_ON(!phydev);
1448         BUG_ON(phydev->attached_dev);
1449
1450         phydev = phy_connect(dev, phydev->dev.bus_id, &ar2313_adjust_link, 0,
1451                 PHY_INTERFACE_MODE_MII);
1452
1453         if (IS_ERR(phydev)) {
1454                 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
1455                 return PTR_ERR(phydev);
1456         }
1457
1458         sp->rx = phydev->netif_rx;
1459
1460         /* mask with MAC supported features */
1461         phydev->supported &= (SUPPORTED_10baseT_Half
1462                 | SUPPORTED_10baseT_Full
1463                 | SUPPORTED_100baseT_Half
1464                 | SUPPORTED_100baseT_Full
1465                 | SUPPORTED_Autoneg
1466                 /* | SUPPORTED_Pause | SUPPORTED_Asym_Pause */
1467                 | SUPPORTED_MII
1468                 | SUPPORTED_TP);
1469
1470         phydev->advertising = phydev->supported;
1471
1472         sp->oldduplex = -1;
1473         sp->phy_dev = phydev;
1474
1475         printk(KERN_INFO "%s: attached PHY driver [%s] "
1476                 "(mii_bus:phy_addr=%s)\n",
1477                 dev->name, phydev->drv->name, phydev->dev.bus_id);
1478
1479         return 0;
1480 }
1481
Note: See TracBrowser for help on using the browser.