switch: export name of device found via /proc
[openwrt/staging/chunkeey.git] / package / switch / src / switch-robo.c
index 9c78d8281f9e903f97b7e0b8089e42786b83602c..f7159729b8842191aaf8b93c4f4b9ae4234eeccd 100644 (file)
@@ -3,6 +3,7 @@
  *
  * Copyright (C) 2005 Felix Fietkau <nbd@nbd.name>
  * Copyright (C) 2008 Michael Buesch <mb@bu3sch.de>
+ * Copyright (C) 2013 Hauke Mehrtens <hauke@hauke-m.de>
  * Based on 'robocfg' by Oleg I. Vdovikin
  *
  * This program is free software; you can redistribute it and/or
@@ -40,7 +41,7 @@
 #endif
 
 #define DRIVER_NAME            "bcm53xx"
-#define DRIVER_VERSION         "0.02"
+#define DRIVER_VERSION         "0.03"
 #define PFX                    "roboswitch: "
 
 #define ROBO_PHY_ADDR          0x1E    /* robo switch phy address */
@@ -63,6 +64,7 @@
 #define  ROBO_DEVICE_ID_5397   0x97
 #define  ROBO_DEVICE_ID_5398   0x98
 #define  ROBO_DEVICE_ID_53115  0x3115
+#define  ROBO_DEVICE_ID_53125  0x3125
 
 /* Private et.o ioctls */
 #define SIOCGETCPHYRD           (SIOCDEVPRIVATE + 9)
@@ -74,11 +76,12 @@ struct robo_switch {
        u16 devid;                      /* ROBO_DEVICE_ID_53xx */
        bool is_5365;
        bool gmii;                      /* gigabit mii */
+       u8 corerev;
        int gpio_robo_reset;
        int gpio_lanports_enable;
        struct ifreq ifr;
        struct net_device *dev;
-       unsigned char port[6];
+       unsigned char port[9];
 };
 
 /* Currently we can only have one device in the system. */
@@ -237,10 +240,21 @@ static int robo_switch_enable(void)
                        return -EBUSY;
                }
 
+               /* No spanning tree for unmanaged mode */
                last_port = (robo.devid == ROBO_DEVICE_ID_5398) ?
-                               ROBO_PORT6_CTRL : ROBO_PORT3_CTRL;
-               for (i = ROBO_PORT0_CTRL; i < last_port + 1; i++)
+                               ROBO_PORT7_CTRL : ROBO_PORT4_CTRL;
+               for (i = ROBO_PORT0_CTRL; i <= last_port; i++)
                        robo_write16(ROBO_CTRL_PAGE, i, 0);
+
+               /* No spanning tree on IMP port too */
+               robo_write16(ROBO_CTRL_PAGE, ROBO_IM_PORT_CTRL, 0);
+       }
+
+       if (robo.devid == ROBO_DEVICE_ID_53125) {
+               /* Make IM port status link by default */
+               val = robo_read16(ROBO_CTRL_PAGE, ROBO_PORT_OVERRIDE_CTRL) | 0xb1;
+               robo_write16(ROBO_CTRL_PAGE, ROBO_PORT_OVERRIDE_CTRL, val);
+               // TODO: init EEE feature
        }
 
 #ifdef CONFIG_BCM47XX
@@ -308,9 +322,6 @@ static int robo_probe(char *devname)
        }
 
        robo.device = devname;
-       for (i = 0; i < 5; i++)
-               robo.port[i] = i;
-       robo.port[5] = 8;
 
        /* try access using MII ioctls - get phy address */
        err = do_ioctl(SIOCGMIIPHY);
@@ -386,6 +397,14 @@ static int robo_probe(char *devname)
                robo.is_5365 = false;
 
        robo.gmii = robo_gmii();
+       if (robo.devid == ROBO_DEVICE_ID_5325) {
+               for (i = 0; i < 5; i++)
+                       robo.port[i] = i;
+       } else {
+               for (i = 0; i < 8; i++)
+                       robo.port[i] = i;
+       }
+       robo.port[i] = ROBO_IM_PORT_CTRL;
 
        robo_switch_reset();
        err = robo_switch_enable();
@@ -410,8 +429,7 @@ err_done:
        return err;
 }
 
-
-static int handle_vlan_port_read(void *driver, char *buf, int nr)
+static int handle_vlan_port_read_old(switch_driver *d, char *buf, int nr)
 {
        __u16 val16;
        int len = 0;
@@ -419,17 +437,17 @@ static int handle_vlan_port_read(void *driver, char *buf, int nr)
 
        val16 = (nr) /* vlan */ | (0 << 12) /* read */ | (1 << 13) /* enable */;
 
-       if (!robo.is_5365) {
-               u32 val32;
-               robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS, val16);
+       if (robo.is_5365) {
+               robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5365, val16);
                /* actual read */
-               val32 = robo_read32(ROBO_VLAN_PAGE, ROBO_VLAN_READ);
-               if ((val32 & (1 << 20)) /* valid */) {
-                       for (j = 0; j < 6; j++) {
-                               if (val32 & (1 << j)) {
+               val16 = robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_READ);
+               if ((val16 & (1 << 14)) /* valid */) {
+                       for (j = 0; j < d->ports; j++) {
+                               if (val16 & (1 << j)) {
                                        len += sprintf(buf + len, "%d", j);
-                                       if (val32 & (1 << (j + 6))) {
-                                               if (j == 5) buf[len++] = 'u';
+                                       if (val16 & (1 << (j + 7))) {
+                                               if (j == d->cpuport)
+                                                       buf[len++] = 'u';
                                        } else {
                                                buf[len++] = 't';
                                                if (robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1)) == nr)
@@ -441,15 +459,17 @@ static int handle_vlan_port_read(void *driver, char *buf, int nr)
                        len += sprintf(buf + len, "\n");
                }
        } else {
-               robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5365, val16);
+               u32 val32;
+               robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS, val16);
                /* actual read */
-               val16 = robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_READ);
-               if ((val16 & (1 << 14)) /* valid */) {
-                       for (j = 0; j < 6; j++) {
-                               if (val16 & (1 << j)) {
+               val32 = robo_read32(ROBO_VLAN_PAGE, ROBO_VLAN_READ);
+               if ((val32 & (1 << 20)) /* valid */) {
+                       for (j = 0; j < d->ports; j++) {
+                               if (val32 & (1 << j)) {
                                        len += sprintf(buf + len, "%d", j);
-                                       if (val16 & (1 << (j + 7))) {
-                                               if (j == 5) buf[len++] = 'u';
+                                       if (val32 & (1 << (j + d->ports))) {
+                                               if (j == d->cpuport)
+                                                       buf[len++] = 'u';
                                        } else {
                                                buf[len++] = 't';
                                                if (robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1)) == nr)
@@ -467,45 +487,122 @@ static int handle_vlan_port_read(void *driver, char *buf, int nr)
        return len;
 }
 
-static int handle_vlan_port_write(void *driver, char *buf, int nr)
+static int handle_vlan_port_read_new(switch_driver *d, char *buf, int nr)
 {
-       switch_driver *d = (switch_driver *) driver;
-       switch_vlan_config *c = switch_parse_vlan(d, buf);
+       __u8 vtbl_entry, vtbl_index, vtbl_access;
+       __u32 val32;
+       int len = 0;
        int j;
-       __u16 val16;
 
-       if (c == NULL)
-               return -EINVAL;
+       if ((robo.devid == ROBO_DEVICE_ID_5395) ||
+           (robo.devid == ROBO_DEVICE_ID_53115) ||
+           (robo.devid == ROBO_DEVICE_ID_53125)) {
+               vtbl_access = ROBO_VTBL_ACCESS_5395;
+               vtbl_index = ROBO_VTBL_INDX_5395;
+               vtbl_entry = ROBO_VTBL_ENTRY_5395;
+       } else {
+               vtbl_access = ROBO_VTBL_ACCESS;
+               vtbl_index = ROBO_VTBL_INDX;
+               vtbl_entry = ROBO_VTBL_ENTRY;
+       }
 
+       robo_write16(ROBO_ARLIO_PAGE, vtbl_index, nr);
+       robo_write16(ROBO_ARLIO_PAGE, vtbl_access, (1 << 7) | (1 << 0));
+       val32 = robo_read32(ROBO_ARLIO_PAGE, vtbl_entry);
        for (j = 0; j < d->ports; j++) {
-               if ((c->untag | c->pvid) & (1 << j))
-                       /* change default vlan tag */
-                       robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1), nr);
+               if (val32 & (1 << j)) {
+                       len += sprintf(buf + len, "%d", j);
+                       if (val32 & (1 << (j + d->ports))) {
+                               if (j == d->cpuport)
+                                       buf[len++] = 'u';
+                       } else {
+                               buf[len++] = 't';
+                               if (robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1)) == nr)
+                                       buf[len++] = '*';
+                       }
+                       buf[len++] = '\t';
+               }
        }
+       len += sprintf(buf + len, "\n");
+       buf[len] = '\0';
+       return len;
+}
 
-       /* write config now */
+static int handle_vlan_port_read(void *driver, char *buf, int nr)
+{
+       switch_driver *d = (switch_driver *) driver;
 
-       if (robo.devid != ROBO_DEVICE_ID_5325) {
-               __u8 regoff = ((robo.devid == ROBO_DEVICE_ID_5395) ||
-                       (robo.devid == ROBO_DEVICE_ID_53115)) ? 0x20 : 0;
+       if (robo.devid != ROBO_DEVICE_ID_5325)
+               return handle_vlan_port_read_new(d, buf, nr);
+       else
+               return handle_vlan_port_read_old(d, buf, nr);
+}
 
-               robo_write32(ROBO_ARLIO_PAGE, 0x63 + regoff, (c->untag << 9) | c->port);
-               robo_write16(ROBO_ARLIO_PAGE, 0x61 + regoff, nr);
-               robo_write16(ROBO_ARLIO_PAGE, 0x60 + regoff, 1 << 7);
-               kfree(c);
-               return 0;
-       }
+static void handle_vlan_port_write_old(switch_driver *d, switch_vlan_config *c, int nr)
+{
+       __u16 val16;
+       __u32 val32;
+       __u32 untag = ((c->untag  & ~(1 << d->cpuport)) << d->ports);
 
+       /* write config now */
        val16 = (nr) /* vlan */ | (1 << 12) /* write */ | (1 << 13) /* enable */;
        if (robo.is_5365) {
-               robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE_5365,
-                       (1 << 14)  /* valid */ | (c->untag << 7) | c->port);
+               robo_write32(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE_5365,
+                       (1 << 14)  /* valid */ | (untag << 1 ) | c->port);
                robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5365, val16);
        } else {
-               robo_write32(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE,
-                       (1 << 20) /* valid */ | (c->untag << 6) | c->port);
+               if (robo.corerev < 3)
+                       val32 = (1 << 20) | ((nr >> 4) << 12) | untag | c->port;
+               else
+                       val32 = (1 << 24) | (nr << 12) | untag | c->port;
+               robo_write32(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE, val32);
                robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS, val16);
        }
+}
+
+static void handle_vlan_port_write_new(switch_driver *d, switch_vlan_config *c, int nr)
+{
+       __u8 vtbl_entry, vtbl_index, vtbl_access;
+       __u32 untag = ((c->untag  & ~(1 << d->cpuport)) << d->ports);
+
+       /* write config now */
+       if ((robo.devid == ROBO_DEVICE_ID_5395) ||
+           (robo.devid == ROBO_DEVICE_ID_53115) ||
+           (robo.devid == ROBO_DEVICE_ID_53125)) {
+               vtbl_access = ROBO_VTBL_ACCESS_5395;
+               vtbl_index = ROBO_VTBL_INDX_5395;
+               vtbl_entry = ROBO_VTBL_ENTRY_5395;
+       } else {
+               vtbl_access = ROBO_VTBL_ACCESS;
+               vtbl_index = ROBO_VTBL_INDX;
+               vtbl_entry = ROBO_VTBL_ENTRY;
+       }
+
+       robo_write32(ROBO_ARLIO_PAGE, vtbl_entry, untag | c->port);
+       robo_write16(ROBO_ARLIO_PAGE, vtbl_index, nr);
+       robo_write16(ROBO_ARLIO_PAGE, vtbl_access, 1 << 7);
+}
+
+static int handle_vlan_port_write(void *driver, char *buf, int nr)
+{
+       switch_driver *d = (switch_driver *)driver;
+       switch_vlan_config *c = switch_parse_vlan(d, buf);
+       int j;
+
+       if (c == NULL)
+               return -EINVAL;
+
+       for (j = 0; j < d->ports; j++) {
+               if ((c->untag | c->pvid) & (1 << j)) {
+                       /* change default vlan tag */
+                       robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1), nr);
+               }
+       }
+
+       if (robo.devid != ROBO_DEVICE_ID_5325)
+               handle_vlan_port_write_new(d, c, nr);
+       else
+               handle_vlan_port_write_old(d, c, nr);
 
        kfree(c);
        return 0;
@@ -607,12 +704,16 @@ static int handle_enable_vlan_read(void *driver, char *buf, int nr)
 
 static int handle_enable_vlan_write(void *driver, char *buf, int nr)
 {
+       __u16 val16;
        int disable = ((buf[0] != '1') ? 1 : 0);
 
+       val16 = robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL0);
        robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL0, disable ? 0 :
-               (1 << 7) /* 802.1Q VLAN */ | (3 << 5) /* mac check and hash */);
+               val16 | (1 << 7) /* 802.1Q VLAN */ | (3 << 5) /* mac check and hash */);
+
+       val16 = robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL1);
        robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL1, disable ? 0 :
-               (robo.devid == ROBO_DEVICE_ID_5325 ? (1 << 1) :
+               val16 | (robo.devid == ROBO_DEVICE_ID_5325 ? (1 << 1) :
                0) | (1 << 2) | (1 << 3)); /* RSV multicast */
 
        if (robo.devid != ROBO_DEVICE_ID_5325)
@@ -626,15 +727,11 @@ static int handle_enable_vlan_write(void *driver, char *buf, int nr)
        return 0;
 }
 
-static int handle_reset(void *driver, char *buf, int nr)
+static void handle_reset_old(switch_driver *d, char *buf, int nr)
 {
-       switch_driver *d = (switch_driver *) driver;
        int j;
        __u16 val16;
 
-       /* disable switching */
-       set_switch(0);
-
        /* reset vlans */
        for (j = 0; j <= ((robo.is_5365) ? VLAN_ID_MAX_5365 : VLAN_ID_MAX); j++) {
                /* write config now */
@@ -647,6 +744,45 @@ static int handle_reset(void *driver, char *buf, int nr)
                                                            ROBO_VLAN_TABLE_ACCESS,
                             val16);
        }
+}
+
+static void handle_reset_new(switch_driver *d, char *buf, int nr)
+{
+       int j;
+       __u8 vtbl_entry, vtbl_index, vtbl_access;
+
+       if ((robo.devid == ROBO_DEVICE_ID_5395) ||
+           (robo.devid == ROBO_DEVICE_ID_53115) ||
+           (robo.devid == ROBO_DEVICE_ID_53125)) {
+               vtbl_access = ROBO_VTBL_ACCESS_5395;
+               vtbl_index = ROBO_VTBL_INDX_5395;
+               vtbl_entry = ROBO_VTBL_ENTRY_5395;
+       } else {
+               vtbl_access = ROBO_VTBL_ACCESS;
+               vtbl_index = ROBO_VTBL_INDX;
+               vtbl_entry = ROBO_VTBL_ENTRY;
+       }
+
+       for (j = 0; j <= VLAN_ID_MAX; j++) {
+               /* write config now */
+               robo_write32(ROBO_ARLIO_PAGE, vtbl_entry, 0);
+               robo_write16(ROBO_ARLIO_PAGE, vtbl_index, j);
+               robo_write16(ROBO_ARLIO_PAGE, vtbl_access, 1 << 7);
+       }
+}
+
+static int handle_reset(void *driver, char *buf, int nr)
+{
+       int j;
+       switch_driver *d = (switch_driver *) driver;
+
+       /* disable switching */
+       set_switch(0);
+
+       if (robo.devid != ROBO_DEVICE_ID_5325)
+               handle_reset_new(d, buf, nr);
+       else
+               handle_reset_old(d, buf, nr);
 
        /* reset ports to a known good state */
        for (j = 0; j < d->ports; j++) {
@@ -727,6 +863,10 @@ static int __init robo_init(void)
                        driver.ports = 9;
                        driver.cpuport = 8;
                }
+               if (robo.is_5365)
+                       snprintf(driver.dev_name, SWITCH_NAME_BUFSZ, "BCM5365");
+               else
+                       snprintf(driver.dev_name, SWITCH_NAME_BUFSZ, "BCM5%s%x", robo.devid & 0xff00 ? "" : "3", robo.devid);
 
                return switch_register_driver(&driver);
        }