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