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> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Neil Armstrong | 63f475c | 2019-06-12 11:49:06 +0200 | [diff] [blame] | 10 | #include <asm/arch/sm.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 11 | #include <asm/cache.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 12 | #include <asm/global_data.h> |
Simon Glass | 6b9f010 | 2020-05-10 11:40:06 -0600 | [diff] [blame] | 13 | #include <asm/ptrace.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 14 | #include <linux/bitops.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 15 | #include <linux/err.h> |
Beniamino Galvani | 38e1a60 | 2016-05-08 08:30:17 +0200 | [diff] [blame] | 16 | #include <linux/kernel.h> |
Neil Armstrong | 385309c | 2019-08-06 17:28:36 +0200 | [diff] [blame] | 17 | #include <dm.h> |
| 18 | #include <linux/bitfield.h> |
| 19 | #include <regmap.h> |
| 20 | #include <syscon.h> |
Beniamino Galvani | 38e1a60 | 2016-05-08 08:30:17 +0200 | [diff] [blame] | 21 | |
| 22 | #define FN_GET_SHARE_MEM_INPUT_BASE 0x82000020 |
| 23 | #define FN_GET_SHARE_MEM_OUTPUT_BASE 0x82000021 |
| 24 | #define FN_EFUSE_READ 0x82000030 |
| 25 | #define FN_EFUSE_WRITE 0x82000031 |
Neil Armstrong | 63f475c | 2019-06-12 11:49:06 +0200 | [diff] [blame] | 26 | #define FN_CHIP_ID 0x82000044 |
Alexey Romanov | 92404a1 | 2023-05-31 12:31:54 +0300 | [diff] [blame] | 27 | #define FN_PWRDM_SET 0x82000093 |
Beniamino Galvani | 38e1a60 | 2016-05-08 08:30:17 +0200 | [diff] [blame] | 28 | |
| 29 | static void *shmem_input; |
| 30 | static void *shmem_output; |
| 31 | |
| 32 | static 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(®s); |
| 41 | shmem_input = (void *)regs.regs[0]; |
| 42 | |
| 43 | regs.regs[0] = FN_GET_SHARE_MEM_OUTPUT_BASE; |
| 44 | smc_call(®s); |
| 45 | shmem_output = (void *)regs.regs[0]; |
| 46 | |
| 47 | debug("Secure Monitor shmem: 0x%p 0x%p\n", shmem_input, shmem_output); |
| 48 | } |
| 49 | |
| 50 | ssize_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(®s); |
| 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 Armstrong | 63f475c | 2019-06-12 11:49:06 +0200 | [diff] [blame] | 69 | |
Vyacheslav Bocharov | 14eb82c | 2021-10-05 15:00:03 +0300 | [diff] [blame] | 70 | ssize_t meson_sm_write_efuse(uintptr_t offset, void *buffer, size_t size) |
| 71 | { |
| 72 | struct pt_regs regs; |
| 73 | |
| 74 | meson_init_shmem(); |
| 75 | |
| 76 | memcpy(shmem_input, buffer, size); |
| 77 | |
| 78 | regs.regs[0] = FN_EFUSE_WRITE; |
| 79 | regs.regs[1] = offset; |
| 80 | regs.regs[2] = size; |
| 81 | |
| 82 | smc_call(®s); |
| 83 | |
Jerome Brunet | 25759f1 | 2022-08-04 16:41:38 +0200 | [diff] [blame] | 84 | return regs.regs[0]; |
Vyacheslav Bocharov | 14eb82c | 2021-10-05 15:00:03 +0300 | [diff] [blame] | 85 | } |
| 86 | |
Neil Armstrong | 63f475c | 2019-06-12 11:49:06 +0200 | [diff] [blame] | 87 | #define SM_CHIP_ID_LENGTH 119 |
| 88 | #define SM_CHIP_ID_OFFSET 4 |
| 89 | #define SM_CHIP_ID_SIZE 12 |
| 90 | |
| 91 | int meson_sm_get_serial(void *buffer, size_t size) |
| 92 | { |
| 93 | struct pt_regs regs; |
| 94 | |
| 95 | meson_init_shmem(); |
| 96 | |
| 97 | regs.regs[0] = FN_CHIP_ID; |
| 98 | regs.regs[1] = 0; |
| 99 | regs.regs[2] = 0; |
| 100 | |
| 101 | smc_call(®s); |
| 102 | |
| 103 | memcpy(buffer, shmem_output + SM_CHIP_ID_OFFSET, |
| 104 | min_t(size_t, size, SM_CHIP_ID_SIZE)); |
| 105 | |
| 106 | return 0; |
| 107 | } |
Neil Armstrong | 69800ec | 2019-08-06 17:21:02 +0200 | [diff] [blame] | 108 | |
Neil Armstrong | 385309c | 2019-08-06 17:28:36 +0200 | [diff] [blame] | 109 | #define AO_SEC_SD_CFG15 0xfc |
| 110 | #define REBOOT_REASON_MASK GENMASK(15, 12) |
| 111 | |
| 112 | int meson_sm_get_reboot_reason(void) |
| 113 | { |
| 114 | struct regmap *regmap; |
| 115 | int nodeoffset; |
| 116 | ofnode node; |
| 117 | unsigned int reason; |
| 118 | |
| 119 | /* find the offset of compatible node */ |
| 120 | nodeoffset = fdt_node_offset_by_compatible(gd->fdt_blob, -1, |
| 121 | "amlogic,meson-gx-ao-secure"); |
| 122 | if (nodeoffset < 0) { |
| 123 | printf("%s: failed to get amlogic,meson-gx-ao-secure\n", |
| 124 | __func__); |
| 125 | return -ENODEV; |
| 126 | } |
| 127 | |
| 128 | /* get regmap from the syscon node */ |
| 129 | node = offset_to_ofnode(nodeoffset); |
| 130 | regmap = syscon_node_to_regmap(node); |
| 131 | if (IS_ERR(regmap)) { |
| 132 | printf("%s: failed to get regmap\n", __func__); |
| 133 | return -EINVAL; |
| 134 | } |
| 135 | |
| 136 | regmap_read(regmap, AO_SEC_SD_CFG15, &reason); |
| 137 | |
| 138 | /* The SMC call is not used, we directly use AO_SEC_SD_CFG15 */ |
| 139 | return FIELD_GET(REBOOT_REASON_MASK, reason); |
| 140 | } |
Alexey Romanov | 92404a1 | 2023-05-31 12:31:54 +0300 | [diff] [blame] | 141 | |
| 142 | int meson_sm_pwrdm_set(size_t index, int cmd) |
| 143 | { |
| 144 | struct pt_regs regs; |
| 145 | |
| 146 | regs.regs[0] = FN_PWRDM_SET; |
| 147 | regs.regs[1] = index; |
| 148 | regs.regs[2] = cmd; |
| 149 | |
| 150 | smc_call(®s); |
| 151 | |
| 152 | return regs.regs[0]; |
| 153 | } |