d83845c536dbff06af8cf4094eb9b4922cdde5b8
[openwrt/svn-archive/archive.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 "ar8216.h"
31
32 #define AR8216_REG_PORT_RATE(_i) (AR8216_PORT_OFFSET(_i) + 0x000c)
33 #define AR8216_REG_PORT_PRIO(_i) (AR8216_PORT_OFFSET(_i) + 0x0010)
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 /* all fields below are cleared on reset */
41 bool vlan;
42 u8 vlan_id[AR8216_NUM_VLANS];
43 u8 vlan_table[AR8216_NUM_VLANS];
44 u8 vlan_tagged;
45 u16 pvid[AR8216_NUM_PORTS];
46 };
47 static struct switch_dev athdev;
48
49 #define to_ar8216(_dev) container_of(_dev, struct ar8216_priv, dev)
50
51 static inline void
52 split_addr(u32 regaddr, u16 *r1, u16 *r2, u16 *page)
53 {
54 regaddr >>= 1;
55 *r1 = regaddr & 0x1e;
56
57 regaddr >>= 5;
58 *r2 = regaddr & 0x7;
59
60 regaddr >>= 3;
61 *page = regaddr & 0x1ff;
62 }
63
64 static u32
65 ar8216_mii_read(struct ar8216_priv *priv, int reg)
66 {
67 struct phy_device *phy = priv->phy;
68 u16 r1, r2, page;
69 u16 lo, hi;
70
71 split_addr((u32) reg, &r1, &r2, &page);
72 phy->bus->write(phy->bus, 0x18, 0, page);
73 lo = phy->bus->read(phy->bus, 0x10 | r2, r1);
74 hi = phy->bus->read(phy->bus, 0x10 | r2, r1 + 1);
75
76 return (hi << 16) | lo;
77 }
78
79 static void
80 ar8216_mii_write(struct ar8216_priv *priv, int reg, u32 val)
81 {
82 struct phy_device *phy = priv->phy;
83 u16 r1, r2, r3;
84 u16 lo, hi;
85
86 split_addr((u32) reg, &r1, &r2, &r3);
87 phy->bus->write(phy->bus, 0x18, 0, r3);
88
89 lo = val & 0xffff;
90 hi = (u16) (val >> 16);
91 phy->bus->write(phy->bus, 0x10 | r2, r1 + 1, hi);
92 phy->bus->write(phy->bus, 0x10 | r2, r1, lo);
93 }
94
95 static u32
96 ar8216_rmw(struct ar8216_priv *priv, int reg, u32 mask, u32 val)
97 {
98 u32 v;
99
100 v = priv->read(priv, reg);
101 v &= ~mask;
102 v |= val;
103 priv->write(priv, reg, v);
104
105 return v;
106 }
107
108 static int
109 ar8216_set_vlan(struct switch_dev *dev, const struct switch_attr *attr,
110 struct switch_val *val)
111 {
112 struct ar8216_priv *priv = to_ar8216(dev);
113 priv->vlan = !!val->value.i;
114 return 0;
115 }
116
117 static int
118 ar8216_get_vlan(struct switch_dev *dev, const struct switch_attr *attr,
119 struct switch_val *val)
120 {
121 struct ar8216_priv *priv = to_ar8216(dev);
122 val->value.i = priv->vlan;
123 return 0;
124 }
125
126
127 static int
128 ar8216_set_pvid(struct switch_dev *dev, int port, int vlan)
129 {
130 struct ar8216_priv *priv = to_ar8216(dev);
131 priv->pvid[port] = vlan;
132 return 0;
133 }
134
135 static int
136 ar8216_get_pvid(struct switch_dev *dev, int port, int *vlan)
137 {
138 struct ar8216_priv *priv = to_ar8216(dev);
139 *vlan = priv->pvid[port];
140 return 0;
141 }
142
143 static int
144 ar8216_set_vid(struct switch_dev *dev, const struct switch_attr *attr,
145 struct switch_val *val)
146 {
147 struct ar8216_priv *priv = to_ar8216(dev);
148 priv->vlan_id[val->port_vlan] = val->value.i;
149 return 0;
150 }
151
152 static int
153 ar8216_get_vid(struct switch_dev *dev, const struct switch_attr *attr,
154 struct switch_val *val)
155 {
156 struct ar8216_priv *priv = to_ar8216(dev);
157 val->value.i = priv->vlan_id[val->port_vlan];
158 return 0;
159 }
160
161
162 static struct switch_attr ar8216_globals[] = {
163 {
164 .type = SWITCH_TYPE_INT,
165 .name = "vlan",
166 .description = "Enable VLAN mode",
167 .set = ar8216_set_vlan,
168 .get = ar8216_get_vlan,
169 .max = 1
170 },
171 };
172
173 static struct switch_attr ar8216_port[] = {
174 };
175
176 static struct switch_attr ar8216_vlan[] = {
177 {
178 .type = SWITCH_TYPE_INT,
179 .name = "pvid",
180 .description = "VLAN ID",
181 .set = ar8216_set_vid,
182 .get = ar8216_get_vid,
183 .max = 4095,
184 },
185 };
186
187
188 static int
189 ar8216_get_ports(struct switch_dev *dev, struct switch_val *val)
190 {
191 struct ar8216_priv *priv = to_ar8216(dev);
192 u8 ports = priv->vlan_table[val->port_vlan];
193 int i;
194
195 val->len = 0;
196 for (i = 0; i < AR8216_NUM_PORTS; i++) {
197 struct switch_port *p;
198
199 if (!(ports & (1 << i)))
200 continue;
201
202 p = &val->value.ports[val->len++];
203 p->id = i;
204 if (priv->vlan_tagged & (1 << i))
205 p->flags = (1 << SWITCH_PORT_FLAG_TAGGED);
206 else
207 p->flags = 0;
208 }
209 return 0;
210 }
211
212 static int
213 ar8216_set_ports(struct switch_dev *dev, struct switch_val *val)
214 {
215 struct ar8216_priv *priv = to_ar8216(dev);
216 u8 *vt = &priv->vlan_table[val->port_vlan];
217 int i, j;
218
219 *vt = 0;
220 for (i = 0; i < val->len; i++) {
221 struct switch_port *p = &val->value.ports[i];
222
223 if (p->flags & (1 << SWITCH_PORT_FLAG_TAGGED))
224 priv->vlan_tagged |= (1 << p->id);
225 else {
226 priv->vlan_tagged &= ~(1 << p->id);
227 priv->pvid[p->id] = val->port_vlan;
228
229 /* make sure that an untagged port does not
230 * appear in other vlans */
231 for (j = 0; j < AR8216_NUM_VLANS; j++) {
232 if (j == val->port_vlan)
233 continue;
234 priv->vlan_table[j] &= ~(1 << p->id);
235 }
236 }
237
238 *vt |= 1 << p->id;
239 }
240 return 0;
241 }
242
243 static int
244 ar8216_wait_bit(struct ar8216_priv *priv, int reg, u32 mask, u32 val)
245 {
246 int timeout = 20;
247
248 while ((priv->read(priv, reg) & mask) != val) {
249 if (timeout-- <= 0) {
250 printk(KERN_ERR "ar8216: timeout waiting for operation to complete\n");
251 return 1;
252 }
253 }
254 return 0;
255 }
256
257 static void
258 ar8216_vtu_op(struct ar8216_priv *priv, u32 op, u32 val)
259 {
260 if (ar8216_wait_bit(priv, AR8216_REG_VTU, AR8216_VTU_ACTIVE, 0))
261 return;
262 if ((op & AR8216_VTU_OP) == AR8216_VTU_OP_LOAD) {
263 val &= AR8216_VTUDATA_MEMBER;
264 val |= AR8216_VTUDATA_VALID;
265 priv->write(priv, AR8216_REG_VTU_DATA, val);
266 }
267 op |= AR8216_VTU_ACTIVE;
268 priv->write(priv, AR8216_REG_VTU, op);
269 }
270
271 static int
272 ar8216_hw_apply(struct switch_dev *dev)
273 {
274 struct ar8216_priv *priv = to_ar8216(dev);
275 u8 portmask[AR8216_NUM_PORTS];
276 int i, j;
277
278 /* flush all vlan translation unit entries */
279 ar8216_vtu_op(priv, AR8216_VTU_OP_FLUSH, 0);
280
281 memset(portmask, 0, sizeof(portmask));
282 if (priv->vlan) {
283 /* calculate the port destination masks and load vlans
284 * into the vlan translation unit */
285 for (j = 0; j < AR8216_NUM_VLANS; j++) {
286 u8 vp = priv->vlan_table[j];
287
288 if (!vp)
289 continue;
290
291 for (i = 0; i < AR8216_NUM_PORTS; i++) {
292 u8 mask = (1 << i);
293 if (vp & mask)
294 portmask[i] |= vp & ~mask;
295 }
296
297 if (!priv->vlan_table[j])
298 continue;
299
300 ar8216_vtu_op(priv,
301 AR8216_VTU_OP_LOAD |
302 (priv->vlan_id[j] << AR8216_VTU_VID_S),
303 priv->vlan_table[j]);
304 }
305 } else {
306 /* vlan disabled:
307 * isolate all ports, but connect them to the cpu port */
308 for (i = 0; i < AR8216_NUM_PORTS; i++) {
309 if (i == AR8216_PORT_CPU)
310 continue;
311
312 portmask[i] = 1 << AR8216_PORT_CPU;
313 portmask[AR8216_PORT_CPU] |= (1 << i);
314 }
315 }
316
317 /* update the port destination mask registers and tag settings */
318 for (i = 0; i < AR8216_NUM_PORTS; i++) {
319 int egress, ingress;
320 int pvid;
321
322 if (priv->vlan) {
323 pvid = priv->vlan_id[priv->pvid[i]];
324 } else {
325 pvid = i;
326 }
327
328 if (priv->vlan && (priv->vlan_tagged & (1 << i))) {
329 egress = AR8216_OUT_ADD_VLAN;
330 ingress = AR8216_IN_PORT_FALLBACK;
331 } else {
332 egress = AR8216_OUT_STRIP_VLAN;
333 ingress = AR8216_IN_SECURE;
334 }
335
336 ar8216_rmw(priv, AR8216_REG_PORT_CTRL(i),
337 AR8216_PORT_CTRL_LEARN | AR8216_PORT_CTRL_VLAN_MODE |
338 AR8216_PORT_CTRL_SINGLE_VLAN | AR8216_PORT_CTRL_STATE |
339 AR8216_PORT_CTRL_HEADER | AR8216_PORT_CTRL_LEARN_LOCK,
340 AR8216_PORT_CTRL_LEARN |
341 (egress << AR8216_PORT_CTRL_VLAN_MODE_S) |
342 (priv->vlan ? AR8216_PORT_CTRL_SINGLE_VLAN : 0) |
343 (AR8216_PORT_STATE_FORWARD << AR8216_PORT_CTRL_STATE_S));
344
345 ar8216_rmw(priv, AR8216_REG_PORT_VLAN(i),
346 AR8216_PORT_VLAN_DEST_PORTS | AR8216_PORT_VLAN_MODE |
347 AR8216_PORT_VLAN_DEFAULT_ID,
348 (portmask[i] << AR8216_PORT_VLAN_DEST_PORTS_S) |
349 (ingress << AR8216_PORT_VLAN_MODE_S) |
350 (pvid << AR8216_PORT_VLAN_DEFAULT_ID_S));
351 }
352
353 return 0;
354 }
355
356 static int
357 ar8216_reset_switch(struct switch_dev *dev)
358 {
359 struct ar8216_priv *priv = to_ar8216(dev);
360 int i;
361
362 memset(&priv->vlan, 0, sizeof(struct ar8216_priv) -
363 offsetof(struct ar8216_priv, vlan));
364 for (i = 0; i < AR8216_NUM_VLANS; i++) {
365 priv->vlan_id[i] = i;
366 }
367 for (i = 0; i < AR8216_NUM_PORTS; i++) {
368 /* Enable port learning and tx */
369 priv->write(priv, AR8216_REG_PORT_CTRL(i),
370 AR8216_PORT_CTRL_LEARN |
371 (4 << AR8216_PORT_CTRL_STATE_S));
372
373 priv->write(priv, AR8216_REG_PORT_VLAN(i), 0);
374
375 /* Configure all PHYs */
376 if (i == AR8216_PORT_CPU) {
377 priv->write(priv, AR8216_REG_PORT_STATUS(i),
378 AR8216_PORT_STATUS_LINK_UP |
379 AR8216_PORT_STATUS_SPEED |
380 AR8216_PORT_STATUS_TXMAC |
381 AR8216_PORT_STATUS_RXMAC |
382 AR8216_PORT_STATUS_DUPLEX);
383 } else {
384 priv->write(priv, AR8216_REG_PORT_STATUS(i),
385 AR8216_PORT_STATUS_LINK_AUTO);
386 }
387 }
388 /* XXX: undocumented magic from atheros, required! */
389 priv->write(priv, 0x38, 0xc000050e);
390 return ar8216_hw_apply(dev);
391 }
392
393 static int
394 ar8216_config_init(struct phy_device *pdev)
395 {
396 struct ar8216_priv *priv;
397 int ret;
398
399 printk("%s: AR8216 PHY driver attached.\n", pdev->attached_dev->name);
400 pdev->supported = ADVERTISED_100baseT_Full;
401 pdev->advertising = ADVERTISED_100baseT_Full;
402
403 priv = kzalloc(sizeof(struct ar8216_priv), GFP_KERNEL);
404 if (priv == NULL)
405 return -ENOMEM;
406
407 priv->phy = pdev;
408 priv->read = ar8216_mii_read;
409 priv->write = ar8216_mii_write;
410 memcpy(&priv->dev, &athdev, sizeof(struct switch_dev));
411 pdev->priv = priv;
412 if ((ret = register_switch(&priv->dev, pdev->attached_dev)) < 0) {
413 kfree(priv);
414 goto done;
415 }
416
417 ret = ar8216_reset_switch(&priv->dev);
418 done:
419 return ret;
420 }
421
422 static int
423 ar8216_read_status(struct phy_device *phydev)
424 {
425 struct ar8216_priv *priv = phydev->priv;
426
427 phydev->speed = SPEED_100;
428 phydev->duplex = DUPLEX_FULL;
429 phydev->state = PHY_UP;
430
431 /* flush the address translation unit */
432 if (ar8216_wait_bit(priv, AR8216_REG_ATU, AR8216_ATU_ACTIVE, 0))
433 return -ETIMEDOUT;
434
435 priv->write(priv, AR8216_REG_ATU, AR8216_ATU_OP_FLUSH);
436
437 return 0;
438 }
439
440 static int
441 ar8216_config_aneg(struct phy_device *phydev)
442 {
443 return 0;
444 }
445
446 static int
447 ar8216_probe(struct phy_device *pdev)
448 {
449 struct ar8216_priv priv;
450
451 u8 id, rev;
452 u32 val;
453
454 priv.phy = pdev;
455 val = ar8216_mii_read(&priv, AR8216_REG_CTRL);
456 rev = val & 0xff;
457 id = (val >> 8) & 0xff;
458 if ((id != 1) || (rev != 1))
459 return -ENODEV;
460
461 return 0;
462 }
463
464 static void
465 ar8216_remove(struct phy_device *pdev)
466 {
467 struct ar8216_priv *priv = pdev->priv;
468
469 if (!priv)
470 return;
471
472 unregister_switch(&priv->dev);
473 kfree(priv);
474 }
475
476 /* template */
477 static struct switch_dev athdev = {
478 .name = "Atheros AR8216",
479 .cpu_port = AR8216_PORT_CPU,
480 .ports = AR8216_NUM_PORTS,
481 .vlans = AR8216_NUM_VLANS,
482 .attr_global = {
483 .attr = ar8216_globals,
484 .n_attr = ARRAY_SIZE(ar8216_globals),
485 },
486 .attr_port = {
487 .attr = ar8216_port,
488 .n_attr = ARRAY_SIZE(ar8216_port),
489 },
490 .attr_vlan = {
491 .attr = ar8216_vlan,
492 .n_attr = ARRAY_SIZE(ar8216_vlan),
493 },
494 .get_port_pvid = ar8216_get_pvid,
495 .set_port_pvid = ar8216_set_pvid,
496 .get_vlan_ports = ar8216_get_ports,
497 .set_vlan_ports = ar8216_set_ports,
498 .apply_config = ar8216_hw_apply,
499 .reset_switch = ar8216_reset_switch,
500 };
501
502 static struct phy_driver ar8216_driver = {
503 .name = "Atheros AR8216",
504 .features = PHY_BASIC_FEATURES,
505 .probe = ar8216_probe,
506 .remove = ar8216_remove,
507 .config_init = &ar8216_config_init,
508 .config_aneg = &ar8216_config_aneg,
509 .read_status = &ar8216_read_status,
510 .driver = { .owner = THIS_MODULE },
511 };
512
513 int __init
514 ar8216_init(void)
515 {
516 return phy_driver_register(&ar8216_driver);
517 }
518
519 void __exit
520 ar8216_exit(void)
521 {
522 phy_driver_unregister(&ar8216_driver);
523 }
524
525 module_init(ar8216_init);
526 module_exit(ar8216_exit);
527 MODULE_LICENSE("GPL");
528