1 // SPDX-License-Identifier: GPL-2.0-only
3 * Amlogic Secure Monitor driver
5 * Copyright (C) 2016 Endless Mobile, Inc.
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 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),
51 struct meson_sm_firmware {
52 const struct meson_sm_chip *chip;
53 void __iomem *sm_shmem_in_base;
54 void __iomem *sm_shmem_out_base;
57 static struct meson_sm_firmware fw;
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 * @cmd_index: Index of the SMC32 function ID
94 * @ret: Returned value
95 * @arg0: SMC32 Argument 0
96 * @arg1: SMC32 Argument 1
97 * @arg2: SMC32 Argument 2
98 * @arg3: SMC32 Argument 3
99 * @arg4: SMC32 Argument 4
101 * Return: 0 on success, a negative value on error
103 int meson_sm_call(unsigned int cmd_index, u32 *ret, u32 arg0,
104 u32 arg1, u32 arg2, u32 arg3, u32 arg4)
111 cmd = meson_sm_get_cmd(fw.chip, cmd_index);
115 lret = __meson_sm_call(cmd, arg0, arg1, arg2, arg3, arg4);
122 EXPORT_SYMBOL(meson_sm_call);
125 * meson_sm_call_read - retrieve data from secure-monitor
127 * @buffer: Buffer to store the retrieved data
128 * @bsize: Size of the buffer
129 * @cmd_index: Index of the SMC32 function ID
130 * @arg0: SMC32 Argument 0
131 * @arg1: SMC32 Argument 1
132 * @arg2: SMC32 Argument 2
133 * @arg3: SMC32 Argument 3
134 * @arg4: SMC32 Argument 4
136 * Return: size of read data on success, a negative value on error
137 * When 0 is returned there is no guarantee about the amount of
138 * data read and bsize bytes are copied in buffer.
140 int meson_sm_call_read(void *buffer, unsigned int bsize, unsigned int cmd_index,
141 u32 arg0, u32 arg1, u32 arg2, u32 arg3, u32 arg4)
149 if (!fw.chip->cmd_shmem_out_base)
152 if (bsize > fw.chip->shmem_size)
155 if (meson_sm_call(cmd_index, &size, arg0, arg1, arg2, arg3, arg4) < 0)
167 memcpy(buffer, fw.sm_shmem_out_base, size);
171 EXPORT_SYMBOL(meson_sm_call_read);
174 * meson_sm_call_write - send data to secure-monitor
176 * @buffer: Buffer containing data to send
177 * @size: Size of the data to send
178 * @cmd_index: Index of the SMC32 function ID
179 * @arg0: SMC32 Argument 0
180 * @arg1: SMC32 Argument 1
181 * @arg2: SMC32 Argument 2
182 * @arg3: SMC32 Argument 3
183 * @arg4: SMC32 Argument 4
185 * Return: size of sent data on success, a negative value on error
187 int meson_sm_call_write(void *buffer, unsigned int size, unsigned int cmd_index,
188 u32 arg0, u32 arg1, u32 arg2, u32 arg3, u32 arg4)
195 if (size > fw.chip->shmem_size)
198 if (!fw.chip->cmd_shmem_in_base)
201 memcpy(fw.sm_shmem_in_base, buffer, size);
203 if (meson_sm_call(cmd_index, &written, arg0, arg1, arg2, arg3, arg4) < 0)
211 EXPORT_SYMBOL(meson_sm_call_write);
213 #define SM_CHIP_ID_LENGTH 119
214 #define SM_CHIP_ID_OFFSET 4
215 #define SM_CHIP_ID_SIZE 12
217 static ssize_t serial_show(struct device *dev, struct device_attribute *attr,
223 id_buf = kmalloc(SM_CHIP_ID_LENGTH, GFP_KERNEL);
227 ret = meson_sm_call_read(id_buf, SM_CHIP_ID_LENGTH, SM_GET_CHIP_ID,
234 ret = sprintf(buf, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
235 id_buf[SM_CHIP_ID_OFFSET + 0],
236 id_buf[SM_CHIP_ID_OFFSET + 1],
237 id_buf[SM_CHIP_ID_OFFSET + 2],
238 id_buf[SM_CHIP_ID_OFFSET + 3],
239 id_buf[SM_CHIP_ID_OFFSET + 4],
240 id_buf[SM_CHIP_ID_OFFSET + 5],
241 id_buf[SM_CHIP_ID_OFFSET + 6],
242 id_buf[SM_CHIP_ID_OFFSET + 7],
243 id_buf[SM_CHIP_ID_OFFSET + 8],
244 id_buf[SM_CHIP_ID_OFFSET + 9],
245 id_buf[SM_CHIP_ID_OFFSET + 10],
246 id_buf[SM_CHIP_ID_OFFSET + 11]);
253 static DEVICE_ATTR_RO(serial);
255 static struct attribute *meson_sm_sysfs_attributes[] = {
256 &dev_attr_serial.attr,
260 static const struct attribute_group meson_sm_sysfs_attr_group = {
261 .attrs = meson_sm_sysfs_attributes,
264 static const struct of_device_id meson_sm_ids[] = {
265 { .compatible = "amlogic,meson-gxbb-sm", .data = &gxbb_chip },
269 static int __init meson_sm_probe(struct platform_device *pdev)
271 const struct meson_sm_chip *chip;
273 chip = of_match_device(meson_sm_ids, &pdev->dev)->data;
275 if (chip->cmd_shmem_in_base) {
276 fw.sm_shmem_in_base = meson_sm_map_shmem(chip->cmd_shmem_in_base,
278 if (WARN_ON(!fw.sm_shmem_in_base))
282 if (chip->cmd_shmem_out_base) {
283 fw.sm_shmem_out_base = meson_sm_map_shmem(chip->cmd_shmem_out_base,
285 if (WARN_ON(!fw.sm_shmem_out_base))
290 pr_info("secure-monitor enabled\n");
292 if (sysfs_create_group(&pdev->dev.kobj, &meson_sm_sysfs_attr_group))
298 iounmap(fw.sm_shmem_in_base);
303 static struct platform_driver meson_sm_driver = {
306 .of_match_table = of_match_ptr(meson_sm_ids),
309 module_platform_driver_probe(meson_sm_driver, meson_sm_probe);