From 31441281694a585caadd29af1dab396914b7577a Mon Sep 17 00:00:00 2001 From: Wang Jinwei Date: Tue, 15 Aug 2023 17:02:10 +0800 Subject: [PATCH 01/12] Support to update smbios online Support List: (SMBIOS SPEC 3.6.0) Type1 -Offset 04h Manufacturer -Offset 05h Product Name -Offset 06h Version -Offset 07h Serial Number -Offset 08h UUID -Offset 18h SKU Number -Offset 1Ah Family Type2 -Offset 04h Manufacturer -Offset 05h Product -Offset 06h Version -Offset 07h Serial Number -Offset 08h Asset Tag Type3 -Offset 04h Manufacturer -Offset 06h Version -Offset 07h Serial Number -Offset 08h Asset Tag Number -Offset 15h + n*m SKU Number Signed-off-by: Wang Jinwei Signed-off-by: Dongyan Qian --- LoongArch/Makefile | 4 +- LoongArch/smbios.c | 206 +++++++++++++++++++++++++++++++++++++++++++++ LoongArch/spi.c | 9 +- README.md | 1 + 4 files changed, 217 insertions(+), 3 deletions(-) create mode 100644 LoongArch/smbios.c diff --git a/LoongArch/Makefile b/LoongArch/Makefile index 294bb70..731fb6a 100644 --- a/LoongArch/Makefile +++ b/LoongArch/Makefile @@ -1,6 +1,6 @@ -COMPILE_FILE_C = main.c gpio.c rtc.c acpi.c conf.c pci.c util.c spi.c mps.c i2c.c spd.c ht.c argparse.c process.c -COMPILE_FILE_O = main.o gpio.o rtc.o acpi.o conf.o pci.o util.o spi.o mps.o i2c.o spd.o ht.o argparse.o process.o +COMPILE_FILE_C = main.c gpio.c rtc.c acpi.c conf.c pci.c util.c spi.c mps.c i2c.c spd.c ht.c argparse.c process.c smbios.c +COMPILE_FILE_O = main.o gpio.o rtc.o acpi.o conf.o pci.o util.o spi.o mps.o i2c.o spd.o ht.o argparse.o process.o smbios.o #COMPILE_PATH = /opt/LoongArch_Toolchains/loongarch64-linux-gnu-2021-06-19-vector/bin/loongarch64-linux-gnu-gcc #COMPILE_PATH = /opt/LoongArch_Toolchains/loongarch64-linux-gnu-2021-06-19-vector/bin/loongarch64-linux-gnu-gcc diff --git a/LoongArch/smbios.c b/LoongArch/smbios.c new file mode 100644 index 0000000..b8332ad --- /dev/null +++ b/LoongArch/smbios.c @@ -0,0 +1,206 @@ +#include +#include +#include +#include +#include +#include +#include +#include "def.h" + +#pragma pack(1) +typedef struct { + UINT8 Type; + UINT8 StrNum; + UINT64 HOffset; + UINT64 NOffset; + UINT64 ResultH; + UINT64 ResultL; + UINT32 StrSize; +} SmbiosHeader; +#pragma pack() + +const char *commandLineHelp = + "\n" + "Usage: spi -s (-type) number (-number) number (-string) string\n" + "\n" + "Now only support below command, Examples:\n" + " spi -s 1 1 Manufacture # update smbios type1 Manufacture\n" + " spi -s 1 2 ProductName # update smbios type1 ProductName\n" + " spi -s 1 3 Version # update smbios type1 Version\n" + " spi -s 1 4 SerialNumber # update smbios type1 SerialNumber\n" + " spi -s 1 5 SKUNumber # update smbios type1 SKUNumber\n" + " spi -s 1 6 Family # update smbios type1 Family\n" + " spi -s 1 7 0011223344556677 8899aabbccddeeff # update smbios type1 UUID\n" + " spi -s 2 1 Manufacture # update smbios type2 Manufacture\n" + " spi -s 2 2 Product # update smbios type2 Product\n" + " spi -s 2 3 Version # update smbios type2 Version\n" + " spi -s 2 4 SerialNumber # update smbios type2 SerialNumber\n" + " spi -s 2 5 AssetTag # update smbios type2 AssetTag\n" + " spi -s 3 1 Manufacture # update smbios type3 Manufacture\n" + " spi -s 3 2 Version # update smbios type3 Version\n" + " spi -s 3 3 SerialNumber # update smbios type3 SerialNumber\n" + " spi -s 3 4 AssetTag # update smbios type3 AssetTag\n" + " spi -s 3 5 SKUNumber # update smbios type3 SKUNumber\n" + "\n" + ; + +extern UINT64 SPI_REG_BASE; +VOID SpiFlashSafeWrite ( + UINTN Offset, + VOID *Buffer, + UINTN Num + ); + +int spi_update_smbios() +{ + void * p = NULL; + unsigned long long devaddr; + char RecordName[0x40]; + char RecordName1[0x40]; + wchar_t RecordName2[16]; + wchar_t RecordName3[16]; + int status; + SmbiosHeader *smbiosheader; + UINT64 t = 0; + UINT64 n = 0; + UINT64 s = 0; + UINT64 l = 0; + UINT32 offset = 0x3D000; //smbiosheader addr + UINT32 size = 1024*4; + UINT32 nameoffset = 0x4E000; //nameheader addr + UINT32 namesize = 1024*4; + UINT32 Index; + UINT8 temptype; + UINT8 tempnum; + + Index = 0; + smbiosheader = NULL; + devaddr = 0x1fe001f0; + + int fd = open ("/dev/mem", O_RDWR|O_SYNC); + if(fd < 0) { + printf("can't open file,please use root .\n"); + return 1; + } + + /*Transfer Virtul to Phy Addr*/ + p = vtpa(devaddr,fd); + SPI_REG_BASE = (UINTN)p; + + printf("%s", commandLineHelp); + + printf("Please input smbios type (only support 1/2/3):"); + + status = scanf("%s",RecordName); //1.smbios type + if (RecordName != NULL) { + //printf("RecordName: (%s)\n", RecordName); + t = atoi(RecordName); + printf("t: (%x)\n", t); + if ((t > 3) || (t <= 0)){ + printf("Unsupport smbios type! Please retry!\n"); + return 1; + } + + printf("\n"); + printf("Note: if you input smbiostype is 1 in the previous step, the string number only support 1/2/3/4/5/6/7.\n"); + printf(" if you input smbiostype is 2 or 3 in the previous step, the string number only support 1/2/3/4/5.\n"); + printf("\n"); + printf("Please input smbios string number (only support 1/2/3/4/5/6/7):"); + + status = scanf("%s",RecordName1); //2. smbios string number + if (RecordName1 != NULL) { + //printf("RecordName1: (%s)\n", RecordName1); + n = atoi(RecordName1); + printf("n: (%x)\n", n); + if ((n > 7) || (n <= 0)){ + printf("Unsupport string number! Please retry!"); + return 1; + } + + if ((t > 1) && (n > 5)){ + printf("Unsupport string number! Please retry!"); + return 1; + } + + if ((t == 1) && (n == 1)) Index = 0; + if ((t == 1) && (n == 2)) Index = 1; + if ((t == 1) && (n == 3)) Index = 2; + if ((t == 1) && (n == 4)) Index = 3; + if ((t == 1) && (n == 5)) Index = 4; + if ((t == 1) && (n == 6)) Index = 5; + if ((t == 1) && (n == 7)) Index = 6; + if ((t == 2) && (n == 1)) Index = 7; + if ((t == 2) && (n == 2)) Index = 8; + if ((t == 2) && (n == 3)) Index = 9; + if ((t == 2) && (n == 4)) Index = 10; + if ((t == 2) && (n == 5)) Index = 11; + if ((t == 3) && (n == 1)) Index = 12; + if ((t == 3) && (n == 2)) Index = 13; + if ((t == 3) && (n == 3)) Index = 14; + if ((t == 3) && (n == 4)) Index = 15; + if ((t == 3) && (n == 5)) Index = 16; + + offset += Index * size; + nameoffset += Index * namesize; + + smbiosheader = malloc(sizeof(SmbiosHeader)); + if (smbiosheader == NULL) { + return 1; + } + + //printf("(%x)-(0x%lx)-(0x%lx)\n", Index, offset, nameoffset); + + smbiosheader->Type = t; + smbiosheader->StrNum = n; + smbiosheader->HOffset = offset; + smbiosheader->NOffset = nameoffset; + + if (Index == 6) { + printf("Please Input smbios HEX1(64):"); + } else { + printf("Please Input smbios strings:"); + } + status = scanf("%s",RecordName2); //3.smbios string + + if (RecordName2 != NULL) { + //printf("RecordName2: (%s)-(%d)\n", RecordName2, (strlen((char *)RecordName2) + 1) * 2); + smbiosheader->StrSize = (strlen((char *)RecordName2) + 1) * 2; + if (Index == 6) { + printf("Please Input smbios HEX2(64):"); + status = scanf("%s",RecordName3); + if (RecordName3 != NULL) { + //printf("RecordName3: (%s)\n", RecordName3); + char *endptr1; + char *endptr2; + + s = strtoull((char *)RecordName2, &endptr1, 16); + l = strtoull((char *)RecordName3, &endptr2, 16); + + if ((*endptr1 == '\0') && (*endptr2 == '\0')) { + //printf("(0x%lx)-(0x%lx)\n", s, l); + smbiosheader->ResultH = s; + smbiosheader->ResultL = l; + printf("\n"); + } else { + printf("Unsupport string! please retry!\n"); + return 1; + } + } + } else { + smbiosheader->ResultH = 0; + smbiosheader->ResultL = 0; + } + + SpiFlashSafeWrite(0 + nameoffset, RecordName2, namesize); + SpiFlashSafeWrite(0 + offset, smbiosheader, size); + } + } + } + + status = releaseMem(p); + close(fd); + free(smbiosheader); + printf("Program OK.\n"); + + return status; +} diff --git a/LoongArch/spi.c b/LoongArch/spi.c index 75eb0d5..5ad0845 100644 --- a/LoongArch/spi.c +++ b/LoongArch/spi.c @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -949,6 +950,8 @@ static int spi_read_flash (const char* addr, int count) return status; } +int spi_update_smbios(); + static const char *const spi_usages[] = { PROGRAM_NAME" spi ", NULL, @@ -961,6 +964,7 @@ int cmd_spi (int argc, const char **argv) int flag_dump = 0; int flag_gmac = 0; int flag_tcm = 0; + int flag_smbios = 0; int count = 0; int id = 0; const char *file = NULL; @@ -977,6 +981,7 @@ int cmd_spi (int argc, const char **argv) OPT_BOOLEAN ('d', "dump", &flag_dump, "dump the ls3a spi flash", NULL, 0, 0), OPT_BOOLEAN ('g', "gmac", &flag_gmac, "update gmac flash", NULL, 0, 0), OPT_BOOLEAN ('t', "tcm", &flag_tcm, "read ls7a tcm from address", NULL, 0, 0), + OPT_BOOLEAN ('s', "smbios", &flag_smbios, "update smbios ls3a spi flash", NULL, 0, 0), OPT_GROUP ("Arguments:"), OPT_STRING ('f', "file", &file, "file path to read/write", NULL, 0, 0), OPT_STRING ('a', "address", &addr, "Pci's spi control address(e.g. 1fe001f0)", NULL, 0, 0), @@ -989,7 +994,7 @@ int cmd_spi (int argc, const char **argv) argparse_init (&argparse, options, spi_usages, 0); argc = argparse_parse (&argparse, argc, argv); - if (!(flag_update || flag_dump || flag_read || flag_tcm || flag_gmac)) { + if (!(flag_update || flag_dump || flag_read || flag_tcm || flag_gmac || flag_smbios)) { argparse_usage(&argparse); return 1; } @@ -1041,6 +1046,8 @@ int cmd_spi (int argc, const char **argv) return 1; } spi_update_gmac(addr, id, mac); + } else if (flag_smbios) { + spi_update_smbios(); } return 0; diff --git a/README.md b/README.md index ea118b5..11b0c14 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ Options: -d, --dump dump the ls3a spi flash -g, --gmac update gmac flash -t, --tcm read ls7a tcm from address + -s, update smbios ls3a spi flash Arguments: -f, --file= file path to read/write From af309fa75c6b1a7ecde7ef82bb98b12d0f6b0607 Mon Sep 17 00:00:00 2001 From: Dongyan Qian Date: Mon, 4 Dec 2023 13:57:09 +0800 Subject: [PATCH 02/12] spi: adjust fd size to 8M Signed-off-by: Dongyan Qian --- LoongArch/spi.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/LoongArch/spi.c b/LoongArch/spi.c index 5ad0845..018fd99 100644 --- a/LoongArch/spi.c +++ b/LoongArch/spi.c @@ -12,7 +12,7 @@ //#define SPI_ADDR 0x1fe001f0 #define SPI_ADDR 0 //Get at runtime -#define FLASH_SIZE 4128768 +#define FLASH_SIZE 0x800000 #define GPIO_0 (0x1<<0) #define GPIO_1 (0x1<<1) @@ -89,9 +89,9 @@ #define TRUE 1 #define FALSE 0 -#define INVALID_OFFSET(x) ((x > 0x400000)||(x < 0x0)) ? TRUE:FALSE +#define INVALID_OFFSET(x) ((x > FLASH_SIZE)||(x < 0x0)) ? TRUE:FALSE #define TCM_INVALID_OFFSET(x) ((x > 0xffffffff)||(x < 0x0)) ? TRUE:FALSE -#define INVALID_NUM(x) ((x > 0x400000)||( x <= 0x0)) ? TRUE:FALSE +#define INVALID_NUM(x) ((x > FLASH_SIZE)||( x <= 0x0)) ? TRUE:FALSE #define IS_SST25VF032B(M,D,C) ((M == 0xBF)&&(D == 0x25)&&(C == 0x4A)) ? TRUE:FALSE static UINT8 ValueRegSpcr = 0xFF; From faff9dae072e829c639edaefb61ef84695ec9e72 Mon Sep 17 00:00:00 2001 From: Dongyan Qian Date: Mon, 4 Dec 2023 15:47:33 +0800 Subject: [PATCH 03/12] Add Mips64el/3a4000 smbiosupdate support Signed-off-by: Bo Zhu Signed-off-by: Dongyan Qian --- LoongArch/Makefile | 11 ++++++++++- LoongArch/smbios.c | 14 ++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/LoongArch/Makefile b/LoongArch/Makefile index 731fb6a..f406eeb 100644 --- a/LoongArch/Makefile +++ b/LoongArch/Makefile @@ -6,6 +6,15 @@ COMPILE_FILE_O = main.o gpio.o rtc.o acpi.o conf.o pci.o util.o spi.o mps.o i2c. #COMPILE_PATH = /opt/LoongArch_Toolchains/loongarch64-linux-gnu-2021-06-19-vector/bin/loongarch64-linux-gnu-gcc COMPILE_PATH = gcc +ARCH := $(shell uname -m) +ifeq ($(ARCH),loongarch64) +CFLAGS = -DPLATFORM_LA64 +else ifeq ($(ARCH),mips64) +CFLAGS = -DPLATFORM_MIPS64 +else + $(warning Unsupported architecture: $(ARCH)) +endif + all: OsTools ##.PHONY all SEnd OsTools : OsTools.o @@ -13,7 +22,7 @@ OsTools : OsTools.o chmod 777 OsTools OsTools.o : #$(COMPILE_PATH) -g -c $(COMPILE_FILE_C) - $(COMPILE_PATH) -c $(COMPILE_FILE_C) + $(COMPILE_PATH) $(CFLAGS) -c $(COMPILE_FILE_C) clean : rm *.o OsTools diff --git a/LoongArch/smbios.c b/LoongArch/smbios.c index b8332ad..bc61e01 100644 --- a/LoongArch/smbios.c +++ b/LoongArch/smbios.c @@ -7,7 +7,19 @@ #include #include "def.h" +#ifdef PLATFORM_LA64 +UINT32 offset = 0x3D000; //smbiosheader addr +UINT32 nameoffset = 0x4E000; //nameheader addr +#elif defined(PLATFORM_MIPS64) // mips64el +UINT32 offset = 0x1E000; //smbiosheader addr +UINT32 nameoffset = 0x2F000; //nameheader addr +#else +UINT32 offset = 0x3D000; //smbiosheader addr +UINT32 nameoffset = 0x4E000; //nameheader addr +#endif + #pragma pack(1) + typedef struct { UINT8 Type; UINT8 StrNum; @@ -65,9 +77,7 @@ int spi_update_smbios() UINT64 n = 0; UINT64 s = 0; UINT64 l = 0; - UINT32 offset = 0x3D000; //smbiosheader addr UINT32 size = 1024*4; - UINT32 nameoffset = 0x4E000; //nameheader addr UINT32 namesize = 1024*4; UINT32 Index; UINT8 temptype; From a6fece930c73510c5ddb424275eb44425fafd00a Mon Sep 17 00:00:00 2001 From: Dongyan Qian Date: Mon, 4 Dec 2023 15:48:40 +0800 Subject: [PATCH 04/12] Version: Change Version to V1.2 --- LoongArch/def.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LoongArch/def.h b/LoongArch/def.h index 537688d..51b81d8 100644 --- a/LoongArch/def.h +++ b/LoongArch/def.h @@ -9,7 +9,7 @@ #endif #define PROGRAM_NAME "OsTools" -#define PROGRAM_VERSION "1.1" +#define PROGRAM_VERSION "1.2" #define LS7A_CONF_BASE_ADDR 0x10010000 #define LS7A_MISC_BASE_ADDR 0x10080000 From 95852b0f8ed85d98960c83c89bf5014b47d8cb93 Mon Sep 17 00:00:00 2001 From: Chen Zhang Date: Fri, 15 Dec 2023 10:45:10 +0800 Subject: [PATCH 05/12] Support for updating smbios to 7A flash Signed-off-by: Chen Zhang --- LoongArch/smbios.c | 11 +++++++++-- LoongArch/spi.c | 4 ++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/LoongArch/smbios.c b/LoongArch/smbios.c index bc61e01..a6078ad 100644 --- a/LoongArch/smbios.c +++ b/LoongArch/smbios.c @@ -63,7 +63,7 @@ VOID SpiFlashSafeWrite ( UINTN Num ); -int spi_update_smbios() +int spi_update_smbios(const char *addr) { void * p = NULL; unsigned long long devaddr; @@ -85,7 +85,14 @@ int spi_update_smbios() Index = 0; smbiosheader = NULL; - devaddr = 0x1fe001f0; + if(addr != NULL){ + sscanf (addr,"%lx", &devaddr); + devaddr &= 0xfffffffffffffff0ULL; + offset = 0x41000; //7A flash smbiosheader offset + nameoffset = 0x52000; //7A flash nameheader offset + } else { + devaddr = 0x1fe001f0; + } int fd = open ("/dev/mem", O_RDWR|O_SYNC); if(fd < 0) { diff --git a/LoongArch/spi.c b/LoongArch/spi.c index 018fd99..889e836 100644 --- a/LoongArch/spi.c +++ b/LoongArch/spi.c @@ -950,7 +950,7 @@ static int spi_read_flash (const char* addr, int count) return status; } -int spi_update_smbios(); +int spi_update_smbios(const char *addr); static const char *const spi_usages[] = { PROGRAM_NAME" spi ", @@ -1047,7 +1047,7 @@ int cmd_spi (int argc, const char **argv) } spi_update_gmac(addr, id, mac); } else if (flag_smbios) { - spi_update_smbios(); + spi_update_smbios(addr); } return 0; From abc41b345bc0e178c496262ec2d2604a75beb0e6 Mon Sep 17 00:00:00 2001 From: Dongyan Qian Date: Thu, 11 Apr 2024 19:06:46 +0800 Subject: [PATCH 06/12] spi: add param spi_dump --address and spi_update --Size specify --dump for 7a flash and --Size for updatebios Signed-off-by: Dongyan Qian --- LoongArch/spi.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/LoongArch/spi.c b/LoongArch/spi.c index 889e836..4999e40 100644 --- a/LoongArch/spi.c +++ b/LoongArch/spi.c @@ -12,7 +12,9 @@ //#define SPI_ADDR 0x1fe001f0 #define SPI_ADDR 0 //Get at runtime -#define FLASH_SIZE 0x800000 + +unsigned int Size = 0; +#define FLASH_SIZE (Size ? (Size * 0x100000) : 0x800000) #define GPIO_0 (0x1<<0) #define GPIO_1 (0x1<<1) @@ -881,13 +883,17 @@ static int spi_update_flash (const char *path) return status; } -static int spi_dump_flash (const char *path) +static int spi_dump_flash (const char *path, const char *addr) { void *p = NULL; int status ; unsigned long long devaddr; - devaddr = 0x1fe001f0; + if (addr != NULL) { + sscanf (addr,"%lx", &devaddr); + } else { + devaddr = 0x1fe001f0; + } int fd = open ("/dev/mem", O_RDWR |O_SYNC); if(fd < 0) { @@ -988,6 +994,7 @@ int cmd_spi (int argc, const char **argv) OPT_INTEGER ('i', "id", &id, "Mac id", NULL, 0, 0), OPT_STRING ('m', "mac", &mac, "Mac address(e.g. 00:11:22:33:44:55)", NULL, 0, 0), OPT_INTEGER ('c', "count", &count, "read count", NULL, 0, 0), + OPT_INTEGER ('S', "Size", &Size, "Flush Size", NULL, 0, 0), OPT_END (), }; @@ -1023,7 +1030,7 @@ int cmd_spi (int argc, const char **argv) printf ("Please setup the file.\n"); return 1; } - spi_dump_flash (file); + spi_dump_flash (file, addr); } else if (flag_read) { if (addr == NULL) { printf ("Please setup the address.\n"); From 98289337fd58eddb682d1b8f2ea3a22cb7580cdc Mon Sep 17 00:00:00 2001 From: Dongyan Qian Date: Mon, 15 Apr 2024 10:52:29 +0800 Subject: [PATCH 07/12] spi_update_flash: adapt flush size according file size Signed-off-by: Dongyan Qian --- LoongArch/spi.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/LoongArch/spi.c b/LoongArch/spi.c index 4999e40..c4668c9 100644 --- a/LoongArch/spi.c +++ b/LoongArch/spi.c @@ -7,6 +7,7 @@ #include "def.h" #include "file.h" #include "process.h" +#include #define readw(addr) (*(volatile unsigned int *)(addr)) @@ -14,7 +15,7 @@ #define SPI_ADDR 0 //Get at runtime unsigned int Size = 0; -#define FLASH_SIZE (Size ? (Size * 0x100000) : 0x800000) +#define FLASH_SIZE (Size ? Size : 0x800000) #define GPIO_0 (0x1<<0) #define GPIO_1 (0x1<<1) @@ -853,6 +854,7 @@ static int spi_update_flash (const char *path) void *p = NULL; int status ; unsigned long long devaddr; + struct stat statbuf; devaddr = 0x1fe001f0; @@ -862,6 +864,18 @@ static int spi_update_flash (const char *path) return 1; } + // get file size + if (stat (path, &statbuf) == 0) { + printf ("The file size %s is %#lx bytes.\n", path, statbuf.st_size); + if (Size != 0) { + printf ("specify Flush Size %#lx .\n",Size); + } else { + Size = statbuf.st_size; + } + } else { + printf ("Failed to get file status.\n"); + } + /*Transfer Virtul to Phy Addr*/ p = vtpa(devaddr, fd); SPI_REG_BASE = (UINTN)p; @@ -873,6 +887,7 @@ static int spi_update_flash (const char *path) printf("Read File Error , PATH error!!!\n"); return 1; } + fread(buf, FLASH_SIZE, 1, pfile); printf("------------Read Buf Get Success!-----------\n"); From 965d5572e07e8a483ce4809dd272fedba3f65d9f Mon Sep 17 00:00:00 2001 From: Dongyan Qian Date: Wed, 21 Feb 2024 09:45:24 +0800 Subject: [PATCH 08/12] temp: add chipset0 support Signed-off-by: Dongyan Qian --- LoongArch/Makefile | 4 +-- LoongArch/def.h | 1 + LoongArch/main.c | 1 + LoongArch/temp.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 LoongArch/temp.c diff --git a/LoongArch/Makefile b/LoongArch/Makefile index f406eeb..cc97788 100644 --- a/LoongArch/Makefile +++ b/LoongArch/Makefile @@ -1,6 +1,6 @@ -COMPILE_FILE_C = main.c gpio.c rtc.c acpi.c conf.c pci.c util.c spi.c mps.c i2c.c spd.c ht.c argparse.c process.c smbios.c -COMPILE_FILE_O = main.o gpio.o rtc.o acpi.o conf.o pci.o util.o spi.o mps.o i2c.o spd.o ht.o argparse.o process.o smbios.o +COMPILE_FILE_C = main.c gpio.c rtc.c acpi.c conf.c pci.c util.c spi.c mps.c i2c.c spd.c ht.c argparse.c process.c smbios.c temp.c +COMPILE_FILE_O = main.o gpio.o rtc.o acpi.o conf.o pci.o util.o spi.o mps.o i2c.o spd.o ht.o argparse.o process.o smbios.o temp.o #COMPILE_PATH = /opt/LoongArch_Toolchains/loongarch64-linux-gnu-2021-06-19-vector/bin/loongarch64-linux-gnu-gcc #COMPILE_PATH = /opt/LoongArch_Toolchains/loongarch64-linux-gnu-2021-06-19-vector/bin/loongarch64-linux-gnu-gcc diff --git a/LoongArch/def.h b/LoongArch/def.h index 51b81d8..8da0788 100644 --- a/LoongArch/def.h +++ b/LoongArch/def.h @@ -45,5 +45,6 @@ int cmd_pci (int argc, const char **argv); int cmd_rtc (int argc, const char **argv); int cmd_spd (int argc, const char **argv); int cmd_spi (int argc, const char **argv); +int cmd_temp (int argc, const char **argv); #endif diff --git a/LoongArch/main.c b/LoongArch/main.c index 48122f0..101ca13 100644 --- a/LoongArch/main.c +++ b/LoongArch/main.c @@ -32,6 +32,7 @@ static struct cmd_struct commands[] = { {"ht", cmd_ht}, {"mps", cmd_mps}, {"spd", cmd_spd}, + {"temp", cmd_temp}, }; int main (int argc, const char *argv[]) diff --git a/LoongArch/temp.c b/LoongArch/temp.c new file mode 100644 index 0000000..21dd822 --- /dev/null +++ b/LoongArch/temp.c @@ -0,0 +1,76 @@ +#include +#include +#include +#include "def.h" + +#define CHIPSET0_TEMP 0xe0010000414ULL + +static const char *const temp_usages[] = { + PROGRAM_NAME" temp ", + NULL, +}; + +int chipset_temp_read (void) +{ + void * p = NULL; + int status ; + unsigned long long devaddr; + + int fd = open ("/dev/mem", O_RDWR | O_SYNC); + if (fd < 0) { + printf("can't open file,please use root .\n"); + return 1; + } + + devaddr = CHIPSET0_TEMP; + + /*Transfer Virtul to Phy Addr*/ + p = vtpa (devaddr, fd); + + /*Debug Rtc*/ + printf("Chipset Temp Read Start ...\n"); + unsigned int tmp_tmp = 0; + + tmp_tmp = *(volatile unsigned int *)p; + printf ("Chipset0 Current Temp Val:%d \n", (tmp_tmp & 0xffff0000) >> 24); + + status = releaseMem(p); + close(fd); + return status; +} + +int cmd_temp (int argc, const char **argv) +{ + int read = 0; + int write = 0; + uid_t uid; + struct argparse argparse; + + struct argparse_option options[] = { + OPT_HELP(), + OPT_BOOLEAN ('r', "read", &read, "read Temp", NULL, 0, 0), + OPT_BOOLEAN ('w', "write", &write, "write No", NULL, 0, 0), + OPT_END(), + }; + + argparse_init(&argparse, options, temp_usages, 0); + argc = argparse_parse(&argparse, argc, argv); + + if (!(read || write)) { + argparse_usage(&argparse); + return 1; + } + + uid = geteuid (); + if (uid != 0) { + printf("Please run with root!\n"); + return -1; + } + + if (read) { + chipset_temp_read (); + } else if (write) { + //rtc_write (); + } + return 0; +} From 0c1f6220d77bbf0995cdcf2e0475bd0a60dbe2a6 Mon Sep 17 00:00:00 2001 From: Dongyan Qian Date: Wed, 20 Mar 2024 17:55:47 +0800 Subject: [PATCH 09/12] avs: support avs adjust vddn/vddp Signed-off-by: Dongyan Qian --- LoongArch/Makefile | 4 +- LoongArch/avs.c | 213 +++++++++++++++++++++++++++++++++++++++++++++ LoongArch/def.h | 1 + LoongArch/main.c | 1 + 4 files changed, 217 insertions(+), 2 deletions(-) create mode 100644 LoongArch/avs.c diff --git a/LoongArch/Makefile b/LoongArch/Makefile index cc97788..395c17c 100644 --- a/LoongArch/Makefile +++ b/LoongArch/Makefile @@ -1,6 +1,6 @@ -COMPILE_FILE_C = main.c gpio.c rtc.c acpi.c conf.c pci.c util.c spi.c mps.c i2c.c spd.c ht.c argparse.c process.c smbios.c temp.c -COMPILE_FILE_O = main.o gpio.o rtc.o acpi.o conf.o pci.o util.o spi.o mps.o i2c.o spd.o ht.o argparse.o process.o smbios.o temp.o +COMPILE_FILE_C = main.c gpio.c rtc.c acpi.c conf.c pci.c util.c spi.c mps.c i2c.c spd.c ht.c argparse.c process.c smbios.c temp.c avs.c +COMPILE_FILE_O = main.o gpio.o rtc.o acpi.o conf.o pci.o util.o spi.o mps.o i2c.o spd.o ht.o argparse.o process.o smbios.o temp.o avs.o #COMPILE_PATH = /opt/LoongArch_Toolchains/loongarch64-linux-gnu-2021-06-19-vector/bin/loongarch64-linux-gnu-gcc #COMPILE_PATH = /opt/LoongArch_Toolchains/loongarch64-linux-gnu-2021-06-19-vector/bin/loongarch64-linux-gnu-gcc diff --git a/LoongArch/avs.c b/LoongArch/avs.c new file mode 100644 index 0000000..48332a7 --- /dev/null +++ b/LoongArch/avs.c @@ -0,0 +1,213 @@ +#include +#include +#include +#include "def.h" +#include "i2c.h" + +extern int is3d; +extern int is3c; + +//#define SPI_CONFUSE_SPACE (0x0efdfe000000 + 0x8000/*Need 4K align*/ ) //b000 is Spi,so add 0x3000 + +//3D5000 +#define CPU_3C5000_NODE0_I2C0 0x1fe00160 +#define CPU_3C5000_NODE1_I2C0 0x10001fe00160ULL +#define CPU_3C5000_NODE2_I2C0 0x20001fe00160ULL +#define CPU_3C5000_NODE3_I2C0 0x30001fe00160ULL + +#define Write64(addr, data) (*(volatile UINT64*)(addr) = (data)) +#define Write32(addr, data) (*(volatile UINT32*)(addr) = (data)) +#define Write16(addr, data) (*(volatile UINT16*)(addr) = (data)) +#define Writel(addr, data) (*(volatile UINT32*)(addr) = (data)) +#define Writew(addr, data) (*(volatile UINT16*)(addr) = (data)) +#define Writeb(addr, data) (*(volatile UINT8*)(addr) = (data)) +#define Read64(addr) (*(volatile UINT64*)(addr)) +#define Read32(addr) (*(volatile UINT32*)(addr)) +#define Read16(addr) (*(volatile UINT16*)(addr)) +#define Readl(addr) (*(volatile UINT32*)(addr)) +#define Readw(addr) (*(volatile UINT16*)(addr)) +#define Readb(addr) (*(volatile UINT8*)(addr)) + + +UINTN AvsRegBaseAddr; + +static const char *const avs_usages[] = { + PROGRAM_NAME" avs ", + NULL, +}; + +unsigned long long devaddr_3c5000_avs[4] = { + CPU_3C5000_NODE0_I2C0, + CPU_3C5000_NODE1_I2C0, + CPU_3C5000_NODE2_I2C0, + CPU_3C5000_NODE3_I2C0 +}; + +#define AVS_BASE AvsRegBaseAddr +#define AVS_CSR AVS_BASE +#define AVS_MREG AVS_BASE + 0x4 +#define AVS_SREG AVS_BASE + 0x8 + +/* p: 1 n: 0*/ +VOID +AvsSetVol(int rail_sel, int vol, int rx_delay, int clk_div) +{ + if ((vol < 600) || (vol > 1300)) { + printf("\r\nAVS: Set vol range error!!!\r\n"); + return; + } + + Writel(AVS_CSR, 0x10000 | (clk_div << 17) | (0x7 << 25) | (rx_delay << 20)); + Writel(AVS_MREG, 0x80000000 | (vol << 4) | (rail_sel << 20)); + + while(Readl(AVS_SREG) & 0x80000000); + + if((Readl(AVS_SREG) & 0x60000000)) { + printf("set avs_vol erro!\n"); + } +} + +/* p: 1 n: 0*/ +UINTN +AvsGetVol(int rail_sel, int rx_delay, int clk_div) +{ + Writel(AVS_CSR, 0x10000 | (clk_div << 17) | (0x7 << 25) | (rx_delay << 20)); + Writel(AVS_MREG, 0xe0000000 | (rail_sel << 20)); + + while(Readl(AVS_SREG) & 0x80000000); + + if((Readl(AVS_SREG) & 0x60000000)) + return 0; + else + return (Readl(AVS_SREG) & 0xffff); +} + +/* p: 1 n: 0*/ +VOID +AvsVolPrint() +{ + UINT32 Val; + Val = AvsGetVol(0, 0, 4); + if (!Val) { + printf("AVS: Get Vddn error!\n"); + } else { + printf("AVS: Get Vddn value is: %d \n",Val); + } + Val = AvsGetVol(1, 0, 4); + if (!Val) { + printf("AVS: Get Vddp error!\n"); + } else { + printf("AVS: Get Vddp value is: %d \n",Val); + } +} + +int avs_read (int id) +{ + void * p = NULL; + int status ; + char RecordName[30] = {0}; + int c = 0; + char str[30] = {0}; + int fd; + + fd = open ("/dev/mem", O_RDWR | O_SYNC); + if (fd < 0) { + printf("can't open file,please use root .\n"); + return 1; + } + + p = vtpa(devaddr_3c5000_avs[id], fd); + + UINT64 NodeId; + UINT16 Val16; + + AvsRegBaseAddr = (UINT64) p; + + AvsVolPrint(); + + close(fd); + + return status; +} + +int avs_write_vdd (int id, int vol, int channel) +{ + void * p = NULL; + int status ; + char RecordName[30] = {0}; + int c = 0; + char str[30] = {0}; + int fd; + + fd = open ("/dev/mem", O_RDWR | O_SYNC); + if (fd < 0) { + printf("can't open file,please use root .\n"); + return 1; + } + + p = vtpa(devaddr_3c5000_avs[id], fd); + + UINT64 NodeId; + UINT32 Val16; + + AvsRegBaseAddr = (UINT64) p; + + AvsSetVol(channel, vol, 0, 4); + + close (fd); + return 0; +} + +int cmd_avs (int argc, const char **argv) +{ + int read = 0; + int write = 0; + int id = 0; + int vol = 0; + int channel = 0; + uid_t uid; + struct argparse argparse; + + struct argparse_option options[] = { + OPT_HELP(), + OPT_BOOLEAN ('r', "read", &read, "read avs", NULL, 0, 0), + OPT_BOOLEAN ('w', "write", &write, "write avs", NULL, 0, 0), + OPT_INTEGER ('i', "id", &id, "avs id(0-3)", NULL, 0, 0), + OPT_INTEGER ('d', "vol", &vol, "vol(800-1200)", NULL, 0, 0), + OPT_INTEGER ('c', "channel", &channel, "channel(0-1)", NULL, 0, 0), + OPT_END(), + }; + + argparse_init(&argparse, options, avs_usages, 0); + argc = argparse_parse(&argparse, argc, argv); + + if (!(read || write)) { + argparse_usage(&argparse); + return 1; + } + + uid = geteuid (); + if (uid != 0) { + printf("Please run with root!\n"); + return -1; + } + + if (read) { + if (id > 3) { + printf("id is 0~3\n"); + return -1; + } + avs_read (id); + } else if (write) { + if (id > 3) { + printf("id is 0~3\n"); + return -1; + } + if ((vol < 800) || (vol > 1200)) { + printf("vol is 800~1200\n"); + return -1; + } + avs_write_vdd (id,vol,channel); + } + return 0; +} diff --git a/LoongArch/def.h b/LoongArch/def.h index 8da0788..94af150 100644 --- a/LoongArch/def.h +++ b/LoongArch/def.h @@ -46,5 +46,6 @@ int cmd_rtc (int argc, const char **argv); int cmd_spd (int argc, const char **argv); int cmd_spi (int argc, const char **argv); int cmd_temp (int argc, const char **argv); +int cmd_avs (int argc, const char **argv); #endif diff --git a/LoongArch/main.c b/LoongArch/main.c index 101ca13..2a4e9bf 100644 --- a/LoongArch/main.c +++ b/LoongArch/main.c @@ -33,6 +33,7 @@ static struct cmd_struct commands[] = { {"mps", cmd_mps}, {"spd", cmd_spd}, {"temp", cmd_temp}, + {"avs", cmd_avs}, }; int main (int argc, const char *argv[]) From 09fe3a125b773904e0e57dc4fa1b4c17b4d9a6b9 Mon Sep 17 00:00:00 2001 From: Dongyan Qian Date: Thu, 9 May 2024 20:39:46 +0800 Subject: [PATCH 10/12] spi: optimize code Signed-off-by: Dongyan Qian --- LoongArch/spi.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/LoongArch/spi.c b/LoongArch/spi.c index c4668c9..1a952a4 100644 --- a/LoongArch/spi.c +++ b/LoongArch/spi.c @@ -167,6 +167,7 @@ Fopen_File ( Str[i] = Ch; i++; } + fclose(fp); // printf("Cpu_Tame:%s", Str); // printf("Cpu_Type0:%s", Cpu_Type[0]); } @@ -870,7 +871,8 @@ static int spi_update_flash (const char *path) if (Size != 0) { printf ("specify Flush Size %#lx .\n",Size); } else { - Size = statbuf.st_size; + Size = (statbuf.st_size + 4*1024 - 1) & ~(4*1024 - 1); + printf ("Assign file Size: %lx for one section(4K): .\n",Size); } } else { printf ("Failed to get file status.\n"); @@ -929,7 +931,7 @@ static int spi_dump_flash (const char *path, const char *addr) return 1; } - fwrite (buf, 1, 4128768, pfile); + fwrite (buf, 1, FLASH_SIZE, pfile); fflush (pfile); fclose (pfile); free (buf); @@ -1036,8 +1038,8 @@ int cmd_spi (int argc, const char **argv) char *Path="./"FILE_NAME_1; char Cpu_Name[100]; Fopen_File(Path, Cpu_Name); - is3c = !strncmp("Loongson-3C5000", Cpu_Name, 15); - is3d = !strncmp("Loongson-3D5000", Cpu_Name, 15); + is3c = !!strstr(Cpu_Name, "3C5000"); + is3d = !!strstr(Cpu_Name, "3D5000"); spi_update_flash (file); } else if (flag_dump) { From f51fb2ee74be9e8e6263f07054b9d236af2e0847 Mon Sep 17 00:00:00 2001 From: Dongyan Qian Date: Sat, 11 May 2024 10:24:16 +0800 Subject: [PATCH 11/12] Added GPL-2.0 declaration Signed-off-by: Dongyan Qian --- LoongArch/Makefile | 2 +- LoongArch/acpi.c | 2 ++ LoongArch/avs.c | 1 + LoongArch/conf.c | 1 + LoongArch/def.h | 1 + LoongArch/file.h | 2 +- LoongArch/gpio.c | 1 + LoongArch/ht.c | 1 + LoongArch/main.c | 1 + LoongArch/mps.c | 1 + LoongArch/pci.c | 1 + LoongArch/process.c | 1 + LoongArch/process.h | 2 +- LoongArch/rtc.c | 1 + LoongArch/smbios.c | 1 + LoongArch/spd.c | 1 + LoongArch/spi.c | 1 + LoongArch/temp.c | 1 + LoongArch/util.c | 1 + build.sh | 1 + 20 files changed, 21 insertions(+), 3 deletions(-) diff --git a/LoongArch/Makefile b/LoongArch/Makefile index 395c17c..a08b590 100644 --- a/LoongArch/Makefile +++ b/LoongArch/Makefile @@ -1,4 +1,4 @@ - +# SPDX-License-Identifier: GPL-2.0 COMPILE_FILE_C = main.c gpio.c rtc.c acpi.c conf.c pci.c util.c spi.c mps.c i2c.c spd.c ht.c argparse.c process.c smbios.c temp.c avs.c COMPILE_FILE_O = main.o gpio.o rtc.o acpi.o conf.o pci.o util.o spi.o mps.o i2c.o spd.o ht.o argparse.o process.o smbios.o temp.o avs.o diff --git a/LoongArch/acpi.c b/LoongArch/acpi.c index 94423ef..c5b6698 100644 --- a/LoongArch/acpi.c +++ b/LoongArch/acpi.c @@ -1,3 +1,5 @@ +// SPDX-License-Identifier: GPL-2.0 + #include #include #include diff --git a/LoongArch/avs.c b/LoongArch/avs.c index 48332a7..5361c90 100644 --- a/LoongArch/avs.c +++ b/LoongArch/avs.c @@ -1,3 +1,4 @@ +// SPDX-License-Identifier: GPL-2.0 #include #include #include diff --git a/LoongArch/conf.c b/LoongArch/conf.c index 24365ce..284496f 100644 --- a/LoongArch/conf.c +++ b/LoongArch/conf.c @@ -1,3 +1,4 @@ +// SPDX-License-Identifier: GPL-2.0 #include #include #include diff --git a/LoongArch/def.h b/LoongArch/def.h index 94af150..c7d2cbe 100644 --- a/LoongArch/def.h +++ b/LoongArch/def.h @@ -1,3 +1,4 @@ +// SPDX-License-Identifier: GPL-2.0 #ifndef __DEF_H_ #define __DEF_H_ #include "argparse.h" diff --git a/LoongArch/file.h b/LoongArch/file.h index c3a597a..10f3e22 100644 --- a/LoongArch/file.h +++ b/LoongArch/file.h @@ -1,4 +1,4 @@ - +// SPDX-License-Identifier: GPL-2.0 #ifndef _FILE_H_ #define _FILE_H_ diff --git a/LoongArch/gpio.c b/LoongArch/gpio.c index 4b8e471..36d498a 100644 --- a/LoongArch/gpio.c +++ b/LoongArch/gpio.c @@ -1,3 +1,4 @@ +// SPDX-License-Identifier: GPL-2.0 #include #include #include diff --git a/LoongArch/ht.c b/LoongArch/ht.c index 4d73a91..b7bf76a 100644 --- a/LoongArch/ht.c +++ b/LoongArch/ht.c @@ -1,3 +1,4 @@ +// SPDX-License-Identifier: GPL-2.0 #include #include #include diff --git a/LoongArch/main.c b/LoongArch/main.c index 2a4e9bf..800a75b 100644 --- a/LoongArch/main.c +++ b/LoongArch/main.c @@ -1,3 +1,4 @@ +// SPDX-License-Identifier: GPL-2.0 #include #include #include diff --git a/LoongArch/mps.c b/LoongArch/mps.c index 8c812bc..b3582ce 100644 --- a/LoongArch/mps.c +++ b/LoongArch/mps.c @@ -1,3 +1,4 @@ +// SPDX-License-Identifier: GPL-2.0 #include #include #include diff --git a/LoongArch/pci.c b/LoongArch/pci.c index 0cec09e..82b87b4 100644 --- a/LoongArch/pci.c +++ b/LoongArch/pci.c @@ -1,3 +1,4 @@ +// SPDX-License-Identifier: GPL-2.0 #include #include #include diff --git a/LoongArch/process.c b/LoongArch/process.c index d5fd049..d7e09cc 100644 --- a/LoongArch/process.c +++ b/LoongArch/process.c @@ -1,3 +1,4 @@ +// SPDX-License-Identifier: GPL-2.0 #include #include diff --git a/LoongArch/process.h b/LoongArch/process.h index bef8d62..e3ba7c8 100644 --- a/LoongArch/process.h +++ b/LoongArch/process.h @@ -1,4 +1,4 @@ - +// SPDX-License-Identifier: GPL-2.0 #ifndef _PROCESS_H_ #define _PROCESS_H_ diff --git a/LoongArch/rtc.c b/LoongArch/rtc.c index 47bbef4..6f8f842 100644 --- a/LoongArch/rtc.c +++ b/LoongArch/rtc.c @@ -1,3 +1,4 @@ +// SPDX-License-Identifier: GPL-2.0 #include #include #include diff --git a/LoongArch/smbios.c b/LoongArch/smbios.c index a6078ad..3038b9e 100644 --- a/LoongArch/smbios.c +++ b/LoongArch/smbios.c @@ -1,3 +1,4 @@ +// SPDX-License-Identifier: GPL-2.0 #include #include #include diff --git a/LoongArch/spd.c b/LoongArch/spd.c index 4de3f6e..1dfde59 100644 --- a/LoongArch/spd.c +++ b/LoongArch/spd.c @@ -1,3 +1,4 @@ +// SPDX-License-Identifier: GPL-2.0 #include #include #include diff --git a/LoongArch/spi.c b/LoongArch/spi.c index 1a952a4..73e5f73 100644 --- a/LoongArch/spi.c +++ b/LoongArch/spi.c @@ -1,3 +1,4 @@ +// SPDX-License-Identifier: GPL-2.0 #include #include #include diff --git a/LoongArch/temp.c b/LoongArch/temp.c index 21dd822..3446f05 100644 --- a/LoongArch/temp.c +++ b/LoongArch/temp.c @@ -1,3 +1,4 @@ +// SPDX-License-Identifier: GPL-2.0 #include #include #include diff --git a/LoongArch/util.c b/LoongArch/util.c index 30fd86c..b79c1c1 100644 --- a/LoongArch/util.c +++ b/LoongArch/util.c @@ -1,3 +1,4 @@ +// SPDX-License-Identifier: GPL-2.0 #include #include #include diff --git a/build.sh b/build.sh index feca3be..16dab03 100755 --- a/build.sh +++ b/build.sh @@ -1,3 +1,4 @@ +# SPDX-License-Identifier: GPL-2.0 #!/bin/bash make -C LoongArch clean From 37001618dd987f5e31bab19d9d52d6de6d21cc06 Mon Sep 17 00:00:00 2001 From: Dongyan Qian Date: Sat, 11 May 2024 10:31:26 +0800 Subject: [PATCH 12/12] Version: Change Version to V1.3 Signed-off-by: Dongyan Qian --- LoongArch/def.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LoongArch/def.h b/LoongArch/def.h index c7d2cbe..a482121 100644 --- a/LoongArch/def.h +++ b/LoongArch/def.h @@ -10,7 +10,7 @@ #endif #define PROGRAM_NAME "OsTools" -#define PROGRAM_VERSION "1.2" +#define PROGRAM_VERSION "1.3" #define LS7A_CONF_BASE_ADDR 0x10010000 #define LS7A_MISC_BASE_ADDR 0x10080000