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/of_device.h>
16 #include <linux/platform_device.h>
17 #include <linux/slab.h>
18 #include <linux/random.h>
20 /* Registers and special values for doing register-based SID readout on H3 */
21 #define SUN8I_SID_PRCTL 0x40
22 #define SUN8I_SID_RDKEY 0x60
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)
29 struct sunxi_sid_cfg
{
32 bool need_register_readout
;
40 static int sunxi_sid_read(void *context
, unsigned int offset
,
41 void *val
, size_t bytes
)
43 struct sunxi_sid
*sid
= context
;
45 memcpy_fromio(val
, sid
->base
+ sid
->value_offset
+ offset
, bytes
);
50 static int sun8i_sid_register_readout(const struct sunxi_sid
*sid
,
51 const unsigned int offset
,
57 /* Set word, lock access, and set read command */
58 reg_val
= (offset
& SUN8I_SID_OFFSET_MASK
)
59 << SUN8I_SID_OFFSET_SHIFT
;
60 reg_val
|= SUN8I_SID_OP_LOCK
| SUN8I_SID_READ
;
61 writel(reg_val
, sid
->base
+ SUN8I_SID_PRCTL
);
63 ret
= readl_poll_timeout(sid
->base
+ SUN8I_SID_PRCTL
, reg_val
,
64 !(reg_val
& SUN8I_SID_READ
), 100, 250000);
69 *out
= readl(sid
->base
+ SUN8I_SID_RDKEY
);
71 writel(0, sid
->base
+ SUN8I_SID_PRCTL
);
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.
81 static int sun8i_sid_read_by_reg(void *context
, unsigned int offset
,
82 void *val
, size_t bytes
)
84 struct sunxi_sid
*sid
= context
;
88 /* .stride = 4 so offset is guaranteed to be aligned */
90 ret
= sun8i_sid_register_readout(sid
, offset
, val
);
102 /* Handle any trailing bytes */
103 ret
= sun8i_sid_register_readout(sid
, offset
, &word
);
107 memcpy(val
, &word
, bytes
);
112 static int sunxi_sid_probe(struct platform_device
*pdev
)
114 struct device
*dev
= &pdev
->dev
;
115 struct resource
*res
;
116 struct nvmem_config
*nvmem_cfg
;
117 struct nvmem_device
*nvmem
;
118 struct sunxi_sid
*sid
;
121 const struct sunxi_sid_cfg
*cfg
;
123 sid
= devm_kzalloc(dev
, sizeof(*sid
), GFP_KERNEL
);
127 cfg
= of_device_get_match_data(dev
);
130 sid
->value_offset
= cfg
->value_offset
;
132 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
);
139 nvmem_cfg
= devm_kzalloc(dev
, sizeof(*nvmem_cfg
), GFP_KERNEL
);
143 nvmem_cfg
->dev
= dev
;
144 nvmem_cfg
->name
= "sunxi-sid";
145 nvmem_cfg
->read_only
= true;
146 nvmem_cfg
->size
= cfg
->size
;
147 nvmem_cfg
->word_size
= 1;
148 nvmem_cfg
->stride
= 4;
149 nvmem_cfg
->priv
= sid
;
150 if (cfg
->need_register_readout
)
151 nvmem_cfg
->reg_read
= sun8i_sid_read_by_reg
;
153 nvmem_cfg
->reg_read
= sunxi_sid_read
;
155 nvmem
= devm_nvmem_register(dev
, nvmem_cfg
);
157 return PTR_ERR(nvmem
);
159 randomness
= kzalloc(size
, GFP_KERNEL
);
163 nvmem_cfg
->reg_read(sid
, 0, randomness
, size
);
164 add_device_randomness(randomness
, size
);
167 platform_set_drvdata(pdev
, nvmem
);
172 static const struct sunxi_sid_cfg sun4i_a10_cfg
= {
176 static const struct sunxi_sid_cfg sun7i_a20_cfg
= {
180 static const struct sunxi_sid_cfg sun8i_h3_cfg
= {
181 .value_offset
= 0x200,
183 .need_register_readout
= true,
186 static const struct sunxi_sid_cfg sun50i_a64_cfg
= {
187 .value_offset
= 0x200,
189 .need_register_readout
= true,
192 static const struct sunxi_sid_cfg sun50i_h6_cfg
= {
193 .value_offset
= 0x200,
197 static const struct of_device_id sunxi_sid_of_match
[] = {
198 { .compatible
= "allwinner,sun4i-a10-sid", .data
= &sun4i_a10_cfg
},
199 { .compatible
= "allwinner,sun7i-a20-sid", .data
= &sun7i_a20_cfg
},
200 { .compatible
= "allwinner,sun8i-a83t-sid", .data
= &sun50i_a64_cfg
},
201 { .compatible
= "allwinner,sun8i-h3-sid", .data
= &sun8i_h3_cfg
},
202 { .compatible
= "allwinner,sun50i-a64-sid", .data
= &sun50i_a64_cfg
},
203 { .compatible
= "allwinner,sun50i-h5-sid", .data
= &sun50i_a64_cfg
},
204 { .compatible
= "allwinner,sun50i-h6-sid", .data
= &sun50i_h6_cfg
},
207 MODULE_DEVICE_TABLE(of
, sunxi_sid_of_match
);
209 static struct platform_driver sunxi_sid_driver
= {
210 .probe
= sunxi_sid_probe
,
212 .name
= "eeprom-sunxi-sid",
213 .of_match_table
= sunxi_sid_of_match
,
216 module_platform_driver(sunxi_sid_driver
);
218 MODULE_AUTHOR("Oliver Schinagl <oliver@schinagl.nl>");
219 MODULE_DESCRIPTION("Allwinner sunxi security id driver");
220 MODULE_LICENSE("GPL");