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 | |
| 8 | #include <common.h> |
Alexey Romanov | df1a71c | 2023-09-21 11:13:40 +0300 | [diff] [blame] | 9 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Alexey Romanov | df1a71c | 2023-09-21 11:13:40 +0300 | [diff] [blame] | 11 | #include <regmap.h> |
Alexey Romanov | babf17e | 2023-09-21 11:13:41 +0300 | [diff] [blame] | 12 | #include <sm.h> |
Alexey Romanov | df1a71c | 2023-09-21 11:13:40 +0300 | [diff] [blame] | 13 | #include <syscon.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> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 19 | #include <linux/err.h> |
Beniamino Galvani | 38e1a60 | 2016-05-08 08:30:17 +0200 | [diff] [blame] | 20 | #include <linux/kernel.h> |
Neil Armstrong | 385309c | 2019-08-06 17:28:36 +0200 | [diff] [blame] | 21 | #include <linux/bitfield.h> |
Alexey Romanov | babf17e | 2023-09-21 11:13:41 +0300 | [diff] [blame] | 22 | #include <meson/sm.h> |
Beniamino Galvani | 38e1a60 | 2016-05-08 08:30:17 +0200 | [diff] [blame] | 23 | |
Alexey Romanov | babf17e | 2023-09-21 11:13:41 +0300 | [diff] [blame] | 24 | static inline struct udevice *meson_get_sm_device(void) |
Beniamino Galvani | 38e1a60 | 2016-05-08 08:30:17 +0200 | [diff] [blame] | 25 | { |
Alexey Romanov | babf17e | 2023-09-21 11:13:41 +0300 | [diff] [blame] | 26 | struct udevice *dev; |
| 27 | int err; |
Beniamino Galvani | 38e1a60 | 2016-05-08 08:30:17 +0200 | [diff] [blame] | 28 | |
Alexey Romanov | babf17e | 2023-09-21 11:13:41 +0300 | [diff] [blame] | 29 | err = uclass_first_device_err(UCLASS_SM, &dev); |
| 30 | if (err) { |
| 31 | pr_err("Mesom SM device not found\n"); |
| 32 | return ERR_PTR(err); |
| 33 | } |
Beniamino Galvani | 38e1a60 | 2016-05-08 08:30:17 +0200 | [diff] [blame] | 34 | |
Alexey Romanov | babf17e | 2023-09-21 11:13:41 +0300 | [diff] [blame] | 35 | return dev; |
Beniamino Galvani | 38e1a60 | 2016-05-08 08:30:17 +0200 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | ssize_t meson_sm_read_efuse(uintptr_t offset, void *buffer, size_t size) |
| 39 | { |
Alexey Romanov | babf17e | 2023-09-21 11:13:41 +0300 | [diff] [blame] | 40 | struct udevice *dev; |
| 41 | struct pt_regs regs = { 0 }; |
| 42 | int err; |
Beniamino Galvani | 38e1a60 | 2016-05-08 08:30:17 +0200 | [diff] [blame] | 43 | |
Alexey Romanov | babf17e | 2023-09-21 11:13:41 +0300 | [diff] [blame] | 44 | dev = meson_get_sm_device(); |
| 45 | if (IS_ERR(dev)) |
| 46 | return PTR_ERR(dev); |
Beniamino Galvani | 38e1a60 | 2016-05-08 08:30:17 +0200 | [diff] [blame] | 47 | |
Beniamino Galvani | 38e1a60 | 2016-05-08 08:30:17 +0200 | [diff] [blame] | 48 | regs.regs[1] = offset; |
| 49 | regs.regs[2] = size; |
| 50 | |
Alexey Romanov | babf17e | 2023-09-21 11:13:41 +0300 | [diff] [blame] | 51 | err = sm_call_read(dev, buffer, size, |
| 52 | MESON_SMC_CMD_EFUSE_READ, ®s); |
| 53 | if (err < 0) |
| 54 | pr_err("Failed to read efuse memory (%d)\n", err); |
Beniamino Galvani | 38e1a60 | 2016-05-08 08:30:17 +0200 | [diff] [blame] | 55 | |
Alexey Romanov | babf17e | 2023-09-21 11:13:41 +0300 | [diff] [blame] | 56 | return err; |
Beniamino Galvani | 38e1a60 | 2016-05-08 08:30:17 +0200 | [diff] [blame] | 57 | } |
Neil Armstrong | 63f475c | 2019-06-12 11:49:06 +0200 | [diff] [blame] | 58 | |
Vyacheslav Bocharov | 14eb82c | 2021-10-05 15:00:03 +0300 | [diff] [blame] | 59 | ssize_t meson_sm_write_efuse(uintptr_t offset, void *buffer, size_t size) |
| 60 | { |
Alexey Romanov | babf17e | 2023-09-21 11:13:41 +0300 | [diff] [blame] | 61 | struct udevice *dev; |
| 62 | struct pt_regs regs = { 0 }; |
| 63 | int err; |
Vyacheslav Bocharov | 14eb82c | 2021-10-05 15:00:03 +0300 | [diff] [blame] | 64 | |
Alexey Romanov | babf17e | 2023-09-21 11:13:41 +0300 | [diff] [blame] | 65 | dev = meson_get_sm_device(); |
| 66 | if (IS_ERR(dev)) |
| 67 | return PTR_ERR(dev); |
Vyacheslav Bocharov | 14eb82c | 2021-10-05 15:00:03 +0300 | [diff] [blame] | 68 | |
Vyacheslav Bocharov | 14eb82c | 2021-10-05 15:00:03 +0300 | [diff] [blame] | 69 | regs.regs[1] = offset; |
| 70 | regs.regs[2] = size; |
| 71 | |
Alexey Romanov | babf17e | 2023-09-21 11:13:41 +0300 | [diff] [blame] | 72 | err = sm_call_write(dev, buffer, size, |
| 73 | MESON_SMC_CMD_EFUSE_WRITE, ®s); |
| 74 | if (err < 0) |
| 75 | pr_err("Failed to write efuse memory (%d)\n", err); |
Vyacheslav Bocharov | 14eb82c | 2021-10-05 15:00:03 +0300 | [diff] [blame] | 76 | |
Alexey Romanov | babf17e | 2023-09-21 11:13:41 +0300 | [diff] [blame] | 77 | return err; |
Vyacheslav Bocharov | 14eb82c | 2021-10-05 15:00:03 +0300 | [diff] [blame] | 78 | } |
| 79 | |
Neil Armstrong | 63f475c | 2019-06-12 11:49:06 +0200 | [diff] [blame] | 80 | #define SM_CHIP_ID_LENGTH 119 |
| 81 | #define SM_CHIP_ID_OFFSET 4 |
| 82 | #define SM_CHIP_ID_SIZE 12 |
| 83 | |
| 84 | int meson_sm_get_serial(void *buffer, size_t size) |
| 85 | { |
Alexey Romanov | babf17e | 2023-09-21 11:13:41 +0300 | [diff] [blame] | 86 | struct udevice *dev; |
| 87 | struct pt_regs regs = { 0 }; |
| 88 | u8 id_buffer[SM_CHIP_ID_LENGTH]; |
| 89 | int err; |
Neil Armstrong | 63f475c | 2019-06-12 11:49:06 +0200 | [diff] [blame] | 90 | |
Alexey Romanov | babf17e | 2023-09-21 11:13:41 +0300 | [diff] [blame] | 91 | dev = meson_get_sm_device(); |
| 92 | if (IS_ERR(dev)) |
| 93 | return PTR_ERR(dev); |
Neil Armstrong | 63f475c | 2019-06-12 11:49:06 +0200 | [diff] [blame] | 94 | |
Alexey Romanov | babf17e | 2023-09-21 11:13:41 +0300 | [diff] [blame] | 95 | err = sm_call_read(dev, id_buffer, SM_CHIP_ID_LENGTH, |
| 96 | MESON_SMC_CMD_CHIP_ID_GET, ®s); |
| 97 | if (err < 0) |
| 98 | pr_err("Failed to read serial number (%d)\n", err); |
Neil Armstrong | 63f475c | 2019-06-12 11:49:06 +0200 | [diff] [blame] | 99 | |
Alexey Romanov | babf17e | 2023-09-21 11:13:41 +0300 | [diff] [blame] | 100 | memcpy(buffer, id_buffer + SM_CHIP_ID_OFFSET, size); |
Neil Armstrong | 63f475c | 2019-06-12 11:49:06 +0200 | [diff] [blame] | 101 | |
| 102 | return 0; |
| 103 | } |
Neil Armstrong | 69800ec | 2019-08-06 17:21:02 +0200 | [diff] [blame] | 104 | |
Neil Armstrong | 385309c | 2019-08-06 17:28:36 +0200 | [diff] [blame] | 105 | #define AO_SEC_SD_CFG15 0xfc |
| 106 | #define REBOOT_REASON_MASK GENMASK(15, 12) |
| 107 | |
| 108 | int meson_sm_get_reboot_reason(void) |
| 109 | { |
| 110 | struct regmap *regmap; |
| 111 | int nodeoffset; |
| 112 | ofnode node; |
| 113 | unsigned int reason; |
| 114 | |
| 115 | /* find the offset of compatible node */ |
| 116 | nodeoffset = fdt_node_offset_by_compatible(gd->fdt_blob, -1, |
| 117 | "amlogic,meson-gx-ao-secure"); |
| 118 | if (nodeoffset < 0) { |
| 119 | printf("%s: failed to get amlogic,meson-gx-ao-secure\n", |
| 120 | __func__); |
| 121 | return -ENODEV; |
| 122 | } |
| 123 | |
| 124 | /* get regmap from the syscon node */ |
| 125 | node = offset_to_ofnode(nodeoffset); |
| 126 | regmap = syscon_node_to_regmap(node); |
| 127 | if (IS_ERR(regmap)) { |
| 128 | printf("%s: failed to get regmap\n", __func__); |
| 129 | return -EINVAL; |
| 130 | } |
| 131 | |
| 132 | regmap_read(regmap, AO_SEC_SD_CFG15, &reason); |
| 133 | |
| 134 | /* The SMC call is not used, we directly use AO_SEC_SD_CFG15 */ |
| 135 | return FIELD_GET(REBOOT_REASON_MASK, reason); |
| 136 | } |
Alexey Romanov | 92404a1 | 2023-05-31 12:31:54 +0300 | [diff] [blame] | 137 | |
| 138 | int meson_sm_pwrdm_set(size_t index, int cmd) |
| 139 | { |
Alexey Romanov | babf17e | 2023-09-21 11:13:41 +0300 | [diff] [blame] | 140 | struct udevice *dev; |
| 141 | struct pt_regs regs = { 0 }; |
| 142 | int err; |
Alexey Romanov | 92404a1 | 2023-05-31 12:31:54 +0300 | [diff] [blame] | 143 | |
Alexey Romanov | babf17e | 2023-09-21 11:13:41 +0300 | [diff] [blame] | 144 | dev = meson_get_sm_device(); |
| 145 | if (IS_ERR(dev)) |
| 146 | return PTR_ERR(dev); |
| 147 | |
Alexey Romanov | 92404a1 | 2023-05-31 12:31:54 +0300 | [diff] [blame] | 148 | regs.regs[1] = index; |
| 149 | regs.regs[2] = cmd; |
| 150 | |
Alexey Romanov | babf17e | 2023-09-21 11:13:41 +0300 | [diff] [blame] | 151 | err = sm_call(dev, MESON_SMC_CMD_PWRDM_SET, NULL, ®s); |
| 152 | if (err) |
| 153 | pr_err("Failed to %s power domain ind=%zu (%d)\n", cmd == PWRDM_ON ? |
| 154 | "enable" : "disable", index, err); |
Alexey Romanov | 92404a1 | 2023-05-31 12:31:54 +0300 | [diff] [blame] | 155 | |
Alexey Romanov | babf17e | 2023-09-21 11:13:41 +0300 | [diff] [blame] | 156 | return err; |
Alexey Romanov | 92404a1 | 2023-05-31 12:31:54 +0300 | [diff] [blame] | 157 | } |