[Add devtools regs and mii_mgr]

[Description]
Add devtools regs and mii_mgr
1. regs tool for debug
2. mii_mgr tool for detecting wan link up when external 2.5G phy in force mode

[Release-log]

diff --git a/recipes-devtools/mii-mgr/files/COPYING b/recipes-devtools/mii-mgr/files/COPYING
new file mode 100644
index 0000000..7975bdb
--- /dev/null
+++ b/recipes-devtools/mii-mgr/files/COPYING
@@ -0,0 +1 @@
+Copyright (c) Mediatek 2020
diff --git a/recipes-devtools/mii-mgr/files/mii_mgr.c b/recipes-devtools/mii-mgr/files/mii_mgr.c
new file mode 100644
index 0000000..f968807
--- /dev/null
+++ b/recipes-devtools/mii-mgr/files/mii_mgr.c
@@ -0,0 +1,180 @@
+#include <errno.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <sys/ioctl.h>
+#include <net/if.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#include <linux/ethtool.h>
+#include <linux/mdio.h>
+#include <linux/sockios.h>
+
+#include "mii_mgr.h"
+
+void show_usage(void)
+{
+	printf("mii_mgr -g -i [ifname] -p [phy number] -r [register number]\n");
+	printf("  Get: mii_mgr -g -p 3 -r 4\n\n");
+	printf("mii_mgr -s -p [phy number] -r [register number] -v [0xvalue]\n");
+	printf("  Set: mii_mgr -s -p 4 -r 1 -v 0xff11\n");
+	printf("#NOTE: Without -i , eth0 is default ifname!\n");
+	printf("----------------------------------------------------------------------------------------\n");
+	printf("Get: mii_mgr_cl45 -g -p [port number] -d [dev number] -r [register number]\n");
+	printf("Example: mii_mgr_cl45 -g -p 3 -d 0x5 -r 0x4\n\n");
+	printf("Set: mii_mgr_cl45 -s -p [port number] -d [dev number] -r [register number] -v [value]\n");
+	printf("Example: mii_mgr_cl45 -s -p 4 -d 0x6 -r 0x1 -v 0xff11\n\n");
+}
+
+static void fill_mii_ioctl(struct mii_ioctl_data *mii, uint16_t phy_id,
+			   uint16_t reg_num, uint16_t *val)
+{
+	mii->phy_id  = phy_id;
+	mii->reg_num = reg_num;
+	mii->val_in  = *val;
+	mii->val_out = 0;
+}
+
+
+static void fill_mtk_mii_ioctl(struct mtk_mii_ioctl_data *mtk_mii, uint16_t phy_id,
+			      uint16_t reg_num, unsigned int *val)
+{
+	mtk_mii->phy_id  = phy_id;
+	mtk_mii->reg_num = reg_num;
+	mtk_mii->val_in  = *val;
+	mtk_mii->val_out = 0;
+}
+
+static int __phy_op(char *ifname, uint16_t phy_id, uint16_t reg_num, unsigned int *val, uint16_t cmd, int is_priv)
+{
+        static int sd = -1;
+
+        struct ifreq ifr;
+        struct mii_ioctl_data mii;
+	struct mtk_mii_ioctl_data mtk_mii;
+        int err;
+
+        if (sd < 0)
+                sd = socket(AF_INET, SOCK_DGRAM, 0);
+
+        if (sd < 0)
+                return sd;
+
+	strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1);
+	ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';
+
+	if (is_priv) {
+		fill_mtk_mii_ioctl(&mtk_mii, phy_id, reg_num, val);
+		ifr.ifr_data = (char *)&mtk_mii;
+	} else {
+		fill_mii_ioctl(&mii, phy_id, reg_num, (uint16_t *)val);
+		ifr.ifr_data = (char *)&mii;
+	}
+
+        err = ioctl(sd, cmd, &ifr);
+        if (err)
+                return -errno;
+
+	if ((cmd == MTKETH_MII_WRITE) || (cmd == MTKETH_MII_WRITE_CL45) ||
+	    (cmd == SIOCSMIIREG))
+		*val = (is_priv) ? mtk_mii.val_in : mii.val_in;
+	else
+		*val = (is_priv) ? mtk_mii.val_out : mii.val_out;
+
+        return 0;
+}
+
+int main(int argc, char *argv[])
+{
+	int opt;
+	char options[] = "gsui:p:d:r:v:?t";
+	int is_write = 0,is_cl45 = 0;
+	int is_priv = 1;
+	unsigned int port=0, dev=0,reg_num=0,val=0;
+	char ifname[IFNAMSIZ]="eth0";	
+	uint16_t phy_id=0;
+	uint16_t cmd;
+
+
+	if (argc < 6) {
+		show_usage();
+		return 0;
+	}
+
+	while ((opt = getopt(argc, argv, options)) != -1) {
+		switch (opt) {
+			case 'g':
+				is_write=0;
+				break;
+			case 's':
+				is_write=1;
+				break;
+			case 'u':
+				is_priv = 0;
+				break;
+			case 'i':
+				strncpy(ifname, optarg, IFNAMSIZ);
+				ifname[IFNAMSIZ - 1] = '\0';
+				break;	
+			case 'p':
+				port = strtoul(optarg, NULL, 16);
+				break;
+			case 'd':
+				dev = strtoul(optarg, NULL, 16);
+				is_cl45 = 1;
+				break;
+			case 'r':
+				reg_num = strtoul(optarg, NULL, 16);
+				break;
+
+			case 'v':
+				val = strtoul(optarg, NULL, 16);
+				break;
+			case '?':
+				show_usage();
+				break;
+		}
+	}
+
+	if(is_cl45)
+		phy_id = mdio_phy_id_c45(port, dev);
+	else
+		phy_id = port;
+
+	if(is_write) { 
+		if (is_priv)
+			cmd = (is_cl45) ? MTKETH_MII_WRITE_CL45 :
+					  MTKETH_MII_WRITE;
+		else
+			cmd = SIOCSMIIREG;
+
+		__phy_op(ifname,phy_id,reg_num, &val, cmd, is_priv);
+
+		if(is_cl45)
+			printf("Set: port%x dev%Xh_reg%Xh = 0x%04X\n",port, dev, reg_num, val);
+		else
+			printf("Set: phy[%x].reg[%x] = %04x\n",port, reg_num, val);
+	}	
+	else {
+		if (is_priv)
+			cmd = (is_cl45) ? MTKETH_MII_READ_CL45 :
+					  MTKETH_MII_READ;
+		else
+			cmd = SIOCGMIIREG;
+
+		__phy_op(ifname,phy_id,reg_num, &val, cmd, is_priv);
+
+		if(is_cl45)
+			printf("Get: port%x dev%Xh_reg%Xh = 0x%04X\n",port, dev, reg_num, val);
+		else
+			printf("Get: phy[%x].reg[%x] = %04x\n",port, reg_num, val);
+	
+	}
+
+	return 0;	
+}
diff --git a/recipes-devtools/mii-mgr/files/mii_mgr.h b/recipes-devtools/mii-mgr/files/mii_mgr.h
new file mode 100644
index 0000000..66f7a61
--- /dev/null
+++ b/recipes-devtools/mii-mgr/files/mii_mgr.h
@@ -0,0 +1,20 @@
+/*
+ * switch_ioctl.h: switch(ioctl) set API
+ */
+
+#ifndef MII_MGR_H
+#define MII_MGR_H
+
+#define MTKETH_MII_READ		0x89F3
+#define MTKETH_MII_WRITE	0x89F4
+#define MTKETH_MII_READ_CL45	0x89FC
+#define MTKETH_MII_WRITE_CL45	0x89FD
+
+struct mtk_mii_ioctl_data {
+        __u16 phy_id;
+        __u16 reg_num;
+        __u32 val_in;
+        __u32 val_out;
+};
+
+#endif /* MII_MGR_H */
diff --git a/recipes-devtools/mii-mgr/mii-mgr_1.0.bb b/recipes-devtools/mii-mgr/mii-mgr_1.0.bb
new file mode 100644
index 0000000..9a16340
--- /dev/null
+++ b/recipes-devtools/mii-mgr/mii-mgr_1.0.bb
@@ -0,0 +1,22 @@
+SUMMARY = "An program to read/write from/to phy from userspace"
+SECTION = "applications"
+LICENSE = "GPLv2"
+LIC_FILES_CHKSUM = "file://COPYING;md5=c188eeeb69c0a05d0545816f1458a0c9"
+
+S = "${WORKDIR}"
+
+SRC_URI = " \
+    file://COPYING \
+    file://mii_mgr.c \
+    file://mii_mgr.h \
+    "
+
+do_compile() {
+    ${CC} ${LDFLAGS} mii_mgr.c -o mii_mgr
+}
+
+do_install() {
+    install -d ${D}${sbindir}
+    install -m 0755 ${WORKDIR}/mii_mgr ${D}${sbindir}
+    install -m 0755 ${WORKDIR}/mii_mgr ${D}${sbindir}/mii_mgr_cl45
+}
diff --git a/recipes-devtools/regs/files/COPYING b/recipes-devtools/regs/files/COPYING
new file mode 100644
index 0000000..7975bdb
--- /dev/null
+++ b/recipes-devtools/regs/files/COPYING
@@ -0,0 +1 @@
+Copyright (c) Mediatek 2020
diff --git a/recipes-devtools/regs/files/regs.c b/recipes-devtools/regs/files/regs.c
new file mode 100644
index 0000000..93a3532
--- /dev/null
+++ b/recipes-devtools/regs/files/regs.c
@@ -0,0 +1,155 @@
+/*
+ * pcimem.c: Simple program to read/write from/to a pci device from userspace.
+ *
+ *  Copyright (C) 2010, Bill Farrow (bfarrow@beyondelectronics.us)
+ *
+ *  Based on the devmem2.c code
+ *  Copyright (C) 2000, Jan-Derk Bakker (J.D.Bakker@its.tudelft.nl)
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+#include <signal.h>
+#include <fcntl.h>
+#include <ctype.h>
+#include <termios.h>
+#include <sys/types.h>
+#include <sys/mman.h>
+
+#define PRINT_ERROR \
+	do { \
+		fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n", \
+		__LINE__, __FILE__, errno, strerror(errno)); exit(1); \
+	} while(0)
+
+#define MAP_SIZE 4096UL
+#define MAP_MASK (MAP_SIZE - 1)
+
+void dump_page(uint32_t *vaddr, uint32_t *vbase, uint32_t *pbase){
+	
+	int i =0;
+	uint32_t *end = vaddr + (MAP_SIZE >> 6);
+	uint32_t *start = vaddr;
+
+	while(start  < end) {
+		printf("%p:%08x %08x %08x %08x\n",
+			start - vbase + pbase, start[0], start[1] , start[2], start[3]);
+		start+=4;
+	}
+}
+
+void reg_mod_bits(uint32_t *virt_addr, int data, int  start_bit, int data_len)
+{
+    int mask=0;
+    int value;
+    int i;
+
+    for (i = 0; i < data_len; i++) {
+        mask |= 1 << (start_bit + i);
+    }
+
+    value = *((volatile uint32_t *) virt_addr);
+    value &= ~mask;
+    value |= (data << start_bit) & mask;;
+
+    *((uint32_t *) virt_addr) = value;
+}
+
+void usage(void)
+{
+		fprintf(stderr, "\nUsage:\tregs [Type] [ Offset:Hex ] [ Data:Hex ] [StartBit:Dec] [DataLen:Dec]\n"
+			"\tType    : access operation type : [m]odify, [w]wite, [d]ump\n"
+			"\tOffset  : offset into memory region to act upon\n"
+			"\tData    : data to be written\n"
+			"\tStartbit: Startbit of Addr that want to be modified\n"
+			"\tDataLen : Data length of Data\n\n"
+			"Example:\tRead/Write/Modify register \n"
+			"\tRead    : regs d 0x1b100000           //dump 0x1b100000~0x1b1000f0 \n"
+			"\tWrite   : regs w 0x1b100000 0x1234    //write 0x1b100000=0x1234\n"
+			"\tModify  : regs m 0x1b100000 0x0 29 3  //modify 0x1b100000[29:31]=0\n");
+}
+
+int main(int argc, char **argv) {
+	int fd;
+	void *map_base = NULL;
+        void *virt_addr = NULL;
+	uint32_t read_result =0;
+        uint32_t writeval = 0;
+	uint32_t startbit = 0;
+       	uint32_t datalen = 0;
+	char *filename = NULL;
+	off_t offset = 0;
+	int access_type = 0;
+
+	if(argc < 2) {
+		usage();
+		exit(1);
+	}
+
+	access_type = tolower(argv[1][0]);
+	if((access_type == 'd' && argc < 3) || (access_type == 'w' && argc < 4) || (access_type == 'm' && argc < 6)) {
+		usage();
+		exit(1);
+	}
+
+	filename = "/dev/mem";
+	if((fd = open(filename, O_RDWR | O_SYNC)) == -1) 
+		PRINT_ERROR;
+
+	/* Map one page */
+	offset = strtoul(argv[2], NULL, 16);
+	map_base = mmap(0, 2*MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset & ~MAP_MASK);
+	if(map_base == (void *) -1) 
+		PRINT_ERROR;
+
+	virt_addr = map_base + (offset & MAP_MASK);
+	read_result = *((volatile uint32_t *) virt_addr);
+	printf("Value at 0x%X (%p): 0x%X\n", offset, virt_addr, read_result);
+
+	switch(access_type) {
+		case 'm':
+			writeval = strtoul(argv[3], 0, 16);
+			startbit = strtoul(argv[4], 0, 10);
+			datalen  = strtoul(argv[5], 0, 10);
+			reg_mod_bits((uint32_t *)virt_addr, writeval, startbit, datalen);
+			break;
+		case 'w':
+			writeval = strtoul(argv[3], 0, 16);
+			*((uint32_t *) virt_addr) = writeval;
+			break;
+		case 'd':
+			dump_page(virt_addr, map_base, (uint32_t *)(offset & ~MAP_MASK));
+			return;
+		default:
+			fprintf(stderr, "Illegal data type '%c'.\n", access_type);
+			exit(2);
+	}
+
+	read_result = *((volatile uint32_t *) virt_addr);
+	printf("Written 0x%X; Readback 0x%X\n", writeval, read_result);
+
+	if(munmap(map_base, MAP_SIZE) == -1) 
+		PRINT_ERROR;
+
+	close(fd);
+	return 0;
+}
diff --git a/recipes-devtools/regs/regs_1.0.bb b/recipes-devtools/regs/regs_1.0.bb
new file mode 100644
index 0000000..80666fa
--- /dev/null
+++ b/recipes-devtools/regs/regs_1.0.bb
@@ -0,0 +1,20 @@
+SUMMARY = "An program to read/write from/to a pci device from userspace"
+SECTION = "applications"
+LICENSE = "GPLv2"
+LIC_FILES_CHKSUM = "file://COPYING;md5=c188eeeb69c0a05d0545816f1458a0c9"
+
+S = "${WORKDIR}"
+
+SRC_URI = " \
+    file://COPYING \
+    file://regs.c \
+    "
+
+do_compile() {
+    ${CC} ${CFLAGS} ${LDFLAGS} regs.c -o regs
+}
+
+do_install() {
+    install -d ${D}${base_bindir}
+    install -m 0755 ${WORKDIR}/regs ${D}${base_bindir}
+}