switch: improve robo_switch_enable()
[librecmc/librecmc.git] / package / switch / src / switch-robo.c
index e33c30ec4d76093f706601a66acd7e6928f5f674..41f0c1826dbd6b01bc481b77b51c063880788f0a 100644 (file)
@@ -29,6 +29,7 @@
 #include <linux/ethtool.h>
 #include <linux/mii.h>
 #include <linux/delay.h>
+#include <linux/gpio.h>
 #include <asm/uaccess.h>
 
 #include "switch-core.h"
 struct robo_switch {
        char *device;                   /* The device name string (ethX) */
        u16 devid;                      /* ROBO_DEVICE_ID_53xx */
-       bool is_5350;
-       u8 gmii;                        /* gigabit mii */
+       bool is_5365;
+       bool gmii;                      /* gigabit mii */
+       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. */
@@ -195,34 +198,22 @@ static void robo_write32(__u8 page, __u8 reg, __u32 val32)
        robo_reg(page, reg, REG_MII_ADDR_WRITE);
 }
 
-/* checks that attached switch is 5325/5352/5354/5356/5357/53115 */
-static int robo_vlan5350(__u32 phyid)
+/* checks that attached switch is 5365 */
+static bool robo_bcm5365(void)
 {
        /* set vlan access id to 15 and read it back */
        __u16 val16 = 15;
-       robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350, val16);
+       robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS, val16);
 
        /* 5365 will refuse this as it does not have this reg */
-       if (robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350) != val16)
-               return 0;
-       /* gigabit ? */
+       return robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS) != val16;
+}
+
+static bool robo_gmii(void)
+{
        if (mdio_read(0, ROBO_MII_STAT) & 0x0100)
-               robo.gmii = ((mdio_read(0, 0x0f) & 0xf000) != 0);
-       /* 53115 ? */
-       if (robo.gmii && robo_read32(ROBO_STAT_PAGE, ROBO_LSA_IM_PORT) != 0) {
-               robo_write16(ROBO_ARLIO_PAGE, ROBO_VTBL_INDX_5395, val16);
-               robo_write16(ROBO_ARLIO_PAGE, ROBO_VTBL_ACCESS_5395,
-                                        (1 << 7) /* start */ | 1 /* read */);
-               if (robo_read16(ROBO_ARLIO_PAGE, ROBO_VTBL_ACCESS_5395) == 1 &&
-                   robo_read16(ROBO_ARLIO_PAGE, ROBO_VTBL_INDX_5395) == val16)
-                       return 4;
-       }
-       /* dirty trick for 5356/5357 */
-       if ((phyid & 0xfff0ffff ) == 0x5da00362 ||
-           (phyid & 0xfff0ffff ) == 0x5e000362)
-               return 3;
-       /* 5325/5352/5354*/
-       return 1;
+               return ((mdio_read(0, 0x0f) & 0xf000) != 0);
+       return false;
 }
 
 static int robo_switch_enable(void)
@@ -246,10 +237,14 @@ 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);
        }
 
 #ifdef CONFIG_BCM47XX
@@ -274,6 +269,27 @@ static void robo_switch_reset(void)
        }
 }
 
+#ifdef CONFIG_BCM47XX
+static int get_gpio_pin(const char *name)
+{
+       int i, err;
+       char nvram_var[10];
+       char buf[30];
+
+       for (i = 0; i < 16; i++) {
+               err = snprintf(nvram_var, sizeof(nvram_var), "gpio%i", i);
+               if (err <= 0)
+                       continue;
+               err = bcm47xx_nvram_getenv(nvram_var, buf, sizeof(buf));
+               if (err <= 0)
+                       continue;
+               if (!strcmp(name, buf))
+                       return i;
+       }
+       return -1;
+}
+#endif
+
 static int robo_probe(char *devname)
 {
        __u32 phyid;
@@ -296,9 +312,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);
@@ -317,13 +330,46 @@ static int robo_probe(char *devname)
                goto err_put;
        }
 
+#ifdef CONFIG_BCM47XX
+       robo.gpio_lanports_enable = get_gpio_pin("lanports_enable");
+       if (robo.gpio_lanports_enable >= 0) {
+               err = gpio_request(robo.gpio_lanports_enable, "lanports_enable");
+               if (err) {
+                       printk(KERN_ERR PFX "error (%i) requesting lanports_enable gpio (%i)\n",
+                              err, robo.gpio_lanports_enable);
+                       goto err_put;
+               }
+               gpio_direction_output(robo.gpio_lanports_enable, 1);
+               mdelay(5);
+       }
+
+       robo.gpio_robo_reset = get_gpio_pin("robo_reset");
+       if (robo.gpio_robo_reset >= 0) {
+               err = gpio_request(robo.gpio_robo_reset, "robo_reset");
+               if (err) {
+                       printk(KERN_ERR PFX "error (%i) requesting robo_reset gpio (%i)\n",
+                              err, robo.gpio_robo_reset);
+                       goto err_gpio_robo;
+               }
+               gpio_set_value(robo.gpio_robo_reset, 0);
+               gpio_direction_output(robo.gpio_robo_reset, 1);
+               gpio_set_value(robo.gpio_robo_reset, 0);
+               mdelay(50);
+
+               gpio_set_value(robo.gpio_robo_reset, 1);
+               mdelay(20);
+       } else {
+               // TODO: reset the internal robo switch
+       }
+#endif
+
        phyid = mdio_read(ROBO_PHY_ADDR, 0x2) |
                (mdio_read(ROBO_PHY_ADDR, 0x3) << 16);
 
        if (phyid == 0xffffffff || phyid == 0x55210022) {
                printk(KERN_ERR PFX "No Robo switch in managed mode found, phy_id = 0x%08x\n", phyid);
                err = -ENODEV;
-               goto err_put;
+               goto err_gpio_lanports;
        }
 
        /* Get the device ID */
@@ -335,17 +381,37 @@ static int robo_probe(char *devname)
        }
        if (!robo.devid)
                robo.devid = ROBO_DEVICE_ID_5325; /* Fake it */
-       robo.is_5350 = robo_vlan5350(phyid);
+       if (robo.devid == ROBO_DEVICE_ID_5325)
+               robo.is_5365 = robo_bcm5365();
+       else
+               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();
        if (err)
-               goto err_put;
+               goto err_gpio_lanports;
 
        printk(KERN_INFO PFX "found a 5%s%x!%s at %s\n", robo.devid & 0xff00 ? "" : "3", robo.devid,
-               robo.is_5350 ? " It's a 5350." : "", devname);
+               robo.is_5365 ? " It's a BCM5365." : "", devname);
 
        return 0;
+
+err_gpio_lanports:
+       if (robo.gpio_lanports_enable >= 0)
+               gpio_free(robo.gpio_lanports_enable);
+err_gpio_robo:
+       if (robo.gpio_robo_reset >= 0)
+               gpio_free(robo.gpio_robo_reset);
 err_put:
        dev_put(robo.dev);
        robo.dev = NULL;
@@ -362,17 +428,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_5350) {
-               u32 val32;
-               robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350, 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 */) {
+               val16 = robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_READ);
+               if ((val16 & (1 << 14)) /* valid */) {
                        for (j = 0; j < 6; j++) {
-                               if (val32 & (1 << 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 == 5)
+                                                       buf[len++] = 'u';
                                        } else {
                                                buf[len++] = 't';
                                                if (robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1)) == nr)
@@ -384,15 +450,17 @@ static int handle_vlan_port_read(void *driver, char *buf, int nr)
                        len += sprintf(buf + len, "\n");
                }
        } else {
+               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 */) {
+               val32 = robo_read32(ROBO_VLAN_PAGE, ROBO_VLAN_READ);
+               if ((val32 & (1 << 20)) /* valid */) {
                        for (j = 0; j < 6; j++) {
-                               if (val16 & (1 << 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 + 6))) {
+                                               if (j == 5)
+                                                       buf[len++] = 'u';
                                        } else {
                                                buf[len++] = 't';
                                                if (robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1)) == nr)
@@ -440,13 +508,13 @@ static int handle_vlan_port_write(void *driver, char *buf, int nr)
        }
 
        val16 = (nr) /* vlan */ | (1 << 12) /* write */ | (1 << 13) /* enable */;
-       if (robo.is_5350) {
-               robo_write32(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE_5350,
-                       (1 << 20) /* valid */ | (c->untag << 6) | c->port);
-               robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350, val16);
-       } else {
-               robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE,
+       if (robo.is_5365) {
+               robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE_5365,
                        (1 << 14)  /* valid */ | (c->untag << 7) | 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);
                robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS, val16);
        }
 
@@ -579,14 +647,14 @@ static int handle_reset(void *driver, char *buf, int nr)
        set_switch(0);
 
        /* reset vlans */
-       for (j = 0; j <= ((robo.is_5350) ? VLAN_ID_MAX5350 : VLAN_ID_MAX); j++) {
+       for (j = 0; j <= ((robo.is_5365) ? VLAN_ID_MAX_5365 : VLAN_ID_MAX); j++) {
                /* write config now */
                val16 = (j) /* vlan */ | (1 << 12) /* write */ | (1 << 13) /* enable */;
-               if (robo.is_5350)
-                       robo_write32(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE_5350, 0);
+               if (robo.is_5365)
+                       robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE_5365, 0);
                else
-                       robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE, 0);
-               robo_write16(ROBO_VLAN_PAGE, robo.is_5350 ? ROBO_VLAN_TABLE_ACCESS_5350 :
+                       robo_write32(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE, 0);
+               robo_write16(ROBO_VLAN_PAGE, robo.is_5365 ? ROBO_VLAN_TABLE_ACCESS_5365 :
                                                            ROBO_VLAN_TABLE_ACCESS,
                             val16);
        }
@@ -680,6 +748,10 @@ static void __exit robo_exit(void)
        switch_unregister_driver(DRIVER_NAME);
        if (robo.dev)
                dev_put(robo.dev);
+       if (robo.gpio_robo_reset >= 0)
+               gpio_free(robo.gpio_robo_reset);
+       if (robo.gpio_lanports_enable >= 0)
+               gpio_free(robo.gpio_lanports_enable);
        kfree(robo.device);
 }