[][MAC80211][mt76][Implement pre-cal support in testmode]

[Description]
Add atenl and mt76-test commands for getting pre-cal data.

[Release-log]
N/A

Change-Id: Ib113465d0ce1c8e9c8d5461e6911ba26c0afde1c
Reviewed-on: https://gerrit.mediatek.inc/c/openwrt/feeds/mtk_openwrt_feeds/+/6365523
diff --git a/feed/atenl/src/eeprom.c b/feed/atenl/src/eeprom.c
index ee77ed7..1c1cb6c 100644
--- a/feed/atenl/src/eeprom.c
+++ b/feed/atenl/src/eeprom.c
@@ -6,7 +6,7 @@
 
 #include "atenl.h"
 
-#define EEPROM_PART_SIZE 20480
+#define EEPROM_PART_SIZE 0x64000
 char *eeprom_file;
 
 static FILE *mtd_open(const char *mtd)
@@ -36,7 +36,7 @@
 static int
 atenl_flash_create_file(struct atenl *an)
 {
-#define READ_LEN_LIMIT	20000
+#define READ_LEN_LIMIT	0x64000
 	char buf[1024];
 	ssize_t len, limit = 0;
 	FILE *f;
@@ -47,6 +47,7 @@
 		atenl_err("Failed to open MTD device\n");
 		return -1;
 	}
+	fseek(f, an->mtd_offset, SEEK_SET);
 
 	fd = open(eeprom_file, O_RDWR | O_CREAT | O_EXCL, 00644);
 	if (fd < 0)
@@ -212,11 +213,13 @@
 	switch (an->chip_id) {
 	case 0x7915:
 		an->eeprom_size = 3584;
+		an->eeprom_prek_offs = 0x62;
 		break;
 	case 0x7906:
 	case 0x7916:
 	case 0x7986:
 		an->eeprom_size = 4096;
+		an->eeprom_prek_offs = 0x19a;
 		break;
 	default:
 		break;
@@ -309,20 +312,24 @@
 	bool flash_mode;
 	int eeprom_fd;
 	char buf[30];
+	u8 main_phy_idx = phy_idx;
 
 	set_band_val(an, 0, phy_idx, phy_idx);
-	snprintf(buf, sizeof(buf), "/tmp/atenl-eeprom-phy%u", phy_idx);
-	eeprom_file = strdup(buf);
-
 	atenl_nl_check_mtd(an);
 	flash_mode = an->mtd_part != NULL;
 
+	if (flash_mode)
+		main_phy_idx = an->is_main_phy ? main_phy_idx : (main_phy_idx - 1);
+
+	snprintf(buf, sizeof(buf), "/tmp/atenl-eeprom-phy%u", main_phy_idx);
+	eeprom_file = strdup(buf);
+
 	eeprom_fd = atenl_eeprom_init_file(an, flash_mode);
 	if (eeprom_fd < 0)
 		return -1;
 
 	an->eeprom_data = mmap(NULL, EEPROM_PART_SIZE, PROT_READ | PROT_WRITE,
-			       MAP_SHARED, eeprom_fd, an->mtd_offset);
+			       MAP_SHARED, eeprom_fd, 0);
 	if (!an->eeprom_data) {
 		perror("mmap");
 		close(eeprom_fd);
@@ -355,10 +362,35 @@
 	free(eeprom_file);
 }
 
+int atenl_eeprom_update_precal(struct atenl *an, int write_offs, int size)
+{
+	u32 offs = an->eeprom_prek_offs;
+	u8 cal_indicator, *eeprom, *pre_cal;
+
+	if (!an->cal && !an->cal_info)
+		return 0;
+
+	eeprom = an->eeprom_data;
+	pre_cal = eeprom + an->eeprom_size;
+	cal_indicator = an->cal_info[4];
+
+	memcpy(eeprom + offs, &cal_indicator, sizeof(u8));
+	memcpy(pre_cal, an->cal_info, PRE_CAL_INFO);
+	pre_cal += (PRE_CAL_INFO + write_offs);
+
+	if (an->cal)
+		memcpy(pre_cal, an->cal, size);
+	else
+		memset(pre_cal, 0, size);
+
+	return 0;
+}
+
 int atenl_eeprom_write_mtd(struct atenl *an)
 {
 	bool flash_mode = an->mtd_part != NULL;
 	pid_t pid;
+	char offset[10];
 
 	if (!flash_mode)
 		return 0;
@@ -368,9 +400,10 @@
 		perror("Fork");
 		return EXIT_FAILURE;
 	} else if (pid == 0) {
-		char *part = strdup(an->mtd_part);
-		char *cmd[] = {"mtd", "write", eeprom_file, part, NULL};
 		int ret;
+		char *part = strdup(an->mtd_part);
+		snprintf(offset, sizeof(offset), "%d", an->mtd_offset);
+		char *cmd[] = {"mtd", "-p", offset, "write", eeprom_file, part, NULL};
 
 		ret = execvp("mtd", cmd);
 		if (ret < 0) {
@@ -500,6 +533,27 @@
 
 			atenl_info("val = 0x%x (%u)\n", an->eeprom_data[offset],
 							an->eeprom_data[offset]);
+		} else if (!strncmp(s, "precal", 6)) {
+			s = strchr(s, ' ');
+			if (!s)
+				return;
+			s++;
+
+			if (!strncmp(s, "sync group", 10)) {
+				atenl_nl_precal_sync_from_driver(an, PREK_SYNC_GROUP);
+			} else if (!strncmp(s, "sync dpd 2g", 11)) {
+				atenl_nl_precal_sync_from_driver(an, PREK_SYNC_DPD_2G);
+			} else if (!strncmp(s, "sync dpd 5g", 11)) {
+				atenl_nl_precal_sync_from_driver(an, PREK_SYNC_DPD_5G);
+			} else if (!strncmp(s, "sync dpd 6g", 11)) {
+				atenl_nl_precal_sync_from_driver(an, PREK_SYNC_DPD_6G);
+			} else if (!strncmp(s, "group clean", 11)) {
+				atenl_nl_precal_sync_from_driver(an, PREK_CLEAN_GROUP);
+			} else if (!strncmp(s, "dpd clean", 9)) {
+				atenl_nl_precal_sync_from_driver(an, PREK_CLEAN_DPD);
+			} else if (!strncmp(s, "sync", 4)) {
+				atenl_nl_precal_sync_from_driver(an, PREK_SYNC_ALL);
+			}
 		} else {
             atenl_err("Unknown eeprom command: %s\n", cmd);
         }