I wrote this patch some time ago because I had a need for one of the 
Gigabit ports (Linksys E3000) to be forced to 100FD.  This is based
on the robocfg sources included w/ the RT-N16 sources from ASUS.
Since work is progressing on a BGMAC driver that could be included in
OpenWRT, this may be useful to someone else.

In testing, forcing the speed to 10/100 or 1000 worked fine; however,
when trying to force full-duplex mode, the result was always
half-duplex.  I was not able to isolate the source of the problem
(this patch, driver or H/W limitation).  The only way I could get it
to work was to set the port to Auto, but then only advertise 100FD
(not included in this patch).

I have a modified version of the robocfg package as well, I'd have to
clean it up a little first (remove the full-duplex hack) before
submitting it if there is interest.

Signed-off-by: Nathan Hintz <nlhi...@hotmail.com>

Index: package/switch/src/switch-core.c
===================================================================
--- package/switch/src/switch-core.c    (revision 34778)
+++ package/switch/src/switch-core.c    (working copy)
@@ -26,7 +26,7 @@
  *   enable_vlan:            "0", "1"
  *   port/<port-number>/
  *     enabled:              "0", "1"
- *     media:                "AUTO", "100FD", "100HD", "10FD", "10HD"
+ *     media:                "AUTO", "1000FD", "1000HD", "100FD", "100HD", 
"10FD", "10HD"
  *   vlan/<port-number>/
  *     ports: same syntax as for nvram's vlan*ports (eg. "1 2 3 4 5*")
  */
@@ -305,6 +305,10 @@
 
        if (strncmp(str, "AUTO", 4) == 0)
                return SWITCH_MEDIA_AUTO;
+       else if (strncmp(str, "1000FD", 6) == 0)
+               return SWITCH_MEDIA_1000 | SWITCH_MEDIA_FD;
+       else if (strncmp(str, "1000HD", 6) == 0)
+               return SWITCH_MEDIA_1000;
        else if (strncmp(str, "100FD", 5) == 0)
                return SWITCH_MEDIA_100 | SWITCH_MEDIA_FD;
        else if (strncmp(str, "100HD", 5) == 0)
@@ -322,14 +326,18 @@
 
        if (media & SWITCH_MEDIA_AUTO)
                len = sprintf(buf, "Auto");
+       else if (media == (SWITCH_MEDIA_1000 | SWITCH_MEDIA_FD))
+               len = sprintf(buf, "1000FD");
+       else if (media == SWITCH_MEDIA_1000)
+               len = sprintf(buf, "1000HD");
        else if (media == (SWITCH_MEDIA_100 | SWITCH_MEDIA_FD))
                len = sprintf(buf, "100FD");
        else if (media == SWITCH_MEDIA_100)
-               len = sprintf(buf,  "100HD");
+               len = sprintf(buf, "100HD");
        else if (media == SWITCH_MEDIA_FD)
-               len = sprintf(buf,  "10FD");
+               len = sprintf(buf, "10FD");
        else if (media == 0)
-               len = sprintf(buf,  "10HD");
+               len = sprintf(buf, "10HD");
        else
                len = sprintf(buf, "Invalid");
 
Index: package/switch/src/etc53xx.h
===================================================================
--- package/switch/src/etc53xx.h        (revision 34778)
+++ package/switch/src/etc53xx.h        (working copy)
@@ -15,9 +15,10 @@
 #define __BCM535M_H_
 
 /* ROBO embedded device type */
-#define ROBO_DEV_5380 1
-#define ROBO_DEV_5365 2
-#define ROBO_DEV_5350 3
+#define ROBO_DEV_5380  1
+#define ROBO_DEV_5365  2
+#define ROBO_DEV_5350  3
+#define ROBO_DEV_53115 4
 
 /* BCM5325m GLOBAL PAGE REGISTER MAP */
 #ifndef _CFE_
@@ -316,7 +317,19 @@
 #define ROBO_ARL_SEARCH_RESULT_EXT 0x2c /* ARL Search Result Extension (5350): 
8bit */
 #define ROBO_ARL_VID_ENTRY0       0x30 /* ARL VID Entry 0: 64bit */
 #define ROBO_ARL_VID_ENTRY1       0x32 /* ARL VID Entry 1: 64bit */
+#define ROBO_ARL_SEARCH_CTRL_53115       0x50 /* ARL Search Control: 8bit */
+#define ROBO_ARL_SEARCH_ADDR_53115       0x51 /* ARL Search Address: 16bit */
+#define ROBO_ARL_SEARCH_RESULT_53115     0x60 /* ARL Search Result: 64bit */
+#define ROBO_ARL_SEARCH_RESULT_EXT_53115 0x68 /* ARL Search Result Extension 
(53115): 16bit */
 
+/* BCM5395/5397/5398/53115 */
+#define ROBO_VTBL_ACCESS          0x60 /* VLAN table access: 8bit */
+#define ROBO_VTBL_INDX            0x61 /* VLAN table address index: 16bit */
+#define ROBO_VTBL_ENTRY           0x63 /* VLAN table entry: 32bit */
+#define ROBO_VTBL_ACCESS_5395     0x80 /* VLAN table access: 8bit */
+#define ROBO_VTBL_INDX_5395       0x81 /* VLAN table address index: 16bit */
+#define ROBO_VTBL_ENTRY_5395      0x83 /* VLAN table entry: 32bit */
+
 /* BCM5325m MANAGEMENT FRAME REGISTERS (0x6) REGISTER MAP: 8/16 bit regs */
 #define ROBO_MGMT_FRAME_RD_DATA   0x00 /* Management Frame Read Data :8bit*/
 #define ROBO_MGMT_FRAME_WR_DATA   0x01 /* Management Frame Write Data:8bit*/
@@ -342,6 +355,8 @@
 #define NUM_VLAN_TABLE_ENTRIES_5350 16  /* number of entries in VLAN table */
 #define ARL_TABLE_ADDR_5350 0x1c00      /* offset of ARL table start (5350) */
 #endif
+#define NUM_ARL_TABLE_ENTRIES_53115 4096 /* number of entries in ARL table 
(53115) */
+#define NUM_VLAN_TABLE_ENTRIES_53115 4096 /* number of entries in VLAN table */
 typedef struct _ROBO_MEM_ACCESS_CTRL_STRUC
 {
     unsigned int    memAddr:14; /* 64-bit memory address */
@@ -529,6 +544,7 @@
 #define VLAN_ID_HIGH_BITS 0             /* static high bits in table access 
reg */
 #define VLAN_ID_MAX 255                 /* max VLAN id */
 #define VLAN_ID_MAX5350 15              /* max VLAN id (5350) */
+#define VLAN_ID_MAX5395 4094            /* max VLAN id (5395) */
 #define VLAN_ID_MASK VLAN_ID_MAX        /* VLAN id mask */
 #ifdef BCM5380
 #define VLAN_UNTAG_SHIFT 13             /* for postioning untag bits in write 
reg */
@@ -594,26 +610,15 @@
 #define ROBO_VLAN_PORT5_DEF_TAG     0x1a /* 16b: VLAN Port 5 Default Tag 
Register */
 #define ROBO_VLAN_PORT6_DEF_TAG     0x1c /* 16b: VLAN Port 6 Default Tag 
Register */
 #define ROBO_VLAN_PORT7_DEF_TAG     0x1e /* 16b: VLAN Port 7 Default Tag 
Register */
+#define ROBO_VLAN_PORT8_DEF_TAG     0x20 /* 16b: VLAN Port 8 Default Tag 
Register */
+/* 53115 only */
+#define ROBO_DUPLEX_STAT_SUMMARY_53115 0x08 /* Duplex Status Summary: 16bit */
+#define ROBO_JUMBO_PAGE                0x40
+#define ROBO_JUMBO_CTRL                0x01 /* 32bit */
+#define ROBO_JUMBO_SIZE                0x05 /* 16bit */
 
-/* obsolete */
-#define ROBO_VLAN_PORT0_CTRL       0x00 /* 16b: Port 0 VLAN  Register */
-#define ROBO_VLAN_PORT1_CTRL       0x02 /* 16b: Port 1 VLAN  Register */
-#define ROBO_VLAN_PORT2_CTRL       0x04 /* 16b: Port 2 VLAN  Register */
-#define ROBO_VLAN_PORT3_CTRL       0x06 /* 16b: Port 3 VLAN  Register */
-#define ROBO_VLAN_PORT4_CTRL       0x08 /* 16b: Port 4 VLAN  Register */
-#define ROBO_VLAN_IM_PORT_CTRL     0x10 /* 16b: Inverse MII Port VLAN Reg */
-#define ROBO_VLAN_SMP_PORT_CTRL    0x12 /* 16b: Serial Port VLAN  Register */
-#define ROBO_VLAN_PORTSPI_DEF_TAG  0x1c /* 16b: VLAN Port SPI Default Tag 
Register */
-#define ROBO_VLAN_PRIORITY_REMAP   0x20 /* 24b: VLAN Priority Re-Map Register 
*/
-
 #ifndef _CFE_
 #pragma pack()
 #endif
 
-
 #endif /* !__BCM535M_H_ */
-
-
-
-
-
Index: package/switch/src/switch-core.h
===================================================================
--- package/switch/src/switch-core.h    (revision 34778)
+++ package/switch/src/switch-core.h    (working copy)
@@ -8,6 +8,7 @@
 #define SWITCH_MEDIA_AUTO      1
 #define SWITCH_MEDIA_100       2
 #define SWITCH_MEDIA_FD                4
+#define SWITCH_MEDIA_1000      8
 
 typedef int (*switch_handler)(void *driver, char *buf, int nr);
 
Index: package/switch/src/switch-adm.c
===================================================================
--- package/switch/src/switch-adm.c     (revision 34778)
+++ package/switch/src/switch-adm.c     (working copy)
@@ -386,7 +386,7 @@
        int media = switch_parse_media(buf);
        int reg = adm_rreg(0, port_conf[nr]);
 
-       if (media < 0)
+       if (media < 0 || media & SWITCH_MEDIA_1000)
                return -1;
 
        reg &= ~((1 << 1) | (1 << 2) | (1 << 3));
Index: package/switch/src/switch-robo.c
===================================================================
--- package/switch/src/switch-robo.c    (revision 34778)
+++ package/switch/src/switch-robo.c    (working copy)
@@ -72,6 +72,7 @@
        char *device;                   /* The device name string (ethX) */
        u16 devid;                      /* ROBO_DEVICE_ID_53xx */
        bool is_5350;
+       u8 gmii;                        /* gigabit mii */
        struct ifreq ifr;
        struct net_device *dev;
        unsigned char port[6];
@@ -149,7 +150,7 @@
 
        printk(KERN_ERR PFX "[%s:%d] timeout in robo_reg!\n", __FILE__, 
__LINE__);
 
-       return 0;
+       return 1;
 }
 
 /*
@@ -175,7 +176,7 @@
 {
        robo_reg(page, reg, REG_MII_ADDR_READ);
 
-       return mdio_read(ROBO_PHY_ADDR, REG_MII_DATA0) +
+       return mdio_read(ROBO_PHY_ADDR, REG_MII_DATA0) |
                (mdio_read(ROBO_PHY_ADDR, REG_MII_DATA0 + 1) << 16);
 }
 
@@ -190,21 +191,40 @@
 static void robo_write32(__u8 page, __u8 reg, __u32 val32)
 {
        /* write data */
-       mdio_write(ROBO_PHY_ADDR, REG_MII_DATA0, val32 & 65535);
+       mdio_write(ROBO_PHY_ADDR, REG_MII_DATA0, val32 & 0xFFFF);
        mdio_write(ROBO_PHY_ADDR, REG_MII_DATA0 + 1, val32 >> 16);
 
        robo_reg(page, reg, REG_MII_ADDR_WRITE);
 }
 
-/* checks that attached switch is 5325E/5350 */
-static int robo_vlan5350(void)
+/* checks that attached switch is 5325/5352/5354/5356/5357/53115 */
+static int robo_vlan5350(__u32 phyid)
 {
        /* set vlan access id to 15 and read it back */
        __u16 val16 = 15;
        robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350, val16);
 
        /* 5365 will refuse this as it does not have this reg */
-       return (robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350) == 
val16);
+       if (robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350) != val16)
+               return 0;
+       /* gigabit ? */
+       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;
 }
 
 static int robo_switch_enable(void)
@@ -224,7 +244,7 @@
                robo_write16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE, val);
                val = robo_read16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE);
                if (!(val & (1 << 1))) {
-                       printk("Failed to enable switch\n");
+                       printk(KERN_ERR PFX "Failed to enable switch\n");
                        return -EBUSY;
                }
 
@@ -261,16 +281,17 @@
        __u32 phyid;
        unsigned int i;
        int err = 1;
+       struct mii_ioctl_data *mii;
 
-       printk(KERN_INFO PFX "Probing device %s: ", devname);
+       printk(KERN_INFO PFX "Probing device '%s'", devname);
        strcpy(robo.ifr.ifr_name, devname);
 
        if ((robo.dev = dev_get_by_name(&init_net, devname)) == NULL) {
-               printk("No such device\n");
+               printk(KERN_ERR PFX "No such device\n");
                return 1;
        }
        if (!robo.dev->netdev_ops || !robo.dev->netdev_ops->ndo_do_ioctl) {
-               printk("ndo_do_ioctl not implemented in ethernet driver\n");
+               printk(KERN_ERR PFX "ndo_do_ioctl not implemented in ethernet 
driver\n");
                return 1;
        }
 
@@ -281,16 +302,16 @@
 
        /* try access using MII ioctls - get phy address */
        if (do_ioctl(SIOCGMIIPHY) < 0) {
-               printk("error while accessing MII phy registers with ioctls\n");
+               printk(KERN_ERR PFX "error while accessing MII phy registers 
with ioctls\n");
                goto done;
        }
 
        /* got phy address check for robo address */
-       struct mii_ioctl_data *mii = if_mii(&robo.ifr);
+       mii = if_mii(&robo.ifr);
        if ((mii->phy_id != ROBO_PHY_ADDR) &&
            (mii->phy_id != ROBO_PHY_ADDR_BCM63XX) &&
            (mii->phy_id != ROBO_PHY_ADDR_TG3)) {
-               printk("Invalid phy address (%d)\n", mii->phy_id);
+               printk(KERN_ERR PFX "Invalid phy address (%d)\n", mii->phy_id);
                goto done;
        }
 
@@ -298,7 +319,7 @@
                (mdio_read(ROBO_PHY_ADDR, 0x3) << 16);
 
        if (phyid == 0xffffffff || phyid == 0x55210022) {
-               printk("No Robo switch in managed mode found, phy_id = 
0x%08x\n", phyid);
+               printk(KERN_ERR PFX "No Robo switch in managed mode found, 
phy_id = 0x%08x\n", phyid);
                goto done;
        }
 
@@ -311,7 +332,7 @@
        }
        if (!robo.devid)
                robo.devid = ROBO_DEVICE_ID_5325; /* Fake it */
-       robo.is_5350 = robo_vlan5350();
+       robo.is_5350 = robo_vlan5350(phyid);
 
        robo_switch_reset();
        err = robo_switch_enable();
@@ -319,7 +340,7 @@
                goto done;
        err = 0;
 
-       printk("found a 5%s%x!%s\n", robo.devid & 0xff00 ? "" : "3", robo.devid,
+       printk(KERN_INFO PFX "found a 5%s%x!%s\n", robo.devid & 0xff00 ? "" : 
"3", robo.devid,
                robo.is_5350 ? " It's a 5350." : "");
 
 done:
@@ -446,6 +467,80 @@
        return 0;
 }
 
+static int handle_port_enable_read(void *driver, char *buf, int nr)
+{
+       return sprintf(buf, "%d\n", ((robo_read16(ROBO_CTRL_PAGE, 
robo.port[nr]) & 3) == 3 ? 0 : 1));
+}
+
+static int handle_port_enable_write(void *driver, char *buf, int nr)
+{
+       u16 val16;
+
+       if (buf[0] == '0')
+               val16 = 3; /* disabled */
+       else if (buf[0] == '1')
+               val16 = 0; /* enabled */
+       else
+               return -EINVAL;
+
+       robo_write16(ROBO_CTRL_PAGE, robo.port[nr],
+               (robo_read16(ROBO_CTRL_PAGE, robo.port[nr]) & ~3) | val16);
+
+       return 0;
+}
+
+static int handle_port_media_read(void *driver, char *buf, int nr)
+{
+       u16 bmcr = mdio_read(robo.port[nr], MII_BMCR);
+       int media, len;
+
+       if (bmcr & BMCR_ANENABLE)
+               media = SWITCH_MEDIA_AUTO;
+       else {
+               if (bmcr & BMCR_SPEED1000)
+                       media = SWITCH_MEDIA_1000;
+               else if (bmcr & BMCR_SPEED100)
+                       media = SWITCH_MEDIA_100;
+               else
+                       media = 0;
+
+               if (bmcr & BMCR_FULLDPLX)
+                       media |= SWITCH_MEDIA_FD;
+       }
+
+       len = switch_print_media(buf, media);
+       return len + sprintf(buf + len, "\n");
+}
+
+static int handle_port_media_write(void *driver, char *buf, int nr)
+{
+       int media = switch_parse_media(buf);
+       u16 bmcr, bmcr_mask;
+
+       if (media & SWITCH_MEDIA_AUTO)
+               bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
+       else {
+               if (media & SWITCH_MEDIA_1000) {
+                       if (!robo.gmii)
+                               return -EINVAL;
+                       bmcr = BMCR_SPEED1000;
+               }
+               else if (media & SWITCH_MEDIA_100)
+                       bmcr = BMCR_SPEED100;
+               else
+                       bmcr = 0;
+
+               if (media & SWITCH_MEDIA_FD)
+                       bmcr |= BMCR_FULLDPLX;
+       }
+
+       bmcr_mask = ~(BMCR_SPEED1000 | BMCR_SPEED100 | BMCR_FULLDPLX | 
BMCR_ANENABLE | BMCR_ANRESTART);
+       mdio_write(robo.port[nr], MII_BMCR, 
+               (mdio_read(robo.port[nr], MII_BMCR) & bmcr_mask) | bmcr);
+
+       return 0;
+}
+
 static int handle_enable_vlan_read(void *driver, char *buf, int nr)
 {
        return sprintf(buf, "%d\n", (((robo_read16(ROBO_VLAN_PAGE, 
ROBO_VLAN_CTRL0) & (1 << 7)) == (1 << 7)) ? 1 : 0));
@@ -540,6 +635,17 @@
                                .write  = handle_reset
                        }, { NULL, },
                };
+               static const switch_config port[] = {
+                       {
+                               .name   = "enable",
+                               .read   = handle_port_enable_read,
+                               .write  = handle_port_enable_write
+                       }, {
+                               .name   = "media",
+                               .read   = handle_port_media_read,
+                               .write  = handle_port_media_write
+                       }, { NULL, },
+               };
                static const switch_config vlan[] = {
                        {
                                .name   = "ports",
@@ -555,7 +661,7 @@
                        .ports                  = 6,
                        .vlans                  = 16,
                        .driver_handlers        = cfg,
-                       .port_handlers          = NULL,
+                       .port_handlers          = port,
                        .vlan_handlers          = vlan,
                };
                if (robo.devid != ROBO_DEVICE_ID_5325) {
Index: package/switch/Makefile
===================================================================
--- package/switch/Makefile     (revision 34778)
+++ package/switch/Makefile     (working copy)
@@ -9,7 +9,7 @@
 include $(INCLUDE_DIR)/kernel.mk
 
 PKG_NAME:=kmod-switch
-PKG_RELEASE:=5
+PKG_RELEASE:=6
 
 include $(INCLUDE_DIR)/package.mk
 

_______________________________________________
openwrt-devel mailing list
openwrt-devel@lists.openwrt.org
https://lists.openwrt.org/mailman/listinfo/openwrt-devel

Reply via email to