1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2016 Beniamino Galvani <b.galvani@gmail.com>
5 * Secure monitor calls.
12 #include <asm/arch/sm.h>
13 #include <asm/cache.h>
14 #include <asm/global_data.h>
15 #include <asm/ptrace.h>
16 #include <linux/bitops.h>
17 #include <linux/err.h>
18 #include <linux/kernel.h>
20 #include <linux/bitfield.h>
24 #define FN_GET_SHARE_MEM_INPUT_BASE 0x82000020
25 #define FN_GET_SHARE_MEM_OUTPUT_BASE 0x82000021
26 #define FN_EFUSE_READ 0x82000030
27 #define FN_EFUSE_WRITE 0x82000031
28 #define FN_CHIP_ID 0x82000044
30 static void *shmem_input;
31 static void *shmem_output;
33 static void meson_init_shmem(void)
37 if (shmem_input && shmem_output)
40 regs.regs[0] = FN_GET_SHARE_MEM_INPUT_BASE;
42 shmem_input = (void *)regs.regs[0];
44 regs.regs[0] = FN_GET_SHARE_MEM_OUTPUT_BASE;
46 shmem_output = (void *)regs.regs[0];
48 debug("Secure Monitor shmem: 0x%p 0x%p\n", shmem_input, shmem_output);
51 ssize_t meson_sm_read_efuse(uintptr_t offset, void *buffer, size_t size)
57 regs.regs[0] = FN_EFUSE_READ;
58 regs.regs[1] = offset;
63 if (regs.regs[0] == 0)
66 memcpy(buffer, shmem_output, min(size, regs.regs[0]));
71 #define SM_CHIP_ID_LENGTH 119
72 #define SM_CHIP_ID_OFFSET 4
73 #define SM_CHIP_ID_SIZE 12
75 int meson_sm_get_serial(void *buffer, size_t size)
81 regs.regs[0] = FN_CHIP_ID;
87 memcpy(buffer, shmem_output + SM_CHIP_ID_OFFSET,
88 min_t(size_t, size, SM_CHIP_ID_SIZE));
93 #define AO_SEC_SD_CFG15 0xfc
94 #define REBOOT_REASON_MASK GENMASK(15, 12)
96 int meson_sm_get_reboot_reason(void)
98 struct regmap *regmap;
103 /* find the offset of compatible node */
104 nodeoffset = fdt_node_offset_by_compatible(gd->fdt_blob, -1,
105 "amlogic,meson-gx-ao-secure");
106 if (nodeoffset < 0) {
107 printf("%s: failed to get amlogic,meson-gx-ao-secure\n",
112 /* get regmap from the syscon node */
113 node = offset_to_ofnode(nodeoffset);
114 regmap = syscon_node_to_regmap(node);
115 if (IS_ERR(regmap)) {
116 printf("%s: failed to get regmap\n", __func__);
120 regmap_read(regmap, AO_SEC_SD_CFG15, &reason);
122 /* The SMC call is not used, we directly use AO_SEC_SD_CFG15 */
123 return FIELD_GET(REBOOT_REASON_MASK, reason);
126 static int do_sm_serial(struct cmd_tbl *cmdtp, int flag, int argc,
133 return CMD_RET_USAGE;
135 address = simple_strtoul(argv[1], NULL, 0);
137 ret = meson_sm_get_serial((void *)address, SM_CHIP_ID_SIZE);
139 return CMD_RET_FAILURE;
141 return CMD_RET_SUCCESS;
144 #define MAX_REBOOT_REASONS 14
146 static const char *reboot_reasons[MAX_REBOOT_REASONS] = {
147 [REBOOT_REASON_COLD] = "cold_boot",
148 [REBOOT_REASON_NORMAL] = "normal",
149 [REBOOT_REASON_RECOVERY] = "recovery",
150 [REBOOT_REASON_UPDATE] = "update",
151 [REBOOT_REASON_FASTBOOT] = "fastboot",
152 [REBOOT_REASON_SUSPEND_OFF] = "suspend_off",
153 [REBOOT_REASON_HIBERNATE] = "hibernate",
154 [REBOOT_REASON_BOOTLOADER] = "bootloader",
155 [REBOOT_REASON_SHUTDOWN_REBOOT] = "shutdown_reboot",
156 [REBOOT_REASON_RPMBP] = "rpmbp",
157 [REBOOT_REASON_CRASH_DUMP] = "crash_dump",
158 [REBOOT_REASON_KERNEL_PANIC] = "kernel_panic",
159 [REBOOT_REASON_WATCHDOG_REBOOT] = "watchdog_reboot",
162 static int do_sm_reboot_reason(struct cmd_tbl *cmdtp, int flag, int argc,
165 const char *reason_str;
166 char *destarg = NULL;
172 reason = meson_sm_get_reboot_reason();
174 return CMD_RET_FAILURE;
176 if (reason >= MAX_REBOOT_REASONS ||
177 !reboot_reasons[reason])
178 reason_str = "unknown";
180 reason_str = reboot_reasons[reason];
183 env_set(destarg, reason_str);
185 printf("reboot reason: %s (%x)\n", reason_str, reason);
187 return CMD_RET_SUCCESS;
190 static struct cmd_tbl cmd_sm_sub[] = {
191 U_BOOT_CMD_MKENT(serial, 2, 1, do_sm_serial, "", ""),
192 U_BOOT_CMD_MKENT(reboot_reason, 1, 1, do_sm_reboot_reason, "", ""),
195 static int do_sm(struct cmd_tbl *cmdtp, int flag, int argc,
201 return CMD_RET_USAGE;
203 /* Strip off leading 'sm' command argument */
207 c = find_cmd_tbl(argv[0], &cmd_sm_sub[0], ARRAY_SIZE(cmd_sm_sub));
210 return c->cmd(cmdtp, flag, argc, argv);
212 return CMD_RET_USAGE;
217 "Secure Monitor Control",
218 "serial <address> - read chip unique id to memory address\n"
219 "sm reboot_reason [name] - get reboot reason and store to to environment"