0a6eed9577a87d823159b1abae41cc5759b488f3
[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
35 struct ar8216_priv {
36 struct switch_dev dev;
37 struct phy_device *phy;
38 u32 (*read)(struct ar8216_priv *priv, int reg);
39 void (*write)(struct ar8216_priv *priv, int reg, u32 val);
40 const struct net_device_ops *ndo_old;
41 struct net_device_ops ndo;
42
43 /* all fields below are cleared on reset */
44 bool vlan;
45 u16 vlan_id[AR8216_NUM_VLANS];
46 u8 vlan_table[AR8216_NUM_VLANS];
47 u8 vlan_tagged;
48 u16 pvid[AR8216_NUM_PORTS];
49 };
50 static struct switch_dev athdev;
51
52 #define to_ar8216(_dev) container_of(_dev, struct ar8216_priv, dev)
53
54 static inline void
55 split_addr(u32 regaddr, u16 *r1, u16 *r2, u16 *page)
56 {
57 regaddr >>= 1;
58 *r1 = regaddr & 0x1e;
59
60 regaddr >>= 5;
61 *r2 = regaddr & 0x7;
62
63 regaddr >>= 3;
64 *page = regaddr & 0x1ff;
65 }
66
67 static u32
68 ar8216_mii_read(struct ar8216_priv *priv, int reg)
69 {
70 struct phy_device *phy = priv->phy;
71 u16 r1, r2, page;
72 u16 lo, hi;
73
74 split_addr((u32) reg, &r1, &r2, &page);
75 phy->bus->write(phy->bus, 0x18, 0, page);
76 msleep(1); /* wait for the page switch to propagate */
77 lo = phy->bus->read(phy->bus, 0x10 | r2, r1);
78 hi = phy->bus->read(phy->bus, 0x10 | r2, r1 + 1);
79
80 return (hi << 16) | lo;
81 }
82
83 static void
84 ar8216_mii_write(struct ar8216_priv *priv, int reg, u32 val)
85 {
86 struct phy_device *phy = priv->phy;
87 u16 r1, r2, r3;
88 u16 lo, hi;
89
90 split_addr((u32) reg, &r1, &r2, &r3);
91 phy->bus->write(phy->bus, 0x18, 0, r3);
92 msleep(1); /* wait for the page switch to propagate */
93
94 lo = val & 0xffff;
95 hi = (u16) (val >> 16);
96 phy->bus->write(phy->bus, 0x10 | r2, r1 + 1, hi);
97 phy->bus->write(phy->bus, 0x10 | r2, r1, lo);
98 }
99
100 static u32
101 ar8216_rmw(struct ar8216_priv *priv, int reg, u32 mask, u32 val)
102 {
103 u32 v;
104
105 v = priv->read(priv, reg);
106 v &= ~mask;
107 v |= val;
108 priv->write(priv, reg, v);
109
110 return v;
111 }
112
113 static int
114 ar8216_set_vlan(struct switch_dev *dev, const struct switch_attr *attr,
115 struct switch_val *val)
116 {
117 struct ar8216_priv *priv = to_ar8216(dev);
118 priv->vlan = !!val->value.i;
119 return 0;
120 }
121
122 static int
123 ar8216_get_vlan(struct switch_dev *dev, const struct switch_attr *attr,
124 struct switch_val *val)
125 {
126 struct ar8216_priv *priv = to_ar8216(dev);
127 val->value.i = priv->vlan;
128 return 0;
129 }
130
131
132 static int
133 ar8216_set_pvid(struct switch_dev *dev, int port, int vlan)
134 {
135 struct ar8216_priv *priv = to_ar8216(dev);
136
137 /* make sure no invalid PVIDs get set */
138
139 if (vlan >= AR8216_NUM_VLANS)
140 return -EINVAL;
141
142 priv->pvid[port] = vlan;
143 return 0;
144 }
145
146 static int
147 ar8216_get_pvid(struct switch_dev *dev, int port, int *vlan)
148 {
149 struct ar8216_priv *priv = to_ar8216(dev);
150 *vlan = priv->pvid[port];
151 return 0;
152 }
153
154 static int
155 ar8216_set_vid(struct switch_dev *dev, const struct switch_attr *attr,
156 struct switch_val *val)
157 {
158 struct ar8216_priv *priv = to_ar8216(dev);
159 priv->vlan_id[val->port_vlan] = val->value.i;
160 return 0;
161 }
162
163 static int
164 ar8216_get_vid(struct switch_dev *dev, const struct switch_attr *attr,
165 struct switch_val *val)
166 {
167 struct ar8216_priv *priv = to_ar8216(dev);
168 val->value.i = priv->vlan_id[val->port_vlan];
169 return 0;
170 }
171
172
173 static int
174 ar8216_mangle_tx(struct sk_buff *skb, struct net_device *dev)
175 {
176 struct ar8216_priv *priv = dev->phy_ptr;
177 unsigned char *buf;
178
179 if (unlikely(!priv))
180 goto error;
181
182 if (!priv->vlan)
183 goto send;
184
185 if (unlikely(skb_headroom(skb) < 2)) {
186 if (pskb_expand_head(skb, 2, 0, GFP_ATOMIC) < 0)
187 goto error;
188 }
189
190 buf = skb_push(skb, 2);
191 buf[0] = 0x10;
192 buf[1] = 0x80;
193
194 send:
195 return priv->ndo_old->ndo_start_xmit(skb, dev);
196
197 error:
198 dev_kfree_skb_any(skb);
199 return 0;
200 }
201
202 static int
203 ar8216_mangle_rx(struct sk_buff *skb, int napi)
204 {
205 struct ar8216_priv *priv;
206 struct net_device *dev;
207 unsigned char *buf;
208 int port, vlan;
209
210 dev = skb->dev;
211 if (!dev)
212 goto error;
213
214 priv = dev->phy_ptr;
215 if (!priv)
216 goto error;
217
218 /* don't strip the header if vlan mode is disabled */
219 if (!priv->vlan)
220 goto recv;
221
222 /* strip header, get vlan id */
223 buf = skb->data;
224 skb_pull(skb, 2);
225
226 /* check for vlan header presence */
227 if ((buf[12 + 2] != 0x81) || (buf[13 + 2] != 0x00))
228 goto recv;
229
230 port = buf[0] & 0xf;
231
232 /* no need to fix up packets coming from a tagged source */
233 if (priv->vlan_tagged & (1 << port))
234 goto recv;
235
236 /* lookup port vid from local table, the switch passes an invalid vlan id */
237 vlan = priv->vlan_id[priv->pvid[port]];
238
239 buf[14 + 2] &= 0xf0;
240 buf[14 + 2] |= vlan >> 8;
241 buf[15 + 2] = vlan & 0xff;
242
243 recv:
244 skb->protocol = eth_type_trans(skb, skb->dev);
245
246 if (napi)
247 return netif_receive_skb(skb);
248 else
249 return netif_rx(skb);
250
251 error:
252 /* no vlan? eat the packet! */
253 dev_kfree_skb_any(skb);
254 return 0;
255 }
256
257 static int
258 ar8216_netif_rx(struct sk_buff *skb)
259 {
260 return ar8216_mangle_rx(skb, 0);
261 }
262
263 static int
264 ar8216_netif_receive_skb(struct sk_buff *skb)
265 {
266 return ar8216_mangle_rx(skb, 1);
267 }
268
269
270 static struct switch_attr ar8216_globals[] = {
271 {
272 .type = SWITCH_TYPE_INT,
273 .name = "enable_vlan",
274 .description = "Enable VLAN mode",
275 .set = ar8216_set_vlan,
276 .get = ar8216_get_vlan,
277 .max = 1
278 },
279 };
280
281 static struct switch_attr ar8216_port[] = {
282 };
283
284 static struct switch_attr ar8216_vlan[] = {
285 {
286 .type = SWITCH_TYPE_INT,
287 .name = "pvid",
288 .description = "VLAN ID",
289 .set = ar8216_set_vid,
290 .get = ar8216_get_vid,
291 .max = 4094,
292 },
293 };
294
295
296 static int
297 ar8216_get_ports(struct switch_dev *dev, struct switch_val *val)
298 {
299 struct ar8216_priv *priv = to_ar8216(dev);
300 u8 ports = priv->vlan_table[val->port_vlan];
301 int i;
302
303 val->len = 0;
304 for (i = 0; i < AR8216_NUM_PORTS; i++) {
305 struct switch_port *p;
306
307 if (!(ports & (1 << i)))
308 continue;
309
310 p = &val->value.ports[val->len++];
311 p->id = i;
312 if (priv->vlan_tagged & (1 << i))
313 p->flags = (1 << SWITCH_PORT_FLAG_TAGGED);
314 else
315 p->flags = 0;
316 }
317 return 0;
318 }
319
320 static int
321 ar8216_set_ports(struct switch_dev *dev, struct switch_val *val)
322 {
323 struct ar8216_priv *priv = to_ar8216(dev);
324 u8 *vt = &priv->vlan_table[val->port_vlan];
325 int i, j;
326
327 *vt = 0;
328 for (i = 0; i < val->len; i++) {
329 struct switch_port *p = &val->value.ports[i];
330
331 if (p->flags & (1 << SWITCH_PORT_FLAG_TAGGED))
332 priv->vlan_tagged |= (1 << p->id);
333 else {
334 priv->vlan_tagged &= ~(1 << p->id);
335 priv->pvid[p->id] = val->port_vlan;
336
337 /* make sure that an untagged port does not
338 * appear in other vlans */
339 for (j = 0; j < AR8216_NUM_VLANS; j++) {
340 if (j == val->port_vlan)
341 continue;
342 priv->vlan_table[j] &= ~(1 << p->id);
343 }
344 }
345
346 *vt |= 1 << p->id;
347 }
348 return 0;
349 }
350
351 static int
352 ar8216_wait_bit(struct ar8216_priv *priv, int reg, u32 mask, u32 val)
353 {
354 int timeout = 20;
355
356 while ((priv->read(priv, reg) & mask) != val) {
357 if (timeout-- <= 0) {
358 printk(KERN_ERR "ar8216: timeout waiting for operation to complete\n");
359 return 1;
360 }
361 }
362 return 0;
363 }
364
365 static void
366 ar8216_vtu_op(struct ar8216_priv *priv, u32 op, u32 val)
367 {
368 if (ar8216_wait_bit(priv, AR8216_REG_VTU, AR8216_VTU_ACTIVE, 0))
369 return;
370 if ((op & AR8216_VTU_OP) == AR8216_VTU_OP_LOAD) {
371 val &= AR8216_VTUDATA_MEMBER;
372 val |= AR8216_VTUDATA_VALID;
373 priv->write(priv, AR8216_REG_VTU_DATA, val);
374 }
375 op |= AR8216_VTU_ACTIVE;
376 priv->write(priv, AR8216_REG_VTU, op);
377 }
378
379 static int
380 ar8216_hw_apply(struct switch_dev *dev)
381 {
382 struct ar8216_priv *priv = to_ar8216(dev);
383 u8 portmask[AR8216_NUM_PORTS];
384 int i, j;
385
386 /* flush all vlan translation unit entries */
387 ar8216_vtu_op(priv, AR8216_VTU_OP_FLUSH, 0);
388
389 memset(portmask, 0, sizeof(portmask));
390 if (priv->vlan) {
391 /* calculate the port destination masks and load vlans
392 * into the vlan translation unit */
393 for (j = 0; j < AR8216_NUM_VLANS; j++) {
394 u8 vp = priv->vlan_table[j];
395
396 if (!vp)
397 continue;
398
399 for (i = 0; i < AR8216_NUM_PORTS; i++) {
400 u8 mask = (1 << i);
401 if (vp & mask)
402 portmask[i] |= vp & ~mask;
403 }
404
405 ar8216_vtu_op(priv,
406 AR8216_VTU_OP_LOAD |
407 (priv->vlan_id[j] << AR8216_VTU_VID_S),
408 priv->vlan_table[j]);
409 }
410 } else {
411 /* vlan disabled:
412 * isolate all ports, but connect them to the cpu port */
413 for (i = 0; i < AR8216_NUM_PORTS; i++) {
414 if (i == AR8216_PORT_CPU)
415 continue;
416
417 portmask[i] = 1 << AR8216_PORT_CPU;
418 portmask[AR8216_PORT_CPU] |= (1 << i);
419 }
420 }
421
422 /* update the port destination mask registers and tag settings */
423 for (i = 0; i < AR8216_NUM_PORTS; i++) {
424 int egress, ingress;
425 int pvid;
426
427 if (priv->vlan) {
428 pvid = priv->vlan_id[priv->pvid[i]];
429 } else {
430 pvid = i;
431 }
432
433 if (priv->vlan && (priv->vlan_tagged & (1 << i))) {
434 egress = AR8216_OUT_ADD_VLAN;
435 } else {
436 egress = AR8216_OUT_STRIP_VLAN;
437 }
438 if (priv->vlan) {
439 ingress = AR8216_IN_SECURE;
440 } else {
441 ingress = AR8216_IN_PORT_ONLY;
442 }
443
444 ar8216_rmw(priv, AR8216_REG_PORT_CTRL(i),
445 AR8216_PORT_CTRL_LEARN | AR8216_PORT_CTRL_VLAN_MODE |
446 AR8216_PORT_CTRL_SINGLE_VLAN | AR8216_PORT_CTRL_STATE |
447 AR8216_PORT_CTRL_HEADER | AR8216_PORT_CTRL_LEARN_LOCK,
448 AR8216_PORT_CTRL_LEARN |
449 (priv->vlan && i == AR8216_PORT_CPU ?
450 AR8216_PORT_CTRL_HEADER : 0) |
451 (egress << AR8216_PORT_CTRL_VLAN_MODE_S) |
452 (AR8216_PORT_STATE_FORWARD << AR8216_PORT_CTRL_STATE_S));
453
454 ar8216_rmw(priv, AR8216_REG_PORT_VLAN(i),
455 AR8216_PORT_VLAN_DEST_PORTS | AR8216_PORT_VLAN_MODE |
456 AR8216_PORT_VLAN_DEFAULT_ID,
457 (portmask[i] << AR8216_PORT_VLAN_DEST_PORTS_S) |
458 (ingress << AR8216_PORT_VLAN_MODE_S) |
459 (pvid << AR8216_PORT_VLAN_DEFAULT_ID_S));
460 }
461
462 return 0;
463 }
464
465 static int
466 ar8216_reset_switch(struct switch_dev *dev)
467 {
468 struct ar8216_priv *priv = to_ar8216(dev);
469 int i;
470
471 memset(&priv->vlan, 0, sizeof(struct ar8216_priv) -
472 offsetof(struct ar8216_priv, vlan));
473 for (i = 0; i < AR8216_NUM_VLANS; i++) {
474 priv->vlan_id[i] = i;
475 }
476 for (i = 0; i < AR8216_NUM_PORTS; i++) {
477 /* Enable port learning and tx */
478 priv->write(priv, AR8216_REG_PORT_CTRL(i),
479 AR8216_PORT_CTRL_LEARN |
480 (4 << AR8216_PORT_CTRL_STATE_S));
481
482 priv->write(priv, AR8216_REG_PORT_VLAN(i), 0);
483
484 /* Configure all PHYs */
485 if (i == AR8216_PORT_CPU) {
486 priv->write(priv, AR8216_REG_PORT_STATUS(i),
487 AR8216_PORT_STATUS_LINK_UP |
488 AR8216_PORT_SPEED_100M |
489 AR8216_PORT_STATUS_TXMAC |
490 AR8216_PORT_STATUS_RXMAC |
491 AR8216_PORT_STATUS_DUPLEX);
492 } else {
493 priv->write(priv, AR8216_REG_PORT_STATUS(i),
494 AR8216_PORT_STATUS_LINK_AUTO);
495 }
496 }
497 /* XXX: undocumented magic from atheros, required! */
498 priv->write(priv, 0x38, 0xc000050e);
499
500 ar8216_rmw(priv, AR8216_REG_GLOBAL_CTRL,
501 AR8216_GCTRL_MTU, 1518 + 8 + 2);
502
503 return ar8216_hw_apply(dev);
504 }
505
506 static int
507 ar8216_config_init(struct phy_device *pdev)
508 {
509 struct ar8216_priv *priv;
510 struct net_device *dev = pdev->attached_dev;
511 int ret;
512
513 printk("%s: AR8216 PHY driver attached.\n", pdev->attached_dev->name);
514 pdev->supported = ADVERTISED_100baseT_Full;
515 pdev->advertising = ADVERTISED_100baseT_Full;
516
517 priv = kzalloc(sizeof(struct ar8216_priv), GFP_KERNEL);
518 if (priv == NULL)
519 return -ENOMEM;
520
521 priv->phy = pdev;
522 priv->read = ar8216_mii_read;
523 priv->write = ar8216_mii_write;
524 memcpy(&priv->dev, &athdev, sizeof(struct switch_dev));
525 pdev->priv = priv;
526 if ((ret = register_switch(&priv->dev, pdev->attached_dev)) < 0) {
527 kfree(priv);
528 goto done;
529 }
530
531 ret = ar8216_reset_switch(&priv->dev);
532 if (ret)
533 goto done;
534
535 dev->phy_ptr = priv;
536 pdev->pkt_align = 2;
537 pdev->netif_receive_skb = ar8216_netif_receive_skb;
538 pdev->netif_rx = ar8216_netif_rx;
539
540 priv->ndo_old = dev->netdev_ops;
541 memcpy(&priv->ndo, priv->ndo_old, sizeof(struct net_device_ops));
542 priv->ndo.ndo_start_xmit = ar8216_mangle_tx;
543 dev->netdev_ops = &priv->ndo;
544
545 done:
546 return ret;
547 }
548
549 static int
550 ar8216_read_status(struct phy_device *phydev)
551 {
552 struct ar8216_priv *priv = phydev->priv;
553
554 phydev->speed = SPEED_100;
555 phydev->duplex = DUPLEX_FULL;
556 phydev->link = 1;
557
558 /* flush the address translation unit */
559 if (ar8216_wait_bit(priv, AR8216_REG_ATU, AR8216_ATU_ACTIVE, 0))
560 return -ETIMEDOUT;
561
562 priv->write(priv, AR8216_REG_ATU, AR8216_ATU_OP_FLUSH);
563
564 phydev->state = PHY_RUNNING;
565 netif_carrier_on(phydev->attached_dev);
566 phydev->adjust_link(phydev->attached_dev);
567
568 return 0;
569 }
570
571 static int
572 ar8216_config_aneg(struct phy_device *phydev)
573 {
574 return 0;
575 }
576
577 static int
578 ar8216_probe(struct phy_device *pdev)
579 {
580 struct ar8216_priv priv;
581
582 u8 id, rev;
583 u32 val;
584
585 priv.phy = pdev;
586 val = ar8216_mii_read(&priv, AR8216_REG_CTRL);
587 rev = val & AR8216_CTRL_REVISION;
588 id = (val & AR8216_CTRL_VERSION) >> AR8216_CTRL_VERSION_S;
589 if ((id != 1) || (rev != 1))
590 return -ENODEV;
591
592 return 0;
593 }
594
595 static void
596 ar8216_remove(struct phy_device *pdev)
597 {
598 struct ar8216_priv *priv = pdev->priv;
599 struct net_device *dev = pdev->attached_dev;
600
601 if (!priv)
602 return;
603
604 if (priv->ndo_old && dev)
605 dev->netdev_ops = priv->ndo_old;
606 unregister_switch(&priv->dev);
607 kfree(priv);
608 }
609
610 /* template */
611 static struct switch_dev athdev = {
612 .name = "Atheros AR8216",
613 .cpu_port = AR8216_PORT_CPU,
614 .ports = AR8216_NUM_PORTS,
615 .vlans = AR8216_NUM_VLANS,
616 .attr_global = {
617 .attr = ar8216_globals,
618 .n_attr = ARRAY_SIZE(ar8216_globals),
619 },
620 .attr_port = {
621 .attr = ar8216_port,
622 .n_attr = ARRAY_SIZE(ar8216_port),
623 },
624 .attr_vlan = {
625 .attr = ar8216_vlan,
626 .n_attr = ARRAY_SIZE(ar8216_vlan),
627 },
628 .get_port_pvid = ar8216_get_pvid,
629 .set_port_pvid = ar8216_set_pvid,
630 .get_vlan_ports = ar8216_get_ports,
631 .set_vlan_ports = ar8216_set_ports,
632 .apply_config = ar8216_hw_apply,
633 .reset_switch = ar8216_reset_switch,
634 };
635
636 static struct phy_driver ar8216_driver = {
637 .name = "Atheros AR8216",
638 .features = PHY_BASIC_FEATURES,
639 .probe = ar8216_probe,
640 .remove = ar8216_remove,
641 .config_init = &ar8216_config_init,
642 .config_aneg = &ar8216_config_aneg,
643 .read_status = &ar8216_read_status,
644 .driver = { .owner = THIS_MODULE },
645 };
646
647 int __init
648 ar8216_init(void)
649 {
650 return phy_driver_register(&ar8216_driver);
651 }
652
653 void __exit
654 ar8216_exit(void)
655 {
656 phy_driver_unregister(&ar8216_driver);
657 }
658
659 module_init(ar8216_init);
660 module_exit(ar8216_exit);
661 MODULE_LICENSE("GPL");
662
This page took 0.088562 seconds and 3 git commands to generate.