blob: b1f91ca29ceccc368508ad66fc7dfad4a4d6fa01 [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
Alexey Romanovdf1a71c2023-09-21 11:13:40 +03008#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Alexey Romanovdf1a71c2023-09-21 11:13:40 +030010#include <regmap.h>
Alexey Romanovbabf17e2023-09-21 11:13:41 +030011#include <sm.h>
Alexey Romanovdf1a71c2023-09-21 11:13:40 +030012#include <syscon.h>
Evgeny Bachinin98eb5e42025-02-10 20:50:16 +030013#include <asm/arch/boot.h>
Neil Armstrong63f475c2019-06-12 11:49:06 +020014#include <asm/arch/sm.h>
Simon Glass274e0b02020-05-10 11:39:56 -060015#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060016#include <asm/global_data.h>
Simon Glass6b9f0102020-05-10 11:40:06 -060017#include <asm/ptrace.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060018#include <linux/bitops.h>
Evgeny Bachinin98eb5e42025-02-10 20:50:16 +030019#include <linux/compiler_attributes.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070020#include <linux/err.h>
Beniamino Galvani38e1a602016-05-08 08:30:17 +020021#include <linux/kernel.h>
Neil Armstrong385309c2019-08-06 17:28:36 +020022#include <linux/bitfield.h>
Alexey Romanovbabf17e2023-09-21 11:13:41 +030023#include <meson/sm.h>
Beniamino Galvani38e1a602016-05-08 08:30:17 +020024
Alexey Romanovbabf17e2023-09-21 11:13:41 +030025static inline struct udevice *meson_get_sm_device(void)
Beniamino Galvani38e1a602016-05-08 08:30:17 +020026{
Alexey Romanovbabf17e2023-09-21 11:13:41 +030027 struct udevice *dev;
28 int err;
Beniamino Galvani38e1a602016-05-08 08:30:17 +020029
Alexey Romanovbabf17e2023-09-21 11:13:41 +030030 err = uclass_first_device_err(UCLASS_SM, &dev);
31 if (err) {
32 pr_err("Mesom SM device not found\n");
33 return ERR_PTR(err);
34 }
Beniamino Galvani38e1a602016-05-08 08:30:17 +020035
Alexey Romanovbabf17e2023-09-21 11:13:41 +030036 return dev;
Beniamino Galvani38e1a602016-05-08 08:30:17 +020037}
38
39ssize_t meson_sm_read_efuse(uintptr_t offset, void *buffer, size_t size)
40{
Alexey Romanovbabf17e2023-09-21 11:13:41 +030041 struct udevice *dev;
42 struct pt_regs regs = { 0 };
43 int err;
Beniamino Galvani38e1a602016-05-08 08:30:17 +020044
Alexey Romanovbabf17e2023-09-21 11:13:41 +030045 dev = meson_get_sm_device();
46 if (IS_ERR(dev))
47 return PTR_ERR(dev);
Beniamino Galvani38e1a602016-05-08 08:30:17 +020048
Beniamino Galvani38e1a602016-05-08 08:30:17 +020049 regs.regs[1] = offset;
50 regs.regs[2] = size;
51
Alexey Romanovbabf17e2023-09-21 11:13:41 +030052 err = sm_call_read(dev, buffer, size,
53 MESON_SMC_CMD_EFUSE_READ, &regs);
54 if (err < 0)
55 pr_err("Failed to read efuse memory (%d)\n", err);
Beniamino Galvani38e1a602016-05-08 08:30:17 +020056
Alexey Romanovbabf17e2023-09-21 11:13:41 +030057 return err;
Beniamino Galvani38e1a602016-05-08 08:30:17 +020058}
Neil Armstrong63f475c2019-06-12 11:49:06 +020059
Vyacheslav Bocharov14eb82c2021-10-05 15:00:03 +030060ssize_t meson_sm_write_efuse(uintptr_t offset, void *buffer, size_t size)
61{
Alexey Romanovbabf17e2023-09-21 11:13:41 +030062 struct udevice *dev;
63 struct pt_regs regs = { 0 };
64 int err;
Vyacheslav Bocharov14eb82c2021-10-05 15:00:03 +030065
Alexey Romanovbabf17e2023-09-21 11:13:41 +030066 dev = meson_get_sm_device();
67 if (IS_ERR(dev))
68 return PTR_ERR(dev);
Vyacheslav Bocharov14eb82c2021-10-05 15:00:03 +030069
Vyacheslav Bocharov14eb82c2021-10-05 15:00:03 +030070 regs.regs[1] = offset;
71 regs.regs[2] = size;
72
Alexey Romanovbabf17e2023-09-21 11:13:41 +030073 err = sm_call_write(dev, buffer, size,
74 MESON_SMC_CMD_EFUSE_WRITE, &regs);
75 if (err < 0)
76 pr_err("Failed to write efuse memory (%d)\n", err);
Vyacheslav Bocharov14eb82c2021-10-05 15:00:03 +030077
Alexey Romanovbabf17e2023-09-21 11:13:41 +030078 return err;
Vyacheslav Bocharov14eb82c2021-10-05 15:00:03 +030079}
80
Evgeny Bachinin98eb5e42025-02-10 20:50:16 +030081/*
82 * Helps to handle two flavors of cpu_id layouts:
83 *
84 * - in-register view (value read from cpu_id reg, a.k.a. socinfo):
85 * +-----------+------------+------------+------------+
86 * | family_id | package_id | chip_rev | layout_rev |
87 * +-----------+------------+------------+------------+
88 * | 31 24 | 23 16 | 15 8 | 7 0 |
89 * +-----------+------------+------------+------------+
90 *
91 * - in-efuse view (value, residing inside efuse/shmem data usually for
92 * chip_id v2. Chip_id v1 does not contain cpu_id value inside efuse
93 * data (i.e. in chip_id_efuse)):
94 * +-----------+------------+------------+------------+
95 * | family_id | chip_rev | package_id | layout_rev |
96 * +-----------+------------+------------+------------+
97 * | 31 24 | 23 16 | 15 8 | 7 0 |
98 * +-----------+------------+------------+------------+
99 */
100enum {
101 /* In-register view of cpu_id */
102 CPU_ID_REG_MAJOR, /* 31-24 bits */
103 CPU_ID_REG_PACK, /* 23-16 bits */
104 CPU_ID_REG_MINOR, /* 15-8 bits */
105 CPU_ID_REG_MISC, /* 7-0 bits */
Neil Armstrong63f475c2019-06-12 11:49:06 +0200106
Evgeny Bachinin98eb5e42025-02-10 20:50:16 +0300107 /* In-efuse view of cpu_id */
108 CPU_ID_MAJOR = CPU_ID_REG_MAJOR,
109 CPU_ID_PACK = CPU_ID_REG_MINOR,
110 CPU_ID_MINOR = CPU_ID_REG_PACK,
111 CPU_ID_MISC = CPU_ID_REG_MISC,
112};
113
114/*
115 * This is a beginning chunk of the whole efuse storage area, containing
116 * data related to chip_id only
117 */
118struct chip_id_efuse {
119 u32 version;
120 u8 raw[MESON_CHIP_ID_SZ]; /* payload */
121} __packed;
122
123static void meson_sm_serial_reverse(u8 serial[SM_SERIAL_SIZE])
124{
125 for (int i = 0; i < SM_SERIAL_SIZE / 2; i++) {
126 int k = SM_SERIAL_SIZE - 1 - i;
127
128 swap(serial[i], serial[k]);
129 }
130}
131
132int meson_sm_get_chip_id(struct meson_sm_chip_id *chip_id)
Neil Armstrong63f475c2019-06-12 11:49:06 +0200133{
Alexey Romanovbabf17e2023-09-21 11:13:41 +0300134 struct udevice *dev;
Evgeny Bachinin98eb5e42025-02-10 20:50:16 +0300135 union meson_cpu_id socinfo;
Alexey Romanovbabf17e2023-09-21 11:13:41 +0300136 struct pt_regs regs = { 0 };
Evgeny Bachinin98eb5e42025-02-10 20:50:16 +0300137 struct chip_id_efuse chip_id_efuse;
Alexey Romanovbabf17e2023-09-21 11:13:41 +0300138 int err;
Neil Armstrong63f475c2019-06-12 11:49:06 +0200139
Alexey Romanovbabf17e2023-09-21 11:13:41 +0300140 dev = meson_get_sm_device();
141 if (IS_ERR(dev))
142 return PTR_ERR(dev);
Neil Armstrong63f475c2019-06-12 11:49:06 +0200143
Evgeny Bachinin98eb5e42025-02-10 20:50:16 +0300144 /*
145 * Request v2. If not supported by secure monitor, then v1 should be
146 * returned.
147 */
148 regs.regs[1] = 2;
149
150 err = sm_call_read(dev, &chip_id_efuse, sizeof(chip_id_efuse),
Alexey Romanovbabf17e2023-09-21 11:13:41 +0300151 MESON_SMC_CMD_CHIP_ID_GET, &regs);
Evgeny Bachinin98eb5e42025-02-10 20:50:16 +0300152 if (err < 0) {
153 pr_err("Failed to read chip_id (%d)\n", err);
154 return err;
155 }
Neil Armstrong63f475c2019-06-12 11:49:06 +0200156
Evgeny Bachinin98eb5e42025-02-10 20:50:16 +0300157 if (chip_id_efuse.version == 2) {
158 memcpy((u8 *)chip_id, chip_id_efuse.raw,
159 sizeof(struct meson_sm_chip_id));
160 return 0;
161 }
162
163 /*
164 * Legacy chip_id (v1) read out, transform data
165 * to expected order format (little-endian)
166 */
167 memcpy(chip_id->serial, chip_id_efuse.raw, sizeof(chip_id->serial));
168 meson_sm_serial_reverse(chip_id->serial);
169
170 socinfo.val = meson_get_socinfo();
171 if (!socinfo.val)
172 return -ENODEV;
173
174 chip_id->cpu_id = (union meson_cpu_id){
175 .raw[CPU_ID_MAJOR] = socinfo.raw[CPU_ID_REG_MAJOR],
176 .raw[CPU_ID_PACK] = socinfo.raw[CPU_ID_REG_PACK],
177 .raw[CPU_ID_MINOR] = socinfo.raw[CPU_ID_REG_MINOR],
178 .raw[CPU_ID_MISC] = socinfo.raw[CPU_ID_REG_MISC],
179 };
Neil Armstrong63f475c2019-06-12 11:49:06 +0200180
181 return 0;
182}
Neil Armstrong69800ec2019-08-06 17:21:02 +0200183
Evgeny Bachinin98eb5e42025-02-10 20:50:16 +0300184int meson_sm_get_serial(void *buffer, size_t size)
185{
186 struct meson_sm_chip_id chip_id;
187 int ret;
188
189 if (size < SM_SERIAL_SIZE)
190 return -EINVAL;
191
192 ret = meson_sm_get_chip_id(&chip_id);
193 if (ret)
194 return ret;
195
196 /*
197 * The order of serial inside chip_id and serial which function must
198 * return does not match: stick here to big-endian for backward
199 * compatibility.
200 */
201 meson_sm_serial_reverse(chip_id.serial);
202 memcpy(buffer, chip_id.serial, sizeof(chip_id.serial));
203 return ret;
204}
205
Neil Armstrong385309c2019-08-06 17:28:36 +0200206#define AO_SEC_SD_CFG15 0xfc
207#define REBOOT_REASON_MASK GENMASK(15, 12)
208
209int meson_sm_get_reboot_reason(void)
210{
211 struct regmap *regmap;
212 int nodeoffset;
213 ofnode node;
214 unsigned int reason;
215
216 /* find the offset of compatible node */
217 nodeoffset = fdt_node_offset_by_compatible(gd->fdt_blob, -1,
218 "amlogic,meson-gx-ao-secure");
219 if (nodeoffset < 0) {
220 printf("%s: failed to get amlogic,meson-gx-ao-secure\n",
221 __func__);
222 return -ENODEV;
223 }
224
225 /* get regmap from the syscon node */
226 node = offset_to_ofnode(nodeoffset);
227 regmap = syscon_node_to_regmap(node);
228 if (IS_ERR(regmap)) {
229 printf("%s: failed to get regmap\n", __func__);
230 return -EINVAL;
231 }
232
233 regmap_read(regmap, AO_SEC_SD_CFG15, &reason);
234
235 /* The SMC call is not used, we directly use AO_SEC_SD_CFG15 */
236 return FIELD_GET(REBOOT_REASON_MASK, reason);
237}
Alexey Romanov92404a12023-05-31 12:31:54 +0300238
239int meson_sm_pwrdm_set(size_t index, int cmd)
240{
Alexey Romanovbabf17e2023-09-21 11:13:41 +0300241 struct udevice *dev;
242 struct pt_regs regs = { 0 };
243 int err;
Alexey Romanov92404a12023-05-31 12:31:54 +0300244
Alexey Romanovbabf17e2023-09-21 11:13:41 +0300245 dev = meson_get_sm_device();
246 if (IS_ERR(dev))
247 return PTR_ERR(dev);
248
Alexey Romanov92404a12023-05-31 12:31:54 +0300249 regs.regs[1] = index;
250 regs.regs[2] = cmd;
251
Alexey Romanovbabf17e2023-09-21 11:13:41 +0300252 err = sm_call(dev, MESON_SMC_CMD_PWRDM_SET, NULL, &regs);
253 if (err)
254 pr_err("Failed to %s power domain ind=%zu (%d)\n", cmd == PWRDM_ON ?
255 "enable" : "disable", index, err);
Alexey Romanov92404a12023-05-31 12:31:54 +0300256
Alexey Romanovbabf17e2023-09-21 11:13:41 +0300257 return err;
Alexey Romanov92404a12023-05-31 12:31:54 +0300258}