2 * Amlogic Secure Monitor driver
4 * Copyright (C) 2016 Endless Mobile, Inc.
5 * Author: Carlo Caione <carlo@endlessm.com>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * version 2 as published by the Free Software Foundation.
11 * You should have received a copy of the GNU General Public License
12 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 #define pr_fmt(fmt) "meson-sm: " fmt
17 #include <linux/arm-smccc.h>
18 #include <linux/bug.h>
21 #include <linux/of_device.h>
22 #include <linux/printk.h>
23 #include <linux/types.h>
24 #include <linux/sizes.h>
26 #include <linux/firmware/meson/meson_sm.h>
32 #define CMD(d, s) { .index = (d), .smc_id = (s), }
34 struct meson_sm_chip
{
35 unsigned int shmem_size
;
36 u32 cmd_shmem_in_base
;
37 u32 cmd_shmem_out_base
;
38 struct meson_sm_cmd cmd
[];
41 struct meson_sm_chip gxbb_chip
= {
43 .cmd_shmem_in_base
= 0x82000020,
44 .cmd_shmem_out_base
= 0x82000021,
46 CMD(SM_EFUSE_READ
, 0x82000030),
47 CMD(SM_EFUSE_WRITE
, 0x82000031),
48 CMD(SM_EFUSE_USER_MAX
, 0x82000033),
53 struct meson_sm_firmware
{
54 const struct meson_sm_chip
*chip
;
55 void __iomem
*sm_shmem_in_base
;
56 void __iomem
*sm_shmem_out_base
;
59 static struct meson_sm_firmware fw
;
61 static u32
meson_sm_get_cmd(const struct meson_sm_chip
*chip
,
62 unsigned int cmd_index
)
64 const struct meson_sm_cmd
*cmd
= chip
->cmd
;
66 while (cmd
->smc_id
&& cmd
->index
!= cmd_index
)
72 static u32
__meson_sm_call(u32 cmd
, u32 arg0
, u32 arg1
, u32 arg2
,
75 struct arm_smccc_res res
;
77 arm_smccc_smc(cmd
, arg0
, arg1
, arg2
, arg3
, arg4
, 0, 0, &res
);
81 static void __iomem
*meson_sm_map_shmem(u32 cmd_shmem
, unsigned int size
)
85 sm_phy_base
= __meson_sm_call(cmd_shmem
, 0, 0, 0, 0, 0);
89 return ioremap_cache(sm_phy_base
, size
);
93 * meson_sm_call - generic SMC32 call to the secure-monitor
95 * @cmd_index: Index of the SMC32 function ID
96 * @ret: Returned value
97 * @arg0: SMC32 Argument 0
98 * @arg1: SMC32 Argument 1
99 * @arg2: SMC32 Argument 2
100 * @arg3: SMC32 Argument 3
101 * @arg4: SMC32 Argument 4
103 * Return: 0 on success, a negative value on error
105 int meson_sm_call(unsigned int cmd_index
, u32
*ret
, u32 arg0
,
106 u32 arg1
, u32 arg2
, u32 arg3
, u32 arg4
)
113 cmd
= meson_sm_get_cmd(fw
.chip
, cmd_index
);
117 lret
= __meson_sm_call(cmd
, arg0
, arg1
, arg2
, arg3
, arg4
);
124 EXPORT_SYMBOL(meson_sm_call
);
127 * meson_sm_call_read - retrieve data from secure-monitor
129 * @buffer: Buffer to store the retrieved data
130 * @cmd_index: Index of the SMC32 function ID
131 * @arg0: SMC32 Argument 0
132 * @arg1: SMC32 Argument 1
133 * @arg2: SMC32 Argument 2
134 * @arg3: SMC32 Argument 3
135 * @arg4: SMC32 Argument 4
137 * Return: size of read data on success, a negative value on error
139 int meson_sm_call_read(void *buffer
, unsigned int cmd_index
, u32 arg0
,
140 u32 arg1
, u32 arg2
, u32 arg3
, u32 arg4
)
147 if (!fw
.chip
->cmd_shmem_out_base
)
150 if (meson_sm_call(cmd_index
, &size
, arg0
, arg1
, arg2
, arg3
, arg4
) < 0)
153 if (!size
|| size
> fw
.chip
->shmem_size
)
157 memcpy(buffer
, fw
.sm_shmem_out_base
, size
);
161 EXPORT_SYMBOL(meson_sm_call_read
);
164 * meson_sm_call_write - send data to secure-monitor
166 * @buffer: Buffer containing data to send
167 * @size: Size of the data to send
168 * @cmd_index: Index of the SMC32 function ID
169 * @arg0: SMC32 Argument 0
170 * @arg1: SMC32 Argument 1
171 * @arg2: SMC32 Argument 2
172 * @arg3: SMC32 Argument 3
173 * @arg4: SMC32 Argument 4
175 * Return: size of sent data on success, a negative value on error
177 int meson_sm_call_write(void *buffer
, unsigned int size
, unsigned int cmd_index
,
178 u32 arg0
, u32 arg1
, u32 arg2
, u32 arg3
, u32 arg4
)
185 if (size
> fw
.chip
->shmem_size
)
188 if (!fw
.chip
->cmd_shmem_in_base
)
191 memcpy(fw
.sm_shmem_in_base
, buffer
, size
);
193 if (meson_sm_call(cmd_index
, &written
, arg0
, arg1
, arg2
, arg3
, arg4
) < 0)
201 EXPORT_SYMBOL(meson_sm_call_write
);
203 static const struct of_device_id meson_sm_ids
[] = {
204 { .compatible
= "amlogic,meson-gxbb-sm", .data
= &gxbb_chip
},
208 int __init
meson_sm_init(void)
210 const struct meson_sm_chip
*chip
;
211 const struct of_device_id
*matched_np
;
212 struct device_node
*np
;
214 np
= of_find_matching_node_and_match(NULL
, meson_sm_ids
, &matched_np
);
218 chip
= matched_np
->data
;
220 pr_err("unable to setup secure-monitor data\n");
224 if (chip
->cmd_shmem_in_base
) {
225 fw
.sm_shmem_in_base
= meson_sm_map_shmem(chip
->cmd_shmem_in_base
,
227 if (WARN_ON(!fw
.sm_shmem_in_base
))
231 if (chip
->cmd_shmem_out_base
) {
232 fw
.sm_shmem_out_base
= meson_sm_map_shmem(chip
->cmd_shmem_out_base
,
234 if (WARN_ON(!fw
.sm_shmem_out_base
))
239 pr_info("secure-monitor enabled\n");
244 iounmap(fw
.sm_shmem_in_base
);
248 device_initcall(meson_sm_init
);