gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / drivers / firmware / meson / meson_sm.c
blob2854b56f6e0bd1721d3882b345f539bf1cac4791
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Amlogic Secure Monitor driver
5 * Copyright (C) 2016 Endless Mobile, Inc.
6 * Author: Carlo Caione <carlo@endlessm.com>
7 */
9 #define pr_fmt(fmt) "meson-sm: " fmt
11 #include <linux/arm-smccc.h>
12 #include <linux/bug.h>
13 #include <linux/io.h>
14 #include <linux/module.h>
15 #include <linux/of.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>
25 struct meson_sm_cmd {
26 unsigned int index;
27 u32 smc_id;
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 = {
39 .shmem_size = SZ_4K,
40 .cmd_shmem_in_base = 0x82000020,
41 .cmd_shmem_out_base = 0x82000021,
42 .cmd = {
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),
49 { /* sentinel */ },
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)
65 cmd++;
67 return cmd->smc_id;
70 static u32 __meson_sm_call(u32 cmd, u32 arg0, u32 arg1, u32 arg2,
71 u32 arg3, u32 arg4)
73 struct arm_smccc_res res;
75 arm_smccc_smc(cmd, arg0, arg1, arg2, arg3, arg4, 0, 0, &res);
76 return res.a0;
79 static void __iomem *meson_sm_map_shmem(u32 cmd_shmem, unsigned int size)
81 u32 sm_phy_base;
83 sm_phy_base = __meson_sm_call(cmd_shmem, 0, 0, 0, 0, 0);
84 if (!sm_phy_base)
85 return 0;
87 return ioremap_cache(sm_phy_base, size);
90 /**
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)
107 u32 cmd, lret;
109 if (!fw->chip)
110 return -ENOENT;
112 cmd = meson_sm_get_cmd(fw->chip, cmd_index);
113 if (!cmd)
114 return -EINVAL;
116 lret = __meson_sm_call(cmd, arg0, arg1, arg2, arg3, arg4);
118 if (ret)
119 *ret = lret;
121 return 0;
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)
146 u32 size;
147 int ret;
149 if (!fw->chip)
150 return -ENOENT;
152 if (!fw->chip->cmd_shmem_out_base)
153 return -EINVAL;
155 if (bsize > fw->chip->shmem_size)
156 return -EINVAL;
158 if (meson_sm_call(fw, cmd_index, &size, arg0, arg1, arg2, arg3, arg4) < 0)
159 return -EINVAL;
161 if (size > bsize)
162 return -EINVAL;
164 ret = size;
166 if (!size)
167 size = bsize;
169 if (buffer)
170 memcpy(buffer, fw->sm_shmem_out_base, size);
172 return ret;
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)
195 u32 written;
197 if (!fw->chip)
198 return -ENOENT;
200 if (size > fw->chip->shmem_size)
201 return -EINVAL;
203 if (!fw->chip->cmd_shmem_in_base)
204 return -EINVAL;
206 memcpy(fw->sm_shmem_in_base, buffer, size);
208 if (meson_sm_call(fw, cmd_index, &written, arg0, arg1, arg2, arg3, arg4) < 0)
209 return -EINVAL;
211 if (!written)
212 return -EINVAL;
214 return written;
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);
229 if (!pdev)
230 return NULL;
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,
241 char *buf)
243 struct platform_device *pdev = to_platform_device(dev);
244 struct meson_sm_firmware *fw;
245 uint8_t *id_buf;
246 int ret;
248 fw = platform_get_drvdata(pdev);
250 id_buf = kmalloc(SM_CHIP_ID_LENGTH, GFP_KERNEL);
251 if (!id_buf)
252 return -ENOMEM;
254 ret = meson_sm_call_read(fw, id_buf, SM_CHIP_ID_LENGTH, SM_GET_CHIP_ID,
255 0, 0, 0, 0, 0);
256 if (ret < 0) {
257 kfree(id_buf);
258 return ret;
261 ret = sprintf(buf, "%12phN\n", &id_buf[SM_CHIP_ID_OFFSET]);
263 kfree(id_buf);
265 return ret;
268 static DEVICE_ATTR_RO(serial);
270 static struct attribute *meson_sm_sysfs_attributes[] = {
271 &dev_attr_serial.attr,
272 NULL,
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 },
281 { /* sentinel */ },
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);
291 if (!fw)
292 return -ENOMEM;
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,
298 chip->shmem_size);
299 if (WARN_ON(!fw->sm_shmem_in_base))
300 goto out;
303 if (chip->cmd_shmem_out_base) {
304 fw->sm_shmem_out_base = meson_sm_map_shmem(chip->cmd_shmem_out_base,
305 chip->shmem_size);
306 if (WARN_ON(!fw->sm_shmem_out_base))
307 goto out_in_base;
310 fw->chip = chip;
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))
317 goto out_in_base;
319 return 0;
321 out_in_base:
322 iounmap(fw->sm_shmem_in_base);
323 out:
324 return -EINVAL;
327 static struct platform_driver meson_sm_driver = {
328 .driver = {
329 .name = "meson-sm",
330 .of_match_table = of_match_ptr(meson_sm_ids),
333 module_platform_driver_probe(meson_sm_driver, meson_sm_probe);