1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2016 Beniamino Galvani <b.galvani@gmail.com>
5 * Secure monitor calls.
11 #include <asm/arch/sm.h>
12 #include <asm/cache.h>
13 #include <linux/err.h>
14 #include <linux/kernel.h>
16 #include <linux/bitfield.h>
20 #define FN_GET_SHARE_MEM_INPUT_BASE 0x82000020
21 #define FN_GET_SHARE_MEM_OUTPUT_BASE 0x82000021
22 #define FN_EFUSE_READ 0x82000030
23 #define FN_EFUSE_WRITE 0x82000031
24 #define FN_CHIP_ID 0x82000044
26 static void *shmem_input;
27 static void *shmem_output;
29 static void meson_init_shmem(void)
33 if (shmem_input && shmem_output)
36 regs.regs[0] = FN_GET_SHARE_MEM_INPUT_BASE;
38 shmem_input = (void *)regs.regs[0];
40 regs.regs[0] = FN_GET_SHARE_MEM_OUTPUT_BASE;
42 shmem_output = (void *)regs.regs[0];
44 debug("Secure Monitor shmem: 0x%p 0x%p\n", shmem_input, shmem_output);
47 ssize_t meson_sm_read_efuse(uintptr_t offset, void *buffer, size_t size)
53 regs.regs[0] = FN_EFUSE_READ;
54 regs.regs[1] = offset;
59 if (regs.regs[0] == 0)
62 memcpy(buffer, shmem_output, min(size, regs.regs[0]));
67 #define SM_CHIP_ID_LENGTH 119
68 #define SM_CHIP_ID_OFFSET 4
69 #define SM_CHIP_ID_SIZE 12
71 int meson_sm_get_serial(void *buffer, size_t size)
77 regs.regs[0] = FN_CHIP_ID;
83 memcpy(buffer, shmem_output + SM_CHIP_ID_OFFSET,
84 min_t(size_t, size, SM_CHIP_ID_SIZE));
89 #define AO_SEC_SD_CFG15 0xfc
90 #define REBOOT_REASON_MASK GENMASK(15, 12)
92 int meson_sm_get_reboot_reason(void)
94 struct regmap *regmap;
99 /* find the offset of compatible node */
100 nodeoffset = fdt_node_offset_by_compatible(gd->fdt_blob, -1,
101 "amlogic,meson-gx-ao-secure");
102 if (nodeoffset < 0) {
103 printf("%s: failed to get amlogic,meson-gx-ao-secure\n",
108 /* get regmap from the syscon node */
109 node = offset_to_ofnode(nodeoffset);
110 regmap = syscon_node_to_regmap(node);
111 if (IS_ERR(regmap)) {
112 printf("%s: failed to get regmap\n", __func__);
116 regmap_read(regmap, AO_SEC_SD_CFG15, &reason);
118 /* The SMC call is not used, we directly use AO_SEC_SD_CFG15 */
119 return FIELD_GET(REBOOT_REASON_MASK, reason);
122 static int do_sm_serial(struct cmd_tbl *cmdtp, int flag, int argc,
129 return CMD_RET_USAGE;
131 address = simple_strtoul(argv[1], NULL, 0);
133 ret = meson_sm_get_serial((void *)address, SM_CHIP_ID_SIZE);
135 return CMD_RET_FAILURE;
137 return CMD_RET_SUCCESS;
140 #define MAX_REBOOT_REASONS 14
142 static const char *reboot_reasons[MAX_REBOOT_REASONS] = {
143 [REBOOT_REASON_COLD] = "cold_boot",
144 [REBOOT_REASON_NORMAL] = "normal",
145 [REBOOT_REASON_RECOVERY] = "recovery",
146 [REBOOT_REASON_UPDATE] = "update",
147 [REBOOT_REASON_FASTBOOT] = "fastboot",
148 [REBOOT_REASON_SUSPEND_OFF] = "suspend_off",
149 [REBOOT_REASON_HIBERNATE] = "hibernate",
150 [REBOOT_REASON_BOOTLOADER] = "bootloader",
151 [REBOOT_REASON_SHUTDOWN_REBOOT] = "shutdown_reboot",
152 [REBOOT_REASON_RPMBP] = "rpmbp",
153 [REBOOT_REASON_CRASH_DUMP] = "crash_dump",
154 [REBOOT_REASON_KERNEL_PANIC] = "kernel_panic",
155 [REBOOT_REASON_WATCHDOG_REBOOT] = "watchdog_reboot",
158 static int do_sm_reboot_reason(struct cmd_tbl *cmdtp, int flag, int argc,
161 const char *reason_str;
162 char *destarg = NULL;
168 reason = meson_sm_get_reboot_reason();
170 return CMD_RET_FAILURE;
172 if (reason >= MAX_REBOOT_REASONS ||
173 !reboot_reasons[reason])
174 reason_str = "unknown";
176 reason_str = reboot_reasons[reason];
179 env_set(destarg, reason_str);
181 printf("reboot reason: %s (%x)\n", reason_str, reason);
183 return CMD_RET_SUCCESS;
186 static struct cmd_tbl cmd_sm_sub[] = {
187 U_BOOT_CMD_MKENT(serial, 2, 1, do_sm_serial, "", ""),
188 U_BOOT_CMD_MKENT(reboot_reason, 1, 1, do_sm_reboot_reason, "", ""),
191 static int do_sm(struct cmd_tbl *cmdtp, int flag, int argc,
197 return CMD_RET_USAGE;
199 /* Strip off leading 'sm' command argument */
203 c = find_cmd_tbl(argv[0], &cmd_sm_sub[0], ARRAY_SIZE(cmd_sm_sub));
206 return c->cmd(cmdtp, flag, argc, argv);
208 return CMD_RET_USAGE;
213 "Secure Monitor Control",
214 "serial <address> - read chip unique id to memory address\n"
215 "sm reboot_reason [name] - get reboot reason and store to to environment"