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