[package] iproute2: honour when building libnetlink.a, completes r18323 and fixes...
[openwrt.git] / package / switch / src / switch-robo.c
1 /*
2 * Broadcom BCM5325E/536x switch configuration module
3 *
4 * Copyright (C) 2005 Felix Fietkau <nbd@nbd.name>
5 * Copyright (C) 2008 Michael Buesch <mb@bu3sch.de>
6 * Based on 'robocfg' by Oleg I. Vdovikin
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
22 */
23
24 #include <linux/autoconf.h>
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/if.h>
28 #include <linux/if_arp.h>
29 #include <linux/sockios.h>
30 #include <linux/ethtool.h>
31 #include <linux/mii.h>
32 #include <linux/delay.h>
33 #include <asm/uaccess.h>
34
35 #include "switch-core.h"
36 #include "etc53xx.h"
37
38 #define DRIVER_NAME "bcm53xx"
39 #define DRIVER_VERSION "0.02"
40 #define PFX "roboswitch: "
41
42 #define ROBO_PHY_ADDR 0x1E /* robo switch phy address */
43 #define ROBO_PHY_ADDR_TG3 0x01 /* Tigon3 PHY address */
44 #define ROBO_PHY_ADDR_BCM63XX 0x00 /* BCM63XX PHY address */
45
46 /* MII registers */
47 #define REG_MII_PAGE 0x10 /* MII Page register */
48 #define REG_MII_ADDR 0x11 /* MII Address register */
49 #define REG_MII_DATA0 0x18 /* MII Data register 0 */
50
51 #define REG_MII_PAGE_ENABLE 1
52 #define REG_MII_ADDR_WRITE 1
53 #define REG_MII_ADDR_READ 2
54
55 /* Robo device ID register (in ROBO_MGMT_PAGE) */
56 #define ROBO_DEVICE_ID 0x30
57 #define ROBO_DEVICE_ID_5325 0x25 /* Faked */
58 #define ROBO_DEVICE_ID_5395 0x95
59 #define ROBO_DEVICE_ID_5397 0x97
60 #define ROBO_DEVICE_ID_5398 0x98
61 #define ROBO_DEVICE_ID_53115 0x3115
62
63 /* Private et.o ioctls */
64 #define SIOCGETCPHYRD (SIOCDEVPRIVATE + 9)
65 #define SIOCSETCPHYWR (SIOCDEVPRIVATE + 10)
66
67 /* linux 2.4 does not have 'bool' */
68 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
69 #define bool int
70 #endif
71
72 /* Only available on brcm-2.4/brcm47xx */
73 #ifdef BROADCOM
74 extern char *nvram_get(const char *name);
75 #define getvar(str) (nvram_get(str)?:"")
76 #else
77 #define getvar(str) ""
78 #endif
79
80 /* Data structure for a Roboswitch device. */
81 struct robo_switch {
82 char *device; /* The device name string (ethX) */
83 u16 devid; /* ROBO_DEVICE_ID_53xx */
84 bool use_et;
85 bool is_5350;
86 u8 phy_addr; /* PHY address of the device */
87 struct ifreq ifr;
88 struct net_device *dev;
89 unsigned char port[6];
90 };
91
92 /* Currently we can only have one device in the system. */
93 static struct robo_switch robo;
94
95
96 static int do_ioctl(int cmd, void *buf)
97 {
98 mm_segment_t old_fs = get_fs();
99 int ret;
100
101 if (buf != NULL)
102 robo.ifr.ifr_data = (caddr_t) buf;
103
104 set_fs(KERNEL_DS);
105 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,31)
106 ret = robo.dev->netdev_ops->ndo_do_ioctl(robo.dev, &robo.ifr, cmd);
107 #else
108 ret = robo.dev->do_ioctl(robo.dev, &robo.ifr, cmd);
109 #endif
110 set_fs(old_fs);
111
112 return ret;
113 }
114
115 static u16 mdio_read(__u16 phy_id, __u8 reg)
116 {
117 if (robo.use_et) {
118 int args[2] = { reg };
119
120 if (phy_id != robo.phy_addr) {
121 printk(KERN_ERR PFX
122 "Access to real 'phy' registers unavaliable.\n"
123 "Upgrade kernel driver.\n");
124
125 return 0xffff;
126 }
127
128
129 if (do_ioctl(SIOCGETCPHYRD, &args) < 0) {
130 printk(KERN_ERR PFX
131 "[%s:%d] SIOCGETCPHYRD failed!\n", __FILE__, __LINE__);
132 return 0xffff;
133 }
134
135 return args[1];
136 } else {
137 struct mii_ioctl_data *mii = (struct mii_ioctl_data *) &robo.ifr.ifr_data;
138 mii->phy_id = phy_id;
139 mii->reg_num = reg;
140
141 if (do_ioctl(SIOCGMIIREG, NULL) < 0) {
142 printk(KERN_ERR PFX
143 "[%s:%d] SIOCGMIIREG failed!\n", __FILE__, __LINE__);
144
145 return 0xffff;
146 }
147
148 return mii->val_out;
149 }
150 }
151
152 static void mdio_write(__u16 phy_id, __u8 reg, __u16 val)
153 {
154 if (robo.use_et) {
155 int args[2] = { reg, val };
156
157 if (phy_id != robo.phy_addr) {
158 printk(KERN_ERR PFX
159 "Access to real 'phy' registers unavaliable.\n"
160 "Upgrade kernel driver.\n");
161
162 return;
163 }
164
165 if (do_ioctl(SIOCSETCPHYWR, args) < 0) {
166 printk(KERN_ERR PFX
167 "[%s:%d] SIOCGETCPHYWR failed!\n", __FILE__, __LINE__);
168 return;
169 }
170 } else {
171 struct mii_ioctl_data *mii = (struct mii_ioctl_data *)&robo.ifr.ifr_data;
172
173 mii->phy_id = phy_id;
174 mii->reg_num = reg;
175 mii->val_in = val;
176
177 if (do_ioctl(SIOCSMIIREG, NULL) < 0) {
178 printk(KERN_ERR PFX
179 "[%s:%d] SIOCSMIIREG failed!\n", __FILE__, __LINE__);
180 return;
181 }
182 }
183 }
184
185 static int robo_reg(__u8 page, __u8 reg, __u8 op)
186 {
187 int i = 3;
188
189 /* set page number */
190 mdio_write(robo.phy_addr, REG_MII_PAGE,
191 (page << 8) | REG_MII_PAGE_ENABLE);
192
193 /* set register address */
194 mdio_write(robo.phy_addr, REG_MII_ADDR,
195 (reg << 8) | op);
196
197 /* check if operation completed */
198 while (i--) {
199 if ((mdio_read(robo.phy_addr, REG_MII_ADDR) & 3) == 0)
200 return 0;
201 }
202
203 printk(KERN_ERR PFX "[%s:%d] timeout in robo_reg!\n", __FILE__, __LINE__);
204
205 return 0;
206 }
207
208 /*
209 static void robo_read(__u8 page, __u8 reg, __u16 *val, int count)
210 {
211 int i;
212
213 robo_reg(page, reg, REG_MII_ADDR_READ);
214
215 for (i = 0; i < count; i++)
216 val[i] = mdio_read(robo.phy_addr, REG_MII_DATA0 + i);
217 }
218 */
219
220 static __u16 robo_read16(__u8 page, __u8 reg)
221 {
222 robo_reg(page, reg, REG_MII_ADDR_READ);
223
224 return mdio_read(robo.phy_addr, REG_MII_DATA0);
225 }
226
227 static __u32 robo_read32(__u8 page, __u8 reg)
228 {
229 robo_reg(page, reg, REG_MII_ADDR_READ);
230
231 return mdio_read(robo.phy_addr, REG_MII_DATA0) +
232 (mdio_read(robo.phy_addr, REG_MII_DATA0 + 1) << 16);
233 }
234
235 static void robo_write16(__u8 page, __u8 reg, __u16 val16)
236 {
237 /* write data */
238 mdio_write(robo.phy_addr, REG_MII_DATA0, val16);
239
240 robo_reg(page, reg, REG_MII_ADDR_WRITE);
241 }
242
243 static void robo_write32(__u8 page, __u8 reg, __u32 val32)
244 {
245 /* write data */
246 mdio_write(robo.phy_addr, REG_MII_DATA0, val32 & 65535);
247 mdio_write(robo.phy_addr, REG_MII_DATA0 + 1, val32 >> 16);
248
249 robo_reg(page, reg, REG_MII_ADDR_WRITE);
250 }
251
252 /* checks that attached switch is 5325E/5350 */
253 static int robo_vlan5350(void)
254 {
255 /* set vlan access id to 15 and read it back */
256 __u16 val16 = 15;
257 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350, val16);
258
259 /* 5365 will refuse this as it does not have this reg */
260 return (robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350) == val16);
261 }
262
263 static int robo_switch_enable(void)
264 {
265 unsigned int i, last_port;
266 u16 val;
267
268 val = robo_read16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE);
269 if (!(val & (1 << 1))) {
270 /* Unmanaged mode */
271 val &= ~(1 << 0);
272 /* With forwarding */
273 val |= (1 << 1);
274 robo_write16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE, val);
275 val = robo_read16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE);
276 if (!(val & (1 << 1))) {
277 printk("Failed to enable switch\n");
278 return -EBUSY;
279 }
280
281 last_port = (robo.devid == ROBO_DEVICE_ID_5398) ?
282 ROBO_PORT6_CTRL : ROBO_PORT3_CTRL;
283 for (i = ROBO_PORT0_CTRL; i < last_port + 1; i++)
284 robo_write16(ROBO_CTRL_PAGE, i, 0);
285 }
286
287 /* WAN port LED, except for Netgear WGT634U */
288 if (strcmp(getvar("nvram_type"), "cfe") != 0)
289 robo_write16(ROBO_CTRL_PAGE, 0x16, 0x1F);
290
291 return 0;
292 }
293
294 static void robo_switch_reset(void)
295 {
296 if ((robo.devid == ROBO_DEVICE_ID_5395) ||
297 (robo.devid == ROBO_DEVICE_ID_5397) ||
298 (robo.devid == ROBO_DEVICE_ID_5398)) {
299 /* Trigger a software reset. */
300 robo_write16(ROBO_CTRL_PAGE, 0x79, 0x83);
301 mdelay(500);
302 robo_write16(ROBO_CTRL_PAGE, 0x79, 0);
303 }
304 }
305
306 static int robo_probe(char *devname)
307 {
308 __u32 phyid;
309 unsigned int i;
310 int err;
311
312 printk(KERN_INFO PFX "Probing device %s: ", devname);
313 strcpy(robo.ifr.ifr_name, devname);
314
315 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
316 if ((robo.dev = dev_get_by_name(devname)) == NULL) {
317 #else
318 if ((robo.dev = dev_get_by_name(&init_net, devname)) == NULL) {
319 #endif
320 printk("No such device\n");
321 return 1;
322 }
323
324 robo.device = devname;
325 for (i = 0; i < 5; i++)
326 robo.port[i] = i;
327 robo.port[5] = 8;
328
329 /* try access using MII ioctls - get phy address */
330 if (do_ioctl(SIOCGMIIPHY, NULL) < 0) {
331 robo.use_et = 1;
332 robo.phy_addr = ROBO_PHY_ADDR;
333 } else {
334 /* got phy address check for robo address */
335 struct mii_ioctl_data *mii = (struct mii_ioctl_data *) &robo.ifr.ifr_data;
336 if ((mii->phy_id != ROBO_PHY_ADDR) &&
337 (mii->phy_id != ROBO_PHY_ADDR_BCM63XX) &&
338 (mii->phy_id != ROBO_PHY_ADDR_TG3)) {
339 printk("Invalid phy address (%d)\n", mii->phy_id);
340 return 1;
341 }
342 robo.use_et = 0;
343 /* The robo has a fixed PHY address that is different from the
344 * Tigon3 and BCM63xx PHY address. */
345 robo.phy_addr = ROBO_PHY_ADDR;
346 }
347
348 phyid = mdio_read(robo.phy_addr, 0x2) |
349 (mdio_read(robo.phy_addr, 0x3) << 16);
350
351 if (phyid == 0xffffffff || phyid == 0x55210022) {
352 printk("No Robo switch in managed mode found, phy_id = 0x%08x\n", phyid);
353 return 1;
354 }
355
356 /* Get the device ID */
357 for (i = 0; i < 10; i++) {
358 robo.devid = robo_read16(ROBO_MGMT_PAGE, ROBO_DEVICE_ID);
359 if (robo.devid)
360 break;
361 udelay(10);
362 }
363 if (!robo.devid)
364 robo.devid = ROBO_DEVICE_ID_5325; /* Fake it */
365 robo.is_5350 = robo_vlan5350();
366
367 robo_switch_reset();
368 err = robo_switch_enable();
369 if (err)
370 return err;
371
372 printk("found a 5%s%x!%s\n", robo.devid & 0xff00 ? "" : "3", robo.devid,
373 robo.is_5350 ? " It's a 5350." : "");
374 return 0;
375 }
376
377
378 static int handle_vlan_port_read(void *driver, char *buf, int nr)
379 {
380 __u16 val16;
381 int len = 0;
382 int j;
383
384 val16 = (nr) /* vlan */ | (0 << 12) /* read */ | (1 << 13) /* enable */;
385
386 if (robo.is_5350) {
387 u32 val32;
388 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350, val16);
389 /* actual read */
390 val32 = robo_read32(ROBO_VLAN_PAGE, ROBO_VLAN_READ);
391 if ((val32 & (1 << 20)) /* valid */) {
392 for (j = 0; j < 6; j++) {
393 if (val32 & (1 << j)) {
394 len += sprintf(buf + len, "%d", j);
395 if (val32 & (1 << (j + 6))) {
396 if (j == 5) buf[len++] = 'u';
397 } else {
398 buf[len++] = 't';
399 if (robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1)) == nr)
400 buf[len++] = '*';
401 }
402 buf[len++] = '\t';
403 }
404 }
405 len += sprintf(buf + len, "\n");
406 }
407 } else {
408 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS, val16);
409 /* actual read */
410 val16 = robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_READ);
411 if ((val16 & (1 << 14)) /* valid */) {
412 for (j = 0; j < 6; j++) {
413 if (val16 & (1 << j)) {
414 len += sprintf(buf + len, "%d", j);
415 if (val16 & (1 << (j + 7))) {
416 if (j == 5) buf[len++] = 'u';
417 } else {
418 buf[len++] = 't';
419 if (robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1)) == nr)
420 buf[len++] = '*';
421 }
422 buf[len++] = '\t';
423 }
424 }
425 len += sprintf(buf + len, "\n");
426 }
427 }
428
429 buf[len] = '\0';
430
431 return len;
432 }
433
434 static int handle_vlan_port_write(void *driver, char *buf, int nr)
435 {
436 switch_driver *d = (switch_driver *) driver;
437 switch_vlan_config *c = switch_parse_vlan(d, buf);
438 int j;
439 __u16 val16;
440
441 if (c == NULL)
442 return -EINVAL;
443
444 for (j = 0; j < d->ports; j++) {
445 if ((c->untag | c->pvid) & (1 << j))
446 /* change default vlan tag */
447 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1), nr);
448 }
449
450 /* write config now */
451
452 if (robo.devid != ROBO_DEVICE_ID_5325) {
453 __u8 regoff = ((robo.devid == ROBO_DEVICE_ID_5395) ||
454 (robo.devid == ROBO_DEVICE_ID_53115)) ? 0x20 : 0;
455
456 robo_write32(ROBO_ARLIO_PAGE, 0x63 + regoff, (c->untag << 9) | c->port);
457 robo_write16(ROBO_ARLIO_PAGE, 0x61 + regoff, nr);
458 robo_write16(ROBO_ARLIO_PAGE, 0x60 + regoff, 1 << 7);
459 return 0;
460 }
461
462 val16 = (nr) /* vlan */ | (1 << 12) /* write */ | (1 << 13) /* enable */;
463 if (robo.is_5350) {
464 robo_write32(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE_5350,
465 (1 << 20) /* valid */ | (c->untag << 6) | c->port);
466 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350, val16);
467 } else {
468 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE,
469 (1 << 14) /* valid */ | (c->untag << 7) | c->port);
470 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS, val16);
471 }
472
473 return 0;
474 }
475
476 #define set_switch(state) \
477 robo_write16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE, (robo_read16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE) & ~2) | (state ? 2 : 0));
478
479 static int handle_enable_read(void *driver, char *buf, int nr)
480 {
481 return sprintf(buf, "%d\n", (((robo_read16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE) & 2) == 2) ? 1 : 0));
482 }
483
484 static int handle_enable_write(void *driver, char *buf, int nr)
485 {
486 set_switch(buf[0] == '1');
487
488 return 0;
489 }
490
491 static int handle_enable_vlan_read(void *driver, char *buf, int nr)
492 {
493 return sprintf(buf, "%d\n", (((robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL0) & (1 << 7)) == (1 << 7)) ? 1 : 0));
494 }
495
496 static int handle_enable_vlan_write(void *driver, char *buf, int nr)
497 {
498 int disable = ((buf[0] != '1') ? 1 : 0);
499
500 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL0, disable ? 0 :
501 (1 << 7) /* 802.1Q VLAN */ | (3 << 5) /* mac check and hash */);
502 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL1, disable ? 0 :
503 (robo.devid == ROBO_DEVICE_ID_5325 ? (1 << 1) :
504 0) | (1 << 2) | (1 << 3)); /* RSV multicast */
505
506 if (robo.devid != ROBO_DEVICE_ID_5325)
507 return 0;
508
509 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL4, disable ? 0 :
510 (1 << 6) /* drop invalid VID frames */);
511 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL5, disable ? 0 :
512 (1 << 3) /* drop miss V table frames */);
513
514 return 0;
515 }
516
517 static int handle_reset(void *driver, char *buf, int nr)
518 {
519 switch_driver *d = (switch_driver *) driver;
520 switch_vlan_config *c = switch_parse_vlan(d, buf);
521 int j;
522 __u16 val16;
523
524 if (c == NULL)
525 return -EINVAL;
526
527 /* disable switching */
528 set_switch(0);
529
530 /* reset vlans */
531 for (j = 0; j <= ((robo.is_5350) ? VLAN_ID_MAX5350 : VLAN_ID_MAX); j++) {
532 /* write config now */
533 val16 = (j) /* vlan */ | (1 << 12) /* write */ | (1 << 13) /* enable */;
534 if (robo.is_5350)
535 robo_write32(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE_5350, 0);
536 else
537 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE, 0);
538 robo_write16(ROBO_VLAN_PAGE, robo.is_5350 ? ROBO_VLAN_TABLE_ACCESS_5350 :
539 ROBO_VLAN_TABLE_ACCESS,
540 val16);
541 }
542
543 /* reset ports to a known good state */
544 for (j = 0; j < d->ports; j++) {
545 robo_write16(ROBO_CTRL_PAGE, robo.port[j], 0x0000);
546 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1), 0);
547 }
548
549 /* enable switching */
550 set_switch(1);
551
552 /* enable vlans */
553 handle_enable_vlan_write(driver, "1", 0);
554
555 return 0;
556 }
557
558 static int __init robo_init(void)
559 {
560 int notfound = 1;
561 char *device;
562
563 device = strdup("ethX");
564 for (device[3] = '0'; (device[3] <= '3') && notfound; device[3]++) {
565 if (! switch_device_registered (device))
566 notfound = robo_probe(device);
567 }
568 device[3]--;
569
570 if (notfound) {
571 kfree(device);
572 return -ENODEV;
573 } else {
574 static const switch_config cfg[] = {
575 {
576 .name = "enable",
577 .read = handle_enable_read,
578 .write = handle_enable_write
579 }, {
580 .name = "enable_vlan",
581 .read = handle_enable_vlan_read,
582 .write = handle_enable_vlan_write
583 }, {
584 .name = "reset",
585 .read = NULL,
586 .write = handle_reset
587 }, { NULL, },
588 };
589 static const switch_config vlan[] = {
590 {
591 .name = "ports",
592 .read = handle_vlan_port_read,
593 .write = handle_vlan_port_write
594 }, { NULL, },
595 };
596 switch_driver driver = {
597 .name = DRIVER_NAME,
598 .version = DRIVER_VERSION,
599 .interface = device,
600 .cpuport = 5,
601 .ports = 6,
602 .vlans = 16,
603 .driver_handlers = cfg,
604 .port_handlers = NULL,
605 .vlan_handlers = vlan,
606 };
607 if (robo.devid != ROBO_DEVICE_ID_5325) {
608 driver.ports = 9;
609 driver.cpuport = 8;
610 }
611
612 return switch_register_driver(&driver);
613 }
614 }
615
616 static void __exit robo_exit(void)
617 {
618 switch_unregister_driver(DRIVER_NAME);
619 kfree(robo.device);
620 }
621
622
623 MODULE_AUTHOR("Felix Fietkau <openwrt@nbd.name>");
624 MODULE_LICENSE("GPL");
625
626 module_init(robo_init);
627 module_exit(robo_exit);
This page took 0.079662 seconds and 5 git commands to generate.