Root/target/linux/generic-2.6/files/drivers/net/phy/ar8216.c

1/*
2 * ar8216.c: AR8216 switch driver
3 *
4 * Copyright (C) 2009 Felix Fietkau <nbd@openwrt.org>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17#include <linux/if.h>
18#include <linux/module.h>
19#include <linux/init.h>
20#include <linux/list.h>
21#include <linux/if_ether.h>
22#include <linux/skbuff.h>
23#include <linux/netdevice.h>
24#include <linux/netlink.h>
25#include <linux/bitops.h>
26#include <net/genetlink.h>
27#include <linux/switch.h>
28#include <linux/delay.h>
29#include <linux/phy.h>
30#include <linux/netdevice.h>
31#include <linux/etherdevice.h>
32#include "ar8216.h"
33
34/* size of the vlan table */
35#define AR8X16_MAX_VLANS 128
36#define AR8X16_PROBE_RETRIES 10
37
38struct ar8216_priv {
39    struct switch_dev dev;
40    struct phy_device *phy;
41    u32 (*read)(struct ar8216_priv *priv, int reg);
42    void (*write)(struct ar8216_priv *priv, int reg, u32 val);
43    const struct net_device_ops *ndo_old;
44    struct net_device_ops ndo;
45    struct mutex reg_mutex;
46    int chip;
47
48    /* all fields below are cleared on reset */
49    bool vlan;
50    u16 vlan_id[AR8X16_MAX_VLANS];
51    u8 vlan_table[AR8X16_MAX_VLANS];
52    u8 vlan_tagged;
53    u16 pvid[AR8216_NUM_PORTS];
54};
55static struct switch_dev athdev;
56
57#define to_ar8216(_dev) container_of(_dev, struct ar8216_priv, dev)
58
59static inline void
60split_addr(u32 regaddr, u16 *r1, u16 *r2, u16 *page)
61{
62    regaddr >>= 1;
63    *r1 = regaddr & 0x1e;
64
65    regaddr >>= 5;
66    *r2 = regaddr & 0x7;
67
68    regaddr >>= 3;
69    *page = regaddr & 0x1ff;
70}
71
72static u32
73ar8216_mii_read(struct ar8216_priv *priv, int reg)
74{
75    struct phy_device *phy = priv->phy;
76    u16 r1, r2, page;
77    u16 lo, hi;
78
79    split_addr((u32) reg, &r1, &r2, &page);
80    phy->bus->write(phy->bus, 0x18, 0, page);
81    msleep(1); /* wait for the page switch to propagate */
82    lo = phy->bus->read(phy->bus, 0x10 | r2, r1);
83    hi = phy->bus->read(phy->bus, 0x10 | r2, r1 + 1);
84
85    return (hi << 16) | lo;
86}
87
88static void
89ar8216_mii_write(struct ar8216_priv *priv, int reg, u32 val)
90{
91    struct phy_device *phy = priv->phy;
92    u16 r1, r2, r3;
93    u16 lo, hi;
94
95    split_addr((u32) reg, &r1, &r2, &r3);
96    phy->bus->write(phy->bus, 0x18, 0, r3);
97    msleep(1); /* wait for the page switch to propagate */
98
99    lo = val & 0xffff;
100    hi = (u16) (val >> 16);
101    phy->bus->write(phy->bus, 0x10 | r2, r1 + 1, hi);
102    phy->bus->write(phy->bus, 0x10 | r2, r1, lo);
103}
104
105static u32
106ar8216_rmw(struct ar8216_priv *priv, int reg, u32 mask, u32 val)
107{
108    u32 v;
109
110    v = priv->read(priv, reg);
111    v &= ~mask;
112    v |= val;
113    priv->write(priv, reg, v);
114
115    return v;
116}
117
118static inline int
119ar8216_id_chip(struct ar8216_priv *priv)
120{
121    u32 val;
122    u16 id;
123    int i;
124
125    val = ar8216_mii_read(priv, AR8216_REG_CTRL);
126    if (val == ~0)
127        return UNKNOWN;
128
129    id = val & (AR8216_CTRL_REVISION | AR8216_CTRL_VERSION);
130    for (i = 0; i < AR8X16_PROBE_RETRIES; i++) {
131        u16 t;
132
133        val = ar8216_mii_read(priv, AR8216_REG_CTRL);
134        if (val == ~0)
135            return UNKNOWN;
136
137        t = val & (AR8216_CTRL_REVISION | AR8216_CTRL_VERSION);
138        if (t != id)
139            return UNKNOWN;
140    }
141
142    switch (id) {
143    case 0x0101:
144        return AR8216;
145    case 0x1001:
146        return AR8316;
147    default:
148        printk(KERN_DEBUG
149            "ar8216: Unknown Atheros device [ver=%d, rev=%d, phy_id=%04x%04x]\n",
150            (int)(id >> AR8216_CTRL_VERSION_S),
151            (int)(id & AR8216_CTRL_REVISION),
152            priv->phy->bus->read(priv->phy->bus, priv->phy->addr, 2),
153            priv->phy->bus->read(priv->phy->bus, priv->phy->addr, 3));
154
155        return UNKNOWN;
156    }
157}
158
159static int
160ar8216_set_vlan(struct switch_dev *dev, const struct switch_attr *attr,
161                struct switch_val *val)
162{
163    struct ar8216_priv *priv = to_ar8216(dev);
164    priv->vlan = !!val->value.i;
165    return 0;
166}
167
168static int
169ar8216_get_vlan(struct switch_dev *dev, const struct switch_attr *attr,
170                struct switch_val *val)
171{
172    struct ar8216_priv *priv = to_ar8216(dev);
173    val->value.i = priv->vlan;
174    return 0;
175}
176
177
178static int
179ar8216_set_pvid(struct switch_dev *dev, int port, int vlan)
180{
181    struct ar8216_priv *priv = to_ar8216(dev);
182
183    /* make sure no invalid PVIDs get set */
184
185    if (vlan >= dev->vlans)
186        return -EINVAL;
187
188    priv->pvid[port] = vlan;
189    return 0;
190}
191
192static int
193ar8216_get_pvid(struct switch_dev *dev, int port, int *vlan)
194{
195    struct ar8216_priv *priv = to_ar8216(dev);
196    *vlan = priv->pvid[port];
197    return 0;
198}
199
200static int
201ar8216_set_vid(struct switch_dev *dev, const struct switch_attr *attr,
202                struct switch_val *val)
203{
204    struct ar8216_priv *priv = to_ar8216(dev);
205    priv->vlan_id[val->port_vlan] = val->value.i;
206    return 0;
207}
208
209static int
210ar8216_get_vid(struct switch_dev *dev, const struct switch_attr *attr,
211                struct switch_val *val)
212{
213    struct ar8216_priv *priv = to_ar8216(dev);
214    val->value.i = priv->vlan_id[val->port_vlan];
215    return 0;
216}
217
218
219static int
220ar8216_mangle_tx(struct sk_buff *skb, struct net_device *dev)
221{
222    struct ar8216_priv *priv = dev->phy_ptr;
223    unsigned char *buf;
224
225    if (unlikely(!priv))
226        goto error;
227
228    if (!priv->vlan)
229        goto send;
230
231    if (unlikely(skb_headroom(skb) < 2)) {
232        if (pskb_expand_head(skb, 2, 0, GFP_ATOMIC) < 0)
233            goto error;
234    }
235
236    buf = skb_push(skb, 2);
237    buf[0] = 0x10;
238    buf[1] = 0x80;
239
240send:
241    return priv->ndo_old->ndo_start_xmit(skb, dev);
242
243error:
244    dev_kfree_skb_any(skb);
245    return 0;
246}
247
248static int
249ar8216_mangle_rx(struct sk_buff *skb, int napi)
250{
251    struct ar8216_priv *priv;
252    struct net_device *dev;
253    unsigned char *buf;
254    int port, vlan;
255
256    dev = skb->dev;
257    if (!dev)
258        goto error;
259
260    priv = dev->phy_ptr;
261    if (!priv)
262        goto error;
263
264    /* don't strip the header if vlan mode is disabled */
265    if (!priv->vlan)
266        goto recv;
267
268    /* strip header, get vlan id */
269    buf = skb->data;
270    skb_pull(skb, 2);
271
272    /* check for vlan header presence */
273    if ((buf[12 + 2] != 0x81) || (buf[13 + 2] != 0x00))
274        goto recv;
275
276    port = buf[0] & 0xf;
277
278    /* no need to fix up packets coming from a tagged source */
279    if (priv->vlan_tagged & (1 << port))
280        goto recv;
281
282    /* lookup port vid from local table, the switch passes an invalid vlan id */
283    vlan = priv->vlan_id[priv->pvid[port]];
284
285    buf[14 + 2] &= 0xf0;
286    buf[14 + 2] |= vlan >> 8;
287    buf[15 + 2] = vlan & 0xff;
288
289recv:
290    skb->protocol = eth_type_trans(skb, skb->dev);
291
292    if (napi)
293        return netif_receive_skb(skb);
294    else
295        return netif_rx(skb);
296
297error:
298    /* no vlan? eat the packet! */
299    dev_kfree_skb_any(skb);
300    return NET_RX_DROP;
301}
302
303static int
304ar8216_netif_rx(struct sk_buff *skb)
305{
306    return ar8216_mangle_rx(skb, 0);
307}
308
309static int
310ar8216_netif_receive_skb(struct sk_buff *skb)
311{
312    return ar8216_mangle_rx(skb, 1);
313}
314
315
316static struct switch_attr ar8216_globals[] = {
317    {
318        .type = SWITCH_TYPE_INT,
319        .name = "enable_vlan",
320        .description = "Enable VLAN mode",
321        .set = ar8216_set_vlan,
322        .get = ar8216_get_vlan,
323        .max = 1
324    },
325};
326
327static struct switch_attr ar8216_port[] = {
328};
329
330static struct switch_attr ar8216_vlan[] = {
331    {
332        .type = SWITCH_TYPE_INT,
333        .name = "pvid",
334        .description = "VLAN ID",
335        .set = ar8216_set_vid,
336        .get = ar8216_get_vid,
337        .max = 4094,
338    },
339};
340
341
342static int
343ar8216_get_ports(struct switch_dev *dev, struct switch_val *val)
344{
345    struct ar8216_priv *priv = to_ar8216(dev);
346    u8 ports = priv->vlan_table[val->port_vlan];
347    int i;
348
349    val->len = 0;
350    for (i = 0; i < AR8216_NUM_PORTS; i++) {
351        struct switch_port *p;
352
353        if (!(ports & (1 << i)))
354            continue;
355
356        p = &val->value.ports[val->len++];
357        p->id = i;
358        if (priv->vlan_tagged & (1 << i))
359            p->flags = (1 << SWITCH_PORT_FLAG_TAGGED);
360        else
361            p->flags = 0;
362    }
363    return 0;
364}
365
366static int
367ar8216_set_ports(struct switch_dev *dev, struct switch_val *val)
368{
369    struct ar8216_priv *priv = to_ar8216(dev);
370    u8 *vt = &priv->vlan_table[val->port_vlan];
371    int i, j;
372
373    *vt = 0;
374    for (i = 0; i < val->len; i++) {
375        struct switch_port *p = &val->value.ports[i];
376
377        if (p->flags & (1 << SWITCH_PORT_FLAG_TAGGED))
378            priv->vlan_tagged |= (1 << p->id);
379        else {
380            priv->vlan_tagged &= ~(1 << p->id);
381            priv->pvid[p->id] = val->port_vlan;
382
383            /* make sure that an untagged port does not
384             * appear in other vlans */
385            for (j = 0; j < AR8X16_MAX_VLANS; j++) {
386                if (j == val->port_vlan)
387                    continue;
388                priv->vlan_table[j] &= ~(1 << p->id);
389            }
390        }
391
392        *vt |= 1 << p->id;
393    }
394    return 0;
395}
396
397static int
398ar8216_wait_bit(struct ar8216_priv *priv, int reg, u32 mask, u32 val)
399{
400    int timeout = 20;
401
402    while ((priv->read(priv, reg) & mask) != val) {
403        if (timeout-- <= 0) {
404            printk(KERN_ERR "ar8216: timeout waiting for operation to complete\n");
405            return 1;
406        }
407    }
408    return 0;
409}
410
411static void
412ar8216_vtu_op(struct ar8216_priv *priv, u32 op, u32 val)
413{
414    if (ar8216_wait_bit(priv, AR8216_REG_VTU, AR8216_VTU_ACTIVE, 0))
415        return;
416    if ((op & AR8216_VTU_OP) == AR8216_VTU_OP_LOAD) {
417        val &= AR8216_VTUDATA_MEMBER;
418        val |= AR8216_VTUDATA_VALID;
419        priv->write(priv, AR8216_REG_VTU_DATA, val);
420    }
421    op |= AR8216_VTU_ACTIVE;
422    priv->write(priv, AR8216_REG_VTU, op);
423}
424
425static int
426ar8216_hw_apply(struct switch_dev *dev)
427{
428    struct ar8216_priv *priv = to_ar8216(dev);
429    u8 portmask[AR8216_NUM_PORTS];
430    int i, j;
431
432    mutex_lock(&priv->reg_mutex);
433    /* flush all vlan translation unit entries */
434    ar8216_vtu_op(priv, AR8216_VTU_OP_FLUSH, 0);
435
436    memset(portmask, 0, sizeof(portmask));
437    if (priv->vlan) {
438        /* calculate the port destination masks and load vlans
439         * into the vlan translation unit */
440        for (j = 0; j < AR8X16_MAX_VLANS; j++) {
441            u8 vp = priv->vlan_table[j];
442
443            if (!vp)
444                continue;
445
446            for (i = 0; i < AR8216_NUM_PORTS; i++) {
447                u8 mask = (1 << i);
448                if (vp & mask)
449                    portmask[i] |= vp & ~mask;
450            }
451
452            ar8216_vtu_op(priv,
453                AR8216_VTU_OP_LOAD |
454                (priv->vlan_id[j] << AR8216_VTU_VID_S),
455                priv->vlan_table[j]);
456        }
457    } else {
458        /* vlan disabled:
459         * isolate all ports, but connect them to the cpu port */
460        for (i = 0; i < AR8216_NUM_PORTS; i++) {
461            if (i == AR8216_PORT_CPU)
462                continue;
463
464            portmask[i] = 1 << AR8216_PORT_CPU;
465            portmask[AR8216_PORT_CPU] |= (1 << i);
466        }
467    }
468
469    /* update the port destination mask registers and tag settings */
470    for (i = 0; i < AR8216_NUM_PORTS; i++) {
471        int egress, ingress;
472        int pvid;
473
474        if (priv->vlan) {
475            pvid = priv->vlan_id[priv->pvid[i]];
476        } else {
477            pvid = i;
478        }
479
480        if (priv->vlan && (priv->vlan_tagged & (1 << i))) {
481            egress = AR8216_OUT_ADD_VLAN;
482        } else {
483            egress = AR8216_OUT_STRIP_VLAN;
484        }
485        if (priv->vlan) {
486            ingress = AR8216_IN_SECURE;
487        } else {
488            ingress = AR8216_IN_PORT_ONLY;
489        }
490
491        ar8216_rmw(priv, AR8216_REG_PORT_CTRL(i),
492            AR8216_PORT_CTRL_LEARN | AR8216_PORT_CTRL_VLAN_MODE |
493            AR8216_PORT_CTRL_SINGLE_VLAN | AR8216_PORT_CTRL_STATE |
494            AR8216_PORT_CTRL_HEADER | AR8216_PORT_CTRL_LEARN_LOCK,
495            AR8216_PORT_CTRL_LEARN |
496              (priv->vlan && i == AR8216_PORT_CPU && (priv->chip == AR8216) ?
497               AR8216_PORT_CTRL_HEADER : 0) |
498              (egress << AR8216_PORT_CTRL_VLAN_MODE_S) |
499              (AR8216_PORT_STATE_FORWARD << AR8216_PORT_CTRL_STATE_S));
500
501        ar8216_rmw(priv, AR8216_REG_PORT_VLAN(i),
502            AR8216_PORT_VLAN_DEST_PORTS | AR8216_PORT_VLAN_MODE |
503              AR8216_PORT_VLAN_DEFAULT_ID,
504            (portmask[i] << AR8216_PORT_VLAN_DEST_PORTS_S) |
505              (ingress << AR8216_PORT_VLAN_MODE_S) |
506              (pvid << AR8216_PORT_VLAN_DEFAULT_ID_S));
507    }
508    mutex_unlock(&priv->reg_mutex);
509    return 0;
510}
511
512static int
513ar8316_hw_init(struct ar8216_priv *priv) {
514    static int initialized;
515    int i;
516    u32 val;
517    struct mii_bus *bus;
518
519    if (initialized)
520        return 0;
521
522    val = priv->read(priv, 0x8);
523
524    if (priv->phy->interface == PHY_INTERFACE_MODE_RGMII) {
525        /* value taken from Ubiquiti RouterStation Pro */
526        if (val == 0x81461bea) {
527            /* switch already intialized by bootloader */
528            initialized = true;
529            return 0;
530        }
531        priv->write(priv, 0x8, 0x81461bea);
532    } else if (priv->phy->interface == PHY_INTERFACE_MODE_GMII) {
533        /* value taken from AVM Fritz!Box 7390 sources */
534        if (val == 0x010e5b71) {
535            /* switch already initialized by bootloader */
536            initialized = true;
537            return 0;
538        }
539        priv->write(priv, 0x8, 0x010e5b71);
540    } else {
541        /* no known value for phy interface */
542        printk(KERN_ERR "ar8316: unsupported mii mode: %d.\n",
543            priv->phy->interface);
544        return -EINVAL;
545    }
546
547    /* standard atheros magic */
548    priv->write(priv, 0x38, 0xc000050e);
549
550    /* Initialize the ports */
551    bus = priv->phy->bus;
552    for (i = 0; i < 5; i++) {
553        if ((i == 4) &&
554            priv->phy->interface == PHY_INTERFACE_MODE_RGMII) {
555            /* work around for phy4 rgmii mode */
556            bus->write(bus, i, MII_ATH_DBG_ADDR, 0x12);
557            bus->write(bus, i, MII_ATH_DBG_DATA, 0x480c);
558            /* rx delay */
559            bus->write(bus, i, MII_ATH_DBG_ADDR, 0x0);
560            bus->write(bus, i, MII_ATH_DBG_DATA, 0x824e);
561            /* tx delay */
562            bus->write(bus, i, MII_ATH_DBG_ADDR, 0x5);
563            bus->write(bus, i, MII_ATH_DBG_DATA, 0x3d47);
564            msleep(1000);
565        }
566
567        /* initialize the port itself */
568        bus->write(bus, i, MII_ADVERTISE,
569            ADVERTISE_ALL | ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
570        bus->write(bus, i, MII_CTRL1000, ADVERTISE_1000FULL);
571        bus->write(bus, i, MII_BMCR, BMCR_RESET | BMCR_ANENABLE);
572        msleep(1000);
573    }
574    initialized = true;
575    return 0;
576}
577
578static int
579ar8216_reset_switch(struct switch_dev *dev)
580{
581    struct ar8216_priv *priv = to_ar8216(dev);
582    int i;
583
584    mutex_lock(&priv->reg_mutex);
585    memset(&priv->vlan, 0, sizeof(struct ar8216_priv) -
586        offsetof(struct ar8216_priv, vlan));
587    for (i = 0; i < AR8X16_MAX_VLANS; i++) {
588        priv->vlan_id[i] = i;
589    }
590    for (i = 0; i < AR8216_NUM_PORTS; i++) {
591        /* Enable port learning and tx */
592        priv->write(priv, AR8216_REG_PORT_CTRL(i),
593            AR8216_PORT_CTRL_LEARN |
594            (4 << AR8216_PORT_CTRL_STATE_S));
595
596        priv->write(priv, AR8216_REG_PORT_VLAN(i), 0);
597
598        /* Configure all PHYs */
599        if (i == AR8216_PORT_CPU) {
600            priv->write(priv, AR8216_REG_PORT_STATUS(i),
601                AR8216_PORT_STATUS_LINK_UP |
602                ((priv->chip == AR8316) ?
603                    AR8216_PORT_SPEED_1000M : AR8216_PORT_SPEED_100M) |
604                AR8216_PORT_STATUS_TXMAC |
605                AR8216_PORT_STATUS_RXMAC |
606                ((priv->chip == AR8316) ? AR8216_PORT_STATUS_RXFLOW : 0) |
607                ((priv->chip == AR8316) ? AR8216_PORT_STATUS_TXFLOW : 0) |
608                AR8216_PORT_STATUS_DUPLEX);
609        } else {
610            priv->write(priv, AR8216_REG_PORT_STATUS(i),
611                AR8216_PORT_STATUS_LINK_AUTO);
612        }
613    }
614    /* XXX: undocumented magic from atheros, required! */
615    priv->write(priv, 0x38, 0xc000050e);
616
617    if (priv->chip == AR8216) {
618        ar8216_rmw(priv, AR8216_REG_GLOBAL_CTRL,
619            AR8216_GCTRL_MTU, 1518 + 8 + 2);
620    } else if (priv->chip == AR8316) {
621        /* enable jumbo frames */
622        ar8216_rmw(priv, AR8216_REG_GLOBAL_CTRL,
623            AR8316_GCTRL_MTU, 9018 + 8 + 2);
624    }
625
626    if (priv->chip == AR8316) {
627        /* enable cpu port to receive multicast and broadcast frames */
628        priv->write(priv, AR8216_REG_FLOOD_MASK, 0x003f003f);
629    }
630    mutex_unlock(&priv->reg_mutex);
631    return ar8216_hw_apply(dev);
632}
633
634static int
635ar8216_config_init(struct phy_device *pdev)
636{
637    struct ar8216_priv *priv;
638    struct net_device *dev = pdev->attached_dev;
639    int ret;
640
641    priv = kzalloc(sizeof(struct ar8216_priv), GFP_KERNEL);
642    if (priv == NULL)
643        return -ENOMEM;
644
645    priv->phy = pdev;
646
647    priv->chip = ar8216_id_chip(priv);
648
649    if (pdev->addr == 0)
650        printk(KERN_INFO "%s: AR%d switch driver attached.\n",
651            pdev->attached_dev->name, priv->chip);
652
653
654    if (pdev->addr != 0) {
655        if (priv->chip == AR8316) {
656            pdev->supported |= SUPPORTED_1000baseT_Full;
657            pdev->advertising |= ADVERTISED_1000baseT_Full;
658        }
659        kfree(priv);
660        return 0;
661    }
662
663    pdev->supported = priv->chip == AR8316 ?
664        SUPPORTED_1000baseT_Full : SUPPORTED_100baseT_Full;
665    pdev->advertising = pdev->supported;
666
667    mutex_init(&priv->reg_mutex);
668    priv->read = ar8216_mii_read;
669    priv->write = ar8216_mii_write;
670    memcpy(&priv->dev, &athdev, sizeof(struct switch_dev));
671    pdev->priv = priv;
672
673    if (priv->chip == AR8316) {
674        priv->dev.name = "Atheros AR8316";
675        priv->dev.vlans = AR8X16_MAX_VLANS;
676        /* port 5 connected to the other mac, therefore unusable */
677        priv->dev.ports = (AR8216_NUM_PORTS - 1);
678    }
679
680    if ((ret = register_switch(&priv->dev, pdev->attached_dev)) < 0) {
681        kfree(priv);
682        goto done;
683    }
684
685    if (priv->chip == AR8316) {
686        ret = ar8316_hw_init(priv);
687        if (ret) {
688            kfree(priv);
689            goto done;
690        }
691    }
692
693    ret = ar8216_reset_switch(&priv->dev);
694    if (ret) {
695        kfree(priv);
696        goto done;
697    }
698
699    dev->phy_ptr = priv;
700
701    /* VID fixup only needed on ar8216 */
702    if (pdev->addr == 0 && priv->chip == AR8216) {
703        pdev->pkt_align = 2;
704        pdev->netif_receive_skb = ar8216_netif_receive_skb;
705        pdev->netif_rx = ar8216_netif_rx;
706        priv->ndo_old = dev->netdev_ops;
707        memcpy(&priv->ndo, priv->ndo_old, sizeof(struct net_device_ops));
708        priv->ndo.ndo_start_xmit = ar8216_mangle_tx;
709        dev->netdev_ops = &priv->ndo;
710    }
711
712done:
713    return ret;
714}
715
716static int
717ar8216_read_status(struct phy_device *phydev)
718{
719    struct ar8216_priv *priv = phydev->priv;
720    int ret;
721    if (phydev->addr != 0) {
722        return genphy_read_status(phydev);
723    }
724
725    phydev->speed = priv->chip == AR8316 ? SPEED_1000 : SPEED_100;
726    phydev->duplex = DUPLEX_FULL;
727    phydev->link = 1;
728
729    /* flush the address translation unit */
730    mutex_lock(&priv->reg_mutex);
731    ret = ar8216_wait_bit(priv, AR8216_REG_ATU, AR8216_ATU_ACTIVE, 0);
732
733    if (!ret)
734        priv->write(priv, AR8216_REG_ATU, AR8216_ATU_OP_FLUSH);
735    else
736        ret = -ETIMEDOUT;
737    mutex_unlock(&priv->reg_mutex);
738
739    phydev->state = PHY_RUNNING;
740    netif_carrier_on(phydev->attached_dev);
741    phydev->adjust_link(phydev->attached_dev);
742
743    return ret;
744}
745
746static int
747ar8216_config_aneg(struct phy_device *phydev)
748{
749    if (phydev->addr == 0)
750        return 0;
751
752    return genphy_config_aneg(phydev);
753}
754
755static int
756ar8216_probe(struct phy_device *pdev)
757{
758    struct ar8216_priv priv;
759    u16 chip;
760
761    priv.phy = pdev;
762    chip = ar8216_id_chip(&priv);
763    if (chip == UNKNOWN)
764        return -ENODEV;
765
766    return 0;
767}
768
769static void
770ar8216_remove(struct phy_device *pdev)
771{
772    struct ar8216_priv *priv = pdev->priv;
773    struct net_device *dev = pdev->attached_dev;
774
775    if (!priv)
776        return;
777
778    if (priv->ndo_old && dev)
779        dev->netdev_ops = priv->ndo_old;
780    if (pdev->addr == 0)
781        unregister_switch(&priv->dev);
782    kfree(priv);
783}
784
785/* template */
786static struct switch_dev athdev = {
787    .name = "Atheros AR8216",
788    .cpu_port = AR8216_PORT_CPU,
789    .ports = AR8216_NUM_PORTS,
790    .vlans = AR8216_NUM_VLANS,
791    .attr_global = {
792        .attr = ar8216_globals,
793        .n_attr = ARRAY_SIZE(ar8216_globals),
794    },
795    .attr_port = {
796        .attr = ar8216_port,
797        .n_attr = ARRAY_SIZE(ar8216_port),
798    },
799    .attr_vlan = {
800        .attr = ar8216_vlan,
801        .n_attr = ARRAY_SIZE(ar8216_vlan),
802    },
803    .get_port_pvid = ar8216_get_pvid,
804    .set_port_pvid = ar8216_set_pvid,
805    .get_vlan_ports = ar8216_get_ports,
806    .set_vlan_ports = ar8216_set_ports,
807    .apply_config = ar8216_hw_apply,
808    .reset_switch = ar8216_reset_switch,
809};
810
811static struct phy_driver ar8216_driver = {
812    .phy_id = 0x004d0000,
813    .name = "Atheros AR8216/AR8316",
814    .phy_id_mask = 0xffff0000,
815    .features = PHY_BASIC_FEATURES,
816    .probe = ar8216_probe,
817    .remove = ar8216_remove,
818    .config_init = &ar8216_config_init,
819    .config_aneg = &ar8216_config_aneg,
820    .read_status = &ar8216_read_status,
821    .driver = { .owner = THIS_MODULE },
822};
823
824int __init
825ar8216_init(void)
826{
827    return phy_driver_register(&ar8216_driver);
828}
829
830void __exit
831ar8216_exit(void)
832{
833    phy_driver_unregister(&ar8216_driver);
834}
835
836module_init(ar8216_init);
837module_exit(ar8216_exit);
838MODULE_LICENSE("GPL");
839
840

Archive Download this file



interactive