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