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 |
Beniamino Galvani | 38e1a60 | 2016-05-08 08:30:17 +0200 | [diff] [blame] | 27 | |
| 28 | static void *shmem_input; |
| 29 | static void *shmem_output; |
| 30 | |
| 31 | static void meson_init_shmem(void) |
| 32 | { |
| 33 | struct pt_regs regs; |
| 34 | |
| 35 | if (shmem_input && shmem_output) |
| 36 | return; |
| 37 | |
| 38 | regs.regs[0] = FN_GET_SHARE_MEM_INPUT_BASE; |
| 39 | smc_call(®s); |
| 40 | shmem_input = (void *)regs.regs[0]; |
| 41 | |
| 42 | regs.regs[0] = FN_GET_SHARE_MEM_OUTPUT_BASE; |
| 43 | smc_call(®s); |
| 44 | shmem_output = (void *)regs.regs[0]; |
| 45 | |
| 46 | debug("Secure Monitor shmem: 0x%p 0x%p\n", shmem_input, shmem_output); |
| 47 | } |
| 48 | |
| 49 | ssize_t meson_sm_read_efuse(uintptr_t offset, void *buffer, size_t size) |
| 50 | { |
| 51 | struct pt_regs regs; |
| 52 | |
| 53 | meson_init_shmem(); |
| 54 | |
| 55 | regs.regs[0] = FN_EFUSE_READ; |
| 56 | regs.regs[1] = offset; |
| 57 | regs.regs[2] = size; |
| 58 | |
| 59 | smc_call(®s); |
| 60 | |
| 61 | if (regs.regs[0] == 0) |
| 62 | return -1; |
| 63 | |
| 64 | memcpy(buffer, shmem_output, min(size, regs.regs[0])); |
| 65 | |
| 66 | return regs.regs[0]; |
| 67 | } |
Neil Armstrong | 63f475c | 2019-06-12 11:49:06 +0200 | [diff] [blame] | 68 | |
Vyacheslav Bocharov | 14eb82c | 2021-10-05 15:00:03 +0300 | [diff] [blame] | 69 | ssize_t meson_sm_write_efuse(uintptr_t offset, void *buffer, size_t size) |
| 70 | { |
| 71 | struct pt_regs regs; |
| 72 | |
| 73 | meson_init_shmem(); |
| 74 | |
| 75 | memcpy(shmem_input, buffer, size); |
| 76 | |
| 77 | regs.regs[0] = FN_EFUSE_WRITE; |
| 78 | regs.regs[1] = offset; |
| 79 | regs.regs[2] = size; |
| 80 | |
| 81 | smc_call(®s); |
| 82 | |
Jerome Brunet | 25759f1 | 2022-08-04 16:41:38 +0200 | [diff] [blame] | 83 | return regs.regs[0]; |
Vyacheslav Bocharov | 14eb82c | 2021-10-05 15:00:03 +0300 | [diff] [blame] | 84 | } |
| 85 | |
Neil Armstrong | 63f475c | 2019-06-12 11:49:06 +0200 | [diff] [blame] | 86 | #define SM_CHIP_ID_LENGTH 119 |
| 87 | #define SM_CHIP_ID_OFFSET 4 |
| 88 | #define SM_CHIP_ID_SIZE 12 |
| 89 | |
| 90 | int meson_sm_get_serial(void *buffer, size_t size) |
| 91 | { |
| 92 | struct pt_regs regs; |
| 93 | |
| 94 | meson_init_shmem(); |
| 95 | |
| 96 | regs.regs[0] = FN_CHIP_ID; |
| 97 | regs.regs[1] = 0; |
| 98 | regs.regs[2] = 0; |
| 99 | |
| 100 | smc_call(®s); |
| 101 | |
| 102 | memcpy(buffer, shmem_output + SM_CHIP_ID_OFFSET, |
| 103 | min_t(size_t, size, SM_CHIP_ID_SIZE)); |
| 104 | |
| 105 | return 0; |
| 106 | } |
Neil Armstrong | 69800ec | 2019-08-06 17:21:02 +0200 | [diff] [blame] | 107 | |
Neil Armstrong | 385309c | 2019-08-06 17:28:36 +0200 | [diff] [blame] | 108 | #define AO_SEC_SD_CFG15 0xfc |
| 109 | #define REBOOT_REASON_MASK GENMASK(15, 12) |
| 110 | |
| 111 | int meson_sm_get_reboot_reason(void) |
| 112 | { |
| 113 | struct regmap *regmap; |
| 114 | int nodeoffset; |
| 115 | ofnode node; |
| 116 | unsigned int reason; |
| 117 | |
| 118 | /* find the offset of compatible node */ |
| 119 | nodeoffset = fdt_node_offset_by_compatible(gd->fdt_blob, -1, |
| 120 | "amlogic,meson-gx-ao-secure"); |
| 121 | if (nodeoffset < 0) { |
| 122 | printf("%s: failed to get amlogic,meson-gx-ao-secure\n", |
| 123 | __func__); |
| 124 | return -ENODEV; |
| 125 | } |
| 126 | |
| 127 | /* get regmap from the syscon node */ |
| 128 | node = offset_to_ofnode(nodeoffset); |
| 129 | regmap = syscon_node_to_regmap(node); |
| 130 | if (IS_ERR(regmap)) { |
| 131 | printf("%s: failed to get regmap\n", __func__); |
| 132 | return -EINVAL; |
| 133 | } |
| 134 | |
| 135 | regmap_read(regmap, AO_SEC_SD_CFG15, &reason); |
| 136 | |
| 137 | /* The SMC call is not used, we directly use AO_SEC_SD_CFG15 */ |
| 138 | return FIELD_GET(REBOOT_REASON_MASK, reason); |
| 139 | } |