From c16de866d585eb25872732274d5109022193fed1 Mon Sep 17 00:00:00 2001
From: Hauke Mehrtens <hauke@openwrt.org>
Date: Thu, 3 Jan 2013 01:58:01 +0000
Subject: switch: ROBO Switch Gigabit MII Support

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 <nlhintz@hotmail.com>


git-svn-id: svn://svn.openwrt.org/openwrt/trunk@34992 3c298f89-4303-0410-b956-a3cf2f4a3e73
---
 package/switch/Makefile          |   2 +-
 package/switch/src/etc53xx.h     |  45 +++++++-------
 package/switch/src/switch-adm.c  |   2 +-
 package/switch/src/switch-core.c |  16 +++--
 package/switch/src/switch-core.h |   1 +
 package/switch/src/switch-robo.c | 124 ++++++++++++++++++++++++++++++++++++---
 6 files changed, 155 insertions(+), 35 deletions(-)

diff --git a/package/switch/Makefile b/package/switch/Makefile
index 2b036b07dc..9eda1e4d52 100644
--- a/package/switch/Makefile
+++ b/package/switch/Makefile
@@ -9,7 +9,7 @@ include $(TOPDIR)/rules.mk
 include $(INCLUDE_DIR)/kernel.mk
 
 PKG_NAME:=kmod-switch
-PKG_RELEASE:=5
+PKG_RELEASE:=6
 
 include $(INCLUDE_DIR)/package.mk
 
diff --git a/package/switch/src/etc53xx.h b/package/switch/src/etc53xx.h
index d5b1310cb6..c21d1bc79f 100644
--- a/package/switch/src/etc53xx.h
+++ b/package/switch/src/etc53xx.h
@@ -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,6 +317,18 @@ typedef struct _ROBO_ARL_SEARCH_RESULT_MCAST_STRUC
 #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*/
@@ -342,6 +355,8 @@ typedef struct _ROBO_ARL_SEARCH_RESULT_MCAST_STRUC
 #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 @@ typedef struct _ROBO_VLAN_CTRL0_STRUC
 #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 @@ typedef struct _ROBO_VLAN_READ_WRITE_STRUC_5350
 #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 */
-
-/* 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 */
+#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 */
 
 #ifndef _CFE_
 #pragma pack()
 #endif
 
-
 #endif /* !__BCM535M_H_ */
-
-
-
-
-
diff --git a/package/switch/src/switch-adm.c b/package/switch/src/switch-adm.c
index f21470ed53..2d0fcc35ab 100644
--- a/package/switch/src/switch-adm.c
+++ b/package/switch/src/switch-adm.c
@@ -386,7 +386,7 @@ static int handle_port_media_write(void *driver, char *buf, int nr)
 	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));
diff --git a/package/switch/src/switch-core.c b/package/switch/src/switch-core.c
index 79b4e93f03..c77e4bba47 100644
--- a/package/switch/src/switch-core.c
+++ b/package/switch/src/switch-core.c
@@ -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 @@ int switch_parse_media(char *buf)
 
 	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 @@ int switch_print_media(char *buf, int media)
 
 	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");
 
diff --git a/package/switch/src/switch-core.h b/package/switch/src/switch-core.h
index a2114cf92d..7192f7b0f4 100644
--- a/package/switch/src/switch-core.h
+++ b/package/switch/src/switch-core.h
@@ -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);
 
diff --git a/package/switch/src/switch-robo.c b/package/switch/src/switch-robo.c
index f9b306cf10..f3a0eacf19 100644
--- a/package/switch/src/switch-robo.c
+++ b/package/switch/src/switch-robo.c
@@ -72,6 +72,7 @@ struct robo_switch {
 	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];
@@ -147,7 +148,7 @@ static int robo_reg(__u8 page, __u8 reg, __u8 op)
 
 	printk(KERN_ERR PFX "timeout in robo_reg on page %i and reg %i with op %i.\n", page, reg, op);
 
-	return 0;
+	return 1;
 }
 
 /*
@@ -173,7 +174,7 @@ static __u32 robo_read32(__u8 page, __u8 reg)
 {
 	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);
 }
 
@@ -188,21 +189,40 @@ static void robo_write16(__u8 page, __u8 reg, __u16 val16)
 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)
@@ -259,6 +279,7 @@ static int robo_probe(char *devname)
 	__u32 phyid;
 	unsigned int i;
 	int err = 1;
+	struct mii_ioctl_data *mii;
 
 	printk(KERN_INFO PFX "Probing device '%s'\n", devname);
 	strcpy(robo.ifr.ifr_name, devname);
@@ -285,7 +306,7 @@ static int robo_probe(char *devname)
 	}
 
 	/* 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)) {
@@ -310,7 +331,7 @@ static int robo_probe(char *devname)
 	}
 	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();
@@ -445,6 +466,80 @@ static int handle_enable_write(void *driver, char *buf, int nr)
 	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));
@@ -539,6 +634,17 @@ static int __init robo_init(void)
 				.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",
@@ -554,7 +660,7 @@ static int __init robo_init(void)
 			.ports			= 6,
 			.vlans			= 16,
 			.driver_handlers	= cfg,
-			.port_handlers		= NULL,
+			.port_handlers		= port,
 			.vlan_handlers		= vlan,
 		};
 		if (robo.devid != ROBO_DEVICE_ID_5325) {
-- 
cgit v1.2.3