blob: ebbb880b71ea8f490b0eaa7ac5474bb843034f8c [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 Glass6b9f0102020-05-10 11:40:06 -060014#include <asm/ptrace.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060015#include <linux/bitops.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070016#include <linux/err.h>
Beniamino Galvani38e1a602016-05-08 08:30:17 +020017#include <linux/kernel.h>
Neil Armstrong385309c2019-08-06 17:28:36 +020018#include <dm.h>
19#include <linux/bitfield.h>
20#include <regmap.h>
21#include <syscon.h>
Beniamino Galvani38e1a602016-05-08 08:30:17 +020022
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
Neil Armstrong63f475c2019-06-12 11:49:06 +020027#define FN_CHIP_ID 0x82000044
Beniamino Galvani38e1a602016-05-08 08:30:17 +020028
29static void *shmem_input;
30static void *shmem_output;
31
32static void meson_init_shmem(void)
33{
34 struct pt_regs regs;
35
36 if (shmem_input && shmem_output)
37 return;
38
39 regs.regs[0] = FN_GET_SHARE_MEM_INPUT_BASE;
40 smc_call(&regs);
41 shmem_input = (void *)regs.regs[0];
42
43 regs.regs[0] = FN_GET_SHARE_MEM_OUTPUT_BASE;
44 smc_call(&regs);
45 shmem_output = (void *)regs.regs[0];
46
47 debug("Secure Monitor shmem: 0x%p 0x%p\n", shmem_input, shmem_output);
48}
49
50ssize_t meson_sm_read_efuse(uintptr_t offset, void *buffer, size_t size)
51{
52 struct pt_regs regs;
53
54 meson_init_shmem();
55
56 regs.regs[0] = FN_EFUSE_READ;
57 regs.regs[1] = offset;
58 regs.regs[2] = size;
59
60 smc_call(&regs);
61
62 if (regs.regs[0] == 0)
63 return -1;
64
65 memcpy(buffer, shmem_output, min(size, regs.regs[0]));
66
67 return regs.regs[0];
68}
Neil Armstrong63f475c2019-06-12 11:49:06 +020069
70#define SM_CHIP_ID_LENGTH 119
71#define SM_CHIP_ID_OFFSET 4
72#define SM_CHIP_ID_SIZE 12
73
74int meson_sm_get_serial(void *buffer, size_t size)
75{
76 struct pt_regs regs;
77
78 meson_init_shmem();
79
80 regs.regs[0] = FN_CHIP_ID;
81 regs.regs[1] = 0;
82 regs.regs[2] = 0;
83
84 smc_call(&regs);
85
86 memcpy(buffer, shmem_output + SM_CHIP_ID_OFFSET,
87 min_t(size_t, size, SM_CHIP_ID_SIZE));
88
89 return 0;
90}
Neil Armstrong69800ec2019-08-06 17:21:02 +020091
Neil Armstrong385309c2019-08-06 17:28:36 +020092#define AO_SEC_SD_CFG15 0xfc
93#define REBOOT_REASON_MASK GENMASK(15, 12)
94
95int meson_sm_get_reboot_reason(void)
96{
97 struct regmap *regmap;
98 int nodeoffset;
99 ofnode node;
100 unsigned int reason;
101
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",
107 __func__);
108 return -ENODEV;
109 }
110
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__);
116 return -EINVAL;
117 }
118
119 regmap_read(regmap, AO_SEC_SD_CFG15, &reason);
120
121 /* The SMC call is not used, we directly use AO_SEC_SD_CFG15 */
122 return FIELD_GET(REBOOT_REASON_MASK, reason);
123}
124
Simon Glassed38aef2020-05-10 11:40:03 -0600125static int do_sm_serial(struct cmd_tbl *cmdtp, int flag, int argc,
Neil Armstrong69800ec2019-08-06 17:21:02 +0200126 char *const argv[])
127{
128 ulong address;
129 int ret;
130
131 if (argc < 2)
132 return CMD_RET_USAGE;
133
134 address = simple_strtoul(argv[1], NULL, 0);
135
136 ret = meson_sm_get_serial((void *)address, SM_CHIP_ID_SIZE);
137 if (ret)
138 return CMD_RET_FAILURE;
139
Neil Armstrong385309c2019-08-06 17:28:36 +0200140 return CMD_RET_SUCCESS;
141}
142
143#define MAX_REBOOT_REASONS 14
144
145static 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",
159};
160
Simon Glassed38aef2020-05-10 11:40:03 -0600161static int do_sm_reboot_reason(struct cmd_tbl *cmdtp, int flag, int argc,
162 char *const argv[])
Neil Armstrong385309c2019-08-06 17:28:36 +0200163{
164 const char *reason_str;
165 char *destarg = NULL;
166 int reason;
167
168 if (argc > 1)
169 destarg = argv[1];
170
171 reason = meson_sm_get_reboot_reason();
172 if (reason < 0)
173 return CMD_RET_FAILURE;
174
175 if (reason >= MAX_REBOOT_REASONS ||
176 !reboot_reasons[reason])
177 reason_str = "unknown";
178 else
179 reason_str = reboot_reasons[reason];
180
181 if (destarg)
182 env_set(destarg, reason_str);
183 else
184 printf("reboot reason: %s (%x)\n", reason_str, reason);
185
Neil Armstrong69800ec2019-08-06 17:21:02 +0200186 return CMD_RET_SUCCESS;
187}
188
Simon Glassed38aef2020-05-10 11:40:03 -0600189static struct cmd_tbl cmd_sm_sub[] = {
Neil Armstrong69800ec2019-08-06 17:21:02 +0200190 U_BOOT_CMD_MKENT(serial, 2, 1, do_sm_serial, "", ""),
Neil Armstrong385309c2019-08-06 17:28:36 +0200191 U_BOOT_CMD_MKENT(reboot_reason, 1, 1, do_sm_reboot_reason, "", ""),
Neil Armstrong69800ec2019-08-06 17:21:02 +0200192};
193
Simon Glassed38aef2020-05-10 11:40:03 -0600194static int do_sm(struct cmd_tbl *cmdtp, int flag, int argc,
Neil Armstrong69800ec2019-08-06 17:21:02 +0200195 char *const argv[])
196{
Simon Glassed38aef2020-05-10 11:40:03 -0600197 struct cmd_tbl *c;
Neil Armstrong69800ec2019-08-06 17:21:02 +0200198
199 if (argc < 2)
200 return CMD_RET_USAGE;
201
202 /* Strip off leading 'sm' command argument */
203 argc--;
204 argv++;
205
206 c = find_cmd_tbl(argv[0], &cmd_sm_sub[0], ARRAY_SIZE(cmd_sm_sub));
207
208 if (c)
209 return c->cmd(cmdtp, flag, argc, argv);
210 else
211 return CMD_RET_USAGE;
212}
213
214U_BOOT_CMD(
215 sm, 5, 0, do_sm,
216 "Secure Monitor Control",
Neil Armstrong385309c2019-08-06 17:28:36 +0200217 "serial <address> - read chip unique id to memory address\n"
218 "sm reboot_reason [name] - get reboot reason and store to to environment"
Neil Armstrong69800ec2019-08-06 17:21:02 +0200219);