arm: dts: socfpga: cyclone5: Update i2c-scl-falling-time-ns
[oweals/u-boot.git] / arch / arm / mach-meson / sm.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2016 Beniamino Galvani <b.galvani@gmail.com>
4  *
5  * Secure monitor calls.
6  */
7
8 #include <common.h>
9 #include <command.h>
10 #include <env.h>
11 #include <log.h>
12 #include <asm/arch/sm.h>
13 #include <asm/cache.h>
14 #include <asm/ptrace.h>
15 #include <linux/bitops.h>
16 #include <linux/err.h>
17 #include <linux/kernel.h>
18 #include <dm.h>
19 #include <linux/bitfield.h>
20 #include <regmap.h>
21 #include <syscon.h>
22
23 #define FN_GET_SHARE_MEM_INPUT_BASE     0x82000020
24 #define FN_GET_SHARE_MEM_OUTPUT_BASE    0x82000021
25 #define FN_EFUSE_READ                   0x82000030
26 #define FN_EFUSE_WRITE                  0x82000031
27 #define FN_CHIP_ID                      0x82000044
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(&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
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(&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 }
69
70 #define SM_CHIP_ID_LENGTH       119
71 #define SM_CHIP_ID_OFFSET       4
72 #define SM_CHIP_ID_SIZE         12
73
74 int meson_sm_get_serial(void *buffer, size_t size)
75 {
76         struct pt_regs regs;
77
78         meson_init_shmem();
79
80         regs.regs[0] = FN_CHIP_ID;
81         regs.regs[1] = 0;
82         regs.regs[2] = 0;
83
84         smc_call(&regs);
85
86         memcpy(buffer, shmem_output + SM_CHIP_ID_OFFSET,
87                min_t(size_t, size, SM_CHIP_ID_SIZE));
88
89         return 0;
90 }
91
92 #define AO_SEC_SD_CFG15         0xfc
93 #define REBOOT_REASON_MASK      GENMASK(15, 12)
94
95 int meson_sm_get_reboot_reason(void)
96 {
97         struct regmap *regmap;
98         int nodeoffset;
99         ofnode node;
100         unsigned int reason;
101
102         /* find the offset of compatible node */
103         nodeoffset = fdt_node_offset_by_compatible(gd->fdt_blob, -1,
104                                                    "amlogic,meson-gx-ao-secure");
105         if (nodeoffset < 0) {
106                 printf("%s: failed to get amlogic,meson-gx-ao-secure\n",
107                        __func__);
108                 return -ENODEV;
109         }
110
111         /* get regmap from the syscon node */
112         node = offset_to_ofnode(nodeoffset);
113         regmap = syscon_node_to_regmap(node);
114         if (IS_ERR(regmap)) {
115                 printf("%s: failed to get regmap\n", __func__);
116                 return -EINVAL;
117         }
118
119         regmap_read(regmap, AO_SEC_SD_CFG15, &reason);
120
121         /* The SMC call is not used, we directly use AO_SEC_SD_CFG15 */
122         return FIELD_GET(REBOOT_REASON_MASK, reason);
123 }
124
125 static int do_sm_serial(struct cmd_tbl *cmdtp, int flag, int argc,
126                         char *const argv[])
127 {
128         ulong address;
129         int ret;
130
131         if (argc < 2)
132                 return CMD_RET_USAGE;
133
134         address = simple_strtoul(argv[1], NULL, 0);
135
136         ret = meson_sm_get_serial((void *)address, SM_CHIP_ID_SIZE);
137         if (ret)
138                 return CMD_RET_FAILURE;
139
140         return CMD_RET_SUCCESS;
141 }
142
143 #define MAX_REBOOT_REASONS 14
144
145 static const char *reboot_reasons[MAX_REBOOT_REASONS] = {
146         [REBOOT_REASON_COLD] = "cold_boot",
147         [REBOOT_REASON_NORMAL] = "normal",
148         [REBOOT_REASON_RECOVERY] = "recovery",
149         [REBOOT_REASON_UPDATE] = "update",
150         [REBOOT_REASON_FASTBOOT] = "fastboot",
151         [REBOOT_REASON_SUSPEND_OFF] = "suspend_off",
152         [REBOOT_REASON_HIBERNATE] = "hibernate",
153         [REBOOT_REASON_BOOTLOADER] = "bootloader",
154         [REBOOT_REASON_SHUTDOWN_REBOOT] = "shutdown_reboot",
155         [REBOOT_REASON_RPMBP] = "rpmbp",
156         [REBOOT_REASON_CRASH_DUMP] = "crash_dump",
157         [REBOOT_REASON_KERNEL_PANIC] = "kernel_panic",
158         [REBOOT_REASON_WATCHDOG_REBOOT] = "watchdog_reboot",
159 };
160
161 static int do_sm_reboot_reason(struct cmd_tbl *cmdtp, int flag, int argc,
162                                char *const argv[])
163 {
164         const char *reason_str;
165         char *destarg = NULL;
166         int reason;
167
168         if (argc > 1)
169                 destarg = argv[1];
170
171         reason = meson_sm_get_reboot_reason();
172         if (reason < 0)
173                 return CMD_RET_FAILURE;
174
175         if (reason >= MAX_REBOOT_REASONS ||
176             !reboot_reasons[reason])
177                 reason_str = "unknown";
178         else
179                 reason_str = reboot_reasons[reason];
180
181         if (destarg)
182                 env_set(destarg, reason_str);
183         else
184                 printf("reboot reason: %s (%x)\n", reason_str, reason);
185
186         return CMD_RET_SUCCESS;
187 }
188
189 static struct cmd_tbl cmd_sm_sub[] = {
190         U_BOOT_CMD_MKENT(serial, 2, 1, do_sm_serial, "", ""),
191         U_BOOT_CMD_MKENT(reboot_reason, 1, 1, do_sm_reboot_reason, "", ""),
192 };
193
194 static int do_sm(struct cmd_tbl *cmdtp, int flag, int argc,
195                  char *const argv[])
196 {
197         struct cmd_tbl *c;
198
199         if (argc < 2)
200                 return CMD_RET_USAGE;
201
202         /* Strip off leading 'sm' command argument */
203         argc--;
204         argv++;
205
206         c = find_cmd_tbl(argv[0], &cmd_sm_sub[0], ARRAY_SIZE(cmd_sm_sub));
207
208         if (c)
209                 return c->cmd(cmdtp, flag, argc, argv);
210         else
211                 return CMD_RET_USAGE;
212 }
213
214 U_BOOT_CMD(
215         sm, 5, 0, do_sm,
216         "Secure Monitor Control",
217         "serial <address> - read chip unique id to memory address\n"
218         "sm reboot_reason [name] - get reboot reason and store to to environment"
219 );