uboot-sunxi: bump u-boot version
[openwrt.git] / target / linux / sunxi / patches-3.12 / 170-sunxi-sid-initial.patch
1 From 1d4b3ab562fa87e2c7f05cf92af1ff4b6cd42581 Mon Sep 17 00:00:00 2001
2 From: Oliver Schinagl <oliver@schinagl.nl>
3 Date: Tue, 3 Sep 2013 12:33:27 +0200
4 Subject: [PATCH] ARM: sunxi: Initial support for Allwinner's Security ID fuses
5
6 Allwinner has electric fuses (efuse) on their line of chips. This driver
7 reads those fuses, seeds the kernel entropy and exports them as a sysfs
8 node.
9
10 These fuses are most likely to be programmed at the factory, encoding
11 things like Chip ID, some sort of serial number, etc. and appear to be
12 reasonably unique.
13 While in theory, these should be writeable by the user, it will probably
14 be inconvenient to do so. Allwinner recommends that a certain input pin,
15 labeled 'efuse_vddq', be connected to GND. To write these fuses however,
16 a 2.5 V programming voltage needs to be applied to this pin.
17
18 Even so, they can still be used to generate a board-unique mac from,
19 board unique RSA key and seed the kernel RNG.
20
21 On sun7i additional storage is available, this is initially used for an
22 UEFI BOOT key, Secure JTAG key, HDMI-HDCP key and vendor specific keys.
23
24 Currently supported are the following known chips:
25 Allwinner sun4i (A10)
26 Allwinner sun5i (A10s, A13)
27 Allwinner sun7i (A20)
28
29 Signed-off-by: Oliver Schinagl <oliver@schinagl.nl>
30 ---
31  Documentation/ABI/testing/sysfs-driver-sunxi-sid   |  22 +++
32  .../bindings/misc/allwinner,sunxi-sid.txt          |  17 +++
33  drivers/misc/eeprom/Kconfig                        |  13 ++
34  drivers/misc/eeprom/Makefile                       |   1 +
35  drivers/misc/eeprom/sunxi_sid.c                    | 158 +++++++++++++++++++++
36  5 files changed, 211 insertions(+)
37  create mode 100644 Documentation/ABI/testing/sysfs-driver-sunxi-sid
38  create mode 100644 Documentation/devicetree/bindings/misc/allwinner,sunxi-sid.txt
39  create mode 100644 drivers/misc/eeprom/sunxi_sid.c
40
41 diff --git a/Documentation/ABI/testing/sysfs-driver-sunxi-sid b/Documentation/ABI/testing/sysfs-driver-sunxi-sid
42 new file mode 100644
43 index 0000000..ffb9536
44 --- /dev/null
45 +++ b/Documentation/ABI/testing/sysfs-driver-sunxi-sid
46 @@ -0,0 +1,22 @@
47 +What:          /sys/devices/*/<our-device>/eeprom
48 +Date:          August 2013
49 +Contact:       Oliver Schinagl <oliver@schinagl.nl>
50 +Description:   read-only access to the SID (Security-ID) on current
51 +               A-series SoC's from Allwinner. Currently supports A10, A10s, A13
52 +               and A20 CPU's. The earlier A1x series of SoCs exports 16 bytes,
53 +               whereas the newer A20 SoC exposes 512 bytes split into sections.
54 +               Besides the 16 bytes of SID, there's also an SJTAG area,
55 +               HDMI-HDCP key and some custom keys. Below a quick overview, for
56 +               details see the user manual:
57 +               0x000  128 bit root-key (sun[457]i)
58 +               0x010  128 bit boot-key (sun7i)
59 +               0x020   64 bit security-jtag-key (sun7i)
60 +               0x028   16 bit key configuration (sun7i)
61 +               0x02b   16 bit custom-vendor-key (sun7i)
62 +               0x02c  320 bit low general key (sun7i)
63 +               0x040   32 bit read-control access (sun7i)
64 +               0x064  224 bit low general key (sun7i)
65 +               0x080 2304 bit HDCP-key (sun7i)
66 +               0x1a0  768 bit high general key (sun7i)
67 +Users:         any user space application which wants to read the SID on
68 +               Allwinner's A-series of CPU's.
69 diff --git a/Documentation/devicetree/bindings/misc/allwinner,sunxi-sid.txt b/Documentation/devicetree/bindings/misc/allwinner,sunxi-sid.txt
70 new file mode 100644
71 index 0000000..68ba372
72 --- /dev/null
73 +++ b/Documentation/devicetree/bindings/misc/allwinner,sunxi-sid.txt
74 @@ -0,0 +1,17 @@
75 +Allwinner sunxi-sid
76 +
77 +Required properties:
78 +- compatible: "allwinner,sun4i-sid" or "allwinner,sun7i-a20-sid".
79 +- reg: Should contain registers location and length
80 +
81 +Example for sun4i:
82 +       sid@01c23800 {
83 +               compatible = "allwinner,sun4i-sid";
84 +               reg = <0x01c23800 0x10>
85 +       };
86 +
87 +Example for sun7i:
88 +       sid@01c23800 {
89 +               compatible = "allwinner,sun7i-a20-sid";
90 +               reg = <0x01c23800 0x200>
91 +       };
92 diff --git a/drivers/misc/eeprom/Kconfig b/drivers/misc/eeprom/Kconfig
93 index 04f2e1f..9536852f 100644
94 --- a/drivers/misc/eeprom/Kconfig
95 +++ b/drivers/misc/eeprom/Kconfig
96 @@ -96,4 +96,17 @@ config EEPROM_DIGSY_MTC_CFG
97  
98           If unsure, say N.
99  
100 +config EEPROM_SUNXI_SID
101 +       tristate "Allwinner sunxi security ID support"
102 +       depends on ARCH_SUNXI && SYSFS
103 +       help
104 +         This is a driver for the 'security ID' available on various Allwinner
105 +         devices.
106 +
107 +         Due to the potential risks involved with changing e-fuses,
108 +         this driver is read-only.
109 +
110 +         This driver can also be built as a module. If so, the module
111 +         will be called sunxi_sid.
112 +
113  endmenu
114 diff --git a/drivers/misc/eeprom/Makefile b/drivers/misc/eeprom/Makefile
115 index fc1e81d..9507aec 100644
116 --- a/drivers/misc/eeprom/Makefile
117 +++ b/drivers/misc/eeprom/Makefile
118 @@ -4,4 +4,5 @@ obj-$(CONFIG_EEPROM_LEGACY)     += eeprom.o
119  obj-$(CONFIG_EEPROM_MAX6875)   += max6875.o
120  obj-$(CONFIG_EEPROM_93CX6)     += eeprom_93cx6.o
121  obj-$(CONFIG_EEPROM_93XX46)    += eeprom_93xx46.o
122 +obj-$(CONFIG_EEPROM_SUNXI_SID) += sunxi_sid.o
123  obj-$(CONFIG_EEPROM_DIGSY_MTC_CFG) += digsy_mtc_eeprom.o
124 diff --git a/drivers/misc/eeprom/sunxi_sid.c b/drivers/misc/eeprom/sunxi_sid.c
125 new file mode 100644
126 index 0000000..9c34e57
127 --- /dev/null
128 +++ b/drivers/misc/eeprom/sunxi_sid.c
129 @@ -0,0 +1,158 @@
130 +/*
131 + * Copyright (c) 2013 Oliver Schinagl <oliver@schinagl.nl>
132 + * http://www.linux-sunxi.org
133 + *
134 + * This program is free software; you can redistribute it and/or modify
135 + * it under the terms of the GNU General Public License as published by
136 + * the Free Software Foundation; either version 2 of the License, or
137 + * (at your option) any later version.
138 + *
139 + * This program is distributed in the hope that it will be useful,
140 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
141 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
142 + * GNU General Public License for more details.
143 + *
144 + * This driver exposes the Allwinner security ID, efuses exported in byte-
145 + * sized chunks.
146 + */
147 +
148 +#include <linux/compiler.h>
149 +#include <linux/device.h>
150 +#include <linux/err.h>
151 +#include <linux/export.h>
152 +#include <linux/fs.h>
153 +#include <linux/init.h>
154 +#include <linux/io.h>
155 +#include <linux/kernel.h>
156 +#include <linux/kobject.h>
157 +#include <linux/module.h>
158 +#include <linux/of_device.h>
159 +#include <linux/platform_device.h>
160 +#include <linux/random.h>
161 +#include <linux/slab.h>
162 +#include <linux/stat.h>
163 +#include <linux/sysfs.h>
164 +#include <linux/types.h>
165 +
166 +#define DRV_NAME "sunxi-sid"
167 +
168 +struct sunxi_sid_data {
169 +       void __iomem *reg_base;
170 +       unsigned int keysize;
171 +};
172 +
173 +/* We read the entire key, due to a 32 bit read alignment requirement. Since we
174 + * want to return the requested byte, this results in somewhat slower code and
175 + * uses 4 times more reads as needed but keeps code simpler. Since the SID is
176 + * only very rarely probed, this is not really an issue.
177 + */
178 +static u8 sunxi_sid_read_byte(const struct sunxi_sid_data *sid_data,
179 +                             const unsigned int offset)
180 +{
181 +       u32 sid_key;
182 +
183 +       if (offset >= sid_data->keysize)
184 +               return 0;
185 +
186 +       sid_key = ioread32be(sid_data->reg_base + round_down(offset, 4));
187 +       sid_key >>= (offset % 4) * 8;
188 +
189 +       return sid_key; /* Only return the last byte */
190 +}
191 +
192 +static ssize_t sid_read(struct file *fd, struct kobject *kobj,
193 +                       struct bin_attribute *attr, char *buf,
194 +                       loff_t pos, size_t size)
195 +{
196 +       struct platform_device *pdev;
197 +       struct sunxi_sid_data *sid_data;
198 +       int i;
199 +
200 +       pdev = to_platform_device(kobj_to_dev(kobj));
201 +       sid_data = platform_get_drvdata(pdev);
202 +
203 +       if (pos < 0 || pos >= sid_data->keysize)
204 +               return 0;
205 +       if (size > sid_data->keysize - pos)
206 +               size = sid_data->keysize - pos;
207 +
208 +       for (i = 0; i < size; i++)
209 +               buf[i] = sunxi_sid_read_byte(sid_data, pos + i);
210 +
211 +       return i;
212 +}
213 +
214 +static struct bin_attribute sid_bin_attr = {
215 +       .attr = { .name = "eeprom", .mode = S_IRUGO, },
216 +       .read = sid_read,
217 +};
218 +
219 +static int sunxi_sid_remove(struct platform_device *pdev)
220 +{
221 +       device_remove_bin_file(&pdev->dev, &sid_bin_attr);
222 +       dev_dbg(&pdev->dev, "driver unloaded\n");
223 +
224 +       return 0;
225 +}
226 +
227 +static const struct of_device_id sunxi_sid_of_match[] = {
228 +       { .compatible = "allwinner,sun4i-sid", .data = (void *)16},
229 +       { .compatible = "allwinner,sun7i-a20-sid", .data = (void *)512},
230 +       {/* sentinel */},
231 +};
232 +MODULE_DEVICE_TABLE(of, sunxi_sid_of_match);
233 +
234 +static int sunxi_sid_probe(struct platform_device *pdev)
235 +{
236 +       struct sunxi_sid_data *sid_data;
237 +       struct resource *res;
238 +       const struct of_device_id *of_dev_id;
239 +       u8 *entropy;
240 +       unsigned int i;
241 +
242 +       sid_data = devm_kzalloc(&pdev->dev, sizeof(struct sunxi_sid_data),
243 +                               GFP_KERNEL);
244 +       if (!sid_data)
245 +               return -ENOMEM;
246 +
247 +       res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
248 +       sid_data->reg_base = devm_ioremap_resource(&pdev->dev, res);
249 +       if (IS_ERR(sid_data->reg_base))
250 +               return PTR_ERR(sid_data->reg_base);
251 +
252 +       of_dev_id = of_match_device(sunxi_sid_of_match, &pdev->dev);
253 +       if (!of_dev_id)
254 +               return -ENODEV;
255 +       sid_data->keysize = (int)of_dev_id->data;
256 +
257 +       platform_set_drvdata(pdev, sid_data);
258 +
259 +       sid_bin_attr.size = sid_data->keysize;
260 +       if (device_create_bin_file(&pdev->dev, &sid_bin_attr))
261 +               return -ENODEV;
262 +
263 +       entropy = kzalloc(sizeof(u8) * sid_data->keysize, GFP_KERNEL);
264 +       for (i = 0; i < sid_data->keysize; i++)
265 +               entropy[i] = sunxi_sid_read_byte(sid_data, i);
266 +       add_device_randomness(entropy, sid_data->keysize);
267 +       kfree(entropy);
268 +
269 +       dev_dbg(&pdev->dev, "loaded\n");
270 +
271 +       return 0;
272 +}
273 +
274 +static struct platform_driver sunxi_sid_driver = {
275 +       .probe = sunxi_sid_probe,
276 +       .remove = sunxi_sid_remove,
277 +       .driver = {
278 +               .name = DRV_NAME,
279 +               .owner = THIS_MODULE,
280 +               .of_match_table = sunxi_sid_of_match,
281 +       },
282 +};
283 +module_platform_driver(sunxi_sid_driver);
284 +
285 +MODULE_AUTHOR("Oliver Schinagl <oliver@schinagl.nl>");
286 +MODULE_DESCRIPTION("Allwinner sunxi security id driver");
287 +MODULE_LICENSE("GPL");
288 -- 
289 1.8.4
290