1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include <linux/device.h>
19#include <linux/io.h>
20#include <linux/module.h>
21#include <linux/nvmem-provider.h>
22#include <linux/of.h>
23#include <linux/platform_device.h>
24#include <linux/slab.h>
25#include <linux/random.h>
26
27static struct nvmem_config econfig = {
28 .name = "sunxi-sid",
29 .read_only = true,
30 .stride = 4,
31 .word_size = 1,
32 .owner = THIS_MODULE,
33};
34
35struct sunxi_sid {
36 void __iomem *base;
37};
38
39
40
41
42
43
44static u8 sunxi_sid_read_byte(const struct sunxi_sid *sid,
45 const unsigned int offset)
46{
47 u32 sid_key;
48
49 sid_key = ioread32be(sid->base + round_down(offset, 4));
50 sid_key >>= (offset % 4) * 8;
51
52 return sid_key;
53}
54
55static int sunxi_sid_read(void *context, unsigned int offset,
56 void *val, size_t bytes)
57{
58 struct sunxi_sid *sid = context;
59 u8 *buf = val;
60
61 while (bytes--)
62 *buf++ = sunxi_sid_read_byte(sid, offset++);
63
64 return 0;
65}
66
67static int sunxi_sid_probe(struct platform_device *pdev)
68{
69 struct device *dev = &pdev->dev;
70 struct resource *res;
71 struct nvmem_device *nvmem;
72 struct sunxi_sid *sid;
73 int ret, i, size;
74 char *randomness;
75
76 sid = devm_kzalloc(dev, sizeof(*sid), GFP_KERNEL);
77 if (!sid)
78 return -ENOMEM;
79
80 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
81 sid->base = devm_ioremap_resource(dev, res);
82 if (IS_ERR(sid->base))
83 return PTR_ERR(sid->base);
84
85 size = resource_size(res) - 1;
86 econfig.size = resource_size(res);
87 econfig.dev = dev;
88 econfig.reg_read = sunxi_sid_read;
89 econfig.priv = sid;
90 nvmem = nvmem_register(&econfig);
91 if (IS_ERR(nvmem))
92 return PTR_ERR(nvmem);
93
94 randomness = kzalloc(sizeof(u8) * (size), GFP_KERNEL);
95 if (!randomness) {
96 ret = -EINVAL;
97 goto err_unreg_nvmem;
98 }
99
100 for (i = 0; i < size; i++)
101 randomness[i] = sunxi_sid_read_byte(sid, i);
102
103 add_device_randomness(randomness, size);
104 kfree(randomness);
105
106 platform_set_drvdata(pdev, nvmem);
107
108 return 0;
109
110err_unreg_nvmem:
111 nvmem_unregister(nvmem);
112 return ret;
113}
114
115static int sunxi_sid_remove(struct platform_device *pdev)
116{
117 struct nvmem_device *nvmem = platform_get_drvdata(pdev);
118
119 return nvmem_unregister(nvmem);
120}
121
122static const struct of_device_id sunxi_sid_of_match[] = {
123 { .compatible = "allwinner,sun4i-a10-sid" },
124 { .compatible = "allwinner,sun7i-a20-sid" },
125 {},
126};
127MODULE_DEVICE_TABLE(of, sunxi_sid_of_match);
128
129static struct platform_driver sunxi_sid_driver = {
130 .probe = sunxi_sid_probe,
131 .remove = sunxi_sid_remove,
132 .driver = {
133 .name = "eeprom-sunxi-sid",
134 .of_match_table = sunxi_sid_of_match,
135 },
136};
137module_platform_driver(sunxi_sid_driver);
138
139MODULE_AUTHOR("Oliver Schinagl <oliver@schinagl.nl>");
140MODULE_DESCRIPTION("Allwinner sunxi security id driver");
141MODULE_LICENSE("GPL");
142