1From 1c5b450a068583f2407767451ef636d0661071da Mon Sep 17 00:00:00 2001
2From: Alexander Filippov <a.filippov@yadro.com>
3Date: Tue, 7 Apr 2020 16:45:41 +0300
4Subject: [PATCH] Add system reset status support
5
6This is backport of patch file from intel-bmc/openbmc repository.
7  https://github.com/Intel-BMC/openbmc/blob/intel/meta-openbmc-mods/meta-common/recipes-bsp/u-boot/files/0020-Add-system-reset-status-support.patch
8
9Will display the reset reasons in u-boot,
10and save the reset reasons into kernel command line,
11for applications to query.
12
13Signed-off-by: Alexander Filippov <a.filippov@yadro.com>
14---
15 arch/arm/include/asm/arch-aspeed/ast_scu.h  |   2 +-
16 arch/arm/include/asm/arch-aspeed/platform.h |   2 +
17 arch/arm/mach-aspeed/Makefile               |   1 +
18 arch/arm/mach-aspeed/ast-late-init.c        | 114 ++++++++++++++++++++
19 arch/arm/mach-aspeed/ast-scu.c              |   6 +-
20 5 files changed, 123 insertions(+), 2 deletions(-)
21 create mode 100644 arch/arm/mach-aspeed/ast-late-init.c
22
23diff --git a/arch/arm/include/asm/arch-aspeed/ast_scu.h b/arch/arm/include/asm/arch-aspeed/ast_scu.h
24index dcbc6730d4..b428f386d6 100644
25--- a/arch/arm/include/asm/arch-aspeed/ast_scu.h
26+++ b/arch/arm/include/asm/arch-aspeed/ast_scu.h
27@@ -29,7 +29,7 @@
28 #define __AST_SCU_H
29
30 extern void ast_scu_show_system_info (void);
31-extern void ast_scu_sys_rest_info(void);
32+extern u32 ast_scu_sys_rest_info(void);
33 extern void ast_scu_security_info(void);
34 extern u32 ast_scu_revision_id(void);
35 extern u32 ast_scu_get_vga_memsize(void);
36diff --git a/arch/arm/include/asm/arch-aspeed/platform.h b/arch/arm/include/asm/arch-aspeed/platform.h
37index 1c02914fcb..b9207c492f 100644
38--- a/arch/arm/include/asm/arch-aspeed/platform.h
39+++ b/arch/arm/include/asm/arch-aspeed/platform.h
40@@ -31,4 +31,6 @@
41 #err "No define for platform.h"
42 #endif
43
44+#define CONFIG_BOARD_LATE_INIT 1 /* Call board_late_init */
45+
46 #endif
47diff --git a/arch/arm/mach-aspeed/Makefile b/arch/arm/mach-aspeed/Makefile
48index 7d8930beb9..4af2a7c96a 100644
49--- a/arch/arm/mach-aspeed/Makefile
50+++ b/arch/arm/mach-aspeed/Makefile
51@@ -15,3 +15,4 @@ obj-y += timer.o reset.o cpuinfo.o ast-scu.o ast-ahbc.o ast-sdmc.o
52 obj-$(CONFIG_AST_SPI_NOR) += flash.o
53 obj-$(CONFIG_ARCH_AST2500) += platform_g5.o
54 obj-$(CONFIG_ARCH_AST2400) += platform_g4.o
55+obj-$(CONFIG_BOARD_LATE_INIT) += ast-late-init.o
56diff --git a/arch/arm/mach-aspeed/ast-late-init.c b/arch/arm/mach-aspeed/ast-late-init.c
57new file mode 100644
58index 0000000000..5646c0e882
59--- /dev/null
60+++ b/arch/arm/mach-aspeed/ast-late-init.c
61@@ -0,0 +1,114 @@
62+/*
63+ * SPDX-License-Identifier: Apache-2.0
64+ * Copyright (C) 2020 YADRO.
65+ */
66+
67+#include <common.h>
68+
69+#include <asm/arch/ast_scu.h>
70+#include <asm/arch/regs-scu.h>
71+#include <malloc.h>
72+
73+static void update_bootargs_cmd(const char *key, const char *value)
74+{
75+    int buf_len;
76+    char *buf;
77+    char *cmdline;
78+    char *end = NULL;
79+
80+    if (!key || (key[0] == '\0'))
81+    {
82+        printf("%s: Empty key not allowed\n", __func__);
83+        return;
84+    }
85+
86+    cmdline = getenv("bootargs");
87+
88+    /* Allocate space for maximum possible new command line */
89+    buf_len = (cmdline ? strlen(cmdline) : 0)
90+            + 1 /* spacebar as delimiter */
91+            + strlen(key)
92+            + (value ? 1 /* equal sign */ + strlen(value) : 0)
93+            + 1 /* terminating null */;
94+
95+    buf = calloc(buf_len, sizeof(char));
96+    if (!buf)
97+    {
98+        printf("%s: out of memory\n", __func__);
99+        return;
100+    }
101+
102+    if (cmdline)
103+    {
104+        char *start = strstr(cmdline, key);
105+
106+        /* Check for full word match. Match should be start of cmdline
107+         * or there should be space before match */
108+        if (start && ((start == cmdline) || (*(start - 1) == ' ')))
109+        {
110+            strncat(buf, cmdline, (start - cmdline));
111+
112+            /* Find the end of the keyword[=value] pair,
113+             * including a single training space character, if any.
114+             * Skip the found substring, mark the tail of cmdline.
115+             */
116+            end = strchr(start, ' ');
117+            if (end)
118+            {
119+                end++;
120+            }
121+        }
122+        else
123+        {
124+            strcat(buf, cmdline);
125+            strcat(buf, " ");
126+        }
127+    }
128+
129+    strcat(buf, key);
130+    if (value)
131+    {
132+        strcat(buf, "=");
133+        strcat(buf, value);
134+    }
135+
136+    if (end)
137+    {
138+        strcat(buf, " ");
139+        strcat(buf, end);
140+    }
141+
142+    setenv("bootargs", buf);
143+    free(buf);
144+}
145+
146+static void set_reset_reason(void)
147+{
148+    u32 reset_reason = ast_scu_sys_rest_info();
149+
150+    if (reset_reason & SCU_SYS_EXT_RESET_FLAG)
151+    {
152+        update_bootargs_cmd("resetreason", "external");
153+    }
154+    else if (reset_reason & SCU_SYS_WDT_RESET_FLAG)
155+    {
156+        update_bootargs_cmd("resetreason", "watchdog");
157+    }
158+    else if (reset_reason & SCU_SYS_PWR_RESET_FLAG)
159+    {
160+        update_bootargs_cmd("resetreason", "power");
161+    }
162+    else
163+    {
164+        char value[32];
165+        snprintf(value, sizeof(value) - 1, "0x%x", reset_reason);
166+        update_bootargs_cmd("resetreason", value);
167+    }
168+}
169+
170+int board_late_init(void)
171+{
172+    set_reset_reason();
173+
174+    return 0;
175+}
176diff --git a/arch/arm/mach-aspeed/ast-scu.c b/arch/arm/mach-aspeed/ast-scu.c
177index 12de9b8036..5afd3793e3 100644
178--- a/arch/arm/mach-aspeed/ast-scu.c
179+++ b/arch/arm/mach-aspeed/ast-scu.c
180@@ -482,22 +482,26 @@ void ast_scu_security_info(void)
181 	}
182 }
183
184-void ast_scu_sys_rest_info(void)
185+u32 ast_scu_sys_rest_info(void)
186 {
187 	u32 rest = ast_scu_read(AST_SCU_SYS_CTRL);
188
189 	if (rest & SCU_SYS_EXT_RESET_FLAG) {
190 		printf("RST : External\n");
191 		ast_scu_write(SCU_SYS_EXT_RESET_FLAG, AST_SCU_SYS_CTRL);
192+		rest = SCU_SYS_EXT_RESET_FLAG;
193 	} else if (rest & SCU_SYS_WDT_RESET_FLAG) {
194 		printf("RST : Watchdog\n");
195 		ast_scu_write(SCU_SYS_WDT_RESET_FLAG, AST_SCU_SYS_CTRL);
196+		rest = SCU_SYS_WDT_RESET_FLAG;
197 	} else if (rest & SCU_SYS_PWR_RESET_FLAG) {
198 		printf("RST : Power On\n");
199 		ast_scu_write(SCU_SYS_PWR_RESET_FLAG, AST_SCU_SYS_CTRL);
200+		rest = SCU_SYS_PWR_RESET_FLAG;
201 	} else {
202 		printf("RST : CLK en\n");
203 	}
204+	return rest;
205 }
206
207 u32 ast_scu_get_vga_memsize(void)
208--
2092.25.4
210
211