fix link status detection in various switch drivers
[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         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->ndo_old->ndo_start_xmit(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 = "enable_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                           (priv->vlan && i == AR8216_PORT_CPU ?
443                            AR8216_PORT_CTRL_HEADER : 0) |
444                           (egress << AR8216_PORT_CTRL_VLAN_MODE_S) |
445                           (AR8216_PORT_STATE_FORWARD << AR8216_PORT_CTRL_STATE_S));
446
447                 ar8216_rmw(priv, AR8216_REG_PORT_VLAN(i),
448                         AR8216_PORT_VLAN_DEST_PORTS | AR8216_PORT_VLAN_MODE |
449                           AR8216_PORT_VLAN_DEFAULT_ID,
450                         (portmask[i] << AR8216_PORT_VLAN_DEST_PORTS_S) |
451                           (ingress << AR8216_PORT_VLAN_MODE_S) |
452                           (pvid << AR8216_PORT_VLAN_DEFAULT_ID_S));
453         }
454
455         return 0;
456 }
457
458 static int
459 ar8216_reset_switch(struct switch_dev *dev)
460 {
461         struct ar8216_priv *priv = to_ar8216(dev);
462         int i;
463
464         memset(&priv->vlan, 0, sizeof(struct ar8216_priv) -
465                 offsetof(struct ar8216_priv, vlan));
466         for (i = 0; i < AR8216_NUM_VLANS; i++) {
467                 priv->vlan_id[i] = i;
468         }
469         for (i = 0; i < AR8216_NUM_PORTS; i++) {
470                 /* Enable port learning and tx */
471                 priv->write(priv, AR8216_REG_PORT_CTRL(i),
472                         AR8216_PORT_CTRL_LEARN |
473                         (4 << AR8216_PORT_CTRL_STATE_S));
474
475                 priv->write(priv, AR8216_REG_PORT_VLAN(i), 0);
476
477                 /* Configure all PHYs */
478                 if (i == AR8216_PORT_CPU) {
479                         priv->write(priv, AR8216_REG_PORT_STATUS(i),
480                                 AR8216_PORT_STATUS_LINK_UP |
481                                 AR8216_PORT_STATUS_SPEED |
482                                 AR8216_PORT_STATUS_TXMAC |
483                                 AR8216_PORT_STATUS_RXMAC |
484                                 AR8216_PORT_STATUS_DUPLEX);
485                 } else {
486                         priv->write(priv, AR8216_REG_PORT_STATUS(i),
487                                 AR8216_PORT_STATUS_LINK_AUTO);
488                 }
489         }
490         /* XXX: undocumented magic from atheros, required! */
491         priv->write(priv, 0x38, 0xc000050e);
492
493         ar8216_rmw(priv, AR8216_REG_GLOBAL_CTRL,
494                 AR8216_GCTRL_MTU, 1518 + 8 + 2);
495
496         return ar8216_hw_apply(dev);
497 }
498
499 static int
500 ar8216_config_init(struct phy_device *pdev)
501 {
502         struct ar8216_priv *priv;
503         struct net_device *dev = pdev->attached_dev;
504         int ret;
505
506         printk("%s: AR8216 PHY driver attached.\n", pdev->attached_dev->name);
507         pdev->supported = ADVERTISED_100baseT_Full;
508         pdev->advertising = ADVERTISED_100baseT_Full;
509
510         priv = kzalloc(sizeof(struct ar8216_priv), GFP_KERNEL);
511         if (priv == NULL)
512                 return -ENOMEM;
513
514         priv->phy = pdev;
515         priv->read = ar8216_mii_read;
516         priv->write = ar8216_mii_write;
517         memcpy(&priv->dev, &athdev, sizeof(struct switch_dev));
518         pdev->priv = priv;
519         if ((ret = register_switch(&priv->dev, pdev->attached_dev)) < 0) {
520                 kfree(priv);
521                 goto done;
522         }
523
524         ret = ar8216_reset_switch(&priv->dev);
525         if (ret)
526                 goto done;
527
528         dev->phy_ptr = priv;
529         pdev->pkt_align = 2;
530         pdev->netif_receive_skb = ar8216_netif_receive_skb;
531         pdev->netif_rx = ar8216_netif_rx;
532
533         priv->ndo_old = dev->netdev_ops;
534         memcpy(&priv->ndo, priv->ndo_old, sizeof(struct net_device_ops));
535         priv->ndo.ndo_start_xmit = ar8216_mangle_tx;
536         dev->netdev_ops = &priv->ndo;
537
538 done:
539         return ret;
540 }
541
542 static int
543 ar8216_read_status(struct phy_device *phydev)
544 {
545         struct ar8216_priv *priv = phydev->priv;
546
547         phydev->speed = SPEED_100;
548         phydev->duplex = DUPLEX_FULL;
549         phydev->link = 1;
550
551         /* flush the address translation unit */
552         if (ar8216_wait_bit(priv, AR8216_REG_ATU, AR8216_ATU_ACTIVE, 0))
553                 return -ETIMEDOUT;
554
555         priv->write(priv, AR8216_REG_ATU, AR8216_ATU_OP_FLUSH);
556
557         return 0;
558 }
559
560 static int
561 ar8216_config_aneg(struct phy_device *phydev)
562 {
563         return 0;
564 }
565
566 static int
567 ar8216_probe(struct phy_device *pdev)
568 {
569         struct ar8216_priv priv;
570
571         u8 id, rev;
572         u32 val;
573
574         priv.phy = pdev;
575         val = ar8216_mii_read(&priv, AR8216_REG_CTRL);
576         rev = val & 0xff;
577         id = (val >> 8) & 0xff;
578         if ((id != 1) || (rev != 1))
579                 return -ENODEV;
580
581         return 0;
582 }
583
584 static void
585 ar8216_remove(struct phy_device *pdev)
586 {
587         struct ar8216_priv *priv = pdev->priv;
588         struct net_device *dev = pdev->attached_dev;
589
590         if (!priv)
591                 return;
592
593         if (priv->ndo_old && dev)
594                 dev->netdev_ops = priv->ndo_old;
595         unregister_switch(&priv->dev);
596         kfree(priv);
597 }
598
599 /* template */
600 static struct switch_dev athdev = {
601         .name = "Atheros AR8216",
602         .cpu_port = AR8216_PORT_CPU,
603         .ports = AR8216_NUM_PORTS,
604         .vlans = AR8216_NUM_VLANS,
605         .attr_global = {
606                 .attr = ar8216_globals,
607                 .n_attr = ARRAY_SIZE(ar8216_globals),
608         },
609         .attr_port = {
610                 .attr = ar8216_port,
611                 .n_attr = ARRAY_SIZE(ar8216_port),
612         },
613         .attr_vlan = {
614                 .attr = ar8216_vlan,
615                 .n_attr = ARRAY_SIZE(ar8216_vlan),
616         },
617         .get_port_pvid = ar8216_get_pvid,
618         .set_port_pvid = ar8216_set_pvid,
619         .get_vlan_ports = ar8216_get_ports,
620         .set_vlan_ports = ar8216_set_ports,
621         .apply_config = ar8216_hw_apply,
622         .reset_switch = ar8216_reset_switch,
623 };
624
625 static struct phy_driver ar8216_driver = {
626         .name           = "Atheros AR8216",
627         .features       = PHY_BASIC_FEATURES,
628         .probe          = ar8216_probe,
629         .remove         = ar8216_remove,
630         .config_init    = &ar8216_config_init,
631         .config_aneg    = &ar8216_config_aneg,
632         .read_status    = &ar8216_read_status,
633         .driver         = { .owner = THIS_MODULE },
634 };
635
636 int __init
637 ar8216_init(void)
638 {
639         return phy_driver_register(&ar8216_driver);
640 }
641
642 void __exit
643 ar8216_exit(void)
644 {
645         phy_driver_unregister(&ar8216_driver);
646 }
647
648 module_init(ar8216_init);
649 module_exit(ar8216_exit);
650 MODULE_LICENSE("GPL");
651