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/ptrace.h>
15 #include <linux/bitops.h>
16 #include <linux/err.h>
17 #include <linux/kernel.h>
19 #include <linux/bitfield.h>
23 #define FN_GET_SHARE_MEM_INPUT_BASE 0x82000020
24 #define FN_GET_SHARE_MEM_OUTPUT_BASE 0x82000021
25 #define FN_EFUSE_READ 0x82000030
26 #define FN_EFUSE_WRITE 0x82000031
27 #define FN_CHIP_ID 0x82000044
29 static void *shmem_input;
30 static void *shmem_output;
32 static void meson_init_shmem(void)
36 if (shmem_input && shmem_output)
39 regs.regs[0] = FN_GET_SHARE_MEM_INPUT_BASE;
41 shmem_input = (void *)regs.regs[0];
43 regs.regs[0] = FN_GET_SHARE_MEM_OUTPUT_BASE;
45 shmem_output = (void *)regs.regs[0];
47 debug("Secure Monitor shmem: 0x%p 0x%p\n", shmem_input, shmem_output);
50 ssize_t meson_sm_read_efuse(uintptr_t offset, void *buffer, size_t size)
56 regs.regs[0] = FN_EFUSE_READ;
57 regs.regs[1] = offset;
62 if (regs.regs[0] == 0)
65 memcpy(buffer, shmem_output, min(size, regs.regs[0]));
70 #define SM_CHIP_ID_LENGTH 119
71 #define SM_CHIP_ID_OFFSET 4
72 #define SM_CHIP_ID_SIZE 12
74 int meson_sm_get_serial(void *buffer, size_t size)
80 regs.regs[0] = FN_CHIP_ID;
86 memcpy(buffer, shmem_output + SM_CHIP_ID_OFFSET,
87 min_t(size_t, size, SM_CHIP_ID_SIZE));
92 #define AO_SEC_SD_CFG15 0xfc
93 #define REBOOT_REASON_MASK GENMASK(15, 12)
95 int meson_sm_get_reboot_reason(void)
97 struct regmap *regmap;
102 /* find the offset of compatible node */
103 nodeoffset = fdt_node_offset_by_compatible(gd->fdt_blob, -1,
104 "amlogic,meson-gx-ao-secure");
105 if (nodeoffset < 0) {
106 printf("%s: failed to get amlogic,meson-gx-ao-secure\n",
111 /* get regmap from the syscon node */
112 node = offset_to_ofnode(nodeoffset);
113 regmap = syscon_node_to_regmap(node);
114 if (IS_ERR(regmap)) {
115 printf("%s: failed to get regmap\n", __func__);
119 regmap_read(regmap, AO_SEC_SD_CFG15, &reason);
121 /* The SMC call is not used, we directly use AO_SEC_SD_CFG15 */
122 return FIELD_GET(REBOOT_REASON_MASK, reason);
125 static int do_sm_serial(struct cmd_tbl *cmdtp, int flag, int argc,
132 return CMD_RET_USAGE;
134 address = simple_strtoul(argv[1], NULL, 0);
136 ret = meson_sm_get_serial((void *)address, SM_CHIP_ID_SIZE);
138 return CMD_RET_FAILURE;
140 return CMD_RET_SUCCESS;
143 #define MAX_REBOOT_REASONS 14
145 static const char *reboot_reasons[MAX_REBOOT_REASONS] = {
146 [REBOOT_REASON_COLD] = "cold_boot",
147 [REBOOT_REASON_NORMAL] = "normal",
148 [REBOOT_REASON_RECOVERY] = "recovery",
149 [REBOOT_REASON_UPDATE] = "update",
150 [REBOOT_REASON_FASTBOOT] = "fastboot",
151 [REBOOT_REASON_SUSPEND_OFF] = "suspend_off",
152 [REBOOT_REASON_HIBERNATE] = "hibernate",
153 [REBOOT_REASON_BOOTLOADER] = "bootloader",
154 [REBOOT_REASON_SHUTDOWN_REBOOT] = "shutdown_reboot",
155 [REBOOT_REASON_RPMBP] = "rpmbp",
156 [REBOOT_REASON_CRASH_DUMP] = "crash_dump",
157 [REBOOT_REASON_KERNEL_PANIC] = "kernel_panic",
158 [REBOOT_REASON_WATCHDOG_REBOOT] = "watchdog_reboot",
161 static int do_sm_reboot_reason(struct cmd_tbl *cmdtp, int flag, int argc,
164 const char *reason_str;
165 char *destarg = NULL;
171 reason = meson_sm_get_reboot_reason();
173 return CMD_RET_FAILURE;
175 if (reason >= MAX_REBOOT_REASONS ||
176 !reboot_reasons[reason])
177 reason_str = "unknown";
179 reason_str = reboot_reasons[reason];
182 env_set(destarg, reason_str);
184 printf("reboot reason: %s (%x)\n", reason_str, reason);
186 return CMD_RET_SUCCESS;
189 static struct cmd_tbl cmd_sm_sub[] = {
190 U_BOOT_CMD_MKENT(serial, 2, 1, do_sm_serial, "", ""),
191 U_BOOT_CMD_MKENT(reboot_reason, 1, 1, do_sm_reboot_reason, "", ""),
194 static int do_sm(struct cmd_tbl *cmdtp, int flag, int argc,
200 return CMD_RET_USAGE;
202 /* Strip off leading 'sm' command argument */
206 c = find_cmd_tbl(argv[0], &cmd_sm_sub[0], ARRAY_SIZE(cmd_sm_sub));
209 return c->cmd(cmdtp, flag, argc, argv);
211 return CMD_RET_USAGE;
216 "Secure Monitor Control",
217 "serial <address> - read chip unique id to memory address\n"
218 "sm reboot_reason [name] - get reboot reason and store to to environment"