root/src/linux/ar531x/linux-2.6.23/drivers/net/phy/ar8216.c

Revision 12403, 14.2 kB (checked in by BrainSlayer, 5 months ago)

fixes mtu size for ar8216 switch (thx nbd and myself)

Line 
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         mdelay(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         mdelay(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
185         skb_push(skb, 2);
186         skb->data[0] = 0x10;
187         skb->data[1] = 0x80;
188
189 send:
190         return priv->hardstart(skb, dev);
191
192 error:
193         dev_kfree_skb_any(skb);
194         return 0;
195 }
196
197 static int
198 ar8216_mangle_rx(struct sk_buff *skb, int napi)
199 {
200         struct ar8216_priv *priv;
201         struct net_device *dev;
202         unsigned char *buf;
203         int port, vlan;
204
205         dev = skb->dev;
206         if (!dev)
207                 goto error;
208
209         priv = dev->phy_ptr;
210         if (!priv)
211                 goto error;
212
213         /* don't strip the header if vlan mode is disabled */
214         if (!priv->vlan)
215                 goto recv;
216
217         /* strip header, get vlan id */
218         buf = skb->data;
219         skb_pull(skb, 2);
220
221         /* check for vlan header presence */
222         if ((buf[12 + 2] != 0x81) || (buf[13 + 2] != 0x00)) // always present
223                 goto recv;
224
225         port = buf[0] & 0xf;
226
227         /* no need to fix up packets coming from a tagged source */
228         if (priv->vlan_tagged & (1 << port))
229                 goto recv;
230
231         /* lookup port vid from local table, the switch passes an invalid vlan id */
232         vlan = priv->pvid[port];
233
234         buf[14 + 2] &= 0xf0;
235         buf[14 + 2] |= vlan >> 8;  //quatsch
236         buf[15 + 2] = vlan & 0xff;
237
238 recv:
239         skb->protocol = eth_type_trans(skb, skb->dev);
240
241         if (napi)
242                 return netif_receive_skb(skb);
243         else
244                 return netif_rx(skb);
245
246 error:
247         /* no vlan? eat the packet! */
248         dev_kfree_skb_any(skb);
249         return 0;
250 }
251
252 static int
253 ar8216_netif_rx(struct sk_buff *skb)
254 {
255         return ar8216_mangle_rx(skb, 0);
256 }
257
258 static int
259 ar8216_netif_receive_skb(struct sk_buff *skb)
260 {
261         return ar8216_mangle_rx(skb, 1);
262 }
263
264
265 static struct switch_attr ar8216_globals[] = {
266         {
267                 .type = SWITCH_TYPE_INT,
268                 .name = "vlan",
269                 .description = "Enable VLAN mode",
270                 .set = ar8216_set_vlan,
271                 .get = ar8216_get_vlan,
272                 .max = 1
273         },
274 };
275
276 static struct switch_attr ar8216_port[] = {
277 };
278
279 static struct switch_attr ar8216_vlan[] = {
280         {
281                 .type = SWITCH_TYPE_INT,
282                 .name = "pvid",
283                 .description = "VLAN ID",
284                 .set = ar8216_set_vid,
285                 .get = ar8216_get_vid,
286                 .max = 4095,
287         },
288 };
289
290
291 static int
292 ar8216_get_ports(struct switch_dev *dev, struct switch_val *val)
293 {
294         struct ar8216_priv *priv = to_ar8216(dev);
295         u8 ports = priv->vlan_table[val->port_vlan];
296         int i;
297
298         val->len = 0;
299         for (i = 0; i < AR8216_NUM_PORTS; i++) {
300                 struct switch_port *p;
301
302                 if (!(ports & (1 << i)))
303                         continue;
304
305                 p = &val->value.ports[val->len++];
306                 p->id = i;
307                 if (priv->vlan_tagged & (1 << i))
308                         p->flags = (1 << SWITCH_PORT_FLAG_TAGGED);
309                 else
310                         p->flags = 0;
311         }
312         return 0;
313 }
314
315 static int
316 ar8216_set_ports(struct switch_dev *dev, struct switch_val *val)
317 {
318         struct ar8216_priv *priv = to_ar8216(dev);
319         u8 *vt = &priv->vlan_table[val->port_vlan];
320         int i, j;
321
322         *vt = 0;
323         for (i = 0; i < val->len; i++) {
324                 struct switch_port *p = &val->value.ports[i];
325
326                 if (p->flags & (1 << SWITCH_PORT_FLAG_TAGGED))
327                         priv->vlan_tagged |= (1 << p->id);
328                 else {
329                         priv->vlan_tagged &= ~(1 << p->id);
330                         priv->pvid[p->id] = val->port_vlan;
331
332                         /* make sure that an untagged port does not
333                          * appear in other vlans */
334                         for (j = 0; j < AR8216_NUM_VLANS; j++) {
335                                 if (j == val->port_vlan)
336                                         continue;
337                                 priv->vlan_table[j] &= ~(1 << p->id);
338                         }
339                 }
340
341                 *vt |= 1 << p->id;
342         }
343         return 0;
344 }
345
346 static int
347 ar8216_wait_bit(struct ar8216_priv *priv, int reg, u32 mask, u32 val)
348 {
349         int timeout = 20;
350
351         while ((priv->read(priv, reg) & mask) != val) {
352                 if (timeout-- <= 0) {
353                         printk(KERN_ERR "ar8216: timeout waiting for operation to complete\n");
354                         return 1;
355                 }
356         }
357         return 0;
358 }
359
360 static void
361 ar8216_vtu_op(struct ar8216_priv *priv, u32 op, u32 val)
362 {
363         if (ar8216_wait_bit(priv, AR8216_REG_VTU, AR8216_VTU_ACTIVE, 0))
364                 return;
365         if ((op & AR8216_VTU_OP) == AR8216_VTU_OP_LOAD) {
366                 val &= AR8216_VTUDATA_MEMBER;
367                 val |= AR8216_VTUDATA_VALID;
368                 priv->write(priv, AR8216_REG_VTU_DATA, val);
369         }
370         op |= AR8216_VTU_ACTIVE;
371         priv->write(priv, AR8216_REG_VTU, op);
372 }
373
374 static int
375 ar8216_hw_apply(struct switch_dev *dev)
376 {
377         struct ar8216_priv *priv = to_ar8216(dev);
378         u8 portmask[AR8216_NUM_PORTS];
379         int i, j;
380
381         /* flush all vlan translation unit entries */
382         ar8216_vtu_op(priv, AR8216_VTU_OP_FLUSH, 0);
383
384         memset(portmask, 0, sizeof(portmask));
385         if (priv->vlan) {
386                 /* calculate the port destination masks and load vlans
387                  * into the vlan translation unit */
388                 for (j = 0; j < AR8216_NUM_VLANS; j++) {
389                         u8 vp = priv->vlan_table[j];
390
391                         if (!vp)
392                                 continue;
393
394                         for (i = 0; i < AR8216_NUM_PORTS; i++) {
395                                 u8 mask = (1 << i);
396                                 if (vp & mask)
397                                         portmask[i] |= vp & ~mask;
398                         }
399
400                         if (!priv->vlan_table[j])
401                                 continue;
402
403                         ar8216_vtu_op(priv,
404                                 AR8216_VTU_OP_LOAD |
405                                 (priv->vlan_id[j] << AR8216_VTU_VID_S),
406                                 priv->vlan_table[j]);
407                 }
408         } else {
409                 /* vlan disabled:
410                  * isolate all ports, but connect them to the cpu port */
411                 for (i = 0; i < AR8216_NUM_PORTS; i++) {
412                         if (i == AR8216_PORT_CPU)
413                                 continue;
414
415                         portmask[i] = 1 << AR8216_PORT_CPU;
416                         portmask[AR8216_PORT_CPU] |= (1 << i);
417                 }
418         }
419
420         /* update the port destination mask registers and tag settings */
421         for (i = 0; i < AR8216_NUM_PORTS; i++) {
422                 int egress, ingress;
423                 int pvid;
424
425                 if (priv->vlan) {
426                         pvid = priv->vlan_id[priv->pvid[i]];
427                 } else {
428                         pvid = i;
429                 }
430
431                 if (priv->vlan && (priv->vlan_tagged & (1 << i))) {
432                         egress = AR8216_OUT_ADD_VLAN;
433                 } else {
434                         egress = AR8216_OUT_STRIP_VLAN;
435                 }
436                 ingress = AR8216_IN_SECURE;
437
438                 ar8216_rmw(priv, AR8216_REG_PORT_CTRL(i),
439                         AR8216_PORT_CTRL_LEARN | AR8216_PORT_CTRL_VLAN_MODE |
440                         AR8216_PORT_CTRL_SINGLE_VLAN | AR8216_PORT_CTRL_STATE |
441                         AR8216_PORT_CTRL_HEADER | AR8216_PORT_CTRL_LEARN_LOCK,
442                         AR8216_PORT_CTRL_LEARN |
443                           (i == AR8216_PORT_CPU ? 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         priv->write(priv,0x60, 0xffffffff);
493         priv->write(priv,0x64, 0xaaaaaaaa);
494         priv->write(priv,0x68, 0x55555555);   
495         priv->write(priv,0x6c, 0x0);   
496         priv->write(priv,0x70, 0x41af);
497
498         /* set mtu */
499         ar8216_rmw(priv, AR8216_REG_GLOBAL_CTRL,AR8216_GCTRL_MTU,1716 ); //     1500 + 4 /* vlan */ + 2 /* header */);
500
501
502         return ar8216_hw_apply(dev);
503 }
504
505 static int
506 ar8216_config_init(struct phy_device *pdev)
507 {
508         struct ar8216_priv *priv;
509         struct net_device *dev = pdev->attached_dev;
510         int ret;
511
512         printk("%s: AR8216 PHY driver attached.\n", pdev->attached_dev->name);
513         pdev->supported = ADVERTISED_100baseT_Full;
514         pdev->advertising = ADVERTISED_100baseT_Full;
515
516         priv = kzalloc(sizeof(struct ar8216_priv), GFP_KERNEL);
517         if (priv == NULL)
518                 return -ENOMEM;
519
520         priv->phy = pdev;
521         priv->read = ar8216_mii_read;
522         priv->write = ar8216_mii_write;
523         memcpy(&priv->dev, &athdev, sizeof(struct switch_dev));
524         pdev->priv = priv;
525         if ((ret = register_switch(&priv->dev, pdev->attached_dev)) < 0) {
526                 kfree(priv);
527                 goto done;
528         }
529
530         ret = ar8216_reset_switch(&priv->dev);
531         if (ret)
532                 goto done;
533
534         dev->phy_ptr = priv;
535         pdev->pkt_align = 2;
536         priv->hardstart = dev->hard_start_xmit;
537         pdev->netif_receive_skb = ar8216_netif_receive_skb;
538         pdev->netif_rx = ar8216_netif_rx;
539         dev->hard_start_xmit = ar8216_mangle_tx;
540
541 done:
542         return ret;
543 }
544
545 static int
546 ar8216_read_status(struct phy_device *phydev)
547 {
548         struct ar8216_priv *priv = phydev->priv;
549
550         phydev->speed = SPEED_100;
551         phydev->duplex = DUPLEX_FULL;
552         phydev->state = PHY_UP;
553
554         /* flush the address translation unit */
555         if (ar8216_wait_bit(priv, AR8216_REG_ATU, AR8216_ATU_ACTIVE, 0))
556                 return -ETIMEDOUT;
557
558         priv->write(priv, AR8216_REG_ATU, AR8216_ATU_OP_FLUSH);
559
560         return 0;
561 }
562
563 static int
564 ar8216_config_aneg(struct phy_device *phydev)
565 {
566         return 0;
567 }
568
569 static int
570 ar8216_probe(struct phy_device *pdev)
571 {
572         struct ar8216_priv priv;
573
574         u8 id, rev;
575         u32 val;
576
577         priv.phy = pdev;
578         val = ar8216_mii_read(&priv, AR8216_REG_CTRL);
579         rev = val & 0xff;
580         id = (val >> 8) & 0xff;
581         if ((id != 1) || (rev != 1))
582                 return -ENODEV;
583
584         return 0;
585 }
586
587 static void
588 ar8216_remove(struct phy_device *pdev)
589 {
590         struct ar8216_priv *priv = pdev->priv;
591         struct net_device *dev = pdev->attached_dev;
592
593         if (!priv)
594                 return;
595
596         if (priv->hardstart && dev)
597                 dev->hard_start_xmit = priv->hardstart;
598         unregister_switch(&priv->dev);
599         kfree(priv);
600 }
601
602 /* template */
603 static struct switch_dev athdev = {
604         .name = "Atheros AR8216",
605         .cpu_port = AR8216_PORT_CPU,
606         .ports = AR8216_NUM_PORTS,
607         .vlans = AR8216_NUM_VLANS,
608         .attr_global = {
609                 .attr = ar8216_globals,
610                 .n_attr = ARRAY_SIZE(ar8216_globals),
611         },
612         .attr_port = {
613                 .attr = ar8216_port,
614                 .n_attr = ARRAY_SIZE(ar8216_port),
615         },
616         .attr_vlan = {
617                 .attr = ar8216_vlan,
618                 .n_attr = ARRAY_SIZE(ar8216_vlan),
619         },
620         .get_port_pvid = ar8216_get_pvid,
621         .set_port_pvid = ar8216_set_pvid,
622         .get_vlan_ports = ar8216_get_ports,
623         .set_vlan_ports = ar8216_set_ports,
624         .apply_config = ar8216_hw_apply,
625         .reset_switch = ar8216_reset_switch,
626 };
627
628 static struct phy_driver ar8216_driver = {
629         .name           = "Atheros AR8216",
630         .features       = PHY_BASIC_FEATURES,
631         .probe          = ar8216_probe,
632         .remove         = ar8216_remove,
633         .config_init    = &ar8216_config_init,
634         .config_aneg    = &ar8216_config_aneg,
635         .read_status    = &ar8216_read_status,
636         .driver         = { .owner = THIS_MODULE },
637 };
638
639 int __init
640 ar8216_init(void)
641 {
642         return phy_driver_register(&ar8216_driver);
643 }
644
645 void __exit
646 ar8216_exit(void)
647 {
648         phy_driver_unregister(&ar8216_driver);
649 }
650
651 module_init(ar8216_init);
652 module_exit(ar8216_exit);
653 MODULE_LICENSE("GPL");
654
Note: See TracBrowser for help on using the browser.