blob: 275b9155e473307fb82660f655e98022cde60817 [file] [log] [blame]
Yangtao Li055f5df2019-04-13 11:33:04 +01001// SPDX-License-Identifier: GPL-2.0+
Maxime Ripard3d0b16a2015-07-27 12:17:09 +01002/*
3 * Allwinner sunXi SoCs Security ID support.
4 *
5 * Copyright (c) 2013 Oliver Schinagl <oliver@schinagl.nl>
6 * Copyright (C) 2014 Maxime Ripard <maxime.ripard@free-electrons.com>
Maxime Ripard3d0b16a2015-07-27 12:17:09 +01007 */
8
Maxime Ripard3d0b16a2015-07-27 12:17:09 +01009#include <linux/device.h>
10#include <linux/io.h>
Icenowy Zheng1a963642017-03-31 13:44:48 +010011#include <linux/iopoll.h>
Maxime Ripard3d0b16a2015-07-27 12:17:09 +010012#include <linux/module.h>
13#include <linux/nvmem-provider.h>
14#include <linux/of.h>
Icenowy Zheng4a72cda2017-03-31 13:44:47 +010015#include <linux/of_device.h>
Maxime Ripard3d0b16a2015-07-27 12:17:09 +010016#include <linux/platform_device.h>
Maxime Ripard3d0b16a2015-07-27 12:17:09 +010017#include <linux/slab.h>
18#include <linux/random.h>
19
Icenowy Zheng1a963642017-03-31 13:44:48 +010020/* Registers and special values for doing register-based SID readout on H3 */
21#define SUN8I_SID_PRCTL 0x40
22#define SUN8I_SID_RDKEY 0x60
23
24#define SUN8I_SID_OFFSET_MASK 0x1FF
25#define SUN8I_SID_OFFSET_SHIFT 16
26#define SUN8I_SID_OP_LOCK (0xAC << 8)
27#define SUN8I_SID_READ BIT(1)
28
Icenowy Zheng4a72cda2017-03-31 13:44:47 +010029struct sunxi_sid_cfg {
Icenowy Zheng1a963642017-03-31 13:44:48 +010030 u32 value_offset;
Icenowy Zheng4a72cda2017-03-31 13:44:47 +010031 u32 size;
Icenowy Zheng1a963642017-03-31 13:44:48 +010032 bool need_register_readout;
Icenowy Zheng4a72cda2017-03-31 13:44:47 +010033};
34
Maxime Ripard3d0b16a2015-07-27 12:17:09 +010035struct sunxi_sid {
36 void __iomem *base;
Icenowy Zheng1a963642017-03-31 13:44:48 +010037 u32 value_offset;
Maxime Ripard3d0b16a2015-07-27 12:17:09 +010038};
39
Srinivas Kandagatla9c7b16e2016-04-24 20:28:10 +010040static int sunxi_sid_read(void *context, unsigned int offset,
41 void *val, size_t bytes)
Maxime Ripard3d0b16a2015-07-27 12:17:09 +010042{
43 struct sunxi_sid *sid = context;
Maxime Ripard3d0b16a2015-07-27 12:17:09 +010044
Chen-Yu Tsai273a4742019-04-13 11:32:52 +010045 memcpy_fromio(val, sid->base + sid->value_offset + offset, bytes);
Maxime Ripard3d0b16a2015-07-27 12:17:09 +010046
47 return 0;
48}
49
Icenowy Zheng1a963642017-03-31 13:44:48 +010050static int sun8i_sid_register_readout(const struct sunxi_sid *sid,
Icenowy Zheng0ab09d62018-03-09 14:47:17 +000051 const unsigned int offset,
52 u32 *out)
Icenowy Zheng1a963642017-03-31 13:44:48 +010053{
54 u32 reg_val;
55 int ret;
56
57 /* Set word, lock access, and set read command */
Icenowy Zheng0ab09d62018-03-09 14:47:17 +000058 reg_val = (offset & SUN8I_SID_OFFSET_MASK)
Icenowy Zheng1a963642017-03-31 13:44:48 +010059 << SUN8I_SID_OFFSET_SHIFT;
60 reg_val |= SUN8I_SID_OP_LOCK | SUN8I_SID_READ;
61 writel(reg_val, sid->base + SUN8I_SID_PRCTL);
62
63 ret = readl_poll_timeout(sid->base + SUN8I_SID_PRCTL, reg_val,
64 !(reg_val & SUN8I_SID_READ), 100, 250000);
65 if (ret)
66 return ret;
67
Icenowy Zheng0ab09d62018-03-09 14:47:17 +000068 if (out)
69 *out = readl(sid->base + SUN8I_SID_RDKEY);
70
Icenowy Zheng1a963642017-03-31 13:44:48 +010071 writel(0, sid->base + SUN8I_SID_PRCTL);
Icenowy Zheng0ab09d62018-03-09 14:47:17 +000072
73 return 0;
74}
75
76/*
77 * On Allwinner H3, the value on the 0x200 offset of the SID controller seems
78 * to be not reliable at all.
79 * Read by the registers instead.
80 */
Icenowy Zheng0ab09d62018-03-09 14:47:17 +000081static int sun8i_sid_read_by_reg(void *context, unsigned int offset,
82 void *val, size_t bytes)
83{
84 struct sunxi_sid *sid = context;
Chen-Yu Tsaide2a3ea2019-04-13 11:32:50 +010085 u32 word;
Icenowy Zheng0ab09d62018-03-09 14:47:17 +000086 int ret;
87
Chen-Yu Tsaide2a3ea2019-04-13 11:32:50 +010088 /* .stride = 4 so offset is guaranteed to be aligned */
89 while (bytes >= 4) {
90 ret = sun8i_sid_register_readout(sid, offset, val);
Icenowy Zheng0ab09d62018-03-09 14:47:17 +000091 if (ret)
92 return ret;
Chen-Yu Tsaide2a3ea2019-04-13 11:32:50 +010093
94 val += 4;
95 offset += 4;
96 bytes -= 4;
Icenowy Zheng0ab09d62018-03-09 14:47:17 +000097 }
98
Chen-Yu Tsaide2a3ea2019-04-13 11:32:50 +010099 if (!bytes)
100 return 0;
101
102 /* Handle any trailing bytes */
103 ret = sun8i_sid_register_readout(sid, offset, &word);
104 if (ret)
105 return ret;
106
107 memcpy(val, &word, bytes);
108
Icenowy Zheng1a963642017-03-31 13:44:48 +0100109 return 0;
110}
111
Maxime Ripard3d0b16a2015-07-27 12:17:09 +0100112static int sunxi_sid_probe(struct platform_device *pdev)
113{
114 struct device *dev = &pdev->dev;
115 struct resource *res;
Chen-Yu Tsai7fa5ad22019-04-13 11:32:51 +0100116 struct nvmem_config *nvmem_cfg;
Maxime Ripard3d0b16a2015-07-27 12:17:09 +0100117 struct nvmem_device *nvmem;
Maxime Ripard3d0b16a2015-07-27 12:17:09 +0100118 struct sunxi_sid *sid;
Chen-Yu Tsai9c4adfb2019-04-13 11:32:49 +0100119 int size;
Maxime Ripard3d0b16a2015-07-27 12:17:09 +0100120 char *randomness;
Icenowy Zheng4a72cda2017-03-31 13:44:47 +0100121 const struct sunxi_sid_cfg *cfg;
Maxime Ripard3d0b16a2015-07-27 12:17:09 +0100122
123 sid = devm_kzalloc(dev, sizeof(*sid), GFP_KERNEL);
124 if (!sid)
125 return -ENOMEM;
126
Icenowy Zheng4a72cda2017-03-31 13:44:47 +0100127 cfg = of_device_get_match_data(dev);
128 if (!cfg)
129 return -EINVAL;
Icenowy Zheng1a963642017-03-31 13:44:48 +0100130 sid->value_offset = cfg->value_offset;
Icenowy Zheng4a72cda2017-03-31 13:44:47 +0100131
Maxime Ripard3d0b16a2015-07-27 12:17:09 +0100132 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
133 sid->base = devm_ioremap_resource(dev, res);
134 if (IS_ERR(sid->base))
135 return PTR_ERR(sid->base);
136
Icenowy Zheng4a72cda2017-03-31 13:44:47 +0100137 size = cfg->size;
138
Chen-Yu Tsai7fa5ad22019-04-13 11:32:51 +0100139 nvmem_cfg = devm_kzalloc(dev, sizeof(*nvmem_cfg), GFP_KERNEL);
140 if (!nvmem_cfg)
141 return -ENOMEM;
142
143 nvmem_cfg->dev = dev;
144 nvmem_cfg->name = "sunxi-sid";
Samuel Holland78a005a22021-06-11 09:33:45 +0100145 nvmem_cfg->type = NVMEM_TYPE_OTP;
Chen-Yu Tsai7fa5ad22019-04-13 11:32:51 +0100146 nvmem_cfg->read_only = true;
147 nvmem_cfg->size = cfg->size;
148 nvmem_cfg->word_size = 1;
149 nvmem_cfg->stride = 4;
150 nvmem_cfg->priv = sid;
Icenowy Zheng0ab09d62018-03-09 14:47:17 +0000151 if (cfg->need_register_readout)
Chen-Yu Tsai7fa5ad22019-04-13 11:32:51 +0100152 nvmem_cfg->reg_read = sun8i_sid_read_by_reg;
Icenowy Zheng0ab09d62018-03-09 14:47:17 +0000153 else
Chen-Yu Tsai7fa5ad22019-04-13 11:32:51 +0100154 nvmem_cfg->reg_read = sunxi_sid_read;
155
156 nvmem = devm_nvmem_register(dev, nvmem_cfg);
Maxime Ripard3d0b16a2015-07-27 12:17:09 +0100157 if (IS_ERR(nvmem))
158 return PTR_ERR(nvmem);
159
Kees Cook6396bb22018-06-12 14:03:40 -0700160 randomness = kzalloc(size, GFP_KERNEL);
Bartosz Golaszewski6eed8dd2018-09-21 06:40:10 -0700161 if (!randomness)
162 return -ENOMEM;
Maxime Ripardfb727072015-09-30 13:36:31 +0100163
Chen-Yu Tsai7fa5ad22019-04-13 11:32:51 +0100164 nvmem_cfg->reg_read(sid, 0, randomness, size);
Maxime Ripard3d0b16a2015-07-27 12:17:09 +0100165 add_device_randomness(randomness, size);
166 kfree(randomness);
167
168 platform_set_drvdata(pdev, nvmem);
169
170 return 0;
Maxime Ripard3d0b16a2015-07-27 12:17:09 +0100171}
172
Icenowy Zheng4a72cda2017-03-31 13:44:47 +0100173static const struct sunxi_sid_cfg sun4i_a10_cfg = {
174 .size = 0x10,
175};
176
177static const struct sunxi_sid_cfg sun7i_a20_cfg = {
178 .size = 0x200,
179};
180
Icenowy Zheng1a963642017-03-31 13:44:48 +0100181static const struct sunxi_sid_cfg sun8i_h3_cfg = {
182 .value_offset = 0x200,
183 .size = 0x100,
184 .need_register_readout = true,
185};
186
Icenowy Zhengb7fe57b2017-10-24 10:54:34 +0100187static const struct sunxi_sid_cfg sun50i_a64_cfg = {
188 .value_offset = 0x200,
189 .size = 0x100,
Stefan Mavrodiev2ac00e32019-08-18 10:33:41 +0100190 .need_register_readout = true,
Icenowy Zhengb7fe57b2017-10-24 10:54:34 +0100191};
192
Yangtao Lifc1eb6e2019-04-13 11:33:05 +0100193static const struct sunxi_sid_cfg sun50i_h6_cfg = {
194 .value_offset = 0x200,
195 .size = 0x200,
196};
197
Maxime Ripard3d0b16a2015-07-27 12:17:09 +0100198static const struct of_device_id sunxi_sid_of_match[] = {
Icenowy Zheng4a72cda2017-03-31 13:44:47 +0100199 { .compatible = "allwinner,sun4i-a10-sid", .data = &sun4i_a10_cfg },
200 { .compatible = "allwinner,sun7i-a20-sid", .data = &sun7i_a20_cfg },
Chen-Yu Tsaida75b892019-04-13 11:32:53 +0100201 { .compatible = "allwinner,sun8i-a83t-sid", .data = &sun50i_a64_cfg },
Icenowy Zheng1a963642017-03-31 13:44:48 +0100202 { .compatible = "allwinner,sun8i-h3-sid", .data = &sun8i_h3_cfg },
Icenowy Zhengb7fe57b2017-10-24 10:54:34 +0100203 { .compatible = "allwinner,sun50i-a64-sid", .data = &sun50i_a64_cfg },
Chen-Yu Tsaida75b892019-04-13 11:32:53 +0100204 { .compatible = "allwinner,sun50i-h5-sid", .data = &sun50i_a64_cfg },
Yangtao Lifc1eb6e2019-04-13 11:33:05 +0100205 { .compatible = "allwinner,sun50i-h6-sid", .data = &sun50i_h6_cfg },
Maxime Ripard3d0b16a2015-07-27 12:17:09 +0100206 {/* sentinel */},
207};
208MODULE_DEVICE_TABLE(of, sunxi_sid_of_match);
209
210static struct platform_driver sunxi_sid_driver = {
211 .probe = sunxi_sid_probe,
Maxime Ripard3d0b16a2015-07-27 12:17:09 +0100212 .driver = {
213 .name = "eeprom-sunxi-sid",
214 .of_match_table = sunxi_sid_of_match,
215 },
216};
217module_platform_driver(sunxi_sid_driver);
218
219MODULE_AUTHOR("Oliver Schinagl <oliver@schinagl.nl>");
220MODULE_DESCRIPTION("Allwinner sunxi security id driver");
221MODULE_LICENSE("GPL");