source: src/linux/ar531x/linux-2.6.23/drivers/net/ar2313/ar2313.c @ 10868

Last change on this file since 10868 was 10868, checked in by BrainSlayer, 5 years ago

fix marvell switch performance problem (affects fonera 2200 and dir 400) thx nbd

File size: 35.3 KB
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                     1518
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
150MODULE_LICENSE("GPL");
151MODULE_AUTHOR("Sameer Dekate <sdekate@arubanetworks.com>, Imre Kaloz <kaloz@openwrt.org>, Felix Fietkau <nbd@openwrt.org>");
152MODULE_DESCRIPTION("AR2313 Ethernet driver");
153#endif
154
155#define virt_to_phys(x) ((u32)(x) & 0x1fffffff)
156
157// prototypes
158#ifdef TX_TIMEOUT
159static void ar2313_tx_timeout(struct net_device *dev);
160#endif
161static void ar2313_halt(struct net_device *dev);
162static void rx_tasklet_func(unsigned long data);
163static void rx_tasklet_cleanup(struct net_device *dev);
164static void ar2313_multicast_list(struct net_device *dev);
165
166static int mdiobus_read(struct mii_bus *bus, int phy_addr, int regnum);
167static int mdiobus_write(struct mii_bus *bus, int phy_addr, int regnum, u16 value);
168static int mdiobus_reset(struct mii_bus *bus);
169static int mdiobus_probe (struct net_device *dev);
170static 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
177int __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
321static 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
351static 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
366static 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 */
385static 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
424static 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
438static 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 */
452static 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
469static struct platform_driver ar2313_driver = {
470        .driver.name = "ar531x-eth",
471        .probe = ar2313_probe,
472        .remove = ar2313_remove,
473};
474
475int __init ar2313_module_init(void)
476{
477        return platform_driver_register(&ar2313_driver);
478}
479
480void __exit ar2313_module_cleanup(void)
481{
482        platform_driver_unregister(&ar2313_driver);
483}
484
485module_init(ar2313_module_init);
486module_exit(ar2313_module_cleanup);
487
488
489static 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
500static 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 */
551static 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
589static 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
604static 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
623static 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
669static 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
731static 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 */
832static 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
898static 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 | DMA_RX_ERR_LENGTH)) &&
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                                /* set skb */
962                                skb_put(skb,
963                                                ((status >> DMA_RX_LEN_SHIFT) & 0x3fff) - CRC_LEN);
964
965                                dev->stats.rx_bytes += skb->len;
966
967                                /* pass the packet to upper layers */
968                                sp->rx(skb);
969
970                                skb_new->dev = dev;
971                                /* 16 bit align */
972                                offset = RX_OFFSET + 32;
973                                if (sp->phy_dev)
974                                        offset += sp->phy_dev->pkt_align;
975                                skb_reserve(skb_new, offset);
976                                /* reset descriptor's curr_addr */
977                                rxdesc->addr = virt_to_phys(skb_new->data);
978
979                                dev->stats.rx_packets++;
980                                sp->rx_skb[idx] = skb_new;
981                        } else {
982                                dev->stats.rx_dropped++;
983                        }
984                }
985
986                rxdesc->devcs = ((AR2313_BUFSIZE << DMA_RX1_BSIZE_SHIFT) |
987                                                 DMA_RX1_CHAINED);
988                rxdesc->status = DMA_RX_OWN;
989
990                idx = DSC_NEXT(idx);
991        }
992
993        sp->cur_rx = idx;
994
995        return rval;
996}
997
998
999static void ar2313_tx_int(struct net_device *dev)
1000{
1001        struct ar2313_private *sp = netdev_priv(dev);
1002        u32 idx;
1003        struct sk_buff *skb;
1004        ar2313_descr_t *txdesc;
1005        unsigned int status = 0;
1006
1007        idx = sp->tx_csm;
1008
1009        while (idx != sp->tx_prd) {
1010
1011                txdesc = &sp->tx_ring[idx];
1012
1013#if DEBUG_TX
1014                printk
1015                        ("%s: TXINT: csm=%d idx=%d prd=%d status=%x devcs=%x addr=%08x descr=%x\n",
1016                         dev->name, sp->tx_csm, idx, sp->tx_prd, txdesc->status,
1017                         txdesc->devcs, txdesc->addr, txdesc->descr);
1018#endif                                                  /* DEBUG */
1019
1020                if ((status = txdesc->status) & DMA_TX_OWN) {
1021                        /* ar2313 dma still owns descr */
1022                        break;
1023                }
1024                /* done with this descriptor */
1025                dma_unmap_single(NULL, txdesc->addr,
1026                                                 txdesc->devcs & DMA_TX1_BSIZE_MASK,
1027                                                 DMA_TO_DEVICE);
1028                txdesc->status = 0;
1029
1030                if (status & DMA_TX_ERROR) {
1031                        dev->stats.tx_errors++;
1032                        dev->stats.tx_dropped++;
1033                        if (status & DMA_TX_ERR_UNDER)
1034                                dev->stats.tx_fifo_errors++;
1035                        if (status & DMA_TX_ERR_HB)
1036                                dev->stats.tx_heartbeat_errors++;
1037                        if (status & (DMA_TX_ERR_LOSS | DMA_TX_ERR_LINK))
1038                                dev->stats.tx_carrier_errors++;
1039                        if (status & (DMA_TX_ERR_LATE |
1040                                                  DMA_TX_ERR_COL |
1041                                                  DMA_TX_ERR_JABBER | DMA_TX_ERR_DEFER))
1042                                dev->stats.tx_aborted_errors++;
1043                } else {
1044                        /* transmit OK */
1045                        dev->stats.tx_packets++;
1046                }
1047
1048                skb = sp->tx_skb[idx];
1049                sp->tx_skb[idx] = NULL;
1050                idx = DSC_NEXT(idx);
1051                dev->stats.tx_bytes += skb->len;
1052                dev_kfree_skb_irq(skb);
1053        }
1054
1055        sp->tx_csm = idx;
1056
1057        return;
1058}
1059
1060
1061static void rx_tasklet_func(unsigned long data)
1062{
1063        struct net_device *dev = (struct net_device *) data;
1064        struct ar2313_private *sp = netdev_priv(dev);
1065
1066        if (sp->unloading) {
1067                return;
1068        }
1069
1070        if (ar2313_rx_int(dev)) {
1071                tasklet_hi_schedule(&sp->rx_tasklet);
1072        } else {
1073                unsigned long flags;
1074                spin_lock_irqsave(&sp->lock, flags);
1075                sp->dma_regs->intr_ena |= DMA_STATUS_RI;
1076                spin_unlock_irqrestore(&sp->lock, flags);
1077        }
1078}
1079
1080static void rx_schedule(struct net_device *dev)
1081{
1082        struct ar2313_private *sp = netdev_priv(dev);
1083
1084        sp->dma_regs->intr_ena &= ~DMA_STATUS_RI;
1085
1086        tasklet_hi_schedule(&sp->rx_tasklet);
1087}
1088
1089static irqreturn_t ar2313_interrupt(int irq, void *dev_id)
1090{
1091        struct net_device *dev = (struct net_device *) dev_id;
1092        struct ar2313_private *sp = netdev_priv(dev);
1093        unsigned int status, enabled;
1094
1095        /* clear interrupt */
1096        /*
1097         * Don't clear RI bit if currently disabled.
1098         */
1099        status = sp->dma_regs->status;
1100        enabled = sp->dma_regs->intr_ena;
1101        sp->dma_regs->status = status & enabled;
1102
1103        if (status & DMA_STATUS_NIS) {
1104                /* normal status */
1105                /*
1106                 * Don't schedule rx processing if interrupt
1107                 * is already disabled.
1108                 */
1109                if (status & enabled & DMA_STATUS_RI) {
1110                        /* receive interrupt */
1111                        rx_schedule(dev);
1112                }
1113                if (status & DMA_STATUS_TI) {
1114                        /* transmit interrupt */
1115                        ar2313_tx_int(dev);
1116                }
1117        }
1118
1119        if (status & DMA_STATUS_AIS) {
1120#if DEBUG_INT
1121                printk("%s: AIS set %08x & %x\n", __FUNCTION__,
1122                           status, (DMA_STATUS_FBE | DMA_STATUS_TPS));
1123#endif
1124                /* abnormal status */
1125                if (status & (DMA_STATUS_FBE | DMA_STATUS_TPS)) {
1126                        ar2313_restart(dev);
1127                }
1128        }
1129        return IRQ_HANDLED;
1130}
1131
1132
1133static int ar2313_open(struct net_device *dev)
1134{
1135        struct ar2313_private *sp = netdev_priv(dev);
1136
1137        dev->mtu = 1500;
1138        netif_start_queue(dev);
1139
1140        sp->eth_regs->mac_control |= MAC_CONTROL_RE;
1141
1142        return 0;
1143}
1144
1145static void ar2313_halt(struct net_device *dev)
1146{
1147        struct ar2313_private *sp = netdev_priv(dev);
1148        int j;
1149
1150        tasklet_disable(&sp->rx_tasklet);
1151
1152        /* kill the MAC */
1153        sp->eth_regs->mac_control &= ~(MAC_CONTROL_RE | /* disable Receives */
1154                                                                   MAC_CONTROL_TE);     /* disable Transmits */
1155        /* stop dma */
1156        sp->dma_regs->control = 0;
1157        sp->dma_regs->bus_mode = DMA_BUS_MODE_SWR;
1158
1159        /* place phy and MAC in reset */
1160        *sp->int_regs |= (sp->cfg->reset_mac | sp->cfg->reset_phy);
1161
1162        /* free buffers on tx ring */
1163        for (j = 0; j < AR2313_DESCR_ENTRIES; j++) {
1164                struct sk_buff *skb;
1165                ar2313_descr_t *txdesc;
1166
1167                txdesc = &sp->tx_ring[j];
1168                txdesc->descr = 0;
1169
1170                skb = sp->tx_skb[j];
1171                if (skb) {
1172                        dev_kfree_skb(skb);
1173                        sp->tx_skb[j] = NULL;
1174                }
1175        }
1176}
1177
1178/*
1179 * close should do nothing. Here's why. It's called when
1180 * 'ifconfig bond0 down' is run. If it calls free_irq then
1181 * the irq is gone forever ! When bond0 is made 'up' again,
1182 * the ar2313_open () does not call request_irq (). Worse,
1183 * the call to ar2313_halt() generates a WDOG reset due to
1184 * the write to 'sp->int_regs' and the box reboots.
1185 * Commenting this out is good since it allows the
1186 * system to resume when bond0 is made up again.
1187 */
1188static int ar2313_close(struct net_device *dev)
1189{
1190#if 0
1191        /*
1192         * Disable interrupts
1193         */
1194        disable_irq(dev->irq);
1195
1196        /*
1197         * Without (or before) releasing irq and stopping hardware, this
1198         * is an absolute non-sense, by the way. It will be reset instantly
1199         * by the first irq.
1200         */
1201        netif_stop_queue(dev);
1202
1203        /* stop the MAC and DMA engines */
1204        ar2313_halt(dev);
1205
1206        /* release the interrupt */
1207        free_irq(dev->irq, dev);
1208
1209#endif
1210        return 0;
1211}
1212
1213static int ar2313_start_xmit(struct sk_buff *skb, struct net_device *dev)
1214{
1215        struct ar2313_private *sp = netdev_priv(dev);
1216        ar2313_descr_t *td;
1217        u32 idx;
1218
1219        idx = sp->tx_prd;
1220        td = &sp->tx_ring[idx];
1221
1222        if (td->status & DMA_TX_OWN) {
1223#if DEBUG_TX
1224                printk("%s: No space left to Tx\n", __FUNCTION__);
1225#endif
1226                /* free skbuf and lie to the caller that we sent it out */
1227                dev->stats.tx_dropped++;
1228                dev_kfree_skb(skb);
1229
1230                /* restart transmitter in case locked */
1231                sp->dma_regs->xmt_poll = 0;
1232                return 0;
1233        }
1234
1235        /* Setup the transmit descriptor. */
1236        td->devcs = ((skb->len << DMA_TX1_BSIZE_SHIFT) |
1237                                 (DMA_TX1_LS | DMA_TX1_IC | DMA_TX1_CHAINED));
1238        td->addr = dma_map_single(NULL, skb->data, skb->len, DMA_TO_DEVICE);
1239        td->status = DMA_TX_OWN;
1240
1241        /* kick transmitter last */
1242        sp->dma_regs->xmt_poll = 0;
1243
1244#if DEBUG_TX
1245        printk("index %d\n", idx);
1246        printk("TX status %08x\n", td->status);
1247        printk("TX devcs  %08x\n", td->devcs);
1248        printk("TX addr   %08x\n", td->addr);
1249        printk("TX descr  %08x\n", td->descr);
1250#endif
1251
1252        sp->tx_skb[idx] = skb;
1253        idx = DSC_NEXT(idx);
1254        sp->tx_prd = idx;
1255
1256        return 0;
1257}
1258static short armiiread (struct net_device *dev, short phy, short reg);
1259static void armiiwrite (struct net_device *dev, short phy, short reg,
1260                        short data);
1261
1262static int ar2313_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1263{
1264        struct mii_ioctl_data *data = (struct mii_ioctl_data *) &ifr->ifr_data;
1265        struct ar2313_private *sp = netdev_priv(dev);
1266        int ret;
1267
1268        switch (cmd) {
1269
1270        case SIOCETHTOOL:
1271                spin_lock_irq(&sp->lock);
1272                ret = phy_ethtool_ioctl(sp->phy_dev, (void *) ifr->ifr_data);
1273                spin_unlock_irq(&sp->lock);
1274                return ret;
1275
1276        case SIOCSIFHWADDR:
1277                if (copy_from_user
1278                        (dev->dev_addr, ifr->ifr_data, sizeof(dev->dev_addr)))
1279                        return -EFAULT;
1280                return 0;
1281
1282        case SIOCGIFHWADDR:
1283                if (copy_to_user
1284                        (ifr->ifr_data, dev->dev_addr, sizeof(dev->dev_addr)))
1285                        return -EFAULT;
1286                return 0;
1287
1288    case SIOCGMIIPHY:           /* Get address of MII PHY in use. */
1289      data->phy_id = 1;
1290      /* Fall Through */
1291
1292    case SIOCGMIIREG:           /* Read MII PHY register. */
1293      data->val_out = armiiread (dev, data->phy_id & 0x1f,
1294                                 data->reg_num & 0x1f);
1295      return 0;
1296    case SIOCSMIIREG:           /* Write MII PHY register. */
1297      if (!capable (CAP_NET_ADMIN))
1298        return -EPERM;
1299      armiiwrite (dev, data->phy_id & 0x1f,
1300                  data->reg_num & 0x1f, data->val_in);
1301      return 0;
1302
1303        default:
1304                break;
1305        }
1306
1307        return -EOPNOTSUPP;
1308}
1309
1310static void ar2313_adjust_link(struct net_device *dev)
1311{
1312        struct ar2313_private *sp = netdev_priv(dev);
1313        unsigned int mc;
1314
1315        if (!sp->phy_dev->link)
1316                return;
1317
1318        if (sp->phy_dev->duplex != sp->oldduplex) {
1319                mc = readl(&sp->eth_regs->mac_control);
1320                mc &= ~(MAC_CONTROL_F | MAC_CONTROL_DRO);
1321                if (sp->phy_dev->duplex)
1322                        mc |= MAC_CONTROL_F;
1323                else
1324                        mc |= MAC_CONTROL_DRO;
1325                writel(mc, &sp->eth_regs->mac_control);
1326                sp->oldduplex = sp->phy_dev->duplex;
1327        }
1328}
1329
1330#define MII_ADDR(phy, reg) \
1331        ((reg << MII_ADDR_REG_SHIFT) | (phy << MII_ADDR_PHY_SHIFT))
1332
1333
1334
1335
1336static short
1337armiiread (struct net_device *dev, short phy, short reg)
1338{
1339
1340  short outp;
1341
1342  struct ar2313_private *sp = (struct ar2313_private *) dev->priv;
1343  volatile ETHERNET_STRUCT *ethernet = sp->phy_regs;
1344  ethernet->mii_addr = MII_ADDR (phy, reg);
1345  while (ethernet->mii_addr & MII_ADDR_BUSY);
1346  outp = ethernet->mii_data >> MII_DATA_SHIFT;
1347  return (outp);
1348}
1349
1350static void
1351armiiwrite (struct net_device *dev, short phy, short reg, short data)
1352{
1353  struct ar2313_private *sp = (struct ar2313_private *) dev->priv;
1354  volatile ETHERNET_STRUCT *ethernet = sp->phy_regs;
1355  while (ethernet->mii_addr & MII_ADDR_BUSY);
1356  ethernet->mii_data = data << MII_DATA_SHIFT;
1357  ethernet->mii_addr = MII_ADDR (phy, reg) | MII_ADDR_WRITE;
1358}
1359
1360static int
1361mdiobus_read(struct mii_bus *bus, int phy_addr, int regnum)
1362{
1363        struct net_device *const dev = bus->priv;
1364        struct ar2313_private *sp = netdev_priv(dev);
1365        volatile ETHERNET_STRUCT *ethernet = sp->phy_regs;
1366
1367        ethernet->mii_addr = MII_ADDR(phy_addr, regnum);
1368        while (ethernet->mii_addr & MII_ADDR_BUSY);
1369        return (ethernet->mii_data >> MII_DATA_SHIFT);
1370}
1371
1372static int
1373mdiobus_write(struct mii_bus *bus, int phy_addr, int regnum,
1374             u16 value)
1375{
1376        struct net_device *const dev = bus->priv;
1377        struct ar2313_private *sp = netdev_priv(dev);
1378        volatile ETHERNET_STRUCT *ethernet = sp->phy_regs;
1379
1380        while (ethernet->mii_addr & MII_ADDR_BUSY);
1381        ethernet->mii_data = value << MII_DATA_SHIFT;
1382        ethernet->mii_addr = MII_ADDR(phy_addr, regnum) | MII_ADDR_WRITE;
1383
1384        return 0;
1385}
1386
1387static int mdiobus_reset(struct mii_bus *bus)
1388{
1389        struct net_device *const dev = bus->priv;
1390
1391        ar2313_reset_reg(dev);
1392
1393        return 0;
1394}
1395
1396static int mdiobus_probe (struct net_device *dev)
1397{
1398        struct ar2313_private *const sp = netdev_priv(dev);
1399        struct phy_device *phydev = NULL;
1400        int phy_addr;
1401
1402        /* find the first (lowest address) PHY on the current MAC's MII bus */
1403        for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++)
1404                if (sp->mii_bus.phy_map[phy_addr]) {
1405                        phydev = sp->mii_bus.phy_map[phy_addr];
1406                        break; /* break out with first one found */
1407                }
1408
1409        if (!phydev) {
1410                printk (KERN_ERR "ar2313:%s: no PHY found\n", dev->name);
1411                return -1;
1412        }
1413
1414        /* now we are supposed to have a proper phydev, to attach to... */
1415        BUG_ON(!phydev);
1416        BUG_ON(phydev->attached_dev);
1417
1418        phydev = phy_connect(dev, phydev->dev.bus_id, &ar2313_adjust_link, 0,
1419                PHY_INTERFACE_MODE_MII);
1420
1421        if (IS_ERR(phydev)) {
1422                printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
1423                return PTR_ERR(phydev);
1424        }
1425
1426        sp->rx = phydev->netif_rx;
1427
1428        /* mask with MAC supported features */
1429        phydev->supported &= (SUPPORTED_10baseT_Half
1430                | SUPPORTED_10baseT_Full
1431                | SUPPORTED_100baseT_Half
1432                | SUPPORTED_100baseT_Full
1433                | SUPPORTED_Autoneg
1434                /* | SUPPORTED_Pause | SUPPORTED_Asym_Pause */
1435                | SUPPORTED_MII
1436                | SUPPORTED_TP);
1437
1438        phydev->advertising = phydev->supported;
1439
1440        sp->oldduplex = -1;
1441        sp->phy_dev = phydev;
1442
1443        printk(KERN_INFO "%s: attached PHY driver [%s] "
1444                "(mii_bus:phy_addr=%s)\n",
1445                dev->name, phydev->drv->name, phydev->dev.bus_id);
1446
1447        return 0;
1448}
1449
Note: See TracBrowser for help on using the repository browser.