1 // SPDX-License-Identifier: GPL-2.0-only
3 * Amlogic Secure Monitor driver
5 * Copyright (C) 2016 Endless Mobile, Inc.
6 * Author: Carlo Caione <carlo@endlessm.com>
9 #define pr_fmt(fmt) "meson-sm: " fmt
11 #include <linux/arm-smccc.h>
12 #include <linux/bug.h>
14 #include <linux/module.h>
16 #include <linux/of_device.h>
17 #include <linux/platform_device.h>
18 #include <linux/printk.h>
19 #include <linux/types.h>
20 #include <linux/sizes.h>
21 #include <linux/slab.h>
23 #include <linux/firmware/meson/meson_sm.h>
29 #define CMD(d, s) { .index = (d), .smc_id = (s), }
31 struct meson_sm_chip
{
32 unsigned int shmem_size
;
33 u32 cmd_shmem_in_base
;
34 u32 cmd_shmem_out_base
;
35 struct meson_sm_cmd cmd
[];
38 static const struct meson_sm_chip gxbb_chip
= {
40 .cmd_shmem_in_base
= 0x82000020,
41 .cmd_shmem_out_base
= 0x82000021,
43 CMD(SM_EFUSE_READ
, 0x82000030),
44 CMD(SM_EFUSE_WRITE
, 0x82000031),
45 CMD(SM_EFUSE_USER_MAX
, 0x82000033),
46 CMD(SM_GET_CHIP_ID
, 0x82000044),
47 CMD(SM_A1_PWRC_SET
, 0x82000093),
48 CMD(SM_A1_PWRC_GET
, 0x82000095),
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 u32
meson_sm_get_cmd(const struct meson_sm_chip
*chip
,
60 unsigned int cmd_index
)
62 const struct meson_sm_cmd
*cmd
= chip
->cmd
;
64 while (cmd
->smc_id
&& cmd
->index
!= cmd_index
)
70 static u32
__meson_sm_call(u32 cmd
, u32 arg0
, u32 arg1
, u32 arg2
,
73 struct arm_smccc_res res
;
75 arm_smccc_smc(cmd
, arg0
, arg1
, arg2
, arg3
, arg4
, 0, 0, &res
);
79 static void __iomem
*meson_sm_map_shmem(u32 cmd_shmem
, unsigned int size
)
83 sm_phy_base
= __meson_sm_call(cmd_shmem
, 0, 0, 0, 0, 0);
87 return ioremap_cache(sm_phy_base
, size
);
91 * meson_sm_call - generic SMC32 call to the secure-monitor
93 * @fw: Pointer to secure-monitor firmware
94 * @cmd_index: Index of the SMC32 function ID
95 * @ret: Returned value
96 * @arg0: SMC32 Argument 0
97 * @arg1: SMC32 Argument 1
98 * @arg2: SMC32 Argument 2
99 * @arg3: SMC32 Argument 3
100 * @arg4: SMC32 Argument 4
102 * Return: 0 on success, a negative value on error
104 int meson_sm_call(struct meson_sm_firmware
*fw
, unsigned int cmd_index
,
105 u32
*ret
, u32 arg0
, u32 arg1
, u32 arg2
, u32 arg3
, u32 arg4
)
112 cmd
= meson_sm_get_cmd(fw
->chip
, cmd_index
);
116 lret
= __meson_sm_call(cmd
, arg0
, arg1
, arg2
, arg3
, arg4
);
123 EXPORT_SYMBOL(meson_sm_call
);
126 * meson_sm_call_read - retrieve data from secure-monitor
128 * @fw: Pointer to secure-monitor firmware
129 * @buffer: Buffer to store the retrieved data
130 * @bsize: Size of the buffer
131 * @cmd_index: Index of the SMC32 function ID
132 * @arg0: SMC32 Argument 0
133 * @arg1: SMC32 Argument 1
134 * @arg2: SMC32 Argument 2
135 * @arg3: SMC32 Argument 3
136 * @arg4: SMC32 Argument 4
138 * Return: size of read data on success, a negative value on error
139 * When 0 is returned there is no guarantee about the amount of
140 * data read and bsize bytes are copied in buffer.
142 int meson_sm_call_read(struct meson_sm_firmware
*fw
, void *buffer
,
143 unsigned int bsize
, unsigned int cmd_index
, u32 arg0
,
144 u32 arg1
, u32 arg2
, u32 arg3
, u32 arg4
)
152 if (!fw
->chip
->cmd_shmem_out_base
)
155 if (bsize
> fw
->chip
->shmem_size
)
158 if (meson_sm_call(fw
, cmd_index
, &size
, arg0
, arg1
, arg2
, arg3
, arg4
) < 0)
170 memcpy(buffer
, fw
->sm_shmem_out_base
, size
);
174 EXPORT_SYMBOL(meson_sm_call_read
);
177 * meson_sm_call_write - send data to secure-monitor
179 * @fw: Pointer to secure-monitor firmware
180 * @buffer: Buffer containing data to send
181 * @size: Size of the data to send
182 * @cmd_index: Index of the SMC32 function ID
183 * @arg0: SMC32 Argument 0
184 * @arg1: SMC32 Argument 1
185 * @arg2: SMC32 Argument 2
186 * @arg3: SMC32 Argument 3
187 * @arg4: SMC32 Argument 4
189 * Return: size of sent data on success, a negative value on error
191 int meson_sm_call_write(struct meson_sm_firmware
*fw
, void *buffer
,
192 unsigned int size
, unsigned int cmd_index
, u32 arg0
,
193 u32 arg1
, u32 arg2
, u32 arg3
, u32 arg4
)
200 if (size
> fw
->chip
->shmem_size
)
203 if (!fw
->chip
->cmd_shmem_in_base
)
206 memcpy(fw
->sm_shmem_in_base
, buffer
, size
);
208 if (meson_sm_call(fw
, cmd_index
, &written
, arg0
, arg1
, arg2
, arg3
, arg4
) < 0)
216 EXPORT_SYMBOL(meson_sm_call_write
);
219 * meson_sm_get - get pointer to meson_sm_firmware structure.
221 * @sm_node: Pointer to the secure-monitor Device Tree node.
223 * Return: NULL is the secure-monitor device is not ready.
225 struct meson_sm_firmware
*meson_sm_get(struct device_node
*sm_node
)
227 struct platform_device
*pdev
= of_find_device_by_node(sm_node
);
232 return platform_get_drvdata(pdev
);
234 EXPORT_SYMBOL_GPL(meson_sm_get
);
236 #define SM_CHIP_ID_LENGTH 119
237 #define SM_CHIP_ID_OFFSET 4
238 #define SM_CHIP_ID_SIZE 12
240 static ssize_t
serial_show(struct device
*dev
, struct device_attribute
*attr
,
243 struct platform_device
*pdev
= to_platform_device(dev
);
244 struct meson_sm_firmware
*fw
;
248 fw
= platform_get_drvdata(pdev
);
250 id_buf
= kmalloc(SM_CHIP_ID_LENGTH
, GFP_KERNEL
);
254 ret
= meson_sm_call_read(fw
, id_buf
, SM_CHIP_ID_LENGTH
, SM_GET_CHIP_ID
,
261 ret
= sprintf(buf
, "%12phN\n", &id_buf
[SM_CHIP_ID_OFFSET
]);
268 static DEVICE_ATTR_RO(serial
);
270 static struct attribute
*meson_sm_sysfs_attributes
[] = {
271 &dev_attr_serial
.attr
,
275 static const struct attribute_group meson_sm_sysfs_attr_group
= {
276 .attrs
= meson_sm_sysfs_attributes
,
279 static const struct of_device_id meson_sm_ids
[] = {
280 { .compatible
= "amlogic,meson-gxbb-sm", .data
= &gxbb_chip
},
284 static int __init
meson_sm_probe(struct platform_device
*pdev
)
286 struct device
*dev
= &pdev
->dev
;
287 const struct meson_sm_chip
*chip
;
288 struct meson_sm_firmware
*fw
;
290 fw
= devm_kzalloc(dev
, sizeof(*fw
), GFP_KERNEL
);
294 chip
= of_match_device(meson_sm_ids
, dev
)->data
;
296 if (chip
->cmd_shmem_in_base
) {
297 fw
->sm_shmem_in_base
= meson_sm_map_shmem(chip
->cmd_shmem_in_base
,
299 if (WARN_ON(!fw
->sm_shmem_in_base
))
303 if (chip
->cmd_shmem_out_base
) {
304 fw
->sm_shmem_out_base
= meson_sm_map_shmem(chip
->cmd_shmem_out_base
,
306 if (WARN_ON(!fw
->sm_shmem_out_base
))
312 platform_set_drvdata(pdev
, fw
);
314 pr_info("secure-monitor enabled\n");
316 if (sysfs_create_group(&pdev
->dev
.kobj
, &meson_sm_sysfs_attr_group
))
322 iounmap(fw
->sm_shmem_in_base
);
327 static struct platform_driver meson_sm_driver
= {
330 .of_match_table
= of_match_ptr(meson_sm_ids
),
333 module_platform_driver_probe(meson_sm_driver
, meson_sm_probe
);