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