[][MT79xx add USBIF compliance toolkit]

[Description]
Add USBIF compliance toolkit for MT79xx
hqa
usb3hqa
RG_USB20_INTR_EN
RG_UsB20_VRT_VREF_SEL
RG_USB20_TERM_VREF_SEL
RG_USB20_HSTX_SRCTRL
RG_USB20_DISCTH
RG_CHGDT_EN
reg

fixed Port1 show USB20, it shows USB30

[Release-log]
N/A

Change-Id: Iad411fd7ba992a25fc9a72e62cf6e5b421bf10c4
Reviewed-on: https://gerrit.mediatek.inc/c/openwrt/feeds/mtk_openwrt_feeds/+/5694096
diff --git a/target/linux/mediatek/files-5.4/drivers/usb/host/xhci-mtk-test.c b/target/linux/mediatek/files-5.4/drivers/usb/host/xhci-mtk-test.c
index 4939e55..36564d2 100644
--- a/target/linux/mediatek/files-5.4/drivers/usb/host/xhci-mtk-test.c
+++ b/target/linux/mediatek/files-5.4/drivers/usb/host/xhci-mtk-test.c
@@ -16,6 +16,8 @@
 #include <linux/kobject.h>
 #include <linux/of.h>
 #include <linux/of_address.h>
+#include <linux/usb.h>
+#include <linux/usb/hcd.h>
 #include <dt-bindings/phy/phy.h>
 #include "../core/usb.h"
 #include "xhci-mtk.h"
@@ -32,20 +34,50 @@
 				int argc, char **argv);
 static int t_test_enumerate_bus(struct xhci_hcd_mtk *mtk,
 				int argc, char **argv);
+static int t_debug_port(struct xhci_hcd_mtk *mtk, int argc, char **argv);
 static int t_power_u1u2(struct xhci_hcd_mtk *mtk, int argc, char **argv);
 
 #define PORT_PLS_VALUE(p) ((p >> 5) & 0xf)
+/* ip_xhci_cap register */
+#define CAP_U3_PORT_NUM(p)	((p) & 0xff)
+#define CAP_U2_PORT_NUM(p)	(((p) >> 8) & 0xff)
 
 #define MAX_NAME_SIZE 32
 #define MAX_ARG_SIZE 4
 
+struct class_info {
+        int class;
+        char *class_name;
+};
+
+static const struct class_info clas_info[] = {
+        /* max. 5 chars. per name string */
+        {USB_CLASS_PER_INTERFACE,       ">ifc"},
+        {USB_CLASS_AUDIO,               "audio"},
+        {USB_CLASS_COMM,                "comm."},
+        {USB_CLASS_HID,                 "HID"},
+        {USB_CLASS_PHYSICAL,            "PID"},
+        {USB_CLASS_STILL_IMAGE,         "still"},
+        {USB_CLASS_PRINTER,             "print"},
+        {USB_CLASS_MASS_STORAGE,        "stor."},
+        {USB_CLASS_HUB,                 "hub"},
+        {USB_CLASS_CDC_DATA,            "data"},
+        {USB_CLASS_CSCID,               "scard"},
+        {USB_CLASS_CONTENT_SEC,         "c-sec"},
+        {USB_CLASS_VIDEO,               "video"},
+        {USB_CLASS_WIRELESS_CONTROLLER, "wlcon"},
+        {USB_CLASS_MISC,                "misc"},
+        {USB_CLASS_APP_SPEC,            "app."},
+        {USB_CLASS_VENDOR_SPEC,         "vend."},
+        {-1,                            "unk."}         /* leave as last */
+};
+
 struct hqa_test_cmd {
 	char name[MAX_NAME_SIZE];
 	int (*cb_func)(struct xhci_hcd_mtk *mtk, int argc, char **argv);
 	char *discription;
 };
 
-
 struct hqa_test_cmd xhci_mtk_hqa_cmds[] = {
 	{"test.j", &t_test_j, "Test_J"},
 	{"test.k", &t_test_k, "Test_K"},
@@ -56,10 +88,20 @@
 	{"test.enumbus", &t_test_enumerate_bus, "Enumerate Bus"},
 	{"test.getdesc", &t_test_get_device_descriptor,
 				"Get Device Discriptor"},
+	{"test.debug", &t_debug_port, "debug Port infor"},
 	{"pm.u1u2", &t_power_u1u2, "Port U1,U2"},
 	{"", NULL, ""},
 };
 
+static const char *class_decode(const int class)
+{
+        int i;
+
+        for (i = 0; clas_info[i].class != -1; i++)
+                if (clas_info[i].class == class)
+                        break;
+        return clas_info[i].class_name;
+}
 
 int call_hqa_func(struct xhci_hcd_mtk *mtk, char *buf)
 {
@@ -256,6 +298,82 @@
 	return retval;
 }
 
+static void show_string(struct usb_device *udev, char *id, char *string)
+{
+	if (!string)
+		return;
+	dev_info(&udev->dev, "%s: %s\n", id, string);
+}
+
+static void announce_device(struct usb_device *udev)
+{
+	u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
+
+	dev_info(&udev->dev,
+		"New USB device found, idVendor=%04x, idProduct=%04x, bcdDevice=%2x.%02x\n",
+		le16_to_cpu(udev->descriptor.idVendor),
+		le16_to_cpu(udev->descriptor.idProduct),
+		bcdDevice >> 8, bcdDevice & 0xff);
+	dev_info(&udev->dev,
+		"New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
+		udev->descriptor.iManufacturer,
+		udev->descriptor.iProduct,
+		udev->descriptor.iSerialNumber);
+	show_string(udev, "Product", udev->product);
+	show_string(udev, "Manufacturer", udev->manufacturer);
+	show_string(udev, "SerialNumber", udev->serial);
+}
+
+static int t_debug_port(struct xhci_hcd_mtk *mtk, int argc, char **argv)
+{
+	struct usb_hcd *hcd = mtk->hcd;
+	struct xhci_hcd *xhci = hcd_to_xhci(hcd);
+	struct usb_device *usb2_rh;
+	struct usb_device *udev;
+	long port_id;
+        const struct usb_device_descriptor *desc;
+        u16 bcdUSB;
+        u16 bcdDevice;
+
+	port_id = 2;
+
+	if (argc > 1 && kstrtol(argv[1], 10, &port_id))
+		xhci_err(xhci, "mu3h %s get port-id failed\n", __func__);
+
+	xhci_err(xhci, "mu3h %s test port%d\n", __func__, (int)port_id);
+
+
+	usb2_rh = hcd->self.root_hub;
+	udev = usb_hub_find_child(usb2_rh, port_id - 1);
+	if (udev == NULL) {
+		xhci_err(xhci, "mu3h %s usb_hub_find_child(..., %i) failed\n", __func__, (int)port_id);
+		return -EPERM;
+	}
+
+	dev_info(&udev->dev, "%s\n", usb_state_string(udev->state));
+	if (udev && udev->state == USB_STATE_CONFIGURED) {
+		announce_device(udev);
+		desc = (const struct usb_device_descriptor *)&udev->descriptor;
+                bcdUSB    = le16_to_cpu(desc->bcdUSB);
+                bcdDevice = le16_to_cpu(desc->bcdDevice);
+
+                dev_info(&udev->dev, "D:  Ver=%2x.%02x Cls=%02x(%-5s) Sub=%02x Prot=%02x MxPS=%2d #Cfgs=%3d\n",
+                        bcdUSB >> 8, bcdUSB & 0xff,
+                        desc->bDeviceClass,
+                        class_decode(desc->bDeviceClass),
+                        desc->bDeviceSubClass,
+                        desc->bDeviceProtocol,
+                        desc->bMaxPacketSize0,
+                        desc->bNumConfigurations);
+
+                dev_info(&udev->dev, "P:  Vendor=%04x ProdID=%04x Rev=%2x.%02x\n",
+                        le16_to_cpu(desc->idVendor),
+                        le16_to_cpu(desc->idProduct),
+                        bcdDevice >> 8, bcdDevice & 0xff);
+	}
+
+	return 0;
+}
 
 static int t_test_suspend(struct xhci_hcd_mtk *mtk, int argc, char **argv)
 {
@@ -447,33 +565,43 @@
 	u32 val;
 	u32 ports;
 	int len = 0;
-	int bufLen = PAGE_SIZE;
 	struct hqa_test_cmd *hqa;
 	int i;
 
-	len += snprintf(buf+len, bufLen-len, "info:\n");
-	len += snprintf(buf+len, bufLen-len,
+	len += sprintf(buf+len, "info:\n");
+	len += sprintf(buf+len,
 			"\techo -n item port-id > hqa\n");
-	len += snprintf(buf+len, bufLen-len,
+	len += sprintf(buf+len,
 			"\tport-id : based on number of usb3-port, e.g.\n");
-	len += snprintf(buf+len, bufLen-len,
+	len += sprintf(buf+len,
 			"\t\txHCI with 1 u3p, 2 u2p: 1st u2p-id is 2(1+1), 2nd is 3\n");
-	len += snprintf(buf+len, bufLen-len, "items:\n");
+	len += sprintf(buf+len, "items:\n");
 
 	for (i = 0; i < ARRAY_SIZE(xhci_mtk_hqa_cmds); i++) {
 		hqa = &xhci_mtk_hqa_cmds[i];
-		len += snprintf(buf+len, bufLen-len,
+		len += sprintf(buf+len,
 				"\t%s: %s\n", hqa->name, hqa->discription);
 	}
 
 	ports = mtk->num_u3_ports + mtk->num_u2_ports;
-	for (i = mtk->num_u3_ports + 1; i <= ports; i++) {
-		addr = &xhci->op_regs->port_power_base +
+	for (i = 1; i <= ports; i++) {
+		addr = &xhci->op_regs->port_status_base +
 			NUM_PORT_REGS * ((i - 1) & 0xff);
 		val = readl(addr);
-		len += snprintf(buf+len, bufLen-len,
-			"USB20 Port%i PORTMSC[31,28] 4b'0000: 0x%08X\n",
-			i, val);
+		if (i <= mtk->num_u3_ports)
+			len += sprintf(buf + len,
+				"USB30 Port%i: 0x%08X\n", i, val);
+		else {
+			len += sprintf(buf + len,
+				"USB20 Port%i: 0x%08X\n", i, val);
+
+			addr = &xhci->op_regs->port_power_base +
+				NUM_PORT_REGS * ((i - 1) & 0xff);
+			val = readl(addr);
+			len += sprintf(buf+len,
+				"USB20 Port%i PORTMSC[31,28] 4b'0000: 0x%08X\n",
+				i, val);
+		}
 	}
 
 	return len;
@@ -511,17 +639,17 @@
 	int ports;
 
 	cnt += sprintf(buf + cnt, "usb3hqa usage:\n");
-	cnt += sprintf(buf + cnt, "	echo u3port >usb3hqa\n");
+	cnt += sprintf(buf + cnt, "	echo [u3port] >usb3hqa\n");
 
 	ports = mtk->num_u3_ports + mtk->num_u2_ports;
 	for (i = 1; i <= ports; i++) {
 		addr = &xhci->op_regs->port_status_base +
 			NUM_PORT_REGS * ((i - 1) & 0xff);
 		val = readl(addr);
-		if (i < mtk->num_u3_ports)
+		if (i <= mtk->num_u3_ports)
 			cnt += sprintf(buf + cnt,
 				"USB30 Port%i: 0x%08X\n", i, val);
-		else 
+		else
 			cnt += sprintf(buf + cnt,
 				"USB20 Port%i: 0x%08X\n", i, val);
 	}
@@ -554,8 +682,8 @@
 	words = sscanf(buf, "%d", &port);
 	if ((words != 1) ||
 	    (port < 1 || port > mtk->num_u3_ports)) {
-		hqa_info(mtk, "usb3hqa: param number:%i, port:%i failure\n",
-			words, port);
+		hqa_info(mtk, "usb3hqa: param number:%i, port:%i (%i) failure\n",
+			words, port, mtk->num_u3_ports);
 		return -EINVAL;
 	}
 
@@ -580,9 +708,16 @@
 
 int hqa_create_attr(struct device *dev)
 {
-	int idx, err = 0;
-	int num = ARRAY_SIZE(mu3h_hqa_attr_list);
 	struct xhci_hcd_mtk *mtk = dev_get_drvdata(dev);
+	struct usb_hcd *hcd = mtk->hcd;
+	struct mu3c_ippc_regs __iomem *ippc = mtk->ippc_regs;
+	struct platform_device *device = to_platform_device(dev);
+	int num = ARRAY_SIZE(mu3h_hqa_attr_list);
+	int idx;
+	int err = 0;
+	u32 value;
+	u32 addr = hcd->rsrc_start;
+	u32 length;
 
 	if (dev == NULL || mtk == NULL)
 		return -EINVAL;
@@ -593,6 +728,19 @@
 	if (!mtk->hqa_buf)
 		return -ENOMEM;
 
+	if (!mtk->has_ippc) {
+		err = query_reg_addr(device, &addr, &length, "ippc");
+		if (err)
+			return -EINVAL;
+
+		mtk->ippc_regs = ioremap(addr, length);
+	}
+
+	ippc  = mtk->ippc_regs;
+	value = readl(&ippc->ip_xhci_cap);
+	mtk->num_u3_ports = CAP_U3_PORT_NUM(value);
+	mtk->num_u2_ports = CAP_U2_PORT_NUM(value);
+
 	for (idx = 0; idx < num; idx++) {
 		err = device_create_file(dev, mu3h_hqa_attr_list[idx]);
 		if (err)
@@ -614,4 +762,8 @@
 	kfree(mtk->hqa_buf);
 	mtk->hqa_size = 0;
 	mtk->hqa_pos  = 0;
+	if (!mtk->has_ippc) {
+		iounmap(mtk->ippc_regs);
+		mtk->ippc_regs = NULL;
+	}
 }