Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Beniamino Galvani | 38e1a60 | 2016-05-08 08:30:17 +0200 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2016 Beniamino Galvani <b.galvani@gmail.com> |
| 4 | * |
Beniamino Galvani | 38e1a60 | 2016-05-08 08:30:17 +0200 | [diff] [blame] | 5 | * Secure monitor calls. |
| 6 | */ |
| 7 | |
Alexey Romanov | df1a71c | 2023-09-21 11:13:40 +0300 | [diff] [blame] | 8 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Alexey Romanov | df1a71c | 2023-09-21 11:13:40 +0300 | [diff] [blame] | 10 | #include <regmap.h> |
Alexey Romanov | babf17e | 2023-09-21 11:13:41 +0300 | [diff] [blame] | 11 | #include <sm.h> |
Alexey Romanov | df1a71c | 2023-09-21 11:13:40 +0300 | [diff] [blame] | 12 | #include <syscon.h> |
Evgeny Bachinin | 98eb5e4 | 2025-02-10 20:50:16 +0300 | [diff] [blame] | 13 | #include <asm/arch/boot.h> |
Neil Armstrong | 63f475c | 2019-06-12 11:49:06 +0200 | [diff] [blame] | 14 | #include <asm/arch/sm.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 15 | #include <asm/cache.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 16 | #include <asm/global_data.h> |
Simon Glass | 6b9f010 | 2020-05-10 11:40:06 -0600 | [diff] [blame] | 17 | #include <asm/ptrace.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 18 | #include <linux/bitops.h> |
Evgeny Bachinin | 98eb5e4 | 2025-02-10 20:50:16 +0300 | [diff] [blame] | 19 | #include <linux/compiler_attributes.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 20 | #include <linux/err.h> |
Beniamino Galvani | 38e1a60 | 2016-05-08 08:30:17 +0200 | [diff] [blame] | 21 | #include <linux/kernel.h> |
Neil Armstrong | 385309c | 2019-08-06 17:28:36 +0200 | [diff] [blame] | 22 | #include <linux/bitfield.h> |
Alexey Romanov | babf17e | 2023-09-21 11:13:41 +0300 | [diff] [blame] | 23 | #include <meson/sm.h> |
Beniamino Galvani | 38e1a60 | 2016-05-08 08:30:17 +0200 | [diff] [blame] | 24 | |
Alexey Romanov | babf17e | 2023-09-21 11:13:41 +0300 | [diff] [blame] | 25 | static inline struct udevice *meson_get_sm_device(void) |
Beniamino Galvani | 38e1a60 | 2016-05-08 08:30:17 +0200 | [diff] [blame] | 26 | { |
Alexey Romanov | babf17e | 2023-09-21 11:13:41 +0300 | [diff] [blame] | 27 | struct udevice *dev; |
| 28 | int err; |
Beniamino Galvani | 38e1a60 | 2016-05-08 08:30:17 +0200 | [diff] [blame] | 29 | |
Alexey Romanov | babf17e | 2023-09-21 11:13:41 +0300 | [diff] [blame] | 30 | 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 Galvani | 38e1a60 | 2016-05-08 08:30:17 +0200 | [diff] [blame] | 35 | |
Alexey Romanov | babf17e | 2023-09-21 11:13:41 +0300 | [diff] [blame] | 36 | return dev; |
Beniamino Galvani | 38e1a60 | 2016-05-08 08:30:17 +0200 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | ssize_t meson_sm_read_efuse(uintptr_t offset, void *buffer, size_t size) |
| 40 | { |
Alexey Romanov | babf17e | 2023-09-21 11:13:41 +0300 | [diff] [blame] | 41 | struct udevice *dev; |
| 42 | struct pt_regs regs = { 0 }; |
| 43 | int err; |
Beniamino Galvani | 38e1a60 | 2016-05-08 08:30:17 +0200 | [diff] [blame] | 44 | |
Alexey Romanov | babf17e | 2023-09-21 11:13:41 +0300 | [diff] [blame] | 45 | dev = meson_get_sm_device(); |
| 46 | if (IS_ERR(dev)) |
| 47 | return PTR_ERR(dev); |
Beniamino Galvani | 38e1a60 | 2016-05-08 08:30:17 +0200 | [diff] [blame] | 48 | |
Beniamino Galvani | 38e1a60 | 2016-05-08 08:30:17 +0200 | [diff] [blame] | 49 | regs.regs[1] = offset; |
| 50 | regs.regs[2] = size; |
| 51 | |
Alexey Romanov | babf17e | 2023-09-21 11:13:41 +0300 | [diff] [blame] | 52 | err = sm_call_read(dev, buffer, size, |
| 53 | MESON_SMC_CMD_EFUSE_READ, ®s); |
| 54 | if (err < 0) |
| 55 | pr_err("Failed to read efuse memory (%d)\n", err); |
Beniamino Galvani | 38e1a60 | 2016-05-08 08:30:17 +0200 | [diff] [blame] | 56 | |
Alexey Romanov | babf17e | 2023-09-21 11:13:41 +0300 | [diff] [blame] | 57 | return err; |
Beniamino Galvani | 38e1a60 | 2016-05-08 08:30:17 +0200 | [diff] [blame] | 58 | } |
Neil Armstrong | 63f475c | 2019-06-12 11:49:06 +0200 | [diff] [blame] | 59 | |
Vyacheslav Bocharov | 14eb82c | 2021-10-05 15:00:03 +0300 | [diff] [blame] | 60 | ssize_t meson_sm_write_efuse(uintptr_t offset, void *buffer, size_t size) |
| 61 | { |
Alexey Romanov | babf17e | 2023-09-21 11:13:41 +0300 | [diff] [blame] | 62 | struct udevice *dev; |
| 63 | struct pt_regs regs = { 0 }; |
| 64 | int err; |
Vyacheslav Bocharov | 14eb82c | 2021-10-05 15:00:03 +0300 | [diff] [blame] | 65 | |
Alexey Romanov | babf17e | 2023-09-21 11:13:41 +0300 | [diff] [blame] | 66 | dev = meson_get_sm_device(); |
| 67 | if (IS_ERR(dev)) |
| 68 | return PTR_ERR(dev); |
Vyacheslav Bocharov | 14eb82c | 2021-10-05 15:00:03 +0300 | [diff] [blame] | 69 | |
Vyacheslav Bocharov | 14eb82c | 2021-10-05 15:00:03 +0300 | [diff] [blame] | 70 | regs.regs[1] = offset; |
| 71 | regs.regs[2] = size; |
| 72 | |
Alexey Romanov | babf17e | 2023-09-21 11:13:41 +0300 | [diff] [blame] | 73 | err = sm_call_write(dev, buffer, size, |
| 74 | MESON_SMC_CMD_EFUSE_WRITE, ®s); |
| 75 | if (err < 0) |
| 76 | pr_err("Failed to write efuse memory (%d)\n", err); |
Vyacheslav Bocharov | 14eb82c | 2021-10-05 15:00:03 +0300 | [diff] [blame] | 77 | |
Alexey Romanov | babf17e | 2023-09-21 11:13:41 +0300 | [diff] [blame] | 78 | return err; |
Vyacheslav Bocharov | 14eb82c | 2021-10-05 15:00:03 +0300 | [diff] [blame] | 79 | } |
| 80 | |
Evgeny Bachinin | 98eb5e4 | 2025-02-10 20:50:16 +0300 | [diff] [blame] | 81 | /* |
| 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 | */ |
| 100 | enum { |
| 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 Armstrong | 63f475c | 2019-06-12 11:49:06 +0200 | [diff] [blame] | 106 | |
Evgeny Bachinin | 98eb5e4 | 2025-02-10 20:50:16 +0300 | [diff] [blame] | 107 | /* 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 | */ |
| 118 | struct chip_id_efuse { |
| 119 | u32 version; |
| 120 | u8 raw[MESON_CHIP_ID_SZ]; /* payload */ |
| 121 | } __packed; |
| 122 | |
| 123 | static 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 | |
| 132 | int meson_sm_get_chip_id(struct meson_sm_chip_id *chip_id) |
Neil Armstrong | 63f475c | 2019-06-12 11:49:06 +0200 | [diff] [blame] | 133 | { |
Alexey Romanov | babf17e | 2023-09-21 11:13:41 +0300 | [diff] [blame] | 134 | struct udevice *dev; |
Evgeny Bachinin | 98eb5e4 | 2025-02-10 20:50:16 +0300 | [diff] [blame] | 135 | union meson_cpu_id socinfo; |
Alexey Romanov | babf17e | 2023-09-21 11:13:41 +0300 | [diff] [blame] | 136 | struct pt_regs regs = { 0 }; |
Evgeny Bachinin | 98eb5e4 | 2025-02-10 20:50:16 +0300 | [diff] [blame] | 137 | struct chip_id_efuse chip_id_efuse; |
Alexey Romanov | babf17e | 2023-09-21 11:13:41 +0300 | [diff] [blame] | 138 | int err; |
Neil Armstrong | 63f475c | 2019-06-12 11:49:06 +0200 | [diff] [blame] | 139 | |
Alexey Romanov | babf17e | 2023-09-21 11:13:41 +0300 | [diff] [blame] | 140 | dev = meson_get_sm_device(); |
| 141 | if (IS_ERR(dev)) |
| 142 | return PTR_ERR(dev); |
Neil Armstrong | 63f475c | 2019-06-12 11:49:06 +0200 | [diff] [blame] | 143 | |
Evgeny Bachinin | 98eb5e4 | 2025-02-10 20:50:16 +0300 | [diff] [blame] | 144 | /* |
| 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 Romanov | babf17e | 2023-09-21 11:13:41 +0300 | [diff] [blame] | 151 | MESON_SMC_CMD_CHIP_ID_GET, ®s); |
Evgeny Bachinin | 98eb5e4 | 2025-02-10 20:50:16 +0300 | [diff] [blame] | 152 | if (err < 0) { |
| 153 | pr_err("Failed to read chip_id (%d)\n", err); |
| 154 | return err; |
| 155 | } |
Neil Armstrong | 63f475c | 2019-06-12 11:49:06 +0200 | [diff] [blame] | 156 | |
Evgeny Bachinin | 98eb5e4 | 2025-02-10 20:50:16 +0300 | [diff] [blame] | 157 | 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 Armstrong | 63f475c | 2019-06-12 11:49:06 +0200 | [diff] [blame] | 180 | |
| 181 | return 0; |
| 182 | } |
Neil Armstrong | 69800ec | 2019-08-06 17:21:02 +0200 | [diff] [blame] | 183 | |
Evgeny Bachinin | 98eb5e4 | 2025-02-10 20:50:16 +0300 | [diff] [blame] | 184 | int 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 Armstrong | 385309c | 2019-08-06 17:28:36 +0200 | [diff] [blame] | 206 | #define AO_SEC_SD_CFG15 0xfc |
| 207 | #define REBOOT_REASON_MASK GENMASK(15, 12) |
| 208 | |
| 209 | int 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 Romanov | 92404a1 | 2023-05-31 12:31:54 +0300 | [diff] [blame] | 238 | |
| 239 | int meson_sm_pwrdm_set(size_t index, int cmd) |
| 240 | { |
Alexey Romanov | babf17e | 2023-09-21 11:13:41 +0300 | [diff] [blame] | 241 | struct udevice *dev; |
| 242 | struct pt_regs regs = { 0 }; |
| 243 | int err; |
Alexey Romanov | 92404a1 | 2023-05-31 12:31:54 +0300 | [diff] [blame] | 244 | |
Alexey Romanov | babf17e | 2023-09-21 11:13:41 +0300 | [diff] [blame] | 245 | dev = meson_get_sm_device(); |
| 246 | if (IS_ERR(dev)) |
| 247 | return PTR_ERR(dev); |
| 248 | |
Alexey Romanov | 92404a1 | 2023-05-31 12:31:54 +0300 | [diff] [blame] | 249 | regs.regs[1] = index; |
| 250 | regs.regs[2] = cmd; |
| 251 | |
Alexey Romanov | babf17e | 2023-09-21 11:13:41 +0300 | [diff] [blame] | 252 | err = sm_call(dev, MESON_SMC_CMD_PWRDM_SET, NULL, ®s); |
| 253 | if (err) |
| 254 | pr_err("Failed to %s power domain ind=%zu (%d)\n", cmd == PWRDM_ON ? |
| 255 | "enable" : "disable", index, err); |
Alexey Romanov | 92404a1 | 2023-05-31 12:31:54 +0300 | [diff] [blame] | 256 | |
Alexey Romanov | babf17e | 2023-09-21 11:13:41 +0300 | [diff] [blame] | 257 | return err; |
Alexey Romanov | 92404a1 | 2023-05-31 12:31:54 +0300 | [diff] [blame] | 258 | } |