2 * Broadcom BCM5325E/536x switch configuration module
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
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.
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.
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
24 #include <linux/module.h>
25 #include <linux/init.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 <linux/gpio.h>
33 #include <asm/uaccess.h>
35 #include "switch-core.h"
39 #include <bcm47xx_nvram.h>
42 #define DRIVER_NAME "bcm53xx"
43 #define DRIVER_VERSION "0.02"
44 #define PFX "roboswitch: "
46 #define ROBO_PHY_ADDR 0x1E /* robo switch phy address */
47 #define ROBO_PHY_ADDR_TG3 0x01 /* Tigon3 PHY address */
48 #define ROBO_PHY_ADDR_BCM63XX 0x00 /* BCM63XX PHY address */
51 #define REG_MII_PAGE 0x10 /* MII Page register */
52 #define REG_MII_ADDR 0x11 /* MII Address register */
53 #define REG_MII_DATA0 0x18 /* MII Data register 0 */
55 #define REG_MII_PAGE_ENABLE 1
56 #define REG_MII_ADDR_WRITE 1
57 #define REG_MII_ADDR_READ 2
59 /* Robo device ID register (in ROBO_MGMT_PAGE) */
60 #define ROBO_DEVICE_ID 0x30
61 #define ROBO_DEVICE_ID_5325 0x25 /* Faked */
62 #define ROBO_DEVICE_ID_5395 0x95
63 #define ROBO_DEVICE_ID_5397 0x97
64 #define ROBO_DEVICE_ID_5398 0x98
65 #define ROBO_DEVICE_ID_53115 0x3115
67 /* Private et.o ioctls */
68 #define SIOCGETCPHYRD (SIOCDEVPRIVATE + 9)
69 #define SIOCSETCPHYWR (SIOCDEVPRIVATE + 10)
71 /* Data structure for a Roboswitch device. */
73 char *device; /* The device name string (ethX) */
74 u16 devid; /* ROBO_DEVICE_ID_53xx */
76 bool gmii; /* gigabit mii */
78 int gpio_lanports_enable;
80 struct net_device *dev;
81 unsigned char port[9];
84 /* Currently we can only have one device in the system. */
85 static struct robo_switch robo;
88 static int do_ioctl(int cmd)
90 mm_segment_t old_fs = get_fs();
94 ret = robo.dev->netdev_ops->ndo_do_ioctl(robo.dev, &robo.ifr, cmd);
100 static u16 mdio_read(__u16 phy_id, __u8 reg)
102 struct mii_ioctl_data *mii = if_mii(&robo.ifr);
105 mii->phy_id = phy_id;
108 err = do_ioctl(SIOCGMIIREG);
110 printk(KERN_ERR PFX "failed to read mdio reg %i with err %i.\n", reg, err);
118 static void mdio_write(__u16 phy_id, __u8 reg, __u16 val)
120 struct mii_ioctl_data *mii = if_mii(&robo.ifr);
123 mii->phy_id = phy_id;
127 err = do_ioctl(SIOCSMIIREG);
129 printk(KERN_ERR PFX "failed to write mdio reg: %i with err %i.\n", reg, err);
134 static int robo_reg(__u8 page, __u8 reg, __u8 op)
138 /* set page number */
139 mdio_write(ROBO_PHY_ADDR, REG_MII_PAGE,
140 (page << 8) | REG_MII_PAGE_ENABLE);
142 /* set register address */
143 mdio_write(ROBO_PHY_ADDR, REG_MII_ADDR,
146 /* check if operation completed */
148 if ((mdio_read(ROBO_PHY_ADDR, REG_MII_ADDR) & 3) == 0)
152 printk(KERN_ERR PFX "timeout in robo_reg on page %i and reg %i with op %i.\n", page, reg, op);
158 static void robo_read(__u8 page, __u8 reg, __u16 *val, int count)
162 robo_reg(page, reg, REG_MII_ADDR_READ);
164 for (i = 0; i < count; i++)
165 val[i] = mdio_read(ROBO_PHY_ADDR, REG_MII_DATA0 + i);
169 static __u16 robo_read16(__u8 page, __u8 reg)
171 robo_reg(page, reg, REG_MII_ADDR_READ);
173 return mdio_read(ROBO_PHY_ADDR, REG_MII_DATA0);
176 static __u32 robo_read32(__u8 page, __u8 reg)
178 robo_reg(page, reg, REG_MII_ADDR_READ);
180 return mdio_read(ROBO_PHY_ADDR, REG_MII_DATA0) |
181 (mdio_read(ROBO_PHY_ADDR, REG_MII_DATA0 + 1) << 16);
184 static void robo_write16(__u8 page, __u8 reg, __u16 val16)
187 mdio_write(ROBO_PHY_ADDR, REG_MII_DATA0, val16);
189 robo_reg(page, reg, REG_MII_ADDR_WRITE);
192 static void robo_write32(__u8 page, __u8 reg, __u32 val32)
195 mdio_write(ROBO_PHY_ADDR, REG_MII_DATA0, val32 & 0xFFFF);
196 mdio_write(ROBO_PHY_ADDR, REG_MII_DATA0 + 1, val32 >> 16);
198 robo_reg(page, reg, REG_MII_ADDR_WRITE);
201 /* checks that attached switch is 5365 */
202 static bool robo_bcm5365(void)
204 /* set vlan access id to 15 and read it back */
206 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS, val16);
208 /* 5365 will refuse this as it does not have this reg */
209 return robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS) != val16;
212 static bool robo_gmii(void)
214 if (mdio_read(0, ROBO_MII_STAT) & 0x0100)
215 return ((mdio_read(0, 0x0f) & 0xf000) != 0);
219 static int robo_switch_enable(void)
221 unsigned int i, last_port;
223 #ifdef CONFIG_BCM47XX
227 val = robo_read16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE);
228 if (!(val & (1 << 1))) {
231 /* With forwarding */
233 robo_write16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE, val);
234 val = robo_read16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE);
235 if (!(val & (1 << 1))) {
236 printk(KERN_ERR PFX "Failed to enable switch\n");
240 last_port = (robo.devid == ROBO_DEVICE_ID_5398) ?
241 ROBO_PORT6_CTRL : ROBO_PORT3_CTRL;
242 for (i = ROBO_PORT0_CTRL; i < last_port + 1; i++)
243 robo_write16(ROBO_CTRL_PAGE, i, 0);
246 #ifdef CONFIG_BCM47XX
247 /* WAN port LED, except for Netgear WGT634U */
248 if (bcm47xx_nvram_getenv("nvram_type", buf, sizeof(buf)) >= 0) {
249 if (strcmp(buf, "cfe") != 0)
250 robo_write16(ROBO_CTRL_PAGE, 0x16, 0x1F);
256 static void robo_switch_reset(void)
258 if ((robo.devid == ROBO_DEVICE_ID_5395) ||
259 (robo.devid == ROBO_DEVICE_ID_5397) ||
260 (robo.devid == ROBO_DEVICE_ID_5398)) {
261 /* Trigger a software reset. */
262 robo_write16(ROBO_CTRL_PAGE, 0x79, 0x83);
264 robo_write16(ROBO_CTRL_PAGE, 0x79, 0);
268 #ifdef CONFIG_BCM47XX
269 static int get_gpio_pin(const char *name)
275 for (i = 0; i < 16; i++) {
276 err = snprintf(nvram_var, sizeof(nvram_var), "gpio%i", i);
279 err = bcm47xx_nvram_getenv(nvram_var, buf, sizeof(buf));
282 if (!strcmp(name, buf))
289 static int robo_probe(char *devname)
294 struct mii_ioctl_data *mii;
296 printk(KERN_INFO PFX "Probing device '%s'\n", devname);
297 strcpy(robo.ifr.ifr_name, devname);
299 if ((robo.dev = dev_get_by_name(&init_net, devname)) == NULL) {
300 printk(KERN_ERR PFX "No such device\n");
304 if (!robo.dev->netdev_ops || !robo.dev->netdev_ops->ndo_do_ioctl) {
305 printk(KERN_ERR PFX "ndo_do_ioctl not implemented in ethernet driver\n");
310 robo.device = devname;
312 /* try access using MII ioctls - get phy address */
313 err = do_ioctl(SIOCGMIIPHY);
315 printk(KERN_ERR PFX "error (%i) while accessing MII phy registers with ioctls\n", err);
319 /* got phy address check for robo address */
320 mii = if_mii(&robo.ifr);
321 if ((mii->phy_id != ROBO_PHY_ADDR) &&
322 (mii->phy_id != ROBO_PHY_ADDR_BCM63XX) &&
323 (mii->phy_id != ROBO_PHY_ADDR_TG3)) {
324 printk(KERN_ERR PFX "Invalid phy address (%d)\n", mii->phy_id);
329 #ifdef CONFIG_BCM47XX
330 robo.gpio_lanports_enable = get_gpio_pin("lanports_enable");
331 if (robo.gpio_lanports_enable >= 0) {
332 err = gpio_request(robo.gpio_lanports_enable, "lanports_enable");
334 printk(KERN_ERR PFX "error (%i) requesting lanports_enable gpio (%i)\n",
335 err, robo.gpio_lanports_enable);
338 gpio_direction_output(robo.gpio_lanports_enable, 1);
342 robo.gpio_robo_reset = get_gpio_pin("robo_reset");
343 if (robo.gpio_robo_reset >= 0) {
344 err = gpio_request(robo.gpio_robo_reset, "robo_reset");
346 printk(KERN_ERR PFX "error (%i) requesting robo_reset gpio (%i)\n",
347 err, robo.gpio_robo_reset);
350 gpio_set_value(robo.gpio_robo_reset, 0);
351 gpio_direction_output(robo.gpio_robo_reset, 1);
352 gpio_set_value(robo.gpio_robo_reset, 0);
355 gpio_set_value(robo.gpio_robo_reset, 1);
358 // TODO: reset the internal robo switch
362 phyid = mdio_read(ROBO_PHY_ADDR, 0x2) |
363 (mdio_read(ROBO_PHY_ADDR, 0x3) << 16);
365 if (phyid == 0xffffffff || phyid == 0x55210022) {
366 printk(KERN_ERR PFX "No Robo switch in managed mode found, phy_id = 0x%08x\n", phyid);
368 goto err_gpio_lanports;
371 /* Get the device ID */
372 for (i = 0; i < 10; i++) {
373 robo.devid = robo_read16(ROBO_MGMT_PAGE, ROBO_DEVICE_ID);
379 robo.devid = ROBO_DEVICE_ID_5325; /* Fake it */
380 if (robo.devid == ROBO_DEVICE_ID_5325)
381 robo.is_5365 = robo_bcm5365();
383 robo.is_5365 = false;
385 robo.gmii = robo_gmii();
386 if (robo.devid == ROBO_DEVICE_ID_5325) {
387 for (i = 0; i < 5; i++)
390 for (i = 0; i < 8; i++)
393 robo.port[i] = ROBO_IM_PORT_CTRL;
396 err = robo_switch_enable();
398 goto err_gpio_lanports;
400 printk(KERN_INFO PFX "found a 5%s%x!%s at %s\n", robo.devid & 0xff00 ? "" : "3", robo.devid,
401 robo.is_5365 ? " It's a BCM5365." : "", devname);
406 if (robo.gpio_lanports_enable >= 0)
407 gpio_free(robo.gpio_lanports_enable);
409 if (robo.gpio_robo_reset >= 0)
410 gpio_free(robo.gpio_robo_reset);
419 static int handle_vlan_port_read(void *driver, char *buf, int nr)
425 val16 = (nr) /* vlan */ | (0 << 12) /* read */ | (1 << 13) /* enable */;
428 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5365, val16);
430 val16 = robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_READ);
431 if ((val16 & (1 << 14)) /* valid */) {
432 for (j = 0; j < 6; j++) {
433 if (val16 & (1 << j)) {
434 len += sprintf(buf + len, "%d", j);
435 if (val16 & (1 << (j + 7))) {
440 if (robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1)) == nr)
446 len += sprintf(buf + len, "\n");
450 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS, val16);
452 val32 = robo_read32(ROBO_VLAN_PAGE, ROBO_VLAN_READ);
453 if ((val32 & (1 << 20)) /* valid */) {
454 for (j = 0; j < 6; j++) {
455 if (val32 & (1 << j)) {
456 len += sprintf(buf + len, "%d", j);
457 if (val32 & (1 << (j + 6))) {
462 if (robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1)) == nr)
468 len += sprintf(buf + len, "\n");
477 static int handle_vlan_port_write(void *driver, char *buf, int nr)
479 switch_driver *d = (switch_driver *) driver;
480 switch_vlan_config *c = switch_parse_vlan(d, buf);
487 for (j = 0; j < d->ports; j++) {
488 if ((c->untag | c->pvid) & (1 << j))
489 /* change default vlan tag */
490 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1), nr);
493 /* write config now */
495 if (robo.devid != ROBO_DEVICE_ID_5325) {
496 __u8 regoff = ((robo.devid == ROBO_DEVICE_ID_5395) ||
497 (robo.devid == ROBO_DEVICE_ID_53115)) ? 0x20 : 0;
499 robo_write32(ROBO_ARLIO_PAGE, 0x63 + regoff, (c->untag << 9) | c->port);
500 robo_write16(ROBO_ARLIO_PAGE, 0x61 + regoff, nr);
501 robo_write16(ROBO_ARLIO_PAGE, 0x60 + regoff, 1 << 7);
506 val16 = (nr) /* vlan */ | (1 << 12) /* write */ | (1 << 13) /* enable */;
508 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE_5365,
509 (1 << 14) /* valid */ | (c->untag << 7) | c->port);
510 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5365, val16);
512 robo_write32(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE,
513 (1 << 20) /* valid */ | (c->untag << 6) | c->port);
514 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS, val16);
521 #define set_switch(state) \
522 robo_write16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE, (robo_read16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE) & ~2) | (state ? 2 : 0));
524 static int handle_enable_read(void *driver, char *buf, int nr)
526 return sprintf(buf, "%d\n", (((robo_read16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE) & 2) == 2) ? 1 : 0));
529 static int handle_enable_write(void *driver, char *buf, int nr)
531 set_switch(buf[0] == '1');
536 static int handle_port_enable_read(void *driver, char *buf, int nr)
538 return sprintf(buf, "%d\n", ((robo_read16(ROBO_CTRL_PAGE, robo.port[nr]) & 3) == 3 ? 0 : 1));
541 static int handle_port_enable_write(void *driver, char *buf, int nr)
546 val16 = 3; /* disabled */
547 else if (buf[0] == '1')
548 val16 = 0; /* enabled */
552 robo_write16(ROBO_CTRL_PAGE, robo.port[nr],
553 (robo_read16(ROBO_CTRL_PAGE, robo.port[nr]) & ~3) | val16);
558 static int handle_port_media_read(void *driver, char *buf, int nr)
560 u16 bmcr = mdio_read(robo.port[nr], MII_BMCR);
563 if (bmcr & BMCR_ANENABLE)
564 media = SWITCH_MEDIA_AUTO;
566 if (bmcr & BMCR_SPEED1000)
567 media = SWITCH_MEDIA_1000;
568 else if (bmcr & BMCR_SPEED100)
569 media = SWITCH_MEDIA_100;
573 if (bmcr & BMCR_FULLDPLX)
574 media |= SWITCH_MEDIA_FD;
577 len = switch_print_media(buf, media);
578 return len + sprintf(buf + len, "\n");
581 static int handle_port_media_write(void *driver, char *buf, int nr)
583 int media = switch_parse_media(buf);
586 if (media & SWITCH_MEDIA_AUTO)
587 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
589 if (media & SWITCH_MEDIA_1000) {
592 bmcr = BMCR_SPEED1000;
594 else if (media & SWITCH_MEDIA_100)
595 bmcr = BMCR_SPEED100;
599 if (media & SWITCH_MEDIA_FD)
600 bmcr |= BMCR_FULLDPLX;
603 bmcr_mask = ~(BMCR_SPEED1000 | BMCR_SPEED100 | BMCR_FULLDPLX | BMCR_ANENABLE | BMCR_ANRESTART);
604 mdio_write(robo.port[nr], MII_BMCR,
605 (mdio_read(robo.port[nr], MII_BMCR) & bmcr_mask) | bmcr);
610 static int handle_enable_vlan_read(void *driver, char *buf, int nr)
612 return sprintf(buf, "%d\n", (((robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL0) & (1 << 7)) == (1 << 7)) ? 1 : 0));
615 static int handle_enable_vlan_write(void *driver, char *buf, int nr)
617 int disable = ((buf[0] != '1') ? 1 : 0);
619 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL0, disable ? 0 :
620 (1 << 7) /* 802.1Q VLAN */ | (3 << 5) /* mac check and hash */);
621 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL1, disable ? 0 :
622 (robo.devid == ROBO_DEVICE_ID_5325 ? (1 << 1) :
623 0) | (1 << 2) | (1 << 3)); /* RSV multicast */
625 if (robo.devid != ROBO_DEVICE_ID_5325)
628 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL4, disable ? 0 :
629 (1 << 6) /* drop invalid VID frames */);
630 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL5, disable ? 0 :
631 (1 << 3) /* drop miss V table frames */);
636 static int handle_reset(void *driver, char *buf, int nr)
638 switch_driver *d = (switch_driver *) driver;
642 /* disable switching */
646 for (j = 0; j <= ((robo.is_5365) ? VLAN_ID_MAX_5365 : VLAN_ID_MAX); j++) {
647 /* write config now */
648 val16 = (j) /* vlan */ | (1 << 12) /* write */ | (1 << 13) /* enable */;
650 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE_5365, 0);
652 robo_write32(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE, 0);
653 robo_write16(ROBO_VLAN_PAGE, robo.is_5365 ? ROBO_VLAN_TABLE_ACCESS_5365 :
654 ROBO_VLAN_TABLE_ACCESS,
658 /* reset ports to a known good state */
659 for (j = 0; j < d->ports; j++) {
660 robo_write16(ROBO_CTRL_PAGE, robo.port[j], 0x0000);
661 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1), 0);
664 /* enable switching */
668 handle_enable_vlan_write(driver, "1", 0);
673 static int __init robo_init(void)
678 device = strdup("ethX");
679 for (device[3] = '0'; (device[3] <= '3') && notfound; device[3]++) {
680 if (! switch_device_registered (device))
681 notfound = robo_probe(device);
689 static const switch_config cfg[] = {
692 .read = handle_enable_read,
693 .write = handle_enable_write
695 .name = "enable_vlan",
696 .read = handle_enable_vlan_read,
697 .write = handle_enable_vlan_write
701 .write = handle_reset
704 static const switch_config port[] = {
707 .read = handle_port_enable_read,
708 .write = handle_port_enable_write
711 .read = handle_port_media_read,
712 .write = handle_port_media_write
715 static const switch_config vlan[] = {
718 .read = handle_vlan_port_read,
719 .write = handle_vlan_port_write
722 switch_driver driver = {
724 .version = DRIVER_VERSION,
729 .driver_handlers = cfg,
730 .port_handlers = port,
731 .vlan_handlers = vlan,
733 if (robo.devid != ROBO_DEVICE_ID_5325) {
738 return switch_register_driver(&driver);
742 static void __exit robo_exit(void)
744 switch_unregister_driver(DRIVER_NAME);
747 if (robo.gpio_robo_reset >= 0)
748 gpio_free(robo.gpio_robo_reset);
749 if (robo.gpio_lanports_enable >= 0)
750 gpio_free(robo.gpio_lanports_enable);
755 MODULE_AUTHOR("Felix Fietkau <openwrt@nbd.name>");
756 MODULE_LICENSE("GPL");
758 module_init(robo_init);
759 module_exit(robo_exit);