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