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