2 * Allwinner sunXi SoCs Security ID support.
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.
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.
18 #include <linux/device.h>
20 #include <linux/module.h>
21 #include <linux/nvmem-provider.h>
23 #include <linux/platform_device.h>
24 #include <linux/slab.h>
25 #include <linux/random.h>
27 static struct nvmem_config econfig = {
39 /* We read the entire key, due to a 32 bit read alignment requirement. Since we
40 * want to return the requested byte, this results in somewhat slower code and
41 * uses 4 times more reads as needed but keeps code simpler. Since the SID is
42 * only very rarely probed, this is not really an issue.
44 static u8 sunxi_sid_read_byte(const struct sunxi_sid *sid,
45 const unsigned int offset)
49 sid_key = ioread32be(sid->base + round_down(offset, 4));
50 sid_key >>= (offset % 4) * 8;
52 return sid_key; /* Only return the last byte */
55 static int sunxi_sid_read(void *context, unsigned int offset,
56 void *val, size_t bytes)
58 struct sunxi_sid *sid = context;
62 *buf++ = sunxi_sid_read_byte(sid, offset++);
67 static int sunxi_sid_probe(struct platform_device *pdev)
69 struct device *dev = &pdev->dev;
71 struct nvmem_device *nvmem;
72 struct sunxi_sid *sid;
76 sid = devm_kzalloc(dev, sizeof(*sid), GFP_KERNEL);
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);
85 size = resource_size(res) - 1;
86 econfig.size = resource_size(res);
88 econfig.reg_read = sunxi_sid_read;
90 nvmem = nvmem_register(&econfig);
92 return PTR_ERR(nvmem);
94 randomness = kzalloc(sizeof(u8) * (size), GFP_KERNEL);
100 for (i = 0; i < size; i++)
101 randomness[i] = sunxi_sid_read_byte(sid, i);
103 add_device_randomness(randomness, size);
106 platform_set_drvdata(pdev, nvmem);
111 nvmem_unregister(nvmem);
115 static int sunxi_sid_remove(struct platform_device *pdev)
117 struct nvmem_device *nvmem = platform_get_drvdata(pdev);
119 return nvmem_unregister(nvmem);
122 static const struct of_device_id sunxi_sid_of_match[] = {
123 { .compatible = "allwinner,sun4i-a10-sid" },
124 { .compatible = "allwinner,sun7i-a20-sid" },
127 MODULE_DEVICE_TABLE(of, sunxi_sid_of_match);
129 static struct platform_driver sunxi_sid_driver = {
130 .probe = sunxi_sid_probe,
131 .remove = sunxi_sid_remove,
133 .name = "eeprom-sunxi-sid",
134 .of_match_table = sunxi_sid_of_match,
137 module_platform_driver(sunxi_sid_driver);
140 MODULE_DESCRIPTION("Allwinner sunxi security id driver");
141 MODULE_LICENSE("GPL");