blob: d600c64d0bead8c38892e16712f4b76fbe451854 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Beniamino Galvani38e1a602016-05-08 08:30:17 +02002/*
3 * (C) Copyright 2016 Beniamino Galvani <b.galvani@gmail.com>
4 *
Beniamino Galvani38e1a602016-05-08 08:30:17 +02005 * Secure monitor calls.
6 */
7
8#include <common.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Neil Armstrong63f475c2019-06-12 11:49:06 +020010#include <asm/arch/sm.h>
Simon Glass274e0b02020-05-10 11:39:56 -060011#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060012#include <asm/global_data.h>
Simon Glass6b9f0102020-05-10 11:40:06 -060013#include <asm/ptrace.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060014#include <linux/bitops.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070015#include <linux/err.h>
Beniamino Galvani38e1a602016-05-08 08:30:17 +020016#include <linux/kernel.h>
Neil Armstrong385309c2019-08-06 17:28:36 +020017#include <dm.h>
18#include <linux/bitfield.h>
19#include <regmap.h>
20#include <syscon.h>
Beniamino Galvani38e1a602016-05-08 08:30:17 +020021
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 Armstrong63f475c2019-06-12 11:49:06 +020026#define FN_CHIP_ID 0x82000044
Alexey Romanov92404a12023-05-31 12:31:54 +030027#define FN_PWRDM_SET 0x82000093
Beniamino Galvani38e1a602016-05-08 08:30:17 +020028
29static void *shmem_input;
30static void *shmem_output;
31
32static 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(&regs);
41 shmem_input = (void *)regs.regs[0];
42
43 regs.regs[0] = FN_GET_SHARE_MEM_OUTPUT_BASE;
44 smc_call(&regs);
45 shmem_output = (void *)regs.regs[0];
46
47 debug("Secure Monitor shmem: 0x%p 0x%p\n", shmem_input, shmem_output);
48}
49
50ssize_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(&regs);
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 Armstrong63f475c2019-06-12 11:49:06 +020069
Vyacheslav Bocharov14eb82c2021-10-05 15:00:03 +030070ssize_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(&regs);
83
Jerome Brunet25759f12022-08-04 16:41:38 +020084 return regs.regs[0];
Vyacheslav Bocharov14eb82c2021-10-05 15:00:03 +030085}
86
Neil Armstrong63f475c2019-06-12 11:49:06 +020087#define SM_CHIP_ID_LENGTH 119
88#define SM_CHIP_ID_OFFSET 4
89#define SM_CHIP_ID_SIZE 12
90
91int 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(&regs);
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 Armstrong69800ec2019-08-06 17:21:02 +0200108
Neil Armstrong385309c2019-08-06 17:28:36 +0200109#define AO_SEC_SD_CFG15 0xfc
110#define REBOOT_REASON_MASK GENMASK(15, 12)
111
112int 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 Romanov92404a12023-05-31 12:31:54 +0300141
142int 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(&regs);
151
152 return regs.regs[0];
153}