1 // SPDX-License-Identifier: GPL-2.0+
3 * Allwinner sunXi SoCs Security ID support.
5 * Copyright (c) 2013 Oliver Schinagl <oliver@schinagl.nl>
6 * Copyright (C) 2014 Maxime Ripard <maxime.ripard@free-electrons.com>
9 #include <linux/device.h>
11 #include <linux/iopoll.h>
12 #include <linux/module.h>
13 #include <linux/nvmem-provider.h>
15 #include <linux/platform_device.h>
16 #include <linux/slab.h>
17 #include <linux/random.h>
19 /* Registers and special values for doing register-based SID readout on H3 */
20 #define SUN8I_SID_PRCTL 0x40
21 #define SUN8I_SID_RDKEY 0x60
23 #define SUN8I_SID_OFFSET_MASK 0x1FF
24 #define SUN8I_SID_OFFSET_SHIFT 16
25 #define SUN8I_SID_OP_LOCK (0xAC << 8)
26 #define SUN8I_SID_READ BIT(1)
28 struct sunxi_sid_cfg {
31 bool need_register_readout;
39 static int sunxi_sid_read(void *context, unsigned int offset,
40 void *val, size_t bytes)
42 struct sunxi_sid *sid = context;
45 /* .stride = 4 so offset is guaranteed to be aligned */
46 __ioread32_copy(val, sid->base + sid->value_offset + offset, bytes / 4);
48 val += round_down(bytes, 4);
49 offset += round_down(bytes, 4);
55 /* Handle any trailing bytes */
56 word = readl_relaxed(sid->base + sid->value_offset + offset);
57 memcpy(val, &word, bytes);
62 static int sun8i_sid_register_readout(const struct sunxi_sid *sid,
63 const unsigned int offset,
69 /* Set word, lock access, and set read command */
70 reg_val = (offset & SUN8I_SID_OFFSET_MASK)
71 << SUN8I_SID_OFFSET_SHIFT;
72 reg_val |= SUN8I_SID_OP_LOCK | SUN8I_SID_READ;
73 writel(reg_val, sid->base + SUN8I_SID_PRCTL);
75 ret = readl_poll_timeout(sid->base + SUN8I_SID_PRCTL, reg_val,
76 !(reg_val & SUN8I_SID_READ), 100, 250000);
81 *out = readl(sid->base + SUN8I_SID_RDKEY);
83 writel(0, sid->base + SUN8I_SID_PRCTL);
89 * On Allwinner H3, the value on the 0x200 offset of the SID controller seems
90 * to be not reliable at all.
91 * Read by the registers instead.
93 static int sun8i_sid_read_by_reg(void *context, unsigned int offset,
94 void *val, size_t bytes)
96 struct sunxi_sid *sid = context;
100 /* .stride = 4 so offset is guaranteed to be aligned */
102 ret = sun8i_sid_register_readout(sid, offset, val);
114 /* Handle any trailing bytes */
115 ret = sun8i_sid_register_readout(sid, offset, &word);
119 memcpy(val, &word, bytes);
124 static int sunxi_sid_probe(struct platform_device *pdev)
126 struct device *dev = &pdev->dev;
127 struct nvmem_config *nvmem_cfg;
128 struct nvmem_device *nvmem;
129 struct sunxi_sid *sid;
132 const struct sunxi_sid_cfg *cfg;
134 sid = devm_kzalloc(dev, sizeof(*sid), GFP_KERNEL);
138 cfg = of_device_get_match_data(dev);
141 sid->value_offset = cfg->value_offset;
143 sid->base = devm_platform_ioremap_resource(pdev, 0);
144 if (IS_ERR(sid->base))
145 return PTR_ERR(sid->base);
149 nvmem_cfg = devm_kzalloc(dev, sizeof(*nvmem_cfg), GFP_KERNEL);
153 nvmem_cfg->dev = dev;
154 nvmem_cfg->name = "sunxi-sid";
155 nvmem_cfg->type = NVMEM_TYPE_OTP;
156 nvmem_cfg->read_only = true;
157 nvmem_cfg->size = cfg->size;
158 nvmem_cfg->word_size = 1;
159 nvmem_cfg->stride = 4;
160 nvmem_cfg->priv = sid;
161 if (cfg->need_register_readout)
162 nvmem_cfg->reg_read = sun8i_sid_read_by_reg;
164 nvmem_cfg->reg_read = sunxi_sid_read;
166 nvmem = devm_nvmem_register(dev, nvmem_cfg);
168 return PTR_ERR(nvmem);
170 randomness = kzalloc(size, GFP_KERNEL);
174 nvmem_cfg->reg_read(sid, 0, randomness, size);
175 add_device_randomness(randomness, size);
178 platform_set_drvdata(pdev, nvmem);
183 static const struct sunxi_sid_cfg sun4i_a10_cfg = {
187 static const struct sunxi_sid_cfg sun7i_a20_cfg = {
191 static const struct sunxi_sid_cfg sun8i_h3_cfg = {
192 .value_offset = 0x200,
194 .need_register_readout = true,
197 static const struct sunxi_sid_cfg sun50i_a64_cfg = {
198 .value_offset = 0x200,
202 static const struct sunxi_sid_cfg sun50i_h6_cfg = {
203 .value_offset = 0x200,
207 static const struct of_device_id sunxi_sid_of_match[] = {
208 { .compatible = "allwinner,sun4i-a10-sid", .data = &sun4i_a10_cfg },
209 { .compatible = "allwinner,sun7i-a20-sid", .data = &sun7i_a20_cfg },
210 { .compatible = "allwinner,sun8i-a83t-sid", .data = &sun50i_a64_cfg },
211 { .compatible = "allwinner,sun8i-h3-sid", .data = &sun8i_h3_cfg },
212 { .compatible = "allwinner,sun20i-d1-sid", .data = &sun50i_a64_cfg },
213 { .compatible = "allwinner,sun50i-a64-sid", .data = &sun50i_a64_cfg },
214 { .compatible = "allwinner,sun50i-h5-sid", .data = &sun50i_a64_cfg },
215 { .compatible = "allwinner,sun50i-h6-sid", .data = &sun50i_h6_cfg },
218 MODULE_DEVICE_TABLE(of, sunxi_sid_of_match);
220 static struct platform_driver sunxi_sid_driver = {
221 .probe = sunxi_sid_probe,
223 .name = "eeprom-sunxi-sid",
224 .of_match_table = sunxi_sid_of_match,
227 module_platform_driver(sunxi_sid_driver);
229 MODULE_AUTHOR("Oliver Schinagl <oliver@schinagl.nl>");
230 MODULE_DESCRIPTION("Allwinner sunxi security id driver");
231 MODULE_LICENSE("GPL");