blob: f2ca7e7693217f6df4f2a25951d17e00cfa620b9 [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
Beniamino Galvani38e1a602016-05-08 08:30:17 +020027
28static void *shmem_input;
29static void *shmem_output;
30
31static 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(&regs);
40 shmem_input = (void *)regs.regs[0];
41
42 regs.regs[0] = FN_GET_SHARE_MEM_OUTPUT_BASE;
43 smc_call(&regs);
44 shmem_output = (void *)regs.regs[0];
45
46 debug("Secure Monitor shmem: 0x%p 0x%p\n", shmem_input, shmem_output);
47}
48
49ssize_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(&regs);
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 Armstrong63f475c2019-06-12 11:49:06 +020068
Vyacheslav Bocharov14eb82c2021-10-05 15:00:03 +030069ssize_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(&regs);
82
Jerome Brunet25759f12022-08-04 16:41:38 +020083 return regs.regs[0];
Vyacheslav Bocharov14eb82c2021-10-05 15:00:03 +030084}
85
Neil Armstrong63f475c2019-06-12 11:49:06 +020086#define SM_CHIP_ID_LENGTH 119
87#define SM_CHIP_ID_OFFSET 4
88#define SM_CHIP_ID_SIZE 12
89
90int 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(&regs);
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 Armstrong69800ec2019-08-06 17:21:02 +0200107
Neil Armstrong385309c2019-08-06 17:28:36 +0200108#define AO_SEC_SD_CFG15 0xfc
109#define REBOOT_REASON_MASK GENMASK(15, 12)
110
111int 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}