Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris...
[linux/fpc-iii.git] / drivers / misc / eeprom / sunxi_sid.c
blob9c34e5704304f4e40dd33b4293347e6a2d6afa4f
1 /*
2 * Copyright (c) 2013 Oliver Schinagl <oliver@schinagl.nl>
3 * http://www.linux-sunxi.org
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * This driver exposes the Allwinner security ID, efuses exported in byte-
16 * sized chunks.
19 #include <linux/compiler.h>
20 #include <linux/device.h>
21 #include <linux/err.h>
22 #include <linux/export.h>
23 #include <linux/fs.h>
24 #include <linux/init.h>
25 #include <linux/io.h>
26 #include <linux/kernel.h>
27 #include <linux/kobject.h>
28 #include <linux/module.h>
29 #include <linux/of_device.h>
30 #include <linux/platform_device.h>
31 #include <linux/random.h>
32 #include <linux/slab.h>
33 #include <linux/stat.h>
34 #include <linux/sysfs.h>
35 #include <linux/types.h>
37 #define DRV_NAME "sunxi-sid"
39 struct sunxi_sid_data {
40 void __iomem *reg_base;
41 unsigned int keysize;
44 /* We read the entire key, due to a 32 bit read alignment requirement. Since we
45 * want to return the requested byte, this results in somewhat slower code and
46 * uses 4 times more reads as needed but keeps code simpler. Since the SID is
47 * only very rarely probed, this is not really an issue.
49 static u8 sunxi_sid_read_byte(const struct sunxi_sid_data *sid_data,
50 const unsigned int offset)
52 u32 sid_key;
54 if (offset >= sid_data->keysize)
55 return 0;
57 sid_key = ioread32be(sid_data->reg_base + round_down(offset, 4));
58 sid_key >>= (offset % 4) * 8;
60 return sid_key; /* Only return the last byte */
63 static ssize_t sid_read(struct file *fd, struct kobject *kobj,
64 struct bin_attribute *attr, char *buf,
65 loff_t pos, size_t size)
67 struct platform_device *pdev;
68 struct sunxi_sid_data *sid_data;
69 int i;
71 pdev = to_platform_device(kobj_to_dev(kobj));
72 sid_data = platform_get_drvdata(pdev);
74 if (pos < 0 || pos >= sid_data->keysize)
75 return 0;
76 if (size > sid_data->keysize - pos)
77 size = sid_data->keysize - pos;
79 for (i = 0; i < size; i++)
80 buf[i] = sunxi_sid_read_byte(sid_data, pos + i);
82 return i;
85 static struct bin_attribute sid_bin_attr = {
86 .attr = { .name = "eeprom", .mode = S_IRUGO, },
87 .read = sid_read,
90 static int sunxi_sid_remove(struct platform_device *pdev)
92 device_remove_bin_file(&pdev->dev, &sid_bin_attr);
93 dev_dbg(&pdev->dev, "driver unloaded\n");
95 return 0;
98 static const struct of_device_id sunxi_sid_of_match[] = {
99 { .compatible = "allwinner,sun4i-sid", .data = (void *)16},
100 { .compatible = "allwinner,sun7i-a20-sid", .data = (void *)512},
101 {/* sentinel */},
103 MODULE_DEVICE_TABLE(of, sunxi_sid_of_match);
105 static int sunxi_sid_probe(struct platform_device *pdev)
107 struct sunxi_sid_data *sid_data;
108 struct resource *res;
109 const struct of_device_id *of_dev_id;
110 u8 *entropy;
111 unsigned int i;
113 sid_data = devm_kzalloc(&pdev->dev, sizeof(struct sunxi_sid_data),
114 GFP_KERNEL);
115 if (!sid_data)
116 return -ENOMEM;
118 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
119 sid_data->reg_base = devm_ioremap_resource(&pdev->dev, res);
120 if (IS_ERR(sid_data->reg_base))
121 return PTR_ERR(sid_data->reg_base);
123 of_dev_id = of_match_device(sunxi_sid_of_match, &pdev->dev);
124 if (!of_dev_id)
125 return -ENODEV;
126 sid_data->keysize = (int)of_dev_id->data;
128 platform_set_drvdata(pdev, sid_data);
130 sid_bin_attr.size = sid_data->keysize;
131 if (device_create_bin_file(&pdev->dev, &sid_bin_attr))
132 return -ENODEV;
134 entropy = kzalloc(sizeof(u8) * sid_data->keysize, GFP_KERNEL);
135 for (i = 0; i < sid_data->keysize; i++)
136 entropy[i] = sunxi_sid_read_byte(sid_data, i);
137 add_device_randomness(entropy, sid_data->keysize);
138 kfree(entropy);
140 dev_dbg(&pdev->dev, "loaded\n");
142 return 0;
145 static struct platform_driver sunxi_sid_driver = {
146 .probe = sunxi_sid_probe,
147 .remove = sunxi_sid_remove,
148 .driver = {
149 .name = DRV_NAME,
150 .owner = THIS_MODULE,
151 .of_match_table = sunxi_sid_of_match,
154 module_platform_driver(sunxi_sid_driver);
156 MODULE_AUTHOR("Oliver Schinagl <oliver@schinagl.nl>");
157 MODULE_DESCRIPTION("Allwinner sunxi security id driver");
158 MODULE_LICENSE("GPL");