ar8216: support AR8316 rev 0 devices
[openwrt/svn-archive/archive.git] / 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
38 struct 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 };
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 int i;
123
124 val = ar8216_mii_read(priv, AR8216_REG_CTRL);
125 if (val == ~0)
126 return UNKNOWN;
127
128 id = val & (AR8216_CTRL_REVISION | AR8216_CTRL_VERSION);
129 for (i = 0; i < AR8X16_PROBE_RETRIES; i++) {
130 u16 t;
131
132 val = ar8216_mii_read(priv, AR8216_REG_CTRL);
133 if (val == ~0)
134 return UNKNOWN;
135
136 t = val & (AR8216_CTRL_REVISION | AR8216_CTRL_VERSION);
137 if (t != id)
138 return UNKNOWN;
139 }
140
141 switch (id) {
142 case 0x0101:
143 return AR8216;
144 case 0x1000:
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
159 static int
160 ar8216_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
168 static int
169 ar8216_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
178 static int
179 ar8216_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
192 static int
193 ar8216_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
200 static int
201 ar8216_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
209 static int
210 ar8216_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
219 static int
220 ar8216_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
240 send:
241 return priv->ndo_old->ndo_start_xmit(skb, dev);
242
243 error:
244 dev_kfree_skb_any(skb);
245 return 0;
246 }
247
248 static int
249 ar8216_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
289 recv:
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
297 error:
298 /* no vlan? eat the packet! */
299 dev_kfree_skb_any(skb);
300 return NET_RX_DROP;
301 }
302
303 static int
304 ar8216_netif_rx(struct sk_buff *skb)
305 {
306 return ar8216_mangle_rx(skb, 0);
307 }
308
309 static int
310 ar8216_netif_receive_skb(struct sk_buff *skb)
311 {
312 return ar8216_mangle_rx(skb, 1);
313 }
314
315
316 static 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
327 static struct switch_attr ar8216_port[] = {
328 };
329
330 static struct switch_attr ar8216_vlan[] = {
331 {
332 .type = SWITCH_TYPE_INT,
333 .name = "vid",
334 .description = "VLAN ID (0-4094)",
335 .set = ar8216_set_vid,
336 .get = ar8216_get_vid,
337 .max = 4094,
338 },
339 };
340
341
342 static int
343 ar8216_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
366 static int
367 ar8216_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
397 static int
398 ar8216_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
411 static void
412 ar8216_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
425 static int
426 ar8216_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
512 static int
513 ar8316_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
578 static int
579 ar8216_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
634
635 static const struct switch_dev_ops ar8216_ops = {
636 .attr_global = {
637 .attr = ar8216_globals,
638 .n_attr = ARRAY_SIZE(ar8216_globals),
639 },
640 .attr_port = {
641 .attr = ar8216_port,
642 .n_attr = ARRAY_SIZE(ar8216_port),
643 },
644 .attr_vlan = {
645 .attr = ar8216_vlan,
646 .n_attr = ARRAY_SIZE(ar8216_vlan),
647 },
648 .get_port_pvid = ar8216_get_pvid,
649 .set_port_pvid = ar8216_set_pvid,
650 .get_vlan_ports = ar8216_get_ports,
651 .set_vlan_ports = ar8216_set_ports,
652 .apply_config = ar8216_hw_apply,
653 .reset_switch = ar8216_reset_switch,
654 };
655
656 static int
657 ar8216_config_init(struct phy_device *pdev)
658 {
659 struct ar8216_priv *priv;
660 struct net_device *dev = pdev->attached_dev;
661 struct switch_dev *swdev;
662 int ret;
663
664 priv = kzalloc(sizeof(struct ar8216_priv), GFP_KERNEL);
665 if (priv == NULL)
666 return -ENOMEM;
667
668 priv->phy = pdev;
669
670 priv->chip = ar8216_id_chip(priv);
671
672 if (pdev->addr == 0)
673 printk(KERN_INFO "%s: AR%d switch driver attached.\n",
674 pdev->attached_dev->name, priv->chip);
675
676
677 if (pdev->addr != 0) {
678 if (priv->chip == AR8316) {
679 pdev->supported |= SUPPORTED_1000baseT_Full;
680 pdev->advertising |= ADVERTISED_1000baseT_Full;
681 }
682 kfree(priv);
683 return 0;
684 }
685
686 pdev->supported = priv->chip == AR8316 ?
687 SUPPORTED_1000baseT_Full : SUPPORTED_100baseT_Full;
688 pdev->advertising = pdev->supported;
689
690 mutex_init(&priv->reg_mutex);
691 priv->read = ar8216_mii_read;
692 priv->write = ar8216_mii_write;
693
694 pdev->priv = priv;
695
696 swdev = &priv->dev;
697 swdev->cpu_port = AR8216_PORT_CPU;
698 swdev->ops = &ar8216_ops;
699
700 if (priv->chip == AR8316) {
701 swdev->name = "Atheros AR8316";
702 swdev->vlans = AR8X16_MAX_VLANS;
703 /* port 5 connected to the other mac, therefore unusable */
704 swdev->ports = (AR8216_NUM_PORTS - 1);
705 } else {
706 swdev->name = "Atheros AR8216";
707 swdev->vlans = AR8216_NUM_VLANS;
708 swdev->ports = AR8216_NUM_PORTS;
709 }
710
711 if ((ret = register_switch(&priv->dev, pdev->attached_dev)) < 0) {
712 kfree(priv);
713 goto done;
714 }
715
716 if (priv->chip == AR8316) {
717 ret = ar8316_hw_init(priv);
718 if (ret) {
719 kfree(priv);
720 goto done;
721 }
722 }
723
724 ret = ar8216_reset_switch(&priv->dev);
725 if (ret) {
726 kfree(priv);
727 goto done;
728 }
729
730 dev->phy_ptr = priv;
731
732 /* VID fixup only needed on ar8216 */
733 if (pdev->addr == 0 && priv->chip == AR8216) {
734 pdev->pkt_align = 2;
735 pdev->netif_receive_skb = ar8216_netif_receive_skb;
736 pdev->netif_rx = ar8216_netif_rx;
737 priv->ndo_old = dev->netdev_ops;
738 memcpy(&priv->ndo, priv->ndo_old, sizeof(struct net_device_ops));
739 priv->ndo.ndo_start_xmit = ar8216_mangle_tx;
740 dev->netdev_ops = &priv->ndo;
741 }
742
743 done:
744 return ret;
745 }
746
747 static int
748 ar8216_read_status(struct phy_device *phydev)
749 {
750 struct ar8216_priv *priv = phydev->priv;
751 int ret;
752 if (phydev->addr != 0) {
753 return genphy_read_status(phydev);
754 }
755
756 phydev->speed = priv->chip == AR8316 ? SPEED_1000 : SPEED_100;
757 phydev->duplex = DUPLEX_FULL;
758 phydev->link = 1;
759
760 /* flush the address translation unit */
761 mutex_lock(&priv->reg_mutex);
762 ret = ar8216_wait_bit(priv, AR8216_REG_ATU, AR8216_ATU_ACTIVE, 0);
763
764 if (!ret)
765 priv->write(priv, AR8216_REG_ATU, AR8216_ATU_OP_FLUSH);
766 else
767 ret = -ETIMEDOUT;
768 mutex_unlock(&priv->reg_mutex);
769
770 phydev->state = PHY_RUNNING;
771 netif_carrier_on(phydev->attached_dev);
772 phydev->adjust_link(phydev->attached_dev);
773
774 return ret;
775 }
776
777 static int
778 ar8216_config_aneg(struct phy_device *phydev)
779 {
780 if (phydev->addr == 0)
781 return 0;
782
783 return genphy_config_aneg(phydev);
784 }
785
786 static int
787 ar8216_probe(struct phy_device *pdev)
788 {
789 struct ar8216_priv priv;
790 u16 chip;
791
792 priv.phy = pdev;
793 chip = ar8216_id_chip(&priv);
794 if (chip == UNKNOWN)
795 return -ENODEV;
796
797 return 0;
798 }
799
800 static void
801 ar8216_remove(struct phy_device *pdev)
802 {
803 struct ar8216_priv *priv = pdev->priv;
804 struct net_device *dev = pdev->attached_dev;
805
806 if (!priv)
807 return;
808
809 if (priv->ndo_old && dev)
810 dev->netdev_ops = priv->ndo_old;
811 if (pdev->addr == 0)
812 unregister_switch(&priv->dev);
813 kfree(priv);
814 }
815
816 static struct phy_driver ar8216_driver = {
817 .phy_id = 0x004d0000,
818 .name = "Atheros AR8216/AR8316",
819 .phy_id_mask = 0xffff0000,
820 .features = PHY_BASIC_FEATURES,
821 .probe = ar8216_probe,
822 .remove = ar8216_remove,
823 .config_init = &ar8216_config_init,
824 .config_aneg = &ar8216_config_aneg,
825 .read_status = &ar8216_read_status,
826 .driver = { .owner = THIS_MODULE },
827 };
828
829 int __init
830 ar8216_init(void)
831 {
832 return phy_driver_register(&ar8216_driver);
833 }
834
835 void __exit
836 ar8216_exit(void)
837 {
838 phy_driver_unregister(&ar8216_driver);
839 }
840
841 module_init(ar8216_init);
842 module_exit(ar8216_exit);
843 MODULE_LICENSE("GPL");
844