blob: 99bd54d85fcbab9a30a4a5f952a8e82a0388dca1 [file] [log] [blame]
Maxime Ripard3d0b16a2015-07-27 12:17:09 +01001/*
2 * Allwinner sunXi SoCs Security ID support.
3 *
4 * Copyright (c) 2013 Oliver Schinagl <oliver@schinagl.nl>
5 * Copyright (C) 2014 Maxime Ripard <maxime.ripard@free-electrons.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
Maxime Ripard3d0b16a2015-07-27 12:17:09 +010016 */
17
Maxime Ripard3d0b16a2015-07-27 12:17:09 +010018#include <linux/device.h>
19#include <linux/io.h>
Icenowy Zheng1a963642017-03-31 13:44:48 +010020#include <linux/iopoll.h>
Maxime Ripard3d0b16a2015-07-27 12:17:09 +010021#include <linux/module.h>
22#include <linux/nvmem-provider.h>
23#include <linux/of.h>
Icenowy Zheng4a72cda2017-03-31 13:44:47 +010024#include <linux/of_device.h>
Maxime Ripard3d0b16a2015-07-27 12:17:09 +010025#include <linux/platform_device.h>
Maxime Ripard3d0b16a2015-07-27 12:17:09 +010026#include <linux/slab.h>
27#include <linux/random.h>
28
Icenowy Zheng1a963642017-03-31 13:44:48 +010029/* Registers and special values for doing register-based SID readout on H3 */
30#define SUN8I_SID_PRCTL 0x40
31#define SUN8I_SID_RDKEY 0x60
32
33#define SUN8I_SID_OFFSET_MASK 0x1FF
34#define SUN8I_SID_OFFSET_SHIFT 16
35#define SUN8I_SID_OP_LOCK (0xAC << 8)
36#define SUN8I_SID_READ BIT(1)
37
Maxime Ripard3d0b16a2015-07-27 12:17:09 +010038static struct nvmem_config econfig = {
39 .name = "sunxi-sid",
40 .read_only = true,
Srinivas Kandagatla9c7b16e2016-04-24 20:28:10 +010041 .stride = 4,
42 .word_size = 1,
Maxime Ripard3d0b16a2015-07-27 12:17:09 +010043};
44
Icenowy Zheng4a72cda2017-03-31 13:44:47 +010045struct sunxi_sid_cfg {
Icenowy Zheng1a963642017-03-31 13:44:48 +010046 u32 value_offset;
Icenowy Zheng4a72cda2017-03-31 13:44:47 +010047 u32 size;
Icenowy Zheng1a963642017-03-31 13:44:48 +010048 bool need_register_readout;
Icenowy Zheng4a72cda2017-03-31 13:44:47 +010049};
50
Maxime Ripard3d0b16a2015-07-27 12:17:09 +010051struct sunxi_sid {
52 void __iomem *base;
Icenowy Zheng1a963642017-03-31 13:44:48 +010053 u32 value_offset;
Maxime Ripard3d0b16a2015-07-27 12:17:09 +010054};
55
56/* We read the entire key, due to a 32 bit read alignment requirement. Since we
57 * want to return the requested byte, this results in somewhat slower code and
58 * uses 4 times more reads as needed but keeps code simpler. Since the SID is
59 * only very rarely probed, this is not really an issue.
60 */
61static u8 sunxi_sid_read_byte(const struct sunxi_sid *sid,
62 const unsigned int offset)
63{
64 u32 sid_key;
65
66 sid_key = ioread32be(sid->base + round_down(offset, 4));
67 sid_key >>= (offset % 4) * 8;
68
69 return sid_key; /* Only return the last byte */
70}
71
Srinivas Kandagatla9c7b16e2016-04-24 20:28:10 +010072static int sunxi_sid_read(void *context, unsigned int offset,
73 void *val, size_t bytes)
Maxime Ripard3d0b16a2015-07-27 12:17:09 +010074{
75 struct sunxi_sid *sid = context;
Maxime Ripard3d0b16a2015-07-27 12:17:09 +010076 u8 *buf = val;
77
Icenowy Zheng1a963642017-03-31 13:44:48 +010078 /* Offset the read operation to the real position of SID */
79 offset += sid->value_offset;
80
Srinivas Kandagatla9c7b16e2016-04-24 20:28:10 +010081 while (bytes--)
82 *buf++ = sunxi_sid_read_byte(sid, offset++);
Maxime Ripard3d0b16a2015-07-27 12:17:09 +010083
84 return 0;
85}
86
Icenowy Zheng1a963642017-03-31 13:44:48 +010087static int sun8i_sid_register_readout(const struct sunxi_sid *sid,
88 const unsigned int word)
89{
90 u32 reg_val;
91 int ret;
92
93 /* Set word, lock access, and set read command */
94 reg_val = (word & SUN8I_SID_OFFSET_MASK)
95 << SUN8I_SID_OFFSET_SHIFT;
96 reg_val |= SUN8I_SID_OP_LOCK | SUN8I_SID_READ;
97 writel(reg_val, sid->base + SUN8I_SID_PRCTL);
98
99 ret = readl_poll_timeout(sid->base + SUN8I_SID_PRCTL, reg_val,
100 !(reg_val & SUN8I_SID_READ), 100, 250000);
101 if (ret)
102 return ret;
103
104 writel(0, sid->base + SUN8I_SID_PRCTL);
105 return 0;
106}
107
Maxime Ripard3d0b16a2015-07-27 12:17:09 +0100108static int sunxi_sid_probe(struct platform_device *pdev)
109{
110 struct device *dev = &pdev->dev;
111 struct resource *res;
112 struct nvmem_device *nvmem;
Maxime Ripard3d0b16a2015-07-27 12:17:09 +0100113 struct sunxi_sid *sid;
Maxime Ripardfb727072015-09-30 13:36:31 +0100114 int ret, i, size;
Maxime Ripard3d0b16a2015-07-27 12:17:09 +0100115 char *randomness;
Icenowy Zheng4a72cda2017-03-31 13:44:47 +0100116 const struct sunxi_sid_cfg *cfg;
Maxime Ripard3d0b16a2015-07-27 12:17:09 +0100117
118 sid = devm_kzalloc(dev, sizeof(*sid), GFP_KERNEL);
119 if (!sid)
120 return -ENOMEM;
121
Icenowy Zheng4a72cda2017-03-31 13:44:47 +0100122 cfg = of_device_get_match_data(dev);
123 if (!cfg)
124 return -EINVAL;
Icenowy Zheng1a963642017-03-31 13:44:48 +0100125 sid->value_offset = cfg->value_offset;
Icenowy Zheng4a72cda2017-03-31 13:44:47 +0100126
Maxime Ripard3d0b16a2015-07-27 12:17:09 +0100127 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
128 sid->base = devm_ioremap_resource(dev, res);
129 if (IS_ERR(sid->base))
130 return PTR_ERR(sid->base);
131
Icenowy Zheng4a72cda2017-03-31 13:44:47 +0100132 size = cfg->size;
133
Icenowy Zheng1a963642017-03-31 13:44:48 +0100134 if (cfg->need_register_readout) {
135 /*
136 * H3's SID controller have a bug that the value at 0x200
137 * offset is not the correct value when the hardware is reseted.
138 * However, after doing a register-based read operation, the
139 * value become right.
140 * Do a full read operation here, but ignore its value
141 * (as it's more fast to read by direct MMIO value than
142 * with registers)
143 */
144 for (i = 0; i < (size >> 2); i++) {
145 ret = sun8i_sid_register_readout(sid, i);
146 if (ret)
147 return ret;
148 }
149 }
150
Icenowy Zheng4a72cda2017-03-31 13:44:47 +0100151 econfig.size = size;
Maxime Ripard3d0b16a2015-07-27 12:17:09 +0100152 econfig.dev = dev;
Srinivas Kandagatla9c7b16e2016-04-24 20:28:10 +0100153 econfig.reg_read = sunxi_sid_read;
154 econfig.priv = sid;
Maxime Ripard3d0b16a2015-07-27 12:17:09 +0100155 nvmem = nvmem_register(&econfig);
156 if (IS_ERR(nvmem))
157 return PTR_ERR(nvmem);
158
Caesar Wangd16abd32015-12-14 09:43:21 +0000159 randomness = kzalloc(sizeof(u8) * (size), GFP_KERNEL);
Maxime Ripardfb727072015-09-30 13:36:31 +0100160 if (!randomness) {
161 ret = -EINVAL;
162 goto err_unreg_nvmem;
163 }
164
Maxime Ripard3d0b16a2015-07-27 12:17:09 +0100165 for (i = 0; i < size; i++)
166 randomness[i] = sunxi_sid_read_byte(sid, i);
167
168 add_device_randomness(randomness, size);
169 kfree(randomness);
170
171 platform_set_drvdata(pdev, nvmem);
172
173 return 0;
Maxime Ripardfb727072015-09-30 13:36:31 +0100174
175err_unreg_nvmem:
176 nvmem_unregister(nvmem);
177 return ret;
Maxime Ripard3d0b16a2015-07-27 12:17:09 +0100178}
179
180static int sunxi_sid_remove(struct platform_device *pdev)
181{
182 struct nvmem_device *nvmem = platform_get_drvdata(pdev);
183
184 return nvmem_unregister(nvmem);
185}
186
Icenowy Zheng4a72cda2017-03-31 13:44:47 +0100187static const struct sunxi_sid_cfg sun4i_a10_cfg = {
188 .size = 0x10,
189};
190
191static const struct sunxi_sid_cfg sun7i_a20_cfg = {
192 .size = 0x200,
193};
194
Icenowy Zheng1a963642017-03-31 13:44:48 +0100195static const struct sunxi_sid_cfg sun8i_h3_cfg = {
196 .value_offset = 0x200,
197 .size = 0x100,
198 .need_register_readout = true,
199};
200
Icenowy Zhengb7fe57b2017-10-24 10:54:34 +0100201static const struct sunxi_sid_cfg sun50i_a64_cfg = {
202 .value_offset = 0x200,
203 .size = 0x100,
204};
205
Maxime Ripard3d0b16a2015-07-27 12:17:09 +0100206static const struct of_device_id sunxi_sid_of_match[] = {
Icenowy Zheng4a72cda2017-03-31 13:44:47 +0100207 { .compatible = "allwinner,sun4i-a10-sid", .data = &sun4i_a10_cfg },
208 { .compatible = "allwinner,sun7i-a20-sid", .data = &sun7i_a20_cfg },
Icenowy Zheng1a963642017-03-31 13:44:48 +0100209 { .compatible = "allwinner,sun8i-h3-sid", .data = &sun8i_h3_cfg },
Icenowy Zhengb7fe57b2017-10-24 10:54:34 +0100210 { .compatible = "allwinner,sun50i-a64-sid", .data = &sun50i_a64_cfg },
Maxime Ripard3d0b16a2015-07-27 12:17:09 +0100211 {/* sentinel */},
212};
213MODULE_DEVICE_TABLE(of, sunxi_sid_of_match);
214
215static struct platform_driver sunxi_sid_driver = {
216 .probe = sunxi_sid_probe,
217 .remove = sunxi_sid_remove,
218 .driver = {
219 .name = "eeprom-sunxi-sid",
220 .of_match_table = sunxi_sid_of_match,
221 },
222};
223module_platform_driver(sunxi_sid_driver);
224
225MODULE_AUTHOR("Oliver Schinagl <oliver@schinagl.nl>");
226MODULE_DESCRIPTION("Allwinner sunxi security id driver");
227MODULE_LICENSE("GPL");