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