blob: fac286b9c851703cac77a5a61961d2067bb73e01 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Beniamino Galvani38e1a602016-05-08 08:30:17 +02002/*
3 * (C) Copyright 2016 Beniamino Galvani <b.galvani@gmail.com>
4 *
Beniamino Galvani38e1a602016-05-08 08:30:17 +02005 * Secure monitor calls.
6 */
7
8#include <common.h>
Neil Armstrong63f475c2019-06-12 11:49:06 +02009#include <asm/arch/sm.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070010#include <linux/err.h>
Beniamino Galvani38e1a602016-05-08 08:30:17 +020011#include <linux/kernel.h>
Neil Armstrong385309c2019-08-06 17:28:36 +020012#include <dm.h>
13#include <linux/bitfield.h>
14#include <regmap.h>
15#include <syscon.h>
Beniamino Galvani38e1a602016-05-08 08:30:17 +020016
17#define FN_GET_SHARE_MEM_INPUT_BASE 0x82000020
18#define FN_GET_SHARE_MEM_OUTPUT_BASE 0x82000021
19#define FN_EFUSE_READ 0x82000030
20#define FN_EFUSE_WRITE 0x82000031
Neil Armstrong63f475c2019-06-12 11:49:06 +020021#define FN_CHIP_ID 0x82000044
Beniamino Galvani38e1a602016-05-08 08:30:17 +020022
23static void *shmem_input;
24static void *shmem_output;
25
26static void meson_init_shmem(void)
27{
28 struct pt_regs regs;
29
30 if (shmem_input && shmem_output)
31 return;
32
33 regs.regs[0] = FN_GET_SHARE_MEM_INPUT_BASE;
34 smc_call(&regs);
35 shmem_input = (void *)regs.regs[0];
36
37 regs.regs[0] = FN_GET_SHARE_MEM_OUTPUT_BASE;
38 smc_call(&regs);
39 shmem_output = (void *)regs.regs[0];
40
41 debug("Secure Monitor shmem: 0x%p 0x%p\n", shmem_input, shmem_output);
42}
43
44ssize_t meson_sm_read_efuse(uintptr_t offset, void *buffer, size_t size)
45{
46 struct pt_regs regs;
47
48 meson_init_shmem();
49
50 regs.regs[0] = FN_EFUSE_READ;
51 regs.regs[1] = offset;
52 regs.regs[2] = size;
53
54 smc_call(&regs);
55
56 if (regs.regs[0] == 0)
57 return -1;
58
59 memcpy(buffer, shmem_output, min(size, regs.regs[0]));
60
61 return regs.regs[0];
62}
Neil Armstrong63f475c2019-06-12 11:49:06 +020063
64#define SM_CHIP_ID_LENGTH 119
65#define SM_CHIP_ID_OFFSET 4
66#define SM_CHIP_ID_SIZE 12
67
68int meson_sm_get_serial(void *buffer, size_t size)
69{
70 struct pt_regs regs;
71
72 meson_init_shmem();
73
74 regs.regs[0] = FN_CHIP_ID;
75 regs.regs[1] = 0;
76 regs.regs[2] = 0;
77
78 smc_call(&regs);
79
80 memcpy(buffer, shmem_output + SM_CHIP_ID_OFFSET,
81 min_t(size_t, size, SM_CHIP_ID_SIZE));
82
83 return 0;
84}
Neil Armstrong69800ec2019-08-06 17:21:02 +020085
Neil Armstrong385309c2019-08-06 17:28:36 +020086#define AO_SEC_SD_CFG15 0xfc
87#define REBOOT_REASON_MASK GENMASK(15, 12)
88
89int meson_sm_get_reboot_reason(void)
90{
91 struct regmap *regmap;
92 int nodeoffset;
93 ofnode node;
94 unsigned int reason;
95
96 /* find the offset of compatible node */
97 nodeoffset = fdt_node_offset_by_compatible(gd->fdt_blob, -1,
98 "amlogic,meson-gx-ao-secure");
99 if (nodeoffset < 0) {
100 printf("%s: failed to get amlogic,meson-gx-ao-secure\n",
101 __func__);
102 return -ENODEV;
103 }
104
105 /* get regmap from the syscon node */
106 node = offset_to_ofnode(nodeoffset);
107 regmap = syscon_node_to_regmap(node);
108 if (IS_ERR(regmap)) {
109 printf("%s: failed to get regmap\n", __func__);
110 return -EINVAL;
111 }
112
113 regmap_read(regmap, AO_SEC_SD_CFG15, &reason);
114
115 /* The SMC call is not used, we directly use AO_SEC_SD_CFG15 */
116 return FIELD_GET(REBOOT_REASON_MASK, reason);
117}
118
Neil Armstrong69800ec2019-08-06 17:21:02 +0200119static int do_sm_serial(cmd_tbl_t *cmdtp, int flag, int argc,
120 char *const argv[])
121{
122 ulong address;
123 int ret;
124
125 if (argc < 2)
126 return CMD_RET_USAGE;
127
128 address = simple_strtoul(argv[1], NULL, 0);
129
130 ret = meson_sm_get_serial((void *)address, SM_CHIP_ID_SIZE);
131 if (ret)
132 return CMD_RET_FAILURE;
133
Neil Armstrong385309c2019-08-06 17:28:36 +0200134 return CMD_RET_SUCCESS;
135}
136
137#define MAX_REBOOT_REASONS 14
138
139static const char *reboot_reasons[MAX_REBOOT_REASONS] = {
140 [REBOOT_REASON_COLD] = "cold_boot",
141 [REBOOT_REASON_NORMAL] = "normal",
142 [REBOOT_REASON_RECOVERY] = "recovery",
143 [REBOOT_REASON_UPDATE] = "update",
144 [REBOOT_REASON_FASTBOOT] = "fastboot",
145 [REBOOT_REASON_SUSPEND_OFF] = "suspend_off",
146 [REBOOT_REASON_HIBERNATE] = "hibernate",
147 [REBOOT_REASON_BOOTLOADER] = "bootloader",
148 [REBOOT_REASON_SHUTDOWN_REBOOT] = "shutdown_reboot",
149 [REBOOT_REASON_RPMBP] = "rpmbp",
150 [REBOOT_REASON_CRASH_DUMP] = "crash_dump",
151 [REBOOT_REASON_KERNEL_PANIC] = "kernel_panic",
152 [REBOOT_REASON_WATCHDOG_REBOOT] = "watchdog_reboot",
153};
154
155static int do_sm_reboot_reason(cmd_tbl_t *cmdtp, int flag, int argc,
156 char *const argv[])
157{
158 const char *reason_str;
159 char *destarg = NULL;
160 int reason;
161
162 if (argc > 1)
163 destarg = argv[1];
164
165 reason = meson_sm_get_reboot_reason();
166 if (reason < 0)
167 return CMD_RET_FAILURE;
168
169 if (reason >= MAX_REBOOT_REASONS ||
170 !reboot_reasons[reason])
171 reason_str = "unknown";
172 else
173 reason_str = reboot_reasons[reason];
174
175 if (destarg)
176 env_set(destarg, reason_str);
177 else
178 printf("reboot reason: %s (%x)\n", reason_str, reason);
179
Neil Armstrong69800ec2019-08-06 17:21:02 +0200180 return CMD_RET_SUCCESS;
181}
182
183static cmd_tbl_t cmd_sm_sub[] = {
184 U_BOOT_CMD_MKENT(serial, 2, 1, do_sm_serial, "", ""),
Neil Armstrong385309c2019-08-06 17:28:36 +0200185 U_BOOT_CMD_MKENT(reboot_reason, 1, 1, do_sm_reboot_reason, "", ""),
Neil Armstrong69800ec2019-08-06 17:21:02 +0200186};
187
188static int do_sm(cmd_tbl_t *cmdtp, int flag, int argc,
189 char *const argv[])
190{
191 cmd_tbl_t *c;
192
193 if (argc < 2)
194 return CMD_RET_USAGE;
195
196 /* Strip off leading 'sm' command argument */
197 argc--;
198 argv++;
199
200 c = find_cmd_tbl(argv[0], &cmd_sm_sub[0], ARRAY_SIZE(cmd_sm_sub));
201
202 if (c)
203 return c->cmd(cmdtp, flag, argc, argv);
204 else
205 return CMD_RET_USAGE;
206}
207
208U_BOOT_CMD(
209 sm, 5, 0, do_sm,
210 "Secure Monitor Control",
Neil Armstrong385309c2019-08-06 17:28:36 +0200211 "serial <address> - read chip unique id to memory address\n"
212 "sm reboot_reason [name] - get reboot reason and store to to environment"
Neil Armstrong69800ec2019-08-06 17:21:02 +0200213);