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

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

current fixes

File size: 35.2 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
844                if (sp->rx_skb[idx]) {
845#if DEBUG_RX
846                        printk(KERN_INFO "ar2313 rx refill full\n");
847#endif                                                  /* DEBUG */
848                        break;
849                }
850                // partha: create additional room for the second GRE fragment
851                skb = alloc_skb(AR2313_BUFSIZE + 128, GFP_ATOMIC);
852                if (!skb) {
853                        printk("\n\n\n\n %s: No memory in system\n\n\n\n",
854                                   __FUNCTION__);
855                        break;
856                }
857                // partha: create additional room in the front for tx pkt capture
858                skb_reserve(skb, 32);
859
860                /*
861                 * Make sure IP header starts on a fresh cache line.
862                 */
863                skb->dev = dev;
864                skb_reserve(skb, RX_OFFSET);
865                sp->rx_skb[idx] = skb;
866
867                rd = (ar2313_descr_t *) & sp->rx_ring[idx];
868
869                /* initialize dma descriptor */
870                rd->devcs = ((AR2313_BUFSIZE << DMA_RX1_BSIZE_SHIFT) |
871                                         DMA_RX1_CHAINED);
872                rd->addr = virt_to_phys(skb->data);
873                rd->descr =
874                        virt_to_phys(&sp->
875                                                 rx_ring[(idx + 1) & (AR2313_DESCR_ENTRIES - 1)]);
876                rd->status = DMA_RX_OWN;
877
878                idx = DSC_NEXT(idx);
879        }
880
881        if (!i) {
882#if DEBUG_ERR
883                printk(KERN_INFO
884                           "Out of memory when allocating standard receive buffers\n");
885#endif                                                  /* DEBUG */
886        } else {
887                sp->rx_skbprd = idx;
888        }
889
890        return;
891}
892
893#define AR2313_MAX_PKTS_PER_CALL        64
894
895static int ar2313_rx_int(struct net_device *dev)
896{
897        struct ar2313_private *sp = netdev_priv(dev);
898        struct sk_buff *skb, *skb_new;
899        ar2313_descr_t *rxdesc;
900        unsigned int status;
901        u32 idx;
902        int pkts = 0;
903        int rval;
904
905        idx = sp->cur_rx;
906
907        /* process at most the entire ring and then wait for another interrupt
908         */
909        while (1) {
910
911                rxdesc = &sp->rx_ring[idx];
912                status = rxdesc->status;
913                if (status & DMA_RX_OWN) {
914                        /* SiByte owns descriptor or descr not yet filled in */
915                        rval = 0;
916                        break;
917                }
918
919                if (++pkts > AR2313_MAX_PKTS_PER_CALL) {
920                        rval = 1;
921                        break;
922                }
923#if DEBUG_RX
924                printk("index %d\n", idx);
925                printk("RX status %08x\n", rxdesc->status);
926                printk("RX devcs  %08x\n", rxdesc->devcs);
927                printk("RX addr   %08x\n", rxdesc->addr);
928                printk("RX descr  %08x\n", rxdesc->descr);
929#endif
930
931                if ((status & (DMA_RX_ERROR | DMA_RX_ERR_LENGTH)) &&
932                        (!(status & DMA_RX_LONG))) {
933#if DEBUG_RX
934                        printk("%s: rx ERROR %08x\n", __FUNCTION__, status);
935#endif
936                        dev->stats.rx_errors++;
937                        dev->stats.rx_dropped++;
938
939                        /* add statistics counters */
940                        if (status & DMA_RX_ERR_CRC)
941                                dev->stats.rx_crc_errors++;
942                        if (status & DMA_RX_ERR_COL)
943                                dev->stats.rx_over_errors++;
944                        if (status & DMA_RX_ERR_LENGTH)
945                                dev->stats.rx_length_errors++;
946                        if (status & DMA_RX_ERR_RUNT)
947                                dev->stats.rx_over_errors++;
948                        if (status & DMA_RX_ERR_DESC)
949                                dev->stats.rx_over_errors++;
950
951                } else {
952                        /* alloc new buffer. */
953                        skb_new = dev_alloc_skb(AR2313_BUFSIZE + RX_OFFSET + 128);
954                        if (skb_new != NULL) {
955
956                                skb = sp->rx_skb[idx];
957                                /* set skb */
958                                skb_put(skb,
959                                                ((status >> DMA_RX_LEN_SHIFT) & 0x3fff) - CRC_LEN);
960
961                                dev->stats.rx_bytes += skb->len;
962
963                                /* pass the packet to upper layers */
964                                sp->rx(skb);
965
966                                skb_new->dev = dev;
967                                /* 16 bit align */
968                                skb_reserve(skb_new, RX_OFFSET + 32);
969                                /* reset descriptor's curr_addr */
970                                rxdesc->addr = virt_to_phys(skb_new->data);
971
972                                dev->stats.rx_packets++;
973                                sp->rx_skb[idx] = skb_new;
974                        } else {
975                                dev->stats.rx_dropped++;
976                        }
977                }
978
979                rxdesc->devcs = ((AR2313_BUFSIZE << DMA_RX1_BSIZE_SHIFT) |
980                                                 DMA_RX1_CHAINED);
981                rxdesc->status = DMA_RX_OWN;
982
983                idx = DSC_NEXT(idx);
984        }
985
986        sp->cur_rx = idx;
987
988        return rval;
989}
990
991
992static void ar2313_tx_int(struct net_device *dev)
993{
994        struct ar2313_private *sp = netdev_priv(dev);
995        u32 idx;
996        struct sk_buff *skb;
997        ar2313_descr_t *txdesc;
998        unsigned int status = 0;
999
1000        idx = sp->tx_csm;
1001
1002        while (idx != sp->tx_prd) {
1003
1004                txdesc = &sp->tx_ring[idx];
1005
1006#if DEBUG_TX
1007                printk
1008                        ("%s: TXINT: csm=%d idx=%d prd=%d status=%x devcs=%x addr=%08x descr=%x\n",
1009                         dev->name, sp->tx_csm, idx, sp->tx_prd, txdesc->status,
1010                         txdesc->devcs, txdesc->addr, txdesc->descr);
1011#endif                                                  /* DEBUG */
1012
1013                if ((status = txdesc->status) & DMA_TX_OWN) {
1014                        /* ar2313 dma still owns descr */
1015                        break;
1016                }
1017                /* done with this descriptor */
1018                dma_unmap_single(NULL, txdesc->addr,
1019                                                 txdesc->devcs & DMA_TX1_BSIZE_MASK,
1020                                                 DMA_TO_DEVICE);
1021                txdesc->status = 0;
1022
1023                if (status & DMA_TX_ERROR) {
1024                        dev->stats.tx_errors++;
1025                        dev->stats.tx_dropped++;
1026                        if (status & DMA_TX_ERR_UNDER)
1027                                dev->stats.tx_fifo_errors++;
1028                        if (status & DMA_TX_ERR_HB)
1029                                dev->stats.tx_heartbeat_errors++;
1030                        if (status & (DMA_TX_ERR_LOSS | DMA_TX_ERR_LINK))
1031                                dev->stats.tx_carrier_errors++;
1032                        if (status & (DMA_TX_ERR_LATE |
1033                                                  DMA_TX_ERR_COL |
1034                                                  DMA_TX_ERR_JABBER | DMA_TX_ERR_DEFER))
1035                                dev->stats.tx_aborted_errors++;
1036                } else {
1037                        /* transmit OK */
1038                        dev->stats.tx_packets++;
1039                }
1040
1041                skb = sp->tx_skb[idx];
1042                sp->tx_skb[idx] = NULL;
1043                idx = DSC_NEXT(idx);
1044                dev->stats.tx_bytes += skb->len;
1045                dev_kfree_skb_irq(skb);
1046        }
1047
1048        sp->tx_csm = idx;
1049
1050        return;
1051}
1052
1053
1054static void rx_tasklet_func(unsigned long data)
1055{
1056        struct net_device *dev = (struct net_device *) data;
1057        struct ar2313_private *sp = netdev_priv(dev);
1058
1059        if (sp->unloading) {
1060                return;
1061        }
1062
1063        if (ar2313_rx_int(dev)) {
1064                tasklet_hi_schedule(&sp->rx_tasklet);
1065        } else {
1066                unsigned long flags;
1067                spin_lock_irqsave(&sp->lock, flags);
1068                sp->dma_regs->intr_ena |= DMA_STATUS_RI;
1069                spin_unlock_irqrestore(&sp->lock, flags);
1070        }
1071}
1072
1073static void rx_schedule(struct net_device *dev)
1074{
1075        struct ar2313_private *sp = netdev_priv(dev);
1076
1077        sp->dma_regs->intr_ena &= ~DMA_STATUS_RI;
1078
1079        tasklet_hi_schedule(&sp->rx_tasklet);
1080}
1081
1082static irqreturn_t ar2313_interrupt(int irq, void *dev_id)
1083{
1084        struct net_device *dev = (struct net_device *) dev_id;
1085        struct ar2313_private *sp = netdev_priv(dev);
1086        unsigned int status, enabled;
1087
1088        /* clear interrupt */
1089        /*
1090         * Don't clear RI bit if currently disabled.
1091         */
1092        status = sp->dma_regs->status;
1093        enabled = sp->dma_regs->intr_ena;
1094        sp->dma_regs->status = status & enabled;
1095
1096        if (status & DMA_STATUS_NIS) {
1097                /* normal status */
1098                /*
1099                 * Don't schedule rx processing if interrupt
1100                 * is already disabled.
1101                 */
1102                if (status & enabled & DMA_STATUS_RI) {
1103                        /* receive interrupt */
1104                        rx_schedule(dev);
1105                }
1106                if (status & DMA_STATUS_TI) {
1107                        /* transmit interrupt */
1108                        ar2313_tx_int(dev);
1109                }
1110        }
1111
1112        if (status & DMA_STATUS_AIS) {
1113#if DEBUG_INT
1114                printk("%s: AIS set %08x & %x\n", __FUNCTION__,
1115                           status, (DMA_STATUS_FBE | DMA_STATUS_TPS));
1116#endif
1117                /* abnormal status */
1118                if (status & (DMA_STATUS_FBE | DMA_STATUS_TPS)) {
1119                        ar2313_restart(dev);
1120                }
1121        }
1122        return IRQ_HANDLED;
1123}
1124
1125
1126static int ar2313_open(struct net_device *dev)
1127{
1128        struct ar2313_private *sp = netdev_priv(dev);
1129
1130        dev->mtu = 1500;
1131        netif_start_queue(dev);
1132
1133        sp->eth_regs->mac_control |= MAC_CONTROL_RE;
1134
1135        return 0;
1136}
1137
1138static void ar2313_halt(struct net_device *dev)
1139{
1140        struct ar2313_private *sp = netdev_priv(dev);
1141        int j;
1142
1143        tasklet_disable(&sp->rx_tasklet);
1144
1145        /* kill the MAC */
1146        sp->eth_regs->mac_control &= ~(MAC_CONTROL_RE | /* disable Receives */
1147                                                                   MAC_CONTROL_TE);     /* disable Transmits */
1148        /* stop dma */
1149        sp->dma_regs->control = 0;
1150        sp->dma_regs->bus_mode = DMA_BUS_MODE_SWR;
1151
1152        /* place phy and MAC in reset */
1153        *sp->int_regs |= (sp->cfg->reset_mac | sp->cfg->reset_phy);
1154
1155        /* free buffers on tx ring */
1156        for (j = 0; j < AR2313_DESCR_ENTRIES; j++) {
1157                struct sk_buff *skb;
1158                ar2313_descr_t *txdesc;
1159
1160                txdesc = &sp->tx_ring[j];
1161                txdesc->descr = 0;
1162
1163                skb = sp->tx_skb[j];
1164                if (skb) {
1165                        dev_kfree_skb(skb);
1166                        sp->tx_skb[j] = NULL;
1167                }
1168        }
1169}
1170
1171/*
1172 * close should do nothing. Here's why. It's called when
1173 * 'ifconfig bond0 down' is run. If it calls free_irq then
1174 * the irq is gone forever ! When bond0 is made 'up' again,
1175 * the ar2313_open () does not call request_irq (). Worse,
1176 * the call to ar2313_halt() generates a WDOG reset due to
1177 * the write to 'sp->int_regs' and the box reboots.
1178 * Commenting this out is good since it allows the
1179 * system to resume when bond0 is made up again.
1180 */
1181static int ar2313_close(struct net_device *dev)
1182{
1183#if 0
1184        /*
1185         * Disable interrupts
1186         */
1187        disable_irq(dev->irq);
1188
1189        /*
1190         * Without (or before) releasing irq and stopping hardware, this
1191         * is an absolute non-sense, by the way. It will be reset instantly
1192         * by the first irq.
1193         */
1194        netif_stop_queue(dev);
1195
1196        /* stop the MAC and DMA engines */
1197        ar2313_halt(dev);
1198
1199        /* release the interrupt */
1200        free_irq(dev->irq, dev);
1201
1202#endif
1203        return 0;
1204}
1205
1206static int ar2313_start_xmit(struct sk_buff *skb, struct net_device *dev)
1207{
1208        struct ar2313_private *sp = netdev_priv(dev);
1209        ar2313_descr_t *td;
1210        u32 idx;
1211
1212        idx = sp->tx_prd;
1213        td = &sp->tx_ring[idx];
1214
1215        if (td->status & DMA_TX_OWN) {
1216#if DEBUG_TX
1217                printk("%s: No space left to Tx\n", __FUNCTION__);
1218#endif
1219                /* free skbuf and lie to the caller that we sent it out */
1220                dev->stats.tx_dropped++;
1221                dev_kfree_skb(skb);
1222
1223                /* restart transmitter in case locked */
1224                sp->dma_regs->xmt_poll = 0;
1225                return 0;
1226        }
1227
1228        /* Setup the transmit descriptor. */
1229        td->devcs = ((skb->len << DMA_TX1_BSIZE_SHIFT) |
1230                                 (DMA_TX1_LS | DMA_TX1_IC | DMA_TX1_CHAINED));
1231        td->addr = dma_map_single(NULL, skb->data, skb->len, DMA_TO_DEVICE);
1232        td->status = DMA_TX_OWN;
1233
1234        /* kick transmitter last */
1235        sp->dma_regs->xmt_poll = 0;
1236
1237#if DEBUG_TX
1238        printk("index %d\n", idx);
1239        printk("TX status %08x\n", td->status);
1240        printk("TX devcs  %08x\n", td->devcs);
1241        printk("TX addr   %08x\n", td->addr);
1242        printk("TX descr  %08x\n", td->descr);
1243#endif
1244
1245        sp->tx_skb[idx] = skb;
1246        idx = DSC_NEXT(idx);
1247        sp->tx_prd = idx;
1248
1249        return 0;
1250}
1251static short armiiread (struct net_device *dev, short phy, short reg);
1252static void armiiwrite (struct net_device *dev, short phy, short reg,
1253                        short data);
1254
1255static int ar2313_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1256{
1257        struct mii_ioctl_data *data = (struct mii_ioctl_data *) &ifr->ifr_data;
1258        struct ar2313_private *sp = netdev_priv(dev);
1259        int ret;
1260
1261        switch (cmd) {
1262
1263        case SIOCETHTOOL:
1264                spin_lock_irq(&sp->lock);
1265                ret = phy_ethtool_ioctl(sp->phy_dev, (void *) ifr->ifr_data);
1266                spin_unlock_irq(&sp->lock);
1267                return ret;
1268
1269        case SIOCSIFHWADDR:
1270                if (copy_from_user
1271                        (dev->dev_addr, ifr->ifr_data, sizeof(dev->dev_addr)))
1272                        return -EFAULT;
1273                return 0;
1274
1275        case SIOCGIFHWADDR:
1276                if (copy_to_user
1277                        (ifr->ifr_data, dev->dev_addr, sizeof(dev->dev_addr)))
1278                        return -EFAULT;
1279                return 0;
1280
1281    case SIOCGMIIPHY:           /* Get address of MII PHY in use. */
1282      data->phy_id = 1;
1283      /* Fall Through */
1284
1285    case SIOCGMIIREG:           /* Read MII PHY register. */
1286      data->val_out = armiiread (dev, data->phy_id & 0x1f,
1287                                 data->reg_num & 0x1f);
1288      return 0;
1289    case SIOCSMIIREG:           /* Write MII PHY register. */
1290      if (!capable (CAP_NET_ADMIN))
1291        return -EPERM;
1292      armiiwrite (dev, data->phy_id & 0x1f,
1293                  data->reg_num & 0x1f, data->val_in);
1294      return 0;
1295
1296        default:
1297                break;
1298        }
1299
1300        return -EOPNOTSUPP;
1301}
1302
1303static void ar2313_adjust_link(struct net_device *dev)
1304{
1305        struct ar2313_private *sp = netdev_priv(dev);
1306        unsigned int mc;
1307
1308        if (!sp->phy_dev->link)
1309                return;
1310
1311        if (sp->phy_dev->duplex != sp->oldduplex) {
1312                mc = readl(&sp->eth_regs->mac_control);
1313                mc &= ~(MAC_CONTROL_F | MAC_CONTROL_DRO);
1314                if (sp->phy_dev->duplex)
1315                        mc |= MAC_CONTROL_F;
1316                else
1317                        mc |= MAC_CONTROL_DRO;
1318                writel(mc, &sp->eth_regs->mac_control);
1319                sp->oldduplex = sp->phy_dev->duplex;
1320        }
1321}
1322
1323#define MII_ADDR(phy, reg) \
1324        ((reg << MII_ADDR_REG_SHIFT) | (phy << MII_ADDR_PHY_SHIFT))
1325
1326
1327
1328
1329static short
1330armiiread (struct net_device *dev, short phy, short reg)
1331{
1332
1333  short outp;
1334
1335  struct ar2313_private *sp = (struct ar2313_private *) dev->priv;
1336  volatile ETHERNET_STRUCT *ethernet = sp->phy_regs;
1337  ethernet->mii_addr = MII_ADDR (phy, reg);
1338  while (ethernet->mii_addr & MII_ADDR_BUSY);
1339  outp = ethernet->mii_data >> MII_DATA_SHIFT;
1340  return (outp);
1341}
1342
1343static void
1344armiiwrite (struct net_device *dev, short phy, short reg, short data)
1345{
1346  struct ar2313_private *sp = (struct ar2313_private *) dev->priv;
1347  volatile ETHERNET_STRUCT *ethernet = sp->phy_regs;
1348  while (ethernet->mii_addr & MII_ADDR_BUSY);
1349  ethernet->mii_data = data << MII_DATA_SHIFT;
1350  ethernet->mii_addr = MII_ADDR (phy, reg) | MII_ADDR_WRITE;
1351}
1352
1353static int
1354mdiobus_read(struct mii_bus *bus, int phy_addr, int regnum)
1355{
1356        struct net_device *const dev = bus->priv;
1357        struct ar2313_private *sp = netdev_priv(dev);
1358        volatile ETHERNET_STRUCT *ethernet = sp->phy_regs;
1359
1360        ethernet->mii_addr = MII_ADDR(phy_addr, regnum);
1361        while (ethernet->mii_addr & MII_ADDR_BUSY);
1362        return (ethernet->mii_data >> MII_DATA_SHIFT);
1363}
1364
1365static int
1366mdiobus_write(struct mii_bus *bus, int phy_addr, int regnum,
1367             u16 value)
1368{
1369        struct net_device *const dev = bus->priv;
1370        struct ar2313_private *sp = netdev_priv(dev);
1371        volatile ETHERNET_STRUCT *ethernet = sp->phy_regs;
1372
1373        while (ethernet->mii_addr & MII_ADDR_BUSY);
1374        ethernet->mii_data = value << MII_DATA_SHIFT;
1375        ethernet->mii_addr = MII_ADDR(phy_addr, regnum) | MII_ADDR_WRITE;
1376
1377        return 0;
1378}
1379
1380static int mdiobus_reset(struct mii_bus *bus)
1381{
1382        struct net_device *const dev = bus->priv;
1383
1384        ar2313_reset_reg(dev);
1385
1386        return 0;
1387}
1388
1389static int mdiobus_probe (struct net_device *dev)
1390{
1391        struct ar2313_private *const sp = netdev_priv(dev);
1392        struct phy_device *phydev = NULL;
1393        int phy_addr;
1394
1395        /* find the first (lowest address) PHY on the current MAC's MII bus */
1396        for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++)
1397                if (sp->mii_bus.phy_map[phy_addr]) {
1398                        phydev = sp->mii_bus.phy_map[phy_addr];
1399                        break; /* break out with first one found */
1400                }
1401
1402        if (!phydev) {
1403                printk (KERN_ERR "ar2313:%s: no PHY found\n", dev->name);
1404                return -1;
1405        }
1406
1407        /* now we are supposed to have a proper phydev, to attach to... */
1408        BUG_ON(!phydev);
1409        BUG_ON(phydev->attached_dev);
1410
1411        phydev = phy_connect(dev, phydev->dev.bus_id, &ar2313_adjust_link, 0,
1412                PHY_INTERFACE_MODE_MII);
1413
1414        if (IS_ERR(phydev)) {
1415                printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
1416                return PTR_ERR(phydev);
1417        }
1418
1419        sp->rx = phydev->netif_rx;
1420
1421        /* mask with MAC supported features */
1422        phydev->supported &= (SUPPORTED_10baseT_Half
1423                | SUPPORTED_10baseT_Full
1424                | SUPPORTED_100baseT_Half
1425                | SUPPORTED_100baseT_Full
1426                | SUPPORTED_Autoneg
1427                /* | SUPPORTED_Pause | SUPPORTED_Asym_Pause */
1428                | SUPPORTED_MII
1429                | SUPPORTED_TP);
1430
1431        phydev->advertising = phydev->supported;
1432
1433        sp->oldduplex = -1;
1434        sp->phy_dev = phydev;
1435
1436        printk(KERN_INFO "%s: attached PHY driver [%s] "
1437                "(mii_bus:phy_addr=%s)\n",
1438                dev->name, phydev->drv->name, phydev->dev.bus_id);
1439
1440        return 0;
1441}
1442
Note: See TracBrowser for help on using the repository browser.