Linux 4.18.10
[linux/fpc-iii.git] / drivers / firmware / meson / meson_sm.c
blob0ec2ca87318c06c73976fb368f382667a78d3df1
1 /*
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>
19 #include <linux/io.h>
20 #include <linux/module.h>
21 #include <linux/of.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>
30 struct meson_sm_cmd {
31 unsigned int index;
32 u32 smc_id;
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 = {
44 .shmem_size = SZ_4K,
45 .cmd_shmem_in_base = 0x82000020,
46 .cmd_shmem_out_base = 0x82000021,
47 .cmd = {
48 CMD(SM_EFUSE_READ, 0x82000030),
49 CMD(SM_EFUSE_WRITE, 0x82000031),
50 CMD(SM_EFUSE_USER_MAX, 0x82000033),
51 { /* sentinel */ },
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)
69 cmd++;
71 return cmd->smc_id;
74 static u32 __meson_sm_call(u32 cmd, u32 arg0, u32 arg1, u32 arg2,
75 u32 arg3, u32 arg4)
77 struct arm_smccc_res res;
79 arm_smccc_smc(cmd, arg0, arg1, arg2, arg3, arg4, 0, 0, &res);
80 return res.a0;
83 static void __iomem *meson_sm_map_shmem(u32 cmd_shmem, unsigned int size)
85 u32 sm_phy_base;
87 sm_phy_base = __meson_sm_call(cmd_shmem, 0, 0, 0, 0, 0);
88 if (!sm_phy_base)
89 return 0;
91 return ioremap_cache(sm_phy_base, size);
94 /**
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)
110 u32 cmd, lret;
112 if (!fw.chip)
113 return -ENOENT;
115 cmd = meson_sm_get_cmd(fw.chip, cmd_index);
116 if (!cmd)
117 return -EINVAL;
119 lret = __meson_sm_call(cmd, arg0, arg1, arg2, arg3, arg4);
121 if (ret)
122 *ret = lret;
124 return 0;
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)
147 u32 size;
148 int ret;
150 if (!fw.chip)
151 return -ENOENT;
153 if (!fw.chip->cmd_shmem_out_base)
154 return -EINVAL;
156 if (bsize > fw.chip->shmem_size)
157 return -EINVAL;
159 if (meson_sm_call(cmd_index, &size, arg0, arg1, arg2, arg3, arg4) < 0)
160 return -EINVAL;
162 if (size > bsize)
163 return -EINVAL;
165 ret = size;
167 if (!size)
168 size = bsize;
170 if (buffer)
171 memcpy(buffer, fw.sm_shmem_out_base, size);
173 return ret;
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)
194 u32 written;
196 if (!fw.chip)
197 return -ENOENT;
199 if (size > fw.chip->shmem_size)
200 return -EINVAL;
202 if (!fw.chip->cmd_shmem_in_base)
203 return -EINVAL;
205 memcpy(fw.sm_shmem_in_base, buffer, size);
207 if (meson_sm_call(cmd_index, &written, arg0, arg1, arg2, arg3, arg4) < 0)
208 return -EINVAL;
210 if (!written)
211 return -EINVAL;
213 return written;
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 },
219 { /* sentinel */ },
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,
230 chip->shmem_size);
231 if (WARN_ON(!fw.sm_shmem_in_base))
232 goto out;
235 if (chip->cmd_shmem_out_base) {
236 fw.sm_shmem_out_base = meson_sm_map_shmem(chip->cmd_shmem_out_base,
237 chip->shmem_size);
238 if (WARN_ON(!fw.sm_shmem_out_base))
239 goto out_in_base;
242 fw.chip = chip;
243 pr_info("secure-monitor enabled\n");
245 return 0;
247 out_in_base:
248 iounmap(fw.sm_shmem_in_base);
249 out:
250 return -EINVAL;
253 static struct platform_driver meson_sm_driver = {
254 .driver = {
255 .name = "meson-sm",
256 .of_match_table = of_match_ptr(meson_sm_ids),
259 module_platform_driver_probe(meson_sm_driver, meson_sm_probe);