2 * Amlogic Secure Monitor driver
4 * Copyright (C) 2016 Endless Mobile, Inc.
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>
20 #include <linux/module.h>
22 #include <linux/of_device.h>
23 #include <linux/platform_device.h>
24 #include <linux/printk.h>
25 #include <linux/types.h>
26 #include <linux/sizes.h>
28 #include <linux/firmware/meson/meson_sm.h>
34 #define CMD(d, s) { .index = (d), .smc_id = (s), }
36 struct meson_sm_chip {
37 unsigned int shmem_size;
38 u32 cmd_shmem_in_base;
39 u32 cmd_shmem_out_base;
40 struct meson_sm_cmd cmd[];
43 struct meson_sm_chip gxbb_chip = {
45 .cmd_shmem_in_base = 0x82000020,
46 .cmd_shmem_out_base = 0x82000021,
48 CMD(SM_EFUSE_READ, 0x82000030),
49 CMD(SM_EFUSE_WRITE, 0x82000031),
50 CMD(SM_EFUSE_USER_MAX, 0x82000033),
55 struct meson_sm_firmware {
56 const struct meson_sm_chip *chip;
57 void __iomem *sm_shmem_in_base;
58 void __iomem *sm_shmem_out_base;
61 static struct meson_sm_firmware fw;
63 static u32 meson_sm_get_cmd(const struct meson_sm_chip *chip,
64 unsigned int cmd_index)
66 const struct meson_sm_cmd *cmd = chip->cmd;
68 while (cmd->smc_id && cmd->index != cmd_index)
74 static u32 __meson_sm_call(u32 cmd, u32 arg0, u32 arg1, u32 arg2,
77 struct arm_smccc_res res;
79 arm_smccc_smc(cmd, arg0, arg1, arg2, arg3, arg4, 0, 0, &res);
83 static void __iomem *meson_sm_map_shmem(u32 cmd_shmem, unsigned int size)
87 sm_phy_base = __meson_sm_call(cmd_shmem, 0, 0, 0, 0, 0);
91 return ioremap_cache(sm_phy_base, size);
95 * meson_sm_call - generic SMC32 call to the secure-monitor
97 * @cmd_index: Index of the SMC32 function ID
98 * @ret: Returned value
99 * @arg0: SMC32 Argument 0
100 * @arg1: SMC32 Argument 1
101 * @arg2: SMC32 Argument 2
102 * @arg3: SMC32 Argument 3
103 * @arg4: SMC32 Argument 4
105 * Return: 0 on success, a negative value on error
107 int meson_sm_call(unsigned int cmd_index, u32 *ret, u32 arg0,
108 u32 arg1, u32 arg2, u32 arg3, u32 arg4)
115 cmd = meson_sm_get_cmd(fw.chip, cmd_index);
119 lret = __meson_sm_call(cmd, arg0, arg1, arg2, arg3, arg4);
126 EXPORT_SYMBOL(meson_sm_call);
129 * meson_sm_call_read - retrieve data from secure-monitor
131 * @buffer: Buffer to store the retrieved data
132 * @bsize: Size of the buffer
133 * @cmd_index: Index of the SMC32 function ID
134 * @arg0: SMC32 Argument 0
135 * @arg1: SMC32 Argument 1
136 * @arg2: SMC32 Argument 2
137 * @arg3: SMC32 Argument 3
138 * @arg4: SMC32 Argument 4
140 * Return: size of read data on success, a negative value on error
141 * When 0 is returned there is no guarantee about the amount of
142 * data read and bsize bytes are copied in buffer.
144 int meson_sm_call_read(void *buffer, unsigned int bsize, unsigned int cmd_index,
145 u32 arg0, u32 arg1, u32 arg2, u32 arg3, u32 arg4)
153 if (!fw.chip->cmd_shmem_out_base)
156 if (bsize > fw.chip->shmem_size)
159 if (meson_sm_call(cmd_index, &size, arg0, arg1, arg2, arg3, arg4) < 0)
171 memcpy(buffer, fw.sm_shmem_out_base, size);
175 EXPORT_SYMBOL(meson_sm_call_read);
178 * meson_sm_call_write - send data to secure-monitor
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(void *buffer, unsigned int size, unsigned int cmd_index,
192 u32 arg0, u32 arg1, u32 arg2, u32 arg3, u32 arg4)
199 if (size > fw.chip->shmem_size)
202 if (!fw.chip->cmd_shmem_in_base)
205 memcpy(fw.sm_shmem_in_base, buffer, size);
207 if (meson_sm_call(cmd_index, &written, arg0, arg1, arg2, arg3, arg4) < 0)
215 EXPORT_SYMBOL(meson_sm_call_write);
217 static const struct of_device_id meson_sm_ids[] = {
218 { .compatible = "amlogic,meson-gxbb-sm", .data = &gxbb_chip },
222 static int __init meson_sm_probe(struct platform_device *pdev)
224 const struct meson_sm_chip *chip;
226 chip = of_match_device(meson_sm_ids, &pdev->dev)->data;
228 if (chip->cmd_shmem_in_base) {
229 fw.sm_shmem_in_base = meson_sm_map_shmem(chip->cmd_shmem_in_base,
231 if (WARN_ON(!fw.sm_shmem_in_base))
235 if (chip->cmd_shmem_out_base) {
236 fw.sm_shmem_out_base = meson_sm_map_shmem(chip->cmd_shmem_out_base,
238 if (WARN_ON(!fw.sm_shmem_out_base))
243 pr_info("secure-monitor enabled\n");
248 iounmap(fw.sm_shmem_in_base);
253 static struct platform_driver meson_sm_driver = {
256 .of_match_table = of_match_ptr(meson_sm_ids),
259 module_platform_driver_probe(meson_sm_driver, meson_sm_probe);