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