blob: e6da4de53434254d56a449ba8f9a12e3b94a2e7d [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>
Simon Glassed38aef2020-05-10 11:40:03 -06009#include <command.h>
10#include <env.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Neil Armstrong63f475c2019-06-12 11:49:06 +020012#include <asm/arch/sm.h>
Simon Glass274e0b02020-05-10 11:39:56 -060013#include <asm/cache.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070014#include <linux/err.h>
Beniamino Galvani38e1a602016-05-08 08:30:17 +020015#include <linux/kernel.h>
Neil Armstrong385309c2019-08-06 17:28:36 +020016#include <dm.h>
17#include <linux/bitfield.h>
18#include <regmap.h>
19#include <syscon.h>
Beniamino Galvani38e1a602016-05-08 08:30:17 +020020
21#define FN_GET_SHARE_MEM_INPUT_BASE 0x82000020
22#define FN_GET_SHARE_MEM_OUTPUT_BASE 0x82000021
23#define FN_EFUSE_READ 0x82000030
24#define FN_EFUSE_WRITE 0x82000031
Neil Armstrong63f475c2019-06-12 11:49:06 +020025#define FN_CHIP_ID 0x82000044
Beniamino Galvani38e1a602016-05-08 08:30:17 +020026
27static void *shmem_input;
28static void *shmem_output;
29
30static void meson_init_shmem(void)
31{
32 struct pt_regs regs;
33
34 if (shmem_input && shmem_output)
35 return;
36
37 regs.regs[0] = FN_GET_SHARE_MEM_INPUT_BASE;
38 smc_call(&regs);
39 shmem_input = (void *)regs.regs[0];
40
41 regs.regs[0] = FN_GET_SHARE_MEM_OUTPUT_BASE;
42 smc_call(&regs);
43 shmem_output = (void *)regs.regs[0];
44
45 debug("Secure Monitor shmem: 0x%p 0x%p\n", shmem_input, shmem_output);
46}
47
48ssize_t meson_sm_read_efuse(uintptr_t offset, void *buffer, size_t size)
49{
50 struct pt_regs regs;
51
52 meson_init_shmem();
53
54 regs.regs[0] = FN_EFUSE_READ;
55 regs.regs[1] = offset;
56 regs.regs[2] = size;
57
58 smc_call(&regs);
59
60 if (regs.regs[0] == 0)
61 return -1;
62
63 memcpy(buffer, shmem_output, min(size, regs.regs[0]));
64
65 return regs.regs[0];
66}
Neil Armstrong63f475c2019-06-12 11:49:06 +020067
68#define SM_CHIP_ID_LENGTH 119
69#define SM_CHIP_ID_OFFSET 4
70#define SM_CHIP_ID_SIZE 12
71
72int meson_sm_get_serial(void *buffer, size_t size)
73{
74 struct pt_regs regs;
75
76 meson_init_shmem();
77
78 regs.regs[0] = FN_CHIP_ID;
79 regs.regs[1] = 0;
80 regs.regs[2] = 0;
81
82 smc_call(&regs);
83
84 memcpy(buffer, shmem_output + SM_CHIP_ID_OFFSET,
85 min_t(size_t, size, SM_CHIP_ID_SIZE));
86
87 return 0;
88}
Neil Armstrong69800ec2019-08-06 17:21:02 +020089
Neil Armstrong385309c2019-08-06 17:28:36 +020090#define AO_SEC_SD_CFG15 0xfc
91#define REBOOT_REASON_MASK GENMASK(15, 12)
92
93int meson_sm_get_reboot_reason(void)
94{
95 struct regmap *regmap;
96 int nodeoffset;
97 ofnode node;
98 unsigned int reason;
99
100 /* find the offset of compatible node */
101 nodeoffset = fdt_node_offset_by_compatible(gd->fdt_blob, -1,
102 "amlogic,meson-gx-ao-secure");
103 if (nodeoffset < 0) {
104 printf("%s: failed to get amlogic,meson-gx-ao-secure\n",
105 __func__);
106 return -ENODEV;
107 }
108
109 /* get regmap from the syscon node */
110 node = offset_to_ofnode(nodeoffset);
111 regmap = syscon_node_to_regmap(node);
112 if (IS_ERR(regmap)) {
113 printf("%s: failed to get regmap\n", __func__);
114 return -EINVAL;
115 }
116
117 regmap_read(regmap, AO_SEC_SD_CFG15, &reason);
118
119 /* The SMC call is not used, we directly use AO_SEC_SD_CFG15 */
120 return FIELD_GET(REBOOT_REASON_MASK, reason);
121}
122
Simon Glassed38aef2020-05-10 11:40:03 -0600123static int do_sm_serial(struct cmd_tbl *cmdtp, int flag, int argc,
Neil Armstrong69800ec2019-08-06 17:21:02 +0200124 char *const argv[])
125{
126 ulong address;
127 int ret;
128
129 if (argc < 2)
130 return CMD_RET_USAGE;
131
132 address = simple_strtoul(argv[1], NULL, 0);
133
134 ret = meson_sm_get_serial((void *)address, SM_CHIP_ID_SIZE);
135 if (ret)
136 return CMD_RET_FAILURE;
137
Neil Armstrong385309c2019-08-06 17:28:36 +0200138 return CMD_RET_SUCCESS;
139}
140
141#define MAX_REBOOT_REASONS 14
142
143static const char *reboot_reasons[MAX_REBOOT_REASONS] = {
144 [REBOOT_REASON_COLD] = "cold_boot",
145 [REBOOT_REASON_NORMAL] = "normal",
146 [REBOOT_REASON_RECOVERY] = "recovery",
147 [REBOOT_REASON_UPDATE] = "update",
148 [REBOOT_REASON_FASTBOOT] = "fastboot",
149 [REBOOT_REASON_SUSPEND_OFF] = "suspend_off",
150 [REBOOT_REASON_HIBERNATE] = "hibernate",
151 [REBOOT_REASON_BOOTLOADER] = "bootloader",
152 [REBOOT_REASON_SHUTDOWN_REBOOT] = "shutdown_reboot",
153 [REBOOT_REASON_RPMBP] = "rpmbp",
154 [REBOOT_REASON_CRASH_DUMP] = "crash_dump",
155 [REBOOT_REASON_KERNEL_PANIC] = "kernel_panic",
156 [REBOOT_REASON_WATCHDOG_REBOOT] = "watchdog_reboot",
157};
158
Simon Glassed38aef2020-05-10 11:40:03 -0600159static int do_sm_reboot_reason(struct cmd_tbl *cmdtp, int flag, int argc,
160 char *const argv[])
Neil Armstrong385309c2019-08-06 17:28:36 +0200161{
162 const char *reason_str;
163 char *destarg = NULL;
164 int reason;
165
166 if (argc > 1)
167 destarg = argv[1];
168
169 reason = meson_sm_get_reboot_reason();
170 if (reason < 0)
171 return CMD_RET_FAILURE;
172
173 if (reason >= MAX_REBOOT_REASONS ||
174 !reboot_reasons[reason])
175 reason_str = "unknown";
176 else
177 reason_str = reboot_reasons[reason];
178
179 if (destarg)
180 env_set(destarg, reason_str);
181 else
182 printf("reboot reason: %s (%x)\n", reason_str, reason);
183
Neil Armstrong69800ec2019-08-06 17:21:02 +0200184 return CMD_RET_SUCCESS;
185}
186
Simon Glassed38aef2020-05-10 11:40:03 -0600187static struct cmd_tbl cmd_sm_sub[] = {
Neil Armstrong69800ec2019-08-06 17:21:02 +0200188 U_BOOT_CMD_MKENT(serial, 2, 1, do_sm_serial, "", ""),
Neil Armstrong385309c2019-08-06 17:28:36 +0200189 U_BOOT_CMD_MKENT(reboot_reason, 1, 1, do_sm_reboot_reason, "", ""),
Neil Armstrong69800ec2019-08-06 17:21:02 +0200190};
191
Simon Glassed38aef2020-05-10 11:40:03 -0600192static int do_sm(struct cmd_tbl *cmdtp, int flag, int argc,
Neil Armstrong69800ec2019-08-06 17:21:02 +0200193 char *const argv[])
194{
Simon Glassed38aef2020-05-10 11:40:03 -0600195 struct cmd_tbl *c;
Neil Armstrong69800ec2019-08-06 17:21:02 +0200196
197 if (argc < 2)
198 return CMD_RET_USAGE;
199
200 /* Strip off leading 'sm' command argument */
201 argc--;
202 argv++;
203
204 c = find_cmd_tbl(argv[0], &cmd_sm_sub[0], ARRAY_SIZE(cmd_sm_sub));
205
206 if (c)
207 return c->cmd(cmdtp, flag, argc, argv);
208 else
209 return CMD_RET_USAGE;
210}
211
212U_BOOT_CMD(
213 sm, 5, 0, do_sm,
214 "Secure Monitor Control",
Neil Armstrong385309c2019-08-06 17:28:36 +0200215 "serial <address> - read chip unique id to memory address\n"
216 "sm reboot_reason [name] - get reboot reason and store to to environment"
Neil Armstrong69800ec2019-08-06 17:21:02 +0200217);