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