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

Archive Download this file



interactive